Hex Artifact Content
Not logged in

Artifact 116d407316de66fc6b8e7383a78c44c05dc16f71:

File src/sqlite3.c part of check-in [d21b90e365] - Update the built-in SQLite version to 3.6.14.2. by drh on 2009-05-25 14:25:47.

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 31 34 2e 32 2e  ersion 3.6.14.2.
00b0: 20 20 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61    By combining a
00c0: 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61  ll the individua
00d0: 6c 20 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69  l C code files i
00e0: 6e 74 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e  nto this .** sin
00f0: 67 6c 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20  gle large file, 
0100: 74 68 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20  the entire code 
0110: 63 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20  can be compiled 
0120: 61 73 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61  as a one transla
0130: 74 69 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54  tion.** unit.  T
0140: 68 69 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20  his allows many 
0150: 63 6f 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20  compilers to do 
0160: 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68  optimizations th
0170: 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a  at would not be.
0180: 2a 2a 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74  ** possible if t
0190: 68 65 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f  he files were co
01a0: 6d 70 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c  mpiled separatel
01b0: 79 2e 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20  y.  Performance 
01c0: 69 6d 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20  improvements.** 
01d0: 6f 66 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61  of 5% are more a
01e0: 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e  re commonly seen
01f0: 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20   when SQLite is 
0200: 63 6f 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69  compiled as a si
0210: 6e 67 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74  ngle.** translat
0220: 69 6f 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20  ion unit..**.** 
0230: 54 68 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c  This file is all
0240: 20 79 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d   you need to com
0250: 70 69 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f  pile SQLite.  To
0260: 20 75 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f   use SQLite in o
0270: 74 68 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73  ther.** programs
0280: 2c 20 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20  , you need this 
0290: 66 69 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71  file and the "sq
02a0: 6c 69 74 65 33 2e 68 22 20 68 65 61 64 65 72 20  lite3.h" header 
02b0: 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65  file that define
02c0: 73 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d  s.** the program
02d0: 6d 69 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74  ming interface t
02e0: 6f 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  o the SQLite lib
02f0: 72 61 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64  rary.  (If you d
0300: 6f 20 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74  o not have .** t
0310: 68 65 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68  he "sqlite3.h" h
0320: 65 61 64 65 72 20 66 69 6c 65 20 61 74 20 68 61  eader file at ha
0330: 6e 64 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e  nd, you will fin
0340: 64 20 61 20 63 6f 70 79 20 69 6e 20 74 68 65 20  d a copy in the 
0350: 66 69 72 73 74 0a 2a 2a 20 35 35 33 33 20 6c 69  first.** 5533 li
0360: 6e 65 73 20 70 61 73 74 20 74 68 69 73 20 68 65  nes past this he
0370: 61 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 29 20 20  ader comment.)  
0380: 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 20  Additional code 
0390: 66 69 6c 65 73 20 6d 61 79 20 62 65 0a 2a 2a 20  files may be.** 
03a0: 6e 65 65 64 65 64 20 69 66 20 79 6f 75 20 77 61  needed if you wa
03b0: 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f 20  nt a wrapper to 
03c0: 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74 65  interface SQLite
03d0: 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69 63   with your choic
03e0: 65 20 6f 66 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d  e of.** programm
03f0: 69 6e 67 20 6c 61 6e 67 75 61 67 65 2e 20 20 54  ing language.  T
0400: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  he code for the 
0410: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e  "sqlite3" comman
0420: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 0a 2a 2a 20  d-line shell.** 
0430: 69 73 20 61 6c 73 6f 20 69 6e 20 61 20 73 65 70  is also in a sep
0440: 61 72 61 74 65 20 66 69 6c 65 2e 20 20 54 68 69  arate file.  Thi
0450: 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
0460: 6f 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68  only code for th
0470: 65 20 63 6f 72 65 0a 2a 2a 20 53 51 4c 69 74 65  e core.** SQLite
0480: 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20   library..**.** 
0490: 54 68 69 73 20 61 6d 61 6c 67 61 6d 61 74 69 6f  This amalgamatio
04a0: 6e 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20  n was generated 
04b0: 6f 6e 20 32 30 30 39 2d 30 35 2d 32 35 20 31 32  on 2009-05-25 12
04c0: 3a 33 34 3a 33 31 20 55 54 43 2e 0a 2a 2f 0a 23  :34:31 UTC..*/.#
04d0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
04e0: 52 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c  RE 1.#define SQL
04f0: 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
0500: 20 31 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   1.#ifndef SQLIT
0510: 45 5f 50 52 49 56 41 54 45 0a 23 20 64 65 66 69  E_PRIVATE.# defi
0520: 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ne SQLITE_PRIVAT
0530: 45 20 73 74 61 74 69 63 0a 23 65 6e 64 69 66 0a  E static.#endif.
0540: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41  #ifndef SQLITE_A
0550: 50 49 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  PI.# define SQLI
0560: 54 45 5f 41 50 49 0a 23 65 6e 64 69 66 0a 2f 2a  TE_API.#endif./*
0570: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
0580: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 49  gin file sqliteI
0590: 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.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 2a 2a 2a 2f 0a 2f 2a  ************/./*
05c0: 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
05d0: 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
05e0: 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
05f0: 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
0600: 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
0610: 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
0620: 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
0630: 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
0640: 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
0650: 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
0660: 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
0670: 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
0680: 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
0690: 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
06a0: 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
06b0: 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
06c0: 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
06d0: 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
06e0: 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
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 2a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c  ****.** Internal
0740: 20 69 6e 74 65 72 66 61 63 65 20 64 65 66 69 6e   interface defin
0750: 69 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74  itions for SQLit
0760: 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  e..**.** @(#) $I
0770: 64 3a 20 73 71 6c 69 74 65 49 6e 74 2e 68 2c 76  d: sqliteInt.h,v
0780: 20 31 2e 38 36 38 20 32 30 30 39 2f 30 35 2f 30   1.868 2009/05/0
0790: 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65  4 11:42:30 danie
07a0: 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
07b0: 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 49  #ifndef _SQLITEI
07c0: 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53  NT_H_.#define _S
07d0: 51 4c 49 54 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a  QLITEINT_H_../*.
07e0: 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 20 63  ** Include the c
07f0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 65 61  onfiguration hea
0800: 64 65 72 20 6f 75 74 70 75 74 20 62 79 20 27 63  der output by 'c
0810: 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77 65 27  onfigure' if we'
0820: 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  re using the.** 
0830: 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 20 62  autoconf-based b
0840: 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 20 5f  uild.*/.#ifdef _
0850: 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46  HAVE_SQLITE_CONF
0860: 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 22 63  IG_H.#include "c
0870: 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 66 0a  onfig.h".#endif.
0880: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
0890: 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 4c   Include sqliteL
08a0: 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 6d 69  imit.h in the mi
08b0: 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
08c0: 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  t.h ***********/
08d0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
08e0: 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69   Begin file sqli
08f0: 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a  teLimit.h ******
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0910: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
0920: 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20  ./*.** 2007 May 
0930: 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
0940: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
0950: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
0960: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
0970: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
0980: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
0990: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
09a0: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
09b0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
09c0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
09d0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
09e0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
09f0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
0a00: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
0a10: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
0a20: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
0a30: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
0a40: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a90: 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c  .** .** This fil
0aa0: 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75  e defines variou
0ab0: 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74  s limits of what
0ac0: 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63   SQLite can proc
0ad0: 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20  ess..**.** @(#) 
0ae0: 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d 69 74  $Id: sqliteLimit
0af0: 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39 2f 30  .h,v 1.10 2009/0
0b00: 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20 64 61  1/10 16:15:09 da
0b10: 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
0b20: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  */../*.** The ma
0b30: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
0b40: 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 69  a TEXT or BLOB i
0b50: 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69 73 20  n bytes.   This 
0b60: 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 20 74  also.** limits t
0b70: 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72 6f 77  he size of a row
0b80: 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72 20 69   in a table or i
0b90: 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ndex..**.** The 
0ba0: 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20 74 68  hard limit is th
0bb0: 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61 20 33  e ability of a 3
0bc0: 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  2-bit signed int
0bd0: 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74  eger.** to count
0be0: 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33 31 2d   the size: 2^31-
0bf0: 31 20 6f 72 20 32 31 34 37 34 38 33 36 34 37 2e  1 or 2147483647.
0c00: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
0c10: 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20  TE_MAX_LENGTH.# 
0c20: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
0c30: 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30  X_LENGTH 1000000
0c40: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  000.#endif../*.*
0c50: 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 6d 61  * This is the ma
0c60: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a  ximum number of.
0c70: 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d  **.**    * Colum
0c80: 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a 2a 2a  ns in a table.**
0c90: 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e      * Columns in
0ca0: 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20   an index.**    
0cb0: 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 76  * Columns in a v
0cc0: 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d  iew.**    * Term
0cd0: 73 20 69 6e 20 74 68 65 20 53 45 54 20 63 6c 61  s in the SET cla
0ce0: 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 54 45  use of an UPDATE
0cf0: 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20   statement.**   
0d00: 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20   * Terms in the 
0d10: 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20  result set of a 
0d20: 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
0d30: 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69  .**    * Terms i
0d40: 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 6f  n the GROUP BY o
0d50: 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  r ORDER BY claus
0d60: 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  es of a SELECT s
0d70: 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20  tatement..**    
0d80: 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 56  * Terms in the V
0d90: 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f 66 20  ALUES clause of 
0da0: 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  an INSERT statem
0db0: 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61  ent.**.** The ha
0dc0: 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 20 68  rd upper limit h
0dd0: 65 72 65 20 69 73 20 33 32 36 37 36 2e 20 20 4d  ere is 32676.  M
0de0: 6f 73 74 20 64 61 74 61 62 61 73 65 20 70 65 6f  ost database peo
0df0: 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 6c 6c  ple will.** tell
0e00: 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61 20 77   you that in a w
0e10: 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 20 64  ell-normalized d
0e20: 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75 73 75  atabase, you usu
0e30: 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e  ally should.** n
0e40: 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74 68 61  ot have more tha
0e50: 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73 6f 20  n a dozen or so 
0e60: 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 20 74  columns in any t
0e70: 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a 2a 2a  able.  And if.**
0e80: 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61 73   that is the cas
0e90: 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70  e, there is no p
0ea0: 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67 20 6d  oint in having m
0eb0: 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77 0a 2a  ore than a few.*
0ec0: 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73 20 69  * dozen values i
0ed0: 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68  n any of the oth
0ee0: 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20 64 65  er situations de
0ef0: 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a  scribed above..*
0f00: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
0f10: 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 64 65  _MAX_COLUMN.# de
0f20: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
0f30: 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65 6e 64  COLUMN 2000.#end
0f40: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
0f50: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
0f60: 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61  a single SQL sta
0f70: 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65 73 2e  tement in bytes.
0f80: 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64 20 74  .**.** It used t
0f90: 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  o be the case th
0fa0: 61 74 20 73 65 74 74 69 6e 67 20 74 68 69 73 20  at setting this 
0fb0: 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20 77 6f  value to zero wo
0fc0: 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68 65 20  uld.** turn the 
0fd0: 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68 61 74  limit off.  That
0fe0: 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72   is no longer tr
0ff0: 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 70  ue.  It is not p
1000: 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 74 75  ossible.** to tu
1010: 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20 6f 66  rn this limit of
1020: 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  f..*/.#ifndef SQ
1030: 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e  LITE_MAX_SQL_LEN
1040: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  GTH.# define SQL
1050: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
1060: 54 48 20 31 30 30 30 30 30 30 30 30 30 0a 23 65  TH 1000000000.#e
1070: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
1080: 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66  maximum depth of
1090: 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
10a0: 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c 69 6d  ree. This is lim
10b0: 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f 6d 65  ited to .** some
10c0: 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c 49 54   extent by SQLIT
10d0: 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48  E_MAX_SQL_LENGTH
10e0: 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 20 79  . But sometime y
10f0: 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77 61 6e  ou might .** wan
1100: 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72 65 20  t to place more 
1110: 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20 6f 6e  severe limits on
1120: 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74 79 20   the complexity 
1130: 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72 65 73  of an .** expres
1140: 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61  sion..**.** A va
1150: 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20 74 6f  lue of 0 used to
1160: 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c   mean that the l
1170: 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65 6e 66  imit was not enf
1180: 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20 74 68  orced..** But th
1190: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  at is no longer 
11a0: 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d 69 74  true.  The limit
11b0: 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74 6c 79   is now strictly
11c0: 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61 74 20   enforced.** at 
11d0: 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69  all times..*/.#i
11e0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
11f0: 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20 64 65  _EXPR_DEPTH.# de
1200: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
1210: 45 58 50 52 5f 44 45 50 54 48 20 31 30 30 30 0a  EXPR_DEPTH 1000.
1220: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
1230: 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1240: 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63   of terms in a c
1250: 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73  ompound SELECT s
1260: 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65  tatement..** The
1270: 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
1280: 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  for compound SEL
1290: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 64  ECT statements d
12a0: 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76 65 6c  oes one.** level
12b0: 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f   of recursion fo
12c0: 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20 41 20  r each term.  A 
12d0: 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 63  stack overflow c
12e0: 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69 66 20  an result.** if 
12f0: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  the number of te
1300: 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72 67 65  rms is too large
1310: 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20  .  In practice, 
1320: 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65 76 65  most SQL.** neve
1330: 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20  r has more than 
1340: 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20 20 55  3 or 4 terms.  U
1350: 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20 30 20  se a value of 0 
1360: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 61 6e  to disable.** an
1370: 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e  y limit on the n
1380: 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
1390: 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53 45 4c  n a compount SEL
13a0: 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ECT..*/.#ifndef 
13b0: 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f  SQLITE_MAX_COMPO
13c0: 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64 65 66  UND_SELECT.# def
13d0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43  ine SQLITE_MAX_C
13e0: 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 35  OMPOUND_SELECT 5
13f0: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1400: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
1410: 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 69  ber of opcodes i
1420: 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d  n a VDBE program
1430: 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74  ..** Not current
1440: 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2f 0a  ly enforced..*/.
1450: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1460: 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64 65 66  AX_VDBE_OP.# def
1470: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56  ine SQLITE_MAX_V
1480: 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23 65 6e  DBE_OP 25000.#en
1490: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  dif../*.** The m
14a0: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
14b0: 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 6e   arguments to an
14c0: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   SQL function..*
14d0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
14e0: 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  _MAX_FUNCTION_AR
14f0: 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  G.# define SQLIT
1500: 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41  E_MAX_FUNCTION_A
1510: 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a 0a 2f  RG 127.#endif../
1520: 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
1530: 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65   number of in-me
1540: 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20 75 73  mory pages to us
1550: 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  e for the main d
1560: 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65  atabase.** table
1570: 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f 72 61   and for tempora
1580: 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20  ry tables.  The 
1590: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43  SQLITE_DEFAULT_C
15a0: 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23 69 66  ACHE_SIZE.*/.#if
15b0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
15c0: 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23  ULT_CACHE_SIZE.#
15d0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
15e0: 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
15f0: 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66 0a 23  E  2000.#endif.#
1600: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
1610: 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45  FAULT_TEMP_CACHE
1620: 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53  _SIZE.# define S
1630: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45  QLITE_DEFAULT_TE
1640: 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 35  MP_CACHE_SIZE  5
1650: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1660: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
1670: 62 65 72 20 6f 66 20 61 74 74 61 63 68 65 64 20  ber of attached 
1680: 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69 73  databases.  This
1690: 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e   must be between
16a0: 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20 20 54   0.** and 30.  T
16b0: 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f  he upper bound o
16c0: 6e 20 33 30 20 69 73 20 62 65 63 61 75 73 65 20  n 30 is because 
16d0: 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  a 32-bit integer
16e0: 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20 75 73   bitmap.** is us
16f0: 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f  ed internally to
1700: 20 74 72 61 63 6b 20 61 74 74 61 63 68 65 64 20   track attached 
1710: 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69  databases..*/.#i
1720: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
1730: 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65 66 69  _ATTACHED.# defi
1740: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54  ne SQLITE_MAX_AT
1750: 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64 69 66  TACHED 10.#endif
1760: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  .../*.** The max
1770: 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 20  imum value of a 
1780: 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 74 68  ?nnn wildcard th
1790: 61 74 20 74 68 65 20 70 61 72 73 65 72 20 77 69  at the parser wi
17a0: 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a 23 69  ll accept..*/.#i
17b0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
17c0: 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
17d0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
17e0: 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55  _MAX_VARIABLE_NU
17f0: 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69 66 0a  MBER 999.#endif.
1800: 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61 67 65  ./* Maximum page
1810: 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70 70 65   size.  The uppe
1820: 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 20  r bound on this 
1830: 76 61 6c 75 65 20 69 73 20 33 32 37 36 38 2e 20  value is 32768. 
1840: 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a 2a 2a   This a limit.**
1850: 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68 65 20   imposed by the 
1860: 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73 74 6f  necessity of sto
1870: 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69  ring the value i
1880: 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73 69 67  n a 2-byte unsig
1890: 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61  ned integer.** a
18a0: 6e 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74  nd the fact that
18b0: 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d   the page size m
18c0: 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f  ust be a power o
18d0: 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  f 2..**.** If th
18e0: 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68 61 6e  is limit is chan
18f0: 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  ged, then the co
1900: 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79 20 69  mpiled library i
1910: 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a  s technically.**
1920: 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 69   incompatible wi
1930: 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c 69 62  th an SQLite lib
1940: 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20 77 69  rary compiled wi
1950: 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 6c  th a different l
1960: 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20 70 72  imit. If.** a pr
1970: 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e 67 20  ocess operating 
1980: 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 77 69  on a database wi
1990: 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65 20 6f  th a page-size o
19a0: 66 20 36 35 35 33 36 20 62 79 74 65 73 20 0a 2a  f 65536 bytes .*
19b0: 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65 6e 20  * crashes, then 
19c0: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 53  an instance of S
19d0: 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64 20 77  QLite compiled w
19e0: 69 74 68 20 74 68 65 20 64 65 66 61 75 6c 74 20  ith the default 
19f0: 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20 6c 69  page-size .** li
1a00: 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  mit will not be 
1a10: 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  able to rollback
1a20: 20 74 68 65 20 61 62 6f 72 74 65 64 20 74 72 61   the aborted tra
1a30: 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 63  nsaction. This c
1a40: 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74 6f 20  ould.** lead to 
1a50: 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
1a60: 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ion..*/.#ifndef 
1a70: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1a80: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
1a90: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
1aa0: 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69 66 0a  ZE 32768.#endif.
1ab0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ../*.** The defa
1ac0: 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64 61  ult size of a da
1ad0: 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a 2f 0a  tabase page..*/.
1ae0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
1af0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1b00: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1b10: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
1b20: 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 23  ZE 1024.#endif.#
1b30: 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  if SQLITE_DEFAUL
1b40: 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49  T_PAGE_SIZE>SQLI
1b50: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
1b60: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
1b70: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
1b80: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
1b90: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  E_DEFAULT_PAGE_S
1ba0: 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  IZE SQLITE_MAX_P
1bb0: 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a  AGE_SIZE.#endif.
1bc0: 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72 69 6c  ./*.** Ordinaril
1bd0: 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65 20 69  y, if no value i
1be0: 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70 72 6f  s explicitly pro
1bf0: 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20 63 72  vided, SQLite cr
1c00: 65 61 74 65 73 20 64 61 74 61 62 61 73 65 73 0a  eates databases.
1c10: 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73 69 7a  ** with page siz
1c20: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
1c30: 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f 77 65  _PAGE_SIZE. Howe
1c40: 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20 63 65  ver, based on ce
1c50: 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63 65 20  rtain.** device 
1c60: 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  characteristics 
1c70: 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 6e 64  (sector-size and
1c80: 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28 29 20   atomic write() 
1c90: 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c  support),.** SQL
1ca0: 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65 20 61  ite may choose a
1cb0: 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e 20 54   larger value. T
1cc0: 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69 73 20  his constant is 
1cd0: 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75  the maximum valu
1ce0: 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c  e.** SQLite will
1cf0: 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73 20 6f   choose on its o
1d00: 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  wn..*/.#ifndef S
1d10: 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c  QLITE_MAX_DEFAUL
1d20: 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65  T_PAGE_SIZE.# de
1d30: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
1d40: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
1d50: 45 20 38 31 39 32 0a 23 65 6e 64 69 66 0a 23 69  E 8192.#endif.#i
1d60: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  f SQLITE_MAX_DEF
1d70: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53  AULT_PAGE_SIZE>S
1d80: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
1d90: 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  IZE.# undef SQLI
1da0: 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50  TE_MAX_DEFAULT_P
1db0: 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e  AGE_SIZE.# defin
1dc0: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  e SQLITE_MAX_DEF
1dd0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 53  AULT_PAGE_SIZE S
1de0: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
1df0: 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  IZE.#endif.../*.
1e00: 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  ** Maximum numbe
1e10: 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 6f 6e  r of pages in on
1e20: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1e30: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 72  .**.** This is r
1e40: 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 20 64  eally just the d
1e50: 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
1e60: 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f 63 6f   the max_page_co
1e70: 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a 20 54  unt pragma..** T
1e80: 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20 62 65  his value can be
1e90: 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72 61 69   lowered (or rai
1ea0: 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69 6d 65  sed) at run-time
1eb0: 20 75 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a   using that the.
1ec0: 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e  ** max_page_coun
1ed0: 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e  t macro..*/.#ifn
1ee0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  def SQLITE_MAX_P
1ef0: 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65 66 69  AGE_COUNT.# defi
1f00: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ne SQLITE_MAX_PA
1f10: 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37 34 31  GE_COUNT 1073741
1f20: 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  823.#endif../*.*
1f30: 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
1f40: 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74   (in bytes) of t
1f50: 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20  he pattern in a 
1f60: 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20  LIKE or GLOB.** 
1f70: 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66  operator..*/.#if
1f80: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1f90: 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
1fa0: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  GTH.# define SQL
1fb0: 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54  ITE_MAX_LIKE_PAT
1fc0: 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30  TERN_LENGTH 5000
1fd0: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  0.#endif../*****
1fe0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1ff0: 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a   sqliteLimit.h *
2000: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2020: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
2040: 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
2050: 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
2060: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
2070: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69  ********/../* Di
2080: 73 61 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77  sable nuisance w
2090: 61 72 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61  arnings on Borla
20a0: 6e 64 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a  nd compilers */.
20b0: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f  #if defined(__BO
20c0: 52 4c 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d  RLANDC__).#pragm
20d0: 61 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75  a warn -rch /* u
20e0: 6e 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20  nreachable code 
20f0: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20  */.#pragma warn 
2100: 2d 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f  -ccc /* Conditio
2110: 6e 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65  n is always true
2120: 20 6f 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72   or false */.#pr
2130: 61 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f  agma warn -aus /
2140: 2a 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65  * Assigned value
2150: 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a   is never used *
2160: 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d  /.#pragma warn -
2170: 63 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67  csu /* Comparing
2180: 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69   signed and unsi
2190: 67 6e 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20  gned */.#pragma 
21a0: 77 61 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73  warn -spa /* Sus
21b0: 70 69 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20  picious pointer 
21c0: 61 72 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65  arithmetic */.#e
21d0: 6e 64 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20  ndif../* Needed 
21e0: 66 6f 72 20 76 61 72 69 6f 75 73 20 64 65 66 69  for various defi
21f0: 6e 69 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69  nitions... */.#i
2200: 66 6e 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43  fndef _GNU_SOURC
2210: 45 0a 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f  E.# define _GNU_
2220: 53 4f 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f  SOURCE.#endif../
2230: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61  *.** Include sta
2240: 6e 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c  ndard header fil
2250: 65 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a  es as necessary.
2260: 2a 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53  */.#ifdef HAVE_S
2270: 54 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65  TDINT_H.#include
2280: 20 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64   <stdint.h>.#end
2290: 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49  if.#ifdef HAVE_I
22a0: 4e 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75  NTTYPES_H.#inclu
22b0: 64 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a  de <inttypes.h>.
22c0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68  #endif../*. * Th
22d0: 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  is macro is used
22e0: 20 74 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20   to "hide" some 
22f0: 75 67 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74  ugliness in cast
2300: 69 6e 67 20 61 6e 20 69 6e 74 0a 20 2a 20 76 61  ing an int. * va
2310: 6c 75 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c  lue to a ptr val
2320: 75 65 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56  ue under the MSV
2330: 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65  C 64-bit compile
2340: 72 2e 20 20 20 43 61 73 74 69 6e 67 0a 20 2a 20  r.   Casting. * 
2350: 6e 6f 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65  non 64-bit value
2360: 73 20 74 6f 20 70 74 72 20 74 79 70 65 73 20 72  s to ptr types r
2370: 65 73 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72  esults in a "har
2380: 64 22 20 65 72 72 6f 72 20 77 69 74 68 20 0a 20  d" error with . 
2390: 2a 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69  * the MSVC 64-bi
23a0: 74 20 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68  t compiler which
23b0: 20 74 68 69 73 20 61 74 74 65 6d 70 74 73 20 74   this attempts t
23c0: 6f 20 61 76 6f 69 64 2e 20 20 0a 20 2a 0a 20 2a  o avoid.  . *. *
23d0: 20 41 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c   A simple compil
23e0: 65 72 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73  er pragma or cas
23f0: 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f  ting sequence co
2400: 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64  uld not be found
2410: 0a 20 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74  . * to correct t
2420: 68 69 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61  his in all situa
2430: 74 69 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d  tions, so this m
2440: 61 63 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75  acro was introdu
2450: 63 65 64 2e 0a 20 2a 0a 20 2a 20 49 74 20 63 6f  ced.. *. * It co
2460: 75 6c 64 20 62 65 20 61 72 67 75 65 64 20 74 68  uld be argued th
2470: 61 74 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20  at the intptr_t 
2480: 74 79 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73  type could be us
2490: 65 64 20 69 6e 20 74 68 69 73 0a 20 2a 20 63 61  ed in this. * ca
24a0: 73 65 2c 20 62 75 74 20 74 68 61 74 20 74 79 70  se, but that typ
24b0: 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  e is not availab
24c0: 6c 65 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c  le on all compil
24d0: 65 72 73 2c 20 6f 72 20 0a 20 2a 20 72 65 71 75  ers, or . * requ
24e0: 69 72 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64  ires the #includ
24f0: 65 20 6f 66 20 73 70 65 63 69 66 69 63 20 68 65  e of specific he
2500: 61 64 65 72 73 20 77 68 69 63 68 20 64 69 66 66  aders which diff
2510: 65 72 73 20 62 65 74 77 65 65 6e 0a 20 2a 20 70  ers between. * p
2520: 6c 61 74 66 6f 72 6d 73 2e 0a 20 2a 2f 0a 23 64  latforms.. */.#d
2530: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54  efine SQLITE_INT
2540: 5f 54 4f 5f 50 54 52 28 58 29 20 20 20 28 28 76  _TO_PTR(X)   ((v
2550: 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29  oid*)&((char*)0)
2560: 5b 58 5d 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  [X]).#define SQL
2570: 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58  ITE_PTR_TO_INT(X
2580: 29 20 20 20 28 28 69 6e 74 29 28 28 28 63 68 61  )   ((int)(((cha
2590: 72 2a 29 58 29 2d 28 63 68 61 72 2a 29 30 29 29  r*)X)-(char*)0))
25a0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64  ../*.** These #d
25b0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e  efines should en
25c0: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73  able >2GB file s
25d0: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20  upport on POSIX 
25e0: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c  if the.** underl
25f0: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
2600: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69  ystem supports i
2610: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61  t.  If the OS la
2620: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  cks.** large fil
2630: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66  e support, or if
2640: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f   the OS is windo
2650: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64  ws, these should
2660: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a   be no-ops..**.*
2670: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20  * Ticket #2739: 
2680: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f   The _LARGEFILE_
2690: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73  SOURCE macro mus
26a0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20  t appear before 
26b0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69  any.** system #i
26c0: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c  ncludes.  Hence,
26d0: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63   this block of c
26e0: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20  ode must be the 
26f0: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f  very first.** co
2700: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65  de in all source
2710: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61   files..**.** La
2720: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
2730: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64   can be disabled
2740: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c   using the -DSQL
2750: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20  ITE_DISABLE_LFS 
2760: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65  switch.** on the
2770: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
2780: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73  d line.  This is
2790: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f   necessary if yo
27a0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a  u are compiling.
27b0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d  ** on a recent m
27c0: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20  achine (ex: Red 
27d0: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75  Hat 7.2) but you
27e0: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20   want your code 
27f0: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e  to work.** on an
2800: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28   older machine (
2810: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29  ex: Red Hat 6.0)
2820: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c  .  If you compil
2830: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32  e on Red Hat 7.2
2840: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73  .** without this
2850: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20   option, LFS is 
2860: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53  enable.  But LFS
2870: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
2880: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a  in the kernel.**
2890: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c   in Red Hat 6.0,
28a0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e   so the code won
28b0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c  't work.  Hence,
28c0: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e   for maximum bin
28d0: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69  ary.** portabili
28e0: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d  ty you should om
28f0: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69  it LFS..**.** Si
2900: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f  milar is true fo
2910: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53  r Mac OS X.  LFS
2920: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74   is only support
2930: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39  ed on Mac OS X 9
2940: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23   and later..*/.#
2950: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49  ifndef SQLITE_DI
2960: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69  SABLE_LFS.# defi
2970: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20  ne _LARGE_FILE  
2980: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20       1.# ifndef 
2990: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54  _FILE_OFFSET_BIT
29a0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49  S.#   define _FI
29b0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36  LE_OFFSET_BITS 6
29c0: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69  4.# endif.# defi
29d0: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f  ne _LARGEFILE_SO
29e0: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a  URCE 1.#endif...
29f0: 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  /*.** The SQLITE
2a00: 5f 54 48 52 45 41 44 53 41 46 45 20 6d 61 63 72  _THREADSAFE macr
2a10: 6f 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65  o must be define
2a20: 64 20 61 73 20 65 69 74 68 65 72 20 30 20 6f 72  d as either 0 or
2a30: 20 31 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72   1..** Older ver
2a40: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
2a50: 75 73 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c  used an optional
2a60: 20 54 48 52 45 41 44 53 41 46 45 20 6d 61 63 72   THREADSAFE macr
2a70: 6f 2e 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74  o..** We support
2a80: 20 74 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79   that for legacy
2a90: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
2aa0: 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  (SQLITE_THREADSA
2ab0: 46 45 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28  FE).#if defined(
2ac0: 54 48 52 45 41 44 53 41 46 45 29 0a 23 20 64 65  THREADSAFE).# de
2ad0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45  fine SQLITE_THRE
2ae0: 41 44 53 41 46 45 20 54 48 52 45 41 44 53 41 46  ADSAFE THREADSAF
2af0: 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  E.#else.# define
2b00: 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
2b10: 46 45 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64  FE 1.#endif.#end
2b20: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51  if../*.** The SQ
2b30: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d  LITE_DEFAULT_MEM
2b40: 53 54 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73  STATUS macro mus
2b50: 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 73 20  t be defined as 
2b60: 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a  either 0 or 1..*
2b70: 2a 20 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20  * It determines 
2b80: 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
2b90: 68 65 20 66 65 61 74 75 72 65 73 20 72 65 6c 61  he features rela
2ba0: 74 65 64 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54  ted to .** SQLIT
2bb0: 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54  E_CONFIG_MEMSTAT
2bc0: 55 53 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  US are available
2bd0: 20 62 79 20 64 65 66 61 75 6c 74 20 6f 72 20 6e   by default or n
2be0: 6f 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 63  ot. This value c
2bf0: 61 6e 0a 2a 2a 20 62 65 20 6f 76 65 72 72 69 64  an.** be overrid
2c00: 64 65 6e 20 61 74 20 72 75 6e 74 69 6d 65 20 75  den at runtime u
2c10: 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  sing the sqlite3
2c20: 5f 63 6f 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a  _config() API..*
2c30: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
2c40: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45  QLITE_DEFAULT_ME
2c50: 4d 53 54 41 54 55 53 29 0a 23 20 64 65 66 69 6e  MSTATUS).# defin
2c60: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
2c70: 5f 4d 45 4d 53 54 41 54 55 53 20 31 0a 23 65 6e  _MEMSTATUS 1.#en
2c80: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74  dif../*.** Exact
2c90: 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  ly one of the fo
2ca0: 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d  llowing macros m
2cb0: 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 69  ust be defined i
2cc0: 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70  n order to.** sp
2cd0: 65 63 69 66 79 20 77 68 69 63 68 20 6d 65 6d 6f  ecify which memo
2ce0: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
2cf0: 62 73 79 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a  bsystem to use..
2d00: 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  **.**     SQLITE
2d10: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20  _SYSTEM_MALLOC  
2d20: 20 20 20 20 20 20 20 20 2f 2f 20 55 73 65 20 6e          // Use n
2d30: 6f 72 6d 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c  ormal system mal
2d40: 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c  loc().**     SQL
2d50: 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 20 20 20  ITE_MEMDEBUG    
2d60: 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 44 65             // De
2d70: 62 75 67 67 69 6e 67 20 76 65 72 73 69 6f 6e 20  bugging version 
2d80: 6f 66 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63  of system malloc
2d90: 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  ().**     SQLITE
2da0: 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20  _MEMORY_SIZE    
2db0: 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72          // inter
2dc0: 6e 61 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31  nal allocator #1
2dd0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d  .**     SQLITE_M
2de0: 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 20 20 20  MAP_HEAP_SIZE   
2df0: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61        // interna
2e00: 6c 20 6d 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74  l mmap() allocat
2e10: 6f 72 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  or.**     SQLITE
2e20: 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a  _POW2_MEMORY_SIZ
2e30: 45 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72  E       // inter
2e40: 6e 61 6c 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f  nal power-of-two
2e50: 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a   allocator.**.**
2e60: 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   If none of the 
2e70: 61 62 6f 76 65 20 61 72 65 20 64 65 66 69 6e 65  above are define
2e80: 64 2c 20 74 68 65 6e 20 73 65 74 20 53 51 4c 49  d, then set SQLI
2e90: 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43  TE_SYSTEM_MALLOC
2ea0: 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75   as.** the defau
2eb0: 6c 74 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  lt..*/.#if defin
2ec0: 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ed(SQLITE_SYSTEM
2ed0: 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64  _MALLOC)+defined
2ee0: 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47  (SQLITE_MEMDEBUG
2ef0: 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28  )+\.    defined(
2f00: 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49  SQLITE_MEMORY_SI
2f10: 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49  ZE)+defined(SQLI
2f20: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a  TE_MMAP_HEAP_SIZ
2f30: 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64  E)+\.    defined
2f40: 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d  (SQLITE_POW2_MEM
2f50: 4f 52 59 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72  ORY_SIZE)>1.# er
2f60: 72 6f 72 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65  ror "At most one
2f70: 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2f80: 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63  g compile-time c
2f90: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
2fa0: 69 6f 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73  ions\. is allows
2fb0: 3a 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f  : SQLITE_SYSTEM_
2fc0: 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d  MALLOC, SQLITE_M
2fd0: 45 4d 44 45 42 55 47 2c 20 53 51 4c 49 54 45 5f  EMDEBUG, SQLITE_
2fe0: 4d 45 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53  MEMORY_SIZE,\. S
2ff0: 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f  QLITE_MMAP_HEAP_
3000: 53 49 5a 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57  SIZE, SQLITE_POW
3010: 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23  2_MEMORY_SIZE".#
3020: 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65  endif.#if define
3030: 64 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f  d(SQLITE_SYSTEM_
3040: 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28  MALLOC)+defined(
3050: 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29  SQLITE_MEMDEBUG)
3060: 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53  +\.    defined(S
3070: 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a  QLITE_MEMORY_SIZ
3080: 45 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54  E)+defined(SQLIT
3090: 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45  E_MMAP_HEAP_SIZE
30a0: 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28  )+\.    defined(
30b0: 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f  SQLITE_POW2_MEMO
30c0: 52 59 5f 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65  RY_SIZE)==0.# de
30d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 53 54  fine SQLITE_SYST
30e0: 45 4d 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64  EM_MALLOC 1.#end
30f0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c  if../*.** If SQL
3100: 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f  ITE_MALLOC_SOFT_
3110: 4c 49 4d 49 54 20 69 73 20 6e 6f 74 20 7a 65 72  LIMIT is not zer
3120: 6f 2c 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6b  o, then try to k
3130: 65 65 70 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73  eep the.** sizes
3140: 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63   of memory alloc
3150: 61 74 69 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69  ations below thi
3160: 73 20 76 61 6c 75 65 20 77 68 65 72 65 20 70 6f  s value where po
3170: 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21  ssible..*/.#if !
3180: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
3190: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54  ALLOC_SOFT_LIMIT
31a0: 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
31b0: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
31c0: 4d 49 54 20 31 30 32 34 0a 23 65 6e 64 69 66 0a  MIT 1024.#endif.
31d0: 0a 2f 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74  ./*.** We need t
31e0: 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f  o define _XOPEN_
31f0: 53 4f 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77  SOURCE as follow
3200: 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e  s in order to en
3210: 61 62 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69 76  able.** recursiv
3220: 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73  e mutexes on mos
3230: 74 20 55 6e 69 78 20 73 79 73 74 65 6d 73 2e 20  t Unix systems. 
3240: 20 42 75 74 20 4d 61 63 20 4f 53 20 58 20 69 73   But Mac OS X is
3250: 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54   different..** T
3260: 68 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45  he _XOPEN_SOURCE
3270: 20 64 65 66 69 6e 65 20 63 61 75 73 65 73 20 70   define causes p
3280: 72 6f 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20  roblems for Mac 
3290: 4f 53 20 58 20 77 65 20 61 72 65 20 74 6f 6c 64  OS X we are told
32a0: 2c 0a 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d  ,.** so it is om
32b0: 69 74 74 65 64 20 74 68 65 72 65 2e 20 20 53 65  itted there.  Se
32c0: 65 20 74 69 63 6b 65 74 20 23 32 36 37 33 2e 0a  e ticket #2673..
32d0: 2a 2a 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c  **.** Later we l
32e0: 65 61 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e  earn that _XOPEN
32f0: 5f 53 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c  _SOURCE is poorl
3300: 79 20 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79  y or incorrectly
3310: 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  .** implemented 
3320: 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e  on some systems.
3330: 20 20 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65    So we avoid de
3340: 66 69 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c  fining it at all
3350: 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 6c 72  .** if it is alr
3360: 65 61 64 79 20 64 65 66 69 6e 65 64 20 6f 72 20  eady defined or 
3370: 69 66 20 69 74 20 69 73 20 75 6e 6e 65 65 64 65  if it is unneede
3380: 64 20 62 65 63 61 75 73 65 20 77 65 20 61 72 65  d because we are
3390: 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20  .** not doing a 
33a0: 74 68 72 65 61 64 73 61 66 65 20 62 75 69 6c 64  threadsafe build
33b0: 2e 20 20 54 69 63 6b 65 74 20 23 32 36 38 31 2e  .  Ticket #2681.
33c0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
33d0: 74 69 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f  ticket #2741..*/
33e0: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 5f 58  .#if !defined(_X
33f0: 4f 50 45 4e 5f 53 4f 55 52 43 45 29 20 26 26 20  OPEN_SOURCE) && 
3400: 21 64 65 66 69 6e 65 64 28 5f 5f 44 41 52 57 49  !defined(__DARWI
3410: 4e 5f 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64  N__) && !defined
3420: 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
3430: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
3440: 0a 23 20 20 64 65 66 69 6e 65 20 5f 58 4f 50 45  .#  define _XOPE
3450: 4e 5f 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a  N_SOURCE 500  /*
3460: 20 4e 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c   Needed to enabl
3470: 65 20 70 74 68 72 65 61 64 20 72 65 63 75 72 73  e pthread recurs
3480: 69 76 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23  ive mutexes */.#
3490: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
34a0: 20 54 43 4c 20 68 65 61 64 65 72 73 20 61 72 65   TCL headers are
34b0: 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 77 68 65   only needed whe
34c0: 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20  n compiling the 
34d0: 54 43 4c 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f  TCL bindings..*/
34e0: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
34f0: 49 54 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69  ITE_TCL) || defi
3500: 6e 65 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63  ned(TCLSH).# inc
3510: 6c 75 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e  lude <tcl.h>.#en
3520: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20  dif../*.** Many 
3530: 70 65 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69  people are faili
3540: 6e 67 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42  ng to set -DNDEB
3550: 55 47 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c  UG=1 when compil
3560: 69 6e 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53  ing SQLite..** S
3570: 65 74 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61  etting NDEBUG ma
3580: 6b 65 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61  kes the code sma
3590: 6c 6c 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73  ller and run fas
35a0: 74 65 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c  ter.  So the fol
35b0: 6c 6f 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20  lowing.** lines 
35c0: 61 72 65 20 61 64 64 65 64 20 74 6f 20 61 75 74  are added to aut
35d0: 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e  omatically set N
35e0: 44 45 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65  DEBUG unless the
35f0: 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d   -DSQLITE_DEBUG=
3600: 31 0a 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73  1.** option is s
3610: 65 74 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47  et.  Thus NDEBUG
3620: 20 62 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d   becomes an opt-
3630: 69 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 61  in rather than a
3640: 6e 20 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61  n opt-out.** fea
3650: 74 75 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  ture..*/.#if !de
3660: 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26  fined(NDEBUG) &&
3670: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
3680: 5f 44 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e  _DEBUG) .# defin
3690: 65 20 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69  e NDEBUG 1.#endi
36a0: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73  f../*.** The tes
36b0: 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73  tcase() macro is
36c0: 20 75 73 65 64 20 74 6f 20 61 69 64 20 69 6e 20   used to aid in 
36d0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67  coverage testing
36e0: 2e 20 20 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e  .  When .** doin
36f0: 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69  g coverage testi
3700: 6e 67 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f  ng, the conditio
3710: 6e 20 69 6e 73 69 64 65 20 74 68 65 20 61 72 67  n inside the arg
3720: 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74  ument to.** test
3730: 63 61 73 65 28 29 20 6d 75 73 74 20 62 65 20 65  case() must be e
3740: 76 61 6c 75 61 74 65 64 20 62 6f 74 68 20 74 72  valuated both tr
3750: 75 65 20 61 6e 64 20 66 61 6c 73 65 20 69 6e 20  ue and false in 
3760: 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20  order to.** get 
3770: 66 75 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65  full branch cove
3780: 72 61 67 65 2e 20 20 54 68 65 20 74 65 73 74 63  rage.  The testc
3790: 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 69  ase() macro is i
37a0: 6e 73 65 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65  nserted.** to he
37b0: 6c 70 20 65 6e 73 75 72 65 20 61 64 65 71 75 61  lp ensure adequa
37c0: 74 65 20 74 65 73 74 20 63 6f 76 65 72 61 67 65  te test coverage
37d0: 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65   in places where
37e0: 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69   simple.** condi
37f0: 74 69 6f 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f  tion/decision co
3800: 76 65 72 61 67 65 20 69 73 20 69 6e 61 64 65 71  verage is inadeq
3810: 75 61 74 65 2e 20 20 46 6f 72 20 65 78 61 6d 70  uate.  For examp
3820: 6c 65 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a  le, testcase().*
3830: 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  * can be used to
3840: 20 6d 61 6b 65 20 73 75 72 65 20 62 6f 75 6e 64   make sure bound
3850: 61 72 79 20 76 61 6c 75 65 73 20 61 72 65 20 74  ary values are t
3860: 65 73 74 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62  ested.  For.** b
3870: 69 74 6d 61 73 6b 20 74 65 73 74 73 2c 20 74 65  itmask tests, te
3880: 73 74 63 61 73 65 28 29 20 63 61 6e 20 62 65 20  stcase() can be 
3890: 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72  used to make sur
38a0: 65 20 65 61 63 68 20 62 69 74 0a 2a 2a 20 69 73  e each bit.** is
38b0: 20 73 69 67 6e 69 66 69 63 61 6e 74 20 61 6e 64   significant and
38c0: 20 75 73 65 64 20 61 74 20 6c 65 61 73 74 20 6f   used at least o
38d0: 6e 63 65 2e 20 20 4f 6e 20 73 77 69 74 63 68 20  nce.  On switch 
38e0: 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68  statements.** wh
38f0: 65 72 65 20 6d 75 6c 74 69 70 6c 65 20 63 61 73  ere multiple cas
3900: 65 73 20 67 6f 20 74 6f 20 74 68 65 20 73 61 6d  es go to the sam
3910: 65 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c  e block of code,
3920: 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63   testcase().** c
3930: 61 6e 20 69 6e 73 75 72 65 20 74 68 61 74 20 61  an insure that a
3940: 6c 6c 20 63 61 73 65 73 20 61 72 65 20 65 76 61  ll cases are eva
3950: 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69  luated..**.*/.#i
3960: 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45  fdef SQLITE_COVE
3970: 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  RAGE_TEST.SQLITE
3980: 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
3990: 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28  sqlite3Coverage(
39a0: 69 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 74  int);.# define t
39b0: 65 73 74 63 61 73 65 28 58 29 20 20 69 66 28 20  estcase(X)  if( 
39c0: 58 20 29 7b 20 73 71 6c 69 74 65 33 43 6f 76 65  X ){ sqlite3Cove
39d0: 72 61 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20  rage(__LINE__); 
39e0: 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
39f0: 20 74 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e   testcase(X).#en
3a00: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54  dif../*.** The T
3a10: 45 53 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73  ESTONLY macro is
3a20: 20 75 73 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65   used to enclose
3a30: 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72   variable declar
3a40: 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68  ations or.** oth
3a50: 65 72 20 62 69 74 73 20 6f 66 20 63 6f 64 65 20  er bits of code 
3a60: 74 68 61 74 20 61 72 65 20 6e 65 65 64 65 64 20  that are needed 
3a70: 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65 20 61  to support the a
3a80: 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68  rguments.** with
3a90: 69 6e 20 74 65 73 74 63 61 73 65 28 29 20 61 6e  in testcase() an
3aa0: 64 20 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f  d assert() macro
3ab0: 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  s..*/.#if !defin
3ac0: 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65  ed(NDEBUG) || de
3ad0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56  fined(SQLITE_COV
3ae0: 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20 64 65  ERAGE_TEST).# de
3af0: 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29  fine TESTONLY(X)
3b00: 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69    X.#else.# defi
3b10: 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23  ne TESTONLY(X).#
3b20: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d  endif../*.** Som
3b30: 65 74 69 6d 65 73 20 77 65 20 6e 65 65 64 20 61  etimes we need a
3b40: 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66   small amount of
3b50: 20 63 6f 64 65 20 73 75 63 68 20 61 73 20 61 20   code such as a 
3b60: 76 61 72 69 61 62 6c 65 20 69 6e 69 74 69 61 6c  variable initial
3b70: 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65  ization.** to se
3b80: 74 75 70 20 66 6f 72 20 61 20 6c 61 74 65 72 20  tup for a later 
3b90: 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
3ba0: 6e 74 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77  nt.  We do not w
3bb0: 61 6e 74 20 74 68 69 73 20 63 6f 64 65 20 74 6f  ant this code to
3bc0: 0a 2a 2a 20 61 70 70 65 61 72 20 77 68 65 6e 20  .** appear when 
3bd0: 61 73 73 65 72 74 28 29 20 69 73 20 64 69 73 61  assert() is disa
3be0: 62 6c 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  bled.  The follo
3bf0: 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 74 68  wing macro is th
3c00: 65 72 65 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20  erefore.** used 
3c10: 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 61 74 20  to contain that 
3c20: 73 65 74 75 70 20 63 6f 64 65 2e 20 20 54 68 65  setup code.  The
3c30: 20 22 56 56 41 22 20 61 63 72 6f 6e 79 6d 20 73   "VVA" acronym s
3c40: 74 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65  tands for.** "Ve
3c50: 72 69 66 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69  rification, Vali
3c60: 64 61 74 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72  dation, and Accr
3c70: 65 64 69 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20  editation".  In 
3c80: 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
3c90: 0a 2a 2a 20 63 6f 64 65 20 77 69 74 68 69 6e 20  .** code within 
3ca0: 56 56 41 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20  VVA_ONLY() will 
3cb0: 6f 6e 6c 79 20 72 75 6e 20 64 75 72 69 6e 67 20  only run during 
3cc0: 76 65 72 69 66 69 63 61 74 69 6f 6e 20 70 72 6f  verification pro
3cd0: 63 65 73 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64  cesses..*/.#ifnd
3ce0: 65 66 20 4e 44 45 42 55 47 0a 23 20 64 65 66 69  ef NDEBUG.# defi
3cf0: 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20  ne VVA_ONLY(X)  
3d00: 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  X.#else.# define
3d10: 20 56 56 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e   VVA_ONLY(X).#en
3d20: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41  dif../*.** The A
3d30: 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20  LWAYS and NEVER 
3d40: 6d 61 63 72 6f 73 20 73 75 72 72 6f 75 6e 64 20  macros surround 
3d50: 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69  boolean expressi
3d60: 6f 6e 73 20 77 68 69 63 68 20 0a 2a 2a 20 61 72  ons which .** ar
3d70: 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c  e intended to al
3d80: 77 61 79 73 20 62 65 20 74 72 75 65 20 6f 72 20  ways be true or 
3d90: 66 61 6c 73 65 2c 20 72 65 73 70 65 63 74 69 76  false, respectiv
3da0: 65 6c 79 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78  ely.  Such.** ex
3db0: 70 72 65 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20  pressions could 
3dc0: 62 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20  be omitted from 
3dd0: 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74  the code complet
3de0: 65 6c 79 2e 20 20 42 75 74 20 74 68 65 79 0a 2a  ely.  But they.*
3df0: 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69  * are included i
3e00: 6e 20 61 20 66 65 77 20 63 61 73 65 73 20 69 6e  n a few cases in
3e10: 20 6f 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63   order to enhanc
3e20: 65 20 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65  e the resilience
3e30: 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f  .** of SQLite to
3e40: 20 75 6e 65 78 70 65 63 74 65 64 20 62 65 68 61   unexpected beha
3e50: 76 69 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74  vior - to make t
3e60: 68 65 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65  he code "self-he
3e70: 61 6c 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75  aling".** or "du
3e80: 63 74 69 6c 65 22 20 72 61 74 68 65 72 20 74 68  ctile" rather th
3e90: 61 6e 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c  an being "brittl
3ea0: 65 22 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20  e" and crashing 
3eb0: 61 74 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  at the first.** 
3ec0: 68 69 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65  hint of unplanne
3ed0: 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a  d behavior..**.*
3ee0: 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
3ef0: 2c 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56  , ALWAYS and NEV
3f00: 45 52 20 61 72 65 20 61 64 64 65 64 20 66 6f 72  ER are added for
3f10: 20 64 65 66 65 6e 73 69 76 65 20 63 6f 64 65 2e   defensive code.
3f20: 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e  .**.** When doin
3f30: 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69  g coverage testi
3f40: 6e 67 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45  ng ALWAYS and NE
3f50: 56 45 52 20 61 72 65 20 68 61 72 64 2d 63 6f 64  VER are hard-cod
3f60: 65 64 20 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65  ed to.** be true
3f70: 20 61 6e 64 20 66 61 6c 73 65 20 73 6f 20 74 68   and false so th
3f80: 61 74 20 74 68 65 20 75 6e 72 65 61 63 68 61 62  at the unreachab
3f90: 6c 65 20 63 6f 64 65 20 74 68 65 6e 20 73 70 65  le code then spe
3fa0: 63 69 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74  cify will.** not
3fb0: 20 62 65 20 63 6f 75 6e 74 65 64 20 61 73 20 75   be counted as u
3fc0: 6e 74 65 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f  ntested code..*/
3fd0: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
3fe0: 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53  ITE_COVERAGE_TES
3ff0: 54 29 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41  T).# define ALWA
4000: 59 53 28 58 29 20 20 20 20 20 20 28 31 29 0a 23  YS(X)      (1).#
4010: 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29   define NEVER(X)
4020: 20 20 20 20 20 20 20 28 30 29 0a 23 65 6c 69 66         (0).#elif
4030: 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
4040: 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
4050: 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 73     int sqlite3As
4060: 73 65 72 74 28 76 6f 69 64 29 3b 0a 23 20 64 65  sert(void);.# de
4070: 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 20 20  fine ALWAYS(X)  
4080: 20 20 20 20 28 28 58 29 3f 31 3a 73 71 6c 69 74      ((X)?1:sqlit
4090: 65 33 41 73 73 65 72 74 28 29 29 0a 23 20 64 65  e3Assert()).# de
40a0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20  fine NEVER(X)   
40b0: 20 20 20 20 28 28 58 29 3f 73 71 6c 69 74 65 33      ((X)?sqlite3
40c0: 41 73 73 65 72 74 28 29 3a 30 29 0a 23 65 6c 73  Assert():0).#els
40d0: 65 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59  e.# define ALWAY
40e0: 53 28 58 29 20 20 20 20 20 20 28 58 29 0a 23 20  S(X)      (X).# 
40f0: 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20  define NEVER(X) 
4100: 20 20 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66        (X).#endif
4110: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72  ../*.** The macr
4120: 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20  o unlikely() is 
4130: 61 20 68 69 6e 74 20 74 68 61 74 20 73 75 72 72  a hint that surr
4140: 6f 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a  ounds a boolean.
4150: 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  ** expression th
4160: 61 74 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61  at is usually fa
4170: 6c 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65  lse.  Macro like
4180: 6c 79 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a  ly() surrounds.*
4190: 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72  * a boolean expr
41a0: 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75  ession that is u
41b0: 73 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43  sually true.  GC
41c0: 43 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20  C is able to.** 
41d0: 75 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20  use these hints 
41e0: 74 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74  to generate bett
41f0: 65 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d  er code, sometim
4200: 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  es..*/.#if defin
4210: 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
4220: 30 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c  0.# define likel
4230: 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69  y(X)    __builti
4240: 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a  n_expect((X),1).
4250: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c  # define unlikel
4260: 79 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f  y(X)  __builtin_
4270: 65 78 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65  expect((X),0).#e
4280: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b  lse.# define lik
4290: 65 6c 79 28 58 29 20 20 20 20 21 21 28 58 29 0a  ely(X)    !!(X).
42a0: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c  # define unlikel
42b0: 79 28 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64  y(X)  !!(X).#end
42c0: 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
42d0: 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69  *** Include sqli
42e0: 74 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  te3.h in the mid
42f0: 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
4300: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
4310: 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
4320: 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73  *** Begin file s
4330: 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a  qlite3.h *******
4340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4360: 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
4370: 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
4380: 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
4390: 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
43a0: 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
43b0: 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
43c0: 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
43d0: 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
43e0: 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
43f0: 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
4400: 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
4410: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
4420: 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
4430: 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
4440: 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
4450: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
4460: 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
4470: 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
4480: 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
4490: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
44a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
44e0: 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64  is header file d
44f0: 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
4500: 66 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51  face that the SQ
4510: 4c 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20  Lite library.** 
4520: 70 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65  presents to clie
4530: 6e 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66  nt programs.  If
4540: 20 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73   a C-function, s
4550: 74 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79  tructure, dataty
4560: 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61  pe,.** or consta
4570: 6e 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f  nt definition do
4580: 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e  es not appear in
4590: 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e   this file, then
45a0: 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20   it is.** not a 
45b0: 70 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66  published API of
45c0: 20 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a   SQLite, is subj
45d0: 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69  ect to change wi
45e0: 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c  thout.** notice,
45f0: 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20   and should not 
4600: 62 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  be referenced by
4610: 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75   programs that u
4620: 73 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  se SQLite..**.**
4630: 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66   Some of the def
4640: 69 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72  initions that ar
4650: 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61  e in this file a
4660: 72 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20  re marked as.** 
4670: 22 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20  "experimental". 
4680: 20 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e   Experimental in
4690: 74 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72  terfaces are nor
46a0: 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61  mally new.** fea
46b0: 74 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61  tures recently a
46c0: 64 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20  dded to SQLite. 
46d0: 20 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63   We do not antic
46e0: 69 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a  ipate changes.**
46f0: 20 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c   to experimental
4700: 20 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20   interfaces but 
4710: 72 65 73 65 72 76 65 20 74 6f 20 6d 61 6b 65 20  reserve to make 
4720: 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 69 66  minor changes if
4730: 0a 2a 2a 20 65 78 70 65 72 69 65 6e 63 65 20 66  .** experience f
4740: 72 6f 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20  rom use "in the 
4750: 77 69 6c 64 22 20 73 75 67 67 65 73 74 20 73 75  wild" suggest su
4760: 63 68 20 63 68 61 6e 67 65 73 20 61 72 65 20 70  ch changes are p
4770: 72 75 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  rudent..**.** Th
4780: 65 20 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e  e official C-lan
4790: 67 75 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65  guage API docume
47a0: 6e 74 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69  ntation for SQLi
47b0: 74 65 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a  te is derived.**
47c0: 20 66 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69   from comments i
47d0: 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68  n this file.  Th
47e0: 69 73 20 66 69 6c 65 20 69 73 20 74 68 65 20 61  is file is the a
47f0: 75 74 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75  uthoritative sou
4800: 72 63 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51  rce.** on how SQ
4810: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20  Lite interfaces 
4820: 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f  are suppose to o
4830: 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  perate..**.** Th
4840: 65 20 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66  e name of this f
4850: 69 6c 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67  ile under config
4860: 75 72 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65  uration manageme
4870: 6e 74 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e  nt is "sqlite.h.
4880: 69 6e 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65  in"..** The make
4890: 66 69 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20  file makes some 
48a0: 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f  minor changes to
48b0: 20 74 68 69 73 20 66 69 6c 65 20 28 73 75 63 68   this file (such
48c0: 20 61 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a   as inserting.**
48d0: 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d   the version num
48e0: 62 65 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73  ber) and changes
48f0: 20 69 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71   its name to "sq
4900: 6c 69 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70  lite3.h" as.** p
4910: 61 72 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64  art of the build
4920: 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20   process..**.** 
4930: 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65  @(#) $Id: sqlite
4940: 2e 68 2e 69 6e 2c 76 20 31 2e 34 34 37 20 32 30  .h.in,v 1.447 20
4950: 30 39 2f 30 34 2f 33 30 20 31 35 3a 35 39 3a 35  09/04/30 15:59:5
4960: 36 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23  6 drh Exp $.*/.#
4970: 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 5f  ifndef _SQLITE3_
4980: 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49  H_.#define _SQLI
4990: 54 45 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65 20  TE3_H_.#include 
49a0: 3c 73 74 64 61 72 67 2e 68 3e 20 20 20 20 20 2f  <stdarg.h>     /
49b0: 2a 20 4e 65 65 64 65 64 20 66 6f 72 20 74 68 65  * Needed for the
49c0: 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 76   definition of v
49d0: 61 5f 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  a_list */../*.**
49e0: 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 63 61   Make sure we ca
49f0: 6e 20 63 61 6c 6c 20 74 68 69 73 20 73 74 75 66  n call this stuf
4a00: 66 20 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23  f from C++..*/.#
4a10: 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20  if 0.extern "C" 
4a20: 7b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  {.#endif.../*.**
4a30: 20 41 64 64 20 74 68 65 20 61 62 69 6c 69 74 79   Add the ability
4a40: 20 74 6f 20 6f 76 65 72 72 69 64 65 20 27 65 78   to override 'ex
4a50: 74 65 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65 66  tern'.*/.#ifndef
4a60: 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a 23   SQLITE_EXTERN.#
4a70: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45   define SQLITE_E
4a80: 58 54 45 52 4e 20 65 78 74 65 72 6e 0a 23 65 6e  XTERN extern.#en
4a90: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65  dif../*.** These
4aa0: 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61 72   no-op macros ar
4ab0: 65 20 75 73 65 64 20 69 6e 20 66 72 6f 6e 74 20  e used in front 
4ac0: 6f 66 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f  of interfaces to
4ad0: 20 6d 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20 69   mark those.** i
4ae0: 6e 74 65 72 66 61 63 65 73 20 61 73 20 65 69 74  nterfaces as eit
4af0: 68 65 72 20 64 65 70 72 65 63 61 74 65 64 20 6f  her deprecated o
4b00: 72 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20  r experimental. 
4b10: 20 4e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e   New application
4b20: 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20  s.** should not 
4b30: 75 73 65 20 64 65 70 72 65 63 61 74 65 64 20 69  use deprecated i
4b40: 6e 74 72 66 61 63 65 73 20 2d 20 74 68 65 79 20  ntrfaces - they 
4b50: 61 72 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20  are support for 
4b60: 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d  backwards.** com
4b70: 70 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e  patibility only.
4b80: 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 72    Application wr
4b90: 69 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20  iters should be 
4ba0: 61 77 61 72 65 20 74 68 61 74 0a 2a 2a 20 65 78  aware that.** ex
4bb0: 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72  perimental inter
4bc0: 66 61 63 65 73 20 61 72 65 20 73 75 62 6a 65 63  faces are subjec
4bd0: 74 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 70  t to change in p
4be0: 6f 69 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a  oint releases..*
4bf0: 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f  *.** These macro
4c00: 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76  s used to resolv
4c10: 65 20 74 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e  e to various kin
4c20: 64 73 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d  ds of compiler m
4c30: 61 67 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75  agic that.** wou
4c40: 6c 64 20 67 65 6e 65 72 61 74 65 20 77 61 72 6e  ld generate warn
4c50: 69 6e 67 20 6d 65 73 73 61 67 65 73 20 77 68 65  ing messages whe
4c60: 6e 20 74 68 65 79 20 77 65 72 65 20 75 73 65 64  n they were used
4c70: 2e 20 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 63  .  But that.** c
4c80: 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 65 6e  ompiler magic en
4c90: 64 65 64 20 75 70 20 67 65 6e 65 72 61 74 69 6e  ded up generatin
4ca0: 67 20 73 75 63 68 20 61 20 66 6c 75 72 72 79 20  g such a flurry 
4cb0: 6f 66 20 62 75 67 20 72 65 70 6f 72 74 73 0a 2a  of bug reports.*
4cc0: 2a 20 74 68 61 74 20 77 65 20 68 61 76 65 20 74  * that we have t
4cd0: 61 6b 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20  aken it all out 
4ce0: 61 6e 64 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f  and gone back to
4cf0: 20 75 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a   using simple.**
4d00: 20 6e 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f   noop macros..*/
4d10: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
4d20: 44 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69  DEPRECATED.#defi
4d30: 6e 65 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49  ne SQLITE_EXPERI
4d40: 4d 45 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e  MENTAL../*.** En
4d50: 73 75 72 65 20 74 68 65 73 65 20 73 79 6d 62 6f  sure these symbo
4d60: 6c 73 20 77 65 72 65 20 6e 6f 74 20 64 65 66 69  ls were not defi
4d70: 6e 65 64 20 62 79 20 73 6f 6d 65 20 70 72 65 76  ned by some prev
4d80: 69 6f 75 73 20 68 65 61 64 65 72 20 66 69 6c 65  ious header file
4d90: 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
4da0: 54 45 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64  TE_VERSION.# und
4db0: 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  ef SQLITE_VERSIO
4dc0: 4e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  N.#endif.#ifdef 
4dd0: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e  SQLITE_VERSION_N
4de0: 55 4d 42 45 52 0a 23 20 75 6e 64 65 66 20 53 51  UMBER.# undef SQ
4df0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d  LITE_VERSION_NUM
4e00: 42 45 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  BER.#endif../*.*
4e10: 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70  * CAPI3REF: Comp
4e20: 69 6c 65 2d 54 69 6d 65 20 4c 69 62 72 61 72 79  ile-Time Library
4e30: 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73   Version Numbers
4e40: 20 7b 48 31 30 30 31 30 7d 20 3c 53 36 30 31 30   {H10010} <S6010
4e50: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  0>.**.** The SQL
4e60: 49 54 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20  ITE_VERSION and 
4e70: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e  SQLITE_VERSION_N
4e80: 55 4d 42 45 52 20 23 64 65 66 69 6e 65 73 20 69  UMBER #defines i
4e90: 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33  n.** the sqlite3
4ea0: 2e 68 20 66 69 6c 65 20 73 70 65 63 69 66 79 20  .h file specify 
4eb0: 74 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 53  the version of S
4ec0: 51 4c 69 74 65 20 77 69 74 68 20 77 68 69 63 68  QLite with which
4ed0: 0a 2a 2a 20 74 68 61 74 20 68 65 61 64 65 72 20  .** that header 
4ee0: 66 69 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74  file is associat
4ef0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76  ed..**.** The "v
4f00: 65 72 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74  ersion" of SQLit
4f10: 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 66  e is a string of
4f20: 20 74 68 65 20 66 6f 72 6d 20 22 58 2e 59 2e 5a   the form "X.Y.Z
4f30: 22 2e 0a 2a 2a 20 54 68 65 20 70 68 72 61 73 65  "..** The phrase
4f40: 20 22 61 6c 70 68 61 22 20 6f 72 20 22 62 65 74   "alpha" or "bet
4f50: 61 22 20 6d 69 67 68 74 20 62 65 20 61 70 70 65  a" might be appe
4f60: 6e 64 65 64 20 61 66 74 65 72 20 74 68 65 20 5a  nded after the Z
4f70: 2e 0a 2a 2a 20 54 68 65 20 58 20 76 61 6c 75 65  ..** The X value
4f80: 20 69 73 20 6d 61 6a 6f 72 20 76 65 72 73 69 6f   is major versio
4f90: 6e 20 6e 75 6d 62 65 72 20 61 6c 77 61 79 73 20  n number always 
4fa0: 33 20 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a  3 in SQLite3..**
4fb0: 20 54 68 65 20 58 20 76 61 6c 75 65 20 6f 6e 6c   The X value onl
4fc0: 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e 20 62  y changes when b
4fd0: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
4fe0: 62 69 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f  bility is.** bro
4ff0: 6b 65 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 6e  ken and we inten
5000: 64 20 74 6f 20 6e 65 76 65 72 20 62 72 65 61 6b  d to never break
5010: 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
5020: 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65  tibility..** The
5030: 20 59 20 76 61 6c 75 65 20 69 73 20 74 68 65 20   Y value is the 
5040: 6d 69 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75  minor version nu
5050: 6d 62 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68  mber and only ch
5060: 61 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68  anges when.** th
5070: 65 72 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 65  ere are major fe
5080: 61 74 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e  ature enhancemen
5090: 74 73 20 74 68 61 74 20 61 72 65 20 66 6f 72 77  ts that are forw
50a0: 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a  ards compatible.
50b0: 2a 2a 20 62 75 74 20 6e 6f 74 20 62 61 63 6b 77  ** but not backw
50c0: 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e  ards compatible.
50d0: 0a 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 20  .** The Z value 
50e0: 69 73 20 74 68 65 20 72 65 6c 65 61 73 65 20 6e  is the release n
50f0: 75 6d 62 65 72 20 61 6e 64 20 69 73 20 69 6e 63  umber and is inc
5100: 72 65 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a  remented with.**
5110: 20 65 61 63 68 20 72 65 6c 65 61 73 65 20 62 75   each release bu
5120: 74 20 72 65 73 65 74 73 20 62 61 63 6b 20 74 6f  t resets back to
5130: 20 30 20 77 68 65 6e 65 76 65 72 20 59 20 69 73   0 whenever Y is
5140: 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a   incremented..**
5150: 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
5160: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
5170: 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  n()] and [sqlite
5180: 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
5190: 62 65 72 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65  ber()]..**.** Re
51a0: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30  quirements: [H10
51b0: 30 31 31 5d 20 5b 48 31 30 30 31 34 5d 0a 2a 2f  011] [H10014].*/
51c0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
51d0: 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20 20  VERSION         
51e0: 22 33 2e 36 2e 31 34 2e 32 22 0a 23 64 65 66 69  "3.6.14.2".#defi
51f0: 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  ne SQLITE_VERSIO
5200: 4e 5f 4e 55 4d 42 45 52 20 20 33 30 30 36 30 31  N_NUMBER  300601
5210: 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  4../*.** CAPI3RE
5220: 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62 72  F: Run-Time Libr
5230: 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62  ary Version Numb
5240: 65 72 73 20 7b 48 31 30 30 32 30 7d 20 3c 53 36  ers {H10020} <S6
5250: 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0100>.** KEYWORD
5260: 53 3a 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69  S: sqlite3_versi
5270: 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66  on.**.** These f
5280: 65 61 74 75 72 65 73 20 70 72 6f 76 69 64 65 20  eatures provide 
5290: 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61  the same informa
52a0: 74 69 6f 6e 20 61 73 20 74 68 65 20 5b 53 51 4c  tion as the [SQL
52b0: 49 54 45 5f 56 45 52 53 49 4f 4e 5d 0a 2a 2a 20  ITE_VERSION].** 
52c0: 61 6e 64 20 5b 53 51 4c 49 54 45 5f 56 45 52 53  and [SQLITE_VERS
52d0: 49 4f 4e 5f 4e 55 4d 42 45 52 5d 20 23 64 65 66  ION_NUMBER] #def
52e0: 69 6e 65 73 20 69 6e 20 74 68 65 20 68 65 61 64  ines in the head
52f0: 65 72 2c 20 62 75 74 20 61 72 65 20 61 73 73 6f  er, but are asso
5300: 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74  ciated.** with t
5310: 68 65 20 6c 69 62 72 61 72 79 20 69 6e 73 74 65  he library inste
5320: 61 64 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  ad of the header
5330: 20 66 69 6c 65 2e 20 20 43 61 75 74 69 6f 75 73   file.  Cautious
5340: 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67   programmers mig
5350: 68 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 61 20  ht.** include a 
5360: 63 68 65 63 6b 20 69 6e 20 74 68 65 69 72 20 61  check in their a
5370: 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 76 65  pplication to ve
5380: 72 69 66 79 20 74 68 61 74 0a 2a 2a 20 73 71 6c  rify that.** sql
5390: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
53a0: 6e 75 6d 62 65 72 28 29 20 61 6c 77 61 79 73 20  number() always 
53b0: 72 65 74 75 72 6e 73 20 74 68 65 20 76 61 6c 75  returns the valu
53c0: 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52  e.** [SQLITE_VER
53d0: 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a  SION_NUMBER]..**
53e0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
53f0: 6c 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e  libversion() fun
5400: 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
5410: 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69  e same informati
5420: 6f 6e 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74  on as is.** in t
5430: 68 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69  he sqlite3_versi
5440: 6f 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73  on[] string cons
5450: 74 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74  tant.  The funct
5460: 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a  ion is provided.
5470: 2a 2a 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c  ** for use in DL
5480: 4c 73 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65  Ls since DLL use
5490: 72 73 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f  rs usually do no
54a0: 74 20 68 61 76 65 20 64 69 72 65 63 74 20 61 63  t have direct ac
54b0: 63 65 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a  cess to string.*
54c0: 2a 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68  * constants with
54d0: 69 6e 20 74 68 65 20 44 4c 4c 2e 0a 2a 2a 0a 2a  in the DLL..**.*
54e0: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20  * Requirements: 
54f0: 5b 48 31 30 30 32 31 5d 20 5b 48 31 30 30 32 32  [H10021] [H10022
5500: 5d 20 5b 48 31 30 30 32 33 5d 0a 2a 2f 0a 53 51  ] [H10023].*/.SQ
5510: 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
5520: 68 61 72 20 73 71 6c 69 74 65 33 5f 76 65 72 73  har sqlite3_vers
5530: 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 56  ion[] = SQLITE_V
5540: 45 52 53 49 4f 4e 3b 0a 53 51 4c 49 54 45 5f 41  ERSION;.SQLITE_A
5550: 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
5560: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
5570: 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  n(void);.SQLITE_
5580: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
5590: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
55a0: 72 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20  r(void);../*.** 
55b0: 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 54  CAPI3REF: Test T
55c0: 6f 20 53 65 65 20 49 66 20 54 68 65 20 4c 69 62  o See If The Lib
55d0: 72 61 72 79 20 49 73 20 54 68 72 65 61 64 73 61  rary Is Threadsa
55e0: 66 65 20 7b 48 31 30 31 30 30 7d 20 3c 53 36 30  fe {H10100} <S60
55f0: 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  100>.**.** SQLit
5600: 65 20 63 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65  e can be compile
5610: 64 20 77 69 74 68 20 6f 72 20 77 69 74 68 6f 75  d with or withou
5620: 74 20 6d 75 74 65 78 65 73 2e 20 20 57 68 65 6e  t mutexes.  When
5630: 0a 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54 45 5f  .** the [SQLITE_
5640: 54 48 52 45 41 44 53 41 46 45 5d 20 43 20 70 72  THREADSAFE] C pr
5650: 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f  eprocessor macro
5660: 20 31 20 6f 72 20 32 2c 20 6d 75 74 65 78 65 73   1 or 2, mutexes
5670: 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20  .** are enabled 
5680: 61 6e 64 20 53 51 4c 69 74 65 20 69 73 20 74 68  and SQLite is th
5690: 72 65 61 64 73 61 66 65 2e 20 20 57 68 65 6e 20  readsafe.  When 
56a0: 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54  the.** [SQLITE_T
56b0: 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f  HREADSAFE] macro
56c0: 20 69 73 20 30 2c 20 0a 2a 2a 20 74 68 65 20 6d   is 0, .** the m
56d0: 75 74 65 78 65 73 20 61 72 65 20 6f 6d 69 74 74  utexes are omitt
56e0: 65 64 2e 20 20 57 69 74 68 6f 75 74 20 74 68 65  ed.  Without the
56f0: 20 6d 75 74 65 78 65 73 2c 20 69 74 20 69 73 20   mutexes, it is 
5700: 6e 6f 74 20 73 61 66 65 0a 2a 2a 20 74 6f 20 75  not safe.** to u
5710: 73 65 20 53 51 4c 69 74 65 20 63 6f 6e 63 75 72  se SQLite concur
5720: 72 65 6e 74 6c 79 20 66 72 6f 6d 20 6d 6f 72 65  rently from more
5730: 20 74 68 61 6e 20 6f 6e 65 20 74 68 72 65 61 64   than one thread
5740: 2e 0a 2a 2a 0a 2a 2a 20 45 6e 61 62 6c 69 6e 67  ..**.** Enabling
5750: 20 6d 75 74 65 78 65 73 20 69 6e 63 75 72 73 20   mutexes incurs 
5760: 61 20 6d 65 61 73 75 72 61 62 6c 65 20 70 65 72  a measurable per
5770: 66 6f 72 6d 61 6e 63 65 20 70 65 6e 61 6c 74 79  formance penalty
5780: 2e 0a 2a 2a 20 53 6f 20 69 66 20 73 70 65 65 64  ..** So if speed
5790: 20 69 73 20 6f 66 20 75 74 6d 6f 73 74 20 69 6d   is of utmost im
57a0: 70 6f 72 74 61 6e 63 65 2c 20 69 74 20 6d 61 6b  portance, it mak
57b0: 65 73 20 73 65 6e 73 65 20 74 6f 20 64 69 73 61  es sense to disa
57c0: 62 6c 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78  ble.** the mutex
57d0: 65 73 2e 20 20 42 75 74 20 66 6f 72 20 6d 61 78  es.  But for max
57e0: 69 6d 75 6d 20 73 61 66 65 74 79 2c 20 6d 75 74  imum safety, mut
57f0: 65 78 65 73 20 73 68 6f 75 6c 64 20 62 65 20 65  exes should be e
5800: 6e 61 62 6c 65 64 2e 0a 2a 2a 20 54 68 65 20 64  nabled..** The d
5810: 65 66 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20  efault behavior 
5820: 69 73 20 66 6f 72 20 6d 75 74 65 78 65 73 20 74  is for mutexes t
5830: 6f 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a  o be enabled..**
5840: 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
5850: 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 62  ce can be used b
5860: 79 20 61 20 70 72 6f 67 72 61 6d 20 74 6f 20 6d  y a program to m
5870: 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
5880: 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 6f 66 20  e.** version of 
5890: 53 51 4c 69 74 65 20 74 68 61 74 20 69 74 20 69  SQLite that it i
58a0: 73 20 6c 69 6e 6b 69 6e 67 20 61 67 61 69 6e 73  s linking agains
58b0: 74 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77  t was compiled w
58c0: 69 74 68 0a 2a 2a 20 74 68 65 20 64 65 73 69 72  ith.** the desir
58d0: 65 64 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68  ed setting of th
58e0: 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44  e [SQLITE_THREAD
58f0: 53 41 46 45 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a  SAFE] macro..**.
5900: 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63  ** This interfac
5910: 65 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 6f  e only reports o
5920: 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69  n the compile-ti
5930: 6d 65 20 6d 75 74 65 78 20 73 65 74 74 69 6e 67  me mutex setting
5940: 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 53 51 4c 49  .** of the [SQLI
5950: 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 66  TE_THREADSAFE] f
5960: 6c 61 67 2e 20 20 49 66 20 53 51 4c 69 74 65 20  lag.  If SQLite 
5970: 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
5980: 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41  .** SQLITE_THREA
5990: 44 53 41 46 45 3d 31 20 74 68 65 6e 20 6d 75 74  DSAFE=1 then mut
59a0: 65 78 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64  exes are enabled
59b0: 20 62 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a   by default but.
59c0: 2a 2a 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20  ** can be fully 
59d0: 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73  or partially dis
59e0: 61 62 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61  abled using a ca
59f0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
5a00: 6f 6e 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68  onfig()].** with
5a10: 20 74 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49   the verbs [SQLI
5a20: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
5a30: 54 48 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45  THREAD], [SQLITE
5a40: 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52  _CONFIG_MULTITHR
5a50: 45 41 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c  EAD],.** or [SQL
5a60: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
5a70: 5d 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  ].  The return v
5a80: 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e  alue of this fun
5a90: 63 74 69 6f 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f  ction shows.** o
5aa0: 6e 6c 79 20 74 68 65 20 64 65 66 61 75 6c 74 20  nly the default 
5ab0: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74  compile-time set
5ac0: 74 69 6e 67 2c 20 6e 6f 74 20 61 6e 79 20 72 75  ting, not any ru
5ad0: 6e 2d 74 69 6d 65 20 63 68 61 6e 67 65 73 0a 2a  n-time changes.*
5ae0: 2a 20 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e  * to that settin
5af0: 67 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 74 68 65  g..**.** See the
5b00: 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
5b10: 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  ] documentation 
5b20: 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
5b30: 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nformation..**.*
5b40: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20  * Requirements: 
5b50: 5b 48 31 30 31 30 31 5d 20 5b 48 31 30 31 30 32  [H10101] [H10102
5b60: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
5b70: 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65  int sqlite3_thre
5b80: 61 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f  adsafe(void);../
5b90: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
5ba0: 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69  atabase Connecti
5bb0: 6f 6e 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30  on Handle {H1200
5bc0: 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b  0} <S40200>.** K
5bd0: 45 59 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61  EYWORDS: {databa
5be0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b  se connection} {
5bf0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
5c00: 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68  ions}.**.** Each
5c10: 20 6f 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74   open SQLite dat
5c20: 61 62 61 73 65 20 69 73 20 72 65 70 72 65 73 65  abase is represe
5c30: 6e 74 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65  nted by a pointe
5c40: 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65  r to an instance
5c50: 20 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75   of.** the opaqu
5c60: 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65  e structure name
5c70: 64 20 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74  d "sqlite3".  It
5c80: 20 69 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68   is useful to th
5c90: 69 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  ink of an sqlite
5ca0: 33 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20  3.** pointer as 
5cb0: 61 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20  an object.  The 
5cc0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
5cd0: 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  , [sqlite3_open1
5ce0: 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  6()], and.** [sq
5cf0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
5d00: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20   interfaces are 
5d10: 69 74 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73  its constructors
5d20: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  , and [sqlite3_c
5d30: 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74  lose()].** is it
5d40: 73 20 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54  s destructor.  T
5d50: 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74  here are many ot
5d60: 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 28  her interfaces (
5d70: 73 75 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69  such as.** [sqli
5d80: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
5d90: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  ], [sqlite3_crea
5da0: 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20  te_function()], 
5db0: 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  and.** [sqlite3_
5dc0: 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20  busy_timeout()] 
5dd0: 74 6f 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65  to name but thre
5de0: 65 29 20 74 68 61 74 20 61 72 65 20 6d 65 74 68  e) that are meth
5df0: 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c  ods on an.** sql
5e00: 69 74 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  ite3 object..*/.
5e10: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
5e20: 71 6c 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a  qlite3 sqlite3;.
5e30: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
5e40: 20 36 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20   64-Bit Integer 
5e50: 54 79 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c  Types {H10200} <
5e60: 53 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S10110>.** KEYWO
5e70: 52 44 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36  RDS: sqlite_int6
5e80: 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a  4 sqlite_uint64.
5e90: 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68  **.** Because th
5ea0: 65 72 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d  ere is no cross-
5eb0: 70 6c 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20  platform way to 
5ec0: 73 70 65 63 69 66 79 20 36 34 2d 62 69 74 20 69  specify 64-bit i
5ed0: 6e 74 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20  nteger types.** 
5ee0: 53 51 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20  SQLite includes 
5ef0: 74 79 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d  typedefs for 64-
5f00: 62 69 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75  bit signed and u
5f10: 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73  nsigned integers
5f20: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
5f30: 74 65 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71  te3_int64 and sq
5f40: 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65  lite3_uint64 are
5f50: 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74   the preferred t
5f60: 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e  ype definitions.
5f70: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69  .** The sqlite_i
5f80: 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f  nt64 and sqlite_
5f90: 75 69 6e 74 36 34 20 74 79 70 65 73 20 61 72 65  uint64 types are
5fa0: 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62   supported for b
5fb0: 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70  ackwards.** comp
5fc0: 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a  atibility only..
5fd0: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
5fe0: 74 73 3a 20 5b 48 31 30 32 30 31 5d 20 5b 48 31  ts: [H10201] [H1
5ff0: 30 32 30 32 5d 0a 2a 2f 0a 23 69 66 64 65 66 20  0202].*/.#ifdef 
6000: 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50  SQLITE_INT64_TYP
6010: 45 0a 20 20 74 79 70 65 64 65 66 20 53 51 4c 49  E.  typedef SQLI
6020: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71  TE_INT64_TYPE sq
6030: 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79  lite_int64;.  ty
6040: 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53  pedef unsigned S
6050: 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45  QLITE_INT64_TYPE
6060: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a   sqlite_uint64;.
6070: 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d  #elif defined(_M
6080: 53 43 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e  SC_VER) || defin
6090: 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29  ed(__BORLANDC__)
60a0: 0a 20 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74  .  typedef __int
60b0: 36 34 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b  64 sqlite_int64;
60c0: 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 69 67  .  typedef unsig
60d0: 6e 65 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69  ned __int64 sqli
60e0: 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65  te_uint64;.#else
60f0: 0a 20 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20  .  typedef long 
6100: 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f  long int sqlite_
6110: 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66  int64;.  typedef
6120: 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
6130: 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75  ong int sqlite_u
6140: 69 6e 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79  int64;.#endif.ty
6150: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74  pedef sqlite_int
6160: 36 34 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  64 sqlite3_int64
6170: 3b 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65  ;.typedef sqlite
6180: 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  _uint64 sqlite3_
6190: 75 69 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49  uint64;../*.** I
61a0: 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20  f compiling for 
61b0: 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74  a processor that
61c0: 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20   lacks floating 
61d0: 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a  point support,.*
61e0: 2a 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74  * substitute int
61f0: 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e  eger for floatin
6200: 67 2d 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64  g-point..*/.#ifd
6210: 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
6220: 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20  LOATING_POINT.# 
6230: 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71  define double sq
6240: 6c 69 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64  lite3_int64.#end
6250: 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  if../*.** CAPI3R
6260: 45 46 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61  EF: Closing A Da
6270: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
6280: 6e 20 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31  n {H12010} <S301
6290: 30 30 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a  00><S40200>.**.*
62a0: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
62b0: 73 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  s the destructor
62c0: 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65   for the [sqlite
62d0: 33 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a  3] object..**.**
62e0: 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68   Applications sh
62f0: 6f 75 6c 64 20 5b 73 71 6c 69 74 65 33 5f 66 69  ould [sqlite3_fi
6300: 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a  nalize | finaliz
6310: 65 5d 20 61 6c 6c 20 5b 70 72 65 70 61 72 65 64  e] all [prepared
6320: 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20   statements].** 
6330: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  and [sqlite3_blo
6340: 62 5f 63 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d  b_close | close]
6350: 20 61 6c 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c   all [BLOB handl
6360: 65 73 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77  es] associated w
6370: 69 74 68 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69  ith.** the [sqli
6380: 74 65 33 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f  te3] object prio
6390: 72 20 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20  r to attempting 
63a0: 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a  to close the obj
63b0: 65 63 74 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  ect..** The [sql
63c0: 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 29  ite3_next_stmt()
63d0: 5d 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20  ] interface can 
63e0: 62 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  be used to locat
63f0: 65 20 61 6c 6c 0a 2a 2a 20 5b 70 72 65 70 61 72  e all.** [prepar
6400: 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 61  ed statements] a
6410: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
6420: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
6430: 63 74 69 6f 6e 5d 20 69 66 20 64 65 73 69 72 65  ction] if desire
6440: 64 2e 0a 2a 2a 20 54 79 70 69 63 61 6c 20 63 6f  d..** Typical co
6450: 64 65 20 6d 69 67 68 74 20 6c 6f 6f 6b 20 6c 69  de might look li
6460: 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c  ke this:.**.** <
6470: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
6480: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  .** sqlite3_stmt
6490: 20 2a 70 53 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c   *pStmt;.** whil
64a0: 65 28 20 28 70 53 74 6d 74 20 3d 20 73 71 6c 69  e( (pStmt = sqli
64b0: 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 64 62  te3_next_stmt(db
64c0: 2c 20 30 29 29 21 3d 30 20 29 7b 0a 2a 2a 20 26  , 0))!=0 ){.** &
64d0: 6e 62 73 70 3b 20 20 20 73 71 6c 69 74 65 33 5f  nbsp;   sqlite3_
64e0: 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
64f0: 0a 2a 2a 20 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  .** }.** </pre><
6500: 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
6510: 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 63  ** If [sqlite3_c
6520: 6c 6f 73 65 28 29 5d 20 69 73 20 69 6e 76 6f 6b  lose()] is invok
6530: 65 64 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73  ed while a trans
6540: 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a  action is open,.
6550: 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ** the transacti
6560: 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  on is automatica
6570: 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  lly rolled back.
6580: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 70 61 72  .**.** The C par
6590: 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
65a0: 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 6d 75 73  e3_close(C)] mus
65b0: 74 20 62 65 20 65 69 74 68 65 72 20 61 20 4e 55  t be either a NU
65c0: 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 72  LL.** pointer or
65d0: 20 61 6e 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62   an [sqlite3] ob
65e0: 6a 65 63 74 20 70 6f 69 6e 74 65 72 20 6f 62 74  ject pointer obt
65f0: 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73  ained.** from [s
6600: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
6610: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
6620: 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  )], or.** [sqlit
6630: 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61  e3_open_v2()], a
6640: 6e 64 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c  nd not previousl
6650: 79 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  y closed..**.** 
6660: 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
6670: 20 5b 48 31 32 30 31 31 5d 20 5b 48 31 32 30 31   [H12011] [H1201
6680: 32 5d 20 5b 48 31 32 30 31 33 5d 20 5b 48 31 32  2] [H12013] [H12
6690: 30 31 34 5d 20 5b 48 31 32 30 31 35 5d 20 5b 48  014] [H12015] [H
66a0: 31 32 30 31 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12019].*/.SQLITE
66b0: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
66c0: 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a  _close(sqlite3 *
66d0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79  );../*.** The ty
66e0: 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63  pe for a callbac
66f0: 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54  k function..** T
6700: 68 69 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e  his is legacy an
6710: 64 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 49  d deprecated.  I
6720: 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f  t is included fo
6730: 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20  r historical.** 
6740: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e  compatibility an
6750: 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e  d is not documen
6760: 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ted..*/.typedef 
6770: 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61  int (*sqlite3_ca
6780: 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e  llback)(void*,in
6790: 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a  t,char**, char**
67a0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
67b0: 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65  EF: One-Step Que
67c0: 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74  ry Execution Int
67d0: 65 72 66 61 63 65 20 7b 48 31 32 31 30 30 7d 20  erface {H12100} 
67e0: 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10000>.**.** T
67f0: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  he sqlite3_exec(
6800: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 61  ) interface is a
6810: 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20   convenient way 
6820: 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f  of running one o
6830: 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 73 74  r more.** SQL st
6840: 61 74 65 6d 65 6e 74 73 20 77 69 74 68 6f 75 74  atements without
6850: 20 68 61 76 69 6e 67 20 74 6f 20 77 72 69 74 65   having to write
6860: 20 61 20 6c 6f 74 20 6f 66 20 43 20 63 6f 64 65   a lot of C code
6870: 2e 20 20 54 68 65 20 55 54 46 2d 38 20 65 6e 63  .  The UTF-8 enc
6880: 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74  oded.** SQL stat
6890: 65 6d 65 6e 74 73 20 61 72 65 20 70 61 73 73 65  ements are passe
68a0: 64 20 69 6e 20 61 73 20 74 68 65 20 73 65 63 6f  d in as the seco
68b0: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
68c0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a  sqlite3_exec()..
68d0: 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74  ** The statement
68e0: 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20  s are evaluated 
68f0: 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e 74 69 6c  one by one until
6900: 20 65 69 74 68 65 72 20 61 6e 20 65 72 72 6f 72   either an error
6910: 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 72   or.** an interr
6920: 75 70 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  upt is encounter
6930: 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20 74 68 65  ed, or until the
6940: 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e 65 2e 20  y are all done. 
6950: 20 54 68 65 20 33 72 64 20 70 61 72 61 6d 65 74   The 3rd paramet
6960: 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 74 69  er.** is an opti
6970: 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 74 68  onal callback th
6980: 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  at is invoked on
6990: 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20  ce for each row 
69a0: 6f 66 20 61 6e 79 20 71 75 65 72 79 0a 2a 2a 20  of any query.** 
69b0: 72 65 73 75 6c 74 73 20 70 72 6f 64 75 63 65 64  results produced
69c0: 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74   by the SQL stat
69d0: 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 35 74 68  ements.  The 5th
69e0: 20 70 61 72 61 6d 65 74 65 72 20 74 65 6c 6c 73   parameter tells
69f0: 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20 77 72 69   where.** to wri
6a00: 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73  te any error mes
6a10: 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  sages..**.** The
6a20: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70   error message p
6a30: 61 73 73 65 64 20 62 61 63 6b 20 74 68 72 6f 75  assed back throu
6a40: 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61 6d  gh the 5th param
6a50: 65 74 65 72 20 69 73 20 68 65 6c 64 0a 2a 2a 20  eter is held.** 
6a60: 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  in memory obtain
6a70: 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
6a80: 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 6f 20  _malloc()].  To 
6a90: 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c  avoid a memory l
6aa0: 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c  eak,.** the call
6ab0: 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  ing application 
6ac0: 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 71 6c  should call [sql
6ad0: 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20  ite3_free()] on 
6ae0: 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73  any error.** mes
6af0: 73 61 67 65 20 72 65 74 75 72 6e 65 64 20 74 68  sage returned th
6b00: 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61  rough the 5th pa
6b10: 72 61 6d 65 74 65 72 20 77 68 65 6e 20 69 74 20  rameter when it 
6b20: 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69  has finished usi
6b30: 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20  ng.** the error 
6b40: 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49  message..**.** I
6b50: 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  f the SQL statem
6b60: 65 6e 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70  ent in the 2nd p
6b70: 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c  arameter is NULL
6b80: 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72   or an empty str
6b90: 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73 74 72 69  ing.** or a stri
6ba0: 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e  ng containing on
6bb0: 6c 79 20 77 68 69 74 65 73 70 61 63 65 20 61 6e  ly whitespace an
6bc0: 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65 6e  d comments, then
6bd0: 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65   no SQL.** state
6be0: 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61  ments are evalua
6bf0: 74 65 64 20 61 6e 64 20 74 68 65 20 64 61 74 61  ted and the data
6c00: 62 61 73 65 20 69 73 20 6e 6f 74 20 63 68 61 6e  base is not chan
6c10: 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ged..**.** The s
6c20: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e  qlite3_exec() in
6c30: 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65  terface is imple
6c40: 6d 65 6e 74 65 64 20 69 6e 20 74 65 72 6d 73 20  mented in terms 
6c50: 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70  of.** [sqlite3_p
6c60: 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73  repare_v2()], [s
6c70: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20  qlite3_step()], 
6c80: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  and [sqlite3_fin
6c90: 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 68 65  alize()]..** The
6ca0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
6cb0: 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74  routine does not
6cc0: 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61  hing to the data
6cd0: 62 61 73 65 20 74 68 61 74 20 63 61 6e 6e 6f 74  base that cannot
6ce0: 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b   be done.** by [
6cf0: 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
6d00: 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  v2()], [sqlite3_
6d10: 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71  step()], and [sq
6d20: 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
6d30: 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  ]..**.** The fir
6d40: 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  st parameter to 
6d50: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
6d60: 20 6d 75 73 74 20 62 65 20 61 6e 20 76 61 6c 69   must be an vali
6d70: 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a 20 5b 64  d and open.** [d
6d80: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
6d90: 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  on]..**.** The d
6da0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
6db0: 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 63  on must not be c
6dc0: 6c 6f 73 65 64 20 77 68 69 6c 65 0a 2a 2a 20 5b  losed while.** [
6dd0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
6de0: 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a  is running..**.*
6df0: 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75  * The calling fu
6e00: 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73  nction should us
6e10: 65 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  e [sqlite3_free(
6e20: 29 5d 20 74 6f 20 66 72 65 65 0a 2a 2a 20 74 68  )] to free.** th
6e30: 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 2a 65  e memory that *e
6e40: 72 72 6d 73 67 20 69 73 20 6c 65 66 74 20 70 6f  rrmsg is left po
6e50: 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63 65 20 74  inting at once t
6e60: 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73  he error.** mess
6e70: 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72  age is no longer
6e80: 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54   needed..**.** T
6e90: 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
6ea0: 20 74 65 78 74 20 69 6e 20 74 68 65 20 32 6e 64   text in the 2nd
6eb0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
6ec0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a  qlite3_exec()].*
6ed0: 2a 20 6d 75 73 74 20 72 65 6d 61 69 6e 20 75 6e  * must remain un
6ee0: 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 5b 73  changed while [s
6ef0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 69  qlite3_exec()] i
6f00: 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a  s running..**.**
6f10: 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
6f20: 2a 20 5b 48 31 32 31 30 31 5d 20 5b 48 31 32 31  * [H12101] [H121
6f30: 30 32 5d 20 5b 48 31 32 31 30 34 5d 20 5b 48 31  02] [H12104] [H1
6f40: 32 31 30 35 5d 20 5b 48 31 32 31 30 37 5d 20 5b  2105] [H12107] [
6f50: 48 31 32 31 31 30 5d 20 5b 48 31 32 31 31 33 5d  H12110] [H12113]
6f60: 20 5b 48 31 32 31 31 36 5d 0a 2a 2a 20 5b 48 31   [H12116].** [H1
6f70: 32 31 31 39 5d 20 5b 48 31 32 31 32 32 5d 20 5b  2119] [H12122] [
6f80: 48 31 32 31 32 35 5d 20 5b 48 31 32 31 33 31 5d  H12125] [H12131]
6f90: 20 5b 48 31 32 31 33 34 5d 20 5b 48 31 32 31 33   [H12134] [H1213
6fa0: 37 5d 20 5b 48 31 32 31 33 38 5d 0a 2a 2f 0a 53  7] [H12138].*/.S
6fb0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
6fc0: 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20 73 71  lite3_exec(.  sq
6fd0: 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 20 20 20  lite3*,         
6fe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ff0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f           /* An o
7000: 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  pen database */.
7010: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71    const char *sq
7020: 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
7030: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7040: 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c 75 61  SQL to be evalua
7050: 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 63  ted */.  int (*c
7060: 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69  allback)(void*,i
7070: 6e 74 2c 63 68 61 72 2a 2a 2c 63 68 61 72 2a 2a  nt,char**,char**
7080: 29 2c 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20  ),  /* Callback 
7090: 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
70a0: 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20 20 20  id *,           
70b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70c0: 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20           /* 1st 
70d0: 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c  argument to call
70e0: 62 61 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a  back */.  char *
70f0: 2a 65 72 72 6d 73 67 20 20 20 20 20 20 20 20 20  *errmsg         
7100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7110: 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73       /* Error ms
7120: 67 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a  g written here *
7130: 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.);../*.** CAPI
7140: 33 52 45 46 3a 20 52 65 73 75 6c 74 20 43 6f 64  3REF: Result Cod
7150: 65 73 20 7b 48 31 30 32 31 30 7d 20 3c 53 31 30  es {H10210} <S10
7160: 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  700>.** KEYWORDS
7170: 3a 20 53 51 4c 49 54 45 5f 4f 4b 20 7b 65 72 72  : SQLITE_OK {err
7180: 6f 72 20 63 6f 64 65 7d 20 7b 65 72 72 6f 72 20  or code} {error 
7190: 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52  codes}.** KEYWOR
71a0: 44 53 3a 20 7b 72 65 73 75 6c 74 20 63 6f 64 65  DS: {result code
71b0: 7d 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 73 7d  } {result codes}
71c0: 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51 4c 69  .**.** Many SQLi
71d0: 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74  te functions ret
71e0: 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72  urn an integer r
71f0: 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20  esult code from 
7200: 74 68 65 20 73 65 74 20 73 68 6f 77 6e 0a 2a 2a  the set shown.**
7210: 20 68 65 72 65 20 69 6e 20 6f 72 64 65 72 20 74   here in order t
7220: 6f 20 69 6e 64 69 63 61 74 65 73 20 73 75 63 63  o indicates succ
7230: 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a  ess or failure..
7240: 2a 2a 0a 2a 2a 20 4e 65 77 20 65 72 72 6f 72 20  **.** New error 
7250: 63 6f 64 65 73 20 6d 61 79 20 62 65 20 61 64 64  codes may be add
7260: 65 64 20 69 6e 20 66 75 74 75 72 65 20 76 65 72  ed in future ver
7270: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e  sions of SQLite.
7280: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
7290: 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52   [SQLITE_IOERR_R
72a0: 45 41 44 20 7c 20 65 78 74 65 6e 64 65 64 20 72  EAD | extended r
72b0: 65 73 75 6c 74 20 63 6f 64 65 73 5d 0a 2a 2f 0a  esult codes].*/.
72c0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
72d0: 4b 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20  K           0   
72e0: 2f 2a 20 53 75 63 63 65 73 73 66 75 6c 20 72 65  /* Successful re
72f0: 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 65 67 69 6e  sult */./* begin
7300: 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f  ning-of-error-co
7310: 64 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  des */.#define S
7320: 51 4c 49 54 45 5f 45 52 52 4f 52 20 20 20 20 20  QLITE_ERROR     
7330: 20 20 20 31 20 20 20 2f 2a 20 53 51 4c 20 65 72     1   /* SQL er
7340: 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67 20 64  ror or missing d
7350: 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69  atabase */.#defi
7360: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e  ne SQLITE_INTERN
7370: 41 4c 20 20 20 20 20 32 20 20 20 2f 2a 20 49 6e  AL     2   /* In
7380: 74 65 72 6e 61 6c 20 6c 6f 67 69 63 20 65 72 72  ternal logic err
7390: 6f 72 20 69 6e 20 53 51 4c 69 74 65 20 2a 2f 0a  or in SQLite */.
73a0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50  #define SQLITE_P
73b0: 45 52 4d 20 20 20 20 20 20 20 20 20 33 20 20 20  ERM         3   
73c0: 2f 2a 20 41 63 63 65 73 73 20 70 65 72 6d 69 73  /* Access permis
73d0: 73 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23  sion denied */.#
73e0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 42  define SQLITE_AB
73f0: 4f 52 54 20 20 20 20 20 20 20 20 34 20 20 20 2f  ORT        4   /
7400: 2a 20 43 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69  * Callback routi
7410: 6e 65 20 72 65 71 75 65 73 74 65 64 20 61 6e 20  ne requested an 
7420: 61 62 6f 72 74 20 2a 2f 0a 23 64 65 66 69 6e 65  abort */.#define
7430: 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20 20 20   SQLITE_BUSY    
7440: 20 20 20 20 20 35 20 20 20 2f 2a 20 54 68 65 20       5   /* The 
7450: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
7460: 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69   locked */.#defi
7470: 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  ne SQLITE_LOCKED
7480: 20 20 20 20 20 20 20 36 20 20 20 2f 2a 20 41 20         6   /* A 
7490: 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74  table in the dat
74a0: 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 20  abase is locked 
74b0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
74c0: 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 37  E_NOMEM        7
74d0: 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 29     /* A malloc()
74e0: 20 66 61 69 6c 65 64 20 2a 2f 0a 23 64 65 66 69   failed */.#defi
74f0: 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e  ne SQLITE_READON
7500: 4c 59 20 20 20 20 20 38 20 20 20 2f 2a 20 41 74  LY     8   /* At
7510: 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61  tempt to write a
7520: 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62 61   readonly databa
7530: 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  se */.#define SQ
7540: 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 20  LITE_INTERRUPT  
7550: 20 20 39 20 20 20 2f 2a 20 4f 70 65 72 61 74 69    9   /* Operati
7560: 6f 6e 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79  on terminated by
7570: 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
7580: 70 74 28 29 2a 2f 0a 23 64 65 66 69 6e 65 20 53  pt()*/.#define S
7590: 51 4c 49 54 45 5f 49 4f 45 52 52 20 20 20 20 20  QLITE_IOERR     
75a0: 20 20 31 30 20 20 20 2f 2a 20 53 6f 6d 65 20 6b    10   /* Some k
75b0: 69 6e 64 20 6f 66 20 64 69 73 6b 20 49 2f 4f 20  ind of disk I/O 
75c0: 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a  error occurred *
75d0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
75e0: 5f 43 4f 52 52 55 50 54 20 20 20 20 20 31 31 20  _CORRUPT     11 
75f0: 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
7600: 65 20 64 69 73 6b 20 69 6d 61 67 65 20 69 73 20  e disk image is 
7610: 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f 0a 23 64 65  malformed */.#de
7620: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54 46  fine SQLITE_NOTF
7630: 4f 55 4e 44 20 20 20 20 31 32 20 20 20 2f 2a 20  OUND    12   /* 
7640: 4e 4f 54 20 55 53 45 44 2e 20 54 61 62 6c 65 20  NOT USED. Table 
7650: 6f 72 20 72 65 63 6f 72 64 20 6e 6f 74 20 66 6f  or record not fo
7660: 75 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  und */.#define S
7670: 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20 20 20  QLITE_FULL      
7680: 20 20 31 33 20 20 20 2f 2a 20 49 6e 73 65 72 74    13   /* Insert
7690: 69 6f 6e 20 66 61 69 6c 65 64 20 62 65 63 61 75  ion failed becau
76a0: 73 65 20 64 61 74 61 62 61 73 65 20 69 73 20 66  se database is f
76b0: 75 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ull */.#define S
76c0: 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 20  QLITE_CANTOPEN  
76d0: 20 20 31 34 20 20 20 2f 2a 20 55 6e 61 62 6c 65    14   /* Unable
76e0: 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74   to open the dat
76f0: 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64  abase file */.#d
7700: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 4f  efine SQLITE_PRO
7710: 54 4f 43 4f 4c 20 20 20 20 31 35 20 20 20 2f 2a  TOCOL    15   /*
7720: 20 4e 4f 54 20 55 53 45 44 2e 20 44 61 74 61 62   NOT USED. Datab
7730: 61 73 65 20 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f  ase lock protoco
7740: 6c 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69  l error */.#defi
7750: 6e 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20  ne SQLITE_EMPTY 
7760: 20 20 20 20 20 20 31 36 20 20 20 2f 2a 20 44 61        16   /* Da
7770: 74 61 62 61 73 65 20 69 73 20 65 6d 70 74 79 20  tabase is empty 
7780: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7790: 45 5f 53 43 48 45 4d 41 20 20 20 20 20 20 31 37  E_SCHEMA      17
77a0: 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
77b0: 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  se schema change
77c0: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
77d0: 49 54 45 5f 54 4f 4f 42 49 47 20 20 20 20 20 20  ITE_TOOBIG      
77e0: 31 38 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f  18   /* String o
77f0: 72 20 42 4c 4f 42 20 65 78 63 65 65 64 73 20 73  r BLOB exceeds s
7800: 69 7a 65 20 6c 69 6d 69 74 20 2a 2f 0a 23 64 65  ize limit */.#de
7810: 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 53  fine SQLITE_CONS
7820: 54 52 41 49 4e 54 20 20 31 39 20 20 20 2f 2a 20  TRAINT  19   /* 
7830: 41 62 6f 72 74 20 64 75 65 20 74 6f 20 63 6f 6e  Abort due to con
7840: 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
7850: 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  n */.#define SQL
7860: 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 20 20 20  ITE_MISMATCH    
7870: 32 30 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70  20   /* Data typ
7880: 65 20 6d 69 73 6d 61 74 63 68 20 2a 2f 0a 23 64  e mismatch */.#d
7890: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53  efine SQLITE_MIS
78a0: 55 53 45 20 20 20 20 20 20 32 31 20 20 20 2f 2a  USE      21   /*
78b0: 20 4c 69 62 72 61 72 79 20 75 73 65 64 20 69 6e   Library used in
78c0: 63 6f 72 72 65 63 74 6c 79 20 2a 2f 0a 23 64 65  correctly */.#de
78d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4c 46  fine SQLITE_NOLF
78e0: 53 20 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20  S       22   /* 
78f0: 55 73 65 73 20 4f 53 20 66 65 61 74 75 72 65 73  Uses OS features
7900: 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20 6f   not supported o
7910: 6e 20 68 6f 73 74 20 2a 2f 0a 23 64 65 66 69 6e  n host */.#defin
7920: 65 20 53 51 4c 49 54 45 5f 41 55 54 48 20 20 20  e SQLITE_AUTH   
7930: 20 20 20 20 20 32 33 20 20 20 2f 2a 20 41 75 74       23   /* Aut
7940: 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65  horization denie
7950: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
7960: 49 54 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20  ITE_FORMAT      
7970: 32 34 20 20 20 2f 2a 20 41 75 78 69 6c 69 61 72  24   /* Auxiliar
7980: 79 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61  y database forma
7990: 74 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69  t error */.#defi
79a0: 6e 65 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20  ne SQLITE_RANGE 
79b0: 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20 32 6e        25   /* 2n
79c0: 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  d parameter to s
79d0: 71 6c 69 74 65 33 5f 62 69 6e 64 20 6f 75 74 20  qlite3_bind out 
79e0: 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 23 64 65 66  of range */.#def
79f0: 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44  ine SQLITE_NOTAD
7a00: 42 20 20 20 20 20 20 32 36 20 20 20 2f 2a 20 46  B      26   /* F
7a10: 69 6c 65 20 6f 70 65 6e 65 64 20 74 68 61 74 20  ile opened that 
7a20: 69 73 20 6e 6f 74 20 61 20 64 61 74 61 62 61 73  is not a databas
7a30: 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e  e file */.#defin
7a40: 65 20 53 51 4c 49 54 45 5f 52 4f 57 20 20 20 20  e SQLITE_ROW    
7a50: 20 20 20 20 20 31 30 30 20 20 2f 2a 20 73 71 6c       100  /* sql
7a60: 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20  ite3_step() has 
7a70: 61 6e 6f 74 68 65 72 20 72 6f 77 20 72 65 61 64  another row read
7a80: 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  y */.#define SQL
7a90: 49 54 45 5f 44 4f 4e 45 20 20 20 20 20 20 20 20  ITE_DONE        
7aa0: 31 30 31 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f  101  /* sqlite3_
7ab0: 73 74 65 70 28 29 20 68 61 73 20 66 69 6e 69 73  step() has finis
7ac0: 68 65 64 20 65 78 65 63 75 74 69 6e 67 20 2a 2f  hed executing */
7ad0: 0a 2f 2a 20 65 6e 64 2d 6f 66 2d 65 72 72 6f 72  ./* end-of-error
7ae0: 2d 63 6f 64 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  -codes */../*.**
7af0: 20 43 41 50 49 33 52 45 46 3a 20 45 78 74 65 6e   CAPI3REF: Exten
7b00: 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64 65 73  ded Result Codes
7b10: 20 7b 48 31 30 32 32 30 7d 20 3c 53 31 30 37 30   {H10220} <S1070
7b20: 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
7b30: 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20  {extended error 
7b40: 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20  code} {extended 
7b50: 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20  error codes}.** 
7b60: 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e  KEYWORDS: {exten
7b70: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 7d  ded result code}
7b80: 20 7b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c   {extended resul
7b90: 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 49  t codes}.**.** I
7ba0: 6e 20 69 74 73 20 64 65 66 61 75 6c 74 20 63 6f  n its default co
7bb0: 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 53 51 4c  nfiguration, SQL
7bc0: 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73  ite API routines
7bd0: 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 32   return one of 2
7be0: 36 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51  6 integer.** [SQ
7bf0: 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 73 75 6c 74  LITE_OK | result
7c00: 20 63 6f 64 65 73 5d 2e 20 20 48 6f 77 65 76 65   codes].  Howeve
7c10: 72 2c 20 65 78 70 65 72 69 65 6e 63 65 20 68 61  r, experience ha
7c20: 73 20 73 68 6f 77 6e 20 74 68 61 74 20 6d 61 6e  s shown that man
7c30: 79 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 72 65  y of.** these re
7c40: 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 74  sult codes are t
7c50: 6f 6f 20 63 6f 61 72 73 65 2d 67 72 61 69 6e 65  oo coarse-graine
7c60: 64 2e 20 20 54 68 65 79 20 64 6f 20 6e 6f 74 20  d.  They do not 
7c70: 70 72 6f 76 69 64 65 20 61 73 0a 2a 2a 20 6d 75  provide as.** mu
7c80: 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ch information a
7c90: 62 6f 75 74 20 70 72 6f 62 6c 65 6d 73 20 61 73  bout problems as
7ca0: 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67   programmers mig
7cb0: 68 74 20 6c 69 6b 65 2e 20 20 49 6e 20 61 6e 20  ht like.  In an 
7cc0: 65 66 66 6f 72 74 20 74 6f 0a 2a 2a 20 61 64 64  effort to.** add
7cd0: 72 65 73 73 20 74 68 69 73 2c 20 6e 65 77 65 72  ress this, newer
7ce0: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
7cf0: 69 74 65 20 28 76 65 72 73 69 6f 6e 20 33 2e 33  ite (version 3.3
7d00: 2e 38 20 61 6e 64 20 6c 61 74 65 72 29 20 69 6e  .8 and later) in
7d10: 63 6c 75 64 65 0a 2a 2a 20 73 75 70 70 6f 72 74  clude.** support
7d20: 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
7d30: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 74 68 61  result codes tha
7d40: 74 20 70 72 6f 76 69 64 65 20 6d 6f 72 65 20 64  t provide more d
7d50: 65 74 61 69 6c 65 64 20 69 6e 66 6f 72 6d 61 74  etailed informat
7d60: 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 65 72 72  ion.** about err
7d70: 6f 72 73 2e 20 54 68 65 20 65 78 74 65 6e 64 65  ors. The extende
7d80: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61  d result codes a
7d90: 72 65 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69  re enabled or di
7da0: 73 61 62 6c 65 64 0a 2a 2a 20 6f 6e 20 61 20 70  sabled.** on a p
7db0: 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  er database conn
7dc0: 65 63 74 69 6f 6e 20 62 61 73 69 73 20 75 73 69  ection basis usi
7dd0: 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  ng the.** [sqlit
7de0: 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75  e3_extended_resu
7df0: 6c 74 5f 63 6f 64 65 73 28 29 5d 20 41 50 49 2e  lt_codes()] API.
7e00: 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74  .**.** Some of t
7e10: 68 65 20 61 76 61 69 6c 61 62 6c 65 20 65 78 74  he available ext
7e20: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
7e30: 65 73 20 61 72 65 20 6c 69 73 74 65 64 20 68 65  es are listed he
7e40: 72 65 2e 0a 2a 2a 20 4f 6e 65 20 6d 61 79 20 65  re..** One may e
7e50: 78 70 65 63 74 20 74 68 65 20 6e 75 6d 62 65 72  xpect the number
7e60: 20 6f 66 20 65 78 74 65 6e 64 65 64 20 72 65 73   of extended res
7e70: 75 6c 74 20 63 6f 64 65 73 20 77 69 6c 6c 20 62  ult codes will b
7e80: 65 20 65 78 70 61 6e 64 0a 2a 2a 20 6f 76 65 72  e expand.** over
7e90: 20 74 69 6d 65 2e 20 20 53 6f 66 74 77 61 72 65   time.  Software
7ea0: 20 74 68 61 74 20 75 73 65 73 20 65 78 74 65 6e   that uses exten
7eb0: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
7ec0: 20 73 68 6f 75 6c 64 20 65 78 70 65 63 74 0a 2a   should expect.*
7ed0: 2a 20 74 6f 20 73 65 65 20 6e 65 77 20 72 65 73  * to see new res
7ee0: 75 6c 74 20 63 6f 64 65 73 20 69 6e 20 66 75 74  ult codes in fut
7ef0: 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20  ure releases of 
7f00: 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  SQLite..**.** Th
7f10: 65 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 73 75  e SQLITE_OK resu
7f20: 6c 74 20 63 6f 64 65 20 77 69 6c 6c 20 6e 65 76  lt code will nev
7f30: 65 72 20 62 65 20 65 78 74 65 6e 64 65 64 2e 20  er be extended. 
7f40: 20 49 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a   It will always.
7f50: 2a 2a 20 62 65 20 65 78 61 63 74 6c 79 20 7a 65  ** be exactly ze
7f60: 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ro..*/.#define S
7f70: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
7f80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53                (S
7f90: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31  QLITE_IOERR | (1
7fa0: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
7fb0: 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
7fc0: 5f 52 45 41 44 20 20 20 20 20 20 20 20 28 53 51  _READ        (SQ
7fd0: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c  LITE_IOERR | (2<
7fe0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
7ff0: 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20  ITE_IOERR_WRITE 
8000: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
8010: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c  ITE_IOERR | (3<<
8020: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
8030: 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20  TE_IOERR_FSYNC  
8040: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49             (SQLI
8050: 54 45 5f 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38  TE_IOERR | (4<<8
8060: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8070: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e  E_IOERR_DIR_FSYN
8080: 43 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54  C         (SQLIT
8090: 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38 29  E_IOERR | (5<<8)
80a0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
80b0: 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20  _IOERR_TRUNCATE 
80c0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
80d0: 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c 38 29 29  _IOERR | (6<<8))
80e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
80f0: 49 4f 45 52 52 5f 46 53 54 41 54 20 20 20 20 20  IOERR_FSTAT     
8100: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
8110: 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29 29 0a  IOERR | (7<<8)).
8120: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8130: 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20  OERR_UNLOCK     
8140: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
8150: 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29 29 0a 23  OERR | (8<<8)).#
8160: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
8170: 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20 20 20 20  ERR_RDLOCK      
8180: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
8190: 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 0a 23 64  ERR | (9<<8)).#d
81a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
81b0: 52 52 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20  RR_DELETE       
81c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
81d0: 52 52 20 7c 20 28 31 30 3c 3c 38 29 29 0a 23 64  RR | (10<<8)).#d
81e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
81f0: 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20 20 20 20  RR_BLOCKED      
8200: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
8210: 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a 23 64  RR | (11<<8)).#d
8220: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
8230: 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20  RR_NOMEM        
8240: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
8250: 52 52 20 7c 20 28 31 32 3c 3c 38 29 29 0a 23 64  RR | (12<<8)).#d
8260: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
8270: 52 52 5f 41 43 43 45 53 53 20 20 20 20 20 20 20  RR_ACCESS       
8280: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
8290: 52 52 20 7c 20 28 31 33 3c 3c 38 29 29 0a 23 64  RR | (13<<8)).#d
82a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
82b0: 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44  RR_CHECKRESERVED
82c0: 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f 49 4f 45  LOCK (SQLITE_IOE
82d0: 52 52 20 7c 20 28 31 34 3c 3c 38 29 29 0a 23 64  RR | (14<<8)).#d
82e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
82f0: 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20  RR_LOCK         
8300: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
8310: 52 52 20 7c 20 28 31 35 3c 3c 38 29 29 0a 23 64  RR | (15<<8)).#d
8320: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
8330: 52 52 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20  RR_CLOSE        
8340: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
8350: 52 52 20 7c 20 28 31 36 3c 3c 38 29 29 0a 23 64  RR | (16<<8)).#d
8360: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
8370: 52 52 5f 44 49 52 5f 43 4c 4f 53 45 20 20 20 20  RR_DIR_CLOSE    
8380: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
8390: 52 52 20 7c 20 28 31 37 3c 3c 38 29 29 0a 23 64  RR | (17<<8)).#d
83a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43  efine SQLITE_LOC
83b0: 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 20  KED_SHAREDCACHE 
83c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4c 4f 43       (SQLITE_LOC
83d0: 4b 45 44 20 7c 20 28 31 3c 3c 38 29 20 29 0a 0a  KED | (1<<8) )..
83e0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
83f0: 46 6c 61 67 73 20 46 6f 72 20 46 69 6c 65 20 4f  Flags For File O
8400: 70 65 6e 20 4f 70 65 72 61 74 69 6f 6e 73 20 7b  pen Operations {
8410: 48 31 30 32 33 30 7d 20 3c 48 31 31 31 32 30 3e  H10230} <H11120>
8420: 20 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <H12700>.**.** 
8430: 54 68 65 73 65 20 62 69 74 20 76 61 6c 75 65 73  These bit values
8440: 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f   are intended fo
8450: 72 20 75 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20  r use in the.** 
8460: 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  3rd parameter to
8470: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70   the [sqlite3_op
8480: 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61  en_v2()] interfa
8490: 63 65 20 61 6e 64 0a 2a 2a 20 69 6e 20 74 68 65  ce and.** in the
84a0: 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
84b0: 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68  o the xOpen meth
84c0: 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71  od of the.** [sq
84d0: 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63  lite3_vfs] objec
84e0: 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  t..*/.#define SQ
84f0: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
8500: 4c 59 20 20 20 20 20 20 20 20 20 30 78 30 30 30  LY         0x000
8510: 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51  00001.#define SQ
8520: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
8530: 49 54 45 20 20 20 20 20 20 20 20 30 78 30 30 30  ITE        0x000
8540: 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51  00002.#define SQ
8550: 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
8560: 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30             0x000
8570: 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51  00004.#define SQ
8580: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
8590: 4f 4e 43 4c 4f 53 45 20 20 20 20 30 78 30 30 30  ONCLOSE    0x000
85a0: 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51  00008.#define SQ
85b0: 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
85c0: 49 56 45 20 20 20 20 20 20 20 20 30 78 30 30 30  IVE        0x000
85d0: 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51  00010.#define SQ
85e0: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
85f0: 42 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30  B          0x000
8600: 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51  00100.#define SQ
8610: 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44  LITE_OPEN_TEMP_D
8620: 42 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30  B          0x000
8630: 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51  00200.#define SQ
8640: 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49  LITE_OPEN_TRANSI
8650: 45 4e 54 5f 44 42 20 20 20 20 20 30 78 30 30 30  ENT_DB     0x000
8660: 30 30 34 30 30 0a 23 64 65 66 69 6e 65 20 53 51  00400.#define SQ
8670: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
8680: 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30  OURNAL     0x000
8690: 30 30 38 30 30 0a 23 64 65 66 69 6e 65 20 53 51  00800.#define SQ
86a0: 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a  LITE_OPEN_TEMP_J
86b0: 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30  OURNAL     0x000
86c0: 30 31 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51  01000.#define SQ
86d0: 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55  LITE_OPEN_SUBJOU
86e0: 52 4e 41 4c 20 20 20 20 20 20 20 30 78 30 30 30  RNAL       0x000
86f0: 30 32 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51  02000.#define SQ
8700: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52  LITE_OPEN_MASTER
8710: 5f 4a 4f 55 52 4e 41 4c 20 20 20 30 78 30 30 30  _JOURNAL   0x000
8720: 30 34 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51  04000.#define SQ
8730: 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45  LITE_OPEN_NOMUTE
8740: 58 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30  X          0x000
8750: 30 38 30 30 30 0a 23 64 65 66 69 6e 65 20 53 51  08000.#define SQ
8760: 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55  LITE_OPEN_FULLMU
8770: 54 45 58 20 20 20 20 20 20 20 20 30 78 30 30 30  TEX        0x000
8780: 31 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  10000../*.** CAP
8790: 49 33 52 45 46 3a 20 44 65 76 69 63 65 20 43 68  I3REF: Device Ch
87a0: 61 72 61 63 74 65 72 69 73 74 69 63 73 20 7b 48  aracteristics {H
87b0: 31 30 32 34 30 7d 20 3c 48 31 31 31 32 30 3e 0a  10240} <H11120>.
87c0: 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 76 69 63  **.** The xDevic
87d0: 65 43 61 70 61 62 69 6c 69 74 69 65 73 20 6d 65  eCapabilities me
87e0: 74 68 6f 64 20 6f 66 20 74 68 65 20 5b 73 71 6c  thod of the [sql
87f0: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d  ite3_io_methods]
8800: 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 74 75 72  .** object retur
8810: 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68  ns an integer wh
8820: 69 63 68 20 69 73 20 61 20 76 65 63 74 6f 72 20  ich is a vector 
8830: 6f 66 20 74 68 65 20 74 68 65 73 65 0a 2a 2a 20  of the these.** 
8840: 62 69 74 20 76 61 6c 75 65 73 20 65 78 70 72 65  bit values expre
8850: 73 73 69 6e 67 20 49 2f 4f 20 63 68 61 72 61 63  ssing I/O charac
8860: 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65  teristics of the
8870: 20 6d 61 73 73 20 73 74 6f 72 61 67 65 0a 2a 2a   mass storage.**
8880: 20 64 65 76 69 63 65 20 74 68 61 74 20 68 6f 6c   device that hol
8890: 64 73 20 74 68 65 20 66 69 6c 65 20 74 68 61 74  ds the file that
88a0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f   the [sqlite3_io
88b0: 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 65 66  _methods].** ref
88c0: 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68  ers to..**.** Th
88d0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
88e0: 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d  TOMIC property m
88f0: 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72  eans that all wr
8900: 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73  ites of.** any s
8910: 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20  ize are atomic. 
8920: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41   The SQLITE_IOCA
8930: 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75  P_ATOMICnnn valu
8940: 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20  es.** mean that 
8950: 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73  writes of blocks
8960: 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79   that are nnn by
8970: 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a  tes in size and.
8980: 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74  ** are aligned t
8990: 6f 20 61 6e 20 61 64 64 72 65 73 73 20 77 68 69  o an address whi
89a0: 63 68 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  ch is an integer
89b0: 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20   multiple of.** 
89c0: 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20  nnn are atomic. 
89d0: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41   The SQLITE_IOCA
89e0: 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61  P_SAFE_APPEND va
89f0: 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61  lue means.** tha
8a00: 74 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 61  t when data is a
8a10: 70 70 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c  ppended to a fil
8a20: 65 2c 20 74 68 65 20 64 61 74 61 20 69 73 20 61  e, the data is a
8a30: 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74  ppended.** first
8a40: 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f   then the size o
8a50: 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 65 78  f the file is ex
8a60: 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68  tended, never th
8a70: 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61  e other.** way a
8a80: 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49  round.  The SQLI
8a90: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  TE_IOCAP_SEQUENT
8aa0: 49 41 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61  IAL property mea
8ab0: 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72  ns that.** infor
8ac0: 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65  mation is writte
8ad0: 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65  n to disk in the
8ae0: 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63   same order as c
8af0: 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74  alls.** to xWrit
8b00: 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  e()..*/.#define 
8b10: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
8b20: 4d 49 43 20 20 20 20 20 20 20 20 20 20 30 78 30  MIC          0x0
8b30: 30 30 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20  0000001.#define 
8b40: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
8b50: 4d 49 43 35 31 32 20 20 20 20 20 20 20 30 78 30  MIC512       0x0
8b60: 30 30 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20  0000002.#define 
8b70: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
8b80: 4d 49 43 31 4b 20 20 20 20 20 20 20 20 30 78 30  MIC1K        0x0
8b90: 30 30 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20  0000004.#define 
8ba0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
8bb0: 4d 49 43 32 4b 20 20 20 20 20 20 20 20 30 78 30  MIC2K        0x0
8bc0: 30 30 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20  0000008.#define 
8bd0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
8be0: 4d 49 43 34 4b 20 20 20 20 20 20 20 20 30 78 30  MIC4K        0x0
8bf0: 30 30 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20  0000010.#define 
8c00: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
8c10: 4d 49 43 38 4b 20 20 20 20 20 20 20 20 30 78 30  MIC8K        0x0
8c20: 30 30 30 30 30 32 30 0a 23 64 65 66 69 6e 65 20  0000020.#define 
8c30: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
8c40: 4d 49 43 31 36 4b 20 20 20 20 20 20 20 30 78 30  MIC16K       0x0
8c50: 30 30 30 30 30 34 30 0a 23 64 65 66 69 6e 65 20  0000040.#define 
8c60: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
8c70: 4d 49 43 33 32 4b 20 20 20 20 20 20 20 30 78 30  MIC32K       0x0
8c80: 30 30 30 30 30 38 30 0a 23 64 65 66 69 6e 65 20  0000080.#define 
8c90: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
8ca0: 4d 49 43 36 34 4b 20 20 20 20 20 20 20 30 78 30  MIC64K       0x0
8cb0: 30 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20  0000100.#define 
8cc0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46  SQLITE_IOCAP_SAF
8cd0: 45 5f 41 50 50 45 4e 44 20 20 20 20 20 30 78 30  E_APPEND     0x0
8ce0: 30 30 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20  0000200.#define 
8cf0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
8d00: 55 45 4e 54 49 41 4c 20 20 20 20 20 20 30 78 30  UENTIAL      0x0
8d10: 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43  0000400../*.** C
8d20: 41 50 49 33 52 45 46 3a 20 46 69 6c 65 20 4c 6f  API3REF: File Lo
8d30: 63 6b 69 6e 67 20 4c 65 76 65 6c 73 20 7b 48 31  cking Levels {H1
8d40: 30 32 35 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c  0250} <H11120> <
8d50: 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51  H11310>.**.** SQ
8d60: 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f 66  Lite uses one of
8d70: 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76   these integer v
8d80: 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63  alues as the sec
8d90: 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  ond.** argument 
8da0: 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65  to calls it make
8db0: 73 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29  s to the xLock()
8dc0: 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d   and xUnlock() m
8dd0: 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20  ethods.** of an 
8de0: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68  [sqlite3_io_meth
8df0: 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  ods] object..*/.
8e00: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
8e10: 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20  OCK_NONE        
8e20: 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    0.#define SQLI
8e30: 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20  TE_LOCK_SHARED  
8e40: 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
8e50: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45  SQLITE_LOCK_RESE
8e60: 52 56 45 44 20 20 20 20 20 20 32 0a 23 64 65 66  RVED      2.#def
8e70: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  ine SQLITE_LOCK_
8e80: 50 45 4e 44 49 4e 47 20 20 20 20 20 20 20 33 0a  PENDING       3.
8e90: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
8ea0: 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20  OCK_EXCLUSIVE   
8eb0: 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33    4../*.** CAPI3
8ec0: 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61  REF: Synchroniza
8ed0: 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 20  tion Type Flags 
8ee0: 7b 48 31 30 32 36 30 7d 20 3c 48 31 31 31 32 30  {H10260} <H11120
8ef0: 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c  >.**.** When SQL
8f00: 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ite invokes the 
8f10: 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f  xSync() method o
8f20: 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  f an.** [sqlite3
8f30: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  _io_methods] obj
8f40: 65 63 74 20 69 74 20 75 73 65 73 20 61 20 63 6f  ect it uses a co
8f50: 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20  mbination of.** 
8f60: 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 61  these integer va
8f70: 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63 6f  lues as the seco
8f80: 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  nd argument..**.
8f90: 2a 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49  ** When the SQLI
8fa0: 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59  TE_SYNC_DATAONLY
8fb0: 20 66 6c 61 67 20 69 73 20 75 73 65 64 2c 20 69   flag is used, i
8fc0: 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
8fd0: 0a 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69  .** sync operati
8fe0: 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f  on only needs to
8ff0: 20 66 6c 75 73 68 20 64 61 74 61 20 74 6f 20 6d   flush data to m
9000: 61 73 73 20 73 74 6f 72 61 67 65 2e 20 20 49 6e  ass storage.  In
9010: 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  ode.** informati
9020: 6f 6e 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 66  on need not be f
9030: 6c 75 73 68 65 64 2e 20 49 66 20 74 68 65 20 6c  lushed. If the l
9040: 6f 77 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f  ower four bits o
9050: 66 20 74 68 65 20 66 6c 61 67 0a 2a 2a 20 65 71  f the flag.** eq
9060: 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  ual SQLITE_SYNC_
9070: 4e 4f 52 4d 41 4c 2c 20 74 68 61 74 20 6d 65 61  NORMAL, that mea
9080: 6e 73 20 74 6f 20 75 73 65 20 6e 6f 72 6d 61 6c  ns to use normal
9090: 20 66 73 79 6e 63 28 29 20 73 65 6d 61 6e 74 69   fsync() semanti
90a0: 63 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  cs..** If the lo
90b0: 77 65 72 20 66 6f 75 72 20 62 69 74 73 20 65 71  wer four bits eq
90c0: 75 61 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  ual SQLITE_SYNC_
90d0: 46 55 4c 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73  FULL, that means
90e0: 0a 2a 2a 20 74 6f 20 75 73 65 20 4d 61 63 20 4f  .** to use Mac O
90f0: 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 79  S X style fullsy
9100: 6e 63 20 69 6e 73 74 65 61 64 20 6f 66 20 66 73  nc instead of fs
9110: 79 6e 63 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ync()..*/.#defin
9120: 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  e SQLITE_SYNC_NO
9130: 52 4d 41 4c 20 20 20 20 20 20 20 20 30 78 30 30  RMAL        0x00
9140: 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  002.#define SQLI
9150: 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 20 20 20  TE_SYNC_FULL    
9160: 20 20 20 20 20 20 30 78 30 30 30 30 33 0a 23 64        0x00003.#d
9170: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e  efine SQLITE_SYN
9180: 43 5f 44 41 54 41 4f 4e 4c 59 20 20 20 20 20 20  C_DATAONLY      
9190: 30 78 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43  0x00010../*.** C
91a0: 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65  API3REF: OS Inte
91b0: 72 66 61 63 65 20 4f 70 65 6e 20 46 69 6c 65 20  rface Open File 
91c0: 48 61 6e 64 6c 65 20 7b 48 31 31 31 31 30 7d 20  Handle {H11110} 
91d0: 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41  <S20110>.**.** A
91e0: 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d  n [sqlite3_file]
91f0: 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e   object represen
9200: 74 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  ts an open file 
9210: 69 6e 20 74 68 65 20 4f 53 0a 2a 2a 20 69 6e 74  in the OS.** int
9220: 65 72 66 61 63 65 20 6c 61 79 65 72 2e 20 20 49  erface layer.  I
9230: 6e 64 69 76 69 64 75 61 6c 20 4f 53 20 69 6e 74  ndividual OS int
9240: 65 72 66 61 63 65 20 69 6d 70 6c 65 6d 65 6e 74  erface implement
9250: 61 74 69 6f 6e 73 20 77 69 6c 6c 0a 2a 2a 20 77  ations will.** w
9260: 61 6e 74 20 74 6f 20 73 75 62 63 6c 61 73 73 20  ant to subclass 
9270: 74 68 69 73 20 6f 62 6a 65 63 74 20 62 79 20 61  this object by a
9280: 70 70 65 6e 64 69 6e 67 20 61 64 64 69 74 69 6f  ppending additio
9290: 6e 61 6c 20 66 69 65 6c 64 73 0a 2a 2a 20 66 6f  nal fields.** fo
92a0: 72 20 74 68 65 69 72 20 6f 77 6e 20 75 73 65 2e  r their own use.
92b0: 20 20 54 68 65 20 70 4d 65 74 68 6f 64 73 20 65    The pMethods e
92c0: 6e 74 72 79 20 69 73 20 61 20 70 6f 69 6e 74 65  ntry is a pointe
92d0: 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69  r to an.** [sqli
92e0: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20  te3_io_methods] 
92f0: 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 66 69  object that defi
9300: 6e 65 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20  nes methods for 
9310: 70 65 72 66 6f 72 6d 69 6e 67 0a 2a 2a 20 49 2f  performing.** I/
9320: 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20  O operations on 
9330: 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a  the open file..*
9340: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
9350: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 71   sqlite3_file sq
9360: 6c 69 74 65 33 5f 66 69 6c 65 3b 0a 73 74 72 75  lite3_file;.stru
9370: 63 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ct sqlite3_file 
9380: 7b 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74  {.  const struct
9390: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
93a0: 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20  ods *pMethods;  
93b0: 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 61  /* Methods for a
93c0: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d  n open file */.}
93d0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
93e0: 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20  F: OS Interface 
93f0: 46 69 6c 65 20 56 69 72 74 75 61 6c 20 4d 65 74  File Virtual Met
9400: 68 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 31 31  hods Object {H11
9410: 31 32 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a  120} <S20110>.**
9420: 0a 2a 2a 20 45 76 65 72 79 20 66 69 6c 65 20 6f  .** Every file o
9430: 70 65 6e 65 64 20 62 79 20 74 68 65 20 5b 73 71  pened by the [sq
9440: 6c 69 74 65 33 5f 76 66 73 5d 20 78 4f 70 65 6e  lite3_vfs] xOpen
9450: 20 6d 65 74 68 6f 64 20 70 6f 70 75 6c 61 74 65   method populate
9460: 73 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  s an.** [sqlite3
9470: 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 28 6f  _file] object (o
9480: 72 2c 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 6c 79  r, more commonly
9490: 2c 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20  , a subclass of 
94a0: 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  the.** [sqlite3_
94b0: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 29 20 77 69  file] object) wi
94c0: 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  th a pointer to 
94d0: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
94e0: 68 69 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54  his object..** T
94f0: 68 69 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e  his object defin
9500: 65 73 20 74 68 65 20 6d 65 74 68 6f 64 73 20 75  es the methods u
9510: 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 76  sed to perform v
9520: 61 72 69 6f 75 73 20 6f 70 65 72 61 74 69 6f 6e  arious operation
9530: 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65  s.** against the
9540: 20 6f 70 65 6e 20 66 69 6c 65 20 72 65 70 72 65   open file repre
9550: 73 65 6e 74 65 64 20 62 79 20 74 68 65 20 5b 73  sented by the [s
9560: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a  qlite3_file] obj
9570: 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ect..**.** The f
9580: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f  lags argument to
9590: 20 78 53 79 6e 63 20 6d 61 79 20 62 65 20 6f 6e   xSync may be on
95a0: 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 53 59 4e  e of [SQLITE_SYN
95b0: 43 5f 4e 4f 52 4d 41 4c 5d 20 6f 72 0a 2a 2a 20  C_NORMAL] or.** 
95c0: 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c  [SQLITE_SYNC_FUL
95d0: 4c 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 63  L].  The first c
95e0: 68 6f 69 63 65 20 69 73 20 74 68 65 20 6e 6f 72  hoice is the nor
95f0: 6d 61 6c 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20  mal fsync()..** 
9600: 54 68 65 20 73 65 63 6f 6e 64 20 63 68 6f 69 63  The second choic
9610: 65 20 69 73 20 61 20 4d 61 63 20 4f 53 20 58 20  e is a Mac OS X 
9620: 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 2e 20  style fullsync. 
9630: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 53 59 4e   The [SQLITE_SYN
9640: 43 5f 44 41 54 41 4f 4e 4c 59 5d 0a 2a 2a 20 66  C_DATAONLY].** f
9650: 6c 61 67 20 6d 61 79 20 62 65 20 4f 52 65 64 20  lag may be ORed 
9660: 69 6e 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  in to indicate t
9670: 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 64 61 74  hat only the dat
9680: 61 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 2a 2a  a of the file.**
9690: 20 61 6e 64 20 6e 6f 74 20 69 74 73 20 69 6e 6f   and not its ino
96a0: 64 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73  de needs to be s
96b0: 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ynced..**.** The
96c0: 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20   integer values 
96d0: 74 6f 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78  to xLock() and x
96e0: 55 6e 6c 6f 63 6b 28 29 20 61 72 65 20 6f 6e 65  Unlock() are one
96f0: 20 6f 66 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c   of.** <ul>.** <
9700: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b  li> [SQLITE_LOCK
9710: 5f 4e 4f 4e 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20  _NONE],.** <li> 
9720: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41  [SQLITE_LOCK_SHA
9730: 52 45 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  RED],.** <li> [S
9740: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52  QLITE_LOCK_RESER
9750: 56 45 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  VED],.** <li> [S
9760: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49  QLITE_LOCK_PENDI
9770: 4e 47 5d 2c 20 6f 72 0a 2a 2a 20 3c 6c 69 3e 20  NG], or.** <li> 
9780: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43  [SQLITE_LOCK_EXC
9790: 4c 55 53 49 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c  LUSIVE]..** </ul
97a0: 3e 0a 2a 2a 20 78 4c 6f 63 6b 28 29 20 69 6e 63  >.** xLock() inc
97b0: 72 65 61 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e  reases the lock.
97c0: 20 78 55 6e 6c 6f 63 6b 28 29 20 64 65 63 72 65   xUnlock() decre
97d0: 61 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a  ases the lock..*
97e0: 2a 20 54 68 65 20 78 43 68 65 63 6b 52 65 73 65  * The xCheckRese
97f0: 72 76 65 64 4c 6f 63 6b 28 29 20 6d 65 74 68 6f  rvedLock() metho
9800: 64 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72  d checks whether
9810: 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f   any database co
9820: 6e 6e 65 63 74 69 6f 6e 2c 0a 2a 2a 20 65 69 74  nnection,.** eit
9830: 68 65 72 20 69 6e 20 74 68 69 73 20 70 72 6f 63  her in this proc
9840: 65 73 73 20 6f 72 20 69 6e 20 73 6f 6d 65 20 6f  ess or in some o
9850: 74 68 65 72 20 70 72 6f 63 65 73 73 2c 20 69 73  ther process, is
9860: 20 68 6f 6c 64 69 6e 67 20 61 20 52 45 53 45 52   holding a RESER
9870: 56 45 44 2c 0a 2a 2a 20 50 45 4e 44 49 4e 47 2c  VED,.** PENDING,
9880: 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   or EXCLUSIVE lo
9890: 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20  ck on the file. 
98a0: 20 49 74 20 72 65 74 75 72 6e 73 20 74 72 75 65   It returns true
98b0: 0a 2a 2a 20 69 66 20 73 75 63 68 20 61 20 6c 6f  .** if such a lo
98c0: 63 6b 20 65 78 69 73 74 73 20 61 6e 64 20 66 61  ck exists and fa
98d0: 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a  lse otherwise..*
98e0: 2a 0a 2a 2a 20 54 68 65 20 78 46 69 6c 65 43 6f  *.** The xFileCo
98f0: 6e 74 72 6f 6c 28 29 20 6d 65 74 68 6f 64 20 69  ntrol() method i
9900: 73 20 61 20 67 65 6e 65 72 69 63 20 69 6e 74 65  s a generic inte
9910: 72 66 61 63 65 20 74 68 61 74 20 61 6c 6c 6f 77  rface that allow
9920: 73 20 63 75 73 74 6f 6d 0a 2a 2a 20 56 46 53 20  s custom.** VFS 
9930: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
9940: 74 6f 20 64 69 72 65 63 74 6c 79 20 63 6f 6e 74  to directly cont
9950: 72 6f 6c 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65  rol an open file
9960: 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73   using the.** [s
9970: 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
9980: 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65  rol()] interface
9990: 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 22 6f  .  The second "o
99a0: 70 22 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  p" argument is a
99b0: 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 70 63  n.** integer opc
99c0: 6f 64 65 2e 20 20 54 68 65 20 74 68 69 72 64 20  ode.  The third 
99d0: 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 67 65  argument is a ge
99e0: 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 20 69 6e  neric pointer in
99f0: 74 65 6e 64 65 64 20 74 6f 0a 2a 2a 20 70 6f 69  tended to.** poi
9a00: 6e 74 20 74 6f 20 61 20 73 74 72 75 63 74 75 72  nt to a structur
9a10: 65 20 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 61  e that may conta
9a20: 69 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20  in arguments or 
9a30: 73 70 61 63 65 20 69 6e 20 77 68 69 63 68 20 74  space in which t
9a40: 6f 0a 2a 2a 20 77 72 69 74 65 20 72 65 74 75 72  o.** write retur
9a50: 6e 20 76 61 6c 75 65 73 2e 20 20 50 6f 74 65 6e  n values.  Poten
9a60: 74 69 61 6c 20 75 73 65 73 20 66 6f 72 20 78 46  tial uses for xF
9a70: 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 69 67  ileControl() mig
9a80: 68 74 20 62 65 0a 2a 2a 20 66 75 6e 63 74 69 6f  ht be.** functio
9a90: 6e 73 20 74 6f 20 65 6e 61 62 6c 65 20 62 6c 6f  ns to enable blo
9aa0: 63 6b 69 6e 67 20 6c 6f 63 6b 73 20 77 69 74 68  cking locks with
9ab0: 20 74 69 6d 65 6f 75 74 73 2c 20 74 6f 20 63 68   timeouts, to ch
9ac0: 61 6e 67 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b  ange the.** lock
9ad0: 69 6e 67 20 73 74 72 61 74 65 67 79 20 28 66 6f  ing strategy (fo
9ae0: 72 20 65 78 61 6d 70 6c 65 20 74 6f 20 75 73 65  r example to use
9af0: 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 29   dot-file locks)
9b00: 2c 20 74 6f 20 69 6e 71 75 69 72 65 0a 2a 2a 20  , to inquire.** 
9b10: 61 62 6f 75 74 20 74 68 65 20 73 74 61 74 75 73  about the status
9b20: 20 6f 66 20 61 20 6c 6f 63 6b 2c 20 6f 72 20 74   of a lock, or t
9b30: 6f 20 62 72 65 61 6b 20 73 74 61 6c 65 20 6c 6f  o break stale lo
9b40: 63 6b 73 2e 20 20 54 68 65 20 53 51 4c 69 74 65  cks.  The SQLite
9b50: 0a 2a 2a 20 63 6f 72 65 20 72 65 73 65 72 76 65  .** core reserve
9b60: 73 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 6c 65  s all opcodes le
9b70: 73 73 20 74 68 61 6e 20 31 30 30 20 66 6f 72 20  ss than 100 for 
9b80: 69 74 73 20 6f 77 6e 20 75 73 65 2e 0a 2a 2a 20  its own use..** 
9b90: 41 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  A [SQLITE_FCNTL_
9ba0: 4c 4f 43 4b 53 54 41 54 45 20 7c 20 6c 69 73 74  LOCKSTATE | list
9bb0: 20 6f 66 20 6f 70 63 6f 64 65 73 5d 20 6c 65 73   of opcodes] les
9bc0: 73 20 74 68 61 6e 20 31 30 30 20 69 73 20 61 76  s than 100 is av
9bd0: 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 41 70 70 6c  ailable..** Appl
9be0: 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 64 65  ications that de
9bf0: 66 69 6e 65 20 61 20 63 75 73 74 6f 6d 20 78 46  fine a custom xF
9c00: 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f  ileControl metho
9c10: 64 20 73 68 6f 75 6c 64 20 75 73 65 20 6f 70 63  d should use opc
9c20: 6f 64 65 73 0a 2a 2a 20 67 72 65 61 74 65 72 20  odes.** greater 
9c30: 74 68 61 6e 20 31 30 30 20 74 6f 20 61 76 6f 69  than 100 to avoi
9c40: 64 20 63 6f 6e 66 6c 69 63 74 73 2e 0a 2a 2a 0a  d conflicts..**.
9c50: 2a 2a 20 54 68 65 20 78 53 65 63 74 6f 72 53 69  ** The xSectorSi
9c60: 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 65 74 75  ze() method retu
9c70: 72 6e 73 20 74 68 65 20 73 65 63 74 6f 72 20 73  rns the sector s
9c80: 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65  ize of the.** de
9c90: 76 69 63 65 20 74 68 61 74 20 75 6e 64 65 72 6c  vice that underl
9ca0: 69 65 73 20 74 68 65 20 66 69 6c 65 2e 20 20 54  ies the file.  T
9cb0: 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69  he sector size i
9cc0: 73 20 74 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d  s the.** minimum
9cd0: 20 77 72 69 74 65 20 74 68 61 74 20 63 61 6e 20   write that can 
9ce0: 62 65 20 70 65 72 66 6f 72 6d 65 64 20 77 69 74  be performed wit
9cf0: 68 6f 75 74 20 64 69 73 74 75 72 62 69 6e 67 0a  hout disturbing.
9d00: 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20 69  ** other bytes i
9d10: 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65  n the file.  The
9d20: 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65   xDeviceCharacte
9d30: 72 69 73 74 69 63 73 28 29 0a 2a 2a 20 6d 65 74  ristics().** met
9d40: 68 6f 64 20 72 65 74 75 72 6e 73 20 61 20 62 69  hod returns a bi
9d50: 74 20 76 65 63 74 6f 72 20 64 65 73 63 72 69 62  t vector describ
9d60: 69 6e 67 20 62 65 68 61 76 69 6f 72 73 20 6f 66  ing behaviors of
9d70: 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69   the.** underlyi
9d80: 6e 67 20 64 65 76 69 63 65 3a 0a 2a 2a 0a 2a 2a  ng device:.**.**
9d90: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53   <ul>.** <li> [S
9da0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
9db0: 49 43 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  IC].** <li> [SQL
9dc0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
9dd0: 35 31 32 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  512].** <li> [SQ
9de0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9df0: 43 31 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  C1K].** <li> [SQ
9e00: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9e10: 43 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  C2K].** <li> [SQ
9e20: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9e30: 43 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  C4K].** <li> [SQ
9e40: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9e50: 43 38 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  C8K].** <li> [SQ
9e60: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9e70: 43 31 36 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  C16K].** <li> [S
9e80: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
9e90: 49 43 33 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b  IC32K].** <li> [
9ea0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
9eb0: 4d 49 43 36 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20  MIC64K].** <li> 
9ec0: 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41  [SQLITE_IOCAP_SA
9ed0: 46 45 5f 41 50 50 45 4e 44 5d 0a 2a 2a 20 3c 6c  FE_APPEND].** <l
9ee0: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
9ef0: 5f 53 45 51 55 45 4e 54 49 41 4c 5d 0a 2a 2a 20  _SEQUENTIAL].** 
9f00: 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  </ul>.**.** The 
9f10: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
9f20: 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61  MIC property mea
9f30: 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74  ns that all writ
9f40: 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a  es of.** any siz
9f50: 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54  e are atomic.  T
9f60: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
9f70: 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73  ATOMICnnn values
9f80: 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72  .** mean that wr
9f90: 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74  ites of blocks t
9fa0: 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65  hat are nnn byte
9fb0: 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a  s in size and.**
9fc0: 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20   are aligned to 
9fd0: 61 6e 20 61 64 64 72 65 73 73 20 77 68 69 63 68  an address which
9fe0: 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d   is an integer m
9ff0: 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e  ultiple of.** nn
a000: 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54  n are atomic.  T
a010: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
a020: 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75  SAFE_APPEND valu
a030: 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20  e means.** that 
a040: 77 68 65 6e 20 64 61 74 61 20 69 73 20 61 70 70  when data is app
a050: 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c  ended to a file,
a060: 20 74 68 65 20 64 61 74 61 20 69 73 20 61 70 70   the data is app
a070: 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74  ended.** first t
a080: 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20  hen the size of 
a090: 74 68 65 20 66 69 6c 65 20 69 73 20 65 78 74 65  the file is exte
a0a0: 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20  nded, never the 
a0b0: 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f  other.** way aro
a0c0: 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45  und.  The SQLITE
a0d0: 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41  _IOCAP_SEQUENTIA
a0e0: 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73  L property means
a0f0: 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61   that.** informa
a100: 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20  tion is written 
a110: 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73  to disk in the s
a120: 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c  ame order as cal
a130: 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28  ls.** to xWrite(
a140: 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 78 52 65 61  )..**.** If xRea
a150: 64 28 29 20 72 65 74 75 72 6e 73 20 53 51 4c 49  d() returns SQLI
a160: 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52  TE_IOERR_SHORT_R
a170: 45 41 44 20 69 74 20 6d 75 73 74 20 61 6c 73 6f  EAD it must also
a180: 20 66 69 6c 6c 0a 2a 2a 20 69 6e 20 74 68 65 20   fill.** in the 
a190: 75 6e 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 20  unread portions 
a1a0: 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 77 69  of the buffer wi
a1b0: 74 68 20 7a 65 72 6f 73 2e 20 20 41 20 56 46 53  th zeros.  A VFS
a1c0: 20 74 68 61 74 0a 2a 2a 20 66 61 69 6c 73 20 74   that.** fails t
a1d0: 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72  o zero-fill shor
a1e0: 74 20 72 65 61 64 73 20 6d 69 67 68 74 20 73 65  t reads might se
a1f0: 65 6d 20 74 6f 20 77 6f 72 6b 2e 20 20 48 6f 77  em to work.  How
a200: 65 76 65 72 2c 0a 2a 2a 20 66 61 69 6c 75 72 65  ever,.** failure
a210: 20 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68   to zero-fill sh
a220: 6f 72 74 20 72 65 61 64 73 20 77 69 6c 6c 20 65  ort reads will e
a230: 76 65 6e 74 75 61 6c 6c 79 20 6c 65 61 64 20 74  ventually lead t
a240: 6f 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  o.** database co
a250: 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70  rruption..*/.typ
a260: 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
a270: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 73  te3_io_methods s
a280: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
a290: 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  s;.struct sqlite
a2a0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 7b 0a 20  3_io_methods {. 
a2b0: 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20   int iVersion;. 
a2c0: 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73   int (*xClose)(s
a2d0: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20  qlite3_file*);. 
a2e0: 20 69 6e 74 20 28 2a 78 52 65 61 64 29 28 73 71   int (*xRead)(sq
a2f0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f 69  lite3_file*, voi
a300: 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73 71  d*, int iAmt, sq
a310: 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 73  lite3_int64 iOfs
a320: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 57 72 69  t);.  int (*xWri
a330: 74 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  te)(sqlite3_file
a340: 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
a350: 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65  int iAmt, sqlite
a360: 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a  3_int64 iOfst);.
a370: 20 20 69 6e 74 20 28 2a 78 54 72 75 6e 63 61 74    int (*xTruncat
a380: 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  e)(sqlite3_file*
a390: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
a3a0: 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78  size);.  int (*x
a3b0: 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 66 69  Sync)(sqlite3_fi
a3c0: 6c 65 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b  le*, int flags);
a3d0: 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 65 53 69  .  int (*xFileSi
a3e0: 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ze)(sqlite3_file
a3f0: 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
a400: 20 2a 70 53 69 7a 65 29 3b 0a 20 20 69 6e 74 20   *pSize);.  int 
a410: 28 2a 78 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33  (*xLock)(sqlite3
a420: 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20  _file*, int);.  
a430: 69 6e 74 20 28 2a 78 55 6e 6c 6f 63 6b 29 28 73  int (*xUnlock)(s
a440: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
a450: 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 68 65  t);.  int (*xChe
a460: 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 28  ckReservedLock)(
a470: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
a480: 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 20 20  nt *pResOut);.  
a490: 69 6e 74 20 28 2a 78 46 69 6c 65 43 6f 6e 74 72  int (*xFileContr
a4a0: 6f 6c 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ol)(sqlite3_file
a4b0: 2a 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20  *, int op, void 
a4c0: 2a 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 28 2a  *pArg);.  int (*
a4d0: 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c  xSectorSize)(sql
a4e0: 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69  ite3_file*);.  i
a4f0: 6e 74 20 28 2a 78 44 65 76 69 63 65 43 68 61 72  nt (*xDeviceChar
a500: 61 63 74 65 72 69 73 74 69 63 73 29 28 73 71 6c  acteristics)(sql
a510: 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 2f  ite3_file*);.  /
a520: 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65 74  * Additional met
a530: 68 6f 64 73 20 6d 61 79 20 62 65 20 61 64 64 65  hods may be adde
a540: 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  d in future rele
a550: 61 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ases */.};../*.*
a560: 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 6e  * CAPI3REF: Stan
a570: 64 61 72 64 20 46 69 6c 65 20 43 6f 6e 74 72 6f  dard File Contro
a580: 6c 20 4f 70 63 6f 64 65 73 20 7b 48 31 31 33 31  l Opcodes {H1131
a590: 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a  0} <S30800>.**.*
a5a0: 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 20  * These integer 
a5b0: 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 6f 70  constants are op
a5c0: 63 6f 64 65 73 20 66 6f 72 20 74 68 65 20 78 46  codes for the xF
a5d0: 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f  ileControl metho
a5e0: 64 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c  d.** of the [sql
a5f0: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d  ite3_io_methods]
a600: 20 6f 62 6a 65 63 74 20 61 6e 64 20 66 6f 72 20   object and for 
a610: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  the [sqlite3_fil
a620: 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20  e_control()].** 
a630: 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a  interface..**.**
a640: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e   The [SQLITE_FCN
a650: 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 20 6f 70  TL_LOCKSTATE] op
a660: 63 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f 72  code is used for
a670: 20 64 65 62 75 67 67 69 6e 67 2e 20 20 54 68 69   debugging.  Thi
a680: 73 0a 2a 2a 20 6f 70 63 6f 64 65 20 63 61 75 73  s.** opcode caus
a690: 65 73 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74  es the xFileCont
a6a0: 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 77 72  rol method to wr
a6b0: 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ite the current 
a6c0: 73 74 61 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20  state of.** the 
a6d0: 6c 6f 63 6b 20 28 6f 6e 65 20 6f 66 20 5b 53 51  lock (one of [SQ
a6e0: 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c  LITE_LOCK_NONE],
a6f0: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48   [SQLITE_LOCK_SH
a700: 41 52 45 44 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  ARED],.** [SQLIT
a710: 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d  E_LOCK_RESERVED]
a720: 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50  , [SQLITE_LOCK_P
a730: 45 4e 44 49 4e 47 5d 2c 20 6f 72 20 5b 53 51 4c  ENDING], or [SQL
a740: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49  ITE_LOCK_EXCLUSI
a750: 56 45 5d 29 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20  VE]).** into an 
a760: 69 6e 74 65 67 65 72 20 74 68 61 74 20 74 68 65  integer that the
a770: 20 70 41 72 67 20 61 72 67 75 6d 65 6e 74 20 70   pArg argument p
a780: 6f 69 6e 74 73 20 74 6f 2e 20 54 68 69 73 20 63  oints to. This c
a790: 61 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20  apability.** is 
a7a0: 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74  used during test
a7b0: 69 6e 67 20 61 6e 64 20 6f 6e 6c 79 20 6e 65 65  ing and only nee
a7c0: 64 73 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74  ds to be support
a7d0: 65 64 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 54  ed when SQLITE_T
a7e0: 45 53 54 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65  EST.** is define
a7f0: 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  d..*/.#define SQ
a800: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
a810: 54 41 54 45 20 20 20 20 20 20 20 20 31 0a 23 64  TATE        1.#d
a820: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 47 45 54  efine SQLITE_GET
a830: 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20  _LOCKPROXYFILE  
a840: 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51      2.#define SQ
a850: 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_SET_LOCKPRO
a860: 58 59 46 49 4c 45 20 20 20 20 20 20 33 0a 23 64  XYFILE      3.#d
a870: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 41 53  efine SQLITE_LAS
a880: 54 5f 45 52 52 4e 4f 20 20 20 20 20 20 20 20 20  T_ERRNO         
a890: 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50      4../*.** CAP
a8a0: 49 33 52 45 46 3a 20 4d 75 74 65 78 20 48 61 6e  I3REF: Mutex Han
a8b0: 64 6c 65 20 7b 48 31 37 31 31 30 7d 20 3c 53 32  dle {H17110} <S2
a8c0: 30 31 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0130>.**.** The 
a8d0: 6d 75 74 65 78 20 6d 6f 64 75 6c 65 20 77 69 74  mutex module wit
a8e0: 68 69 6e 20 53 51 4c 69 74 65 20 64 65 66 69 6e  hin SQLite defin
a8f0: 65 73 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  es [sqlite3_mute
a900: 78 5d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 61  x] to be an.** a
a910: 62 73 74 72 61 63 74 20 74 79 70 65 20 66 6f 72  bstract type for
a920: 20 61 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 2e   a mutex object.
a930: 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72    The SQLite cor
a940: 65 20 6e 65 76 65 72 20 6c 6f 6f 6b 73 0a 2a 2a  e never looks.**
a950: 20 61 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   at the internal
a960: 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
a970: 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 6d  of an [sqlite3_m
a980: 75 74 65 78 5d 2e 20 20 49 74 20 6f 6e 6c 79 0a  utex].  It only.
a990: 2a 2a 20 64 65 61 6c 73 20 77 69 74 68 20 70 6f  ** deals with po
a9a0: 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 5b 73  inters to the [s
a9b0: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 6f 62  qlite3_mutex] ob
a9c0: 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65  ject..**.** Mute
a9d0: 78 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20  xes are created 
a9e0: 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d  using [sqlite3_m
a9f0: 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 2e 0a 2a  utex_alloc()]..*
aa00: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
aa10: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73   sqlite3_mutex s
aa20: 71 6c 69 74 65 33 5f 6d 75 74 65 78 3b 0a 0a 2f  qlite3_mutex;../
aa30: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
aa40: 53 20 49 6e 74 65 72 66 61 63 65 20 4f 62 6a 65  S Interface Obje
aa50: 63 74 20 7b 48 31 31 31 34 30 7d 20 3c 53 32 30  ct {H11140} <S20
aa60: 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e  100>.**.** An in
aa70: 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71  stance of the sq
aa80: 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74  lite3_vfs object
aa90: 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
aaa0: 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 0a 2a  erface between.*
aab0: 2a 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72  * the SQLite cor
aac0: 65 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c  e and the underl
aad0: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
aae0: 79 73 74 65 6d 2e 20 20 54 68 65 20 22 76 66 73  ystem.  The "vfs
aaf0: 22 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 6d 65  ".** in the name
ab00: 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 20 73   of the object s
ab10: 74 61 6e 64 73 20 66 6f 72 20 22 76 69 72 74 75  tands for "virtu
ab20: 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 22 2e  al file system".
ab30: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .**.** The value
ab40: 20 6f 66 20 74 68 65 20 69 56 65 72 73 69 6f 6e   of the iVersion
ab50: 20 66 69 65 6c 64 20 69 73 20 69 6e 69 74 69 61   field is initia
ab60: 6c 6c 79 20 31 20 62 75 74 20 6d 61 79 20 62 65  lly 1 but may be
ab70: 20 6c 61 72 67 65 72 20 69 6e 0a 2a 2a 20 66 75   larger in.** fu
ab80: 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  ture versions of
ab90: 20 53 51 4c 69 74 65 2e 20 20 41 64 64 69 74 69   SQLite.  Additi
aba0: 6f 6e 61 6c 20 66 69 65 6c 64 73 20 6d 61 79 20  onal fields may 
abb0: 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  be appended to t
abc0: 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 77 68  his.** object wh
abd0: 65 6e 20 74 68 65 20 69 56 65 72 73 69 6f 6e 20  en the iVersion 
abe0: 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73  value is increas
abf0: 65 64 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74  ed.  Note that t
ac00: 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  he structure.** 
ac10: 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  of the sqlite3_v
ac20: 66 73 20 6f 62 6a 65 63 74 20 63 68 61 6e 67 65  fs object change
ac30: 73 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63  s in the transac
ac40: 74 69 6f 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20  tion between.** 
ac50: 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33  SQLite version 3
ac60: 2e 35 2e 39 20 61 6e 64 20 33 2e 36 2e 30 20 61  .5.9 and 3.6.0 a
ac70: 6e 64 20 79 65 74 20 74 68 65 20 69 56 65 72 73  nd yet the iVers
ac80: 69 6f 6e 20 66 69 65 6c 64 20 77 61 73 20 6e 6f  ion field was no
ac90: 74 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 2e 0a 2a  t.** modified..*
aca0: 2a 0a 2a 2a 20 54 68 65 20 73 7a 4f 73 46 69 6c  *.** The szOsFil
acb0: 65 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 73  e field is the s
acc0: 69 7a 65 20 6f 66 20 74 68 65 20 73 75 62 63 6c  ize of the subcl
acd0: 61 73 73 65 64 20 5b 73 71 6c 69 74 65 33 5f 66  assed [sqlite3_f
ace0: 69 6c 65 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72  ile].** structur
acf0: 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20 56  e used by this V
ad00: 46 53 2e 20 20 6d 78 50 61 74 68 6e 61 6d 65 20  FS.  mxPathname 
ad10: 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6c  is the maximum l
ad20: 65 6e 67 74 68 20 6f 66 0a 2a 2a 20 61 20 70 61  ength of.** a pa
ad30: 74 68 6e 61 6d 65 20 69 6e 20 74 68 69 73 20 56  thname in this V
ad40: 46 53 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74  FS..**.** Regist
ad50: 65 72 65 64 20 73 71 6c 69 74 65 33 5f 76 66 73  ered sqlite3_vfs
ad60: 20 6f 62 6a 65 63 74 73 20 61 72 65 20 6b 65 70   objects are kep
ad70: 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  t on a linked li
ad80: 73 74 20 66 6f 72 6d 65 64 20 62 79 0a 2a 2a 20  st formed by.** 
ad90: 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65  the pNext pointe
ada0: 72 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33  r.  The [sqlite3
adb0: 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 5d  _vfs_register()]
adc0: 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  .** and [sqlite3
add0: 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28  _vfs_unregister(
ade0: 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 6d 61  )] interfaces ma
adf0: 6e 61 67 65 20 74 68 69 73 20 6c 69 73 74 0a 2a  nage this list.*
ae00: 2a 20 69 6e 20 61 20 74 68 72 65 61 64 2d 73 61  * in a thread-sa
ae10: 66 65 20 77 61 79 2e 20 20 54 68 65 20 5b 73 71  fe way.  The [sq
ae20: 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29  lite3_vfs_find()
ae30: 5d 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 73  ] interface.** s
ae40: 65 61 72 63 68 65 73 20 74 68 65 20 6c 69 73 74  earches the list
ae50: 2e 20 20 4e 65 69 74 68 65 72 20 74 68 65 20 61  .  Neither the a
ae60: 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20  pplication code 
ae70: 6e 6f 72 20 74 68 65 20 56 46 53 0a 2a 2a 20 69  nor the VFS.** i
ae80: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68  mplementation sh
ae90: 6f 75 6c 64 20 75 73 65 20 74 68 65 20 70 4e 65  ould use the pNe
aea0: 78 74 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  xt pointer..**.*
aeb0: 2a 20 54 68 65 20 70 4e 65 78 74 20 66 69 65 6c  * The pNext fiel
aec0: 64 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 66 69  d is the only fi
aed0: 65 6c 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74  eld in the sqlit
aee0: 65 33 5f 76 66 73 0a 2a 2a 20 73 74 72 75 63 74  e3_vfs.** struct
aef0: 75 72 65 20 74 68 61 74 20 53 51 4c 69 74 65 20  ure that SQLite 
af00: 77 69 6c 6c 20 65 76 65 72 20 6d 6f 64 69 66 79  will ever modify
af10: 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  .  SQLite will o
af20: 6e 6c 79 20 61 63 63 65 73 73 0a 2a 2a 20 6f 72  nly access.** or
af30: 20 6d 6f 64 69 66 79 20 74 68 69 73 20 66 69 65   modify this fie
af40: 6c 64 20 77 68 69 6c 65 20 68 6f 6c 64 69 6e 67  ld while holding
af50: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74   a particular st
af60: 61 74 69 63 20 6d 75 74 65 78 2e 0a 2a 2a 20 54  atic mutex..** T
af70: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73  he application s
af80: 68 6f 75 6c 64 20 6e 65 76 65 72 20 6d 6f 64 69  hould never modi
af90: 66 79 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68  fy anything with
afa0: 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  in the sqlite3_v
afb0: 66 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e 63  fs.** object onc
afc0: 65 20 74 68 65 20 6f 62 6a 65 63 74 20 68 61 73  e the object has
afd0: 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64   been registered
afe0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 4e 61 6d  ..**.** The zNam
aff0: 65 20 66 69 65 6c 64 20 68 6f 6c 64 73 20 74 68  e field holds th
b000: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 56 46  e name of the VF
b010: 53 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 6e  S module.  The n
b020: 61 6d 65 20 6d 75 73 74 0a 2a 2a 20 62 65 20 75  ame must.** be u
b030: 6e 69 71 75 65 20 61 63 72 6f 73 73 20 61 6c 6c  nique across all
b040: 20 56 46 53 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a   VFS modules..**
b050: 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
b060: 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74  guarantee that t
b070: 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72  he zFilename par
b080: 61 6d 65 74 65 72 20 74 6f 20 78 4f 70 65 6e 0a  ameter to xOpen.
b090: 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 4e  ** is either a N
b0a0: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 6f 72 20 73  ULL pointer or s
b0b0: 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 0a 2a  tring obtained.*
b0c0: 2a 20 66 72 6f 6d 20 78 46 75 6c 6c 50 61 74 68  * from xFullPath
b0d0: 6e 61 6d 65 28 29 2e 20 20 53 51 4c 69 74 65 20  name().  SQLite 
b0e0: 66 75 72 74 68 65 72 20 67 75 61 72 61 6e 74 65  further guarante
b0f0: 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 73  es that.** the s
b100: 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 76 61  tring will be va
b110: 6c 69 64 20 61 6e 64 20 75 6e 63 68 61 6e 67 65  lid and unchange
b120: 64 20 75 6e 74 69 6c 20 78 43 6c 6f 73 65 28 29  d until xClose()
b130: 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 20 42   is.** called. B
b140: 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72  ecause of the pr
b150: 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 73 65 2c  evious sentense,
b160: 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33  .** the [sqlite3
b170: 5f 66 69 6c 65 5d 20 63 61 6e 20 73 61 66 65 6c  _file] can safel
b180: 79 20 73 74 6f 72 65 20 61 20 70 6f 69 6e 74 65  y store a pointe
b190: 72 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69 6c 65  r to the.** file
b1a0: 6e 61 6d 65 20 69 66 20 69 74 20 6e 65 65 64 73  name if it needs
b1b0: 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 65   to remember the
b1c0: 20 66 69 6c 65 6e 61 6d 65 20 66 6f 72 20 73 6f   filename for so
b1d0: 6d 65 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20 49 66  me reason..** If
b1e0: 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70   the zFilename p
b1f0: 61 72 61 6d 65 74 65 72 20 69 73 20 78 4f 70 65  arameter is xOpe
b200: 6e 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  n is a NULL poin
b210: 74 65 72 20 74 68 65 6e 20 78 4f 70 65 6e 0a 2a  ter then xOpen.*
b220: 2a 20 6d 75 73 74 20 69 6e 76 69 74 65 20 69 74  * must invite it
b230: 73 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72 79 20  s own temporary 
b240: 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 66 69 6c  name for the fil
b250: 65 2e 20 20 57 68 65 6e 65 76 65 72 20 74 68 65  e.  Whenever the
b260: 20 0a 2a 2a 20 78 46 69 6c 65 6e 61 6d 65 20 70   .** xFilename p
b270: 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c  arameter is NULL
b280: 20 69 74 20 77 69 6c 6c 20 61 6c 73 6f 20 62 65   it will also be
b290: 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 74   the case that t
b2a0: 68 65 0a 2a 2a 20 66 6c 61 67 73 20 70 61 72 61  he.** flags para
b2b0: 6d 65 74 65 72 20 77 69 6c 6c 20 69 6e 63 6c 75  meter will inclu
b2c0: 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  de [SQLITE_OPEN_
b2d0: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 2e 0a  DELETEONCLOSE]..
b2e0: 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20  **.** The flags 
b2f0: 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65  argument to xOpe
b300: 6e 28 29 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c  n() includes all
b310: 20 62 69 74 73 20 73 65 74 20 69 6e 0a 2a 2a 20   bits set in.** 
b320: 74 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65  the flags argume
b330: 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f  nt to [sqlite3_o
b340: 70 65 6e 5f 76 32 28 29 5d 2e 20 20 4f 72 20 69  pen_v2()].  Or i
b350: 66 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  f [sqlite3_open(
b360: 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65  )].** or [sqlite
b370: 33 5f 6f 70 65 6e 31 36 28 29 5d 20 69 73 20 75  3_open16()] is u
b380: 73 65 64 2c 20 74 68 65 6e 20 66 6c 61 67 73 20  sed, then flags 
b390: 69 6e 63 6c 75 64 65 73 20 61 74 20 6c 65 61 73  includes at leas
b3a0: 74 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45  t.** [SQLITE_OPE
b3b0: 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b  N_READWRITE] | [
b3c0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
b3d0: 54 45 5d 2e 20 0a 2a 2a 20 49 66 20 78 4f 70 65  TE]. .** If xOpe
b3e0: 6e 28 29 20 6f 70 65 6e 73 20 61 20 66 69 6c 65  n() opens a file
b3f0: 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65 6e 20   read-only then 
b400: 69 74 20 73 65 74 73 20 2a 70 4f 75 74 46 6c 61  it sets *pOutFla
b410: 67 73 20 74 6f 0a 2a 2a 20 69 6e 63 6c 75 64 65  gs to.** include
b420: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   [SQLITE_OPEN_RE
b430: 41 44 4f 4e 4c 59 5d 2e 20 20 4f 74 68 65 72 20  ADONLY].  Other 
b440: 62 69 74 73 20 69 6e 20 2a 70 4f 75 74 46 6c 61  bits in *pOutFla
b450: 67 73 20 6d 61 79 20 62 65 20 73 65 74 2e 0a 2a  gs may be set..*
b460: 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c  *.** SQLite will
b470: 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66   also add one of
b480: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
b490: 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f 70 65  lags to the xOpe
b4a0: 6e 28 29 0a 2a 2a 20 63 61 6c 6c 2c 20 64 65 70  n().** call, dep
b4b0: 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 62  ending on the ob
b4c0: 6a 65 63 74 20 62 65 69 6e 67 20 6f 70 65 6e 65  ject being opene
b4d0: 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  d:.**.** <ul>.**
b4e0: 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f   <li>  [SQLITE_O
b4f0: 50 45 4e 5f 4d 41 49 4e 5f 44 42 5d 0a 2a 2a 20  PEN_MAIN_DB].** 
b500: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
b510: 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 5d  EN_MAIN_JOURNAL]
b520: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54  .** <li>  [SQLIT
b530: 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 5d 0a  E_OPEN_TEMP_DB].
b540: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45  ** <li>  [SQLITE
b550: 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e  _OPEN_TEMP_JOURN
b560: 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51  AL].** <li>  [SQ
b570: 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49  LITE_OPEN_TRANSI
b580: 45 4e 54 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20  ENT_DB].** <li> 
b590: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55   [SQLITE_OPEN_SU
b5a0: 42 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69  BJOURNAL].** <li
b5b0: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  >  [SQLITE_OPEN_
b5c0: 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 5d 0a  MASTER_JOURNAL].
b5d0: 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
b5e0: 68 65 20 66 69 6c 65 20 49 2f 4f 20 69 6d 70 6c  he file I/O impl
b5f0: 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 75  ementation can u
b600: 73 65 20 74 68 65 20 6f 62 6a 65 63 74 20 74 79  se the object ty
b610: 70 65 20 66 6c 61 67 73 20 74 6f 0a 2a 2a 20 63  pe flags to.** c
b620: 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 69 74  hange the way it
b630: 20 64 65 61 6c 73 20 77 69 74 68 20 66 69 6c 65   deals with file
b640: 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  s.  For example,
b650: 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a   an application.
b660: 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  ** that does not
b670: 20 63 61 72 65 20 61 62 6f 75 74 20 63 72 61 73   care about cras
b680: 68 20 72 65 63 6f 76 65 72 79 20 6f 72 20 72 6f  h recovery or ro
b690: 6c 6c 62 61 63 6b 20 6d 69 67 68 74 20 6d 61 6b  llback might mak
b6a0: 65 0a 2a 2a 20 74 68 65 20 6f 70 65 6e 20 6f 66  e.** the open of
b6b0: 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20   a journal file 
b6c0: 61 20 6e 6f 2d 6f 70 2e 20 20 57 72 69 74 65 73  a no-op.  Writes
b6d0: 20 74 6f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c   to this journal
b6e0: 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62   would.** also b
b6f0: 65 20 6e 6f 2d 6f 70 73 2c 20 61 6e 64 20 61 6e  e no-ops, and an
b700: 79 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61  y attempt to rea
b710: 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 6f  d the journal wo
b720: 75 6c 64 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  uld return.** SQ
b730: 4c 49 54 45 5f 49 4f 45 52 52 2e 20 20 4f 72 20  LITE_IOERR.  Or 
b740: 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
b750: 6f 6e 20 6d 69 67 68 74 20 72 65 63 6f 67 6e 69  on might recogni
b760: 7a 65 20 74 68 61 74 20 61 20 64 61 74 61 62 61  ze that a databa
b770: 73 65 0a 2a 2a 20 66 69 6c 65 20 77 69 6c 6c 20  se.** file will 
b780: 62 65 20 64 6f 69 6e 67 20 70 61 67 65 2d 61 6c  be doing page-al
b790: 69 67 6e 65 64 20 73 65 63 74 6f 72 20 72 65 61  igned sector rea
b7a0: 64 73 20 61 6e 64 20 77 72 69 74 65 73 20 69 6e  ds and writes in
b7b0: 20 61 20 72 61 6e 64 6f 6d 0a 2a 2a 20 6f 72 64   a random.** ord
b7c0: 65 72 20 61 6e 64 20 73 65 74 20 75 70 20 69 74  er and set up it
b7d0: 73 20 49 2f 4f 20 73 75 62 73 79 73 74 65 6d 20  s I/O subsystem 
b7e0: 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a  accordingly..**.
b7f0: 2a 2a 20 53 51 4c 69 74 65 20 6d 69 67 68 74 20  ** SQLite might 
b800: 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20  also add one of 
b810: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c  the following fl
b820: 61 67 73 20 74 6f 20 74 68 65 20 78 4f 70 65 6e  ags to the xOpen
b830: 20 6d 65 74 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c   method:.**.** <
b840: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  ul>.** <li> [SQL
b850: 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
b860: 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 3c 6c 69 3e 20  NCLOSE].** <li> 
b870: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43  [SQLITE_OPEN_EXC
b880: 4c 55 53 49 56 45 5d 0a 2a 2a 20 3c 2f 75 6c 3e  LUSIVE].** </ul>
b890: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49  .**.** The [SQLI
b8a0: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
b8b0: 43 4c 4f 53 45 5d 20 66 6c 61 67 20 6d 65 61 6e  CLOSE] flag mean
b8c0: 73 20 74 68 65 20 66 69 6c 65 20 73 68 6f 75 6c  s the file shoul
b8d0: 64 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 64 20  d be.** deleted 
b8e0: 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65  when it is close
b8f0: 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  d.  The [SQLITE_
b900: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
b910: 53 45 5d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73  SE].** will be s
b920: 65 74 20 66 6f 72 20 54 45 4d 50 20 20 64 61 74  et for TEMP  dat
b930: 61 62 61 73 65 73 2c 20 6a 6f 75 72 6e 61 6c 73  abases, journals
b940: 20 61 6e 64 20 66 6f 72 20 73 75 62 6a 6f 75 72   and for subjour
b950: 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nals..**.** The 
b960: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43  [SQLITE_OPEN_EXC
b970: 4c 55 53 49 56 45 5d 20 66 6c 61 67 20 6d 65 61  LUSIVE] flag mea
b980: 6e 73 20 74 68 65 20 66 69 6c 65 20 73 68 6f 75  ns the file shou
b990: 6c 64 20 62 65 20 6f 70 65 6e 65 64 0a 2a 2a 20  ld be opened.** 
b9a0: 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20 61 63  for exclusive ac
b9b0: 63 65 73 73 2e 20 20 54 68 69 73 20 66 6c 61 67  cess.  This flag
b9c0: 20 69 73 20 73 65 74 20 66 6f 72 20 61 6c 6c 20   is set for all 
b9d0: 66 69 6c 65 73 20 65 78 63 65 70 74 0a 2a 2a 20  files except.** 
b9e0: 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  for the main dat
b9f0: 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
ba00: 2a 20 41 74 20 6c 65 61 73 74 20 73 7a 4f 73 46  * At least szOsF
ba10: 69 6c 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  ile bytes of mem
ba20: 6f 72 79 20 61 72 65 20 61 6c 6c 6f 63 61 74 65  ory are allocate
ba30: 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74  d by SQLite.** t
ba40: 6f 20 68 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c  o hold the  [sql
ba50: 69 74 65 33 5f 66 69 6c 65 5d 20 73 74 72 75 63  ite3_file] struc
ba60: 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74  ture passed as t
ba70: 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75  he third.** argu
ba80: 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20  ment to xOpen.  
ba90: 54 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  The xOpen method
baa0: 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74   does not have t
bab0: 6f 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68  o.** allocate th
bac0: 65 20 73 74 72 75 63 74 75 72 65 3b 20 69 74 20  e structure; it 
bad0: 73 68 6f 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c  should just fill
bae0: 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68   it in..**.** Th
baf0: 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74  e flags argument
bb00: 20 74 6f 20 78 41 63 63 65 73 73 28 29 20 6d 61   to xAccess() ma
bb10: 79 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 43 43  y be [SQLITE_ACC
bb20: 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a 2a 20 74  ESS_EXISTS].** t
bb30: 6f 20 74 65 73 74 20 66 6f 72 20 74 68 65 20 65  o test for the e
bb40: 78 69 73 74 65 6e 63 65 20 6f 66 20 61 20 66 69  xistence of a fi
bb50: 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41  le, or [SQLITE_A
bb60: 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 5d  CCESS_READWRITE]
bb70: 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77 68 65 74   to.** test whet
bb80: 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 72 65  her a file is re
bb90: 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61  adable and writa
bba0: 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ble, or [SQLITE_
bbb0: 41 43 43 45 53 53 5f 52 45 41 44 5d 0a 2a 2a 20  ACCESS_READ].** 
bbc0: 74 6f 20 74 65 73 74 20 77 68 65 74 68 65 72 20  to test whether 
bbd0: 61 20 66 69 6c 65 20 69 73 20 61 74 20 6c 65 61  a file is at lea
bbe0: 73 74 20 72 65 61 64 61 62 6c 65 2e 20 20 20 54  st readable.   T
bbf0: 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 61  he file can be a
bc00: 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a  .** directory..*
bc10: 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c  *.** SQLite will
bc20: 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74 65   always allocate
bc30: 20 61 74 20 6c 65 61 73 74 20 6d 78 50 61 74 68   at least mxPath
bc40: 6e 61 6d 65 2b 31 20 62 79 74 65 73 20 66 6f 72  name+1 bytes for
bc50: 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62   the.** output b
bc60: 75 66 66 65 72 20 78 46 75 6c 6c 50 61 74 68 6e  uffer xFullPathn
bc70: 61 6d 65 2e 20 20 54 68 65 20 65 78 61 63 74 20  ame.  The exact 
bc80: 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70  size of the outp
bc90: 75 74 20 62 75 66 66 65 72 0a 2a 2a 20 69 73 20  ut buffer.** is 
bca0: 61 6c 73 6f 20 70 61 73 73 65 64 20 61 73 20 61  also passed as a
bcb0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 6f   parameter to bo
bcc0: 74 68 20 20 6d 65 74 68 6f 64 73 2e 20 49 66 20  th  methods. If 
bcd0: 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
bce0: 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67  r.** is not larg
bcf0: 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c 49 54  e enough, [SQLIT
bd00: 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68 6f 75  E_CANTOPEN] shou
bd10: 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20  ld be returned. 
bd20: 53 69 6e 63 65 20 74 68 69 73 20 69 73 0a 2a 2a  Since this is.**
bd30: 20 68 61 6e 64 6c 65 64 20 61 73 20 61 20 66 61   handled as a fa
bd40: 74 61 6c 20 65 72 72 6f 72 20 62 79 20 53 51 4c  tal error by SQL
bd50: 69 74 65 2c 20 76 66 73 20 69 6d 70 6c 65 6d 65  ite, vfs impleme
bd60: 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20  ntations should 
bd70: 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f 20 70  endeavor.** to p
bd80: 72 65 76 65 6e 74 20 74 68 69 73 20 62 79 20 73  revent this by s
bd90: 65 74 74 69 6e 67 20 6d 78 50 61 74 68 6e 61 6d  etting mxPathnam
bda0: 65 20 74 6f 20 61 20 73 75 66 66 69 63 69 65 6e  e to a sufficien
bdb0: 74 6c 79 20 6c 61 72 67 65 20 76 61 6c 75 65 2e  tly large value.
bdc0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64  .**.** The xRand
bdd0: 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 65 70  omness(), xSleep
bde0: 28 29 2c 20 61 6e 64 20 78 43 75 72 72 65 6e 74  (), and xCurrent
bdf0: 54 69 6d 65 28 29 20 69 6e 74 65 72 66 61 63 65  Time() interface
be00: 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 73 74 72  s.** are not str
be10: 69 63 74 6c 79 20 61 20 70 61 72 74 20 6f 66 20  ictly a part of 
be20: 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 2c 20  the filesystem, 
be30: 62 75 74 20 74 68 65 79 20 61 72 65 0a 2a 2a 20  but they are.** 
be40: 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20  included in the 
be50: 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f  VFS structure fo
be60: 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 0a  r completeness..
be70: 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65  ** The xRandomne
be80: 73 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 74  ss() function at
be90: 74 65 6d 70 74 73 20 74 6f 20 72 65 74 75 72 6e  tempts to return
bea0: 20 6e 42 79 74 65 73 20 62 79 74 65 73 0a 2a 2a   nBytes bytes.**
beb0: 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c 69 74 79   of good-quality
bec0: 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f   randomness into
bed0: 20 7a 4f 75 74 2e 20 20 54 68 65 20 72 65 74 75   zOut.  The retu
bee0: 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74  rn value is.** t
bef0: 68 65 20 61 63 74 75 61 6c 20 6e 75 6d 62 65 72  he actual number
bf00: 20 6f 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e   of bytes of ran
bf10: 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64  domness obtained
bf20: 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65 65 70 28  ..** The xSleep(
bf30: 29 20 6d 65 74 68 6f 64 20 63 61 75 73 65 73 20  ) method causes 
bf40: 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 65  the calling thre
bf50: 61 64 20 74 6f 20 73 6c 65 65 70 20 66 6f 72 20  ad to sleep for 
bf60: 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74 68 65 20  at.** least the 
bf70: 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73  number of micros
bf80: 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e 20 20 54  econds given.  T
bf90: 68 65 20 78 43 75 72 72 65 6e 74 54 69 6d 65 28  he xCurrentTime(
bfa0: 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75  ).** method retu
bfb0: 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79  rns a Julian Day
bfc0: 20 4e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   Number for the 
bfd0: 63 75 72 72 65 6e 74 20 64 61 74 65 20 61 6e 64  current date and
bfe0: 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79 70   time..**.*/.typ
bff0: 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
c000: 74 65 33 5f 76 66 73 20 73 71 6c 69 74 65 33 5f  te3_vfs sqlite3_
c010: 76 66 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69  vfs;.struct sqli
c020: 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e 74 20  te3_vfs {.  int 
c030: 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20 20 20  iVersion;       
c040: 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72       /* Structur
c050: 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72  e version number
c060: 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f 73 46 69   */.  int szOsFi
c070: 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  le;            /
c080: 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 63 6c 61  * Size of subcla
c090: 73 73 65 64 20 73 71 6c 69 74 65 33 5f 66 69 6c  ssed sqlite3_fil
c0a0: 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 74  e */.  int mxPat
c0b0: 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  hname;          
c0c0: 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 6c 65 20  /* Maximum file 
c0d0: 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 74 68 20  pathname length 
c0e0: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  */.  sqlite3_vfs
c0f0: 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a   *pNext;      /*
c100: 20 4e 65 78 74 20 72 65 67 69 73 74 65 72 65 64   Next registered
c110: 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   VFS */.  const 
c120: 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
c130: 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
c140: 69 73 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20  is virtual file 
c150: 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76 6f 69 64  system */.  void
c160: 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20 20   *pAppData;     
c170: 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
c180: 74 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73  to application-s
c190: 70 65 63 69 66 69 63 20 64 61 74 61 20 2a 2f 0a  pecific data */.
c1a0: 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73    int (*xOpen)(s
c1b0: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e  qlite3_vfs*, con
c1c0: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
c1d0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 0a 20  sqlite3_file*,. 
c1e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
c1f0: 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 4f  t flags, int *pO
c200: 75 74 46 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20  utFlags);.  int 
c210: 28 2a 78 44 65 6c 65 74 65 29 28 73 71 6c 69 74  (*xDelete)(sqlit
c220: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
c230: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20  har *zName, int 
c240: 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 6e 74 20  syncDir);.  int 
c250: 28 2a 78 41 63 63 65 73 73 29 28 73 71 6c 69 74  (*xAccess)(sqlit
c260: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
c270: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20  har *zName, int 
c280: 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 52 65 73  flags, int *pRes
c290: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46  Out);.  int (*xF
c2a0: 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28 73 71 6c  ullPathname)(sql
c2b0: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74  ite3_vfs*, const
c2c0: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
c2d0: 74 20 6e 4f 75 74 2c 20 63 68 61 72 20 2a 7a 4f  t nOut, char *zO
c2e0: 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78  ut);.  void *(*x
c2f0: 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74 65 33 5f  DlOpen)(sqlite3_
c300: 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  vfs*, const char
c310: 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20   *zFilename);.  
c320: 76 6f 69 64 20 28 2a 78 44 6c 45 72 72 6f 72 29  void (*xDlError)
c330: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69  (sqlite3_vfs*, i
c340: 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a  nt nByte, char *
c350: 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 6f 69 64  zErrMsg);.  void
c360: 20 28 2a 28 2a 78 44 6c 53 79 6d 29 28 73 71 6c   (*(*xDlSym)(sql
c370: 69 74 65 33 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c  ite3_vfs*,void*,
c380: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79   const char *zSy
c390: 6d 62 6f 6c 29 29 28 76 6f 69 64 29 3b 0a 20 20  mbol))(void);.  
c3a0: 76 6f 69 64 20 28 2a 78 44 6c 43 6c 6f 73 65 29  void (*xDlClose)
c3b0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 76  (sqlite3_vfs*, v
c3c0: 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  oid*);.  int (*x
c3d0: 52 61 6e 64 6f 6d 6e 65 73 73 29 28 73 71 6c 69  Randomness)(sqli
c3e0: 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 42  te3_vfs*, int nB
c3f0: 79 74 65 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29  yte, char *zOut)
c400: 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c 65 65 70  ;.  int (*xSleep
c410: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  )(sqlite3_vfs*, 
c420: 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73  int microseconds
c430: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 75 72 72  );.  int (*xCurr
c440: 65 6e 74 54 69 6d 65 29 28 73 71 6c 69 74 65 33  entTime)(sqlite3
c450: 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b  _vfs*, double*);
c460: 0a 20 20 69 6e 74 20 28 2a 78 47 65 74 4c 61 73  .  int (*xGetLas
c470: 74 45 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f  tError)(sqlite3_
c480: 76 66 73 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20  vfs*, int, char 
c490: 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 20 66 69 65  *);.  /* New fie
c4a0: 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e  lds may be appen
c4b0: 64 65 64 20 69 6e 20 66 69 67 75 72 65 20 76 65  ded in figure ve
c4c0: 72 73 69 6f 6e 73 2e 20 20 54 68 65 20 69 56 65  rsions.  The iVe
c4d0: 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 61 6c 75 65  rsion.  ** value
c4e0: 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 20   will increment 
c4f0: 77 68 65 6e 65 76 65 72 20 74 68 69 73 20 68 61  whenever this ha
c500: 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ppens. */.};../*
c510: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c  .** CAPI3REF: Fl
c520: 61 67 73 20 66 6f 72 20 74 68 65 20 78 41 63 63  ags for the xAcc
c530: 65 73 73 20 56 46 53 20 6d 65 74 68 6f 64 20 7b  ess VFS method {
c540: 48 31 31 31 39 30 7d 20 3c 48 31 31 31 34 30 3e  H11190} <H11140>
c550: 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74  .**.** These int
c560: 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 63  eger constants c
c570: 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 68  an be used as th
c580: 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
c590: 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78 41 63 63  r to.** the xAcc
c5a0: 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e  ess method of an
c5b0: 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f   [sqlite3_vfs] o
c5c0: 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20 20 54 68  bject. {END}  Th
c5d0: 65 79 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20  ey determine.** 
c5e0: 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 70 65 72  what kind of per
c5f0: 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 78 41 63  missions the xAc
c600: 63 65 73 73 20 6d 65 74 68 6f 64 20 69 73 20 6c  cess method is l
c610: 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20 57  ooking for..** W
c620: 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ith SQLITE_ACCES
c630: 53 5f 45 58 49 53 54 53 2c 20 74 68 65 20 78 41  S_EXISTS, the xA
c640: 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20  ccess method.** 
c650: 73 69 6d 70 6c 79 20 63 68 65 63 6b 73 20 77 68  simply checks wh
c660: 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 20 65  ether the file e
c670: 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74 68 20 53  xists..** With S
c680: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
c690: 44 57 52 49 54 45 2c 20 74 68 65 20 78 41 63 63  DWRITE, the xAcc
c6a0: 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68  ess method.** ch
c6b0: 65 63 6b 73 20 77 68 65 74 68 65 72 20 74 68 65  ecks whether the
c6c0: 20 66 69 6c 65 20 69 73 20 62 6f 74 68 20 72 65   file is both re
c6d0: 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61  adable and writa
c6e0: 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c  ble..** With SQL
c6f0: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 2c  ITE_ACCESS_READ,
c700: 20 74 68 65 20 78 41 63 63 65 73 73 20 6d 65 74   the xAccess met
c710: 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 20 77 68  hod.** checks wh
c720: 65 74 68 65 72 20 74 68 65 20 66 69 6c 65 20 69  ether the file i
c730: 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23  s readable..*/.#
c740: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43  define SQLITE_AC
c750: 43 45 53 53 5f 45 58 49 53 54 53 20 20 20 20 30  CESS_EXISTS    0
c760: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
c770: 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45  ACCESS_READWRITE
c780: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
c790: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20 20 20  E_ACCESS_READ   
c7a0: 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49     2../*.** CAPI
c7b0: 33 52 45 46 3a 20 49 6e 69 74 69 61 6c 69 7a 65  3REF: Initialize
c7c0: 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72   The SQLite Libr
c7d0: 61 72 79 20 7b 48 31 30 31 33 30 7d 20 3c 53 32  ary {H10130} <S2
c7e0: 30 30 30 30 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a  0000><S30100>.**
c7f0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
c800: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75  initialize() rou
c810: 74 69 6e 65 20 69 6e 69 74 69 61 6c 69 7a 65 73  tine initializes
c820: 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74 65 20 6c   the.** SQLite l
c830: 69 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 6c  ibrary.  The sql
c840: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20  ite3_shutdown() 
c850: 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c  routine.** deall
c860: 6f 63 61 74 65 73 20 61 6e 79 20 72 65 73 6f 75  ocates any resou
c870: 72 63 65 73 20 74 68 61 74 20 77 65 72 65 20 61  rces that were a
c880: 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69  llocated by sqli
c890: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
c8a0: 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  ..**.** A call t
c8b0: 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  o sqlite3_initia
c8c0: 6c 69 7a 65 28 29 20 69 73 20 61 6e 20 22 65 66  lize() is an "ef
c8d0: 66 65 63 74 69 76 65 22 20 63 61 6c 6c 20 69 66  fective" call if
c8e0: 20 69 74 20 69 73 0a 2a 2a 20 74 68 65 20 66 69   it is.** the fi
c8f0: 72 73 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33  rst time sqlite3
c900: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73  _initialize() is
c910: 20 69 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 20   invoked during 
c920: 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a  the lifetime of.
c930: 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 2c 20  ** the process, 
c940: 6f 72 20 69 66 20 69 74 20 69 73 20 74 68 65 20  or if it is the 
c950: 66 69 72 73 74 20 74 69 6d 65 20 73 71 6c 69 74  first time sqlit
c960: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
c970: 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f  is invoked.** fo
c980: 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c 20 74  llowing a call t
c990: 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  o sqlite3_shutdo
c9a0: 77 6e 28 29 2e 20 20 4f 6e 6c 79 20 61 6e 20 65  wn().  Only an e
c9b0: 66 66 65 63 74 69 76 65 20 63 61 6c 6c 0a 2a 2a  ffective call.**
c9c0: 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e 69 74   of sqlite3_init
c9d0: 69 61 6c 69 7a 65 28 29 20 64 6f 65 73 20 61 6e  ialize() does an
c9e0: 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  y initialization
c9f0: 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 63 61 6c  .  All other cal
ca00: 6c 73 0a 2a 2a 20 61 72 65 20 68 61 72 6d 6c 65  ls.** are harmle
ca10: 73 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a  ss no-ops..**.**
ca20: 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   A call to sqlit
ca30: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 69 73  e3_shutdown() is
ca40: 20 61 6e 20 22 65 66 66 65 63 74 69 76 65 22 20   an "effective" 
ca50: 63 61 6c 6c 20 69 66 20 69 74 20 69 73 20 74 68  call if it is th
ca60: 65 20 66 69 72 73 74 0a 2a 2a 20 63 61 6c 6c 20  e first.** call 
ca70: 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  to sqlite3_shutd
ca80: 6f 77 6e 28 29 20 73 69 6e 63 65 20 74 68 65 20  own() since the 
ca90: 6c 61 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 69  last sqlite3_ini
caa0: 74 69 61 6c 69 7a 65 28 29 2e 20 20 4f 6e 6c 79  tialize().  Only
cab0: 0a 2a 2a 20 61 6e 20 65 66 66 65 63 74 69 76 65  .** an effective
cac0: 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
cad0: 5f 73 68 75 74 64 6f 77 6e 28 29 20 64 6f 65 73  _shutdown() does
cae0: 20 61 6e 79 20 64 65 69 6e 69 74 69 61 6c 69 7a   any deinitializ
caf0: 61 74 69 6f 6e 2e 0a 2a 2a 20 41 6c 6c 20 6f 74  ation..** All ot
cb00: 68 65 72 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  her calls to sql
cb10: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20  ite3_shutdown() 
cb20: 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d  are harmless no-
cb30: 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67  ops..**.** Among
cb40: 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c 20 73   other things, s
cb50: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
cb60: 65 28 29 20 73 68 61 6c 6c 20 69 6e 76 6f 6b 65  e() shall invoke
cb70: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69  .** sqlite3_os_i
cb80: 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61 72 6c  nit().  Similarl
cb90: 79 2c 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  y, sqlite3_shutd
cba0: 6f 77 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c 20 69  own().** shall i
cbb0: 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 6f 73  nvoke sqlite3_os
cbc0: 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  _end()..**.** Th
cbd0: 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  e sqlite3_initia
cbe0: 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 20 72  lize() routine r
cbf0: 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f  eturns [SQLITE_O
cc00: 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a  K] on success..*
cc10: 2a 20 49 66 20 66 6f 72 20 73 6f 6d 65 20 72 65  * If for some re
cc20: 61 73 6f 6e 2c 20 73 71 6c 69 74 65 33 5f 69 6e  ason, sqlite3_in
cc30: 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 75 6e  itialize() is un
cc40: 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61 6c 69  able to initiali
cc50: 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72  ze.** the librar
cc60: 79 20 28 70 65 72 68 61 70 73 20 69 74 20 69 73  y (perhaps it is
cc70: 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63   unable to alloc
cc80: 61 74 65 20 61 20 6e 65 65 64 65 64 20 72 65 73  ate a needed res
cc90: 6f 75 72 63 65 20 73 75 63 68 0a 2a 2a 20 61 73  ource such.** as
cca0: 20 61 20 6d 75 74 65 78 29 20 69 74 20 72 65 74   a mutex) it ret
ccb0: 75 72 6e 73 20 61 6e 20 5b 65 72 72 6f 72 20 63  urns an [error c
ccc0: 6f 64 65 5d 20 6f 74 68 65 72 20 74 68 61 6e 20  ode] other than 
ccd0: 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a  [SQLITE_OK]..**.
cce0: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69  ** The sqlite3_i
ccf0: 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74  nitialize() rout
cd00: 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 6e  ine is called in
cd10: 74 65 72 6e 61 6c 6c 79 20 62 79 20 6d 61 6e 79  ternally by many
cd20: 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69 74 65   other.** SQLite
cd30: 20 69 6e 74 65 72 66 61 63 65 73 20 73 6f 20 74   interfaces so t
cd40: 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61 74 69  hat an applicati
cd50: 6f 6e 20 75 73 75 61 6c 6c 79 20 64 6f 65 73 20  on usually does 
cd60: 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 69  not need to.** i
cd70: 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e  nvoke sqlite3_in
cd80: 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72 65 63  itialize() direc
cd90: 74 6c 79 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  tly.  For exampl
cda0: 65 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  e, [sqlite3_open
cdb0: 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73 71 6c  ()].** calls sql
cdc0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
cdd0: 29 20 73 6f 20 74 68 65 20 53 51 4c 69 74 65 20  ) so the SQLite 
cde0: 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 62 65 20  library will be 
cdf0: 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a  automatically.**
ce00: 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65   initialized whe
ce10: 6e 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  n [sqlite3_open(
ce20: 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20  )] is called if 
ce30: 69 74 20 68 61 73 20 6e 6f 74 20 62 65 20 69 6e  it has not be in
ce40: 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 6c 72  itialized.** alr
ce50: 65 61 64 79 2e 20 20 48 6f 77 65 76 65 72 2c 20  eady.  However, 
ce60: 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  if SQLite is com
ce70: 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 5b  piled with the [
ce80: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
ce90: 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69 6c 65  INIT].** compile
cea0: 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20 74 68  -time option, th
ceb0: 65 6e 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  en the automatic
cec0: 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
ced0: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a  3_initialize().*
cee0: 2a 20 61 72 65 20 6f 6d 69 74 74 65 64 20 61 6e  * are omitted an
cef0: 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  d the applicatio
cf00: 6e 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69  n must call sqli
cf10: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
cf20: 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 70 72 69   directly.** pri
cf30: 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 20  or to using any 
cf40: 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74  other SQLite int
cf50: 65 72 66 61 63 65 2e 20 20 46 6f 72 20 6d 61 78  erface.  For max
cf60: 69 6d 75 6d 20 70 6f 72 74 61 62 69 6c 69 74 79  imum portability
cf70: 2c 0a 2a 2a 20 69 74 20 69 73 20 72 65 63 6f 6d  ,.** it is recom
cf80: 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c  mended that appl
cf90: 69 63 61 74 69 6f 6e 73 20 61 6c 77 61 79 73 20  ications always 
cfa0: 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69  invoke sqlite3_i
cfb0: 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 64  nitialize().** d
cfc0: 69 72 65 63 74 6c 79 20 70 72 69 6f 72 20 74 6f  irectly prior to
cfd0: 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 72   using any other
cfe0: 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
cff0: 65 2e 20 20 46 75 74 75 72 65 20 72 65 6c 65 61  e.  Future relea
d000: 73 65 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65  ses.** of SQLite
d010: 20 6d 61 79 20 72 65 71 75 69 72 65 20 74 68 69   may require thi
d020: 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  s.  In other wor
d030: 64 73 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72  ds, the behavior
d040: 20 65 78 68 69 62 69 74 65 64 0a 2a 2a 20 77 68   exhibited.** wh
d050: 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  en SQLite is com
d060: 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51 4c 49  piled with [SQLI
d070: 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
d080: 5d 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 74  ] might become t
d090: 68 65 0a 2a 2a 20 64 65 66 61 75 6c 74 20 62 65  he.** default be
d0a0: 68 61 76 69 6f 72 20 69 6e 20 73 6f 6d 65 20 66  havior in some f
d0b0: 75 74 75 72 65 20 72 65 6c 65 61 73 65 20 6f 66  uture release of
d0c0: 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54   SQLite..**.** T
d0d0: 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  he sqlite3_os_in
d0e0: 69 74 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  it() routine doe
d0f0: 73 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74  s operating-syst
d100: 65 6d 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 69  em specific.** i
d110: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66  nitialization of
d120: 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
d130: 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ary.  The sqlite
d140: 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 72 6f  3_os_end().** ro
d150: 75 74 69 6e 65 20 75 6e 64 6f 65 73 20 74 68 65  utine undoes the
d160: 20 65 66 66 65 63 74 20 6f 66 20 73 71 6c 69 74   effect of sqlit
d170: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 20 54  e3_os_init().  T
d180: 79 70 69 63 61 6c 20 74 61 73 6b 73 0a 2a 2a 20  ypical tasks.** 
d190: 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 65  performed by the
d1a0: 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 63 6c  se routines incl
d1b0: 75 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  ude allocation o
d1c0: 72 20 64 65 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  r deallocation.*
d1d0: 2a 20 6f 66 20 73 74 61 74 69 63 20 72 65 73 6f  * of static reso
d1e0: 75 72 63 65 73 2c 20 69 6e 69 74 69 61 6c 69 7a  urces, initializ
d1f0: 61 74 69 6f 6e 20 6f 66 20 67 6c 6f 62 61 6c 20  ation of global 
d200: 76 61 72 69 61 62 6c 65 73 2c 0a 2a 2a 20 73 65  variables,.** se
d210: 74 74 69 6e 67 20 75 70 20 61 20 64 65 66 61 75  tting up a defau
d220: 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  lt [sqlite3_vfs]
d230: 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 73 65 74 74   module, or sett
d240: 69 6e 67 20 75 70 0a 2a 2a 20 61 20 64 65 66 61  ing up.** a defa
d250: 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ult configuratio
d260: 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  n using [sqlite3
d270: 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a 0a 2a  _config()]..**.*
d280: 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * The applicatio
d290: 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 69  n should never i
d2a0: 6e 76 6f 6b 65 20 65 69 74 68 65 72 20 73 71 6c  nvoke either sql
d2b0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a  ite3_os_init().*
d2c0: 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f  * or sqlite3_os_
d2d0: 65 6e 64 28 29 20 64 69 72 65 63 74 6c 79 2e 20  end() directly. 
d2e0: 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
d2f0: 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 69 6e 76   should only inv
d300: 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69  oke.** sqlite3_i
d310: 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 20  nitialize() and 
d320: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
d330: 28 29 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  ().  The sqlite3
d340: 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 69 6e  _os_init().** in
d350: 74 65 72 66 61 63 65 20 69 73 20 63 61 6c 6c 65  terface is calle
d360: 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
d370: 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  by sqlite3_initi
d380: 61 6c 69 7a 65 28 29 20 61 6e 64 0a 2a 2a 20 73  alize() and.** s
d390: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20  qlite3_os_end() 
d3a0: 69 73 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c  is called by sql
d3b0: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e  ite3_shutdown().
d3c0: 20 20 41 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a    Appropriate.**
d3d0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
d3e0: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f   for sqlite3_os_
d3f0: 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74  init() and sqlit
d400: 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 61  e3_os_end().** a
d410: 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51  re built into SQ
d420: 4c 69 74 65 20 77 68 65 6e 20 69 74 20 69 73 20  Lite when it is 
d430: 63 6f 6d 70 69 6c 65 64 20 66 6f 72 20 75 6e 69  compiled for uni
d440: 78 2c 20 77 69 6e 64 6f 77 73 2c 20 6f 72 20 6f  x, windows, or o
d450: 73 2f 32 2e 0a 2a 2a 20 57 68 65 6e 20 62 75 69  s/2..** When bui
d460: 6c 74 20 66 6f 72 20 6f 74 68 65 72 20 70 6c 61  lt for other pla
d470: 74 66 6f 72 6d 73 20 28 75 73 69 6e 67 20 74 68  tforms (using th
d480: 65 20 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48  e [SQLITE_OS_OTH
d490: 45 52 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d 74 69  ER=1] compile-ti
d4a0: 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20 74 68  me.** option) th
d4b0: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  e application mu
d4c0: 73 74 20 73 75 70 70 6c 79 20 61 20 73 75 69 74  st supply a suit
d4d0: 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  able implementat
d4e0: 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74  ion for.** sqlit
d4f0: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64  e3_os_init() and
d500: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
d510: 29 2e 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69  ).  An applicati
d520: 6f 6e 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69  on-supplied.** i
d530: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
d540: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
d550: 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73  () or sqlite3_os
d560: 5f 65 6e 64 28 29 0a 2a 2a 20 6d 75 73 74 20 72  _end().** must r
d570: 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b  eturn [SQLITE_OK
d580: 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  ] on success and
d590: 20 73 6f 6d 65 20 6f 74 68 65 72 20 5b 65 72 72   some other [err
d5a0: 6f 72 20 63 6f 64 65 5d 20 75 70 6f 6e 0a 2a 2a  or code] upon.**
d5b0: 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53 51 4c   failure..*/.SQL
d5c0: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
d5d0: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76  te3_initialize(v
d5e0: 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  oid);.SQLITE_API
d5f0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 68 75   int sqlite3_shu
d600: 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 53 51 4c  tdown(void);.SQL
d610: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
d620: 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64  te3_os_init(void
d630: 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
d640: 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  t sqlite3_os_end
d650: 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  (void);../*.** C
d660: 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75  API3REF: Configu
d670: 72 69 6e 67 20 54 68 65 20 53 51 4c 69 74 65 20  ring The SQLite 
d680: 4c 69 62 72 61 72 79 20 7b 48 31 34 31 30 30 7d  Library {H14100}
d690: 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 32 30 30   <S20000><S30200
d6a0: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
d6b0: 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  L.**.** The sqli
d6c0: 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74  te3_config() int
d6d0: 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74  erface is used t
d6e0: 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20 63 6f  o make global co
d6f0: 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63  nfiguration.** c
d700: 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69 74 65  hanges to SQLite
d710: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 75 6e   in order to tun
d720: 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68 65 20  e SQLite to the 
d730: 73 70 65 63 69 66 69 63 20 6e 65 65 64 73 20 6f  specific needs o
d740: 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61  f.** the applica
d750: 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75  tion.  The defau
d760: 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  lt configuration
d770: 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20   is recommended 
d780: 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c  for most.** appl
d790: 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73 6f 20  ications and so 
d7a0: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
d7b0: 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65 63 65  usually not nece
d7c0: 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a 2a 2a  ssary.  It is.**
d7d0: 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73 75 70   provided to sup
d7e0: 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c 69 63  port rare applic
d7f0: 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e 75 73  ations with unus
d800: 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a  ual needs..**.**
d810: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e   The sqlite3_con
d820: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  fig() interface 
d830: 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
d840: 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74  e.  The applicat
d850: 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 73 75  ion.** must insu
d860: 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  re that no other
d870: 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
d880: 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 62  es are invoked b
d890: 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61  y other.** threa
d8a0: 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74 65 33  ds while sqlite3
d8b0: 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72 75 6e  _config() is run
d8c0: 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72 6d 6f  ning.  Furthermo
d8d0: 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  re, sqlite3_conf
d8e0: 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79  ig().** may only
d8f0: 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f   be invoked prio
d900: 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69 6e 69  r to library ini
d910: 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73 69 6e  tialization usin
d920: 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e  g.** [sqlite3_in
d930: 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61  itialize()] or a
d940: 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20 62 79  fter shutdown by
d950: 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f   [sqlite3_shutdo
d960: 77 6e 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 2c 20  wn()]..** Note, 
d970: 68 6f 77 65 76 65 72 2c 20 74 68 61 74 20 73 71  however, that sq
d980: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 63  lite3_config() c
d990: 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20  an be called as 
d9a0: 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 69  part of the.** i
d9b0: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
d9c0: 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   an application-
d9d0: 64 65 66 69 6e 65 64 20 5b 73 71 6c 69 74 65 33  defined [sqlite3
d9e0: 5f 6f 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a 2a 0a  _os_init()]..**.
d9f0: 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
da00: 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
da10: 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 61 6e 20  _config() is an 
da20: 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49  integer.** [SQLI
da30: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
da40: 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75  THREAD | configu
da50: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 20 74  ration option] t
da60: 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a  hat determines.*
da70: 2a 20 77 68 61 74 20 70 72 6f 70 65 72 74 79 20  * what property 
da80: 6f 66 20 53 51 4c 69 74 65 20 69 73 20 74 6f 20  of SQLite is to 
da90: 62 65 20 63 6f 6e 66 69 67 75 72 65 64 2e 20 20  be configured.  
daa0: 53 75 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d  Subsequent argum
dab0: 65 6e 74 73 0a 2a 2a 20 76 61 72 79 20 64 65 70  ents.** vary dep
dac0: 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 5b 53  ending on the [S
dad0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e  QLITE_CONFIG_SIN
dae0: 47 4c 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66  GLETHREAD | conf
daf0: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
db00: 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73  ].** in the firs
db10: 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  t argument..**.*
db20: 2a 20 57 68 65 6e 20 61 20 63 6f 6e 66 69 67 75  * When a configu
db30: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73  ration option is
db40: 20 73 65 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f   set, sqlite3_co
db50: 6e 66 69 67 28 29 20 72 65 74 75 72 6e 73 20 5b  nfig() returns [
db60: 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49  SQLITE_OK]..** I
db70: 66 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20  f the option is 
db80: 75 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74  unknown or SQLit
db90: 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 73  e is unable to s
dba0: 65 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a  et the option.**
dbb0: 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
dbc0: 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e  ne returns a non
dbd0: 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64  -zero [error cod
dbe0: 65 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  e]..**.** Requir
dbf0: 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 31  ements:.** [H141
dc00: 30 33 5d 20 5b 48 31 34 31 30 36 5d 20 5b 48 31  03] [H14106] [H1
dc10: 34 31 32 30 5d 20 5b 48 31 34 31 32 33 5d 20 5b  4120] [H14123] [
dc20: 48 31 34 31 32 36 5d 20 5b 48 31 34 31 32 39 5d  H14126] [H14129]
dc30: 20 5b 48 31 34 31 33 32 5d 20 5b 48 31 34 31 33   [H14132] [H1413
dc40: 35 5d 0a 2a 2a 20 5b 48 31 34 31 33 38 5d 20 5b  5].** [H14138] [
dc50: 48 31 34 31 34 31 5d 20 5b 48 31 34 31 34 34 5d  H14141] [H14144]
dc60: 20 5b 48 31 34 31 34 37 5d 20 5b 48 31 34 31 35   [H14147] [H1415
dc70: 30 5d 20 5b 48 31 34 31 35 33 5d 20 5b 48 31 34  0] [H14153] [H14
dc80: 31 35 36 5d 20 5b 48 31 34 31 35 39 5d 0a 2a 2a  156] [H14159].**
dc90: 20 5b 48 31 34 31 36 32 5d 20 5b 48 31 34 31 36   [H14162] [H1416
dca0: 35 5d 20 5b 48 31 34 31 36 38 5d 0a 2a 2f 0a 53  5] [H14168].*/.S
dcb0: 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
dcc0: 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e  _EXPERIMENTAL in
dcd0: 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  t sqlite3_config
dce0: 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a  (int, ...);../*.
dcf0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e  ** CAPI3REF: Con
dd00: 66 69 67 75 72 65 20 64 61 74 61 62 61 73 65 20  figure database 
dd10: 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 20 7b 48 31  connections  {H1
dd20: 34 32 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a  4200} <S20000>.*
dd30: 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
dd40: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
dd50: 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74  _db_config() int
dd60: 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74  erface is used t
dd70: 6f 20 6d 61 6b 65 20 63 6f 6e 66 69 67 75 72 61  o make configura
dd80: 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20  tion.** changes 
dd90: 74 6f 20 61 20 5b 64 61 74 61 62 61 73 65 20 63  to a [database c
dda0: 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65  onnection].  The
ddb0: 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 73 69   interface is si
ddc0: 6d 69 6c 61 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c  milar to.** [sql
ddd0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 65  ite3_config()] e
dde0: 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 63  xcept that the c
ddf0: 68 61 6e 67 65 73 20 61 70 70 6c 79 20 74 6f 20  hanges apply to 
de00: 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 5b 64 61 74  a single.** [dat
de10: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
de20: 5d 20 28 73 70 65 63 69 66 69 65 64 20 69 6e 20  ] (specified in 
de30: 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
de40: 6e 74 29 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c  nt).  The.** sql
de50: 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29  ite3_db_config()
de60: 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 6f   interface can o
de70: 6e 6c 79 20 62 65 20 75 73 65 64 20 69 6d 6d 65  nly be used imme
de80: 64 69 61 74 65 6c 79 20 61 66 74 65 72 0a 2a 2a  diately after.**
de90: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
dea0: 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 72 65 61  nnection is crea
deb0: 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ted using [sqlit
dec0: 65 33 5f 6f 70 65 6e 28 29 5d 2c 0a 2a 2a 20 5b  e3_open()],.** [
ded0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29  sqlite3_open16()
dee0: 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f  ], or [sqlite3_o
def0: 70 65 6e 5f 76 32 28 29 5d 2e 20 20 0a 2a 2a 0a  pen_v2()].  .**.
df00: 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ** The second ar
df10: 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65  gument to sqlite
df20: 33 5f 64 62 5f 63 6f 6e 66 69 67 28 44 2c 56 2c  3_db_config(D,V,
df30: 2e 2e 2e 29 20 20 69 73 20 74 68 65 0a 2a 2a 20  ...)  is the.** 
df40: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 76 65  configuration ve
df50: 72 62 20 2d 20 61 6e 20 69 6e 74 65 67 65 72 20  rb - an integer 
df60: 63 6f 64 65 20 74 68 61 74 20 69 6e 64 69 63 61  code that indica
df70: 74 65 73 20 77 68 61 74 0a 2a 2a 20 61 73 70 65  tes what.** aspe
df80: 63 74 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62  ct of the [datab
df90: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
dfa0: 69 73 20 62 65 69 6e 67 20 63 6f 6e 66 69 67 75  is being configu
dfb0: 72 65 64 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79  red..** The only
dfc0: 20 63 68 6f 69 63 65 20 66 6f 72 20 74 68 69 73   choice for this
dfd0: 20 76 61 6c 75 65 20 69 73 20 5b 53 51 4c 49 54   value is [SQLIT
dfe0: 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  E_DBCONFIG_LOOKA
dff0: 53 49 44 45 5d 2e 0a 2a 2a 20 4e 65 77 20 76 65  SIDE]..** New ve
e000: 72 62 73 20 61 72 65 20 6c 69 6b 65 6c 79 20 74  rbs are likely t
e010: 6f 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75  o be added in fu
e020: 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66  ture releases of
e030: 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 41 64 64 69   SQLite..** Addi
e040: 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73  tional arguments
e050: 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 76   depend on the v
e060: 65 72 62 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  erb..**.** Requi
e070: 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34  rements:.** [H14
e080: 32 30 33 5d 20 5b 48 31 34 32 30 36 5d 20 5b 48  203] [H14206] [H
e090: 31 34 32 30 39 5d 20 5b 48 31 34 32 31 32 5d 20  14209] [H14212] 
e0a0: 5b 48 31 34 32 31 35 5d 0a 2a 2f 0a 53 51 4c 49  [H14215].*/.SQLI
e0b0: 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
e0c0: 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73  PERIMENTAL int s
e0d0: 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
e0e0: 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f  (sqlite3*, int o
e0f0: 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20  p, ...);../*.** 
e100: 43 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79  CAPI3REF: Memory
e110: 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 52 6f 75 74   Allocation Rout
e120: 69 6e 65 73 20 7b 48 31 30 31 35 35 7d 20 3c 53  ines {H10155} <S
e130: 32 30 31 32 30 3e 0a 2a 2a 20 45 58 50 45 52 49  20120>.** EXPERI
e140: 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20  MENTAL.**.** An 
e150: 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
e160: 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20   object defines 
e170: 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 65  the interface be
e180: 74 77 65 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20  tween SQLite.** 
e190: 61 6e 64 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  and low-level me
e1a0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
e1b0: 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
e1c0: 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 75  This object is u
e1d0: 73 65 64 20 69 6e 20 6f 6e 6c 79 20 6f 6e 65 20  sed in only one 
e1e0: 70 6c 61 63 65 20 69 6e 20 74 68 65 20 53 51 4c  place in the SQL
e1f0: 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  ite interface..*
e200: 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * A pointer to a
e210: 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
e220: 69 73 20 6f 62 6a 65 63 74 20 69 73 20 74 68 65  is object is the
e230: 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20   argument to.** 
e240: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
e250: 29 5d 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 66  )] when the conf
e260: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
e270: 20 69 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43   is.** [SQLITE_C
e280: 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20  ONFIG_MALLOC].  
e290: 42 79 20 63 72 65 61 74 69 6e 67 20 61 6e 20 69  By creating an i
e2a0: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
e2b0: 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 70 61  object.** and pa
e2c0: 73 73 69 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c  ssing it to [sql
e2d0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 64  ite3_config()] d
e2e0: 75 72 69 6e 67 20 63 6f 6e 66 69 67 75 72 61 74  uring configurat
e2f0: 69 6f 6e 2c 20 61 6e 0a 2a 2a 20 61 70 70 6c 69  ion, an.** appli
e300: 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69  cation can speci
e310: 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76  fy an alternativ
e320: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
e330: 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a  ion subsystem.**
e340: 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75   for SQLite to u
e350: 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74  se for all of it
e360: 73 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79  s dynamic memory
e370: 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f   needs..**.** No
e380: 74 65 20 74 68 61 74 20 53 51 4c 69 74 65 20 63  te that SQLite c
e390: 6f 6d 65 73 20 77 69 74 68 20 61 20 62 75 69 6c  omes with a buil
e3a0: 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  t-in memory allo
e3b0: 63 61 74 6f 72 20 74 68 61 74 20 69 73 0a 2a 2a  cator that is.**
e3c0: 20 70 65 72 66 65 63 74 6c 79 20 61 64 65 71 75   perfectly adequ
e3d0: 61 74 65 20 66 6f 72 20 74 68 65 20 6f 76 65 72  ate for the over
e3e0: 77 68 65 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74  whelming majorit
e3f0: 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f 6e  y of application
e400: 73 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68  s.** and that th
e410: 69 73 20 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c  is object is onl
e420: 79 20 75 73 65 66 75 6c 20 74 6f 20 61 20 74 69  y useful to a ti
e430: 6e 79 20 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61  ny minority of a
e440: 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77  pplications.** w
e450: 69 74 68 20 73 70 65 63 69 61 6c 69 7a 65 64 20  ith specialized 
e460: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
e470: 6e 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2e 20  n requirements. 
e480: 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 0a   This object is.
e490: 2a 2a 20 61 6c 73 6f 20 75 73 65 64 20 64 75 72  ** also used dur
e4a0: 69 6e 67 20 74 65 73 74 69 6e 67 20 6f 66 20 53  ing testing of S
e4b0: 51 4c 69 74 65 20 69 6e 20 6f 72 64 65 72 20 74  QLite in order t
e4c0: 6f 20 73 70 65 63 69 66 79 20 61 6e 20 61 6c 74  o specify an alt
e4d0: 65 72 6e 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f  ernative.** memo
e4e0: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61  ry allocator tha
e4f0: 74 20 73 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f  t simulates memo
e500: 72 79 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79  ry out-of-memory
e510: 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a   conditions in.*
e520: 2a 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66  * order to verif
e530: 79 20 74 68 61 74 20 53 51 4c 69 74 65 20 72 65  y that SQLite re
e540: 63 6f 76 65 72 73 20 67 72 61 63 65 66 75 6c 6c  covers gracefull
e550: 79 20 66 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63  y from such.** c
e560: 6f 6e 64 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  onditions..**.**
e570: 20 54 68 65 20 78 4d 61 6c 6c 6f 63 2c 20 78 46   The xMalloc, xF
e580: 72 65 65 2c 20 61 6e 64 20 78 52 65 61 6c 6c 6f  ree, and xReallo
e590: 63 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 77  c methods must w
e5a0: 6f 72 6b 20 6c 69 6b 65 20 74 68 65 0a 2a 2a 20  ork like the.** 
e5b0: 6d 61 6c 6c 6f 63 28 29 2c 20 66 72 65 65 28 29  malloc(), free()
e5c0: 2c 20 61 6e 64 20 72 65 61 6c 6c 6f 63 28 29 20  , and realloc() 
e5d0: 66 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 74  functions from t
e5e0: 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72  he standard libr
e5f0: 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 78 53 69 7a 65  ary..**.** xSize
e600: 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74   should return t
e610: 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a  he allocated siz
e620: 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  e of a memory al
e630: 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70 72 65 76  location.** prev
e640: 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20  iously obtained 
e650: 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20 6f 72 20  from xMalloc or 
e660: 78 52 65 61 6c 6c 6f 63 2e 20 20 54 68 65 20 61  xRealloc.  The a
e670: 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 0a 2a 2a  llocated size.**
e680: 20 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c 65   is always at le
e690: 61 73 74 20 61 73 20 62 69 67 20 61 73 20 74 68  ast as big as th
e6a0: 65 20 72 65 71 75 65 73 74 65 64 20 73 69 7a 65  e requested size
e6b0: 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 67   but may be larg
e6c0: 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52  er..**.** The xR
e6d0: 6f 75 6e 64 75 70 20 6d 65 74 68 6f 64 20 72 65  oundup method re
e6e0: 74 75 72 6e 73 20 77 68 61 74 20 77 6f 75 6c 64  turns what would
e6f0: 20 62 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 65   be the allocate
e700: 64 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 61 20 6d  d size of.** a m
e710: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
e720: 20 67 69 76 65 6e 20 61 20 70 61 72 74 69 63 75   given a particu
e730: 6c 61 72 20 72 65 71 75 65 73 74 65 64 20 73 69  lar requested si
e740: 7a 65 2e 20 20 4d 6f 73 74 20 6d 65 6d 6f 72 79  ze.  Most memory
e750: 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72  .** allocators r
e760: 6f 75 6e 64 20 75 70 20 6d 65 6d 6f 72 79 20 61  ound up memory a
e770: 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 74 20 6c 65  llocations at le
e780: 61 73 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ast to the next 
e790: 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f 66 20 38  multiple.** of 8
e7a0: 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 61 74 6f  .  Some allocato
e7b0: 72 73 20 72 6f 75 6e 64 20 75 70 20 74 6f 20 61  rs round up to a
e7c0: 20 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65   larger multiple
e7d0: 20 6f 72 20 74 6f 20 61 20 70 6f 77 65 72 20 6f   or to a power o
e7e0: 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  f 2..**.** The x
e7f0: 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e 69 74  Init method init
e800: 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d 6f  ializes the memo
e810: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 28  ry allocator.  (
e820: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20  For example,.** 
e830: 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74  it might allocat
e840: 65 20 61 6e 79 20 72 65 71 75 69 72 65 20 6d 75  e any require mu
e850: 74 65 78 65 73 20 6f 72 20 69 6e 69 74 69 61 6c  texes or initial
e860: 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74  ize internal dat
e870: 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e  a.** structures.
e880: 20 20 54 68 65 20 78 53 68 75 74 64 6f 77 6e 20    The xShutdown 
e890: 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b 65  method is invoke
e8a0: 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20 62  d (indirectly) b
e8b0: 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68  y.** [sqlite3_sh
e8c0: 75 74 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73 68  utdown()] and sh
e8d0: 6f 75 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65 20  ould deallocate 
e8e0: 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 61 63  any resources ac
e8f0: 71 75 69 72 65 64 0a 2a 2a 20 62 79 20 78 49 6e  quired.** by xIn
e900: 69 74 2e 20 20 54 68 65 20 70 41 70 70 44 61 74  it.  The pAppDat
e910: 61 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65  a pointer is use
e920: 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 61  d as the only pa
e930: 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78 49  rameter to.** xI
e940: 6e 69 74 20 61 6e 64 20 78 53 68 75 74 64 6f 77  nit and xShutdow
e950: 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
e960: 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d  ruct sqlite3_mem
e970: 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33  _methods sqlite3
e980: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 3b 0a 73 74  _mem_methods;.st
e990: 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d  ruct sqlite3_mem
e9a0: 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69  _methods {.  voi
e9b0: 64 20 2a 28 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e  d *(*xMalloc)(in
e9c0: 74 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  t);         /* M
e9d0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
e9e0: 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
e9f0: 6f 69 64 20 28 2a 78 46 72 65 65 29 28 76 6f 69  oid (*xFree)(voi
ea00: 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  d*);          /*
ea10: 20 46 72 65 65 20 61 20 70 72 69 6f 72 20 61 6c   Free a prior al
ea20: 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  location */.  vo
ea30: 69 64 20 2a 28 2a 78 52 65 61 6c 6c 6f 63 29 28  id *(*xRealloc)(
ea40: 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20  void*,int);  /* 
ea50: 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61  Resize an alloca
ea60: 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  tion */.  int (*
ea70: 78 53 69 7a 65 29 28 76 6f 69 64 2a 29 3b 20 20  xSize)(void*);  
ea80: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
ea90: 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  rn the size of a
eaa0: 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a  n allocation */.
eab0: 20 20 69 6e 74 20 28 2a 78 52 6f 75 6e 64 75 70    int (*xRoundup
eac0: 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20 20 20  )(int);         
ead0: 20 2f 2a 20 52 6f 75 6e 64 20 75 70 20 72 65 71   /* Round up req
eae0: 75 65 73 74 20 73 69 7a 65 20 74 6f 20 61 6c 6c  uest size to all
eaf0: 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 2a 2f 0a  ocation size */.
eb00: 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 76    int (*xInit)(v
eb10: 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20  oid*);          
eb20: 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
eb30: 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
eb40: 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  tor */.  void (*
eb50: 78 53 68 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a  xShutdown)(void*
eb60: 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 69 6e 69  );      /* Deini
eb70: 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f  tialize the memo
eb80: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a  ry allocator */.
eb90: 20 20 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61    void *pAppData
eba0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ebb0: 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
ebc0: 78 49 6e 69 74 28 29 20 61 6e 64 20 78 53 68 75  xInit() and xShu
ebd0: 74 64 6f 77 6e 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f  tdown() */.};../
ebe0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
ebf0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 74  onfiguration Opt
ec00: 69 6f 6e 73 20 7b 48 31 30 31 36 30 7d 20 3c 53  ions {H10160} <S
ec10: 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49  20000>.** EXPERI
ec20: 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65  MENTAL.**.** The
ec30: 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65  se constants are
ec40: 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69   the available i
ec50: 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 61  nteger configura
ec60: 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61  tion options tha
ec70: 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 73  t.** can be pass
ec80: 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
ec90: 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
eca0: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
ecb0: 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  )] interface..**
ecc0: 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69 67 75 72  .** New configur
ecd0: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 61  ation options ma
ece0: 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75  y be added in fu
ecf0: 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66  ture releases of
ed00: 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45 78 69 73   SQLite..** Exis
ed10: 74 69 6e 67 20 63 6f 6e 66 69 67 75 72 61 74 69  ting configurati
ed20: 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69 67 68 74  on options might
ed30: 20 62 65 20 64 69 73 63 6f 6e 74 69 6e 75 65 64   be discontinued
ed40: 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 0a  .  Applications.
ed50: 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20  ** should check 
ed60: 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
ed70: 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f  from [sqlite3_co
ed80: 6e 66 69 67 28 29 5d 20 74 6f 20 6d 61 6b 65 20  nfig()] to make 
ed90: 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 74 68 65  sure that.** the
eda0: 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 54   call worked.  T
edb0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66  he [sqlite3_conf
edc0: 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  ig()] interface 
edd0: 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a  will return a.**
ede0: 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72   non-zero [error
edf0: 20 63 6f 64 65 5d 20 69 66 20 61 20 64 69 73 63   code] if a disc
ee00: 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73 75  ontinued or unsu
ee10: 70 70 6f 72 74 65 64 20 63 6f 6e 66 69 67 75 72  pported configur
ee20: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20  ation option.** 
ee30: 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a  is invoked..**.*
ee40: 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51  * <dl>.** <dt>SQ
ee50: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47  LITE_CONFIG_SING
ee60: 4c 45 54 48 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a  LETHREAD</dt>.**
ee70: 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e   <dd>There are n
ee80: 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  o arguments to t
ee90: 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69  his option.  Thi
eea0: 73 20 6f 70 74 69 6f 6e 20 64 69 73 61 62 6c 65  s option disable
eeb0: 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 69 6e  s.** all mutexin
eec0: 67 20 61 6e 64 20 70 75 74 73 20 53 51 4c 69 74  g and puts SQLit
eed0: 65 20 69 6e 74 6f 20 61 20 6d 6f 64 65 20 77 68  e into a mode wh
eee0: 65 72 65 20 69 74 20 63 61 6e 20 6f 6e 6c 79 20  ere it can only 
eef0: 62 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 61 20  be used.** by a 
ef00: 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 3c 2f  single thread.</
ef10: 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
ef20: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54  LITE_CONFIG_MULT
ef30: 49 54 48 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20  ITHREAD</dt>.** 
ef40: 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e 6f  <dd>There are no
ef50: 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
ef60: 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73  is option.  This
ef70: 20 6f 70 74 69 6f 6e 20 64 69 73 61 62 6c 65 73   option disables
ef80: 0a 2a 2a 20 6d 75 74 65 78 69 6e 67 20 6f 6e 20  .** mutexing on 
ef90: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
efa0: 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61  tion] and [prepa
efb0: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f  red statement] o
efc0: 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 68 65 20 61  bjects..** The a
efd0: 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20 72 65  pplication is re
efe0: 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65  sponsible for se
eff0: 72 69 61 6c 69 7a 69 6e 67 20 61 63 63 65 73 73  rializing access
f000: 20 74 6f 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65   to.** [database
f010: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 61 6e   connections] an
f020: 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  d [prepared stat
f030: 65 6d 65 6e 74 73 5d 2e 20 20 42 75 74 20 6f 74  ements].  But ot
f040: 68 65 72 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61  her mutexes.** a
f050: 72 65 20 65 6e 61 62 6c 65 64 20 73 6f 20 74 68  re enabled so th
f060: 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62  at SQLite will b
f070: 65 20 73 61 66 65 20 74 6f 20 75 73 65 20 69 6e  e safe to use in
f080: 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65   a multi-threade
f090: 64 0a 2a 2a 20 65 6e 76 69 72 6f 6e 6d 65 6e 74  d.** environment
f0a0: 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 6f 20 74   as long as no t
f0b0: 77 6f 20 74 68 72 65 61 64 73 20 61 74 74 65 6d  wo threads attem
f0c0: 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 73 61  pt to use the sa
f0d0: 6d 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20  me.** [database 
f0e0: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 74 20 74  connection] at t
f0f0: 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 53  he same time.  S
f100: 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e  ee the [threadin
f110: 67 20 6d 6f 64 65 5d 0a 2a 2a 20 64 6f 63 75 6d  g mode].** docum
f120: 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64  entation for add
f130: 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
f140: 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ion.</dd>.**.** 
f150: 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
f160: 47 5f 53 45 52 49 41 4c 49 5a 45 44 3c 2f 64 74  G_SERIALIZED</dt
f170: 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61  >.** <dd>There a
f180: 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20  re no arguments 
f190: 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20  to this option. 
f1a0: 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 65 6e 61   This option ena
f1b0: 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65  bles.** all mute
f1c0: 78 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  xes including th
f1d0: 65 20 72 65 63 75 72 73 69 76 65 0a 2a 2a 20 6d  e recursive.** m
f1e0: 75 74 65 78 65 73 20 6f 6e 20 5b 64 61 74 61 62  utexes on [datab
f1f0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
f200: 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74  and [prepared st
f210: 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73  atement] objects
f220: 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64  ..** In this mod
f230: 65 20 28 77 68 69 63 68 20 69 73 20 74 68 65 20  e (which is the 
f240: 64 65 66 61 75 6c 74 20 77 68 65 6e 20 53 51 4c  default when SQL
f250: 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ite is compiled 
f260: 77 69 74 68 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  with.** [SQLITE_
f270: 54 48 52 45 41 44 53 41 46 45 3d 31 5d 29 20 74  THREADSAFE=1]) t
f280: 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
f290: 79 20 77 69 6c 6c 20 69 74 73 65 6c 66 20 73 65  y will itself se
f2a0: 72 69 61 6c 69 7a 65 20 61 63 63 65 73 73 0a 2a  rialize access.*
f2b0: 2a 20 74 6f 20 5b 64 61 74 61 62 61 73 65 20 63  * to [database c
f2c0: 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20  onnections] and 
f2d0: 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
f2e0: 65 6e 74 73 5d 20 73 6f 20 74 68 61 74 20 74 68  ents] so that th
f2f0: 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  e.** application
f300: 20 69 73 20 66 72 65 65 20 74 6f 20 75 73 65 20   is free to use 
f310: 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61  the same [databa
f320: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f  se connection] o
f330: 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 5b 70  r the.** same [p
f340: 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
f350: 74 5d 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20  t] in different 
f360: 74 68 72 65 61 64 73 20 61 74 20 74 68 65 20 73  threads at the s
f370: 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 20 53 65 65  ame time..** See
f380: 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20   the [threading 
f390: 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 61 74  mode] documentat
f3a0: 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ion for addition
f3b0: 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c  al information.<
f3c0: 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
f3d0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c  QLITE_CONFIG_MAL
f3e0: 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  LOC</dt>.** <dd>
f3f0: 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65  This option take
f400: 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d  s a single argum
f410: 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70  ent which is a p
f420: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20  ointer to an.** 
f430: 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
f440: 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74  [sqlite3_mem_met
f450: 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e  hods] structure.
f460: 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 73    The argument s
f470: 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6c 74 65  pecifies.** alte
f480: 72 6e 61 74 69 76 65 20 6c 6f 77 2d 6c 65 76 65  rnative low-leve
f490: 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
f4a0: 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74 6f 20  ion routines to 
f4b0: 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  be used in place
f4c0: 20 6f 66 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72   of.** the memor
f4d0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75  y allocation rou
f4e0: 74 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f  tines built into
f4f0: 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a   SQLite.</dd>.**
f500: 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
f510: 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 3c  ONFIG_GETMALLOC<
f520: 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
f530: 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
f540: 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
f550: 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
f560: 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74  er to an.** inst
f570: 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c  ance of the [sql
f580: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
f590: 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  ] structure.  Th
f5a0: 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  e [sqlite3_mem_m
f5b0: 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63  ethods].** struc
f5c0: 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64 20 77  ture is filled w
f5d0: 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 6c  ith the currentl
f5e0: 79 20 64 65 66 69 6e 65 64 20 6d 65 6d 6f 72 79  y defined memory
f5f0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74   allocation rout
f600: 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70  ines..** This op
f610: 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64  tion can be used
f620: 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 65   to overload the
f630: 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20   default memory 
f640: 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f  allocation.** ro
f650: 75 74 69 6e 65 73 20 77 69 74 68 20 61 20 77 72  utines with a wr
f660: 61 70 70 65 72 20 74 68 61 74 20 73 69 6d 75 6c  apper that simul
f670: 61 74 69 6f 6e 73 20 6d 65 6d 6f 72 79 20 61 6c  ations memory al
f680: 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 72 65  location failure
f690: 20 6f 72 0a 2a 2a 20 74 72 61 63 6b 73 20 6d 65   or.** tracks me
f6a0: 6d 6f 72 79 20 75 73 61 67 65 2c 20 66 6f 72 20  mory usage, for 
f6b0: 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a  example.</dd>.**
f6c0: 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
f6d0: 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 3c  ONFIG_MEMSTATUS<
f6e0: 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
f6f0: 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 73 69   option takes si
f700: 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 6f 66  ngle argument of
f710: 20 74 79 70 65 20 69 6e 74 2c 20 69 6e 74 65 72   type int, inter
f720: 70 72 65 74 65 64 20 61 73 20 61 20 0a 2a 2a 20  preted as a .** 
f730: 62 6f 6f 6c 65 61 6e 2c 20 77 68 69 63 68 20 65  boolean, which e
f740: 6e 61 62 6c 65 73 20 6f 72 20 64 69 73 61 62 6c  nables or disabl
f750: 65 73 20 74 68 65 20 63 6f 6c 6c 65 63 74 69 6f  es the collectio
f760: 6e 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  n of memory allo
f770: 63 61 74 69 6f 6e 20 0a 2a 2a 20 73 74 61 74 69  cation .** stati
f780: 73 74 69 63 73 2e 20 57 68 65 6e 20 64 69 73 61  stics. When disa
f790: 62 6c 65 64 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  bled, the follow
f7a0: 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 74 65 72  ing SQLite inter
f7b0: 66 61 63 65 73 20 62 65 63 6f 6d 65 20 0a 2a 2a  faces become .**
f7c0: 20 6e 6f 6e 2d 6f 70 65 72 61 74 69 6f 6e 61 6c   non-operational
f7d0: 3a 0a 2a 2a 20 20 20 3c 75 6c 3e 0a 2a 2a 20 20  :.**   <ul>.**  
f7e0: 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d   <li> [sqlite3_m
f7f0: 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 0a 2a 2a  emory_used()].**
f800: 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33     <li> [sqlite3
f810: 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65  _memory_highwate
f820: 72 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b  r()].**   <li> [
f830: 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61  sqlite3_soft_hea
f840: 70 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 20 20  p_limit()].**   
f850: 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 73 74  <li> [sqlite3_st
f860: 61 74 75 73 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75  atus()].**   </u
f870: 6c 3e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a  l>.** </dd>.**.*
f880: 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
f890: 46 49 47 5f 53 43 52 41 54 43 48 3c 2f 64 74 3e  FIG_SCRATCH</dt>
f8a0: 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
f8b0: 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20  ion specifies a 
f8c0: 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75  static memory bu
f8d0: 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65  ffer that SQLite
f8e0: 20 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20   can use for.** 
f8f0: 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 2e 20  scratch memory. 
f900: 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65   There are three
f910: 20 61 72 67 75 6d 65 6e 74 73 3a 20 20 41 20 70   arguments:  A p
f920: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65  ointer to the me
f930: 6d 6f 72 79 2c 20 74 68 65 0a 2a 2a 20 73 69 7a  mory, the.** siz
f940: 65 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63  e of each scratc
f950: 68 20 62 75 66 66 65 72 20 28 73 7a 29 2c 20 61  h buffer (sz), a
f960: 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nd the number of
f970: 20 62 75 66 66 65 72 73 20 28 4e 29 2e 20 20 54   buffers (N).  T
f980: 68 65 20 73 7a 0a 2a 2a 20 61 72 67 75 6d 65 6e  he sz.** argumen
f990: 74 20 6d 75 73 74 20 62 65 20 61 20 6d 75 6c 74  t must be a mult
f9a0: 69 70 6c 65 20 6f 66 20 31 36 2e 20 54 68 65 20  iple of 16. The 
f9b0: 73 7a 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f  sz parameter sho
f9c0: 75 6c 64 20 62 65 20 61 20 66 65 77 20 62 79 74  uld be a few byt
f9d0: 65 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61  es.** larger tha
f9e0: 6e 20 74 68 65 20 61 63 74 75 61 6c 20 73 63 72  n the actual scr
f9f0: 61 74 63 68 20 73 70 61 63 65 20 72 65 71 75 69  atch space requi
fa00: 72 65 64 20 64 75 65 20 69 6e 74 65 72 6e 61 6c  red due internal
fa10: 20 6f 76 65 72 68 65 61 64 2e 0a 2a 2a 20 54 68   overhead..** Th
fa20: 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d  e first.** argum
fa30: 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74  ent should point
fa40: 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f   to an allocatio
fa50: 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a  n of at least sz
fa60: 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  *N bytes of memo
fa70: 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  ry..** SQLite wi
fa80: 6c 6c 20 75 73 65 20 6e 6f 20 6d 6f 72 65 20 74  ll use no more t
fa90: 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20  han one scratch 
faa0: 62 75 66 66 65 72 20 61 74 20 6f 6e 63 65 20 70  buffer at once p
fab0: 65 72 20 74 68 72 65 61 64 2c 20 73 6f 0a 2a 2a  er thread, so.**
fac0: 20 4e 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74   N should be set
fad0: 20 74 6f 20 74 68 65 20 65 78 70 65 63 74 65 64   to the expected
fae0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
faf0: 6f 66 20 74 68 72 65 61 64 73 2e 20 20 54 68 65  of threads.  The
fb00: 20 73 7a 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72   sz.** parameter
fb10: 20 73 68 6f 75 6c 64 20 62 65 20 36 20 74 69 6d   should be 6 tim
fb20: 65 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  es the size of t
fb30: 68 65 20 6c 61 72 67 65 73 74 20 64 61 74 61 62  he largest datab
fb40: 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a  ase page size..*
fb50: 2a 20 53 63 72 61 74 63 68 20 62 75 66 66 65 72  * Scratch buffer
fb60: 73 20 61 72 65 20 75 73 65 64 20 61 73 20 70 61  s are used as pa
fb70: 72 74 20 6f 66 20 74 68 65 20 62 74 72 65 65 20  rt of the btree 
fb80: 62 61 6c 61 6e 63 65 20 6f 70 65 72 61 74 69 6f  balance operatio
fb90: 6e 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20 62 74  n.  If.** The bt
fba0: 72 65 65 20 62 61 6c 61 6e 63 65 72 20 6e 65 65  ree balancer nee
fbb0: 64 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65  ds additional me
fbc0: 6d 6f 72 79 20 62 65 79 6f 6e 64 20 77 68 61 74  mory beyond what
fbd0: 20 69 73 20 70 72 6f 76 69 64 65 64 20 62 79 0a   is provided by.
fbe0: 2a 2a 20 73 63 72 61 74 63 68 20 62 75 66 66 65  ** scratch buffe
fbf0: 72 73 20 6f 72 20 69 66 20 6e 6f 20 73 63 72 61  rs or if no scra
fc00: 74 63 68 20 62 75 66 66 65 72 20 73 70 61 63 65  tch buffer space
fc10: 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 74   is specified, t
fc20: 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 67 6f  hen SQLite.** go
fc30: 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d  es to [sqlite3_m
fc40: 61 6c 6c 6f 63 28 29 5d 20 74 6f 20 6f 62 74 61  alloc()] to obta
fc50: 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 74  in the memory it
fc60: 20 6e 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a   needs.</dd>.**.
fc70: 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
fc80: 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 3c 2f  NFIG_PAGECACHE</
fc90: 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
fca0: 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 73  option specifies
fcb0: 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 79   a static memory
fcc0: 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51 4c   buffer that SQL
fcd0: 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f 72 0a  ite can use for.
fce0: 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
fcf0: 70 61 67 65 20 63 61 63 68 65 20 77 69 74 68 20  page cache with 
fd00: 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  the default page
fd10: 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 61   cache implemena
fd20: 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54 68 69 73 20  tion.  .** This 
fd30: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 73 68  configuration sh
fd40: 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64  ould not be used
fd50: 20 69 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69   if an applicati
fd60: 6f 6e 2d 64 65 66 69 6e 65 20 70 61 67 65 0a 2a  on-define page.*
fd70: 2a 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  * cache implemen
fd80: 74 61 74 69 6f 6e 20 69 73 20 6c 6f 61 64 65 64  tation is loaded
fd90: 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54   using the SQLIT
fda0: 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20  E_CONFIG_PCACHE 
fdb0: 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65  option..** There
fdc0: 20 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d   are three argum
fdd0: 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74  ents to this opt
fde0: 69 6f 6e 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  ion: A pointer t
fdf0: 6f 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c  o the.** memory,
fe00: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63   the size of eac
fe10: 68 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73  h page buffer (s
fe20: 7a 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62  z), and the numb
fe30: 65 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e  er of pages (N).
fe40: 0a 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d  .** The sz argum
fe50: 65 6e 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f  ent must be a po
fe60: 77 65 72 20 6f 66 20 74 77 6f 20 62 65 74 77 65  wer of two betwe
fe70: 65 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38  en 512 and 32768
fe80: 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20  .  The first.** 
fe90: 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20  argument should 
fea0: 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 6c 6c 6f  point to an allo
feb0: 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61  cation of at lea
fec0: 73 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66  st sz*N bytes of
fed0: 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69   memory..** SQLi
fee0: 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20  te will use the 
fef0: 6d 65 6d 6f 72 79 20 70 72 6f 76 69 64 65 64 20  memory provided 
ff00: 62 79 20 74 68 65 20 66 69 72 73 74 20 61 72 67  by the first arg
ff10: 75 6d 65 6e 74 20 74 6f 20 73 61 74 69 73 66 79  ument to satisfy
ff20: 20 69 74 73 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6e   its.** memory n
ff30: 65 65 64 73 20 66 6f 72 20 74 68 65 20 66 69 72  eeds for the fir
ff40: 73 74 20 4e 20 70 61 67 65 73 20 74 68 61 74 20  st N pages that 
ff50: 69 74 20 61 64 64 73 20 74 6f 20 63 61 63 68 65  it adds to cache
ff60: 2e 20 20 49 66 20 61 64 64 69 74 69 6f 6e 61 6c  .  If additional
ff70: 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 6d  .** page cache m
ff80: 65 6d 6f 72 79 20 69 73 20 6e 65 65 64 65 64 20  emory is needed 
ff90: 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70  beyond what is p
ffa0: 72 6f 76 69 64 65 64 20 62 79 20 74 68 69 73 20  rovided by this 
ffb0: 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20  option, then.** 
ffc0: 53 51 4c 69 74 65 20 67 6f 65 73 20 74 6f 20 5b  SQLite goes to [
ffd0: 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
ffe0: 5d 20 66 6f 72 20 74 68 65 20 61 64 64 69 74 69  ] for the additi
fff0: 6f 6e 61 6c 20 73 74 6f 72 61 67 65 20 73 70 61  onal storage spa
10000 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65  ce..** The imple
10010 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20  mentation might 
10020 75 73 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  use one or more 
10030 6f 66 20 74 68 65 20 4e 20 62 75 66 66 65 72 73  of the N buffers
10040 20 74 6f 20 68 6f 6c 64 20 0a 2a 2a 20 6d 65 6d   to hold .** mem
10050 6f 72 79 20 61 63 63 6f 75 6e 74 69 6e 67 20 69  ory accounting i
10060 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 3c 2f 64 64  nformation. </dd
10070 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
10080 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f  TE_CONFIG_HEAP</
10090 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
100a0 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 73  option specifies
100b0 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 79   a static memory
100c0 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51 4c   buffer that SQL
100d0 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20  ite will use.** 
100e0 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64  for all of its d
100f0 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 61 6c  ynamic memory al
10100 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 20 62  location needs b
10110 65 79 6f 6e 64 20 74 68 6f 73 65 20 70 72 6f 76  eyond those prov
10120 69 64 65 64 0a 2a 2a 20 66 6f 72 20 62 79 20 5b  ided.** for by [
10130 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43  SQLITE_CONFIG_SC
10140 52 41 54 43 48 5d 20 61 6e 64 20 5b 53 51 4c 49  RATCH] and [SQLI
10150 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
10160 43 48 45 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 61  CHE]..** There a
10170 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e  re three argumen
10180 74 73 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  ts: A pointer to
10190 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65   the memory, the
101a0 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79   number of.** by
101b0 74 65 73 20 69 6e 20 74 68 65 20 6d 65 6d 6f 72  tes in the memor
101c0 79 20 62 75 66 66 65 72 2c 20 61 6e 64 20 74 68  y buffer, and th
101d0 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61  e minimum alloca
101e0 74 69 6f 6e 20 73 69 7a 65 2e 20 20 49 66 0a 2a  tion size.  If.*
101f0 2a 20 74 68 65 20 66 69 72 73 74 20 70 6f 69 6e  * the first poin
10200 74 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79 20  ter (the memory 
10210 70 6f 69 6e 74 65 72 29 20 69 73 20 4e 55 4c 4c  pointer) is NULL
10220 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 72 65  , then SQLite re
10230 76 65 72 74 73 0a 2a 2a 20 74 6f 20 75 73 69 6e  verts.** to usin
10240 67 20 69 74 73 20 64 65 66 61 75 6c 74 20 6d 65  g its default me
10250 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 28  mory allocator (
10260 74 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f  the system mallo
10270 63 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  c() implementati
10280 6f 6e 29 2c 0a 2a 2a 20 75 6e 64 6f 69 6e 67 20  on),.** undoing 
10290 61 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63 61  any prior invoca
102a0 74 69 6f 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f  tion of [SQLITE_
102b0 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20  CONFIG_MALLOC]. 
102c0 20 49 66 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72   If the.** memor
102d0 79 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74  y pointer is not
102e0 20 4e 55 4c 4c 20 61 6e 64 20 65 69 74 68 65 72   NULL and either
102f0 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f   [SQLITE_ENABLE_
10300 4d 45 4d 53 59 53 33 5d 20 6f 72 0a 2a 2a 20 5b  MEMSYS3] or.** [
10310 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
10320 4d 53 59 53 35 5d 20 61 72 65 20 64 65 66 69 6e  MSYS5] are defin
10330 65 64 2c 20 74 68 65 6e 20 74 68 65 20 61 6c 74  ed, then the alt
10340 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72 79 0a  ernative memory.
10350 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ** allocator is 
10360 65 6e 67 61 67 65 64 20 74 6f 20 68 61 6e 64 6c  engaged to handl
10370 65 20 61 6c 6c 20 6f 66 20 53 51 4c 69 74 65 73  e all of SQLites
10380 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
10390 6f 6e 20 6e 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a  on needs.</dd>.*
103a0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
103b0 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74  CONFIG_MUTEX</dt
103c0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70  >.** <dd>This op
103d0 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e  tion takes a sin
103e0 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69  gle argument whi
103f0 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ch is a pointer 
10400 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63  to an.** instanc
10410 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  e of the [sqlite
10420 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d  3_mutex_methods]
10430 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
10440 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66   argument specif
10450 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69  ies.** alternati
10460 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74  ve low-level mut
10470 65 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62  ex routines to b
10480 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 0a  e used in place.
10490 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f 75  ** the mutex rou
104a0 74 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f  tines built into
104b0 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a   SQLite.</dd>.**
104c0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
104d0 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f  ONFIG_GETMUTEX</
104e0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
104f0 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73  option takes a s
10500 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77  ingle argument w
10510 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65  hich is a pointe
10520 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61  r to an.** insta
10530 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  nce of the [sqli
10540 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
10550 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  s] structure.  T
10560 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d  he.** [sqlite3_m
10570 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a  utex_methods].**
10580 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69   structure is fi
10590 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75  lled with the cu
105a0 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20  rrently defined 
105b0 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a  mutex routines..
105c0 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63  ** This option c
105d0 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76  an be used to ov
105e0 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75  erload the defau
105f0 6c 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74  lt mutex allocat
10600 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ion.** routines 
10610 77 69 74 68 20 61 20 77 72 61 70 70 65 72 20 75  with a wrapper u
10620 73 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75 74  sed to track mut
10630 65 78 20 75 73 61 67 65 20 66 6f 72 20 70 65 72  ex usage for per
10640 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66  formance.** prof
10650 69 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e 67  iling or testing
10660 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f  , for example.</
10670 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
10680 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  LITE_CONFIG_LOOK
10690 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  ASIDE</dt>.** <d
106a0 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61  d>This option ta
106b0 6b 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74  kes two argument
106c0 73 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65  s that determine
106d0 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20   the default.** 
106e0 6d 65 6d 6f 72 79 20 61 6c 6c 63 61 74 69 6f 6e  memory allcation
106f0 20 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d   lookaside optim
10700 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69  ization.  The fi
10710 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
10720 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65  the.** size of e
10730 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  ach lookaside bu
10740 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68  ffer slot and th
10750 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20  e second is the 
10760 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f  number of.** slo
10770 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  ts allocated to 
10780 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f  each database co
10790 6e 6e 65 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a  nnection.</dd>.*
107a0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
107b0 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 3c 2f 64  CONFIG_PCACHE</d
107c0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
107d0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ption takes a si
107e0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68  ngle argument wh
107f0 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ich is a pointer
10800 20 74 6f 0a 2a 2a 20 61 6e 20 5b 73 71 6c 69 74   to.** an [sqlit
10810 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
10820 73 5d 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73  s] object.  This
10830 20 6f 62 6a 65 63 74 20 73 70 65 63 69 66 69 65   object specifie
10840 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 0a  s the interface.
10850 2a 2a 20 74 6f 20 61 20 63 75 73 74 6f 6d 20 70  ** to a custom p
10860 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d  age cache implem
10870 65 6e 74 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74  entation.  SQLit
10880 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f  e makes a copy o
10890 66 20 74 68 65 0a 2a 2a 20 6f 62 6a 65 63 74 20  f the.** object 
108a0 61 6e 64 20 75 73 65 73 20 69 74 20 66 6f 72 20  and uses it for 
108b0 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72  page cache memor
108c0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 3c 2f  y allocations.</
108d0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
108e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50  LITE_CONFIG_GETP
108f0 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  CACHE</dt>.** <d
10900 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61  d>This option ta
10910 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67  kes a single arg
10920 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61  ument which is a
10930 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a   pointer to an.*
10940 2a 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68  * [sqlite3_pcach
10950 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63  e_methods] objec
10960 74 2e 20 20 53 51 4c 69 74 65 20 63 6f 70 69 65  t.  SQLite copie
10970 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  s of the current
10980 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 69  .** page cache i
10990 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e  mplementation in
109a0 74 6f 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 3c  to that object.<
109b0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e  /dd>.**.** </dl>
109c0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
109d0 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
109e0 54 48 52 45 41 44 20 20 31 20 20 2f 2a 20 6e 69  THREAD  1  /* ni
109f0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  l */.#define SQL
10a00 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49  ITE_CONFIG_MULTI
10a10 54 48 52 45 41 44 20 20 20 32 20 20 2f 2a 20 6e  THREAD   2  /* n
10a20 69 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  il */.#define SQ
10a30 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49  LITE_CONFIG_SERI
10a40 41 4c 49 5a 45 44 20 20 20 20 33 20 20 2f 2a 20  ALIZED    3  /* 
10a50 6e 69 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  nil */.#define S
10a60 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c  QLITE_CONFIG_MAL
10a70 4c 4f 43 20 20 20 20 20 20 20 20 34 20 20 2f 2a  LOC        4  /*
10a80 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
10a90 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65  hods* */.#define
10aa0 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47   SQLITE_CONFIG_G
10ab0 45 54 4d 41 4c 4c 4f 43 20 20 20 20 20 35 20 20  ETMALLOC     5  
10ac0 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  /* sqlite3_mem_m
10ad0 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69  ethods* */.#defi
10ae0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
10af0 5f 53 43 52 41 54 43 48 20 20 20 20 20 20 20 36  _SCRATCH       6
10b00 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20    /* void*, int 
10b10 73 7a 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65  sz, int N */.#de
10b20 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
10b30 49 47 5f 50 41 47 45 43 41 43 48 45 20 20 20 20  IG_PAGECACHE    
10b40 20 37 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e   7  /* void*, in
10b50 74 20 73 7a 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23  t sz, int N */.#
10b60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
10b70 4e 46 49 47 5f 48 45 41 50 20 20 20 20 20 20 20  NFIG_HEAP       
10b80 20 20 20 38 20 20 2f 2a 20 76 6f 69 64 2a 2c 20     8  /* void*, 
10b90 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 6d  int nByte, int m
10ba0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  in */.#define SQ
10bb0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53  LITE_CONFIG_MEMS
10bc0 54 41 54 55 53 20 20 20 20 20 39 20 20 2f 2a 20  TATUS     9  /* 
10bd0 62 6f 6f 6c 65 61 6e 20 2a 2f 0a 23 64 65 66 69  boolean */.#defi
10be0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
10bf0 5f 4d 55 54 45 58 20 20 20 20 20 20 20 20 31 30  _MUTEX        10
10c00 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74    /* sqlite3_mut
10c10 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23  ex_methods* */.#
10c20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
10c30 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 20 20 20  NFIG_GETMUTEX   
10c40 20 20 31 31 20 20 2f 2a 20 73 71 6c 69 74 65 33    11  /* sqlite3
10c50 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 20  _mutex_methods* 
10c60 2a 2f 0a 2f 2a 20 70 72 65 76 69 6f 75 73 6c 79  */./* previously
10c70 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 43   SQLITE_CONFIG_C
10c80 48 55 4e 4b 41 4c 4c 4f 43 20 31 32 20 77 68 69  HUNKALLOC 12 whi
10c90 63 68 20 69 73 20 6e 6f 77 20 75 6e 75 73 65 64  ch is now unused
10ca0 2e 20 2a 2f 20 0a 23 64 65 66 69 6e 65 20 53 51  . */ .#define SQ
10cb0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  LITE_CONFIG_LOOK
10cc0 41 53 49 44 45 20 20 20 20 31 33 20 20 2f 2a 20  ASIDE    13  /* 
10cd0 69 6e 74 20 69 6e 74 20 2a 2f 0a 23 64 65 66 69  int int */.#defi
10ce0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
10cf0 5f 50 43 41 43 48 45 20 20 20 20 20 20 20 31 34  _PCACHE       14
10d00 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61    /* sqlite3_pca
10d10 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a  che_methods* */.
10d20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
10d30 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 20  ONFIG_GETPCACHE 
10d40 20 20 20 31 35 20 20 2f 2a 20 73 71 6c 69 74 65     15  /* sqlite
10d50 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73  3_pcache_methods
10d60 2a 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  * */../*.** CAPI
10d70 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 74  3REF: Configurat
10d80 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30  ion Options {H10
10d90 31 37 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a  170} <S20000>.**
10da0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
10db0 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61  .** These consta
10dc0 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 61 69  nts are the avai
10dd0 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63 6f  lable integer co
10de0 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
10df0 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20  ons that.** can 
10e00 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  be passed as the
10e10 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
10e20 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
10e30 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e  _db_config()] in
10e40 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e  terface..**.** N
10e50 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ew configuration
10e60 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20   options may be 
10e70 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20  added in future 
10e80 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
10e90 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20  te..** Existing 
10ea0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
10eb0 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64  tions might be d
10ec0 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70  iscontinued.  Ap
10ed0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68  plications.** sh
10ee0 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72  ould check the r
10ef0 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
10f00 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66  [sqlite3_db_conf
10f10 69 67 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75  ig()] to make su
10f20 72 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63  re that.** the c
10f30 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65  all worked.  The
10f40 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e   [sqlite3_db_con
10f50 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65  fig()] interface
10f60 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 0a 2a   will return a.*
10f70 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f  * non-zero [erro
10f80 72 20 63 6f 64 65 5d 20 69 66 20 61 20 64 69 73  r code] if a dis
10f90 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73  continued or uns
10fa0 75 70 70 6f 72 74 65 64 20 63 6f 6e 66 69 67 75  upported configu
10fb0 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a  ration option.**
10fc0 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a   is invoked..**.
10fd0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53  ** <dl>.** <dt>S
10fe0 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c  QLITE_DBCONFIG_L
10ff0 4f 4f 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a  OOKASIDE</dt>.**
11000 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
11010 20 74 61 6b 65 73 20 74 68 72 65 65 20 61 64 64   takes three add
11020 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74  itional argument
11030 73 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65  s that determine
11040 20 74 68 65 20 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73   the .** [lookas
11050 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ide memory alloc
11060 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75 72 61 74  ator] configurat
11070 69 6f 6e 20 66 6f 72 20 74 68 65 20 5b 64 61 74  ion for the [dat
11080 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
11090 5d 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  ]..** The first 
110a0 61 72 67 75 6d 65 6e 74 20 28 74 68 65 20 74 68  argument (the th
110b0 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ird parameter to
110c0 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e   [sqlite3_db_con
110d0 66 69 67 28 29 5d 20 69 73 20 61 0a 2a 2a 20 70  fig()] is a.** p
110e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f  ointer to a memo
110f0 72 79 20 62 75 66 66 65 72 20 74 6f 20 75 73 65  ry buffer to use
11100 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20 6d   for lookaside m
11110 65 6d 6f 72 79 2e 20 20 54 68 65 20 66 69 72 73  emory.  The firs
11120 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 61  t.** argument ma
11130 79 20 62 65 20 4e 55 4c 4c 20 69 6e 20 77 68 69  y be NULL in whi
11140 63 68 20 63 61 73 65 20 53 51 4c 69 74 65 20 77  ch case SQLite w
11150 69 6c 6c 20 61 6c 6c 6f 63 61 74 65 20 74 68 65  ill allocate the
11160 20 6c 6f 6f 6b 61 73 69 64 65 0a 2a 2a 20 62 75   lookaside.** bu
11170 66 66 65 72 20 69 74 73 65 6c 66 20 75 73 69 6e  ffer itself usin
11180 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  g [sqlite3_mallo
11190 63 28 29 5d 2e 20 20 54 68 65 20 73 65 63 6f 6e  c()].  The secon
111a0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
111b0 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63  e.** size of eac
111c0 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66  h lookaside buff
111d0 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20  er slot and the 
111e0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69  third argument i
111f0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  s the number of.
11200 2a 2a 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 73  ** slots.  The s
11210 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65  ize of the buffe
11220 72 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  r in the first a
11230 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20  rgument must be 
11240 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20  greater than.** 
11250 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
11260 70 72 6f 64 75 63 74 20 6f 66 20 74 68 65 20 73  product of the s
11270 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20  econd and third 
11280 61 72 67 75 6d 65 6e 74 73 2e 3c 2f 64 64 3e 0a  arguments.</dd>.
11290 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23  **.** </dl>.*/.#
112a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42  define SQLITE_DB
112b0 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
112c0 20 20 20 20 31 30 30 31 20 20 2f 2a 20 76 6f 69      1001  /* voi
112d0 64 2a 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a  d* int int */...
112e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
112f0 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c  Enable Or Disabl
11300 65 20 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c  e Extended Resul
11310 74 20 43 6f 64 65 73 20 7b 48 31 32 32 30 30 7d  t Codes {H12200}
11320 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
11330 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65  The sqlite3_exte
11340 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65  nded_result_code
11350 73 28 29 20 72 6f 75 74 69 6e 65 20 65 6e 61 62  s() routine enab
11360 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20  les or disables 
11370 74 68 65 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64  the.** [extended
11380 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66   result codes] f
11390 65 61 74 75 72 65 20 6f 66 20 53 51 4c 69 74 65  eature of SQLite
113a0 2e 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 72  . The extended r
113b0 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 73 20 61  esult.** codes a
113c0 72 65 20 64 69 73 61 62 6c 65 64 20 62 79 20 64  re disabled by d
113d0 65 66 61 75 6c 74 20 66 6f 72 20 68 69 73 74 6f  efault for histo
113e0 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c  rical compatibil
113f0 69 74 79 20 63 6f 6e 73 69 64 65 72 61 74 69 6f  ity consideratio
11400 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ns..**.** Requir
11410 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
11420 30 31 5d 20 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a  01] [H12202].*/.
11430 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
11440 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
11450 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c  result_codes(sql
11460 69 74 65 33 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66  ite3*, int onoff
11470 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
11480 45 46 3a 20 4c 61 73 74 20 49 6e 73 65 72 74 20  EF: Last Insert 
11490 52 6f 77 69 64 20 7b 48 31 32 32 32 30 7d 20 3c  Rowid {H12220} <
114a0 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61  S10700>.**.** Ea
114b0 63 68 20 65 6e 74 72 79 20 69 6e 20 61 6e 20 53  ch entry in an S
114c0 51 4c 69 74 65 20 74 61 62 6c 65 20 68 61 73 20  QLite table has 
114d0 61 20 75 6e 69 71 75 65 20 36 34 2d 62 69 74 20  a unique 64-bit 
114e0 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65  signed.** intege
114f0 72 20 6b 65 79 20 63 61 6c 6c 65 64 20 74 68 65  r key called the
11500 20 5b 52 4f 57 49 44 20 7c 20 22 72 6f 77 69 64   [ROWID | "rowid
11510 22 5d 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73  "]. The rowid is
11520 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c   always availabl
11530 65 0a 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63  e.** as an undec
11540 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  lared column nam
11550 65 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f  ed ROWID, OID, o
11560 72 20 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e  r _ROWID_ as lon
11570 67 20 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61  g as those.** na
11580 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f  mes are not also
11590 20 75 73 65 64 20 62 79 20 65 78 70 6c 69 63 69   used by explici
115a0 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c  tly declared col
115b0 75 6d 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20  umns. If.** the 
115c0 74 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75  table has a colu
115d0 6d 6e 20 6f 66 20 74 79 70 65 20 5b 49 4e 54 45  mn of type [INTE
115e0 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d  GER PRIMARY KEY]
115f0 20 74 68 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d   then that colum
11600 6e 0a 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20  n.** is another 
11610 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f  alias for the ro
11620 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  wid..**.** This 
11630 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
11640 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74  the [rowid] of t
11650 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a  he most recent.*
11660 2a 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e  * successful [IN
11670 53 45 52 54 5d 20 69 6e 74 6f 20 74 68 65 20 64  SERT] into the d
11680 61 74 61 62 61 73 65 20 66 72 6f 6d 20 74 68 65  atabase from the
11690 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
116a0 63 74 69 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65  ction].** in the
116b0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
116c0 20 20 49 66 20 6e 6f 20 73 75 63 63 65 73 73 66    If no successf
116d0 75 6c 20 5b 49 4e 53 45 52 54 5d 73 0a 2a 2a 20  ul [INSERT]s.** 
116e0 68 61 76 65 20 65 76 65 72 20 6f 63 63 75 72 72  have ever occurr
116f0 65 64 20 6f 6e 20 74 68 61 74 20 64 61 74 61 62  ed on that datab
11700 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  ase connection, 
11710 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  zero is returned
11720 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 5b 49  ..**.** If an [I
11730 4e 53 45 52 54 5d 20 6f 63 63 75 72 73 20 77 69  NSERT] occurs wi
11740 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2c 20  thin a trigger, 
11750 74 68 65 6e 20 74 68 65 20 5b 72 6f 77 69 64 5d  then the [rowid]
11760 20 6f 66 20 74 68 65 20 69 6e 73 65 72 74 65 64   of the inserted
11770 0a 2a 2a 20 72 6f 77 20 69 73 20 72 65 74 75 72  .** row is retur
11780 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ned by this rout
11790 69 6e 65 20 61 73 20 6c 6f 6e 67 20 61 73 20 74  ine as long as t
117a0 68 65 20 74 72 69 67 67 65 72 20 69 73 20 72 75  he trigger is ru
117b0 6e 6e 69 6e 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e  nning..** But on
117c0 63 65 20 74 68 65 20 74 72 69 67 67 65 72 20 74  ce the trigger t
117d0 65 72 6d 69 6e 61 74 65 73 2c 20 74 68 65 20 76  erminates, the v
117e0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
117f0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
11800 20 72 65 76 65 72 74 73 20 74 6f 20 74 68 65 20   reverts to the 
11810 6c 61 73 74 20 76 61 6c 75 65 20 69 6e 73 65 72  last value inser
11820 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 74  ted before the t
11830 72 69 67 67 65 72 20 66 69 72 65 64 2e 0a 2a 2a  rigger fired..**
11840 0a 2a 2a 20 41 6e 20 5b 49 4e 53 45 52 54 5d 20  .** An [INSERT] 
11850 74 68 61 74 20 66 61 69 6c 73 20 64 75 65 20 74  that fails due t
11860 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76  o a constraint v
11870 69 6f 6c 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20  iolation is not 
11880 61 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20  a.** successful 
11890 5b 49 4e 53 45 52 54 5d 20 61 6e 64 20 64 6f 65  [INSERT] and doe
118a0 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  s not change the
118b0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
118c0 62 79 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  by this.** routi
118d0 6e 65 2e 20 20 54 68 75 73 20 49 4e 53 45 52 54  ne.  Thus INSERT
118e0 20 4f 52 20 46 41 49 4c 2c 20 49 4e 53 45 52 54   OR FAIL, INSERT
118f0 20 4f 52 20 49 47 4e 4f 52 45 2c 20 49 4e 53 45   OR IGNORE, INSE
11900 52 54 20 4f 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a  RT OR ROLLBACK,.
11910 2a 2a 20 61 6e 64 20 49 4e 53 45 52 54 20 4f 52  ** and INSERT OR
11920 20 41 42 4f 52 54 20 6d 61 6b 65 20 6e 6f 20 63   ABORT make no c
11930 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 72 65  hanges to the re
11940 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
11950 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 68  is.** routine wh
11960 65 6e 20 74 68 65 69 72 20 69 6e 73 65 72 74 69  en their inserti
11970 6f 6e 20 66 61 69 6c 73 2e 20 20 57 68 65 6e 20  on fails.  When 
11980 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
11990 45 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 73 20  E.** encounters 
119a0 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  a constraint vio
119b0 6c 61 74 69 6f 6e 2c 20 69 74 20 64 6f 65 73 20  lation, it does 
119c0 6e 6f 74 20 66 61 69 6c 2e 20 20 54 68 65 0a 2a  not fail.  The.*
119d0 2a 20 49 4e 53 45 52 54 20 63 6f 6e 74 69 6e 75  * INSERT continu
119e0 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e  es to completion
119f0 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20   after deleting 
11a00 72 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64  rows that caused
11a10 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69  .** the constrai
11a20 6e 74 20 70 72 6f 62 6c 65 6d 20 73 6f 20 49 4e  nt problem so IN
11a30 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20  SERT OR REPLACE 
11a40 77 69 6c 6c 20 61 6c 77 61 79 73 20 63 68 61 6e  will always chan
11a50 67 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e  ge.** the return
11a60 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 69   value of this i
11a70 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
11a80 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
11a90 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
11aa0 2c 20 61 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73  , an [INSERT] is
11ab0 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a   considered to.*
11ac0 2a 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 20  * be successful 
11ad0 65 76 65 6e 20 69 66 20 69 74 20 69 73 20 73 75  even if it is su
11ae0 62 73 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65  bsequently rolle
11af0 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65  d back..**.** Re
11b00 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
11b10 48 31 32 32 32 31 5d 20 5b 48 31 32 32 32 33 5d  H12221] [H12223]
11b20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61  .**.** If a sepa
11b30 72 61 74 65 20 74 68 72 65 61 64 20 70 65 72 66  rate thread perf
11b40 6f 72 6d 73 20 61 20 6e 65 77 20 5b 49 4e 53 45  orms a new [INSE
11b50 52 54 5d 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a  RT] on the same.
11b60 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
11b70 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 65  ection while the
11b80 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69   [sqlite3_last_i
11b90 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 0a 2a  nsert_rowid()].*
11ba0 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75  * function is ru
11bb0 6e 6e 69 6e 67 20 61 6e 64 20 74 68 75 73 20 63  nning and thus c
11bc0 68 61 6e 67 65 73 20 74 68 65 20 6c 61 73 74 20  hanges the last 
11bd0 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d 2c 0a  insert [rowid],.
11be0 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  ** then the valu
11bf0 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73  e returned by [s
11c00 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
11c10 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 73 0a 2a  rt_rowid()] is.*
11c20 2a 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20  * unpredictable 
11c30 61 6e 64 20 6d 69 67 68 74 20 6e 6f 74 20 65 71  and might not eq
11c40 75 61 6c 20 65 69 74 68 65 72 20 74 68 65 20 6f  ual either the o
11c50 6c 64 20 6f 72 20 74 68 65 20 6e 65 77 0a 2a 2a  ld or the new.**
11c60 20 6c 61 73 74 20 69 6e 73 65 72 74 20 5b 72 6f   last insert [ro
11c70 77 69 64 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  wid]..*/.SQLITE_
11c80 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
11c90 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  4 sqlite3_last_i
11ca0 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69  nsert_rowid(sqli
11cb0 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  te3*);../*.** CA
11cc0 50 49 33 52 45 46 3a 20 43 6f 75 6e 74 20 54 68  PI3REF: Count Th
11cd0 65 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 73  e Number Of Rows
11ce0 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32 34   Modified {H1224
11cf0 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a  0} <S10600>.**.*
11d00 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
11d10 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
11d20 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 20 72  er of database r
11d30 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63 68  ows that were ch
11d40 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69 6e 73 65  anged.** or inse
11d50 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20  rted or deleted 
11d60 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  by the most rece
11d70 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 53  ntly completed S
11d80 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  QL statement.** 
11d90 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  on the [database
11da0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65   connection] spe
11db0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69  cified by the fi
11dc0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  rst parameter..*
11dd0 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 73 20 74  * Only changes t
11de0 68 61 74 20 61 72 65 20 64 69 72 65 63 74 6c 79  hat are directly
11df0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
11e00 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 50 44  e [INSERT], [UPD
11e10 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c  ATE],.** or [DEL
11e20 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 20 61  ETE] statement a
11e30 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 41 75 78  re counted.  Aux
11e40 69 6c 69 61 72 79 20 63 68 61 6e 67 65 73 20 63  iliary changes c
11e50 61 75 73 65 64 20 62 79 0a 2a 2a 20 74 72 69 67  aused by.** trig
11e60 67 65 72 73 20 61 72 65 20 6e 6f 74 20 63 6f 75  gers are not cou
11e70 6e 74 65 64 2e 20 55 73 65 20 74 68 65 20 5b 73  nted. Use the [s
11e80 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
11e90 6e 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f 6e  nges()] function
11ea0 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65 20  .** to find the 
11eb0 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
11ec0 63 68 61 6e 67 65 73 20 69 6e 63 6c 75 64 69 6e  changes includin
11ed0 67 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64  g changes caused
11ee0 20 62 79 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a   by triggers..**
11ef0 0a 2a 2a 20 43 68 61 6e 67 65 73 20 74 6f 20 61  .** Changes to a
11f00 20 76 69 65 77 20 74 68 61 74 20 61 72 65 20 73   view that are s
11f10 69 6d 75 6c 61 74 65 64 20 62 79 20 61 6e 20 5b  imulated by an [
11f20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
11f30 65 72 5d 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 63  er].** are not c
11f40 6f 75 6e 74 65 64 2e 20 20 4f 6e 6c 79 20 72 65  ounted.  Only re
11f50 61 6c 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73  al table changes
11f60 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a   are counted..**
11f70 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61 6e 67  .** A "row chang
11f80 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 20 74  e" is a change t
11f90 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f  o a single row o
11fa0 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  f a single table
11fb0 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 61 6e  .** caused by an
11fc0 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 2c   INSERT, DELETE,
11fd0 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65   or UPDATE state
11fe0 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 61 74  ment.  Rows that
11ff0 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 64 20  .** are changed 
12000 61 73 20 73 69 64 65 20 65 66 66 65 63 74 73 20  as side effects 
12010 6f 66 20 5b 52 45 50 4c 41 43 45 5d 20 63 6f 6e  of [REPLACE] con
12020 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69  straint resoluti
12030 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c  on,.** rollback,
12040 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e   ABORT processin
12050 67 2c 20 5b 44 52 4f 50 20 54 41 42 4c 45 5d 2c  g, [DROP TABLE],
12060 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 65 72   or by any other
12070 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73 20 64  .** mechanisms d
12080 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 64  o not count as d
12090 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65  irect row change
120a0 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72 69 67  s..**.** A "trig
120b0 67 65 72 20 63 6f 6e 74 65 78 74 22 20 69 73 20  ger context" is 
120c0 61 20 73 63 6f 70 65 20 6f 66 20 65 78 65 63 75  a scope of execu
120d0 74 69 6f 6e 20 74 68 61 74 20 62 65 67 69 6e 73  tion that begins
120e0 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 77 69 74   and.** ends wit
120f0 68 20 74 68 65 20 73 63 72 69 70 74 20 6f 66 20  h the script of 
12100 61 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45  a [CREATE TRIGGE
12110 52 20 7c 20 74 72 69 67 67 65 72 5d 2e 20 0a 2a  R | trigger]. .*
12120 2a 20 4d 6f 73 74 20 53 51 4c 20 73 74 61 74 65  * Most SQL state
12130 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 65 76 61  ments are.** eva
12140 6c 75 61 74 65 64 20 6f 75 74 73 69 64 65 20 6f  luated outside o
12150 66 20 61 6e 79 20 74 72 69 67 67 65 72 2e 20 20  f any trigger.  
12160 54 68 69 73 20 69 73 20 74 68 65 20 22 74 6f 70  This is the "top
12170 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72 69 67 67   level".** trigg
12180 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 49 66 20  er context.  If 
12190 61 20 74 72 69 67 67 65 72 20 66 69 72 65 73 20  a trigger fires 
121a0 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65 76  from the top lev
121b0 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20 74 72 69  el, a.** new tri
121c0 67 67 65 72 20 63 6f 6e 74 65 78 74 20 69 73 20  gger context is 
121d0 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65 20  entered for the 
121e0 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 61 74  duration of that
121f0 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67 65 72 2e   one.** trigger.
12200 20 20 53 75 62 74 72 69 67 67 65 72 73 20 63 72    Subtriggers cr
12210 65 61 74 65 20 73 75 62 63 6f 6e 74 65 78 74 73  eate subcontexts
12220 20 66 6f 72 20 74 68 65 69 72 20 64 75 72 61 74   for their durat
12230 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69  ion..**.** Calli
12240 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  ng [sqlite3_exec
12250 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
12260 73 74 65 70 28 29 5d 20 72 65 63 75 72 73 69 76  step()] recursiv
12270 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20  ely does.** not 
12280 63 72 65 61 74 65 20 61 20 6e 65 77 20 74 72 69  create a new tri
12290 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a  gger context..**
122a0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
122b0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  n returns the nu
122c0 6d 62 65 72 20 6f 66 20 64 69 72 65 63 74 20 72  mber of direct r
122d0 6f 77 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  ow changes in th
122e0 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74  e.** most recent
122f0 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
12300 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65   or DELETE state
12310 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 65 20  ment within the 
12320 73 61 6d 65 0a 2a 2a 20 74 72 69 67 67 65 72 20  same.** trigger 
12330 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54  context..**.** T
12340 68 75 73 2c 20 77 68 65 6e 20 63 61 6c 6c 65 64  hus, when called
12350 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65   from the top le
12360 76 65 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69  vel, this functi
12370 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a  on returns the.*
12380 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e  * number of chan
12390 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20  ges in the most 
123a0 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55  recent INSERT, U
123b0 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45  PDATE, or DELETE
123c0 0a 2a 2a 20 74 68 61 74 20 61 6c 73 6f 20 6f 63  .** that also oc
123d0 63 75 72 72 65 64 20 61 74 20 74 68 65 20 74 6f  curred at the to
123e0 70 20 6c 65 76 65 6c 2e 20 20 57 69 74 68 69 6e  p level.  Within
123f0 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 74   the body of a t
12400 72 69 67 67 65 72 2c 0a 2a 2a 20 74 68 65 20 73  rigger,.** the s
12410 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29  qlite3_changes()
12420 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62   interface can b
12430 65 20 63 61 6c 6c 65 64 20 74 6f 20 66 69 6e 64  e called to find
12440 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
12450 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65  * changes in the
12460 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63   most recently c
12470 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52 54 2c  ompleted INSERT,
12480 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45   UPDATE, or DELE
12490 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  TE.** statement 
124a0 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20  within the body 
124b0 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72 69 67  of the same trig
124c0 67 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  ger..** However,
124d0 20 74 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75   the number retu
124e0 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e  rned does not in
124f0 63 6c 75 64 65 20 63 68 61 6e 67 65 73 0a 2a 2a  clude changes.**
12500 20 63 61 75 73 65 64 20 62 79 20 73 75 62 74 72   caused by subtr
12510 69 67 67 65 72 73 20 73 69 6e 63 65 20 74 68 6f  iggers since tho
12520 73 65 20 68 61 76 65 20 74 68 65 69 72 20 6f 77  se have their ow
12530 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a  n context..**.**
12540 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73   See also the [s
12550 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
12560 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63  nges()] interfac
12570 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b 63 6f  e and the.** [co
12580 75 6e 74 5f 63 68 61 6e 67 65 73 20 70 72 61 67  unt_changes prag
12590 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ma]..**.** Requi
125a0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
125b0 32 34 31 5d 20 5b 48 31 32 32 34 33 5d 0a 2a 2a  241] [H12243].**
125c0 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74  .** If a separat
125d0 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63  e thread makes c
125e0 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61  hanges on the sa
125f0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
12600 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20  ection.** while 
12610 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73  [sqlite3_changes
12620 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74  ()] is running t
12630 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
12640 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 75 6e 70  turned.** is unp
12650 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e  redictable and n
12660 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a  ot meaningful..*
12670 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
12680 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
12690 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
126a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 6f 74  ** CAPI3REF: Tot
126b0 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77  al Number Of Row
126c0 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32  s Modified {H122
126d0 36 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a  60} <S10600>.**.
126e0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
126f0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
12700 62 65 72 20 6f 66 20 72 6f 77 20 63 68 61 6e 67  ber of row chang
12710 65 73 20 63 61 75 73 65 64 20 62 79 20 5b 49 4e  es caused by [IN
12720 53 45 52 54 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54  SERT],.** [UPDAT
12730 45 5d 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73  E] or [DELETE] s
12740 74 61 74 65 6d 65 6e 74 73 20 73 69 6e 63 65 20  tatements since 
12750 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
12760 6e 6e 65 63 74 69 6f 6e 5d 20 77 61 73 20 6f 70  nnection] was op
12770 65 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 63 6f 75  ened..** The cou
12780 6e 74 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20  nt includes all 
12790 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 61 6c 6c  changes from all
127a0 20 0a 2a 2a 20 5b 43 52 45 41 54 45 20 54 52 49   .** [CREATE TRI
127b0 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d 20  GGER | trigger] 
127c0 63 6f 6e 74 65 78 74 73 2e 20 20 48 6f 77 65 76  contexts.  Howev
127d0 65 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74  er,.** the count
127e0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
127f0 65 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74  e changes used t
12800 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50  o implement [REP
12810 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74  LACE] constraint
12820 73 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63  s,.** do rollbac
12830 6b 73 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63  ks or ABORT proc
12840 65 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50  essing, or [DROP
12850 20 54 41 42 4c 45 5d 20 70 72 6f 63 65 73 73 69   TABLE] processi
12860 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 75 6e  ng.  The.** coun
12870 74 20 64 6f 65 73 20 6e 6f 74 20 72 6f 77 73 20  t does not rows 
12880 6f 66 20 76 69 65 77 73 20 74 68 61 74 20 66 69  of views that fi
12890 72 65 20 61 6e 20 5b 49 4e 53 54 45 41 44 20 4f  re an [INSTEAD O
128a0 46 20 74 72 69 67 67 65 72 5d 2c 20 74 68 6f 75  F trigger], thou
128b0 67 68 20 69 66 0a 2a 2a 20 74 68 65 20 49 4e 53  gh if.** the INS
128c0 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 20  TEAD OF trigger 
128d0 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 66  makes changes of
128e0 20 69 74 73 20 6f 77 6e 2c 20 74 68 6f 73 65 20   its own, those 
128f0 63 68 61 6e 67 65 73 20 61 72 65 0a 2a 2a 20 63  changes are.** c
12900 6f 75 6e 74 65 64 2e 0a 2a 2a 20 54 68 65 20 63  ounted..** The c
12910 68 61 6e 67 65 73 20 61 72 65 20 63 6f 75 6e 74  hanges are count
12920 65 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68  ed as soon as th
12930 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  e statement that
12940 20 6d 61 6b 65 73 20 74 68 65 6d 20 69 73 0a 2a   makes them is.*
12950 2a 20 63 6f 6d 70 6c 65 74 65 64 20 28 77 68 65  * completed (whe
12960 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  n the statement 
12970 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 65 64  handle is passed
12980 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73   to [sqlite3_res
12990 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c  et()] or.** [sql
129a0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
129b0 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  )..**.** See als
129c0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  o the [sqlite3_c
129d0 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66  hanges()] interf
129e0 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b  ace and the.** [
129f0 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20 70 72  count_changes pr
12a00 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  agma]..**.** Req
12a10 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
12a20 31 32 32 36 31 5d 20 5b 48 31 32 32 36 33 5d 0a  12261] [H12263].
12a30 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72  **.** If a separ
12a40 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73  ate thread makes
12a50 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20   changes on the 
12a60 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
12a70 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c  nnection.** whil
12a80 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  e [sqlite3_total
12a90 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73 20 72  _changes()] is r
12aa0 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68 65 20  unning then the 
12ab0 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65  value.** returne
12ac0 64 20 69 73 20 75 6e 70 72 65 64 69 63 74 61 62  d is unpredictab
12ad0 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69  le and not meani
12ae0 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ngful..*/.SQLITE
12af0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
12b00 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 73  _total_changes(s
12b10 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  qlite3*);../*.**
12b20 20 43 41 50 49 33 52 45 46 3a 20 49 6e 74 65 72   CAPI3REF: Inter
12b30 72 75 70 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e  rupt A Long-Runn
12b40 69 6e 67 20 51 75 65 72 79 20 7b 48 31 32 32 37  ing Query {H1227
12b50 30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a  0} <S30500>.**.*
12b60 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
12b70 63 61 75 73 65 73 20 61 6e 79 20 70 65 6e 64 69  causes any pendi
12b80 6e 67 20 64 61 74 61 62 61 73 65 20 6f 70 65 72  ng database oper
12b90 61 74 69 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61  ation to abort a
12ba0 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 61 74 20  nd.** return at 
12bb0 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70  its earliest opp
12bc0 6f 72 74 75 6e 69 74 79 2e 20 54 68 69 73 20 72  ortunity. This r
12bd0 6f 75 74 69 6e 65 20 69 73 20 74 79 70 69 63 61  outine is typica
12be0 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e  lly.** called in
12bf0 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 75   response to a u
12c00 73 65 72 20 61 63 74 69 6f 6e 20 73 75 63 68 20  ser action such 
12c10 61 73 20 70 72 65 73 73 69 6e 67 20 22 43 61 6e  as pressing "Can
12c20 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d  cel".** or Ctrl-
12c30 43 20 77 68 65 72 65 20 74 68 65 20 75 73 65 72  C where the user
12c40 20 77 61 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75   wants a long qu
12c50 65 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f  ery operation to
12c60 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61   halt.** immedia
12c70 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  tely..**.** It i
12c80 73 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74  s safe to call t
12c90 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d  his routine from
12ca0 20 61 20 74 68 72 65 61 64 20 64 69 66 66 65 72   a thread differ
12cb0 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ent from the.** 
12cc0 74 68 72 65 61 64 20 74 68 61 74 20 69 73 20 63  thread that is c
12cd0 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67  urrently running
12ce0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 70   the database op
12cf0 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 74  eration.  But it
12d00 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 61 66 65 20  .** is not safe 
12d10 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75  to call this rou
12d20 74 69 6e 65 20 77 69 74 68 20 61 20 5b 64 61 74  tine with a [dat
12d30 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
12d40 5d 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f  ] that.** is clo
12d50 73 65 64 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f  sed or might clo
12d60 73 65 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65  se before sqlite
12d70 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 65  3_interrupt() re
12d80 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  turns..**.** If 
12d90 61 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e  an SQL operation
12da0 20 69 73 20 76 65 72 79 20 6e 65 61 72 6c 79 20   is very nearly 
12db0 66 69 6e 69 73 68 65 64 20 61 74 20 74 68 65 20  finished at the 
12dc0 74 69 6d 65 20 77 68 65 6e 0a 2a 2a 20 73 71 6c  time when.** sql
12dd0 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
12de0 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e   is called, then
12df0 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61   it might not ha
12e00 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74  ve an opportunit
12e10 79 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72  y.** to be inter
12e20 72 75 70 74 65 64 20 61 6e 64 20 6d 69 67 68 74  rupted and might
12e30 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d   continue to com
12e40 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41  pletion..**.** A
12e50 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20  n SQL operation 
12e60 74 68 61 74 20 69 73 20 69 6e 74 65 72 72 75 70  that is interrup
12e70 74 65 64 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ted will return 
12e80 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50  [SQLITE_INTERRUP
12e90 54 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  T]..** If the in
12ea0 74 65 72 72 75 70 74 65 64 20 53 51 4c 20 6f 70  terrupted SQL op
12eb0 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e  eration is an IN
12ec0 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72  SERT, UPDATE, or
12ed0 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20   DELETE.** that 
12ee0 69 73 20 69 6e 73 69 64 65 20 61 6e 20 65 78 70  is inside an exp
12ef0 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f  licit transactio
12f00 6e 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69  n, then the enti
12f10 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a  re transaction.*
12f20 2a 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64  * will be rolled
12f30 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61   back automatica
12f40 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  lly..**.** The s
12f50 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
12f60 28 44 29 20 63 61 6c 6c 20 69 73 20 69 6e 20 65  (D) call is in e
12f70 66 66 65 63 74 20 75 6e 74 69 6c 20 61 6c 6c 20  ffect until all 
12f80 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e  currently runnin
12f90 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65  g.** SQL stateme
12fa0 6e 74 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65  nts on [database
12fb0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 63   connection] D c
12fc0 6f 6d 70 6c 65 74 65 2e 20 20 41 6e 79 20 6e 65  omplete.  Any ne
12fd0 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  w SQL statements
12fe0 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61  .** that are sta
12ff0 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20 73  rted after the s
13000 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
13010 28 29 20 63 61 6c 6c 20 61 6e 64 20 62 65 66 6f  () call and befo
13020 72 65 20 74 68 65 20 0a 2a 2a 20 72 75 6e 6e 69  re the .** runni
13030 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20 72 65  ng statements re
13040 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65 20 69  aches zero are i
13050 6e 74 65 72 72 75 70 74 65 64 20 61 73 20 69 66  nterrupted as if
13060 20 74 68 65 79 20 68 61 64 20 62 65 65 6e 0a 2a   they had been.*
13070 2a 20 72 75 6e 6e 69 6e 67 20 70 72 69 6f 72 20  * running prior 
13080 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  to the sqlite3_i
13090 6e 74 65 72 72 75 70 74 28 29 20 63 61 6c 6c 2e  nterrupt() call.
130a0 20 20 4e 65 77 20 53 51 4c 20 73 74 61 74 65 6d    New SQL statem
130b0 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65  ents.** that are
130c0 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74   started after t
130d0 68 65 20 72 75 6e 6e 69 6e 67 20 73 74 61 74 65  he running state
130e0 6d 65 6e 74 20 63 6f 75 6e 74 20 72 65 61 63 68  ment count reach
130f0 65 73 20 7a 65 72 6f 20 61 72 65 0a 2a 2a 20 6e  es zero are.** n
13100 6f 74 20 65 66 66 65 63 74 65 64 20 62 79 20 74  ot effected by t
13110 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  he sqlite3_inter
13120 72 75 70 74 28 29 2e 0a 2a 2a 20 41 20 63 61 6c  rupt()..** A cal
13130 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74  l to sqlite3_int
13140 65 72 72 75 70 74 28 44 29 20 74 68 61 74 20 6f  errupt(D) that o
13150 63 63 75 72 73 20 77 68 65 6e 20 74 68 65 72 65  ccurs when there
13160 20 61 72 65 20 6e 6f 20 72 75 6e 6e 69 6e 67 0a   are no running.
13170 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ** SQL statement
13180 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64  s is a no-op and
13190 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f   has no effect o
131a0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  n SQL statements
131b0 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61  .** that are sta
131c0 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20 73  rted after the s
131d0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
131e0 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e  () call returns.
131f0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
13200 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 37 31 5d  nts:.** [H12271]
13210 20 5b 48 31 32 32 37 32 5d 0a 2a 2a 0a 2a 2a 20   [H12272].**.** 
13220 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
13230 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6c 6f 73 65  connection close
13240 73 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33  s while [sqlite3
13250 5f 69 6e 74 65 72 72 75 70 74 28 29 5d 0a 2a 2a  _interrupt()].**
13260 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e   is running then
13270 20 62 61 64 20 74 68 69 6e 67 73 20 77 69 6c 6c   bad things will
13280 20 6c 69 6b 65 6c 79 20 68 61 70 70 65 6e 2e 0a   likely happen..
13290 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
132a0 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  id sqlite3_inter
132b0 72 75 70 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a  rupt(sqlite3*);.
132c0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
132d0 20 44 65 74 65 72 6d 69 6e 65 20 49 66 20 41 6e   Determine If An
132e0 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 49   SQL Statement I
132f0 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48 31 30 35  s Complete {H105
13300 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a 2a 2a 0a  10} <S70200>.**.
13310 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
13320 73 20 61 72 65 20 75 73 65 66 75 6c 20 64 75 72  s are useful dur
13330 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65  ing command-line
13340 20 69 6e 70 75 74 20 74 6f 20 64 65 74 65 72 6d   input to determ
13350 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a 20 63 75  ine if the.** cu
13360 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
13370 74 65 78 74 20 73 65 65 6d 73 20 74 6f 20 66 6f  text seems to fo
13380 72 6d 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51  rm a complete SQ
13390 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a  L statement or.*
133a0 2a 20 69 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  * if additional 
133b0 69 6e 70 75 74 20 69 73 20 6e 65 65 64 65 64 20  input is needed 
133c0 62 65 66 6f 72 65 20 73 65 6e 64 69 6e 67 20 74  before sending t
133d0 68 65 20 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20  he text into.** 
133e0 53 51 4c 69 74 65 20 66 6f 72 20 70 61 72 73 69  SQLite for parsi
133f0 6e 67 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69  ng.  These routi
13400 6e 65 73 20 72 65 74 75 72 6e 20 31 20 69 66 20  nes return 1 if 
13410 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
13420 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 6f 20 62  .** appears to b
13430 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c  e a complete SQL
13440 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 73   statement.  A s
13450 74 61 74 65 6d 65 6e 74 20 69 73 20 6a 75 64 67  tatement is judg
13460 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70  ed to be.** comp
13470 6c 65 74 65 20 69 66 20 69 74 20 65 6e 64 73 20  lete if it ends 
13480 77 69 74 68 20 61 20 73 65 6d 69 63 6f 6c 6f 6e  with a semicolon
13490 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 20 6e 6f   token and is no
134a0 74 20 61 20 70 72 65 66 69 78 20 6f 66 20 61 0a  t a prefix of a.
134b0 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 43  ** well-formed C
134c0 52 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74  REATE TRIGGER st
134d0 61 74 65 6d 65 6e 74 2e 20 20 53 65 6d 69 63 6f  atement.  Semico
134e0 6c 6f 6e 73 20 74 68 61 74 20 61 72 65 20 65 6d  lons that are em
134f0 62 65 64 64 65 64 20 77 69 74 68 69 6e 0a 2a 2a  bedded within.**
13500 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 73   string literals
13510 20 6f 72 20 71 75 6f 74 65 64 20 69 64 65 6e 74   or quoted ident
13520 69 66 69 65 72 20 6e 61 6d 65 73 20 6f 72 20 63  ifier names or c
13530 6f 6d 6d 65 6e 74 73 20 61 72 65 20 6e 6f 74 0a  omments are not.
13540 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 74  ** independent t
13550 6f 6b 65 6e 73 20 28 74 68 65 79 20 61 72 65 20  okens (they are 
13560 70 61 72 74 20 6f 66 20 74 68 65 20 74 6f 6b 65  part of the toke
13570 6e 20 69 6e 20 77 68 69 63 68 20 74 68 65 79 20  n in which they 
13580 61 72 65 0a 2a 2a 20 65 6d 62 65 64 64 65 64 29  are.** embedded)
13590 20 61 6e 64 20 74 68 75 73 20 64 6f 20 6e 6f 74   and thus do not
135a0 20 63 6f 75 6e 74 20 61 73 20 61 20 73 74 61 74   count as a stat
135b0 65 6d 65 6e 74 20 74 65 72 6d 69 6e 61 74 6f 72  ement terminator
135c0 2e 20 20 57 68 69 74 65 73 70 61 63 65 0a 2a 2a  .  Whitespace.**
135d0 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 20 74 68   and comments th
135e0 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20 66 69  at follow the fi
135f0 6e 61 6c 20 73 65 6d 69 63 6f 6c 6f 6e 20 61 72  nal semicolon ar
13600 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  e ignored..**.**
13610 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
13620 72 65 74 75 72 6e 20 30 20 69 66 20 74 68 65 20  return 0 if the 
13630 73 74 61 74 65 6d 65 6e 74 20 69 73 20 69 6e 63  statement is inc
13640 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 61 0a 2a  omplete.  If a.*
13650 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  * memory allocat
13660 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 6e 20  ion fails, then 
13670 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20  SQLITE_NOMEM is 
13680 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
13690 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64  These routines d
136a0 6f 20 6e 6f 74 20 70 61 72 73 65 20 74 68 65 20  o not parse the 
136b0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 74  SQL statements t
136c0 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  hus.** will not 
136d0 64 65 74 65 63 74 20 73 79 6e 74 61 63 74 69 63  detect syntactic
136e0 61 6c 6c 79 20 69 6e 63 6f 72 72 65 63 74 20 53  ally incorrect S
136f0 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c  QL..**.** If SQL
13700 69 74 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ite has not been
13710 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 75 73 69   initialized usi
13720 6e 67 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74  ng [sqlite3_init
13730 69 61 6c 69 7a 65 28 29 5d 20 70 72 69 6f 72 20  ialize()] prior 
13740 0a 2a 2a 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20  .** to invoking 
13750 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
13760 31 36 28 29 20 74 68 65 6e 20 73 71 6c 69 74 65  16() then sqlite
13770 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69  3_initialize() i
13780 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 75 74  s invoked.** aut
13790 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 73 71  omatically by sq
137a0 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36  lite3_complete16
137b0 28 29 2e 20 20 49 66 20 74 68 61 74 20 69 6e 69  ().  If that ini
137c0 74 69 61 6c 69 7a 61 74 69 6f 6e 20 66 61 69 6c  tialization fail
137d0 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 72  s,.** then the r
137e0 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d  eturn value from
137f0 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
13800 65 31 36 28 29 20 77 69 6c 6c 20 62 65 20 6e 6f  e16() will be no
13810 6e 2d 7a 65 72 6f 0a 2a 2a 20 72 65 67 61 72 64  n-zero.** regard
13820 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20  less of whether 
13830 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 70 75 74  or not the input
13840 20 53 51 4c 20 69 73 20 63 6f 6d 70 6c 65 74 65   SQL is complete
13850 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
13860 65 6e 74 73 3a 20 5b 48 31 30 35 31 31 5d 20 5b  ents: [H10511] [
13870 48 31 30 35 31 32 5d 0a 2a 2a 0a 2a 2a 20 54 68  H10512].**.** Th
13880 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69  e input to [sqli
13890 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 5d 20  te3_complete()] 
138a0 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74  must be a zero-t
138b0 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46  erminated.** UTF
138c0 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  -8 string..**.**
138d0 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 5b 73   The input to [s
138e0 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31  qlite3_complete1
138f0 36 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a  6()] must be a z
13900 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a  ero-terminated.*
13910 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20  * UTF-16 string 
13920 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  in native byte o
13930 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rder..*/.SQLITE_
13940 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
13950 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74 20 63  complete(const c
13960 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51 4c 49 54  har *sql);.SQLIT
13970 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
13980 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e  3_complete16(con
13990 73 74 20 76 6f 69 64 20 2a 73 71 6c 29 3b 0a 0a  st void *sql);..
139a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
139b0 52 65 67 69 73 74 65 72 20 41 20 43 61 6c 6c 62  Register A Callb
139c0 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65 20 53 51  ack To Handle SQ
139d0 4c 49 54 45 5f 42 55 53 59 20 45 72 72 6f 72 73  LITE_BUSY Errors
139e0 20 7b 48 31 32 33 31 30 7d 20 3c 53 34 30 34 30   {H12310} <S4040
139f0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0>.**.** This ro
13a00 75 74 69 6e 65 20 73 65 74 73 20 61 20 63 61 6c  utine sets a cal
13a10 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74  lback function t
13a20 68 61 74 20 6d 69 67 68 74 20 62 65 20 69 6e 76  hat might be inv
13a30 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a  oked whenever.**
13a40 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
13a50 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 64 61  ade to open a da
13a60 74 61 62 61 73 65 20 74 61 62 6c 65 20 74 68 61  tabase table tha
13a70 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  t another thread
13a80 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73 73 20 68  .** or process h
13a90 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a 0a 2a 2a  as locked..**.**
13aa0 20 49 66 20 74 68 65 20 62 75 73 79 20 63 61 6c   If the busy cal
13ab0 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2c 20 74  lback is NULL, t
13ac0 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  hen [SQLITE_BUSY
13ad0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45  ] or [SQLITE_IOE
13ae0 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 69  RR_BLOCKED].** i
13af0 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64  s returned immed
13b00 69 61 74 65 6c 79 20 75 70 6f 6e 20 65 6e 63 6f  iately upon enco
13b10 75 6e 74 65 72 69 6e 67 20 74 68 65 20 6c 6f 63  untering the loc
13b20 6b 2e 20 49 66 20 74 68 65 20 62 75 73 79 20 63  k. If the busy c
13b30 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 6e 6f  allback.** is no
13b40 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  t NULL, then the
13b50 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 62   callback will b
13b60 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 74  e invoked with t
13b70 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a  wo arguments..**
13b80 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72  .** The first ar
13b90 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 68 61  gument to the ha
13ba0 6e 64 6c 65 72 20 69 73 20 61 20 63 6f 70 79 20  ndler is a copy 
13bb0 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 6f 69  of the void* poi
13bc0 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a 20 69 73  nter which.** is
13bd0 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   the third argum
13be0 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  ent to sqlite3_b
13bf0 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 2e 20 20  usy_handler().  
13c00 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
13c10 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 68 61  ent to.** the ha
13c20 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  ndler callback i
13c30 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
13c40 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 62  times that the b
13c50 75 73 79 20 68 61 6e 64 6c 65 72 20 68 61 73 0a  usy handler has.
13c60 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b 65 64 20  ** been invoked 
13c70 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67  for this locking
13c80 20 65 76 65 6e 74 2e 20 20 49 66 20 74 68 65 0a   event.  If the.
13c90 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  ** busy callback
13ca0 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68 65 6e   returns 0, then
13cb0 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 61   no additional a
13cc0 74 74 65 6d 70 74 73 20 61 72 65 20 6d 61 64 65  ttempts are made
13cd0 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73 20 74 68   to.** access th
13ce0 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 5b  e database and [
13cf0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20  SQLITE_BUSY] or 
13d00 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c  [SQLITE_IOERR_BL
13d10 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 75 72 6e  OCKED] is return
13d20 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 61  ed..** If the ca
13d30 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e  llback returns n
13d40 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e  on-zero, then an
13d50 6f 74 68 65 72 20 61 74 74 65 6d 70 74 0a 2a 2a  other attempt.**
13d60 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e   is made to open
13d70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f   the database fo
13d80 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 74 68  r reading and th
13d90 65 20 63 79 63 6c 65 20 72 65 70 65 61 74 73 2e  e cycle repeats.
13da0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 73 65  .**.** The prese
13db0 6e 63 65 20 6f 66 20 61 20 62 75 73 79 20 68 61  nce of a busy ha
13dc0 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f 74 20 67  ndler does not g
13dd0 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 69 74  uarantee that it
13de0 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64   will be invoked
13df0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72 65 20 69  .** when there i
13e00 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f  s lock contentio
13e10 6e 2e 20 49 66 20 53 51 4c 69 74 65 20 64 65 74  n. If SQLite det
13e20 65 72 6d 69 6e 65 73 20 74 68 61 74 20 69 6e 76  ermines that inv
13e30 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 0a 2a  oking the busy.*
13e40 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75 6c 64 20  * handler could 
13e50 72 65 73 75 6c 74 20 69 6e 20 61 20 64 65 61 64  result in a dead
13e60 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c 20 67 6f  lock, it will go
13e70 20 61 68 65 61 64 20 61 6e 64 20 72 65 74 75 72   ahead and retur
13e80 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 0a  n [SQLITE_BUSY].
13e90 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f  ** or [SQLITE_IO
13ea0 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 6e 73  ERR_BLOCKED] ins
13eb0 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67  tead of invoking
13ec0 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
13ed0 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61  r..** Consider a
13ee0 20 73 63 65 6e 61 72 69 6f 20 77 68 65 72 65 20   scenario where 
13ef0 6f 6e 65 20 70 72 6f 63 65 73 73 20 69 73 20 68  one process is h
13f00 6f 6c 64 69 6e 67 20 61 20 72 65 61 64 20 6c 6f  olding a read lo
13f10 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 73  ck that.** it is
13f20 20 74 72 79 69 6e 67 20 74 6f 20 70 72 6f 6d 6f   trying to promo
13f30 74 65 20 74 6f 20 61 20 72 65 73 65 72 76 65 64  te to a reserved
13f40 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20 61 20 73   lock and.** a s
13f50 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 69 73  econd process is
13f60 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 73 65 72   holding a reser
13f70 76 65 64 20 6c 6f 63 6b 20 74 68 61 74 20 69 74  ved lock that it
13f80 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f   is trying.** to
13f90 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 6e 20 65   promote to an e
13fa0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20  xclusive lock.  
13fb0 54 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 73  The first proces
13fc0 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63 65 65 64  s cannot proceed
13fd0 0a 2a 2a 20 62 65 63 61 75 73 65 20 69 74 20 69  .** because it i
13fe0 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65  s blocked by the
13ff0 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 65 20   second and the 
14000 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 63  second process c
14010 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63 65 65 64  annot.** proceed
14020 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 62   because it is b
14030 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20 66 69  locked by the fi
14040 72 73 74 2e 20 20 49 66 20 62 6f 74 68 20 70 72  rst.  If both pr
14050 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e 76 6f 6b  ocesses.** invok
14060 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c  e the busy handl
14070 65 72 73 2c 20 6e 65 69 74 68 65 72 20 77 69 6c  ers, neither wil
14080 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72 6f 67 72  l make any progr
14090 65 73 73 2e 20 20 54 68 65 72 65 66 6f 72 65 2c  ess.  Therefore,
140a0 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65 74 75 72  .** SQLite retur
140b0 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  ns [SQLITE_BUSY]
140c0 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 70   for the first p
140d0 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e 67 20 74  rocess, hoping t
140e0 68 61 74 20 74 68 69 73 0a 2a 2a 20 77 69 6c 6c  hat this.** will
140f0 20 69 6e 64 75 63 65 20 74 68 65 20 66 69 72 73   induce the firs
14100 74 20 70 72 6f 63 65 73 73 20 74 6f 20 72 65 6c  t process to rel
14110 65 61 73 65 20 69 74 73 20 72 65 61 64 20 6c 6f  ease its read lo
14120 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a 2a 2a 20  ck and allow.** 
14130 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65  the second proce
14140 73 73 20 74 6f 20 70 72 6f 63 65 65 64 2e 0a 2a  ss to proceed..*
14150 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  *.** The default
14160 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69   busy callback i
14170 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68  s NULL..**.** Th
14180 65 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  e [SQLITE_BUSY] 
14190 65 72 72 6f 72 20 69 73 20 63 6f 6e 76 65 72 74  error is convert
141a0 65 64 20 74 6f 20 5b 53 51 4c 49 54 45 5f 49 4f  ed to [SQLITE_IO
141b0 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20  ERR_BLOCKED].** 
141c0 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 69  when SQLite is i
141d0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
141e0 61 20 6c 61 72 67 65 20 74 72 61 6e 73 61 63 74  a large transact
141f0 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c 20 74 68  ion where all th
14200 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20 77 69 6c  e.** changes wil
14210 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 6f 20 74  l not fit into t
14220 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63  he in-memory cac
14230 68 65 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c  he.  SQLite will
14240 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 6f 6c 64  .** already hold
14250 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
14260 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
14270 20 66 69 6c 65 2c 20 62 75 74 20 69 74 20 6e 65   file, but it ne
14280 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74  eds.** to promot
14290 65 20 74 68 69 73 20 6c 6f 63 6b 20 74 6f 20 45  e this lock to E
142a0 58 43 4c 55 53 49 56 45 20 73 6f 20 74 68 61 74  XCLUSIVE so that
142b0 20 69 74 20 63 61 6e 20 73 70 69 6c 6c 20 63 61   it can spill ca
142c0 63 68 65 0a 2a 2a 20 70 61 67 65 73 20 69 6e 74  che.** pages int
142d0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
142e0 69 6c 65 20 77 69 74 68 6f 75 74 20 68 61 72 6d  ile without harm
142f0 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e 74 0a 2a   to concurrent.*
14300 2a 20 72 65 61 64 65 72 73 2e 20 20 49 66 20 69  * readers.  If i
14310 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 70  t is unable to p
14320 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f 63 6b 2c  romote the lock,
14330 20 74 68 65 6e 20 74 68 65 20 69 6e 2d 6d 65 6d   then the in-mem
14340 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20 77 69 6c  ory.** cache wil
14350 6c 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20  l be left in an 
14360 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61  inconsistent sta
14370 74 65 20 61 6e 64 20 73 6f 20 74 68 65 20 65 72  te and so the er
14380 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 70  ror.** code is p
14390 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20 74 68 65  romoted from the
143a0 20 72 65 6c 61 74 69 76 65 6c 79 20 62 65 6e 69   relatively beni
143b0 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  gn [SQLITE_BUSY]
143c0 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20   to.** the more 
143d0 73 65 76 65 72 65 20 5b 53 51 4c 49 54 45 5f 49  severe [SQLITE_I
143e0 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20  OERR_BLOCKED].  
143f0 54 68 69 73 20 65 72 72 6f 72 20 63 6f 64 65 20  This error code 
14400 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72  promotion.** for
14410 63 65 73 20 61 6e 20 61 75 74 6f 6d 61 74 69 63  ces an automatic
14420 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65   rollback of the
14430 20 63 68 61 6e 67 65 73 2e 20 20 53 65 65 20 74   changes.  See t
14440 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 2f  he.** <a href="/
14450 63 76 73 74 72 61 63 2f 77 69 6b 69 3f 70 3d 43  cvstrac/wiki?p=C
14460 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69  orruptionFollowi
14470 6e 67 42 75 73 79 45 72 72 6f 72 22 3e 0a 2a 2a  ngBusyError">.**
14480 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f   CorruptionFollo
14490 77 69 6e 67 42 75 73 79 45 72 72 6f 72 3c 2f 61  wingBusyError</a
144a0 3e 20 77 69 6b 69 20 70 61 67 65 20 66 6f 72 20  > wiki page for 
144b0 61 20 64 69 73 63 75 73 73 69 6f 6e 20 6f 66 20  a discussion of 
144c0 77 68 79 0a 2a 2a 20 74 68 69 73 20 69 73 20 69  why.** this is i
144d0 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20  mportant..**.** 
144e0 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62  There can only b
144f0 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79 20  e a single busy 
14500 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65 64 20  handler defined 
14510 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b 64 61 74  for each.** [dat
14520 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
14530 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 20 6e 65  ].  Setting a ne
14540 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63  w busy handler c
14550 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 70 72 65  lears any.** pre
14560 76 69 6f 75 73 6c 79 20 73 65 74 20 68 61 6e 64  viously set hand
14570 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  ler.  Note that 
14580 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33  calling [sqlite3
14590 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d  _busy_timeout()]
145a0 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f 20 73 65  .** will also se
145b0 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65 20 62  t or clear the b
145c0 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a  usy handler..**.
145d0 2a 2a 20 54 68 65 20 62 75 73 79 20 63 61 6c 6c  ** The busy call
145e0 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e 6f 74 20  back should not 
145f0 74 61 6b 65 20 61 6e 79 20 61 63 74 69 6f 6e 73  take any actions
14600 20 77 68 69 63 68 20 6d 6f 64 69 66 79 20 74 68   which modify th
14610 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  e.** database co
14620 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e  nnection that in
14630 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 20 68  voked the busy h
14640 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20 73 75 63  andler.  Any suc
14650 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 72 65 73  h actions.** res
14660 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e 65 64  ult in undefined
14670 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 20 0a 2a   behavior..** .*
14680 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
14690 2a 2a 20 5b 48 31 32 33 31 31 5d 20 5b 48 31 32  ** [H12311] [H12
146a0 33 31 32 5d 20 5b 48 31 32 33 31 34 5d 20 5b 48  312] [H12314] [H
146b0 31 32 33 31 36 5d 20 5b 48 31 32 33 31 38 5d 0a  12316] [H12318].
146c0 2a 2a 0a 2a 2a 20 41 20 62 75 73 79 20 68 61 6e  **.** A busy han
146d0 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 63 6c  dler must not cl
146e0 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ose the database
146f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f   connection.** o
14700 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  r [prepared stat
14710 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f  ement] that invo
14720 6b 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e  ked the busy han
14730 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dler..*/.SQLITE_
14740 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
14750 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c  busy_handler(sql
14760 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f  ite3*, int(*)(vo
14770 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29  id*,int), void*)
14780 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
14790 46 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69  F: Set A Busy Ti
147a0 6d 65 6f 75 74 20 7b 48 31 32 33 34 30 7d 20 3c  meout {H12340} <
147b0 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S40410>.**.** Th
147c0 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
147d0 61 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  a [sqlite3_busy_
147e0 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 20 68  handler | busy h
147f0 61 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 6c 65  andler] that sle
14800 65 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65  eps.** for a spe
14810 63 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66  cified amount of
14820 20 74 69 6d 65 20 77 68 65 6e 20 61 20 74 61 62   time when a tab
14830 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 54  le is locked.  T
14840 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69  he handler.** wi
14850 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c  ll sleep multipl
14860 65 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 61 74  e times until at
14870 20 6c 65 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c   least "ms" mill
14880 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65  iseconds of slee
14890 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61 63 63  ping.** have acc
148a0 75 6d 75 6c 61 74 65 64 2e 20 7b 48 31 32 33 34  umulated. {H1234
148b0 33 7d 20 41 66 74 65 72 20 22 6d 73 22 20 6d 69  3} After "ms" mi
148c0 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c  lliseconds of sl
148d0 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 68  eeping,.** the h
148e0 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 30  andler returns 0
148f0 20 77 68 69 63 68 20 63 61 75 73 65 73 20 5b 73   which causes [s
14900 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 74  qlite3_step()] t
14910 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c  o return.** [SQL
14920 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51  ITE_BUSY] or [SQ
14930 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
14940 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69  ED]..**.** Calli
14950 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ng this routine 
14960 77 69 74 68 20 61 6e 20 61 72 67 75 6d 65 6e 74  with an argument
14970 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
14980 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74  ual to zero.** t
14990 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20 62 75 73  urns off all bus
149a0 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a  y handlers..**.*
149b0 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  * There can only
149c0 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73   be a single bus
149d0 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20  y handler for a 
149e0 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64  particular.** [d
149f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14a00 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 67 69 76 65  on] any any give
14a10 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 20 61 6e  n moment.  If an
14a20 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c  other busy handl
14a30 65 72 0a 2a 2a 20 77 61 73 20 64 65 66 69 6e 65  er.** was define
14a40 64 20 20 28 75 73 69 6e 67 20 5b 73 71 6c 69 74  d  (using [sqlit
14a50 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28  e3_busy_handler(
14a60 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c  )]) prior to cal
14a70 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ling.** this rou
14a80 74 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 65 72  tine, that other
14a90 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69 73   busy handler is
14aa0 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   cleared..**.** 
14ab0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
14ac0 20 5b 48 31 32 33 34 31 5d 20 5b 48 31 32 33 34   [H12341] [H1234
14ad0 33 5d 20 5b 48 31 32 33 34 34 5d 0a 2a 2f 0a 53  3] [H12344].*/.S
14ae0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
14af0 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f  lite3_busy_timeo
14b00 75 74 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ut(sqlite3*, int
14b10 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50   ms);../*.** CAP
14b20 49 33 52 45 46 3a 20 43 6f 6e 76 65 6e 69 65 6e  I3REF: Convenien
14b30 63 65 20 52 6f 75 74 69 6e 65 73 20 46 6f 72 20  ce Routines For 
14b40 52 75 6e 6e 69 6e 67 20 51 75 65 72 69 65 73 20  Running Queries 
14b50 7b 48 31 32 33 37 30 7d 20 3c 53 31 30 30 30 30  {H12370} <S10000
14b60 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 69 74 69  >.**.** Definiti
14b70 6f 6e 3a 20 41 20 3c 62 3e 72 65 73 75 6c 74 20  on: A <b>result 
14b80 74 61 62 6c 65 3c 2f 62 3e 20 69 73 20 6d 65 6d  table</b> is mem
14b90 6f 72 79 20 64 61 74 61 20 73 74 72 75 63 74 75  ory data structu
14ba0 72 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68  re created by th
14bb0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65  e.** [sqlite3_ge
14bc0 74 5f 74 61 62 6c 65 28 29 5d 20 69 6e 74 65 72  t_table()] inter
14bd0 66 61 63 65 2e 20 20 41 20 72 65 73 75 6c 74 20  face.  A result 
14be0 74 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68  table records th
14bf0 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 20 71 75  e.** complete qu
14c00 65 72 79 20 72 65 73 75 6c 74 73 20 66 72 6f 6d  ery results from
14c10 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 71 75 65   one or more que
14c20 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ries..**.** The 
14c30 74 61 62 6c 65 20 63 6f 6e 63 65 70 74 75 61 6c  table conceptual
14c40 6c 79 20 68 61 73 20 61 20 6e 75 6d 62 65 72 20  ly has a number 
14c50 6f 66 20 72 6f 77 73 20 61 6e 64 20 63 6f 6c 75  of rows and colu
14c60 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65  mns.  But.** the
14c70 73 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6e  se numbers are n
14c80 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 72  ot part of the r
14c90 65 73 75 6c 74 20 74 61 62 6c 65 20 69 74 73 65  esult table itse
14ca0 6c 66 2e 20 20 54 68 65 73 65 0a 2a 2a 20 6e 75  lf.  These.** nu
14cb0 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69 6e  mbers are obtain
14cc0 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20  ed separately.  
14cd0 4c 65 74 20 4e 20 62 65 20 74 68 65 20 6e 75 6d  Let N be the num
14ce0 62 65 72 20 6f 66 20 72 6f 77 73 0a 2a 2a 20 61  ber of rows.** a
14cf0 6e 64 20 4d 20 62 65 20 74 68 65 20 6e 75 6d 62  nd M be the numb
14d00 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 0a 2a  er of columns..*
14d10 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61  *.** A result ta
14d20 62 6c 65 20 69 73 20 61 6e 20 61 72 72 61 79 20  ble is an array 
14d30 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 7a  of pointers to z
14d40 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  ero-terminated U
14d50 54 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a  TF-8 strings..**
14d60 20 54 68 65 72 65 20 61 72 65 20 28 4e 2b 31 29   There are (N+1)
14d70 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  *M elements in t
14d80 68 65 20 61 72 72 61 79 2e 20 20 54 68 65 20 66  he array.  The f
14d90 69 72 73 74 20 4d 20 70 6f 69 6e 74 65 72 73 20  irst M pointers 
14da0 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f  point.** to zero
14db0 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
14dc0 6e 67 73 20 74 68 61 74 20 20 63 6f 6e 74 61 69  ngs that  contai
14dd0 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74  n the names of t
14de0 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54  he columns..** T
14df0 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74  he remaining ent
14e00 72 69 65 73 20 61 6c 6c 20 70 6f 69 6e 74 20 74  ries all point t
14e10 6f 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 2e  o query results.
14e20 20 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 72 65    NULL values re
14e30 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e 55 4c 4c 20  sult.** in NULL 
14e40 70 6f 69 6e 74 65 72 73 2e 20 20 41 6c 6c 20 6f  pointers.  All o
14e50 74 68 65 72 20 76 61 6c 75 65 73 20 61 72 65 20  ther values are 
14e60 69 6e 20 74 68 65 69 72 20 55 54 46 2d 38 20 7a  in their UTF-8 z
14e70 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a  ero-terminated.*
14e80 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  * string represe
14e90 6e 74 61 74 69 6f 6e 20 61 73 20 72 65 74 75 72  ntation as retur
14ea0 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  ned by [sqlite3_
14eb0 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a  column_text()]..
14ec0 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74  **.** A result t
14ed0 61 62 6c 65 20 6d 69 67 68 74 20 63 6f 6e 73 69  able might consi
14ee0 73 74 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72  st of one or mor
14ef0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
14f00 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e  ions..** It is n
14f10 6f 74 20 73 61 66 65 20 74 6f 20 70 61 73 73 20  ot safe to pass 
14f20 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 64  a result table d
14f30 69 72 65 63 74 6c 79 20 74 6f 20 5b 73 71 6c 69  irectly to [sqli
14f40 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 20  te3_free()]..** 
14f50 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 73  A result table s
14f60 68 6f 75 6c 64 20 62 65 20 64 65 61 6c 6c 6f 63  hould be dealloc
14f70 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69  ated using [sqli
14f80 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29  te3_free_table()
14f90 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 61 6e 20 65  ]..**.** As an e
14fa0 78 61 6d 70 6c 65 20 6f 66 20 74 68 65 20 72 65  xample of the re
14fb0 73 75 6c 74 20 74 61 62 6c 65 20 66 6f 72 6d 61  sult table forma
14fc0 74 2c 20 73 75 70 70 6f 73 65 20 61 20 71 75 65  t, suppose a que
14fd0 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20  ry result.** is 
14fe0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
14ff0 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
15000 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 4e 61  re>.**        Na
15010 6d 65 20 20 20 20 20 20 20 20 7c 20 41 67 65 0a  me        | Age.
15020 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d  **        ------
15030 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
15040 2d 0a 2a 2a 20 20 20 20 20 20 20 20 41 6c 69 63  -.**        Alic
15050 65 20 20 20 20 20 20 20 7c 20 34 33 0a 2a 2a 20  e       | 43.** 
15060 20 20 20 20 20 20 20 42 6f 62 20 20 20 20 20 20         Bob      
15070 20 20 20 7c 20 32 38 0a 2a 2a 20 20 20 20 20 20     | 28.**      
15080 20 20 43 69 6e 64 79 20 20 20 20 20 20 20 7c 20    Cindy       | 
15090 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c  21.** </pre></bl
150a0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20  ockquote>.**.** 
150b0 54 68 65 72 65 20 61 72 65 20 74 77 6f 20 63 6f  There are two co
150c0 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20 61 6e 64 20  lumn (M==2) and 
150d0 74 68 72 65 65 20 72 6f 77 73 20 28 4e 3d 3d 33  three rows (N==3
150e0 29 2e 20 20 54 68 75 73 20 74 68 65 0a 2a 2a 20  ).  Thus the.** 
150f0 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68 61 73  result table has
15100 20 38 20 65 6e 74 72 69 65 73 2e 20 20 53 75 70   8 entries.  Sup
15110 70 6f 73 65 20 74 68 65 20 72 65 73 75 6c 74 20  pose the result 
15120 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 0a  table is stored.
15130 2a 2a 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6e  ** in an array n
15140 61 6d 65 73 20 61 7a 52 65 73 75 6c 74 2e 20 20  ames azResult.  
15150 54 68 65 6e 20 61 7a 52 65 73 75 6c 74 20 68 6f  Then azResult ho
15160 6c 64 73 20 74 68 69 73 20 63 6f 6e 74 65 6e 74  lds this content
15170 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
15180 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20  ote><pre>.**    
15190 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
151a0 3b 30 5d 20 3d 20 22 4e 61 6d 65 22 3b 0a 2a 2a  ;0] = "Name";.**
151b0 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
151c0 26 23 39 31 3b 31 5d 20 3d 20 22 41 67 65 22 3b  &#91;1] = "Age";
151d0 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
151e0 75 6c 74 26 23 39 31 3b 32 5d 20 3d 20 22 41 6c  ult&#91;2] = "Al
151f0 69 63 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20  ice";.**        
15200 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 33 5d 20  azResult&#91;3] 
15210 3d 20 22 34 33 22 3b 0a 2a 2a 20 20 20 20 20 20  = "43";.**      
15220 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 34    azResult&#91;4
15230 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 2a 20 20 20  ] = "Bob";.**   
15240 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39       azResult&#9
15250 31 3b 35 5d 20 3d 20 22 32 38 22 3b 0a 2a 2a 20  1;5] = "28";.** 
15260 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26         azResult&
15270 23 39 31 3b 36 5d 20 3d 20 22 43 69 6e 64 79 22  #91;6] = "Cindy"
15280 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65  ;.**        azRe
15290 73 75 6c 74 26 23 39 31 3b 37 5d 20 3d 20 22 32  sult&#91;7] = "2
152a0 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  1";.** </pre></b
152b0 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
152c0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74   The sqlite3_get
152d0 5f 74 61 62 6c 65 28 29 20 66 75 6e 63 74 69 6f  _table() functio
152e0 6e 20 65 76 61 6c 75 61 74 65 73 20 6f 6e 65 20  n evaluates one 
152f0 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73 65 6d 69 63  or more.** semic
15300 6f 6c 6f 6e 2d 73 65 70 61 72 61 74 65 64 20 53  olon-separated S
15310 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  QL statements in
15320 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e   the zero-termin
15330 61 74 65 64 20 55 54 46 2d 38 0a 2a 2a 20 73 74  ated UTF-8.** st
15340 72 69 6e 67 20 6f 66 20 69 74 73 20 32 6e 64 20  ring of its 2nd 
15350 70 61 72 61 6d 65 74 65 72 2e 20 20 49 74 20 72  parameter.  It r
15360 65 74 75 72 6e 73 20 61 20 72 65 73 75 6c 74 20  eturns a result 
15370 74 61 62 6c 65 20 74 6f 20 74 68 65 0a 2a 2a 20  table to the.** 
15380 70 6f 69 6e 74 65 72 20 67 69 76 65 6e 20 69 6e  pointer given in
15390 20 69 74 73 20 33 72 64 20 70 61 72 61 6d 65 74   its 3rd paramet
153a0 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  er..**.** After 
153b0 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
153c0 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65  tion has finishe
153d0 64 20 75 73 69 6e 67 20 74 68 65 20 72 65 73 75  d using the resu
153e0 6c 74 2c 20 69 74 20 73 68 6f 75 6c 64 0a 2a 2a  lt, it should.**
153f0 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 65   pass the pointe
15400 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20  r to the result 
15410 74 61 62 6c 65 20 74 6f 20 73 71 6c 69 74 65 33  table to sqlite3
15420 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 20 69 6e  _free_table() in
15430 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 72 65 6c   order to.** rel
15440 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  ease the memory 
15450 74 68 61 74 20 77 61 73 20 6d 61 6c 6c 6f 63 65  that was malloce
15460 64 2e 20 20 42 65 63 61 75 73 65 20 6f 66 20 74  d.  Because of t
15470 68 65 20 77 61 79 20 74 68 65 0a 2a 2a 20 5b 73  he way the.** [s
15480 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
15490 20 68 61 70 70 65 6e 73 20 77 69 74 68 69 6e 20   happens within 
154a0 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
154b0 65 28 29 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67  e(), the calling
154c0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  .** function mus
154d0 74 20 6e 6f 74 20 74 72 79 20 74 6f 20 63 61 6c  t not try to cal
154e0 6c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  l [sqlite3_free(
154f0 29 5d 20 64 69 72 65 63 74 6c 79 2e 20 20 4f 6e  )] directly.  On
15500 6c 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  ly.** [sqlite3_f
15510 72 65 65 5f 74 61 62 6c 65 28 29 5d 20 69 73 20  ree_table()] is 
15520 61 62 6c 65 20 74 6f 20 72 65 6c 65 61 73 65 20  able to release 
15530 74 68 65 20 6d 65 6d 6f 72 79 20 70 72 6f 70 65  the memory prope
15540 72 6c 79 20 61 6e 64 20 73 61 66 65 6c 79 2e 0a  rly and safely..
15550 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
15560 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 69 6e  3_get_table() in
15570 74 65 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65  terface is imple
15580 6d 65 6e 74 65 64 20 61 73 20 61 20 77 72 61 70  mented as a wrap
15590 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73  per around.** [s
155a0 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 2e 20  qlite3_exec()]. 
155b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74   The sqlite3_get
155c0 5f 74 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65  _table() routine
155d0 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61   does not have a
155e0 63 63 65 73 73 0a 2a 2a 20 74 6f 20 61 6e 79 20  ccess.** to any 
155f0 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74  internal data st
15600 72 75 63 74 75 72 65 73 20 6f 66 20 53 51 4c 69  ructures of SQLi
15610 74 65 2e 20 20 49 74 20 75 73 65 73 20 6f 6e 6c  te.  It uses onl
15620 79 20 74 68 65 20 70 75 62 6c 69 63 0a 2a 2a 20  y the public.** 
15630 69 6e 74 65 72 66 61 63 65 20 64 65 66 69 6e 65  interface define
15640 64 20 68 65 72 65 2e 20 20 41 73 20 61 20 63 6f  d here.  As a co
15650 6e 73 65 71 75 65 6e 63 65 2c 20 65 72 72 6f 72  nsequence, error
15660 73 20 74 68 61 74 20 6f 63 63 75 72 20 69 6e 20  s that occur in 
15670 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 20 6c  the.** wrapper l
15680 61 79 65 72 20 6f 75 74 73 69 64 65 20 6f 66 20  ayer outside of 
15690 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 5b 73 71  the internal [sq
156a0 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 63 61  lite3_exec()] ca
156b0 6c 6c 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 65  ll are not.** re
156c0 66 6c 65 63 74 65 64 20 69 6e 20 73 75 62 73 65  flected in subse
156d0 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b  quent calls to [
156e0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
156f0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65  )] or [sqlite3_e
15700 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  rrmsg()]..**.** 
15710 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
15720 20 5b 48 31 32 33 37 31 5d 20 5b 48 31 32 33 37   [H12371] [H1237
15730 33 5d 20 5b 48 31 32 33 37 34 5d 20 5b 48 31 32  3] [H12374] [H12
15740 33 37 36 5d 20 5b 48 31 32 33 37 39 5d 20 5b 48  376] [H12379] [H
15750 31 32 33 38 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12382].*/.SQLITE
15760 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
15770 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20 73 71  _get_table(.  sq
15780 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
15790 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64      /* An open d
157a0 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e  atabase */.  con
157b0 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
157c0 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20     /* SQL to be 
157d0 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 63  evaluated */.  c
157e0 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74  har ***pazResult
157f0 2c 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20  ,    /* Results 
15800 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a  of the query */.
15810 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20    int *pnRow,   
15820 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
15830 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f 77 73  r of result rows
15840 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
15850 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e  .  int *pnColumn
15860 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ,        /* Numb
15870 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c  er of result col
15880 75 6d 6e 73 20 77 72 69 74 74 65 6e 20 68 65 72  umns written her
15890 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  e */.  char **pz
158a0 45 72 72 6d 73 67 20 20 20 20 20 20 20 2f 2a 20  Errmsg       /* 
158b0 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74 74 65  Error msg writte
158c0 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c  n here */.);.SQL
158d0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
158e0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
158f0 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 29 3b 0a  char **result);.
15900 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
15910 20 46 6f 72 6d 61 74 74 65 64 20 53 74 72 69 6e   Formatted Strin
15920 67 20 50 72 69 6e 74 69 6e 67 20 46 75 6e 63 74  g Printing Funct
15930 69 6f 6e 73 20 7b 48 31 37 34 30 30 7d 20 3c 53  ions {H17400} <S
15940 37 30 30 30 30 3e 3c 53 32 30 30 30 30 3e 0a 2a  70000><S20000>.*
15950 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
15960 6e 65 73 20 61 72 65 20 77 6f 72 6b 61 6c 69 6b  nes are workalik
15970 65 73 20 6f 66 20 74 68 65 20 22 70 72 69 6e 74  es of the "print
15980 66 28 29 22 20 66 61 6d 69 6c 79 20 6f 66 20 66  f()" family of f
15990 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d  unctions.** from
159a0 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20   the standard C 
159b0 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54  library..**.** T
159c0 68 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  he sqlite3_mprin
159d0 74 66 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  tf() and sqlite3
159e0 5f 76 6d 70 72 69 6e 74 66 28 29 20 72 6f 75 74  _vmprintf() rout
159f0 69 6e 65 73 20 77 72 69 74 65 20 74 68 65 69 72  ines write their
15a00 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f  .** results into
15a10 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
15a20 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
15a30 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 54 68 65  alloc()]..** The
15a40 20 73 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65   strings returne
15a50 64 20 62 79 20 74 68 65 73 65 20 74 77 6f 20 72  d by these two r
15a60 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 62  outines should b
15a70 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 62 79  e.** released by
15a80 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
15a90 5d 2e 20 20 42 6f 74 68 20 72 6f 75 74 69 6e 65  ].  Both routine
15aa0 73 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55  s return a.** NU
15ab0 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 5b 73  LL pointer if [s
15ac0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
15ad0 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c   is unable to al
15ae0 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 0a 2a 2a  locate enough.**
15af0 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20   memory to hold 
15b00 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74  the resulting st
15b10 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73  ring..**.** In s
15b20 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
15b30 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d  ) routine is sim
15b40 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 69 6e 74  ilar to "snprint
15b50 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  f()" from.** the
15b60 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72   standard C libr
15b70 61 72 79 2e 20 20 54 68 65 20 72 65 73 75 6c 74  ary.  The result
15b80 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
15b90 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 73   the.** buffer s
15ba0 75 70 70 6c 69 65 64 20 61 73 20 74 68 65 20 73  upplied as the s
15bb0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
15bc0 77 68 6f 73 65 20 73 69 7a 65 20 69 73 20 67 69  whose size is gi
15bd0 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 69  ven by.** the fi
15be0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 4e  rst parameter. N
15bf0 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64  ote that the ord
15c00 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72  er of the.** fir
15c10 73 74 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72  st two parameter
15c20 73 20 69 73 20 72 65 76 65 72 73 65 64 20 66 72  s is reversed fr
15c30 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29 2e 20 20  om snprintf().  
15c40 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a 20 68 69  This is an.** hi
15c50 73 74 6f 72 69 63 61 6c 20 61 63 63 69 64 65 6e  storical acciden
15c60 74 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65  t that cannot be
15c70 20 66 69 78 65 64 20 77 69 74 68 6f 75 74 20 62   fixed without b
15c80 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61 63 6b 77  reaking.** backw
15c90 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
15ca0 74 79 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74  ty.  Note also t
15cb0 68 61 74 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  hat sqlite3_snpr
15cc0 69 6e 74 66 28 29 0a 2a 2a 20 72 65 74 75 72 6e  intf().** return
15cd0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
15ce0 74 73 20 62 75 66 66 65 72 20 69 6e 73 74 65 61  ts buffer instea
15cf0 64 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  d of the number 
15d00 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  of.** characters
15d10 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65   actually writte
15d20 6e 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65  n into the buffe
15d30 72 2e 20 20 57 65 20 61 64 6d 69 74 20 74 68 61  r.  We admit tha
15d40 74 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  t.** the number 
15d50 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 77 72  of characters wr
15d60 69 74 74 65 6e 20 77 6f 75 6c 64 20 62 65 20 61  itten would be a
15d70 20 6d 6f 72 65 20 75 73 65 66 75 6c 20 72 65 74   more useful ret
15d80 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 62 75 74  urn.** value but
15d90 20 77 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67   we cannot chang
15da0 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  e the implementa
15db0 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f  tion of sqlite3_
15dc0 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f  snprintf().** no
15dd0 77 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69  w without breaki
15de0 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ng compatibility
15df0 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20  ..**.** As long 
15e00 61 73 20 74 68 65 20 62 75 66 66 65 72 20 73 69  as the buffer si
15e10 7a 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ze is greater th
15e20 61 6e 20 7a 65 72 6f 2c 20 73 71 6c 69 74 65 33  an zero, sqlite3
15e30 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 67  _snprintf().** g
15e40 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74  uarantees that t
15e50 68 65 20 62 75 66 66 65 72 20 69 73 20 61 6c 77  he buffer is alw
15e60 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  ays zero-termina
15e70 74 65 64 2e 20 20 54 68 65 20 66 69 72 73 74 0a  ted.  The first.
15e80 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 22 6e 22  ** parameter "n"
15e90 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 73 69   is the total si
15ea0 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  ze of the buffer
15eb0 2c 20 69 6e 63 6c 75 64 69 6e 67 20 73 70 61 63  , including spac
15ec0 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 7a 65 72  e for.** the zer
15ed0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 53  o terminator.  S
15ee0 6f 20 74 68 65 20 6c 6f 6e 67 65 73 74 20 73 74  o the longest st
15ef0 72 69 6e 67 20 74 68 61 74 20 63 61 6e 20 62 65  ring that can be
15f00 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 77   completely.** w
15f10 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 6e  ritten will be n
15f20 2d 31 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a  -1 characters..*
15f30 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
15f40 6e 65 73 20 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e  nes all implemen
15f50 74 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61  t some additiona
15f60 6c 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20  l formatting.** 
15f70 6f 70 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65  options that are
15f80 20 75 73 65 66 75 6c 20 66 6f 72 20 63 6f 6e 73   useful for cons
15f90 74 72 75 63 74 69 6e 67 20 53 51 4c 20 73 74 61  tructing SQL sta
15fa0 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20  tements..** All 
15fb0 6f 66 20 74 68 65 20 75 73 75 61 6c 20 70 72 69  of the usual pri
15fc0 6e 74 66 28 29 20 66 6f 72 6d 61 74 74 69 6e 67  ntf() formatting
15fd0 20 6f 70 74 69 6f 6e 73 20 61 70 70 6c 79 2e 20   options apply. 
15fe0 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 74 68   In addition, th
15ff0 65 72 65 0a 2a 2a 20 69 73 20 61 72 65 20 22 25  ere.** is are "%
16000 71 22 2c 20 22 25 51 22 2c 20 61 6e 64 20 22 25  q", "%Q", and "%
16010 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  z" options..**.*
16020 2a 20 54 68 65 20 25 71 20 6f 70 74 69 6f 6e 20  * The %q option 
16030 77 6f 72 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e  works like %s in
16040 20 74 68 61 74 20 69 74 20 73 75 62 73 74 69 74   that it substit
16050 75 74 65 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d  utes a null-term
16060 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67  inated.** string
16070 20 66 72 6f 6d 20 74 68 65 20 61 72 67 75 6d 65   from the argume
16080 6e 74 20 6c 69 73 74 2e 20 20 42 75 74 20 25 71  nt list.  But %q
16090 20 61 6c 73 6f 20 64 6f 75 62 6c 65 73 20 65 76   also doubles ev
160a0 65 72 79 20 27 5c 27 27 20 63 68 61 72 61 63 74  ery '\'' charact
160b0 65 72 2e 0a 2a 2a 20 25 71 20 69 73 20 64 65 73  er..** %q is des
160c0 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 69 6e  igned for use in
160d0 73 69 64 65 20 61 20 73 74 72 69 6e 67 20 6c 69  side a string li
160e0 74 65 72 61 6c 2e 20 20 42 79 20 64 6f 75 62 6c  teral.  By doubl
160f0 69 6e 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a  ing each '\''.**
16100 20 63 68 61 72 61 63 74 65 72 20 69 74 20 65 73   character it es
16110 63 61 70 65 73 20 74 68 61 74 20 63 68 61 72 61  capes that chara
16120 63 74 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20  cter and allows 
16130 69 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  it to be inserte
16140 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74  d into.** the st
16150 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  ring..**.** For 
16160 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20  example, assume 
16170 74 68 65 20 73 74 72 69 6e 67 20 76 61 72 69 61  the string varia
16180 62 6c 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69  ble zText contai
16190 6e 73 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f  ns text as follo
161a0 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ws:.**.** <block
161b0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
161c0 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 22 49  char *zText = "I
161d0 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21  t's a happy day!
161e0 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c  ";.** </pre></bl
161f0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20  ockquote>.**.** 
16200 4f 6e 65 20 63 61 6e 20 75 73 65 20 74 68 69 73  One can use this
16210 20 74 65 78 74 20 69 6e 20 61 6e 20 53 51 4c 20   text in an SQL 
16220 73 74 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c  statement as fol
16230 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  lows:.**.** <blo
16240 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
16250 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73    char *zSQL = s
16260 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
16270 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c  INSERT INTO tabl
16280 65 20 56 41 4c 55 45 53 28 27 25 71 27 29 22 2c  e VALUES('%q')",
16290 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c   zText);.**  sql
162a0 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53  ite3_exec(db, zS
162b0 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a  QL, 0, 0, 0);.**
162c0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
162d0 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  SQL);.** </pre><
162e0 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
162f0 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 25  ** Because the %
16300 71 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  q format string 
16310 69 73 20 75 73 65 64 2c 20 74 68 65 20 27 5c 27  is used, the '\'
16320 27 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a  ' character in z
16330 54 65 78 74 0a 2a 2a 20 69 73 20 65 73 63 61 70  Text.** is escap
16340 65 64 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67  ed and the SQL g
16350 65 6e 65 72 61 74 65 64 20 69 73 20 61 73 20 66  enerated is as f
16360 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62  ollows:.**.** <b
16370 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a  lockquote><pre>.
16380 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  **  INSERT INTO 
16390 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49  table1 VALUES('I
163a0 74 27 27 73 20 61 20 68 61 70 70 79 20 64 61 79  t''s a happy day
163b0 21 27 29 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  !').** </pre></b
163c0 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
163d0 20 54 68 69 73 20 69 73 20 63 6f 72 72 65 63 74   This is correct
163e0 2e 20 20 48 61 64 20 77 65 20 75 73 65 64 20 25  .  Had we used %
163f0 73 20 69 6e 73 74 65 61 64 20 6f 66 20 25 71 2c  s instead of %q,
16400 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 53   the generated S
16410 51 4c 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65  QL.** would have
16420 20 6c 6f 6f 6b 65 64 20 6c 69 6b 65 20 74 68 69   looked like thi
16430 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  s:.**.** <blockq
16440 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49  uote><pre>.**  I
16450 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65  NSERT INTO table
16460 31 20 56 41 4c 55 45 53 28 27 49 74 27 73 20 61  1 VALUES('It's a
16470 20 68 61 70 70 79 20 64 61 79 21 27 29 3b 0a 2a   happy day!');.*
16480 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
16490 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  uote>.**.** This
164a0 20 73 65 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20   second example 
164b0 69 73 20 61 6e 20 53 51 4c 20 73 79 6e 74 61 78  is an SQL syntax
164c0 20 65 72 72 6f 72 2e 20 20 41 73 20 61 20 67 65   error.  As a ge
164d0 6e 65 72 61 6c 20 72 75 6c 65 20 79 6f 75 20 73  neral rule you s
164e0 68 6f 75 6c 64 0a 2a 2a 20 61 6c 77 61 79 73 20  hould.** always 
164f0 75 73 65 20 25 71 20 69 6e 73 74 65 61 64 20 6f  use %q instead o
16500 66 20 25 73 20 77 68 65 6e 20 69 6e 73 65 72 74  f %s when insert
16510 69 6e 67 20 74 65 78 74 20 69 6e 74 6f 20 61 20  ing text into a 
16520 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 0a  string literal..
16530 2a 2a 0a 2a 2a 20 54 68 65 20 25 51 20 6f 70 74  **.** The %Q opt
16540 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25  ion works like %
16550 71 20 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f  q except it also
16560 20 61 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f   adds single quo
16570 74 65 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68  tes around.** th
16580 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65  e outside of the
16590 20 74 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20   total string.  
165a0 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66  Additionally, if
165b0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
165c0 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  n the.** argumen
165d0 74 20 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c  t list is a NULL
165e0 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62   pointer, %Q sub
165f0 73 74 69 74 75 74 65 73 20 74 68 65 20 74 65 78  stitutes the tex
16600 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75  t "NULL" (withou
16610 74 0a 2a 2a 20 73 69 6e 67 6c 65 20 71 75 6f 74  t.** single quot
16620 65 73 29 20 69 6e 20 70 6c 61 63 65 20 6f 66 20  es) in place of 
16630 74 68 65 20 25 51 20 6f 70 74 69 6f 6e 2e 20 20  the %Q option.  
16640 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  So, for example,
16650 20 6f 6e 65 20 63 6f 75 6c 64 20 73 61 79 3a 0a   one could say:.
16660 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
16670 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72  e><pre>.**  char
16680 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33   *zSQL = sqlite3
16690 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54  _mprintf("INSERT
166a0 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55   INTO table VALU
166b0 45 53 28 25 51 29 22 2c 20 7a 54 65 78 74 29 3b  ES(%Q)", zText);
166c0 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65  .**  sqlite3_exe
166d0 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30  c(db, zSQL, 0, 0
166e0 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65  , 0);.**  sqlite
166f0 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a  3_free(zSQL);.**
16700 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75   </pre></blockqu
16710 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ote>.**.** The c
16720 6f 64 65 20 61 62 6f 76 65 20 77 69 6c 6c 20 72  ode above will r
16730 65 6e 64 65 72 20 61 20 63 6f 72 72 65 63 74 20  ender a correct 
16740 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  SQL statement in
16750 20 74 68 65 20 7a 53 51 4c 0a 2a 2a 20 76 61 72   the zSQL.** var
16760 69 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68  iable even if th
16770 65 20 7a 54 65 78 74 20 76 61 72 69 61 62 6c 65  e zText variable
16780 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
16790 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 25  er..**.** The "%
167a0 7a 22 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70  z" formatting op
167b0 74 69 6f 6e 20 77 6f 72 6b 73 20 65 78 61 63 74  tion works exact
167c0 6c 79 20 6c 69 6b 65 20 22 25 73 22 20 77 69 74  ly like "%s" wit
167d0 68 20 74 68 65 0a 2a 2a 20 61 64 64 69 74 69 6f  h the.** additio
167e0 6e 20 74 68 61 74 20 61 66 74 65 72 20 74 68 65  n that after the
167f0 20 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e   string has been
16800 20 72 65 61 64 20 61 6e 64 20 63 6f 70 69 65 64   read and copied
16810 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 72 65 73   into.** the res
16820 75 6c 74 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72  ult, [sqlite3_fr
16830 65 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20  ee()] is called 
16840 6f 6e 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  on the input str
16850 69 6e 67 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  ing. {END}.**.**
16860 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
16870 2a 20 5b 48 31 37 34 30 33 5d 20 5b 48 31 37 34  * [H17403] [H174
16880 30 36 5d 20 5b 48 31 37 34 30 37 5d 0a 2a 2f 0a  06] [H17407].*/.
16890 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
168a0 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66  *sqlite3_mprintf
168b0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e  (const char*,...
168c0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  );.SQLITE_API ch
168d0 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72  ar *sqlite3_vmpr
168e0 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a  intf(const char*
168f0 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49  , va_list);.SQLI
16900 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c  TE_API char *sql
16910 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e  ite3_snprintf(in
16920 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t,char*,const ch
16930 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a  ar*, ...);../*.*
16940 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f  * CAPI3REF: Memo
16950 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 53 75  ry Allocation Su
16960 62 73 79 73 74 65 6d 20 7b 48 31 37 33 30 30 7d  bsystem {H17300}
16970 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20000>.**.** 
16980 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20  The SQLite core 
16990 20 75 73 65 73 20 74 68 65 73 65 20 74 68 72 65   uses these thre
169a0 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61  e routines for a
169b0 6c 6c 20 6f 66 20 69 74 73 20 6f 77 6e 0a 2a 2a  ll of its own.**
169c0 20 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79   internal memory
169d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64   allocation need
169e0 73 2e 20 22 43 6f 72 65 22 20 69 6e 20 74 68 65  s. "Core" in the
169f0 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e   previous senten
16a00 63 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 69  ce.** does not i
16a10 6e 63 6c 75 64 65 20 6f 70 65 72 61 74 69 6e 67  nclude operating
16a20 2d 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 63  -system specific
16a30 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74   VFS implementat
16a40 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 57 69 6e  ion.  The.** Win
16a50 64 6f 77 73 20 56 46 53 20 75 73 65 73 20 6e 61  dows VFS uses na
16a60 74 69 76 65 20 6d 61 6c 6c 6f 63 28 29 20 61 6e  tive malloc() an
16a70 64 20 66 72 65 65 28 29 20 66 6f 72 20 73 6f 6d  d free() for som
16a80 65 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a  e operations..**
16a90 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
16aa0 6d 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65  malloc() routine
16ab0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
16ac0 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b 0a 2a 2a  er to a block.**
16ad0 20 6f 66 20 6d 65 6d 6f 72 79 20 61 74 20 6c 65   of memory at le
16ae0 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 6c  ast N bytes in l
16af0 65 6e 67 74 68 2c 20 77 68 65 72 65 20 4e 20 69  ength, where N i
16b00 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e  s the parameter.
16b10 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 6d  .** If sqlite3_m
16b20 61 6c 6c 6f 63 28 29 20 69 73 20 75 6e 61 62 6c  alloc() is unabl
16b30 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 75 66 66  e to obtain suff
16b40 69 63 69 65 6e 74 20 66 72 65 65 0a 2a 2a 20 6d  icient free.** m
16b50 65 6d 6f 72 79 2c 20 69 74 20 72 65 74 75 72 6e  emory, it return
16b60 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
16b70 2e 20 20 49 66 20 74 68 65 20 70 61 72 61 6d 65  .  If the parame
16b80 74 65 72 20 4e 20 74 6f 0a 2a 2a 20 73 71 6c 69  ter N to.** sqli
16b90 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20  te3_malloc() is 
16ba0 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65  zero or negative
16bb0 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 61   then sqlite3_ma
16bc0 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 0a 2a  lloc() returns.*
16bd0 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  * a NULL pointer
16be0 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20  ..**.** Calling 
16bf0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 77  sqlite3_free() w
16c00 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 70 72  ith a pointer pr
16c10 65 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65  eviously returne
16c20 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f  d.** by sqlite3_
16c30 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69  malloc() or sqli
16c40 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65  te3_realloc() re
16c50 6c 65 61 73 65 73 20 74 68 61 74 20 6d 65 6d 6f  leases that memo
16c60 72 79 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74  ry so.** that it
16c70 20 6d 69 67 68 74 20 62 65 20 72 65 75 73 65 64   might be reused
16c80 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66  .  The sqlite3_f
16c90 72 65 65 28 29 20 72 6f 75 74 69 6e 65 20 69 73  ree() routine is
16ca0 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 69  .** a no-op if i
16cb0 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20  s called with a 
16cc0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 50  NULL pointer.  P
16cd0 61 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f  assing a NULL po
16ce0 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 73 71 6c 69  inter.** to sqli
16cf0 74 65 33 5f 66 72 65 65 28 29 20 69 73 20 68 61  te3_free() is ha
16d00 72 6d 6c 65 73 73 2e 20 20 41 66 74 65 72 20 62  rmless.  After b
16d10 65 69 6e 67 20 66 72 65 65 64 2c 20 6d 65 6d 6f  eing freed, memo
16d20 72 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 65 69  ry.** should nei
16d30 74 68 65 72 20 62 65 20 72 65 61 64 20 6e 6f 72  ther be read nor
16d40 20 77 72 69 74 74 65 6e 2e 20 20 45 76 65 6e 20   written.  Even 
16d50 72 65 61 64 69 6e 67 20 70 72 65 76 69 6f 75 73  reading previous
16d60 6c 79 20 66 72 65 65 64 0a 2a 2a 20 6d 65 6d 6f  ly freed.** memo
16d70 72 79 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20  ry might result 
16d80 69 6e 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f  in a segmentatio
16d90 6e 20 66 61 75 6c 74 20 6f 72 20 6f 74 68 65 72  n fault or other
16da0 20 73 65 76 65 72 65 20 65 72 72 6f 72 2e 0a 2a   severe error..*
16db0 2a 20 4d 65 6d 6f 72 79 20 63 6f 72 72 75 70 74  * Memory corrupt
16dc0 69 6f 6e 2c 20 61 20 73 65 67 6d 65 6e 74 61 74  ion, a segmentat
16dd0 69 6f 6e 20 66 61 75 6c 74 2c 20 6f 72 20 6f 74  ion fault, or ot
16de0 68 65 72 20 73 65 76 65 72 65 20 65 72 72 6f 72  her severe error
16df0 0a 2a 2a 20 6d 69 67 68 74 20 72 65 73 75 6c 74  .** might result
16e00 20 69 66 20 73 71 6c 69 74 65 33 5f 66 72 65 65   if sqlite3_free
16e10 28 29 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74  () is called wit
16e20 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69  h a non-NULL poi
16e30 6e 74 65 72 20 74 68 61 74 0a 2a 2a 20 77 61 73  nter that.** was
16e40 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 66 72   not obtained fr
16e50 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
16e60 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72  c() or sqlite3_r
16e70 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20  ealloc()..**.** 
16e80 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  The sqlite3_real
16e90 6c 6f 63 28 29 20 69 6e 74 65 72 66 61 63 65 20  loc() interface 
16ea0 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 73 69  attempts to resi
16eb0 7a 65 20 61 0a 2a 2a 20 70 72 69 6f 72 20 6d 65  ze a.** prior me
16ec0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
16ed0 74 6f 20 62 65 20 61 74 20 6c 65 61 73 74 20 4e  to be at least N
16ee0 20 62 79 74 65 73 2c 20 77 68 65 72 65 20 4e 20   bytes, where N 
16ef0 69 73 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64  is the.** second
16f00 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65   parameter.  The
16f10 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
16f20 6f 6e 20 74 6f 20 62 65 20 72 65 73 69 7a 65 64  on to be resized
16f30 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   is the first.**
16f40 20 70 61 72 61 6d 65 74 65 72 2e 20 20 49 66 20   parameter.  If 
16f50 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  the first parame
16f60 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
16f70 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61  ealloc().** is a
16f80 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68   NULL pointer th
16f90 65 6e 20 69 74 73 20 62 65 68 61 76 69 6f 72 20  en its behavior 
16fa0 69 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20  is identical to 
16fb0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  calling.** sqlit
16fc0 65 33 5f 6d 61 6c 6c 6f 63 28 4e 29 20 77 68 65  e3_malloc(N) whe
16fd0 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f  re N is the seco
16fe0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
16ff0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
17000 29 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63  )..** If the sec
17010 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ond parameter to
17020 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
17030 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a  () is zero or.**
17040 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 74   negative then t
17050 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 65  he behavior is e
17060 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20  xactly the same 
17070 61 73 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71  as calling.** sq
17080 6c 69 74 65 33 5f 66 72 65 65 28 50 29 20 77 68  lite3_free(P) wh
17090 65 72 65 20 50 20 69 73 20 74 68 65 20 66 69 72  ere P is the fir
170a0 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  st parameter to 
170b0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
170c0 29 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65  )..** sqlite3_re
170d0 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 20  alloc() returns 
170e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6d  a pointer to a m
170f0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
17100 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61 73 74 20  .** of at least 
17110 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20  N bytes in size 
17120 6f 72 20 4e 55 4c 4c 20 69 66 20 73 75 66 66 69  or NULL if suffi
17130 63 69 65 6e 74 20 6d 65 6d 6f 72 79 20 69 73 20  cient memory is 
17140 75 6e 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20  unavailable..** 
17150 49 66 20 4d 20 69 73 20 74 68 65 20 73 69 7a 65  If M is the size
17160 20 6f 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c   of the prior al
17170 6c 6f 63 61 74 69 6f 6e 2c 20 74 68 65 6e 20 6d  location, then m
17180 69 6e 28 4e 2c 4d 29 20 62 79 74 65 73 0a 2a 2a  in(N,M) bytes.**
17190 20 6f 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c   of the prior al
171a0 6c 6f 63 61 74 69 6f 6e 20 61 72 65 20 63 6f 70  location are cop
171b0 69 65 64 20 69 6e 74 6f 20 74 68 65 20 62 65 67  ied into the beg
171c0 69 6e 6e 69 6e 67 20 6f 66 20 62 75 66 66 65 72  inning of buffer
171d0 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20   returned.** by 
171e0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
171f0 29 20 61 6e 64 20 74 68 65 20 70 72 69 6f 72 20  ) and the prior 
17200 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72  allocation is fr
17210 65 65 64 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74  eed..** If sqlit
17220 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74  e3_realloc() ret
17230 75 72 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  urns NULL, then 
17240 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61  the prior alloca
17250 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 66  tion.** is not f
17260 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  reed..**.** The 
17270 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 20  memory returned 
17280 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  by sqlite3_mallo
17290 63 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  c() and sqlite3_
172a0 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20  realloc().** is 
172b0 61 6c 77 61 79 73 20 61 6c 69 67 6e 65 64 20 74  always aligned t
172c0 6f 20 61 74 20 6c 65 61 73 74 20 61 6e 20 38 20  o at least an 8 
172d0 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 7b  byte boundary. {
172e0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  END}.**.** The d
172f0 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74  efault implement
17300 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d  ation of the mem
17310 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
17320 75 62 73 79 73 74 65 6d 20 75 73 65 73 0a 2a 2a  ubsystem uses.**
17330 20 74 68 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 72   the malloc(), r
17340 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65  ealloc() and fre
17350 65 28 29 20 70 72 6f 76 69 64 65 64 20 62 79 20  e() provided by 
17360 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c  the standard C l
17370 69 62 72 61 72 79 2e 0a 2a 2a 20 7b 48 31 37 33  ibrary..** {H173
17380 38 32 7d 20 48 6f 77 65 76 65 72 2c 20 69 66 20  82} However, if 
17390 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
173a0 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 53  ed with the.** S
173b0 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a  QLITE_MEMORY_SIZ
173c0 45 3d 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 43 20 70  E=<i>NNN</i> C p
173d0 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
173e0 6f 20 28 77 68 65 72 65 20 3c 69 3e 4e 4e 4e 3c  o (where <i>NNN<
173f0 2f 69 3e 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74  /i>.** is an int
17400 65 67 65 72 29 2c 20 74 68 65 6e 20 53 51 4c 69  eger), then SQLi
17410 74 65 20 63 72 65 61 74 65 20 61 20 73 74 61 74  te create a stat
17420 69 63 20 61 72 72 61 79 20 6f 66 20 61 74 20 6c  ic array of at l
17430 65 61 73 74 0a 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f  east.** <i>NNN</
17440 69 3e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  i> bytes in size
17450 20 61 6e 64 20 75 73 65 73 20 74 68 61 74 20 61   and uses that a
17460 72 72 61 79 20 66 6f 72 20 61 6c 6c 20 6f 66 20  rray for all of 
17470 69 74 73 20 64 79 6e 61 6d 69 63 0a 2a 2a 20 6d  its dynamic.** m
17480 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
17490 20 6e 65 65 64 73 2e 20 7b 45 4e 44 7d 20 20 41   needs. {END}  A
174a0 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79  dditional memory
174b0 20 61 6c 6c 6f 63 61 74 6f 72 20 6f 70 74 69 6f   allocator optio
174c0 6e 73 0a 2a 2a 20 6d 61 79 20 62 65 20 61 64 64  ns.** may be add
174d0 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c  ed in future rel
174e0 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  eases..**.** In 
174f0 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33  SQLite version 3
17500 2e 35 2e 30 20 61 6e 64 20 33 2e 35 2e 31 2c 20  .5.0 and 3.5.1, 
17510 69 74 20 77 61 73 20 70 6f 73 73 69 62 6c 65 20  it was possible 
17520 74 6f 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65  to define.** the
17530 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d   SQLITE_OMIT_MEM
17540 4f 52 59 5f 41 4c 4c 4f 43 41 54 49 4f 4e 20 77  ORY_ALLOCATION w
17550 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65  hich would cause
17560 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a   the built-in.**
17570 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
17580 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  of these routine
17590 73 20 74 6f 20 62 65 20 6f 6d 69 74 74 65 64 2e  s to be omitted.
175a0 20 20 54 68 61 74 20 63 61 70 61 62 69 6c 69 74    That capabilit
175b0 79 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  y.** is no longe
175c0 72 20 70 72 6f 76 69 64 65 64 2e 20 20 4f 6e 6c  r provided.  Onl
175d0 79 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72  y built-in memor
175e0 79 20 61 6c 6c 6f 63 61 74 6f 72 73 20 63 61 6e  y allocators can
175f0 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   be used..**.** 
17600 54 68 65 20 57 69 6e 64 6f 77 73 20 4f 53 20 69  The Windows OS i
17610 6e 74 65 72 66 61 63 65 20 6c 61 79 65 72 20 63  nterface layer c
17620 61 6c 6c 73 0a 2a 2a 20 74 68 65 20 73 79 73 74  alls.** the syst
17630 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  em malloc() and 
17640 66 72 65 65 28 29 20 64 69 72 65 63 74 6c 79 20  free() directly 
17650 77 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 0a  when converting.
17660 2a 2a 20 66 69 6c 65 6e 61 6d 65 73 20 62 65 74  ** filenames bet
17670 77 65 65 6e 20 74 68 65 20 55 54 46 2d 38 20 65  ween the UTF-8 e
17680 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20  ncoding used by 
17690 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 77 68  SQLite.** and wh
176a0 61 74 65 76 65 72 20 66 69 6c 65 6e 61 6d 65 20  atever filename 
176b0 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64  encoding is used
176c0 20 62 79 20 74 68 65 20 70 61 72 74 69 63 75 6c   by the particul
176d0 61 72 20 57 69 6e 64 6f 77 73 0a 2a 2a 20 69 6e  ar Windows.** in
176e0 73 74 61 6c 6c 61 74 69 6f 6e 2e 20 20 4d 65 6d  stallation.  Mem
176f0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
17700 72 72 6f 72 73 20 61 72 65 20 64 65 74 65 63 74  rrors are detect
17710 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 79 20  ed, but.** they 
17720 61 72 65 20 72 65 70 6f 72 74 65 64 20 62 61 63  are reported bac
17730 6b 20 61 73 20 5b 53 51 4c 49 54 45 5f 43 41 4e  k as [SQLITE_CAN
17740 54 4f 50 45 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51  TOPEN] or.** [SQ
17750 4c 49 54 45 5f 49 4f 45 52 52 5d 20 72 61 74 68  LITE_IOERR] rath
17760 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f  er than [SQLITE_
17770 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65  NOMEM]..**.** Re
17780 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
17790 48 31 37 33 30 33 5d 20 5b 48 31 37 33 30 34 5d  H17303] [H17304]
177a0 20 5b 48 31 37 33 30 35 5d 20 5b 48 31 37 33 30   [H17305] [H1730
177b0 36 5d 20 5b 48 31 37 33 31 30 5d 20 5b 48 31 37  6] [H17310] [H17
177c0 33 31 32 5d 20 5b 48 31 37 33 31 35 5d 20 5b 48  312] [H17315] [H
177d0 31 37 33 31 38 5d 0a 2a 2a 20 5b 48 31 37 33 32  17318].** [H1732
177e0 31 5d 20 5b 48 31 37 33 32 32 5d 20 5b 48 31 37  1] [H17322] [H17
177f0 33 32 33 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  323].**.** The p
17800 6f 69 6e 74 65 72 20 61 72 67 75 6d 65 6e 74 73  ointer arguments
17810 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65   to [sqlite3_fre
17820 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  e()] and [sqlite
17830 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20  3_realloc()].** 
17840 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e  must be either N
17850 55 4c 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e  ULL or else poin
17860 74 65 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72  ters obtained fr
17870 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 69 6e  om a prior.** in
17880 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c  vocation of [sql
17890 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f  ite3_malloc()] o
178a0 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  r [sqlite3_reall
178b0 6f 63 28 29 5d 20 74 68 61 74 20 68 61 76 65 0a  oc()] that have.
178c0 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20  ** not yet been 
178d0 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  released..**.** 
178e0 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
178f0 6d 75 73 74 20 6e 6f 74 20 72 65 61 64 20 6f 72  must not read or
17900 20 77 72 69 74 65 20 61 6e 79 20 70 61 72 74 20   write any part 
17910 6f 66 0a 2a 2a 20 61 20 62 6c 6f 63 6b 20 6f 66  of.** a block of
17920 20 6d 65 6d 6f 72 79 20 61 66 74 65 72 20 69 74   memory after it
17930 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73   has been releas
17940 65 64 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c  ed using.** [sql
17950 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 72 20  ite3_free()] or 
17960 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
17970 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ()]..*/.SQLITE_A
17980 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
17990 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51  _malloc(int);.SQ
179a0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
179b0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76  qlite3_realloc(v
179c0 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  oid*, int);.SQLI
179d0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
179e0 74 65 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 3b  te3_free(void*);
179f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
17a00 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  : Memory Allocat
17a10 6f 72 20 53 74 61 74 69 73 74 69 63 73 20 7b 48  or Statistics {H
17a20 31 37 33 37 30 7d 20 3c 53 33 30 32 31 30 3e 0a  17370} <S30210>.
17a30 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f  **.** SQLite pro
17a40 76 69 64 65 73 20 74 68 65 73 65 20 74 77 6f 20  vides these two 
17a50 69 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 72  interfaces for r
17a60 65 70 6f 72 74 69 6e 67 20 6f 6e 20 74 68 65 20  eporting on the 
17a70 73 74 61 74 75 73 0a 2a 2a 20 6f 66 20 74 68 65  status.** of the
17a80 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
17a90 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72  ()], [sqlite3_fr
17aa0 65 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69  ee()], and [sqli
17ab0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a  te3_realloc()].*
17ac0 2a 20 72 6f 75 74 69 6e 65 73 2c 20 77 68 69 63  * routines, whic
17ad0 68 20 66 6f 72 6d 20 74 68 65 20 62 75 69 6c 74  h form the built
17ae0 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  -in memory alloc
17af0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e  ation subsystem.
17b00 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
17b10 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 37 31 5d  nts:.** [H17371]
17b20 20 5b 48 31 37 33 37 33 5d 20 5b 48 31 37 33 37   [H17373] [H1737
17b30 34 5d 20 5b 48 31 37 33 37 35 5d 0a 2a 2f 0a 53  4] [H17375].*/.S
17b40 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
17b50 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
17b60 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64  memory_used(void
17b70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  );.SQLITE_API sq
17b80 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69  lite3_int64 sqli
17b90 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77  te3_memory_highw
17ba0 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c  ater(int resetFl
17bb0 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ag);../*.** CAPI
17bc0 33 52 45 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e  3REF: Pseudo-Ran
17bd0 64 6f 6d 20 4e 75 6d 62 65 72 20 47 65 6e 65 72  dom Number Gener
17be0 61 74 6f 72 20 7b 48 31 37 33 39 30 7d 20 3c 53  ator {H17390} <S
17bf0 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c  20000>.**.** SQL
17c00 69 74 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 68  ite contains a h
17c10 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75  igh-quality pseu
17c20 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  do-random number
17c30 20 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47   generator (PRNG
17c40 29 20 75 73 65 64 20 74 6f 0a 2a 2a 20 73 65 6c  ) used to.** sel
17c50 65 63 74 20 72 61 6e 64 6f 6d 20 5b 52 4f 57 49  ect random [ROWI
17c60 44 20 7c 20 52 4f 57 49 44 73 5d 20 77 68 65 6e  D | ROWIDs] when
17c70 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 72   inserting new r
17c80 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 74 61  ecords into a ta
17c90 62 6c 65 20 74 68 61 74 0a 2a 2a 20 61 6c 72 65  ble that.** alre
17ca0 61 64 79 20 75 73 65 73 20 74 68 65 20 6c 61 72  ady uses the lar
17cb0 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 5b 52  gest possible [R
17cc0 4f 57 49 44 5d 2e 20 20 54 68 65 20 50 52 4e 47  OWID].  The PRNG
17cd0 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 66 6f   is also used fo
17ce0 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69  r.** the build-i
17cf0 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e 64 20 72  n random() and r
17d00 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53 51 4c 20  andomblob() SQL 
17d10 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73  functions.  This
17d20 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f 77   interface allow
17d30 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  s.** application
17d40 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  s to access the 
17d50 73 61 6d 65 20 50 52 4e 47 20 66 6f 72 20 6f 74  same PRNG for ot
17d60 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2a  her purposes..**
17d70 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68  .** A call to th
17d80 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65  is routine store
17d90 73 20 4e 20 62 79 74 65 73 20 6f 66 20 72 61 6e  s N bytes of ran
17da0 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 62 75 66  domness into buf
17db0 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  fer P..**.** The
17dc0 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73   first time this
17dd0 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f   routine is invo
17de0 6b 65 64 20 28 65 69 74 68 65 72 20 69 6e 74 65  ked (either inte
17df0 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a 2a 2a 20  rnally or by.** 
17e00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 29  the application)
17e10 20 74 68 65 20 50 52 4e 47 20 69 73 20 73 65 65   the PRNG is see
17e20 64 65 64 20 75 73 69 6e 67 20 72 61 6e 64 6f 6d  ded using random
17e30 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  ness obtained.**
17e40 20 66 72 6f 6d 20 74 68 65 20 78 52 61 6e 64 6f   from the xRando
17e50 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20  mness method of 
17e60 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c  the default [sql
17e70 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
17e80 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75 62 73  ..** On all subs
17e90 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f  equent invocatio
17ea0 6e 73 2c 20 74 68 65 20 70 73 65 75 64 6f 2d 72  ns, the pseudo-r
17eb0 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20 67 65 6e  andomness is gen
17ec0 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74 65 72 6e  erated.** intern
17ed0 61 6c 6c 79 20 61 6e 64 20 77 69 74 68 6f 75 74  ally and without
17ee0 20 72 65 63 6f 75 72 73 65 20 74 6f 20 74 68 65   recourse to the
17ef0 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78   [sqlite3_vfs] x
17f00 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65  Randomness.** me
17f10 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  thod..**.** Requ
17f20 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
17f30 37 33 39 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  7392].*/.SQLITE_
17f40 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
17f50 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20  _randomness(int 
17f60 4e 2c 20 76 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a  N, void *P);../*
17f70 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f  .** CAPI3REF: Co
17f80 6d 70 69 6c 65 2d 54 69 6d 65 20 41 75 74 68 6f  mpile-Time Autho
17f90 72 69 7a 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63  rization Callbac
17fa0 6b 73 20 7b 48 31 32 35 30 30 7d 20 3c 53 37 30  ks {H12500} <S70
17fb0 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  100>.**.** This 
17fc0 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74 65 72  routine register
17fd0 73 20 61 20 61 75 74 68 6f 72 69 7a 65 72 20 63  s a authorizer c
17fe0 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 61 20 70  allback with a p
17ff0 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61  articular.** [da
18000 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
18010 6e 5d 2c 20 73 75 70 70 6c 69 65 64 20 69 6e 20  n], supplied in 
18020 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
18030 6e 74 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  nt..** The autho
18040 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
18050 73 20 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c  s invoked as SQL
18060 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
18070 62 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a  being compiled.*
18080 2a 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72  * by [sqlite3_pr
18090 65 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20  epare()] or its 
180a0 76 61 72 69 61 6e 74 73 20 5b 73 71 6c 69 74 65  variants [sqlite
180b0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c  3_prepare_v2()],
180c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
180d0 70 61 72 65 31 36 28 29 5d 20 61 6e 64 20 5b 73  pare16()] and [s
180e0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
180f0 5f 76 32 28 29 5d 2e 20 20 41 74 20 76 61 72 69  _v2()].  At vari
18100 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75  ous.** points du
18110 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61  ring the compila
18120 74 69 6f 6e 20 70 72 6f 63 65 73 73 2c 20 61 73  tion process, as
18130 20 6c 6f 67 69 63 20 69 73 20 62 65 69 6e 67 20   logic is being 
18140 63 72 65 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65  created.** to pe
18150 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 61 63  rform various ac
18160 74 69 6f 6e 73 2c 20 74 68 65 20 61 75 74 68 6f  tions, the autho
18170 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
18180 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20  s invoked to.** 
18190 73 65 65 20 69 66 20 74 68 6f 73 65 20 61 63 74  see if those act
181a0 69 6f 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64  ions are allowed
181b0 2e 20 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65  .  The authorize
181c0 72 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c  r callback shoul
181d0 64 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c  d.** return [SQL
181e0 49 54 45 5f 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77  ITE_OK] to allow
181f0 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 5b 53 51   the action, [SQ
18200 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 6f 20  LITE_IGNORE] to 
18210 64 69 73 61 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20  disallow the.** 
18220 73 70 65 63 69 66 69 63 20 61 63 74 69 6f 6e 20  specific action 
18230 62 75 74 20 61 6c 6c 6f 77 20 74 68 65 20 53 51  but allow the SQ
18240 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 63  L statement to c
18250 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 0a 2a 2a  ontinue to be.**
18260 20 63 6f 6d 70 69 6c 65 64 2c 20 6f 72 20 5b 53   compiled, or [S
18270 51 4c 49 54 45 5f 44 45 4e 59 5d 20 74 6f 20 63  QLITE_DENY] to c
18280 61 75 73 65 20 74 68 65 20 65 6e 74 69 72 65 20  ause the entire 
18290 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  SQL statement to
182a0 20 62 65 0a 2a 2a 20 72 65 6a 65 63 74 65 64 20   be.** rejected 
182b0 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20  with an error.  
182c0 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  If the authorize
182d0 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  r callback retur
182e0 6e 73 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65 20  ns.** any value 
182f0 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49  other than [SQLI
18300 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c  TE_IGNORE], [SQL
18310 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c  ITE_OK], or [SQL
18320 49 54 45 5f 44 45 4e 59 5d 0a 2a 2a 20 74 68 65  ITE_DENY].** the
18330 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70  n the [sqlite3_p
18340 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
18350 65 71 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20  equivalent call 
18360 74 68 61 74 20 74 72 69 67 67 65 72 65 64 0a 2a  that triggered.*
18370 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  * the authorizer
18380 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20   will fail with 
18390 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
183a0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
183b0 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
183c0 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74  s [SQLITE_OK], t
183d0 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 6f 70  hat means the op
183e0 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65 71 75 65  eration.** reque
183f0 73 74 65 64 20 69 73 20 6f 6b 2e 20 20 57 68 65  sted is ok.  Whe
18400 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72  n the callback r
18410 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44  eturns [SQLITE_D
18420 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 5b 73 71  ENY], the.** [sq
18430 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
18440 28 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e  ()] or equivalen
18450 74 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67  t call that trig
18460 67 65 72 65 64 20 74 68 65 0a 2a 2a 20 61 75 74  gered the.** aut
18470 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69  horizer will fai
18480 6c 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  l with an error 
18490 6d 65 73 73 61 67 65 20 65 78 70 6c 61 69 6e 69  message explaini
184a0 6e 67 20 74 68 61 74 0a 2a 2a 20 61 63 63 65 73  ng that.** acces
184b0 73 20 69 73 20 64 65 6e 69 65 64 2e 20 0a 2a 2a  s is denied. .**
184c0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
184d0 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61  rameter to the a
184e0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
184f0 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  ck is a copy of 
18500 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72  the third.** par
18510 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
18520 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
18530 69 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65  izer() interface
18540 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72  . The second par
18550 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ameter.** to the
18560 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20   callback is an 
18570 69 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f  integer [SQLITE_
18580 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f  COPY | action co
18590 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69  de] that specifi
185a0 65 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63  es.** the partic
185b0 75 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62  ular action to b
185c0 65 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68  e authorized. Th
185d0 65 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20  e third through 
185e0 73 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73  sixth parameters
185f0 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  .** to the callb
18600 61 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72  ack are zero-ter
18610 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20  minated strings 
18620 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64  that contain add
18630 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69  itional.** detai
18640 6c 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74  ls about the act
18650 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72  ion to be author
18660 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ized..**.** If t
18670 68 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69  he action code i
18680 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 0a  s [SQLITE_READ].
18690 2a 2a 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 62  ** and the callb
186a0 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  ack returns [SQL
186b0 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e  ITE_IGNORE] then
186c0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
186d0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 73 74 61  d statement] sta
186e0 74 65 6d 65 6e 74 20 69 73 20 63 6f 6e 73 74 72  tement is constr
186f0 75 63 74 65 64 20 74 6f 20 73 75 62 73 74 69 74  ucted to substit
18700 75 74 65 0a 2a 2a 20 61 20 4e 55 4c 4c 20 76 61  ute.** a NULL va
18710 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 6f 66 20  lue in place of 
18720 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  the table column
18730 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65   that would have
18740 0a 2a 2a 20 62 65 65 6e 20 72 65 61 64 20 69 66  .** been read if
18750 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 68 61 64   [SQLITE_OK] had
18760 20 62 65 65 6e 20 72 65 74 75 72 6e 65 64 2e 20   been returned. 
18770 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e   The [SQLITE_IGN
18780 4f 52 45 5d 0a 2a 2a 20 72 65 74 75 72 6e 20 63  ORE].** return c
18790 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 65  an be used to de
187a0 6e 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20  ny an untrusted 
187b0 75 73 65 72 20 61 63 63 65 73 73 20 74 6f 20 69  user access to i
187c0 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c  ndividual.** col
187d0 75 6d 6e 73 20 6f 66 20 61 20 74 61 62 6c 65 2e  umns of a table.
187e0 0a 2a 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f  .** If the actio
187f0 6e 20 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54  n code is [SQLIT
18800 45 5f 44 45 4c 45 54 45 5d 20 61 6e 64 20 74 68  E_DELETE] and th
18810 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  e callback retur
18820 6e 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 47  ns.** [SQLITE_IG
18830 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65 20 5b  NORE] then the [
18840 44 45 4c 45 54 45 5d 20 6f 70 65 72 61 74 69 6f  DELETE] operatio
18850 6e 20 70 72 6f 63 65 65 64 73 20 62 75 74 20 74  n proceeds but t
18860 68 65 0a 2a 2a 20 5b 74 72 75 6e 63 61 74 65 20  he.** [truncate 
18870 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 5d 20 69 73  optimization] is
18880 20 64 69 73 61 62 6c 65 64 20 61 6e 64 20 61 6c   disabled and al
18890 6c 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74  l rows are delet
188a0 65 64 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 2e  ed individually.
188b0 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75 74 68 6f 72  .**.** An author
188c0 69 7a 65 72 20 69 73 20 75 73 65 64 20 77 68 65  izer is used whe
188d0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  n [sqlite3_prepa
188e0 72 65 20 7c 20 70 72 65 70 61 72 69 6e 67 5d 0a  re | preparing].
188f0 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ** SQL statement
18900 73 20 66 72 6f 6d 20 61 6e 20 75 6e 74 72 75 73  s from an untrus
18910 74 65 64 20 73 6f 75 72 63 65 2c 20 74 6f 20 65  ted source, to e
18920 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 53  nsure that the S
18930 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  QL statements.**
18940 20 64 6f 20 6e 6f 74 20 74 72 79 20 74 6f 20 61   do not try to a
18950 63 63 65 73 73 20 64 61 74 61 20 74 68 65 79 20  ccess data they 
18960 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20  are not allowed 
18970 74 6f 20 73 65 65 2c 20 6f 72 20 74 68 61 74 20  to see, or that 
18980 74 68 65 79 20 64 6f 20 6e 6f 74 0a 2a 2a 20 74  they do not.** t
18990 72 79 20 74 6f 20 65 78 65 63 75 74 65 20 6d 61  ry to execute ma
189a0 6c 69 63 69 6f 75 73 20 73 74 61 74 65 6d 65 6e  licious statemen
189b0 74 73 20 74 68 61 74 20 64 61 6d 61 67 65 20 74  ts that damage t
189c0 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 46 6f  he database.  Fo
189d0 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 6e  r.** example, an
189e0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79   application may
189f0 20 61 6c 6c 6f 77 20 61 20 75 73 65 72 20 74 6f   allow a user to
18a00 20 65 6e 74 65 72 20 61 72 62 69 74 72 61 72 79   enter arbitrary
18a10 0a 2a 2a 20 53 51 4c 20 71 75 65 72 69 65 73 20  .** SQL queries 
18a20 66 6f 72 20 65 76 61 6c 75 61 74 69 6f 6e 20 62  for evaluation b
18a30 79 20 61 20 64 61 74 61 62 61 73 65 2e 20 20 42  y a database.  B
18a40 75 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ut the applicati
18a50 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77  on does.** not w
18a60 61 6e 74 20 74 68 65 20 75 73 65 72 20 74 6f 20  ant the user to 
18a70 62 65 20 61 62 6c 65 20 74 6f 20 6d 61 6b 65 20  be able to make 
18a80 61 72 62 69 74 72 61 72 79 20 63 68 61 6e 67 65  arbitrary change
18a90 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61  s to the.** data
18aa0 62 61 73 65 2e 20 20 41 6e 20 61 75 74 68 6f 72  base.  An author
18ab0 69 7a 65 72 20 63 6f 75 6c 64 20 74 68 65 6e 20  izer could then 
18ac0 62 65 20 70 75 74 20 69 6e 20 70 6c 61 63 65 20  be put in place 
18ad0 77 68 69 6c 65 20 74 68 65 0a 2a 2a 20 75 73 65  while the.** use
18ae0 72 2d 65 6e 74 65 72 65 64 20 53 51 4c 20 69 73  r-entered SQL is
18af0 20 62 65 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   being [sqlite3_
18b00 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72  prepare | prepar
18b10 65 64 5d 20 74 68 61 74 0a 2a 2a 20 64 69 73 61  ed] that.** disa
18b20 6c 6c 6f 77 73 20 65 76 65 72 79 74 68 69 6e 67  llows everything
18b30 20 65 78 63 65 70 74 20 5b 53 45 4c 45 43 54 5d   except [SELECT]
18b40 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
18b50 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  ** Applications 
18b60 74 68 61 74 20 6e 65 65 64 20 74 6f 20 70 72 6f  that need to pro
18b70 63 65 73 73 20 53 51 4c 20 66 72 6f 6d 20 75 6e  cess SQL from un
18b80 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 73 0a  trusted sources.
18b90 2a 2a 20 6d 69 67 68 74 20 61 6c 73 6f 20 63 6f  ** might also co
18ba0 6e 73 69 64 65 72 20 6c 6f 77 65 72 69 6e 67 20  nsider lowering 
18bb0 72 65 73 6f 75 72 63 65 20 6c 69 6d 69 74 73 20  resource limits 
18bc0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c  using [sqlite3_l
18bd0 69 6d 69 74 28 29 5d 0a 2a 2a 20 61 6e 64 20 6c  imit()].** and l
18be0 69 6d 69 74 69 6e 67 20 64 61 74 61 62 61 73 65  imiting database
18bf0 20 73 69 7a 65 20 75 73 69 6e 67 20 74 68 65 20   size using the 
18c00 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 5d  [max_page_count]
18c10 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a 20 69 6e 20   [PRAGMA].** in 
18c20 61 64 64 69 74 69 6f 6e 20 74 6f 20 75 73 69 6e  addition to usin
18c30 67 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 2e  g an authorizer.
18c40 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 61 20 73 69  .**.** Only a si
18c50 6e 67 6c 65 20 61 75 74 68 6f 72 69 7a 65 72 20  ngle authorizer 
18c60 63 61 6e 20 62 65 20 69 6e 20 70 6c 61 63 65 20  can be in place 
18c70 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  on a database co
18c80 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 74 20 61  nnection.** at a
18c90 20 74 69 6d 65 2e 20 20 45 61 63 68 20 63 61 6c   time.  Each cal
18ca0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74  l to sqlite3_set
18cb0 5f 61 75 74 68 6f 72 69 7a 65 72 20 6f 76 65 72  _authorizer over
18cc0 72 69 64 65 73 20 74 68 65 0a 2a 2a 20 70 72 65  rides the.** pre
18cd0 76 69 6f 75 73 20 63 61 6c 6c 2e 20 20 44 69 73  vious call.  Dis
18ce0 61 62 6c 65 20 74 68 65 20 61 75 74 68 6f 72 69  able the authori
18cf0 7a 65 72 20 62 79 20 69 6e 73 74 61 6c 6c 69 6e  zer by installin
18d00 67 20 61 20 4e 55 4c 4c 20 63 61 6c 6c 62 61 63  g a NULL callbac
18d10 6b 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  k..** The author
18d20 69 7a 65 72 20 69 73 20 64 69 73 61 62 6c 65 64  izer is disabled
18d30 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a   by default..**.
18d40 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65  ** The authorize
18d50 72 20 63 61 6c 6c 62 61 63 6b 20 6d 75 73 74 20  r callback must 
18d60 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20  not do anything 
18d70 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79  that will modify
18d80 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
18d90 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
18da0 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 61 75 74   invoked the aut
18db0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
18dc0 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b  ..** Note that [
18dd0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
18de0 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  v2()] and [sqlit
18df0 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20  e3_step()] both 
18e00 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20  modify their.** 
18e10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
18e20 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61  ions for the mea
18e30 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22  ning of "modify"
18e40 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61   in this paragra
18e50 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 5b  ph..**.** When [
18e60 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
18e70 76 32 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f  v2()] is used to
18e80 20 70 72 65 70 61 72 65 20 61 20 73 74 61 74 65   prepare a state
18e90 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a 20 73 74 61  ment, the.** sta
18ea0 74 65 6d 65 6e 74 20 6d 69 67 68 74 20 62 65 20  tement might be 
18eb0 72 65 70 72 65 70 61 72 65 64 20 64 75 72 69 6e  reprepared durin
18ec0 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  g [sqlite3_step(
18ed0 29 5d 20 64 75 65 20 74 6f 20 61 20 0a 2a 2a 20  )] due to a .** 
18ee0 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20 20  schema change.  
18ef0 48 65 6e 63 65 2c 20 74 68 65 20 61 70 70 6c 69  Hence, the appli
18f00 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 65 6e  cation should en
18f10 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a  sure that the.**
18f20 20 63 6f 72 72 65 63 74 20 61 75 74 68 6f 72 69   correct authori
18f30 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 6d  zer callback rem
18f40 61 69 6e 73 20 69 6e 20 70 6c 61 63 65 20 64 75  ains in place du
18f50 72 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65  ring the [sqlite
18f60 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a  3_step()]..**.**
18f70 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61   Note that the a
18f80 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
18f90 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  ck is invoked on
18fa0 6c 79 20 64 75 72 69 6e 67 0a 2a 2a 20 5b 73 71  ly during.** [sq
18fb0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d  lite3_prepare()]
18fc0 20 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73   or its variants
18fd0 2e 20 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e  .  Authorization
18fe0 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f   is not.** perfo
18ff0 72 6d 65 64 20 64 75 72 69 6e 67 20 73 74 61 74  rmed during stat
19000 65 6d 65 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e  ement evaluation
19010 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   in [sqlite3_ste
19020 70 28 29 5d 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20  p()], unless.** 
19030 61 73 20 73 74 61 74 65 64 20 69 6e 20 74 68 65  as stated in the
19040 20 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72   previous paragr
19050 61 70 68 2c 20 73 71 6c 69 74 65 33 5f 73 74 65  aph, sqlite3_ste
19060 70 28 29 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73  p() invokes.** s
19070 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
19080 32 28 29 20 74 6f 20 72 65 70 72 65 70 61 72 65  2() to reprepare
19090 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74   a statement aft
190a0 65 72 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e  er a schema chan
190b0 67 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ge..**.** Requir
190c0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35  ements:.** [H125
190d0 30 31 5d 20 5b 48 31 32 35 30 32 5d 20 5b 48 31  01] [H12502] [H1
190e0 32 35 30 33 5d 20 5b 48 31 32 35 30 34 5d 20 5b  2503] [H12504] [
190f0 48 31 32 35 30 35 5d 20 5b 48 31 32 35 30 36 5d  H12505] [H12506]
19100 20 5b 48 31 32 35 30 37 5d 20 5b 48 31 32 35 31   [H12507] [H1251
19110 30 5d 0a 2a 2a 20 5b 48 31 32 35 31 31 5d 20 5b  0].** [H12511] [
19120 48 31 32 35 31 32 5d 20 5b 48 31 32 35 32 30 5d  H12512] [H12520]
19130 20 5b 48 31 32 35 32 31 5d 20 5b 48 31 32 35 32   [H12521] [H1252
19140 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  2].*/.SQLITE_API
19150 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74   int sqlite3_set
19160 5f 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73  _authorizer(.  s
19170 71 6c 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28  qlite3*,.  int (
19180 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e  *xAuth)(void*,in
19190 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  t,const char*,co
191a0 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
191b0 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
191c0 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65  *),.  void *pUse
191d0 72 44 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  rData.);../*.** 
191e0 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72  CAPI3REF: Author
191f0 69 7a 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65  izer Return Code
19200 73 20 7b 48 31 32 35 39 30 7d 20 3c 48 31 32 35  s {H12590} <H125
19210 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73  00>.**.** The [s
19220 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
19230 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a  rizer | authoriz
19240 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  er callback func
19250 74 69 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65  tion] must.** re
19260 74 75 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c  turn either [SQL
19270 49 54 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f  ITE_OK] or one o
19280 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73  f these two cons
19290 74 61 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a  tants in order.*
192a0 2a 20 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69  * to signal SQLi
192b0 74 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  te whether or no
192c0 74 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20  t the action is 
192d0 70 65 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20  permitted.  See 
192e0 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  the.** [sqlite3_
192f0 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c  set_authorizer |
19300 20 61 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75   authorizer docu
19310 6d 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61  mentation] for a
19320 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66  dditional.** inf
19330 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65  ormation..*/.#de
19340 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e 59  fine SQLITE_DENY
19350 20 20 20 31 20 20 20 2f 2a 20 41 62 6f 72 74 20     1   /* Abort 
19360 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
19370 74 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  t with an error 
19380 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
19390 45 5f 49 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20  E_IGNORE 2   /* 
193a0 44 6f 6e 27 74 20 61 6c 6c 6f 77 20 61 63 63 65  Don't allow acce
193b0 73 73 2c 20 62 75 74 20 64 6f 6e 27 74 20 67 65  ss, but don't ge
193c0 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20  nerate an error 
193d0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  */../*.** CAPI3R
193e0 45 46 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 41  EF: Authorizer A
193f0 63 74 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32  ction Codes {H12
19400 35 35 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a  550} <H12500>.**
19410 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
19420 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
19430 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67  )] interface reg
19440 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63  isters a callbac
19450 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68  k function.** th
19460 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f  at is invoked to
19470 20 61 75 74 68 6f 72 69 7a 65 20 63 65 72 74 61   authorize certa
19480 69 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  in SQL statement
19490 20 61 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a   actions.  The.*
194a0 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  * second paramet
194b0 65 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  er to the callba
194c0 63 6b 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  ck is an integer
194d0 20 63 6f 64 65 20 74 68 61 74 20 73 70 65 63 69   code that speci
194e0 66 69 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 74  fies.** what act
194f0 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 61 75 74  ion is being aut
19500 68 6f 72 69 7a 65 64 2e 20 20 54 68 65 73 65 20  horized.  These 
19510 61 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20  are the integer 
19520 61 63 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 61  action codes tha
19530 74 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69  t.** the authori
19540 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79  zer callback may
19550 20 62 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a   be passed..**.*
19560 2a 20 54 68 65 73 65 20 61 63 74 69 6f 6e 20 63  * These action c
19570 6f 64 65 20 76 61 6c 75 65 73 20 73 69 67 6e 69  ode values signi
19580 66 79 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20  fy what kind of 
19590 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 74 6f 20  operation is to 
195a0 62 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64  be.** authorized
195b0 2e 20 20 54 68 65 20 33 72 64 20 61 6e 64 20 34  .  The 3rd and 4
195c0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
195d0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69   the authorizati
195e0 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66  on.** callback f
195f0 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  unction will be 
19600 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 4e 55  parameters or NU
19610 4c 4c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  LL depending on 
19620 77 68 69 63 68 20 6f 66 20 74 68 65 73 65 0a 2a  which of these.*
19630 2a 20 63 6f 64 65 73 20 69 73 20 75 73 65 64 20  * codes is used 
19640 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  as the second pa
19650 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 35 74  rameter.  The 5t
19660 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
19670 68 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72  he.** authorizer
19680 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65   callback is the
19690 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
196a0 61 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22  abase ("main", "
196b0 74 65 6d 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20  temp",.** etc.) 
196c0 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20  if applicable.  
196d0 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65  The 6th paramete
196e0 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69  r to the authori
196f0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20  zer callback.** 
19700 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
19710 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72  he inner-most tr
19720 69 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68  igger or view th
19730 61 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  at is responsibl
19740 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63  e for.** the acc
19750 65 73 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e  ess attempt or N
19760 55 4c 4c 20 69 66 20 74 68 69 73 20 61 63 63 65  ULL if this acce
19770 73 73 20 61 74 74 65 6d 70 74 20 69 73 20 64 69  ss attempt is di
19780 72 65 63 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74  rectly from.** t
19790 6f 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64  op-level SQL cod
197a0 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  e..**.** Require
197b0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 35  ments:.** [H1255
197c0 31 5d 20 5b 48 31 32 35 35 32 5d 20 5b 48 31 32  1] [H12552] [H12
197d0 35 35 33 5d 20 5b 48 31 32 35 35 34 5d 0a 2a 2f  553] [H12554].*/
197e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
197f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72  ************* 3r
19810 64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34  d ************ 4
19820 74 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  th ***********/.
19830 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
19840 52 45 41 54 45 5f 49 4e 44 45 58 20 20 20 20 20  REATE_INDEX     
19850 20 20 20 20 20 31 20 20 20 2f 2a 20 49 6e 64 65       1   /* Inde
19860 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c  x Name      Tabl
19870 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
19880 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
19890 45 41 54 45 5f 54 41 42 4c 45 20 20 20 20 20 20  EATE_TABLE      
198a0 20 20 20 20 32 20 20 20 2f 2a 20 54 61 62 6c 65      2   /* Table
198b0 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20   Name      NULL 
198c0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
198d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45  efine SQLITE_CRE
198e0 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20  ATE_TEMP_INDEX  
198f0 20 20 20 33 20 20 20 2f 2a 20 49 6e 64 65 78 20     3   /* Index 
19900 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20  Name      Table 
19910 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
19920 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41  fine SQLITE_CREA
19930 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20 20  TE_TEMP_TABLE   
19940 20 20 34 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e    4   /* Table N
19950 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  ame      NULL   
19960 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
19970 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54  ine SQLITE_CREAT
19980 45 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20 20  E_TEMP_TRIGGER  
19990 20 35 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20   5   /* Trigger 
199a0 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61  Name    Table Na
199b0 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  me      */.#defi
199c0 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
199d0 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20 20  _TEMP_VIEW      
199e0 36 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65  6   /* View Name
199f0 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20         NULL     
19a00 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
19a10 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
19a20 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 37  TRIGGER        7
19a30 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61     /* Trigger Na
19a40 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65  me    Table Name
19a50 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
19a60 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56   SQLITE_CREATE_V
19a70 49 45 57 20 20 20 20 20 20 20 20 20 20 20 38 20  IEW           8 
19a80 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20    /* View Name  
19a90 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
19aa0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
19ab0 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 20 20 20  SQLITE_DELETE   
19ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 20 20               9  
19ad0 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20   /* Table Name  
19ae0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
19af0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
19b00 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58  QLITE_DROP_INDEX
19b10 20 20 20 20 20 20 20 20 20 20 20 31 30 20 20 20             10   
19b20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20  /* Index Name   
19b30 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
19b40 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
19b50 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 20  LITE_DROP_TABLE 
19b60 20 20 20 20 20 20 20 20 20 20 31 31 20 20 20 2f            11   /
19b70 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
19b80 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
19b90 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
19ba0 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e  ITE_DROP_TEMP_IN
19bb0 44 45 58 20 20 20 20 20 20 31 32 20 20 20 2f 2a  DEX      12   /*
19bc0 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20   Index Name     
19bd0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
19be0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
19bf0 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42  TE_DROP_TEMP_TAB
19c00 4c 45 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20  LE      13   /* 
19c10 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
19c20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
19c30 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
19c40 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47  E_DROP_TEMP_TRIG
19c50 47 45 52 20 20 20 20 31 34 20 20 20 2f 2a 20 54  GER    14   /* T
19c60 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54  rigger Name    T
19c70 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a  able Name      *
19c80 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
19c90 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 20  _DROP_TEMP_VIEW 
19ca0 20 20 20 20 20 20 31 35 20 20 20 2f 2a 20 56 69        15   /* Vi
19cb0 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55  ew Name       NU
19cc0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
19cd0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
19ce0 44 52 4f 50 5f 54 52 49 47 47 45 52 20 20 20 20  DROP_TRIGGER    
19cf0 20 20 20 20 20 31 36 20 20 20 2f 2a 20 54 72 69       16   /* Tri
19d00 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62  gger Name    Tab
19d10 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a  le Name      */.
19d20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
19d30 52 4f 50 5f 56 49 45 57 20 20 20 20 20 20 20 20  ROP_VIEW        
19d40 20 20 20 20 31 37 20 20 20 2f 2a 20 56 69 65 77      17   /* View
19d50 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c   Name       NULL
19d60 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
19d70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
19d80 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20 20  SERT            
19d90 20 20 20 31 38 20 20 20 2f 2a 20 54 61 62 6c 65     18   /* Table
19da0 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20   Name      NULL 
19db0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
19dc0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 41  efine SQLITE_PRA
19dd0 47 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20  GMA             
19de0 20 20 31 39 20 20 20 2f 2a 20 50 72 61 67 6d 61    19   /* Pragma
19df0 20 4e 61 6d 65 20 20 20 20 20 31 73 74 20 61 72   Name     1st ar
19e00 67 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65  g or NULL */.#de
19e10 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44  fine SQLITE_READ
19e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e30 20 32 30 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e   20   /* Table N
19e40 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  ame      Column 
19e50 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66  Name     */.#def
19e60 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 4c 45 43  ine SQLITE_SELEC
19e70 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
19e80 32 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20  21   /* NULL    
19e90 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20          NULL    
19ea0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
19eb0 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41  ne SQLITE_TRANSA
19ec0 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 32  CTION          2
19ed0 32 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e  2   /* Operation
19ee0 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20         NULL     
19ef0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
19f00 65 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20  e SQLITE_UPDATE 
19f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33                23
19f20 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
19f30 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d        Column Nam
19f40 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  e     */.#define
19f50 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20 20   SQLITE_ATTACH  
19f60 20 20 20 20 20 20 20 20 20 20 20 20 20 32 34 20               24 
19f70 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 20 20    /* Filename   
19f80 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
19f90 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
19fa0 53 51 4c 49 54 45 5f 44 45 54 41 43 48 20 20 20  SQLITE_DETACH   
19fb0 20 20 20 20 20 20 20 20 20 20 20 20 32 35 20 20              25  
19fc0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d   /* Database Nam
19fd0 65 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20  e   NULL        
19fe0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
19ff0 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c  QLITE_ALTER_TABL
1a000 45 20 20 20 20 20 20 20 20 20 20 32 36 20 20 20  E          26   
1a010 2f 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65  /* Database Name
1a020 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
1a030 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1a040 4c 49 54 45 5f 52 45 49 4e 44 45 58 20 20 20 20  LITE_REINDEX    
1a050 20 20 20 20 20 20 20 20 20 20 32 37 20 20 20 2f            27   /
1a060 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20  * Index Name    
1a070 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1a080 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1a090 49 54 45 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20  ITE_ANALYZE     
1a0a0 20 20 20 20 20 20 20 20 20 32 38 20 20 20 2f 2a           28   /*
1a0b0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1a0c0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1a0d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1a0e0 54 45 5f 43 52 45 41 54 45 5f 56 54 41 42 4c 45  TE_CREATE_VTABLE
1a0f0 20 20 20 20 20 20 20 20 32 39 20 20 20 2f 2a 20          29   /* 
1a100 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1a110 4d 6f 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20  Module Name     
1a120 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1a130 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 20 20 20  E_DROP_VTABLE   
1a140 20 20 20 20 20 20 20 33 30 20 20 20 2f 2a 20 54         30   /* T
1a150 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d  able Name      M
1a160 6f 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a  odule Name     *
1a170 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1a180 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20  _FUNCTION       
1a190 20 20 20 20 20 20 33 31 20 20 20 2f 2a 20 4e 55        31   /* NU
1a1a0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 46 75  LL            Fu
1a1b0 6e 63 74 69 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f  nction Name   */
1a1c0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1a1d0 53 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 20  SAVEPOINT       
1a1e0 20 20 20 20 20 33 32 20 20 20 2f 2a 20 4f 70 65       32   /* Ope
1a1f0 72 61 74 69 6f 6e 20 20 20 20 20 20 20 53 61 76  ration       Sav
1a200 65 70 6f 69 6e 74 20 4e 61 6d 65 20 20 2a 2f 0a  epoint Name  */.
1a210 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
1a220 4f 50 59 20 20 20 20 20 20 20 20 20 20 20 20 20  OPY             
1a230 20 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 20 6c       0   /* No l
1a240 6f 6e 67 65 72 20 75 73 65 64 20 2a 2f 0a 0a 2f  onger used */../
1a250 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54  *.** CAPI3REF: T
1a260 72 61 63 69 6e 67 20 41 6e 64 20 50 72 6f 66 69  racing And Profi
1a270 6c 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 20 7b  ling Functions {
1a280 48 31 32 32 38 30 7d 20 3c 53 36 30 34 30 30 3e  H12280} <S60400>
1a290 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
1a2a0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
1a2b0 74 69 6e 65 73 20 72 65 67 69 73 74 65 72 20 63  tines register c
1a2c0 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
1a2d0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73  s that can be us
1a2e0 65 64 20 66 6f 72 0a 2a 2a 20 74 72 61 63 69 6e  ed for.** tracin
1a2f0 67 20 61 6e 64 20 70 72 6f 66 69 6c 69 6e 67 20  g and profiling 
1a300 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66  the execution of
1a310 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e   SQL statements.
1a320 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62  .**.** The callb
1a330 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67  ack function reg
1a340 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 74  istered by sqlit
1a350 65 33 5f 74 72 61 63 65 28 29 20 69 73 20 69 6e  e3_trace() is in
1a360 76 6f 6b 65 64 20 61 74 0a 2a 2a 20 76 61 72 69  voked at.** vari
1a370 6f 75 73 20 74 69 6d 65 73 20 77 68 65 6e 20 61  ous times when a
1a380 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
1a390 69 73 20 62 65 69 6e 67 20 72 75 6e 20 62 79 20  is being run by 
1a3a0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1a3b0 2e 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63  ..** The callbac
1a3c0 6b 20 72 65 74 75 72 6e 73 20 61 20 55 54 46 2d  k returns a UTF-
1a3d0 38 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74  8 rendering of t
1a3e0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1a3f0 20 74 65 78 74 0a 2a 2a 20 61 73 20 74 68 65 20   text.** as the 
1a400 73 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 20  statement first 
1a410 62 65 67 69 6e 73 20 65 78 65 63 75 74 69 6e 67  begins executing
1a420 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 61  .  Additional ca
1a430 6c 6c 62 61 63 6b 73 20 6f 63 63 75 72 0a 2a 2a  llbacks occur.**
1a440 20 61 73 20 65 61 63 68 20 74 72 69 67 67 65 72   as each trigger
1a450 65 64 20 73 75 62 70 72 6f 67 72 61 6d 20 69 73  ed subprogram is
1a460 20 65 6e 74 65 72 65 64 2e 20 20 54 68 65 20 63   entered.  The c
1a470 61 6c 6c 62 61 63 6b 73 20 66 6f 72 20 74 72 69  allbacks for tri
1a480 67 67 65 72 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e  ggers.** contain
1a490 20 61 20 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d   a UTF-8 SQL com
1a4a0 6d 65 6e 74 20 74 68 61 74 20 69 64 65 6e 74 69  ment that identi
1a4b0 66 69 65 73 20 74 68 65 20 74 72 69 67 67 65 72  fies the trigger
1a4c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
1a4d0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65  back function re
1a4e0 67 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69  gistered by sqli
1a4f0 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 20 69 73  te3_profile() is
1a500 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 73 20 65   invoked.** as e
1a510 61 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ach SQL statemen
1a520 74 20 66 69 6e 69 73 68 65 73 2e 20 20 54 68 65  t finishes.  The
1a530 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63   profile callbac
1a540 6b 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68  k contains.** th
1a550 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65  e original state
1a560 6d 65 6e 74 20 74 65 78 74 20 61 6e 64 20 61 6e  ment text and an
1a570 20 65 73 74 69 6d 61 74 65 20 6f 66 20 77 61 6c   estimate of wal
1a580 6c 2d 63 6c 6f 63 6b 20 74 69 6d 65 0a 2a 2a 20  l-clock time.** 
1a590 6f 66 20 68 6f 77 20 6c 6f 6e 67 20 74 68 61 74  of how long that
1a5a0 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f 6b 20   statement took 
1a5b0 74 6f 20 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65  to run..**.** Re
1a5c0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
1a5d0 48 31 32 32 38 31 5d 20 5b 48 31 32 32 38 32 5d  H12281] [H12282]
1a5e0 20 5b 48 31 32 32 38 33 5d 20 5b 48 31 32 32 38   [H12283] [H1228
1a5f0 34 5d 20 5b 48 31 32 32 38 35 5d 20 5b 48 31 32  4] [H12285] [H12
1a600 32 38 37 5d 20 5b 48 31 32 32 38 38 5d 20 5b 48  287] [H12288] [H
1a610 31 32 32 38 39 5d 0a 2a 2a 20 5b 48 31 32 32 39  12289].** [H1229
1a620 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  0].*/.SQLITE_API
1a630 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45   SQLITE_EXPERIME
1a640 4e 54 41 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74  NTAL void *sqlit
1a650 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33  e3_trace(sqlite3
1a660 2a 2c 20 76 6f 69 64 28 2a 78 54 72 61 63 65 29  *, void(*xTrace)
1a670 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
1a680 72 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  r*), void*);.SQL
1a690 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45  ITE_API SQLITE_E
1a6a0 58 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64  XPERIMENTAL void
1a6b0 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c   *sqlite3_profil
1a6c0 65 28 73 71 6c 69 74 65 33 2a 2c 0a 20 20 20 76  e(sqlite3*,.   v
1a6d0 6f 69 64 28 2a 78 50 72 6f 66 69 6c 65 29 28 76  oid(*xProfile)(v
1a6e0 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  oid*,const char*
1a6f0 2c 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 29  ,sqlite3_uint64)
1a700 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  , void*);../*.**
1a710 20 43 41 50 49 33 52 45 46 3a 20 51 75 65 72 79   CAPI3REF: Query
1a720 20 50 72 6f 67 72 65 73 73 20 43 61 6c 6c 62 61   Progress Callba
1a730 63 6b 73 20 7b 48 31 32 39 31 30 7d 20 3c 53 36  cks {H12910} <S6
1a740 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0400>.**.** This
1a750 20 72 6f 75 74 69 6e 65 20 63 6f 6e 66 69 67 75   routine configu
1a760 72 65 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66  res a callback f
1a770 75 6e 63 74 69 6f 6e 20 2d 20 74 68 65 0a 2a 2a  unction - the.**
1a780 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
1a790 63 6b 20 2d 20 74 68 61 74 20 69 73 20 69 6e 76  ck - that is inv
1a7a0 6f 6b 65 64 20 70 65 72 69 6f 64 69 63 61 6c 6c  oked periodicall
1a7b0 79 20 64 75 72 69 6e 67 20 6c 6f 6e 67 0a 2a 2a  y during long.**
1a7c0 20 72 75 6e 6e 69 6e 67 20 63 61 6c 6c 73 20 74   running calls t
1a7d0 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  o [sqlite3_exec(
1a7e0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  )], [sqlite3_ste
1a7f0 70 28 29 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  p()] and.** [sql
1a800 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29  ite3_get_table()
1a810 5d 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65 20 75  ].  An example u
1a820 73 65 20 66 6f 72 20 74 68 69 73 0a 2a 2a 20 69  se for this.** i
1a830 6e 74 65 72 66 61 63 65 20 69 73 20 74 6f 20 6b  nterface is to k
1a840 65 65 70 20 61 20 47 55 49 20 75 70 64 61 74 65  eep a GUI update
1a850 64 20 64 75 72 69 6e 67 20 61 20 6c 61 72 67 65  d during a large
1a860 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66   query..**.** If
1a870 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61   the progress ca
1a880 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e  llback returns n
1a890 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6f 70 65  on-zero, the ope
1a8a0 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 74  ration is.** int
1a8b0 65 72 72 75 70 74 65 64 2e 20 20 54 68 69 73 20  errupted.  This 
1a8c0 66 65 61 74 75 72 65 20 63 61 6e 20 62 65 20 75  feature can be u
1a8d0 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
1a8e0 20 61 0a 2a 2a 20 22 43 61 6e 63 65 6c 22 20 62   a.** "Cancel" b
1a8f0 75 74 74 6f 6e 20 6f 6e 20 61 20 47 55 49 20 70  utton on a GUI p
1a900 72 6f 67 72 65 73 73 20 64 69 61 6c 6f 67 20 62  rogress dialog b
1a910 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72  ox..**.** The pr
1a920 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 20 6d  ogress handler m
1a930 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68  ust not do anyth
1a940 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f  ing that will mo
1a950 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61  dify.** the data
1a960 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1a970 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65  that invoked the
1a980 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65   progress handle
1a990 72 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  r..** Note that 
1a9a0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1a9b0 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  _v2()] and [sqli
1a9c0 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68  te3_step()] both
1a9d0 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a   modify their.**
1a9e0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1a9f0 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65  tions for the me
1aa00 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79  aning of "modify
1aa10 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72  " in this paragr
1aa20 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  aph..**.** Requi
1aa30 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
1aa40 39 31 31 5d 20 5b 48 31 32 39 31 32 5d 20 5b 48  911] [H12912] [H
1aa50 31 32 39 31 33 5d 20 5b 48 31 32 39 31 34 5d 20  12913] [H12914] 
1aa60 5b 48 31 32 39 31 35 5d 20 5b 48 31 32 39 31 36  [H12915] [H12916
1aa70 5d 20 5b 48 31 32 39 31 37 5d 20 5b 48 31 32 39  ] [H12917] [H129
1aa80 31 38 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  18].**.*/.SQLITE
1aa90 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1aaa0 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c  3_progress_handl
1aab0 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  er(sqlite3*, int
1aac0 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c  , int(*)(void*),
1aad0 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   void*);../*.** 
1aae0 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 69 6e  CAPI3REF: Openin
1aaf0 67 20 41 20 4e 65 77 20 44 61 74 61 62 61 73 65  g A New Database
1ab00 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 32   Connection {H12
1ab10 37 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a  700} <S40200>.**
1ab20 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
1ab30 65 73 20 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74  es open an SQLit
1ab40 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1ab50 77 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 67 69  whose name is gi
1ab60 76 65 6e 20 62 79 20 74 68 65 0a 2a 2a 20 66 69  ven by the.** fi
1ab70 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 2e  lename argument.
1ab80 20 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72   The filename ar
1ab90 67 75 6d 65 6e 74 20 69 73 20 69 6e 74 65 72 70  gument is interp
1aba0 72 65 74 65 64 20 61 73 20 55 54 46 2d 38 20 66  reted as UTF-8 f
1abb0 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70  or.** sqlite3_op
1abc0 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  en() and sqlite3
1abd0 5f 6f 70 65 6e 5f 76 32 28 29 20 61 6e 64 20 61  _open_v2() and a
1abe0 73 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20  s UTF-16 in the 
1abf0 6e 61 74 69 76 65 20 62 79 74 65 0a 2a 2a 20 6f  native byte.** o
1ac00 72 64 65 72 20 66 6f 72 20 73 71 6c 69 74 65 33  rder for sqlite3
1ac10 5f 6f 70 65 6e 31 36 28 29 2e 20 41 20 5b 64 61  _open16(). A [da
1ac20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1ac30 6e 5d 20 68 61 6e 64 6c 65 20 69 73 20 75 73 75  n] handle is usu
1ac40 61 6c 6c 79 0a 2a 2a 20 72 65 74 75 72 6e 65 64  ally.** returned
1ac50 20 69 6e 20 2a 70 70 44 62 2c 20 65 76 65 6e 20   in *ppDb, even 
1ac60 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
1ac70 72 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 65 78  rs.  The only ex
1ac80 63 65 70 74 69 6f 6e 20 69 73 20 74 68 61 74 0a  ception is that.
1ac90 2a 2a 20 69 66 20 53 51 4c 69 74 65 20 69 73 20  ** if SQLite is 
1aca0 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  unable to alloca
1acb0 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c  te memory to hol
1acc0 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20  d the [sqlite3] 
1acd0 6f 62 6a 65 63 74 2c 0a 2a 2a 20 61 20 4e 55 4c  object,.** a NUL
1ace0 4c 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65  L will be writte
1acf0 6e 20 69 6e 74 6f 20 2a 70 70 44 62 20 69 6e 73  n into *ppDb ins
1ad00 74 65 61 64 20 6f 66 20 61 20 70 6f 69 6e 74 65  tead of a pointe
1ad10 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  r to the [sqlite
1ad20 33 5d 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 49 66  3].** object. If
1ad30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
1ad40 20 6f 70 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20   opened (and/or 
1ad50 63 72 65 61 74 65 64 29 20 73 75 63 63 65 73 73  created) success
1ad60 66 75 6c 6c 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b  fully, then.** [
1ad70 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65  SQLITE_OK] is re
1ad80 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69  turned.  Otherwi
1ad90 73 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64  se an [error cod
1ada0 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  e] is returned. 
1adb0 20 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   The.** [sqlite3
1adc0 5f 65 72 72 6d 73 67 28 29 5d 20 6f 72 20 5b 73  _errmsg()] or [s
1add0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
1ade0 29 5d 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20  )] routines can 
1adf0 62 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69  be used to obtai
1ae00 6e 0a 2a 2a 20 61 6e 20 45 6e 67 6c 69 73 68 20  n.** an English 
1ae10 6c 61 6e 67 75 61 67 65 20 64 65 73 63 72 69 70  language descrip
1ae20 74 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f  tion of the erro
1ae30 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66  r..**.** The def
1ae40 61 75 6c 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f  ault encoding fo
1ae50 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  r the database w
1ae60 69 6c 6c 20 62 65 20 55 54 46 2d 38 20 69 66 0a  ill be UTF-8 if.
1ae70 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ** sqlite3_open(
1ae80 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65  ) or sqlite3_ope
1ae90 6e 5f 76 32 28 29 20 69 73 20 63 61 6c 6c 65 64  n_v2() is called
1aea0 20 61 6e 64 0a 2a 2a 20 55 54 46 2d 31 36 20 69   and.** UTF-16 i
1aeb0 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74  n the native byt
1aec0 65 20 6f 72 64 65 72 20 69 66 20 73 71 6c 69 74  e order if sqlit
1aed0 65 33 5f 6f 70 65 6e 31 36 28 29 20 69 73 20 75  e3_open16() is u
1aee0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68  sed..**.** Wheth
1aef0 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72  er or not an err
1af00 6f 72 20 6f 63 63 75 72 73 20 77 68 65 6e 20 69  or occurs when i
1af10 74 20 69 73 20 6f 70 65 6e 65 64 2c 20 72 65 73  t is opened, res
1af20 6f 75 72 63 65 73 0a 2a 2a 20 61 73 73 6f 63 69  ources.** associ
1af30 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64  ated with the [d
1af40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1af50 6f 6e 5d 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c  on] handle shoul
1af60 64 20 62 65 20 72 65 6c 65 61 73 65 64 20 62 79  d be released by
1af70 0a 2a 2a 20 70 61 73 73 69 6e 67 20 69 74 20 74  .** passing it t
1af80 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  o [sqlite3_close
1af90 28 29 5d 20 77 68 65 6e 20 69 74 20 69 73 20 6e  ()] when it is n
1afa0 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65  o longer require
1afb0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  d..**.** The sql
1afc0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69  ite3_open_v2() i
1afd0 6e 74 65 72 66 61 63 65 20 77 6f 72 6b 73 20 6c  nterface works l
1afe0 69 6b 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ike sqlite3_open
1aff0 28 29 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61  ().** except tha
1b000 74 20 69 74 20 61 63 63 65 70 74 73 20 74 77 6f  t it accepts two
1b010 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61   additional para
1b020 6d 65 74 65 72 73 20 66 6f 72 20 61 64 64 69 74  meters for addit
1b030 69 6f 6e 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a  ional control.**
1b040 20 6f 76 65 72 20 74 68 65 20 6e 65 77 20 64 61   over the new da
1b050 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1b060 6e 2e 20 20 54 68 65 20 66 6c 61 67 73 20 70 61  n.  The flags pa
1b070 72 61 6d 65 74 65 72 20 63 61 6e 20 74 61 6b 65  rameter can take
1b080 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66   one of.** the f
1b090 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 76  ollowing three v
1b0a0 61 6c 75 65 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c  alues, optionall
1b0b0 79 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  y combined with 
1b0c0 74 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  the .** [SQLITE_
1b0d0 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 6f 72  OPEN_NOMUTEX] or
1b0e0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55   [SQLITE_OPEN_FU
1b0f0 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 73 3a 0a  LLMUTEX] flags:.
1b100 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64  **.** <dl>.** <d
1b110 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  t>[SQLITE_OPEN_R
1b120 45 41 44 4f 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a  EADONLY]</dt>.**
1b130 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73   <dd>The databas
1b140 65 20 69 73 20 6f 70 65 6e 65 64 20 69 6e 20 72  e is opened in r
1b150 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20  ead-only mode.  
1b160 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
1b170 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65  does not.** alre
1b180 61 64 79 20 65 78 69 73 74 2c 20 61 6e 20 65 72  ady exist, an er
1b190 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ror is returned.
1b1a0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
1b1b0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  [SQLITE_OPEN_REA
1b1c0 44 57 52 49 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20  DWRITE]</dt>.** 
1b1d0 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 65  <dd>The database
1b1e0 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72   is opened for r
1b1f0 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69  eading and writi
1b200 6e 67 20 69 66 20 70 6f 73 73 69 62 6c 65 2c 20  ng if possible, 
1b210 6f 72 20 72 65 61 64 69 6e 67 0a 2a 2a 20 6f 6e  or reading.** on
1b220 6c 79 20 69 66 20 74 68 65 20 66 69 6c 65 20 69  ly if the file i
1b230 73 20 77 72 69 74 65 20 70 72 6f 74 65 63 74 65  s write protecte
1b240 64 20 62 79 20 74 68 65 20 6f 70 65 72 61 74 69  d by the operati
1b250 6e 67 20 73 79 73 74 65 6d 2e 20 20 49 6e 20 65  ng system.  In e
1b260 69 74 68 65 72 0a 2a 2a 20 63 61 73 65 20 74 68  ither.** case th
1b270 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20  e database must 
1b280 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c 20 6f  already exist, o
1b290 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f  therwise an erro
1b2a0 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f  r is returned.</
1b2b0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53  dd>.**.** <dt>[S
1b2c0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
1b2d0 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f  RITE] | [SQLITE_
1b2e0 4f 50 45 4e 5f 43 52 45 41 54 45 5d 3c 2f 64 74  OPEN_CREATE]</dt
1b2f0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74  >.** <dd>The dat
1b300 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20  abase is opened 
1b310 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20  for reading and 
1b320 77 72 69 74 69 6e 67 2c 20 61 6e 64 20 69 73 20  writing, and is 
1b330 63 72 65 61 74 65 73 20 69 74 20 69 66 0a 2a 2a  creates it if.**
1b340 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72   it does not alr
1b350 65 61 64 79 20 65 78 69 73 74 2e 20 54 68 69 73  eady exist. This
1b360 20 69 73 20 74 68 65 20 62 65 68 61 76 69 6f 72   is the behavior
1b370 20 74 68 61 74 20 69 73 20 61 6c 77 61 79 73 20   that is always 
1b380 75 73 65 64 20 66 6f 72 0a 2a 2a 20 73 71 6c 69  used for.** sqli
1b390 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73  te3_open() and s
1b3a0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e  qlite3_open16().
1b3b0 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a  </dd>.** </dl>.*
1b3c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20  *.** If the 3rd 
1b3d0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
1b3e0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69  ite3_open_v2() i
1b3f0 73 20 6e 6f 74 20 6f 6e 65 20 6f 66 20 74 68 65  s not one of the
1b400 0a 2a 2a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  .** combinations
1b410 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 6f 72 20   shown above or 
1b420 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6d 62 69  one of the combi
1b430 6e 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62  nations shown ab
1b440 6f 76 65 20 63 6f 6d 62 69 6e 65 64 0a 2a 2a 20  ove combined.** 
1b450 77 69 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45  with the [SQLITE
1b460 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 6f  _OPEN_NOMUTEX] o
1b470 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46  r [SQLITE_OPEN_F
1b480 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 73 2c  ULLMUTEX] flags,
1b490 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68  .** then the beh
1b4a0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
1b4b0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
1b4c0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f   [SQLITE_OPEN_NO
1b4d0 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 73  MUTEX] flag is s
1b4e0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74  et, then the dat
1b4f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1b500 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74 68 65  .** opens in the
1b510 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20 5b 74   multi-thread [t
1b520 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 61  hreading mode] a
1b530 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 73 69  s long as the si
1b540 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a 20 6d  ngle-thread.** m
1b550 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ode has not been
1b560 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 2d   set at compile-
1b570 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 69  time or start-ti
1b580 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 5b  me.  If the.** [
1b590 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c  SQLITE_OPEN_FULL
1b5a0 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 73  MUTEX] flag is s
1b5b0 65 74 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  et then the data
1b5c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1b5d0 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20  opens.** in the 
1b5e0 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65  serialized [thre
1b5f0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e 6c 65  ading mode] unle
1b600 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64  ss single-thread
1b610 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73   was.** previous
1b620 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74 20 63  ly selected at c
1b630 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73  ompile-time or s
1b640 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a  tart-time..**.**
1b650 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65   If the filename
1b660 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20   is ":memory:", 
1b670 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 20  then a private, 
1b680 74 65 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d  temporary in-mem
1b690 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ory database.** 
1b6a0 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74  is created for t
1b6b0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20  he connection.  
1b6c0 54 68 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  This in-memory d
1b6d0 61 74 61 62 61 73 65 20 77 69 6c 6c 20 76 61 6e  atabase will van
1b6e0 69 73 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  ish when.** the 
1b6f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1b700 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 20  ion is closed.  
1b710 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
1b720 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a  of SQLite might.
1b730 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 61  ** make use of a
1b740 64 64 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 61  dditional specia
1b750 6c 20 66 69 6c 65 6e 61 6d 65 73 20 74 68 61 74  l filenames that
1b760 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20   begin with the 
1b770 22 3a 22 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  ":" character..*
1b780 2a 20 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e  * It is recommen
1b790 64 65 64 20 74 68 61 74 20 77 68 65 6e 20 61 20  ded that when a 
1b7a0 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  database filenam
1b7b0 65 20 61 63 74 75 61 6c 6c 79 20 64 6f 65 73 20  e actually does 
1b7c0 62 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20  begin with.** a 
1b7d0 22 3a 22 20 63 68 61 72 61 63 74 65 72 20 79 6f  ":" character yo
1b7e0 75 20 73 68 6f 75 6c 64 20 70 72 65 66 69 78 20  u should prefix 
1b7f0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 77 69 74  the filename wit
1b800 68 20 61 20 70 61 74 68 6e 61 6d 65 20 73 75 63  h a pathname suc
1b810 68 20 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20  h as.** "./" to 
1b820 61 76 6f 69 64 20 61 6d 62 69 67 75 69 74 79 2e  avoid ambiguity.
1b830 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
1b840 6c 65 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d 70  lename is an emp
1b850 74 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20  ty string, then 
1b860 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70 6f  a private, tempo
1b870 72 61 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20  rary.** on-disk 
1b880 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65  database will be
1b890 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 20   created.  This 
1b8a0 70 72 69 76 61 74 65 20 64 61 74 61 62 61 73 65  private database
1b8b0 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f   will be.** auto
1b8c0 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65  matically delete
1b8d0 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65  d as soon as the
1b8e0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1b8f0 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a  tion is closed..
1b900 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68  **.** The fourth
1b910 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
1b920 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20  lite3_open_v2() 
1b930 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
1b940 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76  he.** [sqlite3_v
1b950 66 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20  fs] object that 
1b960 64 65 66 69 6e 65 73 20 74 68 65 20 6f 70 65 72  defines the oper
1b970 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74  ating system int
1b980 65 72 66 61 63 65 20 74 68 61 74 0a 2a 2a 20 74  erface that.** t
1b990 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 20  he new database 
1b9a0 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c  connection shoul
1b9b0 64 20 75 73 65 2e 20 20 49 66 20 74 68 65 20 66  d use.  If the f
1b9c0 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20  ourth parameter 
1b9d0 69 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69  is.** a NULL poi
1b9e0 6e 74 65 72 20 74 68 65 6e 20 74 68 65 20 64 65  nter then the de
1b9f0 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76  fault [sqlite3_v
1ba00 66 73 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73  fs] object is us
1ba10 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74  ed..**.** <b>Not
1ba20 65 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65  e to Windows use
1ba30 72 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63  rs:</b>  The enc
1ba40 6f 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74  oding used for t
1ba50 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75  he filename argu
1ba60 6d 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74  ment.** of sqlit
1ba70 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71  e3_open() and sq
1ba80 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20  lite3_open_v2() 
1ba90 6d 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e  must be UTF-8, n
1baa0 6f 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63  ot whatever.** c
1bab0 6f 64 65 70 61 67 65 20 69 73 20 63 75 72 72 65  odepage is curre
1bac0 6e 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46  ntly defined.  F
1bad0 69 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e  ilenames contain
1bae0 69 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61  ing internationa
1baf0 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20  l.** characters 
1bb00 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65  must be converte
1bb10 64 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72  d to UTF-8 prior
1bb20 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d   to passing them
1bb30 20 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33   into.** sqlite3
1bb40 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74  _open() or sqlit
1bb50 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a  e3_open_v2()..**
1bb60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
1bb70 3a 0a 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48  :.** [H12701] [H
1bb80 31 32 37 30 32 5d 20 5b 48 31 32 37 30 33 5d 20  12702] [H12703] 
1bb90 5b 48 31 32 37 30 34 5d 20 5b 48 31 32 37 30 36  [H12704] [H12706
1bba0 5d 20 5b 48 31 32 37 30 37 5d 20 5b 48 31 32 37  ] [H12707] [H127
1bbb0 30 39 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20  09] [H12711].** 
1bbc0 5b 48 31 32 37 31 32 5d 20 5b 48 31 32 37 31 33  [H12712] [H12713
1bbd0 5d 20 5b 48 31 32 37 31 34 5d 20 5b 48 31 32 37  ] [H12714] [H127
1bbe0 31 37 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48 31  17] [H12719] [H1
1bbf0 32 37 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a  2721] [H12723].*
1bc00 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1bc10 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20   sqlite3_open(. 
1bc20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c   const char *fil
1bc30 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61  ename,   /* Data
1bc40 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55  base filename (U
1bc50 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74  TF-8) */.  sqlit
1bc60 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20  e3 **ppDb       
1bc70 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74     /* OUT: SQLit
1bc80 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29  e db handle */.)
1bc90 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
1bca0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
1bcb0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66  .  const void *f
1bcc0 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61  ilename,   /* Da
1bcd0 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
1bce0 28 55 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71  (UTF-16) */.  sq
1bcf0 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20  lite3 **ppDb    
1bd00 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51        /* OUT: SQ
1bd10 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a  Lite db handle *
1bd20 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  /.);.SQLITE_API 
1bd30 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
1bd40 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  _v2(.  const cha
1bd50 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f  r *filename,   /
1bd60 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e  * Database filen
1bd70 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20  ame (UTF-8) */. 
1bd80 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c   sqlite3 **ppDb,
1bd90 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
1bda0 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c   SQLite db handl
1bdb0 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
1bdc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1bdd0 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e  * Flags */.  con
1bde0 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20  st char *zVfs   
1bdf0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1be00 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73  VFS module to us
1be10 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  e */.);../*.** C
1be20 41 50 49 33 52 45 46 3a 20 45 72 72 6f 72 20 43  API3REF: Error C
1be30 6f 64 65 73 20 41 6e 64 20 4d 65 73 73 61 67 65  odes And Message
1be40 73 20 7b 48 31 32 38 30 30 7d 20 3c 53 36 30 32  s {H12800} <S602
1be50 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
1be60 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20  lite3_errcode() 
1be70 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
1be80 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 5b 72  s the numeric [r
1be90 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a  esult code] or.*
1bea0 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75  * [extended resu
1beb0 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65  lt code] for the
1bec0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 66 61 69   most recent fai
1bed0 6c 65 64 20 73 71 6c 69 74 65 33 5f 2a 20 41 50  led sqlite3_* AP
1bee0 49 20 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69  I call.** associ
1bef0 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61 74  ated with a [dat
1bf00 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1bf10 5d 2e 20 49 66 20 61 20 70 72 69 6f 72 20 41 50  ]. If a prior AP
1bf20 49 20 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a  I call failed.**
1bf30 20 62 75 74 20 74 68 65 20 6d 6f 73 74 20 72 65   but the most re
1bf40 63 65 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 75  cent API call su
1bf50 63 63 65 65 64 65 64 2c 20 74 68 65 20 72 65 74  cceeded, the ret
1bf60 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a  urn value from.*
1bf70 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  * sqlite3_errcod
1bf80 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64  e() is undefined
1bf90 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65  .  The sqlite3_e
1bfa0 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28  xtended_errcode(
1bfb0 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69  ).** interface i
1bfc0 73 20 74 68 65 20 73 61 6d 65 20 65 78 63 65 70  s the same excep
1bfd0 74 20 74 68 61 74 20 69 74 20 61 6c 77 61 79 73  t that it always
1bfe0 20 72 65 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a   returns the .**
1bff0 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c   [extended resul
1c000 74 20 63 6f 64 65 5d 20 65 76 65 6e 20 77 68 65  t code] even whe
1c010 6e 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  n extended resul
1c020 74 20 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64  t codes are.** d
1c030 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  isabled..**.** T
1c040 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  he sqlite3_errms
1c050 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  g() and sqlite3_
1c060 65 72 72 6d 73 67 31 36 28 29 20 72 65 74 75 72  errmsg16() retur
1c070 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61  n English-langua
1c080 67 65 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 20  ge.** text that 
1c090 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72  describes the er
1c0a0 72 6f 72 2c 20 61 73 20 65 69 74 68 65 72 20 55  ror, as either U
1c0b0 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72  TF-8 or UTF-16 r
1c0c0 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20  espectively..** 
1c0d0 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74  Memory to hold t
1c0e0 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
1c0f0 20 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67   string is manag
1c100 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a  ed internally..*
1c110 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * The applicatio
1c120 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
1c130 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 66  to worry about f
1c140 72 65 65 69 6e 67 20 74 68 65 20 72 65 73 75 6c  reeing the resul
1c150 74 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74  t..** However, t
1c160 68 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20  he error string 
1c170 6d 69 67 68 74 20 62 65 20 6f 76 65 72 77 72 69  might be overwri
1c180 74 74 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61  tten or dealloca
1c190 74 65 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 71  ted by.** subseq
1c1a0 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74  uent calls to ot
1c1b0 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72  her SQLite inter
1c1c0 66 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  face functions..
1c1d0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73  **.** When the s
1c1e0 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61  erialized [threa
1c1f0 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e  ding mode] is in
1c200 20 75 73 65 2c 20 69 74 20 6d 69 67 68 74 20 62   use, it might b
1c210 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68  e the.** case th
1c220 61 74 20 61 20 73 65 63 6f 6e 64 20 65 72 72 6f  at a second erro
1c230 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 73 65  r occurs on a se
1c240 70 61 72 61 74 65 20 74 68 72 65 61 64 20 69 6e  parate thread in
1c250 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20   between.** the 
1c260 74 69 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73  time of the firs
1c270 74 20 65 72 72 6f 72 20 61 6e 64 20 74 68 65 20  t error and the 
1c280 63 61 6c 6c 20 74 6f 20 74 68 65 73 65 20 69 6e  call to these in
1c290 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65  terfaces..** Whe
1c2a0 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20  n that happens, 
1c2b0 74 68 65 20 73 65 63 6f 6e 64 20 65 72 72 6f 72  the second error
1c2c0 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65   will be reporte
1c2d0 64 20 73 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a  d since these.**
1c2e0 20 69 6e 74 65 72 66 61 63 65 73 20 61 6c 77 61   interfaces alwa
1c2f0 79 73 20 72 65 70 6f 72 74 20 74 68 65 20 6d 6f  ys report the mo
1c300 73 74 20 72 65 63 65 6e 74 20 72 65 73 75 6c 74  st recent result
1c310 2e 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74  .  To avoid.** t
1c320 68 69 73 2c 20 65 61 63 68 20 74 68 72 65 61 64  his, each thread
1c330 20 63 61 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c   can obtain excl
1c340 75 73 69 76 65 20 75 73 65 20 6f 66 20 74 68 65  usive use of the
1c350 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
1c360 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69  ction] D.** by i
1c370 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33  nvoking [sqlite3
1c380 5f 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73  _mutex_enter]([s
1c390 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d  qlite3_db_mutex]
1c3a0 28 44 29 29 20 62 65 66 6f 72 65 20 62 65 67 69  (D)) before begi
1c3b0 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20  nning.** to use 
1c3c0 44 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b  D and invoking [
1c3d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1c3e0 61 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62  ave]([sqlite3_db
1c3f0 5f 6d 75 74 65 78 5d 28 44 29 29 20 61 66 74 65  _mutex](D)) afte
1c400 72 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74  r.** all calls t
1c410 6f 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 73  o the interfaces
1c420 20 6c 69 73 74 65 64 20 68 65 72 65 20 61 72 65   listed here are
1c430 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a   completed..**.*
1c440 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 66 61 63  * If an interfac
1c450 65 20 66 61 69 6c 73 20 77 69 74 68 20 53 51 4c  e fails with SQL
1c460 49 54 45 5f 4d 49 53 55 53 45 2c 20 74 68 61 74  ITE_MISUSE, that
1c470 20 6d 65 61 6e 73 20 74 68 65 20 69 6e 74 65 72   means the inter
1c480 66 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f  face.** was invo
1c490 6b 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20  ked incorrectly 
1c4a0 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  by the applicati
1c4b0 6f 6e 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73  on.  In that cas
1c4c0 65 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20  e, the.** error 
1c4d0 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65  code and message
1c4e0 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20   may or may not 
1c4f0 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65  be set..**.** Re
1c500 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
1c510 48 31 32 38 30 31 5d 20 5b 48 31 32 38 30 32 5d  H12801] [H12802]
1c520 20 5b 48 31 32 38 30 33 5d 20 5b 48 31 32 38 30   [H12803] [H1280
1c530 37 5d 20 5b 48 31 32 38 30 38 5d 20 5b 48 31 32  7] [H12808] [H12
1c540 38 30 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  809].*/.SQLITE_A
1c550 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
1c560 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a  rrcode(sqlite3 *
1c570 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  db);.SQLITE_API 
1c580 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65  int sqlite3_exte
1c590 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c  nded_errcode(sql
1c5a0 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54  ite3 *db);.SQLIT
1c5b0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
1c5c0 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   *sqlite3_errmsg
1c5d0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
1c5e0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1c5f0 64 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  d *sqlite3_errms
1c600 67 31 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a  g16(sqlite3*);..
1c610 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1c620 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f 62  SQL Statement Ob
1c630 6a 65 63 74 20 7b 48 31 33 30 30 30 7d 20 3c 48  ject {H13000} <H
1c640 31 33 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  13010>.** KEYWOR
1c650 44 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74  DS: {prepared st
1c660 61 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72  atement} {prepar
1c670 65 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a  ed statements}.*
1c680 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
1c690 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20   of this object 
1c6a0 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e  represents a sin
1c6b0 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  gle SQL statemen
1c6c0 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63  t..** This objec
1c6d0 74 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b  t is variously k
1c6e0 6e 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70 61  nown as a "prepa
1c6f0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f  red statement" o
1c700 72 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64  r a.** "compiled
1c710 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20   SQL statement" 
1c720 6f 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22  or simply as a "
1c730 73 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a  statement"..**.*
1c740 2a 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20  * The life of a 
1c750 73 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74  statement object
1c760 20 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20   goes something 
1c770 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
1c780 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72   <ol>.** <li> Cr
1c790 65 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20  eate the object 
1c7a0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70  using [sqlite3_p
1c7b0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
1c7c0 61 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20  a related.**    
1c7d0 20 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c    function..** <
1c7e0 6c 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20  li> Bind values 
1c7f0 74 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74  to [host paramet
1c800 65 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73  ers] using the s
1c810 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a  qlite3_bind_*().
1c820 2a 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61 63  **      interfac
1c830 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20  es..** <li> Run 
1c840 74 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69  the SQL by calli
1c850 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ng [sqlite3_step
1c860 28 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ()] one or more 
1c870 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52  times..** <li> R
1c880 65 73 65 74 20 74 68 65 20 73 74 61 74 65 6d 65  eset the stateme
1c890 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  nt using [sqlite
1c8a0 33 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20  3_reset()] then 
1c8b0 67 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20  go back.**      
1c8c0 74 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74  to step 2.  Do t
1c8d0 68 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65  his zero or more
1c8e0 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20   times..** <li> 
1c8f0 44 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65  Destroy the obje
1c900 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ct using [sqlite
1c910 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a  3_finalize()]..*
1c920 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65  * </ol>.**.** Re
1c930 66 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61  fer to documenta
1c940 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75  tion on individu
1c950 61 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65  al methods above
1c960 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a   for additional.
1c970 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  ** information..
1c980 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
1c990 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73  t sqlite3_stmt s
1c9a0 71 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a  qlite3_stmt;../*
1c9b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75  .** CAPI3REF: Ru
1c9c0 6e 2d 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48  n-time Limits {H
1c9d0 31 32 37 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a  12760} <S20600>.
1c9e0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
1c9f0 66 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20  face allows the 
1ca00 73 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20  size of various 
1ca10 63 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65  constructs to be
1ca20 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61   limited.** on a
1ca30 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63   connection by c
1ca40 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e  onnection basis.
1ca50 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61    The first para
1ca60 6d 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20  meter is the.** 
1ca70 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
1ca80 74 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69  tion] whose limi
1ca90 74 20 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f  t is to be set o
1caa0 72 20 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a  r queried.  The.
1cab0 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  ** second parame
1cac0 74 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  ter is one of th
1cad0 65 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72  e [limit categor
1cae0 69 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65  ies] that define
1caf0 20 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63   a.** class of c
1cb00 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20  onstructs to be 
1cb10 73 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54  size limited.  T
1cb20 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  he third paramet
1cb30 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77  er is the.** new
1cb40 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20   limit for that 
1cb50 63 6f 6e 73 74 72 75 63 74 2e 20 20 54 68 65 20  construct.  The 
1cb60 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
1cb70 20 74 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a   the old limit..
1cb80 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 77  **.** If the new
1cb90 20 6c 69 6d 69 74 20 69 73 20 61 20 6e 65 67 61   limit is a nega
1cba0 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 65  tive number, the
1cbb0 20 6c 69 6d 69 74 20 69 73 20 75 6e 63 68 61 6e   limit is unchan
1cbc0 67 65 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20  ged..** For the 
1cbd0 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20 6f  limit category o
1cbe0 66 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58  f SQLITE_LIMIT_X
1cbf0 59 5a 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a  YZ there is a .*
1cc00 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72 64  * [limits | hard
1cc10 20 75 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a   upper bound].**
1cc20 20 73 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c   set by a compil
1cc30 65 2d 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63  e-time C preproc
1cc40 65 73 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65  essor macro name
1cc50 64 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20  d .** [limits | 
1cc60 53 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e  SQLITE_MAX_XYZ].
1cc70 0a 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54  .** (The "_LIMIT
1cc80 5f 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69  _" in the name i
1cc90 73 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d  s changed to "_M
1cca0 41 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70  AX_".).** Attemp
1ccb0 74 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61  ts to increase a
1ccc0 20 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73   limit above its
1ccd0 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e   hard upper boun
1cce0 64 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c  d are.** silentl
1ccf0 79 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74  y truncated to t
1cd00 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69  he hard upper li
1cd10 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74  mit..**.** Run t
1cd20 69 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69  ime limits are i
1cd30 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
1cd40 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  in applications 
1cd50 74 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62  that manage.** b
1cd60 6f 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e  oth their own in
1cd70 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20  ternal database 
1cd80 61 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73  and also databas
1cd90 65 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74  es that are cont
1cda0 72 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74  rolled.** by unt
1cdb0 72 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20  rusted external 
1cdc0 73 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61  sources.  An exa
1cdd0 6d 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e  mple application
1cde0 20 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77   might be a.** w
1cdf0 65 62 20 62 72 6f 77 73 65 72 20 74 68 61 74 20  eb browser that 
1ce00 68 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61  has its own data
1ce10 62 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e  bases for storin
1ce20 67 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a  g history and.**
1ce30 20 73 65 70 61 72 61 74 65 20 64 61 74 61 62 61   separate databa
1ce40 73 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62  ses controlled b
1ce50 79 20 4a 61 76 61 53 63 72 69 70 74 20 61 70 70  y JavaScript app
1ce60 6c 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f  lications downlo
1ce70 61 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20  aded.** off the 
1ce80 49 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69  Internet.  The i
1ce90 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65  nternal database
1cea0 73 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74  s can be given t
1ceb0 68 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66  he.** large, def
1cec0 61 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61  ault limits.  Da
1ced0 74 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20  tabases managed 
1cee0 62 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72  by external sour
1cef0 63 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69  ces can.** be gi
1cf00 76 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72  ven much smaller
1cf10 20 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64   limits designed
1cf20 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65   to prevent a de
1cf30 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a  nial of service.
1cf40 2a 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65  ** attack.  Deve
1cf50 6c 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73  lopers might als
1cf60 6f 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68  o want to use th
1cf70 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  e [sqlite3_set_a
1cf80 75 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20  uthorizer()].** 
1cf90 69 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75 72  interface to fur
1cfa0 74 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74  ther control unt
1cfb0 72 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65  rusted SQL.  The
1cfc0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
1cfd0 61 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64  abase.** created
1cfe0 20 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64   by an untrusted
1cff0 20 73 63 72 69 70 74 20 63 61 6e 20 62 65 20 63   script can be c
1d000 6f 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74  ontained using t
1d010 68 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f  he.** [max_page_
1d020 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e  count] [PRAGMA].
1d030 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74  .**.** New run-t
1d040 69 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f  ime limit catego
1d050 72 69 65 73 20 6d 61 79 20 62 65 20 61 64 64 65  ries may be adde
1d060 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  d in future rele
1d070 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ases..**.** Requ
1d080 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
1d090 32 37 36 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b  2762] [H12766] [
1d0a0 48 31 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54  H12769].*/.SQLIT
1d0b0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1d0c0 33 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a  3_limit(sqlite3*
1d0d0 2c 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65  , int id, int ne
1d0e0 77 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  wVal);../*.** CA
1d0f0 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65  PI3REF: Run-Time
1d100 20 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65   Limit Categorie
1d110 73 20 7b 48 31 32 37 39 30 7d 20 3c 48 31 32 37  s {H12790} <H127
1d120 36 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  60>.** KEYWORDS:
1d130 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79   {limit category
1d140 7d 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72  } {limit categor
1d150 69 65 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ies}.**.** These
1d160 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e   constants defin
1d170 65 20 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72  e various perfor
1d180 6d 61 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20  mance limits.** 
1d190 74 68 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65  that can be lowe
1d1a0 72 65 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20  red at run-time 
1d1b0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c  using [sqlite3_l
1d1c0 69 6d 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20  imit()]..** The 
1d1d0 73 79 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20  synopsis of the 
1d1e0 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20  meanings of the 
1d1f0 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69  various limits i
1d200 73 20 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a  s shown below..*
1d210 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  * Additional inf
1d220 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69  ormation is avai
1d230 6c 61 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73  lable at [limits
1d240 20 7c 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c   | Limits in SQL
1d250 69 74 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  ite]..**.** <dl>
1d260 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
1d270 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e  IMIT_LENGTH</dt>
1d280 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1d290 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20  mum size of any 
1d2a0 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f  string or BLOB o
1d2b0 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e  r table row.<dd>
1d2c0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
1d2d0 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47  E_LIMIT_SQL_LENG
1d2e0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  TH</dt>.** <dd>T
1d2f0 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
1d300 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74  h of an SQL stat
1d310 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ement.</dd>.**.*
1d320 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d  * <dt>SQLITE_LIM
1d330 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a  IT_COLUMN</dt>.*
1d340 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1d350 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  m number of colu
1d360 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64  mns in a table d
1d370 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20  efinition or in 
1d380 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65  the.** result se
1d390 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20  t of a [SELECT] 
1d3a0 6f 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  or the maximum n
1d3b0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1d3c0 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20   in an index.** 
1d3d0 6f 72 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42  or in an ORDER B
1d3e0 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c  Y or GROUP BY cl
1d3f0 61 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ause.</dd>.**.**
1d400 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49   <dt>SQLITE_LIMI
1d410 54 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74  T_EXPR_DEPTH</dt
1d420 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
1d430 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68  imum depth of th
1d440 65 20 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20  e parse tree on 
1d450 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c  any expression.<
1d460 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
1d470 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50  QLITE_LIMIT_COMP
1d480 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e  OUND_SELECT</dt>
1d490 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1d4a0 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  mum number of te
1d4b0 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e  rms in a compoun
1d4c0 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  d SELECT stateme
1d4d0 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  nt.</dd>.**.** <
1d4e0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1d4f0 56 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20  VDBE_OP</dt>.** 
1d500 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
1d510 6e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75  number of instru
1d520 63 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74  ctions in a virt
1d530 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67  ual machine prog
1d540 72 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69  ram.** used to i
1d550 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20  mplement an SQL 
1d560 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a  statement.</dd>.
1d570 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
1d580 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f  _LIMIT_FUNCTION_
1d590 41 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  ARG</dt>.** <dd>
1d5a0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1d5b0 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
1d5c0 6f 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f  on a function.</
1d5d0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
1d5e0 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43  LITE_LIMIT_ATTAC
1d5f0 48 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  HED</dt>.** <dd>
1d600 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1d610 65 72 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20  er of [ATTACH | 
1d620 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1d630 65 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  es].</dd>.**.** 
1d640 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1d650 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
1d660 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NGTH</dt>.** <dd
1d670 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e  >The maximum len
1d680 67 74 68 20 6f 66 20 74 68 65 20 70 61 74 74 65  gth of the patte
1d690 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  rn argument to t
1d6a0 68 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20  he [LIKE] or.** 
1d6b0 5b 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73  [GLOB] operators
1d6c0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
1d6d0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41  >SQLITE_LIMIT_VA
1d6e0 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64  RIABLE_NUMBER</d
1d6f0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1d700 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1d710 76 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20  variables in an 
1d720 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68  SQL statement th
1d730 61 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75  at can.** be bou
1d740 6e 64 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c  nd.</dd>.** </dl
1d750 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  >.*/.#define SQL
1d760 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
1d770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d780 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51      0.#define SQ
1d790 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c  LITE_LIMIT_SQL_L
1d7a0 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20  ENGTH           
1d7b0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53       1.#define S
1d7c0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55  QLITE_LIMIT_COLU
1d7d0 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MN              
1d7e0 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20        2.#define 
1d7f0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50  SQLITE_LIMIT_EXP
1d800 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20  R_DEPTH         
1d810 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65         3.#define
1d820 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f   SQLITE_LIMIT_CO
1d830 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 20  MPOUND_SELECT   
1d840 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e          4.#defin
1d850 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  e SQLITE_LIMIT_V
1d860 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 20  DBE_OP          
1d870 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69           5.#defi
1d880 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
1d890 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 20  FUNCTION_ARG    
1d8a0 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66            6.#def
1d8b0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
1d8c0 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 20 20  _ATTACHED       
1d8d0 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65             7.#de
1d8e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
1d8f0 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  T_LIKE_PATTERN_L
1d900 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 64  ENGTH       8.#d
1d910 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
1d920 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  IT_VARIABLE_NUMB
1d930 45 52 20 20 20 20 20 20 20 20 20 20 20 39 0a 0a  ER           9..
1d940 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1d950 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c  Compiling An SQL
1d960 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 30   Statement {H130
1d970 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20  10} <S10000>.** 
1d980 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73  KEYWORDS: {SQL s
1d990 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65  tatement compile
1d9a0 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63  r}.**.** To exec
1d9b0 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79  ute an SQL query
1d9c0 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 20  , it must first 
1d9d0 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f  be compiled into
1d9e0 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20   a byte-code.** 
1d9f0 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e  program using on
1da00 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  e of these routi
1da10 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  nes..**.** The f
1da20 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 22  irst argument, "
1da30 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61 62  db", is a [datab
1da40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
1da50 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a  obtained from a.
1da60 2a 2a 20 70 72 69 6f 72 20 73 75 63 63 65 73 73  ** prior success
1da70 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ful call to [sql
1da80 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73  ite3_open()], [s
1da90 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
1daa0 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ] or.** [sqlite3
1dab0 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20 54 68 65  _open16()].  The
1dac0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1dad0 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 68 61  tion must not ha
1dae0 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 64 2e 0a  ve been closed..
1daf0 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  **.** The second
1db00 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c   argument, "zSql
1db10 22 2c 20 69 73 20 74 68 65 20 73 74 61 74 65 6d  ", is the statem
1db20 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c  ent to be compil
1db30 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61  ed, encoded.** a
1db40 73 20 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f  s either UTF-8 o
1db50 72 20 55 54 46 2d 31 36 2e 20 20 54 68 65 20 73  r UTF-16.  The s
1db60 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
1db70 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65   and sqlite3_pre
1db80 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74  pare_v2().** int
1db90 65 72 66 61 63 65 73 20 75 73 65 20 55 54 46 2d  erfaces use UTF-
1dba0 38 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70  8, and sqlite3_p
1dbb0 72 65 70 61 72 65 31 36 28 29 20 61 6e 64 20 73  repare16() and s
1dbc0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
1dbd0 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 55 54 46  _v2().** use UTF
1dbe0 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  -16..**.** If th
1dbf0 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74  e nByte argument
1dc00 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65   is less than ze
1dc10 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c 20 69 73  ro, then zSql is
1dc20 20 72 65 61 64 20 75 70 20 74 6f 20 74 68 65 0a   read up to the.
1dc30 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65  ** first zero te
1dc40 72 6d 69 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79  rminator. If nBy
1dc50 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69  te is non-negati
1dc60 76 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74  ve, then it is t
1dc70 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75  he maximum.** nu
1dc80 6d 62 65 72 20 6f 66 20 20 62 79 74 65 73 20 72  mber of  bytes r
1dc90 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20  ead from zSql.  
1dca0 57 68 65 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f  When nByte is no
1dcb0 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 0a  n-negative, the.
1dcc0 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e 67 20 65  ** zSql string e
1dcd0 6e 64 73 20 61 74 20 65 69 74 68 65 72 20 74 68  nds at either th
1dce0 65 20 66 69 72 73 74 20 27 5c 30 30 30 27 20 6f  e first '\000' o
1dcf0 72 20 27 5c 75 30 30 30 30 27 20 63 68 61 72 61  r '\u0000' chara
1dd00 63 74 65 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e  cter or.** the n
1dd10 42 79 74 65 2d 74 68 20 62 79 74 65 2c 20 77 68  Byte-th byte, wh
1dd20 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69  ichever comes fi
1dd30 72 73 74 2e 20 49 66 20 74 68 65 20 63 61 6c 6c  rst. If the call
1dd40 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74  er knows.** that
1dd50 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 74   the supplied st
1dd60 72 69 6e 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d  ring is nul-term
1dd70 69 6e 61 74 65 64 2c 20 74 68 65 6e 20 74 68 65  inated, then the
1dd80 72 65 20 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a  re is a small.**
1dd90 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 64 76   performance adv
1dda0 61 6e 74 61 67 65 20 74 6f 20 62 65 20 67 61 69  antage to be gai
1ddb0 6e 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 61  ned by passing a
1ddc0 6e 20 6e 42 79 74 65 20 70 61 72 61 6d 65 74 65  n nByte paramete
1ddd0 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 65 71 75  r that.** is equ
1dde0 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  al to the number
1ddf0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
1de00 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 3c 69   input string <i
1de10 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a  >including</i>.*
1de20 2a 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e  * the nul-termin
1de30 61 74 6f 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  ator bytes..**.*
1de40 2a 20 49 66 20 70 7a 54 61 69 6c 20 69 73 20 6e  * If pzTail is n
1de50 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a  ot NULL then *pz
1de60 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 6f 20  Tail is made to 
1de70 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72  point to the fir
1de80 73 74 20 62 79 74 65 0a 2a 2a 20 70 61 73 74 20  st byte.** past 
1de90 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  the end of the f
1dea0 69 72 73 74 20 53 51 4c 20 73 74 61 74 65 6d 65  irst SQL stateme
1deb0 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54 68 65  nt in zSql.  The
1dec0 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e 6c 79  se routines only
1ded0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74 68 65 20  .** compile the 
1dee0 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e 74 20  first statement 
1def0 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70 7a 54  in zSql, so *pzT
1df00 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  ail is left poin
1df10 74 69 6e 67 20 74 6f 0a 2a 2a 20 77 68 61 74 20  ting to.** what 
1df20 72 65 6d 61 69 6e 73 20 75 6e 63 6f 6d 70 69 6c  remains uncompil
1df30 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 74 6d  ed..**.** *ppStm
1df40 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  t is left pointi
1df50 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c 65 64  ng to a compiled
1df60 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
1df70 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e 20 62  ment] that can b
1df80 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 75 73  e.** executed us
1df90 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ing [sqlite3_ste
1dfa0 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72 65 20  p()].  If there 
1dfb0 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a 70 70  is an error, *pp
1dfc0 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a 20 74  Stmt is set.** t
1dfd0 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65 20  o NULL.  If the 
1dfe0 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e 74 61  input text conta
1dff0 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66 20 74  ins no SQL (if t
1e000 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 65  he input is an e
1e010 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 6f  mpty.** string o
1e020 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74 68 65  r a comment) the
1e030 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74  n *ppStmt is set
1e040 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 54 68 65   to NULL..** The
1e050 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75   calling procedu
1e060 72 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  re is responsibl
1e070 65 20 66 6f 72 20 64 65 6c 65 74 69 6e 67 20 74  e for deleting t
1e080 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53  he compiled.** S
1e090 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 75 73 69  QL statement usi
1e0a0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61  ng [sqlite3_fina
1e0b0 6c 69 7a 65 28 29 5d 20 61 66 74 65 72 20 69 74  lize()] after it
1e0c0 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69   has finished wi
1e0d0 74 68 20 69 74 2e 0a 2a 2a 20 70 70 53 74 6d 74  th it..** ppStmt
1e0e0 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c   may not be NULL
1e0f0 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65  ..**.** On succe
1e100 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  ss, [SQLITE_OK] 
1e110 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 74 68  is returned, oth
1e120 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72  erwise an [error
1e130 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
1e140 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ed..**.** The sq
1e150 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1e160 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70  () and sqlite3_p
1e170 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 69 6e  repare16_v2() in
1e180 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a 2a 20  terfaces are.** 
1e190 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20  recommended for 
1e1a0 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 6d 73  all new programs
1e1b0 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 72 20  . The two older 
1e1c0 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 72  interfaces are r
1e1d0 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62  etained.** for b
1e1e0 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
1e1f0 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 65 69  bility, but thei
1e200 72 20 75 73 65 20 69 73 20 64 69 73 63 6f 75 72  r use is discour
1e210 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20  aged..** In the 
1e220 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 73 2c  "v2" interfaces,
1e230 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
1e240 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20  atement.** that 
1e250 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 68 65  is returned (the
1e260 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20   [sqlite3_stmt] 
1e270 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e 73  object) contains
1e280 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a   a copy of the.*
1e290 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74  * original SQL t
1e2a0 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 65 73  ext. This causes
1e2b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74   the [sqlite3_st
1e2c0 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  ep()] interface 
1e2d0 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20 64  to.** behave a d
1e2e0 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74 77  ifferently in tw
1e2f0 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f  o ways:.**.** <o
1e300 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66  l>.** <li>.** If
1e310 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
1e320 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69 6e  hema changes, in
1e330 73 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e 69  stead of returni
1e340 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d  ng [SQLITE_SCHEM
1e350 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61  A] as it.** alwa
1e360 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b  ys used to do, [
1e370 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
1e380 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c  will automatical
1e390 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68 65  ly recompile the
1e3a0 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e   SQL.** statemen
1e3b0 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75 6e  t and try to run
1e3c0 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20 74   it again.  If t
1e3d0 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68  he schema has ch
1e3e0 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61  anged in.** a wa
1e3f0 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65  y that makes the
1e400 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f   statement no lo
1e410 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c  nger valid, [sql
1e420 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c  ite3_step()] wil
1e430 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72  l still.** retur
1e440 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  n [SQLITE_SCHEMA
1e450 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20 74  ].  But unlike t
1e460 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69  he legacy behavi
1e470 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45  or, [SQLITE_SCHE
1e480 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20  MA] is.** now a 
1e490 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43 61  fatal error.  Ca
1e4a0 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70  lling [sqlite3_p
1e4b0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 67 61  repare_v2()] aga
1e4c0 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65  in will not make
1e4d0 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f   the.** error go
1e4e0 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73   away.  Note: us
1e4f0 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  e [sqlite3_errms
1e500 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 68 65  g()] to find the
1e510 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68 65 20   text.** of the 
1e520 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 74 68  parsing error th
1e530 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e  at results in an
1e540 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d   [SQLITE_SCHEMA]
1e550 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69   return..** </li
1e560 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20  >.**.** <li>.** 
1e570 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63  When an error oc
1e580 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 5f 73  curs, [sqlite3_s
1e590 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 74 75  tep()] will retu
1e5a0 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65  rn one of the de
1e5b0 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f 72  tailed.** [error
1e5c0 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78 74 65   codes] or [exte
1e5d0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73  nded error codes
1e5e0 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79 20 62  ].  The legacy b
1e5f0 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 61 74  ehavior was that
1e600 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  .** [sqlite3_ste
1e610 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20  p()] would only 
1e620 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72 69 63  return a generic
1e630 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
1e640 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61  result code.** a
1e650 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68 61 76  nd you would hav
1e660 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f  e to make a seco
1e670 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  nd call to [sqli
1e680 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e 20  te3_reset()] in 
1e690 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69 6e 64  order.** to find
1e6a0 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
1e6b0 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72 6f  cause of the pro
1e6c0 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65 20 22  blem. With the "
1e6d0 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a 20 69  v2" prepare.** i
1e6e0 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20 75  nterfaces, the u
1e6f0 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e  nderlying reason
1e700 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 69   for the error i
1e710 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64  s returned immed
1e720 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e  iately..** </li>
1e730 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ol>.**.** 
1e740 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
1e750 20 5b 48 31 33 30 31 31 5d 20 5b 48 31 33 30 31   [H13011] [H1301
1e760 32 5d 20 5b 48 31 33 30 31 33 5d 20 5b 48 31 33  2] [H13013] [H13
1e770 30 31 34 5d 20 5b 48 31 33 30 31 35 5d 20 5b 48  014] [H13015] [H
1e780 31 33 30 31 36 5d 20 5b 48 31 33 30 31 39 5d 20  13016] [H13019] 
1e790 5b 48 31 33 30 32 31 5d 0a 2a 2a 0a 2a 2f 0a 53  [H13021].**.*/.S
1e7a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1e7b0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20  lite3_prepare(. 
1e7c0 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
1e7d0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1e7e0 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
1e7f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
1e800 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20  l,       /* SQL 
1e810 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38  statement, UTF-8
1e820 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e   encoded */.  in
1e830 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20  t nByte,        
1e840 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
1e850 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   length of zSql 
1e860 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73  in bytes. */.  s
1e870 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
1e880 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53  Stmt,  /* OUT: S
1e890 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20  tatement handle 
1e8a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1e8b0 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20  **pzTail     /* 
1e8c0 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20  OUT: Pointer to 
1e8d0 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f  unused portion o
1e8e0 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c  f zSql */.);.SQL
1e8f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1e900 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a  te3_prepare_v2(.
1e910 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
1e920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1e930 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
1e940 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
1e950 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ql,       /* SQL
1e960 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d   statement, UTF-
1e970 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69  8 encoded */.  i
1e980 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20  nt nByte,       
1e990 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
1e9a0 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  m length of zSql
1e9b0 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
1e9c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70  sqlite3_stmt **p
1e9d0 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20  pStmt,  /* OUT: 
1e9e0 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  Statement handle
1e9f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1ea00 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a   **pzTail     /*
1ea10 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f   OUT: Pointer to
1ea20 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20   unused portion 
1ea30 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51  of zSql */.);.SQ
1ea40 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1ea50 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a  ite3_prepare16(.
1ea60 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
1ea70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1ea80 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
1ea90 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53    const void *zS
1eaa0 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ql,       /* SQL
1eab0 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d   statement, UTF-
1eac0 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20  16 encoded */.  
1ead0 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20  int nByte,      
1eae0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
1eaf0 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71  um length of zSq
1eb00 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
1eb10 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
1eb20 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a  ppStmt,  /* OUT:
1eb30 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   Statement handl
1eb40 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  e */.  const voi
1eb50 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f  d **pzTail     /
1eb60 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74  * OUT: Pointer t
1eb70 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e  o unused portion
1eb80 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53   of zSql */.);.S
1eb90 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1eba0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
1ebb0 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
1ebc0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
1ebd0 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
1ebe0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
1ebf0 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
1ec00 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
1ec10 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a  UTF-16 encoded *
1ec20 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20  /.  int nByte,  
1ec30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
1ec40 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
1ec50 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
1ec60 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
1ec70 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20  t **ppStmt,  /* 
1ec80 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68  OUT: Statement h
1ec90 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
1eca0 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20   void **pzTail  
1ecb0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74     /* OUT: Point
1ecc0 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72  er to unused por
1ecd0 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a  tion of zSql */.
1ece0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
1ecf0 45 46 3a 20 52 65 74 72 69 65 76 69 6e 67 20 53  EF: Retrieving S
1ed00 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 7b 48 31  tatement SQL {H1
1ed10 33 31 30 30 7d 20 3c 48 31 33 30 30 30 3e 0a 2a  3100} <H13000>.*
1ed20 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
1ed30 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20  ace can be used 
1ed40 74 6f 20 72 65 74 72 69 65 76 65 20 61 20 73 61  to retrieve a sa
1ed50 76 65 64 20 63 6f 70 79 20 6f 66 20 74 68 65 20  ved copy of the 
1ed60 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53 51 4c 20  original.** SQL 
1ed70 74 65 78 74 20 75 73 65 64 20 74 6f 20 63 72 65  text used to cre
1ed80 61 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20  ate a [prepared 
1ed90 73 74 61 74 65 6d 65 6e 74 5d 20 69 66 20 74 68  statement] if th
1eda0 61 74 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73  at statement was
1edb0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 75 73 69  .** compiled usi
1edc0 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74  ng either [sqlit
1edd0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
1ede0 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   or [sqlite3_pre
1edf0 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a  pare16_v2()]..**
1ee00 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
1ee10 3a 0a 2a 2a 20 5b 48 31 33 31 30 31 5d 20 5b 48  :.** [H13101] [H
1ee20 31 33 31 30 32 5d 20 5b 48 31 33 31 30 33 5d 0a  13102] [H13103].
1ee30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1ee40 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1ee50 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f 73 74  3_sql(sqlite3_st
1ee60 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a  mt *pStmt);../*.
1ee70 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 79 6e  ** CAPI3REF: Dyn
1ee80 61 6d 69 63 61 6c 6c 79 20 54 79 70 65 64 20 56  amically Typed V
1ee90 61 6c 75 65 20 4f 62 6a 65 63 74 20 7b 48 31 35  alue Object {H15
1eea0 30 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a  000} <S20200>.**
1eeb0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 6f 74   KEYWORDS: {prot
1eec0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
1eed0 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 65 63 74 65  lue} {unprotecte
1eee0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d  d sqlite3_value}
1eef0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73  .**.** SQLite us
1ef00 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  es the sqlite3_v
1ef10 61 6c 75 65 20 6f 62 6a 65 63 74 20 74 6f 20 72  alue object to r
1ef20 65 70 72 65 73 65 6e 74 20 61 6c 6c 20 76 61 6c  epresent all val
1ef30 75 65 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20  ues.** that can 
1ef40 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 64  be stored in a d
1ef50 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 20 53  atabase table. S
1ef60 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d  QLite uses dynam
1ef70 69 63 20 74 79 70 69 6e 67 0a 2a 2a 20 66 6f 72  ic typing.** for
1ef80 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 73   the values it s
1ef90 74 6f 72 65 73 2e 20 56 61 6c 75 65 73 20 73 74  tores. Values st
1efa0 6f 72 65 64 20 69 6e 20 73 71 6c 69 74 65 33 5f  ored in sqlite3_
1efb0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 0a 2a 2a  value objects.**
1efc0 20 63 61 6e 20 62 65 20 69 6e 74 65 67 65 72 73   can be integers
1efd0 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  , floating point
1efe0 20 76 61 6c 75 65 73 2c 20 73 74 72 69 6e 67 73   values, strings
1eff0 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c  , BLOBs, or NULL
1f000 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 71 6c 69 74  ..**.** An sqlit
1f010 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20  e3_value object 
1f020 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 22 70  may be either "p
1f030 72 6f 74 65 63 74 65 64 22 20 6f 72 20 22 75 6e  rotected" or "un
1f040 70 72 6f 74 65 63 74 65 64 22 2e 0a 2a 2a 20 53  protected"..** S
1f050 6f 6d 65 20 69 6e 74 65 72 66 61 63 65 73 20 72  ome interfaces r
1f060 65 71 75 69 72 65 20 61 20 70 72 6f 74 65 63 74  equire a protect
1f070 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
1f080 2e 20 20 4f 74 68 65 72 20 69 6e 74 65 72 66 61  .  Other interfa
1f090 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 61 63 63 65  ces.** will acce
1f0a0 70 74 20 65 69 74 68 65 72 20 61 20 70 72 6f 74  pt either a prot
1f0b0 65 63 74 65 64 20 6f 72 20 61 6e 20 75 6e 70 72  ected or an unpr
1f0c0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
1f0d0 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 65 72 79 20  value..** Every 
1f0e0 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 61  interface that a
1f0f0 63 63 65 70 74 73 20 73 71 6c 69 74 65 33 5f 76  ccepts sqlite3_v
1f100 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 73 20 73  alue arguments s
1f110 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 65 74  pecifies.** whet
1f120 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 20 72 65  her or not it re
1f130 71 75 69 72 65 73 20 61 20 70 72 6f 74 65 63 74  quires a protect
1f140 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
1f150 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d  ..**.** The term
1f160 73 20 22 70 72 6f 74 65 63 74 65 64 22 20 61 6e  s "protected" an
1f170 64 20 22 75 6e 70 72 6f 74 65 63 74 65 64 22 20  d "unprotected" 
1f180 72 65 66 65 72 20 74 6f 20 77 68 65 74 68 65 72  refer to whether
1f190 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74   or not.** a mut
1f1a0 65 78 20 69 73 20 68 65 6c 64 2e 20 20 41 20 69  ex is held.  A i
1f1b0 6e 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 73  nternal mutex is
1f1c0 20 68 65 6c 64 20 66 6f 72 20 61 20 70 72 6f 74   held for a prot
1f1d0 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ected.** sqlite3
1f1e0 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 62 75  _value object bu
1f1f0 74 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 68 65  t no mutex is he
1f200 6c 64 20 66 6f 72 20 61 6e 20 75 6e 70 72 6f 74  ld for an unprot
1f210 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ected.** sqlite3
1f220 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 20 20  _value object.  
1f230 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  If SQLite is com
1f240 70 69 6c 65 64 20 74 6f 20 62 65 20 73 69 6e 67  piled to be sing
1f250 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 28  le-threaded.** (
1f260 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 54 48 52  with [SQLITE_THR
1f270 45 41 44 53 41 46 45 3d 30 5d 20 61 6e 64 20 77  EADSAFE=0] and w
1f280 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 74 68 72  ith [sqlite3_thr
1f290 65 61 64 73 61 66 65 28 29 5d 20 72 65 74 75 72  eadsafe()] retur
1f2a0 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f 72 20 69 66  ning 0).** or if
1f2b0 20 53 51 4c 69 74 65 20 69 73 20 72 75 6e 20 69   SQLite is run i
1f2c0 6e 20 6f 6e 65 20 6f 66 20 72 65 64 75 63 65 64  n one of reduced
1f2d0 20 6d 75 74 65 78 20 6d 6f 64 65 73 20 0a 2a 2a   mutex modes .**
1f2e0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
1f2f0 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 20 6f 72  SINGLETHREAD] or
1f300 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
1f310 4d 55 4c 54 49 54 48 52 45 41 44 5d 0a 2a 2a 20  MULTITHREAD].** 
1f320 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f  then there is no
1f330 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74   distinction bet
1f340 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 61  ween protected a
1f350 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a  nd unprotected.*
1f360 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  * sqlite3_value 
1f370 6f 62 6a 65 63 74 73 20 61 6e 64 20 74 68 65 79  objects and they
1f380 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 74   can be used int
1f390 65 72 63 68 61 6e 67 65 61 62 6c 79 2e 20 20 48  erchangeably.  H
1f3a0 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d  owever,.** for m
1f3b0 61 78 69 6d 75 6d 20 63 6f 64 65 20 70 6f 72 74  aximum code port
1f3c0 61 62 69 6c 69 74 79 20 69 74 20 69 73 20 72 65  ability it is re
1f3d0 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61  commended that a
1f3e0 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73  pplications.** s
1f3f0 74 69 6c 6c 20 6d 61 6b 65 20 74 68 65 20 64 69  till make the di
1f400 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65  stinction betwee
1f410 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74 65 63  n between protec
1f420 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 65 63  ted and unprotec
1f430 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  ted.** sqlite3_v
1f440 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 65 76 65  alue objects eve
1f450 6e 20 77 68 65 6e 20 6e 6f 74 20 73 74 72 69 63  n when not stric
1f460 74 6c 79 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a  tly required..**
1f470 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
1f480 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 74 68  value objects th
1f490 61 74 20 61 72 65 20 70 61 73 73 65 64 20 61 73  at are passed as
1f4a0 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 74 6f   parameters into
1f4b0 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e   the.** implemen
1f4c0 74 61 74 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69  tation of [appli
1f4d0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53  cation-defined S
1f4e0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72  QL functions] ar
1f4f0 65 20 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20  e protected..** 
1f500 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  The sqlite3_valu
1f510 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65  e object returne
1f520 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  d by.** [sqlite3
1f530 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d  _column_value()]
1f540 20 69 73 20 75 6e 70 72 6f 74 65 63 74 65 64 2e   is unprotected.
1f550 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 74 65 64 20  .** Unprotected 
1f560 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
1f570 6a 65 63 74 73 20 6d 61 79 20 6f 6e 6c 79 20 62  jects may only b
1f580 65 20 75 73 65 64 20 77 69 74 68 0a 2a 2a 20 5b  e used with.** [
1f590 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
1f5a0 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  alue()] and [sql
1f5b0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28  ite3_bind_value(
1f5c0 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  )]..** The [sqli
1f5d0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 7c  te3_value_blob |
1f5e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1f5f0 79 70 65 28 29 5d 20 66 61 6d 69 6c 79 20 6f 66  ype()] family of
1f600 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 72  .** interfaces r
1f610 65 71 75 69 72 65 20 70 72 6f 74 65 63 74 65 64  equire protected
1f620 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
1f630 62 6a 65 63 74 73 2e 0a 2a 2f 0a 74 79 70 65 64  bjects..*/.typed
1f640 65 66 20 73 74 72 75 63 74 20 4d 65 6d 20 73 71  ef struct Mem sq
1f650 6c 69 74 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a  lite3_value;../*
1f660 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51  .** CAPI3REF: SQ
1f670 4c 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65  L Function Conte
1f680 78 74 20 4f 62 6a 65 63 74 20 7b 48 31 36 30 30  xt Object {H1600
1f690 31 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a  1} <S20200>.**.*
1f6a0 2a 20 54 68 65 20 63 6f 6e 74 65 78 74 20 69 6e  * The context in
1f6b0 20 77 68 69 63 68 20 61 6e 20 53 51 4c 20 66 75   which an SQL fu
1f6c0 6e 63 74 69 6f 6e 20 65 78 65 63 75 74 65 73 20  nction executes 
1f6d0 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 6e 0a  is stored in an.
1f6e0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ** sqlite3_conte
1f6f0 78 74 20 6f 62 6a 65 63 74 2e 20 20 41 20 70 6f  xt object.  A po
1f700 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69  inter to an sqli
1f710 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65  te3_context obje
1f720 63 74 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20  ct.** is always 
1f730 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
1f740 74 6f 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d  to [application-
1f750 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63  defined SQL func
1f760 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 54 68 65 20 61  tions]..** The a
1f770 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
1f780 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ed SQL function 
1f790 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
1f7a0 69 6c 6c 20 70 61 73 73 20 74 68 69 73 0a 2a 2a  ill pass this.**
1f7b0 20 70 6f 69 6e 74 65 72 20 74 68 72 6f 75 67 68   pointer through
1f7c0 20 69 6e 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b   into calls to [
1f7d0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
1f7e0 6e 74 20 7c 20 73 71 6c 69 74 65 33 5f 72 65 73  nt | sqlite3_res
1f7f0 75 6c 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  ult()],.** [sqli
1f800 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
1f810 6e 74 65 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74  ntext()], [sqlit
1f820 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2c  e3_user_data()],
1f830 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  .** [sqlite3_con
1f840 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29  text_db_handle()
1f850 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f  ], [sqlite3_get_
1f860 61 75 78 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 61  auxdata()],.** a
1f870 6e 64 2f 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73  nd/or [sqlite3_s
1f880 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2e 0a 2a  et_auxdata()]..*
1f890 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
1f8a0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1f8b0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1f8c0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
1f8d0 46 3a 20 42 69 6e 64 69 6e 67 20 56 61 6c 75 65  F: Binding Value
1f8e0 73 20 54 6f 20 50 72 65 70 61 72 65 64 20 53 74  s To Prepared St
1f8f0 61 74 65 6d 65 6e 74 73 20 7b 48 31 33 35 30 30  atements {H13500
1f900 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 20 4b 45  } <S70300>.** KE
1f910 59 57 4f 52 44 53 3a 20 7b 68 6f 73 74 20 70 61  YWORDS: {host pa
1f920 72 61 6d 65 74 65 72 7d 20 7b 68 6f 73 74 20 70  rameter} {host p
1f930 61 72 61 6d 65 74 65 72 73 7d 20 7b 68 6f 73 74  arameters} {host
1f940 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 7d   parameter name}
1f950 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53  .** KEYWORDS: {S
1f960 51 4c 20 70 61 72 61 6d 65 74 65 72 7d 20 7b 53  QL parameter} {S
1f970 51 4c 20 70 61 72 61 6d 65 74 65 72 73 7d 20 7b  QL parameters} {
1f980 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e  parameter bindin
1f990 67 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  g}.**.** In the 
1f9a0 53 51 4c 20 73 74 72 69 6e 67 73 20 69 6e 70 75  SQL strings inpu
1f9b0 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72  t to [sqlite3_pr
1f9c0 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
1f9d0 69 74 73 20 76 61 72 69 61 6e 74 73 2c 0a 2a 2a  its variants,.**
1f9e0 20 6c 69 74 65 72 61 6c 73 20 6d 61 79 20 62 65   literals may be
1f9f0 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 20 5b   replaced by a [
1fa00 70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 6f 6e  parameter] in on
1fa10 65 20 6f 66 20 74 68 65 73 65 20 66 6f 72 6d 73  e of these forms
1fa20 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
1fa30 3c 6c 69 3e 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20  <li>  ?.** <li> 
1fa40 20 3f 4e 4e 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a   ?NNN.** <li>  :
1fa50 56 56 56 0a 2a 2a 20 3c 6c 69 3e 20 20 40 56 56  VVV.** <li>  @VV
1fa60 56 0a 2a 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0a  V.** <li>  $VVV.
1fa70 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49  ** </ul>.**.** I
1fa80 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  n the parameter 
1fa90 66 6f 72 6d 73 20 73 68 6f 77 6e 20 61 62 6f 76  forms shown abov
1faa0 65 20 4e 4e 4e 20 69 73 20 61 6e 20 69 6e 74 65  e NNN is an inte
1fab0 67 65 72 20 6c 69 74 65 72 61 6c 2c 0a 2a 2a 20  ger literal,.** 
1fac0 61 6e 64 20 56 56 56 20 69 73 20 61 6e 20 61 6c  and VVV is an al
1fad0 70 68 61 2d 6e 75 6d 65 72 69 63 20 70 61 72 61  pha-numeric para
1fae0 6d 65 74 65 72 20 6e 61 6d 65 2e 20 54 68 65 20  meter name. The 
1faf0 76 61 6c 75 65 73 20 6f 66 20 74 68 65 73 65 0a  values of these.
1fb00 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 28 61  ** parameters (a
1fb10 6c 73 6f 20 63 61 6c 6c 65 64 20 22 68 6f 73 74  lso called "host
1fb20 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 73   parameter names
1fb30 22 20 6f 72 20 22 53 51 4c 20 70 61 72 61 6d 65  " or "SQL parame
1fb40 74 65 72 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65  ters").** can be
1fb50 20 73 65 74 20 75 73 69 6e 67 20 74 68 65 20 73   set using the s
1fb60 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20  qlite3_bind_*() 
1fb70 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64  routines defined
1fb80 20 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   here..**.** The
1fb90 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
1fba0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  to the sqlite3_b
1fbb0 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73  ind_*() routines
1fbc0 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20   is always.** a 
1fbd0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b  pointer to the [
1fbe0 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62  sqlite3_stmt] ob
1fbf0 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 66 72  ject returned fr
1fc00 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70  om.** [sqlite3_p
1fc10 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
1fc20 69 74 73 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a  its variants..**
1fc30 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61  .** The second a
1fc40 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69  rgument is the i
1fc50 6e 64 65 78 20 6f 66 20 74 68 65 20 53 51 4c 20  ndex of the SQL 
1fc60 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20  parameter to be 
1fc70 73 65 74 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74  set..** The left
1fc80 6d 6f 73 74 20 53 51 4c 20 70 61 72 61 6d 65 74  most SQL paramet
1fc90 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  er has an index 
1fca0 6f 66 20 31 2e 20 20 57 68 65 6e 20 74 68 65 20  of 1.  When the 
1fcb0 73 61 6d 65 20 6e 61 6d 65 64 0a 2a 2a 20 53 51  same named.** SQ
1fcc0 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75  L parameter is u
1fcd0 73 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  sed more than on
1fce0 63 65 2c 20 73 65 63 6f 6e 64 20 61 6e 64 20 73  ce, second and s
1fcf0 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 63  ubsequent.** occ
1fd00 75 72 72 65 6e 63 65 73 20 68 61 76 65 20 74 68  urrences have th
1fd10 65 20 73 61 6d 65 20 69 6e 64 65 78 20 61 73 20  e same index as 
1fd20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 72 72  the first occurr
1fd30 65 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 64  ence..** The ind
1fd40 65 78 20 66 6f 72 20 6e 61 6d 65 64 20 70 61 72  ex for named par
1fd50 61 6d 65 74 65 72 73 20 63 61 6e 20 62 65 20 6c  ameters can be l
1fd60 6f 6f 6b 65 64 20 75 70 20 75 73 69 6e 67 20 74  ooked up using t
1fd70 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  he.** [sqlite3_b
1fd80 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
1fd90 64 65 78 28 29 5d 20 41 50 49 20 69 66 20 64 65  dex()] API if de
1fda0 73 69 72 65 64 2e 20 20 54 68 65 20 69 6e 64 65  sired.  The inde
1fdb0 78 0a 2a 2a 20 66 6f 72 20 22 3f 4e 4e 4e 22 20  x.** for "?NNN" 
1fdc0 70 61 72 61 6d 65 74 65 72 73 20 69 73 20 74 68  parameters is th
1fdd0 65 20 76 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a  e value of NNN..
1fde0 2a 2a 20 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65  ** The NNN value
1fdf0 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e   must be between
1fe00 20 31 20 61 6e 64 20 74 68 65 20 5b 73 71 6c 69   1 and the [sqli
1fe10 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20  te3_limit()].** 
1fe20 70 61 72 61 6d 65 74 65 72 20 5b 53 51 4c 49 54  parameter [SQLIT
1fe30 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45  E_LIMIT_VARIABLE
1fe40 5f 4e 55 4d 42 45 52 5d 20 28 64 65 66 61 75 6c  _NUMBER] (defaul
1fe50 74 20 76 61 6c 75 65 3a 20 39 39 39 29 2e 0a 2a  t value: 999)..*
1fe60 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61  *.** The third a
1fe70 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 76  rgument is the v
1fe80 61 6c 75 65 20 74 6f 20 62 69 6e 64 20 74 6f 20  alue to bind to 
1fe90 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  the parameter..*
1fea0 2a 0a 2a 2a 20 49 6e 20 74 68 6f 73 65 20 72 6f  *.** In those ro
1feb0 75 74 69 6e 65 73 20 74 68 61 74 20 68 61 76 65  utines that have
1fec0 20 61 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65   a fourth argume
1fed0 6e 74 2c 20 69 74 73 20 76 61 6c 75 65 20 69 73  nt, its value is
1fee0 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
1fef0 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 70  f bytes in the p
1ff00 61 72 61 6d 65 74 65 72 2e 20 20 54 6f 20 62 65  arameter.  To be
1ff10 20 63 6c 65 61 72 3a 20 74 68 65 20 76 61 6c 75   clear: the valu
1ff20 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  e is the.** numb
1ff30 65 72 20 6f 66 20 3c 75 3e 62 79 74 65 73 3c 2f  er of <u>bytes</
1ff40 75 3e 20 69 6e 20 74 68 65 20 76 61 6c 75 65 2c  u> in the value,
1ff50 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20   not the number 
1ff60 6f 66 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a  of characters..*
1ff70 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20  * If the fourth 
1ff80 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67  parameter is neg
1ff90 61 74 69 76 65 2c 20 74 68 65 20 6c 65 6e 67 74  ative, the lengt
1ffa0 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20  h of the string 
1ffb0 69 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  is.** the number
1ffc0 20 6f 66 20 62 79 74 65 73 20 75 70 20 74 6f 20   of bytes up to 
1ffd0 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 74  the first zero t
1ffe0 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  erminator..**.**
1fff0 20 54 68 65 20 66 69 66 74 68 20 61 72 67 75 6d   The fifth argum
20000 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  ent to sqlite3_b
20010 69 6e 64 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69  ind_blob(), sqli
20020 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c  te3_bind_text(),
20030 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   and.** sqlite3_
20040 62 69 6e 64 5f 74 65 78 74 31 36 28 29 20 69 73  bind_text16() is
20050 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 75 73   a destructor us
20060 65 64 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66  ed to dispose of
20070 20 74 68 65 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20   the BLOB or.** 
20080 73 74 72 69 6e 67 20 61 66 74 65 72 20 53 51 4c  string after SQL
20090 69 74 65 20 68 61 73 20 66 69 6e 69 73 68 65 64  ite has finished
200a0 20 77 69 74 68 20 69 74 2e 20 49 66 20 74 68 65   with it. If the
200b0 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20   fifth argument 
200c0 69 73 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61  is.** the specia
200d0 6c 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f  l value [SQLITE_
200e0 53 54 41 54 49 43 5d 2c 20 74 68 65 6e 20 53 51  STATIC], then SQ
200f0 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74 68 61  Lite assumes tha
20100 74 20 74 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61  t the.** informa
20110 74 69 6f 6e 20 69 73 20 69 6e 20 73 74 61 74 69  tion is in stati
20120 63 2c 20 75 6e 6d 61 6e 61 67 65 64 20 73 70 61  c, unmanaged spa
20130 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20  ce and does not 
20140 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64  need to be freed
20150 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 66 74  ..** If the fift
20160 68 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 74  h argument has t
20170 68 65 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45  he value [SQLITE
20180 5f 54 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65  _TRANSIENT], the
20190 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65  n.** SQLite make
201a0 73 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74  s its own privat
201b0 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 61  e copy of the da
201c0 74 61 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20  ta immediately, 
201d0 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71  before.** the sq
201e0 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72  lite3_bind_*() r
201f0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a  outine returns..
20200 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
20210 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28  3_bind_zeroblob(
20220 29 20 72 6f 75 74 69 6e 65 20 62 69 6e 64 73 20  ) routine binds 
20230 61 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68  a BLOB of length
20240 20 4e 20 74 68 61 74 0a 2a 2a 20 69 73 20 66 69   N that.** is fi
20250 6c 6c 65 64 20 77 69 74 68 20 7a 65 72 6f 65 73  lled with zeroes
20260 2e 20 20 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73  .  A zeroblob us
20270 65 73 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e  es a fixed amoun
20280 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28  t of memory.** (
20290 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20  just an integer 
202a0 74 6f 20 68 6f 6c 64 20 69 74 73 20 73 69 7a 65  to hold its size
202b0 29 20 77 68 69 6c 65 20 69 74 20 69 73 20 62 65  ) while it is be
202c0 69 6e 67 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a  ing processed..*
202d0 2a 20 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20  * Zeroblobs are 
202e0 69 6e 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76  intended to serv
202f0 65 20 61 73 20 70 6c 61 63 65 68 6f 6c 64 65 72  e as placeholder
20300 73 20 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73  s for BLOBs whos
20310 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20  e.** content is 
20320 6c 61 74 65 72 20 77 72 69 74 74 65 6e 20 75 73  later written us
20330 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ing.** [sqlite3_
20340 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72  blob_open | incr
20350 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f  emental BLOB I/O
20360 5d 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41  ] routines..** A
20370 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20   negative value 
20380 66 6f 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f 62  for the zeroblob
20390 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 7a 65   results in a ze
203a0 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e 0a  ro-length BLOB..
203b0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
203c0 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69  3_bind_*() routi
203d0 6e 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  nes must be call
203e0 65 64 20 61 66 74 65 72 0a 2a 2a 20 5b 73 71 6c  ed after.** [sql
203f0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
20400 29 5d 20 28 61 6e 64 20 69 74 73 20 76 61 72 69  )] (and its vari
20410 61 6e 74 73 29 20 6f 72 20 5b 73 71 6c 69 74 65  ants) or [sqlite
20420 33 5f 72 65 73 65 74 28 29 5d 20 61 6e 64 0a 2a  3_reset()] and.*
20430 2a 20 62 65 66 6f 72 65 20 5b 73 71 6c 69 74 65  * before [sqlite
20440 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 42 69  3_step()]..** Bi
20450 6e 64 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 63  ndings are not c
20460 6c 65 61 72 65 64 20 62 79 20 74 68 65 20 5b 73  leared by the [s
20470 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
20480 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f  routine..** Unbo
20490 75 6e 64 20 70 61 72 61 6d 65 74 65 72 73 20 61  und parameters a
204a0 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  re interpreted a
204b0 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68  s NULL..**.** Th
204c0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74  ese routines ret
204d0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  urn [SQLITE_OK] 
204e0 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e  on success or an
204f0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a   error code if.*
20500 2a 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20  * anything goes 
20510 77 72 6f 6e 67 2e 20 20 5b 53 51 4c 49 54 45 5f  wrong.  [SQLITE_
20520 52 41 4e 47 45 5d 20 69 73 20 72 65 74 75 72 6e  RANGE] is return
20530 65 64 20 69 66 20 74 68 65 20 70 61 72 61 6d 65  ed if the parame
20540 74 65 72 0a 2a 2a 20 69 6e 64 65 78 20 69 73 20  ter.** index is 
20550 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 5b  out of range.  [
20560 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73  SQLITE_NOMEM] is
20570 20 72 65 74 75 72 6e 65 64 20 69 66 20 6d 61 6c   returned if mal
20580 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20  loc() fails..** 
20590 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20  [SQLITE_MISUSE] 
205a0 6d 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65  might be returne
205b0 64 20 69 66 20 74 68 65 73 65 20 72 6f 75 74 69  d if these routi
205c0 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 6f  nes are called o
205d0 6e 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d  n a.** virtual m
205e0 61 63 68 69 6e 65 20 74 68 61 74 20 69 73 20 74  achine that is t
205f0 68 65 20 77 72 6f 6e 67 20 73 74 61 74 65 20 6f  he wrong state o
20600 72 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65  r which has alre
20610 61 64 79 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a  ady been finaliz
20620 65 64 2e 0a 2a 2a 20 44 65 74 65 63 74 69 6f 6e  ed..** Detection
20630 20 6f 66 20 6d 69 73 75 73 65 20 69 73 20 75 6e   of misuse is un
20640 72 65 6c 69 61 62 6c 65 2e 20 20 41 70 70 6c 69  reliable.  Appli
20650 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e  cations should n
20660 6f 74 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20  ot depend.** on 
20670 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 72 65  SQLITE_MISUSE re
20680 74 75 72 6e 73 2e 20 20 53 51 4c 49 54 45 5f 4d  turns.  SQLITE_M
20690 49 53 55 53 45 20 69 73 20 69 6e 74 65 6e 64 65  ISUSE is intende
206a0 64 20 74 6f 20 69 6e 64 69 63 61 74 65 20 61 0a  d to indicate a.
206b0 2a 2a 20 61 20 6c 6f 67 69 63 20 65 72 72 6f 72  ** a logic error
206c0 20 69 6e 20 74 68 65 20 61 70 70 6c 69 63 61 74   in the applicat
206d0 69 6f 6e 2e 20 20 46 75 74 75 72 65 20 76 65 72  ion.  Future ver
206e0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
206f0 6d 69 67 68 74 0a 2a 2a 20 70 61 6e 69 63 20 72  might.** panic r
20700 61 74 68 65 72 20 74 68 61 6e 20 72 65 74 75 72  ather than retur
20710 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e  n SQLITE_MISUSE.
20720 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
20730 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
20740 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29  arameter_count()
20750 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  ],.** [sqlite3_b
20760 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
20770 6d 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69  me()], and [sqli
20780 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
20790 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a  er_index()]..**.
207a0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
207b0 0a 2a 2a 20 5b 48 31 33 35 30 36 5d 20 5b 48 31  .** [H13506] [H1
207c0 33 35 30 39 5d 20 5b 48 31 33 35 31 32 5d 20 5b  3509] [H13512] [
207d0 48 31 33 35 31 35 5d 20 5b 48 31 33 35 31 38 5d  H13515] [H13518]
207e0 20 5b 48 31 33 35 32 31 5d 20 5b 48 31 33 35 32   [H13521] [H1352
207f0 34 5d 20 5b 48 31 33 35 32 37 5d 0a 2a 2a 20 5b  4] [H13527].** [
20800 48 31 33 35 33 30 5d 20 5b 48 31 33 35 33 33 5d  H13530] [H13533]
20810 20 5b 48 31 33 35 33 36 5d 20 5b 48 31 33 35 33   [H13536] [H1353
20820 39 5d 20 5b 48 31 33 35 34 32 5d 20 5b 48 31 33  9] [H13542] [H13
20830 35 34 35 5d 20 5b 48 31 33 35 34 38 5d 20 5b 48  545] [H13548] [H
20840 31 33 35 35 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c  13551].**.*/.SQL
20850 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
20860 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71  te3_bind_blob(sq
20870 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
20880 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
20890 6e 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f  nt n, void(*)(vo
208a0 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50  id*));.SQLITE_AP
208b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
208c0 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  nd_double(sqlite
208d0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f  3_stmt*, int, do
208e0 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50  uble);.SQLITE_AP
208f0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
20900 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73  nd_int(sqlite3_s
20910 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  tmt*, int, int);
20920 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
20930 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
20940 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  64(sqlite3_stmt*
20950 2c 20 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69  , int, sqlite3_i
20960 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50  nt64);.SQLITE_AP
20970 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
20980 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f  nd_null(sqlite3_
20990 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  stmt*, int);.SQL
209a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
209b0 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 73 71  te3_bind_text(sq
209c0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
209d0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69  , const char*, i
209e0 6e 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f  nt n, void(*)(vo
209f0 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50  id*));.SQLITE_AP
20a00 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
20a10 6e 64 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65  nd_text16(sqlite
20a20 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
20a30 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20  nst void*, int, 
20a40 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
20a50 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
20a60 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c  sqlite3_bind_val
20a70 75 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  ue(sqlite3_stmt*
20a80 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c  , int, const sql
20a90 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
20aa0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
20ab0 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c  ite3_bind_zerobl
20ac0 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  ob(sqlite3_stmt*
20ad0 2c 20 69 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a  , int, int n);..
20ae0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
20af0 4e 75 6d 62 65 72 20 4f 66 20 53 51 4c 20 50 61  Number Of SQL Pa
20b00 72 61 6d 65 74 65 72 73 20 7b 48 31 33 36 30 30  rameters {H13600
20b10 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a  } <S70300>.**.**
20b20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
20b30 6e 20 62 65 20 75 73 65 64 20 74 6f 20 66 69 6e  n be used to fin
20b40 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  d the number of 
20b50 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 5d  [SQL parameters]
20b60 0a 2a 2a 20 69 6e 20 61 20 5b 70 72 65 70 61 72  .** in a [prepar
20b70 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20  ed statement].  
20b80 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 61  SQL parameters a
20b90 72 65 20 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65  re tokens of the
20ba0 0a 2a 2a 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f  .** form "?", "?
20bb0 4e 4e 4e 22 2c 20 22 3a 41 41 41 22 2c 20 22 24  NNN", ":AAA", "$
20bc0 41 41 41 22 2c 20 6f 72 20 22 40 41 41 41 22 20  AAA", or "@AAA" 
20bd0 74 68 61 74 20 73 65 72 76 65 20 61 73 0a 2a 2a  that serve as.**
20be0 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f   placeholders fo
20bf0 72 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 72  r values that ar
20c00 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  e [sqlite3_bind_
20c10 62 6c 6f 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a  blob | bound].**
20c20 20 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74 65   to the paramete
20c30 72 73 20 61 74 20 61 20 6c 61 74 65 72 20 74 69  rs at a later ti
20c40 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  me..**.** This r
20c50 6f 75 74 69 6e 65 20 61 63 74 75 61 6c 6c 79 20  outine actually 
20c60 72 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65  returns the inde
20c70 78 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74  x of the largest
20c80 20 28 72 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20   (rightmost).** 
20c90 70 61 72 61 6d 65 74 65 72 2e 20 46 6f 72 20 61  parameter. For a
20ca0 6c 6c 20 66 6f 72 6d 73 20 65 78 63 65 70 74 20  ll forms except 
20cb0 3f 4e 4e 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20  ?NNN, this will 
20cc0 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68  correspond to th
20cd0 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75  e.** number of u
20ce0 6e 69 71 75 65 20 70 61 72 61 6d 65 74 65 72 73  nique parameters
20cf0 2e 20 20 49 66 20 70 61 72 61 6d 65 74 65 72 73  .  If parameters
20d00 20 6f 66 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65   of the ?NNN are
20d10 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20   used,.** there 
20d20 6d 61 79 20 62 65 20 67 61 70 73 20 69 6e 20 74  may be gaps in t
20d30 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53  he list..**.** S
20d40 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65  ee also: [sqlite
20d50 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69  3_bind_blob|sqli
20d60 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20  te3_bind()],.** 
20d70 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
20d80 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c  rameter_name()],
20d90 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   and.** [sqlite3
20da0 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
20db0 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  index()]..**.** 
20dc0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
20dd0 20 5b 48 31 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c   [H13601].*/.SQL
20de0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
20df0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
20e00 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  er_count(sqlite3
20e10 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  _stmt*);../*.** 
20e20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f  CAPI3REF: Name O
20e30 66 20 41 20 48 6f 73 74 20 50 61 72 61 6d 65 74  f A Host Paramet
20e40 65 72 20 7b 48 31 33 36 32 30 7d 20 3c 53 37 30  er {H13620} <S70
20e50 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  300>.**.** This 
20e60 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
20e70 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
20e80 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74   name of the n-t
20e90 68 0a 2a 2a 20 5b 53 51 4c 20 70 61 72 61 6d 65  h.** [SQL parame
20ea0 74 65 72 5d 20 69 6e 20 61 20 5b 70 72 65 70 61  ter] in a [prepa
20eb0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a  red statement]..
20ec0 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  ** SQL parameter
20ed0 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f  s of the form "?
20ee0 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f  NNN" or ":AAA" o
20ef0 72 20 22 40 41 41 41 22 20 6f 72 20 22 24 41 41  r "@AAA" or "$AA
20f00 41 22 0a 2a 2a 20 68 61 76 65 20 61 20 6e 61 6d  A".** have a nam
20f10 65 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73  e which is the s
20f20 74 72 69 6e 67 20 22 3f 4e 4e 4e 22 20 6f 72 20  tring "?NNN" or 
20f30 22 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41 22  ":AAA" or "@AAA"
20f40 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 72 65   or "$AAA".** re
20f50 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 49  spectively..** I
20f60 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74  n other words, t
20f70 68 65 20 69 6e 69 74 69 61 6c 20 22 3a 22 20 6f  he initial ":" o
20f80 72 20 22 24 22 20 6f 72 20 22 40 22 20 6f 72 20  r "$" or "@" or 
20f90 22 3f 22 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64  "?".** is includ
20fa0 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
20fb0 65 20 6e 61 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d  e name..** Param
20fc0 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72  eters of the for
20fd0 6d 20 22 3f 22 20 77 69 74 68 6f 75 74 20 61 20  m "?" without a 
20fe0 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65  following intege
20ff0 72 20 68 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a  r have no name.*
21000 2a 20 61 6e 64 20 61 72 65 20 61 6c 73 6f 20 72  * and are also r
21010 65 66 65 72 72 65 64 20 74 6f 20 61 73 20 22 61  eferred to as "a
21020 6e 6f 6e 79 6d 6f 75 73 20 70 61 72 61 6d 65 74  nonymous paramet
21030 65 72 73 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ers"..**.** The 
21040 66 69 72 73 74 20 68 6f 73 74 20 70 61 72 61 6d  first host param
21050 65 74 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65  eter has an inde
21060 78 20 6f 66 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a  x of 1, not 0..*
21070 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  *.** If the valu
21080 65 20 6e 20 69 73 20 6f 75 74 20 6f 66 20 72 61  e n is out of ra
21090 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 6e 2d  nge or if the n-
210a0 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a  th parameter is.
210b0 2a 2a 20 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65  ** nameless, the
210c0 6e 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  n NULL is return
210d0 65 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65  ed.  The returne
210e0 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61  d string is.** a
210f0 6c 77 61 79 73 20 69 6e 20 55 54 46 2d 38 20 65  lways in UTF-8 e
21100 6e 63 6f 64 69 6e 67 20 65 76 65 6e 20 69 66 20  ncoding even if 
21110 74 68 65 20 6e 61 6d 65 64 20 70 61 72 61 6d 65  the named parame
21120 74 65 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69  ter was.** origi
21130 6e 61 6c 6c 79 20 73 70 65 63 69 66 69 65 64 20  nally specified 
21140 61 73 20 55 54 46 2d 31 36 20 69 6e 20 5b 73 71  as UTF-16 in [sq
21150 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28  lite3_prepare16(
21160 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  )] or.** [sqlite
21170 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29  3_prepare16_v2()
21180 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  ]..**.** See als
21190 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  o: [sqlite3_bind
211a0 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69  _blob|sqlite3_bi
211b0 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  nd()],.** [sqlit
211c0 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
211d0 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a  r_count()], and.
211e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  ** [sqlite3_bind
211f0 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
21200 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ()]..**.** Requi
21210 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33  rements:.** [H13
21220 36 32 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  621].*/.SQLITE_A
21230 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
21240 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
21250 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74  meter_name(sqlit
21260 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a  e3_stmt*, int);.
21270 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
21280 20 49 6e 64 65 78 20 4f 66 20 41 20 50 61 72 61   Index Of A Para
21290 6d 65 74 65 72 20 57 69 74 68 20 41 20 47 69 76  meter With A Giv
212a0 65 6e 20 4e 61 6d 65 20 7b 48 31 33 36 34 30 7d  en Name {H13640}
212b0 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S70300>.**.** 
212c0 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78  Return the index
212d0 20 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d   of an SQL param
212e0 65 74 65 72 20 67 69 76 65 6e 20 69 74 73 20 6e  eter given its n
212f0 61 6d 65 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 64  ame.  The.** ind
21300 65 78 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  ex value returne
21310 64 20 69 73 20 73 75 69 74 61 62 6c 65 20 66 6f  d is suitable fo
21320 72 20 75 73 65 20 61 73 20 74 68 65 20 73 65 63  r use as the sec
21330 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ond.** parameter
21340 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e   to [sqlite3_bin
21350 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62  d_blob|sqlite3_b
21360 69 6e 64 28 29 5d 2e 20 20 41 20 7a 65 72 6f 0a  ind()].  A zero.
21370 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ** is returned i
21380 66 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 70 61  f no matching pa
21390 72 61 6d 65 74 65 72 20 69 73 20 66 6f 75 6e 64  rameter is found
213a0 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 72  .  The parameter
213b0 0a 2a 2a 20 6e 61 6d 65 20 6d 75 73 74 20 62 65  .** name must be
213c0 20 67 69 76 65 6e 20 69 6e 20 55 54 46 2d 38 20   given in UTF-8 
213d0 65 76 65 6e 20 69 66 20 74 68 65 20 6f 72 69 67  even if the orig
213e0 69 6e 61 6c 20 73 74 61 74 65 6d 65 6e 74 0a 2a  inal statement.*
213f0 2a 20 77 61 73 20 70 72 65 70 61 72 65 64 20 66  * was prepared f
21400 72 6f 6d 20 55 54 46 2d 31 36 20 74 65 78 74 20  rom UTF-16 text 
21410 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70  using [sqlite3_p
21420 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a  repare16_v2()]..
21430 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
21440 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
21450 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28  ob|sqlite3_bind(
21460 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  )],.** [sqlite3_
21470 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
21480 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20  ount()], and.** 
21490 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
214a0 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d  rameter_index()]
214b0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
214c0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 34 31  ents:.** [H13641
214d0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
214e0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
214f0 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
21500 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
21510 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
21520 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  e);../*.** CAPI3
21530 52 45 46 3a 20 52 65 73 65 74 20 41 6c 6c 20 42  REF: Reset All B
21540 69 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50 72 65  indings On A Pre
21550 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20  pared Statement 
21560 7b 48 31 33 36 36 30 7d 20 3c 53 37 30 33 30 30  {H13660} <S70300
21570 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 79  >.**.** Contrary
21580 20 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69 6f   to the intuitio
21590 6e 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69  n of many, [sqli
215a0 74 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f 65  te3_reset()] doe
215b0 73 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20 74  s not reset.** t
215c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  he [sqlite3_bind
215d0 5f 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73  _blob | bindings
215e0 5d 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64  ] on a [prepared
215f0 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20   statement]..** 
21600 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  Use this routine
21610 20 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 68 6f   to reset all ho
21620 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  st parameters to
21630 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71   NULL..**.** Req
21640 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
21650 31 33 36 36 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13661].*/.SQLITE
21660 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
21670 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28  _clear_bindings(
21680 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
21690 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
216a0 20 4e 75 6d 62 65 72 20 4f 66 20 43 6f 6c 75 6d   Number Of Colum
216b0 6e 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53  ns In A Result S
216c0 65 74 20 7b 48 31 33 37 31 30 7d 20 3c 53 31 30  et {H13710} <S10
216d0 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  700>.**.** Retur
216e0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
216f0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
21700 65 73 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e  esult set return
21710 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 5b 70 72  ed by the.** [pr
21720 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
21730 5d 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ]. This routine 
21740 72 65 74 75 72 6e 73 20 30 20 69 66 20 70 53 74  returns 0 if pSt
21750 6d 74 20 69 73 20 61 6e 20 53 51 4c 0a 2a 2a 20  mt is an SQL.** 
21760 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64  statement that d
21770 6f 65 73 20 6e 6f 74 20 72 65 74 75 72 6e 20 64  oes not return d
21780 61 74 61 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  ata (for example
21790 20 61 6e 20 5b 55 50 44 41 54 45 5d 29 2e 0a 2a   an [UPDATE])..*
217a0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
217b0 73 3a 0a 2a 2a 20 5b 48 31 33 37 31 31 5d 0a 2a  s:.** [H13711].*
217c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
217d0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
217e0 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74  count(sqlite3_st
217f0 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a  mt *pStmt);../*.
21800 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c  ** CAPI3REF: Col
21810 75 6d 6e 20 4e 61 6d 65 73 20 49 6e 20 41 20 52  umn Names In A R
21820 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 32  esult Set {H1372
21830 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a  0} <S10700>.**.*
21840 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
21850 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   return the name
21860 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 20 70   assigned to a p
21870 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e  articular column
21880 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c  .** in the resul
21890 74 20 73 65 74 20 6f 66 20 61 20 5b 53 45 4c 45  t set of a [SELE
218a0 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  CT] statement.  
218b0 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  The sqlite3_colu
218c0 6d 6e 5f 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74  mn_name().** int
218d0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
218e0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65   pointer to a ze
218f0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ro-terminated UT
21900 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 61 6e  F-8 string.** an
21910 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  d sqlite3_column
21920 5f 6e 61 6d 65 31 36 28 29 20 72 65 74 75 72 6e  _name16() return
21930 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
21940 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
21950 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e  .** UTF-16 strin
21960 67 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61  g.  The first pa
21970 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b  rameter is the [
21980 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
21990 6e 74 5d 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c  nt].** that impl
219a0 65 6d 65 6e 74 73 20 74 68 65 20 5b 53 45 4c 45  ements the [SELE
219b0 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  CT] statement. T
219c0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
219d0 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f  ter is the.** co
219e0 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68  lumn number.  Th
219f0 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d  e leftmost colum
21a00 6e 20 69 73 20 6e 75 6d 62 65 72 20 30 2e 0a 2a  n is number 0..*
21a10 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  *.** The returne
21a20 64 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 72  d string pointer
21a30 20 69 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20   is valid until 
21a40 65 69 74 68 65 72 20 74 68 65 20 5b 70 72 65 70  either the [prep
21a50 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a  ared statement].
21a60 2a 2a 20 69 73 20 64 65 73 74 72 6f 79 65 64 20  ** is destroyed 
21a70 62 79 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61  by [sqlite3_fina
21a80 6c 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c  lize()] or until
21a90 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74   the next call t
21aa0 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c  o.** sqlite3_col
21ab0 75 6d 6e 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71  umn_name() or sq
21ac0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
21ad0 65 31 36 28 29 20 6f 6e 20 74 68 65 20 73 61 6d  e16() on the sam
21ae0 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  e column..**.** 
21af0 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  If sqlite3_mallo
21b00 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67  c() fails during
21b10 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20   the processing 
21b20 6f 66 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e  of either routin
21b30 65 0a 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c  e.** (for exampl
21b40 65 20 64 75 72 69 6e 67 20 61 20 63 6f 6e 76 65  e during a conve
21b50 72 73 69 6f 6e 20 66 72 6f 6d 20 55 54 46 2d 38  rsion from UTF-8
21b60 20 74 6f 20 55 54 46 2d 31 36 29 20 74 68 65 6e   to UTF-16) then
21b70 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74   a.** NULL point
21b80 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  er is returned..
21b90 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f  **.** The name o
21ba0 66 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  f a result colum
21bb0 6e 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f  n is the value o
21bc0 66 20 74 68 65 20 22 41 53 22 20 63 6c 61 75 73  f the "AS" claus
21bd0 65 20 66 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f  e for.** that co
21be0 6c 75 6d 6e 2c 20 69 66 20 74 68 65 72 65 20 69  lumn, if there i
21bf0 73 20 61 6e 20 41 53 20 63 6c 61 75 73 65 2e 20  s an AS clause. 
21c00 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
21c10 41 53 20 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65  AS clause.** the
21c20 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
21c30 65 20 63 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70  e column is unsp
21c40 65 63 69 66 69 65 64 20 61 6e 64 20 6d 61 79 20  ecified and may 
21c50 63 68 61 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f  change from.** o
21c60 6e 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51  ne release of SQ
21c70 4c 69 74 65 20 74 6f 20 74 68 65 20 6e 65 78 74  Lite to the next
21c80 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
21c90 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 32 31  ents:.** [H13721
21ca0 5d 20 5b 48 31 33 37 32 33 5d 20 5b 48 31 33 37  ] [H13723] [H137
21cb0 32 34 5d 20 5b 48 31 33 37 32 35 5d 20 5b 48 31  24] [H13725] [H1
21cc0 33 37 32 36 5d 20 5b 48 31 33 37 32 37 5d 0a 2a  3726] [H13727].*
21cd0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
21ce0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
21cf0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c  _column_name(sql
21d00 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
21d10 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  N);.SQLITE_API c
21d20 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
21d30 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
21d40 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
21d50 69 6e 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  int N);../*.** C
21d60 41 50 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20  API3REF: Source 
21d70 4f 66 20 44 61 74 61 20 49 6e 20 41 20 51 75 65  Of Data In A Que
21d80 72 79 20 52 65 73 75 6c 74 20 7b 48 31 33 37 34  ry Result {H1374
21d90 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a  0} <S10700>.**.*
21da0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
21db0 20 70 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73   provide a means
21dc0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68   to determine wh
21dd0 61 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61  at column of wha
21de0 74 0a 2a 2a 20 74 61 62 6c 65 20 69 6e 20 77 68  t.** table in wh
21df0 69 63 68 20 64 61 74 61 62 61 73 65 20 61 20 72  ich database a r
21e00 65 73 75 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45  esult of a [SELE
21e10 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  CT] statement co
21e20 6d 65 73 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65  mes from..** The
21e30 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
21e40 61 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6f  abase or table o
21e50 72 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20  r column can be 
21e60 72 65 74 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65  returned as.** e
21e70 69 74 68 65 72 20 61 20 55 54 46 2d 38 20 6f 72  ither a UTF-8 or
21e80 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20   UTF-16 string. 
21e90 20 54 68 65 20 5f 64 61 74 61 62 61 73 65 5f 20   The _database_ 
21ea0 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 0a  routines return.
21eb0 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
21ec0 6e 61 6d 65 2c 20 74 68 65 20 5f 74 61 62 6c 65  name, the _table
21ed0 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  _ routines retur
21ee0 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  n the table name
21ef0 2c 20 61 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69  , and.** the ori
21f00 67 69 6e 5f 20 72 6f 75 74 69 6e 65 73 20 72 65  gin_ routines re
21f10 74 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20  turn the column 
21f20 6e 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74  name..** The ret
21f30 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20  urned string is 
21f40 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20  valid until the 
21f50 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
21f60 65 6e 74 5d 20 69 73 20 64 65 73 74 72 6f 79 65  ent] is destroye
21f70 64 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69  d.** using [sqli
21f80 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20  te3_finalize()] 
21f90 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d  or until the sam
21fa0 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  e information is
21fb0 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67   requested.** ag
21fc0 61 69 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65  ain in a differe
21fd0 6e 74 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a  nt encoding..**.
21fe0 2a 2a 20 54 68 65 20 6e 61 6d 65 73 20 72 65 74  ** The names ret
21ff0 75 72 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72  urned are the or
22000 69 67 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65  iginal un-aliase
22010 64 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a  d names of the.*
22020 2a 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c  * database, tabl
22030 65 2c 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a  e, and column..*
22040 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
22050 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66  rgument to the f
22060 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69  ollowing calls i
22070 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  s a [prepared st
22080 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65  atement]..** The
22090 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74  se functions ret
220a0 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  urn information 
220b0 61 62 6f 75 74 20 74 68 65 20 4e 74 68 20 63 6f  about the Nth co
220c0 6c 75 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79  lumn returned by
220d0 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e  .** the statemen
220e0 74 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  t, where N is th
220f0 65 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f  e second functio
22100 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  n argument..**.*
22110 2a 20 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c  * If the Nth col
22120 75 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20  umn returned by 
22130 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
22140 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f   an expression o
22150 72 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e  r.** subquery an
22160 64 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d  d is not a colum
22170 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c  n value, then al
22180 6c 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74  l of these funct
22190 69 6f 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e  ions return.** N
221a0 55 4c 4c 2e 20 20 54 68 65 73 65 20 72 6f 75 74  ULL.  These rout
221b0 69 6e 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72  ine might also r
221c0 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20  eturn NULL if a 
221d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
221e0 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72  n error.** occur
221f0 73 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74  s.  Otherwise, t
22200 68 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 6e  hey return the n
22210 61 6d 65 20 6f 66 20 74 68 65 20 61 74 74 61 63  ame of the attac
22220 68 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 61  hed database, ta
22230 62 6c 65 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d  ble.** and colum
22240 6e 20 74 68 61 74 20 71 75 65 72 79 20 72 65 73  n that query res
22250 75 6c 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65  ult column was e
22260 78 74 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a  xtracted from..*
22270 2a 0a 2a 2a 20 41 73 20 77 69 74 68 20 61 6c 6c  *.** As with all
22280 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 41 50   other SQLite AP
22290 49 73 2c 20 74 68 6f 73 65 20 70 6f 73 74 66 69  Is, those postfi
222a0 78 65 64 20 77 69 74 68 20 22 31 36 22 20 72 65  xed with "16" re
222b0 74 75 72 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65  turn.** UTF-16 e
222c0 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 73 2c 20  ncoded strings, 
222d0 74 68 65 20 6f 74 68 65 72 20 66 75 6e 63 74 69  the other functi
222e0 6f 6e 73 20 72 65 74 75 72 6e 20 55 54 46 2d 38  ons return UTF-8
222f0 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68  . {END}.**.** Th
22300 65 73 65 20 41 50 49 73 20 61 72 65 20 6f 6e 6c  ese APIs are onl
22310 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74  y available if t
22320 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63  he library was c
22330 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
22340 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42  .** [SQLITE_ENAB
22350 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
22360 54 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73  TA] C-preprocess
22370 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65  or symbol define
22380 64 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31  d..**.** {A13751
22390 7d 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d  }.** If two or m
223a0 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c 6c  ore threads call
223b0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20   one or more of 
223c0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  these routines a
223d0 67 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65 0a  gainst the same.
223e0 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74  ** prepared stat
223f0 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e  ement and column
22400 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
22410 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  e then the resul
22420 74 73 20 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69  ts are.** undefi
22430 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ned..**.** Requi
22440 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33  rements:.** [H13
22450 37 34 31 5d 20 5b 48 31 33 37 34 32 5d 20 5b 48  741] [H13742] [H
22460 31 33 37 34 33 5d 20 5b 48 31 33 37 34 34 5d 20  13743] [H13744] 
22470 5b 48 31 33 37 34 35 5d 20 5b 48 31 33 37 34 36  [H13745] [H13746
22480 5d 20 5b 48 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a  ] [H13748].**.**
22490 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   If two or more 
224a0 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65  threads call one
224b0 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c   or more.** [sql
224c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
224d0 62 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75  base_name | colu
224e0 6d 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65  mn metadata inte
224f0 72 66 61 63 65 73 5d 0a 2a 2a 20 66 6f 72 20 74  rfaces].** for t
22500 68 65 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65  he same [prepare
22510 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 6e 64  d statement] and
22520 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a   result column.*
22530 2a 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  * at the same ti
22540 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  me then the resu
22550 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65  lts are undefine
22560 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
22570 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
22580 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
22590 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65  base_name(sqlite
225a0 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51  3_stmt*,int);.SQ
225b0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
225c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
225d0 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
225e0 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  e16(sqlite3_stmt
225f0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  *,int);.SQLITE_A
22600 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
22610 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
22620 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  ble_name(sqlite3
22630 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c  _stmt*,int);.SQL
22640 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
22650 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
22660 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28  mn_table_name16(
22670 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
22680 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  t);.SQLITE_API c
22690 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
226a0 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
226b0 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
226c0 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  mt*,int);.SQLITE
226d0 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
226e0 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
226f0 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71  origin_name16(sq
22700 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
22710 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
22720 46 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74 61  F: Declared Data
22730 74 79 70 65 20 4f 66 20 41 20 51 75 65 72 79 20  type Of A Query 
22740 52 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20  Result {H13760} 
22750 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10700>.**.** T
22760 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
22770 65 72 20 69 73 20 61 20 5b 70 72 65 70 61 72 65  er is a [prepare
22780 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a  d statement]..**
22790 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65   If this stateme
227a0 6e 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d  nt is a [SELECT]
227b0 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74   statement and t
227c0 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
227d0 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64   the.** returned
227e0 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
227f0 68 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20  hat [SELECT] is 
22800 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28  a table column (
22810 6e 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73  not an.** expres
22820 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79  sion or subquery
22830 29 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61  ) then the decla
22840 72 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20  red type of the 
22850 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  table.** column 
22860 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  is returned.  If
22870 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
22880 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
22890 74 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65  t is an.** expre
228a0 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72  ssion or subquer
228b0 79 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70  y, then a NULL p
228c0 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e  ointer is return
228d0 65 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  ed..** The retur
228e0 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c  ned string is al
228f0 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64  ways UTF-8 encod
22900 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20  ed. {END}.**.** 
22910 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76  For example, giv
22920 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
22930 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52  schema:.**.** CR
22940 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 31  EATE TABLE t1(c1
22950 20 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a   VARIANT);.**.**
22960 20 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69   and the followi
22970 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  ng statement to 
22980 62 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a  be compiled:.**.
22990 2a 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31  ** SELECT c1 + 1
229a0 2c 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a  , c1 FROM t1;.**
229b0 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
229c0 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68   would return th
229d0 65 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e  e string "VARIAN
229e0 54 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  T" for the secon
229f0 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75  d result.** colu
22a00 6d 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61  mn (i==1), and a
22a10 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f   NULL pointer fo
22a20 72 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75  r the first resu
22a30 6c 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29  lt column (i==0)
22a40 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75  ..**.** SQLite u
22a50 73 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d  ses dynamic run-
22a60 74 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f  time typing.  So
22a70 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20   just because a 
22a80 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63  column.** is dec
22a90 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e  lared to contain
22aa0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79   a particular ty
22ab0 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e  pe does not mean
22ac0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74   that the.** dat
22ad0 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74  a stored in that
22ae0 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68   column is of th
22af0 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e  e declared type.
22b00 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73    SQLite is.** s
22b10 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62  trongly typed, b
22b20 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73  ut the typing is
22b30 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61   dynamic not sta
22b40 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73  tic.  Type.** is
22b50 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
22b60 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75   individual valu
22b70 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65  es, not with the
22b80 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75   containers.** u
22b90 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73  sed to hold thos
22ba0 65 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  e values..**.** 
22bb0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
22bc0 20 5b 48 31 33 37 36 31 5d 20 5b 48 31 33 37 36   [H13761] [H1376
22bd0 32 5d 20 5b 48 31 33 37 36 33 5d 0a 2a 2f 0a 53  2] [H13763].*/.S
22be0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
22bf0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
22c00 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71  lumn_decltype(sq
22c10 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
22c20 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
22c30 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
22c40 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
22c50 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  16(sqlite3_stmt*
22c60 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ,int);../*.** CA
22c70 50 49 33 52 45 46 3a 20 45 76 61 6c 75 61 74 65  PI3REF: Evaluate
22c80 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e   An SQL Statemen
22c90 74 20 7b 48 31 33 32 30 30 7d 20 3c 53 31 30 30  t {H13200} <S100
22ca0 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  00>.**.** After 
22cb0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
22cc0 65 6d 65 6e 74 5d 20 68 61 73 20 62 65 65 6e 20  ement] has been 
22cd0 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 65  prepared using e
22ce0 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  ither.** [sqlite
22cf0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
22d00 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  or [sqlite3_prep
22d10 61 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f  are16_v2()] or o
22d20 6e 65 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79  ne of the legacy
22d30 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 5b  .** interfaces [
22d40 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
22d50 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70  )] or [sqlite3_p
22d60 72 65 70 61 72 65 31 36 28 29 5d 2c 20 74 68 69  repare16()], thi
22d70 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75  s function.** mu
22d80 73 74 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65  st be called one
22d90 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74   or more times t
22da0 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 73  o evaluate the s
22db0 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
22dc0 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74  The details of t
22dd0 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74  he behavior of t
22de0 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  he sqlite3_step(
22df0 29 20 69 6e 74 65 72 66 61 63 65 20 64 65 70 65  ) interface depe
22e00 6e 64 0a 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72  nd.** on whether
22e10 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77   the statement w
22e20 61 73 20 70 72 65 70 61 72 65 64 20 75 73 69 6e  as prepared usin
22e30 67 20 74 68 65 20 6e 65 77 65 72 20 22 76 32 22  g the newer "v2"
22e40 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73   interface.** [s
22e50 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
22e60 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  2()] and [sqlite
22e70 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29  3_prepare16_v2()
22e80 5d 20 6f 72 20 74 68 65 20 6f 6c 64 65 72 20 6c  ] or the older l
22e90 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61  egacy.** interfa
22ea0 63 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ce [sqlite3_prep
22eb0 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  are()] and [sqli
22ec0 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d  te3_prepare16()]
22ed0 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 74 68  .  The use of th
22ee0 65 0a 2a 2a 20 6e 65 77 20 22 76 32 22 20 69 6e  e.** new "v2" in
22ef0 74 65 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d  terface is recom
22f00 6d 65 6e 64 65 64 20 66 6f 72 20 6e 65 77 20 61  mended for new a
22f10 70 70 6c 69 63 61 74 69 6f 6e 73 20 62 75 74 20  pplications but 
22f20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e  the legacy.** in
22f30 74 65 72 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e  terface will con
22f40 74 69 6e 75 65 20 74 6f 20 62 65 20 73 75 70 70  tinue to be supp
22f50 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  orted..**.** In 
22f60 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72  the legacy inter
22f70 66 61 63 65 2c 20 74 68 65 20 72 65 74 75 72 6e  face, the return
22f80 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 65   value will be e
22f90 69 74 68 65 72 20 5b 53 51 4c 49 54 45 5f 42 55  ither [SQLITE_BU
22fa0 53 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  SY],.** [SQLITE_
22fb0 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52  DONE], [SQLITE_R
22fc0 4f 57 5d 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52  OW], [SQLITE_ERR
22fd0 4f 52 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  OR], or [SQLITE_
22fe0 4d 49 53 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68  MISUSE]..** With
22ff0 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66   the "v2" interf
23000 61 63 65 2c 20 61 6e 79 20 6f 66 20 74 68 65 20  ace, any of the 
23010 6f 74 68 65 72 20 5b 72 65 73 75 6c 74 20 63 6f  other [result co
23020 64 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65  des] or.** [exte
23030 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65  nded result code
23040 73 5d 20 6d 69 67 68 74 20 62 65 20 72 65 74 75  s] might be retu
23050 72 6e 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a  rned as well..**
23060 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  .** [SQLITE_BUSY
23070 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  ] means that the
23080 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
23090 20 77 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61   was unable to a
230a0 63 71 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61  cquire the.** da
230b0 74 61 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20  tabase locks it 
230c0 6e 65 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20  needs to do its 
230d0 6a 6f 62 2e 20 20 49 66 20 74 68 65 20 73 74 61  job.  If the sta
230e0 74 65 6d 65 6e 74 20 69 73 20 61 20 5b 43 4f 4d  tement is a [COM
230f0 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75 72  MIT].** or occur
23100 73 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 20  s outside of an 
23110 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63  explicit transac
23120 74 69 6f 6e 2c 20 74 68 65 6e 20 79 6f 75 20 63  tion, then you c
23130 61 6e 20 72 65 74 72 79 20 74 68 65 0a 2a 2a 20  an retry the.** 
23140 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 74  statement.  If t
23150 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
23160 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 61  not a [COMMIT] a
23170 6e 64 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  nd occurs within
23180 20 61 0a 2a 2a 20 65 78 70 6c 69 63 69 74 20 74   a.** explicit t
23190 72 61 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20  ransaction then 
231a0 79 6f 75 20 73 68 6f 75 6c 64 20 72 6f 6c 6c 62  you should rollb
231b0 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74  ack the transact
231c0 69 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f  ion before.** co
231d0 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  ntinuing..**.** 
231e0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 6d 65  [SQLITE_DONE] me
231f0 61 6e 73 20 74 68 61 74 20 74 68 65 20 73 74 61  ans that the sta
23200 74 65 6d 65 6e 74 20 68 61 73 20 66 69 6e 69 73  tement has finis
23210 68 65 64 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a  hed executing.**
23220 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20   successfully.  
23230 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73  sqlite3_step() s
23240 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c  hould not be cal
23250 6c 65 64 20 61 67 61 69 6e 20 6f 6e 20 74 68 69  led again on thi
23260 73 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63  s virtual.** mac
23270 68 69 6e 65 20 77 69 74 68 6f 75 74 20 66 69 72  hine without fir
23280 73 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  st calling [sqli
23290 74 65 33 5f 72 65 73 65 74 28 29 5d 20 74 6f 20  te3_reset()] to 
232a0 72 65 73 65 74 20 74 68 65 20 76 69 72 74 75 61  reset the virtua
232b0 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 62 61 63  l.** machine bac
232c0 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c  k to its initial
232d0 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   state..**.** If
232e0 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
232f0 6e 74 20 62 65 69 6e 67 20 65 78 65 63 75 74 65  nt being execute
23300 64 20 72 65 74 75 72 6e 73 20 61 6e 79 20 64 61  d returns any da
23310 74 61 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45  ta, then [SQLITE
23320 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20 72 65 74 75  _ROW].** is retu
23330 72 6e 65 64 20 65 61 63 68 20 74 69 6d 65 20 61  rned each time a
23340 20 6e 65 77 20 72 6f 77 20 6f 66 20 64 61 74 61   new row of data
23350 20 69 73 20 72 65 61 64 79 20 66 6f 72 20 70 72   is ready for pr
23360 6f 63 65 73 73 69 6e 67 20 62 79 20 74 68 65 0a  ocessing by the.
23370 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 68 65 20 76  ** caller. The v
23380 61 6c 75 65 73 20 6d 61 79 20 62 65 20 61 63 63  alues may be acc
23390 65 73 73 65 64 20 75 73 69 6e 67 20 74 68 65 20  essed using the 
233a0 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66  [column access f
233b0 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71  unctions]..** sq
233c0 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 73 20  lite3_step() is 
233d0 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 74 6f 20  called again to 
233e0 72 65 74 72 69 65 76 65 20 74 68 65 20 6e 65 78  retrieve the nex
233f0 74 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a  t row of data..*
23400 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52  *.** [SQLITE_ERR
23410 4f 52 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 61  OR] means that a
23420 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20   run-time error 
23430 28 73 75 63 68 20 61 73 20 61 20 63 6f 6e 73 74  (such as a const
23440 72 61 69 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69  raint.** violati
23450 6f 6e 29 20 68 61 73 20 6f 63 63 75 72 72 65 64  on) has occurred
23460 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
23470 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ) should not be 
23480 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0a  called again on.
23490 2a 2a 20 74 68 65 20 56 4d 2e 20 4d 6f 72 65 20  ** the VM. More 
234a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20  information may 
234b0 62 65 20 66 6f 75 6e 64 20 62 79 20 63 61 6c 6c  be found by call
234c0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  ing [sqlite3_err
234d0 6d 73 67 28 29 5d 2e 0a 2a 2a 20 57 69 74 68 20  msg()]..** With 
234e0 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72  the legacy inter
234f0 66 61 63 65 2c 20 61 20 6d 6f 72 65 20 73 70 65  face, a more spe
23500 63 69 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65  cific error code
23510 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a   (for example,.*
23520 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  * [SQLITE_INTERR
23530 55 50 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 53 43  UPT], [SQLITE_SC
23540 48 45 4d 41 5d 2c 20 5b 53 51 4c 49 54 45 5f 43  HEMA], [SQLITE_C
23550 4f 52 52 55 50 54 5d 2c 20 61 6e 64 20 73 6f 20  ORRUPT], and so 
23560 66 6f 72 74 68 29 0a 2a 2a 20 63 61 6e 20 62 65  forth).** can be
23570 20 6f 62 74 61 69 6e 65 64 20 62 79 20 63 61 6c   obtained by cal
23580 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65  ling [sqlite3_re
23590 73 65 74 28 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a  set()] on the.**
235a0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
235b0 6d 65 6e 74 5d 2e 20 20 49 6e 20 74 68 65 20 22  ment].  In the "
235c0 76 32 22 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a  v2" interface,.*
235d0 2a 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69  * the more speci
235e0 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 69  fic error code i
235f0 73 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63  s returned direc
23600 74 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 73  tly by sqlite3_s
23610 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51  tep()..**.** [SQ
23620 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61  LITE_MISUSE] mea
23630 6e 73 20 74 68 61 74 20 74 68 65 20 74 68 69 73  ns that the this
23640 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
23650 6c 65 64 20 69 6e 61 70 70 72 6f 70 72 69 61 74  led inappropriat
23660 65 6c 79 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20  ely..** Perhaps 
23670 69 74 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e  it was called on
23680 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
23690 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 68 61 73  tement] that has
236a0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e  .** already been
236b0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
236c0 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20  ze | finalized] 
236d0 6f 72 20 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68  or on one that h
236e0 61 64 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  ad.** previously
236f0 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54   returned [SQLIT
23700 45 5f 45 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c  E_ERROR] or [SQL
23710 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69  ITE_DONE].  Or i
23720 74 20 63 6f 75 6c 64 0a 2a 2a 20 62 65 20 74 68  t could.** be th
23730 65 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20  e case that the 
23740 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
23750 6e 6e 65 63 74 69 6f 6e 20 69 73 20 62 65 69 6e  nnection is bein
23760 67 20 75 73 65 64 20 62 79 20 74 77 6f 20 6f 72  g used by two or
23770 0a 2a 2a 20 6d 6f 72 65 20 74 68 72 65 61 64 73  .** more threads
23780 20 61 74 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d   at the same mom
23790 65 6e 74 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a  ent in time..**.
237a0 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65  ** <b>Goofy Inte
237b0 72 66 61 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e  rface Alert:</b>
237c0 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69   In the legacy i
237d0 6e 74 65 72 66 61 63 65 2c 20 74 68 65 20 73 71  nterface, the sq
237e0 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20  lite3_step().** 
237f0 41 50 49 20 61 6c 77 61 79 73 20 72 65 74 75 72  API always retur
23800 6e 73 20 61 20 67 65 6e 65 72 69 63 20 65 72 72  ns a generic err
23810 6f 72 20 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45  or code, [SQLITE
23820 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69  _ERROR], followi
23830 6e 67 20 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20  ng any.** error 
23840 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49  other than [SQLI
23850 54 45 5f 42 55 53 59 5d 20 61 6e 64 20 5b 53 51  TE_BUSY] and [SQ
23860 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59  LITE_MISUSE].  Y
23870 6f 75 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20  ou must call.** 
23880 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
23890 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69  ] or [sqlite3_fi
238a0 6e 61 6c 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64  nalize()] in ord
238b0 65 72 20 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f  er to find one o
238c0 66 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69  f the.** specifi
238d0 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20  c [error codes] 
238e0 74 68 61 74 20 62 65 74 74 65 72 20 64 65 73 63  that better desc
238f0 72 69 62 65 73 20 74 68 65 20 65 72 72 6f 72 2e  ribes the error.
23900 0a 2a 2a 20 57 65 20 61 64 6d 69 74 20 74 68 61  .** We admit tha
23910 74 20 74 68 69 73 20 69 73 20 61 20 67 6f 6f 66  t this is a goof
23920 79 20 64 65 73 69 67 6e 2e 20 20 54 68 65 20 70  y design.  The p
23930 72 6f 62 6c 65 6d 20 68 61 73 20 62 65 65 6e 20  roblem has been 
23940 66 69 78 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  fixed.** with th
23950 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  e "v2" interface
23960 2e 20 20 49 66 20 79 6f 75 20 70 72 65 70 61 72  .  If you prepar
23970 65 20 61 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51  e all of your SQ
23980 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20  L statements.** 
23990 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71  using either [sq
239a0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
239b0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
239c0 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20  prepare16_v2()] 
239d0 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68  instead.** of th
239e0 65 20 6c 65 67 61 63 79 20 5b 73 71 6c 69 74 65  e legacy [sqlite
239f0 33 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64  3_prepare()] and
23a00 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
23a10 65 31 36 28 29 5d 20 69 6e 74 65 72 66 61 63 65  e16()] interface
23a20 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d  s,.** then the m
23a30 6f 72 65 20 73 70 65 63 69 66 69 63 20 5b 65 72  ore specific [er
23a40 72 6f 72 20 63 6f 64 65 73 5d 20 61 72 65 20 72  ror codes] are r
23a50 65 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79  eturned directly
23a60 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 73  .** by sqlite3_s
23a70 74 65 70 28 29 2e 20 20 54 68 65 20 75 73 65 20  tep().  The use 
23a80 6f 66 20 74 68 65 20 22 76 32 22 20 69 6e 74 65  of the "v2" inte
23a90 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65  rface is recomme
23aa0 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  nded..**.** Requ
23ab0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
23ac0 33 32 30 32 5d 20 5b 48 31 35 33 30 34 5d 20 5b  3202] [H15304] [
23ad0 48 31 35 33 30 36 5d 20 5b 48 31 35 33 30 38 5d  H15306] [H15308]
23ae0 20 5b 48 31 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c   [H15310].*/.SQL
23af0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
23b00 74 65 33 5f 73 74 65 70 28 73 71 6c 69 74 65 33  te3_step(sqlite3
23b10 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  _stmt*);../*.** 
23b20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72  CAPI3REF: Number
23b30 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61   of columns in a
23b40 20 72 65 73 75 6c 74 20 73 65 74 20 7b 48 31 33   result set {H13
23b50 37 37 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a  770} <S10700>.**
23b60 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20  .** Returns the 
23b70 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73  number of values
23b80 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
23b90 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
23ba0 74 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  t set..**.** Req
23bb0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
23bc0 31 33 37 37 31 5d 20 5b 48 31 33 37 37 32 5d 0a  13771] [H13772].
23bd0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
23be0 74 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63  t sqlite3_data_c
23bf0 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  ount(sqlite3_stm
23c00 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a  t *pStmt);../*.*
23c10 2a 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 64  * CAPI3REF: Fund
23c20 61 6d 65 6e 74 61 6c 20 44 61 74 61 74 79 70 65  amental Datatype
23c30 73 20 7b 48 31 30 32 36 35 7d 20 3c 53 31 30 31  s {H10265} <S101
23c40 31 30 3e 3c 53 31 30 31 32 30 3e 0a 2a 2a 20 4b  10><S10120>.** K
23c50 45 59 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f  EYWORDS: SQLITE_
23c60 54 45 58 54 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32  TEXT.**.** {H102
23c70 36 36 7d 20 45 76 65 72 79 20 76 61 6c 75 65 20  66} Every value 
23c80 69 6e 20 53 51 4c 69 74 65 20 68 61 73 20 6f 6e  in SQLite has on
23c90 65 20 6f 66 20 66 69 76 65 20 66 75 6e 64 61 6d  e of five fundam
23ca0 65 6e 74 61 6c 20 64 61 74 61 74 79 70 65 73 3a  ental datatypes:
23cb0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
23cc0 6c 69 3e 20 36 34 2d 62 69 74 20 73 69 67 6e 65  li> 64-bit signe
23cd0 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 6c 69  d integer.** <li
23ce0 3e 20 36 34 2d 62 69 74 20 49 45 45 45 20 66 6c  > 64-bit IEEE fl
23cf0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d  oating point num
23d00 62 65 72 0a 2a 2a 20 3c 6c 69 3e 20 73 74 72 69  ber.** <li> stri
23d10 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a  ng.** <li> BLOB.
23d20 2a 2a 20 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20  ** <li> NULL.** 
23d30 3c 2f 75 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  </ul> {END}.**.*
23d40 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
23d50 73 20 61 72 65 20 63 6f 64 65 73 20 66 6f 72 20  s are codes for 
23d60 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 74 79  each of those ty
23d70 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  pes..**.** Note 
23d80 74 68 61 74 20 74 68 65 20 53 51 4c 49 54 45 5f  that the SQLITE_
23d90 54 45 58 54 20 63 6f 6e 73 74 61 6e 74 20 77 61  TEXT constant wa
23da0 73 20 61 6c 73 6f 20 75 73 65 64 20 69 6e 20 53  s also used in S
23db0 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 0a  QLite version 2.
23dc0 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74  ** for a complet
23dd0 65 6c 79 20 64 69 66 66 65 72 65 6e 74 20 6d 65  ely different me
23de0 61 6e 69 6e 67 2e 20 20 53 6f 66 74 77 61 72 65  aning.  Software
23df0 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69   that links agai
23e00 6e 73 74 20 62 6f 74 68 0a 2a 2a 20 53 51 4c 69  nst both.** SQLi
23e10 74 65 20 76 65 72 73 69 6f 6e 20 32 20 61 6e 64  te version 2 and
23e20 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
23e30 33 20 73 68 6f 75 6c 64 20 75 73 65 20 53 51 4c  3 should use SQL
23e40 49 54 45 33 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a  ITE3_TEXT, not.*
23e50 2a 20 53 51 4c 49 54 45 5f 54 45 58 54 2e 0a 2a  * SQLITE_TEXT..*
23e60 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
23e70 5f 49 4e 54 45 47 45 52 20 20 31 0a 23 64 65 66  _INTEGER  1.#def
23e80 69 6e 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ine SQLITE_FLOAT
23e90 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51      2.#define SQ
23ea0 4c 49 54 45 5f 42 4c 4f 42 20 20 20 20 20 34 0a  LITE_BLOB     4.
23eb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
23ec0 55 4c 4c 20 20 20 20 20 35 0a 23 69 66 64 65 66  ULL     5.#ifdef
23ed0 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 20 75   SQLITE_TEXT.# u
23ee0 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54  ndef SQLITE_TEXT
23ef0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
23f00 53 51 4c 49 54 45 5f 54 45 58 54 20 20 20 20 20  SQLITE_TEXT     
23f10 33 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  3.#endif.#define
23f20 20 53 51 4c 49 54 45 33 5f 54 45 58 54 20 20 20   SQLITE3_TEXT   
23f30 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33    3../*.** CAPI3
23f40 52 45 46 3a 20 52 65 73 75 6c 74 20 56 61 6c 75  REF: Result Valu
23f50 65 73 20 46 72 6f 6d 20 41 20 51 75 65 72 79 20  es From A Query 
23f60 7b 48 31 33 38 30 30 7d 20 3c 53 31 30 37 30 30  {H13800} <S10700
23f70 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
23f80 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75  column access fu
23f90 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54  nctions}.**.** T
23fa0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f  hese routines fo
23fb0 72 6d 20 74 68 65 20 22 72 65 73 75 6c 74 20 73  rm the "result s
23fc0 65 74 20 71 75 65 72 79 22 20 69 6e 74 65 72 66  et query" interf
23fd0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ace..**.** These
23fe0 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
23ff0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
24000 75 74 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75  ut a single colu
24010 6d 6e 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  mn of the curren
24020 74 0a 2a 2a 20 72 65 73 75 6c 74 20 72 6f 77 20  t.** result row 
24030 6f 66 20 61 20 71 75 65 72 79 2e 20 20 49 6e 20  of a query.  In 
24040 65 76 65 72 79 20 63 61 73 65 20 74 68 65 20 66  every case the f
24050 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
24060 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f   a pointer.** to
24070 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73   the [prepared s
24080 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69  tatement] that i
24090 73 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65  s being evaluate
240a0 64 20 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  d (the [sqlite3_
240b0 73 74 6d 74 2a 5d 0a 2a 2a 20 74 68 61 74 20 77  stmt*].** that w
240c0 61 73 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  as returned from
240d0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
240e0 65 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f  e_v2()] or one o
240f0 66 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 0a  f its variants).
24100 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  ** and the secon
24110 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
24120 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63  e index of the c
24130 6f 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20  olumn for which 
24140 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73  information.** s
24150 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65  hould be returne
24160 64 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 74  d.  The leftmost
24170 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72   column of the r
24180 65 73 75 6c 74 20 73 65 74 20 68 61 73 20 74 68  esult set has th
24190 65 20 69 6e 64 65 78 20 30 2e 0a 2a 2a 0a 2a 2a  e index 0..**.**
241a0 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   If the SQL stat
241b0 65 6d 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20 63  ement does not c
241c0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74  urrently point t
241d0 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 2c 20 6f  o a valid row, o
241e0 72 20 69 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75  r if the.** colu
241f0 6d 6e 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20  mn index is out 
24200 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 20 72 65  of range, the re
24210 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65  sult is undefine
24220 64 2e 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  d..** These rout
24230 69 6e 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65  ines may only be
24240 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   called when the
24250 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c   most recent cal
24260 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  l to.** [sqlite3
24270 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74  _step()] has ret
24280 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 4f  urned [SQLITE_RO
24290 57 5d 20 61 6e 64 20 6e 65 69 74 68 65 72 0a 2a  W] and neither.*
242a0 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  * [sqlite3_reset
242b0 28 29 5d 20 6e 6f 72 20 5b 73 71 6c 69 74 65 33  ()] nor [sqlite3
242c0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 68 61 76  _finalize()] hav
242d0 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 73 75  e been called su
242e0 62 73 65 71 75 65 6e 74 6c 79 2e 0a 2a 2a 20 49  bsequently..** I
242f0 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72  f any of these r
24300 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c  outines are call
24310 65 64 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65  ed after [sqlite
24320 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a  3_reset()] or.**
24330 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
24340 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 5b  ze()] or after [
24350 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
24360 68 61 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  has returned.** 
24370 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20  something other 
24380 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57  than [SQLITE_ROW
24390 5d 2c 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  ], the results a
243a0 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a  re undefined..**
243b0 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   If [sqlite3_ste
243c0 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  p()] or [sqlite3
243d0 5f 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71  _reset()] or [sq
243e0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
243f0 5d 0a 2a 2a 20 61 72 65 20 63 61 6c 6c 65 64 20  ].** are called 
24400 66 72 6f 6d 20 61 20 64 69 66 66 65 72 65 6e 74  from a different
24410 20 74 68 72 65 61 64 20 77 68 69 6c 65 20 61 6e   thread while an
24420 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  y of these routi
24430 6e 65 73 0a 2a 2a 20 61 72 65 20 70 65 6e 64 69  nes.** are pendi
24440 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ng, then the res
24450 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e  ults are undefin
24460 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ed..**.** The sq
24470 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
24480 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75  e() routine retu
24490 72 6e 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49  rns the.** [SQLI
244a0 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74  TE_INTEGER | dat
244b0 61 74 79 70 65 20 63 6f 64 65 5d 20 66 6f 72 20  atype code] for 
244c0 74 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61  the initial data
244d0 20 74 79 70 65 0a 2a 2a 20 6f 66 20 74 68 65 20   type.** of the 
244e0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 2e 20 20  result column.  
244f0 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c  The returned val
24500 75 65 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51  ue is one of [SQ
24510 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 0a 2a  LITE_INTEGER],.*
24520 2a 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d  * [SQLITE_FLOAT]
24530 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d 2c  , [SQLITE_TEXT],
24540 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 2c 20   [SQLITE_BLOB], 
24550 6f 72 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d  or [SQLITE_NULL]
24560 2e 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20  .  The value.** 
24570 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
24580 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
24590 29 20 69 73 20 6f 6e 6c 79 20 6d 65 61 6e 69 6e  ) is only meanin
245a0 67 66 75 6c 20 69 66 20 6e 6f 20 74 79 70 65 0a  gful if no type.
245b0 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 68  ** conversions h
245c0 61 76 65 20 6f 63 63 75 72 72 65 64 20 61 73 20  ave occurred as 
245d0 64 65 73 63 72 69 62 65 64 20 62 65 6c 6f 77 2e  described below.
245e0 20 20 41 66 74 65 72 20 61 20 74 79 70 65 20 63    After a type c
245f0 6f 6e 76 65 72 73 69 6f 6e 2c 0a 2a 2a 20 74 68  onversion,.** th
24600 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
24610 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   by sqlite3_colu
24620 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 75 6e 64  mn_type() is und
24630 65 66 69 6e 65 64 2e 20 20 46 75 74 75 72 65 0a  efined.  Future.
24640 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  ** versions of S
24650 51 4c 69 74 65 20 6d 61 79 20 63 68 61 6e 67 65  QLite may change
24660 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66   the behavior of
24670 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
24680 74 79 70 65 28 29 0a 2a 2a 20 66 6f 6c 6c 6f 77  type().** follow
24690 69 6e 67 20 61 20 74 79 70 65 20 63 6f 6e 76 65  ing a type conve
246a0 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rsion..**.** If 
246b0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20  the result is a 
246c0 42 4c 4f 42 20 6f 72 20 55 54 46 2d 38 20 73 74  BLOB or UTF-8 st
246d0 72 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73 71  ring then the sq
246e0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
246f0 65 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  es().** routine 
24700 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
24710 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
24720 68 61 74 20 42 4c 4f 42 20 6f 72 20 73 74 72 69  hat BLOB or stri
24730 6e 67 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  ng..** If the re
24740 73 75 6c 74 20 69 73 20 61 20 55 54 46 2d 31 36  sult is a UTF-16
24750 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 73 71   string, then sq
24760 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
24770 65 73 28 29 20 63 6f 6e 76 65 72 74 73 0a 2a 2a  es() converts.**
24780 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 55   the string to U
24790 54 46 2d 38 20 61 6e 64 20 74 68 65 6e 20 72 65  TF-8 and then re
247a0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
247b0 20 6f 66 20 62 79 74 65 73 2e 0a 2a 2a 20 49 66   of bytes..** If
247c0 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
247d0 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 74   numeric value t
247e0 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  hen sqlite3_colu
247f0 6d 6e 5f 62 79 74 65 73 28 29 20 75 73 65 73 0a  mn_bytes() uses.
24800 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72  ** [sqlite3_snpr
24810 69 6e 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76 65  intf()] to conve
24820 72 74 20 74 68 61 74 20 76 61 6c 75 65 20 74 6f  rt that value to
24830 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20   a UTF-8 string 
24840 61 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74  and returns.** t
24850 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
24860 65 73 20 69 6e 20 74 68 61 74 20 73 74 72 69 6e  es in that strin
24870 67 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  g..** The value 
24880 72 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f  returned does no
24890 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 7a 65  t include the ze
248a0 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 61 74  ro terminator at
248b0 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 74   the end.** of t
248c0 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20  he string.  For 
248d0 63 6c 61 72 69 74 79 3a 20 74 68 65 20 76 61 6c  clarity: the val
248e0 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  ue returned is t
248f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
24900 62 79 74 65 73 20 69 6e 20 74 68 65 20 73 74 72  bytes in the str
24910 69 6e 67 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d  ing, not the num
24920 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
24930 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 69 6e 67 73  s..**.** Strings
24940 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
24950 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
24960 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63  () and sqlite3_c
24970 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 0a  olumn_text16(),.
24980 2a 2a 20 65 76 65 6e 20 65 6d 70 74 79 20 73 74  ** even empty st
24990 72 69 6e 67 73 2c 20 61 72 65 20 61 6c 77 61 79  rings, are alway
249a0 73 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65  s zero terminate
249b0 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a 2a  d.  The return.*
249c0 2a 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c  * value from sql
249d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
249e0 28 29 20 66 6f 72 20 61 20 7a 65 72 6f 2d 6c 65  () for a zero-le
249f0 6e 67 74 68 20 42 4c 4f 42 20 69 73 20 61 6e 20  ngth BLOB is an 
24a00 61 72 62 69 74 72 61 72 79 0a 2a 2a 20 70 6f 69  arbitrary.** poi
24a10 6e 74 65 72 2c 20 70 6f 73 73 69 62 6c 79 20 65  nter, possibly e
24a20 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ven a NULL point
24a30 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  er..**.** The sq
24a40 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
24a50 65 73 31 36 28 29 20 72 6f 75 74 69 6e 65 20 69  es16() routine i
24a60 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c  s similar to sql
24a70 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
24a80 73 28 29 0a 2a 2a 20 62 75 74 20 6c 65 61 76 65  s().** but leave
24a90 73 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  s the result in 
24aa0 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65  UTF-16 in native
24ab0 20 62 79 74 65 20 6f 72 64 65 72 20 69 6e 73 74   byte order inst
24ac0 65 61 64 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a  ead of UTF-8..**
24ad0 20 54 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e   The zero termin
24ae0 61 74 6f 72 20 69 73 20 6e 6f 74 20 69 6e 63 6c  ator is not incl
24af0 75 64 65 64 20 69 6e 20 74 68 69 73 20 63 6f 75  uded in this cou
24b00 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 62  nt..**.** The ob
24b10 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79  ject returned by
24b20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
24b30 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 61 6e 0a  _value()] is an.
24b40 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20  ** [unprotected 
24b50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
24b60 62 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70 72 6f  bject.  An unpro
24b70 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
24b80 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 6d  alue object.** m
24b90 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20  ay only be used 
24ba0 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 62 69  with [sqlite3_bi
24bb0 6e 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 20  nd_value()] and 
24bc0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
24bd0 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49 66 20  value()]..** If 
24be0 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 64  the [unprotected
24bf0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
24c00 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20  object returned 
24c10 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  by.** [sqlite3_c
24c20 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69  olumn_value()] i
24c30 73 20 75 73 65 64 20 69 6e 20 61 6e 79 20 6f 74  s used in any ot
24c40 68 65 72 20 77 61 79 2c 20 69 6e 63 6c 75 64 69  her way, includi
24c50 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 72  ng calls.** to r
24c60 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71  outines like [sq
24c70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
24c80 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  )], [sqlite3_val
24c90 75 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f  ue_text()],.** o
24ca0 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r [sqlite3_value
24cb0 5f 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e 20  _bytes()], then 
24cc0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
24cd0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
24ce0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
24cf0 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65  attempt to conve
24d00 72 74 20 74 68 65 20 76 61 6c 75 65 20 77 68 65  rt the value whe
24d10 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20  re appropriate. 
24d20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c   For.** example,
24d30 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   if the internal
24d40 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
24d50 69 73 20 46 4c 4f 41 54 20 61 6e 64 20 61 20 74  is FLOAT and a t
24d60 65 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73  ext result.** is
24d70 20 72 65 71 75 65 73 74 65 64 2c 20 5b 73 71 6c   requested, [sql
24d80 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d  ite3_snprintf()]
24d90 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61   is used interna
24da0 6c 6c 79 20 74 6f 20 70 65 72 66 6f 72 6d 20 74  lly to perform t
24db0 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e  he.** conversion
24dc0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20   automatically. 
24dd0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
24de0 61 62 6c 65 20 64 65 74 61 69 6c 73 20 74 68 65  able details the
24df0 20 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 2a 2a 20   conversions.** 
24e00 74 68 61 74 20 61 72 65 20 61 70 70 6c 69 65 64  that are applied
24e10 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
24e20 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62  ote>.** <table b
24e30 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74  order="1">.** <t
24e40 72 3e 3c 74 68 3e 20 49 6e 74 65 72 6e 61 6c 3c  r><th> Internal<
24e50 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 52 65 71  br>Type <th> Req
24e60 75 65 73 74 65 64 3c 62 72 3e 54 79 70 65 20 3c  uested<br>Type <
24e70 74 68 3e 20 20 43 6f 6e 76 65 72 73 69 6f 6e 0a  th>  Conversion.
24e80 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  **.** <tr><td>  
24e90 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 49 4e 54  NULL    <td> INT
24ea0 45 47 45 52 20 20 20 3c 74 64 3e 20 52 65 73 75  EGER   <td> Resu
24eb0 6c 74 20 69 73 20 30 0a 2a 2a 20 3c 74 72 3e 3c  lt is 0.** <tr><
24ec0 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64  td>  NULL    <td
24ed0 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e  >  FLOAT    <td>
24ee0 20 52 65 73 75 6c 74 20 69 73 20 30 2e 30 0a 2a   Result is 0.0.*
24ef0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c  * <tr><td>  NULL
24f00 20 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20      <td>   TEXT 
24f10 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69     <td> Result i
24f20 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a  s NULL pointer.*
24f30 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c  * <tr><td>  NULL
24f40 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20      <td>   BLOB 
24f50 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69     <td> Result i
24f60 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a  s NULL pointer.*
24f70 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47  * <tr><td> INTEG
24f80 45 52 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20  ER  <td>  FLOAT 
24f90 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20     <td> Convert 
24fa0 66 72 6f 6d 20 69 6e 74 65 67 65 72 20 74 6f 20  from integer to 
24fb0 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64  float.** <tr><td
24fc0 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20  > INTEGER  <td> 
24fd0 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41    TEXT    <td> A
24fe0 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f  SCII rendering o
24ff0 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a  f the integer.**
25000 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45   <tr><td> INTEGE
25010 52 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20  R  <td>   BLOB  
25020 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 49    <td> Same as I
25030 4e 54 45 47 45 52 2d 3e 54 45 58 54 0a 2a 2a 20  NTEGER->TEXT.** 
25040 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20  <tr><td>  FLOAT 
25050 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20    <td> INTEGER  
25060 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66 72   <td> Convert fr
25070 6f 6d 20 66 6c 6f 61 74 20 74 6f 20 69 6e 74 65  om float to inte
25080 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  ger.** <tr><td> 
25090 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20   FLOAT   <td>   
250a0 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53 43  TEXT    <td> ASC
250b0 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20  II rendering of 
250c0 74 68 65 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72  the float.** <tr
250d0 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c  ><td>  FLOAT   <
250e0 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74  td>   BLOB    <t
250f0 64 3e 20 53 61 6d 65 20 61 73 20 46 4c 4f 41 54  d> Same as FLOAT
25100 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74  ->TEXT.** <tr><t
25110 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  d>  TEXT    <td>
25120 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20   INTEGER   <td> 
25130 55 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c 74  Use atoi().** <t
25140 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20  r><td>  TEXT    
25150 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c  <td>  FLOAT    <
25160 74 64 3e 20 55 73 65 20 61 74 6f 66 28 29 0a 2a  td> Use atof().*
25170 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54  * <tr><td>  TEXT
25180 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20      <td>   BLOB 
25190 20 20 20 3c 74 64 3e 20 4e 6f 20 63 68 61 6e 67     <td> No chang
251a0 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42  e.** <tr><td>  B
251b0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 49 4e 54 45  LOB    <td> INTE
251c0 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65  GER   <td> Conve
251d0 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20  rt to TEXT then 
251e0 75 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c 74  use atoi().** <t
251f0 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20  r><td>  BLOB    
25200 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c  <td>  FLOAT    <
25210 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54  td> Convert to T
25220 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f  EXT then use ato
25230 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  f().** <tr><td> 
25240 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 20   BLOB    <td>   
25250 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 64 64  TEXT    <td> Add
25260 20 61 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74   a zero terminat
25270 6f 72 20 69 66 20 6e 65 65 64 65 64 0a 2a 2a 20  or if needed.** 
25280 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c  </table>.** </bl
25290 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20  ockquote>.**.** 
252a0 54 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65 20  The table above 
252b0 6d 61 6b 65 73 20 72 65 66 65 72 65 6e 63 65 20  makes reference 
252c0 74 6f 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  to standard C li
252d0 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20  brary functions 
252e0 61 74 6f 69 28 29 0a 2a 2a 20 61 6e 64 20 61 74  atoi().** and at
252f0 6f 66 28 29 2e 20 20 53 51 4c 69 74 65 20 64 6f  of().  SQLite do
25300 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 75 73  es not really us
25310 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  e these function
25320 73 2e 20 20 49 74 20 68 61 73 20 69 74 73 0a 2a  s.  It has its.*
25330 2a 20 6f 77 6e 20 65 71 75 69 76 61 6c 65 6e 74  * own equivalent
25340 20 69 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 6e   internal routin
25350 65 73 2e 20 20 54 68 65 20 61 74 6f 69 28 29 20  es.  The atoi() 
25360 61 6e 64 20 61 74 6f 66 28 29 20 6e 61 6d 65 73  and atof() names
25370 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 69 6e 20   are.** used in 
25380 74 68 65 20 74 61 62 6c 65 20 66 6f 72 20 62 72  the table for br
25390 65 76 69 74 79 20 61 6e 64 20 62 65 63 61 75 73  evity and becaus
253a0 65 20 74 68 65 79 20 61 72 65 20 66 61 6d 69 6c  e they are famil
253b0 69 61 72 20 74 6f 20 6d 6f 73 74 0a 2a 2a 20 43  iar to most.** C
253c0 20 70 72 6f 67 72 61 6d 6d 65 72 73 2e 0a 2a 2a   programmers..**
253d0 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68  .** Note that wh
253e0 65 6e 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69  en type conversi
253f0 6f 6e 73 20 6f 63 63 75 72 2c 20 70 6f 69 6e 74  ons occur, point
25400 65 72 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  ers returned by 
25410 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 73 20 74  prior.** calls t
25420 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  o sqlite3_column
25430 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33  _blob(), sqlite3
25440 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 20  _column_text(), 
25450 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  and/or.** sqlite
25460 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
25470 29 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 64  ) may be invalid
25480 61 74 65 64 2e 0a 2a 2a 20 54 79 70 65 20 63 6f  ated..** Type co
25490 6e 76 65 72 73 69 6f 6e 73 20 61 6e 64 20 70 6f  nversions and po
254a0 69 6e 74 65 72 20 69 6e 76 61 6c 69 64 61 74 69  inter invalidati
254b0 6f 6e 73 20 6d 69 67 68 74 20 6f 63 63 75 72 0a  ons might occur.
254c0 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  ** in the follow
254d0 69 6e 67 20 63 61 73 65 73 3a 0a 2a 2a 0a 2a 2a  ing cases:.**.**
254e0 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68   <ul>.** <li> Th
254f0 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e  e initial conten
25500 74 20 69 73 20 61 20 42 4c 4f 42 20 61 6e 64 20  t is a BLOB and 
25510 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
25520 65 78 74 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20  ext() or.**     
25530 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
25540 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c  text16() is call
25550 65 64 2e 20 20 41 20 7a 65 72 6f 2d 74 65 72 6d  ed.  A zero-term
25560 69 6e 61 74 6f 72 20 6d 69 67 68 74 0a 2a 2a 20  inator might.** 
25570 20 20 20 20 20 6e 65 65 64 20 74 6f 20 62 65 20       need to be 
25580 61 64 64 65 64 20 74 6f 20 74 68 65 20 73 74 72  added to the str
25590 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69  ing.</li>.** <li
255a0 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f  > The initial co
255b0 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 38 20 74  ntent is UTF-8 t
255c0 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  ext and sqlite3_
255d0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29  column_bytes16()
255e0 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69   or.**      sqli
255f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
25600 36 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20  6() is called.  
25610 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74  The content must
25620 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a   be converted.**
25630 20 20 20 20 20 20 74 6f 20 55 54 46 2d 31 36 2e        to UTF-16.
25640 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68  </li>.** <li> Th
25650 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e  e initial conten
25660 74 20 69 73 20 55 54 46 2d 31 36 20 74 65 78 74  t is UTF-16 text
25670 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c   and sqlite3_col
25680 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a  umn_bytes() or.*
25690 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  *      sqlite3_c
256a0 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 69 73 20  olumn_text() is 
256b0 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e  called.  The con
256c0 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e  tent must be con
256d0 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74  verted.**      t
256e0 6f 20 55 54 46 2d 38 2e 3c 2f 6c 69 3e 0a 2a 2a  o UTF-8.</li>.**
256f0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e   </ul>.**.** Con
25700 76 65 72 73 69 6f 6e 73 20 62 65 74 77 65 65 6e  versions between
25710 20 55 54 46 2d 31 36 62 65 20 61 6e 64 20 55 54   UTF-16be and UT
25720 46 2d 31 36 6c 65 20 61 72 65 20 61 6c 77 61 79  F-16le are alway
25730 73 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 20  s done in place 
25740 61 6e 64 20 64 6f 0a 2a 2a 20 6e 6f 74 20 69 6e  and do.** not in
25750 76 61 6c 69 64 61 74 65 20 61 20 70 72 69 6f 72  validate a prior
25760 20 70 6f 69 6e 74 65 72 2c 20 74 68 6f 75 67 68   pointer, though
25770 20 6f 66 20 63 6f 75 72 73 65 20 74 68 65 20 63   of course the c
25780 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 62 75  ontent of the bu
25790 66 66 65 72 0a 2a 2a 20 74 68 61 74 20 74 68 65  ffer.** that the
257a0 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 20 70   prior pointer p
257b0 6f 69 6e 74 73 20 74 6f 20 77 69 6c 6c 20 68 61  oints to will ha
257c0 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  ve been modified
257d0 2e 20 20 4f 74 68 65 72 20 6b 69 6e 64 73 0a 2a  .  Other kinds.*
257e0 2a 20 6f 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20  * of conversion 
257f0 61 72 65 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63  are done in plac
25800 65 20 77 68 65 6e 20 69 74 20 69 73 20 70 6f 73  e when it is pos
25810 73 69 62 6c 65 2c 20 62 75 74 20 73 6f 6d 65 74  sible, but somet
25820 69 6d 65 73 20 74 68 65 79 0a 2a 2a 20 61 72 65  imes they.** are
25830 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 61 6e   not possible an
25840 64 20 69 6e 20 74 68 6f 73 65 20 63 61 73 65 73  d in those cases
25850 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 73 20   prior pointers 
25860 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e  are invalidated.
25870 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 66 65 73  .**.** The safes
25880 74 20 61 6e 64 20 65 61 73 69 65 73 74 20 74 6f  t and easiest to
25890 20 72 65 6d 65 6d 62 65 72 20 70 6f 6c 69 63 79   remember policy
258a0 20 69 73 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68   is to invoke th
258b0 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  ese routines.** 
258c0 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  in one of the fo
258d0 6c 6c 6f 77 69 6e 67 20 77 61 79 73 3a 0a 2a 2a  llowing ways:.**
258e0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c 69  .** <ul>.**  <li
258f0 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  >sqlite3_column_
25900 74 65 78 74 28 29 20 66 6f 6c 6c 6f 77 65 64 20  text() followed 
25910 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  by sqlite3_colum
25920 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a  n_bytes()</li>.*
25930 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63  *  <li>sqlite3_c
25940 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 6c  olumn_blob() fol
25950 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33  lowed by sqlite3
25960 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c  _column_bytes()<
25970 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c  /li>.**  <li>sql
25980 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
25990 31 36 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79  16() followed by
259a0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
259b0 62 79 74 65 73 31 36 28 29 3c 2f 6c 69 3e 0a 2a  bytes16()</li>.*
259c0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e  * </ul>.**.** In
259d0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 79 6f   other words, yo
259e0 75 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71  u should call sq
259f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
25a00 74 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  t(),.** sqlite3_
25a10 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 6f  column_blob(), o
25a20 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  r sqlite3_column
25a30 5f 74 65 78 74 31 36 28 29 20 66 69 72 73 74 20  _text16() first 
25a40 74 6f 20 66 6f 72 63 65 20 74 68 65 20 72 65 73  to force the res
25a50 75 6c 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20  ult.** into the 
25a60 64 65 73 69 72 65 64 20 66 6f 72 6d 61 74 2c 20  desired format, 
25a70 74 68 65 6e 20 69 6e 76 6f 6b 65 20 73 71 6c 69  then invoke sqli
25a80 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
25a90 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33  () or.** sqlite3
25aa0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
25ab0 29 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73 69  ) to find the si
25ac0 7a 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ze of the result
25ad0 2e 20 20 44 6f 20 6e 6f 74 20 6d 69 78 20 63 61  .  Do not mix ca
25ae0 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  lls.** to sqlite
25af0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20  3_column_text() 
25b00 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  or sqlite3_colum
25b10 6e 5f 62 6c 6f 62 28 29 20 77 69 74 68 20 63 61  n_blob() with ca
25b20 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  lls to.** sqlite
25b30 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
25b40 28 29 2c 20 61 6e 64 20 64 6f 20 6e 6f 74 20 6d  (), and do not m
25b50 69 78 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ix calls to sqli
25b60 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
25b70 36 28 29 0a 2a 2a 20 77 69 74 68 20 63 61 6c 6c  6().** with call
25b80 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  s to sqlite3_col
25b90 75 6d 6e 5f 62 79 74 65 73 28 29 2e 0a 2a 2a 0a  umn_bytes()..**.
25ba0 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73 20  ** The pointers 
25bb0 72 65 74 75 72 6e 65 64 20 61 72 65 20 76 61 6c  returned are val
25bc0 69 64 20 75 6e 74 69 6c 20 61 20 74 79 70 65 20  id until a type 
25bd0 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72  conversion occur
25be0 73 20 61 73 0a 2a 2a 20 64 65 73 63 72 69 62 65  s as.** describe
25bf0 64 20 61 62 6f 76 65 2c 20 6f 72 20 75 6e 74 69  d above, or unti
25c00 6c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  l [sqlite3_step(
25c10 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  )] or [sqlite3_r
25c20 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73  eset()] or.** [s
25c30 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
25c40 29 5d 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54  )] is called.  T
25c50 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20  he memory space 
25c60 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 73 74 72  used to hold str
25c70 69 6e 67 73 0a 2a 2a 20 61 6e 64 20 42 4c 4f 42  ings.** and BLOB
25c80 73 20 69 73 20 66 72 65 65 64 20 61 75 74 6f 6d  s is freed autom
25c90 61 74 69 63 61 6c 6c 79 2e 20 20 44 6f 20 3c 62  atically.  Do <b
25ca0 3e 6e 6f 74 3c 2f 62 3e 20 70 61 73 73 20 74 68  >not</b> pass th
25cb0 65 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72  e pointers retur
25cc0 6e 65 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ned.** [sqlite3_
25cd0 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 5d 2c 20  column_blob()], 
25ce0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
25cf0 74 65 78 74 28 29 5d 2c 20 65 74 63 2e 20 69 6e  text()], etc. in
25d00 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  to.** [sqlite3_f
25d10 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66  ree()]..**.** If
25d20 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
25d30 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
25d40 73 20 64 75 72 69 6e 67 20 74 68 65 20 65 76 61  s during the eva
25d50 6c 75 61 74 69 6f 6e 20 6f 66 20 61 6e 79 0a 2a  luation of any.*
25d60 2a 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  * of these routi
25d70 6e 65 73 2c 20 61 20 64 65 66 61 75 6c 74 20 76  nes, a default v
25d80 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  alue is returned
25d90 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 76  .  The default v
25da0 61 6c 75 65 0a 2a 2a 20 69 73 20 65 69 74 68 65  alue.** is eithe
25db0 72 20 74 68 65 20 69 6e 74 65 67 65 72 20 30 2c  r the integer 0,
25dc0 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f   the floating po
25dd0 69 6e 74 20 6e 75 6d 62 65 72 20 30 2e 30 2c 20  int number 0.0, 
25de0 6f 72 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69  or a NULL.** poi
25df0 6e 74 65 72 2e 20 20 53 75 62 73 65 71 75 65 6e  nter.  Subsequen
25e00 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69  t calls to [sqli
25e10 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 77  te3_errcode()] w
25e20 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53  ill return.** [S
25e30 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a  QLITE_NOMEM]..**
25e40 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
25e50 3a 0a 2a 2a 20 5b 48 31 33 38 30 33 5d 20 5b 48  :.** [H13803] [H
25e60 31 33 38 30 36 5d 20 5b 48 31 33 38 30 39 5d 20  13806] [H13809] 
25e70 5b 48 31 33 38 31 32 5d 20 5b 48 31 33 38 31 35  [H13812] [H13815
25e80 5d 20 5b 48 31 33 38 31 38 5d 20 5b 48 31 33 38  ] [H13818] [H138
25e90 32 31 5d 20 5b 48 31 33 38 32 34 5d 0a 2a 2a 20  21] [H13824].** 
25ea0 5b 48 31 33 38 32 37 5d 20 5b 48 31 33 38 33 30  [H13827] [H13830
25eb0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
25ec0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
25ed0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
25ee0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
25ef0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
25f00 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
25f10 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71  _column_bytes(sq
25f20 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
25f30 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
25f40 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
25f50 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71  olumn_bytes16(sq
25f60 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
25f70 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
25f80 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  PI double sqlite
25f90 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28  3_column_double(
25fa0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
25fb0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
25fc0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
25fd0 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69  _column_int(sqli
25fe0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
25ff0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  Col);.SQLITE_API
26000 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
26010 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
26020 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  t64(sqlite3_stmt
26030 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51  *, int iCol);.SQ
26040 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75  LITE_API const u
26050 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71  nsigned char *sq
26060 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
26070 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  t(sqlite3_stmt*,
26080 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49   int iCol);.SQLI
26090 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
260a0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
260b0 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33  n_text16(sqlite3
260c0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
260d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
260e0 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
260f0 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74  _type(sqlite3_st
26100 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
26110 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
26120 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65  e3_value *sqlite
26130 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73  3_column_value(s
26140 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
26150 74 20 69 43 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t iCol);../*.** 
26160 43 41 50 49 33 52 45 46 3a 20 44 65 73 74 72 6f  CAPI3REF: Destro
26170 79 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61  y A Prepared Sta
26180 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48  tement Object {H
26190 31 33 33 30 30 7d 20 3c 53 37 30 33 30 30 3e 3c  13300} <S70300><
261a0 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30100>.**.** Th
261b0 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  e sqlite3_finali
261c0 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  ze() function is
261d0 20 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65 74   called to delet
261e0 65 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  e a [prepared st
261f0 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20  atement]..** If 
26200 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61  the statement wa
26210 73 20 65 78 65 63 75 74 65 64 20 73 75 63 63 65  s executed succe
26220 73 73 66 75 6c 6c 79 20 6f 72 20 6e 6f 74 20 65  ssfully or not e
26230 78 65 63 75 74 65 64 20 61 74 20 61 6c 6c 2c 20  xecuted at all, 
26240 74 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  then.** SQLITE_O
26250 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  K is returned. I
26260 66 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74  f execution of t
26270 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 61 69  he statement fai
26280 6c 65 64 20 74 68 65 6e 20 61 6e 0a 2a 2a 20 5b  led then an.** [
26290 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 5b  error code] or [
262a0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
262b0 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
262c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
262d0 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c 6c  tine can be call
262e0 65 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20  ed at any point 
262f0 64 75 72 69 6e 67 20 74 68 65 20 65 78 65 63 75  during the execu
26300 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 5b  tion of the.** [
26310 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
26320 6e 74 5d 2e 20 20 49 66 20 74 68 65 20 76 69 72  nt].  If the vir
26330 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73  tual machine has
26340 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65   not.** complete
26350 64 20 65 78 65 63 75 74 69 6f 6e 20 77 68 65 6e  d execution when
26360 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
26370 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 20 69 73   called, that is
26380 20 6c 69 6b 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74   like.** encount
26390 65 72 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6f  ering an error o
263a0 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6e  r an [sqlite3_in
263b0 74 65 72 72 75 70 74 20 7c 20 69 6e 74 65 72 72  terrupt | interr
263c0 75 70 74 5d 2e 0a 2a 2a 20 49 6e 63 6f 6d 70 6c  upt]..** Incompl
263d0 65 74 65 20 75 70 64 61 74 65 73 20 6d 61 79 20  ete updates may 
263e0 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  be rolled back a
263f0 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  nd transactions 
26400 63 61 6e 63 65 6c 65 64 2c 0a 2a 2a 20 64 65 70  canceled,.** dep
26410 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 63 69  ending on the ci
26420 72 63 75 6d 73 74 61 6e 63 65 73 2c 20 61 6e 64  rcumstances, and
26430 20 74 68 65 0a 2a 2a 20 5b 65 72 72 6f 72 20 63   the.** [error c
26440 6f 64 65 5d 20 72 65 74 75 72 6e 65 64 20 77 69  ode] returned wi
26450 6c 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 42  ll be [SQLITE_AB
26460 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ORT]..**.** Requ
26470 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
26480 31 33 30 32 5d 20 5b 48 31 31 33 30 34 5d 0a 2a  1302] [H11304].*
26490 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
264a0 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
264b0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
264c0 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  pStmt);../*.** C
264d0 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41  API3REF: Reset A
264e0 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d   Prepared Statem
264f0 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 33  ent Object {H133
26500 33 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a  30} <S70300>.**.
26510 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
26520 65 73 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  eset() function 
26530 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 73  is called to res
26540 65 74 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  et a [prepared s
26550 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a  tatement].** obj
26560 65 63 74 20 62 61 63 6b 20 74 6f 20 69 74 73 20  ect back to its 
26570 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2c 20 72  initial state, r
26580 65 61 64 79 20 74 6f 20 62 65 20 72 65 2d 65 78  eady to be re-ex
26590 65 63 75 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 53  ecuted..** Any S
265a0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 72  QL statement var
265b0 69 61 62 6c 65 73 20 74 68 61 74 20 68 61 64 20  iables that had 
265c0 76 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f 20  values bound to 
265d0 74 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74 68  them using.** th
265e0 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  e [sqlite3_bind_
265f0 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 62  blob | sqlite3_b
26600 69 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65 74  ind_*() API] ret
26610 61 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65 73  ain their values
26620 2e 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74 65  ..** Use [sqlite
26630 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73  3_clear_bindings
26640 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65  ()] to reset the
26650 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a   bindings..**.**
26660 20 7b 48 31 31 33 33 32 7d 20 54 68 65 20 5b 73   {H11332} The [s
26670 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d  qlite3_reset(S)]
26680 20 69 6e 74 65 72 66 61 63 65 20 72 65 73 65 74   interface reset
26690 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20  s the [prepared 
266a0 73 74 61 74 65 6d 65 6e 74 5d 20 53 0a 2a 2a 20  statement] S.** 
266b0 20 20 20 20 20 20 20 20 20 62 61 63 6b 20 74 6f           back to
266c0 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
266d0 66 20 69 74 73 20 70 72 6f 67 72 61 6d 2e 0a 2a  f its program..*
266e0 2a 0a 2a 2a 20 7b 48 31 31 33 33 34 7d 20 49 66  *.** {H11334} If
266f0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
26700 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
26710 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74  3_step(S)] for t
26720 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
26730 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
26740 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20 5b  nt] S returned [
26750 53 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 5b  SQLITE_ROW] or [
26760 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a  SQLITE_DONE],.**
26770 20 20 20 20 20 20 20 20 20 20 6f 72 20 69 66 20            or if 
26780 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29  [sqlite3_step(S)
26790 5d 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f  ] has never befo
267a0 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f  re been called o
267b0 6e 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  n S,.**         
267c0 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72   then [sqlite3_r
267d0 65 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73  eset(S)] returns
267e0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a   [SQLITE_OK]..**
267f0 0a 2a 2a 20 7b 48 31 31 33 33 36 7d 20 49 66 20  .** {H11336} If 
26800 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
26810 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
26820 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74 68  _step(S)] for th
26830 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  e.**          [p
26840 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
26850 74 5d 20 53 20 69 6e 64 69 63 61 74 65 64 20 61  t] S indicated a
26860 6e 20 65 72 72 6f 72 2c 20 74 68 65 6e 0a 2a 2a  n error, then.**
26870 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
26880 65 33 5f 72 65 73 65 74 28 53 29 5d 20 72 65 74  e3_reset(S)] ret
26890 75 72 6e 73 20 61 6e 20 61 70 70 72 6f 70 72 69  urns an appropri
268a0 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d  ate [error code]
268b0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 38 7d  ..**.** {H11338}
268c0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65   The [sqlite3_re
268d0 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63  set(S)] interfac
268e0 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67  e does not chang
268f0 65 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20  e the values.** 
26900 20 20 20 20 20 20 20 20 20 6f 66 20 61 6e 79 20           of any 
26910 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
26920 6f 62 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20  ob|bindings] on 
26930 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
26940 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53  atement] S..*/.S
26950 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
26960 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69  lite3_reset(sqli
26970 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
26980 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
26990 46 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65 64  F: Create Or Red
269a0 65 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74 69  efine SQL Functi
269b0 6f 6e 73 20 7b 48 31 36 31 30 30 7d 20 3c 53 32  ons {H16100} <S2
269c0 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0200>.** KEYWORD
269d0 53 3a 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72 65  S: {function cre
269e0 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d 0a  ation routines}.
269f0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70  ** KEYWORDS: {ap
26a00 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
26a10 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a  d SQL function}.
26a20 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70  ** KEYWORDS: {ap
26a30 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
26a40 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 7d  d SQL functions}
26a50 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 74 77 6f  .**.** These two
26a60 20 66 75 6e 63 74 69 6f 6e 73 20 28 63 6f 6c 6c   functions (coll
26a70 65 63 74 69 76 65 6c 79 20 6b 6e 6f 77 6e 20 61  ectively known a
26a80 73 20 22 66 75 6e 63 74 69 6f 6e 20 63 72 65 61  s "function crea
26a90 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 22 29 0a  tion routines").
26aa0 2a 2a 20 61 72 65 20 75 73 65 64 20 74 6f 20 61  ** are used to a
26ab0 64 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  dd SQL functions
26ac0 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 20 6f   or aggregates o
26ad0 72 20 74 6f 20 72 65 64 65 66 69 6e 65 20 74 68  r to redefine th
26ae0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66  e behavior.** of
26af0 20 65 78 69 73 74 69 6e 67 20 53 51 4c 20 66 75   existing SQL fu
26b00 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 65  nctions or aggre
26b10 67 61 74 65 73 2e 20 20 54 68 65 20 6f 6e 6c 79  gates.  The only
26b20 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
26b30 65 65 6e 20 74 68 65 0a 2a 2a 20 74 77 6f 20 69  een the.** two i
26b40 73 20 74 68 61 74 20 74 68 65 20 73 65 63 6f 6e  s that the secon
26b50 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65  d parameter, the
26b60 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 28 73 63   name of the (sc
26b70 61 6c 61 72 29 20 66 75 6e 63 74 69 6f 6e 20 6f  alar) function o
26b80 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 2c 20  r.** aggregate, 
26b90 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54  is encoded in UT
26ba0 46 2d 38 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  F-8 for sqlite3_
26bb0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
26bc0 29 20 61 6e 64 20 55 54 46 2d 31 36 0a 2a 2a 20  ) and UTF-16.** 
26bd0 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61  for sqlite3_crea
26be0 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 2e  te_function16().
26bf0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
26c00 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
26c10 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
26c20 65 63 74 69 6f 6e 5d 20 74 6f 20 77 68 69 63 68  ection] to which
26c30 20 74 68 65 20 53 51 4c 0a 2a 2a 20 66 75 6e 63   the SQL.** func
26c40 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 20 61 64  tion is to be ad
26c50 64 65 64 2e 20 20 49 66 20 61 20 73 69 6e 67 6c  ded.  If a singl
26c60 65 20 70 72 6f 67 72 61 6d 20 75 73 65 73 20 6d  e program uses m
26c70 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74  ore than one dat
26c80 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
26c90 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20  ion internally, 
26ca0 74 68 65 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f  then SQL functio
26cb0 6e 73 20 6d 75 73 74 20 62 65 20 61 64 64 65 64  ns must be added
26cc0 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 74 6f   individually to
26cd0 0a 2a 2a 20 65 61 63 68 20 64 61 74 61 62 61 73  .** each databas
26ce0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a  e connection..**
26cf0 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 70  .** The second p
26d00 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
26d10 6e 61 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20  name of the SQL 
26d20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 63  function to be c
26d30 72 65 61 74 65 64 20 6f 72 0a 2a 2a 20 72 65 64  reated or.** red
26d40 65 66 69 6e 65 64 2e 20 20 54 68 65 20 6c 65 6e  efined.  The len
26d50 67 74 68 20 6f 66 20 74 68 65 20 6e 61 6d 65 20  gth of the name 
26d60 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 32 35  is limited to 25
26d70 35 20 62 79 74 65 73 2c 20 65 78 63 6c 75 73 69  5 bytes, exclusi
26d80 76 65 20 6f 66 0a 2a 2a 20 74 68 65 20 7a 65 72  ve of.** the zer
26d90 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 4e  o-terminator.  N
26da0 6f 74 65 20 74 68 61 74 20 74 68 65 20 6e 61 6d  ote that the nam
26db0 65 20 6c 65 6e 67 74 68 20 6c 69 6d 69 74 20 69  e length limit i
26dc0 73 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 74 0a  s in bytes, not.
26dd0 2a 2a 20 63 68 61 72 61 63 74 65 72 73 2e 20 20  ** characters.  
26de0 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63  Any attempt to c
26df0 72 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f 6e  reate a function
26e00 20 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20 6e   with a longer n
26e10 61 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 75  ame.** will resu
26e20 6c 74 20 69 6e 20 5b 53 51 4c 49 54 45 5f 45 52  lt in [SQLITE_ER
26e30 52 4f 52 5d 20 62 65 69 6e 67 20 72 65 74 75 72  ROR] being retur
26e40 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ned..**.** The t
26e50 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 28  hird parameter (
26e60 6e 41 72 67 29 0a 2a 2a 20 69 73 20 74 68 65 20  nArg).** is the 
26e70 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
26e80 6e 74 73 20 74 68 61 74 20 74 68 65 20 53 51 4c  nts that the SQL
26e90 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20   function or.** 
26ea0 61 67 67 72 65 67 61 74 65 20 74 61 6b 65 73 2e  aggregate takes.
26eb0 20 49 66 20 74 68 69 73 20 70 61 72 61 6d 65 74   If this paramet
26ec0 65 72 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20  er is negative, 
26ed0 74 68 65 6e 20 74 68 65 20 53 51 4c 20 66 75 6e  then the SQL fun
26ee0 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72  ction or.** aggr
26ef0 65 67 61 74 65 20 6d 61 79 20 74 61 6b 65 20 61  egate may take a
26f00 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ny number of arg
26f10 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  uments..**.** Th
26f20 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  e fourth paramet
26f30 65 72 2c 20 65 54 65 78 74 52 65 70 2c 20 73 70  er, eTextRep, sp
26f40 65 63 69 66 69 65 73 20 77 68 61 74 0a 2a 2a 20  ecifies what.** 
26f50 5b 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 20 74  [SQLITE_UTF8 | t
26f60 65 78 74 20 65 6e 63 6f 64 69 6e 67 5d 20 74 68  ext encoding] th
26f70 69 73 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  is SQL function 
26f80 70 72 65 66 65 72 73 20 66 6f 72 0a 2a 2a 20 69  prefers for.** i
26f90 74 73 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20  ts parameters.  
26fa0 41 6e 79 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  Any SQL function
26fb0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
26fc0 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20 74  should be able t
26fd0 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77  o work.** work w
26fe0 69 74 68 20 55 54 46 2d 38 2c 20 55 54 46 2d 31  ith UTF-8, UTF-1
26ff0 36 6c 65 2c 20 6f 72 20 55 54 46 2d 31 36 62 65  6le, or UTF-16be
27000 2e 20 20 42 75 74 20 73 6f 6d 65 20 69 6d 70 6c  .  But some impl
27010 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79 20  ementations may 
27020 62 65 0a 2a 2a 20 6d 6f 72 65 20 65 66 66 69 63  be.** more effic
27030 69 65 6e 74 20 77 69 74 68 20 6f 6e 65 20 65 6e  ient with one en
27040 63 6f 64 69 6e 67 20 74 68 61 6e 20 61 6e 6f 74  coding than anot
27050 68 65 72 2e 20 20 49 74 20 69 73 20 61 6c 6c 6f  her.  It is allo
27060 77 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65  wed to.** invoke
27070 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
27080 66 75 6e 63 74 69 6f 6e 28 29 20 6f 72 20 73 71  function() or sq
27090 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
270a0 63 74 69 6f 6e 31 36 28 29 20 6d 75 6c 74 69 70  ction16() multip
270b0 6c 65 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68  le.** times with
270c0 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69   the same functi
270d0 6f 6e 20 62 75 74 20 77 69 74 68 20 64 69 66 66  on but with diff
270e0 65 72 65 6e 74 20 76 61 6c 75 65 73 20 6f 66 20  erent values of 
270f0 65 54 65 78 74 52 65 70 2e 0a 2a 2a 20 57 68 65  eTextRep..** Whe
27100 6e 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65  n multiple imple
27110 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
27120 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20  e same function 
27130 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 53  are available, S
27140 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 70 69  QLite.** will pi
27150 63 6b 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20  ck the one that 
27160 69 6e 76 6f 6c 76 65 73 20 74 68 65 20 6c 65 61  involves the lea
27170 73 74 20 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74  st amount of dat
27180 61 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a  a conversion..**
27190 20 49 66 20 74 68 65 72 65 20 69 73 20 6f 6e 6c   If there is onl
271a0 79 20 61 20 73 69 6e 67 6c 65 20 69 6d 70 6c 65  y a single imple
271b0 6d 65 6e 74 61 74 69 6f 6e 20 77 68 69 63 68 20  mentation which 
271c0 64 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 77 68  does not care wh
271d0 61 74 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64  at text.** encod
271e0 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68 65  ing is used, the
271f0 6e 20 74 68 65 20 66 6f 75 72 74 68 20 61 72 67  n the fourth arg
27200 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20  ument should be 
27210 5b 53 51 4c 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a  [SQLITE_ANY]..**
27220 0a 2a 2a 20 54 68 65 20 66 69 66 74 68 20 70 61  .** The fifth pa
27230 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 61 72  rameter is an ar
27240 62 69 74 72 61 72 79 20 70 6f 69 6e 74 65 72 2e  bitrary pointer.
27250 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61    The implementa
27260 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 66  tion of the.** f
27270 75 6e 63 74 69 6f 6e 20 63 61 6e 20 67 61 69 6e  unction can gain
27280 20 61 63 63 65 73 73 20 74 6f 20 74 68 69 73 20   access to this 
27290 70 6f 69 6e 74 65 72 20 75 73 69 6e 67 20 5b 73  pointer using [s
272a0 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
272b0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ()]..**.** The s
272c0 65 76 65 6e 74 68 2c 20 65 69 67 68 74 68 20 61  eventh, eighth a
272d0 6e 64 20 6e 69 6e 74 68 20 70 61 72 61 6d 65 74  nd ninth paramet
272e0 65 72 73 2c 20 78 46 75 6e 63 2c 20 78 53 74 65  ers, xFunc, xSte
272f0 70 20 61 6e 64 20 78 46 69 6e 61 6c 2c 20 61 72  p and xFinal, ar
27300 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f  e.** pointers to
27310 20 43 2d 6c 61 6e 67 75 61 67 65 20 66 75 6e 63   C-language func
27320 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
27330 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e  ment the SQL fun
27340 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72  ction or.** aggr
27350 65 67 61 74 65 2e 20 41 20 73 63 61 6c 61 72 20  egate. A scalar 
27360 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71  SQL function req
27370 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65  uires an impleme
27380 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 78  ntation of the x
27390 46 75 6e 63 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  Func.** callback
273a0 20 6f 6e 6c 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e   only, NULL poin
273b0 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 70  ters should be p
273c0 61 73 73 65 64 20 61 73 20 74 68 65 20 78 53 74  assed as the xSt
273d0 65 70 20 61 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a  ep and xFinal.**
273e0 20 70 61 72 61 6d 65 74 65 72 73 2e 20 41 6e 20   parameters. An 
273f0 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75  aggregate SQL fu
27400 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20  nction requires 
27410 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  an implementatio
27420 6e 20 6f 66 20 78 53 74 65 70 0a 2a 2a 20 61 6e  n of xStep.** an
27430 64 20 78 46 69 6e 61 6c 20 61 6e 64 20 4e 55 4c  d xFinal and NUL
27440 4c 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73  L should be pass
27450 65 64 20 66 6f 72 20 78 46 75 6e 63 2e 20 54 6f  ed for xFunc. To
27460 20 64 65 6c 65 74 65 20 61 6e 20 65 78 69 73 74   delete an exist
27470 69 6e 67 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74  ing.** SQL funct
27480 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65  ion or aggregate
27490 2c 20 70 61 73 73 20 4e 55 4c 4c 20 66 6f 72 20  , pass NULL for 
274a0 61 6c 6c 20 74 68 72 65 65 20 66 75 6e 63 74 69  all three functi
274b0 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a  on callbacks..**
274c0 0a 2a 2a 20 49 74 20 69 73 20 70 65 72 6d 69 74  .** It is permit
274d0 74 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20  ted to register 
274e0 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65  multiple impleme
274f0 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
27500 73 61 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  same.** function
27510 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
27520 6e 61 6d 65 20 62 75 74 20 77 69 74 68 20 65 69  name but with ei
27530 74 68 65 72 20 64 69 66 66 65 72 69 6e 67 20 6e  ther differing n
27540 75 6d 62 65 72 73 20 6f 66 0a 2a 2a 20 61 72 67  umbers of.** arg
27550 75 6d 65 6e 74 73 20 6f 72 20 64 69 66 66 65 72  uments or differ
27560 69 6e 67 20 70 72 65 66 65 72 72 65 64 20 74 65  ing preferred te
27570 78 74 20 65 6e 63 6f 64 69 6e 67 73 2e 20 20 53  xt encodings.  S
27580 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a  QLite will use.*
27590 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  * the implementa
275a0 74 69 6f 6e 20 6d 6f 73 74 20 63 6c 6f 73 65 6c  tion most closel
275b0 79 20 6d 61 74 63 68 65 73 20 74 68 65 20 77 61  y matches the wa
275c0 79 20 69 6e 20 77 68 69 63 68 20 74 68 65 0a 2a  y in which the.*
275d0 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69  * SQL function i
275e0 73 20 75 73 65 64 2e 20 20 41 20 66 75 6e 63 74  s used.  A funct
275f0 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ion implementati
27600 6f 6e 20 77 69 74 68 20 61 20 6e 6f 6e 2d 6e 65  on with a non-ne
27610 67 61 74 69 76 65 0a 2a 2a 20 6e 41 72 67 20 70  gative.** nArg p
27620 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 65  arameter is a be
27630 74 74 65 72 20 6d 61 74 63 68 20 74 68 61 6e 20  tter match than 
27640 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65  a function imple
27650 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 0a 2a  mentation with.*
27660 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 41 72  * a negative nAr
27670 67 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 77  g.  A function w
27680 68 65 72 65 20 74 68 65 20 70 72 65 66 65 72 72  here the preferr
27690 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  ed text encoding
276a0 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20  .** matches the 
276b0 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e  database encodin
276c0 67 20 69 73 20 61 20 62 65 74 74 65 72 0a 2a 2a  g is a better.**
276d0 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75   match than a fu
276e0 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65  nction where the
276f0 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 64 69 66   encoding is dif
27700 66 65 72 65 6e 74 2e 20 20 0a 2a 2a 20 41 20 66  ferent.  .** A f
27710 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68  unction where th
27720 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65  e encoding diffe
27730 72 65 6e 63 65 20 69 73 20 62 65 74 77 65 65 6e  rence is between
27740 20 55 54 46 31 36 6c 65 20 61 6e 64 20 55 54 46   UTF16le and UTF
27750 31 36 62 65 0a 2a 2a 20 69 73 20 61 20 63 6c 6f  16be.** is a clo
27760 73 65 72 20 6d 61 74 63 68 20 74 68 61 6e 20 61  ser match than a
27770 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20   function where 
27780 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 66  the encoding dif
27790 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a 20 62 65  ference is.** be
277a0 74 77 65 65 6e 20 55 54 46 38 20 61 6e 64 20 55  tween UTF8 and U
277b0 54 46 31 36 2e 0a 2a 2a 0a 2a 2a 20 42 75 69 6c  TF16..**.** Buil
277c0 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6d  t-in functions m
277d0 61 79 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64  ay be overloaded
277e0 20 62 79 20 6e 65 77 20 61 70 70 6c 69 63 61 74   by new applicat
277f0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
27800 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 66 69  tions..** The fi
27810 72 73 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  rst application-
27820 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
27830 20 77 69 74 68 20 61 20 67 69 76 65 6e 20 6e 61   with a given na
27840 6d 65 20 6f 76 65 72 72 69 64 65 73 20 61 6c 6c  me overrides all
27850 0a 2a 2a 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e  .** built-in fun
27860 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 73 61  ctions in the sa
27870 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  me [database con
27880 6e 65 63 74 69 6f 6e 5d 20 77 69 74 68 20 74 68  nection] with th
27890 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 20  e same name..** 
278a0 53 75 62 73 65 71 75 65 6e 74 20 61 70 70 6c 69  Subsequent appli
278b0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
278c0 75 6e 63 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  unctions of the 
278d0 73 61 6d 65 20 6e 61 6d 65 20 6f 6e 6c 79 20 6f  same name only o
278e0 76 65 72 72 69 64 65 20 0a 2a 2a 20 70 72 69 6f  verride .** prio
278f0 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  r application-de
27900 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20  fined functions 
27910 74 68 61 74 20 61 72 65 20 61 6e 20 65 78 61 63  that are an exac
27920 74 20 6d 61 74 63 68 20 66 6f 72 20 74 68 65 0a  t match for the.
27930 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 72  ** number of par
27940 61 6d 65 74 65 72 73 20 61 6e 64 20 70 72 65 66  ameters and pref
27950 65 72 72 65 64 20 65 6e 63 6f 64 69 6e 67 2e 0a  erred encoding..
27960 2a 2a 0a 2a 2a 20 41 6e 20 61 70 70 6c 69 63 61  **.** An applica
27970 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
27980 63 74 69 6f 6e 20 69 73 20 70 65 72 6d 69 74 74  ction is permitt
27990 65 64 20 74 6f 20 63 61 6c 6c 20 6f 74 68 65 72  ed to call other
279a0 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74 65 72  .** SQLite inter
279b0 66 61 63 65 73 2e 20 20 48 6f 77 65 76 65 72 2c  faces.  However,
279c0 20 73 75 63 68 20 63 61 6c 6c 73 20 6d 75 73 74   such calls must
279d0 20 6e 6f 74 0a 2a 2a 20 63 6c 6f 73 65 20 74 68   not.** close th
279e0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
279f0 63 74 69 6f 6e 20 6e 6f 72 20 66 69 6e 61 6c 69  ction nor finali
27a00 7a 65 20 6f 72 20 72 65 73 65 74 20 74 68 65 20  ze or reset the 
27a10 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74  prepared.** stat
27a20 65 6d 65 6e 74 20 69 6e 20 77 68 69 63 68 20 74  ement in which t
27a30 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72  he function is r
27a40 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65  unning..**.** Re
27a50 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
27a60 48 31 36 31 30 33 5d 20 5b 48 31 36 31 30 36 5d  H16103] [H16106]
27a70 20 5b 48 31 36 31 30 39 5d 20 5b 48 31 36 31 31   [H16109] [H1611
27a80 32 5d 20 5b 48 31 36 31 31 38 5d 20 5b 48 31 36  2] [H16118] [H16
27a90 31 32 31 5d 20 5b 48 31 36 31 32 34 5d 20 5b 48  121] [H16124] [H
27aa0 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36 31 33  16127].** [H1613
27ab0 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48 31 36  0] [H16133] [H16
27ac0 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20 5b 48  136] [H16139] [H
27ad0 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45  16142].*/.SQLITE
27ae0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
27af0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
27b00 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
27b10 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
27b20 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20  FunctionName,.  
27b30 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20  int nArg,.  int 
27b40 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64  eTextRep,.  void
27b50 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28   *pApp,.  void (
27b60 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
27b70 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
27b80 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20  ite3_value**),. 
27b90 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
27ba0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
27bb0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
27bc0 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  e**),.  void (*x
27bd0 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
27be0 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51 4c 49  ontext*).);.SQLI
27bf0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
27c00 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
27c10 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20  on16(.  sqlite3 
27c20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69  *db,.  const voi
27c30 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  d *zFunctionName
27c40 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20  ,.  int nArg,.  
27c50 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20  int eTextRep,.  
27c60 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f  void *pApp,.  vo
27c70 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
27c80 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
27c90 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
27ca0 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65  ),.  void (*xSte
27cb0 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
27cc0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
27cd0 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64  value**),.  void
27ce0 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74   (*xFinal)(sqlit
27cf0 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a  e3_context*).);.
27d00 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
27d10 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67 73 20   Text Encodings 
27d20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32 30 30  {H10267} <S50200
27d30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a 2a 2a  > <H16100>.**.**
27d40 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 20   These constant 
27d50 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63  define integer c
27d60 6f 64 65 73 20 74 68 61 74 20 72 65 70 72 65 73  odes that repres
27d70 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a  ent the various.
27d80 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  ** text encoding
27d90 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 53  s supported by S
27da0 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  QLite..*/.#defin
27db0 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20 20  e SQLITE_UTF8   
27dc0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
27dd0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  e SQLITE_UTF16LE
27de0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
27df0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  e SQLITE_UTF16BE
27e00 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e          3.#defin
27e10 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 20  e SQLITE_UTF16  
27e20 20 20 20 20 20 20 20 20 34 20 20 20 20 2f 2a 20          4    /* 
27e30 55 73 65 20 6e 61 74 69 76 65 20 62 79 74 65 20  Use native byte 
27e40 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  order */.#define
27e50 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20 20 20   SQLITE_ANY     
27e60 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a 20 73         5    /* s
27e70 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
27e80 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23  nction only */.#
27e90 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54  define SQLITE_UT
27ea0 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38 20 20  F16_ALIGNED  8  
27eb0 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72 65    /* sqlite3_cre
27ec0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e  ate_collation on
27ed0 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ly */../*.** CAP
27ee0 49 33 52 45 46 3a 20 44 65 70 72 65 63 61 74 65  I3REF: Deprecate
27ef0 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44  d Functions.** D
27f00 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20  EPRECATED.**.** 
27f10 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  These functions 
27f20 61 72 65 20 5b 64 65 70 72 65 63 61 74 65 64 5d  are [deprecated]
27f30 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d  .  In order to m
27f40 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63 6b 77  aintain.** backw
27f50 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
27f60 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 63 6f  ty with older co
27f70 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63 74 69  de, these functi
27f80 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a  ons continue .**
27f90 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64   to be supported
27fa0 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65 77 20  .  However, new 
27fb0 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f  applications sho
27fc0 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74 68 65  uld avoid.** the
27fd0 20 75 73 65 20 6f 66 20 74 68 65 73 65 20 66 75   use of these fu
27fe0 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68 65 6c  nctions.  To hel
27ff0 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65 6f 70  p encourage peop
28000 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 75  le to avoid.** u
28010 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e 63 74  sing these funct
28020 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e 6f 74  ions, we are not
28030 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c 20 79   going to tell y
28040 6f 75 20 77 68 61 74 20 74 68 65 79 20 64 6f 2e  ou what they do.
28050 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
28060 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
28070 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  ED.SQLITE_API SQ
28080 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20  LITE_DEPRECATED 
28090 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67 72  int sqlite3_aggr
280a0 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69  egate_count(sqli
280b0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53  te3_context*);.S
280c0 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
280d0 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20  _DEPRECATED int 
280e0 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28  sqlite3_expired(
280f0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
28100 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
28110 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74  E_DEPRECATED int
28120 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65   sqlite3_transfe
28130 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  r_bindings(sqlit
28140 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69 74 65  e3_stmt*, sqlite
28150 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45  3_stmt*);.SQLITE
28160 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52  _API SQLITE_DEPR
28170 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74  ECATED int sqlit
28180 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65  e3_global_recove
28190 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  r(void);.SQLITE_
281a0 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45  API SQLITE_DEPRE
281b0 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c 69 74  CATED void sqlit
281c0 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75  e3_thread_cleanu
281d0 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  p(void);.SQLITE_
281e0 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45  API SQLITE_DEPRE
281f0 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65  CATED int sqlite
28200 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 76  3_memory_alarm(v
28210 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c  oid(*)(void*,sql
28220 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 2c  ite3_int64,int),
28230 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e  void*,sqlite3_in
28240 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a  t64);.#endif../*
28250 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62  .** CAPI3REF: Ob
28260 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75 6e 63  taining SQL Func
28270 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72 20 56  tion Parameter V
28280 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d 20 3c  alues {H15100} <
28290 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20200>.**.** Th
282a0 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69 6d 70  e C-language imp
282b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 53  lementation of S
282c0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64  QL functions and
282d0 20 61 67 67 72 65 67 61 74 65 73 20 75 73 65 73   aggregates uses
282e0 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f 66 20  .** this set of 
282f0 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e  interface routin
28300 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  es to access the
28310 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c 75 65   parameter value
28320 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75 6e 63  s on.** the func
28330 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74  tion or aggregat
28340 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46 75  e..**.** The xFu
28350 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72 20 66  nc (for scalar f
28360 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78 53 74  unctions) or xSt
28370 65 70 20 28 66 6f 72 20 61 67 67 72 65 67 61 74  ep (for aggregat
28380 65 73 29 20 70 61 72 61 6d 65 74 65 72 73 0a 2a  es) parameters.*
28390 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72  * to [sqlite3_cr
283a0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d  eate_function()]
283b0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72   and [sqlite3_cr
283c0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
283d0 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63 61 6c  )].** define cal
283e0 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d 70 6c  lbacks that impl
283f0 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75  ement the SQL fu
28400 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72  nctions and aggr
28410 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65 20 34  egates..** The 4
28420 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
28430 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 73 20  these callbacks 
28440 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70  is an array of p
28450 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 5b 70  ointers to.** [p
28460 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
28470 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e  _value] objects.
28480 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 5b    There is one [
28490 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
284a0 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65 61 63  bject for.** eac
284b0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
284c0 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  he SQL function.
284d0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
284e0 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20   are used to.** 
284f0 65 78 74 72 61 63 74 20 76 61 6c 75 65 73 20 66  extract values f
28500 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33  rom the [sqlite3
28510 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e  _value] objects.
28520 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
28530 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c 79 20  tines work only 
28540 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65 64 20  with [protected 
28550 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
28560 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79 20 61  bjects..** Any a
28570 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68  ttempt to use th
28580 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e 20  ese routines on 
28590 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20  an [unprotected 
285a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a 2a  sqlite3_value].*
285b0 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c 74 73  * object results
285c0 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65   in undefined be
285d0 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  havior..**.** Th
285e0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72  ese routines wor
285f0 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20  k just like the 
28600 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 5b 63  corresponding [c
28610 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e  olumn access fun
28620 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63 65 70  ctions].** excep
28630 74 20 74 68 61 74 20 20 74 68 65 73 65 20 72 6f  t that  these ro
28640 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20 73 69  utines take a si
28650 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65 64 20  ngle [protected 
28660 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
28670 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 65 72  bject.** pointer
28680 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 5b 73   instead of a [s
28690 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 70 6f  qlite3_stmt*] po
286a0 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 6e 74  inter and an int
286b0 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62  eger column numb
286c0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  er..**.** The sq
286d0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
286e0 31 36 28 29 20 69 6e 74 65 72 66 61 63 65 20 65  16() interface e
286f0 78 74 72 61 63 74 73 20 61 20 55 54 46 2d 31 36  xtracts a UTF-16
28700 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 74 68   string.** in th
28710 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d 6f 72  e native byte-or
28720 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73 74 20  der of the host 
28730 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a 2a 2a  machine.  The.**
28740 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
28750 65 78 74 31 36 62 65 28 29 20 61 6e 64 20 73 71  ext16be() and sq
28760 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
28770 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65  16le() interface
28780 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55 54 46  s.** extract UTF
28790 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73 20 62  -16 strings as b
287a0 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20 6c 69  ig-endian and li
287b0 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65 73 70  ttle-endian resp
287c0 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20  ectively..**.** 
287d0 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  The sqlite3_valu
287e0 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 29  e_numeric_type()
287f0 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d   interface attem
28800 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a 2a 20  pts to apply.** 
28810 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79  numeric affinity
28820 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e 20 20   to the value.  
28830 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20  This means that 
28840 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a 2a 2a  an attempt is.**
28850 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 72 74   made to convert
28860 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 61 6e   the value to an
28870 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f 61   integer or floa
28880 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 66 0a  ting point.  If.
28890 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 65 72  ** such a conver
288a0 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65  sion is possible
288b0 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66   without loss of
288c0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 69 6e   information (in
288d0 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c   other.** words,
288e0 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73   if the value is
288f0 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 6c   a string that l
28900 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62  ooks like a numb
28910 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  er).** then the 
28920 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 65  conversion is pe
28930 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 72 77  rformed.  Otherw
28940 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f  ise no conversio
28950 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 68 65  n occurs..** The
28960 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52   [SQLITE_INTEGER
28970 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61 66 74   | datatype] aft
28980 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  er conversion is
28990 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
289a0 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 72 74   Please pay part
289b0 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 6f 6e  icular attention
289c0 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 68 61   to the fact tha
289d0 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72 65  t the pointer re
289e0 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b  turned.** from [
289f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
28a00 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  ob()], [sqlite3_
28a10 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20 6f  value_text()], o
28a20 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61  r.** [sqlite3_va
28a30 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 63 61  lue_text16()] ca
28a40 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64  n be invalidated
28a50 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e 74   by a subsequent
28a60 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c   call to.** [sql
28a70 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
28a80 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61  ()], [sqlite3_va
28a90 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d 2c 20  lue_bytes16()], 
28aa0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
28ab0 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73  ext()],.** or [s
28ac0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
28ad0 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  t16()]..**.** Th
28ae0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75 73  ese routines mus
28af0 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  t be called from
28b00 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
28b10 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66   as.** the SQL f
28b20 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 75 70  unction that sup
28b30 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c 69 74  plied the [sqlit
28b40 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 61 6d  e3_value*] param
28b50 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  eters..**.** Req
28b60 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
28b70 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36 5d 20  15103] [H15106] 
28b80 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31 31 32  [H15109] [H15112
28b90 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31 35 31  ] [H15115] [H151
28ba0 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b 48 31  18] [H15121] [H1
28bb0 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31 32 37  5124].** [H15127
28bc0 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31 35 31  ] [H15130] [H151
28bd0 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a 2f 0a  33] [H15136].*/.
28be0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
28bf0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
28c00 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65  alue_blob(sqlite
28c10 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
28c20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
28c30 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71  3_value_bytes(sq
28c40 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
28c50 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
28c60 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
28c70 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  s16(sqlite3_valu
28c80 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
28c90 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 76  double sqlite3_v
28ca0 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69  alue_double(sqli
28cb0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
28cc0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
28cd0 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71  te3_value_int(sq
28ce0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
28cf0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
28d00 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
28d10 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c 69  value_int64(sqli
28d20 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
28d30 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e  ITE_API const un
28d40 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c  signed char *sql
28d50 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
28d60 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28d70 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
28d80 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
28d90 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c  value_text16(sql
28da0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
28db0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
28dc0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  oid *sqlite3_val
28dd0 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69  ue_text16le(sqli
28de0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
28df0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
28e00 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  id *sqlite3_valu
28e10 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 74  e_text16be(sqlit
28e20 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
28e30 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
28e40 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 71  e3_value_type(sq
28e50 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
28e60 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
28e70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
28e80 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33  ric_type(sqlite3
28e90 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _value*);../*.**
28ea0 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69   CAPI3REF: Obtai
28eb0 6e 20 41 67 67 72 65 67 61 74 65 20 46 75 6e 63  n Aggregate Func
28ec0 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b 48 31  tion Context {H1
28ed0 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a  6210} <S20200>.*
28ee0 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65  *.** The impleme
28ef0 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67 72 65  ntation of aggre
28f00 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f  gate SQL functio
28f10 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f 75 74  ns use this rout
28f20 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a  ine to allocate.
28f30 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65 20 66  ** a structure f
28f40 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 69 72  or storing their
28f50 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   state..**.** Th
28f60 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 65  e first time the
28f70 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
28f80 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72 6f 75  te_context() rou
28f90 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66  tine is called f
28fa0 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63 75 6c  or a.** particul
28fb0 61 72 20 61 67 67 72 65 67 61 74 65 2c 20 53 51  ar aggregate, SQ
28fc0 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73 20 6e  Lite allocates n
28fd0 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2c  Bytes of memory,
28fe0 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68 61 74   zeroes out that
28ff0 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e 64 20  .** memory, and 
29000 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
29010 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65 63 6f  r to it. On seco
29020 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
29030 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71  t calls to.** sq
29040 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
29050 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20 74 68  context() for th
29060 65 20 73 61 6d 65 20 61 67 67 72 65 67 61 74 65  e same aggregate
29070 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65 78 2c   function index,
29080 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62 75 66  .** the same buf
29090 66 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  fer is returned.
290a0 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
290b0 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 72 65  ion of the aggre
290c0 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a 2a 20  gate can use.** 
290d0 74 68 65 20 72 65 74 75 72 6e 65 64 20 62 75 66  the returned buf
290e0 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c 61 74  fer to accumulat
290f0 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 51  e data..**.** SQ
29100 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  Lite automatical
29110 6c 79 20 66 72 65 65 73 20 74 68 65 20 61 6c 6c  ly frees the all
29120 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 77 68  ocated buffer wh
29130 65 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 65  en the aggregate
29140 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63 6c 75  .** query conclu
29150 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  des..**.** The f
29160 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 73  irst parameter s
29170 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70 79 20  hould be a copy 
29180 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  of the.** [sqlit
29190 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53 51 4c  e3_context | SQL
291a0 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78   function contex
291b0 74 5d 20 74 68 61 74 20 69 73 20 74 68 65 20 66  t] that is the f
291c0 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 0a 2a  irst parameter.*
291d0 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  * to the callbac
291e0 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 69  k routine that i
291f0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 61 67  mplements the ag
29200 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
29210 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
29220 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c  tine must be cal
29230 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d  led from the sam
29240 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63  e thread in whic
29250 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65 67 61  h.** the aggrega
29260 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  te SQL function 
29270 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a  is running..**.*
29280 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
29290 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48 31 36  ** [H16211] [H16
292a0 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20 5b 48  213] [H16215] [H
292b0 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45  16217].*/.SQLITE
292c0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
292d0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
292e0 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  text(sqlite3_con
292f0 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 74 65  text*, int nByte
29300 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  s);../*.** CAPI3
29310 52 45 46 3a 20 55 73 65 72 20 44 61 74 61 20 46  REF: User Data F
29320 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31  or Functions {H1
29330 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a  6240} <S20200>.*
29340 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
29350 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 6e 74  _user_data() int
29360 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
29370 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20   copy of.** the 
29380 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 61 73  pointer that was
29390 20 74 68 65 20 70 55 73 65 72 44 61 74 61 20 70   the pUserData p
293a0 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 35 74  arameter (the 5t
293b0 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20  h parameter).** 
293c0 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
293d0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
293e0 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74  )].** and [sqlit
293f0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
29400 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73  on16()] routines
29410 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c 79   that originally
29420 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20 74  .** registered t
29430 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64  he application d
29440 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e  efined function.
29450 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69   {END}.**.** Thi
29460 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62  s routine must b
29470 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68  e called from th
29480 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e  e same thread in
29490 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 61 70   which.** the ap
294a0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
294b0 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75  d function is ru
294c0 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  nning..**.** Req
294d0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
294e0 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45  16243].*/.SQLITE
294f0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
29500 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c  e3_user_data(sql
29510 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
29520 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
29530 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63   Database Connec
29540 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69 6f  tion For Functio
29550 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36 30  ns {H16250} <S60
29560 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a  600><S20200>.**.
29570 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
29580 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
29590 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
295a0 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a  urns a copy of.*
295b0 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  * the pointer to
295c0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
295d0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65 20  onnection] (the 
295e0 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a 2a  1st parameter).*
295f0 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  * of the [sqlite
29600 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
29610 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c  n()].** and [sql
29620 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
29630 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e  tion16()] routin
29640 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c  es that original
29650 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64  ly.** registered
29660 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
29670 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f   defined functio
29680 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  n..**.** Require
29690 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 35  ments:.** [H1625
296a0 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  3].*/.SQLITE_API
296b0 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65   sqlite3 *sqlite
296c0 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
296d0 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  dle(sqlite3_cont
296e0 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ext*);../*.** CA
296f0 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 6f 6e  PI3REF: Function
29700 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 61 20   Auxiliary Data 
29710 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 30 30  {H16270} <S20200
29720 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  >.**.** The foll
29730 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74 69  owing two functi
29740 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 64 20  ons may be used 
29750 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75  by scalar SQL fu
29760 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 61 73  nctions to.** as
29770 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 74 61  sociate metadata
29780 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 76   with argument v
29790 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 73 61  alues. If the sa
297a0 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73  me value is pass
297b0 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 70 6c  ed to.** multipl
297c0 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66  e invocations of
297d0 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 66 75   the same SQL fu
297e0 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 71 75  nction during qu
297f0 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c 20 75  ery execution, u
29800 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 69 72  nder.** some cir
29810 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 20 61  cumstances the a
29820 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 64 61  ssociated metada
29830 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 65 72  ta may be preser
29840 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a 2a 2a  ved. This may.**
29850 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 65 78   be used, for ex
29860 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 61 20  ample, to add a 
29870 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 73 69  regular-expressi
29880 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 61 6c  on matching scal
29890 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20  ar.** function. 
298a0 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 65 72  The compiled ver
298b0 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 75  sion of the regu
298c0 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69  lar expression i
298d0 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 6d  s stored as.** m
298e0 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 61 74  etadata associat
298f0 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 20  ed with the SQL 
29900 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
29910 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72  the regular expr
29920 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 65 72  ession.** patter
29930 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 64  n.  The compiled
29940 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73   regular express
29950 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 73 65  ion can be reuse
29960 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  d on multiple.**
29970 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20   invocations of 
29980 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f  the same functio
29990 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 72  n so that the or
299a0 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e 20 73  iginal pattern s
299b0 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 6e 6f  tring.** does no
299c0 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 63  t need to be rec
299d0 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 68 20  ompiled on each 
299e0 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  invocation..**.*
299f0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65  * The sqlite3_ge
29a00 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 65  t_auxdata() inte
29a10 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
29a20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d  pointer to the m
29a30 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 6f 63  etadata.** assoc
29a40 69 61 74 65 64 20 62 79 20 74 68 65 20 73 71 6c  iated by the sql
29a50 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
29a60 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  () function with
29a70 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e   the Nth argumen
29a80 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 74 68  t.** value to th
29a90 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
29aa0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20  fined function. 
29ab0 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 20 68  If no metadata h
29ac0 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a 2a 20  as been ever.** 
29ad0 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 68 65  been set for the
29ae0 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 6f 66   Nth argument of
29af0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20 6f   the function, o
29b00 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 70  r if the corresp
29b10 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  onding.** functi
29b20 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 61 73  on parameter has
29b30 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 74   changed since t
29b40 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 61 73  he meta-data was
29b50 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 73 71   set,.** then sq
29b60 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
29b70 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 4e 55  a() returns a NU
29b80 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
29b90 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 65  * The sqlite3_se
29ba0 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 65  t_auxdata() inte
29bb0 72 66 61 63 65 20 73 61 76 65 73 20 74 68 65 20  rface saves the 
29bc0 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f 69 6e  metadata.** poin
29bd0 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 33 72  ted to by its 3r
29be0 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74  d parameter as t
29bf0 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f 72 20  he metadata for 
29c00 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 67 75  the N-th.** argu
29c10 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 70 6c  ment of the appl
29c20 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
29c30 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 73 65  function.  Subse
29c40 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74  quent.** calls t
29c50 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  o sqlite3_get_au
29c60 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 72 65  xdata() might re
29c70 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 2c 20  turn this data, 
29c80 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e 6f 74  if it has.** not
29c90 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 64 2e   been destroyed.
29ca0 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74  .** If it is not
29cb0 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 77 69   NULL, SQLite wi
29cc0 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65  ll invoke the de
29cd0 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 6e 63  structor.** func
29ce0 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 74 68  tion given by th
29cf0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 4th parameter 
29d00 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  to sqlite3_set_a
29d10 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a 20 74  uxdata() on.** t
29d20 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 65 6e  he metadata when
29d30 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
29d40 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61  ng function para
29d50 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a 2a 2a  meter changes.**
29d60 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 51 4c   or when the SQL
29d70 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 6c   statement compl
29d80 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 72 20  etes, whichever 
29d90 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a 2a 0a  comes first..**.
29da0 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 72 65  ** SQLite is fre
29db0 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 64 65  e to call the de
29dc0 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 72 6f  structor and dro
29dd0 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 61 6e  p metadata on an
29de0 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 6f  y.** parameter o
29df0 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e 20 61  f any function a
29e00 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 68 65  t any time.  The
29e10 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 20   only guarantee 
29e20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 64  is that.** the d
29e30 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c 20 62  estructor will b
29e40 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20  e called before 
29e50 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 73 20  the metadata is 
29e60 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  dropped..**.** I
29e70 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 74 61  n practice, meta
29e80 64 61 74 61 20 69 73 20 70 72 65 73 65 72 76 65  data is preserve
29e90 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 74 69  d between functi
29ea0 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a 2a 20  on calls for.** 
29eb0 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74  expressions that
29ec0 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 61 74   are constant at
29ed0 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20 54   compile time. T
29ee0 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c 69 74  his includes lit
29ef0 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 20 61  eral.** values a
29f00 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73  nd SQL variables
29f10 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
29f20 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63  utines must be c
29f30 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73  alled from the s
29f40 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68  ame thread in wh
29f50 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c 20 66  ich.** the SQL f
29f60 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69  unction is runni
29f70 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ng..**.** Requir
29f80 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32  ements:.** [H162
29f90 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b 48 31  72] [H16274] [H1
29fa0 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d 20 5b  6276] [H16277] [
29fb0 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37 39 5d  H16278] [H16279]
29fc0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
29fd0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 65 74  oid *sqlite3_get
29fe0 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33  _auxdata(sqlite3
29ff0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e  _context*, int N
2a000 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2a010 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  id sqlite3_set_a
2a020 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63  uxdata(sqlite3_c
2a030 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e 2c 20  ontext*, int N, 
2a040 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a 29 28  void*, void (*)(
2a050 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a 2a 2a  void*));.../*.**
2a060 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 73 74   CAPI3REF: Const
2a070 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20 53 70  ants Defining Sp
2a080 65 63 69 61 6c 20 44 65 73 74 72 75 63 74 6f 72  ecial Destructor
2a090 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30 32 38   Behavior {H1028
2a0a0 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S30100>.**.*
2a0b0 2a 20 54 68 65 73 65 20 61 72 65 20 73 70 65 63  * These are spec
2a0c0 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 74  ial values for t
2a0d0 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 74 68  he destructor th
2a0e0 61 74 20 69 73 20 70 61 73 73 65 64 20 69 6e 20  at is passed in 
2a0f0 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61 6c 20  as the.** final 
2a100 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f 75 74  argument to rout
2a110 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74  ines like [sqlit
2a120 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29  e3_result_blob()
2a130 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73 74 72  ].  If the destr
2a140 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d 65 6e  uctor.** argumen
2a150 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54 41 54  t is SQLITE_STAT
2a160 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  IC, it means tha
2a170 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 6f  t the content po
2a180 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74 61 6e  inter is constan
2a190 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20 6e 65  t.** and will ne
2a1a0 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49 74 20  ver change.  It 
2a1b0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
2a1c0 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e 20 20   be destroyed.  
2a1d0 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 52  The.** SQLITE_TR
2a1e0 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20 6d 65  ANSIENT value me
2a1f0 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e  ans that the con
2a200 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 6c 79  tent will likely
2a210 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 74 68   change in.** th
2a220 65 20 6e 65 61 72 20 66 75 74 75 72 65 20 61 6e  e near future an
2a230 64 20 74 68 61 74 20 53 51 4c 69 74 65 20 73 68  d that SQLite sh
2a240 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77  ould make its ow
2a250 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f  n private copy o
2a260 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  f.** the content
2a270 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
2a280 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 79 70  g..**.** The typ
2a290 65 64 65 66 20 69 73 20 6e 65 63 65 73 73 61 72  edef is necessar
2a2a0 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64  y to work around
2a2b0 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63 65 72   problems in cer
2a2c0 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f 6d 70  tain.** C++ comp
2a2d0 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69 63 6b  ilers.  See tick
2a2e0 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74 79 70  et #2191..*/.typ
2a2f0 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71 6c 69  edef void (*sqli
2a300 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74  te3_destructor_t
2a310 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23 64 65  ype)(void*);.#de
2a320 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54  fine SQLITE_STAT
2a330 49 43 20 20 20 20 20 20 28 28 73 71 6c 69 74 65  IC      ((sqlite
2a340 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70  3_destructor_typ
2a350 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  e)0).#define SQL
2a360 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 20 20  ITE_TRANSIENT   
2a370 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75  ((sqlite3_destru
2a380 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a 0a 2f  ctor_type)-1)../
2a390 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53  *.** CAPI3REF: S
2a3a0 65 74 74 69 6e 67 20 54 68 65 20 52 65 73 75 6c  etting The Resul
2a3b0 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75 6e 63  t Of An SQL Func
2a3c0 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20 3c 53  tion {H16400} <S
2a3d0 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20200>.**.** The
2a3e0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
2a3f0 75 73 65 64 20 62 79 20 74 68 65 20 78 46 75 6e  used by the xFun
2a400 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61 6c 6c  c or xFinal call
2a410 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20 69 6d  backs that.** im
2a420 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75 6e 63  plement SQL func
2a430 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67  tions and aggreg
2a440 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20 5b 73  ates.  See.** [s
2a450 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
2a460 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73  nction()] and [s
2a470 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
2a480 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 66  nction16()].** f
2a490 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
2a4a0 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
2a4b0 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   These functions
2a4c0 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63 68 20   work very much 
2a4d0 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61 6d 65  like the [parame
2a4e0 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66 61 6d  ter binding] fam
2a4f0 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63 74 69  ily of.** functi
2a500 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69 6e 64  ons used to bind
2a510 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 74 20   values to host 
2a520 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 70 72  parameters in pr
2a530 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2a540 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 74  s..** Refer to t
2a550 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65  he [SQL paramete
2a560 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  r] documentation
2a570 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
2a580 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a  information..**.
2a590 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2a5a0 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69 6e 74  esult_blob() int
2a5b0 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20  erface sets the 
2a5c0 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61  result from.** a
2a5d0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  n application-de
2a5e0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2a5f0 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20 77 68  o be the BLOB wh
2a600 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 70  ose content is p
2a610 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20  ointed.** to by 
2a620 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
2a630 65 74 65 72 20 61 6e 64 20 77 68 69 63 68 20 69  eter and which i
2a640 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 20 77  s N bytes long w
2a650 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a  here N is the.**
2a660 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
2a670 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2a680 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62  te3_result_zerob
2a690 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 73  lob() interfaces
2a6a0 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20   set the result 
2a6b0 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63  of.** the applic
2a6c0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2a6d0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 42  nction to be a B
2a6e0 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  LOB containing a
2a6f0 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74 65 73  ll zero.** bytes
2a700 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69 6e 20   and N bytes in 
2a710 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20 69 73  size, where N is
2a720 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
2a730 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  e 2nd parameter.
2a740 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2a750 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
2a760 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74  () interface set
2a770 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f  s the result fro
2a780 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74  m.** an applicat
2a790 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2a7a0 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66 6c 6f  tion to be a flo
2a7b0 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
2a7c0 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62  e specified.** b
2a7d0 79 20 69 74 73 20 32 6e 64 20 61 72 67 75 6d 65  y its 2nd argume
2a7e0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  nt..**.** The sq
2a7f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2a800 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  or() and sqlite3
2a810 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
2a820 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 63  ) functions.** c
2a830 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65 6d 65  ause the impleme
2a840 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f  nted SQL functio
2a850 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 78  n to throw an ex
2a860 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51 4c 69  ception..** SQLi
2a870 74 65 20 75 73 65 73 20 74 68 65 20 73 74 72 69  te uses the stri
2a880 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ng pointed to by
2a890 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61 72 61   the.** 2nd para
2a8a0 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 65 33  meter of sqlite3
2a8b0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20  _result_error() 
2a8c0 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  or sqlite3_resul
2a8d0 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 61  t_error16().** a
2a8e0 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 6e  s the text of an
2a8f0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 20   error message. 
2a900 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70 72 65   SQLite interpre
2a910 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20  ts the error.** 
2a920 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 66  message string f
2a930 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75  rom sqlite3_resu
2a940 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20 55 54  lt_error() as UT
2a950 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a 20 69  F-8. SQLite.** i
2a960 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 73 74  nterprets the st
2a970 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65  ring from sqlite
2a980 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
2a990 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20  () as UTF-16 in 
2a9a0 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 20 6f  native.** byte o
2a9b0 72 64 65 72 2e 20 20 49 66 20 74 68 65 20 74 68  rder.  If the th
2a9c0 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ird parameter to
2a9d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2a9e0 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 71  error().** or sq
2a9f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2aa00 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 74 69  or16() is negati
2aa10 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74  ve then SQLite t
2aa20 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 72 6f  akes as the erro
2aa30 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 6c 6c  r.** message all
2aa40 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 67 68   text up through
2aa50 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20   the first zero 
2aa60 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66  character..** If
2aa70 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   the third param
2aa80 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  eter to sqlite3_
2aa90 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 6f  result_error() o
2aaa0 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73  r.** sqlite3_res
2aab0 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73  ult_error16() is
2aac0 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 74 68   non-negative th
2aad0 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20  en SQLite takes 
2aae0 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62 79 74  that many.** byt
2aaf0 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65  es (not characte
2ab00 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32 6e 64  rs) from the 2nd
2ab10 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 74 68   parameter as th
2ab20 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e  e error message.
2ab30 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2ab40 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61  result_error() a
2ab50 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  nd sqlite3_resul
2ab60 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 72  t_error16().** r
2ab70 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61 20 70  outines make a p
2ab80 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74  rivate copy of t
2ab90 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
2aba0 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a 2a 20   text before.** 
2abb0 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20 48 65  they return.  He
2abc0 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67  nce, the calling
2abd0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65   function can de
2abe0 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a 20 6d  allocate or.** m
2abf0 6f 64 69 66 79 20 74 68 65 20 74 65 78 74 20 61  odify the text a
2ac00 66 74 65 72 20 74 68 65 79 20 72 65 74 75 72 6e  fter they return
2ac10 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a   without harm..*
2ac20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2ac30 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28  sult_error_code(
2ac40 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e 67  ) function chang
2ac50 65 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  es the error cod
2ac60 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79  e.** returned by
2ac70 20 53 51 4c 69 74 65 20 61 73 20 61 20 72 65 73   SQLite as a res
2ac80 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20  ult of an error 
2ac90 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20  in a function.  
2aca0 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20 74  By default,.** t
2acb0 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  he error code is
2acc0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20   SQLITE_ERROR.  
2acd0 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  A subsequent cal
2ace0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73  l to sqlite3_res
2acf0 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f  ult_error().** o
2ad00 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  r sqlite3_result
2ad10 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73 65 74  _error16() reset
2ad20 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  s the error code
2ad30 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   to SQLITE_ERROR
2ad40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2ad50 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f 62 69  te3_result_toobi
2ad60 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61  g() interface ca
2ad70 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74  uses SQLite to t
2ad80 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a  hrow an error.**
2ad90 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
2ada0 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f   a string or BLO
2adb0 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74 6f 20  B is to long to 
2adc0 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a  represent..**.**
2add0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
2ade0 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e 74 65  ult_nomem() inte
2adf0 72 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c  rface causes SQL
2ae00 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20  ite to throw an 
2ae10 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74  error.** indicat
2ae20 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d 6f 72  ing that a memor
2ae30 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  y allocation fai
2ae40 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  led..**.** The s
2ae50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
2ae60 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  t() interface se
2ae70 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ts the return va
2ae80 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70  lue.** of the ap
2ae90 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2aea0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
2aeb0 20 74 68 65 20 33 32 2d 62 69 74 20 73 69 67 6e   the 32-bit sign
2aec0 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61  ed integer.** va
2aed0 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65  lue given in the
2aee0 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   2nd argument..*
2aef0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2af00 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 6e 74  sult_int64() int
2af10 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20  erface sets the 
2af20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
2af30 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  of the applicati
2af40 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
2af50 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 36 34  ion to be the 64
2af60 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
2af70 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76  ger.** value giv
2af80 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72  en in the 2nd ar
2af90 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
2afa0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2afb0 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66 61 63  _null() interfac
2afc0 65 20 73 65 74 73 20 74 68 65 20 72 65 74 75 72  e sets the retur
2afd0 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68  n value.** of th
2afe0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
2aff0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2b000 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  o be NULL..**.**
2b010 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
2b020 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71 6c 69  ult_text(), sqli
2b030 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
2b040 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  6(),.** sqlite3_
2b050 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28  result_text16le(
2b060 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72  ), and sqlite3_r
2b070 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 29  esult_text16be()
2b080 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 73   interfaces.** s
2b090 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  et the return va
2b0a0 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70 6c 69  lue of the appli
2b0b0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
2b0c0 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a  unction to be.**
2b0d0 20 61 20 74 65 78 74 20 73 74 72 69 6e 67 20 77   a text string w
2b0e0 68 69 63 68 20 69 73 20 72 65 70 72 65 73 65 6e  hich is represen
2b0f0 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20 55 54  ted as UTF-8, UT
2b100 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65  F-16 native byte
2b110 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46 2d 31   order,.** UTF-1
2b120 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c  6 little endian,
2b130 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 20 65   or UTF-16 big e
2b140 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74 69 76  ndian, respectiv
2b150 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 74  ely..** SQLite t
2b160 61 6b 65 73 20 74 68 65 20 74 65 78 74 20 72 65  akes the text re
2b170 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20 61 70  sult from the ap
2b180 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a  plication from.*
2b190 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65  * the 2nd parame
2b1a0 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ter of the sqlit
2b1b0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
2b1c0 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 49  interfaces..** I
2b1d0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65  f the 3rd parame
2b1e0 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ter to the sqlit
2b1f0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
2b200 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73  interfaces.** is
2b210 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20   negative, then 
2b220 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72 65 73  SQLite takes res
2b230 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20 74 68  ult text from th
2b240 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 0a  e 2nd parameter.
2b250 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 66  ** through the f
2b260 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 61 63  irst zero charac
2b270 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33  ter..** If the 3
2b280 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
2b290 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  the sqlite3_resu
2b2a0 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61  lt_text* interfa
2b2b0 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d 6e 65  ces.** is non-ne
2b2c0 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61 73 20  gative, then as 
2b2d0 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f 74 20  many bytes (not 
2b2e0 63 68 61 72 61 63 74 65 72 73 29 20 6f 66 20 74  characters) of t
2b2f0 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69 6e 74  he text.** point
2b300 65 64 20 74 6f 20 62 79 20 74 68 65 20 32 6e 64  ed to by the 2nd
2b310 20 70 61 72 61 6d 65 74 65 72 20 61 72 65 20 74   parameter are t
2b320 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70 70 6c  aken as the appl
2b330 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 0a  ication-defined.
2b340 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 75  ** function resu
2b350 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 74  lt..** If the 4t
2b360 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
2b370 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2b380 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63  t_text* interfac
2b390 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33  es.** or sqlite3
2b3a0 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20  _result_blob is 
2b3b0 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74  a non-NULL point
2b3c0 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20  er, then SQLite 
2b3d0 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20 66 75  calls that.** fu
2b3e0 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20 64 65  nction as the de
2b3f0 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 65 20  structor on the 
2b400 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 65 73  text or BLOB res
2b410 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61 73 0a  ult when it has.
2b420 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e  ** finished usin
2b430 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a  g that result..*
2b440 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72  * If the 4th par
2b450 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
2b460 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2b470 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20 6f 72  t* interfaces or
2b480 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75  .** sqlite3_resu
2b490 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73  lt_blob is the s
2b4a0 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20  pecial constant 
2b4b0 53 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20 74  SQLITE_STATIC, t
2b4c0 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 61 73  hen SQLite.** as
2b4d0 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 74  sumes that the t
2b4e0 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 65 73 75  ext or BLOB resu
2b4f0 6c 74 20 69 73 20 69 6e 20 63 6f 6e 73 74 61 6e  lt is in constan
2b500 74 20 73 70 61 63 65 20 61 6e 64 20 64 6f 65 73  t space and does
2b510 20 6e 6f 74 0a 2a 2a 20 63 6f 70 79 20 74 68 65   not.** copy the
2b520 20 69 74 20 6f 72 20 63 61 6c 6c 20 61 20 64 65   it or call a de
2b530 73 74 72 75 63 74 6f 72 20 77 68 65 6e 20 69 74  structor when it
2b540 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73   has finished us
2b550 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e  ing that result.
2b560 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70  .** If the 4th p
2b570 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
2b580 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2b590 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a  ext* interfaces.
2b5a0 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65  ** or sqlite3_re
2b5b0 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65  sult_blob is the
2b5c0 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e   special constan
2b5d0 74 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  t SQLITE_TRANSIE
2b5e0 4e 54 0a 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74  NT.** then SQLit
2b5f0 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f  e makes a copy o
2b600 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74  f the result int
2b610 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64  o space obtained
2b620 20 66 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73   from.** from [s
2b630 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
2b640 20 62 65 66 6f 72 65 20 69 74 20 72 65 74 75 72   before it retur
2b650 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ns..**.** The sq
2b660 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
2b670 75 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73  ue() interface s
2b680 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f  ets the result o
2b690 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61  f.** the applica
2b6a0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
2b6b0 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f  ction to be a co
2b6c0 70 79 20 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f  py the.** [unpro
2b6d0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
2b6e0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 73 70 65  alue] object spe
2b6f0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 32 6e  cified by the 2n
2b700 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68  d parameter.  Th
2b710 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73  e.** sqlite3_res
2b720 75 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65  ult_value() inte
2b730 72 66 61 63 65 20 6d 61 6b 65 73 20 61 20 63 6f  rface makes a co
2b740 70 79 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  py of the [sqlit
2b750 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20  e3_value].** so 
2b760 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65  that the [sqlite
2b770 33 5f 76 61 6c 75 65 5d 20 73 70 65 63 69 66 69  3_value] specifi
2b780 65 64 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65  ed in the parame
2b790 74 65 72 20 6d 61 79 20 63 68 61 6e 67 65 20 6f  ter may change o
2b7a0 72 0a 2a 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61  r.** be dealloca
2b7b0 74 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65  ted after sqlite
2b7c0 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29  3_result_value()
2b7d0 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74   returns without
2b7e0 20 68 61 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f   harm..** A [pro
2b7f0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
2b800 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79  alue] object may
2b810 20 61 6c 77 61 79 73 20 62 65 20 75 73 65 64 20   always be used 
2b820 77 68 65 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70  where an.** [unp
2b830 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
2b840 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69  _value] object i
2b850 73 20 72 65 71 75 69 72 65 64 2c 20 73 6f 20 65  s required, so e
2b860 69 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66  ither.** kind of
2b870 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d   [sqlite3_value]
2b880 20 6f 62 6a 65 63 74 20 63 61 6e 20 62 65 20 75   object can be u
2b890 73 65 64 20 77 69 74 68 20 74 68 69 73 20 69 6e  sed with this in
2b8a0 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49  terface..**.** I
2b8b0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
2b8c0 20 61 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d   are called from
2b8d0 20 77 69 74 68 69 6e 20 74 68 65 20 64 69 66 66   within the diff
2b8e0 65 72 65 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20  erent thread.** 
2b8f0 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e  than the one con
2b900 74 61 69 6e 69 6e 67 20 74 68 65 20 61 70 70 6c  taining the appl
2b910 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2b920 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 72 65  function that re
2b930 63 65 69 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73  ceived.** the [s
2b940 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20  qlite3_context] 
2b950 70 6f 69 6e 74 65 72 2c 20 74 68 65 20 72 65 73  pointer, the res
2b960 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e  ults are undefin
2b970 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ed..**.** Requir
2b980 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34  ements:.** [H164
2b990 30 33 5d 20 5b 48 31 36 34 30 36 5d 20 5b 48 31  03] [H16406] [H1
2b9a0 36 34 30 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b  6409] [H16412] [
2b9b0 48 31 36 34 31 35 5d 20 5b 48 31 36 34 31 38 5d  H16415] [H16418]
2b9c0 20 5b 48 31 36 34 32 31 5d 20 5b 48 31 36 34 32   [H16421] [H1642
2b9d0 34 5d 0a 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b  4].** [H16427] [
2b9e0 48 31 36 34 33 30 5d 20 5b 48 31 36 34 33 33 5d  H16430] [H16433]
2b9f0 20 5b 48 31 36 34 33 36 5d 20 5b 48 31 36 34 33   [H16436] [H1643
2ba00 39 5d 20 5b 48 31 36 34 34 32 5d 20 5b 48 31 36  9] [H16442] [H16
2ba10 34 34 35 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a  445] [H16448].**
2ba20 20 5b 48 31 36 34 35 31 5d 20 5b 48 31 36 34 35   [H16451] [H1645
2ba30 34 5d 20 5b 48 31 36 34 35 37 5d 20 5b 48 31 36  4] [H16457] [H16
2ba40 34 36 30 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f  460] [H16463].*/
2ba50 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2ba60 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ba70 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e  blob(sqlite3_con
2ba80 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  text*, const voi
2ba90 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29  d*, int, void(*)
2baa0 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45  (void*));.SQLITE
2bab0 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
2bac0 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28  3_result_double(
2bad0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2bae0 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54  , double);.SQLIT
2baf0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2bb00 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2bb10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2bb20 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69  , const char*, i
2bb30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
2bb40 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
2bb50 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69  ult_error16(sqli
2bb60 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
2bb70 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b  nst void*, int);
2bb80 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2bb90 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2bba0 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c  error_toobig(sql
2bbb0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
2bbc0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2bbd0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2bbe0 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74  rror_nomem(sqlit
2bbf0 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51  e3_context*);.SQ
2bc00 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2bc10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2bc20 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f  or_code(sqlite3_
2bc30 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a  context*, int);.
2bc40 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2bc50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
2bc60 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nt(sqlite3_conte
2bc70 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  xt*, int);.SQLIT
2bc80 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2bc90 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
2bca0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2bcb0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  , sqlite3_int64)
2bcc0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
2bcd0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
2bce0 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f  _null(sqlite3_co
2bcf0 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  ntext*);.SQLITE_
2bd00 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2bd10 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c  _result_text(sql
2bd20 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63  ite3_context*, c
2bd30 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c  onst char*, int,
2bd40 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29   void(*)(void*))
2bd50 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
2bd60 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
2bd70 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f  _text16(sqlite3_
2bd80 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  context*, const 
2bd90 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  void*, int, void
2bda0 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
2bdb0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2bdc0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2bdd0 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  16le(sqlite3_con
2bde0 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  text*, const voi
2bdf0 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28  d*, int,void(*)(
2be00 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  void*));.SQLITE_
2be10 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2be20 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
2be30 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2be40 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
2be50 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
2be60 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *));.SQLITE_API 
2be70 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
2be80 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65  ult_value(sqlite
2be90 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69  3_context*, sqli
2bea0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
2beb0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2bec0 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f  ite3_result_zero
2bed0 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e  blob(sqlite3_con
2bee0 74 65 78 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a  text*, int n);..
2bef0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2bf00 44 65 66 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61  Define New Colla
2bf10 74 69 6e 67 20 53 65 71 75 65 6e 63 65 73 20 7b  ting Sequences {
2bf20 48 31 36 36 30 30 7d 20 3c 53 32 30 33 30 30 3e  H16600} <S20300>
2bf30 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e  .**.** These fun
2bf40 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20  ctions are used 
2bf50 74 6f 20 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61  to add new colla
2bf60 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 74  tion sequences t
2bf70 6f 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61  o the.** [databa
2bf80 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73  se connection] s
2bf90 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20  pecified as the 
2bfa0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a  first argument..
2bfb0 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f  **.** The name o
2bfc0 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74  f the new collat
2bfd0 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20  ion sequence is 
2bfe0 73 70 65 63 69 66 69 65 64 20 61 73 20 61 20 55  specified as a U
2bff0 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66  TF-8 string.** f
2c000 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  or sqlite3_creat
2c010 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e  e_collation() an
2c020 64 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  d sqlite3_create
2c030 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a  _collation_v2().
2c040 2a 2a 20 61 6e 64 20 61 20 55 54 46 2d 31 36 20  ** and a UTF-16 
2c050 73 74 72 69 6e 67 20 66 6f 72 20 73 71 6c 69 74  string for sqlit
2c060 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2c070 69 6f 6e 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20  ion16(). In all 
2c080 63 61 73 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d  cases.** the nam
2c090 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  e is passed as t
2c0a0 68 65 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69  he second functi
2c0b0 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  on argument..**.
2c0c0 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67  ** The third arg
2c0d0 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65  ument may be one
2c0e0 20 6f 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74   of the constant
2c0f0 73 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c  s [SQLITE_UTF8],
2c100 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 31  .** [SQLITE_UTF1
2c110 36 4c 45 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45  6LE], or [SQLITE
2c120 5f 55 54 46 31 36 42 45 5d 2c 20 69 6e 64 69 63  _UTF16BE], indic
2c130 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 75  ating that the u
2c140 73 65 72 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20  ser-supplied.** 
2c150 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73 20  routine expects 
2c160 74 6f 20 62 65 20 70 61 73 73 65 64 20 70 6f 69  to be passed poi
2c170 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73  nters to strings
2c180 20 65 6e 63 6f 64 65 64 20 75 73 69 6e 67 20 55   encoded using U
2c190 54 46 2d 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20  TF-8,.** UTF-16 
2c1a0 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f  little-endian, o
2c1b0 72 20 55 54 46 2d 31 36 20 62 69 67 2d 65 6e 64  r UTF-16 big-end
2c1c0 69 61 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c  ian, respectivel
2c1d0 79 2e 20 54 68 65 0a 2a 2a 20 74 68 69 72 64 20  y. The.** third 
2c1e0 61 72 67 75 6d 65 6e 74 20 6d 69 67 68 74 20 61  argument might a
2c1f0 6c 73 6f 20 62 65 20 5b 53 51 4c 49 54 45 5f 55  lso be [SQLITE_U
2c200 54 46 31 36 5d 20 74 6f 20 69 6e 64 69 63 61 74  TF16] to indicat
2c210 65 20 74 68 61 74 20 74 68 65 20 72 6f 75 74 69  e that the routi
2c220 6e 65 0a 2a 2a 20 65 78 70 65 63 74 73 20 70 6f  ne.** expects po
2c230 69 6e 74 65 72 73 20 74 6f 20 62 65 20 55 54 46  inters to be UTF
2c240 2d 31 36 20 73 74 72 69 6e 67 73 20 69 6e 20 74  -16 strings in t
2c250 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  he native byte o
2c260 72 64 65 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20  rder, or the.** 
2c270 61 72 67 75 6d 65 6e 74 20 63 61 6e 20 62 65 20  argument can be 
2c280 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  [SQLITE_UTF16_AL
2c290 49 47 4e 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a  IGNED] if the.**
2c2a0 20 74 68 65 20 72 6f 75 74 69 6e 65 20 65 78 70   the routine exp
2c2b0 65 63 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f  ects pointers to
2c2c0 20 31 36 2d 62 69 74 20 77 6f 72 64 20 61 6c 69   16-bit word ali
2c2d0 67 6e 65 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20  gned strings.** 
2c2e0 6f 66 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65  of UTF-16 in the
2c2f0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
2c300 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e  er..**.** A poin
2c310 74 65 72 20 74 6f 20 74 68 65 20 75 73 65 72 20  ter to the user 
2c320 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65  supplied routine
2c330 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 64 20   must be passed 
2c340 61 73 20 74 68 65 20 66 69 66 74 68 0a 2a 2a 20  as the fifth.** 
2c350 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 74  argument.  If it
2c360 20 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69   is NULL, this i
2c370 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 64 65  s the same as de
2c380 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61  leting the colla
2c390 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65  tion.** sequence
2c3a0 20 28 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65   (so that SQLite
2c3b0 20 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20   cannot call it 
2c3c0 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63  anymore)..** Eac
2c3d0 68 20 74 69 6d 65 20 74 68 65 20 61 70 70 6c 69  h time the appli
2c3e0 63 61 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 20  cation supplied 
2c3f0 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f  function is invo
2c400 6b 65 64 2c 20 69 74 20 69 73 20 70 61 73 73 65  ked, it is passe
2c410 64 0a 2a 2a 20 61 73 20 69 74 73 20 66 69 72 73  d.** as its firs
2c420 74 20 70 61 72 61 6d 65 74 65 72 20 61 20 63 6f  t parameter a co
2c430 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20  py of the void* 
2c440 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 6f  passed as the fo
2c450 75 72 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  urth argument.**
2c460 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61   to sqlite3_crea
2c470 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f  te_collation() o
2c480 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  r sqlite3_create
2c490 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a  _collation16()..
2c4a0 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e  **.** The remain
2c4b0 69 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  ing arguments to
2c4c0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2c4d0 2d 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e  -supplied routin
2c4e0 65 20 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67  e are two string
2c4f0 73 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 65  s,.** each repre
2c500 73 65 6e 74 65 64 20 62 79 20 61 20 28 6c 65 6e  sented by a (len
2c510 67 74 68 2c 20 64 61 74 61 29 20 70 61 69 72 20  gth, data) pair 
2c520 61 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 74  and encoded in t
2c530 68 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74  he encoding.** t
2c540 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20 61  hat was passed a
2c550 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75  s the third argu
2c560 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 63 6f  ment when the co
2c570 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2c580 20 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72   was.** register
2c590 65 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 61  ed. {END}  The a
2c5a0 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e  pplication defin
2c5b0 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75  ed collation rou
2c5c0 74 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72  tine should.** r
2c5d0 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20  eturn negative, 
2c5e0 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65  zero or positive
2c5f0 20 69 66 20 74 68 65 20 66 69 72 73 74 20 73 74   if the first st
2c600 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68 61  ring is less tha
2c610 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20  n,.** equal to, 
2c620 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  or greater than 
2c630 74 68 65 20 73 65 63 6f 6e 64 20 73 74 72 69 6e  the second strin
2c640 67 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 31  g. i.e. (STRING1
2c650 20 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a   - STRING2)..**.
2c660 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
2c670 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
2c680 76 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20  v2() works like 
2c690 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2c6a0 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78  ollation().** ex
2c6b0 63 65 70 74 20 74 68 61 74 20 69 74 20 74 61 6b  cept that it tak
2c6c0 65 73 20 61 6e 20 65 78 74 72 61 20 61 72 67 75  es an extra argu
2c6d0 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20  ment which is a 
2c6e0 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a  destructor for.*
2c6f0 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e  * the collation.
2c700 20 20 54 68 65 20 64 65 73 74 72 75 63 74 6f 72    The destructor
2c710 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
2c720 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73  the collation is
2c730 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 61 6e  .** destroyed an
2c740 64 20 69 73 20 70 61 73 73 65 64 20 61 20 63 6f  d is passed a co
2c750 70 79 20 6f 66 20 74 68 65 20 66 6f 75 72 74 68  py of the fourth
2c760 20 70 61 72 61 6d 65 74 65 72 20 76 6f 69 64 2a   parameter void*
2c770 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74   pointer.** of t
2c780 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  he sqlite3_creat
2c790 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29  e_collation_v2()
2c7a0 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20  ..** Collations 
2c7b0 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 77 68  are destroyed wh
2c7c0 65 6e 20 74 68 65 79 20 61 72 65 20 6f 76 65 72  en they are over
2c7d0 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65 72 20  ridden by later 
2c7e0 63 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20  calls to the.** 
2c7f0 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69  collation creati
2c800 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20  on functions or 
2c810 77 68 65 6e 20 74 68 65 20 5b 64 61 74 61 62 61  when the [databa
2c820 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69  se connection] i
2c830 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e  s closed.** usin
2c840 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  g [sqlite3_close
2c850 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ()]..**.** See a
2c860 6c 73 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63  lso:  [sqlite3_c
2c870 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28  ollation_needed(
2c880 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
2c890 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
2c8a0 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  16()]..**.** Req
2c8b0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
2c8c0 31 36 36 30 33 5d 20 5b 48 31 36 36 30 34 5d 20  16603] [H16604] 
2c8d0 5b 48 31 36 36 30 36 5d 20 5b 48 31 36 36 30 39  [H16606] [H16609
2c8e0 5d 20 5b 48 31 36 36 31 32 5d 20 5b 48 31 36 36  ] [H16612] [H166
2c8f0 31 35 5d 20 5b 48 31 36 36 31 38 5d 20 5b 48 31  15] [H16618] [H1
2c900 36 36 32 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34  6621].** [H16624
2c910 5d 20 5b 48 31 36 36 32 37 5d 20 5b 48 31 36 36  ] [H16627] [H166
2c920 33 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  30].*/.SQLITE_AP
2c930 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
2c940 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a  eate_collation(.
2c950 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63    sqlite3*, .  c
2c960 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2c970 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65  , .  int eTextRe
2c980 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69  p, .  void*,.  i
2c990 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
2c9a0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
2c9b0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
2c9c0 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  id*).);.SQLITE_A
2c9d0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
2c9e0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
2c9f0 76 32 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  v2(.  sqlite3*, 
2ca00 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2ca10 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65  Name, .  int eTe
2ca20 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c  xtRep, .  void*,
2ca30 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65  .  int(*xCompare
2ca40 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
2ca50 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
2ca60 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64  t void*),.  void
2ca70 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64  (*xDestroy)(void
2ca80 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  *).);.SQLITE_API
2ca90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
2caa0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28  ate_collation16(
2cab0 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20  .  sqlite3*, .  
2cac0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d  const void *zNam
2cad0 65 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65  e,.  int eTextRe
2cae0 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69  p, .  void*,.  i
2caf0 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
2cb00 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
2cb10 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
2cb20 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  id*).);../*.** C
2cb30 41 50 49 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69  API3REF: Collati
2cb40 6f 6e 20 4e 65 65 64 65 64 20 43 61 6c 6c 62 61  on Needed Callba
2cb50 63 6b 73 20 7b 48 31 36 37 30 30 7d 20 3c 53 32  cks {H16700} <S2
2cb60 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61  0300>.**.** To a
2cb70 76 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20 72  void having to r
2cb80 65 67 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c  egister all coll
2cb90 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20  ation sequences 
2cba0 62 65 66 6f 72 65 20 61 20 64 61 74 61 62 61 73  before a databas
2cbb0 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64  e.** can be used
2cbc0 2c 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62  , a single callb
2cbd0 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  ack function may
2cbe0 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 77   be registered w
2cbf0 69 74 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61  ith the.** [data
2cc00 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
2cc10 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 77 68   to be called wh
2cc20 65 6e 65 76 65 72 20 61 6e 20 75 6e 64 65 66 69  enever an undefi
2cc30 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a  ned collation.**
2cc40 20 73 65 71 75 65 6e 63 65 20 69 73 20 72 65 71   sequence is req
2cc50 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  uired..**.** If 
2cc60 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
2cc70 72 65 67 69 73 74 65 72 65 64 20 75 73 69 6e 67  registered using
2cc80 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c   the sqlite3_col
2cc90 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20  lation_needed() 
2cca0 41 50 49 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20  API,.** then it 
2ccb0 69 73 20 70 61 73 73 65 64 20 74 68 65 20 6e 61  is passed the na
2ccc0 6d 65 73 20 6f 66 20 75 6e 64 65 66 69 6e 65 64  mes of undefined
2ccd0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2cce0 6e 63 65 73 20 61 73 20 73 74 72 69 6e 67 73 0a  nces as strings.
2ccf0 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54  ** encoded in UT
2cd00 46 2d 38 2e 20 7b 48 31 36 37 30 33 7d 20 49 66  F-8. {H16703} If
2cd10 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
2cd20 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29 20 69 73  on_needed16() is
2cd30 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61   used,.** the na
2cd40 6d 65 73 20 61 72 65 20 70 61 73 73 65 64 20 61  mes are passed a
2cd50 73 20 55 54 46 2d 31 36 20 69 6e 20 6d 61 63 68  s UTF-16 in mach
2cd60 69 6e 65 20 6e 61 74 69 76 65 20 62 79 74 65 20  ine native byte 
2cd70 6f 72 64 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c  order..** A call
2cd80 20 74 6f 20 65 69 74 68 65 72 20 66 75 6e 63 74   to either funct
2cd90 69 6f 6e 20 72 65 70 6c 61 63 65 73 20 61 6e 79  ion replaces any
2cda0 20 65 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 61   existing callba
2cdb0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74  ck..**.** When t
2cdc0 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  he callback is i
2cdd0 6e 76 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73  nvoked, the firs
2cde0 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65  t argument passe
2cdf0 64 20 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f  d is a copy.** o
2ce00 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
2ce10 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
2ce20 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
2ce30 64 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  d() or.** sqlite
2ce40 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
2ce50 65 64 31 36 28 29 2e 20 20 54 68 65 20 73 65 63  ed16().  The sec
2ce60 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
2ce70 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
2ce80 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65  connection.  The
2ce90 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20   third argument 
2cea0 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54  is one of [SQLIT
2ceb0 45 5f 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45  E_UTF8], [SQLITE
2cec0 5f 55 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72  _UTF16BE],.** or
2ced0 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45   [SQLITE_UTF16LE
2cee0 5d 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  ], indicating th
2cef0 65 20 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65  e most desirable
2cf00 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c   form of the col
2cf10 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e  lation.** sequen
2cf20 63 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75  ce function requ
2cf30 69 72 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74  ired.  The fourt
2cf40 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  h parameter is t
2cf50 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a  he name of the.*
2cf60 2a 20 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61  * required colla
2cf70 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a  tion sequence..*
2cf80 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63  *.** The callbac
2cf90 6b 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  k function shoul
2cfa0 64 20 72 65 67 69 73 74 65 72 20 74 68 65 20 64  d register the d
2cfb0 65 73 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  esired collation
2cfc0 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74   using.** [sqlit
2cfd0 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2cfe0 69 6f 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  ion()], [sqlite3
2cff0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2d000 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73  n16()], or.** [s
2d010 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2d020 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a  llation_v2()]..*
2d030 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2d040 73 3a 0a 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b  s:.** [H16702] [
2d050 48 31 36 37 30 34 5d 20 5b 48 31 36 37 30 36 5d  H16704] [H16706]
2d060 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2d070 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  nt sqlite3_colla
2d080 74 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73  tion_needed(.  s
2d090 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64  qlite3*, .  void
2d0a0 2a 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f  *, .  void(*)(vo
2d0b0 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
2d0c0 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
2d0d0 63 68 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45  char*).);.SQLITE
2d0e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2d0f0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
2d100 64 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c  d16(.  sqlite3*,
2d110 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69   .  void*,.  voi
2d120 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  d(*)(void*,sqlit
2d130 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
2d140 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b  ,const void*).);
2d150 0a 0a 2f 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20  ../*.** Specify 
2d160 74 68 65 20 6b 65 79 20 66 6f 72 20 61 6e 20 65  the key for an e
2d170 6e 63 72 79 70 74 65 64 20 64 61 74 61 62 61 73  ncrypted databas
2d180 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
2d190 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61   should be.** ca
2d1a0 6c 6c 65 64 20 72 69 67 68 74 20 61 66 74 65 72  lled right after
2d1b0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e   sqlite3_open().
2d1c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
2d1d0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69  to implement thi
2d1e0 73 20 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61  s API is not ava
2d1f0 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75  ilable in the pu
2d200 62 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20  blic release.** 
2d210 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51  of SQLite..*/.SQ
2d220 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2d230 69 74 65 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69  ite3_key(.  sqli
2d240 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
2d250 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2d260 74 61 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b  tabase to be rek
2d270 65 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  eyed */.  const 
2d280 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
2d290 6e 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20  nKey     /* The 
2d2a0 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  key */.);../*.**
2d2b0 20 43 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20   Change the key 
2d2c0 6f 6e 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62  on an open datab
2d2d0 61 73 65 2e 20 20 49 66 20 74 68 65 20 63 75 72  ase.  If the cur
2d2e0 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 69 73  rent database is
2d2f0 20 6e 6f 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65   not.** encrypte
2d300 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  d, this routine 
2d310 77 69 6c 6c 20 65 6e 63 72 79 70 74 20 69 74 2e  will encrypt it.
2d320 20 20 49 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20    If pNew==0 or 
2d330 6e 4e 65 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20  nNew==0, the.** 
2d340 64 61 74 61 62 61 73 65 20 69 73 20 64 65 63 72  database is decr
2d350 79 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ypted..**.** The
2d360 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
2d370 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e  nt this API is n
2d380 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  ot available in 
2d390 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61  the public relea
2d3a0 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e  se.** of SQLite.
2d3b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2d3c0 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79  nt sqlite3_rekey
2d3d0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2d3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d3f0 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74     /* Database t
2d400 6f 20 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a  o be rekeyed */.
2d410 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b    const void *pK
2d420 65 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20  ey, int nKey    
2d430 20 2f 2a 20 54 68 65 20 6e 65 77 20 6b 65 79 20   /* The new key 
2d440 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.);../*.** CAP
2d450 49 33 52 45 46 3a 20 53 75 73 70 65 6e 64 20 45  I3REF: Suspend E
2d460 78 65 63 75 74 69 6f 6e 20 46 6f 72 20 41 20 53  xecution For A S
2d470 68 6f 72 74 20 54 69 6d 65 20 7b 48 31 30 35 33  hort Time {H1053
2d480 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a  0} <S40410>.**.*
2d490 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c  * The sqlite3_sl
2d4a0 65 65 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63  eep() function c
2d4b0 61 75 73 65 73 20 74 68 65 20 63 75 72 72 65 6e  auses the curren
2d4c0 74 20 74 68 72 65 61 64 20 74 6f 20 73 75 73 70  t thread to susp
2d4d0 65 6e 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a  end execution.**
2d4e0 20 66 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20   for at least a 
2d4f0 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73  number of millis
2d500 65 63 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64  econds specified
2d510 20 69 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65   in its paramete
2d520 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
2d530 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
2d540 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72   does not suppor
2d550 74 20 73 6c 65 65 70 20 72 65 71 75 65 73 74 73  t sleep requests
2d560 20 77 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65   with.** millise
2d570 63 6f 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75  cond time resolu
2d580 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74  tion, then the t
2d590 69 6d 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e  ime will be roun
2d5a0 64 65 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65  ded up to.** the
2d5b0 20 6e 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e   nearest second.
2d5c0 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   The number of m
2d5d0 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73  illiseconds of s
2d5e0 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a  leep actually.**
2d5f0 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20   requested from 
2d600 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
2d610 73 74 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64  stem is returned
2d620 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69  ..**.** SQLite i
2d630 6d 70 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 69  mplements this i
2d640 6e 74 65 72 66 61 63 65 20 62 79 20 63 61 6c 6c  nterface by call
2d650 69 6e 67 20 74 68 65 20 78 53 6c 65 65 70 28 29  ing the xSleep()
2d660 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  .** method of th
2d670 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74  e default [sqlit
2d680 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a  e3_vfs] object..
2d690 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
2d6a0 74 73 3a 20 5b 48 31 30 35 33 33 5d 20 5b 48 31  ts: [H10533] [H1
2d6b0 30 35 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  0536].*/.SQLITE_
2d6c0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2d6d0 73 6c 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a  sleep(int);../*.
2d6e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d  ** CAPI3REF: Nam
2d6f0 65 20 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20  e Of The Folder 
2d700 48 6f 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72  Holding Temporar
2d710 79 20 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d  y Files {H10310}
2d720 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20000>.**.** 
2d730 49 66 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76  If this global v
2d740 61 72 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20  ariable is made 
2d750 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74  to point to a st
2d760 72 69 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a  ring which is.**
2d770 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66   the name of a f
2d780 6f 6c 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69  older (a.k.a. di
2d790 72 65 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61  rectory), then a
2d7a0 6c 6c 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  ll temporary fil
2d7b0 65 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79  es.** created by
2d7c0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20   SQLite will be 
2d7d0 70 6c 61 63 65 64 20 69 6e 20 74 68 61 74 20 64  placed in that d
2d7e0 69 72 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68  irectory.  If th
2d7f0 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69  is variable.** i
2d800 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
2d810 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65  , then SQLite pe
2d820 72 66 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20  rforms a search 
2d830 66 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61  for an appropria
2d840 74 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20  te.** temporary 
2d850 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a  file directory..
2d860 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20  **.** It is not 
2d870 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20  safe to read or 
2d880 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 72 69  modify this vari
2d890 61 62 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 61  able in more tha
2d8a0 6e 20 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20  n one.** thread 
2d8b0 61 74 20 61 20 74 69 6d 65 2e 20 20 49 74 20 69  at a time.  It i
2d8c0 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65  s not safe to re
2d8d0 61 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69  ad or modify thi
2d8e0 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66  s variable.** if
2d8f0 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   a [database con
2d900 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e  nection] is bein
2d910 67 20 75 73 65 64 20 61 74 20 74 68 65 20 73 61  g used at the sa
2d920 6d 65 20 74 69 6d 65 20 69 6e 20 61 20 73 65 70  me time in a sep
2d930 61 72 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e  arate.** thread.
2d940 0a 2a 2a 20 49 74 20 69 73 20 69 6e 74 65 6e 64  .** It is intend
2d950 65 64 20 74 68 61 74 20 74 68 69 73 20 76 61 72  ed that this var
2d960 69 61 62 6c 65 20 62 65 20 73 65 74 20 6f 6e 63  iable be set onc
2d970 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20  e.** as part of 
2d980 70 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69  process initiali
2d990 7a 61 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72  zation and befor
2d9a0 65 20 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74  e any SQLite int
2d9b0 65 72 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e  erface.** routin
2d9c0 65 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c  es have been cal
2d9d0 6c 65 64 20 61 6e 64 20 74 68 61 74 20 74 68 69  led and that thi
2d9e0 73 20 76 61 72 69 61 62 6c 65 20 72 65 6d 61 69  s variable remai
2d9f0 6e 20 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74  n unchanged.** t
2da00 68 65 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a  hereafter..**.**
2da10 20 54 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65   The [temp_store
2da20 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d  _directory pragm
2da30 61 5d 20 6d 61 79 20 6d 6f 64 69 66 79 20 74 68  a] may modify th
2da40 69 73 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20  is variable and 
2da50 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70  cause.** it to p
2da60 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f  oint to memory o
2da70 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71  btained from [sq
2da80 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20  lite3_malloc].  
2da90 46 75 72 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20  Furthermore,.** 
2daa0 74 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f  the [temp_store_
2dab0 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61  directory pragma
2dac0 5d 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65 73  ] always assumes
2dad0 20 74 68 61 74 20 61 6e 79 20 73 74 72 69 6e 67   that any string
2dae0 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20 76 61  .** that this va
2daf0 72 69 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f  riable points to
2db00 20 69 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f   is held in memo
2db10 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
2db20 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61   .** [sqlite3_ma
2db30 6c 6c 6f 63 5d 20 61 6e 64 20 74 68 65 20 70 72  lloc] and the pr
2db40 61 67 6d 61 20 6d 61 79 20 61 74 74 65 6d 70 74  agma may attempt
2db50 20 74 6f 20 66 72 65 65 20 74 68 61 74 20 6d 65   to free that me
2db60 6d 6f 72 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73  mory.** using [s
2db70 71 6c 69 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a  qlite3_free]..**
2db80 20 48 65 6e 63 65 2c 20 69 66 20 74 68 69 73 20   Hence, if this 
2db90 76 61 72 69 61 62 6c 65 20 69 73 20 6d 6f 64 69  variable is modi
2dba0 66 69 65 64 20 64 69 72 65 63 74 6c 79 2c 20 65  fied directly, e
2dbb0 69 74 68 65 72 20 69 74 20 73 68 6f 75 6c 64 20  ither it should 
2dbc0 62 65 0a 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20  be.** made NULL 
2dbd0 6f 72 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74  or made to point
2dbe0 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69   to memory obtai
2dbf0 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  ned from [sqlite
2dc00 33 5f 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20  3_malloc].** or 
2dc10 65 6c 73 65 20 74 68 65 20 75 73 65 20 6f 66 20  else the use of 
2dc20 74 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f  the [temp_store_
2dc30 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61  directory pragma
2dc40 5d 20 73 68 6f 75 6c 64 20 62 65 20 61 76 6f 69  ] should be avoi
2dc50 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ded..*/.SQLITE_A
2dc60 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  PI char *sqlite3
2dc70 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b  _temp_directory;
2dc80 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
2dc90 3a 20 54 65 73 74 20 46 6f 72 20 41 75 74 6f 2d  : Test For Auto-
2dca0 43 6f 6d 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32  Commit Mode {H12
2dcb0 39 33 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a  930} <S60200>.**
2dcc0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f   KEYWORDS: {auto
2dcd0 63 6f 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a  commit mode}.**.
2dce0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67  ** The sqlite3_g
2dcf0 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20  et_autocommit() 
2dd00 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
2dd10 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a  s non-zero or.**
2dd20 20 7a 65 72 6f 20 69 66 20 74 68 65 20 67 69 76   zero if the giv
2dd30 65 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  en database conn
2dd40 65 63 74 69 6f 6e 20 69 73 20 6f 72 20 69 73 20  ection is or is 
2dd50 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69  not in autocommi
2dd60 74 20 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65  t mode,.** respe
2dd70 63 74 69 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f  ctively.  Autoco
2dd80 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20  mmit mode is on 
2dd90 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41  by default..** A
2dda0 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69  utocommit mode i
2ddb0 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20  s disabled by a 
2ddc0 5b 42 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e  [BEGIN] statemen
2ddd0 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74  t..** Autocommit
2dde0 20 6d 6f 64 65 20 69 73 20 72 65 2d 65 6e 61 62   mode is re-enab
2ddf0 6c 65 64 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54  led by a [COMMIT
2de00 5d 20 6f 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e  ] or [ROLLBACK].
2de10 0a 2a 2a 0a 2a 2a 20 49 66 20 63 65 72 74 61 69  .**.** If certai
2de20 6e 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72  n kinds of error
2de30 73 20 6f 63 63 75 72 20 6f 6e 20 61 20 73 74 61  s occur on a sta
2de40 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 61 20  tement within a 
2de50 6d 75 6c 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a  multi-statement.
2de60 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28  ** transaction (
2de70 65 72 72 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67  errors including
2de80 20 5b 53 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20   [SQLITE_FULL], 
2de90 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a  [SQLITE_IOERR],.
2dea0 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  ** [SQLITE_NOMEM
2deb0 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  ], [SQLITE_BUSY]
2dec0 2c 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e  , and [SQLITE_IN
2ded0 54 45 52 52 55 50 54 5d 29 20 74 68 65 6e 20 74  TERRUPT]) then t
2dee0 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  he.** transactio
2def0 6e 20 6d 69 67 68 74 20 62 65 20 72 6f 6c 6c 65  n might be rolle
2df00 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63  d back automatic
2df10 61 6c 6c 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20  ally.  The only 
2df20 77 61 79 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f  way to.** find o
2df30 75 74 20 77 68 65 74 68 65 72 20 53 51 4c 69 74  ut whether SQLit
2df40 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  e automatically 
2df50 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20  rolled back the 
2df60 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 66 74 65  transaction afte
2df70 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73  r.** an error is
2df80 20 74 6f 20 75 73 65 20 74 68 69 73 20 66 75 6e   to use this fun
2df90 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ction..**.** If 
2dfa0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 63  another thread c
2dfb0 68 61 6e 67 65 73 20 74 68 65 20 61 75 74 6f 63  hanges the autoc
2dfc0 6f 6d 6d 69 74 20 73 74 61 74 75 73 20 6f 66 20  ommit status of 
2dfd0 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
2dfe0 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65  connection while
2dff0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
2e000 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74   running, then t
2e010 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
2e020 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  ** is undefined.
2e030 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
2e040 6e 74 73 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48  nts: [H12931] [H
2e050 31 32 39 33 32 5d 20 5b 48 31 32 39 33 33 5d 20  12932] [H12933] 
2e060 5b 48 31 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49  [H12934].*/.SQLI
2e070 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2e080 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69  e3_get_autocommi
2e090 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a  t(sqlite3*);../*
2e0a0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69  .** CAPI3REF: Fi
2e0b0 6e 64 20 54 68 65 20 44 61 74 61 62 61 73 65 20  nd The Database 
2e0c0 48 61 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70  Handle Of A Prep
2e0d0 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b  ared Statement {
2e0e0 48 31 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e  H13120} <S60600>
2e0f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2e100 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74  e3_db_handle int
2e110 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74  erface returns t
2e120 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
2e130 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a  nection] handle.
2e140 2a 2a 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70  ** to which a [p
2e150 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2e160 74 5d 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65  t] belongs.  The
2e170 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
2e180 63 74 69 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e  ction].** return
2e190 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 64 62  ed by sqlite3_db
2e1a0 5f 68 61 6e 64 6c 65 20 69 73 20 74 68 65 20 73  _handle is the s
2e1b0 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  ame [database co
2e1c0 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77  nnection] that w
2e1d0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
2e1e0 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20  ument.** to the 
2e1f0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
2e200 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20  _v2()] call (or 
2e210 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 74 68  its variants) th
2e220 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a  at was used to.*
2e230 2a 20 63 72 65 61 74 65 20 74 68 65 20 73 74 61  * create the sta
2e240 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69  tement in the fi
2e250 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a  rst place..**.**
2e260 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b   Requirements: [
2e270 48 31 33 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54  H13123].*/.SQLIT
2e280 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73  E_API sqlite3 *s
2e290 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
2e2a0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
2e2b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
2e2c0 3a 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20  : Find the next 
2e2d0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2e2e0 6e 74 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30  nt {H13140} <S60
2e2f0 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  600>.**.** This 
2e300 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
2e310 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
2e320 68 65 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65  he next [prepare
2e330 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74  d statement] aft
2e340 65 72 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f  er.** pStmt asso
2e350 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
2e360 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
2e370 74 69 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70  tion] pDb.  If p
2e380 53 74 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20  Stmt is NULL.** 
2e390 74 68 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66  then this interf
2e3a0 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ace returns a po
2e3b0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 72  inter to the fir
2e3c0 73 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74  st prepared stat
2e3d0 65 6d 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61  ement.** associa
2e3e0 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74  ted with the dat
2e3f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2e400 20 70 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65   pDb.  If no pre
2e410 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a  pared statement.
2e420 2a 2a 20 73 61 74 69 73 66 69 65 73 20 74 68 65  ** satisfies the
2e430 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74   conditions of t
2e440 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20  his routine, it 
2e450 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a  returns NULL..**
2e460 0a 2a 2a 20 54 68 65 20 5b 64 61 74 61 62 61 73  .** The [databas
2e470 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f  e connection] po
2e480 69 6e 74 65 72 20 44 20 69 6e 20 61 20 63 61 6c  inter D in a cal
2e490 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  l to.** [sqlite3
2e4a0 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d  _next_stmt(D,S)]
2e4b0 20 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20 61   must refer to a
2e4c0 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a  n open database.
2e4d0 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e  ** connection an
2e4e0 64 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20  d in particular 
2e4f0 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 20 4e 55  must not be a NU
2e500 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
2e510 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20  * Requirements: 
2e520 5b 48 31 33 31 34 33 5d 20 5b 48 31 33 31 34 36  [H13143] [H13146
2e530 5d 20 5b 48 31 33 31 34 39 5d 20 5b 48 31 33 31  ] [H13149] [H131
2e540 35 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  52].*/.SQLITE_AP
2e550 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  I sqlite3_stmt *
2e560 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d  sqlite3_next_stm
2e570 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20  t(sqlite3 *pDb, 
2e580 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
2e590 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  tmt);../*.** CAP
2e5a0 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e  I3REF: Commit An
2e5b0 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66  d Rollback Notif
2e5c0 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b  ication Callback
2e5d0 73 20 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 34  s {H12950} <S604
2e5e0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
2e5f0 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  lite3_commit_hoo
2e600 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  k() interface re
2e610 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61  gisters a callba
2e620 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74  ck.** function t
2e630 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65  o be invoked whe
2e640 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 74  never a transact
2e650 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
2e660 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63  ..** Any callbac
2e670 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69  k set by a previ
2e680 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ous call to sqli
2e690 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
2e6a0 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d  ).** for the sam
2e6b0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2e6c0 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64  ction is overrid
2e6d0 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  den..** The sqli
2e6e0 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f  te3_rollback_hoo
2e6f0 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  k() interface re
2e700 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61  gisters a callba
2e710 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74  ck.** function t
2e720 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65  o be invoked whe
2e730 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 74  never a transact
2e740 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
2e750 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63  ..** Any callbac
2e760 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69  k set by a previ
2e770 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ous call to sqli
2e780 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
2e790 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d  ).** for the sam
2e7a0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2e7b0 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64  ction is overrid
2e7c0 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72 67  den..** The pArg
2e7d0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61 73   argument is pas
2e7e0 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74  sed through to t
2e7f0 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20  he callback..** 
2e800 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  If the callback 
2e810 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b  on a commit hook
2e820 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
2e830 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74  s non-zero,.** t
2e840 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 69  hen the commit i
2e850 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  s converted into
2e860 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a   a rollback..**.
2e870 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75  ** If another fu
2e880 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69  nction was previ
2e890 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
2e8a0 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 76 61  , its.** pArg va
2e8b0 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  lue is returned.
2e8c0 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
2e8d0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
2e8e0 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  .** The callback
2e8f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2e900 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74  must not do anyt
2e910 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d  hing that will m
2e920 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74  odify.** the dat
2e930 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2e940 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68   that invoked th
2e950 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e 79  e callback.  Any
2e960 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d   actions.** to m
2e970 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61  odify the databa
2e980 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75  se connection mu
2e990 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75  st be deferred u
2e9a0 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a  ntil after the.*
2e9b0 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20  * completion of 
2e9c0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  the [sqlite3_ste
2e9d0 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74  p()] call that t
2e9e0 72 69 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d  riggered the com
2e9f0 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61  mit.** or rollba
2ea00 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66  ck hook in the f
2ea10 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e  irst place..** N
2ea20 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65  ote that [sqlite
2ea30 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
2ea40 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  and [sqlite3_ste
2ea50 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79  p()] both modify
2ea60 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61   their.** databa
2ea70 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66  se connections f
2ea80 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f  or the meaning o
2ea90 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68  f "modify" in th
2eaa0 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a  is paragraph..**
2eab0 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e 67 20  .** Registering 
2eac0 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e 20  a NULL function 
2ead0 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61 6c  disables the cal
2eae0 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  lback..**.** For
2eaf0 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66   the purposes of
2eb00 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 72 61   this API, a tra
2eb10 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 69 64  nsaction is said
2eb20 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a   to have been.**
2eb30 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20   rolled back if 
2eb40 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 4f 4c  an explicit "ROL
2eb50 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 6e 74  LBACK" statement
2eb60 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 6f 72   is executed, or
2eb70 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 72 20  .** an error or 
2eb80 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 73 65  constraint cause
2eb90 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 72 6f  s an implicit ro
2eba0 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 72 2e  llback to occur.
2ebb0 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b  .** The rollback
2ebc0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74   callback is not
2ebd0 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72   invoked if a tr
2ebe0 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20  ansaction is.** 
2ebf0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f  automatically ro
2ec00 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73  lled back becaus
2ec10 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  e the database c
2ec20 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
2ec30 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c  sed..** The roll
2ec40 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73  back callback is
2ec50 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20   not invoked if 
2ec60 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
2ec70 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  .** rolled back 
2ec80 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d 69 74  because a commit
2ec90 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
2eca0 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 20  ed non-zero..** 
2ecb0 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f 6e 20  <todo> Check on 
2ecc0 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a  this </todo>.**.
2ecd0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2ece0 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31  .** [H12951] [H1
2ecf0 32 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b  2952] [H12953] [
2ed00 48 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d  H12954] [H12955]
2ed10 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31  .** [H12961] [H1
2ed20 32 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b  2962] [H12963] [
2ed30 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54  H12964].*/.SQLIT
2ed40 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
2ed50 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
2ed60 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29  sqlite3*, int(*)
2ed70 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b  (void*), void*);
2ed80 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2ed90 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61   *sqlite3_rollba
2eda0 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a  ck_hook(sqlite3*
2edb0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a  , void(*)(void *
2edc0 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a  ), void*);../*.*
2edd0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61  * CAPI3REF: Data
2ede0 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61   Change Notifica
2edf0 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b  tion Callbacks {
2ee00 48 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e  H12970} <S60400>
2ee10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2ee20 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29  e3_update_hook()
2ee30 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73   interface regis
2ee40 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20  ters a callback 
2ee50 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68  function.** with
2ee60 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
2ee70 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74  onnection] ident
2ee80 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72  ified by the fir
2ee90 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  st argument.** t
2eea0 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65  o be invoked whe
2eeb0 6e 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 75  never a row is u
2eec0 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64  pdated, inserted
2eed0 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20   or deleted..** 
2eee0 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74  Any callback set
2eef0 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63   by a previous c
2ef00 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63  all to this func
2ef10 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20  tion.** for the 
2ef20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
2ef30 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72  nnection is over
2ef40 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ridden..**.** Th
2ef50 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
2ef60 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
2ef70 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74  o the function t
2ef80 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a  o invoke when a.
2ef90 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 65  ** row is update
2efa0 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64  d, inserted or d
2efb0 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66  eleted..** The f
2efc0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
2efd0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73   the callback is
2efe0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74   a copy of the t
2eff0 68 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  hird argument.**
2f000 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61   to sqlite3_upda
2f010 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68  te_hook()..** Th
2f020 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63  e second callbac
2f030 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e  k argument is on
2f040 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53  e of [SQLITE_INS
2f050 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45  ERT], [SQLITE_DE
2f060 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51  LETE],.** or [SQ
2f070 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65  LITE_UPDATE], de
2f080 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f  pending on the o
2f090 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61  peration that ca
2f0a0 75 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63  used the callbac
2f0b0 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b  k.** to be invok
2f0c0 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  ed..** The third
2f0d0 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75   and fourth argu
2f0e0 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c  ments to the cal
2f0f0 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f  lback contain po
2f100 69 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a  inters to the.**
2f110 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61   database and ta
2f120 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e  ble name contain
2f130 69 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64  ing the affected
2f140 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e   row..** The fin
2f150 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61  al callback para
2f160 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f  meter is the [ro
2f170 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e  wid] of the row.
2f180 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20  .** In the case 
2f190 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68  of an update, th
2f1a0 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64  is is the [rowid
2f1b0 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61  ] after the upda
2f1c0 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a  te takes place..
2f1d0 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65  **.** The update
2f1e0 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76   hook is not inv
2f1f0 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e  oked when intern
2f200 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73  al system tables
2f210 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64   are.** modified
2f220 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61   (i.e. sqlite_ma
2f230 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f  ster and sqlite_
2f240 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a  sequence)..**.**
2f250 20 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   The update hook
2f260 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2f270 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74  must not do anyt
2f280 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d  hing that will m
2f290 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74  odify.** the dat
2f2a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2f2b0 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68   that invoked th
2f2c0 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20  e update hook.  
2f2d0 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74  Any actions.** t
2f2e0 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74  o modify the dat
2f2f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2f300 20 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65   must be deferre
2f310 64 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68  d until after th
2f320 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20  e.** completion 
2f330 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
2f340 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61  step()] call tha
2f350 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20  t triggered the 
2f360 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20  update hook..** 
2f370 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74  Note that [sqlit
2f380 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
2f390 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74   and [sqlite3_st
2f3a0 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66  ep()] both modif
2f3b0 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62  y their.** datab
2f3c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
2f3d0 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20  for the meaning 
2f3e0 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74  of "modify" in t
2f3f0 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a  his paragraph..*
2f400 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20  *.** If another 
2f410 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65  function was pre
2f420 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72  viously register
2f430 65 64 2c 20 69 74 73 20 70 41 72 67 20 76 61 6c  ed, its pArg val
2f440 75 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  ue.** is returne
2f450 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55  d.  Otherwise NU
2f460 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  LL is returned..
2f470 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
2f480 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 37 31 5d 20  ts:.** [H12971] 
2f490 5b 48 31 32 39 37 33 5d 20 5b 48 31 32 39 37 35  [H12973] [H12975
2f4a0 5d 20 5b 48 31 32 39 37 37 5d 20 5b 48 31 32 39  ] [H12977] [H129
2f4b0 37 39 5d 20 5b 48 31 32 39 38 31 5d 20 5b 48 31  79] [H12981] [H1
2f4c0 32 39 38 33 5d 20 5b 48 31 32 39 38 36 5d 0a 2a  2983] [H12986].*
2f4d0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
2f4e0 64 20 2a 73 71 6c 69 74 65 33 5f 75 70 64 61 74  d *sqlite3_updat
2f4f0 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65  e_hook(.  sqlite
2f500 33 2a 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76  3*, .  void(*)(v
2f510 6f 69 64 20 2a 2c 69 6e 74 20 2c 63 68 61 72 20  oid *,int ,char 
2f520 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e  const *,char con
2f530 73 74 20 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74  st *,sqlite3_int
2f540 36 34 29 2c 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a  64),.  void*.);.
2f550 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2f560 20 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62   Enable Or Disab
2f570 6c 65 20 53 68 61 72 65 64 20 50 61 67 65 72 20  le Shared Pager 
2f580 43 61 63 68 65 20 7b 48 31 30 33 33 30 7d 20 3c  Cache {H10330} <
2f590 53 33 30 39 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S30900>.** KEYWO
2f5a0 52 44 53 3a 20 7b 73 68 61 72 65 64 20 63 61 63  RDS: {shared cac
2f5b0 68 65 7d 20 7b 73 68 61 72 65 64 20 63 61 63 68  he} {shared cach
2f5c0 65 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68  e mode}.**.** Th
2f5d0 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c  is routine enabl
2f5e0 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74  es or disables t
2f5f0 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 68  he sharing of th
2f600 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65  e database cache
2f610 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 64  .** and schema d
2f620 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 62  ata structures b
2f630 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 65  etween [database
2f640 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 6f   connection | co
2f650 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f  nnections].** to
2f660 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
2f670 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 65  se. Sharing is e
2f680 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72  nabled if the ar
2f690 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a 2a  gument is true.*
2f6a0 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 69  * and disabled i
2f6b0 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69  f the argument i
2f6c0 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43  s false..**.** C
2f6d0 61 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20  ache sharing is 
2f6e0 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 61  enabled and disa
2f6f0 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 69  bled for an enti
2f700 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20 54  re process..** T
2f710 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20  his is a change 
2f720 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72  as of SQLite ver
2f730 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70  sion 3.5.0. In p
2f740 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  rior versions of
2f750 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72   SQLite,.** shar
2f760 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20  ing was enabled 
2f770 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20  or disabled for 
2f780 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61  each thread sepa
2f790 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rately..**.** Th
2f7a0 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20  e cache sharing 
2f7b0 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73  mode set by this
2f7c0 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63   interface effec
2f7d0 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  ts all subsequen
2f7e0 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73  t.** calls to [s
2f7f0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
2f800 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
2f810 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
2f820 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20  3_open16()]..** 
2f830 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73  Existing databas
2f840 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f  e connections co
2f850 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73  ntinue use the s
2f860 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74  haring mode.** t
2f870 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63  hat was in effec
2f880 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68  t at the time th
2f890 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a  ey were opened..
2f8a0 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61  **.** Virtual ta
2f8b0 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75  bles cannot be u
2f8c0 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65  sed with a share
2f8d0 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73  d cache.  When s
2f8e0 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69  hared.** cache i
2f8f0 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b  s enabled, the [
2f900 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
2f910 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65  odule()] API use
2f920 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a  d to register.**
2f930 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
2f940 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75  will always retu
2f950 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a  rn an error..**.
2f960 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2f970 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
2f980 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61  OK] if shared ca
2f990 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20  che was enabled 
2f9a0 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73  or disabled.** s
2f9b0 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e  uccessfully.  An
2f9c0 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
2f9d0 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77   returned otherw
2f9e0 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65  ise..**.** Share
2f9f0 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62  d cache is disab
2fa00 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20  led by default. 
2fa10 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63  But this might c
2fa20 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75  hange in.** futu
2fa30 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53  re releases of S
2fa40 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74  QLite.  Applicat
2fa50 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61  ions that care a
2fa60 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63  bout shared.** c
2fa70 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f  ache setting sho
2fa80 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69  uld set it expli
2fa90 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  citly..**.** See
2faa0 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65 20   Also:  [SQLite 
2fab0 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64  Shared-Cache Mod
2fac0 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  e].**.** Require
2fad0 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d 20  ments: [H10331] 
2fae0 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33 37  [H10336] [H10337
2faf0 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53 51  ] [H10339].*/.SQ
2fb00 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2fb10 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
2fb20 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a  ed_cache(int);..
2fb30 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2fb40 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65 20  Attempt To Free 
2fb50 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31 37  Heap Memory {H17
2fb60 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a  340} <S30220>.**
2fb70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2fb80 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
2fb90 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d   interface attem
2fba0 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62 79  pts to free N by
2fbb0 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20 6d  tes.** of heap m
2fbc0 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f 63  emory by dealloc
2fbd0 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74  ating non-essent
2fbe0 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ial memory alloc
2fbf0 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 62  ations.** held b
2fc00 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  y the database l
2fc10 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20 4d  ibrary. {END}  M
2fc20 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63 61  emory used to ca
2fc30 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  che database.** 
2fc40 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76 65  pages to improve
2fc50 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20   performance is 
2fc60 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f  an example of no
2fc70 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f  n-essential memo
2fc80 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72  ry..** sqlite3_r
2fc90 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
2fca0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
2fcb0 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75  er of bytes actu
2fcc0 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20 77  ally freed,.** w
2fcd0 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f  hich might be mo
2fce0 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e 20  re or less than 
2fcf0 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 65  the amount reque
2fd00 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  sted..**.** Requ
2fd10 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33 34  irements: [H1734
2fd20 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a 53  1] [H17342].*/.S
2fd30 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2fd40 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
2fd50 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  mory(int);../*.*
2fd60 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70 6f  * CAPI3REF: Impo
2fd70 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48 65  se A Limit On He
2fd80 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30 7d  ap Size {H17350}
2fd90 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20   <S30220>.**.** 
2fda0 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 74  The sqlite3_soft
2fdb0 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69 6e  _heap_limit() in
2fdc0 74 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 61  terface places a
2fdd0 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a 2a   "soft" limit.**
2fde0 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f   on the amount o
2fdf0 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 68  f heap memory th
2fe00 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61  at may be alloca
2fe10 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a  ted by SQLite..*
2fe20 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c  * If an internal
2fe30 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 72   allocation is r
2fe40 65 71 75 65 73 74 65 64 20 74 68 61 74 20 77 6f  equested that wo
2fe50 75 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a 2a  uld exceed the.*
2fe60 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  * soft heap limi
2fe70 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65  t, [sqlite3_rele
2fe80 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73  ase_memory()] is
2fe90 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72 0a   invoked one or.
2fea0 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f  ** more times to
2feb0 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73 70   free up some sp
2fec0 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 20 61  ace before the a
2fed0 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65 72  llocation is per
2fee0 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  formed..**.** Th
2fef0 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c 65  e limit is calle
2ff00 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75 73  d "soft", becaus
2ff10 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72 65  e if [sqlite3_re
2ff20 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a  lease_memory()].
2ff30 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 73  ** cannot free s
2ff40 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 79  ufficient memory
2ff50 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 20   to prevent the 
2ff60 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e 67  limit from being
2ff70 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74 68   exceeded,.** th
2ff80 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f  e memory is allo
2ff90 63 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e 64  cated anyway and
2ffa0 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65   the current ope
2ffb0 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 2e  ration proceeds.
2ffc0 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76  .**.** A negativ
2ffd0 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65 20  e or zero value 
2ffe0 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61 74  for N means that
2fff0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f 66   there is no sof
30000 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e 64  t heap limit and
30010 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c  .** [sqlite3_rel
30020 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 77  ease_memory()] w
30030 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c  ill only be call
30040 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 69  ed when memory i
30050 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a 20  s exhausted..** 
30060 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  The default valu
30070 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68  e for the soft h
30080 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65 72  eap limit is zer
30090 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  o..**.** SQLite 
300a0 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65 66 66  makes a best eff
300b0 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68 65  ort to honor the
300c0 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
300d0 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20  ..** But if the 
300e0 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20  soft heap limit 
300f0 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72 65  cannot be honore
30100 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69 6c  d, execution wil
30110 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77 69  l.** continue wi
30120 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20 6e  thout error or n
30130 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54 68  otification.  Th
30140 69 73 20 69 73 20 77 68 79 20 74 68 65 20 6c 69  is is why the li
30150 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64  mit is.** called
30160 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 2e   a "soft" limit.
30170 20 20 49 74 20 69 73 20 61 64 76 69 73 6f 72 79    It is advisory
30180 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 69   only..**.** Pri
30190 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65 72  or to SQLite ver
301a0 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69 73  sion 3.5.0, this
301b0 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f   routine only co
301c0 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d 65  nstrained the me
301d0 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  mory.** allocate
301e0 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 68  d by a single th
301f0 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65 20  read - the same 
30200 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 20  thread in which 
30210 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
30220 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e 67  runs.  Beginning
30230 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65 72   with SQLite ver
30240 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 20  sion 3.5.0, the 
30250 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20  soft heap limit 
30260 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74 6f  is.** applied to
30270 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54 68   all threads. Th
30280 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65  e value specifie
30290 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 68  d for the soft h
302a0 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73 20  eap limit.** is 
302b0 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f  an upper bound o
302c0 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d 6f  n the total memo
302d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f  ry allocation fo
302e0 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 49  r all threads. I
302f0 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 35  n.** version 3.5
30300 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  .0 there is no m
30310 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69 6d  echanism for lim
30320 69 74 69 6e 67 20 74 68 65 20 68 65 61 70 20 75  iting the heap u
30330 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64 69  sage for.** indi
30340 76 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e 0a  vidual threads..
30350 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
30360 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d 20  ts:.** [H16351] 
30370 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35 33  [H16352] [H16353
30380 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36 33  ] [H16354] [H163
30390 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f 0a  55] [H16358].*/.
303a0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
303b0 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61  sqlite3_soft_hea
303c0 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f  p_limit(int);../
303d0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45  *.** CAPI3REF: E
303e0 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20  xtract Metadata 
303f0 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f  About A Column O
30400 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35  f A Table {H1285
30410 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a  0} <S60300>.**.*
30420 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
30430 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20  eturns metadata 
30440 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63  about a specific
30450 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65   column of a spe
30460 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73  cific.** databas
30470 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62  e table accessib
30480 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61  le using the [da
30490 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
304a0 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73  n] handle.** pas
304b0 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
304c0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
304d0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  nt..**.** The co
304e0 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69  lumn is identifi
304f0 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  ed by the second
30500 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72  , third and four
30510 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
30520 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
30530 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61  n. The second pa
30540 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65  rameter is eithe
30550 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  r the name of th
30560 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69  e database.** (i
30570 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d  .e. "main", "tem
30580 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65  p" or an attache
30590 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74  d database) cont
305a0 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69  aining the speci
305b0 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72  fied.** table or
305c0 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20   NULL. If it is 
305d0 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61  NULL, then all a
305e0 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
305f0 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a  s are searched.*
30600 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  * for the table 
30610 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61  using the same a
30620 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79  lgorithm used by
30630 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e   the database en
30640 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c  gine to.** resol
30650 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74  ve unqualified t
30660 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e  able references.
30670 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
30680 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61   and fourth para
30690 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66  meters to this f
306a0 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20  unction are the 
306b0 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
306c0 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  .** name of the 
306d0 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20  desired column, 
306e0 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65  respectively. Ne
306f0 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70  ither of these p
30700 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79  arameters.** may
30710 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   be NULL..**.** 
30720 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75  Metadata is retu
30730 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20  rned by writing 
30740 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f  to the memory lo
30750 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61  cations passed a
30760 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64  s the 5th.** and
30770 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61   subsequent para
30780 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66  meters to this f
30790 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20  unction. Any of 
307a0 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20  these arguments 
307b0 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20  may be.** NULL, 
307c0 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68  in which case th
307d0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
307e0 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64  element of metad
307f0 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a  ata is omitted..
30800 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
30810 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72  e>.** <table bor
30820 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e  der="1">.** <tr>
30830 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c  <th> Parameter <
30840 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79  th> Output<br>Ty
30850 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70  pe <th>  Descrip
30860 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c  tion.**.** <tr><
30870 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e  td> 5th <td> con
30880 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61  st char* <td> Da
30890 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c  ta type.** <tr><
308a0 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e  td> 6th <td> con
308b0 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61  st char* <td> Na
308c0 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f  me of default co
308d0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
308e0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68  .** <tr><td> 7th
308f0 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20   <td> int       
30900 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63    <td> True if c
30910 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20  olumn has a NOT 
30920 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a  NULL constraint.
30930 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20  ** <tr><td> 8th 
30940 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20  <td> int        
30950 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f   <td> True if co
30960 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20  lumn is part of 
30970 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a  the PRIMARY KEY.
30980 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20  ** <tr><td> 9th 
30990 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20  <td> int        
309a0 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f   <td> True if co
309b0 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e 43  lumn is [AUTOINC
309c0 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 62  REMENT].** </tab
309d0 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75  le>.** </blockqu
309e0 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  ote>.**.** The m
309f0 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f  emory pointed to
30a00 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74 65   by the characte
30a10 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72  r pointers retur
30a20 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64  ned for the.** d
30a30 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
30a40 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  and collation se
30a50 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64 20  quence is valid 
30a60 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 6e  only until the n
30a70 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61  ext.** call to a
30a80 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20 66 75  ny SQLite API fu
30a90 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  nction..**.** If
30aa0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74   the specified t
30ab0 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79  able is actually
30ac0 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 72   a view, an [err
30ad0 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
30ae0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rned..**.** If t
30af0 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c  he specified col
30b00 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c 20  umn is "rowid", 
30b10 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64  "oid" or "_rowid
30b20 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49 4e  _" and an.** [IN
30b30 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
30b40 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 65  Y] column has be
30b50 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65  en explicitly de
30b60 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65  clared, then the
30b70 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d   output.** param
30b80 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66 6f  eters are set fo
30b90 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c 79  r the explicitly
30ba0 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e   declared column
30bb0 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  . If there is no
30bc0 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 64  .** explicitly d
30bd0 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45 52  eclared [INTEGER
30be0 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f   PRIMARY KEY] co
30bf0 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 6f  lumn, then the o
30c00 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74  utput.** paramet
30c10 65 72 73 20 61 72 65 20 73 65 74 20 61 73 20 66  ers are set as f
30c20 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70  ollows:.**.** <p
30c30 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 20  re>.**     data 
30c40 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 0a  type: "INTEGER".
30c50 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f 6e  **     collation
30c60 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e 41   sequence: "BINA
30c70 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6e  RY".**     not n
30c80 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 72  ull: 0.**     pr
30c90 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a 20  imary key: 1.** 
30ca0 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d 65      auto increme
30cb0 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a  nt: 0.** </pre>.
30cc0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
30cd0 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65  ion may load one
30ce0 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73   or more schemas
30cf0 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66   from database f
30d00 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65  iles. If an.** e
30d10 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
30d20 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c  ng this process,
30d30 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 65   or if the reque
30d40 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f  sted table or co
30d50 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62  lumn.** cannot b
30d60 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 72  e found, an [err
30d70 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
30d80 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f  rned and an erro
30d90 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a 2a  r message left.*
30da0 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61  * in the [databa
30db0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28  se connection] (
30dc0 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20  to be retrieved 
30dd0 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72  using sqlite3_er
30de0 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54  rmsg())..**.** T
30df0 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20  his API is only 
30e00 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65  available if the
30e10 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d   library was com
30e20 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a  piled with the.*
30e30 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  * [SQLITE_ENABLE
30e40 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
30e50 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72  ] C-preprocessor
30e60 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e   symbol defined.
30e70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
30e80 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65  nt sqlite3_table
30e90 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61  _column_metadata
30ea0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
30eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ec0 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  /* Connection ha
30ed0 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
30ee0 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20  char *zDbName,  
30ef0 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
30f00 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a  e name or NULL *
30f10 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
30f20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20  zTableName,     
30f30 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f  /* Table name */
30f40 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
30f50 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f  ColumnName,    /
30f60 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f  * Column name */
30f70 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a  .  char const **
30f80 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 2f  pzDataType,    /
30f90 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 72  * OUTPUT: Declar
30fa0 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f 0a  ed data type */.
30fb0 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70    char const **p
30fc0 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a  zCollSeq,     /*
30fd0 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69   OUTPUT: Collati
30fe0 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
30ff0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e   */.  int *pNotN
31000 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  ull,            
31010 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
31020 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  e if NOT NULL co
31030 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 20  nstraint exists 
31040 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d 61  */.  int *pPrima
31050 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20  ryKey,          
31060 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
31070 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20   if column part 
31080 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a  of PK */.  int *
31090 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 20  pAutoinc        
310a0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54         /* OUTPUT
310b0 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e  : True if column
310c0 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65   is auto-increme
310d0 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.);../*.** 
310e0 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20 41  CAPI3REF: Load A
310f0 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31 32  n Extension {H12
31100 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a  600} <S20500>.**
31110 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
31120 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c 69  ce loads an SQLi
31130 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62  te extension lib
31140 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e 61  rary from the na
31150 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  med file..**.** 
31160 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73 71 6c  {H12601} The sql
31170 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  ite3_load_extens
31180 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20  ion() interface 
31190 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61 64  attempts to load
311a0 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   an.**          
311b0 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e  SQLite extension
311c0 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e   library contain
311d0 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 7a  ed in the file z
311e0 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  File..**.** {H12
311f0 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20 70  602} The entry p
31200 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a 2a  oint is zProc..*
31210 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a 50  *.** {H12603} zP
31220 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69 6e  roc may be 0, in
31230 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
31240 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 72  name of the entr
31250 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20  y point.**      
31260 20 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f 20      defaults to 
31270 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69  "sqlite3_extensi
31280 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20  on_init"..**.** 
31290 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73 71 6c  {H12604} The sql
312a0 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  ite3_load_extens
312b0 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20  ion() interface 
312c0 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20  shall return.** 
312d0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45           [SQLITE
312e0 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20  _OK] on success 
312f0 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  and [SQLITE_ERRO
31300 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20  R] if something 
31310 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a  goes wrong..**.*
31320 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61 6e  * {H12605} If an
31330 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e   error occurs an
31340 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f  d pzErrMsg is no
31350 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  t 0, then the.**
31360 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
31370 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  e3_load_extensio
31380 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73  n()] interface s
31390 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 0a  hall attempt to.
313a0 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c 6c  **          fill
313b0 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 20   *pzErrMsg with 
313c0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65  error message te
313d0 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d  xt stored in mem
313e0 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ory.**          
313f0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73  obtained from [s
31400 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
31410 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61 6c  . {END}  The cal
31420 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  ling function.**
31430 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c 64            should
31440 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72   free this memor
31450 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71  y by calling [sq
31460 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a  lite3_free()]..*
31470 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45 78  *.** {H12606} Ex
31480 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
31490 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64 20  must be enabled 
314a0 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20  using.**        
314b0 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c    [sqlite3_enabl
314c0 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  e_load_extension
314d0 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61 6c  ()] prior to cal
314e0 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a 2a  ling this API,.*
314f0 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68 65 72  *          other
31500 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77 69  wise an error wi
31510 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a  ll be returned..
31520 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
31530 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65  t sqlite3_load_e
31540 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69  xtension(.  sqli
31550 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
31560 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78    /* Load the ex
31570 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69  tension into thi
31580 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
31590 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
315a0 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20   char *zFile,   
315b0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
315c0 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63  shared library c
315d0 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73  ontaining extens
315e0 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
315f0 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f  har *zProc,    /
31600 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20  * Entry point.  
31610 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 69  Derived from zFi
31620 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61  le if 0 */.  cha
31630 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20  r **pzErrMsg    
31640 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20     /* Put error 
31650 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20  message here if 
31660 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  not 0 */.);../*.
31670 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61  ** CAPI3REF: Ena
31680 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45  ble Or Disable E
31690 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67  xtension Loading
316a0 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35 30   {H12620} <S2050
316b0 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e  0>.**.** So as n
316c0 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72  ot to open secur
316d0 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64  ity holes in old
316e0 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  er applications 
316f0 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72  that are.** unpr
31700 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 77  epared to deal w
31710 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  ith extension lo
31720 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 20  ading, and as a 
31730 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c 69  means of disabli
31740 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20  ng.** extension 
31750 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 76  loading while ev
31760 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 6e  aluating user-en
31770 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 66  tered SQL, the f
31780 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20  ollowing API.** 
31790 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 74  is provided to t
317a0 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33  urn the [sqlite3
317b0 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
317c0 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20  )] mechanism on 
317d0 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45  and off..**.** E
317e0 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
317f0 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75   is off by defau
31800 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20 23  lt. See ticket #
31810 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  1863..**.** {H12
31820 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73 71  621} Call the sq
31830 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61  lite3_enable_loa
31840 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f  d_extension() ro
31850 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66 66  utine with onoff
31860 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ==1.**          
31870 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69 6f  to turn extensio
31880 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e 64  n loading on and
31890 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e   call it with on
318a0 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a 2a  off==0 to turn.*
318b0 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 62 61  *          it ba
318c0 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2a  ck off again..**
318d0 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78 74  .** {H12622} Ext
318e0 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69  ension loading i
318f0 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74  s off by default
31900 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
31910 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62  int sqlite3_enab
31920 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le_load_extensio
31930 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  n(sqlite3 *db, i
31940 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a  nt onoff);../*.*
31950 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 6f  * CAPI3REF: Auto
31960 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20 41  matically Load A
31970 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48 31  n Extensions {H1
31980 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a  2640} <S20500>.*
31990 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63 61  *.** This API ca
319a0 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74 20  n be invoked at 
319b0 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70 20  program startup 
319c0 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67 69  in order to regi
319d0 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d  ster.** one or m
319e0 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20 6c  ore statically l
319f0 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e 73  inked extensions
31a00 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61 76   that will be av
31a10 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c  ailable.** to al
31a20 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20  l new [database 
31a30 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b 45  connections]. {E
31a40 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ND}.**.** This r
31a50 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 20  outine stores a 
31a60 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65  pointer to the e
31a70 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61  xtension in an a
31a80 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a 20  rray that is.** 
31a90 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73  obtained from [s
31aa0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
31ab0 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61 20  .  If you run a 
31ac0 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65 63  memory leak chec
31ad0 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 70  ker.** on your p
31ae0 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72 65  rogram and it re
31af0 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65 63  ports a leak bec
31b00 61 75 73 65 20 6f 66 20 74 68 69 73 20 61 72 72  ause of this arr
31b10 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73  ay, invoke.** [s
31b20 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74  qlite3_reset_aut
31b30 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70  o_extension()] p
31b40 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77 6e  rior to shutdown
31b50 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65 6d   to free the mem
31b60 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  ory..**.** {H126
31b70 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 6f  41} This functio
31b80 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e 20 65  n registers an e
31b90 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20 70  xtension entry p
31ba0 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a 20  oint that is.** 
31bb0 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74           automat
31bc0 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 77  ically invoked w
31bd0 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b 64  henever a new [d
31be0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
31bf0 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  on].**          
31c00 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20  is opened using 
31c10 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
31c20 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  , [sqlite3_open1
31c30 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  6()],.**        
31c40 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70    or [sqlite3_op
31c50 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  en_v2()]..**.** 
31c60 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63 61  {H12642} Duplica
31c70 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 72  te extensions ar
31c80 65 20 64 65 74 65 63 74 65 64 20 73 6f 20 63 61  e detected so ca
31c90 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  lling this routi
31ca0 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d  ne.**          m
31cb0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69  ultiple times wi
31cc0 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 74 65  th the same exte
31cd0 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73  nsion is harmles
31ce0 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 33  s..**.** {H12643
31cf0 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  } This routine s
31d00 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20  tores a pointer 
31d10 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e  to the extension
31d20 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20   in an array.** 
31d30 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 73           that is
31d40 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
31d50 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
31d60 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 34  ]..**.** {H12644
31d70 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74 65  } Automatic exte
31d80 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63 72  nsions apply acr
31d90 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73 2e  oss all threads.
31da0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
31db0 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f  nt sqlite3_auto_
31dc0 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28  extension(void (
31dd0 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f  *xEntryPoint)(vo
31de0 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id));../*.** CAP
31df0 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 74  I3REF: Reset Aut
31e00 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f 6e  omatic Extension
31e10 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36 30   Loading {H12660
31e20 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20500>.**.**
31e30 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64   This function d
31e40 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 76  isables all prev
31e50 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65  iously registere
31e60 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 65  d automatic.** e
31e70 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d  xtensions. {END}
31e80 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65 20    It undoes the 
31e90 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 72  effect of all pr
31ea0 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ior.** [sqlite3_
31eb0 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29  auto_extension()
31ec0 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b  ] calls..**.** {
31ed0 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75 6e  H12661} This fun
31ee0 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61  ction disables a
31ef0 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  ll previously re
31f00 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20 20  gistered.**     
31f10 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20 65       automatic e
31f20 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  xtensions..**.**
31f30 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20 66   {H12662} This f
31f40 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73  unction disables
31f50 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e   automatic exten
31f60 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68 72  sions in all thr
31f70 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eads..*/.SQLITE_
31f80 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
31f90 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65  _reset_auto_exte
31fa0 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a  nsion(void);../*
31fb0 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45  .****** EXPERIME
31fc0 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74  NTAL - subject t
31fd0 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74  o change without
31fe0 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a   notice ********
31ff0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  ******.**.** The
32000 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
32010 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  e virtual-table 
32020 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 72  mechanism is cur
32030 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65  rently considere
32040 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72  d.** to be exper
32050 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e  imental.  The in
32060 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68  terface might ch
32070 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74  ange in incompat
32080 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66  ible ways..** If
32090 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c   this is a probl
320a0 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e  em for you, do n
320b0 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72  ot use the inter
320c0 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d  face at this tim
320d0 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  e..**.** When th
320e0 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  e virtual-table 
320f0 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c  mechanism stabil
32100 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65  izes, we will de
32110 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74  clare the.** int
32120 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 75  erface fixed, su
32130 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e  pport it indefin
32140 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76  itely, and remov
32150 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a  e this comment..
32160 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74  */../*.** Struct
32170 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65  ures used by the
32180 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
32190 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65  nterface.*/.type
321a0 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
321b0 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33 5f  e3_vtab sqlite3_
321c0 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73 74  vtab;.typedef st
321d0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
321e0 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f  ex_info sqlite3_
321f0 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65  index_info;.type
32200 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
32210 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 73  e3_vtab_cursor s
32220 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
32230 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  or;.typedef stru
32240 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  ct sqlite3_modul
32250 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  e sqlite3_module
32260 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
32270 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65  F: Virtual Table
32280 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30 7d   Object {H18000}
32290 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59   <S20400>.** KEY
322a0 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 6d  WORDS: sqlite3_m
322b0 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20 74  odule {virtual t
322c0 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20  able module}.** 
322d0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
322e0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
322f0 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c  e, sometimes cal
32300 6c 65 64 20 61 20 61 20 22 76 69 72 74 75 61 6c  led a a "virtual
32310 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c 20   table module", 
32320 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20  .** defines the 
32330 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
32340 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62  f a [virtual tab
32350 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 20  les].  .** This 
32360 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 73  structure consis
32370 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74  ts mostly of met
32380 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64  hods for the mod
32390 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72  ule..**.** A vir
323a0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
323b0 65 20 69 73 20 63 72 65 61 74 65 64 20 62 79 20  e is created by 
323c0 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65 72  filling in a per
323d0 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 61  sistent.** insta
323e0 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
323f0 63 74 75 72 65 20 61 6e 64 20 70 61 73 73 69 6e  cture and passin
32400 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  g a pointer to t
32410 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20  hat instance.** 
32420 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  to [sqlite3_crea
32430 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20  te_module()] or 
32440 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
32450 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a  module_v2()]..**
32460 20 54 68 65 20 72 65 67 69 73 74 72 61 74 69 6f   The registratio
32470 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20  n remains valid 
32480 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70 6c  until it is repl
32490 61 63 65 64 20 62 79 20 61 20 64 69 66 66 65 72  aced by a differ
324a0 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72  ent.** module or
324b0 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74 61   until the [data
324c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
324d0 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63 6f   closes.  The co
324e0 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 73  ntent.** of this
324f0 20 73 74 72 75 63 74 75 72 65 20 6d 75 73 74 20   structure must 
32500 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c 65  not change while
32510 20 69 74 20 69 73 20 72 65 67 69 73 74 65 72 65   it is registere
32520 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64 61  d with.** any da
32530 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
32540 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c  n..*/.struct sql
32550 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20  ite3_module {.  
32560 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20  int iVersion;.  
32570 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73  int (*xCreate)(s
32580 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70  qlite3*, void *p
32590 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Aux,.           
325a0 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f      int argc, co
325b0 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
325c0 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20  argv,.          
325d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
325e0 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72  b **ppVTab, char
325f0 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f  **);.  int (*xCo
32600 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c  nnect)(sqlite3*,
32610 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20   void *pAux,.   
32620 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
32630 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
32640 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20   *const*argv,.  
32650 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
32660 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54  ite3_vtab **ppVT
32670 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69  ab, char**);.  i
32680 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29  nt (*xBestIndex)
32690 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
326a0 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e  VTab, sqlite3_in
326b0 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e  dex_info*);.  in
326c0 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29  t (*xDisconnect)
326d0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
326e0 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78  VTab);.  int (*x
326f0 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33  Destroy)(sqlite3
32700 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
32710 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71   int (*xOpen)(sq
32720 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
32730 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  b, sqlite3_vtab_
32740 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f  cursor **ppCurso
32750 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f  r);.  int (*xClo
32760 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  se)(sqlite3_vtab
32770 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74  _cursor*);.  int
32780 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69   (*xFilter)(sqli
32790 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
327a0 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f  , int idxNum, co
327b0 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72  nst char *idxStr
327c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
327d0 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69    int argc, sqli
327e0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
327f0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74  );.  int (*xNext
32800 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  )(sqlite3_vtab_c
32810 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28  ursor*);.  int (
32820 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76  *xEof)(sqlite3_v
32830 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20  tab_cursor*);.  
32840 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73  int (*xColumn)(s
32850 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
32860 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e  or*, sqlite3_con
32870 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69  text*, int);.  i
32880 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c  nt (*xRowid)(sql
32890 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
328a0 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
328b0 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74   *pRowid);.  int
328c0 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69   (*xUpdate)(sqli
328d0 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c  te3_vtab *, int,
328e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
328f0 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
32900 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65   *);.  int (*xBe
32910 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61  gin)(sqlite3_vta
32920 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74  b *pVTab);.  int
32930 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65   (*xSync)(sqlite
32940 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a  3_vtab *pVTab);.
32950 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29    int (*xCommit)
32960 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
32970 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78  VTab);.  int (*x
32980 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65  Rollback)(sqlite
32990 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a  3_vtab *pVTab);.
329a0 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e    int (*xFindFun
329b0 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76  ction)(sqlite3_v
329c0 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20  tab *pVtab, int 
329d0 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72  nArg, const char
329e0 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20   *zName,.       
329f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a00 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28  void (**pxFunc)(
32a10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
32a20 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
32a30 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20  ue**),.         
32a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f                vo
32a50 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69  id **ppArg);.  i
32a60 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71  nt (*xRename)(sq
32a70 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
32a80 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
32a90 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  New);.};../*.** 
32aa0 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
32ab0 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67  l Table Indexing
32ac0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31   Information {H1
32ad0 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a  8100} <S20400>.*
32ae0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69  * KEYWORDS: sqli
32af0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a  te3_index_info.*
32b00 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
32b10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
32b20 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
32b30 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75  cture and its su
32b40 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75  bstructures is u
32b50 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69  sed to.** pass i
32b60 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20  nformation into 
32b70 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20  and receive the 
32b80 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b  reply from the [
32b90 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d  xBestIndex].** m
32ba0 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72 74  ethod of a [virt
32bb0 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
32bc0 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20 75  ].  The fields u
32bd0 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20  nder **Inputs** 
32be0 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74  are the.** input
32bf0 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20  s to xBestIndex 
32c00 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c  and are read-onl
32c10 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20 69  y.  xBestIndex i
32c20 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72 65  nserts its.** re
32c30 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20 2a  sults into the *
32c40 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c 64  *Outputs** field
32c50 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f  s..**.** The aCo
32c60 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79  nstraint[] array
32c70 20 72 65 63 6f 72 64 73 20 57 48 45 52 45 20 63   records WHERE c
32c80 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74  lause constraint
32c90 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
32ca0 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e  *.** <pre>column
32cb0 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a 2a   OP expr</pre>.*
32cc0 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69 73  *.** where OP is
32cd0 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c   =, &lt;, &lt;=,
32ce0 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e   &gt;, or &gt;=.
32cf0 20 20 54 68 65 20 70 61 72 74 69 63 75 6c 61 72    The particular
32d00 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20   operator is.** 
32d10 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 74  stored in aConst
32d20 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 65  raint[].op.  The
32d30 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f   index of the co
32d40 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20 69  lumn is stored i
32d50 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74  n.** aConstraint
32d60 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f  [].iColumn.  aCo
32d70 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c  nstraint[].usabl
32d80 65 20 69 73 20 54 52 55 45 20 69 66 20 74 68 65  e is TRUE if the
32d90 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65 20  .** expr on the 
32da0 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
32db0 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65 64  can be evaluated
32dc0 20 28 61 6e 64 20 74 68 75 73 20 74 68 65 20 63   (and thus the c
32dd0 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73 20  onstraint.** is 
32de0 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c 73  usable) and fals
32df0 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e 0a  e if it cannot..
32e00 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69  **.** The optimi
32e10 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  zer automaticall
32e20 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73 20  y inverts terms 
32e30 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78 70  of the form "exp
32e40 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20  r OP column".** 
32e50 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 20  and makes other 
32e60 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 20  simplifications 
32e70 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  to the WHERE cla
32e80 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d 70  use in an attemp
32e90 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d  t to.** get as m
32ea0 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73 65  any WHERE clause
32eb0 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 20   terms into the 
32ec0 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65  form shown above
32ed0 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a   as possible..**
32ee0 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74   The aConstraint
32ef0 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 65  [] array only re
32f00 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 75  ports WHERE clau
32f10 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20  se terms in the 
32f20 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d 20  correct.** form 
32f30 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68  that refer to th
32f40 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 69 72  e particular vir
32f50 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e 67  tual table being
32f60 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20   queried..**.** 
32f70 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
32f80 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  t the ORDER BY c
32f90 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65 64 20  lause is stored 
32fa0 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a  in aOrderBy[]..*
32fb0 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20 61  * Each term of a
32fc0 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 73 20  OrderBy records 
32fd0 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  a column of the 
32fe0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e  ORDER BY clause.
32ff0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65 73  .**.** The [xBes
33000 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20 6d  tIndex] method m
33010 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74 72  ust fill aConstr
33020 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 68  aintUsage[] with
33030 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
33040 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 61 6d  about what param
33050 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 74 6f  eters to pass to
33060 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61 72   xFilter.  If ar
33070 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a 2a  gvIndex>0 then.*
33080 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  * the right-hand
33090 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f 72   side of the cor
330a0 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e 73  responding aCons
330b0 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61 6c  traint[] is eval
330c0 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65 63  uated.** and bec
330d0 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e 64  omes the argvInd
330e0 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20 61  ex-th entry in a
330f0 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74 72  rgv.  If aConstr
33100 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 74  aintUsage[].omit
33110 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 65  .** is true, the
33120 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  n the constraint
33130 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62   is assumed to b
33140 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64 20  e fully handled 
33150 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61  by the.** virtua
33160 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20 6e  l table and is n
33170 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 69 6e  ot checked again
33180 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a   by SQLite..**.*
33190 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e 64  * The idxNum and
331a0 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20 61   idxPtr values a
331b0 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64 20  re recorded and 
331c0 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 0a  passed into the.
331d0 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65 74  ** [xFilter] met
331e0 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  hod..** [sqlite3
331f0 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73 65 64  _free()] is used
33200 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 72 20   to free idxPtr 
33210 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66 0a  if and only iff.
33220 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78  ** needToFreeIdx
33230 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a  Ptr is true..**.
33240 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43 6f  ** The orderByCo
33250 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68 61  nsumed means tha
33260 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b 78  t output from [x
33270 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d 20  Filter]/[xNext] 
33280 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a  will occur in.**
33290 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64   the correct ord
332a0 65 72 20 74 6f 20 73 61 74 69 73 66 79 20 74 68  er to satisfy th
332b0 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
332c0 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65 70  e so that no sep
332d0 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67  arate.** sorting
332e0 20 73 74 65 70 20 69 73 20 72 65 71 75 69 72 65   step is require
332f0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74  d..**.** The est
33300 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75 65  imatedCost value
33310 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20   is an estimate 
33320 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20 64  of the cost of d
33330 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 74  oing the.** part
33340 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20  icular lookup.  
33350 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61  A full scan of a
33360 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65 6e   table with N en
33370 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76  tries should hav
33380 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e  e.** a cost of N
33390 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65 61 72  .  A binary sear
333a0 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 66  ch of a table of
333b0 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75 6c   N entries shoul
333c0 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74  d have a.** cost
333d0 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65 6c   of approximatel
333e0 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72  y log(N)..*/.str
333f0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
33400 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e  x_info {.  /* In
33410 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  puts */.  int nC
33420 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20  onstraint;      
33430 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
33440 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f  f entries in aCo
33450 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73 74  nstraint */.  st
33460 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
33470 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a  ex_constraint {.
33480 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e       int iColumn
33490 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
334a0 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74  * Column on left
334b0 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63 6f  -hand side of co
334c0 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20  nstraint */.    
334d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f   unsigned char o
334e0 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  p;         /* Co
334f0 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74 6f  nstraint operato
33500 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e  r */.     unsign
33510 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b 20  ed char usable; 
33520 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
33530 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  his constraint i
33540 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20  s usable */.    
33550 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65 74   int iTermOffset
33560 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73  ;          /* Us
33570 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20  ed internally - 
33580 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75 6c  xBestIndex shoul
33590 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20  d ignore */.  } 
335a0 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20  *aConstraint;   
335b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
335c0 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75 73  e of WHERE claus
335d0 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f  e constraints */
335e0 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b  .  int nOrderBy;
335f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33600 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   Number of terms
33610 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59   in the ORDER BY
33620 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72   clause */.  str
33630 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
33640 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20 20  x_orderby {.    
33650 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
33660 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
33670 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  lumn number */. 
33680 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
33690 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f 2a  r desc;       /*
336a0 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e 20   True for DESC. 
336b0 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e 20   False for ASC. 
336c0 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42 79  */.  } *aOrderBy
336d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
336e0 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
336f0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f  clause */.  /* O
33700 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72 75  utputs */.  stru
33710 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
33720 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67  _constraint_usag
33730 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67 76  e {.    int argv
33740 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 20  Index;          
33750 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74   /* if >0, const
33760 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f 66  raint is part of
33770 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65 72   argv to xFilter
33780 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64   */.    unsigned
33790 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20 20   char omit;     
337a0 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20   /* Do not code 
337b0 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 73 20  a test for this 
337c0 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20  constraint */.  
337d0 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  } *aConstraintUs
337e0 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75  age;.  int idxNu
337f0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
33800 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65 64    /* Number used
33810 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65   to identify the
33820 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61 72   index */.  char
33830 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20 20   *idxStr;       
33840 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67         /* String
33850 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61 69  , possibly obtai
33860 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
33870 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74  _malloc */.  int
33880 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74   needToFreeIdxSt
33890 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20  r;      /* Free 
338a0 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 71 6c  idxStr using sql
338b0 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 20 74  ite3_free() if t
338c0 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64  rue */.  int ord
338d0 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20  erByConsumed;   
338e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6f      /* True if o
338f0 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 64 79  utput is already
33900 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f   ordered */.  do
33910 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43 6f  uble estimatedCo
33920 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74 69  st;      /* Esti
33930 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75 73  mated cost of us
33940 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a  ing this index *
33950 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c  /.};.#define SQL
33960 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
33970 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64 65  AINT_EQ    2.#de
33980 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  fine SQLITE_INDE
33990 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20  X_CONSTRAINT_GT 
339a0 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c     4.#define SQL
339b0 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
339c0 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64 65  AINT_LE    8.#de
339d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  fine SQLITE_INDE
339e0 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20  X_CONSTRAINT_LT 
339f0 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51     16.#define SQ
33a00 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
33a10 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a 23  RAINT_GE    32.#
33a20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
33a30 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d  DEX_CONSTRAINT_M
33a40 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43  ATCH 64../*.** C
33a50 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65  API3REF: Registe
33a60 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c  r A Virtual Tabl
33a70 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e Implementation
33a80 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34 30   {H18200} <S2040
33a90 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
33aa0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  AL.**.** This ro
33ab0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f  utine is used to
33ac0 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 77 20   register a new 
33ad0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d  [virtual table m
33ae0 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20  odule] name..** 
33af0 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73  Module names mus
33b00 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20  t be registered 
33b10 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69  before.** creati
33b20 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61  ng a new [virtua
33b30 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20 74  l table] using t
33b40 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65  he module, or be
33b50 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a 20  fore using a.** 
33b60 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69 72  preexisting [vir
33b70 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72 20  tual table] for 
33b80 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a  the module..**.*
33b90 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d  * The module nam
33ba0 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20  e is registered 
33bb0 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  on the [database
33bc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65   connection] spe
33bd0 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65  cified.** by the
33be0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
33bf0 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  .  The name of t
33c00 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69 76  he module is giv
33c10 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73 65  en by the .** se
33c20 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20  cond parameter. 
33c30 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   The third param
33c40 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65  eter is a pointe
33c50 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 6c  r to.** the impl
33c60 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
33c70 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e [virtual table
33c80 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65 20   module].   The 
33c90 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d 65  fourth.** parame
33ca0 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72  ter is an arbitr
33cb0 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61 20  ary client data 
33cc0 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20  pointer that is 
33cd0 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a  passed through.*
33ce0 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72 65  * into the [xCre
33cf0 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65  ate] and [xConne
33d00 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 74  ct] methods of t
33d10 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
33d20 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e 20   module.** when 
33d30 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61  a new virtual ta
33d40 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67 20  ble is be being 
33d50 63 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e 69  created or reini
33d60 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20  tialized..**.** 
33d70 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 68  This interface h
33d80 61 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73  as exactly the s
33d90 61 6d 65 20 65 66 66 65 63 74 20 61 73 20 63 61  ame effect as ca
33da0 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65  lling.** [sqlite
33db0 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
33dc0 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55 4c  v2()] with a NUL
33dd0 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64 65  L client data de
33de0 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c  structor..*/.SQL
33df0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45  ITE_API SQLITE_E
33e00 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20  XPERIMENTAL int 
33e10 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
33e20 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  odule(.  sqlite3
33e30 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
33e40 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f      /* SQLite co
33e50 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69  nnection to regi
33e60 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68  ster module with
33e70 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
33e80 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
33e90 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
33ea0 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
33eb0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
33ec0 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64   *p,   /* Method
33ed0 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65  s for the module
33ee0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69   */.  void *pCli
33ef0 65 6e 74 44 61 74 61 20 20 20 20 20 20 20 20 20  entData         
33f00 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20   /* Client data 
33f10 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
33f20 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a  nect */.);../*.*
33f30 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69  * CAPI3REF: Regi
33f40 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54  ster A Virtual T
33f50 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74  able Implementat
33f60 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53 32  ion {H18210} <S2
33f70 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0400>.** EXPERIM
33f80 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ENTAL.**.** This
33f90 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65 6e   routine is iden
33fa0 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73 71  tical to the [sq
33fb0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
33fc0 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a  ule()] method,.*
33fd0 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  * except that it
33fe0 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 70 61   has an extra pa
33ff0 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63 69  rameter to speci
34000 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75 63  fy .** a destruc
34010 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  tor function for
34020 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74 61   the client data
34030 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69 74   pointer.  SQLit
34040 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65  e will.** invoke
34050 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
34060 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 20  function (if it 
34070 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65  is not NULL) whe
34080 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c  n SQLite.** no l
34090 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65 20  onger needs the 
340a0 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 6e  pClientData poin
340b0 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45  ter.  .*/.SQLITE
340c0 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
340d0 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c  RIMENTAL int sql
340e0 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
340f0 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  le_v2(.  sqlite3
34100 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
34110 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f      /* SQLite co
34120 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69  nnection to regi
34130 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68  ster module with
34140 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
34150 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
34160 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
34170 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
34180 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
34190 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64   *p,   /* Method
341a0 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65  s for the module
341b0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69   */.  void *pCli
341c0 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 20  entData,        
341d0 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20   /* Client data 
341e0 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
341f0 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a  nect */.  void(*
34200 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29  xDestroy)(void*)
34210 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64       /* Module d
34220 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
34230 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  on */.);../*.** 
34240 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
34250 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 65  l Table Instance
34260 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30 7d   Object {H18010}
34270 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59   <S20400>.** KEY
34280 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76  WORDS: sqlite3_v
34290 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  tab.** EXPERIMEN
342a0 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20  TAL.**.** Every 
342b0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d  [virtual table m
342c0 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74  odule] implement
342d0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62  ation uses a sub
342e0 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65 20  class.** of the 
342f0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
34300 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20  ure to describe 
34310 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 73  a particular ins
34320 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20  tance.** of the 
34330 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e  [virtual table].
34340 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20    Each subclass 
34350 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f  will.** be tailo
34360 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69  red to the speci
34370 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65  fic needs of the
34380 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
34390 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70  tation..** The p
343a0 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73  urpose of this s
343b0 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20  uperclass is to 
343c0 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66  define certain f
343d0 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a  ields that are.*
343e0 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20  * common to all 
343f0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
34400 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69  ations..**.** Vi
34410 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74  rtual tables met
34420 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20  hods can set an 
34430 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79  error message by
34440 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20   assigning a.** 
34450 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20  string obtained 
34460 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70  from [sqlite3_mp
34470 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72  rintf()] to zErr
34480 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64  Msg.  The method
34490 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20   should.** take 
344a0 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72  care that any pr
344b0 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72  ior string is fr
344c0 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  eed by a call to
344d0 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
344e0 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73  ].** prior to as
344f0 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74  signing a new st
34500 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e  ring to zErrMsg.
34510 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72 6f    After the erro
34520 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20  r message.** is 
34530 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20  delivered up to 
34540 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69  the client appli
34550 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69  cation, the stri
34560 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d  ng will be autom
34570 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65  atically.** free
34580 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65  d by sqlite3_fre
34590 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72  e() and the zErr
345a0 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62  Msg field will b
345b0 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72  e zeroed..*/.str
345c0 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62  uct sqlite3_vtab
345d0 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74   {.  const sqlit
345e0 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
345f0 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75  le;  /* The modu
34600 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 72 74  le for this virt
34610 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  ual table */.  i
34620 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
34630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34640 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c  * Used internall
34650 79 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  y */.  char *zEr
34660 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20  rMsg;           
34670 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20         /* Error 
34680 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c  message from sql
34690 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a  ite3_mprintf() *
346a0 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74  /.  /* Virtual t
346b0 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  able implementat
346c0 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61  ions will typica
346d0 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e  lly add addition
346e0 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a  al fields */.};.
346f0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
34700 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43   Virtual Table C
34710 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48  ursor Object  {H
34720 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18020} <S20400>.
34730 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c  ** KEYWORDS: sql
34740 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
34750 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   {virtual table 
34760 63 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52  cursor}.** EXPER
34770 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76  IMENTAL.**.** Ev
34780 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62  ery [virtual tab
34790 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65  le module] imple
347a0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61  mentation uses a
347b0 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65   subclass of the
347c0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  .** following st
347d0 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72  ructure to descr
347e0 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74  ibe cursors that
347f0 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a   point into the.
34800 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  ** [virtual tabl
34810 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a  e] and are used.
34820 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75  ** to loop throu
34830 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  gh the virtual t
34840 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61  able.  Cursors a
34850 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  re created using
34860 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
34870 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20  _module.xOpen | 
34880 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66  xOpen] method of
34890 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20   the module and 
348a0 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a  are destroyed.**
348b0 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33   by the [sqlite3
348c0 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c  _module.xClose |
348d0 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e   xClose] method.
348e0 20 20 43 75 73 73 6f 72 73 20 61 72 65 20 75 73    Cussors are us
348f0 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46  ed.** by the [xF
34900 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c  ilter], [xNext],
34910 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d   [xEof], [xColum
34920 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d  n], and [xRowid]
34930 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74   methods.** of t
34940 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68  he module.  Each
34950 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
34960 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69  tation will defi
34970 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  ne.** the conten
34980 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74  t of a cursor st
34990 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20  ructure to suit 
349a0 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a  its own needs..*
349b0 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63  *.** This superc
349c0 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f  lass exists in o
349d0 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66  rder to define f
349e0 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72  ields of the cur
349f0 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20  sor that.** are 
34a00 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d  common to all im
34a10 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  plementations..*
34a20 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
34a30 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20  _vtab_cursor {. 
34a40 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
34a50 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69  Vtab;      /* Vi
34a60 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74  rtual table of t
34a70 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  his cursor */.  
34a80 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
34a90 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
34aa0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
34ab0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
34ac0 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ields */.};../*.
34ad0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63  ** CAPI3REF: Dec
34ae0 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20  lare The Schema 
34af0 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62  Of A Virtual Tab
34b00 6c 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30  le {H18280} <S20
34b10 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  400>.** EXPERIME
34b20 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  NTAL.**.** The [
34b30 78 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43  xCreate] and [xC
34b40 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20  onnect] methods 
34b50 6f 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c  of a.** [virtual
34b60 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63   table module] c
34b70 61 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61  all this interfa
34b80 63 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65  ce.** to declare
34b90 20 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65   the format (the
34ba0 20 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74   names and datat
34bb0 79 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75  ypes of the colu
34bc0 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76  mns) of.** the v
34bd0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68  irtual tables th
34be0 65 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f  ey implement..*/
34bf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
34c00 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
34c10 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  int sqlite3_decl
34c20 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33  are_vtab(sqlite3
34c30 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
34c40 53 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  SQL);../*.** CAP
34c50 49 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20  I3REF: Overload 
34c60 41 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41  A Function For A
34c70 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b   Virtual Table {
34c80 48 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e  H18300} <S20400>
34c90 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
34ca0 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74  .**.** Virtual t
34cb0 61 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64  ables can provid
34cc0 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d  e alternative im
34cd0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
34ce0 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73   functions.** us
34cf0 69 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75  ing the [xFindFu
34d00 6e 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f  nction] method o
34d10 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74  f the [virtual t
34d20 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a  able module].  .
34d30 2a 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65  ** But global ve
34d40 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20  rsions of those 
34d50 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73  functions.** mus
34d60 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72  t exist in order
34d70 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65   to be overloade
34d80 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50  d..**.** This AP
34d90 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67  I makes sure a g
34da0 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66  lobal version of
34db0 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
34dc0 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
34dd0 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72   name and number
34de0 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65   of parameters e
34df0 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75  xists.  If no su
34e00 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73  ch function exis
34e10 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69  ts.** before thi
34e20 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c  s API is called,
34e30 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20   a new function 
34e40 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65  is created.  The
34e50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
34e60 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75  ** of the new fu
34e70 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61  nction always ca
34e80 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f  uses an exceptio
34e90 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20  n to be thrown. 
34ea0 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66   So.** the new f
34eb0 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67  unction is not g
34ec0 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67  ood for anything
34ed0 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73   by itself.  Its
34ee0 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65   only.** purpose
34ef0 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63   is to be a plac
34f00 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e  eholder function
34f10 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65   that can be ove
34f20 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20  rloaded.** by a 
34f30 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e  [virtual table].
34f40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
34f50 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
34f60 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  AL int sqlite3_o
34f70 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
34f80 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74  (sqlite3*, const
34f90 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65   char *zFuncName
34fa0 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a  , int nArg);../*
34fb0 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63  .** The interfac
34fc0 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c  e to the virtual
34fd0 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d  -table mechanism
34fe0 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28   defined above (
34ff0 62 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20  back up.** to a 
35000 63 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62  comment remarkab
35010 6c 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68  ly similar to th
35020 69 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65  is one) is curre
35030 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a  ntly considered.
35040 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d  ** to be experim
35050 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65  ental.  The inte
35060 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e  rface might chan
35070 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62  ge in incompatib
35080 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74  le ways..** If t
35090 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d  his is a problem
350a0 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74   for you, do not
350b0 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61   use the interfa
350c0 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e  ce at this time.
350d0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
350e0 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65  virtual-table me
350f0 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a  chanism stabiliz
35100 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c  es, we will decl
35110 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72  are the.** inter
35120 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70  face fixed, supp
35130 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74  ort it indefinit
35140 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20  ely, and remove 
35150 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a  this comment..**
35160 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45  .****** EXPERIME
35170 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74  NTAL - subject t
35180 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74  o change without
35190 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a   notice ********
351a0 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ******.*/../*.**
351b0 20 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e   CAPI3REF: A Han
351c0 64 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42  dle To An Open B
351d0 4c 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33  LOB {H17800} <S3
351e0 30 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0230>.** KEYWORD
351f0 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d  S: {BLOB handle}
35200 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a   {BLOB handles}.
35210 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  **.** An instanc
35220 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
35230 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f   represents an o
35240 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63  pen BLOB on whic
35250 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c  h.** [sqlite3_bl
35260 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d  ob_open | increm
35270 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20  ental BLOB I/O] 
35280 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64  can be performed
35290 2e 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20  ..** Objects of 
352a0 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 72  this type are cr
352b0 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65  eated by [sqlite
352c0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a  3_blob_open()].*
352d0 2a 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20  * and destroyed 
352e0 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  by [sqlite3_blob
352f0 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68  _close()]..** Th
35300 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
35310 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c  read()] and [sql
35320 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
35330 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  )] interfaces.**
35340 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
35350 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d  read or write sm
35360 61 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20  all subsections 
35370 6f 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20  of the BLOB..** 
35380 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  The [sqlite3_blo
35390 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72  b_bytes()] inter
353a0 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65  face returns the
353b0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f   size of the BLO
353c0 42 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74  B in bytes..*/.t
353d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
353e0 6c 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74  lite3_blob sqlit
353f0 65 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20  e3_blob;../*.** 
35400 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41  CAPI3REF: Open A
35410 20 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d   BLOB For Increm
35420 65 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31  ental I/O {H1781
35430 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a  0} <S30230>.**.*
35440 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
35450 73 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20  s opens a [BLOB 
35460 68 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d  handle | handle]
35470 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63   to the BLOB loc
35480 61 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69  ated.** in row i
35490 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c  Row, column zCol
354a0 75 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c  umn, table zTabl
354b0 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44  e in database zD
354c0 62 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77  b;.** in other w
354d0 6f 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42  ords, the same B
354e0 4c 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62  LOB that would b
354f0 65 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a  e selected by:.*
35500 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20  *.** <pre>.**   
35510 20 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e    SELECT zColumn
35520 20 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65   FROM zDb.zTable
35530 20 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d   WHERE [rowid] =
35540 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e   iRow;.** </pre>
35550 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20   {END}.**.** If 
35560 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
35570 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  ter is non-zero,
35580 20 74 68 65 20 74 68 65 20 42 4c 4f 42 20 69 73   the the BLOB is
35590 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64   opened for read
355a0 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61 63  .** and write ac
355b0 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20 7a  cess. If it is z
355c0 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69 73  ero, the BLOB is
355d0 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64   opened for read
355e0 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4e   access..**.** N
355f0 6f 74 65 20 74 68 61 74 20 74 68 65 20 64 61 74  ote that the dat
35600 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f  abase name is no
35610 74 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74  t the filename t
35620 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  hat contains.** 
35630 74 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74  the database but
35640 20 72 61 74 68 65 72 20 74 68 65 20 73 79 6d 62   rather the symb
35650 6f 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65  olic name of the
35660 20 64 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a   database that.*
35670 2a 20 69 73 20 61 73 73 69 67 6e 65 64 20 77 68  * is assigned wh
35680 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
35690 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69  is connected usi
356a0 6e 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20  ng [ATTACH]..** 
356b0 46 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  For the main dat
356c0 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20  abase file, the 
356d0 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73  database name is
356e0 20 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20   "main"..** For 
356f0 54 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65  TEMP tables, the
35700 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69   database name i
35710 73 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20  s "temp"..**.** 
35720 4f 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c  On success, [SQL
35730 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72  ITE_OK] is retur
35740 6e 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20  ned and the new 
35750 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73  [BLOB handle] is
35760 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a   written.** to *
35770 70 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73  ppBlob. Otherwis
35780 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  e an [error code
35790 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
357a0 64 20 61 6e 79 20 76 61 6c 75 65 20 77 72 69 74  d any value writ
357b0 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f  ten.** to *ppBlo
357c0 62 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  b should not be 
357d0 75 73 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  used by the call
357e0 65 72 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  er..** This func
357f0 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 5b 64  tion sets the [d
35800 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
35810 6f 6e 5d 20 65 72 72 6f 72 20 63 6f 64 65 20 61  on] error code a
35820 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a 20 61 63  nd message.** ac
35830 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b 73 71  cessible via [sq
35840 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d  lite3_errcode()]
35850 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72   and [sqlite3_er
35860 72 6d 73 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49  rmsg()]..**.** I
35870 66 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 61  f the row that a
35880 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 70 6f 69   BLOB handle poi
35890 6e 74 73 20 74 6f 20 69 73 20 6d 6f 64 69 66 69  nts to is modifi
358a0 65 64 20 62 79 20 61 6e 0a 2a 2a 20 5b 55 50 44  ed by an.** [UPD
358b0 41 54 45 5d 2c 20 5b 44 45 4c 45 54 45 5d 2c 20  ATE], [DELETE], 
358c0 6f 72 20 62 79 20 5b 4f 4e 20 43 4f 4e 46 4c 49  or by [ON CONFLI
358d0 43 54 5d 20 73 69 64 65 2d 65 66 66 65 63 74 73  CT] side-effects
358e0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 42 4c 4f  .** then the BLO
358f0 42 20 68 61 6e 64 6c 65 20 69 73 20 6d 61 72 6b  B handle is mark
35900 65 64 20 61 73 20 22 65 78 70 69 72 65 64 22 2e  ed as "expired".
35910 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 72 75 65  .** This is true
35920 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f   if any column o
35930 66 20 74 68 65 20 72 6f 77 20 69 73 20 63 68 61  f the row is cha
35940 6e 67 65 64 2c 20 65 76 65 6e 20 61 20 63 6f 6c  nged, even a col
35950 75 6d 6e 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61  umn.** other tha
35960 6e 20 74 68 65 20 6f 6e 65 20 74 68 65 20 42 4c  n the one the BL
35970 4f 42 20 68 61 6e 64 6c 65 20 69 73 20 6f 70 65  OB handle is ope
35980 6e 20 6f 6e 2e 0a 2a 2a 20 43 61 6c 6c 73 20 74  n on..** Calls t
35990 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  o [sqlite3_blob_
359a0 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c  read()] and [sql
359b0 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
359c0 29 5d 20 66 6f 72 0a 2a 2a 20 61 20 65 78 70 69  )] for.** a expi
359d0 72 65 64 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20  red BLOB handle 
359e0 66 61 69 6c 20 77 69 74 68 20 61 6e 20 72 65 74  fail with an ret
359f0 75 72 6e 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c  urn code of [SQL
35a00 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 20 43  ITE_ABORT]..** C
35a10 68 61 6e 67 65 73 20 77 72 69 74 74 65 6e 20 69  hanges written i
35a20 6e 74 6f 20 61 20 42 4c 4f 42 20 70 72 69 6f 72  nto a BLOB prior
35a30 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 65 78 70   to the BLOB exp
35a40 69 72 69 6e 67 20 61 72 65 20 6e 6f 74 0a 2a 2a  iring are not.**
35a50 20 72 6f 6c 6c 62 61 63 6b 20 62 79 20 74 68 65   rollback by the
35a60 20 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74   expiration of t
35a70 68 65 20 42 4c 4f 42 2e 20 20 53 75 63 68 20 63  he BLOB.  Such c
35a80 68 61 6e 67 65 73 20 77 69 6c 6c 20 65 76 65 6e  hanges will even
35a90 74 75 61 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74  tually.** commit
35aa0 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74   if the transact
35ab0 69 6f 6e 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f  ion continues to
35ac0 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a   completion..**.
35ad0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
35ae0 0a 2a 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31  .** [H17813] [H1
35af0 37 38 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b  7814] [H17816] [
35b00 48 31 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d  H17819] [H17821]
35b10 20 5b 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c   [H17824].*/.SQL
35b20 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
35b30 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20  te3_blob_open(. 
35b40 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e   sqlite3*,.  con
35b50 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20  st char *zDb,.  
35b60 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
35b70 6c 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  le,.  const char
35b80 20 2a 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c   *zColumn,.  sql
35b90 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c  ite3_int64 iRow,
35ba0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20  .  int flags,.  
35bb0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70  sqlite3_blob **p
35bc0 70 42 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  pBlob.);../*.** 
35bd0 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20  CAPI3REF: Close 
35be0 41 20 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48  A BLOB Handle {H
35bf0 31 37 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a  17830} <S30230>.
35c00 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20  **.** Closes an 
35c10 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c  open [BLOB handl
35c20 65 5d 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e  e]..**.** Closin
35c30 67 20 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63  g a BLOB shall c
35c40 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74  ause the current
35c50 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20   transaction to 
35c60 63 6f 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65  commit.** if the
35c70 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20  re are no other 
35c80 42 4c 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e  BLOBs, no pendin
35c90 67 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  g prepared state
35ca0 6d 65 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a  ments, and the.*
35cb0 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
35cc0 63 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74  ction is in [aut
35cd0 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a  ocommit mode]..*
35ce0 2a 20 49 66 20 61 6e 79 20 77 72 69 74 65 73 20  * If any writes 
35cf0 77 65 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65  were made to the
35d00 20 42 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68   BLOB, they migh
35d10 74 20 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63  t be held in cac
35d20 68 65 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20  he.** until the 
35d30 63 6c 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20  close operation 
35d40 69 66 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74  if they will fit
35d50 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 43 6c  . {END}.**.** Cl
35d60 6f 73 69 6e 67 20 74 68 65 20 42 4c 4f 42 20 6f  osing the BLOB o
35d70 66 74 65 6e 20 66 6f 72 63 65 73 20 74 68 65 20  ften forces the 
35d80 63 68 61 6e 67 65 73 0a 2a 2a 20 6f 75 74 20 74  changes.** out t
35d90 6f 20 64 69 73 6b 20 61 6e 64 20 73 6f 20 69 66  o disk and so if
35da0 20 61 6e 79 20 49 2f 4f 20 65 72 72 6f 72 73 20   any I/O errors 
35db0 6f 63 63 75 72 2c 20 74 68 65 79 20 77 69 6c 6c  occur, they will
35dc0 20 6c 69 6b 65 6c 79 20 6f 63 63 75 72 0a 2a 2a   likely occur.**
35dd0 20 61 74 20 74 68 65 20 74 69 6d 65 20 77 68 65   at the time whe
35de0 6e 20 74 68 65 20 42 4c 4f 42 20 69 73 20 63 6c  n the BLOB is cl
35df0 6f 73 65 64 2e 20 20 7b 48 31 37 38 33 33 7d 20  osed.  {H17833} 
35e00 41 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74 20  Any errors that 
35e10 6f 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20  occur during.** 
35e20 63 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f  closing are repo
35e30 72 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65  rted as a non-ze
35e40 72 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e  ro return value.
35e50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20  .**.** The BLOB 
35e60 69 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64  is closed uncond
35e70 69 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e  itionally.  Even
35e80 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   if this routine
35e90 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65   returns.** an e
35ea0 72 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42  rror code, the B
35eb0 4c 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f  LOB is still clo
35ec0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  sed..**.** Requi
35ed0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37  rements:.** [H17
35ee0 38 33 33 5d 20 5b 48 31 37 38 33 36 5d 20 5b 48  833] [H17836] [H
35ef0 31 37 38 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45  17839].*/.SQLITE
35f00 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
35f10 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69  _blob_close(sqli
35f20 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a  te3_blob *);../*
35f30 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
35f40 74 75 72 6e 20 54 68 65 20 53 69 7a 65 20 4f 66  turn The Size Of
35f50 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48   An Open BLOB {H
35f60 31 37 38 34 30 7d 20 3c 53 33 30 32 33 30 3e 0a  17840} <S30230>.
35f70 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74 68  **.** Returns th
35f80 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  e size in bytes 
35f90 6f 66 20 74 68 65 20 42 4c 4f 42 20 61 63 63 65  of the BLOB acce
35fa0 73 73 69 62 6c 65 20 76 69 61 20 74 68 65 20 6f  ssible via the o
35fb0 70 65 6e 0a 2a 2a 20 5b 5d 42 4c 4f 42 20 68 61  pen.** []BLOB ha
35fc0 6e 64 6c 65 5d 20 69 6e 20 69 74 73 20 6f 6e 6c  ndle] in its onl
35fd0 79 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  y argument..**.*
35fe0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
35ff0 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a 2f 0a 53  ** [H17843].*/.S
36000 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
36010 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73  lite3_blob_bytes
36020 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29  (sqlite3_blob *)
36030 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
36040 46 3a 20 52 65 61 64 20 44 61 74 61 20 46 72 6f  F: Read Data Fro
36050 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65  m A BLOB Increme
36060 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35 30 7d 20  ntally {H17850} 
36070 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S30230>.**.** T
36080 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
36090 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 61 74  used to read dat
360a0 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b  a from an open [
360b0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 74  BLOB handle] int
360c0 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75  o a.** caller-su
360d0 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e 20 4e  pplied buffer. N
360e0 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 61   bytes of data a
360f0 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62  re copied into b
36100 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20  uffer Z.** from 
36110 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73  the open BLOB, s
36120 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65  tarting at offse
36130 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a  t iOffset..**.**
36140 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73   If offset iOffs
36150 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  et is less than 
36160 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65  N bytes from the
36170 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42   end of the BLOB
36180 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52  ,.** [SQLITE_ERR
36190 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  OR] is returned 
361a0 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72  and no data is r
361b0 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 20 69 4f  ead.  If N or iO
361c0 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73 73  ffset is.** less
361d0 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51 4c   than zero, [SQL
361e0 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65  ITE_ERROR] is re
361f0 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61  turned and no da
36200 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a 0a 2a  ta is read..**.*
36210 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  * An attempt to 
36220 72 65 61 64 20 66 72 6f 6d 20 61 6e 20 65 78 70  read from an exp
36230 69 72 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c  ired [BLOB handl
36240 65 5d 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e  e] fails with an
36250 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f  .** error code o
36260 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d  f [SQLITE_ABORT]
36270 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65  ..**.** On succe
36280 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  ss, SQLITE_OK is
36290 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74   returned..** Ot
362a0 68 65 72 77 69 73 65 2c 20 61 6e 20 5b 65 72 72  herwise, an [err
362b0 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b  or code] or an [
362c0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
362d0 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
362e0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
362f0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 35 33  ents:.** [H17853
36300 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48 31 37 38  ] [H17856] [H178
36310 35 39 5d 20 5b 48 31 37 38 36 32 5d 20 5b 48 31  59] [H17862] [H1
36320 37 38 36 33 5d 20 5b 48 31 37 38 36 35 5d 20 5b  7863] [H17865] [
36330 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54  H17868].*/.SQLIT
36340 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
36350 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69  3_blob_read(sqli
36360 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69 64  te3_blob *, void
36370 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e 74 20   *Z, int N, int 
36380 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a  iOffset);../*.**
36390 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 74 65   CAPI3REF: Write
363a0 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42 4c 4f   Data Into A BLO
363b0 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20  B Incrementally 
363c0 7b 48 31 37 38 37 30 7d 20 3c 53 33 30 32 33 30  {H17870} <S30230
363d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  >.**.** This fun
363e0 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
363f0 20 77 72 69 74 65 20 64 61 74 61 20 69 6e 74 6f   write data into
36400 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68   an open [BLOB h
36410 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a  andle] from a.**
36420 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64   caller-supplied
36430 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73   buffer. N bytes
36440 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70   of data are cop
36450 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 66  ied from the buf
36460 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74 68  fer Z.** into th
36470 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61  e open BLOB, sta
36480 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20  rting at offset 
36490 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49  iOffset..**.** I
364a0 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64  f the [BLOB hand
364b0 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20 74 68  le] passed as th
364c0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
364d0 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20   was not opened 
364e0 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 28  for.** writing (
364f0 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
36500 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ter to [sqlite3_
36510 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 61 73  blob_open()] was
36520 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 73 20   zero),.** this 
36530 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
36540 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c   [SQLITE_READONL
36550 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  Y]..**.** This f
36560 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79  unction may only
36570 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f 6e 74   modify the cont
36580 65 6e 74 73 20 6f 66 20 74 68 65 20 42 4c 4f 42  ents of the BLOB
36590 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 70  ; it is.** not p
365a0 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63 72 65  ossible to incre
365b0 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ase the size of 
365c0 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 74 68 69  a BLOB using thi
365d0 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 6f 66 66  s API..** If off
365e0 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c  set iOffset is l
365f0 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73  ess than N bytes
36600 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66   from the end of
36610 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53   the BLOB,.** [S
36620 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20  QLITE_ERROR] is 
36630 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
36640 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e  data is written.
36650 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20 6c 65 73    If N is.** les
36660 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53 51 4c  s than zero [SQL
36670 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65  ITE_ERROR] is re
36680 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61  turned and no da
36690 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e 0a 2a  ta is written..*
366a0 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 74 20  *.** An attempt 
366b0 74 6f 20 77 72 69 74 65 20 74 6f 20 61 6e 20 65  to write to an e
366c0 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 68 61 6e  xpired [BLOB han
366d0 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 74 68 20  dle] fails with 
366e0 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  an.** error code
366f0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52   of [SQLITE_ABOR
36700 54 5d 2e 20 20 57 72 69 74 65 73 20 74 6f 20 74  T].  Writes to t
36710 68 65 20 42 4c 4f 42 20 74 68 61 74 20 6f 63 63  he BLOB that occ
36720 75 72 72 65 64 0a 2a 2a 20 62 65 66 6f 72 65 20  urred.** before 
36730 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  the [BLOB handle
36740 5d 20 65 78 70 69 72 65 64 20 61 72 65 20 6e 6f  ] expired are no
36750 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79  t rolled back by
36760 20 74 68 65 0a 2a 2a 20 65 78 70 69 72 61 74 69   the.** expirati
36770 6f 6e 20 6f 66 20 74 68 65 20 68 61 6e 64 6c 65  on of the handle
36780 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72  , though of cour
36790 73 65 20 74 68 6f 73 65 20 63 68 61 6e 67 65 73  se those changes
367a0 20 6d 69 67 68 74 0a 2a 2a 20 68 61 76 65 20 62   might.** have b
367b0 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 20  een overwritten 
367c0 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  by the statement
367d0 20 74 68 61 74 20 65 78 70 69 72 65 64 20 74 68   that expired th
367e0 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 0a 2a 2a  e BLOB handle.**
367f0 20 6f 72 20 62 79 20 6f 74 68 65 72 20 69 6e 64   or by other ind
36800 65 70 65 6e 64 65 6e 74 20 73 74 61 74 65 6d 65  ependent stateme
36810 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75  nts..**.** On su
36820 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b  ccess, SQLITE_OK
36830 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
36840 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 20   Otherwise, an  
36850 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20  [error code] or 
36860 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72  an [extended err
36870 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
36880 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  rned..**.** Requ
36890 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
368a0 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d 20 5b  7873] [H17874] [
368b0 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37 36 5d  H17875] [H17876]
368c0 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37 38 37   [H17877] [H1787
368d0 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48 31 37  9] [H17882] [H17
368e0 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38 38 5d  885].** [H17888]
368f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
36900 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
36910 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c  write(sqlite3_bl
36920 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ob *, const void
36930 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20   *z, int n, int 
36940 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a  iOffset);../*.**
36950 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75   CAPI3REF: Virtu
36960 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20 4f  al File System O
36970 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 7d 20  bjects {H11200} 
36980 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41  <S20100>.**.** A
36990 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 79 73   virtual filesys
369a0 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 6e 20  tem (VFS) is an 
369b0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
369c0 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c  ject.** that SQL
369d0 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e 74 65  ite uses to inte
369e0 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 68 65  ract.** with the
369f0 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72   underlying oper
36a00 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d  ating system.  M
36a10 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 6c 64  ost SQLite build
36a20 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a  s come with a.**
36a30 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 20   single default 
36a40 56 46 53 20 74 68 61 74 20 69 73 20 61 70 70 72  VFS that is appr
36a50 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20  opriate for the 
36a60 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a  host computer..*
36a70 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e 20  * New VFSes can 
36a80 62 65 20 72 65 67 69 73 74 65 72 65 64 20 61 6e  be registered an
36a90 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 65 73  d existing VFSes
36aa0 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73 74   can be unregist
36ab0 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c  ered..** The fol
36ac0 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65  lowing interface
36ad0 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e 0a  s are provided..
36ae0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
36af0 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e 74  3_vfs_find() int
36b00 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
36b10 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56 46   pointer to a VF
36b20 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65  S given its name
36b30 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 63  ..** Names are c
36b40 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a  ase sensitive..*
36b50 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 72 6f  * Names are zero
36b60 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
36b70 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 66  8 strings..** If
36b80 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74   there is no mat
36b90 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ch, a NULL point
36ba0 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  er is returned..
36bb0 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 20 69  ** If zVfsName i
36bc0 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20  s NULL then the 
36bd0 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 72  default VFS is r
36be0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e  eturned..**.** N
36bf0 65 77 20 56 46 53 65 73 20 61 72 65 20 72 65 67  ew VFSes are reg
36c00 69 73 74 65 72 65 64 20 77 69 74 68 20 73 71 6c  istered with sql
36c10 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
36c20 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77  r()..** Each new
36c30 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 65   VFS becomes the
36c40 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 66 20   default VFS if 
36c50 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61  the makeDflt fla
36c60 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 68 65  g is set..** The
36c70 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 62 65   same VFS can be
36c80 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 6c 74   registered mult
36c90 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 6f  iple times witho
36ca0 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f  ut injury..** To
36cb0 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 69 6e   make an existin
36cc0 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 20 64  g VFS into the d
36cd0 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 67 69  efault VFS, regi
36ce0 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a 2a 2a  ster it again.**
36cf0 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 44 66   with the makeDf
36d00 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 49 66  lt flag set.  If
36d10 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 20 56   two different V
36d20 46 53 65 73 20 77 69 74 68 20 74 68 65 0a 2a 2a  FSes with the.**
36d30 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 20 72   same name are r
36d40 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 62  egistered, the b
36d50 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
36d60 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a 20 56  ined.  If a.** V
36d70 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64  FS is registered
36d80 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 68 61   with a name tha
36d90 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20  t is NULL or an 
36da0 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a 2a 2a  empty string,.**
36db0 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69   then the behavi
36dc0 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  or is undefined.
36dd0 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65  .**.** Unregiste
36de0 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65  r a VFS with the
36df0 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72   sqlite3_vfs_unr
36e00 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 72 66  egister() interf
36e10 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64  ace..** If the d
36e20 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 75 6e  efault VFS is un
36e30 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e 6f 74  registered, anot
36e40 68 65 72 20 56 46 53 20 69 73 20 63 68 6f 73 65  her VFS is chose
36e50 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66 61  n as.** the defa
36e60 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 63 65  ult.  The choice
36e70 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 46 53   for the new VFS
36e80 20 69 73 20 61 72 62 69 74 72 61 72 79 2e 0a 2a   is arbitrary..*
36e90 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
36ea0 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d 20 5b  s:.** [H11203] [
36eb0 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30 39 5d  H11206] [H11209]
36ec0 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31 32 31   [H11212] [H1121
36ed0 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a 53  5] [H11218].*/.S
36ee0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
36ef0 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76  3_vfs *sqlite3_v
36f00 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68  fs_find(const ch
36f10 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53  ar *zVfsName);.S
36f20 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
36f30 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
36f40 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  er(sqlite3_vfs*,
36f50 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a   int makeDflt);.
36f60 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
36f70 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67  qlite3_vfs_unreg
36f80 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66  ister(sqlite3_vf
36f90 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  s*);../*.** CAPI
36fa0 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b 48  3REF: Mutexes {H
36fb0 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a  17000} <S20000>.
36fc0 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65  **.** The SQLite
36fd0 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 65   core uses these
36fe0 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 68   routines for th
36ff0 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e  read.** synchron
37000 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20  ization. Though 
37010 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 65  they are intende
37020 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a  d for internal.*
37030 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2c  * use by SQLite,
37040 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b 73   code that links
37050 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 20   against SQLite 
37060 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 20  is.** permitted 
37070 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68  to use any of th
37080 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ese routines..**
37090 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 73  .** The SQLite s
370a0 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 61  ource code conta
370b0 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70  ins multiple imp
370c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20  lementations.** 
370d0 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 72  of these mutex r
370e0 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 70  outines.  An app
370f0 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d 65  ropriate impleme
37100 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65  ntation.** is se
37110 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 63  lected automatic
37120 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d  ally at compile-
37130 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  time.  The follo
37140 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  wing.** implemen
37150 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69  tations are avai
37160 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c  lable in the SQL
37170 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20  ite core:.**.** 
37180 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53  <ul>.** <li>   S
37190 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a  QLITE_MUTEX_OS2.
371a0 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45  ** <li>   SQLITE
371b0 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a 2a  _MUTEX_PTHREAD.*
371c0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f  * <li>   SQLITE_
371d0 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69  MUTEX_W32.** <li
371e0 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  >   SQLITE_MUTEX
371f0 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  _NOOP.** </ul>.*
37200 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
37210 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65  MUTEX_NOOP imple
37220 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 73  mentation is a s
37230 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a  et of routines.*
37240 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 72  * that does no r
37250 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20  eal locking and 
37260 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
37270 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73  or use in.** a s
37280 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61  ingle-threaded a
37290 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 65  pplication.  The
372a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53   SQLITE_MUTEX_OS
372b0 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54  2,.** SQLITE_MUT
372c0 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 20  EX_PTHREAD, and 
372d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32  SQLITE_MUTEX_W32
372e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
372f0 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69  .** are appropri
37300 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f  ate for use on O
37310 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57  S/2, Unix, and W
37320 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  indows..**.** If
37330 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
37340 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c  led with the SQL
37350 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46  ITE_MUTEX_APPDEF
37360 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a   preprocessor.**
37370 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 28   macro defined (
37380 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d  with "-DSQLITE_M
37390 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 2c  UTEX_APPDEF=1"),
373a0 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a   then no mutex.*
373b0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
373c0 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74   is included wit
373d0 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 49  h the library. I
373e0 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a  n this case the.
373f0 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  ** application m
37400 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 73  ust supply a cus
37410 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  tom mutex implem
37420 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 74  entation using t
37430 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f  he.** [SQLITE_CO
37440 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69  NFIG_MUTEX] opti
37450 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
37460 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 74  3_config() funct
37470 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61  ion.** before ca
37480 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e  lling sqlite3_in
37490 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e  itialize() or an
374a0 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 73  y other public s
374b0 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74  qlite3_.** funct
374c0 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 73  ion that calls s
374d0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
374e0 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  e()..**.** {H170
374f0 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f  11} The sqlite3_
37500 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f  mutex_alloc() ro
37510 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20  utine allocates 
37520 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61  a new.** mutex a
37530 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
37540 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 37  nter to it. {H17
37550 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 72  012} If it retur
37560 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20  ns NULL.** that 
37570 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
37580 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
37590 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 30  allocated. {H170
375a0 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69  13} SQLite.** wi
375b0 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74  ll unwind its st
375c0 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ack and return a
375d0 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 34  n error. {H17014
375e0 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a  } The argument.*
375f0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  * to sqlite3_mut
37600 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e  ex_alloc() is on
37610 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
37620 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a  er constants:.**
37630 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
37640 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46    SQLITE_MUTEX_F
37650 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  AST.** <li>  SQL
37660 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
37670 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  IVE.** <li>  SQL
37680 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
37690 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20  _MASTER.** <li> 
376a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
376b0 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e  ATIC_MEM.** <li>
376c0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
376d0 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c  TATIC_MEM2.** <l
376e0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
376f0 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20  _STATIC_PRNG.** 
37700 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
37710 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a  EX_STATIC_LRU.**
37720 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
37730 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a  TEX_STATIC_LRU2.
37740 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b  ** </ul>.**.** {
37750 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 73  H17015} The firs
37760 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20  t two constants 
37770 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75  cause sqlite3_mu
37780 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63  tex_alloc() to c
37790 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d  reate.** a new m
377a0 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d  utex.  The new m
377b0 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76  utex is recursiv
377c0 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  e when SQLITE_MU
377d0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
377e0 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74   is used but not
377f0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20   necessarily so 
37800 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  when SQLITE_MUTE
37810 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 20  X_FAST is used. 
37820 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74  {END}.** The mut
37830 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
37840 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
37850 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e  to make a distin
37860 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e  ction.** between
37870 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
37880 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49  CURSIVE and SQLI
37890 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66  TE_MUTEX_FAST if
378a0 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
378b0 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 31  want to.  {H1701
378c0 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 69  6} But SQLite wi
378d0 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20  ll only request 
378e0 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
378f0 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68  x in.** cases wh
37900 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65  ere it really ne
37910 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20  eds one.  {END} 
37920 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d  If a faster non-
37930 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a  recursive mutex.
37940 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
37950 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  n is available o
37960 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66  n the host platf
37970 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73  orm, the mutex s
37980 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68  ubsystem.** migh
37990 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20  t return such a 
379a0 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  mutex in respons
379b0 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45  e to SQLITE_MUTE
379c0 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48  X_FAST..**.** {H
379d0 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 72  17017} The other
379e0 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74   allowed paramet
379f0 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  ers to sqlite3_m
37a00 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63  utex_alloc() eac
37a10 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f  h return.** a po
37a20 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69  inter to a stati
37a30 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75  c preexisting mu
37a40 74 65 78 2e 20 7b 45 4e 44 7d 20 20 46 6f 75 72  tex. {END}  Four
37a50 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   static mutexes 
37a60 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74  are.** used by t
37a70 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69  he current versi
37a80 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46  on of SQLite.  F
37a90 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
37aa0 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20  f SQLite.** may 
37ab0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  add additional s
37ac0 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20  tatic mutexes.  
37ad0 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  Static mutexes a
37ae0 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a  re for internal.
37af0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  ** use by SQLite
37b00 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74   only.  Applicat
37b10 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51  ions that use SQ
37b20 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f  Lite mutexes sho
37b30 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20  uld.** use only 
37b40 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  the dynamic mute
37b50 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  xes returned by 
37b60 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
37b70 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  T or.** SQLITE_M
37b80 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a  UTEX_RECURSIVE..
37b90 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e  **.** {H17018} N
37ba0 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20  ote that if one 
37bb0 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d  of the dynamic m
37bc0 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20  utex parameters 
37bd0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  (SQLITE_MUTEX_FA
37be0 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f  ST.** or SQLITE_
37bf0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29  MUTEX_RECURSIVE)
37c00 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71   is used then sq
37c10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
37c20 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61  c().** returns a
37c30 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78   different mutex
37c40 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20   on every call. 
37c50 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f   {H17034} But fo
37c60 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20  r the static.** 
37c70 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
37c80 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
37c90 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
37ca0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
37cb0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
37cc0 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  number..**.** {H
37cd0 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74  17019} The sqlit
37ce0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20  e3_mutex_free() 
37cf0 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61  routine dealloca
37d00 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79  tes a previously
37d10 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79  .** allocated dy
37d20 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31  namic mutex. {H1
37d30 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20  7020} SQLite is 
37d40 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c  careful to deall
37d50 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64  ocate every.** d
37d60 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61  ynamic mutex tha
37d70 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20  t it allocates. 
37d80 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e  {A17021} The dyn
37d90 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73  amic mutexes mus
37da0 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75  t not be in.** u
37db0 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65  se when they are
37dc0 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41   deallocated. {A
37dd0 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e  17022} Attemptin
37de0 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  g to deallocate 
37df0 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65  a static.** mute
37e00 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  x results in und
37e10 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e  efined behavior.
37e20 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65   {H17023} SQLite
37e30 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74   never deallocat
37e40 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d  es.** a static m
37e50 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  utex. {END}.**.*
37e60 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
37e70 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20  tex_enter() and 
37e80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
37e90 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74  y() routines att
37ea0 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72  empt.** to enter
37eb0 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32   a mutex. {H1702
37ec0 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  4} If another th
37ed0 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
37ee0 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
37ef0 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
37f00 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
37f10 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
37f20 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
37f30 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
37f40 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32  ITE_BUSY. {H1702
37f50 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  5}  The sqlite3_
37f60 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65  mutex_try() inte
37f70 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53  rface returns [S
37f80 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f  QLITE_OK].** upo
37f90 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74  n successful ent
37fa0 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75  ry.  {H17026} Mu
37fb0 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73  texes created us
37fc0 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55  ing.** SQLITE_MU
37fd0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61  TEX_RECURSIVE ca
37fe0 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c  n be entered mul
37ff0 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74  tiple times by t
38000 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a  he same thread..
38010 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73  ** {H17027} In s
38020 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
38030 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
38040 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
38050 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
38060 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
38070 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
38080 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66  er.  {A17028} If
38090 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
380a0 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20   tries to enter 
380b0 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e  any other.** kin
380c0 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20  d of mutex more 
380d0 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62  than once, the b
380e0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
380f0 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39  ined..** {H17029
38100 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65  } SQLite will ne
38110 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73  ver exhibit.** s
38120 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20  uch behavior in 
38130 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d  its own use of m
38140 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f  utexes..**.** So
38150 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20  me systems (for 
38160 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73  example, Windows
38170 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70   95) do not supp
38180 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f  ort the operatio
38190 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64  n.** implemented
381a0 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   by sqlite3_mute
381b0 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f  x_try().  On tho
381c0 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69  se systems, sqli
381d0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a  te3_mutex_try().
381e0 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72  ** will always r
381f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
38200 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65  Y.  {H17030} The
38210 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c   SQLite core onl
38220 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73  y ever uses.** s
38230 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
38240 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a  () as an optimiz
38250 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73  ation so this is
38260 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61   acceptable beha
38270 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  vior..**.** {H17
38280 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33  031} The sqlite3
38290 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72  _mutex_leave() r
382a0 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d  outine exits a m
382b0 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a  utex that was.**
382c0 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65   previously ente
382d0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20  red by the same 
382e0 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32  thread.  {A17032
382f0 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a  } The behavior.*
38300 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69  * is undefined i
38310 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e  f the mutex is n
38320 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ot currently ent
38330 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63  ered by the.** c
38340 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72  alling thread or
38350 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
38360 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48  y allocated.  {H
38370 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69  17033} SQLite wi
38380 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65  ll.** never do e
38390 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  ither. {END}.**.
383a0 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65  ** If the argume
383b0 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  nt to sqlite3_mu
383c0 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c  tex_enter(), sql
383d0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
383e0 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  , or.** sqlite3_
383f0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73  mutex_leave() is
38400 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
38410 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20   then all three 
38420 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61  routines.** beha
38430 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a  ve as no-ops..**
38440 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
38450 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
38460 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  d()] and [sqlite
38470 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
38480 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
38490 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
384a0 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  *sqlite3_mutex_a
384b0 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54  lloc(int);.SQLIT
384c0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
384d0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71  e3_mutex_free(sq
384e0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53  lite3_mutex*);.S
384f0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
38500 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
38510 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
38520 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
38530 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
38540 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  _try(sqlite3_mut
38550 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ex*);.SQLITE_API
38560 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
38570 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
38580 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a  3_mutex*);../*.*
38590 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
385a0 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74  x Methods Object
385b0 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33   {H17120} <S2013
385c0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
385d0 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  AL.**.** An inst
385e0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
385f0 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74  ucture defines t
38600 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75  he low-level rou
38610 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f  tines.** used to
38620 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73   allocate and us
38630 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a  e mutexes..**.**
38640 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65   Usually, the de
38650 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c  fault mutex impl
38660 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76  ementations prov
38670 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61  ided by SQLite a
38680 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74  re.** sufficient
38690 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73  , however the us
386a0 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f  er has the optio
386b0 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e  n of substitutin
386c0 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d  g a custom.** im
386d0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
386e0 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70   specialized dep
386f0 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74  loyments or syst
38700 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51  ems for which SQ
38710 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74  Lite.** does not
38720 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61   provide a suita
38730 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
38740 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  on. In this case
38750 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72  , the user.** cr
38760 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61  eates and popula
38770 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  tes an instance 
38780 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
38790 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20  e to pass.** to 
387a0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
387b0 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20   along with the 
387c0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
387d0 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a  UTEX] option..**
387e0 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61   Additionally, a
387f0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
38800 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  is structure can
38810 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a   be used as an.*
38820 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  * output variabl
38830 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20  e when querying 
38840 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74  the system for t
38850 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78  he current mutex
38860 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
38870 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53  on, using the [S
38880 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
38890 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a  MUTEX] option..*
388a0 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49  *.** The xMutexI
388b0 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e  nit method defin
388c0 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63  ed by this struc
388d0 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20  ture is invoked 
388e0 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79  as.** part of sy
388f0 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74  stem initializat
38900 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74  ion by the sqlit
38910 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
38920 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31  function..** {H1
38930 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78  7001} The xMutex
38940 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61  Init routine sha
38950 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20  ll be called by 
38960 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20  SQLite once for 
38970 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76  each.** effectiv
38980 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  e call to [sqlit
38990 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d  e3_initialize()]
389a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74  ..**.** The xMut
389b0 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66  exEnd method def
389c0 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72  ined by this str
389d0 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65  ucture is invoke
389e0 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  d as.** part of 
389f0 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20  system shutdown 
38a00 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  by the sqlite3_s
38a10 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69  hutdown() functi
38a20 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65  on. The.** imple
38a30 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69  mentation of thi
38a40 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65  s method is expe
38a50 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20  cted to release 
38a60 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a  all outstanding.
38a70 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74  ** resources obt
38a80 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74  ained by the mut
38a90 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65  ex methods imple
38aa0 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63  mentation, espec
38ab0 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f  ially.** those o
38ac0 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78  btained by the x
38ad0 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64  MutexInit method
38ae0 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78  . {H17003} The x
38af0 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e  MutexEnd().** in
38b00 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65  terface shall be
38b10 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f   invoked once fo
38b20 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b  r each call to [
38b30 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
38b40 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ()]..**.** The r
38b50 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d  emaining seven m
38b60 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62  ethods defined b
38b70 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
38b80 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a   (xMutexAlloc,.*
38b90 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d  * xMutexFree, xM
38ba0 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65  utexEnter, xMute
38bb0 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76  xTry, xMutexLeav
38bc0 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e  e, xMutexHeld an
38bd0 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65  d.** xMutexNothe
38be0 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  ld) implement th
38bf0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65  e following inte
38c00 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69  rfaces (respecti
38c10 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  vely):.**.** <ul
38c20 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
38c30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
38c40 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20  c()] </li>.**   
38c50 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d  <li>  [sqlite3_m
38c60 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c  utex_free()] </l
38c70 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
38c80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
38c90 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20  er()] </li>.**  
38ca0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
38cb0 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c  mutex_try()] </l
38cc0 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
38cd0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
38ce0 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20  ve()] </li>.**  
38cf0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
38d00 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f  mutex_held()] </
38d10 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b  li>.**   <li>  [
38d20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
38d30 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  theld()] </li>.*
38d40 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
38d50 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63  e only differenc
38d60 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75  e is that the pu
38d70 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58  blic sqlite3_XXX
38d80 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65   functions enume
38d90 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73  rated.** above s
38da0 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61  ilently ignore a
38db0 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74  ny invocations t
38dc0 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20  hat pass a NULL 
38dd0 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a  pointer instead.
38de0 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75  ** of a valid mu
38df0 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20  tex handle. The 
38e00 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
38e10 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64  of the methods d
38e20 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69  efined.** by thi
38e30 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
38e40 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
38e50 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65  handle this case
38e60 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a  , the results.**
38e70 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55   of passing a NU
38e80 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65  LL pointer inste
38e90 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75  ad of a valid mu
38ea0 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75  tex handle are u
38eb0 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65  ndefined.** (i.e
38ec0 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62  . it is acceptab
38ed0 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e  le to provide an
38ee0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
38ef0 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69  that segfaults i
38f00 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65  f.** it is passe
38f10 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  d a NULL pointer
38f20 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  )..*/.typedef st
38f30 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74  ruct sqlite3_mut
38f40 65 78 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74  ex_methods sqlit
38f50 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
38f60 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ;.struct sqlite3
38f70 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 7b  _mutex_methods {
38f80 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 49  .  int (*xMutexI
38f90 6e 69 74 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e  nit)(void);.  in
38fa0 74 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28 76  t (*xMutexEnd)(v
38fb0 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  oid);.  sqlite3_
38fc0 6d 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78 41  mutex *(*xMutexA
38fd0 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f  lloc)(int);.  vo
38fe0 69 64 20 28 2a 78 4d 75 74 65 78 46 72 65 65 29  id (*xMutexFree)
38ff0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
39000 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74  );.  void (*xMut
39010 65 78 45 6e 74 65 72 29 28 73 71 6c 69 74 65 33  exEnter)(sqlite3
39020 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74  _mutex *);.  int
39030 20 28 2a 78 4d 75 74 65 78 54 72 79 29 28 73 71   (*xMutexTry)(sq
39040 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a  lite3_mutex *);.
39050 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 4c    void (*xMutexL
39060 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  eave)(sqlite3_mu
39070 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  tex *);.  int (*
39080 78 4d 75 74 65 78 48 65 6c 64 29 28 73 71 6c 69  xMutexHeld)(sqli
39090 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20  te3_mutex *);.  
390a0 69 6e 74 20 28 2a 78 4d 75 74 65 78 4e 6f 74 68  int (*xMutexNoth
390b0 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 74  eld)(sqlite3_mut
390c0 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ex *);.};../*.**
390d0 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
390e0 20 56 65 72 69 66 69 63 61 74 69 6f 6e 20 52 6f   Verification Ro
390f0 75 74 69 6e 65 73 20 7b 48 31 37 30 38 30 7d 20  utines {H17080} 
39100 3c 53 32 30 31 33 30 3e 20 3c 53 33 30 38 30 30  <S20130> <S30800
39110 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
39120 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
39130 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
39140 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75  ex_notheld() rou
39150 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74  tines.** are int
39160 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e  ended for use in
39170 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74  side assert() st
39180 61 74 65 6d 65 6e 74 73 2e 20 7b 48 31 37 30 38  atements. {H1708
39190 31 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  1} The SQLite co
391a0 72 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 65 73  re.** never uses
391b0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
391c0 65 78 63 65 70 74 20 69 6e 73 69 64 65 20 61 6e  except inside an
391d0 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 61 70   assert() and ap
391e0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72  plications.** ar
391f0 65 20 61 64 76 69 73 65 64 20 74 6f 20 66 6f 6c  e advised to fol
39200 6c 6f 77 20 74 68 65 20 6c 65 61 64 20 6f 66 20  low the lead of 
39210 74 68 65 20 63 6f 72 65 2e 20 20 7b 48 31 37 30  the core.  {H170
39220 38 32 7d 20 54 68 65 20 63 6f 72 65 20 6f 6e 6c  82} The core onl
39230 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69 6d  y.** provides im
39240 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f  plementations fo
39250 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  r these routines
39260 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70   when it is comp
39270 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65  iled.** with the
39280 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66 6c   SQLITE_DEBUG fl
39290 61 67 2e 20 20 7b 41 31 37 30 38 37 7d 20 45 78  ag.  {A17087} Ex
392a0 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 6d 70  ternal mutex imp
392b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20  lementations.** 
392c0 61 72 65 20 6f 6e 6c 79 20 72 65 71 75 69 72 65  are only require
392d0 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65  d to provide the
392e0 73 65 20 72 6f 75 74 69 6e 65 73 20 69 66 20 53  se routines if S
392f0 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 0a 2a  QLITE_DEBUG is.*
39300 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 69 66  * defined and if
39310 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64   NDEBUG is not d
39320 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48  efined..**.** {H
39330 31 37 30 38 33 7d 20 54 68 65 73 65 20 72 6f 75  17083} These rou
39340 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65 74  tines should ret
39350 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
39360 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20 61  mutex in their a
39370 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68 65  rgument.** is he
39380 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c 20  ld or not held, 
39390 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62 79  respectively, by
393a0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72   the calling thr
393b0 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 58 31 37 30  ead..**.** {X170
393c0 38 34 7d 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e  84} The implemen
393d0 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65  tation is not re
393e0 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64  quired to provid
393f0 65 64 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  ed versions of t
39400 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  hese.** routines
39410 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20 77   that actually w
39420 6f 72 6b 2e 20 49 66 20 74 68 65 20 69 6d 70 6c  ork. If the impl
39430 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20  ementation does 
39440 6e 6f 74 20 70 72 6f 76 69 64 65 20 77 6f 72 6b  not provide work
39450 69 6e 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20  ing.** versions 
39460 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  of these routine
39470 73 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74 20  s, it should at 
39480 6c 65 61 73 74 20 70 72 6f 76 69 64 65 20 73 74  least provide st
39490 75 62 73 20 74 68 61 74 20 61 6c 77 61 79 73 0a  ubs that always.
394a0 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20 73  ** return true s
394b0 6f 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73 20  o that one does 
394c0 6e 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75 73  not get spurious
394d0 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 75   assertion failu
394e0 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  res..**.** {H170
394f0 38 35 7d 20 49 66 20 74 68 65 20 61 72 67 75 6d  85} If the argum
39500 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  ent to sqlite3_m
39510 75 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20 61  utex_held() is a
39520 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68   NULL pointer th
39530 65 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e  en.** the routin
39540 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20  e should return 
39550 31 2e 20 20 7b 45 4e 44 7d 20 54 68 69 73 20 73  1.  {END} This s
39560 65 65 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e 74  eems counter-int
39570 75 69 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a 20  uitive since.** 
39580 63 6c 65 61 72 6c 79 20 74 68 65 20 6d 75 74 65  clearly the mute
39590 78 20 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c 64  x cannot be held
395a0 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   if it does not 
395b0 65 78 69 73 74 2e 20 20 42 75 74 20 74 68 65 0a  exist.  But the.
395c0 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68  ** the reason th
395d0 65 20 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f 74  e mutex does not
395e0 20 65 78 69 73 74 20 69 73 20 62 65 63 61 75 73   exist is becaus
395f0 65 20 74 68 65 20 62 75 69 6c 64 20 69 73 20 6e  e the build is n
39600 6f 74 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74 65  ot.** using mute
39610 78 65 73 2e 20 20 41 6e 64 20 77 65 20 64 6f 20  xes.  And we do 
39620 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 61 73 73  not want the ass
39630 65 72 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67  ert() containing
39640 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20   the.** call to 
39650 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
39660 6c 64 28 29 20 74 6f 20 66 61 69 6c 2c 20 73 6f  ld() to fail, so
39670 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75   a non-zero retu
39680 72 6e 20 69 73 0a 2a 2a 20 74 68 65 20 61 70 70  rn is.** the app
39690 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20 74  ropriate thing t
396a0 6f 20 64 6f 2e 20 20 7b 48 31 37 30 38 36 7d 20  o do.  {H17086} 
396b0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
396c0 78 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20 69  x_notheld().** i
396d0 6e 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 20  nterface should 
396e0 61 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 68  also return 1 wh
396f0 65 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c 20  en given a NULL 
39700 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
39710 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
39720 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71  e3_mutex_held(sq
39730 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53  lite3_mutex*);.S
39740 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
39750 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
39760 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  eld(sqlite3_mute
39770 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  x*);../*.** CAPI
39780 33 52 45 46 3a 20 4d 75 74 65 78 20 54 79 70 65  3REF: Mutex Type
39790 73 20 7b 48 31 37 30 30 31 7d 20 3c 48 31 37 30  s {H17001} <H170
397a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73  00>.**.** The [s
397b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
397c0 6f 63 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  oc()] interface 
397d0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
397e0 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 69 63 68  rgument.** which
397f0 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65   is one of these
39800 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
39810 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  ts..**.** The se
39820 74 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74 65  t of static mute
39830 78 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20 66  xes may change f
39840 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20 72  rom one SQLite r
39850 65 6c 65 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a  elease to the.**
39860 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61 74   next.  Applicat
39870 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 72 69  ions that overri
39880 64 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  de the built-in 
39890 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d 75 73 74  mutex logic must
398a0 20 62 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 20   be.** prepared 
398b0 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20 61  to accommodate a
398c0 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63  dditional static
398d0 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 64 65   mutexes..*/.#de
398e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
398f0 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20  X_FAST          
39900 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
39910 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
39920 49 56 45 20 20 20 20 20 20 20 20 31 0a 23 64 65  IVE        1.#de
39930 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
39940 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  X_STATIC_MASTER 
39950 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c     2.#define SQL
39960 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
39970 5f 4d 45 4d 20 20 20 20 20 20 20 33 20 20 2f 2a  _MEM       3  /*
39980 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
39990 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ) */.#define SQL
399a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
399b0 5f 4d 45 4d 32 20 20 20 20 20 20 34 20 20 2f 2a  _MEM2      4  /*
399c0 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 23 64 65   NOT USED */.#de
399d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
399e0 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 20 20 20  X_STATIC_OPEN   
399f0 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 33     4  /* sqlite3
39a00 42 74 72 65 65 4f 70 65 6e 28 29 20 2a 2f 0a 23  BtreeOpen() */.#
39a10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55  define SQLITE_MU
39a20 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 20  TEX_STATIC_PRNG 
39a30 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74       5  /* sqlit
39a40 65 33 5f 72 61 6e 64 6f 6d 28 29 20 2a 2f 0a 23  e3_random() */.#
39a50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55  define SQLITE_MU
39a60 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 20  TEX_STATIC_LRU  
39a70 20 20 20 20 20 36 20 20 2f 2a 20 6c 72 75 20 70       6  /* lru p
39a80 61 67 65 20 6c 69 73 74 20 2a 2f 0a 23 64 65 66  age list */.#def
39a90 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
39aa0 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 20 20 20  _STATIC_LRU2    
39ab0 20 20 37 20 20 2f 2a 20 6c 72 75 20 70 61 67 65    7  /* lru page
39ac0 20 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20   list */../*.** 
39ad0 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69 65  CAPI3REF: Retrie
39ae0 76 65 20 74 68 65 20 6d 75 74 65 78 20 66 6f 72  ve the mutex for
39af0 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e   a database conn
39b00 65 63 74 69 6f 6e 20 7b 48 31 37 30 30 32 7d 20  ection {H17002} 
39b10 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <H17000>.**.** T
39b20 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65  his interface re
39b30 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
39b40 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  the [sqlite3_mut
39b50 65 78 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20  ex] object that 
39b60 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65 73 20 61  .** serializes a
39b70 63 63 65 73 73 20 74 6f 20 74 68 65 20 5b 64 61  ccess to the [da
39b80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
39b90 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  n] given in the 
39ba0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e  argument.** when
39bb0 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20   the [threading 
39bc0 6d 6f 64 65 5d 20 69 73 20 53 65 72 69 61 6c 69  mode] is Seriali
39bd0 7a 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b  zed..** If the [
39be0 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20  threading mode] 
39bf0 69 73 20 53 69 6e 67 6c 65 2d 74 68 72 65 61 64  is Single-thread
39c00 20 6f 72 20 4d 75 6c 74 69 2d 74 68 72 65 61 64   or Multi-thread
39c10 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f   then this.** ro
39c20 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20  utine returns a 
39c30 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
39c40 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
39c50 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74  te3_mutex *sqlit
39c60 65 33 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c 69  e3_db_mutex(sqli
39c70 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  te3*);../*.** CA
39c80 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76 65  PI3REF: Low-Leve
39c90 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61 74  l Control Of Dat
39ca0 61 62 61 73 65 20 46 69 6c 65 73 20 7b 48 31 31  abase Files {H11
39cb0 33 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a  300} <S30800>.**
39cc0 0a 2a 2a 20 7b 48 31 31 33 30 31 7d 20 54 68 65  .** {H11301} The
39cd0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63   [sqlite3_file_c
39ce0 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66  ontrol()] interf
39cf0 61 63 65 20 6d 61 6b 65 73 20 61 20 64 69 72 65  ace makes a dire
39d00 63 74 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a 2a  ct call to the.*
39d10 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d  * xFileControl m
39d20 65 74 68 6f 64 20 66 6f 72 20 74 68 65 20 5b 73  ethod for the [s
39d30 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
39d40 73 5d 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69  s] object associ
39d50 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 70  ated.** with a p
39d60 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61  articular databa
39d70 73 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  se identified by
39d80 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
39d90 6d 65 6e 74 2e 20 7b 48 31 31 33 30 32 7d 20 54  ment. {H11302} T
39da0 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68  he.** name of th
39db0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 74 68  e database is th
39dc0 65 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20  e name assigned 
39dd0 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
39de0 62 79 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65  by the.** <a hre
39df0 66 3d 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e 68  f="lang_attach.h
39e00 74 6d 6c 22 3e 41 54 54 41 43 48 3c 2f 61 3e 20  tml">ATTACH</a> 
39e10 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 68 61 74  SQL command that
39e20 20 6f 70 65 6e 65 64 20 74 68 65 0a 2a 2a 20 64   opened the.** d
39e30 61 74 61 62 61 73 65 2e 20 7b 48 31 31 33 30 33  atabase. {H11303
39e40 7d 20 54 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65  } To control the
39e50 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
39e60 69 6c 65 2c 20 75 73 65 20 74 68 65 20 6e 61 6d  ile, use the nam
39e70 65 20 22 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20 61  e "main".** or a
39e80 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 7b   NULL pointer. {
39e90 48 31 31 33 30 34 7d 20 54 68 65 20 74 68 69 72  H11304} The thir
39ea0 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72  d and fourth par
39eb0 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20  ameters to this 
39ec0 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20 70  routine.** are p
39ed0 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 74  assed directly t
39ee0 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 65  hrough to the se
39ef0 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 70  cond and third p
39f00 61 72 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a 20  arameters of.** 
39f10 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  the xFileControl
39f20 20 6d 65 74 68 6f 64 2e 20 20 7b 48 31 31 33 30   method.  {H1130
39f30 35 7d 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  5} The return va
39f40 6c 75 65 20 6f 66 20 74 68 65 20 78 46 69 6c 65  lue of the xFile
39f50 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68 6f  Control.** metho
39f60 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65  d becomes the re
39f70 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
39f80 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a  is routine..**.*
39f90 2a 20 7b 48 31 31 33 30 36 7d 20 49 66 20 74 68  * {H11306} If th
39fa0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
39fb0 65 72 20 28 7a 44 62 4e 61 6d 65 29 20 64 6f 65  er (zDbName) doe
39fc0 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20  s not match the 
39fd0 6e 61 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f  name of any.** o
39fe0 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  pen database fil
39ff0 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 45  e, then SQLITE_E
3a000 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64  RROR is returned
3a010 2e 20 7b 48 31 31 33 30 37 7d 20 54 68 69 73 20  . {H11307} This 
3a020 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73  error.** code is
3a030 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72 65 64 20   not remembered 
3a040 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  and will not be 
3a050 72 65 63 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c  recalled by [sql
3a060 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 0a  ite3_errcode()].
3a070 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65  ** or [sqlite3_e
3a080 72 72 6d 73 67 28 29 5d 2e 20 7b 41 31 31 33 30  rrmsg()]. {A1130
3a090 38 7d 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e  8} The underlyin
3a0a0 67 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d  g xFileControl m
3a0b0 65 74 68 6f 64 20 6d 69 67 68 74 0a 2a 2a 20 61  ethod might.** a
3a0c0 6c 73 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54  lso return SQLIT
3a0d0 45 5f 45 52 52 4f 52 2e 20 20 7b 41 31 31 33 30  E_ERROR.  {A1130
3a0e0 39 7d 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77  9} There is no w
3a0f0 61 79 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73  ay to distinguis
3a100 68 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e 20  h between.** an 
3a110 69 6e 63 6f 72 72 65 63 74 20 7a 44 62 4e 61 6d  incorrect zDbNam
3a120 65 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f  e and an SQLITE_
3a130 45 52 52 4f 52 20 72 65 74 75 72 6e 20 66 72 6f  ERROR return fro
3a140 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  m the underlying
3a150 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  .** xFileControl
3a160 20 6d 65 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a 2a   method. {END}.*
3a170 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
3a180 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
3a190 4b 53 54 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54  KSTATE].*/.SQLIT
3a1a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3a1b0 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73  3_file_control(s
3a1c0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63  qlite3*, const c
3a1d0 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e  har *zDbName, in
3a1e0 74 20 6f 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f  t op, void*);../
3a1f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54  *.** CAPI3REF: T
3a200 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65  esting Interface
3a210 20 7b 48 31 31 34 30 30 7d 20 3c 53 33 30 38 30   {H11400} <S3080
3a220 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  0>.**.** The sql
3a230 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
3a240 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  l() interface is
3a250 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 75   used to read ou
3a260 74 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 73 74  t internal.** st
3a270 61 74 65 20 6f 66 20 53 51 4c 69 74 65 20 61 6e  ate of SQLite an
3a280 64 20 74 6f 20 69 6e 6a 65 63 74 20 66 61 75 6c  d to inject faul
3a290 74 73 20 69 6e 74 6f 20 53 51 4c 69 74 65 20 66  ts into SQLite f
3a2a0 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75  or testing.** pu
3a2b0 72 70 6f 73 65 73 2e 20 20 54 68 65 20 66 69 72  rposes.  The fir
3a2c0 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  st parameter is 
3a2d0 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64  an operation cod
3a2e0 65 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65  e that determine
3a2f0 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 2c  s.** the number,
3a300 20 6d 65 61 6e 69 6e 67 2c 20 61 6e 64 20 6f 70   meaning, and op
3a310 65 72 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73  eration of all s
3a320 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d 65  ubsequent parame
3a330 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ters..**.** This
3a340 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f   interface is no
3a350 74 20 66 6f 72 20 75 73 65 20 62 79 20 61 70 70  t for use by app
3a360 6c 69 63 61 74 69 6f 6e 73 2e 20 20 49 74 20 65  lications.  It e
3a370 78 69 73 74 73 20 73 6f 6c 65 6c 79 0a 2a 2a 20  xists solely.** 
3a380 66 6f 72 20 76 65 72 69 66 79 69 6e 67 20 74 68  for verifying th
3a390 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74  e correct operat
3a3a0 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74  ion of the SQLit
3a3b0 65 20 6c 69 62 72 61 72 79 2e 20 20 44 65 70 65  e library.  Depe
3a3c0 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 68 6f 77 20  nding.** on how 
3a3d0 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
3a3e0 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 2c 20  ry is compiled, 
3a3f0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 6d  this interface m
3a400 69 67 68 74 20 6e 6f 74 20 65 78 69 73 74 2e 0a  ight not exist..
3a410 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c  **.** The detail
3a420 73 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 69  s of the operati
3a430 6f 6e 20 63 6f 64 65 73 2c 20 74 68 65 69 72 20  on codes, their 
3a440 6d 65 61 6e 69 6e 67 73 2c 20 74 68 65 20 70 61  meanings, the pa
3a450 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 65 79  rameters.** they
3a460 20 74 61 6b 65 2c 20 61 6e 64 20 77 68 61 74 20   take, and what 
3a470 74 68 65 79 20 64 6f 20 61 72 65 20 61 6c 6c 20  they do are all 
3a480 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
3a490 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65  e without notice
3a4a0 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73 74  ..** Unlike most
3a4b0 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 41   of the SQLite A
3a4c0 50 49 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  PI, this functio
3a4d0 6e 20 69 73 20 6e 6f 74 20 67 75 61 72 61 6e 74  n is not guarant
3a4e0 65 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 72 61 74  eed to.** operat
3a4f0 65 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 66  e consistently f
3a500 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61 73 65 20  rom one release 
3a510 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2f 0a  to the next..*/.
3a520 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3a530 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
3a540 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29  rol(int op, ...)
3a550 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3a560 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 72  F: Testing Inter
3a570 66 61 63 65 20 4f 70 65 72 61 74 69 6f 6e 20 43  face Operation C
3a580 6f 64 65 73 20 7b 48 31 31 34 31 30 7d 20 3c 48  odes {H11410} <H
3a590 31 31 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  11400>.**.** The
3a5a0 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65  se constants are
3a5b0 20 74 68 65 20 76 61 6c 69 64 20 6f 70 65 72 61   the valid opera
3a5c0 74 69 6f 6e 20 63 6f 64 65 20 70 61 72 61 6d 65  tion code parame
3a5d0 74 65 72 73 20 75 73 65 64 0a 2a 2a 20 61 73 20  ters used.** as 
3a5e0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
3a5f0 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74  nt to [sqlite3_t
3a600 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 2e 0a  est_control()]..
3a610 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 61 72 61  **.** These para
3a620 6d 65 74 65 72 73 20 61 6e 64 20 74 68 65 69 72  meters and their
3a630 20 6d 65 61 6e 69 6e 67 73 20 61 72 65 20 73 75   meanings are su
3a640 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 0a  bject to change.
3a650 2a 2a 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63  ** without notic
3a660 65 2e 20 20 54 68 65 73 65 20 76 61 6c 75 65 73  e.  These values
3a670 20 61 72 65 20 66 6f 72 20 74 65 73 74 69 6e 67   are for testing
3a680 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 0a   purposes only..
3a690 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  ** Applications 
3a6a0 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 61  should not use a
3a6b0 6e 79 20 6f 66 20 74 68 65 73 65 20 70 61 72 61  ny of these para
3a6c0 6d 65 74 65 72 73 20 6f 72 20 74 68 65 0a 2a 2a  meters or the.**
3a6d0 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63   [sqlite3_test_c
3a6e0 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66  ontrol()] interf
3a6f0 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ace..*/.#define 
3a700 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3a710 50 52 4e 47 5f 53 41 56 45 20 20 20 20 20 20 20  PRNG_SAVE       
3a720 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69           5.#defi
3a730 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
3a740 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45 20  RL_PRNG_RESTORE 
3a750 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64              6.#d
3a760 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
3a770 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54  TCTRL_PRNG_RESET
3a780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
3a790 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3a7a0 54 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43 5f  TESTCTRL_BITVEC_
3a7b0 54 45 53 54 20 20 20 20 20 20 20 20 20 20 20 20  TEST            
3a7c0 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    8.#define SQLI
3a7d0 54 45 5f 54 45 53 54 43 54 52 4c 5f 46 41 55 4c  TE_TESTCTRL_FAUL
3a7e0 54 5f 49 4e 53 54 41 4c 4c 20 20 20 20 20 20 20  T_INSTALL       
3a7f0 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 53       9.#define S
3a800 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42  QLITE_TESTCTRL_B
3a810 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f  ENIGN_MALLOC_HOO
3a820 4b 53 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e  KS     10.#defin
3a830 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
3a840 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 20  L_PENDING_BYTE  
3a850 20 20 20 20 20 20 20 20 20 20 31 31 0a 0a 2f 2a            11../*
3a860 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51  .** CAPI3REF: SQ
3a870 4c 69 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61  Lite Runtime Sta
3a880 74 75 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36  tus {H17200} <S6
3a890 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0200>.** EXPERIM
3a8a0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ENTAL.**.** This
3a8b0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73   interface is us
3a8c0 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72  ed to retrieve r
3a8d0 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e  untime status in
3a8e0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f  formation.** abo
3a8f0 75 74 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e  ut the preforman
3a900 63 65 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e  ce of SQLite, an
3a910 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20  d optionally to 
3a920 72 65 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a  reset various.**
3a930 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73   highwater marks
3a940 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
3a950 75 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65  ument is an inte
3a960 67 65 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20  ger code for.** 
3a970 74 68 65 20 73 70 65 63 69 66 69 63 20 70 61 72  the specific par
3a980 61 6d 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72  ameter to measur
3a990 65 2e 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69  e.  Recognized i
3a9a0 6e 74 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20  nteger codes.** 
3a9b0 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  are of the form 
3a9c0 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  [SQLITE_STATUS_M
3a9d0 45 4d 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c  EMORY_USED | SQL
3a9e0 49 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e  ITE_STATUS_...].
3a9f0 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20  .** The current 
3aa00 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72  value of the par
3aa10 61 6d 65 74 65 72 20 69 73 20 72 65 74 75 72 6e  ameter is return
3aa20 65 64 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e  ed into *pCurren
3aa30 74 2e 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73  t..** The highes
3aa40 74 20 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65  t recorded value
3aa50 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
3aa60 2a 70 48 69 67 68 77 61 74 65 72 2e 20 20 49 66  *pHighwater.  If
3aa70 20 74 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61   the.** resetFla
3aa80 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  g is true, then 
3aa90 74 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f  the highest reco
3aaa0 72 64 20 76 61 6c 75 65 20 69 73 20 72 65 73 65  rd value is rese
3aab0 74 20 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67  t after.** *pHig
3aac0 68 77 61 74 65 72 20 69 73 20 77 72 69 74 74 65  hwater is writte
3aad0 6e 2e 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65  n. Some paramete
3aae0 72 73 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64  rs do not record
3aaf0 20 74 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20   the highest.** 
3ab00 76 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73  value.  For thos
3ab10 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  e parameters.** 
3ab20 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74  nothing is writt
3ab30 65 6e 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61  en into *pHighwa
3ab40 74 65 72 20 61 6e 64 20 74 68 65 20 72 65 73 65  ter and the rese
3ab50 74 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64  tFlag is ignored
3ab60 2e 0a 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d  ..** Other param
3ab70 65 74 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c  eters record onl
3ab80 79 20 74 68 65 20 68 69 67 68 77 61 74 65 72 20  y the highwater 
3ab90 6d 61 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65  mark and not the
3aba0 20 63 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75   current.** valu
3abb0 65 2e 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61  e.  For these la
3abc0 74 74 65 72 20 70 61 72 61 6d 65 74 65 72 73 20  tter parameters 
3abd0 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74  nothing is writt
3abe0 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e  en into *pCurren
3abf0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  t..**.** This ro
3ac00 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51  utine returns SQ
3ac10 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
3ac20 73 73 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72  ss and a non-zer
3ac30 6f 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65  o.** [error code
3ac40 5d 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a  ] on failure..**
3ac50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
3ac60 20 69 73 20 74 68 72 65 61 64 73 61 66 65 20 62   is threadsafe b
3ac70 75 74 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63  ut is not atomic
3ac80 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
3ac90 63 61 6e 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68  can.** called wh
3aca0 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 64  ile other thread
3acb0 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 68  s are running th
3acc0 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 72  e same or differ
3acd0 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e  ent SQLite.** in
3ace0 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76  terfaces.  Howev
3acf0 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72 65  er the values re
3ad00 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72 72  turned in *pCurr
3ad10 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 67  ent and.** *pHig
3ad20 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20 74  hwater reflect t
3ad30 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51 4c  he status of SQL
3ad40 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e 74  ite at different
3ad50 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65 0a   points in time.
3ad60 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f 73  ** and it is pos
3ad70 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68  sible that anoth
3ad80 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74 20  er thread might 
3ad90 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61 6d  change the param
3ada0 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77 65  eter.** in betwe
3adb0 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68 65  en the times whe
3adc0 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 20  n *pCurrent and 
3add0 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65 20  *pHighwater are 
3ade0 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53  written..**.** S
3adf0 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65  ee also: [sqlite
3ae00 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a 2a  3_db_status()].*
3ae10 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
3ae20 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
3ae30 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61   int sqlite3_sta
3ae40 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20  tus(int op, int 
3ae50 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a  *pCurrent, int *
3ae60 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20  pHighwater, int 
3ae70 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a  resetFlag);.../*
3ae80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74  .** CAPI3REF: St
3ae90 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 20  atus Parameters 
3aea0 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30 30  {H17250} <H17200
3aeb0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
3aec0 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e  L.**.** These in
3aed0 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20  teger constants 
3aee0 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f 75  designate variou
3aef0 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74 75  s run-time statu
3af00 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  s parameters.** 
3af10 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74 75  that can be retu
3af20 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  rned by [sqlite3
3af30 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a  _status()]..**.*
3af40 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51  * <dl>.** <dt>SQ
3af50 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
3af60 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20  RY_USED</dt>.** 
3af70 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
3af80 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65 6e  er is the curren
3af90 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  t amount of memo
3afa0 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a 2a  ry checked out.*
3afb0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
3afc0 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68  _malloc()], eith
3afd0 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69  er directly or i
3afe0 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 0a  ndirectly.  The.
3aff0 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75 64  ** figure includ
3b000 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74 6f  es calls made to
3b010 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
3b020 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c 69  ()] by the appli
3b030 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e  cation.** and in
3b040 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73  ternal memory us
3b050 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69 74  age by the SQLit
3b060 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72 61  e library.  Scra
3b070 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f  tch memory.** co
3b080 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c  ntrolled by [SQL
3b090 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
3b0a0 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61 72  CH] and auxiliar
3b0b0 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a 20  y page-cache.** 
3b0c0 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65  memory controlle
3b0d0 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  d by [SQLITE_CON
3b0e0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20 69  FIG_PAGECACHE] i
3b0f0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69  s not included i
3b100 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d 65  n.** this parame
3b110 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e 74  ter.  The amount
3b120 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
3b130 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c 6f   sum of the allo
3b140 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20  cation.** sizes 
3b150 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20 74  as reported by t
3b160 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64 20  he xSize method 
3b170 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  in [sqlite3_mem_
3b180 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a  methods].</dd>.*
3b190 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3b1a0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49  STATUS_MALLOC_SI
3b1b0 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ZE</dt>.** <dd>T
3b1c0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
3b1d0 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73  cords the larges
3b1e0 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  t memory allocat
3b1f0 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68  ion request.** h
3b200 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 65  anded to [sqlite
3b210 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b  3_malloc()] or [
3b220 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
3b230 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20  )] (or their.** 
3b240 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61 6c  internal equival
3b250 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65  ents).  Only the
3b260 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
3b270 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68  in the.** *pHigh
3b280 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20  water parameter 
3b290 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74  to [sqlite3_stat
3b2a0 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65  us()] is of inte
3b2b0 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76  rest.  .** The v
3b2c0 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74  alue written int
3b2d0 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20  o the *pCurrent 
3b2e0 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64  parameter is und
3b2f0 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a  efined.</dd>.**.
3b300 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
3b310 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
3b320 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  SED</dt>.** <dd>
3b330 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
3b340 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
3b350 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64 20  r of pages used 
3b360 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70  out of the.** [p
3b370 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  agecache memory 
3b380 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74 20  allocator] that 
3b390 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 75  was configured u
3b3a0 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 45  sing .** [SQLITE
3b3b0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
3b3c0 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75  E].  The.** valu
3b3d0 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e  e returned is in
3b3e0 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20 62   pages, not in b
3b3f0 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ytes.</dd>.**.**
3b400 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54   <dt>SQLITE_STAT
3b410 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45  US_PAGECACHE_OVE
3b420 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  RFLOW</dt>.** <d
3b430 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
3b440 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
3b450 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
3b460 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61 6c  page cache.** al
3b470 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63  location which c
3b480 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74  ould not be stat
3b490 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53  isfied by the [S
3b4a0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
3b4b0 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66 65  ECACHE].** buffe
3b4c0 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63  r and where forc
3b4d0 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74  ed to overflow t
3b4e0 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  o [sqlite3_mallo
3b4f0 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 65  c()].  The.** re
3b500 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e 63  turned value inc
3b510 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f 6e  ludes allocation
3b520 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65  s that overflowe
3b530 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a 2a  d because they.*
3b540 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72 67  * where too larg
3b550 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61 72  e (they were lar
3b560 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73 7a  ger than the "sz
3b570 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a  " parameter to.*
3b580 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
3b590 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e 64  _PAGECACHE]) and
3b5a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61   allocations tha
3b5b0 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63  t overflowed bec
3b5c0 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 65  ause.** no space
3b5d0 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68 65   was left in the
3b5e0 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64 64   page cache.</dd
3b5f0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
3b600 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41  TE_STATUS_PAGECA
3b610 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a  CHE_SIZE</dt>.**
3b620 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
3b630 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20  ter records the 
3b640 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61  largest memory a
3b650 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73  llocation reques
3b660 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b  t.** handed to [
3b670 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79  pagecache memory
3b680 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e   allocator].  On
3b690 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  ly the value ret
3b6a0 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  urned in the.** 
3b6b0 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61  *pHighwater para
3b6c0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
3b6d0 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f  3_status()] is o
3b6e0 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a  f interest.  .**
3b6f0 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74   The value writt
3b700 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75  en into the *pCu
3b710 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20  rrent parameter 
3b720 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64  is undefined.</d
3b730 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
3b740 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
3b750 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20  CH_USED</dt>.** 
3b760 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
3b770 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  er returns the n
3b780 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74  umber of allocat
3b790 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f 66  ions used out of
3b7a0 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63 68   the.** [scratch
3b7b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
3b7c0 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73  r] configured us
3b7d0 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43  ing.** [SQLITE_C
3b7e0 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e 20  ONFIG_SCRATCH]. 
3b7f0 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
3b800 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63 61  ned is in alloca
3b810 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e  tions, not.** in
3b820 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20 61   bytes.  Since a
3b830 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 6d   single thread m
3b840 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e 65  ay only have one
3b850 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74   scratch allocat
3b860 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69  ion.** outstandi
3b870 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69 73  ng at time, this
3b880 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f 20   parameter also 
3b890 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d 62  reports the numb
3b8a0 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a 2a  er of threads.**
3b8b0 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20 6d   using scratch m
3b8c0 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61 6d  emory at the sam
3b8d0 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a  e time.</dd>.**.
3b8e0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
3b8f0 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45  ATUS_SCRATCH_OVE
3b900 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  RFLOW</dt>.** <d
3b910 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
3b920 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
3b930 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
3b940 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a  scratch memory.*
3b950 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69  * allocation whi
3b960 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ch could not be 
3b970 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74 68  statisfied by th
3b980 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  e [SQLITE_CONFIG
3b990 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75 66  _SCRATCH].** buf
3b9a0 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f  fer and where fo
3b9b0 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77  rced to overflow
3b9c0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c   to [sqlite3_mal
3b9d0 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61 6c  loc()].  The val
3b9e0 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  ues.** returned 
3b9f0 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f 77  include overflow
3ba00 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65  s because the re
3ba10 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69  quested allocati
3ba20 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61  on was too.** la
3ba30 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20 62  rger (that is, b
3ba40 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65  ecause the reque
3ba50 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  sted allocation 
3ba60 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20  was larger than 
3ba70 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72 61  the.** "sz" para
3ba80 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54 45  meter to [SQLITE
3ba90 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d  _CONFIG_SCRATCH]
3baa0 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e 6f  ) and because no
3bab0 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 0a   scratch buffer.
3bac0 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61 76  ** slots were av
3bad0 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64  ailable..** </dd
3bae0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
3baf0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
3bb00 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c  H_SIZE</dt>.** <
3bb10 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
3bb20 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61  r records the la
3bb30 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c  rgest memory all
3bb40 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a  ocation request.
3bb50 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 63  ** handed to [sc
3bb60 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c  ratch memory all
3bb70 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74  ocator].  Only t
3bb80 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
3bb90 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69  d in the.** *pHi
3bba0 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65  ghwater paramete
3bbb0 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  r to [sqlite3_st
3bbc0 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e  atus()] is of in
3bbd0 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65  terest.  .** The
3bbe0 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69   value written i
3bbf0 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e  nto the *pCurren
3bc00 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75  t parameter is u
3bc10 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a  ndefined.</dd>.*
3bc20 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3bc30 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54  STATUS_PARSER_ST
3bc40 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  ACK</dt>.** <dd>
3bc50 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
3bc60 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70 65  ecords the deepe
3bc70 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b 2e  st parser stack.
3bc80 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20    It is only.** 
3bc90 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53 51  meaningful if SQ
3bca0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
3bcb0 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d 41   with [YYTRACKMA
3bcc0 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64  XSTACKDEPTH].</d
3bcd0 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a  d>.** </dl>.**.*
3bce0 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61 72  * New status par
3bcf0 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20 61  ameters may be a
3bd00 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20 74  dded from time t
3bd10 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69  o time..*/.#defi
3bd20 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53  ne SQLITE_STATUS
3bd30 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20 20  _MEMORY_USED    
3bd40 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
3bd50 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
3bd60 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20 20  GECACHE_USED    
3bd70 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
3bd80 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
3bd90 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20 20  ACHE_OVERFLOW   
3bda0 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
3bdb0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
3bdc0 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a 23  USED         3.#
3bdd0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3bde0 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45  ATUS_SCRATCH_OVE
3bdf0 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65 66  RFLOW     4.#def
3be00 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
3be10 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 20  S_MALLOC_SIZE   
3be20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65         5.#define
3be30 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50   SQLITE_STATUS_P
3be40 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20 20  ARSER_STACK     
3be50 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51      6.#define SQ
3be60 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
3be70 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20 20  CACHE_SIZE      
3be80 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   7.#define SQLIT
3be90 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
3bea0 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38 0a  _SIZE         8.
3beb0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3bec0 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63   Database Connec
3bed0 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31 37  tion Status {H17
3bee0 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a  500} <S60200>.**
3bef0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
3bf00 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
3bf10 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ce is used to re
3bf20 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20 73  trieve runtime s
3bf30 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tatus informatio
3bf40 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73 69  n .** about a si
3bf50 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20 63  ngle [database c
3bf60 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65  onnection].  The
3bf70 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
3bf80 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  is the.** databa
3bf90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62  se connection ob
3bfa0 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65 72  ject to be inter
3bfb0 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73 65  rogated.  The se
3bfc0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
3bfd0 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 65   is the paramete
3bfe0 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74 65  r to interrogate
3bff0 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68  .  Currently, th
3c000 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 76  e only allowed v
3c010 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20  alue.** for the 
3c020 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
3c030 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53 54   is [SQLITE_DBST
3c040 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55  ATUS_LOOKASIDE_U
3c050 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 6f  SED]..** Additio
3c060 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c 6c  nal options will
3c070 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20 69   likely appear i
3c080 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
3c090 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s of SQLite..**.
3c0a0 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76  ** The current v
3c0b0 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71 75  alue of the requ
3c0c0 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72 20  ested parameter 
3c0d0 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  is written into 
3c0e0 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68 65  *pCur.** and the
3c0f0 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e 74   highest instant
3c100 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73 20  aneous value is 
3c110 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48  written into *pH
3c120 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68 65  iwtr.  If.** the
3c130 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 75   resetFlg is tru
3c140 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67 68  e, then the high
3c150 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75  est instantaneou
3c160 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72 65  s value is.** re
3c170 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f  set back down to
3c180 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   the current val
3c190 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ue..**.** See al
3c1a0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61  so: [sqlite3_sta
3c1b0 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  tus()] and [sqli
3c1c0 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28  te3_stmt_status(
3c1d0 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
3c1e0 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
3c1f0 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
3c200 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c 69  3_db_status(sqli
3c210 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  te3*, int op, in
3c220 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48  t *pCur, int *pH
3c230 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74 46  iwtr, int resetF
3c240 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lg);../*.** CAPI
3c250 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72  3REF: Status Par
3c260 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74 61  ameters for data
3c270 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
3c280 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35 30   {H17520} <H1750
3c290 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
3c2a0 41 4c 0a 2a 2a 0a 2a 2a 20 53 74 61 74 75 73 20  AL.**.** Status 
3c2b0 76 65 72 62 73 20 66 6f 72 20 5b 73 71 6c 69 74  verbs for [sqlit
3c2c0 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e  e3_db_status()].
3c2d0 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c  .**.** <dl>.** <
3c2e0 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54  dt>SQLITE_DBSTAT
3c2f0 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45  US_LOOKASIDE_USE
3c300 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
3c310 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74  is parameter ret
3c320 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
3c330 6f 66 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d  of lookaside mem
3c340 6f 72 79 20 73 6c 6f 74 73 20 63 75 72 72 65 6e  ory slots curren
3c350 74 6c 79 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f  tly.** checked o
3c360 75 74 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c  ut.</dd>.** </dl
3c370 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  >.*/.#define SQL
3c380 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f  ITE_DBSTATUS_LOO
3c390 4b 41 53 49 44 45 5f 55 53 45 44 20 20 20 20 20  KASIDE_USED     
3c3a0 30 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  0.../*.** CAPI3R
3c3b0 45 46 3a 20 50 72 65 70 61 72 65 64 20 53 74 61  EF: Prepared Sta
3c3c0 74 65 6d 65 6e 74 20 53 74 61 74 75 73 20 7b 48  tement Status {H
3c3d0 31 37 35 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a  17550} <S60200>.
3c3e0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
3c3f0 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 72 65 70 61  **.** Each prepa
3c400 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 61  red statement ma
3c410 69 6e 74 61 69 6e 73 20 76 61 72 69 6f 75 73 0a  intains various.
3c420 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53  ** [SQLITE_STMTS
3c430 54 41 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75  TATUS_SORT | cou
3c440 6e 74 65 72 73 5d 20 74 68 61 74 20 6d 65 61 73  nters] that meas
3c450 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a  ure the number.*
3c460 2a 20 6f 66 20 74 69 6d 65 73 20 69 74 20 68 61  * of times it ha
3c470 73 20 70 65 72 66 6f 72 6d 65 64 20 73 70 65 63  s performed spec
3c480 69 66 69 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e  ific operations.
3c490 20 20 54 68 65 73 65 20 63 6f 75 6e 74 65 72 73    These counters
3c4a0 20 63 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20   can.** be used 
3c4b0 74 6f 20 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70  to monitor the p
3c4c0 65 72 66 6f 72 6d 61 6e 63 65 20 63 68 61 72 61  erformance chara
3c4d0 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68  cteristics of th
3c4e0 65 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74  e prepared.** st
3c4f0 61 74 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65  atements.  For e
3c500 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 6e  xample, if the n
3c510 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73  umber of table s
3c520 74 65 70 73 20 67 72 65 61 74 6c 79 20 65 78 63  teps greatly exc
3c530 65 65 64 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  eeds.** the numb
3c540 65 72 20 6f 66 20 74 61 62 6c 65 20 73 65 61 72  er of table sear
3c550 63 68 65 73 20 6f 72 20 72 65 73 75 6c 74 20 72  ches or result r
3c560 6f 77 73 2c 20 74 68 61 74 20 77 6f 75 6c 64 20  ows, that would 
3c570 74 65 6e 64 20 74 6f 20 69 6e 64 69 63 61 74 65  tend to indicate
3c580 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 72 65  .** that the pre
3c590 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
3c5a0 69 73 20 75 73 69 6e 67 20 61 20 66 75 6c 6c 20  is using a full 
3c5b0 74 61 62 6c 65 20 73 63 61 6e 20 72 61 74 68 65  table scan rathe
3c5c0 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64  r than.** an ind
3c5d0 65 78 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ex.  .**.** This
3c5e0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73   interface is us
3c5f0 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61  ed to retrieve a
3c600 6e 64 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72  nd reset counter
3c610 20 76 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20   values from.** 
3c620 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
3c630 65 6d 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72  ement].  The fir
3c640 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  st argument is t
3c650 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
3c660 65 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20  ement.** object 
3c670 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74  to be interrogat
3c680 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ed.  The second 
3c690 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61  argument.** is a
3c6a0 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66  n integer code f
3c6b0 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 5b 53  or a specific [S
3c6c0 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
3c6d0 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d  _SORT | counter]
3c6e0 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72  .** to be interr
3c6f0 6f 67 61 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20  ogated. .** The 
3c700 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66  current value of
3c710 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63   the requested c
3c720 6f 75 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e  ounter is return
3c730 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  ed..** If the re
3c740 73 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20  setFlg is true, 
3c750 74 68 65 6e 20 74 68 65 20 63 6f 75 6e 74 65 72  then the counter
3c760 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72   is reset to zer
3c770 6f 20 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20  o after this.** 
3c780 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 20 72  interface call r
3c790 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65  eturns..**.** Se
3c7a0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
3c7b0 5f 73 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b  _status()] and [
3c7c0 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75  sqlite3_db_statu
3c7d0 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  s()]..*/.SQLITE_
3c7e0 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52  API SQLITE_EXPER
3c7f0 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69  IMENTAL int sqli
3c800 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28  te3_stmt_status(
3c810 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
3c820 6e 74 20 6f 70 2c 69 6e 74 20 72 65 73 65 74 46  nt op,int resetF
3c830 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lg);../*.** CAPI
3c840 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72  3REF: Status Par
3c850 61 6d 65 74 65 72 73 20 66 6f 72 20 70 72 65 70  ameters for prep
3c860 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
3c870 7b 48 31 37 35 37 30 7d 20 3c 48 31 37 35 35 30  {H17570} <H17550
3c880 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
3c890 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72  L.**.** These pr
3c8a0 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f  eprocessor macro
3c8b0 73 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72  s define integer
3c8c0 20 63 6f 64 65 73 20 74 68 61 74 20 6e 61 6d 65   codes that name
3c8d0 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75   counter.** valu
3c8e0 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
3c8f0 74 68 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  th the [sqlite3_
3c900 73 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 20 69  stmt_status()] i
3c910 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65  nterface..** The
3c920 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65   meanings of the
3c930 20 76 61 72 69 6f 75 73 20 63 6f 75 6e 74 65 72   various counter
3c940 73 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73  s are as follows
3c950 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20  :.**.** <dl>.** 
3c960 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53  <dt>SQLITE_STMTS
3c970 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53  TATUS_FULLSCAN_S
3c980 54 45 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TEP</dt>.** <dd>
3c990 54 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62  This is the numb
3c9a0 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74  er of times that
3c9b0 20 53 51 4c 69 74 65 20 68 61 73 20 73 74 65 70   SQLite has step
3c9c0 70 65 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a  ped forward in.*
3c9d0 2a 20 61 20 74 61 62 6c 65 20 61 73 20 70 61 72  * a table as par
3c9e0 74 20 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c  t of a full tabl
3c9f0 65 20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e  e scan.  Large n
3ca00 75 6d 62 65 72 73 20 66 6f 72 20 74 68 69 73 20  umbers for this 
3ca10 63 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69  counter.** may i
3ca20 6e 64 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e  ndicate opportun
3ca30 69 74 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72  ities for perfor
3ca40 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e  mance improvemen
3ca50 74 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61  t through .** ca
3ca60 72 65 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64  reful use of ind
3ca70 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ices.</dd>.**.**
3ca80 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54   <dt>SQLITE_STMT
3ca90 53 54 41 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e  STATUS_SORT</dt>
3caa0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 73 20  .** <dd>This is 
3cab0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f  the number of so
3cac0 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68  rt operations th
3cad0 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  at have occurred
3cae0 2e 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20  ..** A non-zero 
3caf0 76 61 6c 75 65 20 69 6e 20 74 68 69 73 20 63 6f  value in this co
3cb00 75 6e 74 65 72 20 6d 61 79 20 69 6e 64 69 63 61  unter may indica
3cb10 74 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74  te an opportunit
3cb20 79 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d  y to.** improvem
3cb30 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ent performance 
3cb40 74 68 72 6f 75 67 68 20 63 61 72 65 66 75 6c 20  through careful 
3cb50 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c  use of indices.<
3cb60 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e  /dd>.**.** </dl>
3cb70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
3cb80 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55  TE_STMTSTATUS_FU
3cb90 4c 4c 53 43 41 4e 5f 53 54 45 50 20 20 20 20 20  LLSCAN_STEP     
3cba0 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
3cbb0 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54  _STMTSTATUS_SORT
3cbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a                2.
3cbd0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3cbe0 20 43 75 73 74 6f 6d 20 50 61 67 65 20 43 61 63   Custom Page Cac
3cbf0 68 65 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50  he Object.** EXP
3cc00 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
3cc10 54 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  The sqlite3_pcac
3cc20 68 65 20 74 79 70 65 20 69 73 20 6f 70 61 71 75  he type is opaqu
3cc30 65 2e 20 20 49 74 20 69 73 20 69 6d 70 6c 65 6d  e.  It is implem
3cc40 65 6e 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  ented by.** the 
3cc50 70 6c 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65  pluggable module
3cc60 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  .  The SQLite co
3cc70 72 65 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65  re has no knowle
3cc80 64 67 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69  dge of.** its si
3cc90 7a 65 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73  ze or internal s
3cca0 74 72 75 63 74 75 72 65 20 61 6e 64 20 6e 65 76  tructure and nev
3ccb0 65 72 20 64 65 61 6c 73 20 77 69 74 68 20 74 68  er deals with th
3ccc0 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61  e.** sqlite3_pca
3ccd0 63 68 65 20 6f 62 6a 65 63 74 20 65 78 63 65 70  che object excep
3cce0 74 20 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64  t by holding and
3ccf0 20 70 61 73 73 69 6e 67 20 70 6f 69 6e 74 65 72   passing pointer
3cd00 73 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65  s.** to the obje
3cd10 63 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73  ct..**.** See [s
3cd20 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
3cd30 74 68 6f 64 73 5d 20 66 6f 72 20 61 64 64 69 74  thods] for addit
3cd40 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
3cd50 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
3cd60 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61  ruct sqlite3_pca
3cd70 63 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  che sqlite3_pcac
3cd80 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  he;../*.** CAPI3
3cd90 52 45 46 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e  REF: Application
3cda0 20 44 65 66 69 6e 65 64 20 50 61 67 65 20 43 61   Defined Page Ca
3cdb0 63 68 65 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  che..** EXPERIME
3cdc0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  NTAL.**.** The [
3cdd0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28  sqlite3_config](
3cde0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  [SQLITE_CONFIG_P
3cdf0 43 41 43 48 45 5d 2c 20 2e 2e 2e 29 20 69 6e 74  CACHE], ...) int
3ce00 65 72 66 61 63 65 20 63 61 6e 0a 2a 2a 20 72 65  erface can.** re
3ce10 67 69 73 74 65 72 20 61 6e 20 61 6c 74 65 72 6e  gister an altern
3ce20 61 74 69 76 65 20 70 61 67 65 20 63 61 63 68 65  ative page cache
3ce30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3ce40 62 79 20 70 61 73 73 69 6e 67 20 69 6e 20 61 6e  by passing in an
3ce50 20 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66   .** instance of
3ce60 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
3ce70 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 74 72 75  che_methods stru
3ce80 63 74 75 72 65 2e 20 54 68 65 20 6d 61 6a 6f 72  cture. The major
3ce90 69 74 79 20 6f 66 20 74 68 65 20 0a 2a 2a 20 68  ity of the .** h
3cea0 65 61 70 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  eap memory used 
3ceb0 62 79 20 73 71 6c 69 74 65 20 69 73 20 75 73 65  by sqlite is use
3cec0 64 20 62 79 20 74 68 65 20 70 61 67 65 20 63 61  d by the page ca
3ced0 63 68 65 20 74 6f 20 63 61 63 68 65 20 64 61 74  che to cache dat
3cee0 61 20 72 65 61 64 20 0a 2a 2a 20 66 72 6f 6d 2c  a read .** from,
3cef0 20 6f 72 20 72 65 61 64 79 20 74 6f 20 62 65 20   or ready to be 
3cf00 77 72 69 74 74 65 6e 20 74 6f 2c 20 74 68 65 20  written to, the 
3cf10 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 42  database file. B
3cf20 79 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 61  y implementing a
3cf30 20 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 61 67 65   .** custom page
3cf40 20 63 61 63 68 65 20 75 73 69 6e 67 20 74 68 69   cache using thi
3cf50 73 20 41 50 49 2c 20 61 6e 20 61 70 70 6c 69 63  s API, an applic
3cf60 61 74 69 6f 6e 20 63 61 6e 20 63 6f 6e 74 72 6f  ation can contro
3cf70 6c 20 6d 6f 72 65 20 0a 2a 2a 20 70 72 65 63 69  l more .** preci
3cf80 73 65 6c 79 20 74 68 65 20 61 6d 6f 75 6e 74 20  sely the amount 
3cf90 6f 66 20 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d  of memory consum
3cfa0 65 64 20 62 79 20 73 71 6c 69 74 65 2c 20 74 68  ed by sqlite, th
3cfb0 65 20 77 61 79 20 69 6e 20 77 68 69 63 68 20 0a  e way in which .
3cfc0 2a 2a 20 73 61 69 64 20 6d 65 6d 6f 72 79 20 69  ** said memory i
3cfd0 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  s allocated and 
3cfe0 72 65 6c 65 61 73 65 64 2c 20 61 6e 64 20 74 68  released, and th
3cff0 65 20 70 6f 6c 69 63 69 65 73 20 75 73 65 64 20  e policies used 
3d000 74 6f 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65  to .** determine
3d010 20 65 78 61 63 74 6c 79 20 77 68 69 63 68 20 70   exactly which p
3d020 61 72 74 73 20 6f 66 20 61 20 64 61 74 61 62 61  arts of a databa
3d030 73 65 20 66 69 6c 65 20 61 72 65 20 63 61 63 68  se file are cach
3d040 65 64 20 61 6e 64 20 66 6f 72 20 0a 2a 2a 20 68  ed and for .** h
3d050 6f 77 20 6c 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  ow long..**.** T
3d060 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
3d070 68 65 20 73 74 72 75 63 74 75 72 65 20 61 72 65  he structure are
3d080 20 63 6f 70 69 65 64 20 74 6f 20 61 6e 20 69 6e   copied to an in
3d090 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 79  ternal buffer by
3d0a0 20 73 71 6c 69 74 65 0a 2a 2a 20 77 69 74 68 69   sqlite.** withi
3d0b0 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73  n the call to [s
3d0c0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 0a  qlite3_config]..
3d0d0 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 28  **.** The xInit(
3d0e0 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c  ) method is call
3d0f0 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
3d100 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
3d110 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 0a  3_initialize()].
3d120 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f 6e 6c 79  ** (usually only
3d130 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 74 68 65   once during the
3d140 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65   lifetime of the
3d150 20 70 72 6f 63 65 73 73 29 2e 20 49 74 20 69 73   process). It is
3d160 20 70 61 73 73 65 64 0a 2a 2a 20 61 20 63 6f 70   passed.** a cop
3d170 79 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  y of the sqlite3
3d180 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2e  _pcache_methods.
3d190 70 41 72 67 20 76 61 6c 75 65 2e 20 49 74 20 63  pArg value. It c
3d1a0 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
3d1b0 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 6c 20 73  t.** up global s
3d1c0 74 72 75 63 74 75 72 65 73 20 61 6e 64 20 6d 75  tructures and mu
3d1d0 74 65 78 65 73 20 72 65 71 75 69 72 65 64 20 62  texes required b
3d1e0 79 20 74 68 65 20 63 75 73 74 6f 6d 20 70 61 67  y the custom pag
3d1f0 65 20 63 61 63 68 65 20 0a 2a 2a 20 69 6d 70 6c  e cache .** impl
3d200 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 65 20  ementation. The 
3d210 78 53 68 75 74 64 6f 77 6e 28 29 20 6d 65 74 68  xShutdown() meth
3d220 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f  od is called fro
3d230 6d 20 77 69 74 68 69 6e 20 0a 2a 2a 20 5b 73 71  m within .** [sq
3d240 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
3d250 5d 2c 20 69 66 20 74 68 65 20 61 70 70 6c 69 63  ], if the applic
3d260 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 74 68  ation invokes th
3d270 69 73 20 41 50 49 2e 20 49 74 20 63 61 6e 20 62  is API. It can b
3d280 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 63 6c 65  e used.** to cle
3d290 61 6e 20 75 70 20 61 6e 79 20 6f 75 74 73 74 61  an up any outsta
3d2a0 6e 64 69 6e 67 20 72 65 73 6f 75 72 63 65 73 20  nding resources 
3d2b0 62 65 66 6f 72 65 20 70 72 6f 63 65 73 73 20 73  before process s
3d2c0 68 75 74 64 6f 77 6e 2c 20 69 66 20 72 65 71 75  hutdown, if requ
3d2d0 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ired..**.** The 
3d2e0 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64  xCreate() method
3d2f0 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73   is used to cons
3d300 74 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68  truct a new cach
3d310 65 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 0a  e instance. The.
3d320 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  ** first paramet
3d330 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73 20 74  er, szPage, is t
3d340 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  he size in bytes
3d350 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 74 68   of the pages th
3d360 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6c  at must.** be al
3d370 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 63  located by the c
3d380 61 63 68 65 2e 20 73 7a 50 61 67 65 20 77 69 6c  ache. szPage wil
3d390 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f 77 65 72  l not be a power
3d3a0 20 6f 66 20 74 77 6f 2e 20 54 68 65 0a 2a 2a 20   of two. The.** 
3d3b0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
3d3c0 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20   bPurgeable, is 
3d3d0 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68  true if the cach
3d3e0 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20  e being created 
3d3f0 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20  will.** be used 
3d400 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73  to cache databas
3d410 65 20 70 61 67 65 73 20 72 65 61 64 20 66 72 6f  e pages read fro
3d420 6d 20 61 20 66 69 6c 65 20 73 74 6f 72 65 64 20  m a file stored 
3d430 6f 6e 20 64 69 73 6b 2c 20 6f 72 0a 2a 2a 20 66  on disk, or.** f
3d440 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 75 73  alse if it is us
3d450 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d  ed for an in-mem
3d460 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20 54 68  ory database. Th
3d470 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
3d480 74 61 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e  tation.** does n
3d490 6f 74 20 68 61 76 65 20 74 6f 20 64 6f 20 61 6e  ot have to do an
3d4a0 79 74 68 69 6e 67 20 73 70 65 63 69 61 6c 20 62  ything special b
3d4b0 61 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c 75  ased on the valu
3d4c0 65 20 6f 66 20 62 50 75 72 67 65 61 62 6c 65 2c  e of bPurgeable,
3d4d0 0a 2a 2a 20 69 74 20 69 73 20 70 75 72 65 6c 79  .** it is purely
3d4e0 20 61 64 76 69 73 6f 72 79 2e 20 0a 2a 2a 0a 2a   advisory. .**.*
3d4f0 2a 20 54 68 65 20 78 43 61 63 68 65 73 69 7a 65  * The xCachesize
3d500 28 29 20 6d 65 74 68 6f 64 20 6d 61 79 20 62 65  () method may be
3d510 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79 20 74   called at any t
3d520 69 6d 65 20 62 79 20 53 51 4c 69 74 65 20 74 6f  ime by SQLite to
3d530 20 73 65 74 20 74 68 65 0a 2a 2a 20 73 75 67 67   set the.** sugg
3d540 65 73 74 65 64 20 6d 61 78 69 6d 75 6d 20 63 61  ested maximum ca
3d550 63 68 65 2d 73 69 7a 65 20 28 6e 75 6d 62 65 72  che-size (number
3d560 20 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64   of pages stored
3d570 20 62 79 29 20 74 68 65 20 63 61 63 68 65 0a 2a   by) the cache.*
3d580 2a 20 69 6e 73 74 61 6e 63 65 20 70 61 73 73 65  * instance passe
3d590 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
3d5a0 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  rgument. This is
3d5b0 20 74 68 65 20 76 61 6c 75 65 20 63 6f 6e 66 69   the value confi
3d5c0 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74  gured using.** t
3d5d0 68 65 20 53 51 4c 69 74 65 20 22 5b 50 52 41 47  he SQLite "[PRAG
3d5e0 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 5d 22 20  MA cache_size]" 
3d5f0 63 6f 6d 6d 61 6e 64 2e 20 41 73 20 77 69 74 68  command. As with
3d600 20 74 68 65 20 62 50 75 72 67 65 61 62 6c 65 20   the bPurgeable 
3d610 70 61 72 61 6d 65 74 65 72 2c 0a 2a 2a 20 74 68  parameter,.** th
3d620 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3d630 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
3d640 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20   to do anything 
3d650 73 70 65 63 69 61 6c 20 77 69 74 68 20 74 68 69  special with thi
3d660 73 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69  s.** value, it i
3d670 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e  s advisory only.
3d680 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65  .**.** The xPage
3d690 63 6f 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73  count() method s
3d6a0 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65  hould return the
3d6b0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
3d6c0 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74   currently.** st
3d6d0 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68  ored in the cach
3d6e0 65 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e  e supplied as an
3d6f0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a   argument..** .*
3d700 2a 20 54 68 65 20 78 46 65 74 63 68 28 29 20 6d  * The xFetch() m
3d710 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f  ethod is used to
3d720 20 66 65 74 63 68 20 61 20 70 61 67 65 20 61 6e   fetch a page an
3d730 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  d return a point
3d740 65 72 20 74 6f 20 69 74 2e 20 0a 2a 2a 20 41 20  er to it. .** A 
3d750 27 70 61 67 65 27 2c 20 69 6e 20 74 68 69 73 20  'page', in this 
3d760 63 6f 6e 74 65 78 74 2c 20 69 73 20 61 20 62 75  context, is a bu
3d770 66 66 65 72 20 6f 66 20 73 7a 50 61 67 65 20 62  ffer of szPage b
3d780 79 74 65 73 20 61 6c 69 67 6e 65 64 20 61 74 20  ytes aligned at 
3d790 61 6e 0a 2a 2a 20 38 2d 62 79 74 65 20 62 6f 75  an.** 8-byte bou
3d7a0 6e 64 61 72 79 2e 20 54 68 65 20 70 61 67 65 20  ndary. The page 
3d7b0 74 6f 20 62 65 20 66 65 74 63 68 65 64 20 69 73  to be fetched is
3d7c0 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
3d7d0 68 65 20 6b 65 79 2e 20 54 68 65 0a 2a 2a 20 6d  he key. The.** m
3d7e0 69 6d 69 6d 75 6d 20 6b 65 79 20 76 61 6c 75 65  imimum key value
3d7f0 20 69 73 20 31 2e 20 41 66 74 65 72 20 69 74 20   is 1. After it 
3d800 68 61 73 20 62 65 65 6e 20 72 65 74 72 69 65 76  has been retriev
3d810 65 64 20 75 73 69 6e 67 20 78 46 65 74 63 68 2c  ed using xFetch,
3d820 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 69 73   the page .** is
3d830 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62   considered to b
3d840 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  e pinned..**.** 
3d850 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  If the requested
3d860 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79   page is already
3d870 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63   in the page cac
3d880 68 65 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74  he, then a point
3d890 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 63 61 63  er to.** the cac
3d8a0 68 65 64 20 62 75 66 66 65 72 20 73 68 6f 75 6c  hed buffer shoul
3d8b0 64 20 62 65 20 72 65 74 75 72 6e 65 64 20 77 69  d be returned wi
3d8c0 74 68 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20  th its contents 
3d8d0 69 6e 74 61 63 74 2e 20 49 66 20 74 68 65 0a 2a  intact. If the.*
3d8e0 2a 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c  * page is not al
3d8f0 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63  ready in the cac
3d900 68 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70  he, then the exp
3d910 65 63 74 65 64 20 62 65 68 61 76 69 6f 75 72 20  ected behaviour 
3d920 6f 66 20 74 68 65 0a 2a 2a 20 63 61 63 68 65 20  of the.** cache 
3d930 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
3d940 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
3d950 65 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72  e createFlag par
3d960 61 6d 65 74 65 72 20 70 61 73 73 65 64 0a 2a 2a  ameter passed.**
3d970 20 74 6f 20 78 46 65 74 63 68 2c 20 61 63 63 6f   to xFetch, acco
3d980 72 64 69 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c  rding to the fol
3d990 6c 6f 77 69 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a  lowing table:.**
3d9a0 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65  .** <table borde
3d9b0 72 3d 31 20 77 69 64 74 68 3d 38 35 25 20 61 6c  r=1 width=85% al
3d9c0 69 67 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a 20 20  ign=center>.**  
3d9d0 20 3c 74 72 3e 3c 74 68 3e 63 72 65 61 74 65 46   <tr><th>createF
3d9e0 6c 61 67 3c 74 68 3e 45 78 70 65 63 74 65 64 20  lag<th>Expected 
3d9f0 42 65 68 61 76 69 6f 75 72 0a 2a 2a 20 20 20 3c  Behaviour.**   <
3da00 74 72 3e 3c 74 64 3e 30 3c 74 64 3e 4e 55 4c 4c  tr><td>0<td>NULL
3da10 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72   should be retur
3da20 6e 65 64 2e 20 4e 6f 20 6e 65 77 20 63 61 63 68  ned. No new cach
3da30 65 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74  e entry is creat
3da40 65 64 2e 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64  ed..**   <tr><td
3da50 3e 31 3c 74 64 3e 49 66 20 63 72 65 61 74 65 46  >1<td>If createF
3da60 6c 61 67 20 69 73 20 73 65 74 20 74 6f 20 31 2c  lag is set to 1,
3da70 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   this indicates 
3da80 74 68 61 74 20 0a 2a 2a 20 20 20 20 20 20 20 20  that .**        
3da90 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 69          SQLite i
3daa0 73 20 68 6f 6c 64 69 6e 67 20 70 69 6e 6e 65 64  s holding pinned
3dab0 20 70 61 67 65 73 20 74 68 61 74 20 63 61 6e 20   pages that can 
3dac0 62 65 20 75 6e 70 69 6e 6e 65 64 0a 2a 2a 20 20  be unpinned.**  
3dad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 79                by
3dae0 20 77 72 69 74 69 6e 67 20 74 68 65 69 72 20 63   writing their c
3daf0 6f 6e 74 65 6e 74 73 20 74 6f 20 74 68 65 20 64  ontents to the d
3db00 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 0a  atabase file (a.
3db10 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3db20 20 20 72 65 6c 61 74 69 76 65 6c 79 20 65 78 70    relatively exp
3db30 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e  ensive operation
3db40 29 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75 61  ). In this situa
3db50 74 69 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20  tion the.**     
3db60 20 20 20 20 20 20 20 20 20 20 20 63 61 63 68 65             cache
3db70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3db80 68 61 73 20 74 77 6f 20 63 68 6f 69 63 65 73 3a  has two choices:
3db90 20 69 74 20 63 61 6e 20 72 65 74 75 72 6e 20 4e   it can return N
3dba0 55 4c 4c 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ULL,.**         
3dbb0 20 20 20 20 20 20 20 69 6e 20 77 68 69 63 68 20         in which 
3dbc0 63 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c  case SQLite will
3dbd0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 6e 70 69   attempt to unpi
3dbe0 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 0a 2a  n one or more .*
3dbf0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3dc00 20 70 61 67 65 73 20 62 65 66 6f 72 65 20 72 65   pages before re
3dc10 2d 72 65 71 75 65 73 74 69 6e 67 20 74 68 65 20  -requesting the 
3dc20 73 61 6d 65 20 70 61 67 65 2c 20 6f 72 20 69 74  same page, or it
3dc30 20 63 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20   can.**         
3dc40 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 20         allocate 
3dc50 61 20 6e 65 77 20 70 61 67 65 20 61 6e 64 20 72  a new page and r
3dc60 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
3dc70 74 6f 20 69 74 2e 20 49 66 20 61 20 6e 65 77 0a  to it. If a new.
3dc80 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3dc90 20 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61    page is alloca
3dca0 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20 66 69  ted, then the fi
3dcb0 72 73 74 20 73 69 7a 65 6f 66 28 76 6f 69 64 2a  rst sizeof(void*
3dcc0 29 20 62 79 74 65 73 20 6f 66 0a 2a 2a 20 20 20  ) bytes of.**   
3dcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 74 20               it 
3dce0 28 61 74 20 6c 65 61 73 74 29 20 6d 75 73 74 20  (at least) must 
3dcf0 62 65 20 7a 65 72 6f 65 64 20 62 65 66 6f 72 65  be zeroed before
3dd00 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e   it is returned.
3dd10 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64 3e 32 3c  .**   <tr><td>2<
3dd20 74 64 3e 49 66 20 63 72 65 61 74 65 46 6c 61 67  td>If createFlag
3dd30 20 69 73 20 73 65 74 20 74 6f 20 32 2c 20 74 68   is set to 2, th
3dd40 65 6e 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74  en SQLite is not
3dd50 20 68 6f 6c 64 69 6e 67 20 61 6e 79 0a 2a 2a 20   holding any.** 
3dd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
3dd70 69 6e 6e 65 64 20 70 61 67 65 73 20 61 73 73 6f  inned pages asso
3dd80 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
3dd90 73 70 65 63 69 66 69 63 20 63 61 63 68 65 20 70  specific cache p
3dda0 61 73 73 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  assed.**        
3ddb0 20 20 20 20 20 20 20 20 61 73 20 74 68 65 20 66          as the f
3ddc0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
3ddd0 20 78 46 65 74 63 68 28 29 20 74 68 61 74 20 63   xFetch() that c
3dde0 61 6e 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20  an be unpinned. 
3ddf0 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  The.**          
3de00 20 20 20 20 20 20 63 61 63 68 65 20 69 6d 70 6c        cache impl
3de10 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c  ementation shoul
3de20 64 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c  d attempt to all
3de30 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20  ocate a new.**  
3de40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 61                ca
3de50 63 68 65 20 65 6e 74 72 79 20 61 6e 64 20 72 65  che entry and re
3de60 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
3de70 6f 20 69 74 2e 20 41 67 61 69 6e 2c 20 74 68 65  o it. Again, the
3de80 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 20   first.**       
3de90 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28           sizeof(
3dea0 76 6f 69 64 2a 29 20 62 79 74 65 73 20 6f 66 20  void*) bytes of 
3deb0 74 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64 20  the page should 
3dec0 62 65 20 7a 65 72 6f 65 64 20 62 65 66 6f 72 65  be zeroed before
3ded0 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
3dee0 20 20 20 20 69 74 20 69 73 20 72 65 74 75 72 6e      it is return
3def0 65 64 2e 20 49 66 20 74 68 65 20 78 46 65 74 63  ed. If the xFetc
3df00 68 28 29 20 6d 65 74 68 6f 64 20 72 65 74 75 72  h() method retur
3df10 6e 73 20 4e 55 4c 4c 20 77 68 65 6e 20 0a 2a 2a  ns NULL when .**
3df20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3df30 63 72 65 61 74 65 46 6c 61 67 3d 3d 32 2c 20 53  createFlag==2, S
3df40 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74 68  QLite assumes th
3df50 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  at a memory allo
3df60 63 61 74 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20  cation .**      
3df70 20 20 20 20 20 20 20 20 20 20 66 61 69 6c 65 64            failed
3df80 20 61 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c   and returns SQL
3df90 49 54 45 5f 4e 4f 4d 45 4d 20 74 6f 20 74 68 65  ITE_NOMEM to the
3dfa0 20 75 73 65 72 2e 0a 2a 2a 20 3c 2f 74 61 62 6c   user..** </tabl
3dfb0 65 3e 0a 2a 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28  e>.**.** xUnpin(
3dfc0 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53  ) is called by S
3dfd0 51 4c 69 74 65 20 77 69 74 68 20 61 20 70 6f 69  QLite with a poi
3dfe0 6e 74 65 72 20 74 6f 20 61 20 63 75 72 72 65 6e  nter to a curren
3dff0 74 6c 79 20 70 69 6e 6e 65 64 20 70 61 67 65 0a  tly pinned page.
3e000 2a 2a 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64  ** as its second
3e010 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 68   argument. If th
3e020 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
3e030 72 2c 20 64 69 73 63 61 72 64 2c 20 69 73 20 6e  r, discard, is n
3e040 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e  on-zero,.** then
3e050 20 74 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64   the page should
3e060 20 62 65 20 65 76 69 63 74 65 64 20 66 72 6f 6d   be evicted from
3e070 20 74 68 65 20 63 61 63 68 65 2e 20 49 6e 20 74   the cache. In t
3e080 68 69 73 20 63 61 73 65 20 53 51 4c 69 74 65 20  his case SQLite 
3e090 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74  .** assumes that
3e0a0 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74   the next time t
3e0b0 68 65 20 70 61 67 65 20 69 73 20 72 65 74 72 69  he page is retri
3e0c0 65 76 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61  eved from the ca
3e0d0 63 68 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65  che using.** the
3e0e0 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64   xFetch() method
3e0f0 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 7a 65 72  , it will be zer
3e100 6f 65 64 2e 20 49 66 20 74 68 65 20 64 69 73 63  oed. If the disc
3e110 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  ard parameter is
3e120 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  .** zero, then t
3e130 68 65 20 70 61 67 65 20 69 73 20 63 6f 6e 73 69  he page is consi
3e140 64 65 72 65 64 20 74 6f 20 62 65 20 75 6e 70 69  dered to be unpi
3e150 6e 6e 65 64 2e 20 54 68 65 20 63 61 63 68 65 20  nned. The cache 
3e160 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
3e170 2a 20 6d 61 79 20 63 68 6f 6f 73 65 20 74 6f 20  * may choose to 
3e180 72 65 63 6c 61 69 6d 20 28 66 72 65 65 20 6f 72  reclaim (free or
3e190 20 72 65 63 79 63 6c 65 29 20 75 6e 70 69 6e 6e   recycle) unpinn
3e1a0 65 64 20 70 61 67 65 73 20 61 74 20 61 6e 79 20  ed pages at any 
3e1b0 74 69 6d 65 2e 0a 2a 2a 20 53 51 4c 69 74 65 20  time..** SQLite 
3e1c0 61 73 73 75 6d 65 73 20 74 68 61 74 20 6e 65 78  assumes that nex
3e1d0 74 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 20  t time the page 
3e1e0 69 73 20 72 65 74 72 69 65 76 65 64 20 66 72 6f  is retrieved fro
3e1f0 6d 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69  m the cache.** i
3e200 74 20 77 69 6c 6c 20 65 69 74 68 65 72 20 62 65  t will either be
3e210 20 7a 65 72 6f 65 64 2c 20 6f 72 20 63 6f 6e 74   zeroed, or cont
3e220 61 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74  ain the same dat
3e230 61 20 74 68 61 74 20 69 74 20 64 69 64 20 77 68  a that it did wh
3e240 65 6e 20 69 74 0a 2a 2a 20 77 61 73 20 75 6e 70  en it.** was unp
3e250 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  inned..**.** The
3e260 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 72 65   cache is not re
3e270 71 75 69 72 65 64 20 74 6f 20 70 65 72 66 6f 72  quired to perfor
3e280 6d 20 61 6e 79 20 72 65 66 65 72 65 6e 63 65 20  m any reference 
3e290 63 6f 75 6e 74 69 6e 67 2e 20 41 20 73 69 6e 67  counting. A sing
3e2a0 6c 65 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78  le .** call to x
3e2b0 55 6e 70 69 6e 28 29 20 75 6e 70 69 6e 73 20 74  Unpin() unpins t
3e2c0 68 65 20 70 61 67 65 20 72 65 67 61 72 64 6c 65  he page regardle
3e2d0 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  ss of the number
3e2e0 20 6f 66 20 70 72 69 6f 72 20 63 61 6c 6c 73 20   of prior calls 
3e2f0 0a 2a 2a 20 74 6f 20 78 46 65 74 63 68 28 29 2e  .** to xFetch().
3e300 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 65 6b 65  .**.** The xReke
3e310 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73  y() method is us
3e320 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  ed to change the
3e330 20 6b 65 79 20 76 61 6c 75 65 20 61 73 73 6f 63   key value assoc
3e340 69 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 2a  iated with the.*
3e350 2a 20 70 61 67 65 20 70 61 73 73 65 64 20 61 73  * page passed as
3e360 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
3e370 6d 65 6e 74 20 66 72 6f 6d 20 6f 6c 64 4b 65 79  ment from oldKey
3e380 20 74 6f 20 6e 65 77 4b 65 79 2e 20 49 66 20 74   to newKey. If t
3e390 68 65 20 63 61 63 68 65 0a 2a 2a 20 70 72 65 76  he cache.** prev
3e3a0 69 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e 73 20  iously contains 
3e3b0 61 6e 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61  an entry associa
3e3c0 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 2c  ted with newKey,
3e3d0 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a   it should be.**
3e3e0 20 64 69 73 63 61 72 64 65 64 2e 20 41 6e 79 20   discarded. Any 
3e3f0 70 72 69 6f 72 20 63 61 63 68 65 20 65 6e 74 72  prior cache entr
3e400 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
3e410 68 20 6e 65 77 4b 65 79 20 69 73 20 67 75 61 72  h newKey is guar
3e420 61 6e 74 65 65 64 20 6e 6f 74 0a 2a 2a 20 74 6f  anteed not.** to
3e430 20 62 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a   be pinned..**.*
3e440 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 20 63 61  * When SQLite ca
3e450 6c 6c 73 20 74 68 65 20 78 54 72 75 6e 63 61 74  lls the xTruncat
3e460 65 28 29 20 6d 65 74 68 6f 64 2c 20 74 68 65 20  e() method, the 
3e470 63 61 63 68 65 20 6d 75 73 74 20 64 69 73 63 61  cache must disca
3e480 72 64 20 61 6c 6c 0a 2a 2a 20 65 78 69 73 74 69  rd all.** existi
3e490 6e 67 20 63 61 63 68 65 20 65 6e 74 72 69 65 73  ng cache entries
3e4a0 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65   with page numbe
3e4b0 72 73 20 28 6b 65 79 73 29 20 67 72 65 61 74 65  rs (keys) greate
3e4c0 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a  r than or equal.
3e4d0 2a 2a 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  ** to the value 
3e4e0 6f 66 20 74 68 65 20 69 4c 69 6d 69 74 20 70 61  of the iLimit pa
3e4f0 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74  rameter passed t
3e500 6f 20 78 54 72 75 6e 63 61 74 65 28 29 2e 20 49  o xTruncate(). I
3e510 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73  f any.** of thes
3e520 65 20 70 61 67 65 73 20 61 72 65 20 70 69 6e 6e  e pages are pinn
3e530 65 64 2c 20 74 68 65 79 20 61 72 65 20 69 6d 70  ed, they are imp
3e540 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64  licitly unpinned
3e550 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 0a 2a  , meaning that.*
3e560 2a 20 74 68 65 79 20 63 61 6e 20 62 65 20 73 61  * they can be sa
3e570 66 65 6c 79 20 64 69 73 63 61 72 64 65 64 2e 0a  fely discarded..
3e580 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 73 74 72  **.** The xDestr
3e590 6f 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75  oy() method is u
3e5a0 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20  sed to delete a 
3e5b0 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  cache allocated 
3e5c0 62 79 20 78 43 72 65 61 74 65 28 29 2e 0a 2a 2a  by xCreate()..**
3e5d0 20 41 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61   All resources a
3e5e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
3e5f0 68 65 20 73 70 65 63 69 66 69 65 64 20 63 61 63  he specified cac
3e600 68 65 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65  he should be fre
3e610 65 64 2e 20 41 66 74 65 72 0a 2a 2a 20 63 61 6c  ed. After.** cal
3e620 6c 69 6e 67 20 74 68 65 20 78 44 65 73 74 72 6f  ling the xDestro
3e630 79 28 29 20 6d 65 74 68 6f 64 2c 20 53 51 4c 69  y() method, SQLi
3e640 74 65 20 63 6f 6e 73 69 64 65 72 73 20 74 68 65  te considers the
3e650 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65   [sqlite3_pcache
3e660 2a 5d 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 6e 76  *].** handle inv
3e670 61 6c 69 64 2c 20 61 6e 64 20 77 69 6c 6c 20 6e  alid, and will n
3e680 6f 74 20 75 73 65 20 69 74 20 77 69 74 68 20 61  ot use it with a
3e690 6e 79 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33  ny other sqlite3
3e6a0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 0a  _pcache_methods.
3e6b0 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f  ** functions..*/
3e6c0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
3e6d0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
3e6e0 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 70  ethods sqlite3_p
3e6f0 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3b 0a 73  cache_methods;.s
3e700 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63  truct sqlite3_pc
3e710 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 7b 0a 20  ache_methods {. 
3e720 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 69   void *pArg;.  i
3e730 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64  nt (*xInit)(void
3e740 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 53 68  *);.  void (*xSh
3e750 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 0a  utdown)(void*);.
3e760 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65    sqlite3_pcache
3e770 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69 6e 74   *(*xCreate)(int
3e780 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 50 75   szPage, int bPu
3e790 72 67 65 61 62 6c 65 29 3b 0a 20 20 76 6f 69 64  rgeable);.  void
3e7a0 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29 28 73   (*xCachesize)(s
3e7b0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20  qlite3_pcache*, 
3e7c0 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65 29 3b  int nCachesize);
3e7d0 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65 63 6f  .  int (*xPageco
3e7e0 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70 63 61  unt)(sqlite3_pca
3e7f0 63 68 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28  che*);.  void *(
3e800 2a 78 46 65 74 63 68 29 28 73 71 6c 69 74 65 33  *xFetch)(sqlite3
3e810 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e  _pcache*, unsign
3e820 65 64 20 6b 65 79 2c 20 69 6e 74 20 63 72 65 61  ed key, int crea
3e830 74 65 46 6c 61 67 29 3b 0a 20 20 76 6f 69 64 20  teFlag);.  void 
3e840 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69 74 65  (*xUnpin)(sqlite
3e850 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a  3_pcache*, void*
3e860 2c 20 69 6e 74 20 64 69 73 63 61 72 64 29 3b 0a  , int discard);.
3e870 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65 79 29    void (*xRekey)
3e880 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
3e890 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65  , void*, unsigne
3e8a0 64 20 6f 6c 64 4b 65 79 2c 20 75 6e 73 69 67 6e  d oldKey, unsign
3e8b0 65 64 20 6e 65 77 4b 65 79 29 3b 0a 20 20 76 6f  ed newKey);.  vo
3e8c0 69 64 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28  id (*xTruncate)(
3e8d0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c  sqlite3_pcache*,
3e8e0 20 75 6e 73 69 67 6e 65 64 20 69 4c 69 6d 69 74   unsigned iLimit
3e8f0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73  );.  void (*xDes
3e900 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 70 63  troy)(sqlite3_pc
3e910 61 63 68 65 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  ache*);.};../*.*
3e920 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 6c 69  * CAPI3REF: Onli
3e930 6e 65 20 42 61 63 6b 75 70 20 4f 62 6a 65 63 74  ne Backup Object
3e940 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
3e950 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
3e960 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74  e3_backup object
3e970 20 72 65 63 6f 72 64 73 20 73 74 61 74 65 20 69   records state i
3e980 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
3e990 20 61 6e 20 6f 6e 67 6f 69 6e 67 0a 2a 2a 20 6f   an ongoing.** o
3e9a0 6e 6c 69 6e 65 20 62 61 63 6b 75 70 20 6f 70 65  nline backup ope
3e9b0 72 61 74 69 6f 6e 2e 20 20 54 68 65 20 73 71 6c  ration.  The sql
3e9c0 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65  ite3_backup obje
3e9d0 63 74 20 69 73 20 63 72 65 61 74 65 64 20 62 79  ct is created by
3e9e0 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73  .** a call to [s
3e9f0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e  qlite3_backup_in
3ea00 69 74 28 29 5d 20 61 6e 64 20 69 73 20 64 65 73  it()] and is des
3ea10 74 72 6f 79 65 64 20 62 79 20 61 20 63 61 6c 6c  troyed by a call
3ea20 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
3ea30 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 5d  backup_finish()]
3ea40 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f  ..**.** See Also
3ea50 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c  : [Using the SQL
3ea60 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75  ite Online Backu
3ea70 70 20 41 50 49 5d 0a 2a 2f 0a 74 79 70 65 64 65  p API].*/.typede
3ea80 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
3ea90 5f 62 61 63 6b 75 70 20 73 71 6c 69 74 65 33 5f  _backup sqlite3_
3eaa0 62 61 63 6b 75 70 3b 0a 0a 2f 2a 0a 2a 2a 20 43  backup;../*.** C
3eab0 41 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e 65 20  API3REF: Online 
3eac0 42 61 63 6b 75 70 20 41 50 49 2e 0a 2a 2a 20 45  Backup API..** E
3ead0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
3eae0 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 75 73  * This API is us
3eaf0 65 64 20 74 6f 20 6f 76 65 72 77 72 69 74 65 20  ed to overwrite 
3eb00 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
3eb10 6f 6e 65 20 64 61 74 61 62 61 73 65 20 77 69 74  one database wit
3eb20 68 20 74 68 61 74 0a 2a 2a 20 6f 66 20 61 6e 6f  h that.** of ano
3eb30 74 68 65 72 2e 20 49 74 20 69 73 20 75 73 65 66  ther. It is usef
3eb40 75 6c 20 65 69 74 68 65 72 20 66 6f 72 20 63 72  ul either for cr
3eb50 65 61 74 69 6e 67 20 62 61 63 6b 75 70 73 20 6f  eating backups o
3eb60 66 20 64 61 74 61 62 61 73 65 73 20 6f 72 0a 2a  f databases or.*
3eb70 2a 20 66 6f 72 20 63 6f 70 79 69 6e 67 20 69 6e  * for copying in
3eb80 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
3eb90 73 20 74 6f 20 6f 72 20 66 72 6f 6d 20 70 65 72  s to or from per
3eba0 73 69 73 74 65 6e 74 20 66 69 6c 65 73 2e 20 0a  sistent files. .
3ebb0 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20  **.** See Also: 
3ebc0 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74  [Using the SQLit
3ebd0 65 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20  e Online Backup 
3ebe0 41 50 49 5d 0a 2a 2a 0a 2a 2a 20 45 78 63 6c 75  API].**.** Exclu
3ebf0 73 69 76 65 20 61 63 63 65 73 73 20 69 73 20 72  sive access is r
3ec00 65 71 75 69 72 65 64 20 74 6f 20 74 68 65 20 64  equired to the d
3ec10 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
3ec20 61 73 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20  ase for the .** 
3ec30 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  duration of the 
3ec40 6f 70 65 72 61 74 69 6f 6e 2e 20 48 6f 77 65 76  operation. Howev
3ec50 65 72 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  er the source da
3ec60 74 61 62 61 73 65 20 69 73 20 6f 6e 6c 79 0a 2a  tabase is only.*
3ec70 2a 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20 77 68  * read-locked wh
3ec80 69 6c 65 20 69 74 20 69 73 20 61 63 74 75 61 6c  ile it is actual
3ec90 6c 79 20 62 65 69 6e 67 20 72 65 61 64 2c 20 69  ly being read, i
3eca0 74 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 0a  t is not locked.
3ecb0 2a 2a 20 63 6f 6e 74 69 6e 75 6f 75 73 6c 79 20  ** continuously 
3ecc0 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65 20 6f  for the entire o
3ecd0 70 65 72 61 74 69 6f 6e 2e 20 54 68 75 73 2c 20  peration. Thus, 
3ece0 74 68 65 20 62 61 63 6b 75 70 20 6d 61 79 20 62  the backup may b
3ecf0 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 6f  e.** performed o
3ed00 6e 20 61 20 6c 69 76 65 20 64 61 74 61 62 61 73  n a live databas
3ed10 65 20 77 69 74 68 6f 75 74 20 70 72 65 76 65 6e  e without preven
3ed20 74 69 6e 67 20 6f 74 68 65 72 20 75 73 65 72 73  ting other users
3ed30 20 66 72 6f 6d 0a 2a 2a 20 77 72 69 74 69 6e 67   from.** writing
3ed40 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
3ed50 20 66 6f 72 20 61 6e 20 65 78 74 65 6e 64 65 64   for an extended
3ed60 20 70 65 72 69 6f 64 20 6f 66 20 74 69 6d 65 2e   period of time.
3ed70 0a 2a 2a 20 0a 2a 2a 20 54 6f 20 70 65 72 66 6f  .** .** To perfo
3ed80 72 6d 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72  rm a backup oper
3ed90 61 74 69 6f 6e 3a 20 0a 2a 2a 20 20 20 3c 6f 6c  ation: .**   <ol
3eda0 3e 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e  >.**     <li><b>
3edb0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
3edc0 6e 69 74 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c  nit()</b> is cal
3edd0 6c 65 64 20 6f 6e 63 65 20 74 6f 20 69 6e 69 74  led once to init
3ede0 69 61 6c 69 7a 65 20 74 68 65 0a 2a 2a 20 20 20  ialize the.**   
3edf0 20 20 20 20 20 20 62 61 63 6b 75 70 2c 20 0a 2a        backup, .*
3ee00 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c  *     <li><b>sql
3ee10 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
3ee20 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64  ()</b> is called
3ee30 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d   one or more tim
3ee40 65 73 20 74 6f 20 74 72 61 6e 73 66 65 72 20 0a  es to transfer .
3ee50 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 20 64  **         the d
3ee60 61 74 61 20 62 65 74 77 65 65 6e 20 74 68 65 20  ata between the 
3ee70 74 77 6f 20 64 61 74 61 62 61 73 65 73 2c 20 61  two databases, a
3ee80 6e 64 20 66 69 6e 61 6c 6c 79 0a 2a 2a 20 20 20  nd finally.**   
3ee90 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33    <li><b>sqlite3
3eea0 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
3eeb0 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 74  </b> is called t
3eec0 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 72 65  o release all re
3eed0 73 6f 75 72 63 65 73 20 0a 2a 2a 20 20 20 20 20  sources .**     
3eee0 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 77      associated w
3eef0 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 20 6f  ith the backup o
3ef00 70 65 72 61 74 69 6f 6e 2e 20 0a 2a 2a 20 20 20  peration. .**   
3ef10 3c 2f 6f 6c 3e 0a 2a 2a 20 54 68 65 72 65 20 73  </ol>.** There s
3ef20 68 6f 75 6c 64 20 62 65 20 65 78 61 63 74 6c 79  hould be exactly
3ef30 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c   one call to sql
3ef40 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
3ef50 73 68 28 29 20 66 6f 72 20 65 61 63 68 0a 2a 2a  sh() for each.**
3ef60 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
3ef70 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
3ef80 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a  up_init()..**.**
3ef90 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b   <b>sqlite3_back
3efa0 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 0a 2a 2a  up_init()</b>.**
3efb0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77  .** The first tw
3efc0 6f 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73  o arguments pass
3efd0 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62  ed to [sqlite3_b
3efe0 61 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 72  ackup_init()] ar
3eff0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  e the database.*
3f000 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61  * handle associa
3f010 74 65 64 20 77 69 74 68 20 74 68 65 20 64 65 73  ted with the des
3f020 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
3f030 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  e and the databa
3f040 73 65 20 6e 61 6d 65 20 0a 2a 2a 20 75 73 65 64  se name .** used
3f050 20 74 6f 20 61 74 74 61 63 68 20 74 68 65 20 64   to attach the d
3f060 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
3f070 61 73 65 20 74 6f 20 74 68 65 20 68 61 6e 64 6c  ase to the handl
3f080 65 2e 20 54 68 65 20 64 61 74 61 62 61 73 65 20  e. The database 
3f090 6e 61 6d 65 0a 2a 2a 20 69 73 20 22 6d 61 69 6e  name.** is "main
3f0a0 22 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  " for the main d
3f0b0 61 74 61 62 61 73 65 2c 20 22 74 65 6d 70 22 20  atabase, "temp" 
3f0c0 66 6f 72 20 74 68 65 20 74 65 6d 70 6f 72 61 72  for the temporar
3f0d0 79 20 64 61 74 61 62 61 73 65 2c 20 6f 72 0a 2a  y database, or.*
3f0e0 2a 20 74 68 65 20 6e 61 6d 65 20 73 70 65 63 69  * the name speci
3f0f0 66 69 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  fied as part of 
3f100 74 68 65 20 5b 41 54 54 41 43 48 5d 20 73 74 61  the [ATTACH] sta
3f110 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20 64 65  tement if the de
3f120 73 74 69 6e 61 74 69 6f 6e 20 69 73 0a 2a 2a 20  stination is.** 
3f130 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61  an attached data
3f140 62 61 73 65 2e 20 54 68 65 20 74 68 69 72 64 20  base. The third 
3f150 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d  and fourth argum
3f160 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 0a  ents passed to .
3f170 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
3f180 70 5f 69 6e 69 74 28 29 20 69 64 65 6e 74 69 66  p_init() identif
3f190 79 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  y the [database 
3f1a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 61  connection].** a
3f1b0 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  nd database name
3f1c0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65   used.** to acce
3f1d0 73 73 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  ss the source da
3f1e0 74 61 62 61 73 65 2e 20 54 68 65 20 76 61 6c 75  tabase. The valu
3f1f0 65 73 20 70 61 73 73 65 64 20 66 6f 72 20 74 68  es passed for th
3f200 65 20 73 6f 75 72 63 65 20 61 6e 64 20 0a 2a 2a  e source and .**
3f210 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61   destination [da
3f220 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3f230 6e 5d 20 70 61 72 61 6d 65 74 65 72 73 20 6d 75  n] parameters mu
3f240 73 74 20 6e 6f 74 20 62 65 20 74 68 65 20 73 61  st not be the sa
3f250 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  me..**.** If an 
3f260 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74  error occurs wit
3f270 68 69 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  hin sqlite3_back
3f280 75 70 5f 69 6e 69 74 28 29 2c 20 74 68 65 6e 20  up_init(), then 
3f290 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
3f2a0 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  .** and an error
3f2b0 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20   code and error 
3f2c0 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20  message written 
3f2d0 69 6e 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61  into the [databa
3f2e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 0a  se connection] .
3f2f0 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ** passed as the
3f300 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
3f310 20 54 68 65 79 20 6d 61 79 20 62 65 20 72 65 74   They may be ret
3f320 72 69 65 76 65 64 20 75 73 69 6e 67 20 74 68 65  rieved using the
3f330 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  .** [sqlite3_err
3f340 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69 74 65  code()], [sqlite
3f350 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 61 6e 64  3_errmsg()], and
3f360 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
3f370 31 36 28 29 5d 20 66 75 6e 63 74 69 6f 6e 73 2e  16()] functions.
3f380 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69  .** Otherwise, i
3f390 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 20  f successful, a 
3f3a0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 5b 73  pointer to an [s
3f3b0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f  qlite3_backup] o
3f3c0 62 6a 65 63 74 20 69 73 0a 2a 2a 20 72 65 74 75  bject is.** retu
3f3d0 72 6e 65 64 2e 20 54 68 69 73 20 70 6f 69 6e 74  rned. This point
3f3e0 65 72 20 6d 61 79 20 62 65 20 75 73 65 64 20 77  er may be used w
3f3f0 69 74 68 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ith the sqlite3_
3f400 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 61 6e  backup_step() an
3f410 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63  d.** sqlite3_bac
3f420 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 66 75 6e  kup_finish() fun
3f430 63 74 69 6f 6e 73 20 74 6f 20 70 65 72 66 6f 72  ctions to perfor
3f440 6d 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  m the specified 
3f450 62 61 63 6b 75 70 20 0a 2a 2a 20 6f 70 65 72 61  backup .** opera
3f460 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73  tion..**.** <b>s
3f470 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
3f480 65 70 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 46  ep()</b>.**.** F
3f490 75 6e 63 74 69 6f 6e 20 5b 73 71 6c 69 74 65 33  unction [sqlite3
3f4a0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 5d 20  _backup_step()] 
3f4b0 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70 79 20  is used to copy 
3f4c0 75 70 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65  up to nPage page
3f4d0 73 20 62 65 74 77 65 65 6e 20 0a 2a 2a 20 74 68  s between .** th
3f4e0 65 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73  e source and des
3f4f0 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
3f500 65 73 2c 20 77 68 65 72 65 20 6e 50 61 67 65 20  es, where nPage 
3f510 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  is the value of 
3f520 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20 70  the .** second p
3f530 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20  arameter passed 
3f540 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
3f550 70 5f 73 74 65 70 28 29 2e 20 49 66 20 6e 50 61  p_step(). If nPa
3f560 67 65 20 69 73 20 61 20 6e 65 67 61 74 69 76 65  ge is a negative
3f570 0a 2a 2a 20 76 61 6c 75 65 2c 20 61 6c 6c 20 72  .** value, all r
3f580 65 6d 61 69 6e 69 6e 67 20 73 6f 75 72 63 65 20  emaining source 
3f590 70 61 67 65 73 20 61 72 65 20 63 6f 70 69 65 64  pages are copied
3f5a0 2e 20 49 66 20 74 68 65 20 72 65 71 75 69 72 65  . If the require
3f5b0 64 20 70 61 67 65 73 20 61 72 65 20 0a 2a 2a 20  d pages are .** 
3f5c0 73 75 63 63 65 73 66 75 6c 6c 79 20 63 6f 70 69  succesfully copi
3f5d0 65 64 2c 20 62 75 74 20 74 68 65 72 65 20 61 72  ed, but there ar
3f5e0 65 20 73 74 69 6c 6c 20 6d 6f 72 65 20 70 61 67  e still more pag
3f5f0 65 73 20 74 6f 20 63 6f 70 79 20 62 65 66 6f 72  es to copy befor
3f600 65 20 74 68 65 20 0a 2a 2a 20 62 61 63 6b 75 70  e the .** backup
3f610 20 69 73 20 63 6f 6d 70 6c 65 74 65 2c 20 69 74   is complete, it
3f620 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
3f630 5f 4f 4b 5d 2e 20 49 66 20 6e 6f 20 65 72 72 6f  _OK]. If no erro
3f640 72 20 6f 63 63 75 72 65 64 20 61 6e 64 20 74 68  r occured and th
3f650 65 72 65 20 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d  ere .** are no m
3f660 6f 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70  ore pages to cop
3f670 79 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f  y, then [SQLITE_
3f680 44 4f 4e 45 5d 20 69 73 20 72 65 74 75 72 6e 65  DONE] is returne
3f690 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a  d. If an error .
3f6a0 2a 2a 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20  ** occurs, then 
3f6b0 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
3f6c0 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
3f6d0 2e 20 41 73 20 77 65 6c 6c 20 61 73 20 5b 53 51  . As well as [SQ
3f6e0 4c 49 54 45 5f 4f 4b 5d 20 61 6e 64 0a 2a 2a 20  LITE_OK] and.** 
3f6f0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 61  [SQLITE_DONE], a
3f700 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
3f710 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6d  _backup_step() m
3f720 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  ay return [SQLIT
3f730 45 5f 52 45 41 44 4f 4e 4c 59 5d 2c 0a 2a 2a 20  E_READONLY],.** 
3f740 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20  [SQLITE_NOMEM], 
3f750 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 5b  [SQLITE_BUSY], [
3f760 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 2c 20  SQLITE_LOCKED], 
3f770 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45  or an.** [SQLITE
3f780 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 7c 20  _IOERR_ACCESS | 
3f790 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58  SQLITE_IOERR_XXX
3f7a0 5d 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72  ] extended error
3f7b0 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 73 20   code..**.** As 
3f7c0 77 65 6c 6c 20 61 73 20 74 68 65 20 63 61 73 65  well as the case
3f7d0 20 77 68 65 72 65 20 74 68 65 20 64 65 73 74 69   where the desti
3f7e0 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
3f7f0 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 20  file was opened 
3f800 66 6f 72 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79  for.** read-only
3f810 20 61 63 63 65 73 73 2c 20 73 71 6c 69 74 65 33   access, sqlite3
3f820 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6d  _backup_step() m
3f830 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  ay return [SQLIT
3f840 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 66 0a 2a  E_READONLY] if.*
3f850 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  * the destinatio
3f860 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  n is an in-memor
3f870 79 20 64 61 74 61 62 61 73 65 20 77 69 74 68 20  y database with 
3f880 61 20 64 69 66 66 65 72 65 6e 74 20 70 61 67 65  a different page
3f890 20 73 69 7a 65 0a 2a 2a 20 66 72 6f 6d 20 74 68   size.** from th
3f8a0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
3f8b0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69  e..**.** If sqli
3f8c0 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
3f8d0 29 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 20  ) cannot obtain 
3f8e0 61 20 72 65 71 75 69 72 65 64 20 66 69 6c 65 2d  a required file-
3f8f0 73 79 73 74 65 6d 20 6c 6f 63 6b 2c 20 74 68 65  system lock, the
3f900 6e 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65  n.** the [sqlite
3f910 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c  3_busy_handler |
3f920 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66 75   busy-handler fu
3f930 6e 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20 69 6e  nction].** is in
3f940 76 6f 6b 65 64 20 28 69 66 20 6f 6e 65 20 69 73  voked (if one is
3f950 20 73 70 65 63 69 66 69 65 64 29 2e 20 49 66 20   specified). If 
3f960 74 68 65 20 0a 2a 2a 20 62 75 73 79 2d 68 61 6e  the .** busy-han
3f970 64 6c 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e  dler returns non
3f980 2d 7a 65 72 6f 20 62 65 66 6f 72 65 20 74 68 65  -zero before the
3f990 20 6c 6f 63 6b 20 69 73 20 61 76 61 69 6c 61 62   lock is availab
3f9a0 6c 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 5b 53 51  le, then .** [SQ
3f9b0 4c 49 54 45 5f 42 55 53 59 5d 20 69 73 20 72 65  LITE_BUSY] is re
3f9c0 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61  turned to the ca
3f9d0 6c 6c 65 72 2e 20 49 6e 20 74 68 69 73 20 63 61  ller. In this ca
3f9e0 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a 2a  se the call to.*
3f9f0 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  * sqlite3_backup
3fa00 5f 73 74 65 70 28 29 20 63 61 6e 20 62 65 20 72  _step() can be r
3fa10 65 74 72 69 65 64 20 6c 61 74 65 72 2e 20 49 66  etried later. If
3fa20 20 74 68 65 20 73 6f 75 72 63 65 0a 2a 2a 20 5b   the source.** [
3fa30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3fa40 69 6f 6e 5d 0a 2a 2a 20 69 73 20 62 65 69 6e 67  ion].** is being
3fa50 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 74   used to write t
3fa60 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74  o the source dat
3fa70 61 62 61 73 65 20 77 68 65 6e 20 73 71 6c 69 74  abase when sqlit
3fa80 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
3fa90 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  .** is called, t
3faa0 68 65 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b  hen [SQLITE_LOCK
3fab0 45 44 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  ED] is returned 
3fac0 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 41 67 61  immediately. Aga
3fad0 69 6e 2c 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63  in, in this.** c
3fae0 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  ase the call to 
3faf0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
3fb00 74 65 70 28 29 20 63 61 6e 20 62 65 20 72 65 74  tep() can be ret
3fb10 72 69 65 64 20 6c 61 74 65 72 20 6f 6e 2e 20 49  ried later on. I
3fb20 66 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45  f.** [SQLITE_IOE
3fb30 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49  RR_ACCESS | SQLI
3fb40 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 2c 20 5b  TE_IOERR_XXX], [
3fb50 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 6f  SQLITE_NOMEM], o
3fb60 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 52 45 41  r.** [SQLITE_REA
3fb70 44 4f 4e 4c 59 5d 20 69 73 20 72 65 74 75 72 6e  DONLY] is return
3fb80 65 64 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 68 65  ed, then .** the
3fb90 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69  re is no point i
3fba0 6e 20 72 65 74 72 79 69 6e 67 20 74 68 65 20 63  n retrying the c
3fbb0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  all to sqlite3_b
3fbc0 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68  ackup_step(). Th
3fbd0 65 73 65 20 0a 2a 2a 20 65 72 72 6f 72 73 20 61  ese .** errors a
3fbe0 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61  re considered fa
3fbf0 74 61 6c 2e 20 41 74 20 74 68 69 73 20 70 6f 69  tal. At this poi
3fc00 6e 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  nt the applicati
3fc10 6f 6e 20 6d 75 73 74 20 61 63 63 65 70 74 20 0a  on must accept .
3fc20 2a 2a 20 74 68 61 74 20 74 68 65 20 62 61 63 6b  ** that the back
3fc30 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 73  up operation has
3fc40 20 66 61 69 6c 65 64 20 61 6e 64 20 70 61 73 73   failed and pass
3fc50 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72   the backup oper
3fc60 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 0a 2a 2a  ation handle .**
3fc70 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f   to the sqlite3_
3fc80 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20  backup_finish() 
3fc90 74 6f 20 72 65 6c 65 61 73 65 20 61 73 73 6f 63  to release assoc
3fca0 69 61 74 65 64 20 72 65 73 6f 75 72 63 65 73 2e  iated resources.
3fcb0 0a 2a 2a 0a 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67  .**.** Following
3fcc0 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   the first call 
3fcd0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
3fce0 70 5f 73 74 65 70 28 29 2c 20 61 6e 20 65 78 63  p_step(), an exc
3fcf0 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 0a 2a  lusive lock is.*
3fd00 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68  * obtained on th
3fd10 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66 69  e destination fi
3fd20 6c 65 2e 20 49 74 20 69 73 20 6e 6f 74 20 72 65  le. It is not re
3fd30 6c 65 61 73 65 64 20 75 6e 74 69 6c 20 65 69 74  leased until eit
3fd40 68 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  her .** sqlite3_
3fd50 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20  backup_finish() 
3fd60 69 73 20 63 61 6c 6c 65 64 20 6f 72 20 74 68 65  is called or the
3fd70 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
3fd80 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 0a 2a  n is complete .*
3fd90 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61  * and sqlite3_ba
3fda0 63 6b 75 70 5f 73 74 65 70 28 29 20 72 65 74 75  ckup_step() retu
3fdb0 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45  rns [SQLITE_DONE
3fdc0 5d 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c  ]. Additionally,
3fdd0 20 65 61 63 68 20 74 69 6d 65 20 0a 2a 2a 20 61   each time .** a
3fde0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
3fdf0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69  _backup_step() i
3fe00 73 20 6d 61 64 65 20 61 20 5b 73 68 61 72 65 64  s made a [shared
3fe10 20 6c 6f 63 6b 5d 20 69 73 20 6f 62 74 61 69 6e   lock] is obtain
3fe20 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 73 6f 75  ed on.** the sou
3fe30 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  rce database fil
3fe40 65 2e 20 54 68 69 73 20 6c 6f 63 6b 20 69 73 20  e. This lock is 
3fe50 72 65 6c 65 61 73 65 64 20 62 65 66 6f 72 65 20  released before 
3fe60 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62  the.** sqlite3_b
3fe70 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6c  ackup_step() cal
3fe80 6c 20 72 65 74 75 72 6e 73 2e 20 42 65 63 61 75  l returns. Becau
3fe90 73 65 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  se the source da
3fea0 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a  tabase is not.**
3feb0 20 6c 6f 63 6b 65 64 20 62 65 74 77 65 65 6e 20   locked between 
3fec0 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
3fed0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20  _backup_step(), 
3fee0 69 74 20 6d 61 79 20 62 65 20 6d 6f 64 69 66 69  it may be modifi
3fef0 65 64 20 6d 69 64 2d 77 61 79 0a 2a 2a 20 74 68  ed mid-way.** th
3ff00 72 6f 75 67 68 20 74 68 65 20 62 61 63 6b 75 70  rough the backup
3ff10 20 70 72 6f 63 65 64 75 72 65 2e 20 49 66 20 74   procedure. If t
3ff20 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
3ff30 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62  se is modified b
3ff40 79 20 61 6e 0a 2a 2a 20 65 78 74 65 72 6e 61 6c  y an.** external
3ff50 20 70 72 6f 63 65 73 73 20 6f 72 20 76 69 61 20   process or via 
3ff60 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
3ff70 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e  ction other than
3ff80 20 74 68 65 20 6f 6e 65 20 62 65 69 6e 67 0a 2a   the one being.*
3ff90 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 62 61  * used by the ba
3ffa0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20  ckup operation, 
3ffb0 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75 70 20  then the backup 
3ffc0 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 70 61 72  will be transpar
3ffd0 65 6e 74 6c 79 0a 2a 2a 20 72 65 73 74 61 72 74  ently.** restart
3ffe0 65 64 20 62 79 20 74 68 65 20 6e 65 78 74 20 63  ed by the next c
3fff0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  all to sqlite3_b
40000 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 66  ackup_step(). If
40010 20 74 68 65 20 73 6f 75 72 63 65 20 0a 2a 2a 20   the source .** 
40020 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69  database is modi
40030 66 69 65 64 20 62 79 20 74 68 65 20 75 73 69 6e  fied by the usin
40040 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  g the same datab
40050 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61  ase connection a
40060 73 20 69 73 20 75 73 65 64 0a 2a 2a 20 62 79 20  s is used.** by 
40070 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
40080 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 62  tion, then the b
40090 61 63 6b 75 70 20 64 61 74 61 62 61 73 65 20 69  ackup database i
400a0 73 20 74 72 61 6e 73 70 61 72 65 6e 74 6c 79 20  s transparently 
400b0 0a 2a 2a 20 75 70 64 61 74 65 64 20 61 74 20 74  .** updated at t
400c0 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a  he same time..**
400d0 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62  .** <b>sqlite3_b
400e0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 3c 2f  ackup_finish()</
400f0 62 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 73 71  b>.**.** Once sq
40100 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
40110 70 28 29 20 68 61 73 20 72 65 74 75 72 6e 65 64  p() has returned
40120 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20   [SQLITE_DONE], 
40130 6f 72 20 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20  or when the .** 
40140 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 69 73 68  application wish
40150 65 73 20 74 6f 20 61 62 61 6e 64 6f 6e 20 74 68  es to abandon th
40160 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  e backup operati
40170 6f 6e 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33  on, the [sqlite3
40180 5f 62 61 63 6b 75 70 5d 0a 2a 2a 20 6f 62 6a 65  _backup].** obje
40190 63 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  ct should be pas
401a0 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  sed to sqlite3_b
401b0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20  ackup_finish(). 
401c0 54 68 69 73 20 72 65 6c 65 61 73 65 73 20 61 6c  This releases al
401d0 6c 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 61  l.** resources a
401e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
401f0 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  he backup operat
40200 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33 5f  ion. If sqlite3_
40210 62 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 2a 2a  backup_step().**
40220 20 68 61 73 20 6e 6f 74 20 79 65 74 20 72 65 74   has not yet ret
40230 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44 4f  urned [SQLITE_DO
40240 4e 45 5d 2c 20 74 68 65 6e 20 61 6e 79 20 61 63  NE], then any ac
40250 74 69 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73  tive write-trans
40260 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 0a 2a 2a  action on the.**
40270 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
40280 61 62 61 73 65 20 69 73 20 72 6f 6c 6c 65 64 20  abase is rolled 
40290 62 61 63 6b 2e 20 54 68 65 20 5b 73 71 6c 69 74  back. The [sqlit
402a0 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63  e3_backup] objec
402b0 74 20 69 73 20 69 6e 76 61 6c 69 64 0a 2a 2a 20  t is invalid.** 
402c0 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 75  and may not be u
402d0 73 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  sed following a 
402e0 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
402f0 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e  backup_finish().
40300 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .**.** The value
40310 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
40320 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
40330 73 68 20 69 73 20 5b 53 51 4c 49 54 45 5f 4f 4b  sh is [SQLITE_OK
40340 5d 20 69 66 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a  ] if no error.**
40350 20 6f 63 63 75 72 72 65 64 2c 20 72 65 67 61 72   occurred, regar
40360 64 6c 65 73 73 20 6f 72 20 77 68 65 74 68 65 72  dless or whether
40370 20 6f 72 20 6e 6f 74 20 73 71 6c 69 74 65 33 5f   or not sqlite3_
40380 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 77 61  backup_step() wa
40390 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 73 75  s called.** a su
403a0 66 66 69 63 69 65 6e 74 20 6e 75 6d 62 65 72 20  fficient number 
403b0 6f 66 20 74 69 6d 65 73 20 74 6f 20 63 6f 6d 70  of times to comp
403c0 6c 65 74 65 20 74 68 65 20 62 61 63 6b 75 70 20  lete the backup 
403d0 6f 70 65 72 61 74 69 6f 6e 2e 20 4f 72 2c 20 69  operation. Or, i
403e0 66 0a 2a 2a 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d  f.** an out-of-m
403f0 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 6f 6e 20  emory condition 
40400 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  or IO error occu
40410 72 65 64 20 64 75 72 69 6e 67 20 61 20 63 61 6c  red during a cal
40420 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  l to.** sqlite3_
40430 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 74 68  backup_step() th
40440 65 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  en [SQLITE_NOMEM
40450 5d 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49  ] or an.** [SQLI
40460 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20  TE_IOERR_ACCESS 
40470 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58  | SQLITE_IOERR_X
40480 58 58 5d 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a  XX] error code.*
40490 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  * is returned. I
404a0 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
404b0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 61  error code and a
404c0 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
404d0 61 72 65 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74  are.** written t
404e0 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  o the destinatio
404f0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  n [database conn
40500 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 41  ection]..**.** A
40510 20 72 65 74 75 72 6e 20 6f 66 20 5b 53 51 4c 49   return of [SQLI
40520 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c  TE_BUSY] or [SQL
40530 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 66 72 6f 6d  ITE_LOCKED] from
40540 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
40550 73 74 65 70 28 29 20 69 73 0a 2a 2a 20 6e 6f 74  step() is.** not
40560 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 65 72 72   a permanent err
40570 6f 72 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20  or and does not 
40580 61 66 66 65 63 74 20 74 68 65 20 72 65 74 75 72  affect the retur
40590 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 73 71  n value of.** sq
405a0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
405b0 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e  ish()..**.** <b>
405c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72  sqlite3_backup_r
405d0 65 6d 61 69 6e 69 6e 67 28 29 2c 20 73 71 6c 69  emaining(), sqli
405e0 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63  te3_backup_pagec
405f0 6f 75 6e 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a  ount()</b>.**.**
40600 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 73 71   Each call to sq
40610 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
40620 70 28 29 20 73 65 74 73 20 74 77 6f 20 76 61 6c  p() sets two val
40630 75 65 73 20 73 74 6f 72 65 64 20 69 6e 74 65 72  ues stored inter
40640 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 61 6e 20 5b  nally.** by an [
40650 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20  sqlite3_backup] 
40660 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 75 6d 62  object. The numb
40670 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c  er of pages stil
40680 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64 0a 2a  l to be backed.*
40690 2a 20 75 70 2c 20 77 68 69 63 68 20 6d 61 79 20  * up, which may 
406a0 62 65 20 71 75 65 72 69 65 64 20 62 79 20 73 71  be queried by sq
406b0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d  lite3_backup_rem
406c0 61 69 6e 69 6e 67 28 29 2c 20 61 6e 64 20 74 68  aining(), and th
406d0 65 20 74 6f 74 61 6c 0a 2a 2a 20 6e 75 6d 62 65  e total.** numbe
406e0 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
406f0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
40700 65 20 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61  e file, which ma
40710 79 20 62 65 20 71 75 65 72 69 65 64 20 62 79 0a  y be queried by.
40720 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
40730 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a 2a  p_pagecount()..*
40740 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 73 20  *.** The values 
40750 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73  returned by thes
40760 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
40770 6f 6e 6c 79 20 75 70 64 61 74 65 64 20 62 79 0a  only updated by.
40780 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
40790 70 5f 73 74 65 70 28 29 2e 20 49 66 20 74 68 65  p_step(). If the
407a0 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
407b0 20 69 73 20 6d 6f 64 69 66 69 65 64 20 64 75 72   is modified dur
407c0 69 6e 67 20 61 20 62 61 63 6b 75 70 0a 2a 2a 20  ing a backup.** 
407d0 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20  operation, then 
407e0 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e  the values are n
407f0 6f 74 20 75 70 64 61 74 65 64 20 74 6f 20 61 63  ot updated to ac
40800 63 6f 75 6e 74 20 66 6f 72 20 61 6e 79 20 65 78  count for any ex
40810 74 72 61 0a 2a 2a 20 70 61 67 65 73 20 74 68 61  tra.** pages tha
40820 74 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64  t need to be upd
40830 61 74 65 64 20 6f 72 20 74 68 65 20 73 69 7a 65  ated or the size
40840 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 64   of the source d
40850 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20  atabase file.** 
40860 63 68 61 6e 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  changing..**.** 
40870 3c 62 3e 43 6f 6e 63 75 72 72 65 6e 74 20 55 73  <b>Concurrent Us
40880 61 67 65 20 6f 66 20 44 61 74 61 62 61 73 65 20  age of Database 
40890 48 61 6e 64 6c 65 73 3c 2f 62 3e 0a 2a 2a 0a 2a  Handles</b>.**.*
408a0 2a 20 54 68 65 20 73 6f 75 72 63 65 20 5b 64 61  * The source [da
408b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
408c0 6e 5d 20 6d 61 79 20 62 65 20 75 73 65 64 20 62  n] may be used b
408d0 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  y the applicatio
408e0 6e 20 66 6f 72 20 6f 74 68 65 72 0a 2a 2a 20 70  n for other.** p
408f0 75 72 70 6f 73 65 73 20 77 68 69 6c 65 20 61 20  urposes while a 
40900 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
40910 20 69 73 20 75 6e 64 65 72 77 61 79 20 6f 72 20   is underway or 
40920 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65  being initialize
40930 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20  d..** If SQLite 
40940 69 73 20 63 6f 6d 70 69 6c 65 64 20 61 6e 64 20  is compiled and 
40950 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 73 75  configured to su
40960 70 70 6f 72 74 20 74 68 72 65 61 64 73 61 66 65  pport threadsafe
40970 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e   database.** con
40980 6e 65 63 74 69 6f 6e 73 2c 20 74 68 65 6e 20 74  nections, then t
40990 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
409a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61  se connection ma
409b0 79 20 62 65 20 75 73 65 64 20 63 6f 6e 63 75 72  y be used concur
409c0 72 65 6e 74 6c 79 0a 2a 2a 20 66 72 6f 6d 20 77  rently.** from w
409d0 69 74 68 69 6e 20 6f 74 68 65 72 20 74 68 72 65  ithin other thre
409e0 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76  ads..**.** Howev
409f0 65 72 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74  er, the applicat
40a00 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 6e 74  ion must guarant
40a10 65 65 20 74 68 61 74 20 74 68 65 20 64 65 73 74  ee that the dest
40a20 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
40a30 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  .** connection h
40a40 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 70 61 73  andle is not pas
40a50 73 65 64 20 74 6f 20 61 6e 79 20 6f 74 68 65 72  sed to any other
40a60 20 41 50 49 20 28 62 79 20 61 6e 79 20 74 68 72   API (by any thr
40a70 65 61 64 29 20 61 66 74 65 72 20 0a 2a 2a 20 73  ead) after .** s
40a80 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e  qlite3_backup_in
40a90 69 74 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61  it() is called a
40aa0 6e 64 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f  nd before the co
40ab0 72 72 65 73 70 6f 6e 64 69 6e 67 20 63 61 6c 6c  rresponding call
40ac0 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62   to.** sqlite3_b
40ad0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20  ackup_finish(). 
40ae0 55 6e 66 6f 72 74 75 6e 61 74 65 6c 79 20 53 51  Unfortunately SQ
40af0 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 63 75  Lite does not cu
40b00 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 0a 2a 2a  rrently check.**
40b10 20 66 6f 72 20 74 68 69 73 2c 20 69 66 20 74 68   for this, if th
40b20 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 6f  e application do
40b30 65 73 20 75 73 65 20 74 68 65 20 64 65 73 74 69  es use the desti
40b40 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65  nation [database
40b50 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20   connection].** 
40b60 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 70  for some other p
40b70 75 72 70 6f 73 65 20 64 75 72 69 6e 67 20 61 20  urpose during a 
40b80 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
40b90 2c 20 74 68 69 6e 67 73 20 6d 61 79 20 61 70 70  , things may app
40ba0 65 61 72 20 74 6f 0a 2a 2a 20 77 6f 72 6b 20 63  ear to.** work c
40bb0 6f 72 72 65 63 74 6c 79 20 62 75 74 20 69 6e 20  orrectly but in 
40bc0 66 61 63 74 20 62 65 20 73 75 62 74 6c 79 20 6d  fact be subtly m
40bd0 61 6c 66 75 6e 63 74 69 6f 6e 69 6e 67 2e 20 20  alfunctioning.  
40be0 55 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65  Use of the.** de
40bf0 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
40c00 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68  se connection wh
40c10 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 73 20  ile a backup is 
40c20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 69 67 68  in progress migh
40c30 74 0a 2a 2a 20 61 6c 73 6f 20 63 61 75 73 65 20  t.** also cause 
40c40 61 20 6d 75 74 65 78 20 64 65 61 64 6c 6f 63 6b  a mutex deadlock
40c50 2e 0a 2a 2a 0a 2a 2a 20 46 75 72 74 68 65 72 6d  ..**.** Furtherm
40c60 6f 72 65 2c 20 69 66 20 72 75 6e 6e 69 6e 67 20  ore, if running 
40c70 69 6e 20 5b 73 68 61 72 65 64 20 63 61 63 68 65  in [shared cache
40c80 20 6d 6f 64 65 5d 2c 20 74 68 65 20 61 70 70 6c   mode], the appl
40c90 69 63 61 74 69 6f 6e 20 6d 75 73 74 0a 2a 2a 20  ication must.** 
40ca0 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74  guarantee that t
40cb0 68 65 20 73 68 61 72 65 64 20 63 61 63 68 65 20  he shared cache 
40cc0 75 73 65 64 20 62 79 20 74 68 65 20 64 65 73 74  used by the dest
40cd0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
40ce0 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 63 63 65 73  .** is not acces
40cf0 73 65 64 20 77 68 69 6c 65 20 74 68 65 20 62 61  sed while the ba
40d00 63 6b 75 70 20 69 73 20 72 75 6e 6e 69 6e 67 2e  ckup is running.
40d10 20 49 6e 20 70 72 61 63 74 69 63 65 20 74 68 69   In practice thi
40d20 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20  s means.** that 
40d30 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
40d40 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74  must guarantee t
40d50 68 61 74 20 74 68 65 20 66 69 6c 65 2d 73 79 73  hat the file-sys
40d60 74 65 6d 20 66 69 6c 65 20 62 65 69 6e 67 20 0a  tem file being .
40d70 2a 2a 20 62 61 63 6b 65 64 20 75 70 20 74 6f 20  ** backed up to 
40d80 69 73 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20  is not accessed 
40d90 62 79 20 61 6e 79 20 63 6f 6e 6e 65 63 74 69 6f  by any connectio
40da0 6e 20 77 69 74 68 69 6e 20 74 68 65 20 70 72 6f  n within the pro
40db0 63 65 73 73 2c 0a 2a 2a 20 6e 6f 74 20 6a 75 73  cess,.** not jus
40dc0 74 20 74 68 65 20 73 70 65 63 69 66 69 63 20 63  t the specific c
40dd0 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 77  onnection that w
40de0 61 73 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  as passed to sql
40df0 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
40e00 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73  ()..**.** The [s
40e10 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f  qlite3_backup] o
40e20 62 6a 65 63 74 20 69 74 73 65 6c 66 20 69 73 20  bject itself is 
40e30 70 61 72 74 69 61 6c 6c 79 20 74 68 72 65 61 64  partially thread
40e40 73 61 66 65 2e 20 4d 75 6c 74 69 70 6c 65 20 0a  safe. Multiple .
40e50 2a 2a 20 74 68 72 65 61 64 73 20 6d 61 79 20 73  ** threads may s
40e60 61 66 65 6c 79 20 6d 61 6b 65 20 6d 75 6c 74 69  afely make multi
40e70 70 6c 65 20 63 6f 6e 63 75 72 72 65 6e 74 20 63  ple concurrent c
40e80 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  alls to sqlite3_
40e90 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a  backup_step()..*
40ea0 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 73  * However, the s
40eb0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65  qlite3_backup_re
40ec0 6d 61 69 6e 69 6e 67 28 29 20 61 6e 64 20 73 71  maining() and sq
40ed0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67  lite3_backup_pag
40ee0 65 63 6f 75 6e 74 28 29 0a 2a 2a 20 41 50 49 73  ecount().** APIs
40ef0 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c   are not strictl
40f00 79 20 73 70 65 61 6b 69 6e 67 20 74 68 72 65 61  y speaking threa
40f10 64 73 61 66 65 2e 20 49 66 20 74 68 65 79 20 61  dsafe. If they a
40f20 72 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68  re invoked at th
40f30 65 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65 20 61  e.** same time a
40f40 73 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  s another thread
40f50 20 69 73 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c   is invoking sql
40f60 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
40f70 28 29 20 69 74 20 69 73 0a 2a 2a 20 70 6f 73 73  () it is.** poss
40f80 69 62 6c 65 20 74 68 61 74 20 74 68 65 79 20 72  ible that they r
40f90 65 74 75 72 6e 20 69 6e 76 61 6c 69 64 20 76 61  eturn invalid va
40fa0 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lues..*/.SQLITE_
40fb0 41 50 49 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  API sqlite3_back
40fc0 75 70 20 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b  up *sqlite3_back
40fd0 75 70 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74  up_init(.  sqlit
40fe0 65 33 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20  e3 *pDest,      
40ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41000 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e    /* Destination
41010 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
41020 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
41030 20 2a 7a 44 65 73 74 4e 61 6d 65 2c 20 20 20 20   *zDestName,    
41040 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
41050 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  Destination data
41060 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 73  base name */.  s
41070 71 6c 69 74 65 33 20 2a 70 53 6f 75 72 63 65 2c  qlite3 *pSource,
41080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41090 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20        /* Source 
410a0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
410b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
410c0 2a 7a 53 6f 75 72 63 65 4e 61 6d 65 20 20 20 20  *zSourceName    
410d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
410e0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 6e  ource database n
410f0 61 6d 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45  ame */.);.SQLITE
41100 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
41110 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c  _backup_step(sql
41120 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20  ite3_backup *p, 
41130 69 6e 74 20 6e 50 61 67 65 29 3b 0a 53 51 4c 49  int nPage);.SQLI
41140 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
41150 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
41160 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
41170 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *p);.SQLITE_API 
41180 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  int sqlite3_back
41190 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71 6c  up_remaining(sql
411a0 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b  ite3_backup *p);
411b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
411c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70  sqlite3_backup_p
411d0 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  agecount(sqlite3
411e0 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 0a 2f 2a  _backup *p);../*
411f0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 6e  .** CAPI3REF: Un
41200 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f  lock Notificatio
41210 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  n.** EXPERIMENTA
41220 4c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 72 75 6e  L.**.** When run
41230 6e 69 6e 67 20 69 6e 20 73 68 61 72 65 64 2d 63  ning in shared-c
41240 61 63 68 65 20 6d 6f 64 65 2c 20 61 20 64 61 74  ache mode, a dat
41250 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 20  abase operation 
41260 6d 61 79 20 66 61 69 6c 20 77 69 74 68 0a 2a 2a  may fail with.**
41270 20 61 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b   an [SQLITE_LOCK
41280 45 44 5d 20 65 72 72 6f 72 20 69 66 20 74 68 65  ED] error if the
41290 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 20   required locks 
412a0 6f 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61  on the shared-ca
412b0 63 68 65 20 6f 72 0a 2a 2a 20 69 6e 64 69 76 69  che or.** indivi
412c0 64 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68  dual tables with
412d0 69 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61  in the shared-ca
412e0 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62  che cannot be ob
412f0 74 61 69 6e 65 64 2e 20 53 65 65 0a 2a 2a 20 5b  tained. See.** [
41300 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d 43 61  SQLite Shared-Ca
41310 63 68 65 20 4d 6f 64 65 5d 20 66 6f 72 20 61 20  che Mode] for a 
41320 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 73  description of s
41330 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b  hared-cache lock
41340 69 6e 67 2e 20 0a 2a 2a 20 54 68 69 73 20 41 50  ing. .** This AP
41350 49 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f  I may be used to
41360 20 72 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c   register a call
41370 62 61 63 6b 20 74 68 61 74 20 53 51 4c 69 74 65  back that SQLite
41380 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 0a 2a 2a   will invoke .**
41390 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63   when the connec
413a0 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 68  tion currently h
413b0 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71 75 69  olding the requi
413c0 72 65 64 20 6c 6f 63 6b 20 72 65 6c 69 6e 71 75  red lock relinqu
413d0 69 73 68 65 73 20 69 74 2e 0a 2a 2a 20 54 68 69  ishes it..** Thi
413e0 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 61 76  s API is only av
413f0 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c  ailable if the l
41400 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69  ibrary was compi
41410 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20  led with the.** 
41420 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55  [SQLITE_ENABLE_U
41430 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 5d 20 43 2d  NLOCK_NOTIFY] C-
41440 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d  preprocessor sym
41450 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  bol defined..**.
41460 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73  ** See Also: [Us
41470 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 55  ing the SQLite U
41480 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 74 69  nlock Notificati
41490 6f 6e 20 46 65 61 74 75 72 65 5d 2e 0a 2a 2a 0a  on Feature]..**.
414a0 2a 2a 20 53 68 61 72 65 64 2d 63 61 63 68 65 20  ** Shared-cache 
414b0 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 73  locks are releas
414c0 65 64 20 77 68 65 6e 20 61 20 64 61 74 61 62 61  ed when a databa
414d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f  se connection co
414e0 6e 63 6c 75 64 65 73 0a 2a 2a 20 69 74 73 20 63  ncludes.** its c
414f0 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
41500 6f 6e 2c 20 65 69 74 68 65 72 20 62 79 20 63 6f  on, either by co
41510 6d 6d 69 74 74 69 6e 67 20 69 74 20 6f 72 20 72  mmitting it or r
41520 6f 6c 6c 69 6e 67 20 69 74 20 62 61 63 6b 2e 20  olling it back. 
41530 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 6f  .**.** When a co
41540 6e 6e 65 63 74 69 6f 6e 20 28 6b 6e 6f 77 6e 20  nnection (known 
41550 61 73 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63  as the blocked c
41560 6f 6e 6e 65 63 74 69 6f 6e 29 20 66 61 69 6c 73  onnection) fails
41570 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a 2a 20   to obtain a.** 
41580 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63  shared-cache loc
41590 6b 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43  k and SQLITE_LOC
415a0 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 64 20  KED is returned 
415b0 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2c 20 74  to the caller, t
415c0 68 65 0a 2a 2a 20 69 64 65 6e 74 69 74 79 20 6f  he.** identity o
415d0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
415e0 6f 6e 6e 65 63 74 69 6f 6e 20 28 74 68 65 20 62  onnection (the b
415f0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69  locking connecti
41600 6f 6e 29 20 74 68 61 74 0a 2a 2a 20 68 61 73 20  on) that.** has 
41610 6c 6f 63 6b 65 64 20 74 68 65 20 72 65 71 75 69  locked the requi
41620 72 65 64 20 72 65 73 6f 75 72 63 65 20 69 73 20  red resource is 
41630 73 74 6f 72 65 64 20 69 6e 74 65 72 6e 61 6c 6c  stored internall
41640 79 2e 20 41 66 74 65 72 20 61 6e 20 0a 2a 2a 20  y. After an .** 
41650 61 70 70 6c 69 63 61 74 69 6f 6e 20 72 65 63 65  application rece
41660 69 76 65 73 20 61 6e 20 53 51 4c 49 54 45 5f 4c  ives an SQLITE_L
41670 4f 43 4b 45 44 20 65 72 72 6f 72 2c 20 69 74 20  OCKED error, it 
41680 6d 61 79 20 63 61 6c 6c 20 74 68 65 0a 2a 2a 20  may call the.** 
41690 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
416a0 6f 74 69 66 79 28 29 20 6d 65 74 68 6f 64 20 77  otify() method w
416b0 69 74 68 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  ith the blocked 
416c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
416d0 65 20 61 73 20 0a 2a 2a 20 74 68 65 20 66 69 72  e as .** the fir
416e0 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72  st argument to r
416f0 65 67 69 73 74 65 72 20 66 6f 72 20 61 20 63 61  egister for a ca
41700 6c 6c 62 61 63 6b 20 74 68 61 74 20 77 69 6c 6c  llback that will
41710 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77   be invoked.** w
41720 68 65 6e 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67  hen the blocking
41730 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72   connections cur
41740 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
41750 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20 54   is concluded. T
41760 68 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69  he.** callback i
41770 73 20 69 6e 76 6f 6b 65 64 20 66 72 6f 6d 20 77  s invoked from w
41780 69 74 68 69 6e 20 74 68 65 20 5b 73 71 6c 69 74  ithin the [sqlit
41790 65 33 5f 73 74 65 70 5d 20 6f 72 20 5b 73 71 6c  e3_step] or [sql
417a0 69 74 65 33 5f 63 6c 6f 73 65 5d 0a 2a 2a 20 63  ite3_close].** c
417b0 61 6c 6c 20 74 68 61 74 20 63 6f 6e 63 6c 75 64  all that conclud
417c0 65 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20  es the blocking 
417d0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e  connections tran
417e0 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  saction..**.** I
417f0 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  f sqlite3_unlock
41800 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 61 6c  _notify() is cal
41810 6c 65 64 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74  led in a multi-t
41820 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74  hreaded applicat
41830 69 6f 6e 2c 0a 2a 2a 20 74 68 65 72 65 20 69 73  ion,.** there is
41840 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 74   a chance that t
41850 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e  he blocking conn
41860 65 63 74 69 6f 6e 20 77 69 6c 6c 20 68 61 76 65  ection will have
41870 20 61 6c 72 65 61 64 79 0a 2a 2a 20 63 6f 6e 63   already.** conc
41880 6c 75 64 65 64 20 69 74 73 20 74 72 61 6e 73 61  luded its transa
41890 63 74 69 6f 6e 20 62 79 20 74 68 65 20 74 69 6d  ction by the tim
418a0 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  e sqlite3_unlock
418b0 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 69 6e 76  _notify() is inv
418c0 6f 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 69 73  oked..** If this
418d0 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20 74   happens, then t
418e0 68 65 20 73 70 65 63 69 66 69 65 64 20 63 61 6c  he specified cal
418f0 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
41900 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0a 2a 2a   immediately,.**
41910 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
41920 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
41930 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
41940 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62  ..**.** If the b
41950 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
41960 6e 20 69 73 20 61 74 74 65 6d 70 74 69 6e 67 20  n is attempting 
41970 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 72 69 74  to obtain a writ
41980 65 2d 6c 6f 63 6b 20 6f 6e 20 61 0a 2a 2a 20 73  e-lock on a.** s
41990 68 61 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c  hared-cache tabl
419a0 65 2c 20 61 6e 64 20 6d 6f 72 65 20 74 68 61 6e  e, and more than
419b0 20 6f 6e 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65   one other conne
419c0 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20  ction currently 
419d0 68 6f 6c 64 73 0a 2a 2a 20 61 20 72 65 61 64 2d  holds.** a read-
419e0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 61 6d 65  lock on the same
419f0 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 53 51 4c   table, then SQL
41a00 69 74 65 20 61 72 62 69 74 72 61 72 69 6c 79 20  ite arbitrarily 
41a10 73 65 6c 65 63 74 73 20 6f 6e 65 20 6f 66 20 0a  selects one of .
41a20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 63 6f 6e  ** the other con
41a30 6e 65 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20  nections to use 
41a40 61 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20  as the blocking 
41a50 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  connection..**.*
41a60 2a 20 54 68 65 72 65 20 6d 61 79 20 62 65 20 61  * There may be a
41a70 74 20 6d 6f 73 74 20 6f 6e 65 20 75 6e 6c 6f 63  t most one unloc
41a80 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
41a90 6b 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20  k registered by 
41aa0 61 20 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 6f  a .** blocked co
41ab0 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 73 71 6c  nnection. If sql
41ac0 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69  ite3_unlock_noti
41ad0 66 79 28 29 20 69 73 20 63 61 6c 6c 65 64 20 77  fy() is called w
41ae0 68 65 6e 20 74 68 65 0a 2a 2a 20 62 6c 6f 63 6b  hen the.** block
41af0 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c  ed connection al
41b00 72 65 61 64 79 20 68 61 73 20 61 20 72 65 67 69  ready has a regi
41b10 73 74 65 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f  stered unlock-no
41b20 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2c 0a 2a  tify callback,.*
41b30 2a 20 74 68 65 6e 20 74 68 65 20 6e 65 77 20 63  * then the new c
41b40 61 6c 6c 62 61 63 6b 20 72 65 70 6c 61 63 65 73  allback replaces
41b50 20 74 68 65 20 6f 6c 64 2e 20 49 66 20 73 71 6c   the old. If sql
41b60 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69  ite3_unlock_noti
41b70 66 79 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c 65  fy() is.** calle
41b80 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f  d with a NULL po
41b90 69 6e 74 65 72 20 61 73 20 69 74 73 20 73 65 63  inter as its sec
41ba0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74 68  ond argument, th
41bb0 65 6e 20 61 6e 79 20 65 78 69 73 74 69 6e 67 0a  en any existing.
41bc0 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  ** unlock-notify
41bd0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e   callback is can
41be0 63 65 6c 6c 65 64 2e 20 54 68 65 20 62 6c 6f 63  celled. The bloc
41bf0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ked connections 
41c00 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  .** unlock-notif
41c10 79 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61  y callback may a
41c20 6c 73 6f 20 62 65 20 63 61 6e 63 65 6c 65 64 20  lso be canceled 
41c30 62 79 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 62  by closing the b
41c40 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 6e 65 63  locked.** connec
41c50 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69  tion using [sqli
41c60 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a  te3_close()]..**
41c70 0a 2a 2a 20 54 68 65 20 75 6e 6c 6f 63 6b 2d 6e  .** The unlock-n
41c80 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69  otify callback i
41c90 73 20 6e 6f 74 20 72 65 65 6e 74 72 61 6e 74 2e  s not reentrant.
41ca0 20 49 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69   If an applicati
41cb0 6f 6e 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 61 6e  on invokes.** an
41cc0 79 20 73 71 6c 69 74 65 33 5f 78 78 78 20 41 50  y sqlite3_xxx AP
41cd0 49 20 66 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d  I functions from
41ce0 20 77 69 74 68 69 6e 20 61 6e 20 75 6e 6c 6f 63   within an unloc
41cf0 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
41d00 6b 2c 20 61 0a 2a 2a 20 63 72 61 73 68 20 6f 72  k, a.** crash or
41d10 20 64 65 61 64 6c 6f 63 6b 20 6d 61 79 20 62 65   deadlock may be
41d20 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a   the result..**.
41d30 2a 2a 20 55 6e 6c 65 73 73 20 64 65 61 64 6c 6f  ** Unless deadlo
41d40 63 6b 20 69 73 20 64 65 74 65 63 74 65 64 20 28  ck is detected (
41d50 73 65 65 20 62 65 6c 6f 77 29 2c 20 73 71 6c 69  see below), sqli
41d60 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
41d70 79 28 29 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65  y() always.** re
41d80 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e  turns SQLITE_OK.
41d90 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 61 6c 6c 62 61  .**.** <b>Callba
41da0 63 6b 20 49 6e 76 6f 63 61 74 69 6f 6e 20 44 65  ck Invocation De
41db0 74 61 69 6c 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20  tails</b>.**.** 
41dc0 57 68 65 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e  When an unlock-n
41dd0 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69  otify callback i
41de0 73 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68  s registered, th
41df0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 70 72  e application pr
41e00 6f 76 69 64 65 73 20 61 20 0a 2a 2a 20 73 69 6e  ovides a .** sin
41e10 67 6c 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65  gle void* pointe
41e20 72 20 74 68 61 74 20 69 73 20 70 61 73 73 65 64  r that is passed
41e30 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
41e40 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e 76 6f   when it is invo
41e50 6b 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  ked..** However,
41e60 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 20 6f   the signature o
41e70 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  f the callback f
41e80 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 20 53  unction allows S
41e90 51 4c 69 74 65 20 74 6f 20 70 61 73 73 0a 2a 2a  QLite to pass.**
41ea0 20 69 74 20 61 6e 20 61 72 72 61 79 20 6f 66 20   it an array of 
41eb0 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70 6f  void* context po
41ec0 69 6e 74 65 72 73 2e 20 54 68 65 20 66 69 72 73  inters. The firs
41ed0 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65  t argument passe
41ee0 64 20 74 6f 0a 2a 2a 20 61 6e 20 75 6e 6c 6f 63  d to.** an unloc
41ef0 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
41f00 6b 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  k is a pointer t
41f10 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 76 6f  o an array of vo
41f20 69 64 2a 20 70 6f 69 6e 74 65 72 73 2c 0a 2a 2a  id* pointers,.**
41f30 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
41f40 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
41f50 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
41f60 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  array..**.** Whe
41f70 6e 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e  n a blocking con
41f80 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63  nections transac
41f90 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65  tion is conclude
41fa0 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65 0a  d, there may be.
41fb0 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  ** more than one
41fc0 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
41fd0 69 6f 6e 20 74 68 61 74 20 68 61 73 20 72 65 67  ion that has reg
41fe0 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75  istered for an u
41ff0 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20  nlock-notify.** 
42000 63 61 6c 6c 62 61 63 6b 2e 20 49 66 20 74 77 6f  callback. If two
42010 20 6f 72 20 6d 6f 72 65 20 73 75 63 68 20 62 6c   or more such bl
42020 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
42030 73 20 68 61 76 65 20 73 70 65 63 69 66 69 65 64  s have specified
42040 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 61 6c   the.** same cal
42050 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2c 20  lback function, 
42060 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f 66 20  then instead of 
42070 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 61 6c  invoking the cal
42080 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a  lback function.*
42090 2a 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  * multiple times
420a0 2c 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 20  , it is invoked 
420b0 6f 6e 63 65 20 77 69 74 68 20 74 68 65 20 73 65  once with the se
420c0 74 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74 65  t of void* conte
420d0 78 74 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 73  xt pointers.** s
420e0 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20  pecified by the 
420f0 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
42100 6f 6e 73 20 62 75 6e 64 6c 65 64 20 74 6f 67 65  ons bundled toge
42110 74 68 65 72 20 69 6e 74 6f 20 61 6e 20 61 72 72  ther into an arr
42120 61 79 2e 0a 2a 2a 20 54 68 69 73 20 67 69 76 65  ay..** This give
42130 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  s the applicatio
42140 6e 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79  n an opportunity
42150 20 74 6f 20 70 72 69 6f 72 69 74 69 7a 65 20 61   to prioritize a
42160 6e 79 20 61 63 74 69 6f 6e 73 20 0a 2a 2a 20 72  ny actions .** r
42170 65 6c 61 74 65 64 20 74 6f 20 74 68 65 20 73 65  elated to the se
42180 74 20 6f 66 20 75 6e 62 6c 6f 63 6b 65 64 20 64  t of unblocked d
42190 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
421a0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 44 65  ons..**.** <b>De
421b0 61 64 6c 6f 63 6b 20 44 65 74 65 63 74 69 6f 6e  adlock Detection
421c0 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 41 73 73 75 6d  </b>.**.** Assum
421d0 69 6e 67 20 74 68 61 74 20 61 66 74 65 72 20 72  ing that after r
421e0 65 67 69 73 74 65 72 69 6e 67 20 66 6f 72 20 61  egistering for a
421f0 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  n unlock-notify 
42200 63 61 6c 6c 62 61 63 6b 20 61 20 0a 2a 2a 20 64  callback a .** d
42210 61 74 61 62 61 73 65 20 77 61 69 74 73 20 66 6f  atabase waits fo
42220 72 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 74  r the callback t
42230 6f 20 62 65 20 69 73 73 75 65 64 20 62 65 66 6f  o be issued befo
42240 72 65 20 74 61 6b 69 6e 67 20 61 6e 79 20 66 75  re taking any fu
42250 72 74 68 65 72 0a 2a 2a 20 61 63 74 69 6f 6e 20  rther.** action 
42260 28 61 20 72 65 61 73 6f 6e 61 62 6c 65 20 61 73  (a reasonable as
42270 73 75 6d 70 74 69 6f 6e 29 2c 20 74 68 65 6e 20  sumption), then 
42280 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 20 6d  using this API m
42290 61 79 20 63 61 75 73 65 20 74 68 65 0a 2a 2a 20  ay cause the.** 
422a0 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 64  application to d
422b0 65 61 64 6c 6f 63 6b 2e 20 46 6f 72 20 65 78 61  eadlock. For exa
422c0 6d 70 6c 65 2c 20 69 66 20 63 6f 6e 6e 65 63 74  mple, if connect
422d0 69 6f 6e 20 58 20 69 73 20 77 61 69 74 69 6e 67  ion X is waiting
422e0 20 66 6f 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69   for.** connecti
422f0 6f 6e 20 59 27 73 20 74 72 61 6e 73 61 63 74 69  on Y's transacti
42300 6f 6e 20 74 6f 20 62 65 20 63 6f 6e 63 6c 75 64  on to be conclud
42310 65 64 2c 20 61 6e 64 20 73 69 6d 69 6c 61 72 6c  ed, and similarl
42320 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  y connection.** 
42330 59 20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e 20  Y is waiting on 
42340 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 27 73 20 74  connection X's t
42350 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e  ransaction, then
42360 20 6e 65 69 74 68 65 72 20 63 6f 6e 6e 65 63 74   neither connect
42370 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 70 72 6f 63  ion.** will proc
42380 65 65 64 20 61 6e 64 20 74 68 65 20 73 79 73 74  eed and the syst
42390 65 6d 20 6d 61 79 20 72 65 6d 61 69 6e 20 64 65  em may remain de
423a0 61 64 6c 6f 63 6b 65 64 20 69 6e 64 65 66 69 6e  adlocked indefin
423b0 69 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  itely..**.** To 
423c0 61 76 6f 69 64 20 74 68 69 73 20 73 63 65 6e 61  avoid this scena
423d0 72 69 6f 2c 20 74 68 65 20 73 71 6c 69 74 65 33  rio, the sqlite3
423e0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
423f0 20 70 65 72 66 6f 72 6d 73 20 64 65 61 64 6c 6f   performs deadlo
42400 63 6b 0a 2a 2a 20 64 65 74 65 63 74 69 6f 6e 2e  ck.** detection.
42410 20 49 66 20 61 20 67 69 76 65 6e 20 63 61 6c 6c   If a given call
42420 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f   to sqlite3_unlo
42430 63 6b 5f 6e 6f 74 69 66 79 28 29 20 77 6f 75 6c  ck_notify() woul
42440 64 20 70 75 74 20 74 68 65 0a 2a 2a 20 73 79 73  d put the.** sys
42450 74 65 6d 20 69 6e 20 61 20 64 65 61 64 6c 6f 63  tem in a deadloc
42460 6b 65 64 20 73 74 61 74 65 2c 20 74 68 65 6e 20  ked state, then 
42470 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73  SQLITE_LOCKED is
42480 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f   returned and no
42490 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  .** unlock-notif
424a0 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 65  y callback is re
424b0 67 69 73 74 65 72 65 64 2e 20 54 68 65 20 73 79  gistered. The sy
424c0 73 74 65 6d 20 69 73 20 73 61 69 64 20 74 6f 20  stem is said to 
424d0 62 65 20 69 6e 0a 2a 2a 20 61 20 64 65 61 64 6c  be in.** a deadl
424e0 6f 63 6b 65 64 20 73 74 61 74 65 20 69 66 20 63  ocked state if c
424f0 6f 6e 6e 65 63 74 69 6f 6e 20 41 20 68 61 73 20  onnection A has 
42500 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 61  registered for a
42510 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a  n unlock-notify.
42520 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 74  ** callback on t
42530 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66  he conclusion of
42540 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 27 73 20   connection B's 
42550 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64  transaction, and
42560 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 42   connection.** B
42570 20 68 61 73 20 69 74 73 65 6c 66 20 72 65 67 69   has itself regi
42580 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e  stered for an un
42590 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
425a0 62 61 63 6b 20 77 68 65 6e 20 63 6f 6e 6e 65 63  back when connec
425b0 74 69 6f 6e 0a 2a 2a 20 41 27 73 20 74 72 61 6e  tion.** A's tran
425c0 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c  saction is concl
425d0 75 64 65 64 2e 20 49 6e 64 69 72 65 63 74 20 64  uded. Indirect d
425e0 65 61 64 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20  eadlock is also 
425f0 64 65 74 65 63 74 65 64 2c 20 73 6f 0a 2a 2a 20  detected, so.** 
42600 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61 6c  the system is al
42610 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  so considered to
42620 20 62 65 20 64 65 61 64 6c 6f 63 6b 65 64 20 69   be deadlocked i
42630 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 20 68  f connection B h
42640 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64  as.** registered
42650 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e   for an unlock-n
42660 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 6f  otify callback o
42670 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e  n the conclusion
42680 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a   of connection.*
42690 2a 20 43 27 73 20 74 72 61 6e 73 61 63 74 69 6f  * C's transactio
426a0 6e 2c 20 77 68 65 72 65 20 63 6f 6e 6e 65 63 74  n, where connect
426b0 69 6f 6e 20 43 20 69 73 20 77 61 69 74 69 6e 67  ion C is waiting
426c0 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 41   on connection A
426d0 2e 20 41 6e 79 0a 2a 2a 20 6e 75 6d 62 65 72 20  . Any.** number 
426e0 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 69 6e 64  of levels of ind
426f0 69 72 65 63 74 69 6f 6e 20 61 72 65 20 61 6c 6c  irection are all
42700 6f 77 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 54  owed..**.** <b>T
42710 68 65 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20  he "DROP TABLE" 
42720 45 78 63 65 70 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a  Exception</b>.**
42730 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 6c 6c 20  .** When a call 
42740 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  to [sqlite3_step
42750 28 29 5d 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ()] returns SQLI
42760 54 45 5f 4c 4f 43 4b 45 44 2c 20 69 74 20 69 73  TE_LOCKED, it is
42770 20 61 6c 6d 6f 73 74 20 0a 2a 2a 20 61 6c 77 61   almost .** alwa
42780 79 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 74  ys appropriate t
42790 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 75  o call sqlite3_u
427a0 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 2e 20  nlock_notify(). 
427b0 54 68 65 72 65 20 69 73 20 68 6f 77 65 76 65 72  There is however
427c0 2c 0a 2a 2a 20 6f 6e 65 20 65 78 63 65 70 74 69  ,.** one excepti
427d0 6f 6e 2e 20 57 68 65 6e 20 65 78 65 63 75 74 69  on. When executi
427e0 6e 67 20 61 20 22 44 52 4f 50 20 54 41 42 4c 45  ng a "DROP TABLE
427f0 22 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58  " or "DROP INDEX
42800 22 20 73 74 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20  " statement,.** 
42810 53 51 4c 69 74 65 20 63 68 65 63 6b 73 20 69 66  SQLite checks if
42820 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 63   there are any c
42830 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69  urrently executi
42840 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ng SELECT statem
42850 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 62 65 6c  ents.** that bel
42860 6f 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ong to the same 
42870 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 74  connection. If t
42880 68 65 72 65 20 61 72 65 2c 20 53 51 4c 49 54 45  here are, SQLITE
42890 5f 4c 4f 43 4b 45 44 20 69 73 0a 2a 2a 20 72 65  _LOCKED is.** re
428a0 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20  turned. In this 
428b0 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f  case there is no
428c0 20 22 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65   "blocking conne
428d0 63 74 69 6f 6e 22 2c 20 73 6f 20 69 6e 76 6f 6b  ction", so invok
428e0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75  ing.** sqlite3_u
428f0 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 72  nlock_notify() r
42900 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 75 6e  esults in the un
42910 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
42920 62 61 63 6b 20 62 65 69 6e 67 0a 2a 2a 20 69 6e  back being.** in
42930 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  voked immediatel
42940 79 2e 20 49 66 20 74 68 65 20 61 70 70 6c 69 63  y. If the applic
42950 61 74 69 6f 6e 20 74 68 65 6e 20 72 65 2d 61 74  ation then re-at
42960 74 65 6d 70 74 73 20 74 68 65 20 22 44 52 4f 50  tempts the "DROP
42970 20 54 41 42 4c 45 22 0a 2a 2a 20 6f 72 20 22 44   TABLE".** or "D
42980 52 4f 50 20 49 4e 44 45 58 22 20 71 75 65 72 79  ROP INDEX" query
42990 2c 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6c 6f  , an infinite lo
429a0 6f 70 20 6d 69 67 68 74 20 62 65 20 74 68 65 20  op might be the 
429b0 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  result..**.** On
429c0 65 20 77 61 79 20 61 72 6f 75 6e 64 20 74 68 69  e way around thi
429d0 73 20 70 72 6f 62 6c 65 6d 20 69 73 20 74 6f 20  s problem is to 
429e0 63 68 65 63 6b 20 74 68 65 20 65 78 74 65 6e 64  check the extend
429f0 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65  ed error code re
42a00 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 61 6e 20  turned.** by an 
42a10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63  sqlite3_step() c
42a20 61 6c 6c 2e 20 49 66 20 74 68 65 72 65 20 69 73  all. If there is
42a30 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e   a blocking conn
42a40 65 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  ection, then the
42a50 0a 2a 2a 20 65 78 74 65 6e 64 65 64 20 65 72 72  .** extended err
42a60 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74 20 74  or code is set t
42a70 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f  o SQLITE_LOCKED_
42a80 53 48 41 52 45 44 43 41 43 48 45 2e 20 4f 74 68  SHAREDCACHE. Oth
42a90 65 72 77 69 73 65 2c 20 69 6e 0a 2a 2a 20 74 68  erwise, in.** th
42aa0 65 20 73 70 65 63 69 61 6c 20 22 44 52 4f 50 20  e special "DROP 
42ab0 54 41 42 4c 45 2f 49 4e 44 45 58 22 20 63 61 73  TABLE/INDEX" cas
42ac0 65 2c 20 74 68 65 20 65 78 74 65 6e 64 65 64 20  e, the extended 
42ad0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 6a 75  error code is ju
42ae0 73 74 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f  st .** SQLITE_LO
42af0 43 4b 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  CKED..*/.SQLITE_
42b00 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
42b10 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 0a 20  unlock_notify(. 
42b20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b   sqlite3 *pBlock
42b30 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ed,             
42b40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42b50 57 61 69 74 69 6e 67 20 63 6f 6e 6e 65 63 74 69  Waiting connecti
42b60 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  on */.  void (*x
42b70 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a 2a 61  Notify)(void **a
42b80 70 41 72 67 2c 20 69 6e 74 20 6e 41 72 67 29 2c  pArg, int nArg),
42b90 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20      /* Callback 
42ba0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f  function to invo
42bb0 6b 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4e  ke */.  void *pN
42bc0 6f 74 69 66 79 41 72 67 20 20 20 20 20 20 20 20  otifyArg        
42bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42be0 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
42bf0 74 6f 20 70 61 73 73 20 74 6f 20 78 4e 6f 74 69  to pass to xNoti
42c00 66 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  fy */.);../*.** 
42c10 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 74 68  Undo the hack th
42c20 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c 6f 61  at converts floa
42c30 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 65 73  ting point types
42c40 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f 72 0a   to integer for.
42c50 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 72 6f  ** builds on pro
42c60 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75 74 20  cessors without 
42c70 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73  floating point s
42c80 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 65  upport..*/.#ifde
42c90 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
42ca0 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 75  OATING_POINT.# u
42cb0 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 6e 64  ndef double.#end
42cc0 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20  if..#if 0.}  /* 
42cd0 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78 74 65  End of the 'exte
42ce0 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 2a 2f  rn "C"' block */
42cf0 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  .#endif.#endif..
42d00 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
42d10 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e 68  End of sqlite3.h
42d20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
42d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
42d50 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
42d60 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
42d70 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
42d80 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
42d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
42da0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
42db0 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68 20 69  Include hash.h i
42dc0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
42dd0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
42de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
42df0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
42e00 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e  Begin file hash.
42e10 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
42e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
42e40 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
42e50 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  mber 22.**.** Th
42e60 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
42e70 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
42e80 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
42e90 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
42ea0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
42eb0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
42ec0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
42ed0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
42ee0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
42ef0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
42f00 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
42f10 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
42f20 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
42f30 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
42f40 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
42f50 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
42f60 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
42f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42fb0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
42fc0 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c  s the header fil
42fd0 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69  e for the generi
42fe0 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70  c hash-table imp
42ff0 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73  lemenation.** us
43000 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a  ed in SQLite..**
43010 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e 68 2c  .** $Id: hash.h,
43020 76 20 31 2e 31 35 20 32 30 30 39 2f 30 35 2f 30  v 1.15 2009/05/0
43030 32 20 31 33 3a 32 39 3a 33 38 20 64 72 68 20 45  2 13:29:38 drh E
43040 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
43050 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a  _SQLITE_HASH_H_.
43060 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f  #define _SQLITE_
43070 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77  HASH_H_../* Forw
43080 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
43090 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 2e 20   of structures. 
430a0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
430b0 74 20 48 61 73 68 20 48 61 73 68 3b 0a 74 79 70  t Hash Hash;.typ
430c0 65 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68  edef struct Hash
430d0 45 6c 65 6d 20 48 61 73 68 45 6c 65 6d 3b 0a 0a  Elem HashElem;..
430e0 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 68 61  /* A complete ha
430f0 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69  sh table is an i
43100 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
43110 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
43120 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72  re..** The inter
43130 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72  nals of this str
43140 75 63 74 75 72 65 20 61 72 65 20 69 6e 74 65 6e  ucture are inten
43150 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65  ded to be opaque
43160 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f   -- client.** co
43170 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74  de should not at
43180 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73 73 20  tempt to access 
43190 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20 66 69  or modify the fi
431a0 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 72  elds of this str
431b0 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65 63 74  ucture.** direct
431c0 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68 69 73  ly.  Change this
431d0 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20   structure only 
431e0 62 79 20 75 73 69 6e 67 20 74 68 65 20 72 6f 75  by using the rou
431f0 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20  tines below..** 
43200 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 20 6f 66  However, some of
43210 20 74 68 65 20 22 70 72 6f 63 65 64 75 72 65 73   the "procedures
43220 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73  " and "functions
43230 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20  " for modifying 
43240 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67  and.** accessing
43250 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
43260 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f  are really macro
43270 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74 20 72  s, so we can't r
43280 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68  eally make.** th
43290 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 70 61  is structure opa
432a0 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65  que..**.** All e
432b0 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 68  lements of the h
432c0 61 73 68 20 74 61 62 6c 65 20 61 72 65 20 6f 6e  ash table are on
432d0 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79   a single doubly
432e0 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a  -linked list..**
432f0 20 48 61 73 68 2e 66 69 72 73 74 20 70 6f 69 6e   Hash.first poin
43300 74 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f  ts to the head o
43310 66 20 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2a 0a  f this list..**.
43320 2a 2a 20 54 68 65 72 65 20 61 72 65 20 48 61 73  ** There are Has
43330 68 2e 68 74 73 69 7a 65 20 62 75 63 6b 65 74 73  h.htsize buckets
43340 2e 20 20 45 61 63 68 20 62 75 63 6b 65 74 20 70  .  Each bucket p
43350 6f 69 6e 74 73 20 74 6f 20 61 20 73 70 6f 74 20  oints to a spot 
43360 69 6e 0a 2a 2a 20 74 68 65 20 67 6c 6f 62 61 6c  in.** the global
43370 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c   doubly-linked l
43380 69 73 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e  ist.  The conten
43390 74 73 20 6f 66 20 74 68 65 20 62 75 63 6b 65 74  ts of the bucket
433a0 20 61 72 65 20 74 68 65 0a 2a 2a 20 65 6c 65 6d   are the.** elem
433b0 65 6e 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 70  ent pointed to p
433c0 6c 75 73 20 74 68 65 20 6e 65 78 74 20 5f 68 74  lus the next _ht
433d0 2e 63 6f 75 6e 74 2d 31 20 65 6c 65 6d 65 6e 74  .count-1 element
433e0 73 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a  s in the list..*
433f0 2a 0a 2a 2a 20 48 61 73 68 2e 68 74 73 69 7a 65  *.** Hash.htsize
43400 20 61 6e 64 20 48 61 73 68 2e 68 74 20 6d 61 79   and Hash.ht may
43410 20 62 65 20 7a 65 72 6f 2e 20 20 49 6e 20 74 68   be zero.  In th
43420 61 74 20 63 61 73 65 20 6c 6f 6f 6b 75 70 20 69  at case lookup i
43430 73 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 61 20 6c  s done.** by a l
43440 69 6e 65 61 72 20 73 65 61 72 63 68 20 6f 66 20  inear search of 
43450 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 2e  the global list.
43460 20 20 46 6f 72 20 73 6d 61 6c 6c 20 74 61 62 6c    For small tabl
43470 65 73 2c 20 74 68 65 20 0a 2a 2a 20 48 61 73 68  es, the .** Hash
43480 2e 68 74 20 74 61 62 6c 65 20 69 73 20 6e 65 76  .ht table is nev
43490 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 63  er allocated bec
434a0 61 75 73 65 20 69 66 20 74 68 65 72 65 20 61 72  ause if there ar
434b0 65 20 66 65 77 20 65 6c 65 6d 65 6e 74 73 0a 2a  e few elements.*
434c0 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  * in the table, 
434d0 69 74 20 69 73 20 66 61 73 74 65 72 20 74 6f 20  it is faster to 
434e0 64 6f 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72  do a linear sear
434f0 63 68 20 74 68 61 6e 20 74 6f 20 6d 61 6e 61 67  ch than to manag
43500 65 0a 2a 2a 20 74 68 65 20 68 61 73 68 20 74 61  e.** the hash ta
43510 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48  ble..*/.struct H
43520 61 73 68 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ash {.  unsigned
43530 20 69 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20   int htsize;    
43540 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
43550 75 63 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61  uckets in the ha
43560 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 6e  sh table */.  un
43570 73 69 67 6e 65 64 20 69 6e 74 20 63 6f 75 6e 74  signed int count
43580 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
43590 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
435a0 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
435b0 48 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b  HashElem *first;
435c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
435d0 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
435e0 66 20 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20  f the array */. 
435f0 20 73 74 72 75 63 74 20 5f 68 74 20 7b 20 20 20   struct _ht {   
43600 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68             /* th
43610 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a  e hash table */.
43620 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20      int count;  
43630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
43640 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
43650 69 65 73 20 77 69 74 68 20 74 68 69 73 20 68 61  ies with this ha
43660 73 68 20 2a 2f 0a 20 20 20 20 48 61 73 68 45 6c  sh */.    HashEl
43670 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20  em *chain;      
43680 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
43690 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 77  to first entry w
436a0 69 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f  ith this hash */
436b0 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a  .  } *ht;.};../*
436c0 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e   Each element in
436d0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
436e0 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
436f0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
43700 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20  .** structure.  
43710 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65  All elements are
43720 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e   stored on a sin
43730 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65  gle doubly-linke
43740 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67  d list..**.** Ag
43750 61 69 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74  ain, this struct
43760 75 72 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ure is intended 
43770 74 6f 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75  to be opaque, bu
43780 74 20 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c  t it can't reall
43790 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62  y.** be opaque b
437a0 65 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65  ecause it is use
437b0 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  d by macros..*/.
437c0 73 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20  struct HashElem 
437d0 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65  {.  HashElem *ne
437e0 78 74 2c 20 2a 70 72 65 76 3b 20 20 20 20 20 20  xt, *prev;      
437f0 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65   /* Next and pre
43800 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69  vious elements i
43810 6e 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  n the table */. 
43820 20 76 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20   void *data;    
43830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
43840 20 44 61 74 61 20 61 73 73 6f 63 69 61 74 65 64   Data associated
43850 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65   with this eleme
43860 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  nt */.  const ch
43870 61 72 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b  ar *pKey; int nK
43880 65 79 3b 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f  ey;  /* Key asso
43890 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
438a0 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a   element */.};..
438b0 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75  /*.** Access rou
438c0 74 69 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74  tines.  To delet
438d0 65 2c 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c  e, insert a NULL
438e0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c   pointer..*/.SQL
438f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
43900 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74   sqlite3HashInit
43910 28 48 61 73 68 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Hash*);.SQLITE_
43920 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
43930 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
43940 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61  Hash*, const cha
43950 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  r *pKey, int nKe
43960 79 2c 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b  y, void *pData);
43970 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
43980 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73  void *sqlite3Has
43990 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68  hFind(const Hash
439a0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  *, const char *p
439b0 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a  Key, int nKey);.
439c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
439d0 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43  oid sqlite3HashC
439e0 6c 65 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a  lear(Hash*);../*
439f0 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c  .** Macros for l
43a00 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20  ooping over all 
43a10 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61  elements of a ha
43a20 73 68 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69  sh table.  The i
43a30 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20  diom is.** like 
43a40 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61  this:.**.**   Ha
43a50 73 68 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45  sh h;.**   HashE
43a60 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e  lem *p;.**   ...
43a70 0a 2a 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69  .**   for(p=sqli
43a80 74 65 48 61 73 68 46 69 72 73 74 28 26 68 29 3b  teHashFirst(&h);
43a90 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68   p; p=sqliteHash
43aa0 4e 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20  Next(p)){.**    
43ab0 20 53 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a   SomeStructure *
43ac0 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61  pData = sqliteHa
43ad0 73 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20  shData(p);.**   
43ae0 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e    // do somethin
43af0 67 20 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20  g with pData.** 
43b00 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73    }.*/.#define s
43b10 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 48  qliteHashFirst(H
43b20 29 20 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a  )  ((H)->first).
43b30 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61  #define sqliteHa
43b40 73 68 4e 65 78 74 28 45 29 20 20 20 28 28 45 29  shNext(E)   ((E)
43b50 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20  ->next).#define 
43b60 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 45  sqliteHashData(E
43b70 29 20 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a  )   ((E)->data).
43b80 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  /* #define sqlit
43b90 65 48 61 73 68 4b 65 79 28 45 29 20 20 20 20 28  eHashKey(E)    (
43ba0 28 45 29 2d 3e 70 4b 65 79 29 20 2f 2f 20 4e 4f  (E)->pKey) // NO
43bb0 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 20 23 64 65  T USED */./* #de
43bc0 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b  fine sqliteHashK
43bd0 65 79 73 69 7a 65 28 45 29 20 28 28 45 29 2d 3e  eysize(E) ((E)->
43be0 6e 4b 65 79 29 20 20 2f 2f 20 4e 4f 54 20 55 53  nKey)  // NOT US
43bf0 45 44 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d  ED */../*.** Num
43c00 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
43c10 6e 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a  n a hash table.*
43c20 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c  /./* #define sql
43c30 69 74 65 48 61 73 68 43 6f 75 6e 74 28 48 29 20  iteHashCount(H) 
43c40 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 20 2f 2f   ((H)->count) //
43c50 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 0a 23 65   NOT USED */..#e
43c60 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f  ndif /* _SQLITE_
43c70 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  HASH_H_ */../***
43c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
43c90 6f 66 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a  of hash.h ******
43ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
43cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
43ce0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
43cf0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
43d00 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
43d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
43d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
43d30 75 64 65 20 70 61 72 73 65 2e 68 20 69 6e 20 74  ude parse.h in t
43d40 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
43d50 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
43d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
43d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
43d80 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 68 20 2a  n file parse.h *
43d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66  **********/.#def
43dc0 69 6e 65 20 54 4b 5f 53 45 4d 49 20 20 20 20 20  ine TK_SEMI     
43dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43de0 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
43df0 20 54 4b 5f 45 58 50 4c 41 49 4e 20 20 20 20 20   TK_EXPLAIN     
43e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43e10 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 54 4b      2.#define TK
43e20 5f 51 55 45 52 59 20 20 20 20 20 20 20 20 20 20  _QUERY          
43e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43e40 20 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c   3.#define TK_PL
43e50 41 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AN              
43e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a                4.
43e70 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 47 49 4e  #define TK_BEGIN
43e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43e90 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65             5.#de
43ea0 66 69 6e 65 20 54 4b 5f 54 52 41 4e 53 41 43 54  fine TK_TRANSACT
43eb0 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  ION             
43ec0 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e          6.#defin
43ed0 65 20 54 4b 5f 44 45 46 45 52 52 45 44 20 20 20  e TK_DEFERRED   
43ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ef0 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 54       7.#define T
43f00 4b 5f 49 4d 4d 45 44 49 41 54 45 20 20 20 20 20  K_IMMEDIATE     
43f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43f20 20 20 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45    8.#define TK_E
43f30 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20  XCLUSIVE        
43f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
43f50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d  .#define TK_COMM
43f60 49 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IT              
43f70 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64             10.#d
43f80 65 66 69 6e 65 20 54 4b 5f 45 4e 44 20 20 20 20  efine TK_END    
43f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43fa0 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69          11.#defi
43fb0 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20  ne TK_ROLLBACK  
43fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43fd0 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20       12.#define 
43fe0 54 4b 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 20  TK_SAVEPOINT    
43ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44000 20 20 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    13.#define TK_
44010 52 45 4c 45 41 53 45 20 20 20 20 20 20 20 20 20  RELEASE         
44020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
44030 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20  4.#define TK_TO 
44040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44050 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23              15.#
44060 64 65 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20  define TK_TABLE 
44070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44080 20 20 20 20 20 20 20 20 20 31 36 0a 23 64 65 66           16.#def
44090 69 6e 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20  ine TK_CREATE   
440a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
440b0 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65        17.#define
440c0 20 54 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20   TK_IF          
440d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
440e0 20 20 20 31 38 0a 23 64 65 66 69 6e 65 20 54 4b     18.#define TK
440f0 5f 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20  _NOT            
44100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44110 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58  19.#define TK_EX
44120 49 53 54 53 20 20 20 20 20 20 20 20 20 20 20 20  ISTS            
44130 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a               20.
44140 23 64 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20  #define TK_TEMP 
44150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44160 20 20 20 20 20 20 20 20 20 20 32 31 0a 23 64 65            21.#de
44170 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20  fine TK_LP      
44180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44190 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e         22.#defin
441a0 65 20 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20  e TK_RP         
441b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
441c0 20 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 54      23.#define T
441d0 4b 5f 41 53 20 20 20 20 20 20 20 20 20 20 20 20  K_AS            
441e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
441f0 20 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   24.#define TK_C
44200 4f 4d 4d 41 20 20 20 20 20 20 20 20 20 20 20 20  OMMA            
44210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 35                25
44220 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20  .#define TK_ID  
44230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44240 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 23 64             26.#d
44250 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44  efine TK_INDEXED
44260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44270 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 66 69          27.#defi
44280 6e 65 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20  ne TK_ABORT     
44290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
442a0 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20       28.#define 
442b0 54 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20 20  TK_AFTER        
442c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
442d0 20 20 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    29.#define TK_
442e0 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20  ANALYZE         
442f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
44300 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 43  0.#define TK_ASC
44310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44320 20 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23              31.#
44330 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43 48  define TK_ATTACH
44340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44350 20 20 20 20 20 20 20 20 20 33 32 0a 23 64 65 66           32.#def
44360 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20 20  ine TK_BEFORE   
44370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44380 20 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65        33.#define
44390 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 20   TK_BY          
443a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
443b0 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 54 4b     34.#define TK
443c0 5f 43 41 53 43 41 44 45 20 20 20 20 20 20 20 20  _CASCADE        
443d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
443e0 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41  35.#define TK_CA
443f0 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ST              
44400 20 20 20 20 20 20 20 20 20 20 20 20 20 33 36 0a               36.
44410 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d  #define TK_COLUM
44420 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20  NKW             
44430 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 65            37.#de
44440 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43 54  fine TK_CONFLICT
44450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44460 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e         38.#defin
44470 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20 20  e TK_DATABASE   
44480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44490 20 20 20 20 33 39 0a 23 64 65 66 69 6e 65 20 54      39.#define T
444a0 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20 20  K_DESC          
444b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
444c0 20 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44   40.#define TK_D
444d0 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20  ETACH           
444e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31                41
444f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43 48  .#define TK_EACH
44500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44510 20 20 20 20 20 20 20 20 20 20 20 34 32 0a 23 64             42.#d
44520 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20 20  efine TK_FAIL   
44530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44540 20 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69          43.#defi
44550 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20 20  ne TK_FOR       
44560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44570 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20       44.#define 
44580 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20 20  TK_IGNORE       
44590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445a0 20 20 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    45.#define TK_
445b0 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20  INITIALLY       
445c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
445d0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53  6.#define TK_INS
445e0 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20  TEAD            
445f0 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a 23              47.#
44600 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f 4b  define TK_LIKE_K
44610 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  W               
44620 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66           48.#def
44630 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20 20  ine TK_MATCH    
44640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44650 20 20 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65        49.#define
44660 20 54 4b 5f 4b 45 59 20 20 20 20 20 20 20 20 20   TK_KEY         
44670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44680 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 54 4b     50.#define TK
44690 5f 4f 46 20 20 20 20 20 20 20 20 20 20 20 20 20  _OF             
446a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
446b0 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46  51.#define TK_OF
446c0 46 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20  FSET            
446d0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a               52.
446e0 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 41 47 4d  #define TK_PRAGM
446f0 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  A               
44700 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 65            53.#de
44710 66 69 6e 65 20 54 4b 5f 52 41 49 53 45 20 20 20  fine TK_RAISE   
44720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44730 20 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e         54.#defin
44740 65 20 54 4b 5f 52 45 50 4c 41 43 45 20 20 20 20  e TK_REPLACE    
44750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44760 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65 20 54      55.#define T
44770 4b 5f 52 45 53 54 52 49 43 54 20 20 20 20 20 20  K_RESTRICT      
44780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44790 20 35 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   56.#define TK_R
447a0 4f 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OW              
447b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 37                57
447c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 49 47  .#define TK_TRIG
447d0 47 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  GER             
447e0 20 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64             58.#d
447f0 65 66 69 6e 65 20 54 4b 5f 56 41 43 55 55 4d 20  efine TK_VACUUM 
44800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44810 20 20 20 20 20 20 20 20 35 39 0a 23 64 65 66 69          59.#defi
44820 6e 65 20 54 4b 5f 56 49 45 57 20 20 20 20 20 20  ne TK_VIEW      
44830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44840 20 20 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20       60.#define 
44850 54 4b 5f 56 49 52 54 55 41 4c 20 20 20 20 20 20  TK_VIRTUAL      
44860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44870 20 20 36 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    61.#define TK_
44880 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  REINDEX         
44890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
448a0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e  2.#define TK_REN
448b0 41 4d 45 20 20 20 20 20 20 20 20 20 20 20 20 20  AME             
448c0 20 20 20 20 20 20 20 20 20 20 20 20 36 33 0a 23              63.#
448d0 64 65 66 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f  define TK_CTIME_
448e0 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  KW              
448f0 20 20 20 20 20 20 20 20 20 36 34 0a 23 64 65 66           64.#def
44900 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20 20 20 20  ine TK_ANY      
44910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44920 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65        65.#define
44930 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 20 20 20   TK_OR          
44940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44950 20 20 20 36 36 0a 23 64 65 66 69 6e 65 20 54 4b     66.#define TK
44960 5f 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20  _AND            
44970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44980 36 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53  67.#define TK_IS
44990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
449a0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38 0a               68.
449b0 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 54 57 45  #define TK_BETWE
449c0 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EN              
449d0 20 20 20 20 20 20 20 20 20 20 36 39 0a 23 64 65            69.#de
449e0 66 69 6e 65 20 54 4b 5f 49 4e 20 20 20 20 20 20  fine TK_IN      
449f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a00 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e         70.#defin
44a10 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20  e TK_ISNULL     
44a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a30 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65 20 54      71.#define T
44a40 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20  K_NOTNULL       
44a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a60 20 37 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e   72.#define TK_N
44a70 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
44a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 33                73
44a90 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 51 20 20  .#define TK_EQ  
44aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ab0 20 20 20 20 20 20 20 20 20 20 20 37 34 0a 23 64             74.#d
44ac0 65 66 69 6e 65 20 54 4b 5f 47 54 20 20 20 20 20  efine TK_GT     
44ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ae0 20 20 20 20 20 20 20 20 37 35 0a 23 64 65 66 69          75.#defi
44af0 6e 65 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20  ne TK_LE        
44b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44b10 20 20 20 20 20 37 36 0a 23 64 65 66 69 6e 65 20       76.#define 
44b20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 20 20  TK_LT           
44b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44b40 20 20 37 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    77.#define TK_
44b50 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  GE              
44b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
44b70 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 53 43  8.#define TK_ESC
44b80 41 50 45 20 20 20 20 20 20 20 20 20 20 20 20 20  APE             
44b90 20 20 20 20 20 20 20 20 20 20 20 20 37 39 0a 23              79.#
44ba0 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44  define TK_BITAND
44bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44bc0 20 20 20 20 20 20 20 20 20 38 30 0a 23 64 65 66           80.#def
44bd0 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20 20 20 20  ine TK_BITOR    
44be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44bf0 20 20 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65        81.#define
44c00 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 20 20 20   TK_LSHIFT      
44c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44c20 20 20 20 38 32 0a 23 64 65 66 69 6e 65 20 54 4b     82.#define TK
44c30 5f 52 53 48 49 46 54 20 20 20 20 20 20 20 20 20  _RSHIFT         
44c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44c50 38 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c  83.#define TK_PL
44c60 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  US              
44c70 20 20 20 20 20 20 20 20 20 20 20 20 20 38 34 0a               84.
44c80 23 64 65 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 53  #define TK_MINUS
44c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ca0 20 20 20 20 20 20 20 20 20 20 38 35 0a 23 64 65            85.#de
44cb0 66 69 6e 65 20 54 4b 5f 53 54 41 52 20 20 20 20  fine TK_STAR    
44cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44cd0 20 20 20 20 20 20 20 38 36 0a 23 64 65 66 69 6e         86.#defin
44ce0 65 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 20 20  e TK_SLASH      
44cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44d00 20 20 20 20 38 37 0a 23 64 65 66 69 6e 65 20 54      87.#define T
44d10 4b 5f 52 45 4d 20 20 20 20 20 20 20 20 20 20 20  K_REM           
44d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44d30 20 38 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   88.#define TK_C
44d40 4f 4e 43 41 54 20 20 20 20 20 20 20 20 20 20 20  ONCAT           
44d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 39                89
44d60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c  .#define TK_COLL
44d70 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ATE             
44d80 20 20 20 20 20 20 20 20 20 20 20 39 30 0a 23 64             90.#d
44d90 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20  efine TK_UMINUS 
44da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44db0 20 20 20 20 20 20 20 20 39 31 0a 23 64 65 66 69          91.#defi
44dc0 6e 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 20  ne TK_UPLUS     
44dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44de0 20 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20       92.#define 
44df0 54 4b 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 20  TK_BITNOT       
44e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44e10 20 20 39 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    93.#define TK_
44e20 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 20  STRING          
44e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
44e40 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49  4.#define TK_JOI
44e50 4e 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20 20  N_KW            
44e60 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a 23              95.#
44e70 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 52  define TK_CONSTR
44e80 41 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20  AINT            
44e90 20 20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66           96.#def
44ea0 69 6e 65 20 54 4b 5f 44 45 46 41 55 4c 54 20 20  ine TK_DEFAULT  
44eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ec0 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65        97.#define
44ed0 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20 20   TK_NULL        
44ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ef0 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 54 4b     98.#define TK
44f00 5f 50 52 49 4d 41 52 59 20 20 20 20 20 20 20 20  _PRIMARY        
44f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44f20 39 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e  99.#define TK_UN
44f30 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20 20  IQUE            
44f40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 30               100
44f50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43  .#define TK_CHEC
44f60 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K               
44f70 20 20 20 20 20 20 20 20 20 20 20 31 30 31 0a 23             101.#
44f80 64 65 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45  define TK_REFERE
44f90 4e 43 45 53 20 20 20 20 20 20 20 20 20 20 20 20  NCES            
44fa0 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65           102.#de
44fb0 66 69 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52  fine TK_AUTOINCR
44fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44fd0 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 66 69         103.#defi
44fe0 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20  ne TK_ON        
44ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45000 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65       104.#define
45010 20 54 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20   TK_DELETE      
45020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45030 20 20 20 31 30 35 0a 23 64 65 66 69 6e 65 20 54     105.#define T
45040 4b 5f 55 50 44 41 54 45 20 20 20 20 20 20 20 20  K_UPDATE        
45050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45060 20 31 30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   106.#define TK_
45070 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20  INSERT          
45080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
45090 30 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45  07.#define TK_SE
450a0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
450b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 38               108
450c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45  .#define TK_DEFE
450d0 52 52 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  RRABLE          
450e0 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23             109.#
450f0 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47  define TK_FOREIG
45100 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
45110 20 20 20 20 20 20 20 20 20 31 31 30 0a 23 64 65           110.#de
45120 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20  fine TK_DROP    
45130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45140 20 20 20 20 20 20 20 31 31 31 0a 23 64 65 66 69         111.#defi
45150 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20  ne TK_UNION     
45160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45170 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65       112.#define
45180 20 54 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20   TK_ALL         
45190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
451a0 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 54     113.#define T
451b0 4b 5f 45 58 43 45 50 54 20 20 20 20 20 20 20 20  K_EXCEPT        
451c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
451d0 20 31 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f   114.#define TK_
451e0 49 4e 54 45 52 53 45 43 54 20 20 20 20 20 20 20  INTERSECT       
451f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
45200 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45  15.#define TK_SE
45210 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20  LECT            
45220 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36               116
45230 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54  .#define TK_DIST
45240 49 4e 43 54 20 20 20 20 20 20 20 20 20 20 20 20  INCT            
45250 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23             117.#
45260 64 65 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20  define TK_DOT   
45270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45280 20 20 20 20 20 20 20 20 20 31 31 38 0a 23 64 65           118.#de
45290 66 69 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20  fine TK_FROM    
452a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
452b0 20 20 20 20 20 20 20 31 31 39 0a 23 64 65 66 69         119.#defi
452c0 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20  ne TK_JOIN      
452d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
452e0 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65       120.#define
452f0 20 54 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20   TK_USING       
45300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45310 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 54     121.#define T
45320 4b 5f 4f 52 44 45 52 20 20 20 20 20 20 20 20 20  K_ORDER         
45330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45340 20 31 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   122.#define TK_
45350 47 52 4f 55 50 20 20 20 20 20 20 20 20 20 20 20  GROUP           
45360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
45370 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41  23.#define TK_HA
45380 56 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20  VING            
45390 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 34               124
453a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49  .#define TK_LIMI
453b0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
453c0 20 20 20 20 20 20 20 20 20 20 20 31 32 35 0a 23             125.#
453d0 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20  define TK_WHERE 
453e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
453f0 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 64 65           126.#de
45400 66 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20  fine TK_INTO    
45410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45420 20 20 20 20 20 20 20 31 32 37 0a 23 64 65 66 69         127.#defi
45430 6e 65 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20  ne TK_VALUES    
45440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45450 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65       128.#define
45460 20 54 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20   TK_INTEGER     
45470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45480 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 54     129.#define T
45490 4b 5f 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20  K_FLOAT         
454a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
454b0 20 31 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   130.#define TK_
454c0 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 20  BLOB            
454d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
454e0 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  31.#define TK_RE
454f0 47 49 53 54 45 52 20 20 20 20 20 20 20 20 20 20  GISTER          
45500 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 32               132
45510 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49  .#define TK_VARI
45520 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  ABLE            
45530 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23             133.#
45540 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20  define TK_CASE  
45550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45560 20 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65           134.#de
45570 66 69 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20  fine TK_WHEN    
45580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45590 20 20 20 20 20 20 20 31 33 35 0a 23 64 65 66 69         135.#defi
455a0 6e 65 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20  ne TK_THEN      
455b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
455c0 20 20 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65       136.#define
455d0 20 54 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20   TK_ELSE        
455e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
455f0 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 54     137.#define T
45600 4b 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  K_INDEX         
45610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45620 20 31 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f   138.#define TK_
45630 41 4c 54 45 52 20 20 20 20 20 20 20 20 20 20 20  ALTER           
45640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
45650 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44  39.#define TK_AD
45660 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
45670 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 30               140
45680 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54  .#define TK_TO_T
45690 45 58 54 20 20 20 20 20 20 20 20 20 20 20 20 20  EXT             
456a0 20 20 20 20 20 20 20 20 20 20 20 31 34 31 0a 23             141.#
456b0 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f  define TK_TO_BLO
456c0 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  B               
456d0 20 20 20 20 20 20 20 20 20 31 34 32 0a 23 64 65           142.#de
456e0 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52  fine TK_TO_NUMER
456f0 49 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IC              
45700 20 20 20 20 20 20 20 31 34 33 0a 23 64 65 66 69         143.#defi
45710 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20  ne TK_TO_INT    
45720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45730 20 20 20 20 20 31 34 34 0a 23 64 65 66 69 6e 65       144.#define
45740 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20   TK_TO_REAL     
45750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45760 20 20 20 31 34 35 0a 23 64 65 66 69 6e 65 20 54     145.#define T
45770 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c 45 20 20 20  K_END_OF_FILE   
45780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45790 20 31 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   146.#define TK_
457a0 49 4c 4c 45 47 41 4c 20 20 20 20 20 20 20 20 20  ILLEGAL         
457b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
457c0 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 50  47.#define TK_SP
457d0 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ACE             
457e0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 38               148
457f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 43 4c  .#define TK_UNCL
45800 4f 53 45 44 5f 53 54 52 49 4e 47 20 20 20 20 20  OSED_STRING     
45810 20 20 20 20 20 20 20 20 20 20 20 31 34 39 0a 23             149.#
45820 64 65 66 69 6e 65 20 54 4b 5f 46 55 4e 43 54 49  define TK_FUNCTI
45830 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ON              
45840 20 20 20 20 20 20 20 20 20 31 35 30 0a 23 64 65           150.#de
45850 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20  fine TK_COLUMN  
45860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45870 20 20 20 20 20 20 20 31 35 31 0a 23 64 65 66 69         151.#defi
45880 6e 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  ne TK_AGG_FUNCTI
45890 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ON              
458a0 20 20 20 20 20 31 35 32 0a 23 64 65 66 69 6e 65       152.#define
458b0 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 20   TK_AGG_COLUMN  
458c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
458d0 20 20 20 31 35 33 0a 23 64 65 66 69 6e 65 20 54     153.#define T
458e0 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 20 20 20  K_CONST_FUNC    
458f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45900 20 31 35 34 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   154../*********
45910 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72  ***** End of par
45920 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  se.h ***********
45930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45950 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
45960 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
45970 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
45980 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
45990 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
459a0 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c  ****/.#include <
459b0 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64  stdio.h>.#includ
459c0 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e  e <stdlib.h>.#in
459d0 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e  clude <string.h>
459e0 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72  .#include <asser
459f0 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  t.h>.#include <s
45a00 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20  tddef.h>../*.** 
45a10 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72  If compiling for
45a20 20 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61   a processor tha
45a30 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67  t lacks floating
45a40 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a   point support,.
45a50 2a 2a 20 73 75 62 73 74 69 74 75 74 65 20 69 6e  ** substitute in
45a60 74 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69  teger for floati
45a70 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64  ng-point.*/.#ifd
45a80 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
45a90 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20  LOATING_POINT.# 
45aa0 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71  define double sq
45ab0 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 64 65 66  lite_int64.# def
45ac0 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54  ine LONGDOUBLE_T
45ad0 59 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  YPE sqlite_int64
45ae0 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  .# ifndef SQLITE
45af0 5f 42 49 47 5f 44 42 4c 0a 23 20 20 20 64 65 66  _BIG_DBL.#   def
45b00 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44  ine SQLITE_BIG_D
45b10 42 4c 20 28 30 78 37 66 66 66 66 66 66 66 66 66  BL (0x7fffffffff
45b20 66 66 66 66 66 66 29 0a 23 20 65 6e 64 69 66 0a  ffffff).# endif.
45b30 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
45b40 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55  OMIT_DATETIME_FU
45b50 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65 20 53  NCS 1.# define S
45b60 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
45b70 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54   1.# undef SQLIT
45b80 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36  E_MIXED_ENDIAN_6
45b90 34 42 49 54 5f 46 4c 4f 41 54 0a 23 65 6e 64 69  4BIT_FLOAT.#endi
45ba0 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
45bb0 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 69 6e  _BIG_DBL.# defin
45bc0 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  e SQLITE_BIG_DBL
45bd0 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 0a 0a   (1e99).#endif..
45be0 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d 50 44  /*.** OMIT_TEMPD
45bf0 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 69 66  B is set to 1 if
45c00 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d   SQLITE_OMIT_TEM
45c10 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 2c 20  PDB is defined, 
45c20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 61 72  or 0.** afterwar
45c30 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 20 6d  d. Having this m
45c40 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 20 74  acro allows us t
45c50 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 63 6f  o cause the C co
45c60 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 6f 6d  mpiler .** to om
45c70 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 79 20  it code used by 
45c80 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 74 68  TEMP tables with
45c90 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e 64 65  out messy #ifnde
45ca0 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f  f statements..*/
45cb0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
45cc0 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 66 69  MIT_TEMPDB.#defi
45cd0 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 31  ne OMIT_TEMPDB 1
45ce0 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
45cf0 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 65 6e  MIT_TEMPDB 0.#en
45d00 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  dif../*.** If th
45d10 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
45d20 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 74  o is set to 1, t
45d30 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20  hen NULL values 
45d40 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a  are considered.*
45d50 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 6e 20  * distinct when 
45d60 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 74  determining whet
45d70 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f 20 65  her or not two e
45d80 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20 73  ntries are the s
45d90 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e 49 51  ame.** in a UNIQ
45da0 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20  UE index.  This 
45db0 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 74 67  is the way Postg
45dc0 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c 20 44  reSQL, Oracle, D
45dd0 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 4f 43  B2, MySQL,.** OC
45de0 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 62 69  ELOT, and Firebi
45df0 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 54 68  rd all work.  Th
45e00 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 78 70  e SQL92 spec exp
45e10 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 68 69  licitly says thi
45e20 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 79 20  s.** is the way 
45e30 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 70 6f  things are suppo
45e40 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a  se to work..**.*
45e50 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * If the followi
45e60 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20  ng macro is set 
45e70 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c 73 20  to 0, the NULLs 
45e80 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 20 66  are indistinct f
45e90 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 69  or.** a UNIQUE i
45ea0 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 20 6d  ndex.  In this m
45eb0 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f 6e 6c  ode, you can onl
45ec0 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20  y have a single 
45ed0 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 66 6f  NULL entry.** fo
45ee0 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61  r a column decla
45ef0 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 68 69  red UNIQUE.  Thi
45f00 73 20 69 73 20 74 68 65 20 77 61 79 20 49 6e 66  s is the way Inf
45f10 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 53 65  ormix and SQL Se
45f20 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f  rver.** work..*/
45f30 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f 44 49  .#define NULL_DI
45f40 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51 55  STINCT_FOR_UNIQU
45f50 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22  E 1../*.** The "
45f60 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75 6d  file format" num
45f70 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65  ber is an intege
45f80 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d  r that is increm
45f90 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 0a 2a  ented whenever.*
45fa0 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65 6c  * the VDBE-level
45fb0 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68 61   file format cha
45fc0 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  nges.  The follo
45fd0 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69  wing macros defi
45fe0 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 64 65  ne the.** the de
45ff0 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d 61  fault file forma
46000 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62 61  t for new databa
46010 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78 69  ses and the maxi
46020 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 0a  mum file format.
46030 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62 72  ** that the libr
46040 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a 2a 2f  ary can read..*/
46050 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
46060 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20  MAX_FILE_FORMAT 
46070 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  4.#ifndef SQLITE
46080 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f  _DEFAULT_FILE_FO
46090 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 53 51  RMAT.# define SQ
460a0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
460b0 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e 64 69  E_FORMAT 1.#endi
460c0 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 65  f../*.** Provide
460d0 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65   a default value
460e0 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d 50   for SQLITE_TEMP
460f0 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20 69  _STORE in case i
46100 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69  t is not specifi
46110 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d  ed.** on the com
46120 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 66  mand-line.*/.#if
46130 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50  ndef SQLITE_TEMP
46140 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65 20  _STORE.# define 
46150 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
46160 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  E 1.#endif../*.*
46170 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20 64  * GCC does not d
46180 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65 74  efine the offset
46190 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77 65  of() macro so we
461a0 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20 69  'll have to do i
461b0 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e 0a  t.** ourselves..
461c0 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73 65  */.#ifndef offse
461d0 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66 73  tof.#define offs
461e0 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c 46  etof(STRUCTURE,F
461f0 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63 68  IELD) ((int)((ch
46200 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52 45  ar*)&((STRUCTURE
46210 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23 65  *)0)->FIELD)).#e
46220 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ndif../*.** Chec
46230 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 73  k to see if this
46240 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45 42   machine uses EB
46250 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65 6c  CDIC.  (Yes, bel
46260 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f  ieve it or.** no
46270 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74 69  t, there are sti
46280 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74 20  ll machines out 
46290 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20 45  there that use E
462a0 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 27  BCDIC.).*/.#if '
462b0 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20 64  A' == '\301'.# d
462c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42 43  efine SQLITE_EBC
462d0 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65  DIC 1.#else.# de
462e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43 49  fine SQLITE_ASCI
462f0 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  I 1.#endif../*.*
46300 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b 6e  * Integers of kn
46310 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65 73  own sizes.  Thes
46320 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68 74  e typedefs might
46330 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63 68   change for arch
46340 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68 65  itectures.** whe
46350 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65 72  re the sizes ver
46360 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f 72  y.  Preprocessor
46370 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61 69   macros are avai
46380 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74 68  lable so that th
46390 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 62  e.** types can b
463a0 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 72  e conveniently r
463b0 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70  edefined at comp
463c0 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 20  ile-type.  Like 
463d0 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
463e0 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 54      cc '-DUINTPT
463f0 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67  R_TYPE=long long
46400 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66   int' ....*/.#if
46410 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45  ndef UINT32_TYPE
46420 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 49  .# ifdef HAVE_UI
46430 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e 65  NT32_T.#  define
46440 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69 6e   UINT32_TYPE uin
46450 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  t32_t.# else.#  
46460 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59  define UINT32_TY
46470 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 0a  PE unsigned int.
46480 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  # endif.#endif.#
46490 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54 59  ifndef UINT16_TY
464a0 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f  PE.# ifdef HAVE_
464b0 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69  UINT16_T.#  defi
464c0 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ne UINT16_TYPE u
464d0 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23  int16_t.# else.#
464e0 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 5f    define UINT16_
464f0 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68  TYPE unsigned sh
46500 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a  ort int.# endif.
46510 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49  #endif.#ifndef I
46520 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65  NT16_TYPE.# ifde
46530 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a 23  f HAVE_INT16_T.#
46540 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54    define INT16_T
46550 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65 6c  YPE int16_t.# el
46560 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54  se.#  define INT
46570 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69 6e  16_TYPE short in
46580 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  t.# endif.#endif
46590 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f 54  .#ifndef UINT8_T
465a0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
465b0 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69  _UINT8_T.#  defi
465c0 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 69  ne UINT8_TYPE ui
465d0 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  nt8_t.# else.#  
465e0 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50  define UINT8_TYP
465f0 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a  E unsigned char.
46600 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  # endif.#endif.#
46610 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45  ifndef INT8_TYPE
46620 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e  .# ifdef HAVE_IN
46630 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 49  T8_T.#  define I
46640 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 0a  NT8_TYPE int8_t.
46650 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
46660 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e 65   INT8_TYPE signe
46670 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23  d char.# endif.#
46680 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c 4f  endif.#ifndef LO
46690 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 20  NGDOUBLE_TYPE.# 
466a0 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c  define LONGDOUBL
466b0 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 62  E_TYPE long doub
466c0 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65  le.#endif.typede
466d0 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  f sqlite_int64 i
466e0 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  64;          /* 
466f0 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  8-byte signed in
46700 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66  teger */.typedef
46710 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 75   sqlite_uint64 u
46720 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 38  64;         /* 8
46730 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69  -byte unsigned i
46740 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
46750 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 33  f UINT32_TYPE u3
46760 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  2;           /* 
46770 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  4-byte unsigned 
46780 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
46790 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ef UINT16_TYPE u
467a0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  16;           /*
467b0 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   2-byte unsigned
467c0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
467d0 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 69  def INT16_TYPE i
467e0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  16;            /
467f0 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 2-byte signed 
46800 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
46810 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 38  ef UINT8_TYPE u8
46820 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
46830 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   1-byte unsigned
46840 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
46850 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69 38  def INT8_TYPE i8
46860 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
46870 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 1-byte signed 
46880 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a  integer */../*.*
46890 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65  * Macros to dete
468a0 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68  rmine whether th
468b0 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67  e machine is big
468c0 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61   or little endia
468d0 6e 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20  n,.** evaluated 
468e0 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23  at runtime..*/.#
468f0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41  ifdef SQLITE_AMA
46900 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45  LGAMATION.SQLITE
46910 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69  _PRIVATE const i
46920 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20  nt sqlite3one = 
46930 31 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f  1;.#else.SQLITE_
46940 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e  PRIVATE const in
46950 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65  t sqlite3one;.#e
46960 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64  ndif.#if defined
46970 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
46980 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
46990 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c  efined(_M_IX86)\
469a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
469b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
469c0 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36   defined(__x86_6
469d0 34 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  4) || defined(__
469e0 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69  x86_64__).# defi
469f0 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  ne SQLITE_BIGEND
46a00 49 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69 6e  IAN    0.# defin
46a10 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45  e SQLITE_LITTLEE
46a20 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e 65  NDIAN 1.# define
46a30 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
46a40 49 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46 31  IVE  SQLITE_UTF1
46a50 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  6LE.#else.# defi
46a60 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  ne SQLITE_BIGEND
46a70 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 2a  IAN    (*(char *
46a80 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d  )(&sqlite3one)==
46a90 30 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  0).# define SQLI
46aa0 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20  TE_LITTLEENDIAN 
46ab0 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69  (*(char *)(&sqli
46ac0 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65  te3one)==1).# de
46ad0 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31  fine SQLITE_UTF1
46ae0 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45 5f  6NATIVE (SQLITE_
46af0 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45  BIGENDIAN?SQLITE
46b00 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45 5f  _UTF16BE:SQLITE_
46b10 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a  UTF16LE).#endif.
46b20 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73  ./*.** Constants
46b30 20 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73 74   for the largest
46b40 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f   and smallest po
46b50 73 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73 69  ssible 64-bit si
46b60 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a  gned integers..*
46b70 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 61  * These macros a
46b80 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77  re designed to w
46b90 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e  ork correctly on
46ba0 20 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e 64   both 32-bit and
46bb0 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69   64-bit.** compi
46bc0 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  lers..*/.#define
46bd0 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 20   LARGEST_INT64  
46be0 28 30 78 66 66 66 66 66 66 66 66 7c 28 28 28 69  (0xffffffff|(((i
46bf0 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c  64)0x7fffffff)<<
46c00 33 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d 41  32)).#define SMA
46c10 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28 69  LLEST_INT64 (((i
46c20 36 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53 54  64)-1) - LARGEST
46c30 5f 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20  _INT64)../* .** 
46c40 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 65  Round up a numbe
46c50 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 61  r to the next la
46c60 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66  rger multiple of
46c70 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 65   8.  This is use
46c80 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d  d.** to force 8-
46c90 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f  byte alignment o
46ca0 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 65  n 64-bit archite
46cb0 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69  ctures..*/.#defi
46cc0 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 20  ne ROUND8(x)    
46cd0 20 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f   (((x)+7)&~7)../
46ce0 2a 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20  *.** Round down 
46cf0 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 6d  to the nearest m
46d00 75 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a  ultiple of 8.*/.
46d10 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57  #define ROUNDDOW
46d20 4e 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a 0a  N8(x) ((x)&~7)..
46d30 2f 2a 0a 2a 2a 20 41 73 73 65 72 74 20 74 68 61  /*.** Assert tha
46d40 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 58 20  t the pointer X 
46d50 69 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e  is aligned to an
46d60 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79   8-byte boundary
46d70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 49 47  ..*/.#define EIG
46d80 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
46d90 54 28 58 29 20 20 20 28 28 28 28 63 68 61 72 2a  T(X)   ((((char*
46da0 29 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30 29  )(X) - (char*)0)
46db0 26 37 29 3d 3d 30 29 0a 0a 2f 2a 0a 2a 2a 20 41  &7)==0)../*.** A
46dc0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
46dd0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
46de0 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
46df0 20 73 74 6f 72 65 20 74 68 65 20 62 75 73 79 2d   store the busy-
46e00 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62  handler.** callb
46e10 61 63 6b 20 66 6f 72 20 61 20 67 69 76 65 6e 20  ack for a given 
46e20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a  sqlite handle. .
46e30 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
46e40 2e 62 75 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d  .busyHandler mem
46e50 62 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ber of the sqlit
46e60 65 20 73 74 72 75 63 74 20 63 6f 6e 74 61 69 6e  e struct contain
46e70 73 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 63 61  s the busy.** ca
46e80 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65 20 64  llback for the d
46e90 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
46ea0 45 61 63 68 20 70 61 67 65 72 20 6f 70 65 6e 65  Each pager opene
46eb0 64 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65  d via the sqlite
46ec0 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 70 61  .** handle is pa
46ed0 73 73 65 64 20 61 20 70 6f 69 6e 74 65 72 20 74  ssed a pointer t
46ee0 6f 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e  o sqlite.busyHan
46ef0 64 6c 65 72 2e 20 54 68 65 20 62 75 73 79 2d 68  dler. The busy-h
46f00 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61  andler.** callba
46f10 63 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ck is currently 
46f20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f  invoked only fro
46f30 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 2e 63  m within pager.c
46f40 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
46f50 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20  uct BusyHandler 
46f60 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72  BusyHandler;.str
46f70 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20  uct BusyHandler 
46f80 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29  {.  int (*xFunc)
46f90 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f  (void *,int);  /
46fa0 2a 20 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62  * The busy callb
46fb0 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ack */.  void *p
46fc0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
46fd0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
46fe0 20 74 6f 20 62 75 73 79 20 63 61 6c 6c 62 61 63   to busy callbac
46ff0 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79  k */.  int nBusy
47000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
47010 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64    /* Incremented
47020 20 77 69 74 68 20 65 61 63 68 20 62 75 73 79 20   with each busy 
47030 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  call */.};../*.*
47040 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61  * Name of the ma
47050 73 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61  ster database ta
47060 62 6c 65 2e 20 20 54 68 65 20 6d 61 73 74 65 72  ble.  The master
47070 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a   database table.
47080 2a 2a 20 69 73 20 61 20 73 70 65 63 69 61 6c 20  ** is a special 
47090 74 61 62 6c 65 20 74 68 61 74 20 68 6f 6c 64 73  table that holds
470a0 20 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 61   the names and a
470b0 74 74 72 69 62 75 74 65 73 20 6f 66 20 61 6c 6c  ttributes of all
470c0 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c 65 73 20  .** user tables 
470d0 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a  and indices..*/.
470e0 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e  #define MASTER_N
470f0 41 4d 45 20 20 20 20 20 20 20 22 73 71 6c 69 74  AME       "sqlit
47100 65 5f 6d 61 73 74 65 72 22 0a 23 64 65 66 69 6e  e_master".#defin
47110 65 20 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41  e TEMP_MASTER_NA
47120 4d 45 20 20 22 73 71 6c 69 74 65 5f 74 65 6d 70  ME  "sqlite_temp
47130 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20  _master"../*.** 
47140 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66  The root-page of
47150 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61   the master data
47160 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23  base table..*/.#
47170 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f  define MASTER_RO
47180 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a  OT       1../*.*
47190 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
471a0 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a  e schema table..
471b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43 48 45 4d  */.#define SCHEM
471c0 41 5f 54 41 42 4c 45 28 78 29 20 20 28 28 21 4f  A_TABLE(x)  ((!O
471d0 4d 49 54 5f 54 45 4d 50 44 42 29 26 26 28 78 3d  MIT_TEMPDB)&&(x=
471e0 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f  =1)?TEMP_MASTER_
471f0 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45  NAME:MASTER_NAME
47200 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65  )../*.** A conve
47210 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20 74 68 61  nience macro tha
47220 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  t returns the nu
47230 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
47240 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e   in.** an array.
47250 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41 72 72 61  .*/.#define Arra
47260 79 53 69 7a 65 28 58 29 20 20 20 20 28 28 69 6e  ySize(X)    ((in
47270 74 29 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a  t)(sizeof(X)/siz
47280 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a  eof(X[0])))../*.
47290 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
472a0 20 76 61 6c 75 65 20 61 73 20 61 20 64 65 73 74   value as a dest
472b0 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20  ructor means to 
472c0 75 73 65 20 73 71 6c 69 74 65 33 44 62 46 72 65  use sqlite3DbFre
472d0 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  e()..** This is 
472e0 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65  an internal exte
472f0 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f  nsion to SQLITE_
47300 53 54 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54  STATIC and SQLIT
47310 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a  E_TRANSIENT..*/.
47320 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
47330 59 4e 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74  YNAMIC   ((sqlit
47340 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79  e3_destructor_ty
47350 70 65 29 73 71 6c 69 74 65 33 44 62 46 72 65 65  pe)sqlite3DbFree
47360 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51  )../*.** When SQ
47370 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73  LITE_OMIT_WSD is
47380 20 64 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61   defined, it mea
47390 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 72 67  ns that the targ
473a0 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73  et platform does
473b0 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  .** not support 
473c0 57 72 69 74 61 62 6c 65 20 53 74 61 74 69 63 20  Writable Static 
473d0 44 61 74 61 20 28 57 53 44 29 20 73 75 63 68 20  Data (WSD) such 
473e0 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74  as global and st
473f0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a  atic variables..
47400 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73  ** All variables
47410 20 6d 75 73 74 20 65 69 74 68 65 72 20 62 65 20   must either be 
47420 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20  on the stack or 
47430 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
47440 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68  cated from.** th
47450 65 20 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53  e heap.  When WS
47460 44 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64  D is unsupported
47470 2c 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 64  , the variable d
47480 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74  eclarations scat
47490 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68  tered.** through
474a0 6f 75 74 20 74 68 65 20 53 51 4c 69 74 65 20 63  out the SQLite c
474b0 6f 64 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20  ode must become 
474c0 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61  constants instea
474d0 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57  d.  The SQLITE_W
474e0 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75  SD.** macro is u
474f0 73 65 64 20 66 6f 72 20 74 68 69 73 20 70 75 72  sed for this pur
47500 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65  pose.  And inste
47510 61 64 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e  ad of referencin
47520 67 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a  g the variable.*
47530 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75  * directly, we u
47540 73 65 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20  se its constant 
47550 61 73 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b  as a key to look
47560 75 70 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20  up the run-time 
47570 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66  allocated.** buf
47580 66 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72  fer that holds r
47590 65 61 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54  eal variable.  T
475a0 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61  he constant is a
475b0 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69  lso the initiali
475c0 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72  zer.** for the r
475d0 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65  un-time allocate
475e0 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20  d buffer..**.** 
475f0 49 6e 20 74 68 65 20 75 73 75 61 6c 20 63 61 73  In the usual cas
47600 65 20 77 68 65 72 65 20 57 53 44 20 69 73 20 73  e where WSD is s
47610 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 53 51  upported, the SQ
47620 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f  LITE_WSD and GLO
47630 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65  BAL.** macros be
47640 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20  come no-ops and 
47650 68 61 76 65 20 7a 65 72 6f 20 70 65 72 66 6f 72  have zero perfor
47660 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f  mance impact..*/
47670 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
47680 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 66 69 6e  MIT_WSD.  #defin
47690 65 20 53 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e  e SQLITE_WSD con
476a0 73 74 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f  st.  #define GLO
476b0 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 2a 29 73  BAL(t,v) (*(t*)s
476c0 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28  qlite3_wsd_find(
476d0 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 73 69 7a  (void*)&(v), siz
476e0 65 6f 66 28 76 29 29 29 0a 20 20 23 64 65 66 69  eof(v))).  #defi
476f0 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ne sqlite3Global
47700 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c 28 73 74  Config GLOBAL(st
47710 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66  ruct Sqlite3Conf
47720 69 67 2c 20 73 71 6c 69 74 65 33 43 6f 6e 66 69  ig, sqlite3Confi
47730 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20  g).SQLITE_API   
47740 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 73 64 5f  int sqlite3_wsd_
47750 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20  init(int N, int 
47760 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 20  J);.SQLITE_API  
47770 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77   void *sqlite3_w
47780 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c  sd_find(void *K,
47790 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20   int L);.#else. 
477a0 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f   #define SQLITE_
477b0 57 53 44 20 0a 20 20 23 64 65 66 69 6e 65 20 47  WSD .  #define G
477c0 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a 20 20 23  LOBAL(t,v) v.  #
477d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c  define sqlite3Gl
477e0 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74  obalConfig sqlit
477f0 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a  e3Config.#endif.
47800 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
47810 77 69 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20  wing macros are 
47820 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73  used to suppress
47830 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e   compiler warnin
47840 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b  gs and to.** mak
47850 65 20 69 74 20 63 6c 65 61 72 20 74 6f 20 68 75  e it clear to hu
47860 6d 61 6e 20 72 65 61 64 65 72 73 20 77 68 65 6e  man readers when
47870 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61   a function para
47880 6d 65 74 65 72 20 69 73 20 64 65 6c 69 62 65 72  meter is deliber
47890 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75  ately .** left u
478a0 6e 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 65  nused within the
478b0 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 74   body of a funct
478c0 69 6f 6e 2e 20 54 68 69 73 20 75 73 75 61 6c 6c  ion. This usuall
478d0 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a  y happens when.*
478e0 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  * a function is 
478f0 63 61 6c 6c 65 64 20 76 69 61 20 61 20 66 75 6e  called via a fun
47900 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46  ction pointer. F
47910 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 20 0a  or example the .
47920 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
47930 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61 67 67 72  n of an SQL aggr
47940 65 67 61 74 65 20 73 74 65 70 20 63 61 6c 6c 62  egate step callb
47950 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75 73 65 20  ack may not use 
47960 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  the.** parameter
47970 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20   indicating the 
47980 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
47990 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 74 68  nts passed to th
479a0 65 20 61 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20  e aggregate,.** 
479b0 69 66 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74  if it knows that
479c0 20 74 68 69 73 20 69 73 20 65 6e 66 6f 72 63 65   this is enforce
479d0 64 20 65 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a  d elsewhere..**.
479e0 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e 63 74 69  ** When a functi
479f0 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  on parameter is 
47a00 6e 6f 74 20 75 73 65 64 20 61 74 20 61 6c 6c 20  not used at all 
47a10 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20  within the body 
47a20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a  of a function,.*
47a30 2a 20 69 74 20 69 73 20 67 65 6e 65 72 61 6c 6c  * it is generall
47a40 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55 73 65 64  y named "NotUsed
47a50 22 20 6f 72 20 22 4e 6f 74 55 73 65 64 32 22 20  " or "NotUsed2" 
47a60 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67 73 20 65  to make things e
47a70 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20  ven clearer..** 
47a80 48 6f 77 65 76 65 72 2c 20 74 68 65 73 65 20 6d  However, these m
47a90 61 63 72 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62  acros may also b
47aa0 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72 65  e used to suppre
47ab0 73 73 20 77 61 72 6e 69 6e 67 73 20 72 65 6c 61  ss warnings rela
47ac0 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65  ted to.** parame
47ad0 74 65 72 73 20 74 68 61 74 20 6d 61 79 20 6f 72  ters that may or
47ae0 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64   may not be used
47af0 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f   depending on co
47b00 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  mpilation option
47b10 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c  s..** For exampl
47b20 65 20 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65  e those paramete
47b30 72 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20  rs only used in 
47b40 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
47b50 6e 74 73 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a  nts. In these.**
47b60 20 63 61 73 65 73 20 74 68 65 20 70 61 72 61 6d   cases the param
47b70 65 74 65 72 73 20 61 72 65 20 6e 61 6d 65 64 20  eters are named 
47b80 61 73 20 70 65 72 20 74 68 65 20 75 73 75 61 6c  as per the usual
47b90 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f   conventions..*/
47ba0 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f  .#define UNUSED_
47bb0 50 41 52 41 4d 45 54 45 52 28 78 29 20 28 76 6f  PARAMETER(x) (vo
47bc0 69 64 29 28 78 29 0a 23 64 65 66 69 6e 65 20 55  id)(x).#define U
47bd0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
47be0 28 78 2c 79 29 20 55 4e 55 53 45 44 5f 50 41 52  (x,y) UNUSED_PAR
47bf0 41 4d 45 54 45 52 28 78 29 2c 55 4e 55 53 45 44  AMETER(x),UNUSED
47c00 5f 50 41 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f  _PARAMETER(y)../
47c10 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66  *.** Forward ref
47c20 65 72 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63  erences to struc
47c30 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66  tures.*/.typedef
47c40 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20   struct AggInfo 
47c50 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  AggInfo;.typedef
47c60 20 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74   struct AuthCont
47c70 65 78 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b  ext AuthContext;
47c80 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
47c90 42 69 74 76 65 63 20 42 69 74 76 65 63 3b 0a 74  Bitvec Bitvec;.t
47ca0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52 6f  ypedef struct Ro
47cb0 77 53 65 74 20 52 6f 77 53 65 74 3b 0a 74 79 70  wSet RowSet;.typ
47cc0 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 6c  edef struct Coll
47cd0 53 65 71 20 43 6f 6c 6c 53 65 71 3b 0a 74 79 70  Seq CollSeq;.typ
47ce0 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 75  edef struct Colu
47cf0 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a 74 79 70 65 64  mn Column;.typed
47d00 65 66 20 73 74 72 75 63 74 20 44 62 20 44 62 3b  ef struct Db Db;
47d10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
47d20 53 63 68 65 6d 61 20 53 63 68 65 6d 61 3b 0a 74  Schema Schema;.t
47d30 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78  ypedef struct Ex
47d40 70 72 20 45 78 70 72 3b 0a 74 79 70 65 64 65 66  pr Expr;.typedef
47d50 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
47d60 20 45 78 70 72 4c 69 73 74 3b 0a 74 79 70 65 64   ExprList;.typed
47d70 65 66 20 73 74 72 75 63 74 20 46 4b 65 79 20 46  ef struct FKey F
47d80 4b 65 79 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Key;.typedef str
47d90 75 63 74 20 46 75 6e 63 44 65 66 20 46 75 6e 63  uct FuncDef Func
47da0 44 65 66 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Def;.typedef str
47db0 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 68 20  uct FuncDefHash 
47dc0 46 75 6e 63 44 65 66 48 61 73 68 3b 0a 74 79 70  FuncDefHash;.typ
47dd0 65 64 65 66 20 73 74 72 75 63 74 20 49 64 4c 69  edef struct IdLi
47de0 73 74 20 49 64 4c 69 73 74 3b 0a 74 79 70 65 64  st IdList;.typed
47df0 65 66 20 73 74 72 75 63 74 20 49 6e 64 65 78 20  ef struct Index 
47e00 49 6e 64 65 78 3b 0a 74 79 70 65 64 65 66 20 73  Index;.typedef s
47e10 74 72 75 63 74 20 4b 65 79 43 6c 61 73 73 20 4b  truct KeyClass K
47e20 65 79 43 6c 61 73 73 3b 0a 74 79 70 65 64 65 66  eyClass;.typedef
47e30 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20   struct KeyInfo 
47e40 4b 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  KeyInfo;.typedef
47e50 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64   struct Lookasid
47e60 65 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74 79 70  e Lookaside;.typ
47e70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b  edef struct Look
47e80 61 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73  asideSlot Lookas
47e90 69 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64 65 66  ideSlot;.typedef
47ea0 20 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 4d   struct Module M
47eb0 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66 20 73  odule;.typedef s
47ec0 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78  truct NameContex
47ed0 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 0a 74  t NameContext;.t
47ee0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61  ypedef struct Pa
47ef0 72 73 65 20 50 61 72 73 65 3b 0a 74 79 70 65 64  rse Parse;.typed
47f00 65 66 20 73 74 72 75 63 74 20 53 61 76 65 70 6f  ef struct Savepo
47f10 69 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b 0a 74  int Savepoint;.t
47f20 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65  ypedef struct Se
47f30 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79 70  lect Select;.typ
47f40 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c  edef struct SrcL
47f50 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70  ist SrcList;.typ
47f60 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41  edef struct StrA
47f70 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74  ccum StrAccum;.t
47f80 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61  ypedef struct Ta
47f90 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64  ble Table;.typed
47fa0 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c  ef struct TableL
47fb0 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74  ock TableLock;.t
47fc0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f  ypedef struct To
47fd0 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64  ken Token;.typed
47fe0 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  ef struct Trigge
47ff0 72 53 74 61 63 6b 20 54 72 69 67 67 65 72 53 74  rStack TriggerSt
48000 61 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ack;.typedef str
48010 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20  uct TriggerStep 
48020 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74 79 70  TriggerStep;.typ
48030 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67  edef struct Trig
48040 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74 79 70  ger Trigger;.typ
48050 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 70 61  edef struct Unpa
48060 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70 61 63  ckedRecord Unpac
48070 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70 65 64  kedRecord;.typed
48080 65 66 20 73 74 72 75 63 74 20 57 61 6c 6b 65 72  ef struct Walker
48090 20 57 61 6c 6b 65 72 3b 0a 74 79 70 65 64 65 66   Walker;.typedef
480a0 20 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61   struct WherePla
480b0 6e 20 57 68 65 72 65 50 6c 61 6e 3b 0a 74 79 70  n WherePlan;.typ
480c0 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
480d0 65 49 6e 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b  eInfo WhereInfo;
480e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
480f0 57 68 65 72 65 4c 65 76 65 6c 20 57 68 65 72 65  WhereLevel Where
48100 4c 65 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65  Level;../*.** De
48110 66 65 72 20 73 6f 75 72 63 69 6e 67 20 76 64 62  fer sourcing vdb
48120 65 2e 68 20 61 6e 64 20 62 74 72 65 65 2e 68 20  e.h and btree.h 
48130 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 20  until after the 
48140 22 75 38 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75  "u8" and .** "Bu
48150 73 79 48 61 6e 64 6c 65 72 22 20 74 79 70 65 64  syHandler" typed
48160 65 66 73 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f  efs. vdbe.h also
48170 20 72 65 71 75 69 72 65 73 20 61 20 66 65 77 20   requires a few 
48180 6f 66 20 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a  of the opaque.**
48190 20 70 6f 69 6e 74 65 72 20 74 79 70 65 73 20 28   pointer types (
481a0 69 2e 65 2e 20 46 75 6e 63 44 65 66 29 20 64 65  i.e. FuncDef) de
481b0 66 69 6e 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a  fined above..*/.
481c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
481d0 49 6e 63 6c 75 64 65 20 62 74 72 65 65 2e 68 20  Include btree.h 
481e0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
481f0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
48200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
48210 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
48220 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65  Begin file btree
48230 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
48240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
48260 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
48270 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
48280 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
48290 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
482a0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
482b0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
482c0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
482d0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
482e0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
482f0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
48300 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
48310 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
48320 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
48330 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
48340 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
48350 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
48360 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
48370 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
48380 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
48390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
483a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
483b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
483c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
483d0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68  ******.** This h
483e0 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e  eader file defin
483f0 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  es the interface
48400 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65   that the sqlite
48410 20 42 2d 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20   B-Tree file.** 
48420 73 75 62 73 79 73 74 65 6d 2e 20 20 53 65 65 20  subsystem.  See 
48430 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  comments in the 
48440 73 6f 75 72 63 65 20 63 6f 64 65 20 66 6f 72 20  source code for 
48450 61 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72  a detailed descr
48460 69 70 74 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61  iption.** of wha
48470 74 20 65 61 63 68 20 69 6e 74 65 72 66 61 63 65  t each interface
48480 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a   routine does..*
48490 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 62  *.** @(#) $Id: b
484a0 74 72 65 65 2e 68 2c 76 20 31 2e 31 31 34 20 32  tree.h,v 1.114 2
484b0 30 30 39 2f 30 35 2f 30 34 20 31 31 3a 34 32 3a  009/05/04 11:42:
484c0 33 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  30 danielk1977 E
484d0 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
484e0 5f 42 54 52 45 45 5f 48 5f 0a 23 64 65 66 69 6e  _BTREE_H_.#defin
484f0 65 20 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20  e _BTREE_H_../* 
48500 54 4f 44 4f 3a 20 54 68 69 73 20 64 65 66 69 6e  TODO: This defin
48510 69 74 69 6f 6e 20 69 73 20 6a 75 73 74 20 69 6e  ition is just in
48520 63 6c 75 64 65 64 20 73 6f 20 6f 74 68 65 72 20  cluded so other 
48530 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 69 6c 65 2e  modules compile.
48540 20 49 74 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20   It.** needs to 
48550 62 65 20 72 65 76 69 73 69 74 65 64 2e 0a 2a 2f  be revisited..*/
48560 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
48570 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 31 30 0a  N_BTREE_META 10.
48580 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 66 69 6e 65  ./*.** If define
48590 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61  d as non-zero, a
485a0 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e  uto-vacuum is en
485b0 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
485c0 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 69  . Otherwise.** i
485d0 74 20 6d 75 73 74 20 62 65 20 74 75 72 6e 65 64  t must be turned
485e0 20 6f 6e 20 66 6f 72 20 65 61 63 68 20 64 61 74   on for each dat
485f0 61 62 61 73 65 20 75 73 69 6e 67 20 22 50 52 41  abase using "PRA
48600 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20  GMA auto_vacuum 
48610 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  = 1"..*/.#ifndef
48620 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
48630 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 23 64 65  AUTOVACUUM.  #de
48640 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
48650 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 30  ULT_AUTOVACUUM 0
48660 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
48670 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
48680 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20 20 20 20  M_NONE 0        
48690 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20 61 75 74  /* Do not do aut
486a0 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66  o-vacuum */.#def
486b0 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41  ine BTREE_AUTOVA
486c0 43 55 55 4d 5f 46 55 4c 4c 20 31 20 20 20 20 20  CUUM_FULL 1     
486d0 20 20 20 2f 2a 20 44 6f 20 66 75 6c 6c 20 61 75     /* Do full au
486e0 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65  to-vacuum */.#de
486f0 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56  fine BTREE_AUTOV
48700 41 43 55 55 4d 5f 49 4e 43 52 20 32 20 20 20 20  ACUUM_INCR 2    
48710 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
48720 61 6c 20 76 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a  al vacuum */../*
48730 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  .** Forward decl
48740 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75  arations of stru
48750 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66  cture.*/.typedef
48760 20 73 74 72 75 63 74 20 42 74 72 65 65 20 42 74   struct Btree Bt
48770 72 65 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ree;.typedef str
48780 75 63 74 20 42 74 43 75 72 73 6f 72 20 42 74 43  uct BtCursor BtC
48790 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73  ursor;.typedef s
487a0 74 72 75 63 74 20 42 74 53 68 61 72 65 64 20 42  truct BtShared B
487b0 74 53 68 61 72 65 64 3b 0a 74 79 70 65 64 65 66  tShared;.typedef
487c0 20 73 74 72 75 63 74 20 42 74 72 65 65 4d 75 74   struct BtreeMut
487d0 65 78 41 72 72 61 79 20 42 74 72 65 65 4d 75 74  exArray BtreeMut
487e0 65 78 41 72 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20  exArray;../*.** 
487f0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 72  This structure r
48800 65 63 6f 72 64 73 20 61 6c 6c 20 6f 66 20 74 68  ecords all of th
48810 65 20 42 74 72 65 65 73 20 74 68 61 74 20 6e 65  e Btrees that ne
48820 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20  ed to hold.** a 
48830 6d 75 74 65 78 20 62 65 66 6f 72 65 20 77 65 20  mutex before we 
48840 65 6e 74 65 72 20 73 71 6c 69 74 65 33 56 64 62  enter sqlite3Vdb
48850 65 45 78 65 63 28 29 2e 20 20 54 68 65 20 42 74  eExec().  The Bt
48860 72 65 65 73 20 61 72 65 0a 2a 2a 20 61 72 65 20  rees are.** are 
48870 70 6c 61 63 65 64 20 69 6e 20 61 42 74 72 65 65  placed in aBtree
48880 5b 5d 20 69 6e 20 6f 72 64 65 72 20 6f 66 20 61  [] in order of a
48890 42 74 72 65 65 5b 5d 2d 3e 70 42 74 2e 20 20 54  Btree[]->pBt.  T
488a0 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 65 20 63  hat way,.** we c
488b0 61 6e 20 61 6c 77 61 79 73 20 6c 6f 63 6b 20 61  an always lock a
488c0 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 6d 20 61  nd unlock them a
488d0 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73  ll quickly..*/.s
488e0 74 72 75 63 74 20 42 74 72 65 65 4d 75 74 65 78  truct BtreeMutex
488f0 41 72 72 61 79 20 7b 0a 20 20 69 6e 74 20 6e 4d  Array {.  int nM
48900 75 74 65 78 3b 0a 20 20 42 74 72 65 65 20 2a 61  utex;.  Btree *a
48910 42 74 72 65 65 5b 53 51 4c 49 54 45 5f 4d 41 58  Btree[SQLITE_MAX
48920 5f 41 54 54 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b  _ATTACHED+1];.};
48930 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ...SQLITE_PRIVAT
48940 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
48950 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20  eeOpen(.  const 
48960 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
48970 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
48980 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f  tabase file to o
48990 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  pen */.  sqlite3
489a0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
489b0 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20    /* Associated 
489c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
489d0 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  ion */.  Btree *
489e0 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *,              
489f0 20 20 2f 2a 20 52 65 74 75 72 6e 20 6f 70 65 6e    /* Return open
48a00 20 42 74 72 65 65 2a 20 68 65 72 65 20 2a 2f 0a   Btree* here */.
48a10 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
48a20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
48a30 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73  ags */.  int vfs
48a40 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20  Flags           
48a50 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65    /* Flags passe
48a60 64 20 74 68 72 6f 75 67 68 20 74 6f 20 56 46 53  d through to VFS
48a70 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20   open */.);../* 
48a80 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  The flags parame
48a90 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74  ter to sqlite3Bt
48aa0 72 65 65 4f 70 65 6e 20 63 61 6e 20 62 65 20 74  reeOpen can be t
48ab0 68 65 20 62 69 74 77 69 73 65 20 6f 72 20 6f 66  he bitwise or of
48ac0 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e   the.** followin
48ad0 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  g values..**.** 
48ae0 4e 4f 54 45 3a 20 20 54 68 65 73 65 20 76 61 6c  NOTE:  These val
48af0 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74  ues must match t
48b00 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
48b10 20 50 41 47 45 52 5f 20 76 61 6c 75 65 73 20 69   PAGER_ values i
48b20 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 2e 0a 2a 2f  n.** pager.h..*/
48b30 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4f  .#define BTREE_O
48b40 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 31 20 20  MIT_JOURNAL  1  
48b50 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 6a 6f  /* Do not use jo
48b60 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 72 67 75 6d  urnal.  No argum
48b70 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  ent */.#define B
48b80 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b  TREE_NO_READLOCK
48b90 20 20 20 32 20 20 2f 2a 20 4f 6d 69 74 20 72 65     2  /* Omit re
48ba0 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f  adlocks on reado
48bb0 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 23 64 65  nly files */.#de
48bc0 66 69 6e 65 20 42 54 52 45 45 5f 4d 45 4d 4f 52  fine BTREE_MEMOR
48bd0 59 20 20 20 20 20 20 20 20 34 20 20 2f 2a 20 49  Y        4  /* I
48be0 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e 20 20 4e 6f  n-memory DB.  No
48bf0 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65   argument */.#de
48c00 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 44 4f  fine BTREE_READO
48c10 4e 4c 59 20 20 20 20 20 20 38 20 20 2f 2a 20 4f  NLY      8  /* O
48c20 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  pen the database
48c30 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f   in read-only mo
48c40 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  de */.#define BT
48c50 52 45 45 5f 52 45 41 44 57 52 49 54 45 20 20 20  REE_READWRITE   
48c60 20 31 36 20 20 2f 2a 20 4f 70 65 6e 20 66 6f 72   16  /* Open for
48c70 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e   both reading an
48c80 64 20 77 72 69 74 69 6e 67 20 2a 2f 0a 23 64 65  d writing */.#de
48c90 66 69 6e 65 20 42 54 52 45 45 5f 43 52 45 41 54  fine BTREE_CREAT
48ca0 45 20 20 20 20 20 20 20 33 32 20 20 2f 2a 20 43  E       32  /* C
48cb0 72 65 61 74 65 20 74 68 65 20 64 61 74 61 62 61  reate the databa
48cc0 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f  se if it does no
48cd0 74 20 65 78 69 73 74 20 2a 2f 0a 0a 53 51 4c 49  t exist */..SQLI
48ce0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
48cf0 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
48d00 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
48d10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
48d20 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
48d30 65 53 69 7a 65 28 42 74 72 65 65 2a 2c 69 6e 74  eSize(Btree*,int
48d40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
48d50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
48d60 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c  eeSetSafetyLevel
48d70 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29  (Btree*,int,int)
48d80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
48d90 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
48da0 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74  eSyncDisabled(Bt
48db0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
48dc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
48dd0 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a  3BtreeSetPageSiz
48de0 65 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74  e(Btree*,int,int
48df0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
48e00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
48e10 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
48e20 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  e(Btree*);.SQLIT
48e30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
48e40 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67  lite3BtreeMaxPag
48e50 65 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69 6e  eCount(Btree*,in
48e60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
48e70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
48e80 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74  reeGetReserve(Bt
48e90 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
48ea0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
48eb0 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63  3BtreeSetAutoVac
48ec0 75 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74  uum(Btree *, int
48ed0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
48ee0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
48ef0 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeGetAutoVacuum(
48f00 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Btree *);.SQLITE
48f10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
48f20 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72  ite3BtreeBeginTr
48f30 61 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b  ans(Btree*,int);
48f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
48f50 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
48f60 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42  CommitPhaseOne(B
48f70 74 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  tree*, const cha
48f80 72 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c  r *zMaster);.SQL
48f90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
48fa0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
48fb0 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65  itPhaseTwo(Btree
48fc0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
48fd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
48fe0 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 2a  reeCommit(Btree*
48ff0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
49000 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
49010 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65  eeRollback(Btree
49020 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
49030 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
49040 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72  reeBeginStmt(Btr
49050 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ee*,int);.SQLITE
49060 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
49070 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54  ite3BtreeCreateT
49080 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74  able(Btree*, int
49090 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53  *, int flags);.S
490a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
490b0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  t sqlite3BtreeIs
490c0 49 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b  InTrans(Btree*);
490d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
490e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
490f0 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74  IsInReadTrans(Bt
49100 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
49110 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
49120 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70  3BtreeIsInBackup
49130 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
49140 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
49150 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d  qlite3BtreeSchem
49160 61 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20  a(Btree *, int, 
49170 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 29  void(*)(void *))
49180 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
49190 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
491a0 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74  eSchemaLocked(Bt
491b0 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ree *);.SQLITE_P
491c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
491d0 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65  e3BtreeLockTable
491e0 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 75  (Btree *, int, u
491f0 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  8);.SQLITE_PRIVA
49200 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
49210 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72  reeSavepoint(Btr
49220 65 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  ee *, int, int);
49230 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
49240 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
49250 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65  ite3BtreeGetFile
49260 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53  name(Btree *);.S
49270 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
49280 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
49290 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c  3BtreeGetJournal
492a0 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53  name(Btree *);.S
492b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
492c0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
492d0 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a 2c 20  pyFile(Btree *, 
492e0 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51 4c 49 54  Btree *);..SQLIT
492f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
49300 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 56 61  lite3BtreeIncrVa
49310 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 0a  cuum(Btree *);..
49320 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72  /* The flags par
49330 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
49340 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c  3BtreeCreateTabl
49350 65 20 63 61 6e 20 62 65 20 74 68 65 20 62 69 74  e can be the bit
49360 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66 20 74 68  wise OR.** of th
49370 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67  e following flag
49380 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  s:.*/.#define BT
49390 52 45 45 5f 49 4e 54 4b 45 59 20 20 20 20 20 31  REE_INTKEY     1
493a0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73      /* Table has
493b0 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20 73 69 67   only 64-bit sig
493c0 6e 65 64 20 69 6e 74 65 67 65 72 20 6b 65 79 73  ned integer keys
493d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
493e0 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 32 20 20  E_ZERODATA   2  
493f0 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6b    /* Table has k
49400 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61  eys only - no da
49410 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  ta */.#define BT
49420 52 45 45 5f 4c 45 41 46 44 41 54 41 20 20 20 34  REE_LEAFDATA   4
49430 20 20 20 20 2f 2a 20 44 61 74 61 20 73 74 6f 72      /* Data stor
49440 65 64 20 69 6e 20 6c 65 61 76 65 73 20 6f 6e 6c  ed in leaves onl
49450 79 2e 20 20 49 6d 70 6c 69 65 73 20 49 4e 54 4b  y.  Implies INTK
49460 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52  EY */..SQLITE_PR
49470 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
49480 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28  3BtreeDropTable(
49490 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Btree*, int, int
494a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
494b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
494c0 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74  reeClearTable(Bt
494d0 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29  ree*, int, int*)
494e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
494f0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
49500 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 2a 2c  eGetMeta(Btree*,
49510 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 2a 70   int idx, u32 *p
49520 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 5f 50  Value);.SQLITE_P
49530 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
49540 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74  e3BtreeUpdateMet
49550 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 69 64  a(Btree*, int id
49560 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b 0a 53  x, u32 value);.S
49570 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
49580 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54  id sqlite3BtreeT
49590 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74  ripAllCursors(Bt
495a0 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c  ree*, int);..SQL
495b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
495c0 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
495d0 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c 20 20 20  or(.  Btree*,   
495e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
495f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 54             /* BT
49600 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ree containing t
49610 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  able to open */.
49620 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
49630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49640 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
49650 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a  of root page */.
49660 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20    int wrFlag,   
49670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49680 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20         /* 1 for 
49690 77 72 69 74 69 6e 67 2e 20 20 30 20 66 6f 72 20  writing.  0 for 
496a0 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73  read-only */.  s
496b0 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 2a 2c 20  truct KeyInfo*, 
496c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
496d0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
496e0 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 61 72 65  ument to compare
496f0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42   function */.  B
49700 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  tCursor *pCursor
49710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49720 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20      /* Space to 
49730 77 72 69 74 65 20 63 75 72 73 6f 72 20 73 74 72  write cursor str
49740 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c  ucture */.);.SQL
49750 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
49760 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
49770 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b 0a 0a 53  orSize(void);..S
49780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
49790 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  t sqlite3BtreeCl
497a0 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75 72 73  oseCursor(BtCurs
497b0 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  or*);.SQLITE_PRI
497c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
497d0 42 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 20 42  BtreeMoveto(.  B
497e0 74 43 75 72 73 6f 72 2a 2c 0a 20 20 63 6f 6e 73  tCursor*,.  cons
497f0 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 0a 20 20  t void *pKey,.  
49800 69 36 34 20 6e 4b 65 79 2c 0a 20 20 69 6e 74 20  i64 nKey,.  int 
49810 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 52 65  bias,.  int *pRe
49820 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  s.);.SQLITE_PRIV
49830 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
49840 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
49850 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72 2a 2c  ed(.  BtCursor*,
49860 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
49870 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 20 69 36 34  d *pUnKey,.  i64
49880 20 69 6e 74 4b 65 79 2c 0a 20 20 69 6e 74 20 62   intKey,.  int b
49890 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 52 65 73  ias,.  int *pRes
498a0 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .);.SQLITE_PRIVA
498b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
498c0 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65  reeCursorHasMove
498d0 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  d(BtCursor*, int
498e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
498f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
49900 72 65 65 44 65 6c 65 74 65 28 42 74 43 75 72 73  reeDelete(BtCurs
49910 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  or*);.SQLITE_PRI
49920 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
49930 42 74 72 65 65 49 6e 73 65 72 74 28 42 74 43 75  BtreeInsert(BtCu
49940 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  rsor*, const voi
49950 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65  d *pKey, i64 nKe
49960 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y,.             
49970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49980 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20       const void 
49990 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
499a0 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
499b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
499c0 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20       int nZero, 
499d0 69 6e 74 20 62 69 61 73 2c 20 69 6e 74 20 73 65  int bias, int se
499e0 65 6b 52 65 73 75 6c 74 29 3b 0a 53 51 4c 49 54  ekResult);.SQLIT
499f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
49a00 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28  lite3BtreeFirst(
49a10 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
49a20 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pRes);.SQLITE_PR
49a30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
49a40 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72  3BtreeLast(BtCur
49a50 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29  sor*, int *pRes)
49a60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
49a70 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
49a80 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72 2a 2c  eNext(BtCursor*,
49a90 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c   int *pRes);.SQL
49aa0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
49ab0 73 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28  sqlite3BtreeEof(
49ac0 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49  BtCursor*);.SQLI
49ad0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
49ae0 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73  qlite3BtreeFlags
49af0 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c  (BtCursor*);.SQL
49b00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
49b10 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
49b20 69 6f 75 73 28 42 74 43 75 72 73 6f 72 2a 2c 20  ious(BtCursor*, 
49b30 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49  int *pRes);.SQLI
49b40 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
49b50 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
49b60 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 36  ze(BtCursor*, i6
49b70 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54  4 *pSize);.SQLIT
49b80 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
49b90 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 42 74  lite3BtreeKey(Bt
49ba0 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66  Cursor*, u32 off
49bb0 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f  set, u32 amt, vo
49bc0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id*);.SQLITE_PRI
49bd0 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71  VATE sqlite3 *sq
49be0 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
49bf0 44 62 28 63 6f 6e 73 74 20 42 74 43 75 72 73 6f  Db(const BtCurso
49c00 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
49c10 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ATE const void *
49c20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46  sqlite3BtreeKeyF
49c30 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a 2c 20  etch(BtCursor*, 
49c40 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49  int *pAmt);.SQLI
49c50 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
49c60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74   void *sqlite3Bt
49c70 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43  reeDataFetch(BtC
49c80 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d  ursor*, int *pAm
49c90 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
49ca0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
49cb0 72 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75  reeDataSize(BtCu
49cc0 72 73 6f 72 2a 2c 20 75 33 32 20 2a 70 53 69 7a  rsor*, u32 *pSiz
49cd0 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
49ce0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
49cf0 72 65 65 44 61 74 61 28 42 74 43 75 72 73 6f 72  reeData(BtCursor
49d00 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75  *, u32 offset, u
49d10 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a  32 amt, void*);.
49d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
49d30 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
49d40 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42  SetCachedRowid(B
49d50 74 43 75 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65  tCursor*, sqlite
49d60 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45  3_int64);.SQLITE
49d70 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
49d80 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 42 74  _int64 sqlite3Bt
49d90 72 65 65 47 65 74 43 61 63 68 65 64 52 6f 77 69  reeGetCachedRowi
49da0 64 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 0a 53  d(BtCursor*);..S
49db0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
49dc0 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
49dd0 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 42  IntegrityCheck(B
49de0 74 72 65 65 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f  tree*, int *aRoo
49df0 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e  t, int nRoot, in
49e00 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  t, int*);.SQLITE
49e10 5f 50 52 49 56 41 54 45 20 73 74 72 75 63 74 20  _PRIVATE struct 
49e20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33 42 74  Pager *sqlite3Bt
49e30 72 65 65 50 61 67 65 72 28 42 74 72 65 65 2a 29  reePager(Btree*)
49e40 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
49e50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
49e60 65 65 50 75 74 44 61 74 61 28 42 74 43 75 72 73  eePutData(BtCurs
49e70 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c  or*, u32 offset,
49e80 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29   u32 amt, void*)
49e90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
49ea0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
49eb0 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28  eeCacheOverflow(
49ec0 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a 53 51 4c  BtCursor *);.SQL
49ed0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
49ee0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
49ef0 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f  arCursor(BtCurso
49f00 72 20 2a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  r *);..#ifndef S
49f10 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45  QLITE_OMIT_BTREE
49f20 43 4f 55 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49  COUNT.SQLITE_PRI
49f30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
49f40 42 74 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72  BtreeCount(BtCur
49f50 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 23  sor *, i64 *);.#
49f60 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
49f70 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
49f80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
49f90 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
49fa0 6e 66 6f 28 42 74 43 75 72 73 6f 72 2a 2c 20 69  nfo(BtCursor*, i
49fb0 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  nt*, int);.SQLIT
49fc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
49fd0 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
49fe0 72 4c 69 73 74 28 42 74 72 65 65 2a 29 3b 0a 23  rList(Btree*);.#
49ff0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
4a000 77 65 20 61 72 65 20 6e 6f 74 20 75 73 69 6e 67  we are not using
4a010 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74   shared cache, t
4a020 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20  hen there is no 
4a030 6e 65 65 64 20 74 6f 0a 2a 2a 20 75 73 65 20 6d  need to.** use m
4a040 75 74 65 78 65 73 20 74 6f 20 61 63 63 65 73 73  utexes to access
4a050 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74   the BtShared st
4a060 72 75 63 74 75 72 65 73 2e 20 20 53 6f 20 6d 61  ructures.  So ma
4a070 6b 65 20 74 68 65 0a 2a 2a 20 45 6e 74 65 72 20  ke the.** Enter 
4a080 61 6e 64 20 4c 65 61 76 65 20 70 72 6f 63 65 64  and Leave proced
4a090 75 72 65 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a  ures no-ops..*/.
4a0a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
4a0b0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
4a0c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4a0d0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74    void sqlite3Bt
4a0e0 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 2a 29  reeEnter(Btree*)
4a0f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4a100 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
4a110 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c  treeEnterAll(sql
4a120 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20  ite3*);.#else.# 
4a130 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
4a140 72 65 65 45 6e 74 65 72 28 58 29 20 0a 23 20 64  reeEnter(X) .# d
4a150 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72  efine sqlite3Btr
4a160 65 65 45 6e 74 65 72 41 6c 6c 28 58 29 0a 23 65  eeEnterAll(X).#e
4a170 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e  ndif..#if !defin
4a180 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
4a190 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20  HARED_CACHE) && 
4a1a0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
4a1b0 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  E.SQLITE_PRIVATE
4a1c0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
4a1d0 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 2a  treeLeave(Btree*
4a1e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4a1f0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
4a200 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72  BtreeEnterCursor
4a210 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c  (BtCursor*);.SQL
4a220 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4a230 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  id sqlite3BtreeL
4a240 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 72  eaveCursor(BtCur
4a250 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sor*);.SQLITE_PR
4a260 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
4a270 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
4a280 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  l(sqlite3*);.SQL
4a290 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4a2a0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
4a2b0 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 42  utexArrayEnter(B
4a2c0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29  treeMutexArray*)
4a2d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4a2e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
4a2f0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65  treeMutexArrayLe
4a300 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72  ave(BtreeMutexAr
4a310 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ray*);.SQLITE_PR
4a320 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
4a330 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
4a340 72 61 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d  rayInsert(BtreeM
4a350 75 74 65 78 41 72 72 61 79 2a 2c 20 42 74 72 65  utexArray*, Btre
4a360 65 2a 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  e*);.#ifndef NDE
4a370 42 55 47 0a 20 20 2f 2a 20 54 68 65 73 65 20 72  BUG.  /* These r
4a380 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
4a390 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
4a3a0 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79   statements only
4a3b0 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  . */.SQLITE_PRIV
4a3c0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
4a3d0 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
4a3e0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
4a3f0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
4a400 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
4a410 41 6c 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 74  AllMutexes(sqlit
4a420 65 33 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 65 6c  e3*);.#endif.#el
4a430 73 65 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  se..# define sql
4a440 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 58  ite3BtreeLeave(X
4a450 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
4a460 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73  e3BtreeEnterCurs
4a470 6f 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  or(X).# define s
4a480 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
4a490 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 69  Cursor(X).# defi
4a4a0 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  ne sqlite3BtreeL
4a4b0 65 61 76 65 41 6c 6c 28 58 29 0a 23 20 64 65 66  eaveAll(X).# def
4a4c0 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
4a4d0 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28  MutexArrayEnter(
4a4e0 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  X).# define sqli
4a4f0 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
4a500 61 79 4c 65 61 76 65 28 58 29 0a 23 20 64 65 66  ayLeave(X).# def
4a510 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
4a520 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74  MutexArrayInsert
4a530 28 58 2c 59 29 0a 0a 23 20 64 65 66 69 6e 65 20  (X,Y)..# define 
4a540 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
4a550 73 4d 75 74 65 78 28 58 29 20 31 0a 23 20 64 65  sMutex(X) 1.# de
4a560 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65  fine sqlite3Btre
4a570 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
4a580 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 23  (X) 1.#endif...#
4a590 65 6e 64 69 66 20 2f 2a 20 5f 42 54 52 45 45 5f  endif /* _BTREE_
4a5a0 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
4a5b0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74  ****** End of bt
4a5c0 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ree.h **********
4a5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a5f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
4a600 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
4a610 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
4a620 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  off in sqliteInt
4a630 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
4a640 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
4a650 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76  ****** Include v
4a660 64 62 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  dbe.h in the mid
4a670 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
4a680 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
4a690 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
4a6a0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
4a6b0 65 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  e vdbe.h *******
4a6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a6e0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
4a6f0 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
4a700 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
4a710 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
4a720 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
4a730 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
4a740 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
4a750 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
4a760 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
4a770 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
4a780 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
4a790 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
4a7a0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
4a7b0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
4a7c0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
4a7d0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
4a7e0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
4a7f0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
4a800 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
4a810 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
4a820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
4a860 20 48 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72   Header file for
4a870 20 74 68 65 20 56 69 72 74 75 61 6c 20 44 61 74   the Virtual Dat
4a880 61 42 61 73 65 20 45 6e 67 69 6e 65 20 28 56 44  aBase Engine (VD
4a890 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68  BE).**.** This h
4a8a0 65 61 64 65 72 20 64 65 66 69 6e 65 73 20 74 68  eader defines th
4a8b0 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
4a8c0 68 65 20 76 69 72 74 75 61 6c 20 64 61 74 61 62  he virtual datab
4a8d0 61 73 65 20 65 6e 67 69 6e 65 0a 2a 2a 20 6f 72  ase engine.** or
4a8e0 20 56 44 42 45 2e 20 20 54 68 65 20 56 44 42 45   VDBE.  The VDBE
4a8f0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 61   implements an a
4a900 62 73 74 72 61 63 74 20 6d 61 63 68 69 6e 65 20  bstract machine 
4a910 74 68 61 74 20 72 75 6e 73 20 61 0a 2a 2a 20 73  that runs a.** s
4a920 69 6d 70 6c 65 20 70 72 6f 67 72 61 6d 20 74 6f  imple program to
4a930 20 61 63 63 65 73 73 20 61 6e 64 20 6d 6f 64 69   access and modi
4a940 66 79 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  fy the underlyin
4a950 67 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  g database..**.*
4a960 2a 20 24 49 64 3a 20 76 64 62 65 2e 68 2c 76 20  * $Id: vdbe.h,v 
4a970 31 2e 31 34 31 20 32 30 30 39 2f 30 34 2f 31 30  1.141 2009/04/10
4a980 20 30 30 3a 35 36 3a 32 39 20 64 72 68 20 45 78   00:56:29 drh Ex
4a990 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  p $.*/.#ifndef _
4a9a0 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 5f 0a 23  SQLITE_VDBE_H_.#
4a9b0 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 56  define _SQLITE_V
4a9c0 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20  DBE_H_../*.** A 
4a9d0 73 69 6e 67 6c 65 20 56 44 42 45 20 69 73 20 61  single VDBE is a
4a9e0 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75  n opaque structu
4a9f0 72 65 20 6e 61 6d 65 64 20 22 56 64 62 65 22 2e  re named "Vdbe".
4aa00 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 0a    Only routines.
4aa10 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65  ** in the source
4aa20 20 66 69 6c 65 20 73 71 6c 69 74 65 56 64 62 65   file sqliteVdbe
4aa30 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74  .c are allowed t
4aa40 6f 20 73 65 65 20 74 68 65 20 69 6e 73 69 64 65  o see the inside
4aa50 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72  s.** of this str
4aa60 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64  ucture..*/.typed
4aa70 65 66 20 73 74 72 75 63 74 20 56 64 62 65 20 56  ef struct Vdbe V
4aa80 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  dbe;../*.** The 
4aa90 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 6c  names of the fol
4aaa0 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 64 65 63  lowing types dec
4aab0 6c 61 72 65 64 20 69 6e 20 76 64 62 65 49 6e 74  lared in vdbeInt
4aac0 2e 68 20 61 72 65 20 72 65 71 75 69 72 65 64 0a  .h are required.
4aad0 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 62 65 4f  ** for the VdbeO
4aae0 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f  p definition..*/
4aaf0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4ab00 56 64 62 65 46 75 6e 63 20 56 64 62 65 46 75 6e  VdbeFunc VdbeFun
4ab10 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  c;.typedef struc
4ab20 74 20 4d 65 6d 20 4d 65 6d 3b 0a 0a 2f 2a 0a 2a  t Mem Mem;../*.*
4ab30 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 73 74 72  * A single instr
4ab40 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 69  uction of the vi
4ab50 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61  rtual machine ha
4ab60 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a 2a 20 61  s an opcode.** a
4ab70 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 20 74 68  nd as many as th
4ab80 72 65 65 20 6f 70 65 72 61 6e 64 73 2e 20 20 54  ree operands.  T
4ab90 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  he instruction i
4aba0 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a 20 61 73  s recorded.** as
4abb0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
4abc0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
4abd0 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 74 72 75  ructure:.*/.stru
4abe0 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 20 75 38  ct VdbeOp {.  u8
4abf0 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 20 20   opcode;        
4ac00 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 61 74    /* What operat
4ac10 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a  ion to perform *
4ac20 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20  /.  signed char 
4ac30 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e 65 20 6f  p4type; /* One o
4ac40 66 20 74 68 65 20 50 34 5f 78 78 78 20 63 6f 6e  f the P4_xxx con
4ac50 73 74 61 6e 74 73 20 66 6f 72 20 70 34 20 2a 2f  stants for p4 */
4ac60 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 3b 20 20  .  u8 opflags;  
4ac70 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 63 75         /* Not cu
4ac80 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a  rrently used */.
4ac90 20 20 75 38 20 70 35 3b 20 20 20 20 20 20 20 20    u8 p5;        
4aca0 20 20 20 20 20 20 2f 2a 20 46 69 66 74 68 20 70        /* Fifth p
4acb0 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 75  arameter is an u
4acc0 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65  nsigned characte
4acd0 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20  r */.  int p1;  
4ace0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
4acf0 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  rst operand */. 
4ad00 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20   int p2;        
4ad10 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70       /* Second p
4ad20 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20  arameter (often 
4ad30 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61  the jump destina
4ad40 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e 74 20 70  tion) */.  int p
4ad50 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  3;             /
4ad60 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61  * The third para
4ad70 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e 69 6f 6e  meter */.  union
4ad80 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
4ad90 2a 20 66 6f 72 74 68 20 70 61 72 61 6d 65 74 65  * forth paramete
4ada0 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 20  r */.    int i; 
4adb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4adc0 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65  /* Integer value
4add0 20 69 66 20 70 34 74 79 70 65 3d 3d 50 34 5f 49   if p4type==P4_I
4ade0 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76 6f 69 64  NT32 */.    void
4adf0 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   *p;            
4ae00 20 20 20 2f 2a 20 47 65 6e 65 72 69 63 20 70 6f     /* Generic po
4ae10 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 63 68 61  inter */.    cha
4ae20 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20  r *z;           
4ae30 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
4ae40 6f 20 64 61 74 61 20 66 6f 72 20 73 74 72 69 6e  o data for strin
4ae50 67 20 28 63 68 61 72 20 61 72 72 61 79 29 20 74  g (char array) t
4ae60 79 70 65 73 20 2a 2f 0a 20 20 20 20 69 36 34 20  ypes */.    i64 
4ae70 2a 70 49 36 34 3b 20 20 20 20 20 20 20 20 20 20  *pI64;          
4ae80 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20     /* Used when 
4ae90 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54  p4type is P4_INT
4aea0 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65  64 */.    double
4aeb0 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20   *pReal;        
4aec0 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
4aed0 74 79 70 65 20 69 73 20 50 34 5f 52 45 41 4c 20  type is P4_REAL 
4aee0 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a  */.    FuncDef *
4aef0 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 2f 2a  pFunc;        /*
4af00 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
4af10 65 20 69 73 20 50 34 5f 46 55 4e 43 44 45 46 20  e is P4_FUNCDEF 
4af20 2a 2f 0a 20 20 20 20 56 64 62 65 46 75 6e 63 20  */.    VdbeFunc 
4af30 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 20 2f 2a  *pVdbeFunc;   /*
4af40 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
4af50 65 20 69 73 20 50 34 5f 56 44 42 45 46 55 4e 43  e is P4_VDBEFUNC
4af60 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20   */.    CollSeq 
4af70 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f  *pColl;        /
4af80 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
4af90 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c 53 45 51  pe is P4_COLLSEQ
4afa0 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65   */.    Mem *pMe
4afb0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  m;             /
4afc0 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
4afd0 70 65 20 69 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a  pe is P4_MEM */.
4afe0 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
4aff0 20 2a 70 56 74 61 62 3b 20 20 20 2f 2a 20 55 73   *pVtab;   /* Us
4b000 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4b010 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a 20 20 20  s P4_VTAB */.   
4b020 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
4b030 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20  fo;     /* Used 
4b040 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
4b050 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a 20 20 20  4_KEYINFO */.   
4b060 20 69 6e 74 20 2a 61 69 3b 20 20 20 20 20 20 20   int *ai;       
4b070 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
4b080 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
4b090 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f 0a 20 20  4_INTARRAY */.  
4b0a0 7d 20 70 34 3b 0a 23 69 66 64 65 66 20 53 51 4c  } p4;.#ifdef SQL
4b0b0 49 54 45 5f 44 45 42 55 47 0a 20 20 63 68 61 72  ITE_DEBUG.  char
4b0c0 20 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20   *zComment;     
4b0d0 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20       /* Comment 
4b0e0 74 6f 20 69 6d 70 72 6f 76 65 20 72 65 61 64 61  to improve reada
4b0f0 62 69 6c 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66  bility */.#endif
4b100 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
4b110 46 49 4c 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20  FILE.  int cnt; 
4b120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b130 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d  /* Number of tim
4b140 65 73 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  es this instruct
4b150 69 6f 6e 20 77 61 73 20 65 78 65 63 75 74 65 64  ion was executed
4b160 20 2a 2f 0a 20 20 75 36 34 20 63 79 63 6c 65 73   */.  u64 cycles
4b170 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
4b180 2a 20 54 6f 74 61 6c 20 74 69 6d 65 20 73 70 65  * Total time spe
4b190 6e 74 20 65 78 65 63 75 74 69 6e 67 20 74 68 69  nt executing thi
4b1a0 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f  s instruction */
4b1b0 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64  .#endif.};.typed
4b1c0 65 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70  ef struct VdbeOp
4b1d0 20 56 64 62 65 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20   VdbeOp;../*.** 
4b1e0 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 73 69 6f  A smaller versio
4b1f0 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 73 65 64  n of VdbeOp used
4b200 20 66 6f 72 20 74 68 65 20 56 64 62 65 41 64 64   for the VdbeAdd
4b210 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 74 69 6f  OpList() functio
4b220 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 74 20  n because.** it 
4b230 74 61 6b 65 73 20 75 70 20 6c 65 73 73 20 73 70  takes up less sp
4b240 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56  ace..*/.struct V
4b250 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 20 75 38  dbeOpList {.  u8
4b260 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 20 20   opcode;        
4b270 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 61 74    /* What operat
4b280 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a  ion to perform *
4b290 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20  /.  signed char 
4b2a0 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 72 73 74  p1;     /* First
4b2b0 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 73 69   operand */.  si
4b2c0 67 6e 65 64 20 63 68 61 72 20 70 32 3b 20 20 20  gned char p2;   
4b2d0 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61    /* Second para
4b2e0 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65  meter (often the
4b2f0 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f   jump destinatio
4b300 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63  n) */.  signed c
4b310 68 61 72 20 70 33 3b 20 20 20 20 20 2f 2a 20 54  har p3;     /* T
4b320 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a  hird parameter *
4b330 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72  /.};.typedef str
4b340 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 56  uct VdbeOpList V
4b350 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a  dbeOpList;../*.*
4b360 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
4b370 20 6f 66 20 56 64 62 65 4f 70 2e 70 33 74 79 70   of VdbeOp.p3typ
4b380 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e.*/.#define P4_
4b390 4e 4f 54 55 53 45 44 20 20 20 20 30 20 20 20 2f  NOTUSED    0   /
4b3a0 2a 20 54 68 65 20 50 34 20 70 61 72 61 6d 65 74  * The P4 paramet
4b3b0 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 20 2a  er is not used *
4b3c0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 44 59 4e  /.#define P4_DYN
4b3d0 41 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a 20 50  AMIC  (-1)  /* P
4b3e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69  ointer to a stri
4b3f0 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ng obtained from
4b400 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
4b410 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 54  */.#define P4_ST
4b420 41 54 49 43 20 20 20 28 2d 32 29 20 20 2f 2a 20  ATIC   (-2)  /* 
4b430 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  Pointer to a sta
4b440 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64  tic string */.#d
4b450 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c 53 45 51  efine P4_COLLSEQ
4b460 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 20 69 73    (-4)  /* P4 is
4b470 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
4b480 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 75 72  CollSeq structur
4b490 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
4b4a0 46 55 4e 43 44 45 46 20 20 28 2d 35 29 20 20 2f  FUNCDEF  (-5)  /
4b4b0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
4b4c0 72 20 74 6f 20 61 20 46 75 6e 63 44 65 66 20 73  r to a FuncDef s
4b4d0 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66  tructure */.#def
4b4e0 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 20  ine P4_KEYINFO  
4b4f0 28 2d 36 29 20 20 2f 2a 20 50 34 20 69 73 20 61  (-6)  /* P4 is a
4b500 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65   pointer to a Ke
4b510 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
4b520 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56 44  */.#define P4_VD
4b530 42 45 46 55 4e 43 20 28 2d 37 29 20 20 2f 2a 20  BEFUNC (-7)  /* 
4b540 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
4b550 74 6f 20 61 20 56 64 62 65 46 75 6e 63 20 73 74  to a VdbeFunc st
4b560 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69  ructure */.#defi
4b570 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20 20 20 28  ne P4_MEM      (
4b580 2d 38 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20  -8)  /* P4 is a 
4b590 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d  pointer to a Mem
4b5a0 2a 20 20 20 20 73 74 72 75 63 74 75 72 65 20 2a  *    structure *
4b5b0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 54 52 41  /.#define P4_TRA
4b5c0 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f 2a 20 50  NSIENT (-9) /* P
4b5d0 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
4b5e0 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 73 74  o a transient st
4b5f0 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ring */.#define 
4b600 50 34 5f 56 54 41 42 20 20 20 20 20 28 2d 31 30  P4_VTAB     (-10
4b610 29 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69  ) /* P4 is a poi
4b620 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74  nter to an sqlit
4b630 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72  e3_vtab structur
4b640 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
4b650 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 29 20 2f  MPRINTF  (-11) /
4b660 2a 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67  * P4 is a string
4b670 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
4b680 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
4b690 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 52   */.#define P4_R
4b6a0 45 41 4c 20 20 20 20 20 28 2d 31 32 29 20 2f 2a  EAL     (-12) /*
4b6b0 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20   P4 is a 64-bit 
4b6c0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
4b6d0 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  alue */.#define 
4b6e0 50 34 5f 49 4e 54 36 34 20 20 20 20 28 2d 31 33  P4_INT64    (-13
4b6f0 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d  ) /* P4 is a 64-
4b700 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
4b710 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  er */.#define P4
4b720 5f 49 4e 54 33 32 20 20 20 20 28 2d 31 34 29 20  _INT32    (-14) 
4b730 2f 2a 20 50 34 20 69 73 20 61 20 33 32 2d 62 69  /* P4 is a 32-bi
4b740 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
4b750 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49   */.#define P4_I
4b760 4e 54 41 52 52 41 59 20 28 2d 31 35 29 20 2f 2a  NTARRAY (-15) /*
4b770 20 50 34 20 69 73 20 61 20 76 65 63 74 6f 72 20   P4 is a vector 
4b780 6f 66 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65  of 32-bit intege
4b790 72 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61  rs */../* When a
4b7a0 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 75 6d  dding a P4 argum
4b7b0 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b 45 59  ent using P4_KEY
4b7c0 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66 20  INFO, a copy of 
4b7d0 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  the KeyInfo stru
4b7e0 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64 65  cture.** is made
4b7f0 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 73 20  .  That copy is 
4b800 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56  freed when the V
4b810 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  dbe is finalized
4b820 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a 2a 2a  .  But if the.**
4b830 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 34 5f   argument is P4_
4b840 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c  KEYINFO_HANDOFF,
4b850 20 74 68 65 20 70 61 73 73 65 64 20 69 6e 20 70   the passed in p
4b860 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 2e 20  ointer is used. 
4b870 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 74   It still.** get
4b880 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65  s freed when the
4b890 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a   Vdbe is finaliz
4b8a0 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c 20 73  ed so it still s
4b8b0 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 6e 65  hould be obtaine
4b8c0 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67  d.** from a sing
4b8d0 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  le sqliteMalloc(
4b8e0 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79 20  ).  But no copy 
4b8f0 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20  is made and the 
4b900 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74  calling.** funct
4b910 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a  ion should *not*
4b920 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68 65   try to free the
4b930 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65   KeyInfo..*/.#de
4b940 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  fine P4_KEYINFO_
4b950 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a 23 64  HANDOFF (-16).#d
4b960 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f  efine P4_KEYINFO
4b970 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 0a 0a  _STATIC  (-17)..
4b980 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e 61  /*.** The Vdbe.a
4b990 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 63 6f  ColName array co
4b9a0 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 73 74  ntains 5n Mem st
4b9b0 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 65 20  ructures, where 
4b9c0 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e 75 6d  n is the .** num
4b9d0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f  ber of columns o
4b9e0 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20  f data returned 
4b9f0 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  by the statement
4ba00 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c  ..*/.#define COL
4ba10 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 30 0a  NAME_NAME     0.
4ba20 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f  #define COLNAME_
4ba30 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 66 69  DECLTYPE 1.#defi
4ba40 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42  ne COLNAME_DATAB
4ba50 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 43 4f  ASE 2.#define CO
4ba60 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 20 33  LNAME_TABLE    3
4ba70 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  .#define COLNAME
4ba80 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 66 64  _COLUMN   4.#ifd
4ba90 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
4baa0 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
4bab0 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  .# define COLNAM
4bac0 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20 20  E_N        5    
4bad0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43    /* Number of C
4bae0 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f  OLNAME_xxx symbo
4baf0 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 66  ls */.#else.# if
4bb00 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
4bb10 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 65 66  DECLTYPE.#   def
4bb20 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20  ine COLNAME_N   
4bb30 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 74 6f     1      /* Sto
4bb40 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 6d 65  re only the name
4bb50 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 20 64   */.# else.#   d
4bb60 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
4bb70 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a 20 53       2      /* S
4bb80 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 61 6e  tore the name an
4bb90 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a 23 20  d decltype */.# 
4bba0 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a  endif.#endif../*
4bbb0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
4bbc0 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74 73  g macro converts
4bbd0 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 64 72   a relative addr
4bbe0 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 66 69  ess in the p2 fi
4bbf0 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65  eld.** of a Vdbe
4bc00 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 6e 74  Op structure int
4bc10 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  o a negative num
4bc20 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20  ber so that .** 
4bc30 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
4bc40 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68 61  List() knows tha
4bc50 74 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73  t the address is
4bc60 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c   relative.  Call
4bc70 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f  ing.** the macro
4bc80 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73 20   again restores 
4bc90 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a  the address..*/.
4bca0 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 29 20  #define ADDR(X) 
4bcb0 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a   (-1-(X))../*.**
4bcc0 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 63   The makefile sc
4bcd0 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20 73  ans the vdbe.c s
4bce0 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20 63  ource file and c
4bcf0 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 63 6f  reates the "opco
4bd00 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72  des.h".** header
4bd10 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e   file that defin
4bd20 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72 20  es a number for 
4bd30 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65 64  each opcode used
4bd40 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f   by the VDBE..*/
4bd50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4bd60 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65 73   Include opcodes
4bd70 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
4bd80 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a   of vdbe.h *****
4bd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4bda0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4bdb0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f   Begin file opco
4bdc0 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  des.h **********
4bdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4bdf0 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c  ./* Automaticall
4be00 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f  y generated.  Do
4be10 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20   not edit */./* 
4be20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  See the mkopcode
4be30 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72  h.awk script for
4be40 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66   details */.#def
4be50 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20  ine OP_VNext    
4be60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4be70 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64              1.#d
4be80 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74  efine OP_Affinit
4be90 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
4bea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a                2.
4beb0 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d  #define OP_Colum
4bec0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
4bed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bee0 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74  3.#define OP_Set
4bef0 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20  Cookie          
4bf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bf10 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53    4.#define OP_S
4bf20 65 65 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  eek             
4bf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bf40 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 4f 50      5.#define OP
4bf50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20  _Real           
4bf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bf70 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d      130   /* sam
4bf80 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20  e as TK_FLOAT   
4bf90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   */.#define OP_S
4bfa0 65 71 75 65 6e 63 65 20 20 20 20 20 20 20 20 20  equence         
4bfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bfc0 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50      6.#define OP
4bfd0 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20 20 20  _Savepoint      
4bfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bff0 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20        7.#define 
4c000 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20  OP_Ge           
4c010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c020 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a 20 73         78   /* s
4c030 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20  ame as TK_GE    
4c040 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4c050 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20  _RowKey         
4c060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c070 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20        8.#define 
4c080 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20 20 20  OP_SCopy        
4c090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c0a0 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e          9.#defin
4c0b0 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20 20 20  e OP_Eq         
4c0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c0d0 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a           74   /*
4c0e0 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 20 20   same as TK_EQ  
4c0f0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4c100 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 20 20  OP_OpenWrite    
4c110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c120 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e         10.#defin
4c130 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20  e OP_NotNull    
4c140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c150 20 20 20 20 20 20 20 20 20 37 32 20 20 20 2f 2a           72   /*
4c160 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e   same as TK_NOTN
4c170 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ULL  */.#define 
4c180 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20 20  OP_If           
4c190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c1a0 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e         11.#defin
4c1b0 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20  e OP_ToInt      
4c1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c1d0 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a          144   /*
4c1e0 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49   same as TK_TO_I
4c1f0 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NT   */.#define 
4c200 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20  OP_String8      
4c210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c220 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73         94   /* s
4c230 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47  ame as TK_STRING
4c240 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4c250 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20  _CollSeq        
4c260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c270 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20       12.#define 
4c280 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20  OP_OpenRead     
4c290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c2a0 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e         13.#defin
4c2b0 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20  e OP_Expire     
4c2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c2d0 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66           14.#def
4c2e0 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  ine OP_AutoCommi
4c2f0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4c300 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64             15.#d
4c310 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20  efine OP_Gt     
4c320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c330 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35 20               75 
4c340 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4c350 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  GT       */.#def
4c360 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74  ine OP_Pagecount
4c370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c380 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23 64             16.#d
4c390 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69  efine OP_Integri
4c3a0 74 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20  tyCk            
4c3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a               17.
4c3c0 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20  #define OP_Sort 
4c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4c3f0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 70  8.#define OP_Cop
4c400 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
4c410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c420 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54   20.#define OP_T
4c430 72 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20  race            
4c440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c450 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 4f 50     21.#define OP
4c460 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 20  _Function       
4c470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c480 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20       22.#define 
4c490 4f 50 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20  OP_IfNeg        
4c4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c4b0 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e         23.#defin
4c4c0 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 20 20  e OP_And        
4c4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c4e0 20 20 20 20 20 20 20 20 20 36 37 20 20 20 2f 2a           67   /*
4c4f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 20   same as TK_AND 
4c500 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4c510 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20 20 20  OP_Subtract     
4c520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c530 20 20 20 20 20 20 20 38 35 20 20 20 2f 2a 20 73         85   /* s
4c540 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20  ame as TK_MINUS 
4c550 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4c560 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20 20 20  _Noop           
4c570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c580 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20       24.#define 
4c590 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20 20 20  OP_Return       
4c5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c5b0 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e         25.#defin
4c5c0 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 20  e OP_Remainder  
4c5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c5e0 20 20 20 20 20 20 20 20 20 38 38 20 20 20 2f 2a           88   /*
4c5f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 20   same as TK_REM 
4c600 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4c610 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20 20 20  OP_NewRowid     
4c620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c630 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 69 6e         26.#defin
4c640 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 20 20 20  e OP_Multiply   
4c650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c660 20 20 20 20 20 20 20 20 20 38 36 20 20 20 2f 2a           86   /*
4c670 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 41 52   same as TK_STAR
4c680 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4c690 4f 50 5f 56 61 72 69 61 62 6c 65 20 20 20 20 20  OP_Variable     
4c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c6b0 20 20 20 20 20 20 20 32 37 0a 23 64 65 66 69 6e         27.#defin
4c6c0 65 20 4f 50 5f 53 74 72 69 6e 67 20 20 20 20 20  e OP_String     
4c6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c6e0 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66           28.#def
4c6f0 69 6e 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e  ine OP_RealAffin
4c700 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20 20  ity             
4c710 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64             29.#d
4c720 65 66 69 6e 65 20 4f 50 5f 56 52 65 6e 61 6d 65  efine OP_VRename
4c730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c740 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a               30.
4c750 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 72 73 65  #define OP_Parse
4c760 53 63 68 65 6d 61 20 20 20 20 20 20 20 20 20 20  Schema          
4c770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
4c780 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 4f 70  1.#define OP_VOp
4c790 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  en              
4c7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c7b0 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43   32.#define OP_C
4c7c0 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20 20  lose            
4c7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c7e0 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50     33.#define OP
4c7f0 5f 43 72 65 61 74 65 49 6e 64 65 78 20 20 20 20  _CreateIndex    
4c800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c810 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20       34.#define 
4c820 4f 50 5f 49 73 55 6e 69 71 75 65 20 20 20 20 20  OP_IsUnique     
4c830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c840 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e         35.#defin
4c850 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 20 20  e OP_NotFound   
4c860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c870 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66           36.#def
4c880 69 6e 65 20 4f 50 5f 49 6e 74 36 34 20 20 20 20  ine OP_Int64    
4c890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c8a0 20 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64             37.#d
4c8b0 65 66 69 6e 65 20 4f 50 5f 4d 75 73 74 42 65 49  efine OP_MustBeI
4c8c0 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
4c8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a               38.
4c8e0 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 20  #define OP_Halt 
4c8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
4c910 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77  9.#define OP_Row
4c920 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  id              
4c930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c940 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   40.#define OP_I
4c950 64 78 4c 54 20 20 20 20 20 20 20 20 20 20 20 20  dxLT            
4c960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c970 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50     41.#define OP
4c980 5f 41 64 64 49 6d 6d 20 20 20 20 20 20 20 20 20  _AddImm         
4c990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c9a0 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20       42.#define 
4c9b0 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 20 20 20  OP_Statement    
4c9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c9d0 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e         43.#defin
4c9e0 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20 20 20  e OP_RowData    
4c9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ca00 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66           44.#def
4ca10 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 20 20  ine OP_MemMax   
4ca20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ca30 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64             45.#d
4ca40 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20 20  efine OP_Or     
4ca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ca60 20 20 20 20 20 20 20 20 20 20 20 20 20 36 36 20               66 
4ca70 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4ca80 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  OR       */.#def
4ca90 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  ine OP_NotExists
4caa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cab0 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64             46.#d
4cac0 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62 20 20  efine OP_Gosub  
4cad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cae0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a               47.
4caf0 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76 69 64  #define OP_Divid
4cb00 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
4cb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
4cb20 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  7   /* same as T
4cb30 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a 23 64  K_SLASH    */.#d
4cb40 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 65 72  efine OP_Integer
4cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb60 20 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a               48.
4cb70 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d  #define OP_ToNum
4cb80 65 72 69 63 20 20 20 20 20 20 20 20 20 20 20 20  eric            
4cb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
4cba0 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  3   /* same as T
4cbb0 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23  K_TO_NUMERIC*/.#
4cbc0 64 65 66 69 6e 65 20 4f 50 5f 50 72 65 76 20 20  define OP_Prev  
4cbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39                49
4cbf0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53  .#define OP_RowS
4cc00 65 74 52 65 61 64 20 20 20 20 20 20 20 20 20 20  etRead          
4cc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cc20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f  50.#define OP_Co
4cc30 6e 63 61 74 20 20 20 20 20 20 20 20 20 20 20 20  ncat            
4cc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cc50 20 20 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61    89   /* same a
4cc60 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 2a 2f  s TK_CONCAT   */
4cc70 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53  .#define OP_RowS
4cc80 65 74 41 64 64 20 20 20 20 20 20 20 20 20 20 20  etAdd           
4cc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cca0 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69  51.#define OP_Bi
4ccb0 74 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20  tAnd            
4ccc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ccd0 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65 20 61    80   /* same a
4cce0 73 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 2a 2f  s TK_BITAND   */
4ccf0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c  .#define OP_VCol
4cd00 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  umn             
4cd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cd20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72  52.#define OP_Cr
4cd30 65 61 74 65 54 61 62 6c 65 20 20 20 20 20 20 20  eateTable       
4cd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cd50 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    53.#define OP_
4cd60 4c 61 73 74 20 20 20 20 20 20 20 20 20 20 20 20  Last            
4cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cd80 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 4f      54.#define O
4cd90 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 20 20  P_SeekLe        
4cda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cdb0 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65        55.#define
4cdc0 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20 20 20   OP_IsNull      
4cdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cde0 20 20 20 20 20 20 20 20 37 31 20 20 20 2f 2a 20          71   /* 
4cdf0 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c  same as TK_ISNUL
4ce00 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  L   */.#define O
4ce10 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20 20 20  P_IncrVacuum    
4ce20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce30 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65        56.#define
4ce40 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 20 20   OP_IdxRowid    
4ce50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce60 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 69          57.#defi
4ce70 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74  ne OP_ShiftRight
4ce80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce90 20 20 20 20 20 20 20 20 20 20 38 33 20 20 20 2f            83   /
4cea0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48  * same as TK_RSH
4ceb0 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  IFT   */.#define
4cec0 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 20 20   OP_ResetCount  
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cee0 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 66 69          58.#defi
4cef0 6e 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 75 73  ne OP_ContextPus
4cf00 68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h               
4cf10 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64 65            59.#de
4cf20 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20  fine OP_Yield   
4cf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cf40 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23              60.#
4cf50 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72  define OP_DropTr
4cf60 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20  igger           
4cf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 31                61
4cf80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70  .#define OP_Drop
4cf90 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20  Index           
4cfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cfb0 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64  62.#define OP_Id
4cfc0 78 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20  xGE             
4cfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cfe0 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    63.#define OP_
4cff0 49 64 78 44 65 6c 65 74 65 20 20 20 20 20 20 20  IdxDelete       
4d000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d010 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 4f      64.#define O
4d020 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20 20 20  P_Vacuum        
4d030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d040 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65        65.#define
4d050 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20 20   OP_IfNot       
4d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d070 20 20 20 20 20 20 20 20 36 38 0a 23 64 65 66 69          68.#defi
4d080 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 20  ne OP_DropTable 
4d090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d0a0 20 20 20 20 20 20 20 20 20 20 36 39 0a 23 64 65            69.#de
4d0b0 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 20 20  fine OP_SeekLt  
4d0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d0d0 20 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23              70.#
4d0e0 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65  define OP_MakeRe
4d0f0 63 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 20  cord            
4d100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39                79
4d110 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 42 6c  .#define OP_ToBl
4d120 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ob              
4d130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4d140 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  42   /* same as 
4d150 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23  TK_TO_BLOB  */.#
4d160 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75 6c 74  define OP_Result
4d170 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 20  Row             
4d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30                90
4d190 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c 65  .#define OP_Dele
4d1a0 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
4d1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d1c0 39 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 67  91.#define OP_Ag
4d1d0 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20 20 20  gFinal          
4d1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d1f0 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    92.#define OP_
4d200 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20 20 20  Compare         
4d210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d220 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 4f      95.#define O
4d230 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20 20 20  P_ShiftLeft     
4d240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d250 20 20 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61        82   /* sa
4d260 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 54 20  me as TK_LSHIFT 
4d270 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
4d280 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20 20 20  Goto            
4d290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d2a0 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 4f      96.#define O
4d2b0 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20  P_TableLock     
4d2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d2d0 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65        97.#define
4d2e0 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20 20 20   OP_Clear       
4d2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d300 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69          98.#defi
4d310 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 20 20  ne OP_Le        
4d320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d330 20 20 20 20 20 20 20 20 20 20 37 36 20 20 20 2f            76   /
4d340 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 20  * same as TK_LE 
4d350 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
4d360 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65   OP_VerifyCookie
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d380 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69          99.#defi
4d390 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20  ne OP_AggStep   
4d3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d3b0 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 65           100.#de
4d3c0 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74 20 20  fine OP_ToText  
4d3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d3e0 20 20 20 20 20 20 20 20 20 20 20 31 34 31 20 20             141  
4d3f0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
4d400 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65 66 69  O_TEXT  */.#defi
4d410 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20 20 20  ne OP_Not       
4d420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d430 20 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f            19   /
4d440 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54  * same as TK_NOT
4d450 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
4d460 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 20 20   OP_ToReal      
4d470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d480 20 20 20 20 20 20 20 31 34 35 20 20 20 2f 2a 20         145   /* 
4d490 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45  same as TK_TO_RE
4d4a0 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  AL  */.#define O
4d4b0 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20  P_SetNumColumns 
4d4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d4d0 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65       101.#define
4d4e0 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20   OP_Transaction 
4d4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d500 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69         102.#defi
4d510 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20  ne OP_VFilter   
4d520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d530 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 65           103.#de
4d540 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20  fine OP_Ne      
4d550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d560 20 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20              73  
4d570 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
4d580 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  E       */.#defi
4d590 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20  ne OP_VDestroy  
4d5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d5b0 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65           104.#de
4d5c0 66 69 6e 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50  fine OP_ContextP
4d5d0 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  op              
4d5e0 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23             105.#
4d5f0 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 20  define OP_BitOr 
4d600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31                81
4d620 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4d630 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 65  _BITOR    */.#de
4d640 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20  fine OP_Next    
4d650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d660 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23             106.#
4d670 64 65 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20  define OP_Count 
4d680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d690 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37               107
4d6a0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 49  .#define OP_IdxI
4d6b0 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20  nsert           
4d6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4d6d0 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74  08.#define OP_Lt
4d6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d700 20 20 37 37 20 20 20 2f 2a 20 73 61 6d 65 20 61    77   /* same a
4d710 73 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f  s TK_LT       */
4d720 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b  .#define OP_Seek
4d730 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Ge              
4d740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4d750 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e  09.#define OP_In
4d760 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20 20  sert            
4d770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d780 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f   110.#define OP_
4d790 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20 20  Destroy         
4d7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d7b0 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f     111.#define O
4d7c0 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20  P_ReadCookie    
4d7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d7e0 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65       112.#define
4d7f0 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20   OP_RowSetTest  
4d800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d810 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69         113.#defi
4d820 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73  ne OP_LoadAnalys
4d830 69 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  is              
4d840 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65           114.#de
4d850 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e 20  fine OP_Explain 
4d860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d870 20 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23             115.#
4d880 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49 66  define OP_HaltIf
4d890 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  Null            
4d8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36               116
4d8b0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e  .#define OP_Open
4d8c0 50 73 65 75 64 6f 20 20 20 20 20 20 20 20 20 20  Pseudo          
4d8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4d8e0 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70  17.#define OP_Op
4d8f0 65 6e 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20  enEphemeral     
4d900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d910 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f   118.#define OP_
4d920 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  Null            
4d930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d940 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 20 4f     119.#define O
4d950 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20  P_Move          
4d960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d970 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65       120.#define
4d980 20 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20   OP_Blob        
4d990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d9a0 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69         121.#defi
4d9b0 6e 65 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20  ne OP_Add       
4d9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d9d0 20 20 20 20 20 20 20 20 20 20 38 34 20 20 20 2f            84   /
4d9e0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55  * same as TK_PLU
4d9f0 53 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  S     */.#define
4da00 20 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20   OP_Rewind      
4da10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4da20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69         122.#defi
4da30 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 20 20  ne OP_SeekGt    
4da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4da50 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65           123.#de
4da60 66 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20 20  fine OP_VBegin  
4da70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4da80 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23             124.#
4da90 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74  define OP_VUpdat
4daa0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
4dab0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35               125
4dac0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65  .#define OP_IfZe
4dad0 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ro              
4dae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4daf0 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69  26.#define OP_Bi
4db00 74 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20  tNot            
4db10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4db20 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 65 20 61    93   /* same a
4db30 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f  s TK_BITNOT   */
4db40 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65  .#define OP_VCre
4db50 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ate             
4db60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4db70 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f  27.#define OP_Fo
4db80 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  und             
4db90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dba0 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f   128.#define OP_
4dbb0 49 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20  IfPos           
4dbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dbd0 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 4f     129.#define O
4dbe0 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20  P_NullRow       
4dbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dc00 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65       131.#define
4dc10 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20   OP_Jump        
4dc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dc30 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 66 69         132.#defi
4dc40 6e 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f  ne OP_Permutatio
4dc50 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
4dc60 20 20 20 20 20 20 20 20 20 31 33 33 0a 0a 2f 2a           133../*
4dc70 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f   The following o
4dc80 70 63 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65  pcode values are
4dc90 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23   never used */.#
4dca0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65  define OP_NotUse
4dcb0 64 5f 31 33 34 20 20 20 20 20 20 20 20 20 20 20  d_134           
4dcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34               134
4dcd0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55  .#define OP_NotU
4dce0 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 20 20  sed_135         
4dcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4dd00 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  35.#define OP_No
4dd10 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 20 20  tUsed_136       
4dd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dd30 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f   136.#define OP_
4dd40 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 20 20  NotUsed_137     
4dd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dd60 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 4f     137.#define O
4dd70 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 20 20  P_NotUsed_138   
4dd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dd90 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65       138.#define
4dda0 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 39 20   OP_NotUsed_139 
4ddb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ddc0 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69         139.#defi
4ddd0 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 34  ne OP_NotUsed_14
4dde0 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
4ddf0 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 0a 2f           140.../
4de00 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 75 63  * Properties suc
4de10 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 20 22  h as "out2" or "
4de20 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 20 73  jump" that are s
4de30 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 63  pecified in.** c
4de40 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e  omments followin
4de50 67 20 74 68 65 20 22 63 61 73 65 22 20 66 6f 72  g the "case" for
4de60 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 6e 20   each opcode in 
4de70 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 61 72  the vdbe.c.** ar
4de80 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f 20 62  e encoded into b
4de90 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 6f 6c  itvectors as fol
4dea0 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65  lows:.*/.#define
4deb0 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 20 20   OPFLG_JUMP     
4dec0 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f         0x0001  /
4ded0 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f 6c 64  * jump:  P2 hold
4dee0 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a 2f 0a  s jmp target */.
4def0 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55  #define OPFLG_OU
4df00 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 30 78  T2_PRERELEASE 0x
4df10 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 70 72  0002  /* out2-pr
4df20 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 64 65  erelease: */.#de
4df30 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 20 20  fine OPFLG_IN1  
4df40 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30             0x000
4df50 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 31 20  4  /* in1:   P1 
4df60 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23  is an input */.#
4df70 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 32  define OPFLG_IN2
4df80 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
4df90 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 20 50  008  /* in2:   P
4dfa0 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f  2 is an input */
4dfb0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49  .#define OPFLG_I
4dfc0 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 20 30  N3             0
4dfd0 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 20 20  x0010  /* in3:  
4dfe0 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 74 20   P3 is an input 
4dff0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47  */.#define OPFLG
4e000 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20 20 20  _OUT3           
4e010 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 74 33   0x0020  /* out3
4e020 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75 74 70  :  P3 is an outp
4e030 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  ut */.#define OP
4e040 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20  FLG_INITIALIZER 
4e050 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 78 30  {\./*   0 */ 0x0
4e060 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30  0, 0x01, 0x00, 0
4e070 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 38 2c  x00, 0x10, 0x08,
4e080 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a   0x02, 0x00,\./*
4e090 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20 30 78     8 */ 0x00, 0x
4e0a0 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20  04, 0x00, 0x05, 
4e0b0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
4e0c0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31 36 20  , 0x00,\./*  16 
4e0d0 2a 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  */ 0x02, 0x00, 0
4e0e0 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30 34 2c  x01, 0x04, 0x04,
4e0f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
4e100 35 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 30 78  5,\./*  24 */ 0x
4e110 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 2c 20  00, 0x04, 0x02, 
4e120 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 34  0x00, 0x02, 0x04
4e130 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f  , 0x00, 0x00,\./
4e140 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c 20 30  *  32 */ 0x00, 0
4e150 78 30 30 2c 20 30 78 30 32 2c 20 30 78 31 31 2c  x00, 0x02, 0x11,
4e160 20 30 78 31 31 2c 20 30 78 30 32 2c 20 30 78 30   0x11, 0x02, 0x0
4e170 35 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 34 30  5, 0x00,\./*  40
4e180 20 2a 2f 20 30 78 30 32 2c 20 30 78 31 31 2c 20   */ 0x02, 0x11, 
4e190 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30 30  0x04, 0x00, 0x00
4e1a0 2c 20 30 78 30 63 2c 20 30 78 31 31 2c 20 30 78  , 0x0c, 0x11, 0x
4e1b0 30 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 20 30  01,\./*  48 */ 0
4e1c0 78 30 32 2c 20 30 78 30 31 2c 20 30 78 32 31 2c  x02, 0x01, 0x21,
4e1d0 20 30 78 30 38 2c 20 30 78 30 30 2c 20 30 78 30   0x08, 0x00, 0x0
4e1e0 32 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 5c 0a  2, 0x01, 0x11,\.
4e1f0 2f 2a 20 20 35 36 20 2a 2f 20 30 78 30 31 2c 20  /*  56 */ 0x01, 
4e200 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30  0x02, 0x00, 0x00
4e210 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78  , 0x04, 0x00, 0x
4e220 30 30 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 20 36  00, 0x11,\./*  6
4e230 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c  4 */ 0x00, 0x00,
4e240 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 30   0x2c, 0x2c, 0x0
4e250 35 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30  5, 0x00, 0x11, 0
4e260 78 30 35 2c 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20  x05,\./*  72 */ 
4e270 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 31 35  0x05, 0x15, 0x15
4e280 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78  , 0x15, 0x15, 0x
4e290 31 35 2c 20 30 78 31 35 2c 20 30 78 30 30 2c 5c  15, 0x15, 0x00,\
4e2a0 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 32 63 2c  ./*  80 */ 0x2c,
4e2b0 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32   0x2c, 0x2c, 0x2
4e2c0 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  c, 0x2c, 0x2c, 0
4e2d0 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a 20 20  x2c, 0x2c,\./*  
4e2e0 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78 32 63  88 */ 0x2c, 0x2c
4e2f0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
4e300 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 2c 20  00, 0x04, 0x02, 
4e310 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f  0x00,\./*  96 */
4e320 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30   0x01, 0x00, 0x0
4e330 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
4e340 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 31 2c  x00, 0x00, 0x01,
4e350 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 30 30  \./* 104 */ 0x00
4e360 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78  , 0x00, 0x01, 0x
4e370 30 32 2c 20 30 78 30 38 2c 20 30 78 31 31 2c 20  02, 0x08, 0x11, 
4e380 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20  0x00, 0x02,\./* 
4e390 31 31 32 20 2a 2f 20 30 78 30 32 2c 20 30 78 31  112 */ 0x02, 0x1
4e3a0 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  5, 0x00, 0x00, 0
4e3b0 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x10, 0x00, 0x00,
4e3c0 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30 20 2a   0x02,\./* 120 *
4e3d0 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78  / 0x00, 0x02, 0x
4e3e0 30 31 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20  01, 0x11, 0x00, 
4e3f0 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 30  0x00, 0x05, 0x00
4e400 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 78 31  ,\./* 128 */ 0x1
4e410 31 2c 20 30 78 30 35 2c 20 30 78 30 32 2c 20 30  1, 0x05, 0x02, 0
4e420 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c  x00, 0x01, 0x00,
4e430 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a   0x00, 0x00,\./*
4e440 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 30 78   136 */ 0x00, 0x
4e450 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
4e460 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34  0x00, 0x04, 0x04
4e470 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 34 20  , 0x04,\./* 144 
4e480 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 7d 0a  */ 0x04, 0x04,}.
4e490 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4e4a0 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e   End of opcodes.
4e4b0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4e4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4e4e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4e4f0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
4e500 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
4e510 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.h ********
4e520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4e530 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79 70  ../*.** Prototyp
4e540 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 45 20  es for the VDBE 
4e550 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 65 20  interface.  See 
4e560 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20  comments on the 
4e570 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
4e580 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74  * for a descript
4e590 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 63 68  ion of what each
4e5a0 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
4e5b0 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49  es does..*/.SQLI
4e5c0 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20  TE_PRIVATE Vdbe 
4e5d0 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61  *sqlite3VdbeCrea
4e5e0 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  te(sqlite3*);.SQ
4e5f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4e600 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
4e610 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53  p0(Vdbe*,int);.S
4e620 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4e630 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
4e640 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  Op1(Vdbe*,int,in
4e650 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4e660 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
4e670 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a 2c 69  beAddOp2(Vdbe*,i
4e680 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  nt,int,int);.SQL
4e690 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4e6a0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
4e6b0 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c  3(Vdbe*,int,int,
4e6c0 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
4e6d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4e6e0 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 56  ite3VdbeAddOp4(V
4e6f0 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  dbe*,int,int,int
4e700 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 20  ,int,const char 
4e710 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  *zP4,int);.SQLIT
4e720 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4e730 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
4e740 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 6e 4f  st(Vdbe*, int nO
4e750 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f  p, VdbeOpList co
4e760 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c 49 54  nst *aOp);.SQLIT
4e770 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4e780 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
4e790 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64  P1(Vdbe*, int ad
4e7a0 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 51 4c  dr, int P1);.SQL
4e7b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4e7c0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
4e7d0 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e 74 20  geP2(Vdbe*, int 
4e7e0 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b 0a 53  addr, int P2);.S
4e7f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4e800 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
4e810 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 69 6e  angeP3(Vdbe*, in
4e820 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33 29 3b  t addr, int P3);
4e830 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4e840 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
4e850 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a 2c 20  ChangeP5(Vdbe*, 
4e860 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45 5f 50  u8 P5);.SQLITE_P
4e870 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
4e880 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
4e890 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 29  Vdbe*, int addr)
4e8a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4e8b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
4e8c0 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64  eChangeToNoop(Vd
4e8d0 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69  be*, int addr, i
4e8e0 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt N);.SQLITE_PR
4e8f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4e900 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56  e3VdbeChangeP4(V
4e910 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20  dbe*, int addr, 
4e920 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c  const char *zP4,
4e930 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
4e940 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4e950 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
4e960 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53  e(Vdbe*, int);.S
4e970 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
4e980 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62  beOp *sqlite3Vdb
4e990 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 69 6e  eGetOp(Vdbe*, in
4e9a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4e9b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
4e9c0 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65  beMakeLabel(Vdbe
4e9d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4e9e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
4e9f0 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 2a 29  dbeDelete(Vdbe*)
4ea00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4ea10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
4ea20 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62 65 2a  eMakeReady(Vdbe*
4ea30 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
4ea40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4ea50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
4ea60 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 2a 29  eFinalize(Vdbe*)
4ea70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4ea80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
4ea90 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64  eResolveLabel(Vd
4eaa0 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  be*, int);.SQLIT
4eab0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4eac0 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
4ead0 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 69 66  Addr(Vdbe*);.#if
4eae0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
4eaf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4eb00 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
4eb10 62 65 54 72 61 63 65 28 56 64 62 65 2a 2c 46 49  beTrace(Vdbe*,FI
4eb20 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c  LE*);.#endif.SQL
4eb30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4eb40 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65   sqlite3VdbeRese
4eb50 74 53 74 65 70 52 65 73 75 6c 74 28 56 64 62 65  tStepResult(Vdbe
4eb60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4eb70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
4eb80 62 65 52 65 73 65 74 28 56 64 62 65 2a 29 3b 0a  beReset(Vdbe*);.
4eb90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4eba0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53  oid sqlite3VdbeS
4ebb0 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c  etNumCols(Vdbe*,
4ebc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4ebd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4ebe0 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56  VdbeSetColName(V
4ebf0 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  dbe*, int, int, 
4ec00 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 76 6f  const char *, vo
4ec10 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53  id(*)(void*));.S
4ec20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4ec30 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f  id sqlite3VdbeCo
4ec40 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 2a  untChanges(Vdbe*
4ec50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4ec60 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  E sqlite3 *sqlit
4ec70 65 33 56 64 62 65 44 62 28 56 64 62 65 2a 29 3b  e3VdbeDb(Vdbe*);
4ec80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4ec90 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
4eca0 53 65 74 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f  SetSql(Vdbe*, co
4ecb0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
4ecc0 20 6e 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   n, int);.SQLITE
4ecd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4ece0 6c 69 74 65 33 56 64 62 65 53 77 61 70 28 56 64  lite3VdbeSwap(Vd
4ecf0 62 65 2a 2c 56 64 62 65 2a 29 3b 0a 0a 23 69 66  be*,Vdbe*);..#if
4ed00 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
4ed10 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
4ed20 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ENT.SQLITE_PRIVA
4ed30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
4ed40 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28  beReleaseMemory(
4ed50 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c  int);.#endif.SQL
4ed60 49 54 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61  ITE_PRIVATE Unpa
4ed70 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69  ckedRecord *sqli
4ed80 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70  te3VdbeRecordUnp
4ed90 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74  ack(KeyInfo*,int
4eda0 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 63 68 61  ,const void*,cha
4edb0 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  r*,int);.SQLITE_
4edc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4edd0 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e  ite3VdbeDeleteUn
4ede0 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70  packedRecord(Unp
4edf0 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 53  ackedRecord*);.S
4ee00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4ee10 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  t sqlite3VdbeRec
4ee20 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 2c 63  ordCompare(int,c
4ee30 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 61 63  onst void*,Unpac
4ee40 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a 0a 23  kedRecord*);...#
4ee50 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51  ifndef NDEBUG.SQ
4ee60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
4ee70 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
4ee80 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63 6f  omment(Vdbe*, co
4ee90 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
4eea0 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43 6f  .# define VdbeCo
4eeb0 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65  mment(X)  sqlite
4eec0 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 0a 53  3VdbeComment X.S
4eed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4eee0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
4eef0 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65  NoopComment(Vdbe
4ef00 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
4ef10 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56  ...);.# define V
4ef20 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58  dbeNoopComment(X
4ef30 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f  )  sqlite3VdbeNo
4ef40 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c 73  opComment X.#els
4ef50 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43  e.# define VdbeC
4ef60 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 66 69  omment(X).# defi
4ef70 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65  ne VdbeNoopComme
4ef80 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65  nt(X).#endif..#e
4ef90 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
4efa0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
4efb0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
4efc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4efd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4efe0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
4eff0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
4f000 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
4f010 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
4f020 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4f030 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
4f040 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61  ***** Include pa
4f050 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ger.h in the mid
4f060 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
4f070 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
4f080 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
4f090 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
4f0a0 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a   pager.h *******
4f0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f0d0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
4f0e0 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
4f0f0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
4f100 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
4f110 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
4f120 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
4f130 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
4f140 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
4f150 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
4f160 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
4f170 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
4f180 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
4f190 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
4f1a0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
4f1b0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
4f1c0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
4f1d0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
4f1e0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
4f1f0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
4f200 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
4f210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
4f250 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
4f260 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
4f270 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20  erface that the 
4f280 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68  sqlite page cach
4f290 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20  e.** subsystem. 
4f2a0 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
4f2b0 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 73 20  subsystem reads 
4f2c0 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 69 6c  and writes a fil
4f2d0 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 20 61  e a page.** at a
4f2e0 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 69 64   time and provid
4f2f0 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 6f 72  es a journal for
4f300 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
4f310 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 72   @(#) $Id: pager
4f320 2e 68 2c 76 20 31 2e 31 30 31 20 32 30 30 39 2f  .h,v 1.101 2009/
4f330 30 34 2f 33 30 20 30 39 3a 31 30 3a 33 38 20 64  04/30 09:10:38 d
4f340 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
4f350 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41  .*/..#ifndef _PA
4f360 47 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  GER_H_.#define _
4f370 50 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  PAGER_H_../*.** 
4f380 44 65 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20  Default maximum 
4f390 73 69 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74  size for persist
4f3a0 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ent journal file
4f3b0 73 2e 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a  s. A negative .*
4f3c0 2a 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f  * value means no
4f3d0 20 6c 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c   limit. This val
4f3e0 75 65 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69  ue may be overri
4f3f0 64 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a  dden using the .
4f400 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  ** sqlite3PagerJ
4f410 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28  ournalSizeLimit(
4f420 29 20 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20  ) API. See also 
4f430 22 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f  "PRAGMA journal_
4f440 73 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a  size_limit"..*/.
4f450 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
4f460 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53  EFAULT_JOURNAL_S
4f470 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66  IZE_LIMIT.  #def
4f480 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
4f490 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f  LT_JOURNAL_SIZE_
4f4a0 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a  LIMIT -1.#endif.
4f4b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20  ./*.** The type 
4f4c0 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e  used to represen
4f4d0 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e  t a page number.
4f4e0 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65    The first page
4f4f0 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73   in a file.** is
4f500 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20   called page 1. 
4f510 20 30 20 69 73 20 75 73 65 64 20 74 6f 20 72 65   0 is used to re
4f520 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70  present "not a p
4f530 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  age"..*/.typedef
4f540 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a   u32 Pgno;../*.*
4f550 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65  * Each open file
4f560 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61   is managed by a
4f570 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e   separate instan
4f580 63 65 20 6f 66 20 74 68 65 20 22 50 61 67 65 72  ce of the "Pager
4f590 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  " structure..*/.
4f5a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50  typedef struct P
4f5b0 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a  ager Pager;../*.
4f5c0 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66  ** Handle type f
4f5d0 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70  or pages..*/.typ
4f5e0 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64  edef struct PgHd
4f5f0 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a  r DbPage;../*.**
4f600 20 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47   Page number PAG
4f610 45 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65  ER_MJ_PGNO is ne
4f620 76 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53  ver used in an S
4f630 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28  QLite database (
4f640 69 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65  it is.** reserve
4f650 64 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72  d for working ar
4f660 6f 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70  ound a windows/p
4f670 6f 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69  osix incompatibi
4f680 6c 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20  lity). It is.** 
4f690 75 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72  used in the jour
4f6a0 6e 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74  nal to signify t
4f6b0 68 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65  hat the remainde
4f6c0 72 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  r of the journal
4f6d0 20 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76   file .** is dev
4f6e0 6f 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20  oted to storing 
4f6f0 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
4f700 20 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72   name - there ar
4f710 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20  e no more pages 
4f720 74 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e  to.** roll back.
4f730 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f   See comments fo
4f740 72 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65  r function write
4f750 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20  MasterJournal() 
4f760 69 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66  in pager.c .** f
4f770 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23  or details..*/.#
4f780 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f  define PAGER_MJ_
4f790 50 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28  PGNO(x) ((Pgno)(
4f7a0 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28  (PENDING_BYTE/((
4f7b0 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31  x)->pageSize))+1
4f7c0 29 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65  ))../*.** Allowe
4f7d0 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65  d values for the
4f7e0 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72   flags parameter
4f7f0 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72   to sqlite3Pager
4f800 4f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f  Open()..**.** NO
4f810 54 45 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73  TE: These values
4f820 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20   must match the 
4f830 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54  corresponding BT
4f840 52 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62  REE_ values in b
4f850 74 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69  tree.h..*/.#defi
4f860 6e 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f  ne PAGER_OMIT_JO
4f870 55 52 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20  URNAL  0x0001   
4f880 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61   /* Do not use a
4f890 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
4f8a0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  l */.#define PAG
4f8b0 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20  ER_NO_READLOCK  
4f8c0 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d   0x0002    /* Om
4f8d0 69 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20  it readlocks on 
4f8e0 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a  readonly files *
4f8f0 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76  /../*.** Valid v
4f900 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65  alues for the se
4f910 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
4f920 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
4f930 6b 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23  kingMode()..*/.#
4f940 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43  define PAGER_LOC
4f950 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20  KINGMODE_QUERY  
4f960 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50      -1.#define P
4f970 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
4f980 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23  _NORMAL      0.#
4f990 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43  define PAGER_LOC
4f9a0 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
4f9b0 56 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61  VE   1../*.** Va
4f9c0 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74  lid values for t
4f9d0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
4f9e0 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67  nt to sqlite3Pag
4f9f0 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e  erJournalMode().
4fa00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45  .*/.#define PAGE
4fa10 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
4fa20 45 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66  ERY      -1.#def
4fa30 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ine PAGER_JOURNA
4fa40 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20  LMODE_DELETE    
4fa50 20 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20    0   /* Commit 
4fa60 62 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72  by deleting jour
4fa70 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66  nal file */.#def
4fa80 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ine PAGER_JOURNA
4fa90 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20  LMODE_PERSIST   
4faa0 20 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20    1   /* Commit 
4fab0 62 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e  by zeroing journ
4fac0 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65  al header */.#de
4fad0 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e  fine PAGER_JOURN
4fae0 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20  ALMODE_OFF      
4faf0 20 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61     2   /* Journa
4fb00 6c 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23  l omitted.  */.#
4fb10 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55  define PAGER_JOU
4fb20 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54  RNALMODE_TRUNCAT
4fb30 45 20 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d  E    3   /* Comm
4fb40 69 74 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67  it by truncating
4fb50 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66   journal */.#def
4fb60 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ine PAGER_JOURNA
4fb70 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20  LMODE_MEMORY    
4fb80 20 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f    4   /* In-memo
4fb90 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ry journal file 
4fba0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65  */../*.** The re
4fbb0 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20  mainder of this 
4fbc0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
4fbd0 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f  e declarations o
4fbe0 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a  f the functions.
4fbf0 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20  ** that make up 
4fc00 74 68 65 20 50 61 67 65 72 20 73 75 62 2d 73 79  the Pager sub-sy
4fc10 73 74 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f  stem API. See so
4fc20 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e  urce code commen
4fc30 74 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74  ts for .** a det
4fc40 61 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f  ailed descriptio
4fc50 6e 20 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e  n of each routin
4fc60 65 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61  e..*/../* Open a
4fc70 6e 64 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72  nd close a Pager
4fc80 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20   connection. */ 
4fc90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4fca0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
4fcb0 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73  Open(sqlite3_vfs
4fcc0 20 2a 2c 20 50 61 67 65 72 20 2a 2a 70 70 50 61   *, Pager **ppPa
4fcd0 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  ger, const char*
4fce0 2c 20 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a  , int,int,int);.
4fcf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4fd00 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  nt sqlite3PagerC
4fd10 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 67  lose(Pager *pPag
4fd20 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  er);.SQLITE_PRIV
4fd30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
4fd40 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64  agerReadFilehead
4fd50 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20  er(Pager*, int, 
4fd60 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b  unsigned char*);
4fd70 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75  ../* Functions u
4fd80 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65  sed to configure
4fd90 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e   a Pager object.
4fda0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
4fdb0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
4fdc0 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c  agerSetBusyhandl
4fdd0 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a  er(Pager*, int(*
4fde0 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20  )(void *), void 
4fdf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4fe00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
4fe10 61 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 28  agerSetReiniter(
4fe20 50 61 67 65 72 2a 2c 20 76 6f 69 64 28 2a 29 28  Pager*, void(*)(
4fe30 44 62 50 61 67 65 2a 29 29 3b 0a 53 51 4c 49 54  DbPage*));.SQLIT
4fe40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4fe50 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67  lite3PagerSetPag
4fe60 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20 75 31  esize(Pager*, u1
4fe70 36 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  6*);.SQLITE_PRIV
4fe80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
4fe90 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74  agerMaxPageCount
4fea0 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53  (Pager*, int);.S
4feb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4fec0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
4fed0 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65  etCachesize(Page
4fee0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
4fef0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4ff00 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66  lite3PagerSetSaf
4ff10 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c  etyLevel(Pager*,
4ff20 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
4ff30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4ff40 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67  ite3PagerLocking
4ff50 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e  Mode(Pager *, in
4ff60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4ff70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
4ff80 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50  gerJournalMode(P
4ff90 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  ager *, int);.SQ
4ffa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34  LITE_PRIVATE i64
4ffb0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
4ffc0 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61  rnalSizeLimit(Pa
4ffd0 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c  ger *, i64);.SQL
4ffe0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
4fff0 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c  te3_backup **sql
50000 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50  ite3PagerBackupP
50010 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20  tr(Pager*);../* 
50020 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  Functions used t
50030 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c  o obtain and rel
50040 65 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65  ease page refere
50050 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45  nces. */ .SQLITE
50060 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
50070 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
50080 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
50090 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67  Pgno pgno, DbPag
500a0 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20  e **ppPage, int 
500b0 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e  clrFlag);.#defin
500c0 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  e sqlite3PagerGe
500d0 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33  t(A,B,C) sqlite3
500e0 50 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42  PagerAcquire(A,B
500f0 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49  ,C,0).SQLITE_PRI
50100 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c  VATE DbPage *sql
50110 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
50120 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
50130 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54  gno pgno);.SQLIT
50140 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
50150 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44  qlite3PagerRef(D
50160 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  bPage*);.SQLITE_
50170 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
50180 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44  ite3PagerUnref(D
50190 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65  bPage*);../* Ope
501a0 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20  rations on page 
501b0 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53  references. */.S
501c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
501d0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  t sqlite3PagerWr
501e0 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51  ite(DbPage*);.SQ
501f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
50200 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  d sqlite3PagerDo
50210 6e 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29  ntWrite(DbPage*)
50220 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
50230 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
50240 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a  rMovepage(Pager*
50250 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e  ,DbPage*,Pgno,in
50260 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
50270 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
50280 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28  gerPageRefcount(
50290 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
502a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
502b0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
502c0 74 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53  ta(DbPage *); .S
502d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
502e0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
502f0 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20  GetExtra(DbPage 
50300 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f  *); ../* Functio
50310 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67  ns used to manag
50320 65 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74  e pager transact
50330 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69  ions and savepoi
50340 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts. */.SQLITE_P
50350 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
50360 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
50370 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a  (Pager*, int*);.
50380 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
50390 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42  nt sqlite3PagerB
503a0 65 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74  egin(Pager*, int
503b0 20 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53   exFlag, int);.S
503c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
503d0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
503e0 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67  mmitPhaseOne(Pag
503f0 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a  er*,const char *
50400 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53  zMaster, int);.S
50410 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
50420 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  t sqlite3PagerSy
50430 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nc(Pager *pPager
50440 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
50450 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
50460 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
50470 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
50480 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
50490 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
504a0 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  k(Pager*);.SQLIT
504b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
504c0 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61  lite3PagerOpenSa
504d0 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
504e0 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53  Pager, int n);.S
504f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
50500 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61  t sqlite3PagerSa
50510 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
50520 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69  Pager, int op, i
50530 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a  nt iSavepoint);.
50540 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73  ./* Functions us
50550 65 64 20 74 6f 20 71 75 65 72 79 20 70 61 67 65  ed to query page
50560 72 20 73 74 61 74 65 20 61 6e 64 20 63 6f 6e 66  r state and conf
50570 69 67 75 72 61 74 69 6f 6e 2e 20 2a 2f 0a 53 51  iguration. */.SQ
50580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20  LITE_PRIVATE u8 
50590 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65  sqlite3PagerIsre
505a0 61 64 6f 6e 6c 79 28 50 61 67 65 72 2a 29 3b 0a  adonly(Pager*);.
505b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
505c0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
505d0 65 66 63 6f 75 6e 74 28 50 61 67 65 72 2a 29 3b  efcount(Pager*);
505e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
505f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
50600 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65  te3PagerFilename
50610 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
50620 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
50630 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
50640 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65  te3PagerVfs(Page
50650 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
50660 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ATE sqlite3_file
50670 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
50680 6c 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  le(Pager*);.SQLI
50690 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
506a0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61   char *sqlite3Pa
506b0 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50  gerJournalname(P
506c0 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
506d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
506e0 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61  e3PagerNosync(Pa
506f0 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
50700 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
50710 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63  te3PagerTempSpac
50720 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  e(Pager*);.SQLIT
50730 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
50740 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64  lite3PagerIsMemd
50750 62 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46  b(Pager*);../* F
50760 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f  unctions used to
50770 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 64 61   truncate the da
50780 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a  tabase file. */.
50790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
507a0 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
507b0 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61  TruncateImage(Pa
507c0 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a 0a 2f 2a 20  ger*,Pgno);../* 
507d0 55 73 65 64 20 62 79 20 65 6e 63 72 79 70 74 69  Used by encrypti
507e0 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 2a  on extensions. *
507f0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
50800 48 41 53 5f 43 4f 44 45 43 0a 53 51 4c 49 54 45  HAS_CODEC.SQLITE
50810 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
50820 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
50830 6f 64 65 63 28 50 61 67 65 72 2a 2c 76 6f 69 64  odec(Pager*,void
50840 2a 28 2a 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a  *(*)(void*,void*
50850 2c 50 67 6e 6f 2c 69 6e 74 29 2c 76 6f 69 64 2a  ,Pgno,int),void*
50860 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 75  );.#endif../* Fu
50870 6e 63 74 69 6f 6e 73 20 74 6f 20 73 75 70 70 6f  nctions to suppo
50880 72 74 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  rt testing and d
50890 65 62 75 67 67 69 6e 67 2e 20 2a 2f 0a 23 69 66  ebugging. */.#if
508a0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
508b0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
508c0 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45  ITE_TEST).SQLITE
508d0 5f 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20  _PRIVATE   Pgno 
508e0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
508f0 6e 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b  number(DbPage*);
50900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
50910 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67    int sqlite3Pag
50920 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62  erIswriteable(Db
50930 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 23  Page*);.#endif.#
50940 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
50950 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
50960 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50     int *sqlite3P
50970 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72 2a  agerStats(Pager*
50980 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
50990 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
509a0 50 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 67  PagerRefdump(Pag
509b0 65 72 2a 29 3b 0a 20 20 76 6f 69 64 20 64 69 73  er*);.  void dis
509c0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
509d0 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a  o_errors(void);.
509e0 20 20 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69    void enable_si
509f0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
50a00 73 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23  s(void);.#else.#
50a10 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f   define disable_
50a20 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
50a30 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65  ors().# define e
50a40 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
50a50 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64  io_errors().#end
50a60 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50  if..#endif /* _P
50a70 41 47 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  AGER_H_ */../***
50a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
50a90 6f 66 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a  of pager.h *****
50aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
50ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
50ae0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
50af0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
50b00 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
50b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
50b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
50b30 75 64 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20  ude pcache.h in 
50b40 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71  the middle of sq
50b50 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
50b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
50b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
50b80 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20  n file pcache.h 
50b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
50bc0 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35  * 2008 August 05
50bd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
50be0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
50bf0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
50c00 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
50c10 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
50c20 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
50c30 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
50c40 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
50c50 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
50c60 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
50c70 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
50c80 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
50c90 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
50ca0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
50cb0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
50cc0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
50cd0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
50ce0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
50cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
50d30 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
50d40 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ile defines the 
50d50 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74  interface that t
50d60 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63  he sqlite page c
50d70 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65  ache.** subsyste
50d80 6d 2e 20 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  m. .**.** @(#) $
50d90 49 64 3a 20 70 63 61 63 68 65 2e 68 2c 76 20 31  Id: pcache.h,v 1
50da0 2e 31 39 20 32 30 30 39 2f 30 31 2f 32 30 20 31  .19 2009/01/20 1
50db0 37 3a 30 36 3a 32 37 20 64 61 6e 69 65 6c 6b 31  7:06:27 danielk1
50dc0 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69  977 Exp $.*/..#i
50dd0 66 6e 64 65 66 20 5f 50 43 41 43 48 45 5f 48 5f  fndef _PCACHE_H_
50de0 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
50df0 20 50 67 48 64 72 20 50 67 48 64 72 3b 0a 74 79   PgHdr PgHdr;.ty
50e00 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61  pedef struct PCa
50e10 63 68 65 20 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a  che PCache;../*.
50e20 2a 2a 20 45 76 65 72 79 20 70 61 67 65 20 69 6e  ** Every page in
50e30 20 74 68 65 20 63 61 63 68 65 20 69 73 20 63 6f   the cache is co
50e40 6e 74 72 6f 6c 6c 65 64 20 62 79 20 61 6e 20 69  ntrolled by an i
50e50 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
50e60 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75  ollowing.** stru
50e70 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  cture..*/.struct
50e80 20 50 67 48 64 72 20 7b 0a 20 20 76 6f 69 64 20   PgHdr {.  void 
50e90 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  *pData;         
50ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
50eb0 74 65 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67  tent of this pag
50ec0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 78  e */.  void *pEx
50ed0 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  tra;            
50ee0 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 63        /* Extra c
50ef0 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 50 67 48 64  ontent */.  PgHd
50f00 72 20 2a 70 44 69 72 74 79 3b 20 20 20 20 20 20  r *pDirty;      
50f10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
50f20 61 6e 73 69 65 6e 74 20 6c 69 73 74 20 6f 66 20  ansient list of 
50f30 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20  dirty pages */. 
50f40 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20   Pgno pgno;     
50f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f60 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66  /* Page number f
50f70 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  or this page */.
50f80 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
50f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fa0 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 74 68   /* The pager th
50fb0 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20  is page is part 
50fc0 6f 66 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  of */.#ifdef SQL
50fd0 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
50fe0 20 20 75 33 32 20 70 61 67 65 48 61 73 68 3b 20    u32 pageHash; 
50ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51000 20 2f 2a 20 48 61 73 68 20 6f 66 20 70 61 67 65   /* Hash of page
51010 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 23 65 6e 64   content */.#end
51020 69 66 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20  if.  u16 flags; 
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51040 20 20 20 20 2f 2a 20 50 47 48 44 52 20 66 6c 61      /* PGHDR fla
51050 67 73 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77  gs defined below
51060 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a   */..  /********
51070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
510a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20  **************. 
510b0 20 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f   ** Elements abo
510c0 76 65 20 61 72 65 20 70 75 62 6c 69 63 2e 20 20  ve are public.  
510d0 41 6c 6c 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  All that follows
510e0 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 70   is private to p
510f0 63 61 63 68 65 2e 63 0a 20 20 2a 2a 20 61 6e 64  cache.c.  ** and
51100 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61   should not be a
51110 63 63 65 73 73 65 64 20 62 79 20 6f 74 68 65 72  ccessed by other
51120 20 6d 6f 64 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20   modules..  */. 
51130 20 69 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20   i16 nRef;      
51140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51150 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65  /* Number of use
51160 72 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  rs of this page 
51170 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  */.  PCache *pCa
51180 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  che;            
51190 20 20 20 20 2f 2a 20 43 61 63 68 65 20 74 68 61      /* Cache tha
511a0 74 20 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65  t owns this page
511b0 20 2a 2f 0a 0a 20 20 50 67 48 64 72 20 2a 70 44   */..  PgHdr *pD
511c0 69 72 74 79 4e 65 78 74 3b 20 20 20 20 20 20 20  irtyNext;       
511d0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6c        /* Next el
511e0 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66  ement in list of
511f0 20 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a   dirty pages */.
51200 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 50    PgHdr *pDirtyP
51210 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  rev;            
51220 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 65 6c 65   /* Previous ele
51230 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66 20  ment in list of 
51240 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 7d  dirty pages */.}
51250 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c 75 65 73  ;../* Bit values
51260 20 66 6f 72 20 50 67 48 64 72 2e 66 6c 61 67 73   for PgHdr.flags
51270 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44   */.#define PGHD
51280 52 5f 44 49 52 54 59 20 20 20 20 20 20 20 20 20  R_DIRTY         
51290 20 20 20 20 30 78 30 30 32 20 20 2f 2a 20 50 61      0x002  /* Pa
512a0 67 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a  ge has changed *
512b0 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f  /.#define PGHDR_
512c0 4e 45 45 44 5f 53 59 4e 43 20 20 20 20 20 20 20  NEED_SYNC       
512d0 20 20 30 78 30 30 34 20 20 2f 2a 20 46 73 79 6e    0x004  /* Fsyn
512e0 63 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  c the rollback j
512f0 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 0a 20 20  ournal before.  
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51320 20 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20       ** writing 
51330 74 68 69 73 20 70 61 67 65 20 74 6f 20 74 68 65  this page to the
51340 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65   database */.#de
51350 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f  fine PGHDR_NEED_
51360 52 45 41 44 20 20 20 20 20 20 20 20 20 30 78 30  READ         0x0
51370 30 38 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 69  08  /* Content i
51380 73 20 75 6e 72 65 61 64 20 2a 2f 0a 23 64 65 66  s unread */.#def
51390 69 6e 65 20 50 47 48 44 52 5f 52 45 55 53 45 5f  ine PGHDR_REUSE_
513a0 55 4e 4c 49 4b 45 4c 59 20 20 20 20 30 78 30 31  UNLIKELY    0x01
513b0 30 20 20 2f 2a 20 41 20 68 69 6e 74 20 74 68 61  0  /* A hint tha
513c0 74 20 72 65 75 73 65 20 69 73 20 75 6e 6c 69 6b  t reuse is unlik
513d0 65 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ely */.#define P
513e0 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20  GHDR_DONT_WRITE 
513f0 20 20 20 20 20 20 20 30 78 30 32 30 20 20 2f 2a         0x020  /*
51400 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f   Do not write co
51410 6e 74 65 6e 74 20 74 6f 20 64 69 73 6b 20 2a 2f  ntent to disk */
51420 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  ../* Initialize 
51430 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65  and shutdown the
51440 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73   page cache subs
51450 79 73 74 65 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f  ystem */.SQLITE_
51460 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
51470 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c  te3PcacheInitial
51480 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ize(void);.SQLIT
51490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
514a0 71 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74  qlite3PcacheShut
514b0 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20  down(void);../* 
514c0 50 61 67 65 20 63 61 63 68 65 20 62 75 66 66 65  Page cache buffe
514d0 72 20 6d 61 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a  r management:.**
514e0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
514f0 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 49 54 45  implement SQLITE
51500 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
51510 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  E..*/.SQLITE_PRI
51520 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
51530 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65 74  3PCacheBufferSet
51540 75 70 28 76 6f 69 64 20 2a 2c 20 69 6e 74 20 73  up(void *, int s
51550 7a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43  z, int n);../* C
51560 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  reate a new page
51570 72 20 63 61 63 68 65 2e 0a 2a 2a 20 55 6e 64 65  r cache..** Unde
51580 72 20 6d 65 6d 6f 72 79 20 73 74 72 65 73 73 2c  r memory stress,
51590 20 69 6e 76 6f 6b 65 20 78 53 74 72 65 73 73 20   invoke xStress 
515a0 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70  to try to make p
515b0 61 67 65 73 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f  ages clean..** O
515c0 6e 6c 79 20 63 6c 65 61 6e 20 61 6e 64 20 75 6e  nly clean and un
515d0 70 69 6e 6e 65 64 20 70 61 67 65 73 20 63 61 6e  pinned pages can
515e0 20 62 65 20 72 65 63 6c 61 69 6d 65 64 2e 0a 2a   be reclaimed..*
515f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
51600 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
51610 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73  cheOpen(.  int s
51620 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  zPage,          
51630 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
51640 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20  e of every page 
51650 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61  */.  int szExtra
51660 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
51670 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61      /* Extra spa
51680 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ce associated wi
51690 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  th each page */.
516a0 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65    int bPurgeable
516b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
516c0 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 65   /* True if page
516d0 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67  s are on backing
516e0 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20   store */.  int 
516f0 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a  (*xStress)(void*
51700 2c 20 50 67 48 64 72 2a 29 2c 20 2f 2a 20 43 61  , PgHdr*), /* Ca
51710 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b  ll to try to mak
51720 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f  e pages clean */
51730 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73  .  void *pStress
51740 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
51750 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
51760 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43   xStress */.  PC
51770 61 63 68 65 20 2a 70 54 6f 49 6e 69 74 20 20 20  ache *pToInit   
51780 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
51790 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 61  Preallocated spa
517a0 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 68  ce for the PCach
517b0 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69  e */.);../* Modi
517c0 66 79 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  fy the page-size
517d0 20 61 66 74 65 72 20 74 68 65 20 63 61 63 68 65   after the cache
517e0 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65   has been create
517f0 64 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d. */.SQLITE_PRI
51800 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
51810 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69  3PcacheSetPageSi
51820 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 6e 74  ze(PCache *, int
51830 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68  );../* Return th
51840 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  e size in bytes 
51850 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65  of a PCache obje
51860 63 74 2e 20 20 55 73 65 64 20 74 6f 20 70 72 65  ct.  Used to pre
51870 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 74 6f 72  allocate.** stor
51880 61 67 65 20 73 70 61 63 65 2e 0a 2a 2f 0a 53 51  age space..*/.SQ
51890 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
518a0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69   sqlite3PcacheSi
518b0 7a 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e  ze(void);../* On
518c0 65 20 72 65 6c 65 61 73 65 20 70 65 72 20 73 75  e release per su
518d0 63 63 65 73 73 66 75 6c 20 66 65 74 63 68 2e 20  ccessful fetch. 
518e0 20 50 61 67 65 20 69 73 20 70 69 6e 6e 65 64 20   Page is pinned 
518f0 75 6e 74 69 6c 20 72 65 6c 65 61 73 65 64 2e 0a  until released..
51900 2a 2a 20 52 65 66 65 72 65 6e 63 65 20 63 6f 75  ** Reference cou
51910 6e 74 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45  nted. .*/.SQLITE
51920 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
51930 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28  ite3PcacheFetch(
51940 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 2c 20 69  PCache*, Pgno, i
51950 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 50  nt createFlag, P
51960 67 48 64 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  gHdr**);.SQLITE_
51970 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
51980 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
51990 65 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49  e(PgHdr*);..SQLI
519a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
519b0 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f  sqlite3PcacheDro
519c0 70 28 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20  p(PgHdr*);      
519d0 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 70 61 67     /* Remove pag
519e0 65 20 66 72 6f 6d 20 63 61 63 68 65 20 2a 2f 0a  e from cache */.
519f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
51a00 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
51a10 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72  eMakeDirty(PgHdr
51a20 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73  *);    /* Make s
51a30 75 72 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b  ure page is mark
51a40 65 64 20 64 69 72 74 79 20 2a 2f 0a 53 51 4c 49  ed dirty */.SQLI
51a50 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
51a60 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
51a70 65 43 6c 65 61 6e 28 50 67 48 64 72 2a 29 3b 20  eClean(PgHdr*); 
51a80 20 20 20 2f 2a 20 4d 61 72 6b 20 61 20 73 69 6e     /* Mark a sin
51a90 67 6c 65 20 70 61 67 65 20 61 73 20 63 6c 65 61  gle page as clea
51aa0 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n */.SQLITE_PRIV
51ab0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
51ac0 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50  PcacheCleanAll(P
51ad0 43 61 63 68 65 2a 29 3b 20 20 20 20 2f 2a 20 4d  Cache*);    /* M
51ae0 61 72 6b 20 61 6c 6c 20 64 69 72 74 79 20 6c 69  ark all dirty li
51af0 73 74 20 70 61 67 65 73 20 61 73 20 63 6c 65 61  st pages as clea
51b00 6e 20 2a 2f 0a 0a 2f 2a 20 43 68 61 6e 67 65 20  n */../* Change 
51b10 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20  a page number.  
51b20 55 73 65 64 20 62 79 20 69 6e 63 72 2d 76 61 63  Used by incr-vac
51b30 75 75 6d 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  uum. */.SQLITE_P
51b40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
51b50 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 50 67  te3PcacheMove(Pg
51b60 48 64 72 2a 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a  Hdr*, Pgno);../*
51b70 20 52 65 6d 6f 76 65 20 61 6c 6c 20 70 61 67 65   Remove all page
51b80 73 20 77 69 74 68 20 70 67 6e 6f 3e 78 2e 20 20  s with pgno>x.  
51b90 52 65 73 65 74 20 74 68 65 20 63 61 63 68 65 20  Reset the cache 
51ba0 69 66 20 78 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54  if x==0 */.SQLIT
51bb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
51bc0 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e  qlite3PcacheTrun
51bd0 63 61 74 65 28 50 43 61 63 68 65 2a 2c 20 50 67  cate(PCache*, Pg
51be0 6e 6f 20 78 29 3b 0a 0a 2f 2a 20 47 65 74 20 61  no x);../* Get a
51bf0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72   list of all dir
51c00 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ty pages in the 
51c10 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79  cache, sorted by
51c20 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
51c30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50  SQLITE_PRIVATE P
51c40 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61  gHdr *sqlite3Pca
51c50 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61  cheDirtyList(PCa
51c60 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 73 65 74  che*);../* Reset
51c70 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 63   and close the c
51c80 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 53  ache object */.S
51c90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
51ca0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
51cb0 43 6c 6f 73 65 28 50 43 61 63 68 65 2a 29 3b 0a  Close(PCache*);.
51cc0 0a 2f 2a 20 43 6c 65 61 72 20 66 6c 61 67 73 20  ./* Clear flags 
51cd0 66 72 6f 6d 20 70 61 67 65 73 20 6f 66 20 74 68  from pages of th
51ce0 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a  e page cache */.
51cf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
51d00 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
51d10 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28  eClearSyncFlags(
51d20 50 43 61 63 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44  PCache *);../* D
51d30 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65  iscard the conte
51d40 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65  nts of the cache
51d50 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
51d60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
51d70 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68  cacheClear(PCach
51d80 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20  e*);../* Return 
51d90 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
51da0 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   of outstanding 
51db0 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20  page references 
51dc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
51dd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
51de0 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63  cheRefCount(PCac
51df0 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d  he*);../* Increm
51e00 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63  ent the referenc
51e10 65 20 63 6f 75 6e 74 20 6f 66 20 61 6e 20 65 78  e count of an ex
51e20 69 73 74 69 6e 67 20 70 61 67 65 20 2a 2f 0a 53  isting page */.S
51e30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
51e40 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
51e50 52 65 66 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51  Ref(PgHdr*);..SQ
51e60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
51e70 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61   sqlite3PcachePa
51e80 67 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64 72  geRefcount(PgHdr
51e90 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74  *);../* Return t
51ea0 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
51eb0 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64 20  of pages stored 
51ec0 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a  in the cache */.
51ed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
51ee0 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
51ef0 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65  Pagecount(PCache
51f00 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  *);..#ifdef SQLI
51f10 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f  TE_CHECK_PAGES./
51f20 2a 20 49 74 65 72 61 74 65 20 74 68 72 6f 75 67  * Iterate throug
51f30 68 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  h all dirty page
51f40 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72  s currently stor
51f50 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e  ed in the cache.
51f60 20 54 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61   This.** interfa
51f70 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  ce is only avail
51f80 61 62 6c 65 20 69 66 20 53 51 4c 49 54 45 5f 43  able if SQLITE_C
51f90 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65  HECK_PAGES is de
51fa0 66 69 6e 65 64 20 77 68 65 6e 20 74 68 65 20 0a  fined when the .
51fb0 2a 2a 20 6c 69 62 72 61 72 79 20 69 73 20 62 75  ** library is bu
51fc0 69 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ilt..*/.SQLITE_P
51fd0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
51fe0 74 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65  te3PcacheIterate
51ff0 44 69 72 74 79 28 50 43 61 63 68 65 20 2a 70 43  Dirty(PCache *pC
52000 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49 74  ache, void (*xIt
52010 65 72 29 28 50 67 48 64 72 20 2a 29 29 3b 0a 23  er)(PgHdr *));.#
52020 65 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20 61 6e  endif../* Set an
52030 64 20 67 65 74 20 74 68 65 20 73 75 67 67 65 73  d get the sugges
52040 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 66  ted cache-size f
52050 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64  or the specified
52060 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 2a 2a   pager-cache..**
52070 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62 61 6c  .** If no global
52080 20 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f 6e 66   maximum is conf
52090 69 67 75 72 65 64 2c 20 74 68 65 6e 20 74 68 65  igured, then the
520a0 20 73 79 73 74 65 6d 20 61 74 74 65 6d 70 74 73   system attempts
520b0 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74 68 65   to limit.** the
520c0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
520d0 20 70 61 67 65 73 20 63 61 63 68 65 64 20 62 79   pages cached by
520e0 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 72   purgeable pager
520f0 2d 63 61 63 68 65 73 20 74 6f 20 74 68 65 20 73  -caches to the s
52100 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 67  um.** of the sug
52110 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a  gested cache-siz
52120 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
52130 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
52140 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65  e3PcacheSetCache
52150 73 69 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69  size(PCache *, i
52160 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  nt);.#ifdef SQLI
52170 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  TE_TEST.SQLITE_P
52180 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
52190 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65  e3PcacheGetCache
521a0 73 69 7a 65 28 50 43 61 63 68 65 20 2a 29 3b 0a  size(PCache *);.
521b0 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
521c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
521d0 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f  ORY_MANAGEMENT./
521e0 2a 20 54 72 79 20 74 6f 20 72 65 74 75 72 6e 20  * Try to return 
521f0 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 74  memory used by t
52200 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65  he pcache module
52210 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 6d 65 6d   to the main mem
52220 6f 72 79 20 68 65 61 70 20 2a 2f 0a 53 51 4c 49  ory heap */.SQLI
52230 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
52240 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65  qlite3PcacheRele
52250 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a  aseMemory(int);.
52260 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
52270 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
52280 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52290 71 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74  qlite3PcacheStat
522a0 73 28 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a  s(int*,int*,int*
522b0 2c 69 6e 74 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  ,int*);.#endif..
522c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
522d0 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68  oid sqlite3PCach
522e0 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64  eSetDefault(void
522f0 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50  );..#endif /* _P
52300 43 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a  CACHE_H_ */../**
52310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
52320 20 6f 66 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a   of pcache.h ***
52330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
52360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
52370 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
52380 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c   left off in sql
52390 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
523a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
523b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
523c0 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20 74 68  clude os.h in th
523d0 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
523e0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
523f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
52400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
52410 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68 20 2a 2a  gin file os.h **
52420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
52450 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
52460 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 16.**.** The 
52470 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
52480 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
52490 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
524a0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
524b0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
524c0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
524d0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
524e0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
524f0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
52500 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
52510 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
52520 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
52530 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
52540 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
52550 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
52560 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
52570 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
52580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
525a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
525b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
525c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
525d0 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
525e0 20 28 74 6f 67 65 74 68 65 72 20 77 69 74 68 20   (together with 
525f0 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43 20 73  is companion C s
52600 6f 75 72 63 65 2d 63 6f 64 65 20 66 69 6c 65 0a  ource-code file.
52610 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74 74 65 6d  ** "os.c") attem
52620 70 74 20 74 6f 20 61 62 73 74 72 61 63 74 20 74  pt to abstract t
52630 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70  he underlying op
52640 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73  erating system s
52650 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 53 51  o that.** the SQ
52660 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c  Lite library wil
52670 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68 20 50  l work on both P
52680 4f 53 49 58 20 61 6e 64 20 77 69 6e 64 6f 77 73  OSIX and windows
52690 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20   systems..**.** 
526a0 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
526b0 20 69 73 20 23 69 6e 63 6c 75 64 65 2d 65 64 20   is #include-ed 
526c0 62 79 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 61  by sqliteInt.h a
526d0 6e 64 20 74 68 75 73 20 65 6e 64 73 20 75 70 0a  nd thus ends up.
526e0 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c 75 64 65  ** being include
526f0 64 20 62 79 20 65 76 65 72 79 20 73 6f 75 72 63  d by every sourc
52700 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  e file..**.** $I
52710 64 3a 20 6f 73 2e 68 2c 76 20 31 2e 31 30 38 20  d: os.h,v 1.108 
52720 32 30 30 39 2f 30 32 2f 30 35 20 31 36 3a 33 31  2009/02/05 16:31
52730 3a 34 36 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :46 drh Exp $.*/
52740 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45  .#ifndef _SQLITE
52750 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  _OS_H_.#define _
52760 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a  SQLITE_OS_H_../*
52770 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69  .** Figure out i
52780 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  f we are dealing
52790 20 77 69 74 68 20 55 6e 69 78 2c 20 57 69 6e 64   with Unix, Wind
527a0 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f 74 68  ows, or some oth
527b0 65 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20  er.** operating 
527c0 73 79 73 74 65 6d 2e 20 20 41 66 74 65 72 20 74  system.  After t
527d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
527e0 63 6b 20 6f 66 20 70 72 65 70 72 6f 63 65 73 73  ck of preprocess
527f0 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20   macros,.** all 
52800 6f 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  of SQLITE_OS_UNI
52810 58 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  X, SQLITE_OS_WIN
52820 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c  , SQLITE_OS_OS2,
52830 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53 5f 4f   and SQLITE_OS_O
52840 54 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20 64 65  THER .** will de
52850 66 69 6e 65 64 20 74 6f 20 65 69 74 68 65 72 20  fined to either 
52860 31 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f 66 20  1 or 0.  One of 
52870 74 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 62 65  the four will be
52880 20 31 2e 20 20 54 68 65 20 6f 74 68 65 72 20 0a   1.  The other .
52890 2a 2a 20 74 68 72 65 65 20 77 69 6c 6c 20 62 65  ** three will be
528a0 20 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e   0..*/.#if defin
528b0 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48  ed(SQLITE_OS_OTH
528c0 45 52 29 0a 23 20 69 66 20 53 51 4c 49 54 45 5f  ER).# if SQLITE_
528d0 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 20 20  OS_OTHER==1.#   
528e0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f  undef SQLITE_OS_
528f0 55 4e 49 58 0a 23 20 20 20 64 65 66 69 6e 65 20  UNIX.#   define 
52900 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30  SQLITE_OS_UNIX 0
52910 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54  .#   undef SQLIT
52920 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64 65 66  E_OS_WIN.#   def
52930 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
52940 4e 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51  N 0.#   undef SQ
52950 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20  LITE_OS_OS2.#   
52960 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
52970 5f 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a 23 20  _OS2 0.# else.# 
52980 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f    undef SQLITE_O
52990 53 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69 66 0a  S_OTHER.# endif.
529a0 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66 69  #endif.#if !defi
529b0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 55 4e  ned(SQLITE_OS_UN
529c0 49 58 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  IX) && !defined(
529d0 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29  SQLITE_OS_OTHER)
529e0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
529f0 5f 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 69 66  _OS_OTHER 0.# if
52a00 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  ndef SQLITE_OS_W
52a10 49 4e 0a 23 20 20 20 69 66 20 64 65 66 69 6e 65  IN.#   if define
52a20 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 66  d(_WIN32) || def
52a30 69 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c 20 64  ined(WIN32) || d
52a40 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f  efined(__CYGWIN_
52a50 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  _) || defined(__
52a60 4d 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64 65  MINGW32__) || de
52a70 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43  fined(__BORLANDC
52a80 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65  __).#     define
52a90 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 31   SQLITE_OS_WIN 1
52aa0 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51  .#     define SQ
52ab0 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23  LITE_OS_UNIX 0.#
52ac0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
52ad0 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 20  TE_OS_OS2 0.#   
52ae0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 45  elif defined(__E
52af0 4d 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  MX__) || defined
52b00 28 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65  (_OS2) || define
52b10 64 28 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65  d(OS2) || define
52b20 64 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 66 69  d(_OS2_) || defi
52b30 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20  ned(__OS2__).#  
52b40 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
52b50 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20  _OS_WIN 0.#     
52b60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
52b70 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65  _UNIX 0.#     de
52b80 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  fine SQLITE_OS_O
52b90 53 32 20 31 0a 23 20 20 20 65 6c 73 65 0a 23 20  S2 1.#   else.# 
52ba0 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
52bb0 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20  E_OS_WIN 0.#    
52bc0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
52bd0 53 5f 55 4e 49 58 20 31 0a 23 20 20 20 20 20 64  S_UNIX 1.#     d
52be0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
52bf0 4f 53 32 20 30 0a 23 20 20 65 6e 64 69 66 0a 23  OS2 0.#  endif.#
52c00 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20   else.#  define 
52c10 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30  SQLITE_OS_UNIX 0
52c20 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  .#  define SQLIT
52c30 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6e 64  E_OS_OS2 0.# end
52c40 69 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e 64 65  if.#else.# ifnde
52c50 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a  f SQLITE_OS_WIN.
52c60 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  #  define SQLITE
52c70 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e 64 69  _OS_WIN 0.# endi
52c80 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  f.#endif../*.** 
52c90 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20  Determine if we 
52ca0 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
52cb0 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69   WindowsCE - whi
52cc0 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a  ch has a much.**
52cd0 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f   reduced API..*/
52ce0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49  .#if defined(_WI
52cf0 4e 33 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e  N32_WCE).# defin
52d00 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  e SQLITE_OS_WINC
52d10 45 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  E 1.#else.# defi
52d20 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  ne SQLITE_OS_WIN
52d30 43 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  CE 0.#endif.../*
52d40 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 6d  .** Define the m
52d50 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61  aximum size of a
52d60 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 6e   temporary filen
52d70 61 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ame.*/.#if SQLIT
52d80 45 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63 6c 75  E_OS_WIN.# inclu
52d90 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a 23  de <windows.h>.#
52da0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54   define SQLITE_T
52db0 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 4d 41  EMPNAME_SIZE (MA
52dc0 58 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c 69 66  X_PATH+50).#elif
52dd0 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23   SQLITE_OS_OS2.#
52de0 20 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20   if (__GNUC__ > 
52df0 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d  3 || __GNUC__ ==
52e00 20 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e   3 && __GNUC_MIN
52e10 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20 64 65  OR__ >= 3) && de
52e20 66 69 6e 65 64 28 4f 53 32 5f 48 49 47 48 5f 4d  fined(OS2_HIGH_M
52e30 45 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c 75 64  EMORY).#  includ
52e40 65 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20 2f 2a  e <os2safe.h> /*
52e50 20 68 61 73 20 74 6f 20 62 65 20 69 6e 63 6c 75   has to be inclu
52e60 64 65 64 20 62 65 66 6f 72 65 20 6f 73 32 2e 68  ded before os2.h
52e70 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 6f 20   for linking to 
52e80 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 66 0a  work */.# endif.
52e90 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f  # define INCL_DO
52ea0 53 44 41 54 45 54 49 4d 45 0a 23 20 64 65 66 69  SDATETIME.# defi
52eb0 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d  ne INCL_DOSFILEM
52ec0 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c  GR.# define INCL
52ed0 5f 44 4f 53 45 52 52 4f 52 53 0a 23 20 64 65 66  _DOSERRORS.# def
52ee0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 53 43  ine INCL_DOSMISC
52ef0 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44  .# define INCL_D
52f00 4f 53 50 52 4f 43 45 53 53 0a 23 20 64 65 66 69  OSPROCESS.# defi
52f10 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c  ne INCL_DOSMODUL
52f20 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e  EMGR.# define IN
52f30 43 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52 45 53  CL_DOSSEMAPHORES
52f40 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 2e  .# include <os2.
52f50 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75 63  h>.# include <uc
52f60 6f 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20  onv.h>.# define 
52f70 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f  SQLITE_TEMPNAME_
52f80 53 49 5a 45 20 28 43 43 48 4d 41 58 50 41 54 48  SIZE (CCHMAXPATH
52f90 43 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20 64 65  COMP).#else.# de
52fa0 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50  fine SQLITE_TEMP
52fb0 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a 23 65  NAME_SIZE 200.#e
52fc0 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68 65 20  ndif../* If the 
52fd0 53 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61 63  SET_FULLSYNC mac
52fe0 72 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  ro is not define
52ff0 64 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d 61  d above, then ma
53000 6b 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70  ke it.** a no-op
53010 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45 54 5f  .*/.#ifndef SET_
53020 46 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66 69 6e  FULLSYNC.# defin
53030 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 28 78  e SET_FULLSYNC(x
53040 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ,y).#endif../*.*
53050 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69  * The default si
53060 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65 63  ze of a disk sec
53070 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  tor.*/.#ifndef S
53080 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45  QLITE_DEFAULT_SE
53090 43 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65 66 69  CTOR_SIZE.# defi
530a0 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
530b0 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 31  T_SECTOR_SIZE 51
530c0 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  2.#endif../*.** 
530d0 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20  Temporary files 
530e0 61 72 65 20 6e 61 6d 65 64 20 73 74 61 72 74 69  are named starti
530f0 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 72 65  ng with this pre
53100 66 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  fix followed by 
53110 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70  16 random.** alp
53120 68 61 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63  hanumeric charac
53130 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69 6c  ters, and no fil
53140 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68 65  e extension. The
53150 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  y are stored in 
53160 74 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74 61 6e  the.** OS's stan
53170 64 61 72 64 20 74 65 6d 70 6f 72 61 72 79 20 66  dard temporary f
53180 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2c 20 61  ile directory, a
53190 6e 64 20 61 72 65 20 64 65 6c 65 74 65 64 20 70  nd are deleted p
531a0 72 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a 2a 2a  rior to exit..**
531b0 20 49 66 20 73 71 6c 69 74 65 20 69 73 20 62 65   If sqlite is be
531c0 69 6e 67 20 65 6d 62 65 64 64 65 64 20 69 6e 20  ing embedded in 
531d0 61 6e 6f 74 68 65 72 20 70 72 6f 67 72 61 6d 2c  another program,
531e0 20 79 6f 75 20 6d 61 79 20 77 69 73 68 20 74 6f   you may wish to
531f0 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 70   change the.** p
53200 72 65 66 69 78 20 74 6f 20 72 65 66 6c 65 63 74  refix to reflect
53210 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 27 73 20   your program's 
53220 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74 20 69 66  name, so that if
53230 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 65 78   your program ex
53240 69 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75 72 65  its.** premature
53250 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 61 72  ly, old temporar
53260 79 20 66 69 6c 65 73 20 63 61 6e 20 62 65 20 65  y files can be e
53270 61 73 69 6c 79 20 69 64 65 6e 74 69 66 69 65 64  asily identified
53280 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 64 6f  . This can be do
53290 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 53 51  ne.** using -DSQ
532a0 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50  LITE_TEMP_FILE_P
532b0 52 45 46 49 58 3d 6d 79 70 72 65 66 69 78 5f 20  REFIX=myprefix_ 
532c0 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  on the compiler 
532d0 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a  command line..**
532e0 0a 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31 3a 20  .** 2006-10-31: 
532f0 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 72 65   The default pre
53300 66 69 78 20 75 73 65 64 20 74 6f 20 62 65 20 22  fix used to be "
53310 73 71 6c 69 74 65 5f 22 2e 20 20 42 75 74 20 74  sqlite_".  But t
53320 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20 73 74  hen.** Mcafee st
53330 61 72 74 65 64 20 75 73 69 6e 67 20 53 51 4c 69  arted using SQLi
53340 74 65 20 69 6e 20 74 68 65 69 72 20 61 6e 74 69  te in their anti
53350 2d 76 69 72 75 73 20 70 72 6f 64 75 63 74 20 61  -virus product a
53360 6e 64 20 69 74 0a 2a 2a 20 73 74 61 72 74 65 64  nd it.** started
53370 20 70 75 74 74 69 6e 67 20 66 69 6c 65 73 20 77   putting files w
53380 69 74 68 20 74 68 65 20 22 73 71 6c 69 74 65 22  ith the "sqlite"
53390 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 63 3a 2f   name in the c:/
533a0 74 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20  temp folder..** 
533b0 54 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d 61 6e  This annoyed man
533c0 79 20 77 69 6e 64 6f 77 73 20 75 73 65 72 73 2e  y windows users.
533d0 20 20 54 68 6f 73 65 20 75 73 65 72 73 20 77 6f    Those users wo
533e0 75 6c 64 20 74 68 65 6e 20 64 6f 20 61 20 0a 2a  uld then do a .*
533f0 2a 20 47 6f 6f 67 6c 65 20 73 65 61 72 63 68 20  * Google search 
53400 66 6f 72 20 22 73 71 6c 69 74 65 22 2c 20 66 69  for "sqlite", fi
53410 6e 64 20 74 68 65 20 74 65 6c 65 70 68 6f 6e 65  nd the telephone
53420 20 6e 75 6d 62 65 72 73 20 6f 66 20 74 68 65 0a   numbers of the.
53430 2a 2a 20 64 65 76 65 6c 6f 70 65 72 73 20 61 6e  ** developers an
53440 64 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65 20 74  d call to wake t
53450 68 65 6d 20 75 70 20 61 74 20 6e 69 67 68 74 20  hem up at night 
53460 61 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a  and complain..**
53470 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e   For this reason
53480 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e 61  , the default na
53490 6d 65 20 70 72 65 66 69 78 20 69 73 20 63 68 61  me prefix is cha
534a0 6e 67 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69  nged to be "sqli
534b0 74 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65 64 20  te" .** spelled 
534c0 62 61 63 6b 77 61 72 64 73 2e 20 20 53 6f 20 74  backwards.  So t
534d0 68 65 20 74 65 6d 70 20 66 69 6c 65 73 20 61 72  he temp files ar
534e0 65 20 73 74 69 6c 6c 20 69 64 65 6e 74 69 66 69  e still identifi
534f0 65 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79 62 6f  ed, but.** anybo
53500 64 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 68 20  dy smart enough 
53510 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68  to figure out th
53520 65 20 63 6f 64 65 20 69 73 20 61 6c 73 6f 20 6c  e code is also l
53530 69 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a 20 65  ikely smart.** e
53540 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 74 68  nough to know th
53550 61 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64  at calling the d
53560 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 6e 6f  eveloper will no
53570 74 20 68 65 6c 70 20 67 65 74 20 72 69 64 0a 2a  t help get rid.*
53580 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a  * of the file..*
53590 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
535a0 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49  _TEMP_FILE_PREFI
535b0 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  X.# define SQLIT
535c0 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
535d0 49 58 20 22 65 74 69 6c 71 73 5f 22 0a 23 65 6e  IX "etilqs_".#en
535e0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
535f0 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 20  ollowing values 
53600 6d 61 79 20 62 65 20 70 61 73 73 65 64 20 61 73  may be passed as
53610 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
53620 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69 74  ment to.** sqlit
53630 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68 65 20  e3OsLock(). The 
53640 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 65 78  various locks ex
53650 68 69 62 69 74 20 74 68 65 20 66 6f 6c 6c 6f 77  hibit the follow
53660 69 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a 0a 2a  ing semantics:.*
53670 2a 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20 20 20  *.** SHARED:    
53680 41 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 70 72  Any number of pr
53690 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64  ocesses may hold
536a0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 73   a SHARED lock s
536b0 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a  imultaneously..*
536c0 2a 20 52 45 53 45 52 56 45 44 3a 20 20 41 20 73  * RESERVED:  A s
536d0 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61  ingle process ma
536e0 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45  y hold a RESERVE
536f0 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65  D lock on a file
53700 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   at.**          
53710 20 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74 68 65    any time. Othe
53720 72 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20  r processes may 
53730 68 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69 6e 20  hold and obtain 
53740 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73  new SHARED locks
53750 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 20 20  ..** PENDING:   
53760 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73  A single process
53770 20 6d 61 79 20 68 6f 6c 64 20 61 20 50 45 4e 44   may hold a PEND
53780 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69  ING lock on a fi
53790 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20  le at.**        
537a0 20 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 6d 65      any one time
537b0 2e 20 45 78 69 73 74 69 6e 67 20 53 48 41 52 45  . Existing SHARE
537c0 44 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 72 73  D locks may pers
537d0 69 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 77 0a  ist, but no new.
537e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 53 48  **            SH
537f0 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 62  ARED locks may b
53800 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6f 74  e obtained by ot
53810 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a  her processes..*
53820 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41 6e 20  * EXCLUSIVE: An 
53830 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 70  EXCLUSIVE lock p
53840 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68  recludes all oth
53850 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  er locks..**.** 
53860 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79  PENDING_LOCK may
53870 20 6e 6f 74 20 62 65 20 70 61 73 73 65 64 20 64   not be passed d
53880 69 72 65 63 74 6c 79 20 74 6f 20 73 71 6c 69 74  irectly to sqlit
53890 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 73 74  e3OsLock(). Inst
538a0 65 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63 65 73  ead, a.** proces
538b0 73 20 74 68 61 74 20 72 65 71 75 65 73 74 73 20  s that requests 
538c0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
538d0 6b 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 6f  k may actually o
538e0 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 0a  btain a PENDING.
538f0 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20 63 61  ** lock. This ca
53900 6e 20 62 65 20 75 70 67 72 61 64 65 64 20 74 6f  n be upgraded to
53910 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
53920 63 6b 20 62 79 20 61 20 73 75 62 73 65 71 75 65  ck by a subseque
53930 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71  nt call to.** sq
53940 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a  lite3OsLock()..*
53950 2f 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43  /.#define NO_LOC
53960 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66  K         0.#def
53970 69 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ine SHARED_LOCK 
53980 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 52 45      1.#define RE
53990 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 32 0a  SERVED_LOCK   2.
539a0 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f  #define PENDING_
539b0 4c 4f 43 4b 20 20 20 20 33 0a 23 64 65 66 69 6e  LOCK    3.#defin
539c0 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  e EXCLUSIVE_LOCK
539d0 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20    4../*.** File 
539e0 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a 20 20  Locking Notes:  
539f0 28 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 77 69  (Mostly about wi
53a00 6e 64 6f 77 73 20 62 75 74 20 61 6c 73 6f 20 73  ndows but also s
53a10 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 6e 69  ome info for Uni
53a20 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e  x).**.** We cann
53a30 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c 65 45  ot use LockFileE
53a40 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c  x() or UnlockFil
53a50 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 2f 39  eEx() on Win95/9
53a60 38 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a 2a 20  8/ME because.** 
53a70 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  those functions 
53a80 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
53a90 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20 6f 6e  e.  So we use on
53aa0 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20 61 6e  ly LockFile() an
53ab0 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  d.** UnlockFile(
53ac0 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c  )..**.** LockFil
53ad0 65 28 29 20 70 72 65 76 65 6e 74 73 20 6e 6f 74  e() prevents not
53ae0 20 6a 75 73 74 20 77 72 69 74 69 6e 67 20 62 75   just writing bu
53af0 74 20 61 6c 73 6f 20 72 65 61 64 69 6e 67 20 62  t also reading b
53b00 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  y other processe
53b10 73 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44 5f 4c  s..** A SHARED_L
53b20 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  OCK is obtained 
53b30 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73 69 6e  by locking a sin
53b40 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f  gle randomly-cho
53b50 73 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f 75 74  sen .** byte out
53b60 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 20 72   of a specific r
53b70 61 6e 67 65 20 6f 66 20 62 79 74 65 73 2e 20 54  ange of bytes. T
53b80 68 65 20 6c 6f 63 6b 20 62 79 74 65 20 69 73 20  he lock byte is 
53b90 6f 62 74 61 69 6e 65 64 20 61 74 20 0a 2a 2a 20  obtained at .** 
53ba0 72 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20 73 65  random so two se
53bb0 70 61 72 61 74 65 20 72 65 61 64 65 72 73 20 63  parate readers c
53bc0 61 6e 20 70 72 6f 62 61 62 6c 79 20 61 63 63 65  an probably acce
53bd0 73 73 20 74 68 65 20 66 69 6c 65 20 61 74 20 74  ss the file at t
53be0 68 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65  he .** same time
53bf0 2c 20 75 6e 6c 65 73 73 20 74 68 65 79 20 61 72  , unless they ar
53c00 65 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20 63 68  e unlucky and ch
53c10 6f 6f 73 65 20 74 68 65 20 73 61 6d 65 20 6c 6f  oose the same lo
53c20 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e 20 45  ck byte..** An E
53c30 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 69 73  XCLUSIVE_LOCK is
53c40 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63   obtained by loc
53c50 6b 69 6e 67 20 61 6c 6c 20 62 79 74 65 73 20 69  king all bytes i
53c60 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2a 20  n the range..** 
53c70 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62  There can only b
53c80 65 20 6f 6e 65 20 77 72 69 74 65 72 2e 20 20 41  e one writer.  A
53c90 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 69   RESERVED_LOCK i
53ca0 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f  s obtained by lo
53cb0 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e 67 6c  cking.** a singl
53cc0 65 20 62 79 74 65 20 6f 66 20 74 68 65 20 66 69  e byte of the fi
53cd0 6c 65 20 74 68 61 74 20 69 73 20 64 65 73 69 67  le that is desig
53ce0 6e 61 74 65 64 20 61 73 20 74 68 65 20 72 65 73  nated as the res
53cf0 65 72 76 65 64 20 6c 6f 63 6b 20 62 79 74 65 2e  erved lock byte.
53d00 0a 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f 4c 4f  .** A PENDING_LO
53d10 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62  CK is obtained b
53d20 79 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65 73 69  y locking a desi
53d30 67 6e 61 74 65 64 20 62 79 74 65 20 64 69 66 66  gnated byte diff
53d40 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 68  erent from.** th
53d50 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  e RESERVED_LOCK 
53d60 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57  byte..**.** On W
53d70 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 74 65  inNT/2K/XP syste
53d80 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29  ms, LockFileEx()
53d90 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c 65 45   and UnlockFileE
53da0 78 28 29 20 61 72 65 20 61 76 61 69 6c 61 62 6c  x() are availabl
53db0 65 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e  e,.** which mean
53dc0 73 20 77 65 20 63 61 6e 20 75 73 65 20 72 65 61  s we can use rea
53dd0 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73  der/writer locks
53de0 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72 2f 77  .  When reader/w
53df0 72 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61  riter locks.** a
53e00 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c 6f 63  re used, the loc
53e10 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e 20 74  k is placed on t
53e20 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20 6f 66  he same range of
53e30 20 62 79 74 65 73 20 74 68 61 74 20 69 73 20 75   bytes that is u
53e40 73 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f 62 61  sed.** for proba
53e50 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69 6e 67  bilistic locking
53e60 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e   in Win95/98/ME.
53e70 20 20 48 65 6e 63 65 2c 20 74 68 65 20 6c 6f 63    Hence, the loc
53e80 6b 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a 20 77  king scheme.** w
53e90 69 6c 6c 20 73 75 70 70 6f 72 74 20 74 77 6f 20  ill support two 
53ea0 6f 72 20 6d 6f 72 65 20 57 69 6e 39 35 20 72 65  or more Win95 re
53eb0 61 64 65 72 73 20 6f 72 20 74 77 6f 20 6f 72 20  aders or two or 
53ec0 6d 6f 72 65 20 57 69 6e 4e 54 20 72 65 61 64 65  more WinNT reade
53ed0 72 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73 69 6e  rs..** But a sin
53ee0 67 6c 65 20 57 69 6e 39 35 20 72 65 61 64 65 72  gle Win95 reader
53ef0 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61   will lock out a
53f00 6c 6c 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73  ll WinNT readers
53f10 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a 2a 2a   and a single.**
53f20 20 57 69 6e 4e 54 20 72 65 61 64 65 72 20 77 69   WinNT reader wi
53f30 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20  ll lock out all 
53f40 6f 74 68 65 72 20 57 69 6e 39 35 20 72 65 61 64  other Win95 read
53f50 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ers..**.** The f
53f60 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69 6e 65  ollowing #define
53f70 73 20 73 70 65 63 69 66 79 20 74 68 65 20 72 61  s specify the ra
53f80 6e 67 65 20 6f 66 20 62 79 74 65 73 20 75 73 65  nge of bytes use
53f90 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a  d for locking..*
53fa0 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73  * SHARED_SIZE is
53fb0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
53fc0 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 69  ytes available i
53fd0 6e 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f 6d 20  n the pool from 
53fe0 77 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e 64 6f  which.** a rando
53ff0 6d 20 62 79 74 65 20 69 73 20 73 65 6c 65 63 74  m byte is select
54000 65 64 20 66 6f 72 20 61 20 73 68 61 72 65 64 20  ed for a shared 
54010 6c 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f 6c 20  lock.  The pool 
54020 6f 66 20 62 79 74 65 73 20 66 6f 72 0a 2a 2a 20  of bytes for.** 
54030 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 62 65 67  shared locks beg
54040 69 6e 73 20 61 74 20 53 48 41 52 45 44 5f 46 49  ins at SHARED_FI
54050 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  RST. .**.** The 
54060 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72  same locking str
54070 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 79 74  ategy and.** byt
54080 65 20 72 61 6e 67 65 73 20 61 72 65 20 75 73 65  e ranges are use
54090 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 68 69  d for Unix.  Thi
540a0 73 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 74 68  s leaves open th
540b0 65 20 70 6f 73 73 69 62 6c 69 74 79 20 6f 66 20  e possiblity of 
540c0 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74  having.** client
540d0 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e 4e  s on win95, winN
540e0 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c 20  T, and unix all 
540f0 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20 73  talking to the s
54100 61 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65 0a  ame shared file.
54110 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69  ** and all locki
54120 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54  ng correctly.  T
54130 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72 65  o do so would re
54140 71 75 69 72 65 20 74 68 61 74 20 73 61 6d 62 61  quire that samba
54150 20 28 6f 72 20 77 68 61 74 65 76 65 72 0a 2a 2a   (or whatever.**
54160 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20 75   tool is being u
54170 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68 61  sed for file sha
54180 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74 73  ring) implements
54190 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c 79   locks correctly
541a0 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 6e 64   between.** wind
541b0 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 20 49  ows and unix.  I
541c0 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68 61 74  'm guessing that
541d0 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 74 6f   isn't likely to
541e0 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62 79 0a   happen, but by.
541f0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  ** using the sam
54200 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20  e locking range 
54210 77 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20  we are at least 
54220 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 6f 73 73  open to the poss
54230 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c  ibility..**.** L
54240 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 6f 77  ocking in window
54250 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 2e 20  s is manditory. 
54260 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e   For this reason
54270 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 72  , we cannot stor
54280 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 74 61  e.** actual data
54290 20 69 6e 20 74 68 65 20 62 79 74 65 73 20 75 73   in the bytes us
542a0 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20  ed for locking. 
542b0 20 54 68 65 20 70 61 67 65 72 20 6e 65 76 65 72   The pager never
542c0 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 74 68   allocates.** th
542d0 65 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64  e pages involved
542e0 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65 72   in locking ther
542f0 65 66 6f 72 65 2e 20 20 53 48 41 52 45 44 5f 53  efore.  SHARED_S
54300 49 5a 45 20 69 73 20 73 65 6c 65 63 74 65 64 20  IZE is selected 
54310 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 6c  so.** that all l
54320 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 6f 6e  ocks will fit on
54330 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 65   a single page e
54340 76 65 6e 20 61 74 20 74 68 65 20 6d 69 6e 69 6d  ven at the minim
54350 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a  um page size..**
54360 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 64 65   PENDING_BYTE de
54370 66 69 6e 65 73 20 74 68 65 20 62 65 67 69 6e 6e  fines the beginn
54380 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73  ing of the locks
54390 2e 20 20 42 79 20 64 65 66 61 75 6c 74 20 50 45  .  By default PE
543a0 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 69 73  NDING_BYTE.** is
543b0 20 73 65 74 20 68 69 67 68 20 73 6f 20 74 68 61   set high so tha
543c0 74 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65 20  t we don't have 
543d0 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20 75  to allocate an u
543e0 6e 75 73 65 64 20 70 61 67 65 20 65 78 63 65 70  nused page excep
543f0 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 6c 61  t.** for very la
54400 72 67 65 20 64 61 74 61 62 61 73 65 73 2e 20 20  rge databases.  
54410 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 20 74  But one should t
54420 65 73 74 20 74 68 65 20 70 61 67 65 20 73 6b 69  est the page ski
54430 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a 2a 20  pping logic .** 
54440 62 79 20 73 65 74 74 69 6e 67 20 50 45 4e 44 49  by setting PENDI
54450 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e 64 20  NG_BYTE low and 
54460 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 69  running the enti
54470 72 65 20 72 65 67 72 65 73 73 69 6f 6e 20 73 75  re regression su
54480 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67  ite..**.** Chang
54490 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ing the value of
544a0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 72 65   PENDING_BYTE re
544b0 73 75 6c 74 73 20 69 6e 20 61 20 73 75 62 74 6c  sults in a subtl
544c0 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0a 2a  y incompatible.*
544d0 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20  * file format.  
544e0 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77  Depending on how
544f0 20 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c 20   it is changed, 
54500 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e 6f  you might not no
54510 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e 63 6f  tice.** the inco
54520 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69 67 68  mpatibility righ
54530 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75 6e  t away, even run
54540 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67 72  ning a full regr
54550 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20  ession test..** 
54560 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 61  The default loca
54570 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47 5f  tion of PENDING_
54580 42 59 54 45 20 69 73 20 74 68 65 20 66 69 72 73  BYTE is the firs
54590 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a  t byte past the.
545a0 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2e  ** 1GB boundary.
545b0 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  .**.*/.#define P
545c0 45 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20  ENDING_BYTE     
545d0 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42   sqlite3PendingB
545e0 79 74 65 0a 23 64 65 66 69 6e 65 20 52 45 53 45  yte.#define RESE
545f0 52 56 45 44 5f 42 59 54 45 20 20 20 20 20 28 50  RVED_BYTE     (P
54600 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 0a 23  ENDING_BYTE+1).#
54610 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 46 49  define SHARED_FI
54620 52 53 54 20 20 20 20 20 20 28 50 45 4e 44 49 4e  RST      (PENDIN
54630 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 69 6e  G_BYTE+2).#defin
54640 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20 20 20  e SHARED_SIZE   
54650 20 20 20 20 35 31 30 0a 0a 2f 2a 20 0a 2a 2a 20      510../* .** 
54660 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63  Functions for ac
54670 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  cessing sqlite3_
54680 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a 2a 2f  file methods .*/
54690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
546a0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f  int sqlite3OsClo
546b0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  se(sqlite3_file*
546c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
546d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52  E int sqlite3OsR
546e0 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ead(sqlite3_file
546f0 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d  *, void*, int am
54700 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a  t, i64 offset);.
54710 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
54720 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74  nt sqlite3OsWrit
54730 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  e(sqlite3_file*,
54740 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
54750 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65  t amt, i64 offse
54760 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
54770 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
54780 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33  Truncate(sqlite3
54790 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69 7a 65  _file*, i64 size
547a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
547b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53  E int sqlite3OsS
547c0 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ync(sqlite3_file
547d0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
547e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
547f0 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71  te3OsFileSize(sq
54800 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34  lite3_file*, i64
54810 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45   *pSize);.SQLITE
54820 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
54830 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 74  ite3OsLock(sqlit
54840 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a  e3_file*, int);.
54850 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
54860 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  nt sqlite3OsUnlo
54870 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ck(sqlite3_file*
54880 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
54890 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
548a0 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
548b0 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  dLock(sqlite3_fi
548c0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65  le *id, int *pRe
548d0 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sOut);.SQLITE_PR
548e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
548f0 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73  3OsFileControl(s
54900 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e 74  qlite3_file*,int
54910 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65  ,void*);.#define
54920 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42   SQLITE_FCNTL_DB
54930 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63 61 30  _UNCHANGED 0xca0
54940 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50 52 49  93fa0.SQLITE_PRI
54950 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
54960 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  OsSectorSize(sql
54970 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a  ite3_file *id);.
54980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
54990 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69  nt sqlite3OsDevi
549a0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
549b0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
549c0 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e  id);../* .** Fun
549d0 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73  ctions for acces
549e0 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76 66 73  sing sqlite3_vfs
549f0 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c   methods .*/.SQL
54a00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54a10 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 73 71  sqlite3OsOpen(sq
54a20 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
54a30 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74  st char *, sqlit
54a40 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c 20 69  e3_file*, int, i
54a50 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt *);.SQLITE_PR
54a60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
54a70 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65  3OsDelete(sqlite
54a80 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63  3_vfs *, const c
54a90 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  har *, int);.SQL
54aa0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54ab0 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
54ac0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63  sqlite3_vfs *, c
54ad0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
54ae0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b  , int *pResOut);
54af0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54b00 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c  int sqlite3OsFul
54b10 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69 74 65  lPathname(sqlite
54b20 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63  3_vfs *, const c
54b30 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72  har *, int, char
54b40 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   *);.#ifndef SQL
54b50 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
54b60 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50  TENSION.SQLITE_P
54b70 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
54b80 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c  ite3OsDlOpen(sql
54b90 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73  ite3_vfs *, cons
54ba0 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  t char *);.SQLIT
54bb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
54bc0 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28  qlite3OsDlError(
54bd0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69  sqlite3_vfs *, i
54be0 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nt, char *);.SQL
54bf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
54c00 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79   (*sqlite3OsDlSy
54c10 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  m(sqlite3_vfs *,
54c20 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63   void *, const c
54c30 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b 0a 53  har *))(void);.S
54c40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
54c50 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c  id sqlite3OsDlCl
54c60 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ose(sqlite3_vfs 
54c70 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65 6e 64  *, void *);.#end
54c80 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
54c90 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
54ca0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
54cb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
54cc0 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74  Randomness(sqlit
54cd0 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63  e3_vfs *, int, c
54ce0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har *);.SQLITE_P
54cf0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54d00 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65  e3OsSleep(sqlite
54d10 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b 0a 53  3_vfs *, int);.S
54d20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54d30 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65  t sqlite3OsCurre
54d40 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76  ntTime(sqlite3_v
54d50 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a  fs *, double*);.
54d60 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e  ./*.** Convenien
54d70 63 65 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72  ce functions for
54d80 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20 63 6c 6f   opening and clo
54d90 73 69 6e 67 20 66 69 6c 65 73 20 75 73 69 6e 67  sing files using
54da0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c   .** sqlite3_mal
54db0 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61 69 6e 20  loc() to obtain 
54dc0 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 66 69  space for the fi
54dd0 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72 75 63 74  le-handle struct
54de0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ure..*/.SQLITE_P
54df0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54e00 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73  e3OsOpenMalloc(s
54e10 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f  qlite3_vfs *, co
54e20 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69  nst char *, sqli
54e30 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 6e 74  te3_file **, int
54e40 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ,int*);.SQLITE_P
54e50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54e60 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71  e3OsCloseFree(sq
54e70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a  lite3_file *);..
54e80 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54  #endif /* _SQLIT
54e90 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  E_OS_H_ */../***
54ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
54eb0 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  of os.h ********
54ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
54ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
54f00 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
54f10 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
54f20 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
54f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
54f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
54f50 75 64 65 20 6d 75 74 65 78 2e 68 20 69 6e 20 74  ude mutex.h in t
54f60 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
54f70 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
54f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
54f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
54fa0 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 68 20 2a  n file mutex.h *
54fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
54fe0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38  * 2007 August 28
54ff0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
55000 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
55010 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
55020 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
55030 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
55040 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
55050 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
55060 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
55070 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
55080 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
55090 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
550a0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
550b0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
550c0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
550d0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
550e0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
550f0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
55100 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
55110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
55150 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
55160 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d  contains the com
55170 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f 72 20 61  mon header for a
55180 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65  ll mutex impleme
55190 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65  ntations..** The
551a0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 68 65 61   sqliteInt.h hea
551b0 64 65 72 20 23 69 6e 63 6c 75 64 65 73 20 74 68  der #includes th
551c0 69 73 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20  is file so that 
551d0 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a  it is available.
551e0 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75 72 63 65  ** to all source
551f0 20 66 69 6c 65 73 2e 20 20 57 65 20 62 72 65 61   files.  We brea
55200 6b 20 69 74 20 6f 75 74 20 69 6e 20 61 6e 20 65  k it out in an e
55210 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 74 68  ffort to keep th
55220 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74 74 65 72  e code.** better
55230 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a   organized..**.*
55240 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 63 65 20  * NOTE:  source 
55250 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 2a 6e 6f  files should *no
55260 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73  t* #include this
55270 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 69 72   header file dir
55280 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72 63 65  ectly..** Source
55290 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 23 69   files should #i
552a0 6e 63 6c 75 64 65 20 74 68 65 20 73 71 6c 69 74  nclude the sqlit
552b0 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 6e 64 20  eInt.h file and 
552c0 6c 65 74 20 74 68 61 74 20 66 69 6c 65 0a 2a 2a  let that file.**
552d0 20 69 6e 63 6c 75 64 65 20 74 68 69 73 20 6f 6e   include this on
552e0 65 20 69 6e 64 69 72 65 63 74 6c 79 2e 0a 2a 2a  e indirectly..**
552f0 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 68  .** $Id: mutex.h
55300 2c 76 20 31 2e 39 20 32 30 30 38 2f 31 30 2f 30  ,v 1.9 2008/10/0
55310 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45  7 15:25:48 drh E
55320 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  xp $.*/.../*.** 
55330 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74 20  Figure out what 
55340 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 63  version of the c
55350 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68 65  ode to use.  The
55360 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a 0a   choices are.**.
55370 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  **   SQLITE_MUTE
55380 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20 20 4e  X_OMIT         N
55390 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20 20  o mutex logic.  
553a0 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73 2e 20  Not even stubs. 
553b0 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   The.**         
553c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
553d0 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d 70 6c      mutexes impl
553e0 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  emention cannot 
553f0 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a 2a  be overridden.**
55400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55410 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74 20               at 
55420 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a  start-time..**.*
55430 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  *   SQLITE_MUTEX
55440 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20 46 6f  _NOOP         Fo
55450 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65  r single-threade
55460 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20  d applications. 
55470 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   No.**          
55480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55490 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73     mutual exclus
554a0 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 2e  ion is provided.
554b0 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20 20 20    But this.**   
554c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
554d0 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c 65 6d            implem
554e0 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62 65 20  entation can be 
554f0 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a 2a 2a  overridden at.**
55500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55510 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
55520 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20  rt-time..**.**  
55530 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54   SQLITE_MUTEX_PT
55540 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 20 6d  HREADS     For m
55550 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70  ulti-threaded ap
55560 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55 6e  plications on Un
55570 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  ix..**.**   SQLI
55580 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 20  TE_MUTEX_W32    
55590 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d        For multi-
555a0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
555b0 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e 0a  tions on Win32..
555c0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
555d0 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20 20  UTEX_OS2        
555e0 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65    For multi-thre
555f0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  aded application
55600 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69  s on OS/2..*/.#i
55610 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45 41 44  f !SQLITE_THREAD
55620 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SAFE.# define SQ
55630 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a  LITE_MUTEX_OMIT.
55640 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
55650 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
55660 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
55670 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20 69  MUTEX_NOOP).#  i
55680 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  f SQLITE_OS_UNIX
55690 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  .#    define SQL
556a0 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
556b0 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54  DS.#  elif SQLIT
556c0 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64 65  E_OS_WIN.#    de
556d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
556e0 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20 53 51  X_W32.#  elif SQ
556f0 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20  LITE_OS_OS2.#   
55700 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
55710 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73 65  UTEX_OS2.#  else
55720 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  .#    define SQL
55730 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23  ITE_MUTEX_NOOP.#
55740 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a    endif.#endif..
55750 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  #ifdef SQLITE_MU
55760 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49  TEX_OMIT./*.** I
55770 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  f this is a no-o
55780 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  p implementation
55790 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65 72  , implement ever
557a0 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72 6f 73  ything as macros
557b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c  ..*/.#define sql
557c0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
557d0 28 58 29 20 20 20 20 28 28 73 71 6c 69 74 65 33  (X)    ((sqlite3
557e0 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69  _mutex*)8).#defi
557f0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
55800 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69 6e 65  _free(X).#define
55810 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
55820 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e 65 20  nter(X).#define 
55830 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
55840 79 28 58 29 20 20 20 20 20 20 53 51 4c 49 54 45  y(X)      SQLITE
55850 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _OK.#define sqli
55860 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
55870 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  X).#define sqlit
55880 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 29  e3_mutex_held(X)
55890 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 73       1.#define s
558a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
558b0 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65 66 69  held(X)  1.#defi
558c0 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ne sqlite3MutexA
558d0 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28 28 73  lloc(X)      ((s
558e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29  qlite3_mutex*)8)
558f0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
55900 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20 20 20  MutexInit()     
55910 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65     SQLITE_OK.#de
55920 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65  fine sqlite3Mute
55930 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f 2a  xEnd().#endif /*
55940 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
55950 4f 4d 49 54 5f 4d 55 54 45 58 29 20 2a 2f 0a 0a  OMIT_MUTEX) */..
55960 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
55970 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68 20 2a  End of mutex.h *
55980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
559a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
559b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
559c0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
559d0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
559e0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
559f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
55a00 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74  ../*.** Each dat
55a10 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 62 65  abase file to be
55a20 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65   accessed by the
55a30 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 69 6e   system is an in
55a40 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65  stance.** of the
55a50 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
55a60 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 72 65  ture.  There are
55a70 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f 66   normally two of
55a80 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65   these structure
55a90 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69  s.** in the sqli
55aa0 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e 20  te.aDb[] array. 
55ab0 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 20 6d   aDb[0] is the m
55ac0 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
55ad0 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d 20  e and.** aDb[1] 
55ae0 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
55af0 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 6f 6c  file used to hol
55b00 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  d temporary tabl
55b10 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 0a  es.  Additional.
55b20 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d 61 79  ** databases may
55b30 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a 2a 2f   be attached..*/
55b40 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 20 63  .struct Db {.  c
55b50 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
55b60 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
55b70 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  his database */.
55b80 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20    Btree *pBt;   
55b90 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 2a         /* The B*
55ba0 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 66  Tree structure f
55bb0 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65  or this database
55bc0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e   file */.  u8 in
55bd0 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20  Trans;          
55be0 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 61 62  /* 0: not writab
55bf0 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 63 74  le.  1: Transact
55c00 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70 6f  ion.  2: Checkpo
55c10 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 66 65  int */.  u8 safe
55c20 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f 2a  ty_level;     /*
55c30 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 65 20   How aggressive 
55c40 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74 61 20  at syncing data 
55c50 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 76 6f 69  to disk */.  voi
55c60 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20  d *pAux;        
55c70 20 20 20 20 20 20 20 2f 2a 20 41 75 78 69 6c 69         /* Auxili
55c80 61 72 79 20 64 61 74 61 2e 20 20 55 73 75 61 6c  ary data.  Usual
55c90 6c 79 20 4e 55 4c 4c 20 2a 2f 0a 20 20 76 6f 69  ly NULL */.  voi
55ca0 64 20 28 2a 78 46 72 65 65 41 75 78 29 28 76 6f  d (*xFreeAux)(vo
55cb0 69 64 2a 29 3b 20 20 2f 2a 20 52 6f 75 74 69 6e  id*);  /* Routin
55cc0 65 20 74 6f 20 66 72 65 65 20 70 41 75 78 20 2a  e to free pAux *
55cd0 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  /.  Schema *pSch
55ce0 65 6d 61 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ema;     /* Poin
55cf0 74 65 72 20 74 6f 20 64 61 74 61 62 61 73 65 20  ter to database 
55d00 73 63 68 65 6d 61 20 28 70 6f 73 73 69 62 6c 79  schema (possibly
55d10 20 73 68 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a   shared) */.};..
55d20 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
55d30 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
55d40 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 74 6f  ng structure sto
55d50 72 65 73 20 61 20 64 61 74 61 62 61 73 65 20 73  res a database s
55d60 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  chema..**.** If 
55d70 74 68 65 72 65 20 61 72 65 20 6e 6f 20 76 69 72  there are no vir
55d80 74 75 61 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66  tual tables conf
55d90 69 67 75 72 65 64 20 69 6e 20 74 68 69 73 20 73  igured in this s
55da0 63 68 65 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63  chema, the.** Sc
55db0 68 65 6d 61 2e 64 62 20 76 61 72 69 61 62 6c 65  hema.db variable
55dc0 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
55dd0 20 41 66 74 65 72 20 74 68 65 20 66 69 72 73 74   After the first
55de0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a   virtual table.*
55df0 2a 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 64  * has been added
55e00 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 70  , it is set to p
55e10 6f 69 6e 74 20 74 6f 20 74 68 65 20 64 61 74 61  oint to the data
55e20 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
55e30 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65 61  .** used to crea
55e40 74 65 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  te the connectio
55e50 6e 2e 20 4f 6e 63 65 20 61 20 76 69 72 74 75 61  n. Once a virtua
55e60 6c 20 74 61 62 6c 65 20 68 61 73 20 62 65 65 6e  l table has been
55e70 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65  .** added to the
55e80 20 53 63 68 65 6d 61 20 73 74 72 75 63 74 75 72   Schema structur
55e90 65 20 61 6e 64 20 74 68 65 20 53 63 68 65 6d 61  e and the Schema
55ea0 2e 64 62 20 76 61 72 69 61 62 6c 65 20 70 6f 70  .db variable pop
55eb0 75 6c 61 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79  ulated, .** only
55ec0 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63   that database c
55ed0 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 75 73  onnection may us
55ee0 65 20 74 68 65 20 53 63 68 65 6d 61 20 74 6f 20  e the Schema to 
55ef0 70 72 65 70 61 72 65 20 0a 2a 2a 20 73 74 61 74  prepare .** stat
55f00 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63  ements..*/.struc
55f10 74 20 53 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74  t Schema {.  int
55f20 20 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20   schema_cookie; 
55f30 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73 63    /* Database sc
55f40 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d  hema version num
55f50 62 65 72 20 66 6f 72 20 74 68 69 73 20 66 69 6c  ber for this fil
55f60 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 62 6c 48  e */.  Hash tblH
55f70 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ash;        /* A
55f80 6c 6c 20 74 61 62 6c 65 73 20 69 6e 64 65 78 65  ll tables indexe
55f90 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48  d by name */.  H
55fa0 61 73 68 20 69 64 78 48 61 73 68 3b 20 20 20 20  ash idxHash;    
55fb0 20 20 20 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65      /* All (name
55fc0 64 29 20 69 6e 64 69 63 65 73 20 69 6e 64 65 78  d) indices index
55fd0 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20  ed by name */.  
55fe0 48 61 73 68 20 74 72 69 67 48 61 73 68 3b 20 20  Hash trigHash;  
55ff0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67       /* All trig
56000 67 65 72 73 20 69 6e 64 65 78 65 64 20 62 79 20  gers indexed by 
56010 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20  name */.  Table 
56020 2a 70 53 65 71 54 61 62 3b 20 20 20 20 20 20 2f  *pSeqTab;      /
56030 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 65 71  * The sqlite_seq
56040 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64  uence table used
56050 20 62 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e   by AUTOINCREMEN
56060 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66  T */.  u8 file_f
56070 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53  ormat;      /* S
56080 63 68 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72  chema format ver
56090 73 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69  sion for this fi
560a0 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20  le */.  u8 enc; 
560b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
560c0 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73  Text encoding us
560d0 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 62  ed by this datab
560e0 61 73 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  ase */.  u16 fla
560f0 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gs;           /*
56100 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65   Flags associate
56110 64 20 77 69 74 68 20 74 68 69 73 20 73 63 68 65  d with this sche
56120 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68  ma */.  int cach
56130 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20  e_size;      /* 
56140 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
56150 74 6f 20 75 73 65 20 69 6e 20 74 68 65 20 63 61  to use in the ca
56160 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  che */.#ifndef S
56170 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
56180 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65  ALTABLE.  sqlite
56190 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f  3 *db;         /
561a0 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63  * "Owner" connec
561b0 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e  tion. See commen
561c0 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69  t above */.#endi
561d0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  f.};../*.** Thes
561e0 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20  e macros can be 
561f0 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65  used to test, se
56200 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73  t, or clear bits
56210 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66   in the .** Db.f
56220 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
56230 64 65 66 69 6e 65 20 44 62 48 61 73 50 72 6f 70  define DbHasProp
56240 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20  erty(D,I,P)     
56250 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53  (((D)->aDb[I].pS
56260 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29  chema->flags&(P)
56270 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20  )==(P)).#define 
56280 44 62 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  DbHasAnyProperty
56290 28 44 2c 49 2c 50 29 20 20 28 28 28 44 29 2d 3e  (D,I,P)  (((D)->
562a0 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[I].pSchema->
562b0 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23  flags&(P))!=0).#
562c0 64 65 66 69 6e 65 20 44 62 53 65 74 50 72 6f 70  define DbSetProp
562d0 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20  erty(D,I,P)     
562e0 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68  (D)->aDb[I].pSch
562f0 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a  ema->flags|=(P).
56300 23 64 65 66 69 6e 65 20 44 62 43 6c 65 61 72 50  #define DbClearP
56310 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20  roperty(D,I,P)  
56320 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63   (D)->aDb[I].pSc
56330 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50  hema->flags&=~(P
56340 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  )../*.** Allowed
56350 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
56360 44 42 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a  DB.flags field..
56370 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68  **.** The DB_Sch
56380 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69  emaLoaded flag i
56390 73 20 73 65 74 20 61 66 74 65 72 20 74 68 65 20  s set after the 
563a0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
563b0 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64  has been.** read
563c0 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68   into internal h
563d0 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a  ash tables..**.*
563e0 2a 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  * DB_UnresetView
563f0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65  s means that one
56400 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68   or more views h
56410 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  ave column names
56420 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65   that.** have be
56430 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20  en filled out.  
56440 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 63 68  If the schema ch
56450 61 6e 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c  anges, these col
56460 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a  umn names might.
56470 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73  ** changes and s
56480 6f 20 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20  o the view will 
56490 6e 65 65 64 20 74 6f 20 62 65 20 72 65 73 65 74  need to be reset
564a0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f  ..*/.#define DB_
564b0 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20  SchemaLoaded    
564c0 30 78 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73  0x0001  /* The s
564d0 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c  chema has been l
564e0 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  oaded */.#define
564f0 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73   DB_UnresetViews
56500 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53      0x0002  /* S
56510 6f 6d 65 20 76 69 65 77 73 20 68 61 76 65 20 64  ome views have d
56520 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61  efined column na
56530 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44  mes */.#define D
56540 42 5f 45 6d 70 74 79 20 20 20 20 20 20 20 20 20  B_Empty         
56550 20 20 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65    0x0004  /* The
56560 20 66 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28   file is empty (
56570 6c 65 6e 67 74 68 20 30 20 62 79 74 65 73 29 20  length 0 bytes) 
56580 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75  */../*.** The nu
56590 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e  mber of differen
565a0 74 20 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67  t kinds of thing
565b0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69  s that can be li
565c0 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74  mited.** using t
565d0 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  he sqlite3_limit
565e0 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  () interface..*/
565f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
56600 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f  N_LIMIT (SQLITE_
56610 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
56620 55 4d 42 45 52 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20  UMBER+1)../*.** 
56630 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63  Lookaside malloc
56640 20 69 73 20 61 20 73 65 74 20 6f 66 20 66 69 78   is a set of fix
56650 65 64 2d 73 69 7a 65 20 62 75 66 66 65 72 73 20  ed-size buffers 
56660 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64  that can be used
56670 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66 79 20 73  .** to satisfy s
56680 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20 6d  mall transient m
56690 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
566a0 20 72 65 71 75 65 73 74 73 20 66 6f 72 20 6f 62   requests for ob
566b0 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69 61  jects.** associa
566c0 74 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69  ted with a parti
566d0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63  cular database c
566e0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20  onnection.  The 
566f0 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73  use of.** lookas
56700 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76 69  ide malloc provi
56710 64 65 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e  des a significan
56720 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 65 6e  t performance en
56730 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61 70  hancement.** (ap
56740 70 72 6f 78 20 31 30 25 29 20 62 79 20 61 76 6f  prox 10%) by avo
56750 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20 6d  iding numerous m
56760 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71 75 65  alloc/free reque
56770 73 74 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e  sts while parsin
56780 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65  g.** SQL stateme
56790 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c  nts..**.** The L
567a0 6f 6f 6b 61 73 69 64 65 20 73 74 72 75 63 74 75  ookaside structu
567b0 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 75  re holds configu
567c0 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  ration informati
567d0 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20  on about the.** 
567e0 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63  lookaside malloc
567f0 20 73 75 62 73 79 73 74 65 6d 2e 20 20 45 61 63   subsystem.  Eac
56800 68 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f  h available memo
56810 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e  ry allocation in
56820 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64  .** the lookasid
56830 65 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 73  e subsystem is s
56840 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b 65  tored on a linke
56850 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 73  d list of Lookas
56860 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63  ideSlot.** objec
56870 74 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73  ts..**.** Lookas
56880 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ide allocations 
56890 61 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64  are only allowed
568a0 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 74 68 61   for objects tha
568b0 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64  t are associated
568c0 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 69  .** with a parti
568d0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63  cular database c
568e0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e 63  onnection.  Henc
568f0 65 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d  e, schema inform
56900 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20  ation cannot.** 
56910 62 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 6f  be stored in loo
56920 6b 61 73 69 64 65 20 62 65 63 61 75 73 65 20 69  kaside because i
56930 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 20 6d  n shared cache m
56940 6f 64 65 20 74 68 65 20 73 63 68 65 6d 61 20 69  ode the schema i
56950 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73  nformation.** is
56960 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c 74 69   shared by multi
56970 70 6c 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ple database con
56980 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 72 65  nections.  There
56990 66 6f 72 65 2c 20 77 68 69 6c 65 20 70 61 72 73  fore, while pars
569a0 69 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69 6e  ing.** schema in
569b0 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20 4c  formation, the L
569c0 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
569d0 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65  d flag is cleare
569e0 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f  d so that.** loo
569f0 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f  kaside allocatio
56a00 6e 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20  ns are not used 
56a10 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65  to construct the
56a20 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 2e   schema objects.
56a30 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61  .*/.struct Looka
56a40 73 69 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b  side {.  u16 sz;
56a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
56a60 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68   /* Size of each
56a70 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73   buffer in bytes
56a80 20 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65   */.  u8 bEnable
56a90 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  d;            /*
56aa0 20 46 61 6c 73 65 20 74 6f 20 64 69 73 61 62 6c   False to disabl
56ab0 65 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65 20  e new lookaside 
56ac0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20  allocations */. 
56ad0 20 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20   u8 bMalloced;  
56ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
56af0 20 69 66 20 70 53 74 61 72 74 20 6f 62 74 61 69   if pStart obtai
56b00 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
56b10 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69  _malloc() */.  i
56b20 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20  nt nOut;        
56b30 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
56b40 20 6f 66 20 62 75 66 66 65 72 73 20 63 75 72 72   of buffers curr
56b50 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75  ently checked ou
56b60 74 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74  t */.  int mxOut
56b70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
56b80 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b  * Highwater mark
56b90 20 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c   for nOut */.  L
56ba0 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46  ookasideSlot *pF
56bb0 72 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f  ree;   /* List o
56bc0 66 20 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66  f available buff
56bd0 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ers */.  void *p
56be0 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20  Start;          
56bf0 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f   /* First byte o
56c00 66 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f  f available memo
56c10 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f  ry space */.  vo
56c20 69 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20  id *pEnd;       
56c30 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62        /* First b
56c40 79 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20  yte past end of 
56c50 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20  available space 
56c60 2a 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f  */.};.struct Loo
56c70 6b 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c  kasideSlot {.  L
56c80 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e  ookasideSlot *pN
56c90 65 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20  ext;    /* Next 
56ca0 62 75 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69  buffer in the li
56cb0 73 74 20 6f 66 20 66 72 65 65 20 62 75 66 66 65  st of free buffe
56cc0 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  rs */.};../*.** 
56cd0 41 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72  A hash table for
56ce0 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
56cf0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73  tions..**.** Has
56d00 68 20 65 61 63 68 20 46 75 6e 63 44 65 66 20 73  h each FuncDef s
56d10 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e  tructure into on
56d20 65 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66  e of the FuncDef
56d30 48 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a  Hash.a[] slots..
56d40 2a 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72  ** Collisions ar
56d50 65 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66  e on the FuncDef
56d60 2e 70 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f  .pHash chain..*/
56d70 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 48  .struct FuncDefH
56d80 61 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20  ash {.  FuncDef 
56d90 2a 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a  *a[23];       /*
56da0 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20   Hash table for 
56db0 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a  functions */.};.
56dc0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61  ./*.** Each data
56dd0 62 61 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61  base is an insta
56de0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
56df0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
56e00 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
56e10 2e 6c 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72  .lastRowid recor
56e20 64 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65  ds the last inse
56e30 72 74 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74  rt rowid generat
56e40 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65  ed by an.** inse
56e50 72 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49  rt statement.  I
56e60 6e 73 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20  nserts on views 
56e70 64 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74  do not affect it
56e80 73 20 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a  s value.  Each.*
56e90 2a 20 74 72 69 67 67 65 72 20 68 61 73 20 69 74  * trigger has it
56ea0 73 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73  s own context, s
56eb0 6f 20 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64  o that lastRowid
56ec0 20 63 61 6e 20 62 65 20 75 70 64 61 74 65 64 20   can be updated 
56ed0 69 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65  inside.** trigge
56ee0 72 73 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68  rs as usual.  Th
56ef0 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  e previous value
56f00 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65   will be restore
56f10 64 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67  d once the trigg
56f20 65 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70  er.** exits.  Up
56f30 6f 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65  on entering a be
56f40 66 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20  fore or instead 
56f50 6f 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74  of trigger, last
56f60 52 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c  Rowid is no.** l
56f70 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66 74  onger (since aft
56f80 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31  er version 2.8.1
56f90 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a  2) reset to -1..
56fa0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
56fb0 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f  .nChange does no
56fc0 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20  t count changes 
56fd0 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20  within triggers 
56fe0 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20  and keeps no.** 
56ff0 63 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20  context.  It is 
57000 72 65 73 65 74 20 61 74 20 73 74 61 72 74 20 6f  reset at start o
57010 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a  f sqlite3_exec..
57020 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73  ** The sqlite.ls
57030 43 68 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74  Change represent
57040 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
57050 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20  changes made by 
57060 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65  the last.** inse
57070 72 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64  rt, update, or d
57080 65 6c 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e  elete statement.
57090 20 20 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e    It remains con
570a0 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74  stant throughout
570b0 20 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f   the.** length o
570c0 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  f a statement an
570d0 64 20 69 73 20 74 68 65 6e 20 75 70 64 61 74 65  d is then update
570e0 64 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74  d by OP_SetCount
570f0 73 2e 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a  s.  It keeps a.*
57100 2a 20 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20  * context stack 
57110 6a 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f  just like lastRo
57120 77 69 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  wid so that the 
57130 63 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73  count of changes
57140 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69  .** within a tri
57150 67 67 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e  gger is not seen
57160 20 6f 75 74 73 69 64 65 20 74 68 65 20 74 72 69   outside the tri
57170 67 67 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74  gger.  Changes t
57180 6f 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a  o views do not.*
57190 2a 20 61 66 66 65 63 74 20 74 68 65 20 76 61 6c  * affect the val
571a0 75 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a  ue of lsChange..
571b0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73  ** The sqlite.cs
571c0 43 68 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61  Change keeps tra
571d0 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  ck of the number
571e0 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e   of current chan
571f0 67 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68  ges (since.** th
57200 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74  e last statement
57210 29 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f  ) and is used to
57220 20 75 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c   update sqlite_l
57230 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54  sChange..**.** T
57240 68 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62  he member variab
57250 6c 65 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f  les sqlite.errCo
57260 64 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d  de, sqlite.zErrM
57270 73 67 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45  sg and sqlite.zE
57280 72 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65  rrMsg16.** store
57290 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
572a0 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c   error code and,
572b0 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20   if applicable, 
572c0 73 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69  string. The.** i
572d0 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e  nternal function
572e0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20   sqlite3Error() 
572f0 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74  is used to set t
57300 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a  hese variables.*
57310 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a  * consistently..
57320 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
57330 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  3 {.  sqlite3_vf
57340 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
57350 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66      /* OS Interf
57360 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62  ace */.  int nDb
57370 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
57380 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
57390 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72   of backends cur
573a0 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f  rently in use */
573b0 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20  .  Db *aDb;     
573c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
573d0 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73   /* All backends
573e0 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b   */.  int flags;
573f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57400 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e      /* Miscellan
57410 65 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20  eous flags. See 
57420 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f  below */.  int o
57430 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20  penFlags;       
57440 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
57450 73 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69  s passed to sqli
57460 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20  te3_vfs.xOpen() 
57470 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65  */.  int errCode
57480 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
57490 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e     /* Most recen
574a0 74 20 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51  t error code (SQ
574b0 4c 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74  LITE_*) */.  int
574c0 20 65 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20   errMask;       
574d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20             /* & 
574e0 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74  result codes wit
574f0 68 20 74 68 69 73 20 62 65 66 6f 72 65 20 72 65  h this before re
57500 74 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20  turning */.  u8 
57510 61 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20  autoCommit;     
57520 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
57530 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c  e auto-commit fl
57540 61 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70  ag. */.  u8 temp
57550 5f 73 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20  _store;         
57560 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c         /* 1: fil
57570 65 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64  e 2: memory 0: d
57580 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d  efault */.  u8 m
57590 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20  allocFailed;    
575a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
575b0 65 20 69 66 20 77 65 20 68 61 76 65 20 73 65 65  e if we have see
575c0 6e 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  n a malloc failu
575d0 72 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c  re */.  u8 dfltL
575e0 6f 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20  ockMode;        
575f0 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
57600 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f   locking-mode fo
57610 72 20 61 74 74 61 63 68 65 64 20 64 62 73 20 2a  r attached dbs *
57620 2f 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e  /.  u8 dfltJourn
57630 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20  alMode;         
57640 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75    /* Default jou
57650 72 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74  rnal mode for at
57660 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20  tached dbs */.  
57670 73 69 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74  signed char next
57680 41 75 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a  Autovac;      /*
57690 20 41 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67   Autovac setting
576a0 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 66   after VACUUM if
576b0 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65   >=0 */.  int ne
576c0 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20  xtPagesize;     
576d0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73          /* Pages
576e0 69 7a 65 20 61 66 74 65 72 20 56 41 43 55 55 4d  ize after VACUUM
576f0 20 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20   if >0 */.  int 
57700 6e 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  nTable;         
57710 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
57720 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e  ber of tables in
57730 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
57740 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c  .  CollSeq *pDfl
57750 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  tColl;          
57760 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20   /* The default 
57770 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
57780 63 65 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20  ce (BINARY) */. 
57790 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20   i64 lastRowid; 
577a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
577b0 2a 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20  * ROWID of most 
577c0 72 65 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73  recent insert (s
577d0 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 69  ee above) */.  i
577e0 36 34 20 70 72 69 6f 72 4e 65 77 52 6f 77 69 64  64 priorNewRowid
577f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
57800 4c 61 73 74 20 72 61 6e 64 6f 6d 6c 79 20 67 65  Last randomly ge
57810 6e 65 72 61 74 65 64 20 52 4f 57 49 44 20 2a 2f  nerated ROWID */
57820 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20  .  u32 magic;   
57830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57840 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72   /* Magic number
57850 20 66 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72   for detect libr
57860 61 72 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20  ary misuse */.  
57870 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20  int nChange;    
57880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
57890 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   Value returned 
578a0 62 79 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  by sqlite3_chang
578b0 65 73 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  es() */.  int nT
578c0 6f 74 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20  otalChange;     
578d0 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
578e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
578f0 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
57900 65 73 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65  es() */.  sqlite
57910 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20  3_mutex *mutex; 
57920 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65          /* Conne
57930 63 74 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20  ction mutex */. 
57940 20 69 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49   int aLimit[SQLI
57950 54 45 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f  TE_N_LIMIT];   /
57960 2a 20 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74  * Limits */.  st
57970 72 75 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74  ruct sqlite3Init
57980 49 6e 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49  Info {      /* I
57990 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20  nformation used 
579a0 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a  during initializ
579b0 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74  ation */.    int
579c0 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
579d0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e           /* When
579e0 20 62 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69   back is being i
579f0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
57a00 20 20 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20    int newTnum;  
57a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
57a20 20 52 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62   Rootpage of tab
57a30 6c 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c  le being initial
57a40 69 7a 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62  ized */.    u8 b
57a50 75 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  usy;            
57a60 20 20 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20          /* TRUE 
57a70 69 66 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69  if currently ini
57a80 74 69 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 7d  tializing */.  }
57a90 20 69 6e 69 74 3b 0a 20 20 69 6e 74 20 6e 45 78   init;.  int nEx
57aa0 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20  tension;        
57ab0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
57ac0 20 6f 66 20 6c 6f 61 64 65 64 20 65 78 74 65 6e   of loaded exten
57ad0 73 69 6f 6e 73 20 2a 2f 0a 20 20 76 6f 69 64 20  sions */.  void 
57ae0 2a 2a 61 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20  **aExtension;   
57af0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61           /* Arra
57b00 79 20 6f 66 20 73 68 61 72 65 64 20 6c 69 62 72  y of shared libr
57b10 61 72 79 20 68 61 6e 64 6c 65 73 20 2a 2f 0a 20  ary handles */. 
57b20 20 73 74 72 75 63 74 20 56 64 62 65 20 2a 70 56   struct Vdbe *pV
57b30 64 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  dbe;           /
57b40 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65  * List of active
57b50 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
57b60 73 20 2a 2f 0a 20 20 69 6e 74 20 61 63 74 69 76  s */.  int activ
57b70 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20  eVdbeCnt;       
57b80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
57b90 66 20 56 44 42 45 73 20 63 75 72 72 65 6e 74 6c  f VDBEs currentl
57ba0 79 20 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 20  y executing */. 
57bb0 20 69 6e 74 20 77 72 69 74 65 56 64 62 65 43 6e   int writeVdbeCn
57bc0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t;             /
57bd0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 63 74 69  * Number of acti
57be0 76 65 20 56 44 42 45 73 20 74 68 61 74 20 61 72  ve VDBEs that ar
57bf0 65 20 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 76  e writing */.  v
57c00 6f 69 64 20 28 2a 78 54 72 61 63 65 29 28 76 6f  oid (*xTrace)(vo
57c10 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  id*,const char*)
57c20 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 63  ;        /* Trac
57c30 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
57c40 76 6f 69 64 20 2a 70 54 72 61 63 65 41 72 67 3b  void *pTraceArg;
57c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57c60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
57c70 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61  ument to the tra
57c80 63 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  ce function */. 
57c90 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65   void (*xProfile
57ca0 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68  )(void*,const ch
57cb0 61 72 2a 2c 75 36 34 29 3b 20 20 2f 2a 20 50 72  ar*,u64);  /* Pr
57cc0 6f 66 69 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  ofiling function
57cd0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f   */.  void *pPro
57ce0 66 69 6c 65 41 72 67 3b 20 20 20 20 20 20 20 20  fileArg;        
57cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57d00 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70  /* Argument to p
57d10 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20  rofile function 
57d20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d  */.  void *pComm
57d30 69 74 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  itArg;          
57d40 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
57d50 6e 74 20 74 6f 20 78 43 6f 6d 6d 69 74 43 61 6c  nt to xCommitCal
57d60 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20  lback() */   .  
57d70 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 43 61 6c  int (*xCommitCal
57d80 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 20  lback)(void*);  
57d90 20 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20    /* Invoked at 
57da0 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f  every commit. */
57db0 0a 20 20 76 6f 69 64 20 2a 70 52 6f 6c 6c 62 61  .  void *pRollba
57dc0 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  ckArg;          
57dd0 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
57de0 20 74 6f 20 78 52 6f 6c 6c 62 61 63 6b 43 61 6c   to xRollbackCal
57df0 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20  lback() */   .  
57e00 76 6f 69 64 20 28 2a 78 52 6f 6c 6c 62 61 63 6b  void (*xRollback
57e10 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29  Callback)(void*)
57e20 3b 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20  ; /* Invoked at 
57e30 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f  every commit. */
57e40 0a 20 20 76 6f 69 64 20 2a 70 55 70 64 61 74 65  .  void *pUpdate
57e50 41 72 67 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55  Arg;.  void (*xU
57e60 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 28 76  pdateCallback)(v
57e70 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20  oid*,int, const 
57e80 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
57e90 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b  *,sqlite_int64);
57ea0 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65  .  void(*xCollNe
57eb0 65 64 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69  eded)(void*,sqli
57ec0 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65  te3*,int eTextRe
57ed0 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  p,const char*);.
57ee0 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65    void(*xCollNee
57ef0 64 65 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c  ded16)(void*,sql
57f00 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52  ite3*,int eTextR
57f10 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b  ep,const void*);
57f20 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65  .  void *pCollNe
57f30 65 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74  ededArg;.  sqlit
57f40 65 33 5f 76 61 6c 75 65 20 2a 70 45 72 72 3b 20  e3_value *pErr; 
57f50 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74           /* Most
57f60 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65   recent error me
57f70 73 73 61 67 65 20 2a 2f 0a 20 20 63 68 61 72 20  ssage */.  char 
57f80 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20  *zErrMsg;       
57f90 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74           /* Most
57fa0 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65   recent error me
57fb0 73 73 61 67 65 20 28 55 54 46 2d 38 20 65 6e 63  ssage (UTF-8 enc
57fc0 6f 64 65 64 29 20 2a 2f 0a 20 20 63 68 61 72 20  oded) */.  char 
57fd0 2a 7a 45 72 72 4d 73 67 31 36 3b 20 20 20 20 20  *zErrMsg16;     
57fe0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74           /* Most
57ff0 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65   recent error me
58000 73 73 61 67 65 20 28 55 54 46 2d 31 36 20 65 6e  ssage (UTF-16 en
58010 63 6f 64 65 64 29 20 2a 2f 0a 20 20 75 6e 69 6f  coded) */.  unio
58020 6e 20 7b 0a 20 20 20 20 76 6f 6c 61 74 69 6c 65  n {.    volatile
58030 20 69 6e 74 20 69 73 49 6e 74 65 72 72 75 70 74   int isInterrupt
58040 65 64 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 73  ed; /* True if s
58050 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
58060 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
58070 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e   */.    double n
58080 6f 74 55 73 65 64 31 3b 20 20 20 20 20 20 20 20  otUsed1;        
58090 20 20 20 20 2f 2a 20 53 70 61 63 65 72 20 2a 2f      /* Spacer */
580a0 0a 20 20 7d 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61  .  } u1;.  Looka
580b0 73 69 64 65 20 6c 6f 6f 6b 61 73 69 64 65 3b 20  side lookaside; 
580c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b           /* Look
580d0 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e  aside malloc con
580e0 66 69 67 75 72 61 74 69 6f 6e 20 2a 2f 0a 23 69  figuration */.#i
580f0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
58100 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
58110 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
58120 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
58130 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
58140 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
58150 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20  st char*);.     
58160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58170 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63             /* Ac
58180 63 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69  cess authorizati
58190 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  on function */. 
581a0 20 76 6f 69 64 20 2a 70 41 75 74 68 41 72 67 3b   void *pAuthArg;
581b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
581c0 2a 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74  * 1st argument t
581d0 6f 20 74 68 65 20 61 63 63 65 73 73 20 61 75 74  o the access aut
581e0 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65  h function */.#e
581f0 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
58200 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53  ITE_OMIT_PROGRES
58210 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74  S_CALLBACK.  int
58220 20 28 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f   (*xProgress)(vo
58230 69 64 20 2a 29 3b 20 20 20 20 20 2f 2a 20 54 68  id *);     /* Th
58240 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
58250 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ack */.  void *p
58260 50 72 6f 67 72 65 73 73 41 72 67 3b 20 20 20 20  ProgressArg;    
58270 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
58280 6e 74 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65  nt to the progre
58290 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  ss callback */. 
582a0 20 69 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70   int nProgressOp
582b0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  s;             /
582c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f  * Number of opco
582d0 64 65 73 20 66 6f 72 20 70 72 6f 67 72 65 73 73  des for progress
582e0 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e   callback */.#en
582f0 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
58300 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
58310 41 42 4c 45 0a 20 20 48 61 73 68 20 61 4d 6f 64  ABLE.  Hash aMod
58320 75 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ule;            
58330 20 20 20 20 20 2f 2a 20 70 6f 70 75 6c 61 74 65       /* populate
58340 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72 65  d by sqlite3_cre
58350 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a  ate_module() */.
58360 20 20 54 61 62 6c 65 20 2a 70 56 54 61 62 3b 20    Table *pVTab; 
58370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58380 2f 2a 20 76 74 61 62 20 77 69 74 68 20 61 63 74  /* vtab with act
58390 69 76 65 20 43 6f 6e 6e 65 63 74 2f 43 72 65 61  ive Connect/Crea
583a0 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73  te method */.  s
583b0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 61 56  qlite3_vtab **aV
583c0 54 72 61 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20  Trans;       /* 
583d0 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77  Virtual tables w
583e0 69 74 68 20 6f 70 65 6e 20 74 72 61 6e 73 61 63  ith open transac
583f0 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  tions */.  int n
58400 56 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20  VTrans;         
58410 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f           /* Allo
58420 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 56  cated size of aV
58430 54 72 61 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  Trans */.#endif.
58440 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 61 46    FuncDefHash aF
58450 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  unc;            
58460 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66  /* Hash table of
58470 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63   connection func
58480 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20  tions */.  Hash 
58490 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20  aCollSeq;       
584a0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20           /* All 
584b0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
584c0 63 65 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e  ces */.  BusyHan
584d0 64 6c 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72  dler busyHandler
584e0 3b 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63  ;      /* Busy c
584f0 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  allback */.  int
58500 20 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20   busyTimeout;   
58510 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75             /* Bu
58520 73 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f  sy handler timeo
58530 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20  ut, in msec */. 
58540 20 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d   Db aDbStatic[2]
58550 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
58560 2a 20 53 74 61 74 69 63 20 73 70 61 63 65 20 66  * Static space f
58570 6f 72 20 74 68 65 20 32 20 64 65 66 61 75 6c 74  or the 2 default
58580 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 23 69 66   backends */.#if
58590 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20  def SQLITE_SSE. 
585a0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
585b0 46 65 74 63 68 3b 20 20 20 20 20 20 20 20 20 2f  Fetch;         /
585c0 2a 20 55 73 65 64 20 62 79 20 53 53 45 20 74 6f  * Used by SSE to
585d0 20 66 65 74 63 68 20 73 74 6f 72 65 64 20 73 74   fetch stored st
585e0 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 23 65 6e 64  atements */.#end
585f0 69 66 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a  if.  Savepoint *
58600 70 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20  pSavepoint;     
58610 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63     /* List of ac
58620 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20  tive savepoints 
58630 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f  */.  int nSavepo
58640 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  int;            
58650 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
58660 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  non-transaction 
58670 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20  savepoints */.  
58680 69 6e 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20  int nStatement; 
58690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
586a0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
586b0 64 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e  d statement-tran
586c0 73 61 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75  sactions  */.  u
586d0 38 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53  8 isTransactionS
586e0 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20  avepoint;    /* 
586f0 54 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 65  True if the oute
58700 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20  rmost savepoint 
58710 69 73 20 61 20 54 53 20 2a 2f 0a 0a 23 69 66 64  is a TS */..#ifd
58720 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
58730 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20  _UNLOCK_NOTIFY. 
58740 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
58750 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  g variables are 
58760 61 6c 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79  all protected by
58770 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54   the STATIC_MAST
58780 45 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20  ER .  ** mutex, 
58790 6e 6f 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d  not by sqlite3.m
587a0 75 74 65 78 2e 20 54 68 65 79 20 61 72 65 20 75  utex. They are u
587b0 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e  sed by code in n
587c0 6f 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20  otify.c. .  **. 
587d0 20 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c 6f   ** When X.pUnlo
587e0 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c  ckConnection==Y,
587f0 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
58800 20 58 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f   X is waiting fo
58810 72 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f  r Y to.  ** unlo
58820 63 6b 20 73 6f 20 74 68 61 74 20 69 74 20 63 61  ck so that it ca
58830 6e 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a  n proceed..  **.
58840 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c 6f    ** When X.pBlo
58850 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d  ckingConnection=
58860 3d 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  =Y, that means t
58870 68 61 74 20 73 6f 6d 65 74 68 69 6e 67 20 74 68  hat something th
58880 61 74 20 58 20 74 72 69 65 64 0a 20 20 2a 2a 20  at X tried.  ** 
58890 74 72 69 65 64 20 74 6f 20 64 6f 20 72 65 63 65  tried to do rece
588a0 6e 74 6c 79 20 66 61 69 6c 65 64 20 77 69 74 68  ntly failed with
588b0 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   an SQLITE_LOCKE
588c0 44 20 65 72 72 6f 72 20 64 75 65 20 74 6f 20 6c  D error due to l
588d0 6f 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20 62  ocks.  ** held b
588e0 79 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  y Y..  */.  sqli
588f0 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f  te3 *pBlockingCo
58900 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e  nnection; /* Con
58910 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 63 61 75  nection that cau
58920 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  sed SQLITE_LOCKE
58930 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  D */.  sqlite3 *
58940 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
58950 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  n;           /* 
58960 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77 61  Connection to wa
58970 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a  tch for unlock *
58980 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f 63  /.  void *pUnloc
58990 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  kArg;           
589a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
589b0 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 6b  ument to xUnlock
589c0 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69 64  Notify */.  void
589d0 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79   (*xUnlockNotify
589e0 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 3b  )(void **, int);
589f0 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69    /* Unlock noti
58a00 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  fy callback */. 
58a10 20 73 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 42   sqlite3 *pNextB
58a20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 2f  locked;        /
58a30 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f  * Next in list o
58a40 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 6f  f all blocked co
58a50 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e  nnections */.#en
58a60 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20  dif.};../*.** A 
58a70 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65  macro to discove
58a80 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f  r the encoding o
58a90 66 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  f a database..*/
58aa0 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29  .#define ENC(db)
58ab0 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70   ((db)->aDb[0].p
58ac0 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a  Schema->enc)../*
58ad0 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c  .** Possible val
58ae0 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69  ues for the sqli
58af0 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20  te.flags and or 
58b00 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e  Db.flags fields.
58b10 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65  .**.** On sqlite
58b20 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49  .flags, the SQLI
58b30 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65  TE_InTrans value
58b40 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 68   means that we h
58b50 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20  ave.** executed 
58b60 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e  a BEGIN.  On Db.
58b70 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e  flags, SQLITE_In
58b80 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74  Trans means a st
58b90 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73  atement.** trans
58ba0 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
58bb0 20 6f 6e 20 74 68 61 74 20 70 61 72 74 69 63 75   on that particu
58bc0 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c  lar database fil
58bd0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e..*/.#define SQ
58be0 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 20 20  LITE_VdbeTrace  
58bf0 20 20 20 20 30 78 30 30 30 30 30 30 30 31 20 20      0x00000001  
58c00 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65  /* True to trace
58c10 20 56 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20   VDBE execution 
58c20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
58c30 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20  E_InTrans       
58c40 20 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20   0x00000008  /* 
58c50 54 72 75 65 20 69 66 20 69 6e 20 61 20 74 72 61  True if in a tra
58c60 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66  nsaction */.#def
58c70 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72  ine SQLITE_Inter
58c80 6e 43 68 61 6e 67 65 73 20 20 30 78 30 30 30 30  nChanges  0x0000
58c90 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69  0010  /* Uncommi
58ca0 74 74 65 64 20 48 61 73 68 20 74 61 62 6c 65 20  tted Hash table 
58cb0 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69  changes */.#defi
58cc0 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f  ne SQLITE_FullCo
58cd0 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 30 30 30  lNames   0x00000
58ce0 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c  020  /* Show ful
58cf0 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f  l column names o
58d00 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66  n SELECT */.#def
58d10 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74  ine SQLITE_Short
58d20 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30  ColNames  0x0000
58d30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68  0040  /* Show sh
58d40 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65  ort columns name
58d50 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
58d60 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20  ITE_CountRows   
58d70 20 20 20 30 78 30 30 30 30 30 30 38 30 20 20 2f     0x00000080  /
58d80 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61  * Count rows cha
58d90 6e 67 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20  nged by INSERT, 
58da0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
58db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
58dd0 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44    DELETE, or UPD
58de0 41 54 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a  ATE and return *
58df0 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
58e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58e10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
58e20 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67   the count using
58e30 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a   a callback. */.
58e40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
58e50 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78  ullCallback   0x
58e60 30 30 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76  00000100  /* Inv
58e70 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  oke the callback
58e80 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a   once if the */.
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 20 20 20 20 20 20 20                  
58eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72            /*   r
58ec0 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70  esult set is emp
58ed0 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ty */.#define SQ
58ee0 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20  LITE_SqlTrace   
58ef0 20 20 20 20 30 78 30 30 30 30 30 32 30 30 20 20      0x00000200  
58f00 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 74 20 53  /* Debug print S
58f10 51 4c 20 61 73 20 69 74 20 65 78 65 63 75 74 65  QL as it execute
58f20 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
58f30 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20  ITE_VdbeListing 
58f40 20 20 20 30 78 30 30 30 30 30 34 30 30 20 20 2f     0x00000400  /
58f50 2a 20 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73  * Debug listings
58f60 20 6f 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d   of VDBE program
58f70 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
58f80 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20  ITE_WriteSchema 
58f90 20 20 20 30 78 30 30 30 30 30 38 30 30 20 20 2f     0x00000800  /
58fa0 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53  * OK to update S
58fb0 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a  QLITE_MASTER */.
58fc0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
58fd0 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78  oReadlock     0x
58fe0 30 30 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61  00001000  /* Rea
58ff0 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74  dlocks are omitt
59000 65 64 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20  ed when .       
59010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59030 20 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20     ** accessing 
59040 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61  read-only databa
59050 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ses */.#define S
59060 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63  QLITE_IgnoreChec
59070 6b 73 20 20 20 30 78 30 30 30 30 32 30 30 30 20  ks   0x00002000 
59080 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72   /* Do not enfor
59090 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61  ce check constra
590a0 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ints */.#define 
590b0 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d  SQLITE_ReadUncom
590c0 6d 69 74 74 65 64 20 30 78 30 30 30 30 34 30 30  mitted 0x0000400
590d0 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d  0 /* For shared-
590e0 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64  cache mode */.#d
590f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67  efine SQLITE_Leg
59100 61 63 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30  acyFileFmt  0x00
59110 30 30 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74  008000  /* Creat
59120 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20  e new databases 
59130 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23  in format 1 */.#
59140 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75  define SQLITE_Fu
59150 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 30 78 30  llFSync      0x0
59160 30 30 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20  0010000  /* Use 
59170 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68  full fsync on th
59180 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65  e backend */.#de
59190 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64  fine SQLITE_Load
591a0 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30  Extension  0x000
591b0 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65  20000  /* Enable
591c0 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20   load_extension 
591d0 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
591e0 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20  TE_RecoveryMode 
591f0 20 20 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a    0x00040000  /*
59200 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65   Ignore schema e
59210 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65  rrors */.#define
59220 20 53 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61   SQLITE_SharedCa
59230 63 68 65 20 20 20 20 30 78 30 30 30 38 30 30 30  che    0x0008000
59240 30 20 20 2f 2a 20 43 61 63 68 65 20 73 68 61 72  0  /* Cache shar
59250 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 2a  ing is enabled *
59260 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
59270 5f 43 6f 6d 6d 69 74 42 75 73 79 20 20 20 20 20  _CommitBusy     
59280 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 49  0x00200000  /* I
59290 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66  n the process of
592a0 20 63 6f 6d 6d 69 74 74 69 6e 67 20 2a 2f 0a 23   committing */.#
592b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65  define SQLITE_Re
592c0 76 65 72 73 65 4f 72 64 65 72 20 20 20 30 78 30  verseOrder   0x0
592d0 30 34 30 30 30 30 30 20 20 2f 2a 20 52 65 76 65  0400000  /* Reve
592e0 72 73 65 20 75 6e 6f 72 64 65 72 65 64 20 53 45  rse unordered SE
592f0 4c 45 43 54 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  LECTs */../*.** 
59300 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20  Possible values 
59310 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d  for the sqlite.m
59320 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54  agic field..** T
59330 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f  he numbers are o
59340 62 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f  btained at rando
59350 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70  m and have no sp
59360 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f  ecial meaning, o
59370 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69  ther.** than bei
59380 6e 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d  ng distinct from
59390 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f   one another..*/
593a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
593b0 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30  MAGIC_OPEN     0
593c0 78 61 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61  xa029a697  /* Da
593d0 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a  tabase is open *
593e0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
593f0 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20  _MAGIC_CLOSED   
59400 30 78 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44  0x9f3c2d33  /* D
59410 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65  atabase is close
59420 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
59430 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 20  ITE_MAGIC_SICK  
59440 20 20 20 30 78 34 62 37 37 31 32 39 30 20 20 2f     0x4b771290  /
59450 2a 20 45 72 72 6f 72 20 61 6e 64 20 61 77 61 69  * Error and awai
59460 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 64  ting close */.#d
59470 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47  efine SQLITE_MAG
59480 49 43 5f 42 55 53 59 20 20 20 20 20 30 78 66 30  IC_BUSY     0xf0
59490 33 62 37 39 30 36 20 20 2f 2a 20 44 61 74 61 62  3b7906  /* Datab
594a0 61 73 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  ase currently in
594b0 20 75 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20   use */.#define 
594c0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52  SQLITE_MAGIC_ERR
594d0 4f 52 20 20 20 20 30 78 62 35 33 35 37 39 33 30  OR    0xb5357930
594e0 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4d    /* An SQLITE_M
594f0 49 53 55 53 45 20 65 72 72 6f 72 20 6f 63 63 75  ISUSE error occu
59500 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45  rred */../*.** E
59510 61 63 68 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ach SQL function
59520 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61   is defined by a
59530 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
59540 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73  e following.** s
59550 74 72 75 63 74 75 72 65 2e 20 20 41 20 70 6f 69  tructure.  A poi
59560 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
59570 75 63 74 75 72 65 20 69 73 20 73 74 6f 72 65 64  ucture is stored
59580 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61   in the sqlite.a
59590 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 62  Func.** hash tab
595a0 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 70  le.  When multip
595b0 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76  le functions hav
595c0 65 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c  e the same name,
595d0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a   the hash table.
595e0 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6c  ** points to a l
595f0 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68  inked list of th
59600 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a  ese structures..
59610 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65  */.struct FuncDe
59620 66 20 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b 20  f {.  i16 nArg; 
59630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
59640 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
59650 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c  s.  -1 means unl
59660 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 69  imited */.  u8 i
59670 50 72 65 66 45 6e 63 3b 20 20 20 20 20 20 20 20  PrefEnc;        
59680 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 65   /* Preferred te
59690 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 4c  xt encoding (SQL
596a0 49 54 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c 20  ITE_UTF8, 16LE, 
596b0 31 36 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 6c  16BE) */.  u8 fl
596c0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
596d0 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74  /* Some combinat
596e0 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 55  ion of SQLITE_FU
596f0 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  NC_* */.  void *
59700 70 55 73 65 72 44 61 74 61 3b 20 20 20 20 20 2f  pUserData;     /
59710 2a 20 55 73 65 72 20 64 61 74 61 20 70 61 72 61  * User data para
59720 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 44  meter */.  FuncD
59730 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  ef *pNext;      
59740 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 69 6f 6e  /* Next function
59750 20 77 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 20   with same name 
59760 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  */.  void (*xFun
59770 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
59780 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
59790 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 67  value**); /* Reg
597a0 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ular function */
597b0 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29  .  void (*xStep)
597c0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
597d0 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
597e0 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 65  lue**); /* Aggre
597f0 67 61 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 76  gate step */.  v
59800 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 29  oid (*xFinalize)
59810 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
59820 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *);             
59830 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20     /* Aggregate 
59840 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 63  finalizer */.  c
59850 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
59860 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 20      /* SQL name 
59870 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  of the function.
59880 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70   */.  FuncDef *p
59890 48 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e 65  Hash;      /* Ne
598a0 78 74 20 77 69 74 68 20 61 20 64 69 66 66 65 72  xt with a differ
598b0 65 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68 65  ent name but the
598c0 20 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d 3b   same hash */.};
598d0 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65  ../*.** Possible
598e0 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63   values for Func
598f0 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65  Def.flags.*/.#de
59900 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43  fine SQLITE_FUNC
59910 5f 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20 2f  _LIKE     0x01 /
59920 2a 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72 20  * Candidate for 
59930 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a  the LIKE optimiz
59940 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ation */.#define
59950 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53   SQLITE_FUNC_CAS
59960 45 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43 61  E     0x02 /* Ca
59970 73 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 4b  se-sensitive LIK
59980 45 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e 20  E-type function 
59990 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
599a0 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 20  E_FUNC_EPHEM    
599b0 30 78 30 34 20 2f 2a 20 45 70 68 65 6d 65 72 61  0x04 /* Ephemera
599c0 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68 20  l.  Delete with 
599d0 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20  VDBE */.#define 
599e0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
599f0 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 6c  COLL 0x08 /* sql
59a00 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
59a10 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63 61  eq() might be ca
59a20 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  lled */.#define 
59a30 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49 56  SQLITE_FUNC_PRIV
59a40 41 54 45 20 20 30 78 31 30 20 2f 2a 20 41 6c 6c  ATE  0x10 /* All
59a50 6f 77 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61  owed for interna
59a60 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64  l use only */.#d
59a70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
59a80 43 5f 43 4f 55 4e 54 20 20 20 20 30 78 32 30 20  C_COUNT    0x20 
59a90 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75 6e  /* Built-in coun
59aa0 74 28 2a 29 20 61 67 67 72 65 67 61 74 65 20 2a  t(*) aggregate *
59ab0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  /../*.** The fol
59ac0 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 6d 61 63  lowing three mac
59ad0 72 6f 73 2c 20 46 55 4e 43 54 49 4f 4e 28 29 2c  ros, FUNCTION(),
59ae0 20 4c 49 4b 45 46 55 4e 43 28 29 20 61 6e 64 20   LIKEFUNC() and 
59af0 41 47 47 52 45 47 41 54 45 28 29 20 61 72 65 0a  AGGREGATE() are.
59b00 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65 61 74  ** used to creat
59b10 65 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65  e the initialize
59b20 72 73 20 66 6f 72 20 74 68 65 20 46 75 6e 63 44  rs for the FuncD
59b30 65 66 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a  ef structures..*
59b40 2a 0a 2a 2a 20 20 20 46 55 4e 43 54 49 4f 4e 28  *.**   FUNCTION(
59b50 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72  zName, nArg, iAr
59b60 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 0a 2a  g, bNC, xFunc).*
59b70 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72  *     Used to cr
59b80 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75  eate a scalar fu
59b90 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
59ba0 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20  n of a function 
59bb0 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 69 6d  zName .**     im
59bc0 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 43 20 66  plemented by C f
59bd0 75 6e 63 74 69 6f 6e 20 78 46 75 6e 63 20 74 68  unction xFunc th
59be0 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20  at accepts nArg 
59bf0 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 0a 2a  arguments. The.*
59c00 2a 20 20 20 20 20 76 61 6c 75 65 20 70 61 73 73  *     value pass
59c10 65 64 20 61 73 20 69 41 72 67 20 69 73 20 63 61  ed as iArg is ca
59c20 73 74 20 74 6f 20 61 20 28 76 6f 69 64 2a 29 20  st to a (void*) 
59c30 61 6e 64 20 6d 61 64 65 20 61 76 61 69 6c 61 62  and made availab
59c40 6c 65 0a 2a 2a 20 20 20 20 20 61 73 20 74 68 65  le.**     as the
59c50 20 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69   user-data (sqli
59c60 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29  te3_user_data())
59c70 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f   for the functio
59c80 6e 2e 20 49 66 20 0a 2a 2a 20 20 20 20 20 61 72  n. If .**     ar
59c90 67 75 6d 65 6e 74 20 62 4e 43 20 69 73 20 74 72  gument bNC is tr
59ca0 75 65 2c 20 74 68 65 6e 20 74 68 65 20 46 75 6e  ue, then the Fun
59cb0 63 44 65 66 2e 6e 65 65 64 43 6f 6c 6c 61 74 65  cDef.needCollate
59cc0 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a   flag is set..**
59cd0 0a 2a 2a 20 20 20 41 47 47 52 45 47 41 54 45 28  .**   AGGREGATE(
59ce0 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72  zName, nArg, iAr
59cf0 67 2c 20 62 4e 43 2c 20 78 53 74 65 70 2c 20 78  g, bNC, xStep, x
59d00 46 69 6e 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73  Final).**     Us
59d10 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20  ed to create an 
59d20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
59d30 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d  on definition im
59d40 70 6c 65 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20  plemented by.** 
59d50 20 20 20 20 74 68 65 20 43 20 66 75 6e 63 74 69      the C functi
59d60 6f 6e 73 20 78 53 74 65 70 20 61 6e 64 20 78 46  ons xStep and xF
59d70 69 6e 61 6c 2e 20 54 68 65 20 66 69 72 73 74 20  inal. The first 
59d80 66 6f 75 72 20 70 61 72 61 6d 65 74 65 72 73 0a  four parameters.
59d90 2a 2a 20 20 20 20 20 61 72 65 20 69 6e 74 65 72  **     are inter
59da0 70 72 65 74 65 64 20 69 6e 20 74 68 65 20 73 61  preted in the sa
59db0 6d 65 20 77 61 79 20 61 73 20 74 68 65 20 66 69  me way as the fi
59dc0 72 73 74 20 34 20 70 61 72 61 6d 65 74 65 72 73  rst 4 parameters
59dd0 20 74 6f 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54   to.**     FUNCT
59de0 49 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c  ION()..**.**   L
59df0 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e  IKEFUNC(zName, n
59e00 41 72 67 2c 20 70 41 72 67 2c 20 66 6c 61 67 73  Arg, pArg, flags
59e10 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f  ).**     Used to
59e20 20 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72   create a scalar
59e30 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
59e40 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69  tion of a functi
59e50 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20  on zName .**    
59e60 20 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41   that accepts nA
59e70 72 67 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64  rg arguments and
59e80 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
59e90 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a  by a call to C .
59ea0 2a 2a 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20  **     function 
59eb0 6c 69 6b 65 46 75 6e 63 2e 20 41 72 67 75 6d 65  likeFunc. Argume
59ec0 6e 74 20 70 41 72 67 20 69 73 20 63 61 73 74 20  nt pArg is cast 
59ed0 74 6f 20 61 20 28 76 6f 69 64 20 2a 29 20 61 6e  to a (void *) an
59ee0 64 20 6d 61 64 65 0a 2a 2a 20 20 20 20 20 61 76  d made.**     av
59ef0 61 69 6c 61 62 6c 65 20 61 73 20 74 68 65 20 66  ailable as the f
59f00 75 6e 63 74 69 6f 6e 20 75 73 65 72 2d 64 61 74  unction user-dat
59f10 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  a (sqlite3_user_
59f20 64 61 74 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20  data()). The.** 
59f30 20 20 20 20 46 75 6e 63 44 65 66 2e 66 6c 61 67      FuncDef.flag
59f40 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  s variable is se
59f50 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70  t to the value p
59f60 61 73 73 65 64 20 61 73 20 74 68 65 20 66 6c 61  assed as the fla
59f70 67 73 0a 2a 2a 20 20 20 20 20 70 61 72 61 6d 65  gs.**     parame
59f80 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ter..*/.#define 
59f90 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20  FUNCTION(zName, 
59fa0 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c  nArg, iArg, bNC,
59fb0 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72   xFunc) \.  {nAr
59fc0 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g, SQLITE_UTF8, 
59fd0 62 4e 43 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e  bNC*8, SQLITE_IN
59fe0 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20  T_TO_PTR(iArg), 
59ff0 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20  0, xFunc, 0, 0, 
5a000 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69  #zName, 0}.#defi
5a010 6e 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28  ne STR_FUNCTION(
5a020 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72  zName, nArg, pAr
5a030 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c  g, bNC, xFunc) \
5a040 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45  .  {nArg, SQLITE
5a050 5f 55 54 46 38 2c 20 62 4e 43 2a 38 2c 20 70 41  _UTF8, bNC*8, pA
5a060 72 67 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c  rg, 0, xFunc, 0,
5a070 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23   0, #zName, 0}.#
5a080 64 65 66 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28  define LIKEFUNC(
5a090 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67  zName, nArg, arg
5a0a0 2c 20 66 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41  , flags) \.  {nA
5a0b0 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  rg, SQLITE_UTF8,
5a0c0 20 66 6c 61 67 73 2c 20 28 76 6f 69 64 20 2a 29   flags, (void *)
5a0d0 61 72 67 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63  arg, 0, likeFunc
5a0e0 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20  , 0, 0, #zName, 
5a0f0 30 7d 0a 23 64 65 66 69 6e 65 20 41 47 47 52 45  0}.#define AGGRE
5a100 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67  GATE(zName, nArg
5a110 2c 20 61 72 67 2c 20 6e 63 2c 20 78 53 74 65 70  , arg, nc, xStep
5a120 2c 20 78 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e  , xFinal) \.  {n
5a130 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Arg, SQLITE_UTF8
5a140 2c 20 6e 63 2a 38 2c 20 53 51 4c 49 54 45 5f 49  , nc*8, SQLITE_I
5a150 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20  NT_TO_PTR(arg), 
5a160 30 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e  0, 0, xStep,xFin
5a170 61 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a  al,#zName,0}../*
5a180 0a 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20  .** All current 
5a190 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73  savepoints are s
5a1a0 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65  tored in a linke
5a1b0 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20  d list starting 
5a1c0 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53  at.** sqlite3.pS
5a1d0 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69  avepoint. The fi
5a1e0 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74  rst element in t
5a1f0 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d  he list is the m
5a200 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20  ost recently.** 
5a210 6f 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74  opened savepoint
5a220 2e 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65  . Savepoints are
5a230 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69   added to the li
5a240 73 74 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a  st by the vdbe.*
5a250 2a 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69  * OP_Savepoint i
5a260 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  nstruction..*/.s
5a270 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20  truct Savepoint 
5a280 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  {.  char *zName;
5a290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a2a0 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70          /* Savep
5a2b0 6f 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74  oint name (nul-t
5a2c0 65 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20  erminated) */.  
5a2d0 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74  Savepoint *pNext
5a2e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5a2f0 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 73 61      /* Parent sa
5a300 76 65 70 6f 69 6e 74 20 28 69 66 20 61 6e 79 29  vepoint (if any)
5a310 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
5a320 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
5a330 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  used as the seco
5a340 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
5a350 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74  sqlite3Savepoint
5a360 28 29 2c 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68  (),.** and as th
5a370 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f  e P1 argument to
5a380 20 74 68 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e   the OP_Savepoin
5a390 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
5a3a0 2f 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f  /.#define SAVEPO
5a3b0 49 4e 54 5f 42 45 47 49 4e 20 20 20 20 20 20 30  INT_BEGIN      0
5a3c0 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49  .#define SAVEPOI
5a3d0 4e 54 5f 52 45 4c 45 41 53 45 20 20 20 20 31 0a  NT_RELEASE    1.
5a3e0 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e  #define SAVEPOIN
5a3f0 54 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a  T_ROLLBACK   2..
5a400 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69  ./*.** Each SQLi
5a410 74 65 20 6d 6f 64 75 6c 65 20 28 76 69 72 74 75  te module (virtu
5a420 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74  al table definit
5a430 69 6f 6e 29 20 69 73 20 64 65 66 69 6e 65 64 20  ion) is defined 
5a440 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63  by an.** instanc
5a450 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
5a460 6e 67 20 73 74 72 75 63 74 75 72 65 2c 20 73 74  ng structure, st
5a470 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69  ored in the sqli
5a480 74 65 33 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68  te3.aModule.** h
5a490 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ash table..*/.st
5a4a0 72 75 63 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20  ruct Module {.  
5a4b0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
5a4c0 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20  dule *pModule;  
5a4d0 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b       /* Callback
5a4e0 20 70 6f 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63   pointers */.  c
5a4f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
5a500 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5a510 20 20 20 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73      /* Name pass
5a520 65 64 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64  ed to create_mod
5a530 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20  ule() */.  void 
5a540 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20  *pAux;          
5a550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a560 2f 2a 20 70 41 75 78 20 70 61 73 73 65 64 20 74  /* pAux passed t
5a570 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28  o create_module(
5a580 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  ) */.  void (*xD
5a590 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b  estroy)(void *);
5a5a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
5a5b0 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72  odule destructor
5a5c0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a   function */.};.
5a5d0 0a 2f 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  ./*.** informati
5a5e0 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f  on about each co
5a5f0 6c 75 6d 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74  lumn of an SQL t
5a600 61 62 6c 65 20 69 73 20 68 65 6c 64 20 69 6e 20  able is held in 
5a610 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f  an instance.** o
5a620 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
5a630 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75  ..*/.struct Colu
5a640 6d 6e 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  mn {.  char *zNa
5a650 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  me;     /* Name 
5a660 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a  of this column *
5a670 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b  /.  Expr *pDflt;
5a680 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
5a690 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f  value of this co
5a6a0 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  lumn */.  char *
5a6b0 7a 54 79 70 65 3b 20 20 20 20 20 2f 2a 20 44 61  zType;     /* Da
5a6c0 74 61 20 74 79 70 65 20 66 6f 72 20 74 68 69 73  ta type for this
5a6d0 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61   column */.  cha
5a6e0 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a  r *zColl;     /*
5a6f0 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
5a700 6e 63 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75  nce.  If NULL, u
5a710 73 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 2a  se the default *
5a720 2f 0a 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20  /.  u8 notNull; 
5a730 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
5a740 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20 4e  there is a NOT N
5a750 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a  ULL constraint *
5a760 2f 0a 20 20 75 38 20 69 73 50 72 69 6d 4b 65 79  /.  u8 isPrimKey
5a770 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  ;    /* True if 
5a780 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70  this column is p
5a790 61 72 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41  art of the PRIMA
5a7a0 52 59 20 4b 45 59 20 2a 2f 0a 20 20 63 68 61 72  RY KEY */.  char
5a7b0 20 61 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20   affinity;   /* 
5a7c0 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54  One of the SQLIT
5a7d0 45 5f 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73  E_AFF_... values
5a7e0 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
5a7f0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
5a800 41 42 4c 45 0a 20 20 75 38 20 69 73 48 69 64 64  ABLE.  u8 isHidd
5a810 65 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20  en;     /* True 
5a820 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69  if this column i
5a830 73 20 27 68 69 64 64 65 6e 27 20 2a 2f 0a 23 65  s 'hidden' */.#e
5a840 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ndif.};../*.** A
5a850 20 22 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75   "Collating Sequ
5a860 65 6e 63 65 22 20 69 73 20 64 65 66 69 6e 65 64  ence" is defined
5a870 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   by an instance 
5a880 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
5a890 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 43  .** structure. C
5a8a0 6f 6e 63 65 70 74 75 61 6c 6c 79 2c 20 61 20 63  onceptually, a c
5a8b0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
5a8c0 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20  e consists of a 
5a8d0 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 61 20 63 6f  name and.** a co
5a8e0 6d 70 61 72 69 73 6f 6e 20 72 6f 75 74 69 6e 65  mparison routine
5a8f0 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
5a900 65 20 6f 72 64 65 72 20 6f 66 20 74 68 61 74 20  e order of that 
5a910 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  sequence..**.** 
5a920 54 68 65 72 65 20 6d 61 79 20 74 77 6f 20 73 65  There may two se
5a930 70 61 72 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74  parate implement
5a940 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 63 6f  ations of the co
5a950 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  llation function
5a960 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 70 72  , one.** that pr
5a970 6f 63 65 73 73 65 73 20 74 65 78 74 20 69 6e 20  ocesses text in 
5a980 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 28  UTF-8 encoding (
5a990 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 29 20 61 6e  CollSeq.xCmp) an
5a9a0 64 20 61 6e 6f 74 68 65 72 20 74 68 61 74 0a 2a  d another that.*
5a9b0 2a 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 74  * processes text
5a9c0 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d   encoded in UTF-
5a9d0 31 36 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70  16 (CollSeq.xCmp
5a9e0 31 36 29 2c 20 75 73 69 6e 67 20 74 68 65 20 6d  16), using the m
5a9f0 61 63 68 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 65  achine.** native
5aa00 20 62 79 74 65 20 6f 72 64 65 72 2e 20 57 68 65   byte order. Whe
5aa10 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  n a collation se
5aa20 71 75 65 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65  quence is invoke
5aa30 64 2c 20 53 51 4c 69 74 65 20 73 65 6c 65 63 74  d, SQLite select
5aa40 73 0a 2a 2a 20 74 68 65 20 76 65 72 73 69 6f 6e  s.** the version
5aa50 20 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69   that will requi
5aa60 72 65 20 74 68 65 20 6c 65 61 73 74 20 65 78 70  re the least exp
5aa70 65 6e 73 69 76 65 20 65 6e 63 6f 64 69 6e 67 0a  ensive encoding.
5aa80 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c  ** translations,
5aa90 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54   if any..**.** T
5aaa0 68 65 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72  he CollSeq.pUser
5aab0 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65   member variable
5aac0 20 69 73 20 61 6e 20 65 78 74 72 61 20 70 61 72   is an extra par
5aad0 61 6d 65 74 65 72 20 74 68 61 74 20 70 61 73 73  ameter that pass
5aae0 65 64 20 69 6e 0a 2a 2a 20 61 73 20 74 68 65 20  ed in.** as the 
5aaf0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
5ab00 6f 20 74 68 65 20 55 54 46 2d 38 20 63 6f 6d 70  o the UTF-8 comp
5ab10 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c  arison function,
5ab20 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65   xCmp..** CollSe
5ab30 71 2e 70 55 73 65 72 31 36 20 69 73 20 74 68 65  q.pUser16 is the
5ab40 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 20   equivalent for 
5ab50 74 68 65 20 55 54 46 2d 31 36 20 63 6f 6d 70 61  the UTF-16 compa
5ab60 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a  rison function,.
5ab70 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a  ** xCmp16..**.**
5ab80 20 49 66 20 62 6f 74 68 20 43 6f 6c 6c 53 65 71   If both CollSeq
5ab90 2e 78 43 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 65  .xCmp and CollSe
5aba0 71 2e 78 43 6d 70 31 36 20 61 72 65 20 4e 55 4c  q.xCmp16 are NUL
5abb0 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  L, it means that
5abc0 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e   the.** collatin
5abd0 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 75 6e  g sequence is un
5abe0 64 65 66 69 6e 65 64 2e 20 20 49 6e 64 69 63 65  defined.  Indice
5abf0 73 20 62 75 69 6c 74 20 6f 6e 20 61 6e 20 75 6e  s built on an un
5ac00 64 65 66 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61  defined.** colla
5ac10 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6d 61  ting sequence ma
5ac20 79 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72  y not be read or
5ac30 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72   written..*/.str
5ac40 75 63 74 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 20  uct CollSeq {.  
5ac50 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
5ac60 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
5ac70 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
5ac80 65 71 75 65 6e 63 65 2c 20 55 54 46 2d 38 20 65  equence, UTF-8 e
5ac90 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20 65  ncoded */.  u8 e
5aca0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
5acb0 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69    /* Text encodi
5acc0 6e 67 20 68 61 6e 64 6c 65 64 20 62 79 20 78 43  ng handled by xC
5acd0 6d 70 28 29 20 2a 2f 0a 20 20 75 38 20 74 79 70  mp() */.  u8 typ
5ace0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
5acf0 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51  /* One of the SQ
5ad00 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61  LITE_COLL_... va
5ad10 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20  lues below */.  
5ad20 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 20 20  void *pUser;    
5ad30 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
5ad40 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6d 70 28  rgument to xCmp(
5ad50 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d  ) */.  int (*xCm
5ad60 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f  p)(void*,int, co
5ad70 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20  nst void*, int, 
5ad80 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20  const void*);.  
5ad90 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
5ada0 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63  d*);  /* Destruc
5adb0 74 6f 72 20 66 6f 72 20 70 55 73 65 72 20 2a 2f  tor for pUser */
5adc0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77  .};../*.** Allow
5add0 65 64 20 76 61 6c 75 65 73 20 6f 66 20 43 6f 6c  ed values of Col
5ade0 6c 53 65 71 2e 74 79 70 65 3a 0a 2a 2f 0a 23 64  lSeq.type:.*/.#d
5adf0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c  efine SQLITE_COL
5ae00 4c 5f 42 49 4e 41 52 59 20 20 31 20 20 2f 2a 20  L_BINARY  1  /* 
5ae10 54 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d 63  The default memc
5ae20 6d 70 28 29 20 63 6f 6c 6c 61 74 69 6e 67 20 73  mp() collating s
5ae30 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69  equence */.#defi
5ae40 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e  ne SQLITE_COLL_N
5ae50 4f 43 41 53 45 20 20 32 20 20 2f 2a 20 54 68 65  OCASE  2  /* The
5ae60 20 62 75 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45   built-in NOCASE
5ae70 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
5ae80 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  nce */.#define S
5ae90 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 52  QLITE_COLL_REVER
5aea0 53 45 20 33 20 20 2f 2a 20 54 68 65 20 62 75 69  SE 3  /* The bui
5aeb0 6c 74 2d 69 6e 20 52 45 56 45 52 53 45 20 63 6f  lt-in REVERSE co
5aec0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
5aed0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5aee0 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 20 20 20 20  TE_COLL_USER    
5aef0 30 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20  0  /* Any other 
5af00 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c  user-defined col
5af10 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
5af20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74  */../*.** A sort
5af30 20 6f 72 64 65 72 20 63 61 6e 20 62 65 20 65 69   order can be ei
5af40 74 68 65 72 20 41 53 43 20 6f 72 20 44 45 53 43  ther ASC or DESC
5af50 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
5af60 49 54 45 5f 53 4f 5f 41 53 43 20 20 20 20 20 20  ITE_SO_ASC      
5af70 20 30 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61   0  /* Sort in a
5af80 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a  scending order *
5af90 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5afa0 5f 53 4f 5f 44 45 53 43 20 20 20 20 20 20 31 20  _SO_DESC      1 
5afb0 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65   /* Sort in asce
5afc0 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 0a  nding order */..
5afd0 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 66  /*.** Column aff
5afe0 69 6e 69 74 79 20 74 79 70 65 73 2e 0a 2a 2a 0a  inity types..**.
5aff0 2a 2a 20 54 68 65 73 65 20 75 73 65 64 20 74 6f  ** These used to
5b000 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e   have mnemonic n
5b010 61 6d 65 20 6c 69 6b 65 20 27 69 27 20 66 6f 72  ame like 'i' for
5b020 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
5b030 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74 27 20 66  GER and.** 't' f
5b040 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  or SQLITE_AFF_TE
5b050 58 54 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20  XT.  But we can 
5b060 73 61 76 65 20 61 20 6c 69 74 74 6c 65 20 73 70  save a little sp
5b070 61 63 65 20 61 6e 64 20 69 6d 70 72 6f 76 65 0a  ace and improve.
5b080 2a 2a 20 74 68 65 20 73 70 65 65 64 20 61 20 6c  ** the speed a l
5b090 69 74 74 6c 65 20 62 79 20 6e 75 6d 62 65 72 69  ittle by numberi
5b0a0 6e 67 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f  ng the values co
5b0b0 6e 73 65 63 75 74 69 76 65 6c 79 2e 20 20 0a 2a  nsecutively.  .*
5b0c0 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68 65 72 20  *.** But rather 
5b0d0 74 68 61 6e 20 73 74 61 72 74 20 77 69 74 68 20  than start with 
5b0e0 30 20 6f 72 20 31 2c 20 77 65 20 62 65 67 69 6e  0 or 1, we begin
5b0f0 20 77 69 74 68 20 27 61 27 2e 20 20 54 68 61 74   with 'a'.  That
5b100 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75   way,.** when mu
5b110 6c 74 69 70 6c 65 20 61 66 66 69 6e 69 74 79 20  ltiple affinity 
5b120 74 79 70 65 73 20 61 72 65 20 63 6f 6e 63 61 74  types are concat
5b130 65 6e 61 74 65 64 20 69 6e 74 6f 20 61 20 73 74  enated into a st
5b140 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75 73 65 64  ring and.** used
5b150 20 61 73 20 74 68 65 20 50 34 20 6f 70 65 72 61   as the P4 opera
5b160 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c 20 62 65  nd, they will be
5b170 20 6d 6f 72 65 20 72 65 61 64 61 62 6c 65 2e 0a   more readable..
5b180 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c 73 6f 20  **.** Note also 
5b190 74 68 61 74 20 74 68 65 20 6e 75 6d 65 72 69 63  that the numeric
5b1a0 20 74 79 70 65 73 20 61 72 65 20 67 72 6f 75 70   types are group
5b1b0 65 64 20 74 6f 67 65 74 68 65 72 20 73 6f 20 74  ed together so t
5b1c0 68 61 74 20 74 65 73 74 69 6e 67 0a 2a 2a 20 66  hat testing.** f
5b1d0 6f 72 20 61 20 6e 75 6d 65 72 69 63 20 74 79 70  or a numeric typ
5b1e0 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 63 6f  e is a single co
5b1f0 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65  mparison..*/.#de
5b200 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5b210 54 45 58 54 20 20 20 20 20 27 61 27 0a 23 64 65  TEXT     'a'.#de
5b220 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5b230 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a 23 64 65  NONE     'b'.#de
5b240 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5b250 4e 55 4d 45 52 49 43 20 20 27 63 27 0a 23 64 65  NUMERIC  'c'.#de
5b260 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5b270 49 4e 54 45 47 45 52 20 20 27 64 27 0a 23 64 65  INTEGER  'd'.#de
5b280 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5b290 52 45 41 4c 20 20 20 20 20 27 65 27 0a 0a 23 64  REAL     'e'..#d
5b2a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 4e  efine sqlite3IsN
5b2b0 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 58  umericAffinity(X
5b2c0 29 20 20 28 28 58 29 3e 3d 53 51 4c 49 54 45 5f  )  ((X)>=SQLITE_
5b2d0 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a  AFF_NUMERIC)../*
5b2e0 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 41  .** The SQLITE_A
5b2f0 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65 73 20 6d  FF_MASK values m
5b300 61 73 6b 73 20 6f 66 66 20 74 68 65 20 73 69 67  asks off the sig
5b310 6e 69 66 69 63 61 6e 74 20 62 69 74 73 20 6f 66  nificant bits of
5b320 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20   an.** affinity 
5b330 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64 65 66 69  value. .*/.#defi
5b340 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41  ne SQLITE_AFF_MA
5b350 53 4b 20 20 20 20 20 30 78 36 37 0a 0a 2f 2a 0a  SK     0x67../*.
5b360 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 62 69  ** Additional bi
5b370 74 20 76 61 6c 75 65 73 20 74 68 61 74 20 63 61  t values that ca
5b380 6e 20 62 65 20 4f 52 65 64 20 77 69 74 68 20 61  n be ORed with a
5b390 6e 20 61 66 66 69 6e 69 74 79 20 77 69 74 68 6f  n affinity witho
5b3a0 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74  ut.** changing t
5b3b0 68 65 20 61 66 66 69 6e 69 74 79 2e 0a 2a 2f 0a  he affinity..*/.
5b3c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4a  #define SQLITE_J
5b3d0 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30 78 30 38  UMPIFNULL   0x08
5b3e0 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66 20 65 69    /* jumps if ei
5b3f0 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
5b400 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NULL */.#define 
5b410 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 20  SQLITE_STOREP2  
5b420 20 20 20 20 30 78 31 30 20 20 2f 2a 20 53 74 6f      0x10  /* Sto
5b430 72 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  re result in reg
5b440 5b 50 32 5d 20 72 61 74 68 65 72 20 74 68 61 6e  [P2] rather than
5b450 20 6a 75 6d 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20   jump */../*.** 
5b460 45 61 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69  Each SQL table i
5b470 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e  s represented in
5b480 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e   memory by an in
5b490 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a  stance of the.**
5b4a0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
5b4b0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c  ture..**.** Tabl
5b4c0 65 2e 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e  e.zName is the n
5b4d0 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
5b4e0 2e 20 20 54 68 65 20 63 61 73 65 20 6f 66 20 74  .  The case of t
5b4f0 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43  he original.** C
5b500 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
5b510 65 6d 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c  ement is stored,
5b520 20 62 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74   but case is not
5b530 20 73 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72   significant for
5b540 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e  .** comparisons.
5b550 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f  .**.** Table.nCo
5b560 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  l is the number 
5b570 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
5b580 69 73 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65  is table.  Table
5b590 2e 61 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f  .aCol is a.** po
5b5a0 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61  inter to an arra
5b5b0 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75  y of Column stru
5b5c0 63 74 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20  ctures, one for 
5b5d0 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a  each column..**.
5b5e0 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  ** If the table 
5b5f0 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50  has an INTEGER P
5b600 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e  RIMARY KEY, then
5b610 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20   Table.iPKey is 
5b620 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20  the index of.** 
5b630 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  the column that 
5b640 69 73 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f  is that key.   O
5b650 74 68 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69  therwise Table.i
5b660 50 4b 65 79 20 69 73 20 6e 65 67 61 74 69 76 65  PKey is negative
5b670 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20  .  Note.** that 
5b680 74 68 65 20 64 61 74 61 74 79 70 65 20 6f 66 20  the datatype of 
5b690 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
5b6a0 6d 75 73 74 20 62 65 20 49 4e 54 45 47 45 52 20  must be INTEGER 
5b6b0 66 6f 72 20 74 68 69 73 20 66 69 65 6c 64 20 74  for this field t
5b6c0 6f 0a 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e  o.** be set.  An
5b6d0 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
5b6e0 20 4b 45 59 20 69 73 20 75 73 65 64 20 61 73 20   KEY is used as 
5b6f0 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61  the rowid for ea
5b700 63 68 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65  ch row of.** the
5b710 20 74 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61   table.  If a ta
5b720 62 6c 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47  ble has no INTEG
5b730 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
5b740 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f  then a random ro
5b750 77 69 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61  wid.** is genera
5b760 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77  ted for each row
5b770 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20   of the table.  
5b780 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79  TF_HasPrimaryKey
5b790 20 69 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68   is set if.** th
5b7a0 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 79 20  e table has any 
5b7b0 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54  PRIMARY KEY, INT
5b7c0 45 47 45 52 20 6f 72 20 6f 74 68 65 72 77 69 73  EGER or otherwis
5b7d0 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74  e..**.** Table.t
5b7e0 6e 75 6d 20 69 73 20 74 68 65 20 70 61 67 65 20  num is the page 
5b7f0 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72  number for the r
5b800 6f 6f 74 20 42 54 72 65 65 20 70 61 67 65 20 6f  oot BTree page o
5b810 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74  f the table in t
5b820 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
5b830 69 6c 65 2e 20 20 49 66 20 54 61 62 6c 65 2e 69  ile.  If Table.i
5b840 44 62 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  Db is the index 
5b850 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
5b860 74 61 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a  table backend.**
5b870 20 69 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d   in sqlite.aDb[]
5b880 2e 20 20 30 20 69 73 20 66 6f 72 20 74 68 65 20  .  0 is for the 
5b890 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 61 6e  main database an
5b8a0 64 20 31 20 69 73 20 66 6f 72 20 74 68 65 20 66  d 1 is for the f
5b8b0 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64  ile that.** hold
5b8c0 73 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  s temporary tabl
5b8d0 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 20  es and indices. 
5b8e0 20 49 66 20 54 46 5f 45 70 68 65 6d 65 72 61 6c   If TF_Ephemeral
5b8f0 20 69 73 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20   is set.** then 
5b900 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 74 6f  the table is sto
5b910 72 65 64 20 69 6e 20 61 20 66 69 6c 65 20 74 68  red in a file th
5b920 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  at is automatica
5b930 6c 6c 79 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77  lly deleted.** w
5b940 68 65 6e 20 74 68 65 20 56 44 42 45 20 63 75 72  hen the VDBE cur
5b950 73 6f 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65  sor to the table
5b960 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20   is closed.  In 
5b970 74 68 69 73 20 63 61 73 65 20 54 61 62 6c 65 2e  this case Table.
5b980 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20  tnum .** refers 
5b990 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
5b9a0 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  er that holds th
5b9b0 65 20 74 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f  e table open, no
5b9c0 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a  t to the root.**
5b9d0 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54   page number.  T
5b9e0 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 73 20  ransient tables 
5b9f0 61 72 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  are used to hold
5ba00 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   the results of 
5ba10 61 0a 2a 2a 20 73 75 62 2d 71 75 65 72 79 20 74  a.** sub-query t
5ba20 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 73 74  hat appears inst
5ba30 65 61 64 20 6f 66 20 61 20 72 65 61 6c 20 74 61  ead of a real ta
5ba40 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  ble name in the 
5ba50 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20  FROM clause .** 
5ba60 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
5ba70 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74  ement..*/.struct
5ba80 20 54 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74   Table {.  sqlit
5ba90 65 33 20 2a 64 62 4d 65 6d 3b 20 20 20 20 20 20  e3 *dbMem;      
5baa0 2f 2a 20 44 42 20 63 6f 6e 6e 65 63 74 69 6f 6e  /* DB connection
5bab0 20 75 73 65 64 20 66 6f 72 20 6c 6f 6f 6b 61 73   used for lookas
5bac0 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e  ide allocations.
5bad0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
5bae0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  e;         /* Na
5baf0 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
5bb00 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74  or view */.  int
5bb10 20 69 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20   iPKey;         
5bb20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61    /* If not nega
5bb30 74 69 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69  tive, use aCol[i
5bb40 50 4b 65 79 5d 20 61 73 20 74 68 65 20 70 72 69  PKey] as the pri
5bb50 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  mary key */.  in
5bb60 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  t nCol;         
5bb70 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5bb80 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20  columns in this 
5bb90 74 61 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d  table */.  Colum
5bba0 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20  n *aCol;        
5bbb0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
5bbc0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e  bout each column
5bbd0 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e   */.  Index *pIn
5bbe0 64 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69  dex;       /* Li
5bbf0 73 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65  st of SQL indexe
5bc00 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e  s on this table.
5bc10 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20   */.  int tnum; 
5bc20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
5bc30 6f 74 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f  ot BTree node fo
5bc40 72 20 74 68 69 73 20 74 61 62 6c 65 20 28 73 65  r this table (se
5bc50 65 20 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f  e note above) */
5bc60 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
5bc70 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20  ct;     /* NULL 
5bc80 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69  for tables.  Poi
5bc90 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f  nts to definitio
5bca0 6e 20 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a  n if a view. */.
5bcb0 20 20 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20    u16 nRef;     
5bcc0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
5bcd0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
5bce0 74 68 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20  this Table */.  
5bcf0 75 38 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20  u8 tabFlags;    
5bd00 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
5bd10 54 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20  TF_* values */. 
5bd20 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20   u8 keyConf;    
5bd30 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f        /* What to
5bd40 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75   do in case of u
5bd50 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69  niqueness confli
5bd60 63 74 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20  ct on iPKey */. 
5bd70 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20   FKey *pFKey;   
5bd80 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20        /* Linked 
5bd90 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65  list of all fore
5bda0 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73  ign keys in this
5bdb0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
5bdc0 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20   *zColAff;      
5bdd0 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e   /* String defin
5bde0 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79  ing the affinity
5bdf0 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20   of each column 
5be00 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
5be10 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45  E_OMIT_CHECK.  E
5be20 78 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20  xpr *pCheck;    
5be30 20 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f      /* The AND o
5be40 66 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73  f all CHECK cons
5be50 74 72 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69  traints */.#endi
5be60 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
5be70 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45  _OMIT_ALTERTABLE
5be80 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66  .  int addColOff
5be90 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65  set;    /* Offse
5bea0 74 20 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c  t in CREATE TABL
5beb0 45 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20  E stmt to add a 
5bec0 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65  new column */.#e
5bed0 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
5bee0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
5bef0 54 41 42 4c 45 0a 20 20 4d 6f 64 75 6c 65 20 2a  TABLE.  Module *
5bf00 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  pMod;        /* 
5bf10 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69  Pointer to the i
5bf20 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
5bf30 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20   the module */. 
5bf40 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
5bf50 56 74 61 62 3b 20 2f 2a 20 50 6f 69 6e 74 65 72  Vtab; /* Pointer
5bf60 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69   to the module i
5bf70 6e 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69 6e 74  nstance */.  int
5bf80 20 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20   nModuleArg;    
5bf90 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
5bfa0 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
5bfb0 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72  module */.  char
5bfc0 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20   **azModuleArg; 
5bfd0 20 2f 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20   /* Text of all 
5bfe0 6d 6f 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d  module args. [0]
5bff0 20 69 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20   is module name 
5c000 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54 72 69 67  */.#endif.  Trig
5c010 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20  ger *pTrigger;  
5c020 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67   /* List of trig
5c030 67 65 72 73 20 73 74 6f 72 65 64 20 69 6e 20 70  gers stored in p
5c040 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68 65  Schema */.  Sche
5c050 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20  ma *pSchema;    
5c060 20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20   /* Schema that 
5c070 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61  contains this ta
5c080 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ble */.  Table *
5c090 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a  pNextZombie;  /*
5c0a0 20 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72   Next on the Par
5c0b0 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69  se.pZombieTab li
5c0c0 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  st */.};../*.** 
5c0d0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66  Allowed values f
5c0e0 6f 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73  or Tabe.tabFlags
5c0f0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f  ..*/.#define TF_
5c100 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20  Readonly        
5c110 30 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d  0x01    /* Read-
5c120 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c  only system tabl
5c130 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f  e */.#define TF_
5c140 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20  Ephemeral       
5c150 30 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70  0x02    /* An ep
5c160 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f  hemeral table */
5c170 0a 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50  .#define TF_HasP
5c180 72 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34  rimaryKey   0x04
5c190 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73      /* Table has
5c1a0 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a   a primary key *
5c1b0 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74  /.#define TF_Aut
5c1c0 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30  oincrement   0x0
5c1d0 38 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20  8    /* Integer 
5c1e0 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61  primary key is a
5c1f0 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a  utoincrement */.
5c200 23 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75  #define TF_Virtu
5c210 61 6c 20 20 20 20 20 20 20 20 20 30 78 31 30 20  al         0x10 
5c220 20 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75     /* Is a virtu
5c230 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66  al table */.#def
5c240 69 6e 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64  ine TF_NeedMetad
5c250 61 74 61 20 20 20 20 30 78 32 30 20 20 20 20 2f  ata    0x20    /
5c260 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61  * aCol[].zType a
5c270 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20  nd aCol[].pColl 
5c280 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a  missing */..../*
5c290 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20  .** Test to see 
5c2a0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  whether or not a
5c2b0 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74   table is a virt
5c2c0 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ual table.  This
5c2d0 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61   is.** done as a
5c2e0 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69   macro so that i
5c2f0 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69  t will be optimi
5c300 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72  zed out when vir
5c310 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75  tual.** table su
5c320 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64  pport is omitted
5c330 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e   from the build.
5c340 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
5c350 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
5c360 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49  ABLE.#  define I
5c370 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20  sVirtual(X)     
5c380 20 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73   (((X)->tabFlags
5c390 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d   & TF_Virtual)!=
5c3a0 30 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48  0).#  define IsH
5c3b0 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28  iddenColumn(X) (
5c3c0 28 58 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23  (X)->isHidden).#
5c3d0 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49  else.#  define I
5c3e0 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20  sVirtual(X)     
5c3f0 20 30 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48   0.#  define IsH
5c400 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30  iddenColumn(X) 0
5c410 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45  .#endif../*.** E
5c420 61 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ach foreign key 
5c430 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e  constraint is an
5c440 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
5c450 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
5c460 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f  ture..**.** A fo
5c470 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73  reign key is ass
5c480 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 77 6f  ociated with two
5c490 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66   tables.  The "f
5c4a0 72 6f 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a  rom" table is.**
5c4b0 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
5c4c0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46  contains the REF
5c4d0 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74  ERENCES clause t
5c4e0 68 61 74 20 63 72 65 61 74 65 73 20 74 68 65 20  hat creates the 
5c4f0 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20  foreign.** key. 
5c500 20 54 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20   The "to" table 
5c510 69 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  is the table tha
5c520 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68  t is named in th
5c530 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61  e REFERENCES cla
5c540 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  use..** Consider
5c550 20 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a   this example:.*
5c560 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  *.**     CREATE 
5c570 54 41 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20  TABLE ex1(.**   
5c580 20 20 20 20 61 20 49 4e 54 45 47 45 52 20 50 52      a INTEGER PR
5c590 49 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20  IMARY KEY,.**   
5c5a0 20 20 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f      b INTEGER CO
5c5b0 4e 53 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46  NSTRAINT fk1 REF
5c5c0 45 52 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a  ERENCES ex2(x).*
5c5d0 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46  *     );.**.** F
5c5e0 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22  or foreign key "
5c5f0 66 6b 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74  fk1", the from-t
5c600 61 62 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e  able is "ex1" an
5c610 64 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69  d the to-table i
5c620 73 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45  s "ex2"..**.** E
5c630 61 63 68 20 52 45 46 45 52 45 4e 43 45 53 20 63  ach REFERENCES c
5c640 6c 61 75 73 65 20 67 65 6e 65 72 61 74 65 73 20  lause generates 
5c650 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
5c660 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
5c670 75 63 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20  ucture.** which 
5c680 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  is attached to t
5c690 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20  he from-table.  
5c6a0 54 68 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65  The to-table nee
5c6b0 64 20 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e  d not exist when
5c6c0 0a 2a 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62  .** the from-tab
5c6d0 6c 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  le is created.  
5c6e0 54 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66  The existence of
5c6f0 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73   the to-table is
5c700 20 6e 6f 74 20 63 68 65 63 6b 65 64 2e 0a 2a 2f   not checked..*/
5c710 0a 73 74 72 75 63 74 20 46 4b 65 79 20 7b 0a 20  .struct FKey {. 
5c720 20 54 61 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20   Table *pFrom;  
5c730 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
5c740 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
5c750 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61  e REFERENCES cla
5c760 75 73 65 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70  use */.  FKey *p
5c770 4e 65 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65  NextFrom;  /* Ne
5c780 78 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69  xt foreign key i
5c790 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61  n pFrom */.  cha
5c7a0 72 20 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f  r *zTo;        /
5c7b0 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20  * Name of table 
5c7c0 74 68 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69  that the key poi
5c7d0 6e 74 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  nts to */.  int 
5c7e0 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  nCol;         /*
5c7f0 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
5c800 6e 73 20 69 6e 20 74 68 69 73 20 6b 65 79 20 2a  ns in this key *
5c810 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72 65  /.  u8 isDeferre
5c820 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  d;    /* True if
5c830 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63   constraint chec
5c840 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64  king is deferred
5c850 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a   till COMMIT */.
5c860 20 20 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b    u8 updateConf;
5c870 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65      /* How to re
5c880 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20  solve conflicts 
5c890 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50  that occur on UP
5c8a0 44 41 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c  DATE */.  u8 del
5c8b0 65 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48  eteConf;    /* H
5c8c0 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f  ow to resolve co
5c8d0 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63  nflicts that occ
5c8e0 75 72 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a  ur on DELETE */.
5c8f0 20 20 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b    u8 insertConf;
5c900 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65      /* How to re
5c910 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20  solve conflicts 
5c920 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e  that occur on IN
5c930 53 45 52 54 20 2a 2f 0a 20 20 73 74 72 75 63 74  SERT */.  struct
5c940 20 73 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d   sColMap {  /* M
5c950 61 70 70 69 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e  apping of column
5c960 73 20 69 6e 20 70 46 72 6f 6d 20 74 6f 20 63 6f  s in pFrom to co
5c970 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a  lumns in zTo */.
5c980 20 20 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20      int iFrom;  
5c990 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
5c9a0 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 46 72  of column in pFr
5c9b0 6f 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a  om */.    char *
5c9c0 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20  zCol;        /* 
5c9d0 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69  Name of column i
5c9e0 6e 20 7a 54 6f 2e 20 20 49 66 20 30 20 75 73 65  n zTo.  If 0 use
5c9f0 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a   PRIMARY KEY */.
5ca00 20 20 7d 20 61 43 6f 6c 5b 31 5d 3b 20 20 20 20    } aCol[1];    
5ca10 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79      /* One entry
5ca20 20 66 6f 72 20 65 61 63 68 20 6f 66 20 6e 43 6f   for each of nCo
5ca30 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b  l column s */.};
5ca40 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 73  ../*.** SQLite s
5ca50 75 70 70 6f 72 74 73 20 6d 61 6e 79 20 64 69 66  upports many dif
5ca60 66 65 72 65 6e 74 20 77 61 79 73 20 74 6f 20 72  ferent ways to r
5ca70 65 73 6f 6c 76 65 20 61 20 63 6f 6e 73 74 72 61  esolve a constra
5ca80 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 52  int.** error.  R
5ca90 4f 4c 4c 42 41 43 4b 20 70 72 6f 63 65 73 73 69  OLLBACK processi
5caa0 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  ng means that a 
5cab0 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
5cac0 74 69 6f 6e 0a 2a 2a 20 63 61 75 73 65 73 20 74  tion.** causes t
5cad0 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20  he operation in 
5cae0 70 72 6f 63 65 73 73 20 74 6f 20 66 61 69 6c 20  process to fail 
5caf0 61 6e 64 20 66 6f 72 20 74 68 65 20 63 75 72 72  and for the curr
5cb00 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ent transaction.
5cb10 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20  ** to be rolled 
5cb20 62 61 63 6b 2e 20 20 41 42 4f 52 54 20 70 72 6f  back.  ABORT pro
5cb30 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68  cessing means th
5cb40 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70  e operation in p
5cb50 72 6f 63 65 73 73 0a 2a 2a 20 66 61 69 6c 73 20  rocess.** fails 
5cb60 61 6e 64 20 61 6e 79 20 70 72 69 6f 72 20 63 68  and any prior ch
5cb70 61 6e 67 65 73 20 66 72 6f 6d 20 74 68 61 74 20  anges from that 
5cb80 6f 6e 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72  one operation ar
5cb90 65 20 62 61 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a  e backed out,.**
5cba0 20 62 75 74 20 74 68 65 20 74 72 61 6e 73 61 63   but the transac
5cbb0 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c  tion is not roll
5cbc0 65 64 20 62 61 63 6b 2e 20 20 46 41 49 4c 20 70  ed back.  FAIL p
5cbd0 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20  rocessing means 
5cbe0 74 68 61 74 0a 2a 2a 20 74 68 65 20 6f 70 65 72  that.** the oper
5cbf0 61 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73  ation in progres
5cc00 73 20 73 74 6f 70 73 20 61 6e 64 20 72 65 74 75  s stops and retu
5cc10 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  rns an error cod
5cc20 65 2e 20 20 42 75 74 20 70 72 69 6f 72 0a 2a 2a  e.  But prior.**
5cc30 20 63 68 61 6e 67 65 73 20 64 75 65 20 74 6f 20   changes due to 
5cc40 74 68 65 20 73 61 6d 65 20 6f 70 65 72 61 74 69  the same operati
5cc50 6f 6e 20 61 72 65 20 6e 6f 74 20 62 61 63 6b 65  on are not backe
5cc60 64 20 6f 75 74 20 61 6e 64 20 6e 6f 20 72 6f 6c  d out and no rol
5cc70 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e  lback.** occurs.
5cc80 20 20 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74    IGNORE means t
5cc90 68 61 74 20 74 68 65 20 70 61 72 74 69 63 75 6c  hat the particul
5cca0 61 72 20 72 6f 77 20 74 68 61 74 20 63 61 75 73  ar row that caus
5ccb0 65 64 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  ed the constrain
5ccc0 74 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 6e 6f  t.** error is no
5ccd0 74 20 69 6e 73 65 72 74 65 64 20 6f 72 20 75 70  t inserted or up
5cce0 64 61 74 65 64 2e 20 20 50 72 6f 63 65 73 73 69  dated.  Processi
5ccf0 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e 64  ng continues and
5cd00 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a 20 69 73 20   no error.** is 
5cd10 72 65 74 75 72 6e 65 64 2e 20 20 52 45 50 4c 41  returned.  REPLA
5cd20 43 45 20 6d 65 61 6e 73 20 74 68 61 74 20 70 72  CE means that pr
5cd30 65 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61  eexisting databa
5cd40 73 65 20 72 6f 77 73 20 74 68 61 74 20 63 61 75  se rows that cau
5cd50 73 65 64 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20  sed.** a UNIQUE 
5cd60 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
5cd70 74 69 6f 6e 20 61 72 65 20 72 65 6d 6f 76 65 64  tion are removed
5cd80 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77   so that the new
5cd90 20 69 6e 73 65 72 74 20 6f 72 0a 2a 2a 20 75 70   insert or.** up
5cda0 64 61 74 65 20 63 61 6e 20 70 72 6f 63 65 65 64  date can proceed
5cdb0 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f  .  Processing co
5cdc0 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65  ntinues and no e
5cdd0 72 72 6f 72 20 69 73 20 72 65 70 6f 72 74 65 64  rror is reported
5cde0 2e 0a 2a 2a 0a 2a 2a 20 52 45 53 54 52 49 43 54  ..**.** RESTRICT
5cdf0 2c 20 53 45 54 4e 55 4c 4c 2c 20 61 6e 64 20 43  , SETNULL, and C
5ce00 41 53 43 41 44 45 20 61 63 74 69 6f 6e 73 20 61  ASCADE actions a
5ce10 70 70 6c 79 20 6f 6e 6c 79 20 74 6f 20 66 6f 72  pply only to for
5ce20 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2a 20 52 45  eign keys..** RE
5ce30 53 54 52 49 43 54 20 69 73 20 74 68 65 20 73 61  STRICT is the sa
5ce40 6d 65 20 61 73 20 41 42 4f 52 54 20 66 6f 72 20  me as ABORT for 
5ce50 49 4d 4d 45 44 49 41 54 45 20 66 6f 72 65 69 67  IMMEDIATE foreig
5ce60 6e 20 6b 65 79 73 20 61 6e 64 20 74 68 65 0a 2a  n keys and the.*
5ce70 2a 20 73 61 6d 65 20 61 73 20 52 4f 4c 4c 42 41  * same as ROLLBA
5ce80 43 4b 20 66 6f 72 20 44 45 46 45 52 52 45 44 20  CK for DEFERRED 
5ce90 6b 65 79 73 2e 20 20 53 45 54 4e 55 4c 4c 20 6d  keys.  SETNULL m
5cea0 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f  eans that the fo
5ceb0 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 20 69 73 20  reign.** key is 
5cec0 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 41  set to NULL.  CA
5ced0 53 43 41 44 45 20 6d 65 61 6e 73 20 74 68 61 74  SCADE means that
5cee0 20 61 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44   a DELETE or UPD
5cef0 41 54 45 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65  ATE of the.** re
5cf00 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 20 72  ferenced table r
5cf10 6f 77 20 69 73 20 70 72 6f 70 61 67 61 74 65 64  ow is propagated
5cf20 20 69 6e 74 6f 20 74 68 65 20 72 6f 77 20 74 68   into the row th
5cf30 61 74 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a 20  at holds the.** 
5cf40 66 6f 72 65 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20  foreign key..** 
5cf50 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
5cf60 67 20 73 79 6d 62 6f 6c 69 63 20 76 61 6c 75 65  g symbolic value
5cf70 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 72 65  s are used to re
5cf80 63 6f 72 64 20 77 68 69 63 68 20 74 79 70 65 0a  cord which type.
5cf90 2a 2a 20 6f 66 20 61 63 74 69 6f 6e 20 74 6f 20  ** of action to 
5cfa0 74 61 6b 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  take..*/.#define
5cfb0 20 4f 45 5f 4e 6f 6e 65 20 20 20 20 20 30 20 20   OE_None     0  
5cfc0 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20   /* There is no 
5cfd0 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 63 68  constraint to ch
5cfe0 65 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  eck */.#define O
5cff0 45 5f 52 6f 6c 6c 62 61 63 6b 20 31 20 20 20 2f  E_Rollback 1   /
5d000 2a 20 46 61 69 6c 20 74 68 65 20 6f 70 65 72 61  * Fail the opera
5d010 74 69 6f 6e 20 61 6e 64 20 72 6f 6c 6c 62 61 63  tion and rollbac
5d020 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  k the transactio
5d030 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  n */.#define OE_
5d040 41 62 6f 72 74 20 20 20 20 32 20 20 20 2f 2a 20  Abort    2   /* 
5d050 42 61 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73  Back out changes
5d060 20 62 75 74 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62   but do no rollb
5d070 61 63 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ack transaction 
5d080 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 46 61  */.#define OE_Fa
5d090 69 6c 20 20 20 20 20 33 20 20 20 2f 2a 20 53 74  il     3   /* St
5d0a0 6f 70 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  op the operation
5d0b0 20 62 75 74 20 6c 65 61 76 65 20 61 6c 6c 20 70   but leave all p
5d0c0 72 69 6f 72 20 63 68 61 6e 67 65 73 20 2a 2f 0a  rior changes */.
5d0d0 23 64 65 66 69 6e 65 20 4f 45 5f 49 67 6e 6f 72  #define OE_Ignor
5d0e0 65 20 20 20 34 20 20 20 2f 2a 20 49 67 6e 6f 72  e   4   /* Ignor
5d0f0 65 20 74 68 65 20 65 72 72 6f 72 2e 20 44 6f 20  e the error. Do 
5d100 6e 6f 74 20 64 6f 20 74 68 65 20 49 4e 53 45 52  not do the INSER
5d110 54 20 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 23  T or UPDATE */.#
5d120 64 65 66 69 6e 65 20 4f 45 5f 52 65 70 6c 61 63  define OE_Replac
5d130 65 20 20 35 20 20 20 2f 2a 20 44 65 6c 65 74 65  e  5   /* Delete
5d140 20 65 78 69 73 74 69 6e 67 20 72 65 63 6f 72 64   existing record
5d150 2c 20 74 68 65 6e 20 64 6f 20 49 4e 53 45 52 54  , then do INSERT
5d160 20 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 0a 23   or UPDATE */..#
5d170 64 65 66 69 6e 65 20 4f 45 5f 52 65 73 74 72 69  define OE_Restri
5d180 63 74 20 36 20 20 20 2f 2a 20 4f 45 5f 41 62 6f  ct 6   /* OE_Abo
5d190 72 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45  rt for IMMEDIATE
5d1a0 2c 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f  , OE_Rollback fo
5d1b0 72 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 23 64  r DEFERRED */.#d
5d1c0 65 66 69 6e 65 20 4f 45 5f 53 65 74 4e 75 6c 6c  efine OE_SetNull
5d1d0 20 20 37 20 20 20 2f 2a 20 53 65 74 20 74 68 65    7   /* Set the
5d1e0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c   foreign key val
5d1f0 75 65 20 74 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64  ue to NULL */.#d
5d200 65 66 69 6e 65 20 4f 45 5f 53 65 74 44 66 6c 74  efine OE_SetDflt
5d210 20 20 38 20 20 20 2f 2a 20 53 65 74 20 74 68 65    8   /* Set the
5d220 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c   foreign key val
5d230 75 65 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c  ue to its defaul
5d240 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  t */.#define OE_
5d250 43 61 73 63 61 64 65 20 20 39 20 20 20 2f 2a 20  Cascade  9   /* 
5d260 43 61 73 63 61 64 65 20 74 68 65 20 63 68 61 6e  Cascade the chan
5d270 67 65 73 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20  ges */..#define 
5d280 4f 45 5f 44 65 66 61 75 6c 74 20 20 39 39 20 20  OE_Default  99  
5d290 2f 2a 20 44 6f 20 77 68 61 74 65 76 65 72 20 74  /* Do whatever t
5d2a0 68 65 20 64 65 66 61 75 6c 74 20 61 63 74 69 6f  he default actio
5d2b0 6e 20 69 73 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  n is */.../*.** 
5d2c0 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
5d2d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
5d2e0 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64  ucture is passed
5d2f0 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   as the first.**
5d300 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
5d310 69 74 65 33 56 64 62 65 4b 65 79 43 6f 6d 70 61  ite3VdbeKeyCompa
5d320 72 65 20 61 6e 64 20 69 73 20 75 73 65 64 20 74  re and is used t
5d330 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 0a 2a  o control the .*
5d340 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20  * comparison of 
5d350 74 68 65 20 74 77 6f 20 69 6e 64 65 78 20 6b 65  the two index ke
5d360 79 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4b 65  ys..*/.struct Ke
5d370 79 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65  yInfo {.  sqlite
5d380 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a  3 *db;        /*
5d390 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
5d3a0 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38  nnection */.  u8
5d3b0 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20   enc;           
5d3c0 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69    /* Text encodi
5d3d0 6e 67 20 2d 20 6f 6e 65 20 6f 66 20 74 68 65 20  ng - one of the 
5d3e0 54 45 58 54 5f 55 74 66 2a 20 76 61 6c 75 65 73  TEXT_Utf* values
5d3f0 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64   */.  u16 nField
5d400 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
5d410 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
5d420 6e 20 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75  n aColl[] */.  u
5d430 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20  8 *aSortOrder;  
5d440 20 20 20 2f 2a 20 49 66 20 64 65 66 69 6e 65 64     /* If defined
5d450 20 61 6e 20 61 53 6f 72 74 4f 72 64 65 72 5b 69   an aSortOrder[i
5d460 5d 20 69 73 20 74 72 75 65 2c 20 73 6f 72 74 20  ] is true, sort 
5d470 44 45 53 43 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65  DESC */.  CollSe
5d480 71 20 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a  q *aColl[1];  /*
5d490 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
5d4a0 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74 65 72  nce for each ter
5d4b0 6d 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a  m of the key */.
5d4c0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
5d4d0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
5d4e0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
5d4f0 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69   holds informati
5d500 6f 6e 20 61 62 6f 75 74 20 61 0a 2a 2a 20 73 69  on about a.** si
5d510 6e 67 6c 65 20 69 6e 64 65 78 20 72 65 63 6f 72  ngle index recor
5d520 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61  d that has alrea
5d530 64 79 20 62 65 65 6e 20 70 61 72 73 65 64 20 6f  dy been parsed o
5d540 75 74 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75  ut into individu
5d550 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a  al.** values..**
5d560 0a 2a 2a 20 41 20 72 65 63 6f 72 64 20 69 73 20  .** A record is 
5d570 61 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63  an object that c
5d580 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d  ontains one or m
5d590 6f 72 65 20 66 69 65 6c 64 73 20 6f 66 20 64 61  ore fields of da
5d5a0 74 61 2e 0a 2a 2a 20 52 65 63 6f 72 64 73 20 61  ta..** Records a
5d5b0 72 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  re used to store
5d5c0 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
5d5d0 61 20 74 61 62 6c 65 20 72 6f 77 20 61 6e 64 20  a table row and 
5d5e0 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 68 65 20  to store.** the 
5d5f0 6b 65 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 2e  key of an index.
5d600 20 20 41 20 62 6c 6f 62 20 65 6e 63 6f 64 69 6e    A blob encodin
5d610 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 73  g of a record is
5d620 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74   created by.** t
5d630 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  he OP_MakeRecord
5d640 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56   opcode of the V
5d650 44 42 45 20 61 6e 64 20 69 73 20 64 69 73 61 73  DBE and is disas
5d660 73 65 6d 62 6c 65 64 20 62 79 20 74 68 65 0a 2a  sembled by the.*
5d670 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f  * OP_Column opco
5d680 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  de..**.** This s
5d690 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61  tructure holds a
5d6a0 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61 73   record that has
5d6b0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64 69   already been di
5d6c0 73 61 73 73 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e  sassembled.** in
5d6d0 74 6f 20 69 74 73 20 63 6f 6e 73 74 69 74 75 65  to its constitue
5d6e0 6e 74 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74  nt fields..*/.st
5d6f0 72 75 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63  ruct UnpackedRec
5d700 6f 72 64 20 7b 0a 20 20 4b 65 79 49 6e 66 6f 20  ord {.  KeyInfo 
5d710 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 43  *pKeyInfo;  /* C
5d720 6f 6c 6c 61 74 69 6f 6e 20 61 6e 64 20 73 6f 72  ollation and sor
5d730 74 2d 6f 72 64 65 72 20 69 6e 66 6f 72 6d 61 74  t-order informat
5d740 69 6f 6e 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69  ion */.  u16 nFi
5d750 65 6c 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  eld;         /* 
5d760 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
5d770 73 20 69 6e 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a  s in apMem[] */.
5d780 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20    u16 flags;    
5d790 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e        /* Boolean
5d7a0 20 73 65 74 74 69 6e 67 73 2e 20 20 55 4e 50 41   settings.  UNPA
5d7b0 43 4b 45 44 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a  CKED_... below *
5d7c0 2f 0a 20 20 69 36 34 20 72 6f 77 69 64 3b 20 20  /.  i64 rowid;  
5d7d0 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
5d7e0 62 79 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46  by UNPACKED_PREF
5d7f0 49 58 5f 53 45 41 52 43 48 20 2a 2f 0a 20 20 4d  IX_SEARCH */.  M
5d800 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20  em *aMem;       
5d810 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a     /* Values */.
5d820 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65  };../*.** Allowe
5d830 64 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61  d values of Unpa
5d840 63 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73  ckedRecord.flags
5d850 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41  .*/.#define UNPA
5d860 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20  CKED_NEED_FREE  
5d870 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65     0x0001  /* Me
5d880 6d 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c  mory is from sql
5d890 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a  ite3Malloc() */.
5d8a0 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44  #define UNPACKED
5d8b0 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30  _NEED_DESTROY  0
5d8c0 78 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b  x0002  /* apMem[
5d8d0 5d 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65  ]s should all be
5d8e0 20 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64   destroyed */.#d
5d8f0 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49  efine UNPACKED_I
5d900 47 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30  GNORE_ROWID  0x0
5d910 30 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74  004  /* Ignore t
5d920 72 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e  railing rowid on
5d930 20 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65   key1 */.#define
5d940 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45   UNPACKED_INCRKE
5d950 59 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20  Y       0x0008  
5d960 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79  /* Make this key
5d970 20 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67   an epsilon larg
5d980 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e  er */.#define UN
5d990 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41  PACKED_PREFIX_MA
5d9a0 54 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20  TCH  0x0010  /* 
5d9b0 41 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69  A prefix match i
5d9c0 73 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20  s considered OK 
5d9d0 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43  */.#define UNPAC
5d9e0 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43  KED_PREFIX_SEARC
5d9f0 48 20 30 78 30 30 32 30 20 20 2f 2a 20 41 20 70  H 0x0020  /* A p
5da00 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63  refix match is c
5da10 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a  onsidered OK */.
5da20 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20  ./*.** Each SQL 
5da30 69 6e 64 65 78 20 69 73 20 72 65 70 72 65 73 65  index is represe
5da40 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62  nted in memory b
5da50 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  y an.** instance
5da60 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
5da70 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
5da80 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f  ** The columns o
5da90 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
5daa0 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64 65 78   are to be index
5dab0 65 64 20 61 72 65 20 64 65 73 63 72 69 62 65 64  ed are described
5dac0 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43 6f 6c  .** by the aiCol
5dad0 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66 20 74  umn[] field of t
5dae0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20  his structure.  
5daf0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70  For example, sup
5db00 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76 65 20  pose.** we have 
5db10 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61  the following ta
5db20 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a 0a 2a  ble and index:.*
5db30 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  *.**     CREATE 
5db40 54 41 42 4c 45 20 45 78 31 28 63 31 20 69 6e 74  TABLE Ex1(c1 int
5db50 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74 65 78  , c2 int, c3 tex
5db60 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  t);.**     CREAT
5db70 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e 20 45  E INDEX Ex2 ON E
5db80 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a  x1(c3,c1);.**.**
5db90 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74   In the Table st
5dba0 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 69  ructure describi
5dbb0 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20  ng Ex1, nCol==3 
5dbc0 62 65 63 61 75 73 65 20 74 68 65 72 65 20 61 72  because there ar
5dbd0 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c 75 6d  e.** three colum
5dbe0 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ns in the table.
5dbf0 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78 20 73    In the Index s
5dc00 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62  tructure describ
5dc10 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c  ing.** Ex2, nCol
5dc20 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32 20 6f  umn==2 since 2 o
5dc30 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e 73 20  f the 3 columns 
5dc40 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64 65 78  of Ex1 are index
5dc50 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  ed..** The value
5dc60 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69 73 20   of aiColumn is 
5dc70 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c 75 6d  {2, 0}.  aiColum
5dc80 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73 65 20  n[0]==2 because 
5dc90 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 63 6f  the .** first co
5dca0 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78  lumn to be index
5dcb0 65 64 20 28 63 33 29 20 68 61 73 20 61 6e 20 69  ed (c3) has an i
5dcc0 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45 78 31  ndex of 2 in Ex1
5dcd0 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20  .aCol[]..** The 
5dce0 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f  second column to
5dcf0 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 31 29   be indexed (c1)
5dd00 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
5dd10 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f   0 in.** Ex1.aCo
5dd20 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32 2e 61  l[], hence Ex2.a
5dd30 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a  iColumn[1]==0..*
5dd40 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78 2e 6f  *.** The Index.o
5dd50 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64 65 74  nError field det
5dd60 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20  ermines whether 
5dd70 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 65 78  or not the index
5dd80 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75  ed columns.** mu
5dd90 73 74 20 62 65 20 75 6e 69 71 75 65 20 61 6e 64  st be unique and
5dda0 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66 20 74   what to do if t
5ddb0 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 57 68  hey are not.  Wh
5ddc0 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72  en Index.onError
5ddd0 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20  =OE_None,.** it 
5dde0 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 6e 6f  means this is no
5ddf0 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78  t a unique index
5de00 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 20  .  Otherwise it 
5de10 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65  is a unique inde
5de20 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c  x.** and the val
5de30 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e 45 72  ue of Index.onEr
5de40 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74 68 65  ror indicate the
5de50 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63 74 20   which conflict 
5de60 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61  resolution .** a
5de70 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d 70 6c  lgorithm to empl
5de80 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e 20 61  oy whenever an a
5de90 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74  ttempt is made t
5dea0 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e 2d 75  o insert a non-u
5deb0 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74  nique.** element
5dec0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65  ..*/.struct Inde
5ded0 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  x {.  char *zNam
5dee0 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  e;     /* Name o
5def0 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a  f this index */.
5df00 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20    int nColumn;  
5df10 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5df20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74  columns in the t
5df30 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 69  able used by thi
5df40 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  s index */.  int
5df50 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a   *aiColumn;   /*
5df60 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 61   Which columns a
5df70 72 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20  re used by this 
5df80 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73 20 30  index.  1st is 0
5df90 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a   */.  unsigned *
5dfa0 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52 65 73  aiRowEst; /* Res
5dfb0 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45 3a 20  ult of ANALYZE: 
5dfc0 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65 63 74  Est. rows select
5dfd0 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c 75 6d  ed by each colum
5dfe0 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  n */.  Table *pT
5dff0 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65 20 53  able;   /* The S
5e000 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69  QL table being i
5e010 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ndexed */.  int 
5e020 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20  tnum;        /* 
5e030 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  Page containing 
5e040 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69 6e 64  root of this ind
5e050 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 20 66  ex in database f
5e060 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e 45 72  ile */.  u8 onEr
5e070 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f 45 5f  ror;      /* OE_
5e080 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65  Abort, OE_Ignore
5e090 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72  , OE_Replace, or
5e0a0 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38   OE_None */.  u8
5e0b0 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20 20 2f   autoIndex;    /
5e0c0 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 75 74  * True if is aut
5e0d0 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74  omatically creat
5e0e0 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49 51 55  ed (ex: by UNIQU
5e0f0 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43  E) */.  char *zC
5e100 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74 72 69  olAff;   /* Stri
5e110 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20  ng defining the 
5e120 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68  affinity of each
5e130 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64   column */.  Ind
5e140 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a  ex *pNext;    /*
5e150 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20   The next index 
5e160 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
5e170 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a  the same table *
5e180 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  /.  Schema *pSch
5e190 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61 20 63  ema; /* Schema c
5e1a0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 69  ontaining this i
5e1b0 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 61 53  ndex */.  u8 *aS
5e1c0 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20 41 72  ortOrder;  /* Ar
5e1d0 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e 64 65  ray of size Inde
5e1e0 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d  x.nColumn. True=
5e1f0 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d 41 53  =DESC, False==AS
5e200 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a  C */.  char **az
5e210 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 61 79  Coll;   /* Array
5e220 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65   of collation se
5e230 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66 6f 72  quence names for
5e240 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   index */.};../*
5e250 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e 20 63  .** Each token c
5e260 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74 68 65  oming out of the
5e270 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69 6e 73   lexer is an ins
5e280 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73  tance of.** this
5e290 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 6f 6b   structure.  Tok
5e2a0 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75 73 65  ens are also use
5e2b0 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20  d as part of an 
5e2c0 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a  expression..**.*
5e2d0 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65 6e 2e  * Note if Token.
5e2e0 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65 6e 2e  z==0 then Token.
5e2f0 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20  dyn and Token.n 
5e300 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 61 6e  are undefined an
5e310 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e  d.** may contain
5e320 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73 2e 20   random values. 
5e330 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79   Do not make any
5e340 20 61 73 73 75 6d 70 74 69 6f 6e 73 20 61 62 6f   assumptions abo
5e350 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a 20  ut Token.dyn.** 
5e360 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65 6e  and Token.n when
5e370 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a   Token.z==0..*/.
5e380 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a 20  struct Token {. 
5e390 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
5e3a0 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65 78 74  char *z; /* Text
5e3b0 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20 20   of the token.  
5e3c0 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61  Not NULL-termina
5e3d0 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  ted! */.  unsign
5e3e0 65 64 20 64 79 6e 20 20 20 20 3a 20 31 3b 20 20  ed dyn    : 1;  
5e3f0 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6d 61    /* True for ma
5e400 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c 20 66  lloced memory, f
5e410 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69 63 20  alse for static 
5e420 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 71 75  */.  unsigned qu
5e430 6f 74 65 64 20 3a 20 31 3b 20 20 20 20 2f 2a 20  oted : 1;    /* 
5e440 54 72 75 65 20 69 66 20 74 6f 6b 65 6e 20 73 74  True if token st
5e450 69 6c 6c 20 68 61 73 20 69 74 73 20 71 75 6f 74  ill has its quot
5e460 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  es */.  unsigned
5e470 20 6e 20 20 20 20 20 20 3a 20 33 30 3b 20 20 20   n      : 30;   
5e480 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
5e490 72 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20  racters in this 
5e4a0 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  token */.};../*.
5e4b0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
5e4c0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
5e4d0 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d   contains inform
5e4e0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20  ation needed to 
5e4f0 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65  generate.** code
5e500 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68   for a SELECT th
5e510 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72  at contains aggr
5e520 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e  egate functions.
5e530 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f  .**.** If Expr.o
5e540 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
5e550 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54   or TK_AGG_FUNCT
5e560 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41  ION then Expr.pA
5e570 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70  ggInfo is a.** p
5e580 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
5e590 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45  tructure.  The E
5e5a0 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c  xpr.iColumn fiel
5e5b0 64 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69  d is the index i
5e5c0 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f  n.** AggInfo.aCo
5e5d0 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61  l[] or AggInfo.a
5e5e0 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d  Func[] of inform
5e5f0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20  ation needed to 
5e600 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65  generate.** code
5e610 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a   for that node..
5e620 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47  **.** AggInfo.pG
5e630 72 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e  roupBy and AggIn
5e640 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70  fo.aFunc.pExpr p
5e650 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77  oint to fields w
5e660 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69  ithin the.** ori
5e670 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72  ginal Select str
5e680 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63  ucture that desc
5e690 72 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54  ribes the SELECT
5e6a0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
5e6b0 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20  se.** fields do 
5e6c0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66  not need to be f
5e6d0 72 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f  reed when deallo
5e6e0 63 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e  cating the AggIn
5e6f0 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  fo structure..*/
5e700 0a 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20  .struct AggInfo 
5e710 7b 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64  {.  u8 directMod
5e720 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  e;          /* D
5e730 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20  irect rendering 
5e740 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20  mode means take 
5e750 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20  data directly.  
5e760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e770 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20          ** from 
5e780 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61  source tables ra
5e790 74 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61  ther than from a
5e7a0 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20  ccumulators */. 
5e7b0 20 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64   u8 useSortingId
5e7c0 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64  x;       /* In d
5e7d0 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65  irect mode, refe
5e7e0 72 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e  rence the sortin
5e7f0 67 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20  g index rather. 
5e800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e810 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e           ** than
5e820 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
5e830 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69  e */.  int sorti
5e840 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f  ngIdx;         /
5e850 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
5e860 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69  of the sorting i
5e870 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ndex */.  ExprLi
5e880 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20  st *pGroupBy;   
5e890 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62    /* The group b
5e8a0 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  y clause */.  in
5e8b0 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e  t nSortingColumn
5e8c0 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
5e8d0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
5e8e0 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  e sorting index 
5e8f0 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49  */.  struct AggI
5e900 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20  nfo_col {    /* 
5e910 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  For each column 
5e920 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74  used in source t
5e930 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62  ables */.    Tab
5e940 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
5e950 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20        /* Source 
5e960 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74  table */.    int
5e970 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20   iTable;        
5e980 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
5e990 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f  number of the so
5e9a0 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  urce table */.  
5e9b0 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
5e9c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
5e9d0 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68  lumn number with
5e9e0 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61  in the source ta
5e9f0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ble */.    int i
5ea00 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20  SorterColumn;   
5ea10 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75      /* Column nu
5ea20 6d 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74  mber in the sort
5ea30 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20  ing index */.   
5ea40 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20   int iMem;      
5ea50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
5ea60 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61  ory location tha
5ea70 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75  t acts as accumu
5ea80 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70  lator */.    Exp
5ea90 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
5eaa0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69        /* The ori
5eab0 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e  ginal expression
5eac0 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20   */.  } *aCol;. 
5ead0 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20   int nColumn;   
5eae0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5eaf0 65 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69  er of used entri
5eb00 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a  es in aCol[] */.
5eb10 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c    int nColumnAll
5eb20 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  oc;       /* Num
5eb30 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c  ber of slots all
5eb40 6f 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b  ocated for aCol[
5eb50 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75  ] */.  int nAccu
5eb60 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f  mulator;       /
5eb70 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
5eb80 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68  mns that show th
5eb90 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74  rough to the out
5eba0 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20  put..           
5ebb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
5ebc0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c  * Additional col
5ebd0 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e  umns are used on
5ebe0 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73  ly as parameters
5ebf0 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20   to.            
5ec00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
5ec10 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
5ec20 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  ions */.  struct
5ec30 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20   AggInfo_func { 
5ec40 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67    /* For each ag
5ec50 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
5ec60 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45   */.    Expr *pE
5ec70 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
5ec80 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65   /* Expression e
5ec90 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63  ncoding the func
5eca0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63  tion */.    Func
5ecb0 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20  Def *pFunc;     
5ecc0 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72       /* The aggr
5ecd0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69  egate function i
5ece0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  mplementation */
5ecf0 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20  .    int iMem;  
5ed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5ed10 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   Memory location
5ed20 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63   that acts as ac
5ed30 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20  cumulator */.   
5ed40 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20   int iDistinct; 
5ed50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68            /* Eph
5ed60 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65  emeral table use
5ed70 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53  d to enforce DIS
5ed80 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46  TINCT */.  } *aF
5ed90 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63  unc;.  int nFunc
5eda0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
5edb0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
5edc0 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a  ies in aFunc[] *
5edd0 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c  /.  int nFuncAll
5ede0 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  oc;         /* N
5edf0 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61  umber of slots a
5ee00 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75  llocated for aFu
5ee10 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  nc[] */.};../*.*
5ee20 2a 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61  * Each node of a
5ee30 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  n expression in 
5ee40 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69  the parse tree i
5ee50 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a  s an instance.**
5ee60 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
5ee70 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f  re..**.** Expr.o
5ee80 70 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e  p is the opcode.
5ee90 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   The integer par
5eea0 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20  ser token codes 
5eeb0 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73  are reused.** as
5eec0 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 46   opcodes here. F
5eed0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  or example, the 
5eee0 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20 54  parser defines T
5eef0 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69 6e  K_GE to be an in
5ef00 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72 65  teger.** code re
5ef10 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 22  presenting the "
5ef20 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 54 68  >=" operator. Th
5ef30 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20  is same integer 
5ef40 63 6f 64 65 20 69 73 20 72 65 75 73 65 64 0a 2a  code is reused.*
5ef50 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 74  * to represent t
5ef60 68 65 20 67 72 65 61 74 65 72 2d 74 68 61 6e 2d  he greater-than-
5ef70 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70 65 72  or-equal-to oper
5ef80 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78 70 72  ator in the expr
5ef90 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a  ession.** tree..
5efa0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
5efb0 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51  ression is an SQ
5efc0 4c 20 6c 69 74 65 72 61 6c 20 28 54 4b 5f 49 4e  L literal (TK_IN
5efd0 54 45 47 45 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c  TEGER, TK_FLOAT,
5efe0 20 54 4b 5f 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72   TK_BLOB, .** or
5eff0 20 54 4b 5f 53 54 52 49 4e 47 29 2c 20 74 68 65   TK_STRING), the
5f000 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e  n Expr.token con
5f010 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f  tains the text o
5f020 66 20 74 68 65 20 53 51 4c 20 6c 69 74 65 72 61  f the SQL litera
5f030 6c 2e 20 49 66 0a 2a 2a 20 74 68 65 20 65 78 70  l. If.** the exp
5f040 72 65 73 73 69 6f 6e 20 69 73 20 61 20 76 61 72  ression is a var
5f050 69 61 62 6c 65 20 28 54 4b 5f 56 41 52 49 41 42  iable (TK_VARIAB
5f060 4c 45 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74  LE), then Expr.t
5f070 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68  oken contains th
5f080 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e  e .** variable n
5f090 61 6d 65 2e 20 46 69 6e 61 6c 6c 79 2c 20 69 66  ame. Finally, if
5f0a0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
5f0b0 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  is an SQL functi
5f0c0 6f 6e 20 28 54 4b 5f 46 55 4e 43 54 49 4f 4e 29  on (TK_FUNCTION)
5f0d0 2c 0a 2a 2a 20 74 68 65 6e 20 45 78 70 72 2e 74  ,.** then Expr.t
5f0e0 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68  oken contains th
5f0f0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  e name of the fu
5f100 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78  nction..**.** Ex
5f110 70 72 2e 70 52 69 67 68 74 20 61 6e 64 20 45 78  pr.pRight and Ex
5f120 70 72 2e 70 4c 65 66 74 20 61 72 65 20 74 68 65  pr.pLeft are the
5f130 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20   left and right 
5f140 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 6f  subexpressions o
5f150 66 20 61 0a 2a 2a 20 62 69 6e 61 72 79 20 6f 70  f a.** binary op
5f160 65 72 61 74 6f 72 2e 20 45 69 74 68 65 72 20 6f  erator. Either o
5f170 72 20 62 6f 74 68 20 6d 61 79 20 62 65 20 4e 55  r both may be NU
5f180 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78  LL..**.** Expr.x
5f190 2e 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74  .pList is a list
5f1a0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 69 66   of arguments if
5f1b0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
5f1c0 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  is an SQL functi
5f1d0 6f 6e 2c 0a 2a 2a 20 61 20 43 41 53 45 20 65 78  on,.** a CASE ex
5f1e0 70 72 65 73 73 69 6f 6e 20 6f 72 20 61 6e 20 49  pression or an I
5f1f0 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20  N expression of 
5f200 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20  the form "<lhs> 
5f210 49 4e 20 28 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29  IN (<y>, <z>...)
5f220 22 2e 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 53 65  "..** Expr.x.pSe
5f230 6c 65 63 74 20 69 73 20 75 73 65 64 20 69 66 20  lect is used if 
5f240 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
5f250 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f  s a sub-select o
5f260 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  r an expression 
5f270 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22  of.** the form "
5f280 3c 6c 68 73 3e 20 49 4e 20 28 53 45 4c 45 43 54  <lhs> IN (SELECT
5f290 20 2e 2e 2e 29 22 2e 20 49 66 20 74 68 65 20 45   ...)". If the E
5f2a0 50 5f 78 49 73 53 65 6c 65 63 74 20 62 69 74 20  P_xIsSelect bit 
5f2b0 69 73 20 73 65 74 20 69 6e 20 74 68 65 0a 2a 2a  is set in the.**
5f2c0 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b   Expr.flags mask
5f2d0 2c 20 74 68 65 6e 20 45 78 70 72 2e 78 2e 70 53  , then Expr.x.pS
5f2e0 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64 2e 20  elect is valid. 
5f2f0 4f 74 68 65 72 77 69 73 65 2c 20 45 78 70 72 2e  Otherwise, Expr.
5f300 78 2e 70 4c 69 73 74 20 69 73 20 0a 2a 2a 20 76  x.pList is .** v
5f310 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  alid..**.** An e
5f320 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65  xpression of the
5f330 20 66 6f 72 6d 20 49 44 20 6f 72 20 49 44 2e 49   form ID or ID.I
5f340 44 20 72 65 66 65 72 73 20 74 6f 20 61 20 63 6f  D refers to a co
5f350 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e  lumn in a table.
5f360 0a 2a 2a 20 46 6f 72 20 73 75 63 68 20 65 78 70  .** For such exp
5f370 72 65 73 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f  ressions, Expr.o
5f380 70 20 69 73 20 73 65 74 20 74 6f 20 54 4b 5f 43  p is set to TK_C
5f390 4f 4c 55 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69  OLUMN and Expr.i
5f3a0 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20  Table is.** the 
5f3b0 69 6e 74 65 67 65 72 20 63 75 72 73 6f 72 20 6e  integer cursor n
5f3c0 75 6d 62 65 72 20 6f 66 20 61 20 56 44 42 45 20  umber of a VDBE 
5f3d0 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
5f3e0 74 6f 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e  to that table an
5f3f0 64 0a 2a 2a 20 45 78 70 72 2e 69 43 6f 6c 75 6d  d.** Expr.iColum
5f400 6e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  n is the column 
5f410 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73  number for the s
5f420 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 2e 20  pecific column. 
5f430 20 49 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 65   If the.** expre
5f440 73 73 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73  ssion is used as
5f450 20 61 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20   a result in an 
5f460 61 67 67 72 65 67 61 74 65 20 53 45 4c 45 43 54  aggregate SELECT
5f470 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61  , then the.** va
5f480 6c 75 65 20 69 73 20 61 6c 73 6f 20 73 74 6f 72  lue is also stor
5f490 65 64 20 69 6e 20 74 68 65 20 45 78 70 72 2e 69  ed in the Expr.i
5f4a0 41 67 67 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  Agg column in th
5f4b0 65 20 61 67 67 72 65 67 61 74 65 20 73 6f 20 74  e aggregate so t
5f4c0 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e 20 62 65  hat.** it can be
5f4d0 20 61 63 63 65 73 73 65 64 20 61 66 74 65 72 20   accessed after 
5f4e0 61 6c 6c 20 61 67 67 72 65 67 61 74 65 73 20 61  all aggregates a
5f4f0 72 65 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a  re computed..**.
5f500 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
5f510 73 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75  sion is an unbou
5f520 6e 64 20 76 61 72 69 61 62 6c 65 20 6d 61 72 6b  nd variable mark
5f530 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e 20 6d  er (a question m
5f540 61 72 6b 20 0a 2a 2a 20 63 68 61 72 61 63 74 65  ark .** characte
5f550 72 20 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69  r '?' in the ori
5f560 67 69 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20  ginal SQL) then 
5f570 74 68 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20  the Expr.iTable 
5f580 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78 20  holds the index 
5f590 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  .** number for t
5f5a0 68 61 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a  hat variable..**
5f5b0 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
5f5c0 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 71 75  ssion is a subqu
5f5d0 65 72 79 20 74 68 65 6e 20 45 78 70 72 2e 69 43  ery then Expr.iC
5f5e0 6f 6c 75 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69  olumn holds an i
5f5f0 6e 74 65 67 65 72 0a 2a 2a 20 72 65 67 69 73 74  nteger.** regist
5f600 65 72 20 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69  er number contai
5f610 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20  ning the result 
5f620 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  of the subquery.
5f630 20 20 49 66 20 74 68 65 0a 2a 2a 20 73 75 62 71    If the.** subq
5f640 75 65 72 79 20 67 69 76 65 73 20 61 20 63 6f 6e  uery gives a con
5f650 73 74 61 6e 74 20 72 65 73 75 6c 74 2c 20 74 68  stant result, th
5f660 65 6e 20 69 54 61 62 6c 65 20 69 73 20 2d 31 2e  en iTable is -1.
5f670 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72    If the subquer
5f680 79 0a 2a 2a 20 67 69 76 65 73 20 61 20 64 69 66  y.** gives a dif
5f690 66 65 72 65 6e 74 20 61 6e 73 77 65 72 20 61 74  ferent answer at
5f6a0 20 64 69 66 66 65 72 65 6e 74 20 74 69 6d 65 73   different times
5f6b0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e   during statemen
5f6c0 74 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20  t processing.** 
5f6d0 74 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20 74  then iTable is t
5f6e0 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20  he address of a 
5f6f0 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
5f700 63 6f 6d 70 75 74 65 73 20 74 68 65 20 73 75 62  computes the sub
5f710 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  query..**.** If 
5f720 74 68 65 20 45 78 70 72 20 69 73 20 6f 66 20 74  the Expr is of t
5f730 79 70 65 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61  ype OP_Column, a
5f740 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 74 20  nd the table it 
5f750 69 73 20 73 65 6c 65 63 74 69 6e 67 20 66 72 6f  is selecting fro
5f760 6d 0a 2a 2a 20 69 73 20 61 20 64 69 73 6b 20 74  m.** is a disk t
5f770 61 62 6c 65 20 6f 72 20 74 68 65 20 22 6f 6c 64  able or the "old
5f780 2e 2a 22 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  .*" pseudo-table
5f790 2c 20 74 68 65 6e 20 70 54 61 62 20 70 6f 69 6e  , then pTab poin
5f7a0 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 72  ts to the.** cor
5f7b0 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65  responding table
5f7c0 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a   definition..**.
5f7d0 2a 2a 20 41 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f  ** ALLOCATION NO
5f7e0 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20  TES:.**.** Expr 
5f7f0 6f 62 6a 65 63 74 73 20 63 61 6e 20 75 73 65 20  objects can use 
5f800 61 20 6c 6f 74 20 6f 66 20 6d 65 6d 6f 72 79 20  a lot of memory 
5f810 73 70 61 63 65 20 69 6e 20 64 61 74 61 62 61 73  space in databas
5f820 65 20 73 63 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a  e schema.  To.**
5f830 20 68 65 6c 70 20 72 65 64 75 63 65 20 6d 65 6d   help reduce mem
5f840 6f 72 79 20 72 65 71 75 69 72 65 6d 65 6e 74 73  ory requirements
5f850 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 6e 20 45  , sometimes an E
5f860 78 70 72 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20  xpr object will 
5f870 62 65 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e  be.** truncated.
5f880 20 20 41 6e 64 20 74 6f 20 72 65 64 75 63 65 20    And to reduce 
5f890 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65  the number of me
5f8a0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
5f8b0 2c 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 74  , sometimes.** t
5f8c0 77 6f 20 6f 72 20 6d 6f 72 65 20 45 78 70 72 20  wo or more Expr 
5f8d0 6f 62 6a 65 63 74 73 20 77 69 6c 6c 20 62 65 20  objects will be 
5f8e0 73 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67  stored in a sing
5f8f0 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  le memory alloca
5f900 74 69 6f 6e 2c 0a 2a 2a 20 74 6f 67 65 74 68 65  tion,.** togethe
5f910 72 20 77 69 74 68 20 45 78 70 72 2e 74 6f 6b 65  r with Expr.toke
5f920 6e 20 61 6e 64 2f 6f 72 20 45 78 70 72 2e 73 70  n and/or Expr.sp
5f930 61 6e 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a  an strings..**.*
5f940 2a 20 49 66 20 74 68 65 20 45 50 5f 52 65 64 75  * If the EP_Redu
5f950 63 65 64 2c 20 45 50 5f 53 70 61 6e 54 6f 6b 65  ced, EP_SpanToke
5f960 6e 2c 20 61 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f  n, and EP_TokenO
5f970 6e 6c 79 20 66 6c 61 67 73 20 61 72 65 20 73 65  nly flags are se
5f980 74 20 77 68 65 6e 0a 2a 2a 20 61 6e 20 45 78 70  t when.** an Exp
5f990 72 20 6f 62 6a 65 63 74 20 69 73 20 74 72 75 6e  r object is trun
5f9a0 63 61 74 65 64 2e 20 20 57 68 65 6e 20 45 50 5f  cated.  When EP_
5f9b0 52 65 64 75 63 65 64 20 69 73 20 73 65 74 2c 20  Reduced is set, 
5f9c0 74 68 65 6e 20 61 6c 6c 0a 2a 2a 20 74 68 65 20  then all.** the 
5f9d0 63 68 69 6c 64 20 45 78 70 72 20 6f 62 6a 65 63  child Expr objec
5f9e0 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 70  ts in the Expr.p
5f9f0 4c 65 66 74 20 61 6e 64 20 45 78 70 72 2e 70 52  Left and Expr.pR
5fa00 69 67 68 74 20 73 75 62 74 72 65 65 73 0a 2a 2a  ight subtrees.**
5fa10 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77   are contained w
5fa20 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 6d  ithin the same m
5fa30 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
5fa40 2e 20 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72  .  Note, however
5fa50 2c 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 73 75  , that.** the su
5fa60 62 74 72 65 65 73 20 69 6e 20 45 78 70 72 2e 78  btrees in Expr.x
5fa70 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78  .pList or Expr.x
5fa80 2e 70 53 65 6c 65 63 74 20 61 72 65 20 61 6c 77  .pSelect are alw
5fa90 61 79 73 20 73 65 70 61 72 61 74 65 6c 79 0a 2a  ays separately.*
5faa0 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 72 65 67  * allocated, reg
5fab0 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
5fac0 65 72 20 6f 72 20 6e 6f 74 20 45 50 5f 52 65 64  er or not EP_Red
5fad0 75 63 65 64 20 69 73 20 73 65 74 2e 0a 2a 2f 0a  uced is set..*/.
5fae0 73 74 72 75 63 74 20 45 78 70 72 20 7b 0a 20 20  struct Expr {.  
5faf0 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  u8 op;          
5fb00 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74         /* Operat
5fb10 69 6f 6e 20 70 65 72 66 6f 72 6d 65 64 20 62 79  ion performed by
5fb20 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 20 20   this node */.  
5fb30 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20  char affinity;  
5fb40 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66         /* The af
5fb50 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63 6f  finity of the co
5fb60 6c 75 6d 6e 20 6f 72 20 30 20 69 66 20 6e 6f 74  lumn or 0 if not
5fb70 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 56   a column */.  V
5fb80 56 41 5f 4f 4e 4c 59 28 75 38 20 76 76 61 46 6c  VA_ONLY(u8 vvaFl
5fb90 61 67 73 3b 29 20 2f 2a 20 46 6c 61 67 73 20 75  ags;) /* Flags u
5fba0 73 65 64 20 66 6f 72 20 56 56 26 41 20 6f 6e 6c  sed for VV&A onl
5fbb0 79 2e 20 20 45 56 56 41 5f 2a 20 62 65 6c 6f 77  y.  EVVA_* below
5fbc0 2e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73  . */.  u16 flags
5fbd0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
5fbe0 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73 2e 20   Various flags. 
5fbf0 20 45 50 5f 2a 20 53 65 65 20 62 65 6c 6f 77 20   EP_* See below 
5fc00 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 6f 6b 65 6e  */.  Token token
5fc10 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  ;           /* A
5fc20 6e 20 6f 70 65 72 61 6e 64 20 74 6f 6b 65 6e 20  n operand token 
5fc30 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  */..  /* If the 
5fc40 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61  EP_TokenOnly fla
5fc50 67 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20  g is set in the 
5fc60 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c  Expr.flags mask,
5fc70 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70   then no.  ** sp
5fc80 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ace is allocated
5fc90 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20   for the fields 
5fca0 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74  below this point
5fcb0 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a  . An attempt to.
5fcc0 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d    ** access them
5fcd0 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20   will result in 
5fce0 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61  a segfault or ma
5fcf0 6c 66 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a  lfunction. .  **
5fd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fd40 2a 2a 2a 2f 0a 0a 20 20 54 6f 6b 65 6e 20 73 70  ***/..  Token sp
5fd50 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  an;            /
5fd60 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20  * Complete text 
5fd70 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
5fd80 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68  n */..  /* If th
5fd90 65 20 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 20 66  e EP_SpanToken f
5fda0 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 74 68  lag is set in th
5fdb0 65 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73  e Expr.flags mas
5fdc0 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20  k, then no.  ** 
5fdd0 73 70 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74  space is allocat
5fde0 65 64 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64  ed for the field
5fdf0 73 20 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69  s below this poi
5fe00 6e 74 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74  nt. An attempt t
5fe10 6f 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68  o.  ** access th
5fe20 65 6d 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69  em will result i
5fe30 6e 20 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20  n a segfault or 
5fe40 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 20 0a 20 20  malfunction. .  
5fe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe90 2a 2a 2a 2a 2a 2f 0a 0a 20 20 45 78 70 72 20 2a  *****/..  Expr *
5fea0 70 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20 20  pLeft;          
5feb0 20 2f 2a 20 4c 65 66 74 20 73 75 62 6e 6f 64 65   /* Left subnode
5fec0 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67   */.  Expr *pRig
5fed0 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ht;          /* 
5fee0 52 69 67 68 74 20 73 75 62 6e 6f 64 65 20 2a 2f  Right subnode */
5fef0 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 45  .  union {.    E
5ff00 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20  xprList *pList; 
5ff10 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20      /* Function 
5ff20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 69 6e 20  arguments or in 
5ff30 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 65 78 70  "<expr> IN (<exp
5ff40 72 2d 6c 69 73 74 29 22 20 2a 2f 0a 20 20 20 20  r-list)" */.    
5ff50 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b  Select *pSelect;
5ff60 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72       /* Used for
5ff70 20 73 75 62 2d 73 65 6c 65 63 74 73 20 61 6e 64   sub-selects and
5ff80 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65   "<expr> IN (<se
5ff90 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 7d 20 78  lect>)" */.  } x
5ffa0 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  ;.  CollSeq *pCo
5ffb0 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ll;        /* Th
5ffc0 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65  e collation type
5ffd0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   of the column o
5ffe0 72 20 30 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  r 0 */..  /* If 
5fff0 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 66  the EP_Reduced f
60000 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 74 68  lag is set in th
60010 65 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73  e Expr.flags mas
60020 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20  k, then no.  ** 
60030 73 70 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74  space is allocat
60040 65 64 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64  ed for the field
60050 73 20 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69  s below this poi
60060 6e 74 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74  nt. An attempt t
60070 6f 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68  o.  ** access th
60080 65 6d 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69  em will result i
60090 6e 20 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20  n a segfault or 
600a0 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a  malfunction..  *
600b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
600c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
600d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
600e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
600f0 2a 2a 2a 2a 2f 0a 0a 20 20 69 6e 74 20 69 54 61  ****/..  int iTa
60100 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20  ble, iColumn;   
60110 2f 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43  /* When op==TK_C
60120 4f 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73  OLUMN, then this
60130 20 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73   expr node means
60140 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20   the.           
60150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
60160 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c   iColumn-th fiel
60170 64 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d  d of the iTable-
60180 74 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41  th table. */.  A
60190 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
601a0 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79  ;     /* Used by
601b0 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61   TK_AGG_COLUMN a
601c0 6e 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  nd TK_AGG_FUNCTI
601d0 4f 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67  ON */.  int iAgg
601e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
601f0 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e  * Which entry in
60200 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b   pAggInfo->aCol[
60210 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a  ] or ->aFunc[] *
60220 2f 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f  /.  int iRightJo
60230 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66  inTable;   /* If
60240 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68   EP_FromJoin, th
60250 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66  e right table of
60260 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 54   the join */.  T
60270 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
60280 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66        /* Table f
60290 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70  or TK_COLUMN exp
602a0 72 65 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66  ressions. */.#if
602b0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52   SQLITE_MAX_EXPR
602c0 5f 44 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e  _DEPTH>0.  int n
602d0 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  Height;         
602e0 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74    /* Height of t
602f0 68 65 20 74 72 65 65 20 68 65 61 64 65 64 20 62  he tree headed b
60300 79 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23  y this node */.#
60310 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
60320 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72  The following ar
60330 65 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f  e the meanings o
60340 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 45 78  f bits in the Ex
60350 70 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a  pr.flags field..
60360 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 72  */.#define EP_Fr
60370 6f 6d 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 20  omJoin   0x0001 
60380 20 2f 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 69   /* Originated i
60390 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  n ON or USING cl
603a0 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a  ause of a join *
603b0 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 67 67  /.#define EP_Agg
603c0 20 20 20 20 20 20 20 20 30 78 30 30 30 32 20 20          0x0002  
603d0 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20  /* Contains one 
603e0 6f 72 20 6d 6f 72 65 20 61 67 67 72 65 67 61 74  or more aggregat
603f0 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23  e functions */.#
60400 64 65 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76  define EP_Resolv
60410 65 64 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20  ed   0x0004  /* 
60420 49 44 73 20 68 61 76 65 20 62 65 65 6e 20 72 65  IDs have been re
60430 73 6f 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e  solved to COLUMN
60440 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  s */.#define EP_
60450 45 72 72 6f 72 20 20 20 20 20 20 30 78 30 30 30  Error      0x000
60460 38 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e  8  /* Expression
60470 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72   contains one or
60480 20 6d 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a   more errors */.
60490 23 64 65 66 69 6e 65 20 45 50 5f 44 69 73 74 69  #define EP_Disti
604a0 6e 63 74 20 20 20 30 78 30 30 31 30 20 20 2f 2a  nct   0x0010  /*
604b0 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   Aggregate funct
604c0 69 6f 6e 20 77 69 74 68 20 44 49 53 54 49 4e 43  ion with DISTINC
604d0 54 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65  T keyword */.#de
604e0 66 69 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63  fine EP_VarSelec
604f0 74 20 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53  t  0x0020  /* pS
60500 65 6c 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61  elect is correla
60510 74 65 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e  ted, not constan
60520 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  t */.#define EP_
60530 44 62 6c 51 75 6f 74 65 64 20 20 30 78 30 30 34  DblQuoted  0x004
60540 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e 7a 20 77 61  0  /* token.z wa
60550 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69 6e 20  s originally in 
60560 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65 66 69 6e 65  "..." */.#define
60570 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 20 20 30   EP_InfixFunc  0
60580 78 30 30 38 30 20 20 2f 2a 20 54 72 75 65 20 66  x0080  /* True f
60590 6f 72 20 61 6e 20 69 6e 66 69 78 20 66 75 6e 63  or an infix func
605a0 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f 42  tion: LIKE, GLOB
605b0 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66 69 6e 65  , etc */.#define
605c0 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 30   EP_ExpCollate 0
605d0 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c 6c 61 74  x0100  /* Collat
605e0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70 65  ing sequence spe
605f0 63 69 66 69 65 64 20 65 78 70 6c 69 63 69 74 6c  cified explicitl
60600 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  y */.#define EP_
60610 41 6e 79 41 66 66 20 20 20 20 20 30 78 30 32 30  AnyAff     0x020
60620 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b 65 20 61  0  /* Can take a
60630 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 6f   cached column o
60640 66 20 61 6e 79 20 61 66 66 69 6e 69 74 79 20 2a  f any affinity *
60650 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 69 78  /.#define EP_Fix
60660 65 64 44 65 73 74 20 20 30 78 30 34 30 30 20 20  edDest  0x0400  
60670 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65 64 65 64  /* Result needed
60680 20 69 6e 20 61 20 73 70 65 63 69 66 69 63 20 72   in a specific r
60690 65 67 69 73 74 65 72 20 2a 2f 0a 23 64 65 66 69  egister */.#defi
606a0 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 20  ne EP_IntValue  
606b0 20 30 78 30 38 30 30 20 20 2f 2a 20 49 6e 74 65   0x0800  /* Inte
606c0 67 65 72 20 76 61 6c 75 65 20 63 6f 6e 74 61 69  ger value contai
606d0 6e 65 64 20 69 6e 20 69 54 61 62 6c 65 20 2a 2f  ned in iTable */
606e0 0a 23 64 65 66 69 6e 65 20 45 50 5f 78 49 73 53  .#define EP_xIsS
606f0 65 6c 65 63 74 20 20 30 78 31 30 30 30 20 20 2f  elect  0x1000  /
60700 2a 20 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76  * x.pSelect is v
60710 61 6c 69 64 20 28 6f 74 68 65 72 77 69 73 65 20  alid (otherwise 
60720 78 2e 70 4c 69 73 74 20 69 73 29 20 2a 2f 0a 0a  x.pList is) */..
60730 23 64 65 66 69 6e 65 20 45 50 5f 52 65 64 75 63  #define EP_Reduc
60740 65 64 20 20 20 20 30 78 32 30 30 30 20 20 2f 2a  ed    0x2000  /*
60750 20 45 78 70 72 20 73 74 72 75 63 74 20 69 73 20   Expr struct is 
60760 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45  EXPR_REDUCEDSIZE
60770 20 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23   bytes only */.#
60780 64 65 66 69 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f  define EP_TokenO
60790 6e 6c 79 20 20 30 78 34 30 30 30 20 20 2f 2a 20  nly  0x4000  /* 
607a0 45 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45  Expr struct is E
607b0 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a  XPR_TOKENONLYSIZ
607c0 45 20 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a  E bytes only */.
607d0 23 64 65 66 69 6e 65 20 45 50 5f 53 70 61 6e 54  #define EP_SpanT
607e0 6f 6b 65 6e 20 20 30 78 38 30 30 30 20 20 2f 2a  oken  0x8000  /*
607f0 20 45 78 70 72 20 73 69 7a 65 20 69 73 20 45 58   Expr size is EX
60800 50 52 5f 53 50 41 4e 54 4f 4b 45 4e 53 49 5a 45  PR_SPANTOKENSIZE
60810 20 62 79 74 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a   bytes */../*.**
60820 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
60830 72 65 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20  re the meanings 
60840 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 45  of bits in the E
60850 78 70 72 2e 76 76 61 46 6c 61 67 73 20 66 69 65  xpr.vvaFlags fie
60860 6c 64 2e 0a 2a 2a 20 54 68 69 73 20 69 6e 66 6f  ld..** This info
60870 72 6d 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  rmation is only 
60880 75 73 65 64 20 77 68 65 6e 20 53 51 4c 69 74 65  used when SQLite
60890 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   is compiled wit
608a0 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 45 42 55  h.** SQLITE_DEBU
608b0 47 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69  G defined..*/.#i
608c0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 64 65  fndef NDEBUG.#de
608d0 66 69 6e 65 20 45 56 56 41 5f 52 65 61 64 4f 6e  fine EVVA_ReadOn
608e0 6c 79 54 6f 6b 65 6e 20 20 30 78 30 31 20 20 2f  lyToken  0x01  /
608f0 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 69  * Expr.token.z i
60900 73 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 23  s read-only */.#
60910 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
60920 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65  se macros can be
60930 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73   used to test, s
60940 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74  et, or clear bit
60950 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70  s in the .** Exp
60960 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a  r.flags field..*
60970 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 61  /.#define ExprHa
60980 73 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  sProperty(E,P)  
60990 20 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26     (((E)->flags&
609a0 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69  (P))==(P)).#defi
609b0 6e 65 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  ne ExprHasAnyPro
609c0 70 65 72 74 79 28 45 2c 50 29 20 20 28 28 28 45  perty(E,P)  (((E
609d0 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30  )->flags&(P))!=0
609e0 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53 65  ).#define ExprSe
609f0 74 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  tProperty(E,P)  
60a00 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28     (E)->flags|=(
60a10 50 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43  P).#define ExprC
60a20 6c 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50  learProperty(E,P
60a30 29 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d  )   (E)->flags&=
60a40 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ~(P)../*.** Macr
60a50 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  os to determine 
60a60 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
60a70 74 65 73 20 72 65 71 75 69 72 65 64 20 62 79 20  tes required by 
60a80 61 20 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a  a normal Expr .*
60a90 2a 20 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70  * struct, an Exp
60aa0 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68  r struct with th
60ab0 65 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61  e EP_Reduced fla
60ac0 67 20 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c  g set in Expr.fl
60ad0 61 67 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45  ags .** and an E
60ae0 78 70 72 20 73 74 72 75 63 74 20 77 69 74 68 20  xpr struct with 
60af0 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  the EP_TokenOnly
60b00 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64   flag set..*/.#d
60b10 65 66 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53  efine EXPR_FULLS
60b20 49 5a 45 20 20 20 20 20 20 20 20 20 20 20 73 69  IZE           si
60b30 7a 65 6f 66 28 45 78 70 72 29 20 20 20 20 20 20  zeof(Expr)      
60b40 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a       /* Full siz
60b50 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50  e */.#define EXP
60b60 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20  R_REDUCEDSIZE   
60b70 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78       offsetof(Ex
60b80 70 72 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43  pr,iTable)  /* C
60b90 6f 6d 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a  ommon features *
60ba0 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 53  /.#define EXPR_S
60bb0 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 20 20 20 20  PANTOKENSIZE    
60bc0 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c    offsetof(Expr,
60bd0 70 4c 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65  pLeft)   /* Fewe
60be0 72 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 23 64  r features */.#d
60bf0 65 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b 45 4e  efine EXPR_TOKEN
60c00 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20 20 6f 66  ONLYSIZE      of
60c10 66 73 65 74 6f 66 28 45 78 70 72 2c 73 70 61 6e  fsetof(Expr,span
60c20 29 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 73 74  )    /* Smallest
60c30 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 0a 2f 2a   possible */../*
60c40 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64  .** Flags passed
60c50 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45   to the sqlite3E
60c60 78 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f  xprDup() functio
60c70 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65  n. See the heade
60c80 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62  r comment .** ab
60c90 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 44  ove sqlite3ExprD
60ca0 75 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73  up() for details
60cb0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50  ..*/.#define EXP
60cc0 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 20  RDUP_REDUCE     
60cd0 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55      0x0001  /* U
60ce0 73 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65  sed reduced-size
60cf0 20 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 23   Expr nodes */.#
60d00 64 65 66 69 6e 65 20 45 58 50 52 44 55 50 5f 53  define EXPRDUP_S
60d10 50 41 4e 20 20 20 20 20 20 20 20 20 20 20 30 78  PAN           0x
60d20 30 30 30 32 20 20 2f 2a 20 4d 61 6b 65 20 61 20  0002  /* Make a 
60d30 63 6f 70 79 20 6f 66 20 45 78 70 72 2e 73 70 61  copy of Expr.spa
60d40 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69  n */../*.** A li
60d50 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
60d60 73 2e 20 20 45 61 63 68 20 65 78 70 72 65 73 73  s.  Each express
60d70 69 6f 6e 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c  ion may optional
60d80 6c 79 20 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d  ly have a.** nam
60d90 65 2e 20 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65  e.  An expr/name
60da0 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e   combination can
60db0 20 62 65 20 75 73 65 64 20 69 6e 20 73 65 76 65   be used in seve
60dc0 72 61 6c 20 77 61 79 73 2c 20 73 75 63 68 0a 2a  ral ways, such.*
60dd0 2a 20 61 73 20 74 68 65 20 6c 69 73 74 20 6f 66  * as the list of
60de0 20 22 65 78 70 72 20 41 53 20 49 44 22 20 66 69   "expr AS ID" fi
60df0 65 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  elds following a
60e00 20 22 53 45 4c 45 43 54 22 20 6f 72 20 69 6e 20   "SELECT" or in 
60e10 74 68 65 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22  the.** list of "
60e20 49 44 20 3d 20 65 78 70 72 22 20 69 74 65 6d 73  ID = expr" items
60e30 20 69 6e 20 61 6e 20 55 50 44 41 54 45 2e 20 20   in an UPDATE.  
60e40 41 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73  A list of expres
60e50 73 69 6f 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73  sions can.** als
60e60 6f 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65  o be used as the
60e70 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66   argument to a f
60e80 75 6e 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63  unction, in whic
60e90 68 20 63 61 73 65 20 74 68 65 20 61 2e 7a 4e 61  h case the a.zNa
60ea0 6d 65 0a 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e  me.** field is n
60eb0 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75  ot used..*/.stru
60ec0 63 74 20 45 78 70 72 4c 69 73 74 20 7b 0a 20 20  ct ExprList {.  
60ed0 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20  int nExpr;      
60ee0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
60ef0 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20   of expressions 
60f00 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  on the list */. 
60f10 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   int nAlloc;    
60f20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
60f30 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c  r of entries all
60f40 6f 63 61 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a  ocated below */.
60f50 20 20 69 6e 74 20 69 45 43 75 72 73 6f 72 3b 20    int iECursor; 
60f60 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
60f70 20 43 75 72 73 6f 72 20 61 73 73 6f 63 69 61 74   Cursor associat
60f80 65 64 20 77 69 74 68 20 74 68 69 73 20 45 78 70  ed with this Exp
60f90 72 4c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 63  rList */.  struc
60fa0 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
60fb0 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  {.    Expr *pExp
60fc0 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
60fd0 54 68 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72  The list of expr
60fe0 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63  essions */.    c
60ff0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
61000 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61        /* Token a
61010 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
61020 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  his expression *
61030 2f 0a 20 20 20 20 75 38 20 73 6f 72 74 4f 72 64  /.    u8 sortOrd
61040 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er;          /* 
61050 31 20 66 6f 72 20 44 45 53 43 20 6f 72 20 30 20  1 for DESC or 0 
61060 66 6f 72 20 41 53 43 20 2a 2f 0a 20 20 20 20 75  for ASC */.    u
61070 38 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20  8 done;         
61080 20 20 20 20 20 20 2f 2a 20 41 20 66 6c 61 67 20        /* A flag 
61090 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e  to indicate when
610a0 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 66   processing is f
610b0 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 20 20 75  inished */.    u
610c0 31 36 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  16 iCol;        
610d0 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44        /* For ORD
610e0 45 52 20 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75  ER BY, column nu
610f0 6d 62 65 72 20 69 6e 20 72 65 73 75 6c 74 20 73  mber in result s
61100 65 74 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 41  et */.    u16 iA
61110 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 20  lias;           
61120 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 50   /* Index into P
61130 61 72 73 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f  arse.aAlias[] fo
61140 72 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a  r zName */.  } *
61150 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
61160 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79      /* One entry
61170 20 66 6f 72 20 65 61 63 68 20 65 78 70 72 65 73   for each expres
61180 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  sion */.};../*.*
61190 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
611a0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
611b0 63 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c  can hold a simpl
611c0 65 20 6c 69 73 74 20 6f 66 20 69 64 65 6e 74 69  e list of identi
611d0 66 69 65 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61  fiers,.** such a
611e0 73 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62 2c  s the list "a,b,
611f0 63 22 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  c" in the follow
61200 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a  ing statements:.
61210 2a 2a 0a 2a 2a 20 20 20 20 20 20 49 4e 53 45 52  **.**      INSER
61220 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20  T INTO t(a,b,c) 
61230 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20  VALUES ...;.**  
61240 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58      CREATE INDEX
61250 20 69 64 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29   idx ON t(a,b,c)
61260 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45  ;.**      CREATE
61270 20 54 52 49 47 47 45 52 20 74 72 69 67 20 42 45   TRIGGER trig BE
61280 46 4f 52 45 20 55 50 44 41 54 45 20 4f 4e 20 74  FORE UPDATE ON t
61290 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a  (a,b,c) ...;.**.
612a0 2a 2a 20 54 68 65 20 49 64 4c 69 73 74 2e 61 2e  ** The IdList.a.
612b0 69 64 78 20 66 69 65 6c 64 20 69 73 20 75 73 65  idx field is use
612c0 64 20 77 68 65 6e 20 74 68 65 20 49 64 4c 69 73  d when the IdLis
612d0 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65  t represents the
612e0 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75   list of.** colu
612f0 6d 6e 20 6e 61 6d 65 73 20 61 66 74 65 72 20 61  mn names after a
61300 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61   table name in a
61310 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65  n INSERT stateme
61320 6e 74 2e 20 20 49 6e 20 74 68 65 20 73 74 61 74  nt.  In the stat
61330 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ement.**.**     
61340 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c  INSERT INTO t(a,
61350 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49  b,c) ....**.** I
61360 66 20 22 61 22 20 69 73 20 74 68 65 20 6b 2d 74  f "a" is the k-t
61370 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c  h column of tabl
61380 65 20 22 74 22 2c 20 74 68 65 6e 20 49 64 4c 69  e "t", then IdLi
61390 73 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a  st.a[0].idx==k..
613a0 2a 2f 0a 73 74 72 75 63 74 20 49 64 4c 69 73 74  */.struct IdList
613b0 20 7b 0a 20 20 73 74 72 75 63 74 20 49 64 4c 69   {.  struct IdLi
613c0 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63 68  st_item {.    ch
613d0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
613e0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69  /* Name of the i
613f0 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20 20 20  dentifier */.   
61400 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20   int idx;       
61410 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73     /* Index in s
61420 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d  ome Table.aCol[]
61430 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d   of a column nam
61440 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20  ed zName */.  } 
61450 2a 61 3b 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20  *a;.  int nId;  
61460 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
61470 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72 73 20   of identifiers 
61480 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  on the list */. 
61490 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   int nAlloc;    
614a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
614b0 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64  ntries allocated
614c0 20 66 6f 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a   for a[] below *
614d0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
614e0 62 69 74 6d 61 73 6b 20 64 61 74 61 74 79 70 65  bitmask datatype
614f0 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69   defined below i
61500 73 20 75 73 65 64 20 66 6f 72 20 76 61 72 69 6f  s used for vario
61510 75 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  us optimizations
61520 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67  ..**.** Changing
61530 20 74 68 69 73 20 66 72 6f 6d 20 61 20 36 34 2d   this from a 64-
61540 62 69 74 20 74 6f 20 61 20 33 32 2d 62 69 74 20  bit to a 32-bit 
61550 74 79 70 65 20 6c 69 6d 69 74 73 20 74 68 65 20  type limits the 
61560 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62  number of.** tab
61570 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f  les in a join to
61580 20 33 32 20 69 6e 73 74 65 61 64 20 6f 66 20 36   32 instead of 6
61590 34 2e 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20  4.  But it also 
615a0 72 65 64 75 63 65 73 20 74 68 65 20 73 69 7a 65  reduces the size
615b0 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 69 62 72 61  .** of the libra
615c0 72 79 20 62 79 20 37 33 38 20 62 79 74 65 73 20  ry by 738 bytes 
615d0 6f 6e 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65  on ix86..*/.type
615e0 64 65 66 20 75 36 34 20 42 69 74 6d 61 73 6b 3b  def u64 Bitmask;
615f0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62  ../*.** The numb
61600 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20 61 20  er of bits in a 
61610 42 69 74 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20  Bitmask.  "BMS" 
61620 6d 65 61 6e 73 20 22 42 69 74 4d 61 73 6b 20 53  means "BitMask S
61630 69 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ize"..*/.#define
61640 20 42 4d 53 20 20 28 28 69 6e 74 29 28 73 69 7a   BMS  ((int)(siz
61650 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 29 29  eof(Bitmask)*8))
61660 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
61670 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
61680 64 65 73 63 72 69 62 65 73 20 74 68 65 20 46 52  describes the FR
61690 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53  OM clause of a S
616a0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
616b0 0a 2a 2a 20 45 61 63 68 20 74 61 62 6c 65 20 6f  .** Each table o
616c0 72 20 73 75 62 71 75 65 72 79 20 69 6e 20 74 68  r subquery in th
616d0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
616e0 20 61 20 73 65 70 61 72 61 74 65 20 65 6c 65 6d   a separate elem
616f0 65 6e 74 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72  ent of.** the Sr
61700 63 4c 69 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e  cList.a[] array.
61710 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20  .**.** With the 
61720 61 64 64 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74  addition of mult
61730 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 73 75  iple database su
61740 70 70 6f 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f  pport, the follo
61750 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a  wing structure.*
61760 2a 20 63 61 6e 20 61 6c 73 6f 20 62 65 20 75 73  * can also be us
61770 65 64 20 74 6f 20 64 65 73 63 72 69 62 65 20 61  ed to describe a
61780 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c   particular tabl
61790 65 20 73 75 63 68 20 61 73 20 74 68 65 20 74 61  e such as the ta
617a0 62 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d  ble that.** is m
617b0 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 49 4e  odified by an IN
617c0 53 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72  SERT, DELETE, or
617d0 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
617e0 74 2e 20 20 49 6e 20 73 74 61 6e 64 61 72 64 20  t.  In standard 
617f0 53 51 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74  SQL,.** such a t
61800 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 20 73  able must be a s
61810 69 6d 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20  imple name: ID. 
61820 20 42 75 74 20 69 6e 20 53 51 4c 69 74 65 2c 20   But in SQLite, 
61830 74 68 65 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a  the table can.**
61840 20 6e 6f 77 20 62 65 20 69 64 65 6e 74 69 66 69   now be identifi
61850 65 64 20 62 79 20 61 20 64 61 74 61 62 61 73 65  ed by a database
61860 20 6e 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68   name, a dot, th
61870 65 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  en the table nam
61880 65 3a 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20  e: ID.ID..**.** 
61890 54 68 65 20 6a 6f 69 6e 74 79 70 65 20 73 74 61  The jointype sta
618a0 72 74 73 20 6f 75 74 20 73 68 6f 77 69 6e 67 20  rts out showing 
618b0 74 68 65 20 6a 6f 69 6e 20 74 79 70 65 20 62 65  the join type be
618c0 74 77 65 65 6e 20 74 68 65 20 63 75 72 72 65 6e  tween the curren
618d0 74 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74  t table.** and t
618e0 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e  he next table on
618f0 20 74 68 65 20 6c 69 73 74 2e 20 20 54 68 65 20   the list.  The 
61900 70 61 72 73 65 72 20 62 75 69 6c 64 73 20 74 68  parser builds th
61910 65 20 6c 69 73 74 20 74 68 69 73 20 77 61 79 2e  e list this way.
61920 0a 2a 2a 20 42 75 74 20 73 71 6c 69 74 65 33 53  .** But sqlite3S
61930 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54  rcListShiftJoinT
61940 79 70 65 28 29 20 6c 61 74 65 72 20 73 68 69 66  ype() later shif
61950 74 73 20 74 68 65 20 6a 6f 69 6e 74 79 70 65 73  ts the jointypes
61960 20 73 6f 20 74 68 61 74 20 65 61 63 68 0a 2a 2a   so that each.**
61970 20 6a 6f 69 6e 74 79 70 65 20 65 78 70 72 65 73   jointype expres
61980 73 65 73 20 74 68 65 20 6a 6f 69 6e 20 62 65 74  ses the join bet
61990 77 65 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61  ween the table a
619a0 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  nd the previous 
619b0 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  table..*/.struct
619c0 20 53 72 63 4c 69 73 74 20 7b 0a 20 20 69 31 36   SrcList {.  i16
619d0 20 6e 53 72 63 3b 20 20 20 20 20 20 20 20 2f 2a   nSrc;        /*
619e0 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65   Number of table
619f0 73 20 6f 72 20 73 75 62 71 75 65 72 69 65 73 20  s or subqueries 
61a00 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
61a10 73 65 20 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c  se */.  i16 nAll
61a20 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  oc;      /* Numb
61a30 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c  er of entries al
61a40 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d 20 62  located in a[] b
61a50 65 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 75 63 74  elow */.  struct
61a60 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a   SrcList_item {.
61a70 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74 61 62      char *zDatab
61a80 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ase;  /* Name of
61a90 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e   database holdin
61aa0 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  g this table */.
61ab0 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b      char *zName;
61ac0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
61ad0 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
61ae0 20 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20    char *zAlias; 
61af0 20 20 20 20 2f 2a 20 54 68 65 20 22 42 22 20 70      /* The "B" p
61b00 61 72 74 20 6f 66 20 61 20 22 41 20 41 53 20 42  art of a "A AS B
61b10 22 20 70 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65  " phrase.  zName
61b20 20 69 73 20 74 68 65 20 22 41 22 20 2a 2f 0a 20   is the "A" */. 
61b30 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20     Table *pTab; 
61b40 20 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74       /* An SQL t
61b50 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 69  able correspondi
61b60 6e 67 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20  ng to zName */. 
61b70 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65     Select *pSele
61b80 63 74 3b 20 20 2f 2a 20 41 20 53 45 4c 45 43 54  ct;  /* A SELECT
61b90 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20   statement used 
61ba0 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61  in place of a ta
61bb0 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  ble name */.    
61bc0 75 38 20 69 73 50 6f 70 75 6c 61 74 65 64 3b 20  u8 isPopulated; 
61bd0 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 74    /* Temporary t
61be0 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20  able associated 
61bf0 77 69 74 68 20 53 45 4c 45 43 54 20 69 73 20 70  with SELECT is p
61c00 6f 70 75 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20  opulated */.    
61c10 75 38 20 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20  u8 jointype;    
61c20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69    /* Type of joi
61c30 6e 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 61  n between this a
61c40 62 6c 65 20 61 6e 64 20 74 68 65 20 70 72 65 76  ble and the prev
61c50 69 6f 75 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e  ious */.    u8 n
61c60 6f 74 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a  otIndexed;    /*
61c70 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 69   True if there i
61c80 73 20 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20  s a NOT INDEXED 
61c90 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e  clause */.    in
61ca0 74 20 69 43 75 72 73 6f 72 3b 20 20 20 20 20 20  t iCursor;      
61cb0 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73  /* The VDBE curs
61cc0 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64 20 74  or number used t
61cd0 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 74 61  o access this ta
61ce0 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  ble */.    Expr 
61cf0 2a 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20  *pOn;        /* 
61d00 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66  The ON clause of
61d10 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49   a join */.    I
61d20 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b 20 20  dList *pUsing;  
61d30 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c   /* The USING cl
61d40 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a  ause of a join *
61d50 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f  /.    Bitmask co
61d60 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e  lUsed;  /* Bit N
61d70 20 28 31 3c 3c 4e 29 20 73 65 74 20 69 66 20 63   (1<<N) set if c
61d80 6f 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20  olumn N of pTab 
61d90 69 73 20 75 73 65 64 20 2a 2f 0a 20 20 20 20 63  is used */.    c
61da0 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20  har *zIndex;    
61db0 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 20 66   /* Identifier f
61dc0 72 6f 6d 20 22 49 4e 44 45 58 45 44 20 42 59 20  rom "INDEXED BY 
61dd0 3c 7a 49 6e 64 65 78 3e 22 20 63 6c 61 75 73 65  <zIndex>" clause
61de0 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   */.    Index *p
61df0 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64  Index;    /* Ind
61e00 65 78 20 73 74 72 75 63 74 75 72 65 20 63 6f 72  ex structure cor
61e10 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49  responding to zI
61e20 6e 64 65 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a  ndex, if any */.
61e30 20 20 7d 20 61 5b 31 5d 3b 20 20 20 20 20 20 20    } a[1];       
61e40 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74        /* One ent
61e50 72 79 20 66 6f 72 20 65 61 63 68 20 69 64 65 6e  ry for each iden
61e60 74 69 66 69 65 72 20 6f 6e 20 74 68 65 20 6c 69  tifier on the li
61e70 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  st */.};../*.** 
61e80 50 65 72 6d 69 74 74 65 64 20 76 61 6c 75 65 73  Permitted values
61e90 20 6f 66 20 74 68 65 20 53 72 63 4c 69 73 74 2e   of the SrcList.
61ea0 61 2e 6a 6f 69 6e 74 79 70 65 20 66 69 65 6c 64  a.jointype field
61eb0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49  .*/.#define JT_I
61ec0 4e 4e 45 52 20 20 20 20 20 30 78 30 30 30 31 20  NNER     0x0001 
61ed0 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f     /* Any kind o
61ee0 66 20 69 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73  f inner or cross
61ef0 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65   join */.#define
61f00 20 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 30 78   JT_CROSS     0x
61f10 30 30 30 32 20 20 20 20 2f 2a 20 45 78 70 6c 69  0002    /* Expli
61f20 63 69 74 20 75 73 65 20 6f 66 20 74 68 65 20 43  cit use of the C
61f30 52 4f 53 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a  ROSS keyword */.
61f40 23 64 65 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52  #define JT_NATUR
61f50 41 4c 20 20 20 30 78 30 30 30 34 20 20 20 20 2f  AL   0x0004    /
61f60 2a 20 54 72 75 65 20 66 6f 72 20 61 20 22 6e 61  * True for a "na
61f70 74 75 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23  tural" join */.#
61f80 64 65 66 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20  define JT_LEFT  
61f90 20 20 20 20 30 78 30 30 30 38 20 20 20 20 2f 2a      0x0008    /*
61fa0 20 4c 65 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e   Left outer join
61fb0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52   */.#define JT_R
61fc0 49 47 48 54 20 20 20 20 20 30 78 30 30 31 30 20  IGHT     0x0010 
61fd0 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 75 74 65     /* Right oute
61fe0 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e  r join */.#defin
61ff0 65 20 4a 54 5f 4f 55 54 45 52 20 20 20 20 20 30  e JT_OUTER     0
62000 78 30 30 32 30 20 20 20 20 2f 2a 20 54 68 65 20  x0020    /* The 
62010 22 4f 55 54 45 52 22 20 6b 65 79 77 6f 72 64 20  "OUTER" keyword 
62020 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64  is present */.#d
62030 65 66 69 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20  efine JT_ERROR  
62040 20 20 20 30 78 30 30 34 30 20 20 20 20 2f 2a 20     0x0040    /* 
62050 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70  unknown or unsup
62060 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65  ported join type
62070 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68   */.../*.** A Wh
62080 65 72 65 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68  erePlan object h
62090 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  olds information
620a0 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
620b0 61 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61  a lookup.** stra
620c0 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tegy..**.** This
620d0 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 74 65 6e   object is inten
620e0 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65  ded to be opaque
620f0 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20   outside of the 
62100 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a  where.c module..
62110 2a 2a 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65  ** It is include
62120 64 20 68 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74  d here only so t
62130 68 61 74 20 74 68 61 74 20 63 6f 6d 70 69 6c 65  hat that compile
62140 72 20 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20  r will know how 
62150 62 69 67 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e  big it.** is.  N
62160 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 65 6c 64  one of the field
62170 73 20 69 6e 20 74 68 69 73 20 6f 62 6a 65 63 74  s in this object
62180 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
62190 6f 75 74 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68  outside of.** th
621a0 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65  e where.c module
621b0 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74  ..**.** Within t
621c0 68 65 20 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69  he union, pIdx i
621d0 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e  s only used when
621e0 20 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49   wsFlags&WHERE_I
621f0 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 2e 0a  NDEXED is true..
62200 2a 2a 20 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79  ** pTerm is only
62210 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61   used when wsFla
62220 67 73 26 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f  gs&WHERE_MULTI_O
62230 52 20 69 73 20 74 72 75 65 2e 20 20 41 6e 64 20  R is true.  And 
62240 70 56 74 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f  pVtabIdx.** is o
62250 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73  nly used when ws
62260 46 6c 61 67 73 26 57 48 45 52 45 5f 56 49 52 54  Flags&WHERE_VIRT
62270 55 41 4c 54 41 42 4c 45 20 69 73 20 74 72 75 65  UALTABLE is true
62280 2e 20 20 49 74 20 69 73 20 6e 65 76 65 72 20 74  .  It is never t
62290 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20  he.** case that 
622a0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66  more than one of
622b0 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e   these condition
622c0 73 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74  s is true..*/.st
622d0 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 7b  ruct WherePlan {
622e0 0a 20 20 75 33 32 20 77 73 46 6c 61 67 73 3b 20  .  u32 wsFlags; 
622f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62300 20 20 2f 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61    /* WHERE_* fla
62310 67 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65  gs that describe
62320 20 74 68 65 20 73 74 72 61 74 65 67 79 20 2a 2f   the strategy */
62330 0a 20 20 75 33 32 20 6e 45 71 3b 20 20 20 20 20  .  u32 nEq;     
62340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62350 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d    /* Number of =
62360 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f  = constraints */
62370 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49  .  union {.    I
62380 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
62390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
623a0 20 49 6e 64 65 78 20 77 68 65 6e 20 57 48 45 52   Index when WHER
623b0 45 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75  E_INDEXED is tru
623c0 65 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20  e */.    struct 
623d0 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
623e0 3b 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45  ;       /* WHERE
623f0 20 63 6c 61 75 73 65 20 74 65 72 6d 20 66 6f 72   clause term for
62400 20 4f 52 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20   OR-search */.  
62410 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
62420 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 3b 20  info *pVtabIdx; 
62430 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
62440 65 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 2a  e index to use *
62450 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a  /.  } u;.};../*.
62460 2a 2a 20 46 6f 72 20 65 61 63 68 20 6e 65 73 74  ** For each nest
62470 65 64 20 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45  ed loop in a WHE
62480 52 45 20 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d  RE clause implem
62490 65 6e 74 61 74 69 6f 6e 2c 20 74 68 65 20 57 68  entation, the Wh
624a0 65 72 65 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63  ereInfo.** struc
624b0 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20  ture contains a 
624c0 73 69 6e 67 6c 65 20 69 6e 73 74 61 6e 63 65 20  single instance 
624d0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
624e0 65 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75  e.  This structu
624f0 72 65 0a 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65  re.** is intende
62500 64 20 74 6f 20 62 65 20 70 72 69 76 61 74 65 20  d to be private 
62510 74 68 65 20 74 68 65 20 77 68 65 72 65 2e 63 20  the the where.c 
62520 6d 6f 64 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c  module and shoul
62530 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65  d not be.** acce
62540 73 73 20 6f 72 20 6d 6f 64 69 66 69 65 64 20 62  ss or modified b
62550 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e  y other modules.
62560 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 49 64 78 49  .**.** The pIdxI
62570 6e 66 6f 20 66 69 65 6c 64 20 69 73 20 75 73 65  nfo field is use
62580 64 20 74 6f 20 68 65 6c 70 20 70 69 63 6b 20 74  d to help pick t
62590 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 6f 6e  he best index on
625a0 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61   a.** virtual ta
625b0 62 6c 65 2e 20 20 54 68 65 20 70 49 64 78 49 6e  ble.  The pIdxIn
625c0 66 6f 20 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61  fo pointer conta
625d0 69 6e 73 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20  ins indexing.** 
625e0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
625f0 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20 69  the i-th table i
62600 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
62610 65 20 62 65 66 6f 72 65 20 72 65 6f 72 64 65 72  e before reorder
62620 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20  ing..** All the 
62630 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72  pIdxInfo pointer
62640 73 20 61 72 65 20 66 72 65 65 64 20 62 79 20 77  s are freed by w
62650 68 65 72 65 49 6e 66 6f 46 72 65 65 28 29 20 69  hereInfoFree() i
62660 6e 20 77 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c  n where.c..** Al
62670 6c 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74  l other informat
62680 69 6f 6e 20 69 6e 20 74 68 65 20 69 2d 74 68 20  ion in the i-th 
62690 57 68 65 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63  WhereLevel objec
626a0 74 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74  t for the i-th t
626b0 61 62 6c 65 0a 2a 2a 20 61 66 74 65 72 20 46 52  able.** after FR
626c0 4f 4d 20 63 6c 61 75 73 65 20 6f 72 64 65 72 69  OM clause orderi
626d0 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68  ng..*/.struct Wh
626e0 65 72 65 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65  ereLevel {.  Whe
626f0 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20  rePlan plan;    
62700 20 20 20 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e     /* query plan
62710 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e   for this elemen
62720 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  t of the FROM cl
62730 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c  ause */.  int iL
62740 65 66 74 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20  eftJoin;        
62750 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75  /* Memory cell u
62760 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
62770 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
62780 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 43 75   */.  int iTabCu
62790 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  r;          /* T
627a0 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 75  he VDBE cursor u
627b0 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  sed to access th
627c0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
627d0 20 69 49 64 78 43 75 72 3b 20 20 20 20 20 20 20   iIdxCur;       
627e0 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63     /* The VDBE c
627f0 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63  ursor used to ac
62800 63 65 73 73 20 70 49 64 78 20 2a 2f 0a 20 20 69  cess pIdx */.  i
62810 6e 74 20 61 64 64 72 42 72 6b 3b 20 20 20 20 20  nt addrBrk;     
62820 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
62830 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f  e to break out o
62840 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  f the loop */.  
62850 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20  int addrNxt;    
62860 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
62870 72 65 20 74 6f 20 73 74 61 72 74 20 74 68 65 20  re to start the 
62880 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74  next IN combinat
62890 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ion */.  int add
628a0 72 43 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 2f  rCont;         /
628b0 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63  * Jump here to c
628c0 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65  ontinue with the
628d0 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65   next loop cycle
628e0 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 46 69   */.  int addrFi
628f0 72 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 46  rst;        /* F
62900 69 72 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  irst instruction
62910 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6f 66 20   of interior of 
62920 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38  the loop */.  u8
62930 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20   iFrom;         
62940 20 20 20 20 2f 2a 20 57 68 69 63 68 20 65 6e 74      /* Which ent
62950 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ry in the FROM c
62960 6c 61 75 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70  lause */.  u8 op
62970 2c 20 70 35 3b 20 20 20 20 20 20 20 20 20 20 20  , p5;           
62980 20 2f 2a 20 4f 70 63 6f 64 65 20 61 6e 64 20 50   /* Opcode and P
62990 35 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20  5 of the opcode 
629a0 74 68 61 74 20 65 6e 64 73 20 74 68 65 20 6c 6f  that ends the lo
629b0 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20  op */.  int p1, 
629c0 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p2;           /*
629d0 20 4f 70 65 72 61 6e 64 73 20 6f 66 20 74 68 65   Operands of the
629e0 20 6f 70 63 6f 64 65 20 75 73 65 64 20 74 6f 20   opcode used to 
629f0 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  ends the loop */
62a00 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20  .  union {      
62a10 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f           /* Info
62a20 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 64 65 70  rmation that dep
62a30 65 6e 64 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46  ends on plan.wsF
62a40 6c 61 67 73 20 2a 2f 0a 20 20 20 20 73 74 72 75  lags */.    stru
62a50 63 74 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ct {.      int n
62a60 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  In;             
62a70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
62a80 74 72 69 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70  tries in aInLoop
62a90 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75  [] */.      stru
62aa0 63 74 20 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20  ct InLoop {.    
62ab0 20 20 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20      int iCur;   
62ac0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
62ad0 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73  e VDBE cursor us
62ae0 65 64 20 62 79 20 74 68 69 73 20 49 4e 20 6f 70  ed by this IN op
62af0 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20  erator */.      
62b00 20 20 69 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b    int addrInTop;
62b10 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 20           /* Top 
62b20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a  of the IN loop *
62b30 2f 0a 20 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f  /.      } *aInLo
62b40 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  op;           /*
62b50 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
62b60 75 74 20 65 61 63 68 20 6e 65 73 74 65 64 20 49  ut each nested I
62b70 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  N operator */.  
62b80 20 20 7d 20 69 6e 3b 20 20 20 20 20 20 20 20 20    } in;         
62b90 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
62ba0 77 68 65 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67  when plan.wsFlag
62bb0 73 26 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20  s&WHERE_IN_ABLE 
62bc0 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20  */.  } u;..  /* 
62bd0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69  The following fi
62be0 65 6c 64 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f  eld is really no
62bf0 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 75  t part of the cu
62c00 72 72 65 6e 74 20 6c 65 76 65 6c 2e 20 20 42 75  rrent level.  Bu
62c10 74 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64 20 61  t.  ** we need a
62c20 20 70 6c 61 63 65 20 74 6f 20 63 61 63 68 65 20   place to cache 
62c30 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  virtual table in
62c40 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  dex information 
62c50 66 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 76 69  for each.  ** vi
62c60 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 74  rtual table in t
62c70 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61  he FROM clause a
62c80 6e 64 20 74 68 65 20 57 68 65 72 65 4c 65 76 65  nd the WhereLeve
62c90 6c 20 73 74 72 75 63 74 75 72 65 20 69 73 0a 20  l structure is. 
62ca0 20 2a 2a 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74   ** a convenient
62cb0 20 70 6c 61 63 65 20 73 69 6e 63 65 20 74 68 65   place since the
62cc0 72 65 20 69 73 20 6f 6e 65 20 57 68 65 72 65 4c  re is one WhereL
62cd0 65 76 65 6c 20 66 6f 72 20 65 61 63 68 20 46 52  evel for each FR
62ce0 4f 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2a 20 65  OM clause.  ** e
62cf0 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73  lement..  */.  s
62d00 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
62d10 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a  o *pIdxInfo;  /*
62d20 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20   Index info for 
62d30 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c  n-th source tabl
62d40 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46  e */.};../*.** F
62d50 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 65  lags appropriate
62d60 20 66 6f 72 20 74 68 65 20 77 63 74 72 6c 46 6c   for the wctrlFl
62d70 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 6f 66  ags parameter of
62d80 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
62d90 69 6e 28 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20  in().** and the 
62da0 57 68 65 72 65 49 6e 66 6f 2e 77 63 74 72 6c 46  WhereInfo.wctrlF
62db0 6c 61 67 73 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a  lags member..*/.
62dc0 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52  #define WHERE_OR
62dd0 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 20 20 20 30  DERBY_NORMAL   0
62de0 78 30 30 30 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a  x0000 /* No-op *
62df0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
62e00 4f 52 44 45 52 42 59 5f 4d 49 4e 20 20 20 20 20  ORDERBY_MIN     
62e10 20 30 78 30 30 30 31 20 2f 2a 20 4f 52 44 45 52   0x0001 /* ORDER
62e20 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66   BY processing f
62e30 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63 20 2a 2f  or min() func */
62e40 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f  .#define WHERE_O
62e50 52 44 45 52 42 59 5f 4d 41 58 20 20 20 20 20 20  RDERBY_MAX      
62e60 30 78 30 30 30 32 20 2f 2a 20 4f 52 44 45 52 20  0x0002 /* ORDER 
62e70 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f  BY processing fo
62e80 72 20 6d 61 78 28 29 20 66 75 6e 63 20 2a 2f 0a  r max() func */.
62e90 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e  #define WHERE_ON
62ea0 45 50 41 53 53 5f 44 45 53 49 52 45 44 20 20 30  EPASS_DESIRED  0
62eb0 78 30 30 30 34 20 2f 2a 20 57 61 6e 74 20 74 6f  x0004 /* Want to
62ec0 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50 44   do one-pass UPD
62ed0 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 23 64  ATE/DELETE */.#d
62ee0 65 66 69 6e 65 20 57 48 45 52 45 5f 44 55 50 4c  efine WHERE_DUPL
62ef0 49 43 41 54 45 53 5f 4f 4b 20 20 20 20 30 78 30  ICATES_OK    0x0
62f00 30 30 38 20 2f 2a 20 4f 6b 20 74 6f 20 72 65 74  008 /* Ok to ret
62f10 75 72 6e 20 61 20 72 6f 77 20 6d 6f 72 65 20 74  urn a row more t
62f20 68 61 6e 20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66  han once */.#def
62f30 69 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f  ine WHERE_OMIT_O
62f40 50 45 4e 20 20 20 20 20 20 20 20 30 78 30 30 31  PEN        0x001
62f50 30 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f  0 /* Table curso
62f60 72 20 61 72 65 20 61 6c 72 65 61 64 79 20 6f 70  r are already op
62f70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  en */.#define WH
62f80 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 20  ERE_OMIT_CLOSE  
62f90 20 20 20 20 20 30 78 30 30 32 30 20 2f 2a 20 4f       0x0020 /* O
62fa0 6d 69 74 20 63 6c 6f 73 65 20 6f 66 20 74 61 62  mit close of tab
62fb0 6c 65 20 26 20 69 6e 64 65 78 20 63 75 72 73 6f  le & index curso
62fc0 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  rs */.#define WH
62fd0 45 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20  ERE_FORCE_TABLE 
62fe0 20 20 20 20 20 30 78 30 30 34 30 20 2f 2a 20 44       0x0040 /* D
62ff0 6f 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64  o not use an ind
63000 65 78 2d 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a  ex-only search *
63010 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45  /../*.** The WHE
63020 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73  RE clause proces
63030 73 69 6e 67 20 72 6f 75 74 69 6e 65 20 68 61 73  sing routine has
63040 20 74 77 6f 20 68 61 6c 76 65 73 2e 20 20 54 68   two halves.  Th
63050 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 74 20  e.** first part 
63060 64 6f 65 73 20 74 68 65 20 73 74 61 72 74 20 6f  does the start o
63070 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70  f the WHERE loop
63080 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0a   and the second.
63090 2a 2a 20 68 61 6c 66 20 64 6f 65 73 20 74 68 65  ** half does the
630a0 20 74 61 69 6c 20 6f 66 20 74 68 65 20 57 48 45   tail of the WHE
630b0 52 45 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73  RE loop.  An ins
630c0 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73  tance of.** this
630d0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65   structure is re
630e0 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 69  turned by the fi
630f0 72 73 74 20 68 61 6c 66 20 61 6e 64 20 70 61 73  rst half and pas
63100 73 65 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20  sed.** into the 
63110 73 65 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67  second half to g
63120 69 76 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75  ive some continu
63130 69 74 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57  ity..*/.struct W
63140 68 65 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72  hereInfo {.  Par
63150 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
63160 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64    /* Parsing and
63170 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67   code generating
63180 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 75 31   context */.  u1
63190 36 20 77 63 74 72 6c 46 6c 61 67 73 3b 20 20 20  6 wctrlFlags;   
631a0 20 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67     /* Flags orig
631b0 69 6e 61 6c 6c 79 20 70 61 73 73 65 64 20 74 6f  inally passed to
631c0 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
631d0 69 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f  in() */.  u8 okO
631e0 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 2f  nePass;        /
631f0 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65 2d  * Ok to use one-
63200 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 66  pass algorithm f
63210 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c  or UPDATE or DEL
63220 45 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ETE */.  SrcList
63230 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 20 20   *pTabList;     
63240 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
63250 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  of tables in the
63260 20 6a 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69   join */.  int i
63270 54 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  Top;            
63280 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
63290 20 76 65 72 79 20 62 65 67 69 6e 6e 69 6e 67 20   very beginning 
632a0 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f  of the WHERE loo
632b0 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74  p */.  int iCont
632c0 69 6e 75 65 3b 20 20 20 20 20 20 20 20 20 20 20  inue;           
632d0 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
632e0 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77  re to continue w
632f0 69 74 68 20 6e 65 78 74 20 72 65 63 6f 72 64 20  ith next record 
63300 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 3b  */.  int iBreak;
63310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63320 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
63330 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
63340 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69   the loop */.  i
63350 6e 74 20 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20  nt nLevel;      
63360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
63370 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
63380 64 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74 72 75  d loop */.  stru
63390 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  ct WhereClause *
633a0 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20 44 65  pWC;       /* De
633b0 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74  composition of t
633c0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
633d0 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20  */.  WhereLevel 
633e0 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20  a[1];           
633f0 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
63400 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65  on about each ne
63410 73 74 20 6c 6f 6f 70 20 69 6e 20 57 48 45 52 45  st loop in WHERE
63420 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
63430 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 66 69  NameContext defi
63440 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20 69 6e  nes a context in
63450 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f 6c 76   which to resolv
63460 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  e table and colu
63470 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 68  mn.** names.  Th
63480 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69 73  e context consis
63490 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20  ts of a list of 
634a0 74 61 62 6c 65 73 20 28 74 68 65 20 70 53 72 63  tables (the pSrc
634b0 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e 64 0a  List) field and.
634c0 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d  ** a list of nam
634d0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 28 70  ed expression (p
634e0 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e 61 6d  EList).  The nam
634f0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  ed expression li
63500 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c  st may.** be NUL
63510 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63 6f 72  L.  The pSrc cor
63520 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20  responds to the 
63530 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
63540 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 6f   SELECT or.** to
63550 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
63560 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 79 20   operated on by 
63570 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20  INSERT, UPDATE, 
63580 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68 65 0a  or DELETE.  The.
63590 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 65 73  ** pEList corres
635a0 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 65 73  ponds to the res
635b0 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c  ult set of a SEL
635c0 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c 20  ECT and is NULL 
635d0 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74 61  for.** other sta
635e0 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e  tements..**.** N
635f0 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e 20  ameContexts can 
63600 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65 6e  be nested.  When
63610 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73   resolving names
63620 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74  , the inner-most
63630 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20   .** context is 
63640 73 65 61 72 63 68 65 64 20 66 69 72 73 74 2e 20  searched first. 
63650 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20   If no match is 
63660 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 74 20  found, the next 
63670 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 74  outer.** context
63680 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 49 66   is checked.  If
63690 20 74 68 65 72 65 20 69 73 20 73 74 69 6c 6c 20   there is still 
636a0 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 6e 65  no match, the ne
636b0 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 73  xt context.** is
636c0 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69 73 20   checked.  This 
636d0 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75 65  process continue
636e0 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61  s until either a
636f0 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 0a   match is found.
63700 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 78  ** or all contex
63710 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20 20 57  ts are check.  W
63720 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73 20 66  hen a match is f
63730 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 20 6d  ound, the nRef m
63740 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20  ember of.** the 
63750 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e 69  context containi
63760 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69 73 20  ng the match is 
63770 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a  incremented. .**
63780 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75 65 72  .** Each subquer
63790 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e 61 6d  y gets a new Nam
637a0 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 70  eContext.  The p
637b0 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69 6e 74  Next field point
637c0 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d 65  s to the.** Name
637d0 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 70  Context in the p
637e0 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20 54 68  arent query.  Th
637f0 75 73 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f  us the process o
63800 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a 2a  f scanning the.*
63810 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c 69  * NameContext li
63820 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  st corresponds t
63830 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68 72 6f  o searching thro
63840 75 67 68 20 73 75 63 63 65 73 73 69 76 65 6c 79  ugh successively
63850 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 75 65   outer.** subque
63860 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  ries looking for
63870 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 72   a match..*/.str
63880 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  uct NameContext 
63890 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
638a0 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e;       /* The 
638b0 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 63 4c  parser */.  SrcL
638c0 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20 20  ist *pSrcList;  
638d0 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20   /* One or more 
638e0 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 72  tables used to r
638f0 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f 0a  esolve names */.
63900 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
63910 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e  st;    /* Option
63920 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64  al list of named
63930 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a   expressions */.
63940 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
63950 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
63960 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c 76   of names resolv
63970 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74 65  ed by this conte
63980 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72  xt */.  int nErr
63990 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
639a0 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
639b0 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69   encountered whi
639c0 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d  le resolving nam
639d0 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f 77  es */.  u8 allow
639e0 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Agg;         /* 
639f0 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  Aggregate functi
63a00 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 72 65  ons allowed here
63a10 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 67 3b   */.  u8 hasAgg;
63a20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
63a30 75 65 20 69 66 20 61 67 67 72 65 67 61 74 65 73  ue if aggregates
63a40 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20 75   are seen */.  u
63a50 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20 20 20  8 isCheck;      
63a60 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72      /* True if r
63a70 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69  esolving names i
63a80 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  n a CHECK constr
63a90 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  aint */.  int nD
63aa0 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 2f  epth;          /
63ab0 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62 71 75  * Depth of subqu
63ac0 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e 20 31  ery recursion. 1
63ad0 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 69 6f   for no recursio
63ae0 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a  n */.  AggInfo *
63af0 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49  pAggInfo;   /* I
63b00 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
63b10 20 61 67 67 72 65 67 61 74 65 73 20 61 74 20 74   aggregates at t
63b20 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e  his level */.  N
63b30 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78  ameContext *pNex
63b40 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65  t;  /* Next oute
63b50 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20  r name context. 
63b60 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d   NULL for outerm
63b70 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ost */.};../*.**
63b80 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
63b90 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
63ba0 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
63bb0 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   all information
63bc0 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 67 65  .** needed to ge
63bd0 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
63be0 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 54 20  a single SELECT 
63bf0 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
63c00 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20 74   nLimit is set t
63c10 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73  o -1 if there is
63c20 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65   no LIMIT clause
63c30 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73 65  .  nOffset is se
63c40 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 68  t to 0..** If th
63c50 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20 63  ere is a LIMIT c
63c60 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 73 65  lause, the parse
63c70 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74 6f  r sets nLimit to
63c80 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
63c90 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20 6e  e.** limit and n
63ca0 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 76 61  Offset to the va
63cb0 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73 65  lue of the offse
63cc0 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 72 65  t (or 0 if there
63cd0 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65   is not.** offse
63ce0 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72 20 6f  t).  But later o
63cf0 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e 4f  n, nLimit and nO
63d00 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74 68 65  ffset become the
63d10 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
63d20 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 42 45  s.** in the VDBE
63d30 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 65   that record the
63d40 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65   limit and offse
63d50 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a  t counters..**.*
63d60 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 5d  * addrOpenEphm[]
63d70 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 69 6e   entries contain
63d80 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
63d90 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
63da0 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 65   opcodes..** The
63db0 73 65 20 61 64 64 72 65 73 73 65 73 20 6d 75 73  se addresses mus
63dc0 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f 20 74  t be stored so t
63dd0 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20 62 61  hat we can go ba
63de0 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a  ck and fill in.*
63df0 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e 46 4f  * the P4_KEYINFO
63e00 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65 74 65   and P2 paramete
63e10 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 74 68  rs later.  Neith
63e20 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e  er the KeyInfo n
63e30 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  or.** the number
63e40 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 50   of columns in P
63e50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 74 65  2 can be compute
63e60 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  d at the same ti
63e70 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50 5f  me.** as the OP_
63e80 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 75 63  OpenEphm instruc
63e90 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 62 65  tion is coded be
63ea0 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f  cause not.** eno
63eb0 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ugh information 
63ec0 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f 75  about the compou
63ed0 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e 6f 77  nd query is know
63ee0 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e  n at that point.
63ef0 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f 20  .** The KeyInfo 
63f00 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e  for addrOpenTran
63f10 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e 74  [0] and [1] cont
63f20 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73  ains collating s
63f30 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 20  equences.** for 
63f40 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20  the result set. 
63f50 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72   The KeyInfo for
63f60 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32 5d   addrOpenTran[2]
63f70 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74   contains collat
63f80 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73  ing.** sequences
63f90 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20 42   for the ORDER B
63fa0 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 72  Y clause..*/.str
63fb0 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 20 45  uct Select {.  E
63fc0 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
63fd0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 65        /* The fie
63fe0 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c  lds of the resul
63ff0 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20  t */.  u8 op;   
64000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
64010 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f   One of: TK_UNIO
64020 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45  N TK_ALL TK_INTE
64030 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54 20  RSECT TK_EXCEPT 
64040 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69  */.  char affini
64050 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ty;         /* M
64060 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 20 74  akeRecord with t
64070 68 69 73 20 61 66 66 69 6e 69 74 79 20 66 6f 72  his affinity for
64080 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75 31   SRT_Set */.  u1
64090 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 20 20  6 selFlags;     
640a0 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20       /* Various 
640b0 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20  SF_* values */. 
640c0 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20   SrcList *pSrc; 
640d0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46          /* The F
640e0 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
640f0 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20  Expr *pWhere;   
64100 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
64110 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
64120 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70  ExprList *pGroup
64130 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52  By;    /* The GR
64140 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  OUP BY clause */
64150 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67  .  Expr *pHaving
64160 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
64170 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 2a   HAVING clause *
64180 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
64190 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54 68  rderBy;    /* Th
641a0 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
641b0 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  e */.  Select *p
641c0 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a  Prior;        /*
641d0 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20 69 6e   Prior select in
641e0 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
641f0 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ct statement */.
64200 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 74 3b    Select *pNext;
64210 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
64220 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65 20 6c   select to the l
64230 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e  eft in a compoun
64240 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  d */.  Select *p
64250 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f 2a  Rightmost;    /*
64260 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c 65   Right-most sele
64270 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  ct in a compound
64280 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   select statemen
64290 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69  t */.  Expr *pLi
642a0 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  mit;          /*
642b0 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69 6f   LIMIT expressio
642c0 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f  n. NULL means no
642d0 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78 70  t used. */.  Exp
642e0 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 20 20  r *pOffset;     
642f0 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 65 78      /* OFFSET ex
64300 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d  pression. NULL m
64310 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a  eans not used. *
64320 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20  /.  int iLimit, 
64330 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65  iOffset;   /* Me
64340 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 68  mory registers h
64350 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f  olding LIMIT & O
64360 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73 20 2a  FFSET counters *
64370 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65 6e  /.  int addrOpen
64380 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50  Ephm[3];   /* OP
64390 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64  _OpenEphem opcod
643a0 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68  es related to th
643b0 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a  is select */.};.
643c0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
643d0 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 63 74  alues for Select
643e0 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 65 20  .selFlags.  The 
643f0 22 53 46 22 20 70 72 65 66 69 78 20 73 74 61 6e  "SF" prefix stan
64400 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 63  ds for.** "Selec
64410 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 66  t Flag"..*/.#def
64420 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 74 20  ine SF_Distinct 
64430 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f         0x0001  /
64440 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20  * Output should 
64450 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 23  be DISTINCT */.#
64460 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c 76  define SF_Resolv
64470 65 64 20 20 20 20 20 20 20 20 30 78 30 30 30 32  ed        0x0002
64480 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 73    /* Identifiers
64490 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c   have been resol
644a0 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ved */.#define S
644b0 46 5f 41 67 67 72 65 67 61 74 65 20 20 20 20 20  F_Aggregate     
644c0 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43 6f 6e    0x0004  /* Con
644d0 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 20  tains aggregate 
644e0 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65  functions */.#de
644f0 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70 68 65  fine SF_UsesEphe
64500 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38 20 20  meral   0x0008  
64510 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70 65 6e  /* Uses the Open
64520 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65  Ephemeral opcode
64530 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 45   */.#define SF_E
64540 78 70 61 6e 64 65 64 20 20 20 20 20 20 20 20 30  xpanded        0
64550 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 74 65  x0010  /* sqlite
64560 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20  3SelectExpand() 
64570 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 2a  called on this *
64580 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 61 73  /.#define SF_Has
64590 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30 78 30  TypeInfo     0x0
645a0 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75 62  020  /* FROM sub
645b0 71 75 65 72 69 65 73 20 68 61 76 65 20 54 61 62  queries have Tab
645c0 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a 0a  le metadata */..
645d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  ./*.** The resul
645e0 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20 63  ts of a select c
645f0 61 6e 20 62 65 20 64 69 73 74 72 69 62 75 74 65  an be distribute
64600 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79  d in several way
64610 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 54 22  s.  The.** "SRT"
64620 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20 22 53   prefix means "S
64630 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54 79 70  ELECT Result Typ
64640 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  e"..*/.#define S
64650 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 20  RT_Union        
64660 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75  1  /* Store resu
64670 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e  lt as keys in an
64680 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e   index */.#defin
64690 65 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 20  e SRT_Except    
646a0 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65 20     2  /* Remove 
646b0 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 55 4e  result from a UN
646c0 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65  ION index */.#de
646d0 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74 73 20  fine SRT_Exists 
646e0 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74 6f 72        3  /* Stor
646f0 65 20 31 20 69 66 20 74 68 65 20 72 65 73 75 6c  e 1 if the resul
64700 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 2a  t is not empty *
64710 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 44 69  /.#define SRT_Di
64720 73 63 61 72 64 20 20 20 20 20 20 34 20 20 2f 2a  scard      4  /*
64730 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 65   Do not save the
64740 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68 65 72   results anywher
64750 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 44  e */../* The ORD
64760 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  ER BY clause is 
64770 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c 20  ignored for all 
64780 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a 2f 0a  of the above */.
64790 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 62 6c  #define Ignorabl
647a0 65 4f 72 64 65 72 62 79 28 58 29 20 28 28 58 2d  eOrderby(X) ((X-
647b0 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 69 73  >eDest)<=SRT_Dis
647c0 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 20 53  card)..#define S
647d0 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20 20 20  RT_Output       
647e0 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 61 63  5  /* Output eac
647f0 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20  h row of result 
64800 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4d  */.#define SRT_M
64810 65 6d 20 20 20 20 20 20 20 20 20 20 36 20 20 2f  em          6  /
64820 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69  * Store result i
64830 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  n a memory cell 
64840 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 53  */.#define SRT_S
64850 65 74 20 20 20 20 20 20 20 20 20 20 37 20 20 2f  et          7  /
64860 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20  * Store results 
64870 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e  as keys in an in
64880 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  dex */.#define S
64890 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 20  RT_Table        
648a0 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75  8  /* Store resu
648b0 6c 74 20 61 73 20 64 61 74 61 20 77 69 74 68 20  lt as data with 
648c0 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 77  an automatic row
648d0 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  id */.#define SR
648e0 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 20 39  T_EphemTab     9
648f0 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72 61 6e    /* Create tran
64900 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20 73 74  sient tab and st
64910 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 62  ore like SRT_Tab
64920 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  le */.#define SR
64930 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 31 30  T_Coroutine   10
64940 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
64950 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72 65  single row of re
64960 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  sult */../*.** A
64970 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20   structure used 
64980 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 65  to customize the
64990 20 62 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c   behavior of sql
649a0 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20 53 65  ite3Select(). Se
649b0 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 61 62  e.** comments ab
649c0 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ove sqlite3Selec
649d0 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  t() for details.
649e0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
649f0 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 53 65  ct SelectDest Se
64a00 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75 63 74  lectDest;.struct
64a10 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a 20 20   SelectDest {.  
64a20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20 20 20  u8 eDest;       
64a30 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70    /* How to disp
64a40 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c  ose of the resul
64a50 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e  ts */.  u8 affin
64a60 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66  ity;      /* Aff
64a70 69 6e 69 74 79 20 75 73 65 64 20 77 68 65 6e 20  inity used when 
64a80 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a  eDest==SRT_Set *
64a90 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b 20 20  /.  int iParm;  
64aa0 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 61 6d        /* A param
64ab0 65 74 65 72 20 75 73 65 64 20 62 79 20 74 68 65  eter used by the
64ac0 20 65 44 65 73 74 20 64 69 73 70 6f 73 61 6c 20   eDest disposal 
64ad0 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20  method */.  int 
64ae0 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a  iMem;         /*
64af0 20 42 61 73 65 20 72 65 67 69 73 74 65 72 20 77   Base register w
64b00 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65  here results are
64b10 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e   written */.  in
64b20 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t nMem;         
64b30 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67  /* Number of reg
64b40 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 65 64  isters allocated
64b50 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69   */.};../*.** Si
64b60 7a 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ze of the column
64b70 20 63 61 63 68 65 0a 2a 2f 0a 23 69 66 6e 64 65   cache.*/.#ifnde
64b80 66 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  f SQLITE_N_COLCA
64b90 43 48 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  CHE.# define SQL
64ba0 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 20 31  ITE_N_COLCACHE 1
64bb0 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
64bc0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f  An SQL parser co
64bd0 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f  ntext.  A copy o
64be0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
64bf0 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75   is passed throu
64c00 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72  gh.** the parser
64c10 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61   and down into a
64c20 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63  ll the parser ac
64c30 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20  tion routine in 
64c40 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72  order to.** carr
64c50 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61  y around informa
64c60 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f  tion that is glo
64c70 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72  bal to the entir
64c80 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  e parse..**.** T
64c90 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  he structure is 
64ca0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f  divided into two
64cb0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68   parts.  When th
64cc0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64  e parser and cod
64cd0 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61  e.** generate ca
64ce0 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65  ll themselves re
64cf0 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66  cursively, the f
64d00 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65  irst part of the
64d10 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73   structure.** is
64d20 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68   constant but th
64d30 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73  e second part is
64d40 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65   reset at the be
64d50 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20  ginning and end 
64d60 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72  of.** each recur
64d70 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sion..**.** The 
64d80 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61  nTableLock and a
64d90 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62  TableLock variab
64da0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  les are only use
64db0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d  d if the shared-
64dc0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72  cache .** featur
64dd0 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66  e is enabled (if
64de0 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75   sqlite3Tsd()->u
64df0 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20  seSharedData is 
64e00 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a  true). They are.
64e10 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ** used to store
64e20 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c   the set of tabl
64e30 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64  e-locks required
64e40 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e   by the statemen
64e50 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69  t being.** compi
64e60 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71  led. Function sq
64e70 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29  lite3TableLock()
64e80 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
64e90 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a  entries to the.*
64ea0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63  * list..*/.struc
64eb0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69  t Parse {.  sqli
64ec0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
64ed0 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74   /* The main dat
64ee0 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20  abase structure 
64ef0 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
64f00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
64f10 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78  urn code from ex
64f20 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61  ecution */.  cha
64f30 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
64f40 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65    /* An error me
64f50 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20  ssage */.  Vdbe 
64f60 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20  *pVdbe;         
64f70 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72  /* An engine for
64f80 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62   executing datab
64f90 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a  ase bytecode */.
64fa0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74    u8 colNamesSet
64fb0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61  ;      /* TRUE a
64fc0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61  fter OP_ColumnNa
64fd0 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75  me has been issu
64fe0 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20  ed to pVdbe */. 
64ff0 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20   u8 nameClash;  
65000 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61        /* A perma
65010 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20  nent table name 
65020 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d  clashes with tem
65030 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  p table name */.
65040 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61    u8 checkSchema
65050 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73  ;      /* Causes
65060 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63   schema cookie c
65070 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72  heck after an er
65080 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74  ror */.  u8 nest
65090 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ed;           /*
650a0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
650b0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70  d calls to the p
650c0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72  arser/code gener
650d0 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72  ator */.  u8 par
650e0 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f  seError;       /
650f0 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70  * True after a p
65100 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54  arsing error.  T
65110 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20  icket #1794 */. 
65120 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20   u8 nTempReg;   
65130 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
65140 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  of temporary reg
65150 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52  isters in aTempR
65160 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65  eg[] */.  u8 nTe
65170 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f  mpInUse;       /
65180 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d  * Number of aTem
65190 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79  pReg[] currently
651a0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
651b0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38    int aTempReg[8
651c0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e  ];     /* Holdin
651d0 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f  g area for tempo
651e0 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a  rary registers *
651f0 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65  /.  int nRangeRe
65200 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  g;       /* Size
65210 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72   of the temporar
65220 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b  y register block
65230 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65   */.  int iRange
65240 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69  Reg;       /* Fi
65250 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20  rst register in 
65260 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
65270 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  er block */.  in
65280 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20  t nErr;         
65290 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
652a0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20  errors seen */. 
652b0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20   int nTab;      
652c0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
652d0 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  of previously al
652e0 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72  located VDBE cur
652f0 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  sors */.  int nM
65300 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
65310 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
65320 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f  ry cells used so
65330 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53   far */.  int nS
65340 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  et;            /
65350 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73  * Number of sets
65360 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a   used so far */.
65370 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20    int ckBase;   
65380 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
65390 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20  egister of data 
653a0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e  during check con
653b0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  straints */.  in
653c0 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 20  t iCacheLevel;  
653d0 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 20 76     /* ColCache v
653e0 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c 43 61  alid when aColCa
653f0 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d 69 43  che[].iLevel<=iC
65400 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 20 69  acheLevel */.  i
65410 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 20 20  nt iCacheCnt;   
65420 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 75      /* Counter u
65430 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  sed to generate 
65440 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 75 20  aColCache[].lru 
65450 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 6e  values */.  u8 n
65460 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20  ColCache;       
65470 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
65480 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 6c  tries in the col
65490 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 75  umn cache */.  u
654a0 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20 20  8 iColCache;    
654b0 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72      /* Next entr
654c0 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 74  y of the cache t
654d0 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 73  o replace */.  s
654e0 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
654f0 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65  {.    int iTable
65500 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
65510 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  able cursor numb
65520 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  er */.    int iC
65530 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
65540 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  /* Table column 
65550 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75 38  number */.    u8
65560 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20 20   affChange;     
65570 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
65580 68 69 73 20 72 65 67 69 73 74 65 72 20 68 61 73  his register has
65590 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74 79   had an affinity
655a0 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 75   change */.    u
655b0 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 20 20  8 tempReg;      
655c0 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 73 20       /* iReg is 
655d0 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 20  a temp register 
655e0 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65  that needs to be
655f0 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e   freed */.    in
65600 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 20 20  t iLevel;       
65610 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 6c      /* Nesting l
65620 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20  evel */.    int 
65630 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20  iReg;           
65640 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 76 61    /* Reg with va
65650 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75  lue of this colu
65660 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e 65  mn. 0 means none
65670 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 75  . */.    int lru
65680 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
65690 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 6c 79  * Least recently
656a0 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 73 20   used entry has 
656b0 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c  the smallest val
656c0 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 61  ue */.  } aColCa
656d0 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  che[SQLITE_N_COL
656e0 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20  CACHE];  /* One 
656f0 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
65700 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20  cache entry */. 
65710 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b 3b 20   u32 writeMask; 
65720 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 61        /* Start a
65730 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
65740 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 74 61  on on these data
65750 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 20 63  bases */.  u32 c
65760 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 20 20  ookieMask;      
65770 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 73 63  /* Bitmask of sc
65780 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 64 61  hema verified da
65790 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 69 6e 74  tabases */.  int
657a0 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b 20 20 20 20   cookieGoto;    
657b0 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
657c0 4f 50 5f 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69  OP_Goto to cooki
657d0 65 20 76 65 72 69 66 69 65 72 20 73 75 62 72 6f  e verifier subro
657e0 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 63  utine */.  int c
657f0 6f 6f 6b 69 65 56 61 6c 75 65 5b 53 51 4c 49 54  ookieValue[SQLIT
65800 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32  E_MAX_ATTACHED+2
65810 5d 3b 20 20 2f 2a 20 56 61 6c 75 65 73 20 6f 66  ];  /* Values of
65820 20 63 6f 6f 6b 69 65 73 20 74 6f 20 76 65 72 69   cookies to veri
65830 66 79 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  fy */.#ifndef SQ
65840 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
65850 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 54 61  _CACHE.  int nTa
65860 62 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20  bleLock;        
65870 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63  /* Number of loc
65880 6b 73 20 69 6e 20 61 54 61 62 6c 65 4c 6f 63 6b  ks in aTableLock
65890 20 2a 2f 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20   */.  TableLock 
658a0 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20  *aTableLock; /* 
658b0 52 65 71 75 69 72 65 64 20 74 61 62 6c 65 20 6c  Required table l
658c0 6f 63 6b 73 20 66 6f 72 20 73 68 61 72 65 64 2d  ocks for shared-
658d0 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 65  cache mode */.#e
658e0 6e 64 69 66 0a 20 20 69 6e 74 20 72 65 67 52 6f  ndif.  int regRo
658f0 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 52  wid;        /* R
65900 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
65910 72 6f 77 69 64 20 6f 66 20 43 52 45 41 54 45 20  rowid of CREATE 
65920 54 41 42 4c 45 20 65 6e 74 72 79 20 2a 2f 0a 20  TABLE entry */. 
65930 20 69 6e 74 20 72 65 67 52 6f 6f 74 3b 20 20 20   int regRoot;   
65940 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
65950 72 20 68 6f 6c 64 69 6e 67 20 72 6f 6f 74 20 70  r holding root p
65960 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 6e  age number for n
65970 65 77 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 0a 20  ew objects */.. 
65980 20 2f 2a 20 41 62 6f 76 65 20 69 73 20 63 6f 6e   /* Above is con
65990 73 74 61 6e 74 20 62 65 74 77 65 65 6e 20 72 65  stant between re
659a0 63 75 72 73 69 6f 6e 73 2e 20 20 42 65 6c 6f 77  cursions.  Below
659b0 20 69 73 20 72 65 73 65 74 20 62 65 66 6f 72 65   is reset before
659c0 20 61 6e 64 20 61 66 74 65 72 0a 20 20 2a 2a 20   and after.  ** 
659d0 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e 20 2a  each recursion *
659e0 2f 0a 0a 20 20 69 6e 74 20 6e 56 61 72 3b 20 20  /..  int nVar;  
659f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
65a00 62 65 72 20 6f 66 20 27 3f 27 20 76 61 72 69 61  ber of '?' varia
65a10 62 6c 65 73 20 73 65 65 6e 20 69 6e 20 74 68 65  bles seen in the
65a20 20 53 51 4c 20 73 6f 20 66 61 72 20 2a 2f 0a 20   SQL so far */. 
65a30 20 69 6e 74 20 6e 56 61 72 45 78 70 72 3b 20 20   int nVarExpr;  
65a40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
65a50 6f 66 20 75 73 65 64 20 73 6c 6f 74 73 20 69 6e  of used slots in
65a60 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f 0a   apVarExpr[] */.
65a70 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72 41 6c    int nVarExprAl
65a80 6c 6f 63 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72  loc;   /* Number
65a90 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c   of allocated sl
65aa0 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78 70 72  ots in apVarExpr
65ab0 5b 5d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 61  [] */.  Expr **a
65ac0 70 56 61 72 45 78 70 72 3b 20 20 20 20 2f 2a 20  pVarExpr;    /* 
65ad0 50 6f 69 6e 74 65 72 73 20 74 6f 20 3a 61 61 61  Pointers to :aaa
65ae0 20 61 6e 64 20 24 61 61 61 61 20 77 69 6c 64 63   and $aaaa wildc
65af0 61 72 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ard expressions 
65b00 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69 61 73 3b  */.  int nAlias;
65b10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
65b20 62 65 72 20 6f 66 20 61 6c 69 61 73 65 64 20 72  ber of aliased r
65b30 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e  esult set column
65b40 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69 61  s */.  int nAlia
65b50 73 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 4e  sAlloc;     /* N
65b60 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74  umber of allocat
65b70 65 64 20 73 6c 6f 74 73 20 66 6f 72 20 61 41 6c  ed slots for aAl
65b80 69 61 73 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a  ias[] */.  int *
65b90 61 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20  aAlias;         
65ba0 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73 65 64  /* Register used
65bb0 20 74 6f 20 68 6f 6c 64 20 61 6c 69 61 73 65 64   to hold aliased
65bc0 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 75 38 20   result */.  u8 
65bd0 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 20 20  explain;        
65be0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
65bf0 20 45 58 50 4c 41 49 4e 20 66 6c 61 67 20 69 73   EXPLAIN flag is
65c00 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 20 71 75   found on the qu
65c10 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73  ery */.  Token s
65c20 45 72 72 54 6f 6b 65 6e 3b 20 20 20 20 20 2f 2a  ErrToken;     /*
65c30 20 54 68 65 20 74 6f 6b 65 6e 20 61 74 20 77 68   The token at wh
65c40 69 63 68 20 74 68 65 20 65 72 72 6f 72 20 6f 63  ich the error oc
65c50 63 75 72 72 65 64 20 2a 2f 0a 20 20 54 6f 6b 65  curred */.  Toke
65c60 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20  n sNameToken;   
65c70 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20 75   /* Token with u
65c80 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68 65 6d  nqualified schem
65c90 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f  a object name */
65ca0 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54 6f  .  Token sLastTo
65cb0 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c  ken;    /* The l
65cc0 61 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65 64  ast token parsed
65cd0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
65ce0 20 2a 7a 53 71 6c 3b 20 20 20 20 2f 2a 20 41 6c   *zSql;    /* Al
65cf0 6c 20 53 51 4c 20 74 65 78 74 20 2a 2f 0a 20 20  l SQL text */.  
65d00 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 69  const char *zTai
65d10 6c 3b 20 20 20 2f 2a 20 41 6c 6c 20 53 51 4c 20  l;   /* All SQL 
65d20 74 65 78 74 20 70 61 73 74 20 74 68 65 20 6c 61  text past the la
65d30 73 74 20 73 65 6d 69 63 6f 6c 6f 6e 20 70 61 72  st semicolon par
65d40 73 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  sed */.  Table *
65d50 70 4e 65 77 54 61 62 6c 65 3b 20 20 20 20 2f 2a  pNewTable;    /*
65d60 20 41 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63   A table being c
65d70 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 43 52  onstructed by CR
65d80 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20  EATE TABLE */.  
65d90 54 72 69 67 67 65 72 20 2a 70 4e 65 77 54 72 69  Trigger *pNewTri
65da0 67 67 65 72 3b 20 20 20 20 20 2f 2a 20 54 72 69  gger;     /* Tri
65db0 67 67 65 72 20 75 6e 64 65 72 20 63 6f 6e 73 74  gger under const
65dc0 72 75 63 74 20 62 79 20 61 20 43 52 45 41 54 45  ruct by a CREATE
65dd0 20 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 54 72   TRIGGER */.  Tr
65de0 69 67 67 65 72 53 74 61 63 6b 20 2a 74 72 69 67  iggerStack *trig
65df0 53 74 61 63 6b 3b 20 20 2f 2a 20 54 72 69 67 67  Stack;  /* Trigg
65e00 65 72 20 61 63 74 69 6f 6e 73 20 62 65 69 6e 67  er actions being
65e10 20 63 6f 64 65 64 20 2a 2f 0a 20 20 63 6f 6e 73   coded */.  cons
65e20 74 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e  t char *zAuthCon
65e30 74 65 78 74 3b 20 2f 2a 20 54 68 65 20 36 74 68  text; /* The 6th
65e40 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 64 62   parameter to db
65e50 2d 3e 78 41 75 74 68 20 63 61 6c 6c 62 61 63 6b  ->xAuth callback
65e60 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  s */.#ifndef SQL
65e70 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
65e80 54 41 42 4c 45 0a 20 20 54 6f 6b 65 6e 20 73 41  TABLE.  Token sA
65e90 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
65ea0 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74     /* Complete t
65eb0 65 78 74 20 6f 66 20 61 20 6d 6f 64 75 6c 65 20  ext of a module 
65ec0 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 75 38  argument */.  u8
65ed0 20 64 65 63 6c 61 72 65 56 74 61 62 3b 20 20 20   declareVtab;   
65ee0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
65ef0 20 69 66 20 69 6e 73 69 64 65 20 73 71 6c 69 74   if inside sqlit
65f00 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28  e3_declare_vtab(
65f10 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62  ) */.  int nVtab
65f20 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Lock;           
65f30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76    /* Number of v
65f40 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 6f  irtual tables to
65f50 20 6c 6f 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65   lock */.  Table
65f60 20 2a 2a 61 70 56 74 61 62 4c 6f 63 6b 3b 20 20   **apVtabLock;  
65f70 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
65f80 20 74 6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c   to virtual tabl
65f90 65 73 20 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 69  es needing locki
65fa0 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69  ng */.#endif.  i
65fb0 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 20 20  nt nHeight;     
65fc0 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73         /* Expres
65fd0 73 69 6f 6e 20 74 72 65 65 20 68 65 69 67 68 74  sion tree height
65fe0 20 6f 66 20 63 75 72 72 65 6e 74 20 73 75 62 2d   of current sub-
65ff0 73 65 6c 65 63 74 20 2a 2f 0a 20 20 54 61 62 6c  select */.  Tabl
66000 65 20 2a 70 5a 6f 6d 62 69 65 54 61 62 3b 20 20  e *pZombieTab;  
66010 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 54      /* List of T
66020 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 74 6f 20  able objects to 
66030 64 65 6c 65 74 65 20 61 66 74 65 72 20 63 6f 64  delete after cod
66040 65 20 67 65 6e 20 2a 2f 0a 7d 3b 0a 0a 23 69 66  e gen */.};..#if
66050 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
66060 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 23  VIRTUALTABLE.  #
66070 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52  define IN_DECLAR
66080 45 5f 56 54 41 42 20 30 0a 23 65 6c 73 65 0a 20  E_VTAB 0.#else. 
66090 20 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c   #define IN_DECL
660a0 41 52 45 5f 56 54 41 42 20 28 70 50 61 72 73 65  ARE_VTAB (pParse
660b0 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 29 0a 23  ->declareVtab).#
660c0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  endif../*.** An 
660d0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
660e0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
660f0 75 72 65 20 63 61 6e 20 62 65 20 64 65 63 6c 61  ure can be decla
66100 72 65 64 20 6f 6e 20 61 20 73 74 61 63 6b 20 61  red on a stack a
66110 6e 64 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61  nd used.** to sa
66120 76 65 20 74 68 65 20 50 61 72 73 65 2e 7a 41 75  ve the Parse.zAu
66130 74 68 43 6f 6e 74 65 78 74 20 76 61 6c 75 65 20  thContext value 
66140 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62  so that it can b
66150 65 20 72 65 73 74 6f 72 65 64 20 6c 61 74 65 72  e restored later
66160 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 68  ..*/.struct Auth
66170 43 6f 6e 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73  Context {.  cons
66180 74 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e  t char *zAuthCon
66190 74 65 78 74 3b 20 20 20 2f 2a 20 50 75 74 20 73  text;   /* Put s
661a0 61 76 65 64 20 50 61 72 73 65 2e 7a 41 75 74 68  aved Parse.zAuth
661b0 43 6f 6e 74 65 78 74 20 68 65 72 65 20 2a 2f 0a  Context here */.
661c0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
661d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
661e0 20 54 68 65 20 50 61 72 73 65 20 73 74 72 75 63   The Parse struc
661f0 74 75 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ture */.};../*.*
66200 2a 20 42 69 74 66 69 65 6c 64 20 66 6c 61 67 73  * Bitfield flags
66210 20 66 6f 72 20 50 35 20 76 61 6c 75 65 20 69 6e   for P5 value in
66220 20 4f 50 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f   OP_Insert and O
66230 50 5f 44 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66  P_Delete.*/.#def
66240 69 6e 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e  ine OPFLAG_NCHAN
66250 47 45 20 20 20 20 31 20 20 20 20 2f 2a 20 53 65  GE    1    /* Se
66260 74 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e  t to update db->
66270 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69  nChange */.#defi
66280 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f  ne OPFLAG_LASTRO
66290 57 49 44 20 20 32 20 20 20 20 2f 2a 20 53 65 74  WID  2    /* Set
662a0 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e 6c   to update db->l
662b0 61 73 74 52 6f 77 69 64 20 2a 2f 0a 23 64 65 66  astRowid */.#def
662c0 69 6e 65 20 4f 50 46 4c 41 47 5f 49 53 55 50 44  ine OPFLAG_ISUPD
662d0 41 54 45 20 20 20 34 20 20 20 20 2f 2a 20 54 68  ATE   4    /* Th
662e0 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20  is OP_Insert is 
662f0 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a 2f  an sql UPDATE */
66300 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
66310 41 50 50 45 4e 44 20 20 20 20 20 38 20 20 20 20  APPEND     8    
66320 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65 6c  /* This is likel
66330 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e  y to be an appen
66340 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  d */.#define OPF
66350 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
66360 54 20 31 36 20 20 20 20 2f 2a 20 54 72 79 20 74  T 16    /* Try t
66370 6f 20 61 76 6f 69 64 20 61 20 73 65 65 6b 20 69  o avoid a seek i
66380 6e 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 20  n BtreeInsert() 
66390 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 61 63 68 20 74  */../*. * Each t
663a0 72 69 67 67 65 72 20 70 72 65 73 65 6e 74 20 69  rigger present i
663b0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
663c0 63 68 65 6d 61 20 69 73 20 73 74 6f 72 65 64 20  chema is stored 
663d0 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  as an instance o
663e0 66 0a 20 2a 20 73 74 72 75 63 74 20 54 72 69 67  f. * struct Trig
663f0 67 65 72 2e 20 0a 20 2a 0a 20 2a 20 50 6f 69 6e  ger. . *. * Poin
66400 74 65 72 73 20 74 6f 20 69 6e 73 74 61 6e 63 65  ters to instance
66410 73 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67  s of struct Trig
66420 67 65 72 20 61 72 65 20 73 74 6f 72 65 64 20 69  ger are stored i
66430 6e 20 74 77 6f 20 77 61 79 73 2e 0a 20 2a 20 31  n two ways.. * 1
66440 2e 20 49 6e 20 74 68 65 20 22 74 72 69 67 48 61  . In the "trigHa
66450 73 68 22 20 68 61 73 68 20 74 61 62 6c 65 20 28  sh" hash table (
66460 70 61 72 74 20 6f 66 20 74 68 65 20 73 71 6c 69  part of the sqli
66470 74 65 33 2a 20 74 68 61 74 20 72 65 70 72 65 73  te3* that repres
66480 65 6e 74 73 20 74 68 65 20 0a 20 2a 20 20 20 20  ents the . *    
66490 64 61 74 61 62 61 73 65 29 2e 20 54 68 69 73 20  database). This 
664a0 61 6c 6c 6f 77 73 20 54 72 69 67 67 65 72 20 73  allows Trigger s
664b0 74 72 75 63 74 75 72 65 73 20 74 6f 20 62 65 20  tructures to be 
664c0 72 65 74 72 69 65 76 65 64 20 62 79 20 6e 61 6d  retrieved by nam
664d0 65 2e 0a 20 2a 20 32 2e 20 41 6c 6c 20 74 72 69  e.. * 2. All tri
664e0 67 67 65 72 73 20 61 73 73 6f 63 69 61 74 65 64  ggers associated
664f0 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74   with a single t
66500 61 62 6c 65 20 66 6f 72 6d 20 61 20 6c 69 6e 6b  able form a link
66510 65 64 20 6c 69 73 74 2c 20 75 73 69 6e 67 20 74  ed list, using t
66520 68 65 0a 20 2a 20 20 20 20 70 4e 65 78 74 20 6d  he. *    pNext m
66530 65 6d 62 65 72 20 6f 66 20 73 74 72 75 63 74 20  ember of struct 
66540 54 72 69 67 67 65 72 2e 20 41 20 70 6f 69 6e 74  Trigger. A point
66550 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  er to the first 
66560 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a 20  element of the. 
66570 2a 20 20 20 20 6c 69 6e 6b 65 64 20 6c 69 73 74  *    linked list
66580 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 74 68   is stored as th
66590 65 20 22 70 54 72 69 67 67 65 72 22 20 6d 65 6d  e "pTrigger" mem
665a0 62 65 72 20 6f 66 20 74 68 65 20 61 73 73 6f 63  ber of the assoc
665b0 69 61 74 65 64 0a 20 2a 20 20 20 20 73 74 72 75  iated. *    stru
665c0 63 74 20 54 61 62 6c 65 2e 0a 20 2a 0a 20 2a 20  ct Table.. *. * 
665d0 54 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 20  The "step_list" 
665e0 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73 20 74 6f  member points to
665f0 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   the first eleme
66600 6e 74 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c  nt of a linked l
66610 69 73 74 0a 20 2a 20 63 6f 6e 74 61 69 6e 69 6e  ist. * containin
66620 67 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  g the SQL statem
66630 65 6e 74 73 20 73 70 65 63 69 66 69 65 64 20 61  ents specified a
66640 73 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  s the trigger pr
66650 6f 67 72 61 6d 2e 0a 20 2a 2f 0a 73 74 72 75 63  ogram.. */.struc
66660 74 20 54 72 69 67 67 65 72 20 7b 0a 20 20 63 68  t Trigger {.  ch
66670 61 72 20 2a 6e 61 6d 65 3b 20 20 20 20 20 20 20  ar *name;       
66680 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d        /* The nam
66690 65 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  e of the trigger
666a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
666b0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 63 68 61          */.  cha
666c0 72 20 2a 74 61 62 6c 65 3b 20 20 20 20 20 20 20  r *table;       
666d0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
666e0 65 20 6f 72 20 76 69 65 77 20 74 6f 20 77 68 69  e or view to whi
666f0 63 68 20 74 68 65 20 74 72 69 67 67 65 72 20 61  ch the trigger a
66700 70 70 6c 69 65 73 20 2a 2f 0a 20 20 75 38 20 6f  pplies */.  u8 o
66710 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
66720 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
66730 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41  _DELETE, TK_UPDA
66740 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20 20 20  TE, TK_INSERT   
66750 20 20 20 20 20 20 2a 2f 0a 20 20 75 38 20 74 72        */.  u8 tr
66760 5f 74 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  _tm;            
66770 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49     /* One of TRI
66780 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49  GGER_BEFORE, TRI
66790 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20  GGER_AFTER */.  
667a0 45 78 70 72 20 2a 70 57 68 65 6e 3b 20 20 20 20  Expr *pWhen;    
667b0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
667c0 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20 74 68  HEN clause of th
667d0 65 20 65 78 70 72 65 73 73 69 6f 6e 20 28 6d 61  e expression (ma
667e0 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20  y be NULL) */.  
667f0 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 73  IdList *pColumns
66800 3b 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  ;       /* If th
66810 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 45 20  is is an UPDATE 
66820 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e  OF <column-list>
66830 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20 20 20   trigger,.      
66840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66850 20 20 20 20 20 20 20 74 68 65 20 3c 63 6f 6c 75         the <colu
66860 6d 6e 2d 6c 69 73 74 3e 20 69 73 20 73 74 6f 72  mn-list> is stor
66870 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 54 6f 6b  ed here */.  Tok
66880 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20  en nameToken;   
66890 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 63 6f       /* Token co
668a0 6e 74 61 69 6e 69 6e 67 20 7a 4e 61 6d 65 2e 20  ntaining zName. 
668b0 55 73 65 20 64 75 72 69 6e 67 20 70 61 72 73 69  Use during parsi
668c0 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 20 20 53 63 68  ng only */.  Sch
668d0 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20  ema *pSchema;   
668e0 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63       /* Schema c
668f0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72  ontaining the tr
66900 69 67 67 65 72 20 2a 2f 0a 20 20 53 63 68 65 6d  igger */.  Schem
66910 61 20 2a 70 54 61 62 53 63 68 65 6d 61 3b 20 20  a *pTabSchema;  
66920 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e     /* Schema con
66930 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c  taining the tabl
66940 65 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74  e */.  TriggerSt
66950 65 70 20 2a 73 74 65 70 5f 6c 69 73 74 3b 20 2f  ep *step_list; /
66960 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 6f 66 20 74  * Link list of t
66970 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 73  rigger program s
66980 74 65 70 73 20 20 20 20 20 20 20 20 20 20 20 20  teps            
66990 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
669a0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Next;         /*
669b0 20 4e 65 78 74 20 74 72 69 67 67 65 72 20 61 73   Next trigger as
669c0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
669d0 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f  e table */.};../
669e0 2a 0a 2a 2a 20 41 20 74 72 69 67 67 65 72 20 69  *.** A trigger i
669f0 73 20 65 69 74 68 65 72 20 61 20 42 45 46 4f 52  s either a BEFOR
66a00 45 20 6f 72 20 61 6e 20 41 46 54 45 52 20 74 72  E or an AFTER tr
66a10 69 67 67 65 72 2e 20 20 54 68 65 20 66 6f 6c 6c  igger.  The foll
66a20 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 73 0a  owing constants.
66a30 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 77 68 69  ** determine whi
66a40 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ch. .**.** If th
66a50 65 72 65 20 61 72 65 20 6d 75 6c 74 69 70 6c 65  ere are multiple
66a60 20 74 72 69 67 67 65 72 73 2c 20 79 6f 75 20 6d   triggers, you m
66a70 69 67 68 74 20 6f 66 20 73 6f 6d 65 20 42 45 46  ight of some BEF
66a80 4f 52 45 20 61 6e 64 20 73 6f 6d 65 20 41 46 54  ORE and some AFT
66a90 45 52 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63  ER..** In that c
66aa0 61 73 65 73 2c 20 74 68 65 20 63 6f 6e 73 74 61  ases, the consta
66ab0 6e 74 73 20 62 65 6c 6f 77 20 63 61 6e 20 62 65  nts below can be
66ac0 20 4f 52 65 64 20 74 6f 67 65 74 68 65 72 2e 0a   ORed together..
66ad0 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47  */.#define TRIGG
66ae0 45 52 5f 42 45 46 4f 52 45 20 20 31 0a 23 64 65  ER_BEFORE  1.#de
66af0 66 69 6e 65 20 54 52 49 47 47 45 52 5f 41 46 54  fine TRIGGER_AFT
66b00 45 52 20 20 20 32 0a 0a 2f 2a 0a 20 2a 20 41 6e  ER   2../*. * An
66b10 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73 74 72   instance of str
66b20 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20  uct TriggerStep 
66b30 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  is used to store
66b40 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74   a single SQL st
66b50 61 74 65 6d 65 6e 74 0a 20 2a 20 74 68 61 74 20  atement. * that 
66b60 69 73 20 61 20 70 61 72 74 20 6f 66 20 61 20 74  is a part of a t
66b70 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 20  rigger-program. 
66b80 0a 20 2a 0a 20 2a 20 49 6e 73 74 61 6e 63 65 73  . *. * Instances
66b90 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67   of struct Trigg
66ba0 65 72 53 74 65 70 20 61 72 65 20 73 74 6f 72 65  erStep are store
66bb0 64 20 69 6e 20 61 20 73 69 6e 67 6c 79 20 6c 69  d in a singly li
66bc0 6e 6b 65 64 20 6c 69 73 74 20 28 6c 69 6e 6b 65  nked list (linke
66bd0 64 0a 20 2a 20 75 73 69 6e 67 20 74 68 65 20 22  d. * using the "
66be0 70 4e 65 78 74 22 20 6d 65 6d 62 65 72 29 20 72  pNext" member) r
66bf0 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65  eferenced by the
66c00 20 22 73 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d   "step_list" mem
66c10 62 65 72 20 6f 66 20 74 68 65 20 0a 20 2a 20 61  ber of the . * a
66c20 73 73 6f 63 69 61 74 65 64 20 73 74 72 75 63 74  ssociated struct
66c30 20 54 72 69 67 67 65 72 20 69 6e 73 74 61 6e 63   Trigger instanc
66c40 65 2e 20 54 68 65 20 66 69 72 73 74 20 65 6c 65  e. The first ele
66c50 6d 65 6e 74 20 6f 66 20 74 68 65 20 6c 69 6e 6b  ment of the link
66c60 65 64 20 6c 69 73 74 20 69 73 0a 20 2a 20 74 68  ed list is. * th
66c70 65 20 66 69 72 73 74 20 73 74 65 70 20 6f 66 20  e first step of 
66c80 74 68 65 20 74 72 69 67 67 65 72 2d 70 72 6f 67  the trigger-prog
66c90 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20 54 68 65 20  ram.. * . * The 
66ca0 22 6f 70 22 20 6d 65 6d 62 65 72 20 69 6e 64 69  "op" member indi
66cb0 63 61 74 65 73 20 77 68 65 74 68 65 72 20 74 68  cates whether th
66cc0 69 73 20 69 73 20 61 20 22 44 45 4c 45 54 45 22  is is a "DELETE"
66cd0 2c 20 22 49 4e 53 45 52 54 22 2c 20 22 55 50 44  , "INSERT", "UPD
66ce0 41 54 45 22 20 6f 72 0a 20 2a 20 22 53 45 4c 45  ATE" or. * "SELE
66cf0 43 54 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  CT" statement. T
66d00 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74  he meanings of t
66d10 68 65 20 6f 74 68 65 72 20 6d 65 6d 62 65 72 73  he other members
66d20 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
66d30 79 20 74 68 65 20 0a 20 2a 20 76 61 6c 75 65 20  y the . * value 
66d40 6f 66 20 22 6f 70 22 20 61 73 20 66 6f 6c 6c 6f  of "op" as follo
66d50 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d  ws:. *. * (op ==
66d60 20 54 4b 5f 49 4e 53 45 52 54 29 0a 20 2a 20 6f   TK_INSERT). * o
66d70 72 63 6f 6e 66 20 20 20 20 2d 3e 20 73 74 6f 72  rconf    -> stor
66d80 65 73 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49  es the ON CONFLI
66d90 43 54 20 61 6c 67 6f 72 69 74 68 6d 0a 20 2a 20  CT algorithm. * 
66da0 70 53 65 6c 65 63 74 20 20 20 2d 3e 20 49 66 20  pSelect   -> If 
66db0 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52  this is an INSER
66dc0 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43  T INTO ... SELEC
66dd0 54 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c  T ... statement,
66de0 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20   then. *        
66df0 20 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65        this store
66e00 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
66e10 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
66e20 65 6e 74 2e 20 4f 74 68 65 72 77 69 73 65 20 4e  ent. Otherwise N
66e30 55 4c 4c 2e 0a 20 2a 20 74 61 72 67 65 74 20 20  ULL.. * target  
66e40 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c    -> A token hol
66e50 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66  ding the name of
66e60 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e   the table to in
66e70 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70 45  sert into.. * pE
66e80 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74 68  xprList -> If th
66e90 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
66ea0 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53 20  INTO ... VALUES 
66eb0 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  ... statement, t
66ec0 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20  hen. *          
66ed0 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 20      this stores 
66ee0 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73  values to be ins
66ef0 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65  erted. Otherwise
66f00 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69 73   NULL.. * pIdLis
66f10 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69  t   -> If this i
66f20 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f  s an INSERT INTO
66f30 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61   ... (<column-na
66f40 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e 2e  mes>) VALUES ...
66f50 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20   . *            
66f60 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65    statement, the
66f70 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20 74 68  n this stores th
66f80 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74  e column-names t
66f90 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20 20  o be. *         
66fa0 20 20 20 20 20 69 6e 73 65 72 74 65 64 20 69 6e       inserted in
66fb0 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d  to.. *. * (op ==
66fc0 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20 74   TK_DELETE). * t
66fd0 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f  arget    -> A to
66fe0 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ken holding the 
66ff0 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
67000 65 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d  e to delete from
67010 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d  .. * pWhere    -
67020 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  > The WHERE clau
67030 73 65 20 6f 66 20 74 68 65 20 44 45 4c 45 54 45  se of the DELETE
67040 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e   statement if on
67050 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a  e is specified..
67060 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
67070 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a  Otherwise NULL..
67080 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b   * . * (op == TK
67090 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 61 72 67  _UPDATE). * targ
670a0 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e  et    -> A token
670b0 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d   holding the nam
670c0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
670d0 6f 20 75 70 64 61 74 65 20 72 6f 77 73 20 6f 66  o update rows of
670e0 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d  .. * pWhere    -
670f0 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  > The WHERE clau
67100 73 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45  se of the UPDATE
67110 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e   statement if on
67120 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a  e is specified..
67130 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
67140 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a  Otherwise NULL..
67150 20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20   * pExprList -> 
67160 41 20 6c 69 73 74 20 6f 66 20 74 68 65 20 63 6f  A list of the co
67170 6c 75 6d 6e 73 20 74 6f 20 75 70 64 61 74 65 20  lumns to update 
67180 61 6e 64 20 74 68 65 20 65 78 70 72 65 73 73 69  and the expressi
67190 6f 6e 73 20 74 6f 20 75 70 64 61 74 65 0a 20 2a  ons to update. *
671a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
671b0 65 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c 69 74  em to. See sqlit
671c0 65 33 55 70 64 61 74 65 28 29 20 64 6f 63 75 6d  e3Update() docum
671d0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70 43 68  entation of "pCh
671e0 61 6e 67 65 73 22 0a 20 2a 20 20 20 20 20 20 20  anges". *       
671f0 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e         argument.
67200 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20  . * . */.struct 
67210 54 72 69 67 67 65 72 53 74 65 70 20 7b 0a 20 20  TriggerStep {.  
67220 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20  int op;         
67230 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
67240 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44  K_DELETE, TK_UPD
67250 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20  ATE, TK_INSERT, 
67260 54 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69  TK_SELECT */.  i
67270 6e 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20  nt orconf;      
67280 20 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61      /* OE_Rollba
67290 63 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69  ck etc. */.  Tri
672a0 67 67 65 72 20 2a 70 54 72 69 67 3b 20 20 20 20  gger *pTrig;    
672b0 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
672c0 20 74 68 61 74 20 74 68 69 73 20 73 74 65 70 20   that this step 
672d0 69 73 20 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a  is a part of */.
672e0 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
672f0 63 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c 69 64  ct;     /* Valid
67300 20 66 6f 72 20 53 45 4c 45 43 54 20 61 6e 64 20   for SELECT and 
67310 73 6f 6d 65 74 69 6d 65 73 20 0a 20 20 20 20 20  sometimes .     
67320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67330 20 20 20 20 20 49 4e 53 45 52 54 20 73 74 65 70       INSERT step
67340 73 20 28 77 68 65 6e 20 70 45 78 70 72 4c 69 73  s (when pExprLis
67350 74 20 3d 3d 20 30 29 20 2a 2f 0a 20 20 54 6f 6b  t == 0) */.  Tok
67360 65 6e 20 74 61 72 67 65 74 3b 20 20 20 20 20 20  en target;      
67370 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 44    /* Valid for D
67380 45 4c 45 54 45 2c 20 55 50 44 41 54 45 2c 20 49  ELETE, UPDATE, I
67390 4e 53 45 52 54 20 73 74 65 70 73 20 2a 2f 0a 20  NSERT steps */. 
673a0 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20   Expr *pWhere;  
673b0 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66        /* Valid f
673c0 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 41 54  or DELETE, UPDAT
673d0 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 70  E steps */.  Exp
673e0 72 4c 69 73 74 20 2a 70 45 78 70 72 4c 69 73 74  rList *pExprList
673f0 3b 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 55  ; /* Valid for U
67400 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73  PDATE statements
67410 20 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a   and sometimes .
67420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67430 20 20 20 20 20 20 20 20 20 20 20 49 4e 53 45 52             INSER
67440 54 20 73 74 65 70 73 20 28 77 68 65 6e 20 70 53  T steps (when pS
67450 65 6c 65 63 74 20 3d 3d 20 30 29 20 20 20 20 20  elect == 0)     
67460 20 20 20 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20      */.  IdList 
67470 2a 70 49 64 4c 69 73 74 3b 20 20 20 20 20 2f 2a  *pIdList;     /*
67480 20 56 61 6c 69 64 20 66 6f 72 20 49 4e 53 45 52   Valid for INSER
67490 54 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c  T statements onl
674a0 79 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74  y */.  TriggerSt
674b0 65 70 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e  ep *pNext;  /* N
674c0 65 78 74 20 69 6e 20 74 68 65 20 6c 69 6e 6b 2d  ext in the link-
674d0 6c 69 73 74 20 2a 2f 0a 20 20 54 72 69 67 67 65  list */.  Trigge
674e0 72 53 74 65 70 20 2a 70 4c 61 73 74 3b 20 20 2f  rStep *pLast;  /
674f0 2a 20 4c 61 73 74 20 65 6c 65 6d 65 6e 74 20 69  * Last element i
67500 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c  n link-list. Val
67510 69 64 20 66 6f 72 20 31 73 74 20 65 6c 65 6d 20  id for 1st elem 
67520 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 20  only */.};../*. 
67530 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
67540 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53   struct TriggerS
67550 74 61 63 6b 20 73 74 6f 72 65 73 20 69 6e 66 6f  tack stores info
67560 72 6d 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64  rmation required
67570 20 64 75 72 69 6e 67 20 63 6f 64 65 0a 20 2a 20   during code. * 
67580 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 61 20  generation of a 
67590 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 70  single trigger p
675a0 72 6f 67 72 61 6d 2e 20 57 68 69 6c 65 20 74 68  rogram. While th
675b0 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  e trigger progra
675c0 6d 20 69 73 20 62 65 69 6e 67 0a 20 2a 20 63 6f  m is being. * co
675d0 64 65 64 2c 20 69 74 73 20 61 73 73 6f 63 69 61  ded, its associa
675e0 74 65 64 20 54 72 69 67 67 65 72 53 74 61 63 6b  ted TriggerStack
675f0 20 69 6e 73 74 61 6e 63 65 20 69 73 20 70 6f 69   instance is poi
67600 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 0a 20  nted to by the. 
67610 2a 20 22 70 54 72 69 67 67 65 72 53 74 61 63 6b  * "pTriggerStack
67620 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20  " member of the 
67630 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 2e  Parse structure.
67640 0a 20 2a 0a 20 2a 20 54 68 65 20 70 54 61 62 20  . *. * The pTab 
67650 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73 20 74 6f  member points to
67660 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
67670 74 72 69 67 67 65 72 73 20 61 72 65 20 62 65 69  triggers are bei
67680 6e 67 20 63 6f 64 65 64 20 6f 6e 2e 20 54 68 65  ng coded on. The
67690 20 0a 20 2a 20 6e 65 77 49 64 78 20 6d 65 6d 62   . * newIdx memb
676a0 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  er contains the 
676b0 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 64 62  index of the vdb
676c0 65 20 63 75 72 73 6f 72 20 74 68 61 74 20 70 6f  e cursor that po
676d0 69 6e 74 73 20 61 74 20 74 68 65 20 74 65 6d 70  ints at the temp
676e0 0a 20 2a 20 74 61 62 6c 65 20 74 68 61 74 20 73  . * table that s
676f0 74 6f 72 65 73 20 74 68 65 20 6e 65 77 2e 2a 20  tores the new.* 
67700 72 65 66 65 72 65 6e 63 65 73 2e 20 49 66 20 6e  references. If n
67710 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 20  ew.* references 
67720 61 72 65 20 6e 6f 74 20 76 61 6c 69 64 0a 20 2a  are not valid. *
67730 20 66 6f 72 20 74 68 65 20 74 72 69 67 67 65 72   for the trigger
67740 20 62 65 69 6e 67 20 63 6f 64 65 64 20 28 66 6f   being coded (fo
67750 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 4f 4e 20  r example an ON 
67760 44 45 4c 45 54 45 20 74 72 69 67 67 65 72 29 2c  DELETE trigger),
67770 20 74 68 65 6e 20 6e 65 77 49 64 78 0a 20 2a 20   then newIdx. * 
67780 69 73 20 73 65 74 20 74 6f 20 2d 31 2e 20 54 68  is set to -1. Th
67790 65 20 6f 6c 64 49 64 78 20 6d 65 6d 62 65 72 20  e oldIdx member 
677a0 69 73 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f 20  is analogous to 
677b0 6e 65 77 49 64 78 2c 20 66 6f 72 20 6f 6c 64 2e  newIdx, for old.
677c0 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 20 2a  * references.. *
677d0 0a 20 2a 20 54 68 65 20 4f 4e 20 43 4f 4e 46 4c  . * The ON CONFL
677e0 49 43 54 20 70 6f 6c 69 63 79 20 74 6f 20 62 65  ICT policy to be
677f0 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 74 72   used for the tr
67800 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 73 74  igger program st
67810 65 70 73 20 69 73 20 73 74 6f 72 65 64 20 0a 20  eps is stored . 
67820 2a 20 61 73 20 74 68 65 20 6f 72 63 6f 6e 66 20  * as the orconf 
67830 6d 65 6d 62 65 72 2e 20 49 66 20 74 68 69 73 20  member. If this 
67840 69 73 20 4f 45 5f 44 65 66 61 75 6c 74 2c 20 74  is OE_Default, t
67850 68 65 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c  hen the ON CONFL
67860 49 43 54 20 63 6c 61 75 73 65 20 0a 20 2a 20 73  ICT clause . * s
67870 70 65 63 69 66 69 65 64 20 66 6f 72 20 69 6e 64  pecified for ind
67880 69 76 69 64 75 61 6c 20 74 72 69 67 67 65 72 73  ividual triggers
67890 20 73 74 65 70 73 20 69 73 20 75 73 65 64 2e 0a   steps is used..
678a0 20 2a 0a 20 2a 20 73 74 72 75 63 74 20 54 72 69   *. * struct Tri
678b0 67 67 65 72 53 74 61 63 6b 20 68 61 73 20 61 20  ggerStack has a 
678c0 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 72 2c 20  "pNext" member, 
678d0 74 6f 20 61 6c 6c 6f 77 20 6c 69 6e 6b 65 64 20  to allow linked 
678e0 6c 69 73 74 73 20 74 6f 20 62 65 0a 20 2a 20 63  lists to be. * c
678f0 6f 6e 73 74 72 75 63 74 65 64 2e 20 57 68 65 6e  onstructed. When
67900 20 63 6f 64 69 6e 67 20 6e 65 73 74 65 64 20 74   coding nested t
67910 72 69 67 67 65 72 73 20 28 74 72 69 67 67 65 72  riggers (trigger
67920 73 20 66 69 72 65 64 20 62 79 20 6f 74 68 65 72  s fired by other
67930 20 74 72 69 67 67 65 72 73 29 0a 20 2a 20 65 61   triggers). * ea
67940 63 68 20 6e 65 73 74 65 64 20 74 72 69 67 67 65  ch nested trigge
67950 72 20 73 74 6f 72 65 73 20 69 74 73 20 70 61 72  r stores its par
67960 65 6e 74 20 74 72 69 67 67 65 72 27 73 20 54 72  ent trigger's Tr
67970 69 67 67 65 72 53 74 61 63 6b 20 61 73 20 74 68  iggerStack as th
67980 65 20 22 70 4e 65 78 74 22 20 0a 20 2a 20 70 6f  e "pNext" . * po
67990 69 6e 74 65 72 2e 20 4f 6e 63 65 20 74 68 65 20  inter. Once the 
679a0 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 20 68  nested trigger h
679b0 61 73 20 62 65 65 6e 20 63 6f 64 65 64 2c 20 74  as been coded, t
679c0 68 65 20 70 4e 65 78 74 20 76 61 6c 75 65 20 69  he pNext value i
679d0 73 20 72 65 73 74 6f 72 65 64 0a 20 2a 20 74 6f  s restored. * to
679e0 20 74 68 65 20 70 54 72 69 67 67 65 72 53 74 61   the pTriggerSta
679f0 63 6b 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65  ck member of the
67a00 20 50 61 72 73 65 20 73 74 75 63 74 75 72 65 20   Parse stucture 
67a10 61 6e 64 20 63 6f 64 69 6e 67 20 6f 66 20 74 68  and coding of th
67a20 65 20 70 61 72 65 6e 74 0a 20 2a 20 74 72 69 67  e parent. * trig
67a30 67 65 72 20 63 6f 6e 74 69 6e 75 65 73 2e 0a 20  ger continues.. 
67a40 2a 0a 20 2a 20 42 65 66 6f 72 65 20 61 20 6e 65  *. * Before a ne
67a50 73 74 65 64 20 74 72 69 67 67 65 72 20 69 73 20  sted trigger is 
67a60 63 6f 64 65 64 2c 20 74 68 65 20 6c 69 6e 6b 65  coded, the linke
67a70 64 20 6c 69 73 74 20 70 6f 69 6e 74 65 64 20 74  d list pointed t
67a80 6f 20 62 79 20 74 68 65 20 0a 20 2a 20 70 54 72  o by the . * pTr
67a90 69 67 67 65 72 53 74 61 63 6b 20 69 73 20 73 63  iggerStack is sc
67aa0 61 6e 6e 65 64 20 74 6f 20 65 6e 73 75 72 65 20  anned to ensure 
67ab0 74 68 61 74 20 74 68 65 20 74 72 69 67 67 65 72  that the trigger
67ac0 20 69 73 20 6e 6f 74 20 61 62 6f 75 74 20 74 6f   is not about to
67ad0 20 62 65 20 63 6f 64 65 64 0a 20 2a 20 72 65 63   be coded. * rec
67ae0 75 72 73 69 76 65 6c 79 2e 20 49 66 20 74 68 69  ursively. If thi
67af0 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 64  s condition is d
67b00 65 74 65 63 74 65 64 2c 20 74 68 65 20 6e 65 73  etected, the nes
67b10 74 65 64 20 74 72 69 67 67 65 72 20 69 73 20 6e  ted trigger is n
67b20 6f 74 20 63 6f 64 65 64 2e 0a 20 2a 2f 0a 73 74  ot coded.. */.st
67b30 72 75 63 74 20 54 72 69 67 67 65 72 53 74 61 63  ruct TriggerStac
67b40 6b 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  k {.  Table *pTa
67b50 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  b;         /* Ta
67b60 62 6c 65 20 74 68 61 74 20 74 72 69 67 67 65 72  ble that trigger
67b70 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20  s are currently 
67b80 62 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e 20 2a  being coded on *
67b90 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 3b 20  /.  int newIdx; 
67ba0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
67bb0 78 20 6f 66 20 76 64 62 65 20 63 75 72 73 6f 72  x of vdbe cursor
67bc0 20 74 6f 20 22 6e 65 77 22 20 74 65 6d 70 20 74   to "new" temp t
67bd0 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c  able */.  int ol
67be0 64 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f  dIdx;          /
67bf0 2a 20 49 6e 64 65 78 20 6f 66 20 76 64 62 65 20  * Index of vdbe 
67c00 63 75 72 73 6f 72 20 74 6f 20 22 6f 6c 64 22 20  cursor to "old" 
67c10 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20  temp table */.  
67c20 75 33 32 20 6e 65 77 43 6f 6c 4d 61 73 6b 3b 0a  u32 newColMask;.
67c30 20 20 75 33 32 20 6f 6c 64 43 6f 6c 4d 61 73 6b    u32 oldColMask
67c40 3b 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20  ;.  int orconf; 
67c50 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
67c60 65 6e 74 20 6f 72 63 6f 6e 66 20 70 6f 6c 69 63  ent orconf polic
67c70 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72  y */.  int ignor
67c80 65 4a 75 6d 70 3b 20 20 20 20 20 20 2f 2a 20 77  eJump;      /* w
67c90 68 65 72 65 20 74 6f 20 6a 75 6d 70 20 74 6f 20  here to jump to 
67ca0 66 6f 72 20 61 20 52 41 49 53 45 28 49 47 4e 4f  for a RAISE(IGNO
67cb0 52 45 29 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  RE) */.  Trigger
67cc0 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 2f 2a   *pTrigger;   /*
67cd0 20 54 68 65 20 74 72 69 67 67 65 72 20 63 75 72   The trigger cur
67ce0 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 64  rently being cod
67cf0 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53  ed */.  TriggerS
67d00 74 61 63 6b 20 2a 70 4e 65 78 74 3b 20 2f 2a 20  tack *pNext; /* 
67d10 4e 65 78 74 20 74 72 69 67 67 65 72 20 64 6f 77  Next trigger dow
67d20 6e 20 6f 6e 20 74 68 65 20 74 72 69 67 67 65 72  n on the trigger
67d30 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   stack */.};../*
67d40 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
67d50 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  g structure cont
67d60 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ains information
67d70 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c   used by the sql
67d80 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a 20 72 6f 75  iteFix....** rou
67d90 74 69 6e 65 73 20 61 73 20 74 68 65 79 20 77 61  tines as they wa
67da0 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74 72 65  lk the parse tre
67db0 65 20 74 6f 20 6d 61 6b 65 20 64 61 74 61 62 61  e to make databa
67dc0 73 65 20 72 65 66 65 72 65 6e 63 65 73 0a 2a 2a  se references.**
67dd0 20 65 78 70 6c 69 63 69 74 2e 20 20 0a 2a 2f 0a   explicit.  .*/.
67de0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
67df0 62 46 69 78 65 72 20 44 62 46 69 78 65 72 3b 0a  bFixer DbFixer;.
67e00 73 74 72 75 63 74 20 44 62 46 69 78 65 72 20 7b  struct DbFixer {
67e10 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
67e20 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61  ;      /* The pa
67e30 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20  rsing context.  
67e40 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77  Error messages w
67e50 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20  ritten here */. 
67e60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
67e70 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  ;    /* Make sur
67e80 65 20 61 6c 6c 20 6f 62 6a 65 63 74 73 20 61 72  e all objects ar
67e90 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  e contained in t
67ea0 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  his database */.
67eb0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
67ec0 79 70 65 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66  ype;  /* Type of
67ed0 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d   the container -
67ee0 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20   used for error 
67ef0 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 63 6f  messages */.  co
67f00 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  nst Token *pName
67f10 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65  ; /* Name of the
67f20 20 63 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65   container - use
67f30 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73  d for error mess
67f40 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ages */.};../*.*
67f50 2a 20 41 6e 20 6f 62 6a 65 63 74 65 64 20 75 73  * An objected us
67f60 65 64 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 65  ed to accumulate
67f70 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 73   the text of a s
67f80 74 72 69 6e 67 20 77 68 65 72 65 20 77 65 0a 2a  tring where we.*
67f90 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61  * do not necessa
67fa0 72 69 6c 79 20 6b 6e 6f 77 20 68 6f 77 20 62 69  rily know how bi
67fb0 67 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c  g the string wil
67fc0 6c 20 62 65 20 69 6e 20 74 68 65 20 65 6e 64 2e  l be in the end.
67fd0 0a 2a 2f 0a 73 74 72 75 63 74 20 53 74 72 41 63  .*/.struct StrAc
67fe0 63 75 6d 20 7b 0a 20 20 73 71 6c 69 74 65 33 20  cum {.  sqlite3 
67ff0 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  *db;         /* 
68000 4f 70 74 69 6f 6e 61 6c 20 64 61 74 61 62 61 73  Optional databas
68010 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 2e  e for lookaside.
68020 20 20 43 61 6e 20 62 65 20 4e 55 4c 4c 20 2a 2f    Can be NULL */
68030 0a 20 20 63 68 61 72 20 2a 7a 42 61 73 65 3b 20  .  char *zBase; 
68040 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 61 73          /* A bas
68050 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e  e allocation.  N
68060 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20  ot from malloc. 
68070 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 65 78 74  */.  char *zText
68080 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
68090 20 73 74 72 69 6e 67 20 63 6f 6c 6c 65 63 74 65   string collecte
680a0 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e  d so far */.  in
680b0 74 20 20 6e 43 68 61 72 3b 20 20 20 20 20 20 20  t  nChar;       
680c0 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
680d0 74 68 65 20 73 74 72 69 6e 67 20 73 6f 20 66 61  the string so fa
680e0 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 41 6c 6c  r */.  int  nAll
680f0 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  oc;         /* A
68100 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 61  mount of space a
68110 6c 6c 6f 63 61 74 65 64 20 69 6e 20 7a 54 65 78  llocated in zTex
68120 74 20 2a 2f 0a 20 20 69 6e 74 20 20 6d 78 41 6c  t */.  int  mxAl
68130 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  loc;        /* M
68140 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73  aximum allowed s
68150 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a  tring length */.
68160 20 20 75 38 20 20 20 6d 61 6c 6c 6f 63 46 61 69    u8   mallocFai
68170 6c 65 64 3b 20 20 20 2f 2a 20 42 65 63 6f 6d 65  led;   /* Become
68180 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 6d 65  s true if any me
68190 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
681a0 66 61 69 6c 73 20 2a 2f 0a 20 20 75 38 20 20 20  fails */.  u8   
681b0 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20  useMalloc;      
681c0 2f 2a 20 54 72 75 65 20 69 66 20 7a 54 65 78 74  /* True if zText
681d0 20 69 73 20 65 6e 6c 61 72 67 65 61 62 6c 65 20   is enlargeable 
681e0 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 20 2a 2f  using realloc */
681f0 0a 20 20 75 38 20 20 20 74 6f 6f 42 69 67 3b 20  .  u8   tooBig; 
68200 20 20 20 20 20 20 20 20 2f 2a 20 42 65 63 6f 6d          /* Becom
68210 65 73 20 74 72 75 65 20 69 66 20 73 74 72 69 6e  es true if strin
68220 67 20 73 69 7a 65 20 65 78 63 65 65 64 73 20 6c  g size exceeds l
68230 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  imits */.};../*.
68240 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
68250 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
68260 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e  s used to commun
68270 69 63 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  icate informatio
68280 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65  n.** from sqlite
68290 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f 50 61 72  3Init and OP_Par
682a0 73 65 53 63 68 65 6d 61 20 69 6e 74 6f 20 74 68  seSchema into th
682b0 65 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c  e sqlite3InitCal
682c0 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65  lback..*/.typede
682d0 66 20 73 74 72 75 63 74 20 7b 0a 20 20 73 71 6c  f struct {.  sql
682e0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
682f0 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
68300 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a   being initializ
68310 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  ed */.  int iDb;
68320 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30              /* 0
68330 20 66 6f 72 20 6d 61 69 6e 20 64 61 74 61 62 61   for main databa
68340 73 65 2e 20 20 31 20 66 6f 72 20 54 45 4d 50 2c  se.  1 for TEMP,
68350 20 32 2e 2e 20 66 6f 72 20 41 54 54 41 43 48 65   2.. for ATTACHe
68360 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  d */.  char **pz
68370 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a 20 45 72  ErrMsg;    /* Er
68380 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 6f 72  ror message stor
68390 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  ed here */.  int
683a0 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
683b0 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20   /* Result code 
683c0 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 7d  stored here */.}
683d0 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f 2a 0a 2a   InitData;../*.*
683e0 2a 20 53 74 72 75 63 74 75 72 65 20 63 6f 6e 74  * Structure cont
683f0 61 69 6e 69 6e 67 20 67 6c 6f 62 61 6c 20 63 6f  aining global co
68400 6e 66 69 67 75 72 61 74 69 6f 6e 20 64 61 74 61  nfiguration data
68410 20 66 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20   for the SQLite 
68420 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54  library..**.** T
68430 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6c  his structure al
68440 73 6f 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65  so contains some
68450 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69   state informati
68460 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 71  on..*/.struct Sq
68470 6c 69 74 65 33 43 6f 6e 66 69 67 20 7b 0a 20 20  lite3Config {.  
68480 69 6e 74 20 62 4d 65 6d 73 74 61 74 3b 20 20 20  int bMemstat;   
68490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
684a0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61    /* True to ena
684b0 62 6c 65 20 6d 65 6d 6f 72 79 20 73 74 61 74 75  ble memory statu
684c0 73 20 2a 2f 0a 20 20 69 6e 74 20 62 43 6f 72 65  s */.  int bCore
684d0 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20  Mutex;          
684e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
684f0 20 74 6f 20 65 6e 61 62 6c 65 20 63 6f 72 65 20   to enable core 
68500 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e  mutexing */.  in
68510 74 20 62 46 75 6c 6c 4d 75 74 65 78 3b 20 20 20  t bFullMutex;   
68520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68530 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c  /* True to enabl
68540 65 20 66 75 6c 6c 20 6d 75 74 65 78 69 6e 67 20  e full mutexing 
68550 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 74 72 6c 65  */.  int mxStrle
68560 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
68570 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
68580 6d 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20  m string length 
68590 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c 6f 6f 6b 61  */.  int szLooka
685a0 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  side;           
685b0 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c         /* Defaul
685c0 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66  t lookaside buff
685d0 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74  er size */.  int
685e0 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20   nLookaside;    
685f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
68600 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73  * Default lookas
68610 69 64 65 20 62 75 66 66 65 72 20 63 6f 75 6e 74  ide buffer count
68620 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65   */.  sqlite3_me
68630 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 20 20 20 20  m_methods m;    
68640 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c          /* Low-l
68650 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  evel memory allo
68660 63 61 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65  cation interface
68670 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
68680 74 65 78 5f 6d 65 74 68 6f 64 73 20 6d 75 74 65  tex_methods mute
68690 78 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c  x;      /* Low-l
686a0 65 76 65 6c 20 6d 75 74 65 78 20 69 6e 74 65 72  evel mutex inter
686b0 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  face */.  sqlite
686c0 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73  3_pcache_methods
686d0 20 70 63 61 63 68 65 3b 20 20 20 20 2f 2a 20 4c   pcache;    /* L
686e0 6f 77 2d 6c 65 76 65 6c 20 70 61 67 65 2d 63 61  ow-level page-ca
686f0 63 68 65 20 69 6e 74 65 72 66 61 63 65 20 2a 2f  che interface */
68700 0a 20 20 76 6f 69 64 20 2a 70 48 65 61 70 3b 20  .  void *pHeap; 
68710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68720 20 20 20 20 20 2f 2a 20 48 65 61 70 20 73 74 6f       /* Heap sto
68730 72 61 67 65 20 73 70 61 63 65 20 2a 2f 0a 20 20  rage space */.  
68740 69 6e 74 20 6e 48 65 61 70 3b 20 20 20 20 20 20  int nHeap;      
68750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68760 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 48 65    /* Size of pHe
68770 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 6e  ap[] */.  int mn
68780 52 65 71 2c 20 6d 78 52 65 71 3b 20 20 20 20 20  Req, mxReq;     
68790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
687a0 69 6e 20 61 6e 64 20 6d 61 78 20 68 65 61 70 20  in and max heap 
687b0 72 65 71 75 65 73 74 73 20 73 69 7a 65 73 20 2a  requests sizes *
687c0 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 72 61 74  /.  void *pScrat
687d0 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ch;             
687e0 20 20 20 20 20 20 2f 2a 20 53 63 72 61 74 63 68        /* Scratch
687f0 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74   memory */.  int
68800 20 73 7a 53 63 72 61 74 63 68 3b 20 20 20 20 20   szScratch;     
68810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
68820 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 73  * Size of each s
68830 63 72 61 74 63 68 20 62 75 66 66 65 72 20 2a 2f  cratch buffer */
68840 0a 20 20 69 6e 74 20 6e 53 63 72 61 74 63 68 3b  .  int nScratch;
68850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68860 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
68870 66 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72  f scratch buffer
68880 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 61  s */.  void *pPa
68890 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
688a0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
688b0 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 2a 2f   cache memory */
688c0 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20  .  int szPage;  
688d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
688e0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
688f0 65 61 63 68 20 70 61 67 65 20 69 6e 20 70 50 61  each page in pPa
68900 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 50  ge[] */.  int nP
68910 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
68920 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
68930 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
68940 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69  n pPage[] */.  i
68950 6e 74 20 6d 78 50 61 72 73 65 72 53 74 61 63 6b  nt mxParserStack
68960 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
68970 20 2f 2a 20 6d 61 78 69 6d 75 6d 20 64 65 70 74   /* maximum dept
68980 68 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 20  h of the parser 
68990 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 73  stack */.  int s
689a0 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65  haredCacheEnable
689b0 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d;           /* 
689c0 74 72 75 65 20 69 66 20 73 68 61 72 65 64 2d 63  true if shared-c
689d0 61 63 68 65 20 6d 6f 64 65 20 65 6e 61 62 6c 65  ache mode enable
689e0 64 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 61 62  d */.  /* The ab
689f0 6f 76 65 20 6d 69 67 68 74 20 62 65 20 69 6e 69  ove might be ini
68a00 74 69 61 6c 69 7a 65 64 20 74 6f 20 6e 6f 6e 2d  tialized to non-
68a10 7a 65 72 6f 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  zero.  The follo
68a20 77 69 6e 67 20 6e 65 65 64 20 74 6f 20 61 6c 77  wing need to alw
68a30 61 79 73 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c  ays.  ** initial
68a40 6c 79 20 62 65 20 7a 65 72 6f 2c 20 68 6f 77 65  ly be zero, howe
68a50 76 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ver. */.  int is
68a60 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  Init;           
68a70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
68a80 72 75 65 20 61 66 74 65 72 20 69 6e 69 74 69 61  rue after initia
68a90 6c 69 7a 61 74 69 6f 6e 20 68 61 73 20 66 69 6e  lization has fin
68aa0 69 73 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  ished */.  int i
68ab0 6e 50 72 6f 67 72 65 73 73 3b 20 20 20 20 20 20  nProgress;      
68ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
68ad0 54 72 75 65 20 77 68 69 6c 65 20 69 6e 69 74 69  True while initi
68ae0 61 6c 69 7a 61 74 69 6f 6e 20 69 6e 20 70 72 6f  alization in pro
68af0 67 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 69  gress */.  int i
68b00 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 20 20  sMallocInit;    
68b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
68b20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f  True after mallo
68b30 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  c is initialized
68b40 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
68b50 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b  tex *pInitMutex;
68b60 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78          /* Mutex
68b70 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33   used by sqlite3
68b80 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f  _initialize() */
68b90 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d  .  int nRefInitM
68ba0 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20  utex;           
68bb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
68bc0 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e 69 74  f users of pInit
68bd0 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  Mutex */.};../*.
68be0 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74  ** Context point
68bf0 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e 20 74  er passed down t
68c00 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 65 2d  hrough the tree-
68c10 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  walk..*/.struct 
68c20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28  Walker {.  int (
68c30 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 29 28  *xExprCallback)(
68c40 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b  Walker*, Expr*);
68c50 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b       /* Callback
68c60 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73   for expressions
68c70 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c   */.  int (*xSel
68c80 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c  ectCallback)(Wal
68c90 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20  ker*,Select*);  
68ca0 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20  /* Callback for 
68cb0 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 61 72  SELECTs */.  Par
68cc0 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
68cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68ce0 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
68cf0 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20   context.  */.  
68d00 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20  union {         
68d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68d20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
68d30 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c  ra data for call
68d40 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65  back */.    Name
68d50 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20  Context *pNC;   
68d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68d70 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67         /* Naming
68d80 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20   context */.    
68d90 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
68da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68db0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
68dc0 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20  teger value */. 
68dd0 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72   } u;.};../* For
68de0 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
68df0 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  s */.SQLITE_PRIV
68e00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
68e10 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c  alkExpr(Walker*,
68e20 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
68e30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
68e40 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28  te3WalkExprList(
68e50 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73  Walker*, ExprLis
68e60 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
68e70 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
68e80 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72  alkSelect(Walker
68e90 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
68ea0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
68eb0 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
68ec0 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53  tExpr(Walker*, S
68ed0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
68ee0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
68ef0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f  te3WalkSelectFro
68f00 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63  m(Walker*, Selec
68f10 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  t*);../*.** Retu
68f20 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65  rn code from the
68f30 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b   parse-tree walk
68f40 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61  ing primitives a
68f50 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c  nd their.** call
68f60 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  backs..*/.#defin
68f70 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20  e WRC_Continue  
68f80 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75    0   /* Continu
68f90 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c  e down into chil
68fa0 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  dren */.#define 
68fb0 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20  WRC_Prune       
68fc0 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c  1   /* Omit chil
68fd0 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75  dren but continu
68fe0 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e  e walking siblin
68ff0 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52  gs */.#define WR
69000 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 32 20  C_Abort       2 
69010 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65    /* Abandon the
69020 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f   tree walk */../
69030 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49  *.** Assuming zI
69040 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  n points to the 
69050 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20  first byte of a 
69060 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c  UTF-8 character,
69070 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20  .** advance zIn 
69080 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
69090 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
690a0 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61  e next UTF-8 cha
690b0 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69  racter..*/.#defi
690c0 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55  ne SQLITE_SKIP_U
690d0 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20  TF8(zIn) {      
690e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
690f0 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e    \.  if( (*(zIn
69100 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20  ++))>=0xc0 ){   
69110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
69120 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
69130 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20   while( (*zIn & 
69140 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a  0xc0)==0x80 ){ z
69150 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20  In++; }         
69160 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20      \.  }       
69170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
69180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
69190 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
691a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
691b0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20  TE_CORRUPT_BKPT 
691c0 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65 69 74  macro can be eit
691d0 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 20 28  her a constant (
691e0 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a  for production.*
691f0 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61 20 66  * builds) or a f
69200 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f  unction call (fo
69210 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20 20 49  r debugging).  I
69220 66 20 69 74 20 69 73 20 61 20 66 75 6e 63 74 69  f it is a functi
69230 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61  on call,.** it a
69240 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 61 74  llows the operat
69250 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72 65 61  or to set a brea
69260 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 70  kpoint at the sp
69270 6f 74 20 77 68 65 72 65 20 64 61 74 61 62 61 73  ot where databas
69280 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20  e.** corruption 
69290 69 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65  is first detecte
692a0 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  d..*/.#ifdef SQL
692b0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45  ITE_DEBUG.SQLITE
692c0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
692d0 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f  qlite3Corrupt(vo
692e0 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 53 51  id);.# define SQ
692f0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
69300 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74  T sqlite3Corrupt
69310 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  ().#else.# defin
69320 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  e SQLITE_CORRUPT
69330 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 4f 52  _BKPT SQLITE_COR
69340 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  RUPT.#endif../*.
69350 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
69360 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20 74 68   macros mimic th
69370 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61  e standard libra
69380 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 75  ry functions tou
69390 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73 70 61  pper(),.** isspa
693a0 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 29 2c  ce(), isalnum(),
693b0 20 69 73 64 69 67 69 74 28 29 20 61 6e 64 20 69   isdigit() and i
693c0 73 78 64 69 67 69 74 28 29 2c 20 72 65 73 70 65  sxdigit(), respe
693d0 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20  ctively. The.** 
693e0 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 73 20  sqlite versions 
693f0 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 41 53  only work for AS
69400 43 49 49 20 63 68 61 72 61 63 74 65 72 73 2c 20  CII characters, 
69410 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f  regardless of lo
69420 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  cale..*/.#ifdef 
69430 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20 64  SQLITE_ASCII.# d
69440 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 75  efine sqlite3Tou
69450 70 70 65 72 28 78 29 20 20 28 28 78 29 26 7e 28  pper(x)  ((x)&~(
69460 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
69470 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
69480 78 29 5d 26 30 78 32 30 29 29 0a 23 20 64 65 66  x)]&0x20)).# def
69490 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 61  ine sqlite3Isspa
694a0 63 65 28 78 29 20 20 20 28 73 71 6c 69 74 65 33  ce(x)   (sqlite3
694b0 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
694c0 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30  ed char)(x)]&0x0
694d0 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  1).# define sqli
694e0 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20  te3Isalnum(x)   
694f0 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70  (sqlite3CtypeMap
69500 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  [(unsigned char)
69510 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64 65 66  (x)]&0x06).# def
69520 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70  ine sqlite3Isalp
69530 68 61 28 78 29 20 20 20 28 73 71 6c 69 74 65 33  ha(x)   (sqlite3
69540 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
69550 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30  ed char)(x)]&0x0
69560 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  2).# define sqli
69570 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 20  te3Isdigit(x)   
69580 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70  (sqlite3CtypeMap
69590 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  [(unsigned char)
695a0 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64 65 66  (x)]&0x04).# def
695b0 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 64 69  ine sqlite3Isxdi
695c0 67 69 74 28 78 29 20 20 28 73 71 6c 69 74 65 33  git(x)  (sqlite3
695d0 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
695e0 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30  ed char)(x)]&0x0
695f0 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  8).# define sqli
69600 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 20 20  te3Tolower(x)   
69610 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c  (sqlite3UpperToL
69620 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63  ower[(unsigned c
69630 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73 65 0a  har)(x)]).#else.
69640 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 65  # include <ctype
69650 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  .h>.# define sql
69660 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20  ite3Toupper(x)  
69670 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69 67 6e   toupper((unsign
69680 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64  ed char)(x)).# d
69690 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73  efine sqlite3Iss
696a0 70 61 63 65 28 78 29 20 20 20 69 73 73 70 61 63  pace(x)   isspac
696b0 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  e((unsigned char
696c0 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73  )(x)).# define s
696d0 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29  qlite3Isalnum(x)
696e0 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e 73 69     isalnum((unsi
696f0 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23  gned char)(x)).#
69700 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
69710 73 61 6c 70 68 61 28 78 29 20 20 20 69 73 61 6c  salpha(x)   isal
69720 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20 63 68  pha((unsigned ch
69730 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65  ar)(x)).# define
69740 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
69750 78 29 20 20 20 69 73 64 69 67 69 74 28 28 75 6e  x)   isdigit((un
69760 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29  signed char)(x))
69770 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
69780 33 49 73 78 64 69 67 69 74 28 78 29 20 20 69 73  3Isxdigit(x)  is
69790 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e 65 64  xdigit((unsigned
697a0 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66   char)(x)).# def
697b0 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77  ine sqlite3Tolow
697c0 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 72 28  er(x)   tolower(
697d0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
697e0 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  x)).#endif../*.*
697f0 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74  * Internal funct
69800 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a  ion prototypes.*
69810 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
69820 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49   int sqlite3StrI
69830 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Cmp(const char *
69840 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b  , const char *);
69850 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
69860 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 4e 49  int sqlite3StrNI
69870 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Cmp(const char *
69880 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
69890 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
698a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
698b0 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63  IsNumber(const c
698c0 68 61 72 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b  har*, int*, u8);
698d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
698e0 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65  int sqlite3Strle
698f0 6e 33 30 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  n30(const char*)
69900 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
69910 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c  E int sqlite3Mal
69920 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53  locInit(void);.S
69930 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
69940 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  id sqlite3Malloc
69950 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  End(void);.SQLIT
69960 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
69970 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e  sqlite3Malloc(in
69980 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
69990 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
699a0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 29 3b  MallocZero(int);
699b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
699c0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d  void *sqlite3DbM
699d0 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65  allocZero(sqlite
699e0 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  3*, int);.SQLITE
699f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
69a00 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
69a10 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29  w(sqlite3*, int)
69a20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
69a30 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62   char *sqlite3Db
69a40 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  StrDup(sqlite3*,
69a50 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
69a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
69a70 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e  r *sqlite3DbStrN
69a80 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  Dup(sqlite3*,con
69a90 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a  st char*, int);.
69aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
69ab0 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c  oid *sqlite3Real
69ac0 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b  loc(void*, int);
69ad0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
69ae0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52  void *sqlite3DbR
69af0 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c  eallocOrFree(sql
69b00 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20  ite3 *, void *, 
69b10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
69b20 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
69b30 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69  e3DbRealloc(sqli
69b40 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69  te3 *, void *, i
69b50 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
69b60 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
69b70 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 2a 2c  DbFree(sqlite3*,
69b80 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
69b90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
69ba0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f  te3MallocSize(vo
69bb0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id*);.SQLITE_PRI
69bc0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
69bd0 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c  DbMallocSize(sql
69be0 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53  ite3*, void*);.S
69bf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
69c00 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 74  id *sqlite3Scrat
69c10 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53  chMalloc(int);.S
69c20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
69c30 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 63  id sqlite3Scratc
69c40 68 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51  hFree(void*);.SQ
69c50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
69c60 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61  d *sqlite3PageMa
69c70 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54  lloc(int);.SQLIT
69c80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
69c90 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76  qlite3PageFree(v
69ca0 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  oid*);.SQLITE_PR
69cb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
69cc0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
69cd0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
69ce0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
69cf0 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f  e3BenignMallocHo
69d00 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 76 6f 69  oks(void (*)(voi
69d10 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69  d), void (*)(voi
69d20 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d));.SQLITE_PRIV
69d30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
69d40 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f 69 64 20  emoryAlarm(void 
69d50 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74  (*)(void*, sqlit
69d60 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 29 2c 20  e3_int64, int), 
69d70 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69  void*, sqlite3_i
69d80 6e 74 36 34 29 3b 0a 0a 23 69 66 64 65 66 20 53  nt64);..#ifdef S
69d90 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
69da0 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52 49 56  SYS3.SQLITE_PRIV
69db0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
69dc0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73  3_mem_methods *s
69dd0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73  qlite3MemGetMems
69de0 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69  ys3(void);.#endi
69df0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
69e00 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 53  ENABLE_MEMSYS5.S
69e10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
69e20 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
69e30 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
69e40 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f  MemGetMemsys5(vo
69e50 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  id);.#endif...#i
69e60 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54  fndef SQLITE_MUT
69e70 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f 50  EX_OMIT.SQLITE_P
69e80 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 33  RIVATE   sqlite3
69e90 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a  _mutex_methods *
69ea0 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75  sqlite3DefaultMu
69eb0 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  tex(void);.SQLIT
69ec0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
69ed0 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74  te3_mutex *sqlit
69ee0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74  e3MutexAlloc(int
69ef0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
69f00 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  E   int sqlite3M
69f10 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a  utexInit(void);.
69f20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
69f30 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65   int sqlite3Mute
69f40 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64  xEnd(void);.#end
69f50 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
69f60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  TE int sqlite3St
69f70 61 74 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a  atusValue(int);.
69f80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
69f90 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
69fa0 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a  sAdd(int, int);.
69fb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
69fc0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
69fd0 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a  sSet(int, int);.
69fe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
69ff0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  int sqlite3IsNaN
6a000 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49 54  (double);..SQLIT
6a010 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6a020 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 53  qlite3VXPrintf(S
6a030 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20 63  trAccum*, int, c
6a040 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c  onst char*, va_l
6a050 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ist);.SQLITE_PRI
6a060 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
6a070 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65  e3MPrintf(sqlite
6a080 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  3*,const char*, 
6a090 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ...);.SQLITE_PRI
6a0a0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
6a0b0 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74  e3VMPrintf(sqlit
6a0c0 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
6a0d0 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54   va_list);.SQLIT
6a0e0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
6a0f0 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
6a100 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c 63  sqlite3*,char*,c
6a110 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
6a120 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
6a130 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66  ITE_TEST) || def
6a140 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
6a150 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  G).SQLITE_PRIVAT
6a160 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
6a170 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73  DebugPrintf(cons
6a180 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23  t char*, ...);.#
6a190 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65  endif.#if define
6a1a0 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53  d(SQLITE_TEST).S
6a1b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6a1c0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54 65 73  void *sqlite3Tes
6a1d0 74 54 65 78 74 54 6f 50 74 72 28 63 6f 6e 73 74  tTextToPtr(const
6a1e0 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a   char*);.#endif.
6a1f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6a200 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 74  oid sqlite3SetSt
6a210 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20 73 71  ring(char **, sq
6a220 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68  lite3*, const ch
6a230 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54  ar*, ...);.SQLIT
6a240 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6a250 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50  qlite3ErrorMsg(P
6a260 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  arse*, const cha
6a270 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45  r*, ...);.SQLITE
6a280 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6a290 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28  lite3ErrorClear(
6a2a0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
6a2b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6a2c0 74 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 2a  te3Dequote(char*
6a2d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6a2e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65 79  E int sqlite3Key
6a2f0 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 75  wordCode(const u
6a300 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69  nsigned char*, i
6a310 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6a320 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
6a330 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 2a 2c  unParser(Parse*,
6a340 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 68   const char*, ch
6a350 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ar **);.SQLITE_P
6a360 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6a370 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28  te3FinishCoding(
6a380 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
6a390 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6a3a0 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 61  te3GetTempReg(Pa
6a3b0 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  rse*);.SQLITE_PR
6a3c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6a3d0 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
6a3e0 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51  (Parse*,int);.SQ
6a3f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6a400 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
6a410 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 29  ange(Parse*,int)
6a420 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6a430 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c   void sqlite3Rel
6a440 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 50 61  easeTempRange(Pa
6a450 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  rse*,int,int);.S
6a460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
6a470 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28  pr *sqlite3Expr(
6a480 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 45  sqlite3*, int, E
6a490 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e  xpr*, Expr*, con
6a4a0 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  st Token*);.SQLI
6a4b0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
6a4c0 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 50 61  *sqlite3PExpr(Pa
6a4d0 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a  rse*, int, Expr*
6a4e0 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54  , Expr*, const T
6a4f0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6a500 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
6a510 69 74 65 33 52 65 67 69 73 74 65 72 45 78 70 72  ite3RegisterExpr
6a520 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b  (Parse*,Token*);
6a530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6a540 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
6a550 72 41 6e 64 28 73 71 6c 69 74 65 33 2a 2c 45 78  rAnd(sqlite3*,Ex
6a560 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  pr*, Expr*);.SQL
6a570 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6a580 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
6a590 28 45 78 70 72 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f  (Expr*,Token*,To
6a5a0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
6a5b0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
6a5c0 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28  te3ExprFunction(
6a5d0 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a  Parse*,ExprList*
6a5e0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
6a5f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6a600 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e  qlite3ExprAssign
6a610 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 2a  VarNumber(Parse*
6a620 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
6a630 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6a640 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73  lite3ExprClear(s
6a650 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b  qlite3*, Expr*);
6a660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6a670 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
6a680 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c  Delete(sqlite3*,
6a690 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
6a6a0 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74  PRIVATE ExprList
6a6b0 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73   *sqlite3ExprLis
6a6c0 74 41 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45  tAppend(Parse*,E
6a6d0 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 54  xprList*,Expr*,T
6a6e0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6a6f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6a700 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
6a710 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72  e(sqlite3*, Expr
6a720 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
6a730 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6a740 65 33 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c  e3Init(sqlite3*,
6a750 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45   char**);.SQLITE
6a760 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6a770 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b  ite3InitCallback
6a780 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61  (void*, int, cha
6a790 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51  r**, char**);.SQ
6a7a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6a7b0 64 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28  d sqlite3Pragma(
6a7c0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f  Parse*,Token*,To
6a7d0 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29  ken*,Token*,int)
6a7e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6a7f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73   void sqlite3Res
6a800 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
6a810 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b  (sqlite3*, int);
6a820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6a830 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
6a840 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c 69 6e  nParse(Parse*,in
6a850 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6a860 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
6a870 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61  ommitInternalCha
6a880 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a  nges(sqlite3*);.
6a890 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
6a8a0 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52 65 73  able *sqlite3Res
6a8b0 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 50  ultSetOfSelect(P
6a8c0 61 72 73 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a  arse*,Select*);.
6a8d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6a8e0 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d  oid sqlite3OpenM
6a8f0 61 73 74 65 72 54 61 62 6c 65 28 50 61 72 73 65  asterTable(Parse
6a900 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
6a910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6a920 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
6a930 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f  Parse*,Token*,To
6a940 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ken*,int,int,int
6a950 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
6a960 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6a970 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73  e3AddColumn(Pars
6a980 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  e*,Token*);.SQLI
6a990 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6a9a0 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c  sqlite3AddNotNul
6a9b0 6c 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  l(Parse*, int);.
6a9c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6a9d0 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 50 72  oid sqlite3AddPr
6a9e0 69 6d 61 72 79 4b 65 79 28 50 61 72 73 65 2a 2c  imaryKey(Parse*,
6a9f0 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c   ExprList*, int,
6aa00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
6aa10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6aa20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43  sqlite3AddCheckC
6aa30 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73 65 2a  onstraint(Parse*
6aa40 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
6aa50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6aa60 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79  lite3AddColumnTy
6aa70 70 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a  pe(Parse*,Token*
6aa80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6aa90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
6aaa0 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 50 61  dDefaultValue(Pa
6aab0 72 73 65 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c  rse*,Expr*);.SQL
6aac0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6aad0 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61   sqlite3AddColla
6aae0 74 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54  teType(Parse*, T
6aaf0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6ab00 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6ab10 74 65 33 45 6e 64 54 61 62 6c 65 28 50 61 72 73  te3EndTable(Pars
6ab20 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  e*,Token*,Token*
6ab30 2c 53 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49  ,Select*);..SQLI
6ab40 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76 65  TE_PRIVATE Bitve
6ab50 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63  c *sqlite3Bitvec
6ab60 43 72 65 61 74 65 28 75 33 32 29 3b 0a 53 51 4c  Create(u32);.SQL
6ab70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6ab80 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
6ab90 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b  t(Bitvec*, u32);
6aba0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6abb0 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65  int sqlite3Bitve
6abc0 63 53 65 74 28 42 69 74 76 65 63 2a 2c 20 75 33  cSet(Bitvec*, u3
6abd0 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  2);.SQLITE_PRIVA
6abe0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
6abf0 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 65  itvecClear(Bitve
6ac00 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45  c*, u32);.SQLITE
6ac10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6ac20 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
6ac30 6f 79 28 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c  oy(Bitvec*);.SQL
6ac40 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
6ac50 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a  sqlite3BitvecSiz
6ac60 65 28 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49  e(Bitvec*);.SQLI
6ac70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6ac80 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c  qlite3BitvecBuil
6ac90 74 69 6e 54 65 73 74 28 69 6e 74 2c 69 6e 74 2a  tinTest(int,int*
6aca0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
6acb0 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74  TE RowSet *sqlit
6acc0 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c  e3RowSetInit(sql
6acd0 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e  ite3*, void*, un
6ace0 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 53 51 4c  signed int);.SQL
6acf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6ad00 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c   sqlite3RowSetCl
6ad10 65 61 72 28 52 6f 77 53 65 74 2a 29 3b 0a 53 51  ear(RowSet*);.SQ
6ad20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6ad30 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49  d sqlite3RowSetI
6ad40 6e 73 65 72 74 28 52 6f 77 53 65 74 2a 2c 20 69  nsert(RowSet*, i
6ad50 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
6ad60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
6ad70 6f 77 53 65 74 54 65 73 74 28 52 6f 77 53 65 74  owSetTest(RowSet
6ad80 2a 2c 20 75 38 20 69 42 61 74 63 68 2c 20 69 36  *, u8 iBatch, i6
6ad90 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  4);.SQLITE_PRIVA
6ada0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f  TE int sqlite3Ro
6adb0 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 2a  wSetNext(RowSet*
6adc0 2c 20 69 36 34 2a 29 3b 0a 0a 53 51 4c 49 54 45  , i64*);..SQLITE
6add0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6ade0 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28  lite3CreateView(
6adf0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f  Parse*,Token*,To
6ae00 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65  ken*,Token*,Sele
6ae10 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23  ct*,int,int);..#
6ae20 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
6ae30 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c  TE_OMIT_VIEW) ||
6ae40 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
6ae50 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
6ae60 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  LE).SQLITE_PRIVA
6ae70 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
6ae80 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
6ae90 65 73 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a  es(Parse*,Table*
6aea0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
6aeb0 65 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  e sqlite3ViewGet
6aec0 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41 2c 42 29  ColumnNames(A,B)
6aed0 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54   0.#endif..SQLIT
6aee0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6aef0 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28  qlite3DropTable(
6af00 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
6af10 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
6af20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6af30 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
6af40 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c  ble(Table*);.SQL
6af50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6af60 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 50   sqlite3Insert(P
6af70 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c  arse*, SrcList*,
6af80 20 45 78 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65   ExprList*, Sele
6af90 63 74 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e  ct*, IdList*, in
6afa0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6afb0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
6afc0 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 73 71  ArrayAllocate(sq
6afd0 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74  lite3*,void*,int
6afe0 2c 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  ,int,int*,int*,i
6aff0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
6b000 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c  VATE IdList *sql
6b010 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64  ite3IdListAppend
6b020 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73  (sqlite3*, IdLis
6b030 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  t*, Token*);.SQL
6b040 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6b050 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64  sqlite3IdListInd
6b060 65 78 28 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74  ex(IdList*,const
6b070 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
6b080 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20  PRIVATE SrcList 
6b090 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45  *sqlite3SrcListE
6b0a0 6e 6c 61 72 67 65 28 73 71 6c 69 74 65 33 2a 2c  nlarge(sqlite3*,
6b0b0 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20   SrcList*, int, 
6b0c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6b0d0 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71  VATE SrcList *sq
6b0e0 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
6b0f0 6e 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63  nd(sqlite3*, Src
6b100 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54  List*, Token*, T
6b110 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6b120 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
6b130 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
6b140 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72  pendFromTerm(Par
6b150 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54  se*, SrcList*, T
6b160 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20  oken*, Token*,. 
6b170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b190 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c       Token*, Sel
6b1a0 65 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c  ect*, Expr*, IdL
6b1b0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
6b1c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6b1d0 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64  e3SrcListIndexed
6b1e0 42 79 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c  By(Parse *, SrcL
6b1f0 69 73 74 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b  ist *, Token *);
6b200 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6b210 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78  int sqlite3Index
6b220 65 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65  edByLookup(Parse
6b230 20 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69   *, struct SrcLi
6b240 73 74 5f 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49  st_item *);.SQLI
6b250 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6b260 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68  sqlite3SrcListSh
6b270 69 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c  iftJoinType(SrcL
6b280 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
6b290 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6b2a0 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43  e3SrcListAssignC
6b2b0 75 72 73 6f 72 73 28 50 61 72 73 65 2a 2c 20 53  ursors(Parse*, S
6b2c0 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
6b2d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6b2e0 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
6b2f0 65 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69  e(sqlite3*, IdLi
6b300 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
6b310 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6b320 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73  3SrcListDelete(s
6b330 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74  qlite3*, SrcList
6b340 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6b350 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
6b360 72 65 61 74 65 49 6e 64 65 78 28 50 61 72 73 65  reateIndex(Parse
6b370 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c  *,Token*,Token*,
6b380 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73  SrcList*,ExprLis
6b390 74 2a 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20  t*,int,Token*,. 
6b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b3b0 20 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69         Token*, i
6b3c0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
6b3d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6b3e0 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50  lite3DropIndex(P
6b3f0 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c  arse*, SrcList*,
6b400 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6b410 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6b420 33 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20  3Select(Parse*, 
6b430 53 65 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44  Select*, SelectD
6b440 65 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  est*);.SQLITE_PR
6b450 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71  IVATE Select *sq
6b460 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50  lite3SelectNew(P
6b470 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c  arse*,ExprList*,
6b480 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45  SrcList*,Expr*,E
6b490 78 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20  xprList*,.      
6b4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b4b0 20 20 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73     Expr*,ExprLis
6b4c0 74 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70  t*,int,Expr*,Exp
6b4d0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6b4e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6b4f0 53 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c  SelectDelete(sql
6b500 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b  ite3*, Select*);
6b510 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6b520 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72  Table *sqlite3Sr
6b530 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73  cListLookup(Pars
6b540 65 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53  e*, SrcList*);.S
6b550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6b560 74 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f  t sqlite3IsReadO
6b570 6e 6c 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  nly(Parse*, Tabl
6b580 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  e*, int);.SQLITE
6b590 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6b5a0 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50  lite3OpenTable(P
6b5b0 61 72 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c  arse*, int iCur,
6b5c0 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a   int iDb, Table*
6b5d0 2c 20 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69  , int);.#if defi
6b5e0 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
6b5f0 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f  E_UPDATE_DELETE_
6b600 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e  LIMIT) && !defin
6b610 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
6b620 55 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f  UBQUERY).SQLITE_
6b630 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
6b640 6c 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28  lite3LimitWhere(
6b650 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74  Parse *, SrcList
6b660 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72   *, Expr *, Expr
6b670 4c 69 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20  List *, Expr *, 
6b680 45 78 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b  Expr *, char *);
6b690 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50  .#endif.SQLITE_P
6b6a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6b6b0 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61  te3DeleteFrom(Pa
6b6c0 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
6b6d0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
6b6e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6b6f0 74 65 33 55 70 64 61 74 65 28 50 61 72 73 65 2a  te3Update(Parse*
6b700 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72  , SrcList*, Expr
6b710 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  List*, Expr*, in
6b720 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6b730 54 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71  TE WhereInfo *sq
6b740 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
6b750 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
6b760 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 4c 69 73  , Expr*, ExprLis
6b770 74 2a 2a 2c 20 75 31 36 29 3b 0a 53 51 4c 49 54  t**, u16);.SQLIT
6b780 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6b790 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 57  qlite3WhereEnd(W
6b7a0 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49  hereInfo*);.SQLI
6b7b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6b7c0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65  qlite3ExprCodeGe
6b7d0 74 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20  tColumn(Parse*, 
6b7e0 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Table*, int, int
6b7f0 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
6b800 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6b810 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
6b820 4d 6f 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74  Move(Parse*, int
6b830 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
6b840 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6b850 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
6b860 43 6f 70 79 28 50 61 72 73 65 2a 2c 20 69 6e 74  Copy(Parse*, int
6b870 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
6b880 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6b890 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
6b8a0 65 53 74 6f 72 65 28 50 61 72 73 65 2a 2c 20 69  eStore(Parse*, i
6b8b0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  nt, int, int);.S
6b8c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6b8d0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
6b8e0 63 68 65 50 75 73 68 28 50 61 72 73 65 2a 29 3b  chePush(Parse*);
6b8f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6b900 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
6b910 43 61 63 68 65 50 6f 70 28 50 61 72 73 65 2a 2c  CachePop(Parse*,
6b920 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6b930 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6b940 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76  e3ExprCacheRemov
6b950 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  e(Parse*, int);.
6b960 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6b970 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
6b980 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 2a  acheClear(Parse*
6b990 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6b9a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
6b9b0 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
6b9c0 68 61 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e  hange(Parse*, in
6b9d0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6b9e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6b9f0 69 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79  ite3ExprHardCopy
6ba00 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29  (Parse*,int,int)
6ba10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ba20 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
6ba30 43 6f 64 65 28 50 61 72 73 65 2a 2c 20 45 78 70  Code(Parse*, Exp
6ba40 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
6ba50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6ba60 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
6ba70 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
6ba80 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
6ba90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6baa0 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
6bab0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69  Parse*, Expr*, i
6bac0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6bad0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
6bae0 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28  xprCodeAndCache(
6baf0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69  Parse*, Expr*, i
6bb00 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6bb10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6bb20 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74  ExprCodeConstant
6bb30 73 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29  s(Parse*, Expr*)
6bb40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6bb50 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
6bb60 43 6f 64 65 45 78 70 72 4c 69 73 74 28 50 61 72  CodeExprList(Par
6bb70 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  se*, ExprList*, 
6bb80 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
6bb90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6bba0 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65  qlite3ExprIfTrue
6bbb0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
6bbc0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
6bbd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6bbe0 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
6bbf0 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c  e(Parse*, Expr*,
6bc00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
6bc10 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65  TE_PRIVATE Table
6bc20 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62   *sqlite3FindTab
6bc30 6c 65 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  le(sqlite3*,cons
6bc40 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63  t char*, const c
6bc50 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
6bc60 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
6bc70 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
6bc80 50 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65  Parse*,int isVie
6bc90 77 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63  w,const char*, c
6bca0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
6bcb0 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65  ITE_PRIVATE Inde
6bcc0 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e  x *sqlite3FindIn
6bcd0 64 65 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  dex(sqlite3*,con
6bce0 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20  st char*, const 
6bcf0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
6bd00 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6bd10 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
6bd20 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a  teTable(sqlite3*
6bd30 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
6bd40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6bd50 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e  E void sqlite3Un
6bd60 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64  linkAndDeleteInd
6bd70 65 78 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  ex(sqlite3*,int,
6bd80 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
6bd90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6bda0 64 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28  d sqlite3Vacuum(
6bdb0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
6bdc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6bdd0 74 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61  te3RunVacuum(cha
6bde0 72 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a  r**, sqlite3*);.
6bdf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
6be00 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65  har *sqlite3Name
6be10 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65  FromToken(sqlite
6be20 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  3*, Token*);.SQL
6be30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6be40 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
6be50 72 65 28 45 78 70 72 2a 2c 20 45 78 70 72 2a 29  re(Expr*, Expr*)
6be60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6be70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
6be80 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
6be90 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c  es(NameContext*,
6bea0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
6beb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6bec0 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
6bed0 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65  ggList(NameConte
6bee0 78 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a  xt*,ExprList*);.
6bef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
6bf00 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56  dbe *sqlite3GetV
6bf10 64 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  dbe(Parse*);.SQL
6bf20 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
6bf30 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49   *sqlite3CreateI
6bf40 64 45 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63  dExpr(Parse *, c
6bf50 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
6bf60 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6bf70 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65   sqlite3PrngSave
6bf80 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c  State(void);.SQL
6bf90 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6bfa0 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74   sqlite3PrngRest
6bfb0 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a  oreState(void);.
6bfc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6bfd0 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52  oid sqlite3PrngR
6bfe0 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 3b  esetState(void);
6bff0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6c000 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c  void sqlite3Roll
6c010 62 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a  backAll(sqlite3*
6c020 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6c030 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
6c040 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50  deVerifySchema(P
6c050 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  arse*, int);.SQL
6c060 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6c070 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61   sqlite3BeginTra
6c080 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c  nsaction(Parse*,
6c090 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6c0a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6c0b0 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74  e3CommitTransact
6c0c0 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ion(Parse*);.SQL
6c0d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6c0e0 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
6c0f0 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73  Transaction(Pars
6c100 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
6c110 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6c120 53 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65 2a  Savepoint(Parse*
6c130 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  , int, Token*);.
6c140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6c150 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65  oid sqlite3Close
6c160 53 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 74  Savepoints(sqlit
6c170 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  e3 *);.SQLITE_PR
6c180 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6c190 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28  3ExprIsConstant(
6c1a0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
6c1b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6c1c0 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
6c1d0 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a  NotJoin(Expr*);.
6c1e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6c1f0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  nt sqlite3ExprIs
6c200 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69  ConstantOrFuncti
6c210 6f 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  on(Expr*);.SQLIT
6c220 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6c230 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
6c240 65 72 28 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b  er(Expr*, int*);
6c250 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6c260 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77  int sqlite3IsRow
6c270 69 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  id(const char*);
6c280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6c290 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65  void sqlite3Gene
6c2a0 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 50 61  rateRowDelete(Pa
6c2b0 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e  rse*, Table*, in
6c2c0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
6c2d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6c2e0 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
6c2f0 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28  eRowIndexDelete(
6c300 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
6c310 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  int, int*);.SQLI
6c320 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6c330 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e  qlite3GenerateIn
6c340 64 65 78 4b 65 79 28 50 61 72 73 65 2a 2c 20 49  dexKey(Parse*, I
6c350 6e 64 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  ndex*, int, int,
6c360 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6c370 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6c380 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
6c390 61 69 6e 74 43 68 65 63 6b 73 28 50 61 72 73 65  aintChecks(Parse
6c3a0 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74  *,Table*,int,int
6c3b0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
6c3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c3d0 20 20 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c         int*,int,
6c3e0 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a  int,int,int,int*
6c3f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6c400 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
6c410 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28  mpleteInsertion(
6c420 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
6c430 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20  int, int, int*, 
6c440 69 6e 74 2c 20 69 6e 74 2c 69 6e 74 2c 69 6e 74  int, int,int,int
6c450 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6c460 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65  E int sqlite3Ope
6c470 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73  nTableAndIndices
6c480 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
6c490 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
6c4a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6c4b0 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
6c4c0 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65  eOperation(Parse
6c4d0 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
6c4e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
6c4f0 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75  r *sqlite3ExprDu
6c500 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a  p(sqlite3*,Expr*
6c510 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
6c520 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6c530 65 33 54 6f 6b 65 6e 43 6f 70 79 28 73 71 6c 69  e3TokenCopy(sqli
6c540 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 63 6f 6e 73  te3*,Token*,cons
6c550 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  t Token*);.SQLIT
6c560 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69  E_PRIVATE ExprLi
6c570 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c  st *sqlite3ExprL
6c580 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  istDup(sqlite3*,
6c590 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a  ExprList*,int);.
6c5a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
6c5b0 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
6c5c0 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  rcListDup(sqlite
6c5d0 33 2a 2c 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29  3*,SrcList*,int)
6c5e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6c5f0 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
6c600 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  IdListDup(sqlite
6c610 33 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c  3*,IdList*);.SQL
6c620 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65  ITE_PRIVATE Sele
6c630 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63  ct *sqlite3Selec
6c640 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65  tDup(sqlite3*,Se
6c650 6c 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  lect*,int);.SQLI
6c660 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6c670 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e  sqlite3FuncDefIn
6c680 73 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68  sert(FuncDefHash
6c690 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51  *, FuncDef*);.SQ
6c6a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e  LITE_PRIVATE Fun
6c6b0 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e  cDef *sqlite3Fin
6c6c0 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  dFunction(sqlite
6c6d0 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
6c6e0 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a  nt,int,u8,int);.
6c6f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6c700 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
6c710 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69  terBuiltinFuncti
6c720 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  ons(sqlite3*);.S
6c730 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6c740 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  id sqlite3Regist
6c750 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69  erDateTimeFuncti
6c760 6f 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ons(void);.SQLIT
6c770 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6c780 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c  qlite3RegisterGl
6c790 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f  obalFunctions(vo
6c7a0 69 64 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  id);.#ifdef SQLI
6c7b0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
6c7c0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
6c7d0 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71  lite3SafetyOn(sq
6c7e0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
6c7f0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
6c800 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73  lite3SafetyOff(s
6c810 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a  qlite3*);.#else.
6c820 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6c830 53 61 66 65 74 79 4f 6e 28 41 29 20 30 0a 23 20  SafetyOn(A) 0.# 
6c840 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61  define sqlite3Sa
6c850 66 65 74 79 4f 66 66 28 41 29 20 30 0a 23 65 6e  fetyOff(A) 0.#en
6c860 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
6c870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61  TE int sqlite3Sa
6c880 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69  fetyCheckOk(sqli
6c890 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
6c8a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6c8b0 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
6c8c0 4f 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a  OrOk(sqlite3*);.
6c8d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6c8e0 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67  oid sqlite3Chang
6c8f0 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20  eCookie(Parse*, 
6c900 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69  int);..#if !defi
6c910 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
6c920 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65  VIEW) && !define
6c930 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
6c940 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52  IGGER).SQLITE_PR
6c950 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6c960 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65  e3MaterializeVie
6c970 77 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  w(Parse*, Table*
6c980 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23  , Expr*, int);.#
6c990 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
6c9a0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
6c9b0 45 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ER.SQLITE_PRIVAT
6c9c0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
6c9d0 42 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72  BeginTrigger(Par
6c9e0 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65  se*, Token*,Toke
6c9f0 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73  n*,int,int,IdLis
6ca00 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20  t*,SrcList*,.   
6ca10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ca20 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e          Expr*,in
6ca30 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6ca40 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
6ca50 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
6ca60 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67  ger(Parse*, Trig
6ca70 67 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a  gerStep*, Token*
6ca80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6ca90 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
6caa0 44 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73  DropTrigger(Pars
6cab0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e  e*, SrcList*, in
6cac0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6cad0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
6cae0 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28  3DropTriggerPtr(
6caf0 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a  Parse*, Trigger*
6cb00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6cb10 45 20 20 20 54 72 69 67 67 65 72 20 2a 73 71 6c  E   Trigger *sql
6cb20 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
6cb30 74 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65  t(Parse *, Table
6cb40 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74  *, int, ExprList
6cb50 2a 2c 20 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a  *, int *pMask);.
6cb60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6cb70 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65   Trigger *sqlite
6cb80 33 54 72 69 67 67 65 72 4c 69 73 74 28 50 61 72  3TriggerList(Par
6cb90 73 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a  se *, Table *);.
6cba0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6cbb0 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65   int sqlite3Code
6cbc0 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73 65  RowTrigger(Parse
6cbd0 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e  *, Trigger *, in
6cbe0 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  t, ExprList*, in
6cbf0 74 2c 20 54 61 62 6c 65 20 2a 2c 0a 20 20 20 20  t, Table *,.    
6cc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cc10 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74          int, int
6cc20 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 33 32 2a  , int, int, u32*
6cc30 2c 20 75 33 32 2a 29 3b 0a 20 20 76 6f 69 64 20  , u32*);.  void 
6cc40 73 71 6c 69 74 65 56 69 65 77 54 72 69 67 67 65  sqliteViewTrigge
6cc50 72 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65  rs(Parse*, Table
6cc60 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45  *, Expr*, int, E
6cc70 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  xprList*);.SQLIT
6cc80 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
6cc90 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
6cca0 69 67 67 65 72 53 74 65 70 28 73 71 6c 69 74 65  iggerStep(sqlite
6ccb0 33 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a  3*, TriggerStep*
6ccc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6ccd0 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20  E   TriggerStep 
6cce0 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53  *sqlite3TriggerS
6ccf0 65 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65  electStep(sqlite
6cd00 33 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  3*,Select*);.SQL
6cd10 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72  ITE_PRIVATE   Tr
6cd20 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74  iggerStep *sqlit
6cd30 65 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53  e3TriggerInsertS
6cd40 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b  tep(sqlite3*,Tok
6cd50 65 6e 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20  en*, IdList*,.  
6cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cd80 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c        ExprList*,
6cd90 53 65 6c 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51  Select*,int);.SQ
6cda0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54  LITE_PRIVATE   T
6cdb0 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69  riggerStep *sqli
6cdc0 74 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65  te3TriggerUpdate
6cdd0 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f  Step(sqlite3*,To
6cde0 6b 65 6e 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20  ken*,ExprList*, 
6cdf0 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  Expr*, int);.SQL
6ce00 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72  ITE_PRIVATE   Tr
6ce10 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74  iggerStep *sqlit
6ce20 65 33 54 72 69 67 67 65 72 44 65 6c 65 74 65 53  e3TriggerDeleteS
6ce30 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b  tep(sqlite3*,Tok
6ce40 65 6e 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  en*, Expr*);.SQL
6ce50 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
6ce60 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
6ce70 54 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a  Trigger(sqlite3*
6ce80 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c  , Trigger*);.SQL
6ce90 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
6cea0 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  id sqlite3Unlink
6ceb0 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72  AndDeleteTrigger
6cec0 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f  (sqlite3*,int,co
6ced0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6c 73  nst char*);.#els
6cee0 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  e.# define sqlit
6cef0 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28  e3TriggersExist(
6cf00 42 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64  B,C,D,E,F) 0.# d
6cf10 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 6c  efine sqlite3Del
6cf20 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 29 0a  eteTrigger(A,B).
6cf30 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6cf40 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 41  DropTriggerPtr(A
6cf50 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ,B).# define sql
6cf60 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c  ite3UnlinkAndDel
6cf70 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c 43  eteTrigger(A,B,C
6cf80 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
6cf90 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
6cfa0 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48  (A,B,C,D,E,F,G,H
6cfb0 2c 49 2c 4a 2c 4b 2c 4c 29 20 30 0a 23 20 64 65  ,I,J,K,L) 0.# de
6cfc0 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67  fine sqlite3Trig
6cfd0 67 65 72 4c 69 73 74 28 58 2c 20 59 29 20 30 0a  gerList(X, Y) 0.
6cfe0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
6cff0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6d000 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65  e3JoinType(Parse
6d010 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
6d020 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
6d030 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6d040 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72  sqlite3CreateFor
6d050 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20  eignKey(Parse*, 
6d060 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e  ExprList*, Token
6d070 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
6d080 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6d090 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
6d0a0 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50  eferForeignKey(P
6d0b0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66  arse*, int);.#if
6d0c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
6d0d0 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 53  _AUTHORIZATION.S
6d0e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6d0f0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68  void sqlite3Auth
6d100 52 65 61 64 28 50 61 72 73 65 2a 2c 45 78 70 72  Read(Parse*,Expr
6d110 2a 2c 53 63 68 65 6d 61 2a 2c 53 72 63 4c 69 73  *,Schema*,SrcLis
6d120 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
6d130 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
6d140 33 41 75 74 68 43 68 65 63 6b 28 50 61 72 73 65  3AuthCheck(Parse
6d150 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  *,int, const cha
6d160 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  r*, const char*,
6d170 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53   const char*);.S
6d180 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6d190 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68  void sqlite3Auth
6d1a0 43 6f 6e 74 65 78 74 50 75 73 68 28 50 61 72 73  ContextPush(Pars
6d1b0 65 2a 2c 20 41 75 74 68 43 6f 6e 74 65 78 74 2a  e*, AuthContext*
6d1c0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  , const char*);.
6d1d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6d1e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
6d1f0 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68  hContextPop(Auth
6d200 43 6f 6e 74 65 78 74 2a 29 3b 0a 23 65 6c 73 65  Context*);.#else
6d210 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6d220 33 41 75 74 68 52 65 61 64 28 61 2c 62 2c 63 2c  3AuthRead(a,b,c,
6d230 64 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  d).# define sqli
6d240 74 65 33 41 75 74 68 43 68 65 63 6b 28 61 2c 62  te3AuthCheck(a,b
6d250 2c 63 2c 64 2c 65 29 20 20 20 20 53 51 4c 49 54  ,c,d,e)    SQLIT
6d260 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71  E_OK.# define sq
6d270 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
6d280 50 75 73 68 28 61 2c 62 2c 63 29 0a 23 20 64 65  Push(a,b,c).# de
6d290 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68  fine sqlite3Auth
6d2a0 43 6f 6e 74 65 78 74 50 6f 70 28 61 29 20 20 28  ContextPop(a)  (
6d2b0 28 76 6f 69 64 29 28 61 29 29 0a 23 65 6e 64 69  (void)(a)).#endi
6d2c0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
6d2d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74   void sqlite3Att
6d2e0 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72  ach(Parse*, Expr
6d2f0 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 29  *, Expr*, Expr*)
6d300 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6d310 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74   void sqlite3Det
6d320 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72  ach(Parse*, Expr
6d330 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6d340 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
6d350 72 65 65 46 61 63 74 6f 72 79 28 63 6f 6e 73 74  reeFactory(const
6d360 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f   sqlite3 *db, co
6d370 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
6d380 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ame,.           
6d390 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
6d3a0 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74  omitJournal, int
6d3b0 20 6e 43 61 63 68 65 2c 20 69 6e 74 20 66 6c 61   nCache, int fla
6d3c0 67 73 2c 20 42 74 72 65 65 20 2a 2a 70 70 42 74  gs, Btree **ppBt
6d3d0 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ree);.SQLITE_PRI
6d3e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6d3f0 46 69 78 49 6e 69 74 28 44 62 46 69 78 65 72 2a  FixInit(DbFixer*
6d400 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63  , Parse*, int, c
6d410 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
6d420 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  t Token*);.SQLIT
6d430 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6d440 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28  lite3FixSrcList(
6d450 44 62 46 69 78 65 72 2a 2c 20 53 72 63 4c 69 73  DbFixer*, SrcLis
6d460 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
6d470 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
6d480 69 78 53 65 6c 65 63 74 28 44 62 46 69 78 65 72  ixSelect(DbFixer
6d490 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
6d4a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6d4b0 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 44  sqlite3FixExpr(D
6d4c0 62 46 69 78 65 72 2a 2c 20 45 78 70 72 2a 29 3b  bFixer*, Expr*);
6d4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6d4e0 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78  int sqlite3FixEx
6d4f0 70 72 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c  prList(DbFixer*,
6d500 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c   ExprList*);.SQL
6d510 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6d520 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65  sqlite3FixTrigge
6d530 72 53 74 65 70 28 44 62 46 69 78 65 72 2a 2c 20  rStep(DbFixer*, 
6d540 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53  TriggerStep*);.S
6d550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6d560 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63 6f  t sqlite3AtoF(co
6d570 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75  nst char *z, dou
6d580 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ble*);.SQLITE_PR
6d590 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6d5a0 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20  3GetInt32(const 
6d5b0 63 68 61 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53  char *, int*);.S
6d5c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6d5d0 74 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36  t sqlite3FitsIn6
6d5e0 34 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72  4Bits(const char
6d5f0 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
6d600 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6d610 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e  ite3Utf16ByteLen
6d620 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61  (const void *pDa
6d630 74 61 2c 20 69 6e 74 20 6e 43 68 61 72 29 3b 0a  ta, int nChar);.
6d640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6d650 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 43 68  nt sqlite3Utf8Ch
6d660 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61 72  arLen(const char
6d670 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 42 79   *pData, int nBy
6d680 74 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  te);.SQLITE_PRIV
6d690 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55  ATE int sqlite3U
6d6a0 74 66 38 52 65 61 64 28 63 6f 6e 73 74 20 75 38  tf8Read(const u8
6d6b0 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a  *, const u8**);.
6d6c0 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20  ./*.** Routines 
6d6d0 74 6f 20 72 65 61 64 20 61 6e 64 20 77 72 69 74  to read and writ
6d6e0 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  e variable-lengt
6d6f0 68 20 69 6e 74 65 67 65 72 73 2e 20 20 54 68 65  h integers.  The
6d700 73 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65  se used to.** be
6d710 20 64 65 66 69 6e 65 64 20 6c 6f 63 61 6c 6c 79   defined locally
6d720 2c 20 62 75 74 20 6e 6f 77 20 77 65 20 75 73 65  , but now we use
6d730 20 74 68 65 20 76 61 72 69 6e 74 20 72 6f 75 74   the varint rout
6d740 69 6e 65 73 20 69 6e 20 74 68 65 20 75 74 69 6c  ines in the util
6d750 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20 20 43 6f 64  .c.** file.  Cod
6d760 65 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65  e should use the
6d770 20 4d 41 43 52 4f 20 66 6f 72 6d 73 20 62 65 6c   MACRO forms bel
6d780 6f 77 2c 20 61 73 20 74 68 65 20 56 61 72 69 6e  ow, as the Varin
6d790 74 33 32 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20  t32 versions.** 
6d7a0 61 72 65 20 63 6f 64 65 64 20 74 6f 20 61 73 73  are coded to ass
6d7b0 75 6d 65 20 74 68 65 20 73 69 6e 67 6c 65 20 62  ume the single b
6d7c0 79 74 65 20 63 61 73 65 20 69 73 20 61 6c 72 65  yte case is alre
6d7d0 61 64 79 20 68 61 6e 64 6c 65 64 20 28 77 68 69  ady handled (whi
6d7e0 63 68 20 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f  ch .** the MACRO
6d7f0 20 66 6f 72 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a   form does)..*/.
6d800 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6d810 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72  nt sqlite3PutVar
6d820 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61  int(unsigned cha
6d830 72 2a 2c 20 75 36 34 29 3b 0a 53 51 4c 49 54 45  r*, u64);.SQLITE
6d840 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6d850 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28  ite3PutVarint32(
6d860 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20  unsigned char*, 
6d870 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u32);.SQLITE_PRI
6d880 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47  VATE u8 sqlite3G
6d890 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 75  etVarint(const u
6d8a0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20  nsigned char *, 
6d8b0 75 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  u64 *);.SQLITE_P
6d8c0 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65  RIVATE u8 sqlite
6d8d0 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e  3GetVarint32(con
6d8e0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
6d8f0 20 2a 2c 20 75 33 32 20 2a 29 3b 0a 53 51 4c 49   *, u32 *);.SQLI
6d900 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6d910 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
6d920 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54  u64 v);../*.** T
6d930 68 65 20 68 65 61 64 65 72 20 6f 66 20 61 20 72  he header of a r
6d940 65 63 6f 72 64 20 63 6f 6e 73 69 73 74 73 20 6f  ecord consists o
6d950 66 20 61 20 73 65 71 75 65 6e 63 65 20 76 61 72  f a sequence var
6d960 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
6d970 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20  egers..** These 
6d980 69 6e 74 65 67 65 72 73 20 61 72 65 20 61 6c 6d  integers are alm
6d990 6f 73 74 20 61 6c 77 61 79 73 20 73 6d 61 6c 6c  ost always small
6d9a0 20 61 6e 64 20 61 72 65 20 65 6e 63 6f 64 65 64   and are encoded
6d9b0 20 61 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74   as a single byt
6d9c0 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  e..** The follow
6d9d0 69 6e 67 20 6d 61 63 72 6f 73 20 74 61 6b 65 20  ing macros take 
6d9e0 61 64 76 61 6e 74 61 67 65 20 74 68 69 73 20 66  advantage this f
6d9f0 61 63 74 20 74 6f 20 70 72 6f 76 69 64 65 20 61  act to provide a
6da00 20 66 61 73 74 20 65 6e 63 6f 64 65 0a 2a 2a 20   fast encode.** 
6da10 61 6e 64 20 64 65 63 6f 64 65 20 6f 66 20 74 68  and decode of th
6da20 65 20 69 6e 74 65 67 65 72 73 20 69 6e 20 61 20  e integers in a 
6da30 72 65 63 6f 72 64 20 68 65 61 64 65 72 2e 20 20  record header.  
6da40 49 74 20 69 73 20 66 61 73 74 65 72 20 66 6f 72  It is faster for
6da50 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63   the common.** c
6da60 61 73 65 20 77 68 65 72 65 20 74 68 65 20 69 6e  ase where the in
6da70 74 65 67 65 72 20 69 73 20 61 20 73 69 6e 67 6c  teger is a singl
6da80 65 20 62 79 74 65 2e 20 20 49 74 20 69 73 20 61  e byte.  It is a
6da90 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 20 77   little slower w
6daa0 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67  hen the.** integ
6dab0 65 72 20 69 73 20 74 77 6f 20 6f 72 20 6d 6f 72  er is two or mor
6dac0 65 20 62 79 74 65 73 2e 20 20 42 75 74 20 6f 76  e bytes.  But ov
6dad0 65 72 61 6c 6c 20 69 74 20 69 73 20 66 61 73 74  erall it is fast
6dae0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  er..**.** The fo
6daf0 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 73 69  llowing expressi
6db00 6f 6e 73 20 61 72 65 20 65 71 75 69 76 61 6c 65  ons are equivale
6db10 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20  nt:.**.**     x 
6db20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  = sqlite3GetVari
6db30 6e 74 33 32 28 20 41 2c 20 26 42 20 29 3b 0a 2a  nt32( A, &B );.*
6db40 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65  *     x = sqlite
6db50 33 50 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c  3PutVarint32( A,
6db60 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20   B );.**.**     
6db70 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28  x = getVarint32(
6db80 20 41 2c 20 42 20 29 3b 0a 2a 2a 20 20 20 20 20   A, B );.**     
6db90 78 20 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28  x = putVarint32(
6dba0 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23   A, B );.**.*/.#
6dbb0 64 65 66 69 6e 65 20 67 65 74 56 61 72 69 6e 74  define getVarint
6dbc0 33 32 28 41 2c 42 29 20 20 28 75 38 29 28 28 2a  32(A,B)  (u8)((*
6dbd0 28 41 29 3c 28 75 38 29 30 78 38 30 29 20 3f 20  (A)<(u8)0x80) ? 
6dbe0 28 28 42 29 20 3d 20 28 75 33 32 29 2a 28 41 29  ((B) = (u32)*(A)
6dbf0 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 47 65 74  ),1 : sqlite3Get
6dc00 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28 75  Varint32((A), (u
6dc10 33 32 20 2a 29 26 28 42 29 29 29 0a 23 64 65 66  32 *)&(B))).#def
6dc20 69 6e 65 20 70 75 74 56 61 72 69 6e 74 33 32 28  ine putVarint32(
6dc30 41 2c 42 29 20 20 28 75 38 29 28 28 28 75 33 32  A,B)  (u8)(((u32
6dc40 29 28 42 29 3c 28 75 33 32 29 30 78 38 30 29 20  )(B)<(u32)0x80) 
6dc50 3f 20 28 2a 28 41 29 20 3d 20 28 75 6e 73 69 67  ? (*(A) = (unsig
6dc60 6e 65 64 20 63 68 61 72 29 28 42 29 29 2c 31 20  ned char)(B)),1 
6dc70 3a 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69  : sqlite3PutVari
6dc80 6e 74 33 32 28 28 41 29 2c 20 28 42 29 29 29 0a  nt32((A), (B))).
6dc90 23 64 65 66 69 6e 65 20 67 65 74 56 61 72 69 6e  #define getVarin
6dca0 74 20 20 20 20 73 71 6c 69 74 65 33 47 65 74 56  t    sqlite3GetV
6dcb0 61 72 69 6e 74 0a 23 64 65 66 69 6e 65 20 70 75  arint.#define pu
6dcc0 74 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74  tVarint    sqlit
6dcd0 65 33 50 75 74 56 61 72 69 6e 74 0a 0a 0a 53 51  e3PutVarint...SQ
6dce0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6dcf0 64 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66  d sqlite3IndexAf
6dd00 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a  finityStr(Vdbe *
6dd10 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49  , Index *);.SQLI
6dd20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6dd30 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69  sqlite3TableAffi
6dd40 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20  nityStr(Vdbe *, 
6dd50 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Table *);.SQLITE
6dd60 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
6dd70 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
6dd80 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
6dd90 2c 20 63 68 61 72 20 61 66 66 32 29 3b 0a 53 51  , char aff2);.SQ
6dda0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6ddb0 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66   sqlite3IndexAff
6ddc0 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45  inityOk(Expr *pE
6ddd0 78 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66  xpr, char idx_af
6dde0 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 5f  finity);.SQLITE_
6ddf0 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c  PRIVATE char sql
6de00 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
6de10 28 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53  (Expr *pExpr);.S
6de20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6de30 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28  t sqlite3Atoi64(
6de40 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 34  const char*, i64
6de50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6de60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6de70 72 72 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 69  rror(sqlite3*, i
6de80 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  nt, const char*,
6de90 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ...);.SQLITE_PRI
6dea0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
6deb0 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69  e3HexToBlob(sqli
6dec0 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  te3*, const char
6ded0 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c   *z, int n);.SQL
6dee0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6def0 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
6df00 6d 65 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65  me(Parse *, Toke
6df10 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f  n *, Token *, To
6df20 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  ken **);.SQLITE_
6df30 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
6df40 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74  ar *sqlite3ErrSt
6df50 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  r(int);.SQLITE_P
6df60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6df70 65 33 52 65 61 64 53 63 68 65 6d 61 28 50 61 72  e3ReadSchema(Par
6df80 73 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c  se *pParse);.SQL
6df90 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c  ITE_PRIVATE Coll
6dfa0 53 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  Seq *sqlite3Find
6dfb0 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a  CollSeq(sqlite3*
6dfc0 2c 75 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63  ,u8 enc, const c
6dfd0 68 61 72 20 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  har *,int,int);.
6dfe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
6dff0 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c  ollSeq *sqlite3L
6e000 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72  ocateCollSeq(Par
6e010 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
6e020 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69  t char *zName, i
6e030 6e 74 20 6e 4e 61 6d 65 29 3b 0a 53 51 4c 49 54  nt nName);.SQLIT
6e040 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
6e050 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f  q *sqlite3ExprCo
6e060 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61  llSeq(Parse *pPa
6e070 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
6e080 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6e090 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
6e0a0 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65  xprSetColl(Parse
6e0b0 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
6e0c0 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49  , Token *);.SQLI
6e0d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6e0e0 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53  qlite3CheckCollS
6e0f0 65 71 28 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c  eq(Parse *, Coll
6e100 53 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Seq *);.SQLITE_P
6e110 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6e120 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
6e130 65 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74  e(Parse *, const
6e140 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
6e150 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6e160 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
6e170 67 65 73 28 73 71 6c 69 74 65 33 20 2a 2c 20 69  ges(sqlite3 *, i
6e180 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
6e190 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20  VATE const void 
6e1a0 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78  *sqlite3ValueTex
6e1b0 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t(sqlite3_value*
6e1c0 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52  , u8);.SQLITE_PR
6e1d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6e1e0 33 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69  3ValueBytes(sqli
6e1f0 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b  te3_value*, u8);
6e200 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e210 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75  void sqlite3Valu
6e220 65 53 65 74 53 74 72 28 73 71 6c 69 74 65 33 5f  eSetStr(sqlite3_
6e230 76 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e  value*, int, con
6e240 73 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20  st void *,u8, . 
6e250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e260 20 20 20 20 20 20 20 76 6f 69 64 28 2a 29 28 76         void(*)(v
6e270 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  oid*));.SQLITE_P
6e280 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6e290 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c  te3ValueFree(sql
6e2a0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
6e2b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
6e2c0 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69  ite3_value *sqli
6e2d0 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69  te3ValueNew(sqli
6e2e0 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  te3 *);.SQLITE_P
6e2f0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
6e300 69 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c  ite3Utf16to8(sql
6e310 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f  ite3 *, const vo
6e320 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  id*, int);.SQLIT
6e330 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6e340 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
6e350 70 72 28 73 71 6c 69 74 65 33 20 2a 2c 20 45 78  pr(sqlite3 *, Ex
6e360 70 72 20 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71  pr *, u8, u8, sq
6e370 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b  lite3_value **);
6e380 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e390 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75  void sqlite3Valu
6e3a0 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73  eApplyAffinity(s
6e3b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20  qlite3_value *, 
6e3c0 75 38 2c 20 75 38 29 3b 0a 23 69 66 6e 64 65 66  u8, u8);.#ifndef
6e3d0 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
6e3e0 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56  TION.SQLITE_PRIV
6e3f0 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ATE const unsign
6e400 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55  ed char sqlite3U
6e410 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53  pperToLower[];.S
6e420 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
6e430 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
6e440 72 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61  r sqlite3CtypeMa
6e450 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  p[];.SQLITE_PRIV
6e460 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73  ATE SQLITE_WSD s
6e470 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e  truct Sqlite3Con
6e480 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69  fig sqlite3Confi
6e490 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  g;.SQLITE_PRIVAT
6e4a0 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e  E SQLITE_WSD Fun
6e4b0 63 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33  cDefHash sqlite3
6e4c0 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b  GlobalFunctions;
6e4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e4e0 69 6e 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69  int sqlite3Pendi
6e4f0 6e 67 42 79 74 65 3b 0a 23 65 6e 64 69 66 0a 53  ngByte;.#endif.S
6e500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6e510 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61  id sqlite3RootPa
6e520 67 65 4d 6f 76 65 64 28 44 62 2a 2c 20 69 6e 74  geMoved(Db*, int
6e530 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6e540 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6e550 74 65 33 52 65 69 6e 64 65 78 28 50 61 72 73 65  te3Reindex(Parse
6e560 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
6e570 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e580 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
6e590 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71  lterFunctions(sq
6e5a0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
6e5b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6e5c0 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54  ite3AlterRenameT
6e5d0 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63  able(Parse*, Src
6e5e0 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  List*, Token*);.
6e5f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6e600 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b  nt sqlite3GetTok
6e610 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  en(const unsigne
6e620 64 20 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29  d char *, int *)
6e630 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6e640 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73   void sqlite3Nes
6e650 74 65 64 50 61 72 73 65 28 50 61 72 73 65 2a 2c  tedParse(Parse*,
6e660 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e   const char*, ..
6e670 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .);.SQLITE_PRIVA
6e680 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6e690 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61  xpirePreparedSta
6e6a0 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 2a  tements(sqlite3*
6e6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6e6c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
6e6d0 64 65 53 75 62 73 65 6c 65 63 74 28 50 61 72 73  deSubselect(Pars
6e6e0 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74  e *, Expr *, int
6e6f0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6e700 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6e710 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 50 61  te3SelectPrep(Pa
6e720 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e  rse*, Select*, N
6e730 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51  ameContext*);.SQ
6e740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6e750 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
6e760 78 70 72 4e 61 6d 65 73 28 4e 61 6d 65 43 6f 6e  xprNames(NameCon
6e770 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  text*, Expr*);.S
6e780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6e790 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  id sqlite3Resolv
6e7a0 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 50 61 72  eSelectNames(Par
6e7b0 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61  se*, Select*, Na
6e7c0 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c  meContext*);.SQL
6e7d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6e7e0 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72  sqlite3ResolveOr
6e7f0 64 65 72 47 72 6f 75 70 42 79 28 50 61 72 73 65  derGroupBy(Parse
6e800 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72  *, Select*, Expr
6e810 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61  List*, const cha
6e820 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6e830 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6e840 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64  ColumnDefault(Vd
6e850 62 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69  be *, Table *, i
6e860 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6e870 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6e880 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f  AlterFinishAddCo
6e890 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f  lumn(Parse *, To
6e8a0 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ken *);.SQLITE_P
6e8b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6e8c0 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64  te3AlterBeginAdd
6e8d0 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20  Column(Parse *, 
6e8e0 53 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49  SrcList *);.SQLI
6e8f0 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
6e900 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f  eq *sqlite3GetCo
6e910 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 20  llSeq(sqlite3*, 
6e920 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74  CollSeq *, const
6e930 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53   char *, int);.S
6e940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
6e950 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69  ar sqlite3Affini
6e960 74 79 54 79 70 65 28 63 6f 6e 73 74 20 54 6f 6b  tyType(const Tok
6e970 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
6e980 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6e990 33 41 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c  3Analyze(Parse*,
6e9a0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29   Token*, Token*)
6e9b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6e9c0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f   int sqlite3Invo
6e9d0 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75  keBusyHandler(Bu
6e9e0 73 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c  syHandler*);.SQL
6e9f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6ea00 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71  sqlite3FindDb(sq
6ea10 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  lite3*, Token*);
6ea20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ea30 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44  int sqlite3FindD
6ea40 62 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 2c  bName(sqlite3 *,
6ea50 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
6ea60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6ea70 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73  nt sqlite3Analys
6ea80 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c  isLoad(sqlite3*,
6ea90 69 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54 45  int iDB);.SQLITE
6eaa0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6eab0 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45  lite3DefaultRowE
6eac0 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49  st(Index*);.SQLI
6ead0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6eae0 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c  sqlite3RegisterL
6eaf0 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c  ikeFunctions(sql
6eb00 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ite3*, int);.SQL
6eb10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6eb20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e  sqlite3IsLikeFun
6eb30 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 45  ction(sqlite3*,E
6eb40 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 29  xpr*,int*,char*)
6eb50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6eb60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 6e   void sqlite3Min
6eb70 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 50  imumFileFormat(P
6eb80 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  arse*, int, int)
6eb90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6eba0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68   void sqlite3Sch
6ebb0 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29 3b  emaFree(void *);
6ebc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ebd0 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53  Schema *sqlite3S
6ebe0 63 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33  chemaGet(sqlite3
6ebf0 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 51   *, Btree *);.SQ
6ec00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6ec10 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
6ec20 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64  Index(sqlite3 *d
6ec30 62 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 51  b, Schema *);.SQ
6ec40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65 79  LITE_PRIVATE Key
6ec50 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64  Info *sqlite3Ind
6ec60 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 20  exKeyinfo(Parse 
6ec70 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c  *, Index *);.SQL
6ec80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6ec90 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
6eca0 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e  c(sqlite3 *, con
6ecb0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  st char *, int, 
6ecc0 69 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 20  int, void *, .  
6ecd0 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33  void (*)(sqlite3
6ece0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
6ecf0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c  lite3_value **),
6ed00 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69  .  void (*)(sqli
6ed10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
6ed20 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
6ed30 2a 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 6c  *), void (*)(sql
6ed40 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b  ite3_context*));
6ed50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ed60 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78  int sqlite3ApiEx
6ed70 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  it(sqlite3 *db, 
6ed80 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6ed90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6eda0 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65  OpenTempDatabase
6edb0 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c 49  (Parse *);..SQLI
6edc0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6edd0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49  sqlite3StrAccumI
6ede0 6e 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 63  nit(StrAccum*, c
6edf0 68 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  har*, int, int);
6ee00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ee10 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41  void sqlite3StrA
6ee20 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63  ccumAppend(StrAc
6ee30 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  cum*,const char*
6ee40 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
6ee50 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
6ee60 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73  te3StrAccumFinis
6ee70 68 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51  h(StrAccum*);.SQ
6ee80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6ee90 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  d sqlite3StrAccu
6eea0 6d 52 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a  mReset(StrAccum*
6eeb0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6eec0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
6eed0 6c 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c  lectDestInit(Sel
6eee0 65 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74  ectDest*,int,int
6eef0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
6ef00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
6ef10 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c  ackupRestart(sql
6ef20 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 3b 0a  ite3_backup *);.
6ef30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6ef40 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75  oid sqlite3Backu
6ef50 70 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f  pUpdate(sqlite3_
6ef60 62 61 63 6b 75 70 20 2a 2c 20 50 67 6e 6f 2c 20  backup *, Pgno, 
6ef70 63 6f 6e 73 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a  const u8 *);../*
6ef80 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63  .** The interfac
6ef90 65 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d 67  e to the LEMON-g
6efa0 65 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 0a  enerated parser.
6efb0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
6efc0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50  E void *sqlite3P
6efd0 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 2a  arserAlloc(void*
6efe0 28 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53 51  (*)(size_t));.SQ
6eff0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f000 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46  d sqlite3ParserF
6f010 72 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64 28  ree(void*, void(
6f020 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
6f030 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f040 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 76 6f  sqlite3Parser(vo
6f050 69 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c  id*, int, Token,
6f060 20 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65 66   Parse*);.#ifdef
6f070 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b   YYTRACKMAXSTACK
6f080 44 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49  DEPTH.SQLITE_PRI
6f090 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6f0a0 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61  e3ParserStackPea
6f0b0 6b 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66  k(void*);.#endif
6f0c0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
6f0d0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 6f   int sqlite3Auto
6f0e0 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73  LoadExtensions(s
6f0f0 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65  qlite3*);.#ifnde
6f100 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
6f110 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c  AD_EXTENSION.SQL
6f120 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
6f130 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45  id sqlite3CloseE
6f140 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65  xtensions(sqlite
6f150 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  3*);.#else.# def
6f160 69 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f 73 65  ine sqlite3Close
6f170 45 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65  Extensions(X).#e
6f180 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
6f190 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
6f1a0 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52  _CACHE.SQLITE_PR
6f1b0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
6f1c0 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61  ite3TableLock(Pa
6f1d0 72 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  rse *, int, int,
6f1e0 20 75 38 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   u8, const char 
6f1f0 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  *);.#else.  #def
6f200 69 6e 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ine sqlite3Table
6f210 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a  Lock(v,w,x,y,z).
6f220 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
6f230 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
6f240 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
6f250 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75  sqlite3Utf8To8(u
6f260 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a  nsigned char*);.
6f270 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
6f280 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
6f290 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e  ALTABLE.#  defin
6f2a0 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65  e sqlite3VtabCle
6f2b0 61 72 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20  ar(X).#  define 
6f2c0 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28  sqlite3VtabSync(
6f2d0 58 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23  X,Y) SQLITE_OK.#
6f2e0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
6f2f0 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a  VtabRollback(X).
6f300 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  #  define sqlite
6f310 33 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23  3VtabCommit(X).#
6f320 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
6f330 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 30  VtabInSync(db) 0
6f340 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52  .#else.SQLITE_PR
6f350 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71  IVATE    void sq
6f360 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 54  lite3VtabClear(T
6f370 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  able*);.SQLITE_P
6f380 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71  RIVATE    int sq
6f390 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71  lite3VtabSync(sq
6f3a0 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20  lite3 *db, char 
6f3b0 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
6f3c0 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74  ATE    int sqlit
6f3d0 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73  e3VtabRollback(s
6f3e0 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c  qlite3 *db);.SQL
6f3f0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69  ITE_PRIVATE    i
6f400 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f  nt sqlite3VtabCo
6f410 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  mmit(sqlite3 *db
6f420 29 3b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  );.#  define sql
6f430 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64  ite3VtabInSync(d
6f440 62 29 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e  b) ((db)->nVTran
6f450 73 3e 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54  s>0 && (db)->aVT
6f460 72 61 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a  rans==0).#endif.
6f470 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f480 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d  oid sqlite3VtabM
6f490 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 73  akeWritable(Pars
6f4a0 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49  e*,Table*);.SQLI
6f4b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f4c0 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28  sqlite3VtabLock(
6f4d0 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b 0a  sqlite3_vtab*);.
6f4e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f4f0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55  oid sqlite3VtabU
6f500 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 2a 2c 20  nlock(sqlite3*, 
6f510 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b 0a  sqlite3_vtab*);.
6f520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f530 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42  oid sqlite3VtabB
6f540 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a  eginParse(Parse*
6f550 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a  , Token*, Token*
6f560 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
6f570 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f580 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68  qlite3VtabFinish
6f590 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f  Parse(Parse*, To
6f5a0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
6f5b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f5c0 65 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61  e3VtabArgInit(Pa
6f5d0 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  rse*);.SQLITE_PR
6f5e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f5f0 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28  e3VtabArgExtend(
6f600 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Parse*, Token*);
6f610 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f620 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43  int sqlite3VtabC
6f630 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65  allCreate(sqlite
6f640 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  3*, int, const c
6f650 68 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b  har *, char **);
6f660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f670 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43  int sqlite3VtabC
6f680 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65  allConnect(Parse
6f690 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49  *, Table*);.SQLI
6f6a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6f6b0 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65  qlite3VtabCallDe
6f6c0 73 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20  stroy(sqlite3*, 
6f6d0 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  int, const char 
6f6e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f6f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74  TE int sqlite3Vt
6f700 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 20  abBegin(sqlite3 
6f710 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  *, sqlite3_vtab 
6f720 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f730 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69  TE FuncDef *sqli
6f740 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46  te3VtabOverloadF
6f750 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20  unction(sqlite3 
6f760 2a 2c 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74 20  *,FuncDef*, int 
6f770 6e 41 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53 51  nArg, Expr*);.SQ
6f780 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f790 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64  d sqlite3Invalid
6f7a0 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  Function(sqlite3
6f7b0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
6f7c0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a  lite3_value**);.
6f7d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6f7e0 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66  nt sqlite3Transf
6f7f0 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  erBindings(sqlit
6f800 65 33 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69 74  e3_stmt *, sqlit
6f810 65 33 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c 49  e3_stmt *);.SQLI
6f820 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6f830 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28  qlite3Reprepare(
6f840 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
6f850 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f860 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b  te3ExprListCheck
6f870 4c 65 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45  Length(Parse*, E
6f880 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20  xprList*, const 
6f890 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
6f8a0 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
6f8b0 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
6f8c0 70 61 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 73  pareCollSeq(Pars
6f8d0 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70  e *, Expr *, Exp
6f8e0 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r *);.SQLITE_PRI
6f8f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6f900 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e  TempInMemory(con
6f910 73 74 20 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 0a  st sqlite3*);...
6f920 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 6c 65  ./*.** Available
6f930 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72 73   fault injectors
6f940 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e 75 6d  .  Should be num
6f950 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 20  bered beginning 
6f960 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 66 69  with 0..*/.#defi
6f970 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 49  ne SQLITE_FAULTI
6f980 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20  NJECTOR_MALLOC  
6f990 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
6f9a0 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f  ITE_FAULTINJECTO
6f9b0 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 0a 0a  R_COUNT      1..
6f9c0 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66  /*.** The interf
6f9d0 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64 65 20  ace to the code 
6f9e0 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 64 20  in fault.c used 
6f9f0 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e 67 20  for identifying 
6fa00 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c  "benign".** mall
6fa10 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54 68 69  oc failures. Thi
6fa20 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 65 6e  s is only presen
6fa30 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  t if SQLITE_OMIT
6fa40 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a  _BUILTIN_TEST.**
6fa50 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e   is not defined.
6fa60 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
6fa70 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
6fa80 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
6fa90 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
6faa0 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
6fab0 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  loc(void);.SQLIT
6fac0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
6fad0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
6fae0 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 23  nMalloc(void);.#
6faf0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73  else.  #define s
6fb00 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
6fb10 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 65 66  nMalloc().  #def
6fb20 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 42 65  ine sqlite3EndBe
6fb30 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e  nignMalloc().#en
6fb40 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 4e 5f  dif..#define IN_
6fb50 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 20 20  INDEX_ROWID     
6fb60 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
6fb70 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 20 20  IN_INDEX_EPH    
6fb80 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69           2.#defi
6fb90 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45  ne IN_INDEX_INDE
6fba0 58 20 20 20 20 20 20 20 20 20 20 20 33 0a 53 51  X           3.SQ
6fbb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6fbc0 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e   sqlite3FindInIn
6fbd0 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45 78 70  dex(Parse *, Exp
6fbe0 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69 66  r *, int*);..#if
6fbf0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
6fc00 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 53  E_ATOMIC_WRITE.S
6fc10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6fc20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  int sqlite3Journ
6fc30 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  alOpen(sqlite3_v
6fc40 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  fs *, const char
6fc50 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   *, sqlite3_file
6fc60 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53   *, int, int);.S
6fc70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6fc80 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  int sqlite3Journ
6fc90 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76  alSize(sqlite3_v
6fca0 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  fs *);.SQLITE_PR
6fcb0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
6fcc0 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65  te3JournalCreate
6fcd0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
6fce0 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  ;.#else.  #defin
6fcf0 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  e sqlite3Journal
6fd00 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 56 66  Size(pVfs) ((pVf
6fd10 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 23 65  s)->szOsFile).#e
6fd20 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49  ndif..SQLITE_PRI
6fd30 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6fd40 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28  3MemJournalOpen(
6fd50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b  sqlite3_file *);
6fd60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fd70 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f  int sqlite3MemJo
6fd80 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 3b  urnalSize(void);
6fd90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fda0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65 6d  int sqlite3IsMem
6fdb0 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f  Journal(sqlite3_
6fdc0 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 53 51  file *);..#if SQ
6fdd0 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
6fde0 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 52 49  PTH>0.SQLITE_PRI
6fdf0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
6fe00 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
6fe10 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
6fe20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 54 45  Expr *p);.SQLITE
6fe30 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
6fe40 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72  qlite3SelectExpr
6fe50 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a 29  Height(Select *)
6fe60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6fe70 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78     int sqlite3Ex
6fe80 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 50 61  prCheckHeight(Pa
6fe90 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73  rse*, int);.#els
6fea0 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  e.  #define sqli
6feb0 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
6fec0 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 20  (x,y).  #define 
6fed0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
6fee0 72 48 65 69 67 68 74 28 78 29 20 30 0a 20 20 23  rHeight(x) 0.  #
6fef0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78  define sqlite3Ex
6ff00 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 78 2c  prCheckHeight(x,
6ff10 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54  y).#endif..SQLIT
6ff20 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71  E_PRIVATE u32 sq
6ff30 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63 6f  lite3Get4byte(co
6ff40 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54 45  nst u8*);.SQLITE
6ff50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6ff60 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 38  lite3Put4byte(u8
6ff70 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 65 66  *, u32);..#ifdef
6ff80 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55   SQLITE_ENABLE_U
6ff90 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 51 4c  NLOCK_NOTIFY.SQL
6ffa0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
6ffb0 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63  id sqlite3Connec
6ffc0 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69  tionBlocked(sqli
6ffd0 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 20 2a  te3 *, sqlite3 *
6ffe0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6fff0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
70000 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b  ConnectionUnlock
70010 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  ed(sqlite3 *db);
70020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70030 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f    void sqlite3Co
70040 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 73  nnectionClosed(s
70050 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6c  qlite3 *db);.#el
70060 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  se.  #define sql
70070 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c  ite3ConnectionBl
70080 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 64 65  ocked(x,y).  #de
70090 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e  fine sqlite3Conn
700a0 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 78  ectionUnlocked(x
700b0 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ).  #define sqli
700c0 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f  te3ConnectionClo
700d0 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a 0a 0a  sed(x).#endif...
700e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53  #ifdef SQLITE_SS
700f0 45 0a 23 69 6e 63 6c 75 64 65 20 22 73 73 65 49  E.#include "sseI
70100 6e 74 2e 68 22 0a 23 65 6e 64 69 66 0a 0a 23 69  nt.h".#endif..#i
70110 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
70120 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
70130 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50     void sqlite3P
70140 61 72 73 65 72 54 72 61 63 65 28 46 49 4c 45 2a  arserTrace(FILE*
70150 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69  , char *);.#endi
70160 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  f../*.** If the 
70170 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20 49 4f  SQLITE_ENABLE IO
70180 54 52 41 43 45 20 65 78 69 73 74 73 20 74 68 65  TRACE exists the
70190 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72  n the global var
701a0 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 65 33  iable.** sqlite3
701b0 49 6f 54 72 61 63 65 20 69 73 20 61 20 70 6f 69  IoTrace is a poi
701c0 6e 74 65 72 20 74 6f 20 61 20 70 72 69 6e 74 66  nter to a printf
701d0 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 20 75 73  -like routine us
701e0 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 20 49  ed to.** print I
701f0 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 73 73 61  /O tracing messa
70200 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 66 20  ges. .*/.#ifdef 
70210 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f  SQLITE_ENABLE_IO
70220 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49  TRACE.# define I
70230 4f 54 52 41 43 45 28 41 29 20 20 69 66 28 20 73  OTRACE(A)  if( s
70240 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 29 7b  qlite3IoTrace ){
70250 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20   sqlite3IoTrace 
70260 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  A; }.SQLITE_PRIV
70270 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
70280 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c  e3VdbeIOTraceSql
70290 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
702a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73  PRIVATE void (*s
702b0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29 28 63  qlite3IoTrace)(c
702c0 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
702d0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
702e0 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64 65 66  IOTRACE(A).# def
702f0 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 49  ine sqlite3VdbeI
70300 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e  OTraceSql(X).#en
70310 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  dif..#endif../**
70320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
70330 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
70340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
70370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
70380 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 6c 2e 63  in file global.c
70390 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
703a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
703b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
703c0 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 33 0a  ** 2008 June 13.
703d0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
703e0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
703f0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
70400 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
70410 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
70420 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
70430 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
70440 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
70450 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
70460 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
70470 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
70480 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
70490 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
704a0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
704b0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
704c0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
704d0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
704e0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
704f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
70530 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
70540 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69  ontains definiti
70550 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61  ons of global va
70560 72 69 61 62 6c 65 73 20 61 6e 64 20 63 6f 6e 74  riables and cont
70570 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ants..**.** $Id:
70580 20 67 6c 6f 62 61 6c 2e 63 2c 76 20 31 2e 31 32   global.c,v 1.12
70590 20 32 30 30 39 2f 30 32 2f 30 35 20 31 36 3a 33   2009/02/05 16:3
705a0 31 3a 34 36 20 64 72 68 20 45 78 70 20 24 0a 2a  1:46 drh Exp $.*
705b0 2f 0a 0a 0a 2f 2a 20 41 6e 20 61 72 72 61 79 20  /.../* An array 
705c0 74 6f 20 6d 61 70 20 61 6c 6c 20 75 70 70 65 72  to map all upper
705d0 2d 63 61 73 65 20 63 68 61 72 61 63 74 65 72 73  -case characters
705e0 20 69 6e 74 6f 20 74 68 65 69 72 20 63 6f 72 72   into their corr
705f0 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6c 6f 77  esponding.** low
70600 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 74 65  er-case characte
70610 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  r. .**.** SQLite
70620 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72 73 20   only considers 
70630 55 53 2d 41 53 43 49 49 20 28 6f 72 20 45 42 43  US-ASCII (or EBC
70640 44 49 43 29 20 63 68 61 72 61 63 74 65 72 73 2e  DIC) characters.
70650 20 20 57 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68    We do not.** h
70660 61 6e 64 6c 65 20 63 61 73 65 20 63 6f 6e 76 65  andle case conve
70670 72 73 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 55  rsions for the U
70680 54 46 20 63 68 61 72 61 63 74 65 72 20 73 65 74  TF character set
70690 20 73 69 6e 63 65 20 74 68 65 20 74 61 62 6c 65   since the table
706a0 73 0a 2a 2a 20 69 6e 76 6f 6c 76 65 64 20 61 72  s.** involved ar
706b0 65 20 6e 65 61 72 6c 79 20 61 73 20 62 69 67 20  e nearly as big 
706c0 6f 72 20 62 69 67 67 65 72 20 74 68 61 6e 20 53  or bigger than S
706d0 51 4c 69 74 65 20 69 74 73 65 6c 66 2e 0a 2a 2f  QLite itself..*/
706e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
706f0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
70700 68 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72  har sqlite3Upper
70710 54 6f 4c 6f 77 65 72 5b 5d 20 3d 20 7b 0a 23 69  ToLower[] = {.#i
70720 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49  fdef SQLITE_ASCI
70730 49 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20  I.      0,  1,  
70740 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20  2,  3,  4,  5,  
70750 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31  6,  7,  8,  9, 1
70760 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31  0, 11, 12, 13, 1
70770 34 2c 20 31 35 2c 20 31 36 2c 20 31 37 2c 0a 20  4, 15, 16, 17,. 
70780 20 20 20 20 31 38 2c 20 31 39 2c 20 32 30 2c 20      18, 19, 20, 
70790 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 20  21, 22, 23, 24, 
707a0 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 20  25, 26, 27, 28, 
707b0 32 39 2c 20 33 30 2c 20 33 31 2c 20 33 32 2c 20  29, 30, 31, 32, 
707c0 33 33 2c 20 33 34 2c 20 33 35 2c 0a 20 20 20 20  33, 34, 35,.    
707d0 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c   36, 37, 38, 39,
707e0 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c   40, 41, 42, 43,
707f0 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c   44, 45, 46, 47,
70800 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c   48, 49, 50, 51,
70810 20 35 32 2c 20 35 33 2c 0a 20 20 20 20 20 35 34   52, 53,.     54
70820 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38  , 55, 56, 57, 58
70830 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32  , 59, 60, 61, 62
70840 2c 20 36 33 2c 20 36 34 2c 20 39 37 2c 20 39 38  , 63, 64, 97, 98
70850 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32  , 99,100,101,102
70860 2c 31 30 33 2c 0a 20 20 20 20 31 30 34 2c 31 30  ,103,.    104,10
70870 35 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30  5,106,107,108,10
70880 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31  9,110,111,112,11
70890 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31  3,114,115,116,11
708a0 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32  7,118,119,120,12
708b0 31 2c 0a 20 20 20 20 31 32 32 2c 20 39 31 2c 20  1,.    122, 91, 
708c0 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20  92, 93, 94, 95, 
708d0 39 36 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31  96, 97, 98, 99,1
708e0 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31  00,101,102,103,1
708f0 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 0a  04,105,106,107,.
70900 20 20 20 20 31 30 38 2c 31 30 39 2c 31 31 30 2c      108,109,110,
70910 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c  111,112,113,114,
70920 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c  115,116,117,118,
70930 31 31 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c  119,120,121,122,
70940 31 32 33 2c 31 32 34 2c 31 32 35 2c 0a 20 20 20  123,124,125,.   
70950 20 31 32 36 2c 31 32 37 2c 31 32 38 2c 31 32 39   126,127,128,129
70960 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33  ,130,131,132,133
70970 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37  ,134,135,136,137
70980 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31  ,138,139,140,141
70990 2c 31 34 32 2c 31 34 33 2c 0a 20 20 20 20 31 34  ,142,143,.    14
709a0 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34  4,145,146,147,14
709b0 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35  8,149,150,151,15
709c0 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35  2,153,154,155,15
709d0 36 2c 31 35 37 2c 31 35 38 2c 31 35 39 2c 31 36  6,157,158,159,16
709e0 30 2c 31 36 31 2c 0a 20 20 20 20 31 36 32 2c 31  0,161,.    162,1
709f0 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31  63,164,165,166,1
70a00 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31  67,168,169,170,1
70a10 37 31 2c 31 37 32 2c 31 37 33 2c 31 37 34 2c 31  71,172,173,174,1
70a20 37 35 2c 31 37 36 2c 31 37 37 2c 31 37 38 2c 31  75,176,177,178,1
70a30 37 39 2c 0a 20 20 20 20 31 38 30 2c 31 38 31 2c  79,.    180,181,
70a40 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c  182,183,184,185,
70a50 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c  186,187,188,189,
70a60 31 39 30 2c 31 39 31 2c 31 39 32 2c 31 39 33 2c  190,191,192,193,
70a70 31 39 34 2c 31 39 35 2c 31 39 36 2c 31 39 37 2c  194,195,196,197,
70a80 0a 20 20 20 20 31 39 38 2c 31 39 39 2c 32 30 30  .    198,199,200
70a90 2c 32 30 31 2c 32 30 32 2c 32 30 33 2c 32 30 34  ,201,202,203,204
70aa0 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 32 30 38  ,205,206,207,208
70ab0 2c 32 30 39 2c 32 31 30 2c 32 31 31 2c 32 31 32  ,209,210,211,212
70ac0 2c 32 31 33 2c 32 31 34 2c 32 31 35 2c 0a 20 20  ,213,214,215,.  
70ad0 20 20 32 31 36 2c 32 31 37 2c 32 31 38 2c 32 31    216,217,218,21
70ae0 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32  9,220,221,222,22
70af0 33 2c 32 32 34 2c 32 32 35 2c 32 32 36 2c 32 32  3,224,225,226,22
70b00 37 2c 32 32 38 2c 32 32 39 2c 32 33 30 2c 32 33  7,228,229,230,23
70b10 31 2c 32 33 32 2c 32 33 33 2c 0a 20 20 20 20 32  1,232,233,.    2
70b20 33 34 2c 32 33 35 2c 32 33 36 2c 32 33 37 2c 32  34,235,236,237,2
70b30 33 38 2c 32 33 39 2c 32 34 30 2c 32 34 31 2c 32  38,239,240,241,2
70b40 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32  42,243,244,245,2
70b50 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32  46,247,248,249,2
70b60 35 30 2c 32 35 31 2c 0a 20 20 20 20 32 35 32 2c  50,251,.    252,
70b70 32 35 33 2c 32 35 34 2c 32 35 35 0a 23 65 6e 64  253,254,255.#end
70b80 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
70b90 5f 45 42 43 44 49 43 0a 20 20 20 20 20 20 30 2c  _EBCDIC.      0,
70ba0 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c    1,  2,  3,  4,
70bb0 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c    5,  6,  7,  8,
70bc0 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c    9, 10, 11, 12,
70bd0 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 2f 2a 20   13, 14, 15, /* 
70be0 30 78 20 2a 2f 0a 20 20 20 20 20 31 36 2c 20 31  0x */.     16, 1
70bf0 37 2c 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32  7, 18, 19, 20, 2
70c00 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32  1, 22, 23, 24, 2
70c10 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32  5, 26, 27, 28, 2
70c20 39 2c 20 33 30 2c 20 33 31 2c 20 2f 2a 20 31 78  9, 30, 31, /* 1x
70c30 20 2a 2f 0a 20 20 20 20 20 33 32 2c 20 33 33 2c   */.     32, 33,
70c40 20 33 34 2c 20 33 35 2c 20 33 36 2c 20 33 37 2c   34, 35, 36, 37,
70c50 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c   38, 39, 40, 41,
70c60 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c   42, 43, 44, 45,
70c70 20 34 36 2c 20 34 37 2c 20 2f 2a 20 32 78 20 2a   46, 47, /* 2x *
70c80 2f 0a 20 20 20 20 20 34 38 2c 20 34 39 2c 20 35  /.     48, 49, 5
70c90 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 20 35  0, 51, 52, 53, 5
70ca0 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 35  4, 55, 56, 57, 5
70cb0 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 36  8, 59, 60, 61, 6
70cc0 32 2c 20 36 33 2c 20 2f 2a 20 33 78 20 2a 2f 0a  2, 63, /* 3x */.
70cd0 20 20 20 20 20 36 34 2c 20 36 35 2c 20 36 36 2c       64, 65, 66,
70ce0 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c   67, 68, 69, 70,
70cf0 20 37 31 2c 20 37 32 2c 20 37 33 2c 20 37 34 2c   71, 72, 73, 74,
70d00 20 37 35 2c 20 37 36 2c 20 37 37 2c 20 37 38 2c   75, 76, 77, 78,
70d10 20 37 39 2c 20 2f 2a 20 34 78 20 2a 2f 0a 20 20   79, /* 4x */.  
70d20 20 20 20 38 30 2c 20 38 31 2c 20 38 32 2c 20 38     80, 81, 82, 8
70d30 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38  3, 84, 85, 86, 8
70d40 37 2c 20 38 38 2c 20 38 39 2c 20 39 30 2c 20 39  7, 88, 89, 90, 9
70d50 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39  1, 92, 93, 94, 9
70d60 35 2c 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20  5, /* 5x */.    
70d70 20 39 36 2c 20 39 37 2c 20 36 36 2c 20 36 37 2c   96, 97, 66, 67,
70d80 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c   68, 69, 70, 71,
70d90 20 37 32 2c 20 37 33 2c 31 30 36 2c 31 30 37 2c   72, 73,106,107,
70da0 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c  108,109,110,111,
70db0 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 31   /* 6x */.    11
70dc0 32 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38  2, 81, 82, 83, 8
70dd0 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38  4, 85, 86, 87, 8
70de0 38 2c 20 38 39 2c 31 32 32 2c 31 32 33 2c 31 32  8, 89,122,123,12
70df0 34 2c 31 32 35 2c 31 32 36 2c 31 32 37 2c 20 2f  4,125,126,127, /
70e00 2a 20 37 78 20 2a 2f 0a 20 20 20 20 31 32 38 2c  * 7x */.    128,
70e10 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c  129,130,131,132,
70e20 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c  133,134,135,136,
70e30 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c  137,138,139,140,
70e40 31 34 31 2c 31 34 32 2c 31 34 33 2c 20 2f 2a 20  141,142,143, /* 
70e50 38 78 20 2a 2f 0a 20 20 20 20 31 34 34 2c 31 34  8x */.    144,14
70e60 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34  5,146,147,148,14
70e70 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35  9,150,151,152,15
70e80 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35  3,154,155,156,15
70e90 37 2c 31 35 36 2c 31 35 39 2c 20 2f 2a 20 39 78  7,156,159, /* 9x
70ea0 20 2a 2f 0a 20 20 20 20 31 36 30 2c 31 36 31 2c   */.    160,161,
70eb0 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c  162,163,164,165,
70ec0 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c  166,167,168,169,
70ed0 31 37 30 2c 31 37 31 2c 31 34 30 2c 31 34 31 2c  170,171,140,141,
70ee0 31 34 32 2c 31 37 35 2c 20 2f 2a 20 41 78 20 2a  142,175, /* Ax *
70ef0 2f 0a 20 20 20 20 31 37 36 2c 31 37 37 2c 31 37  /.    176,177,17
70f00 38 2c 31 37 39 2c 31 38 30 2c 31 38 31 2c 31 38  8,179,180,181,18
70f10 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38  2,183,184,185,18
70f20 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39  6,187,188,189,19
70f30 30 2c 31 39 31 2c 20 2f 2a 20 42 78 20 2a 2f 0a  0,191, /* Bx */.
70f40 20 20 20 20 31 39 32 2c 31 32 39 2c 31 33 30 2c      192,129,130,
70f50 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c  131,132,133,134,
70f60 31 33 35 2c 31 33 36 2c 31 33 37 2c 32 30 32 2c  135,136,137,202,
70f70 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c  203,204,205,206,
70f80 32 30 37 2c 20 2f 2a 20 43 78 20 2a 2f 0a 20 20  207, /* Cx */.  
70f90 20 20 32 30 38 2c 31 34 35 2c 31 34 36 2c 31 34    208,145,146,14
70fa0 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35  7,148,149,150,15
70fb0 31 2c 31 35 32 2c 31 35 33 2c 32 31 38 2c 32 31  1,152,153,218,21
70fc0 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32  9,220,221,222,22
70fd0 33 2c 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20  3, /* Dx */.    
70fe0 32 32 34 2c 32 32 35 2c 31 36 32 2c 31 36 33 2c  224,225,162,163,
70ff0 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c  164,165,166,167,
71000 31 36 38 2c 31 36 39 2c 32 33 32 2c 32 30 33 2c  168,169,232,203,
71010 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c  204,205,206,207,
71020 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 32 33   /* Ex */.    23
71030 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34  9,240,241,242,24
71040 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34  3,244,245,246,24
71050 37 2c 32 34 38 2c 32 34 39 2c 32 31 39 2c 32 32  7,248,249,219,22
71060 30 2c 32 32 31 2c 32 32 32 2c 32 35 35 2c 20 2f  0,221,222,255, /
71070 2a 20 46 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  * Fx */.#endif.}
71080 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ;../*.** The fol
71090 6c 6f 77 69 6e 67 20 32 35 36 20 62 79 74 65 20  lowing 256 byte 
710a0 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20  lookup table is 
710b0 75 73 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20  used to support 
710c0 53 51 4c 69 74 65 73 20 62 75 69 6c 74 2d 69 6e  SQLites built-in
710d0 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 73 20  .** equivalents 
710e0 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  to the following
710f0 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61 72   standard librar
71100 79 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a  y functions:.**.
71110 2a 2a 20 20 20 69 73 73 70 61 63 65 28 29 20 20  **   isspace()  
71120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71130 20 20 20 20 20 20 30 78 30 31 0a 2a 2a 20 20 20        0x01.**   
71140 69 73 61 6c 70 68 61 28 29 20 20 20 20 20 20 20  isalpha()       
71150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71160 20 30 78 30 32 0a 2a 2a 20 20 20 69 73 64 69 67   0x02.**   isdig
71170 69 74 28 29 20 20 20 20 20 20 20 20 20 20 20 20  it()            
71180 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 34              0x04
71190 0a 2a 2a 20 20 20 69 73 61 6c 6e 75 6d 28 29 20  .**   isalnum() 
711a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
711b0 20 20 20 20 20 20 20 30 78 30 36 0a 2a 2a 20 20         0x06.**  
711c0 20 69 73 78 64 69 67 69 74 28 29 20 20 20 20 20   isxdigit()     
711d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
711e0 20 20 30 78 30 38 0a 2a 2a 20 20 20 74 6f 75 70    0x08.**   toup
711f0 70 65 72 28 29 20 20 20 20 20 20 20 20 20 20 20  per()           
71200 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 32               0x2
71210 30 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 78 32 30  0.**.** Bit 0x20
71220 20 69 73 20 73 65 74 20 69 66 20 74 68 65 20 6d   is set if the m
71230 61 70 70 65 64 20 63 68 61 72 61 63 74 65 72 20  apped character 
71240 72 65 71 75 69 72 65 73 20 74 72 61 6e 73 6c 61  requires transla
71250 74 69 6f 6e 20 74 6f 20 75 70 70 65 72 0a 2a 2a  tion to upper.**
71260 20 63 61 73 65 2e 20 69 2e 65 2e 20 69 66 20 74   case. i.e. if t
71270 68 65 20 63 68 61 72 61 63 74 65 72 20 69 73 20  he character is 
71280 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43  a lower-case ASC
71290 49 49 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a  II character..**
712a0 20 49 66 20 78 20 69 73 20 61 20 6c 6f 77 65 72   If x is a lower
712b0 2d 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72  -case ASCII char
712c0 61 63 74 65 72 2c 20 74 68 65 6e 20 69 74 73 20  acter, then its 
712d0 75 70 70 65 72 2d 63 61 73 65 20 65 71 75 69 76  upper-case equiv
712e0 61 6c 65 6e 74 0a 2a 2a 20 69 73 20 28 78 20 2d  alent.** is (x -
712f0 20 30 78 32 30 29 2e 20 54 68 65 72 65 66 6f 72   0x20). Therefor
71300 65 20 74 6f 75 70 70 65 72 28 29 20 63 61 6e 20  e toupper() can 
71310 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  be implemented a
71320 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 78 20 26 20  s:.**.**   (x & 
71330 7e 28 6d 61 70 5b 78 5d 26 30 78 32 30 29 29 0a  ~(map[x]&0x20)).
71340 2a 2a 0a 2a 2a 20 53 74 61 6e 64 61 72 64 20 66  **.** Standard f
71350 75 6e 63 74 69 6f 6e 20 74 6f 6c 6f 77 65 72 28  unction tolower(
71360 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  ) is implemented
71370 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
71380 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d  e3UpperToLower[]
71390 0a 2a 2a 20 61 72 72 61 79 2e 20 74 6f 6c 6f 77  .** array. tolow
713a0 65 72 28 29 20 69 73 20 75 73 65 64 20 6d 6f 72  er() is used mor
713b0 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 74 6f 75  e often than tou
713c0 70 70 65 72 28 29 20 62 79 20 53 51 4c 69 74 65  pper() by SQLite
713d0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 27 73  ..**.** SQLite's
713e0 20 76 65 72 73 69 6f 6e 73 20 61 72 65 20 69 64   versions are id
713f0 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 73  entical to the s
71400 74 61 6e 64 61 72 64 20 76 65 72 73 69 6f 6e 73  tandard versions
71410 20 61 73 73 75 6d 69 6e 67 20 61 0a 2a 2a 20 6c   assuming a.** l
71420 6f 63 61 6c 65 20 6f 66 20 22 43 22 2e 20 54 68  ocale of "C". Th
71430 65 79 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74  ey are implement
71440 65 64 20 61 73 20 6d 61 63 72 6f 73 20 69 6e 20  ed as macros in 
71450 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a  sqliteInt.h..*/.
71460 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
71470 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  CII.SQLITE_PRIVA
71480 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  TE const unsigne
71490 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 74  d char sqlite3Ct
714a0 79 70 65 4d 61 70 5b 32 35 36 5d 20 3d 20 7b 0a  ypeMap[256] = {.
714b0 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78    0x00, 0x00, 0x
714c0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
714d0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
714e0 2c 20 20 2f 2a 20 30 30 2e 2e 30 37 20 20 20 20  ,  /* 00..07    
714f0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
71500 30 30 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20  00, 0x01, 0x01, 
71510 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31  0x01, 0x01, 0x01
71520 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
71530 2a 20 30 38 2e 2e 30 66 20 20 20 20 2e 2e 2e 2e  * 08..0f    ....
71540 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20  .... */.  0x00, 
71550 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71560 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71570 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31 30  00, 0x00,  /* 10
71580 2e 2e 31 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..17    ........
71590 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30   */.  0x00, 0x00
715a0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
715b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
715c0 30 78 30 30 2c 20 20 2f 2a 20 31 38 2e 2e 31 66  0x00,  /* 18..1f
715d0 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
715e0 20 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78    0x01, 0x00, 0x
715f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71600 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71610 2c 20 20 2f 2a 20 32 30 2e 2e 32 37 20 20 20 20  ,  /* 20..27    
71620 20 21 22 23 24 25 26 27 20 2a 2f 0a 20 20 30 78   !"#$%&' */.  0x
71630 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71640 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71650 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
71660 2a 20 32 38 2e 2e 32 66 20 20 20 20 28 29 2a 2b  * 28..2f    ()*+
71670 2c 2d 2e 2f 20 2a 2f 0a 20 20 30 78 30 63 2c 20  ,-./ */.  0x0c, 
71680 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63  0x0c, 0x0c, 0x0c
71690 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78  , 0x0c, 0x0c, 0x
716a0 30 63 2c 20 30 78 30 63 2c 20 20 2f 2a 20 33 30  0c, 0x0c,  /* 30
716b0 2e 2e 33 37 20 20 20 20 30 31 32 33 34 35 36 37  ..37    01234567
716c0 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63   */.  0x0c, 0x0c
716d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
716e0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
716f0 30 78 30 30 2c 20 20 2f 2a 20 33 38 2e 2e 33 66  0x00,  /* 38..3f
71700 20 20 20 20 38 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a      89:;<=>? */.
71710 0a 20 20 30 78 30 30 2c 20 30 78 30 61 2c 20 30  .  0x00, 0x0a, 0
71720 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c  x0a, 0x0a, 0x0a,
71730 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30   0x0a, 0x0a, 0x0
71740 32 2c 20 20 2f 2a 20 34 30 2e 2e 34 37 20 20 20  2,  /* 40..47   
71750 20 40 41 42 43 44 45 46 47 20 2a 2f 0a 20 20 30   @ABCDEFG */.  0
71760 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c  x02, 0x02, 0x02,
71770 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
71780 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 20  2, 0x02, 0x02,  
71790 2f 2a 20 34 38 2e 2e 34 66 20 20 20 20 48 49 4a  /* 48..4f    HIJ
717a0 4b 4c 4d 4e 4f 20 2a 2f 0a 20 20 30 78 30 32 2c  KLMNO */.  0x02,
717b0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
717c0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
717d0 78 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 35  x02, 0x02,  /* 5
717e0 30 2e 2e 35 37 20 20 20 20 50 51 52 53 54 55 56  0..57    PQRSTUV
717f0 57 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30  W */.  0x02, 0x0
71800 32 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  2, 0x02, 0x00, 0
71810 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71820 20 30 78 30 30 2c 20 20 2f 2a 20 35 38 2e 2e 35   0x00,  /* 58..5
71830 66 20 20 20 20 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f  f    XYZ[\]^_ */
71840 0a 20 20 30 78 30 30 2c 20 30 78 32 61 2c 20 30  .  0x00, 0x2a, 0
71850 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c  x2a, 0x2a, 0x2a,
71860 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32   0x2a, 0x2a, 0x2
71870 32 2c 20 20 2f 2a 20 36 30 2e 2e 36 37 20 20 20  2,  /* 60..67   
71880 20 60 61 62 63 64 65 66 67 20 2a 2f 0a 20 20 30   `abcdefg */.  0
71890 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c  x22, 0x22, 0x22,
718a0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
718b0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 20  2, 0x22, 0x22,  
718c0 2f 2a 20 36 38 2e 2e 36 66 20 20 20 20 68 69 6a  /* 68..6f    hij
718d0 6b 6c 6d 6e 6f 20 2a 2f 0a 20 20 30 78 32 32 2c  klmno */.  0x22,
718e0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
718f0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30  2, 0x22, 0x22, 0
71900 78 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 37  x22, 0x22,  /* 7
71910 30 2e 2e 37 37 20 20 20 20 70 71 72 73 74 75 76  0..77    pqrstuv
71920 77 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32  w */.  0x22, 0x2
71930 32 2c 20 30 78 32 32 2c 20 30 78 30 30 2c 20 30  2, 0x22, 0x00, 0
71940 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71950 20 30 78 30 30 2c 20 20 2f 2a 20 37 38 2e 2e 37   0x00,  /* 78..7
71960 66 20 20 20 20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f  f    xyz{|}~. */
71970 0a 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  ..  0x00, 0x00, 
71980 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71990 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
719a0 30 30 2c 20 20 2f 2a 20 38 30 2e 2e 38 37 20 20  00,  /* 80..87  
719b0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
719c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
719d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
719e0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
719f0 20 2f 2a 20 38 38 2e 2e 38 66 20 20 20 20 2e 2e   /* 88..8f    ..
71a00 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
71a10 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71a20 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71a30 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
71a40 39 30 2e 2e 39 37 20 20 20 20 2e 2e 2e 2e 2e 2e  90..97    ......
71a50 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
71a60 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71a70 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71a80 2c 20 30 78 30 30 2c 20 20 2f 2a 20 39 38 2e 2e  , 0x00,  /* 98..
71a90 39 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  9f    ........ *
71aa0 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
71ab0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71ac0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71ad0 30 30 2c 20 20 2f 2a 20 61 30 2e 2e 61 37 20 20  00,  /* a0..a7  
71ae0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
71af0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71b00 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71b10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71b20 20 2f 2a 20 61 38 2e 2e 61 66 20 20 20 20 2e 2e   /* a8..af    ..
71b30 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
71b40 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71b50 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71b60 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
71b70 62 30 2e 2e 62 37 20 20 20 20 2e 2e 2e 2e 2e 2e  b0..b7    ......
71b80 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
71b90 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71ba0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71bb0 2c 20 30 78 30 30 2c 20 20 2f 2a 20 62 38 2e 2e  , 0x00,  /* b8..
71bc0 62 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  bf    ........ *
71bd0 2f 0a 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c  /..  0x00, 0x00,
71be0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71bf0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71c00 78 30 30 2c 20 20 2f 2a 20 63 30 2e 2e 63 37 20  x00,  /* c0..c7 
71c10 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
71c20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71c30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71c40 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71c50 20 20 2f 2a 20 63 38 2e 2e 63 66 20 20 20 20 2e    /* c8..cf    .
71c60 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30  ....... */.  0x0
71c70 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71c80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71c90 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
71ca0 20 64 30 2e 2e 64 37 20 20 20 20 2e 2e 2e 2e 2e   d0..d7    .....
71cb0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  ... */.  0x00, 0
71cc0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71cd0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71ce0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 64 38 2e  0, 0x00,  /* d8.
71cf0 2e 64 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .df    ........ 
71d00 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c  */.  0x00, 0x00,
71d10 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71d20 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71d30 78 30 30 2c 20 20 2f 2a 20 65 30 2e 2e 65 37 20  x00,  /* e0..e7 
71d40 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
71d50 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71d60 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71d70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71d80 20 20 2f 2a 20 65 38 2e 2e 65 66 20 20 20 20 2e    /* e8..ef    .
71d90 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30  ....... */.  0x0
71da0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71db0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71dc0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
71dd0 20 66 30 2e 2e 66 37 20 20 20 20 2e 2e 2e 2e 2e   f0..f7    .....
71de0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  ... */.  0x00, 0
71df0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71e00 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71e10 30 2c 20 30 78 30 30 20 20 20 2f 2a 20 66 38 2e  0, 0x00   /* f8.
71e20 2e 66 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .ff    ........ 
71e30 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 0a 0a  */.};.#endif....
71e40 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
71e50 69 6e 67 20 73 69 6e 67 6c 65 74 6f 6e 20 63 6f  ing singleton co
71e60 6e 74 61 69 6e 73 20 74 68 65 20 67 6c 6f 62 61  ntains the globa
71e70 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  l configuration 
71e80 66 6f 72 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74  for.** the SQLit
71e90 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 53 51  e library..*/.SQ
71ea0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c  LITE_PRIVATE SQL
71eb0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53  ITE_WSD struct S
71ec0 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c  qlite3Config sql
71ed0 69 74 65 33 43 6f 6e 66 69 67 20 3d 20 7b 0a 20  ite3Config = {. 
71ee0 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54    SQLITE_DEFAULT
71ef0 5f 4d 45 4d 53 54 41 54 55 53 2c 20 20 2f 2a 20  _MEMSTATUS,  /* 
71f00 62 4d 65 6d 73 74 61 74 20 2a 2f 0a 20 20 20 31  bMemstat */.   1
71f10 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
71f20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 62 43 6f            /* bCo
71f30 72 65 4d 75 74 65 78 20 2a 2f 0a 20 20 20 53 51  reMutex */.   SQ
71f40 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d  LITE_THREADSAFE=
71f50 3d 31 2c 20 20 20 20 20 20 2f 2a 20 62 46 75 6c  =1,      /* bFul
71f60 6c 4d 75 74 65 78 20 2a 2f 0a 20 20 20 30 78 37  lMutex */.   0x7
71f70 66 66 66 66 66 66 65 2c 20 20 20 20 20 20 20 20  ffffffe,        
71f80 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 53 74 72          /* mxStr
71f90 6c 65 6e 20 2a 2f 0a 20 20 20 31 30 30 2c 20 20  len */.   100,  
71fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71fb0 20 20 20 20 20 2f 2a 20 73 7a 4c 6f 6f 6b 61 73       /* szLookas
71fc0 69 64 65 20 2a 2f 0a 20 20 20 35 30 30 2c 20 20  ide */.   500,  
71fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71fe0 20 20 20 20 20 2f 2a 20 6e 4c 6f 6f 6b 61 73 69       /* nLookasi
71ff0 64 65 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c  de */.   {0,0,0,
72000 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20  0,0,0,0,0},     
72010 20 20 20 20 2f 2a 20 6d 20 2a 2f 0a 20 20 20 7b      /* m */.   {
72020 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c  0,0,0,0,0,0,0,0,
72030 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 6d 75 74  0},       /* mut
72040 65 78 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c  ex */.   {0,0,0,
72050 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d  0,0,0,0,0,0,0,0}
72060 2c 20 20 20 2f 2a 20 70 63 61 63 68 65 20 2a 2f  ,   /* pcache */
72070 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20  .   (void*)0,   
72080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
72090 2a 20 70 48 65 61 70 20 2a 2f 0a 20 20 20 30 2c  * pHeap */.   0,
720a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
720b0 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 48 65 61           /* nHea
720c0 70 20 2a 2f 0a 20 20 20 30 2c 20 30 2c 20 20 20  p */.   0, 0,   
720d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
720e0 20 20 20 2f 2a 20 6d 6e 48 65 61 70 2c 20 6d 78     /* mnHeap, mx
720f0 48 65 61 70 20 2a 2f 0a 20 20 20 28 76 6f 69 64  Heap */.   (void
72100 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  *)0,            
72110 20 20 20 20 20 20 2f 2a 20 70 53 63 72 61 74 63        /* pScratc
72120 68 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  h */.   0,      
72130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72140 20 20 20 2f 2a 20 73 7a 53 63 72 61 74 63 68 20     /* szScratch 
72150 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20  */.   0,        
72160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72170 20 2f 2a 20 6e 53 63 72 61 74 63 68 20 2a 2f 0a   /* nScratch */.
72180 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20     (void*)0,    
72190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
721a0 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20   pPage */.   0, 
721b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
721c0 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 50 61 67          /* szPag
721d0 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  e */.   0,      
721e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
721f0 20 20 20 2f 2a 20 6e 50 61 67 65 20 2a 2f 0a 20     /* nPage */. 
72200 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
72210 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
72220 6d 78 50 61 72 73 65 72 53 74 61 63 6b 20 2a 2f  mxParserStack */
72230 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
72240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
72250 2a 20 73 68 61 72 65 64 43 61 63 68 65 45 6e 61  * sharedCacheEna
72260 62 6c 65 64 20 2a 2f 0a 20 20 20 2f 2a 20 41 6c  bled */.   /* Al
72270 6c 20 74 68 65 20 72 65 73 74 20 6e 65 65 64 20  l the rest need 
72280 74 6f 20 61 6c 77 61 79 73 20 62 65 20 7a 65 72  to always be zer
72290 6f 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  o */.   0,      
722a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
722b0 20 20 20 2f 2a 20 69 73 49 6e 69 74 20 2a 2f 0a     /* isInit */.
722c0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
722d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
722e0 20 69 6e 50 72 6f 67 72 65 73 73 20 2a 2f 0a 20   inProgress */. 
722f0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
72300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
72310 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 2a 2f 0a  isMallocInit */.
72320 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
72330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
72340 20 70 49 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 20   pInitMutex */. 
72350 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
72360 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
72370 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 20 2a 2f  nRefInitMutex */
72380 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68  .};.../*.** Hash
72390 20 74 61 62 6c 65 20 66 6f 72 20 67 6c 6f 62 61   table for globa
723a0 6c 20 66 75 6e 63 74 69 6f 6e 73 20 2d 20 66 75  l functions - fu
723b0 6e 63 74 69 6f 6e 73 20 63 6f 6d 6d 6f 6e 20 74  nctions common t
723c0 6f 20 61 6c 6c 0a 2a 2a 20 64 61 74 61 62 61 73  o all.** databas
723d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20  e connections.  
723e0 41 66 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61  After initializa
723f0 74 69 6f 6e 2c 20 74 68 69 73 20 74 61 62 6c 65  tion, this table
72400 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79   is.** read-only
72410 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
72420 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46  ATE SQLITE_WSD F
72430 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c 69 74  uncDefHash sqlit
72440 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e  e3GlobalFunction
72450 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61  s;../*.** The va
72460 6c 75 65 20 6f 66 20 74 68 65 20 22 70 65 6e 64  lue of the "pend
72470 69 6e 67 22 20 62 79 74 65 20 6d 75 73 74 20 62  ing" byte must b
72480 65 20 30 78 34 30 30 30 30 30 30 30 20 28 31 20  e 0x40000000 (1 
72490 62 79 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a  byte past the.**
724a0 20 31 2d 67 69 62 61 62 79 74 65 20 62 6f 75 6e   1-gibabyte boun
724b0 64 61 72 79 29 20 69 6e 20 61 20 63 6f 6d 70 61  dary) in a compa
724c0 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 2e 20  tible database. 
724d0 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 75 73   SQLite never us
724e0 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  es.** the databa
724f0 73 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e  se page that con
72500 74 61 69 6e 73 20 74 68 65 20 70 65 6e 64 69 6e  tains the pendin
72510 67 20 62 79 74 65 2e 20 20 49 74 20 6e 65 76 65  g byte.  It neve
72520 72 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f  r attempts.** to
72530 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74   read or write t
72540 68 61 74 20 70 61 67 65 2e 20 20 54 68 65 20 70  hat page.  The p
72550 65 6e 64 69 6e 67 20 62 79 74 65 20 70 61 67 65  ending byte page
72560 20 69 73 20 73 65 74 20 61 73 73 69 67 6e 0a 2a   is set assign.*
72570 2a 20 66 6f 72 20 75 73 65 20 62 79 20 74 68 65  * for use by the
72580 20 56 46 53 20 6c 61 79 65 72 73 20 61 73 20 73   VFS layers as s
72590 70 61 63 65 20 66 6f 72 20 6d 61 6e 61 67 69 6e  pace for managin
725a0 67 20 66 69 6c 65 20 6c 6f 63 6b 73 2e 0a 2a 2a  g file locks..**
725b0 0a 2a 2a 20 44 75 72 69 6e 67 20 74 65 73 74 69  .** During testi
725c0 6e 67 2c 20 69 74 20 69 73 20 6f 66 74 65 6e 20  ng, it is often 
725d0 64 65 73 69 72 61 62 6c 65 20 74 6f 20 6d 6f 76  desirable to mov
725e0 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79  e the pending by
725f0 74 65 20 74 6f 0a 2a 2a 20 61 20 64 69 66 66 65  te to.** a diffe
72600 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e  rent position in
72610 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 69 73   the file.  This
72620 20 61 6c 6c 6f 77 73 20 63 6f 64 65 20 74 68 61   allows code tha
72630 74 20 68 61 73 20 74 6f 0a 2a 2a 20 64 65 61 6c  t has to.** deal
72640 20 77 69 74 68 20 74 68 65 20 70 65 6e 64 69 6e   with the pendin
72650 67 20 62 79 74 65 20 74 6f 20 72 75 6e 20 6f 6e  g byte to run on
72660 20 66 69 6c 65 73 20 74 68 61 74 20 61 72 65 20   files that are 
72670 6d 75 63 68 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20  much smaller.** 
72680 74 68 61 6e 20 31 20 47 69 42 2e 20 20 54 68 65  than 1 GiB.  The
72690 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
726a0 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63  ntrol() interfac
726b0 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  e can be used to
726c0 0a 2a 2a 20 6d 6f 76 65 20 74 68 65 20 70 65 6e  .** move the pen
726d0 64 69 6e 67 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a  ding byte..**.**
726e0 20 49 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61   IMPORTANT:  Cha
726f0 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e  nging the pendin
72700 67 20 62 79 74 65 20 74 6f 20 61 6e 79 20 76 61  g byte to any va
72710 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 0a 2a  lue other than.*
72720 2a 20 30 78 34 30 30 30 30 30 30 30 20 72 65 73  * 0x40000000 res
72730 75 6c 74 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6d  ults in an incom
72740 70 61 74 69 62 6c 65 20 64 61 74 61 62 61 73 65  patible database
72750 20 66 69 6c 65 20 66 6f 72 6d 61 74 21 0a 2a 2a   file format!.**
72760 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65   Changing the pe
72770 6e 64 69 6e 67 20 62 79 74 65 20 64 75 72 69 6e  nding byte durin
72780 67 20 6f 70 65 72 61 74 69 6e 67 20 72 65 73 75  g operating resu
72790 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64  lts in undefined
727a0 0a 2a 2a 20 61 6e 64 20 64 69 6c 65 74 65 72 69  .** and dileteri
727b0 6f 75 73 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f  ous behavior..*/
727c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
727d0 69 6e 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69  int sqlite3Pendi
727e0 6e 67 42 79 74 65 20 3d 20 30 78 34 30 30 30 30  ngByte = 0x40000
727f0 30 30 30 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  000;../*********
72800 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 67 6c 6f  ***** End of glo
72810 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  bal.c **********
72820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72840 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
72850 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
72860 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a   status.c ******
72870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72890 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
728a0 20 4a 75 6e 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54   June 18.**.** T
728b0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
728c0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
728d0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
728e0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
728f0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
72900 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
72910 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
72920 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
72930 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
72940 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
72950 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
72960 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
72970 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
72980 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
72990 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
729a0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
729b0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
729c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
729d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
729e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
729f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72a00 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
72a10 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d  is module implem
72a20 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74 65 33  ents the sqlite3
72a30 5f 73 74 61 74 75 73 28 29 20 69 6e 74 65 72 66  _status() interf
72a40 61 63 65 20 61 6e 64 20 72 65 6c 61 74 65 64 0a  ace and related.
72a50 2a 2a 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  ** functionality
72a60 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 73 74 61  ..**.** $Id: sta
72a70 74 75 73 2e 63 2c 76 20 31 2e 39 20 32 30 30 38  tus.c,v 1.9 2008
72a80 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 20  /09/02 00:52:52 
72a90 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
72aa0 0a 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 69 6e  .** Variables in
72ab0 20 77 68 69 63 68 20 74 6f 20 72 65 63 6f 72 64   which to record
72ac0 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74   status informat
72ad0 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ion..*/.typedef 
72ae0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74  struct sqlite3St
72af0 61 74 54 79 70 65 20 73 71 6c 69 74 65 33 53 74  atType sqlite3St
72b00 61 74 54 79 70 65 3b 0a 73 74 61 74 69 63 20 53  atType;.static S
72b10 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
72b20 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65   sqlite3StatType
72b30 20 7b 0a 20 20 69 6e 74 20 6e 6f 77 56 61 6c 75   {.  int nowValu
72b40 65 5b 39 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a  e[9];         /*
72b50 20 43 75 72 72 65 6e 74 20 76 61 6c 75 65 20 2a   Current value *
72b60 2f 0a 20 20 69 6e 74 20 6d 78 56 61 6c 75 65 5b  /.  int mxValue[
72b70 39 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  9];          /* 
72b80 4d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 2a 2f  Maximum value */
72b90 0a 7d 20 73 71 6c 69 74 65 33 53 74 61 74 20 3d  .} sqlite3Stat =
72ba0 20 7b 20 7b 30 2c 7d 2c 20 7b 30 2c 7d 20 7d 3b   { {0,}, {0,} };
72bb0 0a 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 53 74  .../* The "wsdSt
72bc0 61 74 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72  at" macro will r
72bd0 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 73 74  esolve to the st
72be0 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  atus information
72bf0 0a 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72  .** state vector
72c00 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73  .  If writable s
72c10 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e  tatic data is un
72c20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65  supported on the
72c30 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68   target,.** we h
72c40 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  ave to locate th
72c50 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61  e state vector a
72c60 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20  t run-time.  In 
72c70 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a  the more common.
72c80 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72  ** case where wr
72c90 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
72ca0 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c  ta is supported,
72cb0 20 77 73 64 53 74 61 74 20 63 61 6e 20 72 65 66   wsdStat can ref
72cc0 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74  er directly.** t
72cd0 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 53 74  o the "sqlite3St
72ce0 61 74 22 20 73 74 61 74 65 20 76 65 63 74 6f 72  at" state vector
72cf0 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e   declared above.
72d00 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
72d10 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66  E_OMIT_WSD.# def
72d20 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74 20  ine wsdStatInit 
72d30 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65   sqlite3StatType
72d40 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 73 71   *x = &GLOBAL(sq
72d50 6c 69 74 65 33 53 74 61 74 54 79 70 65 2c 73 71  lite3StatType,sq
72d60 6c 69 74 65 33 53 74 61 74 29 0a 23 20 64 65 66  lite3Stat).# def
72d70 69 6e 65 20 77 73 64 53 74 61 74 20 78 5b 30 5d  ine wsdStat x[0]
72d80 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
72d90 77 73 64 53 74 61 74 49 6e 69 74 0a 23 20 64 65  wsdStatInit.# de
72da0 66 69 6e 65 20 77 73 64 53 74 61 74 20 73 71 6c  fine wsdStat sql
72db0 69 74 65 33 53 74 61 74 0a 23 65 6e 64 69 66 0a  ite3Stat.#endif.
72dc0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
72dd0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
72de0 6f 66 20 61 20 73 74 61 74 75 73 20 70 61 72 61  of a status para
72df0 6d 65 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  meter..*/.SQLITE
72e00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
72e10 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28  ite3StatusValue(
72e20 69 6e 74 20 6f 70 29 7b 0a 20 20 77 73 64 53 74  int op){.  wsdSt
72e30 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74  atInit;.  assert
72e40 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72  ( op>=0 && op<Ar
72e50 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e  raySize(wsdStat.
72e60 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 72  nowValue) );.  r
72e70 65 74 75 72 6e 20 77 73 64 53 74 61 74 2e 6e 6f  eturn wsdStat.no
72e80 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 7d 0a 0a 2f  wValue[op];.}../
72e90 2a 0a 2a 2a 20 41 64 64 20 4e 20 74 6f 20 74 68  *.** Add N to th
72ea0 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61  e value of a sta
72eb0 74 75 73 20 72 65 63 6f 72 64 2e 20 20 49 74 20  tus record.  It 
72ec0 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  is assumed that 
72ed0 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 68 6f  the.** caller ho
72ee0 6c 64 73 20 61 70 70 72 6f 70 72 69 61 74 65 20  lds appropriate 
72ef0 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  locks..*/.SQLITE
72f00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
72f10 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 69  lite3StatusAdd(i
72f20 6e 74 20 6f 70 2c 20 69 6e 74 20 4e 29 7b 0a 20  nt op, int N){. 
72f30 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20   wsdStatInit;.  
72f40 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26  assert( op>=0 &&
72f50 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73   op<ArraySize(ws
72f60 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20  dStat.nowValue) 
72f70 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77  );.  wsdStat.now
72f80 56 61 6c 75 65 5b 6f 70 5d 20 2b 3d 20 4e 3b 0a  Value[op] += N;.
72f90 20 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f    if( wsdStat.no
72fa0 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74  wValue[op]>wsdSt
72fb0 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29  at.mxValue[op] )
72fc0 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78  {.    wsdStat.mx
72fd0 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53  Value[op] = wsdS
72fe0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
72ff0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  ;.  }.}../*.** S
73000 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  et the value of 
73010 61 20 73 74 61 74 75 73 20 74 6f 20 58 2e 0a 2a  a status to X..*
73020 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
73030 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61   void sqlite3Sta
73040 74 75 73 53 65 74 28 69 6e 74 20 6f 70 2c 20 69  tusSet(int op, i
73050 6e 74 20 58 29 7b 0a 20 20 77 73 64 53 74 61 74  nt X){.  wsdStat
73060 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20  Init;.  assert( 
73070 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61  op>=0 && op<Arra
73080 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f  ySize(wsdStat.no
73090 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64  wValue) );.  wsd
730a0 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70  Stat.nowValue[op
730b0 5d 20 3d 20 58 3b 0a 20 20 69 66 28 20 77 73 64  ] = X;.  if( wsd
730c0 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70  Stat.nowValue[op
730d0 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75  ]>wsdStat.mxValu
730e0 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64  e[op] ){.    wsd
730f0 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d  Stat.mxValue[op]
73100 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61   = wsdStat.nowVa
73110 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a  lue[op];.  }.}..
73120 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74  /*.** Query stat
73130 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  us information..
73140 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  **.** This imple
73150 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65  mentation assume
73160 73 20 74 68 61 74 20 72 65 61 64 69 6e 67 20 6f  s that reading o
73170 72 20 77 72 69 74 69 6e 67 20 61 6e 20 61 6c 69  r writing an ali
73180 67 6e 65 64 0a 2a 2a 20 33 32 2d 62 69 74 20 69  gned.** 32-bit i
73190 6e 74 65 67 65 72 20 69 73 20 61 6e 20 61 74 6f  nteger is an ato
731a0 6d 69 63 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  mic operation.  
731b0 49 66 20 74 68 61 74 20 61 73 73 75 6d 70 74 69  If that assumpti
731c0 6f 6e 20 69 73 20 6e 6f 74 20 74 72 75 65 2c 0a  on is not true,.
731d0 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  ** then this rou
731e0 74 69 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 65  tine is not thre
731f0 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54  adsafe..*/.SQLIT
73200 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
73210 33 5f 73 74 61 74 75 73 28 69 6e 74 20 6f 70 2c  3_status(int op,
73220 20 69 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20   int *pCurrent, 
73230 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c  int *pHighwater,
73240 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b   int resetFlag){
73250 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a  .  wsdStatInit;.
73260 20 20 69 66 28 20 6f 70 3c 30 20 7c 7c 20 6f 70    if( op<0 || op
73270 3e 3d 41 72 72 61 79 53 69 7a 65 28 77 73 64 53  >=ArraySize(wsdS
73280 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 7b  tat.nowValue) ){
73290 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
732a0 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
732b0 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 77 73 64   *pCurrent = wsd
732c0 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70  Stat.nowValue[op
732d0 5d 3b 0a 20 20 2a 70 48 69 67 68 77 61 74 65 72  ];.  *pHighwater
732e0 20 3d 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c   = wsdStat.mxVal
732f0 75 65 5b 6f 70 5d 3b 0a 20 20 69 66 28 20 72 65  ue[op];.  if( re
73300 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 77  setFlag ){.    w
73310 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f  sdStat.mxValue[o
73320 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77  p] = wsdStat.now
73330 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 20  Value[op];.  }. 
73340 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
73350 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72  K;.}../*.** Quer
73360 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61  y status informa
73370 74 69 6f 6e 20 66 6f 72 20 61 20 73 69 6e 67 6c  tion for a singl
73380 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
73390 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ction.*/.SQLITE_
733a0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
733b0 64 62 5f 73 74 61 74 75 73 28 0a 20 20 73 71 6c  db_status(.  sql
733c0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
733d0 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
733e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68  se connection wh
733f0 6f 73 65 20 73 74 61 74 75 73 20 69 73 20 64 65  ose status is de
73400 73 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  sired */.  int o
73410 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
73420 20 2f 2a 20 53 74 61 74 75 73 20 76 65 72 62 20   /* Status verb 
73430 2a 2f 0a 20 20 69 6e 74 20 2a 70 43 75 72 72 65  */.  int *pCurre
73440 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72  nt,        /* Wr
73450 69 74 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  ite current valu
73460 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  e here */.  int 
73470 2a 70 48 69 67 68 77 61 74 65 72 2c 20 20 20 20  *pHighwater,    
73480 20 20 2f 2a 20 57 72 69 74 65 20 68 69 67 68 2d    /* Write high-
73490 77 61 74 65 72 20 6d 61 72 6b 20 68 65 72 65 20  water mark here 
734a0 2a 2f 0a 20 20 69 6e 74 20 72 65 73 65 74 46 6c  */.  int resetFl
734b0 61 67 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  ag         /* Re
734c0 73 65 74 20 68 69 67 68 2d 77 61 74 65 72 20 6d  set high-water m
734d0 61 72 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  ark if true */.)
734e0 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  {.  switch( op )
734f0 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
73500 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41  E_DBSTATUS_LOOKA
73510 53 49 44 45 5f 55 53 45 44 3a 20 7b 0a 20 20 20  SIDE_USED: {.   
73520 20 20 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 64     *pCurrent = d
73530 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75  b->lookaside.nOu
73540 74 3b 0a 20 20 20 20 20 20 2a 70 48 69 67 68 77  t;.      *pHighw
73550 61 74 65 72 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  ater = db->looka
73560 73 69 64 65 2e 6d 78 4f 75 74 3b 0a 20 20 20 20  side.mxOut;.    
73570 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67 20    if( resetFlag 
73580 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c  ){.        db->l
73590 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d  ookaside.mxOut =
735a0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
735b0 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Out;.      }.   
735c0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
735d0 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
735e0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
735f0 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
73600 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
73610 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  ITE_OK;.}../****
73620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
73630 66 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a  f status.c *****
73640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
73670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
73680 20 66 69 6c 65 20 64 61 74 65 2e 63 20 2a 2a 2a   file date.c ***
73690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
736a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
736b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
736c0 20 32 30 30 33 20 4f 63 74 6f 62 65 72 20 33 31   2003 October 31
736d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
736e0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
736f0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
73700 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
73710 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
73720 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
73730 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
73740 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
73750 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
73760 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
73770 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
73780 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
73790 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
737a0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
737b0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
737c0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
737d0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
737e0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
737f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
73830 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
73840 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
73850 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
73860 6d 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69  ment date and ti
73870 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  me.** functions 
73880 66 6f 72 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a  for SQLite.  .**
73890 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c  .** There is onl
738a0 79 20 6f 6e 65 20 65 78 70 6f 72 74 65 64 20 73  y one exported s
738b0 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73 20 66 69  ymbol in this fi
738c0 6c 65 20 2d 20 74 68 65 20 66 75 6e 63 74 69 6f  le - the functio
738d0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 52 65 67 69  n.** sqlite3Regi
738e0 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63  sterDateTimeFunc
738f0 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74  tions() found at
73900 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74   the bottom of t
73910 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20  he file..** All 
73920 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 66  other code has f
73930 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a  ile scope..**.**
73940 20 24 49 64 3a 20 64 61 74 65 2e 63 2c 76 20 31   $Id: date.c,v 1
73950 2e 31 30 37 20 32 30 30 39 2f 30 35 2f 30 33 20  .107 2009/05/03 
73960 32 30 3a 32 33 3a 35 33 20 64 72 68 20 45 78 70  20:23:53 drh Exp
73970 20 24 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20   $.**.** SQLite 
73980 70 72 6f 63 65 73 73 65 73 20 61 6c 6c 20 74 69  processes all ti
73990 6d 65 73 20 61 6e 64 20 64 61 74 65 73 20 61 73  mes and dates as
739a0 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62   Julian Day numb
739b0 65 72 73 2e 20 20 54 68 65 0a 2a 2a 20 64 61 74  ers.  The.** dat
739c0 65 73 20 61 6e 64 20 74 69 6d 65 73 20 61 72 65  es and times are
739d0 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 20 6e   stored as the n
739e0 75 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69  umber of days si
739f0 6e 63 65 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47  nce noon.** in G
73a00 72 65 65 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65  reenwich on Nove
73a10 6d 62 65 72 20 32 34 2c 20 34 37 31 34 20 42 2e  mber 24, 4714 B.
73a20 43 2e 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  C. according to 
73a30 74 68 65 20 47 72 65 67 6f 72 69 61 6e 0a 2a 2a  the Gregorian.**
73a40 20 63 61 6c 65 6e 64 61 72 20 73 79 73 74 65 6d   calendar system
73a50 2e 20 0a 2a 2a 0a 2a 2a 20 31 39 37 30 2d 30 31  . .**.** 1970-01
73a60 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20  -01 00:00:00 is 
73a70 4a 44 20 32 34 34 30 35 38 37 2e 35 0a 2a 2a 20  JD 2440587.5.** 
73a80 32 30 30 30 2d 30 31 2d 30 31 20 30 30 3a 30 30  2000-01-01 00:00
73a90 3a 30 30 20 69 73 20 4a 44 20 32 34 35 31 35 34  :00 is JD 245154
73aa0 34 2e 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  4.5.**.** This i
73ab0 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75  mplemention requ
73ac0 69 72 65 73 20 79 65 61 72 73 20 74 6f 20 62 65  ires years to be
73ad0 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 20   expressed as a 
73ae0 34 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 0a 2a  4-digit number.*
73af0 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68  * which means th
73b00 61 74 20 6f 6e 6c 79 20 64 61 74 65 73 20 62 65  at only dates be
73b10 74 77 65 65 6e 20 30 30 30 30 2d 30 31 2d 30 31  tween 0000-01-01
73b20 20 61 6e 64 20 39 39 39 39 2d 31 32 2d 33 31 20   and 9999-12-31 
73b30 63 61 6e 0a 2a 2a 20 62 65 20 72 65 70 72 65 73  can.** be repres
73b40 65 6e 74 65 64 2c 20 65 76 65 6e 20 74 68 6f 75  ented, even thou
73b50 67 68 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  gh julian day nu
73b60 6d 62 65 72 73 20 61 6c 6c 6f 77 20 61 20 6d 75  mbers allow a mu
73b70 63 68 20 77 69 64 65 72 0a 2a 2a 20 72 61 6e 67  ch wider.** rang
73b80 65 20 6f 66 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a  e of dates..**.*
73b90 2a 20 54 68 65 20 47 72 65 67 6f 72 69 61 6e 20  * The Gregorian 
73ba0 63 61 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 20  calendar system 
73bb0 69 73 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20  is used for all 
73bc0 64 61 74 65 73 20 61 6e 64 20 74 69 6d 65 73 2c  dates and times,
73bd0 0a 2a 2a 20 65 76 65 6e 20 74 68 6f 73 65 20 74  .** even those t
73be0 68 61 74 20 70 72 65 64 61 74 65 20 74 68 65 20  hat predate the 
73bf0 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64  Gregorian calend
73c00 61 72 2e 20 20 48 69 73 74 6f 72 69 61 6e 73 20  ar.  Historians 
73c10 75 73 75 61 6c 6c 79 0a 2a 2a 20 75 73 65 20 74  usually.** use t
73c20 68 65 20 4a 75 6c 69 61 6e 20 63 61 6c 65 6e 64  he Julian calend
73c30 61 72 20 66 6f 72 20 64 61 74 65 73 20 70 72 69  ar for dates pri
73c40 6f 72 20 74 6f 20 31 35 38 32 2d 31 30 2d 31 35  or to 1582-10-15
73c50 20 61 6e 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a   and for some.**
73c60 20 64 61 74 65 73 20 61 66 74 65 72 77 61 72 64   dates afterward
73c70 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  s, depending on 
73c80 6c 6f 63 61 6c 65 2e 20 20 42 65 77 61 72 65 20  locale.  Beware 
73c90 6f 66 20 74 68 69 73 20 64 69 66 66 65 72 65 6e  of this differen
73ca0 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ce..**.** The co
73cb0 6e 76 65 72 73 69 6f 6e 20 61 6c 67 6f 72 69 74  nversion algorit
73cc0 68 6d 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e  hms are implemen
73cd0 74 65 64 20 62 61 73 65 64 20 6f 6e 20 64 65 73  ted based on des
73ce0 63 72 69 70 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20  criptions.** in 
73cf0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65  the following te
73d00 78 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a  xt:.**.**      J
73d10 65 61 6e 20 4d 65 65 75 73 0a 2a 2a 20 20 20 20  ean Meeus.**    
73d20 20 20 41 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41    Astronomical A
73d30 6c 67 6f 72 69 74 68 6d 73 2c 20 32 6e 64 20 45  lgorithms, 2nd E
73d40 64 69 74 69 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20  dition, 1998.** 
73d50 20 20 20 20 20 49 53 42 4d 20 30 2d 39 34 33 33       ISBM 0-9433
73d60 39 36 2d 36 31 2d 31 0a 2a 2a 20 20 20 20 20 20  96-61-1.**      
73d70 57 69 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49  Willmann-Bell, I
73d80 6e 63 0a 2a 2a 20 20 20 20 20 20 52 69 63 68 6d  nc.**      Richm
73d90 6f 6e 64 2c 20 56 69 72 67 69 6e 69 61 20 28 55  ond, Virginia (U
73da0 53 41 29 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20  SA).*/.#include 
73db0 3c 74 69 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65  <time.h>..#ifnde
73dc0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41  f SQLITE_OMIT_DA
73dd0 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a  TETIME_FUNCS../*
73de0 0a 2a 2a 20 4f 6e 20 72 65 63 65 6e 74 20 57 69  .** On recent Wi
73df0 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c  ndows platforms,
73e00 20 74 68 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73   the localtime_s
73e10 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  () function is a
73e20 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70  vailable.** as p
73e30 61 72 74 20 6f 66 20 74 68 65 20 22 53 65 63 75  art of the "Secu
73e40 72 65 20 43 52 54 22 2e 20 49 74 20 69 73 20 65  re CRT". It is e
73e50 73 73 65 6e 74 69 61 6c 6c 79 20 65 71 75 69 76  ssentially equiv
73e60 61 6c 65 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63  alent to .** loc
73e70 61 6c 74 69 6d 65 5f 72 28 29 20 61 76 61 69 6c  altime_r() avail
73e80 61 62 6c 65 20 75 6e 64 65 72 20 6d 6f 73 74 20  able under most 
73e90 50 4f 53 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c  POSIX platforms,
73ea0 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
73eb0 20 0a 2a 2a 20 6f 72 64 65 72 20 6f 66 20 74 68   .** order of th
73ec0 65 20 70 61 72 61 6d 65 74 65 72 73 20 69 73 20  e parameters is 
73ed0 72 65 76 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  reversed..**.** 
73ee0 53 65 65 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e  See http://msdn.
73ef0 6d 69 63 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e  microsoft.com/en
73f00 2d 75 73 2f 6c 69 62 72 61 72 79 2f 61 34 34 32  -us/library/a442
73f10 78 33 79 65 28 56 53 2e 38 30 29 2e 61 73 70 78  x3ye(VS.80).aspx
73f20 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75  ..**.** If the u
73f30 73 65 72 20 68 61 73 20 6e 6f 74 20 69 6e 64 69  ser has not indi
73f40 63 61 74 65 64 20 74 6f 20 75 73 65 20 6c 6f 63  cated to use loc
73f50 61 6c 74 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f  altime_r() or lo
73f60 63 61 6c 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61  caltime_s().** a
73f70 6c 72 65 61 64 79 2c 20 63 68 65 63 6b 20 66 6f  lready, check fo
73f80 72 20 61 6e 20 4d 53 56 43 20 62 75 69 6c 64 20  r an MSVC build 
73f90 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74  environment that
73fa0 20 70 72 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f   provides .** lo
73fb0 63 61 6c 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a  caltime_s()..*/.
73fc0 23 69 66 20 21 64 65 66 69 6e 65 64 28 48 41 56  #if !defined(HAV
73fd0 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26  E_LOCALTIME_R) &
73fe0 26 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f  & !defined(HAVE_
73ff0 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20  LOCALTIME_S) && 
74000 5c 0a 20 20 20 20 20 64 65 66 69 6e 65 64 28 5f  \.     defined(_
74010 4d 53 43 5f 56 45 52 29 20 26 26 20 64 65 66 69  MSC_VER) && defi
74020 6e 65 64 28 5f 43 52 54 5f 49 4e 53 45 43 55 52  ned(_CRT_INSECUR
74030 45 5f 44 45 50 52 45 43 41 54 45 29 0a 23 64 65  E_DEPRECATE).#de
74040 66 69 6e 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54  fine HAVE_LOCALT
74050 49 4d 45 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a  IME_S 1.#endif..
74060 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72  /*.** A structur
74070 65 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20  e for holding a 
74080 73 69 6e 67 6c 65 20 64 61 74 65 20 61 6e 64 20  single date and 
74090 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  time..*/.typedef
740a0 20 73 74 72 75 63 74 20 44 61 74 65 54 69 6d 65   struct DateTime
740b0 20 44 61 74 65 54 69 6d 65 3b 0a 73 74 72 75 63   DateTime;.struc
740c0 74 20 44 61 74 65 54 69 6d 65 20 7b 0a 20 20 73  t DateTime {.  s
740d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4a 44  qlite3_int64 iJD
740e0 3b 20 2f 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20  ; /* The julian 
740f0 64 61 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73  day number times
74100 20 38 36 34 30 30 30 30 30 20 2a 2f 0a 20 20 69   86400000 */.  i
74110 6e 74 20 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20  nt Y, M, D;     
74120 20 20 2f 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68    /* Year, month
74130 2c 20 61 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69  , and day */.  i
74140 6e 74 20 68 2c 20 6d 3b 20 20 20 20 20 20 20 20  nt h, m;        
74150 20 20 2f 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69    /* Hour and mi
74160 6e 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74  nutes */.  int t
74170 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  z;            /*
74180 20 54 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74   Timezone offset
74190 20 69 6e 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20   in minutes */. 
741a0 20 64 6f 75 62 6c 65 20 73 3b 20 20 20 20 20 20   double s;      
741b0 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a      /* Seconds *
741c0 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 59 4d  /.  char validYM
741d0 44 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28  D;     /* True (
741e0 31 29 20 69 66 20 59 2c 4d 2c 44 20 61 72 65 20  1) if Y,M,D are 
741f0 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20  valid */.  char 
74200 76 61 6c 69 64 48 4d 53 3b 20 20 20 20 20 2f 2a  validHMS;     /*
74210 20 54 72 75 65 20 28 31 29 20 69 66 20 68 2c 6d   True (1) if h,m
74220 2c 73 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a  ,s are valid */.
74230 20 20 63 68 61 72 20 76 61 6c 69 64 4a 44 3b 20    char validJD; 
74240 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29       /* True (1)
74250 20 69 66 20 69 4a 44 20 69 73 20 76 61 6c 69 64   if iJD is valid
74260 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64   */.  char valid
74270 54 5a 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  TZ;      /* True
74280 20 28 31 29 20 69 66 20 74 7a 20 69 73 20 76 61   (1) if tz is va
74290 6c 69 64 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a  lid */.};.../*.*
742a0 2a 20 43 6f 6e 76 65 72 74 20 7a 44 61 74 65 20  * Convert zDate 
742b0 69 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  into one or more
742c0 20 69 6e 74 65 67 65 72 73 2e 20 20 41 64 64 69   integers.  Addi
742d0 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73  tional arguments
742e0 0a 2a 2a 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75  .** come in grou
742f0 70 73 20 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f  ps of 5 as follo
74300 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ws:.**.**       
74310 4e 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f  N       number o
74320 66 20 64 69 67 69 74 73 20 69 6e 20 74 68 65 20  f digits in the 
74330 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
74340 20 6d 69 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d   min     minimum
74350 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f   allowed value o
74360 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a  f the integer.**
74370 20 20 20 20 20 20 20 6d 61 78 20 20 20 20 20 6d         max     m
74380 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76  aximum allowed v
74390 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65  alue of the inte
743a0 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78  ger.**       nex
743b0 74 43 20 20 20 66 69 72 73 74 20 63 68 61 72 61  tC   first chara
743c0 63 74 65 72 20 61 66 74 65 72 20 74 68 65 20 69  cter after the i
743d0 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20  nteger.**       
743e0 70 56 61 6c 20 20 20 20 77 68 65 72 65 20 74 6f  pVal    where to
743f0 20 77 72 69 74 65 20 74 68 65 20 69 6e 74 65 67   write the integ
74400 65 72 73 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  ers value..**.**
74410 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e   Conversions con
74420 74 69 6e 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20  tinue until one 
74430 77 69 74 68 20 6e 65 78 74 43 3d 3d 30 20 69 73  with nextC==0 is
74440 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a   encountered..**
74450 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65   The function re
74460 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
74470 20 6f 66 20 73 75 63 63 65 73 73 66 75 6c 20 63   of successful c
74480 6f 6e 76 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73  onversions..*/.s
74490 74 61 74 69 63 20 69 6e 74 20 67 65 74 44 69 67  tatic int getDig
744a0 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  its(const char *
744b0 7a 44 61 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76  zDate, ...){.  v
744c0 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74  a_list ap;.  int
744d0 20 76 61 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20   val;.  int N;. 
744e0 20 69 6e 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20   int min;.  int 
744f0 6d 61 78 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43  max;.  int nextC
74500 3b 0a 20 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20  ;.  int *pVal;. 
74510 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
74520 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 44 61  va_start(ap, zDa
74530 74 65 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e  te);.  do{.    N
74540 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
74550 74 29 3b 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61  t);.    min = va
74560 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
74570 20 20 20 6d 61 78 20 3d 20 76 61 5f 61 72 67 28     max = va_arg(
74580 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65  ap, int);.    ne
74590 78 74 43 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  xtC = va_arg(ap,
745a0 20 69 6e 74 29 3b 0a 20 20 20 20 70 56 61 6c 20   int);.    pVal 
745b0 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
745c0 2a 29 3b 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b  *);.    val = 0;
745d0 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20  .    while( N-- 
745e0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 73 71  ){.      if( !sq
745f0 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44  lite3Isdigit(*zD
74600 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ate) ){.        
74610 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69  goto end_getDigi
74620 74 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ts;.      }.    
74630 20 20 76 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b    val = val*10 +
74640 20 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a 20   *zDate - '0';. 
74650 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20       zDate++;.  
74660 20 20 7d 0a 20 20 20 20 69 66 28 20 76 61 6c 3c    }.    if( val<
74670 6d 69 6e 20 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c  min || val>max |
74680 7c 20 28 6e 65 78 74 43 21 3d 30 20 26 26 20 6e  | (nextC!=0 && n
74690 65 78 74 43 21 3d 2a 7a 44 61 74 65 29 20 29 7b  extC!=*zDate) ){
746a0 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
746b0 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20 7d  getDigits;.    }
746c0 0a 20 20 20 20 2a 70 56 61 6c 20 3d 20 76 61 6c  .    *pVal = val
746d0 3b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20  ;.    zDate++;. 
746e0 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69     cnt++;.  }whi
746f0 6c 65 28 20 6e 65 78 74 43 20 29 3b 0a 65 6e 64  le( nextC );.end
74700 5f 67 65 74 44 69 67 69 74 73 3a 0a 20 20 76 61  _getDigits:.  va
74710 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75  _end(ap);.  retu
74720 72 6e 20 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn cnt;.}../*.**
74730 20 52 65 61 64 20 74 65 78 74 20 66 72 6f 6d 20   Read text from 
74740 7a 5b 5d 20 61 6e 64 20 63 6f 6e 76 65 72 74 20  z[] and convert 
74750 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20  into a floating 
74760 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 20 20 52  point number.  R
74770 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d  eturn.** the num
74780 62 65 72 20 6f 66 20 64 69 67 69 74 73 20 63 6f  ber of digits co
74790 6e 76 65 72 74 65 64 2e 0a 2a 2f 0a 23 64 65 66  nverted..*/.#def
747a0 69 6e 65 20 67 65 74 56 61 6c 75 65 20 73 71 6c  ine getValue sql
747b0 69 74 65 33 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20  ite3AtoF../*.** 
747c0 50 61 72 73 65 20 61 20 74 69 6d 65 7a 6f 6e 65  Parse a timezone
747d0 20 65 78 74 65 6e 73 69 6f 6e 20 6f 6e 20 74 68   extension on th
747e0 65 20 65 6e 64 20 6f 66 20 61 20 64 61 74 65 2d  e end of a date-
747f0 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74  time..** The ext
74800 65 6e 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65  ension is of the
74810 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   form:.**.**    
74820 20 20 20 20 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a      (+/-)HH:MM.*
74830 2a 0a 2a 2a 20 4f 72 20 74 68 65 20 22 7a 75 6c  *.** Or the "zul
74840 75 22 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a  u" notation:.**.
74850 2a 2a 20 20 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a  **        Z.**.*
74860 2a 20 49 66 20 74 68 65 20 70 61 72 73 65 20 69  * If the parse i
74870 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 77 72  s successful, wr
74880 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
74890 66 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 6f 66 20  f minutes.** of 
748a0 63 68 61 6e 67 65 20 69 6e 20 70 2d 3e 74 7a 20  change in p->tz 
748b0 61 6e 64 20 72 65 74 75 72 6e 20 30 2e 20 20 49  and return 0.  I
748c0 66 20 61 20 70 61 72 73 65 72 20 65 72 72 6f 72  f a parser error
748d0 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 72 65 74 75   occurs,.** retu
748e0 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a  rn non-zero..**.
748f0 2a 2a 20 41 20 6d 69 73 73 69 6e 67 20 73 70 65  ** A missing spe
74900 63 69 66 69 65 72 20 69 73 20 6e 6f 74 20 63 6f  cifier is not co
74910 6e 73 69 64 65 72 65 64 20 61 6e 20 65 72 72 6f  nsidered an erro
74920 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
74930 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 63   parseTimezone(c
74940 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65  onst char *zDate
74950 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  , DateTime *p){.
74960 20 20 69 6e 74 20 73 67 6e 20 3d 20 30 3b 0a 20    int sgn = 0;. 
74970 20 69 6e 74 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20   int nHr, nMn;. 
74980 20 69 6e 74 20 63 3b 0a 20 20 77 68 69 6c 65 28   int c;.  while(
74990 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
749a0 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65  *zDate) ){ zDate
749b0 2b 2b 3b 20 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20  ++; }.  p->tz = 
749c0 30 3b 0a 20 20 63 20 3d 20 2a 7a 44 61 74 65 3b  0;.  c = *zDate;
749d0 0a 20 20 69 66 28 20 63 3d 3d 27 2d 27 20 29 7b  .  if( c=='-' ){
749e0 0a 20 20 20 20 73 67 6e 20 3d 20 2d 31 3b 0a 20  .    sgn = -1;. 
749f0 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 2b   }else if( c=='+
74a00 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2b  ' ){.    sgn = +
74a10 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63  1;.  }else if( c
74a20 3d 3d 27 5a 27 20 7c 7c 20 63 3d 3d 27 7a 27 20  =='Z' || c=='z' 
74a30 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a  ){.    zDate++;.
74a40 20 20 20 20 67 6f 74 6f 20 7a 75 6c 75 5f 74 69      goto zulu_ti
74a50 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  me;.  }else{.   
74a60 20 72 65 74 75 72 6e 20 63 21 3d 30 3b 0a 20 20   return c!=0;.  
74a70 7d 0a 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 69  }.  zDate++;.  i
74a80 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61  f( getDigits(zDa
74a90 74 65 2c 20 32 2c 20 30 2c 20 31 34 2c 20 27 3a  te, 2, 0, 14, ':
74aa0 27 2c 20 26 6e 48 72 2c 20 32 2c 20 30 2c 20 35  ', &nHr, 2, 0, 5
74ab0 39 2c 20 30 2c 20 26 6e 4d 6e 29 21 3d 32 20 29  9, 0, &nMn)!=2 )
74ac0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
74ad0 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35    }.  zDate += 5
74ae0 3b 0a 20 20 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a  ;.  p->tz = sgn*
74af0 28 6e 4d 6e 20 2b 20 6e 48 72 2a 36 30 29 3b 0a  (nMn + nHr*60);.
74b00 7a 75 6c 75 5f 74 69 6d 65 3a 0a 20 20 77 68 69  zulu_time:.  whi
74b10 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61  le( sqlite3Isspa
74b20 63 65 28 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44  ce(*zDate) ){ zD
74b30 61 74 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72  ate++; }.  retur
74b40 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a  n *zDate!=0;.}..
74b50 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 69 6d 65  /*.** Parse time
74b60 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 48 48  s of the form HH
74b70 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 20  :MM or HH:MM:SS 
74b80 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 46  or HH:MM:SS.FFFF
74b90 2e 0a 2a 2a 20 54 68 65 20 48 48 2c 20 4d 4d 2c  ..** The HH, MM,
74ba0 20 61 6e 64 20 53 53 20 6d 75 73 74 20 65 61 63   and SS must eac
74bb0 68 20 62 65 20 65 78 61 63 74 6c 79 20 32 20 64  h be exactly 2 d
74bc0 69 67 69 74 73 2e 20 20 54 68 65 0a 2a 2a 20 66  igits.  The.** f
74bd0 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64  ractional second
74be0 73 20 46 46 46 46 20 63 61 6e 20 62 65 20 6f 6e  s FFFF can be on
74bf0 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 73  e or more digits
74c00 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31  ..**.** Return 1
74c10 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70   if there is a p
74c20 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61 6e 64  arsing error and
74c30 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a   0 on success..*
74c40 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72  /.static int par
74c50 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73 74 20 63  seHhMmSs(const c
74c60 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65  har *zDate, Date
74c70 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Time *p){.  int 
74c80 68 2c 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 62 6c  h, m, s;.  doubl
74c90 65 20 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 69 66  e ms = 0.0;.  if
74ca0 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74  ( getDigits(zDat
74cb0 65 2c 20 32 2c 20 30 2c 20 32 34 2c 20 27 3a 27  e, 2, 0, 24, ':'
74cc0 2c 20 26 68 2c 20 32 2c 20 30 2c 20 35 39 2c 20  , &h, 2, 0, 59, 
74cd0 30 2c 20 26 6d 29 21 3d 32 20 29 7b 0a 20 20 20  0, &m)!=2 ){.   
74ce0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
74cf0 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 69   zDate += 5;.  i
74d00 66 28 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 20 29  f( *zDate==':' )
74d10 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20  {.    zDate++;. 
74d20 20 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73     if( getDigits
74d30 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 35 39  (zDate, 2, 0, 59
74d40 2c 20 30 2c 20 26 73 29 21 3d 31 20 29 7b 0a 20  , 0, &s)!=1 ){. 
74d50 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
74d60 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 65 20 2b     }.    zDate +
74d70 3d 20 32 3b 0a 20 20 20 20 69 66 28 20 2a 7a 44  = 2;.    if( *zD
74d80 61 74 65 3d 3d 27 2e 27 20 26 26 20 73 71 6c 69  ate=='.' && sqli
74d90 74 65 33 49 73 64 69 67 69 74 28 7a 44 61 74 65  te3Isdigit(zDate
74da0 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f  [1]) ){.      do
74db0 75 62 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e  uble rScale = 1.
74dc0 30 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b  0;.      zDate++
74dd0 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 73  ;.      while( s
74de0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
74df0 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Date) ){.       
74e00 20 6d 73 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20   ms = ms*10.0 + 
74e10 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20  *zDate - '0';.  
74e20 20 20 20 20 20 20 72 53 63 61 6c 65 20 2a 3d 20        rScale *= 
74e30 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 7a 44  10.0;.        zD
74e40 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20  ate++;.      }. 
74e50 20 20 20 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c       ms /= rScal
74e60 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  e;.    }.  }else
74e70 7b 0a 20 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d  {.    s = 0;.  }
74e80 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20  .  p->validJD = 
74e90 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53  0;.  p->validHMS
74ea0 20 3d 20 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68   = 1;.  p->h = h
74eb0 3b 0a 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20  ;.  p->m = m;.  
74ec0 70 2d 3e 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20  p->s = s + ms;. 
74ed0 20 69 66 28 20 70 61 72 73 65 54 69 6d 65 7a 6f   if( parseTimezo
74ee0 6e 65 28 7a 44 61 74 65 2c 20 70 29 20 29 20 72  ne(zDate, p) ) r
74ef0 65 74 75 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61  eturn 1;.  p->va
74f00 6c 69 64 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d  lidTZ = (p->tz!=
74f10 30 29 3f 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e  0)?1:0;.  return
74f20 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e   0;.}../*.** Con
74f30 76 65 72 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d  vert from YYYY-M
74f40 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f  M-DD HH:MM:SS to
74f50 20 6a 75 6c 69 61 6e 20 64 61 79 2e 20 20 57 65   julian day.  We
74f60 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65 0a 2a   always assume.*
74f70 2a 20 74 68 61 74 20 74 68 65 20 59 59 59 59 2d  * that the YYYY-
74f80 4d 4d 2d 44 44 20 69 73 20 61 63 63 6f 72 64 69  MM-DD is accordi
74f90 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72  ng to the Gregor
74fa0 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a  ian calendar..**
74fb0 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 3a 20 20  .** Reference:  
74fc0 4d 65 65 75 73 20 70 61 67 65 20 36 31 0a 2a 2f  Meeus page 61.*/
74fd0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d  .static void com
74fe0 70 75 74 65 4a 44 28 44 61 74 65 54 69 6d 65 20  puteJD(DateTime 
74ff0 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c  *p){.  int Y, M,
75000 20 44 2c 20 41 2c 20 42 2c 20 58 31 2c 20 58 32   D, A, B, X1, X2
75010 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69  ;..  if( p->vali
75020 64 4a 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  dJD ) return;.  
75030 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20  if( p->validYMD 
75040 29 7b 0a 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b  ){.    Y = p->Y;
75050 0a 20 20 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20  .    M = p->M;. 
75060 20 20 20 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d     D = p->D;.  }
75070 65 6c 73 65 7b 0a 20 20 20 20 59 20 3d 20 32 30  else{.    Y = 20
75080 30 30 3b 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d  00;  /* If no YM
75090 44 20 73 70 65 63 69 66 69 65 64 2c 20 61 73 73  D specified, ass
750a0 75 6d 65 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20  ume 2000-Jan-01 
750b0 2a 2f 0a 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20  */.    M = 1;.  
750c0 20 20 44 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69    D = 1;.  }.  i
750d0 66 28 20 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59  f( M<=2 ){.    Y
750e0 2d 2d 3b 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b  --;.    M += 12;
750f0 0a 20 20 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30  .  }.  A = Y/100
75100 3b 0a 20 20 42 20 3d 20 32 20 2d 20 41 20 2b 20  ;.  B = 2 - A + 
75110 28 41 2f 34 29 3b 0a 20 20 58 31 20 3d 20 33 36  (A/4);.  X1 = 36
75120 35 32 35 2a 28 59 2b 34 37 31 36 29 2f 31 30 30  525*(Y+4716)/100
75130 3b 0a 20 20 58 32 20 3d 20 33 30 36 30 30 31 2a  ;.  X2 = 306001*
75140 28 4d 2b 31 29 2f 31 30 30 30 30 3b 0a 20 20 70  (M+1)/10000;.  p
75150 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33  ->iJD = (sqlite3
75160 5f 69 6e 74 36 34 29 28 28 58 31 20 2b 20 58 32  _int64)((X1 + X2
75170 20 2b 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e   + D + B - 1524.
75180 35 20 29 20 2a 20 38 36 34 30 30 30 30 30 29 3b  5 ) * 86400000);
75190 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20  .  p->validJD = 
751a0 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69  1;.  if( p->vali
751b0 64 48 4d 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69  dHMS ){.    p->i
751c0 4a 44 20 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30  JD += p->h*36000
751d0 30 30 20 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20  00 + p->m*60000 
751e0 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  + (sqlite3_int64
751f0 29 28 70 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20  )(p->s*1000);.  
75200 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a    if( p->validTZ
75210 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44   ){.      p->iJD
75220 20 2d 3d 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b   -= p->tz*60000;
75230 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 59  .      p->validY
75240 4d 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  MD = 0;.      p-
75250 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20  >validHMS = 0;. 
75260 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20       p->validTZ 
75270 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 0;.    }.  }.}
75280 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61  ../*.** Parse da
75290 74 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a  tes of the form.
752a0 2a 2a 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d  **.**     YYYY-M
752b0 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46  M-DD HH:MM:SS.FF
752c0 46 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d  F.**     YYYY-MM
752d0 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20  -DD HH:MM:SS.** 
752e0 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48      YYYY-MM-DD H
752f0 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59  H:MM.**     YYYY
75300 2d 4d 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69  -MM-DD.**.** Wri
75310 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  te the result in
75320 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65 20  to the DateTime 
75330 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65  structure and re
75340 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63  turn 0.** on suc
75350 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74 68  cess and 1 if th
75360 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69  e input string i
75370 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72  s not a well-for
75380 6d 65 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a  med.** date..*/.
75390 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65  static int parse
753a0 59 79 79 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63  YyyyMmDd(const c
753b0 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65  har *zDate, Date
753c0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Time *p){.  int 
753d0 59 2c 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20  Y, M, D, neg;.. 
753e0 20 69 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27   if( zDate[0]=='
753f0 2d 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b  -' ){.    zDate+
75400 2b 3b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a  +;.    neg = 1;.
75410 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67    }else{.    neg
75420 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
75430 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c  getDigits(zDate,
75440 34 2c 30 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c  4,0,9999,'-',&Y,
75450 32 2c 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c  2,1,12,'-',&M,2,
75460 31 2c 33 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b  1,31,0,&D)!=3 ){
75470 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
75480 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 31 30   }.  zDate += 10
75490 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74  ;.  while( sqlit
754a0 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65  e3Isspace(*zDate
754b0 29 20 7c 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29  ) || 'T'==*(u8*)
754c0 7a 44 61 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b  zDate ){ zDate++
754d0 3b 20 7d 0a 20 20 69 66 28 20 70 61 72 73 65 48  ; }.  if( parseH
754e0 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d  hMmSs(zDate, p)=
754f0 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20  =0 ){.    /* We 
75500 67 6f 74 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a  got the time */.
75510 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 44 61    }else if( *zDa
75520 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  te==0 ){.    p->
75530 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20  validHMS = 0;.  
75540 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
75550 6e 20 31 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61  n 1;.  }.  p->va
75560 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e  lidJD = 0;.  p->
75570 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20  validYMD = 1;.  
75580 70 2d 3e 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20  p->Y = neg ? -Y 
75590 3a 20 59 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b  : Y;.  p->M = M;
755a0 0a 20 20 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69  .  p->D = D;.  i
755b0 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b  f( p->validTZ ){
755c0 0a 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70  .    computeJD(p
755d0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
755e0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  0;.}../*.** Set 
755f0 74 68 65 20 74 69 6d 65 20 74 6f 20 74 68 65 20  the time to the 
75600 63 75 72 72 65 6e 74 20 74 69 6d 65 20 72 65 70  current time rep
75610 6f 72 74 65 64 20 62 79 20 74 68 65 20 56 46 53  orted by the VFS
75620 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
75630 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72  setDateTimeToCur
75640 72 65 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  rent(sqlite3_con
75650 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44  text *context, D
75660 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64  ateTime *p){.  d
75670 6f 75 62 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74  ouble r;.  sqlit
75680 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
75690 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
756a0 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73  le(context);.  s
756b0 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54  qlite3OsCurrentT
756c0 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72  ime(db->pVfs, &r
756d0 29 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73  );.  p->iJD = (s
756e0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a  qlite3_int64)(r*
756f0 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35  86400000.0 + 0.5
75700 29 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20  );.  p->validJD 
75710 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74  = 1;.}../*.** At
75720 74 65 6d 70 74 20 74 6f 20 70 61 72 73 65 20 74  tempt to parse t
75730 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20  he given string 
75740 69 6e 74 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61  into a Julian Da
75750 79 20 4e 75 6d 62 65 72 2e 20 20 52 65 74 75 72  y Number.  Retur
75760 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  n.** the number 
75770 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a  of errors..**.**
75780 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
75790 72 65 20 61 63 63 65 70 74 61 62 6c 65 20 66 6f  re acceptable fo
757a0 72 6d 73 20 66 6f 72 20 74 68 65 20 69 6e 70 75  rms for the inpu
757b0 74 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  t string:.**.** 
757c0 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20       YYYY-MM-DD 
757d0 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f  HH:MM:SS.FFF  +/
757e0 2d 48 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44  -HH:MM.**      D
757f0 44 44 44 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20  DDD.DD .**      
75800 6e 6f 77 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  now.**.** In the
75810 20 66 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 65   first form, the
75820 20 2b 2f 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77   +/-HH:MM is alw
75830 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54  ays optional.  T
75840 68 65 20 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a  he fractional.**
75850 20 73 65 63 6f 6e 64 73 20 65 78 74 65 6e 73 69   seconds extensi
75860 6f 6e 20 28 74 68 65 20 22 2e 46 46 46 22 29 20  on (the ".FFF") 
75870 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68  is optional.  Th
75880 65 20 73 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f  e seconds portio
75890 6e 0a 2a 2a 20 28 22 3a 53 53 2e 46 46 46 22 29  n.** (":SS.FFF")
758a0 20 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65   is option.  The
758b0 20 79 65 61 72 20 61 6e 64 20 64 61 74 65 20 63   year and date c
758c0 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73  an be omitted as
758d0 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72   long.** as ther
758e0 65 20 69 73 20 61 20 74 69 6d 65 20 73 74 72 69  e is a time stri
758f0 6e 67 2e 20 20 54 68 65 20 74 69 6d 65 20 73 74  ng.  The time st
75900 72 69 6e 67 20 63 61 6e 20 62 65 20 6f 6d 69 74  ring can be omit
75910 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61  ted as long.** a
75920 73 20 74 68 65 72 65 20 69 73 20 61 20 79 65 61  s there is a yea
75930 72 20 61 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73  r and date..*/.s
75940 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 44  tatic int parseD
75950 61 74 65 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c  ateOrTime(.  sql
75960 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
75970 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20  ntext, .  const 
75980 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20  char *zDate, .  
75990 44 61 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20  DateTime *p.){. 
759a0 20 69 6e 74 20 69 73 52 65 61 6c 4e 75 6d 3b 20   int isRealNum; 
759b0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f     /* Return fro
759c0 6d 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65  m sqlite3IsNumbe
759d0 72 28 29 2e 20 20 4e 6f 74 20 75 73 65 64 20 2a  r().  Not used *
759e0 2f 0a 20 20 69 66 28 20 70 61 72 73 65 59 79 79  /.  if( parseYyy
759f0 79 4d 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d 3d  yMmDd(zDate,p)==
75a00 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
75a10 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  0;.  }else if( p
75a20 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 65  arseHhMmSs(zDate
75a30 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  , p)==0 ){.    r
75a40 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
75a50 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
75a60 43 6d 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22 29  Cmp(zDate,"now")
75a70 3d 3d 30 29 7b 0a 20 20 20 20 73 65 74 44 61 74  ==0){.    setDat
75a80 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63  eTimeToCurrent(c
75a90 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20 20 20  ontext, p);.    
75aa0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
75ab0 65 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e  e if( sqlite3IsN
75ac0 75 6d 62 65 72 28 7a 44 61 74 65 2c 20 26 69 73  umber(zDate, &is
75ad0 52 65 61 6c 4e 75 6d 2c 20 53 51 4c 49 54 45 5f  RealNum, SQLITE_
75ae0 55 54 46 38 29 20 29 7b 0a 20 20 20 20 64 6f 75  UTF8) ){.    dou
75af0 62 6c 65 20 72 3b 0a 20 20 20 20 67 65 74 56 61  ble r;.    getVa
75b00 6c 75 65 28 7a 44 61 74 65 2c 20 26 72 29 3b 0a  lue(zDate, &r);.
75b10 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71      p->iJD = (sq
75b20 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38  lite3_int64)(r*8
75b30 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29  6400000.0 + 0.5)
75b40 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44  ;.    p->validJD
75b50 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
75b60 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
75b70 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d   1;.}../*.** Com
75b80 70 75 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d  pute the Year, M
75b90 6f 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72  onth, and Day fr
75ba0 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61  om the julian da
75bb0 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  y number..*/.sta
75bc0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65  tic void compute
75bd0 59 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29  YMD(DateTime *p)
75be0 7b 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c  {.  int Z, A, B,
75bf0 20 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20   C, D, E, X1;.  
75c00 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20  if( p->validYMD 
75c10 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
75c20 21 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20  !p->validJD ){. 
75c30 20 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a     p->Y = 2000;.
75c40 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20      p->M = 1;.  
75c50 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65    p->D = 1;.  }e
75c60 6c 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e  lse{.    Z = (in
75c70 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32  t)((p->iJD + 432
75c80 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29  00000)/86400000)
75c90 3b 0a 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28  ;.    A = (int)(
75ca0 28 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29  (Z - 1867216.25)
75cb0 2f 33 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20  /36524.25);.    
75cc0 41 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20  A = Z + 1 + A - 
75cd0 28 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41  (A/4);.    B = A
75ce0 20 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d   + 1524;.    C =
75cf0 20 28 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e   (int)((B - 122.
75d00 31 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20  1)/365.25);.    
75d10 44 20 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30  D = (36525*C)/10
75d20 30 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29  0;.    E = (int)
75d30 28 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b  ((B-D)/30.6001);
75d40 0a 20 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28  .    X1 = (int)(
75d50 33 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20  30.6001*E);.    
75d60 70 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58  p->D = B - D - X
75d70 31 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c  1;.    p->M = E<
75d80 31 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b  14 ? E-1 : E-13;
75d90 0a 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d  .    p->Y = p->M
75da0 3e 32 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20  >2 ? C - 4716 : 
75db0 43 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20  C - 4715;.  }.  
75dc0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b  p->validYMD = 1;
75dd0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  .}../*.** Comput
75de0 65 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75  e the Hour, Minu
75df0 74 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20  te, and Seconds 
75e00 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20  from the julian 
75e10 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73  day number..*/.s
75e20 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75  tatic void compu
75e30 74 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a  teHMS(DateTime *
75e40 70 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69  p){.  int s;.  i
75e50 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29  f( p->validHMS )
75e60 20 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75   return;.  compu
75e70 74 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28  teJD(p);.  s = (
75e80 69 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34  int)((p->iJD + 4
75e90 33 32 30 30 30 30 30 29 20 25 20 38 36 34 30 30  3200000) % 86400
75ea0 30 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73  000);.  p->s = s
75eb0 2f 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28  /1000.0;.  s = (
75ec0 69 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73  int)p->s;.  p->s
75ed0 20 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20   -= s;.  p->h = 
75ee0 73 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70  s/3600;.  s -= p
75ef0 2d 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d  ->h*3600;.  p->m
75f00 20 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20   = s/60;.  p->s 
75f10 2b 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a  += s - p->m*60;.
75f20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20    p->validHMS = 
75f30 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  1;.}../*.** Comp
75f40 75 74 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64  ute both YMD and
75f50 20 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76   HMS.*/.static v
75f60 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48  oid computeYMD_H
75f70 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  MS(DateTime *p){
75f80 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29  .  computeYMD(p)
75f90 3b 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70  ;.  computeHMS(p
75fa0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  );.}../*.** Clea
75fb0 72 20 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d  r the YMD and HM
75fc0 53 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a  S and the TZ.*/.
75fd0 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61  static void clea
75fe0 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65  rYMD_HMS_TZ(Date
75ff0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76  Time *p){.  p->v
76000 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70  alidYMD = 0;.  p
76010 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a  ->validHMS = 0;.
76020 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30    p->validTZ = 0
76030 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
76040 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49  ITE_OMIT_LOCALTI
76050 4d 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  ME./*.** Compute
76060 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 20   the difference 
76070 28 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  (in milliseconds
76080 29 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63  ).** between loc
76090 61 6c 74 69 6d 65 20 61 6e 64 20 55 54 43 20 28  altime and UTC (
760a0 61 2e 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66  a.k.a. GMT).** f
760b0 6f 72 20 74 68 65 20 74 69 6d 65 20 76 61 6c 75  or the time valu
760c0 65 20 70 20 77 68 65 72 65 20 70 20 69 73 20 69  e p where p is i
760d0 6e 20 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63  n UTC..*/.static
760e0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6c   sqlite3_int64 l
760f0 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 44  ocaltimeOffset(D
76100 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44  ateTime *p){.  D
76110 61 74 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20  ateTime x, y;.  
76120 74 69 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20  time_t t;.  x = 
76130 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44  *p;.  computeYMD
76140 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20  _HMS(&x);.  if( 
76150 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e  x.Y<1971 || x.Y>
76160 3d 32 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59  =2038 ){.    x.Y
76170 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d   = 2000;.    x.M
76180 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20   = 1;.    x.D = 
76190 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a  1;.    x.h = 0;.
761a0 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20      x.m = 0;.   
761b0 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20   x.s = 0.0;.  } 
761c0 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73  else {.    int s
761d0 20 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30   = (int)(x.s + 0
761e0 2e 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73  .5);.    x.s = s
761f0 3b 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30  ;.  }.  x.tz = 0
76200 3b 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20  ;.  x.validJD = 
76210 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26  0;.  computeJD(&
76220 78 29 3b 0a 20 20 74 20 3d 20 78 2e 69 4a 44 2f  x);.  t = x.iJD/
76230 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a  1000 - 21086676*
76240 28 69 36 34 29 31 30 30 30 30 3b 0a 23 69 66 64  (i64)10000;.#ifd
76250 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ef HAVE_LOCALTIM
76260 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  E_R.  {.    stru
76270 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20  ct tm sLocal;.  
76280 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 26 74    localtime_r(&t
76290 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 20 20 20  , &sLocal);.    
762a0 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.Y = sLocal.tm_
762b0 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20  year + 1900;.   
762c0 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.M = sLocal.tm
762d0 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e  _mon + 1;.    y.
762e0 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64  D = sLocal.tm_md
762f0 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c  ay;.    y.h = sL
76300 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20  ocal.tm_hour;.  
76310 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.m = sLocal.t
76320 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d  m_min;.    y.s =
76330 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a   sLocal.tm_sec;.
76340 20 20 7d 0a 23 65 6c 69 66 20 64 65 66 69 6e 65    }.#elif define
76350 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45  d(HAVE_LOCALTIME
76360 5f 53 29 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  _S).  {.    stru
76370 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20  ct tm sLocal;.  
76380 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26 73    localtime_s(&s
76390 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20 20  Local, &t);.    
763a0 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.Y = sLocal.tm_
763b0 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20  year + 1900;.   
763c0 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.M = sLocal.tm
763d0 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e  _mon + 1;.    y.
763e0 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64  D = sLocal.tm_md
763f0 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c  ay;.    y.h = sL
76400 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20  ocal.tm_hour;.  
76410 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.m = sLocal.t
76420 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d  m_min;.    y.s =
76430 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a   sLocal.tm_sec;.
76440 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20    }.#else.  {.  
76450 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d    struct tm *pTm
76460 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
76470 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
76480 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
76490 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
764a0 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54  MASTER));.    pT
764b0 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 74  m = localtime(&t
764c0 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 6d  );.    y.Y = pTm
764d0 2d 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30  ->tm_year + 1900
764e0 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 2d  ;.    y.M = pTm-
764f0 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20  >tm_mon + 1;.   
76500 20 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d   y.D = pTm->tm_m
76510 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 70  day;.    y.h = p
76520 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20  Tm->tm_hour;.   
76530 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d   y.m = pTm->tm_m
76540 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 54  in;.    y.s = pT
76550 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 73  m->tm_sec;.    s
76560 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
76570 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ve(sqlite3MutexA
76580 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
76590 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
765a0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
765b0 79 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a  y.validYMD = 1;.
765c0 20 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 31    y.validHMS = 1
765d0 3b 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20  ;.  y.validJD = 
765e0 30 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 3d  0;.  y.validTZ =
765f0 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28   0;.  computeJD(
76600 26 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e  &y);.  return y.
76610 69 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23  iJD - x.iJD;.}.#
76620 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
76630 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a  OMIT_LOCALTIME *
76640 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73  /../*.** Process
76650 20 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61   a modifier to a
76660 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70   date-time stamp
76670 2e 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73  .  The modifiers
76680 20 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f   are.** as follo
76690 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e  ws:.**.**     NN
766a0 4e 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e  N days.**     NN
766b0 4e 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e  N hours.**     N
766c0 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20  NN minutes.**   
766d0 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e    NNN.NNNN secon
766e0 64 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f  ds.**     NNN mo
766f0 6e 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20  nths.**     NNN 
76700 79 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61  years.**     sta
76710 72 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20  rt of month.**  
76720 20 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72     start of year
76730 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66  .**     start of
76740 20 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61   week.**     sta
76750 72 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20  rt of day.**    
76760 20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20   weekday N.**   
76770 20 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20    unixepoch.**  
76780 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20     localtime.** 
76790 20 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65      utc.**.** Re
767a0 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73  turn 0 on succes
767b0 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65  s and 1 if there
767c0 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20   is any kind of 
767d0 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  error..*/.static
767e0 20 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66 69   int parseModifi
767f0 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  er(const char *z
76800 4d 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  Mod, DateTime *p
76810 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b  ){.  int rc = 1;
76820 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62  .  int n;.  doub
76830 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c  le r;.  char *z,
76840 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d   zBuf[30];.  z =
76850 20 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30   zBuf;.  for(n=0
76860 3b 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 42  ; n<ArraySize(zB
76870 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d  uf)-1 && zMod[n]
76880 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d  ; n++){.    z[n]
76890 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33   = (char)sqlite3
768a0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38  UpperToLower[(u8
768b0 29 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20  )zMod[n]];.  }. 
768c0 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69   z[n] = 0;.  swi
768d0 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66  tch( z[0] ){.#if
768e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
768f0 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 63  _LOCALTIME.    c
76900 61 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20  ase 'l': {.     
76910 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65   /*    localtime
76920 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
76930 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20  ** Assuming the 
76940 63 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61 6c  current time val
76950 75 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e 61  ue is UTC (a.k.a
76960 2e 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69 74  . GMT), shift it
76970 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f   to.      ** sho
76980 77 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20  w local time..  
76990 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
769a0 20 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61   strcmp(z, "loca
769b0 6c 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20  ltime")==0 ){.  
769c0 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
769d0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69  p);.        p->i
769e0 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f  JD += localtimeO
769f0 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20  ffset(p);.      
76a00 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54    clearYMD_HMS_T
76a10 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63  Z(p);.        rc
76a20 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
76a30 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
76a40 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
76a50 20 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a   'u': {.      /*
76a60 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e 69  .      **    uni
76a70 78 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a 0a  xepoch.      **.
76a80 20 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74        ** Treat t
76a90 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
76aa0 20 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 68   of p->iJD as th
76ab0 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20  e number of.    
76ac0 20 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e    ** seconds sin
76ad0 63 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72  ce 1970.  Conver
76ae0 74 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69  t to a real juli
76af0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 20  an day number.. 
76b00 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
76b10 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e 69  ( strcmp(z, "uni
76b20 78 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 70  xepoch")==0 && p
76b30 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20  ->validJD ){.   
76b40 20 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 70       p->iJD = (p
76b50 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 29 2f 38  ->iJD + 43200)/8
76b60 36 34 30 30 20 2b 20 32 31 30 38 36 36 37 36 2a  6400 + 21086676*
76b70 28 69 36 34 29 31 30 30 30 30 30 30 30 3b 0a 20  (i64)10000000;. 
76b80 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f         clearYMD_
76b90 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20  HMS_TZ(p);.     
76ba0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
76bb0 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
76bc0 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45  E_OMIT_LOCALTIME
76bd0 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20  .      else if( 
76be0 73 74 72 63 6d 70 28 7a 2c 20 22 75 74 63 22 29  strcmp(z, "utc")
76bf0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
76c00 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 31 3b  qlite3_int64 c1;
76c10 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
76c20 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63  JD(p);.        c
76c30 31 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66  1 = localtimeOff
76c40 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  set(p);.        
76c50 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a 20 20  p->iJD -= c1;.  
76c60 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48        clearYMD_H
76c70 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20  MS_TZ(p);.      
76c80 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 63 31 20 2d    p->iJD += c1 -
76c90 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74   localtimeOffset
76ca0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  (p);.        rc 
76cb0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  = 0;.      }.#en
76cc0 64 69 66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  dif.      break;
76cd0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
76ce0 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a  'w': {.      /*.
76cf0 20 20 20 20 20 20 2a 2a 20 20 20 20 77 65 65 6b        **    week
76d00 64 61 79 20 4e 0a 20 20 20 20 20 20 2a 2a 0a 20  day N.      **. 
76d10 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65       ** Move the
76d20 20 64 61 74 65 20 74 6f 20 74 68 65 20 73 61 6d   date to the sam
76d30 65 20 74 69 6d 65 20 6f 6e 20 74 68 65 20 6e 65  e time on the ne
76d40 78 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66  xt occurrence of
76d50 0a 20 20 20 20 20 20 2a 2a 20 77 65 65 6b 64 61  .      ** weekda
76d60 79 20 4e 20 77 68 65 72 65 20 30 3d 3d 53 75 6e  y N where 0==Sun
76d70 64 61 79 2c 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20  day, 1==Monday, 
76d80 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49  and so forth.  I
76d90 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64  f the.      ** d
76da0 61 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f  ate is already o
76db0 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
76dc0 65 20 77 65 65 6b 64 61 79 2c 20 74 68 69 73 20  e weekday, this 
76dd0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20  is a no-op..    
76de0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73    */.      if( s
76df0 74 72 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64  trncmp(z, "weekd
76e00 61 79 20 22 2c 20 38 29 3d 3d 30 20 26 26 20 67  ay ", 8)==0 && g
76e10 65 74 56 61 6c 75 65 28 26 7a 5b 38 5d 2c 26 72  etValue(&z[8],&r
76e20 29 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 20  )>0.            
76e30 20 20 20 20 20 26 26 20 28 6e 3d 28 69 6e 74 29       && (n=(int)
76e40 72 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 26 26  r)==r && n>=0 &&
76e50 20 72 3c 37 20 29 7b 0a 20 20 20 20 20 20 20 20   r<7 ){.        
76e60 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 5a 3b  sqlite3_int64 Z;
76e70 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
76e80 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20  YMD_HMS(p);.    
76e90 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d      p->validTZ =
76ea0 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76   0;.        p->v
76eb0 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20  alidJD = 0;.    
76ec0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
76ed0 3b 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 28 28  ;.        Z = ((
76ee0 70 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30 30 30  p->iJD + 1296000
76ef0 30 30 29 2f 38 36 34 30 30 30 30 30 29 20 25 20  00)/86400000) % 
76f00 37 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 5a  7;.        if( Z
76f10 3e 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 20 20  >n ) Z -= 7;.   
76f20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
76f30 6e 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 30 3b  n - Z)*86400000;
76f40 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d  .        clearYM
76f50 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
76f60 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
76f70 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
76f80 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
76f90 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a   's': {.      /*
76fa0 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73 74 61  .      **    sta
76fb0 72 74 20 6f 66 20 54 54 54 54 54 0a 20 20 20 20  rt of TTTTT.    
76fc0 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f    **.      ** Mo
76fd0 76 65 20 74 68 65 20 64 61 74 65 20 62 61 63 6b  ve the date back
76fe0 77 61 72 64 73 20 74 6f 20 74 68 65 20 62 65 67  wards to the beg
76ff0 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 75  inning of the cu
77000 72 72 65 6e 74 20 64 61 79 2c 0a 20 20 20 20 20  rrent day,.     
77010 20 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f 72 20   ** or month or 
77020 79 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  year..      */. 
77030 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70       if( strncmp
77040 28 7a 2c 20 22 73 74 61 72 74 20 6f 66 20 22 2c  (z, "start of ",
77050 20 39 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a   9)!=0 ) break;.
77060 20 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a 20 20        z += 9;.  
77070 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70      computeYMD(p
77080 29 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  );.      p->vali
77090 64 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20 20 20  dHMS = 1;.      
770a0 70 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b  p->h = p->m = 0;
770b0 0a 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e  .      p->s = 0.
770c0 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  0;.      p->vali
770d0 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  dTZ = 0;.      p
770e0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  ->validJD = 0;. 
770f0 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28       if( strcmp(
77100 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 7b  z,"month")==0 ){
77110 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20  .        p->D = 
77120 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  1;.        rc = 
77130 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  0;.      }else i
77140 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61  f( strcmp(z,"yea
77150 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  r")==0 ){.      
77160 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b    computeYMD(p);
77170 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 3d 20  .        p->M = 
77180 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20  1;.        p->D 
77190 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20  = 1;.        rc 
771a0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
771b0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 64   if( strcmp(z,"d
771c0 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ay")==0 ){.     
771d0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
771e0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
771f0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
77200 2b 27 3a 0a 20 20 20 20 63 61 73 65 20 27 2d 27  +':.    case '-'
77210 3a 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 0a  :.    case '0':.
77220 20 20 20 20 63 61 73 65 20 27 31 27 3a 0a 20 20      case '1':.  
77230 20 20 63 61 73 65 20 27 32 27 3a 0a 20 20 20 20    case '2':.    
77240 63 61 73 65 20 27 33 27 3a 0a 20 20 20 20 63 61  case '3':.    ca
77250 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65  se '4':.    case
77260 20 27 35 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '5':.    case '
77270 36 27 3a 0a 20 20 20 20 63 61 73 65 20 27 37 27  6':.    case '7'
77280 3a 0a 20 20 20 20 63 61 73 65 20 27 38 27 3a 0a  :.    case '8':.
77290 20 20 20 20 63 61 73 65 20 27 39 27 3a 20 7b 0a      case '9': {.
772a0 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 52 6f        double rRo
772b0 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 6e 20 3d  under;.      n =
772c0 20 67 65 74 56 61 6c 75 65 28 7a 2c 20 26 72 29   getValue(z, &r)
772d0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
772e0 6e 3e 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66  n>=1 );.      if
772f0 28 20 7a 5b 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20  ( z[n]==':' ){. 
77300 20 20 20 20 20 20 20 2f 2a 20 41 20 6d 6f 64 69         /* A modi
77310 66 69 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d  fier of the form
77320 20 28 2b 7c 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46   (+|-)HH:MM:SS.F
77330 46 46 20 61 64 64 73 20 28 6f 72 20 73 75 62 74  FF adds (or subt
77340 72 61 63 74 73 29 20 74 68 65 0a 20 20 20 20 20  racts) the.     
77350 20 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20     ** specified 
77360 6e 75 6d 62 65 72 20 6f 66 20 68 6f 75 72 73 2c  number of hours,
77370 20 6d 69 6e 75 74 65 73 2c 20 73 65 63 6f 6e 64   minutes, second
77380 73 2c 20 61 6e 64 20 66 72 61 63 74 69 6f 6e 61  s, and fractiona
77390 6c 20 73 65 63 6f 6e 64 73 0a 20 20 20 20 20 20  l seconds.      
773a0 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 69 6d 65    ** to the time
773b0 2e 20 20 54 68 65 20 22 2e 46 46 46 22 20 6d 61  .  The ".FFF" ma
773c0 79 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54  y be omitted.  T
773d0 68 65 20 22 3a 53 53 2e 46 46 46 22 20 6d 61 79  he ":SS.FFF" may
773e0 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f   be.        ** o
773f0 6d 69 74 74 65 64 2e 0a 20 20 20 20 20 20 20 20  mitted..        
77400 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  */.        const
77410 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20   char *z2 = z;. 
77420 20 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20         DateTime 
77430 74 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  tx;.        sqli
77440 74 65 33 5f 69 6e 74 36 34 20 64 61 79 3b 0a 20  te3_int64 day;. 
77450 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69         if( !sqli
77460 74 65 33 49 73 64 69 67 69 74 28 2a 7a 32 29 20  te3Isdigit(*z2) 
77470 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ) z2++;.        
77480 6d 65 6d 73 65 74 28 26 74 78 2c 20 30 2c 20 73  memset(&tx, 0, s
77490 69 7a 65 6f 66 28 74 78 29 29 3b 0a 20 20 20 20  izeof(tx));.    
774a0 20 20 20 20 69 66 28 20 70 61 72 73 65 48 68 4d      if( parseHhM
774b0 6d 53 73 28 7a 32 2c 20 26 74 78 29 20 29 20 62  mSs(z2, &tx) ) b
774c0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 6f  reak;.        co
774d0 6d 70 75 74 65 4a 44 28 26 74 78 29 3b 0a 20 20  mputeJD(&tx);.  
774e0 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d 3d 20        tx.iJD -= 
774f0 34 33 32 30 30 30 30 30 3b 0a 20 20 20 20 20 20  43200000;.      
77500 20 20 64 61 79 20 3d 20 74 78 2e 69 4a 44 2f 38    day = tx.iJD/8
77510 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20  6400000;.       
77520 20 74 78 2e 69 4a 44 20 2d 3d 20 64 61 79 2a 38   tx.iJD -= day*8
77530 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20  6400000;.       
77540 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29   if( z[0]=='-' )
77550 20 74 78 2e 69 4a 44 20 3d 20 2d 74 78 2e 69 4a   tx.iJD = -tx.iJ
77560 44 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  D;.        compu
77570 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20  teJD(p);.       
77580 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
77590 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
775a0 69 4a 44 20 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20  iJD += tx.iJD;. 
775b0 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
775c0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
775d0 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 20 2b 3d      }.      z +=
775e0 20 6e 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   n;.      while(
775f0 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
77600 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 20  *z) ) z++;.     
77610 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
77620 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 20 20 69  en30(z);.      i
77630 66 28 20 6e 3e 31 30 20 7c 7c 20 6e 3c 33 20 29  f( n>10 || n<3 )
77640 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 66   break;.      if
77650 28 20 7a 5b 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b  ( z[n-1]=='s' ){
77660 20 7a 5b 6e 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d   z[n-1] = 0; n--
77670 3b 20 7d 0a 20 20 20 20 20 20 63 6f 6d 70 75 74  ; }.      comput
77680 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 72 63  eJD(p);.      rc
77690 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 52 6f 75   = 0;.      rRou
776a0 6e 64 65 72 20 3d 20 72 3c 30 20 3f 20 2d 30 2e  nder = r<0 ? -0.
776b0 35 20 3a 20 2b 30 2e 35 3b 0a 20 20 20 20 20 20  5 : +0.5;.      
776c0 69 66 28 20 6e 3d 3d 33 20 26 26 20 73 74 72 63  if( n==3 && strc
776d0 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29  mp(z,"day")==0 )
776e0 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44  {.        p->iJD
776f0 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74   += (sqlite3_int
77700 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30  64)(r*86400000.0
77710 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20   + rRounder);.  
77720 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d      }else if( n=
77730 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22  =4 && strcmp(z,"
77740 68 6f 75 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20  hour")==0 ){.   
77750 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
77760 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72  sqlite3_int64)(r
77770 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 32 34 2e  *(86400000.0/24.
77780 30 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a  0) + rRounder);.
77790 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
777a0 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a  n==6 && strcmp(z
777b0 2c 22 6d 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b  ,"minute")==0 ){
777c0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
777d0 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36  += (sqlite3_int6
777e0 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 2e 30  4)(r*(86400000.0
777f0 2f 28 32 34 2e 30 2a 36 30 2e 30 29 29 20 2b 20  /(24.0*60.0)) + 
77800 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20  rRounder);.     
77810 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20   }else if( n==6 
77820 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65 63  && strcmp(z,"sec
77830 6f 6e 64 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ond")==0 ){.    
77840 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
77850 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a  qlite3_int64)(r*
77860 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 2e  (86400000.0/(24.
77870 30 2a 36 30 2e 30 2a 36 30 2e 30 29 29 20 2b 20  0*60.0*60.0)) + 
77880 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20  rRounder);.     
77890 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 35 20   }else if( n==5 
778a0 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e  && strcmp(z,"mon
778b0 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  th")==0 ){.     
778c0 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20 20 20     int x, y;.   
778d0 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f       computeYMD_
778e0 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20  HMS(p);.        
778f0 70 2d 3e 4d 20 2b 3d 20 28 69 6e 74 29 72 3b 0a  p->M += (int)r;.
77900 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d 3e 4d          x = p->M
77910 3e 30 20 3f 20 28 70 2d 3e 4d 2d 31 29 2f 31 32  >0 ? (p->M-1)/12
77920 20 3a 20 28 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b   : (p->M-12)/12;
77930 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d  .        p->Y +=
77940 20 78 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d   x;.        p->M
77950 20 2d 3d 20 78 2a 31 32 3b 0a 20 20 20 20 20 20   -= x*12;.      
77960 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30    p->validJD = 0
77970 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
77980 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20  eJD(p);.        
77990 79 20 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20  y = (int)r;.    
779a0 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b 0a      if( y!=r ){.
779b0 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44            p->iJD
779c0 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74   += (sqlite3_int
779d0 36 34 29 28 28 72 20 2d 20 79 29 2a 33 30 2e 30  64)((r - y)*30.0
779e0 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52  *86400000.0 + rR
779f0 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 20  ounder);.       
77a00 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69   }.      }else i
77a10 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d  f( n==4 && strcm
77a20 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29  p(z,"year")==0 )
77a30 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 79 20  {.        int y 
77a40 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20  = (int)r;.      
77a50 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53    computeYMD_HMS
77a60 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
77a70 59 20 2b 3d 20 79 3b 0a 20 20 20 20 20 20 20 20  Y += y;.        
77a80 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a  p->validJD = 0;.
77a90 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a          computeJ
77aa0 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66  D(p);.        if
77ab0 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20  ( y!=r ){.      
77ac0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
77ad0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72  qlite3_int64)((r
77ae0 20 2d 20 79 29 2a 33 36 35 2e 30 2a 38 36 34 30   - y)*365.0*8640
77af0 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65  0000.0 + rRounde
77b00 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  r);.        }.  
77b10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
77b20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20     rc = 1;.     
77b30 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 59 4d   }.      clearYM
77b40 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
77b50 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
77b60 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
77b70 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
77b80 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
77b90 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63  c;.}../*.** Proc
77ba0 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74 69 6f  ess time functio
77bb0 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 61 72  n arguments.  ar
77bc0 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74 65 2d  gv[0] is a date-
77bd0 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a 20 61  time stamp..** a
77be0 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f  rgv[1] and follo
77bf0 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66 69 65  wing are modifie
77c00 72 73 2e 20 20 50 61 72 73 65 20 74 68 65 6d 20  rs.  Parse them 
77c10 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a  all and write.**
77c20 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74   the resulting t
77c30 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44 61 74  ime into the Dat
77c40 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65 20  eTime structure 
77c50 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a 2a 20  p.  Return 0.** 
77c60 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31  on success and 1
77c70 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   if there are an
77c80 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  y errors..**.** 
77c90 49 66 20 74 68 65 72 65 20 61 72 65 20 7a 65 72  If there are zer
77ca0 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28 69 66  o parameters (if
77cb0 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20 69 73   even argv[0] is
77cc0 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a 20 74   undefined).** t
77cd0 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64 65 66  hen assume a def
77ce0 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 22 6e  ault value of "n
77cf0 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30 5d 2e  ow" for argv[0].
77d00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
77d10 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33  sDate(.  sqlite3
77d20 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
77d30 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  t, .  int argc, 
77d40 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
77d50 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61 74 65   **argv, .  Date
77d60 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74  Time *p.){.  int
77d70 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   i;.  const unsi
77d80 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20  gned char *z;.  
77d90 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d 65 6d  int eType;.  mem
77da0 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
77db0 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61 72 67  (*p));.  if( arg
77dc0 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65 74 44  c==0 ){.    setD
77dd0 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74  ateTimeToCurrent
77de0 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20  (context, p);.  
77df0 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79 70 65  }else if( (eType
77e00 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
77e10 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 3d  _type(argv[0]))=
77e20 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a 20 20  =SQLITE_FLOAT.  
77e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77e40 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
77e50 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20  E_INTEGER ){.   
77e60 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74   p->iJD = (sqlit
77e70 65 33 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65  e3_int64)(sqlite
77e80 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61  3_value_double(a
77e90 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30 30 30  rgv[0])*86400000
77ea0 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70  .0 + 0.5);.    p
77eb0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20  ->validJD = 1;. 
77ec0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
77ed0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
77ee0 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
77ef0 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72 73 65   if( !z || parse
77f00 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e 74 65  DateOrTime(conte
77f10 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 70 29  xt, (char*)z, p)
77f20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
77f30 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
77f40 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b  for(i=1; i<argc;
77f50 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28   i++){.    if( (
77f60 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  z = sqlite3_valu
77f70 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d 29 29  e_text(argv[i]))
77f80 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f 64 69  ==0 || parseModi
77f90 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c 20 70  fier((char*)z, p
77fa0 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
77fb0 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
77fc0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f   return 0;.}.../
77fd0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
77fe0 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  ng routines impl
77ff0 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75  ement the variou
78000 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20  s date and time 
78010 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20  functions.** of 
78020 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  SQLite..*/../*.*
78030 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79 28 20  *    julianday( 
78040 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c  TIMESTRING, MOD,
78050 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a   MOD, ...).**.**
78060 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75 6c 69   Return the juli
78070 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 6f 66  an day number of
78080 20 74 68 65 20 64 61 74 65 20 73 70 65 63 69 66   the date specif
78090 69 65 64 20 69 6e 20 74 68 65 20 61 72 67 75 6d  ied in the argum
780a0 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ents.*/.static v
780b0 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e  oid juliandayFun
780c0 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
780d0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
780e0 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
780f0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
78100 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20  v.){.  DateTime 
78110 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28  x;.  if( isDate(
78120 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61  context, argc, a
78130 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20  rgv, &x)==0 ){. 
78140 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29     computeJD(&x)
78150 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
78160 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
78170 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30 30  ext, x.iJD/86400
78180 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  000.0);.  }.}../
78190 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 6d 65  *.**    datetime
781a0 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f  ( TIMESTRING, MO
781b0 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a  D, MOD, ...).**.
781c0 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d  ** Return YYYY-M
781d0 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f  M-DD HH:MM:SS.*/
781e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
781f0 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c  etimeFunc(.  sql
78200 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
78210 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
78220 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
78230 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
78240 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28  ateTime x;.  if(
78250 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c   isDate(context,
78260 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29   argc, argv, &x)
78270 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ==0 ){.    char 
78280 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63  zBuf[100];.    c
78290 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78  omputeYMD_HMS(&x
782a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
782b0 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
782c0 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 34  Buf), zBuf, "%04
782d0 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 32 64  d-%02d-%02d %02d
782e0 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 20 20  :%02d:%02d",.   
782f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78300 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c    x.Y, x.M, x.D,
78310 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29   x.h, x.m, (int)
78320 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c 69  (x.s));.    sqli
78330 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
78340 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d  context, zBuf, -
78350 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  1, SQLITE_TRANSI
78360 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ENT);.  }.}../*.
78370 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 4d 45  **    time( TIME
78380 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
78390 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
783a0 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a  urn HH:MM:SS.*/.
783b0 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d 65  static void time
783c0 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
783d0 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
783e0 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
783f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
78400 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69  argv.){.  DateTi
78410 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61  me x;.  if( isDa
78420 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  te(context, argc
78430 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29  , argv, &x)==0 )
78440 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b  {.    char zBuf[
78450 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74  100];.    comput
78460 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71  eHMS(&x);.    sq
78470 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
78480 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75  izeof(zBuf), zBu
78490 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25 30  f, "%02d:%02d:%0
784a0 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28  2d", x.h, x.m, (
784b0 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73 71  int)x.s);.    sq
784c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
784d0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
784e0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
784f0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  SIENT);.  }.}../
78500 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54 49  *.**    date( TI
78510 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d  MESTRING, MOD, M
78520 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52  OD, ...).**.** R
78530 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44  eturn YYYY-MM-DD
78540 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
78550 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69  dateFunc(.  sqli
78560 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
78570 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
78580 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
78590 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61  e **argv.){.  Da
785a0 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20  teTime x;.  if( 
785b0 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20  isDate(context, 
785c0 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d  argc, argv, &x)=
785d0 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a  =0 ){.    char z
785e0 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f  Buf[100];.    co
785f0 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a 20 20  mputeYMD(&x);.  
78600 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
78610 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c  tf(sizeof(zBuf),
78620 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32   zBuf, "%04d-%02
78630 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 78 2e  d-%02d", x.Y, x.
78640 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 71 6c  M, x.D);.    sql
78650 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
78660 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20  (context, zBuf, 
78670 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  -1, SQLITE_TRANS
78680 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  IENT);.  }.}../*
78690 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d 65 28  .**    strftime(
786a0 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54 52   FORMAT, TIMESTR
786b0 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e  ING, MOD, MOD, .
786c0 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ..).**.** Return
786d0 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 72 69   a string descri
786e0 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e 20 20  bed by FORMAT.  
786f0 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 66  Conversions as f
78700 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
78710 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74 68  %d  day of month
78720 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 72 61  .**   %f  ** fra
78730 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20  ctional seconds 
78740 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48 20   SS.SSS.**   %H 
78750 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20 20   hour 00-24.**  
78760 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 61 72   %j  day of year
78770 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25 4a   000-366.**   %J
78780 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79 20    ** Julian day 
78790 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20 20  number.**   %m  
787a0 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20 20  month 01-12.**  
787b0 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d 35   %M  minute 00-5
787c0 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 6f 6e  9.**   %s  secon
787d0 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d 30 31  ds since 1970-01
787e0 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 65 63  -01.**   %S  sec
787f0 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20 20  onds 00-59.**   
78800 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b 20  %w  day of week 
78810 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a 2a  0-6  sunday==0.*
78820 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66 20  *   %W  week of 
78830 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20 20  year 00-53.**   
78840 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39 39  %Y  year 0000-99
78850 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a 2f  99.**   %%  %.*/
78860 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72  .static void str
78870 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c  ftimeFunc(.  sql
78880 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
78890 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
788a0 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
788b0 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
788c0 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 36 34  ateTime x;.  u64
788d0 20 6e 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 6a   n;.  size_t i,j
788e0 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73  ;.  char *z;.  s
788f0 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f  qlite3 *db;.  co
78900 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d  nst char *zFmt =
78910 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
78920 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
78930 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61  (argv[0]);.  cha
78940 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69  r zBuf[100];.  i
78950 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73  f( zFmt==0 || is
78960 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  Date(context, ar
78970 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78  gc-1, argv+1, &x
78980 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62  ) ) return;.  db
78990 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
789a0 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
789b0 74 65 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30  text);.  for(i=0
789c0 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20  , n=1; zFmt[i]; 
789d0 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69  i++, n++){.    i
789e0 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20  f( zFmt[i]=='%' 
789f0 29 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28  ){.      switch(
78a00 20 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20   zFmt[i+1] ){.  
78a10 20 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a        case 'd':.
78a20 20 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27          case 'H'
78a30 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  :.        case '
78a40 6d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65  m':.        case
78a50 20 27 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61   'M':.        ca
78a60 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20  se 'S':.        
78a70 63 61 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20  case 'W':.      
78a80 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20      n++;.       
78a90 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20     /* fall thru 
78aa0 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  */.        case 
78ab0 27 77 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73  'w':.        cas
78ac0 65 20 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20  e '%':.         
78ad0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
78ae0 63 61 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20  case 'f':.      
78af0 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20      n += 8;.    
78b00 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
78b10 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20       case 'j':. 
78b20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b           n += 3;
78b30 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
78b40 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
78b50 59 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20  Y':.          n 
78b60 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20  += 8;.          
78b70 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
78b80 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20  ase 's':.       
78b90 20 63 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20   case 'J':.     
78ba0 20 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20       n += 50;.  
78bb0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
78bc0 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a         default:.
78bd0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
78be0 3b 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65  ;  /* ERROR.  re
78bf0 74 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20  turn a NULL */. 
78c00 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b       }.      i++
78c10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 74 65  ;.    }.  }.  te
78c20 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f  stcase( n==sizeo
78c30 66 28 7a 42 75 66 29 2d 31 20 29 3b 0a 20 20 74  f(zBuf)-1 );.  t
78c40 65 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65  estcase( n==size
78c50 6f 66 28 7a 42 75 66 29 20 29 3b 0a 20 20 74 65  of(zBuf) );.  te
78c60 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29  stcase( n==(u64)
78c70 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
78c80 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b  E_LIMIT_LENGTH]+
78c90 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  1 );.  testcase(
78ca0 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69   n==(u64)db->aLi
78cb0 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
78cc0 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 69 66  _LENGTH] );.  if
78cd0 28 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29  ( n<sizeof(zBuf)
78ce0 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66   ){.    z = zBuf
78cf0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e  ;.  }else if( n>
78d00 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b  (u64)db->aLimit[
78d10 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
78d20 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69  GTH] ){.    sqli
78d30 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
78d40 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29  _toobig(context)
78d50 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
78d60 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73  }else{.    z = s
78d70 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
78d80 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20  w(db, (int)n);. 
78d90 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20     if( z==0 ){. 
78da0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
78db0 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
78dc0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
78dd0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
78de0 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78  }.  computeJD(&x
78df0 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f  );.  computeYMD_
78e00 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f 72 28 69  HMS(&x);.  for(i
78e10 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69  =j=0; zFmt[i]; i
78e20 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d  ++){.    if( zFm
78e30 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a 20 20 20  t[i]!='%' ){.   
78e40 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74     z[j++] = zFmt
78e50 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  [i];.    }else{.
78e60 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
78e70 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 5d   switch( zFmt[i]
78e80 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65   ){.        case
78e90 20 27 64 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'd':  sqlite3_s
78ea0 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d  nprintf(3, &z[j]
78eb0 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b 20 6a 2b  ,"%02d",x.D); j+
78ec0 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
78ed0 20 20 20 63 61 73 65 20 27 66 27 3a 20 7b 0a 20     case 'f': {. 
78ee0 20 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20           double 
78ef0 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20 20 20 20  s = x.s;.       
78f00 20 20 20 69 66 28 20 73 3e 35 39 2e 39 39 39 20     if( s>59.999 
78f10 29 20 73 20 3d 20 35 39 2e 39 39 39 3b 0a 20 20  ) s = 59.999;.  
78f20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
78f30 73 6e 70 72 69 6e 74 66 28 37 2c 20 26 7a 5b 6a  snprintf(7, &z[j
78f40 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73 29 3b 0a  ],"%06.3f", s);.
78f50 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73            j += s
78f60 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26  qlite3Strlen30(&
78f70 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20  z[j]);.         
78f80 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
78f90 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  }.        case '
78fa0 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  H':  sqlite3_snp
78fb0 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
78fc0 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32  %02d",x.h); j+=2
78fd0 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
78fe0 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20 46 61   case 'W': /* Fa
78ff0 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20  ll thru */.     
79000 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b 0a 20     case 'j': {. 
79010 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 44 61           int nDa
79020 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  y;             /
79030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 79 73  * Number of days
79040 20 73 69 6e 63 65 20 31 73 74 20 64 61 79 20 6f   since 1st day o
79050 66 20 79 65 61 72 20 2a 2f 0a 20 20 20 20 20 20  f year */.      
79060 20 20 20 20 44 61 74 65 54 69 6d 65 20 79 20 3d      DateTime y =
79070 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e   x;.          y.
79080 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20  validJD = 0;.   
79090 20 20 20 20 20 20 20 79 2e 4d 20 3d 20 31 3b 0a         y.M = 1;.
790a0 20 20 20 20 20 20 20 20 20 20 79 2e 44 20 3d 20            y.D = 
790b0 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6d  1;.          com
790c0 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 20 20  puteJD(&y);.    
790d0 20 20 20 20 20 20 6e 44 61 79 20 3d 20 28 69 6e        nDay = (in
790e0 74 29 28 28 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b  t)((x.iJD-y.iJD+
790f0 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30  43200000)/864000
79100 30 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  00);.          i
79110 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 27 20  f( zFmt[i]=='W' 
79120 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
79130 6e 74 20 77 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f  nt wd;   /* 0=Mo
79140 6e 64 61 79 2c 20 31 3d 54 75 65 73 64 61 79 2c  nday, 1=Tuesday,
79150 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 79 20 2a 2f   ... 6=Sunday */
79160 0a 20 20 20 20 20 20 20 20 20 20 20 20 77 64 20  .            wd 
79170 3d 20 28 69 6e 74 29 28 28 28 78 2e 69 4a 44 2b  = (int)(((x.iJD+
79180 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30  43200000)/864000
79190 30 30 29 25 37 29 3b 0a 20 20 20 20 20 20 20 20  00)%7);.        
791a0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
791b0 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(3, &z[j],"%
791c0 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29  02d",(nDay+7-wd)
791d0 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  /7);.           
791e0 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20   j += 2;.       
791f0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
79200 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
79210 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c  printf(4, &z[j],
79220 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a  "%03d",nDay+1);.
79230 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d              j +=
79240 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   3;.          }.
79250 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
79260 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
79270 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20     case 'J': {. 
79280 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
79290 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a  _snprintf(20, &z
792a0 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a  [j],"%.16g",x.iJ
792b0 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20  D/86400000.0);. 
792c0 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 71 6c 69           j+=sqli
792d0 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a  te3Strlen30(&z[j
792e0 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  ]);.          br
792f0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
79300 20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a         case 'm':
79310 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
79320 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32  tf(3, &z[j],"%02
79330 64 22 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62  d",x.M); j+=2; b
79340 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
79350 73 65 20 27 4d 27 3a 20 20 73 71 6c 69 74 65 33  se 'M':  sqlite3
79360 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b  _snprintf(3, &z[
79370 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 6d 29 3b 20  j],"%02d",x.m); 
79380 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20  j+=2; break;.   
79390 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b       case 's': {
793a0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
793b0 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 30 2c 26  e3_snprintf(30,&
793c0 7a 5b 6a 5d 2c 22 25 6c 6c 64 22 2c 0a 20 20 20  z[j],"%lld",.   
793d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
793e0 20 20 20 20 20 20 20 20 28 69 36 34 29 28 78 2e          (i64)(x.
793f0 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36  iJD/1000 - 21086
79400 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 29 29  676*(i64)10000))
79410 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d  ;.          j +=
79420 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
79430 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20  (&z[j]);.       
79440 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
79450 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65    }.        case
79460 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'S':  sqlite3_s
79470 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c  nprintf(3,&z[j],
79480 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e 73 29  "%02d",(int)x.s)
79490 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20  ; j+=2; break;. 
794a0 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a         case 'w':
794b0 20 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a   {.          z[j
794c0 2b 2b 5d 20 3d 20 28 63 68 61 72 29 28 28 28 78  ++] = (char)(((x
794d0 2e 69 4a 44 2b 31 32 39 36 30 30 30 30 30 29 2f  .iJD+129600000)/
794e0 38 36 34 30 30 30 30 30 29 20 25 20 37 29 20 2b  86400000) % 7) +
794f0 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '0';.          
79500 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
79510 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59  .        case 'Y
79520 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ': {.          s
79530 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
79540 35 2c 26 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78  5,&z[j],"%04d",x
79550 2e 59 29 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53  .Y); j+=sqlite3S
79560 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a  trlen30(&z[j]);.
79570 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
79580 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
79590 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b     default:   z[
795a0 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20 62 72 65 61  j++] = '%'; brea
795b0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
795c0 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b  .  }.  z[j] = 0;
795d0 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
795e0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
795f0 7a 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20  z, -1,.         
79600 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 3d 3d               z==
79610 7a 42 75 66 20 3f 20 53 51 4c 49 54 45 5f 54 52  zBuf ? SQLITE_TR
79620 41 4e 53 49 45 4e 54 20 3a 20 53 51 4c 49 54 45  ANSIENT : SQLITE
79630 5f 44 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a  _DYNAMIC);.}../*
79640 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65  .** current_time
79650 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ().**.** This fu
79660 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
79670 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73  he same value as
79680 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f   time('now')..*/
79690 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 74 69  .static void cti
796a0 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  meFunc(.  sqlite
796b0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
796c0 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
796d0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
796e0 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
796f0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
79700 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
79710 74 55 73 65 64 32 29 3b 0a 20 20 74 69 6d 65 46  tUsed2);.  timeF
79720 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20  unc(context, 0, 
79730 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72  0);.}../*.** cur
79740 72 65 6e 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a  rent_date().**.*
79750 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
79760 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65  returns the same
79770 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 28 27   value as date('
79780 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  now')..*/.static
79790 20 76 6f 69 64 20 63 64 61 74 65 46 75 6e 63 28   void cdateFunc(
797a0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
797b0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
797c0 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
797d0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f  lite3_value **No
797e0 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53  tUsed2.){.  UNUS
797f0 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
79800 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
79810 3b 0a 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e  ;.  dateFunc(con
79820 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a  text, 0, 0);.}..
79830 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69  /*.** current_ti
79840 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20  mestamp().**.** 
79850 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
79860 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76  turns the same v
79870 61 6c 75 65 20 61 73 20 64 61 74 65 74 69 6d 65  alue as datetime
79880 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74  ('now')..*/.stat
79890 69 63 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61  ic void ctimesta
798a0 6d 70 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  mpFunc(.  sqlite
798b0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
798c0 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
798d0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
798e0 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
798f0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
79900 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
79910 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 74  tUsed2);.  datet
79920 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c  imeFunc(context,
79930 20 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66   0, 0);.}.#endif
79940 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
79950 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d  ITE_OMIT_DATETIM
79960 45 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66  E_FUNCS) */..#if
79970 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
79980 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f  DATETIME_FUNCS./
79990 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72  *.** If the libr
799a0 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ary is compiled 
799b0 74 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c  to omit the full
799c0 2d 73 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20  -scale date and 
799d0 74 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67  time.** handling
799e0 20 28 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c   (to get a small
799f0 65 72 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20  er binary), the 
79a00 66 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61  following minima
79a10 6c 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20  l version.** of 
79a20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75  the functions cu
79a30 72 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75  rrent_time(), cu
79a40 72 72 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64  rrent_date() and
79a50 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61   current_timesta
79a60 6d 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c  mp().** are incl
79a70 75 64 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68  uded instead. Th
79a80 69 73 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74  is is to support
79a90 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74   column declarat
79aa0 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63  ions that.** inc
79ab0 6c 75 64 65 20 22 44 45 46 41 55 4c 54 20 43 55  lude "DEFAULT CU
79ac0 52 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e  RRENT_TIME" etc.
79ad0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
79ae0 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d  tion uses the C-
79af0 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e  library function
79b00 73 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65  s time(), gmtime
79b10 28 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69  ().** and strfti
79b20 6d 65 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74  me(). The format
79b30 20 73 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20   string to pass 
79b40 74 6f 20 73 74 72 66 74 69 6d 65 28 29 20 69 73  to strftime() is
79b50 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20   supplied.** as 
79b60 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f  the user-data fo
79b70 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a  r the function..
79b80 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
79b90 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a  urrentTimeFunc(.
79ba0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
79bb0 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
79bc0 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
79bd0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
79be0 7b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20  {.  time_t t;.  
79bf0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20  char *zFormat = 
79c00 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
79c10 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
79c20 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  t);.  sqlite3 *d
79c30 62 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a  b;.  double rT;.
79c40 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b    char zBuf[20];
79c50 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
79c60 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 55 4e  ETER(argc);.  UN
79c70 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
79c80 72 67 76 29 3b 0a 0a 20 20 64 62 20 3d 20 73 71  rgv);..  db = sq
79c90 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
79ca0 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
79cb0 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72  ;.  sqlite3OsCur
79cc0 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66  rentTime(db->pVf
79cd0 73 2c 20 26 72 54 29 3b 0a 23 69 66 6e 64 65 66  s, &rT);.#ifndef
79ce0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
79cf0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 74 20  ATING_POINT.  t 
79d00 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20 2d 20  = 86400.0*(rT - 
79d10 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30 2e 35  2440587.5) + 0.5
79d20 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 77 69 74  ;.#else.  /* wit
79d30 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f  hout floating po
79d40 69 6e 74 20 73 75 70 70 6f 72 74 2c 20 72 54 20  int support, rT 
79d50 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61  will have.  ** a
79d60 6c 72 65 61 64 79 20 6c 6f 73 74 20 66 72 61 63  lready lost frac
79d70 74 69 6f 6e 61 6c 20 64 61 79 20 70 72 65 63 69  tional day preci
79d80 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d  sion..  */.  t =
79d90 20 38 36 34 30 30 20 2a 20 28 72 54 20 2d 20 32   86400 * (rT - 2
79da0 34 34 30 35 38 37 29 20 2d 20 34 33 32 30 30 3b  440587) - 43200;
79db0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48  .#endif.#ifdef H
79dc0 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20 7b  AVE_GMTIME_R.  {
79dd0 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73  .    struct tm s
79de0 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65 5f  Now;.    gmtime_
79df0 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20  r(&t, &sNow);.  
79e00 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c    strftime(zBuf,
79e10 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73   20, zFormat, &s
79e20 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  Now);.  }.#else.
79e30 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
79e40 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69  m *pTm;.    sqli
79e50 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
79e60 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
79e70 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
79e80 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
79e90 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65      pTm = gmtime
79ea0 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74 69  (&t);.    strfti
79eb0 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f  me(zBuf, 20, zFo
79ec0 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 20  rmat, pTm);.    
79ed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
79ee0 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ave(sqlite3Mutex
79ef0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
79f00 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
79f10 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ));.  }.#endif..
79f20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
79f30 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
79f40 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  Buf, -1, SQLITE_
79f50 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65  TRANSIENT);.}.#e
79f60 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
79f70 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74   function regist
79f80 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20  ered all of the 
79f90 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e  above C function
79fa0 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63  s as SQL.** func
79fb0 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f  tions.  This sho
79fc0 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20  uld be the only 
79fd0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
79fe0 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74  file with.** ext
79ff0 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a  ernal linkage..*
7a000 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
7a010 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
7a020 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e  isterDateTimeFun
7a030 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20  ctions(void){.  
7a040 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
7a050 44 20 46 75 6e 63 44 65 66 20 61 44 61 74 65 54  D FuncDef aDateT
7a060 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23  imeFuncs[] = {.#
7a070 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
7a080 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43  IT_DATETIME_FUNC
7a090 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6a  S.    FUNCTION(j
7a0a0 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20 20  ulianday,       
7a0b0 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69 61   -1, 0, 0, julia
7a0c0 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20 20  ndayFunc ),.    
7a0d0 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20 20  FUNCTION(date,  
7a0e0 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30             -1, 0
7a0f0 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20 20  , 0, dateFunc   
7a100 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
7a110 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20 20  ON(time,        
7a120 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 74       -1, 0, 0, t
7a130 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a  imeFunc      ),.
7a140 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74      FUNCTION(dat
7a150 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 2d  etime,         -
7a160 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69 6d  1, 0, 0, datetim
7a170 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 55  eFunc  ),.    FU
7a180 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65 2c  NCTION(strftime,
7a190 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20           -1, 0, 
7a1a0 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 20  0, strftimeFunc 
7a1b0 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
7a1c0 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20  (current_time,  
7a1d0 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74 69      0, 0, 0, cti
7a1e0 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 20  meFunc     ),.  
7a1f0 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65    FUNCTION(curre
7a200 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c  nt_timestamp, 0,
7a210 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61 6d   0, 0, ctimestam
7a220 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43  pFunc),.    FUNC
7a230 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74  TION(current_dat
7a240 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c  e,      0, 0, 0,
7a250 20 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 29   cdateFunc     )
7a260 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52 5f  ,.#else.    STR_
7a270 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74  FUNCTION(current
7a280 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 22  _time,      0, "
7a290 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 20  %H:%M:%S",      
7a2a0 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69      0, currentTi
7a2b0 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52  meFunc),.    STR
7a2c0 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e  _FUNCTION(curren
7a2d0 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20  t_timestamp, 0, 
7a2e0 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20 20  "%Y-%m-%d",     
7a2f0 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54       0, currentT
7a300 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54  imeFunc),.    ST
7a310 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65  R_FUNCTION(curre
7a320 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 2c  nt_date,      0,
7a330 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25 4d   "%Y-%m-%d %H:%M
7a340 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e 74  :%S", 0, current
7a350 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64 69  TimeFunc),.#endi
7a360 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a  f.  };.  int i;.
7a370 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70    FuncDefHash *p
7a380 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46  Hash = &GLOBAL(F
7a390 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69  uncDefHash, sqli
7a3a0 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f  te3GlobalFunctio
7a3b0 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a  ns);.  FuncDef *
7a3c0 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66  aFunc = (FuncDef
7a3d0 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  *)&GLOBAL(FuncDe
7a3e0 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63  f, aDateTimeFunc
7a3f0 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20  s);..  for(i=0; 
7a400 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61 74  i<ArraySize(aDat
7a410 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b 2b  eTimeFuncs); i++
7a420 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75  ){.    sqlite3Fu
7a430 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73  ncDefInsert(pHas
7a440 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20  h, &aFunc[i]);. 
7a450 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
7a460 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74  ***** End of dat
7a470 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
7a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a4a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
7a4b0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
7a4c0 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   os.c **********
7a4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a4f0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35  ****/./*.** 2005
7a500 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 0a   November 29.**.
7a510 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
7a520 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
7a530 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
7a540 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
7a550 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
7a560 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
7a570 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
7a580 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
7a590 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
7a5a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7a5b0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
7a5c0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
7a5d0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
7a5e0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7a5f0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
7a600 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
7a610 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
7a620 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
7a630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a670 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
7a680 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e 74   contains OS int
7a690 65 72 66 61 63 65 20 63 6f 64 65 20 74 68 61 74  erface code that
7a6a0 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c   is common to al
7a6b0 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72  l.** architectur
7a6c0 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f  es..**.** $Id: o
7a6d0 73 2e 63 2c 76 20 31 2e 31 32 36 20 32 30 30 39  s.c,v 1.126 2009
7a6e0 2f 30 33 2f 32 35 20 31 34 3a 32 34 3a 34 32 20  /03/25 14:24:42 
7a6f0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65  drh Exp $.*/.#de
7a700 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f  fine _SQLITE_OS_
7a710 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c  C_ 1.#undef _SQL
7a720 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a  ITE_OS_C_../*.**
7a730 20 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c   The default SQL
7a740 69 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20  ite sqlite3_vfs 
7a750 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
7a760 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a  do not allocate.
7a770 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61  ** memory (actua
7a780 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61  lly, os_unix.c a
7a790 6c 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c  llocates a small
7a7a0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
7a7b0 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e  y.** from within
7a7c0 20 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20   OsOpen()), but 
7a7d0 73 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79  some third-party
7a7e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
7a7f0 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74   may..** So we t
7a800 65 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20  est the effects 
7a810 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  of a malloc() fa
7a820 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71  iling and the sq
7a830 6c 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20  lite3OsXXX().** 
7a840 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69  function returni
7a850 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ng SQLITE_IOERR_
7a860 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20  NOMEM using the 
7a870 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
7a880 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54  T macro..**.** T
7a890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e  he following fun
7a8a0 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72  ctions are instr
7a8b0 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c  umented for mall
7a8c0 6f 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a  oc() failure .**
7a8d0 20 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20   testing:.**.** 
7a8e0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65      sqlite3OsOpe
7a8f0 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  n().**     sqlit
7a900 65 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20  e3OsRead().**   
7a910 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65    sqlite3OsWrite
7a920 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
7a930 33 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20  3OsSync().**    
7a940 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29   sqlite3OsLock()
7a950 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  .**.*/.#if defin
7a960 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
7a970 26 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49  && (SQLITE_OS_WI
7a980 4e 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20  N==0).  #define 
7a990 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
7a9a0 54 20 69 66 20 28 31 29 20 7b 20 20 20 20 20 20  T if (1) {      
7a9b0 20 20 20 20 20 20 5c 0a 20 20 20 20 76 6f 69 64        \.    void
7a9c0 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73 71   *pTstAlloc = sq
7a9d0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29 3b  lite3Malloc(10);
7a9e0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 20         \.    if 
7a9f0 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 74  (!pTstAlloc) ret
7aa00 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
7aa10 5f 4e 4f 4d 45 4d 3b 20 20 5c 0a 20 20 20 20 73  _NOMEM;  \.    s
7aa20 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 73 74  qlite3_free(pTst
7aa30 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 20 20  Alloc);         
7aa40 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
7aa50 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
7aa60 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
7aa70 53 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  ST.#endif../*.**
7aa80 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
7aa90 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76  outines are conv
7aaa0 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73  enience wrappers
7aab0 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a   around methods.
7aac0 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ** of the sqlite
7aad0 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20  3_file object.  
7aae0 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a  This is mostly j
7aaf0 75 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 75  ust syntactic su
7ab00 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74  gar. All.** of t
7ab10 68 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d  his would be com
7ab20 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 69  pletely automati
7ab30 63 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 65  c if SQLite were
7ab40 20 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20   coded using.** 
7ab50 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 70  C++ instead of p
7ab60 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53  lain old C..*/.S
7ab70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
7ab80 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65  t sqlite3OsClose
7ab90 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
7aba0 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  Id){.  int rc = 
7abb0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
7abc0 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29   pId->pMethods )
7abd0 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d 3e  {.    rc = pId->
7abe0 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65  pMethods->xClose
7abf0 28 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e  (pId);.    pId->
7ac00 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20  pMethods = 0;.  
7ac10 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
7ac20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7ac30 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61  int sqlite3OsRea
7ac40 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  d(sqlite3_file *
7ac50 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  id, void *pBuf, 
7ac60 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66  int amt, i64 off
7ac70 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41  set){.  DO_OS_MA
7ac80 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74  LLOC_TEST;.  ret
7ac90 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  urn id->pMethods
7aca0 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75 66  ->xRead(id, pBuf
7acb0 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a  , amt, offset);.
7acc0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
7acd0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72   int sqlite3OsWr
7ace0 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ite(sqlite3_file
7acf0 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64   *id, const void
7ad00 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c   *pBuf, int amt,
7ad10 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20   i64 offset){.  
7ad20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
7ad30 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e  T;.  return id->
7ad40 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 74 65  pMethods->xWrite
7ad50 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20  (id, pBuf, amt, 
7ad60 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54  offset);.}.SQLIT
7ad70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7ad80 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28  lite3OsTruncate(
7ad90 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
7ada0 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 20 72  , i64 size){.  r
7adb0 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
7adc0 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 69 64  ds->xTruncate(id
7add0 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54  , size);.}.SQLIT
7ade0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7adf0 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71 6c 69  lite3OsSync(sqli
7ae00 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
7ae10 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f 5f 4f  t flags){.  DO_O
7ae20 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20  S_MALLOC_TEST;. 
7ae30 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
7ae40 68 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c 20  hods->xSync(id, 
7ae50 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 45  flags);.}.SQLITE
7ae60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
7ae70 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73  ite3OsFileSize(s
7ae80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
7ae90 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20   i64 *pSize){.  
7aea0 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
7aeb0 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e  T;.  return id->
7aec0 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53  pMethods->xFileS
7aed0 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a  ize(id, pSize);.
7aee0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
7aef0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f   int sqlite3OsLo
7af00 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
7af10 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70  *id, int lockTyp
7af20 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c  e){.  DO_OS_MALL
7af30 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72  OC_TEST;.  retur
7af40 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
7af50 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79  xLock(id, lockTy
7af60 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  pe);.}.SQLITE_PR
7af70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7af80 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  3OsUnlock(sqlite
7af90 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
7afa0 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74  lockType){.  ret
7afb0 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  urn id->pMethods
7afc0 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f  ->xUnlock(id, lo
7afd0 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54  ckType);.}.SQLIT
7afe0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7aff0 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65  lite3OsCheckRese
7b000 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33  rvedLock(sqlite3
7b010 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a  _file *id, int *
7b020 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f  pResOut){.  DO_O
7b030 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20  S_MALLOC_TEST;. 
7b040 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
7b050 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65 73 65  hods->xCheckRese
7b060 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70 52 65  rvedLock(id, pRe
7b070 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  sOut);.}.SQLITE_
7b080 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
7b090 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
7b0a0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
7b0b0 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20  d, int op, void 
7b0c0 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75 72 6e  *pArg){.  return
7b0d0 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
7b0e0 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 20  FileControl(id, 
7b0f0 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53 51 4c  op, pArg);.}.SQL
7b100 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7b110 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53  sqlite3OsSectorS
7b120 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ize(sqlite3_file
7b130 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 2a 78   *id){.  int (*x
7b140 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c 69  SectorSize)(sqli
7b150 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 64 2d  te3_file*) = id-
7b160 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 63 74  >pMethods->xSect
7b170 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e  orSize;.  return
7b180 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20 3f 20   (xSectorSize ? 
7b190 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64 29 20  xSectorSize(id) 
7b1a0 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  : SQLITE_DEFAULT
7b1b0 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d  _SECTOR_SIZE);.}
7b1c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7b1d0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76  int sqlite3OsDev
7b1e0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
7b1f0 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
7b200 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 69  *id){.  return i
7b210 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 44 65  d->pMethods->xDe
7b220 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
7b230 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ics(id);.}../*.*
7b240 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70  * The next group
7b250 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 65   of routines are
7b260 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61   convenience wra
7b270 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68 65  ppers around the
7b280 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 73 2e  .** VFS methods.
7b290 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
7b2a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
7b2b0 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  Open(.  sqlite3_
7b2c0 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f  vfs *pVfs, .  co
7b2d0 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c  nst char *zPath,
7b2e0 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   .  sqlite3_file
7b2f0 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74 20   *pFile, .  int 
7b300 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70  flags, .  int *p
7b310 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 69 6e  FlagsOut.){.  in
7b320 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 5f 4d 41  t rc;.  DO_OS_MA
7b330 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 63 20  LLOC_TEST;.  rc 
7b340 3d 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56  = pVfs->xOpen(pV
7b350 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c 65  fs, zPath, pFile
7b360 2c 20 66 6c 61 67 73 2c 20 70 46 6c 61 67 73 4f  , flags, pFlagsO
7b370 75 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  ut);.  assert( r
7b380 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
7b390 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 3d  pFile->pMethods=
7b3a0 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  =0 );.  return r
7b3b0 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  c;.}.SQLITE_PRIV
7b3c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
7b3d0 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f  sDelete(sqlite3_
7b3e0 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74  vfs *pVfs, const
7b3f0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e   char *zPath, in
7b400 74 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 72 65  t dirSync){.  re
7b410 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65  turn pVfs->xDele
7b420 74 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20  te(pVfs, zPath, 
7b430 64 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49  dirSync);.}.SQLI
7b440 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
7b450 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a  qlite3OsAccess(.
7b460 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
7b470 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  Vfs, .  const ch
7b480 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e  ar *zPath, .  in
7b490 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20  t flags, .  int 
7b4a0 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 44 4f  *pResOut.){.  DO
7b4b0 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b  _OS_MALLOC_TEST;
7b4c0 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e  .  return pVfs->
7b4d0 78 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 50  xAccess(pVfs, zP
7b4e0 61 74 68 2c 20 66 6c 61 67 73 2c 20 70 52 65 73  ath, flags, pRes
7b4f0 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  Out);.}.SQLITE_P
7b500 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
7b510 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e3OsFullPathname
7b520 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
7b530 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20  *pVfs, .  const 
7b540 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20  char *zPath, .  
7b550 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c 20 0a 20  int nPathOut, . 
7b560 20 63 68 61 72 20 2a 7a 50 61 74 68 4f 75 74 0a   char *zPathOut.
7b570 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73  ){.  return pVfs
7b580 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  ->xFullPathname(
7b590 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e 50 61  pVfs, zPath, nPa
7b5a0 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75 74 29  thOut, zPathOut)
7b5b0 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
7b5c0 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
7b5d0 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52  ENSION.SQLITE_PR
7b5e0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
7b5f0 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69  te3OsDlOpen(sqli
7b600 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63  te3_vfs *pVfs, c
7b610 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
7b620 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73  ){.  return pVfs
7b630 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73 2c 20  ->xDlOpen(pVfs, 
7b640 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49 54 45  zPath);.}.SQLITE
7b650 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
7b660 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 73  lite3OsDlError(s
7b670 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
7b680 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61  , int nByte, cha
7b690 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 70  r *zBufOut){.  p
7b6a0 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28 70 56  Vfs->xDlError(pV
7b6b0 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 4f  fs, nByte, zBufO
7b6c0 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ut);.}.SQLITE_PR
7b6d0 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c  IVATE void (*sql
7b6e0 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 6c 69  ite3OsDlSym(sqli
7b6f0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76  te3_vfs *pVfs, v
7b700 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f 6e 73  oid *pHdle, cons
7b710 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29 28 76  t char *zSym))(v
7b720 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  oid){.  return p
7b730 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56 66 73  Vfs->xDlSym(pVfs
7b740 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29 3b 0a  , pHdle, zSym);.
7b750 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
7b760 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44   void sqlite3OsD
7b770 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76  lClose(sqlite3_v
7b780 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a  fs *pVfs, void *
7b790 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56 66 73  pHandle){.  pVfs
7b7a0 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c  ->xDlClose(pVfs,
7b7b0 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e   pHandle);.}.#en
7b7c0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
7b7d0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
7b7e0 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  N */.SQLITE_PRIV
7b7f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
7b800 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  sRandomness(sqli
7b810 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
7b820 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a  nt nByte, char *
7b830 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 74 75  zBufOut){.  retu
7b840 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 6f 6d  rn pVfs->xRandom
7b850 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79 74 65  ness(pVfs, nByte
7b860 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51  , zBufOut);.}.SQ
7b870 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
7b880 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28   sqlite3OsSleep(
7b890 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
7b8a0 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a  s, int nMicro){.
7b8b0 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78    return pVfs->x
7b8c0 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d 69 63  Sleep(pVfs, nMic
7b8d0 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ro);.}.SQLITE_PR
7b8e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7b8f0 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 73  3OsCurrentTime(s
7b900 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
7b910 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d 65 4f  , double *pTimeO
7b920 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  ut){.  return pV
7b930 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69 6d 65  fs->xCurrentTime
7b940 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 74 29  (pVfs, pTimeOut)
7b950 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
7b960 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
7b970 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73  sOpenMalloc(.  s
7b980 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
7b990 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
7b9a0 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c 69 74  *zFile, .  sqlit
7b9b0 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65  e3_file **ppFile
7b9c0 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a  , .  int flags,.
7b9d0 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73    int *pOutFlags
7b9e0 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
7b9f0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73  QLITE_NOMEM;.  s
7ba00 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
7ba10 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 28 73  le;.  pFile = (s
7ba20 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71  qlite3_file *)sq
7ba30 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56 66 73  lite3Malloc(pVfs
7ba40 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69  ->szOsFile);.  i
7ba50 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  f( pFile ){.    
7ba60 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
7ba70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20  en(pVfs, zFile, 
7ba80 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70 4f  pFile, flags, pO
7ba90 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66  utFlags);.    if
7baa0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
7bab0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
7bac0 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20  _free(pFile);.  
7bad0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a    }else{.      *
7bae0 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 3b 0a  ppFile = pFile;.
7baf0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
7bb00 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
7bb10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
7bb20 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73  te3OsCloseFree(s
7bb30 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
7bb40 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  le){.  int rc = 
7bb50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73  SQLITE_OK;.  ass
7bb60 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
7bb70 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 6c  rc = sqlite3OsCl
7bb80 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 73 71  ose(pFile);.  sq
7bb90 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65  lite3_free(pFile
7bba0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
7bbb0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c 69 73  }../*.** The lis
7bbc0 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73 74 65  t of all registe
7bbd0 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e  red VFS implemen
7bbe0 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74  tations..*/.stat
7bbf0 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ic sqlite3_vfs *
7bc00 20 53 51 4c 49 54 45 5f 57 53 44 20 76 66 73 4c   SQLITE_WSD vfsL
7bc10 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  ist = 0;.#define
7bc20 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 41 4c 28   vfsList GLOBAL(
7bc30 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76  sqlite3_vfs *, v
7bc40 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c  fsList)../*.** L
7bc50 6f 63 61 74 65 20 61 20 56 46 53 20 62 79 20 6e  ocate a VFS by n
7bc60 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e 61 6d 65  ame.  If no name
7bc70 20 69 73 20 67 69 76 65 6e 2c 20 73 69 6d 70 6c   is given, simpl
7bc80 79 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20  y return the.** 
7bc90 66 69 72 73 74 20 56 46 53 20 6f 6e 20 74 68 65  first VFS on the
7bca0 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45   list..*/.SQLITE
7bcb0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 66 73  _API sqlite3_vfs
7bcc0 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69   *sqlite3_vfs_fi
7bcd0 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  nd(const char *z
7bce0 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  Vfs){.  sqlite3_
7bcf0 76 66 73 20 2a 70 56 66 73 20 3d 20 30 3b 0a 23  vfs *pVfs = 0;.#
7bd00 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
7bd10 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d  SAFE.  sqlite3_m
7bd20 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 23 65 6e  utex *mutex;.#en
7bd30 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
7bd40 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
7bd50 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69  .  int rc = sqli
7bd60 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
7bd70 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
7bd80 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69  urn 0;.#endif.#i
7bd90 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
7bda0 41 46 45 0a 20 20 6d 75 74 65 78 20 3d 20 73 71  AFE.  mutex = sq
7bdb0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
7bdc0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
7bdd0 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e  TIC_MASTER);.#en
7bde0 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  dif.  sqlite3_mu
7bdf0 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
7be00 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 3d 20 76  ;.  for(pVfs = v
7be10 66 73 4c 69 73 74 3b 20 70 56 66 73 3b 20 70 56  fsList; pVfs; pV
7be20 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 74 29 7b  fs=pVfs->pNext){
7be30 0a 20 20 20 20 69 66 28 20 7a 56 66 73 3d 3d 30  .    if( zVfs==0
7be40 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66   ) break;.    if
7be50 28 20 73 74 72 63 6d 70 28 7a 56 66 73 2c 20 70  ( strcmp(zVfs, p
7be60 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29  Vfs->zName)==0 )
7be70 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71   break;.  }.  sq
7be80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
7be90 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  e(mutex);.  retu
7bea0 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pVfs;.}../*.*
7beb0 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53 20 66  * Unlink a VFS f
7bec0 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c  rom the linked l
7bed0 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ist.*/.static vo
7bee0 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 73 71 6c  id vfsUnlink(sql
7bef0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b  ite3_vfs *pVfs){
7bf00 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
7bf10 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71  e3_mutex_held(sq
7bf20 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
7bf30 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
7bf40 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29 3b 0a  TIC_MASTER)) );.
7bf50 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29 7b    if( pVfs==0 ){
7bf60 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f  .    /* No-op */
7bf70 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66 73  .  }else if( vfs
7bf80 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20  List==pVfs ){.  
7bf90 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66 73    vfsList = pVfs
7bfa0 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65  ->pNext;.  }else
7bfb0 20 69 66 28 20 76 66 73 4c 69 73 74 20 29 7b 0a   if( vfsList ){.
7bfc0 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20      sqlite3_vfs 
7bfd0 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20  *p = vfsList;.  
7bfe0 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e 65 78    while( p->pNex
7bff0 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70  t && p->pNext!=p
7c000 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 20 3d  Vfs ){.      p =
7c010 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d   p->pNext;.    }
7c020 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78  .    if( p->pNex
7c030 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20  t==pVfs ){.     
7c040 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73   p->pNext = pVfs
7c050 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  ->pNext;.    }. 
7c060 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69   }.}../*.** Regi
7c070 73 74 65 72 20 61 20 56 46 53 20 77 69 74 68 20  ster a VFS with 
7c080 74 68 65 20 73 79 73 74 65 6d 2e 20 20 49 74 20  the system.  It 
7c090 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 20 72  is harmless to r
7c0a0 65 67 69 73 74 65 72 20 74 68 65 20 73 61 6d 65  egister the same
7c0b0 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70 6c 65  .** VFS multiple
7c0c0 20 74 69 6d 65 73 2e 20 20 54 68 65 20 6e 65 77   times.  The new
7c0d0 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 65   VFS becomes the
7c0e0 20 64 65 66 61 75 6c 74 20 69 66 20 6d 61 6b 65   default if make
7c0f0 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75 65 2e  Dflt is.** true.
7c100 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
7c110 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72  nt sqlite3_vfs_r
7c120 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f  egister(sqlite3_
7c130 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d  vfs *pVfs, int m
7c140 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73 71 6c 69  akeDflt){.  sqli
7c150 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
7c160 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 0;.#ifndef SQ
7c170 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
7c180 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71  IT.  int rc = sq
7c190 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
7c1a0 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  ();.  if( rc ) r
7c1b0 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66  eturn rc;.#endif
7c1c0 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  .  mutex = sqlit
7c1d0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
7c1e0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
7c1f0 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c 69  _MASTER);.  sqli
7c200 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
7c210 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c  mutex);.  vfsUnl
7c220 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 69 66 28  ink(pVfs);.  if(
7c230 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20 76 66 73   makeDflt || vfs
7c240 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  List==0 ){.    p
7c250 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73  Vfs->pNext = vfs
7c260 4c 69 73 74 3b 0a 20 20 20 20 76 66 73 4c 69 73  List;.    vfsLis
7c270 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 65 6c 73  t = pVfs;.  }els
7c280 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e 65  e{.    pVfs->pNe
7c290 78 74 20 3d 20 76 66 73 4c 69 73 74 2d 3e 70 4e  xt = vfsList->pN
7c2a0 65 78 74 3b 0a 20 20 20 20 76 66 73 4c 69 73 74  ext;.    vfsList
7c2b0 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 3b 0a  ->pNext = pVfs;.
7c2c0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 76 66 73    }.  assert(vfs
7c2d0 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
7c2e0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
7c2f0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
7c300 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
7c310 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 20 56  * Unregister a V
7c320 46 53 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  FS so that it is
7c330 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63 63 65 73   no longer acces
7c340 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sible..*/.SQLITE
7c350 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
7c360 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28  _vfs_unregister(
7c370 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
7c380 73 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  s){.#if SQLITE_T
7c390 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69  HREADSAFE.  sqli
7c3a0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
7c3b0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
7c3c0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
7c3d0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
7c3e0 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ;.#endif.  sqlit
7c3f0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
7c400 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69  utex);.  vfsUnli
7c410 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73 71 6c 69  nk(pVfs);.  sqli
7c420 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
7c430 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
7c440 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
7c450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
7c460 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a  nd of os.c *****
7c470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
7c4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
7c4b0 65 67 69 6e 20 66 69 6c 65 20 66 61 75 6c 74 2e  egin file fault.
7c4c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
7c4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
7c4f0 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e 20 32 32  *.** 2008 Jan 22
7c500 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
7c510 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
7c520 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
7c530 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
7c540 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
7c550 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
7c560 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
7c570 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
7c580 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
7c590 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
7c5a0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
7c5b0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
7c5c0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
7c5d0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
7c5e0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
7c5f0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
7c600 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
7c610 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
7c620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
7c660 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 61 75 6c 74  **.** $Id: fault
7c670 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 30  .c,v 1.11 2008/0
7c680 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 20 64 72  9/02 00:52:52 dr
7c690 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
7c6a0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
7c6b0 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 73 75 70  ains code to sup
7c6c0 70 6f 72 74 20 74 68 65 20 63 6f 6e 63 65 70 74  port the concept
7c6d0 20 6f 66 20 22 62 65 6e 69 67 6e 22 20 0a 2a 2a   of "benign" .**
7c6e0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
7c6f0 20 28 77 68 65 6e 20 74 68 65 20 78 4d 61 6c 6c   (when the xMall
7c700 6f 63 28 29 20 6f 72 20 78 52 65 61 6c 6c 6f 63  oc() or xRealloc
7c710 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  () method of the
7c720 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  .** sqlite3_mem_
7c730 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72  methods structur
7c740 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63  e fails to alloc
7c750 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d  ate a block of m
7c760 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 72 65 74  emory.** and ret
7c770 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20  urns 0). .**.** 
7c780 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  Most malloc fail
7c790 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e  ures are non-ben
7c7a0 69 67 6e 2e 20 41 66 74 65 72 20 74 68 65 79 20  ign. After they 
7c7b0 6f 63 63 75 72 2c 20 53 51 4c 69 74 65 0a 2a 2a  occur, SQLite.**
7c7c0 20 61 62 61 6e 64 6f 6e 73 20 74 68 65 20 63 75   abandons the cu
7c7d0 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrent operation 
7c7e0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65  and returns an e
7c7f0 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c  rror code (usual
7c800 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d  ly.** SQLITE_NOM
7c810 45 4d 29 20 74 6f 20 74 68 65 20 75 73 65 72 2e  EM) to the user.
7c820 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 74 69   However, someti
7c830 6d 65 73 20 61 20 66 61 75 6c 74 20 69 73 20 6e  mes a fault is n
7c840 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a  ot necessarily.*
7c850 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20 65 78 61  * fatal. For exa
7c860 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61 6c 6c 6f  mple, if a mallo
7c870 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 72 65  c fails while re
7c880 73 69 7a 69 6e 67 20 61 20 68 61 73 68 20 74 61  sizing a hash ta
7c890 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a 20 69 73  ble, this .** is
7c8a0 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72 65 63 6f   completely reco
7c8b0 76 65 72 61 62 6c 65 20 73 69 6d 70 6c 79 20 62  verable simply b
7c8c0 79 20 6e 6f 74 20 63 61 72 72 79 69 6e 67 20 6f  y not carrying o
7c8d0 75 74 20 74 68 65 20 72 65 73 69 7a 65 2e 20 54  ut the resize. T
7c8e0 68 65 20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c  he .** hash tabl
7c8f0 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20  e will continue 
7c900 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d  to function norm
7c910 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d 61 6c 6c  ally.  So a mall
7c920 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 64  oc failure .** d
7c930 75 72 69 6e 67 20 61 20 68 61 73 68 20 74 61 62  uring a hash tab
7c940 6c 65 20 72 65 73 69 7a 65 20 69 73 20 61 20 62  le resize is a b
7c950 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a  enign fault..*/.
7c960 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
7c970 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
7c980 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c  ST../*.** Global
7c990 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74   variables..*/.t
7c9a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 65  ypedef struct Be
7c9b0 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20  nignMallocHooks 
7c9c0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b  BenignMallocHook
7c9d0 73 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  s;.static SQLITE
7c9e0 5f 57 53 44 20 73 74 72 75 63 74 20 42 65 6e 69  _WSD struct Beni
7c9f0 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a  gnMallocHooks {.
7ca00 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e    void (*xBenign
7ca10 42 65 67 69 6e 29 28 76 6f 69 64 29 3b 0a 20 20  Begin)(void);.  
7ca20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e  void (*xBenignEn
7ca30 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73 71 6c 69  d)(void);.} sqli
7ca40 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20  te3Hooks = { 0, 
7ca50 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73  0 };../* The "ws
7ca60 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 77 69  dHooks" macro wi
7ca70 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68  ll resolve to th
7ca80 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 42 65  e appropriate Be
7ca90 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a  nignMallocHooks.
7caa0 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 49  ** structure.  I
7cab0 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  f writable stati
7cac0 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70  c data is unsupp
7cad0 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72  orted on the tar
7cae0 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20  get,.** we have 
7caf0 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74  to locate the st
7cb00 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75  ate vector at ru
7cb10 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20  n-time.  In the 
7cb20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63  more common.** c
7cb30 61 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62  ase where writab
7cb40 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
7cb50 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64  s supported, wsd
7cb60 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66 65 72 20  Hooks can refer 
7cb70 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74  directly.** to t
7cb80 68 65 20 22 73 71 6c 69 74 65 33 48 6f 6f 6b 73  he "sqlite3Hooks
7cb90 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64  " state vector d
7cba0 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a  eclared above..*
7cbb0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
7cbc0 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e  OMIT_WSD.# defin
7cbd0 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 20 5c  e wsdHooksInit \
7cbe0 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48  .  BenignMallocH
7cbf0 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c 4f 42 41  ooks *x = &GLOBA
7cc00 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f  L(BenignMallocHo
7cc10 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f 6f 6b 73  oks,sqlite3Hooks
7cc20 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f  ).# define wsdHo
7cc30 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23  oks x[0].#else.#
7cc40 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73   define wsdHooks
7cc50 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73  Init.# define ws
7cc60 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65 33 48 6f  dHooks sqlite3Ho
7cc70 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  oks.#endif.../*.
7cc80 2a 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b  ** Register hook
7cc90 73 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 73  s to call when s
7cca0 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
7ccb0 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a  nMalloc() and.**
7ccc0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
7ccd0 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65 20 63 61  nMalloc() are ca
7cce0 6c 6c 65 64 2c 20 72 65 73 70 65 63 74 69 76 65  lled, respective
7ccf0 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
7cd00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
7cd10 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f  e3BenignMallocHo
7cd20 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28 2a 78 42  oks(.  void (*xB
7cd30 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64  enignBegin)(void
7cd40 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e  ),.  void (*xBen
7cd50 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 0a 29 7b  ignEnd)(void).){
7cd60 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b  .  wsdHooksInit;
7cd70 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e  .  wsdHooks.xBen
7cd80 69 67 6e 42 65 67 69 6e 20 3d 20 78 42 65 6e 69  ignBegin = xBeni
7cd90 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73 64 48 6f  gnBegin;.  wsdHo
7cda0 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 3d  oks.xBenignEnd =
7cdb0 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a   xBenignEnd;.}..
7cdc0 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73 71 6c 69  /*.** This (sqli
7cdd0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
7cde0 6f 63 28 29 29 20 69 73 20 63 61 6c 6c 65 64 20  oc()) is called 
7cdf0 62 79 20 53 51 4c 69 74 65 20 63 6f 64 65 20 74  by SQLite code t
7ce00 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 0a  o indicate that.
7ce10 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61  ** subsequent ma
7ce20 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72  lloc failures ar
7ce30 65 20 62 65 6e 69 67 6e 2e 20 41 20 63 61 6c 6c  e benign. A call
7ce40 20 74 6f 20 73 71 6c 69 74 65 33 45 6e 64 42 65   to sqlite3EndBe
7ce50 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20  nignMalloc().** 
7ce60 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 73  indicates that s
7ce70 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63  ubsequent malloc
7ce80 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f   failures are no
7ce90 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c  n-benign..*/.SQL
7cea0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
7ceb0 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
7cec0 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b  ignMalloc(void){
7ced0 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b  .  wsdHooksInit;
7cee0 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e  .  if( wsdHooks.
7cef0 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 29 7b 0a  xBenignBegin ){.
7cf00 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65      wsdHooks.xBe
7cf10 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a 20 20 7d  nignBegin();.  }
7cf20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
7cf30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e  E void sqlite3En
7cf40 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f  dBenignMalloc(vo
7cf50 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49  id){.  wsdHooksI
7cf60 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f  nit;.  if( wsdHo
7cf70 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 29  oks.xBenignEnd )
7cf80 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78  {.    wsdHooks.x
7cf90 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a 20 20 7d  BenignEnd();.  }
7cfa0 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20  .}..#endif   /* 
7cfb0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
7cfc0 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
7cfd0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
7cfe0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 61 75 6c  **** End of faul
7cff0 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
7d000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d020 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
7d030 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
7d040 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem0.c *********
7d050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d070 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
7d080 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a  October 28.**.**
7d090 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
7d0a0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
7d0b0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
7d0c0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
7d0d0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
7d0e0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
7d0f0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
7d100 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
7d110 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
7d120 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
7d130 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
7d140 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
7d150 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
7d160 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
7d170 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
7d180 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
7d190 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
7d1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
7d1f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
7d200 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72  ns a no-op memor
7d210 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69  y allocation dri
7d220 76 65 72 73 20 66 6f 72 20 75 73 65 20 77 68 65  vers for use whe
7d230 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f  n.** SQLITE_ZERO
7d240 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e  _MALLOC is defin
7d250 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74  ed.  The allocat
7d260 69 6f 6e 20 64 72 69 76 65 72 73 20 69 6d 70 6c  ion drivers impl
7d270 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 72 65 20  emented.** here 
7d280 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 53 51  always fail.  SQ
7d290 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 6f 70  Lite will not op
7d2a0 65 72 61 74 65 20 77 69 74 68 20 74 68 65 73 65  erate with these
7d2b0 20 64 72 69 76 65 72 73 2e 20 20 54 68 65 73 65   drivers.  These
7d2c0 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c 79 20 70  .** are merely p
7d2d0 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20 20 52 65  laceholders.  Re
7d2e0 61 6c 20 64 72 69 76 65 72 73 20 6d 75 73 74 20  al drivers must 
7d2f0 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20 75  be substituted u
7d300 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  sing.** sqlite3_
7d310 63 6f 6e 66 69 67 28 29 20 62 65 66 6f 72 65 20  config() before 
7d320 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 70 65 72  SQLite will oper
7d330 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ate..**.** $Id: 
7d340 6d 65 6d 30 2e 63 2c 76 20 31 2e 31 20 32 30 30  mem0.c,v 1.1 200
7d350 38 2f 31 30 2f 32 38 20 31 38 3a 35 38 3a 32 30  8/10/28 18:58:20
7d360 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
7d370 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f  *.** This versio
7d380 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  n of the memory 
7d390 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65  allocator is the
7d3a0 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73   default.  It is
7d3b0 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 6e 6f  .** used when no
7d3c0 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c   other memory al
7d3d0 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65 63 69  locator is speci
7d3e0 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d 70 69  fied using compi
7d3f0 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f  le-time.** macro
7d400 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  s..*/.#ifdef SQL
7d410 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a  ITE_ZERO_MALLOC.
7d420 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72  ./*.** No-op ver
7d430 73 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 6d 65 6d  sions of all mem
7d440 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
7d450 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73 74 61 74 69  outines.*/.stati
7d460 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d  c void *sqlite3M
7d470 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79  emMalloc(int nBy
7d480 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d  te){ return 0; }
7d490 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
7d4a0 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64  ite3MemFree(void
7d4b0 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72   *pPrior){ retur
7d4c0 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  n; }.static void
7d4d0 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c   *sqlite3MemReal
7d4e0 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  loc(void *pPrior
7d4f0 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65  , int nByte){ re
7d500 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63  turn 0; }.static
7d510 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53   int sqlite3MemS
7d520 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ize(void *pPrior
7d530 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73  ){ return 0; }.s
7d540 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
7d550 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20  3MemRoundup(int 
7d560 6e 29 7b 20 72 65 74 75 72 6e 20 6e 3b 20 7d 0a  n){ return n; }.
7d570 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
7d580 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a  e3MemInit(void *
7d590 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e  NotUsed){ return
7d5a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74   SQLITE_OK; }.st
7d5b0 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
7d5c0 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69  3MemShutdown(voi
7d5d0 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74  d *NotUsed){ ret
7d5e0 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  urn; }../*.** Th
7d5f0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
7d600 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69  e only routine i
7d610 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68  n this file with
7d620 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67   external linkag
7d630 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74  e..**.** Populat
7d640 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20  e the low-level 
7d650 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
7d660 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74  n function point
7d670 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65  ers in.** sqlite
7d680 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20  3GlobalConfig.m 
7d690 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f  with pointers to
7d6a0 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   the routines in
7d6b0 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53   this file..*/.S
7d6c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7d6d0 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74  id sqlite3MemSet
7d6e0 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20  Default(void){. 
7d6f0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
7d700 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
7d710 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73  s defaultMethods
7d720 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65   = {.     sqlite
7d730 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20  3MemMalloc,.    
7d740 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c   sqlite3MemFree,
7d750 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
7d760 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71  Realloc,.     sq
7d770 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20  lite3MemSize,.  
7d780 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75     sqlite3MemRou
7d790 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74  ndup,.     sqlit
7d7a0 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20  e3MemInit,.     
7d7b0 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f  sqlite3MemShutdo
7d7c0 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a  wn,.     0.  };.
7d7d0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67    sqlite3_config
7d7e0 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  (SQLITE_CONFIG_M
7d7f0 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d  ALLOC, &defaultM
7d800 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64  ethods);.}..#end
7d810 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 5a 45 52  if /* SQLITE_ZER
7d820 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a  O_MALLOC */../**
7d830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
7d840 20 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a   of mem0.c *****
7d850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
7d880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
7d890 69 6e 20 66 69 6c 65 20 6d 65 6d 31 2e 63 20 2a  in file mem1.c *
7d8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
7d8d0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31  ** 2007 August 1
7d8e0 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  4.**.** The auth
7d8f0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
7d900 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
7d910 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
7d920 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
7d930 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
7d940 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
7d950 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
7d960 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
7d970 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
7d980 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
7d990 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
7d9a0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
7d9b0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
7d9c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
7d9d0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
7d9e0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
7d9f0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
7da00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7da10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7da20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7da30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7da40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
7da50 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65   contains low-le
7da60 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  vel memory alloc
7da70 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f  ation drivers fo
7da80 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65  r when.** SQLite
7da90 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 73 74   will use the st
7daa0 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 72 79  andard C-library
7dab0 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f   malloc/realloc/
7dac0 66 72 65 65 20 69 6e 74 65 72 66 61 63 65 0a 2a  free interface.*
7dad0 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20  * to obtain the 
7dae0 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 2e  memory it needs.
7daf0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
7db00 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d   contains implem
7db10 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
7db20 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72   low-level memor
7db30 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  y allocation.** 
7db40 72 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69  routines specifi
7db50 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ed in the sqlite
7db60 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62  3_mem_methods ob
7db70 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ject..**.** $Id:
7db80 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 33 30 20 32   mem1.c,v 1.30 2
7db90 30 30 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a  009/03/23 04:33:
7dba0 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  33 danielk1977 E
7dbb0 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
7dbc0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
7dbd0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
7dbe0 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 75  tor is the defau
7dbf0 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73  lt.  It is.** us
7dc00 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72  ed when no other
7dc10 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
7dc20 72 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75  r is specified u
7dc30 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  sing compile-tim
7dc40 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  e.** macros..*/.
7dc50 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 59  #ifdef SQLITE_SY
7dc60 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a  STEM_MALLOC../*.
7dc70 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f 63 28 29  ** Like malloc()
7dc80 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 72 20 74  , but remember t
7dc90 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61  he size of the a
7dca0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20  llocation.** so 
7dcb0 74 68 61 74 20 77 65 20 63 61 6e 20 66 69 6e 64  that we can find
7dcc0 20 69 74 20 6c 61 74 65 72 20 75 73 69 6e 67 20   it later using 
7dcd0 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 29  sqlite3MemSize()
7dce0 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73  ..**.** For this
7dcf0 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69   low-level routi
7dd00 6e 65 2c 20 77 65 20 61 72 65 20 67 75 61 72 61  ne, we are guara
7dd10 6e 74 65 65 64 20 74 68 61 74 20 6e 42 79 74 65  nteed that nByte
7dd20 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a 20 63 61  >0 because.** ca
7dd30 73 65 73 20 6f 66 20 6e 42 79 74 65 3c 3d 30 20  ses of nByte<=0 
7dd40 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 63 65 70  will be intercep
7dd50 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69  ted and dealt wi
7dd60 74 68 20 62 79 20 68 69 67 68 65 72 20 6c 65 76  th by higher lev
7dd70 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a  el.** routines..
7dd80 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
7dd90 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
7dda0 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73  (int nByte){.  s
7ddb0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b  qlite3_int64 *p;
7ddc0 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65  .  assert( nByte
7ddd0 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20  >0 );.  nByte = 
7dde0 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20  ROUND8(nByte);. 
7ddf0 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79   p = malloc( nBy
7de00 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20  te+8 );.  if( p 
7de10 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42  ){.    p[0] = nB
7de20 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20  yte;.    p++;.  
7de30 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  }.  return (void
7de40 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c   *)p;.}../*.** L
7de50 69 6b 65 20 66 72 65 65 28 29 20 62 75 74 20 77  ike free() but w
7de60 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74  orks for allocat
7de70 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 20 66 72  ions obtained fr
7de80 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c  om sqlite3MemMal
7de90 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69  loc().** or sqli
7dea0 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 29 2e  te3MemRealloc().
7deb0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20  .**.** For this 
7dec0 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e  low-level routin
7ded0 65 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e  e, we already kn
7dee0 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d  ow that pPrior!=
7def0 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 73 65 73  0 since.** cases
7df00 20 77 68 65 72 65 20 70 50 72 69 6f 72 3d 3d 30   where pPrior==0
7df10 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20   will have been 
7df20 69 6e 74 65 63 65 70 74 65 64 20 61 6e 64 20 64  intecepted and d
7df30 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 62 79 20  ealt with.** by 
7df40 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75  higher-level rou
7df50 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tines..*/.static
7df60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
7df70 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f  Free(void *pPrio
7df80 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  r){.  sqlite3_in
7df90 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65  t64 *p = (sqlite
7dfa0 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b  3_int64*)pPrior;
7dfb0 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f  .  assert( pPrio
7dfc0 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20  r!=0 );.  p--;. 
7dfd0 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a   free(p);.}../*.
7dfe0 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f 63 28  ** Like realloc(
7dff0 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20 61 6c  ).  Resize an al
7e000 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f 75  location previou
7e010 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  sly obtained fro
7e020 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 4d  m.** sqlite3MemM
7e030 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46  alloc()..**.** F
7e040 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65  or this low-leve
7e050 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77 65 20  l interface, we 
7e060 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72  know that pPrior
7e070 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68 65 72  !=0.  Cases wher
7e080 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30 20 77  e.** pPrior==0 w
7e090 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e 20 69  hile have been i
7e0a0 6e 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69  ntercepted by hi
7e0b0 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69  gher-level routi
7e0c0 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69 72 65  ne and.** redire
7e0d0 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e  cted to xMalloc.
7e0e0 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77 65 20    Similarly, we 
7e0f0 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74 65 3e  know that nByte>
7e100 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20 63 61  0 becauses.** ca
7e110 73 65 73 20 77 68 65 72 65 20 6e 42 79 74 65 3c  ses where nByte<
7e120 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65  =0 will have bee
7e130 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79  n intercepted by
7e140 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a   higher-level.**
7e150 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 72 65   routines and re
7e160 64 69 72 65 63 74 65 64 20 74 6f 20 78 46 72 65  directed to xFre
7e170 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
7e180 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61  d *sqlite3MemRea
7e190 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f  lloc(void *pPrio
7e1a0 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  r, int nByte){. 
7e1b0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
7e1c0 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  p = (sqlite3_int
7e1d0 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 73  64*)pPrior;.  as
7e1e0 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20  sert( pPrior!=0 
7e1f0 26 26 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20  && nByte>0 );.  
7e200 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e  nByte = ROUND8(n
7e210 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 28 73 71  Byte);.  p = (sq
7e220 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72  lite3_int64*)pPr
7e230 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 70 20  ior;.  p--;.  p 
7e240 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 42 79  = realloc(p, nBy
7e250 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20  te+8 );.  if( p 
7e260 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42  ){.    p[0] = nB
7e270 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20  yte;.    p++;.  
7e280 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  }.  return (void
7e290 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  *)p;.}../*.** Re
7e2a0 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 61 74  port the allocat
7e2b0 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 72 69  ed size of a pri
7e2c0 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 78  or return from x
7e2d0 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 78  Malloc().** or x
7e2e0 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74  Realloc()..*/.st
7e2f0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
7e300 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50  MemSize(void *pP
7e310 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  rior){.  sqlite3
7e320 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 69 66 28  _int64 *p;.  if(
7e330 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 72 65 74   pPrior==0 ) ret
7e340 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 28 73 71  urn 0;.  p = (sq
7e350 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72  lite3_int64*)pPr
7e360 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 72 65  ior;.  p--;.  re
7e370 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 5d 3b 0a  turn (int)p[0];.
7e380 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75  }../*.** Round u
7e390 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65  p a request size
7e3a0 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c   to the next val
7e3b0 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69  id allocation si
7e3c0 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ze..*/.static in
7e3d0 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e  t sqlite3MemRoun
7e3e0 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65  dup(int n){.  re
7e3f0 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 29 3b 0a  turn ROUND8(n);.
7e400 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
7e410 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e  ize this module.
7e420 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
7e430 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f  qlite3MemInit(vo
7e440 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  id *NotUsed){.  
7e450 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
7e460 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74  (NotUsed);.  ret
7e470 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
7e480 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61  ../*.** Deinitia
7e490 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  lize this module
7e4a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
7e4b0 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64   sqlite3MemShutd
7e4c0 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  own(void *NotUse
7e4d0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
7e4e0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
7e4f0 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
7e500 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
7e510 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75   is the only rou
7e520 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c  tine in this fil
7e530 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20  e with external 
7e540 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  linkage..**.** P
7e550 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d  opulate the low-
7e560 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
7e570 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ocation function
7e580 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20   pointers in.** 
7e590 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
7e5a0 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74  fig.m with point
7e5b0 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69  ers to the routi
7e5c0 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nes in this file
7e5d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
7e5e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
7e5f0 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f  MemSetDefault(vo
7e600 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  id){.  static co
7e610 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
7e620 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d  methods defaultM
7e630 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20  ethods = {.     
7e640 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
7e650 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
7e660 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 6c 69  mFree,.     sqli
7e670 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20  te3MemRealloc,. 
7e680 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69      sqlite3MemSi
7e690 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  ze,.     sqlite3
7e6a0 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20  MemRoundup,.    
7e6b0 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c   sqlite3MemInit,
7e6c0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
7e6d0 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30  Shutdown,.     0
7e6e0 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f  .  };.  sqlite3_
7e6f0 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f  config(SQLITE_CO
7e700 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65  NFIG_MALLOC, &de
7e710 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d  faultMethods);.}
7e720 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
7e730 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43  TE_SYSTEM_MALLOC
7e740 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
7e750 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 31  **** End of mem1
7e760 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
7e770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e790 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
7e7a0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
7e7b0 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem2.c *********
7e7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e7e0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
7e7f0 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20  August 15.**.** 
7e800 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
7e810 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
7e820 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
7e830 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
7e840 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
7e850 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
7e860 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
7e870 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
7e880 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
7e890 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
7e8a0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
7e8b0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
7e8c0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
7e8d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
7e8e0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
7e8f0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
7e900 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
7e910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e950 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
7e960 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
7e970 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  s low-level memo
7e980 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72  ry allocation dr
7e990 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a  ivers for when.*
7e9a0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73  * SQLite will us
7e9b0 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43  e the standard C
7e9c0 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f  -library malloc/
7e9d0 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74  realloc/free int
7e9e0 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74  erface.** to obt
7e9f0 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69  ain the memory i
7ea00 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20 61 64  t needs while ad
7ea10 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61 64 64  ding lots of add
7ea20 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67 69 6e  itional debuggin
7ea30 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  g.** information
7ea40 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63 61 74   to each allocat
7ea50 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ion in order to 
7ea60 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e 64 20  help detect and 
7ea70 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65  fix memory.** le
7ea80 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79 20 75  aks and memory u
7ea90 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a  sage errors..**.
7eaa0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
7eab0 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61  tains implementa
7eac0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77  tions of the low
7ead0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
7eae0 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74  location.** rout
7eaf0 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69  ines specified i
7eb00 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65  n the sqlite3_me
7eb10 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  m_methods object
7eb20 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d  ..**.** $Id: mem
7eb30 32 2e 63 2c 76 20 31 2e 34 35 20 32 30 30 39 2f  2.c,v 1.45 2009/
7eb40 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 33 20 64  03/23 04:33:33 d
7eb50 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
7eb60 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .*/../*.** This 
7eb70 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d  version of the m
7eb80 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
7eb90 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 69 66 20  is used only if 
7eba0 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45  the.** SQLITE_ME
7ebb0 4d 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20  MDEBUG macro is 
7ebc0 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69 66 64 65  defined.*/.#ifde
7ebd0 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55  f SQLITE_MEMDEBU
7ebe0 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 61 63  G../*.** The bac
7ebf0 6b 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 61  ktrace functiona
7ec00 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20 61 76 61  lity is only ava
7ec10 69 6c 61 62 6c 65 20 77 69 74 68 20 47 4c 49 42  ilable with GLIB
7ec20 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 47 4c  C.*/.#ifdef __GL
7ec30 49 42 43 5f 5f 0a 20 20 65 78 74 65 72 6e 20 69  IBC__.  extern i
7ec40 6e 74 20 62 61 63 6b 74 72 61 63 65 28 76 6f 69  nt backtrace(voi
7ec50 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65 78 74 65  d**,int);.  exte
7ec60 72 6e 20 76 6f 69 64 20 62 61 63 6b 74 72 61 63  rn void backtrac
7ec70 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 76 6f 69  e_symbols_fd(voi
7ec80 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74  d*const*,int,int
7ec90 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
7eca0 65 20 62 61 63 6b 74 72 61 63 65 28 41 2c 42 29  e backtrace(A,B)
7ecb0 20 31 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b   1.# define back
7ecc0 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64  trace_symbols_fd
7ecd0 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 0a 0a  (A,B,C).#endif..
7ece0 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 6d 6f 72  /*.** Each memor
7ecf0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f  y allocation loo
7ed00 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ks like this:.**
7ed10 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**  -----------
7ed20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7ed30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7ed40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7ed50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
7ed60 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 62 61 63    | Title |  bac
7ed70 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 20  ktrace pointers 
7ed80 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7c  |  MemBlockHdr |
7ed90 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 7c 20 20    allocation |  
7eda0 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a 20 20 2d  EndGuard |.**  -
7edb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7edc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7edd0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7ede0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7edf0 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68  -------.**.** Th
7ee00 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f  e application co
7ee10 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 61 20 70  de sees only a p
7ee20 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c  ointer to the al
7ee30 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 20 68 61  location.  We ha
7ee40 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b 20 75 70  ve.** to back up
7ee50 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c 6f 63 61   from the alloca
7ee60 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20  tion pointer to 
7ee70 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63  find the MemBloc
7ee80 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a 20 4d 65  kHdr.  The.** Me
7ee90 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c 6c 73 20  mBlockHdr tells 
7eea0 75 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  us the size of t
7eeb0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e  he allocation an
7eec0 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  d the number of.
7eed0 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 70 6f 69  ** backtrace poi
7eee0 6e 74 65 72 73 2e 20 20 54 68 65 72 65 20 69 73  nters.  There is
7eef0 20 61 6c 73 6f 20 61 20 67 75 61 72 64 20 77 6f   also a guard wo
7ef00 72 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  rd at the end of
7ef10 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b   the.** MemBlock
7ef20 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d  Hdr..*/.struct M
7ef30 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a 20 20 69  emBlockHdr {.  i
7ef40 36 34 20 69 53 69 7a 65 3b 20 20 20 20 20 20 20  64 iSize;       
7ef50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7ef60 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
7ef70 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f  is allocation */
7ef80 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
7ef90 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c 20 2a 70  ckHdr *pNext, *p
7efa0 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64  Prev;  /* Linked
7efb0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 66   list of all unf
7efc0 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20  reed memory */. 
7efd0 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 65   char nBacktrace
7efe0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
7eff0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
7f000 66 20 62 61 63 6b 74 72 61 63 65 73 20 6f 6e 20  f backtraces on 
7f010 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20  this alloc */.  
7f020 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 65 53  char nBacktraceS
7f030 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 20 20 20  lots;           
7f040 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65      /* Available
7f050 20 62 61 63 6b 74 72 61 63 65 20 73 6c 6f 74 73   backtrace slots
7f060 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e 54 69 74   */.  short nTit
7f070 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  le;             
7f080 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
7f090 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 69 6e 63  es of title; inc
7f0a0 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f 0a 20 20  ludes '\0' */.  
7f0b0 69 6e 74 20 69 46 6f 72 65 47 75 61 72 64 3b 20  int iForeGuard; 
7f0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f0d0 20 20 20 20 2f 2a 20 47 75 61 72 64 20 77 6f 72      /* Guard wor
7f0e0 64 20 66 6f 72 20 73 61 6e 69 74 79 20 2a 2f 0a  d for sanity */.
7f0f0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61 72 64 20  };../*.** Guard 
7f100 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66 69 6e 65  words.*/.#define
7f110 20 46 4f 52 45 47 55 41 52 44 20 30 78 38 30 46   FOREGUARD 0x80F
7f120 35 45 31 35 33 0a 23 64 65 66 69 6e 65 20 52 45  5E153.#define RE
7f130 41 52 47 55 41 52 44 20 30 78 45 34 36 37 36 42  ARGUARD 0xE4676B
7f140 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72  53../*.** Number
7f150 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69 7a 65 20   of malloc size 
7f160 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f 20 74 72  increments to tr
7f170 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ack..*/.#define 
7f180 4e 43 53 49 5a 45 20 20 31 30 30 30 0a 0a 2f 2a  NCSIZE  1000../*
7f190 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 73  .** All of the s
7f1a0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20  tatic variables 
7f1b0 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64  used by this mod
7f1c0 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 65  ule are collecte
7f1d0 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67  d.** into a sing
7f1e0 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d  le structure nam
7f1f0 65 64 20 22 6d 65 6d 22 2e 20 20 54 68 69 73 20  ed "mem".  This 
7f200 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a  is to keep the.*
7f210 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  * static variabl
7f220 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64  es organized and
7f230 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73   to reduce names
7f240 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a  pace pollution.*
7f250 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75  * when this modu
7f260 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77  le is combined w
7f270 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65  ith other in the
7f280 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a   amalgamation..*
7f290 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
7f2a0 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d  {.  .  /*.  ** M
7f2b0 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  utex to control 
7f2c0 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65  access to the me
7f2d0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
7f2e0 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a  subsystem..  */.
7f2f0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
7f300 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20  *mutex;..  /*.  
7f310 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 61 69 6c  ** Head and tail
7f320 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   of a linked lis
7f330 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 74 61 6e  t of all outstan
7f340 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  ding allocations
7f350 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d  .  */.  struct M
7f360 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 46 69 72  emBlockHdr *pFir
7f370 73 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d  st;.  struct Mem
7f380 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61 73 74 3b  BlockHdr *pLast;
7f390 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  .  .  /*.  ** Th
7f3a0 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76 65  e number of leve
7f3b0 6c 73 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20  ls of backtrace 
7f3c0 74 6f 20 73 61 76 65 20 69 6e 20 6e 65 77 20 61  to save in new a
7f3d0 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f  llocations..  */
7f3e0 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74 72 61 63  .  int nBacktrac
7f3f0 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 61 63  e;.  void (*xBac
7f400 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e 74  ktrace)(int, int
7f410 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f  , void **);..  /
7f420 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20 74 65 78  *.  ** Title tex
7f430 74 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 20 66  t to insert in f
7f440 72 6f 6e 74 20 6f 66 20 65 61 63 68 20 62 6c 6f  ront of each blo
7f450 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  ck.  */.  int nT
7f460 69 74 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  itle;        /* 
7f470 42 79 74 65 73 20 6f 66 20 7a 54 69 74 6c 65 20  Bytes of zTitle 
7f480 74 6f 20 73 61 76 65 2e 20 20 49 6e 63 6c 75 64  to save.  Includ
7f490 65 73 20 27 5c 30 27 20 61 6e 64 20 70 61 64 64  es '\0' and padd
7f4a0 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54  ing */.  char zT
7f4b0 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f 2a 20 54  itle[100];  /* T
7f4c0 68 65 20 74 69 74 6c 65 20 74 65 78 74 20 2a 2f  he title text */
7f4d0 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c  ..  /* .  ** sql
7f4e0 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c  ite3MallocDisall
7f4f0 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e 74 73 20  ow() increments 
7f500 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  the following co
7f510 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73 71 6c 69  unter..  ** sqli
7f520 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29  te3MallocAllow()
7f530 20 64 65 63 72 65 6d 65 6e 74 73 20 69 74 2e 0a   decrements it..
7f540 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 61 6c    */.  int disal
7f550 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f 74 20 61  low; /* Do not a
7f560 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  llow memory allo
7f570 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a  cation */..  /*.
7f580 20 20 2a 2a 20 47 61 74 68 65 72 20 73 74 61 74    ** Gather stat
7f590 69 73 74 69 63 73 20 6f 6e 20 74 68 65 20 73 69  istics on the si
7f5a0 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c  zes of memory al
7f5b0 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20  locations..  ** 
7f5c0 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20 74 68 65  nAlloc[i] is the
7f5d0 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   number of alloc
7f5e0 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 6f  ation attempts o
7f5f0 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79 74 65 73  f i*8.  ** bytes
7f600 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20 69 73 20  .  i==NCSIZE is 
7f610 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c  the number of al
7f620 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74  location attempt
7f630 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 73  s for.  ** sizes
7f640 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43 53 49 5a   more than NCSIZ
7f650 45 2a 38 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a  E*8 bytes..  */.
7f660 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53    int nAlloc[NCS
7f670 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a 20 54 6f  IZE];      /* To
7f680 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c  tal number of al
7f690 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  locations */.  i
7f6a0 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49  nt nCurrent[NCSI
7f6b0 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75 72 72 65  ZE];    /* Curre
7f6c0 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c  nt number of all
7f6d0 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  ocations */.  in
7f6e0 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e 43 53 49  t mxCurrent[NCSI
7f6f0 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67 68 77 61  ZE];   /* Highwa
7f700 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e 43 75  ter mark for nCu
7f710 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b  rrent */..} mem;
7f720 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20  .../*.** Adjust 
7f730 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 73 74 61  memory usage sta
7f740 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74 61 74 69  tistics.*/.stati
7f750 63 20 76 6f 69 64 20 61 64 6a 75 73 74 53 74 61  c void adjustSta
7f760 74 73 28 69 6e 74 20 69 53 69 7a 65 2c 20 69 6e  ts(int iSize, in
7f770 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b 0a 20 20  t increment){.  
7f780 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44 38 28 69  int i = ROUND8(i
7f790 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66 28 20 69  Size)/8;.  if( i
7f7a0 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a 20 20 20  >NCSIZE-1 ){.   
7f7b0 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d 20 31 3b   i = NCSIZE - 1;
7f7c0 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 63 72 65  .  }.  if( incre
7f7d0 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20 20 6d 65  ment>0 ){.    me
7f7e0 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20  m.nAlloc[i]++;. 
7f7f0 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b     mem.nCurrent[
7f800 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28 20 6d 65  i]++;.    if( me
7f810 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 6d 65  m.nCurrent[i]>me
7f820 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 20 29  m.mxCurrent[i] )
7f830 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d 78 43 75  {.      mem.mxCu
7f840 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e  rrent[i] = mem.n
7f850 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20 20 20 20  Current[i];.    
7f860 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d  }.  }else{.    m
7f870 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2d 2d  em.nCurrent[i]--
7f880 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65  ;.    assert( me
7f890 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 3d 30  m.nCurrent[i]>=0
7f8a0 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   );.  }.}../*.**
7f8b0 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c 6f 63 61   Given an alloca
7f8c0 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68 65 20 4d  tion, find the M
7f8d0 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f 72 20 74  emBlockHdr for t
7f8e0 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a  hat allocation..
7f8f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
7f900 6e 65 20 63 68 65 63 6b 73 20 74 68 65 20 67 75  ne checks the gu
7f910 61 72 64 73 20 61 74 20 65 69 74 68 65 72 20 65  ards at either e
7f920 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61  nd of the alloca
7f930 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69 66 20 74  tion and.** if t
7f940 68 65 79 20 61 72 65 20 69 6e 63 6f 72 72 65 63  hey are incorrec
7f950 74 20 69 74 20 61 73 73 65 72 74 73 2e 0a 2a 2f  t it asserts..*/
7f960 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 4d  .static struct M
7f970 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73 71 6c 69  emBlockHdr *sqli
7f980 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64  te3MemsysGetHead
7f990 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 61  er(void *pAlloca
7f9a0 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75 63 74 20  tion){.  struct 
7f9b0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a  MemBlockHdr *p;.
7f9c0 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 75    int *pInt;.  u
7f9d0 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74 20 6e 52  8 *pU8;.  int nR
7f9e0 65 73 65 72 76 65 3b 0a 0a 20 20 70 20 3d 20 28  eserve;..  p = (
7f9f0 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
7fa00 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b  dr*)pAllocation;
7fa10 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73 65 72 74  .  p--;.  assert
7fa20 28 20 70 2d 3e 69 46 6f 72 65 47 75 61 72 64 3d  ( p->iForeGuard=
7fa30 3d 28 69 6e 74 29 46 4f 52 45 47 55 41 52 44 20  =(int)FOREGUARD 
7fa40 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20  );.  nReserve = 
7fa50 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69 7a 65 29  ROUND8(p->iSize)
7fa60 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a  ;.  pInt = (int*
7fa70 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20  )pAllocation;.  
7fa80 70 55 38 20 3d 20 28 75 38 2a 29 70 41 6c 6c 6f  pU8 = (u8*)pAllo
7fa90 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73 65 72 74  cation;.  assert
7faa0 28 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f  ( pInt[nReserve/
7fab0 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d 3d 28 69  sizeof(int)]==(i
7fac0 6e 74 29 52 45 41 52 47 55 41 52 44 20 29 3b 0a  nt)REARGUARD );.
7fad0 20 20 2f 2a 20 54 68 69 73 20 63 68 65 63 6b 73    /* This checks
7fae0 20 61 6e 79 20 6f 66 20 74 68 65 20 22 65 78 74   any of the "ext
7faf0 72 61 22 20 62 79 74 65 73 20 61 6c 6c 6f 63 61  ra" bytes alloca
7fb00 74 65 64 20 64 75 65 0a 20 20 2a 2a 20 74 6f 20  ted due.  ** to 
7fb10 72 6f 75 6e 64 69 6e 67 20 75 70 20 74 6f 20 61  rounding up to a
7fb20 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61 72  n 8 byte boundar
7fb30 79 20 74 6f 20 65 6e 73 75 72 65 20 0a 20 20 2a  y to ensure .  *
7fb40 2a 20 74 68 65 79 20 68 61 76 65 6e 27 74 20 62  * they haven't b
7fb50 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 2e  een overwritten.
7fb60 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e  .  */.  while( n
7fb70 52 65 73 65 72 76 65 2d 2d 20 3e 20 70 2d 3e 69  Reserve-- > p->i
7fb80 53 69 7a 65 20 29 20 61 73 73 65 72 74 28 20 70  Size ) assert( p
7fb90 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d 3d 30 78  U8[nReserve]==0x
7fba0 36 35 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  65 );.  return p
7fbb0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
7fbc0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
7fbd0 62 79 74 65 73 20 63 75 72 72 65 6e 74 6c 79 20  bytes currently 
7fbe0 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61 64 64  allocated at add
7fbf0 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 61 74 69  ress p..*/.stati
7fc00 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  c int sqlite3Mem
7fc10 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Size(void *p){. 
7fc20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
7fc30 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 69 66 28  Hdr *pHdr;.  if(
7fc40 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72   !p ){.    retur
7fc50 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 64 72 20  n 0;.  }.  pHdr 
7fc60 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47  = sqlite3MemsysG
7fc70 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20 20 72  etHeader(p);.  r
7fc80 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 53 69 7a  eturn pHdr->iSiz
7fc90 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  e;.}../*.** Init
7fca0 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
7fcb0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
7fcc0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69  system..*/.stati
7fcd0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  c int sqlite3Mem
7fce0 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73  Init(void *NotUs
7fcf0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ed){.  UNUSED_PA
7fd00 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
7fd10 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 69 7a  ;.  assert( (siz
7fd20 65 6f 66 28 73 74 72 75 63 74 20 4d 65 6d 42 6c  eof(struct MemBl
7fd30 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d 20 30 20  ockHdr)&7) == 0 
7fd40 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  );.  if( !sqlite
7fd50 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d  3GlobalConfig.bM
7fd60 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 2f 2a  emstat ){.    /*
7fd70 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 61 74 75   If memory statu
7fd80 73 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68  s is enabled, th
7fd90 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 2e 63 20  en the malloc.c 
7fda0 77 72 61 70 70 65 72 20 77 69 6c 6c 20 61 6c 72  wrapper will alr
7fdb0 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 6f 6c 64  eady.    ** hold
7fdc0 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20   the STATIC_MEM 
7fdd0 6d 75 74 65 78 20 77 68 65 6e 20 74 68 65 20 72  mutex when the r
7fde0 6f 75 74 69 6e 65 73 20 68 65 72 65 20 61 72 65  outines here are
7fdf0 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a 20 20 20   invoked. */.   
7fe00 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 73 71 6c   mem.mutex = sql
7fe10 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
7fe20 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
7fe30 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 72  IC_MEM);.  }.  r
7fe40 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
7fe50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74  .}../*.** Deinit
7fe60 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
7fe70 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
7fe80 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69  system..*/.stati
7fe90 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  c void sqlite3Me
7fea0 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a  mShutdown(void *
7feb0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53  NotUsed){.  UNUS
7fec0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
7fed0 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e 6d 75 74  Used);.  mem.mut
7fee0 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ex = 0;.}../*.**
7fef0 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75   Round up a requ
7ff00 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20  est size to the 
7ff10 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63  next valid alloc
7ff20 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73  ation size..*/.s
7ff30 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
7ff40 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20  3MemRoundup(int 
7ff50 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f 55  n){.  return ROU
7ff60 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ND8(n);.}../*.**
7ff70 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20   Allocate nByte 
7ff80 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e  bytes of memory.
7ff90 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
7ffa0 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f  *sqlite3MemMallo
7ffb0 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  c(int nByte){.  
7ffc0 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
7ffd0 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64  dr *pHdr;.  void
7ffe0 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72 20 2a   **pBt;.  char *
7fff0 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a  z;.  int *pInt;.
80000 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20    void *p = 0;. 
80010 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 3b 0a   int totalSize;.
80020 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 3b 0a    int nReserve;.
80030 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
80040 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29  enter(mem.mutex)
80050 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e  ;.  assert( mem.
80060 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20  disallow==0 );. 
80070 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 4e   nReserve = ROUN
80080 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 74 6f 74  D8(nByte);.  tot
80090 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65 72 76  alSize = nReserv
800a0 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72  e + sizeof(*pHdr
800b0 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20  ) + sizeof(int) 
800c0 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +.              
800d0 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2a   mem.nBacktrace*
800e0 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20  sizeof(void*) + 
800f0 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 70 20  mem.nTitle;.  p 
80100 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 6c 53 69  = malloc(totalSi
80110 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  ze);.  if( p ){.
80120 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20 20 70      z = p;.    p
80130 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26 7a 5b  Bt = (void**)&z[
80140 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20 20 20  mem.nTitle];.   
80150 20 70 48 64 72 20 3d 20 28 73 74 72 75 63 74 20   pHdr = (struct 
80160 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 26 70 42  MemBlockHdr*)&pB
80170 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65  t[mem.nBacktrace
80180 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e 65  ];.    pHdr->pNe
80190 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 48 64 72  xt = 0;.    pHdr
801a0 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d 2e 70 4c  ->pPrev = mem.pL
801b0 61 73 74 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d  ast;.    if( mem
801c0 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20  .pLast ){.      
801d0 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e 65 78 74  mem.pLast->pNext
801e0 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 65 6c   = pHdr;.    }el
801f0 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 46  se{.      mem.pF
80200 69 72 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20  irst = pHdr;.   
80210 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74   }.    mem.pLast
80220 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 70 48 64   = pHdr;.    pHd
80230 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 20 3d 20  r->iForeGuard = 
80240 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 20 20 70  FOREGUARD;.    p
80250 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53  Hdr->nBacktraceS
80260 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 61 63 6b  lots = mem.nBack
80270 74 72 61 63 65 3b 0a 20 20 20 20 70 48 64 72 2d  trace;.    pHdr-
80280 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d 2e 6e 54  >nTitle = mem.nT
80290 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 20 6d 65  itle;.    if( me
802a0 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a  m.nBacktrace ){.
802b0 20 20 20 20 20 20 76 6f 69 64 20 2a 61 41 64 64        void *aAdd
802c0 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 70 48 64  r[40];.      pHd
802d0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20  r->nBacktrace = 
802e0 62 61 63 6b 74 72 61 63 65 28 61 41 64 64 72 2c  backtrace(aAddr,
802f0 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2b   mem.nBacktrace+
80300 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d 65 6d 63  1)-1;.      memc
80310 70 79 28 70 42 74 2c 20 26 61 41 64 64 72 5b 31  py(pBt, &aAddr[1
80320 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72  ], pHdr->nBacktr
80330 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a  ace*sizeof(void*
80340 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ));.      assert
80350 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 20 20 20  (pBt[0]);.      
80360 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61  if( mem.xBacktra
80370 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  ce ){.        me
80380 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e 42 79  m.xBacktrace(nBy
80390 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74  te, pHdr->nBackt
803a0 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72 5b 31  race-1, &aAddr[1
803b0 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
803c0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 48 64  }else{.      pHd
803d0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20  r->nBacktrace = 
803e0 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  0;.    }.    if(
803f0 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b 0a 20   mem.nTitle ){. 
80400 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 6d       memcpy(z, m
80410 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e  em.zTitle, mem.n
80420 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Title);.    }.  
80430 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 3d 20    pHdr->iSize = 
80440 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a 75 73  nByte;.    adjus
80450 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20 2b 31  tStats(nByte, +1
80460 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20 28 69  );.    pInt = (i
80470 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a 20 20  nt*)&pHdr[1];.  
80480 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f    pInt[nReserve/
80490 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d 20 52  sizeof(int)] = R
804a0 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20 6d 65  EARGUARD;.    me
804b0 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 36 35 2c  mset(pInt, 0x65,
804c0 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20 20   nReserve);.    
804d0 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 6e 74 3b  p = (void*)pInt;
804e0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
804f0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d  utex_leave(mem.m
80500 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
80510 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  p; .}../*.** Fre
80520 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61  e memory..*/.sta
80530 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
80540 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50  MemFree(void *pP
80550 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 63 74 20  rior){.  struct 
80560 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64  MemBlockHdr *pHd
80570 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b  r;.  void **pBt;
80580 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 61 73  .  char *z;.  as
80590 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f  sert( sqlite3Glo
805a0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
805b0 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 65 78 21  at || mem.mutex!
805c0 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 3d 20 73  =0 );.  pHdr = s
805d0 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48  qlite3MemsysGetH
805e0 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a 20  eader(pPrior);. 
805f0 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70   pBt = (void**)p
80600 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d 20 70 48  Hdr;.  pBt -= pH
80610 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c  dr->nBacktraceSl
80620 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ots;.  sqlite3_m
80630 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d  utex_enter(mem.m
80640 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 48 64  utex);.  if( pHd
80650 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20  r->pPrev ){.    
80660 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 50  assert( pHdr->pP
80670 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 48 64 72  rev->pNext==pHdr
80680 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50   );.    pHdr->pP
80690 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64  rev->pNext = pHd
806a0 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73  r->pNext;.  }els
806b0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  e{.    assert( m
806c0 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 64 72 20  em.pFirst==pHdr 
806d0 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 69 72 73  );.    mem.pFirs
806e0 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74 3b  t = pHdr->pNext;
806f0 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 64 72 2d  .  }.  if( pHdr-
80700 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73  >pNext ){.    as
80710 73 65 72 74 28 20 70 48 64 72 2d 3e 70 4e 65 78  sert( pHdr->pNex
80720 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 72 20 29  t->pPrev==pHdr )
80730 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e 65 78  ;.    pHdr->pNex
80740 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 64 72 2d  t->pPrev = pHdr-
80750 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b  >pPrev;.  }else{
80760 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d  .    assert( mem
80770 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 29 3b 0a  .pLast==pHdr );.
80780 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20      mem.pLast = 
80790 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d  pHdr->pPrev;.  }
807a0 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 42  .  z = (char*)pB
807b0 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e  t;.  z -= pHdr->
807c0 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a 75 73 74  nTitle;.  adjust
807d0 53 74 61 74 73 28 70 48 64 72 2d 3e 69 53 69 7a  Stats(pHdr->iSiz
807e0 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d 73 65 74  e, -1);.  memset
807f0 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a 65 6f 66  (z, 0x2b, sizeof
80800 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e 6e 42  (void*)*pHdr->nB
80810 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 2b 20  acktraceSlots + 
80820 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 0a  sizeof(*pHdr) +.
80830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80840 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 2b 20    pHdr->iSize + 
80850 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20 70 48  sizeof(int) + pH
80860 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a 20 20 66  dr->nTitle);.  f
80870 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c 69 74 65  ree(z);.  sqlite
80880 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
80890 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f  m.mutex);  .}../
808a0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
808b0 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74  size of an exist
808c0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
808d0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  ation..**.** For
808e0 20 74 68 69 73 20 64 65 62 75 67 67 69 6e 67 20   this debugging 
808f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
80900 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d 61 6b 65  we *always* make
80910 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a   a copy of the.*
80920 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74  * allocation int
80930 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 20 69 6e  o a new place in
80940 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 74 68 69   memory.  In thi
80950 73 20 77 61 79 2c 20 69 66 20 74 68 65 20 0a 2a  s way, if the .*
80960 2a 20 68 69 67 68 65 72 20 6c 65 76 65 6c 20 63  * higher level c
80970 6f 64 65 20 69 73 20 75 73 69 6e 67 20 70 6f 69  ode is using poi
80980 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 6c 64 20  nter to the old 
80990 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 69  allocation, it i
809a0 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20  s .** much more 
809b0 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 61 6b 20  likely to break 
809c0 61 6e 64 20 77 65 20 61 72 65 20 6d 75 63 68 20  and we are much 
809d0 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 6f 20 66  more liking to f
809e0 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  ind.** the error
809f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
80a00 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c   *sqlite3MemReal
80a10 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  loc(void *pPrior
80a20 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  , int nByte){.  
80a30 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
80a40 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a 20 20 76  dr *pOldHdr;.  v
80a50 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 61 73 73  oid *pNew;.  ass
80a60 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f  ert( mem.disallo
80a70 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c 64 48 64  w==0 );.  pOldHd
80a80 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79  r = sqlite3Memsy
80a90 73 47 65 74 48 65 61 64 65 72 28 70 50 72 69 6f  sGetHeader(pPrio
80aa0 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  r);.  pNew = sql
80ab0 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42  ite3MemMalloc(nB
80ac0 79 74 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  yte);.  if( pNew
80ad0 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70   ){.    memcpy(p
80ae0 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79  New, pPrior, nBy
80af0 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a  te<pOldHdr->iSiz
80b00 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 4f 6c 64  e ? nByte : pOld
80b10 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20  Hdr->iSize);.   
80b20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f 6c 64 48   if( nByte>pOldH
80b30 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20  dr->iSize ){.   
80b40 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61     memset(&((cha
80b50 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48 64 72  r*)pNew)[pOldHdr
80b60 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 62 2c 20  ->iSize], 0x2b, 
80b70 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 64 72 2d  nByte - pOldHdr-
80b80 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20  >iSize);.    }. 
80b90 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65     sqlite3MemFre
80ba0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20  e(pPrior);.  }. 
80bb0 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
80bc0 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20  ./*.** Populate 
80bd0 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  the low-level me
80be0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
80bf0 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
80c00 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47  s in.** sqlite3G
80c10 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69  lobalConfig.m wi
80c20 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  th pointers to t
80c30 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
80c40 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c  his file..*/.SQL
80c50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
80c60 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65   sqlite3MemSetDe
80c70 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73  fault(void){.  s
80c80 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
80c90 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
80ca0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d  defaultMethods =
80cb0 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d   {.     sqlite3M
80cc0 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73  emMalloc,.     s
80cd0 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20  qlite3MemFree,. 
80ce0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65      sqlite3MemRe
80cf0 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69  alloc,.     sqli
80d00 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20  te3MemSize,.    
80d10 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64   sqlite3MemRound
80d20 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  up,.     sqlite3
80d30 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71  MemInit,.     sq
80d40 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e  lite3MemShutdown
80d50 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20  ,.     0.  };.  
80d60 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53  sqlite3_config(S
80d70 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c  QLITE_CONFIG_MAL
80d80 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74  LOC, &defaultMet
80d90 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  hods);.}../*.** 
80da0 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  Set the number o
80db0 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65 76 65  f backtrace leve
80dc0 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61 63 68  ls kept for each
80dd0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20   allocation..** 
80de0 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 20  A value of zero 
80df0 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b 74 72  turns off backtr
80e00 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75 6d 62  acing.  The numb
80e10 65 72 20 69 73 20 61 6c 77 61 79 73 20 72 6f 75  er is always rou
80e20 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20 61 20  nded.** up to a 
80e30 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e 0a 2a  multiple of 2..*
80e40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
80e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
80e60 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 28 69  debugBacktrace(i
80e70 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69 66 28  nt depth){.  if(
80e80 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65 70 74   depth<0 ){ dept
80e90 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28 20 64  h = 0; }.  if( d
80ea0 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70 74 68  epth>20 ){ depth
80eb0 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70 74 68   = 20; }.  depth
80ec0 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30 78 66   = (depth+1)&0xf
80ed0 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72  e;.  mem.nBacktr
80ee0 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a  ace = depth;.}..
80ef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
80f00 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  oid sqlite3Memde
80f10 62 75 67 42 61 63 6b 74 72 61 63 65 43 61 6c 6c  bugBacktraceCall
80f20 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42 61 63  back(void (*xBac
80f30 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e 74  ktrace)(int, int
80f40 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20 20 6d  , void **)){.  m
80f50 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 3d 20  em.xBacktrace = 
80f60 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f  xBacktrace;.}../
80f70 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 74  *.** Set the tit
80f80 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20 73 75  le string for su
80f90 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74  bsequent allocat
80fa0 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
80fb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
80fc0 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 74  ite3MemdebugSett
80fd0 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20  itle(const char 
80fe0 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 6e 73 69  *zTitle){.  unsi
80ff0 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 73 71 6c  gned int n = sql
81000 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 69  ite3Strlen30(zTi
81010 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c 69  tle) + 1;.  sqli
81020 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
81030 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66  mem.mutex);.  if
81040 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e  ( n>=sizeof(mem.
81050 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20 73 69  zTitle) ) n = si
81060 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29  zeof(mem.zTitle)
81070 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65 6d  -1;.  memcpy(mem
81080 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65 2c  .zTitle, zTitle,
81090 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c   n);.  mem.zTitl
810a0 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e  e[n] = 0;.  mem.
810b0 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e 44 38 28  nTitle = ROUND8(
810c0 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  n);.  sqlite3_mu
810d0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75  tex_leave(mem.mu
810e0 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f  tex);.}..SQLITE_
810f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
81100 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 6e 63  ite3MemdebugSync
81110 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d  (){.  struct Mem
81120 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a  BlockHdr *pHdr;.
81130 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70    for(pHdr=mem.p
81140 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48 64  First; pHdr; pHd
81150 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a  r=pHdr->pNext){.
81160 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 20 3d      void **pBt =
81170 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20   (void**)pHdr;. 
81180 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e     pBt -= pHdr->
81190 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b  nBacktraceSlots;
811a0 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72  .    mem.xBacktr
811b0 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c  ace(pHdr->iSize,
811c0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
811d0 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b 0a 20  e-1, &pBt[1]);. 
811e0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e   }.}../*.** Open
811f0 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61   the file indica
81200 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20  ted and write a 
81210 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65  log of all unfre
81220 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c  ed memory .** al
81230 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74  locations into t
81240 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49  hat log..*/.SQLI
81250 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
81260 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 44  sqlite3MemdebugD
81270 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ump(const char *
81280 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49  zFilename){.  FI
81290 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 75 63  LE *out;.  struc
812a0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
812b0 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42  Hdr;.  void **pB
812c0 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6f 75  t;.  int i;.  ou
812d0 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e  t = fopen(zFilen
812e0 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 66 28  ame, "w");.  if(
812f0 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 66   out==0 ){.    f
81300 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
81310 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74  ** Unable to out
81320 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67  put memory debug
81330 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20   output log: %s 
81340 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  **\n",.         
81350 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65             zFile
81360 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72  name);.    retur
81370 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 48 64  n;.  }.  for(pHd
81380 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70 48  r=mem.pFirst; pH
81390 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e 70  dr; pHdr=pHdr->p
813a0 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 72 20  Next){.    char 
813b0 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 64 72  *z = (char*)pHdr
813c0 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 72 2d  ;.    z -= pHdr-
813d0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
813e0 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b  *sizeof(void*) +
813f0 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20   pHdr->nTitle;. 
81400 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
81410 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 65 73  "**** %lld bytes
81420 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 20 2a   at %p from %s *
81430 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20  ***\n", .       
81440 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65       pHdr->iSize
81450 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 64 72  , &pHdr[1], pHdr
81460 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a 20 22  ->nTitle ? z : "
81470 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 20 70  ???");.    if( p
81480 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20  Hdr->nBacktrace 
81490 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 68 28  ){.      fflush(
814a0 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 74 20  out);.      pBt 
814b0 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a  = (void**)pHdr;.
814c0 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64        pBt -= pHd
814d0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f  r->nBacktraceSlo
814e0 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b 74 72  ts;.      backtr
814f0 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 70  ace_symbols_fd(p
81500 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74  Bt, pHdr->nBackt
81510 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 74  race, fileno(out
81520 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74  ));.      fprint
81530 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20  f(out, "\n");.  
81540 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74    }.  }.  fprint
81550 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 3a 5c  f(out, "COUNTS:\
81560 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  n");.  for(i=0; 
81570 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b 2b 29  i<NCSIZE-1; i++)
81580 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 41  {.    if( mem.nA
81590 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 20 20  lloc[i] ){.     
815a0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
815b0 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 30 64    %5d: %10d %10d
815c0 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 20 20   %10d\n", .     
815d0 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 6d 2e         i*8, mem.
815e0 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e  nAlloc[i], mem.n
815f0 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e  Current[i], mem.
81600 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b 0a 20  mxCurrent[i]);. 
81610 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d     }.  }.  if( m
81620 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45  em.nAlloc[NCSIZE
81630 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  -1] ){.    fprin
81640 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64 3a  tf(out, "   %5d:
81650 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64 5c   %10d %10d %10d\
81660 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
81670 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d   NCSIZE*8-8, mem
81680 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31  .nAlloc[NCSIZE-1
81690 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
816a0 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 53  mem.nCurrent[NCS
816b0 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75  IZE-1], mem.mxCu
816c0 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 29  rrent[NCSIZE-1])
816d0 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 28 6f  ;.  }.  fclose(o
816e0 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ut);.}../*.** Re
816f0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
81700 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 65 33  of times sqlite3
81710 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 73 20  MemMalloc() has 
81720 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  been called..*/.
81730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
81740 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  nt sqlite3Memdeb
81750 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b  ugMallocCount(){
81760 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
81770 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 66 6f  nTotal = 0;.  fo
81780 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 3b  r(i=0; i<NCSIZE;
81790 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 61   i++){.    nTota
817a0 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b  l += mem.nAlloc[
817b0 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  i];.  }.  return
817c0 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e   nTotal;.}...#en
817d0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 45  dif /* SQLITE_ME
817e0 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  MDEBUG */../****
817f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
81800 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f mem2.c *******
81810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
81840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
81850 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a   file mem3.c ***
81860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
81890 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34   2007 October 14
818a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
818b0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
818c0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
818d0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
818e0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
818f0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
81900 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
81910 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
81920 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
81930 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
81940 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
81950 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
81960 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
81970 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
81980 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
81990 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
819a0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
819b0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
819c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
819d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
819e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
819f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
81a00 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
81a10 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
81a20 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
81a30 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a  ment a memory.**
81a40 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
81a50 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79  ystem for use by
81a60 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20   SQLite. .**.** 
81a70 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
81a80 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
81a90 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20  ation subsystem 
81aa0 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65  omits all.** use
81ab0 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68   of malloc(). Th
81ac0 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 73 75  e SQLite user su
81ad0 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f  pplies a block o
81ae0 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f  f memory.** befo
81af0 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  re calling sqlit
81b00 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
81b10 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63  from which alloc
81b20 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61  ations.** are ma
81b30 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20  de and returned 
81b40 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29  by the xMalloc()
81b50 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20   and xRealloc() 
81b60 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
81b70 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65  ons. Once sqlite
81b80 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68  3_initialize() h
81b90 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a  as been called,.
81ba0 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  ** the amount of
81bb0 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c   memory availabl
81bc0 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66  e to SQLite is f
81bd0 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a  ixed and cannot.
81be0 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a  ** be changed..*
81bf0 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f  *.** This versio
81c00 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  n of the memory 
81c10 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
81c20 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64  stem is included
81c30 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64  .** in the build
81c40 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f   only if SQLITE_
81c50 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69  ENABLE_MEMSYS3 i
81c60 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  s defined..**.**
81c70 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c 76 20 31   $Id: mem3.c,v 1
81c80 2e 32 35 20 32 30 30 38 2f 31 31 2f 31 39 20 31  .25 2008/11/19 1
81c90 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31  6:52:44 danielk1
81ca0 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
81cb0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
81cc0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
81cd0 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e 6c 79  llocator is only
81ce0 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20   built into the 
81cf0 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c 49 54  library.** SQLIT
81d00 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33  E_ENABLE_MEMSYS3
81d10 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44 65 66   is defined. Def
81d20 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d 62 6f  ining this symbo
81d30 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65  l does not.** me
81d40 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 62 72  an that the libr
81d50 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61 20 6d  ary will use a m
81d60 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 64 65  emory-pool by de
81d70 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68 61 74  fault, just that
81d80 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69 6c 61  .** it is availa
81d90 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f 6f 6c  ble. The mempool
81da0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 61 63   allocator is ac
81db0 74 69 76 61 74 65 64 20 62 79 20 63 61 6c 6c 69  tivated by calli
81dc0 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  ng.** sqlite3_co
81dd0 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 64 65  nfig()..*/.#ifde
81de0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
81df0 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d  MEMSYS3../*.** M
81e00 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69 6e 20  aximum size (in 
81e10 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 20 61  Mem3Blocks) of a
81e20 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a   "small" chunk..
81e30 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 53 4d  */.#define MX_SM
81e40 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e  ALL 10.../*.** N
81e50 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73  umber of freelis
81e60 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a  t hash slots.*/.
81e70 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48 20 20  #define N_HASH  
81e80 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f  61../*.** A memo
81e90 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 61  ry allocation (a
81ea0 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22 63 68  lso called a "ch
81eb0 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73 20 6f  unk") consists o
81ec0 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72  f two or .** mor
81ed0 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65 20 65  e blocks where e
81ee0 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38 20 62  ach block is 8 b
81ef0 79 74 65 73 2e 20 20 54 68 65 20 66 69 72 73 74  ytes.  The first
81f00 20 38 20 62 79 74 65 73 20 61 72 65 20 0a 2a 2a   8 bytes are .**
81f10 20 61 20 68 65 61 64 65 72 20 74 68 61 74 20 69   a header that i
81f20 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64 20 74  s not returned t
81f30 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a  o the user..**.*
81f40 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74 77 6f  * A chunk is two
81f50 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20   or more blocks 
81f60 74 68 61 74 20 69 73 20 65 69 74 68 65 72 20 63  that is either c
81f70 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a 2a 2a  hecked out or.**
81f80 20 66 72 65 65 2e 20 20 54 68 65 20 66 69 72 73   free.  The firs
81f90 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f 72 6d  t block has form
81fa0 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68 64 72  at u.hdr.  u.hdr
81fb0 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74 69 6d  .size4x is 4 tim
81fc0 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f  es the.** size o
81fd0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
81fe0 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68   in blocks if th
81ff0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  e allocation is 
82000 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75 2e 68  free..** The u.h
82010 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69 74 20  dr.size4x&1 bit 
82020 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 63  is true if the c
82030 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20  hunk is checked 
82040 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65  out and.** false
82050 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73   if the chunk is
82060 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74   on the freelist
82070 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73 69 7a  .  The u.hdr.siz
82080 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69 73 20  e4x&2 bit.** is 
82090 74 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76  true if the prev
820a0 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 63 68  ious chunk is ch
820b0 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 66 61  ecked out and fa
820c0 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20 70 72  lse if the.** pr
820d0 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20  evious chunk is 
820e0 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68 64 72  free.  The u.hdr
820f0 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c 64 20  .prevSize field 
82100 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a  is the size of.*
82110 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  * the previous c
82120 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 20 69  hunk in blocks i
82130 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  f the previous c
82140 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 0a 2a  hunk is on the.*
82150 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66 20 74  * freelist. If t
82160 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e  he previous chun
82170 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74  k is checked out
82180 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e  , then.** u.hdr.
82190 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62 65 20  prevSize can be 
821a0 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
821b0 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e 6b 20   for that chunk 
821c0 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f  and should.** no
821d0 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69  t be read or wri
821e0 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f  tten..**.** We o
821f0 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20 61 20  ften identify a 
82200 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69 6e 64  chunk by its ind
82210 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ex in mem3.aPool
82220 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69  [].  When.** thi
82230 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65 20 63  s is done, the c
82240 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66 65 72  hunk index refer
82250 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20  s to the second 
82260 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 65 20  block of.** the 
82270 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69 73 20  chunk.  In this 
82280 77 61 79 2c 20 74 68 65 20 66 69 72 73 74 20 63  way, the first c
82290 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e 64 65  hunk has an inde
822a0 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 68 75  x of 1..** A chu
822b0 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20 6d 65  nk index of 0 me
822c0 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63 68 75  ans "no such chu
822d0 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65 20 65  nk" and is the e
822e0 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20  quivalent.** of 
822f0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
82300 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  **.** The second
82310 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65 20 63   block of free c
82320 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68 65 20  hunks is of the 
82330 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20 54 68  form u.list.  Th
82340 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64 73 20  e.** two fields 
82350 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d 6c 69  form a double-li
82360 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63 68 75  nked list of chu
82370 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64 20 73  nks of related s
82380 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72  izes..** Pointer
82390 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66  s to the head of
823a0 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 73 74   the list are st
823b0 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61 69 53  ored in mem3.aiS
823c0 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73  mall[] .** for s
823d0 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20 61 6e  maller chunks an
823e0 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 20  d mem3.aiHash[] 
823f0 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75 6e 6b  for larger chunk
82400 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  s..**.** The sec
82410 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61 20 63  ond block of a c
82420 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64 61 74  hunk is user dat
82430 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69  a if the chunk i
82440 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20 6f 75  s checked .** ou
82450 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b 20 69  t.  If a chunk i
82460 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74  s checked out, t
82470 68 65 20 75 73 65 72 20 64 61 74 61 20 6d 61 79  he user data may
82480 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20   extend into.** 
82490 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76 53 69  the u.hdr.prevSi
824a0 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ze value of the 
824b0 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e 6b 2e  following chunk.
824c0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
824d0 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d  ct Mem3Block Mem
824e0 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 20 4d  3Block;.struct M
824f0 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69  em3Block {.  uni
82500 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20  on {.    struct 
82510 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72 65 76  {.      u32 prev
82520 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a 65 20  Size;   /* Size 
82530 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e  of previous chun
82540 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65  k in Mem3Block e
82550 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  lements */.     
82560 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20 20 20   u32 size4x;    
82570 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a 65 20   /* 4x the size 
82580 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75 6e 6b  of current chunk
82590 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c   in Mem3Block el
825a0 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 7d 20  ements */.    } 
825b0 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20  hdr;.    struct 
825c0 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65 78 74  {.      u32 next
825d0 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  ;       /* Index
825e0 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d   in mem3.aPool[]
825f0 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 63 68   of next free ch
82600 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75 33 32  unk */.      u32
82610 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20   prev;       /* 
82620 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50  Index in mem3.aP
82630 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 6f 75  ool[] of previou
82640 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a  s free chunk */.
82650 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20 7d 20      } list;.  } 
82660 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  u;.};../*.** All
82670 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 76   of the static v
82680 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79  ariables used by
82690 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65   this module are
826a0 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e   collected.** in
826b0 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75  to a single stru
826c0 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d  cture named "mem
826d0 33 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20  3".  This is to 
826e0 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74  keep the.** stat
826f0 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67  ic variables org
82700 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65  anized and to re
82710 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70  duce namespace p
82720 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e  ollution.** when
82730 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   this module is 
82740 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74  combined with ot
82750 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67  her in the amalg
82760 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  amation..*/.stat
82770 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  ic SQLITE_WSD st
82780 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 20  ruct Mem3Global 
82790 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f  {.  /*.  ** Memo
827a0 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ry available for
827b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f   allocation. nPo
827c0 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f  ol is the size o
827d0 66 20 74 68 65 20 61 72 72 61 79 0a 20 20 2a 2a  f the array.  **
827e0 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29   (in Mem3Blocks)
827f0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61   pointed to by a
82800 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 20 2a  Pool less 2..  *
82810 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20  /.  u32 nPool;. 
82820 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f   Mem3Block *aPoo
82830 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72  l;..  /*.  ** Tr
82840 75 65 20 69 66 20 77 65 20 61 72 65 20 65 76 61  ue if we are eva
82850 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74 2d 6f  luating an out-o
82860 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 61 63  f-memory callbac
82870 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 6c  k..  */.  int al
82880 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a  armBusy;.  .  /*
82890 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63  .  ** Mutex to c
828a0 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f  ontrol access to
828b0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
828c0 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
828d0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
828e0 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20  _mutex *mutex;. 
828f0 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20   .  /*.  ** The 
82900 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f  minimum amount o
82910 66 20 66 72 65 65 20 73 70 61 63 65 20 74 68 61  f free space tha
82920 74 20 77 65 20 68 61 76 65 20 73 65 65 6e 2e 0a  t we have seen..
82930 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d 61 73    */.  u32 mnMas
82940 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ter;..  /*.  ** 
82950 69 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 69  iMaster is the i
82960 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 73 74  ndex of the mast
82970 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 74 20  er chunk.  Most 
82980 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a  new allocations.
82990 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 20 6f    ** occur off o
829a0 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20 20 73  f this chunk.  s
829b0 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 73  zMaster is the s
829c0 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63  ize (in Mem3Bloc
829d0 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  ks).  ** of the 
829e0 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72 2e 20  current master. 
829f0 20 69 4d 61 73 74 65 72 20 69 73 20 30 20 69 66   iMaster is 0 if
82a00 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 61   there is not ma
82a10 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a  ster chunk..  **
82a20 20 54 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e   The master chun
82a30 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 74 68  k is not in eith
82a40 65 72 20 74 68 65 20 61 69 48 61 73 68 5b 5d 20  er the aiHash[] 
82a50 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20  or aiSmall[]..  
82a60 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74 65 72  */.  u32 iMaster
82a70 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74 65 72  ;.  u32 szMaster
82a80 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 72 72  ;..  /*.  ** Arr
82a90 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66 20 66  ay of lists of f
82aa0 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63 6f 72  ree blocks accor
82ab0 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c 6f 63  ding to the bloc
82ac0 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66 6f 72  k size .  ** for
82ad0 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c   smaller chunks,
82ae0 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20 74 68   or a hash on th
82af0 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72  e block size for
82b00 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63 68 75   larger.  ** chu
82b10 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20  nks..  */.  u32 
82b20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c  aiSmall[MX_SMALL
82b30 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69  -1];   /* For si
82b40 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20 4d 58  zes 2 through MX
82b50 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76  _SMALL, inclusiv
82b60 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48 61 73  e */.  u32 aiHas
82b70 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20 20 20  h[N_HASH];      
82b80 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20 4d    /* For sizes M
82b90 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c 61  X_SMALL+1 and la
82ba0 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d  rger */.} mem3 =
82bb0 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b 0a 0a   { 97535575 };..
82bc0 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47 4c 4f  #define mem3 GLO
82bd0 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 33 47  BAL(struct Mem3G
82be0 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a  lobal, mem3)../*
82bf0 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63  .** Unlink the c
82c00 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f  hunk at mem3.aPo
82c10 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20  ol[i] from list 
82c20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a  it is currently.
82c30 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 20 69  ** on.  *pRoot i
82c40 73 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 20  s the list that 
82c50 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20 6f 66  i is a member of
82c60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
82c70 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
82c80 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20 75 33  omList(u32 i, u3
82c90 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32  2 *pRoot){.  u32
82ca0 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f   next = mem3.aPo
82cb0 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[i].u.list.nex
82cc0 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20 3d 20  t;.  u32 prev = 
82cd0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
82ce0 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61 73 73  list.prev;.  ass
82cf0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
82d00 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
82d10 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 72 65  ex) );.  if( pre
82d20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 6f  v==0 ){.    *pRo
82d30 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c  ot = next;.  }el
82d40 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f  se{.    mem3.aPo
82d50 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e  ol[prev].u.list.
82d60 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d  next = next;.  }
82d70 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b 0a 20  .  if( next ){. 
82d80 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65     mem3.aPool[ne
82d90 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20  xt].u.list.prev 
82da0 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20 6d 65  = prev;.  }.  me
82db0 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69  m3.aPool[i].u.li
82dc0 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 20 6d  st.next = 0;.  m
82dd0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c  em3.aPool[i].u.l
82de0 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a 7d 0a  ist.prev = 0;.}.
82df0 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68  ./*.** Unlink th
82e00 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78  e chunk at index
82e10 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 61 74   i from .** what
82e20 65 76 65 72 20 6c 69 73 74 20 69 73 20 63 75 72  ever list is cur
82e30 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65 72 20  rently a member 
82e40 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  of..*/.static vo
82e50 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b  id memsys3Unlink
82e60 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73  (u32 i){.  u32 s
82e70 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73  ize, hash;.  ass
82e80 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
82e90 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
82ea0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
82eb0 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31   (mem3.aPool[i-1
82ec0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26  ].u.hdr.size4x &
82ed0 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65   1)==0 );.  asse
82ee0 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 73 69  rt( i>=1 );.  si
82ef0 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
82f00 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
82f10 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73  x/4;.  assert( s
82f20 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ize==mem3.aPool[
82f30 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e  i+size-1].u.hdr.
82f40 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73  prevSize );.  as
82f50 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b  sert( size>=2 );
82f60 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d  .  if( size <= M
82f70 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d  X_SMALL ){.    m
82f80 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d  emsys3UnlinkFrom
82f90 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69  List(i, &mem3.ai
82fa0 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a  Small[size-2]);.
82fb0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73    }else{.    has
82fc0 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53  h = size % N_HAS
82fd0 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  H;.    memsys3Un
82fe0 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20  linkFromList(i, 
82ff0 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73  &mem3.aiHash[has
83000 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  h]);.  }.}../*.*
83010 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  * Link the chunk
83020 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69   at mem3.aPool[i
83030 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20  ] so that is on 
83040 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65 64 0a  the list rooted.
83050 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f  ** at *pRoot..*/
83060 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
83070 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74  sys3LinkIntoList
83080 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f  (u32 i, u32 *pRo
83090 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  ot){.  assert( s
830a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
830b0 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b  d(mem3.mutex) );
830c0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  .  mem3.aPool[i]
830d0 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 2a  .u.list.next = *
830e0 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e 61 50  pRoot;.  mem3.aP
830f0 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72  ool[i].u.list.pr
83100 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20 2a 70  ev = 0;.  if( *p
83110 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33  Root ){.    mem3
83120 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75  .aPool[*pRoot].u
83130 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69 3b 0a  .list.prev = i;.
83140 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d 20 69    }.  *pRoot = i
83150 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20  ;.}../*.** Link 
83160 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64  the chunk at ind
83170 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68 65 72  ex i into either
83180 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
83190 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e 6b 20  .** small chunk 
831a0 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20 74 68  list, or into th
831b0 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61  e large chunk ha
831c0 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  sh table..*/.sta
831d0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
831e0 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75  Link(u32 i){.  u
831f0 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20  32 size, hash;. 
83200 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
83210 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33  _mutex_held(mem3
83220 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  .mutex) );.  ass
83230 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61  ert( i>=1 );.  a
83240 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f  ssert( (mem3.aPo
83250 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
83260 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a  ze4x & 1)==0 );.
83270 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50    size = mem3.aP
83280 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
83290 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72  ize4x/4;.  asser
832a0 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50  t( size==mem3.aP
832b0 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e  ool[i+size-1].u.
832c0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a  hdr.prevSize );.
832d0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d    assert( size>=
832e0 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20  2 );.  if( size 
832f0 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20  <= MX_SMALL ){. 
83300 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e     memsys3LinkIn
83310 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  toList(i, &mem3.
83320 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29  aiSmall[size-2])
83330 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68  ;.  }else{.    h
83340 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48  ash = size % N_H
83350 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33  ASH;.    memsys3
83360 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20  LinkIntoList(i, 
83370 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73  &mem3.aiHash[has
83380 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  h]);.  }.}../*.*
83390 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 43 5f  * If the STATIC_
833a0 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  MEM mutex is not
833b0 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f   already held, o
833c0 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68  btain it now. Th
833d0 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20  e mutex.** will 
833e0 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c 64 20  already be held 
833f0 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64  (obtained by cod
83400 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69  e in malloc.c) i
83410 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62  f.** sqlite3Glob
83420 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61  alConfig.bMemSta
83430 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74  t is true..*/.st
83440 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
83450 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20  3Enter(void){.  
83460 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
83470 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
83480 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75 74 65  ==0 && mem3.mute
83490 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 33  x==0 ){.    mem3
834a0 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33  .mutex = sqlite3
834b0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
834c0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
834d0 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  EM);.  }.  sqlit
834e0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
834f0 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74  em3.mutex);.}.st
83500 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
83510 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20  3Leave(void){.  
83520 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
83530 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b  ave(mem3.mutex);
83540 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64  .}../*.** Called
83550 20 77 68 65 6e 20 77 65 20 61 72 65 20 75 6e 61   when we are una
83560 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 20 61  ble to satisfy a
83570 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  n allocation of 
83580 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  nBytes..*/.stati
83590 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4f 75  c void memsys3Ou
835a0 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 42  tOfMemory(int nB
835b0 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d 65 6d  yte){.  if( !mem
835c0 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b 0a 20  3.alarmBusy ){. 
835d0 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73     mem3.alarmBus
835e0 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72  y = 1;.    asser
835f0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
83600 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78  _held(mem3.mutex
83610 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) );.    sqlite3
83620 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
83630 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71  3.mutex);.    sq
83640 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
83650 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20 20 20  mory(nByte);.   
83660 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
83670 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29  nter(mem3.mutex)
83680 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d  ;.    mem3.alarm
83690 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  Busy = 0;.  }.}.
836a0 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 69 20  ../*.** Chunk i 
836b0 69 73 20 61 20 66 72 65 65 20 63 68 75 6e 6b 20  is a free chunk 
836c0 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 75 6e  that has been un
836d0 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73 74 20  linked.  Adjust 
836e0 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70 61 72  its .** size par
836f0 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68 65 63  ameters for chec
83700 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75 72 6e  k-out and return
83710 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
83720 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72 74 69  e .** user porti
83730 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e 6b 2e  on of the chunk.
83740 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
83750 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74  *memsys3Checkout
83760 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42 6c 6f  (u32 i, u32 nBlo
83770 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a 20 20  ck){.  u32 x;.  
83780 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
83790 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e  mutex_held(mem3.
837a0 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
837b0 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73  rt( i>=1 );.  as
837c0 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c  sert( mem3.aPool
837d0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
837e0 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a  4x/4==nBlock );.
837f0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61    assert( mem3.a
83800 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d  Pool[i+nBlock-1]
83810 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3d  .u.hdr.prevSize=
83820 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 20 3d  =nBlock );.  x =
83830 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
83840 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20  .u.hdr.size4x;. 
83850 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
83860 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20  .u.hdr.size4x = 
83870 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c 20 28  nBlock*4 | 1 | (
83880 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f  x&2);.  mem3.aPo
83890 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75  ol[i+nBlock-1].u
838a0 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20  .hdr.prevSize = 
838b0 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61  nBlock;.  mem3.a
838c0 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d  Pool[i+nBlock-1]
838d0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d  .u.hdr.size4x |=
838e0 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d 65   2;.  return &me
838f0 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a  m3.aPool[i];.}..
83900 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20 70 69  /*.** Carve a pi
83910 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65 20 65  ece off of the e
83920 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33 2e 69  nd of the mem3.i
83930 4d 61 73 74 65 72 20 66 72 65 65 20 63 68 75 6e  Master free chun
83940 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  k..** Return a p
83950 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
83960 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f  w allocation.  O
83970 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74 65 72  r, if the master
83980 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74   chunk.** is not
83990 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 72   large enough, r
839a0 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74  eturn 0..*/.stat
839b0 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33  ic void *memsys3
839c0 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32 20 6e  FromMaster(u32 n
839d0 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 72 74  Block){.  assert
839e0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
839f0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
83a00 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65   );.  assert( me
83a10 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c  m3.szMaster>=nBl
83a20 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e 42 6c  ock );.  if( nBl
83a30 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74  ock>=mem3.szMast
83a40 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 55  er-1 ){.    /* U
83a50 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61  se the entire ma
83a60 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f 69 64  ster */.    void
83a70 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43 68 65   *p = memsys3Che
83a80 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 73 74  ckout(mem3.iMast
83a90 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  er, mem3.szMaste
83aa0 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 4d 61  r);.    mem3.iMa
83ab0 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65  ster = 0;.    me
83ac0 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b  m3.szMaster = 0;
83ad0 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74  .    mem3.mnMast
83ae0 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  er = 0;.    retu
83af0 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rn p;.  }else{. 
83b00 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20     /* Split the 
83b10 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 20 52  master block.  R
83b20 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c 2e 20  eturn the tail. 
83b30 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77 69 2c  */.    u32 newi,
83b40 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d 20 6d   x;.    newi = m
83b50 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20 6d 65  em3.iMaster + me
83b60 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20 6e 42  m3.szMaster - nB
83b70 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 74  lock;.    assert
83b80 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e 69 4d  ( newi > mem3.iM
83b90 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20 20 6d  aster+1 );.    m
83ba0 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
83bb0 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61  Master+mem3.szMa
83bc0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  ster-1].u.hdr.pr
83bd0 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b  evSize = nBlock;
83be0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
83bf0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d  mem3.iMaster+mem
83c00 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  3.szMaster-1].u.
83c10 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b  hdr.size4x |= 2;
83c20 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
83c30 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  newi-1].u.hdr.si
83c40 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20  ze4x = nBlock*4 
83c50 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a  + 1;.    mem3.sz
83c60 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b  Master -= nBlock
83c70 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ;.    mem3.aPool
83c80 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 70  [newi-1].u.hdr.p
83c90 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73  revSize = mem3.s
83ca0 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78 20 3d  zMaster;.    x =
83cb0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
83cc0 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  .iMaster-1].u.hd
83cd0 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20  r.size4x & 2;.  
83ce0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d    mem3.aPool[mem
83cf0 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  3.iMaster-1].u.h
83d00 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33  dr.size4x = mem3
83d10 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b  .szMaster*4 | x;
83d20 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a  .    if( mem3.sz
83d30 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e  Master < mem3.mn
83d40 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20  Master ){.      
83d50 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20  mem3.mnMaster = 
83d60 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20  mem3.szMaster;. 
83d70 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
83d80 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 50 6f  (void*)&mem3.aPo
83d90 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a  ol[newi];.  }.}.
83da0 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 69 73  ./*.** *pRoot is
83db0 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c   the head of a l
83dc0 69 73 74 20 6f 66 20 66 72 65 65 20 63 68 75 6e  ist of free chun
83dd0 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73  ks of the same s
83de0 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 20 73  ize.** or same s
83df0 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20 6f 74  ize hash.  In ot
83e00 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52 6f 6f  her words, *pRoo
83e10 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e  t is an entry in
83e20 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e   either.** mem3.
83e30 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d  aiSmall[] or mem
83e40 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a  3.aiHash[].  .**
83e50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
83e60 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20 65 6e   examines all en
83e70 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67 69 76  tries on the giv
83e80 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72 69 65  en list and trie
83e90 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 63 65  s.** to coalesce
83ea0 20 65 61 63 68 20 65 6e 74 72 69 65 73 20 77 69   each entries wi
83eb0 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65  th adjacent free
83ec0 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a   chunks.  .**.**
83ed0 20 49 66 20 69 74 20 73 65 65 73 20 61 20 63 68   If it sees a ch
83ee0 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61 72 67  unk that is larg
83ef0 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69 4d 61  er than mem3.iMa
83f00 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61 63 65  ster, it replace
83f10 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e  s .** the curren
83f20 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 77  t mem3.iMaster w
83f30 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61 72 67  ith the new larg
83f40 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20 6f 72  er chunk.  In or
83f50 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69 73 20  der for.** this 
83f60 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72 65 70  mem3.iMaster rep
83f70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f 72 6b  lacement to work
83f80 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75  , the master chu
83f90 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20 6c 69  nk must be.** li
83fa0 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 68 61  nked into the ha
83fb0 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68 61 74  sh tables.  That
83fc0 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f 72 6d   is not the norm
83fd0 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20 61  al state of.** a
83fe0 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75 72 73  ffairs, of cours
83ff0 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  e.  The calling 
84000 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c 69 6e  routine must lin
84010 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20  k the master.** 
84020 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69 6e 76  chunk before inv
84030 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  oking this routi
84040 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20 75 6e  ne, then must un
84050 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73 69 62  link the (possib
84060 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29 20 6d  ly.** changed) m
84070 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e 63 65  aster chunk once
84080 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61   this routine ha
84090 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73  s finished..*/.s
840a0 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
840b0 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70 52 6f  s3Merge(u32 *pRo
840c0 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65 78 74  ot){.  u32 iNext
840d0 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20 69 2c  , prev, size, i,
840e0 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73   x;..  assert( s
840f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
84100 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b  d(mem3.mutex) );
84110 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b  .  for(i=*pRoot;
84120 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29 7b 0a   i>0; i=iNext){.
84130 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33      iNext = mem3
84140 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
84150 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a 65 20  .next;.    size 
84160 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31  = mem3.aPool[i-1
84170 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a  ].u.hdr.size4x;.
84180 20 20 20 20 61 73 73 65 72 74 28 20 28 73 69 7a      assert( (siz
84190 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69  e&1)==0 );.    i
841a0 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30 20 29  f( (size&2)==0 )
841b0 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55  {.      memsys3U
841c0 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c  nlinkFromList(i,
841d0 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 61   pRoot);.      a
841e0 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d 33 2e  ssert( i > mem3.
841f0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
84200 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 20  .prevSize );.   
84210 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20 6d 65     prev = i - me
84220 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
84230 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20  hdr.prevSize;.  
84240 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d 69 4e      if( prev==iN
84250 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ext ){.        i
84260 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  Next = mem3.aPoo
84270 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e  l[prev].u.list.n
84280 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ext;.      }.   
84290 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
842a0 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 73 69  (prev);.      si
842b0 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f 34 20  ze = i + size/4 
842c0 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20 78 20  - prev;.      x 
842d0 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65  = mem3.aPool[pre
842e0 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  v-1].u.hdr.size4
842f0 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d  x & 2;.      mem
84300 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e  3.aPool[prev-1].
84310 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 73  u.hdr.size4x = s
84320 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20  ize*4 | x;.     
84330 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76   mem3.aPool[prev
84340 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70  +size-1].u.hdr.p
84350 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a  revSize = size;.
84360 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e        memsys3Lin
84370 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 69  k(prev);.      i
84380 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d 65 6c   = prev;.    }el
84390 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 2f  se{.      size /
843a0 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 4;.    }.    i
843b0 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d  f( size>mem3.szM
843c0 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
843d0 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 69 3b  em3.iMaster = i;
843e0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61  .      mem3.szMa
843f0 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20 20 20  ster = size;.   
84400 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
84410 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f  Return a block o
84420 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c  f memory of at l
84430 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73  east nBytes in s
84440 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e  ize..** Return N
84450 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a  ULL if unable..*
84460 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
84470 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  on assumes that 
84480 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75  the necessary mu
84490 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61  texes, if any, a
844a0 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65  re.** already he
844b0 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ld by the caller
844c0 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22  . Hence "Unsafe"
844d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
844e0 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55   *memsys3MallocU
844f0 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29  nsafe(int nByte)
84500 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75 33 32  {.  u32 i;.  u32
84510 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32 20 74   nBlock;.  u32 t
84520 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65 72 74  oFree;..  assert
84530 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
84540 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
84550 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   );.  assert( si
84560 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d  zeof(Mem3Block)=
84570 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  =8 );.  if( nByt
84580 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e 42 6c  e<=12 ){.    nBl
84590 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65  ock = 2;.  }else
845a0 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 28  {.    nBlock = (
845b0 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b 0a 20  nByte + 11)/8;. 
845c0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 42 6c   }.  assert( nBl
845d0 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20  ock>=2 );..  /* 
845e0 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f  STEP 1:.  ** Loo
845f0 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 20 6f  k for an entry o
84600 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69  f the correct si
84610 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74 68 65  ze in either the
84620 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e   small.  ** chun
84630 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20 74 68  k table or in th
84640 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61  e large chunk ha
84650 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  sh table.  This 
84660 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 66  is.  ** successf
84670 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 74  ul most of the t
84680 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74 69 6d  ime (about 9 tim
84690 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e 0a 20  es out of 10).. 
846a0 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b   */.  if( nBlock
846b0 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a   <= MX_SMALL ){.
846c0 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61 69 53      i = mem3.aiS
846d0 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a  mall[nBlock-2];.
846e0 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b 0a 20      if( i>0 ){. 
846f0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
84700 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d  nkFromList(i, &m
84710 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f  em3.aiSmall[nBlo
84720 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20 72 65  ck-2]);.      re
84730 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63  turn memsys3Chec
84740 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b  kout(i, nBlock);
84750 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
84760 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d 20 6e      int hash = n
84770 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48 3b 0a  Block % N_HASH;.
84780 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33 2e 61      for(i=mem3.a
84790 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69 3e 30  iHash[hash]; i>0
847a0 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ; i=mem3.aPool[i
847b0 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a  ].u.list.next){.
847c0 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61        if( mem3.a
847d0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
847e0 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b  size4x/4==nBlock
847f0 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73   ){.        mems
84800 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73  ys3UnlinkFromLis
84810 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73  t(i, &mem3.aiHas
84820 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20 20 20  h[hash]);.      
84830 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33    return memsys3
84840 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f  Checkout(i, nBlo
84850 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ck);.      }.   
84860 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 54 45   }.  }..  /* STE
84870 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20 74 6f  P 2:.  ** Try to
84880 20 73 61 74 69 73 66 79 20 74 68 65 20 61 6c 6c   satisfy the all
84890 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 76 69  ocation by carvi
848a0 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 20 6f  ng a piece off o
848b0 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 6f  f the end.  ** o
848c0 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75  f the master chu
848d0 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 20 75  nk.  This step u
848e0 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20  sually works if 
848f0 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a 20 20  step 1 fails..  
84900 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a  */.  if( mem3.sz
84910 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29  Master>=nBlock )
84920 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d  {.    return mem
84930 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e  sys3FromMaster(n
84940 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20  Block);.  }...  
84950 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20 20 2a  /* STEP 3:  .  *
84960 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
84970 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f 72 79  he entire memory
84980 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 63 65   pool.  Coalesce
84990 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 0a 20   adjacent free. 
849a0 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52 65 63   ** chunks.  Rec
849b0 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73 74 65  ompute the maste
849c0 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65 20 6c  r chunk as the l
849d0 61 72 67 65 73 74 20 66 72 65 65 20 63 68 75 6e  argest free chun
849e0 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74 72 79  k..  ** Then try
849f0 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69 73 66   again to satisf
84a00 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  y the allocation
84a10 20 62 79 20 63 61 72 76 69 6e 67 20 61 20 70 69   by carving a pi
84a20 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20  ece off.  ** of 
84a30 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d  the end of the m
84a40 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 54 68  aster chunk.  Th
84a50 69 73 20 73 74 65 70 20 68 61 70 70 65 6e 73 20  is step happens 
84a60 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 6c 79  very.  ** rarely
84a70 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20 2a 2f   (we hope!).  */
84a80 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d 6e 42  .  for(toFree=nB
84a90 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65 65 3c  lock*16; toFree<
84aa0 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b  (mem3.nPool*16);
84ab0 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b 0a 20   toFree *= 2){. 
84ac0 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d     memsys3OutOfM
84ad0 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b 0a 20  emory(toFree);. 
84ae0 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73     if( mem3.iMas
84af0 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ter ){.      mem
84b00 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d  sys3Link(mem3.iM
84b10 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 6d 65  aster);.      me
84b20 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a  m3.iMaster = 0;.
84b30 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73        mem3.szMas
84b40 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ter = 0;.    }. 
84b50 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f     for(i=0; i<N_
84b60 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  HASH; i++){.    
84b70 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26    memsys3Merge(&
84b80 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 29 3b  mem3.aiHash[i]);
84b90 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
84ba0 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31  =0; i<MX_SMALL-1
84bb0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65  ; i++){.      me
84bc0 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33  msys3Merge(&mem3
84bd0 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20  .aiSmall[i]);.  
84be0 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d 33    }.    if( mem3
84bf0 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20  .szMaster ){.   
84c00 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
84c10 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a  (mem3.iMaster);.
84c20 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73        if( mem3.s
84c30 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20  zMaster>=nBlock 
84c40 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
84c50 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73  n memsys3FromMas
84c60 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20  ter(nBlock);.   
84c70 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
84c80 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20    /* If none of 
84c90 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b 65 64  the above worked
84ca0 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c 2e 20  , then we fail. 
84cb0 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  */.  return 0;.}
84cc0 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20  ../*.** Free an 
84cd0 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f  outstanding memo
84ce0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a  ry allocation..*
84cf0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
84d00 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  on assumes that 
84d10 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75  the necessary mu
84d20 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61  texes, if any, a
84d30 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65  re.** already he
84d40 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ld by the caller
84d50 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22  . Hence "Unsafe"
84d60 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73  ..*/.void memsys
84d70 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69 64  3FreeUnsafe(void
84d80 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d 33 42   *pOld){.  Mem3B
84d90 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d 33 42  lock *p = (Mem3B
84da0 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e  lock*)pOld;.  in
84db0 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a 65 2c  t i;.  u32 size,
84dc0 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   x;.  assert( sq
84dd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
84de0 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a  (mem3.mutex) );.
84df0 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65 6d 33    assert( p>mem3
84e00 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d 65 6d  .aPool && p<&mem
84e10 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f  3.aPool[mem3.nPo
84e20 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70 20 2d  ol] );.  i = p -
84e30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61   mem3.aPool;.  a
84e40 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f  ssert( (mem3.aPo
84e50 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
84e60 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a 20 20  ze4x&1)==1 );.  
84e70 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  size = mem3.aPoo
84e80 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
84e90 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28  e4x/4;.  assert(
84ea0 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50   i+size<=mem3.nP
84eb0 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d 33 2e  ool+1 );.  mem3.
84ec0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
84ed0 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b 0a 20  .size4x &= ~1;. 
84ee0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69   mem3.aPool[i+si
84ef0 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  ze-1].u.hdr.prev
84f00 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 6d  Size = size;.  m
84f10 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65  em3.aPool[i+size
84f20 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
84f30 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73 79 73   &= ~2;.  memsys
84f40 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20  3Link(i);..  /* 
84f50 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20 74 68  Try to expand th
84f60 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67 20 74  e master using t
84f70 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64 20 63  he newly freed c
84f80 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20 6d 65  hunk */.  if( me
84f90 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a 20 20  m3.iMaster ){.  
84fa0 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61    while( (mem3.a
84fb0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
84fc0 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
84fd0 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  x&2)==0 ){.     
84fe0 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f   size = mem3.aPo
84ff0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d  ol[mem3.iMaster-
85000 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
85010 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d  e;.      mem3.iM
85020 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b 0a 20  aster -= size;. 
85030 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74       mem3.szMast
85040 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20 20 20  er += size;.    
85050 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28    memsys3Unlink(
85060 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20  mem3.iMaster);. 
85070 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50       x = mem3.aP
85080 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
85090 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
850a0 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33   & 2;.      mem3
850b0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
850c0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
850d0 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73  e4x = mem3.szMas
850e0 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20  ter*4 | x;.     
850f0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
85100 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
85110 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
85120 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e  prevSize = mem3.
85130 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a  szMaster;.    }.
85140 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f      x = mem3.aPo
85150 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d  ol[mem3.iMaster-
85160 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
85170 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  & 2;.    while( 
85180 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33  (mem3.aPool[mem3
85190 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
851a0 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
851b0 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29 7b 0a  size4x&1)==0 ){.
851c0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c        memsys3Unl
851d0 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ink(mem3.iMaster
851e0 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b  +mem3.szMaster);
851f0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61  .      mem3.szMa
85200 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f  ster += mem3.aPo
85210 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b  ol[mem3.iMaster+
85220 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d  mem3.szMaster-1]
85230 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b  .u.hdr.size4x/4;
85240 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  .      mem3.aPoo
85250 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31  l[mem3.iMaster-1
85260 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
85270 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34   mem3.szMaster*4
85280 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33   | x;.      mem3
85290 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
852a0 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  ter+mem3.szMaste
852b0 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  r-1].u.hdr.prevS
852c0 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73  ize = mem3.szMas
852d0 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ter;.    }.  }.}
852e0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
852f0 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75  he size of an ou
85300 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61  tstanding alloca
85310 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20  tion, in bytes. 
85320 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74   The.** size ret
85330 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20  urned omits the 
85340 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 6f 76  8-byte header ov
85350 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e  erhead.  This on
85360 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20  ly.** works for 
85370 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20  chunks that are 
85380 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65  currently checke
85390 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  d out..*/.static
853a0 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69 7a 65   int memsys3Size
853b0 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d 65 6d  (void *p){.  Mem
853c0 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a  3Block *pBlock;.
853d0 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
853e0 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63 6b 20  urn 0;.  pBlock 
853f0 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b  = (Mem3Block*)p;
85400 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 6c 6f  .  assert( (pBlo
85410 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ck[-1].u.hdr.siz
85420 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20 20 72  e4x&1)!=0 );.  r
85430 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31  eturn (pBlock[-1
85440 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 7e  ].u.hdr.size4x&~
85450 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a  3)*2 - 4;.}../*.
85460 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65  ** Round up a re
85470 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68  quest size to th
85480 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c  e next valid all
85490 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f  ocation size..*/
854a0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73  .static int mems
854b0 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e  ys3Roundup(int n
854c0 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32 20 29  ){.  if( n<=12 )
854d0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 32 3b  {.    return 12;
854e0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
854f0 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e 37 29  turn ((n+11)&~7)
85500 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   - 4;.  }.}../*.
85510 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74  ** Allocate nByt
85520 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f  es of memory..*/
85530 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
85540 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e 74 20  msys3Malloc(int 
85550 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 74  nBytes){.  sqlit
85560 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61  e3_int64 *p;.  a
85570 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e 30 20  ssert( nBytes>0 
85580 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d  );          /* m
85590 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72 73 20  alloc.c filters 
855a0 6f 75 74 20 30 20 62 79 74 65 20 72 65 71 75 65  out 0 byte reque
855b0 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79 73 33  sts */.  memsys3
855c0 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d  Enter();.  p = m
855d0 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61  emsys3MallocUnsa
855e0 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 6d 65  fe(nBytes);.  me
855f0 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20  msys3Leave();.  
85600 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b  return (void*)p;
85610 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20   .}../*.** Free 
85620 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20  memory..*/.void 
85630 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f 69 64  memsys3Free(void
85640 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73   *pPrior){.  ass
85650 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b 0a 20  ert( pPrior );. 
85660 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b   memsys3Enter();
85670 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e  .  memsys3FreeUn
85680 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  safe(pPrior);.  
85690 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a  memsys3Leave();.
856a0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
856b0 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65  the size of an e
856c0 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61  xisting memory a
856d0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69  llocation.*/.voi
856e0 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f  d *memsys3Reallo
856f0 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20  c(void *pPrior, 
85700 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69  int nBytes){.  i
85710 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20  nt nOld;.  void 
85720 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72  *p;.  if( pPrior
85730 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
85740 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  n sqlite3_malloc
85750 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20  (nBytes);.  }.  
85760 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b  if( nBytes<=0 ){
85770 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
85780 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20 20 72  e(pPrior);.    r
85790 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
857a0 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53 69 7a  Old = memsys3Siz
857b0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28  e(pPrior);.  if(
857c0 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26   nBytes<=nOld &&
857d0 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32   nBytes>=nOld-12
857e0 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  8 ){.    return 
857f0 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65  pPrior;.  }.  me
85800 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20  msys3Enter();.  
85810 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f  p = memsys3Mallo
85820 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b  cUnsafe(nBytes);
85830 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
85840 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65 73 20  if( nOld<nBytes 
85850 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
85860 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c 64 29  p, pPrior, nOld)
85870 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
85880 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72     memcpy(p, pPr
85890 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20  ior, nBytes);.  
858a0 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73 33 46    }.    memsys3F
858b0 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72  reeUnsafe(pPrior
858c0 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33  );.  }.  memsys3
858d0 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72  Leave();.  retur
858e0 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n p;.}../*.** In
858f0 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f  itialize this mo
85900 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dule..*/.static 
85910 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69 74 28  int memsys3Init(
85920 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
85930 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
85940 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69  ER(NotUsed);.  i
85950 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61  f( !sqlite3Globa
85960 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 20 29 7b  lConfig.pHeap ){
85970 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
85980 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
85990 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 6f 69 6e   /* Store a poin
859a0 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72  ter to the memor
859b0 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f 62 61  y block in globa
859c0 6c 20 73 74 72 75 63 74 75 72 65 20 6d 65 6d 33  l structure mem3
859d0 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73  . */.  assert( s
859e0 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29  izeof(Mem3Block)
859f0 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50  ==8 );.  mem3.aP
85a00 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b  ool = (Mem3Block
85a10 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   *)sqlite3Global
85a20 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20  Config.pHeap;.  
85a30 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71  mem3.nPool = (sq
85a40 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
85a50 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a 65 6f 66  g.nHeap / sizeof
85a60 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 2d 20 32  (Mem3Block)) - 2
85a70 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ;..  /* Initiali
85a80 7a 65 20 74 68 65 20 6d 61 73 74 65 72 20 62 6c  ze the master bl
85a90 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73  ock. */.  mem3.s
85aa0 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 6e  zMaster = mem3.n
85ab0 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d  Pool;.  mem3.mnM
85ac0 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d  aster = mem3.szM
85ad0 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d  aster;.  mem3.iM
85ae0 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 6d 65 6d  aster = 1;.  mem
85af0 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72  3.aPool[0].u.hdr
85b00 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 6d 33 2e  .size4x = (mem3.
85b10 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 2b 20 32  szMaster<<2) + 2
85b20 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  ;.  mem3.aPool[m
85b30 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72  em3.nPool].u.hdr
85b40 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33  .prevSize = mem3
85b50 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61  .nPool;.  mem3.a
85b60 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d  Pool[mem3.nPool]
85b70 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20  .u.hdr.size4x = 
85b80 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  1;..  return SQL
85b90 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
85ba0 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   Deinitialize th
85bb0 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74  is module..*/.st
85bc0 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
85bd0 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a  3Shutdown(void *
85be0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53  NotUsed){.  UNUS
85bf0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
85c00 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b  Used);.  return;
85c10 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  .}..../*.** Open
85c20 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61   the file indica
85c30 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20  ted and write a 
85c40 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65  log of all unfre
85c50 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c  ed memory .** al
85c60 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74  locations into t
85c70 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49  hat log..*/.SQLI
85c80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
85c90 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 33 44 75  sqlite3Memsys3Du
85ca0 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  mp(const char *z
85cb0 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 69 66 64 65  Filename){.#ifde
85cc0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
85cd0 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 75 33   FILE *out;.  u3
85ce0 32 20 69 2c 20 6a 3b 0a 20 20 75 33 32 20 73 69  2 i, j;.  u32 si
85cf0 7a 65 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  ze;.  if( zFilen
85d00 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e  ame==0 || zFilen
85d10 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20  ame[0]==0 ){.   
85d20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20   out = stdout;. 
85d30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20   }else{.    out 
85d40 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d  = fopen(zFilenam
85d50 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28  e, "w");.    if(
85d60 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20   out==0 ){.     
85d70 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
85d80 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f   "** Unable to o
85d90 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62  utput memory deb
85da0 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25  ug output log: %
85db0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20  s **\n",.       
85dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
85dd0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Filename);.     
85de0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
85df0 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65   }.  memsys3Ente
85e00 72 28 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  r();.  fprintf(o
85e10 75 74 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e 22 29  ut, "CHUNKS:\n")
85e20 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d  ;.  for(i=1; i<=
85e30 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73  mem3.nPool; i+=s
85e40 69 7a 65 2f 34 29 7b 0a 20 20 20 20 73 69 7a 65  ize/4){.    size
85e50 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
85e60 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b  1].u.hdr.size4x;
85e70 0a 20 20 20 20 69 66 28 20 73 69 7a 65 2f 34 3c  .    if( size/4<
85e80 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69  =1 ){.      fpri
85e90 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 73 69 7a  ntf(out, "%p siz
85ea0 65 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d  e error\n", &mem
85eb0 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20  3.aPool[i]);.   
85ec0 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a     assert( 0 );.
85ed0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
85ee0 20 7d 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65   }.    if( (size
85ef0 26 31 29 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 61  &1)==0 && mem3.a
85f00 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d  Pool[i+size/4-1]
85f10 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 21  .u.hdr.prevSize!
85f20 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 20 20 20 20  =size/4 ){.     
85f30 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
85f40 70 20 74 61 69 6c 20 73 69 7a 65 20 64 6f 65 73  p tail size does
85f50 20 6e 6f 74 20 6d 61 74 63 68 5c 6e 22 2c 20 26   not match\n", &
85f60 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a  mem3.aPool[i]);.
85f70 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20        assert( 0 
85f80 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
85f90 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 28      }.    if( ((
85fa0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a  mem3.aPool[i+siz
85fb0 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  e/4-1].u.hdr.siz
85fc0 65 34 78 26 32 29 3e 3e 31 29 21 3d 28 73 69 7a  e4x&2)>>1)!=(siz
85fd0 65 26 31 29 20 29 7b 0a 20 20 20 20 20 20 66 70  e&1) ){.      fp
85fe0 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74  rintf(out, "%p t
85ff0 61 69 6c 20 63 68 65 63 6b 6f 75 74 20 62 69 74  ail checkout bit
86000 20 69 73 20 69 6e 63 6f 72 72 65 63 74 5c 6e 22   is incorrect\n"
86010 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  , &mem3.aPool[i]
86020 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
86030 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61   0 );.      brea
86040 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
86050 20 73 69 7a 65 26 31 20 29 7b 0a 20 20 20 20 20   size&1 ){.     
86060 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
86070 70 20 25 36 64 20 62 79 74 65 73 20 63 68 65 63  p %6d bytes chec
86080 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d  ked out\n", &mem
86090 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a  3.aPool[i], (siz
860a0 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d  e/4)*8-8);.    }
860b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 70 72 69  else{.      fpri
860c0 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64  ntf(out, "%p %6d
860d0 20 62 79 74 65 73 20 66 72 65 65 25 73 5c 6e 22   bytes free%s\n"
860e0 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  , &mem3.aPool[i]
860f0 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a  , (size/4)*8-8,.
86100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
86110 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73 74 65    i==mem3.iMaste
86120 72 20 3f 20 22 20 2a 2a 6d 61 73 74 65 72 2a 2a  r ? " **master**
86130 22 20 3a 20 22 22 29 3b 0a 20 20 20 20 7d 0a 20  " : "");.    }. 
86140 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
86150 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29  MX_SMALL-1; i++)
86160 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61  {.    if( mem3.a
86170 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63  iSmall[i]==0 ) c
86180 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72  ontinue;.    fpr
86190 69 6e 74 66 28 6f 75 74 2c 20 22 73 6d 61 6c 6c  intf(out, "small
861a0 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20  (%2d):", i);.   
861b0 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69   for(j = mem3.ai
861c0 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a  Small[i]; j>0; j
861d0 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75  =mem3.aPool[j].u
861e0 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20  .list.next){.   
861f0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
86200 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33  " %p(%d)", &mem3
86210 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20  .aPool[j],.     
86220 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61           (mem3.a
86230 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[j-1].u.hdr.
86240 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a  size4x/4)*8-8);.
86250 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74      }.    fprint
86260 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20  f(out, "\n"); . 
86270 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
86280 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20  N_HASH; i++){.  
86290 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 48 61 73    if( mem3.aiHas
862a0 68 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  h[i]==0 ) contin
862b0 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  ue;.    fprintf(
862c0 6f 75 74 2c 20 22 68 61 73 68 28 25 32 64 29 3a  out, "hash(%2d):
862d0 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a  ", i);.    for(j
862e0 20 3d 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69   = mem3.aiHash[i
862f0 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61  ]; j>0; j=mem3.a
86300 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[j].u.list.n
86310 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 72 69  ext){.      fpri
86320 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64  ntf(out, " %p(%d
86330 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  )", &mem3.aPool[
86340 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  j],.            
86350 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d    (mem3.aPool[j-
86360 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
86370 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20  4)*8-8);.    }. 
86380 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
86390 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 70  "\n"); .  }.  fp
863a0 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 61 73 74  rintf(out, "mast
863b0 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69  er=%d\n", mem3.i
863c0 4d 61 73 74 65 72 29 3b 0a 20 20 66 70 72 69 6e  Master);.  fprin
863d0 74 66 28 6f 75 74 2c 20 22 6e 6f 77 55 73 65 64  tf(out, "nowUsed
863e0 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f  =%d\n", mem3.nPo
863f0 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61  ol*8 - mem3.szMa
86400 73 74 65 72 2a 38 29 3b 0a 20 20 66 70 72 69 6e  ster*8);.  fprin
86410 74 66 28 6f 75 74 2c 20 22 6d 78 55 73 65 64 3d  tf(out, "mxUsed=
86420 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f  %d\n", mem3.nPoo
86430 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73  l*8 - mem3.mnMas
86440 74 65 72 2a 38 29 3b 0a 20 20 73 71 6c 69 74 65  ter*8);.  sqlite
86450 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
86460 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28  m3.mutex);.  if(
86470 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a   out==stdout ){.
86480 20 20 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75      fflush(stdou
86490 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
864a0 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20   fclose(out);.  
864b0 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44  }.#else.  UNUSED
864c0 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 69 6c 65  _PARAMETER(zFile
864d0 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  name);.#endif.}.
864e0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
864f0 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  ine is the only 
86500 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
86510 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e  file with extern
86520 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a  al .** linkage..
86530 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74  **.** Populate t
86540 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d  he low-level mem
86550 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
86560 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73  unction pointers
86570 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c   in.** sqlite3Gl
86580 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74  obalConfig.m wit
86590 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  h pointers to th
865a0 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  e routines in th
865b0 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a 2a 20  is file. The.** 
865c0 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66  arguments specif
865d0 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d  y the block of m
865e0 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67 65 2e  emory to manage.
865f0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
86600 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c  ine is only call
86610 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f  ed by sqlite3_co
86620 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68 65 72  nfig(), and ther
86630 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f 74 20  efore.** is not 
86640 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 20 74  required to be t
86650 68 72 65 61 64 73 61 66 65 20 28 69 74 20 69 73  hreadsafe (it is
86660 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45   not)..*/.SQLITE
86670 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
86680 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
86690 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65  ds *sqlite3MemGe
866a0 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 7b 0a  tMemsys3(void){.
866b0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
866c0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
866d0 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64  ds mempoolMethod
866e0 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79  s = {.     memsy
866f0 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d  s3Malloc,.     m
86700 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20 20 20  emsys3Free,.    
86710 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 2c   memsys3Realloc,
86720 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 69 7a  .     memsys3Siz
86730 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52  e,.     memsys3R
86740 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d  oundup,.     mem
86750 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20 20 6d  sys3Init,.     m
86760 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e 2c 0a  emsys3Shutdown,.
86770 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65       0.  };.  re
86780 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74  turn &mempoolMet
86790 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  hods;.}..#endif 
867a0 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  /* SQLITE_ENABLE
867b0 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a  _MEMSYS3 */../**
867c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
867d0 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a   of mem3.c *****
867e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
867f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
86810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
86820 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63 20 2a  in file mem5.c *
86830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
86860 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20  ** 2007 October 
86870 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  14.**.** The aut
86880 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
86890 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
868a0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
868b0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
868c0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
868d0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
868e0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
868f0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
86900 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
86910 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
86920 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
86930 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
86940 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
86950 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
86960 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
86970 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
86980 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
86990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
869a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
869b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
869c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
869d0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
869e0 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75  ontains the C fu
869f0 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70  nctions that imp
86a00 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a  lement a memory.
86a10 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ** allocation su
86a20 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20  bsystem for use 
86a30 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a  by SQLite. .**.*
86a40 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
86a50 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
86a60 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
86a70 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75  m omits all.** u
86a80 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20  se of malloc(). 
86a90 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20  The SQLite user 
86aa0 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b  supplies a block
86ab0 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65   of memory.** be
86ac0 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c  fore calling sql
86ad0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
86ae0 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c  ) from which all
86af0 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  ocations.** are 
86b00 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65  made and returne
86b10 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63  d by the xMalloc
86b20 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28  () and xRealloc(
86b30 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ) .** implementa
86b40 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69  tions. Once sqli
86b50 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
86b60 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
86b70 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20  ,.** the amount 
86b80 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61  of memory availa
86b90 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73  ble to SQLite is
86ba0 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f   fixed and canno
86bb0 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e  t.** be changed.
86bc0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  .**.** This vers
86bd0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
86be0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
86bf0 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64  system is includ
86c00 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69  ed.** in the bui
86c10 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54  ld only if SQLIT
86c20 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  E_ENABLE_MEMSYS5
86c30 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a   is defined..**.
86c40 2a 2a 20 24 49 64 3a 20 6d 65 6d 35 2e 63 2c 76  ** $Id: mem5.c,v
86c50 20 31 2e 31 39 20 32 30 30 38 2f 31 31 2f 31 39   1.19 2008/11/19
86c60 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c   16:52:44 daniel
86c70 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
86c80 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
86c90 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
86ca0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73   allocator is us
86cb0 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a  ed only when .**
86cc0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
86cd0 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 6e 65  EMSYS5 is define
86ce0 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  d..*/.#ifdef SQL
86cf0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
86d00 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69  S5../*.** A mini
86d10 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  mum allocation i
86d20 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
86d30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
86d40 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72  tructure..** Lar
86d50 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ger allocations 
86d60 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20  are an array of 
86d70 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73  these structures
86d80 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 73 69   where the.** si
86d90 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 20  ze of the array 
86da0 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e  is a power of 2.
86db0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
86dc0 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35  ct Mem5Link Mem5
86dd0 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d  Link;.struct Mem
86de0 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20 6e 65  5Link {.  int ne
86df0 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  xt;       /* Ind
86e00 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20  ex of next free 
86e10 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74 20 70  chunk */.  int p
86e20 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e  rev;       /* In
86e30 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75 73 20  dex of previous 
86e40 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b  free chunk */.};
86e50 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20  ../*.** Maximum 
86e60 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c 6c 6f  size of any allo
86e70 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c 3c 4c  cation is ((1<<L
86e80 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 6e 41 74 6f  OGMAX)*mem5.nAto
86e90 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d  m). Since.** mem
86ea0 35 2e 6e 41 74 6f 6d 20 69 73 20 61 6c 77 61 79  5.nAtom is alway
86eb0 73 20 61 74 20 6c 65 61 73 74 20 38 2c 20 74 68  s at least 8, th
86ec0 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79  is is not really
86ed0 20 61 20 70 72 61 63 74 69 63 61 6c 0a 2a 2a 20   a practical.** 
86ee0 6c 69 6d 69 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 23  limitation..*/.#
86ef0 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30  define LOGMAX 30
86f00 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73  ../*.** Masks us
86f10 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72  ed for mem5.aCtr
86f20 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f  l[] elements..*/
86f30 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f  .#define CTRL_LO
86f40 47 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f  GSIZE  0x1f    /
86f50 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74  * Log2 Size of t
86f60 68 69 73 20 62 6c 6f 63 6b 20 72 65 6c 61 74 69  his block relati
86f70 76 65 20 74 6f 20 50 4f 57 32 5f 4d 49 4e 20 2a  ve to POW2_MIN *
86f80 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 46  /.#define CTRL_F
86f90 52 45 45 20 20 20 20 20 30 78 32 30 20 20 20 20  REE     0x20    
86fa0 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 20 63  /* True if not c
86fb0 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f  hecked out */../
86fc0 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20  *.** All of the 
86fd0 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73  static variables
86fe0 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f   used by this mo
86ff0 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74  dule are collect
87000 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e  ed.** into a sin
87010 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61  gle structure na
87020 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54 68 69  med "mem5".  Thi
87030 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65  s is to keep the
87040 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61  .** static varia
87050 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61  bles organized a
87060 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d  nd to reduce nam
87070 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e  espace pollution
87080 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f  .** when this mo
87090 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64  dule is combined
870a0 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74   with other in t
870b0 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e  he amalgamation.
870c0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
870d0 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d  E_WSD struct Mem
870e0 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20  5Global {.  /*. 
870f0 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c   ** Memory avail
87100 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74  able for allocat
87110 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e  ion.  */.  int n
87120 41 74 6f 6d 3b 20 20 20 20 20 20 20 2f 2a 20 53  Atom;       /* S
87130 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 6c 65  mallest possible
87140 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62   allocation in b
87150 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ytes */.  int nB
87160 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 4e 75  lock;      /* Nu
87170 6d 62 65 72 20 6f 66 20 6e 41 74 6f 6d 20 73 69  mber of nAtom si
87180 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20 7a 50  zed blocks in zP
87190 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f  ool */.  u8 *zPo
871a0 6f 6c 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a  ol;.  .  /*.  **
871b0 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f   Mutex to contro
871c0 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  l access to the 
871d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
871e0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a  n subsystem..  *
871f0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  /.  sqlite3_mute
87200 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a  x *mutex;..  /*.
87210 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65    ** Performance
87220 20 73 74 61 74 69 73 74 69 63 73 0a 20 20 2a 2f   statistics.  */
87230 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20  .  u64 nAlloc;  
87240 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20         /* Total 
87250 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20  number of calls 
87260 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75  to malloc */.  u
87270 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20  64 totalAlloc;  
87280 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61     /* Total of a
87290 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20  ll malloc calls 
872a0 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74 65 72  - includes inter
872b0 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20 75 36  nal frag */.  u6
872c0 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b 20 20  4 totalExcess;  
872d0 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72    /* Total inter
872e0 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f  nal fragmentatio
872f0 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65  n */.  u32 curre
87300 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20 43 75  ntOut;     /* Cu
87310 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20  rrent checkout, 
87320 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e  including intern
87330 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e  al fragmentation
87340 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e   */.  u32 curren
87350 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72  tCount;   /* Cur
87360 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 64  rent number of d
87370 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f 75 74  istinct checkout
87380 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75  s */.  u32 maxOu
87390 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  t;         /* Ma
873a0 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65  ximum instantane
873b0 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74 20 2a  ous currentOut *
873c0 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75 6e 74  /.  u32 maxCount
873d0 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d  ;       /* Maxim
873e0 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73  um instantaneous
873f0 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f   currentCount */
87400 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75 65 73  .  u32 maxReques
87410 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73  t;     /* Larges
87420 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78  t allocation (ex
87430 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74 65 72  clusive of inter
87440 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a  nal frag) */.  .
87450 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20    /*.  ** Lists 
87460 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 6f  of free blocks o
87470 66 20 76 61 72 69 6f 75 73 20 73 69 7a 65 73 2e  f various sizes.
87480 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69 46 72  .  */.  int aiFr
87490 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d  eelist[LOGMAX+1]
874a0 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 70 61  ;..  /*.  ** Spa
874b0 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e 67 20  ce for tracking 
874c0 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61 72 65  which blocks are
874d0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64   checked out and
874e0 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f   the size.  ** o
874f0 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20 20 4f  f each block.  O
87500 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c 6f 63  ne byte per bloc
87510 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a 61 43  k..  */.  u8 *aC
87520 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d 20 7b  trl;..} mem5 = {
87530 20 31 39 38 30 34 31 36 37 20 7d 3b 0a 0a 23 64   19804167 };..#d
87540 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c 4f 42 41  efine mem5 GLOBA
87550 4c 28 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f  L(struct Mem5Glo
87560 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 23 64 65 66  bal, mem5)..#def
87570 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69 64 78  ine MEM5LINK(idx
87580 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28  ) ((Mem5Link *)(
87590 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78  &mem5.zPool[(idx
875a0 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 29 29 0a  )*mem5.nAtom])).
875b0 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68  ./*.** Unlink th
875c0 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e  e chunk at mem5.
875d0 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69  aPool[i] from li
875e0 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74  st it is current
875f0 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20 73 68  ly.** on.  It sh
87600 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64 20 6f 6e  ould be found on
87610 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
87620 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f 0a 73  [iLogsize]..*/.s
87630 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
87640 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c 20  s5Unlink(int i, 
87650 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20  int iLogsize){. 
87660 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 65 76 3b   int next, prev;
87670 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
87680 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  && i<mem5.nBlock
87690 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c   );.  assert( iL
876a0 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f  ogsize>=0 && iLo
876b0 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b  gsize<=LOGMAX );
876c0 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35  .  assert( (mem5
876d0 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c  .aCtrl[i] & CTRL
876e0 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73  _LOGSIZE)==iLogs
876f0 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74 20 3d  ize );..  next =
87700 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65   MEM5LINK(i)->ne
87710 78 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d 45 4d  xt;.  prev = MEM
87720 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 3b 0a  5LINK(i)->prev;.
87730 20 20 69 66 28 20 70 72 65 76 3c 30 20 29 7b 0a    if( prev<0 ){.
87740 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c      mem5.aiFreel
87750 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20  ist[iLogsize] = 
87760 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  next;.  }else{. 
87770 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72 65 76     MEM5LINK(prev
87780 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a  )->next = next;.
87790 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 3e 3d    }.  if( next>=
877a0 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e  0 ){.    MEM5LIN
877b0 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76 20 3d 20  K(next)->prev = 
877c0 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  prev;.  }.}../*.
877d0 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e  ** Link the chun
877e0 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b  k at mem5.aPool[
877f0 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f 6e  i] so that is on
87800 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a 2a 2a   the iLogsize.**
87810 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73   free list..*/.s
87820 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
87830 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e  s5Link(int i, in
87840 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69  t iLogsize){.  i
87850 6e 74 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20  nt x;.  assert( 
87860 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
87870 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29 20 29  ld(mem5.mutex) )
87880 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ;.  assert( i>=0
87890 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63   && i<mem5.nBloc
878a0 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  k );.  assert( i
878b0 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c  Logsize>=0 && iL
878c0 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29  ogsize<=LOGMAX )
878d0 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d  ;.  assert( (mem
878e0 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52  5.aCtrl[i] & CTR
878f0 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67  L_LOGSIZE)==iLog
87900 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d 20 4d  size );..  x = M
87910 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74  EM5LINK(i)->next
87920 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69   = mem5.aiFreeli
87930 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20  st[iLogsize];.  
87940 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65  MEM5LINK(i)->pre
87950 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 78 3e  v = -1;.  if( x>
87960 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
87970 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20  ( x<mem5.nBlock 
87980 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28  );.    MEM5LINK(
87990 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a 20 20  x)->prev = i;.  
879a0 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c  }.  mem5.aiFreel
879b0 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20  ist[iLogsize] = 
879c0 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  i;.}../*.** If t
879d0 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75  he STATIC_MEM mu
879e0 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  tex is not alrea
879f0 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 6e 20  dy held, obtain 
87a00 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 74 65  it now. The mute
87a10 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 61 64  x.** will alread
87a20 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74 61 69  y be held (obtai
87a30 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6d  ned by code in m
87a40 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a 20 73  alloc.c) if.** s
87a50 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
87a60 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 20 74  ig.bMemStat is t
87a70 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  rue..*/.static v
87a80 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e 74 65 72  oid memsys5Enter
87a90 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71  (void){.  if( sq
87aa0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
87ab0 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 26 26  g.bMemstat==0 &&
87ac0 20 6d 65 6d 35 2e 6d 75 74 65 78 3d 3d 30 20 29   mem5.mutex==0 )
87ad0 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74 65 78  {.    mem5.mutex
87ae0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
87af0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
87b00 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20  X_STATIC_MEM);. 
87b10 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
87b20 65 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75  ex_enter(mem5.mu
87b30 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  tex);.}.static v
87b40 6f 69 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65  oid memsys5Leave
87b50 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  (void){.  sqlite
87b60 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
87b70 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a  m5.mutex);.}../*
87b80 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
87b90 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61  ize of an outsta
87ba0 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e  nding allocation
87bb0 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65  , in bytes.  The
87bc0 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65  .** size returne
87bd0 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79  d omits the 8-by
87be0 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65  te header overhe
87bf0 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a  ad.  This only.*
87c00 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e  * works for chun
87c10 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72  ks that are curr
87c20 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75  ently checked ou
87c30 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
87c40 20 6d 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69   memsys5Size(voi
87c50 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69  d *p){.  int iSi
87c60 7a 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20  ze = 0;.  if( p 
87c70 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28  ){.    int i = (
87c80 28 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f  (u8 *)p-mem5.zPo
87c90 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a  ol)/mem5.nAtom;.
87ca0 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30      assert( i>=0
87cb0 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63   && i<mem5.nBloc
87cc0 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d  k );.    iSize =
87cd0 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 2a 20 28 31   mem5.nAtom * (1
87ce0 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b   << (mem5.aCtrl[
87cf0 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29  i]&CTRL_LOGSIZE)
87d00 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
87d10 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  iSize;.}../*.** 
87d20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 65  Find the first e
87d30 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65 65  ntry on the free
87d40 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20  list iLogsize.  
87d50 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65  Unlink that.** e
87d60 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20  ntry and return 
87d70 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73  its index. .*/.s
87d80 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73  tatic int memsys
87d90 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74  5UnlinkFirst(int
87da0 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e   iLogsize){.  in
87db0 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72 73  t i;.  int iFirs
87dc0 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 4c  t;..  assert( iL
87dd0 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f  ogsize>=0 && iLo
87de0 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b  gsize<=LOGMAX );
87df0 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d 20  .  i = iFirst = 
87e00 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
87e10 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73  iLogsize];.  ass
87e20 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20 29  ert( iFirst>=0 )
87e30 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29  ;.  while( i>0 )
87e40 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69 72  {.    if( i<iFir
87e50 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69 3b  st ) iFirst = i;
87e60 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e  .    i = MEM5LIN
87e70 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a  K(i)->next;.  }.
87e80 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28    memsys5Unlink(
87e90 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65  iFirst, iLogsize
87ea0 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69 72  );.  return iFir
87eb0 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  st;.}../*.** Ret
87ec0 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d  urn a block of m
87ed0 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73  emory of at leas
87ee0 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65  t nBytes in size
87ef0 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c  ..** Return NULL
87f00 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2f 0a 73   if unable..*/.s
87f10 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73  tatic void *mems
87f20 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28  ys5MallocUnsafe(
87f30 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e  int nByte){.  in
87f40 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f  t i;           /
87f50 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d 65 6d  * Index of a mem
87f60 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a  5.aPool[] slot *
87f70 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20 20 20  /.  int iBin;   
87f80 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
87f90 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69  to mem5.aiFreeli
87fa0 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 46  st[] */.  int iF
87fb0 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69  ullSz;     /* Si
87fc0 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  ze of allocation
87fd0 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 70   rounded up to p
87fe0 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20 20 69  ower of 2 */.  i
87ff0 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20 20 20  nt iLogsize;    
88000 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75 6c 6c  /* Log2 of iFull
88010 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a  Sz/POW2_MIN */..
88020 20 20 2f 2a 20 4b 65 65 70 20 74 72 61 63 6b 20    /* Keep track 
88030 6f 66 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61  of the maximum a
88040 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73  llocation reques
88050 74 2e 20 20 45 76 65 6e 20 75 6e 66 75 6c 66 69  t.  Even unfulfi
88060 6c 6c 65 64 0a 20 20 2a 2a 20 72 65 71 75 65 73  lled.  ** reques
88070 74 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 2a  ts are counted *
88080 2f 0a 20 20 69 66 28 20 28 75 33 32 29 6e 42 79  /.  if( (u32)nBy
88090 74 65 3e 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65  te>mem5.maxReque
880a0 73 74 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d  st ){.    mem5.m
880b0 61 78 52 65 71 75 65 73 74 20 3d 20 6e 42 79 74  axRequest = nByt
880c0 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75  e;.  }..  /* Rou
880d0 6e 64 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74  nd nByte up to t
880e0 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f  he next valid po
880f0 77 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20  wer of two */.  
88100 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35  for(iFullSz=mem5
88110 2e 6e 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65  .nAtom, iLogsize
88120 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74  =0; iFullSz<nByt
88130 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c  e; iFullSz *= 2,
88140 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a   iLogsize++){}..
88150 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d    /* Make sure m
88160 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69  em5.aiFreelist[i
88170 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e  Logsize] contain
88180 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66  s at least one f
88190 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20  ree.  ** block. 
881a0 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70   If not, then sp
881b0 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74  lit a block of t
881c0 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70  he next larger p
881d0 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f  ower of.  ** two
881e0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65   in order to cre
881f0 61 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62  ate a new free b
88200 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f  lock of size iLo
88210 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f  gsize..  */.  fo
88220 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b  r(iBin=iLogsize;
88230 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
88240 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e  [iBin]<0 && iBin
88250 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b  <=LOGMAX; iBin++
88260 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c  ){}.  if( iBin>L
88270 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30  OGMAX ) return 0
88280 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55  ;.  i = memsys5U
88290 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29  nlinkFirst(iBin)
882a0 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e  ;.  while( iBin>
882b0 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20  iLogsize ){.    
882c0 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20  int newSize;..  
882d0 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65    iBin--;.    ne
882e0 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69  wSize = 1 << iBi
882f0 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72  n;.    mem5.aCtr
88300 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43  l[i+newSize] = C
88310 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b  TRL_FREE | iBin;
88320 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b  .    memsys5Link
88330 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e  (i+newSize, iBin
88340 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43  );.  }.  mem5.aC
88350 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a  trl[i] = iLogsiz
88360 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20  e;..  /* Update 
88370 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72  allocator perfor
88380 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73  mance statistics
88390 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c  . */.  mem5.nAll
883a0 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74  oc++;.  mem5.tot
883b0 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c  alAlloc += iFull
883c0 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c  Sz;.  mem5.total
883d0 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53  Excess += iFullS
883e0 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d  z - nByte;.  mem
883f0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b  5.currentCount++
88400 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  ;.  mem5.current
88410 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a  Out += iFullSz;.
88420 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f    if( mem5.maxCo
88430 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74  unt<mem5.current
88440 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78  Count ) mem5.max
88450 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72  Count = mem5.cur
88460 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28  rentCount;.  if(
88470 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d   mem5.maxOut<mem
88480 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d  5.currentOut ) m
88490 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d  em5.maxOut = mem
884a0 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20  5.currentOut;.. 
884b0 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69   /* Return a poi
884c0 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f  nter to the allo
884d0 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f  cated memory. */
884e0 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a  .  return (void*
884f0 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d  )&mem5.zPool[i*m
88500 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f  em5.nAtom];.}../
88510 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74  *.** Free an out
88520 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20  standing memory 
88530 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  allocation..*/.s
88540 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
88550 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69  s5FreeUnsafe(voi
88560 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32 20  d *pOld){.  u32 
88570 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a  size, iLogsize;.
88580 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 20 20 20    int iBlock;   
88590 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20 2f 2a            ..  /*
885a0 20 53 65 74 20 69 42 6c 6f 63 6b 20 74 6f 20 74   Set iBlock to t
885b0 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
885c0 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 20 74 6f  block pointed to
885d0 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a 20 20 2a   by pOld in .  *
885e0 2a 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6d  * the array of m
885f0 65 6d 35 2e 6e 41 74 6f 6d 20 62 79 74 65 20 62  em5.nAtom byte b
88600 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 64 20 74 6f  locks pointed to
88610 20 62 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a   by mem5.zPool..
88620 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20 3d 20    */.  iBlock = 
88630 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35  ((u8 *)pOld-mem5
88640 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74  .zPool)/mem5.nAt
88650 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  om;..  /* Check 
88660 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72  that the pointer
88670 20 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20   pOld points to 
88680 61 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65  a valid, non-fre
88690 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73  e block. */.  as
886a0 73 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20  sert( iBlock>=0 
886b0 26 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e  && iBlock<mem5.n
886c0 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72  Block );.  asser
886d0 74 28 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d  t( ((u8 *)pOld-m
886e0 65 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e  em5.zPool)%mem5.
886f0 6e 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73  nAtom==0 );.  as
88700 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72  sert( (mem5.aCtr
88710 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c  l[iBlock] & CTRL
88720 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20  _FREE)==0 );..  
88730 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e  iLogsize = mem5.
88740 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20  aCtrl[iBlock] & 
88750 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20  CTRL_LOGSIZE;.  
88760 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69  size = 1<<iLogsi
88770 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42  ze;.  assert( iB
88780 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32  lock+size-1<(u32
88790 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a  )mem5.nBlock );.
887a0 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42  .  mem5.aCtrl[iB
887b0 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52  lock] |= CTRL_FR
887c0 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c  EE;.  mem5.aCtrl
887d0 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20  [iBlock+size-1] 
887e0 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20  |= CTRL_FREE;.  
887f0 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72  assert( mem5.cur
88800 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20  rentCount>0 );. 
88810 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75   assert( mem5.cu
88820 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a  rrentOut>=(size*
88830 6d 65 6d 35 2e 6e 41 74 6f 6d 29 20 29 3b 0a 20  mem5.nAtom) );. 
88840 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75   mem5.currentCou
88850 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72  nt--;.  mem5.cur
88860 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a  rentOut -= size*
88870 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 61 73  mem5.nAtom;.  as
88880 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65  sert( mem5.curre
88890 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e  ntOut>0 || mem5.
888a0 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20  currentCount==0 
888b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  );.  assert( mem
888c0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30  5.currentCount>0
888d0 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74   || mem5.current
888e0 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d  Out==0 );..  mem
888f0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
88900 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c  = CTRL_FREE | iL
88910 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28  ogsize;.  while(
88920 20 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58   iLogsize<LOGMAX
88930 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 64   ){.    int iBud
88940 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 6c  dy;.    if( (iBl
88950 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26  ock>>iLogsize) &
88960 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 64   1 ){.      iBud
88970 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69  dy = iBlock - si
88980 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ze;.    }else{. 
88990 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42       iBuddy = iB
889a0 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20  lock + size;.   
889b0 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69   }.    assert( i
889c0 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20  Buddy>=0 );.    
889d0 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c 3c  if( (iBuddy+(1<<
889e0 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e  iLogsize))>mem5.
889f0 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a  nBlock ) break;.
88a00 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 74      if( mem5.aCt
88a10 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 52  rl[iBuddy]!=(CTR
88a20 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a  L_FREE | iLogsiz
88a30 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  e) ) break;.    
88a40 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42  memsys5Unlink(iB
88a50 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b  uddy, iLogsize);
88a60 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b  .    iLogsize++;
88a70 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 3c  .    if( iBuddy<
88a80 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  iBlock ){.      
88a90 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64  mem5.aCtrl[iBudd
88aa0 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c  y] = CTRL_FREE |
88ab0 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20   iLogsize;.     
88ac0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
88ad0 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ck] = 0;.      i
88ae0 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a  Block = iBuddy;.
88af0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
88b00 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
88b10 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  ck] = CTRL_FREE 
88b20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20  | iLogsize;.    
88b30 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75    mem5.aCtrl[iBu
88b40 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ddy] = 0;.    }.
88b50 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20      size *= 2;. 
88b60 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b   }.  memsys5Link
88b70 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a  (iBlock, iLogsiz
88b80 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  e);.}../*.** All
88b90 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20  ocate nBytes of 
88ba0 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63  memory.*/.static
88bb0 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61   void *memsys5Ma
88bc0 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29  lloc(int nBytes)
88bd0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
88be0 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  4 *p = 0;.  if( 
88bf0 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 20  nBytes>0 ){.    
88c00 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a  memsys5Enter();.
88c10 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d      p = memsys5M
88c20 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74  allocUnsafe(nByt
88c30 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35  es);.    memsys5
88c40 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72  Leave();.  }.  r
88c50 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20  eturn (void*)p; 
88c60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d  .}../*.** Free m
88c70 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  emory..*/.static
88c80 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 65   void memsys5Fre
88c90 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b  e(void *pPrior){
88ca0 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30  .  if( pPrior==0
88cb0 20 29 7b 0a 61 73 73 65 72 74 28 30 29 3b 0a 20   ){.assert(0);. 
88cc0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
88cd0 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b   memsys5Enter();
88ce0 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e  .  memsys5FreeUn
88cf0 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  safe(pPrior);.  
88d00 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 20  memsys5Leave(); 
88d10 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67   .}../*.** Chang
88d20 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  e the size of an
88d30 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79   existing memory
88d40 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73   allocation.*/.s
88d50 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73  tatic void *mems
88d60 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20  ys5Realloc(void 
88d70 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79  *pPrior, int nBy
88d80 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64  tes){.  int nOld
88d90 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69  ;.  void *p;.  i
88da0 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a  f( pPrior==0 ){.
88db0 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79      return memsy
88dc0 73 35 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29  s5Malloc(nBytes)
88dd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74  ;.  }.  if( nByt
88de0 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d  es<=0 ){.    mem
88df0 73 79 73 35 46 72 65 65 28 70 50 72 69 6f 72 29  sys5Free(pPrior)
88e00 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
88e10 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d    }.  nOld = mem
88e20 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f 72 29  sys5Size(pPrior)
88e30 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d  ;.  if( nBytes<=
88e40 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 74 75  nOld ){.    retu
88e50 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  rn pPrior;.  }. 
88e60 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b   memsys5Enter();
88e70 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61  .  p = memsys5Ma
88e80 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65  llocUnsafe(nByte
88e90 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  s);.  if( p ){. 
88ea0 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72     memcpy(p, pPr
88eb0 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20  ior, nOld);.    
88ec0 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66  memsys5FreeUnsaf
88ed0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20  e(pPrior);.  }. 
88ee0 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b   memsys5Leave();
88ef0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
88f00 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61  /*.** Round up a
88f10 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f   request size to
88f20 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20   the next valid 
88f30 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e  allocation size.
88f40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
88f50 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 6e  emsys5Roundup(in
88f60 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c  t n){.  int iFul
88f70 6c 53 7a 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c  lSz;.  for(iFull
88f80 53 7a 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 20 69  Sz=mem5.nAtom; i
88f90 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53  FullSz<n; iFullS
88fa0 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74 75 72  z *= 2);.  retur
88fb0 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a 73 74  n iFullSz;.}..st
88fc0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35  atic int memsys5
88fd0 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 7b  Log(int iValue){
88fe0 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66  .  int iLog;.  f
88ff0 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69  or(iLog=0; (1<<i
89000 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f  Log)<iValue; iLo
89010 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  g++);.  return i
89020 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  Log;.}../*.** In
89030 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f  itialize this mo
89040 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dule..*/.static 
89050 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69 74 28  int memsys5Init(
89060 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
89070 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
89080 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 47  nByte = sqlite3G
89090 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61  lobalConfig.nHea
890a0 70 3b 0a 20 20 75 38 20 2a 7a 42 79 74 65 20 3d  p;.  u8 *zByte =
890b0 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 47 6c   (u8 *)sqlite3Gl
890c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70  obalConfig.pHeap
890d0 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b  ;.  int nMinLog;
890e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
890f0 20 2f 2a 20 4c 6f 67 20 6f 66 20 6d 69 6e 69 6d   /* Log of minim
89100 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69  um allocation si
89110 7a 65 20 69 6e 20 62 79 74 65 73 2a 2f 0a 20 20  ze in bytes*/.  
89120 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 0a 20 20  int iOffset;..  
89130 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
89140 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66  (NotUsed);..  if
89150 28 20 21 7a 42 79 74 65 20 29 7b 0a 20 20 20 20  ( !zByte ){.    
89160 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
89170 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 6e 4d 69 6e  ROR;.  }..  nMin
89180 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67  Log = memsys5Log
89190 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
891a0 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d  nfig.mnReq);.  m
891b0 65 6d 35 2e 6e 41 74 6f 6d 20 3d 20 28 31 3c 3c  em5.nAtom = (1<<
891c0 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68 69 6c  nMinLog);.  whil
891d0 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 4d  e( (int)sizeof(M
891e0 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 6e 41  em5Link)>mem5.nA
891f0 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e  tom ){.    mem5.
89200 6e 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 6e 41 74  nAtom = mem5.nAt
89210 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20  om << 1;.  }..  
89220 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e  mem5.nBlock = (n
89230 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 6e 41 74  Byte / (mem5.nAt
89240 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29 3b  om+sizeof(u8)));
89250 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20  .  mem5.zPool = 
89260 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43  zByte;.  mem5.aC
89270 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d  trl = (u8 *)&mem
89280 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c  5.zPool[mem5.nBl
89290 6f 63 6b 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b  ock*mem5.nAtom];
892a0 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
892b0 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 7b  <=LOGMAX; ii++){
892c0 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 65  .    mem5.aiFree
892d0 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20  list[ii] = -1;. 
892e0 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 3d 20   }..  iOffset = 
892f0 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f 47 4d  0;.  for(ii=LOGM
89300 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d 2d 29  AX; ii>=0; ii--)
89310 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63  {.    int nAlloc
89320 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 20 20   = (1<<ii);.    
89330 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e 41 6c  if( (iOffset+nAl
89340 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63  loc)<=mem5.nBloc
89350 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e  k ){.      mem5.
89360 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 20 3d  aCtrl[iOffset] =
89370 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 45 3b   ii | CTRL_FREE;
89380 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 69  .      memsys5Li
89390 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 29 3b  nk(iOffset, ii);
893a0 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 20 2b  .      iOffset +
893b0 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a  = nAlloc;.    }.
893c0 20 20 20 20 61 73 73 65 72 74 28 28 69 4f 66 66      assert((iOff
893d0 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35  set+nAlloc)>mem5
893e0 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20  .nBlock);.  }.. 
893f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
89400 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e  K;.}../*.** Dein
89410 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f  itialize this mo
89420 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dule..*/.static 
89430 76 6f 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74  void memsys5Shut
89440 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73  down(void *NotUs
89450 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ed){.  UNUSED_PA
89460 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
89470 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;.  return;.}../
89480 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69  *.** Open the fi
89490 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64  le indicated and
894a0 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20   write a log of 
894b0 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f  all unfreed memo
894c0 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  ry .** allocatio
894d0 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67  ns into that log
894e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
894f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
89500 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 6f 6e 73  Memsys5Dump(cons
89510 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
89520 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  e){.#ifdef SQLIT
89530 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a  E_DEBUG.  FILE *
89540 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c  out;.  int i, j,
89550 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f   n;.  int nMinLo
89560 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  g;..  if( zFilen
89570 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e  ame==0 || zFilen
89580 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20  ame[0]==0 ){.   
89590 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20   out = stdout;. 
895a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20   }else{.    out 
895b0 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d  = fopen(zFilenam
895c0 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28  e, "w");.    if(
895d0 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20   out==0 ){.     
895e0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
895f0 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f   "** Unable to o
89600 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62  utput memory deb
89610 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25  ug output log: %
89620 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20  s **\n",.       
89630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
89640 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Filename);.     
89650 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
89660 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65   }.  memsys5Ente
89670 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d  r();.  nMinLog =
89680 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35   memsys5Log(mem5
89690 2e 6e 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69  .nAtom);.  for(i
896a0 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26  =0; i<=LOGMAX &&
896b0 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69   i+nMinLog<32; i
896c0 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30  ++){.    for(n=0
896d0 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c  , j=mem5.aiFreel
896e0 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20  ist[i]; j>=0; j 
896f0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e  = MEM5LINK(j)->n
89700 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20  ext, n++){}.    
89710 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 72  fprintf(out, "fr
89720 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 20  eelist items of 
89730 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20  size %d: %d\n", 
89740 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c 3c 20 69 2c  mem5.nAtom << i,
89750 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 69 6e   n);.  }.  fprin
89760 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e 41  tf(out, "mem5.nA
89770 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 6c 6c  lloc       = %ll
89780 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f  u\n", mem5.nAllo
89790 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  c);.  fprintf(ou
897a0 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c  t, "mem5.totalAl
897b0 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c  loc   = %llu\n",
897c0 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63   mem5.totalAlloc
897d0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
897e0 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63  , "mem5.totalExc
897f0 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20  ess  = %llu\n", 
89800 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73  mem5.totalExcess
89810 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
89820 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f  , "mem5.currentO
89830 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65  ut   = %u\n", me
89840 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 3b 0a  m5.currentOut);.
89850 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
89860 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e  mem5.currentCoun
89870 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e  t = %u\n", mem5.
89880 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 0a 20  currentCount);. 
89890 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d   fprintf(out, "m
898a0 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 20 20  em5.maxOut      
898b0 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d   = %u\n", mem5.m
898c0 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e 74  axOut);.  fprint
898d0 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78  f(out, "mem5.max
898e0 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 5c 6e  Count     = %u\n
898f0 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74  ", mem5.maxCount
89900 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
89910 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65  , "mem5.maxReque
89920 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65  st   = %u\n", me
89930 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 3b 0a  m5.maxRequest);.
89940 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29    memsys5Leave()
89950 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64  ;.  if( out==std
89960 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73  out ){.    fflus
89970 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c  h(stdout);.  }el
89980 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f  se{.    fclose(o
89990 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  ut);.  }.#else. 
899a0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
899b0 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65  R(zFilename);.#e
899c0 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ndif.}../*.** Th
899d0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
899e0 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69  e only routine i
899f0 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68  n this file with
89a00 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69   external .** li
89a10 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75 72 6e  nkage. It return
89a20 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
89a30 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
89a40 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 73  mem_methods.** s
89a50 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65 64 20  truct populated 
89a60 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79 73 35  with the memsys5
89a70 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c   methods..*/.SQL
89a80 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
89a90 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  t sqlite3_mem_me
89aa0 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65  thods *sqlite3Me
89ab0 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f 69 64  mGetMemsys5(void
89ac0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
89ad0 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  t sqlite3_mem_me
89ae0 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d 65 74  thods memsys5Met
89af0 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65  hods = {.     me
89b00 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20  msys5Malloc,.   
89b10 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c 0a 20    memsys5Free,. 
89b20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61 6c 6c      memsys5Reall
89b30 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35  oc,.     memsys5
89b40 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79  Size,.     memsy
89b50 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20  s5Roundup,.     
89b60 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20 20 20  memsys5Init,.   
89b70 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f 77    memsys5Shutdow
89b80 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20  n,.     0.  };. 
89b90 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79 73 35   return &memsys5
89ba0 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64  Methods;.}..#end
89bb0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41  if /* SQLITE_ENA
89bc0 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a  BLE_MEMSYS5 */..
89bd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
89be0 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a  End of mem5.c **
89bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
89c20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
89c30 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78  Begin file mutex
89c40 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
89c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
89c70 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
89c80 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 14.**.** The a
89c90 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
89ca0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
89cb0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
89cc0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
89cd0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
89ce0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
89cf0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
89d00 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
89d10 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
89d20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
89d30 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
89d40 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
89d50 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
89d60 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
89d70 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
89d80 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
89d90 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
89da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89de0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
89df0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
89e00 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
89e10 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73  mplement mutexes
89e20 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ..**.** This fil
89e30 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
89e40 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61  that is common a
89e50 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65 78 20  cross all mutex 
89e60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e  implementations.
89e70 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74  ..**.** $Id: mut
89e80 65 78 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39  ex.c,v 1.30 2009
89e90 2f 30 32 2f 31 37 20 31 36 3a 32 39 3a 31 31 20  /02/17 16:29:11 
89ea0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
89eb0 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  $.*/..#ifndef SQ
89ec0 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a  LITE_MUTEX_OMIT.
89ed0 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
89ee0 20 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65   the mutex syste
89ef0 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  m..*/.SQLITE_PRI
89f00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
89f10 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b  MutexInit(void){
89f20 20 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c   .  int rc = SQL
89f30 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71  ITE_OK;.  if( sq
89f40 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
89f50 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a  g.bCoreMutex ){.
89f60 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
89f70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
89f80 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29  ex.xMutexAlloc )
89f90 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  {.      /* If th
89fa0 65 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65  e xMutexAlloc me
89fb0 74 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65  thod has not bee
89fc0 6e 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  n set, then the 
89fd0 75 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20  user did not.   
89fe0 20 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20     ** install a 
89ff0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
8a000 74 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65 33  tion via sqlite3
8a010 5f 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72 20  _config() prior 
8a020 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c  to .      ** sql
8a030 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
8a040 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e 20  ) being called. 
8a050 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65  This block copie
8a060 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20 20  s pointers to.  
8a070 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75      ** the defau
8a080 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  lt implementatio
8a090 6e 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74  n into the sqlit
8a0a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73  e3GlobalConfig s
8a0b0 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20  tructure..      
8a0c0 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20  **.      ** The 
8a0d0 64 61 6e 67 65 72 20 69 73 20 74 68 61 74 20 61  danger is that a
8a0e0 6c 74 68 6f 75 67 68 20 73 71 6c 69 74 65 33 5f  lthough sqlite3_
8a0f0 63 6f 6e 66 69 67 28 29 20 69 73 20 6e 6f 74 20  config() is not 
8a100 61 20 74 68 72 65 61 64 73 61 66 65 0a 20 20 20  a threadsafe.   
8a110 20 20 20 2a 2a 20 41 50 49 2c 20 73 71 6c 69 74     ** API, sqlit
8a120 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
8a130 69 73 2c 20 61 6e 64 20 73 6f 20 6d 75 6c 74 69  is, and so multi
8a140 70 6c 65 20 74 68 72 65 61 64 73 20 6d 61 79 20  ple threads may 
8a150 62 65 0a 20 20 20 20 20 20 2a 2a 20 61 74 74 65  be.      ** atte
8a160 6d 70 74 69 6e 67 20 74 6f 20 72 75 6e 20 74 68  mpting to run th
8a170 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 69 6d 75  is function simu
8a180 6c 74 61 6e 65 6f 75 73 6c 79 2e 20 54 6f 20 67  ltaneously. To g
8a190 75 61 72 64 20 77 72 69 74 65 0a 20 20 20 20 20  uard write.     
8a1a0 20 2a 2a 20 61 63 63 65 73 73 20 74 6f 20 74 68   ** access to th
8a1b0 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e sqlite3GlobalC
8a1c0 6f 6e 66 69 67 20 73 74 72 75 63 74 75 72 65 2c  onfig structure,
8a1d0 20 74 68 65 20 27 4d 41 53 54 45 52 27 20 73 74   the 'MASTER' st
8a1e0 61 74 69 63 20 6d 75 74 65 78 0a 20 20 20 20 20  atic mutex.     
8a1f0 20 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20   ** is obtained 
8a200 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67  before modifying
8a210 20 69 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   it..      */.  
8a220 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
8a230 78 5f 6d 65 74 68 6f 64 73 20 2a 70 20 3d 20 73  x_methods *p = s
8a240 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74  qlite3DefaultMut
8a250 65 78 28 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ex();.      sqli
8a260 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74  te3_mutex *pMast
8a270 65 72 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 20  er = 0;.  .     
8a280 20 72 63 20 3d 20 70 2d 3e 78 4d 75 74 65 78 49   rc = p->xMutexI
8a290 6e 69 74 28 29 3b 0a 20 20 20 20 20 20 69 66 28  nit();.      if(
8a2a0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
8a2b0 7b 0a 20 20 20 20 20 20 20 20 70 4d 61 73 74 65  {.        pMaste
8a2c0 72 20 3d 20 70 2d 3e 78 4d 75 74 65 78 41 6c 6c  r = p->xMutexAll
8a2d0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
8a2e0 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
8a2f0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70          assert(p
8a300 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20  Master);.       
8a310 20 70 2d 3e 78 4d 75 74 65 78 45 6e 74 65 72 28   p->xMutexEnter(
8a320 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  pMaster);.      
8a330 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
8a340 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
8a350 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 3d  tex.xMutexAlloc=
8a360 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  =0 .            
8a370 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   || sqlite3Globa
8a380 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
8a390 75 74 65 78 41 6c 6c 6f 63 3d 3d 70 2d 3e 78 4d  utexAlloc==p->xM
8a3a0 75 74 65 78 41 6c 6c 6f 63 0a 20 20 20 20 20 20  utexAlloc.      
8a3b0 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28    );.        if(
8a3c0 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   !sqlite3GlobalC
8a3d0 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74  onfig.mutex.xMut
8a3e0 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20  exAlloc ){.     
8a3f0 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
8a400 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 20 3d  alConfig.mutex =
8a410 20 2a 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   *p;.        }. 
8a420 20 20 20 20 20 20 20 70 2d 3e 78 4d 75 74 65 78         p->xMutex
8a430 4c 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a  Leave(pMaster);.
8a440 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
8a450 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
8a460 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
8a470 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 49 6e  g.mutex.xMutexIn
8a480 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  it();.    }.  }.
8a490 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
8a4a0 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20  ./*.** Shutdown 
8a4b0 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d  the mutex system
8a4c0 2e 20 54 68 69 73 20 63 61 6c 6c 20 66 72 65 65  . This call free
8a4d0 73 20 72 65 73 6f 75 72 63 65 73 20 61 6c 6c 6f  s resources allo
8a4e0 63 61 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69  cated by.** sqli
8a4f0 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 2e 0a  te3MutexInit()..
8a500 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
8a510 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74  E int sqlite3Mut
8a520 65 78 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69  exEnd(void){.  i
8a530 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
8a540 4b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  K;.  if( sqlite3
8a550 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
8a560 65 78 2e 78 4d 75 74 65 78 45 6e 64 20 29 7b 0a  ex.xMutexEnd ){.
8a570 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
8a580 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
8a590 65 78 2e 78 4d 75 74 65 78 45 6e 64 28 29 3b 0a  ex.xMutexEnd();.
8a5a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
8a5b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65  .}../*.** Retrie
8a5c0 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ve a pointer to 
8a5d0 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 6f  a static mutex o
8a5e0 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  r allocate a new
8a5f0 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a 2a 2f   dynamic one..*/
8a600 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
8a610 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74  te3_mutex *sqlit
8a620 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69  e3_mutex_alloc(i
8a630 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65 66 20  nt id){.#ifndef 
8a640 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
8a650 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74  INIT.  if( sqlit
8a660 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
8a670 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64  ) return 0;.#end
8a680 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  if.  return sqli
8a690 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
8a6a0 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f  mutex.xMutexAllo
8a6b0 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45  c(id);.}..SQLITE
8a6c0 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
8a6d0 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d  _mutex *sqlite3M
8a6e0 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64  utexAlloc(int id
8a6f0 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  ){.  if( !sqlite
8a700 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
8a710 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  oreMutex ){.    
8a720 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
8a730 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c  return sqlite3Gl
8a740 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
8a750 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29  .xMutexAlloc(id)
8a760 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
8a770 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e  a dynamic mutex.
8a780 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
8a790 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  oid sqlite3_mute
8a7a0 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  x_free(sqlite3_m
8a7b0 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20  utex *p){.  if( 
8a7c0 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  p ){.    sqlite3
8a7d0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
8a7e0 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29  ex.xMutexFree(p)
8a7f0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ;.  }.}../*.** O
8a800 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20  btain the mutex 
8a810 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72  p. If some other
8a820 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20   thread already 
8a830 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62  has the mutex, b
8a840 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74  lock.** until it
8a850 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64   can be obtained
8a860 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
8a870 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
8a880 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
8a890 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66  _mutex *p){.  if
8a8a0 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
8a8b0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
8a8c0 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72  utex.xMutexEnter
8a8d0 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
8a8e0 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74  * Obtain the mut
8a8f0 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73  ex p. If success
8a900 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ful, return SQLI
8a910 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65  TE_OK. Otherwise
8a920 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20  , if another.** 
8a930 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65  thread holds the
8a940 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61   mutex and it ca
8a950 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
8a960 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
8a970 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  BUSY..*/.SQLITE_
8a980 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
8a990 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65  mutex_try(sqlite
8a9a0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
8a9b0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
8a9c0 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  K;.  if( p ){.  
8a9d0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
8a9e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
8a9f0 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b  ex.xMutexTry(p);
8aa00 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
8aa10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
8aa20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
8aa30 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69  ve() routine exi
8aa40 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20  ts a mutex that 
8aa50 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  was previously.*
8aa60 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65  * entered by the
8aa70 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54   same thread.  T
8aa80 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
8aa90 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20  ndefined if the 
8aaa0 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74  mutex .** is not
8aab0 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
8aac0 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f  ed. If a NULL po
8aad0 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20  inter is passed 
8aae0 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a  as an argument.*
8aaf0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
8ab00 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  is a no-op..*/.S
8ab10 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
8ab20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
8ab30 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
8ab40 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
8ab50 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
8ab60 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
8ab70 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20  MutexLeave(p);. 
8ab80 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44   }.}..#ifndef ND
8ab90 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  EBUG./*.** The s
8aba0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
8abb0 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
8abc0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
8abd0 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
8abe0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
8abf0 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
8ac00 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53  statements..*/.S
8ac10 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
8ac20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
8ac30 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
8ac40 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d  p){.  return p==
8ac50 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62  0 || sqlite3Glob
8ac60 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
8ac70 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a  MutexHeld(p);.}.
8ac80 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
8ac90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
8aca0 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
8acb0 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ex *p){.  return
8acc0 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33   p==0 || sqlite3
8acd0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
8ace0 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64  ex.xMutexNotheld
8acf0 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  (p);.}.#endif..#
8ad00 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
8ad10 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a 0a 2f  OMIT_MUTEX */../
8ad20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
8ad30 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a  nd of mutex.c **
8ad40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ad50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ad60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
8ad70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
8ad80 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f  egin file mutex_
8ad90 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  noop.c *********
8ada0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8adb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
8adc0 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65  *.** 2008 Octobe
8add0 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 07.**.** The a
8ade0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
8adf0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
8ae00 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
8ae10 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
8ae20 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
8ae30 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
8ae40 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
8ae50 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
8ae60 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
8ae70 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
8ae80 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
8ae90 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
8aea0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
8aeb0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
8aec0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
8aed0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
8aee0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
8aef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8af00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8af10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8af20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8af30 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
8af40 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
8af50 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
8af60 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73  mplement mutexes
8af70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70  ..**.** This imp
8af80 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74  lementation in t
8af90 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f  his file does no
8afa0 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75  t provide any mu
8afb0 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f  tual.** exclusio
8afc0 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75  n and is thus su
8afd0 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f  itable for use o
8afe0 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69  nly in applicati
8aff0 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20  ons.** that use 
8b000 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67  SQLite in a sing
8b010 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  le thread.  The 
8b020 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64  routines defined
8b030 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61  .** here are pla
8b040 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70  ce-holders.  App
8b050 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75  lications can su
8b060 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67  bstitute working
8b070 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e  .** mutex routin
8b080 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65  es at start-time
8b090 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a   using the.**.**
8b0a0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e       sqlite3_con
8b0b0 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49  fig(SQLITE_CONFI
8b0c0 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a  G_MUTEX,...).**.
8b0d0 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  ** interface..**
8b0e0 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20  .** If compiled 
8b0f0 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55  with SQLITE_DEBU
8b100 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e  G, then addition
8b110 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65  al logic is inse
8b120 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65  rted.** that doe
8b130 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67  s error checking
8b140 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d   on mutexes to m
8b150 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72  ake sure they ar
8b160 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65  e being.** calle
8b170 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a  d correctly..**.
8b180 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e 6f  ** $Id: mutex_no
8b190 6f 70 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f  op.c,v 1.3 2008/
8b1a0 31 32 2f 30 35 20 31 37 3a 31 37 3a 30 38 20 64  12/05 17:17:08 d
8b1b0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69  rh Exp $.*/...#i
8b1c0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
8b1d0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20  _MUTEX_NOOP) && 
8b1e0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
8b1f0 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75  DEBUG)./*.** Stu
8b200 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61  b routines for a
8b210 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73  ll mutex methods
8b220 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
8b230 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f  tines provide no
8b240 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f   mutual exclusio
8b250 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b  n or error check
8b260 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
8b270 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64  nt noopMutexHeld
8b280 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
8b290 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a  p){ return 1; }.
8b2a0 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d  static int noopM
8b2b0 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
8b2c0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72  te3_mutex *p){ r
8b2d0 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69  eturn 1; }.stati
8b2e0 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49  c int noopMutexI
8b2f0 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72  nit(void){ retur
8b300 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73  n SQLITE_OK; }.s
8b310 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75  tatic int noopMu
8b320 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65  texEnd(void){ re
8b330 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
8b340 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  }.static sqlite3
8b350 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65  _mutex *noopMute
8b360 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20  xAlloc(int id){ 
8b370 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f  return (sqlite3_
8b380 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74  mutex*)8; }.stat
8b390 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65  ic void noopMute
8b3a0 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  xFree(sqlite3_mu
8b3b0 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b  tex *p){ return;
8b3c0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e   }.static void n
8b3d0 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71  oopMutexEnter(sq
8b3e0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
8b3f0 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69   return; }.stati
8b400 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54  c int noopMutexT
8b410 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ry(sqlite3_mutex
8b420 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c   *p){ return SQL
8b430 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
8b440 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c   void noopMutexL
8b450 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
8b460 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20  ex *p){ return; 
8b470 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
8b480 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  E sqlite3_mutex_
8b490 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
8b4a0 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69  DefaultMutex(voi
8b4b0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c  d){.  static sql
8b4c0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
8b4d0 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20  ds sMutex = {.  
8b4e0 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c    noopMutexInit,
8b4f0 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e  .    noopMutexEn
8b500 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78  d,.    noopMutex
8b510 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d  Alloc,.    noopM
8b520 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f  utexFree,.    no
8b530 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20  opMutexEnter,.  
8b540 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a    noopMutexTry,.
8b550 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61      noopMutexLea
8b560 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  ve,..    noopMut
8b570 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70  exHeld,.    noop
8b580 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d  MutexNotheld.  }
8b590 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75  ;..  return &sMu
8b5a0 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  tex;.}.#endif /*
8b5b0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
8b5c0 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21  MUTEX_NOOP) && !
8b5d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
8b5e0 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65  EBUG) */..#if de
8b5f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54  fined(SQLITE_MUT
8b600 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69  EX_NOOP) && defi
8b610 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
8b620 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20  )./*.** In this 
8b630 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
8b640 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69  error checking i
8b650 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74  s provided for t
8b660 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65  esting.** and de
8b670 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73  bugging purposes
8b680 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73  .  The mutexes s
8b690 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76  till do not prov
8b6a0 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61  ide any.** mutua
8b6b0 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a  l exclusion..*/.
8b6c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78  ./*.** The mutex
8b6d0 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63   object.*/.struc
8b6e0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  t sqlite3_mutex 
8b6f0 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20  {.  int id;     
8b700 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70  /* The mutex typ
8b710 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20  e */.  int cnt; 
8b720 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
8b730 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20  entries without 
8b740 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65  a matching leave
8b750 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
8b760 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
8b770 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74  held() and sqlit
8b780 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
8b790 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a  () routine are.*
8b7a0 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  * intended for u
8b7b0 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
8b7c0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
8b7d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62  /.static int deb
8b7e0 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69  ugMutexHeld(sqli
8b7f0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
8b800 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
8b810 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74  p->cnt>0;.}.stat
8b820 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
8b830 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
8b840 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65  _mutex *p){.  re
8b850 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e  turn p==0 || p->
8b860 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  cnt==0;.}../*.**
8b870 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
8b880 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  deinitialize the
8b890 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
8b8a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8b8b0 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76  debugMutexInit(v
8b8c0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
8b8d0 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
8b8e0 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45   int debugMutexE
8b8f0 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  nd(void){ return
8b900 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f   SQLITE_OK; }../
8b910 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
8b920 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
8b930 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
8b940 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
8b950 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
8b960 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
8b970 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
8b980 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
8b990 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
8b9a0 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
8b9b0 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ed. .*/.static s
8b9c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65  qlite3_mutex *de
8b9d0 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  bugMutexAlloc(in
8b9e0 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  t id){.  static 
8b9f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53  sqlite3_mutex aS
8ba00 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69  tatic[6];.  sqli
8ba10 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20  te3_mutex *pNew 
8ba20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69  = 0;.  switch( i
8ba30 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  d ){.    case SQ
8ba40 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a  LITE_MUTEX_FAST:
8ba50 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
8ba60 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
8ba70 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d  : {.      pNew =
8ba80 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73   sqlite3Malloc(s
8ba90 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20  izeof(*pNew));. 
8baa0 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b       if( pNew ){
8bab0 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  .        pNew->i
8bac0 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20  d = id;.        
8bad0 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20  pNew->cnt = 0;. 
8bae0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
8baf0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
8bb00 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61  fault: {.      a
8bb10 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30  ssert( id-2 >= 0
8bb20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
8bb30 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73  ( id-2 < (int)(s
8bb40 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73  izeof(aStatic)/s
8bb50 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d  izeof(aStatic[0]
8bb60 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77  )) );.      pNew
8bb70 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32   = &aStatic[id-2
8bb80 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  ];.      pNew->i
8bb90 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72  d = id;.      br
8bba0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
8bbb0 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
8bbc0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
8bbd0 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
8bbe0 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  a previously all
8bbf0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f  ocated mutex..*/
8bc00 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62  .static void deb
8bc10 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69  ugMutexFree(sqli
8bc20 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
8bc30 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d   assert( p->cnt=
8bc40 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
8bc50 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
8bc60 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69  TEX_FAST || p->i
8bc70 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
8bc80 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73  RECURSIVE );.  s
8bc90 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
8bca0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
8bcb0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
8bcc0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
8bcd0 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69  utex_try() routi
8bce0 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  nes attempt.** t
8bcf0 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e  o enter a mutex.
8bd00 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72    If another thr
8bd10 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
8bd20 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
8bd30 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
8bd40 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
8bd50 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
8bd60 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
8bd70 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
8bd80 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71  TE_BUSY.  The sq
8bd90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
8bda0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
8bdb0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  rns SQLITE_OK.**
8bdc0 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c   upon successful
8bdd0 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73   entry.  Mutexes
8bde0 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53   created using S
8bdf0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
8be00 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20  RSIVE can.** be 
8be10 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  entered multiple
8be20 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61   times by the sa
8be30 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73  me thread.  In s
8be40 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
8be50 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
8be60 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
8be70 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
8be80 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
8be90 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
8bea0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65  er.  If the same
8beb0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f   thread tries to
8bec0 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72   enter any other
8bed0 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a   kind of mutex.*
8bee0 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
8bef0 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
8bf00 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
8bf10 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75  static void debu
8bf20 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69  gMutexEnter(sqli
8bf30 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
8bf40 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
8bf50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
8bf60 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d  URSIVE || debugM
8bf70 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
8bf80 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a  ;.  p->cnt++;.}.
8bf90 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67  static int debug
8bfa0 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33  MutexTry(sqlite3
8bfb0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
8bfc0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
8bfd0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
8bfe0 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65  IVE || debugMute
8bff0 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20  xNotheld(p) );. 
8c000 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74   p->cnt++;.  ret
8c010 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
8c020 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
8c030 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
8c040 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20  ) routine exits 
8c050 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73  a mutex that was
8c060 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65  .** previously e
8c070 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61  ntered by the sa
8c080 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  me thread.  The 
8c090 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75  behavior.** is u
8c0a0 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20  ndefined if the 
8c0b0 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72  mutex is not cur
8c0c0 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f  rently entered o
8c0d0 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72  r.** is not curr
8c0e0 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e  ently allocated.
8c0f0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65    SQLite will ne
8c100 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a  ver do either..*
8c110 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65  /.static void de
8c120 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71  bugMutexLeave(sq
8c130 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
8c140 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67  .  assert( debug
8c150 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a  MutexHeld(p) );.
8c160 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73    p->cnt--;.  as
8c170 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
8c180 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
8c190 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65  IVE || debugMute
8c1a0 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d  xNotheld(p) );.}
8c1b0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
8c1c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
8c1d0 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44  ethods *sqlite3D
8c1e0 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64  efaultMutex(void
8c1f0 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
8c200 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
8c210 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20  s sMutex = {.   
8c220 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c   debugMutexInit,
8c230 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45  .    debugMutexE
8c240 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74  nd,.    debugMut
8c250 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62  exAlloc,.    deb
8c260 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20  ugMutexFree,.   
8c270 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72   debugMutexEnter
8c280 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78  ,.    debugMutex
8c290 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75  Try,.    debugMu
8c2a0 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64  texLeave,..    d
8c2b0 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20  ebugMutexHeld,. 
8c2c0 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74     debugMutexNot
8c2d0 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74  held.  };..  ret
8c2e0 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23  urn &sMutex;.}.#
8c2f0 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
8c300 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f  (SQLITE_MUTEX_NO
8c310 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53  OP) && defined(S
8c320 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a  QLITE_DEBUG) */.
8c330 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
8c340 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f   End of mutex_no
8c350 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  op.c ***********
8c360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
8c380 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
8c390 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65   Begin file mute
8c3a0 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  x_os2.c ********
8c3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
8c3d0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
8c3e0 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 28.**.** The 
8c3f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
8c400 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
8c410 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
8c420 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
8c430 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
8c440 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
8c450 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
8c460 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
8c470 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
8c480 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
8c490 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
8c4a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
8c4b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
8c4c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
8c4d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
8c4e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
8c4f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
8c500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c540 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
8c550 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43  e contains the C
8c560 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
8c570 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65  implement mutexe
8c580 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a  s for OS/2.**.**
8c590 20 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73 32 2e   $Id: mutex_os2.
8c5a0 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 31 31  c,v 1.11 2008/11
8c5b0 2f 32 32 20 31 39 3a 35 30 3a 35 34 20 70 77 65  /22 19:50:54 pwe
8c5c0 69 6c 62 61 63 68 65 72 20 45 78 70 20 24 0a 2a  ilbacher Exp $.*
8c5d0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  /../*.** The cod
8c5e0 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
8c5f0 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 53  s only used if S
8c600 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20  QLITE_MUTEX_OS2 
8c610 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53  is defined..** S
8c620 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20 66  ee the mutex.h f
8c630 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  ile for details.
8c640 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
8c650 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a  E_MUTEX_OS2../**
8c660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c670 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78 20  **** OS/2 Mutex 
8c680 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  Implementation *
8c690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c6a0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
8c6b0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
8c6c0 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20 62 75  of mutexes is bu
8c6d0 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f 53  ilt using the OS
8c6e0 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  /2 API..*/../*.*
8c6f0 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65  * The mutex obje
8c700 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72  ct.** Each recur
8c710 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e  sive mutex is an
8c720 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
8c730 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
8c740 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ture..*/.struct 
8c750 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a  sqlite3_mutex {.
8c760 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 20    HMTX mutex;   
8c770 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e      /* Mutex con
8c780 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63  trolling the loc
8c790 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b 20  k */.  int  id; 
8c7a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65           /* Mute
8c7b0 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20  x type */.  int 
8c7c0 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f 2a   nRef;        /*
8c7d0 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   Number of refer
8c7e0 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20 20  ences */.  TID  
8c7f0 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a 20  owner;       /* 
8c800 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74  Thread holding t
8c810 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a  his mutex */.};.
8c820 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55 54  .#define OS2_MUT
8c830 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 20  EX_INITIALIZER  
8c840 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20   0,0,0,0../*.** 
8c850 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64  Initialize and d
8c860 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
8c870 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e  mutex subsystem.
8c880 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
8c890 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64  s2MutexInit(void
8c8a0 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ){ return SQLITE
8c8b0 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  _OK; }.static in
8c8c0 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76 6f  t os2MutexEnd(vo
8c8d0 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
8c8e0 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK; }../*.** 
8c8f0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
8c900 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  x_alloc() routin
8c910 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
8c920 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72  w.** mutex and r
8c930 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
8c940 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72   to it.  If it r
8c950 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74  eturns NULL.** t
8c960 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61  hat means that a
8c970 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74   mutex could not
8c980 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a   be allocated. .
8c990 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  ** SQLite will u
8c9a0 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20  nwind its stack 
8c9b0 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72  and return an er
8c9c0 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65  ror.  The argume
8c9d0 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  nt.** to sqlite3
8c9e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69  _mutex_alloc() i
8c9f0 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69  s one of these i
8ca00 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
8ca10 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
8ca20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
8ca30 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20  EX_FAST         
8ca40 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20        0.** <li> 
8ca50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
8ca60 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20  CURSIVE         
8ca70 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49   1.** <li>  SQLI
8ca80 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
8ca90 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a  MASTER      2.**
8caa0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
8cab0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20  TEX_STATIC_MEM  
8cac0 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e         3.** <li>
8cad0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
8cae0 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20  TATIC_PRNG      
8caf0 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a    4.** </ul>.**.
8cb00 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f  ** The first two
8cb10 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65   constants cause
8cb20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
8cb30 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65  lloc() to create
8cb40 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e  .** a new mutex.
8cb50 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20    The new mutex 
8cb60 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65  is recursive whe
8cb70 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
8cb80 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75  ECURSIVE.** is u
8cb90 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65  sed but not nece
8cba0 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20  ssarily so when 
8cbb0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
8cbc0 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68  T is used..** Th
8cbd0 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
8cbe0 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
8cbf0 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
8cc00 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65  istinction.** be
8cc10 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54  tween SQLITE_MUT
8cc20 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64  EX_RECURSIVE and
8cc30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
8cc40 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a  ST if it does.**
8cc50 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42   not want to.  B
8cc60 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  ut SQLite will o
8cc70 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65  nly request a re
8cc80 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e  cursive mutex in
8cc90 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20  .** cases where 
8cca0 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20  it really needs 
8ccb0 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65  one.  If a faste
8ccc0 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20  r non-recursive 
8ccd0 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65  mutex.** impleme
8cce0 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  ntation is avail
8ccf0 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74  able on the host
8cd00 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d   platform, the m
8cd10 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a  utex subsystem.*
8cd20 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73  * might return s
8cd30 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72  uch a mutex in r
8cd40 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54  esponse to SQLIT
8cd50 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a  E_MUTEX_FAST..**
8cd60 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c  .** The other al
8cd70 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73  lowed parameters
8cd80 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
8cd90 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72  x_alloc() each r
8cda0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
8cdb0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70  er to a static p
8cdc0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78  reexisting mutex
8cdd0 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20  .  Three static 
8cde0 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75  mutexes are.** u
8cdf0 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65  sed by the curre
8ce00 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  nt version of SQ
8ce10 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65  Lite.  Future ve
8ce20 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
8ce30 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69  .** may add addi
8ce40 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75  tional static mu
8ce50 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d  texes.  Static m
8ce60 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69  utexes are for i
8ce70 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62  nternal.** use b
8ce80 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20  y SQLite only.  
8ce90 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  Applications tha
8cea0 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74  t use SQLite mut
8ceb0 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75  exes should.** u
8cec0 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61  se only the dyna
8ced0 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75  mic mutexes retu
8cee0 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d  rned by SQLITE_M
8cef0 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20  UTEX_FAST or.** 
8cf00 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
8cf10 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  URSIVE..**.** No
8cf20 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f  te that if one o
8cf30 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  f the dynamic mu
8cf40 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28  tex parameters (
8cf50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
8cf60 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d  T.** or SQLITE_M
8cf70 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20  UTEX_RECURSIVE) 
8cf80 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c  is used then sql
8cf90 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
8cfa0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
8cfb0 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20  different mutex 
8cfc0 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20  on every call.  
8cfd0 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74  But for the stat
8cfe0 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65  ic.** mutex type
8cff0 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65  s, the same mute
8d000 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  x is returned on
8d010 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74   every call that
8d020 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65   has.** the same
8d030 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f   type number..*/
8d040 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
8d050 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78 41  mutex *os2MutexA
8d060 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b  lloc(int iType){
8d070 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
8d080 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77   *p = NULL;.  sw
8d090 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20  itch( iType ){. 
8d0a0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
8d0b0 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63  UTEX_FAST:.    c
8d0c0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
8d0d0 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
8d0e0 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
8d0f0 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
8d100 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
8d110 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
8d120 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
8d130 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 72         if( DosCr
8d140 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c  eateMutexSem( 0,
8d150 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46   &p->mutex, 0, F
8d160 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52  ALSE ) != NO_ERR
8d170 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OR ){.          
8d180 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20  sqlite3_free( p 
8d190 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d  );.          p =
8d1a0 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 7d   NULL;.        }
8d1b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
8d1c0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
8d1d0 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
8d1e0 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65   static volatile
8d1f0 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b   int isInit = 0;
8d200 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71  .      static sq
8d210 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74  lite3_mutex stat
8d220 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a  icMutexes[] = {.
8d230 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
8d240 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
8d250 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
8d260 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
8d270 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20  ZER, },.        
8d280 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54  { OS2_MUTEX_INIT
8d290 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20  IALIZER, },.    
8d2a0 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f      { OS2_MUTEX_
8d2b0 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a  INITIALIZER, },.
8d2c0 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
8d2d0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
8d2e0 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
8d2f0 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
8d300 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d 3b  ZER, },.      };
8d310 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 73 49  .      if ( !isI
8d320 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 41  nit ){.        A
8d330 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 20  PIRET rc;.      
8d340 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20    PTIB ptib;.   
8d350 20 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a       PPIB ppib;.
8d360 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 74          HMTX mut
8d370 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  ex;.        char
8d380 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 20   name[32];.     
8d390 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f     DosGetInfoBlo
8d3a0 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69  cks( &ptib, &ppi
8d3b0 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  b );.        sql
8d3c0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73  ite3_snprintf( s
8d3d0 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d  izeof(name), nam
8d3e0 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c  e, "\\SEM32\\SQL
8d3f0 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 20  ITE%04x",.      
8d400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8d410 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 6c      ppib->pib_ul
8d420 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 77  pid );.        w
8d430 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b  hile( !isInit ){
8d440 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78  .          mutex
8d450 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
8d460 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d 75  rc = DosCreateMu
8d470 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 6d  texSem( name, &m
8d480 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 3b  utex, 0, FALSE);
8d490 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
8d4a0 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  c == NO_ERROR ){
8d4b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73  .            uns
8d4c0 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20  igned int i;.   
8d4d0 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73           if( !is
8d4e0 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
8d4f0 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30        for( i = 0
8d500 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61  ; i < sizeof(sta
8d510 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65  ticMutexes)/size
8d520 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73  of(staticMutexes
8d530 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  [0]); i++ ){.   
8d540 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73               Dos
8d550 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20  CreateMutexSem( 
8d560 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65  0, &staticMutexe
8d570 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46  s[i].mutex, 0, F
8d580 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20  ALSE );.        
8d590 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
8d5a0 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31        isInit = 1
8d5b0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
8d5c0 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43              DosC
8d5d0 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75  loseMutexSem( mu
8d5e0 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20  tex );.         
8d5f0 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d   }else if( rc ==
8d600 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45   ERROR_DUPLICATE
8d610 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20  _NAME ){.       
8d620 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31       DosSleep( 1
8d630 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65   );.          }e
8d640 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
8d650 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20   return p;.     
8d660 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
8d670 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
8d680 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e  ssert( iType-2 >
8d690 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  = 0 );.      ass
8d6a0 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73  ert( iType-2 < s
8d6b0 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65  izeof(staticMute
8d6c0 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74  xes)/sizeof(stat
8d6d0 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b  icMutexes[0]) );
8d6e0 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74  .      p = &stat
8d6f0 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d  icMutexes[iType-
8d700 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20  2];.      p->id 
8d710 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62  = iType;.      b
8d720 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
8d730 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a    return p;.}...
8d740 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
8d750 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61  ne deallocates a
8d760 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
8d770 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20  cated mutex..** 
8d780 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75  SQLite is carefu
8d790 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  l to deallocate 
8d7a0 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74  every mutex that
8d7b0 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a   it allocates..*
8d7c0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  /.static void os
8d7d0 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74  2MutexFree(sqlit
8d7e0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
8d7f0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
8d800 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  n;.  assert( p->
8d810 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73  nRef==0 );.  ass
8d820 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
8d830 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c  TE_MUTEX_FAST ||
8d840 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
8d850 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29  UTEX_RECURSIVE )
8d860 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65  ;.  DosCloseMute
8d870 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29  xSem( p->mutex )
8d880 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
8d890 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ( p );.}../*.** 
8d8a0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
8d8b0 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
8d8c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
8d8d0 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
8d8e0 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61  pt.** to enter a
8d8f0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74   mutex.  If anot
8d900 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c  her thread is al
8d910 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65  ready within the
8d920 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74   mutex,.** sqlit
8d930 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
8d940 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20   will block and 
8d950 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
8d960 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  y() will return.
8d970 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  ** SQLITE_BUSY. 
8d980 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
8d990 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61  ex_try() interfa
8d9a0 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ce returns SQLIT
8d9b0 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63  E_OK.** upon suc
8d9c0 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20  cessful entry.  
8d9d0 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20  Mutexes created 
8d9e0 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54  using SQLITE_MUT
8d9f0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
8da00 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d  .** be entered m
8da10 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
8da20 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
8da30 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73  .  In such cases
8da40 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d   the,.** mutex m
8da50 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e  ust be exited an
8da60 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66   equal number of
8da70 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e   times before an
8da80 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20  other thread.** 
8da90 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74  can enter.  If t
8daa0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74  he same thread t
8dab0 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  ries to enter an
8dac0 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20  y other kind of 
8dad0 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68  mutex.** more th
8dae0 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68  an once, the beh
8daf0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
8db00 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
8db10 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72  id os2MutexEnter
8db20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
8db30 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20  p){.  TID tid;. 
8db40 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20   PID holder1;.  
8db50 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20  ULONG holder2;. 
8db60 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
8db70 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rn;.  assert( p-
8db80 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
8db90 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f  X_RECURSIVE || o
8dba0 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70  s2MutexNotheld(p
8dbb0 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73  ) );.  DosReques
8dbc0 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74  tMutexSem(p->mut
8dbd0 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49  ex, SEM_INDEFINI
8dbe0 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51  TE_WAIT);.  DosQ
8dbf0 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e  ueryMutexSem(p->
8dc00 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c  mutex, &holder1,
8dc10 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29   &tid, &holder2)
8dc20 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74  ;.  p->owner = t
8dc30 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b  id;.  p->nRef++;
8dc40 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  .}.static int os
8dc50 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65  2MutexTry(sqlite
8dc60 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
8dc70 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64  nt rc;.  TID tid
8dc80 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b  ;.  PID holder1;
8dc90 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32  .  ULONG holder2
8dca0 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
8dcb0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
8dcc0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
8dcd0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
8dce0 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d  ECURSIVE || os2M
8dcf0 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
8dd00 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65  ;.  if( DosReque
8dd10 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75  stMutexSem(p->mu
8dd20 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41  tex, SEM_IMMEDIA
8dd30 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f  TE_RETURN) == NO
8dd40 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f  _ERROR) {.    Do
8dd50 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
8dd60 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72  ->mutex, &holder
8dd70 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72  1, &tid, &holder
8dd80 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72  2);.    p->owner
8dd90 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e   = tid;.    p->n
8dda0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20  Ref++;.    rc = 
8ddb0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65  SQLITE_OK;.  } e
8ddc0 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53  lse {.    rc = S
8ddd0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
8dde0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
8ddf0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
8de00 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
8de10 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
8de20 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
8de30 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
8de40 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
8de50 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
8de60 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e  ehavior.** is un
8de70 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d  defined if the m
8de80 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72  utex is not curr
8de90 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72  ently entered or
8dea0 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
8deb0 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ntly allocated. 
8dec0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
8ded0 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f  er do either..*/
8dee0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32  .static void os2
8def0 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74  MutexLeave(sqlit
8df00 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
8df10 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68  TID tid;.  PID h
8df20 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20  older1;.  ULONG 
8df30 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70  holder2;.  if( p
8df40 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
8df50 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
8df60 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d  0 );.  DosQueryM
8df70 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78  utexSem(p->mutex
8df80 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64  , &holder1, &tid
8df90 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61  , &holder2);.  a
8dfa0 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d  ssert( p->owner=
8dfb0 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65  =tid );.  p->nRe
8dfc0 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  f--;.  assert( p
8dfd0 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
8dfe0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
8dff0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
8e000 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53  DosReleaseMutexS
8e010 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a  em(p->mutex);.}.
8e020 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
8e030 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  EBUG./*.** The s
8e040 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
8e050 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
8e060 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
8e070 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
8e080 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
8e090 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
8e0a0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  statements..*/.s
8e0b0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74  tatic int os2Mut
8e0c0 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  exHeld(sqlite3_m
8e0d0 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20  utex *p){.  TID 
8e0e0 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a  tid;.  PID pid;.
8e0f0 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b    ULONG ulCount;
8e100 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20  .  PTIB ptib;.  
8e110 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 20  if( p!=0 ) {.   
8e120 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65   DosQueryMutexSe
8e130 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64  m(p->mutex, &pid
8e140 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74  , &tid, &ulCount
8e150 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  );.  } else {.  
8e160 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63    DosGetInfoBloc
8e170 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b  ks(&ptib, NULL);
8e180 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d  .    tid = ptib-
8e190 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32  >tib_ptib2->tib2
8e1a0 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65  _ultid;.  }.  re
8e1b0 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d  turn p==0 || (p-
8e1c0 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f  >nRef!=0 && p->o
8e1d0 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74  wner==tid);.}.st
8e1e0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
8e1f0 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
8e200 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49  _mutex *p){.  TI
8e210 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64  D tid;.  PID pid
8e220 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e  ;.  ULONG ulCoun
8e230 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a  t;.  PTIB ptib;.
8e240 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b 0a    if( p!= 0 ) {.
8e250 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65      DosQueryMute
8e260 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26  xSem(p->mutex, &
8e270 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f  pid, &tid, &ulCo
8e280 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  unt);.  } else {
8e290 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42  .    DosGetInfoB
8e2a0 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c  locks(&ptib, NUL
8e2b0 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74  L);.    tid = pt
8e2c0 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74  ib->tib_ptib2->t
8e2d0 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20  ib2_ultid;.  }. 
8e2e0 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
8e2f0 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d  p->nRef==0 || p-
8e300 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a 23  >owner!=tid;.}.#
8e310 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
8e320 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
8e330 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  tex_methods *sql
8e340 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78  ite3DefaultMutex
8e350 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
8e360 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
8e370 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20  ethods sMutex = 
8e380 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e  {.    os2MutexIn
8e390 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  it,.    os2Mutex
8e3a0 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65  End,.    os2Mute
8e3b0 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d  xAlloc,.    os2M
8e3c0 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73  utexFree,.    os
8e3d0 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20  2MutexEnter,.   
8e3e0 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20   os2MutexTry,.  
8e3f0 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c    os2MutexLeave,
8e400 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
8e410 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65  EBUG.    os2Mute
8e420 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75  xHeld,.    os2Mu
8e430 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69  texNotheld.#endi
8e440 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e  f.  };..  return
8e450 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64   &sMutex;.}.#end
8e460 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
8e470 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EX_OS2 */../****
8e480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
8e490 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a  f mutex_os2.c **
8e4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
8e4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
8e4e0 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78   file mutex_unix
8e4f0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
8e500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
8e520 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a   2007 August 28.
8e530 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
8e540 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
8e550 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
8e560 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
8e570 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
8e580 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
8e590 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
8e5a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
8e5b0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
8e5c0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
8e5d0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
8e5e0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
8e5f0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
8e600 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
8e610 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
8e620 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
8e630 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
8e640 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
8e650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
8e690 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
8e6a0 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
8e6b0 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
8e6c0 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20  ent mutexes for 
8e6d0 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24  pthreads.**.** $
8e6e0 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63  Id: mutex_unix.c
8e6f0 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31 32 2f  ,v 1.16 2008/12/
8e700 30 38 20 31 38 3a 31 39 3a 31 38 20 64 72 68 20  08 18:19:18 drh 
8e710 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
8e720 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
8e730 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73   file is only us
8e740 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d  ed if we are com
8e750 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66  piling threadsaf
8e760 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20  e.** under unix 
8e770 77 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a  with pthreads..*
8e780 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
8e790 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
8e7a0 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65  on requires a ve
8e7b0 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64  rsion of pthread
8e7c0 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72  s that.** suppor
8e7d0 74 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ts recursive mut
8e7e0 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  exes..*/.#ifdef 
8e7f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48  SQLITE_MUTEX_PTH
8e800 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20  READS..#include 
8e810 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a  <pthread.h>.../*
8e820 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69  .** Each recursi
8e830 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69  ve mutex is an i
8e840 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
8e850 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
8e860 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  re..*/.struct sq
8e870 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20  lite3_mutex {.  
8e880 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20  pthread_mutex_t 
8e890 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75  mutex;     /* Mu
8e8a0 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20  tex controlling 
8e8b0 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  the lock */.  in
8e8c0 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  t id;           
8e8d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65           /* Mute
8e8e0 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20  x type */.  int 
8e8f0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
8e900 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
8e910 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f   of entrances */
8e920 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e  .  pthread_t own
8e930 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  er;           /*
8e940 20 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20   Thread that is 
8e950 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65  within this mute
8e960 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  x */.#ifdef SQLI
8e970 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74  TE_DEBUG.  int t
8e980 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20  race;           
8e990 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
8e9a0 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a   trace changes *
8e9b0 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64  /.#endif.};.#ifd
8e9c0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
8e9d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f  #define SQLITE3_
8e9e0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
8e9f0 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45  R { PTHREAD_MUTE
8ea00 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30  X_INITIALIZER, 0
8ea10 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29  , 0, (pthread_t)
8ea20 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65  0, 0 }.#else.#de
8ea30 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54  fine SQLITE3_MUT
8ea40 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b  EX_INITIALIZER {
8ea50 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49   PTHREAD_MUTEX_I
8ea60 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30  NITIALIZER, 0, 0
8ea70 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d  , (pthread_t)0 }
8ea80 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
8ea90 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
8eaa0 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69  _held() and sqli
8eab0 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
8eac0 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a  d() routine are.
8ead0 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  ** intended for 
8eae0 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20  use only inside 
8eaf0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
8eb00 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c  nts.  On some pl
8eb10 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72  atforms,.** ther
8eb20 65 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20  e might be race 
8eb30 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20  conditions that 
8eb40 63 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20  can cause these 
8eb50 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64  routines to.** d
8eb60 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74  eliver incorrect
8eb70 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61   results.  In pa
8eb80 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68  rticular, if pth
8eb90 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a  read_equal() is.
8eba0 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63  ** not an atomic
8ebb0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e   operation, then
8ebc0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
8ebd0 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a  might delivery.*
8ebe0 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75  * incorrect resu
8ebf0 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c  lts.  On most pl
8ec00 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64  atforms, pthread
8ec10 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a  _equal() is a .*
8ec20 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20  * comparison of 
8ec30 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64  two integers and
8ec40 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74   is therefore at
8ec50 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72  omic.  But we ar
8ec60 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48  e.** told that H
8ec70 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20  PUX is not such 
8ec80 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20  a platform.  If 
8ec90 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72  so, then these r
8eca0 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20  outines.** will 
8ecb0 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20  not always work 
8ecc0 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55  correctly on HPU
8ecd0 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73  X..**.** On thos
8ece0 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72  e platforms wher
8ecf0 65 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  e pthread_equal(
8ed00 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c  ) is not atomic,
8ed10 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c   SQLite.** shoul
8ed20 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69  d be compiled wi
8ed30 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44  thout -DSQLITE_D
8ed40 45 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44  EBUG and with -D
8ed50 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b  NDEBUG to.** mak
8ed60 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74  e sure no assert
8ed70 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  () statements ar
8ed80 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  e evaluated and 
8ed90 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72  hence these.** r
8eda0 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65  outines are neve
8edb0 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66  r called..*/.#if
8edc0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
8edd0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
8ede0 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69  ITE_DEBUG).stati
8edf0 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74  c int pthreadMut
8ee00 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  exHeld(sqlite3_m
8ee10 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
8ee20 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26  rn (p->nRef!=0 &
8ee30 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  & pthread_equal(
8ee40 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61  p->owner, pthrea
8ee50 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74  d_self()));.}.st
8ee60 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64  atic int pthread
8ee70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c  MutexNotheld(sql
8ee80 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
8ee90 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66    return p->nRef
8eea0 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f 65  ==0 || pthread_e
8eeb0 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70  qual(p->owner, p
8eec0 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d  thread_self())==
8eed0 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0;.}.#endif../*.
8eee0 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e  ** Initialize an
8eef0 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74  d deinitialize t
8ef00 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
8ef10 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  em..*/.static in
8ef20 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e  t pthreadMutexIn
8ef30 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  it(void){ return
8ef40 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74   SQLITE_OK; }.st
8ef50 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64  atic int pthread
8ef60 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20  MutexEnd(void){ 
8ef70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
8ef80 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ; }../*.** The s
8ef90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
8efa0 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c  oc() routine all
8efb0 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20  ocates a new.** 
8efc0 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e  mutex and return
8efd0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
8efe0 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e  t.  If it return
8eff0 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d  s NULL.** that m
8f000 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65  eans that a mute
8f010 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61  x could not be a
8f020 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
8f030 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64  e.** will unwind
8f040 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72   its stack and r
8f050 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20  eturn an error. 
8f060 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   The argument.**
8f070 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
8f080 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65  x_alloc() is one
8f090 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65   of these intege
8f0a0 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a  r constants:.**.
8f0b0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
8f0c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
8f0d0 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  ST.** <li>  SQLI
8f0e0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
8f0f0 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  VE.** <li>  SQLI
8f100 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
8f110 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20  MASTER.** <li>  
8f120 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
8f130 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20  TIC_MEM.** <li> 
8f140 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
8f150 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69  ATIC_MEM2.** <li
8f160 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
8f170 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c  STATIC_PRNG.** <
8f180 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
8f190 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20  X_STATIC_LRU.** 
8f1a0 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  </ul>.**.** The 
8f1b0 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61  first two consta
8f1c0 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65  nts cause sqlite
8f1d0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
8f1e0 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e  to create.** a n
8f1f0 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e  ew mutex.  The n
8f200 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75  ew mutex is recu
8f210 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54  rsive when SQLIT
8f220 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
8f230 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74  E.** is used but
8f240 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
8f250 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f   so when SQLITE_
8f260 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73  MUTEX_FAST is us
8f270 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78  ed..** The mutex
8f280 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
8f290 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
8f2a0 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74   make a distinct
8f2b0 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53  ion.** between S
8f2c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
8f2d0 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45  RSIVE and SQLITE
8f2e0 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69  _MUTEX_FAST if i
8f2f0 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61  t does.** not wa
8f300 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69  nt to.  But SQLi
8f310 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71  te will only req
8f320 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65  uest a recursive
8f330 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73   mutex in.** cas
8f340 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c  es where it real
8f350 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49  ly needs one.  I
8f360 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72  f a faster non-r
8f370 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a  ecursive mutex.*
8f380 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
8f390 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e   is available on
8f3a0 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f   the host platfo
8f3b0 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75  rm, the mutex su
8f3c0 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74  bsystem.** might
8f3d0 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d   return such a m
8f3e0 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65  utex in response
8f3f0 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   to SQLITE_MUTEX
8f400 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  _FAST..**.** The
8f410 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70   other allowed p
8f420 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c  arameters to sql
8f430 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
8f440 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a  () each return.*
8f450 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
8f460 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74   static preexist
8f470 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 65  ing mutex.  Thre
8f480 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73  e static mutexes
8f490 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20   are.** used by 
8f4a0 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73  the current vers
8f4b0 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ion of SQLite.  
8f4c0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
8f4d0 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79  of SQLite.** may
8f4e0 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
8f4f0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20  static mutexes. 
8f500 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   Static mutexes 
8f510 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  are for internal
8f520 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74  .** use by SQLit
8f530 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61  e only.  Applica
8f540 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53  tions that use S
8f550 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68  QLite mutexes sh
8f560 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79  ould.** use only
8f570 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
8f580 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  exes returned by
8f590 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
8f5a0 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f  ST or.** SQLITE_
8f5b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e  MUTEX_RECURSIVE.
8f5c0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
8f5d0 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64   if one of the d
8f5e0 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72  ynamic mutex par
8f5f0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f  ameters (SQLITE_
8f600 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72  MUTEX_FAST.** or
8f610 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
8f620 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64  CURSIVE) is used
8f630 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75   then sqlite3_mu
8f640 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72  tex_alloc().** r
8f650 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65  eturns a differe
8f660 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72  nt mutex on ever
8f670 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72  y call.  But for
8f680 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20   the static .** 
8f690 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
8f6a0 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
8f6b0 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
8f6c0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
8f6d0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
8f6e0 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
8f6f0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
8f700 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c  *pthreadMutexAll
8f710 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20  oc(int iType){. 
8f720 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
8f730 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65  mutex staticMute
8f740 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51  xes[] = {.    SQ
8f750 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54  LITE3_MUTEX_INIT
8f760 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c  IALIZER,.    SQL
8f770 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
8f780 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49  ALIZER,.    SQLI
8f790 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
8f7a0 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54  LIZER,.    SQLIT
8f7b0 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
8f7c0 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45  IZER,.    SQLITE
8f7d0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
8f7e0 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33  ZER,.    SQLITE3
8f7f0 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
8f800 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65  ER.  };.  sqlite
8f810 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 77  3_mutex *p;.  sw
8f820 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20  itch( iType ){. 
8f830 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
8f840 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20  UTEX_RECURSIVE: 
8f850 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69  {.      p = sqli
8f860 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
8f870 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20  izeof(*p) );.   
8f880 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 64     if( p ){.#ifd
8f890 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52  ef SQLITE_HOMEGR
8f8a0 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55  OWN_RECURSIVE_MU
8f8b0 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  TEX.        /* I
8f8c0 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  f recursive mute
8f8d0 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69  xes are not avai
8f8e0 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68  lable, we will h
8f8f0 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 2a  ave to.        *
8f900 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e  * build our own.
8f910 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a    See below. */.
8f920 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f          pthread_
8f930 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d  mutex_init(&p->m
8f940 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a  utex, 0);.#else.
8f950 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61          /* Use a
8f960 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
8f970 20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c 61   if it is availa
8f980 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 70  ble */.        p
8f990 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72  thread_mutexattr
8f9a0 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 74 72  _t recursiveAttr
8f9b0 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61  ;.        pthrea
8f9c0 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 74  d_mutexattr_init
8f9d0 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29  (&recursiveAttr)
8f9e0 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61  ;.        pthrea
8f9f0 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 74  d_mutexattr_sett
8fa00 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41 74  ype(&recursiveAt
8fa10 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 45  tr, PTHREAD_MUTE
8fa20 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20  X_RECURSIVE);.  
8fa30 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75        pthread_mu
8fa40 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74  tex_init(&p->mut
8fa50 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 74  ex, &recursiveAt
8fa60 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68  tr);.        pth
8fa70 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 64  read_mutexattr_d
8fa80 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 76  estroy(&recursiv
8fa90 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20  eAttr);.#endif. 
8faa0 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69         p->id = i
8fab0 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Type;.      }.  
8fac0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
8fad0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
8fae0 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20  _MUTEX_FAST: {. 
8faf0 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33       p = sqlite3
8fb00 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65  MallocZero( size
8fb10 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20  of(*p) );.      
8fb20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20  if( p ){.       
8fb30 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
8fb40 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f          pthread_
8fb50 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d  mutex_init(&p->m
8fb60 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20  utex, 0);.      
8fb70 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
8fb80 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74     }.    default
8fb90 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
8fba0 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29  ( iType-2 >= 0 )
8fbb0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
8fbc0 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53  iType-2 < ArrayS
8fbd0 69 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 65  ize(staticMutexe
8fbe0 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20  s) );.      p = 
8fbf0 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69  &staticMutexes[i
8fc00 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70  Type-2];.      p
8fc10 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20  ->id = iType;.  
8fc20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
8fc30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
8fc40 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
8fc50 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61  routine dealloca
8fc60 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79  tes a previously
8fc70 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75  .** allocated mu
8fc80 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20  tex.  SQLite is 
8fc90 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c  careful to deall
8fca0 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d  ocate every.** m
8fcb0 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c  utex that it all
8fcc0 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  ocates..*/.stati
8fcd0 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75  c void pthreadMu
8fce0 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f  texFree(sqlite3_
8fcf0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
8fd00 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
8fd10 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
8fd20 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
8fd30 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d  _FAST || p->id==
8fd40 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
8fd50 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72  URSIVE );.  pthr
8fd60 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f  ead_mutex_destro
8fd70 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  y(&p->mutex);.  
8fd80 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
8fd90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
8fda0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
8fdb0 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  r() and sqlite3_
8fdc0 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74  mutex_try() rout
8fdd0 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20  ines attempt.** 
8fde0 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78  to enter a mutex
8fdf0 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  .  If another th
8fe00 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
8fe10 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
8fe20 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
8fe30 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
8fe40 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
8fe50 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
8fe60 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
8fe70 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73  ITE_BUSY.  The s
8fe80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
8fe90 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
8fea0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a  urns SQLITE_OK.*
8feb0 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75  * upon successfu
8fec0 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65  l entry.  Mutexe
8fed0 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
8fee0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
8fef0 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65  URSIVE can.** be
8ff00 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c   entered multipl
8ff10 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73  e times by the s
8ff20 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20  ame thread.  In 
8ff30 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a  such cases the,.
8ff40 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  ** mutex must be
8ff50 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c   exited an equal
8ff60 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
8ff70 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20   before another 
8ff80 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e  thread.** can en
8ff90 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d  ter.  If the sam
8ffa0 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74  e thread tries t
8ffb0 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65  o enter any othe
8ffc0 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a  r kind of mutex.
8ffd0 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ** more than onc
8ffe0 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  e, the behavior 
8fff0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
90000 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68  .static void pth
90010 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73  readMutexEnter(s
90020 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
90030 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  {.  assert( p->i
90040 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
90050 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68  RECURSIVE || pth
90060 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64  readMutexNotheld
90070 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53  (p) );..#ifdef S
90080 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f  QLITE_HOMEGROWN_
90090 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a  RECURSIVE_MUTEX.
900a0 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76    /* If recursiv
900b0 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f  e mutexes are no
900c0 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65  t available, the
900d0 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f  n we have to gro
900e0 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20  w.  ** our own. 
900f0 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
90100 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
90110 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  t pthread_equal(
90120 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63  ).  ** is atomic
90130 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f   - that it canno
90140 74 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e  t be deceived in
90150 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66  to thinking self
90160 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e  .  ** and p->own
90170 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20  er are equal if 
90180 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73  p->owner changes
90190 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c   between two val
901a0 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72  ues.  ** that ar
901b0 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73  e not equal to s
901c0 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f  elf while the co
901d0 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69  mparison is taki
901e0 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54  ng place..  ** T
901f0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
90200 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20  on also assumes 
90210 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65  a coherent cache
90220 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65   - that .  ** se
90230 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73  parate processes
90240 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66   cannot read dif
90250 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72  ferent values fr
90260 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a  om the same.  **
90270 20 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20   address at the 
90280 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65  same time.  If e
90290 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74  ither of these t
902a0 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20  wo conditions.  
902b0 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20  ** are not met, 
902c0 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73  then the mutexes
902d0 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70   will fail and p
902e0 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73  roblems will res
902f0 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20  ult..  */.  {.  
90300 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66    pthread_t self
90310 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28   = pthread_self(
90320 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52  );.    if( p->nR
90330 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f  ef>0 && pthread_
90340 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20  equal(p->owner, 
90350 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70  self) ){.      p
90360 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65  ->nRef++;.    }e
90370 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65  lse{.      pthre
90380 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70  ad_mutex_lock(&p
90390 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
903a0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d  assert( p->nRef=
903b0 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f  =0 );.      p->o
903c0 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20  wner = self;.   
903d0 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a     p->nRef = 1;.
903e0 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a      }.  }.#else.
903f0 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69    /* Use the bui
90400 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20  lt-in recursive 
90410 6d 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20  mutexes if they 
90420 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20  are available.. 
90430 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75   */.  pthread_mu
90440 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  tex_lock(&p->mut
90450 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20  ex);.  p->owner 
90460 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
90470 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23  ;.  p->nRef++;.#
90480 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
90490 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28  LITE_DEBUG.  if(
904a0 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20   p->trace ){.   
904b0 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d   printf("enter m
904c0 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74  utex %p (%d) wit
904d0 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c  h nRef=%d\n", p,
904e0 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52   p->trace, p->nR
904f0 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ef);.  }.#endif.
90500 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68  }.static int pth
90510 72 65 61 64 4d 75 74 65 78 54 72 79 28 73 71 6c  readMutexTry(sql
90520 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
90530 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
90540 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
90550 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
90560 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65  E || pthreadMute
90570 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a  xNotheld(p) );..
90580 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f  #ifdef SQLITE_HO
90590 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56  MEGROWN_RECURSIV
905a0 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20  E_MUTEX.  /* If 
905b0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65  recursive mutexe
905c0 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61  s are not availa
905d0 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76  ble, then we hav
905e0 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f  e to grow.  ** o
905f0 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d  ur own.  This im
90600 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73  plementation ass
90610 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61  umes that pthrea
90620 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69  d_equal().  ** i
90630 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20  s atomic - that 
90640 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63  it cannot be dec
90650 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b  eived into think
90660 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e  ing self.  ** an
90670 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65  d p->owner are e
90680 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72  qual if p->owner
90690 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e   changes between
906a0 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a   two values.  **
906b0 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71   that are not eq
906c0 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c  ual to self whil
906d0 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  e the comparison
906e0 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65   is taking place
906f0 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c  ..  ** This impl
90700 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20  ementation also 
90710 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65  assumes a cohere
90720 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20  nt cache - that 
90730 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70  .  ** separate p
90740 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20  rocesses cannot 
90750 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76  read different v
90760 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73  alues from the s
90770 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73  ame.  ** address
90780 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
90790 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66  e.  If either of
907a0 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69   these two condi
907b0 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e  tions.  ** are n
907c0 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65  ot met, then the
907d0 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61   mutexes will fa
907e0 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20  il and problems 
907f0 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a  will result..  *
90800 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61  /.  {.    pthrea
90810 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65  d_t self = pthre
90820 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69  ad_self();.    i
90830 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20  f( p->nRef>0 && 
90840 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d  pthread_equal(p-
90850 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b  >owner, self) ){
90860 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b  .      p->nRef++
90870 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
90880 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73  ITE_OK;.    }els
90890 65 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75  e if( pthread_mu
908a0 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e  tex_trylock(&p->
908b0 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20  mutex)==0 ){.   
908c0 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52     assert( p->nR
908d0 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ef==0 );.      p
908e0 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a  ->owner = self;.
908f0 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20        p->nRef = 
90900 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  1;.      rc = SQ
90910 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c  LITE_OK;.    }el
90920 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  se{.      rc = S
90930 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
90940 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a  }.  }.#else.  /*
90950 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69   Use the built-i
90960 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  n recursive mute
90970 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20  xes if they are 
90980 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a  available..  */.
90990 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75    if( pthread_mu
909a0 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e  tex_trylock(&p->
909b0 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20  mutex)==0 ){.   
909c0 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72   p->owner = pthr
909d0 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20  ead_self();.    
909e0 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72  p->nRef++;.    r
909f0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
90a00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
90a10 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
90a20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  }.#endif..#ifdef
90a30 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
90a40 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
90a50 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b  K && p->trace ){
90a60 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74  .    printf("ent
90a70 65 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29  er mutex %p (%d)
90a80 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22   with nRef=%d\n"
90a90 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70  , p, p->trace, p
90aa0 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e  ->nRef);.  }.#en
90ab0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  dif.  return rc;
90ac0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
90ad0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
90ae0 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74  e() routine exit
90af0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77  s a mutex that w
90b00 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  as.** previously
90b10 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20   entered by the 
90b20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68  same thread.  Th
90b30 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73  e behavior.** is
90b40 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
90b50 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
90b60 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
90b70 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75   or.** is not cu
90b80 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65  rrently allocate
90b90 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  d.  SQLite will 
90ba0 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e  never do either.
90bb0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
90bc0 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76  pthreadMutexLeav
90bd0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
90be0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
90bf0 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28  threadMutexHeld(
90c00 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d  p) );.  p->nRef-
90c10 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  -;.  assert( p->
90c20 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64  nRef==0 || p->id
90c30 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
90c40 45 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66  ECURSIVE );..#if
90c50 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47  def SQLITE_HOMEG
90c60 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d  ROWN_RECURSIVE_M
90c70 55 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52  UTEX.  if( p->nR
90c80 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68  ef==0 ){.    pth
90c90 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63  read_mutex_unloc
90ca0 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  k(&p->mutex);.  
90cb0 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61  }.#else.  pthrea
90cc0 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26  d_mutex_unlock(&
90cd0 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69  p->mutex);.#endi
90ce0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
90cf0 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e  _DEBUG.  if( p->
90d00 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69  trace ){.    pri
90d10 6e 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78  ntf("leave mutex
90d20 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52   %p (%d) with nR
90d30 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e  ef=%d\n", p, p->
90d40 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b  trace, p->nRef);
90d50 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53  .  }.#endif.}..S
90d60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
90d70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
90d80 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61  ods *sqlite3Defa
90d90 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  ultMutex(void){.
90da0 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
90db0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73  _mutex_methods s
90dc0 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70 74  Mutex = {.    pt
90dd0 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a  hreadMutexInit,.
90de0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78      pthreadMutex
90df0 45 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61 64  End,.    pthread
90e00 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20  MutexAlloc,.    
90e10 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 65  pthreadMutexFree
90e20 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74  ,.    pthreadMut
90e30 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74 68  exEnter,.    pth
90e40 72 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20 20  readMutexTry,.  
90e50 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65    pthreadMutexLe
90e60 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49  ave,.#ifdef SQLI
90e70 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74 68  TE_DEBUG.    pth
90e80 72 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20  readMutexHeld,. 
90e90 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e     pthreadMutexN
90ea0 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20  otheld.#else.   
90eb0 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66   0,.    0.#endif
90ec0 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20  .  };..  return 
90ed0 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64  &sMutex;.}..#end
90ee0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
90ef0 45 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f  EX_PTHREAD */../
90f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
90f10 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78  nd of mutex_unix
90f20 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
90f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
90f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
90f60 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f  egin file mutex_
90f70 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  w32.c **********
90f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
90fa0 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74  *.** 2007 August
90fb0 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   14.**.** The au
90fc0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
90fd0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
90fe0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
90ff0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
91000 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
91010 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
91020 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
91030 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
91040 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
91050 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
91060 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
91070 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
91080 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
91090 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
910a0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
910b0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
910c0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
910d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91110 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
91120 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
91130 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
91140 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20  plement mutexes 
91150 66 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a 2a 20  for win32.**.** 
91160 24 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 2e 63  $Id: mutex_w32.c
91170 2c 76 20 31 2e 31 35 20 32 30 30 39 2f 30 31 2f  ,v 1.15 2009/01/
91180 33 30 20 31 36 3a 30 39 3a 32 33 20 73 68 61 6e  30 16:09:23 shan
91190 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  e Exp $.*/../*.*
911a0 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
911b0 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
911c0 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63  used if we are c
911d0 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68  ompiling multith
911e0 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77  readed.** on a w
911f0 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a  in32 system..*/.
91200 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  #ifdef SQLITE_MU
91210 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45  TEX_W32../*.** E
91220 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75  ach recursive mu
91230 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  tex is an instan
91240 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
91250 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
91260 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
91270 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 49  _mutex {.  CRITI
91280 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65  CAL_SECTION mute
91290 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63  x;    /* Mutex c
912a0 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c  ontrolling the l
912b0 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b  ock */.  int id;
912c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
912d0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70      /* Mutex typ
912e0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b  e */.  int nRef;
912f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
91300 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
91310 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 44  nterances */.  D
91320 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 20  WORD owner;     
91330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72            /* Thr
91340 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73  ead holding this
91350 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   mutex */.};../*
91360 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
91370 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65  (non-zero) if we
91380 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64   are running und
91390 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c  er WinNT, Win2K,
913a0 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69   WinXP,.** or Wi
913b0 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 6c  nCE.  Return fal
913c0 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 69  se (zero) for Wi
913d0 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57  n95, Win98, or W
913e0 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65  inME..**.** Here
913f0 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 69   is an interesti
91400 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20  ng observation: 
91410 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61   Win95, Win98, a
91420 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a  nd WinME lack.**
91430 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28   the LockFileEx(
91440 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 63  ) API.  But we c
91450 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 61  an still statica
91460 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74  lly link against
91470 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 20   that.** API as 
91480 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 74  long as we don't
91490 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e   call it win run
914a0 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45  ning Win95/98/ME
914b0 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  .  A call to.** 
914c0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
914d0 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
914e0 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 73  e if the host is
914f0 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a   Win95/98/ME or.
91500 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73  ** WinNT/2K/XP s
91510 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b  o that we will k
91520 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e  now whether or n
91530 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79  ot we can safely
91540 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63   call.** the Loc
91550 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a  kFileEx() API..*
91560 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 29  *.** mutexIsNT()
91570 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f   is only used fo
91580 72 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 72  r the TryEnterCr
91590 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20  iticalSection() 
915a0 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69  API call,.** whi
915b0 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  ch is only avail
915c0 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 70  able if your app
915d0 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f 6d  lication was com
915e0 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 5f  piled with .** _
915f0 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 69  WIN32_WINNT defi
91600 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 3e  ned to a value >
91610 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 65  = 0x0400.  Curre
91620 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a  ntly, the only.*
91630 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e 74  * call to TryEnt
91640 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f  erCriticalSectio
91650 6e 28 29 20 69 73 20 23 69 66 64 65 66 27 65 64  n() is #ifdef'ed
91660 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 20   out, so #ifdef 
91670 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 20  .** this out as 
91680 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23  well..*/.#if 0.#
91690 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
916a0 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 65  CE.# define mute
916b0 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c  xIsNT()  (1).#el
916c0 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  se.  static int 
916d0 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 7b  mutexIsNT(void){
916e0 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20  .    static int 
916f0 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20  osType = 0;.    
91700 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b  if( osType==0 ){
91710 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e  .      OSVERSION
91720 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20  INFO sInfo;.    
91730 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73    sInfo.dwOSVers
91740 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69  ionInfoSize = si
91750 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20  zeof(sInfo);.   
91760 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28     GetVersionEx(
91770 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f  &sInfo);.      o
91780 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77  sType = sInfo.dw
91790 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f  PlatformId==VER_
917a0 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e  PLATFORM_WIN32_N
917b0 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d  T ? 2 : 1;.    }
917c0 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 79  .    return osTy
917d0 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69  pe==2;.  }.#endi
917e0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57  f /* SQLITE_OS_W
917f0 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  INCE */.#endif..
91800 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
91810 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  BUG./*.** The sq
91820 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
91830 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
91840 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72  utex_notheld() r
91850 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e  outine are.** in
91860 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f  tended for use o
91870 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72  nly inside asser
91880 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  t() statements..
91890 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
918a0 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74  nMutexHeld(sqlit
918b0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
918c0 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d  return p->nRef!=
918d0 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47  0 && p->owner==G
918e0 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49  etCurrentThreadI
918f0 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  d();.}.static in
91900 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c  t winMutexNothel
91910 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  d(sqlite3_mutex 
91920 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
91930 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f  >nRef==0 || p->o
91940 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e 74  wner!=GetCurrent
91950 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 65  ThreadId();.}.#e
91960 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  ndif.../*.** Ini
91970 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
91980 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74  itialize the mut
91990 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  ex subsystem..*/
919a0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d  .static int winM
919b0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20  utexInit(void){ 
919c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
919d0 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  ; }.static int w
919e0 69 6e 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29  inMutexEnd(void)
919f0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
91a00 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OK; }../*.** The
91a10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
91a20 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61  lloc() routine a
91a30 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a  llocates a new.*
91a40 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75  * mutex and retu
91a50 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
91a60 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75   it.  If it retu
91a70 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74  rns NULL.** that
91a80 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75   means that a mu
91a90 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  tex could not be
91aa0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c   allocated.  SQL
91ab0 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69  ite.** will unwi
91ac0 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64  nd its stack and
91ad0 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
91ae0 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a  .  The argument.
91af0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  ** to sqlite3_mu
91b00 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f  tex_alloc() is o
91b10 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65  ne of these inte
91b20 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a  ger constants:.*
91b30 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
91b40 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
91b50 46 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20  FAST            
91b60 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51     0.** <li>  SQ
91b70 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
91b80 53 49 56 45 20 20 20 20 20 20 20 20 20 20 31 0a  SIVE          1.
91b90 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
91ba0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
91bb0 54 45 52 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c  TER      2.** <l
91bc0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
91bd0 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20  _STATIC_MEM     
91be0 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53      3.** <li>  S
91bf0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
91c00 49 43 5f 50 52 4e 47 20 20 20 20 20 20 20 20 34  IC_PRNG        4
91c10 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ul>.**.** 
91c20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f  The first two co
91c30 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71  nstants cause sq
91c40 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
91c50 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a  c() to create.**
91c60 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54   a new mutex.  T
91c70 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20  he new mutex is 
91c80 72 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53  recursive when S
91c90 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
91ca0 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64  RSIVE.** is used
91cb0 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61   but not necessa
91cc0 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c  rily so when SQL
91cd0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69  ITE_MUTEX_FAST i
91ce0 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d  s used..** The m
91cf0 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
91d00 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  ion does not nee
91d10 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74  d to make a dist
91d20 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65  inction.** betwe
91d30 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  en SQLITE_MUTEX_
91d40 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51  RECURSIVE and SQ
91d50 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
91d60 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f  if it does.** no
91d70 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20  t want to.  But 
91d80 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79  SQLite will only
91d90 20 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72   request a recur
91da0 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a  sive mutex in.**
91db0 20 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20   cases where it 
91dc0 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65  really needs one
91dd0 2e 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e  .  If a faster n
91de0 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74  on-recursive mut
91df0 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ex.** implementa
91e00 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c  tion is availabl
91e10 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c  e on the host pl
91e20 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65  atform, the mute
91e30 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d  x subsystem.** m
91e40 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68  ight return such
91e50 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70   a mutex in resp
91e60 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d  onse to SQLITE_M
91e70 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a  UTEX_FAST..**.**
91e80 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77   The other allow
91e90 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  ed parameters to
91ea0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
91eb0 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75  lloc() each retu
91ec0 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
91ed0 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65  to a static pree
91ee0 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20  xisting mutex.  
91ef0 54 68 72 65 65 20 73 74 61 74 69 63 20 6d 75 74  Three static mut
91f00 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64  exes are.** used
91f10 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20   by the current 
91f20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74  version of SQLit
91f30 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69  e.  Future versi
91f40 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a  ons of SQLite.**
91f50 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f   may add additio
91f60 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78  nal static mutex
91f70 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65  es.  Static mute
91f80 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65  xes are for inte
91f90 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53  rnal.** use by S
91fa0 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70  QLite only.  App
91fb0 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75  lications that u
91fc0 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65  se SQLite mutexe
91fd0 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20  s should.** use 
91fe0 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63  only the dynamic
91ff0 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65   mutexes returne
92000 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45  d by SQLITE_MUTE
92010 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c  X_FAST or.** SQL
92020 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
92030 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  IVE..**.** Note 
92040 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74  that if one of t
92050 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  he dynamic mutex
92060 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c   parameters (SQL
92070 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a  ITE_MUTEX_FAST.*
92080 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45  * or SQLITE_MUTE
92090 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20  X_RECURSIVE) is 
920a0 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65  used then sqlite
920b0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a  3_mutex_alloc().
920c0 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66  ** returns a dif
920d0 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20  ferent mutex on 
920e0 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74  every call.  But
920f0 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 20   for the static 
92100 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c  .** mutex types,
92110 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20   the same mutex 
92120 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65  is returned on e
92130 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68  very call that h
92140 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74  as.** the same t
92150 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73  ype number..*/.s
92160 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75  tatic sqlite3_mu
92170 74 65 78 20 2a 77 69 6e 4d 75 74 65 78 41 6c 6c  tex *winMutexAll
92180 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20  oc(int iType){. 
92190 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
921a0 70 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 69 54  p;..  switch( iT
921b0 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ype ){.    case 
921c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
921d0 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  T:.    case SQLI
921e0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
921f0 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20  VE: {.      p = 
92200 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
92210 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b  o( sizeof(*p) );
92220 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a  .      if( p ){.
92230 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20          p->id = 
92240 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 49  iType;.        I
92250 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61  nitializeCritica
92260 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74  lSection(&p->mut
92270 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ex);.      }.   
92280 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
92290 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
922a0 20 20 20 20 20 73 74 61 74 69 63 20 73 71 6c 69       static sqli
922b0 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63  te3_mutex static
922c0 4d 75 74 65 78 65 73 5b 36 5d 3b 0a 20 20 20 20  Mutexes[6];.    
922d0 20 20 73 74 61 74 69 63 20 69 6e 74 20 69 73 49    static int isI
922e0 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 77  nit = 0;.      w
922f0 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b  hile( !isInit ){
92300 0a 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20  .        static 
92310 6c 6f 6e 67 20 6c 6f 63 6b 20 3d 20 30 3b 0a 20  long lock = 0;. 
92320 20 20 20 20 20 20 20 69 66 28 20 49 6e 74 65 72         if( Inter
92330 6c 6f 63 6b 65 64 49 6e 63 72 65 6d 65 6e 74 28  lockedIncrement(
92340 26 6c 6f 63 6b 29 3d 3d 31 20 29 7b 0a 20 20 20  &lock)==1 ){.   
92350 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20         int i;.  
92360 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
92370 20 69 3c 73 69 7a 65 6f 66 28 73 74 61 74 69 63   i<sizeof(static
92380 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28  Mutexes)/sizeof(
92390 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d  staticMutexes[0]
923a0 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ); i++){.       
923b0 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43       InitializeC
923c0 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26  riticalSection(&
923d0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d  staticMutexes[i]
923e0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20  .mutex);.       
923f0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
92400 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  sInit = 1;.     
92410 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
92420 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 20      Sleep(1);.  
92430 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
92440 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54        assert( iT
92450 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20  ype-2 >= 0 );.  
92460 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70      assert( iTyp
92470 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 73 74 61  e-2 < sizeof(sta
92480 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65  ticMutexes)/size
92490 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73  of(staticMutexes
924a0 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20 70 20  [0]) );.      p 
924b0 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73  = &staticMutexes
924c0 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20  [iType-2];.     
924d0 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
924e0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
924f0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
92500 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  p;.}.../*.** Thi
92510 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f  s routine deallo
92520 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73  cates a previous
92530 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  ly.** allocated 
92540 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69  mutex.  SQLite i
92550 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61  s careful to dea
92560 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a  llocate every.**
92570 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61   mutex that it a
92580 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61  llocates..*/.sta
92590 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65  tic void winMute
925a0 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  xFree(sqlite3_mu
925b0 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  tex *p){.  asser
925c0 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74  t( p );.  assert
925d0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a  ( p->nRef==0 );.
925e0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
925f0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  =SQLITE_MUTEX_FA
92600 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c  ST || p->id==SQL
92610 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
92620 49 56 45 20 29 3b 0a 20 20 44 65 6c 65 74 65 43  IVE );.  DeleteC
92630 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26  riticalSection(&
92640 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  p->mutex);.  sql
92650 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a  ite3_free(p);.}.
92660 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
92670 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
92680 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
92690 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
926a0 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20  s attempt.** to 
926b0 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20  enter a mutex.  
926c0 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  If another threa
926d0 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74  d is already wit
926e0 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a  hin the mutex,.*
926f0 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  * sqlite3_mutex_
92700 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f  enter() will blo
92710 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  ck and sqlite3_m
92720 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20  utex_try() will 
92730 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
92740 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69  _BUSY.  The sqli
92750 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
92760 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
92770 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75  s SQLITE_OK.** u
92780 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65  pon successful e
92790 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63  ntry.  Mutexes c
927a0 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c  reated using SQL
927b0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
927c0 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e  IVE can.** be en
927d0 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74  tered multiple t
927e0 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65  imes by the same
927f0 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63   thread.  In suc
92800 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20  h cases the,.** 
92810 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78  mutex must be ex
92820 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75  ited an equal nu
92830 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65  mber of times be
92840 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72  fore another thr
92850 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72  ead.** can enter
92860 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74  .  If the same t
92870 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65  hread tries to e
92880 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b  nter any other k
92890 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20  ind of mutex.** 
928a0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20  more than once, 
928b0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
928c0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74  undefined..*/.st
928d0 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 74  atic void winMut
928e0 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f  exEnter(sqlite3_
928f0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
92900 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
92910 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
92920 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f  VE || winMutexNo
92930 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 45 6e  theld(p) );.  En
92940 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
92950 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20  on(&p->mutex);. 
92960 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43   p->owner = GetC
92970 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29  urrentThreadId()
92980 3b 20 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a  ; .  p->nRef++;.
92990 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  }.static int win
929a0 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33  MutexTry(sqlite3
929b0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e  _mutex *p){.  in
929c0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55  t rc = SQLITE_BU
929d0 53 59 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  SY;.  assert( p-
929e0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
929f0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77  X_RECURSIVE || w
92a00 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70  inMutexNotheld(p
92a10 29 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  ) );.  /*.  ** T
92a20 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
92a30 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 20 69  _try() routine i
92a40 73 20 76 65 72 79 20 72 61 72 65 6c 79 20 75 73  s very rarely us
92a50 65 64 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 0a  ed, and when it.
92a60 20 20 2a 2a 20 69 73 20 75 73 65 64 20 69 74 20    ** is used it 
92a70 69 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f 70 74  is merely an opt
92a80 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f 20 69  imization.  So i
92a90 74 20 69 73 20 4f 4b 20 66 6f 72 20 69 74 20 74  t is OK for it t
92aa0 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 66 61  o always.  ** fa
92ab0 69 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  il.  .  **.  ** 
92ac0 54 68 65 20 54 72 79 45 6e 74 65 72 43 72 69 74  The TryEnterCrit
92ad0 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 6e  icalSection() in
92ae0 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c 79 20  terface is only 
92af0 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57 69 6e  available on Win
92b00 4e 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73 6f 6d  NT..  ** And som
92b10 65 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70 69 6c  e windows compil
92b20 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69 66 20  ers complain if 
92b30 79 6f 75 20 74 72 79 20 74 6f 20 75 73 65 20 69  you try to use i
92b40 74 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 66  t without.  ** f
92b50 69 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d 65 20  irst doing some 
92b60 23 64 65 66 69 6e 65 73 20 74 68 61 74 20 70 72  #defines that pr
92b70 65 76 65 6e 74 20 53 51 4c 69 74 65 20 66 72 6f  event SQLite fro
92b80 6d 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 57 69  m building on Wi
92b90 6e 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68  n98..  ** For th
92ba0 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 77 69  at reason, we wi
92bb0 6c 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f 70 74  ll omit this opt
92bc0 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 6e 6f  imization for no
92bd0 77 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74 69 63  w.  See.  ** tic
92be0 6b 65 74 20 23 32 36 38 35 2e 0a 20 20 2a 2f 0a  ket #2685..  */.
92bf0 23 69 66 20 30 0a 20 20 69 66 28 20 6d 75 74 65  #if 0.  if( mute
92c00 78 49 73 4e 54 28 29 20 26 26 20 54 72 79 45 6e  xIsNT() && TryEn
92c10 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
92c20 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20 29 7b  on(&p->mutex) ){
92c30 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20  .    p->owner = 
92c40 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64  GetCurrentThread
92c50 49 64 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65  Id();.    p->nRe
92c60 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  f++;.    rc = SQ
92c70 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23 65 6c  LITE_OK;.  }.#el
92c80 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
92c90 4d 45 54 45 52 28 70 29 3b 0a 23 65 6e 64 69 66  METER(p);.#endif
92ca0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
92cb0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
92cc0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
92cd0 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
92ce0 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
92cf0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
92d00 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
92d10 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
92d20 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e  ehavior.** is un
92d30 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d  defined if the m
92d40 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72  utex is not curr
92d50 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72  ently entered or
92d60 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
92d70 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ntly allocated. 
92d80 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
92d90 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f  er do either..*/
92da0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e  .static void win
92db0 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74  MutexLeave(sqlit
92dc0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
92dd0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
92de0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
92df0 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 72 72  ->owner==GetCurr
92e00 65 6e 74 54 68 72 65 61 64 49 64 28 29 20 29 3b  entThreadId() );
92e10 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  .  p->nRef--;.  
92e20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d  assert( p->nRef=
92e30 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c  =0 || p->id==SQL
92e40 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
92e50 49 56 45 20 29 3b 0a 20 20 4c 65 61 76 65 43 72  IVE );.  LeaveCr
92e60 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70  iticalSection(&p
92e70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c  ->mutex);.}..SQL
92e80 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
92e90 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
92ea0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c  s *sqlite3Defaul
92eb0 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20  tMutex(void){.  
92ec0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
92ed0 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75  utex_methods sMu
92ee0 74 65 78 20 3d 20 7b 0a 20 20 20 20 77 69 6e 4d  tex = {.    winM
92ef0 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 77 69  utexInit,.    wi
92f00 6e 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 77  nMutexEnd,.    w
92f10 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20  inMutexAlloc,.  
92f20 20 20 77 69 6e 4d 75 74 65 78 46 72 65 65 2c 0a    winMutexFree,.
92f30 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e 74 65      winMutexEnte
92f40 72 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 54  r,.    winMutexT
92f50 72 79 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  ry,.    winMutex
92f60 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51  Leave,.#ifdef SQ
92f70 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 77  LITE_DEBUG.    w
92f80 69 6e 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20  inMutexHeld,.   
92f90 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64   winMutexNotheld
92fa0 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20  .#else.    0,.  
92fb0 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a    0.#endif.  };.
92fc0 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65  .  return &sMute
92fd0 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  x;.}.#endif /* S
92fe0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20  QLITE_MUTEX_W32 
92ff0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
93000 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78  *** End of mutex
93010 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _w32.c *********
93020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93040 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
93050 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d  *** Begin file m
93060 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  alloc.c ********
93070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93090 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
930a0 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
930b0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
930c0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
930d0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
930e0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
930f0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
93100 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
93110 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
93120 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
93130 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
93140 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
93150 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
93160 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
93170 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
93180 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
93190 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
931a0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
931b0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
931c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
931d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
931e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
931f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
93210 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   Memory allocati
93220 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  on functions use
93230 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c  d throughout sql
93240 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ite..**.** $Id: 
93250 6d 61 6c 6c 6f 63 2e 63 2c 76 20 31 2e 36 32 20  malloc.c,v 1.62 
93260 32 30 30 39 2f 30 35 2f 30 33 20 32 30 3a 32 33  2009/05/03 20:23
93270 3a 35 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :54 drh Exp $.*/
93280 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
93290 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e 20 74  tine runs when t
932a0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
932b0 74 6f 72 20 73 65 65 73 20 74 68 61 74 20 74 68  tor sees that th
932c0 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72  e.** total memor
932d0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  y allocation is 
932e0 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65 64 20  about to exceed 
932f0 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a 2a 2a  the soft heap.**
93300 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 74 69   limit..*/.stati
93310 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61 70 4c  c void softHeapL
93320 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a 20 20  imitEnforcer(.  
93330 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 0a  void *NotUsed, .
93340 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
93350 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e 74 20  NotUsed2,.  int 
93360 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 20 55  allocSize.){.  U
93370 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
93380 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
93390 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  d2);.  sqlite3_r
933a0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c  elease_memory(al
933b0 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a  locSize);.}../*.
933c0 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 74 20  ** Set the soft 
933d0 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20  heap-size limit 
933e0 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e  for the library.
933f0 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20   Passing a zero 
93400 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20  or .** negative 
93410 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20  value indicates 
93420 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c  no limit..*/.SQL
93430 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
93440 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
93450 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73  imit(int n){.  s
93460 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c  qlite3_uint64 iL
93470 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72  imit;.  int over
93480 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29  age;.  if( n<0 )
93490 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30  {.    iLimit = 0
934a0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
934b0 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20  Limit = n;.  }. 
934c0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
934d0 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 69 4c 69  ize();.  if( iLi
934e0 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c  mit>0 ){.    sql
934f0 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28  ite3MemoryAlarm(
93500 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66  softHeapLimitEnf
93510 6f 72 63 65 72 2c 20 30 2c 20 69 4c 69 6d 69 74  orcer, 0, iLimit
93520 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
93530 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61  sqlite3MemoryAla
93540 72 6d 28 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d  rm(0, 0, 0);.  }
93550 0a 20 20 6f 76 65 72 61 67 65 20 3d 20 28 69 6e  .  overage = (in
93560 74 29 28 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  t)(sqlite3_memor
93570 79 5f 75 73 65 64 28 29 20 2d 20 28 69 36 34 29  y_used() - (i64)
93580 6e 29 3b 0a 20 20 69 66 28 20 6f 76 65 72 61 67  n);.  if( overag
93590 65 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  e>0 ){.    sqlit
935a0 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
935b0 79 28 6f 76 65 72 61 67 65 29 3b 0a 20 20 7d 0a  y(overage);.  }.
935c0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74  }../*.** Attempt
935d0 20 74 6f 20 72 65 6c 65 61 73 65 20 75 70 20 74   to release up t
935e0 6f 20 6e 20 62 79 74 65 73 20 6f 66 20 6e 6f 6e  o n bytes of non
935f0 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72  -essential memor
93600 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 68  y currently.** h
93610 65 6c 64 20 62 79 20 53 51 4c 69 74 65 2e 20 41  eld by SQLite. A
93620 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e  n example of non
93630 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72  -essential memor
93640 79 20 69 73 20 6d 65 6d 6f 72 79 20 75 73 65 64  y is memory used
93650 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20 64 61 74   to.** cache dat
93660 61 62 61 73 65 20 70 61 67 65 73 20 74 68 61 74  abase pages that
93670 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65 6e 74   are not current
93680 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 53 51  ly in use..*/.SQ
93690 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
936a0 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
936b0 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 69 66 64  ory(int n){.#ifd
936c0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
936d0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
936e0 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 20 3d 20  NT.  int nRet = 
936f0 30 3b 0a 23 69 66 20 30 0a 20 20 6e 52 65 74 20  0;.#if 0.  nRet 
93700 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  += sqlite3VdbeRe
93710 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a  leaseMemory(n);.
93720 23 65 6e 64 69 66 0a 20 20 6e 52 65 74 20 2b 3d  #endif.  nRet +=
93730 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
93740 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52  leaseMemory(n-nR
93750 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52  et);.  return nR
93760 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53  et;.#else.  UNUS
93770 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b  ED_PARAMETER(n);
93780 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
93790 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  _OK;.#endif.}../
937a0 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66 6f 72  *.** State infor
937b0 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20  mation local to 
937c0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
937d0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e  ation subsystem.
937e0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
937f0 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d  E_WSD struct Mem
93800 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e  0Global {.  /* N
93810 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61  umber of free pa
93820 67 65 73 20 66 6f 72 20 73 63 72 61 74 63 68 20  ges for scratch 
93830 61 6e 64 20 70 61 67 65 2d 63 61 63 68 65 20 6d  and page-cache m
93840 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33 32 20 6e  emory */.  u32 n
93850 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75  ScratchFree;.  u
93860 33 32 20 6e 50 61 67 65 46 72 65 65 3b 0a 0a 20  32 nPageFree;.. 
93870 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
93880 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f  mutex;         /
93890 2a 20 4d 75 74 65 78 20 74 6f 20 73 65 72 69 61  * Mutex to seria
938a0 6c 69 7a 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a  lize access */..
938b0 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c    /*.  ** The al
938c0 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64  arm callback and
938d0 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20   its arguments. 
938e0 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20   The mem0.mutex 
938f0 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62  lock will.  ** b
93900 65 20 68 65 6c 64 20 77 68 69 6c 65 20 74 68 65  e held while the
93910 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e   callback is run
93920 6e 69 6e 67 2e 20 20 52 65 63 75 72 73 69 76 65  ning.  Recursive
93930 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a   calls into.  **
93940 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73   the memory subs
93950 79 73 74 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65  ystem are allowe
93960 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77 20 63 61  d, but no new ca
93970 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a  llbacks will be.
93980 20 20 2a 2a 20 69 73 73 75 65 64 2e 20 20 54 68    ** issued.  Th
93990 65 20 61 6c 61 72 6d 42 75 73 79 20 76 61 72 69  e alarmBusy vari
939a0 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 70  able is set to p
939b0 72 65 76 65 6e 74 20 72 65 63 75 72 73 69 76 65  revent recursive
939c0 0a 20 20 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 2e  .  ** callbacks.
939d0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
939e0 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 72 65 73  int64 alarmThres
939f0 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 28 2a 61  hold;.  void (*a
93a00 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 28 76 6f  larmCallback)(vo
93a10 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  id*, sqlite3_int
93a20 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  64,int);.  void 
93a30 2a 61 6c 61 72 6d 41 72 67 3b 0a 20 20 69 6e 74  *alarmArg;.  int
93a40 20 61 6c 61 72 6d 42 75 73 79 3b 0a 0a 20 20 2f   alarmBusy;..  /
93a50 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72 73 20  *.  ** Pointers 
93a60 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71  to the end of sq
93a70 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
93a80 67 2e 70 53 63 72 61 74 63 68 20 61 6e 64 0a 20  g.pScratch and. 
93a90 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   ** sqlite3Globa
93aa0 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 74 6f  lConfig.pPage to
93ab0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f   a block of memo
93ac0 72 79 20 74 68 61 74 20 72 65 63 6f 72 64 73 0a  ry that records.
93ad0 20 20 2a 2a 20 77 68 69 63 68 20 70 61 67 65 73    ** which pages
93ae0 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a   are available..
93af0 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 63 72    */.  u32 *aScr
93b00 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20  atchFree;.  u32 
93b10 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d 20 6d 65  *aPageFree;.} me
93b20 6d 30 20 3d 20 7b 20 36 32 35 36 30 39 35 35 2c  m0 = { 62560955,
93b30 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
93b40 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 64 65 66  0, 0, 0 };..#def
93b50 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 41 4c 28  ine mem0 GLOBAL(
93b60 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61  struct Mem0Globa
93b70 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20  l, mem0)../*.** 
93b80 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
93b90 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
93ba0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53   subsystem..*/.S
93bb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
93bc0 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49  t sqlite3MallocI
93bd0 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 69 66 28  nit(void){.  if(
93be0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
93bf0 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d  nfig.m.xMalloc==
93c00 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
93c10 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3b  MemSetDefault();
93c20 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d  .  }.  memset(&m
93c30 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d  em0, 0, sizeof(m
93c40 65 6d 30 29 29 3b 0a 20 20 69 66 28 20 73 71 6c  em0));.  if( sql
93c50 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
93c60 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20  .bCoreMutex ){. 
93c70 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78 20 3d 20     mem0.mutex = 
93c80 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
93c90 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
93ca0 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a  TATIC_MEM);.  }.
93cb0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
93cc0 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74  balConfig.pScrat
93cd0 63 68 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f  ch && sqlite3Glo
93ce0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61  balConfig.szScra
93cf0 74 63 68 3e 3d 31 30 30 0a 20 20 20 20 20 20 26  tch>=100.      &
93d00 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  & sqlite3GlobalC
93d10 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3e 3d  onfig.nScratch>=
93d20 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
93d30 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
93d40 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
93d50 68 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73  h = ROUNDDOWN8(s
93d60 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
93d70 69 67 2e 73 7a 53 63 72 61 74 63 68 2d 34 29 3b  ig.szScratch-4);
93d80 0a 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74  .    mem0.aScrat
93d90 63 68 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26  chFree = (u32*)&
93da0 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47  ((char*)sqlite3G
93db0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72  lobalConfig.pScr
93dc0 61 74 63 68 29 0a 20 20 20 20 20 20 20 20 20 20  atch).          
93dd0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
93de0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
93df0 63 72 61 74 63 68 2a 73 71 6c 69 74 65 33 47 6c  cratch*sqlite3Gl
93e00 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
93e10 74 63 68 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d  tch];.    for(i=
93e20 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62  0; i<sqlite3Glob
93e30 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63  alConfig.nScratc
93e40 68 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53  h; i++){ mem0.aS
93e50 63 72 61 74 63 68 46 72 65 65 5b 69 5d 20 3d 20  cratchFree[i] = 
93e60 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 53  i; }.    mem0.nS
93e70 63 72 61 74 63 68 46 72 65 65 20 3d 20 73 71 6c  cratchFree = sql
93e80 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
93e90 2e 6e 53 63 72 61 74 63 68 3b 0a 20 20 7d 65 6c  .nScratch;.  }el
93ea0 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  se{.    sqlite3G
93eb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72  lobalConfig.pScr
93ec0 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 73 71  atch = 0;.    sq
93ed0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
93ee0 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 30 3b  g.szScratch = 0;
93ef0 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
93f00 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
93f10 50 61 67 65 20 26 26 20 73 71 6c 69 74 65 33 47  Page && sqlite3G
93f20 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61  lobalConfig.szPa
93f30 67 65 3e 3d 35 31 32 0a 20 20 20 20 20 20 26 26  ge>=512.      &&
93f40 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
93f50 6e 66 69 67 2e 6e 50 61 67 65 3e 3d 31 20 29 7b  nfig.nPage>=1 ){
93f60 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
93f70 69 6e 74 20 6f 76 65 72 68 65 61 64 3b 0a 20 20  int overhead;.  
93f80 20 20 69 6e 74 20 73 7a 20 3d 20 52 4f 55 4e 44    int sz = ROUND
93f90 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f  DOWN8(sqlite3Glo
93fa0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65  balConfig.szPage
93fb0 29 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73  );.    int n = s
93fc0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
93fd0 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76  ig.nPage;.    ov
93fe0 65 72 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20  erhead = (4*n + 
93ff0 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20  sz - 1)/sz;.    
94000 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
94010 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65  fig.nPage -= ove
94020 72 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e  rhead;.    mem0.
94030 61 50 61 67 65 46 72 65 65 20 3d 20 28 75 33 32  aPageFree = (u32
94040 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74  *)&((char*)sqlit
94050 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
94060 50 61 67 65 29 0a 20 20 20 20 20 20 20 20 20 20  Page).          
94070 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
94080 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
94090 61 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61  age*sqlite3Globa
940a0 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a  lConfig.nPage];.
940b0 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
940c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
940d0 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20  ig.nPage; i++){ 
940e0 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69  mem0.aPageFree[i
940f0 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d  ] = i; }.    mem
94100 30 2e 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71  0.nPageFree = sq
94110 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
94120 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65  g.nPage;.  }else
94130 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
94140 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20  balConfig.pPage 
94150 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
94160 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
94170 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  age = 0;.  }.  r
94180 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
94190 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69  balConfig.m.xIni
941a0 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  t(sqlite3GlobalC
941b0 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61  onfig.m.pAppData
941c0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e  );.}../*.** Dein
941d0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
941e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
941f0 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c  ubsystem..*/.SQL
94200 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
94210 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e   sqlite3MallocEn
94220 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73  d(void){.  if( s
94230 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
94240 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 20 29  ig.m.xShutdown )
94250 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
94260 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75  balConfig.m.xShu
94270 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f  tdown(sqlite3Glo
94280 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70  balConfig.m.pApp
94290 44 61 74 61 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d  Data);.  }.  mem
942a0 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69  set(&mem0, 0, si
942b0 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a  zeof(mem0));.}..
942c0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
942d0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
942e0 79 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63  y currently chec
942f0 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49  ked out..*/.SQLI
94300 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69  TE_API sqlite3_i
94310 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d  nt64 sqlite3_mem
94320 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a  ory_used(void){.
94330 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73    int n, mx;.  s
94340 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73  qlite3_int64 res
94350 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74  ;.  sqlite3_stat
94360 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  us(SQLITE_STATUS
94370 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e  _MEMORY_USED, &n
94380 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73  , &mx, 0);.  res
94390 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
943a0 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72  4)n;  /* Work ar
943b0 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c  ound bug in Borl
943c0 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33  and C. Ticket #3
943d0 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  216 */.  return 
943e0 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  res;.}../*.** Re
943f0 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  turn the maximum
94400 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
94410 79 20 74 68 61 74 20 68 61 73 20 65 76 65 72 20  y that has ever 
94420 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20  been.** checked 
94430 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 72  out since either
94440 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
94450 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a  f this process.*
94460 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d  * or since the m
94470 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 74  ost recent reset
94480 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
94490 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
944a0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
944b0 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74  hwater(int reset
944c0 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20  Flag){.  int n, 
944d0 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  mx;.  sqlite3_in
944e0 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74  t64 res;.  sqlit
944f0 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45  e3_status(SQLITE
94500 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
94510 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65  SED, &n, &mx, re
94520 73 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20  setFlag);.  res 
94530 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  = (sqlite3_int64
94540 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72  )mx;  /* Work ar
94550 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c  ound bug in Borl
94560 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33  and C. Ticket #3
94570 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  216 */.  return 
94580 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  res;.}../*.** Ch
94590 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63  ange the alarm c
945a0 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54  allback.*/.SQLIT
945b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
945c0 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d  lite3MemoryAlarm
945d0 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62  (.  void(*xCallb
945e0 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c  ack)(void *pArg,
945f0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75   sqlite3_int64 u
94600 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f  sed,int N),.  vo
94610 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69  id *pArg,.  sqli
94620 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73  te3_int64 iThres
94630 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65  hold.){.  sqlite
94640 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
94650 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d  m0.mutex);.  mem
94660 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20  0.alarmCallback 
94670 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d  = xCallback;.  m
94680 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70  em0.alarmArg = p
94690 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72  Arg;.  mem0.alar
946a0 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68  mThreshold = iTh
946b0 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74  reshold;.  sqlit
946c0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
946d0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65  em0.mutex);.  re
946e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
946f0 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
94700 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
94710 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74  D./*.** Deprecat
94720 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65  ed external inte
94730 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c  rface.  Internal
94740 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64  /core SQLite cod
94750 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c  e.** should call
94760 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c   sqlite3MemoryAl
94770 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  arm..*/.SQLITE_A
94780 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d  PI int sqlite3_m
94790 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76  emory_alarm(.  v
947a0 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28  oid(*xCallback)(
947b0 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69  void *pArg, sqli
947c0 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69  te3_int64 used,i
947d0 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70  nt N),.  void *p
947e0 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69  Arg,.  sqlite3_i
947f0 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a  nt64 iThreshold.
94800 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
94810 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78  te3MemoryAlarm(x
94820 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20  Callback, pArg, 
94830 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23  iThreshold);.}.#
94840 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69  endif../*.** Tri
94850 67 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a  gger the alarm .
94860 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
94870 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72  qlite3MallocAlar
94880 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  m(int nByte){.  
94890 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b  void (*xCallback
948a0 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f  )(void*,sqlite3_
948b0 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71  int64,int);.  sq
948c0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55  lite3_int64 nowU
948d0 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72  sed;.  void *pAr
948e0 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c  g;.  if( mem0.al
948f0 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 7c  armCallback==0 |
94900 7c 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79  | mem0.alarmBusy
94910 20 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65    ) return;.  me
94920 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 31  m0.alarmBusy = 1
94930 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20  ;.  xCallback = 
94940 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
94950 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20  ck;.  nowUsed = 
94960 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c  sqlite3StatusVal
94970 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  ue(SQLITE_STATUS
94980 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20  _MEMORY_USED);. 
94990 20 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61   pArg = mem0.ala
949a0 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33  rmArg;.  sqlite3
949b0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
949c0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c  0.mutex);.  xCal
949d0 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55  lback(pArg, nowU
949e0 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73  sed, nByte);.  s
949f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
94a00 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
94a10 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79    mem0.alarmBusy
94a20 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44   = 0;.}../*.** D
94a30 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  o a memory alloc
94a40 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74 69  ation with stati
94a50 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d 73  stics and alarms
94a60 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a 2a  .  Assume the.**
94a70 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79   lock is already
94a80 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63   held..*/.static
94a90 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68 41   int mallocWithA
94aa0 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 64  larm(int n, void
94ab0 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e 46   **pp){.  int nF
94ac0 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a  ull;.  void *p;.
94ad0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
94ae0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
94af0 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 46  0.mutex) );.  nF
94b00 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f  ull = sqlite3Glo
94b10 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75  balConfig.m.xRou
94b20 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69 74  ndup(n);.  sqlit
94b30 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49  e3StatusSet(SQLI
94b40 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43  TE_STATUS_MALLOC
94b50 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 28  _SIZE, n);.  if(
94b60 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62   mem0.alarmCallb
94b70 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  ack!=0 ){.    in
94b80 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 65  t nUsed = sqlite
94b90 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c  3StatusValue(SQL
94ba0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
94bb0 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66 28  Y_USED);.    if(
94bc0 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20   nUsed+nFull >= 
94bd0 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68  mem0.alarmThresh
94be0 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  old ){.      sql
94bf0 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28  ite3MallocAlarm(
94c00 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  nFull);.    }.  
94c10 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47  }.  p = sqlite3G
94c20 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d  lobalConfig.m.xM
94c30 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20  alloc(nFull);.  
94c40 69 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 6d 30  if( p==0 && mem0
94c50 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29  .alarmCallback )
94c60 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c  {.    sqlite3Mal
94c70 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b  locAlarm(nFull);
94c80 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
94c90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
94ca0 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20  Malloc(nFull);. 
94cb0 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20   }.  if( p ){.  
94cc0 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65    nFull = sqlite
94cd0 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a  3MallocSize(p);.
94ce0 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
94cf0 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
94d00 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20  US_MEMORY_USED, 
94d10 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70  nFull);.  }.  *p
94d20 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20  p = p;.  return 
94d30 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  nFull;.}../*.** 
94d40 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e  Allocate memory.
94d50 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
94d60 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 6d  s like sqlite3_m
94d70 61 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 20 74  alloc() except t
94d80 68 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 6d 65  hat it.** assume
94d90 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62  s the memory sub
94da0 73 79 73 74 65 6d 20 68 61 73 20 61 6c 72 65 61  system has alrea
94db0 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  dy been initiali
94dc0 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  zed..*/.SQLITE_P
94dd0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
94de0 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e  ite3Malloc(int n
94df0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20  ){.  void *p;.  
94e00 69 66 28 20 6e 3c 3d 30 20 7c 7c 20 4e 45 56 45  if( n<=0 || NEVE
94e10 52 28 6e 3e 3d 30 78 37 66 66 66 66 66 30 30 29  R(n>=0x7fffff00)
94e20 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 4e   ){.    /* The N
94e30 45 56 45 52 28 6e 3e 3d 30 78 37 66 66 66 66 66  EVER(n>=0x7fffff
94e40 30 30 29 20 74 65 72 6d 20 69 73 20 61 64 64 65  00) term is adde
94e50 64 20 6f 75 74 20 6f 66 20 70 61 72 61 6e 6f 69  d out of paranoi
94e60 61 2e 20 20 57 65 20 77 61 6e 74 20 74 6f 20 6d  a.  We want to m
94e70 61 6b 65 0a 20 20 20 20 2a 2a 20 61 62 73 6f 6c  ake.    ** absol
94e80 75 74 65 6c 79 20 73 75 72 65 20 74 68 61 74 20  utely sure that 
94e90 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67  there is nothing
94ea0 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 20 74   within SQLite t
94eb0 68 61 74 20 63 61 6e 20 63 61 75 73 65 20 61 0a  hat can cause a.
94ec0 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c      ** memory al
94ed0 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 6e 75  location of a nu
94ee0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 68  mber of bytes wh
94ef0 69 63 68 20 69 73 20 6e 65 61 72 20 74 68 65 20  ich is near the 
94f00 6d 61 78 69 6d 75 6d 20 73 69 67 6e 65 64 0a 20  maximum signed. 
94f10 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 76 61     ** integer va
94f20 6c 75 65 20 61 6e 64 20 74 68 75 73 20 63 61 75  lue and thus cau
94f30 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76  se an integer ov
94f40 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66  erflow inside of
94f50 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 0a 20   the xMalloc(). 
94f60 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61     ** implementa
94f70 74 69 6f 6e 2e 20 20 54 68 65 20 6e 3e 3d 30 78  tion.  The n>=0x
94f80 37 66 66 66 66 66 30 30 20 67 69 76 65 73 20 75  7fffff00 gives u
94f90 73 20 32 35 35 20 62 79 74 65 73 20 6f 66 20 68  s 255 bytes of h
94fa0 65 61 64 72 6f 6f 6d 2e 20 20 54 68 65 0a 20 20  eadroom.  The.  
94fb0 20 20 2a 2a 20 74 65 73 74 20 73 68 6f 75 6c 64    ** test should
94fc0 20 6e 65 76 65 72 20 62 65 20 74 72 75 65 20 62   never be true b
94fd0 65 63 61 75 73 65 20 53 51 4c 49 54 45 5f 4d 41  ecause SQLITE_MA
94fe0 58 5f 4c 45 4e 47 54 48 20 73 68 6f 75 6c 64 20  X_LENGTH should 
94ff0 62 65 20 6d 75 63 68 0a 20 20 20 20 2a 2a 20 6c  be much.    ** l
95000 65 73 73 20 74 68 61 6e 20 30 78 37 66 66 66 66  ess than 0x7ffff
95010 66 30 30 20 61 6e 64 20 69 74 20 73 68 6f 75 6c  f00 and it shoul
95020 64 20 63 61 74 63 68 20 6c 61 72 67 65 20 6d 65  d catch large me
95030 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
95040 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 74  .    ** before t
95050 68 65 79 20 72 65 61 63 68 20 74 68 69 73 20 70  hey reach this p
95060 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20 70 20 3d  oint. */.    p =
95070 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
95080 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
95090 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a  fig.bMemstat ){.
950a0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
950b0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74  x_enter(mem0.mut
950c0 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57  ex);.    mallocW
950d0 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b  ithAlarm(n, &p);
950e0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
950f0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
95100 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tex);.  }else{. 
95110 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c     p = sqlite3Gl
95120 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61  obalConfig.m.xMa
95130 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 72  lloc(n);.  }.  r
95140 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
95150 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
95160 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
95170 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75  ocation is for u
95180 73 65 20 62 79 20 74 68 65 20 61 70 70 6c 69 63  se by the applic
95190 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20  ation..** First 
951a0 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65  make sure the me
951b0 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 69  mory subsystem i
951c0 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74  s initialized, t
951d0 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c  hen do the.** al
951e0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  location..*/.SQL
951f0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
95200 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74  lite3_malloc(int
95210 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c   n){.#ifndef SQL
95220 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
95230 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  T.  if( sqlite3_
95240 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72  initialize() ) r
95250 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a  eturn 0;.#endif.
95260 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
95270 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a  Malloc(n);.}../*
95280 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61 64 20  .** Each thread 
95290 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20  may only have a 
952a0 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69  single outstandi
952b0 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72  ng allocation fr
952c0 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61  om.** xScratchMa
952d0 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69  lloc().  We veri
952e0 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69  fy this constrai
952f0 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65  nt in the single
95300 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73  -threaded.** cas
95310 65 20 62 79 20 73 65 74 74 69 6e 67 20 73 63 72  e by setting scr
95320 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20  atchAllocOut to 
95330 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61  1 when an alloca
95340 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74  tion.** is outst
95350 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20  anding clearing 
95360 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f  it when the allo
95370 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e  cation is freed.
95380 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  .*/.#if SQLITE_T
95390 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20  HREADSAFE==0 && 
953a0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
953b0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 72 61  .static int scra
953c0 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b  tchAllocOut = 0;
953d0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
953e0 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  Allocate memory 
953f0 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 75 73  that is to be us
95400 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20  ed and released 
95410 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54  right away..** T
95420 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73  his routine is s
95430 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61  imilar to alloca
95440 28 29 20 69 6e 20 74 68 61 74 20 69 74 20 69 73  () in that it is
95450 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a   not intended.**
95460 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20   for situations 
95470 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79  where the memory
95480 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c   might be held l
95490 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a  ong-term.  This.
954a0 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e  ** routine is in
954b0 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65  tended to get me
954c0 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67  mory to old larg
954d0 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61  e transient data
954e0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74  .** structures t
954f0 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f  hat would not no
95500 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68  rmally fit on th
95510 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a  e stack of an.**
95520 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73   embedded proces
95530 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sor..*/.SQLITE_P
95540 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
95550 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f  ite3ScratchMallo
95560 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  c(int n){.  void
95570 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e   *p;.  assert( n
95580 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54  >0 );..#if SQLIT
95590 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20  E_THREADSAFE==0 
955a0 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  && !defined(NDEB
955b0 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 79 20  UG).  /* Verify 
955c0 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61  that no more tha
955d0 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c  n one scratch al
955e0 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72  location per thr
955f0 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 74 73  ead.  ** is outs
95600 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74  tanding at one t
95610 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f  ime.  (This is o
95620 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74  nly checked in t
95630 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74  he.  ** single-t
95640 68 72 65 61 64 65 64 20 63 61 73 65 20 73 69 6e  hreaded case sin
95650 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74  ce checking in t
95660 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65  he multi-threade
95670 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f 75 6c  d case.  ** woul
95680 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63  d be much more c
95690 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a  omplicated.) */.
956a0 20 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63    assert( scratc
956b0 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a  hAllocOut==0 );.
956c0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71  #endif..  if( sq
956d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
956e0 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 29 7b  g.szScratch<n ){
956f0 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63  .    goto scratc
95700 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 65  h_overflow;.  }e
95710 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c 69 74  lse{  .    sqlit
95720 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
95730 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
95740 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63  if( mem0.nScratc
95750 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20  hFree==0 ){.    
95760 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
95770 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
95780 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 63  );.      goto sc
95790 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a  ratch_overflow;.
957a0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
957b0 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20   int i;.      i 
957c0 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46  = mem0.aScratchF
957d0 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61  ree[--mem0.nScra
957e0 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 20 20  tchFree];.      
957f0 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  i *= sqlite3Glob
95800 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74  alConfig.szScrat
95810 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ch;.      sqlite
95820 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
95830 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
95840 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 20 20  _USED, 1);.     
95850 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65   sqlite3StatusSe
95860 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t(SQLITE_STATUS_
95870 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29  SCRATCH_SIZE, n)
95880 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
95890 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
958a0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70  .mutex);.      p
958b0 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61   = (void*)&((cha
958c0 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  r*)sqlite3Global
958d0 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29  Config.pScratch)
958e0 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  [i];.      asser
958f0 74 28 20 20 28 28 28 75 38 2a 29 70 20 2d 20 28  t(  (((u8*)p - (
95900 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29  u8*)0) & 7)==0 )
95910 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 20  ;.    }.  }.#if 
95920 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
95930 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64  E==0 && !defined
95940 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74  (NDEBUG).  scrat
95950 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d  chAllocOut = p!=
95960 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74  0;.#endif..  ret
95970 75 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63 68 5f  urn p;..scratch_
95980 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20  overflow:.  if( 
95990 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
959a0 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a  fig.bMemstat ){.
959b0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
959c0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74  x_enter(mem0.mut
959d0 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ex);.    sqlite3
959e0 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45  StatusSet(SQLITE
959f0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
95a00 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20  SIZE, n);.    n 
95a10 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72  = mallocWithAlar
95a20 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66  m(n, &p);.    if
95a30 28 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61  ( p ) sqlite3Sta
95a40 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
95a50 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45  ATUS_SCRATCH_OVE
95a60 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73  RFLOW, n);.    s
95a70 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
95a80 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  ve(mem0.mutex);.
95a90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d    }else{.    p =
95aa0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
95ab0 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e  nfig.m.xMalloc(n
95ac0 29 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54  );.  }.#if SQLIT
95ad0 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20  E_THREADSAFE==0 
95ae0 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  && !defined(NDEB
95af0 55 47 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c  UG).  scratchAll
95b00 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65  ocOut = p!=0;.#e
95b10 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 70 3b  ndif.  return p;
95b20 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52      .}.SQLITE_PR
95b30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
95b40 65 33 53 63 72 61 74 63 68 46 72 65 65 28 76 6f  e3ScratchFree(vo
95b50 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  id *p){.  if( p 
95b60 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  ){..#if SQLITE_T
95b70 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20  HREADSAFE==0 && 
95b80 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
95b90 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74  .    /* Verify t
95ba0 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e  hat no more than
95bb0 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c   one scratch all
95bc0 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65  ocation per thre
95bd0 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75 74  ad.    ** is out
95be0 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20  standing at one 
95bf0 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20  time.  (This is 
95c00 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20  only checked in 
95c10 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67 6c  the.    ** singl
95c20 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20  e-threaded case 
95c30 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69  since checking i
95c40 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65  n the multi-thre
95c50 61 64 65 64 20 63 61 73 65 0a 20 20 20 20 2a 2a  aded case.    **
95c60 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d   would be much m
95c70 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e  ore complicated.
95c80 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  ) */.    assert(
95c90 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74   scratchAllocOut
95ca0 3d 3d 31 20 29 3b 0a 20 20 20 20 73 63 72 61 74  ==1 );.    scrat
95cb0 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a  chAllocOut = 0;.
95cc0 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20  #endif..    if( 
95cd0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
95ce0 66 69 67 2e 70 53 63 72 61 74 63 68 3d 3d 30 0a  fig.pScratch==0.
95cf0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c             || p<
95d00 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
95d10 66 69 67 2e 70 53 63 72 61 74 63 68 0a 20 20 20  fig.pScratch.   
95d20 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28 76          || p>=(v
95d30 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72 61 74  oid*)mem0.aScrat
95d40 63 68 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20  chFree ){.      
95d50 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
95d60 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
95d70 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
95d80 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d  iSize = sqlite3M
95d90 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20  allocSize(p);.  
95da0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
95db0 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
95dc0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73  utex);.        s
95dd0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
95de0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
95df0 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20  RATCH_OVERFLOW, 
95e00 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20  -iSize);.       
95e10 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
95e20 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
95e30 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53  MEMORY_USED, -iS
95e40 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ize);.        sq
95e50 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
95e60 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20  g.m.xFree(p);.  
95e70 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
95e80 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
95e90 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c  utex);.      }el
95ea0 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
95eb0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
95ec0 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20  m.xFree(p);.    
95ed0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
95ee0 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
95ef0 20 20 69 20 3d 20 28 69 6e 74 29 28 28 75 38 2a    i = (int)((u8*
95f00 29 70 20 2d 20 28 75 38 2a 29 73 71 6c 69 74 65  )p - (u8*)sqlite
95f10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53  3GlobalConfig.pS
95f20 63 72 61 74 63 68 29 3b 0a 20 20 20 20 20 20 69  cratch);.      i
95f30 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   /= sqlite3Globa
95f40 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
95f50 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  h;.      assert(
95f60 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74   i>=0 && i<sqlit
95f70 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
95f80 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20  Scratch );.     
95f90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
95fa0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
95fb0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
95fc0 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65  mem0.nScratchFre
95fd0 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c  e<(u32)sqlite3Gl
95fe0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
95ff0 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d  tch );.      mem
96000 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d  0.aScratchFree[m
96010 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65  em0.nScratchFree
96020 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73  ++] = i;.      s
96030 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
96040 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
96050 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b  RATCH_USED, -1);
96060 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
96070 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
96080 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20  mutex);.    }.  
96090 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20  }.}../*.** TRUE 
960a0 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73  if p is a lookas
960b0 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ide memory alloc
960c0 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f  ation from db.*/
960d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
960e0 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73  OMIT_LOOKASIDE.s
960f0 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b  tatic int isLook
96100 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64  aside(sqlite3 *d
96110 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72  b, void *p){.  r
96120 65 74 75 72 6e 20 64 62 20 26 26 20 70 20 26 26  eturn db && p &&
96130 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64   p>=db->lookasid
96140 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c 64 62  e.pStart && p<db
96150 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64  ->lookaside.pEnd
96160 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  ;.}.#else.#defin
96170 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41 2c  e isLookaside(A,
96180 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  B) 0.#endif../*.
96190 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69  ** Return the si
961a0 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61  ze of a memory a
961b0 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f  llocation previo
961c0 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72  usly obtained fr
961d0 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c  om.** sqlite3Mal
961e0 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33  loc() or sqlite3
961f0 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51  _malloc()..*/.SQ
96200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
96210 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69   sqlite3MallocSi
96220 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72  ze(void *p){.  r
96230 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
96240 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a  balConfig.m.xSiz
96250 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  e(p);.}.SQLITE_P
96260 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
96270 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73  e3DbMallocSize(s
96280 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
96290 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
962a0 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
962b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
962c0 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
962d0 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  p==0 ){.    retu
962e0 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 0;.  }else if
962f0 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62  ( isLookaside(db
96300 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , p) ){.    retu
96310 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  rn db->lookaside
96320 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  .sz;.  }else{.  
96330 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
96340 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
96350 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  Size(p);.  }.}..
96360 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72  /*.** Free memor
96370 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74  y previously obt
96380 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
96390 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53  e3Malloc()..*/.S
963a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
963b0 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64  qlite3_free(void
963c0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30   *p){.  if( p==0
963d0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
963e0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
963f0 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b  nfig.bMemstat ){
96400 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
96410 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
96420 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tex);.    sqlite
96430 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
96440 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
96450 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33 4d 61  USED, -sqlite3Ma
96460 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a 20 20  llocSize(p));.  
96470 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
96480 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29  onfig.m.xFree(p)
96490 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
964a0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
964b0 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  utex);.  }else{.
964c0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
964d0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28  lConfig.m.xFree(
964e0 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
964f0 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74 68 61   Free memory tha
96500 74 20 6d 69 67 68 74 20 62 65 20 61 73 73 6f 63  t might be assoc
96510 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72  iated with a par
96520 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
96530 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  .** connection..
96540 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
96550 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62  E void sqlite3Db
96560 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Free(sqlite3 *db
96570 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73  , void *p){.  as
96580 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73  sert( db==0 || s
96590 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
965a0 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
965b0 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64    if( isLookasid
965c0 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20  e(db, p) ){.    
965d0 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70  LookasideSlot *p
965e0 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65  Buf = (Lookaside
965f0 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 42 75  Slot*)p;.    pBu
96600 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c  f->pNext = db->l
96610 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 3b 0a  ookaside.pFree;.
96620 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
96630 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 3b 0a  e.pFree = pBuf;.
96640 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
96650 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73  e.nOut--;.  }els
96660 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  e{.    sqlite3_f
96670 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ree(p);.  }.}../
96680 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
96690 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74  size of an exist
966a0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
966b0 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ation.*/.SQLITE_
966c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
966d0 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69  lite3Realloc(voi
966e0 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79  d *pOld, int nBy
966f0 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64  tes){.  int nOld
96700 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a  , nNew;.  void *
96710 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64  pNew;.  if( pOld
96720 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
96730 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  n sqlite3Malloc(
96740 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69  nBytes);.  }.  i
96750 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 7c 7c 20  f( nBytes<=0 || 
96760 4e 45 56 45 52 28 6e 42 79 74 65 73 3e 3d 30 78  NEVER(nBytes>=0x
96770 37 66 66 66 66 66 30 30 29 20 29 7b 0a 20 20 20  7fffff00) ){.   
96780 20 2f 2a 20 54 68 65 20 4e 45 56 45 52 28 2e 2e   /* The NEVER(..
96790 2e 29 20 74 65 72 6d 20 69 73 20 65 78 70 6c 61  .) term is expla
967a0 69 6e 65 64 20 69 6e 20 63 6f 6d 6d 65 6e 74 73  ined in comments
967b0 20 6f 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   on sqlite3Mallo
967c0 63 28 29 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  c() */.    sqlit
967d0 65 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20  e3_free(pOld);. 
967e0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
967f0 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c 69 74 65  .  nOld = sqlite
96800 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4f 6c 64  3MallocSize(pOld
96810 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
96820 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
96830 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  mstat ){.    sql
96840 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
96850 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
96860 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53    sqlite3StatusS
96870 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  et(SQLITE_STATUS
96880 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42  _MALLOC_SIZE, nB
96890 79 74 65 73 29 3b 0a 20 20 20 20 6e 4e 65 77 20  ytes);.    nNew 
968a0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
968b0 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70  onfig.m.xRoundup
968c0 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66  (nBytes);.    if
968d0 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a  ( nOld==nNew ){.
968e0 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 4f 6c        pNew = pOl
968f0 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  d;.    }else{.  
96900 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
96910 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54  tatusValue(SQLIT
96920 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
96930 55 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20  USED)+nNew-nOld 
96940 3e 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  >= .            
96950 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68  mem0.alarmThresh
96960 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73  old ){.        s
96970 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72  qlite3MallocAlar
96980 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20  m(nNew-nOld);.  
96990 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77      }.      pNew
969a0 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
969b0 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f  Config.m.xReallo
969c0 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20  c(pOld, nNew);. 
969d0 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30       if( pNew==0
969e0 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61   && mem0.alarmCa
969f0 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20  llback ){.      
96a00 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41    sqlite3MallocA
96a10 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20 20  larm(nBytes);.  
96a20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
96a30 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
96a40 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64  .m.xRealloc(pOld
96a50 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 7d  , nNew);.      }
96a60 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20  .      if( pNew 
96a70 29 7b 0a 20 20 20 20 20 20 20 20 6e 4e 65 77 20  ){.        nNew 
96a80 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
96a90 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20  ize(pNew);.     
96aa0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
96ab0 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
96ac0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e  S_MEMORY_USED, n
96ad0 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 20  New-nOld);.     
96ae0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
96af0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
96b00 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
96b10 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20  }else{.    pNew 
96b20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
96b30 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63  onfig.m.xRealloc
96b40 28 70 4f 6c 64 2c 20 6e 42 79 74 65 73 29 3b 0a  (pOld, nBytes);.
96b50 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
96b60 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  w;.}../*.** The 
96b70 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65  public interface
96b80 20 74 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c   to sqlite3Reall
96b90 6f 63 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74  oc.  Make sure t
96ba0 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a  hat the memory.*
96bb0 2a 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69  * subsystem is i
96bc0 6e 69 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72  nitialized prior
96bd0 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c   to invoking sql
96be0 69 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53  iteRealloc..*/.S
96bf0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
96c00 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
96c10 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20  void *pOld, int 
96c20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  n){.#ifndef SQLI
96c30 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
96c40 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
96c50 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65  nitialize() ) re
96c60 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20  turn 0;.#endif. 
96c70 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52   return sqlite3R
96c80 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b  ealloc(pOld, n);
96c90 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .}.../*.** Alloc
96ca0 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d  ate and zero mem
96cb0 6f 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f  ory..*/ .SQLITE_
96cc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
96cd0 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
96ce0 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a  int n){.  void *
96cf0 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  p = sqlite3Mallo
96d00 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  c(n);.  if( p ){
96d10 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  .    memset(p, 0
96d20 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , n);.  }.  retu
96d30 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn p;.}../*.** A
96d40 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f  llocate and zero
96d50 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65   memory.  If the
96d60 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
96d70 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d  s, make.** the m
96d80 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
96d90 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69   in the connecti
96da0 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  on pointer..*/.S
96db0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
96dc0 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c  id *sqlite3DbMal
96dd0 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20  locZero(sqlite3 
96de0 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76  *db, int n){.  v
96df0 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  oid *p = sqlite3
96e00 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
96e10 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  n);.  if( p ){. 
96e20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20     memset(p, 0, 
96e30 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  n);.  }.  return
96e40 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c   p;.}../*.** All
96e50 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d  ocate and zero m
96e60 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61  emory.  If the a
96e70 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
96e80 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c   make.** the mal
96e90 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69  locFailed flag i
96ea0 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  n the connection
96eb0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
96ec0 49 66 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d  If db!=0 and db-
96ed0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73  >mallocFailed is
96ee0 20 74 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e   true (indicatin
96ef0 67 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63  g a prior malloc
96f00 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74  .** failure on t
96f10 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
96f20 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65   connection) the
96f30 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20  n always return 
96f40 30 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20  0..** Hence for 
96f50 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74  a particular dat
96f60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
96f70 2c 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74  , once malloc st
96f80 61 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c  arts.** failing,
96f90 20 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73   it fails consis
96fa0 74 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c  tently until mal
96fb0 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 72 65 73  locFailed is res
96fc0 65 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  et..** This is a
96fd0 6e 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75  n important assu
96fe0 6d 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61  mption.  There a
96ff0 72 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69  re many places i
97000 6e 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68  n the.** code th
97010 61 74 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b  at do things lik
97020 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
97030 20 20 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28        int *a = (
97040 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  int*)sqlite3DbMa
97050 6c 6c 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29  llocRaw(db, 100)
97060 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74  ;.**         int
97070 20 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69   *b = (int*)sqli
97080 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
97090 62 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20  b, 200);.**     
970a0 20 20 20 20 69 66 28 20 62 20 29 20 61 5b 31 30      if( b ) a[10
970b0 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20  ] = 9;.**.** In 
970c0 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
970d0 61 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c  a subsequent mal
970e0 6c 6f 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f  loc (ex: "b") wo
970f0 72 6b 65 64 2c 20 69 74 20 69 73 20 61 73 73 75  rked, it is assu
97100 6d 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20  med.** that all 
97110 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65  prior mallocs (e
97120 78 3a 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74  x: "a") worked t
97130 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  oo..*/.SQLITE_PR
97140 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
97150 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73  te3DbMallocRaw(s
97160 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
97170 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  n){.  void *p;. 
97180 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c   assert( db==0 |
97190 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
971a0 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
971b0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
971c0 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45  E_OMIT_LOOKASIDE
971d0 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20 20 20  .  if( db ){.   
971e0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a   LookasideSlot *
971f0 70 42 75 66 3b 0a 20 20 20 20 69 66 28 20 64 62  pBuf;.    if( db
97200 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
97210 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
97220 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
97230 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
97240 6e 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64 62 2d  nabled && n<=db-
97250 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a 20 20  >lookaside.sz.  
97260 20 20 20 20 20 20 20 26 26 20 28 70 42 75 66 20         && (pBuf 
97270 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  = db->lookaside.
97280 70 46 72 65 65 29 21 3d 30 20 29 7b 0a 20 20 20  pFree)!=0 ){.   
97290 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
972a0 2e 70 46 72 65 65 20 3d 20 70 42 75 66 2d 3e 70  .pFree = pBuf->p
972b0 4e 65 78 74 3b 0a 20 20 20 20 20 20 64 62 2d 3e  Next;.      db->
972c0 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b  lookaside.nOut++
972d0 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e  ;.      if( db->
972e0 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3e 64  lookaside.nOut>d
972f0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f  b->lookaside.mxO
97300 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62  ut ){.        db
97310 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75  ->lookaside.mxOu
97320 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  t = db->lookasid
97330 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  e.nOut;.      }.
97340 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f        return (vo
97350 69 64 2a 29 70 42 75 66 3b 0a 20 20 20 20 7d 0a  id*)pBuf;.    }.
97360 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20    }.#else.  if( 
97370 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63  db && db->malloc
97380 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
97390 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 6e 64  turn 0;.  }.#end
973a0 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  if.  p = sqlite3
973b0 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28  Malloc(n);.  if(
973c0 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20 20 20   !p && db ){.   
973d0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
973e0 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  d = 1;.  }.  ret
973f0 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
97400 52 65 73 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b  Resize the block
97410 20 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74   of memory point
97420 65 64 20 74 6f 20 62 79 20 70 20 74 6f 20 6e 20  ed to by p to n 
97430 62 79 74 65 73 2e 20 49 66 20 74 68 65 0a 2a 2a  bytes. If the.**
97440 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c 20 73   resize fails, s
97450 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69  et the mallocFai
97460 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20  led flag in the 
97470 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63  connection objec
97480 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
97490 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
974a0 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69  e3DbRealloc(sqli
974b0 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70  te3 *db, void *p
974c0 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  , int n){.  void
974d0 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 61 73   *pNew = 0;.  as
974e0 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20  sert( db!=0 );. 
974f0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
97500 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
97510 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
97520 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
97530 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
97540 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
97550 75 72 6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  urn sqlite3DbMal
97560 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20  locRaw(db, n);. 
97570 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4c     }.    if( isL
97580 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20  ookaside(db, p) 
97590 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3c 3d  ){.      if( n<=
975a0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a  db->lookaside.sz
975b0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
975c0 72 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn p;.      }.  
975d0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
975e0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
975f0 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , n);.      if( 
97600 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
97610 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20  memcpy(pNew, p, 
97620 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a  db->lookaside.sz
97630 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
97640 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b  e3DbFree(db, p);
97650 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
97660 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d  se{.      pNew =
97670 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
97680 28 70 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66  (p, n);.      if
97690 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  ( !pNew ){.     
976a0 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
976b0 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  led = 1;.      }
976c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
976d0 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
976e0 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65  ** Attempt to re
976f0 61 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49 66 20  allocate p.  If 
97700 74 68 65 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e  the reallocation
97710 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 66 72 65   fails, then fre
97720 65 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74  e p.** and set t
97730 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
97740 66 6c 61 67 20 69 6e 20 74 68 65 20 64 61 74 61  flag in the data
97750 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
97760 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
97770 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
97780 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
97790 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69  sqlite3 *db, voi
977a0 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  d *p, int n){.  
977b0 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e  void *pNew;.  pN
977c0 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  ew = sqlite3DbRe
977d0 61 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b  alloc(db, p, n);
977e0 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a  .  if( !pNew ){.
977f0 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
97800 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20  e(db, p);.  }.  
97810 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a  return pNew;.}..
97820 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70  /*.** Make a cop
97830 79 20 6f 66 20 61 20 73 74 72 69 6e 67 20 69 6e  y of a string in
97840 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
97850 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c   from sqliteMall
97860 6f 63 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20  oc(). These .** 
97870 66 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73  functions call s
97880 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28  qlite3MallocRaw(
97890 29 20 64 69 72 65 63 74 6c 79 20 69 6e 73 74 65  ) directly inste
978a0 61 64 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c  ad of sqliteMall
978b0 6f 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73  oc(). This.** is
978c0 20 62 65 63 61 75 73 65 20 77 68 65 6e 20 6d 65   because when me
978d0 6d 6f 72 79 20 64 65 62 75 67 67 69 6e 67 20 69  mory debugging i
978e0 73 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65  s turned on, the
978f0 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73  se two functions
97900 20 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20   are .** called 
97910 76 69 61 20 6d 61 63 72 6f 73 20 74 68 61 74 20  via macros that 
97920 72 65 63 6f 72 64 20 74 68 65 20 63 75 72 72 65  record the curre
97930 6e 74 20 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65  nt file and line
97940 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a   number in the.*
97950 2a 20 54 68 72 65 61 64 44 61 74 61 20 73 74 72  * ThreadData str
97960 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
97970 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
97980 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
97990 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
979a0 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63  st char *z){.  c
979b0 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a  har *zNew;.  siz
979c0 65 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d  e_t n;.  if( z==
979d0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
979e0 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c  0;.  }.  n = sql
979f0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20  ite3Strlen30(z) 
97a00 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28  + 1;.  assert( (
97a10 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e  n&0x7fffffff)==n
97a20 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c   );.  zNew = sql
97a30 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
97a40 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69  db, (int)n);.  i
97a50 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d  f( zNew ){.    m
97a60 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e  emcpy(zNew, z, n
97a70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
97a80 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  zNew;.}.SQLITE_P
97a90 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
97aa0 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 73 71  ite3DbStrNDup(sq
97ab0 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
97ac0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
97ad0 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a  {.  char *zNew;.
97ae0 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20    if( z==0 ){.  
97af0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
97b00 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37    assert( (n&0x7
97b10 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20  fffffff)==n );. 
97b20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   zNew = sqlite3D
97b30 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e  bMallocRaw(db, n
97b40 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20  +1);.  if( zNew 
97b50 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e  ){.    memcpy(zN
97b60 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a  ew, z, n);.    z
97b70 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a  New[n] = 0;.  }.
97b80 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d    return zNew;.}
97b90 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
97ba0 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65   string from the
97bb0 20 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e   zFromat argumen
97bc0 74 20 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73  t and the va_lis
97bd0 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a  t that follows..
97be0 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 73 74 72  ** Store the str
97bf0 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62  ing in memory ob
97c00 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
97c10 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d  teMalloc() and m
97c20 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74  ake *pz.** point
97c30 20 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e   to that string.
97c40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
97c50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
97c60 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a  etString(char **
97c70 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  pz, sqlite3 *db,
97c80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
97c90 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
97ca0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
97cb0 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74   *z;..  va_start
97cc0 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
97cd0 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72   z = sqlite3VMPr
97ce0 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74  intf(db, zFormat
97cf0 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
97d00 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  ap);.  sqlite3Db
97d10 46 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20  Free(db, *pz);. 
97d20 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a   *pz = z;.}.../*
97d30 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
97d40 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  n must be called
97d50 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
97d60 61 6e 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  any API function
97d70 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72   (i.e. .** retur
97d80 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20  ning control to 
97d90 74 68 65 20 75 73 65 72 29 20 74 68 61 74 20 68  the user) that h
97da0 61 73 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65  as called sqlite
97db0 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73  3_malloc or.** s
97dc0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a  qlite3_realloc..
97dd0 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
97de0 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d  ed value is norm
97df0 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74  ally a copy of t
97e00 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
97e10 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75  nt to this.** fu
97e20 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c  nction. However,
97e30 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   if a malloc() f
97e40 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72  ailure has occur
97e50 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 70 72  red since the pr
97e60 65 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61  evious.** invoca
97e70 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  tion SQLITE_NOME
97e80 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  M is returned in
97e90 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  stead. .**.** If
97ea0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
97eb0 65 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20  ent, db, is not 
97ec0 4e 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f  NULL and a mallo
97ed0 63 28 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63  c() error has oc
97ee0 63 75 72 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20  curred,.** then 
97ef0 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65  the connection e
97f00 72 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76  rror-code (the v
97f10 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
97f20 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
97f30 28 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  ()).** is set to
97f40 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a   SQLITE_NOMEM..*
97f50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
97f60 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45   int sqlite3ApiE
97f70 78 69 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c  xit(sqlite3* db,
97f80 20 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49   int rc){.  /* I
97f90 66 20 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20  f the db handle 
97fa0 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
97fb0 6e 20 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74  n we must hold t
97fc0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  he connection ha
97fd0 6e 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20  ndle.  ** mutex 
97fe0 68 65 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20  here. Otherwise 
97ff0 74 68 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f  the read (and po
98000 73 73 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66  ssible write) of
98010 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
98020 64 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66  d .  ** is unsaf
98030 65 2c 20 61 73 20 69 73 20 74 68 65 20 63 61 6c  e, as is the cal
98040 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f  l to sqlite3Erro
98050 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  r()..  */.  asse
98060 72 74 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74  rt( !db || sqlit
98070 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
98080 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
98090 28 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c  ( db && (db->mal
980a0 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d  locFailed || rc=
980b0 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f  =SQLITE_IOERR_NO
980c0 4d 45 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  MEM) ){.    sqli
980d0 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
980e0 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20  ITE_NOMEM, 0);. 
980f0 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
98100 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20  led = 0;.    rc 
98110 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
98120 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20    }.  return rc 
98130 26 20 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d  & (db ? db->errM
98140 61 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a  ask : 0xff);.}..
98150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
98160 45 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20  End of malloc.c 
98170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
981a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
981b0 42 65 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74  Begin file print
981c0 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.c ************
981d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
981e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
981f0 2f 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74  /*.** The "print
98200 66 22 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c  f" code that fol
98210 6c 6f 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20  lows dates from 
98220 74 68 65 20 31 39 38 30 27 73 2e 20 20 49 74 20  the 1980's.  It 
98230 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62  is in.** the pub
98240 6c 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65  lic domain.  The
98250 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e   original commen
98260 74 73 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20  ts are included 
98270 68 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70  here for.** comp
98280 6c 65 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20  leteness.  They 
98290 61 72 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d  are very out-of-
982a0 64 61 74 65 20 62 75 74 20 6d 69 67 68 74 20 62  date but might b
982b0 65 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61  e useful as.** a
982c0 6e 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66  n historical ref
982d0 65 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66  erence.  Most of
982e0 20 74 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e   the "enhancemen
982f0 74 73 22 20 68 61 76 65 20 62 65 65 6e 20 62 61  ts" have been ba
98300 63 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74  cked.** out so t
98310 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  hat the function
98320 61 6c 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65  ality is now the
98330 20 73 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72   same as standar
98340 64 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a  d printf()..**.*
98350 2a 20 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c  * $Id: printf.c,
98360 76 20 31 2e 31 30 33 20 32 30 30 39 2f 30 35 2f  v 1.103 2009/05/
98370 30 34 20 32 30 3a 32 30 3a 31 36 20 64 72 68 20  04 20:20:16 drh 
98380 45 78 70 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  Exp $.**.*******
98390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
983a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
983b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
983c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
983d0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ***.**.** The fo
983e0 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20  llowing modules 
983f0 69 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72  is an enhanced r
98400 65 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74  eplacement for t
98410 68 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72  he "printf" subr
98420 6f 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64  outines.** found
98430 20 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64   in the standard
98440 20 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65   C library.  The
98450 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e   following enhan
98460 63 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73  cements are.** s
98470 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20  upported:.**.** 
98480 20 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e       +  Addition
98490 61 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  al functions.  T
984a0 68 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20  he standard set 
984b0 6f 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63  of "printf" func
984c0 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  tions.**        
984d0 20 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66   includes printf
984e0 2c 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e  , fprintf, sprin
984f0 74 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70  tf, vprintf, vfp
98500 72 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20  rintf, and.**   
98510 20 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20        vsprintf. 
98520 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64   This module add
98530 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  s the following:
98540 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
98550 20 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20   *  snprintf -- 
98560 57 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e  Works like sprin
98570 74 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65  tf, but has an e
98580 78 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  xtra argument.**
98590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
985a0 20 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20            which 
985b0 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
985c0 68 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65  he buffer writte
985d0 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  n to..**.**     
985e0 20 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66        *  mprintf
985f0 20 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20   --  Similar to 
98600 73 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73  sprintf.  Writes
98610 20 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72   output to memor
98620 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  y.**            
98630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62                ob
98640 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
98650 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  oc..**.**       
98660 20 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d      *  xprintf -
98670 2d 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74  -  Calls a funct
98680 69 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f  ion to dispose o
98690 66 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20  f output..**.** 
986a0 20 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72            *  npr
986b0 69 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70  intf --  No outp
986c0 75 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20  ut, but returns 
986d0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
986e0 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20  aracters.**     
986f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98700 20 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20       that would 
98710 68 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74  have been output
98720 20 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a   by printf..**.*
98730 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41  *           *  A
98740 20 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a   v- version (ex:
98750 20 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65   vsnprintf) of e
98760 76 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73  very function is
98770 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20   also.**        
98780 20 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a        supplied..
98790 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20  **.**      +  A 
987a0 66 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74  few extensions t
987b0 6f 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67  o the formatting
987c0 20 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75   notation are su
987d0 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  pported:.**.**  
987e0 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20           *  The 
987f0 22 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61  "=" flag (simila
98800 72 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73  r to "-") causes
98810 20 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62   the output to b
98820 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
98830 20 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e    be centered in
98840 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
98850 6c 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a  ly sized field..
98860 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
98870 2a 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20  *  The %b field 
98880 6f 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67  outputs an integ
98890 65 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74  er in binary not
988a0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ation..**.**    
988b0 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63         *  The %c
988c0 20 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70   field now accep
988d0 74 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20  ts a precision. 
988e0 20 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f   The character o
988f0 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20  utput.**        
98900 20 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65        is repeate
98910 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20  d by the number 
98920 6f 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65  of times the pre
98930 63 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73  cision specifies
98940 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..**.**         
98950 20 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c    *  The %' fiel
98960 64 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c  d works like %c,
98970 20 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74   but takes as it
98980 73 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a  s character the.
98990 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
989a0 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f  next character o
989b0 66 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72  f the format str
989c0 69 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20  ing, instead of 
989d0 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20  the next.**     
989e0 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e           argumen
989f0 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  t.  For example,
98a00 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d    printf("%.78'-
98a10 22 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69  ")  prints 78 mi
98a20 6e 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nus.**          
98a30 20 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73      signs, the s
98a40 61 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22  ame as  printf("
98a50 25 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a  %.78c",'-')..**.
98a60 2a 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20  **      +  When 
98a70 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47  compiled using G
98a80 43 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74  CC on a SPARC, t
98a90 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70  his version of p
98aa0 72 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20  rintf is.**     
98ab0 20 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20      faster than 
98ac0 74 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e  the library prin
98ad0 74 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e  tf for SUN OS 4.
98ae0 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20  1..**.**      + 
98af0 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61   All functions a
98b00 72 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61  re fully reentra
98b10 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  nt..**.*/../*.**
98b20 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65   Conversion type
98b30 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69  s fall into vari
98b40 6f 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61  ous categories a
98b50 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65  s defined by the
98b60 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e  .** following en
98b70 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64  umeration..*/.#d
98b80 65 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20  efine etRADIX   
98b90 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72      1 /* Integer
98ba0 20 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c   types.  %d, %x,
98bb0 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74   %o, and so fort
98bc0 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46  h */.#define etF
98bd0 4c 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20  LOAT       2 /* 
98be0 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20  Floating point. 
98bf0 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %f */.#define e
98c00 74 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f  tEXP         3 /
98c10 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20  * Exponentional 
98c20 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64  notation. %e and
98c30 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %E */.#define e
98c40 74 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f  tGENERIC     4 /
98c50 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78  * Floating or ex
98c60 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e  ponential, depen
98c70 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74  ding on exponent
98c80 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  . %g */.#define 
98c90 65 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20  etSIZE        5 
98ca0 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72  /* Return number
98cb0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70   of characters p
98cc0 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e  rocessed so far.
98cd0 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %n */.#define e
98ce0 74 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f  tSTRING      6 /
98cf0 2a 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f  * Strings. %s */
98d00 0a 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54  .#define etDYNST
98d10 52 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61  RING   7 /* Dyna
98d20 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
98d30 64 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f  d strings. %z */
98d40 0a 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45  .#define etPERCE
98d50 4e 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63  NT     8 /* Perc
98d60 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a  ent symbol. %% *
98d70 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52  /.#define etCHAR
98d80 58 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61  X       9 /* Cha
98d90 72 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f  racters. %c */./
98da0 2a 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65  * The rest are e
98db0 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e  xtensions, not n
98dc0 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e  ormally found in
98dd0 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65   printf() */.#de
98de0 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45  fine etSQLESCAPE
98df0 20 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20    10 /* Strings 
98e00 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65  with '\'' double
98e10 64 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e  d.  %q */.#defin
98e20 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31  e etSQLESCAPE2 1
98e30 31 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74  1 /* Strings wit
98e40 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61  h '\'' doubled a
98e50 6e 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27  nd enclosed in '
98e60 27 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ',.             
98e70 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c               NUL
98e80 4c 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61  L pointers repla
98e90 63 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e  ced by SQL NULL.
98ea0 20 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20    %Q */.#define 
98eb0 65 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20  etTOKEN      12 
98ec0 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  /* a pointer to 
98ed0 61 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72  a Token structur
98ee0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  e */.#define etS
98ef0 52 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20  RCLIST    13 /* 
98f00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53  a pointer to a S
98f10 72 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e  rcList */.#defin
98f20 65 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31  e etPOINTER    1
98f30 34 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76  4 /* The %p conv
98f40 65 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  ersion */.#defin
98f50 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31  e etSQLESCAPE3 1
98f60 35 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e  5 /* %w -> Strin
98f70 67 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75  gs with '\"' dou
98f80 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  bled */.#define 
98f90 65 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20  etORDINAL    16 
98fa0 2f 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e  /* %r -> 1st, 2n
98fb0 64 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63  d, 3rd, 4th, etc
98fc0 2e 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20  .  English only 
98fd0 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 65 74 49 4e  */..#define etIN
98fe0 56 41 4c 49 44 20 20 20 20 20 30 20 2f 2a 20 41  VALID     0 /* A
98ff0 6e 79 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20  ny unrecognized 
99000 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20  conversion type 
99010 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65  */.../*.** An "e
99020 74 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62  tByte" is an 8-b
99030 69 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75  it unsigned valu
99040 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e  e..*/.typedef un
99050 73 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79  signed char etBy
99060 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  te;../*.** Each 
99070 62 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69  builtin conversi
99080 6f 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78  on character (ex
99090 3a 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64  : the 'd' in "%d
990a0 22 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a  ") is described.
990b0 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ** by an instanc
990c0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
990d0 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a  ng structure.*/.
990e0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65  typedef struct e
990f0 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e  t_info {   /* In
99100 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
99110 65 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c  each format fiel
99120 64 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74  d */.  char fmtt
99130 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ype;            
99140 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69  /* The format fi
99150 65 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20  eld code letter 
99160 2a 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65  */.  etByte base
99170 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
99180 20 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61   The base for ra
99190 64 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a  dix conversion *
991a0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73  /.  etByte flags
991b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
991c0 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46  One or more of F
991d0 4c 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62  LAG_ constants b
991e0 65 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65  elow */.  etByte
991f0 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20   type;          
99200 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e     /* Conversion
99210 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65   paradigm */.  e
99220 74 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20  tByte charset;  
99230 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
99240 74 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d  t into aDigits[]
99250 20 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73   of the digits s
99260 74 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74  tring */.  etByt
99270 65 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20  e prefix;       
99280 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
99290 74 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20  to aPrefix[] of 
992a0 74 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e  the prefix strin
992b0 67 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a  g */.} et_info;.
992c0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
992d0 61 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66  alues for et_inf
992e0 6f 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69  o.flags.*/.#defi
992f0 6e 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20  ne FLAG_SIGNED  
99300 31 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  1     /* True if
99310 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f   the value to co
99320 6e 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20  nvert is signed 
99330 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f  */.#define FLAG_
99340 49 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a  INTERN  2     /*
99350 20 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74   True if for int
99360 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a  ernal use only *
99370 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53  /.#define FLAG_S
99380 54 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20  TRING  4     /* 
99390 41 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70  Allow infinity p
993a0 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a  recision */.../*
993b0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
993c0 67 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63  g table is searc
993d0 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f  hed linearly, so
993e0 20 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70   it is good to p
993f0 75 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66  ut the.** most f
99400 72 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63  requently used c
99410 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20  onversion types 
99420 66 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  first..*/.static
99430 20 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67   const char aDig
99440 69 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36  its[] = "0123456
99450 37 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36  789ABCDEF0123456
99460 37 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74  789abcdef";.stat
99470 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50  ic const char aP
99480 72 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30  refix[] = "-x0\0
99490 30 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f  00X0";.static co
994a0 6e 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69  nst et_info fmti
994b0 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27  nfo[] = {.  {  '
994c0 64 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44  d', 10, 1, etRAD
994d0 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  IX,      0,  0 }
994e0 2c 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20  ,.  {  's',  0, 
994f0 34 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20  4, etSTRING,    
99500 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
99510 67 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e  g',  0, 1, etGEN
99520 45 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d  ERIC,    30, 0 }
99530 2c 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20  ,.  {  'z',  0, 
99540 34 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20  4, etDYNSTRING, 
99550 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
99560 71 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c  q',  0, 4, etSQL
99570 45 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d  ESCAPE,  0,  0 }
99580 2c 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20  ,.  {  'Q',  0, 
99590 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c  4, etSQLESCAPE2,
995a0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
995b0 77 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c  w',  0, 4, etSQL
995c0 45 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d  ESCAPE3, 0,  0 }
995d0 2c 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20  ,.  {  'c',  0, 
995e0 30 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20  0, etCHARX,     
995f0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
99600 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44  o',  8, 0, etRAD
99610 49 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d  IX,      0,  2 }
99620 2c 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20  ,.  {  'u', 10, 
99630 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20  0, etRADIX,     
99640 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
99650 78 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44  x', 16, 0, etRAD
99660 49 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d  IX,      16, 1 }
99670 2c 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20  ,.  {  'X', 16, 
99680 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20  0, etRADIX,     
99690 20 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65   0,  4 },.#ifnde
996a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
996b0 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b  OATING_POINT.  {
996c0 20 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74    'f',  0, 1, et
996d0 46 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20  FLOAT,      0,  
996e0 30 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20  0 },.  {  'e',  
996f0 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20  0, 1, etEXP,    
99700 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b      30, 0 },.  {
99710 20 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74    'E',  0, 1, et
99720 45 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20  EXP,        14, 
99730 30 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20  0 },.  {  'G',  
99740 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c  0, 1, etGENERIC,
99750 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e      14, 0 },.#en
99760 64 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30  dif.  {  'i', 10
99770 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20  , 1, etRADIX,   
99780 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20     0,  0 },.  { 
99790 20 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53   'n',  0, 0, etS
997a0 49 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30  IZE,       0,  0
997b0 20 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30   },.  {  '%',  0
997c0 2c 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20  , 0, etPERCENT, 
997d0 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20     0,  0 },.  { 
997e0 20 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50   'p', 16, 0, etP
997f0 4f 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31  OINTER,    0,  1
99800 20 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20   },../* All the 
99810 72 65 73 74 20 68 61 76 65 20 74 68 65 20 46 4c  rest have the FL
99820 41 47 5f 49 4e 54 45 52 4e 20 62 69 74 20 73 65  AG_INTERN bit se
99830 74 20 61 6e 64 20 61 72 65 20 74 68 75 73 20 66  t and are thus f
99840 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
99850 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20  se only */.  {  
99860 27 54 27 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f  'T',  0, 2, etTO
99870 4b 45 4e 2c 20 20 20 20 20 20 30 2c 20 20 30 20  KEN,      0,  0 
99880 7d 2c 0a 20 20 7b 20 20 27 53 27 2c 20 20 30 2c  },.  {  'S',  0,
99890 20 32 2c 20 65 74 53 52 43 4c 49 53 54 2c 20 20   2, etSRCLIST,  
998a0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
998b0 27 72 27 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52  'r', 10, 3, etOR
998c0 44 49 4e 41 4c 2c 20 20 20 20 30 2c 20 20 30 20  DINAL,    0,  0 
998d0 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  },.};../*.** If 
998e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
998f0 54 49 4e 47 5f 50 4f 49 4e 54 20 69 73 20 64 65  TING_POINT is de
99900 66 69 6e 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65  fined, then none
99910 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67   of the floating
99920 20 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72   point.** conver
99930 73 69 6f 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e  sions will work.
99940 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
99950 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
99960 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76  _POINT./*.** "*v
99970 61 6c 22 20 69 73 20 61 20 64 6f 75 62 6c 65 20  al" is a double 
99980 73 75 63 68 20 74 68 61 74 20 30 2e 31 20 3c 3d  such that 0.1 <=
99990 20 2a 76 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20   *val < 10.0.** 
999a0 52 65 74 75 72 6e 20 74 68 65 20 61 73 63 69 69  Return the ascii
999b0 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65   code for the le
999c0 61 64 69 6e 67 20 64 69 67 69 74 20 6f 66 20 2a  ading digit of *
999d0 76 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c  val, then.** mul
999e0 74 69 70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20  tiply "*val" by 
999f0 31 30 2e 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c  10.0 to renormal
99a00 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70  ize..**.** Examp
99a10 6c 65 3a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74  le:.**     input
99a20 3a 20 20 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31  :     *val = 3.1
99a30 34 31 35 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70  4159.**     outp
99a40 75 74 3a 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e  ut:    *val = 1.
99a50 34 31 35 39 20 20 20 20 66 75 6e 63 74 69 6f 6e  4159    function
99a60 20 72 65 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a   return = '3'.**
99a70 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 65 72 20  .** The counter 
99a80 2a 63 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e  *cnt is incremen
99a90 74 65 64 20 65 61 63 68 20 74 69 6d 65 2e 20 20  ted each time.  
99aa0 41 66 74 65 72 20 63 6f 75 6e 74 65 72 20 65 78  After counter ex
99ab0 63 65 65 64 73 0a 2a 2a 20 31 36 20 28 74 68 65  ceeds.** 16 (the
99ac0 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 67 6e 69   number of signi
99ad0 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 69 6e  ficant digits in
99ae0 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 29   a 64-bit float)
99af0 20 27 30 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79   '0' is.** alway
99b00 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  s returned..*/.s
99b10 74 61 74 69 63 20 63 68 61 72 20 65 74 5f 67 65  tatic char et_ge
99b20 74 64 69 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c  tdigit(LONGDOUBL
99b30 45 5f 54 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74  E_TYPE *val, int
99b40 20 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69   *cnt){.  int di
99b50 67 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c  git;.  LONGDOUBL
99b60 45 5f 54 59 50 45 20 64 3b 0a 20 20 69 66 28 20  E_TYPE d;.  if( 
99b70 28 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29  (*cnt)++ >= 16 )
99b80 20 72 65 74 75 72 6e 20 27 30 27 3b 0a 20 20 64   return '0';.  d
99b90 69 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c  igit = (int)*val
99ba0 3b 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20  ;.  d = digit;. 
99bb0 20 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20   digit += '0';. 
99bc0 20 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20   *val = (*val - 
99bd0 64 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72  d)*10.0;.  retur
99be0 6e 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d  n (char)digit;.}
99bf0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
99c00 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
99c10 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  POINT */../*.** 
99c20 41 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63  Append N space c
99c30 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65  haracters to the
99c40 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75   given string bu
99c50 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ffer..*/.static 
99c60 76 6f 69 64 20 61 70 70 65 6e 64 53 70 61 63 65  void appendSpace
99c70 28 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75  (StrAccum *pAccu
99c80 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61  m, int N){.  sta
99c90 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  tic const char z
99ca0 53 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20  Spaces[] = "    
99cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99cc0 20 20 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68           ";.  wh
99cd0 69 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a  ile( N>=(int)siz
99ce0 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29  eof(zSpaces)-1 )
99cf0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72  {.    sqlite3Str
99d00 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63  AccumAppend(pAcc
99d10 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a  um, zSpaces, siz
99d20 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b  eof(zSpaces)-1);
99d30 0a 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66  .    N -= sizeof
99d40 28 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d  (zSpaces)-1;.  }
99d50 0a 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20  .  if( N>0 ){.  
99d60 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
99d70 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20  mAppend(pAccum, 
99d80 7a 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d  zSpaces, N);.  }
99d90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63  .}../*.** On mac
99da0 68 69 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61  hines with a sma
99db0 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79  ll stack size, y
99dc0 6f 75 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20  ou can redefine 
99dd0 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52  the.** SQLITE_PR
99de0 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20  INT_BUF_SIZE to 
99df0 62 65 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30  be less than 350
99e00 2e 20 20 42 75 74 20 62 65 77 61 72 65 20 2d 20  .  But beware - 
99e10 66 6f 72 0a 2a 2a 20 73 6d 61 6c 6c 65 72 20 76  for.** smaller v
99e20 61 6c 75 65 73 20 73 6f 6d 65 20 25 66 20 63 6f  alues some %f co
99e30 6e 76 65 72 73 69 6f 6e 73 20 6d 61 79 20 67 6f  nversions may go
99e40 20 69 6e 74 6f 20 61 6e 20 69 6e 66 69 6e 69 74   into an infinit
99e50 65 20 6c 6f 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64  e loop..*/.#ifnd
99e60 65 66 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f  ef SQLITE_PRINT_
99e70 42 55 46 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e  BUF_SIZE.# defin
99e80 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42  e SQLITE_PRINT_B
99e90 55 46 5f 53 49 5a 45 20 33 35 30 0a 23 65 6e 64  UF_SIZE 350.#end
99ea0 69 66 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46  if.#define etBUF
99eb0 53 49 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e  SIZE SQLITE_PRIN
99ec0 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53  T_BUF_SIZE  /* S
99ed0 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75  ize of the outpu
99ee0 74 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a  t buffer */../*.
99ef0 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67  ** The root prog
99f00 72 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74  ram.  All variat
99f10 69 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63  ions call this c
99f20 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54  ore..**.** INPUT
99f30 53 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54  S:.**   func   T
99f40 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72  his is a pointer
99f50 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74   to a function t
99f60 61 6b 69 6e 67 20 74 68 72 65 65 20 61 72 67 75  aking three argu
99f70 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20  ments.**        
99f80 20 20 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72      1. A pointer
99f90 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53   to anything.  S
99fa0 61 6d 65 20 61 73 20 74 68 65 20 22 61 72 67 22  ame as the "arg"
99fb0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20   parameter..**  
99fc0 20 20 20 20 20 20 20 20 20 20 32 2e 20 41 20 70            2. A p
99fd0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69  ointer to the li
99fe0 73 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  st of characters
99ff0 20 74 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a   to be output.**
9a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
9a010 4e 6f 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20  Note, this list 
9a020 69 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d  is NOT null term
9a030 69 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20  inated.).**     
9a040 20 20 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74         3. An int
9a050 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63  eger number of c
9a060 68 61 72 61 63 74 65 72 73 20 74 6f 20 62 65 20  haracters to be 
9a070 6f 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20  output..**      
9a080 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20           (Note: 
9a090 54 68 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68  This number migh
9a0a0 74 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a  t be zero.).**.*
9a0b0 2a 20 20 20 61 72 67 20 20 20 20 54 68 69 73 20  *   arg    This 
9a0c0 69 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  is the pointer t
9a0d0 6f 20 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68  o anything which
9a0e0 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20   will be passed 
9a0f0 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  as the.**       
9a100 20 20 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e     first argumen
9a110 74 20 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73  t to "func".  Us
9a120 65 20 69 74 20 66 6f 72 20 77 68 61 74 65 76 65  e it for whateve
9a130 72 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a  r you like..**.*
9a140 2a 20 20 20 66 6d 74 20 20 20 20 54 68 69 73 20  *   fmt    This 
9a150 69 73 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74  is the format st
9a160 72 69 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20  ring, as in the 
9a170 75 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a  usual print..**.
9a180 2a 2a 20 20 20 61 70 20 20 20 20 20 54 68 69 73  **   ap     This
9a190 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
9a1a0 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d   a list of argum
9a1b0 65 6e 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69  ents.  Same as i
9a1c0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66  n.**          vf
9a1d0 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54  print..**.** OUT
9a1e0 50 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20  PUTS:.**        
9a1f0 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
9a200 75 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20  ue is the total 
9a210 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
9a220 74 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20  ters sent to.** 
9a230 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e           the fun
9a240 63 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52  ction "func".  R
9a250 65 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65  eturns -1 on a e
9a260 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  rror..**.** Note
9a270 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20   that the order 
9a280 69 6e 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74  in which automat
9a290 69 63 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ic variables are
9a2a0 20 64 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a   declared below.
9a2b0 2a 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65  ** seems to make
9a2c0 20 61 20 62 69 67 20 64 69 66 66 65 72 65 6e 63   a big differenc
9a2d0 65 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67  e in determining
9a2e0 20 68 6f 77 20 66 61 73 74 20 74 68 69 73 20 62   how fast this b
9a2f0 65 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e  east.** will run
9a300 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
9a310 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
9a320 56 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41  VXPrintf(.  StrA
9a330 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20  ccum *pAccum,   
9a340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
9a350 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73  * Accumulate res
9a360 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69  ults here */.  i
9a370 6e 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20  nt useExtended, 
9a380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a390 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e    /* Allow exten
9a3a0 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  ded %-conversion
9a3b0 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
9a3c0 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20  r *fmt,         
9a3d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
9a3e0 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  mat string */.  
9a3f0 76 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20  va_list ap      
9a400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a410 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20     /* arguments 
9a420 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20  */.){.  int c;  
9a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a440 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61     /* Next chara
9a450 63 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d  cter in the form
9a460 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63  at string */.  c
9a470 68 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20  har *bufpt;     
9a480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
9a490 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76  nter to the conv
9a4a0 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f  ersion buffer */
9a4b0 0a 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e  .  int precision
9a4c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
9a4d0 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68   Precision of th
9a4e0 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20  e current field 
9a4f0 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b  */.  int length;
9a500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a510 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65  /* Length of the
9a520 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20   field */.  int 
9a530 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  idx;            
9a540 20 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65         /* A gene
9a550 72 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70  ral purpose loop
9a560 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
9a570 74 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 20  t width;        
9a580 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74           /* Widt
9a590 68 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  h of the current
9a5a0 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79   field */.  etBy
9a5b0 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74  te flag_leftjust
9a5c0 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69  ify;   /* True i
9a5d0 66 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72  f "-" flag is pr
9a5e0 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74  esent */.  etByt
9a5f0 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b  e flag_plussign;
9a600 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
9a610 20 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65   "+" flag is pre
9a620 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
9a630 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b   flag_blanksign;
9a640 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
9a650 22 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  " " flag is pres
9a660 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
9a670 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
9a680 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22  rm; /* True if "
9a690 23 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  #" flag is prese
9a6a0 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  nt */.  etByte f
9a6b0 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20  lag_altform2;   
9a6c0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21     /* True if "!
9a6d0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
9a6e0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
9a6f0 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20  ag_zeropad;     
9a700 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65    /* True if fie
9a710 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e  ld width constan
9a720 74 20 73 74 61 72 74 73 20 77 69 74 68 20 7a 65  t starts with ze
9a730 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  ro */.  etByte f
9a740 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20  lag_long;       
9a750 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c     /* True if "l
9a760 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
9a770 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
9a780 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20  ag_longlong;    
9a790 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
9a7a0 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72   "ll" flag is pr
9a7b0 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74  esent */.  etByt
9a7c0 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20  e done;         
9a7d0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65        /* Loop te
9a7e0 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a  rmination flag *
9a7f0 2f 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  /.  sqlite_uint6
9a800 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f  4 longvalue;   /
9a810 2a 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65  * Value for inte
9a820 67 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c  ger types */.  L
9a830 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72  ONGDOUBLE_TYPE r
9a840 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c  ealvalue; /* Val
9a850 75 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65  ue for real type
9a860 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f  s */.  const et_
9a870 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20  info *infop;    
9a880 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
9a890 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
9a8a0 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a  info structure *
9a8b0 2f 0a 20 20 63 68 61 72 20 62 75 66 5b 65 74 42  /.  char buf[etB
9a8c0 55 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f  UFSIZE];       /
9a8d0 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66  * Conversion buf
9a8e0 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72  fer */.  char pr
9a8f0 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  efix;           
9a900 20 20 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68      /* Prefix ch
9a910 61 72 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72  aracter.  "+" or
9a920 20 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27   "-" or " " or '
9a930 5c 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65  \0'. */.  etByte
9a940 20 78 74 79 70 65 20 3d 20 30 3b 20 20 20 20 20   xtype = 0;     
9a950 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69       /* Conversi
9a960 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20  on paradigm */. 
9a970 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20   char *zExtra;  
9a980 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
9a990 78 74 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64  xtra memory used
9a9a0 20 66 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45   for etTCLESCAPE
9a9b0 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a   conversions */.
9a9c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
9a9d0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
9a9e0 4e 54 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65  NT.  int  exp, e
9a9f0 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2;              
9aa00 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72  /* exponent of r
9aa10 65 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20  eal numbers */. 
9aa20 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b   double rounder;
9aa30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
9aa40 73 65 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67  sed for rounding
9aa50 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
9aa60 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79  values */.  etBy
9aa70 74 65 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20  te flag_dp;     
9aa80 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
9aa90 66 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  f decimal point 
9aaa0 73 68 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20  should be shown 
9aab0 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
9aac0 5f 72 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20  _rtz;           
9aad0 2f 2a 20 54 72 75 65 20 69 66 20 74 72 61 69 6c  /* True if trail
9aae0 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64  ing zeros should
9aaf0 20 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20   be removed */. 
9ab00 20 65 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70   etByte flag_exp
9ab10 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
9ab20 72 75 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73  rue to force dis
9ab30 70 6c 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f  play of the expo
9ab40 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73  nent */.  int ns
9ab50 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
9ab60 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
9ab70 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69  f significant di
9ab80 67 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f  gits returned */
9ab90 0a 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74  .#endif..  lengt
9aba0 68 20 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d  h = 0;.  bufpt =
9abb0 20 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28   0;.  for(; (c=(
9abc0 2a 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74  *fmt))!=0; ++fmt
9abd0 29 7b 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25  ){.    if( c!='%
9abe0 27 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61  ' ){.      int a
9abf0 6d 74 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20  mt;.      bufpt 
9ac00 3d 20 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20  = (char *)fmt;. 
9ac10 20 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20       amt = 1;.  
9ac20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a      while( (c=(*
9ac30 2b 2b 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20  ++fmt))!='%' && 
9ac40 63 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20  c!=0 ) amt++;.  
9ac50 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
9ac60 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
9ac70 2c 20 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20  , bufpt, amt);. 
9ac80 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20       if( c==0 ) 
9ac90 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
9aca0 20 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29   if( (c=(*++fmt)
9acb0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )==0 ){.      sq
9acc0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
9acd0 65 6e 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c  end(pAccum, "%",
9ace0 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   1);.      break
9acf0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46  ;.    }.    /* F
9ad00 69 6e 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61  ind out what fla
9ad10 67 73 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a  gs are present *
9ad20 2f 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a  /.    flag_leftj
9ad30 75 73 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c  ustify = flag_pl
9ad40 75 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c  ussign = flag_bl
9ad50 61 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20  anksign = .     
9ad60 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
9ad70 72 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72  rm = flag_altfor
9ad80 6d 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61  m2 = flag_zeropa
9ad90 64 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20  d = 0;.    done 
9ada0 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20  = 0;.    do{.   
9adb0 20 20 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a     switch( c ){.
9adc0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 2d 27          case '-'
9add0 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73  :   flag_leftjus
9ade0 74 69 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72  tify = 1;     br
9adf0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
9ae00 65 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c  e '+':   flag_pl
9ae10 75 73 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20  ussign = 1;     
9ae20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
9ae30 20 20 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c    case ' ':   fl
9ae40 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31  ag_blanksign = 1
9ae50 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;       break;. 
9ae60 20 20 20 20 20 20 20 63 61 73 65 20 27 23 27 3a         case '#':
9ae70 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74     flag_alternat
9ae80 65 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65  eform = 1;   bre
9ae90 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
9aea0 20 27 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74   '!':   flag_alt
9aeb0 66 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20  form2 = 1;      
9aec0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
9aed0 20 63 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61   case '0':   fla
9aee0 67 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20  g_zeropad = 1;  
9aef0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
9af00 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20        default:  
9af10 20 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20    done = 1;     
9af20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
9af30 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
9af40 77 68 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20  while( !done && 
9af50 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20  (c=(*++fmt))!=0 
9af60 29 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68  );.    /* Get th
9af70 65 20 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f  e field width */
9af80 0a 20 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a  .    width = 0;.
9af90 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29      if( c=='*' )
9afa0 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20  {.      width = 
9afb0 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a  va_arg(ap,int);.
9afc0 20 20 20 20 20 20 69 66 28 20 77 69 64 74 68 3c        if( width<
9afd0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61  0 ){.        fla
9afe0 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20  g_leftjustify = 
9aff0 31 3b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68  1;.        width
9b000 20 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20   = -width;.     
9b010 20 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b   }.      c = *++
9b020 66 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  fmt;.    }else{.
9b030 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d        while( c>=
9b040 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b  '0' && c<='9' ){
9b050 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d  .        width =
9b060 20 77 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20   width*10 + c - 
9b070 27 30 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d  '0';.        c =
9b080 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d   *++fmt;.      }
9b090 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77  .    }.    if( w
9b0a0 69 64 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45  idth > etBUFSIZE
9b0b0 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64  -10 ){.      wid
9b0c0 74 68 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31  th = etBUFSIZE-1
9b0d0 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  0;.    }.    /* 
9b0e0 47 65 74 20 74 68 65 20 70 72 65 63 69 73 69 6f  Get the precisio
9b0f0 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d  n */.    if( c==
9b100 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65  '.' ){.      pre
9b110 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20  cision = 0;.    
9b120 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20    c = *++fmt;.  
9b130 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29      if( c=='*' )
9b140 7b 0a 20 20 20 20 20 20 20 20 70 72 65 63 69 73  {.        precis
9b150 69 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  ion = va_arg(ap,
9b160 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  int);.        if
9b170 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20  ( precision<0 ) 
9b180 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65  precision = -pre
9b190 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20  cision;.        
9b1a0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20  c = *++fmt;.    
9b1b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
9b1c0 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26   while( c>='0' &
9b1d0 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20  & c<='9' ){.    
9b1e0 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20        precision 
9b1f0 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b  = precision*10 +
9b200 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20   c - '0';.      
9b210 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a      c = *++fmt;.
9b220 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9b230 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
9b240 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d     precision = -
9b250 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  1;.    }.    /* 
9b260 47 65 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69  Get the conversi
9b270 6f 6e 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72  on type modifier
9b280 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27   */.    if( c=='
9b290 6c 27 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67  l' ){.      flag
9b2a0 5f 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20  _long = 1;.     
9b2b0 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20   c = *++fmt;.   
9b2c0 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b     if( c=='l' ){
9b2d0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f  .        flag_lo
9b2e0 6e 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20  nglong = 1;.    
9b2f0 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a      c = *++fmt;.
9b300 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
9b310 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f       flag_longlo
9b320 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ng = 0;.      }.
9b330 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9b340 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61   flag_long = fla
9b350 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a  g_longlong = 0;.
9b360 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74      }.    /* Fet
9b370 63 68 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72  ch the info entr
9b380 79 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20  y for the field 
9b390 2a 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 26  */.    infop = &
9b3a0 66 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20  fmtinfo[0];.    
9b3b0 78 74 79 70 65 20 3d 20 65 74 49 4e 56 41 4c 49  xtype = etINVALI
9b3c0 44 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 30  D;.    for(idx=0
9b3d0 3b 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65 28  ; idx<ArraySize(
9b3e0 66 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29  fmtinfo); idx++)
9b3f0 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 66  {.      if( c==f
9b400 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74  mtinfo[idx].fmtt
9b410 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ype ){.        i
9b420 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b  nfop = &fmtinfo[
9b430 69 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  idx];.        if
9b440 28 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c 7c  ( useExtended ||
9b450 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26   (infop->flags &
9b460 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30   FLAG_INTERN)==0
9b470 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 74   ){.          xt
9b480 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70  ype = infop->typ
9b490 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  e;.        }else
9b4a0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
9b4b0 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rn;.        }.  
9b4c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
9b4d0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a     }.    }.    z
9b4e0 45 78 74 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20  Extra = 0;...   
9b4f0 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 70 72   /* Limit the pr
9b500 65 63 69 73 69 6f 6e 20 74 6f 20 70 72 65 76 65  ecision to preve
9b510 6e 74 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62  nt overflowing b
9b520 75 66 5b 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76  uf[] during conv
9b530 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66  ersion */.    if
9b540 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55  ( precision>etBU
9b550 46 53 49 5a 45 2d 34 30 20 26 26 20 28 69 6e 66  FSIZE-40 && (inf
9b560 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47  op->flags & FLAG
9b570 5f 53 54 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20  _STRING)==0 ){. 
9b580 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d       precision =
9b590 20 65 74 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20   etBUFSIZE-40;. 
9b5a0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
9b5b0 20 2a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   ** At this poin
9b5c0 74 2c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  t, variables are
9b5d0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 73 20   initialized as 
9b5e0 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a  follows:.    **.
9b5f0 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c      **   flag_al
9b600 74 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20  ternateform     
9b610 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27       TRUE if a '
9b620 23 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20  #' is present.. 
9b630 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74     **   flag_alt
9b640 66 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20  form2           
9b650 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21      TRUE if a '!
9b660 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  ' is present..  
9b670 20 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73    **   flag_plus
9b680 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20  sign            
9b690 20 20 20 54 52 55 45 20 69 66 20 61 20 27 2b 27     TRUE if a '+'
9b6a0 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20   is present..   
9b6b0 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a   **   flag_leftj
9b6c0 75 73 74 69 66 79 20 20 20 20 20 20 20 20 20 20  ustify          
9b6d0 20 20 54 52 55 45 20 69 66 20 61 20 27 2d 27 20    TRUE if a '-' 
9b6e0 69 73 20 70 72 65 73 65 6e 74 20 6f 72 20 69 66  is present or if
9b6f0 20 74 68 65 0a 20 20 20 20 2a 2a 20 20 20 20 20   the.    **     
9b700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b710 20 20 20 20 20 20 20 20 20 20 66 69 65 6c 64 20            field 
9b720 77 69 64 74 68 20 77 61 73 20 6e 65 67 61 74 69  width was negati
9b730 76 65 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61  ve..    **   fla
9b740 67 5f 7a 65 72 6f 70 61 64 20 20 20 20 20 20 20  g_zeropad       
9b750 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66           TRUE if
9b760 20 74 68 65 20 77 69 64 74 68 20 62 65 67 61 6e   the width began
9b770 20 77 69 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20   with 0..    ** 
9b780 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20    flag_long     
9b790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52                TR
9b7a0 55 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72  UE if the letter
9b7b0 20 27 6c 27 20 28 65 6c 6c 29 20 70 72 65 66 69   'l' (ell) prefi
9b7c0 78 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  xed.    **      
9b7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b7e0 20 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e           the con
9b7f0 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65  version characte
9b800 72 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67  r..    **   flag
9b810 5f 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20  _longlong       
9b820 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20          TRUE if 
9b830 74 68 65 20 6c 65 74 74 65 72 20 27 6c 6c 27 20  the letter 'll' 
9b840 28 65 6c 6c 20 65 6c 6c 29 20 70 72 65 66 69 78  (ell ell) prefix
9b850 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  ed.    **       
9b860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b870 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76          the conv
9b880 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72  ersion character
9b890 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f  ..    **   flag_
9b8a0 62 6c 61 6e 6b 73 69 67 6e 20 20 20 20 20 20 20  blanksign       
9b8b0 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61         TRUE if a
9b8c0 20 27 20 27 20 69 73 20 70 72 65 73 65 6e 74 2e   ' ' is present.
9b8d0 0a 20 20 20 20 2a 2a 20 20 20 77 69 64 74 68 20  .    **   width 
9b8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b8f0 20 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66        The specif
9b900 69 65 64 20 66 69 65 6c 64 20 77 69 64 74 68 2e  ied field width.
9b910 20 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a    This is.    **
9b920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
9b940 6c 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69  lways non-negati
9b950 76 65 2e 20 20 5a 65 72 6f 20 69 73 20 74 68 65  ve.  Zero is the
9b960 20 64 65 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a   default..    **
9b970 20 20 20 70 72 65 63 69 73 69 6f 6e 20 20 20 20     precision    
9b980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
9b990 68 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65  he specified pre
9b9a0 63 69 73 69 6f 6e 2e 20 20 54 68 65 20 64 65 66  cision.  The def
9b9b0 61 75 6c 74 0a 20 20 20 20 2a 2a 20 20 20 20 20  ault.    **     
9b9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b9d0 20 20 20 20 20 20 20 20 20 20 69 73 20 2d 31 2e            is -1.
9b9e0 0a 20 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20  .    **   xtype 
9b9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ba00 20 20 20 20 20 20 54 68 65 20 63 6c 61 73 73 20        The class 
9ba10 6f 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  of the conversio
9ba20 6e 2e 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f  n..    **   info
9ba30 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
9ba40 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20          Pointer 
9ba50 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  to the appropria
9ba60 74 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a  te info struct..
9ba70 20 20 20 20 2a 2f 0a 20 20 20 20 73 77 69 74 63      */.    switc
9ba80 68 28 20 78 74 79 70 65 20 29 7b 0a 20 20 20 20  h( xtype ){.    
9ba90 20 20 63 61 73 65 20 65 74 50 4f 49 4e 54 45 52    case etPOINTER
9baa0 3a 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c  :.        flag_l
9bab0 6f 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66  onglong = sizeof
9bac0 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28  (char*)==sizeof(
9bad0 69 36 34 29 3b 0a 20 20 20 20 20 20 20 20 66 6c  i64);.        fl
9bae0 61 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66  ag_long = sizeof
9baf0 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28  (char*)==sizeof(
9bb00 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20  long int);.     
9bb10 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75     /* Fall throu
9bb20 67 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74  gh into the next
9bb30 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 63   case */.      c
9bb40 61 73 65 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20  ase etORDINAL:. 
9bb50 20 20 20 20 20 63 61 73 65 20 65 74 52 41 44 49       case etRADI
9bb60 58 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  X:.        if( i
9bb70 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c  nfop->flags & FL
9bb80 41 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20 20  AG_SIGNED ){.   
9bb90 20 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20         i64 v;.  
9bba0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
9bbb0 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20  _longlong ){.   
9bbc0 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f           v = va_
9bbd0 61 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20  arg(ap,i64);.   
9bbe0 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
9bbf0 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20   flag_long ){.  
9bc00 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61            v = va
9bc10 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74  _arg(ap,long int
9bc20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
9bc30 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
9bc40 76 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e  v = va_arg(ap,in
9bc50 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  t);.          }.
9bc60 20 20 20 20 20 20 20 20 20 20 69 66 28 20 76 3c            if( v<
9bc70 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
9bc80 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b   longvalue = -v;
9bc90 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65  .            pre
9bca0 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20  fix = '-';.     
9bcb0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
9bcc0 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75          longvalu
9bcd0 65 20 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20  e = v;.         
9bce0 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73     if( flag_plus
9bcf0 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 70 72  sign )        pr
9bd00 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20  efix = '+';.    
9bd10 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28          else if(
9bd20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20   flag_blanksign 
9bd30 29 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b  )  prefix = ' ';
9bd40 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73  .            els
9bd50 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
9bd60 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
9bd70 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   0;.          }.
9bd80 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
9bd90 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61           if( fla
9bda0 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20  g_longlong ){.  
9bdb0 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
9bdc0 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  lue = va_arg(ap,
9bdd0 75 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  u64);.          
9bde0 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c  }else if( flag_l
9bdf0 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ong ){.         
9be00 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76     longvalue = v
9be10 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65  a_arg(ap,unsigne
9be20 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20  d long int);.   
9be30 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
9be40 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
9be50 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  lue = va_arg(ap,
9be60 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20  unsigned int);. 
9be70 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
9be80 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b       prefix = 0;
9be90 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
9bea0 20 20 20 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65     if( longvalue
9beb0 3d 3d 30 20 29 20 66 6c 61 67 5f 61 6c 74 65 72  ==0 ) flag_alter
9bec0 6e 61 74 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20  nateform = 0;.  
9bed0 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a        if( flag_z
9bee0 65 72 6f 70 61 64 20 26 26 20 70 72 65 63 69 73  eropad && precis
9bef0 69 6f 6e 3c 77 69 64 74 68 2d 28 70 72 65 66 69  ion<width-(prefi
9bf00 78 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20  x!=0) ){.       
9bf10 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 77     precision = w
9bf20 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29  idth-(prefix!=0)
9bf30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
9bf40 20 20 20 20 62 75 66 70 74 20 3d 20 26 62 75 66      bufpt = &buf
9bf50 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20  [etBUFSIZE-1];. 
9bf60 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65         if( xtype
9bf70 3d 3d 65 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20  ==etORDINAL ){. 
9bf80 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20           static 
9bf90 63 6f 6e 73 74 20 63 68 61 72 20 7a 4f 72 64 5b  const char zOrd[
9bfa0 5d 20 3d 20 22 74 68 73 74 6e 64 72 64 22 3b 0a  ] = "thstndrd";.
9bfb0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 78 20            int x 
9bfc0 3d 20 28 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75  = (int)(longvalu
9bfd0 65 20 25 20 31 30 29 3b 0a 20 20 20 20 20 20 20  e % 10);.       
9bfe0 20 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28     if( x>=4 || (
9bff0 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30  longvalue/10)%10
9c000 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==1 ){.         
9c010 20 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20     x = 0;.      
9c020 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
9c030 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d  buf[etBUFSIZE-3]
9c040 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20   = zOrd[x*2];.  
9c050 20 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55          buf[etBU
9c060 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b  FSIZE-2] = zOrd[
9c070 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20  x*2+1];.        
9c080 20 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20    bufpt -= 2;.  
9c090 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
9c0a0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69  {.          regi
9c0b0 73 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20  ster const char 
9c0c0 2a 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55  *cset;      /* U
9c0d0 73 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72  se registers for
9c0e0 20 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20   speed */.      
9c0f0 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74      register int
9c100 20 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20   base;.         
9c110 20 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73   cset = &aDigits
9c120 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d  [infop->charset]
9c130 3b 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65  ;.          base
9c140 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a   = infop->base;.
9c150 20 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20            do{   
9c160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c180 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65          /* Conve
9c190 72 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20  rt to ascii */. 
9c1a0 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62             *(--b
9c1b0 75 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e  ufpt) = cset[lon
9c1c0 67 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20  gvalue%base];.  
9c1d0 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
9c1e0 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f  lue = longvalue/
9c1f0 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  base;.          
9c200 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75  }while( longvalu
9c210 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d  e>0 );.        }
9c220 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
9c230 3d 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42  = (int)(&buf[etB
9c240 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29  UFSIZE-1]-bufpt)
9c250 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64  ;.        for(id
9c260 78 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67  x=precision-leng
9c270 74 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d  th; idx>0; idx--
9c280 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d  ){.          *(-
9c290 2d 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20  -bufpt) = '0';  
9c2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c2b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65             /* Ze
9c2c0 72 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20  ro pad */.      
9c2d0 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
9c2e0 70 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66  prefix ) *(--buf
9c2f0 70 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20  pt) = prefix;   
9c300 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
9c310 64 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20  dd sign */.     
9c320 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65     if( flag_alte
9c330 72 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66  rnateform && inf
9c340 6f 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20  op->prefix ){   
9c350 20 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72     /* Add "0" or
9c360 20 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20   "0x" */.       
9c370 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
9c380 72 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68  re;.          ch
9c390 61 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20  ar x;.          
9c3a0 70 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69  pre = &aPrefix[i
9c3b0 6e 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20  nfop->prefix];. 
9c3c0 20 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 28           for(; (
9c3d0 78 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70 72  x=(*pre))!=0; pr
9c3e0 65 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29 20  e++) *(--bufpt) 
9c3f0 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = x;.        }. 
9c400 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
9c410 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46  (int)(&buf[etBUF
9c420 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a  SIZE-1]-bufpt);.
9c430 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
9c440 20 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41       case etFLOA
9c450 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  T:.      case et
9c460 45 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20  EXP:.      case 
9c470 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20  etGENERIC:.     
9c480 20 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76     realvalue = v
9c490 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29  a_arg(ap,double)
9c4a0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
9c4b0 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
9c4c0 4f 49 4e 54 0a 20 20 20 20 20 20 20 20 69 66 28  OINT.        if(
9c4d0 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70   precision<0 ) p
9c4e0 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20  recision = 6;   
9c4f0 20 20 20 20 20 20 2f 2a 20 53 65 74 20 64 65 66        /* Set def
9c500 61 75 6c 74 20 70 72 65 63 69 73 69 6f 6e 20 2a  ault precision *
9c510 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  /.        if( pr
9c520 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a  ecision>etBUFSIZ
9c530 45 2f 32 2d 31 30 20 29 20 70 72 65 63 69 73 69  E/2-10 ) precisi
9c540 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2f 32  on = etBUFSIZE/2
9c550 2d 31 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  -10;.        if(
9c560 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29   realvalue<0.0 )
9c570 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c  {.          real
9c580 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c  value = -realval
9c590 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72  ue;.          pr
9c5a0 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20  efix = '-';.    
9c5b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9c5c0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c       if( flag_pl
9c5d0 75 73 73 69 67 6e 20 29 20 20 20 20 20 20 20 20  ussign )        
9c5e0 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a    prefix = '+';.
9c5f0 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69            else i
9c600 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67  f( flag_blanksig
9c610 6e 20 29 20 20 20 20 70 72 65 66 69 78 20 3d 20  n )    prefix = 
9c620 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65  ' ';.          e
9c630 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  lse             
9c640 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66              pref
9c650 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ix = 0;.        
9c660 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74  }.        if( xt
9c670 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 26  ype==etGENERIC &
9c680 26 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 20  & precision>0 ) 
9c690 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66  precision--;.#if
9c6a0 20 30 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f   0.        /* Ro
9c6b0 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b  unding works lik
9c6c0 65 20 42 53 44 20 77 68 65 6e 20 74 68 65 20 63  e BSD when the c
9c6d0 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39 39 20 69  onstant 0.4999 i
9c6e0 73 20 75 73 65 64 2e 20 20 57 69 65 72 64 21 20  s used.  Wierd! 
9c6f0 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  */.        for(i
9c700 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f  dx=precision, ro
9c710 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20 69 64  under=0.4999; id
9c720 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e  x>0; idx--, roun
9c730 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65  der*=0.1);.#else
9c740 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 74 20 6d  .        /* It m
9c750 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73 65 20  akes more sense 
9c760 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20  to use 0.5 */.  
9c770 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72        for(idx=pr
9c780 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72  ecision, rounder
9c790 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69 64 78  =0.5; idx>0; idx
9c7a0 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31  --, rounder*=0.1
9c7b0 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ){}.#endif.     
9c7c0 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74     if( xtype==et
9c7d0 46 4c 4f 41 54 20 29 20 72 65 61 6c 76 61 6c 75  FLOAT ) realvalu
9c7e0 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20  e += rounder;.  
9c7f0 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69        /* Normali
9c800 7a 65 20 72 65 61 6c 76 61 6c 75 65 20 74 6f 20  ze realvalue to 
9c810 77 69 74 68 69 6e 20 31 30 2e 30 20 3e 20 72 65  within 10.0 > re
9c820 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a  alvalue >= 1.0 *
9c830 2f 0a 20 20 20 20 20 20 20 20 65 78 70 20 3d 20  /.        exp = 
9c840 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  0;.        if( s
9c850 71 6c 69 74 65 33 49 73 4e 61 4e 28 28 64 6f 75  qlite3IsNaN((dou
9c860 62 6c 65 29 72 65 61 6c 76 61 6c 75 65 29 20 29  ble)realvalue) )
9c870 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70  {.          bufp
9c880 74 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20  t = "NaN";.     
9c890 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b       length = 3;
9c8a0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
9c8b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
9c8c0 20 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75      if( realvalu
9c8d0 65 3e 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20  e>0.0 ){.       
9c8e0 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
9c8f0 6c 75 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70  lue>=1e32 && exp
9c900 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c  <=350 ){ realval
9c910 75 65 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70  ue *= 1e-32; exp
9c920 2b 3d 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20  +=32; }.        
9c930 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c    while( realval
9c940 75 65 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d  ue>=1e8 && exp<=
9c950 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65  350 ){ realvalue
9c960 20 2a 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38   *= 1e-8; exp+=8
9c970 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68  ; }.          wh
9c980 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d  ile( realvalue>=
9c990 31 30 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30  10.0 && exp<=350
9c9a0 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d   ){ realvalue *=
9c9b0 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20   0.1; exp++; }. 
9c9c0 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
9c9d0 72 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29  realvalue<1e-8 )
9c9e0 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31  { realvalue *= 1
9c9f0 65 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20  e8; exp-=8; }.  
9ca00 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72          while( r
9ca10 65 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20  ealvalue<1.0 ){ 
9ca20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e  realvalue *= 10.
9ca30 30 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20  0; exp--; }.    
9ca40 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 33 35        if( exp>35
9ca50 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
9ca60 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d 27   if( prefix=='-'
9ca70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
9ca80 20 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66 22    bufpt = "-Inf"
9ca90 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65  ;.            }e
9caa0 6c 73 65 20 69 66 28 20 70 72 65 66 69 78 3d 3d  lse if( prefix==
9cab0 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '+' ){.         
9cac0 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b 49       bufpt = "+I
9cad0 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nf";.           
9cae0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
9caf0 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 49        bufpt = "I
9cb00 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nf";.           
9cb10 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c   }.            l
9cb20 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53  ength = sqlite3S
9cb30 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a  trlen30(bufpt);.
9cb40 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
9cb50 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
9cb60 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9cb70 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20   bufpt = buf;.  
9cb80 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20        /*.       
9cb90 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c 64   ** If the field
9cba0 20 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 52   type is etGENER
9cbb0 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 74  IC, then convert
9cbc0 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 50   to either etEXP
9cbd0 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 65  .        ** or e
9cbe0 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 6f  tFLOAT, as appro
9cbf0 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 20  priate..        
9cc00 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  */.        flag_
9cc10 65 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74 45  exp = xtype==etE
9cc20 58 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  XP;.        if( 
9cc30 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29  xtype!=etFLOAT )
9cc40 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c  {.          real
9cc50 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72  value += rounder
9cc60 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
9cc70 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20  realvalue>=10.0 
9cc80 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
9cc90 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20  0.1; exp++; }.  
9cca0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
9ccb0 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e  if( xtype==etGEN
9ccc0 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20  ERIC ){.        
9ccd0 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c    flag_rtz = !fl
9cce0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
9ccf0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
9cd00 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72  exp<-4 || exp>pr
9cd10 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20  ecision ){.     
9cd20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65         xtype = e
9cd30 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20  tEXP;.          
9cd40 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
9cd50 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70     precision = p
9cd60 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a  recision - exp;.
9cd70 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70              xtyp
9cd80 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20  e = etFLOAT;.   
9cd90 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9cda0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
9cdb0 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a    flag_rtz = 0;.
9cdc0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9cdd0 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 45    if( xtype==etE
9cde0 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  XP ){.          
9cdf0 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  e2 = 0;.        
9ce00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
9ce10 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20 20   e2 = exp;.     
9ce20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73 64     }.        nsd
9ce30 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6c   = 0;.        fl
9ce40 61 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73 69  ag_dp = (precisi
9ce50 6f 6e 3e 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61  on>0 ?1:0) | fla
9ce60 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20  g_alternateform 
9ce70 7c 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b  | flag_altform2;
9ce80 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
9ce90 73 69 67 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66  sign in front of
9cea0 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20   the number */. 
9ceb0 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69         if( prefi
9cec0 78 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  x ){.          *
9ced0 28 62 75 66 70 74 2b 2b 29 20 3d 20 70 72 65 66  (bufpt++) = pref
9cee0 69 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ix;.        }.  
9cef0 20 20 20 20 20 20 2f 2a 20 44 69 67 69 74 73 20        /* Digits 
9cf00 70 72 69 6f 72 20 74 6f 20 74 68 65 20 64 65 63  prior to the dec
9cf10 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  imal point */.  
9cf20 20 20 20 20 20 20 69 66 28 20 65 32 3c 30 20 29        if( e2<0 )
9cf30 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  {.          *(bu
9cf40 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20  fpt++) = '0';.  
9cf50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
9cf60 20 20 20 20 20 20 20 66 6f 72 28 3b 20 65 32 3e         for(; e2>
9cf70 3d 30 3b 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20  =0; e2--){.     
9cf80 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
9cf90 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69 74 28  ) = et_getdigit(
9cfa0 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29  &realvalue,&nsd)
9cfb0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
9cfc0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
9cfd0 2f 2a 20 54 68 65 20 64 65 63 69 6d 61 6c 20 70  /* The decimal p
9cfe0 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  oint */.        
9cff0 69 66 28 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20  if( flag_dp ){. 
9d000 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
9d010 2b 2b 29 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20  ++) = '.';.     
9d020 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
9d030 22 30 22 20 64 69 67 69 74 73 20 61 66 74 65 72  "0" digits after
9d040 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69   the decimal poi
9d050 6e 74 20 62 75 74 20 62 65 66 6f 72 65 20 74 68  nt but before th
9d060 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 20  e first.        
9d070 2a 2a 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64  ** significant d
9d080 69 67 69 74 20 6f 66 20 74 68 65 20 6e 75 6d 62  igit of the numb
9d090 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f  er */.        fo
9d0a0 72 28 65 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72  r(e2++; e2<0; pr
9d0b0 65 63 69 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29  ecision--, e2++)
9d0c0 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
9d0d0 72 74 28 20 70 72 65 63 69 73 69 6f 6e 3e 30 20  rt( precision>0 
9d0e0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62  );.          *(b
9d0f0 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20  ufpt++) = '0';. 
9d100 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9d110 20 2f 2a 20 53 69 67 6e 69 66 69 63 61 6e 74 20   /* Significant 
9d120 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65  digits after the
9d130 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a   decimal point *
9d140 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  /.        while(
9d150 20 28 70 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30   (precision--)>0
9d160 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28   ){.          *(
9d170 62 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65  bufpt++) = et_ge
9d180 74 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75  tdigit(&realvalu
9d190 65 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20  e,&nsd);.       
9d1a0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65   }.        /* Re
9d1b0 6d 6f 76 65 20 74 72 61 69 6c 69 6e 67 20 7a 65  move trailing ze
9d1c0 72 6f 73 20 61 6e 64 20 74 68 65 20 22 2e 22 20  ros and the "." 
9d1d0 69 66 20 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c  if no digits fol
9d1e0 6c 6f 77 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20  low the "." */. 
9d1f0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
9d200 72 74 7a 20 26 26 20 66 6c 61 67 5f 64 70 20 29  rtz && flag_dp )
9d210 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  {.          whil
9d220 65 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30  e( bufpt[-1]=='0
9d230 27 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d  ' ) *(--bufpt) =
9d240 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   0;.          as
9d250 73 65 72 74 28 20 62 75 66 70 74 3e 62 75 66 20  sert( bufpt>buf 
9d260 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
9d270 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20   bufpt[-1]=='.' 
9d280 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
9d290 66 28 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32  f( flag_altform2
9d2a0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
9d2b0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27    *(bufpt++) = '
9d2c0 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0';.            
9d2d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
9d2e0 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20       *(--bufpt) 
9d2f0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
9d300 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
9d310 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9d320 20 2f 2a 20 41 64 64 20 74 68 65 20 22 65 4e 4e   /* Add the "eNN
9d330 4e 22 20 73 75 66 66 69 78 20 2a 2f 0a 20 20 20  N" suffix */.   
9d340 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 65 78       if( flag_ex
9d350 70 20 7c 7c 20 78 74 79 70 65 3d 3d 65 74 45 58  p || xtype==etEX
9d360 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  P ){.          *
9d370 28 62 75 66 70 74 2b 2b 29 20 3d 20 61 44 69 67  (bufpt++) = aDig
9d380 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73  its[infop->chars
9d390 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69  et];.          i
9d3a0 66 28 20 65 78 70 3c 30 20 29 7b 0a 20 20 20 20  f( exp<0 ){.    
9d3b0 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
9d3c0 2b 29 20 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20  +) = '-'; exp = 
9d3d0 2d 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20  -exp;.          
9d3e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
9d3f0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
9d400 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  '+';.          }
9d410 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65  .          if( e
9d420 78 70 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20 20  xp>=100 ){.     
9d430 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
9d440 29 20 3d 20 28 63 68 61 72 29 28 28 65 78 70 2f  ) = (char)((exp/
9d450 31 30 30 29 2b 27 30 27 29 3b 20 20 20 20 20 20  100)+'0');      
9d460 20 20 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74    /* 100's digit
9d470 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
9d480 65 78 70 20 25 3d 20 31 30 30 3b 0a 20 20 20 20  exp %= 100;.    
9d490 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
9d4a0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28    *(bufpt++) = (
9d4b0 63 68 61 72 29 28 65 78 70 2f 31 30 2b 27 30 27  char)(exp/10+'0'
9d4c0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  );             /
9d4d0 2a 20 31 30 27 73 20 64 69 67 69 74 20 2a 2f 0a  * 10's digit */.
9d4e0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
9d4f0 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78  t++) = (char)(ex
9d500 70 25 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20  p%10+'0');      
9d510 20 20 20 20 20 20 20 2f 2a 20 31 27 73 20 64 69         /* 1's di
9d520 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d  git */.        }
9d530 0a 20 20 20 20 20 20 20 20 2a 62 75 66 70 74 20  .        *bufpt 
9d540 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  = 0;..        /*
9d550 20 54 68 65 20 63 6f 6e 76 65 72 74 65 64 20 6e   The converted n
9d560 75 6d 62 65 72 20 69 73 20 69 6e 20 62 75 66 5b  umber is in buf[
9d570 5d 20 61 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69  ] and zero termi
9d580 6e 61 74 65 64 2e 20 4f 75 74 70 75 74 20 69 74  nated. Output it
9d590 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74  ..        ** Not
9d5a0 65 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65  e that the numbe
9d5b0 72 20 69 73 20 69 6e 20 74 68 65 20 75 73 75 61  r is in the usua
9d5c0 6c 20 6f 72 64 65 72 2c 20 6e 6f 74 20 72 65 76  l order, not rev
9d5d0 65 72 73 65 64 20 61 73 20 77 69 74 68 0a 20 20  ersed as with.  
9d5e0 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72        ** integer
9d5f0 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f   conversions. */
9d600 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
9d610 3d 20 28 69 6e 74 29 28 62 75 66 70 74 2d 62 75  = (int)(bufpt-bu
9d620 66 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70  f);.        bufp
9d630 74 20 3d 20 62 75 66 3b 0a 0a 20 20 20 20 20 20  t = buf;..      
9d640 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
9d650 65 3a 20 20 41 64 64 20 6c 65 61 64 69 6e 67 20  e:  Add leading 
9d660 7a 65 72 6f 73 20 69 66 20 74 68 65 20 66 6c 61  zeros if the fla
9d670 67 5f 7a 65 72 6f 70 61 64 20 66 6c 61 67 20 69  g_zeropad flag i
9d680 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74  s.        ** set
9d690 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20   and we are not 
9d6a0 6c 65 66 74 20 6a 75 73 74 69 66 69 65 64 20 2a  left justified *
9d6b0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c  /.        if( fl
9d6c0 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 21 66  ag_zeropad && !f
9d6d0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20  lag_leftjustify 
9d6e0 26 26 20 6c 65 6e 67 74 68 20 3c 20 77 69 64 74  && length < widt
9d6f0 68 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  h){.          in
9d700 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69  t i;.          i
9d710 6e 74 20 6e 50 61 64 20 3d 20 77 69 64 74 68 20  nt nPad = width 
9d720 2d 20 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20  - length;.      
9d730 20 20 20 20 66 6f 72 28 69 3d 77 69 64 74 68 3b      for(i=width;
9d740 20 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a   i>=nPad; i--){.
9d750 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70              bufp
9d760 74 5b 69 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e  t[i] = bufpt[i-n
9d770 50 61 64 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  Pad];.          
9d780 7d 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20  }.          i = 
9d790 70 72 65 66 69 78 21 3d 30 3b 0a 20 20 20 20 20  prefix!=0;.     
9d7a0 20 20 20 20 20 77 68 69 6c 65 28 20 6e 50 61 64       while( nPad
9d7b0 2d 2d 20 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20  -- ) bufpt[i++] 
9d7c0 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20  = '0';.         
9d7d0 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 3b   length = width;
9d7e0 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  .        }.#endi
9d7f0 66 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  f.        break;
9d800 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 49  .      case etSI
9d810 5a 45 3a 0a 20 20 20 20 20 20 20 20 2a 28 76 61  ZE:.        *(va
9d820 5f 61 72 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d  _arg(ap,int*)) =
9d830 20 70 41 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a   pAccum->nChar;.
9d840 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
9d850 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20   width = 0;.    
9d860 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
9d870 20 63 61 73 65 20 65 74 50 45 52 43 45 4e 54 3a   case etPERCENT:
9d880 0a 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20  .        buf[0] 
9d890 3d 20 27 25 27 3b 0a 20 20 20 20 20 20 20 20 62  = '%';.        b
9d8a0 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20  ufpt = buf;.    
9d8b0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a      length = 1;.
9d8c0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
9d8d0 20 20 20 20 20 63 61 73 65 20 65 74 43 48 41 52       case etCHAR
9d8e0 58 3a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 76  X:.        c = v
9d8f0 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20  a_arg(ap,int);. 
9d900 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20         buf[0] = 
9d910 28 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20  (char)c;.       
9d920 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d   if( precision>=
9d930 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  0 ){.          f
9d940 6f 72 28 69 64 78 3d 31 3b 20 69 64 78 3c 70 72  or(idx=1; idx<pr
9d950 65 63 69 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20  ecision; idx++) 
9d960 62 75 66 5b 69 64 78 5d 20 3d 20 28 63 68 61 72  buf[idx] = (char
9d970 29 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  )c;.          le
9d980 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e  ngth = precision
9d990 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
9d9a0 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74  .          lengt
9d9b0 68 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  h =1;.        }.
9d9c0 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
9d9d0 62 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65  buf;.        bre
9d9e0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65  ak;.      case e
9d9f0 74 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63  tSTRING:.      c
9da00 61 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a  ase etDYNSTRING:
9da10 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d  .        bufpt =
9da20 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a   va_arg(ap,char*
9da30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62  );.        if( b
9da40 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ufpt==0 ){.     
9da50 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b       bufpt = "";
9da60 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
9da70 66 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53  f( xtype==etDYNS
9da80 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20  TRING ){.       
9da90 20 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70     zExtra = bufp
9daa0 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
9dab0 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69       if( precisi
9dac0 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  on>=0 ){.       
9dad0 20 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b     for(length=0;
9dae0 20 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f   length<precisio
9daf0 6e 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74  n && bufpt[lengt
9db00 68 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a  h]; length++){}.
9db10 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
9db20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20           length 
9db30 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
9db40 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20  0(bufpt);.      
9db50 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
9db60 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  k;.      case et
9db70 53 51 4c 45 53 43 41 50 45 3a 0a 20 20 20 20 20  SQLESCAPE:.     
9db80 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50   case etSQLESCAP
9db90 45 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65  E2:.      case e
9dba0 74 53 51 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20  tSQLESCAPE3: {. 
9dbb0 20 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c         int i, j,
9dbc0 20 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20   n, isnull;.    
9dbd0 20 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74      int needQuot
9dbe0 65 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  e;.        char 
9dbf0 63 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  ch;.        char
9dc00 20 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74   q = ((xtype==et
9dc10 53 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a  SQLESCAPE3)?'"':
9dc20 27 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74  '\'');   /* Quot
9dc30 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  e character */. 
9dc40 20 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63         char *esc
9dc50 61 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  arg = va_arg(ap,
9dc60 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20  char*);.        
9dc70 69 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d  isnull = escarg=
9dc80 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  =0;.        if( 
9dc90 69 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20  isnull ) escarg 
9dca0 3d 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45  = (xtype==etSQLE
9dcb0 53 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20  SCAPE2 ? "NULL" 
9dcc0 3a 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20  : "(NULL)");.   
9dcd0 20 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20       for(i=n=0; 
9dce0 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d  (ch=escarg[i])!=
9dcf0 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  0; i++){.       
9dd00 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20     if( ch==q )  
9dd10 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n++;.        }. 
9dd20 20 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65         needQuote
9dd30 20 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74   = !isnull && xt
9dd40 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45  ype==etSQLESCAPE
9dd50 32 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  2;.        n += 
9dd60 69 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74  i + 1 + needQuot
9dd70 65 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28  e*2;.        if(
9dd80 20 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a   n>etBUFSIZE ){.
9dd90 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20            bufpt 
9dda0 3d 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74  = zExtra = sqlit
9ddb0 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20  e3Malloc( n );. 
9ddc0 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66           if( buf
9ddd0 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  pt==0 ){.       
9dde0 20 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c       pAccum->mal
9ddf0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
9de00 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
9de10 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  n;.          }. 
9de20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
9de30 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
9de40 62 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  buf;.        }. 
9de50 20 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20         j = 0;.  
9de60 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75        if( needQu
9de70 6f 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d  ote ) bufpt[j++]
9de80 20 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 66 6f   = q;.        fo
9de90 72 28 69 3d 30 3b 20 28 63 68 3d 65 73 63 61 72  r(i=0; (ch=escar
9dea0 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a  g[i])!=0; i++){.
9deb0 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b            bufpt[
9dec0 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20  j++] = ch;.     
9ded0 20 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29       if( ch==q )
9dee0 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68   bufpt[j++] = ch
9def0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
9df00 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74      if( needQuot
9df10 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d  e ) bufpt[j++] =
9df20 20 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70   q;.        bufp
9df30 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  t[j] = 0;.      
9df40 20 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20    length = j;.  
9df50 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65        /* The pre
9df60 63 69 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65  cision is ignore
9df70 64 20 6f 6e 20 25 71 20 61 6e 64 20 25 51 20 2a  d on %q and %Q *
9df80 2f 0a 20 20 20 20 20 20 20 20 2f 2a 20 69 66 28  /.        /* if(
9df90 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26   precision>=0 &&
9dfa0 20 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74   precision<lengt
9dfb0 68 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65  h ) length = pre
9dfc0 63 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20  cision; */.     
9dfd0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
9dfe0 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54  }.      case etT
9dff0 4f 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20  OKEN: {.        
9e000 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20  Token *pToken = 
9e010 76 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e  va_arg(ap, Token
9e020 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  *);.        if( 
9e030 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20  pToken ){.      
9e040 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
9e050 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
9e060 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  , (const char*)p
9e070 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e  Token->z, pToken
9e080 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ->n);.        }.
9e090 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
9e0a0 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20   width = 0;.    
9e0b0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
9e0c0 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74   }.      case et
9e0d0 53 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20  SRCLIST: {.     
9e0e0 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
9e0f0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72   = va_arg(ap, Sr
9e100 63 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20  cList*);.       
9e110 20 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28   int k = va_arg(
9e120 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
9e130 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
9e140 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26  _item *pItem = &
9e150 70 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20  pSrc->a[k];.    
9e160 20 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30      assert( k>=0
9e170 20 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63   && k<pSrc->nSrc
9e180 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
9e190 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  pItem->zDatabase
9e1a0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
9e1b0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
9e1c0 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65  end(pAccum, pIte
9e1d0 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31  m->zDatabase, -1
9e1e0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
9e1f0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
9e200 6e 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20  nd(pAccum, ".", 
9e210 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1);.        }.  
9e220 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
9e230 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63  AccumAppend(pAcc
9e240 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  um, pItem->zName
9e250 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c  , -1);.        l
9e260 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20  ength = width = 
9e270 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  0;.        break
9e280 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
9e290 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
9e2a0 20 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65     assert( xtype
9e2b0 3d 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20  ==etINVALID );. 
9e2c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
9e2d0 20 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45       }.    }/* E
9e2e0 6e 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74  nd switch over t
9e2f0 68 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a  he format type *
9e300 2f 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  /.    /*.    ** 
9e310 54 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  The text of the 
9e320 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f  conversion is po
9e330 69 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66  inted to by "buf
9e340 70 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a  pt" and is.    *
9e350 2a 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61  * "length" chara
9e360 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65  cters long.  The
9e370 20 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20   field width is 
9e380 22 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20  "width".  Do.   
9e390 20 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a   ** the output..
9e3a0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
9e3b0 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
9e3c0 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74   ){.      regist
9e3d0 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20  er int nspace;. 
9e3e0 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69       nspace = wi
9e3f0 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20  dth-length;.    
9e400 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29    if( nspace>0 )
9e410 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64  {.        append
9e420 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73  Space(pAccum, ns
9e430 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  pace);.      }. 
9e440 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e     }.    if( len
9e450 67 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73  gth>0 ){.      s
9e460 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
9e470 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66  pend(pAccum, buf
9e480 70 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20  pt, length);.   
9e490 20 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f   }.    if( flag_
9e4a0 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20  leftjustify ){. 
9e4b0 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e       register in
9e4c0 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20  t nspace;.      
9e4d0 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c  nspace = width-l
9e4e0 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28  ength;.      if(
9e4f0 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20   nspace>0 ){.   
9e500 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65       appendSpace
9e510 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29  (pAccum, nspace)
9e520 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
9e530 20 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29      if( zExtra )
9e540 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
9e550 66 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20  free(zExtra);.  
9e560 20 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f    }.  }/* End fo
9e570 72 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20  r loop over the 
9e580 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f  format string */
9e590 0a 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e  .} /* End of fun
9e5a0 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ction */../*.** 
9e5b0 41 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f  Append N bytes o
9e5c0 66 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f  f text from z to
9e5d0 20 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62   the StrAccum ob
9e5e0 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ject..*/.SQLITE_
9e5f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
9e600 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
9e610 6e 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20  nd(StrAccum *p, 
9e620 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
9e630 6e 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28  nt N){.  assert(
9e640 20 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b   z!=0 || N==0 );
9e650 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67  .  if( p->tooBig
9e660 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   | p->mallocFail
9e670 65 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61  ed ){.    testca
9e680 73 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20  se(p->tooBig);. 
9e690 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d     testcase(p->m
9e6a0 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20  allocFailed);.  
9e6b0 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
9e6c0 69 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e  if( N<0 ){.    N
9e6d0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
9e6e0 33 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28  30(z);.  }.  if(
9e6f0 20 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a   N==0 || NEVER(z
9e700 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ==0) ){.    retu
9e710 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  rn;.  }.  if( p-
9e720 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e  >nChar+N >= p->n
9e730 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61  Alloc ){.    cha
9e740 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28  r *zNew;.    if(
9e750 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29   !p->useMalloc )
9e760 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69  {.      p->tooBi
9e770 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d  g = 1;.      N =
9e780 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e   p->nAlloc - p->
9e790 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20  nChar - 1;.     
9e7a0 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20   if( N<=0 ){.   
9e7b0 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
9e7c0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
9e7d0 20 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20        i64 szNew 
9e7e0 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20  = p->nChar;.    
9e7f0 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31    szNew += N + 1
9e800 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65  ;.      if( szNe
9e810 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29  w > p->mxAlloc )
9e820 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
9e830 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70  3StrAccumReset(p
9e840 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f  );.        p->to
9e850 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  oBig = 1;.      
9e860 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
9e870 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
9e880 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29  ->nAlloc = (int)
9e890 73 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20  szNew;.      }. 
9e8a0 20 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69       zNew = sqli
9e8b0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
9e8c0 2d 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20  ->db, p->nAlloc 
9e8d0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65  );.      if( zNe
9e8e0 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  w ){.        mem
9e8f0 63 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65  cpy(zNew, p->zTe
9e900 78 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20  xt, p->nChar);. 
9e910 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
9e920 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a  rAccumReset(p);.
9e930 20 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74          p->zText
9e940 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d   = zNew;.      }
9e950 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d  else{.        p-
9e960 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
9e970 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  1;.        sqlit
9e980 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28  e3StrAccumReset(
9e990 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  p);.        retu
9e9a0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
9e9b0 7d 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26  }.  }.  memcpy(&
9e9c0 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61  p->zText[p->nCha
9e9d0 72 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e  r], z, N);.  p->
9e9e0 6e 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f  nChar += N;.}../
9e9f0 2a 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20  *.** Finish off 
9ea00 61 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69  a string by maki
9ea10 6e 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65  ng sure it is ze
9ea20 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a  ro-terminated..*
9ea30 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
9ea40 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74  er to the result
9ea50 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74  ing string.  Ret
9ea60 75 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f  urn a NULL.** po
9ea70 69 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e  inter if any kin
9ea80 64 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65  d of error was e
9ea90 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53  ncountered..*/.S
9eaa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
9eab0 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63  ar *sqlite3StrAc
9eac0 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63  cumFinish(StrAcc
9ead0 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  um *p){.  if( p-
9eae0 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d  >zText ){.    p-
9eaf0 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d  >zText[p->nChar]
9eb00 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d   = 0;.    if( p-
9eb10 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d  >useMalloc && p-
9eb20 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65  >zText==p->zBase
9eb30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65   ){.      p->zTe
9eb40 78 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  xt = sqlite3DbMa
9eb50 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70  llocRaw(p->db, p
9eb60 2d 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20  ->nChar+1 );.   
9eb70 20 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20     if( p->zText 
9eb80 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ){.        memcp
9eb90 79 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a  y(p->zText, p->z
9eba0 42 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31  Base, p->nChar+1
9ebb0 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
9ebc0 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f          p->mallo
9ebd0 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
9ebe0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
9ebf0 20 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74   return p->zText
9ec00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74  ;.}../*.** Reset
9ec10 20 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72   an StrAccum str
9ec20 69 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c  ing.  Reclaim al
9ec30 6c 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72  l malloced memor
9ec40 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
9ec50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
9ec60 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53  3StrAccumReset(S
9ec70 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69  trAccum *p){.  i
9ec80 66 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e  f( p->zText!=p->
9ec90 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c  zBase ){.    sql
9eca0 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62  ite3DbFree(p->db
9ecb0 2c 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d  , p->zText);.  }
9ecc0 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b  .  p->zText = 0;
9ecd0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
9ece0 6c 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63  lize a string ac
9ecf0 63 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c  cumulator.*/.SQL
9ed00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9ed10 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
9ed20 49 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70  Init(StrAccum *p
9ed30 2c 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69  , char *zBase, i
9ed40 6e 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20  nt n, int mx){. 
9ed50 20 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a   p->zText = p->z
9ed60 42 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20  Base = zBase;.  
9ed70 70 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e  p->db = 0;.  p->
9ed80 6e 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e  nChar = 0;.  p->
9ed90 6e 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d  nAlloc = n;.  p-
9eda0 3e 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20  >mxAlloc = mx;. 
9edb0 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20   p->useMalloc = 
9edc0 31 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d  1;.  p->tooBig =
9edd0 20 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46   0;.  p->mallocF
9ede0 61 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  ailed = 0;.}../*
9edf0 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d  .** Print into m
9ee00 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
9ee10 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
9ee20 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74  ().  Use the int
9ee30 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65  ernal.** %-conve
9ee40 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73  rsion extensions
9ee50 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
9ee60 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
9ee70 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65  3VMPrintf(sqlite
9ee80 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
9ee90 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c  r *zFormat, va_l
9eea0 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20  ist ap){.  char 
9eeb0 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65  *z;.  char zBase
9eec0 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55  [SQLITE_PRINT_BU
9eed0 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63  F_SIZE];.  StrAc
9eee0 63 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72  cum acc;.  asser
9eef0 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71  t( db!=0 );.  sq
9ef00 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
9ef10 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73  t(&acc, zBase, s
9ef20 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20  izeof(zBase),.  
9ef30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ef40 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53      db->aLimit[S
9ef50 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
9ef60 54 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d  TH]);.  acc.db =
9ef70 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58   db;.  sqlite3VX
9ef80 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20  Printf(&acc, 1, 
9ef90 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
9efa0 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63  z = sqlite3StrAc
9efb0 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b  cumFinish(&acc);
9efc0 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f  .  if( acc.mallo
9efd0 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64  cFailed ){.    d
9efe0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
9eff0 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
9f000 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  n z;.}../*.** Pr
9f010 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  int into memory 
9f020 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
9f030 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55  liteMalloc().  U
9f040 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  se the internal.
9f050 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20  ** %-conversion 
9f060 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
9f070 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
9f080 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e  ar *sqlite3MPrin
9f090 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  tf(sqlite3 *db, 
9f0a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
9f0b0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
9f0c0 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20  list ap;.  char 
9f0d0 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61  *z;.  va_start(a
9f0e0 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a  p, zFormat);.  z
9f0f0 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
9f100 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  tf(db, zFormat, 
9f110 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
9f120 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
9f130 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c  ../*.** Like sql
9f140 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62  ite3MPrintf(), b
9f150 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44  ut call sqlite3D
9f160 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20  bFree() on zStr 
9f170 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67  after formatting
9f180 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61  .** the string a
9f190 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  nd before return
9f1a0 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74  ning.  This rout
9f1b0 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ine is intended 
9f1c0 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f  to be used.** to
9f1d0 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74   modify an exist
9f1e0 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72  ing string.  For
9f1f0 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20   example:.**.** 
9f200 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65        x = sqlite
9f210 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20  3MPrintf(db, x, 
9f220 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69  "prefix %s suffi
9f230 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51  x", x);.**.*/.SQ
9f240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
9f250 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e  r *sqlite3MAppen
9f260 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  df(sqlite3 *db, 
9f270 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73  char *zStr, cons
9f280 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
9f290 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
9f2a0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a   ap;.  char *z;.
9f2b0 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
9f2c0 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73  Format);.  z = s
9f2d0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64  qlite3VMPrintf(d
9f2e0 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
9f2f0 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
9f300 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
9f310 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75  b, zStr);.  retu
9f320 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn z;.}../*.** P
9f330 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
9f340 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
9f350 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
9f360 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72    Omit the inter
9f370 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73  nal.** %-convers
9f380 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a  ion extensions..
9f390 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  */.SQLITE_API ch
9f3a0 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72  ar *sqlite3_vmpr
9f3b0 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20  intf(const char 
9f3c0 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73  *zFormat, va_lis
9f3d0 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a  t ap){.  char *z
9f3e0 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53  ;.  char zBase[S
9f3f0 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f  QLITE_PRINT_BUF_
9f400 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75  SIZE];.  StrAccu
9f410 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53  m acc;.#ifndef S
9f420 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
9f430 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  NIT.  if( sqlite
9f440 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29  3_initialize() )
9f450 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
9f460 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  f.  sqlite3StrAc
9f470 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42  cumInit(&acc, zB
9f480 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73  ase, sizeof(zBas
9f490 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  e), SQLITE_MAX_L
9f4a0 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65  ENGTH);.  sqlite
9f4b0 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20  3VXPrintf(&acc, 
9f4c0 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  0, zFormat, ap);
9f4d0 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74  .  z = sqlite3St
9f4e0 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63  rAccumFinish(&ac
9f4f0 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  c);.  return z;.
9f500 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69  }../*.** Print i
9f510 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
9f520 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
9f530 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d  _malloc()().  Om
9f540 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  it the internal.
9f550 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20  ** %-conversion 
9f560 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
9f570 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
9f580 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
9f590 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
9f5a0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
9f5b0 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20  list ap;.  char 
9f5c0 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  *z;.#ifndef SQLI
9f5d0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
9f5e0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
9f5f0 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65  nitialize() ) re
9f600 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20  turn 0;.#endif. 
9f610 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
9f620 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71  ormat);.  z = sq
9f630 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a  lite3_vmprintf(z
9f640 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76  Format, ap);.  v
9f650 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74  a_end(ap);.  ret
9f660 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
9f670 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
9f680 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e  () works like sn
9f690 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20  printf() except 
9f6a0 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20  that it ignores 
9f6b0 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c  the.** current l
9f6c0 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20  ocale settings. 
9f6d0 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61   This is importa
9f6e0 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65  nt for SQLite be
9f6f0 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20  cause we.** are 
9f700 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20  not able to use 
9f710 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63  a "," as the dec
9f720 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c  imal point in pl
9f730 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a  ace of "." as.**
9f740 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f   specified by so
9f750 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53  me locales..*/.S
9f760 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
9f770 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
9f780 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42  (int n, char *zB
9f790 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  uf, const char *
9f7a0 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
9f7b0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c   char *z;.  va_l
9f7c0 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63  ist ap;.  StrAcc
9f7d0 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e  um acc;..  if( n
9f7e0 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  <=0 ){.    retur
9f7f0 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71  n zBuf;.  }.  sq
9f800 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
9f810 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c  t(&acc, zBuf, n,
9f820 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61   0);.  acc.useMa
9f830 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73  lloc = 0;.  va_s
9f840 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29  tart(ap,zFormat)
9f850 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  ;.  sqlite3VXPri
9f860 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f  ntf(&acc, 0, zFo
9f870 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
9f880 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73  end(ap);.  z = s
9f890 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
9f8a0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65  nish(&acc);.  re
9f8b0 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64  turn z;.}..#if d
9f8c0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
9f8d0 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72  BUG)./*.** A ver
9f8e0 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29  sion of printf()
9f8f0 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64   that understand
9f900 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f  s %lld.  Used fo
9f910 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20  r debugging..** 
9f920 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69  The printf() bui
9f930 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72  lt into some ver
9f940 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73  sions of windows
9f950 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73   does not unders
9f960 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64  tand %lld.** and
9f970 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f   segfaults if yo
9f980 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67  u give it a long
9f990 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51   long int..*/.SQ
9f9a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
9f9b0 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  d sqlite3DebugPr
9f9c0 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20  intf(const char 
9f9d0 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
9f9e0 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
9f9f0 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20  StrAccum acc;.  
9fa00 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a  char zBuf[500];.
9fa10 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
9fa20 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66  mInit(&acc, zBuf
9fa30 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20  , sizeof(zBuf), 
9fa40 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c  0);.  acc.useMal
9fa50 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74  loc = 0;.  va_st
9fa60 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b  art(ap,zFormat);
9fa70 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e  .  sqlite3VXPrin
9fa80 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72  tf(&acc, 0, zFor
9fa90 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
9faa0 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65  nd(ap);.  sqlite
9fab0 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
9fac0 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66  &acc);.  fprintf
9fad0 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42  (stdout,"%s", zB
9fae0 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74  uf);.  fflush(st
9faf0 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  dout);.}.#endif.
9fb00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
9fb10 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63   End of printf.c
9fb20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
9fb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
9fb50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
9fb60 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64   Begin file rand
9fb70 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  om.c ***********
9fb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
9fba0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
9fbb0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
9fbc0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
9fbd0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
9fbe0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
9fbf0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
9fc00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
9fc10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
9fc20 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
9fc30 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
9fc40 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
9fc50 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
9fc60 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
9fc70 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
9fc80 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
9fc90 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
9fca0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
9fcb0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
9fcc0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
9fcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fd10 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
9fd20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
9fd30 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
9fd40 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  a pseudo-random 
9fd50 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61  number.** genera
9fd60 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53  tor (PRNG) for S
9fd70 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e  QLite..**.** Ran
9fd80 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20  dom numbers are 
9fd90 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20  used by some of 
9fda0 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63  the database bac
9fdb0 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a  kends in order.*
9fdc0 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61  * to generate ra
9fdd0 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79  ndom integer key
9fde0 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20  s for tables or 
9fdf0 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73  random filenames
9fe00 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61 6e  ..**.** $Id: ran
9fe10 64 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30 30  dom.c,v 1.29 200
9fe20 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34  8/12/10 19:26:24
9fe30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
9fe40 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 73  /* All threads s
9fe50 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 61  hare a single ra
9fe60 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
9fe70 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73  rator..** This s
9fe80 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20  tructure is the 
9fe90 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
9fea0 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a   the generator..
9feb0 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  */.static SQLITE
9fec0 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69  _WSD struct sqli
9fed0 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 20  te3PrngType {.  
9fee0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73  unsigned char is
9fef0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f  Init;          /
9ff00 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 61  * True if initia
9ff10 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67  lized */.  unsig
9ff20 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 20  ned char i, j;  
9ff30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
9ff40 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a  te variables */.
9ff50 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
9ff60 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 20  s[256];         
9ff70 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62   /* State variab
9ff80 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33  les */.} sqlite3
9ff90 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  Prng;../*.** Get
9ffa0 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20   a single 8-bit 
9ffb0 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f  random value fro
9ffc0 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20  m the RC4 PRNG. 
9ffd0 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75   The Mutex.** mu
9ffe0 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65  st be held while
9fff0 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20   executing this 
a0000 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57  routine..**.** W
a0010 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 20  hy not just use 
a0020 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f 6d  a library random
a0030 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20   generator like 
a0040 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 68  lrand48() for th
a0050 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 74  is?.** Because t
a0060 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f  he OP_NewRowid o
a0070 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 42  pcode in the VDB
a0080 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 76  E depends on hav
a0090 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f  ing a very.** go
a00a0 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e  od source of ran
a00b0 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 68  dom numbers.  Th
a00c0 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 72  e lrand48() libr
a00d0 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  ary function may
a00e0 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64  .** well be good
a00f0 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d 61   enough.  But ma
a0100 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79  ybe not.  Or may
a0110 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 73  be lrand48() has
a0120 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20   some.** subtle 
a0130 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65  problems on some
a0140 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 6f   systems that co
a0150 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65  uld cause proble
a0160 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 0a  ms.  It is hard.
a0170 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20  ** to know.  To 
a0180 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 73  minimize the ris
a0190 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 75  k of problems du
a01a0 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 38  e to bad lrand48
a01b0 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ().** implementa
a01c0 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 73  tions, SQLite us
a01d0 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e  es this random n
a01e0 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20  umber generator 
a01f0 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c  based.** on RC4,
a0200 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 77   which we know w
a0210 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a  orks very well..
a0220 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20  **.** (Later):  
a0230 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77  Actually, OP_New
a0240 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 64  Rowid does not d
a0250 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20  epend on a good 
a0260 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e  source of.** ran
a0270 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65  domness any more
a0280 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c  .  But we will l
a0290 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 69  eave this code i
a02a0 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a  n all the same..
a02b0 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 6e  */.static u8 ran
a02c0 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a 20  domByte(void){. 
a02d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74   unsigned char t
a02e0 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 73  ;...  /* The "ws
a02f0 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c  dPrng" macro wil
a0300 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  l resolve to the
a0310 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e   pseudo-random n
a0320 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 0a  umber generator.
a0330 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f    ** state vecto
a0340 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20  r.  If writable 
a0350 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75  static data is u
a0360 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68  nsupported on th
a0370 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77  e target,.  ** w
a0380 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65  e have to locate
a0390 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f   the state vecto
a03a0 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20  r at run-time.  
a03b0 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d  In the more comm
a03c0 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65  on.  ** case whe
a03d0 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74  re writable stat
a03e0 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f  ic data is suppo
a03f0 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 61  rted, wsdPrng ca
a0400 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79  n refer directly
a0410 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 71  .  ** to the "sq
a0420 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 65  lite3Prng" state
a0430 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64   vector declared
a0440 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66   above..  */.#if
a0450 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
a0460 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 6c  WSD.  struct sql
a0470 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 20  ite3PrngType *p 
a0480 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74  = &GLOBAL(struct
a0490 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
a04a0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a  , sqlite3Prng);.
a04b0 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67  # define wsdPrng
a04c0 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65   p[0].#else.# de
a04d0 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 6c  fine wsdPrng sql
a04e0 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a  ite3Prng.#endif.
a04f0 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
a0500 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
a0510 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  he random number
a0520 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c   generator once,
a0530 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20  .  ** the first 
a0540 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e  time this routin
a0550 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68  e is called.  Th
a0560 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f 65  e seed value doe
a0570 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20  s.  ** not need 
a0580 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74  to contain a lot
a0590 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73   of randomness s
a05a0 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 0a  ince we are not.
a05b0 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 64    ** trying to d
a05c0 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 74  o secure encrypt
a05d0 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 20  ion or anything 
a05e0 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a  like that....  *
a05f0 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69  *.  ** Nothing i
a0600 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 61  n this file or a
a0610 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e 20  nywhere else in 
a0620 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 20  SQLite does any 
a0630 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63  kind of.  ** enc
a0640 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 43  ryption.  The RC
a0650 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 62  4 algorithm is b
a0660 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 50  eing used as a P
a0670 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e 64  RNG (pseudo-rand
a0680 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67  om.  ** number g
a0690 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 73  enerator) not as
a06a0 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 64   an encryption d
a06b0 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  evice..  */.  if
a06c0 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e 69  ( !wsdPrng.isIni
a06d0 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  t ){.    int i;.
a06e0 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b      char k[256];
a06f0 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d  .    wsdPrng.j =
a0700 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e   0;.    wsdPrng.
a0710 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  i = 0;.    sqlit
a0720 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73  e3OsRandomness(s
a0730 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28  qlite3_vfs_find(
a0740 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20  0), 256, k);.   
a0750 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b   for(i=0; i<256;
a0760 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 64   i++){.      wsd
a0770 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29  Prng.s[i] = (u8)
a0780 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  i;.    }.    for
a0790 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b  (i=0; i<256; i++
a07a0 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67  ){.      wsdPrng
a07b0 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b  .j += wsdPrng.s[
a07c0 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20  i] + k[i];.     
a07d0 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77   t = wsdPrng.s[w
a07e0 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20  sdPrng.j];.     
a07f0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a0800 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e  ng.j] = wsdPrng.
a0810 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50  s[i];.      wsdP
a0820 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20  rng.s[i] = t;.  
a0830 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e    }.    wsdPrng.
a0840 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a  isInit = 1;.  }.
a0850 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
a0860 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65  nd return single
a0870 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a   random byte.  *
a0880 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b  /.  wsdPrng.i++;
a0890 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73  .  t = wsdPrng.s
a08a0 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77  [wsdPrng.i];.  w
a08b0 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20  sdPrng.j += t;. 
a08c0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a08d0 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e  ng.i] = wsdPrng.
a08e0 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20  s[wsdPrng.j];.  
a08f0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
a0900 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d  g.j] = t;.  t +=
a0910 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a0920 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20  ng.i];.  return 
a0930 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a  wsdPrng.s[t];.}.
a0940 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20  ./*.** Return N 
a0950 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f  random bytes..*/
a0960 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
a0970 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
a0980 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20  ess(int N, void 
a0990 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e  *pBuf){.  unsign
a09a0 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20  ed char *zBuf = 
a09b0 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45  pBuf;.#if SQLITE
a09c0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71  _THREADSAFE.  sq
a09d0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
a09e0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
a09f0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
a0a00 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29  TEX_STATIC_PRNG)
a0a10 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ;.#endif.  sqlit
a0a20 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
a0a30 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20  utex);.  while( 
a0a40 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75  N-- ){.    *(zBu
a0a50 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74  f++) = randomByt
a0a60 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  e();.  }.  sqlit
a0a70 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
a0a80 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  utex);.}..#ifnde
a0a90 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
a0aa0 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  ILTIN_TEST./*.**
a0ab0 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72   For testing pur
a0ac0 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69  poses, we someti
a0ad0 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73  mes want to pres
a0ae0 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f  erve the state o
a0af0 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65  f.** PRNG and re
a0b00 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74  store the PRNG t
a0b10 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74  o its saved stat
a0b20 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d  e at a later tim
a0b30 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65  e, or.** to rese
a0b40 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74  t the PRNG to it
a0b50 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e  s initial state.
a0b60 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
a0b70 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74   accomplish.** t
a0b80 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a  hose tasks..**.*
a0b90 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65  * The sqlite3_te
a0ba0 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74  st_control() int
a0bb0 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65  erface calls the
a0bc0 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a  se routines to.*
a0bd0 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52  * control the PR
a0be0 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51  NG..*/.static SQ
a0bf0 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
a0c00 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20  sqlite3PrngType 
a0c10 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67  sqlite3SavedPrng
a0c20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
a0c30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e   void sqlite3Prn
a0c40 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29  gSaveState(void)
a0c50 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20  {.  memcpy(.    
a0c60 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73  &GLOBAL(struct s
a0c70 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20  qlite3PrngType, 
a0c80 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67  sqlite3SavedPrng
a0c90 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73  ),.    &GLOBAL(s
a0ca0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a0cb0 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72  gType, sqlite3Pr
a0cc0 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28  ng),.    sizeof(
a0cd0 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29  sqlite3Prng).  )
a0ce0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
a0cf0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
a0d00 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28  rngRestoreState(
a0d10 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28  void){.  memcpy(
a0d20 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72  .    &GLOBAL(str
a0d30 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54  uct sqlite3PrngT
a0d40 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67  ype, sqlite3Prng
a0d50 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73  ),.    &GLOBAL(s
a0d60 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a0d70 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61  gType, sqlite3Sa
a0d80 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 69  vedPrng),.    si
a0d90 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67  zeof(sqlite3Prng
a0da0 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  ).  );.}.SQLITE_
a0db0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a0dc0 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61  ite3PrngResetSta
a0dd0 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42  te(void){.  GLOB
a0de0 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  AL(struct sqlite
a0df0 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74  3PrngType, sqlit
a0e00 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d  e3Prng).isInit =
a0e10 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
a0e20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
a0e30 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a  TIN_TEST */../**
a0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
a0e50 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a   of random.c ***
a0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
a0e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
a0ea0 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a  in file utf.c **
a0eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
a0ee0 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 33  ** 2004 April 13
a0ef0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
a0f00 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
a0f10 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
a0f20 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
a0f30 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
a0f40 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
a0f50 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
a0f60 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
a0f70 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
a0f80 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
a0f90 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
a0fa0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
a0fb0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
a0fc0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
a0fd0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
a0fe0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
a0ff0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
a1000 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
a1010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
a1050 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
a1060 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75  tains routines u
a1070 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65  sed to translate
a1080 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20   between UTF-8, 
a1090 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d  .** UTF-16, UTF-
a10a0 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36  16BE, and UTF-16
a10b0 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75  LE..**.** $Id: u
a10c0 74 66 2e 63 2c 76 20 31 2e 37 33 20 32 30 30 39  tf.c,v 1.73 2009
a10d0 2f 30 34 2f 30 31 20 31 38 3a 34 30 3a 33 32 20  /04/01 18:40:32 
a10e0 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20  drh Exp $.**.** 
a10f0 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a 0a  Notes on UTF-8:.
a1100 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 20  **.**   Byte-0  
a1110 20 20 42 79 74 65 2d 31 20 20 20 20 42 79 74 65    Byte-1    Byte
a1120 2d 32 20 20 20 20 42 79 74 65 2d 33 20 20 20 20  -2    Byte-3    
a1130 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78 78  Value.**  0xxxxx
a1140 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  xx              
a1150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1160 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30     00000000 0000
a1170 30 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a 2a  0000 0xxxxxxx.**
a1180 20 20 31 31 30 79 79 79 79 79 20 20 31 30 78 78    110yyyyy  10xx
a1190 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20  xxxx            
a11a0 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30             00000
a11b0 30 30 30 20 30 30 30 30 30 79 79 79 20 79 79 78  000 00000yyy yyx
a11c0 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a 7a  xxxxx.**  1110zz
a11d0 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 30  zz  10yyyyyy  10
a11e0 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20  xxxxxx          
a11f0 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a     00000000 zzzz
a1200 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a  yyyy yyxxxxxx.**
a1210 20 20 31 31 31 31 30 75 75 75 20 20 31 30 75 75    11110uuu  10uu
a1220 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20  zzzz  10yyyyyy  
a1230 31 30 78 78 78 78 78 78 20 20 20 30 30 30 75 75  10xxxxxx   000uu
a1240 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78  uuu zzzzyyyy yyx
a1250 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e  xxxxx.**.**.** N
a1260 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a 20  otes on UTF-16: 
a1270 20 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d 75   (with wwww+1==u
a1280 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20  uuuu).**.**     
a1290 20 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20 20   Word-0         
a12a0 20 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20 20        Word-1    
a12b0 20 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20        Value.**  
a12c0 31 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a 79  110110ww wwzzzzy
a12d0 79 20 20 20 31 31 30 31 31 31 79 79 20 79 79 78  y   110111yy yyx
a12e0 78 78 78 78 78 20 20 20 20 30 30 30 75 75 75 75  xxxxx    000uuuu
a12f0 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78  u zzzzyyyy yyxxx
a1300 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79  xxx.**  zzzzyyyy
a1310 20 79 79 78 78 78 78 78 78 20 20 20 20 20 20 20   yyxxxxxx       
a1320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1330 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79   00000000 zzzzyy
a1340 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a  yy yyxxxxxx.**.*
a1350 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74 65  *.** BOM or Byte
a1360 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20   Order Mark:.** 
a1370 20 20 20 20 30 78 66 66 20 30 78 66 65 20 20 20      0xff 0xfe   
a1380 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75 74  little-endian ut
a1390 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20  f-16 follows.** 
a13a0 20 20 20 20 30 78 66 65 20 30 78 66 66 20 20 20      0xfe 0xff   
a13b0 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31  big-endian utf-1
a13c0 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a  6 follows.**.*/.
a13d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a13e0 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74 2e  Include vdbeInt.
a13f0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
a1400 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a  of utf.c *******
a1410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a1420 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a1430 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 49  Begin file vdbeI
a1440 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
a1450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a1470 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65  /*.** 2003 Septe
a1480 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  mber 6.**.** The
a1490 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
a14a0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
a14b0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
a14c0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
a14d0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
a14e0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
a14f0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
a1500 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
a1510 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
a1520 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
a1530 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
a1540 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
a1550 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
a1560 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
a1570 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
a1580 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
a1590 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
a15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a15c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a15d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a15e0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  *****.** This is
a15f0 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65   the header file
a1600 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   for information
a1610 20 74 68 61 74 20 69 73 20 70 72 69 76 61 74 65   that is private
a1620 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e   to the.** VDBE.
a1630 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69    This informati
a1640 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20 62  on used to all b
a1650 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20  e at the top of 
a1660 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f  the single.** so
a1670 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20 22  urce code file "
a1680 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20 74  vdbe.c".  When t
a1690 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65 20  hat file became 
a16a0 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a 2a  too big (over.**
a16b0 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e 67   6000 lines long
a16c0 29 20 69 74 20 77 61 73 20 73 70 6c 69 74 20 75  ) it was split u
a16d0 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20 73  p into several s
a16e0 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e 64  maller files and
a16f0 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72 20  .** this header 
a1700 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20  information was 
a1710 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a 2a  factored out..**
a1720 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 49 6e 74  .** $Id: vdbeInt
a1730 2e 68 2c 76 20 31 2e 31 37 30 20 32 30 30 39 2f  .h,v 1.170 2009/
a1740 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30 20 64  05/04 11:42:30 d
a1750 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
a1760 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 42  .*/.#ifndef _VDB
a1770 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20  EINT_H_.#define 
a1780 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a  _VDBEINT_H_../*.
a1790 2a 2a 20 69 6e 74 54 6f 4b 65 79 28 29 20 61 6e  ** intToKey() an
a17a0 64 20 6b 65 79 54 6f 49 6e 74 28 29 20 75 73 65  d keyToInt() use
a17b0 64 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 74  d to transform t
a17c0 68 65 20 72 6f 77 69 64 2e 20 20 42 75 74 20 77  he rowid.  But w
a17d0 69 74 68 0a 2a 2a 20 74 68 65 20 6c 61 74 65 73  ith.** the lates
a17e0 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68  t versions of th
a17f0 65 20 64 65 73 69 67 6e 20 74 68 65 79 20 61 72  e design they ar
a1800 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 64 65  e no-ops..*/.#de
a1810 66 69 6e 65 20 6b 65 79 54 6f 49 6e 74 28 58 29  fine keyToInt(X)
a1820 20 20 20 28 58 29 0a 23 64 65 66 69 6e 65 20 69     (X).#define i
a1830 6e 74 54 6f 4b 65 79 28 58 29 20 20 20 28 58 29  ntToKey(X)   (X)
a1840 0a 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73 20  .../*.** SQL is 
a1850 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20  translated into 
a1860 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 69 6e  a sequence of in
a1870 73 74 72 75 63 74 69 6f 6e 73 20 74 6f 20 62 65  structions to be
a1880 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 62 79 20  .** executed by 
a1890 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  a virtual machin
a18a0 65 2e 20 20 45 61 63 68 20 69 6e 73 74 72 75 63  e.  Each instruc
a18b0 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 61  tion is an insta
a18c0 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f  nce.** of the fo
a18d0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
a18e0 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  e..*/.typedef st
a18f0 72 75 63 74 20 56 64 62 65 4f 70 20 4f 70 3b 0a  ruct VdbeOp Op;.
a1900 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76  ./*.** Boolean v
a1910 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66  alues.*/.typedef
a1920 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 42   unsigned char B
a1930 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75  ool;../*.** A cu
a1940 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 65  rsor is a pointe
a1950 72 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  r into a single 
a1960 42 54 72 65 65 20 77 69 74 68 69 6e 20 61 20 64  BTree within a d
a1970 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
a1980 20 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20   The cursor can 
a1990 73 65 65 6b 20 74 6f 20 61 20 42 54 72 65 65 20  seek to a BTree 
a19a0 65 6e 74 72 79 20 77 69 74 68 20 61 20 70 61 72  entry with a par
a19b0 74 69 63 75 6c 61 72 20 6b 65 79 2c 20 6f 72 0a  ticular key, or.
a19c0 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c  ** loop over all
a19d0 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20   entries of the 
a19e0 42 74 72 65 65 2e 20 20 59 6f 75 20 63 61 6e 20  Btree.  You can 
a19f0 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e 65 77 20  also insert new 
a1a00 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72 69 65 73  BTree.** entries
a1a10 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 68 65   or retrieve the
a1a20 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 72 6f   key or data fro
a1a30 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  m the entry that
a1a40 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69   the cursor.** i
a1a50 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
a1a60 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20  ting to..** .** 
a1a70 45 76 65 72 79 20 63 75 72 73 6f 72 20 74 68 61  Every cursor tha
a1a80 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  t the virtual ma
a1a90 63 68 69 6e 65 20 68 61 73 20 6f 70 65 6e 20 69  chine has open i
a1aa0 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79  s represented by
a1ab0 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20   an.** instance 
a1ac0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
a1ad0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
a1ae0 2a 20 49 66 20 74 68 65 20 56 64 62 65 43 75 72  * If the VdbeCur
a1af0 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52 6f 77  sor.isTriggerRow
a1b00 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 74 20   flag is set it 
a1b10 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20  means that this 
a1b20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72 65 61  cursor is.** rea
a1b30 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  lly a single row
a1b40 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73   that represents
a1b50 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20   the NEW or OLD 
a1b60 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 66 0a  pseudo-table of.
a1b70 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67 65 72  ** a row trigger
a1b80 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f 72 20  .  The data for 
a1b90 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f 72 65  the row is store
a1ba0 64 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e  d in VdbeCursor.
a1bb0 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65  pData and.** the
a1bc0 20 72 6f 77 69 64 20 69 73 20 69 6e 20 56 64 62   rowid is in Vdb
a1bd0 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a 2f  eCursor.iKey..*/
a1be0 0a 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73  .struct VdbeCurs
a1bf0 6f 72 20 7b 0a 20 20 42 74 43 75 72 73 6f 72 20  or {.  BtCursor 
a1c00 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20  *pCursor;    /* 
a1c10 54 68 65 20 63 75 72 73 6f 72 20 73 74 72 75 63  The cursor struc
a1c20 74 75 72 65 20 6f 66 20 74 68 65 20 62 61 63 6b  ture of the back
a1c30 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  end */.  int iDb
a1c40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
a1c50 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 73 6f  * Index of curso
a1c60 72 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 62  r database in db
a1c70 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d 31 29 20  ->aDb[] (or -1) 
a1c80 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77  */.  i64 lastRow
a1c90 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61  id;        /* La
a1ca0 73 74 20 72 6f 77 69 64 20 66 72 6f 6d 20 61 20  st rowid from a 
a1cb0 4e 65 78 74 20 6f 72 20 4e 65 78 74 49 64 78 20  Next or NextIdx 
a1cc0 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 42  operation */.  B
a1cd0 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 20 20 20  ool zeroed;     
a1ce0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a1cf0 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e 64 20 72  zeroed out and r
a1d00 65 61 64 79 20 66 6f 72 20 72 65 75 73 65 20 2a  eady for reuse *
a1d10 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 64 49 73  /.  Bool rowidIs
a1d20 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 54 72 75  Valid;    /* Tru
a1d30 65 20 69 66 20 6c 61 73 74 52 6f 77 69 64 20 69  e if lastRowid i
a1d40 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f  s valid */.  Boo
a1d50 6c 20 61 74 46 69 72 73 74 3b 20 20 20 20 20 20  l atFirst;      
a1d60 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f     /* True if po
a1d70 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 73 74 20  inting to first 
a1d80 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20  entry */.  Bool 
a1d90 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 3b 20  useRandomRowid; 
a1da0 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e 65 77   /* Generate new
a1db0 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20   record numbers 
a1dc0 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f  semi-randomly */
a1dd0 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b  .  Bool nullRow;
a1de0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
a1df0 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   if pointing to 
a1e00 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20 64 61  a row with no da
a1e10 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 70 73 65  ta */.  Bool pse
a1e20 75 64 6f 54 61 62 6c 65 3b 20 20 20 20 20 2f 2a  udoTable;     /*
a1e30 20 54 68 69 73 20 69 73 20 61 20 4e 45 57 20 6f   This is a NEW o
a1e40 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62  r OLD pseudo-tab
a1e50 6c 65 73 20 6f 66 20 61 20 74 72 69 67 67 65 72  les of a trigger
a1e60 20 2a 2f 0a 20 20 42 6f 6f 6c 20 65 70 68 65 6d   */.  Bool ephem
a1e70 50 73 65 75 64 6f 54 61 62 6c 65 3b 0a 20 20 42  PseudoTable;.  B
a1e80 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 65  ool deferredMove
a1e90 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 74  to;  /* A call t
a1ea0 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  o sqlite3BtreeMo
a1eb0 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 64  veto() is needed
a1ec0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 62   */.  Bool isTab
a1ed0 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  le;         /* T
a1ee0 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 72  rue if a table r
a1ef0 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 72  equiring integer
a1f00 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20   keys */.  Bool 
a1f10 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  isIndex;        
a1f20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 69   /* True if an i
a1f30 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ndex containing 
a1f40 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64  keys only - no d
a1f50 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f 76  ata */.  i64 mov
a1f60 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 2f  etoTarget;     /
a1f70 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
a1f80 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 74  e deferred sqlit
a1f90 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20  e3BtreeMoveto() 
a1fa0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b  */.  Btree *pBt;
a1fb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
a1fc0 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c 64  parate file hold
a1fd0 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ing temporary ta
a1fe0 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61  ble */.  int nDa
a1ff0 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ta;            /
a2000 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
a2010 73 20 69 6e 20 70 44 61 74 61 20 2a 2f 0a 20 20  s in pData */.  
a2020 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20  char *pData;    
a2030 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f        /* Data fo
a2040 72 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70  r a NEW or OLD p
a2050 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20  seudo-table */. 
a2060 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20   i64 iKey;      
a2070 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 66 6f         /* Key fo
a2080 72 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44  r the NEW or OLD
a2090 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 72 6f   pseudo-table ro
a20a0 77 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  w */.  KeyInfo *
a20b0 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 2f 2a 20  pKeyInfo;    /* 
a20c0 49 6e 66 6f 20 61 62 6f 75 74 20 69 6e 64 65 78  Info about index
a20d0 20 6b 65 79 73 20 6e 65 65 64 65 64 20 62 79 20   keys needed by 
a20e0 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a 2f  index cursors */
a20f0 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20  .  int nField;  
a2100 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
a2110 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
a2120 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  the header */.  
a2130 69 36 34 20 73 65 71 43 6f 75 6e 74 3b 20 20 20  i64 seqCount;   
a2140 20 20 20 20 20 20 2f 2a 20 53 65 71 75 65 6e 63        /* Sequenc
a2150 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73  e counter */.  s
a2160 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
a2170 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b  or *pVtabCursor;
a2180 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
a2190 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61  for a virtual ta
a21a0 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ble */.  const s
a21b0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
a21c0 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d  Module;     /* M
a21d0 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f 72  odule for cursor
a21e0 20 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a   pVtabCursor */.
a21f0 0a 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20  .  /* Result of 
a2200 6c 61 73 74 20 73 71 6c 69 74 65 33 42 74 72 65  last sqlite3Btre
a2210 65 4d 6f 76 65 74 6f 28 29 20 64 6f 6e 65 20 62  eMoveto() done b
a2220 79 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74  y an OP_NotExist
a2230 73 20 6f 72 20 0a 20 20 2a 2a 20 4f 50 5f 49 73  s or .  ** OP_Is
a2240 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 20 6f 6e  Unique opcode on
a2250 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f   this cursor. */
a2260 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c  .  int seekResul
a2270 74 3b 0a 0a 20 20 2f 2a 20 43 61 63 68 65 64 20  t;..  /* Cached 
a2280 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
a2290 74 20 74 68 65 20 68 65 61 64 65 72 20 66 6f 72  t the header for
a22a0 20 74 68 65 20 64 61 74 61 20 72 65 63 6f 72 64   the data record
a22b0 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 63   that the.  ** c
a22c0 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74  ursor is current
a22d0 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20  ly pointing to. 
a22e0 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 63   Only valid if c
a22f0 61 63 68 65 56 61 6c 69 64 20 69 73 20 74 72 75  acheValid is tru
a2300 65 2e 0a 20 20 2a 2a 20 61 52 6f 77 20 6d 69 67  e..  ** aRow mig
a2310 68 74 20 70 6f 69 6e 74 20 74 6f 20 28 65 70 68  ht point to (eph
a2320 65 6d 65 72 61 6c 29 20 64 61 74 61 20 66 6f 72  emeral) data for
a2330 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
a2340 2c 20 6f 72 20 69 74 20 6d 69 67 68 74 0a 20 20  , or it might.  
a2350 2a 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f  ** be NULL..  */
a2360 0a 20 20 69 6e 74 20 63 61 63 68 65 53 74 61 74  .  int cacheStat
a2370 75 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68  us;      /* Cach
a2380 65 20 69 73 20 76 61 6c 69 64 20 69 66 20 74 68  e is valid if th
a2390 69 73 20 6d 61 74 63 68 65 73 20 56 64 62 65 2e  is matches Vdbe.
a23a0 63 61 63 68 65 43 74 72 20 2a 2f 0a 20 20 69 6e  cacheCtr */.  in
a23b0 74 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20  t payloadSize;  
a23c0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
a23d0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
a23e0 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
a23f0 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20  u32 *aType;     
a2400 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 76 61        /* Type va
a2410 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 6e 74  lues for all ent
a2420 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ries in the reco
a2430 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66  rd */.  u32 *aOf
a2440 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  fset;         /*
a2450 20 43 61 63 68 65 64 20 6f 66 66 73 65 74 73 20   Cached offsets 
a2460 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
a2470 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64 61 74  each columns dat
a2480 61 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f 77 3b  a */.  u8 *aRow;
a2490 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a24a0 44 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  Data for the cur
a24b0 72 65 6e 74 20 72 6f 77 2c 20 69 66 20 61 6c 6c  rent row, if all
a24c0 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a   on one page */.
a24d0 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  };.typedef struc
a24e0 74 20 56 64 62 65 43 75 72 73 6f 72 20 56 64 62  t VdbeCursor Vdb
a24f0 65 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20  eCursor;../*.** 
a2500 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64 62 65  A value for Vdbe
a2510 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61 6c 69  Cursor.cacheVali
a2520 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65  d that means the
a2530 20 63 61 63 68 65 20 69 73 20 61 6c 77 61 79 73   cache is always
a2540 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65   invalid..*/.#de
a2550 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41 4c 45  fine CACHE_STALE
a2560 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e   0../*.** Intern
a2570 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65 20 6d  ally, the vdbe m
a2580 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61 72 6c  anipulates nearl
a2590 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73  y all SQL values
a25a0 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63   as Mem.** struc
a25b0 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65 6d 20  tures. Each Mem 
a25c0 73 74 72 75 63 74 20 6d 61 79 20 63 61 63 68 65  struct may cache
a25d0 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72 65 73   multiple repres
a25e0 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72 69 6e  entations (strin
a25f0 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20 65 74  g,.** integer et
a2600 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d 65 20  c.) of the same 
a2610 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75 65 20  value.  A value 
a2620 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 4d  (and therefore M
a2630 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a 2a 2a  em structure).**
a2640 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69   has the followi
a2650 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a  ng properties:.*
a2660 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75 65 20  *.** Each value 
a2670 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74 20 74  has a manifest t
a2680 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66 65 73  ype. The manifes
a2690 74 20 74 79 70 65 20 6f 66 20 74 68 65 20 76 61  t type of the va
a26a0 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e  lue stored.** in
a26b0 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20 69 73   a Mem struct is
a26c0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
a26d0 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d   MemType(Mem*) m
a26e0 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65 20 69  acro. The type i
a26f0 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49  s.** one of SQLI
a2700 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f  TE_NULL, SQLITE_
a2710 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54 45 5f  INTEGER, SQLITE_
a2720 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58  REAL, SQLITE_TEX
a2730 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42  T or.** SQLITE_B
a2740 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d  LOB..*/.struct M
a2750 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20  em {.  union {. 
a2760 20 20 20 69 36 34 20 69 3b 20 20 20 20 20 20 20     i64 i;       
a2770 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
a2780 72 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20  r value. */.    
a2790 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20  int nZero;      
a27a0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
a27b0 20 62 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 73   bit MEM_Zero is
a27c0 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20 2a 2f   set in flags */
a27d0 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44  .    FuncDef *pD
a27e0 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 64  ef;      /* Used
a27f0 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 73   only when flags
a2800 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 20  ==MEM_Agg */.   
a2810 20 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74   RowSet *pRowSet
a2820 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c  ;    /* Used onl
a2830 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45  y when flags==ME
a2840 4d 5f 52 6f 77 53 65 74 20 2a 2f 0a 20 20 7d 20  M_RowSet */.  } 
a2850 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20  u;.  double r;  
a2860 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c           /* Real
a2870 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69   value */.  sqli
a2880 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
a2890 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65  /* The associate
a28a0 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  d database conne
a28b0 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20  ction */.  char 
a28c0 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  *z;            /
a28d0 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42  * String or BLOB
a28e0 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20   value */.  int 
a28f0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
a2900 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
a2910 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e  racters in strin
a2920 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69  g value, excludi
a2930 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36  ng '\0' */.  u16
a2940 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
a2950 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61   /* Some combina
a2960 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c  tion of MEM_Null
a2970 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44  , MEM_Str, MEM_D
a2980 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38  yn, etc. */.  u8
a2990 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20    type;         
a29a0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49    /* One of SQLI
a29b0 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f  TE_NULL, SQLITE_
a29c0 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54  TEXT, SQLITE_INT
a29d0 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75  EGER, etc */.  u
a29e0 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20  8  enc;         
a29f0 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46     /* SQLITE_UTF
a2a00 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  8, SQLITE_UTF16B
a2a10 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  E, SQLITE_UTF16L
a2a20 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  E */.  void (*xD
a2a30 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a  el)(void *);  /*
a2a40 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61   If not null, ca
a2a50 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
a2a60 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a   to delete Mem.z
a2a70 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c   */.  char *zMal
a2a80 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e  loc;      /* Dyn
a2a90 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f  amic buffer allo
a2aa0 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  cated by sqlite3
a2ab0 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a  _malloc() */.};.
a2ac0 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20  ./* One or more 
a2ad0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
a2ae0 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 74   flags are set t
a2af0 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 76  o indicate the v
a2b00 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73  alidOK.** repres
a2b10 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
a2b20 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
a2b30 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e   the Mem struct.
a2b40 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45  .**.** If the ME
a2b50 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73  M_Null flag is s
a2b60 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  et, then the val
a2b70 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c  ue is an SQL NUL
a2b80 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f  L value..** No o
a2b90 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62  ther flags may b
a2ba0 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61  e set in this ca
a2bb0 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
a2bc0 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73   MEM_Str flag is
a2bd0 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20   set then Mem.z 
a2be0 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 69  points at a stri
a2bf0 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
a2c00 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68  n..** Usually th
a2c10 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e  is is encoded in
a2c20 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64   the same unicod
a2c30 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68  e encoding as th
a2c40 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61  e main.** databa
a2c50 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f  se (see below fo
a2c60 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49  r exceptions). I
a2c70 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66  f the MEM_Term f
a2c80 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73  lag is also.** s
a2c90 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72  et, then the str
a2ca0 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69  ing is nul termi
a2cb0 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49  nated. The MEM_I
a2cc0 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20  nt and MEM_Real 
a2cd0 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f  .** flags may co
a2ce0 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 4d  exist with the M
a2cf0 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a  EM_Str flag..**.
a2d00 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74  ** Multiple of t
a2d10 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20  hese values can 
a2d20 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c  appear in Mem.fl
a2d30 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f  ags.  But only o
a2d40 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20  ne.** at a time 
a2d50 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65  can appear in Me
a2d60 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69  m.type..*/.#defi
a2d70 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20  ne MEM_Null     
a2d80 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c   0x0001   /* Val
a2d90 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64  ue is NULL */.#d
a2da0 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20  efine MEM_Str   
a2db0 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20      0x0002   /* 
a2dc0 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  Value is a strin
a2dd0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  g */.#define MEM
a2de0 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30  _Int       0x000
a2df0 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  4   /* Value is 
a2e00 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64  an integer */.#d
a2e10 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20  efine MEM_Real  
a2e20 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20      0x0008   /* 
a2e30 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20  Value is a real 
a2e40 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e  number */.#defin
a2e50 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20  e MEM_Blob      
a2e60 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75  0x0010   /* Valu
a2e70 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23  e is a BLOB */.#
a2e80 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65  define MEM_RowSe
a2e90 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a  t    0x0020   /*
a2ea0 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53   Value is a RowS
a2eb0 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65  et object */.#de
a2ec0 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61 73  fine MEM_TypeMas
a2ed0 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a 20 4d  k  0x00ff   /* M
a2ee0 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74 73  ask of type bits
a2ef0 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 65 72   */../* Whenever
a2f00 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 61 20   Mem contains a 
a2f10 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f 72 20  valid string or 
a2f20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 61 74  blob representat
a2f30 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74  ion, one of.** t
a2f40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61  he following fla
a2f50 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 20 74  gs must be set t
a2f60 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
a2f70 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e  memory managemen
a2f80 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f 72 20  t.** policy for 
a2f90 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 4d 5f  Mem.z.  The MEM_
a2fa0 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c 73 20  Term flag tells 
a2fb0 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  us whether or no
a2fc0 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20  t the.** string 
a2fd0 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 30 30  is \000 or \u000
a2fe0 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a  0 terminated.*/.
a2ff0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 72 6d  #define MEM_Term
a3000 20 20 20 20 20 20 30 78 30 32 30 30 20 20 20 2f        0x0200   /
a3010 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 73 20  * String rep is 
a3020 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 2a  nul terminated *
a3030 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 44 79  /.#define MEM_Dy
a3040 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 20 20  n       0x0400  
a3050 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c   /* Need to call
a3060 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 6f 6e   sqliteFree() on
a3070 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e   Mem.z */.#defin
a3080 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 20 20  e MEM_Static    
a3090 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d 2e  0x0800   /* Mem.
a30a0 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74  z points to a st
a30b0 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23  atic string */.#
a30c0 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 65 6d  define MEM_Ephem
a30d0 20 20 20 20 20 30 78 31 30 30 30 20 20 20 2f 2a       0x1000   /*
a30e0 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f   Mem.z points to
a30f0 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74   an ephemeral st
a3100 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ring */.#define 
a3110 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 30 78  MEM_Agg       0x
a3120 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20  2000   /* Mem.z 
a3130 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 67 67  points to an agg
a3140 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78   function contex
a3150 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  t */.#define MEM
a3160 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 30 30  _Zero      0x400
a3170 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e  0   /* Mem.i con
a3180 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 20 30  tains count of 0
a3190 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 62 6c  s appended to bl
a31a0 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  ob */..#ifdef SQ
a31b0 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
a31c0 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d 5f  OB.  #undef MEM_
a31d0 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20 4d  Zero.  #define M
a31e0 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a 23  EM_Zero 0x0000.#
a31f0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c  endif.../*.** Cl
a3200 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e 67  ear any existing
a3210 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f 6d   type flags from
a3220 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c 61   a Mem and repla
a3230 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a 2a  ce them with f.*
a3240 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65 74  /.#define MemSet
a3250 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20 5c  TypeFlag(p, f) \
a3260 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73 20  .   ((p)->flags 
a3270 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e 28  = ((p)->flags&~(
a3280 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45 4d  MEM_TypeMask|MEM
a3290 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20  _Zero))|f).../* 
a32a0 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a 75  A VdbeFunc is ju
a32b0 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64 65  st a FuncDef (de
a32c0 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49  fined in sqliteI
a32d0 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74 61  nt.h) that conta
a32e0 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61  ins.** additiona
a32f0 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  l information ab
a3300 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 69 6e  out auxiliary in
a3310 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64 20  formation bound 
a3320 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20  to arguments.** 
a3330 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  of the function.
a3340 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74    This is used t
a3350 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
a3360 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
a3370 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71 6c  ata().** and sql
a3380 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
a3390 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 22 61  () APIs.  The "a
a33a0 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65 20  uxdata" is some 
a33b0 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a 2a  auxiliary data.*
a33c0 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 61 73  * that can be as
a33d0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
a33e0 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 6e  constant argumen
a33f0 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e  t to a function.
a3400 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73    This.** allows
a3410 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68 20   functions such 
a3420 61 73 20 22 72 65 67 65 78 70 22 20 74 6f 20 63  as "regexp" to c
a3430 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f 6e  ompile their con
a3440 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a 2a  stant regular.**
a3450 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67 75   expression argu
a3460 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72 65  ment once and re
a3470 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c 65  used the compile
a3480 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74 69  d code for multi
a3490 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f  ple.** invocatio
a34a0 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64  ns..*/.struct Vd
a34b0 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63 44  beFunc {.  FuncD
a34c0 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20  ef *pFunc;      
a34d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
a34e0 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68  definition of th
a34f0 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
a3500 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20 20  int nAux;       
a3510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a3520 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
a3530 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
a3540 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73 74   apAux[] */.  st
a3550 72 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a 20  ruct AuxData {. 
a3560 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20     void *pAux;  
a3570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3580 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f 72   /* Aux data for
a3590 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d 65   the i-th argume
a35a0 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28  nt */.    void (
a35b0 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20 2a  *xDelete)(void *
a35c0 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72  );      /* Destr
a35d0 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61 75  uctor for the au
a35e0 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61 70  x data */.  } ap
a35f0 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20 20  Aux[1];         
a3600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
a3610 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20 66   slot for each f
a3620 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
a3630 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
a3640 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 67 75  e "context" argu
a3650 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 74 61  ment for a insta
a3660 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f 6e 2e  llable function.
a3670 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61    A pointer to a
a3680 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  n.** instance of
a3690 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
a36a0 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  is the first arg
a36b0 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 6f 75  ument to the rou
a36c0 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 69 6d  tines used.** im
a36d0 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20  plement the SQL 
a36e0 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  functions..**.**
a36f0 20 54 68 65 72 65 20 69 73 20 61 20 74 79 70 65   There is a type
a3700 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 74 72  def for this str
a3710 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 74 65  ucture in sqlite
a3720 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f 75 74  .h.  So all rout
a3730 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68  ines,.** even th
a3740 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61  e public interfa
a3750 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 63 61  ce to SQLite, ca
a3760 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 72 20  n use a pointer 
a3770 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
a3780 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 20 66  e..** But this f
a3790 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  ile is the only 
a37a0 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 65 20  place where the 
a37b0 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c 73  internal details
a37c0 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72 75   of this.** stru
a37d0 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 6e 2e  cture are known.
a37e0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  .**.** This stru
a37f0 63 74 75 72 65 20 69 73 20 64 65 66 69 6e 65 64  cture is defined
a3800 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 65 49   inside of vdbeI
a3810 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 74 20  nt.h because it 
a3820 75 73 65 73 20 73 75 62 73 74 72 75 63 74 75 72  uses substructur
a3830 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69 63  es.** (Mem) whic
a3840 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 69 6e  h are only defin
a3850 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 74 72  ed there..*/.str
a3860 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  uct sqlite3_cont
a3870 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 66 20  ext {.  FuncDef 
a3880 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f 2a  *pFunc;       /*
a3890 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e 63   Pointer to func
a38a0 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tion information
a38b0 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 53 54  .  MUST BE FIRST
a38c0 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 20 2a   */.  VdbeFunc *
a38d0 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a 20 41  pVdbeFunc;  /* A
a38e0 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 69 66  uxilary data, if
a38f0 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 20 4d   created. */.  M
a3900 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 20 20  em s;           
a3910 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75       /* The retu
a3920 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72  rn value is stor
a3930 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65 6d  ed here */.  Mem
a3940 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20   *pMem;         
a3950 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c     /* Memory cel
a3960 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  l used to store 
a3970 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78  aggregate contex
a3980 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72  t */.  int isErr
a3990 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  or;          /* 
a39a0 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72  Error code retur
a39b0 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e 63 74  ned by the funct
a39c0 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65  ion. */.  CollSe
a39d0 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20  q *pColl;       
a39e0 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
a39f0 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  uence */.};../*.
a3a00 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 74 75  ** A Set structu
a3a10 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 71  re is used for q
a3a20 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 6f 20  uick testing to 
a3a30 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 0a 2a  see if a value.*
a3a40 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 73  * is part of a s
a3a50 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 73 20  mall set.  Sets 
a3a60 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  are used to impl
a3a70 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b 65 0a  ement code like.
a3a80 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 20 20  ** this:.**     
a3a90 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 28 27         x.y IN ('
a3aa0 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d 27 29  hi','hoo','hum')
a3ab0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
a3ac0 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 72 75  ct Set Set;.stru
a3ad0 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 68 20  ct Set {.  Hash 
a3ae0 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20  hash;           
a3af0 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 6a 75    /* A set is ju
a3b00 73 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 20  st a hash table 
a3b10 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70  */.  HashElem *p
a3b20 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  rev;        /* P
a3b30 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 73 73  reviously access
a3b40 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e 20 2a  ed hash elemen *
a3b50 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f  /.};../*.** A Co
a3b60 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65  ntext stores the
a3b70 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77   last insert row
a3b80 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61  id, the last sta
a3b90 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63 6f  tement change co
a3ba0 75 6e 74 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20  unt,.** and the 
a3bb0 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e  current statemen
a3bc0 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 28  t change count (
a3bd0 69 2e 65 2e 20 63 68 61 6e 67 65 73 20 73 69 6e  i.e. changes sin
a3be0 63 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e  ce last statemen
a3bf0 74 29 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72 65  t)..** The curre
a3c00 6e 74 20 6b 65 79 6c 69 73 74 20 69 73 20 61 6c  nt keylist is al
a3c10 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  so stored in the
a3c20 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 20 45 6c 65   context..** Ele
a3c30 6d 65 6e 74 73 20 6f 66 20 43 6f 6e 74 65 78 74  ments of Context
a3c40 20 73 74 72 75 63 74 75 72 65 20 74 79 70 65 20   structure type 
a3c50 6d 61 6b 65 20 75 70 20 74 68 65 20 43 6f 6e 74  make up the Cont
a3c60 65 78 74 53 74 61 63 6b 2c 20 77 68 69 63 68 20  extStack, which 
a3c70 69 73 0a 2a 2a 20 75 70 64 61 74 65 64 20 62 79  is.** updated by
a3c80 20 74 68 65 20 43 6f 6e 74 65 78 74 50 75 73 68   the ContextPush
a3c90 20 61 6e 64 20 43 6f 6e 74 65 78 74 50 6f 70 20   and ContextPop 
a3ca0 6f 70 63 6f 64 65 73 20 28 75 73 65 64 20 62 79  opcodes (used by
a3cb0 20 74 72 69 67 67 65 72 73 29 2e 0a 2a 2a 20 54   triggers)..** T
a3cc0 68 65 20 63 6f 6e 74 65 78 74 20 69 73 20 70 75  he context is pu
a3cd0 73 68 65 64 20 62 65 66 6f 72 65 20 65 78 65 63  shed before exec
a3ce0 75 74 69 6e 67 20 61 20 74 72 69 67 67 65 72 20  uting a trigger 
a3cf0 61 20 70 6f 70 70 65 64 20 77 68 65 6e 20 74 68  a popped when th
a3d00 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 66 69 6e  e.** trigger fin
a3d10 69 73 68 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  ishes..*/.typede
a3d20 66 20 73 74 72 75 63 74 20 43 6f 6e 74 65 78 74  f struct Context
a3d30 20 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74   Context;.struct
a3d40 20 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 36 34   Context {.  i64
a3d50 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 2f   lastRowid;    /
a3d60 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  * Last insert ro
a3d70 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73  wid (sqlite3.las
a3d80 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74  tRowid) */.  int
a3d90 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f   nChange;      /
a3da0 2a 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e  * Statement chan
a3db0 67 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67  ges (Vdbe.nChang
a3dc0 65 73 29 20 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f  es)     */.};../
a3dd0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
a3de0 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
a3df0 6d 61 63 68 69 6e 65 2e 20 20 54 68 69 73 20 73  machine.  This s
a3e00 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
a3e10 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a  s the complete.*
a3e20 2a 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 76  * state of the v
a3e30 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a  irtual machine..
a3e40 2a 2a 0a 2a 2a 20 54 68 65 20 22 73 71 6c 69 74  **.** The "sqlit
a3e50 65 33 5f 73 74 6d 74 22 20 73 74 72 75 63 74 75  e3_stmt" structu
a3e60 72 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20  re pointer that 
a3e70 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  is returned by s
a3e80 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29  qlite3_compile()
a3e90 0a 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20  .** is really a 
a3ea0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e  pointer to an in
a3eb0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
a3ec0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
a3ed0 54 68 65 20 56 64 62 65 2e 69 6e 56 74 61 62 4d  The Vdbe.inVtabM
a3ee0 65 74 68 6f 64 20 76 61 72 69 61 62 6c 65 20 69  ethod variable i
a3ef0 73 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72  s set to non-zer
a3f00 6f 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69  o for the durati
a3f10 6f 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72  on of.** any vir
a3f20 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f  tual table metho
a3f30 64 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61  d invocations ma
a3f40 64 65 20 62 79 20 74 68 65 20 76 64 62 65 20 70  de by the vdbe p
a3f50 72 6f 67 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a  rogram. It is.**
a3f60 20 73 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44   set to 2 for xD
a3f70 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61  estroy method ca
a3f80 6c 6c 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c  lls and 1 for al
a3f90 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e  l other methods.
a3fa0 20 54 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c   This.** variabl
a3fb0 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 77  e is used for tw
a3fc0 6f 20 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61  o purposes: to a
a3fd0 6c 6c 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65  llow xDestroy me
a3fe0 74 68 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65  thods to execute
a3ff0 0a 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c 45 22  .** "DROP TABLE"
a4000 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20   statements and 
a4010 74 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d 65 20  to prevent some 
a4020 6e 61 73 74 79 20 73 69 64 65 20 65 66 66 65 63  nasty side effec
a4030 74 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20  ts of.** malloc 
a4040 66 61 69 6c 75 72 65 20 77 68 65 6e 20 53 51 4c  failure when SQL
a4050 69 74 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 72  ite is invoked r
a4060 65 63 75 72 73 69 76 65 6c 79 20 62 79 20 61 20  ecursively by a 
a4070 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a  virtual table .*
a4080 2a 20 6d 65 74 68 6f 64 20 66 75 6e 63 74 69 6f  * method functio
a4090 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62  n..*/.struct Vdb
a40a0 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e {.  sqlite3 *d
a40b0 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b;        /* The
a40c0 20 77 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20   whole database 
a40d0 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65 76  */.  Vdbe *pPrev
a40e0 2c 2a 70 4e 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b  ,*pNext; /* Link
a40f0 65 64 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73  ed list of VDBEs
a4100 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56   with the same V
a4110 64 62 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20  dbe.db */.  int 
a4120 6e 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  nOp;            
a4130 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73  /* Number of ins
a4140 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 68 65  tructions in the
a4150 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e   program */.  in
a4160 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20  t nOpAlloc;     
a4170 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
a4180 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  lots allocated f
a4190 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70  or aOp[] */.  Op
a41a0 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20   *aOp;          
a41b0 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f    /* Space to ho
a41c0 6c 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  ld the virtual m
a41d0 61 63 68 69 6e 65 27 73 20 70 72 6f 67 72 61 6d  achine's program
a41e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c   */.  int nLabel
a41f0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
a4200 62 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73  ber of labels us
a4210 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62  ed */.  int nLab
a4220 65 6c 41 6c 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e  elAlloc;    /* N
a4230 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61  umber of slots a
a4240 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 4c 61 62  llocated in aLab
a4250 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  el[] */.  int *a
a4260 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 2f 2a  Label;        /*
a4270 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
a4280 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d  he labels */.  M
a4290 65 6d 20 2a 2a 61 70 41 72 67 3b 20 20 20 20 20  em **apArg;     
a42a0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20     /* Arguments 
a42b0 74 6f 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65  to currently exe
a42c0 63 75 74 69 6e 67 20 75 73 65 72 20 66 75 6e 63  cuting user func
a42d0 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61  tion */.  Mem *a
a42e0 43 6f 6c 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a  ColName;      /*
a42f0 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f   Column names to
a4300 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74   return */.  int
a4310 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20   nCursor;       
a4320 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c   /* Number of sl
a4330 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a  ots in apCsr[] *
a4340 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
a4350 2a 61 70 43 73 72 3b 20 2f 2a 20 4f 6e 65 20 65  *apCsr; /* One e
a4360 6c 65 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 61  lement of this a
a4370 72 72 61 79 20 66 6f 72 20 65 61 63 68 20 6f 70  rray for each op
a4380 65 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69  en cursor */.  i
a4390 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20  nt nVar;        
a43a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
a43b0 65 6e 74 72 69 65 73 20 69 6e 20 61 56 61 72 5b  entries in aVar[
a43c0 5d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72  ] */.  Mem *aVar
a43d0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61  ;          /* Va
a43e0 6c 75 65 73 20 66 6f 72 20 74 68 65 20 4f 50 5f  lues for the OP_
a43f0 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e  Variable opcode.
a4400 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 56   */.  char **azV
a4410 61 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ar;       /* Nam
a4420 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a  e of variables *
a4430 2f 0a 20 20 69 6e 74 20 6f 6b 56 61 72 3b 20 20  /.  int okVar;  
a4440 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
a4450 69 66 20 61 7a 56 61 72 5b 5d 20 68 61 73 20 62  if azVar[] has b
a4460 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  een initialized 
a4470 2a 2f 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20  */.  u32 magic; 
a4480 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a4490 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72  Magic number for
a44a0 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   sanity checking
a44b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20   */.  int nMem; 
a44c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a44d0 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72   Number of memor
a44e0 79 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72  y locations curr
a44f0 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ently allocated 
a4500 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20  */.  Mem *aMem; 
a4510 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a4520 54 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  The memory locat
a4530 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 63 61  ions */.  int ca
a4540 63 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20  cheCtr;         
a4550 20 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20    /* VdbeCursor 
a4560 72 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61  row cache genera
a4570 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  tion counter */.
a4580 20 20 69 6e 74 20 63 6f 6e 74 65 78 74 53 74 61    int contextSta
a4590 63 6b 54 6f 70 3b 20 20 20 20 2f 2a 20 49 6e 64  ckTop;    /* Ind
a45a0 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e  ex of top elemen
a45b0 74 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74  t in the context
a45c0 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   stack */.  int 
a45d0 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74  contextStackDept
a45e0 68 3b 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20  h;  /* The size 
a45f0 6f 66 20 74 68 65 20 22 63 6f 6e 74 65 78 74 22  of the "context"
a4600 20 73 74 61 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74   stack */.  Cont
a4610 65 78 74 20 2a 63 6f 6e 74 65 78 74 53 74 61 63  ext *contextStac
a4620 6b 3b 20 20 2f 2a 20 53 74 61 63 6b 20 75 73 65  k;  /* Stack use
a4630 64 20 62 79 20 6f 70 63 6f 64 65 73 20 43 6f 6e  d by opcodes Con
a4640 74 65 78 74 50 75 73 68 20 26 20 43 6f 6e 74 65  textPush & Conte
a4650 78 74 50 6f 70 2a 2f 0a 20 20 69 6e 74 20 70 63  xtPop*/.  int pc
a4660 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a4670 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d    /* The program
a4680 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
a4690 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
a46a0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
a46b0 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e  o return */.  in
a46c0 74 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20  t errorAction;  
a46d0 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72        /* Recover
a46e0 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69  y action to do i
a46f0 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72  n case of an err
a4700 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73  or */.  int nRes
a4710 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
a4720 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
a4730 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 6f 77 20  umns in one row 
a4740 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
a4750 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a  t */.  char **az
a4760 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f  ResColumn;     /
a4770 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 6f 6e 65  * Values for one
a4780 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a   row of result *
a4790 2f 20 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d  / .  char *zErrM
a47a0 73 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  sg;          /* 
a47b0 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72  Error message wr
a47c0 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20  itten here */.  
a47d0 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 65 74 3b  Mem *pResultSet;
a47e0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
a47f0 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f  er to an array o
a4800 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 75  f results */.  u
a4810 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20  8 explain;      
a4820 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a4830 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e  f EXPLAIN presen
a4840 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  t on SQL command
a4850 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43   */.  u8 changeC
a4860 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  ntOn;         /*
a4870 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 20   True to update 
a4880 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
a4890 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 72  er */.  u8 expir
a48a0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
a48b0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 56  /* True if the V
a48c0 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  M needs to be re
a48d0 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 38  compiled */.  u8
a48e0 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72   minWriteFileFor
a48f0 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d  mat;  /* Minimum
a4900 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72   file format for
a4910 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 61   writable databa
a4920 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38  se files */.  u8
a4930 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 20   inVtabMethod;  
a4940 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d        /* See com
a4950 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20  ments above */. 
a4960 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 72   u8 usesStmtJour
a4970 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  nal;     /* True
a4980 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 65   if uses a state
a4990 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  ment journal */.
a49a0 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20    u8 readOnly;  
a49b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
a49c0 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20  e for read-only 
a49d0 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  statements */.  
a49e0 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b 20  u8 isPrepareV2; 
a49f0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
a4a00 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 68  if prepared with
a4a10 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a 2f   prepare_v2() */
a4a20 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20  .  int nChange; 
a4a30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
a4a40 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67  mber of db chang
a4a50 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61  es made since la
a4a60 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 36  st reset */.  i6
a4a70 34 20 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20  4 startTime;    
a4a80 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68        /* Time wh
a4a90 65 6e 20 71 75 65 72 79 20 73 74 61 72 74 65 64  en query started
a4aa0 20 2d 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66   - used for prof
a4ab0 69 6c 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62  iling */.  int b
a4ac0 74 72 65 65 4d 61 73 6b 3b 20 20 20 20 20 20 20  treeMask;       
a4ad0 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
a4ae0 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69   db->aDb[] entri
a4af0 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f  es referenced */
a4b00 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  .  BtreeMutexArr
a4b10 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e  ay aMutex; /* An
a4b20 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20   array of Btree 
a4b30 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65  used here and ne
a4b40 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20  eding locks */. 
a4b50 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d   int aCounter[2]
a4b60 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e  ;        /* Coun
a4b70 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c  ters used by sql
a4b80 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
a4b90 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53  () */.  char *zS
a4ba0 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ql;           /*
a4bb0 20 54 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c   Text of the SQL
a4bc0 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
a4bd0 67 65 6e 65 72 61 74 65 64 20 74 68 69 73 20 2a  generated this *
a4be0 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 65 65 3b  /.  void *pFree;
a4bf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
a4c00 72 65 65 20 74 68 69 73 20 77 68 65 6e 20 64 65  ree this when de
a4c10 6c 65 74 69 6e 67 20 74 68 65 20 76 64 62 65 20  leting the vdbe 
a4c20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
a4c30 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 74  _DEBUG.  FILE *t
a4c40 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 2f  race;          /
a4c50 2a 20 57 72 69 74 65 20 61 6e 20 65 78 65 63 75  * Write an execu
a4c60 74 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65 2c  tion trace here,
a4c70 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a   if not NULL */.
a4c80 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 53 74  #endif.  int iSt
a4c90 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20  atement;        
a4ca0 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75   /* Statement nu
a4cb0 6d 62 65 72 20 28 6f 72 20 30 20 69 66 20 68 61  mber (or 0 if ha
a4cc0 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 6d  s not opened stm
a4cd0 74 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  t) */.#ifdef SQL
a4ce0 49 54 45 5f 53 53 45 0a 20 20 69 6e 74 20 66 65  ITE_SSE.  int fe
a4cf0 74 63 68 49 64 3b 20 20 20 20 20 20 20 20 20 20  tchId;          
a4d00 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d  /* Statement num
a4d10 62 65 72 20 75 73 65 64 20 62 79 20 73 71 6c 69  ber used by sqli
a4d20 74 65 33 5f 66 65 74 63 68 5f 73 74 61 74 65 6d  te3_fetch_statem
a4d30 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 72 75  ent */.  int lru
a4d40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
a4d50 2a 20 43 6f 75 6e 74 65 72 20 75 73 65 64 20 66  * Counter used f
a4d60 6f 72 20 4c 52 55 20 63 61 63 68 65 20 72 65 70  or LRU cache rep
a4d70 6c 61 63 65 6d 65 6e 74 20 2a 2f 0a 23 65 6e 64  lacement */.#end
a4d80 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
a4d90 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
a4da0 41 4e 41 47 45 4d 45 4e 54 0a 20 20 56 64 62 65  ANAGEMENT.  Vdbe
a4db0 20 2a 70 4c 72 75 50 72 65 76 3b 0a 20 20 56 64   *pLruPrev;.  Vd
a4dc0 62 65 20 2a 70 4c 72 75 4e 65 78 74 3b 0a 23 65  be *pLruNext;.#e
a4dd0 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  ndif.};../*.** T
a4de0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
a4df0 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   allowed values 
a4e00 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a 2a  for Vdbe.magic.*
a4e10 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d  /.#define VDBE_M
a4e20 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 78  AGIC_INIT     0x
a4e30 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 42  26bceaa5    /* B
a4e40 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 70  uilding a VDBE p
a4e50 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 6e  rogram */.#defin
a4e60 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  e VDBE_MAGIC_RUN
a4e70 20 20 20 20 20 20 30 78 62 64 66 32 30 64 61 33        0xbdf20da3
a4e80 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 72      /* VDBE is r
a4e90 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 20  eady to execute 
a4ea0 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f  */.#define VDBE_
a4eb0 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 30  MAGIC_HALT     0
a4ec0 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 20  x519c2973    /* 
a4ed0 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 74  VDBE has complet
a4ee0 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a  ed execution */.
a4ef0 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47  #define VDBE_MAG
a4f00 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 36  IC_DEAD     0xb6
a4f10 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 65  06c3c8    /* The
a4f20 20 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 64   VDBE has been d
a4f30 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f  eallocated */../
a4f40 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 72  *.** Function pr
a4f50 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49  ototypes.*/.SQLI
a4f60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a4f70 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43  sqlite3VdbeFreeC
a4f80 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56 64  ursor(Vdbe *, Vd
a4f90 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69 64  beCursor*);.void
a4fa0 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53 74   sqliteVdbePopSt
a4fb0 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a  ack(Vdbe*,int);.
a4fc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a4fd0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  nt sqlite3VdbeCu
a4fe0 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43  rsorMoveto(VdbeC
a4ff0 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65 66  ursor*);.#if def
a5000 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
a5010 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44  G) || defined(VD
a5020 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c 49  BE_PROFILE).SQLI
a5030 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a5040 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74  sqlite3VdbePrint
a5050 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f  Op(FILE*, int, O
a5060 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49  p*);.#endif.SQLI
a5070 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a5080 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
a5090 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53 51  TypeLen(u32);.SQ
a50a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
a50b0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
a50c0 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e 74  alType(Mem*, int
a50d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
a50e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
a50f0 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69 67  eSerialPut(unsig
a5100 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20  ned char*, int, 
a5110 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  Mem*, int);.SQLI
a5120 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a5130 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
a5140 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  Get(const unsign
a5150 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20 4d  ed char*, u32, M
a5160 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
a5170 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a5180 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61  3VdbeDeleteAuxDa
a5190 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69 6e  ta(VdbeFunc*, in
a51a0 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65 32  t);..int sqlite2
a51b0 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65 28  BtreeKeyCompare(
a51c0 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73  BtCursor *, cons
a51d0 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 69  t void *, int, i
a51e0 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49  nt, int *);.SQLI
a51f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a5200 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79  qlite3VdbeIdxKey
a5210 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72 73  Compare(VdbeCurs
a5220 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f  or*,UnpackedReco
a5230 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  rd*,int*);.SQLIT
a5240 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a5250 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69  lite3VdbeIdxRowi
a5260 64 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 36  d(BtCursor *, i6
a5270 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  4 *);.SQLITE_PRI
a5280 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
a5290 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74  MemCompare(const
a52a0 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d   Mem*, const Mem
a52b0 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71  *, const CollSeq
a52c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
a52d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
a52e0 62 65 45 78 65 63 28 56 64 62 65 2a 29 3b 0a 53  beExec(Vdbe*);.S
a52f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a5300 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73  t sqlite3VdbeLis
a5310 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  t(Vdbe*);.SQLITE
a5320 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a5330 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62  ite3VdbeHalt(Vdb
a5340 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
a5350 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
a5360 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
a5370 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53  g(Mem *, int);.S
a5380 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a5390 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
a53a0 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b 0a 53 51  TooBig(Mem*);.SQ
a53b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
a53c0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43   sqlite3VdbeMemC
a53d0 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20  opy(Mem*, const 
a53e0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
a53f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a5400 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
a5410 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74  Copy(Mem*, const
a5420 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c   Mem*, int);.SQL
a5430 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a5440 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
a5450 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b  ove(Mem*, Mem*);
a5460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a5470 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
a5480 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d  emNulTerminate(M
a5490 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
a54a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
a54b0 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 4d 65  VdbeMemSetStr(Me
a54c0 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  m*, const char*,
a54d0 20 69 6e 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a   int, u8, void(*
a54e0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
a54f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a5500 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
a5510 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29  Int64(Mem*, i64)
a5520 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
a5530 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
a5540 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65  eMemSetDouble(Me
a5550 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c  m*, double);.SQL
a5560 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a5570 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
a5580 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51  etNull(Mem*);.SQ
a5590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a55a0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
a55b0 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a  SetZeroBlob(Mem*
a55c0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
a55d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a55e0 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53  e3VdbeMemSetRowS
a55f0 65 74 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  et(Mem*);.SQLITE
a5600 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a5610 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57  ite3VdbeMemMakeW
a5620 72 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a  riteable(Mem*);.
a5630 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a5640 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
a5650 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c  mStringify(Mem*,
a5660 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
a5670 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65  IVATE i64 sqlite
a5680 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65  3VdbeIntValue(Me
a5690 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
a56a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
a56b0 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
a56c0 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  (Mem*);.SQLITE_P
a56d0 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71  RIVATE double sq
a56e0 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
a56f0 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ue(Mem*);.SQLITE
a5700 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a5710 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
a5720 41 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a  Affinity(Mem*);.
a5730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a5740 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
a5750 6d 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a  mRealify(Mem*);.
a5760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a5770 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
a5780 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b  mNumerify(Mem*);
a5790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a57a0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
a57b0 65 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75  emFromBtree(BtCu
a57c0 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e  rsor*,int,int,in
a57d0 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  t,Mem*);.SQLITE_
a57e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a57f0 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
a5800 73 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49  se(Mem *p);.SQLI
a5810 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a5820 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
a5830 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65  leaseExternal(Me
a5840 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52  m *p);.SQLITE_PR
a5850 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a5860 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65  3VdbeMemFinalize
a5870 28 4d 65 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29  (Mem*, FuncDef*)
a5880 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
a5890 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
a58a0 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69  ite3OpcodeName(i
a58b0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
a58c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
a58d0 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70  dbeOpcodeHasProp
a58e0 65 72 74 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a  erty(int, int);.
a58f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a5900 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
a5910 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c  mGrow(Mem *pMem,
a5920 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73   int n, int pres
a5930 65 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  erve);.SQLITE_PR
a5940 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a5950 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d  3VdbeCloseStatem
a5960 65 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29  ent(Vdbe *, int)
a5970 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
a5980 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
a5990 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f  NAGEMENT.SQLITE_
a59a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a59b0 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75  te3VdbeReleaseBu
a59c0 66 66 65 72 73 28 56 64 62 65 20 2a 70 29 3b 0a  ffers(Vdbe *p);.
a59d0 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
a59e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
a59f0 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f  ED_CACHE.SQLITE_
a5a00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a5a10 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72  ite3VdbeMutexArr
a5a20 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29  ayEnter(Vdbe *p)
a5a30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
a5a40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65   sqlite3VdbeMute
a5a50 78 41 72 72 61 79 45 6e 74 65 72 28 70 29 0a 23  xArrayEnter(p).#
a5a60 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
a5a70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a5a80 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74  3VdbeMemTranslat
a5a90 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66  e(Mem*, u8);.#if
a5aa0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
a5ab0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a5ac0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
a5ad0 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a  bePrintSql(Vdbe*
a5ae0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
a5af0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
a5b00 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69  VdbeMemPrettyPri
a5b10 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68  nt(Mem *pMem, ch
a5b20 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69  ar *zBuf);.#endi
a5b30 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
a5b40 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
a5b50 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d  MemHandleBom(Mem
a5b60 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65   *pMem);..#ifnde
a5b70 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
a5b80 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52  CRBLOB.SQLITE_PR
a5b90 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
a5ba0 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
a5bb0 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c  Blob(Mem *);.#el
a5bc0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  se.  #define sql
a5bd0 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e  ite3VdbeMemExpan
a5be0 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 5f  dBlob(x) SQLITE_
a5bf0 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  OK.#endif..#endi
a5c00 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 56  f /* !defined(_V
a5c10 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f  DBEINT_H_) */../
a5c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
a5c30 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20  nd of vdbeInt.h 
a5c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
a5c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
a5c80 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
a5c90 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 75  we left off in u
a5ca0 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tf.c ***********
a5cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
a5cc0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41  #ifndef SQLITE_A
a5cd0 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a  MALGAMATION./*.*
a5ce0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
a5cf0 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65 20 69  constant value i
a5d00 73 20 75 73 65 64 20 62 79 20 74 68 65 20 53 51  s used by the SQ
a5d10 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 61  LITE_BIGENDIAN a
a5d20 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 54  nd.** SQLITE_LIT
a5d30 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f 73  TLEENDIAN macros
a5d40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a5d50 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71  ATE const int sq
a5d60 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65  lite3one = 1;.#e
a5d70 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41  ndif /* SQLITE_A
a5d80 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a  MALGAMATION */..
a5d90 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b 75  /*.** This looku
a5da0 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20  p table is used 
a5db0 74 6f 20 68 65 6c 70 20 64 65 63 6f 64 65 20 74  to help decode t
a5dc0 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66  he first byte of
a5dd0 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 79 74 65  .** a multi-byte
a5de0 20 55 54 46 38 20 63 68 61 72 61 63 74 65 72 2e   UTF8 character.
a5df0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
a5e00 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73   unsigned char s
a5e10 71 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31  qlite3Utf8Trans1
a5e20 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30  [] = {.  0x00, 0
a5e30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c  x01, 0x02, 0x03,
a5e40 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30   0x04, 0x05, 0x0
a5e50 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 2c  6, 0x07,.  0x08,
a5e60 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 30   0x09, 0x0a, 0x0
a5e70 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 30  b, 0x0c, 0x0d, 0
a5e80 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 31  x0e, 0x0f,.  0x1
a5e90 30 2c 20 30 78 31 31 2c 20 30 78 31 32 2c 20 30  0, 0x11, 0x12, 0
a5ea0 78 31 33 2c 20 30 78 31 34 2c 20 30 78 31 35 2c  x13, 0x14, 0x15,
a5eb0 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 20 20 30   0x16, 0x17,.  0
a5ec0 78 31 38 2c 20 30 78 31 39 2c 20 30 78 31 61 2c  x18, 0x19, 0x1a,
a5ed0 20 30 78 31 62 2c 20 30 78 31 63 2c 20 30 78 31   0x1b, 0x1c, 0x1
a5ee0 64 2c 20 30 78 31 65 2c 20 30 78 31 66 2c 0a 20  d, 0x1e, 0x1f,. 
a5ef0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
a5f00 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30  2, 0x03, 0x04, 0
a5f10 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c  x05, 0x06, 0x07,
a5f20 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30  .  0x08, 0x09, 0
a5f30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c  x0a, 0x0b, 0x0c,
a5f40 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30   0x0d, 0x0e, 0x0
a5f50 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c  f,.  0x00, 0x01,
a5f60 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30   0x02, 0x03, 0x0
a5f70 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30  4, 0x05, 0x06, 0
a5f80 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 30 78 30  x07,.  0x00, 0x0
a5f90 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30  1, 0x02, 0x03, 0
a5fa0 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c  x00, 0x01, 0x00,
a5fb0 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66   0x00,.};...#def
a5fc0 69 6e 65 20 57 52 49 54 45 5f 55 54 46 38 28 7a  ine WRITE_UTF8(z
a5fd0 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20  Out, c) {       
a5fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5ff0 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 30 78 30     \.  if( c<0x0
a6000 30 30 38 30 20 29 7b 20 20 20 20 20 20 20 20 20  0080 ){         
a6010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6020 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a6030 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
a6040 28 63 26 30 78 46 46 29 3b 20 20 20 20 20 20 20  (c&0xFF);       
a6050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6060 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20       \.  }      
a6070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a60a0 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78 30    else if( c<0x0
a60b0 30 38 30 30 20 29 7b 20 20 20 20 20 20 20 20 20  0800 ){         
a60c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a60d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
a60e0 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28 75  ut++ = 0xC0 + (u
a60f0 38 29 28 28 63 3e 3e 36 29 26 30 78 31 46 29 3b  8)((c>>6)&0x1F);
a6100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6110 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
a6120 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20  0x80 + (u8)(c & 
a6130 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20  0x3F);          
a6140 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20           \.  }  
a6150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6180 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63    \.  else if( c
a6190 3c 30 78 31 30 30 30 30 20 29 7b 20 20 20 20 20  <0x10000 ){     
a61a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a61b0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
a61c0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20   *zOut++ = 0xE0 
a61d0 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 26 30  + (u8)((c>>12)&0
a61e0 78 30 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x0F);           
a61f0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
a6200 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28  + = 0x80 + (u8)(
a6210 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20  (c>>6) & 0x3F); 
a6220 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a6230 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
a6240 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33  0 + (u8)(c & 0x3
a6250 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
a6260 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b        \.  }else{
a6270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
a62a0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30  .    *zOut++ = 0
a62b0 78 46 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31  xF0 + (u8)((c>>1
a62c0 38 29 20 26 20 30 78 30 37 29 3b 20 20 20 20 20  8) & 0x07);     
a62d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
a62e0 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28  Out++ = 0x80 + (
a62f0 75 38 29 28 28 63 3e 3e 31 32 29 20 26 20 30 78  u8)((c>>12) & 0x
a6300 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
a6310 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
a6320 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e   0x80 + (u8)((c>
a6330 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20  >6) & 0x3F);    
a6340 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
a6350 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b  *zOut++ = 0x80 +
a6360 20 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b   (u8)(c & 0x3F);
a6370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6380 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20     \.  }        
a6390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a63a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a63b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a              \.}.
a63c0 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55  .#define WRITE_U
a63d0 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20  TF16LE(zOut, c) 
a63e0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
a63f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6400 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d       \.  if( c<=
a6410 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20  0xFFFF ){       
a6420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6440 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
a6450 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
a6460 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20  c&0x00FF);      
a6470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6490 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
a64a0 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30   (u8)((c>>8)&0x0
a64b0 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  0FF);           
a64c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a64d0 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65         \.  }else
a64e0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
a64f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6510 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a6520 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
a6530 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33  )(((c>>10)&0x003
a6540 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30  F) + (((c-0x1000
a6550 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29  0)>>10)&0x00C0))
a6560 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b  ;  \.    *zOut++
a6570 20 3d 20 28 75 38 29 28 30 78 30 30 44 38 20 2b   = (u8)(0x00D8 +
a6580 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e   (((c-0x10000)>>
a6590 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20  18)&0x03));     
a65a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
a65b0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26  zOut++ = (u8)(c&
a65c0 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20  0x00FF);        
a65d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a65e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
a65f0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
a6600 75 38 29 28 30 78 30 30 44 43 20 2b 20 28 28 63  u8)(0x00DC + ((c
a6610 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20 20  >>8)&0x03));    
a6620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6630 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20       \.  }      
a6640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6670 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a             \.}..
a6680 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54  #define WRITE_UT
a6690 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 20 7b  F16BE(zOut, c) {
a66a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a66b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a66c0 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30      \.  if( c<=0
a66d0 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20  xFFFF ){        
a66e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a66f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6700 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
a6710 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28  *zOut++ = (u8)((
a6720 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20  c>>8)&0x00FF);  
a6730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6750 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
a6760 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20  (u8)(c&0x00FF); 
a6770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6790 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b        \.  }else{
a67a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a67b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a67c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a67d0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a67e0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
a67f0 28 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30  (0x00D8 + (((c-0
a6800 78 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30  x10000)>>18)&0x0
a6810 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3));            
a6820 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
a6830 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26  = (u8)(((c>>10)&
a6840 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30  0x003F) + (((c-0
a6850 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30  x10000)>>10)&0x0
a6860 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a  0C0));  \.    *z
a6870 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30  Out++ = (u8)(0x0
a6880 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78  0DC + ((c>>8)&0x
a6890 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20  03));           
a68a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a68b0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
a68c0 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20  8)(c&0x00FF);   
a68d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a68e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a68f0 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20      \.  }       
a6900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6930 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23            \.}..#
a6940 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31  define READ_UTF1
a6950 36 4c 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20  6LE(zIn, c){    
a6960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6980 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a       \.  c = (*z
a6990 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20  In++);          
a69a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a69b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a69c0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a69d0 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c   c += ((*zIn++)<
a69e0 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  <8);            
a69f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6a10 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d       \.  if( c>=
a6a20 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30  0xD800 && c<0xE0
a6a30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  00 ){           
a6a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6a50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a6a60 20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49     int c2 = (*zI
a6a70 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
a6a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6aa0 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d       \.    c2 +=
a6ab0 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20   ((*zIn++)<<8); 
a6ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a6af0 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46     c = (c2&0x03F
a6b00 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29  F) + ((c&0x003F)
a6b10 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30  <<10) + (((c&0x0
a6b20 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30  3C0)+0x0040)<<10
a6b30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20  );   \.  }      
a6b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6b70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
a6b80 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55  ..#define READ_U
a6b90 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20  TF16BE(zIn, c){ 
a6ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6bc0 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20          \.  c = 
a6bd0 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20  ((*zIn++)<<8);  
a6be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c10 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b  \.  c += (*zIn++
a6c20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
a6c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c50 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
a6c60 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30  c>=0xD800 && c<0
a6c70 78 45 30 30 30 20 29 7b 20 20 20 20 20 20 20 20  xE000 ){        
a6c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6ca0 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28  \.    int c2 = (
a6cb0 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20  (*zIn++)<<8);   
a6cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6ce0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32          \.    c2
a6cf0 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20   += (*zIn++);   
a6d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6d30 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78  \.    c = (c2&0x
a6d40 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30  03FF) + ((c&0x00
a6d50 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26  3F)<<10) + (((c&
a6d60 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c  0x03C0)+0x0040)<
a6d70 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20  <10);   \.  }   
a6d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6dc0 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  \.}../*.** Trans
a6dd0 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 55 54  late a single UT
a6de0 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 20 20  F-8 character.  
a6df0 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69 63 6f  Return the unico
a6e00 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  de value..**.** 
a6e10 44 75 72 69 6e 67 20 74 72 61 6e 73 6c 61 74 69  During translati
a6e20 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20  on, assume that 
a6e30 74 68 65 20 62 79 74 65 20 74 68 61 74 20 7a 54  the byte that zT
a6e40 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73  erm points.** is
a6e50 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57   a 0x00..**.** W
a6e60 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74  rite a pointer t
a6e70 6f 20 74 68 65 20 6e 65 78 74 20 75 6e 72 65 61  o the next unrea
a6e80 64 20 62 79 74 65 20 62 61 63 6b 20 69 6e 74 6f  d byte back into
a6e90 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20   *pzNext..**.** 
a6ea0 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64  Notes On Invalid
a6eb0 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a   UTF-8:.**.**  *
a6ec0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e    This routine n
a6ed0 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d  ever allows a 7-
a6ee0 62 69 74 20 63 68 61 72 61 63 74 65 72 20 28 30  bit character (0
a6ef0 78 30 30 20 74 68 72 6f 75 67 68 20 30 78 37 66  x00 through 0x7f
a6f00 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65  ) to.**     be e
a6f10 6e 63 6f 64 65 64 20 61 73 20 61 20 6d 75 6c 74  ncoded as a mult
a6f20 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72  i-byte character
a6f30 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74  .  Any multi-byt
a6f40 65 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74  e character that
a6f50 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d 70 74 73  .**     attempts
a6f60 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c   to encode a val
a6f70 75 65 20 62 65 74 77 65 65 6e 20 30 78 30 30 20  ue between 0x00 
a6f80 61 6e 64 20 30 78 37 66 20 69 73 20 72 65 6e 64  and 0x7f is rend
a6f90 65 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a  ered as 0xfffd..
a6fa0 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72  **.**  *  This r
a6fb0 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c  outine never all
a6fc0 6f 77 73 20 61 20 55 54 46 31 36 20 73 75 72 72  ows a UTF16 surr
a6fd0 6f 67 61 74 65 20 76 61 6c 75 65 20 74 6f 20 62  ogate value to b
a6fe0 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20  e encoded..**   
a6ff0 20 20 49 66 20 61 20 6d 75 6c 74 69 2d 62 79 74    If a multi-byt
a7000 65 20 63 68 61 72 61 63 74 65 72 20 61 74 74 65  e character atte
a7010 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61  mpts to encode a
a7020 20 76 61 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a   value between.*
a7030 2a 20 20 20 20 20 30 78 64 38 30 30 20 61 6e 64  *     0xd800 and
a7040 20 30 78 65 30 30 30 20 74 68 65 6e 20 69 74 20   0xe000 then it 
a7050 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 30  is rendered as 0
a7060 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20  xfffd..**.**  * 
a7070 20 42 79 74 65 73 20 69 6e 20 74 68 65 20 72 61   Bytes in the ra
a7080 6e 67 65 20 6f 66 20 30 78 38 30 20 74 68 72 6f  nge of 0x80 thro
a7090 75 67 68 20 30 78 62 66 20 77 68 69 63 68 20 6f  ugh 0xbf which o
a70a0 63 63 75 72 20 61 73 20 74 68 65 20 66 69 72 73  ccur as the firs
a70b0 74 0a 2a 2a 20 20 20 20 20 62 79 74 65 20 6f 66  t.**     byte of
a70c0 20 61 20 63 68 61 72 61 63 74 65 72 20 61 72 65   a character are
a70d0 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
a70e0 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72  single-byte char
a70f0 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e  acters.**     an
a7100 64 20 72 65 6e 64 65 72 65 64 20 61 73 20 74 68  d rendered as th
a7110 65 6d 73 65 6c 76 65 73 20 65 76 65 6e 20 74 68  emselves even th
a7120 6f 75 67 68 20 74 68 65 79 20 61 72 65 20 74 65  ough they are te
a7130 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20  chnically.**    
a7140 20 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74   invalid charact
a7150 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54  ers..**.**  *  T
a7160 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 63 65  his routine acce
a7170 70 74 73 20 61 6e 20 69 6e 66 69 6e 69 74 65 20  pts an infinite 
a7180 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72  number of differ
a7190 65 6e 74 20 55 54 46 38 20 65 6e 63 6f 64 69 6e  ent UTF8 encodin
a71a0 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e  gs.**     for un
a71b0 69 63 6f 64 65 20 76 61 6c 75 65 73 20 30 78 38  icode values 0x8
a71c0 30 20 61 6e 64 20 67 72 65 61 74 65 72 2e 20 20  0 and greater.  
a71d0 49 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65  It do not change
a71e0 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20   over-length.** 
a71f0 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f      encodings to
a7200 20 30 78 66 66 66 64 20 61 73 20 73 6f 6d 65 20   0xfffd as some 
a7210 73 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e  systems recommen
a7220 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45  d..*/.#define RE
a7230 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65  AD_UTF8(zIn, zTe
a7240 72 6d 2c 20 63 29 20 20 20 20 20 20 20 20 20 20  rm, c)          
a7250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7260 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b   \.  c = *(zIn++
a7270 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
a7280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a72a0 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b    if( c>=0xc0 ){
a72b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a72c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a72d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
a72e0 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38   c = sqlite3Utf8
a72f0 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20  Trans1[c-0xc0]; 
a7300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7310 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68          \.    wh
a7320 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20  ile( zIn!=zTerm 
a7330 26 26 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29  && (*zIn & 0xc0)
a7340 3d 3d 30 78 38 30 20 29 7b 20 20 20 20 20 20 20  ==0x80 ){       
a7350 20 20 20 20 20 5c 0a 20 20 20 20 20 20 63 20 3d       \.      c =
a7360 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20   (c<<6) + (0x3f 
a7370 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20  & *(zIn++));    
a7380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7390 20 20 5c 0a 20 20 20 20 7d 20 20 20 20 20 20 20    \.    }       
a73a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a73b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a73c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
a73d0 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 20  .    if( c<0x80 
a73e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a73f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7400 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a7410 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46        || (c&0xFF
a7420 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30 20  FFF800)==0xD800 
a7430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7440 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20           \.     
a7450 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46     || (c&0xFFFFF
a7460 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20  FFE)==0xFFFE ){ 
a7470 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 20 20   c = 0xFFFD; }  
a7480 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49        \.  }.SQLI
a7490 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a74a0 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 0a  qlite3Utf8Read(.
a74b0 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
a74c0 20 63 68 61 72 20 2a 7a 49 6e 2c 20 20 20 20 20   char *zIn,     
a74d0 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
a74e0 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  of UTF-8 charact
a74f0 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e  er */.  const un
a7500 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a  signed char **pz
a7510 4e 65 78 74 20 20 20 20 2f 2a 20 57 72 69 74 65  Next    /* Write
a7520 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 74   first byte past
a7530 20 55 54 46 2d 38 20 63 68 61 72 20 68 65 72 65   UTF-8 char here
a7540 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 0a   */.){.  int c;.
a7550 0a 20 20 2f 2a 20 53 61 6d 65 20 61 73 20 52 45  .  /* Same as RE
a7560 41 44 5f 55 54 46 38 28 29 20 61 62 6f 76 65 20  AD_UTF8() above 
a7570 62 75 74 20 77 69 74 68 6f 75 74 20 74 68 65 20  but without the 
a7580 7a 54 65 72 6d 20 70 61 72 61 6d 65 74 65 72 2e  zTerm parameter.
a7590 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72  .  ** For this r
a75a0 6f 75 74 69 6e 65 2c 20 77 65 20 61 73 73 75 6d  outine, we assum
a75b0 65 20 74 68 65 20 55 54 46 38 20 73 74 72 69 6e  e the UTF8 strin
a75c0 67 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f  g is always zero
a75d0 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 20 20 2a  -terminated..  *
a75e0 2f 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29  /.  c = *(zIn++)
a75f0 3b 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20  ;.  if( c>=0xc0 
a7600 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74  ){.    c = sqlit
a7610 65 33 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30  e3Utf8Trans1[c-0
a7620 78 63 30 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28  xc0];.    while(
a7630 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d   (*zIn & 0xc0)==
a7640 30 78 38 30 20 29 7b 0a 20 20 20 20 20 20 63 20  0x80 ){.      c 
a7650 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66  = (c<<6) + (0x3f
a7660 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20   & *(zIn++));.  
a7670 20 20 7d 0a 20 20 20 20 69 66 28 20 63 3c 30 78    }.    if( c<0x
a7680 38 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63  80.        || (c
a7690 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78  &0xFFFFF800)==0x
a76a0 44 38 30 30 0a 20 20 20 20 20 20 20 20 7c 7c 20  D800.        || 
a76b0 28 63 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d  (c&0xFFFFFFFE)==
a76c0 30 78 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30  0xFFFE ){  c = 0
a76d0 78 46 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a  xFFFD; }.  }.  *
a76e0 70 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b 0a 20 20  pzNext = zIn;.  
a76f0 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a  return c;.}.....
a7700 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41  /*.** If the TRA
a7710 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63  NSLATE_TRACE mac
a7720 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  ro is defined, t
a7730 68 65 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68  he value of each
a7740 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74   Mem is.** print
a7750 65 64 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20  ed on stderr on 
a7760 74 68 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64  the way into and
a7770 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56   out of sqlite3V
a7780 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28  dbeMemTranslate(
a7790 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e  )..*/ ./* #defin
a77a0 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43  e TRANSLATE_TRAC
a77b0 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  E 1 */..#ifndef 
a77c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
a77d0 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  6./*.** This rou
a77e0 74 69 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20  tine transforms 
a77f0 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78  the internal tex
a7800 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20  t encoding used 
a7810 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65  by pMem to.** de
a7820 73 69 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20  siredEnc. It is 
a7830 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20  an error if the 
a7840 73 74 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64  string is alread
a7850 79 20 6f 66 20 74 68 65 20 64 65 73 69 72 65 64  y of the desired
a7860 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72  .** encoding, or
a7870 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e   if *pMem does n
a7880 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72  ot contain a str
a7890 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51  ing value..*/.SQ
a78a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
a78b0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
a78c0 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d  ranslate(Mem *pM
a78d0 65 6d 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e  em, u8 desiredEn
a78e0 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20  c){.  int len;  
a78f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7900 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e    /* Maximum len
a7910 67 74 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74  gth of output st
a7920 72 69 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f  ring in bytes */
a7930 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
a7940 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20   *zOut;         
a7950 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
a7960 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75  ut buffer */.  u
a7970 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
a7980 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
a7990 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74       /* Input it
a79a0 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69  erator */.  unsi
a79b0 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d  gned char *zTerm
a79c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a79d0 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75    /* End of inpu
a79e0 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  t */.  unsigned 
a79f0 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
a7a00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a7a10 4f 75 74 70 75 74 20 69 74 65 72 61 74 6f 72 20  Output iterator 
a7a20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
a7a30 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  t c;..  assert( 
a7a40 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
a7a50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
a7a60 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
a7a70 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
a7a80 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
a7a90 53 74 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Str );.  assert(
a7aa0 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69   pMem->enc!=desi
a7ab0 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65  redEnc );.  asse
a7ac0 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30  rt( pMem->enc!=0
a7ad0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
a7ae0 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66  em->n>=0 );..#if
a7af0 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41   defined(TRANSLA
a7b00 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 66  TE_TRACE) && def
a7b10 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
a7b20 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20  G).  {.    char 
a7b30 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73  zBuf[100];.    s
a7b40 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65  qlite3VdbeMemPre
a7b50 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a  ttyPrint(pMem, z
a7b60 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74  Buf);.    fprint
a7b70 66 28 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54  f(stderr, "INPUT
a7b80 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b  :  %s\n", zBuf);
a7b90 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
a7ba0 2a 20 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61  * If the transla
a7bb0 74 69 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20  tion is between 
a7bc0 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e  UTF-16 little an
a7bd0 64 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68  d big endian, th
a7be0 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61  en .  ** all tha
a7bf0 74 20 69 73 20 72 65 71 75 69 72 65 64 20 69 73  t is required is
a7c00 20 74 6f 20 73 77 61 70 20 74 68 65 20 62 79 74   to swap the byt
a7c10 65 20 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61  e order. This ca
a7c20 73 65 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20  se is handled.  
a7c30 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66  ** differently f
a7c40 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a  rom the others..
a7c50 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d    */.  if( pMem-
a7c60 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46  >enc!=SQLITE_UTF
a7c70 38 20 26 26 20 64 65 73 69 72 65 64 45 6e 63 21  8 && desiredEnc!
a7c80 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a  =SQLITE_UTF8 ){.
a7c90 20 20 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20      u8 temp;.   
a7ca0 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20   int rc;.    rc 
a7cb0 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
a7cc0 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d  MakeWriteable(pM
a7cd0 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  em);.    if( rc!
a7ce0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
a7cf0 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
a7d00 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
a7d10 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
a7d20 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
a7d30 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29  .    zIn = (u8*)
a7d40 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65  pMem->z;.    zTe
a7d50 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e  rm = &zIn[pMem->
a7d60 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65  n&~1];.    while
a7d70 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20  ( zIn<zTerm ){. 
a7d80 20 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e       temp = *zIn
a7d90 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a  ;.      *zIn = *
a7da0 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a  (zIn+1);.      z
a7db0 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e  In++;.      *zIn
a7dc0 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d  ++ = temp;.    }
a7dd0 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d  .    pMem->enc =
a7de0 20 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20   desiredEnc;.   
a7df0 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f   goto translate_
a7e00 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  out;.  }..  /* S
a7e10 65 74 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61  et len to the ma
a7e20 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
a7e30 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 69  bytes required i
a7e40 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  n the output buf
a7e50 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65  fer. */.  if( de
a7e60 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
a7e70 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20  _UTF8 ){.    /* 
a7e80 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20  When converting 
a7e90 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65  from UTF-16, the
a7ea0 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20   maximum growth 
a7eb0 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20  results from.   
a7ec0 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20   ** translating 
a7ed0 61 20 32 2d 62 79 74 65 20 63 68 61 72 61 63 74  a 2-byte charact
a7ee0 65 72 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55  er to a 4-byte U
a7ef0 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a  TF-8 character..
a7f00 20 20 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20      ** A single 
a7f10 62 79 74 65 20 69 73 20 72 65 71 75 69 72 65 64  byte is required
a7f20 20 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20   for the output 
a7f30 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75  string.    ** nu
a7f40 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20  l-terminator..  
a7f50 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e    */.    pMem->n
a7f60 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20   &= ~1;.    len 
a7f70 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20  = pMem->n * 2 + 
a7f80 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
a7f90 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69  /* When converti
a7fa0 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f  ng from UTF-8 to
a7fb0 20 55 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69   UTF-16 the maxi
a7fc0 6d 75 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61  mum growth is ca
a7fd0 75 73 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e  used.    ** when
a7fe0 20 61 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20   a 1-byte UTF-8 
a7ff0 63 68 61 72 61 63 74 65 72 20 69 73 20 74 72 61  character is tra
a8000 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32  nslated into a 2
a8010 2d 62 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20  -byte UTF-16.   
a8020 20 2a 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54   ** character. T
a8030 77 6f 20 62 79 74 65 73 20 61 72 65 20 72 65 71  wo bytes are req
a8040 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74  uired in the out
a8050 70 75 74 20 62 75 66 66 65 72 20 66 6f 72 20 74  put buffer for t
a8060 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65  he.    ** nul-te
a8070 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f  rminator..    */
a8080 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d  .    len = pMem-
a8090 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a  >n * 2 + 2;.  }.
a80a0 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f  .  /* Set zIn to
a80b0 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74   point at the st
a80c0 61 72 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74  art of the input
a80d0 20 62 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72   buffer and zTer
a80e0 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a  m to point 1.  *
a80f0 2a 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20  * byte past the 
a8100 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56  end..  **.  ** V
a8110 61 72 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20  ariable zOut is 
a8120 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20  set to point at 
a8130 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
a8140 72 2c 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  r, space obtaine
a8150 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69  d.  ** from sqli
a8160 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20  te3_malloc()..  
a8170 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29  */.  zIn = (u8*)
a8180 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d  pMem->z;.  zTerm
a8190 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d   = &zIn[pMem->n]
a81a0 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74  ;.  zOut = sqlit
a81b0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d  e3DbMallocRaw(pM
a81c0 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20  em->db, len);.  
a81d0 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20  if( !zOut ){.   
a81e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
a81f0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20  OMEM;.  }.  z = 
a8200 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65  zOut;..  if( pMe
a8210 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  m->enc==SQLITE_U
a8220 54 46 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64  TF8 ){.    if( d
a8230 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54  esiredEnc==SQLIT
a8240 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20  E_UTF16LE ){.   
a8250 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55     /* UTF-8 -> U
a8260 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64  TF-16 Little-end
a8270 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  ian */.      whi
a8280 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b  le( zIn<zTerm ){
a8290 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20  .        /* c = 
a82a0 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
a82b0 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e  zIn, zTerm, (con
a82c0 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a  st u8**)&zIn); *
a82d0 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55  /.        READ_U
a82e0 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20  TF8(zIn, zTerm, 
a82f0 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54  c);.        WRIT
a8300 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b  E_UTF16LE(z, c);
a8310 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
a8320 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
a8330 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51  ( desiredEnc==SQ
a8340 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a  LITE_UTF16BE );.
a8350 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d        /* UTF-8 -
a8360 3e 20 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64  > UTF-16 Big-end
a8370 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  ian */.      whi
a8380 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b  le( zIn<zTerm ){
a8390 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20  .        /* c = 
a83a0 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
a83b0 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e  zIn, zTerm, (con
a83c0 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a  st u8**)&zIn); *
a83d0 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55  /.        READ_U
a83e0 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20  TF8(zIn, zTerm, 
a83f0 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54  c);.        WRIT
a8400 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b  E_UTF16BE(z, c);
a8410 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
a8420 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e     pMem->n = (in
a8430 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20  t)(z - zOut);.  
a8440 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65    *z++ = 0;.  }e
a8450 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
a8460 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
a8470 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20  ITE_UTF8 );.    
a8480 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53  if( pMem->enc==S
a8490 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b  QLITE_UTF16LE ){
a84a0 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36  .      /* UTF-16
a84b0 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d   Little-endian -
a84c0 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20  > UTF-8 */.     
a84d0 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72   while( zIn<zTer
a84e0 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41  m ){.        REA
a84f0 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63  D_UTF16LE(zIn, c
a8500 29 3b 20 0a 20 20 20 20 20 20 20 20 57 52 49 54  ); .        WRIT
a8510 45 5f 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20  E_UTF8(z, c);.  
a8520 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
a8530 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36  .      /* UTF-16
a8540 20 42 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55   Big-endian -> U
a8550 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68  TF-8 */.      wh
a8560 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29  ile( zIn<zTerm )
a8570 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55  {.        READ_U
a8580 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20  TF16BE(zIn, c); 
a8590 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55  .        WRITE_U
a85a0 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20  TF8(z, c);.     
a85b0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65   }.    }.    pMe
a85c0 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d  m->n = (int)(z -
a85d0 20 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a   zOut);.  }.  *z
a85e0 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
a85f0 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65  (pMem->n+(desire
a8600 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
a8610 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a  8?1:2))<=len );.
a8620 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
a8630 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
a8640 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d    pMem->flags &=
a8650 20 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45   ~(MEM_Static|ME
a8660 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29  M_Dyn|MEM_Ephem)
a8670 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20  ;.  pMem->enc = 
a8680 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d  desiredEnc;.  pM
a8690 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45  em->flags |= (ME
a86a0 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b  M_Term|MEM_Dyn);
a86b0 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68  .  pMem->z = (ch
a86c0 61 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d  ar*)zOut;.  pMem
a86d0 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d  ->zMalloc = pMem
a86e0 2d 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f  ->z;..translate_
a86f0 6f 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64  out:.#if defined
a8700 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45  (TRANSLATE_TRACE
a8710 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
a8720 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20  ITE_DEBUG).  {. 
a8730 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30     char zBuf[100
a8740 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
a8750 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
a8760 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20  (pMem, zBuf);.  
a8770 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
a8780 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22  , "OUTPUT: %s\n"
a8790 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e  , zBuf);.  }.#en
a87a0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  dif.  return SQL
a87b0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
a87c0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
a87d0 65 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d  ecks for a byte-
a87e0 6f 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68  order mark at th
a87f0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
a8800 68 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74  he .** UTF-16 st
a8810 72 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a  ring stored in *
a8820 70 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20  pMem. If one is 
a8830 70 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72  present, it is r
a8840 65 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68  emoved and.** th
a8850 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  e encoding of th
a8860 65 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20  e Mem adjusted. 
a8870 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
a8880 73 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20  s not do any.** 
a8890 62 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69  byte-swapping, i
a88a0 74 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e  t just sets Mem.
a88b0 65 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c  enc appropriatel
a88c0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c  y..**.** The all
a88d0 6f 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c  ocation (static,
a88e0 20 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61   dynamic etc.) a
a88f0 6e 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  nd encoding of t
a8900 68 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a  he Mem may be.**
a8910 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73   changed by this
a8920 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   function..*/.SQ
a8930 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
a8940 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48   sqlite3VdbeMemH
a8950 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d  andleBom(Mem *pM
a8960 65 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  em){.  int rc = 
a8970 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20  SQLITE_OK;.  u8 
a8980 62 6f 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65  bom = 0;..  asse
a8990 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29  rt( pMem->n>=0 )
a89a0 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e  ;.  if( pMem->n>
a89b0 31 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d  1 ){.    u8 b1 =
a89c0 20 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b   *(u8 *)pMem->z;
a89d0 0a 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28  .    u8 b2 = *((
a89e0 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b  (u8 *)pMem->z) +
a89f0 20 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d   1);.    if( b1=
a8a00 3d 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46  =0xFE && b2==0xF
a8a10 46 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d  F ){.      bom =
a8a20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b   SQLITE_UTF16BE;
a8a30 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62  .    }.    if( b
a8a40 31 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30  1==0xFF && b2==0
a8a50 78 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d  xFE ){.      bom
a8a60 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c   = SQLITE_UTF16L
a8a70 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a  E;.    }.  }.  .
a8a80 20 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20    if( bom ){.   
a8a90 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
a8aa0 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c  eMemMakeWriteabl
a8ab0 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28  e(pMem);.    if(
a8ac0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
a8ad0 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20  {.      pMem->n 
a8ae0 2d 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d  -= 2;.      memm
a8af0 6f 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d  ove(pMem->z, &pM
a8b00 65 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e  em->z[2], pMem->
a8b10 6e 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  n);.      pMem->
a8b20 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30  z[pMem->n] = '\0
a8b30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a  ';.      pMem->z
a8b40 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c  [pMem->n+1] = '\
a8b50 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  0';.      pMem->
a8b60 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72  flags |= MEM_Ter
a8b70 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65  m;.      pMem->e
a8b80 6e 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a  nc = bom;.    }.
a8b90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
a8ba0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
a8bb0 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
a8bc0 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61  /../*.** pZ is a
a8bd0 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75   UTF-8 encoded u
a8be0 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49  nicode string. I
a8bf0 66 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20  f nByte is less 
a8c00 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65  than zero,.** re
a8c10 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
a8c20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61  of unicode chara
a8c30 63 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74  cters in pZ up t
a8c40 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75  o (but not inclu
a8c50 64 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72  ding).** the fir
a8c60 73 74 20 30 78 30 30 20 62 79 74 65 2e 20 49 66  st 0x00 byte. If
a8c70 20 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65   nByte is not le
a8c80 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65  ss than zero, re
a8c90 74 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  turn the.** numb
a8ca0 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68  er of unicode ch
a8cb0 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20  aracters in the 
a8cc0 66 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70  first nByte of p
a8cd0 5a 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20  Z (or up to .** 
a8ce0 74 68 65 20 66 69 72 73 74 20 30 78 30 30 2c 20  the first 0x00, 
a8cf0 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20  whichever comes 
a8d00 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54  first)..*/.SQLIT
a8d10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a8d20 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e  lite3Utf8CharLen
a8d30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e  (const char *zIn
a8d40 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  , int nByte){.  
a8d50 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e  int r = 0;.  con
a8d60 73 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73  st u8 *z = (cons
a8d70 74 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e  t u8*)zIn;.  con
a8d80 73 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20  st u8 *zTerm;.  
a8d90 69 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a  if( nByte>=0 ){.
a8da0 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e      zTerm = &z[n
a8db0 42 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Byte];.  }else{.
a8dc0 20 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e      zTerm = (con
a8dd0 73 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d  st u8*)(-1);.  }
a8de0 0a 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54  .  assert( z<=zT
a8df0 65 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  erm );.  while( 
a8e00 2a 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d  *z!=0 && z<zTerm
a8e10 20 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53   ){.    SQLITE_S
a8e20 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20  KIP_UTF8(z);.   
a8e30 20 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75   r++;.  }.  retu
a8e40 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73  rn r;.}../* This
a8e50 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69   test function i
a8e60 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
a8e70 75 73 65 64 20 62 79 20 74 68 65 20 61 75 74 6f  used by the auto
a8e80 6d 61 74 65 64 20 74 65 73 74 2d 73 75 69 74 65  mated test-suite
a8e90 2e 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69  . .** Hence it i
a8ea0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
a8eb0 20 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73   in debug builds
a8ec0 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
a8ed0 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26  (SQLITE_TEST) &&
a8ee0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
a8ef0 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61  DEBUG)./*.** Tra
a8f00 6e 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20  nslate UTF-8 to 
a8f10 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  UTF-8..**.** Thi
a8f20 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74  s has the effect
a8f30 20 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20   of making sure 
a8f40 74 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 20  that the string 
a8f50 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a  is well-formed.*
a8f60 2a 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64  * UTF-8.  Miscod
a8f70 65 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72  ed characters ar
a8f80 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a  e removed..**.**
a8f90 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e   The translation
a8fa0 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63   is done in-plac
a8fb0 65 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 69  e (since it is i
a8fc0 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68  mpossible for th
a8fd0 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46  e.** correct UTF
a8fe0 2d 38 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62  -8 encoding to b
a8ff0 65 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20  e longer than a 
a9000 6d 61 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69  malformed encodi
a9010 6e 67 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ng)..*/.SQLITE_P
a9020 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a9030 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e  e3Utf8To8(unsign
a9040 65 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20  ed char *zIn){. 
a9050 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
a9060 7a 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e  zOut = zIn;.  un
a9070 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74  signed char *zSt
a9080 61 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32  art = zIn;.  u32
a9090 20 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49   c;..  while( zI
a90a0 6e 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20  n[0] ){.    c = 
a90b0 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
a90c0 7a 49 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a  zIn, (const u8**
a90d0 29 26 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20  )&zIn);.    if( 
a90e0 63 21 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20  c!=0xfffd ){.   
a90f0 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f     WRITE_UTF8(zO
a9100 75 74 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20  ut, c);.    }.  
a9110 7d 0a 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20  }.  *zOut = 0;. 
a9120 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f   return (int)(zO
a9130 75 74 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a  ut - zStart);.}.
a9140 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
a9150 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
a9160 36 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  6./*.** Convert 
a9170 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20  a UTF-16 string 
a9180 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e  in the native en
a9190 63 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54  coding into a UT
a91a0 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d  F-8 string..** M
a91b0 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68  emory to hold th
a91c0 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69  e UTF-8 string i
a91d0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
a91e0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61  sqlite3_malloc a
a91f0 6e 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72  nd must.** be fr
a9200 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69  eed by the calli
a9210 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  ng function..**.
a9220 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  ** NULL is retur
a9230 6e 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20  ned if there is 
a9240 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  an allocation er
a9250 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ror..*/.SQLITE_P
a9260 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
a9270 69 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c  ite3Utf16to8(sql
a9280 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
a9290 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79  void *z, int nBy
a92a0 74 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20  te){.  Mem m;.  
a92b0 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69  memset(&m, 0, si
a92c0 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62  zeof(m));.  m.db
a92d0 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33   = db;.  sqlite3
a92e0 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d  VdbeMemSetStr(&m
a92f0 2c 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49  , z, nByte, SQLI
a9300 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20  TE_UTF16NATIVE, 
a9310 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
a9320 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
a9330 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20  ngeEncoding(&m, 
a9340 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20  SQLITE_UTF8);.  
a9350 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
a9360 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  iled ){.    sqli
a9370 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
a9380 65 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d  e(&m);.    m.z =
a9390 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
a93a0 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d  ( (m.flags & MEM
a93b0 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d  _Term)!=0 || db-
a93c0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
a93d0 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c  .  assert( (m.fl
a93e0 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d  ags & MEM_Str)!=
a93f0 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
a9400 61 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72  ailed );.  retur
a9410 6e 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d  n (m.flags & MEM
a9420 5f 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a  _Dyn)!=0 ? m.z :
a9430 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
a9440 28 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a  (db, m.z);.}../*
a9450 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d  .** pZ is a UTF-
a9460 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f  16 encoded unico
a9470 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c 65 61  de string at lea
a9480 73 74 20 6e 43 68 61 72 20 63 68 61 72 61 63 74  st nChar charact
a9490 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74  ers long..** Ret
a94a0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
a94b0 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66  f bytes in the f
a94c0 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 63 6f  irst nChar unico
a94d0 64 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a  de characters.**
a94e0 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72 20 6d   in pZ.  nChar m
a94f0 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74  ust be non-negat
a9500 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ive..*/.SQLITE_P
a9510 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a9520 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28 63  e3Utf16ByteLen(c
a9530 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e 2c 20  onst void *zIn, 
a9540 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 69 6e  int nChar){.  in
a9550 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  t c;.  unsigned 
a9560 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 3d 20  char const *z = 
a9570 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30  zIn;.  int n = 0
a9580 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 55  ;.  if( SQLITE_U
a9590 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49  TF16NATIVE==SQLI
a95a0 54 45 5f 55 54 46 31 36 42 45 20 29 7b 0a 20 20  TE_UTF16BE ){.  
a95b0 20 20 2f 2a 20 55 73 69 6e 67 20 61 6e 20 22 69    /* Using an "i
a95c0 66 20 28 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  f (SQLITE_UTF16N
a95d0 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54  ATIVE==SQLITE_UT
a95e0 46 31 36 42 45 29 22 20 63 6f 6e 73 74 72 75 63  F16BE)" construc
a95f0 74 20 68 65 72 65 0a 20 20 20 20 2a 2a 20 61 6e  t here.    ** an
a9600 64 20 69 6e 20 6f 74 68 65 72 20 70 61 72 74 73  d in other parts
a9610 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 6d 65   of this file me
a9620 61 6e 73 20 74 68 61 74 20 61 74 20 6f 6e 65 20  ans that at one 
a9630 62 72 61 6e 63 68 20 77 69 6c 6c 0a 20 20 20 20  branch will.    
a9640 2a 2a 20 6e 6f 74 20 62 65 20 63 6f 76 65 72 65  ** not be covere
a9650 64 20 62 79 20 63 6f 76 65 72 61 67 65 20 74 65  d by coverage te
a9660 73 74 69 6e 67 20 6f 6e 20 61 6e 79 20 73 69 6e  sting on any sin
a9670 67 6c 65 20 68 6f 73 74 2e 20 42 75 74 20 63 6f  gle host. But co
a9680 76 65 72 61 67 65 0a 20 20 20 20 2a 2a 20 77 69  verage.    ** wi
a9690 6c 6c 20 62 65 20 63 6f 6d 70 6c 65 74 65 20 69  ll be complete i
a96a0 66 20 74 68 65 20 74 65 73 74 73 20 61 72 65 20  f the tests are 
a96b0 72 75 6e 20 6f 6e 20 62 6f 74 68 20 61 20 6c 69  run on both a li
a96c0 74 74 6c 65 2d 65 6e 64 69 61 6e 20 61 6e 64 20  ttle-endian and 
a96d0 0a 20 20 20 20 2a 2a 20 62 69 67 2d 65 6e 64 69  .    ** big-endi
a96e0 61 6e 20 68 6f 73 74 2e 20 42 65 63 61 75 73 65  an host. Because
a96f0 20 62 6f 74 68 20 74 68 65 20 55 54 46 31 36 4e   both the UTF16N
a9700 41 54 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45  ATIVE and SQLITE
a9710 5f 55 54 46 31 36 42 45 0a 20 20 20 20 2a 2a 20  _UTF16BE.    ** 
a9720 6d 61 63 72 6f 73 20 61 72 65 20 63 6f 6e 73 74  macros are const
a9730 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74  ant at compile t
a9740 69 6d 65 20 74 68 65 20 63 6f 6d 70 69 6c 65 72  ime the compiler
a9750 20 63 61 6e 20 64 65 74 65 72 6d 69 6e 65 0a 20   can determine. 
a9760 20 20 20 2a 2a 20 77 68 69 63 68 20 62 72 61 6e     ** which bran
a9770 63 68 20 77 69 6c 6c 20 62 65 20 66 6f 6c 6c 6f  ch will be follo
a9780 77 65 64 2e 20 49 74 20 69 73 20 74 68 65 72 65  wed. It is there
a9790 66 6f 72 65 20 61 73 73 75 6d 65 64 20 74 68 61  fore assumed tha
a97a0 74 20 6e 6f 20 72 75 6e 74 69 6d 65 0a 20 20 20  t no runtime.   
a97b0 20 2a 2a 20 70 65 6e 61 6c 74 79 20 69 73 20 70   ** penalty is p
a97c0 61 69 64 20 66 6f 72 20 74 68 69 73 20 22 69 66  aid for this "if
a97d0 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  " statement..   
a97e0 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 6e   */.    while( n
a97f0 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20  <nChar ){.      
a9800 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20  READ_UTF16BE(z, 
a9810 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20  c);.      n++;. 
a9820 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
a9830 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68 61 72    while( n<nChar
a9840 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f 55   ){.      READ_U
a9850 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20  TF16LE(z, c);.  
a9860 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20      n++;.    }. 
a9870 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74   }.  return (int
a9880 29 28 7a 2d 28 75 6e 73 69 67 6e 65 64 20 63 68  )(z-(unsigned ch
a9890 61 72 20 63 6f 6e 73 74 20 2a 29 7a 49 6e 29 3b  ar const *)zIn);
a98a0 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .}..#if defined(
a98b0 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a  SQLITE_TEST)./*.
a98c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
a98d0 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74  is called from t
a98e0 68 65 20 54 43 4c 20 74 65 73 74 20 66 75 6e 63  he TCL test func
a98f0 74 69 6f 6e 20 22 74 72 61 6e 73 6c 61 74 65 5f  tion "translate_
a9900 73 65 6c 66 74 65 73 74 22 2e 0a 2a 2a 20 49 74  selftest"..** It
a9910 20 63 68 65 63 6b 73 20 74 68 61 74 20 74 68 65   checks that the
a9920 20 70 72 69 6d 69 74 69 76 65 73 20 66 6f 72 20   primitives for 
a9930 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 6e 64 20  serializing and 
a9940 64 65 73 65 72 69 61 6c 69 7a 69 6e 67 0a 2a 2a  deserializing.**
a9950 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 65   characters in e
a9960 61 63 68 20 65 6e 63 6f 64 69 6e 67 20 61 72 65  ach encoding are
a9970 20 69 6e 76 65 72 73 65 73 20 6f 66 20 65 61 63   inverses of eac
a9980 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49  h other..*/.SQLI
a9990 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a99a0 73 71 6c 69 74 65 33 55 74 66 53 65 6c 66 54 65  sqlite3UtfSelfTe
a99b0 73 74 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69  st(void){.  unsi
a99c0 67 6e 65 64 20 69 6e 74 20 69 2c 20 74 3b 0a 20  gned int i, t;. 
a99d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a   unsigned char z
a99e0 42 75 66 5b 32 30 5d 3b 0a 20 20 75 6e 73 69 67  Buf[20];.  unsig
a99f0 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69  ned char *z;.  i
a9a00 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64  nt n;.  unsigned
a9a10 20 69 6e 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69   int c;..  for(i
a9a20 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30  =0; i<0x00110000
a9a30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20  ; i++){.    z = 
a9a40 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f  zBuf;.    WRITE_
a9a50 55 54 46 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20  UTF8(z, i);.    
a9a60 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66  n = (int)(z-zBuf
a9a70 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  );.    assert( n
a9a80 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20  >0 && n<=4 );.  
a9a90 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20    z[0] = 0;.    
a9aa0 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 63 20  z = zBuf;.    c 
a9ab0 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
a9ac0 64 28 7a 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a  d(z, (const u8**
a9ad0 29 26 7a 29 3b 0a 20 20 20 20 74 20 3d 20 69 3b  )&z);.    t = i;
a9ae0 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38  .    if( i>=0xD8
a9af0 30 30 20 26 26 20 69 3c 3d 30 78 44 46 46 46 20  00 && i<=0xDFFF 
a9b00 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20 20  ) t = 0xFFFD;.  
a9b10 20 20 69 66 28 20 28 69 26 30 78 46 46 46 46 46    if( (i&0xFFFFF
a9b20 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 20 74  FFE)==0xFFFE ) t
a9b30 20 3d 20 30 78 46 46 46 44 3b 0a 20 20 20 20 61   = 0xFFFD;.    a
a9b40 73 73 65 72 74 28 20 63 3d 3d 74 20 29 3b 0a 20  ssert( c==t );. 
a9b50 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42     assert( (z-zB
a9b60 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20  uf)==n );.  }.  
a9b70 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
a9b80 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
a9b90 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26   if( i>=0xD800 &
a9ba0 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e  & i<0xE000 ) con
a9bb0 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a  tinue;.    z = z
a9bc0 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55  Buf;.    WRITE_U
a9bd0 54 46 31 36 4c 45 28 7a 2c 20 69 29 3b 0a 20 20  TF16LE(z, i);.  
a9be0 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42    n = (int)(z-zB
a9bf0 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  uf);.    assert(
a9c00 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a   n>0 && n<=4 );.
a9c10 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20      z[0] = 0;.  
a9c20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20    z = zBuf;.    
a9c30 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c 20  READ_UTF16LE(z, 
a9c40 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  c);.    assert( 
a9c50 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65  c==i );.    asse
a9c60 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20  rt( (z-zBuf)==n 
a9c70 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  );.  }.  for(i=0
a9c80 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20  ; i<0x00110000; 
a9c90 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3e  i++){.    if( i>
a9ca0 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 78 45  =0xD800 && i<0xE
a9cb0 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  000 ) continue;.
a9cc0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
a9cd0 20 20 57 52 49 54 45 5f 55 54 46 31 36 42 45 28    WRITE_UTF16BE(
a9ce0 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28  z, i);.    n = (
a9cf0 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20  int)(z-zBuf);.  
a9d00 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26    assert( n>0 &&
a9d10 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30   n<=4 );.    z[0
a9d20 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a  ] = 0;.    z = z
a9d30 42 75 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54  Buf;.    READ_UT
a9d40 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16BE(z, c);.   
a9d50 20 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b   assert( c==i );
a9d60 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d  .    assert( (z-
a9d70 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a  zBuf)==n );.  }.
a9d80 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
a9d90 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64 69  TE_TEST */.#endi
a9da0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
a9db0 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _UTF16 */../****
a9dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
a9dd0 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  f utf.c ********
a9de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
a9e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
a9e20 20 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a 2a   file util.c ***
a9e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
a9e60 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
a9e70 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
a9e80 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
a9e90 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
a9ea0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
a9eb0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
a9ec0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
a9ed0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
a9ee0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
a9ef0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
a9f00 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
a9f10 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
a9f20 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
a9f30 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
a9f40 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
a9f50 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
a9f60 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
a9f70 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
a9f80 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
a9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9fd0 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75 6e  *.** Utility fun
a9fe0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f  ctions used thro
a9ff0 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a  ughout sqlite..*
aa000 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
aa010 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e  ontains function
aa020 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e 67  s for allocating
aa030 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72 69   memory, compari
aa040 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20 61  ng.** strings, a
aa050 6e 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74 68  nd stuff like th
aa060 61 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75  at..**.** $Id: u
aa070 74 69 6c 2e 63 2c 76 20 31 2e 32 35 34 20 32 30  til.c,v 1.254 20
aa080 30 39 2f 30 35 2f 30 36 20 31 39 3a 30 33 3a 31  09/05/06 19:03:1
aa090 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23  4 drh Exp $.*/.#
aa0a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56  ifdef SQLITE_HAV
aa0b0 45 5f 49 53 4e 41 4e 0a 23 20 69 6e 63 6c 75 64  E_ISNAN.# includ
aa0c0 65 20 3c 6d 61 74 68 2e 68 3e 0a 23 65 6e 64 69  e <math.h>.#endi
aa0d0 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  f../*.** Routine
aa0e0 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70 6f   needed to suppo
aa0f0 72 74 20 74 68 65 20 74 65 73 74 63 61 73 65 28  rt the testcase(
aa100 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64  ) macro..*/.#ifd
aa110 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41  ef SQLITE_COVERA
aa120 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  GE_TEST.SQLITE_P
aa130 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
aa140 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 20  te3Coverage(int 
aa150 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74  x){.  static int
aa160 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 64 75   dummy = 0;.  du
aa170 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65 6e 64  mmy += x;.}.#end
aa180 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  if../*.** Routin
aa190 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70  e needed to supp
aa1a0 6f 72 74 20 74 68 65 20 41 4c 57 41 59 53 28 29  ort the ALWAYS()
aa1b0 20 61 6e 64 20 4e 45 56 45 52 28 29 20 6d 61 63   and NEVER() mac
aa1c0 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ros..**.** The a
aa1d0 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c 57 41 59  rgument to ALWAY
aa1e0 53 28 29 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  S() should alway
aa1f0 73 20 62 65 20 74 72 75 65 20 61 6e 64 20 74 68  s be true and th
aa200 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f  e argument.** to
aa210 20 4e 45 56 45 52 28 29 20 73 68 6f 75 6c 64 20   NEVER() should 
aa220 61 6c 77 61 79 73 20 62 65 20 66 61 6c 73 65 2e  always be false.
aa230 20 20 49 66 20 65 69 74 68 65 72 20 69 73 20 6e    If either is n
aa240 6f 74 20 74 68 65 20 63 61 73 65 0a 2a 2a 20 74  ot the case.** t
aa250 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
aa260 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 6f 72   is called in or
aa270 64 65 72 20 74 6f 20 74 68 72 6f 77 20 61 6e 20  der to throw an 
aa280 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  error..**.** Thi
aa290 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 65  s routine only e
aa2a0 78 69 73 74 73 20 69 66 20 61 73 73 65 72 74 28  xists if assert(
aa2b0 29 20 69 73 20 6f 70 65 72 61 74 69 6f 6e 61 6c  ) is operational
aa2c0 2e 20 20 49 74 20 61 6c 77 61 79 73 0a 2a 2a 20  .  It always.** 
aa2d0 74 68 72 6f 77 73 20 61 6e 20 61 73 73 65 72 74  throws an assert
aa2e0 20 6f 6e 20 69 74 73 20 66 69 72 73 74 20 69 6e   on its first in
aa2f0 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 76  vocation.  The v
aa300 61 72 69 61 62 6c 65 20 68 61 73 20 61 20 6c 6f  ariable has a lo
aa310 6e 67 0a 2a 2a 20 6e 61 6d 65 20 74 6f 20 68 65  ng.** name to he
aa320 6c 70 20 74 68 65 20 61 73 73 65 72 74 28 29 20  lp the assert() 
aa330 6d 65 73 73 61 67 65 20 62 65 20 6d 6f 72 65 20  message be more 
aa340 72 65 61 64 61 62 6c 65 2e 20 20 54 68 65 20 76  readable.  The v
aa350 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 75 73  ariable.** is us
aa360 65 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20  ed to prevent a 
aa370 74 6f 6f 2d 63 6c 65 76 65 72 20 6f 70 74 69 6d  too-clever optim
aa380 69 7a 65 72 20 66 72 6f 6d 20 6f 70 74 69 6d 69  izer from optimi
aa390 7a 69 6e 67 20 6f 75 74 20 74 68 65 0a 2a 2a 20  zing out the.** 
aa3a0 65 6e 74 69 72 65 20 63 61 6c 6c 2e 0a 2a 2f 0a  entire call..*/.
aa3b0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53  #ifndef NDEBUG.S
aa3c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
aa3d0 74 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 28  t sqlite3Assert(
aa3e0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
aa3f0 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 41 4c 57  volatile int ALW
aa400 41 59 53 5f 77 61 73 5f 66 61 6c 73 65 5f 6f 72  AYS_was_false_or
aa410 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72 75 65 20  _NEVER_was_true 
aa420 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 41  = 0;.  assert( A
aa430 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c 73 65 5f  LWAYS_was_false_
aa440 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72 75  or_NEVER_was_tru
aa450 65 20 29 3b 20 20 20 20 20 20 2f 2a 20 41 6c 77  e );      /* Alw
aa460 61 79 73 20 66 61 69 6c 73 20 2a 2f 0a 20 20 72  ays fails */.  r
aa470 65 74 75 72 6e 20 41 4c 57 41 59 53 5f 77 61 73  eturn ALWAYS_was
aa480 5f 66 61 6c 73 65 5f 6f 72 5f 4e 45 56 45 52 5f  _false_or_NEVER_
aa490 77 61 73 5f 74 72 75 65 2b 2b 3b 20 20 20 20 20  was_true++;     
aa4a0 20 20 2f 2a 20 4e 6f 74 20 52 65 61 63 68 65 64    /* Not Reached
aa4b0 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a   */.}.#endif../*
aa4c0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
aa4d0 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20  if the floating 
aa4e0 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 4e  point value is N
aa4f0 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e 61 4e  ot a Number (NaN
aa500 29 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65  )..**.** Use the
aa510 20 6d 61 74 68 20 6c 69 62 72 61 72 79 20 69 73   math library is
aa520 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  nan() function i
aa530 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  f compiled with 
aa540 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41  SQLITE_HAVE_ISNA
aa550 4e 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  N..** Otherwise,
aa560 20 77 65 20 68 61 76 65 20 6f 75 72 20 6f 77 6e   we have our own
aa570 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
aa580 74 68 61 74 20 77 6f 72 6b 73 20 6f 6e 20 6d 6f  that works on mo
aa590 73 74 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 53  st systems..*/.S
aa5a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
aa5b0 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 64  t sqlite3IsNaN(d
aa5c0 6f 75 62 6c 65 20 78 29 7b 0a 20 20 69 6e 74 20  ouble x){.  int 
aa5d0 72 63 3b 20 20 20 2f 2a 20 54 68 65 20 76 61 6c  rc;   /* The val
aa5e0 75 65 20 72 65 74 75 72 6e 20 2a 2f 0a 23 69 66  ue return */.#if
aa5f0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
aa600 5f 48 41 56 45 5f 49 53 4e 41 4e 29 0a 20 20 2f  _HAVE_ISNAN).  /
aa610 2a 0a 20 20 2a 2a 20 53 79 73 74 65 6d 73 20 74  *.  ** Systems t
aa620 68 61 74 20 73 75 70 70 6f 72 74 20 74 68 65 20  hat support the 
aa630 69 73 6e 61 6e 28 29 20 6c 69 62 72 61 72 79 20  isnan() library 
aa640 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
aa650 70 72 6f 62 61 62 6c 79 0a 20 20 2a 2a 20 6d 61  probably.  ** ma
aa660 6b 65 20 75 73 65 20 6f 66 20 69 74 20 62 79 20  ke use of it by 
aa670 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 2d  compiling with -
aa680 44 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e  DSQLITE_HAVE_ISN
aa690 41 4e 2e 20 20 42 75 74 20 77 65 20 68 61 76 65  AN.  But we have
aa6a0 0a 20 20 2a 2a 20 66 6f 75 6e 64 20 74 68 61 74  .  ** found that
aa6b0 20 6d 61 6e 79 20 73 79 73 74 65 6d 73 20 64 6f   many systems do
aa6c0 20 6e 6f 74 20 68 61 76 65 20 61 20 77 6f 72 6b   not have a work
aa6d0 69 6e 67 20 69 73 6e 61 6e 28 29 20 66 75 6e 63  ing isnan() func
aa6e0 74 69 6f 6e 20 73 6f 0a 20 20 2a 2a 20 74 68 69  tion so.  ** thi
aa6f0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
aa700 20 69 73 20 70 72 6f 76 69 64 65 64 20 61 73 20   is provided as 
aa710 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a  an alternative..
aa720 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 4e    **.  ** This N
aa730 61 4e 20 74 65 73 74 20 73 6f 6d 65 74 69 6d 65  aN test sometime
aa740 73 20 66 61 69 6c 73 20 69 66 20 63 6f 6d 70 69  s fails if compi
aa750 6c 65 64 20 6f 6e 20 47 43 43 20 77 69 74 68 20  led on GCC with 
aa760 2d 66 66 61 73 74 2d 6d 61 74 68 2e 0a 20 20 2a  -ffast-math..  *
aa770 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  * On the other h
aa780 61 6e 64 2c 20 74 68 65 20 75 73 65 20 6f 66 20  and, the use of 
aa790 2d 66 66 61 73 74 2d 6d 61 74 68 20 63 6f 6d 65  -ffast-math come
aa7a0 73 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f  s with the follo
aa7b0 77 69 6e 67 0a 20 20 2a 2a 20 77 61 72 6e 69 6e  wing.  ** warnin
aa7c0 67 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  g:.  **.  **    
aa7d0 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 5b 2d    This option [-
aa7e0 66 66 61 73 74 2d 6d 61 74 68 5d 20 73 68 6f 75  ffast-math] shou
aa7f0 6c 64 20 6e 65 76 65 72 20 62 65 20 74 75 72 6e  ld never be turn
aa800 65 64 20 6f 6e 20 62 79 20 61 6e 79 0a 20 20 2a  ed on by any.  *
aa810 2a 20 20 20 20 20 20 2d 4f 20 6f 70 74 69 6f 6e  *      -O option
aa820 20 73 69 6e 63 65 20 69 74 20 63 61 6e 20 72 65   since it can re
aa830 73 75 6c 74 20 69 6e 20 69 6e 63 6f 72 72 65 63  sult in incorrec
aa840 74 20 6f 75 74 70 75 74 20 66 6f 72 20 70 72 6f  t output for pro
aa850 67 72 61 6d 73 0a 20 20 2a 2a 20 20 20 20 20 20  grams.  **      
aa860 77 68 69 63 68 20 64 65 70 65 6e 64 20 6f 6e 20  which depend on 
aa870 61 6e 20 65 78 61 63 74 20 69 6d 70 6c 65 6d 65  an exact impleme
aa880 6e 74 61 74 69 6f 6e 20 6f 66 20 49 45 45 45 20  ntation of IEEE 
aa890 6f 72 20 49 53 4f 20 0a 20 20 2a 2a 20 20 20 20  or ISO .  **    
aa8a0 20 20 72 75 6c 65 73 2f 73 70 65 63 69 66 69 63    rules/specific
aa8b0 61 74 69 6f 6e 73 20 66 6f 72 20 6d 61 74 68 20  ations for math 
aa8c0 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a  functions..  **.
aa8d0 20 20 2a 2a 20 55 6e 64 65 72 20 4d 53 56 43 2c    ** Under MSVC,
aa8e0 20 74 68 69 73 20 4e 61 4e 20 74 65 73 74 20 6d   this NaN test m
aa8f0 61 79 20 66 61 69 6c 20 69 66 20 63 6f 6d 70 69  ay fail if compi
aa900 6c 65 64 20 77 69 74 68 20 61 20 66 6c 6f 61 74  led with a float
aa910 69 6e 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74 20  ing-.  ** point 
aa920 70 72 65 63 69 73 69 6f 6e 20 6d 6f 64 65 20 6f  precision mode o
aa930 74 68 65 72 20 74 68 61 6e 20 2f 66 70 3a 70 72  ther than /fp:pr
aa940 65 63 69 73 65 2e 20 20 46 72 6f 6d 20 74 68 65  ecise.  From the
aa950 20 4d 53 44 4e 20 0a 20 20 2a 2a 20 64 6f 63 75   MSDN .  ** docu
aa960 6d 65 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a  mentation:.  **.
aa970 20 20 2a 2a 20 20 20 20 20 20 54 68 65 20 63 6f    **      The co
aa980 6d 70 69 6c 65 72 20 5b 77 69 74 68 20 2f 66 70  mpiler [with /fp
aa990 3a 70 72 65 63 69 73 65 5d 20 77 69 6c 6c 20 70  :precise] will p
aa9a0 72 6f 70 65 72 6c 79 20 68 61 6e 64 6c 65 20 63  roperly handle c
aa9b0 6f 6d 70 61 72 69 73 6f 6e 73 20 0a 20 20 2a 2a  omparisons .  **
aa9c0 20 20 20 20 20 20 69 6e 76 6f 6c 76 69 6e 67 20        involving 
aa9d0 4e 61 4e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  NaN. For example
aa9e0 2c 20 78 20 21 3d 20 78 20 65 76 61 6c 75 61 74  , x != x evaluat
aa9f0 65 73 20 74 6f 20 74 72 75 65 20 69 66 20 78 20  es to true if x 
aaa00 69 73 20 4e 61 4e 20 0a 20 20 2a 2a 20 20 20 20  is NaN .  **    
aaa10 20 20 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64 65    ....  */.#ifde
aaa20 66 20 5f 5f 46 41 53 54 5f 4d 41 54 48 5f 5f 0a  f __FAST_MATH__.
aaa30 23 20 65 72 72 6f 72 20 53 51 4c 69 74 65 20 77  # error SQLite w
aaa40 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72  ill not work cor
aaa50 72 65 63 74 6c 79 20 77 69 74 68 20 74 68 65 20  rectly with the 
aaa60 2d 66 66 61 73 74 2d 6d 61 74 68 20 6f 70 74 69  -ffast-math opti
aaa70 6f 6e 20 6f 66 20 47 43 43 2e 0a 23 65 6e 64 69  on of GCC..#endi
aaa80 66 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75  f.  volatile dou
aaa90 62 6c 65 20 79 20 3d 20 78 3b 0a 20 20 76 6f 6c  ble y = x;.  vol
aaaa0 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 7a 20 3d  atile double z =
aaab0 20 79 3b 0a 20 20 72 63 20 3d 20 28 79 21 3d 7a   y;.  rc = (y!=z
aaac0 29 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 20  );.#else  /* if 
aaad0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48  defined(SQLITE_H
aaae0 41 56 45 5f 49 53 4e 41 4e 29 20 2a 2f 0a 20 20  AVE_ISNAN) */.  
aaaf0 72 63 20 3d 20 69 73 6e 61 6e 28 78 29 3b 0a 23  rc = isnan(x);.#
aab00 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
aab10 48 41 56 45 5f 49 53 4e 41 4e 20 2a 2f 0a 20 20  HAVE_ISNAN */.  
aab20 74 65 73 74 63 61 73 65 28 20 72 63 20 29 3b 0a  testcase( rc );.
aab30 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
aab40 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20  /*.** Compute a 
aab50 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 74 68  string length th
aab60 61 74 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f  at is limited to
aab70 20 77 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f   what can be sto
aab80 72 65 64 20 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20  red in.** lower 
aab90 33 30 20 62 69 74 73 20 6f 66 20 61 20 33 32 2d  30 bits of a 32-
aaba0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
aabb0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61  er..**.** The va
aabc0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 77 69 6c  lue returned wil
aabd0 6c 20 6e 65 76 65 72 20 62 65 20 6e 65 67 61 74  l never be negat
aabe0 69 76 65 2e 20 20 4e 6f 72 20 77 69 6c 6c 20 69  ive.  Nor will i
aabf0 74 20 65 76 65 72 20 62 65 20 67 72 65 61 74 65  t ever be greate
aac00 72 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 63  r.** than the ac
aac10 74 75 61 6c 20 6c 65 6e 67 74 68 20 6f 66 20 74  tual length of t
aac20 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20  he string.  For 
aac30 76 65 72 79 20 6c 6f 6e 67 20 73 74 72 69 6e 67  very long string
aac40 73 20 28 67 72 65 61 74 65 72 0a 2a 2a 20 74 68  s (greater.** th
aac50 61 6e 20 31 47 69 42 29 20 74 68 65 20 76 61 6c  an 1GiB) the val
aac60 75 65 20 72 65 74 75 72 6e 65 64 20 6d 69 67 68  ue returned migh
aac70 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 74  t be less than t
aac80 68 65 20 74 72 75 65 20 73 74 72 69 6e 67 20 6c  he true string l
aac90 65 6e 67 74 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ength..*/.SQLITE
aaca0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
aacb0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e  ite3Strlen30(con
aacc0 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63  st char *z){.  c
aacd0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20  onst char *z2 = 
aace0 7a 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 32 20  z;.  while( *z2 
aacf0 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74  ){ z2++; }.  ret
aad00 75 72 6e 20 30 78 33 66 66 66 66 66 66 66 20 26  urn 0x3fffffff &
aad10 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a   (int)(z2 - z);.
aad20 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
aad30 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   most recent err
aad40 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f  or code and erro
aad50 72 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65  r string for the
aad60 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c   sqlite.** handl
aad70 65 20 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f  e "db". The erro
aad80 72 20 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f  r code is set to
aad90 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a   "err_code"..**.
aada0 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  ** If it is not 
aadb0 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f  NULL, string zFo
aadc0 72 6d 61 74 20 73 70 65 63 69 66 69 65 73 20 74  rmat specifies t
aadd0 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65  he format of the
aade0 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67  .** error string
aadf0 20 69 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66   in the style of
aae00 20 74 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63   the printf func
aae10 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f  tions: The follo
aae20 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63  wing.** format c
aae30 68 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c  haracters are al
aae40 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  lowed:.**.**    
aae50 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74    %s      Insert
aae60 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20   a string.**    
aae70 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69    %z      A stri
aae80 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  ng that should b
aae90 65 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73  e freed after us
aaea0 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20  e.**      %d    
aaeb0 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65    Insert an inte
aaec0 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20  ger.**      %T  
aaed0 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b      Insert a tok
aaee0 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20  en.**      %S   
aaef0 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69     Insert the fi
aaf00 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61  rst element of a
aaf10 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a   SrcList.**.** z
aaf20 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73  Format and any s
aaf30 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61  tring tokens tha
aaf40 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20  t follow it are 
aaf50 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a  assumed to be.**
aaf60 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d   encoded in UTF-
aaf70 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61  8..**.** To clea
aaf80 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  r the most recen
aaf90 74 20 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69  t error for sqli
aafa0 74 65 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20  te handle "db", 
aafb0 73 71 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20  sqlite3Error.** 
aafc0 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64  should be called
aafd0 20 77 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73   with err_code s
aafe0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20  et to SQLITE_OK 
aaff0 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a  and zFormat set.
ab000 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53  ** to NULL..*/.S
ab010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
ab020 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  id sqlite3Error(
ab030 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
ab040 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74   err_code, const
ab050 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
ab060 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26  ...){.  if( db &
ab070 26 20 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28  & (db->pErr || (
ab080 64 62 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74  db->pErr = sqlit
ab090 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21  e3ValueNew(db))!
ab0a0 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65  =0) ){.    db->e
ab0b0 72 72 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64  rrCode = err_cod
ab0c0 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d  e;.    if( zForm
ab0d0 61 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72  at ){.      char
ab0e0 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69   *z;.      va_li
ab0f0 73 74 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f  st ap;.      va_
ab100 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61  start(ap, zForma
ab110 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71  t);.      z = sq
ab120 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62  lite3VMPrintf(db
ab130 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
ab140 20 20 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29        va_end(ap)
ab150 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
ab160 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
ab170 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49  Err, -1, z, SQLI
ab180 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
ab190 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65  DYNAMIC);.    }e
ab1a0 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
ab1b0 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62  e3ValueSetStr(db
ab1c0 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51  ->pErr, 0, 0, SQ
ab1d0 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
ab1e0 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d  E_STATIC);.    }
ab1f0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
ab200 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  d an error messa
ab210 67 65 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45  ge to pParse->zE
ab220 72 72 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d  rrMsg and increm
ab230 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  ent pParse->nErr
ab240 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
ab250 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68  ng formatting ch
ab260 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c  aracters are all
ab270 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  owed:.**.**     
ab280 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20   %s      Insert 
ab290 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20  a string.**     
ab2a0 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e   %z      A strin
ab2b0 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  g that should be
ab2c0 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65   freed after use
ab2d0 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20  .**      %d     
ab2e0 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67   Insert an integ
ab2f0 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20  er.**      %T   
ab300 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65     Insert a toke
ab310 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20  n.**      %S    
ab320 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72    Insert the fir
ab330 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20  st element of a 
ab340 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68  SrcList.**.** Th
ab350 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  is function shou
ab360 6c 64 20 62 65 20 75 73 65 64 20 74 6f 20 72 65  ld be used to re
ab370 70 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74  port any error t
ab380 68 61 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73  hat occurs whils
ab390 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61  t.** compiling a
ab3a0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
ab3b0 28 69 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c  (i.e. within sql
ab3c0 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e  ite3_prepare()).
ab3d0 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69   The.** last thi
ab3e0 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  ng the sqlite3_p
ab3f0 72 65 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f  repare() functio
ab400 6e 20 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74  n does is copy t
ab410 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72  he error.** stor
ab420 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
ab430 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ion into the dat
ab440 61 62 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69  abase handle usi
ab450 6e 67 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  ng sqlite3Error(
ab460 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73  )..** Function s
ab470 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 73 68  qlite3Error() sh
ab480 6f 75 6c 64 20 62 65 20 75 73 65 64 20 64 75 72  ould be used dur
ab490 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78  ing statement ex
ab4a0 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69  ecution.** (sqli
ab4b0 74 65 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29  te3_step() etc.)
ab4c0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
ab4d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
ab4e0 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a  ErrorMsg(Parse *
ab4f0 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
ab500 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
ab510 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
ab520 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
ab530 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70   pParse->db;.  p
ab540 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
ab550 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72 73   testcase( pPars
ab560 65 2d 3e 7a 45 72 72 4d 73 67 21 3d 30 20 29 3b  e->zErrMsg!=0 );
ab570 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
ab580 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72  (db, pParse->zEr
ab590 72 4d 73 67 29 3b 0a 20 20 76 61 5f 73 74 61 72  rMsg);.  va_star
ab5a0 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
ab5b0 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73    pParse->zErrMs
ab5c0 67 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  g = sqlite3VMPri
ab5d0 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  ntf(db, zFormat,
ab5e0 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
ab5f0 70 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  p);.  if( pParse
ab600 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
ab610 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  ){.    pParse->r
ab620 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
ab630 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
ab640 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 6d  lear the error m
ab650 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
ab660 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49  , if any.*/.SQLI
ab670 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
ab680 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61  sqlite3ErrorClea
ab690 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  r(Parse *pParse)
ab6a0 7b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  {.  sqlite3DbFre
ab6b0 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 50  e(pParse->db, pP
ab6c0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  arse->zErrMsg);.
ab6d0 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73    pParse->zErrMs
ab6e0 67 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d  g = 0;.  pParse-
ab6f0 3e 6e 45 72 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  >nErr = 0;.}../*
ab700 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 53  .** Convert an S
ab710 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 20  QL-style quoted 
ab720 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f  string into a no
ab730 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20 72  rmal string by r
ab740 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71  emoving.** the q
ab750 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73 2e  uote characters.
ab760 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e    The conversion
ab770 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63   is done in-plac
ab780 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e  e.  If the.** in
ab790 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 67  put does not beg
ab7a0 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 65 20  in with a quote 
ab7b0 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20  character, then 
ab7c0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
ab7d0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
ab7e0 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74 72 69  * The input stri
ab7f0 6e 67 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d  ng must be zero-
ab800 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 41 20 6e  terminated.  A n
ab810 65 77 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  ew zero-terminat
ab820 6f 72 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74  or.** is added t
ab830 6f 20 74 68 65 20 64 65 71 75 6f 74 65 64 20 73  o the dequoted s
ab840 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tring..**.** The
ab850 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
ab860 20 2d 31 20 69 66 20 6e 6f 20 64 65 71 75 6f 74   -1 if no dequot
ab870 69 6e 67 20 6f 63 63 75 72 73 20 6f 72 20 74 68  ing occurs or th
ab880 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 0a  e length of the.
ab890 2a 2a 20 64 65 71 75 6f 74 65 64 20 73 74 72 69  ** dequoted stri
ab8a0 6e 67 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66  ng, exclusive of
ab8b0 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e   the zero termin
ab8c0 61 74 6f 72 2c 20 69 66 20 64 65 71 75 6f 74 69  ator, if dequoti
ab8d0 6e 67 20 64 6f 65 73 0a 2a 2a 20 6f 63 63 75 72  ng does.** occur
ab8e0 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 65 62  ..**.** 2002-Feb
ab8f0 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74 69 6e  -14: This routin
ab900 65 20 69 73 20 65 78 74 65 6e 64 65 64 20 74 6f  e is extended to
ab910 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63 65 73   remove MS-Acces
ab920 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 61 63 6b  s style.** brack
ab930 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 6e 64 20  ets from around 
ab940 69 64 65 6e 74 69 66 65 72 73 2e 20 20 46 6f 72  identifers.  For
ab950 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b 61 2d 62   example:  "[a-b
ab960 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a 2a 2a 20  -c]" becomes.** 
ab970 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49  "a-b-c"..*/.SQLI
ab980 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ab990 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68  qlite3Dequote(ch
ab9a0 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 71  ar *z){.  char q
ab9b0 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  uote;.  int i, j
ab9c0 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72  ;.  if( z==0 ) r
ab9d0 65 74 75 72 6e 20 2d 31 3b 0a 20 20 71 75 6f 74  eturn -1;.  quot
ab9e0 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74  e = z[0];.  swit
ab9f0 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20  ch( quote ){.   
aba00 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65   case '\'':  bre
aba10 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27  ak;.    case '"'
aba20 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63  :   break;.    c
aba30 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b  ase '`':   break
aba40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
aba50 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f   /* For MySQL co
aba60 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20  mpatibility */. 
aba70 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71     case '[':   q
aba80 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65  uote = ']';  bre
aba90 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53  ak;  /* For MS S
abaa0 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69  qlServer compati
abab0 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65  bility */.    de
abac0 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e  fault:    return
abad0 20 2d 31 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69   -1;.  }.  for(i
abae0 3d 31 2c 20 6a 3d 30 3b 20 41 4c 57 41 59 53 28  =1, j=0; ALWAYS(
abaf0 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  z[i]); i++){.   
abb00 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65   if( z[i]==quote
abb10 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b   ){.      if( z[
abb20 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20  i+1]==quote ){. 
abb30 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20         z[j++] = 
abb40 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 69  quote;.        i
abb50 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ++;.      }else{
abb60 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
abb70 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
abb80 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20  e{.      z[j++] 
abb90 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  = z[i];.    }.  
abba0 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20  }.  z[j] = 0;.  
abbb0 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20  return j;.}../* 
abbc0 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f 72 74  Convenient short
abbd0 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65  -hand */.#define
abbe0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20 73 71   UpperToLower sq
abbf0 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
abc00 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79  r../*.** Some sy
abc10 73 74 65 6d 73 20 68 61 76 65 20 73 74 72 69 63  stems have stric
abc20 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20 68 61  mp().  Others ha
abc30 76 65 20 73 74 72 63 61 73 65 63 6d 70 28 29 2e  ve strcasecmp().
abc40 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68 65    Because.** the
abc50 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69 73 74  re is no consist
abc60 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20 64 65  ency, we will de
abc70 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f  fine our own..*/
abc80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
abc90 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 43  int sqlite3StrIC
abca0 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  mp(const char *z
abcb0 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  Left, const char
abcc0 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 72 65 67   *zRight){.  reg
abcd0 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 63  ister unsigned c
abce0 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 20  har *a, *b;.  a 
abcf0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
abd00 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d 20   *)zLeft;.  b = 
abd10 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
abd20 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c 65  )zRight;.  while
abd30 28 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65 72  ( *a!=0 && Upper
abd40 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 70  ToLower[*a]==Upp
abd50 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20  erToLower[*b]){ 
abd60 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 65  a++; b++; }.  re
abd70 74 75 72 6e 20 55 70 70 65 72 54 6f 4c 6f 77 65  turn UpperToLowe
abd80 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c  r[*a] - UpperToL
abd90 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49  ower[*b];.}.SQLI
abda0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
abdb0 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 63  qlite3StrNICmp(c
abdc0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
abdd0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
abde0 69 67 68 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  ight, int N){.  
abdf0 72 65 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65  register unsigne
abe00 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20  d char *a, *b;. 
abe10 20 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63   a = (unsigned c
abe20 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62  har *)zLeft;.  b
abe30 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
abe40 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68  r *)zRight;.  wh
abe50 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 20  ile( N-- > 0 && 
abe60 2a 61 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f  *a!=0 && UpperTo
abe70 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72  Lower[*a]==Upper
abe80 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b  ToLower[*b]){ a+
abe90 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75  +; b++; }.  retu
abea0 72 6e 20 4e 3c 30 20 3f 20 30 20 3a 20 55 70 70  rn N<0 ? 0 : Upp
abeb0 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20  erToLower[*a] - 
abec0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d  UpperToLower[*b]
abed0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
abee0 6e 20 54 52 55 45 20 69 66 20 7a 20 69 73 20 61  n TRUE if z is a
abef0 20 70 75 72 65 20 6e 75 6d 65 72 69 63 20 73 74   pure numeric st
abf00 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 46 41  ring.  Return FA
abf10 4c 53 45 20 61 6e 64 20 6c 65 61 76 65 0a 2a 2a  LSE and leave.**
abf20 20 2a 72 65 61 6c 6e 75 6d 20 75 6e 63 68 61 6e   *realnum unchan
abf30 67 65 64 20 69 66 20 74 68 65 20 73 74 72 69 6e  ged if the strin
abf40 67 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 63  g contains any c
abf50 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20 69  haracter which i
abf60 73 20 6e 6f 74 0a 2a 2a 20 70 61 72 74 20 6f 66  s not.** part of
abf70 20 61 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a   a number..**.**
abf80 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69   If the string i
abf90 73 20 70 75 72 65 20 6e 75 6d 65 72 69 63 2c 20  s pure numeric, 
abfa0 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20  set *realnum to 
abfb0 54 52 55 45 20 69 66 20 74 68 65 20 73 74 72 69  TRUE if the stri
abfc0 6e 67 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74  ng.** contains t
abfd0 68 65 20 27 2e 27 20 63 68 61 72 61 63 74 65 72  he '.' character
abfe0 20 6f 72 20 61 6e 20 22 45 2b 30 30 30 22 20 73   or an "E+000" s
abff0 74 79 6c 65 20 65 78 70 6f 6e 65 6e 74 69 61 74  tyle exponentiat
ac000 69 6f 6e 20 73 75 66 66 69 78 2e 0a 2a 2a 20 4f  ion suffix..** O
ac010 74 68 65 72 77 69 73 65 20 73 65 74 20 2a 72 65  therwise set *re
ac020 61 6c 6e 75 6d 20 74 6f 20 46 41 4c 53 45 2e 20  alnum to FALSE. 
ac030 20 4e 6f 74 65 20 74 68 61 74 20 6a 75 73 74 20   Note that just 
ac040 62 65 63 61 75 65 20 2a 72 65 61 6c 6e 75 6d 20  becaue *realnum 
ac050 69 73 0a 2a 2a 20 66 61 6c 73 65 20 64 6f 65 73  is.** false does
ac060 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74   not mean that t
ac070 68 65 20 6e 75 6d 62 65 72 20 63 61 6e 20 62 65  he number can be
ac080 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f   successfully co
ac090 6e 76 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20  nverted into.** 
ac0a0 61 6e 20 69 6e 74 65 67 65 72 20 2d 20 69 74 20  an integer - it 
ac0b0 6d 69 67 68 74 20 62 65 20 74 6f 6f 20 62 69 67  might be too big
ac0c0 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6d 70 74 79  ..**.** An empty
ac0d0 20 73 74 72 69 6e 67 20 69 73 20 63 6f 6e 73 69   string is consi
ac0e0 64 65 72 65 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69  dered non-numeri
ac0f0 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  c..*/.SQLITE_PRI
ac100 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ac110 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63  IsNumber(const c
ac120 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 72 65 61  har *z, int *rea
ac130 6c 6e 75 6d 2c 20 75 38 20 65 6e 63 29 7b 0a 20  lnum, u8 enc){. 
ac140 20 69 6e 74 20 69 6e 63 72 20 3d 20 28 65 6e 63   int incr = (enc
ac150 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a  ==SQLITE_UTF8?1:
ac160 32 29 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53  2);.  if( enc==S
ac170 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 20  QLITE_UTF16BE ) 
ac180 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27  z++;.  if( *z=='
ac190 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20  -' || *z=='+' ) 
ac1a0 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 28  z += incr;.  if(
ac1b0 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74   !sqlite3Isdigit
ac1c0 28 2a 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75  (*z) ){.    retu
ac1d0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d  rn 0;.  }.  z +=
ac1e0 20 69 6e 63 72 3b 0a 20 20 2a 72 65 61 6c 6e 75   incr;.  *realnu
ac1f0 6d 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  m = 0;.  while( 
ac200 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
ac210 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b  z) ){ z += incr;
ac220 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27   }.  if( *z=='.'
ac230 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63   ){.    z += inc
ac240 72 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69  r;.    if( !sqli
ac250 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29  te3Isdigit(*z) )
ac260 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77   return 0;.    w
ac270 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64  hile( sqlite3Isd
ac280 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d  igit(*z) ){ z +=
ac290 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65   incr; }.    *re
ac2a0 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20  alnum = 1;.  }. 
ac2b0 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20   if( *z=='e' || 
ac2c0 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a  *z=='E' ){.    z
ac2d0 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66   += incr;.    if
ac2e0 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d  ( *z=='+' || *z=
ac2f0 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72  ='-' ) z += incr
ac300 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74  ;.    if( !sqlit
ac310 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20  e3Isdigit(*z) ) 
ac320 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68  return 0;.    wh
ac330 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
ac340 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20  git(*z) ){ z += 
ac350 69 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61  incr; }.    *rea
ac360 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  lnum = 1;.  }.  
ac370 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a  return *z==0;.}.
ac380 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e  ./*.** The strin
ac390 67 20 7a 5b 5d 20 69 73 20 61 6e 20 61 73 63 69  g z[] is an asci
ac3a0 69 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  i representation
ac3b0 20 6f 66 20 61 20 72 65 61 6c 20 6e 75 6d 62 65   of a real numbe
ac3c0 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68  r..** Convert th
ac3d0 69 73 20 73 74 72 69 6e 67 20 74 6f 20 61 20 64  is string to a d
ac3e0 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ouble..**.** Thi
ac3f0 73 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65  s routine assume
ac400 73 20 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c 6c  s that z[] reall
ac410 79 20 69 73 20 61 20 76 61 6c 69 64 20 6e 75 6d  y is a valid num
ac420 62 65 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20 69  ber.  If it.** i
ac430 73 20 6e 6f 74 2c 20 74 68 65 20 72 65 73 75 6c  s not, the resul
ac440 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  t is undefined..
ac450 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
ac460 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 74 65  ne is used inste
ac470 61 64 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  ad of the librar
ac480 79 20 61 74 6f 66 28 29 20 66 75 6e 63 74 69 6f  y atof() functio
ac490 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65  n because.** the
ac4a0 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29 20   library atof() 
ac4b0 6d 69 67 68 74 20 77 61 6e 74 20 74 6f 20 75 73  might want to us
ac4c0 65 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63  e "," as the dec
ac4d0 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74 65  imal point inste
ac4e0 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65 70  ad.** of "." dep
ac4f0 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f  ending on how lo
ac500 63 61 6c 65 20 69 73 20 73 65 74 2e 20 20 42 75  cale is set.  Bu
ac510 74 20 74 68 61 74 20 77 6f 75 6c 64 20 63 61 75  t that would cau
ac520 73 65 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66  se problems.** f
ac530 6f 72 20 53 51 4c 2e 20 20 53 6f 20 74 68 69 73  or SQL.  So this
ac540 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20   routine always 
ac550 75 73 65 73 20 22 2e 22 20 72 65 67 61 72 64 6c  uses "." regardl
ac560 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a  ess of locale..*
ac570 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
ac580 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46   int sqlite3AtoF
ac590 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
ac5a0 64 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 29  double *pResult)
ac5b0 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
ac5c0 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
ac5d0 4f 49 4e 54 0a 20 20 69 6e 74 20 73 69 67 6e 20  OINT.  int sign 
ac5e0 3d 20 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  = 1;.  const cha
ac5f0 72 20 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a 20  r *zBegin = z;. 
ac600 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45   LONGDOUBLE_TYPE
ac610 20 76 31 20 3d 20 30 2e 30 3b 0a 20 20 69 6e 74   v1 = 0.0;.  int
ac620 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 20 3d 20   nSignificant = 
ac630 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69  0;.  while( sqli
ac640 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29  te3Isspace(*z) )
ac650 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d   z++;.  if( *z==
ac660 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e 20  '-' ){.    sign 
ac670 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  = -1;.    z++;. 
ac680 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27   }else if( *z=='
ac690 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  +' ){.    z++;. 
ac6a0 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d   }.  while( z[0]
ac6b0 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 2b 2b  =='0' ){.    z++
ac6c0 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 73  ;.  }.  while( s
ac6d0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
ac6e0 29 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 76 31  ) ){.    v1 = v1
ac6f0 2a 31 30 2e 30 20 2b 20 28 2a 7a 20 2d 20 27 30  *10.0 + (*z - '0
ac700 27 29 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20  ');.    z++;.   
ac710 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 2b 2b 3b   nSignificant++;
ac720 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27  .  }.  if( *z=='
ac730 2e 27 20 29 7b 0a 20 20 20 20 4c 4f 4e 47 44 4f  .' ){.    LONGDO
ac740 55 42 4c 45 5f 54 59 50 45 20 64 69 76 69 73 6f  UBLE_TYPE diviso
ac750 72 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b 2b  r = 1.0;.    z++
ac760 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 67 6e 69  ;.    if( nSigni
ac770 66 69 63 61 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  ficant==0 ){.   
ac780 20 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d     while( z[0]==
ac790 27 30 27 20 29 7b 0a 20 20 20 20 20 20 20 20 64  '0' ){.        d
ac7a0 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b 0a  ivisor *= 10.0;.
ac7b0 20 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20          z++;.   
ac7c0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77     }.    }.    w
ac7d0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64  hile( sqlite3Isd
ac7e0 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20  igit(*z) ){.    
ac7f0 20 20 69 66 28 20 6e 53 69 67 6e 69 66 69 63 61    if( nSignifica
ac800 6e 74 3c 31 38 20 29 7b 0a 20 20 20 20 20 20 20  nt<18 ){.       
ac810 20 76 31 20 3d 20 76 31 2a 31 30 2e 30 20 2b 20   v1 = v1*10.0 + 
ac820 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20  (*z - '0');.    
ac830 20 20 20 20 64 69 76 69 73 6f 72 20 2a 3d 20 31      divisor *= 1
ac840 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 6e 53 69  0.0;.        nSi
ac850 67 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 20  gnificant++;.   
ac860 20 20 20 7d 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a     }.      z++;.
ac870 20 20 20 20 7d 0a 20 20 20 20 76 31 20 2f 3d 20      }.    v1 /= 
ac880 64 69 76 69 73 6f 72 3b 0a 20 20 7d 0a 20 20 69  divisor;.  }.  i
ac890 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a  f( *z=='e' || *z
ac8a0 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 69 6e 74  =='E' ){.    int
ac8b0 20 65 73 69 67 6e 20 3d 20 31 3b 0a 20 20 20 20   esign = 1;.    
ac8c0 69 6e 74 20 65 76 61 6c 20 3d 20 30 3b 0a 20 20  int eval = 0;.  
ac8d0 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50    LONGDOUBLE_TYP
ac8e0 45 20 73 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20  E scale = 1.0;. 
ac8f0 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 69 66 28 20     z++;.    if( 
ac900 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20  *z=='-' ){.     
ac910 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20   esign = -1;.   
ac920 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73     z++;.    }els
ac930 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b  e if( *z=='+' ){
ac940 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
ac950 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c  }.    while( sql
ac960 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20  ite3Isdigit(*z) 
ac970 29 7b 0a 20 20 20 20 20 20 65 76 61 6c 20 3d 20  ){.      eval = 
ac980 65 76 61 6c 2a 31 30 20 2b 20 2a 7a 20 2d 20 27  eval*10 + *z - '
ac990 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20  0';.      z++;. 
ac9a0 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20     }.    while( 
ac9b0 65 76 61 6c 3e 3d 36 34 20 29 7b 20 73 63 61 6c  eval>=64 ){ scal
ac9c0 65 20 2a 3d 20 31 2e 30 65 2b 36 34 3b 20 65 76  e *= 1.0e+64; ev
ac9d0 61 6c 20 2d 3d 20 36 34 3b 20 7d 0a 20 20 20 20  al -= 64; }.    
ac9e0 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 31 36 20  while( eval>=16 
ac9f0 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65  ){ scale *= 1.0e
aca00 2b 31 36 3b 20 65 76 61 6c 20 2d 3d 20 31 36 3b  +16; eval -= 16;
aca10 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76   }.    while( ev
aca20 61 6c 3e 3d 34 20 29 7b 20 73 63 61 6c 65 20 2a  al>=4 ){ scale *
aca30 3d 20 31 2e 30 65 2b 34 3b 20 65 76 61 6c 20 2d  = 1.0e+4; eval -
aca40 3d 20 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65  = 4; }.    while
aca50 28 20 65 76 61 6c 3e 3d 31 20 29 7b 20 73 63 61  ( eval>=1 ){ sca
aca60 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 76  le *= 1.0e+1; ev
aca70 61 6c 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 69  al -= 1; }.    i
aca80 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20  f( esign<0 ){.  
aca90 20 20 20 20 76 31 20 2f 3d 20 73 63 61 6c 65 3b      v1 /= scale;
acaa0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
acab0 20 20 76 31 20 2a 3d 20 73 63 61 6c 65 3b 0a 20    v1 *= scale;. 
acac0 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73     }.  }.  *pRes
acad0 75 6c 74 20 3d 20 28 64 6f 75 62 6c 65 29 28 73  ult = (double)(s
acae0 69 67 6e 3c 30 20 3f 20 2d 76 31 20 3a 20 76 31  ign<0 ? -v1 : v1
acaf0 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74  );.  return (int
acb00 29 28 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a 23  )(z - zBegin);.#
acb10 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73 71  else.  return sq
acb20 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 70  lite3Atoi64(z, p
acb30 52 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 20  Result);.#endif 
acb40 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  /* SQLITE_OMIT_F
acb50 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f  LOATING_POINT */
acb60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72  .}../*.** Compar
acb70 65 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 74  e the 19-charact
acb80 65 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61  er string zNum a
acb90 67 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 20  gainst the text 
acba0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a  representation.*
acbb0 2a 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39  * value 2^63:  9
acbc0 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
acbd0 30 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61  08.  Return nega
acbe0 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70  tive, zero, or p
acbf0 6f 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e  ositive.** if zN
acc00 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  um is less than,
acc10 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72   equal to, or gr
acc20 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73  eater than the s
acc30 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c  tring..**.** Unl
acc40 69 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 69  ike memcmp() thi
acc50 73 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75 61  s routine is gua
acc60 72 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75 72  ranteed to retur
acc70 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65  n the difference
acc80 0a 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 65  .** in the value
acc90 73 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64 69  s of the last di
acca0 67 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 20  git if the only 
accb0 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 6e  difference is in
accc0 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 67   the.** last dig
accd0 69 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61  it.  So, for exa
acce0 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mple,.**.**     
accf0 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22   compare2pow63("
acd00 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
acd10 38 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c  800").**.** will
acd20 20 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73   return -8..*/.s
acd30 74 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 72  tatic int compar
acd40 65 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 68  e2pow63(const ch
acd50 61 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74  ar *zNum){.  int
acd60 20 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70   c;.  c = memcmp
acd70 28 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 33  (zNum,"922337203
acd80 36 38 35 34 37 37 35 38 30 22 2c 31 38 29 3b 0a  685477580",18);.
acd90 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20    if( c==0 ){.  
acda0 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d    c = zNum[18] -
acdb0 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74 75   '8';.  }.  retu
acdc0 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn c;.}.../*.** 
acdd0 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a  Return TRUE if z
acde0 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 74 20  Num is a 64-bit 
acdf0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61  signed integer a
ace00 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20  nd write.** the 
ace10 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74  value of the int
ace20 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e  eger into *pNum.
ace30 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f 74    If zNum is not
ace40 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 6f   an integer.** o
ace50 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  r is an integer 
ace60 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72 67  that is too larg
ace70 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65  e to be expresse
ace80 64 20 77 69 74 68 20 36 34 20 62 69 74 73 2c 0a  d with 64 bits,.
ace90 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66  ** then return f
acea0 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  alse..**.** When
aceb0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
acec0 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72 69  s originally wri
aced0 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 77 69  tten it dealt wi
acee0 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69  th only.** 32-bi
acef0 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 20 74  t numbers.  At t
acf00 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 61 73  hat time, it was
acf10 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 61   much faster tha
acf20 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29 20  n the.** atoi() 
acf30 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65 20  library routine 
acf40 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e 0a 2a  in RedHat 7.2..*
acf50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
acf60 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69   int sqlite3Atoi
acf70 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  64(const char *z
acf80 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29 7b  Num, i64 *pNum){
acf90 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 20  .  i64 v = 0;.  
acfa0 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20 69  int neg;.  int i
acfb0 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  , c;.  const cha
acfc0 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68 69  r *zStart;.  whi
acfd0 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61  le( sqlite3Isspa
acfe0 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d  ce(*zNum) ) zNum
acff0 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d  ++;.  if( *zNum=
ad000 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20  ='-' ){.    neg 
ad010 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b  = 1;.    zNum++;
ad020 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e  .  }else if( *zN
ad030 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 6e  um=='+' ){.    n
ad040 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 6d  eg = 0;.    zNum
ad050 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ++;.  }else{.   
ad060 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20   neg = 0;.  }.  
ad070 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20  zStart = zNum;. 
ad080 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d   while( zNum[0]=
ad090 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20  ='0' ){ zNum++; 
ad0a0 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 6c  } /* Skip over l
ad0b0 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54 69  eading zeros. Ti
ad0c0 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20  cket #2454 */.  
ad0d0 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d  for(i=0; (c=zNum
ad0e0 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d  [i])>='0' && c<=
ad0f0 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76  '9'; i++){.    v
ad100 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30   = v*10 + c - '0
ad110 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d  ';.  }.  *pNum =
ad120 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a 20   neg ? -v : v;. 
ad130 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 69 3d   if( c!=0 || (i=
ad140 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a 4e  =0 && zStart==zN
ad150 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a 20  um) || i>19 ){. 
ad160 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 6d     /* zNum is em
ad170 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20  pty or contains 
ad180 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78 74  non-numeric text
ad190 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20 20   or is longer.  
ad1a0 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 69 67    ** than 19 dig
ad1b0 69 74 73 20 28 74 68 75 73 20 67 75 61 72 61 6e  its (thus guaran
ad1c0 74 69 6e 67 20 74 68 61 74 20 69 74 20 69 73 20  ting that it is 
ad1d0 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20 20  too large) */.  
ad1e0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
ad1f0 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29 7b 0a  lse if( i<19 ){.
ad200 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e      /* Less than
ad210 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f 20 77   19 digits, so w
ad220 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 20 66  e know that it f
ad230 69 74 73 20 69 6e 20 36 34 20 62 69 74 73 20 2a  its in 64 bits *
ad240 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  /.    return 1;.
ad250 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
ad260 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 73  19-digit numbers
ad270 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 72 67   must be no larg
ad280 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 32 30  er than 92233720
ad290 33 36 38 35 34 37 37 35 38 30 37 20 69 66 20 70  36854775807 if p
ad2a0 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a 20 6f  ositive.    ** o
ad2b0 72 20 39 32 32 33 33 37 32 30 33 36 38 35 34 37  r 92233720368547
ad2c0 37 35 38 30 38 20 69 66 20 6e 65 67 61 74 69 76  75808 if negativ
ad2d0 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 39 32  e.  Note that 92
ad2e0 32 33 33 37 32 30 33 36 38 35 34 36 36 35 38 30  2337203685466580
ad2f0 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 36 33  8.    ** is 2^63
ad300 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
ad310 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e  compare2pow63(zN
ad320 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a  um)<neg;.  }.}..
ad330 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67  /*.** The string
ad340 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73   zNum represents
ad350 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e 74   an unsigned int
ad360 65 67 65 72 2e 20 20 54 68 65 72 65 20 6d 69 67  eger.  There mig
ad370 68 74 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72  ht be some other
ad380 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
ad390 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 69 6e  following the in
ad3a0 74 65 67 65 72 20 74 6f 6f 2c 20 62 75 74 20 74  teger too, but t
ad3b0 68 61 74 20 70 61 72 74 20 69 73 20 69 67 6e 6f  hat part is igno
ad3c0 72 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69  red..** If the i
ad3d0 6e 74 65 67 65 72 20 74 68 61 74 20 74 68 65 20  nteger that the 
ad3e0 70 72 65 66 69 78 20 6f 66 20 7a 4e 75 6d 20 72  prefix of zNum r
ad3f0 65 70 72 65 73 65 6e 74 73 20 77 69 6c 6c 20 66  epresents will f
ad400 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34 2d 62 69  it in a.** 64-bi
ad410 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
ad420 2c 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20  , return TRUE.  
ad430 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e  Otherwise return
ad440 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20 49 66   FALSE..**.** If
ad450 20 74 68 65 20 6e 65 67 46 6c 61 67 20 70 61 72   the negFlag par
ad460 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 2c 20  ameter is true, 
ad470 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  that means that 
ad480 7a 4e 75 6d 20 72 65 61 6c 6c 79 20 72 65 70 72  zNum really repr
ad490 65 73 65 6e 74 73 0a 2a 2a 20 61 20 6e 65 67 61  esents.** a nega
ad4a0 74 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 28 54  tive number.  (T
ad4b0 68 65 20 6c 65 61 64 69 6e 67 20 22 2d 22 20 69  he leading "-" i
ad4c0 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 7a  s omitted from z
ad4d0 4e 75 6d 2e 29 20 20 54 68 69 73 0a 2a 2a 20 70  Num.)  This.** p
ad4e0 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 65 64  arameter is need
ad4f0 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
ad500 61 20 62 6f 75 6e 64 61 72 79 20 63 61 73 65 2e  a boundary case.
ad510 20 20 41 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 66    A string.** of
ad520 20 22 39 32 32 33 33 37 33 30 33 36 38 35 34 37   "92233730368547
ad530 37 35 38 30 38 22 20 72 65 74 75 72 6e 73 20 66  75808" returns f
ad540 61 6c 73 65 20 69 66 20 6e 65 67 46 6c 61 67 20  alse if negFlag 
ad550 69 73 20 66 61 6c 73 65 20 6f 72 20 74 72 75 65  is false or true
ad560 0a 2a 2a 20 69 66 20 6e 65 67 46 6c 61 67 20 69  .** if negFlag i
ad570 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65  s true..**.** Le
ad580 61 64 69 6e 67 20 7a 65 72 6f 73 20 61 72 65 20  ading zeros are 
ad590 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ignored..*/.SQLI
ad5a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ad5b0 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69  qlite3FitsIn64Bi
ad5c0 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ts(const char *z
ad5d0 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67  Num, int negFlag
ad5e0 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20  ){.  int i, c;. 
ad5f0 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 0a 20   int neg = 0;.. 
ad600 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 30 5d   assert( zNum[0]
ad610 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 30 5d  >='0' && zNum[0]
ad620 3c 3d 27 39 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d  <='9' ); /* zNum
ad630 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20   is an unsigned 
ad640 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 69 66 28  number */..  if(
ad650 20 6e 65 67 46 6c 61 67 20 29 20 6e 65 67 20 3d   negFlag ) neg =
ad660 20 31 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65 28   1-neg;.  while(
ad670 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a 20   *zNum=='0' ){. 
ad680 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20     zNum++;   /* 
ad690 53 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72  Skip leading zer
ad6a0 6f 73 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35  os.  Ticket #245
ad6b0 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69  4 */.  }.  for(i
ad6c0 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e  =0; (c=zNum[i])>
ad6d0 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 3b 20  ='0' && c<='9'; 
ad6e0 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 3c 31  i++){}.  if( i<1
ad6f0 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72  9 ){.    /* Guar
ad700 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 69 66  anteed to fit if
ad710 20 6c 65 73 73 20 74 68 61 6e 20 31 39 20 64 69   less than 19 di
ad720 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75  gits */.    retu
ad730 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
ad740 28 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a  ( i>19 ){.    /*
ad750 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62   Guaranteed to b
ad760 65 20 74 6f 6f 20 62 69 67 20 69 66 20 67 72 65  e too big if gre
ad770 61 74 65 72 20 74 68 61 6e 20 31 39 20 64 69 67  ater than 19 dig
ad780 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  its */.    retur
ad790 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  n 0;.  }else{.  
ad7a0 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20 61 67 61    /* Compare aga
ad7b0 69 6e 73 74 20 32 5e 36 33 2e 20 2a 2f 0a 20 20  inst 2^63. */.  
ad7c0 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65    return compare
ad7d0 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67  2pow63(zNum)<neg
ad7e0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
ad7f0 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74  f zNum represent
ad800 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  s an integer tha
ad810 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 33 32  t will fit in 32
ad820 2d 62 69 74 73 2c 20 74 68 65 6e 20 73 65 74 0a  -bits, then set.
ad830 2a 2a 20 2a 70 56 61 6c 75 65 20 74 6f 20 74 68  ** *pValue to th
ad840 61 74 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72  at integer and r
ad850 65 74 75 72 6e 20 74 72 75 65 2e 20 20 4f 74 68  eturn true.  Oth
ad860 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 66 61  erwise return fa
ad870 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e  lse..**.** Any n
ad880 6f 6e 2d 6e 75 6d 65 72 69 63 20 63 68 61 72 61  on-numeric chara
ad890 63 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f  cters that follo
ad8a0 77 69 6e 67 20 7a 4e 75 6d 20 61 72 65 20 69 67  wing zNum are ig
ad8b0 6e 6f 72 65 64 2e 0a 2a 2a 20 54 68 69 73 20 69  nored..** This i
ad8c0 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
ad8d0 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 29   sqlite3Atoi64()
ad8e0 20 77 68 69 63 68 20 72 65 71 75 69 72 65 73 20   which requires 
ad8f0 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 6e 75 6d  the.** input num
ad900 62 65 72 20 74 6f 20 62 65 20 7a 65 72 6f 2d 74  ber to be zero-t
ad910 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51  erminated..*/.SQ
ad920 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ad930 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32   sqlite3GetInt32
ad940 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75  (const char *zNu
ad950 6d 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b  m, int *pValue){
ad960 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
ad970 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c 20  v = 0;.  int i, 
ad980 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30  c;.  int neg = 0
ad990 3b 0a 20 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d  ;.  if( zNum[0]=
ad9a0 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20  ='-' ){.    neg 
ad9b0 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b  = 1;.    zNum++;
ad9c0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 4e 75  .  }else if( zNu
ad9d0 6d 5b 30 5d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20  m[0]=='+' ){.   
ad9e0 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77   zNum++;.  }.  w
ad9f0 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27  hile( zNum[0]=='
ada00 30 27 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66  0' ) zNum++;.  f
ada10 6f 72 28 69 3d 30 3b 20 69 3c 31 31 20 26 26 20  or(i=0; i<11 && 
ada20 28 63 20 3d 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27  (c = zNum[i] - '
ada30 30 27 29 3e 3d 30 20 26 26 20 63 3c 3d 39 3b 20  0')>=0 && c<=9; 
ada40 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a  i++){.    v = v*
ada50 31 30 20 2b 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f  10 + c;.  }..  /
ada60 2a 20 54 68 65 20 6c 6f 6e 67 65 73 74 20 64 65  * The longest de
ada70 63 69 6d 61 6c 20 72 65 70 72 65 73 65 6e 74 61  cimal representa
ada80 74 69 6f 6e 20 6f 66 20 61 20 33 32 20 62 69 74  tion of a 32 bit
ada90 20 69 6e 74 65 67 65 72 20 69 73 20 31 30 20 64   integer is 10 d
adaa0 69 67 69 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  igits:.  **.  **
adab0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33               123
adac0 34 35 36 37 38 39 30 0a 20 20 2a 2a 20 20 20 20  4567890.  **    
adad0 20 32 5e 33 31 20 2d 3e 20 32 31 34 37 34 38 33   2^31 -> 2147483
adae0 36 34 38 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  648.  */.  if( i
adaf0 3e 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  >10 ){.    retur
adb00 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76  n 0;.  }.  if( v
adb10 2d 6e 65 67 3e 32 31 34 37 34 38 33 36 34 37 20  -neg>2147483647 
adb20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
adb30 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 20 29  .  }.  if( neg )
adb40 7b 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a 20 20  {.    v = -v;.  
adb50 7d 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20 28 69  }.  *pValue = (i
adb60 6e 74 29 76 3b 0a 20 20 72 65 74 75 72 6e 20 31  nt)v;.  return 1
adb70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76  ;.}../*.** The v
adb80 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
adb90 6e 74 65 67 65 72 20 65 6e 63 6f 64 69 6e 67 20  nteger encoding 
adba0 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  is as follows:.*
adbb0 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20  *.** KEY:.**    
adbc0 20 20 20 20 20 41 20 3d 20 30 78 78 78 78 78 78       A = 0xxxxxx
adbd0 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64  x    7 bits of d
adbe0 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67  ata and one flag
adbf0 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   bit.**         
adc00 42 20 3d 20 31 78 78 78 78 78 78 78 20 20 20 20  B = 1xxxxxxx    
adc10 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61  7 bits of data a
adc20 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a  nd one flag bit.
adc30 2a 2a 20 20 20 20 20 20 20 20 20 43 20 3d 20 78  **         C = x
adc40 78 78 78 78 78 78 78 20 20 20 20 38 20 62 69 74  xxxxxxx    8 bit
adc50 73 20 6f 66 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20  s of data.**.** 
adc60 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31   7 bits - A.** 1
adc70 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32  4 bits - BA.** 2
adc80 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20  1 bits - BBA.** 
adc90 32 38 20 62 69 74 73 20 2d 20 42 42 42 41 0a 2a  28 bits - BBBA.*
adca0 2a 20 33 35 20 62 69 74 73 20 2d 20 42 42 42 42  * 35 bits - BBBB
adcb0 41 0a 2a 2a 20 34 32 20 62 69 74 73 20 2d 20 42  A.** 42 bits - B
adcc0 42 42 42 42 41 0a 2a 2a 20 34 39 20 62 69 74 73  BBBBA.** 49 bits
adcd0 20 2d 20 42 42 42 42 42 42 41 0a 2a 2a 20 35 36   - BBBBBBA.** 56
adce0 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 41   bits - BBBBBBBA
adcf0 0a 2a 2a 20 36 34 20 62 69 74 73 20 2d 20 42 42  .** 64 bits - BB
add00 42 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a  BBBBBBC.*/../*.*
add10 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69 74  * Write a 64-bit
add20 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68   variable-length
add30 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d 6f   integer to memo
add40 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ry starting at p
add50 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e 67  [0]..** The leng
add60 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74 65  th of data write
add70 20 77 69 6c 6c 20 62 65 20 62 65 74 77 65 65 6e   will be between
add80 20 31 20 61 6e 64 20 39 20 62 79 74 65 73 2e 20   1 and 9 bytes. 
add90 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f   The number.** o
adda0 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20  f bytes written 
addb0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
addc0 2a 2a 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65  ** A variable-le
addd0 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63 6f 6e  ngth integer con
adde0 73 69 73 74 73 20 6f 66 20 74 68 65 20 6c 6f 77  sists of the low
addf0 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 63  er 7 bits of eac
ade00 68 20 62 79 74 65 0a 2a 2a 20 66 6f 72 20 61 6c  h byte.** for al
ade10 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76  l bytes that hav
ade20 65 20 74 68 65 20 38 74 68 20 62 69 74 20 73 65  e the 8th bit se
ade30 74 20 61 6e 64 20 6f 6e 65 20 62 79 74 65 20 77  t and one byte w
ade40 69 74 68 20 74 68 65 20 38 74 68 0a 2a 2a 20 62  ith the 8th.** b
ade50 69 74 20 63 6c 65 61 72 2e 20 20 45 78 63 65 70  it clear.  Excep
ade60 74 2c 20 69 66 20 77 65 20 67 65 74 20 74 6f 20  t, if we get to 
ade70 74 68 65 20 39 74 68 20 62 79 74 65 2c 20 69 74  the 9th byte, it
ade80 20 73 74 6f 72 65 73 20 74 68 65 20 66 75 6c 6c   stores the full
ade90 0a 2a 2a 20 38 20 62 69 74 73 20 61 6e 64 20 69  .** 8 bits and i
adea0 73 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 2e  s the last byte.
adeb0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
adec0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75  TE int sqlite3Pu
aded0 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64  tVarint(unsigned
adee0 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 76 29   char *p, u64 v)
adef0 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b  {.  int i, j, n;
adf00 0a 20 20 75 38 20 62 75 66 5b 31 30 5d 3b 0a 20  .  u8 buf[10];. 
adf10 20 69 66 28 20 76 20 26 20 28 28 28 75 36 34 29   if( v & (((u64)
adf20 30 78 66 66 30 30 30 30 30 30 29 3c 3c 33 32 29  0xff000000)<<32)
adf30 20 29 7b 0a 20 20 20 20 70 5b 38 5d 20 3d 20 28   ){.    p[8] = (
adf40 75 38 29 76 3b 0a 20 20 20 20 76 20 3e 3e 3d 20  u8)v;.    v >>= 
adf50 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d 37 3b 20  8;.    for(i=7; 
adf60 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  i>=0; i--){.    
adf70 20 20 70 5b 69 5d 20 3d 20 28 75 38 29 28 28 76    p[i] = (u8)((v
adf80 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 29   & 0x7f) | 0x80)
adf90 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37 3b  ;.      v >>= 7;
adfa0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
adfb0 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20 6e  n 9;.  }    .  n
adfc0 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20   = 0;.  do{.    
adfd0 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 38 29 28  buf[n++] = (u8)(
adfe0 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 38  (v & 0x7f) | 0x8
adff0 30 29 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b  0);.    v >>= 7;
ae000 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 20  .  }while( v!=0 
ae010 29 3b 0a 20 20 62 75 66 5b 30 5d 20 26 3d 20 30  );.  buf[0] &= 0
ae020 78 37 66 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  x7f;.  assert( n
ae030 3c 3d 39 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  <=9 );.  for(i=0
ae040 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a  , j=n-1; j>=0; j
ae050 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 70 5b  --, i++){.    p[
ae060 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d  i] = buf[j];.  }
ae070 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
ae080 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
ae090 6e 65 20 69 73 20 61 20 66 61 73 74 65 72 20 76  ne is a faster v
ae0a0 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  ersion of sqlite
ae0b0 33 50 75 74 56 61 72 69 6e 74 28 29 20 74 68 61  3PutVarint() tha
ae0c0 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20  t only.** works 
ae0d0 66 6f 72 20 33 32 2d 62 69 74 20 70 6f 73 69 74  for 32-bit posit
ae0e0 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64  ive integers and
ae0f0 20 77 68 69 63 68 20 69 73 20 6f 70 74 69 6d 69   which is optimi
ae100 7a 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63  zed for.** the c
ae110 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 73 6d  ommon case of sm
ae120 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 20 20 41  all integers.  A
ae130 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20   MACRO version, 
ae140 70 75 74 56 61 72 69 6e 74 33 32 2c 0a 2a 2a 20  putVarint32,.** 
ae150 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63  is provided whic
ae160 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 73 69  h inlines the si
ae170 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20  ngle-byte case. 
ae180 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64   All code should
ae190 20 75 73 65 0a 2a 2a 20 74 68 65 20 4d 41 43 52   use.** the MACR
ae1a0 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 74 68 69  O version as thi
ae1b0 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
ae1c0 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79  es the single-by
ae1d0 74 65 20 63 61 73 65 20 68 61 73 0a 2a 2a 20 61  te case has.** a
ae1e0 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64  lready been hand
ae1f0 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  led..*/.SQLITE_P
ae200 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ae210 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e  e3PutVarint32(un
ae220 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20  signed char *p, 
ae230 75 33 32 20 76 29 7b 0a 23 69 66 6e 64 65 66 20  u32 v){.#ifndef 
ae240 70 75 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66  putVarint32.  if
ae250 28 20 28 76 20 26 20 7e 30 78 37 66 29 3d 3d 30  ( (v & ~0x7f)==0
ae260 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 76   ){.    p[0] = v
ae270 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
ae280 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
ae290 20 28 76 20 26 20 7e 30 78 33 66 66 66 29 3d 3d   (v & ~0x3fff)==
ae2a0 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20  0 ){.    p[0] = 
ae2b0 28 75 38 29 28 28 76 3e 3e 37 29 20 7c 20 30 78  (u8)((v>>7) | 0x
ae2c0 38 30 29 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 20  80);.    p[1] = 
ae2d0 28 75 38 29 28 76 20 26 20 30 78 37 66 29 3b 0a  (u8)(v & 0x7f);.
ae2e0 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
ae2f0 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
ae300 65 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 76  e3PutVarint(p, v
ae310 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  );.}../*.** Read
ae320 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62   a 64-bit variab
ae330 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
ae340 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74  r from memory st
ae350 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a  arting at p[0]..
ae360 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
ae370 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
ae380 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69  ad.  The value i
ae390 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a  s stored in *v..
ae3a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
ae3b0 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56  E u8 sqlite3GetV
ae3c0 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69  arint(const unsi
ae3d0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36  gned char *p, u6
ae3e0 34 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 62  4 *v){.  u32 a,b
ae3f0 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a 20  ,s;..  a = *p;. 
ae400 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73   /* a: p0 (unmas
ae410 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
ae420 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  a&0x80)).  {.   
ae430 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74   *v = a;.    ret
ae440 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b  urn 1;.  }..  p+
ae450 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f  +;.  b = *p;.  /
ae460 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65  * b: p1 (unmaske
ae470 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26  d) */.  if (!(b&
ae480 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61  0x80)).  {.    a
ae490 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20   &= 0x7f;.    a 
ae4a0 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = a<<7;.    a |=
ae4b0 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a   b;.    *v = a;.
ae4c0 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
ae4d0 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20  }..  p++;.  a = 
ae4e0 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70  a<<14;.  a |= *p
ae4f0 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34  ;.  /* a: p0<<14
ae500 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29   | p2 (unmasked)
ae510 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
ae520 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26  80)).  {.    a &
ae530 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
ae540 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78  7f);.    b &= 0x
ae550 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37  7f;.    b = b<<7
ae560 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
ae570 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
ae580 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 3;.  }..  /
ae590 2a 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c 6f  * CSE1 from belo
ae5a0 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 37  w */.  a &= (0x7
ae5b0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
ae5c0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31   p++;.  b = b<<1
ae5d0 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  b |= *p;.  
ae5e0 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70  /* b: p1<<14 | p
ae5f0 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  3 (unmasked) */.
ae600 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
ae610 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30  .  {.    b &= (0
ae620 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
ae630 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53  .    /* moved CS
ae640 45 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20  E1 up */.    /* 
ae650 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
ae660 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61  (0x7f); */.    a
ae670 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = a<<7;.    a |
ae680 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b  = b;.    *v = a;
ae690 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20  .    return 4;. 
ae6a0 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c   }..  /* a: p0<<
ae6b0 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29  14 | p2 (masked)
ae6c0 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c   */.  /* b: p1<<
ae6d0 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65  14 | p3 (unmaske
ae6e0 64 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61 76  d) */.  /* 1:sav
ae6f0 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70  e off p0<<21 | p
ae700 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20  1<<14 | p2<<7 | 
ae710 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20  p3 (masked) */. 
ae720 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75   /* moved CSE1 u
ae730 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28  p */.  /* a &= (
ae740 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
ae750 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 37  ; */.  b &= (0x7
ae760 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
ae770 20 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a 20   s = a;.  /* s: 
ae780 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 73  p0<<14 | p2 (mas
ae790 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a  ked) */..  p++;.
ae7a0 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61    a = a<<14;.  a
ae7b0 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20   |= *p;.  /* a: 
ae7c0 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20  p0<<28 | p2<<14 
ae7d0 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p4 (unmasked) 
ae7e0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  */.  if (!(a&0x8
ae7f0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77  0)).  {.    /* w
ae800 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73 65  e can skip these
ae810 20 63 61 75 73 65 20 74 68 65 79 20 77 65 72 65   cause they were
ae820 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64   (effectively) d
ae830 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c  one above in cal
ae840 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f  c'ing s */.    /
ae850 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38  * a &= (0x7f<<28
ae860 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  )|(0x7f<<14)|(0x
ae870 37 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20 62  7f); */.    /* b
ae880 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
ae890 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62 20  0x7f); */.    b 
ae8a0 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = b<<7;.    a |=
ae8b0 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31   b;.    s = s>>1
ae8c0 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36  8;.    *v = ((u6
ae8d0 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20  4)s)<<32 | a;.  
ae8e0 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a    return 5;.  }.
ae8f0 0a 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66 66  .  /* 2:save off
ae900 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34   p0<<21 | p1<<14
ae910 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d   | p2<<7 | p3 (m
ae920 61 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d 20  asked) */.  s = 
ae930 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a  s<<7;.  s |= b;.
ae940 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20 7c    /* s: p0<<21 |
ae950 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20   p1<<14 | p2<<7 
ae960 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f  | p3 (masked) */
ae970 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62  ..  p++;.  b = b
ae980 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b  <<14;.  b |= *p;
ae990 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38 20  .  /* b: p1<<28 
ae9a0 7c 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28 75  | p3<<14 | p5 (u
ae9b0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
ae9c0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b   (!(b&0x80)).  {
ae9d0 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73  .    /* we can s
ae9e0 6b 69 70 20 74 68 69 73 20 63 61 75 73 65 20 69  kip this cause i
ae9f0 74 20 77 61 73 20 28 65 66 66 65 63 74 69 76 65  t was (effective
aea00 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69  ly) done above i
aea10 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a  n calc'ing s */.
aea20 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 37      /* b &= (0x7
aea30 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34  f<<28)|(0x7f<<14
aea40 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20  )|(0x7f); */.   
aea50 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
aea60 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d  |(0x7f);.    a =
aea70 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20   a<<7;.    a |= 
aea80 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 38  b;.    s = s>>18
aea90 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34  ;.    *v = ((u64
aeaa0 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20  )s)<<32 | a;.   
aeab0 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a   return 6;.  }..
aeac0 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c    p++;.  a = a<<
aead0 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20  14;.  a |= *p;. 
aeae0 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c 20   /* a: p2<<28 | 
aeaf0 70 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e 6d  p4<<14 | p6 (unm
aeb00 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
aeb10 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(a&0x80)).  {. 
aeb20 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32     a &= (0x1f<<2
aeb30 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
aeb40 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28  x7f);.    b &= (
aeb50 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
aeb60 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a  ;.    b = b<<7;.
aeb70 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20      a |= b;.    
aeb80 73 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20 2a  s = s>>11;.    *
aeb90 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32  v = ((u64)s)<<32
aeba0 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e   | a;.    return
aebb0 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53   7;.  }..  /* CS
aebc0 45 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f  E2 from below */
aebd0 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31  .  a &= (0x7f<<1
aebe0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b  4)|(0x7f);.  p++
aebf0 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20  ;.  b = b<<14;. 
aec00 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62   b |= *p;.  /* b
aec10 3a 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c 31  : p3<<28 | p5<<1
aec20 34 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 64  4 | p7 (unmasked
aec30 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30  ) */.  if (!(b&0
aec40 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20  x80)).  {.    b 
aec50 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30  &= (0x1f<<28)|(0
aec60 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
aec70 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53  .    /* moved CS
aec80 45 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20  E2 up */.    /* 
aec90 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
aeca0 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61  (0x7f); */.    a
aecb0 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = a<<7;.    a |
aecc0 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e  = b;.    s = s>>
aecd0 34 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36  4;.    *v = ((u6
aece0 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20  4)s)<<32 | a;.  
aecf0 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d 0a    return 8;.  }.
aed00 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c  .  p++;.  a = a<
aed10 3c 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a  <15;.  a |= *p;.
aed20 20 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20 7c    /* a: p4<<29 |
aed30 20 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75 6e   p6<<15 | p8 (un
aed40 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a  masked) */..  /*
aed50 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a   moved CSE2 up *
aed60 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37  /.  /* a &= (0x7
aed70 66 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31 35  f<<29)|(0x7f<<15
aed80 29 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20 62  )|(0xff); */.  b
aed90 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
aeda0 30 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c 3c  0x7f);.  b = b<<
aedb0 38 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20 20  8;.  a |= b;..  
aedc0 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d 20  s = s<<4;.  b = 
aedd0 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30 78  p[-4];.  b &= 0x
aede0 37 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b 0a  7f;.  b = b>>3;.
aedf0 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20    s |= b;..  *v 
aee00 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c  = ((u64)s)<<32 |
aee10 20 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39 3b   a;..  return 9;
aee20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61  .}../*.** Read a
aee30 20 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c 65   32-bit variable
aee40 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20  -length integer 
aee50 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72  from memory star
aee60 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a  ting at p[0]..**
aee70 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
aee80 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64  er of bytes read
aee90 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20  .  The value is 
aeea0 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a  stored in *v..**
aeeb0 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e   A MACRO version
aeec0 2c 20 67 65 74 56 61 72 69 6e 74 33 32 2c 20 69  , getVarint32, i
aeed0 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 68  s provided which
aeee0 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a   inlines the .**
aeef0 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73   single-byte cas
aef00 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f  e.  All code sho
aef10 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 52  uld use the MACR
aef20 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a  O version as .**
aef30 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
aef40 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c  ssumes the singl
aef50 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 20  e-byte case has 
aef60 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e  already been han
aef70 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dled..*/.SQLITE_
aef80 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74  PRIVATE u8 sqlit
aef90 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f  e3GetVarint32(co
aefa0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
aefb0 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20  r *p, u32 *v){. 
aefc0 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20   u32 a,b;..  /* 
aefd0 54 68 65 20 31 2d 62 79 74 65 20 63 61 73 65 2e  The 1-byte case.
aefe0 20 20 4f 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79    Overwhelmingly
aeff0 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e   the most common
af000 2e 20 20 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e  .  Handled inlin
af010 65 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 67 65  e.  ** by the ge
af020 74 56 61 72 69 6e 33 32 28 29 20 6d 61 63 72 6f  tVarin32() macro
af030 20 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20   */.  a = *p;.  
af040 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b  /* a: p0 (unmask
af050 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67  ed) */.#ifndef g
af060 65 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 20  etVarint32.  if 
af070 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(a&0x80)).  {.
af080 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65      /* Values be
af090 74 77 65 65 6e 20 30 20 61 6e 64 20 31 32 37 20  tween 0 and 127 
af0a0 2a 2f 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20  */.    *v = a;. 
af0b0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
af0c0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68  .#endif..  /* Th
af0d0 65 20 32 2d 62 79 74 65 20 63 61 73 65 20 2a 2f  e 2-byte case */
af0e0 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70  .  p++;.  b = *p
af0f0 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e  ;.  /* b: p1 (un
af100 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20  masked) */.  if 
af110 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(b&0x80)).  {.
af120 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65      /* Values be
af130 74 77 65 65 6e 20 31 32 38 20 61 6e 64 20 31 36  tween 128 and 16
af140 33 38 33 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20  383 */.    a &= 
af150 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c  0x7f;.    a = a<
af160 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c  <7;.    *v = a |
af170 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   b;.    return 2
af180 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
af190 33 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20  3-byte case */. 
af1a0 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31   p++;.  a = a<<1
af1b0 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  a |= *p;.  
af1c0 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70  /* a: p0<<14 | p
af1d0 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  2 (unmasked) */.
af1e0 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29    if (!(a&0x80))
af1f0 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75  .  {.    /* Valu
af200 65 73 20 62 65 74 77 65 65 6e 20 31 36 33 38 34  es between 16384
af210 20 61 6e 64 20 32 30 39 37 31 35 31 20 2a 2f 0a   and 2097151 */.
af220 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c      a &= (0x7f<<
af230 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
af240 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62  b &= 0x7f;.    b
af250 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20   = b<<7;.    *v 
af260 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74  = a | b;.    ret
af270 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 3;.  }..  /*
af280 20 41 20 33 32 2d 62 69 74 20 76 61 72 69 6e 74   A 32-bit varint
af290 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72   is used to stor
af2a0 65 20 73 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69  e size informati
af2b0 6f 6e 20 69 6e 20 62 74 72 65 65 73 2e 0a 20 20  on in btrees..  
af2c0 2a 2a 20 4f 62 6a 65 63 74 73 20 61 72 65 20 72  ** Objects are r
af2d0 61 72 65 6c 79 20 6c 61 72 67 65 72 20 74 68 61  arely larger tha
af2e0 6e 20 32 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20  n 2MiB limit of 
af2f0 61 20 33 2d 62 79 74 65 20 76 61 72 69 6e 74 2e  a 3-byte varint.
af300 0a 20 20 2a 2a 20 41 20 33 2d 62 79 74 65 20 76  .  ** A 3-byte v
af310 61 72 69 6e 74 20 69 73 20 73 75 66 66 69 63 69  arint is suffici
af320 65 6e 74 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ent, for example
af330 2c 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20  , to record the 
af340 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31  size.  ** of a 1
af350 30 34 38 35 36 39 2d 62 79 74 65 20 42 4c 4f 42  048569-byte BLOB
af360 20 6f 72 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a   or string..  **
af370 0a 20 20 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e  .  ** We only un
af380 72 6f 6c 6c 20 74 68 65 20 66 69 72 73 74 20 31  roll the first 1
af390 2d 2c 20 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79  -, 2-, and 3- by
af3a0 74 65 20 63 61 73 65 73 2e 20 20 54 68 65 20 76  te cases.  The v
af3b0 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61  ery.  ** rare la
af3c0 72 67 65 72 20 63 61 73 65 73 20 63 61 6e 20 62  rger cases can b
af3d0 65 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65  e handled by the
af3e0 20 73 6c 6f 77 65 72 20 36 34 2d 62 69 74 20 76   slower 64-bit v
af3f0 61 72 69 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69  arint.  ** routi
af400 6e 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20  ne..  */.#if 1. 
af410 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0a   {.    u64 v64;.
af420 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70      u8 n;..    p
af430 20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73   -= 2;.    n = s
af440 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28  qlite3GetVarint(
af450 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61 73  p, &v64);.    as
af460 73 65 72 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d  sert( n>3 && n<=
af470 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 28 75  9 );.    *v = (u
af480 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 74 75  32)v64;.    retu
af490 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65  rn n;.  }..#else
af4a0 0a 20 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77  .  /* For follow
af4b0 69 6e 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66  ing code (kept f
af4c0 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65  or historical re
af4d0 63 6f 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73  cord only) shows
af4e0 20 61 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69   an.  ** unrolli
af4f0 6e 67 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e  ng for the 3- an
af500 64 20 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20  d 4-byte varint 
af510 63 61 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64  cases.  This cod
af520 65 20 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74  e is.  ** slight
af530 6c 79 20 66 61 73 74 65 72 2c 20 62 75 74 20 69  ly faster, but i
af540 74 20 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72  t is also larger
af550 20 61 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72   and much harder
af560 20 74 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20   to test..  */. 
af570 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31   p++;.  b = b<<1
af580 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  b |= *p;.  
af590 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70  /* b: p1<<14 | p
af5a0 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  3 (unmasked) */.
af5b0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
af5c0 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75  .  {.    /* Valu
af5d0 65 73 20 62 65 74 77 65 65 6e 20 32 30 39 37 31  es between 20971
af5e0 35 32 20 61 6e 64 20 32 36 38 34 33 35 34 35 35  52 and 268435455
af5f0 20 2a 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78   */.    b &= (0x
af600 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
af610 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c      a &= (0x7f<<
af620 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
af630 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76  a = a<<7;.    *v
af640 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65   = a | b;.    re
af650 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70  turn 4;.  }..  p
af660 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b  ++;.  a = a<<14;
af670 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  a |= *p;.  /*
af680 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c   a: p0<<28 | p2<
af690 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b  <14 | p4 (unmask
af6a0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61  ed) */.  if (!(a
af6b0 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20  &0x80)).  {.    
af6c0 2f 2a 20 57 61 6c 75 65 73 20 20 62 65 74 77 65  /* Walues  betwe
af6d0 65 6e 20 32 36 38 34 33 35 34 35 36 20 61 6e 64  en 268435456 and
af6e0 20 33 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a   34359738367 */.
af6f0 20 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c      a &= (0x1f<<
af700 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28  28)|(0x7f<<14)|(
af710 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20  0x7f);.    b &= 
af720 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66  (0x1f<<28)|(0x7f
af730 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
af740 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
af750 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20  *v = a | b;.    
af760 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20  return 5;.  }.. 
af770 20 2f 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20   /* We can only 
af780 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74  reach this point
af790 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20   when reading a 
af7a0 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
af7b0 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20  .  ** file.  In 
af7c0 74 68 61 74 20 63 61 73 65 20 77 65 20 61 72 65  that case we are
af7d0 20 6e 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72   not in any hurr
af7e0 79 2e 20 20 55 73 65 20 74 68 65 20 28 72 65 6c  y.  Use the (rel
af7f0 61 74 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f  atively.  ** slo
af800 77 29 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f  w) general-purpo
af810 73 65 20 73 71 6c 69 74 65 33 47 65 74 56 61 72  se sqlite3GetVar
af820 69 6e 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f  int() routine to
af830 20 65 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a   extract the.  *
af840 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a  * value. */.  {.
af850 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20      u64 v64;.   
af860 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d   u8 n;..    p -=
af870 20 34 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69   4;.    n = sqli
af880 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20  te3GetVarint(p, 
af890 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72  &v64);.    asser
af8a0 74 28 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29  t( n>5 && n<=9 )
af8b0 3b 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29  ;.    *v = (u32)
af8c0 76 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  v64;.    return 
af8d0 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  n;.  }.#endif.}.
af8e0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
af8f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
af900 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e  s that will be n
af910 65 65 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74  eeded to store t
af920 68 65 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62  he given.** 64-b
af930 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53  it integer..*/.S
af940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
af950 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c  t sqlite3VarintL
af960 65 6e 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74  en(u64 v){.  int
af970 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20   i = 0;.  do{.  
af980 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d    i++;.    v >>=
af990 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21   7;.  }while( v!
af9a0 3d 30 20 26 26 20 41 4c 57 41 59 53 28 69 3c 39  =0 && ALWAYS(i<9
af9b0 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b  ) );.  return i;
af9c0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  .}.../*.** Read 
af9d0 6f 72 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d  or write a four-
af9e0 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
af9f0 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a  integer value..*
afa00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
afa10 20 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34   u32 sqlite3Get4
afa20 62 79 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70  byte(const u8 *p
afa30 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30  ){.  return (p[0
afa40 5d 3c 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c  ]<<24) | (p[1]<<
afa50 31 36 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20  16) | (p[2]<<8) 
afa60 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45  | p[3];.}.SQLITE
afa70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
afa80 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 6e  lite3Put4byte(un
afa90 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20  signed char *p, 
afaa0 75 33 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d  u32 v){.  p[0] =
afab0 20 28 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20   (u8)(v>>24);.  
afac0 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31  p[1] = (u8)(v>>1
afad0 36 29 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38  6);.  p[2] = (u8
afae0 29 28 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20  )(v>>8);.  p[3] 
afaf0 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69  = (u8)v;.}....#i
afb00 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
afb10 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
afb20 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  RAL) || defined(
afb30 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
afb40 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74  )./*.** Translat
afb50 65 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20  e a single byte 
afb60 6f 66 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69  of Hex into an i
afb70 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20  nteger..** This 
afb80 72 6f 75 74 69 6e 65 6e 20 6f 6e 6c 79 20 77 6f  routinen only wo
afb90 72 6b 73 20 69 66 20 68 20 72 65 61 6c 6c 79 20  rks if h really 
afba0 69 73 20 61 20 76 61 6c 69 64 20 68 65 78 61 64  is a valid hexad
afbb0 65 63 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63  ecimal.** charac
afbc0 74 65 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e  ter:  0..9a..fA.
afbd0 2e 46 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20  .F.*/.static u8 
afbe0 68 65 78 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b  hexToInt(int h){
afbf0 0a 20 20 61 73 73 65 72 74 28 20 28 68 3e 3d 27  .  assert( (h>='
afc00 30 27 20 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c  0' && h<='9') ||
afc10 20 20 28 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d    (h>='a' && h<=
afc20 27 66 27 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27  'f') ||  (h>='A'
afc30 20 26 26 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23   && h<='F') );.#
afc40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
afc50 49 49 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 28  II.  h += 9*(1&(
afc60 68 3e 3e 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23  h>>6));.#endif.#
afc70 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
afc80 44 49 43 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26  DIC.  h += 9*(1&
afc90 7e 28 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66  ~(h>>4));.#endif
afca0 0a 20 20 72 65 74 75 72 6e 20 28 75 38 29 28 68  .  return (u8)(h
afcb0 20 26 20 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69   & 0xf);.}.#endi
afcc0 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49  f /* !SQLITE_OMI
afcd0 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c  T_BLOB_LITERAL |
afce0 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  | SQLITE_HAS_COD
afcf0 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69  EC */..#if !defi
afd00 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
afd10 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c  BLOB_LITERAL) ||
afd20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
afd30 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a  HAS_CODEC)./*.**
afd40 20 43 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20   Convert a BLOB 
afd50 6c 69 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66  literal of the f
afd60 6f 72 6d 20 22 78 27 68 68 68 68 68 68 27 22 20  orm "x'hhhhhh'" 
afd70 69 6e 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a  into its binary.
afd80 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72  ** value.  Retur
afd90 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  n a pointer to i
afda0 74 73 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e  ts binary value.
afdb0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
afdc0 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61  the.** binary va
afdd0 6c 75 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74  lue has been obt
afde0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
afdf0 63 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72  c and must be fr
afe00 65 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61  eed by.** the ca
afe10 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a  lling routine..*
afe20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
afe30 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65   void *sqlite3He
afe40 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20  xToBlob(sqlite3 
afe50 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
afe60 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68  *z, int n){.  ch
afe70 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74  ar *zBlob;.  int
afe80 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28   i;..  zBlob = (
afe90 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62  char *)sqlite3Db
afea0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f  MallocRaw(db, n/
afeb0 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20  2 + 1);.  n--;. 
afec0 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20   if( zBlob ){.  
afed0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20    for(i=0; i<n; 
afee0 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c  i+=2){.      zBl
afef0 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f  ob[i/2] = (hexTo
aff00 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20  Int(z[i])<<4) | 
aff10 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29  hexToInt(z[i+1])
aff20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f  ;.    }.    zBlo
aff30 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a  b[i/2] = 0;.  }.
aff40 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a    return zBlob;.
aff50 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
aff60 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
aff70 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f  TERAL || SQLITE_
aff80 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f  HAS_CODEC */.../
aff90 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
affa0 73 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f  sqlite.magic fro
affb0 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  m SQLITE_MAGIC_O
affc0 50 45 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41  PEN to SQLITE_MA
affd0 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74  GIC_BUSY..** Ret
affe0 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f  urn an error (no
afff0 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d  n-zero) if the m
b0000 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c  agic was not SQL
b0010 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a  ITE_MAGIC_OPEN.*
b0020 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  * when this rout
b0030 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ine is called..*
b0040 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
b0050 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  e is called when
b0060 20 65 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c   entering an SQL
b0070 69 74 65 20 41 50 49 2e 20 20 54 68 65 20 53 51  ite API.  The SQ
b0080 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a  LITE_MAGIC_OPEN.
b0090 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74  ** value indicat
b00a0 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61  es that the data
b00b0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
b00c0 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20  passed into the 
b00d0 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61  API is.** open a
b00e0 6e 64 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  nd is not being 
b00f0 75 73 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20  used by another 
b0100 74 68 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e  thread.  By chan
b0110 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a  ging the value.*
b0120 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49  * to SQLITE_MAGI
b0130 43 5f 42 55 53 59 20 77 65 20 69 6e 64 69 63 61  C_BUSY we indica
b0140 74 65 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e  te that the conn
b0150 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65  ection is in use
b0160 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65  ..** sqlite3Safe
b0170 74 79 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69  tyOff() below wi
b0180 6c 6c 20 63 68 61 6e 67 65 20 74 68 65 20 76 61  ll change the va
b0190 6c 75 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49  lue back to SQLI
b01a0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a  TE_MAGIC_OPEN.**
b01b0 20 77 68 65 6e 20 74 68 65 20 41 50 49 20 65 78   when the API ex
b01c0 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  its. .**.** This
b01d0 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74   routine is a at
b01e0 74 65 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20  tempt to detect 
b01f0 69 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 75  if two threads u
b0200 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73  se the.** same s
b0210 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61  qlite* pointer a
b0220 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e  t the same time.
b0230 20 20 54 68 65 72 65 20 69 73 20 61 20 72 61 63    There is a rac
b0240 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20  e .** condition 
b0250 73 6f 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  so it is possibl
b0260 65 20 74 68 61 74 20 74 68 65 20 65 72 72 6f 72  e that the error
b0270 20 69 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64   is not detected
b0280 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79  ..** But usually
b0290 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c   the problem wil
b02a0 6c 20 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20  l be seen.  The 
b02b0 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61  result will be a
b02c0 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68  n.** error which
b02d0 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
b02e0 64 65 62 75 67 20 74 68 65 20 61 70 70 6c 69 63  debug the applic
b02f0 61 74 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a  ation that is.**
b0300 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e   using SQLite in
b0310 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a  correctly..**.**
b0320 20 54 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49   Ticket #202:  I
b0330 66 20 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e  f db->magic is n
b0340 6f 74 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20  ot a valid open 
b0350 76 61 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65  value, take care
b0360 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66   not.** to modif
b0370 79 20 74 68 65 20 64 62 20 73 74 72 75 63 74 75  y the db structu
b0380 72 65 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63  re at all.  It c
b0390 6f 75 6c 64 20 62 65 20 74 68 61 74 20 64 62 20  ould be that db 
b03a0 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f  is a stale.** po
b03b0 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72  inter.  In other
b03c0 20 77 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64   words, it could
b03d0 20 62 65 20 74 68 61 74 20 74 68 65 72 65 20 68   be that there h
b03e0 61 73 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a  as been a prior.
b03f0 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ** call to sqlit
b0400 65 33 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64  e3_close(db) and
b0410 20 64 62 20 68 61 73 20 62 65 65 6e 20 64 65 61   db has been dea
b0420 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77  llocated.  And w
b0430 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74  e do.** not want
b0440 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64   to write into d
b0450 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72  eallocated memor
b0460 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  y..*/.#ifdef SQL
b0470 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45  ITE_DEBUG.SQLITE
b0480 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b0490 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c  ite3SafetyOn(sql
b04a0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28  ite3 *db){.  if(
b04b0 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49   db->magic==SQLI
b04c0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b  TE_MAGIC_OPEN ){
b04d0 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  .    db->magic =
b04e0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55   SQLITE_MAGIC_BU
b04f0 53 59 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  SY;.    assert( 
b0500 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
b0510 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
b0520 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b0530 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d   }else if( db->m
b0540 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47  agic==SQLITE_MAG
b0550 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64  IC_BUSY ){.    d
b0560 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
b0570 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20  E_MAGIC_ERROR;. 
b0580 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65     db->u1.isInte
b0590 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d  rrupted = 1;.  }
b05a0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
b05b0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  endif../*.** Cha
b05c0 6e 67 65 20 74 68 65 20 6d 61 67 69 63 20 66 72  nge the magic fr
b05d0 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  om SQLITE_MAGIC_
b05e0 42 55 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d  BUSY to SQLITE_M
b05f0 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65  AGIC_OPEN..** Re
b0600 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e  turn an error (n
b0610 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20  on-zero) if the 
b0620 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51  magic was not SQ
b0630 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a  LITE_MAGIC_BUSY.
b0640 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  ** when this rou
b0650 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
b0660 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
b0670 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
b0680 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b0690 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74  3SafetyOff(sqlit
b06a0 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64  e3 *db){.  if( d
b06b0 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45  b->magic==SQLITE
b06c0 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20  _MAGIC_BUSY ){. 
b06d0 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53     db->magic = S
b06e0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b06f0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ;.    assert( sq
b0700 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
b0710 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
b0720 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
b0730 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  else{.    db->ma
b0740 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
b0750 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62  IC_ERROR;.    db
b0760 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
b0770 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  ed = 1;.    retu
b0780 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  rn 1;.  }.}.#end
b0790 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  if../*.** Check 
b07a0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20  to make sure we 
b07b0 68 61 76 65 20 61 20 76 61 6c 69 64 20 64 62 20  have a valid db 
b07c0 70 6f 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74  pointer.  This t
b07d0 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f  est is not.** fo
b07e0 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64  olproof but it d
b07f0 6f 65 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65  oes provide some
b0800 20 6d 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74   measure of prot
b0810 65 63 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a  ection against.*
b0820 2a 20 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20  * misuse of the 
b0830 69 6e 74 65 72 66 61 63 65 20 73 75 63 68 20 61  interface such a
b0840 73 20 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20  s passing in db 
b0850 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20 61 72  pointers that ar
b0860 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69  e.** NULL or whi
b0870 63 68 20 68 61 76 65 20 62 65 65 6e 20 70 72 65  ch have been pre
b0880 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20  viously closed. 
b0890 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
b08a0 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74   returns.** 1 it
b08b0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
b08c0 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61  db pointer is va
b08d0 6c 69 64 20 61 6e 64 20 30 20 69 66 20 69 74 20  lid and 0 if it 
b08e0 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
b08f0 20 64 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f   dereferenced fo
b0900 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54  r any reason.  T
b0910 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
b0920 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b  ion should invok
b0930 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55  e.** SQLITE_MISU
b0940 53 45 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a  SE immediately..
b0950 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66  **.** sqlite3Saf
b0960 65 74 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71  etyCheckOk() req
b0970 75 69 72 65 73 20 74 68 61 74 20 74 68 65 20 64  uires that the d
b0980 62 20 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c  b pointer be val
b0990 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20  id for.** use.  
b09a0 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
b09b0 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c  ckSickOrOk() all
b09c0 6f 77 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72  ows a db pointer
b09d0 20 74 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a   that failed to.
b09e0 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79  ** open properly
b09f0 20 61 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20   and is not fit 
b0a00 66 6f 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20  for general use 
b0a10 62 75 74 20 77 68 69 63 68 20 63 61 6e 20 62 65  but which can be
b0a20 0a 2a 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61  .** used as an a
b0a30 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
b0a40 65 33 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73  e3_errmsg() or s
b0a50 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a  qlite3_close()..
b0a60 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b0a70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
b0a80 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74  etyCheckOk(sqlit
b0a90 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d  e3 *db){.  u32 m
b0aa0 61 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d  agic;.  if( db==
b0ab0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
b0ac0 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69  magic = db->magi
b0ad0 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d  c;.  if( magic!=
b0ae0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
b0af0 4e 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  N .#ifdef SQLITE
b0b00 5f 44 45 42 55 47 0a 20 20 20 20 20 26 26 20 6d  _DEBUG.     && m
b0b10 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47  agic!=SQLITE_MAG
b0b20 49 43 5f 42 55 53 59 0a 23 65 6e 64 69 66 0a 20  IC_BUSY.#endif. 
b0b30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
b0b40 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
b0b50 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53  eturn 1;.  }.}.S
b0b60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b0b70 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  t sqlite3SafetyC
b0b80 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c  heckSickOrOk(sql
b0b90 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32  ite3 *db){.  u32
b0ba0 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67 69 63 20   magic;.  magic 
b0bb0 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69  = db->magic;.  i
b0bc0 66 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45  f( magic!=SQLITE
b0bd0 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26 26 0a 20  _MAGIC_SICK &&. 
b0be0 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49       magic!=SQLI
b0bf0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26  TE_MAGIC_OPEN &&
b0c00 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51  .      magic!=SQ
b0c10 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
b0c20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65  ) return 0;.  re
b0c30 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  turn 1;.}../****
b0c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
b0c50 66 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f util.c *******
b0c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
b0c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
b0ca0 20 66 69 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a   file hash.c ***
b0cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
b0ce0 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
b0cf0 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
b0d00 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
b0d10 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
b0d20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
b0d30 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
b0d40 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
b0d50 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
b0d60 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
b0d70 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
b0d80 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
b0d90 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
b0da0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
b0db0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
b0dc0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
b0dd0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
b0de0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
b0df0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
b0e00 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
b0e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0e50 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
b0e60 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
b0e70 6f 66 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d  of generic hash-
b0e80 74 61 62 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69  tables.** used i
b0e90 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  n SQLite..**.** 
b0ea0 24 49 64 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e  $Id: hash.c,v 1.
b0eb0 33 37 20 32 30 30 39 2f 30 35 2f 30 32 20 31 33  37 2009/05/02 13
b0ec0 3a 32 39 3a 33 38 20 64 72 68 20 45 78 70 20 24  :29:38 drh Exp $
b0ed0 0a 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c  .*/../* Turn bul
b0ee0 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20  k memory into a 
b0ef0 68 61 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63  hash table objec
b0f00 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e  t by initializin
b0f10 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20  g the.** fields 
b0f20 6f 66 20 74 68 65 20 48 61 73 68 20 73 74 72 75  of the Hash stru
b0f30 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e  cture..**.** "pN
b0f40 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ew" is a pointer
b0f50 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62   to the hash tab
b0f60 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  le that is to be
b0f70 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f   initialized..*/
b0f80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0f90 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68  void sqlite3Hash
b0fa0 49 6e 69 74 28 48 61 73 68 20 2a 70 4e 65 77 29  Init(Hash *pNew)
b0fb0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77  {.  assert( pNew
b0fc0 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 66  !=0 );.  pNew->f
b0fd0 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77  irst = 0;.  pNew
b0fe0 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70  ->count = 0;.  p
b0ff0 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b  New->htsize = 0;
b1000 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b  .  pNew->ht = 0;
b1010 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c  .}../* Remove al
b1020 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61  l entries from a
b1030 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65   hash table.  Re
b1040 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79  claim all memory
b1050 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72  ..** Call this r
b1060 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65  outine to delete
b1070 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72   a hash table or
b1080 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 68   to reset a hash
b1090 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65   table.** to the
b10a0 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f   empty state..*/
b10b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b10c0 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68  void sqlite3Hash
b10d0 43 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b  Clear(Hash *pH){
b10e0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  .  HashElem *ele
b10f0 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  m;         /* Fo
b1100 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61  r looping over a
b1110 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  ll elements of t
b1120 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61  he table */..  a
b1130 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a  ssert( pH!=0 );.
b1140 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72    elem = pH->fir
b1150 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20  st;.  pH->first 
b1160 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  = 0;.  sqlite3_f
b1170 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70  ree(pH->ht);.  p
b1180 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d  H->ht = 0;.  pH-
b1190 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77  >htsize = 0;.  w
b11a0 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20  hile( elem ){.  
b11b0 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74    HashElem *next
b11c0 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65  _elem = elem->ne
b11d0 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  xt;.    sqlite3_
b11e0 66 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20  free(elem);.    
b11f0 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d  elem = next_elem
b1200 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e  ;.  }.  pH->coun
b1210 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t = 0;.}../*.** 
b1220 54 68 65 20 68 61 73 68 69 6e 67 20 66 75 6e 63  The hashing func
b1230 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
b1240 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 74 72  unsigned int str
b1250 48 61 73 68 28 63 6f 6e 73 74 20 63 68 61 72 20  Hash(const char 
b1260 2a 7a 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20  *z, int nKey){. 
b1270 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 61 73   int h = 0;.  as
b1280 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b  sert( nKey>=0 );
b1290 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e  .  while( nKey >
b12a0 20 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28   0  ){.    h = (
b12b0 68 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69  h<<3) ^ h ^ sqli
b12c0 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
b12d0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a  (unsigned char)*
b12e0 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d  z++];.    nKey--
b12f0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68  ;.  }.  return h
b1300 3b 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e  ;.}.../* Link pN
b1310 65 77 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20  ew element into 
b1320 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70  the hash table p
b1330 48 2e 20 20 49 66 20 70 45 6e 74 72 79 21 3d 30  H.  If pEntry!=0
b1340 20 74 68 65 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e   then also.** in
b1350 73 65 72 74 20 70 4e 65 77 20 69 6e 74 6f 20 74  sert pNew into t
b1360 68 65 20 70 45 6e 74 72 79 20 68 61 73 68 20 62  he pEntry hash b
b1370 75 63 6b 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ucket..*/.static
b1380 20 76 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d   void insertElem
b1390 65 6e 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c  ent(.  Hash *pH,
b13a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
b13b0 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61   The complete ha
b13c0 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74  sh table */.  st
b13d0 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79  ruct _ht *pEntry
b13e0 2c 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72  ,    /* The entr
b13f0 79 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65  y into which pNe
b1400 77 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f  w is inserted */
b1410 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65  .  HashElem *pNe
b1420 77 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  w         /* The
b1430 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69   element to be i
b1440 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20  nserted */.){.  
b1450 48 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b  HashElem *pHead;
b1460 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
b1470 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20  element already 
b1480 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 69  in pEntry */.  i
b1490 66 28 20 70 45 6e 74 72 79 20 29 7b 0a 20 20 20  f( pEntry ){.   
b14a0 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d   pHead = pEntry-
b14b0 3e 63 6f 75 6e 74 20 3f 20 70 45 6e 74 72 79 2d  >count ? pEntry-
b14c0 3e 63 68 61 69 6e 20 3a 20 30 3b 0a 20 20 20 20  >chain : 0;.    
b14d0 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b  pEntry->count++;
b14e0 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61  .    pEntry->cha
b14f0 69 6e 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c  in = pNew;.  }el
b1500 73 65 7b 0a 20 20 20 20 70 48 65 61 64 20 3d 20  se{.    pHead = 
b1510 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 65  0;.  }.  if( pHe
b1520 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e  ad ){.    pNew->
b1530 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20  next = pHead;.  
b1540 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70    pNew->prev = p
b1550 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20  Head->prev;.    
b1560 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20  if( pHead->prev 
b1570 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e  ){ pHead->prev->
b1580 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20  next = pNew; }. 
b1590 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20     else         
b15a0 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20      { pH->first 
b15b0 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48  = pNew; }.    pH
b15c0 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77  ead->prev = pNew
b15d0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
b15e0 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e  New->next = pH->
b15f0 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70  first;.    if( p
b1600 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e  H->first ){ pH->
b1610 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e  first->prev = pN
b1620 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e  ew; }.    pNew->
b1630 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48  prev = 0;.    pH
b1640 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a  ->first = pNew;.
b1650 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a    }.}.../* Resiz
b1660 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  e the hash table
b1670 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 74   so that it cant
b1680 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22 20  ains "new_size" 
b1690 62 75 63 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  buckets..**.** T
b16a0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69  he hash table mi
b16b0 67 68 74 20 66 61 69 6c 20 74 6f 20 72 65 73 69  ght fail to resi
b16c0 7a 65 20 69 66 20 73 71 6c 69 74 65 33 5f 6d 61  ze if sqlite3_ma
b16d0 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 6f 72 0a  lloc() fails or.
b16e0 2a 2a 20 69 66 20 74 68 65 20 6e 65 77 20 73 69  ** if the new si
b16f0 7a 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ze is the same a
b1700 73 20 74 68 65 20 70 72 69 6f 72 20 73 69 7a 65  s the prior size
b1710 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  ..** Return TRUE
b1720 20 69 66 20 74 68 65 20 72 65 73 69 7a 65 20 6f   if the resize o
b1730 63 63 75 72 73 20 61 6e 64 20 66 61 6c 73 65 20  ccurs and false 
b1740 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69  if not..*/.stati
b1750 63 20 69 6e 74 20 72 65 68 61 73 68 28 48 61 73  c int rehash(Has
b1760 68 20 2a 70 48 2c 20 75 6e 73 69 67 6e 65 64 20  h *pH, unsigned 
b1770 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20  int new_size){. 
b1780 20 73 74 72 75 63 74 20 5f 68 74 20 2a 6e 65 77   struct _ht *new
b1790 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  _ht;            
b17a0 2f 2a 20 54 68 65 20 6e 65 77 20 68 61 73 68 20  /* The new hash 
b17b0 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45  table */.  HashE
b17c0 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74  lem *elem, *next
b17d0 5f 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72  _elem;    /* For
b17e0 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78   looping over ex
b17f0 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20  isting elements 
b1800 2a 2f 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  */..#if SQLITE_M
b1810 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54  ALLOC_SOFT_LIMIT
b1820 3e 30 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a  >0.  if( new_siz
b1830 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  e*sizeof(struct 
b1840 5f 68 74 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c  _ht)>SQLITE_MALL
b1850 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b  OC_SOFT_LIMIT ){
b1860 0a 20 20 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20  .    new_size = 
b1870 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f  SQLITE_MALLOC_SO
b1880 46 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28  FT_LIMIT/sizeof(
b1890 73 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d  struct _ht);.  }
b18a0 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d  .  if( new_size=
b18b0 3d 70 48 2d 3e 68 74 73 69 7a 65 20 29 20 72 65  =pH->htsize ) re
b18c0 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  turn 0;.#endif..
b18d0 20 20 2f 2a 20 54 68 65 20 69 6e 61 62 69 6c 69    /* The inabili
b18e0 74 79 20 74 6f 20 61 6c 6c 6f 63 61 74 65 73 20  ty to allocates 
b18f0 73 70 61 63 65 20 66 6f 72 20 61 20 6c 61 72 67  space for a larg
b1900 65 72 20 68 61 73 68 20 74 61 62 6c 65 20 69 73  er hash table is
b1910 0a 20 20 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61  .  ** a performa
b1920 6e 63 65 20 68 69 74 20 62 75 74 20 69 74 20 69  nce hit but it i
b1930 73 20 6e 6f 74 20 61 20 66 61 74 61 6c 20 65 72  s not a fatal er
b1940 72 6f 72 2e 20 20 53 6f 20 6d 61 72 6b 20 74 68  ror.  So mark th
b1950 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  e.  ** allocatio
b1960 6e 20 61 73 20 61 20 62 65 6e 69 67 6e 2e 0a 20  n as a benign.. 
b1970 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67   */.  sqlite3Beg
b1980 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
b1990 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74  ;.  new_ht = (st
b19a0 72 75 63 74 20 5f 68 74 20 2a 29 73 71 6c 69 74  ruct _ht *)sqlit
b19b0 65 33 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69  e3Malloc( new_si
b19c0 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74  ze*sizeof(struct
b19d0 20 5f 68 74 29 20 29 3b 0a 20 20 73 71 6c 69 74   _ht) );.  sqlit
b19e0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
b19f0 63 28 29 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f  c();..  if( new_
b1a00 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ht==0 ) return 0
b1a10 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
b1a20 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e  (pH->ht);.  pH->
b1a30 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70  ht = new_ht;.  p
b1a40 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f  H->htsize = new_
b1a50 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61  size = sqlite3Ma
b1a60 6c 6c 6f 63 53 69 7a 65 28 6e 65 77 5f 68 74 29  llocSize(new_ht)
b1a70 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f  /sizeof(struct _
b1a80 68 74 29 3b 0a 20 20 6d 65 6d 73 65 74 28 6e 65  ht);.  memset(ne
b1a90 77 5f 68 74 2c 20 30 2c 20 6e 65 77 5f 73 69 7a  w_ht, 0, new_siz
b1aa0 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  e*sizeof(struct 
b1ab0 5f 68 74 29 29 3b 0a 20 20 66 6f 72 28 65 6c 65  _ht));.  for(ele
b1ac0 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d  m=pH->first, pH-
b1ad0 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20  >first=0; elem; 
b1ae0 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d  elem = next_elem
b1af0 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  ){.    unsigned 
b1b00 69 6e 74 20 68 20 3d 20 73 74 72 48 61 73 68 28  int h = strHash(
b1b10 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d  elem->pKey, elem
b1b20 2d 3e 6e 4b 65 79 29 20 25 20 6e 65 77 5f 73 69  ->nKey) % new_si
b1b30 7a 65 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65  ze;.    next_ele
b1b40 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
b1b50 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e      insertElemen
b1b60 74 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d  t(pH, &new_ht[h]
b1b70 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 72  , elem);.  }.  r
b1b80 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54  eturn 1;.}../* T
b1b90 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f  his function (fo
b1ba0 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f  r internal use o
b1bb0 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20  nly) locates an 
b1bc0 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a  element in an.**
b1bd0 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74   hash table that
b1be0 20 6d 61 74 63 68 65 73 20 74 68 65 20 67 69 76   matches the giv
b1bf0 65 6e 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73  en key.  The has
b1c00 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68  h for this key h
b1c10 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65  as.** already be
b1c20 65 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20  en computed and 
b1c30 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  is passed as the
b1c40 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a   4th parameter..
b1c50 2a 2f 0a 73 74 61 74 69 63 20 48 61 73 68 45 6c  */.static HashEl
b1c60 65 6d 20 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47  em *findElementG
b1c70 69 76 65 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73  ivenHash(.  cons
b1c80 74 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20  t Hash *pH,     
b1c90 2f 2a 20 54 68 65 20 70 48 20 74 6f 20 62 65 20  /* The pH to be 
b1ca0 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f  searched */.  co
b1cb0 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20  nst char *pKey, 
b1cc0 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20    /* The key we 
b1cd0 61 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f  are searching fo
b1ce0 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c  r */.  int nKey,
b1cf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
b1d00 74 65 73 20 69 6e 20 6b 65 79 20 28 6e 6f 74 20  tes in key (not 
b1d10 63 6f 75 6e 74 69 6e 67 20 7a 65 72 6f 20 74 65  counting zero te
b1d20 72 6d 69 6e 61 74 6f 72 29 20 2a 2f 0a 20 20 75  rminator) */.  u
b1d30 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 20 20  nsigned int h   
b1d40 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66     /* The hash f
b1d50 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a  or this key. */.
b1d60 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65  ){.  HashElem *e
b1d70 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  lem;            
b1d80 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c      /* Used to l
b1d90 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65  oop thru the ele
b1da0 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69  ment list */.  i
b1db0 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20  nt count;       
b1dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
b1dd0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
b1de0 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74  nts left to test
b1df0 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68   */..  if( pH->h
b1e00 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  t ){.    struct 
b1e10 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70  _ht *pEntry = &p
b1e20 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c  H->ht[h];.    el
b1e30 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61  em = pEntry->cha
b1e40 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20  in;.    count = 
b1e50 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20  pEntry->count;. 
b1e60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 65 6c 65 6d   }else{.    elem
b1e70 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20   = pH->first;.  
b1e80 20 20 63 6f 75 6e 74 20 3d 20 70 48 2d 3e 63 6f    count = pH->co
b1e90 75 6e 74 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65  unt;.  }.  while
b1ea0 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 41 4c 57  ( count-- && ALW
b1eb0 41 59 53 28 65 6c 65 6d 29 20 29 7b 0a 20 20 20  AYS(elem) ){.   
b1ec0 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d   if( elem->nKey=
b1ed0 3d 6e 4b 65 79 20 26 26 20 73 71 6c 69 74 65 33  =nKey && sqlite3
b1ee0 53 74 72 4e 49 43 6d 70 28 65 6c 65 6d 2d 3e 70  StrNICmp(elem->p
b1ef0 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d  Key,pKey,nKey)==
b1f00 30 20 29 7b 20 0a 20 20 20 20 20 20 72 65 74 75  0 ){ .      retu
b1f10 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20  rn elem;.    }. 
b1f20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e     elem = elem->
b1f30 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75  next;.  }.  retu
b1f40 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f  rn 0;.}../* Remo
b1f50 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72  ve a single entr
b1f60 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  y from the hash 
b1f70 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f  table given a po
b1f80 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a  inter to that.**
b1f90 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68   element and a h
b1fa0 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65  ash on the eleme
b1fb0 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61  nt's key..*/.sta
b1fc0 74 69 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 45  tic void removeE
b1fd0 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28  lementGivenHash(
b1fe0 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20  .  Hash *pH,    
b1ff0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63       /* The pH c
b2000 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22  ontaining "elem"
b2010 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20   */.  HashElem* 
b2020 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65  elem,   /* The e
b2030 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d  lement to be rem
b2040 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48  oved from the pH
b2050 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
b2060 6e 74 20 68 20 20 20 20 2f 2a 20 48 61 73 68 20  nt h    /* Hash 
b2070 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c  value for the el
b2080 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74  ement */.){.  st
b2090 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79  ruct _ht *pEntry
b20a0 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72  ;.  if( elem->pr
b20b0 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e  ev ){.    elem->
b20c0 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65  prev->next = ele
b20d0 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73  m->next; .  }els
b20e0 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74  e{.    pH->first
b20f0 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
b2100 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e   }.  if( elem->n
b2110 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d  ext ){.    elem-
b2120 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c  >next->prev = el
b2130 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20  em->prev;.  }.  
b2140 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20  if( pH->ht ){.  
b2150 20 20 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e    pEntry = &pH->
b2160 68 74 5b 68 5d 3b 0a 20 20 20 20 69 66 28 20 70  ht[h];.    if( p
b2170 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c  Entry->chain==el
b2180 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 45 6e 74  em ){.      pEnt
b2190 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d  ry->chain = elem
b21a0 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  ->next;.    }.  
b21b0 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d    pEntry->count-
b21c0 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  -;.    assert( p
b21d0 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20  Entry->count>=0 
b21e0 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
b21f0 5f 66 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20  _free( elem );. 
b2200 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20   pH->count--;.  
b2210 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30  if( pH->count<=0
b2220 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
b2230 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a  pH->first==0 );.
b2240 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e      assert( pH->
b2250 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20  count==0 );.    
b2260 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
b2270 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  (pH);.  }.}../* 
b2280 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74  Attempt to locat
b2290 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20  e an element of 
b22a0 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70  the hash table p
b22b0 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20  H with a key.** 
b22c0 74 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65  that matches pKe
b22d0 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20  y,nKey.  Return 
b22e0 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69  the data for thi
b22f0 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20  s element if it 
b2300 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20  is.** found, or 
b2310 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
b2320 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51   no match..*/.SQ
b2330 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b2340 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69  d *sqlite3HashFi
b2350 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70  nd(const Hash *p
b2360 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  H, const char *p
b2370 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a  Key, int nKey){.
b2380 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d    HashElem *elem
b2390 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d  ;    /* The elem
b23a0 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68 65 73  ent that matches
b23b0 20 6b 65 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e   key */.  unsign
b23c0 65 64 20 69 6e 74 20 68 3b 20 20 20 20 2f 2a 20  ed int h;    /* 
b23d0 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f  A hash on key */
b23e0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d  ..  assert( pH!=
b23f0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
b2400 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  Key!=0 );.  asse
b2410 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20  rt( nKey>=0 );. 
b2420 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20   if( pH->ht ){. 
b2430 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70     h = strHash(p
b2440 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d  Key, nKey) % pH-
b2450 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65  >htsize;.  }else
b2460 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d  {.    h = 0;.  }
b2470 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c  .  elem = findEl
b2480 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70  ementGivenHash(p
b2490 48 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 68  H, pKey, nKey, h
b24a0 29 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d  );.  return elem
b24b0 20 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20   ? elem->data : 
b24c0 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20  0;.}../* Insert 
b24d0 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20  an element into 
b24e0 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70  the hash table p
b24f0 48 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 70  H.  The key is p
b2500 4b 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20  Key,nKey.** and 
b2510 74 68 65 20 64 61 74 61 20 69 73 20 22 64 61 74  the data is "dat
b2520 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20  a"..**.** If no 
b2530 65 6c 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77  element exists w
b2540 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b  ith a matching k
b2550 65 79 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a  ey, then a new.*
b2560 2a 20 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65  * element is cre
b2570 61 74 65 64 20 61 6e 64 20 4e 55 4c 4c 20 69 73  ated and NULL is
b2580 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
b2590 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d   If another elem
b25a0 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  ent already exis
b25b0 74 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  ts with the same
b25c0 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a   key, then the.*
b25d0 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61  * new data repla
b25e0 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61  ces the old data
b25f0 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74   and the old dat
b2600 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  a is returned..*
b2610 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74  * The key is not
b2620 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20   copied in this 
b2630 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20  instance.  If a 
b2640 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68  malloc fails, th
b2650 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61  en.** the new da
b2660 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  ta is returned a
b2670 6e 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  nd the hash tabl
b2680 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a  e is unchanged..
b2690 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61  **.** If the "da
b26a0 74 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ta" parameter to
b26b0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
b26c0 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  s NULL, then the
b26d0 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72  .** element corr
b26e0 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65  esponding to "ke
b26f0 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72  y" is removed fr
b2700 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
b2710 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
b2720 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
b2730 65 33 48 61 73 68 49 6e 73 65 72 74 28 48 61 73  e3HashInsert(Has
b2740 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61  h *pH, const cha
b2750 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  r *pKey, int nKe
b2760 79 2c 20 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a  y, void *data){.
b2770 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
b2780 3b 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68  ;       /* the h
b2790 61 73 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d  ash of the key m
b27a0 6f 64 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65  odulo hash table
b27b0 20 73 69 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45   size */.  HashE
b27c0 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20  lem *elem;      
b27d0 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70   /* Used to loop
b27e0 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e   thru the elemen
b27f0 74 20 6c 69 73 74 20 2a 2f 0a 20 20 48 61 73 68  t list */.  Hash
b2800 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20  Elem *new_elem; 
b2810 20 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74    /* New element
b2820 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 48   added to the pH
b2830 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
b2840 48 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  H!=0 );.  assert
b2850 28 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61  ( pKey!=0 );.  a
b2860 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29  ssert( nKey>=0 )
b2870 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69  ;.  if( pH->htsi
b2880 7a 65 20 29 7b 0a 20 20 20 20 68 20 3d 20 73 74  ze ){.    h = st
b2890 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79  rHash(pKey, nKey
b28a0 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a  ) % pH->htsize;.
b28b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d    }else{.    h =
b28c0 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d 20 3d   0;.  }.  elem =
b28d0 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65   findElementGive
b28e0 6e 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b  nHash(pH,pKey,nK
b28f0 65 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65  ey,h);.  if( ele
b2900 6d 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f  m ){.    void *o
b2910 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e  ld_data = elem->
b2920 64 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61  data;.    if( da
b2930 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  ta==0 ){.      r
b2940 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76 65  emoveElementGive
b2950 6e 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29  nHash(pH,elem,h)
b2960 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
b2970 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20     elem->data = 
b2980 64 61 74 61 3b 0a 20 20 20 20 20 20 65 6c 65 6d  data;.      elem
b2990 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20  ->pKey = pKey;. 
b29a0 20 20 20 20 20 61 73 73 65 72 74 28 6e 4b 65 79       assert(nKey
b29b0 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20  ==elem->nKey);. 
b29c0 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
b29d0 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20  old_data;.  }.  
b29e0 69 66 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65  if( data==0 ) re
b29f0 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c  turn 0;.  new_el
b2a00 65 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29  em = (HashElem*)
b2a10 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73  sqlite3Malloc( s
b2a20 69 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20  izeof(HashElem) 
b2a30 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65  );.  if( new_ele
b2a40 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61  m==0 ) return da
b2a50 74 61 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e  ta;.  new_elem->
b2a60 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 6e  pKey = pKey;.  n
b2a70 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20  ew_elem->nKey = 
b2a80 6e 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d  nKey;.  new_elem
b2a90 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20  ->data = data;. 
b2aa0 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20   pH->count++;.  
b2ab0 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31  if( pH->count>=1
b2ac0 30 20 26 26 20 70 48 2d 3e 63 6f 75 6e 74 20 3e  0 && pH->count >
b2ad0 20 32 2a 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b   2*pH->htsize ){
b2ae0 0a 20 20 20 20 69 66 28 20 72 65 68 61 73 68 28  .    if( rehash(
b2af0 70 48 2c 20 70 48 2d 3e 63 6f 75 6e 74 2a 32 29  pH, pH->count*2)
b2b00 20 26 26 20 70 48 2d 3e 68 74 73 69 7a 65 20 29   && pH->htsize )
b2b10 7b 0a 20 20 20 20 20 20 68 20 3d 20 73 74 72 48  {.      h = strH
b2b20 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20  ash(pKey, nKey) 
b2b30 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20  % pH->htsize;.  
b2b40 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48    }.  }.  if( pH
b2b50 2d 3e 68 74 20 29 7b 0a 20 20 20 20 69 6e 73 65  ->ht ){.    inse
b2b60 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70  rtElement(pH, &p
b2b70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c  H->ht[h], new_el
b2b80 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  em);.  }else{.  
b2b90 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28    insertElement(
b2ba0 70 48 2c 20 30 2c 20 6e 65 77 5f 65 6c 65 6d 29  pH, 0, new_elem)
b2bb0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
b2bc0 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
b2bd0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68  **** End of hash
b2be0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
b2bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2c10 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
b2c20 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
b2c30 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a  opcodes.c ******
b2c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2c60 2a 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69  ***/./* Automati
b2c70 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e  cally generated.
b2c80 20 20 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f    Do not edit */
b2c90 0a 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70  ./* See the mkop
b2ca0 63 6f 64 65 63 2e 61 77 6b 20 73 63 72 69 70 74  codec.awk script
b2cb0 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f   for details. */
b2cc0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
b2cd0 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
b2ce0 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e  N) || !defined(N
b2cf0 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65  DEBUG) || define
b2d00 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20  d(VDBE_PROFILE) 
b2d10 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
b2d20 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f  E_DEBUG).SQLITE_
b2d30 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
b2d40 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64  ar *sqlite3Opcod
b2d50 65 4e 61 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73  eName(int i){. s
b2d60 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
b2d70 20 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d   *const azName[]
b2d80 20 3d 20 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f   = { "?",.     /
b2d90 2a 20 20 20 31 20 2a 2f 20 22 56 4e 65 78 74 22  *   1 */ "VNext"
b2da0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f  ,.     /*   2 */
b2db0 20 22 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20   "Affinity",.   
b2dc0 20 20 2f 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c    /*   3 */ "Col
b2dd0 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20  umn",.     /*   
b2de0 34 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22  4 */ "SetCookie"
b2df0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f  ,.     /*   5 */
b2e00 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a   "Seek",.     /*
b2e10 20 20 20 36 20 2a 2f 20 22 53 65 71 75 65 6e 63     6 */ "Sequenc
b2e20 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 20  e",.     /*   7 
b2e30 2a 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a  */ "Savepoint",.
b2e40 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22       /*   8 */ "
b2e50 52 6f 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a  RowKey",.     /*
b2e60 20 20 20 39 20 2a 2f 20 22 53 43 6f 70 79 22 2c     9 */ "SCopy",
b2e70 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20  .     /*  10 */ 
b2e80 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 20  "OpenWrite",.   
b2e90 20 20 2f 2a 20 20 31 31 20 2a 2f 20 22 49 66 22    /*  11 */ "If"
b2ea0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f  ,.     /*  12 */
b2eb0 20 22 43 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20   "CollSeq",.    
b2ec0 20 2f 2a 20 20 31 33 20 2a 2f 20 22 4f 70 65 6e   /*  13 */ "Open
b2ed0 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Read",.     /*  
b2ee0 31 34 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a  14 */ "Expire",.
b2ef0 20 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f 20 22       /*  15 */ "
b2f00 41 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20  AutoCommit",.   
b2f10 20 20 2f 2a 20 20 31 36 20 2a 2f 20 22 50 61 67    /*  16 */ "Pag
b2f20 65 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a  ecount",.     /*
b2f30 20 20 31 37 20 2a 2f 20 22 49 6e 74 65 67 72 69    17 */ "Integri
b2f40 74 79 43 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20  tyCk",.     /*  
b2f50 31 38 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20  18 */ "Sort",.  
b2f60 20 20 20 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f     /*  19 */ "No
b2f70 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 30 20  t",.     /*  20 
b2f80 2a 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20  */ "Copy",.     
b2f90 2f 2a 20 20 32 31 20 2a 2f 20 22 54 72 61 63 65  /*  21 */ "Trace
b2fa0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 32 20 2a  ",.     /*  22 *
b2fb0 2f 20 22 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20  / "Function",.  
b2fc0 20 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 49 66     /*  23 */ "If
b2fd0 4e 65 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32  Neg",.     /*  2
b2fe0 34 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20  4 */ "Noop",.   
b2ff0 20 20 2f 2a 20 20 32 35 20 2a 2f 20 22 52 65 74    /*  25 */ "Ret
b3000 75 72 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32  urn",.     /*  2
b3010 36 20 2a 2f 20 22 4e 65 77 52 6f 77 69 64 22 2c  6 */ "NewRowid",
b3020 0a 20 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20  .     /*  27 */ 
b3030 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20 20 20  "Variable",.    
b3040 20 2f 2a 20 20 32 38 20 2a 2f 20 22 53 74 72 69   /*  28 */ "Stri
b3050 6e 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39  ng",.     /*  29
b3060 20 2a 2f 20 22 52 65 61 6c 41 66 66 69 6e 69 74   */ "RealAffinit
b3070 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 30 20  y",.     /*  30 
b3080 2a 2f 20 22 56 52 65 6e 61 6d 65 22 2c 0a 20 20  */ "VRename",.  
b3090 20 20 20 2f 2a 20 20 33 31 20 2a 2f 20 22 50 61     /*  31 */ "Pa
b30a0 72 73 65 53 63 68 65 6d 61 22 2c 0a 20 20 20 20  rseSchema",.    
b30b0 20 2f 2a 20 20 33 32 20 2a 2f 20 22 56 4f 70 65   /*  32 */ "VOpe
b30c0 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 33 20  n",.     /*  33 
b30d0 2a 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20 20 20  */ "Close",.    
b30e0 20 2f 2a 20 20 33 34 20 2a 2f 20 22 43 72 65 61   /*  34 */ "Crea
b30f0 74 65 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f  teIndex",.     /
b3100 2a 20 20 33 35 20 2a 2f 20 22 49 73 55 6e 69 71  *  35 */ "IsUniq
b3110 75 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36  ue",.     /*  36
b3120 20 2a 2f 20 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a   */ "NotFound",.
b3130 20 20 20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22       /*  37 */ "
b3140 49 6e 74 36 34 22 2c 0a 20 20 20 20 20 2f 2a 20  Int64",.     /* 
b3150 20 33 38 20 2a 2f 20 22 4d 75 73 74 42 65 49 6e   38 */ "MustBeIn
b3160 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 39 20  t",.     /*  39 
b3170 2a 2f 20 22 48 61 6c 74 22 2c 0a 20 20 20 20 20  */ "Halt",.     
b3180 2f 2a 20 20 34 30 20 2a 2f 20 22 52 6f 77 69 64  /*  40 */ "Rowid
b3190 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31 20 2a  ",.     /*  41 *
b31a0 2f 20 22 49 64 78 4c 54 22 2c 0a 20 20 20 20 20  / "IdxLT",.     
b31b0 2f 2a 20 20 34 32 20 2a 2f 20 22 41 64 64 49 6d  /*  42 */ "AddIm
b31c0 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33 20  m",.     /*  43 
b31d0 2a 2f 20 22 53 74 61 74 65 6d 65 6e 74 22 2c 0a  */ "Statement",.
b31e0 20 20 20 20 20 2f 2a 20 20 34 34 20 2a 2f 20 22       /*  44 */ "
b31f0 52 6f 77 44 61 74 61 22 2c 0a 20 20 20 20 20 2f  RowData",.     /
b3200 2a 20 20 34 35 20 2a 2f 20 22 4d 65 6d 4d 61 78  *  45 */ "MemMax
b3210 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 36 20 2a  ",.     /*  46 *
b3220 2f 20 22 4e 6f 74 45 78 69 73 74 73 22 2c 0a 20  / "NotExists",. 
b3230 20 20 20 20 2f 2a 20 20 34 37 20 2a 2f 20 22 47      /*  47 */ "G
b3240 6f 73 75 62 22 2c 0a 20 20 20 20 20 2f 2a 20 20  osub",.     /*  
b3250 34 38 20 2a 2f 20 22 49 6e 74 65 67 65 72 22 2c  48 */ "Integer",
b3260 0a 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20  .     /*  49 */ 
b3270 22 50 72 65 76 22 2c 0a 20 20 20 20 20 2f 2a 20  "Prev",.     /* 
b3280 20 35 30 20 2a 2f 20 22 52 6f 77 53 65 74 52 65   50 */ "RowSetRe
b3290 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 31  ad",.     /*  51
b32a0 20 2a 2f 20 22 52 6f 77 53 65 74 41 64 64 22 2c   */ "RowSetAdd",
b32b0 0a 20 20 20 20 20 2f 2a 20 20 35 32 20 2a 2f 20  .     /*  52 */ 
b32c0 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20  "VColumn",.     
b32d0 2f 2a 20 20 35 33 20 2a 2f 20 22 43 72 65 61 74  /*  53 */ "Creat
b32e0 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a  eTable",.     /*
b32f0 20 20 35 34 20 2a 2f 20 22 4c 61 73 74 22 2c 0a    54 */ "Last",.
b3300 20 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f 20 22       /*  55 */ "
b3310 53 65 65 6b 4c 65 22 2c 0a 20 20 20 20 20 2f 2a  SeekLe",.     /*
b3320 20 20 35 36 20 2a 2f 20 22 49 6e 63 72 56 61 63    56 */ "IncrVac
b3330 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35  uum",.     /*  5
b3340 37 20 2a 2f 20 22 49 64 78 52 6f 77 69 64 22 2c  7 */ "IdxRowid",
b3350 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20  .     /*  58 */ 
b3360 22 52 65 73 65 74 43 6f 75 6e 74 22 2c 0a 20 20  "ResetCount",.  
b3370 20 20 20 2f 2a 20 20 35 39 20 2a 2f 20 22 43 6f     /*  59 */ "Co
b3380 6e 74 65 78 74 50 75 73 68 22 2c 0a 20 20 20 20  ntextPush",.    
b3390 20 2f 2a 20 20 36 30 20 2a 2f 20 22 59 69 65 6c   /*  60 */ "Yiel
b33a0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 31 20  d",.     /*  61 
b33b0 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65 72 22  */ "DropTrigger"
b33c0 2c 0a 20 20 20 20 20 2f 2a 20 20 36 32 20 2a 2f  ,.     /*  62 */
b33d0 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20   "DropIndex",.  
b33e0 20 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22 49 64     /*  63 */ "Id
b33f0 78 47 45 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  xGE",.     /*  6
b3400 34 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22  4 */ "IdxDelete"
b3410 2c 0a 20 20 20 20 20 2f 2a 20 20 36 35 20 2a 2f  ,.     /*  65 */
b3420 20 22 56 61 63 75 75 6d 22 2c 0a 20 20 20 20 20   "Vacuum",.     
b3430 2f 2a 20 20 36 36 20 2a 2f 20 22 4f 72 22 2c 0a  /*  66 */ "Or",.
b3440 20 20 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20 22       /*  67 */ "
b3450 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  And",.     /*  6
b3460 38 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a 20 20  8 */ "IfNot",.  
b3470 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22 44 72     /*  69 */ "Dr
b3480 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f  opTable",.     /
b3490 2a 20 20 37 30 20 2a 2f 20 22 53 65 65 6b 4c 74  *  70 */ "SeekLt
b34a0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 31 20 2a  ",.     /*  71 *
b34b0 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20 20  / "IsNull",.    
b34c0 20 2f 2a 20 20 37 32 20 2a 2f 20 22 4e 6f 74 4e   /*  72 */ "NotN
b34d0 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  ull",.     /*  7
b34e0 33 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20 20  3 */ "Ne",.     
b34f0 2f 2a 20 20 37 34 20 2a 2f 20 22 45 71 22 2c 0a  /*  74 */ "Eq",.
b3500 20 20 20 20 20 2f 2a 20 20 37 35 20 2a 2f 20 22       /*  75 */ "
b3510 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 36  Gt",.     /*  76
b3520 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20 2f   */ "Le",.     /
b3530 2a 20 20 37 37 20 2a 2f 20 22 4c 74 22 2c 0a 20  *  77 */ "Lt",. 
b3540 20 20 20 20 2f 2a 20 20 37 38 20 2a 2f 20 22 47      /*  78 */ "G
b3550 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 39 20  e",.     /*  79 
b3560 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22 2c  */ "MakeRecord",
b3570 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20  .     /*  80 */ 
b3580 22 42 69 74 41 6e 64 22 2c 0a 20 20 20 20 20 2f  "BitAnd",.     /
b3590 2a 20 20 38 31 20 2a 2f 20 22 42 69 74 4f 72 22  *  81 */ "BitOr"
b35a0 2c 0a 20 20 20 20 20 2f 2a 20 20 38 32 20 2a 2f  ,.     /*  82 */
b35b0 20 22 53 68 69 66 74 4c 65 66 74 22 2c 0a 20 20   "ShiftLeft",.  
b35c0 20 20 20 2f 2a 20 20 38 33 20 2a 2f 20 22 53 68     /*  83 */ "Sh
b35d0 69 66 74 52 69 67 68 74 22 2c 0a 20 20 20 20 20  iftRight",.     
b35e0 2f 2a 20 20 38 34 20 2a 2f 20 22 41 64 64 22 2c  /*  84 */ "Add",
b35f0 0a 20 20 20 20 20 2f 2a 20 20 38 35 20 2a 2f 20  .     /*  85 */ 
b3600 22 53 75 62 74 72 61 63 74 22 2c 0a 20 20 20 20  "Subtract",.    
b3610 20 2f 2a 20 20 38 36 20 2a 2f 20 22 4d 75 6c 74   /*  86 */ "Mult
b3620 69 70 6c 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20  iply",.     /*  
b3630 38 37 20 2a 2f 20 22 44 69 76 69 64 65 22 2c 0a  87 */ "Divide",.
b3640 20 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f 20 22       /*  88 */ "
b3650 52 65 6d 61 69 6e 64 65 72 22 2c 0a 20 20 20 20  Remainder",.    
b3660 20 2f 2a 20 20 38 39 20 2a 2f 20 22 43 6f 6e 63   /*  89 */ "Conc
b3670 61 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30  at",.     /*  90
b3680 20 2a 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c   */ "ResultRow",
b3690 0a 20 20 20 20 20 2f 2a 20 20 39 31 20 2a 2f 20  .     /*  91 */ 
b36a0 22 44 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f  "Delete",.     /
b36b0 2a 20 20 39 32 20 2a 2f 20 22 41 67 67 46 69 6e  *  92 */ "AggFin
b36c0 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 33  al",.     /*  93
b36d0 20 2a 2f 20 22 42 69 74 4e 6f 74 22 2c 0a 20 20   */ "BitNot",.  
b36e0 20 20 20 2f 2a 20 20 39 34 20 2a 2f 20 22 53 74     /*  94 */ "St
b36f0 72 69 6e 67 38 22 2c 0a 20 20 20 20 20 2f 2a 20  ring8",.     /* 
b3700 20 39 35 20 2a 2f 20 22 43 6f 6d 70 61 72 65 22   95 */ "Compare"
b3710 2c 0a 20 20 20 20 20 2f 2a 20 20 39 36 20 2a 2f  ,.     /*  96 */
b3720 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20 2f 2a   "Goto",.     /*
b3730 20 20 39 37 20 2a 2f 20 22 54 61 62 6c 65 4c 6f    97 */ "TableLo
b3740 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38  ck",.     /*  98
b3750 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20 20 20   */ "Clear",.   
b3760 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22 56 65 72    /*  99 */ "Ver
b3770 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20  ifyCookie",.    
b3780 20 2f 2a 20 31 30 30 20 2a 2f 20 22 41 67 67 53   /* 100 */ "AggS
b3790 74 65 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  tep",.     /* 10
b37a0 31 20 2a 2f 20 22 53 65 74 4e 75 6d 43 6f 6c 75  1 */ "SetNumColu
b37b0 6d 6e 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  mns",.     /* 10
b37c0 32 20 2a 2f 20 22 54 72 61 6e 73 61 63 74 69 6f  2 */ "Transactio
b37d0 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 20  n",.     /* 103 
b37e0 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c 0a 20 20  */ "VFilter",.  
b37f0 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 56 44     /* 104 */ "VD
b3800 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a  estroy",.     /*
b3810 20 31 30 35 20 2a 2f 20 22 43 6f 6e 74 65 78 74   105 */ "Context
b3820 50 6f 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  Pop",.     /* 10
b3830 36 20 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 20 20  6 */ "Next",.   
b3840 20 20 2f 2a 20 31 30 37 20 2a 2f 20 22 43 6f 75    /* 107 */ "Cou
b3850 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38  nt",.     /* 108
b3860 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74 22 2c   */ "IdxInsert",
b3870 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 2a 2f 20  .     /* 109 */ 
b3880 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 20 2f  "SeekGe",.     /
b3890 2a 20 31 31 30 20 2a 2f 20 22 49 6e 73 65 72 74  * 110 */ "Insert
b38a0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a  ",.     /* 111 *
b38b0 2f 20 22 44 65 73 74 72 6f 79 22 2c 0a 20 20 20  / "Destroy",.   
b38c0 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 52 65 61    /* 112 */ "Rea
b38d0 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f  dCookie",.     /
b38e0 2a 20 31 31 33 20 2a 2f 20 22 52 6f 77 53 65 74  * 113 */ "RowSet
b38f0 54 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Test",.     /* 1
b3900 31 34 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 6c 79  14 */ "LoadAnaly
b3910 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  sis",.     /* 11
b3920 35 20 2a 2f 20 22 45 78 70 6c 61 69 6e 22 2c 0a  5 */ "Explain",.
b3930 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 22       /* 116 */ "
b3940 48 61 6c 74 49 66 4e 75 6c 6c 22 2c 0a 20 20 20  HaltIfNull",.   
b3950 20 20 2f 2a 20 31 31 37 20 2a 2f 20 22 4f 70 65    /* 117 */ "Ope
b3960 6e 50 73 65 75 64 6f 22 2c 0a 20 20 20 20 20 2f  nPseudo",.     /
b3970 2a 20 31 31 38 20 2a 2f 20 22 4f 70 65 6e 45 70  * 118 */ "OpenEp
b3980 68 65 6d 65 72 61 6c 22 2c 0a 20 20 20 20 20 2f  hemeral",.     /
b3990 2a 20 31 31 39 20 2a 2f 20 22 4e 75 6c 6c 22 2c  * 119 */ "Null",
b39a0 0a 20 20 20 20 20 2f 2a 20 31 32 30 20 2a 2f 20  .     /* 120 */ 
b39b0 22 4d 6f 76 65 22 2c 0a 20 20 20 20 20 2f 2a 20  "Move",.     /* 
b39c0 31 32 31 20 2a 2f 20 22 42 6c 6f 62 22 2c 0a 20  121 */ "Blob",. 
b39d0 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 52      /* 122 */ "R
b39e0 65 77 69 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20  ewind",.     /* 
b39f0 31 32 33 20 2a 2f 20 22 53 65 65 6b 47 74 22 2c  123 */ "SeekGt",
b3a00 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f 20  .     /* 124 */ 
b3a10 22 56 42 65 67 69 6e 22 2c 0a 20 20 20 20 20 2f  "VBegin",.     /
b3a20 2a 20 31 32 35 20 2a 2f 20 22 56 55 70 64 61 74  * 125 */ "VUpdat
b3a30 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36 20  e",.     /* 126 
b3a40 2a 2f 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20 20  */ "IfZero",.   
b3a50 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 43 72    /* 127 */ "VCr
b3a60 65 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31  eate",.     /* 1
b3a70 32 38 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20  28 */ "Found",. 
b3a80 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22 49      /* 129 */ "I
b3a90 66 50 6f 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31  fPos",.     /* 1
b3aa0 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 20 20  30 */ "Real",.  
b3ab0 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 22 4e 75     /* 131 */ "Nu
b3ac0 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20  llRow",.     /* 
b3ad0 31 33 32 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20  132 */ "Jump",. 
b3ae0 20 20 20 20 2f 2a 20 31 33 33 20 2a 2f 20 22 50      /* 133 */ "P
b3af0 65 72 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20 20  ermutation",.   
b3b00 20 20 2f 2a 20 31 33 34 20 2a 2f 20 22 4e 6f 74    /* 134 */ "Not
b3b10 55 73 65 64 5f 31 33 34 22 2c 0a 20 20 20 20 20  Used_134",.     
b3b20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e 6f 74 55 73  /* 135 */ "NotUs
b3b30 65 64 5f 31 33 35 22 2c 0a 20 20 20 20 20 2f 2a  ed_135",.     /*
b3b40 20 31 33 36 20 2a 2f 20 22 4e 6f 74 55 73 65 64   136 */ "NotUsed
b3b50 5f 31 33 36 22 2c 0a 20 20 20 20 20 2f 2a 20 31  _136",.     /* 1
b3b60 33 37 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31  37 */ "NotUsed_1
b3b70 33 37 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 38  37",.     /* 138
b3b80 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 38   */ "NotUsed_138
b3b90 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 39 20 2a  ",.     /* 139 *
b3ba0 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 39 22 2c  / "NotUsed_139",
b3bb0 0a 20 20 20 20 20 2f 2a 20 31 34 30 20 2a 2f 20  .     /* 140 */ 
b3bc0 22 4e 6f 74 55 73 65 64 5f 31 34 30 22 2c 0a 20  "NotUsed_140",. 
b3bd0 20 20 20 20 2f 2a 20 31 34 31 20 2a 2f 20 22 54      /* 141 */ "T
b3be0 6f 54 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20  oText",.     /* 
b3bf0 31 34 32 20 2a 2f 20 22 54 6f 42 6c 6f 62 22 2c  142 */ "ToBlob",
b3c00 0a 20 20 20 20 20 2f 2a 20 31 34 33 20 2a 2f 20  .     /* 143 */ 
b3c10 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a 20 20 20  "ToNumeric",.   
b3c20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 22 54 6f 49    /* 144 */ "ToI
b3c30 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 35  nt",.     /* 145
b3c40 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c 0a 20 20   */ "ToReal",.  
b3c50 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61 7a 4e 61  };.  return azNa
b3c60 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64 69 66 0a  me[i];.}.#endif.
b3c70 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b3c80 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e   End of opcodes.
b3c90 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
b3ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b3cc0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b3cd0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 6f   Begin file os_o
b3ce0 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s2.c ***********
b3cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b3d10 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46 65 62 20  ./*.** 2006 Feb 
b3d20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  14.**.** The aut
b3d30 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
b3d40 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
b3d50 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
b3d60 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
b3d70 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
b3d80 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
b3d90 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
b3da0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
b3db0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
b3dc0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
b3dd0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
b3de0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
b3df0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
b3e00 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
b3e10 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
b3e20 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
b3e30 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
b3e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3e80 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
b3e90 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
b3ea0 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65  code that is spe
b3eb0 63 69 66 69 63 20 74 6f 20 4f 53 2f 32 2e 0a 2a  cific to OS/2..*
b3ec0 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 6f 73 32  *.** $Id: os_os2
b3ed0 2e 63 2c 76 20 31 2e 36 33 20 32 30 30 38 2f 31  .c,v 1.63 2008/1
b3ee0 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 20 64 72  2/10 19:26:24 dr
b3ef0 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66  h Exp $.*/...#if
b3f00 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 0a   SQLITE_OS_OS2..
b3f10 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f  /*.** A Note Abo
b3f20 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61  ut Memory Alloca
b3f30 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tion:.**.** This
b3f40 20 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c   driver uses mal
b3f50 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72  loc()/free() dir
b3f60 65 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61  ectly rather tha
b3f70 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a  n going through.
b3f80 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72  ** the SQLite-wr
b3f90 61 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d  appers sqlite3_m
b3fa0 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f  alloc()/sqlite3_
b3fb0 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77  free().  Those w
b3fc0 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64  rappers.** are d
b3fd0 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20  esigned for use 
b3fe0 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74  on embedded syst
b3ff0 65 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79  ems where memory
b4000 20 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a   is scarce and.*
b4010 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  * malloc failure
b4020 73 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e  s happen frequen
b4030 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f 65 73 20  tly.  OS/2 does 
b4040 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75  not typically ru
b4050 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64  n on.** embedded
b4060 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68   systems, and wh
b4070 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64  en it does the d
b4080 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c  evelopers normal
b4090 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a  ly have bigger.*
b40a0 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f  * problems to wo
b40b0 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72  rry about than r
b40c0 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65  unning out of me
b40d0 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20  mory.  So there 
b40e0 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70  is not.** a comp
b40f0 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75  elling need to u
b4100 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e  se the wrappers.
b4110 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65  .**.** But there
b4120 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f   is a good reaso
b4130 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65  n to not use the
b4140 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77   wrappers.  If w
b4150 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61  e use the.** wra
b4160 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69  ppers then we wi
b4170 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64  ll get simulated
b4180 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
b4190 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a  es within this.*
b41a0 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74  * driver.  And t
b41b0 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b  hat causes all k
b41c0 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73  inds of problems
b41d0 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20   for our tests. 
b41e0 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68   We.** could enh
b41f0 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64  ance SQLite to d
b4200 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74  eal with simulat
b4210 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ed malloc failur
b4220 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65  es within.** the
b4230 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20   OS driver, but 
b4240 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c  the code to deal
b4250 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c   with those fail
b4260 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a  ure would not.**
b4270 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e   be exercised on
b4280 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f   Linux (which do
b4290 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d  es not need to m
b42a0 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64  alloc() in the d
b42b0 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f  river).** and so
b42c0 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64   we would have d
b42d0 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e  ifficulty writin
b42e0 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73  g coverage tests
b42f0 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64   for that.** cod
b4300 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65  e.  Better to le
b4310 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74  ave the code out
b4320 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a  , we think..**.*
b4330 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74  * The point of t
b4340 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69  his discussion i
b4350 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57  s as follows:  W
b4360 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e  hen creating a n
b4370 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66  ew.** OS layer f
b4380 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73  or an embedded s
b4390 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73  ystem, if you us
b43a0 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61  e this file as a
b43b0 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76  n example,.** av
b43c0 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d  oid the use of m
b43d0 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20  alloc()/free(). 
b43e0 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20   Those routines 
b43f0 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53 2f 32 0a  work ok on OS/2.
b4400 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 74 20  ** desktops but 
b4410 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e 20 65  not so well in e
b4420 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 2e  mbedded systems.
b4430 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  .*/../*.** Macro
b4440 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
b4450 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
b4460 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 61 64  ot to use thread
b4470 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  s..*/.#if define
b4480 64 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  d(SQLITE_THREADS
b4490 41 46 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54  AFE) && SQLITE_T
b44a0 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69  HREADSAFE.# defi
b44b0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32 5f 54 48  ne SQLITE_OS2_TH
b44c0 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a  READS 1.#endif..
b44d0 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f  /*.** Include co
b44e0 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f  de that is commo
b44f0 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20  n to all os_*.c 
b4500 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  files.*/./******
b4510 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
b4520 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20   os_common.h in 
b4530 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73  the middle of os
b4540 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _os2.c *********
b4550 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
b4560 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
b4570 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20  ile os_common.h 
b4580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b45a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
b45b0 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a  004 May 22.**.**
b45c0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
b45d0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
b45e0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
b45f0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
b4600 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
b4610 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
b4620 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
b4630 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
b4640 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
b4650 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
b4660 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
b4670 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
b4680 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
b4690 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
b46a0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
b46b0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
b46c0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
b46d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b46e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b46f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
b4720 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
b4730 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61  ontains macros a
b4740 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20  nd a little bit 
b4750 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  of code that is 
b4760 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c  common to.** all
b4770 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d   of the platform
b4780 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 73 20  -specific files 
b4790 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20  (os_*.c) and is 
b47a0 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74  #included into t
b47b0 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a  hose.** files..*
b47c0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73  *.** This file s
b47d0 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64  hould be #includ
b47e0 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63  ed by the os_*.c
b47f0 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74   files only.  It
b4800 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e   is not a.** gen
b4810 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61  eral purpose hea
b4820 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  der file..**.** 
b4830 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  $Id: os_common.h
b4840 2c 76 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f  ,v 1.38 2009/02/
b4850 32 34 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69  24 18:40:50 dani
b4860 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
b4870 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d  .#ifndef _OS_COM
b4880 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  MON_H_.#define _
b4890 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a  OS_COMMON_H_../*
b48a0 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f  .** At least two
b48b0 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 70   bugs have slipp
b48c0 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 65  ed in because we
b48d0 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d   changed the MEM
b48e0 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63  ORY_DEBUG.** mac
b48f0 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42  ro to SQLITE_DEB
b4900 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65  UG and some olde
b4910 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65  r makefiles have
b4920 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68   not yet made th
b4930 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68  e.** switch.  Th
b4940 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65  e following code
b4950 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68   should catch th
b4960 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f  is problem at co
b4970 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23  mpile-time..*/.#
b4980 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42  ifdef MEMORY_DEB
b4990 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20  UG.# error "The 
b49a0 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63  MEMORY_DEBUG mac
b49b0 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20  ro is obsolete. 
b49c0 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55   Use SQLITE_DEBU
b49d0 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64  G instead.".#end
b49e0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
b49f0 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50  E_DEBUG.SQLITE_P
b4a00 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b4a10 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23  e3OSTrace = 0;.#
b4a20 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28  define OSTRACE1(
b4a30 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20 73  X)         if( s
b4a40 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
b4a50 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
b4a60 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53  tf(X).#define OS
b4a70 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20 20  TRACE2(X,Y)     
b4a80 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
b4a90 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
b4aa0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23  bugPrintf(X,Y).#
b4ab0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28  define OSTRACE3(
b4ac0 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73  X,Y,Z)     if( s
b4ad0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
b4ae0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
b4af0 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e  tf(X,Y,Z).#defin
b4b00 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a  e OSTRACE4(X,Y,Z
b4b10 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74 65  ,A)   if( sqlite
b4b20 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
b4b30 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
b4b40 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f  Y,Z,A).#define O
b4b50 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c  STRACE5(X,Y,Z,A,
b4b60 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53  B) if( sqlite3OS
b4b70 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
b4b80 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
b4b90 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,A,B).#define OS
b4ba0 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE6(X,Y,Z,A,B
b4bb0 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c  ,C) \.    if(sql
b4bc0 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c  ite3OSTrace) sql
b4bd0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
b4be0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65  X,Y,Z,A,B,C).#de
b4bf0 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c  fine OSTRACE7(X,
b4c00 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20  Y,Z,A,B,C,D) \. 
b4c10 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54     if(sqlite3OST
b4c20 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
b4c30 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
b4c40 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64  ,B,C,D).#else.#d
b4c50 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58  efine OSTRACE1(X
b4c60 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
b4c70 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20  E2(X,Y).#define 
b4c80 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a  OSTRACE3(X,Y,Z).
b4c90 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34  #define OSTRACE4
b4ca0 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e  (X,Y,Z,A).#defin
b4cb0 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a  e OSTRACE5(X,Y,Z
b4cc0 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,A,B).#define OS
b4cd0 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE6(X,Y,Z,A,B
b4ce0 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,C).#define OSTR
b4cf0 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE7(X,Y,Z,A,B,C
b4d00 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ,D).#endif../*.*
b4d10 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72  * Macros for per
b4d20 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67  formance tracing
b4d30 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e  .  Normally turn
b4d40 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f  ed off.  Only wo
b4d50 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68  rks.** on i486 h
b4d60 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64  ardware..*/.#ifd
b4d70 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52  ef SQLITE_PERFOR
b4d80 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20  MANCE_TRACE../* 
b4d90 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e  .** hwtime.h con
b4da0 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73  tains inline ass
b4db0 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20  embler code for 
b4dc0 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a  implementing .**
b4dd0 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63   high-performanc
b4de0 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65  e timing routine
b4df0 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  s..*/./*********
b4e00 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77  ***** Include hw
b4e10 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69  time.h in the mi
b4e20 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f  ddle of os_commo
b4e30 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
b4e40 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
b4e50 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
b4e60 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a   hwtime.h ******
b4e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4e90 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
b4ea0 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68   May 27.**.** Th
b4eb0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
b4ec0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
b4ed0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
b4ee0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
b4ef0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
b4f00 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
b4f10 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
b4f20 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
b4f30 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
b4f40 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
b4f50 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
b4f60 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
b4f70 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
b4f80 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
b4f90 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
b4fa0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
b4fb0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
b4fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
b5010 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
b5020 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20  ains inline asm 
b5030 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 76  code for retriev
b5040 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72  ing "high-perfor
b5050 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65  mance".** counte
b5060 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73  rs for x86 class
b5070 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   CPUs..**.** $Id
b5080 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33  : hwtime.h,v 1.3
b5090 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33   2008/08/01 14:3
b50a0 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20 24  3:15 shane Exp $
b50b0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54  .*/.#ifndef _HWT
b50c0 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  IME_H_.#define _
b50d0 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  HWTIME_H_../*.**
b50e0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
b50f0 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b  outine only work
b5100 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61  s on pentium-cla
b5110 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72  ss (or newer) pr
b5120 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20  ocessors..** It 
b5130 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f  uses the RDTSC o
b5140 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68  pcode to read th
b5150 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61  e cycle count va
b5160 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a  lue out of the.*
b5170 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20  * processor and 
b5180 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c  returns that val
b5190 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  ue.  This can be
b51a0 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72   used for high-r
b51b0 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e  es.** profiling.
b51c0 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64  .*/.#if (defined
b51d0 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65  (__GNUC__) || de
b51e0 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29  fined(_MSC_VER))
b51f0 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66   && \.      (def
b5200 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65  ined(i386) || de
b5210 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20  fined(__i386__) 
b5220 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58  || defined(_M_IX
b5230 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69  86))..  #if defi
b5240 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20  ned(__GNUC__).. 
b5250 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
b5260 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
b5270 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
b5280 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
b5290 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f   lo, hi;.     __
b52a0 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
b52b0 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
b52c0 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68  a" (lo), "=d" (h
b52d0 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e  i));.     return
b52e0 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29   (sqlite_uint64)
b52f0 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20  hi << 32 | lo;. 
b5300 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69   }..  #elif defi
b5310 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20  ned(_MSC_VER).. 
b5320 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65   __declspec(nake
b5330 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69  d) __inline sqli
b5340 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63  te_uint64 __cdec
b5350 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  l sqlite3Hwtime(
b5360 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73  void){.     __as
b5370 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73  m {.        rdts
b5380 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20  c.        ret   
b5390 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c      ; return val
b53a0 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20  ue at EDX:EAX.  
b53b0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64     }.  }..  #end
b53c0 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  if..#elif (defin
b53d0 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
b53e0 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34  defined(__x86_64
b53f0 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  __))..  __inline
b5400 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
b5410 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
b5420 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  oid){.      unsi
b5430 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20  gned long val;. 
b5440 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
b5450 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73  olatile__ ("rdts
b5460 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29  c" : "=A" (val))
b5470 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76  ;.      return v
b5480 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20  al;.  }. .#elif 
b5490 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
b54a0 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  _) && defined(__
b54b0 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  ppc__))..  __inl
b54c0 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
b54d0 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
b54e0 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
b54f0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
b5500 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20  g retval;.      
b5510 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75  unsigned long ju
b5520 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  nk;.      __asm_
b5530 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
b5540 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31  "\n\.          1
b5550 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25  :      mftbu   %
b5560 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
b5570 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25         mftb    %
b5580 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  L0\n\.          
b5590 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20          mftbu   
b55a0 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %0\n\.          
b55b0 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20          cmpw    
b55c0 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20  %0,%1\n\.       
b55d0 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20             bne  
b55e0 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20     1b".         
b55f0 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20           : "=r" 
b5600 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28  (retval), "=r" (
b5610 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65  junk));.      re
b5620 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d  turn retval;.  }
b5630 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f  ..#else..  #erro
b5640 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74  r Need implement
b5650 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  ation of sqlite3
b5660 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75  Hwtime() for you
b5670 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f  r platform...  /
b5680 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c  *.  ** To compil
b5690 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d  e without implem
b56a0 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77  enting sqlite3Hw
b56b0 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
b56c0 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79  platform,.  ** y
b56d0 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68  ou can remove th
b56e0 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61  e above #error a
b56f0 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f  nd use the follo
b5700 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66  wing.  ** stub f
b5710 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69  unction.  You wi
b5720 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73  ll lose timing s
b5730 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a  upport for many.
b5740 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75    ** of the debu
b5750 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e  gging and testin
b5760 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74  g utilities, but
b5770 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20   it should at.  
b5780 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65  ** least compile
b5790 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53   and run..  */.S
b57a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
b57b0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
b57c0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
b57d0 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69  ){ return ((sqli
b57e0 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a  te_uint64)0); }.
b57f0 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
b5800 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54  /* !defined(_HWT
b5810 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a  IME_H_) */../***
b5820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
b5830 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a  of hwtime.h ****
b5840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
b5870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
b5880 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
b5890 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63  left off in os_c
b58a0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
b58b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61  **********/..sta
b58c0 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  tic sqlite_uint6
b58d0 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69  4 g_start;.stati
b58e0 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  c sqlite_uint64 
b58f0 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69  g_elapsed;.#defi
b5900 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20  ne TIMER_START  
b5910 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c       g_start=sql
b5920 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65  ite3Hwtime().#de
b5930 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20  fine TIMER_END  
b5940 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64         g_elapsed
b5950 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29  =sqlite3Hwtime()
b5960 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65  -g_start.#define
b5970 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20   TIMER_ELAPSED  
b5980 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c     g_elapsed.#el
b5990 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  se.#define TIMER
b59a0 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54  _START.#define T
b59b0 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65  IMER_END.#define
b59c0 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20   TIMER_ELAPSED  
b59d0 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74     ((sqlite_uint
b59e0 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  64)0).#endif../*
b59f0 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c  .** If we compil
b5a00 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54  e with the SQLIT
b5a10 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74  E_TEST macro set
b5a20 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  , then the follo
b5a30 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66  wing block.** of
b5a40 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20   code will give 
b5a50 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74  us the ability t
b5a60 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73  o simulate a dis
b5a70 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68  k I/O error.  Th
b5a80 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f  is.** is used fo
b5a90 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f  r testing the I/
b5aa0 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63  O recovery logic
b5ab0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
b5ac0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
b5ad0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
b5ae0 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b  o_error_hit = 0;
b5af0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
b5b00 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49  otal number of I
b5b10 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c  /O Errors */.SQL
b5b20 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
b5b30 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72  te3_io_error_har
b5b40 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20  dhit = 0;       
b5b50 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f   /* Number of no
b5b60 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20  n-benign errors 
b5b70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
b5b80 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
b5b90 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20  or_pending = 0; 
b5ba0 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20         /* Count 
b5bb0 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f  down to first I/
b5bc0 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54  O error */.SQLIT
b5bd0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
b5be0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69  3_io_error_persi
b5bf0 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  st = 0;        /
b5c00 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72  * True if I/O er
b5c10 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a  rors persist */.
b5c20 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
b5c30 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
b5c40 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20  benign = 0;     
b5c50 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65      /* True if e
b5c60 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e  rrors are benign
b5c70 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
b5c80 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
b5c90 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b  ull_pending = 0;
b5ca0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
b5cb0 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
b5cc0 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69   = 0;.#define Si
b5cd0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
b5ce0 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69  ign(X) sqlite3_i
b5cf0 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28  o_error_benign=(
b5d00 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  X).#define Simul
b5d10 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29  ateIOError(CODE)
b5d20 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74    \.  if( (sqlit
b5d30 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73  e3_io_error_pers
b5d40 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69  ist && sqlite3_i
b5d50 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20  o_error_hit) \. 
b5d60 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
b5d70 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
b5d80 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20  g-- == 1 )  \.  
b5d90 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f              { lo
b5da0 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44  cal_ioerr(); COD
b5db0 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  E; }.static void
b5dc0 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a   local_ioerr(){.
b5dd0 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52    IOTRACE(("IOER
b5de0 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65  R\n"));.  sqlite
b5df0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b  3_io_error_hit++
b5e00 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
b5e10 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e  _io_error_benign
b5e20 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72   ) sqlite3_io_er
b5e30 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d  ror_hardhit++;.}
b5e40 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
b5e50 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43  eDiskfullError(C
b5e60 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71  ODE) \.   if( sq
b5e70 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
b5e80 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20  ending ){ \.    
b5e90 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73   if( sqlite3_dis
b5ea0 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d  kfull_pending ==
b5eb0 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c   1 ){ \.       l
b5ec0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a  ocal_ioerr(); \.
b5ed0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64         sqlite3_d
b5ee0 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20  iskfull = 1; \. 
b5ef0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f        sqlite3_io
b5f00 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20  _error_hit = 1; 
b5f10 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c  \.       CODE; \
b5f20 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20  .     }else{ \. 
b5f30 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69        sqlite3_di
b5f40 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d  skfull_pending--
b5f50 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20  ; \.     } \.   
b5f60 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  }.#else.#define 
b5f70 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42  SimulateIOErrorB
b5f80 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65  enign(X).#define
b5f90 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
b5fa0 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  (A).#define Simu
b5fb0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
b5fc0 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  r(A).#endif../*.
b5fd0 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c  ** When testing,
b5fe0 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66   keep a count of
b5ff0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f   the number of o
b6000 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69  pen files..*/.#i
b6010 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
b6020 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
b6030 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c  sqlite3_open_fil
b6040 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65  e_count = 0;.#de
b6050 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72  fine OpenCounter
b6060 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65  (X)  sqlite3_ope
b6070 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58  n_file_count+=(X
b6080 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ).#else.#define 
b6090 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23  OpenCounter(X).#
b60a0 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
b60b0 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f   !defined(_OS_CO
b60c0 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a  MMON_H_) */../**
b60d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
b60e0 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   of os_common.h 
b60f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
b6120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
b6130 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
b6140 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f   left off in os_
b6150 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os2.c **********
b6160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
b6170 0a 2a 2a 20 54 68 65 20 6f 73 32 46 69 6c 65 20  .** The os2File 
b6180 73 74 72 75 63 74 75 72 65 20 69 73 20 73 75 62  structure is sub
b6190 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33  class of sqlite3
b61a0 5f 66 69 6c 65 20 73 70 65 63 69 66 69 63 20 66  _file specific f
b61b0 6f 72 20 74 68 65 20 4f 53 2f 32 0a 2a 2a 20 70  or the OS/2.** p
b61c0 72 6f 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72  rotability layer
b61d0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
b61e0 75 63 74 20 6f 73 32 46 69 6c 65 20 6f 73 32 46  uct os2File os2F
b61f0 69 6c 65 3b 0a 73 74 72 75 63 74 20 6f 73 32 46  ile;.struct os2F
b6200 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  ile {.  const sq
b6210 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
b6220 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41   *pMethod;  /* A
b6230 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20  lways the first 
b6240 65 6e 74 72 79 20 2a 2f 0a 20 20 48 46 49 4c 45  entry */.  HFILE
b6250 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   h;             
b6260 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66       /* Handle f
b6270 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  or accessing the
b6280 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 2a   file */.  char*
b6290 20 70 61 74 68 54 6f 44 65 6c 3b 20 20 20 20 20   pathToDel;     
b62a0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
b62b0 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 6f  file to delete o
b62c0 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c 4c 20 69 66  n close, NULL if
b62d0 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e   not */.  unsign
b62e0 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65  ed char locktype
b62f0 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c  ;   /* Type of l
b6300 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65  ock currently he
b6310 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20  ld on this file 
b6320 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 4c  */.};..#define L
b6330 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 31 30 4c 20  OCK_TIMEOUT 10L 
b6340 2f 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c  /* the default l
b6350 6f 63 6b 69 6e 67 20 74 69 6d 65 6f 75 74 20 2a  ocking timeout *
b6360 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
b6370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b63a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b63b0 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72  *.** The next gr
b63c0 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20  oup of routines 
b63d0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49 2f  implement the I/
b63e0 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69 66  O methods specif
b63f0 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 71  ied.** by the sq
b6400 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
b6410 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a   object..*******
b6420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6460 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
b6470 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f  Close a file..*/
b6480 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 43  .static int os2C
b6490 6c 6f 73 65 28 20 73 71 6c 69 74 65 33 5f 66 69  lose( sqlite3_fi
b64a0 6c 65 20 2a 69 64 20 29 7b 0a 20 20 41 50 49 52  le *id ){.  APIR
b64b0 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52  ET rc = NO_ERROR
b64c0 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  ;.  os2File *pFi
b64d0 6c 65 3b 0a 20 20 69 66 28 20 69 64 20 26 26 20  le;.  if( id && 
b64e0 28 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  (pFile = (os2Fil
b64f0 65 2a 29 69 64 29 20 21 3d 20 30 20 29 7b 0a 20  e*)id) != 0 ){. 
b6500 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 43 4c     OSTRACE2( "CL
b6510 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  OSE %d\n", pFile
b6520 2d 3e 68 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ->h );.    rc = 
b6530 44 6f 73 43 6c 6f 73 65 28 20 70 46 69 6c 65 2d  DosClose( pFile-
b6540 3e 68 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  >h );.    pFile-
b6550 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c  >locktype = NO_L
b6560 4f 43 4b 3b 0a 20 20 20 20 69 66 28 20 70 46 69  OCK;.    if( pFi
b6570 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 21 3d  le->pathToDel !=
b6580 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72   NULL ){.      r
b6590 63 20 3d 20 44 6f 73 46 6f 72 63 65 44 65 6c 65  c = DosForceDele
b65a0 74 65 28 20 28 50 53 5a 29 70 46 69 6c 65 2d 3e  te( (PSZ)pFile->
b65b0 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20  pathToDel );.   
b65c0 20 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d 3e     free( pFile->
b65d0 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20  pathToDel );.   
b65e0 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f     pFile->pathTo
b65f0 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  Del = NULL;.    
b6600 7d 0a 20 20 20 20 69 64 20 3d 20 30 3b 0a 20 20  }.    id = 0;.  
b6610 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 20 2d    OpenCounter( -
b6620 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  1 );.  }..  retu
b6630 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  rn rc == NO_ERRO
b6640 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  R ? SQLITE_OK : 
b6650 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a  SQLITE_IOERR;.}.
b6660 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
b6670 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74   from a file int
b6680 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74  o a buffer.  Ret
b6690 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
b66a0 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65   all.** bytes we
b66b0 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66  re read successf
b66c0 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f  ully and SQLITE_
b66d0 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e  IOERR if anythin
b66e0 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e  g goes.** wrong.
b66f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
b6700 73 32 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65  s2Read(.  sqlite
b6710 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20  3_file *id,     
b6720 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
b6730 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a  e to read from *
b6740 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  /.  void *pBuf, 
b6750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b6760 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e      /* Write con
b6770 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62  tent into this b
b6780 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61  uffer */.  int a
b6790 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
b67a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
b67b0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f  mber of bytes to
b67c0 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74   read */.  sqlit
b67d0 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20  e3_int64 offset 
b67e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65             /* Be
b67f0 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74  gin reading at t
b6800 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b  his offset */.){
b6810 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f 63  .  ULONG fileLoc
b6820 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 55 4c  ation = 0L;.  UL
b6830 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f 73 32 46 69  ONG got;.  os2Fi
b6840 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32  le *pFile = (os2
b6850 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65  File*)id;.  asse
b6860 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53  rt( id!=0 );.  S
b6870 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
b6880 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
b6890 45 52 52 5f 52 45 41 44 20 29 3b 0a 20 20 4f 53  ERR_READ );.  OS
b68a0 54 52 41 43 45 33 28 20 22 52 45 41 44 20 25 64  TRACE3( "READ %d
b68b0 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69   lock=%d\n", pFi
b68c0 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f  le->h, pFile->lo
b68d0 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20  cktype );.  if( 
b68e0 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28 70 46  DosSetFilePtr(pF
b68f0 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20  ile->h, offset, 
b6900 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c  FILE_BEGIN, &fil
b6910 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f  eLocation) != NO
b6920 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65  _ERROR ){.    re
b6930 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
b6940 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 44 6f 73  R;.  }.  if( Dos
b6950 52 65 61 64 28 20 70 46 69 6c 65 2d 3e 68 2c 20  Read( pFile->h, 
b6960 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 20  pBuf, amt, &got 
b6970 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  ) != NO_ERROR ){
b6980 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
b6990 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20  TE_IOERR_READ;. 
b69a0 20 7d 0a 20 20 69 66 28 20 67 6f 74 20 3d 3d 20   }.  if( got == 
b69b0 28 55 4c 4f 4e 47 29 61 6d 74 20 29 0a 20 20 20  (ULONG)amt ).   
b69c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
b69d0 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20 20 20 20  K;.  else {.    
b69e0 2f 2a 20 55 6e 72 65 61 64 20 70 6f 72 74 69 6f  /* Unread portio
b69f0 6e 73 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20  ns of the input 
b6a00 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a  buffer must be z
b6a10 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20  ero-filled */.  
b6a20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72    memset(&((char
b6a30 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c  *)pBuf)[got], 0,
b6a40 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72   amt-got);.    r
b6a50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
b6a60 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20  RR_SHORT_READ;. 
b6a70 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74   }.}../*.** Writ
b6a80 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75  e data from a bu
b6a90 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65  ffer into a file
b6aa0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
b6ab0 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a  _OK on success.*
b6ac0 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20  * or some other 
b6ad0 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61  error code on fa
b6ae0 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ilure..*/.static
b6af0 20 69 6e 74 20 6f 73 32 57 72 69 74 65 28 0a 20   int os2Write(. 
b6b00 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
b6b10 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
b6b20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 72 69 74   /* File to writ
b6b30 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73  e into */.  cons
b6b40 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20  t void *pBuf,   
b6b50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
b6b60 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 20 77  he bytes to be w
b6b70 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  ritten */.  int 
b6b80 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  amt,            
b6b90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
b6ba0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
b6bb0 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c  o write */.  sql
b6bc0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65  ite3_int64 offse
b6bd0 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t            /* 
b6be0 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20  Offset into the 
b6bf0 66 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77 72  file to begin wr
b6c00 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a 20  iting at */.){. 
b6c10 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f 63 61 74   ULONG fileLocat
b6c20 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 41 50 49 52  ion = 0L;.  APIR
b6c30 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52  ET rc = NO_ERROR
b6c40 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 6f 74 65 3b  ;.  ULONG wrote;
b6c50 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
b6c60 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
b6c70 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ;.  assert( id!=
b6c80 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  0 );.  SimulateI
b6c90 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
b6ca0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54  QLITE_IOERR_WRIT
b6cb0 45 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44  E );.  SimulateD
b6cc0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65  iskfullError( re
b6cd0 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
b6ce0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20   );.  OSTRACE3( 
b6cf0 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25  "WRITE %d lock=%
b6d00 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
b6d10 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
b6d20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 65 74 46  );.  if( DosSetF
b6d30 69 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e 68 2c  ilePtr(pFile->h,
b6d40 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f 42 45   offset, FILE_BE
b6d50 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74 69  GIN, &fileLocati
b6d60 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20  on) != NO_ERROR 
b6d70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
b6d80 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a  LITE_IOERR;.  }.
b6d90 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20    assert( amt>0 
b6da0 29 3b 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 20  );.  while( amt 
b6db0 3e 20 30 20 26 26 0a 20 20 20 20 20 20 20 20 20  > 0 &&.         
b6dc0 28 20 72 63 20 3d 20 44 6f 73 57 72 69 74 65 28  ( rc = DosWrite(
b6dd0 20 70 46 69 6c 65 2d 3e 68 2c 20 28 50 56 4f 49   pFile->h, (PVOI
b6de0 44 29 70 42 75 66 2c 20 61 6d 74 2c 20 26 77 72  D)pBuf, amt, &wr
b6df0 6f 74 65 20 29 20 29 20 3d 3d 20 4e 4f 5f 45 52  ote ) ) == NO_ER
b6e00 52 4f 52 20 26 26 0a 20 20 20 20 20 20 20 20 20  ROR &&.         
b6e10 77 72 6f 74 65 20 3e 20 30 0a 20 20 29 7b 0a 20  wrote > 0.  ){. 
b6e20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b     amt -= wrote;
b6e30 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63  .    pBuf = &((c
b6e40 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65  har*)pBuf)[wrote
b6e50 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  ];.  }..  return
b6e60 20 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f   ( rc != NO_ERRO
b6e70 52 20 7c 7c 20 61 6d 74 20 3e 20 28 69 6e 74 29  R || amt > (int)
b6e80 77 72 6f 74 65 20 29 20 3f 20 53 51 4c 49 54 45  wrote ) ? SQLITE
b6e90 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 54 45 5f 4f  _FULL : SQLITE_O
b6ea0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e  K;.}../*.** Trun
b6eb0 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c  cate an open fil
b6ec0 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64  e to a specified
b6ed0 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20   size.*/.static 
b6ee0 69 6e 74 20 6f 73 32 54 72 75 6e 63 61 74 65 28  int os2Truncate(
b6ef0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
b6f00 64 2c 20 69 36 34 20 6e 42 79 74 65 20 29 7b 0a  d, i64 nByte ){.
b6f10 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f    APIRET rc = NO
b6f20 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c  _ERROR;.  os2Fil
b6f30 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46  e *pFile = (os2F
b6f40 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41  ile*)id;.  OSTRA
b6f50 43 45 33 28 20 22 54 52 55 4e 43 41 54 45 20 25  CE3( "TRUNCATE %
b6f60 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c 65  d %lld\n", pFile
b6f70 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20  ->h, nByte );.  
b6f80 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
b6f90 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
b6fa0 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b  OERR_TRUNCATE );
b6fb0 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46 69  .  rc = DosSetFi
b6fc0 6c 65 53 69 7a 65 28 20 70 46 69 6c 65 2d 3e 68  leSize( pFile->h
b6fd0 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 72 65 74  , nByte );.  ret
b6fe0 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52  urn rc == NO_ERR
b6ff0 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  OR ? SQLITE_OK :
b7000 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52   SQLITE_IOERR_TR
b7010 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69 66 64 65  UNCATE;.}..#ifde
b7020 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
b7030 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75  .** Count the nu
b7040 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63  mber of fullsync
b7050 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e  s and normal syn
b7060 63 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  cs.  This is use
b7070 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61  d to test.** tha
b7080 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c  t syncs and full
b7090 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72 69  syncs are occuri
b70a0 6e 67 20 61 74 20 74 68 65 20 72 69 67 68 74 20  ng at the right 
b70b0 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  times..*/.SQLITE
b70c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
b70d0 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b  _sync_count = 0;
b70e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
b70f0 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63  sqlite3_fullsync
b7100 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64  _count = 0;.#end
b7110 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  if../*.** Make s
b7120 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74  ure all writes t
b7130 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66  o a particular f
b7140 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65  ile are committe
b7150 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74  d to disk..*/.st
b7160 61 74 69 63 20 69 6e 74 20 6f 73 32 53 79 6e 63  atic int os2Sync
b7170 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ( sqlite3_file *
b7180 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 20 29 7b  id, int flags ){
b7190 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
b71a0 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
b71b0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 53  ;.  OSTRACE3( "S
b71c0 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e  YNC %d lock=%d\n
b71d0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69  ", pFile->h, pFi
b71e0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a  le->locktype );.
b71f0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
b7200 53 54 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  ST.  if( flags &
b7210 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c   SQLITE_SYNC_FUL
b7220 4c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  L){.    sqlite3_
b7230 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b  fullsync_count++
b7240 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
b7250 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  sync_count++;.#e
b7260 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 20  ndif.  /* If we 
b7270 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
b7280 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  e SQLITE_NO_SYNC
b7290 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63   flag, then sync
b72a0 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f  ing is a.  ** no
b72b0 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  -op.  */.#ifdef 
b72c0 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20  SQLITE_NO_SYNC. 
b72d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
b72e0 52 28 70 46 69 6c 65 29 3b 0a 20 20 72 65 74 75  R(pFile);.  retu
b72f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65  rn SQLITE_OK;.#e
b7300 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 44 6f 73  lse.  return Dos
b7310 52 65 73 65 74 42 75 66 66 65 72 28 20 70 46 69  ResetBuffer( pFi
b7320 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f 5f 45 52  le->h ) == NO_ER
b7330 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20  ROR ? SQLITE_OK 
b7340 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a  : SQLITE_IOERR;.
b7350 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
b7360 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75  Determine the cu
b7370 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20  rrent size of a 
b7380 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f  file in bytes.*/
b7390 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46  .static int os2F
b73a0 69 6c 65 53 69 7a 65 28 20 73 71 6c 69 74 65 33  ileSize( sqlite3
b73b0 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74  _file *id, sqlit
b73c0 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 20  e3_int64 *pSize 
b73d0 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  ){.  APIRET rc =
b73e0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49 4c   NO_ERROR;.  FIL
b73f0 45 53 54 41 54 55 53 33 20 66 73 74 73 33 46 69  ESTATUS3 fsts3Fi
b7400 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 73 65 74  leInfo;.  memset
b7410 28 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c  (&fsts3FileInfo,
b7420 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33   0, sizeof(fsts3
b7430 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 20 61 73  FileInfo));.  as
b7440 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
b7450 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
b7460 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
b7470 49 4f 45 52 52 5f 46 53 54 41 54 20 29 3b 0a 20  IOERR_FSTAT );. 
b7480 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 46 69   rc = DosQueryFi
b7490 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32 46 69 6c  leInfo( ((os2Fil
b74a0 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49 4c 5f 53  e*)id)->h, FIL_S
b74b0 54 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33 46  TANDARD, &fsts3F
b74c0 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 28  ileInfo, sizeof(
b74d0 46 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b 0a  FILESTATUS3) );.
b74e0 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45    if( rc == NO_E
b74f0 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a 70 53 69  RROR ){.    *pSi
b7500 7a 65 20 3d 20 66 73 74 73 33 46 69 6c 65 49 6e  ze = fsts3FileIn
b7510 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 20 20 72  fo.cbFile;.    r
b7520 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
b7530 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
b7540 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
b7550 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 7d 0a 0a  R_FSTAT;.  }.}..
b7560 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20  /*.** Acquire a 
b7570 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2f 0a  reader lock..*/.
b7580 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65  static int getRe
b7590 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20  adLock( os2File 
b75a0 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46 49 4c 45  *pFile ){.  FILE
b75b0 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a  LOCK  LockArea,.
b75c0 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f              Unlo
b75d0 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45 54  ckArea;.  APIRET
b75e0 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26   res;.  memset(&
b75f0 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  LockArea, 0, siz
b7600 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a  eof(LockArea));.
b7610 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b    memset(&Unlock
b7620 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
b7630 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  UnlockArea));.  
b7640 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
b7650 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
b7660 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e  .  LockArea.lRan
b7670 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  ge = SHARED_SIZE
b7680 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c  ;.  UnlockArea.l
b7690 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 55  Offset = 0L;.  U
b76a0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
b76b0 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20 3d 20 44   = 0L;.  res = D
b76c0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
b76d0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
b76e0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
b76f0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
b7700 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  1L );.  OSTRACE3
b7710 28 20 22 47 45 54 52 45 41 44 4c 4f 43 4b 20 25  ( "GETREADLOCK %
b7720 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69  d res=%d\n", pFi
b7730 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20  le->h, res );.  
b7740 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f  return res;.}../
b7750 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64  *.** Undo a read
b7760 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lock.*/.static i
b7770 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63  nt unlockReadLoc
b7780 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69 64 20 29  k( os2File *id )
b7790 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f  {.  FILELOCK  Lo
b77a0 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20  ckArea,.        
b77b0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a      UnlockArea;.
b77c0 20 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20 20    APIRET res;.  
b77d0 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61  memset(&LockArea
b77e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b  , 0, sizeof(Lock
b77f0 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74  Area));.  memset
b7800 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c  (&UnlockArea, 0,
b7810 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72   sizeof(UnlockAr
b7820 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61  ea));.  LockArea
b7830 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20  .lOffset = 0L;. 
b7840 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65   LockArea.lRange
b7850 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41   = 0L;.  UnlockA
b7860 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48  rea.lOffset = SH
b7870 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 55 6e  ARED_FIRST;.  Un
b7880 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
b7890 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20  = SHARED_SIZE;. 
b78a0 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c   res = DosSetFil
b78b0 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 2c 20 26  eLocks( id->h, &
b78c0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
b78d0 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45  kArea, LOCK_TIME
b78e0 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54  OUT, 1L );.  OST
b78f0 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 2d 52  RACE3( "UNLOCK-R
b7900 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20 68 61 6e  EADLOCK file han
b7910 64 6c 65 3d 25 64 20 72 65 73 3d 25 64 3f 5c 6e  dle=%d res=%d?\n
b7920 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73 20 29 3b  ", id->h, res );
b7930 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d  .  return res;.}
b7940 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65  ../*.** Lock the
b7950 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c   file with the l
b7960 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79  ock specified by
b7970 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74   parameter lockt
b7980 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20  ype - one.** of 
b7990 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
b79a0 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41  *.**     (1) SHA
b79b0 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  RED_LOCK.**     
b79c0 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  (2) RESERVED_LOC
b79d0 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e  K.**     (3) PEN
b79e0 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  DING_LOCK.**    
b79f0 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c   (4) EXCLUSIVE_L
b7a00 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69  OCK.**.** Someti
b7a10 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74  mes when request
b7a20 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61  ing one lock sta
b7a30 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c  te, additional l
b7a40 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72  ock states.** ar
b7a50 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65  e inserted in be
b7a60 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b  tween.  The lock
b7a70 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f  ing might fail o
b7a80 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74  n one of the lat
b7a90 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  er.** transition
b7aa0 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f  s leaving the lo
b7ab0 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65  ck state differe
b7ac0 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20  nt from what it 
b7ad0 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73  started but.** s
b7ae0 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74  till short of it
b7af0 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c  s goal.  The fol
b7b00 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f  lowing chart sho
b7b10 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a  ws the allowed.*
b7b20 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e  * transitions an
b7b30 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69  d the inserted i
b7b40 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74  ntermediate stat
b7b50 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c  es:.**.**    UNL
b7b60 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a  OCKED -> SHARED.
b7b70 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
b7b80 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53  RESERVED.**    S
b7b90 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  HARED -> (PENDIN
b7ba0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
b7bb0 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d  **    RESERVED -
b7bc0 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
b7bd0 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50  XCLUSIVE.**    P
b7be0 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53  ENDING -> EXCLUS
b7bf0 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  IVE.**.** This r
b7c00 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79  outine will only
b7c10 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b   increase a lock
b7c20 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c 6f 63 6b  .  The os2Unlock
b7c30 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72  () routine.** er
b7c40 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61  ases all locks a
b7c50 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72  t once and retur
b7c60 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c  ns us immediatel
b7c70 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  y to locking lev
b7c80 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e  el 0..** It is n
b7c90 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c  ot possible to l
b7ca0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
b7cb0 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20   level one step 
b7cc0 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a  at a time.  You.
b7cd0 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69  ** must go strai
b7ce0 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c  ght to locking l
b7cf0 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69  evel 0..*/.stati
b7d00 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b 28 20 73  c int os2Lock( s
b7d10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
b7d20 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 7b   int locktype ){
b7d30 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
b7d40 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20  TE_OK;       /* 
b7d50 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  Return code from
b7d60 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a   subroutines */.
b7d70 20 20 41 50 49 52 45 54 20 72 65 73 20 3d 20 4e    APIRET res = N
b7d80 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f 2a 20 52  O_ERROR;    /* R
b7d90 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f 53 2f 32  esult of an OS/2
b7da0 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20   lock call */.  
b7db0 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b  int newLocktype;
b7dc0 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46         /* Set pF
b7dd0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f  ile->locktype to
b7de0 20 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f   this value befo
b7df0 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 20  re exiting */.  
b7e00 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f  int gotPendingLo
b7e10 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69  ck = 0;/* True i
b7e20 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20  f we acquired a 
b7e30 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69  PENDING lock thi
b7e40 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46 49 4c 45  s time */.  FILE
b7e50 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a  LOCK  LockArea,.
b7e60 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f              Unlo
b7e70 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32 46 69 6c  ckArea;.  os2Fil
b7e80 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46  e *pFile = (os2F
b7e90 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 6d 73 65  ile*)id;.  memse
b7ea0 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  t(&LockArea, 0, 
b7eb0 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29  sizeof(LockArea)
b7ec0 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c  );.  memset(&Unl
b7ed0 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65  ockArea, 0, size
b7ee0 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b  of(UnlockArea));
b7ef0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
b7f00 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  !=0 );.  OSTRACE
b7f10 34 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 77  4( "LOCK %d %d w
b7f20 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  as %d\n", pFile-
b7f30 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46  >h, locktype, pF
b7f40 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b  ile->locktype );
b7f50 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
b7f60 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63  is already a loc
b7f70 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f  k of this type o
b7f80 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69  r more restricti
b7f90 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f  ve on the.  ** o
b7fa0 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69  s2File, do nothi
b7fb0 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68  ng. Don't use th
b7fc0 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74  e end_lock: exit
b7fd0 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73   path, as.  ** s
b7fe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
b7ff0 65 72 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e  er() hasn't been
b8000 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a   called yet..  *
b8010 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
b8020 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70  ocktype>=locktyp
b8030 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45  e ){.    OSTRACE
b8040 33 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 6f  3( "LOCK %d %d o
b8050 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29  k (already held)
b8060 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c  \n", pFile->h, l
b8070 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 72  ocktype );.    r
b8080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
b8090 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
b80a0 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  sure the locking
b80b0 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72   sequence is cor
b80c0 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65  rect.  */.  asse
b80d0 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
b80e0 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20  ype!=NO_LOCK || 
b80f0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
b8100 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
b8110 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e  t( locktype!=PEN
b8120 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  DING_LOCK );.  a
b8130 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
b8140 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c  =RESERVED_LOCK |
b8150 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  | pFile->locktyp
b8160 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
b8170 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65  ;..  /* Lock the
b8180 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79   PENDING_LOCK by
b8190 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f  te if we need to
b81a0 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44 49   acquire a PENDI
b81b0 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20  NG lock or.  ** 
b81c0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20  a SHARED lock.  
b81d0 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69 72  If we are acquir
b81e0 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ing a SHARED loc
b81f0 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74 69  k, the acquisiti
b8200 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50  on of.  ** the P
b8210 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65  ENDING_LOCK byte
b8220 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20   is temporary.. 
b8230 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70   */.  newLocktyp
b8240 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  e = pFile->lockt
b8250 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  ype;.  if( pFile
b8260 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c  ->locktype==NO_L
b8270 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f  OCK.      || (lo
b8280 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
b8290 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d  E_LOCK && pFile-
b82a0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52  >locktype==RESER
b82b0 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20  VED_LOCK).  ){. 
b82c0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
b82d0 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59  set = PENDING_BY
b82e0 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  TE;.    LockArea
b82f0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
b8300 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
b8310 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55  fset = 0L;.    U
b8320 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
b8330 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f 2a 20 77   = 0L;..    /* w
b8340 61 69 74 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20  ait longer than 
b8350 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 68 65 72  LOCK_TIMEOUT her
b8360 65 20 6e 6f 74 20 74 6f 20 68 61 76 65 20 74 6f  e not to have to
b8370 20 74 72 79 20 6d 75 6c 74 69 70 6c 65 20 74 69   try multiple ti
b8380 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65 73 20 3d  mes */.    res =
b8390 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
b83a0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
b83b0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
b83c0 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20 29 3b 0a  ea, 100L, 0L );.
b83d0 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e      if( res == N
b83e0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
b83f0 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20   gotPendingLock 
b8400 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53 54 52 41  = 1;.      OSTRA
b8410 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 70 65  CE3( "LOCK %d pe
b8420 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65  nding lock boole
b8430 61 6e 20 73 65 74 2e 20 20 72 65 73 3d 25 64 5c  an set.  res=%d\
b8440 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
b8450 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  s );.    }.  }..
b8460 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 73    /* Acquire a s
b8470 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a  hared lock.  */.
b8480 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
b8490 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72  SHARED_LOCK && r
b84a0 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
b84b0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  {.    assert( pF
b84c0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e  ile->locktype==N
b84d0 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65  O_LOCK );.    re
b84e0 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28  s = getReadLock(
b84f0 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20  pFile);.    if( 
b8500 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
b8510 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
b8520 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f  type = SHARED_LO
b8530 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53  CK;.    }.    OS
b8540 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64  TRACE3( "LOCK %d
b8550 20 61 63 71 75 69 72 65 20 73 68 61 72 65 64 20   acquire shared 
b8560 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c  lock. res=%d\n",
b8570 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29   pFile->h, res )
b8580 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
b8590 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c  ire a RESERVED l
b85a0 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ock.  */.  if( l
b85b0 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45  ocktype==RESERVE
b85c0 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d  D_LOCK && res ==
b85d0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
b85e0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
b85f0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
b8600 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c 6f 63  _LOCK );.    Loc
b8610 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
b8620 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20  RESERVED_BYTE;. 
b8630 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e     LockArea.lRan
b8640 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c  ge = 1L;.    Unl
b8650 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
b8660 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
b8670 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
b8680 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53  ;.    res = DosS
b8690 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69  etFileLocks( pFi
b86a0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  le->h, &UnlockAr
b86b0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c  ea, &LockArea, L
b86c0 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20  OCK_TIMEOUT, 0L 
b86d0 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d  );.    if( res =
b86e0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
b86f0 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
b8700 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b  = RESERVED_LOCK;
b8710 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41  .    }.    OSTRA
b8720 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63  CE3( "LOCK %d ac
b8730 71 75 69 72 65 20 72 65 73 65 72 76 65 64 20 6c  quire reserved l
b8740 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c 20  ock. res=%d\n", 
b8750 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
b8760 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
b8770 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  re a PENDING loc
b8780 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
b8790 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
b87a0 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20  _LOCK && res == 
b87b0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
b87c0 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45  newLocktype = PE
b87d0 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20  NDING_LOCK;.    
b87e0 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d  gotPendingLock =
b87f0 20 30 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32   0;.    OSTRACE2
b8800 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69  ( "LOCK %d acqui
b8810 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e  re pending lock.
b8820 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f   pending lock bo
b8830 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e 22 2c  olean unset.\n",
b8840 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 7d   pFile->h );.  }
b8850 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
b8860 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
b8870 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
b8880 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
b8890 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e  LOCK && res == N
b88a0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 61  O_ERROR ){.    a
b88b0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
b88c0 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c  cktype>=SHARED_L
b88d0 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d  OCK );.    res =
b88e0 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28   unlockReadLock(
b88f0 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52  pFile);.    OSTR
b8900 41 43 45 32 28 20 22 75 6e 72 65 61 64 6c 6f 63  ACE2( "unreadloc
b8910 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29  k = %d\n", res )
b8920 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
b8930 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f  Offset = SHARED_
b8940 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63 6b 41  FIRST;.    LockA
b8950 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41  rea.lRange = SHA
b8960 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 55 6e  RED_SIZE;.    Un
b8970 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
b8980 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
b8990 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
b89a0 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  L;.    res = Dos
b89b0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
b89c0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
b89d0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
b89e0 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
b89f0 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20   );.    if( res 
b8a00 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
b8a10 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65       newLocktype
b8a20 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43   = EXCLUSIVE_LOC
b8a30 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  K;.    }else{.  
b8a40 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f      OSTRACE2( "O
b8a50 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64 65 20 3d  S/2 error-code =
b8a60 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a 20   %d\n", res );. 
b8a70 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 6b       getReadLock
b8a80 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20  (pFile);.    }. 
b8a90 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f     OSTRACE3( "LO
b8aa0 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 65 78  CK %d acquire ex
b8ab0 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 72  clusive lock.  r
b8ac0 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  es=%d\n", pFile-
b8ad0 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 0a  >h, res );.  }..
b8ae0 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68    /* If we are h
b8af0 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47  olding a PENDING
b8b00 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74   lock that ought
b8b10 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c   to be released,
b8b20 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61   then.  ** relea
b8b30 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  se it now..  */.
b8b40 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67    if( gotPending
b8b50 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65  Lock && locktype
b8b60 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  ==SHARED_LOCK ){
b8b70 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20 20  .    int r;.    
b8b80 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
b8b90 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41   = 0L;.    LockA
b8ba0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b  rea.lRange = 0L;
b8bb0 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  .    UnlockArea.
b8bc0 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e  lOffset = PENDIN
b8bd0 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f  G_BYTE;.    Unlo
b8be0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
b8bf0 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44 6f 73 53  1L;.    r = DosS
b8c00 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69  etFileLocks( pFi
b8c10 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  le->h, &UnlockAr
b8c20 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c  ea, &LockArea, L
b8c30 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20  OCK_TIMEOUT, 0L 
b8c40 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  );.    OSTRACE3(
b8c50 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 6b   "LOCK %d unlock
b8c60 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69 73 20 73  ing pending/is s
b8c70 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e 22 2c 20  hared. r=%d\n", 
b8c80 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20  pFile->h, r );. 
b8c90 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20   }..  /* Update 
b8ca0 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65  the state of the
b8cb0 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 69   lock has held i
b8cc0 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  n the file descr
b8cd0 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20  iptor then.  ** 
b8ce0 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f  return the appro
b8cf0 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 6f  priate result co
b8d00 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  de..  */.  if( r
b8d10 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
b8d20 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
b8d30 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
b8d40 20 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c 4f     OSTRACE4( "LO
b8d50 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 79  CK FAILED %d try
b8d60 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 67  ing for %d but g
b8d70 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ot %d\n", pFile-
b8d80 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  >h,.            
b8d90 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c    locktype, newL
b8da0 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 72  ocktype );.    r
b8db0 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
b8dc0 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f  .  }.  pFile->lo
b8dd0 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63 6b  cktype = newLock
b8de0 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33  type;.  OSTRACE3
b8df0 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25  ( "LOCK %d now %
b8e00 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
b8e10 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
b8e20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
b8e30 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
b8e40 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
b8e50 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
b8e60 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
b8e70 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
b8e80 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
b8e90 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
b8ea0 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
b8eb0 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74  ock is held, ret
b8ec0 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c  urn.** non-zero,
b8ed0 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e   otherwise zero.
b8ee0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
b8ef0 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  s2CheckReservedL
b8f00 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c  ock( sqlite3_fil
b8f10 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 4f 75 74  e *id, int *pOut
b8f20 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b   ){.  int r = 0;
b8f30 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
b8f40 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
b8f50 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ;.  assert( pFil
b8f60 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46  e!=0 );.  if( pF
b8f70 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52  ile->locktype>=R
b8f80 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a  ESERVED_LOCK ){.
b8f90 20 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20 4f      r = 1;.    O
b8fa0 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57  STRACE3( "TEST W
b8fb0 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f  R-LOCK %d %d (lo
b8fc0 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  cal)\n", pFile->
b8fd0 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  h, r );.  }else{
b8fe0 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c  .    FILELOCK  L
b8ff0 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20  ockArea,.       
b9000 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
b9010 61 3b 0a 20 20 20 20 41 50 49 52 45 54 20 72 63  a;.    APIRET rc
b9020 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 20   = NO_ERROR;.   
b9030 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65   memset(&LockAre
b9040 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63  a, 0, sizeof(Loc
b9050 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 6d 65 6d  kArea));.    mem
b9060 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  set(&UnlockArea,
b9070 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63   0, sizeof(Unloc
b9080 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 4c 6f 63  kArea));.    Loc
b9090 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
b90a0 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20  RESERVED_BYTE;. 
b90b0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e     LockArea.lRan
b90c0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c  ge = 1L;.    Unl
b90d0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
b90e0 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
b90f0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
b9100 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 53 65  ;.    rc = DosSe
b9110 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c  tFileLocks( pFil
b9120 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65  e->h, &UnlockAre
b9130 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f  a, &LockArea, LO
b9140 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29  CK_TIMEOUT, 0L )
b9150 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20  ;.    OSTRACE3( 
b9160 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
b9170 20 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62   lock reserved b
b9180 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c 20 70 46  yte rc=%d\n", pF
b9190 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20 20  ile->h, rc );.  
b91a0 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45    if( rc == NO_E
b91b0 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 41 50  RROR ){.      AP
b91c0 49 52 45 54 20 72 63 75 20 3d 20 4e 4f 5f 45 52  IRET rcu = NO_ER
b91d0 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72 6e 20 63  ROR; /* return c
b91e0 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63 6b 69 6e  ode for unlockin
b91f0 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 6b 41  g */.      LockA
b9200 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
b9210 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61  ;.      LockArea
b9220 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
b9230 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
b9240 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45  Offset = RESERVE
b9250 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 55 6e  D_BYTE;.      Un
b9260 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
b9270 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72 63 75 20  = 1L;.      rcu 
b9280 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
b9290 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
b92a0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
b92b0 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
b92c0 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 20 20 4f  T, 0L );.      O
b92d0 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57  STRACE3( "TEST W
b92e0 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 6b  R-LOCK %d unlock
b92f0 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20 72   reserved byte r
b9300 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
b9310 2c 20 72 63 75 20 29 3b 0a 20 20 20 20 7d 0a 20  , rcu );.    }. 
b9320 20 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20 4e     r = !(rc == N
b9330 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 4f 53  O_ERROR);.    OS
b9340 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52  TRACE3( "TEST WR
b9350 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d  -LOCK %d %d (rem
b9360 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ote)\n", pFile->
b9370 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 20 2a 70  h, r );.  }.  *p
b9380 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65 74 75 72  Out = r;.  retur
b9390 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
b93a0 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20  /*.** Lower the 
b93b0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e  locking level on
b93c0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
b93d0 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e   id to locktype.
b93e0 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75    locktype.** mu
b93f0 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f  st be either NO_
b9400 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c  LOCK or SHARED_L
b9410 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  OCK..**.** If th
b9420 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
b9430 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  of the file desc
b9440 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64  riptor is alread
b9450 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a  y at or below.**
b9460 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
b9470 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68  ocking level, th
b9480 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
b9490 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  no-op..**.** It 
b94a0 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
b94b0 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  for this routine
b94c0 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20   to fail if the 
b94d0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a  second argument.
b94e0 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20  ** is NO_LOCK.  
b94f0 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
b9500 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44  gument is SHARED
b9510 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20  _LOCK then this 
b9520 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74  routine.** might
b9530 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
b9540 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20  OERR;.*/.static 
b9550 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28 20 73  int os2Unlock( s
b9560 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
b9570 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 7b   int locktype ){
b9580 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 6f  .  int type;.  o
b9590 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
b95a0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
b95b0 41 50 49 52 45 54 20 72 63 20 3d 20 53 51 4c 49  APIRET rc = SQLI
b95c0 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 45 54 20  TE_OK;.  APIRET 
b95d0 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a  res = NO_ERROR;.
b95e0 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b    FILELOCK  Lock
b95f0 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Area,.          
b9600 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20    UnlockArea;.  
b9610 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61  memset(&LockArea
b9620 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b  , 0, sizeof(Lock
b9630 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74  Area));.  memset
b9640 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c  (&UnlockArea, 0,
b9650 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72   sizeof(UnlockAr
b9660 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ea));.  assert( 
b9670 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73  pFile!=0 );.  as
b9680 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d  sert( locktype<=
b9690 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
b96a0 20 4f 53 54 52 41 43 45 34 28 20 22 55 4e 4c 4f   OSTRACE4( "UNLO
b96b0 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20  CK %d to %d was 
b96c0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
b96d0 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65   locktype, pFile
b96e0 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ->locktype );.  
b96f0 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f  type = pFile->lo
b9700 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79  cktype;.  if( ty
b9710 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe>=EXCLUSIVE_LO
b9720 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72  CK ){.    LockAr
b9730 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b  ea.lOffset = 0L;
b9740 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52  .    LockArea.lR
b9750 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55  ange = 0L;.    U
b9760 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
b9770 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
b9780 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
b9790 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44  .lRange = SHARED
b97a0 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65 73 20 3d  _SIZE;.    res =
b97b0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
b97c0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
b97d0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
b97e0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
b97f0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
b9800 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
b9810 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
b9820 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  res=%d\n", pFile
b9830 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20  ->h, res );.    
b9840 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
b9850 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 67 65 74  ARED_LOCK && get
b9860 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20  ReadLock(pFile) 
b9870 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  != NO_ERROR ){. 
b9880 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f       /* This sho
b9890 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
b98a0 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77  .  We should alw
b98b0 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20  ays be able to. 
b98c0 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72       ** reacquir
b98d0 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20  e the read lock 
b98e0 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  */.      OSTRACE
b98f0 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f  3( "UNLOCK %d to
b9900 20 25 64 20 67 65 74 52 65 61 64 4c 6f 63 6b 28   %d getReadLock(
b9910 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20 70 46 69  ) failed\n", pFi
b9920 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20  le->h, locktype 
b9930 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
b9940 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
b9950 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  K;.    }.  }.  i
b9960 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45  f( type>=RESERVE
b9970 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f  D_LOCK ){.    Lo
b9980 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
b9990 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65   0L;.    LockAre
b99a0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
b99b0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
b99c0 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44  ffset = RESERVED
b99d0 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63  _BYTE;.    Unloc
b99e0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31  kArea.lRange = 1
b99f0 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  L;.    res = Dos
b9a00 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
b9a10 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
b9a20 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
b9a30 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
b9a40 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33   );.    OSTRACE3
b9a50 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 72 65 73  ( "UNLOCK %d res
b9a60 65 72 76 65 64 20 72 65 73 3d 25 64 5c 6e 22 2c  erved res=%d\n",
b9a70 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29   pFile->h, res )
b9a80 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b  ;.  }.  if( lock
b9a90 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26  type==NO_LOCK &&
b9aa0 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f   type>=SHARED_LO
b9ab0 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20  CK ){.    res = 
b9ac0 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70  unlockReadLock(p
b9ad0 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41  File);.    OSTRA
b9ae0 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20  CE5( "UNLOCK %d 
b9af0 69 73 20 25 64 20 77 61 6e 74 20 25 64 20 72 65  is %d want %d re
b9b00 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s=%d\n", pFile->
b9b10 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b 74 79 70  h, type, locktyp
b9b20 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20  e, res );.  }.  
b9b30 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e  if( type>=PENDIN
b9b40 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f  G_LOCK ){.    Lo
b9b50 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
b9b60 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65   0L;.    LockAre
b9b70 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
b9b80 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
b9b90 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f  ffset = PENDING_
b9ba0 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  BYTE;.    Unlock
b9bb0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c  Area.lRange = 1L
b9bc0 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53  ;.    res = DosS
b9bd0 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69  etFileLocks( pFi
b9be0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  le->h, &UnlockAr
b9bf0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c  ea, &LockArea, L
b9c00 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20  OCK_TIMEOUT, 0L 
b9c10 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  );.    OSTRACE3(
b9c20 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70 65 6e 64   "UNLOCK %d pend
b9c30 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  ing res=%d\n", p
b9c40 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  File->h, res );.
b9c50 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63    }.  pFile->loc
b9c60 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
b9c70 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 55  ;.  OSTRACE3( "U
b9c80 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64 5c  NLOCK %d now %d\
b9c90 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46  n", pFile->h, pF
b9ca0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b  ile->locktype );
b9cb0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
b9cc0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61  ./*.** Control a
b9cd0 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 20  nd query of the 
b9ce0 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65  open file handle
b9cf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
b9d00 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73  os2FileControl(s
b9d10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
b9d20 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70   int op, void *p
b9d30 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20  Arg){.  switch( 
b9d40 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  op ){.    case S
b9d50 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b  QLITE_FCNTL_LOCK
b9d60 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a  STATE: {.      *
b9d70 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 6f  (int*)pArg = ((o
b9d80 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63  s2File*)id)->loc
b9d90 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 4f 53 54  ktype;.      OST
b9da0 52 41 43 45 33 28 20 22 46 43 4e 54 4c 5f 4c 4f  RACE3( "FCNTL_LO
b9db0 43 4b 53 54 41 54 45 20 25 64 20 6c 6f 63 6b 3d  CKSTATE %d lock=
b9dc0 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c 65  %d\n", ((os2File
b9dd0 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32 46  *)id)->h, ((os2F
b9de0 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79  ile*)id)->lockty
b9df0 70 65 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  pe );.      retu
b9e00 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
b9e10 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
b9e20 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d   SQLITE_ERROR;.}
b9e30 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
b9e40 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69  he sector size i
b9e50 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75  n bytes of the u
b9e60 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20  nderlying block 
b9e70 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68  device for.** th
b9e80 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65  e specified file
b9e90 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74  . This is almost
b9ea0 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65   always 512 byte
b9eb0 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a  s, but may be.**
b9ec0 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65   larger for some
b9ed0 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20   devices..**.** 
b9ee0 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75  SQLite code assu
b9ef0 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  mes this functio
b9f00 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49  n cannot fail. I
b9f10 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74  t also assumes t
b9f20 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69  hat.** if two fi
b9f30 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20  les are created 
b9f40 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65  in the same file
b9f50 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72  -system director
b9f60 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74  y (i.e..** a dat
b9f70 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f  abase and its jo
b9f80 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74  urnal file) that
b9f90 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
b9fa0 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20   will be the.** 
b9fb0 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a  same for both..*
b9fc0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
b9fd0 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74  SectorSize(sqlit
b9fe0 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20  e3_file *id){.  
b9ff0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45  return SQLITE_DE
ba000 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a  FAULT_SECTOR_SIZ
ba010 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  E;.}../*.** Retu
ba020 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20 64  rn a vector of d
ba030 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69  evice characteri
ba040 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  stics..*/.static
ba050 20 69 6e 74 20 6f 73 32 44 65 76 69 63 65 43 68   int os2DeviceCh
ba060 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71  aracteristics(sq
ba070 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
ba080 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
ba090 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 72  ./*.** Character
ba0a0 20 73 65 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20   set conversion 
ba0b0 6f 62 6a 65 63 74 73 20 75 73 65 64 20 62 79 20  objects used by 
ba0c0 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f 75 74 69  conversion routi
ba0d0 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 55  nes..*/.static U
ba0e0 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 55 74 66  convObject ucUtf
ba0f0 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 63 6f 6e  8 = NULL; /* con
ba100 76 65 72 74 20 62 65 74 77 65 65 6e 20 55 54 46  vert between UTF
ba110 2d 38 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f 0a  -8 and UCS-2 */.
ba120 73 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65  static UconvObje
ba130 63 74 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b  ct uclCp = NULL;
ba140 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65 74    /* convert bet
ba150 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f 64 65 70  ween local codep
ba160 61 67 65 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f  age and UCS-2 */
ba170 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66  ../*.** Helper f
ba180 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 69 74 69  unction to initi
ba190 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 76 65 72  alize the conver
ba1a0 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72 6f  sion objects fro
ba1b0 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e 0a  m and to UTF-8..
ba1c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
ba1d0 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28  nitUconvObjects(
ba1e0 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 28 20 55   void ){.  if( U
ba1f0 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 6a  niCreateUconvObj
ba200 65 63 74 28 20 55 54 46 5f 38 2c 20 26 75 63 55  ect( UTF_8, &ucU
ba210 74 66 38 20 29 20 21 3d 20 55 4c 53 5f 53 55 43  tf8 ) != ULS_SUC
ba220 43 45 53 53 20 29 0a 20 20 20 20 75 63 55 74 66  CESS ).    ucUtf
ba230 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 66 20 28  8 = NULL;.  if (
ba240 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f   UniCreateUconvO
ba250 62 6a 65 63 74 28 20 28 55 6e 69 43 68 61 72 20  bject( (UniChar 
ba260 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 22 2c 20  *)L"@path=yes", 
ba270 26 75 63 6c 43 70 20 29 20 21 3d 20 55 4c 53 5f  &uclCp ) != ULS_
ba280 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 63  SUCCESS ).    uc
ba290 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f  lCp = NULL;.}../
ba2a0 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63  *.** Helper func
ba2b0 74 69 6f 6e 20 74 6f 20 66 72 65 65 20 74 68 65  tion to free the
ba2c0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65   conversion obje
ba2d0 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20  cts from and to 
ba2e0 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63  UTF-8..*/.static
ba2f0 20 76 6f 69 64 20 66 72 65 65 55 63 6f 6e 76 4f   void freeUconvO
ba300 62 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a  bjects( void ){.
ba310 20 20 69 66 20 28 20 75 63 55 74 66 38 20 29 0a    if ( ucUtf8 ).
ba320 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e 76      UniFreeUconv
ba330 4f 62 6a 65 63 74 28 20 75 63 55 74 66 38 20 29  Object( ucUtf8 )
ba340 3b 0a 20 20 69 66 20 28 20 75 63 6c 43 70 20 29  ;.  if ( uclCp )
ba350 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e  .    UniFreeUcon
ba360 76 4f 62 6a 65 63 74 28 20 75 63 6c 43 70 20 29  vObject( uclCp )
ba370 3b 0a 20 20 75 63 55 74 66 38 20 3d 20 4e 55 4c  ;.  ucUtf8 = NUL
ba380 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c  L;.  uclCp = NUL
ba390 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70  L;.}../*.** Help
ba3a0 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63  er function to c
ba3b0 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 66 69 6c  onvert UTF-8 fil
ba3c0 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 61 6c 20  enames to local 
ba3d0 4f 53 2f 32 20 63 6f 64 65 70 61 67 65 2e 0a 2a  OS/2 codepage..*
ba3e0 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70  * The two-step p
ba3f0 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f  rocess: first co
ba400 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69  nvert the incomi
ba410 6e 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a  ng UTF-8 string.
ba420 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e  ** into UCS-2 an
ba430 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d  d then from UCS-
ba440 32 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  2 to the current
ba450 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 68   codepage..** Th
ba460 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20  e returned char 
ba470 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62  pointer has to b
ba480 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 74  e freed..*/.stat
ba490 69 63 20 63 68 61 72 20 2a 63 6f 6e 76 65 72 74  ic char *convert
ba4a0 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 63 6f  Utf8PathToCp( co
ba4b0 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b 0a  nst char *in ){.
ba4c0 20 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50 61    UniChar tempPa
ba4d0 74 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a  th[CCHMAXPATH];.
ba4e0 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28 63    char *out = (c
ba4f0 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43 43  har *)calloc( CC
ba500 48 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a 0a  HMAXPATH, 1 );..
ba510 20 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20 20    if( !out ).   
ba520 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20   return NULL;.. 
ba530 20 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c 20   if( !ucUtf8 || 
ba540 21 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e 69  !uclCp ).    ini
ba550 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b  tUconvObjects();
ba560 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65  ..  /* determine
ba570 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20   string for the 
ba580 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54  conversion of UT
ba590 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 31  F-8 which is CP1
ba5a0 32 30 38 20 2a 2f 0a 20 20 69 66 28 20 55 6e 69  208 */.  if( Uni
ba5b0 53 74 72 54 6f 55 63 73 28 20 75 63 55 74 66 38  StrToUcs( ucUtf8
ba5c0 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68 61  , tempPath, (cha
ba5d0 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50 41  r *)in, CCHMAXPA
ba5e0 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43  TH ) != ULS_SUCC
ba5f0 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72 6e  ESS ).    return
ba600 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e 76   out; /* if conv
ba610 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72 65  ersion fails, re
ba620 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20 73  turn the empty s
ba630 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 63  tring */..  /* c
ba640 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75  onversion for cu
ba650 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77  rrent codepage w
ba660 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64  hich can be used
ba670 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20   for paths */.  
ba680 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20 75  UniStrFromUcs( u
ba690 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65 6d 70 50  clCp, out, tempP
ba6a0 61 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48 20  ath, CCHMAXPATH 
ba6b0 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75 74  );..  return out
ba6c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65  ;.}../*.** Helpe
ba6d0 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f  r function to co
ba6e0 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d 65 73 20  nvert filenames 
ba6f0 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f 64 65 70  from local codep
ba700 61 67 65 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a  age to UTF-8..**
ba710 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 72   The two-step pr
ba720 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f 6e  ocess: first con
ba730 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e  vert the incomin
ba740 67 20 63 6f 64 65 70 61 67 65 2d 73 70 65 63 69  g codepage-speci
ba750 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e  fic.** string in
ba760 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68 65  to UCS-2 and the
ba770 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f 20  n from UCS-2 to 
ba780 74 68 65 20 63 6f 64 65 70 61 67 65 20 6f 66 20  the codepage of 
ba790 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 72 65  UTF-8..** The re
ba7a0 74 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 6e  turned char poin
ba7b0 74 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 72  ter has to be fr
ba7c0 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  eed..**.** This 
ba7d0 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 6e 2d  function is non-
ba7e0 73 74 61 74 69 63 20 74 6f 20 62 65 20 61 62 6c  static to be abl
ba7f0 65 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e  e to use this in
ba800 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a 2a 2a 20   shell.c and.** 
ba810 73 69 6d 69 6c 61 72 20 61 70 70 6c 69 63 61 74  similar applicat
ba820 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 63  ions that take c
ba830 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 72 67 75  ommand line argu
ba840 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 72 20 2a  ments..*/.char *
ba850 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f 55  convertCpPathToU
ba860 74 66 38 28 20 63 6f 6e 73 74 20 63 68 61 72 20  tf8( const char 
ba870 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72  *in ){.  UniChar
ba880 20 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58   tempPath[CCHMAX
ba890 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f  PATH];.  char *o
ba8a0 75 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c  ut = (char *)cal
ba8b0 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c  loc( CCHMAXPATH,
ba8c0 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75   1 );..  if( !ou
ba8d0 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e  t ).    return N
ba8e0 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55  ULL;..  if( !ucU
ba8f0 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a  tf8 || !uclCp ).
ba900 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a      initUconvObj
ba910 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 63 6f  ects();..  /* co
ba920 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 72  nversion for cur
ba930 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 68  rent codepage wh
ba940 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20  ich can be used 
ba950 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 69  for paths */.  i
ba960 66 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20  f( UniStrToUcs( 
ba970 75 63 6c 43 70 2c 20 74 65 6d 70 50 61 74 68 2c  uclCp, tempPath,
ba980 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48   (char *)in, CCH
ba990 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53  MAXPATH ) != ULS
ba9a0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72  _SUCCESS ).    r
ba9b0 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66  eturn out; /* if
ba9c0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c   conversion fail
ba9d0 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d  s, return the em
ba9e0 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20  pty string */.. 
ba9f0 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 74   /* determine st
baa00 72 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6e  ring for the con
baa10 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d 38  version of UTF-8
baa20 20 77 68 69 63 68 20 69 73 20 43 50 31 32 30 38   which is CP1208
baa30 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d   */.  UniStrFrom
baa40 55 63 73 28 20 75 63 55 74 66 38 2c 20 6f 75 74  Ucs( ucUtf8, out
baa50 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d  , tempPath, CCHM
baa60 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74  AXPATH );..  ret
baa70 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn out;.}../*.*
baa80 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65  * This vector de
baa90 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65  fines all the me
baaa0 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f  thods that can o
baab0 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20  perate on an.** 
baac0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f 72  sqlite3_file for
baad0 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   os2..*/.static 
baae0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
baaf0 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49 6f 4d 65  _methods os2IoMe
bab00 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20  thod = {.  1,   
bab10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bab20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
bab30 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 65 2c 0a   */.  os2Close,.
bab40 20 20 6f 73 32 52 65 61 64 2c 0a 20 20 6f 73 32    os2Read,.  os2
bab50 57 72 69 74 65 2c 0a 20 20 6f 73 32 54 72 75 6e  Write,.  os2Trun
bab60 63 61 74 65 2c 0a 20 20 6f 73 32 53 79 6e 63 2c  cate,.  os2Sync,
bab70 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a 65 2c 0a  .  os2FileSize,.
bab80 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 6f 73 32    os2Lock,.  os2
bab90 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 43 68 65  Unlock,.  os2Che
baba0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a  ckReservedLock,.
babb0 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c    os2FileControl
babc0 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72 53 69 7a  ,.  os2SectorSiz
babd0 65 2c 0a 20 20 6f 73 32 44 65 76 69 63 65 43 68  e,.  os2DeviceCh
babe0 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b  aracteristics.};
babf0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
bac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
bac40 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20  * Here ends the 
bac50 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74  I/O methods that
bac60 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65   form the sqlite
bac70 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
bac80 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ect..**.** The n
bac90 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  ext block of cod
baca0 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
bacb0 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a   VFS methods..**
bacc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bacd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bace0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bacf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bad00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
bad10 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d 70  ** Create a temp
bad20 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20  orary file name 
bad30 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d  in zBuf.  zBuf m
bad40 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67  ust be big enoug
bad50 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 20  h to.** hold at 
bad60 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
bad70 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f 0a   characters..*/.
bad80 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 65  static int getTe
bad90 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c  mpname(int nBuf,
bada0 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20   char *zBuf ){. 
badb0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
badc0 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61  signed char zCha
badd0 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64  rs[] =.    "abcd
bade0 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74  efghijklmnopqrst
badf0 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43  uvwxyz".    "ABC
bae00 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53  DEFGHIJKLMNOPQRS
bae10 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31  TUVWXYZ".    "01
bae20 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e 74  23456789";.  int
bae30 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54   i, j;.  char zT
bae40 65 6d 70 50 61 74 68 42 75 66 5b 33 5d 3b 0a 20  empPathBuf[3];. 
bae50 20 50 53 5a 20 7a 54 65 6d 70 50 61 74 68 20 3d   PSZ zTempPath =
bae60 20 28 50 53 5a 29 26 7a 54 65 6d 70 50 61 74 68   (PSZ)&zTempPath
bae70 42 75 66 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  Buf;.  if( sqlit
bae80 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72  e3_temp_director
bae90 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d 70 50 61  y ){.    zTempPa
baea0 74 68 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d  th = sqlite3_tem
baeb0 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 7d  p_directory;.  }
baec0 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 44 6f  else{.    if( Do
baed0 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22  sScanEnv( (PSZ)"
baee0 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61 74  TEMP", &zTempPat
baef0 68 20 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  h ) ){.      if(
baf00 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53   DosScanEnv( (PS
baf10 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70 50  Z)"TMP", &zTempP
baf20 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 20  ath ) ){.       
baf30 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28   if( DosScanEnv(
baf40 20 28 50 53 5a 29 22 54 4d 50 44 49 52 22 2c 20   (PSZ)"TMPDIR", 
baf50 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a  &zTempPath ) ){.
baf60 20 20 20 20 20 20 20 20 20 20 20 55 4c 4f 4e 47             ULONG
baf70 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30 2c   ulDriveNum = 0,
baf80 20 75 6c 44 72 69 76 65 4d 61 70 20 3d 20 30 3b   ulDriveMap = 0;
baf90 0a 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 51  .           DosQ
bafa0 75 65 72 79 43 75 72 72 65 6e 74 44 69 73 6b 28  ueryCurrentDisk(
bafb0 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26 75   &ulDriveNum, &u
bafc0 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a 20 20 20  lDriveMap );.   
bafd0 20 20 20 20 20 20 20 20 73 70 72 69 6e 74 66 28          sprintf(
bafe0 20 28 63 68 61 72 2a 29 7a 54 65 6d 70 50 61 74   (char*)zTempPat
baff0 68 2c 20 22 25 63 3a 22 2c 20 28 63 68 61 72 29  h, "%c:", (char)
bb000 28 20 27 41 27 20 2b 20 75 6c 44 72 69 76 65 4e  ( 'A' + ulDriveN
bb010 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20 20 20 20  um - 1 ) );.    
bb020 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
bb030 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 53 74 72    }.  }.  /* Str
bb040 69 70 20 6f 66 66 20 61 20 74 72 61 69 6c 69 6e  ip off a trailin
bb050 67 20 73 6c 61 73 68 65 73 20 6f 72 20 62 61 63  g slashes or bac
bb060 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68 65 72 77  kslashes, otherw
bb070 69 73 65 20 77 65 20 77 6f 75 6c 64 20 67 65 74  ise we would get
bb080 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 70 6c 65   *.   * multiple
bb090 20 28 62 61 63 6b 29 73 6c 61 73 68 65 73 20 77   (back)slashes w
bb0a0 68 69 63 68 20 63 61 75 73 65 73 20 44 6f 73 4f  hich causes DosO
bb0b0 70 65 6e 28 29 20 74 6f 20 66 61 69 6c 2e 20 20  pen() to fail.  
bb0c0 20 20 20 20 20 20 20 20 20 20 20 20 2a 0a 20 20              *.  
bb0d0 20 2a 20 54 72 61 69 6c 69 6e 67 20 73 70 61 63   * Trailing spac
bb0e0 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77  es are not allow
bb0f0 65 64 2c 20 65 69 74 68 65 72 2e 20 20 20 20 20  ed, either.     
bb100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb110 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a 20 3d 20         */.  j = 
bb120 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
bb130 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 77 68  zTempPath);.  wh
bb140 69 6c 65 28 20 6a 20 3e 20 30 20 26 26 20 28 20  ile( j > 0 && ( 
bb150 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d  zTempPath[j-1] =
bb160 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 65 6d 70 50  = '\\' || zTempP
bb170 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 2f 27 0a  ath[j-1] == '/'.
bb180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb190 20 20 20 20 7c 7c 20 7a 54 65 6d 70 50 61 74 68      || zTempPath
bb1a0 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 20 29 20 29  [j-1] == ' ' ) )
bb1b0 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 20 7d 0a 20  {.    j--;.  }. 
bb1c0 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d 20 3d 20   zTempPath[j] = 
bb1d0 27 5c 30 27 3b 0a 20 20 69 66 28 20 21 73 71 6c  '\0';.  if( !sql
bb1e0 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
bb1f0 6f 72 79 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ory ){.    char 
bb200 2a 7a 54 65 6d 70 50 61 74 68 55 54 46 20 3d 20  *zTempPathUTF = 
bb210 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f 55  convertCpPathToU
bb220 74 66 38 28 20 7a 54 65 6d 70 50 61 74 68 20 29  tf8( zTempPath )
bb230 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ;.    sqlite3_sn
bb240 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c  printf( nBuf-30,
bb250 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20   zBuf,.         
bb260 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73               "%s
bb270 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46  \\"SQLITE_TEMP_F
bb280 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d  ILE_PREFIX, zTem
bb290 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20 20 20  pPathUTF );.    
bb2a0 66 72 65 65 28 20 7a 54 65 6d 70 50 61 74 68 55  free( zTempPathU
bb2b0 54 46 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  TF );.  }else{. 
bb2c0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
bb2d0 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c 20 7a 42  ntf( nBuf-30, zB
bb2e0 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  uf,.            
bb2f0 20 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22            "%s\\"
bb300 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45  SQLITE_TEMP_FILE
bb310 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61  _PREFIX, zTempPa
bb320 74 68 20 29 3b 0a 20 20 7d 0a 20 20 6a 20 3d 20  th );.  }.  j = 
bb330 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
bb340 20 7a 42 75 66 20 29 3b 0a 20 20 73 71 6c 69 74   zBuf );.  sqlit
bb350 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 20 32  e3_randomness( 2
bb360 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20  0, &zBuf[j] );. 
bb370 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c   for( i = 0; i <
bb380 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b   20; i++, j++ ){
bb390 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28  .    zBuf[j] = (
bb3a0 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75  char)zChars[ ((u
bb3b0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75  nsigned char)zBu
bb3c0 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43  f[j])%(sizeof(zC
bb3d0 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a  hars)-1) ];.  }.
bb3e0 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20    zBuf[j] = 0;. 
bb3f0 20 4f 53 54 52 41 43 45 32 28 20 22 54 45 4d 50   OSTRACE2( "TEMP
bb400 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22   FILENAME: %s\n"
bb410 2c 20 7a 42 75 66 20 29 3b 0a 20 20 72 65 74 75  , zBuf );.  retu
bb420 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
bb430 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72  ../*.** Turn a r
bb440 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65  elative pathname
bb450 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74   into a full pat
bb460 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68  hname.  Write th
bb470 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61  e full.** pathna
bb480 6d 65 20 69 6e 74 6f 20 7a 46 75 6c 6c 5b 5d 2e  me into zFull[].
bb490 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c 6c 20 62    zFull[] will b
bb4a0 65 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d  e at least pVfs-
bb4b0 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62  >mxPathname.** b
bb4c0 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f  ytes in size..*/
bb4d0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46  .static int os2F
bb4e0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73  ullPathname(.  s
bb4f0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
bb500 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  ,          /* Po
bb510 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a  inter to vfs obj
bb520 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ect */.  const c
bb530 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20  har *zRelative, 
bb540 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79       /* Possibly
bb550 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20   relative input 
bb560 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  path */.  int nF
bb570 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  ull,            
bb580 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
bb590 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69   output buffer i
bb5a0 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61  n bytes */.  cha
bb5b0 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20  r *zFull        
bb5c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
bb5d0 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a  ut buffer */.){.
bb5e0 20 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76    char *zRelativ
bb5f0 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  eCp = convertUtf
bb600 38 50 61 74 68 54 6f 43 70 28 20 7a 52 65 6c 61  8PathToCp( zRela
bb610 74 69 76 65 20 29 3b 0a 20 20 63 68 61 72 20 7a  tive );.  char z
bb620 46 75 6c 6c 43 70 5b 43 43 48 4d 41 58 50 41 54  FullCp[CCHMAXPAT
bb630 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 20 63 68 61  H] = "\0";.  cha
bb640 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a 20 20 41  r *zFullUTF;.  A
bb650 50 49 52 45 54 20 72 63 20 3d 20 44 6f 73 51 75  PIRET rc = DosQu
bb660 65 72 79 50 61 74 68 49 6e 66 6f 28 20 7a 52 65  eryPathInfo( zRe
bb670 6c 61 74 69 76 65 43 70 2c 20 46 49 4c 5f 51 55  lativeCp, FIL_QU
bb680 45 52 59 46 55 4c 4c 4e 41 4d 45 2c 20 7a 46 75  ERYFULLNAME, zFu
bb690 6c 6c 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20  llCp,.          
bb6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb6b0 20 20 20 20 20 20 43 43 48 4d 41 58 50 41 54 48        CCHMAXPATH
bb6c0 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 52 65 6c   );.  free( zRel
bb6d0 61 74 69 76 65 43 70 20 29 3b 0a 20 20 7a 46 75  ativeCp );.  zFu
bb6e0 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43  llUTF = convertC
bb6f0 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a 46 75  pPathToUtf8( zFu
bb700 6c 6c 43 70 20 29 3b 0a 20 20 73 71 6c 69 74 65  llCp );.  sqlite
bb710 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 46 75 6c  3_snprintf( nFul
bb720 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55  l, zFull, zFullU
bb730 54 46 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46  TF );.  free( zF
bb740 75 6c 6c 55 54 46 20 29 3b 0a 20 20 72 65 74 75  ullUTF );.  retu
bb750 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  rn rc == NO_ERRO
bb760 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  R ? SQLITE_OK : 
bb770 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a  SQLITE_IOERR;.}.
bb780 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66  ../*.** Open a f
bb790 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
bb7a0 6e 74 20 6f 73 32 4f 70 65 6e 28 0a 20 20 73 71  nt os2Open(.  sq
bb7b0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
bb7c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
bb7d0 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e  ot used */.  con
bb7e0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
bb7f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
bb800 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a  me of the file *
bb810 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
bb820 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20   *id,           
bb830 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 53    /* Write the S
bb840 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c  QLite file handl
bb850 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  e here */.  int 
bb860 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
bb870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
bb880 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a  n mode flags */.
bb890 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73    int *pOutFlags
bb8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb8b0 2f 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e  /* Status return
bb8c0 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48   flags */.){.  H
bb8d0 46 49 4c 45 20 68 3b 0a 20 20 55 4c 4f 4e 47 20  FILE h;.  ULONG 
bb8e0 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 20  ulFileAttribute 
bb8f0 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a 20  = FILE_NORMAL;. 
bb900 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c 61   ULONG ulOpenFla
bb910 67 73 20 3d 20 30 3b 0a 20 20 55 4c 4f 4e 47 20  gs = 0;.  ULONG 
bb920 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20 30 3b 0a  ulOpenMode = 0;.
bb930 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65    os2File *pFile
bb940 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b   = (os2File*)id;
bb950 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e  .  APIRET rc = N
bb960 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47  O_ERROR;.  ULONG
bb970 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 63 68 61   ulAction;.  cha
bb980 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20 20 63 68  r *zNameCp;.  ch
bb990 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 43 43 48 4d  ar zTmpname[CCHM
bb9a0 41 58 50 41 54 48 2b 31 5d 3b 20 20 20 20 2f 2a  AXPATH+1];    /*
bb9b0 20 42 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20   Buffer to hold 
bb9c0 6e 61 6d 65 20 6f 66 20 74 65 6d 70 20 66 69 6c  name of temp fil
bb9d0 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68  e */..  /* If th
bb9e0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
bb9f0 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
bba00 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65  on is NULL, gene
bba10 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d  rate a .  ** tem
bba20 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65  porary file name
bba30 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20   to use .  */.  
bba40 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20  if( !zName ){.  
bba50 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65    int rc = getTe
bba60 6d 70 6e 61 6d 65 28 43 43 48 4d 41 58 50 41 54  mpname(CCHMAXPAT
bba70 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a  H+1, zTmpname);.
bba80 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
bba90 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
bbaa0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
bbab0 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70      zName = zTmp
bbac0 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20 20 6d 65  name;.  }...  me
bbad0 6d 73 65 74 28 20 70 46 69 6c 65 2c 20 30 2c 20  mset( pFile, 0, 
bbae0 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 20 29  sizeof(*pFile) )
bbaf0 3b 0a 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22  ;..  OSTRACE2( "
bbb00 4f 50 45 4e 20 77 61 6e 74 20 25 64 5c 6e 22 2c  OPEN want %d\n",
bbb10 20 66 6c 61 67 73 20 29 3b 0a 0a 20 20 69 66 28   flags );..  if(
bbb20 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
bbb30 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29  OPEN_READWRITE )
bbb40 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65  {.    ulOpenMode
bbb50 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f   |= OPEN_ACCESS_
bbb60 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20 4f  READWRITE;.    O
bbb70 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 72  STRACE1( "OPEN r
bbb80 65 61 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a  ead/write\n" );.
bbb90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f    }else{.    ulO
bbba0 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f  penMode |= OPEN_
bbbb0 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3b  ACCESS_READONLY;
bbbc0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22  .    OSTRACE1( "
bbbd0 4f 50 45 4e 20 72 65 61 64 20 6f 6e 6c 79 5c 6e  OPEN read only\n
bbbe0 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  " );.  }..  if( 
bbbf0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
bbc00 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 20  PEN_CREATE ){.  
bbc10 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d    ulOpenFlags |=
bbc20 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45   OPEN_ACTION_OPE
bbc30 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f 50  N_IF_EXISTS | OP
bbc40 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54 45  EN_ACTION_CREATE
bbc50 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54  _IF_NEW;.    OST
bbc60 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65  RACE1( "OPEN ope
bbc70 6e 20 6e 65 77 2f 63 72 65 61 74 65 5c 6e 22 20  n new/create\n" 
bbc80 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
bbc90 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f  ulOpenFlags |= O
bbca0 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f  PEN_ACTION_OPEN_
bbcb0 49 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 4e  IF_EXISTS | OPEN
bbcc0 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f 49 46 5f  _ACTION_FAIL_IF_
bbcd0 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  NEW;.    OSTRACE
bbce0 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 65 78  1( "OPEN open ex
bbcf0 69 73 74 69 6e 67 5c 6e 22 20 29 3b 0a 20 20 7d  isting\n" );.  }
bbd00 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ..  if( flags & 
bbd10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
bbd20 5f 44 42 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65  _DB ){.    ulOpe
bbd30 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48  nMode |= OPEN_SH
bbd40 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b 0a 20 20  ARE_DENYNONE;.  
bbd50 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
bbd60 4e 20 73 68 61 72 65 20 72 65 61 64 2f 77 72 69  N share read/wri
bbd70 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65  te\n" );.  }else
bbd80 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65  {.    ulOpenMode
bbd90 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44   |= OPEN_SHARE_D
bbda0 45 4e 59 57 52 49 54 45 3b 0a 20 20 20 20 4f 53  ENYWRITE;.    OS
bbdb0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 68  TRACE1( "OPEN sh
bbdc0 61 72 65 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22  are read only\n"
bbdd0 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66   );.  }..  if( f
bbde0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
bbdf0 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
bbe00 20 29 7b 0a 20 20 20 20 63 68 61 72 20 70 61 74   ){.    char pat
bbe10 68 55 74 66 38 5b 43 43 48 4d 41 58 50 41 54 48  hUtf8[CCHMAXPATH
bbe20 5d 3b 0a 23 69 66 64 65 66 20 4e 44 45 42 55 47  ];.#ifdef NDEBUG
bbe30 20 2f 2a 20 77 68 65 6e 20 64 65 62 75 67 67 69   /* when debuggi
bbe40 6e 67 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61  ng we want to ma
bbe50 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 64 65  ke sure it is de
bbe60 6c 65 74 65 64 20 2a 2f 0a 20 20 20 20 75 6c 46  leted */.    ulF
bbe70 69 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 46  ileAttribute = F
bbe80 49 4c 45 5f 48 49 44 44 45 4e 3b 0a 23 65 6e 64  ILE_HIDDEN;.#end
bbe90 69 66 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61  if.    os2FullPa
bbea0 74 68 6e 61 6d 65 28 20 70 56 66 73 2c 20 7a 4e  thname( pVfs, zN
bbeb0 61 6d 65 2c 20 43 43 48 4d 41 58 50 41 54 48 2c  ame, CCHMAXPATH,
bbec0 20 70 61 74 68 55 74 66 38 20 29 3b 0a 20 20 20   pathUtf8 );.   
bbed0 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65   pFile->pathToDe
bbee0 6c 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50  l = convertUtf8P
bbef0 61 74 68 54 6f 43 70 28 20 70 61 74 68 55 74 66  athToCp( pathUtf
bbf00 38 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  8 );.    OSTRACE
bbf10 31 28 20 22 4f 50 45 4e 20 68 69 64 64 65 6e 2f  1( "OPEN hidden/
bbf20 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20  delete on close 
bbf30 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 5c  file attributes\
bbf40 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n" );.  }else{. 
bbf50 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f     pFile->pathTo
bbf60 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  Del = NULL;.    
bbf70 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20  OSTRACE1( "OPEN 
bbf80 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 61 74 74 72  normal file attr
bbf90 69 62 75 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 0a  ibute\n" );.  }.
bbfa0 0a 20 20 2f 2a 20 61 6c 77 61 79 73 20 6f 70 65  .  /* always ope
bbfb0 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 61 63 63 65  n in random acce
bbfc0 73 73 20 6d 6f 64 65 20 66 6f 72 20 70 6f 73 73  ss mode for poss
bbfd0 69 62 6c 79 20 62 65 74 74 65 72 20 73 70 65 65  ibly better spee
bbfe0 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64  d */.  ulOpenMod
bbff0 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f  e |= OPEN_FLAGS_
bc000 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f 70 65 6e  RANDOM;.  ulOpen
bc010 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41  Mode |= OPEN_FLA
bc020 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52  GS_FAIL_ON_ERROR
bc030 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c  ;.  ulOpenMode |
bc040 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 4e 4f 49  = OPEN_FLAGS_NOI
bc050 4e 48 45 52 49 54 3b 0a 0a 20 20 7a 4e 61 6d 65  NHERIT;..  zName
bc060 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  Cp = convertUtf8
bc070 50 61 74 68 54 6f 43 70 28 20 7a 4e 61 6d 65 20  PathToCp( zName 
bc080 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4f 70 65  );.  rc = DosOpe
bc090 6e 28 20 28 50 53 5a 29 7a 4e 61 6d 65 43 70 2c  n( (PSZ)zNameCp,
bc0a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
bc0b0 20 26 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20   &h,.           
bc0c0 20 20 20 20 20 26 75 6c 41 63 74 69 6f 6e 2c 0a       &ulAction,.
bc0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bc0e0 30 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0L,.            
bc0f0 20 20 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62      ulFileAttrib
bc100 75 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ute,.           
bc110 20 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73       ulOpenFlags
bc120 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
bc130 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c 0a 20 20    ulOpenMode,.  
bc140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 50                (P
bc150 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 20  EAOP2)NULL );.  
bc160 66 72 65 65 28 20 7a 4e 61 6d 65 43 70 20 29 3b  free( zNameCp );
bc170 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f  .  if( rc != NO_
bc180 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 4f 53 54  ERROR ){.    OST
bc190 52 41 43 45 37 28 20 22 4f 50 45 4e 20 49 6e 76  RACE7( "OPEN Inv
bc1a0 61 6c 69 64 20 68 61 6e 64 6c 65 20 72 63 3d 25  alid handle rc=%
bc1b0 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20 75 6c 41  d: zName=%s, ulA
bc1c0 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 75 6c 41 74  ction=%#lx, ulAt
bc1d0 74 72 3d 25 23 6c 78 2c 20 75 6c 46 6c 61 67 73  tr=%#lx, ulFlags
bc1e0 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 65 3d 25 23  =%#lx, ulMode=%#
bc1f0 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  lx\n",.         
bc200 20 20 20 20 20 72 63 2c 20 7a 4e 61 6d 65 2c 20       rc, zName, 
bc210 75 6c 41 63 74 69 6f 6e 2c 20 75 6c 46 69 6c 65  ulAction, ulFile
bc220 41 74 74 72 69 62 75 74 65 2c 20 75 6c 4f 70 65  Attribute, ulOpe
bc230 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 65 6e 4d 6f  nFlags, ulOpenMo
bc240 64 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 46  de );.    if( pF
bc250 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29  ile->pathToDel )
bc260 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69  .      free( pFi
bc270 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b  le->pathToDel );
bc280 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68  .    pFile->path
bc290 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20  ToDel = NULL;.  
bc2a0 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51    if( flags & SQ
bc2b0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
bc2c0 49 54 45 20 29 7b 0a 20 20 20 20 20 20 4f 53 54  ITE ){.      OST
bc2d0 52 41 43 45 32 28 20 22 4f 50 45 4e 20 25 64 20  RACE2( "OPEN %d 
bc2e0 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 5c 6e  Invalid handle\n
bc2f0 22 2c 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c  ", ((flags | SQL
bc300 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
bc310 59 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45  Y) & ~SQLITE_OPE
bc320 4e 5f 52 45 41 44 57 52 49 54 45 29 20 29 3b 0a  N_READWRITE) );.
bc330 20 20 20 20 20 20 72 65 74 75 72 6e 20 6f 73 32        return os2
bc340 4f 70 65 6e 28 20 70 56 66 73 2c 20 7a 4e 61 6d  Open( pVfs, zNam
bc350 65 2c 20 69 64 2c 0a 20 20 20 20 20 20 20 20 20  e, id,.         
bc360 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66               ((f
bc370 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50  lags | SQLITE_OP
bc380 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20 7e  EN_READONLY) & ~
bc390 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
bc3a0 57 52 49 54 45 29 2c 0a 20 20 20 20 20 20 20 20  WRITE),.        
bc3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f                pO
bc3c0 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 20 20 7d  utFlags );.    }
bc3d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75  else{.      retu
bc3e0 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  rn SQLITE_CANTOP
bc3f0 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  EN;.    }.  }.. 
bc400 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29   if( pOutFlags )
bc410 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73  {.    *pOutFlags
bc420 20 3d 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54   = flags & SQLIT
bc430 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
bc440 20 3f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   ? SQLITE_OPEN_R
bc450 45 41 44 57 52 49 54 45 20 3a 20 53 51 4c 49 54  EADWRITE : SQLIT
bc460 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b  E_OPEN_READONLY;
bc470 0a 20 20 7d 0a 0a 20 20 70 46 69 6c 65 2d 3e 70  .  }..  pFile->p
bc480 4d 65 74 68 6f 64 20 3d 20 26 6f 73 32 49 6f 4d  Method = &os2IoM
bc490 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e  ethod;.  pFile->
bc4a0 68 20 3d 20 68 3b 0a 20 20 4f 70 65 6e 43 6f 75  h = h;.  OpenCou
bc4b0 6e 74 65 72 28 2b 31 29 3b 0a 20 20 4f 53 54 52  nter(+1);.  OSTR
bc4c0 41 43 45 33 28 20 22 4f 50 45 4e 20 25 64 20 70  ACE3( "OPEN %d p
bc4d0 4f 75 74 46 6c 61 67 73 3d 25 64 5c 6e 22 2c 20  OutFlags=%d\n", 
bc4e0 70 46 69 6c 65 2d 3e 68 2c 20 70 4f 75 74 46 6c  pFile->h, pOutFl
bc4f0 61 67 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ags );.  return 
bc500 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
bc510 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6e  .** Delete the n
bc520 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  amed file..*/.st
bc530 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 6c 65  atic int os2Dele
bc540 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  te(.  sqlite3_vf
bc550 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
bc560 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
bc570 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20  Not used on os2 
bc580 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
bc590 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20  *zFilename,     
bc5a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
bc5b0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64  ame of file to d
bc5c0 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73  elete */.  int s
bc5d0 79 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20  yncDir          
bc5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bc5f0 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e    /* Not used on
bc600 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20 41 50 49   os2 */.){.  API
bc610 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f  RET rc = NO_ERRO
bc620 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  R;.  char *zFile
bc630 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74  nameCp = convert
bc640 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 46  Utf8PathToCp( zF
bc650 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 53 69 6d  ilename );.  Sim
bc660 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
bc670 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
bc680 52 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 72 63  R_DELETE );.  rc
bc690 20 3d 20 44 6f 73 44 65 6c 65 74 65 28 20 28 50   = DosDelete( (P
bc6a0 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29  SZ)zFilenameCp )
bc6b0 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65 6e  ;.  free( zFilen
bc6c0 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52 41  ameCp );.  OSTRA
bc6d0 43 45 32 28 20 22 44 45 4c 45 54 45 20 5c 22 25  CE2( "DELETE \"%
bc6e0 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d  s\"\n", zFilenam
bc6f0 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  e );.  return rc
bc700 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53   == NO_ERROR ? S
bc710 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54  QLITE_OK : SQLIT
bc720 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a  E_IOERR_DELETE;.
bc730 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  }../*.** Check t
bc740 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64  he existance and
bc750 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c   status of a fil
bc760 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
bc770 20 6f 73 32 41 63 63 65 73 73 28 0a 20 20 73 71   os2Access(.  sq
bc780 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
bc790 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75          /* Not u
bc7a0 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20  sed on os2 */.  
bc7b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
bc7c0 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d  ename,    /* Nam
bc7d0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65  e of file to che
bc7e0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  ck */.  int flag
bc7f0 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
bc800 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73    /* Type of tes
bc810 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69  t to make on thi
bc820 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  s file */.  int 
bc830 2a 70 4f 75 74 20 20 20 20 20 20 20 20 20 20 20  *pOut           
bc840 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72        /* Write r
bc850 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29  esults here */.)
bc860 7b 0a 20 20 46 49 4c 45 53 54 41 54 55 53 33 20  {.  FILESTATUS3 
bc870 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 3b  fsts3ConfigInfo;
bc880 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e  .  APIRET rc = N
bc890 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20  O_ERROR;.  char 
bc8a0 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63  *zFilenameCp = c
bc8b0 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f  onvertUtf8PathTo
bc8c0 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b  Cp( zFilename );
bc8d0 0a 0a 20 20 6d 65 6d 73 65 74 28 20 26 66 73 74  ..  memset( &fst
bc8e0 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 30 2c  s3ConfigInfo, 0,
bc8f0 20 73 69 7a 65 6f 66 28 66 73 74 73 33 43 6f 6e   sizeof(fsts3Con
bc900 66 69 67 49 6e 66 6f 29 20 29 3b 0a 20 20 72 63  figInfo) );.  rc
bc910 20 3d 20 44 6f 73 51 75 65 72 79 50 61 74 68 49   = DosQueryPathI
bc920 6e 66 6f 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e  nfo( (PSZ)zFilen
bc930 61 6d 65 43 70 2c 20 46 49 4c 5f 53 54 41 4e 44  ameCp, FIL_STAND
bc940 41 52 44 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ARD,.           
bc950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 66                &f
bc960 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20  sts3ConfigInfo, 
bc970 73 69 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55  sizeof(FILESTATU
bc980 53 33 29 20 29 3b 0a 20 20 66 72 65 65 28 20 7a  S3) );.  free( z
bc990 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20  FilenameCp );.  
bc9a0 4f 53 54 52 41 43 45 34 28 20 22 41 43 43 45 53  OSTRACE4( "ACCES
bc9b0 53 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66  S fsts3ConfigInf
bc9c0 6f 2e 61 74 74 72 46 69 6c 65 3d 25 64 20 66 6c  o.attrFile=%d fl
bc9d0 61 67 73 3d 25 64 20 72 63 3d 25 64 5c 6e 22 2c  ags=%d rc=%d\n",
bc9e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 73 74  .            fst
bc9f0 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74  s3ConfigInfo.att
bca00 72 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 72 63  rFile, flags, rc
bca10 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c   );.  switch( fl
bca20 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ags ){.    case 
bca30 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45  SQLITE_ACCESS_RE
bca40 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  AD:.    case SQL
bca50 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
bca60 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72  S:.      rc = (r
bca70 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a  c == NO_ERROR);.
bca80 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20        OSTRACE3( 
bca90 22 41 43 43 45 53 53 20 25 73 20 61 63 63 65 73  "ACCESS %s acces
bcaa0 73 20 6f 66 20 72 65 61 64 20 61 6e 64 20 65 78  s of read and ex
bcab0 69 73 74 73 20 20 72 63 3d 25 64 5c 6e 22 2c 20  ists  rc=%d\n", 
bcac0 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b  zFilename, rc );
bcad0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
bcae0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
bcaf0 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a  CESS_READWRITE:.
bcb00 20 20 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d        rc = (rc =
bcb10 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 26 26 20 28  = NO_ERROR) && (
bcb20 20 28 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66   (fsts3ConfigInf
bcb30 6f 2e 61 74 74 72 46 69 6c 65 20 26 20 46 49 4c  o.attrFile & FIL
bcb40 45 5f 52 45 41 44 4f 4e 4c 59 29 20 3d 3d 20 30  E_READONLY) == 0
bcb50 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43   );.      OSTRAC
bcb60 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61  E3( "ACCESS %s a
bcb70 63 63 65 73 73 20 6f 66 20 72 65 61 64 2f 77 72  ccess of read/wr
bcb80 69 74 65 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a  ite  rc=%d\n", z
bcb90 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a  Filename, rc );.
bcba0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
bcbb0 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
bcbc0 61 73 73 65 72 74 28 20 21 22 49 6e 76 61 6c 69  assert( !"Invali
bcbd0 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74  d flags argument
bcbe0 22 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74  " );.  }.  *pOut
bcbf0 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20   = rc;.  return 
bcc00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23  SQLITE_OK;.}...#
bcc10 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
bcc20 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
bcc30 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63  N./*.** Interfac
bcc40 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61  es for opening a
bcc50 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
bcc60 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70   finding entry p
bcc70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20  oints.** within 
bcc80 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
bcc90 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20  ry, and closing 
bcca0 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
bccb0 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74  ry..*/./*.** Int
bccc0 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e  erfaces for open
bccd0 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62  ing a shared lib
bcce0 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e  rary, finding en
bccf0 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69  try points.** wi
bcd00 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  thin the shared 
bcd10 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f  library, and clo
bcd20 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  sing the shared 
bcd30 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74  library..*/.stat
bcd40 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 4f 70  ic void *os2DlOp
bcd50 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  en(sqlite3_vfs *
bcd60 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72  pVfs, const char
bcd70 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20   *zFilename){.  
bcd80 55 43 48 41 52 20 6c 6f 61 64 45 72 72 5b 32 35  UCHAR loadErr[25
bcd90 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45 20 68 6d  6];.  HMODULE hm
bcda0 6f 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 3b  od;.  APIRET rc;
bcdb0 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  .  char *zFilena
bcdc0 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74  meCp = convertUt
bcdd0 66 38 50 61 74 68 54 6f 43 70 28 7a 46 69 6c 65  f8PathToCp(zFile
bcde0 6e 61 6d 65 29 3b 0a 20 20 72 63 20 3d 20 44 6f  name);.  rc = Do
bcdf0 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 50 53 5a  sLoadModule((PSZ
bce00 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a 65 6f 66  )loadErr, sizeof
bce10 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46 69 6c 65  (loadErr), zFile
bce20 6e 61 6d 65 43 70 2c 20 26 68 6d 6f 64 29 3b 0a  nameCp, &hmod);.
bce30 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65    free(zFilename
bce40 43 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  Cp);.  return rc
bce50 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30   != NO_ERROR ? 0
bce60 20 3a 20 28 76 6f 69 64 2a 29 68 6d 6f 64 3b 0a   : (void*)hmod;.
bce70 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d 6f 70 20  }./*.** A no-op 
bce80 73 69 6e 63 65 20 74 68 65 20 65 72 72 6f 72 20  since the error 
bce90 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
bcea0 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f 61 64 4d   on the DosLoadM
bceb0 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a 2a 20 6f  odule call..** o
bcec0 73 32 44 6c 6f 70 65 6e 20 72 65 74 75 72 6e 73  s2Dlopen returns
bced0 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c 6f 61 64   zero if DosLoad
bcee0 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 73 75  Module is not su
bcef0 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61  ccessful..*/.sta
bcf00 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 45 72  tic void os2DlEr
bcf10 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
bcf20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c  *pVfs, int nBuf,
bcf30 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b   char *zBufOut){
bcf40 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73  ./* no-op */.}.s
bcf50 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44  tatic void *os2D
bcf60 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73  lSym(sqlite3_vfs
bcf70 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48   *pVfs, void *pH
bcf80 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61  andle, const cha
bcf90 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 50  r *zSymbol){.  P
bcfa0 46 4e 20 70 66 6e 3b 0a 20 20 41 50 49 52 45 54  FN pfn;.  APIRET
bcfb0 20 72 63 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51   rc;.  rc = DosQ
bcfc0 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d  ueryProcAddr((HM
bcfd0 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30  ODULE)pHandle, 0
bcfe0 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e  L, zSymbol, &pfn
bcff0 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e  );.  if( rc != N
bd000 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2f  O_ERROR ){.    /
bd010 2a 20 69 66 20 74 68 65 20 73 79 6d 62 6f 6c 20  * if the symbol 
bd020 69 74 73 65 6c 66 20 77 61 73 20 6e 6f 74 20 66  itself was not f
bd030 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 61 67 61  ound, search aga
bd040 69 6e 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a  in for the same.
bd050 20 20 20 20 20 2a 20 73 79 6d 62 6f 6c 20 77 69       * symbol wi
bd060 74 68 20 61 6e 20 65 78 74 72 61 20 75 6e 64 65  th an extra unde
bd070 72 73 63 6f 72 65 2c 20 74 68 61 74 20 6d 69 67  rscore, that mig
bd080 68 74 20 62 65 20 6e 65 65 64 65 64 20 64 65 70  ht be needed dep
bd090 65 6e 64 69 6e 67 0a 20 20 20 20 20 2a 20 6f 6e  ending.     * on
bd0a0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 6e   the calling con
bd0b0 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 63  vention */.    c
bd0c0 68 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36  har _zSymbol[256
bd0d0 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 20 73 74 72  ] = "_";.    str
bd0e0 6e 63 61 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a  ncat(_zSymbol, z
bd0f0 53 79 6d 62 6f 6c 2c 20 32 35 35 29 3b 0a 20 20  Symbol, 255);.  
bd100 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50    rc = DosQueryP
bd110 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c 45  rocAddr((HMODULE
bd120 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 5f 7a  )pHandle, 0L, _z
bd130 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20  Symbol, &pfn);. 
bd140 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 21   }.  return rc !
bd150 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a  = NO_ERROR ? 0 :
bd160 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0a 7d 0a 73   (void*)pfn;.}.s
bd170 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c  tatic void os2Dl
bd180 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66  Close(sqlite3_vf
bd190 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
bd1a0 48 61 6e 64 6c 65 29 7b 0a 20 20 44 6f 73 46 72  Handle){.  DosFr
bd1b0 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f 44 55 4c  eeModule((HMODUL
bd1c0 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65  E)pHandle);.}.#e
bd1d0 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45  lse /* if SQLITE
bd1e0 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
bd1f0 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a  SION is defined:
bd200 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 6f 73   */.  #define os
bd210 32 44 6c 4f 70 65 6e 20 30 0a 20 20 23 64 65 66  2DlOpen 0.  #def
bd220 69 6e 65 20 6f 73 32 44 6c 45 72 72 6f 72 20 30  ine os2DlError 0
bd230 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c  .  #define os2Dl
bd240 53 79 6d 20 30 0a 20 20 23 64 65 66 69 6e 65 20  Sym 0.  #define 
bd250 6f 73 32 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e  os2DlClose 0.#en
bd260 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  dif.../*.** Writ
bd270 65 20 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74  e up to nBuf byt
bd280 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73  es of randomness
bd290 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73   into zBuf..*/.s
bd2a0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 52 61 6e  tatic int os2Ran
bd2b0 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f  domness(sqlite3_
bd2c0 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
bd2d0 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20  Buf, char *zBuf 
bd2e0 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a  ){.  int n = 0;.
bd2f0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
bd300 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e  TE_TEST).  n = n
bd310 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42  Buf;.  memset(zB
bd320 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65  uf, 0, nBuf);.#e
bd330 6c 73 65 0a 20 20 69 6e 74 20 73 69 7a 65 6f 66  lse.  int sizeof
bd340 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 55  ULong = sizeof(U
bd350 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 20 28 69 6e  LONG);.  if( (in
bd360 74 29 73 69 7a 65 6f 66 28 44 41 54 45 54 49 4d  t)sizeof(DATETIM
bd370 45 29 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29  E) <= nBuf - n )
bd380 7b 0a 20 20 20 20 44 41 54 45 54 49 4d 45 20 78  {.    DATETIME x
bd390 3b 0a 20 20 20 20 44 6f 73 47 65 74 44 61 74 65  ;.    DosGetDate
bd3a0 54 69 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65  Time(&x);.    me
bd3b0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
bd3c0 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20  x, sizeof(x));. 
bd3d0 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78     n += sizeof(x
bd3e0 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 69  );.  }..  if( si
bd3f0 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75  zeofULong <= nBu
bd400 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 50 50 49  f - n ){.    PPI
bd410 42 20 70 70 69 62 3b 0a 20 20 20 20 44 6f 73 47  B ppib;.    DosG
bd420 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 4e 55 4c  etInfoBlocks(NUL
bd430 4c 2c 20 26 70 70 69 62 29 3b 0a 20 20 20 20 6d  L, &ppib);.    m
bd440 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20  emcpy(&zBuf[n], 
bd450 26 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64  &ppib->pib_ulpid
bd460 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a  , sizeofULong);.
bd470 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55      n += sizeofU
bd480 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  Long;.  }..  if(
bd490 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20   sizeofULong <= 
bd4a0 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20  nBuf - n ){.    
bd4b0 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20 20 44  PTIB ptib;.    D
bd4c0 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
bd4d0 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20  &ptib, NULL);.  
bd4e0 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
bd4f0 5d 2c 20 26 70 74 69 62 2d 3e 74 69 62 5f 70 74  ], &ptib->tib_pt
bd500 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 2c  ib2->tib2_ultid,
bd510 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20   sizeofULong);. 
bd520 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c     n += sizeofUL
bd530 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69  ong;.  }..  /* i
bd540 66 20 77 65 20 73 74 69 6c 6c 20 68 61 76 65 6e  f we still haven
bd550 27 74 20 66 69 6c 6c 65 64 20 74 68 65 20 62 75  't filled the bu
bd560 66 66 65 72 20 79 65 74 20 74 68 65 20 66 6f 6c  ffer yet the fol
bd570 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 2a 2f 0a 20  lowing will */. 
bd580 20 2f 2a 20 67 72 61 62 20 65 76 65 72 79 74 68   /* grab everyth
bd590 69 6e 67 20 6f 6e 63 65 20 69 6e 73 74 65 61 64  ing once instead
bd5a0 20 6f 66 20 6d 61 6b 69 6e 67 20 73 65 76 65 72   of making sever
bd5b0 61 6c 20 63 61 6c 6c 73 20 66 6f 72 20 61 20 73  al calls for a s
bd5c0 69 6e 67 6c 65 20 69 74 65 6d 20 2a 2f 0a 20 20  ingle item */.  
bd5d0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20  if( sizeofULong 
bd5e0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20  <= nBuf - n ){. 
bd5f0 20 20 20 55 4c 4f 4e 47 20 75 6c 53 79 73 49 6e     ULONG ulSysIn
bd600 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b 0a 20 20 20  fo[QSV_MAX];.   
bd610 20 44 6f 73 51 75 65 72 79 53 79 73 49 6e 66 6f   DosQuerySysInfo
bd620 28 31 4c 2c 20 51 53 56 5f 4d 41 58 2c 20 75 6c  (1L, QSV_MAX, ul
bd630 53 79 73 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 55  SysInfo, sizeofU
bd640 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d 41 58 29 3b  Long * QSV_MAX);
bd650 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42  ..    memcpy(&zB
bd660 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66  uf[n], &ulSysInf
bd670 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 4e 54 20 2d  o[QSV_MS_COUNT -
bd680 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   1], sizeofULong
bd690 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
bd6a0 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 20 20 69 66  ofULong;..    if
bd6b0 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d  ( sizeofULong <=
bd6c0 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20   nBuf - n ){.   
bd6d0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
bd6e0 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51  n], &ulSysInfo[Q
bd6f0 53 56 5f 54 49 4d 45 52 5f 49 4e 54 45 52 56 41  SV_TIMER_INTERVA
bd700 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c  L - 1], sizeofUL
bd710 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d  ong);.      n +=
bd720 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20   sizeofULong;.  
bd730 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65    }.    if( size
bd740 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20  ofULong <= nBuf 
bd750 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  - n ){.      mem
bd760 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75  cpy(&zBuf[n], &u
bd770 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d  lSysInfo[QSV_TIM
bd780 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 73 69 7a 65  E_LOW - 1], size
bd790 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20  ofULong);.      
bd7a0 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  n += sizeofULong
bd7b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
bd7c0 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e  sizeofULong <= n
bd7d0 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20  Buf - n ){.     
bd7e0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
bd7f0 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56  , &ulSysInfo[QSV
bd800 5f 54 49 4d 45 5f 48 49 47 48 20 2d 20 31 5d 2c  _TIME_HIGH - 1],
bd810 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20   sizeofULong);. 
bd820 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66       n += sizeof
bd830 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20  ULong;.    }.   
bd840 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   if( sizeofULong
bd850 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a   <= nBuf - n ){.
bd860 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42        memcpy(&zB
bd870 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66  uf[n], &ulSysInf
bd880 6f 5b 51 53 56 5f 54 4f 54 41 56 41 49 4c 4d 45  o[QSV_TOTAVAILME
bd890 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c  M - 1], sizeofUL
bd8a0 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d  ong);.      n +=
bd8b0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20   sizeofULong;.  
bd8c0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
bd8d0 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f    return n;.}../
bd8e0 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61  *.** Sleep for a
bd8f0 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20   little while.  
bd900 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e  Return the amoun
bd910 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e  t of time slept.
bd920 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74  .** The argument
bd930 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
bd940 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77  f microseconds w
bd950 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e  e want to sleep.
bd960 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
bd970 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62  alue is the numb
bd980 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e  er of microsecon
bd990 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75  ds of sleep actu
bd9a0 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65  ally.** requeste
bd9b0 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72  d from the under
bd9c0 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20  lying operating 
bd9d0 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72  system, a number
bd9e0 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 20   which.** might 
bd9f0 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  be greater than 
bda00 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
bda10 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f  argument, but no
bda20 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74  t less.** than t
bda30 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  he argument..*/.
bda40 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 6c  static int os2Sl
bda50 65 65 70 28 20 73 71 6c 69 74 65 33 5f 76 66 73  eep( sqlite3_vfs
bda60 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72   *pVfs, int micr
bda70 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73 53 6c 65  osec ){.  DosSle
bda80 65 70 28 20 28 6d 69 63 72 6f 73 65 63 2f 31 30  ep( (microsec/10
bda90 30 30 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  00) );.  return 
bdaa0 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a 0a  microsec;.}../*.
bdab0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
bdac0 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65   variable, if se
bdad0 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  t to a non-zero 
bdae0 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74  value, becomes t
bdaf0 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74  he result.** ret
bdb00 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  urned from sqlit
bdb10 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28  e3OsCurrentTime(
bdb20 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  ).  This is used
bdb30 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f   for testing..*/
bdb40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
bdb50 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
bdb60 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65  nt sqlite3_curre
bdb70 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e  nt_time = 0;.#en
bdb80 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  dif../*.** Find 
bdb90 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65  the current time
bdba0 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43   (in Universal C
bdbb0 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29  oordinated Time)
bdbc0 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20  .  Write the.** 
bdbd0 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64  current time and
bdbe0 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61   date as a Julia
bdbf0 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74  n Day number int
bdc00 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20  o *prNow and.** 
bdc10 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72  return 0.  Retur
bdc20 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20  n 1 if the time 
bdc30 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20  and date cannot 
bdc40 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74  be found..*/.int
bdc50 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 28   os2CurrentTime(
bdc60 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
bdc70 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f  fs, double *prNo
bdc80 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6e 6f  w ){.  double no
bdc90 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69 6e 75 74  w;.  SHORT minut
bdca0 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74 6f 20 62  e; /* needs to b
bdcb0 65 20 61 62 6c 65 20 74 6f 20 63 6f 70 65 20 77  e able to cope w
bdcc0 69 74 68 20 6e 65 67 61 74 69 76 65 20 74 69 6d  ith negative tim
bdcd0 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 2a 2f 0a  ezone offset */.
bdce0 20 20 55 53 48 4f 52 54 20 73 65 63 6f 6e 64 2c    USHORT second,
bdcf0 20 68 6f 75 72 2c 0a 20 20 20 20 20 20 20 20 20   hour,.         
bdd00 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 65 61 72  day, month, year
bdd10 3b 0a 20 20 44 41 54 45 54 49 4d 45 20 64 74 3b  ;.  DATETIME dt;
bdd20 0a 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d  .  DosGetDateTim
bdd30 65 28 20 26 64 74 20 29 3b 0a 20 20 73 65 63 6f  e( &dt );.  seco
bdd40 6e 64 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e  nd = (USHORT)dt.
bdd50 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75 74  seconds;.  minut
bdd60 65 20 3d 20 28 53 48 4f 52 54 29 64 74 2e 6d 69  e = (SHORT)dt.mi
bdd70 6e 75 74 65 73 20 2b 20 64 74 2e 74 69 6d 65 7a  nutes + dt.timez
bdd80 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28 55  one;.  hour = (U
bdd90 53 48 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b 0a  SHORT)dt.hours;.
bdda0 20 20 64 61 79 20 3d 20 28 55 53 48 4f 52 54 29    day = (USHORT)
bddb0 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68 20  dt.day;.  month 
bddc0 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f 6e  = (USHORT)dt.mon
bddd0 74 68 3b 0a 20 20 79 65 61 72 20 3d 20 28 55 53  th;.  year = (US
bdde0 48 4f 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a 20  HORT)dt.year;.. 
bddf0 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e 73   /* Calculations
bde00 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77 77   from http://www
bde10 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e  .astro.keele.ac.
bde20 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d  uk/~rno/Astronom
bde30 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20 20  y/hjd.html.     
bde40 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 6f  http://www.astro
bde50 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e  .keele.ac.uk/~rn
bde60 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2d  o/Astronomy/hjd-
bde70 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43 61  0.1.c */.  /* Ca
bde80 6c 63 75 6c 61 74 65 20 74 68 65 20 4a 75 6c 69  lculate the Juli
bde90 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f 77  an days */.  now
bdea0 20 3d 20 64 61 79 20 2d 20 33 32 30 37 36 20 2b   = day - 32076 +
bdeb0 0a 20 20 20 20 31 34 36 31 2a 28 79 65 61 72 20  .    1461*(year 
bdec0 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68 20  + 4800 + (month 
bded0 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20 20  - 14)/12)/4 +.  
bdee0 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20 32    367*(month - 2
bdef0 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f   - (month - 14)/
bdf00 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20 20  12*12)/12 -.    
bdf10 33 2a 28 28 79 65 61 72 20 2b 20 34 39 30 30 20  3*((year + 4900 
bdf20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31  + (month - 14)/1
bdf30 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f 2a  2)/100)/4;..  /*
bdf40 20 41 64 64 20 74 68 65 20 66 72 61 63 74 69 6f   Add the fractio
bdf50 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73 20  nal hours, mins 
bdf60 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a 20  and seconds */. 
bdf70 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b 20   now += (hour + 
bdf80 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e 6f  12.0)/24.0;.  no
bdf90 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34 30  w += minute/1440
bdfa0 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65 63  .0;.  now += sec
bdfb0 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20 2a  ond/86400.0;.  *
bdfc0 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69 66  prNow = now;.#if
bdfd0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
bdfe0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75    if( sqlite3_cu
bdff0 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20  rrent_time ){.  
be000 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74    *prNow = sqlit
be010 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f  e3_current_time/
be020 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38  86400.0 + 244058
be030 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  7.5;.  }.#endif.
be040 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73    return 0;.}..s
be050 74 61 74 69 63 20 69 6e 74 20 6f 73 32 47 65 74  tatic int os2Get
be060 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65  LastError(sqlite
be070 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
be080 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
be090 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  f){.  return 0;.
be0a0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
be0b0 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61  ize and deinitia
be0c0 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69  lize the operati
be0d0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
be0e0 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ace..*/.SQLITE_A
be0f0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
be100 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20  s_init(void){.  
be110 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76  static sqlite3_v
be120 66 73 20 6f 73 32 56 66 73 20 3d 20 7b 0a 20 20  fs os2Vfs = {.  
be130 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
be140 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
be150 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 6f   */.    sizeof(o
be160 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a  s2File),   /* sz
be170 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 43 43  OsFile */.    CC
be180 48 4d 41 58 50 41 54 48 2c 20 20 20 20 20 20 20  HMAXPATH,       
be190 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a   /* mxPathname *
be1a0 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
be1b0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78           /* pNex
be1c0 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32 22 2c 20  t */.    "os2", 
be1d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a              /* z
be1e0 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Name */.    0,  
be1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
be200 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 0a 20  * pAppData */.. 
be210 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20 20 20     os2Open,     
be220 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a        /* xOpen *
be230 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65 74 65 2c  /.    os2Delete,
be240 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c           /* xDel
be250 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73 32 41 63  ete */.    os2Ac
be260 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a  cess,         /*
be270 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20   xAccess */.    
be280 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c  os2FullPathname,
be290 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e     /* xFullPathn
be2a0 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c  ame */.    os2Dl
be2b0 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a  Open,         /*
be2c0 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20   xDlOpen */.    
be2d0 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20 20 20 20  os2DlError,     
be2e0 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a     /* xDlError *
be2f0 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79 6d 2c 20  /.    os2DlSym, 
be300 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53           /* xDlS
be310 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 43  ym */.    os2DlC
be320 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  lose,        /* 
be330 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20  xDlClose */.    
be340 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20  os2Randomness,  
be350 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73     /* xRandomnes
be360 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53 6c 65 65  s */.    os2Slee
be370 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  p,          /* x
be380 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 6f 73 32  Sleep */.    os2
be390 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20  CurrentTime,    
be3a0 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20  /* xCurrentTime 
be3b0 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74 4c 61 73  */.    os2GetLas
be3c0 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65  tError    /* xGe
be3d0 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20  tLastError */.  
be3e0 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  };.  sqlite3_vfs
be3f0 5f 72 65 67 69 73 74 65 72 28 26 6f 73 32 56 66  _register(&os2Vf
be400 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74 55 63 6f  s, 1);.  initUco
be410 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20 72  nvObjects();.  r
be420 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
be430 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
be440 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  t sqlite3_os_end
be450 28 76 6f 69 64 29 7b 0a 20 20 66 72 65 65 55 63  (void){.  freeUc
be460 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20  onvObjects();.  
be470 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
be480 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
be490 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 2a 2f 0a  QLITE_OS_OS2 */.
be4a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
be4b0 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e 63   End of os_os2.c
be4c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
be4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
be4f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
be500 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 75   Begin file os_u
be510 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nix.c **********
be520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
be540 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
be550 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
be560 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
be570 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
be580 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
be590 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
be5a0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
be5b0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
be5c0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
be5d0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
be5e0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
be5f0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
be600 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
be610 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
be620 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
be630 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
be640 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
be650 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
be660 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
be670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be6b0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
be6c0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
be6d0 74 68 65 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e  the VFS implemen
be6e0 74 61 74 69 6f 6e 20 66 6f 72 20 75 6e 69 78 2d  tation for unix-
be6f0 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e 67 20 73  like operating s
be700 79 73 74 65 6d 73 0a 2a 2a 20 69 6e 63 6c 75 64  ystems.** includ
be710 65 20 4c 69 6e 75 78 2c 20 4d 61 63 4f 53 58 2c  e Linux, MacOSX,
be720 20 2a 42 53 44 2c 20 51 4e 58 2c 20 56 78 57 6f   *BSD, QNX, VxWo
be730 72 6b 73 2c 20 41 49 58 2c 20 48 50 55 58 2c 20  rks, AIX, HPUX, 
be740 61 6e 64 20 6f 74 68 65 72 73 2e 0a 2a 2a 0a 2a  and others..**.*
be750 2a 20 54 68 65 72 65 20 61 72 65 20 61 63 74 75  * There are actu
be760 61 6c 6c 79 20 73 65 76 65 72 61 6c 20 64 69 66  ally several dif
be770 66 65 72 65 6e 74 20 56 46 53 20 69 6d 70 6c 65  ferent VFS imple
be780 6d 65 6e 74 61 74 69 6f 6e 73 20 69 6e 20 74 68  mentations in th
be790 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20  is file..** The 
be7a0 64 69 66 66 65 72 65 6e 63 65 73 20 61 72 65 20  differences are 
be7b0 69 6e 20 74 68 65 20 77 61 79 20 74 68 61 74 20  in the way that 
be7c0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20  file locking is 
be7d0 64 6f 6e 65 2e 20 20 54 68 65 20 64 65 66 61 75  done.  The defau
be7e0 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  lt.** implementa
be7f0 74 69 6f 6e 20 75 73 65 73 20 50 6f 73 69 78 20  tion uses Posix 
be800 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 73 2e 20  Advisory Locks. 
be810 20 41 6c 74 65 72 6e 61 74 69 76 65 20 69 6d 70   Alternative imp
be820 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20  lementations.** 
be830 75 73 65 20 66 6c 6f 63 6b 28 29 2c 20 64 6f 74  use flock(), dot
be840 2d 66 69 6c 65 73 2c 20 76 61 72 69 6f 75 73 20  -files, various 
be850 70 72 6f 70 72 69 65 74 61 72 79 20 6c 6f 63 6b  proprietary lock
be860 69 6e 67 20 73 63 68 65 6d 61 73 2c 20 6f 72 20  ing schemas, or 
be870 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b 69 70 20 6c  simply.** skip l
be880 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 6f 67 65 74  ocking all toget
be890 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  her..**.** This 
be8a0 73 6f 75 72 63 65 20 66 69 6c 65 20 69 73 20 6f  source file is o
be8b0 72 67 61 6e 69 7a 65 64 20 69 6e 74 6f 20 64 69  rganized into di
be8c0 76 69 73 69 6f 6e 73 20 77 68 65 72 65 20 74 68  visions where th
be8d0 65 20 6c 6f 67 69 63 20 66 6f 72 20 76 61 72 69  e logic for vari
be8e0 6f 75 73 0a 2a 2a 20 73 75 62 66 75 6e 63 74 69  ous.** subfuncti
be8f0 6f 6e 73 20 69 73 20 63 6f 6e 74 61 69 6e 65 64  ons is contained
be900 20 77 69 74 68 69 6e 20 74 68 65 20 61 70 70 72   within the appr
be910 6f 70 72 69 61 74 65 20 64 69 76 69 73 69 6f 6e  opriate division
be920 2e 20 20 50 4c 45 41 53 45 0a 2a 2a 20 4b 45 45  .  PLEASE.** KEE
be930 50 20 54 48 45 20 53 54 52 55 43 54 55 52 45 20  P THE STRUCTURE 
be940 4f 46 20 54 48 49 53 20 46 49 4c 45 20 49 4e 54  OF THIS FILE INT
be950 41 43 54 2e 20 20 4e 65 77 20 63 6f 64 65 20 73  ACT.  New code s
be960 68 6f 75 6c 64 20 62 65 20 70 6c 61 63 65 64 0a  hould be placed.
be970 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63  ** in the correc
be980 74 20 64 69 76 69 73 69 6f 6e 20 61 6e 64 20 73  t division and s
be990 68 6f 75 6c 64 20 62 65 20 63 6c 65 61 72 6c 79  hould be clearly
be9a0 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20   labeled..**.** 
be9b0 54 68 65 20 6c 61 79 6f 75 74 20 6f 66 20 64 69  The layout of di
be9c0 76 69 73 69 6f 6e 73 20 69 73 20 61 73 20 66 6f  visions is as fo
be9d0 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  llows:.**.**   *
be9e0 20 20 47 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73    General-purpos
be9f0 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 61  e declarations a
bea00 6e 64 20 75 74 69 6c 69 74 79 20 66 75 6e 63 74  nd utility funct
bea10 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20 55 6e  ions..**   *  Un
bea20 69 71 75 65 20 66 69 6c 65 20 49 44 20 6c 6f 67  ique file ID log
bea30 69 63 20 75 73 65 64 20 62 79 20 56 78 57 6f 72  ic used by VxWor
bea40 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 56 61 72 69  ks..**   *  Vari
bea50 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d  ous locking prim
bea60 69 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61  itive implementa
bea70 74 69 6f 6e 73 20 28 61 6c 6c 20 65 78 63 65 70  tions (all excep
bea80 74 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 29  t proxy locking)
bea90 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20  :.**      + for 
beaa0 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c  Posix Advisory L
beab0 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66  ocks.**      + f
beac0 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 73 0a 2a  or no-op locks.*
bead0 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 64 6f 74  *      + for dot
beae0 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 20  -file locks.**  
beaf0 20 20 20 20 2b 20 66 6f 72 20 66 6c 6f 63 6b 28      + for flock(
beb00 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20  ) locking.**    
beb10 20 20 2b 20 66 6f 72 20 6e 61 6d 65 64 20 73 65    + for named se
beb20 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 28 56  maphore locks (V
beb30 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 0a 2a 2a 20  xWorks only).** 
beb40 20 20 20 20 20 2b 20 66 6f 72 20 41 46 50 20 66       + for AFP f
beb50 69 6c 65 73 79 73 74 65 6d 20 6c 6f 63 6b 73 20  ilesystem locks 
beb60 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a  (MacOSX only).**
beb70 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 66 69     *  sqlite3_fi
beb80 6c 65 20 6d 65 74 68 6f 64 73 20 6e 6f 74 20 61  le methods not a
beb90 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6c  ssociated with l
beba0 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 2a 20 20  ocking..**   *  
bebb0 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73  Definitions of s
bebc0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
bebd0 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 6c  s objects for al
bebe0 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20  l locking.**    
bebf0 20 20 6d 65 74 68 6f 64 73 20 70 6c 75 73 20 22    methods plus "
bec00 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e  finder" function
bec10 73 20 66 6f 72 20 65 61 63 68 20 6c 6f 63 6b 69  s for each locki
bec20 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 20 20  ng method..**   
bec30 2a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d  *  sqlite3_vfs m
bec40 65 74 68 6f 64 20 69 6d 70 6c 65 6d 65 6e 74 61  ethod implementa
bec50 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20 4c  tions..**   *  L
bec60 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65  ocking primitive
bec70 73 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 20  s for the proxy 
bec80 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 74  uber-locking-met
bec90 68 6f 64 2e 20 28 4d 61 63 4f 53 58 20 6f 6e 6c  hod. (MacOSX onl
beca0 79 29 0a 2a 2a 20 20 20 2a 20 20 44 65 66 69 6e  y).**   *  Defin
becb0 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65  itions of sqlite
becc0 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 66 6f  3_vfs objects fo
becd0 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 65  r all locking me
bece0 74 68 6f 64 73 0a 2a 2a 20 20 20 20 20 20 70 6c  thods.**      pl
becf0 75 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  us implementatio
bed00 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73  ns of sqlite3_os
bed10 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69  _init() and sqli
bed20 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a  te3_os_end()..**
bed30 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 75 6e 69 78  .** $Id: os_unix
bed40 2e 63 2c 76 20 31 2e 32 35 30 20 32 30 30 39 2f  .c,v 1.250 2009/
bed50 30 34 2f 30 37 20 30 35 3a 33 35 3a 30 34 20 63  04/07 05:35:04 c
bed60 68 77 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 20  hw Exp $.*/.#if 
bed70 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20  SQLITE_OS_UNIX  
bed80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
bed90 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 64  his file is used
beda0 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f   on unix only */
bedb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72  ../*.** There ar
bedc0 65 20 76 61 72 69 6f 75 73 20 6d 65 74 68 6f 64  e various method
bedd0 73 20 66 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69  s for file locki
bede0 6e 67 20 75 73 65 64 20 66 6f 72 20 63 6f 6e 63  ng used for conc
bedf0 75 72 72 65 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72  urrency.** contr
bee00 6f 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50  ol:.**.**   1. P
bee10 4f 53 49 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68  OSIX locking (th
bee20 65 20 64 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20  e default),.**  
bee30 20 32 2e 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a   2. No locking,.
bee40 2a 2a 20 20 20 33 2e 20 44 6f 74 2d 66 69 6c 65  **   3. Dot-file
bee50 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34   locking,.**   4
bee60 2e 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e  . flock() lockin
bee70 67 2c 0a 2a 2a 20 20 20 35 2e 20 41 46 50 20 6c  g,.**   5. AFP l
bee80 6f 63 6b 69 6e 67 20 28 4f 53 58 20 6f 6e 6c 79  ocking (OSX only
bee90 29 2c 0a 2a 2a 20 20 20 36 2e 20 4e 61 6d 65 64  ),.**   6. Named
beea0 20 50 4f 53 49 58 20 73 65 6d 61 70 68 6f 72 65   POSIX semaphore
beeb0 73 20 28 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 29  s (VXWorks only)
beec0 2c 0a 2a 2a 20 20 20 37 2e 20 70 72 6f 78 79 20  ,.**   7. proxy 
beed0 6c 6f 63 6b 69 6e 67 2e 20 28 4f 53 58 20 6f 6e  locking. (OSX on
beee0 6c 79 29 0a 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73  ly).**.** Styles
beef0 20 34 2c 20 35 2c 20 61 6e 64 20 37 20 61 72 65   4, 5, and 7 are
bef00 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
bef10 6f 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  of SQLITE_ENABLE
bef20 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a  _LOCKING_STYLE.*
bef30 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20  * is defined to 
bef40 31 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 45  1.  The SQLITE_E
bef50 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
bef60 59 4c 45 20 61 6c 73 6f 20 65 6e 61 62 6c 65 73  YLE also enables
bef70 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 73 65   automatic.** se
bef80 6c 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 61  lection of the a
bef90 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69  ppropriate locki
befa0 6e 67 20 73 74 79 6c 65 20 62 61 73 65 64 20 6f  ng style based o
befb0 6e 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d  n the filesystem
befc0 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 64 61  .** where the da
befd0 74 61 62 61 73 65 20 69 73 20 6c 6f 63 61 74 65  tabase is locate
befe0 64 2e 20 20 0a 2a 2f 0a 23 69 66 20 21 64 65 66  d.  .*/.#if !def
beff0 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
bf000 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
bf010 29 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28  ).#  if defined(
bf020 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20  __APPLE__).#    
bf030 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e  define SQLITE_EN
bf040 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
bf050 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20  LE 1.#  else.#  
bf060 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
bf070 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
bf080 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a  TYLE 0.#  endif.
bf090 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65  #endif../*.** De
bf0a0 66 69 6e 65 20 74 68 65 20 4f 53 5f 56 58 57 4f  fine the OS_VXWO
bf0b0 52 4b 53 20 70 72 65 2d 70 72 6f 63 65 73 73 6f  RKS pre-processo
bf0c0 72 20 6d 61 63 72 6f 20 74 6f 20 31 20 69 66 20  r macro to 1 if 
bf0d0 62 75 69 6c 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20  building on .** 
bf0e0 76 78 77 6f 72 6b 73 2c 20 6f 72 20 30 20 6f 74  vxworks, or 0 ot
bf0f0 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e  herwise..*/.#ifn
bf100 64 65 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23  def OS_VXWORKS.#
bf110 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 52    if defined(__R
bf120 54 50 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  TP__) || defined
bf130 28 5f 57 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20  (_WRS_KERNEL).# 
bf140 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 57     define OS_VXW
bf150 4f 52 4b 53 20 31 0a 23 20 20 65 6c 73 65 0a 23  ORKS 1.#  else.#
bf160 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 56 58      define OS_VX
bf170 57 4f 52 4b 53 20 30 0a 23 20 20 65 6e 64 69 66  WORKS 0.#  endif
bf180 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
bf190 68 65 73 65 20 23 64 65 66 69 6e 65 73 20 73 68  hese #defines sh
bf1a0 6f 75 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 42  ould enable >2GB
bf1b0 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e   file support on
bf1c0 20 50 6f 73 69 78 20 69 66 20 74 68 65 0a 2a 2a   Posix if the.**
bf1d0 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72   underlying oper
bf1e0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75 70  ating system sup
bf1f0 70 6f 72 74 73 20 69 74 2e 20 20 49 66 20 74 68  ports it.  If th
bf200 65 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61  e OS lacks.** la
bf210 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
bf220 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62  , these should b
bf230 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  e no-ops..**.** 
bf240 4c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f  Large file suppo
bf250 72 74 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c  rt can be disabl
bf260 65 64 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53  ed using the -DS
bf270 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46  QLITE_DISABLE_LF
bf280 53 20 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74  S switch.** on t
bf290 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d  he compiler comm
bf2a0 61 6e 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20  and line.  This 
bf2b0 69 73 20 6e 65 63 65 73 73 61 72 79 20 69 66 20  is necessary if 
bf2c0 79 6f 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e  you are compilin
bf2d0 67 0a 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74  g.** on a recent
bf2e0 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65   machine (ex: Re
bf2f0 64 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f  dHat 7.2) but yo
bf300 75 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65  u want your code
bf310 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61   to work.** on a
bf320 6e 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20  n older machine 
bf330 28 65 78 3a 20 52 65 64 48 61 74 20 36 2e 30 29  (ex: RedHat 6.0)
bf340 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c  .  If you compil
bf350 65 20 6f 6e 20 52 65 64 48 61 74 20 37 2e 32 0a  e on RedHat 7.2.
bf360 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20  ** without this 
bf370 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65  option, LFS is e
bf380 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20  nable.  But LFS 
bf390 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69  does not exist i
bf3a0 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20  n the kernel.** 
bf3b0 69 6e 20 52 65 64 48 61 74 20 36 2e 30 2c 20 73  in RedHat 6.0, s
bf3c0 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74  o the code won't
bf3d0 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66   work.  Hence, f
bf3e0 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72  or maximum binar
bf3f0 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79  y.** portability
bf400 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74   you should omit
bf410 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   LFS..**.** The 
bf420 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 61  previous paragra
bf430 70 68 20 77 61 73 20 77 72 69 74 74 65 6e 20 69  ph was written i
bf440 6e 20 32 30 30 35 2e 20 20 28 54 68 69 73 20 70  n 2005.  (This p
bf450 61 72 61 67 72 61 70 68 20 69 73 20 77 72 69 74  aragraph is writ
bf460 74 65 6e 0a 2a 2a 20 6f 6e 20 32 30 30 38 2d 31  ten.** on 2008-1
bf470 31 2d 32 38 2e 29 20 54 68 65 73 65 20 64 61 79  1-28.) These day
bf480 73 2c 20 61 6c 6c 20 4c 69 6e 75 78 20 6b 65 72  s, all Linux ker
bf490 6e 65 6c 73 20 73 75 70 70 6f 72 74 20 6c 61 72  nels support lar
bf4a0 67 65 20 66 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20  ge files, so.** 
bf4b0 79 6f 75 20 73 68 6f 75 6c 64 20 70 72 6f 62 61  you should proba
bf4c0 62 6c 79 20 6c 65 61 76 65 20 4c 46 53 20 65 6e  bly leave LFS en
bf4d0 61 62 6c 65 64 2e 20 20 42 75 74 20 73 6f 6d 65  abled.  But some
bf4e0 20 65 6d 62 65 64 64 65 64 20 70 6c 61 74 66 6f   embedded platfo
bf4f0 72 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 6c 61 63  rms might.** lac
bf500 6b 20 4c 46 53 20 69 6e 20 77 68 69 63 68 20 63  k LFS in which c
bf510 61 73 65 20 74 68 65 20 53 51 4c 49 54 45 5f 44  ase the SQLITE_D
bf520 49 53 41 42 4c 45 5f 4c 46 53 20 6d 61 63 72 6f  ISABLE_LFS macro
bf530 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 62 65 20   might still be 
bf540 75 73 65 66 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64  useful..*/.#ifnd
bf550 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c  ef SQLITE_DISABL
bf560 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65 20 5f  E_LFS.# define _
bf570 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20 20 20  LARGE_FILE      
bf580 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46 49 4c   1.# ifndef _FIL
bf590 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a 23 20  E_OFFSET_BITS.# 
bf5a0 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45 5f 4f    define _FILE_O
bf5b0 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a 23 20  FFSET_BITS 64.# 
bf5c0 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 5f  endif.# define _
bf5d0 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52 43 45  LARGEFILE_SOURCE
bf5e0 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   1.#endif../*.**
bf5f0 20 73 74 61 6e 64 61 72 64 20 69 6e 63 6c 75 64   standard includ
bf600 65 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63  e files..*/.#inc
bf610 6c 75 64 65 20 3c 73 79 73 2f 74 79 70 65 73 2e  lude <sys/types.
bf620 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  h>.#include <sys
bf630 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 6c 75 64  /stat.h>.#includ
bf640 65 20 3c 66 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63  e <fcntl.h>.#inc
bf650 6c 75 64 65 20 3c 75 6e 69 73 74 64 2e 68 3e 0a  lude <unistd.h>.
bf660 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 69  #include <sys/ti
bf670 6d 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  me.h>.#include <
bf680 65 72 72 6e 6f 2e 68 3e 0a 0a 23 69 66 20 53 51  errno.h>..#if SQ
bf690 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
bf6a0 49 4e 47 5f 53 54 59 4c 45 0a 23 20 69 6e 63 6c  ING_STYLE.# incl
bf6b0 75 64 65 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68  ude <sys/ioctl.h
bf6c0 3e 0a 23 20 69 66 20 4f 53 5f 56 58 57 4f 52 4b  >.# if OS_VXWORK
bf6d0 53 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 65  S.#  include <se
bf6e0 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20 20 69 6e  maphore.h>.#  in
bf6f0 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73 2e 68 3e  clude <limits.h>
bf700 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e 63 6c 75  .# else.#  inclu
bf710 64 65 20 3c 73 79 73 2f 66 69 6c 65 2e 68 3e 0a  de <sys/file.h>.
bf720 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f  #  include <sys/
bf730 70 61 72 61 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c  param.h>.#  incl
bf740 75 64 65 20 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68  ude <sys/mount.h
bf750 3e 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  >.# endif.#endif
bf760 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
bf770 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
bf780 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  */../*.** If we 
bf790 61 72 65 20 74 6f 20 62 65 20 74 68 72 65 61 64  are to be thread
bf7a0 2d 73 61 66 65 2c 20 69 6e 63 6c 75 64 65 20 74  -safe, include t
bf7b0 68 65 20 70 74 68 72 65 61 64 73 20 68 65 61 64  he pthreads head
bf7c0 65 72 20 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a  er and define.**
bf7d0 20 74 68 65 20 53 51 4c 49 54 45 5f 55 4e 49 58   the SQLITE_UNIX
bf7e0 5f 54 48 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a  _THREADS macro..
bf7f0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
bf800 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e  READSAFE.# defin
bf810 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48  e SQLITE_UNIX_TH
bf820 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a  READS 1.#endif..
bf830 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65  /*.** Default pe
bf840 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63  rmissions when c
bf850 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69  reating a new fi
bf860 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  le.*/.#ifndef SQ
bf870 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
bf880 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20  E_PERMISSIONS.# 
bf890 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
bf8a0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
bf8b0 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64  SSIONS 0644.#end
bf8c0 69 66 0a 0a 2f 2a 0a 20 2a 2a 20 44 65 66 61 75  if../*. ** Defau
bf8d0 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 77  lt permissions w
bf8e0 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 75 74  hen creating aut
bf8f0 6f 20 70 72 6f 78 79 20 64 69 72 0a 20 2a 2f 0a  o proxy dir. */.
bf900 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
bf910 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f  EFAULT_PROXYDIR_
bf920 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65  PERMISSIONS.# de
bf930 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
bf940 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52  ULT_PROXYDIR_PER
bf950 4d 49 53 53 49 4f 4e 53 20 30 37 35 35 0a 23 65  MISSIONS 0755.#e
bf960 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69  ndif../*.** Maxi
bf970 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61  mum supported pa
bf980 74 68 2d 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64  th-length..*/.#d
bf990 65 66 69 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41  efine MAX_PATHNA
bf9a0 4d 45 20 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e  ME 512../*.** On
bf9b0 6c 79 20 73 65 74 20 74 68 65 20 6c 61 73 74 45  ly set the lastE
bf9c0 72 72 6e 6f 20 69 66 20 74 68 65 20 65 72 72 6f  rrno if the erro
bf9d0 72 20 63 6f 64 65 20 69 73 20 61 20 72 65 61 6c  r code is a real
bf9e0 20 65 72 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a   error and not .
bf9f0 2a 2a 20 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65  ** a normal expe
bfa00 63 74 65 64 20 72 65 74 75 72 6e 20 63 6f 64 65  cted return code
bfa10 20 6f 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20   of SQLITE_BUSY 
bfa20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a  or SQLITE_OK.*/.
bfa30 23 64 65 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f  #define IS_LOCK_
bfa40 45 52 52 4f 52 28 78 29 20 20 28 28 78 20 21 3d  ERROR(x)  ((x !=
bfa50 20 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28   SQLITE_OK) && (
bfa60 78 20 21 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  x != SQLITE_BUSY
bfa70 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75  )).../*.** The u
bfa80 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72  nixFile structur
bfa90 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66  e is subclass of
bfaa0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70   sqlite3_file sp
bfab0 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 75 6e  ecific to the un
bfac0 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d  ix.** VFS implem
bfad0 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79  entations..*/.ty
bfae0 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e 69  pedef struct uni
bfaf0 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a  xFile unixFile;.
bfb00 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20  struct unixFile 
bfb10 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  {.  sqlite3_io_m
bfb20 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d  ethods const *pM
bfb30 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79  ethod;  /* Alway
bfb40 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72  s the first entr
bfb50 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e  y */.  struct un
bfb60 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  ixOpenCnt *pOpen
bfb70 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20  ;       /* Info 
bfb80 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66  about all open f
bfb90 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64  d's on this inod
bfba0 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e  e */.  struct un
bfbb0 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63  ixLockInfo *pLoc
bfbc0 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20  k;      /* Info 
bfbd0 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74  about locks on t
bfbe0 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69  his inode */.  i
bfbf0 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20  nt h;           
bfc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bfc10 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63  /* The file desc
bfc20 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  riptor */.  int 
bfc30 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 20 20  dirfd;          
bfc40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
bfc50 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  File descriptor 
bfc60 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f 72  for the director
bfc70 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  y */.  unsigned 
bfc80 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20  char locktype;  
bfc90 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
bfca0 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64  ype of lock held
bfcb0 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a 20   on this fd */. 
bfcc0 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b 20   int lastErrno; 
bfcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bfce0 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 65 72    /* The unix er
bfcf0 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73  rno from the las
bfd00 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20  t I/O error */. 
bfd10 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f   void *lockingCo
bfd20 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  ntext;          
bfd30 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79    /* Locking sty
bfd40 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 61 74  le specific stat
bfd50 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  e */.#if SQLITE_
bfd60 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
bfd70 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e 46  TYLE.  int openF
bfd80 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
bfd90 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
bfda0 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 20 61  lags specified a
bfdb0 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64  t open() */.#end
bfdc0 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  if.#if SQLITE_TH
bfdd0 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69  READSAFE && defi
bfde0 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20  ned(__linux__). 
bfdf0 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20   pthread_t tid; 
bfe00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bfe10 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64 20    /* The thread 
bfe20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69 73  that "owns" this
bfe30 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e   unixFile */.#en
bfe40 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  dif.#if OS_VXWOR
bfe50 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74  KS.  int isDelet
bfe60 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
bfe70 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20        /* Delete 
bfe80 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 75 65  on close if true
bfe90 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77   */.  struct vxw
bfea0 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 3b  orksFileId *pId;
bfeb0 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 65         /* Unique
bfec0 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e 64   file ID */.#end
bfed0 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  if.#ifndef NDEBU
bfee0 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20  G.  /* The next 
bfef0 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 62 6c  group of variabl
bff00 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 74  es are used to t
bff10 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f 72 20  rack whether or 
bff20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 72 61  not the.  ** tra
bff30 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72  nsaction counter
bff40 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 37 20   in bytes 24-27 
bff50 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  of database file
bff60 73 20 61 72 65 20 75 70 64 61 74 65 64 0a 20 20  s are updated.  
bff70 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e 79 20  ** whenever any 
bff80 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
bff90 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41  base changes.  A
bffa0 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c  n assertion faul
bffb0 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75  t will.  ** occu
bffc0 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 20 75  r if a file is u
bffd0 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 20 61  pdated without a
bffe0 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 68 65  lso updating the
bfff0 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a   transaction.  *
c0000 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 73  * counter.  This
c0010 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 74 6f   test is made to
c0020 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f 62 6c   avoid new probl
c0030 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 74  ems similar to t
c0040 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73 63  he.  ** one desc
c0050 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 74 20  ribed by ticket 
c0060 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 75  #3584. .  */.  u
c0070 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 72 61  nsigned char tra
c0080 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a  nsCntrChng;   /*
c0090 20 54 72 75 65 20 69 66 20 74 68 65 20 74 72 61   True if the tra
c00a0 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72  nsaction counter
c00b0 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e   changed */.  un
c00c0 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 55 70  signed char dbUp
c00d0 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  date;        /* 
c00e0 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 72 74  True if any part
c00f0 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
c0100 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75  e changed */.  u
c0110 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e 4e  nsigned char inN
c0120 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a  ormalWrite;   /*
c0130 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e 6f   True if in a no
c0140 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72 61  rmal write opera
c0150 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  tion */..  /* If
c0160 20 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e   true, that mean
c0170 73 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  s we are dealing
c0180 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73 65   with a database
c0190 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 0a 20   file that has. 
c01a0 20 2a 2a 20 61 20 72 61 6e 67 65 20 6f 66 20 6c   ** a range of l
c01b0 6f 63 6b 69 6e 67 20 62 79 74 65 73 20 66 72 6f  ocking bytes fro
c01c0 6d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 74  m PENDING_BYTE t
c01d0 68 72 6f 75 67 68 20 50 45 4e 44 49 4e 47 5f 42  hrough PENDING_B
c01e0 59 54 45 2b 35 31 31 0a 20 20 2a 2a 20 77 68 69  YTE+511.  ** whi
c01f0 63 68 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  ch should never 
c0200 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74  be read or writt
c0210 65 6e 2e 20 20 41 73 73 65 72 74 73 28 29 20 77  en.  Asserts() w
c0220 69 6c 6c 20 76 65 72 69 66 79 20 74 68 69 73 20  ill verify this 
c0230 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
c0240 61 72 20 69 73 4c 6f 63 6b 61 62 6c 65 3b 20 20  ar isLockable;  
c0250 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66      /* True if f
c0260 69 6c 65 20 6d 69 67 68 74 20 62 65 20 6c 6f 63  ile might be loc
c0270 6b 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ked */.#endif.#i
c0280 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
c0290 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 6d 6f  .  /* In test mo
c02a0 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65  de, increase the
c02b0 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 73 74   size of this st
c02c0 72 75 63 74 75 72 65 20 61 20 62 69 74 20 73 6f  ructure a bit so
c02d0 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 20 69   that .  ** it i
c02e0 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
c02f0 65 20 73 74 72 75 63 74 20 43 72 61 73 68 46 69  e struct CrashFi
c0300 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20 74 65  le defined in te
c0310 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63 68  st6.c..  */.  ch
c0320 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32 5d 3b  ar aPadding[32];
c0330 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a  .#endif.};../*.*
c0340 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74  * Include code t
c0350 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f  hat is common to
c0360 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65   all os_*.c file
c0370 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.*/./**********
c0380 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f  **** Include os_
c0390 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20  common.h in the 
c03a0 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69  middle of os_uni
c03b0 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
c03c0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
c03d0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
c03e0 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
c03f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0410 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
c0420 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65  May 22.**.** The
c0430 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
c0440 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
c0450 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
c0460 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
c0470 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
c0480 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
c0490 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
c04a0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
c04b0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
c04c0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
c04d0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
c04e0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
c04f0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
c0500 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
c0510 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
c0520 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
c0530 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
c0540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
c0590 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
c05a0 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61  ins macros and a
c05b0 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63   little bit of c
c05c0 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d  ode that is comm
c05d0 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20  on to.** all of 
c05e0 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65  the platform-spe
c05f0 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f  cific files (os_
c0600 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63  *.c) and is #inc
c0610 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65  luded into those
c0620 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a  .** files..**.**
c0630 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c   This file shoul
c0640 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62  d be #included b
c0650 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c  y the os_*.c fil
c0660 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20  es only.  It is 
c0670 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c  not a.** general
c0680 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 20   purpose header 
c0690 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  file..**.** $Id:
c06a0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31   os_common.h,v 1
c06b0 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 20 31  .38 2009/02/24 1
c06c0 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31  8:40:50 danielk1
c06d0 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  977 Exp $.*/.#if
c06e0 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f  ndef _OS_COMMON_
c06f0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43  H_.#define _OS_C
c0700 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  OMMON_H_../*.** 
c0710 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67  At least two bug
c0720 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69  s have slipped i
c0730 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61  n because we cha
c0740 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f  nged the MEMORY_
c0750 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74  DEBUG.** macro t
c0760 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61  o SQLITE_DEBUG a
c0770 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61  nd some older ma
c0780 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74  kefiles have not
c0790 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a   yet made the.**
c07a0 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f   switch.  The fo
c07b0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f  llowing code sho
c07c0 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70  uld catch this p
c07d0 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c  roblem at compil
c07e0 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65  e-time..*/.#ifde
c07f0 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23  f MEMORY_DEBUG.#
c0800 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f   error "The MEMO
c0810 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69  RY_DEBUG macro i
c0820 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65  s obsolete.  Use
c0830 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e   SQLITE_DEBUG in
c0840 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a  stead.".#endif..
c0850 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
c0860 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
c0870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53  TE int sqlite3OS
c0880 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69  Trace = 0;.#defi
c0890 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20  ne OSTRACE1(X)  
c08a0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
c08b0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
c08c0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
c08d0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c08e0 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66  E2(X,Y)       if
c08f0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
c0900 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
c0910 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69  rintf(X,Y).#defi
c0920 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
c0930 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  Z)     if( sqlit
c0940 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
c0950 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
c0960 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Y,Z).#define OS
c0970 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20  TRACE4(X,Y,Z,A) 
c0980 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
c0990 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
c09a0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
c09b0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
c09c0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69  CE5(X,Y,Z,A,B) i
c09d0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
c09e0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
c09f0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
c0a00 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c0a10 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20  E6(X,Y,Z,A,B,C) 
c0a20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
c0a30 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  OSTrace) sqlite3
c0a40 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
c0a50 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
c0a60 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
c0a70 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69  A,B,C,D) \.    i
c0a80 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
c0a90 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
c0aa0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
c0ab0 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  ,D).#else.#defin
c0ac0 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64  e OSTRACE1(X).#d
c0ad0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58  efine OSTRACE2(X
c0ae0 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
c0af0 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66  ACE3(X,Y,Z).#def
c0b00 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59  ine OSTRACE4(X,Y
c0b10 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Z,A).#define OS
c0b20 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE5(X,Y,Z,A,B
c0b30 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c0b40 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a  E6(X,Y,Z,A,B,C).
c0b50 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37  #define OSTRACE7
c0b60 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a  (X,Y,Z,A,B,C,D).
c0b70 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
c0b80 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  cros for perform
c0b90 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e  ance tracing.  N
c0ba0 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f  ormally turned o
c0bb0 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a  ff.  Only works.
c0bc0 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77  ** on i486 hardw
c0bd0 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  are..*/.#ifdef S
c0be0 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43  QLITE_PERFORMANC
c0bf0 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20  E_TRACE../* .** 
c0c00 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e  hwtime.h contain
c0c10 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c  s inline assembl
c0c20 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c  er code for impl
c0c30 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67  ementing .** hig
c0c40 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69  h-performance ti
c0c50 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  ming routines..*
c0c60 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c0c70 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65  * Include hwtime
c0c80 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
c0c90 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   of os_common.h 
c0ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0cb0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c0cc0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74  * Begin file hwt
c0cd0 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
c0ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0d00 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79  /./*.** 2008 May
c0d10 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   27.**.** The au
c0d20 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
c0d30 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
c0d40 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
c0d50 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
c0d60 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
c0d70 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
c0d80 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
c0d90 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
c0da0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
c0db0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
c0dc0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
c0dd0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
c0de0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
c0df0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
c0e00 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
c0e10 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
c0e20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
c0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0e70 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
c0e80 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
c0e90 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65   inline asm code
c0ea0 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20   for retrieving 
c0eb0 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63  "high-performanc
c0ec0 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66  e".** counters f
c0ed0 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55  or x86 class CPU
c0ee0 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77  s..**.** $Id: hw
c0ef0 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30  time.h,v 1.3 200
c0f00 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35  8/08/01 14:33:15
c0f10 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
c0f20 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f  #ifndef _HWTIME_
c0f30 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49  H_.#define _HWTI
c0f40 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ME_H_../*.** The
c0f50 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
c0f60 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ne only works on
c0f70 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28   pentium-class (
c0f80 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73  or newer) proces
c0f90 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73  sors..** It uses
c0fa0 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64   the RDTSC opcod
c0fb0 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79  e to read the cy
c0fc0 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20  cle count value 
c0fd0 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72  out of the.** pr
c0fe0 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75  ocessor and retu
c0ff0 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20  rns that value. 
c1000 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
c1010 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a  d for high-res.*
c1020 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a  * profiling..*/.
c1030 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  #if (defined(__G
c1040 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  NUC__) || define
c1050 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20  d(_MSC_VER)) && 
c1060 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64  \.      (defined
c1070 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
c1080 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
c1090 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29  efined(_M_IX86))
c10a0 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28  ..  #if defined(
c10b0 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69  __GNUC__)..  __i
c10c0 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
c10d0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
c10e0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
c10f0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c  unsigned int lo,
c1100 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f   hi;.     __asm_
c1110 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
c1120 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28  "rdtsc" : "=a" (
c1130 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b  lo), "=d" (hi));
c1140 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71  .     return (sq
c1150 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c  lite_uint64)hi <
c1160 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a  < 32 | lo;.  }..
c1170 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28    #elif defined(
c1180 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64  _MSC_VER)..  __d
c1190 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f  eclspec(naked) _
c11a0 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75  _inline sqlite_u
c11b0 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71  int64 __cdecl sq
c11c0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
c11d0 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a  ){.     __asm {.
c11e0 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20          rdtsc.  
c11f0 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20        ret       
c1200 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61  ; return value a
c1210 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d  t EDX:EAX.     }
c1220 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a  .  }..  #endif..
c1230 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f  #elif (defined(_
c1240 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69  _GNUC__) && defi
c1250 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29  ned(__x86_64__))
c1260 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
c1270 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
c1280 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
c1290 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
c12a0 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20   long val;.     
c12b0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
c12c0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
c12d0 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20   "=A" (val));.  
c12e0 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a      return val;.
c12f0 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66    }. .#elif (def
c1300 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
c1310 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f  & defined(__ppc_
c1320 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  _))..  __inline_
c1330 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
c1340 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
c1350 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  id){.      unsig
c1360 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
c1370 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69  tval;.      unsi
c1380 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a  gned long junk;.
c1390 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
c13a0 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c  volatile__ ("\n\
c13b0 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20  .          1:   
c13c0 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c     mftbu   %1\n\
c13d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c13e0 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e     mftb    %L0\n
c13f0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
c1400 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e      mftbu   %0\n
c1410 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
c1420 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25      cmpw    %0,%
c1430 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
c1440 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31         bne     1
c1450 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b".             
c1460 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74       : "=r" (ret
c1470 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b  val), "=r" (junk
c1480 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ));.      return
c1490 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65   retval;.  }..#e
c14a0 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65  lse..  #error Ne
c14b0 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ed implementatio
c14c0 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69  n of sqlite3Hwti
c14d0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
c14e0 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20  atform...  /*.  
c14f0 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69  ** To compile wi
c1500 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69  thout implementi
c1510 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  ng sqlite3Hwtime
c1520 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
c1530 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63  form,.  ** you c
c1540 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62  an remove the ab
c1550 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75  ove #error and u
c1560 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  se the following
c1570 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74  .  ** stub funct
c1580 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c  ion.  You will l
c1590 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f  ose timing suppo
c15a0 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a  rt for many.  **
c15b0 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e   of the debuggin
c15c0 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74  g and testing ut
c15d0 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20  ilities, but it 
c15e0 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c  should at.  ** l
c15f0 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64  east compile and
c1600 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54   run..  */.SQLIT
c1610 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
c1620 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
c1630 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72  3Hwtime(void){ r
c1640 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75  eturn ((sqlite_u
c1650 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e  int64)0); }..#en
c1660 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
c1670 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f  defined(_HWTIME_
c1680 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  H_) */../*******
c1690 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
c16a0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
c16b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c16c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c16d0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
c16e0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
c16f0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
c1700 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f   off in os_commo
c1710 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
c1720 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20  ******/..static 
c1730 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
c1740 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71  start;.static sq
c1750 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c  lite_uint64 g_el
c1760 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54  apsed;.#define T
c1770 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20  IMER_START      
c1780 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33   g_start=sqlite3
c1790 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65  Hwtime().#define
c17a0 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20   TIMER_END      
c17b0 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c     g_elapsed=sql
c17c0 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73  ite3Hwtime()-g_s
c17d0 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d  tart.#define TIM
c17e0 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67  ER_ELAPSED     g
c17f0 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23  _elapsed.#else.#
c1800 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41  define TIMER_STA
c1810 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  RT.#define TIMER
c1820 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d  _END.#define TIM
c1830 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28  ER_ELAPSED     (
c1840 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
c1850 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
c1860 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  If we compile wi
c1870 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45  th the SQLITE_TE
c1880 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68  ST macro set, th
c1890 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  en the following
c18a0 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64   block.** of cod
c18b0 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74  e will give us t
c18c0 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69  he ability to si
c18d0 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f  mulate a disk I/
c18e0 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a  O error.  This.*
c18f0 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  * is used for te
c1900 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65  sting the I/O re
c1910 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f  covery logic..*/
c1920 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
c1930 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
c1940 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
c1950 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20  ror_hit = 0;    
c1960 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
c1970 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45   number of I/O E
c1980 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f  rrors */.SQLITE_
c1990 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c19a0 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
c19b0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
c19c0 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65  Number of non-be
c19d0 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53  nign errors */.S
c19e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
c19f0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
c1a00 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20  ending = 0;     
c1a10 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e     /* Count down
c1a20 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72   to first I/O er
c1a30 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ror */.SQLITE_AP
c1a40 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
c1a50 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d  _error_persist =
c1a60 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
c1a70 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73  ue if I/O errors
c1a80 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49   persist */.SQLI
c1a90 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c1aa0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
c1ab0 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  gn = 0;         
c1ac0 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72  /* True if error
c1ad0 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a  s are benign */.
c1ae0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
c1af0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
c1b00 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c  pending = 0;.SQL
c1b10 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c1b20 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30  te3_diskfull = 0
c1b30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ;.#define Simula
c1b40 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
c1b50 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  X) sqlite3_io_er
c1b60 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23  ror_benign=(X).#
c1b70 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
c1b80 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a  OError(CODE)  \.
c1b90 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69    if( (sqlite3_i
c1ba0 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20  o_error_persist 
c1bb0 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  && sqlite3_io_er
c1bc0 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20  ror_hit) \.     
c1bd0 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f    || sqlite3_io_
c1be0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20  error_pending-- 
c1bf0 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20  == 1 )  \.      
c1c00 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f          { local_
c1c10 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d  ioerr(); CODE; }
c1c20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63  .static void loc
c1c30 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f  al_ioerr(){.  IO
c1c40 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22  TRACE(("IOERR\n"
c1c50 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f  ));.  sqlite3_io
c1c60 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20  _error_hit++;.  
c1c70 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f  if( !sqlite3_io_
c1c80 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73  error_benign ) s
c1c90 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c1ca0 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65  hardhit++;.}.#de
c1cb0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
c1cc0 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29  kfullError(CODE)
c1cd0 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65   \.   if( sqlite
c1ce0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
c1cf0 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28  ng ){ \.     if(
c1d00 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
c1d10 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29  l_pending == 1 )
c1d20 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c  { \.       local
c1d30 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20  _ioerr(); \.    
c1d40 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66     sqlite3_diskf
c1d50 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  ull = 1; \.     
c1d60 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
c1d70 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20  or_hit = 1; \.  
c1d80 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20       CODE; \.   
c1d90 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20    }else{ \.     
c1da0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75    sqlite3_diskfu
c1db0 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a  ll_pending--; \.
c1dc0 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65       } \.   }.#e
c1dd0 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  lse.#define Simu
c1de0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
c1df0 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  n(X).#define Sim
c1e00 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a  ulateIOError(A).
c1e10 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
c1e20 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29  DiskfullError(A)
c1e30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
c1e40 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65  hen testing, kee
c1e50 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65  p a count of the
c1e60 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20   number of open 
c1e70 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  files..*/.#ifdef
c1e80 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
c1e90 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c1ea0 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f  te3_open_file_co
c1eb0 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  unt = 0;.#define
c1ec0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20   OpenCounter(X) 
c1ed0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69   sqlite3_open_fi
c1ee0 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65  le_count+=(X).#e
c1ef0 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e  lse.#define Open
c1f00 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69  Counter(X).#endi
c1f10 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
c1f20 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e  fined(_OS_COMMON
c1f30 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _H_) */../******
c1f40 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
c1f50 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
c1f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1f80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
c1f90 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
c1fa0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
c1fb0 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78  t off in os_unix
c1fc0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
c1fd0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
c1fe0 44 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d  Define various m
c1ff0 61 63 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d  acros that are m
c2000 69 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65  issing from some
c2010 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66   systems..*/.#if
c2020 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45  ndef O_LARGEFILE
c2030 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47  .# define O_LARG
c2040 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23  EFILE 0.#endif.#
c2050 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53  ifdef SQLITE_DIS
c2060 41 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66  ABLE_LFS.# undef
c2070 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64   O_LARGEFILE.# d
c2080 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c  efine O_LARGEFIL
c2090 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  E 0.#endif.#ifnd
c20a0 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20  ef O_NOFOLLOW.# 
c20b0 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f  define O_NOFOLLO
c20c0 57 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  W 0.#endif.#ifnd
c20d0 65 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65  ef O_BINARY.# de
c20e0 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a  fine O_BINARY 0.
c20f0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
c2100 65 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72  e DJGPP compiler
c2110 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f   environment loo
c2120 6b 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55  ks mostly like U
c2130 6e 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c  nix, but it.** l
c2140 61 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 29  acks the fcntl()
c2150 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53   system call.  S
c2160 6f 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 6c  o redefine fcntl
c2170 28 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69  () to be somethi
c2180 6e 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79  ng.** that alway
c2190 73 20 73 75 63 63 65 65 64 73 2e 20 20 54 68 69  s succeeds.  Thi
c21a0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63  s means that loc
c21b0 6b 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63  king does not oc
c21c0 63 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47  cur under.** DJG
c21d0 50 50 2e 20 20 42 75 74 20 69 74 20 69 73 20 44  PP.  But it is D
c21e0 4f 53 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f  OS - what did yo
c21f0 75 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66  u expect?.*/.#if
c2200 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20  def __DJGPP__.# 
c2210 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42  define fcntl(A,B
c2220 2c 43 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ,C) 0.#endif../*
c2230 0a 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69 64  .** The threadid
c2240 20 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 20   macro resolves 
c2250 74 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69 64  to the thread-id
c2260 20 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64 20   or to 0.  Used 
c2270 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61  for.** testing a
c2280 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c  nd debugging onl
c2290 79 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  y..*/.#if SQLITE
c22a0 5f 54 48 52 45 41 44 53 41 46 45 0a 23 64 65 66  _THREADSAFE.#def
c22b0 69 6e 65 20 74 68 72 65 61 64 69 64 20 70 74 68  ine threadid pth
c22c0 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73  read_self().#els
c22d0 65 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 64  e.#define thread
c22e0 69 64 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  id 0.#endif.../*
c22f0 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  .** Helper funct
c2300 69 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61  ions to obtain a
c2310 6e 64 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68  nd relinquish th
c2320 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 0a  e global mutex..
c2330 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
c2340 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f  nixEnterMutex(vo
c2350 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  id){.  sqlite3_m
c2360 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
c2370 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
c2380 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
c2390 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61  _MASTER));.}.sta
c23a0 74 69 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61  tic void unixLea
c23b0 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20  veMutex(void){. 
c23c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
c23d0 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65  eave(sqlite3Mute
c23e0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
c23f0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
c2400 52 29 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20  R));.}...#ifdef 
c2410 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
c2420 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69  ** Helper functi
c2430 6f 6e 20 66 6f 72 20 70 72 69 6e 74 69 6e 67 20  on for printing 
c2440 6f 75 74 20 74 72 61 63 65 20 69 6e 66 6f 72 6d  out trace inform
c2450 61 74 69 6f 6e 20 66 72 6f 6d 20 64 65 62 75 67  ation from debug
c2460 67 69 6e 67 0a 2a 2a 20 62 69 6e 61 72 69 65 73  ging.** binaries
c2470 2e 20 54 68 69 73 20 72 65 74 75 72 6e 73 20 74  . This returns t
c2480 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73  he string repres
c2490 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  etation of the s
c24a0 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6e 74 65 67  upplied.** integ
c24b0 65 72 20 6c 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f  er lock-type..*/
c24c0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
c24d0 61 72 20 2a 6c 6f 63 6b 74 79 70 65 4e 61 6d 65  ar *locktypeName
c24e0 28 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a  (int locktype){.
c24f0 20 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79    switch( lockty
c2500 70 65 20 29 7b 0a 20 20 63 61 73 65 20 4e 4f 5f  pe ){.  case NO_
c2510 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f  LOCK: return "NO
c2520 4e 45 22 3b 0a 20 20 63 61 73 65 20 53 48 41 52  NE";.  case SHAR
c2530 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20  ED_LOCK: return 
c2540 22 53 48 41 52 45 44 22 3b 0a 20 20 63 61 73 65  "SHARED";.  case
c2550 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20   RESERVED_LOCK: 
c2560 72 65 74 75 72 6e 20 22 52 45 53 45 52 56 45 44  return "RESERVED
c2570 22 3b 0a 20 20 63 61 73 65 20 50 45 4e 44 49 4e  ";.  case PENDIN
c2580 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22  G_LOCK: return "
c2590 50 45 4e 44 49 4e 47 22 3b 0a 20 20 63 61 73 65  PENDING";.  case
c25a0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a   EXCLUSIVE_LOCK:
c25b0 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53 49   return "EXCLUSI
c25c0 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  VE";.  }.  retur
c25d0 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e  n "ERROR";.}.#en
c25e0 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
c25f0 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a  TE_LOCK_TRACE./*
c2600 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20 69 6e  .** Print out in
c2610 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
c2620 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72  all locking oper
c2630 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ations..**.** Th
c2640 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
c2650 65 64 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68  ed for troublesh
c2660 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20  ooting locks on 
c2670 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a  multithreaded.**
c2680 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61   platforms.  Ena
c2690 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67  ble by compiling
c26a0 20 77 69 74 68 20 74 68 65 20 2d 44 53 51 4c 49   with the -DSQLI
c26b0 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a  TE_LOCK_TRACE.**
c26c0 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70   command-line op
c26d0 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70  tion on the comp
c26e0 69 6c 65 72 2e 20 20 54 68 69 73 20 63 6f 64 65  iler.  This code
c26f0 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20   is normally.** 
c2700 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73  turned off..*/.s
c2710 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72  tatic int lockTr
c2720 61 63 65 28 69 6e 74 20 66 64 2c 20 69 6e 74 20  ace(int fd, int 
c2730 6f 70 2c 20 73 74 72 75 63 74 20 66 6c 6f 63 6b  op, struct flock
c2740 20 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f   *p){.  char *zO
c2750 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20  pName, *zType;. 
c2760 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20 73 61   int s;.  int sa
c2770 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20  vedErrno;.  if( 
c2780 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20  op==F_GETLK ){. 
c2790 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45     zOpName = "GE
c27a0 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66  TLK";.  }else if
c27b0 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b  ( op==F_SETLK ){
c27c0 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22  .    zOpName = "
c27d0 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b  SETLK";.  }else{
c27e0 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 66  .    s = fcntl(f
c27f0 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20 20 73  d, op, p);.    s
c2800 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
c2810 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e  f("fcntl unknown
c2820 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 66 64   %d %d %d\n", fd
c2830 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20 72 65  , op, s);.    re
c2840 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20 69 66  turn s;.  }.  if
c2850 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52  ( p->l_type==F_R
c2860 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70  DLCK ){.    zTyp
c2870 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 7d  e = "RDLCK";.  }
c2880 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79  else if( p->l_ty
c2890 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20  pe==F_WRLCK ){. 
c28a0 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43     zType = "WRLC
c28b0 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  K";.  }else if( 
c28c0 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c  p->l_type==F_UNL
c28d0 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20  CK ){.    zType 
c28e0 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c  = "UNLCK";.  }el
c28f0 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
c2900 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  0 );.  }.  asser
c2910 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d  t( p->l_whence==
c2920 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20 73 20  SEEK_SET );.  s 
c2930 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c 20  = fcntl(fd, op, 
c2940 70 29 3b 0a 20 20 73 61 76 65 64 45 72 72 6e 6f  p);.  savedErrno
c2950 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 71 6c 69   = errno;.  sqli
c2960 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
c2970 66 63 6e 74 6c 20 25 64 20 25 64 20 25 73 20 25  fcntl %d %d %s %
c2980 73 20 25 64 20 25 64 20 25 64 20 25 64 5c 6e 22  s %d %d %d %d\n"
c2990 2c 0a 20 20 20 20 20 74 68 72 65 61 64 69 64 2c  ,.     threadid,
c29a0 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54   fd, zOpName, zT
c29b0 79 70 65 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 73  ype, (int)p->l_s
c29c0 74 61 72 74 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f  tart, (int)p->l_
c29d0 6c 65 6e 2c 0a 20 20 20 20 20 28 69 6e 74 29 70  len,.     (int)p
c29e0 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a 20 20 69  ->l_pid, s);.  i
c29f0 66 28 20 73 3d 3d 28 2d 31 29 20 26 26 20 6f 70  f( s==(-1) && op
c2a00 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28 70 2d  ==F_SETLK && (p-
c2a10 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b  >l_type==F_RDLCK
c2a20 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46   || p->l_type==F
c2a30 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 20 20 20 73  _WRLCK) ){.    s
c2a40 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32 3b 0a  truct flock l2;.
c2a50 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 20 20 20      l2 = *p;.   
c2a60 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54   fcntl(fd, F_GET
c2a70 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 20 69 66  LK, &l2);.    if
c2a80 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 52  ( l2.l_type==F_R
c2a90 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54  DLCK ){.      zT
c2aa0 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20  ype = "RDLCK";. 
c2ab0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e     }else if( l2.
c2ac0 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20  l_type==F_WRLCK 
c2ad0 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d  ){.      zType =
c2ae0 20 22 57 52 4c 43 4b 22 3b 0a 20 20 20 20 7d 65   "WRLCK";.    }e
c2af0 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70  lse if( l2.l_typ
c2b00 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20  e==F_UNLCK ){.  
c2b10 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c      zType = "UNL
c2b20 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  CK";.    }else{.
c2b30 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20        assert( 0 
c2b40 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
c2b50 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
c2b60 22 66 63 6e 74 6c 2d 66 61 69 6c 75 72 65 2d 72  "fcntl-failure-r
c2b70 65 61 73 6f 6e 3a 20 25 73 20 25 64 20 25 64 20  eason: %s %d %d 
c2b80 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 7a 54  %d\n",.       zT
c2b90 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 73  ype, (int)l2.l_s
c2ba0 74 61 72 74 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f  tart, (int)l2.l_
c2bb0 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 70  len, (int)l2.l_p
c2bc0 69 64 29 3b 0a 20 20 7d 0a 20 20 65 72 72 6e 6f  id);.  }.  errno
c2bd0 20 3d 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20   = savedErrno;. 
c2be0 20 72 65 74 75 72 6e 20 73 3b 0a 7d 0a 23 64 65   return s;.}.#de
c2bf0 66 69 6e 65 20 66 63 6e 74 6c 20 6c 6f 63 6b 54  fine fcntl lockT
c2c00 72 61 63 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  race.#endif /* S
c2c10 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45  QLITE_LOCK_TRACE
c2c20 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   */..../*.** Thi
c2c30 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 6c  s routine transl
c2c40 61 74 65 73 20 61 20 73 74 61 6e 64 61 72 64 20  ates a standard 
c2c50 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f 64 65  POSIX errno code
c2c60 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a   into something.
c2c70 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 68 65  ** useful to the
c2c80 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 65 20   clients of the 
c2c90 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69 6f 6e  sqlite3 function
c2ca0 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79  s.  Specifically
c2cb0 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 65 6e  , it is.** inten
c2cc0 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65  ded to translate
c2cd0 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 22 74   a variety of "t
c2ce0 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f 72 73  ry again" errors
c2cf0 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 55 53   into SQLITE_BUS
c2d00 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 69 65  Y.** and a varie
c2d10 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20 63 6c  ty of "please cl
c2d20 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 65 73  ose the file des
c2d30 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 72 72  criptor NOW" err
c2d40 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c  ors into .** SQL
c2d50 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a  ITE_IOERR.** .**
c2d60 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67 20 69   Errors during i
c2d70 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66  nitialization of
c2d80 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c 65 20   locks, or file 
c2d90 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 66  system support f
c2da0 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f  or locks,.** sho
c2db0 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f 4c 43  uld handle ENOLC
c2dc0 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f 50 4e  K, ENOTSUP, EOPN
c2dd0 4f 54 53 55 50 50 20 73 65 70 61 72 61 74 65 6c  OTSUPP separatel
c2de0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
c2df0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
c2e00 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 20 70  PosixError(int p
c2e10 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 20 73  osixError, int s
c2e20 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a 20 20  qliteIOErr) {.  
c2e30 73 77 69 74 63 68 20 28 70 6f 73 69 78 45 72 72  switch (posixErr
c2e40 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30 3a 20  or) {.  case 0: 
c2e50 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
c2e60 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 63 61  TE_OK;.    .  ca
c2e70 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 61 73  se EAGAIN:.  cas
c2e80 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 20 63  e ETIMEDOUT:.  c
c2e90 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63 61 73  ase EBUSY:.  cas
c2ea0 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 65 20  e EINTR:.  case 
c2eb0 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 2f 2a  ENOLCK:  .    /*
c2ec0 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 74 72   random NFS retr
c2ed0 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 73 20  y error, unless 
c2ee0 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74  during file syst
c2ef0 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20 20  em support .    
c2f00 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e   * introspection
c2f10 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 61 63  , in which it ac
c2f20 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61  tually means wha
c2f30 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 20 20  t it says */.   
c2f40 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
c2f50 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 73 65  USY;.    .  case
c2f60 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20 2f 2a   EACCES: .    /*
c2f70 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b 65 20   EACCES is like 
c2f80 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20 6c 6f  EAGAIN during lo
c2f90 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73  cking operations
c2fa0 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 74  , but not any ot
c2fb0 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 20 69  her time*/.    i
c2fc0 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72 72 20  f( (sqliteIOErr 
c2fd0 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  == SQLITE_IOERR_
c2fe0 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69  LOCK) || ..(sqli
c2ff0 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54  teIOErr == SQLIT
c3000 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20  E_IOERR_UNLOCK) 
c3010 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72  || ..(sqliteIOEr
c3020 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  r == SQLITE_IOER
c3030 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73  R_RDLOCK) ||..(s
c3040 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51  qliteIOErr == SQ
c3050 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
c3060 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29 7b  RESERVEDLOCK) ){
c3070 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
c3080 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
c3090 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61 6c  .    /* else fal
c30a0 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 63  l through */.  c
c30b0 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20 20  ase EPERM: .    
c30c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45  return SQLITE_PE
c30d0 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20  RM;.    .  case 
c30e0 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74  EDEADLK:.    ret
c30f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
c3100 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 0a 23  _BLOCKED;.    .#
c3110 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 3d 45  if EOPNOTSUPP!=E
c3120 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4f  NOTSUP.  case EO
c3130 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 20 2f  PNOTSUPP: .    /
c3140 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74  * something went
c3150 20 74 65 72 72 69 62 6c 79 20 61 77 72 79 2c 20   terribly awry, 
c3160 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69  unless during fi
c3170 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  le system suppor
c3180 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73  t .     * intros
c3190 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63  pection, in whic
c31a0 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65  h it actually me
c31b0 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73  ans what it says
c31c0 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65   */.#endif.#ifde
c31d0 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65  f ENOTSUP.  case
c31e0 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 20 2f   ENOTSUP: .    /
c31f0 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 75 6e  * invalid fd, un
c3200 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65  less during file
c3210 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20   system support 
c3220 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69  introspection, i
c3230 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20 2a 20  n which .     * 
c3240 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e  it actually mean
c3250 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a  s what it says *
c3260 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 65 20  /.#endif.  case 
c3270 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 41 44  EIO:.  case EBAD
c3280 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 41 4c  F:.  case EINVAL
c3290 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 4f 4e  :.  case ENOTCON
c32a0 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 45 56  N:.  case ENODEV
c32b0 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f 3a 0a  :.  case ENXIO:.
c32c0 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a 0a 20    case ENOENT:. 
c32d0 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a 20 20   case ESTALE:.  
c32e0 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20  case ENOSYS:.   
c32f0 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 6c 64   /* these should
c3300 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69 65 6e   force the clien
c3310 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 66  t to close the f
c3320 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63  ile and reconnec
c3330 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66 61  t */.    .  defa
c3340 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 72 6e  ult: .    return
c3350 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20   sqliteIOErr;.  
c3360 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  }.}..../********
c3370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c33a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c33b0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
c33c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
c33d0 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 55  Unique File ID U
c33e0 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 56  tility Used By V
c33f0 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  xWorks *********
c3400 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20  ******.**.** On 
c3410 6d 6f 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66  most versions of
c3420 20 75 6e 69 78 2c 20 77 65 20 63 61 6e 20 67 65   unix, we can ge
c3430 74 20 61 20 75 6e 69 71 75 65 20 49 44 20 66 6f  t a unique ID fo
c3440 72 20 61 20 66 69 6c 65 20 62 79 20 63 6f 6e 63  r a file by conc
c3450 61 74 65 6e 61 74 69 6e 67 0a 2a 2a 20 74 68 65  atenating.** the
c3460 20 64 65 76 69 63 65 20 6e 75 6d 62 65 72 20 61   device number a
c3470 6e 64 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d  nd the inode num
c3480 62 65 72 2e 20 20 42 75 74 20 74 68 69 73 20 64  ber.  But this d
c3490 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20  oes not work on 
c34a0 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56  VxWorks..** On V
c34b0 78 57 6f 72 6b 73 2c 20 61 20 75 6e 69 71 75 65  xWorks, a unique
c34c0 20 66 69 6c 65 20 69 64 20 6d 75 73 74 20 62 65   file id must be
c34d0 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 61   based on the ca
c34e0 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65  nonical filename
c34f0 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  ..**.** A pointe
c3500 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65  r to an instance
c3510 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
c3520 67 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20  g structure can 
c3530 62 65 20 75 73 65 64 20 61 73 20 61 0a 2a 2a 20  be used as a.** 
c3540 75 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 69  unique file ID i
c3550 6e 20 56 78 57 6f 72 6b 73 2e 20 20 45 61 63 68  n VxWorks.  Each
c3560 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
c3570 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  s structure cont
c3580 61 69 6e 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f  ains.** a copy o
c3590 66 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20  f the canonical 
c35a0 66 69 6c 65 6e 61 6d 65 2e 20 20 54 68 65 72 65  filename.  There
c35b0 20 69 73 20 61 6c 73 6f 20 61 20 72 65 66 65 72   is also a refer
c35c0 65 6e 63 65 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a  ence count.  .**
c35d0 20 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69   The structure i
c35e0 73 20 72 65 63 6c 61 69 6d 65 64 20 77 68 65 6e  s reclaimed when
c35f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
c3600 6f 69 6e 74 65 72 73 20 74 6f 20 69 74 20 64 72  ointers to it dr
c3610 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a  ops to.** zero..
c3620 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
c3630 6e 65 76 65 72 20 76 65 72 79 20 6d 61 6e 79 20  never very many 
c3640 66 69 6c 65 73 20 6f 70 65 6e 20 61 74 20 6f 6e  files open at on
c3650 65 20 74 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75  e time and looku
c3660 70 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 20  ps are not.** a 
c3670 70 65 72 66 6f 72 6d 61 6e 63 65 2d 63 72 69 74  performance-crit
c3680 69 63 61 6c 20 70 61 74 68 2c 20 73 6f 20 69 74  ical path, so it
c3690 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 74   is sufficient t
c36a0 6f 20 70 75 74 20 74 68 65 73 65 0a 2a 2a 20 73  o put these.** s
c36b0 74 72 75 63 74 75 72 65 73 20 6f 6e 20 61 20 6c  tructures on a l
c36c0 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73  inked list..*/.s
c36d0 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c  truct vxworksFil
c36e0 65 49 64 20 7b 0a 20 20 73 74 72 75 63 74 20 76  eId {.  struct v
c36f0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e  xworksFileId *pN
c3700 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e  ext;  /* Next in
c3710 20 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20   a list of them 
c3720 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  all */.  int nRe
c3730 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
c3740 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
c3750 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
c3760 6f 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20  o this one */.  
c3770 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20  int nName;      
c3780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c3790 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 7a   Length of the z
c37a0 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20  CanonicalName[] 
c37b0 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72  string */.  char
c37c0 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65   *zCanonicalName
c37d0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e  ;         /* Can
c37e0 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20  onical filename 
c37f0 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56 58  */.};..#if OS_VX
c3800 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c  WORKS./* .** All
c3810 20 75 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d 65   unique filename
c3820 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 61 20  s are held on a 
c3830 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64  linked list head
c3840 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 76 61  ed by this.** va
c3850 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69  riable:.*/.stati
c3860 63 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73  c struct vxworks
c3870 46 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46  FileId *vxworksF
c3880 69 6c 65 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a  ileList = 0;../*
c3890 0a 2a 2a 20 53 69 6d 70 6c 69 66 79 20 61 20 66  .** Simplify a f
c38a0 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 69 74 73  ilename into its
c38b0 20 63 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a   canonical form.
c38c0 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20 74 68 65  ** by making the
c38d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67   following chang
c38e0 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d  es:.**.**  * rem
c38f0 6f 76 69 6e 67 20 61 6e 79 20 74 72 61 69 6c 69  oving any traili
c3900 6e 67 20 61 6e 64 20 64 75 70 6c 69 63 61 74 65  ng and duplicate
c3910 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74   /.**  * convert
c3920 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 74 20 2f   /./ into just /
c3930 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f  .**  * convert /
c3940 41 2f 2e 2e 2f 20 77 68 65 72 65 20 41 20 69 73  A/../ where A is
c3950 20 61 6e 79 20 73 69 6d 70 6c 65 20 6e 61 6d 65   any simple name
c3960 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a   into just /.**.
c3970 2a 2a 20 43 68 61 6e 67 65 73 20 61 72 65 20 6d  ** Changes are m
c3980 61 64 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 52  ade in-place.  R
c3990 65 74 75 72 6e 20 74 68 65 20 6e 65 77 20 6e 61  eturn the new na
c39a0 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a  me length..**.**
c39b0 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69   The original fi
c39c0 6c 65 6e 61 6d 65 20 69 73 20 69 6e 20 7a 5b 30  lename is in z[0
c39d0 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20  ..n-1].  Return 
c39e0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
c39f0 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74   characters in t
c3a00 68 65 20 73 69 6d 70 6c 69 66 69 65 64 20 6e 61  he simplified na
c3a10 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  me..*/.static in
c3a20 74 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66  t vxworksSimplif
c3a30 79 4e 61 6d 65 28 63 68 61 72 20 2a 7a 2c 20 69  yName(char *z, i
c3a40 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20  nt n){.  int i, 
c3a50 6a 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20  j;.  while( n>1 
c3a60 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29  && z[n-1]=='/' )
c3a70 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28 69  { n--; }.  for(i
c3a80 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b  =j=0; i<n; i++){
c3a90 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27  .    if( z[i]=='
c3aa0 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  /' ){.      if( 
c3ab0 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f  z[i+1]=='/' ) co
c3ac0 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66  ntinue;.      if
c3ad0 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26  ( z[i+1]=='.' &&
c3ae0 20 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d   i+2<n && z[i+2]
c3af0 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20  =='/' ){.       
c3b00 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20   i += 1;.       
c3b10 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
c3b20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69   }.      if( z[i
c3b30 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 33 3c  +1]=='.' && i+3<
c3b40 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27  n && z[i+2]=='.'
c3b50 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20   && z[i+3]=='/' 
c3b60 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  ){.        while
c3b70 28 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d 21  ( j>0 && z[j-1]!
c3b80 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20  ='/' ){ j--; }. 
c3b90 20 20 20 20 20 20 20 69 66 28 20 6a 3e 30 20 29         if( j>0 )
c3ba0 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20  { j--; }.       
c3bb0 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20   i += 2;.       
c3bc0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
c3bd0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a   }.    }.    z[j
c3be0 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a  ++] = z[i];.  }.
c3bf0 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65    z[j] = 0;.  re
c3c00 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn j;.}../*.**
c3c10 20 46 69 6e 64 20 61 20 75 6e 69 71 75 65 20 66   Find a unique f
c3c20 69 6c 65 20 49 44 20 66 6f 72 20 74 68 65 20 67  ile ID for the g
c3c30 69 76 65 6e 20 61 62 73 6f 6c 75 74 65 20 70 61  iven absolute pa
c3c40 74 68 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 0a  thname.  Return.
c3c50 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
c3c60 74 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  the vxworksFileI
c3c70 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20  d object.  This 
c3c80 70 6f 69 6e 74 65 72 20 69 73 20 74 68 65 20 75  pointer is the u
c3c90 6e 69 71 75 65 0a 2a 2a 20 66 69 6c 65 20 49 44  nique.** file ID
c3ca0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52 65 66  ..**.** The nRef
c3cb0 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 76 78   field of the vx
c3cc0 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65  worksFileId obje
c3cd0 63 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  ct is incremente
c3ce0 64 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20  d before.** the 
c3cf0 6f 62 6a 65 63 74 20 69 73 20 72 65 74 75 72 6e  object is return
c3d00 65 64 2e 20 20 41 20 6e 65 77 20 76 78 77 6f 72  ed.  A new vxwor
c3d10 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20  ksFileId object 
c3d20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e  is created.** an
c3d30 64 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 67  d added to the g
c3d40 6c 6f 62 61 6c 20 6c 69 73 74 20 69 66 20 6e 65  lobal list if ne
c3d50 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49  cessary..**.** I
c3d60 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
c3d70 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75  ation error occu
c3d80 72 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  rs, return NULL.
c3d90 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
c3da0 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  t vxworksFileId 
c3db0 2a 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65  *vxworksFindFile
c3dc0 49 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  Id(const char *z
c3dd0 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a 20  AbsoluteName){. 
c3de0 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46   struct vxworksF
c3df0 69 6c 65 49 64 20 2a 70 4e 65 77 3b 20 20 20 20  ileId *pNew;    
c3e00 20 20 20 20 20 2f 2a 20 73 65 61 72 63 68 20 6b       /* search k
c3e10 65 79 20 61 6e 64 20 6e 65 77 20 66 69 6c 65 20  ey and new file 
c3e20 49 44 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76  ID */.  struct v
c3e30 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 43  xworksFileId *pC
c3e40 61 6e 64 69 64 61 74 65 3b 20 20 20 2f 2a 20 46  andidate;   /* F
c3e50 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
c3e60 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20 49 44  existing file ID
c3e70 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20  s */.  int n;   
c3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3e90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
c3ea0 6e 67 74 68 20 6f 66 20 7a 41 62 73 6f 6c 75 74  ngth of zAbsolut
c3eb0 65 4e 61 6d 65 20 73 74 72 69 6e 67 20 2a 2f 0a  eName string */.
c3ec0 0a 20 20 61 73 73 65 72 74 28 20 7a 41 62 73 6f  .  assert( zAbso
c3ed0 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27  luteName[0]=='/'
c3ee0 20 29 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29 73   );.  n = (int)s
c3ef0 74 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 74 65 4e  trlen(zAbsoluteN
c3f00 61 6d 65 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73  ame);.  pNew = s
c3f10 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73  qlite3_malloc( s
c3f20 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20 28  izeof(*pNew) + (
c3f30 6e 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 70 4e  n+1) );.  if( pN
c3f40 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ew==0 ) return 0
c3f50 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e  ;.  pNew->zCanon
c3f60 69 63 61 6c 4e 61 6d 65 20 3d 20 28 63 68 61 72  icalName = (char
c3f70 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65  *)&pNew[1];.  me
c3f80 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f  mcpy(pNew->zCano
c3f90 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f  nicalName, zAbso
c3fa0 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a  luteName, n+1);.
c3fb0 20 20 6e 20 3d 20 76 78 77 6f 72 6b 73 53 69 6d    n = vxworksSim
c3fc0 70 6c 69 66 79 4e 61 6d 65 28 70 4e 65 77 2d 3e  plifyName(pNew->
c3fd0 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20  zCanonicalName, 
c3fe0 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68  n);..  /* Search
c3ff0 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67   for an existing
c4000 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63   entry that matc
c4010 68 69 6e 67 20 74 68 65 20 63 61 6e 6f 6e 69 63  hing the canonic
c4020 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49 66  al name..  ** If
c4030 20 66 6f 75 6e 64 2c 20 69 6e 63 72 65 6d 65 6e   found, incremen
c4040 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  t the reference 
c4050 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e  count and return
c4060 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 20 20   a pointer to.  
c4070 2a 2a 20 74 68 65 20 65 78 69 73 74 69 6e 67 20  ** the existing 
c4080 66 69 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a 20 20  file ID..  */.  
c4090 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
c40a0 3b 0a 20 20 66 6f 72 28 70 43 61 6e 64 69 64 61  ;.  for(pCandida
c40b0 74 65 3d 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69  te=vxworksFileLi
c40c0 73 74 3b 20 70 43 61 6e 64 69 64 61 74 65 3b 20  st; pCandidate; 
c40d0 70 43 61 6e 64 69 64 61 74 65 3d 70 43 61 6e 64  pCandidate=pCand
c40e0 69 64 61 74 65 2d 3e 70 4e 65 78 74 29 7b 0a 20  idate->pNext){. 
c40f0 20 20 20 69 66 28 20 70 43 61 6e 64 69 64 61 74     if( pCandidat
c4100 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20  e->nName==n .   
c4110 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 43 61 6e    && memcmp(pCan
c4120 64 69 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63  didate->zCanonic
c4130 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43  alName, pNew->zC
c4140 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29  anonicalName, n)
c4150 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ==0.    ){.     
c4160 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
c4170 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 70 43 61  New);.       pCa
c4180 6e 64 69 64 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b  ndidate->nRef++;
c4190 0a 20 20 20 20 20 20 20 75 6e 69 78 4c 65 61 76  .       unixLeav
c41a0 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20  eMutex();.      
c41b0 20 72 65 74 75 72 6e 20 70 43 61 6e 64 69 64 61   return pCandida
c41c0 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  te;.    }.  }.. 
c41d0 20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20 77 61 73   /* No match was
c41e0 20 66 6f 75 6e 64 2e 20 20 57 65 20 77 69 6c 6c   found.  We will
c41f0 20 6d 61 6b 65 20 61 20 6e 65 77 20 66 69 6c 65   make a new file
c4200 20 49 44 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e   ID */.  pNew->n
c4210 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d  Ref = 1;.  pNew-
c4220 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e  >nName = n;.  pN
c4230 65 77 2d 3e 70 4e 65 78 74 20 3d 20 76 78 77 6f  ew->pNext = vxwo
c4240 72 6b 73 46 69 6c 65 4c 69 73 74 3b 0a 20 20 76  rksFileList;.  v
c4250 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d  xworksFileList =
c4260 20 70 4e 65 77 3b 0a 20 20 75 6e 69 78 4c 65 61   pNew;.  unixLea
c4270 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74  veMutex();.  ret
c4280 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
c4290 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  ** Decrement the
c42a0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
c42b0 20 6f 6e 20 61 20 76 78 77 6f 72 6b 73 46 69 6c   on a vxworksFil
c42c0 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 46 72 65  eId object.  Fre
c42d0 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 20  e.** the object 
c42e0 77 68 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e  when the referen
c42f0 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73  ce count reaches
c4300 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63   zero..*/.static
c4310 20 76 6f 69 64 20 76 78 77 6f 72 6b 73 52 65 6c   void vxworksRel
c4320 65 61 73 65 46 69 6c 65 49 64 28 73 74 72 75 63  easeFileId(struc
c4330 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  t vxworksFileId 
c4340 2a 70 49 64 29 7b 0a 20 20 75 6e 69 78 45 6e 74  *pId){.  unixEnt
c4350 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 73  erMutex();.  ass
c4360 65 72 74 28 20 70 49 64 2d 3e 6e 52 65 66 3e 30  ert( pId->nRef>0
c4370 20 29 3b 0a 20 20 70 49 64 2d 3e 6e 52 65 66 2d   );.  pId->nRef-
c4380 2d 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e 6e 52  -;.  if( pId->nR
c4390 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72  ef==0 ){.    str
c43a0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
c43b0 64 20 2a 2a 70 70 3b 0a 20 20 20 20 66 6f 72 28  d **pp;.    for(
c43c0 70 70 3d 26 76 78 77 6f 72 6b 73 46 69 6c 65 4c  pp=&vxworksFileL
c43d0 69 73 74 3b 20 2a 70 70 20 26 26 20 2a 70 70 21  ist; *pp && *pp!
c43e0 3d 70 49 64 3b 20 70 70 20 3d 20 26 28 28 2a 70  =pId; pp = &((*p
c43f0 70 29 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a 20 20  p)->pNext)){}.  
c4400 20 20 61 73 73 65 72 74 28 20 2a 70 70 3d 3d 70    assert( *pp==p
c4410 49 64 20 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20  Id );.    *pp = 
c4420 70 49 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pId->pNext;.    
c4430 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64  sqlite3_free(pId
c4440 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61  );.  }.  unixLea
c4450 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e  veMutex();.}.#en
c4460 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b  dif /* OS_VXWORK
c4470 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  S */./**********
c4480 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 55 6e 69  ***** End of Uni
c4490 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c  que File ID Util
c44a0 69 74 79 20 55 73 65 64 20 42 79 20 56 78 57 6f  ity Used By VxWo
c44b0 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  rks ************
c44c0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
c44d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c44e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c44f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4510 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ***/.../********
c4520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4560 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
c4570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4580 2a 2a 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72  ** Posix Advisor
c4590 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a  y Locking ******
c45a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c45b0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53  ******.**.** POS
c45c0 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b  IX advisory lock
c45d0 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 62 79 20  s are broken by 
c45e0 64 65 73 69 67 6e 2e 20 20 41 4e 53 49 20 53 54  design.  ANSI ST
c45f0 44 20 31 30 30 33 2e 31 20 28 31 39 39 36 29 0a  D 1003.1 (1996).
c4600 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32  ** section 6.5.2
c4610 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 74 68 72  .2 lines 483 thr
c4620 6f 75 67 68 20 34 39 30 20 73 70 65 63 69 66 79  ough 490 specify
c4630 20 74 68 61 74 20 77 68 65 6e 20 61 20 70 72 6f   that when a pro
c4640 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20  cess.** sets or 
c4650 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74  clears a lock, t
c4660 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76  hat operation ov
c4670 65 72 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f  errides any prio
c4680 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62  r locks set.** b
c4690 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  y the same proce
c46a0 73 73 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74  ss.  It does not
c46b0 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 79 20   explicitly say 
c46c0 73 6f 2c 20 62 75 74 20 74 68 69 73 20 69 6d 70  so, but this imp
c46d0 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 69 74 20  lies.** that it 
c46e0 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 20  overrides locks 
c46f0 73 65 74 20 62 79 20 74 68 65 20 73 61 6d 65 20  set by the same 
c4700 70 72 6f 63 65 73 73 20 75 73 69 6e 67 20 61 20  process using a 
c4710 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c  different.** fil
c4720 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43  e descriptor.  C
c4730 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74 65 73  onsider this tes
c4740 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  t case:.**.**   
c4750 20 20 20 20 69 6e 74 20 66 64 31 20 3d 20 6f 70      int fd1 = op
c4760 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f  en("./file1", O_
c4770 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36  RDWR|O_CREAT, 06
c4780 34 34 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e  44);.**       in
c4790 74 20 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f  t fd2 = open("./
c47a0 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f  file2", O_RDWR|O
c47b0 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a  _CREAT, 0644);.*
c47c0 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66  *.** Suppose ./f
c47d0 69 6c 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32  ile1 and ./file2
c47e0 20 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20   are really the 
c47f0 73 61 6d 65 20 66 69 6c 65 20 28 62 65 63 61 75  same file (becau
c4800 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68  se.** one is a h
c4810 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20  ard or symbolic 
c4820 6c 69 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68 65  link to the othe
c4830 72 29 20 74 68 65 6e 20 69 66 20 79 6f 75 20 73  r) then if you s
c4840 65 74 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69  et.** an exclusi
c4850 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20  ve lock on fd1, 
c4860 74 68 65 6e 20 74 72 79 20 74 6f 20 67 65 74 20  then try to get 
c4870 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
c4880 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20  k.** on fd2, it 
c4890 77 6f 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20  works.  I would 
c48a0 68 61 76 65 20 65 78 70 65 63 74 65 64 20 74 68  have expected th
c48b0 65 20 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f  e second lock to
c48c0 0a 2a 2a 20 66 61 69 6c 20 73 69 6e 63 65 20 74  .** fail since t
c48d0 68 65 72 65 20 77 61 73 20 61 6c 72 65 61 64 79  here was already
c48e0 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66   a lock on the f
c48f0 69 6c 65 20 64 75 65 20 74 6f 20 66 64 31 2e 0a  ile due to fd1..
c4900 2a 2a 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20  ** But not so.  
c4910 53 69 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73  Since both locks
c4920 20 63 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 73   came from the s
c4930 61 6d 65 20 70 72 6f 63 65 73 73 2c 20 74 68 65  ame process, the
c4940 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72 72  .** second overr
c4950 69 64 65 73 20 74 68 65 20 66 69 72 73 74 2c 20  ides the first, 
c4960 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79  even though they
c4970 20 77 65 72 65 20 6f 6e 20 64 69 66 66 65 72 65   were on differe
c4980 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72  nt.** file descr
c4990 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e  iptors opened on
c49a0 20 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65 20   different file 
c49b0 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  names..**.** Thi
c49c0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20  s means that we 
c49d0 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58  cannot use POSIX
c49e0 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72   locks to synchr
c49f0 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73  onize file acces
c4a00 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 65  s.** among compe
c4a10 74 69 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20  ting threads of 
c4a20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
c4a30 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77  .  POSIX locks w
c4a40 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a  ill work fine.**
c4a50 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20   to synchronize 
c4a60 61 63 63 65 73 73 20 66 6f 72 20 74 68 72 65 61  access for threa
c4a70 64 73 20 69 6e 20 73 65 70 61 72 61 74 65 20 70  ds in separate p
c4a80 72 6f 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f  rocesses, but no
c4a90 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 69 74  t.** threads wit
c4aa0 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f  hin the same pro
c4ab0 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77  cess..**.** To w
c4ac0 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70  ork around the p
c4ad0 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68  roblem, SQLite h
c4ae0 61 73 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c  as to manage fil
c4af0 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c  e locks internal
c4b00 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e  ly.** on its own
c4b10 2e 20 20 57 68 65 6e 65 76 65 72 20 61 20 6e 65  .  Whenever a ne
c4b20 77 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  w database is op
c4b30 65 6e 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f  ened, we have to
c4b40 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65   find the.** spe
c4b50 63 69 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 74  cific inode of t
c4b60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
c4b70 20 28 74 68 65 20 69 6e 6f 64 65 20 69 73 20 64   (the inode is d
c4b80 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65  etermined by the
c4b90 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 73  .** st_dev and s
c4ba0 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20  t_ino fields of 
c4bb0 74 68 65 20 73 74 61 74 20 73 74 72 75 63 74 75  the stat structu
c4bc0 72 65 20 74 68 61 74 20 66 73 74 61 74 28 29 20  re that fstat() 
c4bd0 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64  fills in).** and
c4be0 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73   check for locks
c4bf0 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 69 6e   already existin
c4c00 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e  g on that inode.
c4c10 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65    When locks are
c4c20 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f 72 20 72  .** created or r
c4c30 65 6d 6f 76 65 64 2c 20 77 65 20 68 61 76 65 20  emoved, we have 
c4c40 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f  to look at our o
c4c50 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f  wn internal reco
c4c60 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63  rd of the.** loc
c4c70 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f  ks to see if ano
c4c80 74 68 65 72 20 74 68 72 65 61 64 20 68 61 73 20  ther thread has 
c4c90 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 61  previously set a
c4ca0 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 61   lock on that sa
c4cb0 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a  me.** inode..**.
c4cc0 2a 2a 20 28 41 73 69 64 65 3a 20 54 68 65 20 75  ** (Aside: The u
c4cd0 73 65 20 6f 66 20 69 6e 6f 64 65 20 6e 75 6d 62  se of inode numb
c4ce0 65 72 73 20 61 73 20 75 6e 69 71 75 65 20 49 44  ers as unique ID
c4cf0 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20  s does not work 
c4d00 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46  on VxWorks..** F
c4d10 6f 72 20 56 78 57 6f 72 6b 73 2c 20 77 65 20 68  or VxWorks, we h
c4d20 61 76 65 20 74 6f 20 75 73 65 20 74 68 65 20 61  ave to use the a
c4d30 6c 74 65 72 6e 61 74 69 76 65 20 75 6e 69 71 75  lternative uniqu
c4d40 65 20 49 44 20 73 79 73 74 65 6d 20 62 61 73 65  e ID system base
c4d50 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61  d on.** canonica
c4d60 6c 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69  l filename and i
c4d70 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 68  mplemented in th
c4d80 65 20 70 72 65 76 69 6f 75 73 20 64 69 76 69 73  e previous divis
c4d90 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ion.).**.** The 
c4da0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72  sqlite3_file str
c4db0 75 63 74 75 72 65 20 66 6f 72 20 50 4f 53 49 58  ucture for POSIX
c4dc0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75   is no longer ju
c4dd0 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 66 69  st an integer fi
c4de0 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72  le.** descriptor
c4df0 2e 20 20 49 74 20 69 73 20 6e 6f 77 20 61 20 73  .  It is now a s
c4e00 74 72 75 63 74 75 72 65 20 74 68 61 74 20 68 6f  tructure that ho
c4e10 6c 64 73 20 74 68 65 20 69 6e 74 65 67 65 72 20  lds the integer 
c4e20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74  file.** descript
c4e30 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72  or and a pointer
c4e40 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20   to a structure 
c4e50 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
c4e60 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c  he internal.** l
c4e70 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 72 72  ocks on the corr
c4e80 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 2e  esponding inode.
c4e90 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6c    There is one l
c4ea0 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65  ocking structure
c4eb0 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 65 2c 20 73  .** per inode, s
c4ec0 6f 20 69 66 20 74 68 65 20 73 61 6d 65 20 69 6e  o if the same in
c4ed0 6f 64 65 20 69 73 20 6f 70 65 6e 65 64 20 74 77  ode is opened tw
c4ee0 69 63 65 2c 20 62 6f 74 68 20 75 6e 69 78 46 69  ice, both unixFi
c4ef0 6c 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a  le structures.**
c4f00 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 61   point to the sa
c4f10 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63  me locking struc
c4f20 74 75 72 65 2e 20 20 54 68 65 20 6c 6f 63 6b 69  ture.  The locki
c4f30 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  ng structure kee
c4f40 70 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63  ps.** a referenc
c4f50 65 20 63 6f 75 6e 74 20 28 73 6f 20 77 65 20 77  e count (so we w
c4f60 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 74 6f  ill know when to
c4f70 20 64 65 6c 65 74 65 20 69 74 29 20 61 6e 64 20   delete it) and 
c4f80 61 20 22 63 6e 74 22 0a 2a 2a 20 66 69 65 6c 64  a "cnt".** field
c4f90 20 74 68 61 74 20 74 65 6c 6c 73 20 75 73 20 69   that tells us i
c4fa0 74 73 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b  ts internal lock
c4fb0 20 73 74 61 74 75 73 2e 20 20 63 6e 74 3d 3d 30   status.  cnt==0
c4fc0 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 66 69   means the.** fi
c4fd0 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 20  le is unlocked. 
c4fe0 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 74   cnt==-1 means t
c4ff0 68 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 65  he file has an e
c5000 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a  xclusive lock..*
c5010 2a 20 63 6e 74 3e 30 20 6d 65 61 6e 73 20 74 68  * cnt>0 means th
c5020 65 72 65 20 61 72 65 20 63 6e 74 20 73 68 61 72  ere are cnt shar
c5030 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20  ed locks on the 
c5040 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20  file..**.** Any 
c5050 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20  attempt to lock 
c5060 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 66 69 6c 65  or unlock a file
c5070 20 66 69 72 73 74 20 63 68 65 63 6b 73 20 74 68   first checks th
c5080 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72  e locking.** str
c5090 75 63 74 75 72 65 2e 20 20 54 68 65 20 66 63 6e  ucture.  The fcn
c50a0 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c  tl() system call
c50b0 20 69 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64   is only invoked
c50c0 20 74 6f 20 73 65 74 20 61 20 0a 2a 2a 20 50 4f   to set a .** PO
c50d0 53 49 58 20 6c 6f 63 6b 20 69 66 20 74 68 65 20  SIX lock if the 
c50e0 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74  internal lock st
c50f0 72 75 63 74 75 72 65 20 74 72 61 6e 73 69 74 69  ructure transiti
c5100 6f 6e 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61  ons between.** a
c5110 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 75   locked and an u
c5120 6e 6c 6f 63 6b 65 64 20 73 74 61 74 65 2e 0a 2a  nlocked state..*
c5130 2a 0a 2a 2a 20 42 75 74 20 77 61 69 74 3a 20 20  *.** But wait:  
c5140 74 68 65 72 65 20 61 72 65 20 79 65 74 20 6d 6f  there are yet mo
c5150 72 65 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68  re problems with
c5160 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20   POSIX advisory 
c5170 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  locks..**.** If 
c5180 79 6f 75 20 63 6c 6f 73 65 20 61 20 66 69 6c 65  you close a file
c5190 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 74   descriptor that
c51a0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 69 6c   points to a fil
c51b0 65 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 73  e that has locks
c51c0 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f  ,.** all locks o
c51d0 6e 20 74 68 61 74 20 66 69 6c 65 20 74 68 61 74  n that file that
c51e0 20 61 72 65 20 6f 77 6e 65 64 20 62 79 20 74 68   are owned by th
c51f0 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73  e current proces
c5200 73 20 61 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65  s are.** release
c5210 64 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75  d.  To work arou
c5220 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c  nd this problem,
c5230 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65 20 73   each unixFile s
c5240 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
c5250 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  s.** a pointer t
c5260 6f 20 61 6e 20 75 6e 69 78 4f 70 65 6e 43 6e 74  o an unixOpenCnt
c5270 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
c5280 72 65 20 69 73 20 6f 6e 65 20 75 6e 69 78 4f 70  re is one unixOp
c5290 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 0a  enCnt structure.
c52a0 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64  ** per open inod
c52b0 65 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74  e, which means t
c52c0 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 6e 69  hat multiple uni
c52d0 78 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e 74 20  xFile can point 
c52e0 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 75  to a single.** u
c52f0 6e 69 78 4f 70 65 6e 43 6e 74 2e 20 20 57 68 65  nixOpenCnt.  Whe
c5300 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20  n an attempt is 
c5310 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61 6e  made to close an
c5320 20 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74 68   unixFile, if th
c5330 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65 72  ere are.** other
c5340 20 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20 6f   unixFile open o
c5350 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65  n the same inode
c5360 20 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69 6e   that are holdin
c5370 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61 6c  g locks, the cal
c5380 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29 20  l.** to close() 
c5390 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
c53a0 74 6f 72 20 69 73 20 64 65 66 65 72 72 65 64 20  tor is deferred 
c53b0 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68 65  until all of the
c53c0 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a   locks clear..**
c53d0 20 54 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74   The unixOpenCnt
c53e0 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73   structure keeps
c53f0 20 61 20 6c 69 73 74 20 6f 66 20 66 69 6c 65 20   a list of file 
c5400 64 65 73 63 72 69 70 74 6f 72 73 20 74 68 61 74  descriptors that
c5410 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65 20 63   need to.** be c
c5420 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 74 20 6c  losed and that l
c5430 69 73 74 20 69 73 20 77 61 6c 6b 65 64 20 28 61  ist is walked (a
c5440 6e 64 20 63 6c 65 61 72 65 64 29 20 77 68 65 6e  nd cleared) when
c5450 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 0a 2a   the last lock.*
c5460 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a 2a 20  * clears..**.** 
c5470 59 65 74 20 61 6e 6f 74 68 65 72 20 70 72 6f 62  Yet another prob
c5480 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72 65 61  lem:  LinuxThrea
c5490 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79 20 77  ds do not play w
c54a0 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 78 20 6c  ell with posix l
c54b0 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79  ocks..**.** Many
c54c0 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20   older versions 
c54d0 6f 66 20 6c 69 6e 75 78 20 75 73 65 20 74 68 65  of linux use the
c54e0 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 6c 69   LinuxThreads li
c54f0 62 72 61 72 79 20 77 68 69 63 68 20 69 73 0a 2a  brary which is.*
c5500 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63 6f 6d 70  * not posix comp
c5510 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72 20 4c 69  liant.  Under Li
c5520 6e 75 78 54 68 72 65 61 64 73 2c 20 61 20 6c 6f  nuxThreads, a lo
c5530 63 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 68  ck created by th
c5540 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e 6e 6f 74  read.** A cannot
c5550 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 72 20   be modified or 
c5560 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 61 20  overridden by a 
c5570 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
c5580 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 72 65   B..** Only thre
c5590 61 64 20 41 20 63 61 6e 20 6d 6f 64 69 66 79 20  ad A can modify 
c55a0 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f 63 6b 69  the lock.  Locki
c55b0 6e 67 20 62 65 68 61 76 69 6f 72 20 69 73 20 63  ng behavior is c
c55c0 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20 74 68 65  orrect.** if the
c55d0 20 61 70 70 6c 69 61 74 69 6f 6e 20 75 73 65 73   appliation uses
c55e0 20 74 68 65 20 6e 65 77 65 72 20 4e 61 74 69 76   the newer Nativ
c55f0 65 20 50 6f 73 69 78 20 54 68 72 65 61 64 20 4c  e Posix Thread L
c5600 69 62 72 61 72 79 20 28 4e 50 54 4c 29 0a 2a 2a  ibrary (NPTL).**
c5610 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 69 74 68   on linux - with
c5620 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 63 72 65   NPTL a lock cre
c5630 61 74 65 64 20 62 79 20 74 68 72 65 61 64 20 41  ated by thread A
c5640 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f   can override lo
c5650 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 65 61 64  cks.** in thread
c5660 20 42 2e 20 20 42 75 74 20 74 68 65 72 65 20 69   B.  But there i
c5670 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f 77  s no way to know
c5680 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
c5690 20 77 68 69 63 68 0a 2a 2a 20 74 68 72 65 61 64   which.** thread
c56a0 69 6e 67 20 6c 69 62 72 61 72 79 20 69 73 20 62  ing library is b
c56b0 65 69 6e 67 20 75 73 65 64 2e 20 20 53 6f 20 74  eing used.  So t
c56c0 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74  here is no way t
c56d0 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 63 6f 6d  o know at.** com
c56e0 70 69 6c 65 2d 74 69 6d 65 20 77 68 65 74 68 65  pile-time whethe
c56f0 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 20  r or not thread 
c5700 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c  A can override l
c5710 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61 64 20 42  ocks on thread B
c5720 2e 0a 2a 2a 20 57 65 20 68 61 76 65 20 74 6f 20  ..** We have to 
c5730 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65 20 63 68  do a run-time ch
c5740 65 63 6b 20 74 6f 20 64 69 73 63 6f 76 65 72 20  eck to discover 
c5750 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20  the behavior of 
c5760 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 70  the.** current p
c5770 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  rocess..**.** On
c5780 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 74   systems where t
c5790 68 72 65 61 64 20 41 20 69 73 20 75 6e 61 62 6c  hread A is unabl
c57a0 65 20 74 6f 20 6d 6f 64 69 66 79 20 6c 6f 63 6b  e to modify lock
c57b0 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20  s created by.** 
c57c0 74 68 72 65 61 64 20 42 2c 20 77 65 20 68 61 76  thread B, we hav
c57d0 65 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20  e to keep track 
c57e0 6f 66 20 77 68 69 63 68 20 74 68 72 65 61 64 20  of which thread 
c57f0 63 72 65 61 74 65 64 20 65 61 63 68 0a 2a 2a 20  created each.** 
c5800 6c 6f 63 6b 2e 20 20 48 65 6e 63 65 20 74 68 65  lock.  Hence the
c5810 72 65 20 69 73 20 61 6e 20 65 78 74 72 61 20 66  re is an extra f
c5820 69 65 6c 64 20 69 6e 20 74 68 65 20 6b 65 79 20  ield in the key 
c5830 74 6f 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49  to the unixLockI
c5840 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  nfo.** structure
c5850 20 74 6f 20 72 65 63 6f 72 64 20 74 68 69 73 20   to record this 
c5860 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 6e  information.  An
c5870 64 20 6f 6e 20 74 68 6f 73 65 20 73 79 73 74 65  d on those syste
c5880 6d 73 20 69 74 0a 2a 2a 20 69 73 20 69 6c 6c 65  ms it.** is ille
c5890 67 61 6c 20 74 6f 20 62 65 67 69 6e 20 61 20 74  gal to begin a t
c58a0 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 6f 6e  ransaction in on
c58b0 65 20 74 68 72 65 61 64 20 61 6e 64 20 66 69 6e  e thread and fin
c58c0 69 73 68 20 69 74 0a 2a 2a 20 69 6e 20 61 6e 6f  ish it.** in ano
c58d0 74 68 65 72 2e 20 20 46 6f 72 20 74 68 69 73 20  ther.  For this 
c58e0 6c 61 74 74 65 72 20 72 65 73 74 72 69 63 74 69  latter restricti
c58f0 6f 6e 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20  on, there is no 
c5900 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a 2a 2a 20  work-around..** 
c5910 49 74 20 69 73 20 61 20 6c 69 6d 69 74 61 74 69  It is a limitati
c5920 6f 6e 20 6f 66 20 4c 69 6e 75 78 54 68 72 65 61  on of LinuxThrea
c5930 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65  ds..*/../*.** Se
c5940 74 20 6f 72 20 63 68 65 63 6b 20 74 68 65 20 75  t or check the u
c5950 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 69 65 6c  nixFile.tid fiel
c5960 64 2e 20 20 54 68 69 73 20 66 69 65 6c 64 20 69  d.  This field i
c5970 73 20 73 65 74 20 77 68 65 6e 20 61 6e 20 75 6e  s set when an un
c5980 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66 69 72  ixFile.** is fir
c5990 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c 6c 20  st opened.  All 
c59a0 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 73 20  subsequent uses 
c59b0 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20  of the unixFile 
c59c0 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 0a  verify that the.
c59d0 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 64 20 69  ** same thread i
c59e0 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e 20 74  s operating on t
c59f0 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 20 53 6f  he unixFile.  So
c5a00 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  me operating sys
c5a10 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74 20 61  tems do.** not a
c5a20 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20 62 65  llow locks to be
c5a30 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6f   overridden by o
c5a40 74 68 65 72 20 74 68 72 65 61 64 73 20 61 6e 64  ther threads and
c5a50 20 74 68 61 74 20 72 65 73 74 72 69 63 74 69 6f   that restrictio
c5a60 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20  n.** means that 
c5a70 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73  sqlite3* databas
c5a80 65 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e 6f 74  e handles cannot
c5a90 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f   be moved from o
c5aa0 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 6f 20  ne thread.** to 
c5ab0 61 6e 6f 74 68 65 72 20 77 68 69 6c 65 20 6c 6f  another while lo
c5ac0 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 0a 2a 2a  cks are held..**
c5ad0 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e 33 2e  .** Version 3.3.
c5ae0 31 20 28 32 30 30 36 2d 30 31 2d 31 35 29 3a 20  1 (2006-01-15): 
c5af0 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 62 65   unixFile can be
c5b00 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20   moved from one 
c5b10 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61 6e 6f  thread to.** ano
c5b20 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 61 73 20  ther as long as 
c5b30 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 6f  we are running o
c5b40 6e 20 61 20 73 79 73 74 65 6d 20 74 68 61 74 20  n a system that 
c5b50 73 75 70 70 6f 72 74 73 20 74 68 72 65 61 64 73  supports threads
c5b60 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67 20 65  .** overriding e
c5b70 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
c5b80 20 28 77 68 69 63 68 20 69 73 20 6e 6f 77 20 74   (which is now t
c5b90 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62  he most common b
c5ba0 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 69  ehavior).** or i
c5bb0 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68  f no locks are h
c5bc0 65 6c 64 2e 20 20 42 75 74 20 74 68 65 20 75 6e  eld.  But the un
c5bd0 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65  ixFile.pLock fie
c5be0 6c 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a  ld needs to be.*
c5bf0 2a 20 72 65 63 6f 6d 70 75 74 65 64 20 62 65 63  * recomputed bec
c5c00 61 75 73 65 20 69 74 73 20 6b 65 79 20 69 6e 63  ause its key inc
c5c10 6c 75 64 65 73 20 74 68 65 20 74 68 72 65 61 64  ludes the thread
c5c20 2d 69 64 2e 20 20 53 65 65 20 74 68 65 20 0a 2a  -id.  See the .*
c5c30 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  * transferOwners
c5c40 68 69 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 62  hip() function b
c5c50 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f  elow for additio
c5c60 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  nal information.
c5c70 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
c5c80 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69  READSAFE && defi
c5c90 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23  ned(__linux__).#
c5ca0 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45   define SET_THRE
c5cb0 41 44 49 44 28 58 29 20 20 20 28 58 29 2d 3e 74  ADID(X)   (X)->t
c5cc0 69 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c  id = pthread_sel
c5cd0 66 28 29 0a 23 20 64 65 66 69 6e 65 20 43 48 45  f().# define CHE
c5ce0 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29 20 28  CK_THREADID(X) (
c5cf0 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45  threadsOverrideE
c5d00 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d  achOthersLocks==
c5d10 30 20 26 26 20 5c 0a 20 20 20 20 20 20 20 20 20  0 && \.         
c5d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5d30 20 20 20 21 70 74 68 72 65 61 64 5f 65 71 75 61     !pthread_equa
c5d40 6c 28 28 58 29 2d 3e 74 69 64 2c 20 70 74 68 72  l((X)->tid, pthr
c5d50 65 61 64 5f 73 65 6c 66 28 29 29 29 0a 23 65 6c  ead_self())).#el
c5d60 73 65 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f  se.# define SET_
c5d70 54 48 52 45 41 44 49 44 28 58 29 0a 23 20 64 65  THREADID(X).# de
c5d80 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 45 41  fine CHECK_THREA
c5d90 44 49 44 28 58 29 20 30 0a 23 65 6e 64 69 66 0a  DID(X) 0.#endif.
c5da0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
c5db0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
c5dc0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 65  ing structure se
c5dd0 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 20  rves as the key 
c5de0 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74  used.** to locat
c5df0 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 75  e a particular u
c5e00 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63  nixOpenCnt struc
c5e10 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 69  ture given its i
c5e20 6e 6f 64 65 2e 20 20 54 68 69 73 0a 2a 2a 20 69  node.  This.** i
c5e30 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  s the same as th
c5e40 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 65 78  e unixLockKey ex
c5e50 63 65 70 74 20 74 68 61 74 20 74 68 65 20 74 68  cept that the th
c5e60 72 65 61 64 20 49 44 20 69 73 20 6f 6d 69 74 74  read ID is omitt
c5e70 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e  ed..*/.struct un
c5e80 69 78 46 69 6c 65 49 64 20 7b 0a 20 20 64 65 76  ixFileId {.  dev
c5e90 5f 74 20 64 65 76 3b 20 20 20 20 20 20 20 20 20  _t dev;         
c5ea0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69           /* Devi
c5eb0 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66  ce number */.#if
c5ec0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74   OS_VXWORKS.  st
c5ed0 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
c5ee0 49 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55 6e 69  Id *pId;  /* Uni
c5ef0 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20  que file ID for 
c5f00 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73  vxworks. */.#els
c5f10 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20  e.  ino_t ino;  
c5f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5f30 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20  /* Inode number 
c5f40 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  */.#endif.};../*
c5f50 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
c5f60 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
c5f70 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65   structure serve
c5f80 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65  s as the key use
c5f90 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61  d.** to locate a
c5fa0 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78   particular unix
c5fb0 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75  LockInfo structu
c5fc0 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f  re given its ino
c5fd0 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 72  de..**.** If thr
c5fe0 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72  eads cannot over
c5ff0 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73  ride each others
c6000 20 6c 6f 63 6b 73 20 28 4c 69 6e 75 78 54 68 72   locks (LinuxThr
c6010 65 61 64 73 29 2c 20 74 68 65 6e 20 77 65 0a 2a  eads), then we.*
c6020 2a 20 73 65 74 20 74 68 65 20 75 6e 69 78 4c 6f  * set the unixLo
c6030 63 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c 64 20  ckKey.tid field 
c6040 74 6f 20 74 68 65 20 74 68 72 65 61 64 20 49 44  to the thread ID
c6050 2e 20 20 49 66 20 74 68 72 65 61 64 73 20 63 61  .  If threads ca
c6060 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20 65 61  n override.** ea
c6070 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20  ch others locks 
c6080 28 50 6f 73 69 78 20 61 6e 64 20 4e 50 54 4c 29  (Posix and NPTL)
c6090 20 74 68 65 6e 20 74 69 64 20 69 73 20 61 6c 77   then tid is alw
c60a0 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e  ays set to zero.
c60b0 0a 2a 2a 20 74 69 64 20 69 73 20 6f 6d 69 74 74  .** tid is omitt
c60c0 65 64 20 69 66 20 77 65 20 63 6f 6d 70 69 6c 65  ed if we compile
c60d0 20 77 69 74 68 6f 75 74 20 74 68 72 65 61 64 69   without threadi
c60e0 6e 67 20 73 75 70 70 6f 72 74 20 6f 72 20 6f 6e  ng support or on
c60f0 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68 65 72 20   an OS.** other 
c6100 74 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a 2f 0a 73  than linux..*/.s
c6110 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65  truct unixLockKe
c6120 79 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69  y {.  struct uni
c6130 78 46 69 6c 65 49 64 20 66 69 64 3b 20 20 2f 2a  xFileId fid;  /*
c6140 20 55 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69   Unique identifi
c6150 65 72 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20  er for the file 
c6160 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
c6170 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69  READSAFE && defi
c6180 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20  ned(__linux__). 
c6190 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20   pthread_t tid; 
c61a0 20 2f 2a 20 54 68 72 65 61 64 20 49 44 20 6f 66   /* Thread ID of
c61b0 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20 5a 65 72   lock owner. Zer
c61c0 6f 20 69 66 20 6e 6f 74 20 75 73 69 6e 67 20 4c  o if not using L
c61d0 69 6e 75 78 54 68 72 65 61 64 73 20 2a 2f 0a 23  inuxThreads */.#
c61e0 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
c61f0 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
c6200 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
c6210 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61  ucture is alloca
c6220 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65  ted for each ope
c6230 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 4f 72 2c  n.** inode.  Or,
c6240 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65 61 64 73   on LinuxThreads
c6250 2c 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 6f  , there is one o
c6260 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
c6270 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 69  es for.** each i
c6280 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62 79 20 65  node opened by e
c6290 61 63 68 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a  ach thread..**.*
c62a0 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f 64 65  * A single inode
c62b0 20 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 69 70   can have multip
c62c0 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  le file descript
c62d0 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 75 6e 69  ors, so each uni
c62e0 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75  xFile.** structu
c62f0 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  re contains a po
c6300 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74  inter to an inst
c6310 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
c6320 65 63 74 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20  ect and this.** 
c6330 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 61 20 63  object keeps a c
c6340 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62  ount of the numb
c6350 65 72 20 6f 66 20 75 6e 69 78 46 69 6c 65 20 70  er of unixFile p
c6360 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e 0a 2a  ointing to it..*
c6370 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63  /.struct unixLoc
c6380 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63 74  kInfo {.  struct
c6390 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63   unixLockKey loc
c63a0 6b 4b 65 79 3b 20 20 20 20 20 2f 2a 20 54 68 65  kKey;     /* The
c63b0 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20   lookup key */. 
c63c0 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20   int cnt;       
c63d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c63e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53 48   /* Number of SH
c63f0 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c 64 20  ARED locks held 
c6400 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b 74 79 70  */.  int locktyp
c6410 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
c6420 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53       /* One of S
c6430 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45  HARED_LOCK, RESE
c6440 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a  RVED_LOCK etc. *
c6450 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  /.  int nRef;   
c6460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6470 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
c6480 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69   pointers to thi
c6490 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  s structure */. 
c64a0 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b   struct unixLock
c64b0 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20 20  Info *pNext;    
c64c0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20   /* List of all 
c64d0 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a  unixLockInfo obj
c64e0 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  ects */.  struct
c64f0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70   unixLockInfo *p
c6500 50 72 65 76 3b 20 20 20 20 20 2f 2a 20 20 20 20  Prev;     /*    
c6510 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69 6e 6b  .... doubly link
c6520 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ed */.};../*.** 
c6530 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
c6540 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
c6550 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61  ucture is alloca
c6560 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65  ted for each ope
c6570 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 54 68 69  n.** inode.  Thi
c6580 73 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70  s structure keep
c6590 73 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 6e  s track of the n
c65a0 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 6f  umber of locks o
c65b0 6e 20 74 68 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e  n that.** inode.
c65c0 20 20 49 66 20 61 20 63 6c 6f 73 65 20 69 73 20    If a close is 
c65d0 61 74 74 65 6d 70 74 65 64 20 61 67 61 69 6e 73  attempted agains
c65e0 74 20 61 6e 20 69 6e 6f 64 65 20 74 68 61 74 20  t an inode that 
c65f0 69 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f  is holding.** lo
c6600 63 6b 73 2c 20 74 68 65 20 63 6c 6f 73 65 20 69  cks, the close i
c6610 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  s deferred until
c6620 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65 61 72   all locks clear
c6630 20 62 79 20 61 64 64 69 6e 67 20 74 68 65 0a 2a   by adding the.*
c6640 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * file descripto
c6650 72 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 20 74  r to be closed t
c6660 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 69  o the pending li
c6670 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20  st..**.** TODO: 
c6680 20 43 6f 6e 73 69 64 65 72 20 63 68 61 6e 67 69   Consider changi
c6690 6e 67 20 74 68 69 73 20 73 6f 20 74 68 61 74 20  ng this so that 
c66a0 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20  there is only a 
c66b0 73 69 6e 67 6c 65 20 66 69 6c 65 0a 2a 2a 20 64  single file.** d
c66c0 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 65 61  escriptor for ea
c66d0 63 68 20 6f 70 65 6e 20 66 69 6c 65 2c 20 65 76  ch open file, ev
c66e0 65 6e 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70  en when it is op
c66f0 65 6e 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  ened multiple ti
c6700 6d 65 73 2e 0a 2a 2a 20 54 68 65 20 63 6c 6f 73  mes..** The clos
c6710 65 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20  e() system call 
c6720 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63 63 75 72  would only occur
c6730 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 64   when the last d
c6740 61 74 61 62 61 73 65 0a 2a 2a 20 75 73 69 6e 67  atabase.** using
c6750 20 74 68 65 20 66 69 6c 65 20 63 6c 6f 73 65 73   the file closes
c6760 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78  ..*/.struct unix
c6770 4f 70 65 6e 43 6e 74 20 7b 0a 20 20 73 74 72 75  OpenCnt {.  stru
c6780 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69  ct unixFileId fi
c6790 6c 65 49 64 3b 20 20 20 2f 2a 20 54 68 65 20 6c  leId;   /* The l
c67a0 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69  ookup key */.  i
c67b0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
c67c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
c67d0 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73  mber of pointers
c67e0 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
c67f0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63  re */.  int nLoc
c6800 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
c6810 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
c6820 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63   outstanding loc
c6830 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 65 6e  ks */.  int nPen
c6840 64 69 6e 67 3b 20 20 20 20 20 20 20 20 20 20 20  ding;           
c6850 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
c6860 20 70 65 6e 64 69 6e 67 20 63 6c 6f 73 65 28 29   pending close()
c6870 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20   operations */. 
c6880 20 69 6e 74 20 2a 61 50 65 6e 64 69 6e 67 3b 20   int *aPending; 
c6890 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
c68a0 6c 6c 6f 63 65 64 20 73 70 61 63 65 20 68 6f 6c  lloced space hol
c68b0 64 69 6e 67 20 66 64 27 73 20 61 77 61 69 74 69  ding fd's awaiti
c68c0 6e 67 20 61 20 63 6c 6f 73 65 28 29 20 2a 2f 0a  ng a close() */.
c68d0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
c68e0 20 73 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20   sem_t *pSem;   
c68f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6900 20 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58    /* Named POSIX
c6910 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20   semaphore */.  
c6920 63 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41  char aSemName[MA
c6930 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 20 20  X_PATHNAME+1];  
c6940 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 74   /* Name of that
c6950 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65   semaphore */.#e
c6960 6e 64 69 66 0a 20 20 73 74 72 75 63 74 20 75 6e  ndif.  struct un
c6970 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74  ixOpenCnt *pNext
c6980 2c 20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c  , *pPrev;   /* L
c6990 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f  ist of all unixO
c69a0 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a  penCnt objects *
c69b0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74  /.};../*.** List
c69c0 73 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63  s of all unixLoc
c69d0 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70  kInfo and unixOp
c69e0 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20  enCnt objects.  
c69f0 54 68 65 73 65 20 75 73 65 64 20 74 6f 20 62 65  These used to be
c6a00 20 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e   hash.** tables.
c6a10 20 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72    But the number
c6a20 20 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72   of objects is r
c6a30 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20  arely more than 
c6a40 61 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e  a dozen and.** n
c6a50 65 76 65 72 20 65 78 63 65 65 64 73 20 61 20 66  ever exceeds a f
c6a60 65 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e  ew thousand.  An
c6a70 64 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20  d lookup is not 
c6a80 6f 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a  on a critical.**
c6a90 20 70 61 74 68 20 73 6f 20 61 20 73 69 6d 70 6c   path so a simpl
c6aa0 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69  e linked list wi
c6ab0 6c 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73  ll suffice..*/.s
c6ac0 74 61 74 69 63 20 73 74 72 75 63 74 20 75 6e 69  tatic struct uni
c6ad0 78 4c 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c  xLockInfo *lockL
c6ae0 69 73 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 20  ist = 0;.static 
c6af0 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
c6b00 6e 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30  nt *openList = 0
c6b10 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61  ;../*.** This va
c6b20 72 69 61 62 6c 65 20 72 65 6d 65 6d 62 65 72 73  riable remembers
c6b30 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
c6b40 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72  threads can over
c6b50 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73  ride each others
c6b60 0a 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a  .** locks..**.**
c6b70 20 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72      0:  No.  Thr
c6b80 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72  eads cannot over
c6b90 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73  ride each others
c6ba0 20 6c 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54   locks.  (LinuxT
c6bb0 68 72 65 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a  hreads).**    1:
c6bc0 20 20 59 65 73 2e 20 20 54 68 72 65 61 64 73 20    Yes.  Threads 
c6bd0 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63  can override eac
c6be0 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20  h others locks. 
c6bf0 20 28 50 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a   (Posix & NLPT).
c6c00 2a 2a 20 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e  **   -1:  We don
c6c10 27 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a  't know yet..**.
c6c20 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65  ** On some syste
c6c30 6d 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20 63  ms, we know at c
c6c40 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66 20 74  ompile-time if t
c6c50 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72  hreads can overr
c6c60 69 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65  ide each.** othe
c6c70 72 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68  rs locks.  On th
c6c80 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65  ose systems, the
c6c90 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f   SQLITE_THREAD_O
c6ca0 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63  VERRIDE_LOCK mac
c6cb0 72 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65  ro.** will be se
c6cc0 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  t appropriately.
c6cd0 20 20 4f 6e 20 6f 74 68 65 72 20 73 79 73 74 65    On other syste
c6ce0 6d 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20 63  ms, we have to c
c6cf0 68 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69  heck at.** runti
c6d00 6d 65 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c 61  me.  On these la
c6d10 74 74 65 72 20 73 79 73 74 65 6d 73 2c 20 53 51  tter systems, SQ
c6d20 4c 54 49 45 5f 54 48 52 45 41 44 5f 4f 56 45 52  LTIE_THREAD_OVER
c6d30 52 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20  RIDE_LOCK is.** 
c6d40 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
c6d50 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 6e   This variable n
c6d60 6f 72 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c 65  ormally has file
c6d70 20 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75   scope only.  Bu
c6d80 74 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67  t during testing
c6d90 2c 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20  , we make.** it 
c6da0 61 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74  a global so that
c6db0 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 20 63   the test code c
c6dc0 61 6e 20 63 68 61 6e 67 65 20 69 74 73 20 76 61  an change its va
c6dd0 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  lue in order to 
c6de0 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 74  verify.** that t
c6df0 68 65 20 72 69 67 68 74 20 73 74 75 66 66 20 68  he right stuff h
c6e00 61 70 70 65 6e 73 20 69 6e 20 65 69 74 68 65 72  appens in either
c6e10 20 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51   case..*/.#if SQ
c6e20 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
c6e30 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e  && defined(__lin
c6e40 75 78 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66 20  ux__).#  ifndef 
c6e50 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56  SQLITE_THREAD_OV
c6e60 45 52 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20  ERRIDE_LOCK.#   
c6e70 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54   define SQLITE_T
c6e80 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c  HREAD_OVERRIDE_L
c6e90 4f 43 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a  OCK -1.#  endif.
c6ea0 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f  #  ifdef SQLITE_
c6eb0 54 45 53 54 0a 69 6e 74 20 74 68 72 65 61 64 73  TEST.int threads
c6ec0 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
c6ed0 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45  rsLocks = SQLITE
c6ee0 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45  _THREAD_OVERRIDE
c6ef0 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73  _LOCK;.#  else.s
c6f00 74 61 74 69 63 20 69 6e 74 20 74 68 72 65 61 64  tatic int thread
c6f10 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
c6f20 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54  ersLocks = SQLIT
c6f30 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44  E_THREAD_OVERRID
c6f40 45 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66  E_LOCK;.#  endif
c6f50 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
c6f60 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f  his structure ho
c6f70 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  lds information 
c6f80 70 61 73 73 65 64 20 69 6e 74 6f 20 69 6e 64 69  passed into indi
c6f90 76 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74  vidual test.** t
c6fa0 68 72 65 61 64 73 20 62 79 20 74 68 65 20 74 65  hreads by the te
c6fb0 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42  stThreadLockingB
c6fc0 65 68 61 76 69 6f 72 28 29 20 72 6f 75 74 69 6e  ehavior() routin
c6fd0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68 72  e..*/.struct thr
c6fe0 65 61 64 54 65 73 74 44 61 74 61 20 7b 0a 20 20  eadTestData {.  
c6ff0 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20  int fd;         
c7000 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74         /* File t
c7010 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20  o be locked */. 
c7020 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f   struct flock lo
c7030 63 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c  ck;     /* The l
c7040 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ocking operation
c7050 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c 74   */.  int result
c7060 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
c7070 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f  Result of the lo
c7080 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20  cking operation 
c7090 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54  */.};..#if SQLIT
c70a0 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
c70b0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
c70c0 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  _)./*.** This fu
c70d0 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61  nction is used a
c70e0 73 20 74 68 65 20 6d 61 69 6e 20 72 6f 75 74 69  s the main routi
c70f0 6e 65 20 66 6f 72 20 61 20 74 68 72 65 61 64 20  ne for a thread 
c7100 6c 61 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74  launched by.** t
c7110 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67  estThreadLocking
c7120 42 65 68 61 76 69 6f 72 28 29 2e 20 49 74 20 74  Behavior(). It t
c7130 65 73 74 73 20 77 68 65 74 68 65 72 20 74 68 65  ests whether the
c7140 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74   shared-lock obt
c7150 61 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  ained.** by the 
c7160 6d 61 69 6e 20 74 68 72 65 61 64 20 69 6e 20 74  main thread in t
c7170 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67  estThreadLocking
c7180 42 65 68 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c  Behavior() confl
c7190 69 63 74 73 20 77 69 74 68 20 61 0a 2a 2a 20 68  icts with a.** h
c71a0 79 70 6f 74 68 65 74 69 63 61 6c 20 77 72 69 74  ypothetical writ
c71b0 65 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20  e-lock obtained 
c71c0 62 79 20 74 68 69 73 20 74 68 72 65 61 64 20 6f  by this thread o
c71d0 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e  n the same file.
c71e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74 65  .**.** The write
c71f0 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 74  -lock is not act
c7200 75 61 6c 6c 79 20 61 63 71 75 69 72 65 64 2c 20  ually acquired, 
c7210 61 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 70  as this is not p
c7220 6f 73 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74  ossible if .** t
c7230 68 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20  he file is open 
c7240 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64  in read-only mod
c7250 65 20 28 73 65 65 20 74 69 63 6b 65 74 20 23 33  e (see ticket #3
c7260 34 37 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  472)..*/ .static
c7270 20 76 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63   void *threadLoc
c7280 6b 69 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70  kingTest(void *p
c7290 41 72 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74  Arg){.  struct t
c72a0 68 72 65 61 64 54 65 73 74 44 61 74 61 20 2a 70  hreadTestData *p
c72b0 44 61 74 61 20 3d 20 28 73 74 72 75 63 74 20 74  Data = (struct t
c72c0 68 72 65 61 64 54 65 73 74 44 61 74 61 2a 29 70  hreadTestData*)p
c72d0 41 72 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65  Arg;.  pData->re
c72e0 73 75 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61  sult = fcntl(pDa
c72f0 74 61 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c  ta->fd, F_GETLK,
c7300 20 26 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a   &pData->lock);.
c7310 20 20 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d    return pArg;.}
c7320 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
c7330 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
c7340 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
c7350 5f 29 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49  _) */...#if SQLI
c7360 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
c7370 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
c7380 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70  __)./*.** This p
c7390 72 6f 63 65 64 75 72 65 20 61 74 74 65 6d 70 74  rocedure attempt
c73a0 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  s to determine w
c73b0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
c73c0 72 65 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65  reads.** can ove
c73d0 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72  rride each other
c73e0 73 20 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 74  s locks then set
c73f0 73 20 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 64  s the .** thread
c7400 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
c7410 65 72 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c  ersLocks variabl
c7420 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  e appropriately.
c7430 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
c7440 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e  testThreadLockin
c7450 67 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66 64  gBehavior(int fd
c7460 5f 6f 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 64  _orig){.  int fd
c7470 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74  ;.  int rc;.  st
c7480 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44  ruct threadTestD
c7490 61 74 61 20 64 3b 0a 20 20 73 74 72 75 63 74 20  ata d;.  struct 
c74a0 66 6c 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65  flock l;.  pthre
c74b0 61 64 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20  ad_t t;..  fd = 
c74c0 64 75 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20  dup(fd_orig);.  
c74d0 69 66 28 20 66 64 3c 30 20 29 20 72 65 74 75 72  if( fd<0 ) retur
c74e0 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20  n;.  memset(&l, 
c74f0 30 2c 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20  0, sizeof(l));. 
c7500 20 6c 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44   l.l_type = F_RD
c7510 4c 43 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d  LCK;.  l.l_len =
c7520 20 31 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20   1;.  l.l_start 
c7530 3d 20 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63  = 0;.  l.l_whenc
c7540 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
c7550 72 63 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72  rc = fcntl(fd_or
c7560 69 67 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29  ig, F_SETLK, &l)
c7570 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 20  ;.  if( rc!=0 ) 
c7580 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74  return;.  memset
c7590 28 26 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64  (&d, 0, sizeof(d
c75a0 29 29 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b  ));.  d.fd = fd;
c75b0 0a 20 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20  .  d.lock = l;. 
c75c0 20 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d   d.lock.l_type =
c75d0 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 70 74 68 72   F_WRLCK;.  pthr
c75e0 65 61 64 5f 63 72 65 61 74 65 28 26 74 2c 20 30  ead_create(&t, 0
c75f0 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54  , threadLockingT
c7600 65 73 74 2c 20 26 64 29 3b 0a 20 20 70 74 68 72  est, &d);.  pthr
c7610 65 61 64 5f 6a 6f 69 6e 28 74 2c 20 30 29 3b 0a  ead_join(t, 0);.
c7620 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69    close(fd);.  i
c7630 66 28 20 64 2e 72 65 73 75 6c 74 21 3d 30 20 29  f( d.result!=0 )
c7640 20 72 65 74 75 72 6e 3b 0a 20 20 74 68 72 65 61   return;.  threa
c7650 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
c7660 68 65 72 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c  hersLocks = (d.l
c7670 6f 63 6b 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e  ock.l_type==F_UN
c7680 4c 43 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  LCK);.}.#endif /
c7690 2a 20 53 51 4c 49 54 45 5f 54 48 45 52 41 44 53  * SQLITE_THERADS
c76a0 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  AFE && defined(_
c76b0 5f 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a  _linux__) */../*
c76c0 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e  .** Release a un
c76d0 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63  ixLockInfo struc
c76e0 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20  ture previously 
c76f0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e  allocated by fin
c7700 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a  dLockInfo()..*/.
c7710 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65  static void rele
c7720 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 73 74 72 75  aseLockInfo(stru
c7730 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
c7740 2a 70 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 70  *pLock){.  if( p
c7750 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63  Lock ){.    pLoc
c7760 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69  k->nRef--;.    i
c7770 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d  f( pLock->nRef==
c7780 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
c7790 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29 7b 0a 20  Lock->pPrev ){. 
c77a0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
c77b0 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  Lock->pPrev->pNe
c77c0 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20  xt==pLock );.   
c77d0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 72 65       pLock->pPre
c77e0 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c 6f 63 6b  v->pNext = pLock
c77f0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
c7800 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
c7810 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d  sert( lockList==
c7820 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20  pLock );.       
c7830 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63   lockList = pLoc
c7840 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  k->pNext;.      
c7850 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63  }.      if( pLoc
c7860 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  k->pNext ){.    
c7870 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63      assert( pLoc
c7880 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d  k->pNext->pPrev=
c7890 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20  =pLock );.      
c78a0 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e    pLock->pNext->
c78b0 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70  pPrev = pLock->p
c78c0 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Prev;.      }.  
c78d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
c78e0 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 7d 0a 20  (pLock);.    }. 
c78f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65   }.}../*.** Rele
c7900 61 73 65 20 61 20 75 6e 69 78 4f 70 65 6e 43 6e  ase a unixOpenCn
c7910 74 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76  t structure prev
c7920 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
c7930 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f   by findLockInfo
c7940 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
c7950 69 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e  id releaseOpenCn
c7960 74 28 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65  t(struct unixOpe
c7970 6e 43 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20  nCnt *pOpen){.  
c7980 69 66 28 20 70 4f 70 65 6e 20 29 7b 0a 20 20 20  if( pOpen ){.   
c7990 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a   pOpen->nRef--;.
c79a0 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e      if( pOpen->n
c79b0 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Ref==0 ){.      
c79c0 69 66 28 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76  if( pOpen->pPrev
c79d0 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
c79e0 72 74 28 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76  rt( pOpen->pPrev
c79f0 2d 3e 70 4e 65 78 74 3d 3d 70 4f 70 65 6e 20 29  ->pNext==pOpen )
c7a00 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d  ;.        pOpen-
c7a10 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
c7a20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20  pOpen->pNext;.  
c7a30 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
c7a40 20 20 20 61 73 73 65 72 74 28 20 6f 70 65 6e 4c     assert( openL
c7a50 69 73 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20  ist==pOpen );.  
c7a60 20 20 20 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d        openList =
c7a70 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20   pOpen->pNext;. 
c7a80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
c7a90 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 29 7b   pOpen->pNext ){
c7aa0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
c7ab0 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70   pOpen->pNext->p
c7ac0 50 72 65 76 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20  Prev==pOpen );. 
c7ad0 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e         pOpen->pN
c7ae0 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70  ext->pPrev = pOp
c7af0 65 6e 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20  en->pPrev;.     
c7b00 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
c7b10 5f 66 72 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65  _free(pOpen->aPe
c7b20 6e 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 73 71  nding);.      sq
c7b30 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e  lite3_free(pOpen
c7b40 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
c7b50 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 66 69  /*.** Given a fi
c7b60 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2c 20 6c  le descriptor, l
c7b70 6f 63 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e  ocate unixLockIn
c7b80 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43  fo and unixOpenC
c7b90 6e 74 20 73 74 72 75 63 74 75 72 65 73 20 74 68  nt structures th
c7ba0 61 74 0a 2a 2a 20 64 65 73 63 72 69 62 65 73 20  at.** describes 
c7bb0 74 68 61 74 20 66 69 6c 65 20 64 65 73 63 72 69  that file descri
c7bc0 70 74 6f 72 2e 20 20 43 72 65 61 74 65 20 6e 65  ptor.  Create ne
c7bd0 77 20 6f 6e 65 73 20 69 66 20 6e 65 63 65 73 73  w ones if necess
c7be0 61 72 79 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74  ary.  The.** ret
c7bf0 75 72 6e 20 76 61 6c 75 65 73 20 6d 69 67 68 74  urn values might
c7c00 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65   be uninitialize
c7c10 64 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  d if an error oc
c7c20 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  curs..**.** Retu
c7c30 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  rn an appropriat
c7c40 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f  e error code..*/
c7c50 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64  .static int find
c7c60 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78  LockInfo(.  unix
c7c70 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20  File *pFile,    
c7c80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e             /* Un
c7c90 69 78 20 66 69 6c 65 20 77 69 74 68 20 66 69 6c  ix file with fil
c7ca0 65 20 64 65 73 63 20 75 73 65 64 20 69 6e 20 74  e desc used in t
c7cb0 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75  he key */.  stru
c7cc0 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
c7cd0 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65  **ppLock,  /* Re
c7ce0 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63  turn the unixLoc
c7cf0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  kInfo structure 
c7d00 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  here */.  struct
c7d10 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70   unixOpenCnt **p
c7d20 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65 74 75  pOpen    /* Retu
c7d30 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43  rn the unixOpenC
c7d40 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 65 72  nt structure her
c7d50 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
c7d60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c7d70 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 73 74           /* Syst
c7d80 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e 20 63  em call return c
c7d90 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b  ode */.  int fd;
c7da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7db0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
c7dc0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66  ile descriptor f
c7dd0 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74  or pFile */.  st
c7de0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79  ruct unixLockKey
c7df0 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20   lockKey;    /* 
c7e00 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74  Lookup key for t
c7e10 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  he unixLockInfo 
c7e20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73  structure */.  s
c7e30 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64  truct unixFileId
c7e40 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a   fileId;      /*
c7e50 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20   Lookup key for 
c7e60 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  the unixOpenCnt 
c7e70 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74 72 75  struct */.  stru
c7e80 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 3b  ct stat statbuf;
c7e90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
c7ea0 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66  w-level file inf
c7eb0 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74  ormation */.  st
c7ec0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66  ruct unixLockInf
c7ed0 6f 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20  o *pLock;    /* 
c7ee0 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4c 6f  Candidate unixLo
c7ef0 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f  ckInfo object */
c7f00 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70  .  struct unixOp
c7f10 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20  enCnt *pOpen;   
c7f20 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75    /* Candidate u
c7f30 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63  nixOpenCnt objec
c7f40 74 20 2a 2f 0a 0a 20 20 2f 2a 20 47 65 74 20 6c  t */..  /* Get l
c7f50 6f 77 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61  ow-level informa
c7f60 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66  tion about the f
c7f70 69 6c 65 20 74 68 61 74 20 77 65 20 63 61 6e 20  ile that we can 
c7f80 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65  used to.  ** cre
c7f90 61 74 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d  ate a unique nam
c7fa0 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a  e for the file..
c7fb0 20 20 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c    */.  fd = pFil
c7fc0 65 2d 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74  e->h;.  rc = fst
c7fd0 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29  at(fd, &statbuf)
c7fe0 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b  ;.  if( rc!=0 ){
c7ff0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
c8000 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23  Errno = errno;.#
c8010 69 66 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a  ifdef EOVERFLOW.
c8020 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c      if( pFile->l
c8030 61 73 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46  astErrno==EOVERF
c8040 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  LOW ) return SQL
c8050 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69  ITE_NOLFS;.#endi
c8060 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  f.    return SQL
c8070 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a  ITE_IOERR;.  }..
c8080 23 69 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f  #ifdef __APPLE__
c8090 0a 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e  .  /* On OS X on
c80a0 20 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79   an msdos filesy
c80b0 73 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20  stem, the inode 
c80c0 6e 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74  number is report
c80d0 65 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63  ed.  ** incorrec
c80e0 74 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a  tly for zero-siz
c80f0 65 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69  e files.  See ti
c8100 63 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20  cket #3260.  To 
c8110 77 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64  work.  ** around
c8120 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77   this problem (w
c8130 65 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20  e consider it a 
c8140 62 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74  bug in OS X, not
c8150 20 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65   SQLite).  ** we
c8160 20 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65   always increase
c8170 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74   the file size t
c8180 6f 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61  o 1 by writing a
c8190 20 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a   single byte.  *
c81a0 2a 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73  * prior to acces
c81b0 73 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e  sing the inode n
c81c0 75 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20  umber.  The one 
c81d0 62 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a  byte written is.
c81e0 20 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53    ** an ASCII 'S
c81f0 27 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63  ' character whic
c8200 68 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74  h also happens t
c8210 6f 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62  o be the first b
c8220 79 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  yte.  ** in the 
c8230 68 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20  header of every 
c8240 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e  SQLite database.
c8250 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69    In this way, i
c8260 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20  f there.  ** is 
c8270 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  a race condition
c8280 20 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68   such that anoth
c8290 65 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c  er thread has al
c82a0 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a  ready populated.
c82b0 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70    ** the first p
c82c0 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  age of the datab
c82d0 61 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69  ase, no damage i
c82e0 73 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69  s done..  */.  i
c82f0 66 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69  f( statbuf.st_si
c8300 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  ze==0 ){.    rc 
c8310 3d 20 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c  = write(fd, "S",
c8320 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   1);.    if( rc!
c8330 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =1 ){.      retu
c8340 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
c8350 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
c8360 66 73 74 61 74 28 66 64 2c 20 26 73 74 61 74 62  fstat(fd, &statb
c8370 75 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  uf);.    if( rc!
c8380 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  =0 ){.      pFil
c8390 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
c83a0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75  rrno;.      retu
c83b0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
c83c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
c83d0 66 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63  f..  memset(&loc
c83e0 6b 4b 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28  kKey, 0, sizeof(
c83f0 6c 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63  lockKey));.  loc
c8400 6b 4b 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73  kKey.fid.dev = s
c8410 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23  tatbuf.st_dev;.#
c8420 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
c8430 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20  lockKey.fid.pId 
c8440 3d 20 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65  = pFile->pId;.#e
c8450 6c 73 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69  lse.  lockKey.fi
c8460 64 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e  d.ino = statbuf.
c8470 73 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23  st_ino;.#endif.#
c8480 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
c8490 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28  SAFE && defined(
c84a0 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28  __linux__).  if(
c84b0 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65   threadsOverride
c84c0 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c  EachOthersLocks<
c84d0 30 20 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72  0 ){.    testThr
c84e0 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69  eadLockingBehavi
c84f0 6f 72 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f  or(fd);.  }.  lo
c8500 63 6b 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65  ckKey.tid = thre
c8510 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f  adsOverrideEachO
c8520 74 68 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a  thersLocks ? 0 :
c8530 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b   pthread_self();
c8540 0a 23 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64  .#endif.  fileId
c8550 20 3d 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a   = lockKey.fid;.
c8560 20 20 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20    if( ppLock!=0 
c8570 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c  ){.    pLock = l
c8580 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69  ockList;.    whi
c8590 6c 65 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d  le( pLock && mem
c85a0 63 6d 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70  cmp(&lockKey, &p
c85b0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73  Lock->lockKey, s
c85c0 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20  izeof(lockKey)) 
c85d0 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d  ){.      pLock =
c85e0 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20   pLock->pNext;. 
c85f0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f     }.    if( pLo
c8600 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  ck==0 ){.      p
c8610 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Lock = sqlite3_m
c8620 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70  alloc( sizeof(*p
c8630 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69  Lock) );.      i
c8640 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  f( pLock==0 ){. 
c8650 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
c8660 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
c8670 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64    goto exit_find
c8680 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20  lockinfo;.      
c8690 7d 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c  }.      pLock->l
c86a0 6f 63 6b 4b 65 79 20 3d 20 6c 6f 63 6b 4b 65 79  ockKey = lockKey
c86b0 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e  ;.      pLock->n
c86c0 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  Ref = 1;.      p
c86d0 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20  Lock->cnt = 0;. 
c86e0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b       pLock->lock
c86f0 74 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  type = 0;.      
c8700 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c  pLock->pNext = l
c8710 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 20 20 70  ockList;.      p
c8720 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b  Lock->pPrev = 0;
c8730 0a 20 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c  .      if( lockL
c8740 69 73 74 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e  ist ) lockList->
c8750 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20  pPrev = pLock;. 
c8760 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20       lockList = 
c8770 70 4c 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65  pLock;.    }else
c8780 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e  {.      pLock->n
c8790 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  Ref++;.    }.   
c87a0 20 2a 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b   *ppLock = pLock
c87b0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 70 4f 70  ;.  }.  if( ppOp
c87c0 65 6e 21 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70  en!=0 ){.    pOp
c87d0 65 6e 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20  en = openList;. 
c87e0 20 20 20 77 68 69 6c 65 28 20 70 4f 70 65 6e 20     while( pOpen 
c87f0 26 26 20 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49  && memcmp(&fileI
c8800 64 2c 20 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49  d, &pOpen->fileI
c8810 64 2c 20 73 69 7a 65 6f 66 28 66 69 6c 65 49 64  d, sizeof(fileId
c8820 29 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65  )) ){.      pOpe
c8830 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74  n = pOpen->pNext
c8840 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
c8850 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  pOpen==0 ){.    
c8860 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65    pOpen = sqlite
c8870 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  3_malloc( sizeof
c8880 28 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20  (*pOpen) );.    
c8890 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29    if( pOpen==0 )
c88a0 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  {.        releas
c88b0 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29  eLockInfo(pLock)
c88c0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ;.        rc = S
c88d0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
c88e0 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66       goto exit_f
c88f0 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20  indlockinfo;.   
c8900 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 65 6e     }.      pOpen
c8910 2d 3e 66 69 6c 65 49 64 20 3d 20 66 69 6c 65 49  ->fileId = fileI
c8920 64 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  d;.      pOpen->
c8930 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  nRef = 1;.      
c8940 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30  pOpen->nLock = 0
c8950 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e  ;.      pOpen->n
c8960 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20  Pending = 0;.   
c8970 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69     pOpen->aPendi
c8980 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f  ng = 0;.      pO
c8990 70 65 6e 2d 3e 70 4e 65 78 74 20 3d 20 6f 70 65  pen->pNext = ope
c89a0 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4f 70  nList;.      pOp
c89b0 65 6e 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20  en->pPrev = 0;. 
c89c0 20 20 20 20 20 69 66 28 20 6f 70 65 6e 4c 69 73       if( openLis
c89d0 74 20 29 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50  t ) openList->pP
c89e0 72 65 76 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20  rev = pOpen;.   
c89f0 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f     openList = pO
c8a00 70 65 6e 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f  pen;.#if OS_VXWO
c8a10 52 4b 53 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  RKS.      pOpen-
c8a20 3e 70 53 65 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20  >pSem = NULL;.  
c8a30 20 20 20 20 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e      pOpen->aSemN
c8a40 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 23  ame[0] = '\0';.#
c8a50 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b  endif.    }else{
c8a60 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52  .      pOpen->nR
c8a70 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ef++;.    }.    
c8a80 2a 70 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b  *ppOpen = pOpen;
c8a90 0a 20 20 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c  .  }..exit_findl
c8aa0 6f 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72  ockinfo:.  retur
c8ab0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
c8ac0 66 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74  f we are current
c8ad0 6c 79 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e  ly in a differen
c8ae0 74 20 74 68 72 65 61 64 20 74 68 61 6e 20 74 68  t thread than th
c8af0 65 20 74 68 72 65 61 64 20 74 68 61 74 20 74 68  e thread that th
c8b00 65 0a 2a 2a 20 75 6e 69 78 46 69 6c 65 20 61 72  e.** unixFile ar
c8b10 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74  gument belongs t
c8b20 6f 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72  o, then transfer
c8b30 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68   ownership of th
c8b40 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76  e unixFile.** ov
c8b50 65 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e  er to the curren
c8b60 74 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  t thread..**.** 
c8b70 41 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e  A unixFile is on
c8b80 6c 79 20 6f 77 6e 65 64 20 62 79 20 61 20 74 68  ly owned by a th
c8b90 72 65 61 64 20 6f 6e 20 73 79 73 74 65 6d 73 20  read on systems 
c8ba0 74 68 61 74 20 75 73 65 20 4c 69 6e 75 78 54 68  that use LinuxTh
c8bb0 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e  reads..**.** Own
c8bc0 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20  ership transfer 
c8bd0 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20  is only allowed 
c8be0 69 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20  if the unixFile 
c8bf0 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c  is currently unl
c8c00 6f 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65  ocked..** If the
c8c10 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6c 6f 63   unixFile is loc
c8c20 6b 65 64 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72  ked and an owner
c8c30 73 68 69 70 20 69 73 20 77 72 6f 6e 67 2c 20 74  ship is wrong, t
c8c40 68 65 6e 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  hen return.** SQ
c8c50 4c 49 54 45 5f 4d 49 53 55 53 45 2e 20 20 53 51  LITE_MISUSE.  SQ
c8c60 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
c8c70 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e  ned if everythin
c8c80 67 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20  g works..*/.#if 
c8c90 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
c8ca0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c  E && defined(__l
c8cb0 69 6e 75 78 5f 5f 29 0a 73 74 61 74 69 63 20 69  inux__).static i
c8cc0 6e 74 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72  nt transferOwner
c8cd0 73 68 69 70 28 75 6e 69 78 46 69 6c 65 20 2a 70  ship(unixFile *p
c8ce0 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  File){.  int rc;
c8cf0 0a 20 20 70 74 68 72 65 61 64 5f 74 20 68 53 65  .  pthread_t hSe
c8d00 6c 66 3b 0a 20 20 69 66 28 20 74 68 72 65 61 64  lf;.  if( thread
c8d10 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
c8d20 65 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20  ersLocks ){.    
c8d30 2f 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61  /* Ownership tra
c8d40 6e 73 66 65 72 73 20 6e 6f 74 20 6e 65 65 64 65  nsfers not neede
c8d50 64 20 6f 6e 20 74 68 69 73 20 73 79 73 74 65 6d  d on this system
c8d60 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53   */.    return S
c8d70 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
c8d80 68 53 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f  hSelf = pthread_
c8d90 73 65 6c 66 28 29 3b 0a 20 20 69 66 28 20 70 74  self();.  if( pt
c8da0 68 72 65 61 64 5f 65 71 75 61 6c 28 70 46 69 6c  hread_equal(pFil
c8db0 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 20 29  e->tid, hSelf) )
c8dc0 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20  {.    /* We are 
c8dd0 73 74 69 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d  still in the sam
c8de0 65 20 74 68 72 65 61 64 20 2a 2f 0a 20 20 20 20  e thread */.    
c8df0 4f 53 54 52 41 43 45 31 28 22 4e 6f 2d 74 72 61  OSTRACE1("No-tra
c8e00 6e 73 66 65 72 2c 20 73 61 6d 65 20 74 68 72 65  nsfer, same thre
c8e10 61 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 75  ad\n");.    retu
c8e20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
c8e30 7d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  }.  if( pFile->l
c8e40 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
c8e50 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 63 61   ){.    /* We ca
c8e60 6e 6e 6f 74 20 63 68 61 6e 67 65 20 6f 77 6e 65  nnot change owne
c8e70 72 73 68 69 70 20 77 68 69 6c 65 20 77 65 20 61  rship while we a
c8e80 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63  re holding a loc
c8e90 6b 21 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  k! */.    return
c8ea0 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
c8eb0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22    }.  OSTRACE4("
c8ec0 54 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68  Transfer ownersh
c8ed0 69 70 20 6f 66 20 25 64 20 66 72 6f 6d 20 25 64  ip of %d from %d
c8ee0 20 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20   to %d\n",.     
c8ef0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c         pFile->h,
c8f00 20 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65   pFile->tid, hSe
c8f10 6c 66 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69  lf);.  pFile->ti
c8f20 64 20 3d 20 68 53 65 6c 66 3b 0a 20 20 69 66 20  d = hSelf;.  if 
c8f30 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d  (pFile->pLock !=
c8f40 20 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c   NULL) {.    rel
c8f50 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69  easeLockInfo(pFi
c8f60 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20  le->pLock);.    
c8f70 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66  rc = findLockInf
c8f80 6f 28 70 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d  o(pFile, &pFile-
c8f90 3e 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20  >pLock, 0);.    
c8fa0 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20  OSTRACE5("LOCK  
c8fb0 20 20 25 64 20 69 73 20 6e 6f 77 20 25 73 28 25    %d is now %s(%
c8fc0 73 2c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  s,%d)\n", pFile-
c8fd0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c  >h,.           l
c8fe0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c  ocktypeName(pFil
c8ff0 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20  e->locktype),.  
c9000 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70           locktyp
c9010 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f  eName(pFile->pLo
c9020 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70  ck->locktype), p
c9030 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74  File->pLock->cnt
c9040 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
c9050 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
c9060 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c9070 4b 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20  K;.  }.}.#else  
c9080 2f 2a 20 69 66 20 6e 6f 74 20 53 51 4c 49 54 45  /* if not SQLITE
c9090 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 20  _THREADSAFE */. 
c90a0 20 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68   /* On single-th
c90b0 72 65 61 64 65 64 20 62 75 69 6c 64 73 2c 20 6f  readed builds, o
c90c0 77 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65  wnership transfe
c90d0 72 20 69 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a  r is a no-op */.
c90e0 23 20 64 65 66 69 6e 65 20 74 72 61 6e 73 66 65  # define transfe
c90f0 72 4f 77 6e 65 72 73 68 69 70 28 58 29 20 53 51  rOwnership(X) SQ
c9100 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f  LITE_OK.#endif /
c9110 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  * SQLITE_THREADS
c9120 41 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  AFE */.../*.** T
c9130 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
c9140 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ks if there is a
c9150 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68   RESERVED lock h
c9160 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69  eld on the speci
c9170 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20  fied.** file by 
c9180 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65  this or any othe
c9190 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75  r process. If su
c91a0 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c  ch a lock is hel
c91b0 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a  d, set *pResOut.
c91c0 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  ** to a non-zero
c91d0 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65   value otherwise
c91e0 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74   *pResOut is set
c91f0 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72   to zero.  The r
c9200 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69  eturn value.** i
c9210 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  s set to SQLITE_
c9220 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f  OK unless an I/O
c9230 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
c9240 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69  ring lock checki
c9250 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
c9260 74 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72  t unixCheckReser
c9270 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
c9280 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
c9290 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72  ResOut){.  int r
c92a0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
c92b0 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20   int reserved = 
c92c0 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  0;.  unixFile *p
c92d0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
c92e0 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74  *)id;..  Simulat
c92f0 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
c9300 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48   SQLITE_IOERR_CH
c9310 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b  ECKRESERVEDLOCK;
c9320 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
c9330 46 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e  File );.  unixEn
c9340 74 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42  terMutex(); /* B
c9350 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c  ecause pFile->pL
c9360 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63  ock is shared ac
c9370 72 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f 0a  ross threads */.
c9380 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61  .  /* Check if a
c9390 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20   thread in this 
c93a0 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75  process holds su
c93b0 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69  ch a lock */.  i
c93c0 66 28 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d  f( pFile->pLock-
c93d0 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44  >locktype>SHARED
c93e0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73  _LOCK ){.    res
c93f0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a  erved = 1;.  }..
c9400 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73    /* Otherwise s
c9410 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72  ee if some other
c9420 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69   process holds i
c9430 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  t..  */.#ifndef 
c9440 5f 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20  __DJGPP__.  if( 
c9450 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20  !reserved ){.   
c9460 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f   struct flock lo
c9470 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77  ck;.    lock.l_w
c9480 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
c9490 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61  ;.    lock.l_sta
c94a0 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  rt = RESERVED_BY
c94b0 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c  TE;.    lock.l_l
c94c0 65 6e 20 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b  en = 1;.    lock
c94d0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43  .l_type = F_WRLC
c94e0 4b 3b 0a 20 20 20 20 69 66 20 28 2d 31 20 3d 3d  K;.    if (-1 ==
c94f0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
c9500 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_GETLK, &lock)
c9510 29 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45  ) {.      int tE
c9520 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
c9530 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
c9540 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
c9550 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
c9560 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
c9570 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20  ERVEDLOCK);.    
c9580 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
c9590 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
c95a0 20 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b   } else if( lock
c95b0 2e 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b  .l_type!=F_UNLCK
c95c0 20 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72 76   ){.      reserv
c95d0 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ed = 1;.    }.  
c95e0 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e  }.#endif.  .  un
c95f0 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
c9600 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54    OSTRACE4("TEST
c9610 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25   WR-LOCK %d %d %
c9620 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
c9630 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a  rc, reserved);..
c9640 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73    *pResOut = res
c9650 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20  erved;.  return 
c9660 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  rc;.}../*.** Loc
c9670 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20  k the file with 
c9680 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  the lock specifi
c9690 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
c96a0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a  locktype - one.*
c96b0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
c96c0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31  ng:.**.**     (1
c96d0 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a  ) SHARED_LOCK.**
c96e0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45       (2) RESERVE
c96f0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33  D_LOCK.**     (3
c9700 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a  ) PENDING_LOCK.*
c9710 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53  *     (4) EXCLUS
c9720 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53  IVE_LOCK.**.** S
c9730 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65  ometimes when re
c9740 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63  questing one loc
c9750 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f  k state, additio
c9760 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a  nal lock states.
c9770 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
c9780 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
c9790 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
c97a0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
c97b0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73  e later.** trans
c97c0 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
c97d0 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
c97e0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
c97f0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
c9800 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20  .** still short 
c9810 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68  of its goal.  Th
c9820 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
c9830 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f  t shows the allo
c9840 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  wed.** transitio
c9850 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
c9860 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
c9870 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
c9880 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48    UNLOCKED -> SH
c9890 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  ARED.**    SHARE
c98a0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a  D -> RESERVED.**
c98b0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50      SHARED -> (P
c98c0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
c98d0 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52  SIVE.**    RESER
c98e0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
c98f0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
c9900 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
c9910 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54  XCLUSIVE.**.** T
c9920 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
c9930 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61   only increase a
c9940 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20   lock.  Use the 
c9950 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28  sqlite3OsUnlock(
c9960 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20  ).** routine to 
c9970 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20  lower a locking 
c9980 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  level..*/.static
c9990 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71   int unixLock(sq
c99a0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
c99b0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20  int locktype){. 
c99c0 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
c99d0 67 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  g describes the 
c99e0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
c99f0 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c 6f  f the various lo
c9a00 63 6b 73 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63  cks and.  ** loc
c9a10 6b 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e  k transitions in
c9a20 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 50 4f   terms of the PO
c9a30 53 49 58 20 61 64 76 69 73 6f 72 79 20 73 68 61  SIX advisory sha
c9a40 72 65 64 20 61 6e 64 20 65 78 63 6c 75 73 69 76  red and exclusiv
c9a50 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d  e.  ** lock prim
c9a60 69 74 69 76 65 73 20 28 63 61 6c 6c 65 64 20 72  itives (called r
c9a70 65 61 64 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72  ead-locks and wr
c9a80 69 74 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c  ite-locks below,
c9a90 20 74 6f 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63   to avoid.  ** c
c9aa0 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 53 51  onfusion with SQ
c9ab0 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29  Lite lock names)
c9ac0 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 73  . The algorithms
c9ad0 20 61 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64   are complicated
c9ae0 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69  .  ** slightly i
c9af0 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63 6f  n order to be co
c9b00 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 77 69  mpatible with wi
c9b10 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 20 73 69  ndows systems si
c9b20 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a  multaneously.  *
c9b30 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20  * accessing the 
c9b40 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69  same database fi
c9b50 6c 65 2c 20 69 6e 20 63 61 73 65 20 74 68 61 74  le, in case that
c9b60 20 69 73 20 65 76 65 72 20 72 65 71 75 69 72 65   is ever require
c9b70 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d  d..  **.  ** Sym
c9b80 62 6f 6c 73 20 64 65 66 69 6e 65 64 20 69 6e 20  bols defined in 
c9b90 6f 73 2e 68 20 69 6e 64 65 6e 74 69 66 79 20 74  os.h indentify t
c9ba0 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65  he 'pending byte
c9bb0 27 20 61 6e 64 20 74 68 65 20 27 72 65 73 65 72  ' and the 'reser
c9bc0 76 65 64 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20  ved.  ** byte', 
c9bd0 65 61 63 68 20 73 69 6e 67 6c 65 20 62 79 74 65  each single byte
c9be0 73 20 61 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20  s at well known 
c9bf0 6f 66 66 73 65 74 73 2c 20 61 6e 64 20 74 68 65  offsets, and the
c9c00 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20   'shared byte.  
c9c10 2a 2a 20 72 61 6e 67 65 27 2c 20 61 20 72 61 6e  ** range', a ran
c9c20 67 65 20 6f 66 20 35 31 30 20 62 79 74 65 73 20  ge of 510 bytes 
c9c30 61 74 20 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20  at a well known 
c9c40 6f 66 66 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a  offset..  **.  *
c9c50 2a 20 54 6f 20 6f 62 74 61 69 6e 20 61 20 53 48  * To obtain a SH
c9c60 41 52 45 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61  ARED lock, a rea
c9c70 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e  d-lock is obtain
c9c80 65 64 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69  ed on the 'pendi
c9c90 6e 67 0a 20 20 2a 2a 20 62 79 74 65 27 2e 20 20  ng.  ** byte'.  
c9ca0 49 66 20 74 68 69 73 20 69 73 20 73 75 63 63 65  If this is succe
c9cb0 73 73 66 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20  ssful, a random 
c9cc0 62 79 74 65 20 66 72 6f 6d 20 74 68 65 20 27 73  byte from the 's
c9cd0 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20  hared byte.  ** 
c9ce0 72 61 6e 67 65 27 20 69 73 20 72 65 61 64 2d 6c  range' is read-l
c9cf0 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 20 6c 6f  ocked and the lo
c9d00 63 6b 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69  ck on the 'pendi
c9d10 6e 67 20 62 79 74 65 27 20 72 65 6c 65 61 73 65  ng byte' release
c9d20 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70  d..  **.  ** A p
c9d30 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20  rocess may only 
c9d40 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45  obtain a RESERVE
c9d50 44 20 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20  D lock after it 
c9d60 68 61 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63  has a SHARED loc
c9d70 6b 2e 0a 20 20 2a 2a 20 41 20 52 45 53 45 52 56  k..  ** A RESERV
c9d80 45 44 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65  ED lock is imple
c9d90 6d 65 6e 74 65 64 20 62 79 20 67 72 61 62 62 69  mented by grabbi
c9da0 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  ng a write-lock 
c9db0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 27 72 65 73  on the.  ** 'res
c9dc0 65 72 76 65 64 20 62 79 74 65 27 2e 20 0a 20 20  erved byte'. .  
c9dd0 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73  **.  ** A proces
c9de0 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69  s may only obtai
c9df0 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  n a PENDING lock
c9e00 20 61 66 74 65 72 20 69 74 20 68 61 73 20 6f 62   after it has ob
c9e10 74 61 69 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48  tained a.  ** SH
c9e20 41 52 45 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e  ARED lock. A PEN
c9e30 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70  DING lock is imp
c9e40 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61  lemented by obta
c9e50 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f  ining a write-lo
c9e60 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27  ck.  ** on the '
c9e70 70 65 6e 64 69 6e 67 20 62 79 74 65 27 2e 20 54  pending byte'. T
c9e80 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
c9e90 20 6e 6f 20 6e 65 77 20 53 48 41 52 45 44 20 6c   no new SHARED l
c9ea0 6f 63 6b 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a  ocks can be.  **
c9eb0 20 6f 62 74 61 69 6e 65 64 2c 20 62 75 74 20 65   obtained, but e
c9ec0 78 69 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c  xisting SHARED l
c9ed0 6f 63 6b 73 20 61 72 65 20 61 6c 6c 6f 77 65 64  ocks are allowed
c9ee0 20 74 6f 20 70 65 72 73 69 73 74 2e 20 41 20 70   to persist. A p
c9ef0 72 6f 63 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73  rocess.  ** does
c9f00 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6f 62 74   not have to obt
c9f10 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c  ain a RESERVED l
c9f20 6f 63 6b 20 6f 6e 20 74 68 65 20 77 61 79 20 74  ock on the way t
c9f30 6f 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  o a PENDING lock
c9f40 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 6f 70  ..  ** This prop
c9f50 65 72 74 79 20 69 73 20 75 73 65 64 20 62 79 20  erty is used by 
c9f60 74 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f  the algorithm fo
c9f70 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61  r rolling back a
c9f80 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20   journal file.  
c9f90 2a 2a 20 61 66 74 65 72 20 61 20 63 72 61 73 68  ** after a crash
c9fa0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45  ..  **.  ** An E
c9fb0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f  XCLUSIVE lock, o
c9fc0 62 74 61 69 6e 65 64 20 61 66 74 65 72 20 61 20  btained after a 
c9fd0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20  PENDING lock is 
c9fe0 68 65 6c 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d  held, is.  ** im
c9ff0 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74  plemented by obt
ca000 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c  aining a write-l
ca010 6f 63 6b 20 6f 6e 20 74 68 65 20 65 6e 74 69 72  ock on the entir
ca020 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20  e 'shared byte. 
ca030 20 2a 2a 20 72 61 6e 67 65 27 2e 20 53 69 6e 63   ** range'. Sinc
ca040 65 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b  e all other lock
ca050 73 20 72 65 71 75 69 72 65 20 61 20 72 65 61 64  s require a read
ca060 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20  -lock on one of 
ca070 74 68 65 20 62 79 74 65 73 0a 20 20 2a 2a 20 77  the bytes.  ** w
ca080 69 74 68 69 6e 20 74 68 69 73 20 72 61 6e 67 65  ithin this range
ca090 2c 20 74 68 69 73 20 65 6e 73 75 72 65 73 20 74  , this ensures t
ca0a0 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63  hat no other loc
ca0b0 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 74  ks are held on t
ca0c0 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  he.  ** database
ca0d0 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  . .  **.  ** The
ca0e0 20 72 65 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65   reason a single
ca0f0 20 62 79 74 65 20 63 61 6e 6e 6f 74 20 62 65 20   byte cannot be 
ca100 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20  used instead of 
ca110 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74 65  the 'shared byte
ca120 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20  .  ** range' is 
ca130 74 68 61 74 20 73 6f 6d 65 20 76 65 72 73 69 6f  that some versio
ca140 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f  ns of windows do
ca150 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61   not support rea
ca160 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a  d-locks. By.  **
ca170 20 6c 6f 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f   locking a rando
ca180 6d 20 62 79 74 65 20 66 72 6f 6d 20 61 20 72 61  m byte from a ra
ca190 6e 67 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20  nge, concurrent 
ca1a0 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79  SHARED locks may
ca1b0 20 65 78 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e   exist.  ** even
ca1c0 20 69 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   if the locking 
ca1d0 70 72 69 6d 69 74 69 76 65 20 75 73 65 64 20 69  primitive used i
ca1e0 73 20 61 6c 77 61 79 73 20 61 20 77 72 69 74 65  s always a write
ca1f0 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e  -lock..  */.  in
ca200 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
ca210 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
ca220 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
ca230 29 69 64 3b 0a 20 20 73 74 72 75 63 74 20 75 6e  )id;.  struct un
ca240 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63  ixLockInfo *pLoc
ca250 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  k = pFile->pLock
ca260 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b  ;.  struct flock
ca270 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 73 3b 0a   lock;.  int s;.
ca280 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
ca290 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22   );.  OSTRACE7("
ca2a0 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61  LOCK    %d %s wa
ca2b0 73 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d  s %s(%s,%d) pid=
ca2c0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
ca2d0 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e  .      locktypeN
ca2e0 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c  ame(locktype), l
ca2f0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c  ocktypeName(pFil
ca300 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20  e->locktype),.  
ca310 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65      locktypeName
ca320 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65  (pLock->locktype
ca330 29 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20  ), pLock->cnt , 
ca340 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a  getpid());..  /*
ca350 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72   If there is alr
ca360 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74  eady a lock of t
ca370 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65  his type or more
ca380 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20   restrictive on 
ca390 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c  the.  ** unixFil
ca3a0 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44  e, do nothing. D
ca3b0 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64  on't use the end
ca3c0 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68  _lock: exit path
ca3d0 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e  , as.  ** unixEn
ca3e0 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27  terMutex() hasn'
ca3f0 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65  t been called ye
ca400 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46  t..  */.  if( pF
ca410 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c  ile->locktype>=l
ca420 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f  ocktype ){.    O
ca430 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20  STRACE3("LOCK   
ca440 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61   %d %s ok (alrea
ca450 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69  dy held)\n", pFi
ca460 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
ca470 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
ca480 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20  locktype));.    
ca490 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
ca4a0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
ca4b0 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e   sure the lockin
ca4c0 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f  g sequence is co
ca4d0 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73  rrect.  */.  ass
ca4e0 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ert( pFile->lock
ca4f0 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c  type!=NO_LOCK ||
ca500 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
ca510 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  D_LOCK );.  asse
ca520 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45  rt( locktype!=PE
ca530 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20  NDING_LOCK );.  
ca540 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
ca550 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  !=RESERVED_LOCK 
ca560 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  || pFile->lockty
ca570 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
ca580 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75  );..  /* This mu
ca590 74 65 78 20 69 73 20 6e 65 65 64 65 64 20 62 65  tex is needed be
ca5a0 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f  cause pFile->pLo
ca5b0 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72  ck is shared acr
ca5c0 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f  oss threads.  */
ca5d0 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65  .  unixEnterMute
ca5e0 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  x();..  /* Make 
ca5f0 73 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74  sure the current
ca600 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65   thread owns the
ca610 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72   pFile..  */.  r
ca620 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65  c = transferOwne
ca630 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20  rship(pFile);.  
ca640 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
ca650 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61  K ){.    unixLea
ca660 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72  veMutex();.    r
ca670 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
ca680 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70  pLock = pFile->p
ca690 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73  Lock;..  /* If s
ca6a0 6f 6d 65 20 74 68 72 65 61 64 20 75 73 69 6e 67  ome thread using
ca6b0 20 74 68 69 73 20 50 49 44 20 68 61 73 20 61 20   this PID has a 
ca6c0 6c 6f 63 6b 20 76 69 61 20 61 20 64 69 66 66 65  lock via a diffe
ca6d0 72 65 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20  rent unixFile*. 
ca6e0 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20   ** handle that 
ca6f0 70 72 65 63 6c 75 64 65 73 20 74 68 65 20 72 65  precludes the re
ca700 71 75 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65  quested lock, re
ca710 74 75 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a  turn BUSY..  */.
ca720 20 20 69 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f    if( (pFile->lo
ca730 63 6b 74 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c  cktype!=pLock->l
ca740 6f 63 6b 74 79 70 65 20 26 26 20 0a 20 20 20 20  ocktype && .    
ca750 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f        (pLock->lo
ca760 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f  cktype>=PENDING_
ca770 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65  LOCK || locktype
ca780 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20  >SHARED_LOCK)). 
ca790 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
ca7a0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f  ITE_BUSY;.    go
ca7b0 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d  to end_lock;.  }
ca7c0 0a 0a 20 20 2f 2a 20 49 66 20 61 20 53 48 41 52  ..  /* If a SHAR
ca7d0 45 44 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65  ED lock is reque
ca7e0 73 74 65 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74  sted, and some t
ca7f0 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 69 73  hread using this
ca800 20 50 49 44 20 61 6c 72 65 61 64 79 0a 20 20 2a   PID already.  *
ca810 2a 20 68 61 73 20 61 20 53 48 41 52 45 44 20 6f  * has a SHARED o
ca820 72 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c  r RESERVED lock,
ca830 20 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20   then increment 
ca840 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 73  reference counts
ca850 20 61 6e 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e   and.  ** return
ca860 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f   SQLITE_OK..  */
ca870 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
ca880 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20  =SHARED_LOCK && 
ca890 0a 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c  .      (pLock->l
ca8a0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
ca8b0 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c  LOCK || pLock->l
ca8c0 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45  ocktype==RESERVE
ca8d0 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61  D_LOCK) ){.    a
ca8e0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d  ssert( locktype=
ca8f0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
ca900 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
ca910 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29  e->locktype==0 )
ca920 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c  ;.    assert( pL
ca930 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20  ock->cnt>0 );.  
ca940 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
ca950 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b  e = SHARED_LOCK;
ca960 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b  .    pLock->cnt+
ca970 2b 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f  +;.    pFile->pO
ca980 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20  pen->nLock++;.  
ca990 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b    goto end_lock;
ca9a0 0a 20 20 7d 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c  .  }..  lock.l_l
ca9b0 65 6e 20 3d 20 31 4c 3b 0a 0a 20 20 6c 6f 63 6b  en = 1L;..  lock
ca9c0 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b  .l_whence = SEEK
ca9d0 5f 53 45 54 3b 0a 0a 20 20 2f 2a 20 41 20 50 45  _SET;..  /* A PE
ca9e0 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65  NDING lock is ne
ca9f0 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 71 75  eded before acqu
caa00 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c  iring a SHARED l
caa10 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20  ock and before. 
caa20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e   ** acquiring an
caa30 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
caa40 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 44    For the SHARED
caa50 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49   lock, the PENDI
caa60 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20  NG will.  ** be 
caa70 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20  released..  */. 
caa80 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
caa90 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20  HARED_LOCK .    
caaa0 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d    || (locktype==
caab0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26  EXCLUSIVE_LOCK &
caac0 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  & pFile->locktyp
caad0 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a  e<PENDING_LOCK).
caae0 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f    ){.    lock.l_
caaf0 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65  type = (locktype
cab00 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f  ==SHARED_LOCK?F_
cab10 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a  RDLCK:F_WRLCK);.
cab20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
cab30 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b   = PENDING_BYTE;
cab40 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 70  .    s = fcntl(p
cab50 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b  File->h, F_SETLK
cab60 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66  , &lock);.    if
cab70 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20  ( s==(-1) ){.   
cab80 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
cab90 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20  errno;.      rc 
caba0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
cabb0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
cabc0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
cabd0 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66  _LOCK);.      if
cabe0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
cabf0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  rc) ){.        p
cac00 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
cac10 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
cac20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64  }.      goto end
cac30 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  _lock;.    }.  }
cac40 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72  ...  /* If contr
cac50 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73 20  ol gets to this 
cac60 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75  point, then actu
cac70 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e  ally go ahead an
cac80 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72  d make.  ** oper
cac90 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c  ating system cal
caca0 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65 63 69  ls for the speci
cacb0 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a  fied lock..  */.
cacc0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
cacd0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
cace0 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
cacf0 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  0;.    assert( p
cad00 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a  Lock->cnt==0 );.
cad10 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63      assert( pLoc
cad20 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29  k->locktype==0 )
cad30 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65  ;..    /* Now ge
cad40 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20  t the read-lock 
cad50 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74  */.    lock.l_st
cad60 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52  art = SHARED_FIR
cad70 53 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c  ST;.    lock.l_l
cad80 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  en = SHARED_SIZE
cad90 3b 0a 20 20 20 20 69 66 28 20 28 73 20 3d 20 66  ;.    if( (s = f
cada0 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46  cntl(pFile->h, F
cadb0 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 3d  _SETLK, &lock))=
cadc0 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 74  =(-1) ){.      t
cadd0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
cade0 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70     }.    /* Drop
cadf0 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50   the temporary P
cae00 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20  ENDING lock */. 
cae10 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
cae20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
cae30 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
cae40 20 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f   1L;.    lock.l_
cae50 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a  type = F_UNLCK;.
cae60 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46      if( fcntl(pF
cae70 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c  ile->h, F_SETLK,
cae80 20 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20   &lock)!=0 ){.  
cae90 20 20 20 20 69 66 28 20 73 20 21 3d 20 2d 31 20      if( s != -1 
caea0 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
caeb0 69 73 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 20  is could happen 
caec0 77 69 74 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d  with a network m
caed0 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  ount */.        
caee0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20  tErrno = errno; 
caef0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
caf00 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
caf10 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
caf20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
caf30 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69  OCK); .        i
caf40 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
caf50 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (rc) ){.        
caf60 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
caf70 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
caf80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67       }.        g
caf90 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20  oto end_lock;.  
cafa0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
cafb0 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20  if( s==(-1) ){. 
cafc0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
cafd0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
cafe0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
caff0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a  TE_IOERR_LOCK);.
cb000 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43        if( IS_LOC
cb010 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20  K_ERROR(rc) ){. 
cb020 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
cb030 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
cb040 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
cb050 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  lse{.      pFile
cb060 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41  ->locktype = SHA
cb070 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20  RED_LOCK;.      
cb080 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c  pFile->pOpen->nL
cb090 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f  ock++;.      pLo
cb0a0 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20  ck->cnt = 1;.   
cb0b0 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c   }.  }else if( l
cb0c0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
cb0d0 56 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b  VE_LOCK && pLock
cb0e0 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f  ->cnt>1 ){.    /
cb0f0 2a 20 57 65 20 61 72 65 20 74 72 79 69 6e 67 20  * We are trying 
cb100 66 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65  for an exclusive
cb110 20 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 68 65   lock but anothe
cb120 72 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  r thread in this
cb130 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f  .    ** same pro
cb140 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68 6f  cess is still ho
cb150 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c  lding a shared l
cb160 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d  ock. */.    rc =
cb170 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
cb180 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
cb190 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f  e request was fo
cb1a0 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  r a RESERVED or 
cb1b0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
cb1c0 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73   It is.    ** as
cb1d0 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65  sumed that there
cb1e0 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20   is a SHARED or 
cb1f0 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20  greater lock on 
cb200 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  the file.    ** 
cb210 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a  already..    */.
cb220 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70      assert( 0!=p
cb230 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
cb240 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  ;.    lock.l_typ
cb250 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20  e = F_WRLCK;.   
cb260 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70   switch( locktyp
cb270 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  e ){.      case 
cb280 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20  RESERVED_LOCK:. 
cb290 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74         lock.l_st
cb2a0 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42  art = RESERVED_B
cb2b0 59 54 45 3b 0a 20 20 20 20 20 20 20 20 62 72 65  YTE;.        bre
cb2c0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 45  ak;.      case E
cb2d0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20  XCLUSIVE_LOCK:. 
cb2e0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74         lock.l_st
cb2f0 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52  art = SHARED_FIR
cb300 53 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b  ST;.        lock
cb310 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f  .l_len = SHARED_
cb320 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 62 72  SIZE;.        br
cb330 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75  eak;.      defau
cb340 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 73 73 65  lt:.        asse
cb350 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rt(0);.    }.   
cb360 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65   s = fcntl(pFile
cb370 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  ->h, F_SETLK, &l
cb380 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 3d  ock);.    if( s=
cb390 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69  =(-1) ){.      i
cb3a0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
cb3b0 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  o;.      rc = sq
cb3c0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
cb3d0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
cb3e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
cb3f0 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53  K);.      if( IS
cb400 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
cb410 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
cb420 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
cb430 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rrno;.      }.  
cb440 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69 66 6e    }.  }.  ..#ifn
cb450 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20  def NDEBUG.  /* 
cb460 53 65 74 20 75 70 20 74 68 65 20 74 72 61 6e 73  Set up the trans
cb470 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 63  action-counter c
cb480 68 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 20 66  hange checking f
cb490 6c 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74  lags when.  ** t
cb4a0 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f  ransitioning fro
cb4b0 6d 20 61 20 53 48 41 52 45 44 20 74 6f 20 61 20  m a SHARED to a 
cb4c0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 20 20  RESERVED lock.  
cb4d0 54 68 65 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20  The change.  ** 
cb4e0 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 52  from SHARED to R
cb4f0 45 53 45 52 56 45 44 20 6d 61 72 6b 73 20 74 68  ESERVED marks th
cb500 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61  e beginning of a
cb510 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 69   normal.  ** wri
cb520 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6e 6f  te operation (no
cb530 74 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20  t a hot journal 
cb540 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a  rollback)..  */.
cb550 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
cb560 5f 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c 65 2d  _OK.   && pFile-
cb570 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45  >locktype<=SHARE
cb580 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c 6f 63  D_LOCK.   && loc
cb590 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f  ktype==RESERVED_
cb5a0 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46  LOCK.  ){.    pF
cb5b0 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68  ile->transCntrCh
cb5c0 6e 67 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c  ng = 0;.    pFil
cb5d0 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b  e->dbUpdate = 0;
cb5e0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f  .    pFile->inNo
cb5f0 72 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20  rmalWrite = 1;. 
cb600 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66   }.#endif...  if
cb610 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
cb620 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  ){.    pFile->lo
cb630 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
cb640 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f  e;.    pLock->lo
cb650 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
cb660 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c  e;.  }else if( l
cb670 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
cb680 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70  VE_LOCK ){.    p
cb690 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
cb6a0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20   PENDING_LOCK;. 
cb6b0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
cb6c0 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43  pe = PENDING_LOC
cb6d0 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b  K;.  }..end_lock
cb6e0 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  :.  unixLeaveMut
cb6f0 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34  ex();.  OSTRACE4
cb700 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
cb710 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %s\n", pFile->h,
cb720 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f   locktypeName(lo
cb730 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20  cktype), .      
cb740 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20  rc==SQLITE_OK ? 
cb750 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29  "ok" : "failed")
cb760 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
cb770 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68  ../*.** Lower th
cb780 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
cb790 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  on file descript
cb7a0 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b  or pFile to lock
cb7b0 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
cb7c0 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
cb7d0 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
cb7e0 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
cb7f0 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
cb800 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
cb810 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
cb820 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
cb830 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
cb840 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
cb850 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
cb860 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73  is a no-op..*/.s
cb870 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e  tatic int unixUn
cb880 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
cb890 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
cb8a0 79 70 65 29 7b 0a 20 20 73 74 72 75 63 74 20 75  ype){.  struct u
cb8b0 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f  nixLockInfo *pLo
cb8c0 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f  ck;.  struct flo
cb8d0 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72  ck lock;.  int r
cb8e0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
cb8f0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
cb900 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
cb910 3b 0a 20 20 69 6e 74 20 68 3b 0a 0a 20 20 61 73  ;.  int h;..  as
cb920 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
cb930 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c 4f 43   OSTRACE7("UNLOC
cb940 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 28  K  %d %d was %d(
cb950 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22  %d,%d) pid=%d\n"
cb960 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
cb970 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46 69 6c  type,.      pFil
cb980 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69  e->locktype, pFi
cb990 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74  le->pLock->lockt
cb9a0 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63  ype, pFile->pLoc
cb9b0 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64 28 29  k->cnt, getpid()
cb9c0 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6c 6f  );..  assert( lo
cb9d0 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
cb9e0 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 46 69  OCK );.  if( pFi
cb9f0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f  le->locktype<=lo
cba00 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65  cktype ){.    re
cba10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
cba20 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43 4b 5f    }.  if( CHECK_
cba30 54 48 52 45 41 44 49 44 28 70 46 69 6c 65 29 20  THREADID(pFile) 
cba40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
cba50 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
cba60 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65  .  unixEnterMute
cba70 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65  x();.  h = pFile
cba80 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70  ->h;.  pLock = p
cba90 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61  File->pLock;.  a
cbaa0 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e  ssert( pLock->cn
cbab0 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46  t!=0 );.  if( pF
cbac0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48  ile->locktype>SH
cbad0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
cbae0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
cbaf0 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d  locktype==pFile-
cbb00 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20  >locktype );.   
cbb10 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
cbb20 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 53  Benign(1);.    S
cbb30 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
cbb40 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 69 6d  h=(-1) ).    Sim
cbb50 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
cbb60 67 6e 28 30 29 3b 0a 0a 23 69 66 6e 64 65 66 20  gn(0);..#ifndef 
cbb70 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 57 68  NDEBUG.    /* Wh
cbb80 65 6e 20 72 65 64 75 63 69 6e 67 20 61 20 6c 6f  en reducing a lo
cbb90 63 6b 20 73 75 63 68 20 74 68 61 74 20 6f 74 68  ck such that oth
cbba0 65 72 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e  er processes can
cbbb0 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 72 65   start.    ** re
cbbc0 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ading the databa
cbbd0 73 65 20 66 69 6c 65 20 61 67 61 69 6e 2c 20 6d  se file again, m
cbbe0 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
cbbf0 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63  e.    ** transac
cbc00 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61 73  tion counter was
cbc10 20 75 70 64 61 74 65 64 20 69 66 20 61 6e 79 20   updated if any 
cbc20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
cbc30 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65  base.    ** file
cbc40 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20 74 68   changed.  If th
cbc50 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
cbc60 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 75 70 64  unter is not upd
cbc70 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 6f 74 68  ated,.    ** oth
cbc80 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  er connections t
cbc90 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20  o the same file 
cbca0 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 6c 69 7a  might not realiz
cbcb0 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 68  e that.    ** th
cbcc0 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67  e file has chang
cbcd0 65 64 20 61 6e 64 20 68 65 6e 63 65 20 6d 69 67  ed and hence mig
cbce0 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 6f 20 66  ht not know to f
cbcf0 6c 75 73 68 20 74 68 65 69 72 0a 20 20 20 20 2a  lush their.    *
cbd00 2a 20 63 61 63 68 65 2e 20 20 54 68 65 20 75 73  * cache.  The us
cbd10 65 20 6f 66 20 61 20 73 74 61 6c 65 20 63 61 63  e of a stale cac
cbd20 68 65 20 63 61 6e 20 6c 65 61 64 20 74 6f 20 64  he can lead to d
cbd30 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
cbd40 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  on..    */.    a
cbd50 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 69 6e  ssert( pFile->in
cbd60 4e 6f 72 6d 61 6c 57 72 69 74 65 3d 3d 30 0a 20  NormalWrite==0. 
cbd70 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c 65          || pFile
cbd80 2d 3e 64 62 55 70 64 61 74 65 3d 3d 30 0a 20 20  ->dbUpdate==0.  
cbd90 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d         || pFile-
cbda0 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d  >transCntrChng==
cbdb0 31 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  1 );.    pFile->
cbdc0 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20  inNormalWrite = 
cbdd0 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20  0;.#endif...    
cbde0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
cbdf0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
cbe00 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d     lock.l_type =
cbe10 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 20 20 20 20   F_RDLCK;.      
cbe20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  lock.l_whence = 
cbe30 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20  SEEK_SET;.      
cbe40 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53  lock.l_start = S
cbe50 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20  HARED_FIRST;.   
cbe60 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20     lock.l_len = 
cbe70 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20  SHARED_SIZE;.   
cbe80 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20     if( fcntl(h, 
cbe90 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3d  F_SETLK, &lock)=
cbea0 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  =(-1) ){.       
cbeb0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72   int tErrno = er
cbec0 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20  rno;.        rc 
cbed0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
cbee0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
cbef0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
cbf00 5f 52 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  _RDLOCK);.      
cbf10 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
cbf20 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
cbf30 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
cbf40 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
cbf50 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
cbf60 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63    goto end_unloc
cbf70 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
cbf80 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65  .    lock.l_type
cbf90 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20   = F_UNLCK;.    
cbfa0 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  lock.l_whence = 
cbfb0 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f  SEEK_SET;.    lo
cbfc0 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e  ck.l_start = PEN
cbfd0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c  DING_BYTE;.    l
cbfe0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20  ock.l_len = 2L; 
cbff0 20 61 73 73 65 72 74 28 20 50 45 4e 44 49 4e 47   assert( PENDING
cc000 5f 42 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 45  _BYTE+1==RESERVE
cc010 44 5f 42 59 54 45 20 29 3b 0a 20 20 20 20 69 66  D_BYTE );.    if
cc020 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54  ( fcntl(h, F_SET
cc030 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29  LK, &lock)!=(-1)
cc040 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d   ){.      pLock-
cc050 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  >locktype = SHAR
cc060 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c  ED_LOCK;.    }el
cc070 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45  se{.      int tE
cc080 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
cc090 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
cc0a0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
cc0b0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
cc0c0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
cc0d0 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  .      if( IS_LO
cc0e0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
cc0f0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
cc100 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
cc110 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  o;.      }.     
cc120 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b   goto end_unlock
cc130 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
cc140 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c  ( locktype==NO_L
cc150 4f 43 4b 20 29 7b 0a 20 20 20 20 73 74 72 75 63  OCK ){.    struc
cc160 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70  t unixOpenCnt *p
cc170 4f 70 65 6e 3b 0a 20 20 20 20 69 6e 74 20 72 63  Open;.    int rc
cc180 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  2 = SQLITE_OK;..
cc190 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74      /* Decrement
cc1a0 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b   the shared lock
cc1b0 20 63 6f 75 6e 74 65 72 2e 20 20 52 65 6c 65 61   counter.  Relea
cc1c0 73 65 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e  se the lock usin
cc1d0 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f 53 20 63  g an.    ** OS c
cc1e0 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 61 6c  all only when al
cc1f0 6c 20 74 68 72 65 61 64 73 20 69 6e 20 74 68 69  l threads in thi
cc200 73 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 68  s same process h
cc210 61 76 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20  ave released.   
cc220 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20   ** the lock..  
cc230 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e    */.    pLock->
cc240 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  cnt--;.    if( p
cc250 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a  Lock->cnt==0 ){.
cc260 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70        lock.l_typ
cc270 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20  e = F_UNLCK;.   
cc280 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65     lock.l_whence
cc290 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20   = SEEK_SET;.   
cc2a0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
cc2b0 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30  = lock.l_len = 0
cc2c0 4c 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74  L;.      Simulat
cc2d0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31  eIOErrorBenign(1
cc2e0 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74  );.      Simulat
cc2f0 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 29  eIOError( h=(-1)
cc300 20 29 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74   ).      Simulat
cc310 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30  eIOErrorBenign(0
cc320 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 63 6e  );.      if( fcn
cc330 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  tl(h, F_SETLK, &
cc340 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20  lock)!=(-1) ){. 
cc350 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f         pLock->lo
cc360 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b  cktype = NO_LOCK
cc370 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
cc380 20 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e         int tErrn
cc390 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
cc3a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
cc3b0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
cc3c0 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
cc3d0 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a  _IOERR_UNLOCK);.
cc3e0 20 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c          if( IS_L
cc3f0 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
cc400 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65  .          pFile
cc410 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
cc420 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rrno;.        }.
cc430 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c          pLock->l
cc440 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43  ocktype = NO_LOC
cc450 4b 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  K;.        pFile
cc460 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f  ->locktype = NO_
cc470 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  LOCK;.      }.  
cc480 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72    }..    /* Decr
cc490 65 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 20  ement the count 
cc4a0 6f 66 20 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74  of locks against
cc4b0 20 74 68 69 73 20 73 61 6d 65 20 66 69 6c 65 2e   this same file.
cc4c0 20 20 57 68 65 6e 20 74 68 65 0a 20 20 20 20 2a    When the.    *
cc4d0 2a 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  * count reaches 
cc4e0 7a 65 72 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20  zero, close any 
cc4f0 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 72  other file descr
cc500 69 70 74 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f  iptors whose clo
cc510 73 65 0a 20 20 20 20 2a 2a 20 77 61 73 20 64 65  se.    ** was de
cc520 66 65 72 72 65 64 20 62 65 63 61 75 73 65 20 6f  ferred because o
cc530 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f  f outstanding lo
cc540 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  cks..    */.    
cc550 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70  pOpen = pFile->p
cc560 4f 70 65 6e 3b 0a 20 20 20 20 70 4f 70 65 6e 2d  Open;.    pOpen-
cc570 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 73  >nLock--;.    as
cc580 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f  sert( pOpen->nLo
cc590 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  ck>=0 );.    if(
cc5a0 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30   pOpen->nLock==0
cc5b0 20 26 26 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64   && pOpen->nPend
cc5c0 69 6e 67 3e 30 20 29 7b 0a 20 20 20 20 20 20 69  ing>0 ){.      i
cc5d0 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28  nt i;.      for(
cc5e0 69 3d 30 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50  i=0; i<pOpen->nP
cc5f0 65 6e 64 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20  ending; i++){.  
cc600 20 20 20 20 20 20 2f 2a 20 63 6c 6f 73 65 20 70        /* close p
cc610 65 6e 64 69 6e 67 20 66 64 73 2c 20 62 75 74 20  ending fds, but 
cc620 69 66 20 63 6c 6f 73 69 6e 67 20 66 61 69 6c 73  if closing fails
cc630 20 64 6f 6e 27 74 20 66 72 65 65 20 74 68 65 20   don't free the 
cc640 61 72 72 61 79 0a 20 20 20 20 20 20 20 20 2a 2a  array.        **
cc650 20 61 73 73 69 67 6e 20 2d 31 20 74 6f 20 74 68   assign -1 to th
cc660 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63  e successfully c
cc670 6c 6f 73 65 64 20 64 65 73 63 72 69 70 74 6f 72  losed descriptor
cc680 73 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68 65  s and record the
cc690 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 72 72 6f  .        ** erro
cc6a0 72 2e 20 20 54 68 65 20 6e 65 78 74 20 61 74 74  r.  The next att
cc6b0 65 6d 70 74 20 74 6f 20 75 6e 6c 6f 63 6b 20 77  empt to unlock w
cc6c0 69 6c 6c 20 74 72 79 20 61 67 61 69 6e 2e 20 2a  ill try again. *
cc6d0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f  /.        if( pO
cc6e0 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d  pen->aPending[i]
cc6f0 20 3c 20 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b   < 0 ) continue;
cc700 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f  .        if( clo
cc710 73 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69  se(pOpen->aPendi
cc720 6e 67 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20  ng[i]) ){.      
cc730 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
cc740 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
cc750 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 53 51          rc2 = SQ
cc760 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45  LITE_IOERR_CLOSE
cc770 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
cc780 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e  .          pOpen
cc790 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20  ->aPending[i] = 
cc7a0 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  -1;.        }.  
cc7b0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
cc7c0 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  rc2==SQLITE_OK )
cc7d0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
cc7e0 33 5f 66 72 65 65 28 70 4f 70 65 6e 2d 3e 61 50  3_free(pOpen->aP
cc7f0 65 6e 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 20  ending);.       
cc800 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67   pOpen->nPending
cc810 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 4f   = 0;.        pO
cc820 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20  pen->aPending = 
cc830 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
cc840 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
cc850 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
cc860 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a  rc = rc2;.    }.
cc870 20 20 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b    }...end_unlock
cc880 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  :.  unixLeaveMut
cc890 65 78 28 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ex();.  if( rc==
cc8a0 53 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c  SQLITE_OK ) pFil
cc8b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
cc8c0 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e  cktype;.  return
cc8d0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
cc8e0 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66  is function perf
cc8f0 6f 72 6d 73 20 74 68 65 20 70 61 72 74 73 20 6f  orms the parts o
cc900 66 20 74 68 65 20 22 63 6c 6f 73 65 20 66 69 6c  f the "close fil
cc910 65 22 20 6f 70 65 72 61 74 69 6f 6e 20 0a 2a 2a  e" operation .**
cc920 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c   common to all l
cc930 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 2e 20  ocking schemes. 
cc940 49 74 20 63 6c 6f 73 65 73 20 74 68 65 20 64 69  It closes the di
cc950 72 65 63 74 6f 72 79 20 61 6e 64 20 66 69 6c 65  rectory and file
cc960 0a 2a 2a 20 68 61 6e 64 6c 65 73 2c 20 69 66 20  .** handles, if 
cc970 74 68 65 79 20 61 72 65 20 76 61 6c 69 64 2c 20  they are valid, 
cc980 61 6e 64 20 73 65 74 73 20 61 6c 6c 20 66 69 65  and sets all fie
cc990 6c 64 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46  lds of the unixF
cc9a0 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ile.** structure
cc9b0 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20   to 0..**.** It 
cc9c0 69 73 20 2a 6e 6f 74 2a 20 6e 65 63 65 73 73 61  is *not* necessa
cc9d0 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d  ry to hold the m
cc9e0 75 74 65 78 20 77 68 65 6e 20 74 68 69 73 20 72  utex when this r
cc9f0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
cca00 2c 0a 2a 2a 20 65 76 65 6e 20 6f 6e 20 56 78 57  ,.** even on VxW
cca10 6f 72 6b 73 2e 20 20 41 20 6d 75 74 65 78 20 77  orks.  A mutex w
cca20 69 6c 6c 20 62 65 20 61 63 71 75 69 72 65 64 20  ill be acquired 
cca30 6f 6e 20 56 78 57 6f 72 6b 73 20 62 79 20 74 68  on VxWorks by th
cca40 65 0a 2a 2a 20 76 78 77 6f 72 6b 73 52 65 6c 65  e.** vxworksRele
cca50 61 73 65 46 69 6c 65 49 64 28 29 20 72 6f 75 74  aseFileId() rout
cca60 69 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ine..*/.static i
cca70 6e 74 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65  nt closeUnixFile
cca80 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
cca90 64 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  d){.  unixFile *
ccaa0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
ccab0 65 2a 29 69 64 3b 0a 20 20 69 66 28 20 70 46 69  e*)id;.  if( pFi
ccac0 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 46  le ){.    if( pF
ccad0 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b  ile->dirfd>=0 ){
ccae0 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d  .      int err =
ccaf0 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69   close(pFile->di
ccb00 72 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  rfd);.      if( 
ccb10 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70  err ){.        p
ccb20 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
ccb30 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
ccb40 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
ccb50 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a  OERR_DIR_CLOSE;.
ccb60 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
ccb70 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 69 72 66       pFile->dirf
ccb80 64 3d 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  d=-1;.      }.  
ccb90 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 69 6c    }.    if( pFil
ccba0 65 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20  e->h>=0 ){.     
ccbb0 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 65   int err = close
ccbc0 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20  (pFile->h);.    
ccbd0 20 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20    if( err ){.   
ccbe0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
ccbf0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
ccc00 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
ccc10 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45  LITE_IOERR_CLOSE
ccc20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
ccc30 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
ccc40 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 49     if( pFile->pI
ccc50 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  d ){.      if( p
ccc60 46 69 6c 65 2d 3e 69 73 44 65 6c 65 74 65 20 29  File->isDelete )
ccc70 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c 69 6e 6b  {.        unlink
ccc80 28 70 46 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61  (pFile->pId->zCa
ccc90 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20  nonicalName);.  
ccca0 20 20 20 20 7d 0a 20 20 20 20 20 20 76 78 77 6f      }.      vxwo
cccb0 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64  rksReleaseFileId
cccc0 28 70 46 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 20  (pFile->pId);.  
cccd0 20 20 20 20 70 46 69 6c 65 2d 3e 70 49 64 20 3d      pFile->pId =
ccce0 20 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66   0;.    }.#endif
cccf0 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 43  .    OSTRACE2("C
ccd00 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20  LOSE   %-3d\n", 
ccd10 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f  pFile->h);.    O
ccd20 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a  penCounter(-1);.
ccd30 20 20 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65      memset(pFile
ccd40 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78  , 0, sizeof(unix
ccd50 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65  File));.  }.  re
ccd60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
ccd70 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
ccd80 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
ccd90 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 73   int unixClose(s
ccda0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
ccdb0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
ccdc0 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64  ITE_OK;.  if( id
ccdd0 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65   ){.    unixFile
ccde0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
ccdf0 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e  ile *)id;.    un
cce00 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f  ixUnlock(id, NO_
cce10 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45  LOCK);.    unixE
cce20 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
cce30 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65   if( pFile->pOpe
cce40 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65  n && pFile->pOpe
cce50 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  n->nLock ){.    
cce60 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
cce70 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f  e outstanding lo
cce80 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75  cks, do not actu
cce90 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 66  ally close the f
ccea0 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20 2a  ile just.      *
cceb0 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74 68  * yet because th
ccec0 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74  at would clear t
cced0 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73  hose locks.  Ins
ccee0 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 69  tead, add the fi
ccef0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63  le.      ** desc
ccf00 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d  riptor to pOpen-
ccf10 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 77  >aPending.  It w
ccf20 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63  ill be automatic
ccf30 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 6e  ally closed when
ccf40 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 61  .      ** the la
ccf50 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72  st lock is clear
ccf60 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
ccf70 20 20 20 69 6e 74 20 2a 61 4e 65 77 3b 0a 20 20     int *aNew;.  
ccf80 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f      struct unixO
ccf90 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20  penCnt *pOpen = 
ccfa0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20  pFile->pOpen;.  
ccfb0 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74      aNew = sqlit
ccfc0 65 33 5f 72 65 61 6c 6c 6f 63 28 70 4f 70 65 6e  e3_realloc(pOpen
ccfd0 2d 3e 61 50 65 6e 64 69 6e 67 2c 20 28 70 4f 70  ->aPending, (pOp
ccfe0 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 31 29 2a  en->nPending+1)*
ccff0 73 69 7a 65 6f 66 28 69 6e 74 29 20 29 3b 0a 20  sizeof(int) );. 
cd000 20 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30       if( aNew==0
cd010 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49   ){.        /* I
cd020 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  f a malloc fails
cd030 2c 20 6a 75 73 74 20 6c 65 61 6b 20 74 68 65 20  , just leak the 
cd040 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
cd050 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  */.      }else{.
cd060 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61          pOpen->a
cd070 50 65 6e 64 69 6e 67 20 3d 20 61 4e 65 77 3b 0a  Pending = aNew;.
cd080 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61          pOpen->a
cd090 50 65 6e 64 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e  Pending[pOpen->n
cd0a0 50 65 6e 64 69 6e 67 5d 20 3d 20 70 46 69 6c 65  Pending] = pFile
cd0b0 2d 3e 68 3b 0a 20 20 20 20 20 20 20 20 70 4f 70  ->h;.        pOp
cd0c0 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a  en->nPending++;.
cd0d0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68          pFile->h
cd0e0 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20   = -1;.      }. 
cd0f0 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65     }.    release
cd100 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e  LockInfo(pFile->
cd110 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65  pLock);.    rele
cd120 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65  aseOpenCnt(pFile
cd130 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 72 63  ->pOpen);.    rc
cd140 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65   = closeUnixFile
cd150 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65  (id);.    unixLe
cd160 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
cd170 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
cd180 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
cd190 45 6e 64 20 6f 66 20 74 68 65 20 70 6f 73 69 78  End of the posix
cd1a0 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 20 69   advisory lock i
cd1b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a  mplementation **
cd1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
cd1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
cd220 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
cd230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd270 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
cd280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
cd290 4e 6f 2d 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a  No-op Locking **
cd2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd2c0 0a 2a 2a 0a 2a 2a 20 4f 66 20 74 68 65 20 76 61  .**.** Of the va
cd2d0 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 69 6d  rious locking im
cd2e0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 76  plementations av
cd2f0 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 69 73  ailable, this is
cd300 20 62 79 20 66 61 72 20 74 68 65 0a 2a 2a 20 73   by far the.** s
cd310 69 6d 70 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 6e  implest:  lockin
cd320 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 4e  g is ignored.  N
cd330 6f 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64  o attempt is mad
cd340 65 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 64 61  e to lock the da
cd350 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 66  tabase.** file f
cd360 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72  or reading or wr
cd370 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  iting..**.** Thi
cd380 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69  s locking mode i
cd390 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  s appropriate fo
cd3a0 72 20 75 73 65 20 6f 6e 20 72 65 61 64 2d 6f 6e  r use on read-on
cd3b0 6c 79 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20  ly databases.** 
cd3c0 28 65 78 3a 20 64 61 74 61 62 61 73 65 73 20 74  (ex: databases t
cd3d0 68 61 74 20 61 72 65 20 62 75 72 6e 65 64 20 69  hat are burned i
cd3e0 6e 74 6f 20 43 44 2d 52 4f 4d 2c 20 66 6f 72 20  nto CD-ROM, for 
cd3f0 65 78 61 6d 70 6c 65 2e 29 20 20 49 74 20 63 61  example.)  It ca
cd400 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65  n.** also be use
cd410 64 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61  d if the applica
cd420 74 69 6f 6e 20 65 6d 70 6c 6f 79 73 20 73 6f 6d  tion employs som
cd430 65 20 65 78 74 65 72 6e 61 6c 20 6d 65 63 68 61  e external mecha
cd440 6e 69 73 6d 20 74 6f 0a 2a 2a 20 70 72 65 76 65  nism to.** preve
cd450 6e 74 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20  nt simultaneous 
cd460 61 63 63 65 73 73 20 6f 66 20 74 68 65 20 73 61  access of the sa
cd470 6d 65 20 64 61 74 61 62 61 73 65 20 62 79 20 74  me database by t
cd480 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64 61  wo or more.** da
cd490 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
cd4a0 6e 73 2e 20 20 42 75 74 20 74 68 65 72 65 20 69  ns.  But there i
cd4b0 73 20 61 20 73 65 72 69 6f 75 73 20 72 69 73 6b  s a serious risk
cd4c0 20 6f 66 20 64 61 74 61 62 61 73 65 0a 2a 2a 20   of database.** 
cd4d0 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68  corruption if th
cd4e0 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20  is locking mode 
cd4f0 69 73 20 75 73 65 64 20 69 6e 20 73 69 74 75 61  is used in situa
cd500 74 69 6f 6e 73 20 77 68 65 72 65 20 6d 75 6c 74  tions where mult
cd510 69 70 6c 65 0a 2a 2a 20 64 61 74 61 62 61 73 65  iple.** database
cd520 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65   connections are
cd530 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73   accessing the s
cd540 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ame database fil
cd550 65 20 61 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a  e at the same.**
cd560 20 74 69 6d 65 20 61 6e 64 20 6f 6e 65 20 6f 72   time and one or
cd570 20 6d 6f 72 65 20 6f 66 20 74 68 6f 73 65 20 63   more of those c
cd580 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 77  onnections are w
cd590 72 69 74 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74  riting..*/..stat
cd5a0 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65  ic int nolockChe
cd5b0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
cd5c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74  qlite3_file *Not
cd5d0 55 73 65 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f  Used, int *pResO
cd5e0 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ut){.  UNUSED_PA
cd5f0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
cd600 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 30  ;.  *pResOut = 0
cd610 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
cd620 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69  E_OK;.}.static i
cd630 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71  nt nolockLock(sq
cd640 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55  lite3_file *NotU
cd650 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64  sed, int NotUsed
cd660 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  2){.  UNUSED_PAR
cd670 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
cd680 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65   NotUsed2);.  re
cd690 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
cd6a0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c  }.static int nol
cd6b0 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  ockUnlock(sqlite
cd6c0 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c  3_file *NotUsed,
cd6d0 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a   int NotUsed2){.
cd6e0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
cd6f0 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74  ER2(NotUsed, Not
cd700 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e  Used2);.  return
cd710 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
cd720 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66  *.** Close the f
cd730 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
cd740 6e 74 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73  nt nolockClose(s
cd750 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
cd760 20 7b 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73   {.  return clos
cd770 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d  eUnixFile(id);.}
cd780 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
cd790 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68  ****** End of th
cd7a0 65 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70  e no-op lock imp
cd7b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a  lementation ****
cd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd7d0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
cd7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd820 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
cd830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd870 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
cd880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
cd890 69 6e 20 64 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b  in dot-file Lock
cd8a0 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ing ************
cd8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd8c0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f 74  **.**.** The dot
cd8d0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70  file locking imp
cd8e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73  lementation uses
cd8f0 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 6f 66   the existing of
cd900 20 73 65 70 61 72 61 74 65 20 6c 6f 63 6b 0a 2a   separate lock.*
cd910 2a 20 66 69 6c 65 73 20 69 6e 20 6f 72 64 65 72  * files in order
cd920 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65   to control acce
cd930 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ss to the databa
cd940 73 65 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 20  se.  This works 
cd950 6f 6e 20 6a 75 73 74 0a 2a 2a 20 61 62 6f 75 74  on just.** about
cd960 20 65 76 65 72 79 20 66 69 6c 65 73 79 73 74 65   every filesyste
cd970 6d 20 69 6d 61 67 69 6e 61 62 6c 65 2e 20 20 42  m imaginable.  B
cd980 75 74 20 74 68 65 72 65 20 61 72 65 20 73 65 72  ut there are ser
cd990 69 6f 75 73 20 64 6f 77 6e 73 69 64 65 73 3a 0a  ious downsides:.
cd9a0 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 68  **.**    (1)  Th
cd9b0 65 72 65 20 69 73 20 7a 65 72 6f 20 63 6f 6e 63  ere is zero conc
cd9c0 75 72 72 65 6e 63 79 2e 20 20 41 20 73 69 6e 67  urrency.  A sing
cd9d0 6c 65 20 72 65 61 64 65 72 20 62 6c 6f 63 6b 73  le reader blocks
cd9e0 20 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 20 20   all other.**   
cd9f0 20 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e        connection
cda00 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 20 6f  s from reading o
cda10 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61  r writing the da
cda20 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  tabase..**.**   
cda30 20 28 32 29 20 20 41 6e 20 61 70 70 6c 69 63 61   (2)  An applica
cda40 74 69 6f 6e 20 63 72 61 73 68 20 6f 72 20 70 6f  tion crash or po
cda50 77 65 72 20 6c 6f 73 73 20 63 61 6e 20 6c 65 61  wer loss can lea
cda60 76 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20 66 69  ve stale lock fi
cda70 6c 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 73  les.**         s
cda80 69 74 74 69 6e 67 20 61 72 6f 75 6e 64 20 74 68  itting around th
cda90 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c  at need to be cl
cdaa0 65 61 72 65 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a  eared manually..
cdab0 2a 2a 0a 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65  **.** Neverthele
cdac0 73 73 2c 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73  ss, a dotlock is
cdad0 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
cdae0 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72  locking mode for
cdaf0 20 75 73 65 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74   use if no.** ot
cdb00 68 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61  her locking stra
cdb10 74 65 67 79 20 69 73 20 61 76 61 69 6c 61 62 6c  tegy is availabl
cdb20 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65  e..**.** Dotfile
cdb30 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 20 62   locking works b
cdb40 79 20 63 72 65 61 74 69 6e 67 20 61 20 66 69 6c  y creating a fil
cdb50 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69  e in the same di
cdb60 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 0a 2a  rectory as the.*
cdb70 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77  * database and w
cdb80 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  ith the same nam
cdb90 65 20 62 75 74 20 77 69 74 68 20 61 20 22 2e 6c  e but with a ".l
cdba0 6f 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e 20 61  ock" extension a
cdbb0 64 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69  dded..** The exi
cdbc0 73 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b  stance of a lock
cdbd0 20 66 69 6c 65 20 69 6d 70 6c 69 65 73 20 61 6e   file implies an
cdbe0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
cdbf0 20 20 41 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b    All other lock
cdc00 0a 2a 2a 20 74 79 70 65 73 20 28 53 48 41 52 45  .** types (SHARE
cdc10 44 2c 20 52 45 53 45 52 56 45 44 2c 20 50 45 4e  D, RESERVED, PEN
cdc20 44 49 4e 47 29 20 61 72 65 20 6d 61 70 70 65 64  DING) are mapped
cdc30 20 69 6e 74 6f 20 45 58 43 4c 55 53 49 56 45 2e   into EXCLUSIVE.
cdc40 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .*/../*.** The f
cdc50 69 6c 65 20 73 75 66 66 69 78 20 61 64 64 65 64  ile suffix added
cdc60 20 74 6f 20 74 68 65 20 64 61 74 61 20 62 61 73   to the data bas
cdc70 65 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6f 72  e filename in or
cdc80 64 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68  der to create th
cdc90 65 0a 2a 2a 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a  e.** lock file..
cdca0 2a 2f 0a 23 64 65 66 69 6e 65 20 44 4f 54 4c 4f  */.#define DOTLO
cdcb0 43 4b 5f 53 55 46 46 49 58 20 22 2e 6c 6f 63 6b  CK_SUFFIX ".lock
cdcc0 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  "../*.** This ro
cdcd0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
cdce0 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
cdcf0 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
cdd00 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
cdd10 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
cdd20 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
cdd30 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
cdd40 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74  ock is held, set
cdd50 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20   *pResOut.** to 
cdd60 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
cdd70 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73   otherwise *pRes
cdd80 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65  Out is set to ze
cdd90 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  ro.  The return 
cdda0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20  value.** is set 
cddb0 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c  to SQLITE_OK unl
cddc0 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72  ess an I/O error
cddd0 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c   occurs during l
cdde0 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a  ock checking..**
cddf0 0a 2a 2a 20 49 6e 20 64 6f 74 66 69 6c 65 20 6c  .** In dotfile l
cde00 6f 63 6b 69 6e 67 2c 20 65 69 74 68 65 72 20 61  ocking, either a
cde10 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 6f 72 20   lock exists or 
cde20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 20 20 53 6f  it does not.  So
cde30 20 69 6e 20 74 68 69 73 0a 2a 2a 20 76 61 72 69   in this.** vari
cde40 61 74 69 6f 6e 20 6f 66 20 43 68 65 63 6b 52 65  ation of CheckRe
cde50 73 65 72 76 65 64 4c 6f 63 6b 28 29 2c 20 2a 70  servedLock(), *p
cde60 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f  ResOut is set to
cde70 20 74 72 75 65 20 69 66 20 61 6e 79 20 6c 6f 63   true if any loc
cde80 6b 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 6e 20  k.** is held on 
cde90 74 68 65 20 66 69 6c 65 20 61 6e 64 20 66 61 6c  the file and fal
cdea0 73 65 20 69 66 20 74 68 65 20 66 69 6c 65 20 69  se if the file i
cdeb0 73 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73  s unlocked..*/.s
cdec0 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63  tatic int dotloc
cded0 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  kCheckReservedLo
cdee0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
cdef0 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
cdf00 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t) {.  int rc = 
cdf10 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
cdf20 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20   reserved = 0;. 
cdf30 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
cdf40 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
cdf50 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  ;..  SimulateIOE
cdf60 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
cdf70 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
cdf80 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a  ESERVEDLOCK; );.
cdf90 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69    .  assert( pFi
cdfa0 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  le );..  /* Chec
cdfb0 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e  k if a thread in
cdfc0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f   this process ho
cdfd0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20  lds such a lock 
cdfe0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
cdff0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
ce000 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45  LOCK ){.    /* E
ce010 69 74 68 65 72 20 74 68 69 73 20 63 6f 6e 6e 65  ither this conne
ce020 63 74 69 6f 6e 20 6f 72 20 73 6f 6d 65 20 6f 74  ction or some ot
ce030 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  her connection i
ce040 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  n the same proce
ce050 73 73 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 73 20  ss.    ** holds 
ce060 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69  a lock on the fi
ce070 6c 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20  le.  No need to 
ce080 63 68 65 63 6b 20 66 75 72 74 68 65 72 2e 20 2a  check further. *
ce090 2f 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d  /.    reserved =
ce0a0 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
ce0b0 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 20 69 73 20   /* The lock is 
ce0c0 68 65 6c 64 20 69 66 20 61 6e 64 20 6f 6e 6c 79  held if and only
ce0d0 20 69 66 20 74 68 65 20 6c 6f 63 6b 66 69 6c 65   if the lockfile
ce0e0 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 63   exists */.    c
ce0f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 6b  onst char *zLock
ce100 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  File = (const ch
ce110 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  ar*)pFile->locki
ce120 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 72  ngContext;.    r
ce130 65 73 65 72 76 65 64 20 3d 20 61 63 63 65 73 73  eserved = access
ce140 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 30 29 3d 3d  (zLockFile, 0)==
ce150 30 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45  0;.  }.  OSTRACE
ce160 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  4("TEST WR-LOCK 
ce170 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69  %d %d %d\n", pFi
ce180 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72  le->h, rc, reser
ce190 76 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74  ved);.  *pResOut
ce1a0 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72   = reserved;.  r
ce1b0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
ce1c0 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65  ** Lock the file
ce1d0 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73   with the lock s
ce1e0 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
ce1f0 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d  meter locktype -
ce200 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66   one.** of the f
ce210 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
ce220 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c      (1) SHARED_L
ce230 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52  OCK.**     (2) R
ce240 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20  ESERVED_LOCK.** 
ce250 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f      (3) PENDING_
ce260 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20  LOCK.**     (4) 
ce270 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a  EXCLUSIVE_LOCK.*
ce280 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
ce290 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
ce2a0 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
ce2b0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
ce2c0 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73  tates.** are ins
ce2d0 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e  erted in between
ce2e0 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d  .  The locking m
ce2f0 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65  ight fail on one
ce300 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a   of the later.**
ce310 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61   transitions lea
ce320 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74  ving the lock st
ce330 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  ate different fr
ce340 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74  om what it start
ce350 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20  ed but.** still 
ce360 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
ce370 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
ce380 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
ce390 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61  e allowed.** tra
ce3a0 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
ce3b0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d   inserted interm
ce3c0 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a  ediate states:.*
ce3d0 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44  *.**    UNLOCKED
ce3e0 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20   -> SHARED.**   
ce3f0 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
ce400 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  VED.**    SHARED
ce410 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
ce420 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
ce430 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45   RESERVED -> (PE
ce440 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
ce450 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  IVE.**    PENDIN
ce460 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  G -> EXCLUSIVE.*
ce470 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
ce480 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72  e will only incr
ce490 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73  ease a lock.  Us
ce4a0 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55  e the sqlite3OsU
ce4b0 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69  nlock().** routi
ce4c0 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f  ne to lower a lo
ce4d0 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a  cking level..**.
ce4e0 2a 2a 20 57 69 74 68 20 64 6f 74 66 69 6c 65 20  ** With dotfile 
ce4f0 6c 6f 63 6b 69 6e 67 2c 20 77 65 20 72 65 61 6c  locking, we real
ce500 6c 79 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20  ly only support 
ce510 73 74 61 74 65 20 28 34 29 3a 20 45 58 43 4c 55  state (4): EXCLU
ce520 53 49 56 45 2e 0a 2a 2a 20 42 75 74 20 77 65 20  SIVE..** But we 
ce530 74 72 61 63 6b 20 74 68 65 20 6f 74 68 65 72 20  track the other 
ce540 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73 20 69  locking levels i
ce550 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74  nternally..*/.st
ce560 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b  atic int dotlock
ce570 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
ce580 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
ce590 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c  ype) {.  unixFil
ce5a0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
ce5b0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20  File*)id;.  int 
ce5c0 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63  fd;.  char *zLoc
ce5d0 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29  kFile = (char *)
ce5e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
ce5f0 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 72 63 20  ntext;.  int rc 
ce600 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20  = SQLITE_OK;... 
ce610 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61   /* If we have a
ce620 6e 79 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68  ny lock, then th
ce630 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65  e lock file alre
ce640 61 64 79 20 65 78 69 73 74 73 2e 20 20 41 6c 6c  ady exists.  All
ce650 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 74 6f   we have.  ** to
ce660 20 64 6f 20 69 73 20 61 64 6a 75 73 74 20 6f 75   do is adjust ou
ce670 72 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72  r internal recor
ce680 64 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 6c 65  d of the lock le
ce690 76 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  vel..  */.  if( 
ce6a0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
ce6b0 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  > NO_LOCK ){.   
ce6c0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
ce6d0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 23 69 66   = locktype;.#if
ce6e0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20   !OS_VXWORKS.   
ce6f0 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64 61 74   /* Always updat
ce700 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d 70 20  e the timestamp 
ce710 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c 65 20  on the old file 
ce720 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73 28 7a 4c  */.    utimes(zL
ce730 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a  ockFile, NULL);.
ce740 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72  #endif.    retur
ce750 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
ce760 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e  .  .  /* grab an
ce770 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
ce780 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a  */.  fd = open(z
ce790 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c  LockFile,O_RDONL
ce7a0 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 58 43 4c  Y|O_CREAT|O_EXCL
ce7b0 2c 30 36 30 30 29 3b 0a 20 20 69 66 28 20 66 64  ,0600);.  if( fd
ce7c0 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66 61 69  <0 ){.    /* fai
ce7d0 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 72 65 61  led to open/crea
ce7e0 74 65 20 74 68 65 20 66 69 6c 65 2c 20 73 6f 6d  te the file, som
ce7f0 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79 20 68 61  eone else may ha
ce800 76 65 20 73 74 6f 6c 65 6e 20 74 68 65 20 6c 6f  ve stolen the lo
ce810 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 74 45  ck */.    int tE
ce820 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
ce830 20 20 69 66 28 20 45 45 58 49 53 54 20 3d 3d 20    if( EEXIST == 
ce840 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20  tErrno ){.      
ce850 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
ce860 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20  ;.    } else {. 
ce870 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
ce880 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
ce890 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
ce8a0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a  TE_IOERR_LOCK);.
ce8b0 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43        if( IS_LOC
ce8c0 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20  K_ERROR(rc) ){. 
ce8d0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
ce8e0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
ce8f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
ce900 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
ce910 20 7d 20 0a 20 20 69 66 28 20 63 6c 6f 73 65 28   } .  if( close(
ce920 66 64 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  fd) ){.    pFile
ce930 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
ce940 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  rno;.    rc = SQ
ce950 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45  LITE_IOERR_CLOSE
ce960 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f  ;.  }.  .  /* go
ce970 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79  t it, set the ty
ce980 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b  pe and return ok
ce990 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63   */.  pFile->loc
ce9a0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
ce9b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
ce9c0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68  ../*.** Lower th
ce9d0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
ce9e0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  on file descript
ce9f0 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b  or pFile to lock
cea00 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
cea10 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
cea20 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
cea30 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
cea40 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
cea50 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
cea60 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
cea70 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
cea80 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
cea90 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
ceaa0 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
ceab0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
ceac0 2a 20 57 68 65 6e 20 74 68 65 20 6c 6f 63 6b 69  * When the locki
cead0 6e 67 20 6c 65 76 65 6c 20 72 65 61 63 68 65 73  ng level reaches
ceae0 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 65 6c 65 74 65   NO_LOCK, delete
ceaf0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a   the lock file..
ceb00 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f  */.static int do
ceb10 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69  tlockUnlock(sqli
ceb20 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
ceb30 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20  t locktype) {.  
ceb40 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
ceb50 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
ceb60 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69  .  char *zLockFi
ceb70 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69  le = (char *)pFi
ceb80 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
ceb90 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  xt;..  assert( p
ceba0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
cebb0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25  E5("UNLOCK  %d %
cebc0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c  d was %d pid=%d\
cebd0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
cebe0 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 69 6c  cktype,..   pFil
cebf0 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74  e->locktype, get
cec00 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74  pid());.  assert
cec10 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
cec20 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20  ED_LOCK );.  .  
cec30 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73  /* no-op if poss
cec40 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46  ible */.  if( pF
cec50 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c  ile->locktype==l
cec60 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72  ocktype ){.    r
cec70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
cec80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f  .  }..  /* To do
cec90 77 6e 67 72 61 64 65 20 74 6f 20 73 68 61 72 65  wngrade to share
ceca0 64 2c 20 73 69 6d 70 6c 79 20 75 70 64 61 74 65  d, simply update
cecb0 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   our internal no
cecc0 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a  tion of the.  **
cecd0 20 6c 6f 63 6b 20 73 74 61 74 65 2e 20 20 4e 6f   lock state.  No
cece0 20 6e 65 65 64 20 74 6f 20 6d 65 73 73 20 77 69   need to mess wi
cecf0 74 68 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64  th the file on d
ced00 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  isk..  */.  if( 
ced10 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
ced20 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69  _LOCK ){.    pFi
ced30 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53  le->locktype = S
ced40 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20  HARED_LOCK;.    
ced50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
ced60 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f  ;.  }.  .  /* To
ced70 20 66 75 6c 6c 79 20 75 6e 6c 6f 63 6b 20 74 68   fully unlock th
ced80 65 20 64 61 74 61 62 61 73 65 2c 20 64 65 6c 65  e database, dele
ced90 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65  te the lock file
ceda0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   */.  assert( lo
cedb0 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20  cktype==NO_LOCK 
cedc0 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6e 6b 28  );.  if( unlink(
cedd0 7a 4c 6f 63 6b 46 69 6c 65 29 20 29 7b 0a 20 20  zLockFile) ){.  
cede0 20 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f    int rc, tErrno
cedf0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66   = errno;.    if
cee00 28 20 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72  ( ENOENT != tErr
cee10 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  no ){.      rc =
cee20 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
cee30 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
cee40 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
cee50 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20  UNLOCK);.    }. 
cee60 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
cee70 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
cee80 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
cee90 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
ceea0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63   }.    return rc
ceeb0 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e  ; .  }.  pFile->
ceec0 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f  locktype = NO_LO
ceed0 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  CK;.  return SQL
ceee0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
ceef0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 20 20   Close a file.  
cef00 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f  Make sure the lo
cef10 63 6b 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65  ck has been rele
cef20 61 73 65 64 20 62 65 66 6f 72 65 20 63 6c 6f 73  ased before clos
cef30 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
cef40 6e 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28  nt dotlockClose(
cef50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
cef60 29 20 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  ) {.  int rc;.  
cef70 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e  if( id ){.    un
cef80 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
cef90 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
cefa0 20 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b     dotlockUnlock
cefb0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  (id, NO_LOCK);. 
cefc0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
cefd0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
cefe0 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 63  ntext);.  }.  rc
ceff0 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65   = closeUnixFile
cf000 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  (id);.  return r
cf010 63 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c;.}./**********
cf020 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
cf030 74 68 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63  the dot-file loc
cf040 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
cf050 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
cf060 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
cf070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf0b0 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
cf0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf100 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
cf110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf120 20 42 65 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63   Begin flock Loc
cf130 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  king ***********
cf140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf150 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20  *****.**.** Use 
cf160 74 68 65 20 66 6c 6f 63 6b 28 29 20 73 79 73 74  the flock() syst
cf170 65 6d 20 63 61 6c 6c 20 74 6f 20 64 6f 20 66 69  em call to do fi
cf180 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a  le locking..**.*
cf190 2a 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e  * flock() lockin
cf1a0 67 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d 66 69  g is like dot-fi
cf1b0 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68  le locking in th
cf1c0 61 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a 2a  at the various.*
cf1d0 2a 20 66 69 6e 65 2d 67 72 61 69 6e 20 6c 6f 63  * fine-grain loc
cf1e0 6b 69 6e 67 20 6c 65 76 65 6c 73 20 73 75 70 70  king levels supp
cf1f0 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 20  orted by SQLite 
cf200 61 72 65 20 63 6f 6c 6c 61 70 73 65 64 20 69 6e  are collapsed in
cf210 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 65  to.** a single e
cf220 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20  xclusive lock.  
cf230 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
cf240 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44  SHARED, RESERVED
cf250 2c 20 61 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e 47  , and.** PENDING
cf260 20 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20 73   locks are the s
cf270 61 6d 65 20 74 68 69 6e 67 20 61 73 20 61 6e 20  ame thing as an 
cf280 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
cf290 20 53 51 4c 69 74 65 0a 2a 2a 20 73 74 69 6c 6c   SQLite.** still
cf2a0 20 77 6f 72 6b 73 20 77 68 65 6e 20 79 6f 75 20   works when you 
cf2b0 64 6f 20 74 68 69 73 2c 20 62 75 74 20 63 6f 6e  do this, but con
cf2c0 63 75 72 72 65 6e 63 79 20 69 73 20 72 65 64 75  currency is redu
cf2d0 63 65 64 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c  ced since.** onl
cf2e0 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65  y a single proce
cf2f0 73 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e  ss can be readin
cf300 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  g the database a
cf310 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  t a time..**.** 
cf320 4f 6d 69 74 20 74 68 69 73 20 73 65 63 74 69 6f  Omit this sectio
cf330 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  n if SQLITE_ENAB
cf340 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
cf350 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66 20 6f   is turned off o
cf360 72 20 69 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e  r if.** compilin
cf370 67 20 66 6f 72 20 56 58 57 4f 52 4b 53 2e 0a 2a  g for VXWORKS..*
cf380 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  /.#if SQLITE_ENA
cf390 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
cf3a0 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53  E && !OS_VXWORKS
cf3b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
cf3c0 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
cf3d0 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
cf3e0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
cf3f0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
cf400 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72   file by this or
cf410 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
cf420 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ss. If such a lo
cf430 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20  ck is held, set 
cf440 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61  *pResOut.** to a
cf450 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20   non-zero value 
cf460 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f  otherwise *pResO
cf470 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72  ut is set to zer
cf480 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  o.  The return v
cf490 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74  alue.** is set t
cf4a0 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65  o SQLITE_OK unle
cf4b0 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20  ss an I/O error 
cf4c0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f  occurs during lo
cf4d0 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a  ck checking..*/.
cf4e0 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b  static int flock
cf4f0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
cf500 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
cf510 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
cf520 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
cf530 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72  LITE_OK;.  int r
cf540 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75  eserved = 0;.  u
cf550 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
cf560 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
cf570 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45    .  SimulateIOE
cf580 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
cf590 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
cf5a0 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a  ESERVEDLOCK; );.
cf5b0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69    .  assert( pFi
cf5c0 6c 65 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68  le );.  .  /* Ch
cf5d0 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20  eck if a thread 
cf5e0 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
cf5f0 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63  holds such a loc
cf600 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  k */.  if( pFile
cf610 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45  ->locktype>SHARE
cf620 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65  D_LOCK ){.    re
cf630 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  served = 1;.  }.
cf640 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73    .  /* Otherwis
cf650 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74  e see if some ot
cf660 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64  her process hold
cf670 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21  s it. */.  if( !
cf680 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20  reserved ){.    
cf690 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 67 65  /* attempt to ge
cf6a0 74 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  t the lock */.  
cf6b0 20 20 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63    int lrc = floc
cf6c0 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b  k(pFile->h, LOCK
cf6d0 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a  _EX | LOCK_NB);.
cf6e0 20 20 20 20 69 66 28 20 21 6c 72 63 20 29 7b 0a      if( !lrc ){.
cf6f0 20 20 20 20 20 20 2f 2a 20 67 6f 74 20 74 68 65        /* got the
cf700 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74   lock, unlock it
cf710 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20   */.      lrc = 
cf720 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20  flock(pFile->h, 
cf730 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20  LOCK_UN);.      
cf740 69 66 20 28 20 6c 72 63 20 29 20 7b 0a 20 20 20  if ( lrc ) {.   
cf750 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20       int tErrno 
cf760 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
cf770 20 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65   /* unlock faile
cf780 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  d with an error 
cf790 2a 2f 0a 20 20 20 20 20 20 20 20 6c 72 63 20 3d  */.        lrc =
cf7a0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
cf7b0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
cf7c0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
cf7d0 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20  UNLOCK); .      
cf7e0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
cf7f0 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20  ROR(lrc) ){.    
cf800 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
cf810 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
cf820 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
cf830 6c 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  lrc;.        }. 
cf840 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73       }.    } els
cf850 65 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45  e {.      int tE
cf860 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
cf870 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31      reserved = 1
cf880 3b 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f  ;.      /* someo
cf890 6e 65 20 65 6c 73 65 20 6d 69 67 68 74 20 68 61  ne else might ha
cf8a0 76 65 20 69 74 20 72 65 73 65 72 76 65 64 20 2a  ve it reserved *
cf8b0 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71  /.      lrc = sq
cf8c0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
cf8d0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
cf8e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
cf8f0 4b 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 49  K); .      if( I
cf900 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63  S_LOCK_ERROR(lrc
cf910 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  ) ){.        pFi
cf920 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
cf930 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20  tErrno;.        
cf940 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20  rc = lrc;.      
cf950 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53  }.    }.  }.  OS
cf960 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d  TRACE4("TEST WR-
cf970 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22  LOCK %d %d %d\n"
cf980 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20  , pFile->h, rc, 
cf990 72 65 73 65 72 76 65 64 29 3b 0a 0a 23 69 66 64  reserved);..#ifd
cf9a0 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  ef SQLITE_IGNORE
cf9b0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
cf9c0 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53  RS.  if( (rc & S
cf9d0 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20  QLITE_IOERR) == 
cf9e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a  SQLITE_IOERR ){.
cf9f0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
cfa00 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64  OK;.    reserved
cfa10 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  =1;.  }.#endif /
cfa20 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f  * SQLITE_IGNORE_
cfa30 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52  FLOCK_LOCK_ERROR
cfa40 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74 20  S */.  *pResOut 
cfa50 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65  = reserved;.  re
cfa60 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
cfa70 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20  * Lock the file 
cfa80 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70  with the lock sp
cfa90 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ecified by param
cfaa0 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20  eter locktype - 
cfab0 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f  one.** of the fo
cfac0 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
cfad0 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f     (1) SHARED_LO
cfae0 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45  CK.**     (2) RE
cfaf0 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SERVED_LOCK.**  
cfb00 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c     (3) PENDING_L
cfb10 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45  OCK.**     (4) E
cfb20 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a  XCLUSIVE_LOCK.**
cfb30 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68  .** Sometimes wh
cfb40 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e  en requesting on
cfb50 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64  e lock state, ad
cfb60 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74  ditional lock st
cfb70 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65  ates.** are inse
cfb80 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e  rted in between.
cfb90 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69    The locking mi
cfba0 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20  ght fail on one 
cfbb0 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20  of the later.** 
cfbc0 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76  transitions leav
cfbd0 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61  ing the lock sta
cfbe0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  te different fro
cfbf0 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65  m what it starte
cfc00 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73  d but.** still s
cfc10 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c  hort of its goal
cfc20 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
cfc30 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65   chart shows the
cfc40 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e   allowed.** tran
cfc50 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20  sitions and the 
cfc60 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65  inserted interme
cfc70 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a  diate states:.**
cfc80 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20  .**    UNLOCKED 
cfc90 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20  -> SHARED.**    
cfca0 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56  SHARED -> RESERV
cfcb0 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
cfcc0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
cfcd0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
cfce0 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e  RESERVED -> (PEN
cfcf0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
cfd00 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47  VE.**    PENDING
cfd10 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
cfd20 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79  .** flock() only
cfd30 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20   really support 
cfd40 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e  EXCLUSIVE locks.
cfd50 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72    We track inter
cfd60 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20  mediate.** lock 
cfd70 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 71  states in the sq
cfd80 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63  lite3_file struc
cfd90 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f  ture, but all lo
cfda0 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a  cks SHARED or.**
cfdb0 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c   above are reall
cfdc0 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  y EXCLUSIVE lock
cfdd0 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c  s and exclude al
cfde0 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  l other processe
cfdf0 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73  s from.** access
cfe00 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
cfe10 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
cfe20 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65  ll only increase
cfe30 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68   a lock.  Use th
cfe40 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63  e sqlite3OsUnloc
cfe50 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74  k().** routine t
cfe60 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e  o lower a lockin
cfe70 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74  g level..*/.stat
cfe80 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b  ic int flockLock
cfe90 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
cfea0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
cfeb0 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51   {.  int rc = SQ
cfec0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46  LITE_OK;.  unixF
cfed0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
cfee0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61  ixFile*)id;..  a
cfef0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
cff00 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65  .  /* if we alre
cff10 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c  ady have a lock,
cff20 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65   it is exclusive
cff30 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64  .  .  ** Just ad
cff40 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70  just level and p
cff50 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72  unt on outta her
cff60 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c  e. */.  if (pFil
cff70 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f  e->locktype > NO
cff80 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69  _LOCK) {.    pFi
cff90 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
cffa0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74  ocktype;.    ret
cffb0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
cffc0 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20   }.  .  /* grab 
cffd0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
cffe0 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66 6c  k */.  .  if (fl
cfff0 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f  ock(pFile->h, LO
d0000 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29  CK_EX | LOCK_NB)
d0010 29 20 7b 0a 20 20 20 20 69 6e 74 20 74 45 72 72  ) {.    int tErr
d0020 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d0030 2f 2a 20 64 69 64 6e 27 74 20 67 65 74 2c 20 6d  /* didn't get, m
d0040 75 73 74 20 62 65 20 62 75 73 79 20 2a 2f 0a 20  ust be busy */. 
d0050 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
d0060 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
d0070 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
d0080 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20  _IOERR_LOCK);.  
d0090 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d00a0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
d00b0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d00c0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d00d0 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  }.  } else {.   
d00e0 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20   /* got it, set 
d00f0 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74  the type and ret
d0100 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 46  urn ok */.    pF
d0110 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
d0120 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20  locktype;.  }.  
d0130 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20  OSTRACE4("LOCK  
d0140 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70    %d %s %s\n", p
d0150 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
d0160 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c  eName(locktype),
d0170 20 0a 20 20 20 20 20 20 20 20 20 20 20 72 63 3d   .           rc=
d0180 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b  =SQLITE_OK ? "ok
d0190 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 23  " : "failed");.#
d01a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e  ifdef SQLITE_IGN
d01b0 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45  ORE_FLOCK_LOCK_E
d01c0 52 52 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20  RRORS.  if( (rc 
d01d0 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20  & SQLITE_IOERR) 
d01e0 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  == SQLITE_IOERR 
d01f0 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
d0200 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e  TE_BUSY;.  }.#en
d0210 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47  dif /* SQLITE_IG
d0220 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f  NORE_FLOCK_LOCK_
d0230 45 52 52 4f 52 53 20 2a 2f 0a 20 20 72 65 74 75  ERRORS */.  retu
d0240 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
d0250 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
d0260 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
d0270 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c   descriptor pFil
d0280 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20  e to locktype.  
d0290 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74  locktype.** must
d02a0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
d02b0 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
d02c0 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  K..**.** If the 
d02d0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66  locking level of
d02e0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
d02f0 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  ptor is already 
d0300 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74  at or below.** t
d0310 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
d0320 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
d0330 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
d0340 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
d0350 6e 74 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73  nt flockUnlock(s
d0360 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d0370 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
d0380 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d0390 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d03a0 69 64 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28  id;.  .  assert(
d03b0 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52   pFile );.  OSTR
d03c0 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64  ACE5("UNLOCK  %d
d03d0 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25   %d was %d pid=%
d03e0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
d03f0 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20  locktype,.      
d0400 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b       pFile->lock
d0410 74 79 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b  type, getpid());
d0420 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
d0430 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
d0440 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f   );.  .  /* no-o
d0450 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f  p if possible */
d0460 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d0470 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65  cktype==locktype
d0480 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
d0490 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
d04a0 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e  .  /* shared can
d04b0 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65 63   just be set bec
d04c0 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20 68  ause we always h
d04d0 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ave an exclusive
d04e0 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79   */.  if (lockty
d04f0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29  pe==SHARED_LOCK)
d0500 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f   {.    pFile->lo
d0510 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
d0520 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e;.    return SQ
d0530 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a  LITE_OK;.  }.  .
d0540 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c    /* no, really,
d0550 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e   unlock. */.  in
d0560 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69  t rc = flock(pFi
d0570 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b  le->h, LOCK_UN);
d0580 0a 20 20 69 66 20 28 72 63 29 20 7b 0a 20 20 20  .  if (rc) {.   
d0590 20 69 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20 3d   int r, tErrno =
d05a0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d 20   errno;.    r = 
d05b0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d05c0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d05d0 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  , SQLITE_IOERR_U
d05e0 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20  NLOCK);.    if( 
d05f0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 29  IS_LOCK_ERROR(r)
d0600 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   ){.      pFile-
d0610 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d0620 72 6e 6f 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65  rno;.    }.#ifde
d0630 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f  f SQLITE_IGNORE_
d0640 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52  FLOCK_LOCK_ERROR
d0650 53 0a 20 20 20 20 69 66 28 20 28 72 20 26 20 53  S.    if( (r & S
d0660 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20  QLITE_IOERR) == 
d0670 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a  SQLITE_IOERR ){.
d0680 20 20 20 20 20 20 72 20 3d 20 53 51 4c 49 54 45        r = SQLITE
d0690 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 23 65 6e  _BUSY;.    }.#en
d06a0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47  dif /* SQLITE_IG
d06b0 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f  NORE_FLOCK_LOCK_
d06c0 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 0a 20  ERRORS */.    . 
d06d0 20 20 20 72 65 74 75 72 6e 20 72 3b 0a 20 20 7d     return r;.  }
d06e0 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 46 69 6c   else {.    pFil
d06f0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f  e->locktype = NO
d0700 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 72  _LOCK;.    retur
d0710 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d0720 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
d0730 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
d0740 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73 65  c int flockClose
d0750 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d0760 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b  d) {.  if( id ){
d0770 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b  .    flockUnlock
d0780 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  (id, NO_LOCK);. 
d0790 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73   }.  return clos
d07a0 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d  eUnixFile(id);.}
d07b0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
d07c0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
d07d0 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56  G_STYLE && !OS_V
d07e0 58 57 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  XWORK */../*****
d07f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
d0800 6e 64 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b 20  nd of the flock 
d0810 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lock implementat
d0820 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ion ************
d0830 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
d0840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0880 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a  ********/../****
d0890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d08a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d08b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d08c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d08d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
d08e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d08f0 2a 2a 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64 20  *** Begin Named 
d0900 53 65 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69 6e  Semaphore Lockin
d0910 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  g **************
d0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
d0930 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65   Named semaphore
d0940 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79   locking is only
d0950 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 56 78   supported on Vx
d0960 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d  Works..**.** Sem
d0970 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69  aphore locking i
d0980 73 20 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20  s like dot-lock 
d0990 61 6e 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68 61  and flock in tha
d09a0 74 20 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c 79  t it really only
d09b0 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 45 58 43  .** supports EXC
d09c0 4c 55 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e 20  LUSIVE locking. 
d09d0 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70   Only a single p
d09e0 72 6f 63 65 73 73 20 63 61 6e 20 72 65 61 64 20  rocess can read 
d09f0 6f 72 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20  or write.** the 
d0a00 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74  database file at
d0a10 20 61 20 74 69 6d 65 2e 20 20 54 68 69 73 20 72   a time.  This r
d0a20 65 64 75 63 65 73 20 70 6f 74 65 6e 74 69 61 6c  educes potential
d0a30 20 63 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62 75   concurrency, bu
d0a40 74 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 6c  t.** makes the l
d0a50 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ock implementati
d0a60 6f 6e 20 6d 75 63 68 20 65 61 73 69 65 72 2e 0a  on much easier..
d0a70 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  */.#if OS_VXWORK
d0a80 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  S../*.** This ro
d0a90 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
d0aa0 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
d0ab0 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
d0ac0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
d0ad0 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
d0ae0 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
d0af0 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
d0b00 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74  ock is held, set
d0b10 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20   *pResOut.** to 
d0b20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
d0b30 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73   otherwise *pRes
d0b40 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65  Out is set to ze
d0b50 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  ro.  The return 
d0b60 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20  value.** is set 
d0b70 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c  to SQLITE_OK unl
d0b80 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72  ess an I/O error
d0b90 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c   occurs during l
d0ba0 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f  ock checking..*/
d0bb0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43  .static int semC
d0bc0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
d0bd0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d0be0 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  d, int *pResOut)
d0bf0 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51   {.  int rc = SQ
d0c00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72  LITE_OK;.  int r
d0c10 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75  eserved = 0;.  u
d0c20 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d0c30 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d0c40 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
d0c50 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
d0c60 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
d0c70 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20  ERVEDLOCK; );.  
d0c80 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
d0c90 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20   );..  /* Check 
d0ca0 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74  if a thread in t
d0cb0 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64  his process hold
d0cc0 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f  s such a lock */
d0cd0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d0ce0 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f  cktype>SHARED_LO
d0cf0 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76  CK ){.    reserv
d0d00 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20  ed = 1;.  }.  . 
d0d10 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65   /* Otherwise se
d0d20 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  e if some other 
d0d30 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74  process holds it
d0d40 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65  . */.  if( !rese
d0d50 72 76 65 64 20 29 7b 0a 20 20 20 20 73 65 6d 5f  rved ){.    sem_
d0d60 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d  t *pSem = pFile-
d0d70 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20  >pOpen->pSem;.  
d0d80 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 74    struct stat st
d0d90 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66 28 20  atBuf;..    if( 
d0da0 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d  sem_trywait(pSem
d0db0 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 69  )==-1 ){.      i
d0dc0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
d0dd0 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 41 47  o;.      if( EAG
d0de0 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b  AIN != tErrno ){
d0df0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
d0e00 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d0e10 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d0e20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
d0e30 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b  CKRESERVEDLOCK);
d0e40 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
d0e50 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d0e60 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65  no;.      } else
d0e70 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 6f   {.        /* so
d0e80 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 74  meone else has t
d0e90 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65 20  he lock when we 
d0ea0 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a  are in NO_LOCK *
d0eb0 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 72 76  /.        reserv
d0ec0 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f 63  ed = (pFile->loc
d0ed0 6b 74 79 70 65 20 3c 20 53 48 41 52 45 44 5f 4c  ktype < SHARED_L
d0ee0 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OCK);.      }.  
d0ef0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
d0f00 2a 20 77 65 20 63 6f 75 6c 64 20 68 61 76 65 20  * we could have 
d0f10 69 74 20 69 66 20 77 65 20 77 61 6e 74 20 69 74  it if we want it
d0f20 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70 6f   */.      sem_po
d0f30 73 74 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d 0a  st(pSem);.    }.
d0f40 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22    }.  OSTRACE4("
d0f50 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20  TEST WR-LOCK %d 
d0f60 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  %d %d\n", pFile-
d0f70 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64  >h, rc, reserved
d0f80 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d  );..  *pResOut =
d0f90 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74   reserved;.  ret
d0fa0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
d0fb0 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77   Lock the file w
d0fc0 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65  ith the lock spe
d0fd0 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65  cified by parame
d0fe0 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f  ter locktype - o
d0ff0 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ne.** of the fol
d1000 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
d1010 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43    (1) SHARED_LOC
d1020 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53  K.**     (2) RES
d1030 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  ERVED_LOCK.**   
d1040 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f    (3) PENDING_LO
d1050 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58  CK.**     (4) EX
d1060 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a  CLUSIVE_LOCK.**.
d1070 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65  ** Sometimes whe
d1080 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65  n requesting one
d1090 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64   lock state, add
d10a0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61  itional lock sta
d10b0 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72  tes.** are inser
d10c0 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20  ted in between. 
d10d0 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67   The locking mig
d10e0 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f  ht fail on one o
d10f0 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74  f the later.** t
d1100 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69  ransitions leavi
d1110 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74  ng the lock stat
d1120 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
d1130 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64   what it started
d1140 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68   but.** still sh
d1150 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e  ort of its goal.
d1160 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
d1170 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20  chart shows the 
d1180 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73  allowed.** trans
d1190 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69  itions and the i
d11a0 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64  nserted intermed
d11b0 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  iate states:.**.
d11c0 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d  **    UNLOCKED -
d11d0 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53  > SHARED.**    S
d11e0 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45  HARED -> RESERVE
d11f0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
d1200 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
d1210 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52  XCLUSIVE.**    R
d1220 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44  ESERVED -> (PEND
d1230 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
d1240 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20  E.**    PENDING 
d1250 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a  -> EXCLUSIVE.**.
d1260 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63  ** Semaphore loc
d1270 6b 73 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73  ks only really s
d1280 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 45  upport EXCLUSIVE
d1290 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 63   locks.  We trac
d12a0 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a  k intermediate.*
d12b0 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 6e  * lock states in
d12c0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c   the sqlite3_fil
d12d0 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75 74  e structure, but
d12e0 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45   all locks SHARE
d12f0 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72  D or.** above ar
d1300 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 49  e really EXCLUSI
d1310 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63  VE locks and exc
d1320 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 70  lude all other p
d1330 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a  rocesses from.**
d1340 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65   access the file
d1350 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
d1360 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69  tine will only i
d1370 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20  ncrease a lock. 
d1380 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33   Use the sqlite3
d1390 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f  OsUnlock().** ro
d13a0 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61  utine to lower a
d13b0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a   locking level..
d13c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
d13d0 6d 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  mLock(sqlite3_fi
d13e0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
d13f0 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69  type) {.  unixFi
d1400 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
d1410 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74  xFile*)id;.  int
d1420 20 66 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53   fd;.  sem_t *pS
d1430 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  em = pFile->pOpe
d1440 6e 2d 3e 70 53 65 6d 3b 0a 20 20 69 6e 74 20 72  n->pSem;.  int r
d1450 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
d1460 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61    /* if we alrea
d1470 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20  dy have a lock, 
d1480 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e  it is exclusive.
d1490 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a    .  ** Just adj
d14a0 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75  ust level and pu
d14b0 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65  nt on outta here
d14c0 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65  . */.  if (pFile
d14d0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f  ->locktype > NO_
d14e0 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c  LOCK) {.    pFil
d14f0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
d1500 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 63 20 3d  cktype;.    rc =
d1510 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
d1520 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63  goto sem_end_loc
d1530 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c  k;.  }.  .  /* l
d1540 6f 63 6b 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f  ock semaphore no
d1550 77 20 62 75 74 20 62 61 69 6c 20 6f 75 74 20 77  w but bail out w
d1560 68 65 6e 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b  hen already lock
d1570 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d  ed. */.  if( sem
d1580 5f 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d  _trywait(pSem)==
d1590 2d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  -1 ){.    rc = S
d15a0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
d15b0 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63  goto sem_end_loc
d15c0 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74  k;.  }..  /* got
d15d0 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70   it, set the typ
d15e0 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20  e and return ok 
d15f0 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  */.  pFile->lock
d1600 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
d1610 0a 0a 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a  .. sem_end_lock:
d1620 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
d1630 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
d1640 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
d1650 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
d1660 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74  r pFile to lockt
d1670 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a  ype.  locktype.*
d1680 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  * must be either
d1690 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52   NO_LOCK or SHAR
d16a0 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49  ED_LOCK..**.** I
d16b0 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  f the locking le
d16c0 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20  vel of the file 
d16d0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c  descriptor is al
d16e0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f  ready at or belo
d16f0 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74  w.** the request
d1700 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  ed locking level
d1710 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
d1720 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
d1730 61 74 69 63 20 69 6e 74 20 73 65 6d 55 6e 6c 6f  atic int semUnlo
d1740 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d1750 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
d1760 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  e) {.  unixFile 
d1770 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d1780 6c 65 2a 29 69 64 3b 0a 20 20 73 65 6d 5f 74 20  le*)id;.  sem_t 
d1790 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70  *pSem = pFile->p
d17a0 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61  Open->pSem;..  a
d17b0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
d17c0 20 20 61 73 73 65 72 74 28 20 70 53 65 6d 20 29    assert( pSem )
d17d0 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e  ;.  OSTRACE5("UN
d17e0 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20  LOCK  %d %d was 
d17f0 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  %d pid=%d\n", pF
d1800 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
d1810 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  ,..   pFile->loc
d1820 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 29 29  ktype, getpid())
d1830 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
d1840 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43  type<=SHARED_LOC
d1850 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d  K );.  .  /* no-
d1860 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a  op if possible *
d1870 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
d1880 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70  ocktype==locktyp
d1890 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
d18a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
d18b0 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61   .  /* shared ca
d18c0 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65  n just be set be
d18d0 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20  cause we always 
d18e0 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76  have an exclusiv
d18f0 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74  e */.  if (lockt
d1900 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d1910 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ) {.    pFile->l
d1920 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d1930 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  pe;.    return S
d1940 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
d1950 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79  .  /* no, really
d1960 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66   unlock. */.  if
d1970 20 28 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d   ( sem_post(pSem
d1980 29 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e  )==-1 ) {.    in
d1990 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65  t rc, tErrno = e
d19a0 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 73  rrno;.    rc = s
d19b0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d19c0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d19d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
d19e0 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49  LOCK);.    if( I
d19f0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d1a00 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   ){.      pFile-
d1a10 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d1a20 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  rno;.    }.    r
d1a30 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20  eturn rc; .  }. 
d1a40 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d1a50 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65   = NO_LOCK;.  re
d1a60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d1a70 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20  }../*. ** Close 
d1a80 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74  a file.. */.stat
d1a90 69 63 20 69 6e 74 20 73 65 6d 43 6c 6f 73 65 28  ic int semClose(
d1aa0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d1ab0 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a  ) {.  if( id ){.
d1ac0 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46      unixFile *pF
d1ad0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
d1ae0 29 69 64 3b 0a 20 20 20 20 73 65 6d 55 6e 6c 6f  )id;.    semUnlo
d1af0 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  ck(id, NO_LOCK);
d1b00 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69  .    assert( pFi
d1b10 6c 65 20 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e  le );.    unixEn
d1b20 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  terMutex();.    
d1b30 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28  releaseLockInfo(
d1b40 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20  pFile->pLock);. 
d1b50 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e     releaseOpenCn
d1b60 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b  t(pFile->pOpen);
d1b70 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  .    unixLeaveMu
d1b80 74 65 78 28 29 3b 0a 20 20 20 20 63 6c 6f 73 65  tex();.    close
d1b90 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20  UnixFile(id);.  
d1ba0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
d1bb0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  E_OK;.}..#endif 
d1bc0 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f  /* OS_VXWORKS */
d1bd0 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d  ./*.** Named sem
d1be0 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69  aphore locking i
d1bf0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
d1c00 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a   on VxWorks..**.
d1c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
d1c20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61 6d 65 64  End of the named
d1c30 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 20   semaphore lock 
d1c40 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
d1c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
d1c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
d1cb0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
d1cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1d00 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
d1d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
d1d20 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69 6e 67 20  gin AFP Locking 
d1d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1d50 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69 73 20 74  *.**.** AFP is t
d1d60 68 65 20 41 70 70 6c 65 20 46 69 6c 69 6e 67 20  he Apple Filing 
d1d70 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46 50 20 69  Protocol.  AFP i
d1d80 73 20 61 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65  s a network file
d1d90 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20  system found.** 
d1da0 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69 6e 74 6f  on Apple Macinto
d1db0 73 68 20 63 6f 6d 70 75 74 65 72 73 20 2d 20 62  sh computers - b
d1dc0 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f 53 58 2e  oth OS9 and OSX.
d1dd0 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d 70 61 72  .**.** Third-par
d1de0 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ty implementatio
d1df0 6e 73 20 6f 66 20 41 46 50 20 61 72 65 20 61 76  ns of AFP are av
d1e00 61 69 6c 61 62 6c 65 2e 20 20 42 75 74 20 74 68  ailable.  But th
d1e10 69 73 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20  is code here.** 
d1e20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53  only works on OS
d1e30 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e  X..*/..#if defin
d1e40 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
d1e50 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
d1e60 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a  OCKING_STYLE./*.
d1e70 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e  ** The afpLockin
d1e80 67 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75  gContext structu
d1e90 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20  re contains all 
d1ea0 61 66 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  afp lock specifi
d1eb0 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79 70 65 64  c state.*/.typed
d1ec0 65 66 20 73 74 72 75 63 74 20 61 66 70 4c 6f 63  ef struct afpLoc
d1ed0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 66 70 4c  kingContext afpL
d1ee0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73  ockingContext;.s
d1ef0 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67  truct afpLocking
d1f00 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73 69  Context {.  unsi
d1f10 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73  gned long long s
d1f20 68 61 72 65 64 42 79 74 65 3b 0a 20 20 63 6f 6e  haredByte;.  con
d1f30 73 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b  st char *dbPath;
d1f40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d1f50 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e  Name of the open
d1f60 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72   file */.};..str
d1f70 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63  uct ByteRangeLoc
d1f80 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65  kPB2.{.  unsigne
d1f90 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73  d long long offs
d1fa0 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 6f 66  et;        /* of
d1fb0 66 73 65 74 20 74 6f 20 66 69 72 73 74 20 62 79  fset to first by
d1fc0 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20  te to lock */.  
d1fd0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
d1fe0 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 20  ng length;      
d1ff0 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62 79 74 65    /* nbr of byte
d2000 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75  s to lock */.  u
d2010 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
d2020 67 20 72 65 74 52 61 6e 67 65 53 74 61 72 74 3b  g retRangeStart;
d2030 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73 74 20 62   /* nbr of 1st b
d2040 79 74 65 20 6c 6f 63 6b 65 64 20 69 66 20 73 75  yte locked if su
d2050 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e  ccessful */.  un
d2060 73 69 67 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f  signed char unLo
d2070 63 6b 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20  ckFlag;         
d2080 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30  /* 1 = unlock, 0
d2090 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73   = lock */.  uns
d20a0 69 67 6e 65 64 20 63 68 61 72 20 73 74 61 72 74  igned char start
d20b0 45 6e 64 46 6c 61 67 3b 20 20 20 20 20 20 20 2f  EndFlag;       /
d20c0 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f  * 1=rel to end o
d20d0 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f  f fork, 0=rel to
d20e0 20 73 74 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20   start */.  int 
d20f0 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fd;             
d2100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d2110 20 66 69 6c 65 20 64 65 73 63 20 74 6f 20 61 73   file desc to as
d2120 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b 20 77 69  soc this lock wi
d2130 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e  th */.};..#defin
d2140 65 20 61 66 70 66 73 42 79 74 65 52 61 6e 67 65  e afpfsByteRange
d2150 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20 20 20 20  Lock2FSCTL      
d2160 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c    _IOWR('z', 23,
d2170 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67   struct ByteRang
d2180 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a  eLockPB2)../*.**
d2190 20 54 68 69 73 20 69 73 20 61 20 75 74 69 6c 69   This is a utili
d21a0 74 79 20 66 6f 72 20 73 65 74 74 69 6e 67 20 6f  ty for setting o
d21b0 72 20 63 6c 65 61 72 69 6e 67 20 61 20 62 69 74  r clearing a bit
d21c0 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 61  -range lock on a
d21d0 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65 73 79 73  n.** AFP filesys
d21e0 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75  tem..** .** Retu
d21f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
d2200 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f  success, SQLITE_
d2210 42 55 53 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e  BUSY on failure.
d2220 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
d2230 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e  fpSetLock(.  con
d2240 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 20  st char *path,  
d2250 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
d2260 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ame of the file 
d2270 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 6f 72 20  to be locked or 
d2280 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e  unlocked */.  un
d2290 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20  ixFile *pFile,  
d22a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d22b0 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69  Open file descri
d22c0 70 74 6f 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0a  ptor on path */.
d22d0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
d22e0 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 20 20 20  long offset,    
d22f0 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 74   /* First byte t
d2300 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20  o be locked */. 
d2310 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
d2320 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20 20 20 20  ong length,     
d2330 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
d2340 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20  es to lock */.  
d2350 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20  int setLockFlag 
d2360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d2370 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20 6c 6f  * True to set lo
d2380 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f 20 63 6c  ck.  False to cl
d2390 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20  ear lock */.){. 
d23a0 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67   struct ByteRang
d23b0 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a 20 20 69  eLockPB2 pb;.  i
d23c0 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e  nt err;.  .  pb.
d23d0 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74  unLockFlag = set
d23e0 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31  LockFlag ? 0 : 1
d23f0 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 6e 64 46  ;.  pb.startEndF
d2400 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66  lag = 0;.  pb.of
d2410 66 73 65 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20  fset = offset;. 
d2420 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e   pb.length = len
d2430 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20  gth; .  pb.fd = 
d2440 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f  pFile->h;.  .  O
d2450 53 54 52 41 43 45 36 28 22 41 46 50 53 45 54 4c  STRACE6("AFPSETL
d2460 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20 25 64 25  OCK [%s] for %d%
d2470 73 20 69 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a  s in range %llx:
d2480 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73  %llx\n", .    (s
d2490 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a  etLockFlag?"ON":
d24a0 22 4f 46 46 22 29 2c 20 70 46 69 6c 65 2d 3e 68  "OFF"), pFile->h
d24b0 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74  , (pb.fd==-1?"[t
d24c0 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a  estval-1]":""),.
d24d0 20 20 20 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67      offset, leng
d24e0 74 68 29 3b 0a 20 20 65 72 72 20 3d 20 66 73 63  th);.  err = fsc
d24f0 74 6c 28 70 61 74 68 2c 20 61 66 70 66 73 42 79  tl(path, afpfsBy
d2500 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54  teRangeLock2FSCT
d2510 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20 20 69 66  L, &pb, 0);.  if
d2520 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 7b 0a 20   ( err==-1 ) {. 
d2530 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69     int rc;.    i
d2540 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
d2550 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28  o;.    OSTRACE4(
d2560 22 41 46 50 53 45 54 4c 4f 43 4b 20 66 61 69 6c  "AFPSETLOCK fail
d2570 65 64 20 74 6f 20 66 73 63 74 6c 28 29 20 27 25  ed to fsctl() '%
d2580 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20  s' %d %s\n",.   
d2590 20 20 20 20 20 20 20 20 20 20 70 61 74 68 2c 20            path, 
d25a0 74 45 72 72 6e 6f 2c 20 73 74 72 65 72 72 6f 72  tErrno, strerror
d25b0 28 74 45 72 72 6e 6f 29 29 3b 0a 23 69 66 64 65  (tErrno));.#ifde
d25c0 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f  f SQLITE_IGNORE_
d25d0 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a  AFP_LOCK_ERRORS.
d25e0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
d25f0 42 55 53 59 3b 0a 23 65 6c 73 65 0a 20 20 20 20  BUSY;.#else.    
d2600 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
d2610 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
d2620 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20  Errno,.         
d2630 20 20 20 20 20 20 20 20 20 20 20 73 65 74 4c 6f             setLo
d2640 63 6b 46 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f  ckFlag ? SQLITE_
d2650 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c  IOERR_LOCK : SQL
d2660 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
d2670 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  );.#endif /* SQL
d2680 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c  ITE_IGNORE_AFP_L
d2690 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20  OCK_ERRORS */.  
d26a0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d26b0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
d26c0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d26d0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d26e0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  }.    return rc;
d26f0 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
d2700 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d2710 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
d2720 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
d2730 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ks if there is a
d2740 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68   RESERVED lock h
d2750 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69  eld on the speci
d2760 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20  fied.** file by 
d2770 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65  this or any othe
d2780 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75  r process. If su
d2790 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c  ch a lock is hel
d27a0 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a  d, set *pResOut.
d27b0 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  ** to a non-zero
d27c0 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65   value otherwise
d27d0 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74   *pResOut is set
d27e0 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72   to zero.  The r
d27f0 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69  eturn value.** i
d2800 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  s set to SQLITE_
d2810 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f  OK unless an I/O
d2820 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
d2830 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69  ring lock checki
d2840 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
d2850 74 20 61 66 70 43 68 65 63 6b 52 65 73 65 72 76  t afpCheckReserv
d2860 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
d2870 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
d2880 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63  esOut){.  int rc
d2890 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
d28a0 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30  int reserved = 0
d28b0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
d28c0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
d28d0 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61  )id;.  .  Simula
d28e0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
d28f0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  n SQLITE_IOERR_C
d2900 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b  HECKRESERVEDLOCK
d2910 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  ; );.  .  assert
d2920 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 66 70  ( pFile );.  afp
d2930 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
d2940 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f  context = (afpLo
d2950 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20  ckingContext *) 
d2960 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
d2970 6e 74 65 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43  ntext;.  .  /* C
d2980 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64  heck if a thread
d2990 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73   in this process
d29a0 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f   holds such a lo
d29b0 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  ck */.  if( pFil
d29c0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52  e->locktype>SHAR
d29d0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  ED_LOCK ){.    r
d29e0 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d  eserved = 1;.  }
d29f0 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69  .  .  /* Otherwi
d2a00 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f  se see if some o
d2a10 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c  ther process hol
d2a20 64 73 20 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69  ds it..   */.  i
d2a30 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a  f( !reserved ){.
d2a40 20 20 20 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20      /* lock the 
d2a50 52 45 53 45 52 56 45 44 20 62 79 74 65 20 2a 2f  RESERVED byte */
d2a60 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 3d 20 61  .    int lrc = a
d2a70 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  fpSetLock(contex
d2a80 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  t->dbPath, pFile
d2a90 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c  , RESERVED_BYTE,
d2aa0 20 31 2c 31 29 3b 20 20 0a 20 20 20 20 69 66 28   1,1);  .    if(
d2ab0 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20   SQLITE_OK==lrc 
d2ac0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77  ){.      /* if w
d2ad0 65 20 73 75 63 63 65 65 64 65 64 20 69 6e 20 74  e succeeded in t
d2ae0 61 6b 69 6e 67 20 74 68 65 20 72 65 73 65 72 76  aking the reserv
d2af0 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20  ed lock, unlock 
d2b00 69 74 20 74 6f 20 72 65 73 74 6f 72 65 0a 20 20  it to restore.  
d2b10 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 69 67 69      ** the origi
d2b20 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a 20 20 20  nal state */.   
d2b30 20 20 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c     lrc = afpSetL
d2b40 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
d2b50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45  ath, pFile, RESE
d2b60 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 29  RVED_BYTE, 1, 0)
d2b70 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20  ;.    } else {. 
d2b80 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 66 61       /* if we fa
d2b90 69 6c 65 64 20 74 6f 20 67 65 74 20 74 68 65 20  iled to get the 
d2ba0 6c 6f 63 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e  lock then someon
d2bb0 65 20 65 6c 73 65 20 6d 75 73 74 20 68 61 76 65  e else must have
d2bc0 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 73   it */.      res
d2bd0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d  erved = 1;.    }
d2be0 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b  .    if( IS_LOCK
d2bf0 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20  _ERROR(lrc) ){. 
d2c00 20 20 20 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20       rc=lrc;.   
d2c10 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52   }.  }.  .  OSTR
d2c20 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f  ACE4("TEST WR-LO
d2c30 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20  CK %d %d %d\n", 
d2c40 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65  pFile->h, rc, re
d2c50 73 65 72 76 65 64 29 3b 0a 20 20 0a 20 20 2a 70  served);.  .  *p
d2c60 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65  ResOut = reserve
d2c70 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  d;.  return rc;.
d2c80 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
d2c90 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
d2ca0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
d2cb0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
d2cc0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
d2cd0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
d2ce0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
d2cf0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
d2d00 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
d2d10 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
d2d20 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
d2d30 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
d2d40 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
d2d50 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
d2d60 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
d2d70 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
d2d80 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
d2d90 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
d2da0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
d2db0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
d2dc0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
d2dd0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
d2de0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
d2df0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
d2e00 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
d2e10 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
d2e20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
d2e30 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
d2e40 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
d2e50 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
d2e60 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
d2e70 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
d2e80 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
d2e90 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
d2ea0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
d2eb0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
d2ec0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
d2ed0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
d2ee0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
d2ef0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
d2f00 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
d2f10 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
d2f20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
d2f30 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
d2f40 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
d2f50 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
d2f60 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69  k.  Use the sqli
d2f70 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a  te3OsUnlock().**
d2f80 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65   routine to lowe
d2f90 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  r a locking leve
d2fa0 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
d2fb0 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69 74 65 33   afpLock(sqlite3
d2fc0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
d2fd0 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20  ocktype){.  int 
d2fe0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d2ff0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d3000 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d3010 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43  d;.  afpLockingC
d3020 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20  ontext *context 
d3030 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  = (afpLockingCon
d3040 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c  text *) pFile->l
d3050 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
d3060 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c   .  assert( pFil
d3070 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28  e );.  OSTRACE5(
d3080 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77  "LOCK    %d %s w
d3090 61 73 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c  as %s pid=%d\n",
d30a0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20   pFile->h,.     
d30b0 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65      locktypeName
d30c0 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b  (locktype), lock
d30d0 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e  typeName(pFile->
d30e0 6c 6f 63 6b 74 79 70 65 29 2c 20 67 65 74 70 69  locktype), getpi
d30f0 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  d());..  /* If t
d3100 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
d3110 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74  a lock of this t
d3120 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74  ype or more rest
d3130 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20  rictive on the. 
d3140 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f   ** unixFile, do
d3150 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20   nothing. Don't 
d3160 75 73 65 20 74 68 65 20 61 66 70 5f 65 6e 64 5f  use the afp_end_
d3170 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c  lock: exit path,
d3180 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74   as.  ** unixEnt
d3190 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74  erMutex() hasn't
d31a0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74   been called yet
d31b0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ..  */.  if( pFi
d31c0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f  le->locktype>=lo
d31d0 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53  cktype ){.    OS
d31e0 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20  TRACE3("LOCK    
d31f0 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64  %d %s ok (alread
d3200 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c  y held)\n", pFil
d3210 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20  e->h,.          
d3220 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f   locktypeName(lo
d3230 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65  cktype));.    re
d3240 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d3250 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
d3260 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ure the locking 
d3270 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72  sequence is corr
d3280 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ect.  */.  asser
d3290 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
d32a0 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c  pe!=NO_LOCK || l
d32b0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
d32c0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
d32d0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44  ( locktype!=PEND
d32e0 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  ING_LOCK );.  as
d32f0 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d  sert( locktype!=
d3300 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c  RESERVED_LOCK ||
d3310 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d3320 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
d3330 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75  .  .  /* This mu
d3340 74 65 78 20 69 73 20 6e 65 65 64 65 64 20 62 65  tex is needed be
d3350 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f  cause pFile->pLo
d3360 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72  ck is shared acr
d3370 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f  oss threads.  */
d3380 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65  .  unixEnterMute
d3390 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  x();..  /* Make 
d33a0 73 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74  sure the current
d33b0 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65   thread owns the
d33c0 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72   pFile..  */.  r
d33d0 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65  c = transferOwne
d33e0 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20  rship(pFile);.  
d33f0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
d3400 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61  K ){.    unixLea
d3410 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72  veMutex();.    r
d3420 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
d3430 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e    .  /* A PENDIN
d3440 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64  G lock is needed
d3450 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e   before acquirin
d3460 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20  g a SHARED lock 
d3470 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20  and before.  ** 
d3480 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43  acquiring an EXC
d3490 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f  LUSIVE lock.  Fo
d34a0 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63  r the SHARED loc
d34b0 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77  k, the PENDING w
d34c0 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65  ill.  ** be rele
d34d0 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ased..  */.  if(
d34e0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
d34f0 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c  D_LOCK .      ||
d3500 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c   (locktype==EXCL
d3510 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46  USIVE_LOCK && pF
d3520 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45  ile->locktype<PE
d3530 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b  NDING_LOCK).  ){
d3540 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 3b  .    int failed;
d3550 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66  .    failed = af
d3560 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  pSetLock(context
d3570 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
d3580 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31   PENDING_BYTE, 1
d3590 2c 20 31 29 3b 0a 20 20 20 20 69 66 20 28 66 61  , 1);.    if (fa
d35a0 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63  iled) {.      rc
d35b0 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20   = failed;.     
d35c0 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f   goto afp_end_lo
d35d0 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ck;.    }.  }.  
d35e0 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c  .  /* If control
d35f0 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f   gets to this po
d3600 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c  int, then actual
d3610 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20  ly go ahead and 
d3620 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74  make.  ** operat
d3630 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73  ing system calls
d3640 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
d3650 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  ed lock..  */.  
d3660 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
d3670 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d3680 20 69 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c   int lk, lrc1, l
d3690 72 63 32 2c 20 6c 72 63 31 45 72 72 6e 6f 3b 0a  rc2, lrc1Errno;.
d36a0 20 20 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20      .    /* Now 
d36b0 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63  get the read-loc
d36c0 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f  k SHARED_LOCK */
d36d0 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61  .    /* note tha
d36e0 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66  t the quality of
d36f0 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20   the randomness 
d3700 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74  doesn't matter t
d3710 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20  hat much */.    
d3720 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a  lk = random(); .
d3730 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61      context->sha
d3740 72 65 64 42 79 74 65 20 3d 20 28 6c 6b 20 26 20  redByte = (lk & 
d3750 30 78 37 66 66 66 66 66 66 66 29 25 28 53 48 41  0x7fffffff)%(SHA
d3760 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 3b 0a 20  RED_SIZE - 1);. 
d3770 20 20 20 6c 72 63 31 20 3d 20 61 66 70 53 65 74     lrc1 = afpSet
d3780 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
d3790 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20  Path, pFile, .  
d37a0 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46          SHARED_F
d37b0 49 52 53 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68  IRST+context->sh
d37c0 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 3b  aredByte, 1, 1);
d37d0 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b  .    if( IS_LOCK
d37e0 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a  _ERROR(lrc1) ){.
d37f0 20 20 20 20 20 20 6c 72 63 31 45 72 72 6e 6f 20        lrc1Errno 
d3800 3d 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72  = pFile->lastErr
d3810 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a  no;.    }.    /*
d3820 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f 72   Drop the tempor
d3830 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  ary PENDING lock
d3840 20 2a 2f 0a 20 20 20 20 6c 72 63 32 20 3d 20 61   */.    lrc2 = a
d3850 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  fpSetLock(contex
d3860 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  t->dbPath, pFile
d3870 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
d3880 31 2c 20 30 29 3b 0a 20 20 20 20 0a 20 20 20 20  1, 0);.    .    
d3890 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
d38a0 52 28 6c 72 63 31 29 20 29 20 7b 0a 20 20 20 20  R(lrc1) ) {.    
d38b0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d38c0 6e 6f 20 3d 20 6c 72 63 31 45 72 72 6e 6f 3b 0a  no = lrc1Errno;.
d38d0 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b        rc = lrc1;
d38e0 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f  .      goto afp_
d38f0 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20  end_lock;.    } 
d3900 65 6c 73 65 20 69 66 28 20 49 53 5f 4c 4f 43 4b  else if( IS_LOCK
d3910 5f 45 52 52 4f 52 28 6c 72 63 32 29 20 29 7b 0a  _ERROR(lrc2) ){.
d3920 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 32 3b        rc = lrc2;
d3930 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f  .      goto afp_
d3940 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20  end_lock;.    } 
d3950 65 6c 73 65 20 69 66 28 20 6c 72 63 31 20 21 3d  else if( lrc1 !=
d3960 20 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20   SQLITE_OK ) {. 
d3970 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a       rc = lrc1;.
d3980 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
d3990 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d39a0 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b  pe = SHARED_LOCK
d39b0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70  ;.      pFile->p
d39c0 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20  Open->nLock++;. 
d39d0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
d39e0 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74    /* The request
d39f0 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52   was for a RESER
d3a00 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45  VED or EXCLUSIVE
d3a10 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20   lock.  It is.  
d3a20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61    ** assumed tha
d3a30 74 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41  t there is a SHA
d3a40 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c  RED or greater l
d3a50 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a  ock on the file.
d3a60 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a      ** already..
d3a70 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66      */.    int f
d3a80 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 61  ailed = 0;.    a
d3a90 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d  ssert( 0!=pFile-
d3aa0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20  >locktype );.   
d3ab0 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 20 3e 3d   if (locktype >=
d3ac0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26   RESERVED_LOCK &
d3ad0 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  & pFile->locktyp
d3ae0 65 20 3c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  e < RESERVED_LOC
d3af0 4b 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  K) {.        /* 
d3b00 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56  Acquire a RESERV
d3b10 45 44 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20  ED lock */.     
d3b20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53     failed = afpS
d3b30 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
d3b40 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52  dbPath, pFile, R
d3b50 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c  ESERVED_BYTE, 1,
d3b60 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  1);.    }.    if
d3b70 20 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f 63   (!failed && loc
d3b80 6b 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53 49  ktype == EXCLUSI
d3b90 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20  VE_LOCK) {.     
d3ba0 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45   /* Acquire an E
d3bb0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f  XCLUSIVE lock */
d3bc0 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20  .        .      
d3bd0 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 68  /* Remove the sh
d3be0 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65  ared lock before
d3bf0 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e 67   trying the rang
d3c00 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20 74  e.  we'll need t
d3c10 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65 73  o .      ** rees
d3c20 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72  tablish the shar
d3c30 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63 61  ed lock if we ca
d3c40 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66 70  n't get the  afp
d3c50 55 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a  Unlock.      */.
d3c60 20 20 20 20 20 20 69 66 28 20 21 28 66 61 69 6c        if( !(fail
d3c70 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28  ed = afpSetLock(
d3c80 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c  context->dbPath,
d3c90 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46   pFile, SHARED_F
d3ca0 49 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20 20  IRST +.         
d3cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d3cc0 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42  context->sharedB
d3cd0 79 74 65 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20  yte, 1, 0)) ){. 
d3ce0 20 20 20 20 20 20 20 69 6e 74 20 66 61 69 6c 65         int faile
d3cf0 64 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  d2 = SQLITE_OK;.
d3d00 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61          /* now a
d3d10 74 74 65 6d 6d 70 74 20 74 6f 20 67 65 74 20 74  ttemmpt to get t
d3d20 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  he exclusive loc
d3d30 6b 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20  k range */.     
d3d40 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53     failed = afpS
d3d50 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
d3d60 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53  dbPath, pFile, S
d3d70 48 41 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20  HARED_FIRST, .  
d3d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d3d90 20 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41               SHA
d3da0 52 45 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20  RED_SIZE, 1);.  
d3db0 20 20 20 20 20 20 69 66 28 20 66 61 69 6c 65 64        if( failed
d3dc0 20 26 26 20 28 66 61 69 6c 65 64 32 20 3d 20 61   && (failed2 = a
d3dd0 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  fpSetLock(contex
d3de0 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  t->dbPath, pFile
d3df0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
d3e00 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44            SHARED
d3e10 5f 46 49 52 53 54 20 2b 20 63 6f 6e 74 65 78 74  _FIRST + context
d3e20 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c  ->sharedByte, 1,
d3e30 20 31 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20   1)) ){.        
d3e40 20 20 2f 2a 20 43 61 6e 27 74 20 72 65 65 73 74    /* Can't reest
d3e50 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 65  ablish the share
d3e60 64 20 6c 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20  d lock.  Sqlite 
d3e70 63 61 6e 27 74 20 64 65 61 6c 2c 20 74 68 69 73  can't deal, this
d3e80 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a   is.          **
d3e90 20 61 20 63 72 69 74 69 63 61 6c 20 49 2f 4f 20   a critical I/O 
d3ea0 65 72 72 6f 72 0a 20 20 20 20 20 20 20 20 20 20  error.          
d3eb0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  */.          rc 
d3ec0 3d 20 28 28 66 61 69 6c 65 64 20 26 20 53 51 4c  = ((failed & SQL
d3ed0 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51  ITE_IOERR) == SQ
d3ee0 4c 49 54 45 5f 49 4f 45 52 52 29 20 3f 20 66 61  LITE_IOERR) ? fa
d3ef0 69 6c 65 64 32 20 3a 20 0a 20 20 20 20 20 20 20  iled2 : .       
d3f00 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49          SQLITE_I
d3f10 4f 45 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20  OERR_LOCK;.     
d3f20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e       goto afp_en
d3f30 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20  d_lock;.        
d3f40 7d 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  } .      }else{.
d3f50 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 69          rc = fai
d3f60 6c 65 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20  led; .      }.  
d3f70 20 20 7d 0a 20 20 20 20 69 66 28 20 66 61 69 6c    }.    if( fail
d3f80 65 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ed ){.      rc =
d3f90 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20   failed;.    }. 
d3fa0 20 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d   }.  .  if( rc==
d3fb0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
d3fc0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d3fd0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d   = locktype;.  }
d3fe0 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70  else if( locktyp
d3ff0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
d4000 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
d4010 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49  locktype = PENDI
d4020 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a  NG_LOCK;.  }.  .
d4030 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20  afp_end_lock:.  
d4040 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
d4050 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f  ;.  OSTRACE4("LO
d4060 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e  CK    %d %s %s\n
d4070 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63  ", pFile->h, loc
d4080 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79  ktypeName(lockty
d4090 70 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20 72  pe), .         r
d40a0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22  c==SQLITE_OK ? "
d40b0 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b  ok" : "failed");
d40c0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
d40d0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
d40e0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
d40f0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
d4100 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74  r pFile to lockt
d4110 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a  ype.  locktype.*
d4120 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  * must be either
d4130 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52   NO_LOCK or SHAR
d4140 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49  ED_LOCK..**.** I
d4150 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  f the locking le
d4160 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20  vel of the file 
d4170 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c  descriptor is al
d4180 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f  ready at or belo
d4190 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74  w.** the request
d41a0 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  ed locking level
d41b0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
d41c0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
d41d0 61 74 69 63 20 69 6e 74 20 61 66 70 55 6e 6c 6f  atic int afpUnlo
d41e0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d41f0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
d4200 65 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  e) {.  int rc = 
d4210 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69  SQLITE_OK;.  uni
d4220 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
d4230 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
d4240 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
d4250 74 20 2a 70 43 74 78 20 3d 20 28 61 66 70 4c 6f  t *pCtx = (afpLo
d4260 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20  ckingContext *) 
d4270 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
d4280 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74  ntext;..  assert
d4290 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54  ( pFile );.  OST
d42a0 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25  RACE5("UNLOCK  %
d42b0 64 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d  d %d was %d pid=
d42c0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
d42d0 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20   locktype,.     
d42e0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d42f0 79 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a  ype, getpid());.
d4300 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
d4310 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
d4320 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   );.  if( pFile-
d4330 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74  >locktype<=lockt
d4340 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ype ){.    retur
d4350 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d4360 0a 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52  .  if( CHECK_THR
d4370 45 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a  EADID(pFile) ){.
d4380 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d4390 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
d43a0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
d43b0 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  ;.  if( pFile->l
d43c0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
d43d0 4f 43 4b 20 29 7b 0a 20 20 20 20 0a 20 20 20 20  OCK ){.    .    
d43e0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
d43f0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
d4400 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  OCK ){.      rc 
d4410 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74  = afpSetLock(pCt
d4420 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  x->dbPath, pFile
d4430 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20  , SHARED_FIRST, 
d4440 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b  SHARED_SIZE, 0);
d4450 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
d4460 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b  QLITE_OK && lock
d4470 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
d4480 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  K ){.        /* 
d4490 6f 6e 6c 79 20 72 65 2d 65 73 74 61 62 6c 69 73  only re-establis
d44a0 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  h the shared loc
d44b0 6b 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a  k if necessary *
d44c0 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 68  /.        int sh
d44d0 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53  aredLockByte = S
d44e0 48 41 52 45 44 5f 46 49 52 53 54 2b 70 43 74 78  HARED_FIRST+pCtx
d44f0 2d 3e 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20  ->sharedByte;.  
d4500 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65        rc = afpSe
d4510 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61  tLock(pCtx->dbPa
d4520 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 72 65  th, pFile, share
d4530 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 31 29  dLockByte, 1, 1)
d4540 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
d4550 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
d4560 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e  TE_OK && pFile->
d4570 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e  locktype>=PENDIN
d4580 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20  G_LOCK ){.      
d4590 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28  rc = afpSetLock(
d45a0 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46  pCtx->dbPath, pF
d45b0 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54  ile, PENDING_BYT
d45c0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20  E, 1, 0);.    } 
d45d0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
d45e0 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d  ITE_OK && pFile-
d45f0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52  >locktype>=RESER
d4600 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  VED_LOCK ){.    
d4610 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63    rc = afpSetLoc
d4620 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20  k(pCtx->dbPath, 
d4630 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f  pFile, RESERVED_
d4640 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20  BYTE, 1, 0);.   
d4650 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c   }.  }else if( l
d4660 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
d4670 20 29 7b 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72   ){.    /* clear
d4680 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b   the shared lock
d4690 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 68 61 72   */.    int shar
d46a0 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41  edLockByte = SHA
d46b0 52 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e  RED_FIRST+pCtx->
d46c0 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20  sharedByte;.    
d46d0 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28  rc = afpSetLock(
d46e0 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46  pCtx->dbPath, pF
d46f0 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42  ile, sharedLockB
d4700 79 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a  yte, 1, 0);.  }.
d4710 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
d4720 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
d4730 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
d4740 4b 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63  K ){.      struc
d4750 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70  t unixOpenCnt *p
d4760 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f  Open = pFile->pO
d4770 70 65 6e 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e  pen;.      pOpen
d4780 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20  ->nLock--;.     
d4790 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e   assert( pOpen->
d47a0 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20  nLock>=0 );.    
d47b0 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f    if( pOpen->nLo
d47c0 63 6b 3d 3d 30 20 26 26 20 70 4f 70 65 6e 2d 3e  ck==0 && pOpen->
d47d0 6e 50 65 6e 64 69 6e 67 3e 30 20 29 7b 0a 20 20  nPending>0 ){.  
d47e0 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
d47f0 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
d4800 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3b  pOpen->nPending;
d4810 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
d4820 20 69 66 28 20 70 4f 70 65 6e 2d 3e 61 50 65 6e   if( pOpen->aPen
d4830 64 69 6e 67 5b 69 5d 20 3c 20 30 20 29 20 63 6f  ding[i] < 0 ) co
d4840 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
d4850 20 20 69 66 28 20 63 6c 6f 73 65 28 70 4f 70 65    if( close(pOpe
d4860 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 29 20  n->aPending[i]) 
d4870 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
d4880 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d4890 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
d48a0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
d48b0 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20  _IOERR_CLOSE;.  
d48c0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
d48d0 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e             pOpen
d48e0 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20  ->aPending[i] = 
d48f0 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  -1;.          }.
d4900 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
d4910 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
d4920 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
d4930 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f   sqlite3_free(pO
d4940 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 29 3b 0a  pen->aPending);.
d4950 20 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d            pOpen-
d4960 3e 6e 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20  >nPending = 0;. 
d4970 20 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e           pOpen->
d4980 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20  aPending = 0;.  
d4990 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
d49a0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78      }.  }.  unix
d49b0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
d49c0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
d49d0 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  K ) pFile->lockt
d49e0 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
d49f0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
d4a00 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
d4a10 6c 65 20 26 20 63 6c 65 61 6e 75 70 20 41 46 50  le & cleanup AFP
d4a20 20 73 70 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e   specific lockin
d4a30 67 20 63 6f 6e 74 65 78 74 20 0a 2a 2f 0a 73 74  g context .*/.st
d4a40 61 74 69 63 20 69 6e 74 20 61 66 70 43 6c 6f 73  atic int afpClos
d4a50 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
d4a60 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29  id) {.  if( id )
d4a70 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a  {.    unixFile *
d4a80 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d4a90 65 2a 29 69 64 3b 0a 20 20 20 20 61 66 70 55 6e  e*)id;.    afpUn
d4aa0 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b  lock(id, NO_LOCK
d4ab0 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72  );.    unixEnter
d4ac0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28  Mutex();.    if(
d4ad0 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26   pFile->pOpen &&
d4ae0 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e   pFile->pOpen->n
d4af0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Lock ){.      /*
d4b00 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75   If there are ou
d4b10 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c  tstanding locks,
d4b20 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79   do not actually
d4b30 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20   close the file 
d4b40 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 65  just.      ** ye
d4b50 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20 77  t because that w
d4b60 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 65  ould clear those
d4b70 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 64   locks.  Instead
d4b80 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a 20  , add the file. 
d4b90 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 74       ** descript
d4ba0 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 61 50 65  or to pOpen->aPe
d4bb0 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c 6c 20  nding.  It will 
d4bc0 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  be automatically
d4bd0 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a 20 20 20   closed when.   
d4be0 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 6c     ** the last l
d4bf0 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e 0a  ock is cleared..
d4c00 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
d4c10 6e 74 20 2a 61 4e 65 77 3b 0a 20 20 20 20 20 20  nt *aNew;.      
d4c20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
d4c30 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c  nt *pOpen = pFil
d4c40 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20  e->pOpen;.      
d4c50 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  aNew = sqlite3_r
d4c60 65 61 6c 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61 50  ealloc(pOpen->aP
d4c70 65 6e 64 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e  ending, (pOpen->
d4c80 6e 50 65 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65  nPending+1)*size
d4c90 6f 66 28 69 6e 74 29 20 29 3b 0a 20 20 20 20 20  of(int) );.     
d4ca0 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a   if( aNew==0 ){.
d4cb0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20          /* If a 
d4cc0 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 6a 75  malloc fails, ju
d4cd0 73 74 20 6c 65 61 6b 20 74 68 65 20 66 69 6c 65  st leak the file
d4ce0 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
d4cf0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
d4d00 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64      pOpen->aPend
d4d10 69 6e 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20  ing = aNew;.    
d4d20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64      pOpen->aPend
d4d30 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64  ing[pOpen->nPend
d4d40 69 6e 67 5d 20 3d 20 70 46 69 6c 65 2d 3e 68 3b  ing] = pFile->h;
d4d50 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  .        pOpen->
d4d60 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20 20  nPending++;.    
d4d70 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d      pFile->h = -
d4d80 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
d4d90 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e  .    releaseOpen
d4da0 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e  Cnt(pFile->pOpen
d4db0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
d4dc0 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  ree(pFile->locki
d4dd0 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  ngContext);.    
d4de0 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64  closeUnixFile(id
d4df0 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65  );.    unixLeave
d4e00 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72  Mutex();.  }.  r
d4e10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d4e20 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  .}..#endif /* de
d4e30 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
d4e40 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
d4e50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
d4e60 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  */./*.** The cod
d4e70 65 20 61 62 6f 76 65 20 69 73 20 74 68 65 20 41  e above is the A
d4e80 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  FP lock implemen
d4e90 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64  tation.  The cod
d4ea0 65 20 69 73 20 73 70 65 63 69 66 69 63 0a 2a 2a  e is specific.**
d4eb0 20 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 20 64   to MacOSX and d
d4ec0 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20  oes not work on 
d4ed0 6f 74 68 65 72 20 75 6e 69 78 20 70 6c 61 74 66  other unix platf
d4ee0 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e  orms.  No altern
d4ef0 61 74 69 76 65 0a 2a 2a 20 69 73 20 61 76 61 69  ative.** is avai
d4f00 6c 61 62 6c 65 2e 20 20 49 66 20 79 6f 75 20 64  lable.  If you d
d4f10 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72  on't compile for
d4f20 20 61 20 6d 61 63 2c 20 74 68 65 6e 20 74 68 65   a mac, then the
d4f30 20 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56   "unix-afp".** V
d4f40 46 53 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  FS is not availa
d4f50 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ble..**.********
d4f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
d4f70 64 20 6f 66 20 74 68 65 20 41 46 50 20 6c 6f 63  d of the AFP loc
d4f80 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
d4f90 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
d4fa0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
d4fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4ff0 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a  ******/.../*****
d5000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5040 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
d5050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c  ********** Non-l
d5060 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66  ocking sqlite3_f
d5070 69 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a  ile methods ****
d5080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5090 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
d50a0 54 68 65 20 6e 65 78 74 20 64 69 76 69 73 69 6f  The next divisio
d50b0 6e 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65  n contains imple
d50c0 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 61  mentations for a
d50d0 6c 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68  ll methods of th
d50e0 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69  e .** sqlite3_fi
d50f0 6c 65 20 6f 62 6a 65 63 74 20 6f 74 68 65 72 20  le object other 
d5100 74 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67  than the locking
d5110 20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65 20 6c   methods.  The l
d5120 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64  ocking.** method
d5130 73 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 69  s were defined i
d5140 6e 20 64 69 76 69 73 69 6f 6e 73 20 61 62 6f 76  n divisions abov
d5150 65 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d  e (one locking m
d5160 65 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76  ethod per.** div
d5170 69 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d  ision).  Those m
d5180 65 74 68 6f 64 73 20 74 68 61 74 20 61 72 65 20  ethods that are 
d5190 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f  common to all lo
d51a0 63 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61  cking modes.** a
d51b0 72 65 20 67 61 74 68 65 72 20 74 6f 67 65 74 68  re gather togeth
d51c0 65 72 20 69 6e 74 6f 20 74 68 69 73 20 64 69 76  er into this div
d51d0 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ision..*/../*.**
d51e0 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66   Seek to the off
d51f0 73 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68  set passed as th
d5200 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
d5210 74 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74  t, then read cnt
d5220 20 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20   .** bytes into 
d5230 70 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65  pBuf. Return the
d5240 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
d5250 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a   actually read..
d5260 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f  **.** NB:  If yo
d5270 75 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45  u define USE_PRE
d5280 41 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36  AD or USE_PREAD6
d5290 34 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74  4, then it might
d52a0 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65   also.** be nece
d52b0 73 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20  ssary to define 
d52c0 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f  _XOPEN_SOURCE to
d52d0 20 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76   be 500.  This v
d52e0 61 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e  aries from.** on
d52f0 65 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74  e system to anot
d5300 68 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69  her.  Since SQLi
d5310 74 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69  te does not defi
d5320 6e 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20  ne USE_PREAD.** 
d5330 61 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20  any any form by 
d5340 64 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c  default, we will
d5350 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
d5360 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f  define _XOPEN_SO
d5370 55 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63  URCE..** See tic
d5380 6b 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23  kets #2741 and #
d5390 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61  2681..**.** To a
d53a0 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68  void stomping th
d53b0 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e  e errno value on
d53c0 20 61 20 66 61 69 6c 65 64 20 72 65 61 64 20 74   a failed read t
d53d0 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c  he lastErrno val
d53e0 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66  ue.** is set bef
d53f0 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
d5400 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65  /.static int see
d5410 6b 41 6e 64 52 65 61 64 28 75 6e 69 78 46 69 6c  kAndRead(unixFil
d5420 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69  e *id, sqlite3_i
d5430 6e 74 36 34 20 6f 66 66 73 65 74 2c 20 76 6f 69  nt64 offset, voi
d5440 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74  d *pBuf, int cnt
d5450 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20  ){.  int got;.  
d5460 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20  i64 newOffset;. 
d5470 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69   TIMER_START;.#i
d5480 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52  f defined(USE_PR
d5490 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 72 65  EAD).  got = pre
d54a0 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20  ad(id->h, pBuf, 
d54b0 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20  cnt, offset);.  
d54c0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
d54d0 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c   got = -1 );.#el
d54e0 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50  if defined(USE_P
d54f0 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20  READ64).  got = 
d5500 70 72 65 61 64 36 34 28 69 64 2d 3e 68 2c 20 70  pread64(id->h, p
d5510 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74  Buf, cnt, offset
d5520 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
d5530 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29  rror( got = -1 )
d5540 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66  ;.#else.  newOff
d5550 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e  set = lseek(id->
d5560 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f  h, offset, SEEK_
d5570 53 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  SET);.  Simulate
d5580 49 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73  IOError( newOffs
d5590 65 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65  et-- );.  if( ne
d55a0 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20  wOffset!=offset 
d55b0 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66  ){.    if( newOf
d55c0 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20  fset == -1 ){.  
d55d0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
d55e0 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  id)->lastErrno =
d55f0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73   errno;.    }els
d5600 65 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46  e{.      ((unixF
d5610 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
d5620 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20  rno = 0;....    
d5630 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b  }.    return -1;
d5640 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61  .  }.  got = rea
d5650 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  d(id->h, pBuf, c
d5660 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49  nt);.#endif.  TI
d5670 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67  MER_END;.  if( g
d5680 6f 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e  ot<0 ){.    ((un
d5690 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
d56a0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d56b0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 35 28 22    }.  OSTRACE5("
d56c0 52 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64  READ    %-3d %5d
d56d0 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20   %7lld %llu\n", 
d56e0 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73  id->h, got, offs
d56f0 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  et, TIMER_ELAPSE
d5700 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74  D);.  return got
d5710 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
d5720 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65  data from a file
d5730 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20   into a buffer. 
d5740 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
d5750 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65  K if all.** byte
d5760 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63  s were read succ
d5770 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c  essfully and SQL
d5780 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79  ITE_IOERR if any
d5790 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72  thing goes.** wr
d57a0 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ong..*/.static i
d57b0 6e 74 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73  nt unixRead(.  s
d57c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d57d0 20 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20   .  void *pBuf, 
d57e0 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71  .  int amt,.  sq
d57f0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
d5800 65 74 0a 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b  et.){.  int got;
d5810 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b  .  assert( id );
d5820 0a 0a 20 20 2f 2a 20 4e 65 76 65 72 20 72 65 61  ..  /* Never rea
d5830 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 6f  d or write any o
d5840 66 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74  f the bytes in t
d5850 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65  he locking range
d5860 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 28   */.  assert( ((
d5870 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69  unixFile*)id)->i
d5880 73 4c 6f 63 6b 61 62 6c 65 3d 3d 30 0a 20 20 20  sLockable==0.   
d5890 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74         || offset
d58a0 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35  >=PENDING_BYTE+5
d58b0 31 32 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20  12.          || 
d58c0 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44  offset+amt<=PEND
d58d0 49 4e 47 5f 42 59 54 45 20 29 3b 0a 0a 20 20 67  ING_BYTE );..  g
d58e0 6f 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64  ot = seekAndRead
d58f0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20  ((unixFile*)id, 
d5900 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d  offset, pBuf, am
d5910 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61  t);.  if( got==a
d5920 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  mt ){.    return
d5930 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
d5940 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b  lse if( got<0 ){
d5950 0a 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e  .    /* lastErrn
d5960 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64  o set by seekAnd
d5970 52 65 61 64 20 2a 2f 0a 20 20 20 20 72 65 74 75  Read */.    retu
d5980 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
d5990 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  READ;.  }else{. 
d59a0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
d59b0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  d)->lastErrno = 
d59c0 30 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74  0; /* not a syst
d59d0 65 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  em error */.    
d59e0 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 74 73 20  /* Unread parts 
d59f0 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6d 75  of the buffer mu
d5a00 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65  st be zero-fille
d5a10 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28  d */.    memset(
d5a20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67  &((char*)pBuf)[g
d5a30 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29  ot], 0, amt-got)
d5a40 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
d5a50 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
d5a60 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  READ;.  }.}../*.
d5a70 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f  ** Seek to the o
d5a80 66 66 73 65 74 20 69 6e 20 69 64 2d 3e 6f 66 66  ffset in id->off
d5a90 73 65 74 20 74 68 65 6e 20 72 65 61 64 20 63 6e  set then read cn
d5aa0 74 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42 75  t bytes into pBu
d5ab0 66 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  f..** Return the
d5ac0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
d5ad0 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 20   actually read. 
d5ae0 20 55 70 64 61 74 65 20 74 68 65 20 6f 66 66 73   Update the offs
d5af0 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f  et..**.** To avo
d5b00 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 20  id stomping the 
d5b10 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61  errno value on a
d5b20 20 66 61 69 6c 65 64 20 77 72 69 74 65 20 74 68   failed write th
d5b30 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c 75  e lastErrno valu
d5b40 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66 6f  e.** is set befo
d5b50 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f  re returning..*/
d5b60 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65 6b  .static int seek
d5b70 41 6e 64 57 72 69 74 65 28 75 6e 69 78 46 69 6c  AndWrite(unixFil
d5b80 65 20 2a 69 64 2c 20 69 36 34 20 6f 66 66 73 65  e *id, i64 offse
d5b90 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  t, const void *p
d5ba0 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20  Buf, int cnt){. 
d5bb0 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36 34 20   int got;.  i64 
d5bc0 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d  newOffset;.  TIM
d5bd0 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20 64 65  ER_START;.#if de
d5be0 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29  fined(USE_PREAD)
d5bf0 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 28  .  got = pwrite(
d5c00 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74  id->h, pBuf, cnt
d5c10 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 69 66  , offset);.#elif
d5c20 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45   defined(USE_PRE
d5c30 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 70 77  AD64).  got = pw
d5c40 72 69 74 65 36 34 28 69 64 2d 3e 68 2c 20 70 42  rite64(id->h, pB
d5c50 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29  uf, cnt, offset)
d5c60 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66  ;.#else.  newOff
d5c70 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e  set = lseek(id->
d5c80 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f  h, offset, SEEK_
d5c90 53 45 54 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f  SET);.  if( newO
d5ca0 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 29 7b  ffset!=offset ){
d5cb0 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66 66 73  .    if( newOffs
d5cc0 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20  et == -1 ){.    
d5cd0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64    ((unixFile*)id
d5ce0 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  )->lastErrno = e
d5cf0 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rrno;.    }else{
d5d00 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c  .      ((unixFil
d5d10 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e  e*)id)->lastErrn
d5d20 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a  o = 0;....    }.
d5d30 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20      return -1;. 
d5d40 20 7d 0a 20 20 67 6f 74 20 3d 20 77 72 69 74 65   }.  got = write
d5d50 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e  (id->h, pBuf, cn
d5d60 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d  t);.#endif.  TIM
d5d70 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f  ER_END;.  if( go
d5d80 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69  t<0 ){.    ((uni
d5d90 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74  xFile*)id)->last
d5da0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d5db0 20 7d 0a 0a 20 20 4f 53 54 52 41 43 45 35 28 22   }..  OSTRACE5("
d5dc0 57 52 49 54 45 20 20 20 25 2d 33 64 20 25 35 64  WRITE   %-3d %5d
d5dd0 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20   %7lld %llu\n", 
d5de0 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73  id->h, got, offs
d5df0 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  et, TIMER_ELAPSE
d5e00 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74  D);.  return got
d5e10 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  ;.}.../*.** Writ
d5e20 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75  e data from a bu
d5e30 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65  ffer into a file
d5e40 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
d5e50 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a  _OK on success.*
d5e60 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20  * or some other 
d5e70 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61  error code on fa
d5e80 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ilure..*/.static
d5e90 20 69 6e 74 20 75 6e 69 78 57 72 69 74 65 28 0a   int unixWrite(.
d5ea0 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
d5eb0 69 64 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69  id, .  const voi
d5ec0 64 20 2a 70 42 75 66 2c 20 0a 20 20 69 6e 74 20  d *pBuf, .  int 
d5ed0 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69  amt,.  sqlite3_i
d5ee0 6e 74 36 34 20 6f 66 66 73 65 74 20 0a 29 7b 0a  nt64 offset .){.
d5ef0 20 20 69 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b    int wrote = 0;
d5f00 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b  .  assert( id );
d5f10 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30  .  assert( amt>0
d5f20 20 29 3b 0a 0a 20 20 2f 2a 20 4e 65 76 65 72 20   );..  /* Never 
d5f30 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e  read or write an
d5f40 79 20 6f 66 20 74 68 65 20 62 79 74 65 73 20 69  y of the bytes i
d5f50 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61  n the locking ra
d5f60 6e 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  nge */.  assert(
d5f70 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
d5f80 2d 3e 69 73 4c 6f 63 6b 61 62 6c 65 3d 3d 30 0a  ->isLockable==0.
d5f90 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6f 66 66            || off
d5fa0 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54  set>=PENDING_BYT
d5fb0 45 2b 35 31 32 0a 20 20 20 20 20 20 20 20 20 20  E+512.          
d5fc0 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50  || offset+amt<=P
d5fd0 45 4e 44 49 4e 47 5f 42 59 54 45 20 29 3b 0a 0a  ENDING_BYTE );..
d5fe0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
d5ff0 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f   /* If we are do
d6000 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69  ing a normal wri
d6010 74 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65  te to a database
d6020 20 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65   file (as oppose
d6030 64 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20  d to.  ** doing 
d6040 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f  a hot-journal ro
d6050 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74  llback or a writ
d6060 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f  e to some file o
d6070 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a  ther than a.  **
d6080 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65   normal database
d6090 20 66 69 6c 65 29 20 74 68 65 6e 20 72 65 63 6f   file) then reco
d60a0 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74  rd the fact that
d60b0 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20   the database.  
d60c0 2a 2a 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20  ** has changed. 
d60d0 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74   If the transact
d60e0 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6d  ion counter is m
d60f0 6f 64 69 66 69 65 64 2c 20 72 65 63 6f 72 64 20  odified, record 
d6100 74 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 74  that.  ** fact t
d6110 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  oo..  */.  if( (
d6120 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
d6130 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 29 7b  inNormalWrite ){
d6140 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
d6150 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d6160 2a 29 69 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d  *)id;.    pFile-
d6170 3e 64 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20  >dbUpdate = 1;  
d6180 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
d6190 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  has been modifie
d61a0 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66  d */.    if( off
d61b0 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65  set<=24 && offse
d61c0 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20  t+amt>=27 ){.   
d61d0 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20     int rc;.     
d61e0 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d   char oldCntr[4]
d61f0 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65  ;.      Simulate
d6200 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29  IOErrorBenign(1)
d6210 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 65  ;.      rc = see
d6220 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20  kAndRead(pFile, 
d6230 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b  24, oldCntr, 4);
d6240 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49  .      SimulateI
d6250 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b  OErrorBenign(0);
d6260 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 34  .      if( rc!=4
d6270 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e   || memcmp(oldCn
d6280 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75  tr, &((char*)pBu
d6290 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34  f)[24-offset], 4
d62a0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
d62b0 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72  pFile->transCntr
d62c0 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68  Chng = 1;  /* Th
d62d0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
d62e0 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e 67 65  unter has change
d62f0 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  d */.      }.   
d6300 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
d6310 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26   while( amt>0 &&
d6320 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e   (wrote = seekAn
d6330 64 57 72 69 74 65 28 28 75 6e 69 78 46 69 6c 65  dWrite((unixFile
d6340 2a 29 69 64 2c 20 6f 66 66 73 65 74 2c 20 70 42  *)id, offset, pB
d6350 75 66 2c 20 61 6d 74 29 29 3e 30 20 29 7b 0a 20  uf, amt))>0 ){. 
d6360 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b     amt -= wrote;
d6370 0a 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 77  .    offset += w
d6380 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d  rote;.    pBuf =
d6390 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b   &((char*)pBuf)[
d63a0 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20 53 69  wrote];.  }.  Si
d63b0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 28 20  mulateIOError(( 
d63c0 77 72 6f 74 65 3d 28 2d 31 29 2c 20 61 6d 74 3d  wrote=(-1), amt=
d63d0 31 20 29 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  1 ));.  Simulate
d63e0 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 28 20  DiskfullError(( 
d63f0 77 72 6f 74 65 3d 30 2c 20 61 6d 74 3d 31 20 29  wrote=0, amt=1 )
d6400 29 3b 0a 20 20 69 66 28 20 61 6d 74 3e 30 20 29  );.  if( amt>0 )
d6410 7b 0a 20 20 20 20 69 66 28 20 77 72 6f 74 65 3c  {.    if( wrote<
d6420 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6c 61  0 ){.      /* la
d6430 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73  stErrno set by s
d6440 65 65 6b 41 6e 64 57 72 69 74 65 20 2a 2f 0a 20  eekAndWrite */. 
d6450 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
d6460 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a  TE_IOERR_WRITE;.
d6470 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d6480 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
d6490 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b  ->lastErrno = 0;
d64a0 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d   /* not a system
d64b0 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20   error */.      
d64c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55  return SQLITE_FU
d64d0 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  LL;.    }.  }.  
d64e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d64f0 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
d6500 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f  TE_TEST./*.** Co
d6510 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  unt the number o
d6520 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20  f fullsyncs and 
d6530 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54  normal syncs.  T
d6540 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74  his is used to t
d6550 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63  est.** that sync
d6560 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20  s and fullsyncs 
d6570 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 61 74  are occurring at
d6580 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73   the right times
d6590 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
d65a0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63  int sqlite3_sync
d65b0 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49  _count = 0;.SQLI
d65c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
d65d0 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e  e3_fullsync_coun
d65e0 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  t = 0;.#endif../
d65f0 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 74  *.** We do not t
d6600 72 75 73 74 20 73 79 73 74 65 6d 73 20 74 6f 20  rust systems to 
d6610 70 72 6f 76 69 64 65 20 61 20 77 6f 72 6b 69 6e  provide a workin
d6620 67 20 66 64 61 74 61 73 79 6e 63 28 29 2e 20 20  g fdatasync().  
d6630 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65  Some do..** Othe
d6640 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 65  rs do no.  To be
d6650 20 73 61 66 65 2c 20 77 65 20 77 69 6c 6c 20 73   safe, we will s
d6660 74 69 63 6b 20 77 69 74 68 20 74 68 65 20 28 73  tick with the (s
d6670 6c 6f 77 65 72 29 20 66 73 79 6e 63 28 29 2e 0a  lower) fsync()..
d6680 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20 74  ** If you know t
d6690 68 61 74 20 79 6f 75 72 20 73 79 73 74 65 6d 20  hat your system 
d66a0 64 6f 65 73 20 73 75 70 70 6f 72 74 20 66 64 61  does support fda
d66b0 74 61 73 79 6e 63 28 29 20 63 6f 72 72 65 63 74  tasync() correct
d66c0 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d 70  ly,.** then simp
d66d0 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  ly compile with 
d66e0 2d 44 66 64 61 74 61 73 79 6e 63 3d 66 64 61 74  -Dfdatasync=fdat
d66f0 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 65  async.*/.#if !de
d6700 66 69 6e 65 64 28 66 64 61 74 61 73 79 6e 63 29  fined(fdatasync)
d6710 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 6c   && !defined(__l
d6720 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65  inux__).# define
d6730 20 66 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63   fdatasync fsync
d6740 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44  .#endif../*.** D
d6750 65 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46  efine HAVE_FULLF
d6760 53 59 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 64  SYNC to 0 or 1 d
d6770 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74  epending on whet
d6780 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68  her or not.** th
d6790 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61  e F_FULLFSYNC ma
d67a0 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 20  cro is defined. 
d67b0 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 20   F_FULLFSYNC is 
d67c0 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c  currently.** onl
d67d0 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d  y available on M
d67e0 61 63 20 4f 53 20 58 2e 20 20 42 75 74 20 74 68  ac OS X.  But th
d67f0 61 74 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e  at could change.
d6800 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 4c  .*/.#ifdef F_FUL
d6810 4c 46 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20  LFSYNC.# define 
d6820 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31  HAVE_FULLFSYNC 1
d6830 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
d6840 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30  HAVE_FULLFSYNC 0
d6850 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
d6860 54 68 65 20 66 73 79 6e 63 28 29 20 73 79 73 74  The fsync() syst
d6870 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74  em call does not
d6880 20 77 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69   work as adverti
d6890 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75  sed on many.** u
d68a0 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 54 68  nix systems.  Th
d68b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63  e following proc
d68c0 65 64 75 72 65 20 69 73 20 61 6e 20 61 74 74 65  edure is an atte
d68d0 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69  mpt to make.** i
d68e0 74 20 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a  t work better..*
d68f0 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
d6900 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 69  NO_SYNC macro di
d6910 73 61 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63  sables all fsync
d6920 28 29 73 2e 20 20 54 68 69 73 20 69 73 20 75 73  ()s.  This is us
d6930 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74  eful.** for test
d6940 69 6e 67 20 77 68 65 6e 20 77 65 20 77 61 6e 74  ing when we want
d6950 20 74 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 20   to run through 
d6960 74 68 65 20 74 65 73 74 20 73 75 69 74 65 20 71  the test suite q
d6970 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61  uickly..** You a
d6980 72 65 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 69  re strongly advi
d6990 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70  sed *not* to dep
d69a0 6c 6f 79 20 77 69 74 68 20 53 51 4c 49 54 45 5f  loy with SQLITE_
d69b0 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c  NO_SYNC.** enabl
d69c0 65 64 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 6e  ed, however, sin
d69d0 63 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e  ce with SQLITE_N
d69e0 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20  O_SYNC enabled, 
d69f0 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f  an OS crash.** o
d6a00 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  r power failure 
d6a10 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72  will likely corr
d6a20 75 70 74 20 74 68 65 20 64 61 74 61 62 61 73 65  upt the database
d6a30 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c   file..**.** SQL
d6a40 69 74 65 20 73 65 74 73 20 74 68 65 20 64 61 74  ite sets the dat
d6a50 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 68  aOnly flag if th
d6a60 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69  e size of the fi
d6a70 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  le is unchanged.
d6a80 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 62 65 68  .** The idea beh
d6a90 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 20  ind dataOnly is 
d6aa0 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 6f  that it should o
d6ab0 6e 6c 79 20 77 72 69 74 65 20 74 68 65 20 66 69  nly write the fi
d6ac0 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f  le content.** to
d6ad0 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69   disk, not the i
d6ae0 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73  node.  We only s
d6af0 65 74 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 74  et dataOnly if t
d6b00 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20  he file size is 
d6b10 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 69  .** unchanged si
d6b20 6e 63 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a  nce the file siz
d6b30 65 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65  e is part of the
d6b40 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 72   inode.  However
d6b50 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74  , .** Ted Ts'o t
d6b60 65 6c 6c 73 20 75 73 20 74 68 61 74 20 66 64 61  ells us that fda
d6b70 74 61 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c  tasync() will al
d6b80 73 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f  so write the ino
d6b90 64 65 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c  de if the.** fil
d6ba0 65 20 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67  e size has chang
d6bb0 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65  ed.  The only re
d6bc0 61 6c 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  al difference be
d6bd0 74 77 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28  tween fdatasync(
d6be0 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29  ).** and fsync()
d6bf0 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20  , Ted tells us, 
d6c00 69 73 20 74 68 61 74 20 66 64 61 74 61 73 79 6e  is that fdatasyn
d6c10 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75  c() will not flu
d6c20 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20  sh the.** inode 
d6c30 69 66 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 20  if the mtime or 
d6c40 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 69  owner or other i
d6c50 6e 6f 64 65 20 61 74 74 72 69 62 75 74 65 73 20  node attributes 
d6c60 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a  have changed..**
d6c70 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 62   We only care ab
d6c80 6f 75 74 20 74 68 65 20 66 69 6c 65 20 73 69 7a  out the file siz
d6c90 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 72  e, not the other
d6ca0 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 73   file attributes
d6cb0 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61  , so.** as far a
d6cc0 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6e 63  s SQLite is conc
d6cd0 65 72 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 73  erned, an fdatas
d6ce0 79 6e 63 28 29 20 69 73 20 61 6c 77 61 79 73 20  ync() is always 
d6cf0 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c  adequate..** So,
d6d00 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 66   we always use f
d6d10 64 61 74 61 73 79 6e 63 28 29 20 69 66 20 69 74  datasync() if it
d6d20 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72   is available, r
d6d30 65 67 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20  egardless of.** 
d6d40 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
d6d50 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a   dataOnly flag..
d6d60 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  */.static int fu
d6d70 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 2c  ll_fsync(int fd,
d6d80 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69   int fullSync, i
d6d90 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20  nt dataOnly){.  
d6da0 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68  int rc;..  /* Th
d6db0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64  e following "ifd
d6dc0 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62  ef/elif/else/" b
d6dd0 6c 6f 63 6b 20 68 61 73 20 74 68 65 20 73 61 6d  lock has the sam
d6de0 65 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 20  e structure as. 
d6df0 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f   ** the one belo
d6e00 77 2e 20 49 74 20 69 73 20 72 65 70 6c 69 63 61  w. It is replica
d6e10 74 65 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 20  ted here solely 
d6e20 74 6f 20 61 76 6f 69 64 20 63 6c 75 74 74 65 72  to avoid clutter
d6e30 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 65  ing .  ** up the
d6e40 20 72 65 61 6c 20 63 6f 64 65 20 77 69 74 68 20   real code with 
d6e50 74 68 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  the UNUSED_PARAM
d6e60 45 54 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20  ETER() macros.. 
d6e70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
d6e80 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53  E_NO_SYNC.  UNUS
d6e90 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29  ED_PARAMETER(fd)
d6ea0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
d6eb0 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a  ETER(fullSync);.
d6ec0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
d6ed0 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65  ER(dataOnly);.#e
d6ee0 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  lif HAVE_FULLFSY
d6ef0 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  NC.  UNUSED_PARA
d6f00 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b  METER(dataOnly);
d6f10 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f  .#else.  UNUSED_
d6f20 50 41 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79  PARAMETER(fullSy
d6f30 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  nc);.  UNUSED_PA
d6f40 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79  RAMETER(dataOnly
d6f50 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  );.#endif..  /* 
d6f60 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65  Record the numbe
d6f70 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20  r of times that 
d6f80 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66  we do a normal f
d6f90 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a  sync() and .  **
d6fa0 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73   FULLSYNC.  This
d6fb0 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20   is used during 
d6fc0 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66  testing to verif
d6fd0 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f 63  y that this proc
d6fe0 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20  edure.  ** gets 
d6ff0 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20  called with the 
d7000 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74  correct argument
d7010 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  s..  */.#ifdef S
d7020 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28  QLITE_TEST.  if(
d7030 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69   fullSync ) sqli
d7040 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
d7050 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f  nt++;.  sqlite3_
d7060 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  sync_count++;.#e
d7070 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65  ndif..  /* If we
d7080 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
d7090 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  he SQLITE_NO_SYN
d70a0 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e  C flag, then syn
d70b0 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e  cing is a.  ** n
d70c0 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66  o-op.  */.#ifdef
d70d0 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a   SQLITE_NO_SYNC.
d70e0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
d70f0 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c  ;.#elif HAVE_FUL
d7100 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c  LFSYNC.  if( ful
d7110 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20  lSync ){.    rc 
d7120 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55  = fcntl(fd, F_FU
d7130 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d  LLFSYNC, 0);.  }
d7140 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31  else{.    rc = 1
d7150 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68  ;.  }.  /* If th
d7160 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c  e FULLFSYNC fail
d7170 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f  ed, fall back to
d7180 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66   attempting an f
d7190 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20  sync()..  ** It 
d71a0 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73  shouldn't be pos
d71b0 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73  sible for fullfs
d71c0 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74  ync to fail on t
d71d0 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66  he local .  ** f
d71e0 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f  ile system (on O
d71f0 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20  SX), so failure 
d7200 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 46  indicates that F
d7210 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73  ULLFSYNC.  ** is
d7220 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f  n't supported fo
d7230 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73 74  r this file syst
d7240 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20  em. So, attempt 
d7250 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61  an fsync .  ** a
d7260 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e  nd (for now) ign
d7270 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61 64  ore the overhead
d7280 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75   of a superfluou
d7290 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a  s fcntl call.  .
d72a0 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74    ** It'd be bet
d72b0 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66 75  ter to detect fu
d72c0 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20  llfsync support 
d72d0 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a  once and avoid .
d72e0 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63    ** the fcntl c
d72f0 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73  all every time s
d7300 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20  ync is called.. 
d7310 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72   */.  if( rc ) r
d7320 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a  c = fsync(fd);..
d7330 23 65 6c 73 65 20 0a 20 20 72 63 20 3d 20 66 64  #else .  rc = fd
d7340 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 69 66  atasync(fd);.#if
d7350 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66   OS_VXWORKS.  if
d7360 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e  ( rc==-1 && errn
d7370 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20  o==ENOTSUP ){.  
d7380 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29    rc = fsync(fd)
d7390 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
d73a0 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65  OS_VXWORKS */.#e
d73b0 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53 51  ndif /* ifdef SQ
d73c0 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69  LITE_NO_SYNC eli
d73d0 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  f HAVE_FULLFSYNC
d73e0 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58   */..  if( OS_VX
d73f0 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d 31  WORKS && rc!= -1
d7400 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a   ){.    rc = 0;.
d7410 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
d7420 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
d7430 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74  ure all writes t
d7440 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66  o a particular f
d7450 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65  ile are committe
d7460 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  d to disk..**.**
d7470 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20   If dataOnly==0 
d7480 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69  then both the fi
d7490 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74  le itself and it
d74a0 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65  s metadata (file
d74b0 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73  .** size, access
d74c0 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20   time, etc) are 
d74d0 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61  synced.  If data
d74e0 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c  Only!=0 then onl
d74f0 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61  y the.** file da
d7500 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a  ta is synced..**
d7510 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20  .** Under Unix, 
d7520 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  also make sure t
d7530 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f 72  hat the director
d7540 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  y entry for the 
d7550 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e  file.** has been
d7560 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79 6e   created by fsyn
d7570 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 74  c-ing the direct
d7580 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ory that contain
d7590 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49  s the file..** I
d75a0 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74  f we do not do t
d75b0 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75  his and we encou
d75c0 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 69  nter a power fai
d75d0 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 74  lure, the direct
d75e0 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72  ory.** entry for
d75f0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67   the journal mig
d7600 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74  ht not exist aft
d7610 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54  er we reboot.  T
d7620 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74  he next.** SQLit
d7630 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  e to access the 
d7640 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e  file will not kn
d7650 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72  ow that the jour
d7660 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61  nal exists (beca
d7670 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63  use.** the direc
d7680 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74  tory entry for t
d7690 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e  he journal was n
d76a0 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e  ever created) an
d76b0 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  d the transactio
d76c0 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f  n.** will not ro
d76d0 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62  ll back - possib
d76e0 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61  ly leading to da
d76f0 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
d7700 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
d7710 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65   unixSync(sqlite
d7720 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
d7730 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63  flags){.  int rc
d7740 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
d7750 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
d7760 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61  )id;..  int isDa
d7770 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26  taOnly = (flags&
d7780 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41  SQLITE_SYNC_DATA
d7790 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46  ONLY);.  int isF
d77a0 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73  ullsync = (flags
d77b0 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53  &0x0F)==SQLITE_S
d77c0 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20  YNC_FULL;..  /* 
d77d0 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f  Check that one o
d77e0 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  f SQLITE_SYNC_NO
d77f0 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73  RMAL or FULL was
d7800 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73   passed */.  ass
d7810 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 29  ert((flags&0x0F)
d7820 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  ==SQLITE_SYNC_NO
d7830 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66  RMAL.      || (f
d7840 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49  lags&0x0F)==SQLI
d7850 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29  TE_SYNC_FULL.  )
d7860 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e  ;..  /* Unix can
d7870 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79  not, but some sy
d7880 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e  stems may return
d7890 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f   SQLITE_FULL fro
d78a0 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a  m here. This.  *
d78b0 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73  * line is to tes
d78c0 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20  t that doing so 
d78d0 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61  does not cause a
d78e0 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a  ny problems..  *
d78f0 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  /.  SimulateDisk
d7900 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72  fullError( retur
d7910 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b  n SQLITE_FULL );
d7920 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
d7930 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28  e );.  OSTRACE2(
d7940 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22  "SYNC    %-3d\n"
d7950 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72  , pFile->h);.  r
d7960 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70  c = full_fsync(p
d7970 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73  File->h, isFulls
d7980 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29  ync, isDataOnly)
d7990 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
d79a0 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69  ror( rc=1 );.  i
d79b0 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46 69  f( rc ){.    pFi
d79c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d79d0 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72  errno;.    retur
d79e0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46  n SQLITE_IOERR_F
d79f0 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SYNC;.  }.  if( 
d7a00 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20  pFile->dirfd>=0 
d7a10 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3b 0a  ){.    int err;.
d7a20 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 44 49      OSTRACE4("DI
d7a30 52 53 59 4e 43 20 25 2d 33 64 20 28 68 61 76 65  RSYNC %-3d (have
d7a40 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75  _fullfsync=%d fu
d7a50 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70  llsync=%d)\n", p
d7a60 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20 20  File->dirfd,.   
d7a70 20 20 20 20 20 20 20 20 20 48 41 56 45 5f 46 55           HAVE_FU
d7a80 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73  LLFSYNC, isFulls
d7a90 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ync);.#ifndef SQ
d7aa0 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52  LITE_DISABLE_DIR
d7ab0 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65 20  SYNC.    /* The 
d7ac0 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69  directory sync i
d7ad0 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64  s only attempted
d7ae0 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20 69   if full_fsync i
d7af0 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e 65 64 20  s.    ** turned 
d7b00 6f 66 66 20 6f 72 20 75 6e 61 76 61 69 6c 61 62  off or unavailab
d7b10 6c 65 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f 66  le.  If a full_f
d7b20 73 79 6e 63 20 6f 63 63 75 72 72 65 64 20 61 62  sync occurred ab
d7b30 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e  ove,.    ** then
d7b40 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 73   the directory s
d7b50 79 6e 63 20 69 73 20 73 75 70 65 72 66 6c 75 6f  ync is superfluo
d7b60 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  us..    */.    i
d7b70 66 28 20 28 21 48 41 56 45 5f 46 55 4c 4c 46 53  f( (!HAVE_FULLFS
d7b80 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73 79  YNC || !isFullsy
d7b90 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 73 79 6e  nc) && full_fsyn
d7ba0 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 30  c(pFile->dirfd,0
d7bb0 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a  ,0) ){.       /*
d7bc0 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 20 68 61  .       ** We ha
d7bd0 76 65 20 72 65 63 65 69 76 65 64 20 6d 75 6c 74  ve received mult
d7be0 69 70 6c 65 20 72 65 70 6f 72 74 73 20 6f 66 20  iple reports of 
d7bf0 66 73 79 6e 63 28 29 20 72 65 74 75 72 6e 69 6e  fsync() returnin
d7c00 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 72 72 6f  g.       ** erro
d7c10 72 73 20 77 68 65 6e 20 61 70 70 6c 69 65 64 20  rs when applied 
d7c20 74 6f 20 64 69 72 65 63 74 6f 72 69 65 73 20 6f  to directories o
d7c30 6e 20 63 65 72 74 61 69 6e 20 66 69 6c 65 20 73  n certain file s
d7c40 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 20 20 2a  ystems..       *
d7c50 2a 20 41 20 66 61 69 6c 65 64 20 64 69 72 65 63  * A failed direc
d7c60 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6e 6f 74  tory sync is not
d7c70 20 61 20 62 69 67 20 64 65 61 6c 2e 20 20 53 6f   a big deal.  So
d7c80 20 69 74 20 73 65 65 6d 73 0a 20 20 20 20 20 20   it seems.      
d7c90 20 2a 2a 20 62 65 74 74 65 72 20 74 6f 20 69 67   ** better to ig
d7ca0 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e 20  nore the error. 
d7cb0 20 54 69 63 6b 65 74 20 23 31 36 35 37 0a 20 20   Ticket #1657.  
d7cc0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 2f       */.       /
d7cd0 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72  * pFile->lastErr
d7ce0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20  no = errno; */. 
d7cf0 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20        /* return 
d7d00 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f  SQLITE_IOERR; */
d7d10 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
d7d20 20 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46    err = close(pF
d7d30 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a 20  ile->dirfd); /* 
d7d40 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79 6e  Only need to syn
d7d50 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 65  c once, so close
d7d60 20 74 68 65 20 2a 2f 0a 20 20 20 20 69 66 28 20   the */.    if( 
d7d70 65 72 72 3d 3d 30 20 29 7b 20 20 20 20 20 20 20  err==0 ){       
d7d80 20 20 20 20 20 20 20 2f 2a 20 64 69 72 65 63 74         /* direct
d7d90 6f 72 79 20 77 68 65 6e 20 77 65 20 61 72 65 20  ory when we are 
d7da0 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70 46  done */.      pF
d7db0 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b  ile->dirfd = -1;
d7dc0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
d7dd0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d7de0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d7df0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
d7e00 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20  ERR_DIR_CLOSE;. 
d7e10 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
d7e20 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
d7e30 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20  runcate an open 
d7e40 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66  file to a specif
d7e50 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74  ied size.*/.stat
d7e60 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63  ic int unixTrunc
d7e70 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ate(sqlite3_file
d7e80 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29   *id, i64 nByte)
d7e90 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
d7ea0 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69  sert( id );.  Si
d7eb0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
d7ec0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
d7ed0 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20  RR_TRUNCATE );. 
d7ee0 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28   rc = ftruncate(
d7ef0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
d7f00 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65  >h, (off_t)nByte
d7f10 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
d7f20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
d7f30 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  d)->lastErrno = 
d7f40 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72  errno;.    retur
d7f50 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  n SQLITE_IOERR_T
d7f60 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73 65  RUNCATE;.  }else
d7f70 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
d7f80 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ITE_OK;.  }.}../
d7f90 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74  *.** Determine t
d7fa0 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
d7fb0 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74  of a file in byt
d7fc0 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  es.*/.static int
d7fd0 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 28 73 71   unixFileSize(sq
d7fe0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
d7ff0 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69  i64 *pSize){.  i
d8000 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20  nt rc;.  struct 
d8010 73 74 61 74 20 62 75 66 3b 0a 20 20 61 73 73 65  stat buf;.  asse
d8020 72 74 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d  rt( id );.  rc =
d8030 20 66 73 74 61 74 28 28 28 75 6e 69 78 46 69 6c   fstat(((unixFil
d8040 65 2a 29 69 64 29 2d 3e 68 2c 20 26 62 75 66 29  e*)id)->h, &buf)
d8050 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
d8060 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69  ror( rc=1 );.  i
d8070 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20  f( rc!=0 ){.    
d8080 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
d8090 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
d80a0 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  no;.    return S
d80b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41  QLITE_IOERR_FSTA
d80c0 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20  T;.  }.  *pSize 
d80d0 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a  = buf.st_size;..
d80e0 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e    /* When openin
d80f0 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61  g a zero-size da
d8100 74 61 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64  tabase, the find
d8110 4c 6f 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65  LockInfo() proce
d8120 64 75 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73  dure.  ** writes
d8130 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69   a single byte i
d8140 6e 74 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e  nto that file in
d8150 20 6f 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61   order to work a
d8160 72 6f 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a  round a bug.  **
d8170 20 69 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64   in the OS-X msd
d8180 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20  os filesystem.  
d8190 49 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69  In order to avoi
d81a0 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20  d problems with 
d81b0 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72  upper.  ** layer
d81c0 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65  s, we need to re
d81d0 70 6f 72 74 20 74 68 69 73 20 66 69 6c 65 20 73  port this file s
d81e0 69 7a 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e  ize as zero even
d81f0 20 74 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20   though it is.  
d8200 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54  ** really 1.   T
d8210 69 63 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a  icket #3260..  *
d8220 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d  /.  if( *pSize==
d8230 31 20 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a  1 ) *pSize = 0;.
d8240 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
d8250 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c  E_OK;.}..#if SQL
d8260 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
d8270 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69  NG_STYLE && defi
d8280 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f  ned(__APPLE__)./
d8290 2a 0a 2a 2a 20 48 61 6e 64 6c 65 72 20 66 6f 72  *.** Handler for
d82a0 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 66   proxy-locking f
d82b0 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 76 65 72 62  ile-control verb
d82c0 73 2e 20 20 44 65 66 69 6e 65 64 20 62 65 6c 6f  s.  Defined belo
d82d0 77 20 69 6e 20 74 68 65 0a 2a 2a 20 70 72 6f 78  w in the.** prox
d82e0 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 64 69 76  ying locking div
d82f0 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ision..*/.static
d8300 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65 43 6f   int proxyFileCo
d8310 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69  ntrol(sqlite3_fi
d8320 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a  le*,int,void*);.
d8330 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49  #endif.../*.** I
d8340 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63  nformation and c
d8350 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65  ontrol of an ope
d8360 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a  n file handle..*
d8370 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
d8380 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c  xFileControl(sql
d8390 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
d83a0 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72  nt op, void *pAr
d83b0 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  g){.  switch( op
d83c0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
d83d0 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54  ITE_FCNTL_LOCKST
d83e0 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69  ATE: {.      *(i
d83f0 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69  nt*)pArg = ((uni
d8400 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b  xFile*)id)->lock
d8410 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65 74 75  type;.      retu
d8420 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
d8430 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
d8440 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20  ITE_LAST_ERRNO: 
d8450 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70  {.      *(int*)p
d8460 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65  Arg = ((unixFile
d8470 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f  *)id)->lastErrno
d8480 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
d8490 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
d84a0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
d84b0 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20     /* The pager 
d84c0 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f  calls this metho
d84d0 64 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 61 74  d to signal that
d84e0 20 69 74 20 68 61 73 20 64 6f 6e 65 0a 20 20 20   it has done.   
d84f0 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61   ** a rollback a
d8500 6e 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61  nd that the data
d8510 62 61 73 65 20 69 73 20 74 68 65 72 65 66 6f 72  base is therefor
d8520 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a  e unchanged and.
d8530 20 20 20 20 2a 2a 20 69 74 20 68 65 6e 63 65 20      ** it hence 
d8540 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65  it is OK for the
d8550 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 68 61   transaction cha
d8560 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 62  nge counter to b
d8570 65 0a 20 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67  e.    ** unchang
d8580 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ed..    */.    c
d8590 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ase SQLITE_FCNTL
d85a0 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b  _DB_UNCHANGED: {
d85b0 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c  .      ((unixFil
d85c0 65 2a 29 69 64 29 2d 3e 64 62 55 70 64 61 74 65  e*)id)->dbUpdate
d85d0 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75   = 0;.      retu
d85e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
d85f0 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 53    }.#endif.#if S
d8600 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
d8610 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65  KING_STYLE && de
d8620 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
d8630 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
d8640 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
d8650 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  LE:.    case SQL
d8660 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_GET_LOCKPROX
d8670 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72  YFILE: {.      r
d8680 65 74 75 72 6e 20 70 72 6f 78 79 46 69 6c 65 43  eturn proxyFileC
d8690 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72  ontrol(id,op,pAr
d86a0 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  g);.    }.#endif
d86b0 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
d86c0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
d86d0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  && defined(__APP
d86e0 4c 45 5f 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72  LE__) */.  }.  r
d86f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
d8700 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  OR;.}../*.** Ret
d8710 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73  urn the sector s
d8720 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
d8730 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62  the underlying b
d8740 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a  lock device for.
d8750 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ** the specified
d8760 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61   file. This is a
d8770 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32  lmost always 512
d8780 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20   bytes, but may 
d8790 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72  be.** larger for
d87a0 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a   some devices..*
d87b0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65  *.** SQLite code
d87c0 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75   assumes this fu
d87d0 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61  nction cannot fa
d87e0 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75  il. It also assu
d87f0 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74  mes that.** if t
d8800 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65  wo files are cre
d8810 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  ated in the same
d8820 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72   file-system dir
d8830 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20  ectory (i.e..** 
d8840 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69  a database and i
d8850 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29  ts journal file)
d8860 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72   that the sector
d8870 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68   size will be th
d8880 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f  e.** same for bo
d8890 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  th..*/.static in
d88a0 74 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65  t unixSectorSize
d88b0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e  (sqlite3_file *N
d88c0 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45  otUsed){.  UNUSE
d88d0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
d88e0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  sed);.  return S
d88f0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45  QLITE_DEFAULT_SE
d8900 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a  CTOR_SIZE;.}../*
d8910 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64  .** Return the d
d8920 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69  evice characteri
d8930 73 74 69 63 73 20 66 6f 72 20 74 68 65 20 66 69  stics for the fi
d8940 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61  le. This is alwa
d8950 79 73 20 30 20 66 6f 72 20 75 6e 69 78 2e 0a 2a  ys 0 for unix..*
d8960 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
d8970 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
d8980 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66  istics(sqlite3_f
d8990 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  ile *NotUsed){. 
d89a0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
d89b0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65  R(NotUsed);.  re
d89c0 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
d89d0 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 69   Here ends the i
d89e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
d89f0 20 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c   all sqlite3_fil
d8a00 65 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a  e methods..**.**
d8a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a20 2a 2a 2a 2a 20 45 6e 64 20 73 71 6c 69 74 65 33  **** End sqlite3
d8a30 5f 66 69 6c 65 20 4d 65 74 68 6f 64 73 20 2a 2a  _file Methods **
d8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
d8ab0 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69 73 69  *.** This divisi
d8ac0 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69  on contains defi
d8ad0 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74  nitions of sqlit
d8ae0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
d8af0 6a 65 63 74 73 20 74 68 61 74 0a 2a 2a 20 69 6d  jects that.** im
d8b00 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20  plement various 
d8b10 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72  file locking str
d8b20 61 74 65 67 69 65 73 2e 20 20 49 74 20 61 6c 73  ategies.  It als
d8b30 6f 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e  o contains defin
d8b40 69 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 22 66 69  itions.** of "fi
d8b50 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 2e  nder" functions.
d8b60 20 20 41 20 66 69 6e 64 65 72 2d 66 75 6e 63 74    A finder-funct
d8b70 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6c  ion is used to l
d8b80 6f 63 61 74 65 20 74 68 65 20 61 70 70 72 6f 70  ocate the approp
d8b90 72 69 61 74 65 0a 2a 2a 20 73 71 6c 69 74 65 33  riate.** sqlite3
d8ba0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
d8bb0 63 74 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  ct for a particu
d8bc0 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c  lar database fil
d8bd0 65 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61  e.  The pAppData
d8be0 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65  .** field of the
d8bf0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 56 46 53   sqlite3_vfs VFS
d8c00 20 6f 62 6a 65 63 74 73 20 61 72 65 20 69 6e 69   objects are ini
d8c10 74 69 61 6c 69 7a 65 64 20 74 6f 20 62 65 20 70  tialized to be p
d8c20 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68  ointers to.** th
d8c30 65 20 63 6f 72 72 65 63 74 20 66 69 6e 64 65 72  e correct finder
d8c40 2d 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68  -function for th
d8c50 61 74 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f  at VFS..**.** Mo
d8c60 73 74 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69  st finder functi
d8c70 6f 6e 73 20 72 65 74 75 72 6e 20 61 20 70 6f 69  ons return a poi
d8c80 6e 74 65 72 20 74 6f 20 61 20 66 69 78 65 64 20  nter to a fixed 
d8c90 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
d8ca0 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 54  ds.** object.  T
d8cb0 68 65 20 6f 6e 6c 79 20 69 6e 74 65 72 65 73 74  he only interest
d8cc0 69 6e 67 20 66 69 6e 64 65 72 2d 66 75 6e 63 74  ing finder-funct
d8cd0 69 6f 6e 20 69 73 20 61 75 74 6f 6c 6f 63 6b 49  ion is autolockI
d8ce0 6f 46 69 6e 64 65 72 2c 20 77 68 69 63 68 0a 2a  oFinder, which.*
d8cf0 2a 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66  * looks at the f
d8d00 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 61  ilesystem type a
d8d10 6e 64 20 74 72 69 65 73 20 74 6f 20 67 75 65 73  nd tries to gues
d8d20 73 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69  s the best locki
d8d30 6e 67 0a 2a 2a 20 73 74 72 61 74 65 67 79 20 66  ng.** strategy f
d8d40 72 6f 6d 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20  rom that..**.** 
d8d50 46 6f 72 20 66 69 6e 64 65 72 2d 66 75 6e 74 69  For finder-funti
d8d60 6f 6e 20 46 2c 20 74 77 6f 20 6f 62 6a 65 63 74  on F, two object
d8d70 73 20 61 72 65 20 63 72 65 61 74 65 64 3a 0a 2a  s are created:.*
d8d80 2a 0a 2a 2a 20 20 20 20 28 31 29 20 54 68 65 20  *.**    (1) The 
d8d90 72 65 61 6c 20 66 69 6e 64 65 72 2d 66 75 6e 63  real finder-func
d8da0 74 69 6f 6e 20 6e 61 6d 65 64 20 22 46 49 6d 70  tion named "FImp
d8db0 74 28 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28  t()"..**.**    (
d8dc0 32 29 20 41 20 63 6f 6e 73 74 61 6e 74 20 70 6f  2) A constant po
d8dd0 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 66 75  inter to this fu
d8de0 6e 63 74 69 6f 20 6e 61 6d 65 64 20 6a 75 73 74  nctio named just
d8df0 20 22 46 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41   "F"..**.**.** A
d8e00 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
d8e10 46 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65  F pointer is use
d8e20 64 20 61 73 20 74 68 65 20 70 41 70 70 44 61 74  d as the pAppDat
d8e30 61 20 76 61 6c 75 65 20 66 6f 72 20 56 46 53 0a  a value for VFS.
d8e40 2a 2a 20 6f 62 6a 65 63 74 73 2e 20 20 57 65 20  ** objects.  We 
d8e50 68 61 76 65 20 74 6f 20 64 6f 20 74 68 69 73 20  have to do this 
d8e60 69 6e 73 74 65 61 64 20 6f 66 20 6c 65 74 74 69  instead of letti
d8e70 6e 67 20 70 41 70 70 44 61 74 61 20 70 6f 69 6e  ng pAppData poin
d8e80 74 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 61 74  t.** directly at
d8e90 20 74 68 65 20 66 69 6e 64 65 72 2d 66 75 6e 63   the finder-func
d8ea0 74 69 6f 6e 20 73 69 6e 63 65 20 43 39 30 20 72  tion since C90 r
d8eb0 75 6c 65 73 20 70 72 65 76 65 6e 74 20 61 20 76  ules prevent a v
d8ec0 6f 69 64 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20  oid*.** from be 
d8ed0 63 61 73 74 20 69 6e 74 6f 20 61 20 66 75 6e 63  cast into a func
d8ee0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  tion pointer..**
d8ef0 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74  .**.** Each inst
d8f00 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6d 61 63  ance of this mac
d8f10 72 6f 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f  ro generates two
d8f20 20 6f 62 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20   objects:.**.** 
d8f30 20 20 2a 20 20 41 20 63 6f 6e 73 74 61 6e 74 20    *  A constant 
d8f40 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
d8f50 64 73 20 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d  ds object call M
d8f60 45 54 48 4f 44 20 74 68 61 74 20 68 61 73 20 6c  ETHOD that has l
d8f70 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d  ocking.**      m
d8f80 65 74 68 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f  ethods CLOSE, LO
d8f90 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45  CK, UNLOCK, CKRE
d8fa0 53 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a  SLOCK..**.**   *
d8fb0 20 20 41 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20    An I/O method 
d8fc0 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20  finder function 
d8fd0 63 61 6c 6c 65 64 20 46 49 4e 44 45 52 20 74 68  called FINDER th
d8fe0 61 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  at returns a poi
d8ff0 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20  nter.**      to 
d9000 74 68 65 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63  the METHOD objec
d9010 74 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75  t in the previou
d9020 73 20 62 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65  s bullet..*/.#de
d9030 66 69 6e 65 20 49 4f 4d 45 54 48 4f 44 53 28 46  fine IOMETHODS(F
d9040 49 4e 44 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43  INDER, METHOD, C
d9050 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f  LOSE, LOCK, UNLO
d9060 43 4b 2c 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20  CK, CKLOCK)     
d9070 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74            \.stat
d9080 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
d9090 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48  _io_methods METH
d90a0 4f 44 20 3d 20 7b 20 20 20 20 20 20 20 20 20 20  OD = {          
d90b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d90c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 31 2c           \.   1,
d90d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d90e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
d90f0 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20  rsion */        
d9100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9110 20 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f          \.   CLO
d9120 53 45 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  SE,             
d9130 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f           /* xClo
d9140 73 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  se */           
d9150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9160 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78         \.   unix
d9170 52 65 61 64 2c 20 20 20 20 20 20 20 20 20 20 20  Read,           
d9180 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64          /* xRead
d9190 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
d91a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d91b0 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 57        \.   unixW
d91c0 72 69 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  rite,           
d91d0 20 20 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65         /* xWrite
d91e0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
d91f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9200 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 54 72       \.   unixTr
d9210 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20  uncate,         
d9220 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61        /* xTrunca
d9230 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  te */           
d9240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9250 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e      \.   unixSyn
d9260 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
d9270 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f       /* xSync */
d9280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d92a0 20 20 20 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65     \.   unixFile
d92b0 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20  Size,           
d92c0 20 20 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65      /* xFileSize
d92d0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
d92e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d92f0 20 20 5c 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20    \.   LOCK,    
d9300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9310 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20     /* xLock */  
d9320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9340 20 5c 0a 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20   \.   UNLOCK,   
d9350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9360 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20    /* xUnlock */ 
d9370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9390 5c 0a 20 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20  \.   CKLOCK,    
d93a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d93b0 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   /* xCheckReserv
d93c0 65 64 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20  edLock */       
d93d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
d93e0 0a 20 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74  .   unixFileCont
d93f0 72 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  rol,            
d9400 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  /* xFileControl 
d9410 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
d9420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
d9430 20 20 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a     unixSectorSiz
d9440 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e,             /
d9450 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f  * xSectorSize */
d9460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9470 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
d9480 20 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72    unixDeviceChar
d9490 61 63 74 65 72 69 73 74 69 63 73 20 20 20 2f 2a  acteristics   /*
d94a0 20 78 44 65 76 69 63 65 43 61 70 61 62 69 6c 69   xDeviceCapabili
d94b0 74 69 65 73 20 2a 2f 20 20 20 20 20 20 20 20 20  ties */         
d94c0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b              \.};
d94d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d94e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d94f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9510 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61             \.sta
d9520 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
d9530 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49  3_io_methods *FI
d9540 4e 44 45 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74  NDER##Impl(const
d9550 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 68 29   char *z, int h)
d9560 7b 20 20 20 20 20 20 20 20 20 5c 0a 20 20 55 4e  {         \.  UN
d9570 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a  USED_PARAMETER(z
d9580 29 3b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  ); UNUSED_PARAME
d9590 54 45 52 28 68 29 3b 20 20 20 20 20 20 20 20 20  TER(h);         
d95a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d95b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 72 65 74           \.  ret
d95c0 75 72 6e 20 26 4d 45 54 48 4f 44 3b 20 20 20 20  urn &METHOD;    
d95d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d95e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d95f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9600 20 20 20 20 20 20 20 20 5c 0a 7d 20 20 20 20 20          \.}     
d9610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9650 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20         \.static 
d9660 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
d9670 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73  _methods *(*cons
d9680 74 20 46 49 4e 44 45 52 29 28 63 6f 6e 73 74 20  t FINDER)(const 
d9690 63 68 61 72 2a 2c 69 6e 74 29 20 20 20 20 20 20  char*,int)      
d96a0 20 20 20 20 20 20 5c 0a 20 20 20 20 3d 20 46 49        \.    = FI
d96b0 4e 44 45 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a  NDER##Impl;../*.
d96c0 2a 2a 20 48 65 72 65 20 61 72 65 20 61 6c 6c 20  ** Here are all 
d96d0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  of the sqlite3_i
d96e0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
d96f0 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68  s for each of th
d9700 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72  e.** locking str
d9710 61 74 65 67 69 65 73 2e 20 20 46 75 6e 63 74 69  ategies.  Functi
d9720 6f 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e 20  ons that return 
d9730 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 73  pointers to thes
d9740 65 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65  e methods.** are
d9750 20 61 6c 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a   also created..*
d9760 2f 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70  /.IOMETHODS(.  p
d9770 6f 73 69 78 49 6f 46 69 6e 64 65 72 2c 20 20 20  osixIoFinder,   
d9780 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64           /* Find
d9790 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  er function name
d97a0 20 2a 2f 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74   */.  posixIoMet
d97b0 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20  hods,           
d97c0 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  /* sqlite3_io_me
d97d0 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d  thods object nam
d97e0 65 20 2a 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65  e */.  unixClose
d97f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
d9800 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f   /* xClose metho
d9810 64 20 2a 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c  d */.  unixLock,
d9820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9830 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64   /* xLock method
d9840 20 2a 2f 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b   */.  unixUnlock
d9850 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
d9860 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f  /* xUnlock metho
d9870 64 20 2a 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b  d */.  unixCheck
d9880 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20  ReservedLock    
d9890 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   /* xCheckReserv
d98a0 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  edLock method */
d98b0 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20  .).IOMETHODS(.  
d98c0 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20  nolockIoFinder, 
d98d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e            /* Fin
d98e0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  der function nam
d98f0 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d  e */.  nolockIoM
d9900 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20  ethods,         
d9910 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   /* sqlite3_io_m
d9920 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61  ethods object na
d9930 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c  me */.  nolockCl
d9940 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ose,            
d9950 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68    /* xClose meth
d9960 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f  od */.  nolockLo
d9970 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
d9980 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f    /* xLock metho
d9990 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c  d */.  nolockUnl
d99a0 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
d99b0 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68   /* xUnlock meth
d99c0 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68  od */.  nolockCh
d99d0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
d99e0 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
d99f0 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  vedLock method *
d9a00 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20  /.).IOMETHODS(. 
d9a10 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72   dotlockIoFinder
d9a20 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ,          /* Fi
d9a30 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61  nder function na
d9a40 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49  me */.  dotlockI
d9a50 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20  oMethods,       
d9a60 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f    /* sqlite3_io_
d9a70 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e  methods object n
d9a80 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b  ame */.  dotlock
d9a90 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
d9aa0 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74     /* xClose met
d9ab0 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b  hod */.  dotlock
d9ac0 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  Lock,           
d9ad0 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68     /* xLock meth
d9ae0 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55  od */.  dotlockU
d9af0 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  nlock,          
d9b00 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74    /* xUnlock met
d9b10 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b  hod */.  dotlock
d9b20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d9b30 6b 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65  k  /* xCheckRese
d9b40 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  rvedLock method 
d9b50 2a 2f 0a 29 0a 0a 23 69 66 20 53 51 4c 49 54 45  */.)..#if SQLITE
d9b60 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
d9b70 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57  STYLE && !OS_VXW
d9b80 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a  ORKS.IOMETHODS(.
d9b90 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c    flockIoFinder,
d9ba0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
d9bb0 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e  inder function n
d9bc0 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f  ame */.  flockIo
d9bd0 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20  Methods,        
d9be0 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f     /* sqlite3_io
d9bf0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20  _methods object 
d9c00 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43  name */.  flockC
d9c10 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  lose,           
d9c20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65      /* xClose me
d9c30 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c  thod */.  flockL
d9c40 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
d9c50 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74      /* xLock met
d9c60 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e  hod */.  flockUn
d9c70 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  lock,           
d9c80 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65     /* xUnlock me
d9c90 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43  thod */.  flockC
d9ca0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
d9cb0 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73      /* xCheckRes
d9cc0 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64  ervedLock method
d9cd0 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69   */.).#endif..#i
d9ce0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d  f OS_VXWORKS.IOM
d9cf0 45 54 48 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46  ETHODS(.  semIoF
d9d00 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20  inder,          
d9d10 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75      /* Finder fu
d9d20 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20  nction name */. 
d9d30 20 73 65 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20   semIoMethods,  
d9d40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71             /* sq
d9d50 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
d9d60 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a   object name */.
d9d70 20 20 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20    semClose,     
d9d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
d9d90 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a  Close method */.
d9da0 20 20 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20    semLock,      
d9db0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
d9dc0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  Lock method */. 
d9dd0 20 73 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20   semUnlock,     
d9de0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55             /* xU
d9df0 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  nlock method */.
d9e00 20 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76    semCheckReserv
d9e10 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78  edLock      /* x
d9e20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d9e30 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65  k method */.).#e
d9e40 6e 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65  ndif..#if define
d9e50 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20  d(__APPLE__) && 
d9e60 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
d9e70 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45  CKING_STYLE.IOME
d9e80 54 48 4f 44 53 28 0a 20 20 61 66 70 49 6f 46 69  THODS(.  afpIoFi
d9e90 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  nder,           
d9ea0 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e     /* Finder fun
d9eb0 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  ction name */.  
d9ec0 61 66 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20  afpIoMethods,   
d9ed0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c            /* sql
d9ee0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
d9ef0 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20  object name */. 
d9f00 20 61 66 70 43 6c 6f 73 65 2c 20 20 20 20 20 20   afpClose,      
d9f10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
d9f20 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lose method */. 
d9f30 20 61 66 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20   afpLock,       
d9f40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c             /* xL
d9f50 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ock method */.  
d9f60 61 66 70 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20  afpUnlock,      
d9f70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e            /* xUn
d9f80 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lock method */. 
d9f90 20 61 66 70 43 68 65 63 6b 52 65 73 65 72 76 65   afpCheckReserve
d9fa0 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43  dLock      /* xC
d9fb0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
d9fc0 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e   method */.).#en
d9fd0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  dif../*.** The p
d9fe0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 6d 65 74  roxy locking met
d9ff0 68 6f 64 20 69 73 20 61 20 22 73 75 70 65 72 2d  hod is a "super-
da000 6d 65 74 68 6f 64 22 20 69 6e 20 74 68 65 20 73  method" in the s
da010 65 6e 73 65 20 74 68 61 74 20 69 74 0a 2a 2a 20  ense that it.** 
da020 6f 70 65 6e 73 20 73 65 63 6f 6e 64 61 72 79 20  opens secondary 
da030 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
da040 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 61   for the conch a
da050 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 6e  nd lock files an
da060 64 0a 2a 2a 20 69 74 20 75 73 65 73 20 70 72 6f  d.** it uses pro
da070 78 79 2c 20 64 6f 74 2d 66 69 6c 65 2c 20 41 46  xy, dot-file, AF
da080 50 2c 20 61 6e 64 20 66 6c 6f 63 6b 28 29 20 6c  P, and flock() l
da090 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f  ocking methods o
da0a0 6e 20 74 68 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e  n those.** secon
da0b0 64 61 72 79 20 66 69 6c 65 73 2e 20 20 46 6f 72  dary files.  For
da0c0 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68   this reason, th
da0d0 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20  e division that 
da0e0 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 70 72  implements.** pr
da0f0 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c  oxy locking is l
da100 6f 63 61 74 65 64 20 6d 75 63 68 20 66 75 72 74  ocated much furt
da110 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20  her down in the 
da120 66 69 6c 65 2e 20 20 42 75 74 20 77 65 20 6e 65  file.  But we ne
da130 65 64 0a 2a 2a 20 74 6f 20 67 6f 20 61 68 65 61  ed.** to go ahea
da140 64 20 61 6e 64 20 64 65 66 69 6e 65 20 74 68 65  d and define the
da150 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
da160 6f 64 73 20 61 6e 64 20 66 69 6e 64 65 72 20 66  ods and finder f
da170 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70  unction.** for p
da180 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 68 65 72  roxy locking her
da190 65 2e 20 20 53 6f 20 77 65 20 66 6f 72 77 61 72  e.  So we forwar
da1a0 64 20 64 65 63 6c 61 72 65 20 74 68 65 20 49 2f  d declare the I/
da1b0 4f 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69  O methods..*/.#i
da1c0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  f defined(__APPL
da1d0 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45  E__) && SQLITE_E
da1e0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
da1f0 59 4c 45 0a 73 74 61 74 69 63 20 69 6e 74 20 70  YLE.static int p
da200 72 6f 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65  roxyClose(sqlite
da210 33 5f 66 69 6c 65 2a 29 3b 0a 73 74 61 74 69 63  3_file*);.static
da220 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73   int proxyLock(s
da230 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
da240 74 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70  t);.static int p
da250 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  roxyUnlock(sqlit
da260 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a  e3_file*, int);.
da270 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
da280 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
da290 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  k(sqlite3_file*,
da2a0 20 69 6e 74 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44   int*);.IOMETHOD
da2b0 53 28 0a 20 20 70 72 6f 78 79 49 6f 46 69 6e 64  S(.  proxyIoFind
da2c0 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  er,            /
da2d0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f  * Finder functio
da2e0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78  n name */.  prox
da2f0 79 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20  yIoMethods,     
da300 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
da310 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
da320 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f  ct name */.  pro
da330 78 79 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  xyClose,        
da340 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
da350 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f   method */.  pro
da360 78 79 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  xyLock,         
da370 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
da380 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78  method */.  prox
da390 79 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  yUnlock,        
da3a0 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
da3b0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f   method */.  pro
da3c0 78 79 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  xyCheckReservedL
da3d0 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b  ock    /* xCheck
da3e0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74  ReservedLock met
da3f0 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a  hod */.).#endif.
da400 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  ..#if defined(__
da410 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49  APPLE__) && SQLI
da420 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
da430 47 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54  G_STYLE./* .** T
da440 68 69 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e  his "finder" fun
da450 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74  ction attempts t
da460 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
da470 62 65 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72  best locking str
da480 61 74 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68  ategy .** for th
da490 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
da4a0 22 66 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20  "filePath".  It 
da4b0 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65  then returns the
da4c0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
da4d0 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68  ods.** object th
da4e0 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
da4f0 61 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a  at strategy..**.
da500 2a 2a 20 54 68 69 73 20 69 73 20 66 6f 72 20 4d  ** This is for M
da510 61 63 4f 53 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73  acOSX only..*/.s
da520 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
da530 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
da540 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72  autolockIoFinder
da550 49 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68  Impl(.  const ch
da560 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20  ar *filePath,   
da570 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20   /* name of the 
da580 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
da590 0a 20 20 69 6e 74 20 66 64 20 20 20 20 20 20 20  .  int fd       
da5a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
da5b0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f  ile descriptor o
da5c0 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62  pen on the datab
da5d0 61 73 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20  ase file */.){. 
da5e0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74   static const st
da5f0 72 75 63 74 20 4d 61 70 70 69 6e 67 20 7b 0a 20  ruct Mapping {. 
da600 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
da610 46 69 6c 65 73 79 73 74 65 6d 3b 20 20 20 20 20  Filesystem;     
da620 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
da630 73 79 73 74 65 6d 20 74 79 70 65 20 6e 61 6d 65  system type name
da640 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 73 71   */.    const sq
da650 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
da660 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20 20 2f 2a   *pMethods;   /*
da670 20 41 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63   Appropriate loc
da680 6b 69 6e 67 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  king method */. 
da690 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 7b 0a 20 20   } aMap[] = {.  
da6a0 20 20 7b 20 22 68 66 73 22 2c 20 20 20 20 26 70    { "hfs",    &p
da6b0 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c  osixIoMethods },
da6c0 0a 20 20 20 20 7b 20 22 75 66 73 22 2c 20 20 20  .    { "ufs",   
da6d0 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73   &posixIoMethods
da6e0 20 7d 2c 0a 20 20 20 20 7b 20 22 61 66 70 66 73   },.    { "afpfs
da6f0 22 2c 20 20 26 61 66 70 49 6f 4d 65 74 68 6f 64  ",  &afpIoMethod
da700 73 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c 49  s },.#ifdef SQLI
da710 54 45 5f 45 4e 41 42 4c 45 5f 41 46 50 5f 4c 4f  TE_ENABLE_AFP_LO
da720 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 20 20 7b 20  CKING_SMB.    { 
da730 22 73 6d 62 66 73 22 2c 20 20 26 61 66 70 49 6f  "smbfs",  &afpIo
da740 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65 6c 73 65  Methods },.#else
da750 0a 20 20 20 20 7b 20 22 73 6d 62 66 73 22 2c 20  .    { "smbfs", 
da760 20 26 66 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73   &flockIoMethods
da770 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b   },.#endif.    {
da780 20 22 77 65 62 64 61 76 22 2c 20 26 6e 6f 6c 6f   "webdav", &nolo
da790 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20  ckIoMethods },. 
da7a0 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20 7d 3b     { 0, 0 }.  };
da7b0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
da7c0 63 74 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f  ct statfs fsInfo
da7d0 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b  ;.  struct flock
da7e0 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66   lockInfo;..  if
da7f0 28 20 21 66 69 6c 65 50 61 74 68 20 29 7b 0a 20  ( !filePath ){. 
da800 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 61 74     /* If filePat
da810 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61  h==NULL that mea
da820 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e  ns we are dealin
da830 67 20 77 69 74 68 20 61 20 74 72 61 6e 73 69 65  g with a transie
da840 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74  nt file.    ** t
da850 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  hat does not nee
da860 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20  d to be locked. 
da870 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 26 6e  */.    return &n
da880 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a  olockIoMethods;.
da890 20 20 7d 0a 20 20 69 66 28 20 73 74 61 74 66 73    }.  if( statfs
da8a0 28 66 69 6c 65 50 61 74 68 2c 20 26 66 73 49 6e  (filePath, &fsIn
da8b0 66 6f 29 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20  fo) != -1 ){.   
da8c0 20 69 66 28 20 66 73 49 6e 66 6f 2e 66 5f 66 6c   if( fsInfo.f_fl
da8d0 61 67 73 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59  ags & MNT_RDONLY
da8e0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
da8f0 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64   &nolockIoMethod
da900 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  s;.    }.    for
da910 28 69 3d 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46  (i=0; aMap[i].zF
da920 69 6c 65 73 79 73 74 65 6d 3b 20 69 2b 2b 29 7b  ilesystem; i++){
da930 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d  .      if( strcm
da940 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70  p(fsInfo.f_fstyp
da950 65 6e 61 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a  ename, aMap[i].z
da960 46 69 6c 65 73 79 73 74 65 6d 29 3d 3d 30 20 29  Filesystem)==0 )
da970 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
da980 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 74 68 6f 64   aMap[i].pMethod
da990 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  s;.      }.    }
da9a0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75  .  }..  /* Defau
da9b0 6c 74 20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73  lt case. Handles
da9c0 2c 20 61 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73  , amongst others
da9d0 2c 20 22 6e 66 73 22 2e 0a 20 20 2a 2a 20 54 65  , "nfs"..  ** Te
da9e0 73 74 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f  st byte-range lo
da9f0 63 6b 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 29  ck using fcntl()
daa00 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 20 73 75  . If the call su
daa10 63 63 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73  cceeds, .  ** as
daa20 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 66 69  sume that the fi
daa30 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72  le-system suppor
daa40 74 73 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c  ts POSIX style l
daa50 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f  ocks. .  */.  lo
daa60 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31  ckInfo.l_len = 1
daa70 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73  ;.  lockInfo.l_s
daa80 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b  tart = 0;.  lock
daa90 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  Info.l_whence = 
daaa0 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b  SEEK_SET;.  lock
daab0 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f  Info.l_type = F_
daac0 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e  RDLCK;.  if( fcn
daad0 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20  tl(fd, F_GETLK, 
daae0 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29  &lockInfo)!=-1 )
daaf0 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70   {.    return &p
dab00 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  osixIoMethods;. 
dab10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
dab20 72 6e 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74  rn &dotlockIoMet
dab30 68 6f 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74  hods;.  }.}.stat
dab40 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
dab50 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63  _io_methods *(*c
dab60 6f 6e 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46  onst autolockIoF
dab70 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 68 61  inder)(const cha
dab80 72 2a 2c 69 6e 74 29 0a 20 20 20 20 20 20 20 20  r*,int).        
dab90 3d 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64  = autolockIoFind
daba0 65 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20  erImpl;..#endif 
dabb0 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  /* defined(__APP
dabc0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f  LE__) && SQLITE_
dabd0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
dabe0 54 59 4c 45 20 2a 2f 0a 0a 23 69 66 20 4f 53 5f  TYLE */..#if OS_
dabf0 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54  VXWORKS && SQLIT
dac00 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
dac10 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68  _STYLE./* .** Th
dac20 69 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63  is "finder" func
dac30 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f  tion attempts to
dac40 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 62   determine the b
dac50 65 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61  est locking stra
dac60 74 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65  tegy .** for the
dac70 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 22   database file "
dac80 66 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 74  filePath".  It t
dac90 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  hen returns the 
daca0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
dacb0 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61  ds.** object tha
dacc0 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61  t implements tha
dacd0 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a  t strategy..**.*
dace0 2a 20 54 68 69 73 20 69 73 20 66 6f 72 20 56 58  * This is for VX
dacf0 57 6f 72 6b 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73  Works only..*/.s
dad00 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
dad10 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
dad20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72  autolockIoFinder
dad30 49 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68  Impl(.  const ch
dad40 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20  ar *filePath,   
dad50 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20   /* name of the 
dad60 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
dad70 0a 20 20 69 6e 74 20 66 64 20 20 20 20 20 20 20  .  int fd       
dad80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
dad90 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f  ile descriptor o
dada0 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62  pen on the datab
dadb0 61 73 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20  ase file */.){. 
dadc0 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f   struct flock lo
dadd0 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21  ckInfo;..  if( !
dade0 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20  filePath ){.    
dadf0 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d  /* If filePath==
dae00 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20  NULL that means 
dae10 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77  we are dealing w
dae20 69 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20  ith a transient 
dae30 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74  file.    ** that
dae40 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
dae50 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a  o be locked. */.
dae60 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f      return &nolo
dae70 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d  ckIoMethods;.  }
dae80 0a 0a 20 20 2f 2a 20 54 65 73 74 20 69 66 20 66  ..  /* Test if f
dae90 63 6e 74 6c 28 29 20 69 73 20 73 75 70 70 6f 72  cntl() is suppor
daea0 74 65 64 20 61 6e 64 20 75 73 65 20 50 4f 53 49  ted and use POSI
daeb0 58 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e 0a 20  X style locks.. 
daec0 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61   ** Otherwise fa
daed0 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 6e  ll back to the n
daee0 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6d  amed semaphore m
daef0 65 74 68 6f 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f  ethod..  */.  lo
daf00 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31  ckInfo.l_len = 1
daf10 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73  ;.  lockInfo.l_s
daf20 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b  tart = 0;.  lock
daf30 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  Info.l_whence = 
daf40 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b  SEEK_SET;.  lock
daf50 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f  Info.l_type = F_
daf60 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e  RDLCK;.  if( fcn
daf70 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20  tl(fd, F_GETLK, 
daf80 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29  &lockInfo)!=-1 )
daf90 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70   {.    return &p
dafa0 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  osixIoMethods;. 
dafb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
dafc0 72 6e 20 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73  rn &semIoMethods
dafd0 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63  ;.  }.}.static c
dafe0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
daff0 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74  methods *(*const
db000 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65   autolockIoFinde
db010 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  r)(const char*,i
db020 6e 74 29 0a 20 20 20 20 20 20 20 20 3d 20 61 75  nt).        = au
db030 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d  tolockIoFinderIm
db040 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f  pl;..#endif /* O
db050 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c  S_VXWORKS && SQL
db060 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
db070 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a  NG_STYLE */../*.
db080 2a 2a 20 41 6e 20 61 62 73 74 72 61 63 74 20 74  ** An abstract t
db090 79 70 65 20 66 6f 72 20 61 20 70 6f 69 6e 74 65  ype for a pointe
db0a0 72 20 74 6f 20 61 20 49 4f 20 6d 65 74 68 6f 64  r to a IO method
db0b0 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e   finder function
db0c0 3a 0a 2a 2f 0a 74 79 70 65 64 65 66 20 63 6f 6e  :.*/.typedef con
db0d0 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
db0e0 74 68 6f 64 73 20 2a 28 2a 66 69 6e 64 65 72 5f  thods *(*finder_
db0f0 74 79 70 65 29 28 63 6f 6e 73 74 20 63 68 61 72  type)(const char
db100 2a 2c 69 6e 74 29 3b 0a 0a 0a 2f 2a 2a 2a 2a 2a  *,int);.../*****
db110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db150 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
db160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db170 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 73  **** sqlite3_vfs
db180 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a   methods *******
db190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db1a0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
db1b0 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69   division contai
db1c0 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ns the implement
db1d0 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64 73  ation of methods
db1e0 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74   on the.** sqlit
db1f0 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a 2a  e3_vfs object..*
db200 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  /../*.** Initial
db210 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ize the contents
db220 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65   of the unixFile
db230 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74   structure point
db240 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a 2a 2f  ed to by pId..*/
db250 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 6c  .static int fill
db260 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 73 71  InUnixFile(.  sq
db270 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
db280 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
db290 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a   to vfs object *
db2a0 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20 20  /.  int h,      
db2b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
db2c0 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70  pen file descrip
db2d0 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65 69 6e  tor of file bein
db2e0 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e  g opened */.  in
db2f0 74 20 64 69 72 66 64 2c 20 20 20 20 20 20 20 20  t dirfd,        
db300 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f        /* Directo
db310 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ry file descript
db320 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  or */.  sqlite3_
db330 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20 20 20  file *pId,      
db340 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68 65 20  /* Write to the 
db350 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75  unixFile structu
db360 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e  re here */.  con
db370 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
db380 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  me,  /* Name of 
db390 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 20 6f  the file being o
db3a0 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  pened */.  int n
db3b0 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  oLock,          
db3c0 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63 6b 69     /* Omit locki
db3d0 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20  ng if true */.  
db3e0 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20 20  int isDelete    
db3f0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74          /* Delet
db400 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72  e on close if tr
db410 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  ue */.){.  const
db420 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
db430 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53 74 79  ods *pLockingSty
db440 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  le;.  unixFile *
db450 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 6c 65  pNew = (unixFile
db460 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20 72 63   *)pId;.  int rc
db470 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
db480 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70   assert( pNew->p
db490 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20 20  Lock==NULL );.  
db4a0 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 4f  assert( pNew->pO
db4b0 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20  pen==NULL );..  
db4c0 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69 73 44  /* Parameter isD
db4d0 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20 75 73  elete is only us
db4e0 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 0a 20  ed on vxworks.. 
db4f0 20 2a 2a 20 45 78 70 72 65 73 73 20 74 68 69 73   ** Express this
db500 20 65 78 70 6c 69 63 69 74 6c 79 20 68 65 72 65   explicitly here
db510 20 74 6f 20 70 72 65 76 65 6e 74 20 63 6f 6d 70   to prevent comp
db520 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 0a 20 20  iler warnings.  
db530 2a 2a 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20  ** about unused 
db540 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f  parameters..  */
db550 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53  .#if !OS_VXWORKS
db560 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
db570 54 45 52 28 69 73 44 65 6c 65 74 65 29 3b 0a 23  TER(isDelete);.#
db580 65 6e 64 69 66 0a 0a 20 20 4f 53 54 52 41 43 45  endif..  OSTRACE
db590 33 28 22 4f 50 45 4e 20 20 20 20 25 2d 33 64 20  3("OPEN    %-3d 
db5a0 25 73 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e  %s\n", h, zFilen
db5b0 61 6d 65 29 3b 20 20 20 20 0a 20 20 70 4e 65 77  ame);    .  pNew
db5c0 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 4e 65 77 2d  ->h = h;.  pNew-
db5d0 3e 64 69 72 66 64 20 3d 20 64 69 72 66 64 3b 0a  >dirfd = dirfd;.
db5e0 20 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 70    SET_THREADID(p
db5f0 4e 65 77 29 3b 0a 0a 23 69 66 20 4f 53 5f 56 58  New);..#if OS_VX
db600 57 4f 52 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49  WORKS.  pNew->pI
db610 64 20 3d 20 76 78 77 6f 72 6b 73 46 69 6e 64 46  d = vxworksFindF
db620 69 6c 65 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29  ileId(zFilename)
db630 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49  ;.  if( pNew->pI
db640 64 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f  d==0 ){.    noLo
db650 63 6b 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d  ck = 1;.    rc =
db660 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
db670 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28   }.#endif..  if(
db680 20 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70   noLock ){.    p
db690 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26  LockingStyle = &
db6a0 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b  nolockIoMethods;
db6b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c  .  }else{.    pL
db6c0 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a  ockingStyle = (*
db6d0 2a 28 66 69 6e 64 65 72 5f 74 79 70 65 2a 29 70  *(finder_type*)p
db6e0 56 66 73 2d 3e 70 41 70 70 44 61 74 61 29 28 7a  Vfs->pAppData)(z
db6f0 46 69 6c 65 6e 61 6d 65 2c 20 68 29 3b 0a 23 69  Filename, h);.#i
db700 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
db710 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20  LOCKING_STYLE.  
db720 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c 65    /* Cache zFile
db730 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63 6b  name in the lock
db740 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46 50  ing context (AFP
db750 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76 65   and dotlock ove
db760 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20 2a  rride) for.    *
db770 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74 69  * proxyLock acti
db780 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69 62  vation is possib
db790 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78 79  le (remote proxy
db7a0 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62 20   is based on db 
db7b0 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46 69  name).    ** zFi
db7c0 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20 76  lename remains v
db7d0 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65 20  alid until file 
db7e0 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73 75  is closed, to su
db7f0 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e 65  pport */.    pNe
db800 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  w->lockingContex
db810 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c 65  t = (void*)zFile
db820 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  name;.#endif.  }
db830 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67  ..  if( pLocking
db840 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78 49  Style == &posixI
db850 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
db860 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
db870 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
db880 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70  ockInfo(pNew, &p
db890 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65  New->pLock, &pNe
db8a0 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 75  w->pOpen);.    u
db8b0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
db8c0 0a 20 20 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45  .  }..#if SQLITE
db8d0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
db8e0 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64  STYLE && defined
db8f0 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 65 6c  (__APPLE__).  el
db900 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53  se if( pLockingS
db910 74 79 6c 65 20 3d 3d 20 26 61 66 70 49 6f 4d 65  tyle == &afpIoMe
db920 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
db930 41 46 50 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73  AFP locking uses
db940 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73   the file path s
db950 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65  o it needs to be
db960 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20   included in.   
db970 20 2a 2a 20 74 68 65 20 61 66 70 4c 6f 63 6b 69   ** the afpLocki
db980 6e 67 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a  ngContext..    *
db990 2f 0a 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e 67  /.    afpLocking
db9a0 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20  Context *pCtx;. 
db9b0 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67     pNew->locking
db9c0 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 20 3d  Context = pCtx =
db9d0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
db9e0 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29   sizeof(*pCtx) )
db9f0 3b 0a 20 20 20 20 69 66 28 20 70 43 74 78 3d 3d  ;.    if( pCtx==
dba00 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
dba10 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
dba20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
dba30 2a 20 4e 42 3a 20 7a 46 69 6c 65 6e 61 6d 65 20  * NB: zFilename 
dba40 65 78 69 73 74 73 20 61 6e 64 20 72 65 6d 61 69  exists and remai
dba50 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74  ns valid until t
dba60 68 65 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65  he file is close
dba70 64 0a 20 20 20 20 20 20 2a 2a 20 61 63 63 6f 72  d.      ** accor
dba80 64 69 6e 67 20 74 6f 20 72 65 71 75 69 72 65 6d  ding to requirem
dba90 65 6e 74 20 46 31 31 31 34 31 2e 20 20 53 6f 20  ent F11141.  So 
dbaa0 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74  we do not need t
dbab0 6f 20 6d 61 6b 65 20 61 0a 20 20 20 20 20 20 2a  o make a.      *
dbac0 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 66 69  * copy of the fi
dbad0 6c 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20  lename. */.     
dbae0 20 70 43 74 78 2d 3e 64 62 50 61 74 68 20 3d 20   pCtx->dbPath = 
dbaf0 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20  zFilename;.     
dbb00 20 73 72 61 6e 64 6f 6d 64 65 76 28 29 3b 0a 20   srandomdev();. 
dbb10 20 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75       unixEnterMu
dbb20 74 65 78 28 29 3b 0a 20 20 20 20 20 20 72 63 20  tex();.      rc 
dbb30 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70  = findLockInfo(p
dbb40 4e 65 77 2c 20 4e 55 4c 4c 2c 20 26 70 4e 65 77  New, NULL, &pNew
dbb50 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20  ->pOpen);.      
dbb60 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
dbb70 3b 20 20 20 20 20 20 20 20 0a 20 20 20 20 7d 0a  ;        .    }.
dbb80 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 65 6c    }.#endif..  el
dbb90 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53  se if( pLockingS
dbba0 74 79 6c 65 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b  tyle == &dotlock
dbbb0 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  IoMethods ){.   
dbbc0 20 2f 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 6b   /* Dotfile lock
dbbd0 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 69 6c  ing uses the fil
dbbe0 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65  e path so it nee
dbbf0 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65  ds to be include
dbc00 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  d in.    ** the 
dbc10 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f  dotlockLockingCo
dbc20 6e 74 65 78 74 20 0a 20 20 20 20 2a 2f 0a 20 20  ntext .    */.  
dbc30 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c    char *zLockFil
dbc40 65 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 6c 65  e;.    int nFile
dbc50 6e 61 6d 65 3b 0a 20 20 20 20 6e 46 69 6c 65 6e  name;.    nFilen
dbc60 61 6d 65 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  ame = (int)strle
dbc70 6e 28 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 36  n(zFilename) + 6
dbc80 3b 0a 20 20 20 20 7a 4c 6f 63 6b 46 69 6c 65 20  ;.    zLockFile 
dbc90 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
dbca0 33 5f 6d 61 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61  3_malloc(nFilena
dbcb0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 7a 4c 6f  me);.    if( zLo
dbcc0 63 6b 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20  ckFile==0 ){.   
dbcd0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
dbce0 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  OMEM;.    }else{
dbcf0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
dbd00 6e 70 72 69 6e 74 66 28 6e 46 69 6c 65 6e 61 6d  nprintf(nFilenam
dbd10 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65 2c 20 22 25  e, zLockFile, "%
dbd20 73 22 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49  s" DOTLOCK_SUFFI
dbd30 58 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  X, zFilename);. 
dbd40 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 6c     }.    pNew->l
dbd50 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
dbd60 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a  zLockFile;.  }..
dbd70 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
dbd80 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69   else if( pLocki
dbd90 6e 67 53 74 79 6c 65 20 3d 3d 20 26 73 65 6d 49  ngStyle == &semI
dbda0 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
dbdb0 2f 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f  /* Named semapho
dbdc0 72 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20  re locking uses 
dbdd0 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f  the file path so
dbde0 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 0a   it needs to be.
dbdf0 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20      ** included 
dbe00 69 6e 20 74 68 65 20 73 65 6d 4c 6f 63 6b 69 6e  in the semLockin
dbe10 67 43 6f 6e 74 65 78 74 0a 20 20 20 20 2a 2f 0a  gContext.    */.
dbe20 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74      unixEnterMut
dbe30 65 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 66  ex();.    rc = f
dbe40 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77  indLockInfo(pNew
dbe50 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20  , &pNew->pLock, 
dbe60 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20  &pNew->pOpen);. 
dbe70 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49     if( (rc==SQLI
dbe80 54 45 5f 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d  TE_OK) && (pNew-
dbe90 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55  >pOpen->pSem==NU
dbea0 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 63 68 61  LL) ){.      cha
dbeb0 72 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20 70 4e  r *zSemName = pN
dbec0 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e  ew->pOpen->aSemN
dbed0 61 6d 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ame;.      int n
dbee0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
dbef0 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
dbf00 48 4e 41 4d 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c  HNAME, zSemName,
dbf10 20 22 25 73 2e 73 65 6d 22 2c 0a 20 20 20 20 20   "%s.sem",.     
dbf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dbf30 20 20 70 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43 61    pNew->pId->zCa
dbf40 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20  nonicalName);.  
dbf50 20 20 20 20 66 6f 72 28 20 6e 3d 30 3b 20 7a 53      for( n=0; zS
dbf60 65 6d 4e 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29  emName[n]; n++ )
dbf70 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 65  .        if( zSe
dbf80 6d 4e 61 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20  mName[n]=='/' ) 
dbf90 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f  zSemName[n] = '_
dbfa0 27 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  ';.      pNew->p
dbfb0 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 20 73 65 6d  Open->pSem = sem
dbfc0 5f 6f 70 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20  _open(zSemName, 
dbfd0 4f 5f 43 52 45 41 54 2c 20 30 36 36 36 2c 20 31  O_CREAT, 0666, 1
dbfe0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65  );.      if( pNe
dbff0 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d  w->pOpen->pSem =
dc000 3d 20 53 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a  = SEM_FAILED ){.
dc010 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
dc020 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
dc030 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e     pNew->pOpen->
dc040 61 53 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c  aSemName[0] = '\
dc050 30 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0';.      }.    
dc060 7d 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d  }.    unixLeaveM
dc070 75 74 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64  utex();.  }.#end
dc080 69 66 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61  if.  .  pNew->la
dc090 73 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66  stErrno = 0;.#if
dc0a0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66   OS_VXWORKS.  if
dc0b0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
dc0c0 29 7b 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46  ){.    unlink(zF
dc0d0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 73  ilename);.    is
dc0e0 44 65 6c 65 74 65 20 3d 20 30 3b 0a 20 20 7d 0a  Delete = 0;.  }.
dc0f0 20 20 70 4e 65 77 2d 3e 69 73 44 65 6c 65 74 65    pNew->isDelete
dc100 20 3d 20 69 73 44 65 6c 65 74 65 3b 0a 23 65 6e   = isDelete;.#en
dc110 64 69 66 0a 20 20 69 66 28 20 72 63 21 3d 53 51  dif.  if( rc!=SQ
dc120 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
dc130 66 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63 6c  f( dirfd>=0 ) cl
dc140 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a 20 73  ose(dirfd); /* s
dc150 69 6c 65 6e 74 20 6c 65 61 6b 20 69 66 20 66 61  ilent leak if fa
dc160 69 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e 20 65  il, already in e
dc170 72 72 6f 72 20 2a 2f 0a 20 20 20 20 63 6c 6f 73  rror */.    clos
dc180 65 28 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  e(h);.  }else{. 
dc190 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64     pNew->pMethod
dc1a0 20 3d 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65   = pLockingStyle
dc1b0 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65  ;.    OpenCounte
dc1c0 72 28 2b 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74  r(+1);.  }.  ret
dc1d0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
dc1e0 20 4f 70 65 6e 20 61 20 66 69 6c 65 20 64 65 73   Open a file des
dc1f0 63 72 69 70 74 6f 72 20 74 6f 20 74 68 65 20 64  criptor to the d
dc200 69 72 65 63 74 6f 72 79 20 63 6f 6e 74 61 69 6e  irectory contain
dc210 69 6e 67 20 66 69 6c 65 20 7a 46 69 6c 65 6e 61  ing file zFilena
dc220 6d 65 2e 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  me..** If succes
dc230 73 66 75 6c 2c 20 2a 70 46 64 20 69 73 20 73 65  sful, *pFd is se
dc240 74 20 74 6f 20 74 68 65 20 6f 70 65 6e 65 64 20  t to the opened 
dc250 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
dc260 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  and.** SQLITE_OK
dc270 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
dc280 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
dc290 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f  , either SQLITE_
dc2a0 4e 4f 4d 45 4d 0a 2a 2a 20 6f 72 20 53 51 4c 49  NOMEM.** or SQLI
dc2b0 54 45 5f 43 41 4e 54 4f 50 45 4e 20 69 73 20 72  TE_CANTOPEN is r
dc2c0 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 46 64  eturned and *pFd
dc2d0 20 69 73 20 73 65 74 20 74 6f 20 61 6e 20 75 6e   is set to an un
dc2e0 64 65 66 69 6e 65 64 0a 2a 2a 20 76 61 6c 75 65  defined.** value
dc2f0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ..**.** If SQLIT
dc300 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
dc310 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20  , the caller is 
dc320 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
dc330 63 6c 6f 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66  closing.** the f
dc340 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
dc350 70 46 64 20 75 73 69 6e 67 20 63 6c 6f 73 65 28  pFd using close(
dc360 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
dc370 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 63   openDirectory(c
dc380 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
dc390 6e 61 6d 65 2c 20 69 6e 74 20 2a 70 46 64 29 7b  name, int *pFd){
dc3a0 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
dc3b0 20 66 64 20 3d 20 2d 31 3b 0a 20 20 63 68 61 72   fd = -1;.  char
dc3c0 20 7a 44 69 72 6e 61 6d 65 5b 4d 41 58 5f 50 41   zDirname[MAX_PA
dc3d0 54 48 4e 41 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71  THNAME+1];..  sq
dc3e0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d  lite3_snprintf(M
dc3f0 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 44 69  AX_PATHNAME, zDi
dc400 72 6e 61 6d 65 2c 20 22 25 73 22 2c 20 7a 46 69  rname, "%s", zFi
dc410 6c 65 6e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69  lename);.  for(i
dc420 69 3d 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 44  i=(int)strlen(zD
dc430 69 72 6e 61 6d 65 29 3b 20 69 69 3e 31 20 26 26  irname); ii>1 &&
dc440 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 21 3d 27   zDirname[ii]!='
dc450 2f 27 3b 20 69 69 2d 2d 29 3b 0a 20 20 69 66 28  /'; ii--);.  if(
dc460 20 69 69 3e 30 20 29 7b 0a 20 20 20 20 7a 44 69   ii>0 ){.    zDi
dc470 72 6e 61 6d 65 5b 69 69 5d 20 3d 20 27 5c 30 27  rname[ii] = '\0'
dc480 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28  ;.    fd = open(
dc490 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e  zDirname, O_RDON
dc4a0 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c 20 30 29 3b  LY|O_BINARY, 0);
dc4b0 0a 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29  .    if( fd>=0 )
dc4c0 7b 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f 45  {.#ifdef FD_CLOE
dc4d0 58 45 43 0a 20 20 20 20 20 20 66 63 6e 74 6c 28  XEC.      fcntl(
dc4e0 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e  fd, F_SETFD, fcn
dc4f0 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20  tl(fd, F_GETFD, 
dc500 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29  0) | FD_CLOEXEC)
dc510 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 4f  ;.#endif.      O
dc520 53 54 52 41 43 45 33 28 22 4f 50 45 4e 44 49 52  STRACE3("OPENDIR
dc530 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 66 64 2c   %-3d %s\n", fd,
dc540 20 7a 44 69 72 6e 61 6d 65 29 3b 0a 20 20 20 20   zDirname);.    
dc550 7d 0a 20 20 7d 0a 20 20 2a 70 46 64 20 3d 20 66  }.  }.  *pFd = f
dc560 64 3b 0a 20 20 72 65 74 75 72 6e 20 28 66 64 3e  d;.  return (fd>
dc570 3d 30 3f 53 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c  =0?SQLITE_OK:SQL
dc580 49 54 45 5f 43 41 4e 54 4f 50 45 4e 29 3b 0a 7d  ITE_CANTOPEN);.}
dc590 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
dc5a0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
dc5b0 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a  name in zBuf.  z
dc5c0 42 75 66 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f  Buf must be allo
dc5d0 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  cated.** by the 
dc5e0 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 73 73 20  calling process 
dc5f0 61 6e 64 20 6d 75 73 74 20 62 65 20 62 69 67 20  and must be big 
dc600 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 61  enough to hold a
dc610 74 20 6c 65 61 73 74 0a 2a 2a 20 70 56 66 73 2d  t least.** pVfs-
dc620 3e 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74 65  >mxPathname byte
dc630 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
dc640 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74   getTempname(int
dc650 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
dc660 66 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  f){.  static con
dc670 73 74 20 63 68 61 72 20 2a 61 7a 44 69 72 73 5b  st char *azDirs[
dc680 5d 20 3d 20 7b 0a 20 20 20 20 20 30 2c 0a 20 20  ] = {.     0,.  
dc690 20 20 20 30 2c 0a 20 20 20 20 20 22 2f 76 61 72     0,.     "/var
dc6a0 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 75 73  /tmp",.     "/us
dc6b0 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 74  r/tmp",.     "/t
dc6c0 6d 70 22 2c 0a 20 20 20 20 20 22 2e 22 2c 0a 20  mp",.     ".",. 
dc6d0 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   };.  static con
dc6e0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
dc6f0 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20   zChars[] =.    
dc700 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f  "abcdefghijklmno
dc710 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20  pqrstuvwxyz".   
dc720 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e   "ABCDEFGHIJKLMN
dc730 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20  OPQRSTUVWXYZ".  
dc740 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a    "0123456789";.
dc750 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
dc760 2c 20 6a 3b 0a 20 20 73 74 72 75 63 74 20 73 74  , j;.  struct st
dc770 61 74 20 62 75 66 3b 0a 20 20 63 6f 6e 73 74 20  at buf;.  const 
dc780 63 68 61 72 20 2a 7a 44 69 72 20 3d 20 22 2e 22  char *zDir = "."
dc790 3b 0a 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 64  ;..  /* It's odd
dc7a0 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20   to simulate an 
dc7b0 69 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 62  io-error here, b
dc7c0 75 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20 69  ut really this i
dc7d0 73 20 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e  s just.  ** usin
dc7e0 67 20 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69  g the io-error i
dc7f0 6e 66 72 61 73 74 72 75 63 74 75 72 65 20 74 6f  nfrastructure to
dc800 20 74 65 73 74 20 74 68 61 74 20 53 51 4c 69 74   test that SQLit
dc810 65 20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a 20  e handles this. 
dc820 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 69   ** function fai
dc830 6c 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20 53 69  ling. .  */.  Si
dc840 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
dc850 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
dc860 52 52 20 29 3b 0a 0a 20 20 61 7a 44 69 72 73 5b  RR );..  azDirs[
dc870 30 5d 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d  0] = sqlite3_tem
dc880 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 69  p_directory;.  i
dc890 66 20 28 4e 55 4c 4c 20 3d 3d 20 61 7a 44 69 72  f (NULL == azDir
dc8a0 73 5b 31 5d 29 20 7b 0a 20 20 20 20 61 7a 44 69  s[1]) {.    azDi
dc8b0 72 73 5b 31 5d 20 3d 20 67 65 74 65 6e 76 28 22  rs[1] = getenv("
dc8c0 54 4d 50 44 49 52 22 29 3b 0a 20 20 7d 0a 20 20  TMPDIR");.  }.  
dc8d0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69  .  for(i=0; i<si
dc8e0 7a 65 6f 66 28 61 7a 44 69 72 73 29 2f 73 69 7a  zeof(azDirs)/siz
dc8f0 65 6f 66 28 61 7a 44 69 72 73 5b 30 5d 29 3b 20  eof(azDirs[0]); 
dc900 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 7a  i++){.    if( az
dc910 44 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e  Dirs[i]==0 ) con
dc920 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 73  tinue;.    if( s
dc930 74 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c 20 26  tat(azDirs[i], &
dc940 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  buf) ) continue;
dc950 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 49  .    if( !S_ISDI
dc960 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29  R(buf.st_mode) )
dc970 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
dc980 66 28 20 61 63 63 65 73 73 28 61 7a 44 69 72 73  f( access(azDirs
dc990 5b 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e 74 69  [i], 07) ) conti
dc9a0 6e 75 65 3b 0a 20 20 20 20 7a 44 69 72 20 3d 20  nue;.    zDir = 
dc9b0 61 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20 20 62  azDirs[i];.    b
dc9c0 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  reak;.  }..  /* 
dc9d0 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6f  Check that the o
dc9e0 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 20  utput buffer is 
dc9f0 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f 72  large enough for
dca00 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66   the temporary f
dca10 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20  ile .  ** name. 
dca20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 65  If it is not, re
dca30 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
dca40 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 73  R..  */.  if( (s
dca50 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 74  trlen(zDir) + st
dca60 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d 50  rlen(SQLITE_TEMP
dca70 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b 20  _FILE_PREFIX) + 
dca80 31 37 29 20 3e 3d 20 28 73 69 7a 65 5f 74 29 6e  17) >= (size_t)n
dca90 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Buf ){.    retur
dcaa0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
dcab0 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 73    }..  do{.    s
dcac0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
dcad0 6e 42 75 66 2d 31 37 2c 20 7a 42 75 66 2c 20 22  nBuf-17, zBuf, "
dcae0 25 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f  %s/"SQLITE_TEMP_
dcaf0 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 44 69  FILE_PREFIX, zDi
dcb00 72 29 3b 0a 20 20 20 20 6a 20 3d 20 28 69 6e 74  r);.    j = (int
dcb10 29 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20  )strlen(zBuf);. 
dcb20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f     sqlite3_rando
dcb30 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66 5b  mness(15, &zBuf[
dcb40 6a 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  j]);.    for(i=0
dcb50 3b 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b  ; i<15; i++, j++
dcb60 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a 5d  ){.      zBuf[j]
dcb70 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b   = (char)zChars[
dcb80 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72   ((unsigned char
dcb90 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f  )zBuf[j])%(sizeo
dcba0 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a  f(zChars)-1) ];.
dcbb0 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6a      }.    zBuf[j
dcbc0 5d 20 3d 20 30 3b 0a 20 20 7d 77 68 69 6c 65 28  ] = 0;.  }while(
dcbd0 20 61 63 63 65 73 73 28 7a 42 75 66 2c 30 29 3d   access(zBuf,0)=
dcbe0 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  =0 );.  return S
dcbf0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66  QLITE_OK;.}..#if
dcc00 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
dcc10 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20  OCKING_STYLE && 
dcc20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
dcc30 5f 29 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  _)./*.** Routine
dcc40 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 61 20   to transform a 
dcc50 75 6e 69 78 46 69 6c 65 20 69 6e 74 6f 20 61 20  unixFile into a 
dcc60 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 75 6e  proxy-locking un
dcc70 69 78 46 69 6c 65 2e 0a 2a 2a 20 49 6d 70 6c 65  ixFile..** Imple
dcc80 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 65  mentation in the
dcc90 20 70 72 6f 78 79 2d 6c 6f 63 6b 20 64 69 76 69   proxy-lock divi
dcca0 73 69 6f 6e 2c 20 62 75 74 20 75 73 65 64 20 62  sion, but used b
dccb0 79 20 75 6e 69 78 4f 70 65 6e 28 29 0a 2a 2a 20  y unixOpen().** 
dccc0 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52  if SQLITE_PREFER
dccd0 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 69  _PROXY_LOCKING i
dcce0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74  s defined..*/.st
dccf0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 72  atic int proxyTr
dcd00 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28  ansformUnixFile(
dcd10 75 6e 69 78 46 69 6c 65 2a 2c 20 63 6f 6e 73 74  unixFile*, const
dcd20 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a   char*);.#endif.
dcd30 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  ../*.** Open the
dcd40 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a 2a 2a 20   file zPath..** 
dcd50 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c 79 2c 20  .** Previously, 
dcd60 74 68 65 20 53 51 4c 69 74 65 20 4f 53 20 6c 61  the SQLite OS la
dcd70 79 65 72 20 75 73 65 64 20 74 68 72 65 65 20 66  yer used three f
dcd80 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70 6c 61 63  unctions in plac
dcd90 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f 6e 65  e of this.** one
dcda0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69  :.**.**     sqli
dcdb0 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 57 72 69  te3OsOpenReadWri
dcdc0 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20 73 71 6c  te();.**     sql
dcdd0 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 4f 6e  ite3OsOpenReadOn
dcde0 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20 73 71 6c  ly();.**     sql
dcdf0 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73  ite3OsOpenExclus
dce00 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65  ive();.**.** The
dce10 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 65 73 70  se calls corresp
dce20 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  ond to the follo
dce30 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  wing combination
dce40 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a  s of flags:.**.*
dce50 2a 20 20 20 20 20 52 65 61 64 57 72 69 74 65 28  *     ReadWrite(
dce60 29 20 2d 3e 20 20 20 20 20 28 52 45 41 44 57 52  ) ->     (READWR
dce70 49 54 45 20 7c 20 43 52 45 41 54 45 29 0a 2a 2a  ITE | CREATE).**
dce80 20 20 20 20 20 52 65 61 64 4f 6e 6c 79 28 29 20       ReadOnly() 
dce90 20 2d 3e 20 20 20 20 20 28 52 45 41 44 4f 4e 4c   ->     (READONL
dcea0 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70 65 6e 45  Y) .**     OpenE
dceb0 78 63 6c 75 73 69 76 65 28 29 20 2d 3e 20 28 52  xclusive() -> (R
dcec0 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 41 54  EADWRITE | CREAT
dced0 45 20 7c 20 45 58 43 4c 55 53 49 56 45 29 0a 2a  E | EXCLUSIVE).*
dcee0 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 4f 70 65  *.** The old Ope
dcef0 6e 45 78 63 6c 75 73 69 76 65 28 29 20 61 63 63  nExclusive() acc
dcf00 65 70 74 65 64 20 61 20 62 6f 6f 6c 65 61 6e 20  epted a boolean 
dcf10 61 72 67 75 6d 65 6e 74 20 2d 20 22 64 65 6c 46  argument - "delF
dcf20 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74 72 75 65  lag". If.** true
dcf30 2c 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 63  , the file was c
dcf40 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 62 65 20  onfigured to be 
dcf50 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65  automatically de
dcf60 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 0a 2a  leted when the.*
dcf70 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 63 6c  * file handle cl
dcf80 6f 73 65 64 2e 20 54 6f 20 61 63 68 69 65 76 65  osed. To achieve
dcf90 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 74   the same effect
dcfa0 20 75 73 69 6e 67 20 74 68 69 73 20 6e 65 77 20   using this new 
dcfb0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c 20 61  .** interface, a
dcfc0 64 64 20 74 68 65 20 44 45 4c 45 54 45 4f 4e 43  dd the DELETEONC
dcfd0 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20 74 68 6f  LOSE flag to tho
dcfe0 73 65 20 73 70 65 63 69 66 69 65 64 20 61 62 6f  se specified abo
dcff0 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 65 6e 45  ve for .** OpenE
dd000 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a 2f 0a 73  xclusive()..*/.s
dd010 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4f 70  tatic int unixOp
dd020 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  en(.  sqlite3_vf
dd030 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
dd040 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 66 6f     /* The VFS fo
dd050 72 20 77 68 69 63 68 20 74 68 69 73 20 69 73 20  r which this is 
dd060 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  the xOpen method
dd070 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
dd080 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20   *zPath,        
dd090 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d 65 20 6f     /* Pathname o
dd0a0 66 20 66 69 6c 65 20 74 6f 20 62 65 20 6f 70 65  f file to be ope
dd0b0 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ned */.  sqlite3
dd0c0 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20  _file *pFile,   
dd0d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c        /* The fil
dd0e0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20  e descriptor to 
dd0f0 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a  be filled in */.
dd100 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
dd110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
dd120 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 20 74 6f  * Input flags to
dd130 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6f 70 65   control the ope
dd140 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ning */.  int *p
dd150 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20 20  OutFlags        
dd160 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74         /* Output
dd170 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64 20   flags returned 
dd180 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 65 20 2a  to SQLite core *
dd190 2f 0a 29 7b 0a 20 20 69 6e 74 20 66 64 20 3d 20  /.){.  int fd = 
dd1a0 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  -1;             
dd1b0 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64         /* File d
dd1c0 65 73 63 72 69 70 74 6f 72 20 72 65 74 75 72 6e  escriptor return
dd1d0 65 64 20 62 79 20 6f 70 65 6e 28 29 20 2a 2f 0a  ed by open() */.
dd1e0 20 20 69 6e 74 20 64 69 72 66 64 20 3d 20 2d 31    int dirfd = -1
dd1f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
dd200 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69   /* Directory fi
dd210 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f  le descriptor */
dd220 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73  .  int openFlags
dd230 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
dd240 20 20 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 61    /* Flags to pa
dd250 73 73 20 74 6f 20 6f 70 65 6e 28 29 20 2a 2f 0a  ss to open() */.
dd260 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 66 6c    int eType = fl
dd270 61 67 73 26 30 78 46 46 46 46 46 46 30 30 3b 20  ags&0xFFFFFF00; 
dd280 20 2f 2a 20 54 79 70 65 20 6f 66 20 66 69 6c 65   /* Type of file
dd290 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e   to open */.  in
dd2a0 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20 20 20 20 20  t noLock;       
dd2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
dd2c0 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 6c 6f 63  True to omit loc
dd2d0 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20  king primitives 
dd2e0 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
dd2f0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 6e 74 20  LITE_OK;..  int 
dd300 69 73 45 78 63 6c 75 73 69 76 65 20 20 3d 20 28  isExclusive  = (
dd310 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
dd320 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a  PEN_EXCLUSIVE);.
dd330 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20    int isDelete  
dd340 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51     = (flags & SQ
dd350 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
dd360 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 69 6e 74 20  ONCLOSE);.  int 
dd370 69 73 43 72 65 61 74 65 20 20 20 20 20 3d 20 28  isCreate     = (
dd380 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
dd390 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 69  PEN_CREATE);.  i
dd3a0 6e 74 20 69 73 52 65 61 64 6f 6e 6c 79 20 20 20  nt isReadonly   
dd3b0 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  = (flags & SQLIT
dd3c0 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29  E_OPEN_READONLY)
dd3d0 3b 0a 20 20 69 6e 74 20 69 73 52 65 61 64 57 72  ;.  int isReadWr
dd3e0 69 74 65 20 20 3d 20 28 66 6c 61 67 73 20 26 20  ite  = (flags & 
dd3f0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
dd400 57 52 49 54 45 29 3b 0a 0a 20 20 2f 2a 20 49 66  WRITE);..  /* If
dd410 20 63 72 65 61 74 69 6e 67 20 61 20 6d 61 73 74   creating a mast
dd420 65 72 20 6f 72 20 6d 61 69 6e 2d 66 69 6c 65 20  er or main-file 
dd430 6a 6f 75 72 6e 61 6c 2c 20 74 68 69 73 20 66 75  journal, this fu
dd440 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 6f 70 65 6e  nction will open
dd450 0a 20 20 2a 2a 20 61 20 66 69 6c 65 2d 64 65 73  .  ** a file-des
dd460 63 72 69 70 74 6f 72 20 6f 6e 20 74 68 65 20 64  criptor on the d
dd470 69 72 65 63 74 6f 72 79 20 74 6f 6f 2e 20 54 68  irectory too. Th
dd480 65 20 66 69 72 73 74 20 74 69 6d 65 20 75 6e 69  e first time uni
dd490 78 53 79 6e 63 28 29 0a 20 20 2a 2a 20 69 73 20  xSync().  ** is 
dd4a0 63 61 6c 6c 65 64 20 74 68 65 20 64 69 72 65 63  called the direc
dd4b0 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69  tory file descri
dd4c0 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 66 73 79  ptor will be fsy
dd4d0 6e 63 28 29 65 64 20 61 6e 64 20 63 6c 6f 73 65  nc()ed and close
dd4e0 28 29 64 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ()d..  */.  int 
dd4f0 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20  isOpenDirectory 
dd500 3d 20 28 69 73 43 72 65 61 74 65 20 26 26 20 0a  = (isCreate && .
dd510 20 20 20 20 20 20 28 65 54 79 70 65 3d 3d 53 51        (eType==SQ
dd520 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52  LITE_OPEN_MASTER
dd530 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70  _JOURNAL || eTyp
dd540 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e==SQLITE_OPEN_M
dd550 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a 20 20 29  AIN_JOURNAL).  )
dd560 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 72 67 75 6d  ;..  /* If argum
dd570 65 6e 74 20 7a 50 61 74 68 20 69 73 20 61 20 4e  ent zPath is a N
dd580 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 69  ULL pointer, thi
dd590 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65  s function is re
dd5a0 71 75 69 72 65 64 20 74 6f 20 6f 70 65 6e 0a 20  quired to open. 
dd5b0 20 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 20   ** a temporary 
dd5c0 66 69 6c 65 2e 20 55 73 65 20 74 68 69 73 20 62  file. Use this b
dd5d0 75 66 66 65 72 20 74 6f 20 73 74 6f 72 65 20 74  uffer to store t
dd5e0 68 65 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 2e  he file name in.
dd5f0 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d  .  */.  char zTm
dd600 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41  pname[MAX_PATHNA
dd610 4d 45 2b 31 5d 3b 0a 20 20 63 6f 6e 73 74 20 63  ME+1];.  const c
dd620 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 7a 50 61  har *zName = zPa
dd630 74 68 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  th;..  /* Check 
dd640 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
dd650 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74 72 75  atements are tru
dd660 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  e: .  **.  **   
dd670 28 61 29 20 45 78 61 63 74 6c 79 20 6f 6e 65 20  (a) Exactly one 
dd680 6f 66 20 74 68 65 20 52 45 41 44 57 52 49 54 45  of the READWRITE
dd690 20 61 6e 64 20 52 45 41 44 4f 4e 4c 59 20 66 6c   and READONLY fl
dd6a0 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 2c  ags must be set,
dd6b0 20 61 6e 64 20 0a 20 20 2a 2a 20 20 20 28 62 29   and .  **   (b)
dd6c0 20 69 66 20 43 52 45 41 54 45 20 69 73 20 73 65   if CREATE is se
dd6d0 74 2c 20 74 68 65 6e 20 52 45 41 44 57 52 49 54  t, then READWRIT
dd6e0 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73  E must also be s
dd6f0 65 74 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 20 28  et, and.  **   (
dd700 63 29 20 69 66 20 45 58 43 4c 55 53 49 56 45 20  c) if EXCLUSIVE 
dd710 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43 52 45  is set, then CRE
dd720 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65  ATE must also be
dd730 20 73 65 74 2e 0a 20 20 2a 2a 20 20 20 28 64 29   set..  **   (d)
dd740 20 69 66 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53   if DELETEONCLOS
dd750 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43  E is set, then C
dd760 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20  REATE must also 
dd770 62 65 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 61  be set..  */.  a
dd780 73 73 65 72 74 28 28 69 73 52 65 61 64 6f 6e 6c  ssert((isReadonl
dd790 79 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 57 72  y==0 || isReadWr
dd7a0 69 74 65 3d 3d 30 29 20 26 26 20 28 69 73 52 65  ite==0) && (isRe
dd7b0 61 64 57 72 69 74 65 20 7c 7c 20 69 73 52 65 61  adWrite || isRea
dd7c0 64 6f 6e 6c 79 29 29 3b 0a 20 20 61 73 73 65 72  donly));.  asser
dd7d0 74 28 69 73 43 72 65 61 74 65 3d 3d 30 20 7c 7c  t(isCreate==0 ||
dd7e0 20 69 73 52 65 61 64 57 72 69 74 65 29 3b 0a 20   isReadWrite);. 
dd7f0 20 61 73 73 65 72 74 28 69 73 45 78 63 6c 75 73   assert(isExclus
dd800 69 76 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61  ive==0 || isCrea
dd810 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73  te);.  assert(is
dd820 44 65 6c 65 74 65 3d 3d 30 20 7c 7c 20 69 73 43  Delete==0 || isC
dd830 72 65 61 74 65 29 3b 0a 0a 20 20 2f 2a 20 54 68  reate);..  /* Th
dd840 65 20 6d 61 69 6e 20 44 42 2c 20 6d 61 69 6e 20  e main DB, main 
dd850 6a 6f 75 72 6e 61 6c 2c 20 61 6e 64 20 6d 61 73  journal, and mas
dd860 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20  ter journal are 
dd870 6e 65 76 65 72 20 61 75 74 6f 6d 61 74 69 63 61  never automatica
dd880 6c 6c 79 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64  lly.  ** deleted
dd890 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
dd8a0 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50  eType!=SQLITE_OP
dd8b0 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c 7c 20 21 69  EN_MAIN_DB || !i
dd8c0 73 44 65 6c 65 74 65 20 29 3b 0a 20 20 61 73 73  sDelete );.  ass
dd8d0 65 72 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49  ert( eType!=SQLI
dd8e0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
dd8f0 52 4e 41 4c 20 7c 7c 20 21 69 73 44 65 6c 65 74  RNAL || !isDelet
dd900 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65  e );.  assert( e
dd910 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45  Type!=SQLITE_OPE
dd920 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c  N_MASTER_JOURNAL
dd930 20 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b   || !isDelete );
dd940 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68  ..  /* Assert th
dd950 61 74 20 74 68 65 20 75 70 70 65 72 20 6c 61 79  at the upper lay
dd960 65 72 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f  er has set one o
dd970 66 20 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65  f the "file-type
dd980 22 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73  " flags. */.  as
dd990 73 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c  sert( eType==SQL
dd9a0 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
dd9b0 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d        || eType==
dd9c0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
dd9d0 5f 44 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65  _DB .       || e
dd9e0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
dd9f0 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c  N_MAIN_JOURNAL |
dda00 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
dda10 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41  OPEN_TEMP_JOURNA
dda20 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79  L .       || eTy
dda30 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
dda40 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20  SUBJOURNAL   || 
dda50 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
dda60 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41  EN_MASTER_JOURNA
dda70 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79  L .       || eTy
dda80 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
dda90 54 52 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29  TRANSIENT_DB.  )
ddaa0 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c  ;..  memset(pFil
ddab0 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69  e, 0, sizeof(uni
ddac0 78 46 69 6c 65 29 29 3b 0a 0a 20 20 69 66 28 20  xFile));..  if( 
ddad0 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 61 73  !zName ){.    as
ddae0 73 65 72 74 28 69 73 44 65 6c 65 74 65 20 26 26  sert(isDelete &&
ddaf0 20 21 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72   !isOpenDirector
ddb00 79 29 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74  y);.    rc = get
ddb10 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54  Tempname(MAX_PAT
ddb20 48 4e 41 4d 45 2b 31 2c 20 7a 54 6d 70 6e 61 6d  HNAME+1, zTmpnam
ddb30 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
ddb40 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
ddb50 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
ddb60 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20    }.    zName = 
ddb70 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20  zTmpname;.  }.. 
ddb80 20 69 66 28 20 69 73 52 65 61 64 6f 6e 6c 79 20   if( isReadonly 
ddb90 29 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20  )  openFlags |= 
ddba0 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20  O_RDONLY;.  if( 
ddbb0 69 73 52 65 61 64 57 72 69 74 65 20 29 20 6f 70  isReadWrite ) op
ddbc0 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 57  enFlags |= O_RDW
ddbd0 52 3b 0a 20 20 69 66 28 20 69 73 43 72 65 61 74  R;.  if( isCreat
ddbe0 65 20 29 20 20 20 20 6f 70 65 6e 46 6c 61 67 73  e )    openFlags
ddbf0 20 7c 3d 20 4f 5f 43 52 45 41 54 3b 0a 20 20 69   |= O_CREAT;.  i
ddc00 66 28 20 69 73 45 78 63 6c 75 73 69 76 65 20 29  f( isExclusive )
ddc10 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f   openFlags |= (O
ddc20 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57  _EXCL|O_NOFOLLOW
ddc30 29 3b 0a 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c  );.  openFlags |
ddc40 3d 20 28 4f 5f 4c 41 52 47 45 46 49 4c 45 7c 4f  = (O_LARGEFILE|O
ddc50 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 20 66 64 20  _BINARY);..  fd 
ddc60 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70  = open(zName, op
ddc70 65 6e 46 6c 61 67 73 2c 20 69 73 44 65 6c 65 74  enFlags, isDelet
ddc80 65 3f 30 36 30 30 3a 53 51 4c 49 54 45 5f 44 45  e?0600:SQLITE_DE
ddc90 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
ddca0 53 53 49 4f 4e 53 29 3b 0a 20 20 4f 53 54 52 41  SSIONS);.  OSTRA
ddcb0 43 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33  CE4("OPENX   %-3
ddcc0 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c  d %s 0%o\n", fd,
ddcd0 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67   zName, openFlag
ddce0 73 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 26  s);.  if( fd<0 &
ddcf0 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49 52 20  & errno!=EISDIR 
ddd00 26 26 20 69 73 52 65 61 64 57 72 69 74 65 20 26  && isReadWrite &
ddd10 26 20 21 69 73 45 78 63 6c 75 73 69 76 65 20 29  & !isExclusive )
ddd20 7b 0a 20 20 20 20 2f 2a 20 46 61 69 6c 65 64 20  {.    /* Failed 
ddd30 74 6f 20 6f 70 65 6e 20 74 68 65 20 66 69 6c 65  to open the file
ddd40 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20   for read/write 
ddd50 61 63 63 65 73 73 2e 20 54 72 79 20 72 65 61 64  access. Try read
ddd60 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 20 20 66 6c  -only. */.    fl
ddd70 61 67 73 20 26 3d 20 7e 28 53 51 4c 49 54 45 5f  ags &= ~(SQLITE_
ddd80 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53  OPEN_READWRITE|S
ddd90 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
ddda0 45 29 3b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d  E);.    flags |=
dddb0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
dddc0 44 4f 4e 4c 59 3b 0a 20 20 20 20 72 65 74 75 72  DONLY;.    retur
dddd0 6e 20 75 6e 69 78 4f 70 65 6e 28 70 56 66 73 2c  n unixOpen(pVfs,
ddde0 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66   zPath, pFile, f
dddf0 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 67 73 29  lags, pOutFlags)
dde00 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 64 3c 30  ;.  }.  if( fd<0
dde10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
dde20 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
dde30 20 20 7d 0a 20 20 69 66 28 20 69 73 44 65 6c 65    }.  if( isDele
dde40 74 65 20 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57  te ){.#if OS_VXW
dde50 4f 52 4b 53 0a 20 20 20 20 7a 50 61 74 68 20 3d  ORKS.    zPath =
dde60 20 7a 4e 61 6d 65 3b 0a 23 65 6c 73 65 0a 20 20   zName;.#else.  
dde70 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b    unlink(zName);
dde80 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 69 66 20  .#endif.  }.#if 
dde90 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
ddea0 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 65 6c  CKING_STYLE.  el
ddeb0 73 65 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69  se{.    ((unixFi
ddec0 6c 65 2a 29 70 46 69 6c 65 29 2d 3e 6f 70 65 6e  le*)pFile)->open
dded0 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46 6c 61 67  Flags = openFlag
ddee0 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  s;.  }.#endif.  
ddef0 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b  if( pOutFlags ){
ddf00 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20  .    *pOutFlags 
ddf10 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a 23 69  = flags;.  }..#i
ddf20 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
ddf30 66 28 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49  f( (flags & SQLI
ddf40 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29  TE_OPEN_MAIN_DB)
ddf50 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69  !=0 ){.    ((uni
ddf60 78 46 69 6c 65 2a 29 70 46 69 6c 65 29 2d 3e 69  xFile*)pFile)->i
ddf70 73 4c 6f 63 6b 61 62 6c 65 20 3d 20 31 3b 0a 20  sLockable = 1;. 
ddf80 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73   }.#endif..  ass
ddf90 65 72 74 28 20 66 64 3e 3d 30 20 29 3b 0a 20 20  ert( fd>=0 );.  
ddfa0 69 66 28 20 69 73 4f 70 65 6e 44 69 72 65 63 74  if( isOpenDirect
ddfb0 6f 72 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ory ){.    rc = 
ddfc0 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50  openDirectory(zP
ddfd0 61 74 68 2c 20 26 64 69 72 66 64 29 3b 0a 20 20  ath, &dirfd);.  
ddfe0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
ddff0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f  _OK ){.      clo
de000 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e  se(fd); /* silen
de010 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c  tly leak if fail
de020 2c 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72  , already in err
de030 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  or */.      retu
de040 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
de050 0a 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f 45  ..#ifdef FD_CLOE
de060 58 45 43 0a 20 20 66 63 6e 74 6c 28 66 64 2c 20  XEC.  fcntl(fd, 
de070 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66  F_SETFD, fcntl(f
de080 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c  d, F_GETFD, 0) |
de090 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65   FD_CLOEXEC);.#e
de0a0 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63 6b 20 3d  ndif..  noLock =
de0b0 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f   eType!=SQLITE_O
de0c0 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a 0a 23 69  PEN_MAIN_DB;..#i
de0d0 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f  f SQLITE_PREFER_
de0e0 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 20 20  PROXY_LOCKING.  
de0f0 69 66 28 20 7a 50 61 74 68 21 3d 4e 55 4c 4c 20  if( zPath!=NULL 
de100 26 26 20 21 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20  && !noLock ){.  
de110 20 20 63 68 61 72 20 2a 65 6e 76 66 6f 72 63 65    char *envforce
de120 20 3d 20 67 65 74 65 6e 76 28 22 53 51 4c 49 54   = getenv("SQLIT
de130 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f  E_FORCE_PROXY_LO
de140 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20 69 6e 74  CKING");.    int
de150 20 75 73 65 50 72 6f 78 79 20 3d 20 30 3b 0a 0a   useProxy = 0;..
de160 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 4f      /* SQLITE_FO
de170 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e  RCE_PROXY_LOCKIN
de180 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f 72 63 65  G==1 means force
de190 20 61 6c 77 61 79 73 20 75 73 65 20 70 72 6f 78   always use prox
de1a0 79 2c 20 0a 20 20 20 20 2a 2a 20 30 20 6d 65 61  y, .    ** 0 mea
de1b0 6e 73 20 6e 65 76 65 72 20 75 73 65 20 70 72 6f  ns never use pro
de1c0 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 75  xy, NULL means u
de1d0 73 65 20 70 72 6f 78 79 20 66 6f 72 20 6e 6f 6e  se proxy for non
de1e0 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20 6f 6e 6c  -local files onl
de1f0 79 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  y.    */.    if(
de200 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c 20   envforce!=NULL 
de210 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78  ){.      useProx
de220 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72 63  y = atoi(envforc
de230 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  e)>0;.    }else{
de240 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 74  .      struct st
de250 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 0a 20 20  atfs fsInfo;..  
de260 20 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a      if( statfs(z
de270 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d  Path, &fsInfo) =
de280 3d 20 2d 31 20 29 7b 0a 09 09 09 09 28 28 75 6e  = -1 ){.....((un
de290 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 29 2d 3e  ixFile*)pFile)->
de2a0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
de2b0 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64  o;.        if( d
de2c0 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28  irfd>=0 ) close(
de2d0 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e  dirfd); /* silen
de2e0 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c  tly leak if fail
de2f0 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20  , in error */.  
de300 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b        close(fd);
de310 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61   /* silently lea
de320 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72  k if fail, in er
de330 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  ror */.        r
de340 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
de350 52 52 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20  RR_ACCESS;.     
de360 20 7d 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78   }.      useProx
de370 79 20 3d 20 21 28 66 73 49 6e 66 6f 2e 66 5f 66  y = !(fsInfo.f_f
de380 6c 61 67 73 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b  lags&MNT_LOCAL);
de390 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75  .    }.    if( u
de3a0 73 65 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20  seProxy ){.     
de3b0 20 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78   rc = fillInUnix
de3c0 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64  File(pVfs, fd, d
de3d0 69 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61  irfd, pFile, zPa
de3e0 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65  th, noLock, isDe
de3f0 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28  lete);.      if(
de400 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
de410 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  {.        rc = p
de420 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69  roxyTransformUni
de430 78 46 69 6c 65 28 28 75 6e 69 78 46 69 6c 65 2a  xFile((unixFile*
de440 29 70 46 69 6c 65 2c 20 22 3a 61 75 74 6f 3a 22  )pFile, ":auto:"
de450 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
de460 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
de470 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a  }.  }.#endif.  .
de480 20 20 72 65 74 75 72 6e 20 66 69 6c 6c 49 6e 55    return fillInU
de490 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66 64  nixFile(pVfs, fd
de4a0 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c 20  , dirfd, pFile, 
de4b0 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69  zPath, noLock, i
de4c0 73 44 65 6c 65 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a  sDelete);.}../*.
de4d0 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 66 69  ** Delete the fi
de4e0 6c 65 20 61 74 20 7a 50 61 74 68 2e 20 49 66 20  le at zPath. If 
de4f0 74 68 65 20 64 69 72 53 79 6e 63 20 61 72 67 75  the dirSync argu
de500 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 66 73  ment is true, fs
de510 79 6e 63 28 29 0a 2a 2a 20 74 68 65 20 64 69 72  ync().** the dir
de520 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c  ectory after del
de530 65 74 69 6e 67 20 74 68 65 20 66 69 6c 65 2e 0a  eting the file..
de540 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
de550 69 78 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69  ixDelete(.  sqli
de560 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
de570 2c 20 20 20 20 20 2f 2a 20 56 46 53 20 63 6f 6e  ,     /* VFS con
de580 74 61 69 6e 69 6e 67 20 74 68 69 73 20 61 73 20  taining this as 
de590 74 68 65 20 78 44 65 6c 65 74 65 20 6d 65 74 68  the xDelete meth
de5a0 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  od */.  const ch
de5b0 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20  ar *zPath,      
de5c0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
de5d0 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20  e to be deleted 
de5e0 2a 2f 0a 20 20 69 6e 74 20 64 69 72 53 79 6e 63  */.  int dirSync
de5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
de600 2a 20 49 66 20 74 72 75 65 2c 20 66 73 79 6e 63  * If true, fsync
de610 28 29 20 64 69 72 65 63 74 6f 72 79 20 61 66 74  () directory aft
de620 65 72 20 64 65 6c 65 74 69 6e 67 20 66 69 6c 65  er deleting file
de630 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
de640 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55  = SQLITE_OK;.  U
de650 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
de660 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75  NotUsed);.  Simu
de670 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75  lateIOError(retu
de680 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
de690 44 45 4c 45 54 45 29 3b 0a 20 20 75 6e 6c 69 6e  DELETE);.  unlin
de6a0 6b 28 7a 50 61 74 68 29 3b 0a 23 69 66 6e 64 65  k(zPath);.#ifnde
de6b0 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  f SQLITE_DISABLE
de6c0 5f 44 49 52 53 59 4e 43 0a 20 20 69 66 28 20 64  _DIRSYNC.  if( d
de6d0 69 72 53 79 6e 63 20 29 7b 0a 20 20 20 20 69 6e  irSync ){.    in
de6e0 74 20 66 64 3b 0a 20 20 20 20 72 63 20 3d 20 6f  t fd;.    rc = o
de6f0 70 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61  penDirectory(zPa
de700 74 68 2c 20 26 66 64 29 3b 0a 20 20 20 20 69 66  th, &fd);.    if
de710 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
de720 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  ){.#if OS_VXWORK
de730 53 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 6e  S.      if( fsyn
de740 63 28 66 64 29 3d 3d 2d 31 20 29 0a 23 65 6c 73  c(fd)==-1 ).#els
de750 65 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 6e  e.      if( fsyn
de760 63 28 66 64 29 20 29 0a 23 65 6e 64 69 66 0a 20  c(fd) ).#endif. 
de770 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 72       {.        r
de780 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
de790 5f 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20 20 20  _DIR_FSYNC;.    
de7a0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 6c    }.      if( cl
de7b0 6f 73 65 28 66 64 29 26 26 21 72 63 20 29 7b 0a  ose(fd)&&!rc ){.
de7c0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
de7d0 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c  ITE_IOERR_DIR_CL
de7e0 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  OSE;.      }.   
de7f0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
de800 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
de810 0a 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78 69  .** Test the exi
de820 73 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 63  stance of or acc
de830 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 20  ess permissions 
de840 6f 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 54  of file zPath. T
de850 68 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 6f  he.** test perfo
de860 72 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20  rmed depends on 
de870 74 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c 61  the value of fla
de880 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51  gs:.**.**     SQ
de890 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
de8a0 54 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20  TS: Return 1 if 
de8b0 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 0a  the file exists.
de8c0 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43  **     SQLITE_AC
de8d0 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 20  CESS_READWRITE: 
de8e0 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
de8f0 66 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e 64  file is read and
de900 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20 20   writable..**   
de910 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f    SQLITE_ACCESS_
de920 52 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72 6e  READONLY: Return
de930 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69   1 if the file i
de940 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a  s readable..**.*
de950 2a 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75  * Otherwise retu
de960 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rn 0..*/.static 
de970 69 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28 0a  int unixAccess(.
de980 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e    sqlite3_vfs *N
de990 6f 74 55 73 65 64 2c 20 20 20 2f 2a 20 54 68 65  otUsed,   /* The
de9a0 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20   VFS containing 
de9b0 74 68 69 73 20 78 41 63 63 65 73 73 20 6d 65 74  this xAccess met
de9c0 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  hod */.  const c
de9d0 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20  har *zPath,     
de9e0 20 2f 2a 20 50 61 74 68 20 6f 66 20 74 68 65 20   /* Path of the 
de9f0 66 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65 20  file to examine 
dea00 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
dea10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
dea20 57 68 61 74 20 64 6f 20 77 65 20 77 61 6e 74 20  What do we want 
dea30 74 6f 20 6c 65 61 72 6e 20 61 62 6f 75 74 20 74  to learn about t
dea40 68 65 20 7a 50 61 74 68 20 66 69 6c 65 3f 20 2a  he zPath file? *
dea50 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  /.  int *pResOut
dea60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
dea70 72 69 74 65 20 72 65 73 75 6c 74 20 62 6f 6f 6c  rite result bool
dea80 65 61 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ean here */.){. 
dea90 20 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b 0a   int amode = 0;.
deaa0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
deab0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53  ER(NotUsed);.  S
deac0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
dead0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
deae0 45 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a 20  ERR_ACCESS; );. 
deaf0 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29   switch( flags )
deb00 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
deb10 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a  E_ACCESS_EXISTS:
deb20 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 46  .      amode = F
deb30 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  _OK;.      break
deb40 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
deb50 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
deb60 54 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20  TE:.      amode 
deb70 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20  = W_OK|R_OK;.   
deb80 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
deb90 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
deba0 5f 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d 6f  _READ:.      amo
debb0 64 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20 20  de = R_OK;.     
debc0 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66   break;..    def
debd0 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65  ault:.      asse
debe0 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61  rt(!"Invalid fla
debf0 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20  gs argument");. 
dec00 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20   }.  *pResOut = 
dec10 28 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20 61  (access(zPath, a
dec20 6d 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 72 65 74  mode)==0);.  ret
dec30 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
dec40 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20  .../*.** Turn a 
dec50 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d  relative pathnam
dec60 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61  e into a full pa
dec70 74 68 6e 61 6d 65 2e 20 54 68 65 20 72 65 6c 61  thname. The rela
dec80 74 69 76 65 20 70 61 74 68 0a 2a 2a 20 69 73 20  tive path.** is 
dec90 73 74 6f 72 65 64 20 61 73 20 61 20 6e 75 6c 2d  stored as a nul-
deca0 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
decb0 67 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20  g in the buffer 
decc0 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a  pointed to by.**
decd0 20 7a 50 61 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a   zPath. .**.** z
dece0 4f 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  Out points to a 
decf0 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61  buffer of at lea
ded00 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d  st sqlite3_vfs.m
ded10 78 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73 20  xPathname bytes 
ded20 0a 2a 2a 20 28 69 6e 20 74 68 69 73 20 63 61 73  .** (in this cas
ded30 65 2c 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20  e, MAX_PATHNAME 
ded40 62 79 74 65 73 29 2e 20 54 68 65 20 66 75 6c 6c  bytes). The full
ded50 2d 70 61 74 68 20 69 73 20 77 72 69 74 74 65 6e  -path is written
ded60 20 74 6f 0a 2a 2a 20 74 68 69 73 20 62 75 66 66   to.** this buff
ded70 65 72 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  er before return
ded80 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
ded90 6e 74 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e  nt unixFullPathn
deda0 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ame(.  sqlite3_v
dedb0 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20  fs *pVfs,       
dedc0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
dedd0 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f  to vfs object */
dede0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
dedf0 50 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20  Path,           
dee00 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c   /* Possibly rel
dee10 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68  ative input path
dee20 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 2c 20   */.  int nOut, 
dee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dee40 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f      /* Size of o
dee50 75 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20  utput buffer in 
dee60 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20  bytes */.  char 
dee70 2a 7a 4f 75 74 20 20 20 20 20 20 20 20 20 20 20  *zOut           
dee80 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
dee90 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a  ut buffer */.){.
deea0 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74  .  /* It's odd t
deeb0 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f  o simulate an io
deec0 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74  -error here, but
deed0 20 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20   really this is 
deee0 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20  just.  ** using 
deef0 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66  the io-error inf
def00 72 61 73 74 72 75 63 74 75 72 65 20 74 6f 20 74  rastructure to t
def10 65 73 74 20 74 68 61 74 20 53 51 4c 69 74 65 20  est that SQLite 
def20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a  handles this.  *
def30 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69  * function faili
def40 6e 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ng. This functio
def50 6e 20 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 2c  n could fail if,
def60 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68   for example, th
def70 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 77  e.  ** current w
def80 6f 72 6b 69 6e 67 20 64 69 72 65 63 74 6f 72 79  orking directory
def90 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b   has been unlink
defa0 65 64 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c  ed..  */.  Simul
defb0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
defc0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  rn SQLITE_ERROR 
defd0 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 56  );..  assert( pV
defe0 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3d 3d  fs->mxPathname==
deff0 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 29 3b 0a  MAX_PATHNAME );.
df000 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
df010 45 52 28 70 56 66 73 29 3b 0a 0a 20 20 7a 4f 75  ER(pVfs);..  zOu
df020 74 5b 6e 4f 75 74 2d 31 5d 20 3d 20 27 5c 30 27  t[nOut-1] = '\0'
df030 3b 0a 20 20 69 66 28 20 7a 50 61 74 68 5b 30 5d  ;.  if( zPath[0]
df040 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 73 71 6c  =='/' ){.    sql
df050 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f  ite3_snprintf(nO
df060 75 74 2c 20 7a 4f 75 74 2c 20 22 25 73 22 2c 20  ut, zOut, "%s", 
df070 7a 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 7b  zPath);.  }else{
df080 0a 20 20 20 20 69 6e 74 20 6e 43 77 64 3b 0a 20  .    int nCwd;. 
df090 20 20 20 69 66 28 20 67 65 74 63 77 64 28 7a 4f     if( getcwd(zO
df0a0 75 74 2c 20 6e 4f 75 74 2d 31 29 3d 3d 30 20 29  ut, nOut-1)==0 )
df0b0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
df0c0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
df0d0 20 20 20 20 7d 0a 20 20 20 20 6e 43 77 64 20 3d      }.    nCwd =
df0e0 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 4f 75   (int)strlen(zOu
df0f0 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t);.    sqlite3_
df100 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2d 6e 43  snprintf(nOut-nC
df110 77 64 2c 20 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c  wd, &zOut[nCwd],
df120 20 22 2f 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a   "/%s", zPath);.
df130 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
df140 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e  ITE_OK;.}...#ifn
df150 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
df160 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f  LOAD_EXTENSION./
df170 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20  *.** Interfaces 
df180 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68  for opening a sh
df190 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69  ared library, fi
df1a0 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e  nding entry poin
df1b0 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  ts.** within the
df1c0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
df1d0 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65   and closing the
df1e0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e   shared library.
df1f0 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 6c  .*/.#include <dl
df200 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 6f  fcn.h>.static vo
df210 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 73  id *unixDlOpen(s
df220 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55  qlite3_vfs *NotU
df230 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  sed, const char 
df240 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55  *zFilename){.  U
df250 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
df260 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75  NotUsed);.  retu
df270 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e  rn dlopen(zFilen
df280 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20  ame, RTLD_NOW | 
df290 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a  RTLD_GLOBAL);.}.
df2a0 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61  ./*.** SQLite ca
df2b0 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  lls this functio
df2c0 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66  n immediately af
df2d0 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e  ter a call to un
df2e0 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a 20  ixDlSym() or.** 
df2f0 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69  unixDlOpen() fai
df300 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75  ls (returns a nu
df310 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20  ll pointer). If 
df320 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20  a more detailed 
df330 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65  error.** message
df340 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69   is available, i
df350 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  t is written to 
df360 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65  zBufOut. If no e
df370 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20  rror message.** 
df380 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a 42  is available, zB
df390 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75 6e  ufOut is left un
df3a0 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51 4c  modified and SQL
df3b0 69 74 65 20 75 73 65 73 20 61 20 64 65 66 61 75  ite uses a defau
df3c0 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73  lt.** error mess
df3d0 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  age..*/.static v
df3e0 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 28  oid unixDlError(
df3f0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74  sqlite3_vfs *Not
df400 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20  Used, int nBuf, 
df410 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a  char *zBufOut){.
df420 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20    char *zErr;.  
df430 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
df440 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 75 6e 69  (NotUsed);.  uni
df450 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
df460 20 7a 45 72 72 20 3d 20 64 6c 65 72 72 6f 72 28   zErr = dlerror(
df470 29 3b 0a 20 20 69 66 28 20 7a 45 72 72 20 29 7b  );.  if( zErr ){
df480 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
df490 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66  rintf(nBuf, zBuf
df4a0 4f 75 74 2c 20 22 25 73 22 2c 20 7a 45 72 72 29  Out, "%s", zErr)
df4b0 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76  ;.  }.  unixLeav
df4c0 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 73 74 61 74  eMutex();.}.stat
df4d0 69 63 20 76 6f 69 64 20 28 2a 75 6e 69 78 44 6c  ic void (*unixDl
df4e0 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20  Sym(sqlite3_vfs 
df4f0 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a  *NotUsed, void *
df500 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 53  p, const char*zS
df510 79 6d 29 29 28 76 6f 69 64 29 7b 0a 20 20 2f 2a  ym))(void){.  /*
df520 20 0a 20 20 2a 2a 20 47 43 43 20 77 69 74 68 20   .  ** GCC with 
df530 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 73  -pedantic-errors
df540 20 73 61 79 73 20 74 68 61 74 20 43 39 30 20 64   says that C90 d
df550 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 20  oes not allow a 
df560 76 6f 69 64 2a 20 74 6f 20 62 65 0a 20 20 2a 2a  void* to be.  **
df570 20 63 61 73 74 20 69 6e 74 6f 20 61 20 70 6f 69   cast into a poi
df580 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69  nter to a functi
df590 6f 6e 2e 20 20 41 6e 64 20 79 65 74 20 74 68 65  on.  And yet the
df5a0 20 6c 69 62 72 61 72 79 20 64 6c 73 79 6d 28 29   library dlsym()
df5b0 20 72 6f 75 74 69 6e 65 0a 20 20 2a 2a 20 72 65   routine.  ** re
df5c0 74 75 72 6e 73 20 61 20 76 6f 69 64 2a 20 77 68  turns a void* wh
df5d0 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20 61 20  ich is really a 
df5e0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
df5f0 63 74 69 6f 6e 2e 20 20 53 6f 20 68 6f 77 20 64  ction.  So how d
df600 6f 20 77 65 0a 20 20 2a 2a 20 75 73 65 20 64 6c  o we.  ** use dl
df610 73 79 6d 28 29 20 77 69 74 68 20 2d 70 65 64 61  sym() with -peda
df620 6e 74 69 63 2d 65 72 72 6f 72 73 3f 0a 20 20 2a  ntic-errors?.  *
df630 2a 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20  *.  ** Variable 
df640 78 20 62 65 6c 6f 77 20 69 73 20 64 65 66 69 6e  x below is defin
df650 65 64 20 74 6f 20 62 65 20 61 20 70 6f 69 6e 74  ed to be a point
df660 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
df670 20 74 61 6b 69 6e 67 0a 20 20 2a 2a 20 70 61 72   taking.  ** par
df680 61 6d 65 74 65 72 73 20 76 6f 69 64 2a 20 61 6e  ameters void* an
df690 64 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 61 6e  d const char* an
df6a0 64 20 72 65 74 75 72 6e 69 6e 67 20 61 20 70 6f  d returning a po
df6b0 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74  inter to a funct
df6c0 69 6f 6e 2e 0a 20 20 2a 2a 20 57 65 20 69 6e 69  ion..  ** We ini
df6d0 74 69 61 6c 69 7a 65 20 78 20 62 79 20 61 73 73  tialize x by ass
df6e0 69 67 6e 69 6e 67 20 69 74 20 61 20 70 6f 69 6e  igning it a poin
df6f0 74 65 72 20 74 6f 20 74 68 65 20 64 6c 73 79 6d  ter to the dlsym
df700 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a  () function..  *
df710 2a 20 28 54 68 61 74 20 61 73 73 69 67 6e 6d 65  * (That assignme
df720 6e 74 20 72 65 71 75 69 72 65 73 20 61 20 63 61  nt requires a ca
df730 73 74 2e 29 20 20 54 68 65 6e 20 77 65 20 63 61  st.)  Then we ca
df740 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ll the function 
df750 74 68 61 74 0a 20 20 2a 2a 20 78 20 70 6f 69 6e  that.  ** x poin
df760 74 73 20 74 6f 2e 20 20 0a 20 20 2a 2a 0a 20 20  ts to.  .  **.  
df770 2a 2a 20 54 68 69 73 20 77 6f 72 6b 2d 61 72 6f  ** This work-aro
df780 75 6e 64 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20  und is unlikely 
df790 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c  to work correctl
df7a0 79 20 6f 6e 20 61 6e 79 20 73 79 73 74 65 6d 20  y on any system 
df7b0 77 68 65 72 65 0a 20 20 2a 2a 20 79 6f 75 20 72  where.  ** you r
df7c0 65 61 6c 6c 79 20 63 61 6e 6e 6f 74 20 63 61 73  eally cannot cas
df7d0 74 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  t a function poi
df7e0 6e 74 65 72 20 69 6e 74 6f 20 76 6f 69 64 2a 2e  nter into void*.
df7f0 20 20 42 75 74 20 74 68 65 6e 2c 20 6f 6e 20 74    But then, on t
df800 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 68 61  he.  ** other ha
df810 6e 64 2c 20 64 6c 73 79 6d 28 29 20 77 69 6c 6c  nd, dlsym() will
df820 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 73 75 63   not work on suc
df830 68 20 61 20 73 79 73 74 65 6d 20 65 69 74 68 65  h a system eithe
df840 72 2c 20 73 6f 20 77 65 20 68 61 76 65 0a 20 20  r, so we have.  
df850 2a 2a 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6c 6f  ** not really lo
df860 73 74 20 61 6e 79 74 68 69 6e 67 2e 0a 20 20 2a  st anything..  *
df870 2f 0a 20 20 76 6f 69 64 20 28 2a 28 2a 78 29 28  /.  void (*(*x)(
df880 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
df890 2a 29 29 28 76 6f 69 64 29 3b 0a 20 20 55 4e 55  *))(void);.  UNU
df8a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
df8b0 74 55 73 65 64 29 3b 0a 20 20 78 20 3d 20 28 76  tUsed);.  x = (v
df8c0 6f 69 64 28 2a 28 2a 29 28 76 6f 69 64 2a 2c 63  oid(*(*)(void*,c
df8d0 6f 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69  onst char*))(voi
df8e0 64 29 29 64 6c 73 79 6d 3b 0a 20 20 72 65 74 75  d))dlsym;.  retu
df8f0 72 6e 20 28 2a 78 29 28 70 2c 20 7a 53 79 6d 29  rn (*x)(p, zSym)
df900 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
df910 75 6e 69 78 44 6c 43 6c 6f 73 65 28 73 71 6c 69  unixDlClose(sqli
df920 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
df930 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29  , void *pHandle)
df940 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
df950 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
df960 20 64 6c 63 6c 6f 73 65 28 70 48 61 6e 64 6c 65   dlclose(pHandle
df970 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66  );.}.#else /* if
df980 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
df990 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64  D_EXTENSION is d
df9a0 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65  efined: */.  #de
df9b0 66 69 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e 20  fine unixDlOpen 
df9c0 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69   0.  #define uni
df9d0 78 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65  xDlError 0.  #de
df9e0 66 69 6e 65 20 75 6e 69 78 44 6c 53 79 6d 20 20  fine unixDlSym  
df9f0 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69   0.  #define uni
dfa00 78 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69  xDlClose 0.#endi
dfa10 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6e  f../*.** Write n
dfa20 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e  Buf bytes of ran
dfa30 64 6f 6d 20 64 61 74 61 20 74 6f 20 74 68 65 20  dom data to the 
dfa40 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 20  supplied buffer 
dfa50 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  zBuf..*/.static 
dfa60 69 6e 74 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65  int unixRandomne
dfa70 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ss(sqlite3_vfs *
dfa80 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e 42 75  NotUsed, int nBu
dfa90 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
dfaa0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
dfab0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61  ER(NotUsed);.  a
dfac0 73 73 65 72 74 28 28 73 69 7a 65 5f 74 29 6e 42  ssert((size_t)nB
dfad0 75 66 3e 3d 28 73 69 7a 65 6f 66 28 74 69 6d 65  uf>=(sizeof(time
dfae0 5f 74 29 2b 73 69 7a 65 6f 66 28 69 6e 74 29 29  _t)+sizeof(int))
dfaf0 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65  );..  /* We have
dfb00 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 7a   to initialize z
dfb10 42 75 66 20 74 6f 20 70 72 65 76 65 6e 74 20 76  Buf to prevent v
dfb20 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 72 65 70  algrind from rep
dfb30 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 65 72 72 6f  orting.  ** erro
dfb40 72 73 2e 20 20 54 68 65 20 72 65 70 6f 72 74 73  rs.  The reports
dfb50 20 69 73 73 75 65 64 20 62 79 20 76 61 6c 67 72   issued by valgr
dfb60 69 6e 64 20 61 72 65 20 69 6e 63 6f 72 72 65 63  ind are incorrec
dfb70 74 20 2d 20 77 65 20 77 6f 75 6c 64 0a 20 20 2a  t - we would.  *
dfb80 2a 20 70 72 65 66 65 72 20 74 68 61 74 20 74 68  * prefer that th
dfb90 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 62 65 20  e randomness be 
dfba0 69 6e 63 72 65 61 73 65 64 20 62 79 20 6d 61 6b  increased by mak
dfbb0 69 6e 67 20 75 73 65 20 6f 66 20 74 68 65 0a 20  ing use of the. 
dfbc0 20 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65   ** uninitialize
dfbd0 64 20 73 70 61 63 65 20 69 6e 20 7a 42 75 66 20  d space in zBuf 
dfbe0 2d 20 62 75 74 20 76 61 6c 67 72 69 6e 64 20 65  - but valgrind e
dfbf0 72 72 6f 72 73 20 74 65 6e 64 20 74 6f 20 77 6f  rrors tend to wo
dfc00 72 72 79 0a 20 20 2a 2a 20 73 6f 6d 65 20 75 73  rry.  ** some us
dfc10 65 72 73 2e 20 20 52 61 74 68 65 72 20 74 68 61  ers.  Rather tha
dfc20 6e 20 61 72 67 75 65 2c 20 69 74 20 73 65 65 6d  n argue, it seem
dfc30 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f  s easier just to
dfc40 20 69 6e 69 74 69 61 6c 69 7a 65 0a 20 20 2a 2a   initialize.  **
dfc50 20 74 68 65 20 77 68 6f 6c 65 20 61 72 72 61 79   the whole array
dfc60 20 61 6e 64 20 73 69 6c 65 6e 63 65 20 76 61 6c   and silence val
dfc70 67 72 69 6e 64 2c 20 65 76 65 6e 20 69 66 20 74  grind, even if t
dfc80 68 61 74 20 6d 65 61 6e 73 20 6c 65 73 73 20 72  hat means less r
dfc90 61 6e 64 6f 6d 6e 65 73 73 0a 20 20 2a 2a 20 69  andomness.  ** i
dfca0 6e 20 74 68 65 20 72 61 6e 64 6f 6d 20 73 65 65  n the random see
dfcb0 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65  d..  **.  ** Whe
dfcc0 6e 20 74 65 73 74 69 6e 67 2c 20 69 6e 69 74 69  n testing, initi
dfcd0 61 6c 69 7a 69 6e 67 20 7a 42 75 66 5b 5d 20 74  alizing zBuf[] t
dfce0 6f 20 7a 65 72 6f 20 69 73 20 61 6c 6c 20 77 65  o zero is all we
dfcf0 20 64 6f 2e 20 20 54 68 61 74 20 6d 65 61 6e 73   do.  That means
dfd00 0a 20 20 2a 2a 20 74 68 61 74 20 77 65 20 61 6c  .  ** that we al
dfd10 77 61 79 73 20 75 73 65 20 74 68 65 20 73 61 6d  ways use the sam
dfd20 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  e random number 
dfd30 73 65 71 75 65 6e 63 65 2e 20 20 54 68 69 73 20  sequence.  This 
dfd40 6d 61 6b 65 73 20 74 68 65 0a 20 20 2a 2a 20 74  makes the.  ** t
dfd50 65 73 74 73 20 72 65 70 65 61 74 61 62 6c 65 2e  ests repeatable.
dfd60 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 7a  .  */.  memset(z
dfd70 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23  Buf, 0, nBuf);.#
dfd80 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
dfd90 54 45 5f 54 45 53 54 29 0a 20 20 7b 0a 20 20 20  TE_TEST).  {.   
dfda0 20 69 6e 74 20 70 69 64 2c 20 66 64 3b 0a 20 20   int pid, fd;.  
dfdb0 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65    fd = open("/de
dfdc0 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44  v/urandom", O_RD
dfdd0 4f 4e 4c 59 29 3b 0a 20 20 20 20 69 66 28 20 66  ONLY);.    if( f
dfde0 64 3c 30 20 29 7b 0a 20 20 20 20 20 20 74 69 6d  d<0 ){.      tim
dfdf0 65 5f 74 20 74 3b 0a 20 20 20 20 20 20 74 69 6d  e_t t;.      tim
dfe00 65 28 26 74 29 3b 0a 20 20 20 20 20 20 6d 65 6d  e(&t);.      mem
dfe10 63 70 79 28 7a 42 75 66 2c 20 26 74 2c 20 73 69  cpy(zBuf, &t, si
dfe20 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 20 20  zeof(t));.      
dfe30 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a  pid = getpid();.
dfe40 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42        memcpy(&zB
dfe50 75 66 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26  uf[sizeof(t)], &
dfe60 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29  pid, sizeof(pid)
dfe70 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
dfe80 20 73 69 7a 65 6f 66 28 74 29 2b 73 69 7a 65 6f   sizeof(t)+sizeo
dfe90 66 28 70 69 64 29 3c 3d 28 73 69 7a 65 5f 74 29  f(pid)<=(size_t)
dfea0 6e 42 75 66 20 29 3b 0a 20 20 20 20 20 20 6e 42  nBuf );.      nB
dfeb0 75 66 20 3d 20 73 69 7a 65 6f 66 28 74 29 20 2b  uf = sizeof(t) +
dfec0 20 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20   sizeof(pid);.  
dfed0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
dfee0 42 75 66 20 3d 20 72 65 61 64 28 66 64 2c 20 7a  Buf = read(fd, z
dfef0 42 75 66 2c 20 6e 42 75 66 29 3b 0a 20 20 20 20  Buf, nBuf);.    
dff00 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20    close(fd);.   
dff10 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
dff20 72 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a  return nBuf;.}..
dff30 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72  ./*.** Sleep for
dff40 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e   a little while.
dff50 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f    Return the amo
dff60 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70  unt of time slep
dff70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65  t..** The argume
dff80 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  nt is the number
dff90 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73   of microseconds
dffa0 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65   we want to slee
dffb0 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  p..** The return
dffc0 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75   value is the nu
dffd0 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63  mber of microsec
dffe0 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63  onds of sleep ac
dfff0 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73  tually.** reques
e0000 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64  ted from the und
e0010 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e  erlying operatin
e0020 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62  g system, a numb
e0030 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68  er which.** migh
e0040 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61  t be greater tha
e0050 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68  n or equal to th
e0060 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20  e argument, but 
e0070 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e  not less.** than
e0080 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   the argument..*
e0090 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e00a0 78 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76  xSleep(sqlite3_v
e00b0 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74  fs *NotUsed, int
e00c0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b 0a   microseconds){.
e00d0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
e00e0 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63   struct timespec
e00f0 20 73 70 3b 0a 0a 20 20 73 70 2e 74 76 5f 73 65   sp;..  sp.tv_se
e0100 63 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73  c = microseconds
e0110 20 2f 20 31 30 30 30 30 30 30 3b 0a 20 20 73 70   / 1000000;.  sp
e0120 2e 74 76 5f 6e 73 65 63 20 3d 20 28 6d 69 63 72  .tv_nsec = (micr
e0130 6f 73 65 63 6f 6e 64 73 20 25 20 31 30 30 30 30  oseconds % 10000
e0140 30 30 29 20 2a 20 31 30 30 30 3b 0a 20 20 6e 61  00) * 1000;.  na
e0150 6e 6f 73 6c 65 65 70 28 26 73 70 2c 20 4e 55 4c  nosleep(&sp, NUL
e0160 4c 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  L);.  UNUSED_PAR
e0170 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e0180 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73  .  return micros
e0190 65 63 6f 6e 64 73 3b 0a 23 65 6c 69 66 20 64 65  econds;.#elif de
e01a0 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45  fined(HAVE_USLEE
e01b0 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45  P) && HAVE_USLEE
e01c0 50 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72 6f  P.  usleep(micro
e01d0 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e 55 53  seconds);.  UNUS
e01e0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e01f0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
e0200 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 65  microseconds;.#e
e0210 6c 73 65 0a 20 20 69 6e 74 20 73 65 63 6f 6e 64  lse.  int second
e0220 73 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64  s = (microsecond
e0230 73 2b 39 39 39 39 39 39 29 2f 31 30 30 30 30 30  s+999999)/100000
e0240 30 3b 0a 20 20 73 6c 65 65 70 28 73 65 63 6f 6e  0;.  sleep(secon
e0250 64 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ds);.  UNUSED_PA
e0260 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e0270 3b 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f 6e  ;.  return secon
e0280 64 73 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e 64  ds*1000000;.#end
e0290 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  if.}../*.** The 
e02a0 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62  following variab
e02b0 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20  le, if set to a 
e02c0 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20  non-zero value, 
e02d0 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  is interpreted a
e02e0 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  s.** the number 
e02f0 6f 66 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65  of seconds since
e0300 20 31 39 37 30 20 61 6e 64 20 69 73 20 75 73 65   1970 and is use
e0310 64 20 74 6f 20 73 65 74 20 74 68 65 20 72 65 73  d to set the res
e0320 75 6c 74 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65  ult of.** sqlite
e0330 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29  3OsCurrentTime()
e0340 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2e   during testing.
e0350 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
e0360 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
e0370 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75  I int sqlite3_cu
e0380 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 20  rrent_time = 0; 
e0390 20 2f 2a 20 46 61 6b 65 20 73 79 73 74 65 6d 20   /* Fake system 
e03a0 74 69 6d 65 20 69 6e 20 73 65 63 6f 6e 64 73 20  time in seconds 
e03b0 73 69 6e 63 65 20 31 39 37 30 2e 20 2a 2f 0a 23  since 1970. */.#
e03c0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  endif../*.** Fin
e03d0 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69  d the current ti
e03e0 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c  me (in Universal
e03f0 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d   Coordinated Tim
e0400 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a  e).  Write the.*
e0410 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61  * current time a
e0420 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c  nd date as a Jul
e0430 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69  ian Day number i
e0440 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a  nto *prNow and.*
e0450 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74  * return 0.  Ret
e0460 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d  urn 1 if the tim
e0470 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f  e and date canno
e0480 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73  t be found..*/.s
e0490 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75  tatic int unixCu
e04a0 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65  rrentTime(sqlite
e04b0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
e04c0 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a  double *prNow){.
e04d0 23 69 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47  #if defined(NO_G
e04e0 45 54 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20  ETTOD).  time_t 
e04f0 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20  t;.  time(&t);. 
e0500 20 2a 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30   *prNow = t/8640
e0510 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b  0.0 + 2440587.5;
e0520 0a 23 65 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b  .#elif OS_VXWORK
e0530 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 73  S.  struct times
e0540 70 65 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63  pec sNow;.  cloc
e0550 6b 5f 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f  k_gettime(CLOCK_
e0560 52 45 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29  REALTIME, &sNow)
e0570 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34  ;.  *prNow = 244
e0580 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76  0587.5 + sNow.tv
e0590 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73  _sec/86400.0 + s
e05a0 4e 6f 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30  Now.tv_nsec/8640
e05b0 30 30 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65  0000000000.0;.#e
e05c0 6c 73 65 0a 20 20 73 74 72 75 63 74 20 74 69 6d  lse.  struct tim
e05d0 65 76 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74  eval sNow;.  get
e05e0 74 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c  timeofday(&sNow,
e05f0 20 30 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20   0);.  *prNow = 
e0600 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77  2440587.5 + sNow
e0610 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20  .tv_sec/86400.0 
e0620 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38  + sNow.tv_usec/8
e0630 36 34 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65  6400000000.0;.#e
e0640 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
e0650 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73  ITE_TEST.  if( s
e0660 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
e0670 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f  ime ){.    *prNo
e0680 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72  w = sqlite3_curr
e0690 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30  ent_time/86400.0
e06a0 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20   + 2440587.5;.  
e06b0 7d 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45  }.#endif.  UNUSE
e06c0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
e06d0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  sed);.  return 0
e06e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64  ;.}../*.** We ad
e06f0 64 65 64 20 74 68 65 20 78 47 65 74 4c 61 73 74  ded the xGetLast
e0700 45 72 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77  Error() method w
e0710 69 74 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f  ith the intentio
e0720 6e 20 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a  n of providing.*
e0730 2a 20 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76  * better low-lev
e0740 65 6c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  el error message
e0750 73 20 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67  s when operating
e0760 2d 73 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73  -system problems
e0770 20 63 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69   come up.** duri
e0780 6e 67 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74  ng SQLite operat
e0790 69 6f 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72  ion.  But so far
e07a0 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68  , none of that h
e07b0 61 73 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e  as been implemen
e07c0 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f  ted.** in the co
e07d0 72 65 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75  re.  So this rou
e07e0 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61  tine is never ca
e07f0 6c 6c 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20  lled.  For now, 
e0800 69 74 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20  it is merely.** 
e0810 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a  a place-holder..
e0820 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
e0830 69 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73  ixGetLastError(s
e0840 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55  qlite3_vfs *NotU
e0850 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64  sed, int NotUsed
e0860 32 2c 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64  2, char *NotUsed
e0870 33 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  3){.  UNUSED_PAR
e0880 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e0890 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
e08a0 54 45 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20  TER(NotUsed2);. 
e08b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e08c0 52 28 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72  R(NotUsed3);.  r
e08d0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
e08e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e08f0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73  ******* End of s
e0900 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f  qlite3_vfs metho
e0910 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ds *************
e0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
e0930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
e0980 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
e0990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e09a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e09b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e09c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
e09d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e09e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
e09f0 20 50 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a   Proxy Locking *
e0a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
e0a20 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b  **.** Proxy lock
e0a30 69 6e 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c  ing is a "uber-l
e0a40 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69  ocking-method" i
e0a50 6e 20 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49  n this sense:  I
e0a60 74 20 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74  t uses the.** ot
e0a70 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68  her locking meth
e0a80 6f 64 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79  ods on secondary
e0a90 20 6c 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72   lock files.  Pr
e0aa0 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61  oxy locking is a
e0ab0 0a 2a 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f  .** meta-layer o
e0ac0 76 65 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70  ver top of the p
e0ad0 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67  rimitive locking
e0ae0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f   implemented abo
e0af0 76 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73  ve.  For.** this
e0b00 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76   reason, the div
e0b10 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65  ision that imple
e0b20 6d 65 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c  ments of proxy l
e0b30 6f 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72  ocking is deferr
e0b40 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65  ed.** until late
e0b50 20 69 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65   in the file (he
e0b60 72 65 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66  re) after all of
e0b70 20 74 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d   the other I/O m
e0b80 65 74 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62  ethods have.** b
e0b90 65 65 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f  een defined - so
e0ba0 20 74 68 61 74 20 74 68 65 20 70 72 69 6d 69 74   that the primit
e0bb0 69 76 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68  ive locking meth
e0bc0 6f 64 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  ods are availabl
e0bd0 65 0a 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73  e.** as services
e0be0 20 74 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68   to help with th
e0bf0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
e0c00 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e   of proxy lockin
e0c10 67 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  g..**.****.**.**
e0c20 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63   The default loc
e0c30 6b 69 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20  king schemes in 
e0c40 53 51 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d  SQLite use byte-
e0c50 72 61 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74  range locks on t
e0c60 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
e0c70 69 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74  ile to coordinat
e0c80 65 20 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65  e safe, concurre
e0c90 6e 74 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c  nt access by mul
e0ca0 74 69 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a  tiple readers.**
e0cb0 20 61 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74   and writers [ht
e0cc0 74 70 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f  tp://sqlite.org/
e0cd0 6c 6f 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e  lockingv3.html].
e0ce0 20 20 54 68 65 20 66 69 76 65 20 66 69 6c 65 20    The five file 
e0cf0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65  locking.** state
e0d00 73 20 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e  s (UNLOCKED, PEN
e0d10 44 49 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45  DING, SHARED, RE
e0d20 53 45 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56  SERVED, EXCLUSIV
e0d30 45 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74  E) are implement
e0d40 65 64 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72  ed.** as POSIX r
e0d50 65 61 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b  ead & write lock
e0d60 73 20 6f 76 65 72 20 66 69 78 65 64 20 73 65 74  s over fixed set
e0d70 20 6f 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76   of locations (v
e0d80 69 61 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e  ia fsctl),.** on
e0d90 20 41 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c   AFP and SMB onl
e0da0 79 20 65 78 63 6c 75 73 69 76 65 20 62 79 74 65  y exclusive byte
e0db0 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65  -range locks are
e0dc0 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66   available via f
e0dd0 73 63 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f  sctl.** with _IO
e0de0 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75  WR('z', 23, stru
e0df0 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b  ct ByteRangeLock
e0e00 50 42 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68  PB2) to track th
e0e10 65 20 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e  e same 5 states.
e0e20 0a 2a 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20  .** To simulate 
e0e30 61 20 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65  a F_RDLCK on the
e0e40 20 73 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f   shared range, o
e0e50 6e 20 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79  n AFP a randomly
e0e60 20 73 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64   selected.** add
e0e70 72 65 73 73 20 69 6e 20 74 68 65 20 73 68 61 72  ress in the shar
e0e80 65 64 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65  ed range is take
e0e90 6e 20 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c  n for a SHARED l
e0ea0 6f 63 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a  ock, the entire.
e0eb0 2a 2a 20 73 68 61 72 65 64 20 72 61 6e 67 65 20  ** shared range 
e0ec0 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20  is taken for an 
e0ed0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a  EXCLUSIVE lock):
e0ee0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44  .**.**      PEND
e0ef0 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20  ING_BYTE        
e0f00 30 78 34 30 30 30 30 30 30 30 09 09 20 20 20 09  0x40000000..   .
e0f10 0a 2a 2a 20 20 20 20 20 20 52 45 53 45 52 56 45  .**      RESERVE
e0f20 44 5f 42 59 54 45 20 20 20 20 20 20 20 30 78 34  D_BYTE       0x4
e0f30 30 30 30 30 30 30 31 0a 2a 2a 20 20 20 20 20 20  0000001.**      
e0f40 53 48 41 52 45 44 5f 52 41 4e 47 45 20 20 20 20  SHARED_RANGE    
e0f50 20 20 20 20 30 78 34 30 30 30 30 30 30 32 20 2d      0x40000002 -
e0f60 3e 20 30 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a  > 0x40000200.**.
e0f70 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 77 65  ** This works we
e0f80 6c 6c 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ll on the local 
e0f90 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 62 75 74  file system, but
e0fa0 20 73 68 6f 77 73 20 61 20 6e 65 61 72 6c 79 20   shows a nearly 
e0fb0 31 30 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e  100x.** slowdown
e0fc0 20 69 6e 20 72 65 61 64 20 70 65 72 66 6f 72 6d   in read perform
e0fd0 61 6e 63 65 20 6f 6e 20 41 46 50 20 62 65 63 61  ance on AFP beca
e0fe0 75 73 65 20 74 68 65 20 41 46 50 20 63 6c 69 65  use the AFP clie
e0ff0 6e 74 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74  nt disables.** t
e1000 68 65 20 72 65 61 64 20 63 61 63 68 65 20 77 68  he read cache wh
e1010 65 6e 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f  en byte-range lo
e1020 63 6b 73 20 61 72 65 20 70 72 65 73 65 6e 74 2e  cks are present.
e1030 20 20 45 6e 61 62 6c 69 6e 67 20 74 68 65 20 72    Enabling the r
e1040 65 61 64 0a 2a 2a 20 63 61 63 68 65 20 65 78 70  ead.** cache exp
e1050 6f 73 65 73 20 61 20 63 61 63 68 65 20 63 6f 68  oses a cache coh
e1060 65 72 65 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74  erency problem t
e1070 68 61 74 20 69 73 20 70 72 65 73 65 6e 74 20 6f  hat is present o
e1080 6e 20 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75  n all OS X.** su
e1090 70 70 6f 72 74 65 64 20 6e 65 74 77 6f 72 6b 20  pported network 
e10a0 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20 20 4e  file systems.  N
e10b0 46 53 20 61 6e 64 20 41 46 50 20 62 6f 74 68 20  FS and AFP both 
e10c0 6f 62 73 65 72 76 65 20 74 68 65 0a 2a 2a 20 63  observe the.** c
e10d0 6c 6f 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d  lose-to-open sem
e10e0 61 6e 74 69 63 73 20 66 6f 72 20 65 6e 73 75 72  antics for ensur
e10f0 69 6e 67 20 63 61 63 68 65 20 63 6f 68 65 72 65  ing cache cohere
e1100 6e 63 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e  ncy.** [http://n
e1110 66 73 2e 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e  fs.sourceforge.n
e1120 65 74 2f 23 66 61 71 5f 61 38 5d 2c 20 77 68 69  et/#faq_a8], whi
e1130 63 68 20 64 6f 65 73 20 6e 6f 74 20 65 66 66 65  ch does not effe
e1140 63 74 69 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65  ctively.** addre
e1150 73 73 20 74 68 65 20 72 65 71 75 69 72 65 6d 65  ss the requireme
e1160 6e 74 73 20 66 6f 72 20 63 6f 6e 63 75 72 72 65  nts for concurre
e1170 6e 74 20 64 61 74 61 62 61 73 65 20 61 63 63 65  nt database acce
e1180 73 73 20 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a  ss by multiple.*
e1190 2a 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72  * readers and wr
e11a0 69 74 65 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f  iters.** [http:/
e11b0 2f 77 77 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f  /www.nabble.com/
e11c0 53 51 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61  SQLite-on-NFS-ca
e11d0 63 68 65 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64  che-coherency-td
e11e0 31 35 36 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a  15655701.html]..
e11f0 2a 2a 0a 2a 2a 20 54 6f 20 61 64 64 72 65 73 73  **.** To address
e1200 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65   the performance
e1210 20 61 6e 64 20 63 61 63 68 65 20 63 6f 68 65 72   and cache coher
e1220 65 6e 63 79 20 69 73 73 75 65 73 2c 20 70 72 6f  ency issues, pro
e1230 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a  xy file locking.
e1240 2a 2a 20 63 68 61 6e 67 65 73 20 74 68 65 20 77  ** changes the w
e1250 61 79 20 64 61 74 61 62 61 73 65 20 61 63 63 65  ay database acce
e1260 73 73 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64  ss is controlled
e1270 20 62 79 20 6c 69 6d 69 74 69 6e 67 20 61 63 63   by limiting acc
e1280 65 73 73 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67  ess to a.** sing
e1290 6c 65 20 68 6f 73 74 20 61 74 20 61 20 74 69 6d  le host at a tim
e12a0 65 20 61 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c  e and moving fil
e12b0 65 20 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74  e locks off of t
e12c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
e12d0 0a 2a 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70  .** and onto a p
e12e0 72 6f 78 79 20 66 69 6c 65 20 6f 6e 20 74 68 65  roxy file on the
e12f0 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74   local file syst
e1300 65 6d 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55  em.  .**.**.** U
e1310 73 69 6e 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73  sing proxy locks
e1320 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .** ------------
e1330 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50  -----.**.** C AP
e1340 49 73 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65  Is.**.**  sqlite
e1350 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64  3_file_control(d
e1360 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54  b, dbname, SQLIT
e1370 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_SET_LOCKPROXYF
e1380 49 4c 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ILE,.**         
e1390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3c 70                <p
e13a0 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61  roxy_path> | ":a
e13b0 75 74 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69  uto:");.**  sqli
e13c0 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
e13d0 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c  (db, dbname, SQL
e13e0 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_GET_LOCKPROX
e13f0 59 46 49 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70  YFILE, &<proxy_p
e1400 61 74 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  ath>);.**.**.** 
e1410 53 51 4c 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a  SQL pragmas.**.*
e1420 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
e1430 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f  ase.]lock_proxy_
e1440 66 69 6c 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68  file=<proxy_path
e1450 3e 20 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50  > | :auto:.**  P
e1460 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
e1470 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65  ]lock_proxy_file
e1480 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e  .**.** Specifyin
e1490 67 20 22 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73  g ":auto:" means
e14a0 20 74 68 61 74 20 69 66 20 74 68 65 72 65 20 69   that if there i
e14b0 73 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 20 77  s a conch file w
e14c0 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a  ith a matching.*
e14d0 2a 20 68 6f 73 74 20 49 44 20 69 6e 20 69 74 2c  * host ID in it,
e14e0 20 74 68 65 20 70 72 6f 78 79 20 70 61 74 68 20   the proxy path 
e14f0 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  in the conch fil
e1500 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20  e will be used, 
e1510 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 61 20 70  otherwise.** a p
e1520 72 6f 78 79 20 70 61 74 68 20 62 61 73 65 64 20  roxy path based 
e1530 6f 6e 20 74 68 65 20 75 73 65 72 27 73 20 74 65  on the user's te
e1540 6d 70 20 64 69 72 0a 2a 2a 20 28 76 69 61 20 63  mp dir.** (via c
e1550 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49  onfstr(_CS_DARWI
e1560 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c  N_USER_TEMP_DIR,
e1570 2e 2e 2e 29 29 20 77 69 6c 6c 20 62 65 20 75 73  ...)) will be us
e1580 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63  ed and the.** ac
e1590 74 75 61 6c 20 70 72 6f 78 79 20 66 69 6c 65 20  tual proxy file 
e15a0 6e 61 6d 65 20 69 73 20 67 65 6e 65 72 61 74 65  name is generate
e15b0 64 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20  d from the name 
e15c0 61 6e 64 20 70 61 74 68 20 6f 66 20 74 68 65 0a  and path of the.
e15d0 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
e15e0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a  .  For example:.
e15f0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 46 6f 72 20  **.**       For 
e1600 64 61 74 61 62 61 73 65 20 70 61 74 68 20 22 2f  database path "/
e1610 55 73 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22  Users/me/foo.db"
e1620 20 0a 2a 2a 20 20 20 20 20 20 20 54 68 65 20 6c   .**       The l
e1630 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65  ock path will be
e1640 20 22 3c 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74   "<tmpdir>/sqlit
e1650 65 70 6c 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d  eplocks/_Users_m
e1660 65 5f 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29  e_foo.db:auto:")
e1670 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f  .**.** Once a lo
e1680 63 6b 20 70 72 6f 78 79 20 69 73 20 63 6f 6e 66  ck proxy is conf
e1690 69 67 75 72 65 64 20 66 6f 72 20 61 20 64 61 74  igured for a dat
e16a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
e16b0 2c 20 69 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20  , it can not.** 
e16c0 62 65 20 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65  be removed, howe
e16d0 76 65 72 20 69 74 20 6d 61 79 20 62 65 20 73 77  ver it may be sw
e16e0 69 74 63 68 65 64 20 74 6f 20 61 20 64 69 66 66  itched to a diff
e16f0 65 72 65 6e 74 20 70 72 6f 78 79 20 70 61 74 68  erent proxy path
e1700 20 76 69 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76   via.** the abov
e1710 65 20 41 50 49 73 20 28 61 73 73 75 6d 69 6e 67  e APIs (assuming
e1720 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
e1730 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c  is not being hel
e1740 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20  d by another.** 
e1750 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72  connection or pr
e1760 6f 63 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a  ocess). .**.**.*
e1770 2a 20 48 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b  * How proxy lock
e1780 69 6e 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d  ing works.** ---
e1790 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e17a0 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79  ----.**.** Proxy
e17b0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65   file locking re
e17c0 6c 69 65 73 20 70 72 69 6d 61 72 69 6c 79 20 6f  lies primarily o
e17d0 6e 20 74 77 6f 20 6e 65 77 20 73 75 70 70 6f 72  n two new suppor
e17e0 74 69 6e 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a  ting files: .**.
e17f0 2a 2a 20 20 20 2a 20 20 63 6f 6e 63 68 20 66 69  **   *  conch fi
e1800 6c 65 20 74 6f 20 6c 69 6d 69 74 20 61 63 63 65  le to limit acce
e1810 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ss to the databa
e1820 73 65 20 66 69 6c 65 20 74 6f 20 61 20 73 69 6e  se file to a sin
e1830 67 6c 65 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20  gle host.**     
e1840 20 61 74 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a   at a time.**.**
e1850 20 20 20 2a 20 20 70 72 6f 78 79 20 66 69 6c 65     *  proxy file
e1860 20 74 6f 20 61 63 74 20 61 73 20 61 20 70 72 6f   to act as a pro
e1870 78 79 20 66 6f 72 20 74 68 65 20 61 64 76 69 73  xy for the advis
e1880 6f 72 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c  ory locks normal
e1890 6c 79 0a 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e  ly.**      taken
e18a0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
e18b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68  .**.** The conch
e18c0 20 66 69 6c 65 20 2d 20 74 6f 20 75 73 65 20 61   file - to use a
e18d0 20 70 72 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c   proxy file, sql
e18e0 69 74 65 20 6d 75 73 74 20 66 69 72 73 74 20 22  ite must first "
e18f0 68 6f 6c 64 20 74 68 65 20 63 6f 6e 63 68 22 0a  hold the conch".
e1900 2a 2a 20 62 79 20 74 61 6b 69 6e 67 20 61 6e 20  ** by taking an 
e1910 73 71 6c 69 74 65 2d 73 74 79 6c 65 20 73 68 61  sqlite-style sha
e1920 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  red lock on the 
e1930 63 6f 6e 63 68 20 66 69 6c 65 2c 20 72 65 61 64  conch file, read
e1940 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65  ing the.** conte
e1950 6e 74 73 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e  nts and comparin
e1960 67 20 74 68 65 20 68 6f 73 74 27 73 20 75 6e 69  g the host's uni
e1970 71 75 65 20 68 6f 73 74 20 49 44 20 28 73 65 65  que host ID (see
e1980 20 62 65 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b   below) and lock
e1990 0a 2a 2a 20 70 72 6f 78 79 20 70 61 74 68 20 61  .** proxy path a
e19a0 67 61 69 6e 73 74 20 74 68 65 20 76 61 6c 75 65  gainst the value
e19b0 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
e19c0 63 6f 6e 63 68 2e 20 20 54 68 65 20 63 6f 6e 63  conch.  The conc
e19d0 68 20 66 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f  h file is.** sto
e19e0 72 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  red in the same 
e19f0 64 69 72 65 63 74 6f 72 79 20 61 73 20 74 68 65  directory as the
e1a00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
e1a10 6e 64 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65  nd the file name
e1a20 0a 2a 2a 20 69 73 20 70 61 74 74 65 72 6e 65 64  .** is patterned
e1a30 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 62   after the datab
e1a40 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 73  ase file name as
e1a50 20 22 2e 3c 64 61 74 61 62 61 73 65 6e 61 6d 65   ".<databasename
e1a60 3e 2d 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20  >-conch"..** If 
e1a70 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 64  the conch file d
e1a80 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f  oes not exist, o
e1a90 72 20 69 74 27 73 20 63 6f 6e 74 65 6e 74 73 20  r it's contents 
e1aa0 64 6f 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  do not match the
e1ab0 0a 2a 2a 20 68 6f 73 74 20 49 44 20 61 6e 64 2f  .** host ID and/
e1ac0 6f 72 20 70 72 6f 78 79 20 70 61 74 68 2c 20 74  or proxy path, t
e1ad0 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  hen the lock is 
e1ae0 65 73 63 61 6c 61 74 65 64 20 74 6f 20 61 6e 20  escalated to an 
e1af0 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63  exclusive.** loc
e1b00 6b 20 61 6e 64 20 74 68 65 20 63 6f 6e 63 68 20  k and the conch 
e1b10 66 69 6c 65 20 63 6f 6e 74 65 6e 74 73 20 69 73  file contents is
e1b20 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68   updated with th
e1b30 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20 70 72  e host ID and pr
e1b40 6f 78 79 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20  oxy.** path and 
e1b50 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e  the lock is down
e1b60 67 72 61 64 65 64 20 74 6f 20 61 20 73 68 61 72  graded to a shar
e1b70 65 64 20 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20  ed lock again.  
e1b80 49 66 20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20  If the conch.** 
e1b90 69 73 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68  is held by anoth
e1ba0 65 72 20 70 72 6f 63 65 73 73 20 28 77 69 74 68  er process (with
e1bb0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c   a shared lock),
e1bc0 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c   the exclusive l
e1bd0 6f 63 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c  ock.** will fail
e1be0 20 61 6e 64 20 53 51 4c 49 54 45 5f 42 55 53 59   and SQLITE_BUSY
e1bf0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
e1c00 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69  .** The proxy fi
e1c10 6c 65 20 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79  le - a single-by
e1c20 74 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72  te file used for
e1c30 20 61 6c 6c 20 61 64 76 69 73 6f 72 79 20 66 69   all advisory fi
e1c40 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d  le locks.** norm
e1c50 61 6c 6c 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68  ally taken on th
e1c60 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
e1c70 20 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 66     This allows f
e1c80 6f 72 20 73 61 66 65 20 73 68 61 72 69 6e 67 0a  or safe sharing.
e1c90 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ** of the databa
e1ca0 73 65 20 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74  se file for mult
e1cb0 69 70 6c 65 20 72 65 61 64 65 72 73 20 61 6e 64  iple readers and
e1cc0 20 77 72 69 74 65 72 73 20 6f 6e 20 74 68 65 20   writers on the 
e1cd0 73 61 6d 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68  same.** host (th
e1ce0 65 20 63 6f 6e 63 68 20 65 6e 73 75 72 65 73 20  e conch ensures 
e1cf0 74 68 61 74 20 74 68 65 79 20 61 6c 6c 20 75 73  that they all us
e1d00 65 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c  e the same local
e1d10 20 6c 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a   lock file)..**.
e1d20 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 68  ** There is a th
e1d30 69 72 64 20 66 69 6c 65 20 2d 20 74 68 65 20 68  ird file - the h
e1d40 6f 73 74 20 49 44 20 66 69 6c 65 20 2d 20 75 73  ost ID file - us
e1d50 65 64 20 61 73 20 61 20 70 65 72 73 69 73 74 65  ed as a persiste
e1d60 6e 74 20 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20  nt record.** of 
e1d70 61 20 75 6e 69 71 75 65 20 69 64 65 6e 74 69 66  a unique identif
e1d80 69 65 72 20 66 6f 72 20 74 68 65 20 68 6f 73 74  ier for the host
e1d90 2c 20 61 20 31 32 38 2d 62 79 74 65 20 75 6e 69  , a 128-byte uni
e1da0 71 75 65 20 68 6f 73 74 20 69 64 20 66 69 6c 65  que host id file
e1db0 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 74 68 20  .** in the path 
e1dc0 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 20 48  defined by the H
e1dd0 4f 53 54 49 44 50 41 54 48 20 6d 61 63 72 6f 20  OSTIDPATH macro 
e1de0 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69  (default value i
e1df0 73 0a 2a 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61  s.** /Library/Ca
e1e00 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e  ches/.com.apple.
e1e10 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49  sqliteConchHostI
e1e20 64 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73  d)..**.** Reques
e1e30 74 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 70 72  ting the lock pr
e1e40 6f 78 79 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d  oxy does not imm
e1e50 65 64 69 61 74 65 6c 79 20 74 61 6b 65 20 74 68  ediately take th
e1e60 65 20 63 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a  e conch, it is.*
e1e70 2a 20 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65  * only taken whe
e1e80 6e 20 74 68 65 20 66 69 72 73 74 20 72 65 71 75  n the first requ
e1e90 65 73 74 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61  est to lock data
e1ea0 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d 61 64  base file is mad
e1eb0 65 2e 20 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74  e.  .** This mat
e1ec0 63 68 65 73 20 74 68 65 20 73 65 6d 61 6e 74 69  ches the semanti
e1ed0 63 73 20 6f 66 20 74 68 65 20 74 72 61 64 69 74  cs of the tradit
e1ee0 69 6f 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65  ional locking be
e1ef0 68 61 76 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a  havior, where.**
e1f00 20 6f 70 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65   opening a conne
e1f10 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62  ction to a datab
e1f20 61 73 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f  ase file does no
e1f30 74 20 74 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e  t take a lock on
e1f40 20 69 74 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72   it..** The shar
e1f50 65 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f  ed lock and an o
e1f60 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70  pen file descrip
e1f70 74 6f 72 20 61 72 65 20 6d 61 69 6e 74 61 69 6e  tor are maintain
e1f80 65 64 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65  ed until .** the
e1f90 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74   connection to t
e1fa0 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63  he database is c
e1fb0 6c 6f 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  losed. .**.** Th
e1fc0 65 20 70 72 6f 78 79 20 66 69 6c 65 20 61 6e 64  e proxy file and
e1fd0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61   the lock file a
e1fe0 72 65 20 6e 65 76 65 72 20 64 65 6c 65 74 65 64  re never deleted
e1ff0 20 73 6f 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65   so they only ne
e2000 65 64 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61  ed.** to be crea
e2010 74 65 64 20 74 68 65 20 66 69 72 73 74 20 74 69  ted the first ti
e2020 6d 65 20 74 68 65 79 20 61 72 65 20 75 73 65 64  me they are used
e2030 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72  ..**.** Configur
e2040 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a  ation options.**
e2050 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
e2060 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51  ------.**.**  SQ
e2070 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58  LITE_PREFER_PROX
e2080 59 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20  Y_LOCKING.**.** 
e2090 20 20 20 20 20 20 44 61 74 61 62 61 73 65 20 66        Database f
e20a0 69 6c 65 73 20 61 63 63 65 73 73 65 64 20 6f 6e  iles accessed on
e20b0 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20   non-local file 
e20c0 73 79 73 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20  systems are.**  
e20d0 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c       automatical
e20e0 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f  ly configured fo
e20f0 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c  r proxy locking,
e2100 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 72 65 0a   lock files are.
e2110 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 64 20 61  **       named a
e2120 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 75 73 69  utomatically usi
e2130 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69  ng the same logi
e2140 63 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 50 52  c as.**       PR
e2150 41 47 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f  AGMA lock_proxy_
e2160 66 69 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a  file=":auto:".**
e2170 20 20 20 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f      .**  SQLITE_
e2180 50 52 4f 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a  PROXY_DEBUG.**.*
e2190 2a 20 20 20 20 20 20 20 45 6e 61 62 6c 65 73 20  *       Enables 
e21a0 74 68 65 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65  the logging of e
e21b0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 64 75  rror messages du
e21c0 72 69 6e 67 20 68 6f 73 74 20 69 64 20 66 69 6c  ring host id fil
e21d0 65 0a 2a 2a 20 20 20 20 20 20 20 72 65 74 72 69  e.**       retri
e21e0 65 76 61 6c 20 61 6e 64 20 63 72 65 61 74 69 6f  eval and creatio
e21f0 6e 0a 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50  n.**.**  HOSTIDP
e2200 41 54 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ATH.**.**       
e2210 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 64 65  Overrides the de
e2220 66 61 75 6c 74 20 68 6f 73 74 20 49 44 20 66 69  fault host ID fi
e2230 6c 65 20 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e  le path location
e2240 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58  .**.**  LOCKPROX
e2250 59 44 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  YDIR.**.**      
e2260 20 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 64   Overrides the d
e2270 65 66 61 75 6c 74 20 64 69 72 65 63 74 6f 72 79  efault directory
e2280 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70   used for lock p
e2290 72 6f 78 79 20 66 69 6c 65 73 20 74 68 61 74 0a  roxy files that.
e22a0 2a 2a 20 20 20 20 20 20 20 61 72 65 20 6e 61 6d  **       are nam
e22b0 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
e22c0 20 76 69 61 20 74 68 65 20 22 3a 61 75 74 6f 3a   via the ":auto:
e22d0 22 20 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20  " setting.**.** 
e22e0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
e22f0 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53  PROXYDIR_PERMISS
e2300 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  IONS.**.**      
e2310 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20   Permissions to 
e2320 75 73 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e  use when creatin
e2330 67 20 61 20 64 69 72 65 63 74 6f 72 79 20 66 6f  g a directory fo
e2340 72 20 73 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a  r storing the.**
e2350 20 20 20 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78         lock prox
e2360 79 20 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73  y files, only us
e2370 65 64 20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58  ed when LOCKPROX
e2380 59 44 49 52 20 69 73 20 6e 6f 74 20 73 65 74 2e  YDIR is not set.
e2390 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a  .**    .**    .*
e23a0 2a 20 41 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61  * As mentioned a
e23b0 62 6f 76 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69  bove, when compi
e23c0 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f  led with SQLITE_
e23d0 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43  PREFER_PROXY_LOC
e23e0 4b 49 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67  KING,.** setting
e23f0 20 74 68 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74   the environment
e2400 20 76 61 72 69 61 62 6c 65 20 53 51 4c 49 54 45   variable SQLITE
e2410 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43  _FORCE_PROXY_LOC
e2420 4b 49 4e 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a  KING to 1 will.*
e2430 2a 20 66 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f  * force proxy lo
e2440 63 6b 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64  cking to be used
e2450 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62   for every datab
e2460 61 73 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c  ase file opened,
e2470 20 61 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66   and 0.** will f
e2480 6f 72 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70  orce automatic p
e2490 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20  roxy locking to 
e24a0 62 65 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20  be disabled for 
e24b0 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  all database.** 
e24c0 66 69 6c 65 73 20 28 65 78 70 6c 69 63 69 74 79  files (explicity
e24d0 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c   calling the SQL
e24e0 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_SET_LOCKPROX
e24f0 59 46 49 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a  YFILE pragma or.
e2500 2a 2a 20 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63  ** sqlite_file_c
e2510 6f 6e 74 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f  ontrol API is no
e2520 74 20 61 66 66 65 63 74 65 64 20 62 79 20 53 51  t affected by SQ
e2530 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59  LITE_FORCE_PROXY
e2540 5f 4c 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f  _LOCKING)..*/../
e2550 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69  *.** Proxy locki
e2560 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  ng is only avail
e2570 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a  able on MacOSX .
e2580 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  */.#if defined(_
e2590 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
e25a0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e25b0 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66 64 65 66  NG_STYLE..#ifdef
e25c0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20   SQLITE_TEST./* 
e25d0 73 69 6d 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c  simulate multipl
e25e0 65 20 68 6f 73 74 73 20 62 79 20 63 72 65 61 74  e hosts by creat
e25f0 69 6e 67 20 75 6e 69 71 75 65 20 68 6f 73 74 69  ing unique hosti
e2600 64 20 66 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a  d file paths */.
e2610 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
e2620 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75  qlite3_hostid_nu
e2630 6d 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  m = 0;.#endif../
e2640 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f  *.** The proxyLo
e2650 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73  ckingContext has
e2660 20 74 68 65 20 70 61 74 68 20 61 6e 64 20 66 69   the path and fi
e2670 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 66 6f  le structures fo
e2680 72 20 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a  r the remote .**
e2690 20 61 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79   and local proxy
e26a0 20 66 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a   files in it.*/.
e26b0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 70  typedef struct p
e26c0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
e26d0 78 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43  xt proxyLockingC
e26e0 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70  ontext;.struct p
e26f0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
e2700 78 74 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  xt {.  unixFile 
e2710 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20  *conchFile;     
e2720 20 20 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63      /* Open conc
e2730 68 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72  h file */.  char
e2740 20 2a 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b   *conchFilePath;
e2750 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
e2760 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69   of the conch fi
e2770 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65  le */.  unixFile
e2780 20 2a 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20   *lockProxy;    
e2790 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f       /* Open pro
e27a0 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a  xy lock file */.
e27b0 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78    char *lockProx
e27c0 79 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f  yPath;         /
e27d0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72  * Name of the pr
e27e0 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f  oxy lock file */
e27f0 0a 20 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b  .  char *dbPath;
e2800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2810 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f  /* Name of the o
e2820 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e  pen file */.  in
e2830 74 20 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20  t conchHeld;    
e2840 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
e2850 75 65 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20  ue if the conch 
e2860 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c  is currently hel
e2870 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64  d */.  void *old
e2880 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20  LockingContext; 
e2890 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20      /* Original 
e28a0 6c 6f 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74  lockingcontext t
e28b0 6f 20 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f  o restore on clo
e28c0 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  se */.  sqlite3_
e28d0 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74  io_methods const
e28e0 20 2a 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20   *pOldMethod;   
e28f0 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f    /* Original I/
e2900 4f 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 63 6c  O methods for cl
e2910 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f  ose */.};../* HO
e2920 53 54 49 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43  STIDLEN and CONC
e2930 48 4c 45 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64  HLEN both includ
e2940 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  e space for the 
e2950 73 74 72 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69  string .** termi
e2960 6e 61 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23  nating nul .*/.#
e2970 64 65 66 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e  define HOSTIDLEN
e2980 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65           128.#de
e2990 66 69 6e 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20  fine CONCHLEN   
e29a0 20 20 20 20 20 20 20 28 4d 41 58 50 41 54 48 4c         (MAXPATHL
e29b0 45 4e 2b 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a  EN+HOSTIDLEN+1).
e29c0 23 69 66 6e 64 65 66 20 48 4f 53 54 49 44 50 41  #ifndef HOSTIDPA
e29d0 54 48 0a 23 20 64 65 66 69 6e 65 20 48 4f 53 54  TH.# define HOST
e29e0 49 44 50 41 54 48 20 20 20 20 20 20 20 22 2f 4c  IDPATH       "/L
e29f0 69 62 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63  ibrary/Caches/.c
e2a00 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43  om.apple.sqliteC
e2a10 6f 6e 63 68 48 6f 73 74 49 64 22 0a 23 65 6e 64  onchHostId".#end
e2a20 69 66 0a 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79  if../* basically
e2a30 20 61 20 63 6f 70 79 20 6f 66 20 75 6e 69 78 52   a copy of unixR
e2a40 61 6e 64 6f 6d 6e 65 73 73 20 77 69 74 68 20 64  andomness with d
e2a50 69 66 66 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74  ifferent.** test
e2a60 20 62 65 68 61 76 69 6f 72 20 62 75 69 6c 74 20   behavior built 
e2a70 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  in */.static int
e2a80 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f   proxyGenerateHo
e2a90 73 74 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74  stID(char *pHost
e2aa0 49 44 29 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20  ID){.  int pid, 
e2ab0 66 64 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67  fd, len;.  unsig
e2ac0 6e 65 64 20 63 68 61 72 20 2a 6b 65 79 20 3d 20  ned char *key = 
e2ad0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
e2ae0 29 70 48 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d  )pHostID;.  .  m
e2af0 65 6d 73 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f  emset(key, 0, HO
e2b00 53 54 49 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20  STIDLEN);.  len 
e2b10 3d 20 30 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e  = 0;.  fd = open
e2b20 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c  ("/dev/urandom",
e2b30 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66   O_RDONLY);.  if
e2b40 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c  ( fd>=0 ){.    l
e2b50 65 6e 20 3d 20 72 65 61 64 28 66 64 2c 20 6b 65  en = read(fd, ke
e2b60 79 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20  y, HOSTIDLEN);. 
e2b70 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a     close(fd); /*
e2b80 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74   silently leak t
e2b90 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c  he fd if it fail
e2ba0 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c  s */.  }.  if( l
e2bb0 65 6e 20 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29  en < HOSTIDLEN )
e2bc0 7b 0a 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a  {.    time_t t;.
e2bd0 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20      time(&t);.  
e2be0 20 20 6d 65 6d 63 70 79 28 6b 65 79 2c 20 26 74    memcpy(key, &t
e2bf0 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20  , sizeof(t));.  
e2c00 20 20 70 69 64 20 3d 20 67 65 74 70 69 64 28 29    pid = getpid()
e2c10 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 6b 65  ;.    memcpy(&ke
e2c20 79 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70  y[sizeof(t)], &p
e2c30 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29  id, sizeof(pid))
e2c40 3b 0a 20 20 7d 0a 20 20 0a 23 69 66 64 65 66 20  ;.  }.  .#ifdef 
e2c50 4d 41 4b 45 5f 50 52 45 54 54 59 5f 48 4f 53 54  MAKE_PRETTY_HOST
e2c60 49 44 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69  ID.  {.    int i
e2c70 3b 0a 20 20 20 20 2f 2a 20 66 69 6c 74 65 72 20  ;.    /* filter 
e2c80 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 70  the bytes into p
e2c90 72 69 6e 74 61 62 6c 65 20 61 73 63 69 69 20 63  rintable ascii c
e2ca0 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 4e 55  haracters and NU
e2cb0 4c 20 74 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20  L terminate */. 
e2cc0 20 20 20 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45     key[(HOSTIDLE
e2cd0 4e 2d 31 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20  N-1)] = 0x00;.  
e2ce0 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48    for( i=0; i<(H
e2cf0 4f 53 54 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b  OSTIDLEN-1); i++
e2d00 20 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e   ){.      unsign
e2d10 65 64 20 63 68 61 72 20 70 61 20 3d 20 6b 65 79  ed char pa = key
e2d20 5b 69 5d 26 30 78 37 46 3b 0a 20 20 20 20 20 20  [i]&0x7F;.      
e2d30 69 66 28 20 70 61 3c 30 78 32 30 20 29 7b 0a 20  if( pa<0x20 ){. 
e2d40 20 20 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20         key[i] = 
e2d50 28 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20  (key[i]&0x80 == 
e2d60 30 78 38 30 29 20 3f 20 70 61 2b 30 78 34 30 20  0x80) ? pa+0x40 
e2d70 3a 20 70 61 2b 30 78 32 30 3b 0a 20 20 20 20 20  : pa+0x20;.     
e2d80 20 7d 65 6c 73 65 20 69 66 28 20 70 61 3d 3d 30   }else if( pa==0
e2d90 78 37 46 20 29 7b 0a 20 20 20 20 20 20 20 20 6b  x7F ){.        k
e2da0 65 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26  ey[i] = (key[i]&
e2db0 30 78 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20  0x80 == 0x80) ? 
e2dc0 70 61 3d 30 78 32 30 20 3a 20 70 61 2b 30 78 37  pa=0x20 : pa+0x7
e2dd0 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  E;.      }.    }
e2de0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65  .  }.#endif.  re
e2df0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e2e00 7d 0a 0a 2f 2a 20 77 72 69 74 65 73 20 74 68 65  }../* writes the
e2e10 20 68 6f 73 74 20 69 64 20 70 61 74 68 20 74 6f   host id path to
e2e20 20 70 61 74 68 2c 20 70 61 74 68 20 73 68 6f 75   path, path shou
e2e30 6c 64 20 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c  ld be an pre-all
e2e40 6f 63 61 74 65 64 20 62 75 66 66 65 72 0a 2a 2a  ocated buffer.**
e2e50 20 77 69 74 68 20 65 6e 6f 75 67 68 20 73 70 61   with enough spa
e2e60 63 65 20 66 6f 72 20 61 20 70 61 74 68 20 0a 2a  ce for a path .*
e2e70 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 72  /.static void pr
e2e80 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61 74 68  oxyGetHostIDPath
e2e90 28 63 68 61 72 20 2a 70 61 74 68 2c 20 73 69 7a  (char *path, siz
e2ea0 65 5f 74 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c  e_t len){.  strl
e2eb0 63 70 79 28 70 61 74 68 2c 20 48 4f 53 54 49 44  cpy(path, HOSTID
e2ec0 50 41 54 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64  PATH, len);.#ifd
e2ed0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
e2ee0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 68 6f 73   if( sqlite3_hos
e2ef0 74 69 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20  tid_num>0 ){.   
e2f00 20 63 68 61 72 20 73 75 66 66 69 78 5b 32 5d 20   char suffix[2] 
e2f10 3d 20 22 31 22 3b 0a 20 20 20 20 73 75 66 66 69  = "1";.    suffi
e2f20 78 5b 30 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d  x[0] = suffix[0]
e2f30 20 2b 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69   + sqlite3_hosti
e2f40 64 5f 6e 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63  d_num;.    strlc
e2f50 61 74 28 70 61 74 68 2c 20 73 75 66 66 69 78 2c  at(path, suffix,
e2f60 20 6c 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69   len);.  }.#endi
e2f70 66 0a 20 20 4f 53 54 52 41 43 45 33 28 22 47 45  f.  OSTRACE3("GE
e2f80 54 48 4f 53 54 49 44 50 41 54 48 20 20 25 73 20  THOSTIDPATH  %s 
e2f90 70 69 64 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c  pid=%d\n", path,
e2fa0 20 67 65 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f   getpid());.}../
e2fb0 2a 20 67 65 74 20 74 68 65 20 68 6f 73 74 20 49  * get the host I
e2fc0 44 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 20  D from a sqlite 
e2fd0 68 6f 73 74 69 64 20 66 69 6c 65 20 73 74 6f 72  hostid file stor
e2fe0 65 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73  ed in the .** us
e2ff0 65 72 2d 73 70 65 63 69 66 69 63 20 74 6d 70 20  er-specific tmp 
e3000 64 69 72 65 63 74 6f 72 79 2c 20 63 72 65 61 74  directory, creat
e3010 65 20 74 68 65 20 49 44 20 69 66 20 69 74 27 73  e the ID if it's
e3020 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61   not there alrea
e3030 64 79 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  dy .*/.static in
e3040 74 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44  t proxyGetHostID
e3050 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 2c 20  (char *pHostID, 
e3060 69 6e 74 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20  int *pError){.  
e3070 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20 70  int fd;.  char p
e3080 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b  ath[MAXPATHLEN];
e3090 20 0a 20 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a   .  size_t len;.
e30a0 20 20 69 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f    int rc=SQLITE_
e30b0 4f 4b 3b 0a 0a 20 20 70 72 6f 78 79 47 65 74 48  OK;..  proxyGetH
e30c0 6f 73 74 49 44 50 61 74 68 28 70 61 74 68 2c 20  ostIDPath(path, 
e30d0 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f  MAXPATHLEN);.  /
e30e0 2a 20 74 72 79 20 74 6f 20 63 72 65 61 74 65 20  * try to create 
e30f0 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65  the host ID file
e3100 2c 20 69 66 20 69 74 20 61 6c 72 65 61 64 79 20  , if it already 
e3110 65 78 69 73 74 73 20 72 65 61 64 20 74 68 65 20  exists read the 
e3120 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64  contents */.  fd
e3130 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f   = open(path, O_
e3140 43 52 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f  CREAT|O_WRONLY|O
e3150 5f 45 58 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20  _EXCL, 0644);.  
e3160 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20  if( fd<0 ){.    
e3170 69 6e 74 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09  int err=errno;..
e3180 09 0a 20 20 20 20 69 66 28 20 65 72 72 21 3d 45  ..    if( err!=E
e3190 45 58 49 53 54 20 29 7b 0a 23 69 66 64 65 66 20  EXIST ){.#ifdef 
e31a0 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42  SQLITE_PROXY_DEB
e31b0 55 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71  UG /* set the sq
e31c0 6c 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61  lite error messa
e31d0 67 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20  ge instead */.  
e31e0 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
e31f0 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72 72 6f  rr, "sqlite erro
e3200 72 20 63 72 65 61 74 69 6e 67 20 68 6f 73 74 20  r creating host 
e3210 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e  ID file %s: %s\n
e3220 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
e3230 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28   path, strerror(
e3240 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20  err));.#endif.  
e3250 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e3260 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20  E_PERM;.    }.  
e3270 20 20 2f 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72    /* couldn't cr
e3280 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20 72  eate the file, r
e3290 65 61 64 20 69 74 20 69 6e 73 74 65 61 64 20 2a  ead it instead *
e32a0 2f 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28  /.    fd = open(
e32b0 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f  path, O_RDONLY|O
e32c0 5f 45 58 43 4c 29 3b 0a 20 20 20 20 69 66 28 20  _EXCL);.    if( 
e32d0 66 64 3c 30 20 29 7b 0a 23 69 66 64 65 66 20 53  fd<0 ){.#ifdef S
e32e0 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55  QLITE_PROXY_DEBU
e32f0 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c  G /* set the sql
e3300 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ite error messag
e3310 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20  e instead */.   
e3320 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72     int err = err
e3330 6e 6f 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74  no;.      fprint
e3340 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74  f(stderr, "sqlit
e3350 65 20 65 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20  e error opening 
e3360 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a  host ID file %s:
e3370 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
e3380 20 20 20 20 20 20 70 61 74 68 2c 20 73 74 72 65        path, stre
e3390 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64  rror(err));.#end
e33a0 69 66 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  if.      return 
e33b0 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20  SQLITE_PERM;.   
e33c0 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 70 72 65   }.    len = pre
e33d0 61 64 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20  ad(fd, pHostID, 
e33e0 48 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20  HOSTIDLEN, 0);. 
e33f0 20 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a     if( len<0 ){.
e3400 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20        *pError = 
e3410 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20  errno;.      rc 
e3420 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  = SQLITE_IOERR_R
e3430 45 41 44 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  EAD;.    }else i
e3440 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e  f( len<HOSTIDLEN
e3450 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f   ){.      *pErro
e3460 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  r = 0;.      rc 
e3470 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  = SQLITE_IOERR_S
e3480 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d  HORT_READ;.    }
e3490 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20  .    close(fd); 
e34a0 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b  /* silently leak
e34b0 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61   the fd if it fa
e34c0 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41  ils */.    OSTRA
e34d0 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 20  CE3("GETHOSTID  
e34e0 72 65 61 64 20 25 73 20 70 69 64 3d 25 64 5c 6e  read %s pid=%d\n
e34f0 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70  ", pHostID, getp
e3500 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72  id());.    retur
e3510 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n rc;.  }else{. 
e3520 20 20 20 2f 2a 20 77 65 27 72 65 20 63 72 65 61     /* we're crea
e3530 74 69 6e 67 20 74 68 65 20 68 6f 73 74 20 49 44  ting the host ID
e3540 20 66 69 6c 65 20 28 75 73 65 20 61 20 72 61 6e   file (use a ran
e3550 64 6f 6d 20 73 74 72 69 6e 67 20 6f 66 20 62 79  dom string of by
e3560 74 65 73 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78  tes) */.    prox
e3570 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28  yGenerateHostID(
e3580 70 48 6f 73 74 49 44 29 3b 0a 20 20 20 20 6c 65  pHostID);.    le
e3590 6e 20 3d 20 70 77 72 69 74 65 28 66 64 2c 20 70  n = pwrite(fd, p
e35a0 48 6f 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45  HostID, HOSTIDLE
e35b0 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c  N, 0);.    if( l
e35c0 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70  en<0 ){.      *p
e35d0 45 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20  Error = errno;. 
e35e0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
e35f0 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20  _IOERR_WRITE;.  
e3600 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c    }else if( len<
e3610 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20  HOSTIDLEN ){.   
e3620 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a     *pError = 0;.
e3630 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e3640 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20  E_IOERR_WRITE;. 
e3650 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66     }.    close(f
e3660 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20  d); /* silently 
e3670 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20 69  leak the fd if i
e3680 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f  t fails */.    O
e3690 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53 54  STRACE3("GETHOST
e36a0 49 44 20 20 77 72 6f 74 65 20 25 73 20 70 69 64  ID  wrote %s pid
e36b0 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c  =%d\n", pHostID,
e36c0 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20   getpid());.    
e36d0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d  return rc;.  }.}
e36e0 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  ..static int pro
e36f0 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f  xyGetLockPath(co
e3700 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 74 68  nst char *dbPath
e3710 2c 20 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73  , char *lPath, s
e3720 69 7a 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20  ize_t maxLen){. 
e3730 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20   int len;.  int 
e3740 64 62 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a  dbLen;.  int i;.
e3750 0a 23 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58  .#ifdef LOCKPROX
e3760 59 44 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72  YDIR.  len = str
e3770 6c 63 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b  lcpy(lPath, LOCK
e3780 50 52 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e  PROXYDIR, maxLen
e3790 29 3b 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66  );.#else.# ifdef
e37a0 20 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52   _CS_DARWIN_USER
e37b0 5f 54 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20  _TEMP_DIR.  {.  
e37c0 20 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41    confstr(_CS_DA
e37d0 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44  RWIN_USER_TEMP_D
e37e0 49 52 2c 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65  IR, lPath, maxLe
e37f0 6e 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74  n);.    len = st
e3800 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71  rlcat(lPath, "sq
e3810 6c 69 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78  liteplocks", max
e3820 4c 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b  Len);.    if( mk
e3830 64 69 72 28 6c 50 61 74 68 2c 20 53 51 4c 49 54  dir(lPath, SQLIT
e3840 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44  E_DEFAULT_PROXYD
e3850 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 20  IR_PERMISSIONS) 
e3860 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 6d  ){.      /* if m
e3870 6b 64 69 72 20 66 61 69 6c 73 2c 20 68 61 6e 64  kdir fails, hand
e3880 6c 65 20 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20  le as lock file 
e3890 63 72 65 61 74 69 6f 6e 20 66 61 69 6c 75 72 65  creation failure
e38a0 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 65 72   */.      int er
e38b0 72 20 3d 20 65 72 72 6e 6f 3b 0a 23 20 20 69 66  r = errno;.#  if
e38c0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
e38d0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 21 3d  .      if( err!=
e38e0 45 45 58 49 53 54 20 29 7b 0a 20 20 20 20 20 20  EEXIST ){.      
e38f0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
e3900 2c 20 22 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50  , "proxyGetLockP
e3910 61 74 68 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25  ath: mkdir(%s,0%
e3920 6f 29 20 65 72 72 6f 72 20 25 64 20 25 73 5c 6e  o) error %d %s\n
e3930 22 2c 20 6c 50 61 74 68 2c 0a 20 20 20 20 20 20  ", lPath,.      
e3940 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
e3950 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49  _DEFAULT_PROXYDI
e3960 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65  R_PERMISSIONS, e
e3970 72 72 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72  rr, strerror(err
e3980 29 29 3b 0a 20 20 20 20 20 20 7d 0a 23 20 20 65  ));.      }.#  e
e3990 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ndif.    }else{.
e39a0 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22        OSTRACE3("
e39b0 47 45 54 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64  GETLOCKPATH  mkd
e39c0 69 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c  ir %s pid=%d\n",
e39d0 20 6c 50 61 74 68 2c 20 67 65 74 70 69 64 28 29   lPath, getpid()
e39e0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20  );.    }.    .  
e39f0 7d 0a 23 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d  }.# else.  len =
e3a00 20 73 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20   strlcpy(lPath, 
e3a10 22 2f 74 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29  "/tmp/", maxLen)
e3a20 3b 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  ;.# endif.#endif
e3a30 0a 0a 20 20 69 66 28 20 6c 50 61 74 68 5b 6c 65  ..  if( lPath[le
e3a40 6e 2d 31 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20  n-1]!='/' ){.   
e3a50 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c   len = strlcat(l
e3a60 50 61 74 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65  Path, "/", maxLe
e3a70 6e 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  n);.  }.  .  /* 
e3a80 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 64 62  transform the db
e3a90 20 70 61 74 68 20 74 6f 20 61 20 75 6e 69 71 75   path to a uniqu
e3aa0 65 20 63 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a  e cache name */.
e3ab0 20 20 64 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73    dbLen = (int)s
e3ac0 74 72 6c 65 6e 28 64 62 50 61 74 68 29 3b 0a 20  trlen(dbPath);. 
e3ad0 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c   for( i=0; i<dbL
e3ae0 65 6e 20 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c  en && (i+len+7)<
e3af0 6d 61 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20  maxLen; i++){.  
e3b00 20 20 63 68 61 72 20 63 20 3d 20 64 62 50 61 74    char c = dbPat
e3b10 68 5b 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b  h[i];.    lPath[
e3b20 69 2b 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27  i+len] = (c=='/'
e3b30 29 3f 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c  )?'_':c;.  }.  l
e3b40 50 61 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27  Path[i+len]='\0'
e3b50 3b 0a 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74  ;.  strlcat(lPat
e3b60 68 2c 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78  h, ":auto:", max
e3b70 4c 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Len);.  return S
e3b80 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
e3b90 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
e3ba0 56 46 53 20 66 69 6c 65 20 64 65 73 63 72 69 70  VFS file descrip
e3bb0 74 6f 72 20 28 73 74 6f 72 65 64 20 69 6e 20 6d  tor (stored in m
e3bc0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
e3bd0 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  rom.** sqlite3_m
e3be0 61 6c 6c 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20  alloc) and open 
e3bf0 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 22  the file named "
e3c00 70 61 74 68 22 20 69 6e 20 74 68 65 20 66 69 6c  path" in the fil
e3c10 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a  e descriptor..**
e3c20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69  .** The caller i
e3c30 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f  s responsible no
e3c40 74 20 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69  t only for closi
e3c50 6e 67 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  ng the file desc
e3c60 72 69 70 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c  riptor.** but al
e3c70 73 6f 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74  so for freeing t
e3c80 68 65 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69  he memory associ
e3c90 61 74 65 64 20 77 69 74 68 20 74 68 65 20 66 69  ated with the fi
e3ca0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a  le descriptor..*
e3cb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
e3cc0 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65  xyCreateUnixFile
e3cd0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74  (const char *pat
e3ce0 68 2c 20 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70  h, unixFile **pp
e3cf0 46 69 6c 65 29 20 7b 0a 20 20 69 6e 74 20 66 64  File) {.  int fd
e3d00 3b 0a 20 20 69 6e 74 20 64 69 72 66 64 20 3d 20  ;.  int dirfd = 
e3d10 2d 31 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  -1;.  unixFile *
e3d20 70 4e 65 77 3b 0a 20 20 69 6e 74 20 72 63 20 3d  pNew;.  int rc =
e3d30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71   SQLITE_OK;.  sq
e3d40 6c 69 74 65 33 5f 76 66 73 20 64 75 6d 6d 79 56  lite3_vfs dummyV
e3d50 66 73 3b 0a 0a 20 20 66 64 20 3d 20 6f 70 65 6e  fs;..  fd = open
e3d60 28 70 61 74 68 2c 20 4f 5f 52 44 57 52 20 7c 20  (path, O_RDWR | 
e3d70 4f 5f 43 52 45 41 54 2c 20 53 51 4c 49 54 45 5f  O_CREAT, SQLITE_
e3d80 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52  DEFAULT_FILE_PER
e3d90 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 69 66 28  MISSIONS);.  if(
e3da0 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 72 65 74   fd<0 ){.    ret
e3db0 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  urn SQLITE_CANTO
e3dc0 50 45 4e 3b 0a 20 20 7d 0a 20 20 0a 20 20 70 4e  PEN;.  }.  .  pN
e3dd0 65 77 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a  ew = (unixFile *
e3de0 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
e3df0 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29  sizeof(unixFile)
e3e00 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 4e  );.  if( pNew==N
e3e10 55 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ULL ){.    rc = 
e3e20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
e3e30 20 20 67 6f 74 6f 20 65 6e 64 5f 63 72 65 61 74    goto end_creat
e3e40 65 5f 70 72 6f 78 79 3b 0a 20 20 7d 0a 20 20 6d  e_proxy;.  }.  m
e3e50 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73  emset(pNew, 0, s
e3e60 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29  izeof(unixFile))
e3e70 3b 0a 0a 20 20 64 75 6d 6d 79 56 66 73 2e 70 41  ;..  dummyVfs.pA
e3e80 70 70 44 61 74 61 20 3d 20 28 76 6f 69 64 2a 29  ppData = (void*)
e3e90 26 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65  &autolockIoFinde
e3ea0 72 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e  r;.  rc = fillIn
e3eb0 55 6e 69 78 46 69 6c 65 28 26 64 75 6d 6d 79 56  UnixFile(&dummyV
e3ec0 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 28  fs, fd, dirfd, (
e3ed0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 4e  sqlite3_file*)pN
e3ee0 65 77 2c 20 70 61 74 68 2c 20 30 2c 20 30 29 3b  ew, path, 0, 0);
e3ef0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
e3f00 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 70 46  E_OK ){.    *ppF
e3f10 69 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  ile = pNew;.    
e3f20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e3f30 3b 0a 20 20 7d 0a 65 6e 64 5f 63 72 65 61 74 65  ;.  }.end_create
e3f40 5f 70 72 6f 78 79 3a 20 20 20 20 0a 20 20 63 6c  _proxy:    .  cl
e3f50 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65  ose(fd); /* sile
e3f60 6e 74 6c 79 20 6c 65 61 6b 20 66 64 20 69 66 20  ntly leak fd if 
e3f70 65 72 72 6f 72 2c 20 77 65 27 72 65 20 61 6c 72  error, we're alr
e3f80 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f  eady in error */
e3f90 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
e3fa0 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20  pNew);.  return 
e3fb0 72 63 3b 0a 7d 0a 0a 2f 2a 20 74 61 6b 65 73 20  rc;.}../* takes 
e3fc0 74 68 65 20 63 6f 6e 63 68 20 62 79 20 74 61 6b  the conch by tak
e3fd0 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63  ing a shared loc
e3fe0 6b 20 61 6e 64 20 72 65 61 64 20 74 68 65 20 63  k and read the c
e3ff0 6f 6e 74 65 6e 74 73 20 63 6f 6e 63 68 2c 20 69  ontents conch, i
e4000 66 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 69  f .** lockPath i
e4010 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 20  s non-NULL, the 
e4020 68 6f 73 74 20 49 44 20 61 6e 64 20 6c 6f 63 6b  host ID and lock
e4030 20 66 69 6c 65 20 70 61 74 68 20 6d 75 73 74 20   file path must 
e4040 6d 61 74 63 68 2e 20 20 41 20 4e 55 4c 4c 20 0a  match.  A NULL .
e4050 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 6d 65 61 6e  ** lockPath mean
e4060 73 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 50  s that the lockP
e4070 61 74 68 20 69 6e 20 74 68 65 20 63 6f 6e 63 68  ath in the conch
e4080 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 73   file will be us
e4090 65 64 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 6f  ed if the .** ho
e40a0 73 74 20 49 44 73 20 6d 61 74 63 68 2c 20 6f 72  st IDs match, or
e40b0 20 61 20 6e 65 77 20 6c 6f 63 6b 20 70 61 74 68   a new lock path
e40c0 20 77 69 6c 6c 20 62 65 20 67 65 6e 65 72 61 74   will be generat
e40d0 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
e40e0 20 0a 2a 2a 20 61 6e 64 20 77 72 69 74 74 65 6e   .** and written
e40f0 20 74 6f 20 74 68 65 20 63 6f 6e 63 68 20 66 69   to the conch fi
e4100 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
e4110 74 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68  t proxyTakeConch
e4120 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
e4130 29 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e  ){.  proxyLockin
e4140 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d  gContext *pCtx =
e4150 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f   (proxyLockingCo
e4160 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c  ntext *)pFile->l
e4170 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 0a  ockingContext; .
e4180 20 20 0a 20 20 69 66 28 20 70 43 74 78 2d 3e 63    .  if( pCtx->c
e4190 6f 6e 63 68 48 65 6c 64 3e 30 20 29 7b 0a 20 20  onchHeld>0 ){.  
e41a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e41b0 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
e41c0 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68   unixFile *conch
e41d0 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e  File = pCtx->con
e41e0 63 68 46 69 6c 65 3b 0a 20 20 20 20 63 68 61 72  chFile;.    char
e41f0 20 74 65 73 74 56 61 6c 75 65 5b 43 4f 4e 43 48   testValue[CONCH
e4200 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 63  LEN];.    char c
e4210 6f 6e 63 68 56 61 6c 75 65 5b 43 4f 4e 43 48 4c  onchValue[CONCHL
e4220 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 6c 6f  EN];.    char lo
e4230 63 6b 50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45  ckPath[MAXPATHLE
e4240 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a 74 4c  N];.    char *tL
e4250 6f 63 6b 50 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a  ockPath = NULL;.
e4260 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c      int rc = SQL
e4270 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20  ITE_OK;.    int 
e4280 72 65 61 64 52 63 20 3d 20 53 51 4c 49 54 45 5f  readRc = SQLITE_
e4290 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 73 79 6e 63  OK;.    int sync
e42a0 50 65 72 6d 73 20 3d 20 30 3b 0a 0a 20 20 20 20  Perms = 0;..    
e42b0 4f 53 54 52 41 43 45 34 28 22 54 41 4b 45 43 4f  OSTRACE4("TAKECO
e42c0 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70  NCH  %d for %s p
e42d0 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46  id=%d\n", conchF
e42e0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
e42f0 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b       (pCtx->lock
e4300 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78  ProxyPath ? pCtx
e4310 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
e4320 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74  : ":auto:"), get
e4330 70 69 64 28 29 29 3b 0a 0a 20 20 20 20 72 63 20  pid());..    rc 
e4340 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65  = conchFile->pMe
e4350 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c  thod->xLock((sql
e4360 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68  ite3_file*)conch
e4370 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43  File, SHARED_LOC
e4380 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  K);.    if( rc==
e4390 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
e43a0 20 20 20 69 6e 74 20 70 45 72 72 6f 72 20 3d 20     int pError = 
e43b0 30 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  0;.      memset(
e43c0 74 65 73 74 56 61 6c 75 65 2c 20 30 2c 20 43 4f  testValue, 0, CO
e43d0 4e 43 48 4c 45 4e 29 3b 20 2f 2a 20 63 6f 6e 63  NCHLEN); /* conc
e43e0 68 20 69 73 20 66 69 78 65 64 20 73 69 7a 65 20  h is fixed size 
e43f0 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 72  */.      rc = pr
e4400 6f 78 79 47 65 74 48 6f 73 74 49 44 28 74 65 73  oxyGetHostID(tes
e4410 74 56 61 6c 75 65 2c 20 26 70 45 72 72 6f 72 29  tValue, &pError)
e4420 3b 0a 20 20 20 20 20 20 69 66 28 20 28 72 63 26  ;.      if( (rc&
e4430 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f  0xff)==SQLITE_IO
e4440 45 52 52 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ERR ){.        p
e4450 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
e4460 3d 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 20 20  = pError;.      
e4470 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78  }.      if( pCtx
e4480 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
e4490 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 6c 63  ){.        strlc
e44a0 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b 48 4f  py(&testValue[HO
e44b0 53 54 49 44 4c 45 4e 5d 2c 20 70 43 74 78 2d 3e  STIDLEN], pCtx->
e44c0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 4d  lockProxyPath, M
e44d0 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20  AXPATHLEN);.    
e44e0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
e44f0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
e4500 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  ){.      goto en
e4510 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20  d_takeconch;.   
e4520 20 7d 0a 20 20 20 20 0a 20 20 20 20 72 65 61 64   }.    .    read
e4530 52 63 20 3d 20 75 6e 69 78 52 65 61 64 28 28 73  Rc = unixRead((s
e4540 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f  qlite3_file *)co
e4550 6e 63 68 46 69 6c 65 2c 20 63 6f 6e 63 68 56 61  nchFile, conchVa
e4560 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30  lue, CONCHLEN, 0
e4570 29 3b 0a 20 20 20 20 69 66 28 20 72 65 61 64 52  );.    if( readR
e4580 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c!=SQLITE_IOERR_
e4590 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20  SHORT_READ ){.  
e45a0 20 20 20 20 69 66 28 20 72 65 61 64 52 63 21 3d      if( readRc!=
e45b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
e45c0 20 20 20 20 20 69 66 28 20 28 72 63 26 30 78 66       if( (rc&0xf
e45d0 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  f)==SQLITE_IOERR
e45e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46   ){.          pF
e45f0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
e4600 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 6c 61 73 74   conchFile->last
e4610 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d  Errno;.        }
e4620 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65  .        rc = re
e4630 61 64 52 63 3b 0a 20 20 20 20 20 20 20 20 67 6f  adRc;.        go
e4640 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68  to end_takeconch
e4650 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
e4660 2f 2a 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20  /* if the conch 
e4670 68 61 73 20 64 61 74 61 20 63 6f 6d 70 61 72 65  has data compare
e4680 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f   the contents */
e4690 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 74 78  .      if( !pCtx
e46a0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
e46b0 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 6f  ){.        /* fo
e46c0 72 20 61 75 74 6f 2d 6e 61 6d 65 64 20 6c 6f 63  r auto-named loc
e46d0 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 2c 20 6a 75  al lock file, ju
e46e0 73 74 20 63 68 65 63 6b 20 74 68 65 20 68 6f 73  st check the hos
e46f0 74 20 49 44 20 61 6e 64 20 77 65 27 6c 6c 0a 20  t ID and we'll. 
e4700 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 74          ** use t
e4710 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69  he local lock fi
e4720 6c 65 20 70 61 74 68 20 74 68 61 74 27 73 20 61  le path that's a
e4730 6c 72 65 61 64 79 20 69 6e 20 74 68 65 72 65 20  lready in there 
e4740 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  */.        if( !
e4750 6d 65 6d 63 6d 70 28 74 65 73 74 56 61 6c 75 65  memcmp(testValue
e4760 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 48 4f  , conchValue, HO
e4770 53 54 49 44 4c 45 4e 29 20 29 7b 0a 20 20 20 20  STIDLEN) ){.    
e4780 20 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 68 20        tLockPath 
e4790 3d 20 28 63 68 61 72 20 2a 29 26 63 6f 6e 63 68  = (char *)&conch
e47a0 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d  Value[HOSTIDLEN]
e47b0 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
e47c0 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a   end_takeconch;.
e47d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
e47e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
e47f0 2a 20 77 65 27 76 65 20 67 6f 74 20 74 68 65 20  * we've got the 
e4800 63 6f 6e 63 68 20 69 66 20 63 6f 6e 63 68 56 61  conch if conchVa
e4810 6c 75 65 20 6d 61 74 63 68 65 73 20 6f 75 72 20  lue matches our 
e4820 70 61 74 68 20 61 6e 64 20 68 6f 73 74 20 49 44  path and host ID
e4830 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
e4840 21 6d 65 6d 63 6d 70 28 74 65 73 74 56 61 6c 75  !memcmp(testValu
e4850 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43  e, conchValue, C
e4860 4f 4e 43 48 4c 45 4e 29 20 29 7b 0a 20 20 20 20  ONCHLEN) ){.    
e4870 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74        goto end_t
e4880 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20  akeconch;.      
e4890 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
e48a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
e48b0 61 20 73 68 6f 72 74 20 72 65 61 64 20 6d 65 61  a short read mea
e48c0 6e 73 20 77 65 27 72 65 20 22 63 72 65 61 74 69  ns we're "creati
e48d0 6e 67 22 20 74 68 65 20 63 6f 6e 63 68 20 28 65  ng" the conch (e
e48e0 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20 63 6f  ven though it co
e48f0 75 6c 64 20 0a 20 20 20 20 20 20 2a 2a 20 68 61  uld .      ** ha
e4900 76 65 20 62 65 65 6e 20 75 73 65 72 2d 69 6e 74  ve been user-int
e4910 65 72 76 65 6e 74 69 6f 6e 29 2c 20 69 66 20 77  ervention), if w
e4920 65 20 61 63 71 75 69 72 65 20 74 68 65 20 65 78  e acquire the ex
e4930 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 0a 20 20  clusive lock,.  
e4940 20 20 20 20 2a 2a 20 77 65 27 6c 6c 20 74 72 79      ** we'll try
e4950 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 63 75   to match the cu
e4960 72 72 65 6e 74 20 6f 6e 2d 64 69 73 6b 20 70 65  rrent on-disk pe
e4970 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 74 68 65  rmissions of the
e4980 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20   database.      
e4990 2a 2f 0a 20 20 20 20 20 20 73 79 6e 63 50 65 72  */.      syncPer
e49a0 6d 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ms = 1;.    }.  
e49b0 20 20 0a 20 20 20 20 2f 2a 20 65 69 74 68 65 72    .    /* either
e49c0 20 63 6f 6e 63 68 20 77 61 73 20 65 6d 74 70 79   conch was emtpy
e49d0 20 6f 72 20 64 69 64 6e 27 74 20 6d 61 74 63 68   or didn't match
e49e0 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 43 74   */.    if( !pCt
e49f0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
e4a00 20 29 7b 0a 20 20 20 20 20 20 70 72 6f 78 79 47   ){.      proxyG
e4a10 65 74 4c 6f 63 6b 50 61 74 68 28 70 43 74 78 2d  etLockPath(pCtx-
e4a20 3e 64 62 50 61 74 68 2c 20 6c 6f 63 6b 50 61 74  >dbPath, lockPat
e4a30 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a  h, MAXPATHLEN);.
e4a40 20 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 68 20        tLockPath 
e4a50 3d 20 6c 6f 63 6b 50 61 74 68 3b 0a 20 20 20 20  = lockPath;.    
e4a60 20 20 73 74 72 6c 63 70 79 28 26 74 65 73 74 56    strlcpy(&testV
e4a70 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c  alue[HOSTIDLEN],
e4a80 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58 50 41   lockPath, MAXPA
e4a90 54 48 4c 45 4e 29 3b 0a 20 20 20 20 7d 0a 20 20  THLEN);.    }.  
e4aa0 20 20 0a 20 20 20 20 2f 2a 20 75 70 64 61 74 65    .    /* update
e4ab0 20 63 6f 6e 63 68 20 77 69 74 68 20 68 6f 73 74   conch with host
e4ac0 20 61 6e 64 20 70 61 74 68 20 28 74 68 69 73 20   and path (this 
e4ad0 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 6f 74 68  will fail if oth
e4ae0 65 72 20 70 72 6f 63 65 73 73 0a 20 20 20 20 20  er process.     
e4af0 2a 2a 20 68 61 73 20 61 20 73 68 61 72 65 64 20  ** has a shared 
e4b00 6c 6f 63 6b 20 61 6c 72 65 61 64 79 29 20 2a 2f  lock already) */
e4b10 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46  .    rc = conchF
e4b20 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c  ile->pMethod->xL
e4b30 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
e4b40 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 45 58  e*)conchFile, EX
e4b50 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20  CLUSIVE_LOCK);. 
e4b60 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
e4b70 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
e4b80 20 3d 20 75 6e 69 78 57 72 69 74 65 28 28 73 71   = unixWrite((sq
e4b90 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e  lite3_file *)con
e4ba0 63 68 46 69 6c 65 2c 20 74 65 73 74 56 61 6c 75  chFile, testValu
e4bb0 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b  e, CONCHLEN, 0);
e4bc0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
e4bd0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 79 6e 63  QLITE_OK && sync
e4be0 50 65 72 6d 73 20 29 7b 0a 20 20 20 20 20 20 20  Perms ){.       
e4bf0 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66   struct stat buf
e4c00 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 65 72  ;.        int er
e4c10 72 20 3d 20 66 73 74 61 74 28 70 46 69 6c 65 2d  r = fstat(pFile-
e4c20 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 20 20 20  >h, &buf);.     
e4c30 20 20 20 69 66 28 20 65 72 72 3d 3d 30 20 29 7b     if( err==0 ){
e4c40 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 72  .          /* tr
e4c50 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 64  y to match the d
e4c60 61 74 61 62 61 73 65 20 66 69 6c 65 20 70 65 72  atabase file per
e4c70 6d 69 73 73 69 6f 6e 73 2c 20 69 67 6e 6f 72 65  missions, ignore
e4c80 20 66 61 69 6c 75 72 65 20 2a 2f 0a 23 69 66 6e   failure */.#ifn
e4c90 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59  def SQLITE_PROXY
e4ca0 5f 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 20  _DEBUG.         
e4cb0 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69 6c   fchmod(conchFil
e4cc0 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f 64  e->h, buf.st_mod
e4cd0 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  e);.#else.      
e4ce0 20 20 20 20 69 66 28 20 66 63 68 6d 6f 64 28 63      if( fchmod(c
e4cf0 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66  onchFile->h, buf
e4d00 2e 73 74 5f 6d 6f 64 65 29 21 3d 30 20 29 7b 0a  .st_mode)!=0 ){.
e4d10 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
e4d20 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  code = errno;.  
e4d30 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74            fprint
e4d40 66 28 73 74 64 65 72 72 2c 20 22 66 63 68 6d 6f  f(stderr, "fchmo
e4d50 64 20 25 6f 20 46 41 49 4c 45 44 20 77 69 74 68  d %o FAILED with
e4d60 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20   %d %s\n",.     
e4d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4d80 20 20 20 20 20 20 20 20 62 75 66 2e 73 74 5f 6d          buf.st_m
e4d90 6f 64 65 2c 20 63 6f 64 65 2c 20 73 74 72 65 72  ode, code, strer
e4da0 72 6f 72 28 63 6f 64 65 29 29 3b 0a 20 20 20 20  ror(code));.    
e4db0 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20        } else {. 
e4dc0 20 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e             fprin
e4dd0 74 66 28 73 74 64 65 72 72 2c 20 22 66 63 68 6d  tf(stderr, "fchm
e4de0 6f 64 20 25 6f 20 53 55 43 43 45 44 45 44 5c 6e  od %o SUCCEDED\n
e4df0 22 2c 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a  ",buf.st_mode);.
e4e00 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
e4e10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
e4e20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20       int code = 
e4e30 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20  errno;.         
e4e40 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
e4e50 20 22 53 54 41 54 20 46 41 49 4c 45 44 5b 25 64   "STAT FAILED[%d
e4e60 5d 20 77 69 74 68 20 25 64 20 25 73 5c 6e 22 2c  ] with %d %s\n",
e4e70 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
e4e80 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 2c              err,
e4e90 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28   code, strerror(
e4ea0 63 6f 64 65 29 29 3b 0a 23 65 6e 64 69 66 0a 20  code));.#endif. 
e4eb0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
e4ec0 0a 20 20 20 20 7d 0a 20 20 20 20 63 6f 6e 63 68  .    }.    conch
e4ed0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  File->pMethod->x
e4ee0 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f  Unlock((sqlite3_
e4ef0 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c  file*)conchFile,
e4f00 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20   SHARED_LOCK);. 
e4f10 20 0a 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3a   .end_takeconch:
e4f20 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 54  .    OSTRACE2("T
e4f30 52 41 4e 53 50 52 4f 58 59 3a 20 43 4c 4f 53 45  RANSPROXY: CLOSE
e4f40 20 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e    %d\n", pFile->
e4f50 68 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  h);.    if( rc==
e4f60 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69  SQLITE_OK && pFi
e4f70 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 29 7b  le->openFlags ){
e4f80 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65  .      if( pFile
e4f90 2d 3e 68 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66  ->h>=0 ){.#ifdef
e4fa0 20 53 54 52 49 43 54 5f 43 4c 4f 53 45 5f 45 52   STRICT_CLOSE_ER
e4fb0 52 4f 52 0a 20 20 20 20 20 20 20 20 69 66 28 20  ROR.        if( 
e4fc0 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 20  close(pFile->h) 
e4fd0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ){.          pFi
e4fe0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
e4ff0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20  errno;.         
e5000 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
e5010 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20  OERR_CLOSE;.    
e5020 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
e5030 20 20 20 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d      close(pFile-
e5040 3e 68 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79  >h); /* silently
e5050 20 6c 65 61 6b 20 66 64 20 69 66 20 66 61 69 6c   leak fd if fail
e5060 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 20 20 20   */.#endif.     
e5070 20 7d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e   }.      pFile->
e5080 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 69 6e  h = -1;.      in
e5090 74 20 66 64 20 3d 20 6f 70 65 6e 28 70 43 74 78  t fd = open(pCtx
e50a0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2d  ->dbPath, pFile-
e50b0 3e 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20  >openFlags,.    
e50c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e50d0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
e50e0 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29  ILE_PERMISSIONS)
e50f0 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  ;.      OSTRACE2
e5100 28 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 4f 50  ("TRANSPROXY: OP
e5110 45 4e 20 20 25 64 5c 6e 22 2c 20 66 64 29 3b 0a  EN  %d\n", fd);.
e5120 20 20 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20        if( fd>=0 
e5130 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
e5140 2d 3e 68 20 3d 20 66 64 3b 0a 20 20 20 20 20 20  ->h = fd;.      
e5150 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
e5160 63 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  c=SQLITE_CANTOPE
e5170 4e 3b 20 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53  N; /* SQLITE_BUS
e5180 59 3f 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63  Y? proxyTakeConc
e5190 68 20 63 61 6c 6c 65 64 0a 20 20 20 20 20 20 20  h called.       
e51a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e51b0 20 20 20 20 20 20 20 20 64 75 72 69 6e 67 20 6c          during l
e51c0 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20  ocking */.      
e51d0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
e51e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
e51f0 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78   !pCtx->lockProx
e5200 79 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  y ){.      char 
e5210 2a 70 61 74 68 20 3d 20 74 4c 6f 63 6b 50 61 74  *path = tLockPat
e5220 68 20 3f 20 74 4c 6f 63 6b 50 61 74 68 20 3a 20  h ? tLockPath : 
e5230 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
e5240 61 74 68 3b 0a 20 20 20 20 20 20 2f 2a 20 41 43  ath;.      /* AC
e5250 53 3a 20 4e 65 65 64 20 74 6f 20 6d 61 6b 65 20  S: Need to make 
e5260 61 20 63 6f 70 79 20 6f 66 20 70 61 74 68 20 73  a copy of path s
e5270 6f 6d 65 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20  ometimes */.    
e5280 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61    rc = proxyCrea
e5290 74 65 55 6e 69 78 46 69 6c 65 28 70 61 74 68 2c  teUnixFile(path,
e52a0 20 26 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78   &pCtx->lockProx
e52b0 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  y);.    }.    if
e52c0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
e52d0 29 7b 0a 20 20 20 20 20 20 70 43 74 78 2d 3e 63  ){.      pCtx->c
e52e0 6f 6e 63 68 48 65 6c 64 20 3d 20 31 3b 0a 0a 20  onchHeld = 1;.. 
e52f0 20 20 20 20 20 69 66 28 20 74 4c 6f 63 6b 50 61       if( tLockPa
e5300 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  th ){.        pC
e5310 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
e5320 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  h = sqlite3DbStr
e5330 44 75 70 28 30 2c 20 74 4c 6f 63 6b 50 61 74 68  Dup(0, tLockPath
e5340 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
e5350 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e  Ctx->lockProxy->
e5360 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 49  pMethod == &afpI
e5370 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
e5380 20 20 20 20 20 20 28 28 61 66 70 4c 6f 63 6b 69        ((afpLocki
e5390 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 43 74 78  ngContext *)pCtx
e53a0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63  ->lockProxy->loc
e53b0 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62  kingContext)->db
e53c0 50 61 74 68 20 3d 0a 20 20 20 20 20 20 20 20 20  Path =.         
e53d0 20 20 20 20 20 20 20 20 20 20 20 20 70 43 74 78              pCtx
e53e0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b  ->lockProxyPath;
e53f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
e5400 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a   }.    } else {.
e5410 20 20 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d        conchFile-
e5420 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
e5430 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
e5440 29 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c  )conchFile, NO_L
e5450 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OCK);.    }.    
e5460 4f 53 54 52 41 43 45 33 28 22 54 41 4b 45 43 4f  OSTRACE3("TAKECO
e5470 4e 43 48 20 20 25 64 20 25 73 5c 6e 22 2c 20 63  NCH  %d %s\n", c
e5480 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 72 63 3d  onchFile->h, rc=
e5490 3d 53 51 4c 49 54 45 5f 4f 4b 3f 22 6f 6b 22 3a  =SQLITE_OK?"ok":
e54a0 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 72  "failed");.    r
e54b0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a  eturn rc;.  }.}.
e54c0 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20  ./*.** If pFile 
e54d0 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20  holds a lock on 
e54e0 61 20 63 6f 6e 63 68 20 66 69 6c 65 2c 20 74 68  a conch file, th
e54f0 65 6e 20 72 65 6c 65 61 73 65 20 74 68 61 74 20  en release that 
e5500 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lock..*/.static 
e5510 69 6e 74 20 70 72 6f 78 79 52 65 6c 65 61 73 65  int proxyRelease
e5520 43 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65 20 2a  Conch(unixFile *
e5530 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63  pFile){.  int rc
e5540 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
e5550 20 20 20 20 20 20 2f 2a 20 53 75 62 72 6f 75 74        /* Subrout
e5560 69 6e 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  ine return code 
e5570 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e  */.  proxyLockin
e5580 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 20  gContext *pCtx; 
e5590 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20   /* The locking 
e55a0 63 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20  context for the 
e55b0 70 72 6f 78 79 20 6c 6f 63 6b 20 2a 2f 0a 20 20  proxy lock */.  
e55c0 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46  unixFile *conchF
e55d0 69 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  ile;        /* N
e55e0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 68  ame of the conch
e55f0 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 70 43 74 78   file */..  pCtx
e5600 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67   = (proxyLocking
e5610 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d  Context *)pFile-
e5620 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
e5630 0a 20 20 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70  .  conchFile = p
e5640 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a  Ctx->conchFile;.
e5650 20 20 4f 53 54 52 41 43 45 34 28 22 52 45 4c 45    OSTRACE4("RELE
e5660 41 53 45 43 4f 4e 43 48 20 20 25 64 20 66 6f 72  ASECONCH  %d for
e5670 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63   %s pid=%d\n", c
e5680 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20  onchFile->h,.   
e5690 20 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e 6c          (pCtx->l
e56a0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20 70  ockProxyPath ? p
e56b0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
e56c0 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20  th : ":auto:"), 
e56d0 0a 20 20 20 20 20 20 20 20 20 20 20 67 65 74 70  .           getp
e56e0 69 64 28 29 29 3b 0a 20 20 70 43 74 78 2d 3e 63  id());.  pCtx->c
e56f0 6f 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20 20  onchHeld = 0;.  
e5700 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  rc = conchFile->
e5710 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b  pMethod->xUnlock
e5720 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
e5730 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f  conchFile, NO_LO
e5740 43 4b 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  CK);.  OSTRACE3(
e5750 22 52 45 4c 45 41 53 45 43 4f 4e 43 48 20 20 25  "RELEASECONCH  %
e5760 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69  d %s\n", conchFi
e5770 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
e5780 20 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b    (rc==SQLITE_OK
e5790 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65   ? "ok" : "faile
e57a0 64 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  d"));.  return r
e57b0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  c;.}../*.** Give
e57c0 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  n the name of a 
e57d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 63  database file, c
e57e0 6f 6d 70 75 74 65 20 74 68 65 20 6e 61 6d 65 20  ompute the name 
e57f0 6f 66 20 69 74 73 20 63 6f 6e 63 68 20 66 69 6c  of its conch fil
e5800 65 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  e..** Store the 
e5810 63 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65 20 69  conch filename i
e5820 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
e5830 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
e5840 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 4d 61 6b 65  alloc()..** Make
e5850 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 70 6f 69   *pConchPath poi
e5860 6e 74 20 74 6f 20 74 68 65 20 6e 65 77 20 6e 61  nt to the new na
e5870 6d 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  me.  Return SQLI
e5880 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
e5890 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f  .** or SQLITE_NO
e58a0 4d 45 4d 20 69 66 20 75 6e 61 62 6c 65 20 74 6f  MEM if unable to
e58b0 20 6f 62 74 61 69 6e 20 6d 65 6d 6f 72 79 2e 0a   obtain memory..
e58c0 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72  **.** The caller
e58d0 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
e58e0 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61  for ensuring tha
e58f0 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  t the allocated 
e5900 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20  memory.** space 
e5910 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72  is eventually fr
e5920 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e  eed..**.** *pCon
e5930 63 68 50 61 74 68 20 69 73 20 73 65 74 20 74 6f  chPath is set to
e5940 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72   NULL if a memor
e5950 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
e5960 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74  or occurs..*/.st
e5970 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 72  atic int proxyCr
e5980 65 61 74 65 43 6f 6e 63 68 50 61 74 68 6e 61 6d  eateConchPathnam
e5990 65 28 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20  e(char *dbPath, 
e59a0 63 68 61 72 20 2a 2a 70 43 6f 6e 63 68 50 61 74  char **pConchPat
e59b0 68 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  h){.  int i;    
e59c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e59d0 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
e59e0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e  ter */.  int len
e59f0 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64   = (int)strlen(d
e5a00 62 50 61 74 68 29 3b 20 2f 2a 20 4c 65 6e 67 74  bPath); /* Lengt
e5a10 68 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69  h of database fi
e5a20 6c 65 6e 61 6d 65 20 2d 20 64 62 50 61 74 68 20  lename - dbPath 
e5a30 2a 2f 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63 68  */.  char *conch
e5a40 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  Path;           
e5a50 20 20 20 2f 2a 20 62 75 66 66 65 72 20 69 6e 20     /* buffer in 
e5a60 77 68 69 63 68 20 74 6f 20 63 6f 6e 73 74 72 75  which to constru
e5a70 63 74 20 63 6f 6e 63 68 20 6e 61 6d 65 20 2a 2f  ct conch name */
e5a80 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
e5a90 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 63 6f  space for the co
e5aa0 6e 63 68 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64  nch filename and
e5ab0 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
e5ac0 6e 61 6d 65 20 74 6f 0a 20 20 2a 2a 20 74 68 65  name to.  ** the
e5ad0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 72 69   name of the ori
e5ae0 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66  ginal database f
e5af0 69 6c 65 2e 20 2a 2f 20 20 0a 20 20 2a 70 43 6f  ile. */  .  *pCo
e5b00 6e 63 68 50 61 74 68 20 3d 20 63 6f 6e 63 68 50  nchPath = conchP
e5b10 61 74 68 20 3d 20 28 63 68 61 72 20 2a 29 73 71  ath = (char *)sq
e5b20 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e  lite3_malloc(len
e5b30 20 2b 20 38 29 3b 0a 20 20 69 66 28 20 63 6f 6e   + 8);.  if( con
e5b40 63 68 50 61 74 68 3d 3d 30 20 29 7b 0a 20 20 20  chPath==0 ){.   
e5b50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
e5b60 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63  OMEM;.  }.  memc
e5b70 70 79 28 63 6f 6e 63 68 50 61 74 68 2c 20 64 62  py(conchPath, db
e5b80 50 61 74 68 2c 20 6c 65 6e 2b 31 29 3b 0a 20 20  Path, len+1);.  
e5b90 0a 20 20 2f 2a 20 6e 6f 77 20 69 6e 73 65 72 74  .  /* now insert
e5ba0 20 61 20 22 2e 22 20 62 65 66 6f 72 65 20 74 68   a "." before th
e5bb0 65 20 6c 61 73 74 20 2f 20 63 68 61 72 61 63 74  e last / charact
e5bc0 65 72 20 2a 2f 0a 20 20 66 6f 72 28 20 69 3d 28  er */.  for( i=(
e5bd0 6c 65 6e 2d 31 29 3b 20 69 3e 3d 30 3b 20 69 2d  len-1); i>=0; i-
e5be0 2d 20 29 7b 0a 20 20 20 20 69 66 28 20 63 6f 6e  - ){.    if( con
e5bf0 63 68 50 61 74 68 5b 69 5d 3d 3d 27 2f 27 20 29  chPath[i]=='/' )
e5c00 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20  {.      i++;.   
e5c10 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
e5c20 20 20 7d 0a 20 20 63 6f 6e 63 68 50 61 74 68 5b    }.  conchPath[
e5c30 69 5d 3d 27 2e 27 3b 0a 20 20 77 68 69 6c 65 20  i]='.';.  while 
e5c40 28 20 69 3c 6c 65 6e 20 29 7b 0a 20 20 20 20 63  ( i<len ){.    c
e5c50 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d 3d 64 62  onchPath[i+1]=db
e5c60 50 61 74 68 5b 69 5d 3b 0a 20 20 20 20 69 2b 2b  Path[i];.    i++
e5c70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 70 70 65  ;.  }..  /* appe
e5c80 6e 64 20 74 68 65 20 22 2d 63 6f 6e 63 68 22 20  nd the "-conch" 
e5c90 73 75 66 66 69 78 20 74 6f 20 74 68 65 20 66 69  suffix to the fi
e5ca0 6c 65 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 26  le */.  memcpy(&
e5cb0 63 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d 2c 20  conchPath[i+1], 
e5cc0 22 2d 63 6f 6e 63 68 22 2c 20 37 29 3b 0a 20 20  "-conch", 7);.  
e5cd0 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 74 72  assert( (int)str
e5ce0 6c 65 6e 28 63 6f 6e 63 68 50 61 74 68 29 20 3d  len(conchPath) =
e5cf0 3d 20 6c 65 6e 2b 37 20 29 3b 0a 0a 20 20 72 65  = len+7 );..  re
e5d00 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e5d10 7d 0a 0a 0a 2f 2a 20 54 61 6b 65 73 20 61 20 66  }.../* Takes a f
e5d20 75 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20  ully configured 
e5d30 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2d 73 74  proxy locking-st
e5d40 79 6c 65 20 75 6e 69 78 20 66 69 6c 65 20 61 6e  yle unix file an
e5d50 64 20 73 77 69 74 63 68 65 73 0a 2a 2a 20 74 68  d switches.** th
e5d60 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c  e local lock fil
e5d70 65 20 70 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69  e path .*/.stati
e5d80 63 20 69 6e 74 20 73 77 69 74 63 68 4c 6f 63 6b  c int switchLock
e5d90 50 72 6f 78 79 50 61 74 68 28 75 6e 69 78 46 69  ProxyPath(unixFi
e5da0 6c 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74  le *pFile, const
e5db0 20 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 20   char *path) {. 
e5dc0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
e5dd0 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72  text *pCtx = (pr
e5de0 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
e5df0 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  t*)pFile->lockin
e5e00 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 68 61 72  gContext;.  char
e5e10 20 2a 6f 6c 64 50 61 74 68 20 3d 20 70 43 74 78   *oldPath = pCtx
e5e20 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b  ->lockProxyPath;
e5e30 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
e5e40 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 46  TE_OK;..  if( pF
e5e50 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e  ile->locktype!=N
e5e60 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65  O_LOCK ){.    re
e5e70 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
e5e80 3b 0a 20 20 7d 20 20 0a 0a 20 20 2f 2a 20 6e 6f  ;.  }  ..  /* no
e5e90 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 66 20 74  thing to do if t
e5ea0 68 65 20 70 61 74 68 20 69 73 20 4e 55 4c 4c 2c  he path is NULL,
e5eb0 20 3a 61 75 74 6f 3a 20 6f 72 20 6d 61 74 63 68   :auto: or match
e5ec0 65 73 20 74 68 65 20 65 78 69 73 74 69 6e 67 20  es the existing 
e5ed0 70 61 74 68 20 2a 2f 0a 20 20 69 66 28 20 21 70  path */.  if( !p
e5ee0 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d  ath || path[0]==
e5ef0 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 28  '\0' || !strcmp(
e5f00 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20  path, ":auto:") 
e5f10 7c 7c 0a 20 20 20 20 28 6f 6c 64 50 61 74 68 20  ||.    (oldPath 
e5f20 26 26 20 21 73 74 72 6e 63 6d 70 28 6f 6c 64 50  && !strncmp(oldP
e5f30 61 74 68 2c 20 70 61 74 68 2c 20 4d 41 58 50 41  ath, path, MAXPA
e5f40 54 48 4c 45 4e 29 29 20 29 7b 0a 20 20 20 20 72  THLEN)) ){.    r
e5f50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e5f60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e  .  }else{.    un
e5f70 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78  ixFile *lockProx
e5f80 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  y = pCtx->lockPr
e5f90 6f 78 79 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c  oxy;.    pCtx->l
e5fa0 6f 63 6b 50 72 6f 78 79 3d 4e 55 4c 4c 3b 0a 20  ockProxy=NULL;. 
e5fb0 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65     pCtx->conchHe
e5fc0 6c 64 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  ld = 0;.    if( 
e5fd0 6c 6f 63 6b 50 72 6f 78 79 21 3d 4e 55 4c 4c 20  lockProxy!=NULL 
e5fe0 29 7b 0a 20 20 20 20 20 20 72 63 3d 6c 6f 63 6b  ){.      rc=lock
e5ff0 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e  Proxy->pMethod->
e6000 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f  xClose((sqlite3_
e6010 66 69 6c 65 20 2a 29 6c 6f 63 6b 50 72 6f 78 79  file *)lockProxy
e6020 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
e6030 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
e6040 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
e6050 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20  lockProxy);.    
e6060 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
e6070 65 65 28 6f 6c 64 50 61 74 68 29 3b 0a 20 20 20  ee(oldPath);.   
e6080 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
e6090 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62  Path = sqlite3Db
e60a0 53 74 72 44 75 70 28 30 2c 20 70 61 74 68 29 3b  StrDup(0, path);
e60b0 0a 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e  .  }.  .  return
e60c0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46   rc;.}../*.** pF
e60d0 69 6c 65 20 69 73 20 61 20 66 69 6c 65 20 74 68  ile is a file th
e60e0 61 74 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e  at has been open
e60f0 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 78 4f  ed by a prior xO
e6100 70 65 6e 20 63 61 6c 6c 2e 20 20 64 62 50 61 74  pen call.  dbPat
e6110 68 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67  h.** is a string
e6120 20 62 75 66 66 65 72 20 61 74 20 6c 65 61 73 74   buffer at least
e6130 20 4d 41 58 50 41 54 48 4c 45 4e 2b 31 20 63 68   MAXPATHLEN+1 ch
e6140 61 72 61 63 74 65 72 73 20 69 6e 20 73 69 7a 65  aracters in size
e6150 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
e6160 74 69 6e 65 20 66 69 6e 64 20 74 68 65 20 66 69  tine find the fi
e6170 6c 65 6e 61 6d 65 20 61 73 73 6f 63 69 61 74 65  lename associate
e6180 64 20 77 69 74 68 20 70 46 69 6c 65 20 61 6e 64  d with pFile and
e6190 20 77 72 69 74 65 73 20 69 74 0a 2a 2a 20 69 6e   writes it.** in
e61a0 74 20 64 62 50 61 74 68 2e 0a 2a 2f 0a 73 74 61  t dbPath..*/.sta
e61b0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74  tic int proxyGet
e61c0 44 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c  DbPathForUnixFil
e61d0 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c  e(unixFile *pFil
e61e0 65 2c 20 63 68 61 72 20 2a 64 62 50 61 74 68 29  e, char *dbPath)
e61f0 7b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  {.#if defined(__
e6200 41 50 50 4c 45 5f 5f 29 0a 20 20 69 66 28 20 70  APPLE__).  if( p
e6210 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d  File->pMethod ==
e6220 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 29   &afpIoMethods )
e6230 7b 0a 20 20 20 20 2f 2a 20 61 66 70 20 73 74 79  {.    /* afp sty
e6240 6c 65 20 6b 65 65 70 73 20 61 20 72 65 66 65 72  le keeps a refer
e6250 65 6e 63 65 20 74 6f 20 74 68 65 20 64 62 20 70  ence to the db p
e6260 61 74 68 20 69 6e 20 74 68 65 20 66 69 6c 65 50  ath in the fileP
e6270 61 74 68 20 66 69 65 6c 64 20 0a 20 20 20 20 2a  ath field .    *
e6280 2a 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 20  * of the struct 
e6290 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  */.    assert( (
e62a0 69 6e 74 29 73 74 72 6c 65 6e 28 28 63 68 61 72  int)strlen((char
e62b0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
e62c0 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41 54  Context)<=MAXPAT
e62d0 48 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74 72 63  HLEN );.    strc
e62e0 70 79 28 64 62 50 61 74 68 2c 20 28 28 61 66 70  py(dbPath, ((afp
e62f0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
e6300 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
e6310 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 29  ontext)->dbPath)
e6320 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  ;.  }else.#endif
e6330 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d  .  if( pFile->pM
e6340 65 74 68 6f 64 20 3d 3d 20 26 64 6f 74 6c 6f 63  ethod == &dotloc
e6350 6b 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  kIoMethods ){.  
e6360 20 20 2f 2a 20 64 6f 74 20 6c 6f 63 6b 20 73 74    /* dot lock st
e6370 79 6c 65 20 75 73 65 73 20 74 68 65 20 6c 6f 63  yle uses the loc
e6380 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20  king context to 
e6390 73 74 6f 72 65 20 74 68 65 20 64 6f 74 20 6c 6f  store the dot lo
e63a0 63 6b 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 70  ck.    ** file p
e63b0 61 74 68 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c  ath */.    int l
e63c0 65 6e 20 3d 20 73 74 72 6c 65 6e 28 28 63 68 61  en = strlen((cha
e63d0 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  r *)pFile->locki
e63e0 6e 67 43 6f 6e 74 65 78 74 29 20 2d 20 73 74 72  ngContext) - str
e63f0 6c 65 6e 28 44 4f 54 4c 4f 43 4b 5f 53 55 46 46  len(DOTLOCK_SUFF
e6400 49 58 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  IX);.    memcpy(
e6410 64 62 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29  dbPath, (char *)
e6420 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
e6430 6e 74 65 78 74 2c 20 6c 65 6e 20 2b 20 31 29 3b  ntext, len + 1);
e6440 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
e6450 20 61 6c 6c 20 6f 74 68 65 72 20 73 74 79 6c 65   all other style
e6460 73 20 75 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e  s use the lockin
e6470 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f  g context to sto
e6480 72 65 20 74 68 65 20 64 62 20 66 69 6c 65 20 70  re the db file p
e6490 61 74 68 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ath */.    asser
e64a0 74 28 20 73 74 72 6c 65 6e 28 28 63 68 61 72 2a  t( strlen((char*
e64b0 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
e64c0 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 48  ontext)<=MAXPATH
e64d0 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74 72 63 70  LEN );.    strcp
e64e0 79 28 64 62 50 61 74 68 2c 20 28 63 68 61 72 20  y(dbPath, (char 
e64f0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
e6500 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20  Context);.  }.  
e6510 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e6520 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b 65 73  ;.}../*.** Takes
e6530 20 61 6e 20 61 6c 72 65 61 64 79 20 66 69 6c 6c   an already fill
e6540 65 64 20 69 6e 20 75 6e 69 78 20 66 69 6c 65 20  ed in unix file 
e6550 61 6e 64 20 61 6c 74 65 72 73 20 69 74 20 73 6f  and alters it so
e6560 20 61 6c 6c 20 66 69 6c 65 20 6c 6f 63 6b 69 6e   all file lockin
e6570 67 20 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 70 65  g .** will be pe
e6580 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 6c  rformed on the l
e6590 6f 63 61 6c 20 70 72 6f 78 79 20 6c 6f 63 6b 20  ocal proxy lock 
e65a0 66 69 6c 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  file.  The follo
e65b0 77 69 6e 67 20 66 69 65 6c 64 73 0a 2a 2a 20 61  wing fields.** a
e65c0 72 65 20 70 72 65 73 65 72 76 65 64 20 69 6e 20  re preserved in 
e65d0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74  the locking cont
e65e0 65 78 74 20 73 6f 20 74 68 61 74 20 74 68 65 79  ext so that they
e65f0 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64   can be restored
e6600 20 61 6e 64 20 0a 2a 2a 20 74 68 65 20 75 6e 69   and .** the uni
e6610 78 20 73 74 72 75 63 74 75 72 65 20 70 72 6f 70  x structure prop
e6620 65 72 6c 79 20 63 6c 65 61 6e 65 64 20 75 70 20  erly cleaned up 
e6630 61 74 20 63 6c 6f 73 65 20 74 69 6d 65 3a 0a 2a  at close time:.*
e6640 2a 20 20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  *  ->lockingCont
e6650 65 78 74 0a 2a 2a 20 20 2d 3e 70 4d 65 74 68 6f  ext.**  ->pMetho
e6660 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  d.*/.static int 
e6670 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e  proxyTransformUn
e6680 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 20  ixFile(unixFile 
e6690 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68  *pFile, const ch
e66a0 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72  ar *path) {.  pr
e66b0 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
e66c0 74 20 2a 70 43 74 78 3b 0a 20 20 63 68 61 72 20  t *pCtx;.  char 
e66d0 64 62 50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45  dbPath[MAXPATHLE
e66e0 4e 2b 31 5d 3b 20 20 20 20 20 20 20 2f 2a 20 4e  N+1];       /* N
e66f0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
e6700 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68  ase file */.  ch
e6710 61 72 20 2a 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c  ar *lockPath=NUL
e6720 4c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  L;.  int rc = SQ
e6730 4c 49 54 45 5f 4f 4b 3b 0a 20 20 0a 20 20 69 66  LITE_OK;.  .  if
e6740 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
e6750 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20  e!=NO_LOCK ){.  
e6760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e6770 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 72 6f 78  BUSY;.  }.  prox
e6780 79 47 65 74 44 62 50 61 74 68 46 6f 72 55 6e 69  yGetDbPathForUni
e6790 78 46 69 6c 65 28 70 46 69 6c 65 2c 20 64 62 50  xFile(pFile, dbP
e67a0 61 74 68 29 3b 0a 20 20 69 66 28 20 21 70 61 74  ath);.  if( !pat
e67b0 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c  h || path[0]=='\
e67c0 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 61  0' || !strcmp(pa
e67d0 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 29 7b  th, ":auto:") ){
e67e0 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d 4e 55  .    lockPath=NU
e67f0 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  LL;.  }else{.   
e6800 20 6c 6f 63 6b 50 61 74 68 3d 28 63 68 61 72 20   lockPath=(char 
e6810 2a 29 70 61 74 68 3b 0a 20 20 7d 0a 20 20 0a 20  *)path;.  }.  . 
e6820 20 4f 53 54 52 41 43 45 34 28 22 54 52 41 4e 53   OSTRACE4("TRANS
e6830 50 52 4f 58 59 20 20 25 64 20 66 6f 72 20 25 73  PROXY  %d for %s
e6840 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   pid=%d\n", pFil
e6850 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20  e->h,.          
e6860 20 28 6c 6f 63 6b 50 61 74 68 20 3f 20 6c 6f 63   (lockPath ? loc
e6870 6b 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22  kPath : ":auto:"
e6880 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20  ), getpid());.. 
e6890 20 70 43 74 78 20 3d 20 73 71 6c 69 74 65 33 5f   pCtx = sqlite3_
e68a0 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a  malloc( sizeof(*
e68b0 70 43 74 78 29 20 29 3b 0a 20 20 69 66 28 20 70  pCtx) );.  if( p
e68c0 43 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  Ctx==0 ){.    re
e68d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
e68e0 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  M;.  }.  memset(
e68f0 70 43 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pCtx, 0, sizeof(
e6900 2a 70 43 74 78 29 29 3b 0a 0a 20 20 72 63 20 3d  *pCtx));..  rc =
e6910 20 70 72 6f 78 79 43 72 65 61 74 65 43 6f 6e 63   proxyCreateConc
e6920 68 50 61 74 68 6e 61 6d 65 28 64 62 50 61 74 68  hPathname(dbPath
e6930 2c 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69  , &pCtx->conchFi
e6940 6c 65 50 61 74 68 29 3b 0a 20 20 69 66 28 20 72  lePath);.  if( r
e6950 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
e6960 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72      rc = proxyCr
e6970 65 61 74 65 55 6e 69 78 46 69 6c 65 28 70 43 74  eateUnixFile(pCt
e6980 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68  x->conchFilePath
e6990 2c 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69  , &pCtx->conchFi
e69a0 6c 65 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28  le);.  }  .  if(
e69b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
e69c0 26 20 6c 6f 63 6b 50 61 74 68 20 29 7b 0a 20 20  & lockPath ){.  
e69d0 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78    pCtx->lockProx
e69e0 79 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44  yPath = sqlite3D
e69f0 62 53 74 72 44 75 70 28 30 2c 20 6c 6f 63 6b 50  bStrDup(0, lockP
e6a00 61 74 68 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ath);.  }..  if(
e6a10 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
e6a20 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6d 65 6d  {.    /* all mem
e6a30 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ory is allocated
e6a40 2c 20 70 72 6f 78 79 73 20 61 72 65 20 63 72 65  , proxys are cre
e6a50 61 74 65 64 20 61 6e 64 20 61 73 73 69 67 6e 65  ated and assigne
e6a60 64 2c 20 0a 20 20 20 20 2a 2a 20 73 77 69 74 63  d, .    ** switc
e6a70 68 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f  h the locking co
e6a80 6e 74 65 78 74 20 61 6e 64 20 70 4d 65 74 68 6f  ntext and pMetho
e6a90 64 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 20  d then return.. 
e6aa0 20 20 20 2a 2f 0a 20 20 20 20 70 43 74 78 2d 3e     */.    pCtx->
e6ab0 64 62 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33  dbPath = sqlite3
e6ac0 44 62 53 74 72 44 75 70 28 30 2c 20 64 62 50 61  DbStrDup(0, dbPa
e6ad0 74 68 29 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6f  th);.    pCtx->o
e6ae0 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ldLockingContext
e6af0 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   = pFile->lockin
e6b00 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 46  gContext;.    pF
e6b10 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
e6b20 65 78 74 20 3d 20 70 43 74 78 3b 0a 20 20 20 20  ext = pCtx;.    
e6b30 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64  pCtx->pOldMethod
e6b40 20 3d 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f   = pFile->pMetho
e6b50 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4d  d;.    pFile->pM
e6b60 65 74 68 6f 64 20 3d 20 26 70 72 6f 78 79 49 6f  ethod = &proxyIo
e6b70 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65  Methods;.  }else
e6b80 7b 0a 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e  {.    if( pCtx->
e6b90 63 6f 6e 63 68 46 69 6c 65 20 29 7b 20 0a 20 20  conchFile ){ .  
e6ba0 20 20 20 20 72 63 20 3d 20 70 43 74 78 2d 3e 63      rc = pCtx->c
e6bb0 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  onchFile->pMetho
e6bc0 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74  d->xClose((sqlit
e6bd0 65 33 5f 66 69 6c 65 20 2a 29 70 43 74 78 2d 3e  e3_file *)pCtx->
e6be0 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20  conchFile);.    
e6bf0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
e6c00 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69  n rc;.      sqli
e6c10 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63  te3_free(pCtx->c
e6c20 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d  onchFile);.    }
e6c30 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
e6c40 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  e(pCtx->conchFil
e6c50 65 50 61 74 68 29 3b 20 0a 20 20 20 20 73 71 6c  ePath); .    sql
e6c60 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 29 3b  ite3_free(pCtx);
e6c70 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 33 28  .  }.  OSTRACE3(
e6c80 22 54 52 41 4e 53 50 52 4f 58 59 20 20 25 64 20  "TRANSPROXY  %d 
e6c90 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %s\n", pFile->h,
e6ca0 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63 3d  .           (rc=
e6cb0 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b  =SQLITE_OK ? "ok
e6cc0 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a  " : "failed"));.
e6cd0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
e6ce0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
e6cf0 69 6e 65 20 68 61 6e 64 6c 65 73 20 73 71 6c 69  ine handles sqli
e6d00 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
e6d10 28 29 20 63 61 6c 6c 73 20 74 68 61 74 20 61 72  () calls that ar
e6d20 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f  e specific.** to
e6d30 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a   proxy locking..
e6d40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
e6d50 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73  oxyFileControl(s
e6d60 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
e6d70 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70   int op, void *p
e6d80 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20  Arg){.  switch( 
e6d90 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  op ){.    case S
e6da0 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52  QLITE_GET_LOCKPR
e6db0 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20  OXYFILE: {.     
e6dc0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
e6dd0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
e6de0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c  ;.      if( pFil
e6df0 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70  e->pMethod == &p
e6e00 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 20 29 7b  roxyIoMethods ){
e6e10 0a 20 20 20 20 20 20 20 20 70 72 6f 78 79 4c 6f  .        proxyLo
e6e20 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
e6e30 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
e6e40 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65  ngContext*)pFile
e6e50 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
e6e60 3b 0a 20 20 20 20 20 20 20 20 70 72 6f 78 79 54  ;.        proxyT
e6e70 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b  akeConch(pFile);
e6e80 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 74  .        if( pCt
e6e90 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
e6ea0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28   ){.          *(
e6eb0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41  const char **)pA
e6ec0 72 67 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  rg = pCtx->lockP
e6ed0 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20 20  roxyPath;.      
e6ee0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
e6ef0 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72 20     *(const char 
e6f00 2a 2a 29 70 41 72 67 20 3d 20 22 3a 61 75 74 6f  **)pArg = ":auto
e6f10 3a 20 28 6e 6f 74 20 68 65 6c 64 29 22 3b 0a 20  : (not held)";. 
e6f20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
e6f30 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20   else {.        
e6f40 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29  *(const char **)
e6f50 70 41 72 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  pArg = NULL;.   
e6f60 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
e6f70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
e6f80 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
e6f90 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59  TE_SET_LOCKPROXY
e6fa0 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e  FILE: {.      un
e6fb0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
e6fc0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
e6fd0 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51       int rc = SQ
e6fe0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 69  LITE_OK;.      i
e6ff0 6e 74 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20  nt isProxyStyle 
e7000 3d 20 28 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  = (pFile->pMetho
e7010 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74  d == &proxyIoMet
e7020 68 6f 64 73 29 3b 0a 20 20 20 20 20 20 69 66 28  hods);.      if(
e7030 20 70 41 72 67 3d 3d 4e 55 4c 4c 20 7c 7c 20 28   pArg==NULL || (
e7040 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 41 72  const char *)pAr
e7050 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  g==0 ){.        
e7060 69 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65  if( isProxyStyle
e7070 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
e7080 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 20   turn off proxy 
e7090 6c 6f 63 6b 69 6e 67 20 2d 20 6e 6f 74 20 73 75  locking - not su
e70a0 70 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20 20 20  pported */.     
e70b0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
e70c0 5f 45 52 52 4f 52 20 2f 2a 53 51 4c 49 54 45 5f  _ERROR /*SQLITE_
e70d0 50 52 4f 54 4f 43 4f 4c 3f 20 53 51 4c 49 54 45  PROTOCOL? SQLITE
e70e0 5f 4d 49 53 55 53 45 3f 2a 2f 3b 0a 20 20 20 20  _MISUSE?*/;.    
e70f0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
e7100 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66       /* turn off
e7110 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d   proxy locking -
e7120 20 61 6c 72 65 61 64 79 20 6f 66 66 20 2d 20 4e   already off - N
e7130 4f 4f 50 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  OOP */.         
e7140 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
e7150 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
e7160 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
e7170 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 6f 78  const char *prox
e7180 79 50 61 74 68 20 3d 20 28 63 6f 6e 73 74 20 63  yPath = (const c
e7190 68 61 72 20 2a 29 70 41 72 67 3b 0a 20 20 20 20  har *)pArg;.    
e71a0 20 20 20 20 69 66 28 20 69 73 50 72 6f 78 79 53      if( isProxyS
e71b0 74 79 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  tyle ){.        
e71c0 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f    proxyLockingCo
e71d0 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 0a 20  ntext *pCtx = . 
e71e0 20 20 20 20 20 20 20 20 20 20 20 28 70 72 6f 78             (prox
e71f0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a  yLockingContext*
e7200 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
e7210 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20  ontext;.        
e7220 20 20 69 66 28 20 21 73 74 72 63 6d 70 28 70 41    if( !strcmp(pA
e7230 72 67 2c 20 22 3a 61 75 74 6f 3a 22 29 20 0a 20  rg, ":auto:") . 
e7240 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 43            || (pC
e7250 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
e7260 68 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  h &&.           
e7270 20 20 20 20 21 73 74 72 6e 63 6d 70 28 70 43 74      !strncmp(pCt
e7280 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
e7290 2c 20 70 72 6f 78 79 50 61 74 68 2c 20 4d 41 58  , proxyPath, MAX
e72a0 50 41 54 48 4c 45 4e 29 29 0a 20 20 20 20 20 20  PATHLEN)).      
e72b0 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
e72c0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
e72d0 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  K;.          }el
e72e0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
e72f0 72 63 20 3d 20 73 77 69 74 63 68 4c 6f 63 6b 50  rc = switchLockP
e7300 72 6f 78 79 50 61 74 68 28 70 46 69 6c 65 2c 20  roxyPath(pFile, 
e7310 70 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20  proxyPath);.    
e7320 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
e7330 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
e7340 20 2f 2a 20 74 75 72 6e 20 6f 6e 20 70 72 6f 78   /* turn on prox
e7350 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a  y file locking *
e7360 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  /.          rc =
e7370 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55   proxyTransformU
e7380 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c 20 70  nixFile(pFile, p
e7390 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20 20  roxyPath);.     
e73a0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
e73b0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
e73c0 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
e73d0 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
e73e0 20 30 20 29 3b 20 20 2f 2a 20 54 68 65 20 63 61   0 );  /* The ca
e73f0 6c 6c 20 61 73 73 75 72 65 73 20 74 68 61 74 20  ll assures that 
e7400 6f 6e 6c 79 20 76 61 6c 69 64 20 6f 70 63 6f 64  only valid opcod
e7410 65 73 20 61 72 65 20 73 65 6e 74 20 2a 2f 0a 20  es are sent */. 
e7420 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54     }.  }.  /*NOT
e7430 52 45 41 43 48 45 44 2a 2f 0a 20 20 72 65 74 75  REACHED*/.  retu
e7440 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
e7450 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 69 74 68 69 6e  .}../*.** Within
e7460 20 74 68 69 73 20 64 69 76 69 73 69 6f 6e 20 28   this division (
e7470 74 68 65 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63  the proxying loc
e7480 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74  king implementat
e7490 69 6f 6e 29 20 74 68 65 20 70 72 6f 63 65 64 75  ion) the procedu
e74a0 72 65 73 0a 2a 2a 20 61 62 6f 76 65 20 74 68 69  res.** above thi
e74b0 73 20 70 6f 69 6e 74 20 61 72 65 20 61 6c 6c 20  s point are all 
e74c0 75 74 69 6c 69 74 69 65 73 2e 20 20 54 68 65 20  utilities.  The 
e74d0 6c 6f 63 6b 2d 72 65 6c 61 74 65 64 20 6d 65 74  lock-related met
e74e0 68 6f 64 73 20 6f 66 20 74 68 65 0a 2a 2a 20 70  hods of the.** p
e74f0 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c  roxy-locking sql
e7500 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 20 6f  ite3_io_method o
e7510 62 6a 65 63 74 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f  bject follow..*/
e7520 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  .../*.** This ro
e7530 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
e7540 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
e7550 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
e7560 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
e7570 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
e7580 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
e7590 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
e75a0 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74  ock is held, set
e75b0 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20   *pResOut.** to 
e75c0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
e75d0 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73   otherwise *pRes
e75e0 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65  Out is set to ze
e75f0 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  ro.  The return 
e7600 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20  value.** is set 
e7610 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c  to SQLITE_OK unl
e7620 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72  ess an I/O error
e7630 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c   occurs during l
e7640 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f  ock checking..*/
e7650 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
e7660 79 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  yCheckReservedLo
e7670 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
e7680 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
e7690 74 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  t) {.  unixFile 
e76a0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
e76b0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63  le*)id;.  int rc
e76c0 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63   = proxyTakeConc
e76d0 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  h(pFile);.  if( 
e76e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
e76f0 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e  .    proxyLockin
e7700 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d  gContext *pCtx =
e7710 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f   (proxyLockingCo
e7720 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c  ntext *)pFile->l
e7730 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
e7740 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f     unixFile *pro
e7750 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  xy = pCtx->lockP
e7760 72 6f 78 79 3b 0a 20 20 20 20 72 65 74 75 72 6e  roxy;.    return
e7770 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d   proxy->pMethod-
e7780 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  >xCheckReservedL
e7790 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
e77a0 65 2a 29 70 72 6f 78 79 2c 20 70 52 65 73 4f 75  e*)proxy, pResOu
e77b0 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
e77c0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
e77d0 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68  ck the file with
e77e0 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66   the lock specif
e77f0 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
e7800 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a   locktype - one.
e7810 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** of the follow
e7820 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  ing:.**.**     (
e7830 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a  1) SHARED_LOCK.*
e7840 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56  *     (2) RESERV
e7850 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
e7860 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a  3) PENDING_LOCK.
e7870 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55  **     (4) EXCLU
e7880 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20  SIVE_LOCK.**.** 
e7890 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72  Sometimes when r
e78a0 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f  equesting one lo
e78b0 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69  ck state, additi
e78c0 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73  onal lock states
e78d0 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64  .** are inserted
e78e0 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68   in between.  Th
e78f0 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20  e locking might 
e7900 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74  fail on one of t
e7910 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e  he later.** tran
e7920 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20  sitions leaving 
e7930 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64  the lock state d
e7940 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68  ifferent from wh
e7950 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75  at it started bu
e7960 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74  t.** still short
e7970 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54   of its goal.  T
e7980 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
e7990 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c  rt shows the all
e79a0 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69  owed.** transiti
e79b0 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65  ons and the inse
e79c0 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74  rted intermediat
e79d0 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
e79e0 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53     UNLOCKED -> S
e79f0 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  HARED.**    SHAR
e7a00 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a  ED -> RESERVED.*
e7a10 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28  *    SHARED -> (
e7a20 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
e7a30 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45  USIVE.**    RESE
e7a40 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  RVED -> (PENDING
e7a50 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
e7a60 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20  *    PENDING -> 
e7a70 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20  EXCLUSIVE.**.** 
e7a80 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
e7a90 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20  l only increase 
e7aa0 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65  a lock.  Use the
e7ab0 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b   sqlite3OsUnlock
e7ac0 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f  ().** routine to
e7ad0 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67   lower a locking
e7ae0 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69   level..*/.stati
e7af0 63 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28  c int proxyLock(
e7b00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
e7b10 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
e7b20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
e7b30 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
e7b40 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  )id;.  int rc = 
e7b50 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70  proxyTakeConch(p
e7b60 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  File);.  if( rc=
e7b70 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
e7b80 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f    proxyLockingCo
e7b90 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70  ntext *pCtx = (p
e7ba0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
e7bb0 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt *)pFile->lock
e7bc0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
e7bd0 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20  unixFile *proxy 
e7be0 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  = pCtx->lockProx
e7bf0 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78  y;.    rc = prox
e7c00 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63  y->pMethod->xLoc
e7c10 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
e7c20 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65  )proxy, locktype
e7c30 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  );.    pFile->lo
e7c40 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e  cktype = proxy->
e7c50 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20  locktype;.  }.  
e7c60 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
e7c70 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c  *.** Lower the l
e7c80 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20  ocking level on 
e7c90 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e7ca0 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70  pFile to locktyp
e7cb0 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20  e.  locktype.** 
e7cc0 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e  must be either N
e7cd0 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44  O_LOCK or SHARED
e7ce0 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  _LOCK..**.** If 
e7cf0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
e7d00 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65  l of the file de
e7d10 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65  scriptor is alre
e7d20 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a  ady at or below.
e7d30 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64  ** the requested
e7d40 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20   locking level, 
e7d50 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
e7d60 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74  a no-op..*/.stat
e7d70 69 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f  ic int proxyUnlo
e7d80 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
e7d90 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
e7da0 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  e) {.  unixFile 
e7db0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
e7dc0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63  le*)id;.  int rc
e7dd0 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63   = proxyTakeConc
e7de0 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  h(pFile);.  if( 
e7df0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
e7e00 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e  .    proxyLockin
e7e10 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d  gContext *pCtx =
e7e20 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f   (proxyLockingCo
e7e30 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c  ntext *)pFile->l
e7e40 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
e7e50 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f     unixFile *pro
e7e60 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  xy = pCtx->lockP
e7e70 72 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d 20 70  roxy;.    rc = p
e7e80 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  roxy->pMethod->x
e7e90 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f  Unlock((sqlite3_
e7ea0 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63  file*)proxy, loc
e7eb0 6b 74 79 70 65 29 3b 0a 20 20 20 20 70 46 69 6c  ktype);.    pFil
e7ec0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72  e->locktype = pr
e7ed0 6f 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20  oxy->locktype;. 
e7ee0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
e7ef0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
e7f00 20 66 69 6c 65 20 74 68 61 74 20 75 73 65 73 20   file that uses 
e7f10 70 72 6f 78 79 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a  proxy locks..*/.
e7f20 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
e7f30 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69  Close(sqlite3_fi
e7f40 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20  le *id) {.  if( 
e7f50 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69  id ){.    unixFi
e7f60 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
e7f70 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 70  xFile*)id;.    p
e7f80 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
e7f90 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
e7fa0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
e7fb0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
e7fc0 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69  Context;.    uni
e7fd0 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79  xFile *lockProxy
e7fe0 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f   = pCtx->lockPro
e7ff0 78 79 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65  xy;.    unixFile
e8000 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43   *conchFile = pC
e8010 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20  tx->conchFile;. 
e8020 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49     int rc = SQLI
e8030 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 20 20  TE_OK;.    .    
e8040 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 20 29 7b  if( lockProxy ){
e8050 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b  .      rc = lock
e8060 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e  Proxy->pMethod->
e8070 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33  xUnlock((sqlite3
e8080 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79  _file*)lockProxy
e8090 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  , NO_LOCK);.    
e80a0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
e80b0 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d  n rc;.      rc =
e80c0 20 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74   lockProxy->pMet
e80d0 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c  hod->xClose((sql
e80e0 69 74 65 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50  ite3_file*)lockP
e80f0 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 69 66 28  roxy);.      if(
e8100 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
e8110 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
e8120 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a  ree(lockProxy);.
e8130 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b        pCtx->lock
e8140 50 72 6f 78 79 20 3d 20 30 3b 0a 20 20 20 20 7d  Proxy = 0;.    }
e8150 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 68 46 69  .    if( conchFi
e8160 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  le ){.      if( 
e8170 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20  pCtx->conchHeld 
e8180 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
e8190 70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63  proxyReleaseConc
e81a0 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20  h(pFile);.      
e81b0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
e81c0 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
e81d0 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69      rc = conchFi
e81e0 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c  le->pMethod->xCl
e81f0 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ose((sqlite3_fil
e8200 65 2a 29 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20  e*)conchFile);. 
e8210 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65       if( rc ) re
e8220 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73  turn rc;.      s
e8230 71 6c 69 74 65 33 5f 66 72 65 65 28 63 6f 6e 63  qlite3_free(conc
e8240 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  hFile);.    }.  
e8250 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
e8260 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
e8270 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  th);.    sqlite3
e8280 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63  _free(pCtx->conc
e8290 68 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 20 20  hFilePath);.    
e82a0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74  sqlite3_free(pCt
e82b0 78 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 20 20  x->dbPath);.    
e82c0 2f 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 6f  /* restore the o
e82d0 72 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20  riginal locking 
e82e0 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d 65 74  context and pMet
e82f0 68 6f 64 20 74 68 65 6e 20 63 6c 6f 73 65 20 69  hod then close i
e8300 74 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e  t */.    pFile->
e8310 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d  lockingContext =
e8320 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e   pCtx->oldLockin
e8330 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 46  gContext;.    pF
e8340 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70  ile->pMethod = p
e8350 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 3b  Ctx->pOldMethod;
e8360 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
e8370 65 28 70 43 74 78 29 3b 0a 20 20 20 20 72 65 74  e(pCtx);.    ret
e8380 75 72 6e 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68  urn pFile->pMeth
e8390 6f 64 2d 3e 78 43 6c 6f 73 65 28 69 64 29 3b 0a  od->xClose(id);.
e83a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
e83b0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e  ITE_OK;.}....#en
e83c0 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f  dif /* defined(_
e83d0 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
e83e0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e83f0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a  NG_STYLE */./*.*
e8400 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b  * The proxy lock
e8410 69 6e 67 20 73 74 79 6c 65 20 69 73 20 69 6e 74  ing style is int
e8420 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 77 69  ended for use wi
e8430 74 68 20 41 46 50 20 66 69 6c 65 73 79 73 74 65  th AFP filesyste
e8440 6d 73 2e 0a 2a 2a 20 41 6e 64 20 73 69 6e 63 65  ms..** And since
e8450 20 41 46 50 20 69 73 20 6f 6e 6c 79 20 73 75 70   AFP is only sup
e8460 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63 4f 53 58  ported on MacOSX
e8470 2c 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b  , the proxy lock
e8480 69 6e 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72  ing is also.** r
e8490 65 73 74 72 69 63 74 65 64 20 74 6f 20 4d 61 63  estricted to Mac
e84a0 4f 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a  OSX..** .**.****
e84b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
e84c0 45 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 78 79  End of the proxy
e84d0 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61   lock implementa
e84e0 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tion ***********
e84f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
e8500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
e8550 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  ** Initialize th
e8560 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
e8570 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  em interface..**
e8580 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
e8590 20 72 65 67 69 73 74 65 72 73 20 61 6c 6c 20 56   registers all V
e85a0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  FS implementatio
e85b0 6e 73 20 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65  ns for unix-like
e85c0 20 6f 70 65 72 61 74 69 6e 67 0a 2a 2a 20 73 79   operating.** sy
e85d0 73 74 65 6d 73 2e 20 20 54 68 69 73 20 72 6f 75  stems.  This rou
e85e0 74 69 6e 65 2c 20 61 6e 64 20 74 68 65 20 73 71  tine, and the sq
e85f0 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 72  lite3_os_end() r
e8600 6f 75 74 69 6e 65 20 74 68 61 74 20 66 6f 6c 6c  outine that foll
e8610 6f 77 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  ows,.** should b
e8620 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69  e the only routi
e8630 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nes in this file
e8640 20 74 68 61 74 20 61 72 65 20 76 69 73 69 62 6c   that are visibl
e8650 65 20 66 72 6f 6d 20 6f 74 68 65 72 0a 2a 2a 20  e from other.** 
e8660 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  files..**.** Thi
e8670 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
e8680 6c 65 64 20 6f 6e 63 65 20 64 75 72 69 6e 67 20  led once during 
e8690 53 51 4c 69 74 65 20 69 6e 69 74 69 61 6c 69 7a  SQLite initializ
e86a0 61 74 69 6f 6e 20 61 6e 64 20 62 79 20 61 0a 2a  ation and by a.*
e86b0 2a 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e  * single thread.
e86c0 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c    The memory all
e86d0 6f 63 61 74 69 6f 6e 20 61 6e 64 20 6d 75 74 65  ocation and mute
e86e0 78 20 73 75 62 73 79 73 74 65 6d 73 20 68 61 76  x subsystems hav
e86f0 65 20 6e 6f 74 0a 2a 2a 20 6e 65 63 65 73 73 61  e not.** necessa
e8700 72 69 6c 79 20 62 65 65 6e 20 69 6e 69 74 69 61  rily been initia
e8710 6c 69 7a 65 64 20 77 68 65 6e 20 74 68 69 73 20  lized when this 
e8720 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
e8730 64 2c 20 61 6e 64 20 73 6f 20 74 68 65 79 0a 2a  d, and so they.*
e8740 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  * should not be 
e8750 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  used..*/.SQLITE_
e8760 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
e8770 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 20 0a  os_init(void){ .
e8780 20 20 2f 2a 20 0a 20 20 2a 2a 20 54 68 65 20 66    /* .  ** The f
e8790 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 64  ollowing macro d
e87a0 65 66 69 6e 65 73 20 61 6e 20 69 6e 69 74 69 61  efines an initia
e87b0 6c 69 7a 65 72 20 66 6f 72 20 61 6e 20 73 71 6c  lizer for an sql
e87c0 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e  ite3_vfs object.
e87d0 0a 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f  .  ** The name o
e87e0 66 20 74 68 65 20 56 46 53 20 69 73 20 4e 41 4d  f the VFS is NAM
e87f0 45 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61  E.  The pAppData
e8800 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
e8810 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20   a pointer.  ** 
e8820 74 6f 20 74 68 65 20 22 66 69 6e 64 65 72 22 20  to the "finder" 
e8830 66 75 6e 63 74 69 6f 6e 2e 20 20 28 70 41 70 70  function.  (pApp
e8840 44 61 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65  Data is a pointe
e8850 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 62  r to a pointer b
e8860 65 63 61 75 73 65 0a 20 20 2a 2a 20 73 69 6c 6c  ecause.  ** sill
e8870 79 20 43 39 30 20 72 75 6c 65 73 20 70 72 6f 68  y C90 rules proh
e8880 69 62 69 74 20 61 20 76 6f 69 64 2a 20 66 72 6f  ibit a void* fro
e8890 6d 20 62 65 69 6e 67 20 63 61 73 74 20 74 6f 20  m being cast to 
e88a0 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74  a function point
e88b0 65 72 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 77  er.  ** and so w
e88c0 65 20 68 61 76 65 20 74 6f 20 67 6f 20 74 68 72  e have to go thr
e88d0 6f 75 67 68 20 74 68 65 20 69 6e 74 65 72 6d 65  ough the interme
e88e0 64 69 61 74 65 20 70 6f 69 6e 74 65 72 20 74 6f  diate pointer to
e88f0 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a   avoid problems.
e8900 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c    ** when compil
e8910 69 6e 67 20 77 69 74 68 20 2d 70 65 64 61 6e 74  ing with -pedant
e8920 69 63 2d 65 72 72 6f 72 73 20 6f 6e 20 47 43 43  ic-errors on GCC
e8930 2e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  .).  **.  ** The
e8940 20 46 49 4e 44 45 52 20 70 61 72 61 6d 65 74 65   FINDER paramete
e8950 72 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20  r to this macro 
e8960 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
e8970 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68  he pointer to th
e8980 65 0a 20 20 2a 2a 20 66 69 6e 64 65 72 2d 66 75  e.  ** finder-fu
e8990 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 6e  nction.  The fin
e89a0 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 74  der-function ret
e89b0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
e89c0 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74  o the.  ** sqlit
e89d0 65 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  e_io_methods obj
e89e0 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ect that impleme
e89f0 6e 74 73 20 74 68 65 20 64 65 73 69 72 65 64 20  nts the desired 
e8a00 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 62 65 68  locking.  ** beh
e8a10 61 76 69 6f 72 73 2e 20 20 53 65 65 20 74 68 65  aviors.  See the
e8a20 20 64 69 76 69 73 69 6f 6e 20 61 62 6f 76 65 20   division above 
e8a30 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
e8a40 65 20 49 4f 4d 45 54 48 4f 44 53 0a 20 20 2a 2a  e IOMETHODS.  **
e8a50 20 6d 61 63 72 6f 20 66 6f 72 20 61 64 64 69 74   macro for addit
e8a60 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ion information 
e8a70 6f 6e 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69  on finder-functi
e8a80 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d  ons..  **.  ** M
e8a90 6f 73 74 20 66 69 6e 64 65 72 73 20 73 69 6d 70  ost finders simp
e8aa0 6c 79 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  ly return a poin
e8ab0 74 65 72 20 74 6f 20 61 20 66 69 78 65 64 20 73  ter to a fixed s
e8ac0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e8ad0 73 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 2e 20 20  s.  ** object.  
e8ae0 42 75 74 20 74 68 65 20 22 61 75 74 6f 6c 6f 63  But the "autoloc
e8af0 6b 49 6f 46 69 6e 64 65 72 22 20 61 76 61 69 6c  kIoFinder" avail
e8b00 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 64  able on MacOSX d
e8b10 6f 65 73 20 61 20 6c 69 74 74 6c 65 0a 20 20 2a  oes a little.  *
e8b20 2a 20 6d 6f 72 65 20 74 68 61 6e 20 74 68 61 74  * more than that
e8b30 3b 20 69 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68  ; it looks at th
e8b40 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74 79 70  e filesystem typ
e8b50 65 20 74 68 61 74 20 68 6f 73 74 73 20 74 68 65  e that hosts the
e8b60 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20   .  ** database 
e8b70 66 69 6c 65 20 61 6e 64 20 74 72 69 65 73 20 74  file and tries t
e8b80 6f 20 63 68 6f 6f 73 65 20 61 6e 20 6c 6f 63 6b  o choose an lock
e8b90 69 6e 67 20 6d 65 74 68 6f 64 20 61 70 70 72 6f  ing method appro
e8ba0 70 72 69 61 74 65 20 66 6f 72 0a 20 20 2a 2a 20  priate for.  ** 
e8bb0 74 68 61 74 20 66 69 6c 65 73 79 73 74 65 6d 20  that filesystem 
e8bc0 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 23 64 65  time..  */.  #de
e8bd0 66 69 6e 65 20 55 4e 49 58 56 46 53 28 56 46 53  fine UNIXVFS(VFS
e8be0 4e 41 4d 45 2c 20 46 49 4e 44 45 52 29 20 7b 20  NAME, FINDER) { 
e8bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8c00 20 20 20 20 20 20 20 5c 0a 20 20 20 20 31 2c 20         \.    1, 
e8c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8c20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a     /* iVersion *
e8c30 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e8c40 20 20 20 20 20 5c 0a 20 20 20 20 73 69 7a 65 6f       \.    sizeo
e8c50 66 28 75 6e 69 78 46 69 6c 65 29 2c 20 20 20 20  f(unixFile),    
e8c60 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 20   /* szOsFile */ 
e8c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8c80 20 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50 41 54     \.    MAX_PAT
e8c90 48 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20 2f  HNAME,         /
e8ca0 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 20  * mxPathname */ 
e8cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8cc0 20 5c 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20   \.    0,       
e8cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e8ce0 70 4e 65 78 74 20 2a 2f 20 20 20 20 20 20 20 20  pNext */        
e8cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e8d00 0a 20 20 20 20 56 46 53 4e 41 4d 45 2c 20 20 20  .    VFSNAME,   
e8d10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e             /* zN
e8d20 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ame */          
e8d30 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e8d40 20 20 20 28 76 6f 69 64 2a 29 26 46 49 4e 44 45     (void*)&FINDE
e8d50 52 2c 20 20 20 20 20 20 20 2f 2a 20 70 41 70 70  R,       /* pApp
e8d60 44 61 74 61 20 2a 2f 20 20 20 20 20 20 20 20 20  Data */         
e8d70 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e8d80 20 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20 20 20   unixOpen,      
e8d90 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
e8da0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e8db0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
e8dc0 6e 69 78 44 65 6c 65 74 65 2c 20 20 20 20 20 20  nixDelete,      
e8dd0 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20       /* xDelete 
e8de0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e8df0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
e8e00 78 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20  xAccess,        
e8e10 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f     /* xAccess */
e8e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8e30 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 46       \.    unixF
e8e40 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 20  ullPathname,    
e8e50 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d   /* xFullPathnam
e8e60 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
e8e70 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 4f     \.    unixDlO
e8e80 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 2f  pen,           /
e8e90 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20 20 20  * xDlOpen */    
e8ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8eb0 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45 72 72   \.    unixDlErr
e8ec0 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  or,          /* 
e8ed0 78 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20 20 20  xDlError */     
e8ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e8ef0 0a 20 20 20 20 75 6e 69 78 44 6c 53 79 6d 2c 20  .    unixDlSym, 
e8f00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44             /* xD
e8f10 6c 53 79 6d 20 2a 2f 20 20 20 20 20 20 20 20 20  lSym */         
e8f20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e8f30 20 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65 2c 20     unixDlClose, 
e8f40 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43           /* xDlC
e8f50 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20 20  lose */         
e8f60 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e8f70 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 2c   unixRandomness,
e8f80 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f         /* xRando
e8f90 6d 6e 65 73 73 20 2a 2f 20 20 20 20 20 20 20 20  mness */        
e8fa0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
e8fb0 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20 20 20  nixSleep,       
e8fc0 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a       /* xSleep *
e8fd0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e8fe0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
e8ff0 78 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20  xCurrentTime,   
e9000 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69     /* xCurrentTi
e9010 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  me */           
e9020 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 47       \.    unixG
e9030 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 20  etLastError     
e9040 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f   /* xGetLastErro
e9050 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  r */            
e9060 20 20 20 5c 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20     \.  }..  /*. 
e9070 20 2a 2a 20 41 6c 6c 20 64 65 66 61 75 6c 74 20   ** All default 
e9080 56 46 53 65 73 20 66 6f 72 20 75 6e 69 78 20 61  VFSes for unix a
e9090 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  re contained in 
e90a0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72  the following ar
e90b0 72 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e  ray..  **.  ** N
e90c0 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 71 6c  ote that the sql
e90d0 69 74 65 33 5f 76 66 73 2e 70 4e 65 78 74 20 66  ite3_vfs.pNext f
e90e0 69 65 6c 64 20 6f 66 20 74 68 65 20 56 46 53 20  ield of the VFS 
e90f0 6f 62 6a 65 63 74 20 69 73 20 6d 6f 64 69 66 69  object is modifi
e9100 65 64 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 53  ed.  ** by the S
e9110 51 4c 69 74 65 20 63 6f 72 65 20 77 68 65 6e 20  QLite core when 
e9120 74 68 65 20 56 46 53 20 69 73 20 72 65 67 69 73  the VFS is regis
e9130 74 65 72 65 64 2e 20 20 53 6f 20 74 68 65 20 66  tered.  So the f
e9140 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 61 72  ollowing.  ** ar
e9150 72 61 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f  ray cannot be co
e9160 6e 73 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74  nst..  */.  stat
e9170 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 61  ic sqlite3_vfs a
e9180 56 66 73 5b 5d 20 3d 20 7b 0a 23 69 66 20 53 51  Vfs[] = {.#if SQ
e9190 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
e91a0 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 28 4f 53  ING_STYLE && (OS
e91b0 5f 56 58 57 4f 52 4b 53 20 7c 7c 20 64 65 66 69  _VXWORKS || defi
e91c0 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 29 0a  ned(__APPLE__)).
e91d0 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69      UNIXVFS("uni
e91e0 78 22 2c 20 20 20 20 20 20 20 20 20 20 61 75 74  x",          aut
e91f0 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c  olockIoFinder ),
e9200 0a 23 65 6c 73 65 0a 20 20 20 20 55 4e 49 58 56  .#else.    UNIXV
e9210 46 53 28 22 75 6e 69 78 22 2c 20 20 20 20 20 20  FS("unix",      
e9220 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65      posixIoFinde
e9230 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20  r ),.#endif.    
e9240 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 6e 6f  UNIXVFS("unix-no
e9250 6e 65 22 2c 20 20 20 20 20 6e 6f 6c 6f 63 6b 49  ne",     nolockI
e9260 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55  oFinder ),.    U
e9270 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 64 6f 74  NIXVFS("unix-dot
e9280 66 69 6c 65 22 2c 20 20 64 6f 74 6c 6f 63 6b 49  file",  dotlockI
e9290 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 4f  oFinder ),.#if O
e92a0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e  S_VXWORKS.    UN
e92b0 49 58 56 46 53 28 22 75 6e 69 78 2d 6e 61 6d 65  IXVFS("unix-name
e92c0 64 73 65 6d 22 2c 20 73 65 6d 49 6f 46 69 6e 64  dsem", semIoFind
e92d0 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66  er ),.#endif.#if
e92e0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
e92f0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20  OCKING_STYLE.   
e9300 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70   UNIXVFS("unix-p
e9310 6f 73 69 78 22 2c 20 20 20 20 70 6f 73 69 78 49  osix",    posixI
e9320 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 21  oFinder ),.#if !
e9330 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55  OS_VXWORKS.    U
e9340 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 66 6c 6f  NIXVFS("unix-flo
e9350 63 6b 22 2c 20 20 20 20 66 6c 6f 63 6b 49 6f 46  ck",    flockIoF
e9360 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a  inder ),.#endif.
e9370 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
e9380 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
e9390 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65  _STYLE && define
e93a0 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20  d(__APPLE__).   
e93b0 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 61   UNIXVFS("unix-a
e93c0 66 70 22 2c 20 20 20 20 20 20 61 66 70 49 6f 46  fp",      afpIoF
e93d0 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49  inder ),.    UNI
e93e0 58 56 46 53 28 22 75 6e 69 78 2d 70 72 6f 78 79  XVFS("unix-proxy
e93f0 22 2c 20 20 20 20 70 72 6f 78 79 49 6f 46 69 6e  ",    proxyIoFin
e9400 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20  der ),.#endif.  
e9410 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  };.  unsigned in
e9420 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  t i;          /*
e9430 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
e9440 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ..  /* Register 
e9450 61 6c 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65  all VFSes define
e9460 64 20 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20  d in the aVfs[] 
e9470 61 72 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69  array */.  for(i
e9480 3d 30 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56  =0; i<(sizeof(aV
e9490 66 73 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74  fs)/sizeof(sqlit
e94a0 65 33 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a  e3_vfs)); i++){.
e94b0 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f      sqlite3_vfs_
e94c0 72 65 67 69 73 74 65 72 28 26 61 56 66 73 5b 69  register(&aVfs[i
e94d0 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20  ], i==0);.  }.  
e94e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e94f0 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74  ; .}../*.** Shut
e9500 64 6f 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69  down the operati
e9510 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
e9520 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20  ace..**.** Some 
e9530 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
e9540 73 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20  s might need to 
e9550 64 6f 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20  do some cleanup 
e9560 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  in this routine,
e9570 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64  .** to release d
e9580 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
e9590 61 74 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42  ated objects.  B
e95a0 75 74 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a  ut not on unix..
e95b0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
e95c0 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75  is a no-op for u
e95d0 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  nix..*/.SQLITE_A
e95e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
e95f0 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20  s_end(void){ .  
e9600 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e9610 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a  ; .}. .#endif /*
e9620 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20   SQLITE_OS_UNIX 
e9630 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
e9640 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e  *** End of os_un
e9650 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ix.c ***********
e9660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9680 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
e9690 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
e96a0 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_win.c ********
e96b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e96c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e96d0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d  **/./*.** 2004 M
e96e0 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 22.**.** The 
e96f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
e9700 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
e9710 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
e9720 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
e9730 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
e9740 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
e9750 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
e9760 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
e9770 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
e9780 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
e9790 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
e97a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
e97b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
e97c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
e97d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
e97e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
e97f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
e9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9840 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
e9850 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
e9860 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ns code that is 
e9870 73 70 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64  specific to wind
e9880 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ows..**.** $Id: 
e9890 6f 73 5f 77 69 6e 2e 63 2c 76 20 31 2e 31 35 36  os_win.c,v 1.156
e98a0 20 32 30 30 39 2f 30 34 2f 32 33 20 31 39 3a 30   2009/04/23 19:0
e98b0 38 3a 33 33 20 73 68 61 6e 65 20 45 78 70 20 24  8:33 shane Exp $
e98c0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
e98d0 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20  S_WIN           
e98e0 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65      /* This file
e98f0 20 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e   is used for win
e9900 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f  dows only */.../
e9910 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75  *.** A Note Abou
e9920 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  t Memory Allocat
e9930 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion:.**.** This 
e9940 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c  driver uses mall
e9950 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65  oc()/free() dire
e9960 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e  ctly rather than
e9970 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a   going through.*
e9980 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61  * the SQLite-wra
e9990 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61  ppers sqlite3_ma
e99a0 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66  lloc()/sqlite3_f
e99b0 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72  ree().  Those wr
e99c0 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65  appers.** are de
e99d0 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f  signed for use o
e99e0 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
e99f0 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20  ms where memory 
e9a00 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a  is scarce and.**
e9a10 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
e9a20 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74   happen frequent
e9a30 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20  ly.  Win32 does 
e9a40 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75  not typically ru
e9a50 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64  n on.** embedded
e9a60 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68   systems, and wh
e9a70 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64  en it does the d
e9a80 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c  evelopers normal
e9a90 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a  ly have bigger.*
e9aa0 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f  * problems to wo
e9ab0 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72  rry about than r
e9ac0 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65  unning out of me
e9ad0 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20  mory.  So there 
e9ae0 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70  is not.** a comp
e9af0 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75  elling need to u
e9b00 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e  se the wrappers.
e9b10 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65  .**.** But there
e9b20 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f   is a good reaso
e9b30 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65  n to not use the
e9b40 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77   wrappers.  If w
e9b50 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61  e use the.** wra
e9b60 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69  ppers then we wi
e9b70 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64  ll get simulated
e9b80 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
e9b90 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a  es within this.*
e9ba0 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74  * driver.  And t
e9bb0 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b  hat causes all k
e9bc0 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73  inds of problems
e9bd0 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20   for our tests. 
e9be0 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68   We.** could enh
e9bf0 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64  ance SQLite to d
e9c00 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74  eal with simulat
e9c10 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ed malloc failur
e9c20 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65  es within.** the
e9c30 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20   OS driver, but 
e9c40 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c  the code to deal
e9c50 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c   with those fail
e9c60 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a  ure would not.**
e9c70 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e   be exercised on
e9c80 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f   Linux (which do
e9c90 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d  es not need to m
e9ca0 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64  alloc() in the d
e9cb0 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f  river).** and so
e9cc0 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64   we would have d
e9cd0 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e  ifficulty writin
e9ce0 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73  g coverage tests
e9cf0 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64   for that.** cod
e9d00 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65  e.  Better to le
e9d10 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74  ave the code out
e9d20 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a  , we think..**.*
e9d30 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74  * The point of t
e9d40 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69  his discussion i
e9d50 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57  s as follows:  W
e9d60 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e  hen creating a n
e9d70 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66  ew.** OS layer f
e9d80 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73  or an embedded s
e9d90 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73  ystem, if you us
e9da0 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61  e this file as a
e9db0 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76  n example,.** av
e9dc0 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d  oid the use of m
e9dd0 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20  alloc()/free(). 
e9de0 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20   Those routines 
e9df0 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f  work ok on windo
e9e00 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62  ws.** desktops b
e9e10 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69  ut not so well i
e9e20 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
e9e30 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65  ms..*/..#include
e9e40 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69   <winbase.h>..#i
e9e50 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a  fdef __CYGWIN__.
e9e60 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63  # include <sys/c
e9e70 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a  ygwin.h>.#endif.
e9e80 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73  ./*.** Macros us
e9e90 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
e9ea0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
e9eb0 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a  o use threads..*
e9ec0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48  /.#if defined(TH
e9ed0 52 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52  READSAFE) && THR
e9ee0 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65  EADSAFE.# define
e9ef0 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45   SQLITE_W32_THRE
e9f00 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ADS 1.#endif../*
e9f10 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65  .** Include code
e9f20 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
e9f30 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69  to all os_*.c fi
e9f40 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  les.*/./********
e9f50 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f  ****** Include o
e9f60 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68  s_common.h in th
e9f70 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77  e middle of os_w
e9f80 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
e9f90 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
e9fa0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
e9fb0 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  e os_common.h **
e9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9fe0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
e9ff0 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54  4 May 22.**.** T
ea000 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
ea010 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
ea020 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
ea030 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
ea040 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
ea050 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
ea060 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
ea070 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
ea080 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
ea090 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
ea0a0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
ea0b0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
ea0c0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
ea0d0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
ea0e0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
ea0f0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
ea100 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
ea110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
ea160 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
ea170 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64  tains macros and
ea180 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66   a little bit of
ea190 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
ea1a0 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f  mmon to.** all o
ea1b0 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73  f the platform-s
ea1c0 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f  pecific files (o
ea1d0 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69  s_*.c) and is #i
ea1e0 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f  ncluded into tho
ea1f0 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a  se.** files..**.
ea200 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f  ** This file sho
ea210 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64  uld be #included
ea220 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66   by the os_*.c f
ea230 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69  iles only.  It i
ea240 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72  s not a.** gener
ea250 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65  al purpose heade
ea260 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  r file..**.** $I
ea270 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76  d: os_common.h,v
ea280 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34   1.38 2009/02/24
ea290 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c   18:40:50 daniel
ea2a0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23  k1977 Exp $.*/.#
ea2b0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f  ifndef _OS_COMMO
ea2c0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53  N_H_.#define _OS
ea2d0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a  _COMMON_H_../*.*
ea2e0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62  * At least two b
ea2f0 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64  ugs have slipped
ea300 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63   in because we c
ea310 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52  hanged the MEMOR
ea320 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f  Y_DEBUG.** macro
ea330 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47   to SQLITE_DEBUG
ea340 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20   and some older 
ea350 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e  makefiles have n
ea360 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a  ot yet made the.
ea370 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20  ** switch.  The 
ea380 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73  following code s
ea390 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73  hould catch this
ea3a0 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70   problem at comp
ea3b0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66  ile-time..*/.#if
ea3c0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  def MEMORY_DEBUG
ea3d0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45  .# error "The ME
ea3e0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f  MORY_DEBUG macro
ea3f0 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55   is obsolete.  U
ea400 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  se SQLITE_DEBUG 
ea410 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66  instead.".#endif
ea420 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
ea430 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
ea440 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ea450 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65  OSTrace = 0;.#de
ea460 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29  fine OSTRACE1(X)
ea470 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
ea480 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
ea490 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
ea4a0 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  (X).#define OSTR
ea4b0 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20  ACE2(X,Y)       
ea4c0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
ea4d0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
ea4e0 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65  gPrintf(X,Y).#de
ea4f0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
ea500 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c  Y,Z)     if( sql
ea510 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
ea520 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
ea530 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
ea540 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41  OSTRACE4(X,Y,Z,A
ea550 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f  )   if( sqlite3O
ea560 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
ea570 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
ea580 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Z,A).#define OST
ea590 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29  RACE5(X,Y,Z,A,B)
ea5a0 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72   if( sqlite3OSTr
ea5b0 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
ea5c0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
ea5d0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
ea5e0 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
ea5f0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
ea600 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
ea610 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
ea620 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69  Y,Z,A,B,C).#defi
ea630 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c  ne OSTRACE7(X,Y,
ea640 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20  Z,A,B,C,D) \.   
ea650 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61   if(sqlite3OSTra
ea660 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
ea670 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
ea680 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66  ,C,D).#else.#def
ea690 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a  ine OSTRACE1(X).
ea6a0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32  #define OSTRACE2
ea6b0 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53  (X,Y).#define OS
ea6c0 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64  TRACE3(X,Y,Z).#d
ea6d0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58  efine OSTRACE4(X
ea6e0 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20  ,Y,Z,A).#define 
ea6f0 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41  OSTRACE5(X,Y,Z,A
ea700 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
ea710 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
ea720 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
ea730 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
ea740 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
ea750 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f  Macros for perfo
ea760 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20  rmance tracing. 
ea770 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64   Normally turned
ea780 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b   off.  Only work
ea790 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72  s.** on i486 har
ea7a0 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  dware..*/.#ifdef
ea7b0 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41   SQLITE_PERFORMA
ea7c0 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a  NCE_TRACE../* .*
ea7d0 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61  * hwtime.h conta
ea7e0 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d  ins inline assem
ea7f0 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d  bler code for im
ea800 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68  plementing .** h
ea810 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20  igh-performance 
ea820 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e  timing routines.
ea830 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
ea840 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69  *** Include hwti
ea850 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  me.h in the midd
ea860 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  le of os_common.
ea870 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
ea880 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
ea890 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
ea8a0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
ea8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea8d0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d  **/./*.** 2008 M
ea8e0 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 27.**.** The 
ea8f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
ea900 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
ea910 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
ea920 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
ea930 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
ea940 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
ea950 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
ea960 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
ea970 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
ea980 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
ea990 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
ea9a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
ea9b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
ea9c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
ea9d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
ea9e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
ea9f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
eaa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
eaa50 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
eaa60 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f  ns inline asm co
eaa70 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e  de for retrievin
eaa80 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61  g "high-performa
eaa90 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73  nce".** counters
eaaa0 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43   for x86 class C
eaab0 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  PUs..**.** $Id: 
eaac0 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32  hwtime.h,v 1.3 2
eaad0 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a  008/08/01 14:33:
eaae0 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  15 shane Exp $.*
eaaf0 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d  /.#ifndef _HWTIM
eab00 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57  E_H_.#define _HW
eab10 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54  TIME_H_../*.** T
eab20 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
eab30 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tine only works 
eab40 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73  on pentium-class
eab50 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63   (or newer) proc
eab60 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73  essors..** It us
eab70 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63  es the RDTSC opc
eab80 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20  ode to read the 
eab90 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75  cycle count valu
eaba0 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20  e out of the.** 
eabb0 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65  processor and re
eabc0 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65  turns that value
eabd0 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  .  This can be u
eabe0 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73  sed for high-res
eabf0 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a  .** profiling..*
eac00 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f  /.#if (defined(_
eac10 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69  _GNUC__) || defi
eac20 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26  ned(_MSC_VER)) &
eac30 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e  & \.      (defin
eac40 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69  ed(i386) || defi
eac50 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c  ned(__i386__) ||
eac60 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36   defined(_M_IX86
eac70 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65  ))..  #if define
eac80 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f  d(__GNUC__)..  _
eac90 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
eaca0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
eacb0 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
eacc0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c    unsigned int l
eacd0 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73  o, hi;.     __as
eace0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
eacf0 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22   ("rdtsc" : "=a"
ead00 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29   (lo), "=d" (hi)
ead10 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28  );.     return (
ead20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69  sqlite_uint64)hi
ead30 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d   << 32 | lo;.  }
ead40 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65  ..  #elif define
ead50 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f  d(_MSC_VER)..  _
ead60 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29  _declspec(naked)
ead70 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65   __inline sqlite
ead80 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20  _uint64 __cdecl 
ead90 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
eada0 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20  id){.     __asm 
eadb0 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a  {.        rdtsc.
eadc0 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20          ret     
eadd0 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65    ; return value
eade0 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20   at EDX:EAX.    
eadf0 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66   }.  }..  #endif
eae00 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64  ..#elif (defined
eae10 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65  (__GNUC__) && de
eae20 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f  fined(__x86_64__
eae30 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ))..  __inline__
eae40 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
eae50 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
eae60 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  d){.      unsign
eae70 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20  ed long val;.   
eae80 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
eae90 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
eaea0 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a   : "=A" (val));.
eaeb0 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c        return val
eaec0 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64  ;.  }. .#elif (d
eaed0 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
eaee0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70   && defined(__pp
eaef0 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  c__))..  __inlin
eaf00 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
eaf10 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
eaf20 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
eaf30 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
eaf40 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e  retval;.      un
eaf50 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b  signed long junk
eaf60 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
eaf70 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c  __volatile__ ("\
eaf80 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20  n\.          1: 
eaf90 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c       mftbu   %1\
eafa0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
eafb0 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30       mftb    %L0
eafc0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
eafd0 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30        mftbu   %0
eafe0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
eaff0 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30        cmpw    %0
eb000 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  ,%1\n\.         
eb010 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20           bne    
eb020 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20   1b".           
eb030 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72         : "=r" (r
eb040 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75  etval), "=r" (ju
eb050 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75  nk));.      retu
eb060 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a  rn retval;.  }..
eb070 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20  #else..  #error 
eb080 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74  Need implementat
eb090 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77  ion of sqlite3Hw
eb0a0 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
eb0b0 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a  platform...  /*.
eb0c0 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20    ** To compile 
eb0d0 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e  without implemen
eb0e0 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69  ting sqlite3Hwti
eb0f0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
eb100 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75  atform,.  ** you
eb110 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20   can remove the 
eb120 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64  above #error and
eb130 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69   use the followi
eb140 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e  ng.  ** stub fun
eb150 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c  ction.  You will
eb160 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70   lose timing sup
eb170 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20  port for many.  
eb180 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67  ** of the debugg
eb190 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20  ing and testing 
eb1a0 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69  utilities, but i
eb1b0 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a  t should at.  **
eb1c0 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61   least compile a
eb1d0 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c  nd run..  */.SQL
eb1e0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71  ITE_PRIVATE   sq
eb1f0 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
eb200 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
eb210 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65   return ((sqlite
eb220 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23  _uint64)0); }..#
eb230 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
eb240 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d   !defined(_HWTIM
eb250 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  E_H_) */../*****
eb260 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
eb270 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a   hwtime.h ******
eb280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb2a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
eb2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
eb2c0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
eb2d0 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d  ft off in os_com
eb2e0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mon.h **********
eb2f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69  ********/..stati
eb300 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  c sqlite_uint64 
eb310 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20  g_start;.static 
eb320 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
eb330 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65  elapsed;.#define
eb340 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20   TIMER_START    
eb350 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74     g_start=sqlit
eb360 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69  e3Hwtime().#defi
eb370 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20  ne TIMER_END    
eb380 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73       g_elapsed=s
eb390 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67  qlite3Hwtime()-g
eb3a0 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54  _start.#define T
eb3b0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20  IMER_ELAPSED    
eb3c0 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65   g_elapsed.#else
eb3d0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53  .#define TIMER_S
eb3e0 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d  TART.#define TIM
eb3f0 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54  ER_END.#define T
eb400 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20  IMER_ELAPSED    
eb410 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34   ((sqlite_uint64
eb420 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  )0).#endif../*.*
eb430 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20  * If we compile 
eb440 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
eb450 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20  TEST macro set, 
eb460 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  then the followi
eb470 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63  ng block.** of c
eb480 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73  ode will give us
eb490 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20   the ability to 
eb4a0 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20  simulate a disk 
eb4b0 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73  I/O error.  This
eb4c0 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20  .** is used for 
eb4d0 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20  testing the I/O 
eb4e0 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a  recovery logic..
eb4f0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
eb500 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
eb510 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
eb520 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20  error_hit = 0;  
eb530 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
eb540 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f  al number of I/O
eb550 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54   Errors */.SQLIT
eb560 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
eb570 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68  3_io_error_hardh
eb580 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  it = 0;        /
eb590 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d  * Number of non-
eb5a0 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f  benign errors */
eb5b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
eb5c0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
eb5d0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20  _pending = 0;   
eb5e0 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f       /* Count do
eb5f0 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20  wn to first I/O 
eb600 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f  error */.SQLITE_
eb610 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
eb620 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74  io_error_persist
eb630 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
eb640 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f  True if I/O erro
eb650 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51  rs persist */.SQ
eb660 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
eb670 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65  ite3_io_error_be
eb680 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  nign = 0;       
eb690 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72    /* True if err
eb6a0 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a  ors are benign *
eb6b0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
eb6c0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
eb6d0 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53  l_pending = 0;.S
eb6e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
eb6f0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d  lite3_diskfull =
eb700 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75   0;.#define Simu
eb710 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
eb720 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f  n(X) sqlite3_io_
eb730 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29  error_benign=(X)
eb740 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
eb750 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20  eIOError(CODE)  
eb760 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33  \.  if( (sqlite3
eb770 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73  _io_error_persis
eb780 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f  t && sqlite3_io_
eb790 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20  error_hit) \.   
eb7a0 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69      || sqlite3_i
eb7b0 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d  o_error_pending-
eb7c0 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20  - == 1 )  \.    
eb7d0 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61            { loca
eb7e0 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b  l_ioerr(); CODE;
eb7f0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c   }.static void l
eb800 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20  ocal_ioerr(){.  
eb810 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c  IOTRACE(("IOERR\
eb820 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  n"));.  sqlite3_
eb830 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a  io_error_hit++;.
eb840 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69    if( !sqlite3_i
eb850 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29  o_error_benign )
eb860 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
eb870 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23  r_hardhit++;.}.#
eb880 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44  define SimulateD
eb890 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44  iskfullError(COD
eb8a0 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69  E) \.   if( sqli
eb8b0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
eb8c0 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69  ding ){ \.     i
eb8d0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
eb8e0 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31  ull_pending == 1
eb8f0 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63   ){ \.       loc
eb900 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20  al_ioerr(); \.  
eb910 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73       sqlite3_dis
eb920 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20  kfull = 1; \.   
eb930 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65      sqlite3_io_e
eb940 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a  rror_hit = 1; \.
eb950 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20         CODE; \. 
eb960 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20      }else{ \.   
eb970 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b      sqlite3_disk
eb980 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20  full_pending--; 
eb990 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a  \.     } \.   }.
eb9a0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69  #else.#define Si
eb9b0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
eb9c0 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53  ign(X).#define S
eb9d0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41  imulateIOError(A
eb9e0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
eb9f0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
eba00 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  A).#endif../*.**
eba10 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b   When testing, k
eba20 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74  eep a count of t
eba30 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65  he number of ope
eba40 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64  n files..*/.#ifd
eba50 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
eba60 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
eba70 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f  lite3_open_file_
eba80 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69  count = 0;.#defi
eba90 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
ebaa0 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  )  sqlite3_open_
ebab0 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a  file_count+=(X).
ebac0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70  #else.#define Op
ebad0 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e  enCounter(X).#en
ebae0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
ebaf0 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d  defined(_OS_COMM
ebb00 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ON_H_) */../****
ebb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
ebb20 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  f os_common.h **
ebb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
ebb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
ebb70 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
ebb80 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69  eft off in os_wi
ebb90 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
ebba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
ebbb0 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74  * Some microsoft
ebbc0 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20   compilers lack 
ebbd0 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e  this definition.
ebbe0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41  .*/.#ifndef INVA
ebbf0 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  LID_FILE_ATTRIBU
ebc00 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56  TES.# define INV
ebc10 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
ebc20 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29  UTES ((DWORD)-1)
ebc30 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20   .#endif../*.** 
ebc40 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20  Determine if we 
ebc50 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
ebc60 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69   WindowsCE - whi
ebc70 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a  ch has a much.**
ebc80 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f   reduced API..*/
ebc90 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
ebca0 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72  INCE.# define Ar
ebcb0 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20  eFileApisANSI() 
ebcc0 31 0a 23 20 64 65 66 69 6e 65 20 47 65 74 44 69  1.# define GetDi
ebcd0 73 6b 46 72 65 65 53 70 61 63 65 57 28 29 20 30  skFreeSpaceW() 0
ebce0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
ebcf0 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76  inCE lacks nativ
ebd00 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69  e support for fi
ebd10 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65  le locking so we
ebd20 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74   have to fake it
ebd30 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f  .** with some co
ebd40 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a  de of our own..*
ebd50 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
ebd60 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74  WINCE.typedef st
ebd70 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b  ruct winceLock {
ebd80 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b  .  int nReaders;
ebd90 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
ebda0 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73   of reader locks
ebdb0 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42   obtained */.  B
ebdc0 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20  OOL bPending;   
ebdd0 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20     /* Indicates 
ebde0 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68  a pending lock h
ebdf0 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64  as been obtained
ebe00 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65   */.  BOOL bRese
ebe10 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64  rved;     /* Ind
ebe20 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65  icates a reserve
ebe30 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20  d lock has been 
ebe40 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f  obtained */.  BO
ebe50 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20  OL bExclusive;  
ebe60 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61    /* Indicates a
ebe70 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
ebe80 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e   has been obtain
ebe90 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63  ed */.} winceLoc
ebea0 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  k;.#endif../*.**
ebeb0 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72   The winFile str
ebec0 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63  ucture is a subc
ebed0 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  lass of sqlite3_
ebee0 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74  file* specific t
ebef0 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70  o the win32.** p
ebf00 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72  ortability layer
ebf10 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
ebf20 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46  uct winFile winF
ebf30 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46  ile;.struct winF
ebf40 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  ile {.  const sq
ebf50 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
ebf60 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73   *pMethod;/* Mus
ebf70 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20  t be first */.  
ebf80 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20  HANDLE h;       
ebf90 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
ebfa0 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  e for accessing 
ebfb0 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e  the file */.  un
ebfc0 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b  signed char lock
ebfd0 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66  type; /* Type of
ebfe0 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20   lock currently 
ebff0 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c  held on this fil
ec000 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61  e */.  short sha
ec010 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f  redLockByte;   /
ec020 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65  * Randomly chose
ec030 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61  n byte used as a
ec040 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a   shared lock */.
ec050 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e    DWORD lastErrn
ec060 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  o;        /* The
ec070 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20 66   Windows errno f
ec080 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f  rom the last I/O
ec090 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f 52   error */.  DWOR
ec0a0 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20  D sectorSize;   
ec0b0 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69      /* Sector si
ec0c0 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65  ze of the device
ec0d0 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a 23   file is on */.#
ec0e0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ec0f0 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c  CE.  WCHAR *zDel
ec100 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20  eteOnClose;  /* 
ec110 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
ec120 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73  delete when clos
ec130 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20  ing */.  HANDLE 
ec140 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  hMutex;         
ec150 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74   /* Mutex used t
ec160 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
ec170 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20   to shared lock 
ec180 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53  */  .  HANDLE hS
ec190 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f  hared;         /
ec1a0 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * Shared memory 
ec1b0 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72  segment used for
ec1c0 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69   locking */.  wi
ec1d0 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20  nceLock local;  
ec1e0 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f        /* Locks o
ec1f0 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20  btained by this 
ec200 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46  instance of winF
ec210 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f  ile */.  winceLo
ec220 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20  ck *shared;     
ec230 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65   /* Global share
ec240 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f  d lock memory fo
ec250 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23  r the file  */.#
ec260 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
ec270 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79 70  Forward prototyp
ec280 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
ec290 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28  t getSectorSize(
ec2a0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
ec2b0 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73   *pVfs,.    cons
ec2c0 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
ec2d0 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66  e     /* UTF-8 f
ec2e0 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a  ile name */.);..
ec2f0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
ec300 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20  ing variable is 
ec310 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f  (normally) set o
ec320 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68  nce and never ch
ec330 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66  anges.** thereaf
ec340 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73  ter.  It records
ec350 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65   whether the ope
ec360 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73  rating system is
ec370 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e   Win95.** or Win
ec380 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f  NT..**.** 0:   O
ec390 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
ec3a0 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20  unknown..** 1:  
ec3b0 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   Operating syste
ec3c0 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32  m is Win95..** 2
ec3d0 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79  :   Operating sy
ec3e0 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a  stem is WinNT..*
ec3f0 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f  *.** In order to
ec400 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74   facilitate test
ec410 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73  ing on a WinNT s
ec420 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20  ystem, the test 
ec430 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d  fixture.** can m
ec440 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73  anually set this
ec450 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65   value to 1 to e
ec460 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68  mulate Win98 beh
ec470 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66  avior..*/.#ifdef
ec480 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
ec490 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
ec4a0 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b  te3_os_type = 0;
ec4b0 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e  .#else.static in
ec4c0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70  t sqlite3_os_typ
ec4d0 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  e = 0;.#endif../
ec4e0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
ec4f0 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77   (non-zero) if w
ec500 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e  e are running un
ec510 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b  der WinNT, Win2K
ec520 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57  , WinXP,.** or W
ec530 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61  inCE.  Return fa
ec540 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57  lse (zero) for W
ec550 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20  in95, Win98, or 
ec560 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72  WinME..**.** Her
ec570 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74  e is an interest
ec580 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a  ing observation:
ec590 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20    Win95, Win98, 
ec5a0 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a  and WinME lack.*
ec5b0 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78  * the LockFileEx
ec5c0 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20  () API.  But we 
ec5d0 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63  can still static
ec5e0 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73  ally link agains
ec5f0 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73  t that.** API as
ec600 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27   long as we don'
ec610 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20 72  t call it when r
ec620 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f  unning Win95/98/
ec630 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a  ME.  A call to.*
ec640 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * this routine i
ec650 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
ec660 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20  ine if the host 
ec670 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f  is Win95/98/ME o
ec680 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50  r.** WinNT/2K/XP
ec690 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c   so that we will
ec6a0 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72   know whether or
ec6b0 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65   not we can safe
ec6c0 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c  ly call.** the L
ec6d0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
ec6e0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
ec6f0 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65  S_WINCE.# define
ec700 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c   isNT()  (1).#el
ec710 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  se.  static int 
ec720 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20  isNT(void){.    
ec730 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74  if( sqlite3_os_t
ec740 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ype==0 ){.      
ec750 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49  OSVERSIONINFO sI
ec760 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f  nfo;.      sInfo
ec770 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f  .dwOSVersionInfo
ec780 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49  Size = sizeof(sI
ec790 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56  nfo);.      GetV
ec7a0 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29  ersionEx(&sInfo)
ec7b0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
ec7c0 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e  os_type = sInfo.
ec7d0 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45  dwPlatformId==VE
ec7e0 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32  R_PLATFORM_WIN32
ec7f0 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20  _NT ? 2 : 1;.   
ec800 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71   }.    return sq
ec810 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32  lite3_os_type==2
ec820 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
ec830 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
ec840 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  */../*.** Conver
ec850 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67  t a UTF-8 string
ec860 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e   to microsoft un
ec870 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e  icode (UTF-16?).
ec880 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f   .**.** Space to
ec890 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e   hold the return
ec8a0 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74  ed string is obt
ec8b0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
ec8c0 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48  c..*/.static WCH
ec8d0 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64  AR *utf8ToUnicod
ec8e0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  e(const char *zF
ec8f0 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20  ilename){.  int 
ec900 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a  nChar;.  WCHAR *
ec910 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a  zWideFilename;..
ec920 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42    nChar = MultiB
ec930 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50  yteToWideChar(CP
ec940 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e  _UTF8, 0, zFilen
ec950 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30  ame, -1, NULL, 0
ec960 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61  );.  zWideFilena
ec970 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68  me = malloc( nCh
ec980 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46  ar*sizeof(zWideF
ec990 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20  ilename[0]) );. 
ec9a0 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61   if( zWideFilena
ec9b0 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  me==0 ){.    ret
ec9c0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68  urn 0;.  }.  nCh
ec9d0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f  ar = MultiByteTo
ec9e0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38  WideChar(CP_UTF8
ec9f0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  , 0, zFilename, 
eca00 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  -1, zWideFilenam
eca10 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28  e, nChar);.  if(
eca20 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20   nChar==0 ){.   
eca30 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e   free(zWideFilen
eca40 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46  ame);.    zWideF
eca50 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d  ilename = 0;.  }
eca60 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46  .  return zWideF
eca70 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ilename;.}../*.*
eca80 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73  * Convert micros
eca90 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55  oft unicode to U
ecaa0 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20  TF-8.  Space to 
ecab0 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
ecac0 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f  d string is.** o
ecad0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c  btained from mal
ecae0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
ecaf0 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f   char *unicodeTo
ecb00 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52  Utf8(const WCHAR
ecb10 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29   *zWideFilename)
ecb20 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  {.  int nByte;. 
ecb30 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
ecb40 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64  ;..  nByte = Wid
ecb50 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
ecb60 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69  (CP_UTF8, 0, zWi
ecb70 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20  deFilename, -1, 
ecb80 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a  0, 0, 0, 0);.  z
ecb90 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f  Filename = mallo
ecba0 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66  c( nByte );.  if
ecbb0 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29  ( zFilename==0 )
ecbc0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
ecbd0 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69    }.  nByte = Wi
ecbe0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74  deCharToMultiByt
ecbf0 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57  e(CP_UTF8, 0, zW
ecc00 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  ideFilename, -1,
ecc10 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74   zFilename, nByt
ecc20 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
ecc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecc40 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42   0, 0);.  if( nB
ecc50 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  yte == 0 ){.    
ecc60 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  free(zFilename);
ecc70 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d  .    zFilename =
ecc80 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
ecc90 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f   zFilename;.}../
ecca0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20  *.** Convert an 
eccb0 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d  ansi string to m
eccc0 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65  icrosoft unicode
eccd0 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a  , based on the.*
ecce0 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61  * current codepa
eccf0 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20  ge settings for 
ecd00 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a  file apis..** .*
ecd10 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
ecd20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  the returned str
ecd30 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a  ing is obtained.
ecd40 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a  ** from malloc..
ecd50 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20  */.static WCHAR 
ecd60 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63  *mbcsToUnicode(c
ecd70 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
ecd80 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79  name){.  int nBy
ecd90 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62  te;.  WCHAR *zMb
ecda0 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e  csFilename;.  in
ecdb0 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65  t codepage = Are
ecdc0 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f  FileApisANSI() ?
ecdd0 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d   CP_ACP : CP_OEM
ecde0 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d  CP;..  nByte = M
ecdf0 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68  ultiByteToWideCh
ece00 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20  ar(codepage, 0, 
ece10 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e  zFilename, -1, N
ece20 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43  ULL,0)*sizeof(WC
ece30 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c  HAR);.  zMbcsFil
ece40 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20  ename = malloc( 
ece50 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62  nByte*sizeof(zMb
ece60 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29  csFilename[0]) )
ece70 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c  ;.  if( zMbcsFil
ece80 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  ename==0 ){.    
ece90 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
ecea0 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74  nByte = MultiByt
eceb0 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65  eToWideChar(code
ecec0 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61  page, 0, zFilena
eced0 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c  me, -1, zMbcsFil
ecee0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20  ename, nByte);. 
ecef0 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b   if( nByte==0 ){
ecf00 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46  .    free(zMbcsF
ecf10 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d  ilename);.    zM
ecf20 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b  bcsFilename = 0;
ecf30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d  .  }.  return zM
ecf40 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  bcsFilename;.}..
ecf50 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69  /*.** Convert mi
ecf60 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20  crosoft unicode 
ecf70 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61  to multibyte cha
ecf80 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62  racter string, b
ecf90 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75  ased on the.** u
ecfa0 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70  ser's Ansi codep
ecfb0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65  age..**.** Space
ecfc0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74   to hold the ret
ecfd0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20  urned string is 
ecfe0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
ecff0 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74   malloc()..*/.st
ed000 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f  atic char *unico
ed010 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57  deToMbcs(const W
ed020 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e  CHAR *zWideFilen
ed030 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ame){.  int nByt
ed040 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  e;.  char *zFile
ed050 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65  name;.  int code
ed060 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70  page = AreFileAp
ed070 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43  isANSI() ? CP_AC
ed080 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20  P : CP_OEMCP;.. 
ed090 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61   nByte = WideCha
ed0a0 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64  rToMultiByte(cod
ed0b0 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46  epage, 0, zWideF
ed0c0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20  ilename, -1, 0, 
ed0d0 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c  0, 0, 0);.  zFil
ed0e0 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20  ename = malloc( 
ed0f0 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a  nByte );.  if( z
ed100 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20  Filename==0 ){. 
ed110 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
ed120 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43  .  nByte = WideC
ed130 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63  harToMultiByte(c
ed140 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64  odepage, 0, zWid
ed150 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a  eFilename, -1, z
ed160 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c  Filename, nByte,
ed170 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ed180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
ed190 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  , 0);.  if( nByt
ed1a0 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72  e == 0 ){.    fr
ed1b0 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  ee(zFilename);. 
ed1c0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30     zFilename = 0
ed1d0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
ed1e0 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  Filename;.}../*.
ed1f0 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69  ** Convert multi
ed200 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73  byte character s
ed210 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20  tring to UTF-8. 
ed220 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
ed230 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73  he.** returned s
ed240 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
ed250 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e  d from malloc().
ed260 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
ed270 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e  har *sqlite3_win
ed280 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28  32_mbcs_to_utf8(
ed290 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
ed2a0 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a  ename){.  char *
ed2b0 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20  zFilenameUtf8;. 
ed2c0 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65   WCHAR *zTmpWide
ed2d0 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20  ;..  zTmpWide = 
ed2e0 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46  mbcsToUnicode(zF
ed2f0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ilename);.  if( 
ed300 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20  zTmpWide==0 ){. 
ed310 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
ed320 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38  .  zFilenameUtf8
ed330 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38   = unicodeToUtf8
ed340 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72  (zTmpWide);.  fr
ed350 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20  ee(zTmpWide);.  
ed360 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65  return zFilename
ed370 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Utf8;.}../*.** C
ed380 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20  onvert UTF-8 to 
ed390 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63  multibyte charac
ed3a0 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61  ter string.  Spa
ed3b0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a  ce to hold the .
ed3c0 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  ** returned stri
ed3d0 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  ng is obtained f
ed3e0 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  rom malloc()..*/
ed3f0 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74  .static char *ut
ed400 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63  f8ToMbcs(const c
ed410 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
ed420 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  .  char *zFilena
ed430 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20  meMbcs;.  WCHAR 
ed440 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54  *zTmpWide;..  zT
ed450 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55  mpWide = utf8ToU
ed460 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65  nicode(zFilename
ed470 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64  );.  if( zTmpWid
ed480 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
ed490 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c  rn 0;.  }.  zFil
ed4a0 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63  enameMbcs = unic
ed4b0 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69  odeToMbcs(zTmpWi
ed4c0 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70  de);.  free(zTmp
ed4d0 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Wide);.  return 
ed4e0 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d  zFilenameMbcs;.}
ed4f0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
ed500 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  WINCE./*********
ed510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed550 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e  .** This section
ed560 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66   contains code f
ed570 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a  or WinCE only..*
ed580 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43  /./*.** WindowsC
ed590 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  E does not have 
ed5a0 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75  a localtime() fu
ed5b0 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61  nction.  So crea
ed5c0 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75  te a.** substitu
ed5d0 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d  te..*/.struct tm
ed5e0 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74   *__cdecl localt
ed5f0 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74  ime(const time_t
ed600 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20   *t).{.  static 
ed610 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46  struct tm y;.  F
ed620 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d  ILETIME uTm, lTm
ed630 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70  ;.  SYSTEMTIME p
ed640 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  Tm;.  sqlite3_in
ed650 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d  t64 t64;.  t64 =
ed660 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36   *t;.  t64 = (t6
ed670 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29  4 + 11644473600)
ed680 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d  *10000000;.  uTm
ed690 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d  .dwLowDateTime =
ed6a0 20 74 36 34 20 26 20 30 78 46 46 46 46 46 46 46   t64 & 0xFFFFFFF
ed6b0 46 3b 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44  F;.  uTm.dwHighD
ed6c0 61 74 65 54 69 6d 65 3d 20 74 36 34 20 3e 3e 20  ateTime= t64 >> 
ed6d0 33 32 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f  32;.  FileTimeTo
ed6e0 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28 26 75  LocalFileTime(&u
ed6f0 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65  Tm,&lTm);.  File
ed700 54 69 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65  TimeToSystemTime
ed710 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79  (&lTm,&pTm);.  y
ed720 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77  .tm_year = pTm.w
ed730 59 65 61 72 20 2d 20 31 39 30 30 3b 0a 20 20 79  Year - 1900;.  y
ed740 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d  .tm_mon = pTm.wM
ed750 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d  onth - 1;.  y.tm
ed760 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79  _wday = pTm.wDay
ed770 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d  OfWeek;.  y.tm_m
ed780 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a  day = pTm.wDay;.
ed790 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54    y.tm_hour = pT
ed7a0 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f  m.wHour;.  y.tm_
ed7b0 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74  min = pTm.wMinut
ed7c0 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20  e;.  y.tm_sec = 
ed7d0 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72  pTm.wSecond;.  r
ed7e0 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20  eturn &y;.}../* 
ed7f0 54 68 69 73 20 77 69 6c 6c 20 6e 65 76 65 72 20  This will never 
ed800 62 65 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 64  be called, but d
ed810 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74  efined to make t
ed820 68 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20  he code compile 
ed830 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65 74 54 65  */.#define GetTe
ed840 6d 70 50 61 74 68 41 28 61 2c 62 29 0a 0a 23 64  mpPathA(a,b)..#d
ed850 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61  efine LockFile(a
ed860 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 20 20  ,b,c,d,e)       
ed870 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61  winceLockFile(&a
ed880 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64  , b, c, d, e).#d
ed890 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65  efine UnlockFile
ed8a0 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20  (a,b,c,d,e)     
ed8b0 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28  winceUnlockFile(
ed8c0 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a  &a, b, c, d, e).
ed8d0 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65  #define LockFile
ed8e0 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20  Ex(a,b,c,d,e,f) 
ed8f0 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45    winceLockFileE
ed900 78 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65  x(&a, b, c, d, e
ed910 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65 20 48 41  , f)..#define HA
ed920 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28  NDLE_TO_WINFILE(
ed930 61 29 20 28 77 69 6e 46 69 6c 65 2a 29 26 28 28  a) (winFile*)&((
ed940 63 68 61 72 2a 29 61 29 5b 2d 6f 66 66 73 65 74  char*)a)[-offset
ed950 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a  of(winFile,h)]..
ed960 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20  /*.** Acquire a 
ed970 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61 6e 64  lock on the hand
ed980 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63 20 76  le h.*/.static v
ed990 6f 69 64 20 77 69 6e 63 65 4d 75 74 65 78 41 63  oid winceMutexAc
ed9a0 71 75 69 72 65 28 48 41 4e 44 4c 45 20 68 29 7b  quire(HANDLE h){
ed9b0 0a 20 20 20 44 57 4f 52 44 20 64 77 45 72 72 3b  .   DWORD dwErr;
ed9c0 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 64 77  .   do {.     dw
ed9d0 45 72 72 20 3d 20 57 61 69 74 46 6f 72 53 69 6e  Err = WaitForSin
ed9e0 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49 4e 46  gleObject(h, INF
ed9f0 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77 68 69  INITE);.   } whi
eda00 6c 65 20 28 64 77 45 72 72 20 21 3d 20 57 41 49  le (dwErr != WAI
eda10 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20 64 77  T_OBJECT_0 && dw
eda20 45 72 72 20 21 3d 20 57 41 49 54 5f 41 42 41 4e  Err != WAIT_ABAN
eda30 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20  DONED);.}./*.** 
eda40 52 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b 20 61  Release a lock a
eda50 63 71 75 69 72 65 64 20 62 79 20 77 69 6e 63 65  cquired by wince
eda60 4d 75 74 65 78 41 63 71 75 69 72 65 28 29 0a 2a  MutexAcquire().*
eda70 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e 63 65 4d  /.#define winceM
eda80 75 74 65 78 52 65 6c 65 61 73 65 28 68 29 20 52  utexRelease(h) R
eda90 65 6c 65 61 73 65 4d 75 74 65 78 28 68 29 0a 0a  eleaseMutex(h)..
edaa0 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65  /*.** Create the
edab0 20 6d 75 74 65 78 20 61 6e 64 20 73 68 61 72 65   mutex and share
edac0 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f  d memory used fo
edad0 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 65  r locking in the
edae0 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70   file.** descrip
edaf0 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73 74 61  tor pFile.*/.sta
edb00 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 43 72  tic BOOL winceCr
edb10 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74 20 63  eateLock(const c
edb20 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
edb30 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b  winFile *pFile){
edb40 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a  .  WCHAR *zTok;.
edb50 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65 20 3d    WCHAR *zName =
edb60 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a   utf8ToUnicode(z
edb70 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f  Filename);.  BOO
edb80 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a  L bInit = TRUE;.
edb90 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
edba0 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64   the local lockd
edbb0 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d  ata */.  ZeroMem
edbc0 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f 63 61  ory(&pFile->loca
edbd0 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c 65 2d  l, sizeof(pFile-
edbe0 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20  >local));..  /* 
edbf0 52 65 70 6c 61 63 65 20 74 68 65 20 62 61 63 6b  Replace the back
edc00 73 6c 61 73 68 65 73 20 66 72 6f 6d 20 74 68 65  slashes from the
edc10 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 6c 6f   filename and lo
edc20 77 65 72 63 61 73 65 20 69 74 0a 20 20 2a 2a 20  wercase it.  ** 
edc30 74 6f 20 64 65 72 69 76 65 20 61 20 6d 75 74 65  to derive a mute
edc40 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f  x name. */.  zTo
edc50 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72 57 28 7a  k = CharLowerW(z
edc60 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28 3b 2a  Name);.  for (;*
edc70 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20  zTok;zTok++){.  
edc80 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27    if (*zTok == '
edc90 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27  \\') *zTok = '_'
edca0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61  ;.  }..  /* Crea
edcb0 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61 6d 65  te/open the name
edcc0 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70 46 69  d mutex */.  pFi
edcd0 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43 72 65  le->hMutex = Cre
edce0 61 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c 2c 20  ateMutexW(NULL, 
edcf0 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20  FALSE, zName);. 
edd00 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75   if (!pFile->hMu
edd10 74 65 78 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  tex){.    pFile-
edd20 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74  >lastErrno = Get
edd30 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
edd40 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20   free(zName);.  
edd50 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a    return FALSE;.
edd60 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
edd70 65 20 74 68 65 20 6d 75 74 65 78 20 62 65 66 6f  e the mutex befo
edd80 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20 2a 2f  re continuing */
edd90 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71  .  winceMutexAcq
edda0 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  uire(pFile->hMut
eddb0 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53 69 6e  ex);.  .  /* Sin
eddc0 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  ce the names of 
eddd0 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c 20 73  named mutexes, s
edde0 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c 65 20  emaphores, file 
eddf0 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61 72 65  mappings etc are
ede00 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65 6e 73   .  ** case-sens
ede10 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64 76 61  itive, take adva
ede20 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20 62 79  ntage of that by
ede30 20 75 70 70 65 72 63 61 73 69 6e 67 20 74 68 65   uppercasing the
ede40 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20 2a 2a   mutex name.  **
ede50 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61 74 20   and using that 
ede60 61 73 20 74 68 65 20 73 68 61 72 65 64 20 66 69  as the shared fi
ede70 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65 2e 0a  lemapping name..
ede80 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 70 65 72    */.  CharUpper
ede90 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 46 69 6c  W(zName);.  pFil
edea0 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 43 72 65  e->hShared = Cre
edeb0 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 57 28  ateFileMappingW(
edec0 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56  INVALID_HANDLE_V
eded0 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20  ALUE, NULL,.    
edee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
edef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
edf00 20 20 20 50 41 47 45 5f 52 45 41 44 57 52 49 54     PAGE_READWRIT
edf10 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e  E, 0, sizeof(win
edf20 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 20 20 20  ceLock),.       
edf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
edf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
edf50 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 2f 2a 20  zName);  ..  /* 
edf60 53 65 74 20 61 20 66 6c 61 67 20 74 68 61 74 20  Set a flag that 
edf70 69 6e 64 69 63 61 74 65 73 20 77 65 27 72 65 20  indicates we're 
edf80 74 68 65 20 66 69 72 73 74 20 74 6f 20 63 72 65  the first to cre
edf90 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 73  ate the memory s
edfa0 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 73 74 20  o it .  ** must 
edfb0 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 61 6c 69  be zero-initiali
edfc0 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 47 65 74  zed */.  if (Get
edfd0 4c 61 73 74 45 72 72 6f 72 28 29 20 3d 3d 20 45  LastError() == E
edfe0 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f 45 58 49  RROR_ALREADY_EXI
edff0 53 54 53 29 7b 0a 20 20 20 20 62 49 6e 69 74 20  STS){.    bInit 
ee000 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20  = FALSE;.  }..  
ee010 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a 20 20  free(zName);..  
ee020 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65 65 64  /* If we succeed
ee030 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74 68 65  ed in making the
ee040 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68   shared memory h
ee050 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e 20 2a  andle, map it. *
ee060 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68  /.  if (pFile->h
ee070 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70 46 69  Shared){.    pFi
ee080 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28 77 69  le->shared = (wi
ee090 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 69 65 77  nceLock*)MapView
ee0a0 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 53  OfFile(pFile->hS
ee0b0 68 61 72 65 64 2c 20 0a 20 20 20 20 20 20 20 20  hared, .        
ee0c0 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f 52 45       FILE_MAP_RE
ee0d0 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52 49 54  AD|FILE_MAP_WRIT
ee0e0 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28  E, 0, 0, sizeof(
ee0f0 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 20  winceLock));.   
ee100 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67 20 66   /* If mapping f
ee110 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74 68 65  ailed, close the
ee120 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68   shared memory h
ee130 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73 65 20  andle and erase 
ee140 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28 21 70  it */.    if (!p
ee150 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b 0a 20  File->shared){. 
ee160 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
ee170 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45  Errno = GetLastE
ee180 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 43 6c  rror();.      Cl
ee190 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d  oseHandle(pFile-
ee1a0 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20  >hShared);.     
ee1b0 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20   pFile->hShared 
ee1c0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20  = NULL;.    }.  
ee1d0 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61 72 65  }..  /* If share
ee1e0 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 20 6e  d memory could n
ee1f0 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c 20 74  ot be created, t
ee200 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 6d 75  hen close the mu
ee210 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a 2f 0a  tex and fail */.
ee220 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68    if (pFile->hSh
ee230 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20  ared == NULL){. 
ee240 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c     winceMutexRel
ee250 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  ease(pFile->hMut
ee260 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61  ex);.    CloseHa
ee270 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  ndle(pFile->hMut
ee280 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ex);.    pFile->
ee290 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20  hMutex = NULL;. 
ee2a0 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b     return FALSE;
ee2b0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 6e 69  .  }.  .  /* Ini
ee2c0 74 69 61 6c 69 7a 65 20 74 68 65 20 73 68 61 72  tialize the shar
ee2d0 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 77 65 27  ed memory if we'
ee2e0 72 65 20 73 75 70 70 6f 73 65 64 20 74 6f 20 2a  re supposed to *
ee2f0 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 29 20 7b  /.  if (bInit) {
ee300 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28  .    ZeroMemory(
ee310 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c 20 73  pFile->shared, s
ee320 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29  izeof(winceLock)
ee330 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d  );.  }..  winceM
ee340 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
ee350 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65  e->hMutex);.  re
ee360 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a  turn TRUE;.}../*
ee370 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 68 65 20  .** Destroy the 
ee380 70 61 72 74 20 6f 66 20 77 69 6e 46 69 6c 65 20  part of winFile 
ee390 74 68 61 74 20 64 65 61 6c 73 20 77 69 74 68 20  that deals with 
ee3a0 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73  wince locks.*/.s
ee3b0 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65  tatic void wince
ee3c0 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69 6e 46  DestroyLock(winF
ee3d0 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69  ile *pFile){.  i
ee3e0 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  f (pFile->hMutex
ee3f0 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75 69 72  ){.    /* Acquir
ee400 65 20 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20  e the mutex */. 
ee410 20 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71     winceMutexAcq
ee420 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  uire(pFile->hMut
ee430 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  ex);..    /* The
ee440 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
ee450 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c  s should probabl
ee460 79 20 61 73 73 65 72 74 20 69 6e 20 64 65 62 75  y assert in debu
ee470 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 68 65 79  g mode, but they
ee480 0a 20 20 20 20 20 20 20 61 72 65 20 74 6f 20 63  .       are to c
ee490 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 65 20 61  leanup in case a
ee4a0 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69 6e 65  ny locks remaine
ee4b0 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69 66  d open */.    if
ee4c0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e   (pFile->local.n
ee4d0 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20  Readers){.      
ee4e0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e  pFile->shared->n
ee4f0 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20  Readers --;.    
ee500 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  }.    if (pFile-
ee510 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64  >local.bReserved
ee520 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
ee530 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65  shared->bReserve
ee540 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d  d = FALSE;.    }
ee550 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
ee560 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b  local.bPending){
ee570 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
ee580 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d  ared->bPending =
ee590 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20   FALSE;.    }.  
ee5a0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
ee5b0 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a  al.bExclusive){.
ee5c0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
ee5d0 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20  red->bExclusive 
ee5e0 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a  = FALSE;.    }..
ee5f0 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 65 72 65      /* De-refere
ee600 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 20 6f 75  nce and close ou
ee610 72 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 68  r copy of the sh
ee620 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64  ared memory hand
ee630 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61 70 56  le */.    UnmapV
ee640 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d  iewOfFile(pFile-
ee650 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 20 43 6c  >shared);.    Cl
ee660 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d  oseHandle(pFile-
ee670 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 20 20 20  >hShared);..    
ee680 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 68 65  /* Done with the
ee690 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69   mutex */.    wi
ee6a0 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28  nceMutexRelease(
ee6b0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20  pFile->hMutex); 
ee6c0 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e     .    CloseHan
ee6d0 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  dle(pFile->hMute
ee6e0 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68  x);.    pFile->h
ee6f0 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20  Mutex = NULL;.  
ee700 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69  }.}../* .** An i
ee710 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
ee720 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20   the LockFile() 
ee730 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66  API of windows f
ee740 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74  or wince.*/.stat
ee750 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63  ic BOOL winceLoc
ee760 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20  kFile(.  HANDLE 
ee770 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44  *phFile,.  DWORD
ee780 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77   dwFileOffsetLow
ee790 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65  ,.  DWORD dwFile
ee7a0 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57  OffsetHigh,.  DW
ee7b0 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
ee7c0 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44  esToLockLow,.  D
ee7d0 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79  WORD nNumberOfBy
ee7e0 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b  tesToLockHigh.){
ee7f0 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
ee800 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49  e = HANDLE_TO_WI
ee810 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20  NFILE(phFile);. 
ee820 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20   BOOL bReturn = 
ee830 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70  FALSE;..  if (!p
ee840 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65  File->hMutex) re
ee850 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e  turn TRUE;.  win
ee860 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70  ceMutexAcquire(p
ee870 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a  File->hMutex);..
ee880 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20    /* Wanting an 
ee890 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20  exclusive lock? 
ee8a0 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f  */.  if (dwFileO
ee8b0 66 66 73 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52  ffsetLow == SHAR
ee8c0 45 44 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20  ED_FIRST.       
ee8d0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
ee8e0 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48  sToLockLow == SH
ee8f0 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20  ARED_SIZE){.    
ee900 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65  if (pFile->share
ee910 64 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30  d->nReaders == 0
ee920 20 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65   && pFile->share
ee930 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d  d->bExclusive ==
ee940 20 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c   0){.       pFil
ee950 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c  e->shared->bExcl
ee960 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20  usive = TRUE;.  
ee970 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
ee980 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54  l.bExclusive = T
ee990 52 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74  RUE;.       bRet
ee9a0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
ee9b0 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74  }.  }..  /* Want
ee9c0 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63   a read-only loc
ee9d0 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20  k? */.  else if 
ee9e0 28 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f  ((dwFileOffsetLo
ee9f0 77 20 3e 3d 20 53 48 41 52 45 44 5f 46 49 52 53  w >= SHARED_FIRS
eea00 54 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  T &&.           
eea10 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77   dwFileOffsetLow
eea20 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20   < SHARED_FIRST 
eea30 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29 20 26  + SHARED_SIZE) &
eea40 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e  &.            nN
eea50 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f  umberOfBytesToLo
eea60 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20  ckLow == 1){.   
eea70 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72   if (pFile->shar
eea80 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d  ed->bExclusive =
eea90 3d 20 30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  = 0){.      pFil
eeaa0 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
eeab0 73 20 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28  s ++;.      if (
eeac0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65  pFile->local.nRe
eead0 61 64 65 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20  aders == 1){.   
eeae0 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
eeaf0 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b  ed->nReaders ++;
eeb00 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
eeb10 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20  Return = TRUE;. 
eeb20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57     }.  }..  /* W
eeb30 61 6e 74 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f  ant a pending lo
eeb40 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ck? */.  else if
eeb50 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f   (dwFileOffsetLo
eeb60 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54  w == PENDING_BYT
eeb70 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79  E && nNumberOfBy
eeb80 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20  tesToLockLow == 
eeb90 31 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f  1){.    /* If no
eeba0 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61   pending lock ha
eebb0 73 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c  s been acquired,
eebc0 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 69 74   then acquire it
eebd0 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c   */.    if (pFil
eebe0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64  e->shared->bPend
eebf0 69 6e 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20  ing == 0) {.    
eec00 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
eec10 3e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45  >bPending = TRUE
eec20 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
eec30 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20  ocal.bPending = 
eec40 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74  TRUE;.      bRet
eec50 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
eec60 7d 0a 20 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20  }.  }.  /* Want 
eec70 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f  a reserved lock?
eec80 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
eec90 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
eeca0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20  = RESERVED_BYTE 
eecb0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
eecc0 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29  sToLockLow == 1)
eecd0 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  {.    if (pFile-
eece0 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76  >shared->bReserv
eecf0 65 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20  ed == 0) {.     
eed00 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
eed10 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 45  bReserved = TRUE
eed20 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
eed30 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d  ocal.bReserved =
eed40 20 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65   TRUE;.      bRe
eed50 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
eed60 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d   }.  }..  winceM
eed70 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
eed80 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65  e->hMutex);.  re
eed90 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a  turn bReturn;.}.
eeda0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d  ./*.** An implem
eedb0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
eedc0 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f  UnlockFile API o
eedd0 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69  f windows for wi
eede0 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f  nce.*/.static BO
eedf0 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69  OL winceUnlockFi
eee00 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68  le(.  HANDLE *ph
eee10 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77  File,.  DWORD dw
eee20 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20  FileOffsetLow,. 
eee30 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66   DWORD dwFileOff
eee40 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44  setHigh,.  DWORD
eee50 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
eee60 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57  oUnlockLow,.  DW
eee70 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
eee80 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29  esToUnlockHigh.)
eee90 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  {.  winFile *pFi
eeea0 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57  le = HANDLE_TO_W
eeeb0 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a  INFILE(phFile);.
eeec0 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d    BOOL bReturn =
eeed0 20 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21   FALSE;..  if (!
eeee0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72  pFile->hMutex) r
eeef0 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69  eturn TRUE;.  wi
eef00 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28  nceMutexAcquire(
eef10 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
eef20 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20  .  /* Releasing 
eef30 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72  a reader lock or
eef40 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
eef50 63 6b 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69  ck */.  if (dwFi
eef60 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53  leOffsetLow >= S
eef70 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20  HARED_FIRST &&. 
eef80 20 20 20 20 20 20 64 77 46 69 6c 65 4f 66 66 73        dwFileOffs
eef90 65 74 4c 6f 77 20 3c 20 53 48 41 52 45 44 5f 46  etLow < SHARED_F
eefa0 49 52 53 54 20 2b 20 53 48 41 52 45 44 5f 53 49  IRST + SHARED_SI
eefb0 5a 45 29 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20  ZE){.    /* Did 
eefc0 77 65 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75  we have an exclu
eefd0 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20  sive lock? */.  
eefe0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
eeff0 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a  al.bExclusive){.
ef000 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
ef010 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20  al.bExclusive = 
ef020 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69  FALSE;.      pFi
ef030 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63  le->shared->bExc
ef040 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a  lusive = FALSE;.
ef050 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
ef060 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  TRUE;.    }..   
ef070 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 74 20   /* Did we just 
ef080 68 61 76 65 20 61 20 72 65 61 64 65 72 20 6c 6f  have a reader lo
ef090 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20  ck? */.    else 
ef0a0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
ef0b0 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20  .nReaders){.    
ef0c0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e    pFile->local.n
ef0d0 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20  Readers --;.    
ef0e0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
ef0f0 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30  al.nReaders == 0
ef100 29 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  ).      {.      
ef110 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
ef120 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20  >nReaders --;.  
ef130 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74      }.      bRet
ef140 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
ef150 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65  }.  }..  /* Rele
ef160 61 73 69 6e 67 20 61 20 70 65 6e 64 69 6e 67 20  asing a pending 
ef170 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69  lock */.  else i
ef180 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c  f (dwFileOffsetL
ef190 6f 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59  ow == PENDING_BY
ef1a0 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42  TE && nNumberOfB
ef1b0 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20  ytesToUnlockLow 
ef1c0 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70  == 1){.    if (p
ef1d0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e  File->local.bPen
ef1e0 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69  ding){.      pFi
ef1f0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69  le->local.bPendi
ef200 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ng = FALSE;.    
ef210 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
ef220 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53  >bPending = FALS
ef230 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
ef240 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
ef250 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e   }.  /* Releasin
ef260 67 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  g a reserved loc
ef270 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  k */.  else if (
ef280 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
ef290 3d 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  == RESERVED_BYTE
ef2a0 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74   && nNumberOfByt
ef2b0 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d  esToUnlockLow ==
ef2c0 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69   1){.    if (pFi
ef2d0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72  le->local.bReser
ef2e0 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46 69  ved) {.      pFi
ef2f0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72  le->local.bReser
ef300 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ved = FALSE;.   
ef310 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
ef320 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41  ->bReserved = FA
ef330 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75  LSE;.      bRetu
ef340 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d  rn = TRUE;.    }
ef350 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74  .  }..  winceMut
ef360 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d  exRelease(pFile-
ef370 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  >hMutex);.  retu
ef380 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  rn bReturn;.}../
ef390 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e  *.** An implemen
ef3a0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f  tation of the Lo
ef3b0 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20 6f  ckFileEx() API o
ef3c0 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69  f windows for wi
ef3d0 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f  nce.*/.static BO
ef3e0 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65  OL winceLockFile
ef3f0 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68  Ex(.  HANDLE *ph
ef400 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77  File,.  DWORD dw
ef410 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 64  Flags,.  DWORD d
ef420 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57 4f  wReserved,.  DWO
ef430 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  RD nNumberOfByte
ef440 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57  sToLockLow,.  DW
ef450 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
ef460 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20  esToLockHigh,.  
ef470 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70 4f  LPOVERLAPPED lpO
ef480 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f  verlapped.){.  /
ef490 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  * If the caller 
ef4a0 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72  wants a shared r
ef4b0 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72  ead lock, forwar
ef4c0 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a  d this call.  **
ef4d0 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c   to winceLockFil
ef4e0 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65  e */.  if (lpOve
ef4f0 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20  rlapped->Offset 
ef500 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20  == SHARED_FIRST 
ef510 26 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 73  &&.      dwFlags
ef520 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 6e   == 1 &&.      n
ef530 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
ef540 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44  ockLow == SHARED
ef550 5f 53 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75  _SIZE){.    retu
ef560 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65  rn winceLockFile
ef570 28 70 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f  (phFile, SHARED_
ef580 46 49 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b  FIRST, 0, 1, 0);
ef590 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41  .  }.  return FA
ef5a0 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64  LSE;.}./*.** End
ef5b0 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20   of the special 
ef5c0 63 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a  code for wince.*
ef5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65  ************/.#e
ef620 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
ef630 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a  S_WINCE */../***
ef640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
ef690 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20  e next group of 
ef6a0 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65  routines impleme
ef6b0 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f  nt the I/O metho
ef6c0 64 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  ds specified.** 
ef6d0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  by the sqlite3_i
ef6e0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
ef6f0 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
ef700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef740 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  /../*.** Close a
ef750 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20   file..**.** It 
ef760 69 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74  is reported that
ef770 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63   an attempt to c
ef780 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69  lose a handle mi
ef790 67 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a  ght sometimes.**
ef7a0 20 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20   fail.  This is 
ef7b0 61 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61  a very unreasona
ef7c0 62 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20  ble result, but 
ef7d0 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72  windows is notor
ef7e0 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e  ious.** for bein
ef7f0 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73  g unreasonable s
ef800 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74  o I do not doubt
ef810 20 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68   that it might h
ef820 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68  appen.  If.** th
ef830 65 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77  e close fails, w
ef840 65 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20  e pause for 100 
ef850 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64  milliseconds and
ef860 20 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a   try again.  As.
ef870 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c  ** many as MX_CL
ef880 4f 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65  OSE_ATTEMPT atte
ef890 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68  mpts to close th
ef8a0 65 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64  e handle are mad
ef8b0 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69  e before.** givi
ef8c0 6e 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e  ng up and return
ef8d0 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  ing an error..*/
ef8e0 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53  .#define MX_CLOS
ef8f0 45 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74  E_ATTEMPT 3.stat
ef900 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28  ic int winClose(
ef910 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
ef920 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74  ){.  int rc, cnt
ef930 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20   = 0;.  winFile 
ef940 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
ef950 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74  e*)id;..  assert
ef960 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54  ( id!=0 );.  OST
ef970 52 41 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c  RACE2("CLOSE %d\
ef980 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20  n", pFile->h);. 
ef990 20 64 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c   do{.    rc = Cl
ef9a0 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d  oseHandle(pFile-
ef9b0 3e 68 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72  >h);.  }while( r
ef9c0 63 3d 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20  c==0 && ++cnt < 
ef9d0 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54  MX_CLOSE_ATTEMPT
ef9e0 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c   && (Sleep(100),
ef9f0 20 31 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54   1) );.#if SQLIT
efa00 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69  E_OS_WINCE.#defi
efa10 6e 65 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f  ne WINCE_DELETIO
efa20 4e 5f 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77  N_ATTEMPTS 3.  w
efa30 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28  inceDestroyLock(
efa40 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46  pFile);.  if( pF
efa50 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c  ile->zDeleteOnCl
efa60 6f 73 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63  ose ){.    int c
efa70 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c  nt = 0;.    whil
efa80 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 44 65  e(.           De
efa90 6c 65 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d  leteFileW(pFile-
efaa0 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29  >zDeleteOnClose)
efab0 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 47  ==0.        && G
efac0 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
efad0 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65  W(pFile->zDelete
efae0 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66  OnClose)!=0xffff
efaf0 66 66 66 66 20 0a 20 20 20 20 20 20 20 20 26 26  ffff .        &&
efb00 20 63 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44   cnt++ < WINCE_D
efb10 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53  ELETION_ATTEMPTS
efb20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53  .    ){.       S
efb30 6c 65 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57  leep(100);  /* W
efb40 61 69 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66  ait a little bef
efb50 6f 72 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e  ore trying again
efb60 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72   */.    }.    fr
efb70 65 65 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74  ee(pFile->zDelet
efb80 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23  eOnClose);.  }.#
efb90 65 6e 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e  endif.  OpenCoun
efba0 74 65 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72  ter(-1);.  retur
efbb0 6e 20 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  n rc ? SQLITE_OK
efbc0 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b   : SQLITE_IOERR;
efbd0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d  .}../*.** Some m
efbe0 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65  icrosoft compile
efbf0 72 73 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66  rs lack this def
efc00 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e  inition..*/.#ifn
efc10 64 65 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f  def INVALID_SET_
efc20 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64  FILE_POINTER.# d
efc30 65 66 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45  efine INVALID_SE
efc40 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28  T_FILE_POINTER (
efc50 28 44 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69  (DWORD)-1).#endi
efc60 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61  f../*.** Read da
efc70 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69  ta from a file i
efc80 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52  nto a buffer.  R
efc90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
efca0 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20  if all.** bytes 
efcb0 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73  were read succes
efcc0 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54  sfully and SQLIT
efcd0 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68  E_IOERR if anyth
efce0 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e  ing goes.** wron
efcf0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
efd00 20 77 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69   winRead(.  sqli
efd10 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20  te3_file *id,   
efd20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74         /* File t
efd30 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20  o read from */. 
efd40 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20   void *pBuf,    
efd50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
efd60 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74  rite content int
efd70 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f  o this buffer */
efd80 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20  .  int amt,     
efd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
efda0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
efdb0 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71   to read */.  sq
efdc0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
efdd0 65 74 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69  et       /* Begi
efde0 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69  n reading at thi
efdf0 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20  s offset */.){. 
efe00 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20   LONG upperBits 
efe10 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74  = (LONG)((offset
efe20 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66  >>32) & 0x7fffff
efe30 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65  ff);.  LONG lowe
efe40 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f  rBits = (LONG)(o
efe50 66 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66  ffset & 0xffffff
efe60 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b  ff);.  DWORD rc;
efe70 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
efe80 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64  e = (winFile*)id
efe90 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b  ;.  DWORD error;
efea0 0a 20 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20  .  DWORD got;.. 
efeb0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
efec0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
efed0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
efee0 45 5f 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20  E_IOERR_READ);. 
efef0 20 4f 53 54 52 41 43 45 33 28 22 52 45 41 44 20   OSTRACE3("READ 
eff00 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70  %d lock=%d\n", p
eff10 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e  File->h, pFile->
eff20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20  locktype);.  rc 
eff30 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72  = SetFilePointer
eff40 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72  (pFile->h, lower
eff50 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73  Bits, &upperBits
eff60 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20  , FILE_BEGIN);. 
eff70 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44   if( rc==INVALID
eff80 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  _SET_FILE_POINTE
eff90 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c  R && (error=GetL
effa0 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f  astError())!=NO_
effb0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69  ERROR ){.    pFi
effc0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
effd0 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72  error;.    retur
effe0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
efff0 20 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69   }.  if( !ReadFi
f0000 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75  le(pFile->h, pBu
f0010 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29  f, amt, &got, 0)
f0020 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
f0030 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61  astErrno = GetLa
f0040 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72  stError();.    r
f0050 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
f0060 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69  RR_READ;.  }.  i
f0070 66 28 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61  f( got==(DWORD)a
f0080 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  mt ){.    return
f0090 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
f00a0 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65  lse{.    /* Unre
f00b0 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20  ad parts of the 
f00c0 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a  buffer must be z
f00d0 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20  ero-filled */.  
f00e0 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72    memset(&((char
f00f0 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c  *)pBuf)[got], 0,
f0100 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72   amt-got);.    r
f0110 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
f0120 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20  RR_SHORT_READ;. 
f0130 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74   }.}../*.** Writ
f0140 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75  e data from a bu
f0150 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65  ffer into a file
f0160 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
f0170 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a  _OK on success.*
f0180 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20  * or some other 
f0190 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61  error code on fa
f01a0 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ilure..*/.static
f01b0 20 69 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20   int winWrite(. 
f01c0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
f01d0 64 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  d,         /* Fi
f01e0 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f  le to write into
f01f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
f0200 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20   *pBuf,         
f0210 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20  /* The bytes to 
f0220 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  be written */.  
f0230 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20  int amt,        
f0240 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
f0250 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
f0260 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74  write */.  sqlit
f0270 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20  e3_int64 offset 
f0280 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
f0290 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20  nto the file to 
f02a0 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74  begin writing at
f02b0 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70   */.){.  LONG up
f02c0 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29  perBits = (LONG)
f02d0 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20  ((offset>>32) & 
f02e0 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c  0x7fffffff);.  L
f02f0 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20  ONG lowerBits = 
f0300 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20  (LONG)(offset & 
f0310 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44  0xffffffff);.  D
f0320 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69  WORD rc;.  winFi
f0330 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
f0340 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52  File*)id;.  DWOR
f0350 44 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44  D error;.  DWORD
f0360 20 77 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61   wrote = 0;..  a
f0370 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
f0380 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
f0390 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
f03a0 49 4f 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20  IOERR_WRITE);.  
f03b0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
f03c0 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c  Error(return SQL
f03d0 49 54 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54  ITE_FULL);.  OST
f03e0 52 41 43 45 33 28 22 57 52 49 54 45 20 25 64 20  RACE3("WRITE %d 
f03f0 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c  lock=%d\n", pFil
f0400 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63  e->h, pFile->loc
f0410 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53  ktype);.  rc = S
f0420 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46  etFilePointer(pF
f0430 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74  ile->h, lowerBit
f0440 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46  s, &upperBits, F
f0450 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66  ILE_BEGIN);.  if
f0460 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45  ( rc==INVALID_SE
f0470 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26  T_FILE_POINTER &
f0480 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74  & (error=GetLast
f0490 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52  Error())!=NO_ERR
f04a0 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  OR ){.    pFile-
f04b0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
f04c0 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  or;.    return S
f04d0 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a  QLITE_FULL;.  }.
f04e0 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20    assert( amt>0 
f04f0 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20  );.  while(.    
f0500 20 61 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28   amt>0.     && (
f0510 72 63 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70  rc = WriteFile(p
f0520 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61  File->h, pBuf, a
f0530 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21  mt, &wrote, 0))!
f0540 3d 30 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65  =0.     && wrote
f0550 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20  >0.  ){.    amt 
f0560 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42  -= wrote;.    pB
f0570 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42  uf = &((char*)pB
f0580 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a  uf)[wrote];.  }.
f0590 20 20 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74    if( !rc || amt
f05a0 3e 28 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20  >(int)wrote ){. 
f05b0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
f05c0 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72  rno = GetLastErr
f05d0 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  or();.    return
f05e0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20   SQLITE_FULL;.  
f05f0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
f0600 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
f0610 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20  runcate an open 
f0620 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66  file to a specif
f0630 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74  ied size.*/.stat
f0640 69 63 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61  ic int winTrunca
f0650 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
f0660 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  *id, sqlite3_int
f0670 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e  64 nByte){.  LON
f0680 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c  G upperBits = (L
f0690 4f 4e 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29  ONG)((nByte>>32)
f06a0 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a   & 0x7fffffff);.
f06b0 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73    LONG lowerBits
f06c0 20 3d 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20   = (LONG)(nByte 
f06d0 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20  & 0xffffffff);. 
f06e0 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e   DWORD rc;.  win
f06f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77  File *pFile = (w
f0700 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57  inFile*)id;.  DW
f0710 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73  ORD error;..  as
f0720 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
f0730 20 4f 53 54 52 41 43 45 33 28 22 54 52 55 4e 43   OSTRACE3("TRUNC
f0740 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20  ATE %d %lld\n", 
f0750 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29  pFile->h, nByte)
f0760 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
f0770 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
f0780 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45  E_IOERR_TRUNCATE
f0790 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c  );.  rc = SetFil
f07a0 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e  ePointer(pFile->
f07b0 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75  h, lowerBits, &u
f07c0 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42  pperBits, FILE_B
f07d0 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d  EGIN);.  if( rc=
f07e0 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c  =INVALID_SET_FIL
f07f0 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72  E_POINTER && (er
f0800 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72  ror=GetLastError
f0810 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b  ())!=NO_ERROR ){
f0820 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
f0830 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20  Errno = error;. 
f0840 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f0850 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b  _IOERR_TRUNCATE;
f0860 0a 20 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64  .  }.  /* SetEnd
f0870 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c  OfFile will fail
f0880 20 69 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67   if nByte is neg
f0890 61 74 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21  ative */.  if( !
f08a0 53 65 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69  SetEndOfFile(pFi
f08b0 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46  le->h) ){.    pF
f08c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
f08d0 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b   GetLastError();
f08e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
f08f0 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54  TE_IOERR_TRUNCAT
f0900 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  E;.  }.  return 
f0910 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69  SQLITE_OK;.}..#i
f0920 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
f0930 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65  ./*.** Count the
f0940 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73   number of fulls
f0950 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20  yncs and normal 
f0960 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20  syncs.  This is 
f0970 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20  used to test.** 
f0980 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66  that syncs and f
f0990 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63  ullsyncs are occ
f09a0 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67  uring at the rig
f09b0 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c  ht times..*/.SQL
f09c0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f09d0 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d  te3_sync_count =
f09e0 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
f09f0 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73  nt sqlite3_fulls
f0a00 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  ync_count = 0;.#
f0a10 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  endif../*.** Mak
f0a20 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65  e sure all write
f0a30 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61  s to a particula
f0a40 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69  r file are commi
f0a50 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f  tted to disk..*/
f0a60 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53  .static int winS
f0a70 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ync(sqlite3_file
f0a80 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29   *id, int flags)
f0a90 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
f0aa0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69  _NO_SYNC.  winFi
f0ab0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
f0ac0 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73  File*)id;..  ass
f0ad0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
f0ae0 4f 53 54 52 41 43 45 33 28 22 53 59 4e 43 20 25  OSTRACE3("SYNC %
f0af0 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46  d lock=%d\n", pF
f0b00 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c  ile->h, pFile->l
f0b10 6f 63 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a  ocktype);.#else.
f0b20 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
f0b30 45 52 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23  ER(id);.#endif.#
f0b40 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45  ifndef SQLITE_TE
f0b50 53 54 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ST.  UNUSED_PARA
f0b60 4d 45 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65  METER(flags);.#e
f0b70 6c 73 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20  lse.  if( flags 
f0b80 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  & SQLITE_SYNC_FU
f0b90 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  LL ){.    sqlite
f0ba0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
f0bb0 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ++;.  }.  sqlite
f0bc0 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a  3_sync_count++;.
f0bd0 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77  #endif.  /* If w
f0be0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  e compiled with 
f0bf0 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  the SQLITE_NO_SY
f0c00 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79  NC flag, then sy
f0c10 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20  ncing is a.  ** 
f0c20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65  no-op.  */.#ifde
f0c30 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  f SQLITE_NO_SYNC
f0c40 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
f0c50 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69  TE_OK;.#else.  i
f0c60 66 28 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66  f( FlushFileBuff
f0c70 65 72 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b  ers(pFile->h) ){
f0c80 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
f0c90 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
f0ca0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
f0cb0 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72  rrno = GetLastEr
f0cc0 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72  ror();.    retur
f0cd0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a  n SQLITE_IOERR;.
f0ce0 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a    }.#endif.}../*
f0cf0 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68  .** Determine th
f0d00 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  e current size o
f0d10 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65  f a file in byte
f0d20 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  s.*/.static int 
f0d30 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69  winFileSize(sqli
f0d40 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71  te3_file *id, sq
f0d50 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69  lite3_int64 *pSi
f0d60 7a 65 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70  ze){.  DWORD upp
f0d70 65 72 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20  erBits;.  DWORD 
f0d80 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e  lowerBits;.  win
f0d90 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77  File *pFile = (w
f0da0 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57  inFile*)id;.  DW
f0db0 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73  ORD error;..  as
f0dc0 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
f0dd0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
f0de0 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49  (return SQLITE_I
f0df0 4f 45 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c  OERR_FSTAT);.  l
f0e00 6f 77 65 72 42 69 74 73 20 3d 20 47 65 74 46 69  owerBits = GetFi
f0e10 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c  leSize(pFile->h,
f0e20 20 26 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20   &upperBits);.  
f0e30 69 66 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73  if(   (lowerBits
f0e40 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45   == INVALID_FILE
f0e50 5f 53 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28  _SIZE).     && (
f0e60 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74  (error = GetLast
f0e70 45 72 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45  Error()) != NO_E
f0e80 52 52 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20  RROR) ).  {.    
f0e90 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
f0ea0 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65   = error;.    re
f0eb0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
f0ec0 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a  R_FSTAT;.  }.  *
f0ed0 70 53 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74  pSize = (((sqlit
f0ee0 65 33 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69  e3_int64)upperBi
f0ef0 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72  ts)<<32) + lower
f0f00 42 69 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53  Bits;.  return S
f0f10 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
f0f20 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c  ** LOCKFILE_FAIL
f0f30 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20  _IMMEDIATELY is 
f0f40 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d  undefined on som
f0f50 65 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d  e Windows system
f0f60 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f  s..*/.#ifndef LO
f0f70 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45  CKFILE_FAIL_IMME
f0f80 44 49 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65  DIATELY.# define
f0f90 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49   LOCKFILE_FAIL_I
f0fa0 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e  MMEDIATELY 1.#en
f0fb0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69  dif../*.** Acqui
f0fc0 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b  re a reader lock
f0fd0 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41  ..** Different A
f0fe0 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  PI routines are 
f0ff0 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67  called depending
f1000 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e   on whether or n
f1010 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69  ot this.** is Wi
f1020 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f  n95 or WinNT..*/
f1030 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52  .static int getR
f1040 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20  eadLock(winFile 
f1050 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72  *pFile){.  int r
f1060 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29  es;.  if( isNT()
f1070 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50   ){.    OVERLAPP
f1080 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c  ED ovlp;.    ovl
f1090 70 2e 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45  p.Offset = SHARE
f10a0 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c  D_FIRST;.    ovl
f10b0 70 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30  p.OffsetHigh = 0
f10c0 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e  ;.    ovlp.hEven
f10d0 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d  t = 0;.    res =
f10e0 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c   LockFileEx(pFil
f10f0 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46  e->h, LOCKFILE_F
f1100 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c  AIL_IMMEDIATELY,
f1110 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
f1120 20 20 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f        0, SHARED_
f1130 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b  SIZE, 0, &ovlp);
f1140 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
f1150 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f1160 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
f1170 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
f1180 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53  cuted. .*/.#if S
f1190 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
f11a0 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  0.  }else{.    i
f11b0 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74  nt lk;.    sqlit
f11c0 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69  e3_randomness(si
f11d0 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a  zeof(lk), &lk);.
f11e0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
f11f0 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f  dLockByte = (sho
f1200 72 74 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66  rt)((lk & 0x7fff
f1210 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 49  ffff)%(SHARED_SI
f1220 5a 45 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65  ZE - 1));.    re
f1230 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69  s = LockFile(pFi
f1240 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
f1250 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65  RST+pFile->share
f1260 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c  dLockByte, 0, 1,
f1270 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a   0);.#endif.  }.
f1280 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29    if( res == 0 )
f1290 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
f12a0 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
f12b0 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72  Error();.  }.  r
f12c0 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a  eturn res;.}../*
f12d0 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c  .** Undo a readl
f12e0 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ock.*/.static in
f12f0 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b  t unlockReadLock
f1300 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29  (winFile *pFile)
f1310 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69  {.  int res;.  i
f1320 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
f1330 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c   res = UnlockFil
f1340 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  e(pFile->h, SHAR
f1350 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41  ED_FIRST, 0, SHA
f1360 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a  RED_SIZE, 0);./*
f1370 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20   isNT() is 1 if 
f1380 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
f1390 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65  =1, so this else
f13a0 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74   is never execut
f13b0 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  ed. .*/.#if SQLI
f13c0 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20  TE_OS_WINCE==0. 
f13d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20   }else{.    res 
f13e0 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69  = UnlockFile(pFi
f13f0 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
f1400 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61  RST + pFile->sha
f1410 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20  redLockByte, 0, 
f1420 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20  1, 0);.#endif.  
f1430 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30  }.  if( res == 0
f1440 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
f1450 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61  astErrno = GetLa
f1460 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20  stError();.  }. 
f1470 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a   return res;.}..
f1480 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66  /*.** Lock the f
f1490 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63  ile with the loc
f14a0 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70  k specified by p
f14b0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70  arameter locktyp
f14c0 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68  e - one.** of th
f14d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
f14e0 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45  **     (1) SHARE
f14f0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32  D_LOCK.**     (2
f1500 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a  ) RESERVED_LOCK.
f1510 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49  **     (3) PENDI
f1520 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  NG_LOCK.**     (
f1530 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  4) EXCLUSIVE_LOC
f1540 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65  K.**.** Sometime
f1550 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e  s when requestin
f1560 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65  g one lock state
f1570 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63  , additional loc
f1580 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20  k states.** are 
f1590 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77  inserted in betw
f15a0 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e  een.  The lockin
f15b0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20  g might fail on 
f15c0 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72  one of the later
f15d0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
f15e0 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b  leaving the lock
f15f0 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74   state different
f1600 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74   from what it st
f1610 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69  arted but.** sti
f1620 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20  ll short of its 
f1630 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  goal.  The follo
f1640 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73  wing chart shows
f1650 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20   the allowed.** 
f1660 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20  transitions and 
f1670 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74  the inserted int
f1680 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73  ermediate states
f1690 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43  :.**.**    UNLOC
f16a0 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a  KED -> SHARED.**
f16b0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45      SHARED -> RE
f16c0 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41  SERVED.**    SHA
f16d0 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  RED -> (PENDING)
f16e0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
f16f0 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20      RESERVED -> 
f1700 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
f1710 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e  LUSIVE.**    PEN
f1720 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56  DING -> EXCLUSIV
f1730 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  E.**.** This rou
f1740 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69  tine will only i
f1750 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20  ncrease a lock. 
f1760 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29   The winUnlock()
f1770 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73   routine.** eras
f1780 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20  es all locks at 
f1790 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73  once and returns
f17a0 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   us immediately 
f17b0 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  to locking level
f17c0 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74   0..** It is not
f17d0 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77   possible to low
f17e0 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
f17f0 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74  evel one step at
f1800 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a   a time.  You.**
f1810 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69 67 68   must go straigh
f1820 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  t to locking lev
f1830 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  el 0..*/.static 
f1840 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69  int winLock(sqli
f1850 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
f1860 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69  t locktype){.  i
f1870 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
f1880 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20  K;    /* Return 
f1890 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75  code from subrou
f18a0 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72  tines */.  int r
f18b0 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  es = 1;         
f18c0 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61    /* Result of a
f18d0 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61   windows lock ca
f18e0 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c  ll */.  int newL
f18f0 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f  ocktype;       /
f1900 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63  * Set pFile->loc
f1910 6b 74 79 70 65 20 74 6f 20 74 68 69 73 20 76 61  ktype to this va
f1920 6c 75 65 20 62 65 66 6f 72 65 20 65 78 69 74 69  lue before exiti
f1930 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50  ng */.  int gotP
f1940 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f  endingLock = 0;/
f1950 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 63 71  * True if we acq
f1960 75 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20  uired a PENDING 
f1970 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a  lock this time *
f1980 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  /.  winFile *pFi
f1990 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69  le = (winFile*)i
f19a0 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72  d;.  DWORD error
f19b0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20   = NO_ERROR;..  
f19c0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
f19d0 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43  .  OSTRACE5("LOC
f19e0 4b 20 25 64 20 25 64 20 77 61 73 20 25 64 28 25  K %d %d was %d(%
f19f0 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  d)\n",.         
f1a00 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
f1a10 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ype, pFile->lock
f1a20 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61  type, pFile->sha
f1a30 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20  redLockByte);.. 
f1a40 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
f1a50 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f  already a lock o
f1a60 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d  f this type or m
f1a70 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20  ore restrictive 
f1a80 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69  on the.  ** OsFi
f1a90 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20  le, do nothing. 
f1aa0 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e  Don't use the en
f1ab0 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74  d_lock: exit pat
f1ac0 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74  h, as.  ** sqlit
f1ad0 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29  e3OsEnterMutex()
f1ae0 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c   hasn't been cal
f1af0 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20  led yet..  */.  
f1b00 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
f1b10 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype>=locktype ){
f1b20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
f1b30 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
f1b40 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c   Make sure the l
f1b50 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20  ocking sequence 
f1b60 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a  is correct.  */.
f1b70 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
f1b80 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f  >locktype!=NO_LO
f1b90 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d  CK || locktype==
f1ba0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
f1bb0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
f1bc0 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  e!=PENDING_LOCK 
f1bd0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63  );.  assert( loc
f1be0 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f  ktype!=RESERVED_
f1bf0 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c  LOCK || pFile->l
f1c00 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
f1c10 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f  LOCK );..  /* Lo
f1c20 63 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c  ck the PENDING_L
f1c30 4f 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e  OCK byte if we n
f1c40 65 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61  eed to acquire a
f1c50 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72   PENDING lock or
f1c60 0a 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c  .  ** a SHARED l
f1c70 6f 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20  ock.  If we are 
f1c80 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52  acquiring a SHAR
f1c90 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71  ED lock, the acq
f1ca0 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a  uisition of.  **
f1cb0 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   the PENDING_LOC
f1cc0 4b 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72  K byte is tempor
f1cd0 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c  ary..  */.  newL
f1ce0 6f 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d  ocktype = pFile-
f1cf0 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28  >locktype;.  if(
f1d00 20 20 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74     (pFile->lockt
f1d10 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20  ype==NO_LOCK).  
f1d20 20 20 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74     || (   (lockt
f1d30 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
f1d40 4f 43 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26  OCK).         &&
f1d50 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
f1d60 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e==RESERVED_LOCK
f1d70 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20  )).  ){.    int 
f1d80 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69  cnt = 3;.    whi
f1d90 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28  le( cnt-->0 && (
f1da0 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70  res = LockFile(p
f1db0 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
f1dc0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29  _BYTE, 0, 1, 0))
f1dd0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
f1de0 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67  Try 3 times to g
f1df0 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c  et the pending l
f1e00 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e  ock.  The pendin
f1e10 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a  g lock might be.
f1e20 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79        ** held by
f1e30 20 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20   another reader 
f1e40 70 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c  process who will
f1e50 20 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65   release it mome
f1e60 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a  ntarily..      *
f1e70 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  /.      OSTRACE2
f1e80 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20  ("could not get 
f1e90 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20  a PENDING lock. 
f1ea0 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b  cnt=%d\n", cnt);
f1eb0 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b  .      Sleep(1);
f1ec0 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65  .    }.    gotPe
f1ed0 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b  ndingLock = res;
f1ee0 0a 20 20 20 20 69 66 28 20 21 72 65 73 20 29 7b  .    if( !res ){
f1ef0 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47  .      error = G
f1f00 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
f1f10 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
f1f20 63 71 75 69 72 65 20 61 20 73 68 61 72 65 64 20  cquire a shared 
f1f30 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
f1f40 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
f1f50 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
f1f60 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
f1f70 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  e->locktype==NO_
f1f80 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20  LOCK );.    res 
f1f90 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46  = getReadLock(pF
f1fa0 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65  ile);.    if( re
f1fb0 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f  s ){.      newLo
f1fc0 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f  cktype = SHARED_
f1fd0 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LOCK;.    }else{
f1fe0 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47  .      error = G
f1ff0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
f2000 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
f2010 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45  cquire a RESERVE
f2020 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  D lock.  */.  if
f2030 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45  ( locktype==RESE
f2040 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73  RVED_LOCK && res
f2050 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
f2060 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
f2070 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
f2080 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69      res = LockFi
f2090 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53  le(pFile->h, RES
f20a0 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31  ERVED_BYTE, 0, 1
f20b0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65  , 0);.    if( re
f20c0 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f  s ){.      newLo
f20d0 63 6b 74 79 70 65 20 3d 20 52 45 53 45 52 56 45  cktype = RESERVE
f20e0 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73  D_LOCK;.    }els
f20f0 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d  e{.      error =
f2100 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b   GetLastError();
f2110 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
f2120 20 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49   Acquire a PENDI
f2130 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69  NG lock.  */.  i
f2140 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  f( locktype==EXC
f2150 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72  LUSIVE_LOCK && r
f2160 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63  es ){.    newLoc
f2170 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f  ktype = PENDING_
f2180 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e  LOCK;.    gotPen
f2190 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  dingLock = 0;.  
f21a0 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
f21b0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
f21c0 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
f21d0 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
f21e0 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
f21f0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
f2200 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41  e->locktype>=SHA
f2210 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
f2220 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64  res = unlockRead
f2230 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
f2240 20 4f 53 54 52 41 43 45 32 28 22 75 6e 72 65 61   OSTRACE2("unrea
f2250 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72  dlock = %d\n", r
f2260 65 73 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c  es);.    res = L
f2270 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
f2280 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20  , SHARED_FIRST, 
f2290 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20  0, SHARED_SIZE, 
f22a0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20  0);.    if( res 
f22b0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
f22c0 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45  type = EXCLUSIVE
f22d0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  _LOCK;.    }else
f22e0 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20  {.      error = 
f22f0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a  GetLastError();.
f2300 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22        OSTRACE2("
f2310 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c  error-code = %d\
f2320 6e 22 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20  n", error);.    
f2330 20 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46    getReadLock(pF
f2340 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ile);.    }.  }.
f2350 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
f2360 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e  holding a PENDIN
f2370 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68  G lock that ough
f2380 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64  t to be released
f2390 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65  , then.  ** rele
f23a0 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f  ase it now..  */
f23b0 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e  .  if( gotPendin
f23c0 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70  gLock && locktyp
f23d0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
f23e0 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65  {.    UnlockFile
f23f0 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49  (pFile->h, PENDI
f2400 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30  NG_BYTE, 0, 1, 0
f2410 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64  );.  }..  /* Upd
f2420 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66  ate the state of
f2430 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65   the lock has he
f2440 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64  ld in the file d
f2450 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20  escriptor then. 
f2460 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61   ** return the a
f2470 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c  ppropriate resul
f2480 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69  t code..  */.  i
f2490 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63  f( res ){.    rc
f24a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
f24b0 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41  }else{.    OSTRA
f24c0 43 45 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44  CE4("LOCK FAILED
f24d0 20 25 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25   %d trying for %
f24e0 64 20 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c  d but got %d\n",
f24f0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20   pFile->h,.     
f2500 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20        locktype, 
f2510 6e 65 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20  newLocktype);.  
f2520 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
f2530 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20  no = error;.    
f2540 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
f2550 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c  ;.  }.  pFile->l
f2560 6f 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65  ocktype = (u8)ne
f2570 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74  wLocktype;.  ret
f2580 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
f2590 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
f25a0 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73  ecks if there is
f25b0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
f25c0 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65   held on the spe
f25d0 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62  cified.** file b
f25e0 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74  y this or any ot
f25f0 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20  her process. If 
f2600 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68  such a lock is h
f2610 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e  eld, return.** n
f2620 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69  on-zero, otherwi
f2630 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74  se zero..*/.stat
f2640 69 63 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52  ic int winCheckR
f2650 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
f2660 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
f2670 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69  t *pResOut){.  i
f2680 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65  nt rc;.  winFile
f2690 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
f26a0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72  le*)id;..  asser
f26b0 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66  t( id!=0 );.  if
f26c0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
f26d0 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
f26e0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a   ){.    rc = 1;.
f26f0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45      OSTRACE3("TE
f2700 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
f2710 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69   (local)\n", pFi
f2720 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65  le->h, rc);.  }e
f2730 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f  lse{.    rc = Lo
f2740 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
f2750 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
f2760 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 1, 0);.    if
f2770 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e  ( rc ){.      Un
f2780 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  lockFile(pFile->
f2790 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  h, RESERVED_BYTE
f27a0 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  , 0, 1, 0);.    
f27b0 7d 0a 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a  }.    rc = !rc;.
f27c0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45      OSTRACE3("TE
f27d0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
f27e0 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46   (remote)\n", pF
f27f0 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d  ile->h, rc);.  }
f2800 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63  .  *pResOut = rc
f2810 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
f2820 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  E_OK;.}../*.** L
f2830 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
f2840 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
f2850 65 73 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20  escriptor id to 
f2860 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74  locktype.  lockt
f2870 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65  ype.** must be e
f2880 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72  ither NO_LOCK or
f2890 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a   SHARED_LOCK..**
f28a0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69  .** If the locki
f28b0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  ng level of the 
f28c0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
f28d0 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72  is already at or
f28e0 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65   below.** the re
f28f0 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20  quested locking 
f2900 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74  level, this rout
f2910 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
f2920 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20  **.** It is not 
f2930 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69  possible for thi
f2940 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69  s routine to fai
f2950 6c 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20  l if the second 
f2960 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e  argument.** is N
f2970 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20  O_LOCK.  If the 
f2980 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
f2990 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74  is SHARED_LOCK t
f29a0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
f29b0 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e  .** might return
f29c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a   SQLITE_IOERR;.*
f29d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
f29e0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
f29f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
f2a00 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79  ktype){.  int ty
f2a10 70 65 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  pe;.  winFile *p
f2a20 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
f2a30 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  )id;.  int rc = 
f2a40 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73  SQLITE_OK;.  ass
f2a50 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b  ert( pFile!=0 );
f2a60 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
f2a70 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
f2a80 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22   );.  OSTRACE5("
f2a90 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20  UNLOCK %d to %d 
f2aa0 77 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70  was %d(%d)\n", p
f2ab0 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
f2ac0 65 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  e,.          pFi
f2ad0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46  le->locktype, pF
f2ae0 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42  ile->sharedLockB
f2af0 79 74 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70  yte);.  type = p
f2b00 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a  File->locktype;.
f2b10 20 20 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c    if( type>=EXCL
f2b20 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20  USIVE_LOCK ){.  
f2b30 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69    UnlockFile(pFi
f2b40 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
f2b50 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53  RST, 0, SHARED_S
f2b60 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  IZE, 0);.    if(
f2b70 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
f2b80 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65  D_LOCK && !getRe
f2b90 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b  adLock(pFile) ){
f2ba0 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73  .      /* This s
f2bb0 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70  hould never happ
f2bc0 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61  en.  We should a
f2bd0 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f  lways be able to
f2be0 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75  .      ** reacqu
f2bf0 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63  ire the read loc
f2c00 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  k */.      rc = 
f2c10 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
f2c20 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  OCK;.    }.  }. 
f2c30 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52   if( type>=RESER
f2c40 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  VED_LOCK ){.    
f2c50 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  UnlockFile(pFile
f2c60 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59  ->h, RESERVED_BY
f2c70 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  TE, 0, 1, 0);.  
f2c80 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  }.  if( locktype
f2c90 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70  ==NO_LOCK && typ
f2ca0 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e>=SHARED_LOCK )
f2cb0 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64  {.    unlockRead
f2cc0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d  Lock(pFile);.  }
f2cd0 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e  .  if( type>=PEN
f2ce0 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  DING_LOCK ){.   
f2cf0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   UnlockFile(pFil
f2d00 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59  e->h, PENDING_BY
f2d10 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  TE, 0, 1, 0);.  
f2d20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  }.  pFile->lockt
f2d30 79 70 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79  ype = (u8)lockty
f2d40 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  pe;.  return rc;
f2d50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f  .}../*.** Contro
f2d60 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74  l and query of t
f2d70 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e  he open file han
f2d80 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  dle..*/.static i
f2d90 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f  nt winFileContro
f2da0 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  l(sqlite3_file *
f2db0 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64  id, int op, void
f2dc0 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63   *pArg){.  switc
f2dd0 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  h( op ){.    cas
f2de0 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c  e SQLITE_FCNTL_L
f2df0 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20  OCKSTATE: {.    
f2e00 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20    *(int*)pArg = 
f2e10 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e  ((winFile*)id)->
f2e20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20  locktype;.      
f2e30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f2e40 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
f2e50 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52   SQLITE_LAST_ERR
f2e60 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e  NO: {.      *(in
f2e70 74 2a 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28  t*)pArg = (int)(
f2e80 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  (winFile*)id)->l
f2e90 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  astErrno;.      
f2ea0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f2eb0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
f2ec0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
f2ed0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  R;.}../*.** Retu
f2ee0 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  rn the sector si
f2ef0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
f2f00 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c  he underlying bl
f2f10 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a  ock device for.*
f2f20 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  * the specified 
f2f30 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c  file. This is al
f2f40 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20  most always 512 
f2f50 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62  bytes, but may b
f2f60 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20  e.** larger for 
f2f70 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a  some devices..**
f2f80 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20  .** SQLite code 
f2f90 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e  assumes this fun
f2fa0 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69  ction cannot fai
f2fb0 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d  l. It also assum
f2fc0 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77  es that.** if tw
f2fd0 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61  o files are crea
f2fe0 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ted in the same 
f2ff0 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65  file-system dire
f3000 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61  ctory (i.e..** a
f3010 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74   database and it
f3020 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20  s journal file) 
f3030 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20  that the sector 
f3040 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65  size will be the
f3050 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74  .** same for bot
f3060 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h..*/.static int
f3070 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73   winSectorSize(s
f3080 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
f3090 7b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  {.  assert( id!=
f30a0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69  0 );.  return (i
f30b0 6e 74 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69  nt)(((winFile*)i
f30c0 64 29 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b  d)->sectorSize);
f30d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
f30e0 20 61 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76   a vector of dev
f30f0 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74  ice characterist
f3100 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ics..*/.static i
f3110 6e 74 20 77 69 6e 44 65 76 69 63 65 43 68 61 72  nt winDeviceChar
f3120 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69  acteristics(sqli
f3130 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
f3140 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f3150 52 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  R(id);.  return 
f3160 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  0;.}../*.** This
f3170 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20   vector defines 
f3180 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20  all the methods 
f3190 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65  that can operate
f31a0 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65   on an.** sqlite
f31b0 33 5f 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32  3_file for win32
f31c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
f31d0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
f31e0 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64  hods winIoMethod
f31f0 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20   = {.  1,       
f3200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3210 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
f3220 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69    winClose,.  wi
f3230 6e 52 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74  nRead,.  winWrit
f3240 65 2c 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65  e,.  winTruncate
f3250 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77  ,.  winSync,.  w
f3260 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69  inFileSize,.  wi
f3270 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f  nLock,.  winUnlo
f3280 63 6b 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65  ck,.  winCheckRe
f3290 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69  servedLock,.  wi
f32a0 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20  nFileControl,.  
f32b0 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20  winSectorSize,. 
f32c0 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63   winDeviceCharac
f32d0 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a  teristics.};../*
f32e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f32f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65  **********.** He
f3330 72 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20  re ends the I/O 
f3340 6d 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72  methods that for
f3350 6d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f  m the sqlite3_io
f3360 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e  _methods object.
f3370 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  .**.** The next 
f3380 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d  block of code im
f3390 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53  plements the VFS
f33a0 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a   methods..******
f33b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f33c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f33d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f33e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f33f0 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  ******/../*.** C
f3400 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 66  onvert a UTF-8 f
f3410 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61  ilename into wha
f3420 74 65 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75  tever form the u
f3430 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65  nderlying.** ope
f3440 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61  rating system wa
f3450 6e 74 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e  nts filenames in
f3460 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  .  Space to hold
f3470 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69   the result.** i
f3480 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
f3490 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20  malloc and must 
f34a0 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20  be freed by the 
f34b0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74  calling.** funct
f34c0 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
f34d0 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38  oid *convertUtf8
f34e0 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63  Filename(const c
f34f0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
f3500 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72  .  void *zConver
f3510 74 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69  ted = 0;.  if( i
f3520 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f  sNT() ){.    zCo
f3530 6e 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f  nverted = utf8To
f3540 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d  Unicode(zFilenam
f3550 65 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73  e);./* isNT() is
f3560 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
f3570 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
f3580 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
f3590 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69  executed. .*/.#i
f35a0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
f35b0 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  E==0.  }else{.  
f35c0 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75    zConverted = u
f35d0 74 66 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e  tf8ToMbcs(zFilen
f35e0 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  ame);.#endif.  }
f35f0 0a 20 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c  .  /* caller wil
f3600 6c 20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20  l handle out of 
f3610 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75  memory */.  retu
f3620 72 6e 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d  rn zConverted;.}
f3630 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
f3640 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
f3650 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a  name in zBuf.  z
f3660 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20  Buf must be big 
f3670 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c  enough to.** hol
f3680 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74  d at pVfs->mxPat
f3690 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73  hname characters
f36a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f36b0 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20  getTempname(int 
f36c0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
f36d0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 68 61 72  ){.  static char
f36e0 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20   zChars[] =.    
f36f0 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f  "abcdefghijklmno
f3700 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20  pqrstuvwxyz".   
f3710 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e   "ABCDEFGHIJKLMN
f3720 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20  OPQRSTUVWXYZ".  
f3730 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a    "0123456789";.
f3740 20 20 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20    size_t i, j;. 
f3750 20 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b   char zTempPath[
f3760 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69  MAX_PATH+1];.  i
f3770 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  f( sqlite3_temp_
f3780 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20  directory ){.   
f3790 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
f37a0 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a  f(MAX_PATH-30, z
f37b0 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20  TempPath, "%s", 
f37c0 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
f37d0 65 63 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65  ectory);.  }else
f37e0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
f37f0 20 20 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b     char *zMulti;
f3800 0a 20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65  .    WCHAR zWide
f3810 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a  Path[MAX_PATH];.
f3820 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 57      GetTempPathW
f3830 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57  (MAX_PATH-30, zW
f3840 69 64 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d  idePath);.    zM
f3850 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f  ulti = unicodeTo
f3860 55 74 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b  Utf8(zWidePath);
f3870 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20  .    if( zMulti 
f3880 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
f3890 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41  _snprintf(MAX_PA
f38a0 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68  TH-30, zTempPath
f38b0 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b  , "%s", zMulti);
f38c0 0a 20 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c  .      free(zMul
f38d0 74 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ti);.    }else{.
f38e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
f38f0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
f3900 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
f3910 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f3920 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
f3930 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
f3940 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65  cuted. .** Since
f3950 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69   the ASCII versi
f3960 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64  on of these Wind
f3970 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65  ows API do not e
f3980 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a  xist for WINCE,.
f3990 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e  ** it's importan
f39a0 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e  t to not referen
f39b0 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43  ce them for WINC
f39c0 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66  E builds..*/.#if
f39d0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
f39e0 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
f39f0 20 63 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20   char *zUtf8;.  
f3a00 20 20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68    char zMbcsPath
f3a10 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20  [MAX_PATH];.    
f3a20 47 65 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58  GetTempPathA(MAX
f3a30 5f 50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50  _PATH-30, zMbcsP
f3a40 61 74 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20  ath);.    zUtf8 
f3a50 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f  = sqlite3_win32_
f3a60 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62  mbcs_to_utf8(zMb
f3a70 63 73 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28  csPath);.    if(
f3a80 20 7a 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20   zUtf8 ){.      
f3a90 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
f3aa0 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54  (MAX_PATH-30, zT
f3ab0 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a  empPath, "%s", z
f3ac0 55 74 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65  Utf8);.      fre
f3ad0 65 28 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65  e(zUtf8);.    }e
f3ae0 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
f3af0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
f3b00 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
f3b10 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33  .  for(i=sqlite3
f3b20 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61  Strlen30(zTempPa
f3b30 74 68 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d  th); i>0 && zTem
f3b40 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27  pPath[i-1]=='\\'
f3b50 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70  ; i--){}.  zTemp
f3b60 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73  Path[i] = 0;.  s
f3b70 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
f3b80 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20  nBuf-30, zBuf,. 
f3b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3ba0 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54    "%s\\"SQLITE_T
f3bb0 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c  EMP_FILE_PREFIX,
f3bc0 20 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a   zTempPath);.  j
f3bd0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
f3be0 33 30 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69  30(zBuf);.  sqli
f3bf0 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32  te3_randomness(2
f3c00 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20  0, &zBuf[j]);.  
f3c10 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69  for(i=0; i<20; i
f3c20 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42  ++, j++){.    zB
f3c30 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43  uf[j] = (char)zC
f3c40 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64  hars[ ((unsigned
f3c50 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28   char)zBuf[j])%(
f3c60 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31  sizeof(zChars)-1
f3c70 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b  ) ];.  }.  zBuf[
f3c80 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43  j] = 0;.  OSTRAC
f3c90 45 32 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d  E2("TEMP FILENAM
f3ca0 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b  E: %s\n", zBuf);
f3cb0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
f3cc0 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  _OK; .}../*.** T
f3cd0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
f3ce0 6f 66 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d  of getLastErrorM
f3cf0 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66  sg.** is zero if
f3d00 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
f3d10 67 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62  ge fits in the b
f3d20 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65  uffer, or non-ze
f3d30 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20  ro.** otherwise 
f3d40 28 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20  (if the message 
f3d50 77 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a  was truncated)..
f3d60 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
f3d70 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e  tLastErrorMsg(in
f3d80 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
f3d90 75 66 29 7b 0a 20 20 44 57 4f 52 44 20 65 72 72  uf){.  DWORD err
f3da0 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  or = GetLastErro
f3db0 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45  r();..#if SQLITE
f3dc0 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73 71 6c 69  _OS_WINCE.  sqli
f3dd0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
f3de0 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f  f, zBuf, "OsErro
f3df0 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72  r 0x%x (%u)", er
f3e00 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 23 65 6c  ror, error);.#el
f3e10 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65  se.  /* FormatMe
f3e20 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30 20  ssage returns 0 
f3e30 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68  on failure.  Oth
f3e40 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72  erwise it.  ** r
f3e50 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
f3e60 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69 74  r of TCHARs writ
f3e70 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ten to the outpu
f3e80 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65  t.  ** buffer, e
f3e90 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72  xcluding the ter
f3ea0 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68  minating null ch
f3eb0 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 20 28 21  ar..  */.  if (!
f3ec0 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 28 46  FormatMessageA(F
f3ed0 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52  ORMAT_MESSAGE_FR
f3ee0 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20 20 20 20  OM_SYSTEM,.     
f3ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3f00 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20   NULL,.         
f3f10 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72               err
f3f20 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  or,.            
f3f30 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20            0,.   
f3f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3f50 20 20 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20     zBuf,.       
f3f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e                 n
f3f70 42 75 66 2d 31 2c 0a 20 20 20 20 20 20 20 20 20  Buf-1,.         
f3f80 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29 29               0))
f3f90 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  .  {.    sqlite3
f3fa0 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20  _snprintf(nBuf, 
f3fb0 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30  zBuf, "OsError 0
f3fc0 78 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72  x%x (%u)", error
f3fd0 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d 0a 23 65  , error);.  }.#e
f3fe0 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 30  ndif..  return 0
f3ff0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
f4000 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
f4010 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e 28 0a 20  c int winOpen(. 
f4020 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
f4030 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  fs,        /* No
f4040 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  t used */.  cons
f4050 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
f4060 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
f4070 20 74 68 65 20 66 69 6c 65 20 28 55 54 46 2d 38   the file (UTF-8
f4080 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  ) */.  sqlite3_f
f4090 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20  ile *id,        
f40a0 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 53 51   /* Write the SQ
f40b0 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65  Lite file handle
f40c0 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66   here */.  int f
f40d0 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
f40e0 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64       /* Open mod
f40f0 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74  e flags */.  int
f4100 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20   *pOutFlags     
f4110 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73         /* Status
f4120 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f   return flags */
f4130 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a  .){.  HANDLE h;.
f4140 20 20 44 57 4f 52 44 20 64 77 44 65 73 69 72 65    DWORD dwDesire
f4150 64 41 63 63 65 73 73 3b 0a 20 20 44 57 4f 52 44  dAccess;.  DWORD
f4160 20 64 77 53 68 61 72 65 4d 6f 64 65 3b 0a 20 20   dwShareMode;.  
f4170 44 57 4f 52 44 20 64 77 43 72 65 61 74 69 6f 6e  DWORD dwCreation
f4180 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a 20 20 44  Disposition;.  D
f4190 57 4f 52 44 20 64 77 46 6c 61 67 73 41 6e 64 41  WORD dwFlagsAndA
f41a0 74 74 72 69 62 75 74 65 73 20 3d 20 30 3b 0a 23  ttributes = 0;.#
f41b0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f41c0 43 45 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 20  CE.  int isTemp 
f41d0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 77 69  = 0;.#endif.  wi
f41e0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
f41f0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 76  winFile*)id;.  v
f4200 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b  oid *zConverted;
f4210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4220 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20   /* Filename in 
f4230 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  OS encoding */. 
f4240 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 74   const char *zUt
f4250 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20  f8Name = zName; 
f4260 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69     /* Filename i
f4270 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67  n UTF-8 encoding
f4280 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e   */.  char zTmpn
f4290 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b  ame[MAX_PATH+1];
f42a0 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65          /* Buffe
f42b0 72 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  r used to create
f42c0 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d 65 20 2a   temp filename *
f42d0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21  /..  assert( id!
f42e0 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =0 );.  UNUSED_P
f42f0 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a  ARAMETER(pVfs);.
f4300 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63  .  /* If the sec
f4310 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
f4320 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
f4330 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20   NULL, generate 
f4340 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72  a .  ** temporar
f4350 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75  y file name to u
f4360 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  se .  */.  if( !
f4370 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20  zUtf8Name ){.   
f4380 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d   int rc = getTem
f4390 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b 31  pname(MAX_PATH+1
f43a0 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20  , zTmpname);.   
f43b0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
f43c0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
f43d0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
f43e0 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54 6d   zUtf8Name = zTm
f43f0 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  pname;.  }..  /*
f4400 20 43 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c   Convert the fil
f4410 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73  ename to the sys
f4420 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f  tem encoding. */
f4430 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  .  zConverted = 
f4440 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
f4450 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a  ame(zUtf8Name);.
f4460 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64    if( zConverted
f4470 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f4480 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
f4490 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73    }..  if( flags
f44a0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
f44b0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20  EADWRITE ){.    
f44c0 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20  dwDesiredAccess 
f44d0 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 20 7c  = GENERIC_READ |
f44e0 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 3b 0a   GENERIC_WRITE;.
f44f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 44    }else{.    dwD
f4500 65 73 69 72 65 64 41 63 63 65 73 73 20 3d 20 47  esiredAccess = G
f4510 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a 20 20 7d  ENERIC_READ;.  }
f4520 0a 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 50 45  .  /* SQLITE_OPE
f4530 4e 5f 45 58 43 4c 55 53 49 56 45 20 69 73 20 75  N_EXCLUSIVE is u
f4540 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  sed to make sure
f4550 20 74 68 61 74 20 61 20 6e 65 77 20 66 69 6c 65   that a new file
f4560 20 69 73 20 0a 20 20 2a 2a 20 63 72 65 61 74 65   is .  ** create
f4570 64 2e 20 53 51 4c 69 74 65 20 64 6f 65 73 6e 27  d. SQLite doesn'
f4580 74 20 75 73 65 20 69 74 20 74 6f 20 69 6e 64 69  t use it to indi
f4590 63 61 74 65 20 22 65 78 63 6c 75 73 69 76 65 20  cate "exclusive 
f45a0 61 63 63 65 73 73 22 20 0a 20 20 2a 2a 20 61 73  access" .  ** as
f45b0 20 69 74 20 69 73 20 75 73 75 61 6c 6c 79 20 75   it is usually u
f45c0 6e 64 65 72 73 74 6f 6f 64 2e 0a 20 20 2a 2f 0a  nderstood..  */.
f45d0 20 20 61 73 73 65 72 74 28 21 28 66 6c 61 67 73    assert(!(flags
f45e0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45   & SQLITE_OPEN_E
f45f0 58 43 4c 55 53 49 56 45 29 20 7c 7c 20 28 66 6c  XCLUSIVE) || (fl
f4600 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
f4610 4e 5f 43 52 45 41 54 45 29 29 3b 0a 20 20 69 66  N_CREATE));.  if
f4620 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
f4630 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20  _OPEN_EXCLUSIVE 
f4640 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65  ){.    /* Create
f4650 73 20 61 20 6e 65 77 20 66 69 6c 65 2c 20 6f 6e  s a new file, on
f4660 6c 79 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f  ly if it does no
f4670 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e  t already exist.
f4680 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   */.    /* If th
f4690 65 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 69  e file exists, i
f46a0 74 20 66 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20  t fails. */.    
f46b0 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73  dwCreationDispos
f46c0 69 74 69 6f 6e 20 3d 20 43 52 45 41 54 45 5f 4e  ition = CREATE_N
f46d0 45 57 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  EW;.  }else if( 
f46e0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
f46f0 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 20  PEN_CREATE ){.  
f4700 20 20 2f 2a 20 4f 70 65 6e 20 65 78 69 73 74 69    /* Open existi
f4710 6e 67 20 66 69 6c 65 2c 20 6f 72 20 63 72 65 61  ng file, or crea
f4720 74 65 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74  te if it doesn't
f4730 20 65 78 69 73 74 20 2a 2f 0a 20 20 20 20 64 77   exist */.    dw
f4740 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
f4750 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57 41 59  ion = OPEN_ALWAY
f4760 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  S;.  }else{.    
f4770 2f 2a 20 4f 70 65 6e 73 20 61 20 66 69 6c 65 2c  /* Opens a file,
f4780 20 6f 6e 6c 79 20 69 66 20 69 74 20 65 78 69 73   only if it exis
f4790 74 73 2e 20 2a 2f 0a 20 20 20 20 64 77 43 72 65  ts. */.    dwCre
f47a0 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e  ationDisposition
f47b0 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 49 4e 47   = OPEN_EXISTING
f47c0 3b 0a 20 20 7d 0a 20 20 64 77 53 68 61 72 65 4d  ;.  }.  dwShareM
f47d0 6f 64 65 20 3d 20 46 49 4c 45 5f 53 48 41 52 45  ode = FILE_SHARE
f47e0 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f 53 48 41  _READ | FILE_SHA
f47f0 52 45 5f 57 52 49 54 45 3b 0a 20 20 69 66 28 20  RE_WRITE;.  if( 
f4800 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
f4810 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
f4820 45 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  E ){.#if SQLITE_
f4830 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46  OS_WINCE.    dwF
f4840 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
f4850 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55  s = FILE_ATTRIBU
f4860 54 45 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69  TE_HIDDEN;.    i
f4870 73 54 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65  sTemp = 1;.#else
f4880 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41  .    dwFlagsAndA
f4890 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45  ttributes = FILE
f48a0 5f 41 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f  _ATTRIBUTE_TEMPO
f48b0 52 41 52 59 0a 20 20 20 20 20 20 20 20 20 20 20  RARY.           
f48c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f48d0 20 20 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49      | FILE_ATTRI
f48e0 42 55 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20  BUTE_HIDDEN.    
f48f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4900 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c             | FIL
f4910 45 5f 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e  E_FLAG_DELETE_ON
f4920 5f 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20  _CLOSE;.#endif. 
f4930 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c   }else{.    dwFl
f4940 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73  agsAndAttributes
f4950 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54   = FILE_ATTRIBUT
f4960 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20  E_NORMAL;.  }.  
f4970 2f 2a 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20  /* Reports from 
f4980 74 68 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65  the internet are
f4990 20 74 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63   that performanc
f49a0 65 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a  e is always.  **
f49b0 20 62 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f   better if FILE_
f49c0 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45  FLAG_RANDOM_ACCE
f49d0 53 53 20 69 73 20 75 73 65 64 2e 20 20 54 69 63  SS is used.  Tic
f49e0 6b 65 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69  ket #2699. */.#i
f49f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
f4a00 45 0a 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74  E.  dwFlagsAndAt
f4a10 74 72 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45  tributes |= FILE
f4a20 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43  _FLAG_RANDOM_ACC
f4a30 45 53 53 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66  ESS;.#endif.  if
f4a40 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
f4a50 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 57 28  h = CreateFileW(
f4a60 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74  (WCHAR*)zConvert
f4a70 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73  ed,.       dwDes
f4a80 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20  iredAccess,.    
f4a90 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a     dwShareMode,.
f4aa0 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20         NULL,.   
f4ab0 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69      dwCreationDi
f4ac0 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20  sposition,.     
f4ad0 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72    dwFlagsAndAttr
f4ae0 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e  ibutes,.       N
f4af0 55 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20 69 73  ULL.    );./* is
f4b00 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
f4b10 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
f4b20 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
f4b30 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
f4b40 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41   .** Since the A
f4b50 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20  SCII version of 
f4b60 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50  these Windows AP
f4b70 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66  I do not exist f
f4b80 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27  or WINCE,.** it'
f4b90 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e  s important to n
f4ba0 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  ot reference the
f4bb0 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c  m for WINCE buil
f4bc0 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ds..*/.#if SQLIT
f4bd0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20  E_OS_WINCE==0.  
f4be0 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 43  }else{.    h = C
f4bf0 72 65 61 74 65 46 69 6c 65 41 28 28 63 68 61 72  reateFileA((char
f4c00 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20  *)zConverted,.  
f4c10 20 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63       dwDesiredAc
f4c20 63 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53  cess,.       dwS
f4c30 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20  hareMode,.      
f4c40 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77   NULL,.       dw
f4c50 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
f4c60 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c  ion,.       dwFl
f4c70 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73  agsAndAttributes
f4c80 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20  ,.       NULL.  
f4c90 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a    );.#endif.  }.
f4ca0 20 20 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49 44    if( h==INVALID
f4cb0 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 7b  _HANDLE_VALUE ){
f4cc0 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65  .    free(zConve
f4cd0 72 74 65 64 29 3b 0a 20 20 20 20 69 66 28 20 66  rted);.    if( f
f4ce0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
f4cf0 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a  EN_READWRITE ){.
f4d00 20 20 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e        return win
f4d10 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d 65  Open(pVfs, zName
f4d20 2c 20 69 64 2c 20 0a 20 20 20 20 20 20 20 20 20  , id, .         
f4d30 20 20 20 20 28 28 66 6c 61 67 73 7c 53 51 4c 49      ((flags|SQLI
f4d40 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
f4d50 29 26 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  )&~SQLITE_OPEN_R
f4d60 45 41 44 57 52 49 54 45 29 2c 20 70 4f 75 74 46  EADWRITE), pOutF
f4d70 6c 61 67 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65  lags);.    }else
f4d80 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
f4d90 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
f4da0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
f4db0 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20  pOutFlags ){.   
f4dc0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
f4dd0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
f4de0 54 45 20 29 7b 0a 20 20 20 20 20 20 2a 70 4f 75  TE ){.      *pOu
f4df0 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f  tFlags = SQLITE_
f4e00 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 3b 0a  OPEN_READWRITE;.
f4e10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
f4e20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51   *pOutFlags = SQ
f4e30 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
f4e40 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  LY;.    }.  }.  
f4e50 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c  memset(pFile, 0,
f4e60 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 29   sizeof(*pFile))
f4e70 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68  ;.  pFile->pMeth
f4e80 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65 74 68 6f  od = &winIoMetho
f4e90 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20  d;.  pFile->h = 
f4ea0 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  h;.  pFile->last
f4eb0 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52 52 4f 52  Errno = NO_ERROR
f4ec0 3b 0a 20 20 70 46 69 6c 65 2d 3e 73 65 63 74 6f  ;.  pFile->secto
f4ed0 72 53 69 7a 65 20 3d 20 67 65 74 53 65 63 74 6f  rSize = getSecto
f4ee0 72 53 69 7a 65 28 70 56 66 73 2c 20 7a 55 74 66  rSize(pVfs, zUtf
f4ef0 38 4e 61 6d 65 29 3b 0a 23 69 66 20 53 51 4c 49  8Name);.#if SQLI
f4f00 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 66  TE_OS_WINCE.  if
f4f10 28 20 28 66 6c 61 67 73 20 26 20 28 53 51 4c 49  ( (flags & (SQLI
f4f20 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
f4f30 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  E|SQLITE_OPEN_MA
f4f40 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20 20 20 20  IN_DB)) ==.     
f4f50 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
f4f60 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
f4f70 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  |SQLITE_OPEN_MAI
f4f80 4e 5f 44 42 29 0a 20 20 20 20 20 20 20 26 26 20  N_DB).       && 
f4f90 21 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b  !winceCreateLock
f4fa0 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 29 0a 20  (zName, pFile). 
f4fb0 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e   ){.    CloseHan
f4fc0 64 6c 65 28 68 29 3b 0a 20 20 20 20 66 72 65 65  dle(h);.    free
f4fd0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
f4fe0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f4ff0 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20  CANTOPEN;.  }.  
f5000 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a 20 20  if( isTemp ){.  
f5010 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65    pFile->zDelete
f5020 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e 76 65  OnClose = zConve
f5030 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  rted;.  }else.#e
f5040 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 66 72 65  ndif.  {.    fre
f5050 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
f5060 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72   }.  OpenCounter
f5070 28 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  (+1);.  return S
f5080 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
f5090 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6e 61  ** Delete the na
f50a0 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  med file..**.** 
f50b0 4e 6f 74 65 20 74 68 61 74 20 77 69 6e 64 6f 77  Note that window
f50c0 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77  s does not allow
f50d0 20 61 20 66 69 6c 65 20 74 6f 20 62 65 20 64 65   a file to be de
f50e0 6c 65 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74  leted if some ot
f50f0 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 68  her.** process h
f5100 61 73 20 69 74 20 6f 70 65 6e 2e 20 20 53 6f 6d  as it open.  Som
f5110 65 74 69 6d 65 73 20 61 20 76 69 72 75 73 20 73  etimes a virus s
f5120 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64 65 78 69  canner or indexi
f5130 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 77 69  ng program.** wi
f5140 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75 72 6e 61  ll open a journa
f5150 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c 79 20 61  l file shortly a
f5160 66 74 65 72 20 69 74 20 69 73 20 63 72 65 61 74  fter it is creat
f5170 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64  ed in order to d
f5180 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72 20 69 74  o.** whatever it
f5190 20 64 6f 65 73 2e 20 20 57 68 69 6c 65 20 74 68   does.  While th
f51a0 69 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  is other process
f51b0 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a   is holding the.
f51c0 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c 20 77 65  ** file open, we
f51d0 20 77 69 6c 6c 20 62 65 20 75 6e 61 62 6c 65 20   will be unable 
f51e0 74 6f 20 64 65 6c 65 74 65 20 69 74 2e 20 20 54  to delete it.  T
f51f0 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68  o work around th
f5200 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c 20 77  is.** problem, w
f5210 65 20 64 65 6c 61 79 20 31 30 30 20 6d 69 6c 6c  e delay 100 mill
f5220 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79  iseconds and try
f5230 20 74 6f 20 64 65 6c 65 74 65 20 61 67 61 69 6e   to delete again
f5240 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d 58 5f 44  .  Up.** to MX_D
f5250 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 73  ELETION_ATTEMPTs
f5260 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 65 6d 70   deletion attemp
f5270 74 73 20 61 72 65 20 72 75 6e 20 62 65 66 6f 72  ts are run befor
f5280 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75 70 20 61  e giving.** up a
f5290 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20  nd returning an 
f52a0 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e  error..*/.#defin
f52b0 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54  e MX_DELETION_AT
f52c0 54 45 4d 50 54 53 20 35 0a 73 74 61 74 69 63 20  TEMPTS 5.static 
f52d0 69 6e 74 20 77 69 6e 44 65 6c 65 74 65 28 0a 20  int winDelete(. 
f52e0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
f52f0 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  fs,          /* 
f5300 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33  Not used on win3
f5310 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  2 */.  const cha
f5320 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *zFilename,   
f5330 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69     /* Name of fi
f5340 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a  le to delete */.
f5350 20 20 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20    int syncDir   
f5360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f5370 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e   Not used on win
f5380 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63  32 */.){.  int c
f5390 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20  nt = 0;.  DWORD 
f53a0 72 63 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f  rc;.  DWORD erro
f53b0 72 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 7a  r = 0;.  void *z
f53c0 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76  Converted = conv
f53d0 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28  ertUtf8Filename(
f53e0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e  zFilename);.  UN
f53f0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
f5400 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  Vfs);.  UNUSED_P
f5410 41 52 41 4d 45 54 45 52 28 73 79 6e 63 44 69 72  ARAMETER(syncDir
f5420 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72  );.  if( zConver
f5430 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ted==0 ){.    re
f5440 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
f5450 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74  M;.  }.  Simulat
f5460 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20  eIOError(return 
f5470 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c  SQLITE_IOERR_DEL
f5480 45 54 45 29 3b 0a 20 20 69 66 28 20 69 73 4e 54  ETE);.  if( isNT
f5490 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b 0a 20 20  () ){.    do{.  
f54a0 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28      DeleteFileW(
f54b0 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
f54c0 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 20 28   }while(   (   (
f54d0 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74  (rc = GetFileAtt
f54e0 72 69 62 75 74 65 73 57 28 7a 43 6f 6e 76 65 72  ributesW(zConver
f54f0 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44  ted)) != INVALID
f5500 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53  _FILE_ATTRIBUTES
f5510 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
f5520 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65   || ((error = Ge
f5530 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d  tLastError()) ==
f5540 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45   ERROR_ACCESS_DE
f5550 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 20 20  NIED)).         
f5560 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58    && (++cnt < MX
f5570 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50  _DELETION_ATTEMP
f5580 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 26  TS).           &
f5590 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31  & (Sleep(100), 1
f55a0 29 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69  ) );./* isNT() i
f55b0 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53  s 1 if SQLITE_OS
f55c0 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68  _WINCE==1, so th
f55d0 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72  is else is never
f55e0 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53   executed. .** S
f55f0 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76  ince the ASCII v
f5600 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  ersion of these 
f5610 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e  Windows API do n
f5620 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e  ot exist for WIN
f5630 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f  CE,.** it's impo
f5640 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66  rtant to not ref
f5650 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20  erence them for 
f5660 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f  WINCE builds..*/
f5670 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
f5680 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b  INCE==0.  }else{
f5690 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 44  .    do{.      D
f56a0 65 6c 65 74 65 46 69 6c 65 41 28 7a 43 6f 6e 76  eleteFileA(zConv
f56b0 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77 68 69  erted);.    }whi
f56c0 6c 65 28 20 20 20 28 20 20 20 28 28 72 63 20 3d  le(   (   ((rc =
f56d0 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74   GetFileAttribut
f56e0 65 73 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 29  esA(zConverted))
f56f0 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45   != INVALID_FILE
f5700 5f 41 54 54 52 49 42 55 54 45 53 29 0a 20 20 20  _ATTRIBUTES).   
f5710 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28              || (
f5720 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74  (error = GetLast
f5730 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f  Error()) == ERRO
f5740 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 44 29  R_ACCESS_DENIED)
f5750 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  ).           && 
f5760 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45  (++cnt < MX_DELE
f5770 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20  TION_ATTEMPTS). 
f5780 20 20 20 20 20 20 20 20 20 20 26 26 20 28 53 6c            && (Sl
f5790 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a  eep(100), 1) );.
f57a0 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65  #endif.  }.  fre
f57b0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
f57c0 20 4f 53 54 52 41 43 45 32 28 22 44 45 4c 45 54   OSTRACE2("DELET
f57d0 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69  E \"%s\"\n", zFi
f57e0 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72  lename);.  retur
f57f0 6e 20 28 20 20 20 28 72 63 20 3d 3d 20 49 4e 56  n (   (rc == INV
f5800 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
f5810 55 54 45 53 29 20 0a 20 20 20 20 20 20 20 20 20  UTES) .         
f5820 20 26 26 20 28 65 72 72 6f 72 20 3d 3d 20 45 52   && (error == ER
f5830 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55  ROR_FILE_NOT_FOU
f5840 4e 44 29 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  ND)) ? SQLITE_OK
f5850 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   : SQLITE_IOERR_
f5860 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  DELETE;.}../*.**
f5870 20 43 68 65 63 6b 20 74 68 65 20 65 78 69 73 74   Check the exist
f5880 61 6e 63 65 20 61 6e 64 20 73 74 61 74 75 73 20  ance and status 
f5890 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  of a file..*/.st
f58a0 61 74 69 63 20 69 6e 74 20 77 69 6e 41 63 63 65  atic int winAcce
f58b0 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  ss(.  sqlite3_vf
f58c0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
f58d0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20   /* Not used on 
f58e0 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74  win32 */.  const
f58f0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f5900 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ,     /* Name of
f5910 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a   file to check *
f5920 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
f5930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f5940 2a 20 54 79 70 65 20 6f 66 20 74 65 73 74 20 74  * Type of test t
f5950 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66  o make on this f
f5960 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52  ile */.  int *pR
f5970 65 73 4f 75 74 20 20 20 20 20 20 20 20 20 20 20  esOut           
f5980 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 73 75      /* OUT: Resu
f5990 6c 74 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 44  lt */.){.  DWORD
f59a0 20 61 74 74 72 3b 0a 20 20 69 6e 74 20 72 63 20   attr;.  int rc 
f59b0 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  = 0;.  void *zCo
f59c0 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72  nverted = conver
f59d0 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46  tUtf8Filename(zF
f59e0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53  ilename);.  UNUS
f59f0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
f5a00 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65  s);.  if( zConve
f5a10 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72  rted==0 ){.    r
f5a20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
f5a30 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73  EM;.  }.  if( is
f5a40 4e 54 28 29 20 29 7b 0a 20 20 20 20 61 74 74 72  NT() ){.    attr
f5a50 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62   = GetFileAttrib
f5a60 75 74 65 73 57 28 28 57 43 48 41 52 2a 29 7a 43  utesW((WCHAR*)zC
f5a70 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73  onverted);./* is
f5a80 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
f5a90 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
f5aa0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
f5ab0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
f5ac0 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41   .** Since the A
f5ad0 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20  SCII version of 
f5ae0 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50  these Windows AP
f5af0 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66  I do not exist f
f5b00 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27  or WINCE,.** it'
f5b10 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e  s important to n
f5b20 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  ot reference the
f5b30 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c  m for WINCE buil
f5b40 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ds..*/.#if SQLIT
f5b50 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20  E_OS_WINCE==0.  
f5b60 7d 65 6c 73 65 7b 0a 20 20 20 20 61 74 74 72 20  }else{.    attr 
f5b70 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75  = GetFileAttribu
f5b80 74 65 73 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e  tesA((char*)zCon
f5b90 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a  verted);.#endif.
f5ba0 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76    }.  free(zConv
f5bb0 65 72 74 65 64 29 3b 0a 20 20 73 77 69 74 63 68  erted);.  switch
f5bc0 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63  ( flags ){.    c
f5bd0 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ase SQLITE_ACCES
f5be0 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65  S_READ:.    case
f5bf0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
f5c00 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20  XISTS:.      rc 
f5c10 3d 20 61 74 74 72 21 3d 49 4e 56 41 4c 49 44 5f  = attr!=INVALID_
f5c20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 3b  FILE_ATTRIBUTES;
f5c30 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
f5c40 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
f5c50 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a  CESS_READWRITE:.
f5c60 20 20 20 20 20 20 72 63 20 3d 20 28 61 74 74 72        rc = (attr
f5c70 20 26 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54   & FILE_ATTRIBUT
f5c80 45 5f 52 45 41 44 4f 4e 4c 59 29 3d 3d 30 3b 0a  E_READONLY)==0;.
f5c90 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
f5ca0 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
f5cb0 61 73 73 65 72 74 28 21 22 49 6e 76 61 6c 69 64  assert(!"Invalid
f5cc0 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22   flags argument"
f5cd0 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75  );.  }.  *pResOu
f5ce0 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e  t = rc;.  return
f5cf0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
f5d00 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c  /*.** Turn a rel
f5d10 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69  ative pathname i
f5d20 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e  nto a full pathn
f5d30 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68 65 20  ame.  Write the 
f5d40 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65  full.** pathname
f5d50 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a   into zOut[].  z
f5d60 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74  Out[] will be at
f5d70 20 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50   least pVfs->mxP
f5d80 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73  athname.** bytes
f5d90 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61   in size..*/.sta
f5da0 74 69 63 20 69 6e 74 20 77 69 6e 46 75 6c 6c 50  tic int winFullP
f5db0 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74  athname(.  sqlit
f5dc0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
f5dd0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
f5de0 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63  ter to vfs objec
f5df0 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
f5e00 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20  r *zRelative,   
f5e10 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79       /* Possibly
f5e20 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20   relative input 
f5e30 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  path */.  int nF
f5e40 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  ull,            
f5e50 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
f5e60 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  of output buffer
f5e70 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63   in bytes */.  c
f5e80 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20  har *zFull      
f5e90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f5ea0 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f  Output buffer */
f5eb0 0a 29 7b 0a 20 20 0a 23 69 66 20 64 65 66 69 6e  .){.  .#if defin
f5ec0 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20  ed(__CYGWIN__). 
f5ed0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f5ee0 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 63 79 67 77  R(nFull);.  cygw
f5ef0 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c 6c 5f  in_conv_to_full_
f5f00 77 69 6e 33 32 5f 70 61 74 68 28 7a 52 65 6c 61  win32_path(zRela
f5f10 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b 0a 20 20  tive, zFull);.  
f5f20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f5f30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 51  ;.#endif..#if SQ
f5f40 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20  LITE_OS_WINCE.  
f5f50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f5f60 28 6e 46 75 6c 6c 29 3b 0a 20 20 2f 2a 20 57 69  (nFull);.  /* Wi
f5f70 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f 6e 63 65  nCE has no conce
f5f80 70 74 20 6f 66 20 61 20 72 65 6c 61 74 69 76 65  pt of a relative
f5f90 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 20 73 6f   pathname, or so
f5fa0 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f 0a 20   I am told. */. 
f5fb0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
f5fc0 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  f(pVfs->mxPathna
f5fd0 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c  me, zFull, "%s",
f5fe0 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 72   zRelative);.  r
f5ff0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f6000 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 53 51  .#endif..#if !SQ
f6010 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26  LITE_OS_WINCE &&
f6020 20 21 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57   !defined(__CYGW
f6030 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e 42 79 74  IN__).  int nByt
f6040 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76  e;.  void *zConv
f6050 65 72 74 65 64 3b 0a 20 20 63 68 61 72 20 2a 7a  erted;.  char *z
f6060 4f 75 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  Out;.  UNUSED_PA
f6070 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a  RAMETER(nFull);.
f6080 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63    zConverted = c
f6090 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61  onvertUtf8Filena
f60a0 6d 65 28 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20  me(zRelative);. 
f60b0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
f60c0 20 20 20 57 43 48 41 52 20 2a 7a 54 65 6d 70 3b     WCHAR *zTemp;
f60d0 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74  .    nByte = Get
f60e0 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57  FullPathNameW((W
f60f0 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
f6100 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a  , 0, 0, 0) + 3;.
f6110 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c      zTemp = mall
f6120 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66  oc( nByte*sizeof
f6130 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20  (zTemp[0]) );.  
f6140 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29    if( zTemp==0 )
f6150 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f  {.      free(zCo
f6160 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20  nverted);.      
f6170 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
f6180 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47  MEM;.    }.    G
f6190 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 28  etFullPathNameW(
f61a0 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74  (WCHAR*)zConvert
f61b0 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70  ed, nByte, zTemp
f61c0 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a  , 0);.    free(z
f61d0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
f61e0 7a 4f 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f  zOut = unicodeTo
f61f0 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20  Utf8(zTemp);.   
f6200 20 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 2f 2a   free(zTemp);./*
f6210 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20   isNT() is 1 if 
f6220 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
f6230 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65  =1, so this else
f6240 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74   is never execut
f6250 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68  ed. .** Since th
f6260 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20  e ASCII version 
f6270 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73  of these Windows
f6280 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73   API do not exis
f6290 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20  t for WINCE,.** 
f62a0 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  it's important t
f62b0 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20  o not reference 
f62c0 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62  them for WINCE b
f62d0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51  uilds..*/.#if SQ
f62e0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30  LITE_OS_WINCE==0
f62f0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68  .  }else{.    ch
f6300 61 72 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e  ar *zTemp;.    n
f6310 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61  Byte = GetFullPa
f6320 74 68 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a  thNameA((char*)z
f6330 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c  Converted, 0, 0,
f6340 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65   0) + 3;.    zTe
f6350 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79  mp = malloc( nBy
f6360 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b  te*sizeof(zTemp[
f6370 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a  0]) );.    if( z
f6380 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Temp==0 ){.     
f6390 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64   free(zConverted
f63a0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
f63b0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
f63c0 20 20 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50    }.    GetFullP
f63d0 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72 2a 29  athNameA((char*)
f63e0 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74  zConverted, nByt
f63f0 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20  e, zTemp, 0);.  
f6400 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65    free(zConverte
f6410 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 73  d);.    zOut = s
f6420 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63  qlite3_win32_mbc
f6430 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 6d 70 29  s_to_utf8(zTemp)
f6440 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70  ;.    free(zTemp
f6450 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  );.#endif.  }.  
f6460 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20 20 20 20  if( zOut ){.    
f6470 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
f6480 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d  (pVfs->mxPathnam
f6490 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20  e, zFull, "%s", 
f64a0 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72 65 65 28  zOut);.    free(
f64b0 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65 74 75 72  zOut);.    retur
f64c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
f64d0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
f64e0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
f64f0 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
f6500 2a 2a 20 47 65 74 20 74 68 65 20 73 65 63 74 6f  ** Get the secto
f6510 72 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 65  r size of the de
f6520 76 69 63 65 20 75 73 65 64 20 74 6f 20 73 74 6f  vice used to sto
f6530 72 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73  re.** file..*/.s
f6540 74 61 74 69 63 20 69 6e 74 20 67 65 74 53 65 63  tatic int getSec
f6550 74 6f 72 53 69 7a 65 28 0a 20 20 20 20 73 71 6c  torSize(.    sql
f6560 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 0a  ite3_vfs *pVfs,.
f6570 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
f6580 7a 52 65 6c 61 74 69 76 65 20 20 20 20 20 2f 2a  zRelative     /*
f6590 20 55 54 46 2d 38 20 66 69 6c 65 20 6e 61 6d 65   UTF-8 file name
f65a0 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 62   */.){.  DWORD b
f65b0 79 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20  ytesPerSector = 
f65c0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53  SQLITE_DEFAULT_S
f65d0 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 63 68  ECTOR_SIZE;.  ch
f65e0 61 72 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41 58  ar zFullpath[MAX
f65f0 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74 20  _PATH+1];.  int 
f6600 72 63 3b 0a 20 20 44 57 4f 52 44 20 64 77 52 65  rc;.  DWORD dwRe
f6610 74 20 3d 20 30 2c 20 64 77 44 75 6d 6d 79 3b 0a  t = 0, dwDummy;.
f6620 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 6e 65  .  /*.  ** We ne
f6630 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 66 75  ed to get the fu
f6640 6c 6c 20 70 61 74 68 20 6e 61 6d 65 20 6f 66 20  ll path name of 
f6650 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 6f  the file.  ** to
f6660 20 67 65 74 20 74 68 65 20 64 72 69 76 65 20 6c   get the drive l
f6670 65 74 74 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70  etter to look up
f6680 20 74 68 65 20 73 65 63 74 6f 72 0a 20 20 2a 2a   the sector.  **
f6690 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 72 63   size..  */.  rc
f66a0 20 3d 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61   = winFullPathna
f66b0 6d 65 28 70 56 66 73 2c 20 7a 52 65 6c 61 74 69  me(pVfs, zRelati
f66c0 76 65 2c 20 4d 41 58 5f 50 41 54 48 2c 20 7a 46  ve, MAX_PATH, zF
f66d0 75 6c 6c 70 61 74 68 29 3b 0a 20 20 69 66 28 20  ullpath);.  if( 
f66e0 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b 20  rc == SQLITE_OK 
f66f0 29 0a 20 20 7b 0a 20 20 20 20 76 6f 69 64 20 2a  ).  {.    void *
f6700 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
f6710 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
f6720 28 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 20  (zFullpath);.   
f6730 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 20   if( zConverted 
f6740 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 4e  ){.      if( isN
f6750 54 28 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  T() ){.        /
f6760 2a 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a  * trim path to j
f6770 75 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65  ust drive refere
f6780 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 57  nce */.        W
f6790 43 48 41 52 20 2a 70 20 3d 20 7a 43 6f 6e 76 65  CHAR *p = zConve
f67a0 72 74 65 64 3b 0a 20 20 20 20 20 20 20 20 66 6f  rted;.        fo
f67b0 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a 20 20 20 20  r(;*p;p++){.    
f67c0 20 20 20 20 20 20 69 66 28 20 2a 70 20 3d 3d 20        if( *p == 
f67d0 27 5c 5c 27 20 29 7b 0a 20 20 20 20 20 20 20 20  '\\' ){.        
f67e0 20 20 20 20 2a 70 20 3d 20 27 5c 30 27 3b 0a 20      *p = '\0';. 
f67f0 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
f6800 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
f6810 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
f6820 64 77 52 65 74 20 3d 20 47 65 74 44 69 73 6b 46  dwRet = GetDiskF
f6830 72 65 65 53 70 61 63 65 57 28 28 57 43 48 41 52  reeSpaceW((WCHAR
f6840 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20  *)zConverted,.  
f6850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6870 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20  &dwDummy,.      
f6880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6890 20 20 20 20 20 20 20 20 20 20 20 20 26 62 79 74              &byt
f68a0 65 73 50 65 72 53 65 63 74 6f 72 2c 0a 20 20 20  esPerSector,.   
f68b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f68c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
f68d0 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 20 20 20  dwDummy,.       
f68e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f68f0 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75             &dwDu
f6900 6d 6d 79 29 3b 0a 23 69 66 20 53 51 4c 49 54 45  mmy);.#if SQLITE
f6910 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 20  _OS_WINCE==0.   
f6920 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
f6930 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20 74    /* trim path t
f6940 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65 66  o just drive ref
f6950 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20  erence */.      
f6960 20 20 43 48 41 52 20 2a 70 20 3d 20 28 43 48 41    CHAR *p = (CHA
f6970 52 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64 3b 0a  R *)zConverted;.
f6980 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b          for(;*p;
f6990 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  p++){.          
f69a0 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29  if( *p == '\\' )
f69b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70  {.            *p
f69c0 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20   = '\0';.       
f69d0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
f69e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
f69f0 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65 74 20  }.        dwRet 
f6a00 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 61  = GetDiskFreeSpa
f6a10 63 65 41 28 28 43 48 41 52 2a 29 7a 43 6f 6e 76  ceA((CHAR*)zConv
f6a20 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20  erted,.         
f6a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6a40 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d           &dwDumm
f6a50 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y,.             
f6a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6a70 20 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65       &bytesPerSe
f6a80 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20  ctor,.          
f6a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6aa0 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79          &dwDummy
f6ab0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
f6ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6ad0 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 23      &dwDummy);.#
f6ae0 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20  endif.      }.  
f6af0 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
f6b00 74 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ted);.    }.    
f6b10 69 66 28 20 21 64 77 52 65 74 20 29 7b 0a 20 20  if( !dwRet ){.  
f6b20 20 20 20 20 62 79 74 65 73 50 65 72 53 65 63 74      bytesPerSect
f6b30 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  or = SQLITE_DEFA
f6b40 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b  ULT_SECTOR_SIZE;
f6b50 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
f6b60 75 72 6e 20 28 69 6e 74 29 20 62 79 74 65 73 50  urn (int) bytesP
f6b70 65 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69  erSector; .}..#i
f6b80 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
f6b90 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
f6ba0 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65  ./*.** Interface
f6bb0 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20  s for opening a 
f6bc0 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
f6bd0 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f  finding entry po
f6be0 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74  ints.** within t
f6bf0 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
f6c00 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74  y, and closing t
f6c10 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
f6c20 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  y..*/./*.** Inte
f6c30 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69  rfaces for openi
f6c40 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72  ng a shared libr
f6c50 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74  ary, finding ent
f6c60 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74  ry points.** wit
f6c70 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c  hin the shared l
f6c80 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73  ibrary, and clos
f6c90 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
f6ca0 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  ibrary..*/.stati
f6cb0 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65  c void *winDlOpe
f6cc0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  n(sqlite3_vfs *p
f6cd0 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Vfs, const char 
f6ce0 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48  *zFilename){.  H
f6cf0 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20  ANDLE h;.  void 
f6d00 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f  *zConverted = co
f6d10 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d  nvertUtf8Filenam
f6d20 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
f6d30 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f6d40 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43  (pVfs);.  if( zC
f6d50 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20  onverted==0 ){. 
f6d60 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
f6d70 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
f6d80 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62  .    h = LoadLib
f6d90 72 61 72 79 57 28 28 57 43 48 41 52 2a 29 7a 43  raryW((WCHAR*)zC
f6da0 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73  onverted);./* is
f6db0 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
f6dc0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
f6dd0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
f6de0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
f6df0 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41   .** Since the A
f6e00 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20  SCII version of 
f6e10 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50  these Windows AP
f6e20 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66  I do not exist f
f6e30 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27  or WINCE,.** it'
f6e40 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e  s important to n
f6e50 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  ot reference the
f6e60 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c  m for WINCE buil
f6e70 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ds..*/.#if SQLIT
f6e80 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20  E_OS_WINCE==0.  
f6e90 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c  }else{.    h = L
f6ea0 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63 68 61  oadLibraryA((cha
f6eb0 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  r*)zConverted);.
f6ec0 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65  #endif.  }.  fre
f6ed0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
f6ee0 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 68   return (void*)h
f6ef0 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
f6f00 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69 74  winDlError(sqlit
f6f10 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
f6f20 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
f6f30 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 44  ufOut){.  UNUSED
f6f40 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
f6f50 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f 72  ;.  getLastError
f6f60 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75  Msg(nBuf, zBufOu
f6f70 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77 69 6e  t);.}.void (*win
f6f80 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66  DlSym(sqlite3_vf
f6f90 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
f6fa0 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68  Handle, const ch
f6fb0 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f  ar *zSymbol))(vo
f6fc0 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  id){.  UNUSED_PA
f6fd0 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23  RAMETER(pVfs);.#
f6fe0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f6ff0 43 45 0a 20 20 2f 2a 20 54 68 65 20 47 65 74 50  CE.  /* The GetP
f7000 72 6f 63 41 64 64 72 65 73 73 41 28 29 20 72 6f  rocAddressA() ro
f7010 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76  utine is only av
f7020 61 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65  ailable on wince
f7030 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76  . */.  return (v
f7040 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 74  oid(*)(void))Get
f7050 50 72 6f 63 41 64 64 72 65 73 73 41 28 28 48 41  ProcAddressA((HA
f7060 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53  NDLE)pHandle, zS
f7070 79 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20  ymbol);.#else.  
f7080 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e  /* All other win
f7090 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20 65  dows platforms e
f70a0 78 70 65 63 74 20 47 65 74 50 72 6f 63 41 64 64  xpect GetProcAdd
f70b0 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a 20  ress() to take. 
f70c0 20 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 72 69   ** an Ansi stri
f70d0 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  ng regardless of
f70e0 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73 65   the _UNICODE se
f70f0 74 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72  tting */.  retur
f7100 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29  n (void(*)(void)
f7110 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 28  )GetProcAddress(
f7120 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c  (HANDLE)pHandle,
f7130 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69   zSymbol);.#endi
f7140 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c  f.}.void winDlCl
f7150 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ose(sqlite3_vfs 
f7160 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61  *pVfs, void *pHa
f7170 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f  ndle){.  UNUSED_
f7180 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
f7190 0a 20 20 46 72 65 65 4c 69 62 72 61 72 79 28 28  .  FreeLibrary((
f71a0 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 29 3b  HANDLE)pHandle);
f71b0 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53  .}.#else /* if S
f71c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
f71d0 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66  EXTENSION is def
f71e0 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69  ined: */.  #defi
f71f0 6e 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20 30 0a  ne winDlOpen  0.
f7200 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 45    #define winDlE
f7210 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65  rror 0.  #define
f7220 20 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a 20 20   winDlSym   0.  
f7230 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 43 6c 6f  #define winDlClo
f7240 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  se 0.#endif.../*
f7250 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74 6f 20  .** Write up to 
f7260 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61  nBuf bytes of ra
f7270 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42  ndomness into zB
f7280 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  uf..*/.static in
f7290 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 28  t winRandomness(
f72a0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
f72b0 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
f72c0 72 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e 74 20  r *zBuf){.  int 
f72d0 6e 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f  n = 0;.  UNUSED_
f72e0 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
f72f0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
f7300 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20  ITE_TEST).  n = 
f7310 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a  nBuf;.  memset(z
f7320 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23  Buf, 0, nBuf);.#
f7330 65 6c 73 65 0a 20 20 69 66 28 20 73 69 7a 65 6f  else.  if( sizeo
f7340 66 28 53 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e  f(SYSTEMTIME)<=n
f7350 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53  Buf-n ){.    SYS
f7360 54 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20 20 47  TEMTIME x;.    G
f7370 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 78 29  etSystemTime(&x)
f7380 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42  ;.    memcpy(&zB
f7390 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f  uf[n], &x, sizeo
f73a0 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  f(x));.    n += 
f73b0 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20  sizeof(x);.  }. 
f73c0 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52   if( sizeof(DWOR
f73d0 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20  D)<=nBuf-n ){.  
f73e0 20 20 44 57 4f 52 44 20 70 69 64 20 3d 20 47 65    DWORD pid = Ge
f73f0 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49  tCurrentProcessI
f7400 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  d();.    memcpy(
f7410 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20  &zBuf[n], &pid, 
f7420 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20  sizeof(pid));.  
f7430 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69    n += sizeof(pi
f7440 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69  d);.  }.  if( si
f7450 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75  zeof(DWORD)<=nBu
f7460 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44  f-n ){.    DWORD
f7470 20 63 6e 74 20 3d 20 47 65 74 54 69 63 6b 43 6f   cnt = GetTickCo
f7480 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70  unt();.    memcp
f7490 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74  y(&zBuf[n], &cnt
f74a0 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a  , sizeof(cnt));.
f74b0 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28      n += sizeof(
f74c0 63 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  cnt);.  }.  if( 
f74d0 73 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54  sizeof(LARGE_INT
f74e0 45 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b  EGER)<=nBuf-n ){
f74f0 0a 20 20 20 20 4c 41 52 47 45 5f 49 4e 54 45 47  .    LARGE_INTEG
f7500 45 52 20 69 3b 0a 20 20 20 20 51 75 65 72 79 50  ER i;.    QueryP
f7510 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65  erformanceCounte
f7520 72 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70  r(&i);.    memcp
f7530 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20  y(&zBuf[n], &i, 
f7540 73 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20 20 20  sizeof(i));.    
f7550 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a  n += sizeof(i);.
f7560 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
f7570 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn n;.}.../*.**
f7580 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74   Sleep for a lit
f7590 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75  tle while.  Retu
f75a0 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  rn the amount of
f75b0 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a   time slept..*/.
f75c0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 6c  static int winSl
f75d0 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20  eep(sqlite3_vfs 
f75e0 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f  *pVfs, int micro
f75f0 73 65 63 29 7b 0a 20 20 53 6c 65 65 70 28 28 6d  sec){.  Sleep((m
f7600 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 30  icrosec+999)/100
f7610 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  0);.  UNUSED_PAR
f7620 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
f7630 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f 73 65  return ((microse
f7640 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31 30 30  c+999)/1000)*100
f7650 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
f7660 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62  following variab
f7670 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20  le, if set to a 
f7680 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20  non-zero value, 
f7690 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73 75  becomes the resu
f76a0 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 66  lt.** returned f
f76b0 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 75 72  rom sqlite3OsCur
f76c0 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 68 69  rentTime().  Thi
f76d0 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  s is used for te
f76e0 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66  sting..*/.#ifdef
f76f0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
f7700 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f7710 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
f7720 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a   = 0;.#endif../*
f7730 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72  .** Find the cur
f7740 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e  rent time (in Un
f7750 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61  iversal Coordina
f7760 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74  ted Time).  Writ
f7770 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  e the.** current
f7780 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61   time and date a
f7790 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e  s a Julian Day n
f77a0 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f  umber into *prNo
f77b0 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20  w and.** return 
f77c0 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  0.  Return 1 if 
f77d0 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74  the time and dat
f77e0 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  e cannot be foun
f77f0 64 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72  d..*/.int winCur
f7800 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33  rentTime(sqlite3
f7810 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62  _vfs *pVfs, doub
f7820 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49  le *prNow){.  FI
f7830 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20  LETIME ft;.  /* 
f7840 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63 74 75  FILETIME structu
f7850 72 65 20 69 73 20 61 20 36 34 2d 62 69 74 20 76  re is a 64-bit v
f7860 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74 69 6e  alue representin
f7870 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  g the number of 
f7880 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65  .     100-nanose
f7890 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 73  cond intervals s
f78a0 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31 2c 20  ince January 1, 
f78b0 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30 35 38  1601 (= JD 23058
f78c0 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 73  13.5). .  */.  s
f78d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 69 6d  qlite3_int64 tim
f78e0 65 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65 20 64  eW;   /* Whole d
f78f0 61 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ays */.  sqlite3
f7900 5f 69 6e 74 36 34 20 74 69 6d 65 46 3b 20 20 20  _int64 timeF;   
f7910 2f 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20 44 61  /* Fractional Da
f7920 79 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62  ys */..  /* Numb
f7930 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65  er of 100-nanose
f7940 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 69  cond intervals i
f7950 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 79 20 2a  n a single day *
f7960 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
f7970 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e   sqlite3_int64 n
f7980 74 75 50 65 72 44 61 79 20 3d 20 0a 20 20 20 20  tuPerDay = .    
f7990 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 69    10000000*(sqli
f79a0 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30 3b  te3_int64)86400;
f79b0 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ..  /* Number of
f79c0 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20   100-nanosecond 
f79d0 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 68 61 6c  intervals in hal
f79e0 66 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a 20 20  f of a day */.  
f79f0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
f7a00 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 65  ite3_int64 ntuPe
f7a10 72 48 61 6c 66 44 61 79 20 3d 20 0a 20 20 20 20  rHalfDay = .    
f7a20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 69    10000000*(sqli
f7a30 74 65 33 5f 69 6e 74 36 34 29 34 33 32 30 30 3b  te3_int64)43200;
f7a40 0a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20 74 6f  ..  /* 2^32 - to
f7a50 20 61 76 6f 69 64 20 75 73 65 20 6f 66 20 4c 4c   avoid use of LL
f7a60 20 61 6e 64 20 77 61 72 6e 69 6e 67 73 20 69 6e   and warnings in
f7a70 20 67 63 63 20 2a 2f 0a 20 20 73 74 61 74 69 63   gcc */.  static
f7a80 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
f7a90 6e 74 36 34 20 6d 61 78 33 32 42 69 74 56 61 6c  nt64 max32BitVal
f7aa0 75 65 20 3d 20 0a 20 20 20 20 20 20 28 73 71 6c  ue = .      (sql
f7ab0 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30 30 30  ite3_int64)20000
f7ac0 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33  00000 + (sqlite3
f7ad0 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 30  _int64)200000000
f7ae0 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  0 + (sqlite3_int
f7af0 36 34 29 32 39 34 39 36 37 32 39 36 3b 0a 0a 23  64)294967296;..#
f7b00 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f7b10 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20  CE.  SYSTEMTIME 
f7b20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 74 65  time;.  GetSyste
f7b30 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a 20 20  mTime(&time);.  
f7b40 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69 6d 65  /* if SystemTime
f7b50 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 61 69  ToFileTime() fai
f7b60 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20 7a  ls, it returns z
f7b70 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 21 53  ero. */.  if (!S
f7b80 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54  ystemTimeToFileT
f7b90 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 29 7b  ime(&time,&ft)){
f7ba0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
f7bb0 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 53 79   }.#else.  GetSy
f7bc0 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54 69  stemTimeAsFileTi
f7bd0 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e 64 69  me( &ft );.#endi
f7be0 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  f.  UNUSED_PARAM
f7bf0 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 74 69  ETER(pVfs);.  ti
f7c00 6d 65 57 20 3d 20 28 28 28 73 71 6c 69 74 65 33  meW = (((sqlite3
f7c10 5f 69 6e 74 36 34 29 66 74 2e 64 77 48 69 67 68  _int64)ft.dwHigh
f7c20 44 61 74 65 54 69 6d 65 29 2a 6d 61 78 33 32 42  DateTime)*max32B
f7c30 69 74 56 61 6c 75 65 29 20 2b 20 28 73 71 6c 69  itValue) + (sqli
f7c40 74 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c  te3_int64)ft.dwL
f7c50 6f 77 44 61 74 65 54 69 6d 65 3b 0a 20 20 74 69  owDateTime;.  ti
f7c60 6d 65 46 20 3d 20 74 69 6d 65 57 20 25 20 6e 74  meF = timeW % nt
f7c70 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20  uPerDay;        
f7c80 20 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20    /* fractional 
f7c90 64 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f 73 65  days (100-nanose
f7ca0 63 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69 6d 65  conds) */.  time
f7cb0 57 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74 75 50  W = timeW / ntuP
f7cc0 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20  erDay;          
f7cd0 2f 2a 20 77 68 6f 6c 65 20 64 61 79 73 20 2a 2f  /* whole days */
f7ce0 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57  .  timeW = timeW
f7cf0 20 2b 20 32 33 30 35 38 31 33 3b 20 20 20 20 20   + 2305813;     
f7d00 20 20 20 20 20 20 20 2f 2a 20 61 64 64 20 77 68         /* add wh
f7d10 6f 6c 65 20 64 61 79 73 20 28 66 72 6f 6d 20 32  ole days (from 2
f7d20 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74  305813.5) */.  t
f7d30 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b 20 6e  imeF = timeF + n
f7d40 74 75 50 65 72 48 61 6c 66 44 61 79 3b 20 20 20  tuPerHalfDay;   
f7d50 20 20 20 2f 2a 20 61 64 64 20 68 61 6c 66 20 61     /* add half a
f7d60 20 64 61 79 20 28 66 72 6f 6d 20 32 33 30 35 38   day (from 23058
f7d70 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 57  13.5) */.  timeW
f7d80 20 3d 20 74 69 6d 65 57 20 2b 20 28 74 69 6d 65   = timeW + (time
f7d90 46 2f 6e 74 75 50 65 72 44 61 79 29 3b 20 20 2f  F/ntuPerDay);  /
f7da0 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61 79 20  * add whole day 
f7db0 69 66 20 68 61 6c 66 20 64 61 79 20 6d 61 64 65  if half day made
f7dc0 20 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65 46 20   one */.  timeF 
f7dd0 3d 20 74 69 6d 65 46 20 25 20 6e 74 75 50 65 72  = timeF % ntuPer
f7de0 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Day;          /*
f7df0 20 63 6f 6d 70 75 74 65 20 6e 65 77 20 66 72 61   compute new fra
f7e00 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a 2f 0a  ctional days */.
f7e10 20 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f 75 62    *prNow = (doub
f7e20 6c 65 29 74 69 6d 65 57 20 2b 20 28 28 64 6f 75  le)timeW + ((dou
f7e30 62 6c 65 29 74 69 6d 65 46 20 2f 20 28 64 6f 75  ble)timeF / (dou
f7e40 62 6c 65 29 6e 74 75 50 65 72 44 61 79 29 3b 0a  ble)ntuPerDay);.
f7e50 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
f7e60 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ST.  if( sqlite3
f7e70 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b  _current_time ){
f7e80 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28  .    *prNow = ((
f7e90 64 6f 75 62 6c 65 29 73 71 6c 69 74 65 33 5f 63  double)sqlite3_c
f7ea0 75 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20 28 64  urrent_time + (d
f7eb0 6f 75 62 6c 65 29 34 33 32 30 30 29 20 2f 20 28  ouble)43200) / (
f7ec0 64 6f 75 62 6c 65 29 38 36 34 30 30 20 2b 20 28  double)86400 + (
f7ed0 64 6f 75 62 6c 65 29 32 34 34 30 35 38 37 3b 0a  double)2440587;.
f7ee0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
f7ef0 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
f7f00 54 68 65 20 69 64 65 61 20 69 73 20 74 68 61 74  The idea is that
f7f10 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   this function w
f7f20 6f 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f 6d 62  orks like a comb
f7f30 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65  ination of.** Ge
f7f40 74 4c 61 73 74 45 72 72 6f 72 28 29 20 61 6e 64  tLastError() and
f7f50 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 28 29   FormatMessage()
f7f60 20 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f 72 20   on windows (or 
f7f70 65 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74 72  errno and.** str
f7f80 65 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75 6e 69  error_r() on uni
f7f90 78 29 2e 20 41 66 74 65 72 20 61 6e 20 65 72 72  x). After an err
f7fa0 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 62  or is returned b
f7fb0 79 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63 74  y an OS.** funct
f7fc0 69 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 6c 6c  ion, SQLite call
f7fd0 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  s this function 
f7fe0 77 69 74 68 20 7a 42 75 66 20 70 6f 69 6e 74 69  with zBuf pointi
f7ff0 6e 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66 66 65  ng to.** a buffe
f8000 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65 73 2e  r of nBuf bytes.
f8010 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20 73 68   The OS layer sh
f8020 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20 74 68  ould populate th
f8030 65 0a 2a 2a 20 62 75 66 66 65 72 20 77 69 74 68  e.** buffer with
f8040 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65   a nul-terminate
f8050 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  d UTF-8 encoded 
f8060 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a  error message.**
f8070 20 64 65 73 63 72 69 62 69 6e 67 20 74 68 65 20   describing the 
f8080 6c 61 73 74 20 49 4f 20 65 72 72 6f 72 20 74 6f  last IO error to
f8090 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 77   have occurred w
f80a0 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 69 6e  ithin the callin
f80b0 67 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 0a  g.** thread..**.
f80c0 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72 20  ** If the error 
f80d0 6d 65 73 73 61 67 65 20 69 73 20 74 6f 6f 20 6c  message is too l
f80e0 61 72 67 65 20 66 6f 72 20 74 68 65 20 73 75 70  arge for the sup
f80f0 70 6c 69 65 64 20 62 75 66 66 65 72 2c 0a 2a 2a  plied buffer,.**
f8100 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 74 72   it should be tr
f8110 75 6e 63 61 74 65 64 2e 20 54 68 65 20 72 65 74  uncated. The ret
f8120 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 78 47 65  urn value of xGe
f8130 74 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20 69 73  tLastError.** is
f8140 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72   zero if the err
f8150 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20  or message fits 
f8160 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f  in the buffer, o
f8170 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74  r non-zero.** ot
f8180 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20  herwise (if the 
f8190 6d 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e  message was trun
f81a0 63 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a  cated). If non-z
f81b0 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2c  ero is returned,
f81c0 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 6e  .** then it is n
f81d0 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20  ot necessary to 
f81e0 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 6c 2d  include the nul-
f81f0 74 65 72 6d 69 6e 61 74 6f 72 20 63 68 61 72 61  terminator chara
f8200 63 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6f  cter.** in the o
f8210 75 74 70 75 74 20 62 75 66 66 65 72 2e 0a 2a 2a  utput buffer..**
f8220 0a 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79 69 6e  .** Not supplyin
f8230 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  g an error messa
f8240 67 65 20 77 69 6c 6c 20 68 61 76 65 20 6e 6f 20  ge will have no 
f8250 61 64 76 65 72 73 65 20 65 66 66 65 63 74 0a 2a  adverse effect.*
f8260 2a 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49 74 20  * on SQLite. It 
f8270 69 73 20 66 69 6e 65 20 74 6f 20 68 61 76 65 20  is fine to have 
f8280 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  an implementatio
f8290 6e 20 74 68 61 74 20 6e 65 76 65 72 0a 2a 2a 20  n that never.** 
f82a0 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
f82b0 20 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20   message:.**.** 
f82c0 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74 45 72    int xGetLastEr
f82d0 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
f82e0 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c  *pVfs, int nBuf,
f82f0 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a   char *zBuf){.**
f8300 20 20 20 20 20 61 73 73 65 72 74 28 7a 42 75 66       assert(zBuf
f8310 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20  [0]=='\0');.**  
f8320 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2a 20     return 0;.** 
f8330 20 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65    }.**.** Howeve
f8340 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6d 65  r if an error me
f8350 73 73 61 67 65 20 69 73 20 73 75 70 70 6c 69 65  ssage is supplie
f8360 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e  d, it will be in
f8370 63 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20 62 79  corporated.** by
f8380 20 73 71 6c 69 74 65 20 69 6e 74 6f 20 74 68 65   sqlite into the
f8390 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 61   error message a
f83a0 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20  vailable to the 
f83b0 75 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20 73 71  user using.** sq
f83c0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 2c 20  lite3_errmsg(), 
f83d0 70 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e 67 20  possibly making 
f83e0 49 4f 20 65 72 72 6f 72 73 20 65 61 73 69 65 72  IO errors easier
f83f0 20 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a 73 74   to debug..*/.st
f8400 61 74 69 63 20 69 6e 74 20 77 69 6e 47 65 74 4c  atic int winGetL
f8410 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 33  astError(sqlite3
f8420 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
f8430 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
f8440 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
f8450 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72  METER(pVfs);.  r
f8460 65 74 75 72 6e 20 67 65 74 4c 61 73 74 45 72 72  eturn getLastErr
f8470 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66  orMsg(nBuf, zBuf
f8480 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  );.}../*.** Init
f8490 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69  ialize and deini
f84a0 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72  tialize the oper
f84b0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74  ating system int
f84c0 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erface..*/.SQLIT
f84d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
f84e0 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b  3_os_init(void){
f84f0 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
f8500 33 5f 76 66 73 20 77 69 6e 56 66 73 20 3d 20 7b  3_vfs winVfs = {
f8510 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20  .    1,         
f8520 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
f8530 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f  ion */.    sizeo
f8540 66 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a  f(winFile),   /*
f8550 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20   szOsFile */.   
f8560 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 20 20   MAX_PATH,      
f8570 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d      /* mxPathnam
f8580 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  e */.    0,     
f8590 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
f85a0 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e  Next */.    "win
f85b0 33 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 2f  32",           /
f85c0 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30  * zName */.    0
f85d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
f85e0 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f    /* pAppData */
f85f0 0a 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20  . .    winOpen, 
f8600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70            /* xOp
f8610 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c  en */.    winDel
f8620 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ete,         /* 
f8630 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77  xDelete */.    w
f8640 69 6e 41 63 63 65 73 73 2c 20 20 20 20 20 20 20  inAccess,       
f8650 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a    /* xAccess */.
f8660 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e      winFullPathn
f8670 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50  ame,   /* xFullP
f8680 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77  athname */.    w
f8690 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20  inDlOpen,       
f86a0 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a    /* xDlOpen */.
f86b0 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20      winDlError, 
f86c0 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72         /* xDlErr
f86d0 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53  or */.    winDlS
f86e0 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ym,          /* 
f86f0 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69  xDlSym */.    wi
f8700 6e 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  nDlClose,       
f8710 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a   /* xDlClose */.
f8720 20 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73      winRandomnes
f8730 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f  s,     /* xRando
f8740 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e  mness */.    win
f8750 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20  Sleep,          
f8760 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20  /* xSleep */.   
f8770 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c   winCurrentTime,
f8780 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54      /* xCurrentT
f8790 69 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65  ime */.    winGe
f87a0 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a  tLastError    /*
f87b0 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a   xGetLastError *
f87c0 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33  /.  };.  sqlite3
f87d0 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 77  _vfs_register(&w
f87e0 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20 72 65 74  inVfs, 1);.  ret
f87f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a  urn SQLITE_OK; .
f8800 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
f8810 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
f8820 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e  void){ .  return
f8830 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
f8840 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
f8850 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  OS_WIN */../****
f8860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f8870 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a  f os_win.c *****
f8880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f88a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f88b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
f88c0 20 66 69 6c 65 20 62 69 74 76 65 63 2e 63 20 2a   file bitvec.c *
f88d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
f8900 20 32 30 30 38 20 46 65 62 72 75 61 72 79 20 31   2008 February 1
f8910 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
f8920 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
f8930 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
f8940 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
f8950 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
f8960 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
f8970 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
f8980 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
f8990 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
f89a0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
f89b0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
f89c0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
f89d0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
f89e0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
f89f0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
f8a00 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
f8a10 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
f8a20 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
f8a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a70 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
f8a80 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65  plements an obje
f8a90 63 74 20 74 68 61 74 20 72 65 70 72 65 73 65 6e  ct that represen
f8aa0 74 73 20 61 20 66 69 78 65 64 2d 6c 65 6e 67 74  ts a fixed-lengt
f8ab0 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 42 69  h.** bitmap.  Bi
f8ac0 74 73 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20  ts are numbered 
f8ad0 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e  starting with 1.
f8ae0 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20  .**.** A bitmap 
f8af0 69 73 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72  is used to recor
f8b00 64 20 77 68 69 63 68 20 70 61 67 65 73 20 6f 66  d which pages of
f8b10 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
f8b20 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f   have been.** jo
f8b30 75 72 6e 61 6c 6c 65 64 20 64 75 72 69 6e 67 20  urnalled during 
f8b40 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f  a transaction, o
f8b50 72 20 77 68 69 63 68 20 70 61 67 65 73 20 68 61  r which pages ha
f8b60 76 65 20 74 68 65 20 22 64 6f 6e 74 2d 77 72 69  ve the "dont-wri
f8b70 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e  te".** property.
f8b80 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61    Usually only a
f8b90 20 66 65 77 20 70 61 67 65 73 20 61 72 65 20 6d   few pages are m
f8ba0 65 65 74 20 65 69 74 68 65 72 20 63 6f 6e 64 69  eet either condi
f8bb0 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20  tion..** So the 
f8bc0 62 69 74 6d 61 70 20 69 73 20 75 73 75 61 6c 6c  bitmap is usuall
f8bd0 79 20 73 70 61 72 73 65 20 61 6e 64 20 68 61 73  y sparse and has
f8be0 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c 69 74 79   low cardinality
f8bf0 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d  ..** But sometim
f8c00 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20  es (for example 
f8c10 77 68 65 6e 20 64 75 72 69 6e 67 20 61 20 44 52  when during a DR
f8c20 4f 50 20 6f 66 20 61 20 6c 61 72 67 65 20 74 61  OP of a large ta
f8c30 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20  ble) most.** or 
f8c40 61 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 65 73  all of the pages
f8c50 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 63   in a database c
f8c60 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65  an get journalle
f8c70 64 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73  d.  In those cas
f8c80 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d  es, .** the bitm
f8c90 61 70 20 62 65 63 6f 6d 65 73 20 64 65 6e 73 65  ap becomes dense
f8ca0 20 77 69 74 68 20 68 69 67 68 20 63 61 72 64 69   with high cardi
f8cb0 6e 61 6c 69 74 79 2e 20 20 54 68 65 20 61 6c 67  nality.  The alg
f8cc0 6f 72 69 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a  orithm needs .**
f8cd0 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f 74 68 20   to handle both 
f8ce0 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a  cases well..**.*
f8cf0 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68  * The size of th
f8d00 65 20 62 69 74 6d 61 70 20 69 73 20 66 69 78 65  e bitmap is fixe
f8d10 64 20 77 68 65 6e 20 74 68 65 20 6f 62 6a 65 63  d when the objec
f8d20 74 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a  t is created..**
f8d30 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20 61 72 65  .** All bits are
f8d40 20 63 6c 65 61 72 20 77 68 65 6e 20 74 68 65 20   clear when the 
f8d50 62 69 74 6d 61 70 20 69 73 20 63 72 65 61 74 65  bitmap is create
f8d60 64 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 62  d.  Individual b
f8d70 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65  its.** may be se
f8d80 74 20 6f 72 20 63 6c 65 61 72 65 64 20 6f 6e 65  t or cleared one
f8d90 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a   at a time..**.*
f8da0 2a 20 54 65 73 74 20 6f 70 65 72 61 74 69 6f 6e  * Test operation
f8db0 73 20 61 72 65 20 61 62 6f 75 74 20 31 30 30 20  s are about 100 
f8dc0 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f  times more commo
f8dd0 6e 20 74 68 61 74 20 73 65 74 20 6f 70 65 72 61  n that set opera
f8de0 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20  tions..** Clear 
f8df0 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 65  operations are e
f8e00 78 63 65 65 64 69 6e 67 6c 79 20 72 61 72 65 2e  xceedingly rare.
f8e10 20 20 54 68 65 72 65 20 61 72 65 20 75 73 75 61    There are usua
f8e20 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35  lly between.** 5
f8e30 20 61 6e 64 20 35 30 30 20 73 65 74 20 6f 70 65   and 500 set ope
f8e40 72 61 74 69 6f 6e 73 20 70 65 72 20 42 69 74 76  rations per Bitv
f8e50 65 63 20 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67  ec object, thoug
f8e60 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  h the number of 
f8e70 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65  sets can.** some
f8e80 74 69 6d 65 73 20 67 72 6f 77 20 69 6e 74 6f 20  times grow into 
f8e90 74 65 6e 73 20 6f 66 20 74 68 6f 75 73 61 6e 64  tens of thousand
f8ea0 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 20 54 68  s or larger.  Th
f8eb0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a  e size of the.**
f8ec0 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 69   Bitvec object i
f8ed0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
f8ee0 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
f8ef0 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68  abase file at th
f8f00 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66 20 61 20  e.** start of a 
f8f10 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64  transaction, and
f8f20 20 69 73 20 74 68 75 73 20 75 73 75 61 6c 6c 79   is thus usually
f8f30 20 6c 65 73 73 20 74 68 61 6e 20 61 20 66 65 77   less than a few
f8f40 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75   thousand,.** bu
f8f50 74 20 63 61 6e 20 62 65 20 61 73 20 6c 61 72 67  t can be as larg
f8f60 65 20 61 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66  e as 2 billion f
f8f70 6f 72 20 61 20 72 65 61 6c 6c 79 20 62 69 67 20  or a really big 
f8f80 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
f8f90 40 28 23 29 20 24 49 64 3a 20 62 69 74 76 65 63  @(#) $Id: bitvec
f8fa0 2e 63 2c 76 20 31 2e 31 34 20 32 30 30 39 2f 30  .c,v 1.14 2009/0
f8fb0 34 2f 30 31 20 32 33 3a 34 39 3a 30 34 20 64 72  4/01 23:49:04 dr
f8fc0 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53  h Exp $.*/../* S
f8fd0 69 7a 65 20 6f 66 20 74 68 65 20 42 69 74 76 65  ize of the Bitve
f8fe0 63 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 62  c structure in b
f8ff0 79 74 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  ytes. */.#define
f9000 20 42 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20   BITVEC_SZ      
f9010 20 20 35 31 32 0a 0a 2f 2a 20 52 6f 75 6e 64 20    512../* Round 
f9020 74 68 65 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64  the union size d
f9030 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 61 72 65  own to the neare
f9040 73 74 20 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64  st pointer bound
f9050 61 72 79 2c 20 73 69 6e 63 65 20 74 68 61 74 27  ary, since that'
f9060 73 20 68 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c  s how .** it wil
f9070 6c 20 62 65 20 61 6c 69 67 6e 65 64 20 77 69 74  l be aligned wit
f9080 68 69 6e 20 74 68 65 20 42 69 74 76 65 63 20 73  hin the Bitvec s
f9090 74 72 75 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e  truct. */.#defin
f90a0 65 20 42 49 54 56 45 43 5f 55 53 49 5a 45 20 20  e BITVEC_USIZE  
f90b0 20 20 20 28 28 28 42 49 54 56 45 43 5f 53 5a 2d     (((BITVEC_SZ-
f90c0 28 33 2a 73 69 7a 65 6f 66 28 75 33 32 29 29 29  (3*sizeof(u32)))
f90d0 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29  /sizeof(Bitvec*)
f90e0 29 2a 73 69 7a 65 6f 66 28 42 69 74 76 65 63 2a  )*sizeof(Bitvec*
f90f0 29 29 0a 0a 2f 2a 20 54 79 70 65 20 6f 66 20 74  ))../* Type of t
f9100 68 65 20 61 72 72 61 79 20 22 65 6c 65 6d 65 6e  he array "elemen
f9110 74 22 20 66 6f 72 20 74 68 65 20 62 69 74 6d 61  t" for the bitma
f9120 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  p representation
f9130 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64 20 62 65 20  . .** Should be 
f9140 61 20 70 6f 77 65 72 20 6f 66 20 32 2c 20 61 6e  a power of 2, an
f9150 64 20 69 64 65 61 6c 6c 79 2c 20 65 76 65 6e 6c  d ideally, evenl
f9160 79 20 64 69 76 69 64 65 20 69 6e 74 6f 20 42 49  y divide into BI
f9170 54 56 45 43 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20  TVEC_USIZE. .** 
f9180 53 65 74 74 69 6e 67 20 74 68 69 73 20 74 6f 20  Setting this to 
f9190 74 68 65 20 22 6e 61 74 75 72 61 6c 20 77 6f 72  the "natural wor
f91a0 64 22 20 73 69 7a 65 20 6f 66 20 79 6f 75 72 20  d" size of your 
f91b0 43 50 55 20 6d 61 79 20 69 6d 70 72 6f 76 65 0a  CPU may improve.
f91c0 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20  ** performance. 
f91d0 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  */.#define BITVE
f91e0 43 5f 54 45 4c 45 4d 20 20 20 20 20 75 38 0a 2f  C_TELEM     u8./
f91f0 2a 20 53 69 7a 65 2c 20 69 6e 20 62 69 74 73 2c  * Size, in bits,
f9200 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 20 65   of the bitmap e
f9210 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23 64 65 66 69  lement. */.#defi
f9220 6e 65 20 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d  ne BITVEC_SZELEM
f9230 20 20 20 20 38 0a 2f 2a 20 4e 75 6d 62 65 72 20      8./* Number 
f9240 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61  of elements in a
f9250 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a   bitmap array. *
f9260 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43  /.#define BITVEC
f9270 5f 4e 45 4c 45 4d 20 20 20 20 20 28 42 49 54 56  _NELEM     (BITV
f9280 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28  EC_USIZE/sizeof(
f9290 42 49 54 56 45 43 5f 54 45 4c 45 4d 29 29 0a 2f  BITVEC_TELEM))./
f92a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73  * Number of bits
f92b0 20 69 6e 20 74 68 65 20 62 69 74 6d 61 70 20 61   in the bitmap a
f92c0 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  rray. */.#define
f92d0 20 42 49 54 56 45 43 5f 4e 42 49 54 20 20 20 20   BITVEC_NBIT    
f92e0 20 20 28 42 49 54 56 45 43 5f 4e 45 4c 45 4d 2a    (BITVEC_NELEM*
f92f0 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a  BITVEC_SZELEM)..
f9300 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 33 32  /* Number of u32
f9310 20 76 61 6c 75 65 73 20 69 6e 20 68 61 73 68 20   values in hash 
f9320 74 61 62 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e  table. */.#defin
f9330 65 20 42 49 54 56 45 43 5f 4e 49 4e 54 20 20 20  e BITVEC_NINT   
f9340 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45     (BITVEC_USIZE
f9350 2f 73 69 7a 65 6f 66 28 75 33 32 29 29 0a 2f 2a  /sizeof(u32))./*
f9360 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   Maximum number 
f9370 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 68 61  of entries in ha
f9380 73 68 20 74 61 62 6c 65 20 62 65 66 6f 72 65 20  sh table before 
f9390 0a 2a 2a 20 73 75 62 2d 64 69 76 69 64 69 6e 67  .** sub-dividing
f93a0 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e   and re-hashing.
f93b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56   */.#define BITV
f93c0 45 43 5f 4d 58 48 41 53 48 20 20 20 20 28 42 49  EC_MXHASH    (BI
f93d0 54 56 45 43 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20  TVEC_NINT/2)./* 
f93e0 48 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e  Hashing function
f93f0 20 66 6f 72 20 74 68 65 20 61 48 61 73 68 20 72   for the aHash r
f9400 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a  epresentation..*
f9410 2a 20 45 6d 70 69 72 69 63 61 6c 20 74 65 73 74  * Empirical test
f9420 69 6e 67 20 73 68 6f 77 65 64 20 74 68 61 74 20  ing showed that 
f9430 74 68 65 20 2a 33 37 20 6d 75 6c 74 69 70 6c 69  the *37 multipli
f9440 65 72 20 0a 2a 2a 20 28 61 6e 20 61 72 62 69 74  er .** (an arbit
f9450 72 61 72 79 20 70 72 69 6d 65 29 69 6e 20 74 68  rary prime)in th
f9460 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20  e hash function 
f9470 70 72 6f 76 69 64 65 64 20 0a 2a 2a 20 6e 6f 20  provided .** no 
f9480 66 65 77 65 72 20 63 6f 6c 6c 69 73 69 6f 6e 73  fewer collisions
f9490 20 74 68 61 6e 20 74 68 65 20 6e 6f 2d 6f 70 20   than the no-op 
f94a0 2a 31 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  *1. */.#define B
f94b0 49 54 56 45 43 5f 48 41 53 48 28 58 29 20 20 20  ITVEC_HASH(X)   
f94c0 28 28 28 58 29 2a 31 29 25 42 49 54 56 45 43 5f  (((X)*1)%BITVEC_
f94d0 4e 49 4e 54 29 0a 0a 23 64 65 66 69 6e 65 20 42  NINT)..#define B
f94e0 49 54 56 45 43 5f 4e 50 54 52 20 20 20 20 20 20  ITVEC_NPTR      
f94f0 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69  (BITVEC_USIZE/si
f9500 7a 65 6f 66 28 42 69 74 76 65 63 20 2a 29 29 0a  zeof(Bitvec *)).
f9510 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70  ../*.** A bitmap
f9520 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
f9530 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
f9540 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
f9550 2a 20 54 68 69 73 20 62 69 74 6d 61 70 20 72 65  * This bitmap re
f9560 63 6f 72 64 73 20 74 68 65 20 65 78 69 73 74 61  cords the exista
f9570 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d  nce of zero or m
f9580 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77 69 74 68  ore bits.** with
f9590 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20   values between 
f95a0 31 20 61 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63  1 and iSize, inc
f95b0 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  lusive..**.** Th
f95c0 65 72 65 20 61 72 65 20 74 68 72 65 65 20 70 6f  ere are three po
f95d0 73 73 69 62 6c 65 20 72 65 70 72 65 73 65 6e 74  ssible represent
f95e0 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 69  ations of the bi
f95f0 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a  tmap..** If iSiz
f9600 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 2c 20  e<=BITVEC_NBIT, 
f9610 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 42  then Bitvec.u.aB
f9620 69 74 6d 61 70 5b 5d 20 69 73 20 61 20 73 74 72  itmap[] is a str
f9630 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e  aight.** bitmap.
f9640 20 20 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e    The least sign
f9650 69 66 69 63 61 6e 74 20 62 69 74 20 69 73 20 62  ificant bit is b
f9660 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  it 1..**.** If i
f9670 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e 42 49 54  Size>BITVEC_NBIT
f9680 20 61 6e 64 20 69 44 69 76 69 73 6f 72 3d 3d 30   and iDivisor==0
f9690 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61   then Bitvec.u.a
f96a0 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68  Hash[] is.** a h
f96b0 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 77  ash table that w
f96c0 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 6f 20 42  ill hold up to B
f96d0 49 54 56 45 43 5f 4d 58 48 41 53 48 20 64 69 73  ITVEC_MXHASH dis
f96e0 74 69 6e 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a  tinct values..**
f96f0 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74  .** Otherwise, t
f9700 68 65 20 76 61 6c 75 65 20 69 20 69 73 20 72 65  he value i is re
f9710 64 69 72 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e  directed into on
f9720 65 20 6f 66 20 42 49 54 56 45 43 5f 4e 50 54 52  e of BITVEC_NPTR
f9730 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61 70 73 20  .** sub-bitmaps 
f9740 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 42 69  pointed to by Bi
f9750 74 76 65 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20  tvec.u.apSub[]. 
f9760 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 0a   Each subbitmap.
f9770 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70 20 74 6f  ** handles up to
f9780 20 69 44 69 76 69 73 6f 72 20 73 65 70 61 72 61   iDivisor separa
f9790 74 65 20 76 61 6c 75 65 73 20 6f 66 20 69 2e 20  te values of i. 
f97a0 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a   apSub[0] holds.
f97b0 2a 2a 20 76 61 6c 75 65 73 20 62 65 74 77 65 65  ** values betwee
f97c0 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72  n 1 and iDivisor
f97d0 2e 20 20 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64  .  apSub[1] hold
f97e0 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e  s values between
f97f0 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b 31 20 61  .** iDivisor+1 a
f9800 6e 64 20 32 2a 69 44 69 76 69 73 6f 72 2e 20 20  nd 2*iDivisor.  
f9810 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76  apSub[N] holds v
f9820 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a  alues between.**
f9830 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31 20 61 6e   N*iDivisor+1 an
f9840 64 20 28 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72  d (N+1)*iDivisor
f9850 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d 61  .  Each subbitma
f9860 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a  p is normalized.
f9870 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20  ** to hold deal 
f9880 77 69 74 68 20 76 61 6c 75 65 73 20 62 65 74 77  with values betw
f9890 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73  een 1 and iDivis
f98a0 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69  or..*/.struct Bi
f98b0 74 76 65 63 20 7b 0a 20 20 75 33 32 20 69 53 69  tvec {.  u32 iSi
f98c0 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d 61 78 69  ze;      /* Maxi
f98d0 6d 75 6d 20 62 69 74 20 69 6e 64 65 78 2e 20 20  mum bit index.  
f98e0 4d 61 78 20 69 53 69 7a 65 20 69 73 20 34 2c 32  Max iSize is 4,2
f98f0 39 34 2c 39 36 37 2c 32 39 36 2e 20 2a 2f 0a 20  94,967,296. */. 
f9900 20 75 33 32 20 6e 53 65 74 3b 20 20 20 20 20 20   u32 nSet;      
f9910 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69   /* Number of bi
f9920 74 73 20 74 68 61 74 20 61 72 65 20 73 65 74 20  ts that are set 
f9930 2d 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72  - only valid for
f9940 20 61 48 61 73 68 0a 20 20 20 20 20 20 20 20 20   aHash.         
f9950 20 20 20 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d           ** elem
f9960 65 6e 74 2e 20 20 4d 61 78 20 69 73 20 42 49 54  ent.  Max is BIT
f9970 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f 72 20 42  VEC_NINT.  For B
f9980 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c  ITVEC_SZ of 512,
f9990 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
f99a0 20 20 20 2a 2a 20 74 68 69 73 20 77 6f 75 6c 64     ** this would
f99b0 20 62 65 20 31 32 35 2e 20 2a 2f 0a 20 20 75 33   be 125. */.  u3
f99c0 32 20 69 44 69 76 69 73 6f 72 3b 20 20 20 2f 2a  2 iDivisor;   /*
f99d0 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20   Number of bits 
f99e0 68 61 6e 64 6c 65 64 20 62 79 20 65 61 63 68 20  handled by each 
f99f0 61 70 53 75 62 5b 5d 20 65 6e 74 72 79 2e 20 2a  apSub[] entry. *
f9a00 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
f9a10 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d      /* Should >=
f9a20 30 20 66 6f 72 20 61 70 53 75 62 20 65 6c 65 6d  0 for apSub elem
f9a30 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ent. */.        
f9a40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
f9a50 20 69 44 69 76 69 73 6f 72 20 69 73 20 6d 61 78   iDivisor is max
f9a60 28 75 33 32 29 20 2f 20 42 49 54 56 45 43 5f 4e  (u32) / BITVEC_N
f9a70 50 54 52 20 2b 20 31 2e 20 20 2a 2f 0a 20 20 20  PTR + 1.  */.   
f9a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f9a90 2a 20 46 6f 72 20 61 20 42 49 54 56 45 43 5f 53  * For a BITVEC_S
f9aa0 5a 20 6f 66 20 35 31 32 2c 20 74 68 69 73 20 77  Z of 512, this w
f9ab0 6f 75 6c 64 20 62 65 20 33 34 2c 33 35 39 2c 37  ould be 34,359,7
f9ac0 33 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b  39. */.  union {
f9ad0 0a 20 20 20 20 42 49 54 56 45 43 5f 54 45 4c 45  .    BITVEC_TELE
f9ae0 4d 20 61 42 69 74 6d 61 70 5b 42 49 54 56 45 43  M aBitmap[BITVEC
f9af0 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a 20 42  _NELEM];    /* B
f9b00 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61  itmap representa
f9b10 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 33 32 20  tion */.    u32 
f9b20 61 48 61 73 68 5b 42 49 54 56 45 43 5f 4e 49 4e  aHash[BITVEC_NIN
f9b30 54 5d 3b 20 20 20 20 20 20 2f 2a 20 48 61 73 68  T];      /* Hash
f9b40 20 74 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74   table represent
f9b50 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 42 69 74  ation */.    Bit
f9b60 76 65 63 20 2a 61 70 53 75 62 5b 42 49 54 56 45  vec *apSub[BITVE
f9b70 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20 52 65 63  C_NPTR];  /* Rec
f9b80 75 72 73 69 76 65 20 72 65 70 72 65 73 65 6e 74  ursive represent
f9b90 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a  ation */.  } u;.
f9ba0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  };../*.** Create
f9bb0 20 61 20 6e 65 77 20 62 69 74 6d 61 70 20 6f 62   a new bitmap ob
f9bc0 6a 65 63 74 20 61 62 6c 65 20 74 6f 20 68 61 6e  ject able to han
f9bd0 64 6c 65 20 62 69 74 73 20 62 65 74 77 65 65 6e  dle bits between
f9be0 20 30 20 61 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a   0 and iSize,.**
f9bf0 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 52 65 74   inclusive.  Ret
f9c00 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
f9c10 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e   the new object.
f9c20 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66    Return NULL if
f9c30 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c   .** malloc fail
f9c40 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
f9c50 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 71 6c  VATE Bitvec *sql
f9c60 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
f9c70 28 75 33 32 20 69 53 69 7a 65 29 7b 0a 20 20 42  (u32 iSize){.  B
f9c80 69 74 76 65 63 20 2a 70 3b 0a 20 20 61 73 73 65  itvec *p;.  asse
f9c90 72 74 28 20 73 69 7a 65 6f 66 28 2a 70 29 3d 3d  rt( sizeof(*p)==
f9ca0 42 49 54 56 45 43 5f 53 5a 20 29 3b 0a 20 20 70  BITVEC_SZ );.  p
f9cb0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
f9cc0 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29  Zero( sizeof(*p)
f9cd0 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20   );.  if( p ){. 
f9ce0 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 69 53     p->iSize = iS
f9cf0 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ize;.  }.  retur
f9d00 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  n p;.}../*.** Ch
f9d10 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
f9d20 65 20 69 2d 74 68 20 62 69 74 20 69 73 20 73 65  e i-th bit is se
f9d30 74 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20  t.  Return true 
f9d40 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20  or false..** If 
f9d50 70 20 69 73 20 4e 55 4c 4c 20 28 69 66 20 74 68  p is NULL (if th
f9d60 65 20 62 69 74 6d 61 70 20 68 61 73 20 6e 6f 74  e bitmap has not
f9d70 20 62 65 65 6e 20 63 72 65 61 74 65 64 29 20 6f   been created) o
f9d80 72 20 69 66 0a 2a 2a 20 69 20 69 73 20 6f 75 74  r if.** i is out
f9d90 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20   of range, then 
f9da0 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f  return false..*/
f9db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f9dc0 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65  int sqlite3Bitve
f9dd0 63 54 65 73 74 28 42 69 74 76 65 63 20 2a 70 2c  cTest(Bitvec *p,
f9de0 20 75 33 32 20 69 29 7b 0a 20 20 69 66 28 20 70   u32 i){.  if( p
f9df0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
f9e00 20 20 69 66 28 20 69 3e 70 2d 3e 69 53 69 7a 65    if( i>p->iSize
f9e10 20 7c 7c 20 69 3d 3d 30 20 29 20 72 65 74 75 72   || i==0 ) retur
f9e20 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68  n 0;.  i--;.  wh
f9e30 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72  ile( p->iDivisor
f9e40 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20   ){.    u32 bin 
f9e50 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b  = i/p->iDivisor;
f9e60 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44  .    i = i%p->iD
f9e70 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20  ivisor;.    p = 
f9e80 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b  p->u.apSub[bin];
f9e90 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20  .    if (!p) {. 
f9ea0 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
f9eb0 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
f9ec0 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f  ->iSize<=BITVEC_
f9ed0 4e 42 49 54 20 29 7b 0a 20 20 20 20 72 65 74 75  NBIT ){.    retu
f9ee0 72 6e 20 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70  rn (p->u.aBitmap
f9ef0 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d  [i/BITVEC_SZELEM
f9f00 5d 20 26 20 28 31 3c 3c 28 69 26 28 42 49 54 56  ] & (1<<(i&(BITV
f9f10 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 29 21  EC_SZELEM-1))))!
f9f20 3d 30 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a 20 20  =0;.  } else{.  
f9f30 20 20 75 33 32 20 68 20 3d 20 42 49 54 56 45 43    u32 h = BITVEC
f9f40 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 20 20  _HASH(i++);.    
f9f50 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73  while( p->u.aHas
f9f60 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 69 66  h[h] ){.      if
f9f70 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d  ( p->u.aHash[h]=
f9f80 3d 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  =i ) return 1;. 
f9f90 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20       h++;.      
f9fa0 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49  if( h>=BITVEC_NI
f9fb0 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20  NT ) h = 0;.    
f9fc0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  }.    return 0;.
f9fd0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74    }.}../*.** Set
f9fe0 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e 20 20   the i-th bit.  
f9ff0 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63  Return 0 on succ
fa000 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ess and an error
fa010 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74   code if.** anyt
fa020 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
fa030 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
fa040 69 6e 65 20 6d 69 67 68 74 20 63 61 75 73 65 20  ine might cause 
fa050 73 75 62 2d 62 69 74 6d 61 70 73 20 74 6f 20 62  sub-bitmaps to b
fa060 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 61  e allocated.  Fa
fa070 69 6c 69 6e 67 0a 2a 2a 20 74 6f 20 67 65 74 20  iling.** to get 
fa080 74 68 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65  the memory neede
fa090 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75  d to hold the su
fa0a0 62 2d 62 69 74 6d 61 70 20 69 73 20 74 68 65 20  b-bitmap is the 
fa0b0 6f 6e 6c 79 0a 2a 2a 20 74 68 61 74 20 63 61 6e  only.** that can
fa0c0 20 67 6f 20 77 72 6f 6e 67 20 77 69 74 68 20 61   go wrong with a
fa0d0 6e 20 69 6e 73 65 72 74 2c 20 61 73 73 75 6d 69  n insert, assumi
fa0e0 6e 67 20 70 20 61 6e 64 20 69 20 61 72 65 20 76  ng p and i are v
fa0f0 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  alid..**.** The 
fa100 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
fa110 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61   must ensure tha
fa120 74 20 70 20 69 73 20 61 20 76 61 6c 69 64 20 42  t p is a valid B
fa130 69 74 76 65 63 20 6f 62 6a 65 63 74 0a 2a 2a 20  itvec object.** 
fa140 61 6e 64 20 74 68 61 74 20 74 68 65 20 76 61 6c  and that the val
fa150 75 65 20 66 6f 72 20 22 69 22 20 69 73 20 77 69  ue for "i" is wi
fa160 74 68 69 6e 20 72 61 6e 67 65 20 6f 66 20 74 68  thin range of th
fa170 65 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e  e Bitvec object.
fa180 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 74 68  .** Otherwise th
fa190 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e  e behavior is un
fa1a0 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  defined..*/.SQLI
fa1b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
fa1c0 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
fa1d0 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69  Bitvec *p, u32 i
fa1e0 29 7b 0a 20 20 75 33 32 20 68 3b 0a 20 20 61 73  ){.  u32 h;.  as
fa1f0 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20  sert( p!=0 );.  
fa200 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20  assert( i>0 );. 
fa210 20 61 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e 69   assert( i<=p->i
fa220 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20  Size );.  i--;. 
fa230 20 77 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a 65   while((p->iSize
fa240 20 3e 20 42 49 54 56 45 43 5f 4e 42 49 54 29 20   > BITVEC_NBIT) 
fa250 26 26 20 70 2d 3e 69 44 69 76 69 73 6f 72 29 20  && p->iDivisor) 
fa260 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20  {.    u32 bin = 
fa270 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20  i/p->iDivisor;. 
fa280 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76     i = i%p->iDiv
fa290 69 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 2d  isor;.    if( p-
fa2a0 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30  >u.apSub[bin]==0
fa2b0 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 2e 61   ){.      p->u.a
fa2c0 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c 69  pSub[bin] = sqli
fa2d0 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28  te3BitvecCreate(
fa2e0 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b 0a   p->iDivisor );.
fa2f0 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61        if( p->u.a
fa300 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20 72  pSub[bin]==0 ) r
fa310 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
fa320 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20  EM;.    }.    p 
fa330 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e  = p->u.apSub[bin
fa340 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  ];.  }.  if( p->
fa350 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42  iSize<=BITVEC_NB
fa360 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61  IT ){.    p->u.a
fa370 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f  Bitmap[i/BITVEC_
fa380 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20  SZELEM] |= 1 << 
fa390 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45  (i&(BITVEC_SZELE
fa3a0 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75 72  M-1));.    retur
fa3b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
fa3c0 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41  .  h = BITVEC_HA
fa3d0 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66  SH(i++);.  /* if
fa3e0 20 74 68 65 72 65 20 77 61 73 6e 27 74 20 61 20   there wasn't a 
fa3f0 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20  hash collision, 
fa400 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27 74  and this doesn't
fa410 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65 74   */.  /* complet
fa420 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61 73  ely fill the has
fa430 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64 64  h, then just add
fa440 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a 20   it without */. 
fa450 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f 75   /* worring abou
fa460 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61  t sub-dividing a
fa470 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a  nd re-hashing. *
fa480 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61 48  /.  if( !p->u.aH
fa490 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69 66  ash[h] ){.    if
fa4a0 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56 45   (p->nSet<(BITVE
fa4b0 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20 20  C_NINT-1)) {.   
fa4c0 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73     goto bitvec_s
fa4d0 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65 6c  et_end;.    } el
fa4e0 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  se {.      goto 
fa4f0 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73  bitvec_set_rehas
fa500 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f  h;.    }.  }.  /
fa510 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 63 6f  * there was a co
fa520 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20 74  llision, check t
fa530 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 61 6c  o see if it's al
fa540 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e  ready */.  /* in
fa550 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20 74   hash, if not, t
fa560 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70 6f  ry to find a spo
fa570 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64 6f  t for it */.  do
fa580 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e   {.    if( p->u.
fa590 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65  aHash[h]==i ) re
fa5a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
fa5b0 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66 28      h++;.    if(
fa5c0 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20   h>=BITVEC_NINT 
fa5d0 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69  ) h = 0;.  } whi
fa5e0 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68  le( p->u.aHash[h
fa5f0 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69 64  ] );.  /* we did
fa600 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20 74  n't find it in t
fa610 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69 6e  he hash.  h poin
fa620 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  ts to the first 
fa630 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62 6c  */.  /* availabl
fa640 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68 65  e free spot. che
fa650 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69  ck to see if thi
fa660 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a 2f  s is going to */
fa670 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20 68  .  /* make our h
fa680 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e 20  ash too "full". 
fa690 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f 72   */.bitvec_set_r
fa6a0 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d 3e  ehash:.  if( p->
fa6b0 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58 48  nSet>=BITVEC_MXH
fa6c0 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69 67  ASH ){.    unsig
fa6d0 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 69  ned int j;.    i
fa6e0 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20 61  nt rc;.    u32 a
fa6f0 69 56 61 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e  iValues[BITVEC_N
fa700 49 4e 54 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79  INT];.    memcpy
fa710 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e  (aiValues, p->u.
fa720 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 61 69  aHash, sizeof(ai
fa730 56 61 6c 75 65 73 29 29 3b 0a 20 20 20 20 6d 65  Values));.    me
fa740 6d 73 65 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c  mset(p->u.apSub,
fa750 20 30 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c   0, sizeof(aiVal
fa760 75 65 73 29 29 3b 0a 20 20 20 20 70 2d 3e 69 44  ues));.    p->iD
fa770 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69  ivisor = (p->iSi
fa780 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52  ze + BITVEC_NPTR
fa790 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54   - 1)/BITVEC_NPT
fa7a0 52 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  R;.    rc = sqli
fa7b0 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20  te3BitvecSet(p, 
fa7c0 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b  i);.    for(j=0;
fa7d0 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20   j<BITVEC_NINT; 
fa7e0 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
fa7f0 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63  aiValues[j] ) rc
fa800 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65   |= sqlite3Bitve
fa810 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73  cSet(p, aiValues
fa820 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  [j]);.    }.    
fa830 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 62  return rc;.  }.b
fa840 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20  itvec_set_end:. 
fa850 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d   p->nSet++;.  p-
fa860 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b  >u.aHash[h] = i;
fa870 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
fa880 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
fa890 65 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74  ear the i-th bit
fa8a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
fa8b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
fa8c0 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76  BitvecClear(Bitv
fa8d0 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20  ec *p, u32 i){. 
fa8e0 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
fa8f0 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29  .  assert( i>0 )
fa900 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65  ;.  i--;.  while
fa910 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b  ( p->iDivisor ){
fa920 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69  .    u32 bin = i
fa930 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20  /p->iDivisor;.  
fa940 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69    i = i%p->iDivi
fa950 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e  sor;.    p = p->
fa960 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20  u.apSub[bin];.  
fa970 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20    if (!p) {.    
fa980 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
fa990 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69    }.  if( p->iSi
fa9a0 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20  ze<=BITVEC_NBIT 
fa9b0 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74  ){.    p->u.aBit
fa9c0 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45  map[i/BITVEC_SZE
fa9d0 4c 45 4d 5d 20 26 3d 20 7e 28 31 20 3c 3c 20 28  LEM] &= ~(1 << (
fa9e0 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d  i&(BITVEC_SZELEM
fa9f0 2d 31 29 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  -1)));.  }else{.
faa00 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
faa10 20 6a 3b 0a 20 20 20 20 75 33 32 20 61 69 56 61   j;.    u32 aiVa
faa20 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54  lues[BITVEC_NINT
faa30 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69  ];.    memcpy(ai
faa40 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61  Values, p->u.aHa
faa50 73 68 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c  sh, sizeof(aiVal
faa60 75 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65  ues));.    memse
faa70 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c  t(p->u.aHash, 0,
faa80 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73   sizeof(aiValues
faa90 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20  ));.    p->nSet 
faaa0 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30  = 0;.    for(j=0
faab0 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b  ; j<BITVEC_NINT;
faac0 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   j++){.      if(
faad0 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20   aiValues[j] && 
faae0 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b  aiValues[j]!=(i+
faaf0 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33  1) ){.        u3
fab00 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53  2 h = BITVEC_HAS
fab10 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d 31 29  H(aiValues[j]-1)
fab20 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 53 65  ;.        p->nSe
fab30 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69  t++;.        whi
fab40 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68  le( p->u.aHash[h
fab50 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 68  ] ){.          h
fab60 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ++;.          if
fab70 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54  ( h>=BITVEC_NINT
fab80 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 20 20   ) h = 0;.      
fab90 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 75    }.        p->u
faba0 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61 69 56 61  .aHash[h] = aiVa
fabb0 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d  lues[j];.      }
fabc0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
fabd0 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 62 69  .** Destroy a bi
fabe0 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 52 65  tmap object.  Re
fabf0 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79  claim all memory
fac00 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45   used..*/.SQLITE
fac10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
fac20 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
fac30 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20  oy(Bitvec *p){. 
fac40 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
fac50 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 44 69  rn;.  if( p->iDi
fac60 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 6e 73  visor ){.    uns
fac70 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20  igned int i;.   
fac80 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49 54 56   for(i=0; i<BITV
fac90 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b 0a 20  EC_NPTR; i++){. 
faca0 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
facb0 65 63 44 65 73 74 72 6f 79 28 70 2d 3e 75 2e 61  ecDestroy(p->u.a
facc0 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  pSub[i]);.    }.
facd0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
face0 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ee(p);.}../*.** 
facf0 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65  Return the value
fad00 20 6f 66 20 74 68 65 20 69 53 69 7a 65 20 70 61   of the iSize pa
fad10 72 61 6d 65 74 65 72 20 73 70 65 63 69 66 69 65  rameter specifie
fad20 64 20 77 68 65 6e 20 42 69 74 76 65 63 20 2a 70  d when Bitvec *p
fad30 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2e  .** was created.
fad40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fad50 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 42 69  TE u32 sqlite3Bi
fad60 74 76 65 63 53 69 7a 65 28 42 69 74 76 65 63 20  tvecSize(Bitvec 
fad70 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
fad80 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64  >iSize;.}..#ifnd
fad90 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
fada0 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a  UILTIN_TEST./*.*
fadb0 2a 20 4c 65 74 20 56 5b 5d 20 62 65 20 61 6e 20  * Let V[] be an 
fadc0 61 72 72 61 79 20 6f 66 20 75 6e 73 69 67 6e 65  array of unsigne
fadd0 64 20 63 68 61 72 61 63 74 65 72 73 20 73 75 66  d characters suf
fade0 66 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c 64 0a  ficient to hold.
fadf0 2a 2a 20 75 70 20 74 6f 20 4e 20 62 69 74 73 2e  ** up to N bits.
fae00 20 20 4c 65 74 20 49 20 62 65 20 61 6e 20 69 6e    Let I be an in
fae10 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 30 20  teger between 0 
fae20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a  and N.  0<=I<N..
fae30 2a 2a 20 54 68 65 6e 20 74 68 65 20 66 6f 6c 6c  ** Then the foll
fae40 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 63 61 6e  owing macros can
fae50 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 2c   be used to set,
fae60 20 63 6c 65 61 72 2c 20 6f 72 20 74 65 73 74 0a   clear, or test.
fae70 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 62 69  ** individual bi
fae80 74 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a  ts within V..*/.
fae90 23 64 65 66 69 6e 65 20 53 45 54 42 49 54 28 56  #define SETBIT(V
faea0 2c 49 29 20 20 20 20 20 20 56 5b 49 3e 3e 33 5d  ,I)      V[I>>3]
faeb0 20 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29 0a 23   |= (1<<(I&7)).#
faec0 64 65 66 69 6e 65 20 43 4c 45 41 52 42 49 54 28  define CLEARBIT(
faed0 56 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33 5d 20  V,I)    V[I>>3] 
faee0 26 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29 0a 23  &= ~(1<<(I&7)).#
faef0 64 65 66 69 6e 65 20 54 45 53 54 42 49 54 28 56  define TESTBIT(V
faf00 2c 49 29 20 20 20 20 20 28 56 5b 49 3e 3e 33 5d  ,I)     (V[I>>3]
faf10 26 28 31 3c 3c 28 49 26 37 29 29 29 21 3d 30 0a  &(1<<(I&7)))!=0.
faf20 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
faf30 69 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 74 65  ine runs an exte
faf40 6e 73 69 76 65 20 74 65 73 74 20 6f 66 20 74 68  nsive test of th
faf50 65 20 42 69 74 76 65 63 20 63 6f 64 65 2e 0a 2a  e Bitvec code..*
faf60 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69  *.** The input i
faf70 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e  s an array of in
faf80 74 65 67 65 72 73 20 74 68 61 74 20 61 63 74 73  tegers that acts
faf90 20 61 73 20 61 20 70 72 6f 67 72 61 6d 0a 2a 2a   as a program.**
fafa0 20 74 6f 20 74 65 73 74 20 74 68 65 20 42 69 74   to test the Bit
fafb0 76 65 63 2e 20 20 54 68 65 20 69 6e 74 65 67 65  vec.  The intege
fafc0 72 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66  rs are opcodes f
fafd0 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 30 2c  ollowed.** by 0,
fafe0 20 31 2c 20 6f 72 20 33 20 6f 70 65 72 61 6e 64   1, or 3 operand
faff0 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  s, depending on 
fb000 74 68 65 20 6f 70 63 6f 64 65 2e 20 20 41 6e 6f  the opcode.  Ano
fb010 74 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65 20 66  ther.** opcode f
fb020 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61 74 65  ollows immediate
fb030 6c 79 20 61 66 74 65 72 20 74 68 65 20 6c 61 73  ly after the las
fb040 74 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a  t operand..**.**
fb050 20 54 68 65 72 65 20 61 72 65 20 36 20 6f 70 63   There are 6 opc
fb060 6f 64 65 73 20 6e 75 6d 62 65 72 65 64 20 66 72  odes numbered fr
fb070 6f 6d 20 30 20 74 68 72 6f 75 67 68 20 35 2e 20  om 0 through 5. 
fb080 20 30 20 69 73 20 74 68 65 0a 2a 2a 20 22 68 61   0 is the.** "ha
fb090 6c 74 22 20 6f 70 63 6f 64 65 20 61 6e 64 20 63  lt" opcode and c
fb0a0 61 75 73 65 73 20 74 68 65 20 74 65 73 74 20 74  auses the test t
fb0b0 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  o end..**.**    
fb0c0 30 20 20 20 20 20 20 20 20 20 20 48 61 6c 74 20  0          Halt 
fb0d0 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e  and return the n
fb0e0 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 0a  umber of errors.
fb0f0 2a 2a 20 20 20 20 31 20 4e 20 53 20 58 20 20 20  **    1 N S X   
fb100 20 53 65 74 20 4e 20 62 69 74 73 20 62 65 67 69   Set N bits begi
fb110 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64  nning with S and
fb120 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79   incrementing by
fb130 20 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53 20 58   X.**    2 N S X
fb140 20 20 20 20 43 6c 65 61 72 20 4e 20 62 69 74 73      Clear N bits
fb150 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
fb160 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69  S and incrementi
fb170 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 33 20  ng by X.**    3 
fb180 4e 20 20 20 20 20 20 20 20 53 65 74 20 4e 20 72  N        Set N r
fb190 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62  andomly chosen b
fb1a0 69 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20  its.**    4 N   
fb1b0 20 20 20 20 20 43 6c 65 61 72 20 4e 20 72 61 6e       Clear N ran
fb1c0 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74  domly chosen bit
fb1d0 73 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20 58 20  s.**    5 N S X 
fb1e0 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 66 72     Set N bits fr
fb1f0 6f 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74 20 58  om S increment X
fb200 20 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20   in array only, 
fb210 6e 6f 74 20 69 6e 20 62 69 74 76 65 63 0a 2a 2a  not in bitvec.**
fb220 0a 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 73 20  .** The opcodes 
fb230 31 20 74 68 72 6f 75 67 68 20 34 20 70 65 72 66  1 through 4 perf
fb240 6f 72 6d 20 73 65 74 20 61 6e 64 20 63 6c 65 61  orm set and clea
fb250 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65  r operations are
fb260 20 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e   performed.** on
fb270 20 62 6f 74 68 20 61 20 42 69 74 76 65 63 20 6f   both a Bitvec o
fb280 62 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 20 6c  bject and on a l
fb290 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66 20 62  inear array of b
fb2a0 69 74 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  its obtained fro
fb2b0 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63  m malloc..** Opc
fb2c0 6f 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e 20 74  ode 5 works on t
fb2d0 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20  he linear array 
fb2e0 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74 68 65  only, not on the
fb2f0 20 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f   Bitvec..** Opco
fb300 64 65 20 35 20 69 73 20 75 73 65 64 20 74 6f 20  de 5 is used to 
fb310 64 65 6c 69 62 65 72 61 74 65 6c 79 20 69 6e 64  deliberately ind
fb320 75 63 65 20 61 20 66 61 75 6c 74 20 69 6e 20 6f  uce a fault in o
fb330 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69  rder to.** confi
fb340 72 6d 20 74 68 61 74 20 65 72 72 6f 72 20 64 65  rm that error de
fb350 74 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a  tection works..*
fb360 2a 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f 6e 63  *.** At the conc
fb370 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 65  lusion of the te
fb380 73 74 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72  st the linear ar
fb390 72 61 79 20 69 73 20 63 6f 6d 70 61 72 65 64 0a  ray is compared.
fb3a0 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65 20 42  ** against the B
fb3b0 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 49  itvec object.  I
fb3c0 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20  f there are any 
fb3d0 64 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20  differences,.** 
fb3e0 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  an error is retu
fb3f0 72 6e 65 64 2e 20 20 49 66 20 74 68 65 79 20 61  rned.  If they a
fb400 72 65 20 74 68 65 20 73 61 6d 65 2c 20 7a 65 72  re the same, zer
fb410 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  o is returned..*
fb420 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79  *.** If a memory
fb430 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
fb440 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e  r occurs, return
fb450 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50   -1..*/.SQLITE_P
fb460 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
fb470 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54  e3BitvecBuiltinT
fb480 65 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e 74 20  est(int sz, int 
fb490 2a 61 4f 70 29 7b 0a 20 20 42 69 74 76 65 63 20  *aOp){.  Bitvec 
fb4a0 2a 70 42 69 74 76 65 63 20 3d 20 30 3b 0a 20 20  *pBitvec = 0;.  
fb4b0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
fb4c0 56 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20  V = 0;.  int rc 
fb4d0 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c 20 6e  = -1;.  int i, n
fb4e0 78 2c 20 70 63 2c 20 6f 70 3b 0a 0a 20 20 2f 2a  x, pc, op;..  /*
fb4f0 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 42 69   Allocate the Bi
fb500 74 76 65 63 20 74 6f 20 62 65 20 74 65 73 74 65  tvec to be teste
fb510 64 20 61 6e 64 20 61 20 6c 69 6e 65 61 72 20 61  d and a linear a
fb520 72 72 61 79 20 6f 66 0a 20 20 2a 2a 20 62 69 74  rray of.  ** bit
fb530 73 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20  s to act as the 
fb540 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 70  reference */.  p
fb550 42 69 74 76 65 63 20 3d 20 73 71 6c 69 74 65 33  Bitvec = sqlite3
fb560 42 69 74 76 65 63 43 72 65 61 74 65 28 20 73 7a  BitvecCreate( sz
fb570 20 29 3b 0a 20 20 70 56 20 3d 20 73 71 6c 69 74   );.  pV = sqlit
fb580 65 33 5f 6d 61 6c 6c 6f 63 28 20 28 73 7a 2b 37  e3_malloc( (sz+7
fb590 29 2f 38 20 2b 20 31 20 29 3b 0a 20 20 69 66 28  )/8 + 1 );.  if(
fb5a0 20 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70   pBitvec==0 || p
fb5b0 56 3d 3d 30 20 29 20 67 6f 74 6f 20 62 69 74 76  V==0 ) goto bitv
fb5c0 65 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74  ec_end;.  memset
fb5d0 28 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38  (pV, 0, (sz+7)/8
fb5e0 20 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e   + 1);..  /* Run
fb5f0 20 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a   the program */.
fb600 20 20 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c    pc = 0;.  whil
fb610 65 28 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d  e( (op = aOp[pc]
fb620 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74  )!=0 ){.    swit
fb630 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20  ch( op ){.      
fb640 63 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61  case 1:.      ca
fb650 73 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65  se 2:.      case
fb660 20 35 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78   5: {.        nx
fb670 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20   = 4;.        i 
fb680 3d 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b  = aOp[pc+2] - 1;
fb690 0a 20 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b  .        aOp[pc+
fb6a0 32 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b  2] += aOp[pc+3];
fb6b0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
fb6c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
fb6d0 73 65 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65  se 3:.      case
fb6e0 20 34 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75   4: .      defau
fb6f0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78  lt: {.        nx
fb700 20 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71   = 2;.        sq
fb710 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
fb720 28 73 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b  (sizeof(i), &i);
fb730 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
fb740 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
fb750 20 20 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b    if( (--aOp[pc+
fb760 31 5d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30  1]) > 0 ) nx = 0
fb770 3b 0a 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a  ;.    pc += nx;.
fb780 20 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37      i = (i & 0x7
fb790 66 66 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20  fffffff)%sz;.   
fb7a0 20 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30   if( (op & 1)!=0
fb7b0 20 29 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54   ){.      SETBIT
fb7c0 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20  (pV, (i+1));.   
fb7d0 20 20 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a     if( op!=5 ){.
fb7e0 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
fb7f0 74 65 33 42 69 74 76 65 63 53 65 74 28 70 42 69  te3BitvecSet(pBi
fb800 74 76 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74  tvec, i+1) ) got
fb810 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20  o bitvec_end;.  
fb820 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
fb830 0a 20 20 20 20 20 20 43 4c 45 41 52 42 49 54 28  .      CLEARBIT(
fb840 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20  pV, (i+1));.    
fb850 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43    sqlite3BitvecC
fb860 6c 65 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b  lear(pBitvec, i+
fb870 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  1);.    }.  }.. 
fb880 20 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65   /* Test to make
fb890 20 73 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72   sure the linear
fb8a0 20 61 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d   array exactly m
fb8b0 61 74 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20  atches the.  ** 
fb8c0 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20  Bitvec object.  
fb8d0 53 74 61 72 74 20 77 69 74 68 20 74 68 65 20 61  Start with the a
fb8e0 73 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74  ssumption that t
fb8f0 68 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63  hey do.  ** matc
fb900 68 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e  h (rc==0).  Chan
fb910 67 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72  ge rc to non-zer
fb920 6f 20 69 66 20 61 20 64 69 73 63 72 65 70 61 6e  o if a discrepan
fb930 63 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64  cy.  ** is found
fb940 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ..  */.  rc = sq
fb950 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
fb960 30 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69  0,0) + sqlite3Bi
fb970 74 76 65 63 54 65 73 74 28 70 42 69 74 76 65 63  tvecTest(pBitvec
fb980 2c 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20  , sz+1).        
fb990 20 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65    + sqlite3Bitve
fb9a0 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 30  cTest(pBitvec, 0
fb9b0 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20 28 73  ).          + (s
fb9c0 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a 65  qlite3BitvecSize
fb9d0 28 70 42 69 74 76 65 63 29 20 2d 20 73 7a 29 3b  (pBitvec) - sz);
fb9e0 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73  .  for(i=1; i<=s
fb9f0 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  z; i++){.    if(
fba00 20 20 28 54 45 53 54 42 49 54 28 70 56 2c 69 29    (TESTBIT(pV,i)
fba10 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63  )!=sqlite3Bitvec
fba20 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 29 20  Test(pBitvec,i) 
fba30 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 3b  ){.      rc = i;
fba40 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
fba50 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72    }.  }..  /* Fr
fba60 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72  ee allocated str
fba70 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 65 63  ucture */.bitvec
fba80 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 5f  _end:.  sqlite3_
fba90 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71 6c 69  free(pV);.  sqli
fbaa0 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
fbab0 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72 65 74  (pBitvec);.  ret
fbac0 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
fbad0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
fbae0 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a  BUILTIN_TEST */.
fbaf0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
fbb00 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63 2e 63   End of bitvec.c
fbb10 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
fbb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
fbb40 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
fbb50 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 61 63   Begin file pcac
fbb60 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  he.c ***********
fbb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
fbb90 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75  ./*.** 2008 Augu
fbba0 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 05.**.** The 
fbbb0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
fbbc0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
fbbd0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
fbbe0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
fbbf0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
fbc00 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
fbc10 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
fbc20 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
fbc30 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
fbc40 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
fbc50 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
fbc60 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
fbc70 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
fbc80 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
fbc90 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
fbca0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
fbcb0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
fbcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbd00 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
fbd10 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61  e implements tha
fbd20 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a 2a 2a  t page cache..**
fbd30 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63  .** @(#) $Id: pc
fbd40 61 63 68 65 2e 63 2c 76 20 31 2e 34 34 20 32 30  ache.c,v 1.44 20
fbd50 30 39 2f 30 33 2f 33 31 20 30 31 3a 33 32 3a 31  09/03/31 01:32:1
fbd60 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  8 drh Exp $.*/..
fbd70 2f 2a 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74 65  /*.** A complete
fbd80 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61   page cache is a
fbd90 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
fbda0 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  is structure..*/
fbdb0 0a 73 74 72 75 63 74 20 50 43 61 63 68 65 20 7b  .struct PCache {
fbdc0 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79  .  PgHdr *pDirty
fbdd0 2c 20 2a 70 44 69 72 74 79 54 61 69 6c 3b 20 20  , *pDirtyTail;  
fbde0 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
fbdf0 66 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e  f dirty pages in
fbe00 20 4c 52 55 20 6f 72 64 65 72 20 2a 2f 0a 20 20   LRU order */.  
fbe10 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 3b 20  PgHdr *pSynced; 
fbe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbe30 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 79 6e 63      /* Last sync
fbe40 65 64 20 70 61 67 65 20 69 6e 20 64 69 72 74 79  ed page in dirty
fbe50 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20   page list */.  
fbe60 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20  int nRef;       
fbe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbe80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
fbe90 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65   referenced page
fbea0 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 3b  s */.  int nMax;
fbeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbec0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
fbed0 6e 66 69 67 75 72 65 64 20 63 61 63 68 65 20 73  nfigured cache s
fbee0 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50  ize */.  int szP
fbef0 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
fbf00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fbf10 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61  Size of every pa
fbf20 67 65 20 69 6e 20 74 68 69 73 20 63 61 63 68 65  ge in this cache
fbf30 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72   */.  int szExtr
fbf40 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
fbf50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
fbf60 65 20 6f 66 20 65 78 74 72 61 20 73 70 61 63 65  e of extra space
fbf70 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 2a   for each page *
fbf80 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 62  /.  int bPurgeab
fbf90 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  le;             
fbfa0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
fbfb0 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20  if pages are on 
fbfc0 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f  backing store */
fbfd0 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73  .  int (*xStress
fbfe0 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 3b  )(void*,PgHdr*);
fbff0 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74         /* Call t
fc000 6f 20 74 72 79 20 6d 61 6b 65 20 61 20 70 61 67  o try make a pag
fc010 65 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69  e clean */.  voi
fc020 64 20 2a 70 53 74 72 65 73 73 3b 20 20 20 20 20  d *pStress;     
fc030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fc040 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
fc050 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 73 71 6c  xStress */.  sql
fc060 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 43 61  ite3_pcache *pCa
fc070 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  che;            
fc080 20 2f 2a 20 50 6c 75 67 67 61 62 6c 65 20 63 61   /* Pluggable ca
fc090 63 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  che module */.  
fc0a0 50 67 48 64 72 20 2a 70 50 61 67 65 31 3b 20 20  PgHdr *pPage1;  
fc0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fc0c0 20 20 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65      /* Reference
fc0d0 20 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 7d 3b   to page 1 */.};
fc0e0 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20  ../*.** Some of 
fc0f0 74 68 65 20 61 73 73 65 72 74 28 29 20 6d 61 63  the assert() mac
fc100 72 6f 73 20 69 6e 20 74 68 69 73 20 63 6f 64 65  ros in this code
fc110 20 61 72 65 20 74 6f 6f 20 65 78 70 65 6e 73 69   are too expensi
fc120 76 65 20 74 6f 20 72 75 6e 0a 2a 2a 20 65 76 65  ve to run.** eve
fc130 6e 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20  n during normal 
fc140 64 65 62 75 67 67 69 6e 67 2e 20 20 55 73 65 20  debugging.  Use 
fc150 74 68 65 6d 20 6f 6e 6c 79 20 72 61 72 65 6c 79  them only rarely
fc160 20 6f 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67   on long-running
fc170 0a 2a 2a 20 74 65 73 74 73 2e 20 20 45 6e 61 62  .** tests.  Enab
fc180 6c 65 20 74 68 65 20 65 78 70 65 6e 73 69 76 65  le the expensive
fc190 20 61 73 73 65 72 74 73 20 75 73 69 6e 67 20 74   asserts using t
fc1a0 68 65 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 45  he.** -DSQLITE_E
fc1b0 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f  NABLE_EXPENSIVE_
fc1c0 41 53 53 45 52 54 3d 31 20 63 6f 6d 70 69 6c 65  ASSERT=1 compile
fc1d0 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f  -time option..*/
fc1e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
fc1f0 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f  NABLE_EXPENSIVE_
fc200 41 53 53 45 52 54 0a 23 20 64 65 66 69 6e 65 20  ASSERT.# define 
fc210 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74  expensive_assert
fc220 28 58 29 20 20 61 73 73 65 72 74 28 58 29 0a 23  (X)  assert(X).#
fc230 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 65 78  else.# define ex
fc240 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58  pensive_assert(X
fc250 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ).#endif../*****
fc260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fc270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69  ************* Li
fc280 6e 6b 65 64 20 4c 69 73 74 20 4d 61 6e 61 67 65  nked List Manage
fc290 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ment ***********
fc2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20  *********/..#if 
fc2b0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
fc2c0 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
fc2d0 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53  TE_ENABLE_EXPENS
fc2e0 49 56 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a 2a  IVE_ASSERT)./*.*
fc2f0 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
fc300 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64   pCache->pSynced
fc310 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
fc320 20 63 6f 72 72 65 63 74 6c 79 2e 20 49 66 20 69   correctly. If i
fc330 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 65 69 74  t.** is not, eit
fc340 68 65 72 20 66 61 69 6c 20 61 6e 20 61 73 73 65  her fail an asse
fc350 72 74 20 6f 72 20 72 65 74 75 72 6e 20 7a 65 72  rt or return zer
fc360 6f 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  o. Otherwise, re
fc370 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f  turn.** non-zero
fc380 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75  . This is only u
fc390 73 65 64 20 69 6e 20 64 65 62 75 67 67 69 6e 67  sed in debugging
fc3a0 20 62 75 69 6c 64 73 2c 20 61 73 20 66 6f 6c 6c   builds, as foll
fc3b0 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70  ows:.**.**   exp
fc3c0 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70  ensive_assert( p
fc3d0 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64  cacheCheckSynced
fc3e0 28 70 43 61 63 68 65 29 20 29 3b 0a 2a 2f 0a 73  (pCache) );.*/.s
fc3f0 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65  tatic int pcache
fc400 43 68 65 63 6b 53 79 6e 63 65 64 28 50 43 61 63  CheckSynced(PCac
fc410 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50  he *pCache){.  P
fc420 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70  gHdr *p;.  for(p
fc430 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54  =pCache->pDirtyT
fc440 61 69 6c 3b 20 70 21 3d 70 43 61 63 68 65 2d 3e  ail; p!=pCache->
fc450 70 53 79 6e 63 65 64 3b 20 70 3d 70 2d 3e 70 44  pSynced; p=p->pD
fc460 69 72 74 79 50 72 65 76 29 7b 0a 20 20 20 20 61  irtyPrev){.    a
fc470 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 20 7c  ssert( p->nRef |
fc480 7c 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44  | (p->flags&PGHD
fc490 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 3b 0a  R_NEED_SYNC) );.
fc4a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 3d    }.  return (p=
fc4b0 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c  =0 || p->nRef ||
fc4c0 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52   (p->flags&PGHDR
fc4d0 5f 4e 45 45 44 5f 53 59 4e 43 29 3d 3d 30 29 3b  _NEED_SYNC)==0);
fc4e0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e 44  .}.#endif /* !ND
fc4f0 45 42 55 47 20 26 26 20 53 51 4c 49 54 45 5f 45  EBUG && SQLITE_E
fc500 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f  NABLE_EXPENSIVE_
fc510 41 53 53 45 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ASSERT */../*.**
fc520 20 52 65 6d 6f 76 65 20 70 61 67 65 20 70 50 61   Remove page pPa
fc530 67 65 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74  ge from the list
fc540 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 2e   of dirty pages.
fc550 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
fc560 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d  pcacheRemoveFrom
fc570 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20  DirtyList(PgHdr 
fc580 2a 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68  *pPage){.  PCach
fc590 65 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43  e *p = pPage->pC
fc5a0 61 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  ache;..  assert(
fc5b0 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65   pPage->pDirtyNe
fc5c0 78 74 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d 3e  xt || pPage==p->
fc5d0 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20  pDirtyTail );.  
fc5e0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
fc5f0 44 69 72 74 79 50 72 65 76 20 7c 7c 20 70 50 61  DirtyPrev || pPa
fc600 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29 3b  ge==p->pDirty );
fc610 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68  ..  /* Update th
fc620 65 20 50 43 61 63 68 65 31 2e 70 53 79 6e 63 65  e PCache1.pSynce
fc630 64 20 76 61 72 69 61 62 6c 65 20 69 66 20 6e 65  d variable if ne
fc640 63 65 73 73 61 72 79 2e 20 2a 2f 0a 20 20 69 66  cessary. */.  if
fc650 28 20 70 2d 3e 70 53 79 6e 63 65 64 3d 3d 70 50  ( p->pSynced==pP
fc660 61 67 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72  age ){.    PgHdr
fc670 20 2a 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67   *pSynced = pPag
fc680 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20  e->pDirtyPrev;. 
fc690 20 20 20 77 68 69 6c 65 28 20 70 53 79 6e 63 65     while( pSynce
fc6a0 64 20 26 26 20 28 70 53 79 6e 63 65 64 2d 3e 66  d && (pSynced->f
fc6b0 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f  lags&PGHDR_NEED_
fc6c0 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 20 20 70  SYNC) ){.      p
fc6d0 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 64  Synced = pSynced
fc6e0 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20  ->pDirtyPrev;.  
fc6f0 20 20 7d 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63    }.    p->pSync
fc700 65 64 20 3d 20 70 53 79 6e 63 65 64 3b 0a 20 20  ed = pSynced;.  
fc710 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  }..  if( pPage->
fc720 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 20  pDirtyNext ){.  
fc730 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e    pPage->pDirtyN
fc740 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76 20  ext->pDirtyPrev 
fc750 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50  = pPage->pDirtyP
fc760 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  rev;.  }else{.  
fc770 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d    assert( pPage=
fc780 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29  =p->pDirtyTail )
fc790 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54  ;.    p->pDirtyT
fc7a0 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 44 69  ail = pPage->pDi
fc7b0 72 74 79 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69  rtyPrev;.  }.  i
fc7c0 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79  f( pPage->pDirty
fc7d0 50 72 65 76 20 29 7b 0a 20 20 20 20 70 50 61 67  Prev ){.    pPag
fc7e0 65 2d 3e 70 44 69 72 74 79 50 72 65 76 2d 3e 70  e->pDirtyPrev->p
fc7f0 44 69 72 74 79 4e 65 78 74 20 3d 20 70 50 61 67  DirtyNext = pPag
fc800 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20  e->pDirtyNext;. 
fc810 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
fc820 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44  rt( pPage==p->pD
fc830 69 72 74 79 20 29 3b 0a 20 20 20 20 70 2d 3e 70  irty );.    p->p
fc840 44 69 72 74 79 20 3d 20 70 50 61 67 65 2d 3e 70  Dirty = pPage->p
fc850 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20  DirtyNext;.  }. 
fc860 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65   pPage->pDirtyNe
fc870 78 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d  xt = 0;.  pPage-
fc880 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 30 3b  >pDirtyPrev = 0;
fc890 0a 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73  ..  expensive_as
fc8a0 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63  sert( pcacheChec
fc8b0 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a  kSynced(p) );.}.
fc8c0 0a 2f 2a 0a 2a 2a 20 41 64 64 20 70 61 67 65 20  ./*.** Add page 
fc8d0 70 50 61 67 65 20 74 6f 20 74 68 65 20 68 65 61  pPage to the hea
fc8e0 64 20 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c  d of the dirty l
fc8f0 69 73 74 20 28 50 43 61 63 68 65 31 2e 70 44 69  ist (PCache1.pDi
fc900 72 74 79 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a  rty is set to.**
fc910 20 70 50 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74   pPage)..*/.stat
fc920 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 41 64  ic void pcacheAd
fc930 64 54 6f 44 69 72 74 79 4c 69 73 74 28 50 67 48  dToDirtyList(PgH
fc940 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 43  dr *pPage){.  PC
fc950 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67 65 2d  ache *p = pPage-
fc960 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 65  >pCache;..  asse
fc970 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74  rt( pPage->pDirt
fc980 79 4e 65 78 74 3d 3d 30 20 26 26 20 70 50 61 67  yNext==0 && pPag
fc990 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30  e->pDirtyPrev==0
fc9a0 20 26 26 20 70 2d 3e 70 44 69 72 74 79 21 3d 70   && p->pDirty!=p
fc9b0 50 61 67 65 20 29 3b 0a 0a 20 20 70 50 61 67 65  Page );..  pPage
fc9c0 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 70  ->pDirtyNext = p
fc9d0 2d 3e 70 44 69 72 74 79 3b 0a 20 20 69 66 28 20  ->pDirty;.  if( 
fc9e0 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
fc9f0 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  t ){.    assert(
fca00 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65   pPage->pDirtyNe
fca10 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d  xt->pDirtyPrev==
fca20 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  0 );.    pPage->
fca30 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72  pDirtyNext->pDir
fca40 74 79 50 72 65 76 20 3d 20 70 50 61 67 65 3b 0a  tyPrev = pPage;.
fca50 20 20 7d 0a 20 20 70 2d 3e 70 44 69 72 74 79 20    }.  p->pDirty 
fca60 3d 20 70 50 61 67 65 3b 0a 20 20 69 66 28 20 21  = pPage;.  if( !
fca70 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29 7b  p->pDirtyTail ){
fca80 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61  .    p->pDirtyTa
fca90 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a  il = pPage;.  }.
fcaa0 20 20 69 66 28 20 21 70 2d 3e 70 53 79 6e 63 65    if( !p->pSynce
fcab0 64 20 26 26 20 30 3d 3d 28 70 50 61 67 65 2d 3e  d && 0==(pPage->
fcac0 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
fcad0 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 2d  _SYNC) ){.    p-
fcae0 3e 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65  >pSynced = pPage
fcaf0 3b 0a 20 20 7d 0a 20 20 65 78 70 65 6e 73 69 76  ;.  }.  expensiv
fcb00 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65  e_assert( pcache
fcb10 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20 29  CheckSynced(p) )
fcb20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70  ;.}../*.** Wrapp
fcb30 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 70 6c  er around the pl
fcb40 75 67 67 61 62 6c 65 20 63 61 63 68 65 73 20 78  uggable caches x
fcb50 55 6e 70 69 6e 20 6d 65 74 68 6f 64 2e 20 49 66  Unpin method. If
fcb60 20 74 68 65 20 63 61 63 68 65 20 69 73 0a 2a 2a   the cache is.**
fcb70 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20   being used for 
fcb80 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
fcb90 61 62 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63  abase, this func
fcba0 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
fcbb0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
fcbc0 70 63 61 63 68 65 55 6e 70 69 6e 28 50 67 48 64  pcacheUnpin(PgHd
fcbd0 72 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20  r *p){.  PCache 
fcbe0 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61  *pCache = p->pCa
fcbf0 63 68 65 3b 0a 20 20 69 66 28 20 70 43 61 63 68  che;.  if( pCach
fcc00 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b  e->bPurgeable ){
fcc10 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f  .    if( p->pgno
fcc20 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61  ==1 ){.      pCa
fcc30 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b  che->pPage1 = 0;
fcc40 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
fcc50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
fcc60 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61  cache.xUnpin(pCa
fcc70 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20  che->pCache, p, 
fcc80 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a  0);.  }.}../****
fcc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fccb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
fccc0 47 65 6e 65 72 61 6c 20 49 6e 74 65 72 66 61 63  General Interfac
fccd0 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  es ******.**.** 
fcce0 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73  Initialize and s
fccf0 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65  hutdown the page
fcd00 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d   cache subsystem
fcd10 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65  . Neither of the
fcd20 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  se .** functions
fcd30 20 61 72 65 20 74 68 72 65 61 64 73 61 66 65 2e   are threadsafe.
fcd40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fcd50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
fcd60 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76  acheInitialize(v
fcd70 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69  oid){.  if( sqli
fcd80 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
fcd90 70 63 61 63 68 65 2e 78 49 6e 69 74 3d 3d 30 20  pcache.xInit==0 
fcda0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 43  ){.    sqlite3PC
fcdb0 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 29  acheSetDefault()
fcdc0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
fcdd0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
fcde0 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 28  ig.pcache.xInit(
fcdf0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
fce00 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67 29  fig.pcache.pArg)
fce10 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
fce20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
fce30 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 6f  cacheShutdown(vo
fce40 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74  id){.  if( sqlit
fce50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
fce60 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20  cache.xShutdown 
fce70 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
fce80 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
fce90 65 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69  e.xShutdown(sqli
fcea0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
fceb0 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 20 20  pcache.pArg);.  
fcec0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
fced0 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79  n the size in by
fcee0 74 65 73 20 6f 66 20 61 20 50 43 61 63 68 65 20  tes of a PCache 
fcef0 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54  object..*/.SQLIT
fcf00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
fcf10 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28  lite3PcacheSize(
fcf20 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73 69  void){ return si
fcf30 7a 65 6f 66 28 50 43 61 63 68 65 29 3b 20 7d 0a  zeof(PCache); }.
fcf40 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
fcf50 6e 65 77 20 50 43 61 63 68 65 20 6f 62 6a 65 63  new PCache objec
fcf60 74 2e 20 53 74 6f 72 61 67 65 20 73 70 61 63 65  t. Storage space
fcf70 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a   to hold the obj
fcf80 65 63 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61  ect.** has alrea
fcf90 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  dy been allocate
fcfa0 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 20  d and is passed 
fcfb0 69 6e 20 61 73 20 74 68 65 20 70 20 70 6f 69 6e  in as the p poin
fcfc0 74 65 72 2e 20 0a 2a 2a 20 54 68 65 20 63 61 6c  ter. .** The cal
fcfd0 6c 65 72 20 64 69 73 63 6f 76 65 72 73 20 68 6f  ler discovers ho
fcfe0 77 20 6d 75 63 68 20 73 70 61 63 65 20 6e 65 65  w much space nee
fcff0 64 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74  ds to be allocat
fd000 65 64 20 62 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e  ed by .** callin
fd010 67 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  g sqlite3PcacheS
fd020 69 7a 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ize()..*/.SQLITE
fd030 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
fd040 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28  lite3PcacheOpen(
fd050 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20  .  int szPage,  
fd060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd070 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79  /* Size of every
fd080 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73   page */.  int s
fd090 7a 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20  zExtra,         
fd0a0 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61          /* Extra
fd0b0 20 73 70 61 63 65 20 61 73 73 6f 63 69 61 74 65   space associate
fd0c0 64 20 77 69 74 68 20 65 61 63 68 20 70 61 67 65  d with each page
fd0d0 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65   */.  int bPurge
fd0e0 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20  able,           
fd0f0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61     /* True if pa
fd100 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69  ges are on backi
fd110 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e  ng store */.  in
fd120 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69  t (*xStress)(voi
fd130 64 2a 2c 50 67 48 64 72 2a 29 2c 2f 2a 20 43 61  d*,PgHdr*),/* Ca
fd140 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b  ll to try to mak
fd150 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f  e pages clean */
fd160 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73  .  void *pStress
fd170 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
fd180 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78  /* Argument to x
fd190 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63  Stress */.  PCac
fd1a0 68 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20  he *p           
fd1b0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61           /* Prea
fd1c0 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66  llocated space f
fd1d0 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f  or the PCache */
fd1e0 0a 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20  .){.  memset(p, 
fd1f0 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65  0, sizeof(PCache
fd200 29 29 3b 0a 20 20 70 2d 3e 73 7a 50 61 67 65 20  ));.  p->szPage 
fd210 3d 20 73 7a 50 61 67 65 3b 0a 20 20 70 2d 3e 73  = szPage;.  p->s
fd220 7a 45 78 74 72 61 20 3d 20 73 7a 45 78 74 72 61  zExtra = szExtra
fd230 3b 0a 20 20 70 2d 3e 62 50 75 72 67 65 61 62 6c  ;.  p->bPurgeabl
fd240 65 20 3d 20 62 50 75 72 67 65 61 62 6c 65 3b 0a  e = bPurgeable;.
fd250 20 20 70 2d 3e 78 53 74 72 65 73 73 20 3d 20 78    p->xStress = x
fd260 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 70 53 74  Stress;.  p->pSt
fd270 72 65 73 73 20 3d 20 70 53 74 72 65 73 73 3b 0a  ress = pStress;.
fd280 20 20 70 2d 3e 6e 4d 61 78 20 3d 20 31 30 30 3b    p->nMax = 100;
fd290 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
fd2a0 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 66   the page size f
fd2b0 6f 72 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74  or PCache object
fd2c0 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  . The caller mus
fd2d0 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  t ensure that th
fd2e0 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6f 75  ere.** are no ou
fd2f0 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72  tstanding page r
fd300 65 66 65 72 65 6e 63 65 73 20 77 68 65 6e 20 74  eferences when t
fd310 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
fd320 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  called..*/.SQLIT
fd330 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
fd340 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 50  qlite3PcacheSetP
fd350 61 67 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a  ageSize(PCache *
fd360 70 43 61 63 68 65 2c 20 69 6e 74 20 73 7a 50 61  pCache, int szPa
fd370 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  ge){.  assert( p
fd380 43 61 63 68 65 2d 3e 6e 52 65 66 3d 3d 30 20 26  Cache->nRef==0 &
fd390 26 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  & pCache->pDirty
fd3a0 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 61  ==0 );.  if( pCa
fd3b0 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20  che->pCache ){. 
fd3c0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
fd3d0 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 44  Config.pcache.xD
fd3e0 65 73 74 72 6f 79 28 70 43 61 63 68 65 2d 3e 70  estroy(pCache->p
fd3f0 43 61 63 68 65 29 3b 0a 20 20 20 20 70 43 61 63  Cache);.    pCac
fd400 68 65 2d 3e 70 43 61 63 68 65 20 3d 20 30 3b 0a  he->pCache = 0;.
fd410 20 20 7d 0a 20 20 70 43 61 63 68 65 2d 3e 73 7a    }.  pCache->sz
fd420 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 7d  Page = szPage;.}
fd430 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f  ../*.** Try to o
fd440 62 74 61 69 6e 20 61 20 70 61 67 65 20 66 72 6f  btain a page fro
fd450 6d 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a  m the cache..*/.
fd460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
fd470 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
fd480 46 65 74 63 68 28 0a 20 20 50 43 61 63 68 65 20  Fetch(.  PCache 
fd490 2a 70 43 61 63 68 65 2c 20 20 20 20 20 20 20 2f  *pCache,       /
fd4a0 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67  * Obtain the pag
fd4b0 65 20 66 72 6f 6d 20 74 68 69 73 20 63 61 63 68  e from this cach
fd4c0 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
fd4d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
fd4e0 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6f  Page number to o
fd4f0 62 74 61 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 63  btain */.  int c
fd500 72 65 61 74 65 46 6c 61 67 2c 20 20 20 20 20 20  reateFlag,      
fd510 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 63 72 65   /* If true, cre
fd520 61 74 65 20 70 61 67 65 20 69 66 20 69 74 20 64  ate page if it d
fd530 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 61 6c  oes not exist al
fd540 72 65 61 64 79 20 2a 2f 0a 20 20 50 67 48 64 72  ready */.  PgHdr
fd550 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20 20 20   **ppPage       
fd560 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 61   /* Write the pa
fd570 67 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ge here */.){.  
fd580 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 30  PgHdr *pPage = 0
fd590 3b 0a 20 20 69 6e 74 20 65 43 72 65 61 74 65 3b  ;.  int eCreate;
fd5a0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63  ..  assert( pCac
fd5b0 68 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  he!=0 );.  asser
fd5c0 74 28 20 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20 20  t( pgno>0 );..  
fd5d0 2f 2a 20 49 66 20 74 68 65 20 70 6c 75 67 67 61  /* If the plugga
fd5e0 62 6c 65 20 63 61 63 68 65 20 28 73 71 6c 69 74  ble cache (sqlit
fd5f0 65 33 5f 70 63 61 63 68 65 2a 29 20 68 61 73 20  e3_pcache*) has 
fd600 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  not been allocat
fd610 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  ed,.  ** allocat
fd620 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20  e it now..  */. 
fd630 20 69 66 28 20 21 70 43 61 63 68 65 2d 3e 70 43   if( !pCache->pC
fd640 61 63 68 65 20 26 26 20 63 72 65 61 74 65 46 6c  ache && createFl
fd650 61 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ag ){.    sqlite
fd660 33 5f 70 63 61 63 68 65 20 2a 70 3b 0a 20 20 20  3_pcache *p;.   
fd670 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20   int nByte;.    
fd680 6e 42 79 74 65 20 3d 20 70 43 61 63 68 65 2d 3e  nByte = pCache->
fd690 73 7a 50 61 67 65 20 2b 20 70 43 61 63 68 65 2d  szPage + pCache-
fd6a0 3e 73 7a 45 78 74 72 61 20 2b 20 73 69 7a 65 6f  >szExtra + sizeo
fd6b0 66 28 50 67 48 64 72 29 3b 0a 20 20 20 20 70 20  f(PgHdr);.    p 
fd6c0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
fd6d0 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43 72  onfig.pcache.xCr
fd6e0 65 61 74 65 28 6e 42 79 74 65 2c 20 70 43 61 63  eate(nByte, pCac
fd6f0 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29 3b  he->bPurgeable);
fd700 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20  .    if( !p ){. 
fd710 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
fd720 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
fd730 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
fd740 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78  lConfig.pcache.x
fd750 43 61 63 68 65 73 69 7a 65 28 70 2c 20 70 43 61  Cachesize(p, pCa
fd760 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 20  che->nMax);.    
fd770 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 3d  pCache->pCache =
fd780 20 70 3b 0a 20 20 7d 0a 0a 20 20 65 43 72 65 61   p;.  }..  eCrea
fd790 74 65 20 3d 20 63 72 65 61 74 65 46 6c 61 67 20  te = createFlag 
fd7a0 3f 20 31 20 3a 20 30 3b 0a 20 20 69 66 28 20 65  ? 1 : 0;.  if( e
fd7b0 43 72 65 61 74 65 20 26 26 20 28 21 70 43 61 63  Create && (!pCac
fd7c0 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c  he->bPurgeable |
fd7d0 7c 20 21 70 43 61 63 68 65 2d 3e 70 44 69 72 74  | !pCache->pDirt
fd7e0 79 29 20 29 7b 0a 20 20 20 20 65 43 72 65 61 74  y) ){.    eCreat
fd7f0 65 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28  e = 2;.  }.  if(
fd800 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
fd810 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73  ){.    pPage = s
fd820 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
fd830 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68  ig.pcache.xFetch
fd840 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
fd850 20 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b   pgno, eCreate);
fd860 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61  .  }..  if( !pPa
fd870 67 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31  ge && eCreate==1
fd880 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70   ){.    PgHdr *p
fd890 50 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64  Pg;..    /* Find
fd8a0 20 61 20 64 69 72 74 79 20 70 61 67 65 20 74 6f   a dirty page to
fd8b0 20 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72   write-out and r
fd8c0 65 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72  ecycle. First tr
fd8d0 79 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20  y to find a .   
fd8e0 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f   ** page that do
fd8f0 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61  es not require a
fd900 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f   journal-sync (o
fd910 6e 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45  ne with PGHDR_NE
fd920 45 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63  ED_SYNC.    ** c
fd930 6c 65 61 72 65 64 29 2c 20 62 75 74 20 69 66 20  leared), but if 
fd940 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  that is not poss
fd950 69 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20  ible settle for 
fd960 61 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a  any other .    *
fd970 2a 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64  * unreferenced d
fd980 69 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a  irty page..    *
fd990 2f 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f  /.    expensive_
fd9a0 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68  assert( pcacheCh
fd9b0 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65  eckSynced(pCache
fd9c0 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67  ) );.    for(pPg
fd9d0 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64  =pCache->pSynced
fd9e0 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 20 26  ; .        pPg &
fd9f0 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20  & (pPg->nRef || 
fda00 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44  (pPg->flags&PGHD
fda10 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a  R_NEED_SYNC)); .
fda20 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d          pPg=pPg-
fda30 3e 70 44 69 72 74 79 50 72 65 76 0a 20 20 20 20  >pDirtyPrev.    
fda40 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 67 20  );.    if( !pPg 
fda50 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 70 50 67  ){.      for(pPg
fda60 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54  =pCache->pDirtyT
fda70 61 69 6c 3b 20 70 50 67 20 26 26 20 70 50 67 2d  ail; pPg && pPg-
fda80 3e 6e 52 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e  >nRef; pPg=pPg->
fda90 70 44 69 72 74 79 50 72 65 76 29 3b 0a 20 20 20  pDirtyPrev);.   
fdaa0 20 7d 0a 20 20 20 20 69 66 28 20 70 50 67 20 29   }.    if( pPg )
fdab0 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a  {.      int rc;.
fdac0 20 20 20 20 20 20 72 63 20 3d 20 70 43 61 63 68        rc = pCach
fdad0 65 2d 3e 78 53 74 72 65 73 73 28 70 43 61 63 68  e->xStress(pCach
fdae0 65 2d 3e 70 53 74 72 65 73 73 2c 20 70 50 67 29  e->pStress, pPg)
fdaf0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
fdb00 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
fdb10 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a  =SQLITE_BUSY ){.
fdb20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
fdb30 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
fdb40 0a 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71  ..    pPage = sq
fdb50 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
fdb60 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28  g.pcache.xFetch(
fdb70 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
fdb80 70 67 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20  pgno, 2);.  }.. 
fdb90 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20   if( pPage ){.  
fdba0 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70 44    if( !pPage->pD
fdbb0 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ata ){.      mem
fdbc0 73 65 74 28 70 50 61 67 65 2c 20 30 2c 20 73 69  set(pPage, 0, si
fdbd0 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43  zeof(PgHdr) + pC
fdbe0 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a  ache->szExtra);.
fdbf0 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78        pPage->pEx
fdc00 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 50  tra = (void*)&pP
fdc10 61 67 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50  age[1];.      pP
fdc20 61 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f  age->pData = (vo
fdc30 69 64 20 2a 29 26 28 28 63 68 61 72 20 2a 29 70  id *)&((char *)p
fdc40 50 61 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 48  Page)[sizeof(PgH
fdc50 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a  dr) + pCache->sz
fdc60 45 78 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 50  Extra];.      pP
fdc70 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43  age->pCache = pC
fdc80 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50 61 67  ache;.      pPag
fdc90 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a  e->pgno = pgno;.
fdca0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
fdcb0 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d  ( pPage->pCache=
fdcc0 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 20 20 61  =pCache );.    a
fdcd0 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67  ssert( pPage->pg
fdce0 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20  no==pgno );.    
fdcf0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
fdd00 45 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26  Extra==(void *)&
fdd10 70 50 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20  pPage[1] );..   
fdd20 20 69 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e   if( 0==pPage->n
fdd30 52 65 66 20 29 7b 0a 20 20 20 20 20 20 70 43 61  Ref ){.      pCa
fdd40 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20  che->nRef++;.   
fdd50 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 52   }.    pPage->nR
fdd60 65 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 67  ef++;.    if( pg
fdd70 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70  no==1 ){.      p
fdd80 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20  Cache->pPage1 = 
fdd90 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d  pPage;.    }.  }
fdda0 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61  .  *ppPage = pPa
fddb0 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50  ge;.  return (pP
fddc0 61 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61 74  age==0 && eCreat
fddd0 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  e) ? SQLITE_NOME
fdde0 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  M : SQLITE_OK;.}
fddf0 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e  ../*.** Decremen
fde00 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  t the reference 
fde10 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e  count on a page.
fde20 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20   If the page is 
fde30 63 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a  clean and the.**
fde40 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
fde50 20 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 65   drops to 0, the
fde60 6e 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c 69  n it is made eli
fde70 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e  ble for recyclin
fde80 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
fde90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
fdea0 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50  3PcacheRelease(P
fdeb0 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65  gHdr *p){.  asse
fdec0 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b  rt( p->nRef>0 );
fded0 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  .  p->nRef--;.  
fdee0 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  if( p->nRef==0 )
fdef0 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70 43  {.    PCache *pC
fdf00 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65  ache = p->pCache
fdf10 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52  ;.    pCache->nR
fdf20 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 70  ef--;.    if( (p
fdf30 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
fdf40 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  RTY)==0 ){.     
fdf50 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b   pcacheUnpin(p);
fdf60 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
fdf70 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61    /* Move the pa
fdf80 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f  ge to the head o
fdf90 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74  f the dirty list
fdfa0 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 68  . */.      pcach
fdfb0 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79  eRemoveFromDirty
fdfc0 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70  List(p);.      p
fdfd0 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c  cacheAddToDirtyL
fdfe0 69 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20  ist(p);.    }.  
fdff0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65  }.}../*.** Incre
fe000 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e 63  ase the referenc
fe010 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 70  e count of a sup
fe020 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 31 2e  plied page by 1.
fe030 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fe040 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
fe050 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20 2a  cacheRef(PgHdr *
fe060 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d 3e  p){.  assert(p->
fe070 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52  nRef>0);.  p->nR
fe080 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ef++;.}../*.** D
fe090 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d 20  rop a page from 
fe0a0 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72 65  the cache. There
fe0b0 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79   must be exactly
fe0c0 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74   one reference t
fe0d0 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54  o the.** page. T
fe0e0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c  his function del
fe0f0 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72 65  etes that refere
fe100 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69 74  nce, so after it
fe110 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20   returns the.** 
fe120 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  page pointed to 
fe130 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64 2e  by p is invalid.
fe140 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fe150 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
fe160 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 20  cacheDrop(PgHdr 
fe170 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70  *p){.  PCache *p
fe180 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 28  Cache;.  assert(
fe190 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20   p->nRef==1 );. 
fe1a0 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 47   if( p->flags&PG
fe1b0 48 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 20  HDR_DIRTY ){.   
fe1c0 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f   pcacheRemoveFro
fe1d0 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20  mDirtyList(p);. 
fe1e0 20 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70 2d   }.  pCache = p-
fe1f0 3e 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63 68  >pCache;.  pCach
fe200 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28  e->nRef--;.  if(
fe210 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20   p->pgno==1 ){. 
fe220 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65     pCache->pPage
fe230 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  1 = 0;.  }.  sql
fe240 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
fe250 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70  .pcache.xUnpin(p
fe260 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70  Cache->pCache, p
fe270 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  , 1);.}../*.** M
fe280 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67  ake sure the pag
fe290 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64  e is marked as d
fe2a0 69 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27  irty. If it isn'
fe2b0 74 20 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c  t dirty already,
fe2c0 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a  .** make it so..
fe2d0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
fe2e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
fe2f0 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67  acheMakeDirty(Pg
fe300 48 64 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c  Hdr *p){.  p->fl
fe310 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f  ags &= ~PGHDR_DO
fe320 4e 54 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65  NT_WRITE;.  asse
fe330 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b  rt( p->nRef>0 );
fe340 0a 20 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c  .  if( 0==(p->fl
fe350 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54  ags & PGHDR_DIRT
fe360 59 29 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61  Y) ){.    p->fla
fe370 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54  gs |= PGHDR_DIRT
fe380 59 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64 64  Y;.    pcacheAdd
fe390 54 6f 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b  ToDirtyList( p);
fe3a0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  .  }.}../*.** Ma
fe3b0 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65  ke sure the page
fe3c0 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c   is marked as cl
fe3d0 65 61 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74  ean. If it isn't
fe3e0 20 63 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a   clean already,.
fe3f0 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a  ** make it so..*
fe400 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
fe410 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
fe420 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48  cheMakeClean(PgH
fe430 64 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70  dr *p){.  if( (p
fe440 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f  ->flags & PGHDR_
fe450 44 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63  DIRTY) ){.    pc
fe460 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69  acheRemoveFromDi
fe470 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20  rtyList(p);.    
fe480 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47  p->flags &= ~(PG
fe490 48 44 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f  HDR_DIRTY|PGHDR_
fe4a0 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20  NEED_SYNC);.    
fe4b0 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  if( p->nRef==0 )
fe4c0 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 6e  {.      pcacheUn
fe4d0 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20  pin(p);.    }.  
fe4e0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  }.}../*.** Make 
fe4f0 65 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68  every page in th
fe500 65 20 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a  e cache clean..*
fe510 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
fe520 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
fe530 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63  cheCleanAll(PCac
fe540 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50  he *pCache){.  P
fe550 67 48 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65  gHdr *p;.  while
fe560 28 20 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70  ( (p = pCache->p
fe570 44 69 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20  Dirty)!=0 ){.   
fe580 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61   sqlite3PcacheMa
fe590 6b 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a  keClean(p);.  }.
fe5a0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74  }../*.** Clear t
fe5b0 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
fe5c0 4e 43 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c  NC flag from all
fe5d0 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f   dirty pages..*/
fe5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
fe5f0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
fe600 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73  heClearSyncFlags
fe610 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
fe620 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20  {.  PgHdr *p;.  
fe630 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44  for(p=pCache->pD
fe640 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44  irty; p; p=p->pD
fe650 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70  irtyNext){.    p
fe660 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44  ->flags &= ~PGHD
fe670 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d  R_NEED_SYNC;.  }
fe680 0a 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63  .  pCache->pSync
fe690 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69  ed = pCache->pDi
fe6a0 72 74 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a  rtyTail;.}../*.*
fe6b0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 67  * Change the pag
fe6c0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
fe6d0 20 70 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a   p to newPgno. .
fe6e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
fe6f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
fe700 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a  acheMove(PgHdr *
fe710 70 2c 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29  p, Pgno newPgno)
fe720 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63  {.  PCache *pCac
fe730 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a  he = p->pCache;.
fe740 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
fe750 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  f>0 );.  assert(
fe760 20 6e 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20   newPgno>0 );.  
fe770 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
fe780 66 69 67 2e 70 63 61 63 68 65 2e 78 52 65 6b 65  fig.pcache.xReke
fe790 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  y(pCache->pCache
fe7a0 2c 20 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65  , p, p->pgno, ne
fe7b0 77 50 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e  wPgno);.  p->pgn
fe7c0 6f 20 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69  o = newPgno;.  i
fe7d0 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48  f( (p->flags&PGH
fe7e0 44 52 5f 44 49 52 54 59 29 20 26 26 20 28 70 2d  DR_DIRTY) && (p-
fe7f0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
fe800 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70  D_SYNC) ){.    p
fe810 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44  cacheRemoveFromD
fe820 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20  irtyList(p);.   
fe830 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74   pcacheAddToDirt
fe840 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a  yList(p);.  }.}.
fe850 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72  ./*.** Drop ever
fe860 79 20 63 61 63 68 65 20 65 6e 74 72 79 20 77 68  y cache entry wh
fe870 6f 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ose page number 
fe880 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
fe890 22 70 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63  "pgno". The.** c
fe8a0 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72  aller must ensur
fe8b0 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65  e that there are
fe8c0 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   no outstanding 
fe8d0 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e  references to an
fe8e0 79 20 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72  y pages.** other
fe8f0 20 74 68 61 6e 20 70 61 67 65 20 31 20 77 69 74   than page 1 wit
fe900 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20  h a page number 
fe910 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 67 6e  greater than pgn
fe920 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  o..**.** If ther
fe930 65 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65  e is a reference
fe940 20 74 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74   to page 1 and t
fe950 68 65 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65  he pgno paramete
fe960 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  r passed to this
fe970 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  .** function is 
fe980 30 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  0, then the data
fe990 20 61 72 65 61 20 61 73 73 6f 63 69 61 74 65 64   area associated
fe9a0 20 77 69 74 68 20 70 61 67 65 20 31 20 69 73 20   with page 1 is 
fe9b0 7a 65 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74  zeroed, but.** t
fe9c0 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69  he page object i
fe9d0 73 20 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a  s not dropped..*
fe9e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
fe9f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
fea00 63 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 63  cheTruncate(PCac
fea10 68 65 20 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f  he *pCache, Pgno
fea20 20 70 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43   pgno){.  if( pC
fea30 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a  ache->pCache ){.
fea40 20 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20      PgHdr *p;.  
fea50 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a    PgHdr *pNext;.
fea60 20 20 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65      for(p=pCache
fea70 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70  ->pDirty; p; p=p
fea80 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 70 4e 65  Next){.      pNe
fea90 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65  xt = p->pDirtyNe
feaa0 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d  xt;.      if( p-
feab0 3e 70 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20  >pgno>pgno ){.  
feac0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
fead0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
feae0 54 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TY );.        sq
feaf0 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
feb00 6c 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d  lean(p);.      }
feb10 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
feb20 67 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 65  gno==0 && pCache
feb30 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20  ->pPage1 ){.    
feb40 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2d    memset(pCache-
feb50 3e 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c 20  >pPage1->pData, 
feb60 30 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67  0, pCache->szPag
feb70 65 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d  e);.      pgno =
feb80 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71   1;.    }.    sq
feb90 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
feba0 67 2e 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61  g.pcache.xTrunca
febb0 74 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  te(pCache->pCach
febc0 65 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a  e, pgno+1);.  }.
febd0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
febe0 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54   cache..*/.SQLIT
febf0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
fec00 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73  qlite3PcacheClos
fec10 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  e(PCache *pCache
fec20 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  ){.  if( pCache-
fec30 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73  >pCache ){.    s
fec40 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
fec50 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72  ig.pcache.xDestr
fec60 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  oy(pCache->pCach
fec70 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a  e);.  }.}../* .*
fec80 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f  * Discard the co
fec90 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61  ntents of the ca
feca0 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  che..*/.SQLITE_P
fecb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
fecc0 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 50  te3PcacheClear(P
fecd0 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
fece0 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54    sqlite3PcacheT
fecf0 72 75 6e 63 61 74 65 28 70 43 61 63 68 65 2c 20  runcate(pCache, 
fed00 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72  0);.}../*.** Mer
fed10 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20  ge two lists of 
fed20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20  pages connected 
fed30 62 79 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e  by pDirty and in
fed40 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20   pgno order..** 
fed50 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69  Do not both fixi
fed60 6e 67 20 74 68 65 20 70 44 69 72 74 79 50 72 65  ng the pDirtyPre
fed70 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73  v pointers..*/.s
fed80 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61  tatic PgHdr *pca
fed90 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73  cheMergeDirtyLis
feda0 74 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48  t(PgHdr *pA, PgH
fedb0 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72  dr *pB){.  PgHdr
fedc0 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b   result, *pTail;
fedd0 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75  .  pTail = &resu
fede0 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20  lt;.  while( pA 
fedf0 26 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28  && pB ){.    if(
fee00 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67   pA->pgno<pB->pg
fee10 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69  no ){.      pTai
fee20 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a  l->pDirty = pA;.
fee30 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41        pTail = pA
fee40 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d  ;.      pA = pA-
fee50 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c  >pDirty;.    }el
fee60 73 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d  se{.      pTail-
fee70 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20  >pDirty = pB;.  
fee80 20 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a      pTail = pB;.
fee90 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70        pB = pB->p
feea0 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d  Dirty;.    }.  }
feeb0 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20  .  if( pA ){.   
feec0 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d   pTail->pDirty =
feed0 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   pA;.  }else if(
feee0 20 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c   pB ){.    pTail
feef0 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20  ->pDirty = pB;. 
fef00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69   }else{.    pTai
fef10 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20  l->pDirty = 0;. 
fef20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75   }.  return resu
fef30 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a  lt.pDirty;.}../*
fef40 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73  .** Sort the lis
fef50 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63  t of pages in ac
fef60 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79  cending order by
fef70 20 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72   pgno.  Pages ar
fef80 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62  e.** connected b
fef90 79 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72  y pDirty pointer
fefa0 73 2e 20 20 54 68 65 20 70 44 69 72 74 79 50 72  s.  The pDirtyPr
fefb0 65 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a  ev pointers are.
fefc0 2a 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79 20  ** corrupted by 
fefd0 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64  this sort..*/.#d
fefe0 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43  efine N_SORT_BUC
feff0 4b 45 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 65  KET_ALLOC 25.#de
ff000 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b  fine N_SORT_BUCK
ff010 45 54 20 20 20 20 20 20 20 32 35 0a 23 69 66 64  ET       25.#ifd
ff020 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
ff030 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67   int sqlite3_pag
ff040 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74  er_n_sort_bucket
ff050 20 3d 20 30 3b 0a 20 20 23 75 6e 64 65 66 20 4e   = 0;.  #undef N
ff060 5f 53 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20 23  _SORT_BUCKET.  #
ff070 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55  define N_SORT_BU
ff080 43 4b 45 54 20 5c 0a 20 20 20 28 73 71 6c 69 74  CKET \.   (sqlit
ff090 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f  e3_pager_n_sort_
ff0a0 62 75 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f 70  bucket?sqlite3_p
ff0b0 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b  ager_n_sort_buck
ff0c0 65 74 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  et:N_SORT_BUCKET
ff0d0 5f 41 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a 73  _ALLOC).#endif.s
ff0e0 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61  tatic PgHdr *pca
ff0f0 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74  cheSortDirtyList
ff100 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20  (PgHdr *pIn){.  
ff110 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f  PgHdr *a[N_SORT_
ff120 42 55 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a  BUCKET_ALLOC], *
ff130 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65  p;.  int i;.  me
ff140 6d 73 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f  mset(a, 0, sizeo
ff150 66 28 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20  f(a));.  while( 
ff160 70 49 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70  pIn ){.    p = p
ff170 49 6e 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d  In;.    pIn = p-
ff180 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e  >pDirty;.    p->
ff190 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20  pDirty = 0;.    
ff1a0 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52  for(i=0; i<N_SOR
ff1b0 54 5f 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b 29  T_BUCKET-1; i++)
ff1c0 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69 5d  {.      if( a[i]
ff1d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==0 ){.        a
ff1e0 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 20  [i] = p;.       
ff1f0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65   break;.      }e
ff200 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20 3d  lse{.        p =
ff210 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74   pcacheMergeDirt
ff220 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a  yList(a[i], p);.
ff230 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 30          a[i] = 0
ff240 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
ff250 20 20 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f 52      if( i==N_SOR
ff260 54 5f 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 20  T_BUCKET-1 ){.  
ff270 20 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a      /* Coverage:
ff280 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68   To get here, th
ff290 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32  ere need to be 2
ff2a0 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29  ^(N_SORT_BUCKET)
ff2b0 20 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65   .      ** eleme
ff2c0 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74  nts in the input
ff2d0 20 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 70   list. This is p
ff2e0 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 69 6d 70  ossible, but imp
ff2f0 72 61 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 20  ractical..      
ff300 2a 2a 20 54 65 73 74 69 6e 67 20 74 68 69 73 20  ** Testing this 
ff310 6c 69 6e 65 20 69 73 20 74 68 65 20 70 6f 69 6e  line is the poin
ff320 74 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69  t of global vari
ff330 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 71  able.      ** sq
ff340 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f  lite3_pager_n_so
ff350 72 74 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 20  rt_bucket..     
ff360 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d   */.      a[i] =
ff370 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74   pcacheMergeDirt
ff380 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a  yList(a[i], p);.
ff390 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20      }.  }.  p = 
ff3a0 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b  a[0];.  for(i=1;
ff3b0 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54   i<N_SORT_BUCKET
ff3c0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20  ; i++){.    p = 
ff3d0 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79  pcacheMergeDirty
ff3e0 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20  List(p, a[i]);. 
ff3f0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
ff400 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
ff410 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72   list of all dir
ff420 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ty pages in the 
ff430 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79  cache, sorted by
ff440 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f   page number..*/
ff450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ff460 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63  PgHdr *sqlite3Pc
ff470 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43  acheDirtyList(PC
ff480 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
ff490 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72   PgHdr *p;.  for
ff4a0 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74  (p=pCache->pDirt
ff4b0 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74  y; p; p=p->pDirt
ff4c0 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70  yNext){.    p->p
ff4d0 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69 72 74  Dirty = p->pDirt
ff4e0 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74  yNext;.  }.  ret
ff4f0 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74 44 69  urn pcacheSortDi
ff500 72 74 79 4c 69 73 74 28 70 43 61 63 68 65 2d 3e  rtyList(pCache->
ff510 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a  pDirty);.}../* .
ff520 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f  ** Return the to
ff530 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  tal number of re
ff540 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 68  ferenced pages h
ff550 65 6c 64 20 62 79 20 74 68 65 20 63 61 63 68 65  eld by the cache
ff560 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
ff570 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
ff580 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43  cacheRefCount(PC
ff590 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
ff5a0 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e   return pCache->
ff5b0 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  nRef;.}../*.** R
ff5c0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
ff5d0 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
ff5e0 6f 20 74 68 65 20 70 61 67 65 20 73 75 70 70 6c  o the page suppl
ff5f0 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65  ied as an argume
ff600 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
ff610 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ff620 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f  3PcachePageRefco
ff630 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b 0a 20  unt(PgHdr *p){. 
ff640 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b   return p->nRef;
ff650 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72  .}../* .** Retur
ff660 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  n the total numb
ff670 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
ff680 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c  he cache..*/.SQL
ff690 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ff6a0 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67  sqlite3PcachePag
ff6b0 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70  ecount(PCache *p
ff6c0 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50  Cache){.  int nP
ff6d0 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  age = 0;.  if( p
ff6e0 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b  Cache->pCache ){
ff6f0 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73 71 6c  .    nPage = sql
ff700 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
ff710 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63 6f 75  .pcache.xPagecou
ff720 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  nt(pCache->pCach
ff730 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
ff740 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65   nPage;.}..#ifde
ff750 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
ff760 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75 67 67  .** Get the sugg
ff770 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65  ested cache-size
ff780 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54   value..*/.SQLIT
ff790 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ff7a0 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61  lite3PcacheGetCa
ff7b0 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a  chesize(PCache *
ff7c0 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 75 72  pCache){.  retur
ff7d0 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a  n pCache->nMax;.
ff7e0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
ff7f0 53 65 74 20 74 68 65 20 73 75 67 67 65 73 74 65  Set the suggeste
ff800 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c  d cache-size val
ff810 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ue..*/.SQLITE_PR
ff820 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ff830 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65  e3PcacheSetCache
ff840 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61  size(PCache *pCa
ff850 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67 65 29  che, int mxPage)
ff860 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78  {.  pCache->nMax
ff870 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69 66 28   = mxPage;.  if(
ff880 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
ff890 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
ff8a0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
ff8b0 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 43 61  e.xCachesize(pCa
ff8c0 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50  che->pCache, mxP
ff8d0 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66  age);.  }.}..#if
ff8e0 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  def SQLITE_CHECK
ff8f0 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 46 6f 72  _PAGES./*.** For
ff900 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
ff910 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68   currently in th
ff920 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65 20  e cache, invoke 
ff930 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
ff940 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 20   callback. This 
ff950 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20  is only used if 
ff960 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  the SQLITE_CHECK
ff970 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73 0a  _PAGES macro is.
ff980 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53  ** defined..*/.S
ff990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
ff9a0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
ff9b0 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 61  IterateDirty(PCa
ff9c0 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69  che *pCache, voi
ff9d0 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 72  d (*xIter)(PgHdr
ff9e0 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a 70   *)){.  PgHdr *p
ff9f0 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44 69  Dirty;.  for(pDi
ffa00 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 72  rty=pCache->pDir
ffa10 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69 72  ty; pDirty; pDir
ffa20 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 74  ty=pDirty->pDirt
ffa30 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74 65  yNext){.    xIte
ffa40 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a 7d  r(pDirty);.  }.}
ffa50 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
ffa60 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
ffa70 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  pcache.c *******
ffa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffa90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffaa0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
ffab0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
ffac0 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a 2a  ile pcache1.c **
ffad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffaf0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
ffb00 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35 0a  008 November 05.
ffb10 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
ffb20 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
ffb30 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
ffb40 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
ffb50 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
ffb60 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
ffb70 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
ffb80 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
ffb90 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
ffba0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
ffbb0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
ffbc0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
ffbd0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
ffbe0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
ffbf0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
ffc00 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
ffc10 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
ffc20 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
ffc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
ffc70 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69  *.** This file i
ffc80 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 65  mplements the de
ffc90 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
ffca0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
ffcb0 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  (the.** sqlite3_
ffcc0 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65  pcache interface
ffcd0 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61  ). It also conta
ffce0 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65 20  ins part of the 
ffcf0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
ffd00 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f  * of the SQLITE_
ffd10 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
ffd20 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 6c   and sqlite3_rel
ffd30 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66 65  ease_memory() fe
ffd40 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74 68  atures..** If th
ffd50 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63  e default page c
ffd60 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ache implementat
ffd70 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65 6e  ion is overriden
ffd80 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 6f  , then neither o
ffd90 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20 66  f.** these two f
ffda0 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61 69  eatures are avai
ffdb0 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  lable..**.** @(#
ffdc0 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e 63  ) $Id: pcache1.c
ffdd0 2c 76 20 31 2e 31 31 2e 32 2e 31 20 32 30 30 39  ,v 1.11.2.1 2009
ffde0 2f 30 35 2f 31 38 20 31 36 3a 31 34 3a 32 35 20  /05/18 16:14:25 
ffdf0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 74  drh Exp $.*/...t
ffe00 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43  ypedef struct PC
ffe10 61 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a 74  ache1 PCache1;.t
ffe20 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67  ypedef struct Pg
ffe30 48 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79 70  Hdr1 PgHdr1;.typ
ffe40 65 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72  edef struct PgFr
ffe50 65 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f  eeslot PgFreeslo
ffe60 74 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20  t;../* Pointers 
ffe70 74 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f 66  to structures of
ffe80 20 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63   this type are c
ffe90 61 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65 64  ast and returned
ffea0 20 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73   as .** opaque s
ffeb0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20 68  qlite3_pcache* h
ffec0 61 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74  andles.*/.struct
ffed0 20 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20   PCache1 {.  /* 
ffee0 43 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74  Cache configurat
ffef0 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20  ion parameters. 
fff00 50 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67  Page size (szPag
fff10 65 29 20 61 6e 64 20 74 68 65 20 70 75 72 67 65  e) and the purge
fff20 61 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28  able.  ** flag (
fff30 62 50 75 72 67 65 61 62 6c 65 29 20 61 72 65 20  bPurgeable) are 
fff40 73 65 74 20 77 68 65 6e 20 74 68 65 20 63 61 63  set when the cac
fff50 68 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e  he is created. n
fff60 4d 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a  Max may be .  **
fff70 20 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79   modified at any
fff80 20 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20   time by a call 
fff90 74 6f 20 74 68 65 20 70 63 61 63 68 65 31 43 61  to the pcache1Ca
fffa0 63 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f 64  cheSize() method
fffb0 2e 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61  ..  ** The globa
fffc0 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
fffd0 68 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73  held when access
fffe0 69 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20  ing nMax..  */. 
ffff0 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20   int szPage;    
10000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10001 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
10002 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 20  allocated pages 
10003 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e  in bytes */.  in
10004 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20  t bPurgeable;   
10005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10006 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63    /* True if cac
10007 68 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20  he is purgeable 
10008 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
10009 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20  t nMin;         
1000a 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69           /* Mini
1000b 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  mum number of pa
1000c 67 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a  ges reserved */.
1000d 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
1000e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Max;            
1000f 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75        /* Configu
10010 72 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22  red "cache_size"
10011 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20   value */..  /* 
10012 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 6c  Hash table of al
10013 6c 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f 6c  l pages. The fol
10014 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73  lowing variables
10015 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 63   may only be acc
10016 65 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20  essed.  ** when 
10017 74 68 65 20 61 63 63 65 73 73 6f 72 20 69 73 20  the accessor is 
10018 68 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f 62  holding the glob
10019 61 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70 63  al mutex (see pc
1001a 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
1001b 29 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61 63  ) .  ** and pcac
1001c 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 29  he1LeaveMutex())
1001d 2e 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ..  */.  unsigne
1001e 64 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c  d int nRecyclabl
1001f 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
10020 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
10021 69 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74 20  in the LRU list 
10022 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
10023 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20  t nPage;        
10024 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
10025 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
10026 73 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20  s in apHash */. 
10027 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48   unsigned int nH
10028 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  ash;            
10029 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1002a 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61 73  f slots in apHas
1002b 68 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31 20  h[] */.  PgHdr1 
1002c 2a 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20 20  **apHash;       
1002d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1002e 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66  Hash table for f
1002f 61 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65  ast lookup by ke
10030 79 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64  y */..  unsigned
10031 20 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 20   int iMaxKey;   
10032 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
10033 61 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e 20  argest key seen 
10034 73 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 28  since xTruncate(
10035 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45  ) */.};../*.** E
10036 61 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20  ach cache entry 
10037 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  is represented b
10038 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
10039 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a   the following .
1003a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41 20  ** structure. A 
1003b 62 75 66 66 65 72 20 6f 66 20 50 67 48 64 72 31  buffer of PgHdr1
1003c 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20  .pCache->szPage 
1003d 62 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 74  bytes is allocat
1003e 65 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20  ed .** directly 
1003f 61 66 74 65 72 20 74 68 65 20 73 74 72 75 63 74  after the struct
10040 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28 73  ure in memory (s
10041 65 65 20 74 68 65 20 50 47 48 44 52 31 5f 54 4f  ee the PGHDR1_TO
10042 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63 72  _PAGE() .** macr
10043 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 72  o below)..*/.str
10044 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20 75  uct PgHdr1 {.  u
10045 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79  nsigned int iKey
10046 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
10047 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67 65   Key value (page
10048 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50 67   number) */.  Pg
10049 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20 20  Hdr1 *pNext;    
1004a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1004b 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74 61 62  Next in hash tab
1004c 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50 43  le chain */.  PC
1004d 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 20  ache1 *pCache;  
1004e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1004f 43 61 63 68 65 20 74 68 61 74 20 63 75 72 72 65  Cache that curre
10050 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20 70  ntly owns this p
10051 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31 20  age */.  PgHdr1 
10052 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20 20  *pLruNext;      
10053 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
10054 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75  in LRU list of u
10055 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f  npinned pages */
10056 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 50  .  PgHdr1 *pLruP
10057 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  rev;            
10058 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 6e    /* Previous in
10059 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e 70   LRU list of unp
1005a 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a 7d  inned pages */.}
1005b 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73 6c  ;../*.** Free sl
1005c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f 63  ots in the alloc
1005d 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 76  ator used to div
1005e 69 64 65 20 75 70 20 74 68 65 20 62 75 66 66 65  ide up the buffe
1005f 72 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e 67  r provided using
10060 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 43  .** the SQLITE_C
10061 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20  ONFIG_PAGECACHE 
10062 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73 74  mechanism..*/.st
10063 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20  ruct PgFreeslot 
10064 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a  {.  PgFreeslot *
10065 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20  pNext;  /* Next 
10066 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a  free slot */.};.
10067 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64 61  ./*.** Global da
10068 74 61 20 75 73 65 64 20 62 79 20 74 68 69 73 20  ta used by this 
10069 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  cache..*/.static
1006a 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75   SQLITE_WSD stru
1006b 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 20  ct PCacheGlobal 
1006c 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
1006d 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20  x *mutex;       
1006e 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74 69          /* stati
1006f 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53 54  c mutex MUTEX_ST
10070 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20 69  ATIC_LRU */..  i
10071 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 20  nt nMaxPage;    
10072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10073 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 61     /* Sum of nMa
10074 78 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61  xPage for purgea
10075 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20  ble caches */.  
10076 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20 20  int nMinPage;   
10077 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10078 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d      /* Sum of nM
10079 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67 65  inPage for purge
1007a 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20  able caches */. 
1007b 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 67   int nCurrentPag
1007c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1007d 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1007e 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65  f purgeable page
1007f 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20  s allocated */. 
10080 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 61   PgHdr1 *pLruHea
10081 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 20  d, *pLruTail;   
10082 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73 74       /* LRU list
10083 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67   of unpinned pag
10084 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72 69  es */..  /* Vari
10085 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74 6f  ables related to
10086 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50   SQLITE_CONFIG_P
10087 41 47 45 43 41 43 48 45 20 73 65 74 74 69 6e 67  AGECACHE setting
10088 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 6c  s. */.  int szSl
10089 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ot;             
1008a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1008b 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 65  ize of each free
1008c 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64 20   slot */.  void 
1008d 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 20  *pStart, *pEnd; 
1008e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1008f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67 65  * Bounds of page
10090 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61 6e  cache malloc ran
10091 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73 6c  ge */.  PgFreesl
10092 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20  ot *pFree;      
10093 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
10094 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73 20  ree page blocks 
10095 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b 0a  */.} pcache1_g;.
10096 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 20  ./*.** All code 
10097 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 6f  in this file sho
10098 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 67  uld access the g
10099 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20  lobal structure 
1009a 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a 2a  above via the.**
1009b 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 31 22   alias "pcache1"
1009c 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74  . This ensures t
1009d 68 61 74 20 74 68 65 20 57 53 44 20 65 6d 75 6c  hat the WSD emul
1009e 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77 68  ation is used wh
1009f 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20  en.** compiling 
100a0 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61 74  for systems that
100a1 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   do not support 
100a2 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 65  real WSD..*/.#de
100a3 66 69 6e 65 20 70 63 61 63 68 65 31 20 28 47 4c  fine pcache1 (GL
100a4 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 63  OBAL(struct PCac
100a5 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 65  heGlobal, pcache
100a6 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  1_g))../*.** Whe
100a7 6e 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63  n a PgHdr1 struc
100a8 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65  ture is allocate
100a9 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  d, the associate
100aa 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65  d PCache1.szPage
100ab 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61 74  .** bytes of dat
100ac 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64 69  a are located di
100ad 72 65 63 74 6c 79 20 61 66 74 65 72 20 69 74 20  rectly after it 
100ae 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 2e 20  in memory (i.e. 
100af 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 69 7a  the total.** siz
100b0 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  e of the allocat
100b1 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 50 67  ion is sizeof(Pg
100b2 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e 73 7a  Hdr1)+PCache1.sz
100b3 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 65 0a  Page byte). The.
100b4 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47  ** PGHDR1_TO_PAG
100b5 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 73 20  E() macro takes 
100b6 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50  a pointer to a P
100b7 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20  gHdr1 structure 
100b8 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d 65 6e  as.** an argumen
100b9 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  t and returns a 
100ba 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61  pointer to the a
100bb 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 6b 20  ssociated block 
100bc 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 79 74  of szPage.** byt
100bd 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 4f 5f  es. The PAGE_TO_
100be 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f 20 64  PGHDR1() macro d
100bf 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 74 65  oes the opposite
100c0 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 20 69  : its argument i
100c1 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  s.** a pointer t
100c2 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 7a 50  o a block of szP
100c3 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  age bytes of dat
100c4 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 72 6e  a and the return
100c5 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 70   value is.** a p
100c6 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 73  ointer to the as
100c7 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 31 20  sociated PgHdr1 
100c8 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
100c9 20 20 20 61 73 73 65 72 74 28 20 50 47 48 44 52     assert( PGHDR
100ca 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 5f 54  1_TO_PAGE(PAGE_T
100cb 4f 5f 50 47 48 44 52 31 28 58 29 29 3d 3d 58 20  O_PGHDR1(X))==X 
100cc 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47  );.*/.#define PG
100cd 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 29 20  HDR1_TO_PAGE(p) 
100ce 28 76 6f 69 64 20 2a 29 28 26 28 28 75 6e 73 69  (void *)(&((unsi
100cf 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 5b 73  gned char *)p)[s
100d0 69 7a 65 6f 66 28 50 67 48 64 72 31 29 5d 29 0a  izeof(PgHdr1)]).
100d1 23 64 65 66 69 6e 65 20 50 41 47 45 5f 54 4f 5f  #define PAGE_TO_
100d2 50 47 48 44 52 31 28 70 29 20 28 50 67 48 64 72  PGHDR1(p) (PgHdr
100d3 31 20 2a 29 28 26 28 28 75 6e 73 69 67 6e 65 64  1 *)(&((unsigned
100d4 20 63 68 61 72 20 2a 29 70 29 5b 2d 31 2a 28 69   char *)p)[-1*(i
100d5 6e 74 29 73 69 7a 65 6f 66 28 50 67 48 64 72 31  nt)sizeof(PgHdr1
100d6 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  )])../*.** Macro
100d7 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c  s to enter and l
100d8 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20  eave the global 
100d9 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64  LRU mutex..*/.#d
100da 65 66 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74  efine pcache1Ent
100db 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65  erMutex() sqlite
100dc 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63  3_mutex_enter(pc
100dd 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65  ache1.mutex).#de
100de 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76  fine pcache1Leav
100df 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33  eMutex() sqlite3
100e0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61  _mutex_leave(pca
100e1 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a  che1.mutex)../**
100e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
100e7 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c  ******* Page All
100e8 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43  ocation/SQLITE_C
100e9 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c  ONFIG_PCACHE Rel
100ea 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a  ated Functions *
100eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
100ec 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
100ed 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75  ion is called du
100ee 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
100ef 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69 63 20  ion if a static 
100f0 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75  buffer is .** su
100f1 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f  pplied to use fo
100f2 72 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65  r the page-cache
100f3 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20   by passing the 
100f4 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
100f5 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20  GECACHE.** verb 
100f6 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  to sqlite3_confi
100f7 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70  g(). Parameter p
100f8 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  Buf points to an
100f9 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67   allocation larg
100fa 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63  e.** enough to c
100fb 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65  ontain 'n' buffe
100fc 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73  rs of 'sz' bytes
100fd 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45   each..*/.SQLITE
100fe 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
100ff 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65  lite3PCacheBuffe
10100 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75  rSetup(void *pBu
10101 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e  f, int sz, int n
10102 29 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20  ){.  PgFreeslot 
10103 2a 70 3b 0a 20 20 73 7a 20 3d 20 52 4f 55 4e 44  *p;.  sz = ROUND
10104 44 4f 57 4e 38 28 73 7a 29 3b 0a 20 20 70 63 61  DOWN8(sz);.  pca
10105 63 68 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a  che1.szSlot = sz
10106 3b 0a 20 20 70 63 61 63 68 65 31 2e 70 53 74 61  ;.  pcache1.pSta
10107 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 70 63 61  rt = pBuf;.  pca
10108 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 3b 0a  che1.pFree = 0;.
10109 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29 7b 0a    while( n-- ){.
1010a 20 20 20 20 70 20 3d 20 28 50 67 46 72 65 65 73      p = (PgFrees
1010b 6c 6f 74 2a 29 70 42 75 66 3b 0a 20 20 20 20 70  lot*)pBuf;.    p
1010c 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65  ->pNext = pcache
1010d 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61  1.pFree;.    pca
1010e 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 3b 0a  che1.pFree = p;.
1010f 20 20 20 20 70 42 75 66 20 3d 20 28 76 6f 69 64      pBuf = (void
10110 2a 29 26 28 28 63 68 61 72 2a 29 70 42 75 66 29  *)&((char*)pBuf)
10111 5b 73 7a 5d 3b 0a 20 20 7d 0a 20 20 70 63 61 63  [sz];.  }.  pcac
10112 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b  he1.pEnd = pBuf;
10113 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63  .}../*.** Malloc
10114 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 77   function used w
10115 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c 65 20  ithin this file 
10116 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63  to allocate spac
10117 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65  e from the buffe
10118 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20  r.** configured 
10119 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f  using sqlite3_co
1011a 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46  nfig(SQLITE_CONF
1011b 49 47 5f 50 41 47 45 43 41 43 48 45 29 20 6f 70  IG_PAGECACHE) op
1011c 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a 2a 20  tion. If no .** 
1011d 73 75 63 68 20 62 75 66 66 65 72 20 65 78 69 73  such buffer exis
1011e 74 73 20 6f 72 20 74 68 65 72 65 20 69 73 20 6e  ts or there is n
1011f 6f 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20  o space left in 
10120 69 74 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  it, this functio
10121 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 62 61 63 6b  n falls .** back
10122 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   to sqlite3Mallo
10123 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  c()..*/.static v
10124 6f 69 64 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f  oid *pcache1Allo
10125 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  c(int nByte){.  
10126 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72  void *p;.  asser
10127 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
10128 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75  _held(pcache1.mu
10129 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 6e 42  tex) );.  if( nB
1012a 79 74 65 3c 3d 70 63 61 63 68 65 31 2e 73 7a 53  yte<=pcache1.szS
1012b 6c 6f 74 20 26 26 20 70 63 61 63 68 65 31 2e 70  lot && pcache1.p
1012c 46 72 65 65 20 29 7b 0a 20 20 20 20 70 20 3d 20  Free ){.    p = 
1012d 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65  (PgHdr1 *)pcache
1012e 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61  1.pFree;.    pca
1012f 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 63 61  che1.pFree = pca
10130 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e 65 78  che1.pFree->pNex
10131 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  t;.    sqlite3St
10132 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
10133 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
10134 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a 20 20  SIZE, nByte);.  
10135 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41    sqlite3StatusA
10136 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
10137 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c  _PAGECACHE_USED,
10138 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20   1);.  }else{.. 
10139 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61     /* Allocate a
1013a 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 69 6e   new buffer usin
1013b 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 2e  g sqlite3Malloc.
1013c 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f   Before doing so
1013d 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 20 2a  , exit the.    *
1013e 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 65 20  * global pcache 
1013f 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f 63 6b  mutex and unlock
10140 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65   the pager-cache
10141 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 2e 20   object pCache. 
10142 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a 2a 20  This is .    ** 
10143 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 61  so that if the a
10144 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61  ttempt to alloca
10145 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 20  te a new buffer 
10146 63 61 75 73 65 73 20 74 68 65 20 74 68 65 20 0a  causes the the .
10147 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65      ** configure
10148 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 6d 69  d soft-heap-limi
10149 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 65 64  t to be breached
1014a 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 6f 73  , it will be pos
1014b 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a 2a 20  sible to.    ** 
1014c 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 66  reclaim memory f
1014d 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 2d 63  rom this pager-c
1014e 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ache..    */.   
1014f 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10150 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 73 71  ex();.    p = sq
10151 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74  lite3Malloc(nByt
10152 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45  e);.    pcache1E
10153 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
10154 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
10155 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 65 33  int sz = sqlite3
10156 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20  MallocSize(p);. 
10157 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
10158 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
10159 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56  TUS_PAGECACHE_OV
1015a 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 20 20  ERFLOW, sz);.   
1015b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1015c 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  p;.}../*.** Free
1015d 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75   an allocated bu
1015e 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72  ffer obtained fr
1015f 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28  om pcache1Alloc(
10160 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
10161 64 20 70 63 61 63 68 65 31 46 72 65 65 28 76 6f  d pcache1Free(vo
10162 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  id *p){.  assert
10163 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
10164 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74  held(pcache1.mut
10165 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d  ex) );.  if( p==
10166 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
10167 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 53 74  ( p>=pcache1.pSt
10168 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 65 31  art && p<pcache1
10169 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 67 46  .pEnd ){.    PgF
1016a 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 3b 0a  reeslot *pSlot;.
1016b 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
1016c 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
1016d 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45  US_PAGECACHE_USE
1016e 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 6c 6f  D, -1);.    pSlo
1016f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f 74 2a  t = (PgFreeslot*
10170 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d 3e 70  )p;.    pSlot->p
10171 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70  Next = pcache1.p
10172 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65  Free;.    pcache
10173 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f 74 3b  1.pFree = pSlot;
10174 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
10175 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  t iSize = sqlite
10176 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a  3MallocSize(p);.
10177 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
10178 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
10179 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45  US_PAGECACHE_OVE
1017a 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a  RFLOW, -iSize);.
1017b 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1017c 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
1017d 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
1017e 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 6e 69   page object ini
1017f 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 74 65  tially associate
10180 64 20 77 69 74 68 20 63 61 63 68 65 20 70 43 61  d with cache pCa
10181 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50  che..*/.static P
10182 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 41 6c  gHdr1 *pcache1Al
10183 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 31 20  locPage(PCache1 
10184 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20  *pCache){.  int 
10185 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 50  nByte = sizeof(P
10186 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 65 2d  gHdr1) + pCache-
10187 3e 73 7a 50 61 67 65 3b 0a 20 20 50 67 48 64 72  >szPage;.  PgHdr
10188 31 20 2a 70 20 3d 20 28 50 67 48 64 72 31 20 2a  1 *p = (PgHdr1 *
10189 29 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 6e 42  )pcache1Alloc(nB
1018a 79 74 65 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  yte);.  if( p ){
1018b 0a 20 20 20 20 69 66 28 20 70 43 61 63 68 65 2d  .    if( pCache-
1018c 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20  >bPurgeable ){. 
1018d 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43 75       pcache1.nCu
1018e 72 72 65 6e 74 50 61 67 65 2b 2b 3b 0a 20 20 20  rrentPage++;.   
1018f 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
10190 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  p;.}../*.** Free
10191 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61   a page object a
10192 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63  llocated by pcac
10193 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a  he1AllocPage()..
10194 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10195 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 50  cache1FreePage(P
10196 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69 66 28  gHdr1 *p){.  if(
10197 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d   p ){.    if( p-
10198 3e 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61  >pCache->bPurgea
10199 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61  ble ){.      pca
1019a 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67  che1.nCurrentPag
1019b 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  e--;.    }.    p
1019c 63 61 63 68 65 31 46 72 65 65 28 70 29 3b 0a 20  cache1Free(p);. 
1019d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c   }.}../*.** Mall
1019e 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  oc function used
1019f 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 6f 62   by SQLite to ob
101a0 74 61 69 6e 20 73 70 61 63 65 20 66 72 6f 6d 20  tain space from 
101a1 74 68 65 20 62 75 66 66 65 72 20 63 6f 6e 66 69  the buffer confi
101a2 67 75 72 65 64 0a 2a 2a 20 75 73 69 6e 67 20 73  gured.** using s
101a3 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51  qlite3_config(SQ
101a4 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45  LITE_CONFIG_PAGE
101a5 43 41 43 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49  CACHE) option. I
101a6 66 20 6e 6f 20 73 75 63 68 20 62 75 66 66 65 72  f no such buffer
101a7 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 69 73  .** exists, this
101a8 20 66 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20   function falls 
101a9 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d  back to sqlite3M
101aa 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
101ab 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
101ac 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c  *sqlite3PageMall
101ad 6f 63 28 69 6e 74 20 73 7a 29 7b 0a 20 20 76 6f  oc(int sz){.  vo
101ae 69 64 20 2a 70 3b 0a 20 20 70 63 61 63 68 65 31  id *p;.  pcache1
101af 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
101b0 70 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63  p = pcache1Alloc
101b1 28 73 7a 29 3b 0a 20 20 70 63 61 63 68 65 31 4c  (sz);.  pcache1L
101b2 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
101b3 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
101b4 2a 20 46 72 65 65 20 61 6e 20 61 6c 6c 6f 63 61  * Free an alloca
101b5 74 65 64 20 62 75 66 66 65 72 20 6f 62 74 61 69  ted buffer obtai
101b6 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
101b7 50 61 67 65 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  PageMalloc()..*/
101b8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
101b9 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
101ba 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Free(void *p){. 
101bb 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
101bc 65 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31 46  ex();.  pcache1F
101bd 72 65 65 28 70 29 3b 0a 20 20 70 63 61 63 68 65  ree(p);.  pcache
101be 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d  1LeaveMutex();.}
101bf 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
101c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c4 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e  */./******** Gen
101c5 65 72 61 6c 20 49 6d 70 6c 65 6d 65 6e 74 61 74  eral Implementat
101c6 69 6f 6e 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a  ion Functions **
101c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c9 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  **/../*.** This 
101ca 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
101cb 20 74 6f 20 72 65 73 69 7a 65 20 74 68 65 20 68   to resize the h
101cc 61 73 68 20 74 61 62 6c 65 20 75 73 65 64 20 62  ash table used b
101cd 79 20 74 68 65 20 63 61 63 68 65 20 70 61 73 73  y the cache pass
101ce 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72  ed.** as the fir
101cf 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  st argument..**.
101d0 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75  ** The global mu
101d1 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64  tex must be held
101d2 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
101d3 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ion is called..*
101d4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 61  /.static int pca
101d5 63 68 65 31 52 65 73 69 7a 65 48 61 73 68 28 50  che1ResizeHash(P
101d6 43 61 63 68 65 31 20 2a 70 29 7b 0a 20 20 50 67  Cache1 *p){.  Pg
101d7 48 64 72 31 20 2a 2a 61 70 4e 65 77 3b 0a 20 20  Hdr1 **apNew;.  
101d8 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4e 65  unsigned int nNe
101d9 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  w;.  unsigned in
101da 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  t i;..  assert( 
101db 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
101dc 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78  ld(pcache1.mutex
101dd 29 20 29 3b 0a 0a 20 20 6e 4e 65 77 20 3d 20 70  ) );..  nNew = p
101de 2d 3e 6e 48 61 73 68 2a 32 3b 0a 20 20 69 66 28  ->nHash*2;.  if(
101df 20 6e 4e 65 77 3c 32 35 36 20 29 7b 0a 20 20 20   nNew<256 ){.   
101e0 20 6e 4e 65 77 20 3d 20 32 35 36 3b 0a 20 20 7d   nNew = 256;.  }
101e1 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65  ..  pcache1Leave
101e2 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 70  Mutex();.  if( p
101e3 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69 74  ->nHash ){ sqlit
101e4 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
101e5 6c 6f 63 28 29 3b 20 7d 0a 20 20 61 70 4e 65 77  loc(); }.  apNew
101e6 20 3d 20 28 50 67 48 64 72 31 20 2a 2a 29 73 71   = (PgHdr1 **)sq
101e7 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
101e8 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e  eof(PgHdr1 *)*nN
101e9 65 77 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48  ew);.  if( p->nH
101ea 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 45 6e  ash ){ sqlite3En
101eb 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
101ec 20 7d 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65   }.  pcache1Ente
101ed 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20  rMutex();.  if( 
101ee 61 70 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d  apNew ){.    mem
101ef 73 65 74 28 61 70 4e 65 77 2c 20 30 2c 20 73 69  set(apNew, 0, si
101f0 7a 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a 6e  zeof(PgHdr1 *)*n
101f1 4e 65 77 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  New);.    for(i=
101f2 30 3b 20 69 3c 70 2d 3e 6e 48 61 73 68 3b 20 69  0; i<p->nHash; i
101f3 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 48 64 72  ++){.      PgHdr
101f4 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20  1 *pPage;.      
101f5 50 67 48 64 72 31 20 2a 70 4e 65 78 74 20 3d 20  PgHdr1 *pNext = 
101f6 70 2d 3e 61 70 48 61 73 68 5b 69 5d 3b 0a 20 20  p->apHash[i];.  
101f7 20 20 20 20 77 68 69 6c 65 28 20 28 70 50 61 67      while( (pPag
101f8 65 20 3d 20 70 4e 65 78 74 29 21 3d 30 20 29 7b  e = pNext)!=0 ){
101f9 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65  .        unsigne
101fa 64 20 69 6e 74 20 68 20 3d 20 70 50 61 67 65 2d  d int h = pPage-
101fb 3e 69 4b 65 79 20 25 20 6e 4e 65 77 3b 0a 20 20  >iKey % nNew;.  
101fc 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 50        pNext = pP
101fd 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  age->pNext;.    
101fe 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74      pPage->pNext
101ff 20 3d 20 61 70 4e 65 77 5b 68 5d 3b 0a 20 20 20   = apNew[h];.   
10200 20 20 20 20 20 61 70 4e 65 77 5b 68 5d 20 3d 20       apNew[h] = 
10201 70 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 20  pPage;.      }. 
10202 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
10203 5f 66 72 65 65 28 70 2d 3e 61 70 48 61 73 68 29  _free(p->apHash)
10204 3b 0a 20 20 20 20 70 2d 3e 61 70 48 61 73 68 20  ;.    p->apHash 
10205 3d 20 61 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e  = apNew;.    p->
10206 6e 48 61 73 68 20 3d 20 6e 4e 65 77 3b 0a 20 20  nHash = nNew;.  
10207 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e  }..  return (p->
10208 61 70 48 61 73 68 20 3f 20 53 51 4c 49 54 45 5f  apHash ? SQLITE_
10209 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  OK : SQLITE_NOME
1020a 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  M);.}../*.** Thi
1020b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
1020c 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f  ed internally to
1020d 20 72 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65   remove the page
1020e 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65 20   pPage from the 
1020f 0a 2a 2a 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c  .** global LRU l
10210 69 73 74 2c 20 69 66 20 69 73 20 70 61 72 74 20  ist, if is part 
10211 6f 66 20 69 74 2e 20 49 66 20 70 50 61 67 65 20  of it. If pPage 
10212 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74  is not part of t
10213 68 65 20 67 6c 6f 62 61 6c 0a 2a 2a 20 4c 52 55  he global.** LRU
10214 20 6c 69 73 74 2c 20 74 68 65 6e 20 74 68 69 73   list, then this
10215 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
10216 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  o-op..**.** The 
10217 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73  global mutex mus
10218 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74  t be held when t
10219 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1021a 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  called..*/.stati
1021b 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 50 69  c void pcache1Pi
1021c 6e 50 61 67 65 28 50 67 48 64 72 31 20 2a 70 50  nPage(PgHdr1 *pP
1021d 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  age){.  assert( 
1021e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1021f 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78  ld(pcache1.mutex
10220 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ) );.  if( pPage
10221 20 26 26 20 28 70 50 61 67 65 2d 3e 70 4c 72 75   && (pPage->pLru
10222 4e 65 78 74 20 7c 7c 20 70 50 61 67 65 3d 3d 70  Next || pPage==p
10223 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 29  cache1.pLruTail)
10224 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67   ){.    if( pPag
10225 65 2d 3e 70 4c 72 75 50 72 65 76 20 29 7b 0a 20  e->pLruPrev ){. 
10226 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75       pPage->pLru
10227 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 20 3d  Prev->pLruNext =
10228 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10229 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1022a 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
1022b 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e  ){.      pPage->
1022c 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72  pLruNext->pLruPr
1022d 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75  ev = pPage->pLru
1022e 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Prev;.    }.    
1022f 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75  if( pcache1.pLru
10230 48 65 61 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20  Head==pPage ){. 
10231 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72       pcache1.pLr
10232 75 48 65 61 64 20 3d 20 70 50 61 67 65 2d 3e 70  uHead = pPage->p
10233 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  LruNext;.    }. 
10234 20 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70     if( pcache1.p
10235 4c 72 75 54 61 69 6c 3d 3d 70 50 61 67 65 20 29  LruTail==pPage )
10236 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  {.      pcache1.
10237 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65  pLruTail = pPage
10238 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20  ->pLruPrev;.    
10239 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72  }.    pPage->pLr
1023a 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70  uNext = 0;.    p
1023b 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20 3d  Page->pLruPrev =
1023c 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70   0;.    pPage->p
1023d 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62  Cache->nRecyclab
1023e 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  le--;.  }.}.../*
1023f 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70  .** Remove the p
10240 61 67 65 20 73 75 70 70 6c 69 65 64 20 61 73 20  age supplied as 
10241 61 6e 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d  an argument from
10242 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
10243 0a 2a 2a 20 28 50 43 61 63 68 65 31 2e 61 70 48  .** (PCache1.apH
10244 61 73 68 20 73 74 72 75 63 74 75 72 65 29 20 74  ash structure) t
10245 68 61 74 20 69 74 20 69 73 20 63 75 72 72 65 6e  hat it is curren
10246 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 2e 0a 2a  tly stored in..*
10247 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20  *.** The global 
10248 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
10249 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  ld when this fun
1024a 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
1024b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1024c 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f  pcache1RemoveFro
1024d 6d 48 61 73 68 28 50 67 48 64 72 31 20 2a 70 50  mHash(PgHdr1 *pP
1024e 61 67 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  age){.  unsigned
1024f 20 69 6e 74 20 68 3b 0a 20 20 50 43 61 63 68 65   int h;.  PCache
10250 31 20 2a 70 43 61 63 68 65 20 3d 20 70 50 61 67  1 *pCache = pPag
10251 65 2d 3e 70 43 61 63 68 65 3b 0a 20 20 50 67 48  e->pCache;.  PgH
10252 64 72 31 20 2a 2a 70 70 3b 0a 0a 20 20 68 20 3d  dr1 **pp;..  h =
10253 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25 20 70   pPage->iKey % p
10254 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20  Cache->nHash;.  
10255 66 6f 72 28 70 70 3d 26 70 43 61 63 68 65 2d 3e  for(pp=&pCache->
10256 61 70 48 61 73 68 5b 68 5d 3b 20 28 2a 70 70 29  apHash[h]; (*pp)
10257 21 3d 70 50 61 67 65 3b 20 70 70 3d 26 28 2a 70  !=pPage; pp=&(*p
10258 70 29 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 2a 70  p)->pNext);.  *p
10259 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74  p = (*pp)->pNext
1025a 3b 0a 0a 20 20 70 43 61 63 68 65 2d 3e 6e 50 61  ;..  pCache->nPa
1025b 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ge--;.}../*.** I
1025c 66 20 74 68 65 72 65 20 61 72 65 20 63 75 72 72  f there are curr
1025d 65 6e 74 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20  ently more than 
1025e 70 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65 20  pcache.nMaxPage 
1025f 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 2c  pages allocated,
10260 20 74 72 79 0a 2a 2a 20 74 6f 20 72 65 63 79 63   try.** to recyc
10261 6c 65 20 70 61 67 65 73 20 74 6f 20 72 65 64 75  le pages to redu
10262 63 65 20 74 68 65 20 6e 75 6d 62 65 72 20 61 6c  ce the number al
10263 6c 6f 63 61 74 65 64 20 74 6f 20 70 63 61 63 68  located to pcach
10264 65 2e 6e 4d 61 78 50 61 67 65 2e 0a 2a 2f 0a 73  e.nMaxPage..*/.s
10265 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10266 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65  e1EnforceMaxPage
10267 28 76 6f 69 64 29 7b 0a 20 20 61 73 73 65 72 74  (void){.  assert
10268 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
10269 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74  held(pcache1.mut
1026a 65 78 29 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  ex) );.  while( 
1026b 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74  pcache1.nCurrent
1026c 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61  Page>pcache1.nMa
1026d 78 50 61 67 65 20 26 26 20 70 63 61 63 68 65 31  xPage && pcache1
1026e 2e 70 4c 72 75 54 61 69 6c 20 29 7b 0a 20 20 20  .pLruTail ){.   
1026f 20 50 67 48 64 72 31 20 2a 70 20 3d 20 70 63 61   PgHdr1 *p = pca
10270 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a 20  che1.pLruTail;. 
10271 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67     pcache1PinPag
10272 65 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65  e(p);.    pcache
10273 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28  1RemoveFromHash(
10274 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 46  p);.    pcache1F
10275 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 7d 0a  reePage(p);.  }.
10276 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 63 61 72 64  }../*.** Discard
10277 20 61 6c 6c 20 70 61 67 65 73 20 66 72 6f 6d 20   all pages from 
10278 63 61 63 68 65 20 70 43 61 63 68 65 20 77 69 74  cache pCache wit
10279 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20  h a page number 
1027a 28 6b 65 79 20 76 61 6c 75 65 29 20 0a 2a 2a 20  (key value) .** 
1027b 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
1027c 65 71 75 61 6c 20 74 6f 20 69 4c 69 6d 69 74 2e  equal to iLimit.
1027d 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65   Any pinned page
1027e 73 20 74 68 61 74 20 6d 65 65 74 20 74 68 69 73  s that meet this
1027f 20 0a 2a 2a 20 63 72 69 74 65 72 69 61 20 61 72   .** criteria ar
10280 65 20 75 6e 70 69 6e 6e 65 64 20 62 65 66 6f 72  e unpinned befor
10281 65 20 74 68 65 79 20 61 72 65 20 64 69 73 63 61  e they are disca
10282 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rded..**.** The 
10283 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73  global mutex mus
10284 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74  t be held when t
10285 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
10286 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  called..*/.stati
10287 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 54 72  c void pcache1Tr
10288 75 6e 63 61 74 65 55 6e 73 61 66 65 28 0a 20 20  uncateUnsafe(.  
10289 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 2c  PCache1 *pCache,
1028a 20 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74   .  unsigned int
1028b 20 69 4c 69 6d 69 74 20 0a 29 7b 0a 20 20 54 45   iLimit .){.  TE
1028c 53 54 4f 4e 4c 59 28 20 69 6e 74 20 6e 50 61 67  STONLY( int nPag
1028d 65 20 3d 20 30 3b 20 29 20 20 20 20 20 20 2f 2a  e = 0; )      /*
1028e 20 55 73 65 64 20 74 6f 20 61 73 73 65 72 74 20   Used to assert 
1028f 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 69 73  pCache->nPage is
10290 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20 75 6e   correct */.  un
10291 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20  signed int h;.  
10292 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
10293 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68  mutex_held(pcach
10294 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  e1.mutex) );.  f
10295 6f 72 28 68 3d 30 3b 20 68 3c 70 43 61 63 68 65  or(h=0; h<pCache
10296 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b 29 7b 0a 20  ->nHash; h++){. 
10297 20 20 20 50 67 48 64 72 31 20 2a 2a 70 70 20 3d     PgHdr1 **pp =
10298 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68   &pCache->apHash
10299 5b 68 5d 3b 20 0a 20 20 20 20 50 67 48 64 72 31  [h]; .    PgHdr1
1029a 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 77 68 69   *pPage;.    whi
1029b 6c 65 28 20 28 70 50 61 67 65 20 3d 20 2a 70 70  le( (pPage = *pp
1029c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  )!=0 ){.      if
1029d 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3e 3d 69  ( pPage->iKey>=i
1029e 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20  Limit ){.       
1029f 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d   pCache->nPage--
102a0 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 3d 20  ;.        *pp = 
102a1 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20  pPage->pNext;.  
102a2 20 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e        pcache1Pin
102a3 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
102a4 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65       pcache1Free
102a5 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
102a6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
102a7 20 20 70 70 20 3d 20 26 70 50 61 67 65 2d 3e 70    pp = &pPage->p
102a8 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 54 45  Next;.        TE
102a9 53 54 4f 4e 4c 59 28 20 6e 50 61 67 65 2b 2b 20  STONLY( nPage++ 
102aa 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
102ab 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
102ac 43 61 63 68 65 2d 3e 6e 50 61 67 65 3d 3d 6e 50  Cache->nPage==nP
102ad 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  age );.}../*****
102ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
102b3 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61  **** sqlite3_pca
102b4 63 68 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a  che Methods ****
102b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
102b8 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
102b9 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
102ba 5f 70 63 61 63 68 65 2e 78 49 6e 69 74 20 6d 65  _pcache.xInit me
102bb 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
102bc 69 6e 74 20 70 63 61 63 68 65 31 49 6e 69 74 28  int pcache1Init(
102bd 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
102be 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
102bf 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d  ER(NotUsed);.  m
102c0 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c 20  emset(&pcache1, 
102c1 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65  0, sizeof(pcache
102c2 31 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  1));.  if( sqlit
102c3 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
102c4 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20  CoreMutex ){.   
102c5 20 70 63 61 63 68 65 31 2e 6d 75 74 65 78 20 3d   pcache1.mutex =
102c6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
102c7 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
102c8 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 3b 0a 20  X_STATIC_LRU);. 
102c9 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
102ca 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
102cb 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
102cc 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
102cd 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20 6d  ache.xShutdown m
102ce 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
102cf 20 76 6f 69 64 20 70 63 61 63 68 65 31 53 68 75   void pcache1Shu
102d0 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
102d1 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
102d2 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
102d3 29 3b 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f  );.  /* no-op */
102d4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
102d5 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
102d6 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
102d7 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a  Create method..*
102d8 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
102d9 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74  new cache..*/.st
102da 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61  atic sqlite3_pca
102db 63 68 65 20 2a 70 63 61 63 68 65 31 43 72 65 61  che *pcache1Crea
102dc 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69  te(int szPage, i
102dd 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 7b 0a  nt bPurgeable){.
102de 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
102df 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d 20 28  e;..  pCache = (
102e0 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69 74 65  PCache1 *)sqlite
102e1 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
102e2 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28  PCache1));.  if(
102e3 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6d   pCache ){.    m
102e4 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20 30 2c  emset(pCache, 0,
102e5 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31 29   sizeof(PCache1)
102e6 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 73  );.    pCache->s
102e7 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a  zPage = szPage;.
102e8 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50 75 72      pCache->bPur
102e9 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72 67 65  geable = (bPurge
102ea 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20  able ? 1 : 0);. 
102eb 20 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c     if( bPurgeabl
102ec 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68  e ){.      pCach
102ed 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20  e->nMin = 10;.  
102ee 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72      pcache1Enter
102ef 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 70  Mutex();.      p
102f0 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20  cache1.nMinPage 
102f1 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b  += pCache->nMin;
102f2 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 4c 65  .      pcache1Le
102f3 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  aveMutex();.    
102f4 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  }.  }.  return (
102f5 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
102f6 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  )pCache;.}../*.*
102f7 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
102f8 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
102f9 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a  pcache.xCachesiz
102fa 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a  e method. .**.**
102fb 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63   Configure the c
102fc 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20  ache_size limit 
102fd 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a  for a cache..*/.
102fe 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
102ff 68 65 31 43 61 63 68 65 73 69 7a 65 28 73 71 6c  he1Cachesize(sql
10300 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20  ite3_pcache *p, 
10301 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61  int nMax){.  PCa
10302 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28  che1 *pCache = (
10303 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 69  PCache1 *)p;.  i
10304 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  f( pCache->bPurg
10305 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61  eable ){.    pca
10306 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10307 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6e 4d  ;.    pcache1.nM
10308 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20  axPage += (nMax 
10309 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b  - pCache->nMax);
1030a 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61  .    pCache->nMa
1030b 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63  x = nMax;.    pc
1030c 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50  ache1EnforceMaxP
1030d 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61 63 68  age();.    pcach
1030e 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  e1LeaveMutex();.
1030f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
10310 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
10311 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
10312 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d 65 74  e.xPagecount met
10313 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20  hod. .*/.static 
10314 69 6e 74 20 70 63 61 63 68 65 31 50 61 67 65 63  int pcache1Pagec
10315 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 63 61  ount(sqlite3_pca
10316 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  che *p){.  int n
10317 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10318 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28  Mutex();.  n = (
10319 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e  (PCache1 *)p)->n
1031a 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65 31 4c  Page;.  pcache1L
1031b 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
1031c 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
1031d 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
1031e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
1031f 70 63 61 63 68 65 2e 78 46 65 74 63 68 20 6d 65  pcache.xFetch me
10320 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74  thod. .**.** Fet
10321 63 68 20 61 20 70 61 67 65 20 62 79 20 6b 65 79  ch a page by key
10322 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68   value..**.** Wh
10323 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e  ether or not a n
10324 65 77 20 70 61 67 65 20 6d 61 79 20 62 65 20 61  ew page may be a
10325 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73  llocated by this
10326 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64   function depend
10327 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75  s on.** the valu
10328 65 20 6f 66 20 74 68 65 20 63 72 65 61 74 65 46  e of the createF
10329 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  lag argument..**
1032a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68  .** There are th
1032b 72 65 65 20 64 69 66 66 65 72 65 6e 74 20 61 70  ree different ap
1032c 70 72 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 61  proaches to obta
1032d 69 6e 69 6e 67 20 73 70 61 63 65 20 66 6f 72 20  ining space for 
1032e 61 20 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e  a page,.** depen
1032f 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75  ding on the valu
10330 65 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20 63  e of parameter c
10331 72 65 61 74 65 46 6c 61 67 20 28 77 68 69 63 68  reateFlag (which
10332 20 6d 61 79 20 62 65 20 30 2c 20 31 20 6f 72 20   may be 0, 1 or 
10333 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52  2)..**.**   1. R
10334 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
10335 20 76 61 6c 75 65 20 6f 66 20 63 72 65 61 74 65   value of create
10336 46 6c 61 67 2c 20 74 68 65 20 63 61 63 68 65 20  Flag, the cache 
10337 69 73 20 73 65 61 72 63 68 65 64 20 66 6f 72 20  is searched for 
10338 61 20 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79 20  a .**      copy 
10339 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  of the requested
1033a 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 20 69 73   page. If one is
1033b 20 66 6f 75 6e 64 2c 20 69 74 20 69 73 20 72 65   found, it is re
1033c 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  turned..**.**   
1033d 32 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67  2. If createFlag
1033e 3d 3d 30 20 61 6e 64 20 74 68 65 20 70 61 67 65  ==0 and the page
1033f 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
10340 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 4e 55  in the cache, NU
10341 4c 4c 20 69 73 0a 2a 2a 20 20 20 20 20 20 72 65  LL is.**      re
10342 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  turned..**.**   
10343 33 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67  3. If createFlag
10344 20 69 73 20 31 2c 20 74 68 65 20 63 61 63 68 65   is 1, the cache
10345 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 70 75   is marked as pu
10346 72 67 65 61 62 6c 65 20 61 6e 64 20 74 68 65 20  rgeable and the 
10347 70 61 67 65 20 69 73 20 0a 2a 2a 20 20 20 20 20  page is .**     
10348 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20   not already in 
10349 74 68 65 20 63 61 63 68 65 2c 20 61 6e 64 20 69  the cache, and i
1034a 66 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20  f either of the 
1034b 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72  following are tr
1034c 75 65 2c 20 0a 2a 2a 20 20 20 20 20 20 72 65 74  ue, .**      ret
1034d 75 72 6e 20 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20  urn NULL:.**.** 
1034e 20 20 20 20 20 20 28 61 29 20 74 68 65 20 6e 75        (a) the nu
1034f 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69  mber of pages pi
10350 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68  nned by the cach
10351 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  e is greater tha
10352 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50  n.**           P
10353 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a  Cache1.nMax, or.
10354 2a 2a 20 20 20 20 20 20 20 28 62 29 20 74 68 65  **       (b) the
10355 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10356 20 70 69 6e 6e 65 64 20 62 79 20 74 68 65 20 63   pinned by the c
10357 61 63 68 65 20 69 73 20 67 72 65 61 74 65 72 20  ache is greater 
10358 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  than.**         
10359 20 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61    the sum of nMa
1035a 78 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 61  x for all purgea
1035b 62 6c 65 20 63 61 63 68 65 73 2c 20 6c 65 73 73  ble caches, less
1035c 20 74 68 65 20 73 75 6d 20 6f 66 20 0a 2a 2a 20   the sum of .** 
1035d 20 20 20 20 20 20 20 20 20 20 6e 4d 69 6e 20 66            nMin f
1035e 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 70 75 72  or all other pur
1035f 67 65 61 62 6c 65 20 63 61 63 68 65 73 2e 20 0a  geable caches. .
10360 2a 2a 0a 2a 2a 20 20 20 34 2e 20 49 66 20 6e 6f  **.**   4. If no
10361 6e 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ne of the first 
10362 74 68 72 65 65 20 63 6f 6e 64 69 74 69 6f 6e 73  three conditions
10363 20 61 70 70 6c 79 20 61 6e 64 20 74 68 65 20 63   apply and the c
10364 61 63 68 65 20 69 73 20 6d 61 72 6b 65 64 0a 2a  ache is marked.*
10365 2a 20 20 20 20 20 20 61 73 20 70 75 72 67 65 61  *      as purgea
10366 62 6c 65 2c 20 61 6e 64 20 69 66 20 6f 6e 65 20  ble, and if one 
10367 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
10368 20 69 73 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20   is true:.**.** 
10369 20 20 20 20 20 20 28 61 29 20 54 68 65 20 6e 75        (a) The nu
1036a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c  mber of pages al
1036b 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20  located for the 
1036c 63 61 63 68 65 20 69 73 20 61 6c 72 65 61 64 79  cache is already
1036d 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50   .**           P
1036e 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a  Cache1.nMax, or.
1036f 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20  **.**       (b) 
10370 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  The number of pa
10371 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ges allocated fo
10372 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20  r all purgeable 
10373 63 61 63 68 65 73 20 69 73 0a 2a 2a 20 20 20 20  caches is.**    
10374 20 20 20 20 20 20 20 61 6c 72 65 61 64 79 20 65         already e
10375 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74  qual to or great
10376 65 72 20 74 68 61 6e 20 74 68 65 20 73 75 6d 20  er than the sum 
10377 6f 66 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 0a  of nMax for all.
10378 2a 2a 20 20 20 20 20 20 20 20 20 20 20 70 75 72  **           pur
10379 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c 0a 2a  geable caches,.*
1037a 2a 0a 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 61  *.**      then a
1037b 74 74 65 6d 70 74 20 74 6f 20 72 65 63 79 63 6c  ttempt to recycl
1037c 65 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  e a page from th
1037d 65 20 4c 52 55 20 6c 69 73 74 2e 20 49 66 20 69  e LRU list. If i
1037e 74 20 69 73 20 74 68 65 20 72 69 67 68 74 0a 2a  t is the right.*
1037f 2a 20 20 20 20 20 20 73 69 7a 65 2c 20 72 65 74  *      size, ret
10380 75 72 6e 20 74 68 65 20 72 65 63 79 63 6c 65 64  urn the recycled
10381 20 62 75 66 66 65 72 2e 20 4f 74 68 65 72 77 69   buffer. Otherwi
10382 73 65 2c 20 66 72 65 65 20 74 68 65 20 62 75 66  se, free the buf
10383 66 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  fer and.**      
10384 70 72 6f 63 65 65 64 20 74 6f 20 73 74 65 70 20  proceed to step 
10385 35 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f  5. .**.**   5. O
10386 74 68 65 72 77 69 73 65 2c 20 61 6c 6c 6f 63 61  therwise, alloca
10387 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  te and return a 
10388 6e 65 77 20 70 61 67 65 20 62 75 66 66 65 72 2e  new page buffer.
10389 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1038a 2a 70 63 61 63 68 65 31 46 65 74 63 68 28 73 71  *pcache1Fetch(sq
1038b 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c  lite3_pcache *p,
1038c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b   unsigned int iK
1038d 65 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c  ey, int createFl
1038e 61 67 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  ag){.  unsigned 
1038f 69 6e 74 20 6e 50 69 6e 6e 65 64 3b 0a 20 20 50  int nPinned;.  P
10390 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d  Cache1 *pCache =
10391 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20   (PCache1 *)p;. 
10392 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d   PgHdr1 *pPage =
10393 20 30 3b 0a 0a 20 20 70 63 61 63 68 65 31 45 6e   0;..  pcache1En
10394 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66  terMutex();.  if
10395 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20  ( createFlag==1 
10396 29 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65  ) sqlite3BeginBe
10397 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20  nignMalloc();.. 
10398 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 68   /* Search the h
10399 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 61 6e  ash table for an
1039a 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 2e   existing entry.
1039b 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65   */.  if( pCache
1039c 2d 3e 6e 48 61 73 68 3e 30 20 29 7b 0a 20 20 20  ->nHash>0 ){.   
1039d 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20   unsigned int h 
1039e 3d 20 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d  = iKey % pCache-
1039f 3e 6e 48 61 73 68 3b 0a 20 20 20 20 66 6f 72 28  >nHash;.    for(
103a0 70 50 61 67 65 3d 70 43 61 63 68 65 2d 3e 61 70  pPage=pCache->ap
103a1 48 61 73 68 5b 68 5d 3b 20 70 50 61 67 65 26 26  Hash[h]; pPage&&
103a2 70 50 61 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65  pPage->iKey!=iKe
103a3 79 3b 20 70 50 61 67 65 3d 70 50 61 67 65 2d 3e  y; pPage=pPage->
103a4 70 4e 65 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69  pNext);.  }..  i
103a5 66 28 20 70 50 61 67 65 20 7c 7c 20 63 72 65 61  f( pPage || crea
103a6 74 65 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20  teFlag==0 ){.   
103a7 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28   pcache1PinPage(
103a8 70 50 61 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f  pPage);.    goto
103a9 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a   fetch_out;.  }.
103aa 0a 20 20 2f 2a 20 53 74 65 70 20 33 20 6f 66 20  .  /* Step 3 of 
103ab 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20  header comment. 
103ac 2a 2f 0a 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70  */.  nPinned = p
103ad 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70  Cache->nPage - p
103ae 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62  Cache->nRecyclab
103af 6c 65 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65  le;.  if( create
103b0 46 6c 61 67 3d 3d 31 20 26 26 20 70 43 61 63 68  Flag==1 && pCach
103b1 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 26  e->bPurgeable &&
103b2 20 28 0a 20 20 20 20 20 20 20 20 6e 50 69 6e 6e   (.        nPinn
103b3 65 64 3e 3d 28 70 63 61 63 68 65 31 2e 6e 4d 61  ed>=(pcache1.nMa
103b4 78 50 61 67 65 2b 70 43 61 63 68 65 2d 3e 6e 4d  xPage+pCache->nM
103b5 69 6e 2d 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50  in-pcache1.nMinP
103b6 61 67 65 29 0a 20 20 20 20 20 7c 7c 20 6e 50 69  age).     || nPi
103b7 6e 6e 65 64 3e 3d 28 70 43 61 63 68 65 2d 3e 6e  nned>=(pCache->n
103b8 4d 61 78 20 2a 20 39 20 2f 20 31 30 29 0a 20 20  Max * 9 / 10).  
103b9 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74  )){.    goto fet
103ba 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69  ch_out;.  }..  i
103bb 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65  f( pCache->nPage
103bc 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 20  >=pCache->nHash 
103bd 26 26 20 70 63 61 63 68 65 31 52 65 73 69 7a 65  && pcache1Resize
103be 48 61 73 68 28 70 43 61 63 68 65 29 20 29 7b 0a  Hash(pCache) ){.
103bf 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f      goto fetch_o
103c0 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  ut;.  }..  /* St
103c1 65 70 20 34 2e 20 54 72 79 20 74 6f 20 72 65 63  ep 4. Try to rec
103c2 79 63 6c 65 20 61 20 70 61 67 65 20 62 75 66 66  ycle a page buff
103c3 65 72 20 69 66 20 61 70 70 72 6f 70 72 69 61 74  er if appropriat
103c4 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63  e. */.  if( pCac
103c5 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26  he->bPurgeable &
103c6 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  & pcache1.pLruTa
103c7 69 6c 20 26 26 20 28 0a 20 20 20 20 20 28 70 43  il && (.     (pC
103c8 61 63 68 65 2d 3e 6e 50 61 67 65 2b 31 3e 3d 70  ache->nPage+1>=p
103c9 43 61 63 68 65 2d 3e 6e 4d 61 78 29 20 7c 7c 20  Cache->nMax) || 
103ca 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74  pcache1.nCurrent
103cb 50 61 67 65 3e 3d 70 63 61 63 68 65 31 2e 6e 4d  Page>=pcache1.nM
103cc 61 78 50 61 67 65 0a 20 20 29 29 7b 0a 20 20 20  axPage.  )){.   
103cd 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31   pPage = pcache1
103ce 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70  .pLruTail;.    p
103cf 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d  cache1RemoveFrom
103d0 48 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20  Hash(pPage);.   
103d1 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28   pcache1PinPage(
103d2 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  pPage);.    if( 
103d3 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 73  pPage->pCache->s
103d4 7a 50 61 67 65 21 3d 70 43 61 63 68 65 2d 3e 73  zPage!=pCache->s
103d5 7a 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70  zPage ){.      p
103d6 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70  cache1FreePage(p
103d7 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 50 61  Page);.      pPa
103d8 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ge = 0;.    }els
103d9 65 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  e{.      pcache1
103da 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 20 2d 3d  .nCurrentPage -=
103db 20 28 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d   (pPage->pCache-
103dc 3e 62 50 75 72 67 65 61 62 6c 65 20 2d 20 70 43  >bPurgeable - pC
103dd 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
103de 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
103df 2f 2a 20 53 74 65 70 20 35 2e 20 49 66 20 61 20  /* Step 5. If a 
103e0 75 73 61 62 6c 65 20 70 61 67 65 20 62 75 66 66  usable page buff
103e1 65 72 20 68 61 73 20 73 74 69 6c 6c 20 6e 6f 74  er has still not
103e2 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20 0a 20 20   been found, .  
103e3 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c  ** attempt to al
103e4 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 6f 6e 65  locate a new one
103e5 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70  . .  */.  if( !p
103e6 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67  Page ){.    pPag
103e7 65 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63  e = pcache1Alloc
103e8 50 61 67 65 28 70 43 61 63 68 65 29 3b 0a 20 20  Page(pCache);.  
103e9 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 29  }..  if( pPage )
103ea 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69  {.    unsigned i
103eb 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70 43  nt h = iKey % pC
103ec 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 20  ache->nHash;.   
103ed 20 2a 28 76 6f 69 64 20 2a 2a 29 28 50 47 48 44   *(void **)(PGHD
103ee 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67 65  R1_TO_PAGE(pPage
103ef 29 29 20 3d 20 30 3b 0a 20 20 20 20 70 43 61 63  )) = 0;.    pCac
103f0 68 65 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 20  he->nPage++;.   
103f1 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69   pPage->iKey = i
103f2 4b 65 79 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  Key;.    pPage->
103f3 70 4e 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e  pNext = pCache->
103f4 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 20 20 70  apHash[h];.    p
103f5 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70  Page->pCache = p
103f6 43 61 63 68 65 3b 0a 20 20 20 20 70 50 61 67 65  Cache;.    pPage
103f7 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a  ->pLruPrev = 0;.
103f8 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e      pPage->pLruN
103f9 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 43 61  ext = 0;.    pCa
103fa 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d  che->apHash[h] =
103fb 20 70 50 61 67 65 3b 0a 20 20 7d 0a 0a 66 65 74   pPage;.  }..fet
103fc 63 68 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 50  ch_out:.  if( pP
103fd 61 67 65 20 26 26 20 69 4b 65 79 3e 70 43 61 63  age && iKey>pCac
103fe 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20  he->iMaxKey ){. 
103ff 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b     pCache->iMaxK
10400 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 7d 0a 20  ey = iKey;.  }. 
10401 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d   if( createFlag=
10402 3d 31 20 29 20 73 71 6c 69 74 65 33 45 6e 64 42  =1 ) sqlite3EndB
10403 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
10404 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10405 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  ex();.  return (
10406 70 50 61 67 65 20 3f 20 50 47 48 44 52 31 5f 54  pPage ? PGHDR1_T
10407 4f 5f 50 41 47 45 28 70 50 61 67 65 29 20 3a 20  O_PAGE(pPage) : 
10408 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d  0);.}.../*.** Im
10409 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1040a 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  the sqlite3_pcac
1040b 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74 68 6f 64  he.xUnpin method
1040c 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 70  ..**.** Mark a p
1040d 61 67 65 20 61 73 20 75 6e 70 69 6e 6e 65 64 20  age as unpinned 
1040e 28 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 61 73  (eligible for as
1040f 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65 63 79 63  ynchronous recyc
10410 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ling)..*/.static
10411 20 76 6f 69 64 20 70 63 61 63 68 65 31 55 6e 70   void pcache1Unp
10412 69 6e 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  in(sqlite3_pcach
10413 65 20 2a 70 2c 20 76 6f 69 64 20 2a 70 50 67 2c  e *p, void *pPg,
10414 20 69 6e 74 20 72 65 75 73 65 55 6e 6c 69 6b 65   int reuseUnlike
10415 6c 79 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a  ly){.  PCache1 *
10416 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65  pCache = (PCache
10417 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20  1 *)p;.  PgHdr1 
10418 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f  *pPage = PAGE_TO
10419 5f 50 47 48 44 52 31 28 70 50 67 29 3b 0a 0a 20  _PGHDR1(pPg);.. 
1041a 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
1041b 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69  ex();..  /* It i
1041c 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63 61  s an error to ca
1041d 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
1041e 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
1041f 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a 20 70 61  already .  ** pa
10420 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c  rt of the global
10421 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a   LRU list..  */.
10422 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
10423 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 26 26 20  >pLruPrev==0 && 
10424 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3d  pPage->pLruNext=
10425 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
10426 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
10427 21 3d 70 50 61 67 65 20 26 26 20 70 63 61 63 68  !=pPage && pcach
10428 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d 70 50 61  e1.pLruTail!=pPa
10429 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 72 65 75  ge );..  if( reu
1042a 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c 20 70 63  seUnlikely || pc
1042b 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61  ache1.nCurrentPa
1042c 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61 78 50  ge>pcache1.nMaxP
1042d 61 67 65 20 29 7b 0a 20 20 20 20 70 63 61 63 68  age ){.    pcach
1042e 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68  e1RemoveFromHash
1042f 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63 61  (pPage);.    pca
10430 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50 61  che1FreePage(pPa
10431 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ge);.  }else{.  
10432 20 20 2f 2a 20 41 64 64 20 74 68 65 20 70 61 67    /* Add the pag
10433 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20  e to the global 
10434 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 6d 61 6c  LRU list. Normal
10435 6c 79 2c 20 74 68 65 20 70 61 67 65 20 69 73 20  ly, the page is 
10436 61 64 64 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20  added to.    ** 
10437 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 20  the head of the 
10438 6c 69 73 74 20 28 6c 61 73 74 20 70 61 67 65 20  list (last page 
10439 74 6f 20 62 65 20 72 65 63 79 63 6c 65 64 29 2e  to be recycled).
1043a 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65   However, if the
1043b 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 65 55 6e   .    ** reuseUn
1043c 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 61 73 73  likely flag pass
1043d 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ed to this funct
1043e 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65  ion is true, the
1043f 20 70 61 67 65 20 69 73 20 61 64 64 65 64 0a 20   page is added. 
10440 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 61 69     ** to the tai
10441 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28 66  l of the list (f
10442 69 72 73 74 20 70 61 67 65 20 74 6f 20 62 65 20  irst page to be 
10443 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 20 20 2a  recycled)..    *
10444 2f 0a 20 20 20 20 69 66 28 20 70 63 61 63 68 65  /.    if( pcache
10445 31 2e 70 4c 72 75 48 65 61 64 20 29 7b 0a 20 20  1.pLruHead ){.  
10446 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75      pcache1.pLru
10447 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 76 20 3d  Head->pLruPrev =
10448 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50   pPage;.      pP
10449 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20  age->pLruNext = 
1044a 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
1044b 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  ;.      pcache1.
1044c 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67 65  pLruHead = pPage
1044d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1044e 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54     pcache1.pLruT
1044f 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 20  ail = pPage;.   
10450 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48     pcache1.pLruH
10451 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 20  ead = pPage;.   
10452 20 7d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e   }.    pCache->n
10453 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20  Recyclable++;.  
10454 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  }..  pcache1Leav
10455 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a  eMutex();.}../*.
10456 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
10457 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
10458 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 79 20 6d  _pcache.xRekey m
10459 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69  ethod. .*/.stati
1045a 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 52 65  c void pcache1Re
1045b 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 5f 70  key(.  sqlite3_p
1045c 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 6f 69 64  cache *p,.  void
1045d 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 67 6e 65   *pPg,.  unsigne
1045e 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 20 75 6e  d int iOld,.  un
1045f 73 69 67 6e 65 64 20 69 6e 74 20 69 4e 65 77 0a  signed int iNew.
10460 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  ){.  PCache1 *pC
10461 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20  ache = (PCache1 
10462 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70  *)p;.  PgHdr1 *p
10463 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f 5f 50  Page = PAGE_TO_P
10464 47 48 44 52 31 28 70 50 67 29 3b 0a 20 20 50 67  GHDR1(pPg);.  Pg
10465 48 64 72 31 20 2a 2a 70 70 3b 0a 20 20 75 6e 73  Hdr1 **pp;.  uns
10466 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 0a 20 20  igned int h; .  
10467 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
10468 4b 65 79 3d 3d 69 4f 6c 64 20 29 3b 0a 0a 20 20  Key==iOld );..  
10469 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
1046a 78 28 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64  x();..  h = iOld
1046b 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a  %pCache->nHash;.
1046c 20 20 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e    pp = &pCache->
1046d 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69  apHash[h];.  whi
1046e 6c 65 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65  le( (*pp)!=pPage
1046f 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a   ){.    pp = &(*
10470 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  pp)->pNext;.  }.
10471 20 20 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70    *pp = pPage->p
10472 4e 65 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65  Next;..  h = iNe
10473 77 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b  w%pCache->nHash;
10474 0a 20 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d  .  pPage->iKey =
10475 20 69 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e   iNew;.  pPage->
10476 70 4e 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e  pNext = pCache->
10477 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61  apHash[h];.  pCa
10478 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d  che->apHash[h] =
10479 20 70 50 61 67 65 3b 0a 0a 20 20 69 66 28 20 69   pPage;..  if( i
1047a 4e 65 77 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78  New>pCache->iMax
1047b 4b 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 68  Key ){.    pCach
1047c 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65  e->iMaxKey = iNe
1047d 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65  w;.  }..  pcache
1047e 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d  1LeaveMutex();.}
1047f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
10480 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
10481 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 54 72  lite3_pcache.xTr
10482 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a  uncate method. .
10483 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c  **.** Discard al
10484 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73  l unpinned pages
10485 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 69   in the cache wi
10486 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  th a page number
10487 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20   equal to.** or 
10488 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 61 72  greater than par
10489 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 2e 20 41  ameter iLimit. A
1048a 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20  ny pinned pages 
1048b 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62  with a page numb
1048c 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f  er.** equal to o
1048d 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69  r greater than i
1048e 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 6c 69 63  Limit are implic
1048f 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a  itly unpinned..*
10490 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
10491 61 63 68 65 31 54 72 75 6e 63 61 74 65 28 73 71  ache1Truncate(sq
10492 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c  lite3_pcache *p,
10493 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c   unsigned int iL
10494 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 68 65 31  imit){.  PCache1
10495 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63   *pCache = (PCac
10496 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 63 68  he1 *)p;.  pcach
10497 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  e1EnterMutex();.
10498 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43    if( iLimit<=pC
10499 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b  ache->iMaxKey ){
1049a 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75 6e  .    pcache1Trun
1049b 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 68  cateUnsafe(pCach
1049c 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  e, iLimit);.    
1049d 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20  pCache->iMaxKey 
1049e 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a  = iLimit-1;.  }.
1049f 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
104a0 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex();.}../*.** 
104a1 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
104a2 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
104a3 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20 6d 65  ache.xDestroy me
104a4 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73  thod. .**.** Des
104a5 74 72 6f 79 20 61 20 63 61 63 68 65 20 61 6c 6c  troy a cache all
104a6 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70 63 61  ocated using pca
104a7 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f  che1Create()..*/
104a8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
104a9 63 68 65 31 44 65 73 74 72 6f 79 28 73 71 6c 69  che1Destroy(sqli
104aa 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a  te3_pcache *p){.
104ab 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
104ac 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70  e = (PCache1 *)p
104ad 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
104ae 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68  Mutex();.  pcach
104af 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65  e1TruncateUnsafe
104b0 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20 70  (pCache, 0);.  p
104b1 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20  cache1.nMaxPage 
104b2 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b  -= pCache->nMax;
104b3 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50  .  pcache1.nMinP
104b4 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e  age -= pCache->n
104b5 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e  Min;.  pcache1En
104b6 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b 0a  forceMaxPage();.
104b7 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
104b8 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74 65 33  tex();.  sqlite3
104b9 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e 61 70  _free(pCache->ap
104ba 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33  Hash);.  sqlite3
104bb 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b 0a 7d  _free(pCache);.}
104bc 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
104bd 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
104be 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a  during initializ
104bf 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69  ation (sqlite3_i
104c0 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a  nitialize()) to.
104c1 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65 20 64  ** install the d
104c2 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62 6c 65  efault pluggable
104c3 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61   cache module, a
104c4 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73 65 72  ssuming the user
104c5 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65   has not.** alre
104c6 61 64 79 20 70 72 6f 76 69 64 65 64 20 61 6e 20  ady provided an 
104c7 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a  alternative..*/.
104c8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
104c9 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68  oid sqlite3PCach
104ca 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64  eSetDefault(void
104cb 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
104cc 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
104cd 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  ds defaultMethod
104ce 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20  s = {.    0,    
104cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
104d0 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20     /* pArg */.  
104d1 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c 20 20    pcache1Init,  
104d2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 49             /* xI
104d3 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68  nit */.    pcach
104d4 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20 20 20  e1Shutdown,     
104d5 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e      /* xShutdown
104d6 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43   */.    pcache1C
104d7 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  reate,          
104d8 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f 0a 20   /* xCreate */. 
104d9 20 20 20 70 63 61 63 68 65 31 43 61 63 68 65 73     pcache1Caches
104da 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78  ize,        /* x
104db 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20  Cachesize */.   
104dc 20 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e   pcache1Pagecoun
104dd 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 50 61  t,        /* xPa
104de 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70  gecount */.    p
104df 63 61 63 68 65 31 46 65 74 63 68 2c 20 20 20 20  cache1Fetch,    
104e0 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65 74 63          /* xFetc
104e1 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31  h */.    pcache1
104e2 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20 20 20  Unpin,          
104e3 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20    /* xUnpin */. 
104e4 20 20 20 70 63 61 63 68 65 31 52 65 6b 65 79 2c     pcache1Rekey,
104e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
104e6 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70 63 61  Rekey */.    pca
104e7 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20 20 20  che1Truncate,   
104e8 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61        /* xTrunca
104e9 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  te */.    pcache
104ea 31 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20  1Destroy        
104eb 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a     /* xDestroy *
104ec 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33  /.  };.  sqlite3
104ed 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43  _config(SQLITE_C
104ee 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20 26 64  ONFIG_PCACHE, &d
104ef 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a  efaultMethods);.
104f0 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
104f1 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
104f2 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20  ANAGEMENT./*.** 
104f3 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
104f4 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 65 20   called to free 
104f5 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 6e 61  superfluous dyna
104f6 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
104f7 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64  d memory.** held
104f8 20 62 79 20 74 68 65 20 70 61 67 65 72 20 73 79   by the pager sy
104f9 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20  stem. Memory in 
104fa 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74  use by any SQLit
104fb 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65  e pager allocate
104fc 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72  d.** by the curr
104fd 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 20 62  ent thread may b
104fe 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  e sqlite3_free()
104ff 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69  ed..**.** nReq i
10500 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
10501 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
10502 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 20 74  required. Once t
10503 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20  his much has.** 
10504 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c 20 74  been released, t
10505 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
10506 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 6e 20  rns. The return 
10507 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74  value is the tot
10508 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66  al number .** of
10509 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
1050a 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51   released..*/.SQ
1050b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1050c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
1050d 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20  leaseMemory(int 
1050e 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72  nReq){.  int nFr
1050f 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 63  ee = 0;.  if( pc
10510 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d 30 20  ache1.pStart==0 
10511 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70  ){.    PgHdr1 *p
10512 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e 74  ;.    pcache1Ent
10513 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 77  erMutex();.    w
10514 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c  hile( (nReq<0 ||
10515 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 26 20   nFree<nReq) && 
10516 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 54  (p=pcache1.pLruT
10517 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20 6e 46  ail) ){.      nF
10518 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61  ree += sqlite3Ma
10519 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20  llocSize(p);.   
1051a 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67     pcache1PinPag
1051b 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63  e(p);.      pcac
1051c 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73  he1RemoveFromHas
1051d 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63  h(p);.      pcac
1051e 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b 0a  he1FreePage(p);.
1051f 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
10520 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
10521 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65   }.  return nFre
10522 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  e;.}.#endif /* S
10523 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
10524 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a  ORY_MANAGEMENT *
10525 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
10526 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73  _TEST./*.** This
10527 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
10528 64 20 62 79 20 74 65 73 74 20 70 72 6f 63 65 64  d by test proced
10529 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63 74 20  ures to inspect 
1052a 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61  the internal sta
1052b 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f  te.** of the glo
1052c 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51  bal cache..*/.SQ
1052d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1052e 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
1052f 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43  tats(.  int *pnC
10530 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20  urrent,      /* 
10531 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65  OUT: Total numbe
10532 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 65  r of pages cache
10533 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61  d */.  int *pnMa
10534 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  x,          /* O
10535 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d  UT: Global maxim
10536 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f  um cache size */
10537 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20  .  int *pnMin,  
10538 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
10539 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31 2e 6e  Sum of PCache1.n
1053a 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61 62 6c  Min for purgeabl
1053b 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e  e caches */.  in
1053c 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 20  t *pnRecyclable 
1053d 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c     /* OUT: Total
1053e 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
1053f 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72   available for r
10540 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20  ecycling */.){. 
10541 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e   PgHdr1 *p;.  in
10542 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d 20  t nRecyclable = 
10543 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61 63 68  0;.  for(p=pcach
10544 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70 3b 20  e1.pLruHead; p; 
10545 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a  p=p->pLruNext){.
10546 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b      nRecyclable+
10547 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72  +;.  }.  *pnCurr
10548 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e 6e 43  ent = pcache1.nC
10549 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70  urrentPage;.  *p
1054a 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31 2e 6e  nMax = pcache1.n
1054b 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69  MaxPage;.  *pnMi
1054c 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e  n = pcache1.nMin
1054d 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63  Page;.  *pnRecyc
1054e 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61  lable = nRecycla
1054f 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ble;.}.#endif../
10550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
10551 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e 63 20  nd of pcache1.c 
10552 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10553 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
10555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
10556 65 67 69 6e 20 66 69 6c 65 20 72 6f 77 73 65 74  egin file rowset
10557 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
10558 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1055a 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63 65 6d 62  *.** 2008 Decemb
1055b 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  er 3.**.** The a
1055c 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
1055d 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
1055e 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
1055f 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
10560 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
10561 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
10562 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
10563 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
10564 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
10565 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
10566 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
10567 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
10568 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
10569 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
1056a 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
1056b 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
1056c 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
1056d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1056e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1056f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10571 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d  ***.**.** This m
10572 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73  odule implements
10573 20 61 6e 20 6f 62 6a 65 63 74 20 77 65 20 63 61   an object we ca
10574 6c 6c 20 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a  ll a "RowSet"..*
10575 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20  *.** The RowSet 
10576 6f 62 6a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c  object is a coll
10577 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73  ection of rowids
10578 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65  .  Rowids.** are
10579 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
1057a 68 65 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20  he RowSet in an 
1057b 61 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e  arbitrary order.
1057c 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e    Inserts.** can
1057d 20 62 65 20 69 6e 74 65 72 6d 69 78 65 64 20 77   be intermixed w
1057e 69 74 68 20 74 65 73 74 73 20 74 6f 20 73 65 65  ith tests to see
1057f 20 69 66 20 61 20 67 69 76 65 6e 20 72 6f 77 69   if a given rowi
10580 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72  d has been.** pr
10581 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65  eviously inserte
10582 64 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65  d into the RowSe
10583 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61  t..**.** After a
10584 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 65 20 66  ll inserts are f
10585 69 6e 69 73 68 65 64 2c 20 69 74 20 69 73 20 70  inished, it is p
10586 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 72 61  ossible to extra
10587 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e  ct the.** elemen
10588 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74  ts of the RowSet
10589 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
1058a 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 65 78 74  .  Once this ext
1058b 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65  raction.** proce
1058c 73 73 20 68 61 73 20 73 74 61 72 74 65 64 2c 20  ss has started, 
1058d 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20  no new elements 
1058e 6d 61 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e  may be inserted.
1058f 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68  .**.** Hence, th
10590 65 20 70 72 69 6d 69 74 69 76 65 20 6f 70 65 72  e primitive oper
10591 61 74 69 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77  ations for a Row
10592 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20  Set are:.**.**  
10593 20 20 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 49    CREATE.**    I
10594 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54 45 53 54  NSERT.**    TEST
10595 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a  .**    SMALLEST.
10596 2a 2a 20 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a  **    DESTROY.**
10597 0a 2a 2a 20 54 68 65 20 43 52 45 41 54 45 20 61  .** The CREATE a
10598 6e 64 20 44 45 53 54 52 4f 59 20 70 72 69 6d 69  nd DESTROY primi
10599 74 69 76 65 73 20 61 72 65 20 74 68 65 20 63 6f  tives are the co
1059a 6e 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 65  nstructor and de
1059b 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76  structor,.** obv
1059c 69 6f 75 73 6c 79 2e 20 20 54 68 65 20 49 4e 53  iously.  The INS
1059d 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 61 64  ERT primitive ad
1059e 64 73 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74  ds a new element
1059f 20 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a   to the RowSet..
105a0 2a 2a 20 54 45 53 54 20 63 68 65 63 6b 73 20 74  ** TEST checks t
105a1 6f 20 73 65 65 20 69 66 20 61 6e 20 65 6c 65 6d  o see if an elem
105a2 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69  ent is already i
105a3 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 20 20 53  n the RowSet.  S
105a4 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61  MALLEST.** extra
105a5 63 74 73 20 74 68 65 20 6c 65 61 73 74 20 76 61  cts the least va
105a6 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52 6f 77  lue from the Row
105a7 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49  Set..**.** The I
105a8 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20  NSERT primitive 
105a9 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61  might allocate a
105aa 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79  dditional memory
105ab 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20  .  Memory is.** 
105ac 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63 68 75  allocated in chu
105ad 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45  nks so most INSE
105ae 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61  RTs do no alloca
105af 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 69 73 20  tion.  There is 
105b0 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75  an .** upper bou
105b1 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f  nd on the size o
105b2 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  f allocated memo
105b3 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69  ry.  No memory i
105b4 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c  s freed.** until
105b5 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20   DESTROY..**.** 
105b6 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69  The TEST primiti
105b7 76 65 20 69 6e 63 6c 75 64 65 73 20 61 20 22 62  ve includes a "b
105b8 61 74 63 68 22 20 6e 75 6d 62 65 72 2e 20 20 54  atch" number.  T
105b9 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76  he TEST primitiv
105ba 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73  e.** will only s
105bb 65 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74  ee elements that
105bc 20 77 65 72 65 20 69 6e 73 65 72 74 65 64 20 62   were inserted b
105bd 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 63  efore the last c
105be 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20  hange.** in the 
105bf 62 61 74 63 68 20 6e 75 6d 62 65 72 2e 20 20 49  batch number.  I
105c0 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69  n other words, i
105c1 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 63 75  f an INSERT occu
105c2 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77  rs between.** tw
105c3 6f 20 54 45 53 54 73 20 77 68 65 72 65 20 74 68  o TESTs where th
105c4 65 20 54 45 53 54 73 20 68 61 76 65 20 74 68 65  e TESTs have the
105c5 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 62 6d   same batch nubm
105c6 65 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  er, then the.** 
105c7 76 61 6c 75 65 20 61 64 64 65 64 20 62 79 20 74  value added by t
105c8 68 65 20 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e  he INSERT will n
105c9 6f 74 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f  ot be visible to
105ca 20 74 68 65 20 73 65 63 6f 6e 64 20 54 45 53 54   the second TEST
105cb 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69 61 6c  ..** The initial
105cc 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73   batch number is
105cd 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74 68 65   zero, so if the
105ce 20 76 65 72 79 20 66 69 72 73 74 20 54 45 53 54   very first TEST
105cf 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e   contains.** a n
105d0 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20 6e 75  on-zero batch nu
105d1 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c 20 73 65  mber, it will se
105d2 65 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45  e all prior INSE
105d3 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e  RTs..**.** No IN
105d4 53 45 52 54 73 20 6d 61 79 20 6f 63 63 75 72 73  SERTs may occurs
105d5 20 61 66 74 65 72 20 61 20 53 4d 41 4c 4c 45 53   after a SMALLES
105d6 54 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  T.  An assertion
105d7 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a   will fail if.**
105d8 20 74 68 61 74 20 69 73 20 61 74 74 65 6d 70 74   that is attempt
105d9 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ed..**.** The co
105da 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20  st of an INSERT 
105db 69 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74  is roughly const
105dc 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20  ant.  (Sometime 
105dd 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61  new memory.** ha
105de 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65  s to be allocate
105df 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29  d on an INSERT.)
105e0 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20    The cost of a 
105e1 54 45 53 54 20 77 69 74 68 20 61 20 6e 65 77 0a  TEST with a new.
105e2 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20  ** batch number 
105e3 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72  is O(NlogN) wher
105e4 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
105e5 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
105e6 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20   the RowSet..** 
105e7 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45  The cost of a TE
105e8 53 54 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  ST using the sam
105e9 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69  e batch number i
105ea 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20  s O(logN).  The 
105eb 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66  cost.** of the f
105ec 69 72 73 74 20 53 4d 41 4c 4c 45 53 54 20 69 73  irst SMALLEST is
105ed 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f   O(NlogN).  Seco
105ee 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
105ef 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72  t SMALLEST.** pr
105f0 69 6d 69 74 69 76 65 73 20 61 72 65 20 63 6f 6e  imitives are con
105f1 73 74 61 6e 74 20 74 69 6d 65 2e 20 20 54 68 65  stant time.  The
105f2 20 63 6f 73 74 20 6f 66 20 44 45 53 54 52 4f 59   cost of DESTROY
105f3 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20   is O(N)..**.** 
105f4 54 68 65 72 65 20 69 73 20 61 6e 20 61 64 64 65  There is an adde
105f5 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77  d cost of O(N) w
105f6 68 65 6e 20 73 77 69 74 63 68 69 6e 67 20 62 65  hen switching be
105f7 74 77 65 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a  tween TEST and.*
105f8 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69  * SMALLEST primi
105f9 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  tives..**.** $Id
105fa 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31 2e 36  : rowset.c,v 1.6
105fb 20 32 30 30 39 2f 30 34 2f 32 32 20 31 35 3a 33   2009/04/22 15:3
105fc 32 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a 2a  2:59 drh Exp $.*
105fd 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67 65 74  /.../*.** Target
105fe 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f 63 61   size for alloca
105ff 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a 2f 0a  tion chunks..*/.
10600 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 41  #define ROWSET_A
10601 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 20 31  LLOCATION_SIZE 1
10602 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  024../*.** The n
10603 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20  umber of rowset 
10604 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f  entries per allo
10605 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f  cation chunk..*/
10606 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f  .#define ROWSET_
10607 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20  ENTRY_PER_CHUNK 
10608 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
10609 20 20 20 20 20 20 20 20 20 20 28 28 52 4f 57 53            ((ROWS
1060a 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49  ET_ALLOCATION_SI
1060b 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73 74 72  ZE-8)/sizeof(str
1060c 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29  uct RowSetEntry)
1060d 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e  )../*.** Each en
1060e 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20  try in a RowSet 
1060f 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
10610 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
10611 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63  object..*/.struc
10612 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 7b 20  t RowSetEntry { 
10613 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 69 36             .  i6
10614 34 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  4 v;            
10615 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
10616 4f 57 49 44 20 76 61 6c 75 65 20 66 6f 72 20 74  OWID value for t
10617 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73  his entry */.  s
10618 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
10619 79 20 2a 70 52 69 67 68 74 3b 20 20 20 2f 2a 20  y *pRight;   /* 
1061a 52 69 67 68 74 20 73 75 62 74 72 65 65 20 28 6c  Right subtree (l
1061b 61 72 67 65 72 20 65 6e 74 72 69 65 73 29 20 6f  arger entries) o
1061c 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75  r list */.  stru
1061d 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
1061e 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66  pLeft;    /* Lef
1061f 74 20 73 75 62 74 72 65 65 20 28 73 6d 61 6c 6c  t subtree (small
10620 65 72 20 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d  er entries) */.}
10621 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45  ;../*.** RowSetE
10622 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 61 72 65  ntry objects are
10623 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61   allocated in la
10624 72 67 65 20 63 68 75 6e 6b 73 20 28 69 6e 73 74  rge chunks (inst
10625 61 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20  ances of the.** 
10626 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
10627 75 72 65 29 20 74 6f 20 72 65 64 75 63 65 20 6d  ure) to reduce m
10628 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
10629 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 65 0a   overhead.  The.
1062a 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65 20 6b 65  ** chunks are ke
1062b 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c  pt on a linked l
1062c 69 73 74 20 73 6f 20 74 68 61 74 20 74 68 65 79  ist so that they
1062d 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61   can be dealloca
1062e 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ted.** when the 
1062f 52 6f 77 53 65 74 20 69 73 20 64 65 73 74 72 6f  RowSet is destro
10630 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52  yed..*/.struct R
10631 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73  owSetChunk {.  s
10632 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e  truct RowSetChun
10633 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20  k *pNextChunk;  
10634 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68        /* Next ch
10635 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74  unk on list of t
10636 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72  hem all */.  str
10637 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
10638 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e  aEntry[ROWSET_EN
10639 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20  TRY_PER_CHUNK]; 
1063a 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74  /* Allocated ent
1063b 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ries */.};../*.*
1063c 2a 20 41 20 52 6f 77 53 65 74 20 69 6e 20 61 6e  * A RowSet in an
1063d 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
1063e 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
1063f 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79  ture..**.** A ty
10640 70 65 64 65 66 20 6f 66 20 74 68 69 73 20 73 74  pedef of this st
10641 72 75 63 74 75 72 65 20 69 66 20 66 6f 75 6e 64  ructure if found
10642 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e   in sqliteInt.h.
10643 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65  .*/.struct RowSe
10644 74 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77  t {.  struct Row
10645 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b  SetChunk *pChunk
10646 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  ;    /* List of 
10647 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61  all chunk alloca
10648 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74  tions */.  sqlit
10649 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
1064a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1064b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1064c 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74  tion */.  struct
1064d 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45   RowSetEntry *pE
1064e 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74  ntry;    /* List
1064f 20 6f 66 20 65 6e 74 72 69 65 73 20 75 73 69 6e   of entries usin
10650 67 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 73 74  g pRight */.  st
10651 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
10652 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20   *pLast;     /* 
10653 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68  Last entry on th
10654 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f  e pEntry list */
10655 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
10656 45 6e 74 72 79 20 2a 70 46 72 65 73 68 3b 20 20  Entry *pFresh;  
10657 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e    /* Source of n
10658 65 77 20 65 6e 74 72 79 20 6f 62 6a 65 63 74 73  ew entry objects
10659 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77   */.  struct Row
1065a 53 65 74 45 6e 74 72 79 20 2a 70 54 72 65 65 3b  SetEntry *pTree;
1065b 20 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 20 74       /* Binary t
1065c 72 65 65 20 6f 66 20 65 6e 74 72 69 65 73 20 2a  ree of entries *
1065d 2f 0a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20  /.  u16 nFresh; 
1065e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1065f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
10660 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73  objects on pFres
10661 68 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74  h */.  u8 isSort
10662 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
10663 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
10664 20 70 45 6e 74 72 79 20 69 73 20 73 6f 72 74 65   pEntry is sorte
10665 64 20 2a 2f 0a 20 20 75 38 20 69 42 61 74 63 68  d */.  u8 iBatch
10666 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10667 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
10668 20 69 6e 73 65 72 74 20 62 61 74 63 68 20 2a 2f   insert batch */
10669 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20  .};../*.** Turn 
1066a 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f  bulk memory into
1066b 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74   a RowSet object
1066c 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65  .  N bytes of me
1066d 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69  mory.** are avai
1066e 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e  lable at pSpace.
1066f 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72    The db pointer
10670 20 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65   is used as a me
10671 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20  mory context.** 
10672 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65  for any subseque
10673 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74  nt allocations t
10674 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75  hat need to occu
10675 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  r..** Return a p
10676 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
10677 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e  w RowSet object.
10678 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62  .**.** It must b
10679 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20  e the case that 
1067a 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20  N is sufficient 
1067b 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74  to make a Rowset
1067c 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20  .  If not.** an 
1067d 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
1067e 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49  occurs..** .** I
1067f 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68  f N is larger th
10680 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20  an the minimum, 
10681 75 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20  use the surplus 
10682 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a  as an initial.**
10683 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65   allocation of e
10684 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65  ntries available
10685 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a   to be filled..*
10686 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10687 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33   RowSet *sqlite3
10688 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74  RowSetInit(sqlit
10689 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53  e3 *db, void *pS
1068a 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69  pace, unsigned i
1068b 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20  nt N){.  RowSet 
1068c 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20  *p;.  assert( N 
1068d 3e 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b  >= sizeof(*p) );
1068e 0a 20 20 70 20 3d 20 70 53 70 61 63 65 3b 0a 20  .  p = pSpace;. 
1068f 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a   p->pChunk = 0;.
10690 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20    p->db = db;.  
10691 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20  p->pEntry = 0;. 
10692 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20   p->pLast = 0;. 
10693 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20   p->pTree = 0;. 
10694 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 28 73 74   p->pFresh = (st
10695 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
10696 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d 3e 6e 46  *)&p[1];.  p->nF
10697 72 65 73 68 20 3d 20 28 75 31 36 29 28 28 4e 20  resh = (u16)((N 
10698 2d 20 73 69 7a 65 6f 66 28 2a 70 29 29 2f 73 69  - sizeof(*p))/si
10699 7a 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53  zeof(struct RowS
1069a 65 74 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e  etEntry));.  p->
1069b 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20  isSorted = 1;.  
1069c 70 2d 3e 69 42 61 74 63 68 20 3d 20 30 3b 0a 20  p->iBatch = 0;. 
1069d 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
1069e 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61  .** Deallocate a
1069f 6c 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61  ll chunks from a
106a0 20 52 6f 77 53 65 74 2e 20 20 54 68 69 73 20 66   RowSet.  This f
106a1 72 65 65 73 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  rees all memory 
106a2 74 68 61 74 0a 2a 2a 20 74 68 65 20 52 6f 77 53  that.** the RowS
106a3 65 74 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64  et has allocated
106a4 20 6f 76 65 72 20 69 74 73 20 6c 69 66 65 74 69   over its lifeti
106a5 6d 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  me.  This routin
106a6 65 20 69 73 0a 2a 2a 20 74 68 65 20 64 65 73 74  e is.** the dest
106a7 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 52  ructor for the R
106a8 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  owSet..*/.SQLITE
106a9 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
106aa 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72  lite3RowSetClear
106ab 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 73  (RowSet *p){.  s
106ac 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e  truct RowSetChun
106ad 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65 78  k *pChunk, *pNex
106ae 74 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70 43  tChunk;.  for(pC
106af 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20  hunk=p->pChunk; 
106b0 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d  pChunk; pChunk =
106b1 20 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20 20   pNextChunk){.  
106b2 20 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70    pNextChunk = p
106b3 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 43 68 75 6e  Chunk->pNextChun
106b4 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  k;.    sqlite3Db
106b5 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75  Free(p->db, pChu
106b6 6e 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43  nk);.  }.  p->pC
106b7 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e  hunk = 0;.  p->n
106b8 46 72 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e  Fresh = 0;.  p->
106b9 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d  pEntry = 0;.  p-
106ba 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d  >pLast = 0;.  p-
106bb 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d  >pTree = 0;.  p-
106bc 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d  >isSorted = 1;.}
106bd 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61  ../*.** Insert a
106be 20 6e 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20   new value into 
106bf 61 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20  a RowSet..**.** 
106c0 54 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  The mallocFailed
106c1 20 66 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74   flag of the dat
106c2 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
106c3 20 69 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20   is set if a.** 
106c4 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
106c5 6e 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49  n fails..*/.SQLI
106c6 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
106c7 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73  sqlite3RowSetIns
106c8 65 72 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69  ert(RowSet *p, i
106c9 36 34 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72  64 rowid){.  str
106ca 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
106cb 2a 70 45 6e 74 72 79 3b 20 20 2f 2a 20 54 68 65  *pEntry;  /* The
106cc 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20   new entry */.  
106cd 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
106ce 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 2f 2a 20  ry *pLast;   /* 
106cf 54 68 65 20 6c 61 73 74 20 70 72 69 6f 72 20 65  The last prior e
106d0 6e 74 72 79 20 2a 2f 0a 20 20 61 73 73 65 72 74  ntry */.  assert
106d1 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ( p!=0 );.  if( 
106d2 70 2d 3e 6e 46 72 65 73 68 3d 3d 30 20 29 7b 0a  p->nFresh==0 ){.
106d3 20 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65      struct RowSe
106d4 74 43 68 75 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20  tChunk *pNew;.  
106d5 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
106d6 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64  DbMallocRaw(p->d
106d7 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  b, sizeof(*pNew)
106d8 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d  );.    if( pNew=
106d9 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
106da 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e  rn;.    }.    pN
106db 65 77 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 20 3d  ew->pNextChunk =
106dc 20 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20   p->pChunk;.    
106dd 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77  p->pChunk = pNew
106de 3b 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20  ;.    p->pFresh 
106df 3d 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a  = pNew->aEntry;.
106e0 20 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20      p->nFresh = 
106e1 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52  ROWSET_ENTRY_PER
106e2 5f 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45  _CHUNK;.  }.  pE
106e3 6e 74 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68  ntry = p->pFresh
106e4 2b 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d  ++;.  p->nFresh-
106e5 2d 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d  -;.  pEntry->v =
106e6 20 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79   rowid;.  pEntry
106e7 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  ->pRight = 0;.  
106e8 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74  pLast = p->pLast
106e9 3b 0a 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b  ;.  if( pLast ){
106ea 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f  .    if( p->isSo
106eb 72 74 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70  rted && rowid<=p
106ec 4c 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20  Last->v ){.     
106ed 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30   p->isSorted = 0
106ee 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73  ;.    }.    pLas
106ef 74 2d 3e 70 52 69 67 68 74 20 3d 20 70 45 6e 74  t->pRight = pEnt
106f0 72 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ry;.  }else{.   
106f1 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74   assert( p->pEnt
106f2 72 79 3d 3d 30 20 29 3b 20 2f 2a 20 46 69 72 65  ry==0 ); /* Fire
106f3 73 20 69 66 20 49 4e 53 45 52 54 20 61 66 74 65  s if INSERT afte
106f4 72 20 53 4d 41 4c 4c 45 53 54 20 2a 2f 0a 20 20  r SMALLEST */.  
106f5 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45    p->pEntry = pE
106f6 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  ntry;.  }.  p->p
106f7 4c 61 73 74 20 3d 20 70 45 6e 74 72 79 3b 0a 7d  Last = pEntry;.}
106f8 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77  ../*.** Merge tw
106f9 6f 20 6c 69 73 74 73 20 6f 66 20 52 6f 77 53 65  o lists of RowSe
106fa 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 20  tEntry objects. 
106fb 20 52 65 6d 6f 76 65 20 64 75 70 6c 69 63 61 74   Remove duplicat
106fc 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  es..**.** The in
106fd 70 75 74 20 6c 69 73 74 73 20 61 72 65 20 63 6f  put lists are co
106fe 6e 6e 65 63 74 65 64 20 76 69 61 20 70 52 69 67  nnected via pRig
106ff 68 74 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20  ht pointers and 
10700 61 72 65 20 0a 2a 2a 20 61 73 73 75 6d 65 64 20  are .** assumed 
10701 74 6f 20 65 61 63 68 20 61 6c 72 65 61 64 79 20  to each already 
10702 62 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64  be in sorted ord
10703 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74  er..*/.static st
10704 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
10705 20 2a 72 6f 77 53 65 74 4d 65 72 67 65 28 0a 20   *rowSetMerge(. 
10706 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
10707 74 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46  try *pA,    /* F
10708 69 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74  irst sorted list
10709 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f   to be merged */
1070a 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
1070b 45 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a  Entry *pB     /*
1070c 20 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c   Second sorted l
1070d 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64  ist to be merged
1070e 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
1070f 52 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61 64  RowSetEntry head
10710 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  ;.  struct RowSe
10711 74 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a  tEntry *pTail;..
10712 20 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b    pTail = &head;
10713 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20  .  while( pA && 
10714 70 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  pB ){.    assert
10715 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20  ( pA->pRight==0 
10716 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52  || pA->v<=pA->pR
10717 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61  ight->v );.    a
10718 73 73 65 72 74 28 20 70 42 2d 3e 70 52 69 67 68  ssert( pB->pRigh
10719 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70  t==0 || pB->v<=p
1071a 42 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a  B->pRight->v );.
1071b 20 20 20 20 69 66 28 20 70 41 2d 3e 76 3c 70 42      if( pA->v<pB
1071c 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61  ->v ){.      pTa
1071d 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b  il->pRight = pA;
1071e 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e  .      pA = pA->
1071f 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54  pRight;.      pT
10720 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69  ail = pTail->pRi
10721 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ght;.    }else i
10722 66 28 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20 29  f( pB->v<pA->v )
10723 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70  {.      pTail->p
10724 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 20 20  Right = pB;.    
10725 20 20 70 42 20 3d 20 70 42 2d 3e 70 52 69 67 68    pB = pB->pRigh
10726 74 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d  t;.      pTail =
10727 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a   pTail->pRight;.
10728 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
10729 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74   pA = pA->pRight
1072a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
1072b 28 20 70 41 20 29 7b 0a 20 20 20 20 61 73 73 65  ( pA ){.    asse
1072c 72 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d  rt( pA->pRight==
1072d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e  0 || pA->v<=pA->
1072e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20  pRight->v );.   
1072f 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d   pTail->pRight =
10730 20 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20   pA;.  }else{.  
10731 20 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20    assert( pB==0 
10732 7c 7c 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30  || pB->pRight==0
10733 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70   || pB->v<=pB->p
10734 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20  Right->v );.    
10735 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20  pTail->pRight = 
10736 70 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  pB;.  }.  return
10737 20 68 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d 0a   head.pRight;.}.
10738 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20  ./*.** Sort all 
10739 65 6c 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20  elements on the 
1073a 70 45 6e 74 72 79 20 6c 69 73 74 20 6f 66 20 74  pEntry list of t
1073b 68 65 20 52 6f 77 53 65 74 20 69 6e 74 6f 20 61  he RowSet into a
1073c 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a  scending order..
1073d 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */ .static void 
1073e 72 6f 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65  rowSetSort(RowSe
1073f 74 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65  t *p){.  unsigne
10740 64 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63  d int i;.  struc
10741 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
10742 45 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20  Entry;.  struct 
10743 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75  RowSetEntry *aBu
10744 63 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73  cket[40];..  ass
10745 65 72 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64  ert( p->isSorted
10746 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28  ==0 );.  memset(
10747 61 42 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65  aBucket, 0, size
10748 6f 66 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20  of(aBucket));.  
10749 77 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79  while( p->pEntry
1074a 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d   ){.    pEntry =
1074b 20 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20   p->pEntry;.    
1074c 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74  p->pEntry = pEnt
1074d 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  ry->pRight;.    
1074e 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d  pEntry->pRight =
1074f 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   0;.    for(i=0;
10750 20 61 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b   aBucket[i]; i++
10751 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20  ){.      pEntry 
10752 3d 20 72 6f 77 53 65 74 4d 65 72 67 65 28 61 42  = rowSetMerge(aB
10753 75 63 6b 65 74 5b 69 5d 2c 20 70 45 6e 74 72 79  ucket[i], pEntry
10754 29 3b 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74  );.      aBucket
10755 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  [i] = 0;.    }. 
10756 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20     aBucket[i] = 
10757 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45  pEntry;.  }.  pE
10758 6e 74 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28  ntry = 0;.  for(
10759 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42  i=0; i<sizeof(aB
1075a 75 63 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42  ucket)/sizeof(aB
1075b 75 63 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b  ucket[0]); i++){
1075c 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f  .    pEntry = ro
1075d 77 53 65 74 4d 65 72 67 65 28 70 45 6e 74 72 79  wSetMerge(pEntry
1075e 2c 20 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20  , aBucket[i]);. 
1075f 20 7d 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d   }.  p->pEntry =
10760 20 70 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c   pEntry;.  p->pL
10761 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73  ast = 0;.  p->is
10762 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a  Sorted = 1;.}...
10763 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 2c  /*.** The input,
10764 20 70 49 6e 2c 20 69 73 20 61 20 62 69 6e 61 72   pIn, is a binar
10765 79 20 74 72 65 65 20 28 6f 72 20 73 75 62 74 72  y tree (or subtr
10766 65 65 29 20 6f 66 20 52 6f 77 53 65 74 45 6e 74  ee) of RowSetEnt
10767 72 79 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 43  ry objects..** C
10768 6f 6e 76 65 72 74 20 74 68 69 73 20 74 72 65 65  onvert this tree
10769 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c   into a linked l
1076a 69 73 74 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  ist connected by
1076b 20 74 68 65 20 70 52 69 67 68 74 20 70 6f 69 6e   the pRight poin
1076c 74 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 74 75  ters.** and retu
1076d 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  rn pointers to t
1076e 68 65 20 66 69 72 73 74 20 61 6e 64 20 6c 61 73  he first and las
1076f 74 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  t elements of th
10770 65 20 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73  e new list..*/.s
10771 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65  tatic void rowSe
10772 74 54 72 65 65 54 6f 4c 69 73 74 28 0a 20 20 73  tTreeToList(.  s
10773 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
10774 79 20 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 20  y *pIn,         
10775 2f 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 69  /* Root of the i
10776 6e 70 75 74 20 74 72 65 65 20 2a 2f 0a 20 20 73  nput tree */.  s
10777 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
10778 79 20 2a 2a 70 70 46 69 72 73 74 2c 20 20 20 20  y **ppFirst,    
10779 2f 2a 20 57 72 69 74 65 20 68 65 61 64 20 6f 66  /* Write head of
1077a 20 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74   the output list
1077b 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63   here */.  struc
1077c 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a  t RowSetEntry **
1077d 70 70 4c 61 73 74 20 20 20 20 20 20 2f 2a 20 57  ppLast      /* W
1077e 72 69 74 65 20 74 61 69 6c 20 6f 66 20 74 68 65  rite tail of the
1077f 20 6f 75 74 70 75 74 20 6c 69 73 74 20 68 65 72   output list her
10780 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74  e */.){.  assert
10781 28 20 70 49 6e 21 3d 30 20 29 3b 0a 20 20 69 66  ( pIn!=0 );.  if
10782 28 20 70 49 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a  ( pIn->pLeft ){.
10783 20 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65      struct RowSe
10784 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 20 20 72  tEntry *p;.    r
10785 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28  owSetTreeToList(
10786 70 49 6e 2d 3e 70 4c 65 66 74 2c 20 70 70 46 69  pIn->pLeft, ppFi
10787 72 73 74 2c 20 26 70 29 3b 0a 20 20 20 20 70 2d  rst, &p);.    p-
10788 3e 70 52 69 67 68 74 20 3d 20 70 49 6e 3b 0a 20  >pRight = pIn;. 
10789 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 46   }else{.    *ppF
1078a 69 72 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a  irst = pIn;.  }.
1078b 20 20 69 66 28 20 70 49 6e 2d 3e 70 52 69 67 68    if( pIn->pRigh
1078c 74 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 54  t ){.    rowSetT
1078d 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70  reeToList(pIn->p
1078e 52 69 67 68 74 2c 20 26 70 49 6e 2d 3e 70 52 69  Right, &pIn->pRi
1078f 67 68 74 2c 20 70 70 4c 61 73 74 29 3b 0a 20 20  ght, ppLast);.  
10790 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 4c 61  }else{.    *ppLa
10791 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20  st = pIn;.  }.  
10792 61 73 73 65 72 74 28 20 28 2a 70 70 4c 61 73 74  assert( (*ppLast
10793 29 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a  )->pRight==0 );.
10794 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  }.../*.** Conver
10795 74 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20  t a sorted list 
10796 6f 66 20 65 6c 65 6d 65 6e 74 73 20 28 63 6f 6e  of elements (con
10797 6e 65 63 74 65 64 20 62 79 20 70 52 69 67 68 74  nected by pRight
10798 29 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 0a  ) into a binary.
10799 2a 2a 20 74 72 65 65 20 77 69 74 68 20 64 65 70  ** tree with dep
1079a 74 68 20 6f 66 20 69 44 65 70 74 68 2e 20 20 41  th of iDepth.  A
1079b 20 64 65 70 74 68 20 6f 66 20 31 20 6d 65 61 6e   depth of 1 mean
1079c 73 20 74 68 65 20 74 72 65 65 20 63 6f 6e 74 61  s the tree conta
1079d 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  ins a single.** 
1079e 6e 6f 64 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20  node taken from 
1079f 74 68 65 20 68 65 61 64 20 6f 66 20 2a 70 70 4c  the head of *ppL
107a0 69 73 74 2e 20 20 41 20 64 65 70 74 68 20 6f 66  ist.  A depth of
107a1 20 32 20 6d 65 61 6e 73 20 61 20 74 72 65 65 20   2 means a tree 
107a2 77 69 74 68 0a 2a 2a 20 74 68 72 65 65 20 6e 6f  with.** three no
107a3 64 65 73 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72  des.  And so for
107a4 74 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 61 73  th..**.** Use as
107a5 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 20 66 72   many entries fr
107a6 6f 6d 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73  om the input lis
107a7 74 20 61 73 20 72 65 71 75 69 72 65 64 20 61 6e  t as required an
107a8 64 20 75 70 64 61 74 65 20 74 68 65 0a 2a 2a 20  d update the.** 
107a9 2a 70 70 4c 69 73 74 20 74 6f 20 70 6f 69 6e 74  *ppList to point
107aa 20 74 6f 20 74 68 65 20 75 6e 75 73 65 64 20 65   to the unused e
107ab 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c  lements of the l
107ac 69 73 74 2e 20 20 49 66 20 74 68 65 20 69 6e 70  ist.  If the inp
107ad 75 74 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e 74 61  ut.** list conta
107ae 69 6e 73 20 74 6f 6f 20 66 65 77 20 65 6c 65 6d  ins too few elem
107af 65 6e 74 73 2c 20 74 68 65 6e 20 63 6f 6e 73 74  ents, then const
107b0 72 75 63 74 20 61 6e 20 69 6e 63 6f 6d 70 6c 65  ruct an incomple
107b1 74 65 20 74 72 65 65 0a 2a 2a 20 61 6e 64 20 6c  te tree.** and l
107b2 65 61 76 65 20 2a 70 70 4c 69 73 74 20 73 65 74  eave *ppList set
107b3 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   to NULL..**.** 
107b4 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
107b5 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20   to the root of 
107b6 74 68 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20  the constructed 
107b7 62 69 6e 61 72 79 20 74 72 65 65 2e 0a 2a 2f 0a  binary tree..*/.
107b8 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f  static struct Ro
107b9 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65  wSetEntry *rowSe
107ba 74 4e 44 65 65 70 54 72 65 65 28 0a 20 20 73 74  tNDeepTree(.  st
107bb 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
107bc 20 2a 2a 70 70 4c 69 73 74 2c 0a 20 20 69 6e 74   **ppList,.  int
107bd 20 69 44 65 70 74 68 0a 29 7b 0a 20 20 73 74 72   iDepth.){.  str
107be 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
107bf 2a 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52  *p;         /* R
107c0 6f 6f 74 20 6f 66 20 74 68 65 20 6e 65 77 20 74  oot of the new t
107c1 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ree */.  struct 
107c2 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65  RowSetEntry *pLe
107c3 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20  ft;     /* Left 
107c4 73 75 62 74 72 65 65 20 2a 2f 0a 20 20 69 66 28  subtree */.  if(
107c5 20 2a 70 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20   *ppList==0 ){. 
107c6 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
107c7 0a 20 20 69 66 28 20 69 44 65 70 74 68 3d 3d 31  .  if( iDepth==1
107c8 20 29 7b 0a 20 20 20 20 70 20 3d 20 2a 70 70 4c   ){.    p = *ppL
107c9 69 73 74 3b 0a 20 20 20 20 2a 70 70 4c 69 73 74  ist;.    *ppList
107ca 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20   = p->pRight;.  
107cb 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e    p->pLeft = p->
107cc 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20  pRight = 0;.    
107cd 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20  return p;.  }.  
107ce 70 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 4e 44  pLeft = rowSetND
107cf 65 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20  eepTree(ppList, 
107d0 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 70 20 3d  iDepth-1);.  p =
107d1 20 2a 70 70 4c 69 73 74 3b 0a 20 20 69 66 28 20   *ppList;.  if( 
107d2 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  p==0 ){.    retu
107d3 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20  rn pLeft;.  }.  
107d4 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74  p->pLeft = pLeft
107d5 3b 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d  ;.  *ppList = p-
107d6 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 52  >pRight;.  p->pR
107d7 69 67 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65  ight = rowSetNDe
107d8 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69  epTree(ppList, i
107d9 44 65 70 74 68 2d 31 29 3b 0a 20 20 72 65 74 75  Depth-1);.  retu
107da 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rn p;.}../*.** C
107db 6f 6e 76 65 72 74 20 61 20 73 6f 72 74 65 64 20  onvert a sorted 
107dc 6c 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73  list of elements
107dd 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 20 74   into a binary t
107de 72 65 65 2e 20 4d 61 6b 65 20 74 68 65 20 74 72  ree. Make the tr
107df 65 65 0a 2a 2a 20 61 73 20 64 65 65 70 20 61 73  ee.** as deep as
107e0 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20   it needs to be 
107e1 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74  in order to cont
107e2 61 69 6e 20 74 68 65 20 65 6e 74 69 72 65 20 6c  ain the entire l
107e3 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ist..*/.static s
107e4 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
107e5 79 20 2a 72 6f 77 53 65 74 4c 69 73 74 54 6f 54  y *rowSetListToT
107e6 72 65 65 28 73 74 72 75 63 74 20 52 6f 77 53 65  ree(struct RowSe
107e7 74 45 6e 74 72 79 20 2a 70 4c 69 73 74 29 7b 0a  tEntry *pList){.
107e8 20 20 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20    int iDepth;   
107e9 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68          /* Depth
107ea 20 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 20   of the tree so 
107eb 66 61 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  far */.  struct 
107ec 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20  RowSetEntry *p; 
107ed 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
107ee 20 74 72 65 65 20 72 6f 6f 74 20 2a 2f 0a 20 20   tree root */.  
107ef 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
107f0 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 2f 2a 20  ry *pLeft;   /* 
107f1 4c 65 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a  Left subtree */.
107f2 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74  .  assert( pList
107f3 21 3d 30 20 29 3b 0a 20 20 70 20 3d 20 70 4c 69  !=0 );.  p = pLi
107f4 73 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20 70 2d  st;.  pList = p-
107f5 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 4c  >pRight;.  p->pL
107f6 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20  eft = p->pRight 
107f7 3d 20 30 3b 0a 20 20 66 6f 72 28 69 44 65 70 74  = 0;.  for(iDept
107f8 68 3d 31 3b 20 70 4c 69 73 74 3b 20 69 44 65 70  h=1; pList; iDep
107f9 74 68 2b 2b 29 7b 0a 20 20 20 20 70 4c 65 66 74  th++){.    pLeft
107fa 20 3d 20 70 3b 0a 20 20 20 20 70 20 3d 20 70 4c   = p;.    p = pL
107fb 69 73 74 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d  ist;.    pList =
107fc 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20   p->pRight;.    
107fd 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74  p->pLeft = pLeft
107fe 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 20  ;.    p->pRight 
107ff 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65  = rowSetNDeepTre
10800 65 28 26 70 4c 69 73 74 2c 20 69 44 65 70 74 68  e(&pList, iDepth
10801 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
10802 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  p;.}../*.** Conv
10803 65 72 74 20 74 68 65 20 6c 69 73 74 20 69 6e 20  ert the list in 
10804 70 2d 3e 70 45 6e 74 72 79 20 69 6e 74 6f 20 61  p->pEntry into a
10805 20 73 6f 72 74 65 64 20 6c 69 73 74 20 69 66 20   sorted list if 
10806 69 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72  it is not.** sor
10807 74 65 64 20 61 6c 72 65 61 64 79 2e 20 20 49 66  ted already.  If
10808 20 74 68 65 72 65 20 69 73 20 61 20 62 69 6e 61   there is a bina
10809 72 79 20 74 72 65 65 20 6f 6e 20 70 2d 3e 70 54  ry tree on p->pT
1080a 72 65 65 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e  ree, then.** con
1080b 76 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6c  vert it into a l
1080c 69 73 74 20 74 6f 6f 20 61 6e 64 20 6d 65 72 67  ist too and merg
1080d 65 20 69 74 20 69 6e 74 6f 20 74 68 65 20 70 2d  e it into the p-
1080e 3e 70 45 6e 74 72 79 20 6c 69 73 74 2e 0a 2a 2f  >pEntry list..*/
1080f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77  .static void row
10810 53 65 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74  SetToList(RowSet
10811 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e   *p){.  if( !p->
10812 69 73 53 6f 72 74 65 64 20 29 7b 0a 20 20 20 20  isSorted ){.    
10813 72 6f 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20  rowSetSort(p);. 
10814 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 54 72 65   }.  if( p->pTre
10815 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  e ){.    struct 
10816 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 48 65  RowSetEntry *pHe
10817 61 64 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 20 20  ad, *pTail;.    
10818 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74  rowSetTreeToList
10819 28 70 2d 3e 70 54 72 65 65 2c 20 26 70 48 65 61  (p->pTree, &pHea
1081a 64 2c 20 26 70 54 61 69 6c 29 3b 0a 20 20 20 20  d, &pTail);.    
1081b 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20  p->pTree = 0;.  
1081c 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f    p->pEntry = ro
1081d 77 53 65 74 4d 65 72 67 65 28 70 2d 3e 70 45 6e  wSetMerge(p->pEn
1081e 74 72 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 7d  try, pHead);.  }
1081f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63  .}../*.** Extrac
10820 74 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65  t the smallest e
10821 6c 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20  lement from the 
10822 52 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65  RowSet..** Write
10823 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74   the element int
10824 6f 20 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75  o *pRowid.  Retu
10825 72 6e 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e  rn 1 on success.
10826 20 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66    Return.** 0 if
10827 20 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 61   the RowSet is a
10828 6c 72 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a  lready empty..**
10829 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72  .** After this r
1082a 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20  outine has been 
1082b 63 61 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69  called, the sqli
1082c 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28  te3RowSetInsert(
1082d 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79  ).** routine may
1082e 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61   not be called a
1082f 67 61 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54  gain.  .*/.SQLIT
10830 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
10831 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28  lite3RowSetNext(
10832 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a  RowSet *p, i64 *
10833 70 52 6f 77 69 64 29 7b 0a 20 20 72 6f 77 53 65  pRowid){.  rowSe
10834 74 54 6f 4c 69 73 74 28 70 29 3b 0a 20 20 69 66  tToList(p);.  if
10835 28 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20  ( p->pEntry ){. 
10836 20 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d 3e     *pRowid = p->
10837 70 45 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20 70  pEntry->v;.    p
10838 2d 3e 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45  ->pEntry = p->pE
10839 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20  ntry->pRight;.  
1083a 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 3d    if( p->pEntry=
1083b 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
1083c 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70  te3RowSetClear(p
1083d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
1083e 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  urn 1;.  }else{.
1083f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
10840 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  }.}../*.** Check
10841 20 74 6f 20 73 65 65 20 69 66 20 65 6c 65 6d 65   to see if eleme
10842 6e 74 20 69 52 6f 77 69 64 20 77 61 73 20 69 6e  nt iRowid was in
10843 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20  serted into the 
10844 74 68 65 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a  the rowset as.**
10845 20 70 61 72 74 20 6f 66 20 61 6e 79 20 69 6e 73   part of any ins
10846 65 72 74 20 62 61 74 63 68 20 70 72 69 6f 72 20  ert batch prior 
10847 74 6f 20 69 42 61 74 63 68 2e 20 20 52 65 74 75  to iBatch.  Retu
10848 72 6e 20 31 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51  rn 1 or 0..*/.SQ
10849 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1084a 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65   sqlite3RowSetTe
1084b 73 74 28 52 6f 77 53 65 74 20 2a 70 52 6f 77 53  st(RowSet *pRowS
1084c 65 74 2c 20 75 38 20 69 42 61 74 63 68 2c 20 73  et, u8 iBatch, s
1084d 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f  qlite3_int64 iRo
1084e 77 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52  wid){.  struct R
1084f 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20  owSetEntry *p;. 
10850 20 69 66 28 20 69 42 61 74 63 68 21 3d 70 52 6f   if( iBatch!=pRo
10851 77 53 65 74 2d 3e 69 42 61 74 63 68 20 29 7b 0a  wSet->iBatch ){.
10852 20 20 20 20 69 66 28 20 70 52 6f 77 53 65 74 2d      if( pRowSet-
10853 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 20  >pEntry ){.     
10854 20 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 52   rowSetToList(pR
10855 6f 77 53 65 74 29 3b 0a 20 20 20 20 20 20 70 52  owSet);.      pR
10856 6f 77 53 65 74 2d 3e 70 54 72 65 65 20 3d 20 72  owSet->pTree = r
10857 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28  owSetListToTree(
10858 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 29  pRowSet->pEntry)
10859 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d  ;.      pRowSet-
1085a 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 20  >pEntry = 0;.   
1085b 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 4c 61 73     pRowSet->pLas
1085c 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  t = 0;.    }.   
1085d 20 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68   pRowSet->iBatch
1085e 20 3d 20 69 42 61 74 63 68 3b 0a 20 20 7d 0a 20   = iBatch;.  }. 
1085f 20 70 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 54   p = pRowSet->pT
10860 72 65 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20  ree;.  while( p 
10861 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 3c  ){.    if( p->v<
10862 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  iRowid ){.      
10863 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20  p = p->pRight;. 
10864 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
10865 76 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  v>iRowid ){.    
10866 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a    p = p->pLeft;.
10867 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
10868 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
10869 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
1086a 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
1086b 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 6f 77 73 65  *** End of rowse
1086c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
1086d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1086e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1086f 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
10870 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
10871 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ager.c *********
10872 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10873 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10874 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
10875 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
10876 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
10877 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
10878 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
10879 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1087a 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1087b 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1087c 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1087d 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1087e 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1087f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
10880 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
10881 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
10882 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
10883 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
10884 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
10885 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
10886 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
10887 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
10888 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10889 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1088a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1088b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1088c 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d  is is the implem
1088d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
1088e 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 79  page cache subsy
1088f 73 74 65 6d 20 6f 72 20 22 70 61 67 65 72 22 2e  stem or "pager".
10890 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 70 61 67 65  .** .** The page
10891 72 20 69 73 20 75 73 65 64 20 74 6f 20 61 63 63  r is used to acc
10892 65 73 73 20 61 20 64 61 74 61 62 61 73 65 20 64  ess a database d
10893 69 73 6b 20 66 69 6c 65 2e 20 20 49 74 20 69 6d  isk file.  It im
10894 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d  plements.** atom
10895 69 63 20 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f  ic commit and ro
10896 6c 6c 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74  llback through t
10897 68 65 20 75 73 65 20 6f 66 20 61 20 6a 6f 75 72  he use of a jour
10898 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a  nal file that.**
10899 20 69 73 20 73 65 70 61 72 61 74 65 20 66 72 6f   is separate fro
1089a 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
1089b 69 6c 65 2e 20 20 54 68 65 20 70 61 67 65 72 20  ile.  The pager 
1089c 61 6c 73 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20  also implements 
1089d 66 69 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20  file.** locking 
1089e 74 6f 20 70 72 65 76 65 6e 74 20 74 77 6f 20 70  to prevent two p
1089f 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 20 77 72  rocesses from wr
108a0 69 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64  iting the same d
108a1 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20  atabase.** file 
108a2 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20  simultaneously, 
108a3 6f 72 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 66  or one process f
108a4 72 6f 6d 20 72 65 61 64 69 6e 67 20 74 68 65 20  rom reading the 
108a5 64 61 74 61 62 61 73 65 20 77 68 69 6c 65 0a 2a  database while.*
108a6 2a 20 61 6e 6f 74 68 65 72 20 69 73 20 77 72 69  * another is wri
108a7 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  ting..**.** @(#)
108a8 20 24 49 64 3a 20 70 61 67 65 72 2e 63 2c 76 20   $Id: pager.c,v 
108a9 31 2e 35 38 36 2e 32 2e 31 20 32 30 30 39 2f 30  1.586.2.1 2009/0
108aa 35 2f 31 38 20 31 37 3a 31 31 3a 33 31 20 64 72  5/18 17:11:31 dr
108ab 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  h Exp $.*/.#ifnd
108ac 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
108ad 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  ISKIO../*.** Mac
108ae 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c 65 73  ros for troubles
108af 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c  hooting.  Normal
108b0 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2f  ly turned off.*/
108b1 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 74  .#if 0.int sqlit
108b2 65 33 50 61 67 65 72 54 72 61 63 65 3d 31 3b 20  e3PagerTrace=1; 
108b3 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62   /* True to enab
108b4 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 64  le tracing */.#d
108b5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 62  efine sqlite3Deb
108b6 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 66 0a  ugPrintf printf.
108b7 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 41  #define PAGERTRA
108b8 43 45 28 58 29 20 20 20 20 20 69 66 28 20 73 71  CE(X)     if( sq
108b9 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 65 20  lite3PagerTrace 
108ba 29 7b 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  ){ sqlite3DebugP
108bb 72 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c 73 65  rintf X; }.#else
108bc 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52  .#define PAGERTR
108bd 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f  ACE(X).#endif../
108be 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
108bf 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 61 72  ng two macros ar
108c0 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 68  e used within th
108c1 65 20 50 41 47 45 52 54 52 41 43 45 28 29 20 6d  e PAGERTRACE() m
108c2 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74  acros above.** t
108c3 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c 65  o print out file
108c4 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a  -descriptors. .*
108c5 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20 74  *.** PAGERID() t
108c6 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74  akes a pointer t
108c7 6f 20 61 20 50 61 67 65 72 20 73 74 72 75 63 74  o a Pager struct
108c8 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74   as its argument
108c9 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61  . The.** associa
108ca 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70  ted file-descrip
108cb 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  tor is returned.
108cc 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 20   FILEHANDLEID() 
108cd 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65 33  takes an sqlite3
108ce 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20  _file.** struct 
108cf 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e  as its argument.
108d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45  .*/.#define PAGE
108d1 52 49 44 28 70 29 20 28 28 69 6e 74 29 28 70 2d  RID(p) ((int)(p-
108d2 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20 46 49  >fd)).#define FI
108d3 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29 20 28  LEHANDLEID(fd) (
108d4 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20  (int)fd)../*.** 
108d5 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 61  The page cache a
108d6 73 20 61 20 77 68 6f 6c 65 20 69 73 20 61 6c 77  s a whole is alw
108d7 61 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68  ays in one of th
108d8 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73  e following.** s
108d9 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50  tates:.**.**   P
108da 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20  AGER_UNLOCK     
108db 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68     The page cach
108dc 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  e is not current
108dd 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a  ly reading or .*
108de 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
108df 20 20 20 20 20 20 20 20 77 72 69 74 69 6e 67 20          writing 
108e0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
108e1 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 0a  e.  There is no.
108e2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
108e3 20 20 20 20 20 20 20 20 20 64 61 74 61 20 68 65           data he
108e4 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54  ld in memory.  T
108e5 68 69 73 20 69 73 20 74 68 65 20 69 6e 69 74 69  his is the initi
108e6 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  al.**           
108e7 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74              stat
108e8 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52  e..**.**   PAGER
108e9 5f 53 48 41 52 45 44 20 20 20 20 20 20 20 20 54  _SHARED        T
108ea 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69 73  he page cache is
108eb 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74   reading the dat
108ec 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20  abase..**       
108ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
108ee 57 72 69 74 69 6e 67 20 69 73 20 6e 6f 74 20 70  Writing is not p
108ef 65 72 6d 69 74 74 65 64 2e 20 20 54 68 65 72 65  ermitted.  There
108f0 20 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20   can be.**      
108f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
108f2 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72   multiple reader
108f3 73 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20  s accessing the 
108f4 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  same database.**
108f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
108f6 20 20 20 20 20 20 20 66 69 6c 65 20 61 74 20 74         file at t
108f7 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a  he same time..**
108f8 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 53 45  .**   PAGER_RESE
108f9 52 56 45 44 20 20 20 20 20 20 54 68 69 73 20 70  RVED      This p
108fa 72 6f 63 65 73 73 20 68 61 73 20 72 65 73 65 72  rocess has reser
108fb 76 65 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ved the database
108fc 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20   for writing.** 
108fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
108fe 20 20 20 20 20 20 62 75 74 20 68 61 73 20 6e 6f        but has no
108ff 74 20 79 65 74 20 6d 61 64 65 20 61 6e 79 20 63  t yet made any c
10900 68 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e  hanges.  Only on
10901 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20  e process.**    
10902 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10903 20 20 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e     at a time can
10904 20 72 65 73 65 72 76 65 20 74 68 65 20 64 61 74   reserve the dat
10905 61 62 61 73 65 2e 20 20 54 68 65 20 6f 72 69 67  abase.  The orig
10906 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  inal.**         
10907 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61                da
10908 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20  tabase file has 
10909 6e 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  not been modifie
1090a 64 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20  d so other.**   
1090b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1090c 20 20 20 20 70 72 6f 63 65 73 73 65 73 20 6d 61      processes ma
1090d 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61 64 69  y still be readi
1090e 6e 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a  ng the on-disk.*
1090f 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
10910 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
10911 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50   file..**.**   P
10912 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20  AGER_EXCLUSIVE  
10913 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68     The page cach
10914 65 20 69 73 20 77 72 69 74 69 6e 67 20 74 68 65  e is writing the
10915 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20   database..**   
10916 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10917 20 20 20 20 41 63 63 65 73 73 20 69 73 20 65 78      Access is ex
10918 63 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68  clusive.  No oth
10919 65 72 20 70 72 6f 63 65 73 73 65 73 20 6f 72 0a  er processes or.
1091a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1091b 20 20 20 20 20 20 20 20 20 74 68 72 65 61 64 73           threads
1091c 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20   can be reading 
1091d 6f 72 20 77 72 69 74 69 6e 67 20 77 68 69 6c 65  or writing while
1091e 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   one.**         
1091f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72                pr
10920 6f 63 65 73 73 20 69 73 20 77 72 69 74 69 6e 67  ocess is writing
10921 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f  ..**.**   PAGER_
10922 53 59 4e 43 45 44 20 20 20 20 20 20 20 20 54 68  SYNCED        Th
10923 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f  e pager moves to
10924 20 74 68 69 73 20 73 74 61 74 65 20 66 72 6f 6d   this state from
10925 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
10926 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
10927 20 20 20 20 20 20 20 20 20 20 61 66 74 65 72 20            after 
10928 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20  all dirty pages 
10929 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65  have been writte
1092a 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20  n to the.**     
1092b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1092c 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20    database file 
1092d 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61 73  and the file has
1092e 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a   been synced to.
1092f 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
10930 20 20 20 20 20 20 20 20 20 64 69 73 6b 2e 20 41           disk. A
10931 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20  ll that remains 
10932 74 6f 20 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f  to do is to remo
10933 76 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  ve or.**        
10934 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
10935 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72  runcate the jour
10936 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 74 68 65  nal file and the
10937 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a   transaction .**
10938 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10939 20 20 20 20 20 20 20 77 69 6c 6c 20 62 65 20 63         will be c
1093a 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ommitted..**.** 
1093b 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 63  The page cache c
1093c 6f 6d 65 73 20 75 70 20 69 6e 20 50 41 47 45 52  omes up in PAGER
1093d 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69  _UNLOCK.  The fi
1093e 72 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71  rst time a.** sq
1093f 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20  lite3PagerGet() 
10940 6f 63 63 75 72 73 2c 20 74 68 65 20 73 74 61 74  occurs, the stat
10941 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f  e transitions to
10942 20 50 41 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a   PAGER_SHARED..*
10943 2a 20 41 66 74 65 72 20 61 6c 6c 20 70 61 67 65  * After all page
10944 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c 65  s have been rele
10945 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74  ased using sqlit
10946 65 5f 70 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a  e_page_unref(),.
10947 2a 2a 20 74 68 65 20 73 74 61 74 65 20 74 72 61  ** the state tra
10948 6e 73 69 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f  nsitions back to
10949 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20   PAGER_UNLOCK.  
1094a 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a 2a  The first time.*
1094b 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 61  * that sqlite3Pa
1094c 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61  gerWrite() is ca
1094d 6c 6c 65 64 2c 20 74 68 65 20 73 74 61 74 65 20  lled, the state 
1094e 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a  transitions to.*
1094f 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  * PAGER_RESERVED
10950 2e 20 20 28 4e 6f 74 65 20 74 68 61 74 20 73 71  .  (Note that sq
10951 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
10952 29 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a  ) can only be.**
10953 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75   called on an ou
10954 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 77  tstanding page w
10955 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20  hich means that 
10956 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 0a 2a  the pager must.*
10957 2a 20 62 65 20 69 6e 20 50 41 47 45 52 5f 53 48  * be in PAGER_SH
10958 41 52 45 44 20 62 65 66 6f 72 65 20 69 74 20 74  ARED before it t
10959 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41  ransitions to PA
1095a 47 45 52 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a  GER_RESERVED.).*
1095b 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  * PAGER_RESERVED
1095c 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72   means that ther
1095d 65 20 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c  e is an open rol
1095e 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a  lback journal..*
1095f 2a 20 54 68 65 20 74 72 61 6e 73 69 74 69 6f 6e  * The transition
10960 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 53   to PAGER_EXCLUS
10961 49 56 45 20 6f 63 63 75 72 73 20 62 65 66 6f 72  IVE occurs befor
10962 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a  e any changes.**
10963 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65   are made to the
10964 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
10965 74 68 6f 75 67 68 20 77 72 69 74 65 73 20 74 6f  though writes to
10966 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a   the rollback.**
10967 20 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73 20   journal occurs 
10968 77 69 74 68 20 6a 75 73 74 20 50 41 47 45 52 5f  with just PAGER_
10969 52 45 53 45 52 56 45 44 2e 20 20 41 66 74 65 72  RESERVED.  After
1096a 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72   an sqlite3Pager
1096b 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72  Rollback().** or
1096c 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
1096d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2c 20 74  mitPhaseTwo(), t
1096e 68 65 20 73 74 61 74 65 20 63 61 6e 20 67 6f 20  he state can go 
1096f 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48  back to PAGER_SH
10970 41 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63  ARED,.** or it c
10971 61 6e 20 73 74 61 79 20 61 74 20 50 41 47 45 52  an stay at PAGER
10972 5f 45 58 43 4c 55 53 49 56 45 20 69 66 20 77 65  _EXCLUSIVE if we
10973 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69 76   are in exclusiv
10974 65 20 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a  e access mode..*
10975 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
10976 55 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23 64  UNLOCK      0.#d
10977 65 66 69 6e 65 20 50 41 47 45 52 5f 53 48 41 52  efine PAGER_SHAR
10978 45 44 20 20 20 20 20 20 31 20 20 20 2f 2a 20 73  ED      1   /* s
10979 61 6d 65 20 61 73 20 53 48 41 52 45 44 5f 4c 4f  ame as SHARED_LO
1097a 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  CK */.#define PA
1097b 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20  GER_RESERVED    
1097c 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 52  2   /* same as R
1097d 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a  ESERVED_LOCK */.
1097e 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 45 58  #define PAGER_EX
1097f 43 4c 55 53 49 56 45 20 20 20 34 20 20 20 2f 2a  CLUSIVE   4   /*
10980 20 73 61 6d 65 20 61 73 20 45 58 43 4c 55 53 49   same as EXCLUSI
10981 56 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69  VE_LOCK */.#defi
10982 6e 65 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20  ne PAGER_SYNCED 
10983 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41 20       5../*.** A 
10984 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20 69  macro used for i
10985 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64 65  nvoking the code
10986 63 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e  c if there is on
10987 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  e.*/.#ifdef SQLI
10988 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20 64  TE_HAS_CODEC.# d
10989 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c 44  efine CODEC1(P,D
1098a 2c 4e 2c 58 29 20 69 66 28 20 50 2d 3e 78 43 6f  ,N,X) if( P->xCo
1098b 64 65 63 21 3d 30 20 29 7b 20 50 2d 3e 78 43 6f  dec!=0 ){ P->xCo
1098c 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72 67  dec(P->pCodecArg
1098d 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23 20 64 65 66  ,D,N,X); }.# def
1098e 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e  ine CODEC2(P,D,N
1098f 2c 58 29 20 28 28 63 68 61 72 2a 29 28 50 2d 3e  ,X) ((char*)(P->
10990 78 43 6f 64 65 63 21 3d 30 3f 50 2d 3e 78 43 6f  xCodec!=0?P->xCo
10991 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72 67  dec(P->pCodecArg
10992 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a 23 65 6c 73  ,D,N,X):D)).#els
10993 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43  e.# define CODEC
10994 31 28 50 2c 44 2c 4e 2c 58 29 20 2f 2a 20 4e 4f  1(P,D,N,X) /* NO
10995 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20  -OP */.# define 
10996 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 29 20  CODEC2(P,D,N,X) 
10997 28 28 63 68 61 72 2a 29 44 29 0a 23 65 6e 64 69  ((char*)D).#endi
10998 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  f../*.** The max
10999 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63  imum allowed sec
1099a 74 6f 72 20 73 69 7a 65 2e 20 31 36 4d 42 2e 20  tor size. 16MB. 
1099b 49 66 20 74 68 65 20 78 53 65 63 74 6f 72 73 69  If the xSectorsi
1099c 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20  ze() method .** 
1099d 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20  returns a value 
1099e 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73  larger than this
1099f 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f  , then MAX_SECTO
109a0 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20 69  R_SIZE is used i
109a1 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20  nstead..** This 
109a2 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c  could conceivabl
109a3 79 20 63 61 75 73 65 20 63 6f 72 72 75 70 74 69  y cause corrupti
109a4 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70  on following a p
109a5 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a  ower failure on.
109a6 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74 65 6d  ** such a system
109a7 2e 20 54 68 69 73 20 69 73 20 63 75 72 72 65 6e  . This is curren
109a8 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e  tly an undocumen
109a9 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64  ted limit..*/.#d
109aa 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52  efine MAX_SECTOR
109ab 5f 53 49 5a 45 20 30 78 30 31 30 30 30 30 30 0a  _SIZE 0x0100000.
109ac 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
109ad 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
109ae 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
109af 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65   allocated for e
109b0 61 63 68 20 61 63 74 69 76 65 0a 2a 2a 20 73 61  ach active.** sa
109b1 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 74 61 74  vepoint and stat
109b2 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
109b3 6e 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d 2e  n in the system.
109b4 20 41 6c 6c 20 73 75 63 68 20 73 74 72 75 63 74   All such struct
109b5 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72  ures.** are stor
109b6 65 64 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e  ed in the Pager.
109b7 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72  aSavepoint[] arr
109b8 61 79 2c 20 77 68 69 63 68 20 69 73 20 61 6c 6c  ay, which is all
109b9 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65  ocated and.** re
109ba 73 69 7a 65 64 20 75 73 69 6e 67 20 73 71 6c 69  sized using sqli
109bb 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  te3Realloc()..**
109bc 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 76 65 70  .** When a savep
109bd 6f 69 6e 74 20 69 73 20 63 72 65 61 74 65 64 2c  oint is created,
109be 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f   the PagerSavepo
109bf 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66  int.iHdrOffset f
109c0 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74  ield is.** set t
109c1 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e 61  o 0. If a journa
109c2 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 74  l-header is writ
109c3 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69  ten into the mai
109c4 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65 0a  n journal while.
109c5 2a 2a 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74  ** the savepoint
109c6 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 65 6e   is active, then
109c7 20 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 73   iHdrOffset is s
109c8 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f  et to the byte o
109c9 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 69  ffset .** immedi
109ca 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20  ately following 
109cb 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c  the last journal
109cc 20 72 65 63 6f 72 64 20 77 72 69 74 74 65 6e 20   record written 
109cd 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a  into the main.**
109ce 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20   journal before 
109cf 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64  the journal-head
109d0 65 72 2e 20 54 68 69 73 20 69 73 20 72 65 71 75  er. This is requ
109d1 69 72 65 64 20 64 75 72 69 6e 67 20 73 61 76 65  ired during save
109d2 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63  point.** rollbac
109d3 6b 20 28 73 65 65 20 70 61 67 65 72 50 6c 61 79  k (see pagerPlay
109d4 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 29 29  backSavepoint())
109d5 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
109d6 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f 69  uct PagerSavepoi
109d7 6e 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  nt PagerSavepoin
109d8 74 3b 0a 73 74 72 75 63 74 20 50 61 67 65 72 53  t;.struct PagerS
109d9 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36 34  avepoint {.  i64
109da 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20   iOffset;       
109db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
109dc 72 74 69 6e 67 20 6f 66 66 73 65 74 20 69 6e 20  rting offset in 
109dd 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  main journal */.
109de 20 20 69 36 34 20 69 48 64 72 4f 66 66 73 65 74    i64 iHdrOffset
109df 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
109e0 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a 20  * See above */. 
109e1 20 42 69 74 76 65 63 20 2a 70 49 6e 53 61 76 65   Bitvec *pInSave
109e2 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a  point;        /*
109e3 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 69 6e   Set of pages in
109e4 20 74 68 69 73 20 73 61 76 65 70 6f 69 6e 74 20   this savepoint 
109e5 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b  */.  Pgno nOrig;
109e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
109e7 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75    /* Original nu
109e8 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
109e9 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20   file */.  Pgno 
109ea 69 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20  iSubRec;        
109eb 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
109ec 20 6f 66 20 66 69 72 73 74 20 72 65 63 6f 72 64   of first record
109ed 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20   in sub-journal 
109ee 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f  */.};../*.** A o
109ef 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20 69  pen page cache i
109f0 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
109f1 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
109f2 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
109f3 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20  errCode.**.**   
109f4 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 6d 61  Pager.errCode ma
109f5 79 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49  y be set to SQLI
109f6 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45  TE_IOERR, SQLITE
109f7 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20  _CORRUPT, or.** 
109f8 20 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c    or SQLITE_FULL
109f9 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68  . Once one of th
109fa 65 20 66 69 72 73 74 20 74 68 72 65 65 20 65 72  e first three er
109fb 72 6f 72 73 20 6f 63 63 75 72 73 2c 20 69 74 20  rors occurs, it 
109fc 70 65 72 73 69 73 74 73 0a 2a 2a 20 20 20 61 6e  persists.**   an
109fd 64 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73  d is returned as
109fe 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65   the result of e
109ff 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 67 65 72  very major pager
10a00 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a   API call.  The.
10a01 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c  **   SQLITE_FULL
10a02 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73 20   return code is 
10a03 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65  slightly differe
10a04 6e 74 2e 20 49 74 20 70 65 72 73 69 73 74 73 20  nt. It persists 
10a05 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a  only until the.*
10a06 2a 20 20 20 6e 65 78 74 20 73 75 63 63 65 73 73  *   next success
10a07 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20  ful rollback is 
10a08 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65  performed on the
10a09 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 41 6c   pager cache. Al
10a0a 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  so,.**   SQLITE_
10a0b 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66  FULL does not af
10a0c 66 65 63 74 20 74 68 65 20 73 71 6c 69 74 65 33  fect the sqlite3
10a0d 50 61 67 65 72 47 65 74 28 29 20 61 6e 64 20 73  PagerGet() and s
10a0e 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
10a0f 70 28 29 0a 2a 2a 20 20 20 41 50 49 73 2c 20 74  p().**   APIs, t
10a10 68 65 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 65  hey may still be
10a11 20 75 73 65 64 20 73 75 63 63 65 73 73 66 75 6c   used successful
10a12 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65  ly..**.** dbSize
10a13 56 61 6c 69 64 2c 20 64 62 53 69 7a 65 2c 20 64  Valid, dbSize, d
10a14 62 4f 72 69 67 53 69 7a 65 2c 20 64 62 46 69 6c  bOrigSize, dbFil
10a15 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61  eSize.**.**   Ma
10a16 6e 61 67 69 6e 67 20 74 68 65 20 73 69 7a 65 20  naging the size 
10a17 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
10a18 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 69 73  file in pages is
10a19 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c 69   a little compli
10a1a 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54 68 65 20  cated..**   The 
10a1b 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64  variable Pager.d
10a1c 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74  bSize contains t
10a1d 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
10a1e 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61  es that the data
10a1f 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61 67 65 20  base.**   image 
10a20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69  currently contai
10a21 6e 73 2e 20 41 73 20 74 68 65 20 64 61 74 61 62  ns. As the datab
10a22 61 73 65 20 69 6d 61 67 65 20 67 72 6f 77 73 20  ase image grows 
10a23 6f 72 20 73 68 72 69 6e 6b 73 20 74 68 69 73 0a  or shrinks this.
10a24 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 20 69 73  **   variable is
10a25 20 75 70 64 61 74 65 64 2e 20 54 68 65 20 76 61   updated. The va
10a26 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 46  riable Pager.dbF
10a27 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73  ileSize contains
10a28 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20   the number.**  
10a29 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
10a2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
10a2b 54 68 69 73 20 6d 61 79 20 62 65 20 64 69 66 66  This may be diff
10a2c 65 72 65 6e 74 20 66 72 6f 6d 20 50 61 67 65 72  erent from Pager
10a2d 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20 69 66 20  .dbSize.**   if 
10a2e 73 6f 6d 65 20 70 61 67 65 73 20 68 61 76 65 20  some pages have 
10a2f 62 65 65 6e 20 61 70 70 65 6e 64 65 64 20 74 6f  been appended to
10a30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d   the database im
10a31 61 67 65 20 62 75 74 20 6e 6f 74 20 79 65 74 20  age but not yet 
10a32 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20 6f 75 74  written.**   out
10a33 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20   from the cache 
10a34 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66 69  to the actual fi
10a35 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f 72 20 69  le on disk. Or i
10a36 66 20 74 68 65 20 69 6d 61 67 65 20 68 61 73 20  f the image has 
10a37 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75 6e 63 61  been.**   trunca
10a38 74 65 64 20 62 79 20 61 6e 20 69 6e 63 72 65 6d  ted by an increm
10a39 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 6f 70 65  ental-vacuum ope
10a3a 72 61 74 69 6f 6e 2e 20 54 68 65 20 50 61 67 65  ration. The Page
10a3b 72 2e 64 62 4f 72 69 67 53 69 7a 65 20 76 61 72  r.dbOrigSize var
10a3c 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74 61  iable.**   conta
10a3d 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ins the number o
10a3e 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
10a3f 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 68  atabase image wh
10a40 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  en the current.*
10a41 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  *   transaction 
10a42 77 61 73 20 6f 70 65 6e 65 64 2e 20 54 68 65 20  was opened. The 
10a43 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20  contents of all 
10a44 74 68 72 65 65 20 6f 66 20 74 68 65 73 65 20 76  three of these v
10a45 61 72 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20 20  ariables is.**  
10a46 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 64   only guaranteed
10a47 20 74 6f 20 62 65 20 63 6f 72 72 65 63 74 20 69   to be correct i
10a48 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61  f the boolean Pa
10a49 67 65 72 2e 64 62 53 69 7a 65 56 61 6c 69 64 20  ger.dbSizeValid 
10a4a 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20  is true..**.**  
10a4b 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20 77 68 61   TODO: Under wha
10a4c 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20  t conditions is 
10a4d 64 62 53 69 7a 65 56 61 6c 69 64 20 73 65 74 3f  dbSizeValid set?
10a4e 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20   Cleared?.**.** 
10a4f 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 0a  changeCountDone.
10a50 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 62 6f 6f  **.**   This boo
10a51 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 20 69 73  lean variable is
10a52 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75   used to make su
10a53 72 65 20 74 68 61 74 20 74 68 65 20 63 68 61 6e  re that the chan
10a54 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a 2a 20 20  ge-counter .**  
10a55 20 28 74 68 65 20 34 2d 62 79 74 65 20 68 65 61   (the 4-byte hea
10a56 64 65 72 20 66 69 65 6c 64 20 61 74 20 62 79 74  der field at byt
10a57 65 20 6f 66 66 73 65 74 20 32 34 20 6f 66 20 74  e offset 24 of t
10a58 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
10a59 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f 74 20 75  ) is .**   not u
10a5a 70 64 61 74 65 64 20 6d 6f 72 65 20 6f 66 74 65  pdated more ofte
10a5b 6e 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79  n than necessary
10a5c 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73  . .**.**   It is
10a5d 20 73 65 74 20 74 6f 20 74 72 75 65 20 77 68 65   set to true whe
10a5e 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75  n the change-cou
10a5f 6e 74 65 72 20 66 69 65 6c 64 20 69 73 20 75 70  nter field is up
10a60 64 61 74 65 64 2c 20 77 68 69 63 68 20 0a 2a 2a  dated, which .**
10a61 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70     can only happ
10a62 65 6e 20 69 66 20 61 6e 20 65 78 63 6c 75 73 69  en if an exclusi
10a63 76 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20  ve lock is held 
10a64 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
10a65 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74 20 69 73  file..**   It is
10a66 20 63 6c 65 61 72 65 64 20 28 73 65 74 20 74 6f   cleared (set to
10a67 20 66 61 6c 73 65 29 20 77 68 65 6e 65 76 65 72   false) whenever
10a68 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
10a69 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72 65 6c 69  ck is .**   reli
10a6a 6e 71 75 69 73 68 65 64 20 6f 6e 20 74 68 65 20  nquished on the 
10a6b 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 45  database file. E
10a6c 61 63 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73  ach time a trans
10a6d 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74  action is commit
10a6e 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65 20 63 68  ted,.**   The ch
10a6f 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c  angeCountDone fl
10a70 61 67 20 69 73 20 69 6e 73 70 65 63 74 65 64 2e  ag is inspected.
10a71 20 49 66 20 69 74 20 69 73 20 74 72 75 65 2c 20   If it is true, 
10a72 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20  the work of.**  
10a73 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 63 68   updating the ch
10a74 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20  ange-counter is 
10a75 6f 6d 69 74 74 65 64 20 66 6f 72 20 74 68 65 20  omitted for the 
10a76 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
10a77 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69  ion..**.**   Thi
10a78 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d 65 61 6e  s mechanism mean
10a79 73 20 74 68 61 74 20 77 68 65 6e 20 72 75 6e 6e  s that when runn
10a7a 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65  ing in exclusive
10a7b 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e 65 63 74   mode, a connect
10a7c 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65 64 20 6f  ion .**   need o
10a7d 6e 6c 79 20 75 70 64 61 74 65 20 74 68 65 20 63  nly update the c
10a7e 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 6f 6e  hange-counter on
10a7f 63 65 2c 20 66 6f 72 20 74 68 65 20 66 69 72 73  ce, for the firs
10a80 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  t transaction.**
10a81 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a     committed..**
10a82 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65 64 0a 2a  .** dbModified.*
10a83 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62 4d 6f 64  *.**   The dbMod
10a84 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 73 65  ified flag is se
10a85 74 20 77 68 65 6e 65 76 65 72 20 61 20 64 61 74  t whenever a dat
10a86 61 62 61 73 65 20 70 61 67 65 20 69 73 20 64 69  abase page is di
10a87 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49 74 20 69  rtied..**   It i
10a88 73 20 63 6c 65 61 72 65 64 20 61 74 20 74 68 65  s cleared at the
10a89 20 65 6e 64 20 6f 66 20 65 61 63 68 20 74 72 61   end of each tra
10a8a 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
10a8b 20 20 49 74 20 69 73 20 75 73 65 64 20 77 68 65    It is used whe
10a8c 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 6f 72 20  n committing or 
10a8d 6f 74 68 65 72 77 69 73 65 20 65 6e 64 69 6e 67  otherwise ending
10a8e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20   a transaction. 
10a8f 49 66 0a 2a 2a 20 20 20 74 68 65 20 64 62 4d 6f  If.**   the dbMo
10a90 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 63  dified flag is c
10a91 6c 65 61 72 20 74 68 65 6e 20 6c 65 73 73 20 77  lear then less w
10a92 6f 72 6b 20 68 61 73 20 74 6f 20 62 65 20 64 6f  ork has to be do
10a93 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61  ne..**.** journa
10a94 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20  lStarted.**.**  
10a95 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 73 65   This flag is se
10a96 74 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 74  t whenever the t
10a97 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
10a98 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a  is synced. .**.*
10a99 2a 20 20 20 54 68 65 20 70 6f 69 6e 74 20 6f 66  *   The point of
10a9a 20 74 68 69 73 20 66 6c 61 67 20 69 73 20 74 68   this flag is th
10a9b 61 74 20 69 74 20 6d 75 73 74 20 62 65 20 73 65  at it must be se
10a9c 74 20 61 66 74 65 72 20 74 68 65 20 0a 2a 2a 20  t after the .** 
10a9d 20 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20    first journal 
10a9e 68 65 61 64 65 72 20 69 6e 20 61 20 6a 6f 75 72  header in a jour
10a9f 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 62 65 65  nal file has bee
10aa0 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b  n synced to disk
10aa1 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20 74 68 69  ..**   After thi
10aa2 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20  s has happened, 
10aa3 6e 65 77 20 70 61 67 65 73 20 61 70 70 65 6e 64  new pages append
10aa4 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ed to the databa
10aa5 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74 20  se .**   do not 
10aa6 6e 65 65 64 20 74 68 65 20 50 47 48 44 52 5f 4e  need the PGHDR_N
10aa7 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 73 65  EED_SYNC flag se
10aa8 74 2c 20 61 73 20 74 68 65 79 20 64 6f 20 6e 6f  t, as they do no
10aa9 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74 6f 20 77  t need.**   to w
10aaa 61 69 74 20 66 6f 72 20 61 20 6a 6f 75 72 6e 61  ait for a journa
10aab 6c 20 73 79 6e 63 20 62 65 66 6f 72 65 20 74 68  l sync before th
10aac 65 79 20 63 61 6e 20 62 65 20 77 72 69 74 74 65  ey can be writte
10aad 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20 20 74 68  n out to.**   th
10aae 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
10aaf 28 73 65 65 20 66 75 6e 63 74 69 6f 6e 20 70 61  (see function pa
10ab0 67 65 72 5f 77 72 69 74 65 28 29 29 2e 0a 2a 2a  ger_write())..**
10ab1 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73 74 65 72     .** setMaster
10ab2 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 76 61  .**.**   This va
10ab3 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 74  riable is used t
10ab4 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  o ensure that th
10ab5 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
10ab6 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20   file name.**   
10ab7 28 69 66 20 61 6e 79 29 20 69 73 20 6f 6e 6c 79  (if any) is only
10ab8 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
10ab9 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  e journal file o
10aba 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65  nce..**.**   Whe
10abb 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74  n committing a t
10abc 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20  ransaction, the 
10abd 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
10abe 69 6c 65 20 6e 61 6d 65 20 28 69 66 20 61 6e 79  ile name (if any
10abf 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65 20 77 72  ).**   may be wr
10ac0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a  itten into the j
10ac1 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 69 6c  ournal file whil
10ac2 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 73  e the pager is s
10ac3 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20 50 41 47  till in.**   PAG
10ac4 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 61 74  ER_RESERVED stat
10ac5 65 20 28 73 65 65 20 43 6f 6d 6d 69 74 50 68 61  e (see CommitPha
10ac6 73 65 4f 6e 65 28 29 20 66 6f 72 20 74 68 65 20  seOne() for the 
10ac7 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20  action). It.**  
10ac8 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74   then attempts t
10ac9 6f 20 75 70 67 72 61 64 65 20 74 6f 20 61 6e 20  o upgrade to an 
10aca 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20  exclusive lock. 
10acb 49 66 20 74 68 69 73 20 61 74 74 65 6d 70 74 0a  If this attempt.
10acc 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74 68 65 6e  **   fails, then
10acd 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6d 61 79   SQLITE_BUSY may
10ace 20 62 65 20 72 65 74 75 72 6e 65 64 20 74 6f 20   be returned to 
10acf 74 68 65 20 75 73 65 72 20 61 6e 64 20 74 68 65  the user and the
10ad0 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61 79 20 61   user.**   may a
10ad1 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d 6d 69 74  ttempt to commit
10ad2 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
10ad3 20 61 67 61 69 6e 20 6c 61 74 65 72 20 28 63 61   again later (ca
10ad4 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69  lling.**   Commi
10ad5 74 50 68 61 73 65 4f 6e 65 28 29 20 61 67 61 69  tPhaseOne() agai
10ad6 6e 29 2e 20 54 68 69 73 20 66 6c 61 67 20 69 73  n). This flag is
10ad7 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20   used to ensure 
10ad8 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 20 20 6d  that the .**   m
10ad9 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
10ada 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72 69 74 74  me is only writt
10adb 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  en to the journa
10adc 6c 20 66 69 6c 65 20 74 68 65 20 66 69 72 73 74  l file the first
10add 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f 6d 6d 69  .**   time Commi
10ade 74 50 68 61 73 65 4f 6e 65 28 29 20 69 73 20 63  tPhaseOne() is c
10adf 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e  alled..**.** doN
10ae0 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54  otSync.**.**   T
10ae1 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20  his variable is 
10ae2 73 65 74 20 61 6e 64 20 63 6c 65 61 72 65 64 20  set and cleared 
10ae3 62 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  by sqlite3PagerW
10ae4 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65  rite()..**.** ne
10ae5 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54  edSync.**.**   T
10ae6 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74 20 62 65  ODO: It might be
10ae7 20 65 61 73 69 65 72 20 74 6f 20 73 65 74 20 74   easier to set t
10ae8 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20  his variable in 
10ae9 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
10aea 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72 69 74 65  ).**   and write
10aeb 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20  MasterJournal() 
10aec 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20 69 74 73  only. Change its
10aed 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22 75 6e 73   meaning to "uns
10aee 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a 20 20 20  ynced data.**   
10aef 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e  has been written
10af0 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 22   to the journal"
10af1 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65  ..**.** subjInMe
10af2 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69  mory.**.**   Thi
10af3 73 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76  s is a boolean v
10af4 61 72 69 61 62 6c 65 2e 20 49 66 20 74 72 75 65  ariable. If true
10af5 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69  , then any requi
10af6 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a  red sub-journal.
10af7 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65 64 20 61  **   is opened a
10af8 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a  s an in-memory j
10af9 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
10afa 66 61 6c 73 65 2c 20 74 68 65 6e 20 69 6e 2d 6d  false, then in-m
10afb 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75 62 2d 6a  emory.**   sub-j
10afc 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f 6e 6c 79  ournals are only
10afd 20 75 73 65 64 20 66 6f 72 20 69 6e 2d 6d 65 6d   used for in-mem
10afe 6f 72 79 20 70 61 67 65 72 20 66 69 6c 65 73 2e  ory pager files.
10aff 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 67 65 72  .*/.struct Pager
10b00 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73   {.  sqlite3_vfs
10b01 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20   *pVfs;         
10b02 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e 73   /* OS functions
10b03 20 74 6f 20 75 73 65 20 66 6f 72 20 49 4f 20 2a   to use for IO *
10b04 2f 0a 20 20 75 38 20 65 78 63 6c 75 73 69 76 65  /.  u8 exclusive
10b05 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Mode;           
10b06 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65  /* Boolean. True
10b07 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65   if locking_mode
10b08 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20  ==EXCLUSIVE */. 
10b09 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b   u8 journalMode;
10b0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b0b 4f 6e 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f  On of the PAGER_
10b0c 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61  JOURNALMODE_* va
10b0d 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 75 73 65  lues */.  u8 use
10b0e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20  Journal;        
10b0f 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72        /* Use a r
10b10 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
10b11 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  on this file */.
10b12 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b    u8 noReadlock;
10b13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b14 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 74   Do not bother t
10b15 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f 63  o obtain readloc
10b16 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e  ks */.  u8 noSyn
10b17 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
10b18 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79      /* Do not sy
10b19 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  nc the journal i
10b1a 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 66  f true */.  u8 f
10b1b 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 20  ullSync;        
10b1c 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 78          /* Do ex
10b1d 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68 65  tra syncs of the
10b1e 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62   journal for rob
10b1f 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20  ustness */.  u8 
10b20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20 20  sync_flags;     
10b21 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
10b22 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f  of SYNC_NORMAL o
10b23 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20  r SYNC_FULL */. 
10b24 20 75 38 20 74 65 6d 70 46 69 6c 65 3b 20 20 20   u8 tempFile;   
10b25 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b26 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 74  zFilename is a t
10b27 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f  emporary file */
10b28 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20  .  u8 readOnly; 
10b29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10b2a 2a 20 54 72 75 65 20 66 6f 72 20 61 20 72 65 61  * True for a rea
10b2b 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20  d-only database 
10b2c 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 3b 20 20  */.  u8 memDb;  
10b2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b2e 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e 68 69   /* True to inhi
10b2f 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f  bit all file I/O
10b30 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f   */..  /* The fo
10b31 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 63 6f  llowing block co
10b32 6e 74 61 69 6e 73 20 74 68 6f 73 65 20 63 6c 61  ntains those cla
10b33 73 73 20 6d 65 6d 62 65 72 73 20 74 68 61 74 20  ss members that 
10b34 61 72 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a  are dynamically.
10b35 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 64 75    ** modified du
10b36 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f 70 65 72  ring normal oper
10b37 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f 74 68 65  ations. The othe
10b38 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74  r variables in t
10b39 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 20 20  his structure.  
10b3a 2a 2a 20 61 72 65 20 65 69 74 68 65 72 20 63 6f  ** are either co
10b3b 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75  nstant throughou
10b3c 74 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f  t the lifetime o
10b3d 66 20 74 68 65 20 70 61 67 65 72 2c 20 6f 72 20  f the pager, or 
10b3e 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65 64 20 74  else.  ** used t
10b3f 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69 67 75 72  o store configur
10b40 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73  ation parameters
10b41 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68 65   that affect the
10b42 20 77 61 79 20 74 68 65 20 70 61 67 65 72 20 0a   way the pager .
10b43 20 20 2a 2a 20 6f 70 65 72 61 74 65 73 2e 0a 20    ** operates.. 
10b44 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 27 73 74   **.  ** The 'st
10b45 61 74 65 27 20 76 61 72 69 61 62 6c 65 20 69 73  ate' variable is
10b46 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 6d 6f   described in mo
10b47 72 65 20 64 65 74 61 69 6c 20 61 6c 6f 6e 67 20  re detail along 
10b48 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 64 65  with the.  ** de
10b49 73 63 72 69 70 74 69 6f 6e 73 20 6f 66 20 74 68  scriptions of th
10b4a 65 20 76 61 6c 75 65 73 20 69 74 20 6d 61 79 20  e values it may 
10b4b 74 61 6b 65 20 2d 20 50 41 47 45 52 5f 55 4e 4c  take - PAGER_UNL
10b4c 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79 20 6f 66  OCK etc. Many of
10b4d 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20   the.  ** other 
10b4e 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69  variables in thi
10b4f 73 20 62 6c 6f 63 6b 20 61 72 65 20 64 65 73 63  s block are desc
10b50 72 69 62 65 64 20 69 6e 20 74 68 65 20 63 6f 6d  ribed in the com
10b51 6d 65 6e 74 20 64 69 72 65 63 74 6c 79 20 0a 20  ment directly . 
10b52 20 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20 63   ** above this c
10b53 6c 61 73 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e  lass definition.
10b54 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 65  .  */.  u8 state
10b55 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10b56 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c      /* PAGER_UNL
10b57 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52  OCK, _SHARED, _R
10b58 45 53 45 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f  ESERVED, etc. */
10b59 0a 20 20 75 38 20 64 62 4d 6f 64 69 66 69 65 64  .  u8 dbModified
10b5a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
10b5b 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20  * True if there 
10b5c 61 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20  are any changes 
10b5d 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 75  to the Db */.  u
10b5e 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20  8 needSync;     
10b5f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
10b60 75 65 20 69 66 20 61 6e 20 66 73 79 6e 63 28 29  ue if an fsync()
10b61 20 69 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 68   is needed on th
10b62 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75  e journal */.  u
10b63 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  8 journalStarted
10b64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ;          /* Tr
10b65 75 65 20 69 66 20 68 65 61 64 65 72 20 6f 66 20  ue if header of 
10b66 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65  journal is synce
10b67 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65  d */.  u8 change
10b68 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 20 20 20  CountDone;      
10b69 20 20 20 2f 2a 20 53 65 74 20 61 66 74 65 72 20     /* Set after 
10b6a 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 65  incrementing the
10b6b 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
10b6c 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 73 74 65  */.  u8 setMaste
10b6d 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
10b6e 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6d 2d   /* True if a m-
10b6f 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20  j name has been 
10b70 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 6e 6c 20  written to jrnl 
10b71 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 53 79 6e  */.  u8 doNotSyn
10b72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
10b73 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 57 68 69   /* Boolean. Whi
10b74 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e 6f 74 20  le true, do not 
10b75 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20  spill the cache 
10b76 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a 65 56 61  */.  u8 dbSizeVa
10b77 6c 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  lid;            
10b78 20 2f 2a 20 53 65 74 20 77 68 65 6e 20 64 62 53   /* Set when dbS
10b79 69 7a 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a  ize is correct *
10b7a 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e 4d 65 6d  /.  u8 subjInMem
10b7b 6f 72 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  ory;            
10b7c 2f 2a 20 54 72 75 65 20 74 6f 20 75 73 65 20 69  /* True to use i
10b7d 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75  n-memory sub-jou
10b7e 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20  rnals */.  Pgno 
10b7f 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  dbSize;         
10b80 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
10b81 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
10b82 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50   database */.  P
10b83 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20  gno dbOrigSize; 
10b84 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62             /* db
10b85 53 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20  Size before the 
10b86 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
10b87 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62  ion */.  Pgno db
10b88 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20  FileSize;       
10b89 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
10b8a 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
10b8b 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
10b8c 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20    int errCode;  
10b8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b8e 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c 20   One of several 
10b8f 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20  kinds of errors 
10b90 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 20  */.  int nRec;  
10b91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b92 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e 61   /* Pages journa
10b93 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61 73 74 20  lled since last 
10b94 6a 2d 68 65 61 64 65 72 20 77 72 69 74 74 65 6e  j-header written
10b95 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49   */.  u32 cksumI
10b96 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  nit;            
10b97 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f    /* Quasi-rando
10b98 6d 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f  m value added to
10b99 20 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20   every checksum 
10b9a 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62 52 65 63  */.  u32 nSubRec
10b9b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10b9c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
10b9d 63 6f 72 64 73 20 77 72 69 74 74 65 6e 20 74 6f  cords written to
10b9e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   sub-journal */.
10b9f 20 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75    Bitvec *pInJou
10ba0 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  rnal;         /*
10ba1 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63   One bit for eac
10ba2 68 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61  h page in the da
10ba3 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
10ba4 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66   sqlite3_file *f
10ba5 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d;           /* 
10ba6 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  File descriptor 
10ba7 66 6f 72 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  for database */.
10ba8 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
10ba9 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  jfd;          /*
10baa 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   File descriptor
10bab 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   for main journa
10bac 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  l */.  sqlite3_f
10bad 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 20  ile *sjfd;      
10bae 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72     /* File descr
10baf 69 70 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f  iptor for sub-jo
10bb0 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a  urnal */.  i64 j
10bb1 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20  ournalOff;      
10bb2 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
10bb3 74 20 77 72 69 74 65 20 6f 66 66 73 65 74 20 69  t write offset i
10bb4 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
10bb5 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72  le */.  i64 jour
10bb6 6e 61 6c 48 64 72 3b 20 20 20 20 20 20 20 20 20  nalHdr;         
10bb7 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73      /* Byte offs
10bb8 65 74 20 74 6f 20 70 72 65 76 69 6f 75 73 20 6a  et to previous j
10bb9 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f  ournal header */
10bba 0a 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  .  PagerSavepoin
10bbb 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f  t *aSavepoint; /
10bbc 2a 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 76  * Array of activ
10bbd 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a  e savepoints */.
10bbe 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74    int nSavepoint
10bbf 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
10bc0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
10bc1 6e 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e  nts in aSavepoin
10bc2 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 62  t[] */.  char db
10bc3 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20  FileVers[16];   
10bc4 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20       /* Changes 
10bc5 77 68 65 6e 65 76 65 72 20 64 61 74 61 62 61 73  whenever databas
10bc6 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a  e file changes *
10bc7 2f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53 69  /.  u32 sectorSi
10bc8 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
10bc9 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f  /* Assumed secto
10bca 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f  r size during ro
10bcb 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 69 6e 74  llback */..  int
10bcc 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20 20   nExtra;        
10bcd 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20           /* Add 
10bce 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20  this many bytes 
10bcf 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72  to each in-memor
10bd0 79 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20  y page */.  u32 
10bd1 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20  vfsFlags;       
10bd2 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73          /* Flags
10bd3 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66 73   for sqlite3_vfs
10bd4 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e  .xOpen() */.  in
10bd5 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20  t pageSize;     
10bd6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
10bd7 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
10bd8 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f  a page */.  Pgno
10bd9 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 20   mxPgno;        
10bda 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
10bdb 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20  um allowed size 
10bdc 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
10bdd 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  */.  char *zFile
10bde 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  name;           
10bdf 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
10be0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
10be1 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61  .  char *zJourna
10be2 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
10be3 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  * Name of the jo
10be4 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20  urnal file */.  
10be5 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c  int (*xBusyHandl
10be6 65 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46  er)(void*); /* F
10be7 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20  unction to call 
10be8 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76  when busy */.  v
10be9 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65  oid *pBusyHandle
10bea 72 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f  rArg;      /* Co
10beb 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66  ntext argument f
10bec 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20  or xBusyHandler 
10bed 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
10bee 5f 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74  _TEST.  int nHit
10bef 2c 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20  , nMiss;        
10bf0 20 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74      /* Cache hit
10bf1 73 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f  s and missing */
10bf2 0a 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57  .  int nRead, nW
10bf3 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f  rite;          /
10bf4 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 73  * Database pages
10bf5 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f   read/written */
10bf6 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28  .#endif.  void (
10bf7 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62 50 61  *xReiniter)(DbPa
10bf8 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68  ge*); /* Call th
10bf9 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  is routine when 
10bfa 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20  reloading pages 
10bfb 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
10bfc 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69  _HAS_CODEC.  voi
10bfd 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69  d *(*xCodec)(voi
10bfe 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e  d*,void*,Pgno,in
10bff 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66  t); /* Routine f
10c00 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64  or en/decoding d
10c01 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ata */.  void *p
10c02 43 6f 64 65 63 41 72 67 3b 20 20 20 20 20 20 20  CodecArg;       
10c03 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72       /* First ar
10c04 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63  gument to xCodec
10c05 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63  () */.#endif.  c
10c06 68 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20  har *pTmpSpace; 
10c07 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
10c08 67 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74  ger.pageSize byt
10c09 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20  es of space for 
10c0a 74 6d 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 34  tmp use */.  i64
10c0b 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69   journalSizeLimi
10c0c 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  t;       /* Size
10c0d 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69   limit for persi
10c0e 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69  stent journal fi
10c0f 6c 65 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20  les */.  PCache 
10c10 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20 20 20  *pPCache;       
10c11 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
10c12 74 6f 20 70 61 67 65 20 63 61 63 68 65 20 6f 62  to page cache ob
10c13 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ject */.  sqlite
10c14 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75  3_backup *pBacku
10c15 70 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  p;    /* Pointer
10c16 20 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f   to list of ongo
10c17 69 6e 67 20 62 61 63 6b 75 70 20 70 72 6f 63 65  ing backup proce
10c18 73 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  sses */.};../*.*
10c19 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
10c1a 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73  global variables
10c1b 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75   hold counters u
10c1c 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69  sed for.** testi
10c1d 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79  ng purposes only
10c1e 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c  .  These variabl
10c1f 65 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20  es do not exist 
10c20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74  in.** a non-test
10c21 69 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73  ing build.  Thes
10c22 65 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  e variables are 
10c23 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e  not thread-safe.
10c24 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
10c25 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
10c26 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61  I int sqlite3_pa
10c27 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74  ger_readdb_count
10c28 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62   = 0;    /* Numb
10c29 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73  er of full pages
10c2a 20 72 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f   read from DB */
10c2b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
10c2c 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72  sqlite3_pager_wr
10c2d 69 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b  itedb_count = 0;
10c2e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
10c2f 66 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74  full pages writt
10c30 65 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49  en to DB */.SQLI
10c31 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
10c32 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f  e3_pager_writej_
10c33 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a  count = 0;    /*
10c34 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
10c35 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72   written to jour
10c36 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20  nal */.# define 
10c37 50 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76  PAGER_INCR(v)  v
10c38 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  ++.#else.# defin
10c39 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a  e PAGER_INCR(v).
10c3a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  #endif..../*.** 
10c3b 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65  Journal files be
10c3c 67 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c  gin with the fol
10c3d 6c 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72  lowing magic str
10c3e 69 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a  ing.  The data.*
10c3f 2a 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66  * was obtained f
10c40 72 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e  rom /dev/random.
10c41 20 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c    It is used onl
10c42 79 20 61 73 20 61 20 73 61 6e 69 74 79 20 63 68  y as a sanity ch
10c43 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65  eck..**.** Since
10c44 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20   version 2.8.0, 
10c45 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d  the journal form
10c46 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69  at contains addi
10c47 74 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a  tional sanity.**
10c48 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d   checking inform
10c49 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70  ation.  If the p
10c4a 6f 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65  ower fails while
10c4b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
10c4c 62 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e  being.** written
10c4d 2c 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61  , semi-random ga
10c4e 72 62 61 67 65 20 64 61 74 61 20 6d 69 67 68 74  rbage data might
10c4f 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a   appear in the j
10c50 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61  ournal.** file a
10c51 66 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 65  fter power is re
10c52 73 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61  stored.  If an a
10c53 74 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d  ttempt is then m
10c54 61 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74  ade.** to roll t
10c55 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c  he journal back,
10c56 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
10c57 75 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 64  uld be corrupted
10c58 2e 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61  .  The additiona
10c59 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63  l.** sanity chec
10c5a 6b 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20  king data is an 
10c5b 61 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f  attempt to disco
10c5c 76 65 72 20 74 68 65 20 67 61 72 62 61 67 65 20  ver the garbage 
10c5d 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61  in the.** journa
10c5e 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e  l and ignore it.
10c5f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74  .**.** The sanit
10c60 79 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72  y checking infor
10c61 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e  mation for the n
10c62 65 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61  ew journal forma
10c63 74 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66  t consists.** of
10c64 20 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73   a 32-bit checks
10c65 75 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20  um on each page 
10c66 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 63 68  of data.  The ch
10c67 65 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f  ecksum covers bo
10c68 74 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e  th.** the page n
10c69 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70 50  umber and the pP
10c6a 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62  ager->pageSize b
10c6b 79 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72  ytes of data for
10c6c 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68   the page..** Th
10c6d 69 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74  is cksum is init
10c6e 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d  ialized to a 32-
10c6f 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65  bit random value
10c70 20 74 68 61 74 20 61 70 70 65 61 72 73 20 69 6e   that appears in
10c71 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
10c72 66 69 6c 65 20 72 69 67 68 74 20 61 66 74 65 72  file right after
10c73 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 54 68   the header.  Th
10c74 65 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c  e random initial
10c75 69 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e  izer is importan
10c76 74 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61  t,.** because ga
10c77 72 62 61 67 65 20 64 61 74 61 20 74 68 61 74 20  rbage data that 
10c78 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65  appears at the e
10c79 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20  nd of a journal 
10c7a 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74  is likely.** dat
10c7b 61 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20  a that was once 
10c7c 69 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74  in other files t
10c7d 68 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65  hat have now bee
10c7e 6e 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74  n deleted.  If t
10c7f 68 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61  he.** garbage da
10c80 74 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20  ta came from an 
10c81 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c  obsolete journal
10c82 20 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b   file, the check
10c83 73 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65  sums might.** be
10c84 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62   correct.  But b
10c85 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  y initializing t
10c86 68 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72  he checksum to r
10c87 61 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63  andom value whic
10c88 68 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e  h.** is differen
10c89 74 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72  t for every jour
10c8a 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65  nal, we minimize
10c8b 20 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73   that risk..*/.s
10c8c 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
10c8d 67 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e  gned char aJourn
10c8e 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20  alMagic[] = {.  
10c8f 30 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35  0xd9, 0xd5, 0x05
10c90 2c 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78  , 0xf9, 0x20, 0x
10c91 61 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a  a1, 0x63, 0xd7,.
10c92 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69  };../*.** The si
10c93 7a 65 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63  ze of the of eac
10c94 68 20 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e  h page record in
10c95 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
10c96 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20  given by.** the 
10c97 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e  following macro.
10c98 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52  .*/.#define JOUR
10c99 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72  NAL_PG_SZ(pPager
10c9a 29 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67  )  ((pPager->pag
10c9b 65 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a  eSize) + 8)../*.
10c9c 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68  ** The journal h
10c9d 65 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74  eader size for t
10c9e 68 69 73 20 70 61 67 65 72 2e 20 54 68 69 73 20  his pager. This 
10c9f 69 73 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73  is usually the s
10ca0 61 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20  ame .** size as 
10ca1 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65  a single disk se
10ca2 63 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73  ctor. See also s
10ca3 65 74 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a  etSectorSize()..
10ca4 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e  */.#define JOURN
10ca5 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
10ca6 29 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f  ) (pPager->secto
10ca7 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  rSize)../*.** Th
10ca8 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73  e macro MEMDB is
10ca9 20 74 72 75 65 20 69 66 20 77 65 20 61 72 65 20   true if we are 
10caa 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20  dealing with an 
10cab 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
10cac 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69  se..** We do thi
10cad 73 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20  s as a macro so 
10cae 74 68 61 74 20 69 66 20 74 68 65 20 53 51 4c 49  that if the SQLI
10caf 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42  TE_OMIT_MEMORYDB
10cb0 20 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a   macro is set,.*
10cb1 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d  * the value of M
10cb2 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63  EMDB will be a c
10cb3 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20  onstant and the 
10cb4 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70  compiler will op
10cb5 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f  timize.** out co
10cb6 64 65 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65  de that would ne
10cb7 76 65 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a  ver execute..*/.
10cb8 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
10cb9 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65  IT_MEMORYDB.# de
10cba 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c  fine MEMDB 0.#el
10cbb 73 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44  se.# define MEMD
10cbc 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a  B pPager->memDb.
10cbd 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
10cbe 65 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20  e maximum legal 
10cbf 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28  page number is (
10cc0 32 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64  2^31 - 1)..*/.#d
10cc1 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f  efine PAGER_MAX_
10cc2 50 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a  PGNO 2147483647.
10cc3 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20  .#ifndef NDEBUG 
10cc4 0a 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a  ./*.** Usage:.**
10cc5 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61 73  .**   assert( as
10cc6 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65  sert_pager_state
10cc7 28 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73  (pPager) );.*/.s
10cc8 74 61 74 69 63 20 69 6e 74 20 61 73 73 65 72 74  tatic int assert
10cc9 5f 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 67  _pager_state(Pag
10cca 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20  er *pPager){..  
10ccb 2f 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69  /* A temp-file i
10ccc 73 20 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45  s always in PAGE
10ccd 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50  R_EXCLUSIVE or P
10cce 41 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74  AGER_SYNCED stat
10ccf 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  e. */.  assert( 
10cd0 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
10cd1 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73  ==0 || pPager->s
10cd2 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c  tate>=PAGER_EXCL
10cd3 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54  USIVE );..  /* T
10cd4 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  he changeCountDo
10cd5 6e 65 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79  ne flag is alway
10cd6 73 20 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66  s set for temp-f
10cd7 69 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74  iles */.  assert
10cd8 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  ( pPager->tempFi
10cd9 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d  le==0 || pPager-
10cda 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
10cdb 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b   );..  return 1;
10cdc 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
10cdd 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
10cde 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  it is necessary 
10cdf 74 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a 70  to write page *p
10ce0 50 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d  Pg into the sub-
10ce1 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61  journal..** A pa
10ce2 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77  ge needs to be w
10ce3 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
10ce4 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74  sub-journal if t
10ce5 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a  here exists one.
10ce6 2a 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20  ** or more open 
10ce7 73 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77  savepoints for w
10ce8 68 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  hich:.**.**   * 
10ce9 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  The page-number 
10cea 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  is less than or 
10ceb 65 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61  equal to PagerSa
10cec 76 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61  vepoint.nOrig, a
10ced 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69  nd.**   * The bi
10cee 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  t corresponding 
10cef 74 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62  to the page-numb
10cf0 65 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e  er is not set in
10cf1 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76  .**     PagerSav
10cf2 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f  epoint.pInSavepo
10cf3 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  int..*/.static i
10cf4 6e 74 20 73 75 62 6a 52 65 71 75 69 72 65 73 50  nt subjRequiresP
10cf5 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b  age(PgHdr *pPg){
10cf6 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70  .  Pgno pgno = p
10cf7 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65  Pg->pgno;.  Page
10cf8 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
10cf9 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69  >pPager;.  int i
10cfa 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
10cfb 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e  Pager->nSavepoin
10cfc 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67  t; i++){.    Pag
10cfd 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d  erSavepoint *p =
10cfe 20 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70   &pPager->aSavep
10cff 6f 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28  oint[i];.    if(
10d00 20 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20   p->nOrig>=pgno 
10d01 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74  && 0==sqlite3Bit
10d02 76 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61  vecTest(p->pInSa
10d03 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29  vepoint, pgno) )
10d04 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
10d05 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
10d06 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
10d07 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
10d08 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65  the page is alre
10d09 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ady in the journ
10d0a 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  al file..*/.stat
10d0b 69 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75  ic int pageInJou
10d0c 72 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29  rnal(PgHdr *pPg)
10d0d 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
10d0e 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 67  e3BitvecTest(pPg
10d0f 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  ->pPager->pInJou
10d10 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29  rnal, pPg->pgno)
10d11 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
10d12 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  a 32-bit integer
10d13 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20   from the given 
10d14 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e  file descriptor.
10d15 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65    Store the inte
10d16 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72  ger.** that is r
10d17 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52  ead in *pRes.  R
10d18 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
10d19 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f  if everything wo
10d1a 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65  rked, or an.** e
10d1b 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d  rror code is som
10d1c 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ething goes wron
10d1d 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c  g..**.** All val
10d1e 75 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f  ues are stored o
10d1f 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e  n disk as big-en
10d20 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dian..*/.static 
10d21 69 6e 74 20 72 65 61 64 33 32 62 69 74 73 28 73  int read32bits(s
10d22 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c  qlite3_file *fd,
10d23 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32   i64 offset, u32
10d24 20 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67   *pRes){.  unsig
10d25 6e 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a  ned char ac[4];.
10d26 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
10d27 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c  e3OsRead(fd, ac,
10d28 20 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66   sizeof(ac), off
10d29 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  set);.  if( rc==
10d2a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
10d2b 20 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33   *pRes = sqlite3
10d2c 47 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20  Get4byte(ac);.  
10d2d 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
10d2e 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20  ../*.** Write a 
10d2f 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69  32-bit integer i
10d30 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66  nto a string buf
10d31 66 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61  fer in big-endia
10d32 6e 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f  n byte order..*/
10d33 0a 23 64 65 66 69 6e 65 20 70 75 74 33 32 62 69  .#define put32bi
10d34 74 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33  ts(A,B)  sqlite3
10d35 50 75 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c  Put4byte((u8*)A,
10d36 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  B)../*.** Write 
10d37 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  a 32-bit integer
10d38 20 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20   into the given 
10d39 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e  file descriptor.
10d3a 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
10d3b 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73  OK.** on success
10d3c 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64   or an error cod
10d3d 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67  e is something g
10d3e 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74  oes wrong..*/.st
10d3f 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 33 32  atic int write32
10d40 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c  bits(sqlite3_fil
10d41 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65  e *fd, i64 offse
10d42 74 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63  t, u32 val){.  c
10d43 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74  har ac[4];.  put
10d44 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b  32bits(ac, val);
10d45 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
10d46 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c  3OsWrite(fd, ac,
10d47 20 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a   4, offset);.}..
10d48 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65  /*.** The argume
10d49 6e 74 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f  nt to this macro
10d4a 20 69 73 20 61 20 66 69 6c 65 20 64 65 73 63 72   is a file descr
10d4b 69 70 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69  iptor (type sqli
10d4c 74 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52  te3_file*)..** R
10d4d 65 74 75 72 6e 20 30 20 69 66 20 69 74 20 69 73  eturn 0 if it is
10d4e 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f   not open, or no
10d4f 6e 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20  n-zero (but not 
10d50 31 29 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a  1) if it is..**.
10d51 2a 2a 20 54 68 69 73 20 69 73 20 73 6f 20 74 68  ** This is so th
10d52 61 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63  at expressions c
10d53 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 73  an be written as
10d54 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73  :.**.**   if( is
10d55 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
10d56 29 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69  ) ){ ....**.** i
10d57 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20  nstead of.**.** 
10d58 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66    if( pPager->jf
10d59 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e  d->pMethods ){ .
10d5a 2e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73  ...*/.#define is
10d5b 4f 70 65 6e 28 70 46 64 29 20 28 28 70 46 64 29  Open(pFd) ((pFd)
10d5c 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a  ->pMethods)../*.
10d5d 2a 2a 20 49 66 20 66 69 6c 65 20 70 46 64 20 69  ** If file pFd i
10d5e 73 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c  s open, call sql
10d5f 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f  ite3OsUnlock() o
10d60 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  n it..*/.static 
10d61 69 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c  int osUnlock(sql
10d62 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20  ite3_file *pFd, 
10d63 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66  int eLock){.  if
10d64 28 20 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29  ( !isOpen(pFd) )
10d65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
10d66 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65  ITE_OK;.  }.  re
10d67 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e  turn sqlite3OsUn
10d68 6c 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29  lock(pFd, eLock)
10d69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
10d6a 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69  function determi
10d6b 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  nes whether or n
10d6c 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72  ot the atomic-wr
10d6d 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ite optimization
10d6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20  .** can be used 
10d6f 77 69 74 68 20 74 68 69 73 20 70 61 67 65 72 2e  with this pager.
10d70 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f   The optimizatio
10d71 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66  n can be used if
10d72 3a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65  :.**.**  (a) the
10d73 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
10d74 62 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 61  by OsDeviceChara
10d75 63 74 65 72 69 73 74 69 63 73 28 29 20 69 6e 64  cteristics() ind
10d76 69 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20  icates that.**  
10d77 20 20 20 20 61 20 64 61 74 61 62 61 73 65 20 70      a database p
10d78 61 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 74  age may be writt
10d79 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61  en atomically, a
10d7a 6e 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76  nd.**  (b) the v
10d7b 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
10d7c 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20   OsSectorSize() 
10d7d 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  is less than or 
10d7e 65 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f  equal.**      to
10d7f 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a   the page size..
10d80 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69  **.** The optimi
10d81 7a 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61  zation is also a
10d82 6c 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f  lways enabled fo
10d83 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  r temporary file
10d84 73 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65  s. It is.** an e
10d85 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69  rror to call thi
10d86 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50  s function if pP
10d87 61 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f  ager is opened o
10d88 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a  n an in-memory.*
10d89 2a 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  * database..**.*
10d8a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a  * If the optimiz
10d8b 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20  ation cannot be 
10d8c 75 73 65 64 2c 20 30 20 69 73 20 72 65 74 75 72  used, 0 is retur
10d8d 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62  ned. If it can b
10d8e 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20  e used,.** then 
10d8f 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
10d90 65 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f  ed is the size o
10d91 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
10d92 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f  le when it.** co
10d93 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20  ntains rollback 
10d94 64 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79  data for exactly
10d95 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69   one page..*/.#i
10d96 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
10d97 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a  LE_ATOMIC_WRITE.
10d98 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42  static int jrnlB
10d99 75 66 66 65 72 53 69 7a 65 28 50 61 67 65 72 20  ufferSize(Pager 
10d9a 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65  *pPager){.  asse
10d9b 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20  rt( !MEMDB );.  
10d9c 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d  if( !pPager->tem
10d9d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74  pFile ){.    int
10d9e 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   dc;            
10d9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10da0 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 63 74  * Device charact
10da1 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20  eristics */.    
10da2 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20  int nSector;    
10da3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10da4 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65    /* Sector size
10da5 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61   */.    int szPa
10da6 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
10da7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
10da8 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61  e size */..    a
10da9 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
10daa 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20  ager->fd) );.   
10dab 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44   dc = sqlite3OsD
10dac 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
10dad 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29  tics(pPager->fd)
10dae 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20  ;.    nSector = 
10daf 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
10db0 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d  ze;.    szPage =
10db1 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
10db2 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53  e;..    assert(S
10db3 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
10db4 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29  IC512==(512>>8))
10db5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c  ;.    assert(SQL
10db6 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
10db7 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29  64K==(65536>>8))
10db8 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63  ;.    if( 0==(dc
10db9 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  &(SQLITE_IOCAP_A
10dba 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38  TOMIC|(szPage>>8
10dbb 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a  )) || nSector>sz
10dbc 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72  Page) ){.      r
10dbd 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
10dbe 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55   }..  return JOU
10dbf 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
10dc0 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47  er) + JOURNAL_PG
10dc1 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23  _SZ(pPager);.}.#
10dc2 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
10dc3 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
10dc4 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 68  ES is defined th
10dc5 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61  en we do some sa
10dc6 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a  nity checking.**
10dc7 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 73   on the cache us
10dc8 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 74  ing a hash funct
10dc9 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73  ion.  This is us
10dca 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a  ed for testing.*
10dcb 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20  * and debugging 
10dcc 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  only..*/.#ifdef 
10dcd 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
10dce 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  ES./*.** Return 
10dcf 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66  a 32-bit hash of
10dd0 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 66   the page data f
10dd1 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61  or pPage..*/.sta
10dd2 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 61  tic u32 pager_da
10dd3 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65  tahash(int nByte
10dd4 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  , unsigned char 
10dd5 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68  *pData){.  u32 h
10dd6 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  ash = 0;.  int i
10dd7 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
10dd8 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Byte; i++){.    
10dd9 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33  hash = (hash*103
10dda 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20  9) + pData[i];. 
10ddb 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68   }.  return hash
10ddc 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70  ;.}.static u32 p
10ddd 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 67  ager_pagehash(Pg
10dde 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72  Hdr *pPage){.  r
10ddf 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61  eturn pager_data
10de0 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67  hash(pPage->pPag
10de1 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75  er->pageSize, (u
10de2 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
10de3 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a  Page->pData);.}.
10de4 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
10de5 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 50  r_set_pagehash(P
10de6 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20  gHdr *pPage){.  
10de7 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20  pPage->pageHash 
10de8 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  = pager_pagehash
10de9 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (pPage);.}../*.*
10dea 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45  * The CHECK_PAGE
10deb 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50   macro takes a P
10dec 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75  gHdr* as an argu
10ded 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f  ment. If SQLITE_
10dee 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69  CHECK_PAGES.** i
10def 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e  s defined, and N
10df0 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66  DEBUG is not def
10df1 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28  ined, an assert(
10df2 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63  ) statement chec
10df3 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70  ks.** that the p
10df4 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64 69  age is either di
10df5 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74  rty or still mat
10df6 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61  ches the calcula
10df7 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a  ted page-hash..*
10df8 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f  /.#define CHECK_
10df9 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 67  PAGE(x) checkPag
10dfa 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64  e(x).static void
10dfb 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 72   checkPage(PgHdr
10dfc 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20   *pPg){.  Pager 
10dfd 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
10dfe 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28  Pager;.  assert(
10dff 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20   !pPg->pageHash 
10e00 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  || pPager->errCo
10e01 64 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67  de.      || (pPg
10e02 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
10e03 52 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67  RTY) || pPg->pag
10e04 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67  eHash==pager_pag
10e05 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a  ehash(pPg) );.}.
10e06 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70  .#else.#define p
10e07 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c  ager_datahash(X,
10e08 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61  Y)  0.#define pa
10e09 67 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 20  ger_pagehash(X) 
10e0a 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b   0.#define CHECK
10e0b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20  _PAGE(x).#endif 
10e0c 20 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b   /* SQLITE_CHECK
10e0d 5f 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _PAGES */../*.**
10e0e 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61   When this is ca
10e0f 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lled the journal
10e10 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20   file for pager 
10e11 70 50 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f  pPager must be o
10e12 70 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e  pen..** This fun
10e13 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74  ction attempts t
10e14 6f 20 72 65 61 64 20 61 20 6d 61 73 74 65 72 20  o read a master 
10e15 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
10e16 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65  e from the .** e
10e17 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61  nd of the file a
10e18 6e 64 2c 20 69 66 20 73 75 63 63 65 73 73 66 75  nd, if successfu
10e19 6c 2c 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74  l, copies it int
10e1a 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65  o memory supplie
10e1b 64 20 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c  d .** by the cal
10e1c 6c 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74  ler. See comment
10e1d 73 20 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73  s above writeMas
10e1e 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72  terJournal() for
10e1f 20 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75   the format.** u
10e20 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d  sed to store a m
10e21 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
10e22 6c 65 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65  le name at the e
10e23 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20  nd of a journal 
10e24 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73  file..**.** zMas
10e25 74 65 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74  ter must point t
10e26 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 61 74  o a buffer of at
10e27 20 6c 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62   least nMaster b
10e28 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62  ytes allocated b
10e29 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e  y.** the caller.
10e2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20   This should be 
10e2b 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61  sqlite3_vfs.mxPa
10e2c 74 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73  thname+1 (to ens
10e2d 75 72 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20  ure there is.** 
10e2e 65 6e 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20  enough space to 
10e2f 77 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72  write the master
10e30 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20   journal name). 
10e31 49 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  If the master jo
10e32 75 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e  urnal.** name in
10e33 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
10e34 6c 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73  longer than nMas
10e35 74 65 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75  ter bytes (inclu
10e36 64 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65  ding a.** nul-te
10e37 72 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20  rminator), then 
10e38 74 68 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20  this is handled 
10e39 61 73 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20  as if no master 
10e3a 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20  journal name.** 
10e3b 77 65 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20  were present in 
10e3c 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a  the journal..**.
10e3d 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a  ** If a master j
10e3e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
10e3f 20 69 73 20 70 72 65 73 65 6e 74 20 61 74 20 74   is present at t
10e40 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f  he end of the jo
10e41 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74  urnal.** file, t
10e42 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64  hen it is copied
10e43 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72   into the buffer
10e44 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a   pointed to by z
10e45 4d 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c  Master. A.** nul
10e46 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65  -terminator byte
10e47 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   is appended to 
10e48 74 68 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f  the buffer follo
10e49 77 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a  wing the master.
10e4a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ** journal file 
10e4b 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  name..**.** If i
10e4c 74 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  t is determined 
10e4d 74 68 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a  that no master j
10e4e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
10e4f 20 69 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20   is present .** 
10e50 7a 4d 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65  zMaster[0] is se
10e51 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54  t to 0 and SQLIT
10e52 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a  E_OK returned..*
10e53 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
10e54 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65   occurs while re
10e55 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a  ading from the j
10e56 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20  ournal file, an 
10e57 53 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20  SQLite.** error 
10e58 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
10e59 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
10e5a 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61  readMasterJourna
10e5b 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  l(sqlite3_file *
10e5c 70 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61  pJrnl, char *zMa
10e5d 73 74 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65  ster, u32 nMaste
10e5e 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  r){.  int rc;   
10e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10e60 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
10e61 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20  */.  u32 len;   
10e62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10e63 2f 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74  /* Length in byt
10e64 65 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75  es of master jou
10e65 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69  rnal name */.  i
10e66 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20  64 szJ;         
10e67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
10e68 61 6c 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  al size in bytes
10e69 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   of journal file
10e6a 20 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20   pJrnl */.  u32 
10e6b 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20  cksum;          
10e6c 20 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65         /* MJ che
10e6d 63 6b 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64  cksum value read
10e6e 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f   from journal */
10e6f 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20  .  u32 u;       
10e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10e71 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63   Unsigned loop c
10e72 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69  ounter */.  unsi
10e73 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 63  gned char aMagic
10e74 5b 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66  [8];   /* A buff
10e75 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d  er to hold the m
10e76 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20  agic header */. 
10e77 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c   zMaster[0] = '\
10e78 30 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54  0';..  if( SQLIT
10e79 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
10e7a 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a  te3OsFileSize(pJ
10e7b 72 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c  rnl, &szJ)).   |
10e7c 7c 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53  | szJ<16.   || S
10e7d 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
10e7e 72 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c  read32bits(pJrnl
10e7f 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29  , szJ-16, &len))
10e80 0a 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73  .   || len>=nMas
10e81 74 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54  ter .   || SQLIT
10e82 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64  E_OK!=(rc = read
10e83 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a  32bits(pJrnl, sz
10e84 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20  J-12, &cksum)). 
10e85 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
10e86 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52  (rc = sqlite3OsR
10e87 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69  ead(pJrnl, aMagi
10e88 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20  c, 8, szJ-8)).  
10e89 20 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69   || memcmp(aMagi
10e8a 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  c, aJournalMagic
10e8b 2c 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54  , 8).   || SQLIT
10e8c 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
10e8d 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c  te3OsRead(pJrnl,
10e8e 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73   zMaster, len, s
10e8f 7a 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b  zJ-16-len)).  ){
10e90 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
10e91 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66    }..  /* See if
10e92 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61   the checksum ma
10e93 74 63 68 65 73 20 74 68 65 20 6d 61 73 74 65 72  tches the master
10e94 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f   journal name */
10e95 0a 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65  .  for(u=0; u<le
10e96 6e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73  n; u++){.    cks
10e97 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d  um -= zMaster[u]
10e98 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75  ;.  }.  if( cksu
10e99 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  m ){.    /* If t
10e9a 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73  he checksum does
10e9b 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e  n't add up, then
10e9c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20   one or more of 
10e9d 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73  the disk sectors
10e9e 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69  .    ** containi
10e9f 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  ng the master jo
10ea0 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69  urnal filename i
10ea1 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69  s corrupted. Thi
10ea2 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64  s means.    ** d
10ea3 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62  efinitely roll b
10ea4 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74  ack, so just ret
10ea5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e  urn SQLITE_OK an
10ea6 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29  d report a (nul)
10ea7 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a  .    ** master-j
10ea8 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e  ournal filename.
10ea9 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20  .    */.    len 
10eaa 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74  = 0;.  }.  zMast
10eab 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a  er[len] = '\0';.
10eac 20 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c     .  return SQL
10ead 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
10eae 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73   Return the offs
10eaf 65 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72  et of the sector
10eb0 20 62 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20   boundary at or 
10eb1 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20  immediately .** 
10eb2 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61  following the va
10eb3 6c 75 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a  lue in pPager->j
10eb4 6f 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d  ournalOff, assum
10eb5 69 6e 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a  ing a sector .**
10eb6 20 73 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d   size of pPager-
10eb7 3e 73 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65  >sectorSize byte
10eb8 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72  s..**.** i.e for
10eb9 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f   a sector size o
10eba 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50  f 512:.**.**   P
10ebb 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ager.journalOff 
10ebc 20 20 20 20 20 20 20 20 20 52 65 74 75 72 6e 20           Return 
10ebd 76 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d  value.**   -----
10ebe 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10ebf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10ec0 2d 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20  --.**   0       
10ec1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ec2 20 20 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20    0.**   512    
10ec3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ec4 20 20 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20     512.**   100 
10ec5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ec6 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32        512.**   2
10ec7 30 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20  000             
10ec8 20 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a           2048.**
10ec9 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20   .*/.static i64 
10eca 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74  journalHdrOffset
10ecb 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
10ecc 0a 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20  .  i64 offset = 
10ecd 30 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61  0;.  i64 c = pPa
10ece 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
10ecf 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20  .  if( c ){.    
10ed0 6f 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f  offset = ((c-1)/
10ed1 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
10ed2 50 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f  Pager) + 1) * JO
10ed3 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
10ed4 67 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  ger);.  }.  asse
10ed5 72 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e  rt( offset%JOURN
10ed6 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
10ed7 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )==0 );.  assert
10ed8 28 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20  ( offset>=c );. 
10ed9 20 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 74   assert( (offset
10eda 2d 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  -c)<JOURNAL_HDR_
10edb 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  SZ(pPager) );.  
10edc 72 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d  return offset;.}
10edd 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72  ../*.** The jour
10ede 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65  nal file must be
10edf 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20   open when this 
10ee0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
10ee1 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ed..**.** This f
10ee2 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d  unction is a no-
10ee3 6f 70 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61  op if the journa
10ee4 6c 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62  l file has not b
10ee5 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a  een written to.*
10ee6 2a 20 77 69 74 68 69 6e 20 74 68 65 20 63 75 72  * within the cur
10ee7 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
10ee8 20 28 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e   (i.e. if Pager.
10ee9 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a  journalOff==0)..
10eea 2a 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63  **.** If doTrunc
10eeb 61 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20  ate is non-zero 
10eec 6f 72 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75  or the Pager.jou
10eed 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61  rnalSizeLimit va
10eee 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74  riable is.** set
10eef 20 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e   to 0, then trun
10ef0 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  cate the journal
10ef1 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79   file to zero by
10ef2 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68  tes in size. Oth
10ef3 65 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20  erwise,.** zero 
10ef4 74 68 65 20 32 38 2d 62 79 74 65 20 68 65 61 64  the 28-byte head
10ef5 65 72 20 61 74 20 74 68 65 20 73 74 61 72 74 20  er at the start 
10ef6 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
10ef7 69 6c 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63  ile. In either c
10ef8 61 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20  ase, .** if the 
10ef9 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20  pager is not in 
10efa 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79  no-sync mode, sy
10efb 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nc the journal f
10efc 69 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ile immediately 
10efd 0a 2a 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e  .** after writin
10efe 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20  g or truncating 
10eff 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67  it..**.** If Pag
10f00 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  er.journalSizeLi
10f01 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 61 20  mit is set to a 
10f02 70 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65  positive, non-ze
10f03 72 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a  ro value, and.**
10f04 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74   following the t
10f05 72 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72  runcation or zer
10f06 6f 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 61  oing described a
10f07 62 6f 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66  bove the size of
10f08 20 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c   the .** journal
10f09 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69   file in bytes i
10f0a 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
10f0b 69 73 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74  is value, then t
10f0c 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a  runcate the.** j
10f0d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50  ournal file to P
10f0e 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65  ager.journalSize
10f0f 4c 69 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65  Limit bytes. The
10f10 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f   journal file do
10f11 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74  es.** not need t
10f12 6f 20 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c  o be synced foll
10f13 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61  owing this opera
10f14 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  tion..**.** If a
10f15 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
10f16 73 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65  s, abandon proce
10f17 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e  ssing and return
10f18 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f   the IO error co
10f19 64 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  de..** Otherwise
10f1a 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
10f1b 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  OK..*/.static in
10f1c 74 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72  t zeroJournalHdr
10f1d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
10f1e 69 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b  int doTruncate){
10f1f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
10f20 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
10f21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10f22 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
10f23 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ode */.  assert(
10f24 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
10f25 6a 66 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50  jfd) );.  if( pP
10f26 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
10f27 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36   ){.    const i6
10f28 34 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65  4 iLimit = pPage
10f29 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  r->journalSizeLi
10f2a 6d 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c  mit;    /* Local
10f2b 20 63 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f   cache of jsl */
10f2c 0a 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22  ..    IOTRACE(("
10f2d 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20  JZEROHDR %p\n", 
10f2e 70 50 61 67 65 72 29 29 0a 20 20 20 20 69 66 28  pPager)).    if(
10f2f 20 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69   doTruncate || i
10f30 4c 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20  Limit==0 ){.    
10f31 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
10f32 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d  Truncate(pPager-
10f33 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65  >jfd, 0);.    }e
10f34 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69  lse{.      stati
10f35 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72  c const char zer
10f36 6f 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a  oHdr[28] = {0};.
10f37 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
10f38 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
10f39 2d 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20  ->jfd, zeroHdr, 
10f3a 73 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c  sizeof(zeroHdr),
10f3b 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   0);.    }.    i
10f3c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
10f3d 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53   && !pPager->noS
10f3e 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ync ){.      rc 
10f3f 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  = sqlite3OsSync(
10f40 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c  pPager->jfd, SQL
10f41 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c  ITE_SYNC_DATAONL
10f42 59 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66  Y|pPager->sync_f
10f43 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  lags);.    }..  
10f44 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69    /* At this poi
10f45 6e 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  nt the transacti
10f46 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20  on is committed 
10f47 62 75 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f  but the write lo
10f48 63 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74  ck .    ** is st
10f49 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ill held on the 
10f4a 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69  file. If there i
10f4b 73 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63  s a size limit c
10f4c 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20  onfigured for . 
10f4d 20 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73     ** the persis
10f4e 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64  tent journal and
10f4f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
10f50 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73  e currently cons
10f51 75 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a  umes more.    **
10f52 20 73 70 61 63 65 20 74 68 61 6e 20 74 68 61 74   space than that
10f53 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f   limit allows fo
10f54 72 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e  r, truncate it n
10f55 6f 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20  ow. There is no 
10f56 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73  need.    ** to s
10f57 79 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c  ync the file fol
10f58 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72  lowing this oper
10f59 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ation..    */.  
10f5a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
10f5b 5f 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20  _OK && iLimit>0 
10f5c 29 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b  ){.      i64 sz;
10f5d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
10f5e 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50  te3OsFileSize(pP
10f5f 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b  ager->jfd, &sz);
10f60 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
10f61 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69  QLITE_OK && sz>i
10f62 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20  Limit ){.       
10f63 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
10f64 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
10f65 6a 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20  jfd, iLimit);.  
10f66 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
10f67 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
10f68 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  /*.** The journa
10f69 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f  l file must be o
10f6a 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f  pen when this ro
10f6b 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
10f6c 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65   A journal.** he
10f6d 61 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44  ader (JOURNAL_HD
10f6e 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 77  R_SZ bytes) is w
10f6f 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
10f70 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20  journal file at 
10f71 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c  the.** current l
10f72 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ocation..**.** T
10f73 68 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68  he format for th
10f74 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
10f75 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a   is as follows:.
10f76 2a 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61  ** - 8 bytes: Ma
10f77 67 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20  gic identifying 
10f78 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a  journal format..
10f79 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75  ** - 4 bytes: Nu
10f7a 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20  mber of records 
10f7b 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d  in journal, or -
10f7c 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69  1 no-sync mode i
10f7d 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74  s on..** - 4 byt
10f7e 65 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65  es: Random numbe
10f7f 72 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 20  r used for page 
10f80 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74  hash..** - 4 byt
10f81 65 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61  es: Initial data
10f82 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e  base page count.
10f83 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53  .** - 4 bytes: S
10f84 65 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20  ector size used 
10f85 62 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 74  by the process t
10f86 68 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a  hat wrote this j
10f87 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62  ournal..** - 4 b
10f88 79 74 65 73 3a 20 44 61 74 61 62 61 73 65 20 70  ytes: Database p
10f89 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a  age size..** .**
10f8a 20 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f   Followed by (JO
10f8b 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32  URNAL_HDR_SZ - 2
10f8c 38 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73  8) bytes of unus
10f8d 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61  ed space..*/.sta
10f8e 74 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75  tic int writeJou
10f8f 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70  rnalHdr(Pager *p
10f90 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  Pager){.  int rc
10f91 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
10f92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10f93 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
10f94 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20    char *zHeader 
10f95 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70  = pPager->pTmpSp
10f96 61 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61  ace;  /* Tempora
10f97 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f  ry space used to
10f98 20 62 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f   build header */
10f99 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d  .  u32 nHeader =
10f9a 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
10f9b 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f  e;     /* Size o
10f9c 66 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64  f buffer pointed
10f9d 20 74 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a   to by zHeader *
10f9e 2f 0a 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20  /.  u32 nWrite; 
10f9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10fa0 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
10fa1 20 6f 66 20 68 65 61 64 65 72 20 73 65 63 74 6f   of header secto
10fa2 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69  r written */.  i
10fa3 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
10fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10fa5 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
10fa6 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  er */..  assert(
10fa7 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
10fa8 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20  jfd) );      /* 
10fa9 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73  Journal file mus
10faa 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20  t be open. */.. 
10fab 20 69 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55   if( nHeader>JOU
10fac 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
10fad 65 72 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64  er) ){.    nHead
10fae 65 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  er = JOURNAL_HDR
10faf 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  _SZ(pPager);.  }
10fb0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
10fb1 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70  are active savep
10fb2 6f 69 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66  oints and any of
10fb3 20 74 68 65 6d 20 77 65 72 65 20 63 72 65 61 74   them were creat
10fb4 65 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74  ed .  ** since t
10fb5 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a  he most recent j
10fb6 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61  ournal header wa
10fb7 73 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74  s written, updat
10fb8 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65  e the .  ** Page
10fb9 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f  rSavepoint.iHdrO
10fba 66 66 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77  ffset fields now
10fbb 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d  ..  */.  for(ii=
10fbc 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53  0; ii<pPager->nS
10fbd 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b  avepoint; ii++){
10fbe 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
10fbf 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e  >aSavepoint[ii].
10fc0 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b  iHdrOffset==0 ){
10fc1 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61  .      pPager->a
10fc2 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48  Savepoint[ii].iH
10fc3 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65  drOffset = pPage
10fc4 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20  r->journalOff;. 
10fc5 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67     }.  }..  pPag
10fc6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d  er->journalHdr =
10fc7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
10fc8 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72  Off = journalHdr
10fc9 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a  Offset(pPager);.
10fca 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 65 72    memcpy(zHeader
10fcb 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c  , aJournalMagic,
10fcc 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c   sizeof(aJournal
10fcd 4d 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a 20 0a  Magic));..  /* .
10fce 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e    ** Write the n
10fcf 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20  Rec Field - the 
10fd0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72  number of page r
10fd1 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c  ecords that foll
10fd2 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75  ow this.  ** jou
10fd3 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72  rnal header. Nor
10fd4 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77  mally, zero is w
10fd5 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76  ritten to this v
10fd6 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 69 6d  alue at this tim
10fd7 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68  e..  ** After th
10fd8 65 20 72 65 63 6f 72 64 73 20 61 72 65 20 61 64  e records are ad
10fd9 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ded to the journ
10fda 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72  al (and the jour
10fdb 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a  nal synced, .  *
10fdc 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e  * if in full-syn
10fdd 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72  c mode), the zer
10fde 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e  o is overwritten
10fdf 20 77 69 74 68 20 74 68 65 20 74 72 75 65 20 6e   with the true n
10fe0 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65  umber.  ** of re
10fe1 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a  cords (see syncJ
10fe2 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a  ournal())..  **.
10fe3 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c    ** A faster al
10fe4 74 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20  ternative is to 
10fe5 77 72 69 74 65 20 30 78 46 46 46 46 46 46 46 46  write 0xFFFFFFFF
10fe6 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65   to the nRec fie
10fe7 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65  ld. When.  ** re
10fe8 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61  ading the journa
10fe9 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c  l this value tel
10fea 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73  ls SQLite to ass
10feb 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a  ume that the.  *
10fec 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f  * rest of the jo
10fed 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61  urnal file conta
10fee 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20 72  ins valid page r
10fef 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73  ecords. This ass
10ff0 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20  umption.  ** is 
10ff1 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66  dangerous, as if
10ff2 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72   a failure occur
10ff3 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 69  red whilst writi
10ff4 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  ng to the journa
10ff5 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d  l.  ** file it m
10ff6 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20  ay contain some 
10ff7 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 68  garbage data. Th
10ff8 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e  ere are two scen
10ff9 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65  arios.  ** where
10ffa 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62   this risk can b
10ffb 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a  e ignored:.  **.
10ffc 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68    **   * When th
10ffd 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f  e pager is in no
10ffe 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72  -sync mode. Corr
10fff 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f  uption can follo
11000 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77  w a.  **     pow
11001 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68  er failure in th
11002 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a  is case anyway..
11003 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68    **.  **   * Wh
11004 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f  en the SQLITE_IO
11005 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20  CAP_SAFE_APPEND 
11006 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 69  flag is set. Thi
11007 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a  s guarantees.  *
11008 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62 61  *     that garba
11009 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 72  ge data is never
1100a 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65   appended to the
1100b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20   journal file.. 
1100c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73   */.  assert( is
1100d 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
1100e 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79   || pPager->noSy
1100f 6e 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61  nc );.  if( (pPa
11010 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20  ger->noSync) || 
11011 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  (pPager->journal
11012 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
11013 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a  NALMODE_MEMORY).
11014 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73     || (sqlite3Os
11015 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
11016 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64  stics(pPager->fd
11017 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  )&SQLITE_IOCAP_S
11018 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29  AFE_APPEND) .  )
11019 7b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28  {.    put32bits(
1101a 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
1101b 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c  aJournalMagic)],
1101c 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20   0xffffffff);.  
1101d 7d 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 32  }else{.    put32
1101e 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69  bits(&zHeader[si
1101f 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11020 69 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  ic)], 0);.  }.. 
11021 20 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63   /* The random c
11022 68 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61  heck-hash initia
11023 6c 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69  liser */ .  sqli
11024 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73  te3_randomness(s
11025 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b  izeof(pPager->ck
11026 73 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65  sumInit), &pPage
11027 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20  r->cksumInit);. 
11028 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61   put32bits(&zHea
11029 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72  der[sizeof(aJour
1102a 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50  nalMagic)+4], pP
1102b 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29  ager->cksumInit)
1102c 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69  ;.  /* The initi
1102d 61 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65  al database size
1102e 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28   */.  put32bits(
1102f 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
11030 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38  aJournalMagic)+8
11031 5d 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  ], pPager->dbOri
11032 67 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65  gSize);.  /* The
11033 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20   assumed sector 
11034 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72  size for this pr
11035 6f 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32  ocess */.  put32
11036 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69  bits(&zHeader[si
11037 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11038 69 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d  ic)+12], pPager-
11039 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20  >sectorSize);.. 
1103a 20 2f 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a   /* The page siz
1103b 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73  e */.  put32bits
1103c 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66  (&zHeader[sizeof
1103d 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b  (aJournalMagic)+
1103e 31 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  16], pPager->pag
1103f 65 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e  eSize);..  /* In
11040 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74  itializing the t
11041 61 69 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65  ail of the buffe
11042 72 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  r is not necessa
11043 72 79 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0a  ry.  Everything.
11044 20 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20    ** works find 
11045 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  if the following
11046 20 6d 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69   memset() is omi
11047 74 74 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69  tted.  But initi
11048 61 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 65  alizing.  ** the
11049 20 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 73   memory prevents
1104a 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 63   valgrind from c
1104b 6f 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 77  omplaining, so w
1104c 65 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f  e are willing to
1104d 0a 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 70  .  ** take the p
1104e 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e 0a  erformance hit..
1104f 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 7a    */.  memset(&z
11050 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a  Header[sizeof(aJ
11051 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 5d  ournalMagic)+20]
11052 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 6e 48  , 0,.         nH
11053 65 61 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 4a  eader-(sizeof(aJ
11054 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 29  ournalMagic)+20)
11055 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 6f  );..  /* In theo
11056 72 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 6e  ry, it is only n
11057 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 74  ecessary to writ
11058 65 20 74 68 65 20 32 38 20 62 79 74 65 73 20 74  e the 28 bytes t
11059 68 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a 6f  hat the .  ** jo
1105a 75 72 6e 61 6c 20 68 65 61 64 65 72 20 63 6f 6e  urnal header con
1105b 73 75 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f 75  sumes to the jou
1105c 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 20  rnal file here. 
1105d 54 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74  Then increment t
1105e 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 6a  he .  ** Pager.j
1105f 6f 75 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 62  ournalOff variab
11060 6c 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 44  le by JOURNAL_HD
11061 52 5f 53 5a 20 73 6f 20 74 68 61 74 20 74 68 65  R_SZ so that the
11062 20 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 6f   next .  ** reco
11063 72 64 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  rd is written to
11064 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
11065 65 63 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 61  ector (leaving a
11066 20 67 61 70 20 69 6e 20 74 68 65 20 66 69 6c 65   gap in the file
11067 0a 20 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20  .  ** that will 
11068 62 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 69  be implicitly fi
11069 6c 6c 65 64 20 69 6e 20 62 79 20 74 68 65 20 4f  lled in by the O
1106a 53 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 6f  S)..  **.  ** Ho
1106b 77 65 76 65 72 20 69 74 20 68 61 73 20 62 65 65  wever it has bee
1106c 6e 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61  n discovered tha
1106d 74 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d  t on some system
1106e 73 20 74 68 69 73 20 70 61 74 74 65 72 6e 20 63  s this pattern c
1106f 61 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e  an .  ** be sign
11070 69 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72  ificantly slower
11071 20 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73   than contiguous
11072 6c 79 20 77 72 69 74 69 6e 67 20 64 61 74 61 20  ly writing data 
11073 74 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 2a  to the file,.  *
11074 2a 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d  * even if that m
11075 65 61 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20  eans explicitly 
11076 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20  writing data to 
11077 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 20  the block of .  
11078 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ** (JOURNAL_HDR_
11079 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20 74  SZ - 28) bytes t
1107a 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  hat will not be 
1107b 75 73 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73  used. So that is
1107c 20 77 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 6f   what.  ** is do
1107d 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ne. .  **.  ** T
1107e 68 65 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 69  he loop is requi
1107f 72 65 64 20 68 65 72 65 20 69 6e 20 63 61 73 65  red here in case
11080 20 74 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65   the sector-size
11081 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
11082 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61  the .  ** databa
11083 73 65 20 70 61 67 65 20 73 69 7a 65 2e 20 53 69  se page size. Si
11084 6e 63 65 20 74 68 65 20 7a 48 65 61 64 65 72 20  nce the zHeader 
11085 62 75 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 50  buffer is only P
11086 61 67 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20  ager.pageSize.  
11087 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ** bytes in size
11088 2c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  , more than one 
11089 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f  call to sqlite3O
1108a 73 57 72 69 74 65 28 29 20 6d 61 79 20 62 65 20  sWrite() may be 
1108b 72 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 6f  required.  ** to
1108c 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 65 6e   populate the en
1108d 74 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61  tire journal hea
1108e 64 65 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a 2f  der sector..  */
1108f 20 0a 20 20 66 6f 72 28 6e 57 72 69 74 65 3d 30   .  for(nWrite=0
11090 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 26  ; rc==SQLITE_OK&
11091 26 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c 5f  &nWrite<JOURNAL_
11092 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 20  HDR_SZ(pPager); 
11093 6e 57 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 29  nWrite+=nHeader)
11094 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22  {.    IOTRACE(("
11095 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c  JHDR %p %lld %d\
11096 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67  n", pPager, pPag
11097 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20  er->journalHdr, 
11098 6e 48 65 61 64 65 72 29 29 0a 20 20 20 20 72 63  nHeader)).    rc
11099 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
1109a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a  e(pPager->jfd, z
1109b 48 65 61 64 65 72 2c 20 6e 48 65 61 64 65 72 2c  Header, nHeader,
1109c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1109d 4f 66 66 29 3b 0a 20 20 20 20 70 50 61 67 65 72  Off);.    pPager
1109e 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20  ->journalOff += 
1109f 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20 20  nHeader;.  }..  
110a0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
110a1 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  .** The journal 
110a2 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65  file must be ope
110a3 6e 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63  n when this is c
110a4 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c  alled. A journal
110a5 20 68 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a 20   header file.** 
110a6 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20  (JOURNAL_HDR_SZ 
110a7 62 79 74 65 73 29 20 69 73 20 72 65 61 64 20 66  bytes) is read f
110a8 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20  rom the current 
110a9 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  location in the 
110aa 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e  journal.** file.
110ab 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63   The current loc
110ac 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75  ation in the jou
110ad 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 67 69 76  rnal file is giv
110ae 65 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65 72 2d  en by.** pPager-
110af 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 65  >journalOff. See
110b0 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20   comments above 
110b1 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f  function writeJo
110b2 75 72 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a 2a  urnalHdr() for.*
110b3 2a 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20  * a description 
110b4 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  of the journal h
110b5 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a  eader format..**
110b6 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61 64 65  .** If the heade
110b7 72 20 69 73 20 72 65 61 64 20 73 75 63 63 65 73  r is read succes
110b8 73 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20 69  sfully, *pNRec i
110b9 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d  s set to the num
110ba 62 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 72  ber of.** page r
110bb 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e 67  ecords following
110bc 20 74 68 69 73 20 68 65 61 64 65 72 20 61 6e 64   this header and
110bd 20 2a 70 44 62 53 69 7a 65 20 69 73 20 73 65 74   *pDbSize is set
110be 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20   to the size of 
110bf 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
110c0 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73  before the trans
110c1 61 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 6e  action began, in
110c2 20 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 50   pages. Also, pP
110c3 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 0a  ager->cksumInit.
110c4 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ** is set to the
110c5 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d   value read from
110c6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
110c7 64 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  der. SQLITE_OK i
110c8 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 6e  s returned.** in
110c9 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a   this case..**.*
110ca 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * If the journal
110cb 20 68 65 61 64 65 72 20 66 69 6c 65 20 61 70 70   header file app
110cc 65 61 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75  ears to be corru
110cd 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e  pted, SQLITE_DON
110ce 45 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  E is.** returned
110cf 20 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64 20   and *pNRec and 
110d0 2a 50 44 62 53 69 7a 65 20 61 72 65 20 75 6e 64  *PDbSize are und
110d1 65 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55 52  efined.  If JOUR
110d2 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73  NAL_HDR_SZ bytes
110d3 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 65  .** cannot be re
110d4 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ad from the jour
110d5 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 6f  nal file an erro
110d6 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
110d7 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
110d8 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72  t readJournalHdr
110d9 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  (.  Pager *pPage
110da 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
110db 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74   /* Pager object
110dc 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61   */.  i64 journa
110dd 6c 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20  lSize,          
110de 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
110df 65 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66  e open journal f
110e0 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ile in bytes */.
110e1 20 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20 20    u32 *pNRec,   
110e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
110e3 2a 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65 61  * OUT: Value rea
110e4 64 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63 20  d from the nRec 
110e5 66 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20 2a  field */.  u32 *
110e6 70 44 62 53 69 7a 65 20 20 20 20 20 20 20 20 20  pDbSize         
110e7 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
110e8 56 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e 61  Value of origina
110e9 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  l database size 
110ea 66 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e  field */.){.  in
110eb 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
110ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
110ed 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75  turn code */.  u
110ee 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61  nsigned char aMa
110ef 67 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 41  gic[8];     /* A
110f0 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20   buffer to hold 
110f1 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72  the magic header
110f2 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66   */.  i64 iHdrOf
110f3 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
110f4 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20     /* Offset of 
110f5 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 62  journal header b
110f6 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20 20  eing read */..  
110f7 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
110f8 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20  Pager->jfd) );  
110f9 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66      /* Journal f
110fa 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ile must be open
110fb 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61 6e  . */..  /* Advan
110fc 63 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  ce Pager.journal
110fd 4f 66 66 20 74 6f 20 74 68 65 20 73 74 61 72 74  Off to the start
110fe 20 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65 63   of the next sec
110ff 74 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a  tor. If the.  **
11100 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
11101 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74   too small for t
11102 68 65 72 65 20 74 6f 20 62 65 20 61 20 68 65 61  here to be a hea
11103 64 65 72 20 73 74 6f 72 65 64 20 61 74 20 74 68  der stored at th
11104 69 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20 72  is.  ** point, r
11105 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
11106 45 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72  E..  */.  pPager
11107 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a  ->journalOff = j
11108 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28  ournalHdrOffset(
11109 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70  pPager);.  if( p
1110a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
1110b 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  f+JOURNAL_HDR_SZ
1110c 28 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e  (pPager) > journ
1110d 61 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65  alSize ){.    re
1110e 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
1110f 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20  ;.  }.  iHdrOff 
11110 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
11111 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61 64  lOff;..  /* Read
11112 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 20   in the first 8 
11113 62 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75  bytes of the jou
11114 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 20  rnal header. If 
11115 74 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 63  they do not matc
11116 68 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 69  h.  ** the  magi
11117 63 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 61  c string found a
11118 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65  t the start of e
11119 61 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  ach journal head
1111a 65 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20  er, return.  ** 
1111b 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66 20  SQLITE_DONE. If 
1111c 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
1111d 72 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  rs, return an er
1111e 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77  ror code. Otherw
1111f 69 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65 65  ise,.  ** procee
11120 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73  d..  */.  rc = s
11121 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61  qlite3OsRead(pPa
11122 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63  ger->jfd, aMagic
11123 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29  , sizeof(aMagic)
11124 2c 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 69 66  , iHdrOff);.  if
11125 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75  ( rc ){.    retu
11126 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28  rn rc;.  }.  if(
11127 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20   memcmp(aMagic, 
11128 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73  aJournalMagic, s
11129 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d  izeof(aMagic))!=
1112a 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1112b 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
1112c 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20  ..  /* Read the 
1112d 66 69 72 73 74 20 74 68 72 65 65 20 33 32 2d 62  first three 32-b
1112e 69 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  it fields of the
1112f 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 3a   journal header:
11130 20 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20 66   The nRec.  ** f
11131 69 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b 73  ield, the checks
11132 75 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 61  um-initializer a
11133 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nd the database 
11134 73 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 72  size at the star
11135 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 72  t.  ** of the tr
11136 61 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 72  ansaction. Retur
11137 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  n an error code 
11138 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  if anything goes
11139 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69   wrong..  */.  i
1113a 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  f( SQLITE_OK!=(r
1113b 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70  c = read32bits(p
1113c 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72  Pager->jfd, iHdr
1113d 4f 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a 20  Off+8, pNRec)). 
1113e 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
1113f 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73  (rc = read32bits
11140 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48  (pPager->jfd, iH
11141 64 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65  drOff+12, &pPage
11142 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a 20  r->cksumInit)). 
11143 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
11144 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73  (rc = read32bits
11145 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48  (pPager->jfd, iH
11146 64 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69 7a  drOff+16, pDbSiz
11147 65 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  e)).  ){.    ret
11148 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69  urn rc;.  }..  i
11149 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
1114a 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20  alOff==0 ){.    
1114b 75 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20 20  u32 iPageSize;  
1114c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1114d 50 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 20  Page-size field 
1114e 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  of journal heade
1114f 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53 65  r */.    u32 iSe
11150 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20  ctorSize;       
11151 20 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 2d        /* Sector-
11152 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f  size field of jo
11153 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a  urnal header */.
11154 20 20 20 20 75 31 36 20 69 50 61 67 65 53 69 7a      u16 iPageSiz
11155 65 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20  e16;            
11156 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 50 61 67   /* Copy of iPag
11157 65 53 69 7a 65 20 69 6e 20 31 36 2d 62 69 74 20  eSize in 16-bit 
11158 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20  variable */..   
11159 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67   /* Read the pag
1115a 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f  e-size and secto
1115b 72 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 6c 20 68  r-size journal h
1115c 65 61 64 65 72 20 66 69 65 6c 64 73 2e 20 2a 2f  eader fields. */
1115d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
1115e 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
1115f 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
11160 2c 20 69 48 64 72 4f 66 66 2b 32 30 2c 20 26 69  , iHdrOff+20, &i
11161 53 65 63 74 6f 72 53 69 7a 65 29 29 0a 20 20 20  SectorSize)).   
11162 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
11163 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73  (rc = read32bits
11164 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48  (pPager->jfd, iH
11165 64 72 4f 66 66 2b 32 34 2c 20 26 69 50 61 67 65  drOff+24, &iPage
11166 53 69 7a 65 29 29 0a 20 20 20 20 29 7b 0a 20 20  Size)).    ){.  
11167 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
11168 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65     }..    /* Che
11169 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75  ck that the valu
1116a 65 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  es read from the
1116b 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73   page-size and s
1116c 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64  ector-size field
1116d 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 77 69 74  s.    ** are wit
1116e 68 69 6e 20 72 61 6e 67 65 2e 20 54 6f 20 62 65  hin range. To be
1116f 20 27 69 6e 20 72 61 6e 67 65 27 2c 20 62 6f 74   'in range', bot
11170 68 20 76 61 6c 75 65 73 20 6e 65 65 64 20 74 6f  h values need to
11171 20 62 65 20 61 20 70 6f 77 65 72 0a 20 20 20 20   be a power.    
11172 2a 2a 20 6f 66 20 74 77 6f 20 67 72 65 61 74 65  ** of two greate
11173 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
11174 74 6f 20 35 31 32 2c 20 61 6e 64 20 6e 6f 74 20  to 512, and not 
11175 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
11176 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 65  ir .    ** respe
11177 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 69  ctive compile ti
11178 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 74  me maximum limit
11179 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
1117a 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 20  ( iPageSize<512 
1117b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1117c 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3c   || iSectorSize<
1117d 35 31 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67  512.     || iPag
1117e 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58  eSize>SQLITE_MAX
1117f 5f 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53  _PAGE_SIZE || iS
11180 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45  ectorSize>MAX_SE
11181 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c  CTOR_SIZE.     |
11182 7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29  | ((iPageSize-1)
11183 26 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20  &iPageSize)!=0  
11184 20 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a   || ((iSectorSiz
11185 65 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65  e-1)&iSectorSize
11186 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20  )!=0 .    ){.   
11187 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74     /* If the eit
11188 68 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a  her the page-siz
11189 65 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65  e or sector-size
1118a 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d   in the journal-
1118b 68 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20  header is .     
1118c 20 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65   ** invalid, the
1118d 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68  n the process th
1118e 61 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75  at wrote the jou
1118f 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74  rnal-header must
11190 20 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20   have .      ** 
11191 63 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74  crashed before t
11192 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79  he header was sy
11193 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  nced. In this ca
11194 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20  se stop reading 
11195 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f  .      ** the jo
11196 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e  urnal file here.
11197 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
11198 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
11199 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  NE;.    }..    /
1119a 2a 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67  * Update the pag
1119b 65 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20  e-size to match 
1119c 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66  the value read f
1119d 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e  rom the journal.
1119e 20 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74   .    ** Use a t
1119f 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20  estcase() macro 
111a0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
111a1 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  t malloc failure
111a2 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20   within .    ** 
111a3 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
111a4 28 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20  () is tested..  
111a5 20 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69    */.    iPageSi
111a6 7a 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67  ze16 = (u16)iPag
111a7 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20  eSize;.    rc = 
111a8 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
111a9 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20  agesize(pPager, 
111aa 26 69 50 61 67 65 53 69 7a 65 31 36 29 3b 0a 20  &iPageSize16);. 
111ab 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21     testcase( rc!
111ac 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
111ad 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
111ae 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 50 61 67 65  LITE_OK || iPage
111af 53 69 7a 65 31 36 3d 3d 28 75 31 36 29 69 50 61  Size16==(u16)iPa
111b0 67 65 53 69 7a 65 20 29 3b 0a 0a 20 20 20 20 2f  geSize );..    /
111b1 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 73 73  * Update the ass
111b2 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 7a 65  umed sector-size
111b3 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61   to match the va
111b4 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 20 20  lue used by .   
111b5 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20   ** the process 
111b6 74 68 61 74 20 63 72 65 61 74 65 64 20 74 68 69  that created thi
111b7 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68  s journal. If th
111b8 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20  is journal was. 
111b9 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79     ** created by
111ba 20 61 20 70 72 6f 63 65 73 73 20 6f 74 68 65 72   a process other
111bb 20 74 68 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20   than this one, 
111bc 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
111bd 65 0a 20 20 20 20 2a 2a 20 69 73 20 62 65 69 6e  e.    ** is bein
111be 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  g called from wi
111bf 74 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 79 62  thin pager_playb
111c0 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61 6c  ack(). The local
111c1 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 6f 66   value.    ** of
111c2 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a   Pager.sectorSiz
111c3 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 61 74  e is restored at
111c4 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 61 74   the end of that
111c5 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20 20 2a 2f   routine..    */
111c6 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
111c7 74 6f 72 53 69 7a 65 20 3d 20 69 53 65 63 74 6f  torSize = iSecto
111c8 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70 50  rSize;.  }..  pP
111c9 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
111ca 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f   += JOURNAL_HDR_
111cb 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65  SZ(pPager);.  re
111cc 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
111cd 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 75 70  ** Write the sup
111ce 70 6c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f 75  plied master jou
111cf 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74  rnal name into t
111d0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
111d1 66 6f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50 61  for pager.** pPa
111d2 67 65 72 20 61 74 20 74 68 65 20 63 75 72 72 65  ger at the curre
111d3 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 65  nt location. The
111d4 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
111d5 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 74 68 65  name must be the
111d6 20 6c 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20 77   last.** thing w
111d7 72 69 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75 72  ritten to a jour
111d8 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65  nal file. If the
111d9 20 70 61 67 65 72 20 69 73 20 69 6e 20 66 75 6c   pager is in ful
111da 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65  l-sync mode, the
111db 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
111dc 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
111dd 64 76 61 6e 63 65 64 20 74 6f 20 74 68 65 20 6e  dvanced to the n
111de 65 78 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64  ext sector bound
111df 61 72 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e  ary before.** an
111e0 79 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65  ything is writte
111e1 6e 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 69 73  n. The format is
111e2 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34 20 62 79  :.**.**   + 4 by
111e3 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47  tes: PAGER_MJ_PG
111e4 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20 62 79 74  NO..**   + N byt
111e5 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e  es: Master journ
111e6 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 75  al filename in u
111e7 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20 34 20 62  tf-8..**   + 4 b
111e8 79 74 65 73 3a 20 4e 20 28 6c 65 6e 67 74 68 20  ytes: N (length 
111e9 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  of master journa
111ea 6c 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 2c  l name in bytes,
111eb 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74   no nul-terminat
111ec 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79  or)..**   + 4 by
111ed 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72  tes: Master jour
111ee 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75  nal name checksu
111ef 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 62 79 74 65  m..**   + 8 byte
111f0 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  s: aJournalMagic
111f1 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61  []..**.** The ma
111f2 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67  ster journal pag
111f3 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68  e checksum is th
111f4 65 20 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74  e sum of the byt
111f5 65 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72  es in the master
111f6 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  .** journal name
111f7 2c 20 77 68 65 72 65 20 65 61 63 68 20 62 79 74  , where each byt
111f8 65 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64  e is interpreted
111f9 20 61 73 20 61 20 73 69 67 6e 65 64 20 38 2d 62   as a signed 8-b
111fa 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a  it integer..**.*
111fb 2a 20 49 66 20 7a 4d 61 73 74 65 72 20 69 73 20  * If zMaster is 
111fc 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28  a NULL pointer (
111fd 6f 63 63 75 72 73 20 66 6f 72 20 61 20 73 69 6e  occurs for a sin
111fe 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61  gle database tra
111ff 6e 73 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74  nsaction), .** t
11200 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f  his call is a no
11201 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
11202 6e 74 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f  nt writeMasterJo
11203 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61  urnal(Pager *pPa
11204 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ger, const char 
11205 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74  *zMaster){.  int
11206 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
11207 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11208 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
11209 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 3b 20 20    int nMaster;  
1120a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1120b 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
1120c 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a  string zMaster *
1120d 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b  /.  i64 iHdrOff;
1120e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1120f 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f       /* Offset o
11210 66 20 68 65 61 64 65 72 20 69 6e 20 6a 6f 75 72  f header in jour
11211 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36  nal file */.  i6
11212 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 20 20 20 20  4 jrnlSize;     
11213 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11214 2a 20 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61  * Size of journa
11215 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 2a  l file on disk *
11216 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20  /.  u32 cksum = 
11217 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
11218 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d       /* Checksum
11219 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74   of string zMast
1121a 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 21 7a 4d  er */..  if( !zM
1121b 61 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72 2d  aster || pPager-
1121c 3e 73 65 74 4d 61 73 74 65 72 0a 20 20 20 7c 7c  >setMaster.   ||
1121d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1121e 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
1121f 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a  NALMODE_MEMORY .
11220 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f     || pPager->jo
11221 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
11222 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
11223 20 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72   .  ){.    retur
11224 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
11225 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61  .  pPager->setMa
11226 73 74 65 72 20 3d 20 31 3b 0a 20 20 61 73 73 65  ster = 1;.  asse
11227 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  rt( isOpen(pPage
11228 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a  r->jfd) );..  /*
11229 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 6c   Calculate the l
1122a 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 61  ength in bytes a
1122b 6e 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20  nd the checksum 
1122c 6f 66 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20  of zMaster */.  
1122d 66 6f 72 28 6e 4d 61 73 74 65 72 3d 30 3b 20 7a  for(nMaster=0; z
1122e 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b  Master[nMaster];
1122f 20 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a 20 20 20   nMaster++){.   
11230 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65   cksum += zMaste
11231 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20 20 7d 0a  r[nMaster];.  }.
11232 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 75 6c 6c  .  /* If in full
11233 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64 76 61  -sync mode, adva
11234 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  nce to the next 
11235 64 69 73 6b 20 73 65 63 74 6f 72 20 62 65 66 6f  disk sector befo
11236 72 65 20 77 72 69 74 69 6e 67 0a 20 20 2a 2a 20  re writing.  ** 
11237 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
11238 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20 69 73  al name. This is
11239 20 69 6e 20 63 61 73 65 20 74 68 65 20 70 72 65   in case the pre
1123a 76 69 6f 75 73 20 70 61 67 65 20 77 72 69 74 74  vious page writt
1123b 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6a  en to.  ** the j
1123c 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c 72 65 61  ournal has alrea
1123d 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 0a  dy been synced..
1123e 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65    */.  if( pPage
1123f 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20  r->fullSync ){. 
11240 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
11241 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48  alOff = journalH
11242 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29  drOffset(pPager)
11243 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20  ;.  }.  iHdrOff 
11244 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
11245 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57 72 69 74  lOff;..  /* Writ
11246 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
11247 72 6e 61 6c 20 64 61 74 61 20 74 6f 20 74 68 65  rnal data to the
11248 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72   end of the jour
11249 6e 61 6c 20 66 69 6c 65 2e 20 49 66 0a 20 20 2a  nal file. If.  *
1124a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  * an error occur
1124b 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  s, return the er
1124c 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20  ror code to the 
1124d 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a 20 20 69  caller..  */.  i
1124e 66 28 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77  f( (0 != (rc = w
1124f 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65  rite32bits(pPage
11250 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2c  r->jfd, iHdrOff,
11251 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70   PAGER_MJ_PGNO(p
11252 50 61 67 65 72 29 29 29 29 0a 20 20 20 7c 7c 20  Pager)))).   || 
11253 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69  (0 != (rc = sqli
11254 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
11255 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c  r->jfd, zMaster,
11256 20 6e 4d 61 73 74 65 72 2c 20 69 48 64 72 4f 66   nMaster, iHdrOf
11257 66 2b 34 29 29 29 0a 20 20 20 7c 7c 20 28 30 20  f+4))).   || (0 
11258 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 32  != (rc = write32
11259 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
1125a 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73  , iHdrOff+4+nMas
1125b 74 65 72 2c 20 6e 4d 61 73 74 65 72 29 29 29 0a  ter, nMaster))).
1125c 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20     || (0 != (rc 
1125d 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50  = write32bits(pP
1125e 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f  ager->jfd, iHdrO
1125f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 34 2c 20  ff+4+nMaster+4, 
11260 63 6b 73 75 6d 29 29 29 0a 20 20 20 7c 7c 20 28  cksum))).   || (
11261 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74  0 != (rc = sqlit
11262 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
11263 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e 61 6c 4d  ->jfd, aJournalM
11264 61 67 69 63 2c 20 38 2c 20 69 48 64 72 4f 66 66  agic, 8, iHdrOff
11265 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 29 29 29 0a  +4+nMaster+8))).
11266 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
11267 72 63 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72  rc;.  }.  pPager
11268 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20  ->journalOff += 
11269 28 6e 4d 61 73 74 65 72 2b 32 30 29 3b 0a 20 20  (nMaster+20);.  
1126a 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
1126b 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79   = !pPager->noSy
1126c 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  nc;..  /* If the
1126d 20 70 61 67 65 72 20 69 73 20 69 6e 20 70 65 72   pager is in per
1126e 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d  istent-journal m
1126f 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68  ode, then the ph
11270 79 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75  ysical .  ** jou
11271 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 78  rnal-file may ex
11272 74 65 6e 64 20 70 61 73 74 20 74 68 65 20 65 6e  tend past the en
11273 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d  d of the master-
11274 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a  journal name.  *
11275 2a 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f 66  * and 8 bytes of
11276 20 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73 74   magic data just
11277 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
11278 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a 20  file. This is . 
11279 20 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 65   ** dangerous be
1127a 63 61 75 73 65 20 74 68 65 20 63 6f 64 65 20 74  cause the code t
1127b 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f 74  o rollback a hot
1127c 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20  -journal file.  
1127d 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61  ** will not be a
1127e 62 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20  ble to find the 
1127f 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e  master-journal n
11280 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ame to determine
11281 20 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f   .  ** whether o
11282 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61  r not the journa
11283 6c 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a  l is hot. .  **.
11284 20 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69    ** Easiest thi
11285 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73  ng to do in this
11286 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20   scenario is to 
11287 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75  truncate the jou
11288 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20  rnal .  ** file 
11289 74 6f 20 74 68 65 20 72 65 71 75 69 72 65 64 20  to the required 
1128a 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 66  size..  */ .  if
1128b 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
1128c 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
1128d 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  Size(pPager->jfd
1128e 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 29 0a 20 20  , &jrnlSize)).  
1128f 20 26 26 20 6a 72 6e 6c 53 69 7a 65 3e 70 50 61   && jrnlSize>pPa
11290 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a  ger->journalOff.
11291 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71    ){.    rc = sq
11292 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28  lite3OsTruncate(
11293 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61  pPager->jfd, pPa
11294 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29  ger->journalOff)
11295 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
11296 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  c;.}../*.** Find
11297 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 68   a page in the h
11298 61 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20  ash table given 
11299 69 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72 2e  its page number.
1129a 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   Return.** a poi
1129b 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65  nter to the page
1129c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20   or NULL if the 
1129d 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69  requested page i
1129e 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c 72 65 61 64  s not .** alread
1129f 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  y in memory..*/.
112a0 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 61  static PgHdr *pa
112a1 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 67 65 72  ger_lookup(Pager
112a2 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70   *pPager, Pgno p
112a3 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70  gno){.  PgHdr *p
112a4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
112a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
112a6 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20  urn value */..  
112a7 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  /* It is not pos
112a8 73 69 62 6c 65 20 66 6f 72 20 61 20 63 61 6c 6c  sible for a call
112a9 20 74 6f 20 50 63 61 63 68 65 46 65 74 63 68 28   to PcacheFetch(
112aa 29 20 77 69 74 68 20 63 72 65 61 74 65 46 6c 61  ) with createFla
112ab 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a 20 66 61 69  g==0 to.  ** fai
112ac 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 61 74 74 65  l, since no atte
112ad 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  mpt to allocate 
112ae 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 77  dynamic memory w
112af 69 6c 6c 20 62 65 20 6d 61 64 65 2e 0a 20 20 2a  ill be made..  *
112b0 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65  /.  (void)sqlite
112b1 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61  3PcacheFetch(pPa
112b2 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67  ger->pPCache, pg
112b3 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72 65  no, 0, &p);.  re
112b4 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
112b5 20 55 6e 6c 65 73 73 20 74 68 65 20 70 61 67 65   Unless the page
112b6 72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74  r is in error-st
112b7 61 74 65 2c 20 64 69 73 63 61 72 64 20 61 6c 6c  ate, discard all
112b8 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73   in-memory pages
112b9 2e 20 49 66 0a 2a 2a 20 74 68 65 20 70 61 67 65  . If.** the page
112ba 72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74  r is in error-st
112bb 61 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20 63  ate, then this c
112bc 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  all is a no-op..
112bd 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 57 68 79 20  **.** TODO: Why 
112be 63 61 6e 20 77 65 20 6e 6f 74 20 72 65 73 65 74  can we not reset
112bf 20 74 68 65 20 70 61 67 65 72 20 77 68 69 6c 65   the pager while
112c0 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 3f   in error state?
112c1 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
112c2 70 61 67 65 72 5f 72 65 73 65 74 28 50 61 67 65  pager_reset(Page
112c3 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66  r *pPager){.  if
112c4 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 50 61  ( SQLITE_OK==pPa
112c5 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a  ger->errCode ){.
112c6 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75      sqlite3Backu
112c7 70 52 65 73 74 61 72 74 28 70 50 61 67 65 72 2d  pRestart(pPager-
112c8 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20 20 20 73  >pBackup);.    s
112c9 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61  qlite3PcacheClea
112ca 72 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  r(pPager->pPCach
112cb 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  e);.    pPager->
112cc 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b  dbSizeValid = 0;
112cd 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  .  }.}../*.** Fr
112ce 65 65 20 61 6c 6c 20 73 74 72 75 63 74 75 72 65  ee all structure
112cf 73 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e 61  s in the Pager.a
112d0 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61  Savepoint[] arra
112d1 79 20 61 6e 64 20 73 65 74 20 62 6f 74 68 0a 2a  y and set both.*
112d2 2a 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69  * Pager.aSavepoi
112d3 6e 74 20 61 6e 64 20 50 61 67 65 72 2e 6e 53 61  nt and Pager.nSa
112d4 76 65 70 6f 69 6e 74 20 74 6f 20 7a 65 72 6f 2e  vepoint to zero.
112d5 20 43 6c 6f 73 65 20 74 68 65 20 73 75 62 2d 6a   Close the sub-j
112d6 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 20 69 74 20  ournal.** if it 
112d7 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20  is open and the 
112d8 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20  pager is not in 
112d9 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a  exclusive mode..
112da 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
112db 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69  eleaseAllSavepoi
112dc 6e 74 73 28 50 61 67 65 72 20 2a 70 50 61 67 65  nts(Pager *pPage
112dd 72 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20  r){.  int ii;   
112de 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
112df 74 65 72 61 74 6f 72 20 66 6f 72 20 6c 6f 6f 70  terator for loop
112e0 69 6e 67 20 74 68 72 6f 75 67 68 20 50 61 67 65  ing through Page
112e1 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 2a 2f 0a  r.aSavepoint */.
112e2 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
112e3 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e  Pager->nSavepoin
112e4 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 71  t; ii++){.    sq
112e5 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
112e6 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65  oy(pPager->aSave
112e7 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76  point[ii].pInSav
112e8 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 20 20 69  epoint);.  }.  i
112e9 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  f( !pPager->excl
112ea 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 73 71 6c  usiveMode || sql
112eb 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c  ite3IsMemJournal
112ec 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29  (pPager->sjfd) )
112ed 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43  {.    sqlite3OsC
112ee 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 73 6a 66  lose(pPager->sjf
112ef 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  d);.  }.  sqlite
112f0 33 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61  3_free(pPager->a
112f1 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 70 50  Savepoint);.  pP
112f2 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
112f3 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e   = 0;.  pPager->
112f4 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a  nSavepoint = 0;.
112f5 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65    pPager->nSubRe
112f6 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  c = 0;.}../*.** 
112f7 53 65 74 20 74 68 65 20 62 69 74 20 6e 75 6d 62  Set the bit numb
112f8 65 72 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 50  er pgno in the P
112f9 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49  agerSavepoint.pI
112fa 6e 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 62  nSavepoint .** b
112fb 69 74 76 65 63 73 20 6f 66 20 61 6c 6c 20 6f 70  itvecs of all op
112fc 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 52  en savepoints. R
112fd 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
112fe 69 66 20 73 75 63 63 65 73 73 66 75 6c 0a 2a 2a  if successful.**
112ff 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   or SQLITE_NOMEM
11300 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   if a malloc fai
11301 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a  lure occurs..*/.
11302 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54 6f  static int addTo
11303 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73  SavepointBitvecs
11304 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
11305 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e  Pgno pgno){.  in
11306 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20  t ii;           
11307 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
11308 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
11309 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1130a 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
1130b 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 66 6f 72 28   code */..  for(
1130c 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d  ii=0; ii<pPager-
1130d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b  >nSavepoint; ii+
1130e 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 53 61 76  +){.    PagerSav
1130f 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 61  epoint *p = &pPa
11310 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b  ger->aSavepoint[
11311 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 67 6e  ii];.    if( pgn
11312 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a 20  o<=p->nOrig ){. 
11313 20 20 20 20 20 72 63 20 7c 3d 20 73 71 6c 69 74       rc |= sqlit
11314 65 33 42 69 74 76 65 63 53 65 74 28 70 2d 3e 70  e3BitvecSet(p->p
11315 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e  InSavepoint, pgn
11316 6f 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  o);.      testca
11317 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  se( rc==SQLITE_N
11318 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 61 73  OMEM );.      as
11319 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1131a 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  _OK || rc==SQLIT
1131b 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 7d  E_NOMEM );.    }
1131c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1131d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63  ;.}../*.** Unloc
1131e 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  k the database f
1131f 69 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ile. This functi
11320 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  on is a no-op if
11321 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 69 73   the pager.** is
11322 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
11323 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  de..**.** If the
11324 20 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e   pager is curren
11325 74 6c 79 20 69 6e 20 65 72 72 6f 72 20 73 74 61  tly in error sta
11326 74 65 2c 20 64 69 73 63 61 72 64 20 74 68 65 20  te, discard the 
11327 63 6f 6e 74 65 6e 74 73 20 6f 66 20 0a 2a 2a 20  contents of .** 
11328 74 68 65 20 63 61 63 68 65 20 61 6e 64 20 72 65  the cache and re
11329 73 65 74 20 74 68 65 20 50 61 67 65 72 20 73 74  set the Pager st
1132a 72 75 63 74 75 72 65 20 69 6e 74 65 72 6e 61 6c  ructure internal
1132b 20 73 74 61 74 65 2e 20 49 66 20 74 68 65 72 65   state. If there
1132c 20 69 73 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a   is.** an open j
1132d 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 65  ournal-file, the
1132e 6e 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20  n the next time 
1132f 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73  a shared-lock is
11330 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20   obtained.** on 
11331 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 28  the pager file (
11332 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f  by this or any o
11333 74 68 65 72 20 70 72 6f 63 65 73 73 29 2c 20 69  ther process), i
11334 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 65  t will be.** tre
11335 61 74 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f  ated as a hot-jo
11336 75 72 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64  urnal and rolled
11337 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63   back..*/.static
11338 20 76 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f   void pager_unlo
11339 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ck(Pager *pPager
1133a 29 7b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72  ){.  if( !pPager
1133b 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
1133c 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20  ){.    int rc;  
1133d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1133e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
1133f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 6c  de */..    /* Al
11340 77 61 79 73 20 63 6c 6f 73 65 20 74 68 65 20 6a  ways close the j
11341 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e  ournal file when
11342 20 64 72 6f 70 70 69 6e 67 20 74 68 65 20 64 61   dropping the da
11343 74 61 62 61 73 65 20 6c 6f 63 6b 2e 0a 20 20 20  tabase lock..   
11344 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61   ** Otherwise, a
11345 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f  nother connectio
11346 6e 20 77 69 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d  n with journal_m
11347 6f 64 65 3d 64 65 6c 65 74 65 20 6d 69 67 68 74  ode=delete might
11348 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 65 20 74  .    ** delete t
11349 68 65 20 66 69 6c 65 20 6f 75 74 20 66 72 6f 6d  he file out from
1134a 20 75 6e 64 65 72 20 75 73 2e 0a 20 20 20 20 2a   under us..    *
1134b 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43  /.    sqlite3OsC
1134c 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  lose(pPager->jfd
1134d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69  );.    sqlite3Bi
1134e 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67  tvecDestroy(pPag
1134f 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b  er->pInJournal);
11350 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e  .    pPager->pIn
11351 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20  Journal = 0;.   
11352 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70   releaseAllSavep
11353 6f 69 6e 74 73 28 70 50 61 67 65 72 29 3b 0a 0a  oints(pPager);..
11354 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69      /* If the fi
11355 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20  le is unlocked, 
11356 73 6f 6d 65 62 6f 64 79 20 65 6c 73 65 20 6d 69  somebody else mi
11357 67 68 74 20 63 68 61 6e 67 65 20 69 74 2e 20 54  ght change it. T
11358 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73  he.    ** values
11359 20 73 74 6f 72 65 64 20 69 6e 20 50 61 67 65 72   stored in Pager
1135a 2e 64 62 53 69 7a 65 20 65 74 63 2e 20 6d 69 67  .dbSize etc. mig
1135b 68 74 20 62 65 63 6f 6d 65 20 69 6e 76 61 6c 69  ht become invali
1135c 64 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 69 73  d if.    ** this
1135d 20 68 61 70 70 65 6e 73 2e 20 54 4f 44 4f 3a 20   happens. TODO: 
1135e 52 65 61 6c 6c 79 2c 20 74 68 69 73 20 64 6f 65  Really, this doe
1135f 73 6e 27 74 20 6e 65 65 64 20 74 6f 20 62 65 20  sn't need to be 
11360 63 6c 65 61 72 65 64 0a 20 20 20 20 2a 2a 20 75  cleared.    ** u
11361 6e 74 69 6c 20 74 68 65 20 63 68 61 6e 67 65 2d  ntil the change-
11362 63 6f 75 6e 74 65 72 20 63 68 65 63 6b 20 66 61  counter check fa
11363 69 6c 73 20 69 6e 20 70 61 67 65 72 53 68 61 72  ils in pagerShar
11364 65 64 4c 6f 63 6b 28 29 2e 0a 20 20 20 20 2a 2f  edLock()..    */
11365 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53  .    pPager->dbS
11366 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20  izeValid = 0;.. 
11367 20 20 20 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b     rc = osUnlock
11368 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f  (pPager->fd, NO_
11369 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72  LOCK);.    if( r
1136a 63 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  c ){.      pPage
1136b 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b  r->errCode = rc;
1136c 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41  .    }.    IOTRA
1136d 43 45 28 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e  CE(("UNLOCK %p\n
1136e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20  ", pPager))..   
1136f 20 2f 2a 20 49 66 20 50 61 67 65 72 2e 65 72 72   /* If Pager.err
11370 43 6f 64 65 20 69 73 20 73 65 74 2c 20 74 68 65  Code is set, the
11371 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
11372 20 70 61 67 65 72 20 63 61 63 68 65 20 63 61 6e   pager cache can
11373 6e 6f 74 20 62 65 0a 20 20 20 20 2a 2a 20 74 72  not be.    ** tr
11374 75 73 74 65 64 2e 20 4e 6f 77 20 74 68 61 74 20  usted. Now that 
11375 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 69  the pager file i
11376 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 20  s unlocked, the 
11377 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a  contents of the.
11378 20 20 20 20 2a 2a 20 63 61 63 68 65 20 63 61 6e      ** cache can
11379 20 62 65 20 64 69 73 63 61 72 64 65 64 20 61 6e   be discarded an
1137a 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  d the error code
1137b 20 73 61 66 65 6c 79 20 63 6c 65 61 72 65 64 2e   safely cleared.
1137c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1137d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
1137e 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  ){.      if( rc=
1137f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11380 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72        pPager->er
11381 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f  rCode = SQLITE_O
11382 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  K;.      }.     
11383 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61   pager_reset(pPa
11384 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ger);.    }..   
11385 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43   pPager->changeC
11386 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20  ountDone = 0;.  
11387 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
11388 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 0a  = PAGER_UNLOCK;.
11389 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
1138a 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  s function shoul
1138b 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  d be called when
1138c 20 61 6e 20 49 4f 45 52 52 2c 20 43 4f 52 52 55   an IOERR, CORRU
1138d 50 54 20 6f 72 20 46 55 4c 4c 20 65 72 72 6f 72  PT or FULL error
1138e 0a 2a 2a 20 6d 61 79 20 68 61 76 65 20 6f 63 63  .** may have occ
1138f 75 72 72 65 64 2e 20 54 68 65 20 66 69 72 73 74  urred. The first
11390 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
11391 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61  ointer to the pa
11392 67 65 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  ger .** structur
11393 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 74 68  e, the second th
11394 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 61 62 6f  e error-code abo
11395 75 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  ut to be returne
11396 64 20 62 79 20 61 20 70 61 67 65 72 20 0a 2a 2a  d by a pager .**
11397 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 54   API function. T
11398 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
11399 64 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  d is a copy of t
1139a 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
1139b 6e 74 20 0a 2a 2a 20 74 6f 20 74 68 69 73 20 66  nt .** to this f
1139c 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20  unction. .**.** 
1139d 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
1139e 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45  gument is SQLITE
1139f 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43  _IOERR, SQLITE_C
113a0 4f 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 54  ORRUPT, or SQLIT
113a1 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72  E_FULL.** the er
113a2 72 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72 73  ror becomes pers
113a3 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68  istent. Until th
113a4 65 20 70 65 72 73 69 73 74 65 6e 20 65 72 72 6f  e persisten erro
113a5 72 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a  r is cleared,.**
113a6 20 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 20   subsequent API 
113a7 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 61  calls on this Pa
113a8 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61  ger will immedia
113a9 74 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20  tely return the 
113aa 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63  same .** error c
113ab 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72  ode..**.** A per
113ac 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69 6e  sistent error in
113ad 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65  dicates that the
113ae 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
113af 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a   pager-cache .**
113b0 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74   cannot be trust
113b1 65 64 2e 20 54 68 69 73 20 73 74 61 74 65 20 63  ed. This state c
113b2 61 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62 79  an be cleared by
113b3 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 63   completely disc
113b4 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63  arding .** the c
113b5 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70  ontents of the p
113b6 61 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20 61  ager-cache. If a
113b7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
113b8 20 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20   active when.** 
113b9 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 65  the persistent e
113ba 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74  rror occurred, t
113bb 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  hen the rollback
113bc 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65   journal may nee
113bd 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c 61  d.** to be repla
113be 79 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20 74  yed to restore t
113bf 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
113c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
113c1 20 28 61 73 20 69 66 0a 2a 2a 20 69 74 20 77 65   (as if.** it we
113c2 72 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  re a hot-journal
113c3 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
113c4 20 70 61 67 65 72 5f 65 72 72 6f 72 28 50 61 67   pager_error(Pag
113c5 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
113c6 72 63 29 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d  rc){.  int rc2 =
113c7 20 72 63 20 26 20 30 78 66 66 3b 0a 20 20 61 73   rc & 0xff;.  as
113c8 73 65 72 74 28 0a 20 20 20 20 20 20 20 70 50 61  sert(.       pPa
113c9 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51  ger->errCode==SQ
113ca 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20  LITE_FULL ||.   
113cb 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43      pPager->errC
113cc 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  ode==SQLITE_OK |
113cd 7c 0a 20 20 20 20 20 20 20 28 70 50 61 67 65 72  |.       (pPager
113ce 2d 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66 66  ->errCode & 0xff
113cf 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a  )==SQLITE_IOERR.
113d0 20 20 29 3b 0a 20 20 69 66 28 0a 20 20 20 20 72    );.  if(.    r
113d1 63 32 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20  c2==SQLITE_FULL 
113d2 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53 51 4c 49  ||.    rc2==SQLI
113d3 54 45 5f 49 4f 45 52 52 20 7c 7c 0a 20 20 20 20  TE_IOERR ||.    
113d4 72 63 32 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52  rc2==SQLITE_CORR
113d5 55 50 54 0a 20 20 29 7b 0a 20 20 20 20 70 50 61  UPT.  ){.    pPa
113d6 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72  ger->errCode = r
113d7 63 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  c;.    if( pPage
113d8 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f  r->state==PAGER_
113d9 55 4e 4c 4f 43 4b 20 0a 20 20 20 20 20 26 26 20  UNLOCK .     && 
113da 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
113db 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50  Count(pPager->pP
113dc 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20 29  Cache)==0 .    )
113dd 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  {.      /* If th
113de 65 20 70 61 67 65 72 20 69 73 20 61 6c 72 65 61  e pager is alrea
113df 64 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 63 61 6c  dy unlocked, cal
113e0 6c 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29  l pager_unlock()
113e1 20 6e 6f 77 20 74 6f 0a 20 20 20 20 20 20 2a 2a   now to.      **
113e2 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72   clear the error
113e3 20 73 74 61 74 65 20 61 6e 64 20 65 6e 73 75 72   state and ensur
113e4 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72  e that the pager
113e5 2d 63 61 63 68 65 20 69 73 20 0a 20 20 20 20 20  -cache is .     
113e6 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65   ** completely e
113e7 6d 70 74 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  mpty..      */. 
113e8 20 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63       pager_unloc
113e9 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d  k(pPager);.    }
113ea 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
113eb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75  ;.}../*.** Execu
113ec 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 66  te a rollback if
113ed 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
113ee 73 20 61 63 74 69 76 65 20 61 6e 64 20 75 6e 6c  s active and unl
113ef 6f 63 6b 20 74 68 65 20 0a 2a 2a 20 64 61 74 61  ock the .** data
113f0 62 61 73 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a  base file. .**.*
113f1 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 68  * If the pager h
113f2 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72  as already enter
113f3 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  ed the error sta
113f4 74 65 2c 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d  te, do not attem
113f5 70 74 20 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62  pt .** the rollb
113f6 61 63 6b 20 61 74 20 74 68 69 73 20 74 69 6d 65  ack at this time
113f7 2e 20 49 6e 73 74 65 61 64 2c 20 70 61 67 65 72  . Instead, pager
113f8 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 63 61 6c  _unlock() is cal
113f9 6c 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c  led. The.** call
113fa 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b   to pager_unlock
113fb 28 29 20 77 69 6c 6c 20 64 69 73 63 61 72 64 20  () will discard 
113fc 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61  all in-memory pa
113fd 67 65 73 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74  ges, unlock.** t
113fe 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
113ff 20 61 6e 64 20 63 6c 65 61 72 20 74 68 65 20 65   and clear the e
11400 72 72 6f 72 20 73 74 61 74 65 2e 20 49 66 20 74  rror state. If t
11401 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a  his means that.*
11402 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74  * there is a hot
11403 2d 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 69 6e  -journal left in
11404 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d   the file-system
11405 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e 6e 65  , the next conne
11406 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61  ction.** to obta
11407 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  in a shared lock
11408 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 28 77   on the pager (w
11409 68 69 63 68 20 6d 61 79 20 62 65 20 74 68 69 73  hich may be this
1140a 20 6f 6e 65 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f   one) will.** ro
1140b 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a  ll it back..**.*
1140c 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 68  * If the pager h
1140d 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65  as not already e
1140e 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72  ntered the error
1140f 20 73 74 61 74 65 2c 20 62 75 74 20 61 6e 20 49   state, but an I
11410 4f 20 6f 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65  O or.** malloc e
11411 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
11412 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74  ng a rollback, t
11413 68 65 6e 20 74 68 69 73 20 77 69 6c 6c 20 69 74  hen this will it
11414 73 65 6c 66 20 63 61 75 73 65 20 0a 2a 2a 20 74  self cause .** t
11415 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e 74 65  he pager to ente
11416 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  r the error stat
11417 65 2e 20 57 68 69 63 68 20 77 69 6c 6c 20 62 65  e. Which will be
11418 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65 0a   cleared by the.
11419 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72  ** call to pager
1141a 5f 75 6e 6c 6f 63 6b 28 29 2c 20 61 73 20 64 65  _unlock(), as de
1141b 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a  scribed above..*
1141c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  /.static void pa
1141d 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c  gerUnlockAndRoll
1141e 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67  back(Pager *pPag
1141f 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65  er){.  if( pPage
11420 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49  r->errCode==SQLI
11421 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d  TE_OK && pPager-
11422 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45  >state>=PAGER_RE
11423 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 73 71  SERVED ){.    sq
11424 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
11425 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 73 71  Malloc();.    sq
11426 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
11427 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  ck(pPager);.    
11428 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
11429 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20 20  Malloc();.  }.  
1142a 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61  pager_unlock(pPa
1142b 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ger);.}../*.** T
1142c 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 64 73  his routine ends
1142d 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20   a transaction. 
1142e 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  A transaction is
1142f 20 75 73 75 61 6c 6c 79 20 65 6e 64 65 64 20 62   usually ended b
11430 79 20 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 43  y .** either a C
11431 4f 4d 4d 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42  OMMIT or a ROLLB
11432 41 43 4b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54  ACK operation. T
11433 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 79 20  his routine may 
11434 62 65 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 66  be called .** af
11435 74 65 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20  ter rollback of 
11436 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f  a hot-journal, o
11437 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  r if an error oc
11438 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65 6e 69  curs while openi
11439 6e 67 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61  ng.** the journa
1143a 6c 20 66 69 6c 65 20 6f 72 20 77 72 69 74 69 6e  l file or writin
1143b 67 20 74 68 65 20 76 65 72 79 20 66 69 72 73 74  g the very first
1143c 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20   journal-header 
1143d 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65  of a.** database
1143e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a   transaction..**
1143f 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65   .** If the page
11440 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53 48  r is in PAGER_SH
11441 41 52 45 44 20 6f 72 20 50 41 47 45 52 5f 55 4e  ARED or PAGER_UN
11442 4c 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e 20  LOCK state when 
11443 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
11444 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73  is called, it is
11445 20 61 20 6e 6f 2d 6f 70 20 28 72 65 74 75 72 6e   a no-op (return
11446 73 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a  s SQLITE_OK)..**
11447 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61  .** Otherwise, a
11448 6e 79 20 61 63 74 69 76 65 20 73 61 76 65 70 6f  ny active savepo
11449 69 6e 74 73 20 61 72 65 20 72 65 6c 65 61 73 65  ints are release
1144a 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1144b 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
1144c 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 69 73  open, then it is
1144d 20 22 66 69 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e   "finalized". On
1144e 63 65 20 61 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a  ce a journal .**
1144f 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 66   file has been f
11450 69 6e 61 6c 69 7a 65 64 20 69 74 20 69 73 20 6e  inalized it is n
11451 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75  ot possible to u
11452 73 65 20 69 74 20 74 6f 20 72 6f 6c 6c 20 62 61  se it to roll ba
11453 63 6b 20 61 20 0a 2a 2a 20 74 72 61 6e 73 61 63  ck a .** transac
11454 74 69 6f 6e 2e 20 4e 6f 72 20 77 69 6c 6c 20 69  tion. Nor will i
11455 74 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 20  t be considered 
11456 74 6f 20 62 65 20 61 20 68 6f 74 2d 6a 6f 75 72  to be a hot-jour
11457 6e 61 6c 20 62 79 20 74 68 69 73 0a 2a 2a 20 6f  nal by this.** o
11458 72 20 61 6e 79 20 6f 74 68 65 72 20 64 61 74 61  r any other data
11459 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
1145a 20 45 78 61 63 74 6c 79 20 68 6f 77 20 61 20 6a   Exactly how a j
1145b 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69  ournal is finali
1145c 7a 65 64 0a 2a 2a 20 64 65 70 65 6e 64 73 20 6f  zed.** depends o
1145d 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  n whether or not
1145e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75   the pager is ru
1145f 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69  nning in exclusi
11460 76 65 20 6d 6f 64 65 20 61 6e 64 0a 2a 2a 20 74  ve mode and.** t
11461 68 65 20 63 75 72 72 65 6e 74 20 6a 6f 75 72 6e  he current journ
11462 61 6c 2d 6d 6f 64 65 20 28 50 61 67 65 72 2e 6a  al-mode (Pager.j
11463 6f 75 72 6e 61 6c 4d 6f 64 65 20 76 61 6c 75 65  ournalMode value
11464 29 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ), as follows:.*
11465 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f  *.**   journalMo
11466 64 65 3d 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20  de==MEMORY.**   
11467 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64    Journal file d
11468 65 73 63 72 69 70 74 6f 72 20 69 73 20 73 69 6d  escriptor is sim
11469 70 6c 79 20 63 6c 6f 73 65 64 2e 20 54 68 69 73  ply closed. This
1146a 20 64 65 73 74 72 6f 79 73 20 61 6e 20 0a 2a 2a   destroys an .**
1146b 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a       in-memory j
1146c 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20  ournal..**.**   
1146d 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55  journalMode==TRU
1146e 4e 43 41 54 45 0a 2a 2a 20 20 20 20 20 4a 6f 75  NCATE.**     Jou
1146f 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 72 75  rnal file is tru
11470 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 62  ncated to zero b
11471 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a  ytes in size..**
11472 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64  .**   journalMod
11473 65 3d 3d 50 45 52 53 49 53 54 0a 2a 2a 20 20 20  e==PERSIST.**   
11474 20 20 54 68 65 20 66 69 72 73 74 20 32 38 20 62    The first 28 b
11475 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  ytes of the jour
11476 6e 61 6c 20 66 69 6c 65 20 61 72 65 20 7a 65 72  nal file are zer
11477 6f 65 64 2e 20 54 68 69 73 20 69 6e 76 61 6c 69  oed. This invali
11478 64 61 74 65 73 0a 2a 2a 20 20 20 20 20 74 68 65  dates.**     the
11479 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68   first journal h
1147a 65 61 64 65 72 20 69 6e 20 74 68 65 20 66 69 6c  eader in the fil
1147b 65 2c 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65  e, and hence the
1147c 20 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 0a   entire journal.
1147d 2a 2a 20 20 20 20 20 66 69 6c 65 2e 20 41 6e 20  **     file. An 
1147e 69 6e 76 61 6c 69 64 20 6a 6f 75 72 6e 61 6c 20  invalid journal 
1147f 66 69 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 72  file cannot be r
11480 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a  olled back..**.*
11481 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  *   journalMode=
11482 3d 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 54  =DELETE.**     T
11483 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11484 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65  is closed and de
11485 6c 65 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69  leted using sqli
11486 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a  te3OsDelete()..*
11487 2a 0a 2a 2a 20 20 20 20 20 49 66 20 74 68 65 20  *.**     If the 
11488 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67  pager is running
11489 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
1148a 64 65 2c 20 74 68 69 73 20 6d 65 74 68 6f 64 20  de, this method 
1148b 6f 66 20 66 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a  of finalizing.**
1148c 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c       the journal
1148d 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72 20 75   file is never u
1148e 73 65 64 2e 20 49 6e 73 74 65 61 64 2c 20 69 66  sed. Instead, if
1148f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65   the journalMode
11490 20 69 73 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54   is.**     DELET
11491 45 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20  E and the pager 
11492 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  is in exclusive 
11493 6d 6f 64 65 2c 20 74 68 65 20 6d 65 74 68 6f 64  mode, the method
11494 20 64 65 73 63 72 69 62 65 64 20 75 6e 64 65 72   described under
11495 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 4d  .**     journalM
11496 6f 64 65 3d 3d 50 45 52 53 49 53 54 20 69 73 20  ode==PERSIST is 
11497 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a  used instead..**
11498 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6a 6f  .** After the jo
11499 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a  urnal is finaliz
1149a 65 64 2c 20 69 66 20 72 75 6e 6e 69 6e 67 20 69  ed, if running i
1149b 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73 69 76 65 20  n non-exclusive 
1149c 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 70 61 67  mode, the.** pag
1149d 65 72 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45  er moves to PAGE
1149e 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 20 28  R_SHARED state (
1149f 61 6e 64 20 64 6f 77 6e 67 72 61 64 65 73 20 74  and downgrades t
114a0 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a  he lock on the.*
114a1 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
114a2 61 63 63 6f 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a  accordingly)..**
114a3 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72  .** If the pager
114a4 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65   is running in e
114a5 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e  xclusive mode an
114a6 64 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53 59  d is in PAGER_SY
114a7 4e 43 45 44 20 73 74 61 74 65 2c 0a 2a 2a 20 69  NCED state,.** i
114a8 74 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 52  t moves to PAGER
114a9 5f 45 58 43 4c 55 53 49 56 45 2e 20 4e 6f 20 6c  _EXCLUSIVE. No l
114aa 6f 63 6b 73 20 61 72 65 20 64 6f 77 6e 67 72 61  ocks are downgra
114ab 64 65 64 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67  ded when running
114ac 20 69 6e 0a 2a 2a 20 65 78 63 6c 75 73 69 76 65   in.** exclusive
114ad 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c   mode..**.** SQL
114ae 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
114af 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f  ed if no error o
114b0 63 63 75 72 73 2e 20 49 66 20 61 6e 20 65 72 72  ccurs. If an err
114b1 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
114b2 0a 2a 2a 20 61 6e 79 20 6f 66 20 74 68 65 20 49  .** any of the I
114b3 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f 20  O operations to 
114b4 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75  finalize the jou
114b5 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 75 6e 6c  rnal file or unl
114b6 6f 63 6b 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  ock the.** datab
114b7 61 73 65 20 74 68 65 6e 20 74 68 65 20 49 4f 20  ase then the IO 
114b8 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
114b9 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73  turned to the us
114ba 65 72 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 6f  er. If the .** o
114bb 70 65 72 61 74 69 6f 6e 20 74 6f 20 66 69 6e 61  peration to fina
114bc 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lize the journal
114bd 20 66 69 6c 65 20 66 61 69 6c 73 2c 20 74 68 65   file fails, the
114be 6e 20 74 68 65 20 63 6f 64 65 20 73 74 69 6c 6c  n the code still
114bf 0a 2a 2a 20 74 72 69 65 73 20 74 6f 20 75 6e 6c  .** tries to unl
114c0 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65  ock the database
114c1 20 66 69 6c 65 20 69 66 20 6e 6f 74 20 69 6e 20   file if not in 
114c2 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 20  exclusive mode. 
114c3 49 66 20 74 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b  If the.** unlock
114c4 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 73   operation fails
114c5 20 61 73 20 77 65 6c 6c 2c 20 74 68 65 6e 20 74   as well, then t
114c6 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 63  he first error c
114c7 6f 64 65 20 72 65 6c 61 74 65 64 0a 2a 2a 20 74  ode related.** t
114c8 6f 20 74 68 65 20 66 69 72 73 74 20 65 72 72 6f  o the first erro
114c9 72 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 28 74  r encountered (t
114ca 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c  he journal final
114cb 69 7a 61 74 69 6f 6e 20 6f 6e 65 29 20 69 73 0a  ization one) is.
114cc 2a 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  ** returned..*/.
114cd 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
114ce 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
114cf 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
114d0 69 6e 74 20 68 61 73 4d 61 73 74 65 72 29 7b 0a  int hasMaster){.
114d1 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
114d2 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 45 72  E_OK;      /* Er
114d3 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 6a 6f  ror code from jo
114d4 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69  urnal finalizati
114d5 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  on operation */.
114d6 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49    int rc2 = SQLI
114d7 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 72  TE_OK;     /* Er
114d8 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 64 62  ror code from db
114d9 20 66 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 65   file unlock ope
114da 72 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28  ration */..  if(
114db 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50   pPager->state<P
114dc 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b  AGER_RESERVED ){
114dd 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
114de 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c  TE_OK;.  }.  rel
114df 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74  easeAllSavepoint
114e0 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 61 73  s(pPager);..  as
114e1 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61  sert( isOpen(pPa
114e2 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61  ger->jfd) || pPa
114e3 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d  ger->pInJournal=
114e4 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73 4f 70  =0 );.  if( isOp
114e5 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
114e6 29 7b 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a  ){..    /* TODO:
114e7 20 54 68 65 72 65 27 73 20 61 20 70 72 6f 62 6c   There's a probl
114e8 65 6d 20 68 65 72 65 20 69 66 20 61 20 6a 6f 75  em here if a jou
114e9 72 6e 61 6c 2d 66 69 6c 65 20 77 61 73 20 6f 70  rnal-file was op
114ea 65 6e 65 64 20 69 6e 20 4d 45 4d 4f 52 59 0a 20  ened in MEMORY. 
114eb 20 20 20 2a 2a 20 6d 6f 64 65 20 61 6e 64 20 74     ** mode and t
114ec 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d  hen the journal-
114ed 6d 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64 20  mode is changed 
114ee 74 6f 20 54 52 55 4e 43 41 54 45 20 6f 72 20 50  to TRUNCATE or P
114ef 45 52 53 49 53 54 0a 20 20 20 20 2a 2a 20 64 75  ERSIST.    ** du
114f0 72 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63  ring the transac
114f1 74 69 6f 6e 2e 20 54 68 69 73 20 63 6f 64 65 20  tion. This code 
114f2 73 68 6f 75 6c 64 20 62 65 20 63 68 61 6e 67 65  should be change
114f3 64 20 74 6f 20 61 73 73 75 6d 65 0a 20 20 20 20  d to assume.    
114f4 2a 2a 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72  ** that the jour
114f5 6e 61 6c 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74  nal mode has not
114f6 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 74   changed since t
114f7 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  he transaction w
114f8 61 73 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 65  as.    ** starte
114f9 64 2e 20 41 6e 64 20 74 68 65 20 73 71 6c 69 74  d. And the sqlit
114fa 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f  e3PagerJournalMo
114fb 64 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 73 68  de() function sh
114fc 6f 75 6c 64 20 62 65 0a 20 20 20 20 2a 2a 20 63  ould be.    ** c
114fd 68 61 6e 67 65 64 20 74 6f 20 6d 61 6b 65 20 73  hanged to make s
114fe 75 72 65 20 74 68 61 74 20 74 68 69 73 20 69 73  ure that this is
114ff 20 74 68 65 20 63 61 73 65 20 74 6f 6f 2e 0a 20   the case too.. 
11500 20 20 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69     */..    /* Fi
11501 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e  nalize the journ
11502 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20  al file. */.    
11503 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
11504 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
11505 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
11506 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  Y ){.      int i
11507 73 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61 6c 20 3d  sMemoryJournal =
11508 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75   sqlite3IsMemJou
11509 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64  rnal(pPager->jfd
1150a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1150b 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
1150c 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  jfd);.      if( 
1150d 21 69 73 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61 6c  !isMemoryJournal
1150e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1150f 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
11510 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70  (pPager->pVfs, p
11511 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
11512 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
11513 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65   }else if( pPage
11514 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
11515 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
11516 45 5f 54 52 55 4e 43 41 54 45 20 29 7b 0a 20 20  E_TRUNCATE ){.  
11517 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
11518 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b  journalOff==0 ){
11519 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1151a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
1151b 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
1151c 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e   = sqlite3OsTrun
1151d 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  cate(pPager->jfd
1151e 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
1151f 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
11520 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20  nalOff = 0;.    
11521 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11522 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20  lStarted = 0;.  
11523 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67    }else if( pPag
11524 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
11525 65 20 0a 20 20 20 20 20 7c 7c 20 70 50 61 67 65  e .     || pPage
11526 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
11527 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
11528 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 29 7b  E_PERSIST.    ){
11529 0a 20 20 20 20 20 20 72 63 20 3d 20 7a 65 72 6f  .      rc = zero
1152a 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65  JournalHdr(pPage
1152b 72 2c 20 68 61 73 4d 61 73 74 65 72 29 3b 0a 20  r, hasMaster);. 
1152c 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72       pager_error
1152d 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20  (pPager, rc);.  
1152e 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
1152f 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20  nalOff = 0;.    
11530 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11531 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20  lStarted = 0;.  
11532 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
11533 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a  ssert( pPager->j
11534 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
11535 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45  R_JOURNALMODE_DE
11536 4c 45 54 45 20 7c 7c 20 72 63 20 29 3b 0a 20 20  LETE || rc );.  
11537 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
11538 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  se(pPager->jfd);
11539 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
1153a 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61  QLITE_OK && !pPa
1153b 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b  ger->tempFile ){
1153c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1153d 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 50  lite3OsDelete(pP
1153e 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 67  ager->pVfs, pPag
1153f 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29  er->zJournal, 0)
11540 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
11541 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
11542 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 73  HECK_PAGES.    s
11543 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72  qlite3PcacheIter
11544 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72 2d  ateDirty(pPager-
11545 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72 5f  >pPCache, pager_
11546 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a 23  set_pagehash);.#
11547 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c 69 74  endif..    sqlit
11548 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c  e3PcacheCleanAll
11549 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
1154a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69  );.    sqlite3Bi
1154b 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67  tvecDestroy(pPag
1154c 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b  er->pInJournal);
1154d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e  .    pPager->pIn
1154e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20  Journal = 0;.   
1154f 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20   pPager->nRec = 
11550 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70  0;.  }..  if( !p
11551 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
11552 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32 20  Mode ){.    rc2 
11553 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65  = osUnlock(pPage
11554 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f  r->fd, SHARED_LO
11555 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  CK);.    pPager-
11556 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53  >state = PAGER_S
11557 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67 65  HARED;.    pPage
11558 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
11559 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  ne = 0;.  }else 
1155a 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
1155b 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20  e==PAGER_SYNCED 
1155c 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  ){.    pPager->s
1155d 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43  tate = PAGER_EXC
1155e 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70 50  LUSIVE;.  }.  pP
1155f 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20  ager->setMaster 
11560 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  = 0;.  pPager->n
11561 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 70  eedSync = 0;.  p
11562 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65  Pager->dbModifie
11563 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f 44  d = 0;..  /* TOD
11564 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 69 6d  O: Is this optim
11565 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 20 64  al? Why is the d
11566 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 61 74  b size invalidat
11567 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 77 68  ed here .  ** wh
11568 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
11569 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e 6c 6f  file is not unlo
1156a 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 67 65  cked? */.  pPage
1156b 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20  r->dbOrigSize = 
1156c 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  0;.  sqlite3Pcac
1156d 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67 65  heTruncate(pPage
1156e 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61 67  r->pPCache, pPag
1156f 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69  er->dbSize);.  i
11570 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20  f( !MEMDB ){.   
11571 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
11572 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  alid = 0;.  }.. 
11573 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c   return (rc==SQL
11574 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a  ITE_OK?rc2:rc);.
11575 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  }../*.** Paramet
11576 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70 6f  er aData must po
11577 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20  int to a buffer 
11578 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  of pPager->pageS
11579 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20  ize bytes.** of 
1157a 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61 6e  data. Compute an
1157b 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63 6b  d return a check
1157c 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74 68  sum based ont th
1157d 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
1157e 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 64 61  e .** page of da
1157f 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 72 65  ta and the curre
11580 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 61 67  nt value of pPag
11581 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a 2a  er->cksumInit..*
11582 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74  *.** This is not
11583 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75 6d   a real checksum
11584 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20 6a  . It is really j
11585 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20 74  ust the sum of t
11586 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69 6e  he .** random in
11587 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 50 61  itial value (pPa
11588 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 20  ger->cksumInit) 
11589 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 68 20  and every 200th 
1158a 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70  byte.** of the p
1158b 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 74 69  age data, starti
1158c 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f 66 66  ng with byte off
1158d 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 61 67  set (pPager->pag
1158e 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a 20 45  eSize%200)..** E
1158f 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74 65  ach byte is inte
11590 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 38 2d  rpreted as an 8-
11591 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  bit unsigned int
11592 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e  eger..**.** Chan
11593 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 6c 61  ging the formula
11594 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 65   used to compute
11595 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d 20 72   this checksum r
11596 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a 20  esults in an.** 
11597 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f 75  incompatible jou
11598 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74  rnal file format
11599 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72 6e  ..**.** If journ
1159a 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f 63  al corruption oc
1159b 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 70 6f  curs due to a po
1159c 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68 65  wer failure, the
1159d 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a 2a 2a   most likely .**
1159e 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68 61   scenario is tha
1159f 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 68 65  t one end or the
115a0 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 72 65   other of the re
115a1 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 68 61  cord will be cha
115a2 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 73 20  nged. .** It is 
115a3 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c 79  much less likely
115a4 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 65 6e   that the two en
115a5 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ds of the journa
115a6 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65  l record will be
115a7 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64 20  .** correct and 
115a8 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63 6f  the middle be co
115a9 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74 68  rrupt.  Thus, th
115aa 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73 63  is "checksum" sc
115ab 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 68 20  heme,.** though 
115ac 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65 2c  fast and simple,
115ad 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f 73   catches the mos
115ae 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64 20  tly likely kind 
115af 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a  of corruption..*
115b0 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 67  /.static u32 pag
115b1 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20 2a  er_cksum(Pager *
115b2 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75 38  pPager, const u8
115b3 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32 20   *aData){.  u32 
115b4 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e  cksum = pPager->
115b5 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 20  cksumInit;      
115b6 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 76     /* Checksum v
115b7 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a  alue to return *
115b8 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61 67  /.  int i = pPag
115b9 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 30  er->pageSize-200
115ba 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  ;          /* Lo
115bb 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
115bc 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20  while( i>0 ){.  
115bd 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 74 61    cksum += aData
115be 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 32 30  [i];.    i -= 20
115bf 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
115c0 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  cksum;.}../*.** 
115c1 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70 61  Read a single pa
115c2 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20 74  ge from either t
115c3 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
115c4 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d  (if isMainJrnl==
115c5 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74 68  1) or.** from th
115c6 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28 69  e sub-journal (i
115c7 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 29  f isMainJrnl==0)
115c8 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74 68   and playback th
115c9 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20  at page..** The 
115ca 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20 6f  page begins at o
115cb 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20 69  ffset *pOffset i
115cc 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 68  nto the file. Th
115cd 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76 61  e *pOffset.** va
115ce 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 64  lue is increased
115cf 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
115d0 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69   the next page i
115d1 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a  n the journal..*
115d2 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e 4a  *.** The isMainJ
115d3 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75 65  rnl flag is true
115d4 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20   if this is the 
115d5 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  main rollback jo
115d6 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61 6c  urnal and.** fal
115d7 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74 65  se for the state
115d8 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 54  ment journal.  T
115d9 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b  he main rollback
115da 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a 2a   journal uses.**
115db 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68 65   checksums - the
115dc 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
115dd 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a  al does not..**.
115de 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 6e  ** If the page n
115df 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
115e0 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 66 72  e record read fr
115e1 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75  om the (sub-)jou
115e2 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20  rnal file.** is 
115e3 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
115e4 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f   current value o
115e5 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20  f Pager.dbSize, 
115e6 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 69 73  then playback is
115e7 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e 64 20  .** skipped and 
115e8 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
115e9 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
115ea 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e 55 4c  pDone is not NUL
115eb 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61 20  L, then it is a 
115ec 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 73 20  record of pages 
115ed 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 61 64  that have alread
115ee 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 65 64  y.** been played
115ef 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 20 70   back.  If the p
115f0 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 74 20  age at *pOffset 
115f1 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
115f2 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a 2a 20   played back.** 
115f3 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f  (if the correspo
115f4 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 74 20  nding pDone bit 
115f5 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 6b 69  is set) then ski
115f6 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b 2e 0a  p the playback..
115f7 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  ** Make sure the
115f8 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 72 65   pDone bit corre
115f9 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20  sponding to the 
115fa 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 69 73  *pOffset page is
115fb 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 74 6f   set.** prior to
115fc 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a   returning..**.*
115fd 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65  * If the page re
115fe 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 73 66  cord is successf
115ff 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 74  ully read from t
11600 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c  he (sub-)journal
11601 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c 61   file.** and pla
11602 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20 53  yed back, then S
11603 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
11604 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65  rned. If an IO e
11605 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 77  rror occurs.** w
11606 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68 65  hile reading the
11607 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68 65   record from the
11608 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66   (sub-)journal f
11609 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72 69  ile or while wri
1160a 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20 64  ting.** to the d
1160b 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
1160c 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20  en the IO error 
1160d 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
1160e 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 73 20  . If data.** is 
1160f 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61  successfully rea
11610 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d  d from the (sub-
11611 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 75  )journal file bu
11612 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 0a  t appears to be.
11613 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51  ** corrupted, SQ
11614 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74  LITE_DONE is ret
11615 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 20 63  urned. Data is c
11616 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 75 70  onsidered corrup
11617 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 63 69  ted in.** two ci
11618 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a 20  rcumstances:.** 
11619 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 72  .**   * If the r
1161a 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d 62 65  ecord page-numbe
1161b 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 30 20  r is illegal (0 
1161c 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  or PAGER_MJ_PGNO
1161d 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 66 20  ), or.**   * If 
1161e 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 62 65  the record is be
1161f 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  ing rolled back 
11620 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f  from the main jo
11621 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20  urnal file.**   
11622 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73    and the checks
11623 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 6e 6f  um field does no
11624 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 63 6f  t match the reco
11625 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a  rd content..**.*
11626 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65  * Neither of the
11627 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f 73  se two scenarios
11628 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 64 75   are possible du
11629 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 6e 74  ring a savepoint
1162a 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
1162b 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73 61   If this is a sa
1162c 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b  vepoint rollback
1162d 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 6d 61  , then memory ma
1162e 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 79 6e  y have to be dyn
1162f 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f  amically.** allo
11630 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75  cated by this fu
11631 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 20  nction. If this 
11632 69 73 20 74 68 65 20 63 61 73 65 20 61 6e 64 20  is the case and 
11633 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  an allocation fa
11634 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e  ils,.** SQLITE_N
11635 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64  OMEM is returned
11636 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11637 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f  pager_playback_o
11638 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 65 72  ne_page(.  Pager
11639 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20   *pPager,       
1163a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1163b 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c 61 79  pager being play
1163c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  ed back */.  int
1163d 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20 20   isMainJrnl,    
1163e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
1163f 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2e  -> main journal.
11640 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e 61   0 -> sub-journa
11641 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 6e  l. */.  int isUn
11642 73 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20  sync,           
11643 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
11644 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 75 6e   reading from un
11645 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a 6f 75 72  synced main jour
11646 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 2a 70 4f  nal */.  i64 *pO
11647 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
11648 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
11649 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70 6c   of record to pl
1164a 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20  ayback */.  int 
1164b 69 73 53 61 76 65 70 6e 74 2c 20 20 20 20 20 20  isSavepnt,      
1164c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1164d 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69 6e  e for a savepoin
1164e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  t rollback */.  
1164f 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 20 20  Bitvec *pDone   
11650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11651 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 65 73   Bitvec of pages
11652 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 64 20   already played 
11653 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  back */.){.  int
11654 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 50   rc;.  PgHdr *pP
11655 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
11656 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 74       /* An exist
11657 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 20  ing page in the 
11658 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  cache */.  Pgno 
11659 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20  pgno;           
1165a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1165b 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61  page number of a
1165c 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c   page in journal
1165d 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b   */.  u32 cksum;
1165e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1165f 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20      /* Checksum 
11660 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 20  used for sanity 
11661 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 38  checking */.  u8
11662 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20   *aData;        
11663 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
11664 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65  emporary storage
11665 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 2a 2f   for the page */
11666 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
11667 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 20  *jfd;           
11668 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73   /* The file des
11669 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20  criptor for the 
1166a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
1166b 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d 61  .  assert( (isMa
1166c 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29 3b  inJrnl&~1)==0 );
1166d 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e 4a        /* isMainJ
1166e 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a 2f  rnl is 0 or 1 */
1166f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53 61  .  assert( (isSa
11670 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b 20  vepnt&~1)==0 ); 
11671 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65 70        /* isSavep
11672 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a  nt is 0 or 1 */.
11673 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69 6e    assert( isMain
11674 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29 3b  Jrnl || pDone );
11675 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61 6c       /* pDone al
11676 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75 62  ways used on sub
11677 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 61  -journals */.  a
11678 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e 74  ssert( isSavepnt
11679 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b 20   || pDone==0 ); 
1167a 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65 72    /* pDone never
1167b 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61 76   used on non-sav
1167c 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44 61  epoint */..  aDa
1167d 74 61 20 3d 20 28 75 38 2a 29 70 50 61 67 65 72  ta = (u8*)pPager
1167e 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 61  ->pTmpSpace;.  a
1167f 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b 20  ssert( aData ); 
11680 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20          /* Temp 
11681 73 74 6f 72 61 67 65 20 6d 75 73 74 20 68 61 76  storage must hav
11682 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61  e already been a
11683 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 2f  llocated */..  /
11684 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 20  * Read the page 
11685 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20  number and page 
11686 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 6a 6f  data from the jo
11687 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75  urnal or sub-jou
11688 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e 20  rnal.  ** file. 
11689 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
1168a 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c  code to the call
1168b 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f  er if an IO erro
1168c 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a 20  r occurs..  */. 
1168d 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72 6e   jfd = isMainJrn
1168e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 20  l ? pPager->jfd 
1168f 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b 0a  : pPager->sjfd;.
11690 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74    rc = read32bit
11691 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74 2c  s(jfd, *pOffset,
11692 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72   &pgno);.  if( r
11693 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
11694 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d  eturn rc;.  rc =
11695 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 6a   sqlite3OsRead(j
11696 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65  fd, aData, pPage
11697 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 2a 70  r->pageSize, (*p
11698 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 69 66  Offset)+4);.  if
11699 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1169a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a  ) return rc;.  *
1169b 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 67 65  pOffset += pPage
1169c 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34 20  r->pageSize + 4 
1169d 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b 0a  + isMainJrnl*4;.
1169e 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65  .  /* Sanity che
1169f 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61 67  cking on the pag
116a0 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72 65  e.  This is more
116a1 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20   important that 
116a2 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 2a  I originally.  *
116a3 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20 61  * thought.  If a
116a4 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f   power failure o
116a5 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65 20  ccurs while the 
116a6 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e 67  journal is being
116a7 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20 69   written,.  ** i
116a8 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69 6e  t could cause in
116a9 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62 65  valid data to be
116aa 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
116ab 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20 6e  e journal.  We n
116ac 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74 65  eed to.  ** dete
116ad 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64 20  ct this invalid 
116ae 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68 20  data (with high 
116af 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e 64  probability) and
116b0 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a 2f   ignore it..  */
116b1 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 7c  .  if( pgno==0 |
116b2 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a  | pgno==PAGER_MJ
116b3 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b  _PGNO(pPager) ){
116b4 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73  .    assert( !is
116b5 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 72  Savepnt );.    r
116b6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
116b7 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 6e  E;.  }.  if( pgn
116b8 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d 3e  o>(Pgno)pPager->
116b9 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 74 65  dbSize || sqlite
116ba 33 42 69 74 76 65 63 54 65 73 74 28 70 44 6f 6e  3BitvecTest(pDon
116bb 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20  e, pgno) ){.    
116bc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
116bd 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4d 61  ;.  }.  if( isMa
116be 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72 63  inJrnl ){.    rc
116bf 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66   = read32bits(jf
116c0 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d 34 2c  d, (*pOffset)-4,
116c1 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69 66   &cksum);.    if
116c2 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
116c3 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 61 76  ;.    if( !isSav
116c4 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f 63 6b  epnt && pager_ck
116c5 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 74  sum(pPager, aDat
116c6 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20 20  a)!=cksum ){.   
116c7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
116c8 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _DONE;.    }.  }
116c9 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26 26  ..  if( pDone &&
116ca 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69   (rc = sqlite3Bi
116cb 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20 70  tvecSet(pDone, p
116cc 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b  gno))!=SQLITE_OK
116cd 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
116ce 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  c;.  }..  assert
116cf 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  ( pPager->state=
116d0 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  =PAGER_RESERVED 
116d1 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  || pPager->state
116d2 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  >=PAGER_EXCLUSIV
116d3 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  E );..  /* If th
116d4 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 52 45  e pager is in RE
116d5 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68  SERVED state, th
116d6 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 65  en there must be
116d7 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73 0a   a copy of this.
116d8 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65    ** page in the
116d9 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 6e   pager cache. In
116da 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74 20   this case just 
116db 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72  update the pager
116dc 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74   cache,.  ** not
116dd 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
116de 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 20  le. The page is 
116df 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72 74  left marked dirt
116e0 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  y in this case..
116e1 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 63    **.  ** An exc
116e2 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 62  eption to the ab
116e3 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68 65  ove rule: If the
116e4 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 20   database is in 
116e5 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 2a  no-sync mode.  *
116e6 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 73 20  * and a page is 
116e7 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20  moved during an 
116e8 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75  incremental vacu
116e9 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67 65  um then the page
116ea 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 65   may.  ** not be
116eb 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61   in the pager ca
116ec 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61  che. Later: if a
116ed 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20   malloc() or IO 
116ee 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 2a  error occurs.  *
116ef 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70  * during a Movep
116f0 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e  age() call, then
116f1 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f   the page may no
116f2 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68  t be in the cach
116f3 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 53  e.  ** either. S
116f4 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20  o the condition 
116f5 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65  described in the
116f6 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70 68   above paragraph
116f7 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 73   is not.  ** ass
116f8 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a  ert()able..  **.
116f9 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c 55    ** If in EXCLU
116fa 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e  SIVE state, then
116fb 20 77 65 20 75 70 64 61 74 65 20 74 68 65 20 70   we update the p
116fc 61 67 65 72 20 63 61 63 68 65 20 69 66 20 69 74  ager cache if it
116fd 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e 64   exists.  ** and
116fe 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e 20   the main file. 
116ff 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65 6e  The page is then
11700 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 74   marked not dirt
11701 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63  y..  **.  ** Tic
11702 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 65 20  ket #1171:  The 
11703 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
11704 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20  l might contain 
11705 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61  page content tha
11706 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72  t is.  ** differ
11707 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67  ent from the pag
11708 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65  e content at the
11709 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72   start of the tr
1170a 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20  ansaction..  ** 
1170b 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e  This occurs when
1170c 20 61 20 70 61 67 65 20 69 73 20 63 68 61 6e 67   a page is chang
1170d 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  ed prior to the 
1170e 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 74 65  start of a state
1170f 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63  ment.  ** then c
11710 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74  hanged again wit
11711 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e  hin the statemen
11712 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67  t.  When rolling
11713 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a   back such a.  *
11714 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d  * statement we m
11715 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f  ust not write to
11716 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
11717 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65  tabase unless we
11718 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63   know.  ** for c
11719 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67  ertain that orig
1171a 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e  inal page conten
1171b 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e  ts are synced in
1171c 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c  to the main roll
1171d 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61  back.  ** journa
1171e 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61  l.  Otherwise, a
1171f 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68   power loss migh
11720 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64  t leave modified
11721 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a   data in the.  *
11722 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
11723 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79  without an entry
11724 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   in the rollback
11725 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61   journal that ca
11726 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74  n.  ** restore t
11727 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69  he database to i
11728 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d  ts original form
11729 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e  .  Two condition
1172a 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d  s must be.  ** m
1172b 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e  et before writin
1172c 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  g to the databas
1172d 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65  e files. (1) the
1172e 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62   database must b
1172f 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20  e.  ** locked.  
11730 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74  (2) we know that
11731 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61   the original pa
11732 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75  ge content is fu
11733 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20  lly synced.  ** 
11734 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  in the main jour
11735 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 75  nal either becau
11736 73 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  se the page is n
11737 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65  ot in cache or e
11738 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67  lse.  ** the pag
11739 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e  e is marked as n
1173a 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a  eedSync==0..  **
1173b 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34  .  ** 2008-04-14
1173c 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69  :  When attempti
1173d 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63  ng to vacuum a c
1173e 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20  orrupt database 
1173f 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73  file, it.  ** is
11740 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69   possible to fai
11741 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e  l a statement on
11742 20 61 20 64 61 74 61 62 61 73 65 20 74 68 61 74   a database that
11743 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78   does not yet ex
11744 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74  ist..  ** Do not
11745 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74   attempt to writ
11746 65 20 69 66 20 64 61 74 61 62 61 73 65 20 66 69  e if database fi
11747 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 65  le has never bee
11748 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20  n opened..  */. 
11749 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f   pPg = pager_loo
1174a 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f  kup(pPager, pgno
1174b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67  );.  assert( pPg
1174c 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20   || !MEMDB );.  
1174d 50 41 47 45 52 54 52 41 43 45 28 28 22 50 4c 41  PAGERTRACE(("PLA
1174e 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 25 64  YBACK %d page %d
1174f 20 68 61 73 68 28 25 30 38 78 29 20 25 73 5c 6e   hash(%08x) %s\n
11750 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
11751 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72    PAGERID(pPager
11752 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 64  ), pgno, pager_d
11753 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d 3e  atahash(pPager->
11754 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61 29  pageSize, aData)
11755 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
11756 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d 61   (isMainJrnl?"ma
11757 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75 62  in-journal":"sub
11758 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29 3b  -journal").  ));
11759 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e  .  if( (pPager->
1175a 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
1175b 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 28 70  LUSIVE).   && (p
1175c 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50 67  Pg==0 || 0==(pPg
1175d 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
1175e 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26 20  ED_SYNC)).   && 
1175f 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
11760 64 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73 79  d).   && !isUnsy
11761 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34 20  nc.  ){.    i64 
11762 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a  ofst = (pgno-1)*
11763 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67  (i64)pPager->pag
11764 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20  eSize;.    rc = 
11765 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
11766 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74 61  Pager->fd, aData
11767 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
11768 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20 69  ze, ofst);.    i
11769 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e  f( pgno>pPager->
1176a 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20  dbFileSize ){.  
1176b 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69      pPager->dbFi
1176c 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20  leSize = pgno;. 
1176d 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1176e 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61  BackupUpdate(pPa
1176f 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67  ger->pBackup, pg
11770 6e 6f 2c 20 61 44 61 74 61 29 3b 0a 20 20 7d 65  no, aData);.  }e
11771 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 6e 4a  lse if( !isMainJ
11772 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 29 7b  rnl && pPg==0 ){
11773 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
11774 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 66  is a rollback of
11775 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64   a savepoint and
11776 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77 72   data was not wr
11777 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a 20  itten to.    ** 
11778 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64  the database and
11779 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
1177a 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 72   in-memory, ther
1177b 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61 6c  e is a potential
1177c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 2e  .    ** problem.
1177d 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 20 69   When the page i
1177e 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 20 62  s next fetched b
1177f 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79  y the b-tree lay
11780 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20 77  er, it .    ** w
11781 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 6f 6d  ill be read from
11782 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
11783 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6f 72  le, which may or
11784 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 20 20   may not be .   
11785 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a 20 20   ** current. .  
11786 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 72    **.    ** Ther
11787 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 20 6f  e are a couple o
11788 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73  f different ways
11789 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e   this can happen
1178a 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 65 0a  . All are quite.
1178b 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 2e 20      ** obscure. 
1178c 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20  When running in 
1178d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65  synchronous mode
1178e 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20  , this can only 
1178f 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a 20 69  happen .    ** i
11790 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f 6e  f the page is on
11791 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61   the free-list a
11792 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
11793 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  he transaction, 
11794 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 75  then.    ** popu
11795 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f 76 65  lated, then move
11796 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 50  d using sqlite3P
11797 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 2e 0a  agerMovepage()..
11798 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
11799 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f  e solution is to
1179a 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72   add an in-memor
1179b 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 61  y page to the ca
1179c 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20  che containing. 
1179d 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20 6a     ** the data j
1179e 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 74 68  ust read from th
1179f 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 4d  e sub-journal. M
117a0 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20  ark the page as 
117a1 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 61 6e  dirty .    ** an
117a2 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20 72  d if the pager r
117a3 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 6e 61  equires a journa
117a4 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d 61 72  l-sync, then mar
117a5 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 0a 20  k the page as . 
117a6 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67 20     ** requiring 
117a7 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 62  a journal-sync b
117a8 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 69 74  efore it is writ
117a9 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ten..    */.    
117aa 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e  assert( isSavepn
117ab 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63  t );.    if( (rc
117ac 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 41   = sqlite3PagerA
117ad 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20 70  cquire(pPager, p
117ae 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 21 3d  gno, &pPg, 1))!=
117af 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
117b0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
117b1 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61    }.    pPg->fla
117b2 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45  gs &= ~PGHDR_NEE
117b3 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71 6c 69  D_READ;.    sqli
117b4 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72  te3PcacheMakeDir
117b5 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 69  ty(pPg);.  }.  i
117b6 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a  f( pPg ){.    /*
117b7 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 20   No page should 
117b8 65 76 65 72 20 62 65 20 65 78 70 6c 69 63 69 74  ever be explicit
117b9 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74  ly rolled back t
117ba 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c 20 65  hat is in use, e
117bb 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72  xcept.    ** for
117bc 20 70 61 67 65 20 31 20 77 68 69 63 68 20 69 73   page 1 which is
117bd 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69 6e 20   held in use in 
117be 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68  order to keep th
117bf 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20  e lock on the.  
117c0 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63    ** database ac
117c1 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20 73 75  tive. However su
117c2 63 68 20 61 20 70 61 67 65 20 6d 61 79 20 62 65  ch a page may be
117c3 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20   rolled back as 
117c4 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20  a result.    ** 
117c5 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65  of an internal e
117c6 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 69  rror resulting i
117c7 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 63  n an automatic c
117c8 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71  all to.    ** sq
117c9 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
117ca 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ck()..    */.   
117cb 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 20   void *pData;.  
117cc 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70    pData = pPg->p
117cd 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79  Data;.    memcpy
117ce 28 70 44 61 74 61 2c 20 61 44 61 74 61 2c 20 70  (pData, aData, p
117cf 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29  Pager->pageSize)
117d0 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  ;.    if( pPager
117d1 2d 3e 78 52 65 69 6e 69 74 65 72 20 29 7b 0a 20  ->xReiniter ){. 
117d2 20 20 20 20 20 70 50 61 67 65 72 2d 3e 78 52 65       pPager->xRe
117d3 69 6e 69 74 65 72 28 70 50 67 29 3b 0a 20 20 20  initer(pPg);.   
117d4 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69   }.    if( isMai
117d5 6e 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61 76  nJrnl && (!isSav
117d6 65 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74  epnt || *pOffset
117d7 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  <=pPager->journa
117d8 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f  lHdr) ){.      /
117d9 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74  * If the content
117da 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 77  s of this page w
117db 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65  ere just restore
117dc 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20  d from the main 
117dd 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61  .      ** journa
117de 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73  l file, then its
117df 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65   content must be
117e0 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77 68   as they were wh
117e1 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  en the .      **
117e2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
117e3 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49   first opened. I
117e4 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 63  n this case we c
117e5 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65  an mark the page
117e6 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65  .      ** as cle
117e7 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20  an, since there 
117e8 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20  will be no need 
117e9 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 20  to write it out 
117ea 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a  to the..      **
117eb 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20  .      ** There 
117ec 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e  is one exception
117ed 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 49   to this rule. I
117ee 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 65  f the page is be
117ef 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20  ing rolled.     
117f0 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 74   ** back as part
117f1 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20   of a savepoint 
117f2 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 72  (or statement) r
117f3 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20  ollback from an 
117f4 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63  .      ** unsync
117f5 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68  ed portion of th
117f6 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66  e main journal f
117f7 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ile, then it is 
117f8 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 2a  not safe.      *
117f9 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 61  * to mark the pa
117fa 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 69  ge as clean. Thi
117fb 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 72  s is because mar
117fc 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 73  king the page as
117fd 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20  .      ** clean 
117fe 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 50  will clear the P
117ff 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66  GHDR_NEED_SYNC f
11800 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 70  lag. Since the p
11801 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20  age is.      ** 
11802 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a  already in the j
11803 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 63  ournal file (rec
11804 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e 70  orded in Pager.p
11805 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20  InJournal) and. 
11806 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44       ** the PGHD
11807 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
11808 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 20   is cleared, if 
11809 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74  the page is writ
1180a 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ten to.      ** 
1180b 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69  again within thi
1180c 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69  s transaction, i
1180d 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64  t will be marked
1180e 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 20   as dirty but.  
1180f 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52      ** the PGHDR
11810 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20  _NEED_SYNC flag 
11811 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e  will not be set.
11812 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 70   It could then p
11813 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20  otentially.     
11814 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 6f   ** be written o
11815 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ut into the data
11816 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65  base file before
11817 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   its journal fil
11818 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65  e.      ** segme
11819 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66  nt is synced. If
1181a 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 20   a crash occurs 
1181b 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77  during or follow
1181c 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 20  ing this,.      
1181d 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  ** database corr
1181e 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65  uption may ensue
1181f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
11820 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61   sqlite3PcacheMa
11821 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20  keClean(pPg);.  
11822 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
11823 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
11824 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20    pPg->pageHash 
11825 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  = pager_pagehash
11826 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20  (pPg);.#endif.  
11827 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73    /* If this was
11828 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 65   page 1, then re
11829 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20  store the value 
1182a 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56  of Pager.dbFileV
1182b 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74  ers..    ** Do t
1182c 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20 64  his before any d
1182d 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20  ecoding. */.    
1182e 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20  if( pgno==1 ){. 
1182f 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61       memcpy(&pPa
11830 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c  ger->dbFileVers,
11831 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b 32   &((u8*)pData)[2
11832 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 72  4],sizeof(pPager
11833 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a  ->dbFileVers));.
11834 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
11835 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a 75  code the page ju
11836 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73  st read from dis
11837 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 28  k */.    CODEC1(
11838 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70  pPager, pData, p
11839 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20 20  Pg->pgno, 3);.  
1183a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52    sqlite3PcacheR
1183b 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d  elease(pPg);.  }
1183c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1183d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
1183e 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64  EBUG) || defined
1183f 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45  (SQLITE_COVERAGE
11840 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69  _TEST)./*.** Thi
11841 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20  s routine looks 
11842 61 68 65 61 64 20 69 6e 74 6f 20 74 68 65 20 6d  ahead into the m
11843 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
11844 20 61 6e 64 20 64 65 74 65 72 6d 69 6e 65 73 0a   and determines.
11845 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ** whether or no
11846 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72  t the next recor
11847 64 20 28 74 68 65 20 72 65 63 6f 72 64 20 74 68  d (the record th
11848 61 74 20 62 65 67 69 6e 73 20 61 74 20 66 69 6c  at begins at fil
11849 65 0a 2a 2a 20 6f 66 66 73 65 74 20 70 50 61 67  e.** offset pPag
1184a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20  er->journalOff) 
1184b 69 73 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  is a well-formed
1184c 20 70 61 67 65 20 72 65 63 6f 72 64 20 63 6f 6e   page record con
1184d 73 69 73 74 69 6e 67 0a 2a 2a 20 6f 66 20 61 20  sisting.** of a 
1184e 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62 65  valid page numbe
1184f 72 2c 20 70 50 61 67 65 2d 3e 70 61 67 65 53 69  r, pPage->pageSi
11850 7a 65 20 62 79 74 65 73 20 6f 66 20 63 6f 6e 74  ze bytes of cont
11851 65 6e 74 2c 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a  ent, followed.**
11852 20 62 79 20 61 20 76 61 6c 69 64 20 63 68 65 63   by a valid chec
11853 6b 73 75 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ksum..**.** The 
11854 70 61 67 65 72 20 6e 65 76 65 72 20 6e 65 65 64  pager never need
11855 73 20 74 6f 20 6b 6e 6f 77 20 74 68 69 73 20 69  s to know this i
11856 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 20 69 74  n order to do it
11857 73 20 6a 6f 62 2e 20 20 20 54 68 69 73 0a 2a 2a  s job.   This.**
11858 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79   routine is only
11859 20 75 73 65 64 20 66 72 6f 6d 20 77 69 74 68 20   used from with 
1185a 61 73 73 65 72 74 28 29 20 61 6e 64 20 74 65 73  assert() and tes
1185b 74 63 61 73 65 28 29 20 6d 61 63 72 6f 73 2e 0a  tcase() macros..
1185c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
1185d 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50 61  gerNextJournalPa
1185e 67 65 49 73 56 61 6c 69 64 28 50 61 67 65 72 20  geIsValid(Pager 
1185f 2a 70 50 61 67 65 72 29 7b 0a 20 20 50 67 6e 6f  *pPager){.  Pgno
11860 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
11861 20 2f 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d   /* The page num
11862 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20  ber of the page 
11863 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20  */.  u32 cksum; 
11864 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
11865 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 2a   page checksum *
11866 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
11867 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
11868 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 72 65 61  rn code from rea
11869 64 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a  d operations */.
1186a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1186b 66 64 3b 20 20 20 20 2f 2a 20 54 68 65 20 66 69  fd;    /* The fi
1186c 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 72  le descriptor fr
1186d 6f 6d 20 77 68 69 63 68 20 77 65 20 61 72 65 20  om which we are 
1186e 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 75 38 20  reading */.  u8 
1186f 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  *aData;         
11870 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20    /* Content of 
11871 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20 2f  the page */..  /
11872 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 20  * Read the page 
11873 6e 75 6d 62 65 72 20 68 65 61 64 65 72 20 2a 2f  number header */
11874 0a 20 20 66 64 20 3d 20 70 50 61 67 65 72 2d 3e  .  fd = pPager->
11875 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64  jfd;.  rc = read
11876 33 32 62 69 74 73 28 66 64 2c 20 70 50 61 67 65  32bits(fd, pPage
11877 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 26  r->journalOff, &
11878 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21  pgno);.  if( rc!
11879 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72 65  =SQLITE_OK ){ re
1187a 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20  turn 0; }       
1187b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1187c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
1187d 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 6e  TEST*/.  if( pgn
1187e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41  o==0 || pgno==PA
1187f 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
11880 65 72 29 20 29 7b 20 72 65 74 75 72 6e 20 30 3b  er) ){ return 0;
11881 20 7d 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f   }         /*NO_
11882 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 6e  TEST*/.  if( pgn
11883 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d 3e  o>(Pgno)pPager->
11884 64 62 53 69 7a 65 20 29 7b 20 72 65 74 75 72 6e  dbSize ){ return
11885 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20   0; }           
11886 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
11887 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61  TEST*/..  /* Rea
11888 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 2a  d the checksum *
11889 2f 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62  /.  rc = read32b
1188a 69 74 73 28 66 64 2c 20 70 50 61 67 65 72 2d 3e  its(fd, pPager->
1188b 6a 6f 75 72 6e 61 6c 4f 66 66 2b 70 50 61 67 65  journalOff+pPage
1188c 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 2c 20 26  r->pageSize+4, &
1188d 63 6b 73 75 6d 29 3b 0a 20 20 69 66 28 20 72 63  cksum);.  if( rc
1188e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72  !=SQLITE_OK ){ r
1188f 65 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20  eturn 0; }      
11890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11891 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f              /*NO
11892 5f 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65  _TEST*/..  /* Re
11893 61 64 20 74 68 65 20 64 61 74 61 20 61 6e 64 20  ad the data and 
11894 76 65 72 69 66 79 20 74 68 65 20 63 68 65 63 6b  verify the check
11895 73 75 6d 20 2a 2f 0a 20 20 61 44 61 74 61 20 3d  sum */.  aData =
11896 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 54   (u8*)pPager->pT
11897 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20  mpSpace;.  rc = 
11898 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66 64  sqlite3OsRead(fd
11899 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d  , aData, pPager-
1189a 3e 70 61 67 65 53 69 7a 65 2c 20 70 50 61 67 65  >pageSize, pPage
1189b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 34 29  r->journalOff+4)
1189c 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
1189d 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e 20  TE_OK ){ return 
1189e 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  0; }            
1189f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118a0 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
118a1 2f 0a 20 20 69 66 28 20 70 61 67 65 72 5f 63 6b  /.  if( pager_ck
118a2 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 74  sum(pPager, aDat
118a3 61 29 21 3d 63 6b 73 75 6d 20 29 7b 20 72 65 74  a)!=cksum ){ ret
118a4 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20 20  urn 0; }        
118a5 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
118a6 2f 0a 0a 20 20 2f 2a 20 52 65 61 63 68 20 74 68  /..  /* Reach th
118a7 69 73 20 70 6f 69 6e 74 20 6f 6e 6c 79 20 69 66  is point only if
118a8 20 74 68 65 20 70 61 67 65 20 69 73 20 76 61 6c   the page is val
118a9 69 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31  id */.  return 1
118aa 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  ;.}.#endif /* !d
118ab 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c  efined(NDEBUG) |
118ac 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
118ad 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 20  _COVERAGE_TEST) 
118ae 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65  */../*.** Parame
118af 74 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74  ter zMaster is t
118b0 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73  he name of a mas
118b1 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
118b2 2e 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e  . A single journ
118b3 61 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20  al.** file that 
118b4 72 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20  referred to the 
118b5 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
118b6 69 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65  ile has just bee
118b7 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  n rolled back..*
118b8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
118b9 68 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70  hecks if it is p
118ba 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74  ossible to delet
118bb 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
118bc 72 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e  rnal file,.** an
118bd 64 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20  d does so if it 
118be 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65  is..**.** Argume
118bf 6e 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70  nt zMaster may p
118c0 6f 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54  oint to Pager.pT
118c1 6d 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74  mpSpace. So that
118c2 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a   buffer is not .
118c3 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ** available for
118c4 20 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73   use within this
118c5 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
118c6 20 57 68 65 6e 20 61 20 6d 61 73 74 65 72 20 6a   When a master j
118c7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63  ournal file is c
118c8 72 65 61 74 65 64 2c 20 69 74 20 69 73 20 70 6f  reated, it is po
118c9 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 65  pulated with the
118ca 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 61 6c   names .** of al
118cb 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 6a  l of its child j
118cc 6f 75 72 6e 61 6c 73 2c 20 6f 6e 65 20 61 66 74  ournals, one aft
118cd 65 72 20 61 6e 6f 74 68 65 72 2c 20 66 6f 72 6d  er another, form
118ce 61 74 74 65 64 20 61 73 20 75 74 66 2d 38 20 0a  atted as utf-8 .
118cf 2a 2a 20 65 6e 63 6f 64 65 64 20 74 65 78 74 2e  ** encoded text.
118d0 20 54 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68   The end of each
118d1 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 66   child journal f
118d2 69 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 77 69  ile is marked wi
118d3 74 68 20 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65 72  th a .** nul-ter
118d4 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 28 30 78  minator byte (0x
118d5 30 30 29 2e 20 69 2e 65 2e 20 74 68 65 20 65 6e  00). i.e. the en
118d6 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66  tire contents of
118d7 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
118d8 6c 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 61 20  l.** file for a 
118d9 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 76 6f  transaction invo
118da 6c 76 69 6e 67 20 74 77 6f 20 64 61 74 61 62 61  lving two databa
118db 73 65 73 20 6d 69 67 68 74 20 62 65 3a 0a 2a 2a  ses might be:.**
118dc 0a 2a 2a 20 20 20 22 2f 68 6f 6d 65 2f 62 69 6c  .**   "/home/bil
118dd 6c 2f 61 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78  l/a.db-journal\x
118de 30 30 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e 64  00/home/bill/b.d
118df 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 22 0a 2a  b-journal\x00".*
118e0 2a 0a 2a 2a 20 41 20 6d 61 73 74 65 72 20 6a 6f  *.** A master jo
118e1 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f  urnal file may o
118e2 6e 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 6f  nly be deleted o
118e3 6e 63 65 20 61 6c 6c 20 6f 66 20 69 74 73 20 63  nce all of its c
118e4 68 69 6c 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  hild .** journal
118e5 73 20 68 61 76 65 20 62 65 65 6e 20 72 6f 6c 6c  s have been roll
118e6 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  ed back..**.** T
118e7 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 61  his function rea
118e8 64 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ds the contents 
118e9 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f  of the master-jo
118ea 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20  urnal file into 
118eb 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20 6c  .** memory and l
118ec 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63  oops through eac
118ed 68 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6a  h of the child j
118ee 6f 75 72 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 6f  ournal names. Fo
118ef 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20  r.** each child 
118f0 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 63 68 65 63  journal, it chec
118f1 6b 73 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  ks if:.**.**   *
118f2 20 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f   if the child jo
118f3 75 72 6e 61 6c 20 65 78 69 73 74 73 2c 20 61 6e  urnal exists, an
118f4 64 20 69 66 20 73 6f 0a 2a 2a 20 20 20 2a 20 69  d if so.**   * i
118f5 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72  f the child jour
118f6 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 72  nal contains a r
118f7 65 66 65 72 65 6e 63 65 20 74 6f 20 6d 61 73 74  eference to mast
118f8 65 72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20  er journal .**  
118f9 20 20 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 0a     file zMaster.
118fa 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 68 69 6c 64  **.** If a child
118fb 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20   journal can be 
118fc 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 74 63 68  found that match
118fd 65 73 20 62 6f 74 68 20 6f 66 20 74 68 65 20 63  es both of the c
118fe 72 69 74 65 72 69 61 0a 2a 2a 20 61 62 6f 76 65  riteria.** above
118ff 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
11900 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20  returns without 
11901 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20  doing anything. 
11902 4f 74 68 65 72 77 69 73 65 2c 20 69 66 0a 2a 2a  Otherwise, if.**
11903 20 6e 6f 20 73 75 63 68 20 63 68 69 6c 64 20 6a   no such child j
11904 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f  ournal can be fo
11905 75 6e 64 2c 20 66 69 6c 65 20 7a 4d 61 73 74 65  und, file zMaste
11906 72 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f  r is deleted fro
11907 6d 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79  m.** the file-sy
11908 73 74 65 6d 20 75 73 69 6e 67 20 73 71 6c 69 74  stem using sqlit
11909 65 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a  e3OsDelete()..**
1190a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72  .** If an IO err
1190b 6f 72 20 77 69 74 68 69 6e 20 74 68 69 73 20 66  or within this f
1190c 75 6e 63 74 69 6f 6e 2c 20 61 6e 20 65 72 72 6f  unction, an erro
1190d 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
1190e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63  ed. This.** func
1190f 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 6d  tion allocates m
11910 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67  emory by calling
11911 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29   sqlite3Malloc()
11912 2e 20 49 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69  . If an allocati
11913 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c  on.** fails, SQL
11914 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74  ITE_NOMEM is ret
11915 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
11916 2c 20 69 66 20 6e 6f 20 49 4f 20 6f 72 20 6d 61  , if no IO or ma
11917 6c 6c 6f 63 20 65 72 72 6f 72 73 20 0a 2a 2a 20  lloc errors .** 
11918 6f 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b  occur, SQLITE_OK
11919 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
1191a 0a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 66  .** TODO: This f
1191b 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65  unction allocate
1191c 73 20 61 20 73 69 6e 67 6c 65 20 62 6c 6f 63 6b  s a single block
1191d 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6c 6f   of memory to lo
1191e 61 64 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65  ad.** the entire
1191f 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
11920 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11921 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 75 6c 64  file. This could
11922 20 62 65 0a 2a 2a 20 61 20 63 6f 75 70 6c 65 20   be.** a couple 
11923 6f 66 20 6b 69 6c 6f 62 79 74 65 73 20 6f 72 20  of kilobytes or 
11924 73 6f 20 2d 20 70 6f 74 65 6e 74 69 61 6c 6c 79  so - potentially
11925 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
11926 20 70 61 67 65 20 0a 2a 2a 20 73 69 7a 65 2e 0a   page .** size..
11927 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
11928 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 61  ger_delmaster(Pa
11929 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e  ger *pPager, con
1192a 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
1192b 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  ){.  sqlite3_vfs
1192c 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d   *pVfs = pPager-
1192d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b  >pVfs;.  int rc;
1192e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1192f 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
11930 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  e */.  sqlite3_f
11931 69 6c 65 20 2a 70 4d 61 73 74 65 72 3b 20 20 20  ile *pMaster;   
11932 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 6d 61 73   /* Malloc'd mas
11933 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter-journal file
11934 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
11935 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
11936 4a 6f 75 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d 61  Journal;   /* Ma
11937 6c 6c 6f 63 27 64 20 63 68 69 6c 64 2d 6a 6f 75  lloc'd child-jou
11938 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69  rnal file descri
11939 70 74 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ptor */.  char *
1193a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d  zMasterJournal =
1193b 20 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 20   0; /* Contents 
1193c 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  of master journa
1193d 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20  l file */.  i64 
1193e 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 20  nMasterJournal; 
1193f 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
11940 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11941 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c  file */..  /* Al
11942 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
11943 20 62 6f 74 68 20 74 68 65 20 70 4a 6f 75 72 6e   both the pJourn
11944 61 6c 20 61 6e 64 20 70 4d 61 73 74 65 72 20 66  al and pMaster f
11945 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2e  ile descriptors.
11946 0a 20 20 2a 2a 20 49 66 20 73 75 63 63 65 73 73  .  ** If success
11947 66 75 6c 2c 20 6f 70 65 6e 20 74 68 65 20 6d 61  ful, open the ma
11948 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
11949 65 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 20  e for reading.. 
1194a 20 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20   */.  pMaster = 
1194b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
1194c 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
1194d 6f 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  o(pVfs->szOsFile
1194e 20 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e 61   * 2);.  pJourna
1194f 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c  l = (sqlite3_fil
11950 65 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61 73  e *)(((u8 *)pMas
11951 74 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f  ter) + pVfs->szO
11952 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70  sFile);.  if( !p
11953 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72 63  Master ){.    rc
11954 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
11955 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f  .  }else{.    co
11956 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20  nst int flags = 
11957 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  (SQLITE_OPEN_REA
11958 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45  DONLY|SQLITE_OPE
11959 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c  N_MASTER_JOURNAL
1195a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1195b 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
1195c 7a 4d 61 73 74 65 72 2c 20 70 4d 61 73 74 65 72  zMaster, pMaster
1195d 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d  , flags, 0);.  }
1195e 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1195f 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d  E_OK ) goto delm
11960 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 72 63  aster_out;..  rc
11961 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
11962 53 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e  Size(pMaster, &n
11963 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a  MasterJournal);.
11964 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
11965 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61  _OK ) goto delma
11966 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28  ster_out;..  if(
11967 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e   nMasterJournal>
11968 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  0 ){.    char *z
11969 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61  Journal;.    cha
1196a 72 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20  r *zMasterPtr = 
1196b 30 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74  0;.    int nMast
1196c 65 72 50 74 72 20 3d 20 70 56 66 73 2d 3e 6d 78  erPtr = pVfs->mx
1196d 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20  Pathname+1;..   
1196e 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74   /* Load the ent
1196f 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  ire master journ
11970 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61  al file into spa
11971 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ce obtained from
11972 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  .    ** sqlite3_
11973 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69  malloc() and poi
11974 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74  nted to by zMast
11975 65 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20  erJournal. .    
11976 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f  */.    zMasterJo
11977 75 72 6e 61 6c 20 3d 20 28 63 68 61 72 20 2a 29  urnal = (char *)
11978 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69  sqlite3Malloc((i
11979 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61  nt)nMasterJourna
1197a 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 72 29 3b  l + nMasterPtr);
1197b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65  .    if( !zMaste
1197c 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  rJournal ){.    
1197d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
1197e 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  MEM;.      goto 
1197f 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20  delmaster_out;. 
11980 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 65 72     }.    zMaster
11981 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 4a 6f  Ptr = &zMasterJo
11982 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75  urnal[nMasterJou
11983 72 6e 61 6c 5d 3b 0a 20 20 20 20 72 63 20 3d 20  rnal];.    rc = 
11984 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4d  sqlite3OsRead(pM
11985 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 72 4a 6f  aster, zMasterJo
11986 75 72 6e 61 6c 2c 20 28 69 6e 74 29 6e 4d 61 73  urnal, (int)nMas
11987 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a  terJournal, 0);.
11988 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
11989 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c  TE_OK ) goto del
1198a 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 20  master_out;..   
1198b 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61 73   zJournal = zMas
1198c 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20  terJournal;.    
1198d 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61 6c  while( (zJournal
1198e 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29  -zMasterJournal)
1198f 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20  <nMasterJournal 
11990 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78 69  ){.      int exi
11991 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  sts;.      rc = 
11992 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
11993 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20  pVfs, zJournal, 
11994 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
11995 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a  ISTS, &exists);.
11996 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
11997 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
11998 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65     goto delmaste
11999 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  r_out;.      }. 
1199a 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73 20       if( exists 
1199b 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e  ){.        /* On
1199c 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
1199d 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
1199e 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1199f 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20 20  al exists..     
119a0 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61 6e     ** Open it an
119a1 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70 6f  d check if it po
119a2 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73 74  ints at the mast
119a3 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20  er journal. If. 
119a4 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72 65         ** so, re
119a5 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65 6c  turn without del
119a6 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72  eting the master
119a7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20   journal file.. 
119a8 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
119a9 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20    int c;.       
119aa 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51   int flags = (SQ
119ab 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
119ac 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  LY|SQLITE_OPEN_M
119ad 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20  AIN_JOURNAL);.  
119ae 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
119af 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a  e3OsOpen(pVfs, z
119b0 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e 61  Journal, pJourna
119b1 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20  l, flags, 0);.  
119b2 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
119b3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
119b4 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73       goto delmas
119b5 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  ter_out;.       
119b6 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20 3d   }..        rc =
119b7 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e   readMasterJourn
119b8 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d 61  al(pJournal, zMa
119b9 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65 72  sterPtr, nMaster
119ba 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71  Ptr);.        sq
119bb 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a 6f  lite3OsClose(pJo
119bc 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20 20  urnal);.        
119bd 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
119be 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67  K ){.          g
119bf 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75  oto delmaster_ou
119c0 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  t;.        }..  
119c1 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74 65        c = zMaste
119c2 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73 74  rPtr[0]!=0 && st
119c3 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72 2c  rcmp(zMasterPtr,
119c4 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20 20   zMaster)==0;.  
119c5 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a 20        if( c ){. 
119c6 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 68           /* We h
119c7 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f 20  ave a match. Do 
119c8 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 6d  not delete the m
119c9 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
119ca 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  le. */.         
119cb 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
119cc 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  out;.        }. 
119cd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a 6f       }.      zJo
119ce 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74 65  urnal += (sqlite
119cf 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72 6e  3Strlen30(zJourn
119d0 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20  al)+1);.    }.  
119d1 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c 69  }.  .  rc = sqli
119d2 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73  te3OsDelete(pVfs
119d3 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 0a  , zMaster, 0);..
119d4 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a 20  delmaster_out:. 
119d5 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75 72   if( zMasterJour
119d6 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  nal ){.    sqlit
119d7 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 4a  e3_free(zMasterJ
119d8 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a 20  ournal);.  }  . 
119d9 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b 0a   if( pMaster ){.
119da 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
119db 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20  se(pMaster);.   
119dc 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e   assert( !isOpen
119dd 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20 20  (pJournal) );.  
119de 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
119df 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74  (pMaster);.  ret
119e0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
119e1 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
119e2 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e 67  is used to chang
119e3 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69 7a  e the actual siz
119e4 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
119e5 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74 68  e .** file in th
119e6 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20 54  e file-system. T
119e7 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73  his only happens
119e8 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67   when committing
119e9 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a   a transaction,.
119ea 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61  ** or rolling ba
119eb 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ck a transaction
119ec 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c 6c   (including roll
119ed 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d 6a  ing back a hot-j
119ee 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49  ournal)..**.** I
119ef 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
119f0 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20  ase file is not 
119f1 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63 6c  open, or an excl
119f2 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e 6f  usive lock is no
119f3 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73 20  t.** held, this 
119f4 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
119f5 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  -op. Otherwise, 
119f6 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
119f7 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e 67  file is.** chang
119f8 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65  ed to nPage page
119f9 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72 2d  s (nPage*pPager-
119fa 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 29  >pageSize bytes)
119fb 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a 2a  . If the file.**
119fc 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72 72   on disk is curr
119fd 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 61  ently larger tha
119fe 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20 74  n nPage pages, t
119ff 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53 0a  hen use the VFS.
11a00 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20 6d  ** xTruncate() m
11a01 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61 74  ethod to truncat
11a02 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20  e it..**.** Or, 
11a03 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20 62  it might might b
11a04 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20  e the case that 
11a05 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b  the file on disk
11a06 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e   is smaller than
11a07 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65 73   .** nPage pages
11a08 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67  . Some operating
11a09 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65 6e   system implemen
11a0a 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74 20  tations can get 
11a0b 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a 20  confused if .** 
11a0c 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e 63  you try to trunc
11a0d 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73 6f  ate a file to so
11a0e 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73 20  me size that is 
11a0f 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20 0a  larger than it .
11a10 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73 2c  ** currently is,
11a11 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73 20   so detect this 
11a12 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20 61  case and write a
11a13 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79 74   single zero byt
11a14 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e 64  e to .** the end
11a15 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c 65   of the new file
11a16 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20   instead..**.** 
11a17 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  If successful, r
11a18 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
11a19 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
11a1a 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f 64  occurs while mod
11a1b 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64 61  ifying.** the da
11a1c 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65 74  tabase file, ret
11a1d 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f  urn the error co
11a1e 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  de to the caller
11a1f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11a20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 50  pager_truncate(P
11a21 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67  ager *pPager, Pg
11a22 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74  no nPage){.  int
11a23 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
11a24 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
11a25 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c  tate>=PAGER_EXCL
11a26 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e 28  USIVE && isOpen(
11a27 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20  pPager->fd) ){. 
11a28 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53 69     i64 currentSi
11a29 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20 20  ze, newSize;.   
11a2a 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74 20   /* TODO: Is it 
11a2b 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67 65  safe to use Page
11a2c 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65 72  r.dbFileSize her
11a2d 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  e? */.    rc = s
11a2e 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
11a2f 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63 75  (pPager->fd, &cu
11a30 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20 20  rrentSize);.    
11a31 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65 72  newSize = pPager
11a32 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34 29  ->pageSize*(i64)
11a33 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20 72  nPage;.    if( r
11a34 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
11a35 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65 77  currentSize!=new
11a36 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69 66  Size ){.      if
11a37 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e 65  ( currentSize>ne
11a38 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  wSize ){.       
11a39 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
11a3a 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
11a3b 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20 20  fd, newSize);.  
11a3c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
11a3d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
11a3e 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66  sWrite(pPager->f
11a3f 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69 7a  d, "", 1, newSiz
11a40 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  e-1);.      }.  
11a41 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
11a42 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
11a43 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53   pPager->dbFileS
11a44 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20  ize = nPage;.   
11a45 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
11a46 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
11a47 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c  *.** Set the val
11a48 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72 2e  ue of the Pager.
11a49 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69 61  sectorSize varia
11a4a 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76 65  ble for the give
11a4b 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65 64  n.** pager based
11a4c 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65   on the value re
11a4d 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78 53  turned by the xS
11a4e 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f 64  ectorSize method
11a4f 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e 20  .** of the open 
11a50 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54  database file. T
11a51 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77  he sector size w
11a52 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65 64  ill be used used
11a53 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e   .** to determin
11a54 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20 61  e the size and a
11a55 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75 72  lignment of jour
11a56 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20 0a  nal header and .
11a57 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  ** master journa
11a58 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68 69  l pointers withi
11a59 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e 61  n created journa
11a5a 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46  l files..**.** F
11a5b 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  or temporary fil
11a5c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76 65  es the effective
11a5d 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20   sector size is 
11a5e 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 73  always 512 bytes
11a5f 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
11a60 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70 6f  e, for non-tempo
11a61 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65 20  rary files, the 
11a62 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f 72  effective sector
11a63 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65 20   size is.** the 
11a64 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
11a65 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69 7a  y the xSectorSiz
11a66 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e 64  e() method round
11a67 65 64 20 75 70 20 74 6f 20 35 31 32 20 69 66 0a  ed up to 512 if.
11a68 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74 68  ** it is less th
11a69 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f 75 6e 64  an 512, or round
11a6a 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f 53  ed down to MAX_S
11a6b 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69 74  ECTOR_SIZE if it
11a6c 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74  .** is greater t
11a6d 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53  han MAX_SECTOR_S
11a6e 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  IZE..*/.static v
11a6f 6f 69 64 20 73 65 74 53 65 63 74 6f 72 53 69 7a  oid setSectorSiz
11a70 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  e(Pager *pPager)
11a71 7b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  {.  assert( isOp
11a72 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c  en(pPager->fd) |
11a73 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  | pPager->tempFi
11a74 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 50  le );..  if( !pP
11a75 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
11a76 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20  {.    /* Sector 
11a77 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 74  size doesn't mat
11a78 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 72  ter for temporar
11a79 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 74  y files. Also, t
11a7a 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 6d  he file.    ** m
11a7b 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e  ay not have been
11a7c 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e 20   opened yet, in 
11a7d 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 4f  which case the O
11a7e 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 20  sSectorSize().  
11a7f 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 73    ** call will s
11a80 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a  egfault..    */.
11a81 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74      pPager->sect
11a82 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33  orSize = sqlite3
11a83 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61  OsSectorSize(pPa
11a84 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20 20  ger->fd);.  }.  
11a85 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74  if( pPager->sect
11a86 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20 20  orSize<512 ){.  
11a87 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72    pPager->sector
11a88 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d 0a  Size = 512;.  }.
11a89 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65    if( pPager->se
11a8a 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43  ctorSize>MAX_SEC
11a8b 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20  TOR_SIZE ){.    
11a8c 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 43 54  assert( MAX_SECT
11a8d 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b 0a  OR_SIZE>=512 );.
11a8e 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74      pPager->sect
11a8f 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45 43  orSize = MAX_SEC
11a90 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d 0a  TOR_SIZE;.  }.}.
11a91 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20  ./*.** Playback 
11a92 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  the journal and 
11a93 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68 65  thus restore the
11a94 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
11a95 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 69  o.** the state i
11a96 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65 20  t was in before 
11a97 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69 6e  we started makin
11a98 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a 0a  g changes.  .**.
11a99 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ** The journal f
11a9a 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 61 73  ile format is as
11a9b 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a 2a   follows: .**.**
11a9c 20 20 28 31 29 20 20 38 20 62 79 74 65 20 70 72    (1)  8 byte pr
11a9d 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 6f 66  efix.  A copy of
11a9e 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d   aJournalMagic[]
11a9f 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 79 74  ..**  (2)  4 byt
11aa0 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74  e big-endian int
11aa1 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
11aa2 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 69  e number of vali
11aa3 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 0a 2a  d page records.*
11aa4 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 20 6a  *       in the j
11aa5 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 69 73  ournal.  If this
11aa6 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 66 66   value is 0xffff
11aa7 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70 75  ffff, then compu
11aa8 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  te the.**       
11aa9 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72  number of page r
11aaa 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20  ecords from the 
11aab 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a 2a  journal size..**
11aac 20 20 28 33 29 20 20 34 20 62 79 74 65 20 62 69    (3)  4 byte bi
11aad 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
11aae 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 6e   which is the in
11aaf 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f 72 20  itial value for 
11ab0 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 73 61  the .**       sa
11ab1 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a 2a  nity checksum..*
11ab2 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 20 69  *  (4)  4 byte i
11ab3 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20  nteger which is 
11ab4 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
11ab5 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 65 20  ges to truncate 
11ab6 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 61 74  the.**       dat
11ab7 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e 67 20  abase to during 
11ab8 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 20  a rollback..**  
11ab9 28 35 29 20 20 34 20 62 79 74 65 20 62 69 67 2d  (5)  4 byte big-
11aba 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77  endian integer w
11abb 68 69 63 68 20 69 73 20 74 68 65 20 73 65 63 74  hich is the sect
11abc 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 68 65  or size.  The he
11abd 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 69 73  ader.**       is
11abe 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73   this many bytes
11abf 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 36   in size..**  (6
11ac0 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e  )  4 byte big-en
11ac1 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  dian integer whi
11ac2 63 68 20 69 73 20 74 68 65 20 70 61 67 65 20 63  ch is the page c
11ac3 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 20 34 20  ase..**  (7)  4 
11ac4 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 69  byte integer whi
11ac5 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ch is the number
11ac6 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
11ac7 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a   master journal.
11ac8 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 2e 20 20  **       name.  
11ac9 54 68 65 20 76 61 6c 75 65 20 6d 61 79 20 62 65  The value may be
11aca 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 74 65 20   zero (indicate 
11acb 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f  that there is no
11acc 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 20   master.**      
11acd 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a 20 20 28   journal.).**  (
11ace 38 29 20 20 4e 20 62 79 74 65 73 20 6f 66 20 74  8)  N bytes of t
11acf 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
11ad0 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 6e 61 6d  l name.  The nam
11ad1 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c 2d 74 65  e will be nul-te
11ad2 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20  rminated.**     
11ad3 20 20 61 6e 64 20 6d 69 67 68 74 20 62 65 20 73    and might be s
11ad4 68 6f 72 74 65 72 20 74 68 61 6e 20 74 68 65 20  horter than the 
11ad5 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20  value read from 
11ad6 28 35 29 2e 20 20 49 66 20 74 68 65 20 66 69 72  (5).  If the fir
11ad7 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 20  st byte.**      
11ad8 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20   of the name is 
11ad9 5c 30 30 30 20 74 68 65 6e 20 74 68 65 72 65 20  \000 then there 
11ada 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  is no master jou
11adb 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 73 74 65  rnal.  The maste
11adc 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72 6e  r.**       journ
11add 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 6f 72 65  al name is store
11ade 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 20 20  d in UTF-8..**  
11adf 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f 72  (9)  Zero or mor
11ae0 65 20 70 61 67 65 73 20 69 6e 73 74 61 6e 63 65  e pages instance
11ae1 73 2c 20 65 61 63 68 20 61 73 20 66 6f 6c 6c 6f  s, each as follo
11ae2 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20  ws:.**        + 
11ae3 20 34 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d   4 byte page num
11ae4 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b  ber..**        +
11ae5 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69    pPager->pageSi
11ae6 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  ze bytes of data
11ae7 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 34  ..**        +  4
11ae8 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0a 2a   byte checksum.*
11ae9 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 70 65  *.** When we spe
11aea 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ak of the journa
11aeb 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d 65 61  l header, we mea
11aec 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 69 74  n the first 8 it
11aed 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45 61  ems above..** Ea
11aee 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  ch entry in the 
11aef 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 69 6e  journal is an in
11af0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 39 74  stance of the 9t
11af1 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43 61  h item..**.** Ca
11af2 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 66 72 6f  ll the value fro
11af3 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75 6c  m the second bul
11af4 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 6e 52 65  let "nRec".  nRe
11af5 63 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  c is the number 
11af6 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67 65  of.** valid page
11af7 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
11af8 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f 73  journal.  In mos
11af9 74 20 63 61 73 65 73 2c 20 79 6f 75 20 63 61 6e  t cases, you can
11afa 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20   compute the.** 
11afb 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 66 72  value of nRec fr
11afc 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  om the size of t
11afd 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
11afe 20 20 42 75 74 20 69 66 20 61 20 70 6f 77 65 72    But if a power
11aff 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63 75  .** failure occu
11b00 72 72 65 64 20 77 68 69 6c 65 20 74 68 65 20 6a  rred while the j
11b01 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67  ournal was being
11b02 20 77 72 69 74 74 65 6e 2c 20 69 74 20 63 6f 75   written, it cou
11b03 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 73  ld be the.** cas
11b04 65 20 74 68 61 74 20 74 68 65 20 73 69 7a 65 20  e that the size 
11b05 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
11b06 69 6c 65 20 68 61 64 20 61 6c 72 65 61 64 79 20  ile had already 
11b07 62 65 65 6e 20 69 6e 63 72 65 61 73 65 64 20 62  been increased b
11b08 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 61 20  ut.** the extra 
11b09 65 6e 74 72 69 65 73 20 68 61 64 20 6e 6f 74 20  entries had not 
11b0a 79 65 74 20 6d 61 64 65 20 69 74 20 73 61 66 65  yet made it safe
11b0b 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e 20  ly to disk.  In 
11b0c 73 75 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a 20  such a case,.** 
11b0d 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65  the value of nRe
11b0e 63 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d 20  c computed from 
11b0f 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 77 6f  the file size wo
11b10 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 67 65  uld be too large
11b11 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20 72  .  For.** that r
11b12 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79 73  eason, we always
11b13 20 75 73 65 20 74 68 65 20 6e 52 65 63 20 76 61   use the nRec va
11b14 6c 75 65 20 69 6e 20 74 68 65 20 68 65 61 64 65  lue in the heade
11b15 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
11b16 6e 52 65 63 20 76 61 6c 75 65 20 69 73 20 30 78  nRec value is 0x
11b17 66 66 66 66 66 66 66 66 20 69 74 20 6d 65 61 6e  ffffffff it mean
11b18 73 20 74 68 61 74 20 6e 52 65 63 20 73 68 6f 75  s that nRec shou
11b19 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 0a 2a  ld be computed.*
11b1a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20  * from the file 
11b1b 73 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c 75  size.  This valu
11b1c 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 74  e is used when t
11b1d 68 65 20 75 73 65 72 20 73 65 6c 65 63 74 73 20  he user selects 
11b1e 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20 6f  the.** no-sync o
11b1f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a 6f  ption for the jo
11b20 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72 20  urnal.  A power 
11b21 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65  failure could le
11b22 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f 6e  ad to corruption
11b23 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65  .** in this case
11b24 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e 67  .  But for thing
11b25 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72 79  s like temporary
11b26 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 77 69   table (which wi
11b27 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 64  ll be.** deleted
11b28 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65 72 20   when the power 
11b29 69 73 20 72 65 73 74 6f 72 65 64 29 20 77 65 20  is restored) we 
11b2a 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a 2a  don't care.  .**
11b2b 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20  .** If the file 
11b2c 6f 70 65 6e 65 64 20 61 73 20 74 68 65 20 6a 6f  opened as the jo
11b2d 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f  urnal file is no
11b2e 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a  t a well-formed.
11b2f 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ** journal file 
11b30 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20 75  then all pages u
11b31 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 63  p to the first c
11b32 6f 72 72 75 70 74 65 64 20 70 61 67 65 20 61 72  orrupted page ar
11b33 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b  e rolled.** back
11b34 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 69 66   (or no pages if
11b35 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
11b36 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65 64  der is corrupted
11b37 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ). The journal f
11b38 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20 64  ile.** is then d
11b39 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c 49 54  eleted and SQLIT
11b3a 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20 6a  E_OK returned, j
11b3b 75 73 74 20 61 73 20 69 66 20 6e 6f 20 63 6f 72  ust as if no cor
11b3c 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20 62  ruption had.** b
11b3d 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e  een encountered.
11b3e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f 4f  .**.** If an I/O
11b3f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72   or malloc() err
11b40 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 6a  or occurs, the j
11b41 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e  ournal-file is n
11b42 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 6e  ot deleted.** an
11b43 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  d an error code 
11b44 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
11b45 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 70 61 72  ** The isHot par
11b46 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65 73  ameter indicates
11b47 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72 79   that we are try
11b48 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20  ing to rollback 
11b49 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68 61  a journal.** tha
11b4a 74 20 6d 69 67 68 74 20 62 65 20 61 20 68 6f 74  t might be a hot
11b4b 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20 69   journal.  Or, i
11b4c 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20  t could be that 
11b4d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 0a  the journal is .
11b4e 2a 2a 20 70 72 65 73 65 72 76 65 64 20 62 65 63  ** preserved bec
11b4f 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c 4d  ause of JOURNALM
11b50 4f 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20 4a  ODE_PERSIST or J
11b51 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43  OURNALMODE_TRUNC
11b52 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6a  ATE..** If the j
11b53 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69 73  ournal really is
11b54 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65 20   hot, reset the 
11b55 70 61 67 65 72 20 63 61 63 68 65 20 70 72 69 6f  pager cache prio
11b56 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61 63  r rolling.** bac
11b57 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20 20  k any content.  
11b58 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  If the journal i
11b59 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73 74  s merely persist
11b5a 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69 73  ent, no reset is
11b5b 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73  .** needed..*/.s
11b5c 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f  tatic int pager_
11b5d 70 6c 61 79 62 61 63 6b 28 50 61 67 65 72 20 2a  playback(Pager *
11b5e 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 48 6f  pPager, int isHo
11b5f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  t){.  sqlite3_vf
11b60 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72  s *pVfs = pPager
11b61 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 73 7a  ->pVfs;.  i64 sz
11b62 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  J;              
11b63 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
11b64 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
11b65 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 32  n bytes */.  u32
11b66 20 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 20   nRec;          
11b67 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
11b68 6f 66 20 52 65 63 6f 72 64 73 20 69 6e 20 74 68  of Records in th
11b69 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75  e journal */.  u
11b6a 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 20  32 u;           
11b6b 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67          /* Unsig
11b6c 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72  ned loop counter
11b6d 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 20   */.  Pgno mxPg 
11b6e 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f  = 0;           /
11b6f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 72  * Size of the or
11b70 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20 70  iginal file in p
11b71 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ages */.  int rc
11b72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11b73 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
11b74 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e  e of a subroutin
11b75 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 3d  e */.  int res =
11b76 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   1;             
11b77 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65  /* Value returne
11b78 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 41 63  d by sqlite3OsAc
11b79 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 61 72  cess() */.  char
11b7a 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20 20   *zMaster = 0;  
11b7b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
11b7c 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
11b7d 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20  ile if any */.  
11b7e 69 6e 74 20 6e 65 65 64 50 61 67 65 72 52 65 73  int needPagerRes
11b7f 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  et;      /* True
11b80 20 74 6f 20 72 65 73 65 74 20 70 61 67 65 20 70   to reset page p
11b81 72 69 6f 72 20 74 6f 20 66 69 72 73 74 20 70 61  rior to first pa
11b82 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a  ge rollback */..
11b83 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
11b84 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64 73  how many records
11b85 20 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 72   are in the jour
11b86 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72 6c  nal.  Abort earl
11b87 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a 6f  y if.  ** the jo
11b88 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e 0a  urnal is empty..
11b89 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69    */.  assert( i
11b8a 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
11b8b 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  d) );.  rc = sql
11b8c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70  ite3OsFileSize(p
11b8d 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 4a  Pager->jfd, &szJ
11b8e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
11b8f 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d 30  ITE_OK || szJ==0
11b90 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64   ){.    goto end
11b91 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 0a  _playback;.  }..
11b92 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6d 61    /* Read the ma
11b93 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
11b94 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e  e from the journ
11b95 61 6c 2c 20 69 66 20 69 74 20 69 73 20 70 72 65  al, if it is pre
11b96 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61 20  sent..  ** If a 
11b97 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
11b98 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 65 63  ile name is spec
11b99 69 66 69 65 64 2c 20 62 75 74 20 74 68 65 20 66  ified, but the f
11b9a 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20  ile is not.  ** 
11b9b 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b 2c  present on disk,
11b9c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   then the journa
11b9d 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e 64  l is not hot and
11b9e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
11b9f 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65 64  o be.  ** played
11ba0 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   back..  **.  **
11ba1 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61 6c   TODO: Technical
11ba2 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ly the following
11ba3 20 69 73 20 61 6e 20 65 72 72 6f 72 20 62 65 63   is an error bec
11ba4 61 75 73 65 20 69 74 20 61 73 73 75 6d 65 73 20  ause it assumes 
11ba5 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 66 65 72  that.  ** buffer
11ba6 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65   Pager.pTmpSpace
11ba7 20 69 73 20 28 6d 78 50 61 74 68 6e 61 6d 65 2b   is (mxPathname+
11ba8 31 29 20 62 79 74 65 73 20 6f 72 20 6c 61 72 67  1) bytes or larg
11ba9 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20 20  er. i.e. that.  
11baa 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 61 67 65  ** (pPager->page
11bab 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d 3e  Size >= pPager->
11bac 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
11bad 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75 6e  +1). Using os_un
11bae 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50 61  ix.c,.  **  mxPa
11baf 74 68 6e 61 6d 65 20 69 73 20 35 31 32 2c 20 77  thname is 512, w
11bb0 68 69 63 68 20 69 73 20 74 68 65 20 73 61 6d 65  hich is the same
11bb1 20 61 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20   as the minimum 
11bb2 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65 0a  allowable value.
11bb3 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 53 69 7a    ** for pageSiz
11bb4 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74 65  e..  */.  zMaste
11bb5 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70  r = pPager->pTmp
11bb6 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 72 65  Space;.  rc = re
11bb7 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28  adMasterJournal(
11bb8 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61  pPager->jfd, zMa
11bb9 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 56  ster, pPager->pV
11bba 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  fs->mxPathname+1
11bbb 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
11bbc 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65  ITE_OK && zMaste
11bbd 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d  r[0] ){.    rc =
11bbe 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
11bbf 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20  (pVfs, zMaster, 
11bc0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
11bc1 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 7d  ISTS, &res);.  }
11bc2 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a  .  zMaster = 0;.
11bc3 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
11bc4 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a 20  _OK || !res ){. 
11bc5 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79     goto end_play
11bc6 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61 67  back;.  }.  pPag
11bc7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
11bc8 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 65 72 52   0;.  needPagerR
11bc9 65 73 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a 20  eset = isHot;.. 
11bca 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74 65   /* This loop te
11bcb 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72 20  rminates either 
11bcc 77 68 65 6e 20 61 20 72 65 61 64 4a 6f 75 72 6e  when a readJourn
11bcd 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 20 2a 2a  alHdr() or .  **
11bce 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
11bcf 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 6c 6c 20  one_page() call 
11bd0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44  returns SQLITE_D
11bd1 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72 72  ONE or an IO err
11bd2 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73 2e  or .  ** occurs.
11bd3 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20   .  */.  while( 
11bd4 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 55  1 ){.    int isU
11bd5 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20 20  nsync = 0;..    
11bd6 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78 74  /* Read the next
11bd7 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
11bd8 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c  from the journal
11bd9 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65   file.  If there
11bda 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 20   are.    ** not 
11bdb 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c 65 66  enough bytes lef
11bdc 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t in the journal
11bdd 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d 70   file for a comp
11bde 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f 72 0a  lete header, or.
11bdf 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 6f 72      ** it is cor
11be0 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 20 70  rupted, then a p
11be1 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 20 66  rocess must of f
11be2 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 69 74  ailed while writ
11be3 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20 54  ing it..    ** T
11be4 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 6e 6f  his indicates no
11be5 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64 73  thing more needs
11be6 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61   to be rolled ba
11be7 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  ck..    */.    r
11be8 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48  c = readJournalH
11be9 64 72 28 70 50 61 67 65 72 2c 20 73 7a 4a 2c 20  dr(pPager, szJ, 
11bea 26 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b 0a 20  &nRec, &mxPg);. 
11beb 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
11bec 45 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20 20 69  E_OK ){ .      i
11bed 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
11bee 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  NE ){.        rc
11bef 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
11bf0 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f      }.      goto
11bf1 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20   end_playback;. 
11bf2 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
11bf3 6e 52 65 63 20 69 73 20 30 78 66 66 66 66 66 66  nRec is 0xffffff
11bf4 66 66 2c 20 74 68 65 6e 20 74 68 69 73 20 6a 6f  ff, then this jo
11bf5 75 72 6e 61 6c 20 77 61 73 20 63 72 65 61 74 65  urnal was create
11bf6 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 0a 20  d by a process. 
11bf7 20 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20 69 6e     ** working in
11bf8 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 54   no-sync mode. T
11bf9 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74  his means that t
11bfa 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a  he rest of the j
11bfb 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69  ournal.    ** fi
11bfc 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 70  le consists of p
11bfd 61 67 65 73 2c 20 74 68 65 72 65 20 61 72 65 20  ages, there are 
11bfe 6e 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61 6c 20  no more journal 
11bff 68 65 61 64 65 72 73 2e 20 43 6f 6d 70 75 74 65  headers. Compute
11c00 0a 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75  .    ** the valu
11c01 65 20 6f 66 20 6e 52 65 63 20 62 61 73 65 64 20  e of nRec based 
11c02 6f 6e 20 74 68 69 73 20 61 73 73 75 6d 70 74 69  on this assumpti
11c03 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  on..    */.    i
11c04 66 28 20 6e 52 65 63 3d 3d 30 78 66 66 66 66 66  f( nRec==0xfffff
11c05 66 66 66 20 29 7b 0a 20 20 20 20 20 20 61 73 73  fff ){.      ass
11c06 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ert( pPager->jou
11c07 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c  rnalOff==JOURNAL
11c08 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
11c09 29 3b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20  );.      nRec = 
11c0a 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 4a 4f 55  (int)((szJ - JOU
11c0b 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
11c0c 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f  er))/JOURNAL_PG_
11c0d 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20  SZ(pPager));.   
11c0e 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52   }..    /* If nR
11c0f 65 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 73  ec is 0 and this
11c10 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 20   rollback is of 
11c11 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 72  a transaction cr
11c12 65 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 20  eated by this.  
11c13 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e 64    ** process and
11c14 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20   if this is the 
11c15 66 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20  final header in 
11c16 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65  the journal, the
11c17 6e 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a  n it means.    *
11c18 2a 20 74 68 61 74 20 74 68 69 73 20 70 61 72 74  * that this part
11c19 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
11c1a 77 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 64  was being filled
11c1b 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74   but has not yet
11c1c 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 6e   been.    ** syn
11c1d 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 6f  ced to disk.  Co
11c1e 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72  mpute the number
11c1f 20 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 20   of pages based 
11c20 6f 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e 67  on the remaining
11c21 0a 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20  .    ** size of 
11c22 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a  the file..    **
11c23 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 72  .    ** The thir
11c24 64 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 65  d term of the te
11c25 73 74 20 77 61 73 20 61 64 64 65 64 20 74 6f 20  st was added to 
11c26 66 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 35  fix ticket #2565
11c27 2e 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 72 6f  ..    ** When ro
11c28 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74  lling back a hot
11c29 20 6a 6f 75 72 6e 61 6c 2c 20 6e 52 65 63 3d 3d   journal, nRec==
11c2a 30 20 61 6c 77 61 79 73 20 6d 65 61 6e 73 20 74  0 always means t
11c2b 68 61 74 20 74 68 65 20 6e 65 78 74 0a 20 20 20  hat the next.   
11c2c 20 2a 2a 20 63 68 75 6e 6b 20 6f 66 20 74 68 65   ** chunk of the
11c2d 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e   journal contain
11c2e 73 20 7a 65 72 6f 20 70 61 67 65 73 20 74 6f 20  s zero pages to 
11c2f 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  be rolled back. 
11c30 20 42 75 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e   But.    ** when
11c31 20 64 6f 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43   doing a ROLLBAC
11c32 4b 20 61 6e 64 20 74 68 65 20 6e 52 65 63 3d 3d  K and the nRec==
11c33 30 20 63 68 75 6e 6b 20 69 73 20 74 68 65 20 6c  0 chunk is the l
11c34 61 73 74 20 63 68 75 6e 6b 20 69 6e 0a 20 20 20  ast chunk in.   
11c35 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c   ** the journal,
11c36 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
11c37 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74  he journal might
11c38 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 6f   contain additio
11c39 6e 61 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 73  nal.    ** pages
11c3a 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65   that need to be
11c3b 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64   rolled back and
11c3c 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72   that the number
11c3d 20 6f 66 20 70 61 67 65 73 20 0a 20 20 20 20 2a   of pages .    *
11c3e 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70  * should be comp
11c3f 75 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68  uted based on th
11c40 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73  e journal file s
11c41 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ize..    */.    
11c42 74 65 73 74 63 61 73 65 28 20 6e 52 65 63 3d 3d  testcase( nRec==
11c43 30 20 26 26 20 21 69 73 48 6f 74 0a 20 20 20 20  0 && !isHot.    
11c44 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e       && pPager->
11c45 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e  journalHdr+JOURN
11c46 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
11c47 29 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  )!=pPager->journ
11c48 61 6c 4f 66 66 0a 20 20 20 20 20 20 20 20 20 26  alOff.         &
11c49 26 20 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72  & ((szJ - pPager
11c4a 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20  ->journalOff) / 
11c4b 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50  JOURNAL_PG_SZ(pP
11c4c 61 67 65 72 29 29 3e 30 0a 20 20 20 20 20 20 20  ager))>0.       
11c4d 20 20 26 26 20 70 61 67 65 72 4e 65 78 74 4a 6f    && pagerNextJo
11c4e 75 72 6e 61 6c 50 61 67 65 49 73 56 61 6c 69 64  urnalPageIsValid
11c4f 28 70 50 61 67 65 72 29 0a 20 20 20 20 29 3b 0a  (pPager).    );.
11c50 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 20      if( nRec==0 
11c51 26 26 20 21 69 73 48 6f 74 20 26 26 0a 20 20 20  && !isHot &&.   
11c52 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
11c53 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f  rnalHdr+JOURNAL_
11c54 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d  HDR_SZ(pPager)==
11c55 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11c56 66 66 20 29 7b 0a 20 20 20 20 20 20 6e 52 65 63  ff ){.      nRec
11c57 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20   = (int)((szJ - 
11c58 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11c59 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47  ff) / JOURNAL_PG
11c5a 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20  _SZ(pPager));.  
11c5b 20 20 20 20 69 73 55 6e 73 79 6e 63 20 3d 20 31      isUnsync = 1
11c5c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
11c5d 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  If this is the f
11c5e 69 72 73 74 20 68 65 61 64 65 72 20 72 65 61 64  irst header read
11c5f 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
11c60 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a  l, truncate the.
11c61 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
11c62 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 73  file back to its
11c63 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0a   original size..
11c64 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
11c65 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11c66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  f==JOURNAL_HDR_S
11c67 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20  Z(pPager) ){.   
11c68 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72     rc = pager_tr
11c69 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 6d  uncate(pPager, m
11c6a 78 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  xPg);.      if( 
11c6b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
11c6c 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  .        goto en
11c6d 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20  d_playback;.    
11c6e 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72    }.      pPager
11c6f 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78 50 67 3b  ->dbSize = mxPg;
11c70 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
11c71 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  opy original pag
11c72 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a 6f  es out of the jo
11c73 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 69  urnal and back i
11c74 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a 20  nto the .    ** 
11c75 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
11c76 64 2f 6f 72 20 70 61 67 65 20 63 61 63 68 65 2e  d/or page cache.
11c77 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28  .    */.    for(
11c78 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 75 2b 2b  u=0; u<nRec; u++
11c79 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 65  ){.      if( nee
11c7a 64 50 61 67 65 72 52 65 73 65 74 20 29 7b 0a 20  dPagerReset ){. 
11c7b 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73         pager_res
11c7c 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  et(pPager);.    
11c7d 20 20 20 20 6e 65 65 64 50 61 67 65 72 52 65 73      needPagerRes
11c7e 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  et = 0;.      }.
11c7f 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
11c80 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61  _playback_one_pa
11c81 67 65 28 70 50 61 67 65 72 2c 31 2c 69 73 55 6e  ge(pPager,1,isUn
11c82 73 79 6e 63 2c 26 70 50 61 67 65 72 2d 3e 6a 6f  sync,&pPager->jo
11c83 75 72 6e 61 6c 4f 66 66 2c 30 2c 30 29 3b 0a 20  urnalOff,0,0);. 
11c84 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
11c85 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
11c86 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
11c87 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20  _DONE ){.       
11c88 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
11c89 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  K;.          pPa
11c8a 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
11c8b 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 20 20 20 20  = szJ;.         
11c8c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
11c8d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
11c8e 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 75 6e   /* If we are un
11c8f 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  able to rollback
11c90 2c 20 71 75 69 74 20 61 6e 64 20 72 65 74 75 72  , quit and retur
11c91 6e 20 74 68 65 20 65 72 72 6f 72 0a 20 20 20 20  n the error.    
11c92 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 2e 20 20        ** code.  
11c93 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20  This will cause 
11c94 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e 74  the pager to ent
11c95 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  er the error sta
11c96 74 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  te.          ** 
11c97 73 6f 20 74 68 61 74 20 6e 6f 20 66 75 72 74 68  so that no furth
11c98 65 72 20 68 61 72 6d 20 77 69 6c 6c 20 62 65 20  er harm will be 
11c99 64 6f 6e 65 2e 20 20 50 65 72 68 61 70 73 20 74  done.  Perhaps t
11c9a 68 65 20 6e 65 78 74 0a 20 20 20 20 20 20 20 20  he next.        
11c9b 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 74 6f 20    ** process to 
11c9c 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 69 6c 6c 20  come along will 
11c9d 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62  be able to rollb
11c9e 61 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65  ack the database
11c9f 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
11ca0 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
11ca1 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20  d_playback;.    
11ca2 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
11ca3 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52    }.  }.  /*NOTR
11ca4 45 41 43 48 45 44 2a 2f 0a 20 20 61 73 73 65 72  EACHED*/.  asser
11ca5 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f 70 6c 61  t( 0 );..end_pla
11ca6 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 46 6f 6c 6c  yback:.  /* Foll
11ca7 6f 77 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b  owing a rollback
11ca8 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  , the database f
11ca9 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 62 61  ile should be ba
11caa 63 6b 20 69 6e 20 69 74 73 20 6f 72 69 67 69 6e  ck in its origin
11cab 61 6c 0a 20 20 2a 2a 20 73 74 61 74 65 20 70 72  al.  ** state pr
11cac 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 74  ior to the start
11cad 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74   of the transact
11cae 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f 6b 65 20 74  ion, so invoke t
11caf 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 46  he.  ** SQLITE_F
11cb0 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45  CNTL_DB_UNCHANGE
11cb1 44 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6d  D file-control m
11cb2 65 74 68 6f 64 20 74 6f 20 64 69 73 61 62 6c 65  ethod to disable
11cb3 20 74 68 65 0a 20 20 2a 2a 20 61 73 73 65 72 74   the.  ** assert
11cb4 69 6f 6e 20 74 68 61 74 20 74 68 65 20 74 72 61  ion that the tra
11cb5 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72  nsaction counter
11cb6 20 77 61 73 20 6d 6f 64 69 66 69 65 64 2e 0a 20   was modified.. 
11cb7 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 0a 20 20   */.  assert(.  
11cb8 20 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d    pPager->fd->pM
11cb9 65 74 68 6f 64 73 3d 3d 30 20 7c 7c 0a 20 20 20  ethods==0 ||.   
11cba 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f   sqlite3OsFileCo
11cbb 6e 74 72 6f 6c 28 70 50 61 67 65 72 2d 3e 66 64  ntrol(pPager->fd
11cbc 2c 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42  ,SQLITE_FCNTL_DB
11cbd 5f 55 4e 43 48 41 4e 47 45 44 2c 30 29 3e 3d 53  _UNCHANGED,0)>=S
11cbe 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 3b 0a 0a 20  QLITE_OK.  );.. 
11cbf 20 2f 2a 20 49 66 20 74 68 69 73 20 70 6c 61 79   /* If this play
11cc0 62 61 63 6b 20 69 73 20 68 61 70 70 65 6e 69 6e  back is happenin
11cc1 67 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  g automatically 
11cc2 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61  as a result of a
11cc3 6e 20 49 4f 20 6f 72 20 0a 20 20 2a 2a 20 6d 61  n IO or .  ** ma
11cc4 6c 6c 6f 63 20 65 72 72 6f 72 20 74 68 61 74 20  lloc error that 
11cc5 6f 63 63 75 72 72 65 64 20 61 66 74 65 72 20 74  occurred after t
11cc6 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  he change-counte
11cc7 72 20 77 61 73 20 75 70 64 61 74 65 64 20 62 75  r was updated bu
11cc8 74 20 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 74  t .  ** before t
11cc9 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  he transaction w
11cca 61 73 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74 68  as committed, th
11ccb 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  en the change-co
11ccc 75 6e 74 65 72 20 0a 20 20 2a 2a 20 6d 6f 64 69  unter .  ** modi
11ccd 66 69 63 61 74 69 6f 6e 20 6d 61 79 20 6a 75 73  fication may jus
11cce 74 20 68 61 76 65 20 62 65 65 6e 20 72 65 76 65  t have been reve
11ccf 72 74 65 64 2e 20 49 66 20 74 68 69 73 20 68 61  rted. If this ha
11cd0 70 70 65 6e 73 20 69 6e 20 65 78 63 6c 75 73 69  ppens in exclusi
11cd1 76 65 20 0a 20 20 2a 2a 20 6d 6f 64 65 2c 20 74  ve .  ** mode, t
11cd2 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 74  hen subsequent t
11cd3 72 61 6e 73 61 63 74 69 6f 6e 73 20 70 65 72 66  ransactions perf
11cd4 6f 72 6d 65 64 20 62 79 20 74 68 65 20 63 6f 6e  ormed by the con
11cd5 6e 65 63 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74  nection will not
11cd6 0a 20 20 2a 2a 20 75 70 64 61 74 65 20 74 68 65  .  ** update the
11cd7 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
11cd8 61 74 20 61 6c 6c 2e 20 54 68 69 73 20 6d 61 79  at all. This may
11cd9 20 6c 65 61 64 20 74 6f 20 63 61 63 68 65 20 69   lead to cache i
11cda 6e 63 6f 6e 73 69 73 74 65 6e 63 79 0a 20 20 2a  nconsistency.  *
11cdb 2a 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f  * problems for o
11cdc 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 61  ther processes a
11cdd 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20  t some point in 
11cde 74 68 65 20 66 75 74 75 72 65 2e 20 53 6f 2c 20  the future. So, 
11cdf 6a 75 73 74 0a 20 20 2a 2a 20 69 6e 20 63 61 73  just.  ** in cas
11ce0 65 20 74 68 69 73 20 68 61 73 20 68 61 70 70 65  e this has happe
11ce1 6e 65 64 2c 20 63 6c 65 61 72 20 74 68 65 20 63  ned, clear the c
11ce2 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66  hangeCountDone f
11ce3 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20  lag now..  */.  
11ce4 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
11ce5 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72  untDone = pPager
11ce6 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 0a 20 20 69  ->tempFile;..  i
11ce7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
11ce8 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20   ){.    zMaster 
11ce9 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70  = pPager->pTmpSp
11cea 61 63 65 3b 0a 20 20 20 20 72 63 20 3d 20 72 65  ace;.    rc = re
11ceb 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28  adMasterJournal(
11cec 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61  pPager->jfd, zMa
11ced 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 56  ster, pPager->pV
11cee 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  fs->mxPathname+1
11cef 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
11cf0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11cf1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
11cf2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
11cf3 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f   rc = pager_end_
11cf4 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67  transaction(pPag
11cf5 65 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21 3d  er, zMaster[0]!=
11cf6 27 5c 30 27 29 3b 0a 20 20 20 20 74 65 73 74 63  '\0');.    testc
11cf7 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ase( rc!=SQLITE_
11cf8 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OK );.  }.  if( 
11cf9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
11cfa 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 26 20 72   zMaster[0] && r
11cfb 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  es ){.    /* If 
11cfc 74 68 65 72 65 20 77 61 73 20 61 20 6d 61 73 74  there was a mast
11cfd 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74  er journal and t
11cfe 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
11cff 20 72 65 74 75 72 6e 20 73 75 63 63 65 73 73 2c   return success,
11d00 0a 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 69  .    ** see if i
11d01 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f  t is possible to
11d02 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74   delete the mast
11d03 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20  er journal..    
11d04 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  */.    rc = page
11d05 72 5f 64 65 6c 6d 61 73 74 65 72 28 70 50 61 67  r_delmaster(pPag
11d06 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  er, zMaster);.  
11d07 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d    testcase( rc!=
11d08 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d  SQLITE_OK );.  }
11d09 0a 0a 20 20 2f 2a 20 54 68 65 20 50 61 67 65 72  ..  /* The Pager
11d0a 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69  .sectorSize vari
11d0b 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20 62 65  able may have be
11d0c 65 6e 20 75 70 64 61 74 65 64 20 77 68 69 6c 65  en updated while
11d0d 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a 2a 20 62 61   rolling.  ** ba
11d0e 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 63 72 65  ck a journal cre
11d0f 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65 73  ated by a proces
11d10 73 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65  s with a differe
11d11 6e 74 20 73 65 63 74 6f 72 20 73 69 7a 65 0a 20  nt sector size. 
11d12 20 2a 2a 20 76 61 6c 75 65 2e 20 52 65 73 65 74   ** value. Reset
11d13 20 69 74 20 74 6f 20 74 68 65 20 63 6f 72 72 65   it to the corre
11d14 63 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 69  ct value for thi
11d15 73 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a 2f 0a  s process..  */.
11d16 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28    setSectorSize(
11d17 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72  pPager);.  retur
11d18 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  n rc;.}../*.** P
11d19 6c 61 79 62 61 63 6b 20 73 61 76 65 70 6f 69 6e  layback savepoin
11d1a 74 20 70 53 61 76 65 70 6f 69 6e 74 2e 20 4f 72  t pSavepoint. Or
11d1b 2c 20 69 66 20 70 53 61 76 65 70 6f 69 6e 74 3d  , if pSavepoint=
11d1c 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 6c 61 79  =NULL, then play
11d1d 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65 6e 74 69  back.** the enti
11d1e 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  re master journa
11d1f 6c 20 66 69 6c 65 2e 20 54 68 65 20 63 61 73 65  l file. The case
11d20 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c   pSavepoint==NUL
11d21 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 0a 2a  L occurs when .*
11d22 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20  * a ROLLBACK TO 
11d23 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e 76 6f 6b  command is invok
11d24 65 64 20 6f 6e 20 61 20 53 41 56 45 50 4f 49 4e  ed on a SAVEPOIN
11d25 54 20 74 68 61 74 20 69 73 20 61 20 74 72 61 6e  T that is a tran
11d26 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73 61 76 65  saction .** save
11d27 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  point..**.** Whe
11d28 6e 20 70 53 61 76 65 70 6f 69 6e 74 20 69 73 20  n pSavepoint is 
11d29 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61 6e 69 6e  not NULL (meanin
11d2a 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74  g a non-transact
11d2b 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73  ion savepoint is
11d2c 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f 6c 6c 65   .** being rolle
11d2d 64 20 62 61 63 6b 29 2c 20 74 68 65 6e 20 74 68  d back), then th
11d2e 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73 69  e rollback consi
11d2f 73 74 73 20 6f 66 20 75 70 20 74 6f 20 74 68 72  sts of up to thr
11d30 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a 20 70 65  ee stages,.** pe
11d31 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 65 20 6f  rformed in the o
11d32 72 64 65 72 20 73 70 65 63 69 66 69 65 64 3a 0a  rder specified:.
11d33 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 20  **.**   * Pages 
11d34 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20  are played back 
11d35 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f  from the main jo
11d36 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61  urnal starting a
11d37 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 6f 66  t byte.**     of
11d38 66 73 65 74 20 50 61 67 65 72 53 61 76 65 70 6f  fset PagerSavepo
11d39 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20  int.iOffset and 
11d3a 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 0a 2a  continuing to .*
11d3b 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70  *     PagerSavep
11d3c 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 2c  oint.iHdrOffset,
11d3d 20 6f 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f   or to the end o
11d3e 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  f the main journ
11d3f 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 69  al.**     file i
11d40 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  f PagerSavepoint
11d41 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 7a  .iHdrOffset is z
11d42 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49  ero..**.**   * I
11d43 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  f PagerSavepoint
11d44 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 6e  .iHdrOffset is n
11d45 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 70 61  ot zero, then pa
11d46 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 0a 2a  ges are played.*
11d47 2a 20 20 20 20 20 62 61 63 6b 20 73 74 61 72 74  *     back start
11d48 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75  ing from the jou
11d49 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6d 6d 65  rnal header imme
11d4a 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e  diately followin
11d4b 67 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53  g .**     PagerS
11d4c 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
11d4d 73 65 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  set to the end o
11d4e 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  f the main journ
11d4f 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20  al file..**.**  
11d50 20 2a 20 50 61 67 65 73 20 61 72 65 20 74 68 65   * Pages are the
11d51 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72  n played back fr
11d52 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  om the sub-journ
11d53 61 6c 20 66 69 6c 65 2c 20 73 74 61 72 74 69 6e  al file, startin
11d54 67 0a 2a 2a 20 20 20 20 20 77 69 74 68 20 74 68  g.**     with th
11d55 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  e PagerSavepoint
11d56 2e 69 53 75 62 52 65 63 20 61 6e 64 20 63 6f 6e  .iSubRec and con
11d57 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 65  tinuing to the e
11d58 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20 74 68 65  nd of.**     the
11d59 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a   journal file..*
11d5a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f 75 74 20  *.** Throughout 
11d5b 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 70 72 6f  the rollback pro
11d5c 63 65 73 73 2c 20 65 61 63 68 20 74 69 6d 65 20  cess, each time 
11d5d 61 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64  a page is rolled
11d5e 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 63 6f   back, the.** co
11d5f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20  rresponding bit 
11d60 69 73 20 73 65 74 20 69 6e 20 61 20 62 69 74 76  is set in a bitv
11d61 65 63 20 73 74 72 75 63 74 75 72 65 20 28 76 61  ec structure (va
11d62 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 69 6e 20  riable pDone in 
11d63 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  the.** implement
11d64 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e 20 54 68  ation below). Th
11d65 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e  is is used to en
11d66 73 75 72 65 20 74 68 61 74 20 61 20 70 61 67 65  sure that a page
11d67 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 6f 6c 6c   is only.** roll
11d68 65 64 20 62 61 63 6b 20 74 68 65 20 66 69 72 73  ed back the firs
11d69 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e 63  t time it is enc
11d6a 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 69 74 68  ountered in eith
11d6b 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a  er journal..**.*
11d6c 2a 20 49 66 20 70 53 61 76 65 70 6f 69 6e 74 20  * If pSavepoint 
11d6d 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 61  is NULL, then pa
11d6e 67 65 73 20 61 72 65 20 6f 6e 6c 79 20 70 6c 61  ges are only pla
11d6f 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68  yed back from th
11d70 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61  e main.** journa
11d71 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 20 69 73  l file. There is
11d72 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 61 20 62   no need for a b
11d73 69 74 76 65 63 20 69 6e 20 74 68 69 73 20 63 61  itvec in this ca
11d74 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 74  se..**.** In eit
11d75 68 65 72 20 63 61 73 65 2c 20 62 65 66 6f 72 65  her case, before
11d76 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d 6d 65 6e   playback commen
11d77 63 65 73 20 74 68 65 20 50 61 67 65 72 2e 64 62  ces the Pager.db
11d78 53 69 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a  Size variable.**
11d79 20 69 73 20 72 65 73 65 74 20 74 6f 20 74 68 65   is reset to the
11d7a 20 76 61 6c 75 65 20 74 68 61 74 20 69 74 20 68   value that it h
11d7b 65 6c 64 20 61 74 20 74 68 65 20 73 74 61 72 74  eld at the start
11d7c 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69 6e   of the savepoin
11d7d 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61 6e 73 61  t .** (or transa
11d7e 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61 67 65 20  ction). No page 
11d7f 77 69 74 68 20 61 20 70 61 67 65 2d 6e 75 6d 62  with a page-numb
11d80 65 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  er greater than 
11d81 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a 20 69 73  this value.** is
11d82 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 49 66   played back. If
11d83 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75 6e 74 65   one is encounte
11d84 72 65 64 20 69 74 20 69 73 20 73 69 6d 70 6c 79  red it is simply
11d85 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a 73 74 61   skipped..*/.sta
11d86 74 69 63 20 69 6e 74 20 70 61 67 65 72 50 6c 61  tic int pagerPla
11d87 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 50  ybackSavepoint(P
11d88 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 61  ager *pPager, Pa
11d89 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 53  gerSavepoint *pS
11d8a 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 36 34  avepoint){.  i64
11d8b 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20   szJ;           
11d8c 20 20 20 20 20 20 2f 2a 20 45 66 66 65 63 74 69        /* Effecti
11d8d 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d  ve size of the m
11d8e 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20  ain journal */. 
11d8f 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20   i64 iHdrOff;   
11d90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64            /* End
11d91 20 6f 66 20 66 69 72 73 74 20 73 65 67 6d 65 6e   of first segmen
11d92 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e 61  t of main-journa
11d93 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a 20 20 69  l records */.  i
11d94 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
11d95 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72  K;      /* Retur
11d96 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 69 74 76  n code */.  Bitv
11d97 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 3b 20 20  ec *pDone = 0;  
11d98 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20 74       /* Bitvec t
11d99 6f 20 65 6e 73 75 72 65 20 70 61 67 65 73 20 70  o ensure pages p
11d9a 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e 6c 79 20  layed back only 
11d9b 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  once */..  asser
11d9c 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
11d9d 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29  >=PAGER_SHARED )
11d9e 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
11d9f 20 61 20 62 69 74 76 65 63 20 74 6f 20 75 73 65   a bitvec to use
11da0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65   to store the se
11da1 74 20 6f 66 20 70 61 67 65 73 20 72 6f 6c 6c 65  t of pages rolle
11da2 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20  d back */.  if( 
11da3 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20  pSavepoint ){.  
11da4 20 20 70 44 6f 6e 65 20 3d 20 73 71 6c 69 74 65    pDone = sqlite
11da5 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 53  3BitvecCreate(pS
11da6 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 29  avepoint->nOrig)
11da7 3b 0a 20 20 20 20 69 66 28 20 21 70 44 6f 6e 65  ;.    if( !pDone
11da8 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
11da9 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
11daa 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53     }.  }..  /* S
11dab 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  et the database 
11dac 73 69 7a 65 20 62 61 63 6b 20 74 6f 20 74 68 65  size back to the
11dad 20 76 61 6c 75 65 20 69 74 20 77 61 73 20 62 65   value it was be
11dae 66 6f 72 65 20 74 68 65 20 73 61 76 65 70 6f 69  fore the savepoi
11daf 6e 74 20 0a 20 20 2a 2a 20 62 65 69 6e 67 20 72  nt .  ** being r
11db0 65 76 65 72 74 65 64 20 77 61 73 20 6f 70 65 6e  everted was open
11db1 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65  ed..  */.  pPage
11db2 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 53 61 76  r->dbSize = pSav
11db3 65 70 6f 69 6e 74 20 3f 20 70 53 61 76 65 70 6f  epoint ? pSavepo
11db4 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a 20 70 50 61  int->nOrig : pPa
11db5 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b  ger->dbOrigSize;
11db6 0a 0a 20 20 2f 2a 20 55 73 65 20 70 50 61 67 65  ..  /* Use pPage
11db7 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 61 73  r->journalOff as
11db8 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 73   the effective s
11db9 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  ize of the main 
11dba 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f  rollback.  ** jo
11dbb 75 72 6e 61 6c 2e 20 20 54 68 65 20 61 63 74 75  urnal.  The actu
11dbc 61 6c 20 66 69 6c 65 20 6d 69 67 68 74 20 62 65  al file might be
11dbd 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69   larger than thi
11dbe 73 20 69 6e 0a 20 20 2a 2a 20 50 41 47 45 52 5f  s in.  ** PAGER_
11dbf 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e  JOURNALMODE_TRUN
11dc0 43 41 54 45 20 6f 72 20 50 41 47 45 52 5f 4a 4f  CATE or PAGER_JO
11dc1 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
11dc2 54 2e 20 20 42 75 74 20 61 6e 79 74 68 69 6e 67  T.  But anything
11dc3 0a 20 20 2a 2a 20 70 61 73 74 20 70 50 61 67 65  .  ** past pPage
11dc4 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69 73  r->journalOff is
11dc5 20 6f 66 66 2d 6c 69 6d 69 74 73 20 74 6f 20 75   off-limits to u
11dc6 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a 4a 20 3d 20  s..  */.  szJ = 
11dc7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11dc8 66 66 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20  ff;..  /* Begin 
11dc9 62 79 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  by rolling back 
11dca 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65  records from the
11dcb 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74   main journal st
11dcc 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 50  arting at.  ** P
11dcd 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f  agerSavepoint.iO
11dce 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e  ffset and contin
11dcf 75 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 78 74  uing to the next
11dd0 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e   journal header.
11dd1 0a 20 20 2a 2a 20 54 68 65 72 65 20 6d 69 67 68  .  ** There migh
11dd2 74 20 62 65 20 72 65 63 6f 72 64 73 20 69 6e 20  t be records in 
11dd3 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
11dd4 20 74 68 61 74 20 68 61 76 65 20 61 20 70 61 67   that have a pag
11dd5 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 67 72  e number.  ** gr
11dd6 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63  eater than the c
11dd7 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65 20  urrent database 
11dd8 73 69 7a 65 20 28 70 50 61 67 65 72 2d 3e 64 62  size (pPager->db
11dd9 53 69 7a 65 29 20 62 75 74 20 74 68 6f 73 65 0a  Size) but those.
11dda 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73 6b 69    ** will be ski
11ddb 70 70 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  pped automatical
11ddc 6c 79 2e 20 20 50 61 67 65 73 20 61 72 65 20 61  ly.  Pages are a
11ddd 64 64 65 64 20 74 6f 20 70 44 6f 6e 65 20 61 73  dded to pDone as
11dde 20 74 68 65 79 0a 20 20 2a 2a 20 61 72 65 20 70   they.  ** are p
11ddf 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2f  layed back..  */
11de0 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e  .  if( pSavepoin
11de1 74 20 29 7b 0a 20 20 20 20 69 48 64 72 4f 66 66  t ){.    iHdrOff
11de2 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69   = pSavepoint->i
11de3 48 64 72 4f 66 66 73 65 74 20 3f 20 70 53 61 76  HdrOffset ? pSav
11de4 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73  epoint->iHdrOffs
11de5 65 74 20 3a 20 73 7a 4a 3b 0a 20 20 20 20 70 50  et : szJ;.    pP
11de6 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11de7 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69   = pSavepoint->i
11de8 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c  Offset;.    whil
11de9 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
11dea 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   && pPager->jour
11deb 6e 61 6c 4f 66 66 3c 69 48 64 72 4f 66 66 20 29  nalOff<iHdrOff )
11dec 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  {.      rc = pag
11ded 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f  er_playback_one_
11dee 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c 20  page(pPager, 1, 
11def 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72  0, &pPager->jour
11df0 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65  nalOff, 1, pDone
11df1 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  );.    }.    ass
11df2 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
11df3 44 4f 4e 45 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  DONE );.  }else{
11df4 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
11df5 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 7d  rnalOff = 0;.  }
11df6 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20  ..  /* Continue 
11df7 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65 63  rolling back rec
11df8 6f 72 64 73 20 6f 75 74 20 6f 66 20 74 68 65 20  ords out of the 
11df9 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61  main journal sta
11dfa 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 74 68  rting at.  ** th
11dfb 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20  e first journal 
11dfc 68 65 61 64 65 72 20 73 65 65 6e 20 61 6e 64 20  header seen and 
11dfd 63 6f 6e 74 69 6e 75 69 6e 67 20 75 6e 74 69 6c  continuing until
11dfe 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 65   the effective e
11dff 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d  nd.  ** of the m
11e00 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
11e01 2e 20 20 43 6f 6e 74 69 6e 75 65 20 74 6f 20 73  .  Continue to s
11e02 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65  kip out-of-range
11e03 20 70 61 67 65 73 20 61 6e 64 0a 20 20 2a 2a 20   pages and.  ** 
11e04 63 6f 6e 74 69 6e 75 65 20 61 64 64 69 6e 67 20  continue adding 
11e05 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61 63  pages rolled bac
11e06 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a 20 20 2a 2f  k to pDone..  */
11e07 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
11e08 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65  LITE_OK && pPage
11e09 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a  r->journalOff<sz
11e0a 4a 20 29 7b 0a 20 20 20 20 75 33 32 20 69 69 3b  J ){.    u32 ii;
11e0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
11e0c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
11e0d 20 20 20 75 33 32 20 6e 4a 52 65 63 20 3d 20 30     u32 nJRec = 0
11e0e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
11e0f 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f 72  of Journal Recor
11e10 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 64 75  ds */.    u32 du
11e11 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20 72 65  mmy;.    rc = re
11e12 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  adJournalHdr(pPa
11e13 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63  ger, szJ, &nJRec
11e14 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 61  , &dummy);.    a
11e15 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
11e16 45 5f 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 2f  E_DONE );..    /
11e17 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 22 70 50  *.    ** The "pP
11e18 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
11e19 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  +JOURNAL_HDR_SZ(
11e1a 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d  pPager)==pPager-
11e1b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20 20  >journalOff".   
11e1c 20 2a 2a 20 74 65 73 74 20 69 73 20 72 65 6c 61   ** test is rela
11e1d 74 65 64 20 74 6f 20 74 69 63 6b 65 74 20 23 32  ted to ticket #2
11e1e 35 36 35 2e 20 20 53 65 65 20 74 68 65 20 64 69  565.  See the di
11e1f 73 63 75 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a  scussion in the.
11e20 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61      ** pager_pla
11e21 79 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e  yback() function
11e22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
11e23 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20  information..   
11e24 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
11e25 21 28 6e 4a 52 65 63 3d 3d 30 0a 20 20 20 20 20  !(nJRec==0.     
11e26 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a      && pPager->j
11e27 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41  ournalHdr+JOURNA
11e28 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
11e29 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  !=pPager->journa
11e2a 6c 4f 66 66 0a 20 20 20 20 20 20 20 20 20 26 26  lOff.         &&
11e2b 20 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d   ((szJ - pPager-
11e2c 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a  >journalOff) / J
11e2d 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61  OURNAL_PG_SZ(pPa
11e2e 67 65 72 29 29 3e 30 0a 20 20 20 20 20 20 20 20  ger))>0.        
11e2f 20 26 26 20 70 61 67 65 72 4e 65 78 74 4a 6f 75   && pagerNextJou
11e30 72 6e 61 6c 50 61 67 65 49 73 56 61 6c 69 64 28  rnalPageIsValid(
11e31 70 50 61 67 65 72 29 29 0a 20 20 20 20 29 3b 0a  pPager)).    );.
11e32 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d 30      if( nJRec==0
11e33 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 72   .     && pPager
11e34 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55  ->journalHdr+JOU
11e35 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
11e36 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75  er)==pPager->jou
11e37 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a 20  rnalOff.    ){. 
11e38 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 75 33       nJRec = (u3
11e39 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72  2)((szJ - pPager
11e3a 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f  ->journalOff)/JO
11e3b 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67  URNAL_PG_SZ(pPag
11e3c 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  er));.    }.    
11e3d 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51  for(ii=0; rc==SQ
11e3e 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a  LITE_OK && ii<nJ
11e3f 52 65 63 20 26 26 20 70 50 61 67 65 72 2d 3e 6a  Rec && pPager->j
11e40 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69  ournalOff<szJ; i
11e41 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  i++){.      rc =
11e42 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
11e43 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c  one_page(pPager,
11e44 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e   1, 0, &pPager->
11e45 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70  journalOff, 1, p
11e46 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Done);.    }.   
11e47 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
11e48 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a  ITE_DONE );.  }.
11e49 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
11e4a 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65  LITE_OK || pPage
11e4b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73  r->journalOff==s
11e4c 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61  zJ );..  /* Fina
11e4d 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70  lly,  rollback p
11e4e 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 75  ages from the su
11e4f 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65  b-journal.  Page
11e50 20 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a 20   that were.  ** 
11e51 70 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65  previously rolle
11e52 64 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 68  d back out of th
11e53 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28  e main journal (
11e54 61 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69 6e  and are hence in
11e55 20 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c   pDone).  ** wil
11e56 6c 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20 4f  l be skipped.  O
11e57 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65  ut-of-range page
11e58 73 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 70  s are also skipp
11e59 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ed..  */.  if( p
11e5a 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20  Savepoint ){.   
11e5b 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20   u32 ii;        
11e5c 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
11e5d 74 65 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f  ter */.    i64 o
11e5e 66 66 73 65 74 20 3d 20 70 53 61 76 65 70 6f 69  ffset = pSavepoi
11e5f 6e 74 2d 3e 69 53 75 62 52 65 63 2a 28 34 2b 70  nt->iSubRec*(4+p
11e60 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29  Pager->pageSize)
11e61 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 70 53 61  ;.    for(ii=pSa
11e62 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63  vepoint->iSubRec
11e63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
11e64 26 26 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53  && ii<pPager->nS
11e65 75 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 20  ubRec; ii++){.  
11e66 20 20 20 20 61 73 73 65 72 74 28 20 6f 66 66 73      assert( offs
11e67 65 74 3d 3d 69 69 2a 28 34 2b 70 50 61 67 65 72  et==ii*(4+pPager
11e68 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 3b 0a 20  ->pageSize) );. 
11e69 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
11e6a 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67  playback_one_pag
11e6b 65 28 70 50 61 67 65 72 2c 20 30 2c 20 30 2c 20  e(pPager, 0, 0, 
11e6c 26 6f 66 66 73 65 74 2c 20 31 2c 20 70 44 6f 6e  &offset, 1, pDon
11e6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  e);.    }.    as
11e6e 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
11e6f 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20 20  _DONE );.  }..  
11e70 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
11e71 74 72 6f 79 28 70 44 6f 6e 65 29 3b 0a 20 20 69  troy(pDone);.  i
11e72 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
11e73 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
11e74 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a  journalOff = szJ
11e75 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
11e76 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  c;.}../*.** Chan
11e77 67 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ge the maximum n
11e78 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f  umber of in-memo
11e79 72 79 20 70 61 67 65 73 20 74 68 61 74 20 61 72  ry pages that ar
11e7a 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51  e allowed..*/.SQ
11e7b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
11e7c 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  d sqlite3PagerSe
11e7d 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72  tCachesize(Pager
11e7e 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78   *pPager, int mx
11e7f 50 61 67 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  Page){.  sqlite3
11e80 50 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69  PcacheSetCachesi
11e81 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ze(pPager->pPCac
11e82 68 65 2c 20 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a  he, mxPage);.}..
11e83 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 65  /*.** Adjust the
11e84 20 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 74   robustness of t
11e85 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 64  he database to d
11e86 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20  amage due to OS 
11e87 63 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f  crashes.** or po
11e88 77 65 72 20 66 61 69 6c 75 72 65 73 20 62 79 20  wer failures by 
11e89 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d  changing the num
11e8a 62 65 72 20 6f 66 20 73 79 6e 63 73 28 29 73 20  ber of syncs()s 
11e8b 77 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20  when writing.** 
11e8c 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
11e8d 72 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 72 65  rnal.  There are
11e8e 20 74 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a   three levels:.*
11e8f 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 20  *.**    OFF     
11e90 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28    sqlite3OsSync(
11e91 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  ) is never calle
11e92 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  d.  This is the 
11e93 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20  default.**      
11e94 20 20 20 20 20 20 20 20 66 6f 72 20 74 65 6d 70          for temp
11e95 6f 72 61 72 79 20 61 6e 64 20 74 72 61 6e 73 69  orary and transi
11e96 65 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a  ent files..**.**
11e97 20 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68      NORMAL    Th
11e98 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e  e journal is syn
11e99 63 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 20  ced once before 
11e9a 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20  writes begin on 
11e9b 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
11e9c 20 20 20 20 64 61 74 61 62 61 73 65 2e 20 20 54      database.  T
11e9d 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20  his is normally 
11e9e 61 64 65 71 75 61 74 65 20 70 72 6f 74 65 63 74  adequate protect
11e9f 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20  ion, but.**     
11ea0 20 20 20 20 20 20 20 20 20 69 74 20 69 73 20 74           it is t
11ea1 68 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f 73  heoretically pos
11ea2 73 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 65  sible, though ve
11ea3 72 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20  ry unlikely,.** 
11ea4 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 61               tha
11ea5 74 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20  t an inopertune 
11ea6 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f  power failure co
11ea7 75 6c 64 20 6c 65 61 76 65 20 74 68 65 20 6a 6f  uld leave the jo
11ea8 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20  urnal.**        
11ea9 20 20 20 20 20 20 69 6e 20 61 20 73 74 61 74 65        in a state
11eaa 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75   which would cau
11eab 73 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65  se damage to the
11eac 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20   database.**    
11ead 20 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 69            when i
11eae 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  t is rolled back
11eaf 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20  ..**.**    FULL 
11eb0 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c       The journal
11eb1 20 69 73 20 73 79 6e 63 65 64 20 74 77 69 63 65   is synced twice
11eb2 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 62   before writes b
11eb3 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20  egin on the.**  
11eb4 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
11eb5 62 61 73 65 20 28 77 69 74 68 20 73 6f 6d 65 20  base (with some 
11eb6 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
11eb7 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65  mation - the nRe
11eb8 63 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20  c field.**      
11eb9 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6a          of the j
11eba 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2d 20  ournal header - 
11ebb 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 69 6e  being written in
11ebc 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f   between the two
11ebd 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
11ebe 20 73 79 6e 63 73 29 2e 20 20 49 66 20 77 65 20   syncs).  If we 
11ebf 61 73 73 75 6d 65 20 74 68 61 74 20 77 72 69 74  assume that writ
11ec0 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 20  ing a.**        
11ec1 20 20 20 20 20 20 73 69 6e 67 6c 65 20 64 69 73        single dis
11ec2 6b 20 73 65 63 74 6f 72 20 69 73 20 61 74 6f 6d  k sector is atom
11ec3 69 63 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 6f  ic, then this mo
11ec4 64 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20  de provides.**  
11ec5 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 75              assu
11ec6 72 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 6a  rance that the j
11ec7 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20  ournal will not 
11ec8 62 65 20 63 6f 72 72 75 70 74 65 64 20 74 6f 20  be corrupted to 
11ec9 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
11eca 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 75      point of cau
11ecb 73 69 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 74  sing damage to t
11ecc 68 65 20 64 61 74 61 62 61 73 65 20 64 75 72 69  he database duri
11ecd 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  ng rollback..**.
11ece 2a 2a 20 4e 75 6d 65 72 69 63 20 76 61 6c 75 65  ** Numeric value
11ecf 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
11ed0 68 20 74 68 65 73 65 20 73 74 61 74 65 73 20 61  h these states a
11ed1 72 65 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41  re OFF==1, NORMA
11ed2 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c  L=2,.** and FULL
11ed3 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  =3..*/.#ifndef S
11ed4 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
11ed5 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f  _PRAGMAS.SQLITE_
11ed6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
11ed7 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65  ite3PagerSetSafe
11ed8 74 79 4c 65 76 65 6c 28 50 61 67 65 72 20 2a 70  tyLevel(Pager *p
11ed9 50 61 67 65 72 2c 20 69 6e 74 20 6c 65 76 65 6c  Pager, int level
11eda 2c 20 69 6e 74 20 62 46 75 6c 6c 46 73 79 6e 63  , int bFullFsync
11edb 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53  ){.  pPager->noS
11edc 79 6e 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31  ync =  (level==1
11edd 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70   || pPager->temp
11ede 46 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50  File) ?1:0;.  pP
11edf 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d  ager->fullSync =
11ee0 20 28 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70   (level==3 && !p
11ee1 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29  Pager->tempFile)
11ee2 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d   ?1:0;.  pPager-
11ee3 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 28 62  >sync_flags = (b
11ee4 46 75 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 54 45  FullFsync?SQLITE
11ee5 5f 53 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 54  _SYNC_FULL:SQLIT
11ee6 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a  E_SYNC_NORMAL);.
11ee7 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f    if( pPager->no
11ee8 53 79 6e 63 20 29 20 70 50 61 67 65 72 2d 3e 6e  Sync ) pPager->n
11ee9 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23  eedSync = 0;.}.#
11eea 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
11eeb 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61   following globa
11eec 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e  l variable is in
11eed 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76  cremented whenev
11eee 65 72 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a  er the library.*
11eef 2a 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6f 70  * attempts to op
11ef0 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  en a temporary f
11ef1 69 6c 65 2e 20 20 54 68 69 73 20 69 6e 66 6f 72  ile.  This infor
11ef2 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66  mation is used f
11ef3 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e  or.** testing an
11ef4 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e  d analysis only.
11ef5 20 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c    .*/.#ifdef SQL
11ef6 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
11ef7 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
11ef8 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d  opentemp_count =
11ef9 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   0;.#endif../*.*
11efa 2a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61  * Open a tempora
11efb 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57  ry file..**.** W
11efc 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65  rite the file de
11efd 73 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70  scriptor into *p
11efe 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 51 4c  File. Return SQL
11eff 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
11f00 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74  s .** or some ot
11f01 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 69  her error code i
11f02 66 20 77 65 20 66 61 69 6c 2e 20 54 68 65 20 4f  f we fail. The O
11f03 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63  S will automatic
11f04 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20  ally .** delete 
11f05 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69  the temporary fi
11f06 6c 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c  le when it is cl
11f07 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  osed..**.** The 
11f08 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20  flags passed to 
11f09 74 68 65 20 56 46 53 20 6c 61 79 65 72 20 78 4f  the VFS layer xO
11f0a 70 65 6e 28 29 20 63 61 6c 6c 20 61 72 65 20 74  pen() call are t
11f0b 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 0a 2a  hose specified.*
11f0c 2a 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 76  * by parameter v
11f0d 66 73 46 6c 61 67 73 20 4f 52 65 64 20 77 69 74  fsFlags ORed wit
11f0e 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  h the following:
11f0f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54  .**.**     SQLIT
11f10 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
11f11 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  .**     SQLITE_O
11f12 50 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20  PEN_CREATE.**   
11f13 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58    SQLITE_OPEN_EX
11f14 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 53  CLUSIVE.**     S
11f15 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
11f16 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74  EONCLOSE.*/.stat
11f17 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e  ic int pagerOpen
11f18 74 65 6d 70 28 0a 20 20 50 61 67 65 72 20 2a 70  temp(.  Pager *p
11f19 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a  Pager,        /*
11f1a 20 54 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63   The pager objec
11f1b 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  t */.  sqlite3_f
11f1c 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20  ile *pFile,  /* 
11f1d 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64  Write the file d
11f1e 65 73 63 72 69 70 74 6f 72 20 68 65 72 65 20 2a  escriptor here *
11f1f 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73  /.  int vfsFlags
11f20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
11f21 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67  gs passed throug
11f22 68 20 74 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a  h to the VFS */.
11f23 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
11f24 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
11f25 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69  turn code */..#i
11f26 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
11f27 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74  .  sqlite3_opent
11f28 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a  emp_count++;  /*
11f29 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   Used for testin
11f2a 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f  g and analysis o
11f2b 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20  nly */.#endif.. 
11f2c 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 53 51   vfsFlags |=  SQ
11f2d 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
11f2e 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  ITE | SQLITE_OPE
11f2f 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20  N_CREATE |.     
11f30 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
11f31 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 20 53  EN_EXCLUSIVE | S
11f32 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
11f33 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d  EONCLOSE;.  rc =
11f34 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
11f35 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20  Pager->pVfs, 0, 
11f36 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c  pFile, vfsFlags,
11f37 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72   0);.  assert( r
11f38 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
11f39 69 73 4f 70 65 6e 28 70 46 69 6c 65 29 20 29 3b  isOpen(pFile) );
11f3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
11f3b 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62  ./*.** Set the b
11f3c 75 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63  usy handler func
11f3d 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
11f3e 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 20 74 68  pager invokes th
11f3f 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69  e busy-handler i
11f40 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  f sqlite3OsLock(
11f41 29 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51  ) returns .** SQ
11f42 4c 49 54 45 5f 42 55 53 59 20 77 68 65 6e 20 74  LITE_BUSY when t
11f43 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65  rying to upgrade
11f44 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f   from no-lock to
11f45 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a   a SHARED lock,.
11f46 2a 2a 20 6f 72 20 77 68 65 6e 20 74 72 79 69 6e  ** or when tryin
11f47 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f  g to upgrade fro
11f48 6d 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  m a RESERVED loc
11f49 6b 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56  k to an EXCLUSIV
11f4a 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64  E .** lock. It d
11f4b 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65  oes *not* invoke
11f4c 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
11f4d 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67  r when upgrading
11f4e 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20   from.** SHARED 
11f4f 74 6f 20 52 45 53 45 52 56 45 44 2c 20 6f 72 20  to RESERVED, or 
11f50 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66  when upgrading f
11f51 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 45 58  rom SHARED to EX
11f52 43 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68 69 63  CLUSIVE.** (whic
11f53 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  h occurs during 
11f54 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c  hot-journal roll
11f55 62 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a  back). Summary:.
11f56 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74 69  **.**   Transiti
11f57 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
11f58 20 20 20 20 20 20 20 20 20 20 7c 20 49 6e 76 6f            | Invo
11f59 6b 65 73 20 78 42 75 73 79 48 61 6e 64 6c 65 72  kes xBusyHandler
11f5a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
11f5b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11f5c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11f5d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
11f5e 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20  *   NO_LOCK     
11f5f 20 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b    -> SHARED_LOCK
11f60 20 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20        | Yes.**  
11f61 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d   SHARED_LOCK   -
11f62 3e 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  > RESERVED_LOCK 
11f63 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41     | No.**   SHA
11f64 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58  RED_LOCK   -> EX
11f65 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c  CLUSIVE_LOCK   |
11f66 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56 45   No.**   RESERVE
11f67 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53  D_LOCK -> EXCLUS
11f68 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73  IVE_LOCK   | Yes
11f69 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75  .**.** If the bu
11f6a 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62  sy-handler callb
11f6b 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ack returns non-
11f6c 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69  zero, the lock i
11f6d 73 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20 49  s .** retried. I
11f6e 66 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72  f it returns zer
11f6f 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49  o, then the SQLI
11f70 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69 73  TE_BUSY error is
11f71 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 74 6f 20  .** returned to 
11f72 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 20 74 68  the caller of th
11f73 65 20 70 61 67 65 72 20 41 50 49 20 66 75 6e 63  e pager API func
11f74 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
11f75 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
11f76 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79  ite3PagerSetBusy
11f77 68 61 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 72  handler(.  Pager
11f78 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20   *pPager,       
11f79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11f7a 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  /* Pager object 
11f7b 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79  */.  int (*xBusy
11f7c 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a 29  Handler)(void *)
11f7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  ,         /* Poi
11f7e 6e 74 65 72 20 74 6f 20 62 75 73 79 2d 68 61 6e  nter to busy-han
11f7f 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  dler function */
11f80 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61  .  void *pBusyHa
11f81 6e 64 6c 65 72 41 72 67 20 20 20 20 20 20 20 20  ndlerArg        
11f82 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
11f83 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78  ent to pass to x
11f84 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 29  BusyHandler */.)
11f85 7b 20 20 0a 20 20 70 50 61 67 65 72 2d 3e 78 42  {  .  pPager->xB
11f86 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 75  usyHandler = xBu
11f87 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 61  syHandler;.  pPa
11f88 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65  ger->pBusyHandle
11f89 72 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e 64  rArg = pBusyHand
11f8a 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  lerArg;.}../*.**
11f8b 20 53 65 74 20 74 68 65 20 72 65 69 6e 69 74 69   Set the reiniti
11f8c 61 6c 69 7a 65 72 20 66 6f 72 20 74 68 69 73 20  alizer for this 
11f8d 70 61 67 65 72 2e 20 49 66 20 6e 6f 74 20 4e 55  pager. If not NU
11f8e 4c 4c 2c 20 74 68 65 20 72 65 69 6e 69 74 69 61  LL, the reinitia
11f8f 6c 69 7a 65 72 0a 2a 2a 20 69 73 20 63 61 6c 6c  lizer.** is call
11f90 65 64 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 74  ed when the cont
11f91 65 6e 74 20 6f 66 20 61 20 70 61 67 65 20 69 6e  ent of a page in
11f92 20 63 61 63 68 65 20 69 73 20 6d 6f 64 69 66 69   cache is modifi
11f93 65 64 20 28 72 65 73 74 6f 72 65 64 29 0a 2a 2a  ed (restored).**
11f94 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 74 72   as part of a tr
11f95 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 73 61 76  ansaction or sav
11f96 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e  epoint rollback.
11f97 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 67 69   The callback gi
11f98 76 65 73 20 0a 2a 2a 20 68 69 67 68 65 72 2d 6c  ves .** higher-l
11f99 65 76 65 6c 20 63 6f 64 65 20 61 6e 20 6f 70 70  evel code an opp
11f9a 6f 72 74 75 6e 69 74 79 20 74 6f 20 72 65 73 74  ortunity to rest
11f9b 6f 72 65 20 74 68 65 20 45 58 54 52 41 20 73 65  ore the EXTRA se
11f9c 63 74 69 6f 6e 20 74 6f 20 0a 2a 2a 20 61 67 72  ction to .** agr
11f9d 65 65 20 77 69 74 68 20 74 68 65 20 72 65 73 74  ee with the rest
11f9e 6f 72 65 64 20 70 61 67 65 20 64 61 74 61 2e 0a  ored page data..
11f9f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11fa0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
11fa1 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 28 50  gerSetReiniter(P
11fa2 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 76 6f  ager *pPager, vo
11fa3 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44 62  id (*xReinit)(Db
11fa4 50 61 67 65 2a 29 29 7b 0a 20 20 70 50 61 67 65  Page*)){.  pPage
11fa5 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 78  r->xReiniter = x
11fa6 52 65 69 6e 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Reinit;.}../*.**
11fa7 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65   Change the page
11fa8 20 73 69 7a 65 20 75 73 65 64 20 62 79 20 74 68   size used by th
11fa9 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20  e Pager object. 
11faa 54 68 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a  The new page siz
11fab 65 20 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20  e .** is passed 
11fac 69 6e 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a  in *pPageSize..*
11fad 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  *.** If the page
11fae 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f  r is in the erro
11faf 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69  r state when thi
11fb0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
11fb1 6c 6c 65 64 2c 20 69 74 0a 2a 2a 20 69 73 20 61  lled, it.** is a
11fb2 20 6e 6f 2d 6f 70 2e 20 54 68 65 20 76 61 6c 75   no-op. The valu
11fb3 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68  e returned is th
11fb4 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72  e error state er
11fb5 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e 20 0a  ror code (i.e. .
11fb6 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45  ** one of SQLITE
11fb7 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43  _IOERR, SQLITE_C
11fb8 4f 52 52 55 50 54 20 6f 72 20 53 51 4c 49 54 45  ORRUPT or SQLITE
11fb9 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74  _FULL)..**.** Ot
11fba 68 65 72 77 69 73 65 2c 20 69 66 20 61 6c 6c 20  herwise, if all 
11fbb 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
11fbc 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a   are true:.**.**
11fbd 20 20 20 2a 20 74 68 65 20 6e 65 77 20 70 61 67     * the new pag
11fbe 65 20 73 69 7a 65 20 28 76 61 6c 75 65 20 6f 66  e size (value of
11fbf 20 2a 70 50 61 67 65 53 69 7a 65 29 20 69 73 20   *pPageSize) is 
11fc0 76 61 6c 69 64 20 28 61 20 70 6f 77 65 72 20 0a  valid (a power .
11fc1 2a 2a 20 20 20 20 20 6f 66 20 74 77 6f 20 62 65  **     of two be
11fc2 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 53 51  tween 512 and SQ
11fc3 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
11fc4 5a 45 2c 20 69 6e 63 6c 75 73 69 76 65 29 2c 20  ZE, inclusive), 
11fc5 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68  and.**.**   * th
11fc6 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74  ere are no outst
11fc7 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65  anding page refe
11fc8 72 65 6e 63 65 73 2c 20 61 6e 64 0a 2a 2a 0a 2a  rences, and.**.*
11fc9 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 61  *   * the databa
11fca 73 65 20 69 73 20 65 69 74 68 65 72 20 6e 6f 74  se is either not
11fcb 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
11fcc 74 61 62 61 73 65 20 6f 72 20 69 74 20 69 73 0a  tabase or it is.
11fcd 2a 2a 20 20 20 20 20 61 6e 20 69 6e 2d 6d 65 6d  **     an in-mem
11fce 6f 72 79 20 64 61 74 61 62 61 73 65 20 74 68 61  ory database tha
11fcf 74 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73  t currently cons
11fd0 69 73 74 73 20 6f 66 20 7a 65 72 6f 20 70 61 67  ists of zero pag
11fd1 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74  es..**.** then t
11fd2 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20  he pager object 
11fd3 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 65 74  page size is set
11fd4 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a   to *pPageSize..
11fd5 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  **.** If the pag
11fd6 65 20 73 69 7a 65 20 69 73 20 63 68 61 6e 67 65  e size is change
11fd7 64 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  d, then this fun
11fd8 63 74 69 6f 6e 20 75 73 65 73 20 73 71 6c 69 74  ction uses sqlit
11fd9 65 33 50 61 67 65 72 4d 61 6c 6c 6f 63 28 29 20  e3PagerMalloc() 
11fda 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  .** to obtain a 
11fdb 6e 65 77 20 50 61 67 65 72 2e 70 54 6d 70 53 70  new Pager.pTmpSp
11fdc 61 63 65 20 62 75 66 66 65 72 2e 20 49 66 20 74  ace buffer. If t
11fdd 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  his allocation a
11fde 74 74 65 6d 70 74 20 0a 2a 2a 20 66 61 69 6c 73  ttempt .** fails
11fdf 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  , SQLITE_NOMEM i
11fe0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
11fe1 68 65 20 70 61 67 65 20 73 69 7a 65 20 72 65 6d  he page size rem
11fe2 61 69 6e 73 20 75 6e 63 68 61 6e 67 65 64 2e 20  ains unchanged. 
11fe3 0a 2a 2a 20 49 6e 20 61 6c 6c 20 6f 74 68 65 72  .** In all other
11fe4 20 63 61 73 65 73 2c 20 53 51 4c 49 54 45 5f 4f   cases, SQLITE_O
11fe5 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  K is returned..*
11fe6 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  *.** If the page
11fe7 20 73 69 7a 65 20 69 73 20 6e 6f 74 20 63 68 61   size is not cha
11fe8 6e 67 65 64 2c 20 65 69 74 68 65 72 20 62 65 63  nged, either bec
11fe9 61 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 65 20  ause one of the 
11fea 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20 63 6f  enumerated.** co
11feb 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 69  nditions above i
11fec 73 20 6e 6f 74 20 74 72 75 65 2c 20 74 68 65 20  s not true, the 
11fed 70 61 67 65 72 20 77 61 73 20 69 6e 20 65 72 72  pager was in err
11fee 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68  or state when th
11fef 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77  is.** function w
11ff0 61 73 20 63 61 6c 6c 65 64 2c 20 6f 72 20 62 65  as called, or be
11ff1 63 61 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79  cause the memory
11ff2 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65   allocation atte
11ff3 6d 70 74 20 66 61 69 6c 65 64 2c 20 0a 2a 2a 20  mpt failed, .** 
11ff4 74 68 65 6e 20 2a 70 50 61 67 65 53 69 7a 65 20  then *pPageSize 
11ff5 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f 6c  is set to the ol
11ff6 64 2c 20 72 65 74 61 69 6e 65 64 20 70 61 67 65  d, retained page
11ff7 20 73 69 7a 65 20 62 65 66 6f 72 65 20 72 65 74   size before ret
11ff8 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  urning..*/.SQLIT
11ff9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
11ffa 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67  lite3PagerSetPag
11ffb 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61  esize(Pager *pPa
11ffc 67 65 72 2c 20 75 31 36 20 2a 70 50 61 67 65 53  ger, u16 *pPageS
11ffd 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ize){.  int rc =
11ffe 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11fff 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
12000 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 31 36  TE_OK ){.    u16
12001 20 70 61 67 65 53 69 7a 65 20 3d 20 2a 70 50 61   pageSize = *pPa
12002 67 65 53 69 7a 65 3b 0a 20 20 20 20 61 73 73 65  geSize;.    asse
12003 72 74 28 20 70 61 67 65 53 69 7a 65 3d 3d 30 20  rt( pageSize==0 
12004 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e 3d 35 31  || (pageSize>=51
12005 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53  2 && pageSize<=S
12006 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
12007 49 5a 45 29 20 29 3b 0a 20 20 20 20 69 66 28 20  IZE) );.    if( 
12008 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67 65  pageSize && page
12009 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70 61  Size!=pPager->pa
1200a 67 65 53 69 7a 65 20 0a 20 20 20 20 20 26 26 20  geSize .     && 
1200b 28 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d  (pPager->memDb==
1200c 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 53  0 || pPager->dbS
1200d 69 7a 65 3d 3d 30 29 0a 20 20 20 20 20 26 26 20  ize==0).     && 
1200e 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
1200f 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50  Count(pPager->pP
12010 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20 29  Cache)==0 .    )
12011 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 4e  {.      char *pN
12012 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  ew = (char *)sql
12013 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 70  ite3PageMalloc(p
12014 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  ageSize);.      
12015 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20  if( !pNew ){.   
12016 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
12017 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65  _NOMEM;.      }e
12018 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 61 67  lse{.        pag
12019 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29  er_reset(pPager)
1201a 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  ;.        pPager
1201b 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67  ->pageSize = pag
1201c 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 73  eSize;.        s
1201d 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70  qlite3PageFree(p
1201e 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
1201f 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65  );.        pPage
12020 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 70  r->pTmpSpace = p
12021 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  New;.        sql
12022 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67  ite3PcacheSetPag
12023 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50  eSize(pPager->pP
12024 43 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65 29  Cache, pageSize)
12025 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
12026 20 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20 3d      *pPageSize =
12027 20 28 75 31 36 29 70 50 61 67 65 72 2d 3e 70 61   (u16)pPager->pa
12028 67 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65  geSize;.  }.  re
12029 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1202a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
1202b 65 72 20 74 6f 20 74 68 65 20 22 74 65 6d 70 6f  er to the "tempo
1202c 72 61 72 79 20 70 61 67 65 22 20 62 75 66 66 65  rary page" buffe
1202d 72 20 68 65 6c 64 20 69 6e 74 65 72 6e 61 6c 6c  r held internall
1202e 79 0a 2a 2a 20 62 79 20 74 68 65 20 70 61 67 65  y.** by the page
1202f 72 2e 20 20 54 68 69 73 20 69 73 20 61 20 62 75  r.  This is a bu
12030 66 66 65 72 20 74 68 61 74 20 69 73 20 62 69 67  ffer that is big
12031 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20   enough to hold 
12032 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 63 6f  the.** entire co
12033 6e 74 65 6e 74 20 6f 66 20 61 20 64 61 74 61 62  ntent of a datab
12034 61 73 65 20 70 61 67 65 2e 20 20 54 68 69 73 20  ase page.  This 
12035 62 75 66 66 65 72 20 69 73 20 75 73 65 64 20 69  buffer is used i
12036 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72  nternally.** dur
12037 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64  ing rollback and
12038 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69   will be overwri
12039 74 74 65 6e 20 77 68 65 6e 65 76 65 72 20 61 20  tten whenever a 
1203a 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75  rollback.** occu
1203b 72 73 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d  rs.  But other m
1203c 6f 64 75 6c 65 73 20 61 72 65 20 66 72 65 65 20  odules are free 
1203d 74 6f 20 75 73 65 20 69 74 20 74 6f 6f 2c 20 61  to use it too, a
1203e 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20  s long as.** no 
1203f 72 6f 6c 6c 62 61 63 6b 73 20 61 72 65 20 68 61  rollbacks are ha
12040 70 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  ppening..*/.SQLI
12041 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12042 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d  *sqlite3PagerTem
12043 70 53 70 61 63 65 28 50 61 67 65 72 20 2a 70 50  pSpace(Pager *pP
12044 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
12045 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
12046 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  e;.}../*.** Atte
12047 6d 70 74 20 74 6f 20 73 65 74 20 74 68 65 20 6d  mpt to set the m
12048 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20  aximum database 
12049 70 61 67 65 20 63 6f 75 6e 74 20 69 66 20 6d 78  page count if mx
1204a 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65  Page is positive
1204b 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68  . .** Make no ch
1204c 61 6e 67 65 73 20 69 66 20 6d 78 50 61 67 65 20  anges if mxPage 
1204d 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74  is zero or negat
1204e 69 76 65 2e 20 20 41 6e 64 20 6e 65 76 65 72 20  ive.  And never 
1204f 72 65 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d 61  reduce the.** ma
12050 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74  ximum page count
12051 20 62 65 6c 6f 77 20 74 68 65 20 63 75 72 72 65   below the curre
12052 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  nt size of the d
12053 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52  atabase..**.** R
12054 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6d 78 50  egardless of mxP
12055 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20  age, return the 
12056 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20  current maximum 
12057 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53  page count..*/.S
12058 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
12059 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61  t sqlite3PagerMa
1205a 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67 65 72  xPageCount(Pager
1205b 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78   *pPager, int mx
1205c 50 61 67 65 29 7b 0a 20 20 69 66 28 20 6d 78 50  Page){.  if( mxP
1205d 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 70 50 61  age>0 ){.    pPa
1205e 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78  ger->mxPgno = mx
1205f 50 61 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Page;.  }.  sqli
12060 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
12061 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20  t(pPager, 0);.  
12062 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6d  return pPager->m
12063 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  xPgno;.}../*.** 
12064 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65  The following se
12065 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72  t of routines ar
12066 65 20 75 73 65 64 20 74 6f 20 64 69 73 61 62 6c  e used to disabl
12067 65 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 0a  e the simulated.
12068 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 63  ** I/O error mec
12069 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 72  hanism.  These r
1206a 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
1206b 20 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c 61   to avoid simula
1206c 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 6e  ted.** errors in
1206d 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 77 65   places where we
1206e 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f   do not care abo
1206f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a  ut errors..**.**
12070 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 45   Unless -DSQLITE
12071 5f 54 45 53 54 3d 31 20 69 73 20 75 73 65 64 2c  _TEST=1 is used,
12072 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
12073 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a  are all no-ops.*
12074 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 6e  * and generate n
12075 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65  o code..*/.#ifde
12076 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
12077 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20  LITE_API extern 
12078 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
12079 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 53 51  rror_pending;.SQ
1207a 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20  LITE_API extern 
1207b 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
1207c 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61 74 69 63  rror_hit;.static
1207d 20 69 6e 74 20 73 61 76 65 64 5f 63 6e 74 3b 0a   int saved_cnt;.
1207e 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d  void disable_sim
1207f 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
12080 28 76 6f 69 64 29 7b 0a 20 20 73 61 76 65 64 5f  (void){.  saved_
12081 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 69 6f  cnt = sqlite3_io
12082 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a  _error_pending;.
12083 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
12084 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 2d 31 3b  or_pending = -1;
12085 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73  .}.void enable_s
12086 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
12087 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  rs(void){.  sqli
12088 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
12089 64 69 6e 67 20 3d 20 73 61 76 65 64 5f 63 6e 74  ding = saved_cnt
1208a 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  ;.}.#else.# defi
1208b 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c  ne disable_simul
1208c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
1208d 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65  .# define enable
1208e 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
1208f 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 2f  rors().#endif../
12090 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 66 69  *.** Read the fi
12091 72 73 74 20 4e 20 62 79 74 65 73 20 66 72 6f 6d  rst N bytes from
12092 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
12093 66 20 74 68 65 20 66 69 6c 65 20 69 6e 74 6f 20  f the file into 
12094 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 20 70  memory.** that p
12095 44 65 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20  Dest points to. 
12096 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
12097 67 65 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f  ger was opened o
12098 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69  n a transient fi
12099 6c 65 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 22  le (zFilename=="
1209a 22 29 2c 20 6f 72 0a 2a 2a 20 6f 70 65 6e 65 64  "), or.** opened
1209b 20 6f 6e 20 61 20 66 69 6c 65 20 6c 65 73 73 20   on a file less 
1209c 74 68 61 6e 20 4e 20 62 79 74 65 73 20 69 6e 20  than N bytes in 
1209d 73 69 7a 65 2c 20 74 68 65 20 6f 75 74 70 75 74  size, the output
1209e 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 7a 65   buffer is.** ze
1209f 72 6f 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f  roed and SQLITE_
120a0 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65  OK returned. The
120a1 20 72 61 74 69 6f 6e 61 6c 65 20 66 6f 72 20 74   rationale for t
120a2 68 69 73 20 69 73 20 74 68 61 74 20 74 68 69 73  his is that this
120a3 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73   .** function is
120a4 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 61   used to read da
120a5 74 61 62 61 73 65 20 68 65 61 64 65 72 73 2c 20  tabase headers, 
120a6 61 6e 64 20 61 20 6e 65 77 20 74 72 61 6e 73 69  and a new transi
120a7 65 6e 74 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 73  ent or.** zero s
120a8 69 7a 65 64 20 64 61 74 61 62 61 73 65 20 68 61  ized database ha
120a9 73 20 61 20 68 65 61 64 65 72 20 74 68 61 6e 20  s a header than 
120aa 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65 6c  consists entirel
120ab 79 20 6f 66 20 7a 65 72 6f 65 73 2e 0a 2a 2a 0a  y of zeroes..**.
120ac 2a 2a 20 49 66 20 61 6e 79 20 49 4f 20 65 72 72  ** If any IO err
120ad 6f 72 20 61 70 61 72 74 20 66 72 6f 6d 20 53 51  or apart from SQ
120ae 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
120af 5f 52 45 41 44 20 69 73 20 65 6e 63 6f 75 6e 74  _READ is encount
120b0 65 72 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72  ered,.** the err
120b1 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
120b2 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ned to the calle
120b3 72 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e  r and the conten
120b4 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 75 74  ts of the.** out
120b5 70 75 74 20 62 75 66 66 65 72 20 75 6e 64 65 66  put buffer undef
120b6 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ined..*/.SQLITE_
120b7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
120b8 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c 65  te3PagerReadFile
120b9 68 65 61 64 65 72 28 50 61 67 65 72 20 2a 70 50  header(Pager *pP
120ba 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20 75 6e 73  ager, int N, uns
120bb 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44 65 73  igned char *pDes
120bc 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
120bd 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65 6d 73  QLITE_OK;.  mems
120be 65 74 28 70 44 65 73 74 2c 20 30 2c 20 4e 29 3b  et(pDest, 0, N);
120bf 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65  .  assert( isOpe
120c0 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c  n(pPager->fd) ||
120c1 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
120c2 65 20 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65  e );.  if( isOpe
120c3 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b  n(pPager->fd) ){
120c4 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 44  .    IOTRACE(("D
120c5 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e 22 2c  BHDR %p 0 %d\n",
120c6 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 20 20   pPager, N)).   
120c7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
120c8 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ead(pPager->fd, 
120c9 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a 20 20  pDest, N, 0);.  
120ca 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
120cb 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
120cc 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  D ){.      rc = 
120cd 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
120ce 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
120cf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
120d0 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  n the total numb
120d1 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
120d2 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
120d3 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a 20   associated .** 
120d4 77 69 74 68 20 70 50 61 67 65 72 2e 20 4e 6f 72  with pPager. Nor
120d5 6d 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63  mally, this is c
120d6 61 6c 63 75 6c 61 74 65 64 20 61 73 20 28 3c 64  alculated as (<d
120d7 62 20 66 69 6c 65 20 73 69 7a 65 3e 2f 3c 70 61  b file size>/<pa
120d8 67 65 2d 73 69 7a 65 3e 29 2e 0a 2a 2a 20 48 6f  ge-size>)..** Ho
120d9 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 66 69  wever, if the fi
120da 6c 65 20 69 73 20 62 65 74 77 65 65 6e 20 31 20  le is between 1 
120db 61 6e 64 20 3c 70 61 67 65 2d 73 69 7a 65 3e 20  and <page-size> 
120dc 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74  bytes in size, t
120dd 68 65 6e 20 0a 2a 2a 20 74 68 69 73 20 69 73 20  hen .** this is 
120de 63 6f 6e 73 69 64 65 72 65 64 20 61 20 31 20 70  considered a 1 p
120df 61 67 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  age file..**.** 
120e0 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
120e1 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 77  in error state w
120e2 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
120e3 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65  n is called, the
120e4 6e 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73  n the.** error s
120e5 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  tate error code 
120e6 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
120e7 2a 70 6e 50 61 67 65 20 6c 65 66 74 20 75 6e 63  *pnPage left unc
120e8 68 61 6e 67 65 64 2e 20 4f 72 2c 0a 2a 2a 20 69  hanged. Or,.** i
120e9 66 20 74 68 65 20 66 69 6c 65 20 73 79 73 74 65  f the file syste
120ea 6d 20 68 61 73 20 74 6f 20 62 65 20 71 75 65 72  m has to be quer
120eb 69 65 64 20 66 6f 72 20 74 68 65 20 73 69 7a 65  ied for the size
120ec 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64   of the file and
120ed 0a 2a 2a 20 74 68 65 20 71 75 65 72 79 20 61 74  .** the query at
120ee 74 65 6d 70 74 20 72 65 74 75 72 6e 73 20 61 6e  tempt returns an
120ef 20 49 4f 20 65 72 72 6f 72 2c 20 74 68 65 20 49   IO error, the I
120f0 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  O error code is 
120f1 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20  returned.** and 
120f2 2a 70 6e 50 61 67 65 20 69 73 20 6c 65 66 74 20  *pnPage is left 
120f3 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  unchanged..**.**
120f4 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 65   Otherwise, if e
120f5 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63  verything is suc
120f6 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 53 51  cessful, then SQ
120f7 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
120f8 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61  ned.** and *pnPa
120f9 67 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ge is set to the
120fa 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
120fb 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
120fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
120fd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
120fe 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61  agerPagecount(Pa
120ff 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
12100 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20 50 67 6e   *pnPage){.  Pgn
12101 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20  o nPage;        
12102 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
12103 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20 2a 70  to return via *p
12104 6e 50 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49  nPage */..  /* I
12105 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61  f the pager is a
12106 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72  lready in the er
12107 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74 75 72  ror state, retur
12108 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  n the error code
12109 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65  . */.  if( pPage
1210a 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20  r->errCode ){.  
1210b 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
1210c 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20  >errCode;.  }.. 
1210d 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68   /* Determine th
1210e 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
1210f 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 53  s in the file. S
12110 74 6f 72 65 20 74 68 69 73 20 69 6e 20 6e 50 61  tore this in nPa
12111 67 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ge. */.  if( pPa
12112 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
12113 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20   ){.    nPage = 
12114 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a  pPager->dbSize;.
12115 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
12116 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
12117 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 72 65       /* Error re
12118 74 75 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65  turned by OsFile
12119 53 69 7a 65 28 29 20 2a 2f 0a 20 20 20 20 69 36  Size() */.    i6
1211a 34 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20  4 n = 0;        
1211b 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 73 69        /* File si
1211c 7a 65 20 69 6e 20 62 79 74 65 73 20 72 65 74 75  ze in bytes retu
1211d 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69  rned by OsFileSi
1211e 7a 65 28 29 20 2a 2f 0a 0a 20 20 20 20 61 73 73  ze() */..    ass
1211f 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67  ert( isOpen(pPag
12120 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65  er->fd) || pPage
12121 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20  r->tempFile );. 
12122 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50     if( isOpen(pP
12123 61 67 65 72 2d 3e 66 64 29 20 26 26 20 28 30 20  ager->fd) && (0 
12124 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  != (rc = sqlite3
12125 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65  OsFileSize(pPage
12126 72 2d 3e 66 64 2c 20 26 6e 29 29 29 20 29 7b 0a  r->fd, &n))) ){.
12127 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f        pager_erro
12128 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20  r(pPager, rc);. 
12129 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1212a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 3e      }.    if( n>
1212b 30 20 26 26 20 6e 3c 70 50 61 67 65 72 2d 3e 70  0 && n<pPager->p
1212c 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  ageSize ){.     
1212d 20 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20 20 20   nPage = 1;.    
1212e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61  }else{.      nPa
1212f 67 65 20 3d 20 28 50 67 6e 6f 29 28 6e 20 2f 20  ge = (Pgno)(n / 
12130 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
12131 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
12132 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d   pPager->state!=
12133 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a  PAGER_UNLOCK ){.
12134 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62        pPager->db
12135 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20  Size = nPage;.  
12136 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69      pPager->dbFi
12137 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a  leSize = nPage;.
12138 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62        pPager->db
12139 53 69 7a 65 56 61 6c 69 64 20 3d 20 31 3b 0a 20  SizeValid = 1;. 
1213a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
1213b 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75  f the current nu
1213c 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
1213d 20 74 68 65 20 66 69 6c 65 20 69 73 20 67 72 65   the file is gre
1213e 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20  ater than the . 
1213f 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 6d   ** configured m
12140 61 78 69 6d 75 6d 20 70 61 67 65 72 20 6e 75 6d  aximum pager num
12141 62 65 72 2c 20 69 6e 63 72 65 61 73 65 20 74 68  ber, increase th
12142 65 20 61 6c 6c 6f 77 65 64 20 6c 69 6d 69 74 20  e allowed limit 
12143 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65  so.  ** that the
12144 20 66 69 6c 65 20 63 61 6e 20 62 65 20 72 65 61   file can be rea
12145 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 50  d..  */.  if( nP
12146 61 67 65 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67  age>pPager->mxPg
12147 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  no ){.    pPager
12148 2d 3e 6d 78 50 67 6e 6f 20 3d 20 28 50 67 6e 6f  ->mxPgno = (Pgno
12149 29 6e 50 61 67 65 3b 0a 20 20 7d 0a 0a 20 20 2f  )nPage;.  }..  /
1214a 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70 75 74  * Set the output
1214b 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 72 65   variable and re
1214c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 2a  turn SQLITE_OK *
1214d 2f 0a 20 20 69 66 28 20 70 6e 50 61 67 65 20 29  /.  if( pnPage )
1214e 7b 0a 20 20 20 20 2a 70 6e 50 61 67 65 20 3d 20  {.    *pnPage = 
1214f 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 72 65 74  nPage;.  }.  ret
12150 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
12151 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20  .../*.** Try to 
12152 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66  obtain a lock of
12153 20 74 79 70 65 20 6c 6f 63 6b 74 79 70 65 20 6f   type locktype o
12154 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
12155 69 6c 65 2e 20 49 66 0a 2a 2a 20 61 20 73 69 6d  ile. If.** a sim
12156 69 6c 61 72 20 6f 72 20 67 72 65 61 74 65 72 20  ilar or greater 
12157 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20  lock is already 
12158 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 74  held, this funct
12159 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 0a 2a  ion is a no-op.*
1215a 2a 20 28 72 65 74 75 72 6e 69 6e 67 20 53 51 4c  * (returning SQL
1215b 49 54 45 5f 4f 4b 20 69 6d 6d 65 64 69 61 74 65  ITE_OK immediate
1215c 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72  ly)..**.** Other
1215d 77 69 73 65 2c 20 61 74 74 65 6d 70 74 20 74 6f  wise, attempt to
1215e 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b   obtain the lock
1215f 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73   using sqlite3Os
12160 4c 6f 63 6b 28 29 2e 20 49 6e 76 6f 6b 65 20 0a  Lock(). Invoke .
12161 2a 2a 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  ** the busy call
12162 62 61 63 6b 20 69 66 20 74 68 65 20 6c 6f 63 6b  back if the lock
12163 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f   is currently no
12164 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 52 65 70  t available. Rep
12165 65 61 74 20 0a 2a 2a 20 75 6e 74 69 6c 20 74 68  eat .** until th
12166 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  e busy callback 
12167 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20 6f 72  returns false or
12168 20 75 6e 74 69 6c 20 74 68 65 20 61 74 74 65 6d   until the attem
12169 70 74 20 74 6f 20 0a 2a 2a 20 6f 62 74 61 69 6e  pt to .** obtain
1216a 20 74 68 65 20 6c 6f 63 6b 20 73 75 63 63 65 65   the lock succee
1216b 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ds..**.** Return
1216c 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
1216d 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72  ccess and an err
1216e 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 63 61  or code if we ca
1216f 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74  nnot obtain.** t
12170 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20  he lock. If the 
12171 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64  lock is obtained
12172 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 73   successfully, s
12173 65 74 20 74 68 65 20 50 61 67 65 72 2e 73 74 61  et the Pager.sta
12174 74 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20  te .** variable 
12175 74 6f 20 6c 6f 63 6b 74 79 70 65 20 62 65 66 6f  to locktype befo
12176 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f  re returning..*/
12177 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
12178 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50  r_wait_on_lock(P
12179 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
1217a 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69  t locktype){.  i
1217b 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
1217c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1217d 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
1217e 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20  de */..  /* The 
1217f 4f 53 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 6d  OS lock values m
12180 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  ust be the same 
12181 61 73 20 74 68 65 20 50 61 67 65 72 20 6c 6f 63  as the Pager loc
12182 6b 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 61 73  k values */.  as
12183 73 65 72 74 28 20 50 41 47 45 52 5f 53 48 41 52  sert( PAGER_SHAR
12184 45 44 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ED==SHARED_LOCK 
12185 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
12186 45 52 5f 52 45 53 45 52 56 45 44 3d 3d 52 45 53  ER_RESERVED==RES
12187 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  ERVED_LOCK );.  
12188 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 45 58  assert( PAGER_EX
12189 43 4c 55 53 49 56 45 3d 3d 45 58 43 4c 55 53 49  CLUSIVE==EXCLUSI
1218a 56 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a  VE_LOCK );..  /*
1218b 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   If the file is 
1218c 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b  currently unlock
1218d 65 64 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65  ed then the size
1218e 20 6d 75 73 74 20 62 65 20 75 6e 6b 6e 6f 77 6e   must be unknown
1218f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
12190 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
12191 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 61  ER_SHARED || pPa
12192 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
12193 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65  ==0 );..  /* Che
12194 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 20  ck that this is 
12195 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f 70 20 28  either a no-op (
12196 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75  because the requ
12197 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 20 0a 20  ested lock is . 
12198 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64   ** already held
12199 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20  , or one of the 
1219a 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 74 68 61  transistions tha
1219b 74 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c  t the busy-handl
1219c 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69  er.  ** may be i
1219d 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 2c 20 61  nvoked during, a
1219e 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
1219f 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 20 20  comment above.  
121a0 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  ** sqlite3PagerS
121a1 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 29 2e  etBusyhandler().
121a2 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
121a3 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d  (pPager->state>=
121a4 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 20 20 20  locktype).      
121a5 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74 61   || (pPager->sta
121a6 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  te==PAGER_UNLOCK
121a7 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41   && locktype==PA
121a8 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20  GER_SHARED).    
121a9 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73     || (pPager->s
121aa 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45  tate==PAGER_RESE
121ab 52 56 45 44 20 26 26 20 6c 6f 63 6b 74 79 70 65  RVED && locktype
121ac 3d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  ==PAGER_EXCLUSIV
121ad 45 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 70  E).  );..  if( p
121ae 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f  Pager->state>=lo
121af 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 63  cktype ){.    rc
121b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
121b1 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b 0a  }else{.    do {.
121b2 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
121b3 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d  e3OsLock(pPager-
121b4 3e 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a  >fd, locktype);.
121b5 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d      }while( rc==
121b6 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70  SQLITE_BUSY && p
121b7 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64  Pager->xBusyHand
121b8 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 75 73  ler(pPager->pBus
121b9 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29 3b 0a  yHandlerArg) );.
121ba 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
121bb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
121bc 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 28  Pager->state = (
121bd 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20  u8)locktype;.   
121be 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c 4f 43     IOTRACE(("LOC
121bf 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  K %p %d\n", pPag
121c0 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29 29 0a 20  er, locktype)). 
121c1 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
121c2 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
121c3 72 75 6e 63 61 74 65 20 74 68 65 20 69 6e 2d 6d  runcate the in-m
121c4 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 66  emory database f
121c5 69 6c 65 20 69 6d 61 67 65 20 74 6f 20 6e 50 61  ile image to nPa
121c6 67 65 20 70 61 67 65 73 2e 20 54 68 69 73 20 0a  ge pages. This .
121c7 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  ** function does
121c8 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f   not actually mo
121c9 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73  dify the databas
121ca 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20  e file on disk. 
121cb 49 74 20 0a 2a 2a 20 6a 75 73 74 20 73 65 74 73  It .** just sets
121cc 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74   the internal st
121cd 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 65 72  ate of the pager
121ce 20 6f 62 6a 65 63 74 20 73 6f 20 74 68 61 74 20   object so that 
121cf 74 68 65 20 0a 2a 2a 20 74 72 75 6e 63 61 74 69  the .** truncati
121d0 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 6e 65 20  on will be done 
121d1 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74  when the current
121d2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
121d3 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a 53 51  committed..*/.SQ
121d4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
121d5 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72  d sqlite3PagerTr
121d6 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61 67 65  uncateImage(Page
121d7 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20  r *pPager, Pgno 
121d8 6e 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74  nPage){.  assert
121d9 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
121da 56 61 6c 69 64 20 29 3b 0a 20 20 61 73 73 65 72  Valid );.  asser
121db 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  t( pPager->dbSiz
121dc 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 20 61 73  e>=nPage );.  as
121dd 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
121de 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52  ate>=PAGER_RESER
121df 56 45 44 20 29 3b 0a 20 20 70 50 61 67 65 72 2d  VED );.  pPager-
121e0 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b  >dbSize = nPage;
121e1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f  .}../*.** Shutdo
121e2 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  wn the page cach
121e3 65 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d  e.  Free all mem
121e4 6f 72 79 20 61 6e 64 20 63 6c 6f 73 65 20 61 6c  ory and close al
121e5 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  l files..**.** I
121e6 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
121e7 77 61 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20  was in progress 
121e8 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
121e9 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 61  e is called, tha
121ea 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  t.** transaction
121eb 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   is rolled back.
121ec 20 20 41 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e    All outstandin
121ed 67 20 70 61 67 65 73 20 61 72 65 20 69 6e 76 61  g pages are inva
121ee 6c 69 64 61 74 65 64 0a 2a 2a 20 61 6e 64 20 74  lidated.** and t
121ef 68 65 69 72 20 6d 65 6d 6f 72 79 20 69 73 20 66  heir memory is f
121f0 72 65 65 64 2e 20 20 41 6e 79 20 61 74 74 65 6d  reed.  Any attem
121f1 70 74 20 74 6f 20 75 73 65 20 61 20 70 61 67 65  pt to use a page
121f2 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
121f3 69 74 68 20 74 68 69 73 20 70 61 67 65 20 63 61  ith this page ca
121f4 63 68 65 20 61 66 74 65 72 20 74 68 69 73 20 66  che after this f
121f5 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
121f6 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72  will likely.** r
121f7 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 65 64  esult in a cored
121f8 75 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ump..**.** This 
121f9 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20  function always 
121fa 73 75 63 63 65 65 64 73 2e 20 49 66 20 61 20 74  succeeds. If a t
121fb 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
121fc 74 69 76 65 20 61 6e 20 61 74 74 65 6d 70 74 0a  tive an attempt.
121fd 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 72 6f  ** is made to ro
121fe 6c 6c 20 69 74 20 62 61 63 6b 2e 20 49 66 20 61  ll it back. If a
121ff 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  n error occurs d
12200 75 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61  uring the rollba
12201 63 6b 20 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f 75  ck .** a hot jou
12202 72 6e 61 6c 20 6d 61 79 20 62 65 20 6c 65 66 74  rnal may be left
12203 20 69 6e 20 74 68 65 20 66 69 6c 65 73 79 73 74   in the filesyst
12204 65 6d 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20  em but no error 
12205 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 74  is returned.** t
12206 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f  o the caller..*/
12207 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12208 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
12209 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61  Close(Pager *pPa
1220a 67 65 72 29 7b 0a 20 20 64 69 73 61 62 6c 65 5f  ger){.  disable_
1220b 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
1220c 6f 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33  ors();.  sqlite3
1220d 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
1220e 63 28 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65  c();.  pPager->e
1220f 72 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20 70 50  rrCode = 0;.  pP
12210 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
12211 6f 64 65 20 3d 20 30 3b 0a 20 20 70 61 67 65 72  ode = 0;.  pager
12212 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a  _reset(pPager);.
12213 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20    if( MEMDB ){. 
12214 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28     pager_unlock(
12215 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65  pPager);.  }else
12216 7b 0a 20 20 20 20 2f 2a 20 53 65 74 20 50 61 67  {.    /* Set Pag
12217 65 72 2e 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f  er.journalHdr to
12218 20 2d 31 20 66 6f 72 20 74 68 65 20 62 65 6e 65   -1 for the bene
12219 66 69 74 20 6f 66 20 74 68 65 20 70 61 67 65 72  fit of the pager
1221a 5f 70 6c 61 79 62 61 63 6b 28 29 20 0a 20 20 20  _playback() .   
1221b 20 2a 2a 20 63 61 6c 6c 20 77 68 69 63 68 20 6d   ** call which m
1221c 61 79 20 62 65 20 6d 61 64 65 20 66 72 6f 6d 20  ay be made from 
1221d 77 69 74 68 69 6e 20 70 61 67 65 72 55 6e 6c 6f  within pagerUnlo
1221e 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 29 2e  ckAndRollback().
1221f 20 49 66 20 69 74 0a 20 20 20 20 2a 2a 20 69 73   If it.    ** is
12220 20 6e 6f 74 20 2d 31 2c 20 74 68 65 6e 20 74 68   not -1, then th
12221 65 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 69  e unsynced porti
12222 6f 6e 20 6f 66 20 61 6e 20 6f 70 65 6e 20 6a 6f  on of an open jo
12223 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 0a 20  urnal file may. 
12224 20 20 20 2a 2a 20 62 65 20 70 6c 61 79 65 64 20     ** be played 
12225 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61  back into the da
12226 74 61 62 61 73 65 2e 20 49 66 20 61 20 70 6f 77  tabase. If a pow
12227 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72  er failure occur
12228 73 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74  s while.    ** t
12229 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67  his is happening
1222a 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d  , the database m
1222b 61 79 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70  ay become corrup
1222c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50  t..    */.    pP
1222d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
1222e 20 3d 20 2d 31 3b 0a 20 20 20 20 70 61 67 65 72   = -1;.    pager
1222f 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63  UnlockAndRollbac
12230 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20  k(pPager);.  }. 
12231 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
12232 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e 61  nMalloc();.  ena
12233 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
12234 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 41 47  _errors();.  PAG
12235 45 52 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20  ERTRACE(("CLOSE 
12236 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
12237 50 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 52  Pager)));.  IOTR
12238 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c 6e  ACE(("CLOSE %p\n
12239 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 73 71  ", pPager)).  sq
1223a 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
1223b 67 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 6c 69  ger->fd);.  sqli
1223c 74 65 33 50 61 67 65 46 72 65 65 28 70 50 61 67  te3PageFree(pPag
1223d 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a  er->pTmpSpace);.
1223e 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43    sqlite3PcacheC
1223f 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 70 50 43  lose(pPager->pPC
12240 61 63 68 65 29 3b 0a 0a 20 20 61 73 73 65 72 74  ache);..  assert
12241 28 20 21 70 50 61 67 65 72 2d 3e 61 53 61 76 65  ( !pPager->aSave
12242 70 6f 69 6e 74 20 26 26 20 21 70 50 61 67 65 72  point && !pPager
12243 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b 0a  ->pInJournal );.
12244 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65    assert( !isOpe
12245 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26  n(pPager->jfd) &
12246 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  & !isOpen(pPager
12247 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73 71  ->sjfd) );..  sq
12248 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65  lite3_free(pPage
12249 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  r);.  return SQL
1224a 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 21  ITE_OK;.}..#if !
1224b 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20  defined(NDEBUG) 
1224c 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
1224d 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65  E_TEST)./*.** Re
1224e 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 6e 75  turn the page nu
1224f 6d 62 65 72 20 66 6f 72 20 70 61 67 65 20 70 50  mber for page pP
12250 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
12251 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 65  VATE Pgno sqlite
12252 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72  3PagerPagenumber
12253 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20  (DbPage *pPg){. 
12254 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 67 6e   return pPg->pgn
12255 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  o;.}.#endif../*.
12256 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  ** Increment the
12257 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
12258 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a   for page pPg..*
12259 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1225a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
1225b 65 72 52 65 66 28 44 62 50 61 67 65 20 2a 70 50  erRef(DbPage *pP
1225c 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61  g){.  sqlite3Pca
1225d 63 68 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a 0a  cheRef(pPg);.}..
1225e 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 6a  /*.** Sync the j
1225f 6f 75 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65 72  ournal. In other
12260 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75 72   words, make sur
12261 65 20 61 6c 6c 20 74 68 65 20 70 61 67 65 73 20  e all the pages 
12262 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 62 65 65  that have.** bee
12263 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  n written to the
12264 20 6a 6f 75 72 6e 61 6c 20 68 61 76 65 20 61 63   journal have ac
12265 74 75 61 6c 6c 79 20 72 65 61 63 68 65 64 20 74  tually reached t
12266 68 65 20 73 75 72 66 61 63 65 20 6f 66 20 74 68  he surface of th
12267 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63 61  e.** disk and ca
12268 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 69 6e  n be restored in
12269 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20   the event of a 
1226a 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c  hot-journal roll
1226b 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  back..**.** If t
1226c 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e  he Pager.needSyn
1226d 63 20 66 6c 61 67 20 69 73 20 6e 6f 74 20 73 65  c flag is not se
1226e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  t, then this fun
1226f 63 74 69 6f 6e 20 69 73 20 61 0a 2a 2a 20 6e 6f  ction is a.** no
12270 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  -op. Otherwise, 
12271 74 68 65 20 61 63 74 69 6f 6e 73 20 72 65 71 75  the actions requ
12272 69 72 65 64 20 64 65 70 65 6e 64 20 6f 6e 20 74  ired depend on t
12273 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 0a  he journal-mode.
12274 2a 2a 20 61 6e 64 20 74 68 65 20 64 65 76 69 63  ** and the devic
12275 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
12276 73 20 6f 66 20 74 68 65 20 74 68 65 20 66 69 6c  s of the the fil
12277 65 2d 73 79 73 74 65 6d 2c 20 61 73 20 66 6f 6c  e-system, as fol
12278 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  lows:.**.**   * 
12279 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  If the journal f
1227a 69 6c 65 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d  ile is an in-mem
1227b 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ory journal file
1227c 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 6e 65 65 64  , no action need
1227d 0a 2a 2a 20 20 20 20 20 62 65 20 74 61 6b 65 6e  .**     be taken
1227e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 4f 74 68 65  ..**.**   * Othe
1227f 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 64 65  rwise, if the de
12280 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75  vice does not su
12281 70 70 6f 72 74 20 74 68 65 20 53 41 46 45 5f 41  pport the SAFE_A
12282 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2c 0a  PPEND property,.
12283 2a 2a 20 20 20 20 20 74 68 65 6e 20 74 68 65 20  **     then the 
12284 6e 52 65 63 20 66 69 65 6c 64 20 6f 66 20 74 68  nRec field of th
12285 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
12286 77 72 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c 20  written journal 
12287 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 69 73  header.**     is
12288 20 75 70 64 61 74 65 64 20 74 6f 20 63 6f 6e 74   updated to cont
12289 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ain the number o
1228a 66 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64  f journal record
1228b 73 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 20  s that have.**  
1228c 20 20 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20     been written 
1228d 66 6f 6c 6c 6f 77 69 6e 67 20 69 74 2e 20 49 66  following it. If
1228e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6f 70   the pager is op
1228f 65 72 61 74 69 6e 67 20 69 6e 20 66 75 6c 6c 2d  erating in full-
12290 73 79 6e 63 0a 2a 2a 20 20 20 20 20 6d 6f 64 65  sync.**     mode
12291 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
12292 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65  al file is synce
12293 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 69  d before this fi
12294 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2e 0a  eld is updated..
12295 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65  **.**   * If the
12296 20 64 65 76 69 63 65 20 64 6f 65 73 20 6e 6f 74   device does not
12297 20 73 75 70 70 6f 72 74 20 74 68 65 20 53 45 51   support the SEQ
12298 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79  UENTIAL property
12299 2c 20 74 68 65 6e 20 0a 2a 2a 20 20 20 20 20 6a  , then .**     j
1229a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 73  ournal file is s
1229b 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c  ynced..**.** Or,
1229c 20 69 6e 20 70 73 65 75 64 6f 2d 63 6f 64 65 3a   in pseudo-code:
1229d 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 4e 4f 54  .**.**   if( NOT
1229e 20 3c 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72   <in-memory jour
1229f 6e 61 6c 3e 20 29 7b 0a 2a 2a 20 20 20 20 20 69  nal> ){.**     i
122a0 66 28 20 4e 4f 54 20 53 41 46 45 5f 41 50 50 45  f( NOT SAFE_APPE
122a1 4e 44 20 29 7b 0a 2a 2a 20 20 20 20 20 20 20 69  ND ){.**       i
122a2 66 28 20 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f  f( <full-sync mo
122a3 64 65 3e 20 29 20 78 53 79 6e 63 28 3c 6a 6f 75  de> ) xSync(<jou
122a4 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20  rnal file>);.** 
122a5 20 20 20 20 20 20 3c 75 70 64 61 74 65 20 6e 52        <update nR
122a6 65 63 20 66 69 65 6c 64 3e 0a 2a 2a 20 20 20 20  ec field>.**    
122a7 20 7d 20 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e   } .**     if( N
122a8 4f 54 20 53 45 51 55 45 4e 54 49 41 4c 20 29 20  OT SEQUENTIAL ) 
122a9 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66  xSync(<journal f
122aa 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a  ile>);.**   }.**
122ab 0a 2a 2a 20 54 68 65 20 50 61 67 65 72 2e 6e 65  .** The Pager.ne
122ac 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20 6e  edSync flag is n
122ad 65 76 65 72 20 62 65 20 73 65 74 20 66 6f 72 20  ever be set for 
122ae 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c  temporary files,
122af 20 6f 72 20 61 6e 79 0a 2a 2a 20 66 69 6c 65 20   or any.** file 
122b0 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 6e 6f 2d  operating in no-
122b1 73 79 6e 63 20 6d 6f 64 65 20 28 50 61 67 65 72  sync mode (Pager
122b2 2e 6e 6f 53 79 6e 63 20 73 65 74 20 74 6f 20 6e  .noSync set to n
122b3 6f 6e 2d 7a 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a 20  on-zero)..**.** 
122b4 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74  If successful, t
122b5 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 61  his routine clea
122b6 72 73 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45  rs the PGHDR_NEE
122b7 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f 66 20 65  D_SYNC flag of e
122b8 76 65 72 79 20 0a 2a 2a 20 70 61 67 65 20 63 75  very .** page cu
122b9 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e 20  rrently held in 
122ba 6d 65 6d 6f 72 79 20 62 65 66 6f 72 65 20 72 65  memory before re
122bb 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f  turning SQLITE_O
122bc 4b 2e 20 49 66 20 61 6e 20 49 4f 0a 2a 2a 20 65  K. If an IO.** e
122bd 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
122be 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 49 4f  red, then the IO
122bf 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
122c0 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63  eturned to the c
122c1 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  aller..*/.static
122c2 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 6c   int syncJournal
122c3 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
122c4 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e  .  if( pPager->n
122c5 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 61  eedSync ){.    a
122c6 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e  ssert( !pPager->
122c7 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20  tempFile );.    
122c8 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
122c9 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a  nalMode!=PAGER_J
122ca 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
122cb 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  Y ){.      int r
122cc 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
122cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
122ce 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
122cf 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e  /.      const in
122d0 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f  t iDc = sqlite3O
122d1 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
122d2 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
122d3 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  d);.      assert
122d4 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
122d5 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 20 20 20 20  >jfd) );..      
122d6 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49  if( 0==(iDc&SQLI
122d7 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50  TE_IOCAP_SAFE_AP
122d8 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20 20  PEND) ){.       
122d9 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 69 4e 52   /* Variable iNR
122da 65 63 4f 66 66 73 65 74 20 69 73 20 73 65 74 20  ecOffset is set 
122db 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e  to the offset in
122dc 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
122dd 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20  e.        ** of 
122de 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 6f  the nRec field o
122df 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
122e0 74 6c 79 20 77 72 69 74 74 65 6e 20 6a 6f 75 72  tly written jour
122e1 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 20 20  nal header..    
122e2 20 20 20 20 2a 2a 20 54 68 69 73 20 66 69 65 6c      ** This fiel
122e3 64 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65  d will be update
122e4 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  d following the 
122e5 78 53 79 6e 63 28 29 20 6f 70 65 72 61 74 69 6f  xSync() operatio
122e6 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20  n.        ** on 
122e7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
122e8 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34  . */.        i64
122e9 20 69 4e 52 65 63 4f 66 66 73 65 74 20 3d 20 70   iNRecOffset = p
122ea 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
122eb 72 20 2b 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72  r + sizeof(aJour
122ec 6e 61 6c 4d 61 67 69 63 29 3b 0a 0a 20 20 20 20  nalMagic);..    
122ed 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63      /* This bloc
122ee 6b 20 64 65 61 6c 73 20 77 69 74 68 20 61 6e 20  k deals with an 
122ef 6f 62 73 63 75 72 65 20 70 72 6f 62 6c 65 6d 2e  obscure problem.
122f0 20 49 66 20 74 68 65 20 6c 61 73 74 20 63 6f 6e   If the last con
122f1 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20 20 20 20  nection.        
122f2 2a 2a 20 74 68 61 74 20 77 72 6f 74 65 20 74 6f  ** that wrote to
122f3 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 77   this database w
122f4 61 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20  as operating in 
122f5 70 65 72 73 69 73 74 65 6e 74 2d 6a 6f 75 72 6e  persistent-journ
122f6 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f  al.        ** mo
122f7 64 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75  de, then the jou
122f8 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 61 74  rnal file may at
122f9 20 74 68 69 73 20 70 6f 69 6e 74 20 61 63 74 75   this point actu
122fa 61 6c 6c 79 20 62 65 20 6c 61 72 67 65 72 0a 20  ally be larger. 
122fb 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 50         ** than P
122fc 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ager.journalOff 
122fd 62 79 74 65 73 2e 20 49 66 20 74 68 65 20 6e 65  bytes. If the ne
122fe 78 74 20 74 68 69 6e 67 20 69 6e 20 74 68 65 20  xt thing in the 
122ff 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20  journal.        
12300 2a 2a 20 66 69 6c 65 20 68 61 70 70 65 6e 73 20  ** file happens 
12301 74 6f 20 62 65 20 61 20 6a 6f 75 72 6e 61 6c 2d  to be a journal-
12302 68 65 61 64 65 72 20 28 77 72 69 74 74 65 6e 20  header (written 
12303 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20  as part of the. 
12304 20 20 20 20 20 20 20 2a 2a 20 70 72 65 76 69 6f         ** previo
12305 75 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  us connections t
12306 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 61 6e 64  ransaction), and
12307 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65   a crash or powe
12308 72 2d 66 61 69 6c 75 72 65 20 0a 20 20 20 20 20  r-failure .     
12309 20 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 66 74     ** occurs aft
1230a 65 72 20 6e 52 65 63 20 69 73 20 75 70 64 61 74  er nRec is updat
1230b 65 64 20 62 75 74 20 62 65 66 6f 72 65 20 74 68  ed but before th
1230c 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72  is connection wr
1230d 69 74 65 73 20 0a 20 20 20 20 20 20 20 20 2a 2a  ites .        **
1230e 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 74   anything else t
1230f 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
12310 6c 65 20 28 6f 72 20 63 6f 6d 6d 69 74 73 2f 72  le (or commits/r
12311 6f 6c 6c 73 20 62 61 63 6b 20 69 74 73 20 0a 20  olls back its . 
12312 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61         ** transa
12313 63 74 69 6f 6e 29 2c 20 74 68 65 6e 20 53 51 4c  ction), then SQL
12314 69 74 65 20 6d 61 79 20 62 65 63 6f 6d 65 20 63  ite may become c
12315 6f 6e 66 75 73 65 64 20 77 68 65 6e 20 64 6f 69  onfused when doi
12316 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 20 20  ng the .        
12317 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72  ** hot-journal r
12318 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 6e  ollback followin
12319 67 20 72 65 63 6f 76 65 72 79 2e 20 49 74 20 6d  g recovery. It m
1231a 61 79 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6c 6c  ay roll back all
1231b 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  .        ** of t
1231c 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  his connections 
1231d 64 61 74 61 2c 20 74 68 65 6e 20 70 72 6f 63 65  data, then proce
1231e 65 64 20 74 6f 20 72 6f 6c 6c 69 6e 67 20 62 61  ed to rolling ba
1231f 63 6b 20 74 68 65 20 6f 6c 64 2c 0a 20 20 20 20  ck the old,.    
12320 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d 64 61      ** out-of-da
12321 74 65 20 64 61 74 61 20 74 68 61 74 20 66 6f 6c  te data that fol
12322 6c 6f 77 73 20 69 74 2e 20 44 61 74 61 62 61 73  lows it. Databas
12323 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20  e corruption..  
12324 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
12325 20 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75   ** To work arou
12326 6e 64 20 74 68 69 73 2c 20 69 66 20 74 68 65 20  nd this, if the 
12327 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65  journal file doe
12328 73 20 61 70 70 65 61 72 20 74 6f 20 63 6f 6e 74  s appear to cont
12329 61 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ain.        ** a
1232a 20 76 61 6c 69 64 20 68 65 61 64 65 72 20 66 6f   valid header fo
1232b 6c 6c 6f 77 69 6e 67 20 50 61 67 65 72 2e 6a 6f  llowing Pager.jo
1232c 75 72 6e 61 6c 4f 66 66 2c 20 74 68 65 6e 20 77  urnalOff, then w
1232d 72 69 74 65 20 61 20 30 78 30 30 0a 20 20 20 20  rite a 0x00.    
1232e 20 20 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 74      ** byte to t
1232f 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 20 74  he start of it t
12330 6f 20 70 72 65 76 65 6e 74 20 69 74 20 66 72 6f  o prevent it fro
12331 6d 20 62 65 69 6e 67 20 72 65 63 6f 67 6e 69 7a  m being recogniz
12332 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  ed..        **. 
12333 20 20 20 20 20 20 20 2a 2a 20 56 61 72 69 61 62         ** Variab
12334 6c 65 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65  le iNextHdrOffse
12335 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  t is set to the 
12336 6f 66 66 73 65 74 20 61 74 20 77 68 69 63 68 20  offset at which 
12337 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  this.        ** 
12338 70 72 6f 62 6c 65 6d 61 74 69 63 20 68 65 61 64  problematic head
12339 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 2c 20 69  er will occur, i
1233a 66 20 69 74 20 65 78 69 73 74 73 2e 20 61 4d 61  f it exists. aMa
1233b 67 69 63 20 69 73 20 75 73 65 64 20 0a 20 20 20  gic is used .   
1233c 20 20 20 20 20 2a 2a 20 61 73 20 61 20 74 65 6d       ** as a tem
1233d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 74 6f  porary buffer to
1233e 20 69 6e 73 70 65 63 74 20 74 68 65 20 66 69 72   inspect the fir
1233f 73 74 20 63 6f 75 70 6c 65 20 6f 66 20 62 79 74  st couple of byt
12340 65 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a  es of.        **
12341 20 74 68 65 20 70 6f 74 65 6e 74 69 61 6c 20 6a   the potential j
12342 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20  ournal header.. 
12343 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
12344 20 20 69 36 34 20 69 4e 65 78 74 48 64 72 4f 66    i64 iNextHdrOf
12345 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 64  fset = journalHd
12346 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b  rOffset(pPager);
12347 0a 20 20 20 20 20 20 20 20 75 38 20 61 4d 61 67  .        u8 aMag
12348 69 63 5b 38 5d 3b 0a 20 20 20 20 20 20 20 20 72  ic[8];.        r
12349 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
1234a 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61  d(pPager->jfd, a
1234b 4d 61 67 69 63 2c 20 38 2c 20 69 4e 65 78 74 48  Magic, 8, iNextH
1234c 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20  drOffset);.     
1234d 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1234e 45 5f 4f 4b 20 26 26 20 30 3d 3d 6d 65 6d 63 6d  E_OK && 0==memcm
1234f 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e  p(aMagic, aJourn
12350 61 6c 4d 61 67 69 63 2c 20 38 29 20 29 7b 0a 20  alMagic, 8) ){. 
12351 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20           static 
12352 63 6f 6e 73 74 20 75 38 20 7a 65 72 6f 62 79 74  const u8 zerobyt
12353 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  e = 0;.         
12354 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
12355 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  rite(pPager->jfd
12356 2c 20 26 7a 65 72 6f 62 79 74 65 2c 20 31 2c 20  , &zerobyte, 1, 
12357 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b  iNextHdrOffset);
12358 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
12359 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1235a 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49  E_OK && rc!=SQLI
1235b 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52  TE_IOERR_SHORT_R
1235c 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EAD ){.         
1235d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1235e 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f      }..        /
1235f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65 63  * Write the nRec
12360 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20   value into the 
12361 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 61  journal file hea
12362 64 65 72 2e 20 49 66 20 69 6e 0a 20 20 20 20 20  der. If in.     
12363 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79 6e 63 68     ** full-synch
12364 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 73 79 6e  ronous mode, syn
12365 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  c the journal fi
12366 72 73 74 2e 20 54 68 69 73 20 65 6e 73 75 72 65  rst. This ensure
12367 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a  s that.        *
12368 2a 20 61 6c 6c 20 64 61 74 61 20 68 61 73 20 72  * all data has r
12369 65 61 6c 6c 79 20 68 69 74 20 74 68 65 20 64 69  eally hit the di
1236a 73 6b 20 62 65 66 6f 72 65 20 6e 52 65 63 20 69  sk before nRec i
1236b 73 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61 72  s updated to mar
1236c 6b 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20  k.        ** it 
1236d 61 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 66  as a candidate f
1236e 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 20  or rollback..   
1236f 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
12370 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72  ** This is not r
12371 65 71 75 69 72 65 64 20 69 66 20 74 68 65 20 70  equired if the p
12372 65 72 73 69 73 74 65 6e 74 20 6d 65 64 69 61 20  ersistent media 
12373 73 75 70 70 6f 72 74 73 20 74 68 65 0a 20 20 20  supports the.   
12374 20 20 20 20 20 2a 2a 20 53 41 46 45 5f 41 50 50       ** SAFE_APP
12375 45 4e 44 20 70 72 6f 70 65 72 74 79 2e 20 42 65  END property. Be
12376 63 61 75 73 65 20 69 6e 20 74 68 69 73 20 63 61  cause in this ca
12377 73 65 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  se it is not pos
12378 73 69 62 6c 65 20 0a 20 20 20 20 20 20 20 20 2a  sible .        *
12379 2a 20 66 6f 72 20 67 61 72 62 61 67 65 20 64 61  * for garbage da
1237a 74 61 20 74 6f 20 62 65 20 61 70 70 65 6e 64 65  ta to be appende
1237b 64 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 20 74  d to the file, t
1237c 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a 20 20  he nRec field.  
1237d 20 20 20 20 20 20 2a 2a 20 69 73 20 70 6f 70 75        ** is popu
1237e 6c 61 74 65 64 20 77 69 74 68 20 30 78 46 46 46  lated with 0xFFF
1237f 46 46 46 46 46 20 77 68 65 6e 20 74 68 65 20 6a  FFFFF when the j
12380 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73  ournal header is
12381 20 77 72 69 74 74 65 6e 0a 20 20 20 20 20 20 20   written.       
12382 20 2a 2a 20 61 6e 64 20 6e 65 76 65 72 20 6e 65   ** and never ne
12383 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 74 65  eds to be update
12384 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  d..        */.  
12385 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72        if( pPager
12386 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 30 3d  ->fullSync && 0=
12387 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43  =(iDc&SQLITE_IOC
12388 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29  AP_SEQUENTIAL) )
12389 7b 0a 20 20 20 20 20 20 20 20 20 20 50 41 47 45  {.          PAGE
1238a 52 54 52 41 43 45 28 28 22 53 59 4e 43 20 6a 6f  RTRACE(("SYNC jo
1238b 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20  urnal of %d\n", 
1238c 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29  PAGERID(pPager))
1238d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4f 54  );.          IOT
1238e 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70 5c  RACE(("JSYNC %p\
1238f 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20  n", pPager)).   
12390 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
12391 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72  te3OsSync(pPager
12392 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73  ->jfd, pPager->s
12393 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20  ync_flags);.    
12394 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
12395 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
12396 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
12397 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28         IOTRACE((
12398 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64  "JHDR %p %lld %d
12399 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 69 4e 52  \n", pPager, iNR
1239a 65 63 4f 66 66 73 65 74 2c 20 34 29 29 3b 0a 20  ecOffset, 4));. 
1239b 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74         rc = writ
1239c 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  e32bits(pPager->
1239d 6a 66 64 2c 20 69 4e 52 65 63 4f 66 66 73 65 74  jfd, iNRecOffset
1239e 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b  , pPager->nRec);
1239f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
123a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
123a1 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
123a2 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44        if( 0==(iD
123a3 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  c&SQLITE_IOCAP_S
123a4 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20  EQUENTIAL) ){.  
123a5 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
123a6 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20  (("SYNC journal 
123a7 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  of %d\n", PAGERI
123a8 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20  D(pPager)));.   
123a9 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a       IOTRACE(("J
123aa 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67  SYNC %p\n", pPag
123ab 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 20  er)).        rc 
123ac 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  = sqlite3OsSync(
123ad 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61  pPager->jfd, pPa
123ae 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c  ger->sync_flags|
123af 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61   .          (pPa
123b0 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d  ger->sync_flags=
123b1 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c  =SQLITE_SYNC_FUL
123b2 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41  L?SQLITE_SYNC_DA
123b3 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20  TAONLY:0).      
123b4 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28    );.        if(
123b5 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
123b6 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
123b7 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
123b8 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
123b9 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63 63  le was just succ
123ba 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e  essfully synced.
123bb 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64 53   Set Pager.needS
123bc 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20 7a  ync .    ** to z
123bd 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74 68  ero and clear th
123be 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  e PGHDR_NEED_SYN
123bf 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 61  C flag on all pa
123c0 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  gess..    */.   
123c1 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
123c2 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  c = 0;.    pPage
123c3 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65  r->journalStarte
123c4 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74  d = 1;.    sqlit
123c5 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e  e3PcacheClearSyn
123c6 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e 70  cFlags(pPager->p
123c7 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20  PCache);.  }..  
123c8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
123c9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61  ;.}../*.** The a
123ca 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 66  rgument is the f
123cb 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64  irst in a linked
123cc 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70   list of dirty p
123cd 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a 2a  ages connected.*
123ce 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e 70  * by the PgHdr.p
123cf 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20 54  Dirty pointer. T
123d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 69  his function wri
123d1 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66 20  tes each one of 
123d2 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79  the.** in-memory
123d3 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69   pages in the li
123d4 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  st to the databa
123d5 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72 67  se file. The arg
123d6 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 20  ument may.** be 
123d7 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74 69  NULL, representi
123d8 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73 74  ng an empty list
123d9 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
123da 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a  his function is.
123db 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  ** a no-op..**.*
123dc 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 74  * The pager must
123dd 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 61   hold at least a
123de 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 77   RESERVED lock w
123df 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
123e0 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20  n.** is called. 
123e1 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 61  Before writing a
123e2 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64  nything to the d
123e3 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
123e4 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 70  is lock.** is up
123e5 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43  graded to an EXC
123e6 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20  LUSIVE lock. If 
123e7 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20  the lock cannot 
123e8 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a 20  be obtained,.** 
123e9 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
123ea 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64  eturned and no d
123eb 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74  ata is written t
123ec 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
123ed 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74  ile..** .** If t
123ee 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74 65  he pager is a te
123ef 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61 6e  mp-file pager an
123f0 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c  d the actual fil
123f1 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a 2a  e-system file.**
123f2 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e   is not yet open
123f3 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20  , it is created 
123f4 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f 72  and opened befor
123f5 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a 2a  e any data is .*
123f6 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a 2a  * written out..*
123f7 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c 6f  *.** Once the lo
123f8 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67 72  ck has been upgr
123f9 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65 63  aded and, if nec
123fa 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c 65  essary, the file
123fb 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 20   opened,.** the 
123fc 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74 65  pages are writte
123fd 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74  n out to the dat
123fe 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c 69  abase file in li
123ff 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69 6e  st order. Writin
12400 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20 73  g.** a page is s
12401 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65 65  kipped if it mee
12402 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68 65  ts either of the
12403 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65   following crite
12404 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54  ria:.**.**   * T
12405 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  he page number i
12406 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 50  s greater than P
12407 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72 0a  ager.dbSize, or.
12408 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44 52  **   * The PGHDR
12409 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 67  _DONT_WRITE flag
1240a 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 70   is set on the p
1240b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72  age..**.** If wr
1240c 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67 65  iting out a page
1240d 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61   causes the data
1240e 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f  base file to gro
1240f 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53  w, Pager.dbFileS
12410 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74 65  ize.** is update
12411 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 49  d accordingly. I
12412 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69 74  f page 1 is writ
12413 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74 68  ten out, then th
12414 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a 2a  e value cached.*
12415 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69 6c  * in Pager.dbFil
12416 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61 74  eVers[] is updat
12417 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20  ed to match the 
12418 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65 64  new value stored
12419 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62   in.** the datab
1241a 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
1241b 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  If everything is
1241c 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
1241d 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
1241e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72  ed. If an IO err
1241f 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61  or .** occurs, a
12420 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  n IO error code 
12421 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c  is returned. Or,
12422 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49 56   if the EXCLUSIV
12423 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a  E lock cannot.**
12424 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53 51   be obtained, SQ
12425 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74  LITE_BUSY is ret
12426 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
12427 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65   int pager_write
12428 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72 20  _pagelist(PgHdr 
12429 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 72  *pList){.  Pager
1242a 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20   *pPager;       
1242b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1242c 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  /* Pager object 
1242d 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
1242e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1242f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
12430 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69  urn code */..  i
12431 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
12432 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
12433 20 20 70 50 61 67 65 72 20 3d 20 70 4c 69 73 74    pPager = pList
12434 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a 20  ->pPager;..  /* 
12435 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68  At this point th
12436 65 72 65 20 6d 61 79 20 62 65 20 65 69 74 68 65  ere may be eithe
12437 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  r a RESERVED or 
12438 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
12439 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  n the.  ** datab
1243a 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65  ase file. If the
1243b 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e  re is already an
1243c 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c   EXCLUSIVE lock,
1243d 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
1243e 20 2a 2a 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f   ** call is a no
1243f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d  -op..  **.  ** M
12440 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 66  oving the lock f
12441 72 6f 6d 20 52 45 53 45 52 56 45 44 20 74 6f 20  rom RESERVED to 
12442 45 58 43 4c 55 53 49 56 45 20 61 63 74 75 61 6c  EXCLUSIVE actual
12443 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67 6f 69 6e  ly involves goin
12444 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 61  g.  ** through a
12445 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73  n intermediate s
12446 74 61 74 65 20 50 45 4e 44 49 4e 47 2e 20 20 20  tate PENDING.   
12447 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 70  A PENDING lock p
12448 72 65 76 65 6e 74 73 20 6e 65 77 0a 20 20 2a 2a  revents new.  **
12449 20 72 65 61 64 65 72 73 20 66 72 6f 6d 20 61 74   readers from at
1244a 74 61 63 68 69 6e 67 20 74 6f 20 74 68 65 20 64  taching to the d
1244b 61 74 61 62 61 73 65 20 62 75 74 20 69 73 20 75  atabase but is u
1244c 6e 73 75 66 66 69 63 69 65 6e 74 20 66 6f 72 20  nsufficient for 
1244d 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72 69 74 65  us to.  ** write
1244e 2e 20 20 54 68 65 20 69 64 65 61 20 6f 66 20 61  .  The idea of a
1244f 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73   PENDING lock is
12450 20 74 6f 20 70 72 65 76 65 6e 74 20 6e 65 77 20   to prevent new 
12451 72 65 61 64 65 72 73 20 66 72 6f 6d 0a 20 20 2a  readers from.  *
12452 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77 68 69 6c  * coming in whil
12453 65 20 77 65 20 77 61 69 74 20 66 6f 72 20 65 78  e we wait for ex
12454 69 73 74 69 6e 67 20 72 65 61 64 65 72 73 20 74  isting readers t
12455 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a 0a 20 20  o clear..  **.  
12456 2a 2a 20 57 68 69 6c 65 20 74 68 65 20 70 61 67  ** While the pag
12457 65 72 20 69 73 20 69 6e 20 74 68 65 20 52 45 53  er is in the RES
12458 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65  ERVED state, the
12459 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61   original databa
1245a 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20  se file.  ** is 
1245b 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 77 65  unchanged and we
1245c 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b 20 77 69   can rollback wi
1245d 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
1245e 70 6c 61 79 62 61 63 6b 20 74 68 65 0a 20 20 2a  playback the.  *
1245f 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f 20 74  * journal into t
12460 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
12461 62 61 73 65 20 66 69 6c 65 2e 20 20 4f 6e 63 65  base file.  Once
12462 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74   we transition t
12463 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45  o.  ** EXCLUSIVE
12464 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 64  , it means the d
12465 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73  atabase file has
12466 20 62 65 65 6e 20 63 68 61 6e 67 65 64 20 61 6e   been changed an
12467 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63 6b 0a 20  d any rollback. 
12468 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75 69 72 65   ** will require
12469 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 62   a journal playb
1246a 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ack..  */.  asse
1246b 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
1246c 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45  e>=PAGER_RESERVE
1246d 44 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65  D );.  rc = page
1246e 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70  r_wait_on_lock(p
1246f 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45  Pager, EXCLUSIVE
12470 5f 4c 4f 43 4b 29 3b 0a 0a 20 20 2f 2a 20 49 66  _LOCK);..  /* If
12471 20 74 68 65 20 66 69 6c 65 20 69 73 20 61 20 74   the file is a t
12472 65 6d 70 2d 66 69 6c 65 20 68 61 73 20 6e 6f 74  emp-file has not
12473 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64   yet been opened
12474 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20 49  , open it now. I
12475 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f  t.  ** is not po
12476 73 73 69 62 6c 65 20 66 6f 72 20 72 63 20 74 6f  ssible for rc to
12477 20 62 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53   be other than S
12478 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 69 73  QLITE_OK if this
12479 20 62 72 61 6e 63 68 0a 20 20 2a 2a 20 69 73 20   branch.  ** is 
1247a 74 61 6b 65 6e 2c 20 61 73 20 70 61 67 65 72 5f  taken, as pager_
1247b 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 29 20 69  wait_on_lock() i
1247c 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 74 65  s a no-op for te
1247d 6d 70 2d 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20  mp-files..  */. 
1247e 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61   if( !isOpen(pPa
1247f 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20  ger->fd) ){.    
12480 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
12481 74 65 6d 70 46 69 6c 65 20 26 26 20 72 63 3d 3d  tempFile && rc==
12482 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
12483 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74   rc = pagerOpent
12484 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61 67  emp(pPager, pPag
12485 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e  er->fd, pPager->
12486 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 0a  vfsFlags);.  }..
12487 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
12488 49 54 45 5f 4f 4b 20 26 26 20 70 4c 69 73 74 20  ITE_OK && pList 
12489 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f  ){.    Pgno pgno
1248a 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b 0a   = pList->pgno;.
1248b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
1248c 20 61 72 65 20 64 69 72 74 79 20 70 61 67 65 73   are dirty pages
1248d 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63   in the page cac
1248e 68 65 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d  he with page num
1248f 62 65 72 73 20 67 72 65 61 74 65 72 0a 20 20 20  bers greater.   
12490 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 64   ** than Pager.d
12491 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65 61 6e  bSize, this mean
12492 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72  s sqlite3PagerTr
12493 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 77 61  uncateImage() wa
12494 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20  s called to.    
12495 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 6c 65  ** make the file
12496 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73 75 6d   smaller (presum
12497 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76 61 63  ably by auto-vac
12498 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f  uum code). Do no
12499 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a 20 61  t write.    ** a
1249a 6e 79 20 73 75 63 68 20 70 61 67 65 73 20 74 6f  ny such pages to
1249b 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a   the file..    *
1249c 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 64  *.    ** Also, d
1249d 6f 20 6e 6f 74 20 77 72 69 74 65 20 6f 75 74 20  o not write out 
1249e 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 68 61  any page that ha
1249f 73 20 74 68 65 20 50 47 48 44 52 5f 44 4f 4e 54  s the PGHDR_DONT
124a0 5f 57 52 49 54 45 20 66 6c 61 67 0a 20 20 20 20  _WRITE flag.    
124a1 2a 2a 20 73 65 74 20 28 73 65 74 20 62 79 20 73  ** set (set by s
124a2 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57  qlite3PagerDontW
124a3 72 69 74 65 28 29 29 2e 0a 20 20 20 20 2a 2f 0a  rite())..    */.
124a4 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50      if( pgno<=pP
124a5 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20  ager->dbSize && 
124a6 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73  0==(pList->flags
124a7 26 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54  &PGHDR_DONT_WRIT
124a8 45 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20  E) ){.      i64 
124a9 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31  offset = (pgno-1
124aa 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70  )*(i64)pPager->p
124ab 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  ageSize;        
124ac 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 77 72   /* Offset to wr
124ad 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 63 68 61  ite */.      cha
124ae 72 20 2a 70 44 61 74 61 20 3d 20 43 4f 44 45 43  r *pData = CODEC
124af 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74 2d  2(pPager, pList-
124b0 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36 29  >pData, pgno, 6)
124b1 3b 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 72 69  ; /* Data to wri
124b2 74 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20  te */..      /* 
124b3 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61  Write out the pa
124b4 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20  ge data. */.    
124b5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
124b6 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64  Write(pPager->fd
124b7 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d  , pData, pPager-
124b8 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65  >pageSize, offse
124b9 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66  t);..      /* If
124ba 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 74   page 1 was just
124bb 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65   written, update
124bc 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72   Pager.dbFileVer
124bd 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 20  s to match.     
124be 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e 6f   ** the value no
124bf 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  w stored in the 
124c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
124c1 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 0a  f writing this .
124c2 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 61        ** page ca
124c3 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61 73  used the databas
124c4 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20  e file to grow, 
124c5 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 7a  update dbFileSiz
124c6 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  e. .      */.   
124c7 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29     if( pgno==1 )
124c8 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
124c9 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  (&pPager->dbFile
124ca 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d  Vers, &pData[24]
124cb 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d  , sizeof(pPager-
124cc 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20  >dbFileVers));. 
124cd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
124ce 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62   pgno>pPager->db
124cf 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  FileSize ){.    
124d0 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69      pPager->dbFi
124d1 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20  leSize = pgno;. 
124d2 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
124d3 20 55 70 64 61 74 65 20 61 6e 79 20 62 61 63 6b   Update any back
124d4 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 79 69  up objects copyi
124d5 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ng the contents 
124d6 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e 20 2a  of this pager. *
124d7 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  /.      sqlite3B
124d8 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67  ackupUpdate(pPag
124d9 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e  er->pBackup, pgn
124da 6f 2c 20 28 75 38 20 2a 29 70 44 61 74 61 29 3b  o, (u8 *)pData);
124db 0a 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41  ..      PAGERTRA
124dc 43 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 61  CE(("STORE %d pa
124dd 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29  ge %d hash(%08x)
124de 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
124df 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28          PAGERID(
124e0 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70  pPager), pgno, p
124e1 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c  ager_pagehash(pL
124e2 69 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 4f  ist)));.      IO
124e3 54 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 70  TRACE(("PGOUT %p
124e4 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
124e5 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 41  pgno));.      PA
124e6 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33  GER_INCR(sqlite3
124e7 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63  _pager_writedb_c
124e8 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41 47  ount);.      PAG
124e9 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e  ER_INCR(pPager->
124ea 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65 6c  nWrite);.    }el
124eb 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54  se{.      PAGERT
124ec 52 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20 25  RACE(("NOSTORE %
124ed 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41  d page %d\n", PA
124ee 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
124ef 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69 66  gno));.    }.#if
124f0 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  def SQLITE_CHECK
124f1 5f 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73 74  _PAGES.    pList
124f2 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67  ->pageHash = pag
124f3 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73  er_pagehash(pLis
124f4 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70  t);.#endif.    p
124f5 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44  List = pList->pD
124f6 69 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  irty;.  }..  ret
124f7 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
124f8 20 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72 64   Append a record
124f9 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
124fa 73 74 61 74 65 20 6f 66 20 70 61 67 65 20 70 50  state of page pP
124fb 67 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75  g to the sub-jou
124fc 72 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73 20  rnal. .** It is 
124fd 74 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73 70  the callers resp
124fe 6f 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75 73  onsibility to us
124ff 65 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61  e subjRequiresPa
12500 67 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a 2a  ge() to check .*
12501 2a 20 74 68 61 74 20 69 74 20 69 73 20 72 65 61  * that it is rea
12502 6c 6c 79 20 72 65 71 75 69 72 65 64 20 62 65 66  lly required bef
12503 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73  ore calling this
12504 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
12505 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
12506 73 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 72  set the bit corr
12507 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50 67  esponding to pPg
12508 2d 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62 69  ->pgno in the bi
12509 74 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c 6c  tvecs.** for all
1250a 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73   open savepoints
1250b 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
1250c 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  g..**.** This fu
1250d 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53  nction returns S
1250e 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72  QLITE_OK if ever
1250f 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73  ything is succes
12510 73 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20 65  sful, an IO.** e
12511 72 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68 65  rror code if the
12512 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74   attempt to writ
12513 65 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75  e to the sub-jou
12514 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a  rnal fails, or .
12515 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  ** SQLITE_NOMEM 
12516 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  if a malloc fail
12517 73 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67 20  s while setting 
12518 61 20 62 69 74 20 69 6e 20 61 20 73 61 76 65 70  a bit in a savep
12519 6f 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e 0a  oint.** bitvec..
1251a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75  */.static int su
1251b 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 48  bjournalPage(PgH
1251c 64 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 20  dr *pPg){.  int 
1251d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1251e 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
1251f 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20  = pPg->pPager;. 
12520 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
12521 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20  er->sjfd) ){.   
12522 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70   void *pData = p
12523 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 69  Pg->pData;.    i
12524 36 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67  64 offset = pPag
12525 65 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b 70  er->nSubRec*(4+p
12526 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29  Pager->pageSize)
12527 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61 74  ;.    char *pDat
12528 61 32 20 3d 20 43 4f 44 45 43 32 28 70 50 61 67  a2 = CODEC2(pPag
12529 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e  er, pData, pPg->
1252a 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 0a 20 20 20  pgno, 7);.  .   
1252b 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54   PAGERTRACE(("ST
1252c 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61  MT-JOURNAL %d pa
1252d 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  ge %d\n", PAGERI
1252e 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e  D(pPager), pPg->
1252f 70 67 6e 6f 29 29 3b 0a 20 20 0a 20 20 20 20 61  pgno));.  .    a
12530 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75  ssert( pageInJou
12531 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67  rnal(pPg) || pPg
12532 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64  ->pgno>pPager->d
12533 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20  bOrigSize );.   
12534 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74   rc = write32bit
12535 73 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20  s(pPager->sjfd, 
12536 6f 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e  offset, pPg->pgn
12537 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  o);.    if( rc==
12538 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
12539 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1253a 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 73  sWrite(pPager->s
1253b 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 61  jfd, pData2, pPa
1253c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f  ger->pageSize, o
1253d 66 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 7d 0a  ffset+4);.    }.
1253e 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
1253f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
12540 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2b 2b  Pager->nSubRec++
12541 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
12542 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
12543 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61  >0 );.    rc = a
12544 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74  ddToSavepointBit
12545 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67  vecs(pPager, pPg
12546 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 74 65 73  ->pgno);.    tes
12547 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54  tcase( rc!=SQLIT
12548 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 72 65  E_OK );.  }.  re
12549 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
1254a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1254b 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
1254c 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 77  e pcache layer w
1254d 68 65 6e 20 69 74 20 68 61 73 20 72 65 61 63 68  hen it has reach
1254e 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 20  ed some.** soft 
1254f 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 68  memory limit. Th
12550 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
12551 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
12552 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 0a   a Pager object.
12553 2a 2a 20 28 63 61 73 74 20 61 73 20 61 20 76 6f  ** (cast as a vo
12554 69 64 2a 29 2e 20 54 68 65 20 70 61 67 65 72 20  id*). The pager 
12555 69 73 20 61 6c 77 61 79 73 20 27 70 75 72 67 65  is always 'purge
12556 61 62 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69 6e  able' (not an in
12557 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62  -memory.** datab
12558 61 73 65 29 2e 20 54 68 65 20 73 65 63 6f 6e 64  ase). The second
12559 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 72   argument is a r
1255a 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 70 61  eference to a pa
1255b 67 65 20 74 68 61 74 20 69 73 20 0a 2a 2a 20 63  ge that is .** c
1255c 75 72 72 65 6e 74 6c 79 20 64 69 72 74 79 20 62  urrently dirty b
1255d 75 74 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 61  ut has no outsta
1255e 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73  nding references
1255f 2e 20 54 68 65 20 70 61 67 65 0a 2a 2a 20 69 73  . The page.** is
12560 20 61 6c 77 61 79 73 20 61 73 73 6f 63 69 61 74   always associat
12561 65 64 20 77 69 74 68 20 74 68 65 20 50 61 67 65  ed with the Page
12562 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20  r object passed 
12563 61 73 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a  as the first .**
12564 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
12565 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73   The job of this
12566 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20   function is to 
12567 6d 61 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 62  make pPg clean b
12568 79 20 77 72 69 74 69 6e 67 20 69 74 73 20 63 6f  y writing its co
12569 6e 74 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74 6f  ntents.** out to
1256a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1256b 6c 65 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 2e  le, if possible.
1256c 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c 76   This may involv
1256d 65 20 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a 2a  e syncing the.**
1256e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 0a   journal file. .
1256f 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  **.** If success
12570 66 75 6c 2c 20 73 71 6c 69 74 65 33 50 63 61 63  ful, sqlite3Pcac
12571 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73  heMakeClean() is
12572 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 70   called on the p
12573 61 67 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54  age and.** SQLIT
12574 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49  E_OK returned. I
12575 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63  f an IO error oc
12576 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e  curs while tryin
12577 67 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a  g to make the.**
12578 20 70 61 67 65 20 63 6c 65 61 6e 2c 20 74 68 65   page clean, the
12579 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
1257a 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74  s returned. If t
1257b 68 65 20 70 61 67 65 20 63 61 6e 6e 6f 74 20 62  he page cannot b
1257c 65 0a 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e 20  e.** made clean 
1257d 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 72  for some other r
1257e 65 61 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65 72  eason, but no er
1257f 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e  ror occurs, then
12580 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73   SQLITE_OK.** is
12581 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
12582 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c  ite3PcacheMakeCl
12583 65 61 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c  ean() is not cal
12584 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  led..*/.static i
12585 6e 74 20 70 61 67 65 72 53 74 72 65 73 73 28 76  nt pagerStress(v
12586 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70  oid *p, PgHdr *p
12587 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50  Pg){.  Pager *pP
12588 61 67 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29  ager = (Pager *)
12589 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  p;.  int rc = SQ
1258a 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65  LITE_OK;..  asse
1258b 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d  rt( pPg->pPager=
1258c 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 61 73 73  =pPager );.  ass
1258d 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26  ert( pPg->flags&
1258e 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 0a  PGHDR_DIRTY );..
1258f 20 20 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 79    /* The doNotSy
12590 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 20 62  nc flag is set b
12591 79 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67  y the sqlite3Pag
12592 65 72 57 72 69 74 65 28 29 20 66 75 6e 63 74 69  erWrite() functi
12593 6f 6e 20 77 68 69 6c 65 20 69 74 0a 20 20 2a 2a  on while it.  **
12594 20 69 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20   is journalling 
12595 61 20 73 65 74 20 6f 66 20 74 77 6f 20 6f 72 20  a set of two or 
12596 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61  more database pa
12597 67 65 73 20 74 68 61 74 20 61 72 65 20 73 74 6f  ges that are sto
12598 72 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  red.  ** on the 
12599 73 61 6d 65 20 64 69 73 6b 20 73 65 63 74 6f 72  same disk sector
1259a 2e 20 53 79 6e 63 69 6e 67 20 74 68 65 20 6a 6f  . Syncing the jo
1259b 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 6c  urnal is not all
1259c 6f 77 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20  owed while.  ** 
1259d 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e  this is happenin
1259e 67 20 61 73 20 69 74 20 69 73 20 69 6d 70 6f 72  g as it is impor
1259f 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d 65  tant that all me
125a0 6d 62 65 72 73 20 6f 66 20 73 75 63 68 20 61 0a  mbers of such a.
125a1 20 20 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 65    ** set of page
125a2 73 20 61 72 65 20 73 79 6e 63 65 64 20 74 6f 20  s are synced to 
125a3 64 69 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 53  disk together. S
125a4 6f 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 74  o, if the page t
125a5 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a  his function.  *
125a6 2a 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 6d  * is trying to m
125a7 61 6b 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 72  ake clean will r
125a8 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c  equire a journal
125a9 20 73 79 6e 63 20 61 6e 64 20 74 68 65 20 64 6f   sync and the do
125aa 4e 6f 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c 61  NotSync.  ** fla
125ab 67 20 69 73 20 73 65 74 2c 20 72 65 74 75 72 6e  g is set, return
125ac 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61   without doing a
125ad 6e 79 74 68 69 6e 67 2e 20 54 68 65 20 70 63 61  nything. The pca
125ae 63 68 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a 20  che layer will. 
125af 20 2a 2a 20 6a 75 73 74 20 68 61 76 65 20 74 6f   ** just have to
125b0 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 61 6c   go ahead and al
125b1 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67  locate a new pag
125b2 65 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64  e buffer instead
125b3 20 6f 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e 67   of.  ** reusing
125b4 20 70 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20   pPg..  **.  ** 
125b5 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68  Similarly, if th
125b6 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72 65  e pager has alre
125b7 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20  ady entered the 
125b8 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f 20  error state, do 
125b9 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20  not.  ** try to 
125ba 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
125bb 74 73 20 6f 66 20 70 50 67 20 74 6f 20 64 69 73  ts of pPg to dis
125bc 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  k..  */.  if( pP
125bd 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 7c 7c  ager->errCode ||
125be 20 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53   (pPager->doNotS
125bf 79 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61 67  ync && pPg->flag
125c0 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
125c1 43 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  C) ){.    return
125c2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
125c3 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a  .  /* Sync the j
125c4 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 72  ournal file if r
125c5 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66  equired. */.  if
125c6 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48  ( pPg->flags&PGH
125c7 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a  DR_NEED_SYNC ){.
125c8 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75      rc = syncJou
125c9 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20  rnal(pPager);.  
125ca 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
125cb 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 66  _OK && pPager->f
125cc 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 20  ullSync && .    
125cd 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72    !(pPager->jour
125ce 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
125cf 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
125d0 59 29 20 26 26 0a 20 20 20 20 20 20 21 28 73 71  Y) &&.      !(sq
125d1 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61  lite3OsDeviceCha
125d2 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61  racteristics(pPa
125d3 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f  ger->fd)&SQLITE_
125d4 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e  IOCAP_SAFE_APPEN
125d5 44 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  D).    ){.      
125d6 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30  pPager->nRec = 0
125d7 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 69  ;.      rc = wri
125d8 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  teJournalHdr(pPa
125d9 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ger);.    }.  }.
125da 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67  .  /* If the pag
125db 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 69 73  e number of this
125dc 20 70 61 67 65 20 69 73 20 6c 61 72 67 65 72 20   page is larger 
125dd 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74  than the current
125de 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74 68   size of.  ** th
125df 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  e database image
125e0 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f  , it may need to
125e1 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74   be written to t
125e2 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a  he sub-journal..
125e3 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65 63    ** This is bec
125e4 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f  ause the call to
125e5 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67   pager_write_pag
125e6 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77 69  elist() below wi
125e7 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74 75  ll not.  ** actu
125e8 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61 20  ally write data 
125e9 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20 74  to the file in t
125ea 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a 20  his case..  **. 
125eb 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65   ** Consider the
125ec 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75 65   following seque
125ed 6e 63 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a 20  nce of events:. 
125ee 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 4e   **.  **   BEGIN
125ef 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72  ;.  **     <jour
125f0 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a 2a  nal page X>.  **
125f1 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 61 67       <modify pag
125f2 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53 41  e X>.  **     SA
125f3 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a 2a  VEPOINT sp;.  **
125f4 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20 64         <shrink d
125f5 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
125f6 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20 20  Y pages>.  **   
125f7 20 20 20 20 70 61 67 65 72 53 74 72 65 73 73 28      pagerStress(
125f8 70 61 67 65 20 58 29 0a 20 20 2a 2a 20 20 20 20  page X).  **    
125f9 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 3b   ROLLBACK TO sp;
125fa 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28 58  .  **.  ** If (X
125fb 3e 59 29 2c 20 74 68 65 6e 20 77 68 65 6e 20 70  >Y), then when p
125fc 61 67 65 72 53 74 72 65 73 73 20 69 73 20 63 61  agerStress is ca
125fd 6c 6c 65 64 20 70 61 67 65 20 58 20 77 69 6c 6c  lled page X will
125fe 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 0a   not be written.
125ff 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20    ** out to the 
12600 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62  database file, b
12601 75 74 20 77 69 6c 6c 20 62 65 20 64 72 6f 70 70  ut will be dropp
12602 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68  ed from the cach
12603 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66 6f  e. Then,.  ** fo
12604 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 4f 4c  llowing the "ROL
12605 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 73 74 61  LBACK TO sp" sta
12606 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67 20  tement, reading 
12607 70 61 67 65 20 58 20 77 69 6c 6c 20 72 65 61 64  page X will read
12608 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20  .  ** data from 
12609 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1260a 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20  e. This will be 
1260b 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67 65  the copy of page
1260c 20 58 20 61 73 20 69 74 0a 20 20 2a 2a 20 77 61   X as it.  ** wa
1260d 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73  s when the trans
1260e 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c 20  action started, 
1260f 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77 68  not as it was wh
12610 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73 70  en "SAVEPOINT sp
12611 22 0a 20 20 2a 2a 20 77 61 73 20 65 78 65 63 75  ".  ** was execu
12612 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ted..  **.  ** T
12613 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74  he solution is t
12614 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72  o write the curr
12615 65 6e 74 20 64 61 74 61 20 66 6f 72 20 70 61 67  ent data for pag
12616 65 20 58 20 69 6e 74 6f 20 74 68 65 20 0a 20 20  e X into the .  
12617 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66  ** sub-journal f
12618 69 6c 65 20 6e 6f 77 20 28 69 66 20 69 74 20 69  ile now (if it i
12619 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 74 68  s not already th
1261a 65 72 65 29 2c 20 73 6f 20 74 68 61 74 20 69 74  ere), so that it
1261b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65   will.  ** be re
1261c 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 63 75  stored to its cu
1261d 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68 65 6e  rrent value when
1261e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54   the "ROLLBACK T
1261f 4f 20 73 70 22 20 69 73 20 0a 20 20 2a 2a 20 65  O sp" is .  ** e
12620 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  xecuted..  */.  
12621 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12622 4b 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70  K && pPg->pgno>p
12623 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26  Pager->dbSize &&
12624 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67   subjRequiresPag
12625 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 72 63  e(pPg) ){.    rc
12626 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67   = subjournalPag
12627 65 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f  e(pPg);.  }..  /
12628 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e 74  * Write the cont
12629 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
1262a 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
1262b 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20  base file. */.  
1262c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1262d 4b 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 70 44  K ){.    pPg->pD
1262e 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 63  irty = 0;.    rc
1262f 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70   = pager_write_p
12630 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a 20 20  agelist(pPg);.  
12631 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65  }..  /* Mark the
12632 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20   page as clean. 
12633 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
12634 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 41  ITE_OK ){.    PA
12635 47 45 52 54 52 41 43 45 28 28 22 53 54 52 45 53  GERTRACE(("STRES
12636 53 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c  S %d page %d\n",
12637 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
12638 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20  , pPg->pgno));. 
12639 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
1263a 4d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a  MakeClean(pPg);.
1263b 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 61    }..  return pa
1263c 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
1263d 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  , rc);.}.../*.**
1263e 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e   Allocate and in
1263f 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 20 50  itialize a new P
12640 61 67 65 72 20 6f 62 6a 65 63 74 20 61 6e 64 20  ager object and 
12641 70 75 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  put a pointer to
12642 20 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50 61 67   it.** in *ppPag
12643 65 72 2e 20 54 68 65 20 70 61 67 65 72 20 73 68  er. The pager sh
12644 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 20  ould eventually 
12645 62 65 20 66 72 65 65 64 20 62 79 20 70 61 73 73  be freed by pass
12646 69 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73 71 6c  ing it.** to sql
12647 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 29  ite3PagerClose()
12648 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46 69 6c  ..**.** The zFil
12649 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69  ename argument i
1264a 73 20 74 68 65 20 70 61 74 68 20 74 6f 20 74 68  s the path to th
1264b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1264c 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66 20 7a  to open..** If z
1264d 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c  Filename is NULL
1264e 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79   then a randomly
1264f 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 72 79  -named temporary
12650 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64   file is created
12651 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 73 20  .** and used as 
12652 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 63  the file to be c
12653 61 63 68 65 64 2e 20 54 65 6d 70 6f 72 61 72 79  ached. Temporary
12654 20 66 69 6c 65 73 20 61 72 65 20 62 65 20 64 65   files are be de
12655 6c 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74  leted.** automat
12656 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 79  ically when they
12657 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 49 66 20   are closed. If 
12658 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d  zFilename is ":m
12659 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a 2a 2a  emory:" then .**
1265a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   all information
1265b 20 69 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68   is held in cach
1265c 65 2e 20 49 74 20 69 73 20 6e 65 76 65 72 20 77  e. It is never w
1265d 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20  ritten to disk. 
1265e 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 62 65 20  .** This can be 
1265f 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
12660 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  t an in-memory d
12661 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  atabase..**.** T
12662 68 65 20 6e 45 78 74 72 61 20 70 61 72 61 6d 65  he nExtra parame
12663 74 65 72 20 73 70 65 63 69 66 69 65 73 20 74 68  ter specifies th
12664 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
12665 73 20 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63  s of space alloc
12666 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69  ated.** along wi
12667 74 68 20 65 61 63 68 20 70 61 67 65 20 72 65 66  th each page ref
12668 65 72 65 6e 63 65 2e 20 54 68 69 73 20 73 70 61  erence. This spa
12669 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  ce is available 
1266a 74 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 76  to the user.** v
1266b 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 50 61  ia the sqlite3Pa
1266c 67 65 72 47 65 74 45 78 74 72 61 28 29 20 41 50  gerGetExtra() AP
1266d 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61  I..**.** The fla
1266e 67 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20 75  gs argument is u
1266f 73 65 64 20 74 6f 20 73 70 65 63 69 66 79 20 70  sed to specify p
12670 72 6f 70 65 72 74 69 65 73 20 74 68 61 74 20 61  roperties that a
12671 66 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f 70 65  ffect the.** ope
12672 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61  ration of the pa
12673 67 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 62  ger. It should b
12674 65 20 70 61 73 73 65 64 20 73 6f 6d 65 20 62 69  e passed some bi
12675 74 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 69 6f  twise combinatio
12676 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 47 45  n.** of the PAGE
12677 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 61  R_OMIT_JOURNAL a
12678 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44  nd PAGER_NO_READ
12679 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a  LOCK flags..**.*
1267a 2a 20 54 68 65 20 76 66 73 46 6c 61 67 73 20 70  * The vfsFlags p
1267b 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 69  arameter is a bi
1267c 74 6d 61 73 6b 20 74 6f 20 70 61 73 73 20 74 6f  tmask to pass to
1267d 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   the flags param
1267e 65 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 78  eter.** of the x
1267f 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 6f 66  Open() method of
12680 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 56 46   the supplied VF
12681 53 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 66  S when opening f
12682 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  iles. .**.** If 
12683 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74  the pager object
12684 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
12685 64 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  d the specified 
12686 66 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a 2a 20  file opened .** 
12687 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 53 51  successfully, SQ
12688 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
12689 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 72  ned and *ppPager
1268a 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
1268b 0a 2a 2a 20 74 68 65 20 6e 65 77 20 70 61 67 65  .** the new page
1268c 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 6e 20  r object. If an 
1268d 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70  error occurs, *p
1268e 70 50 61 67 65 72 20 69 73 20 73 65 74 20 74 6f  pPager is set to
1268f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65 72 72   NULL.** and err
12690 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64  or code returned
12691 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
12692 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54  may return SQLIT
12693 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71 6c 69  E_NOMEM.** (sqli
12694 74 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73 20 75  te3Malloc() is u
12695 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  sed to allocate 
12696 6d 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54 45 5f  memory), SQLITE_
12697 43 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a 2a 20  CANTOPEN or .** 
12698 76 61 72 69 6f 75 73 20 53 51 4c 49 54 45 5f 49  various SQLITE_I
12699 4f 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a 2a 2f  O_XXX errors..*/
1269a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1269b 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
1269c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  Open(.  sqlite3_
1269d 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
1269e 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
1269f 66 69 6c 65 20 73 79 73 74 65 6d 20 74 6f 20 75  file system to u
126a0 73 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 2a  se */.  Pager **
126a1 70 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20  ppPager,        
126a2 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20   /* OUT: Return 
126a3 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74  the Pager struct
126a4 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f  ure here */.  co
126a5 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
126a6 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ame,   /* Name o
126a7 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
126a8 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20  ile to open */. 
126a9 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 20   int nExtra,    
126aa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
126ab 72 61 20 62 79 74 65 73 20 61 70 70 65 6e 64 20  ra bytes append 
126ac 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72  to each in-memor
126ad 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  y page */.  int 
126ae 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
126af 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63 6f       /* flags co
126b0 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20 66  ntrolling this f
126b1 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73  ile */.  int vfs
126b2 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20  Flags           
126b3 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 73 65    /* flags passe
126b4 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 6c  d through to sql
126b5 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29  ite3_vfs.xOpen()
126b6 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 50 74   */.){.  u8 *pPt
126b7 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  r;.  Pager *pPag
126b8 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  er = 0;       /*
126b9 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 74 6f   Pager object to
126ba 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65   allocate and re
126bb 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63  turn */.  int rc
126bc 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
126bd 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
126be 65 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d 70 46  e */.  int tempF
126bf 69 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ile = 0;        
126c0 2f 2a 20 54 72 75 65 20 66 6f 72 20 74 65 6d 70  /* True for temp
126c1 20 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20 69 6e   files (incl. in
126c2 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29 20 2a  -memory files) *
126c3 2f 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20  /.  int memDb = 
126c4 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0;           /* 
126c5 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
126c6 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c  an in-memory fil
126c7 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61 64 4f  e */.  int readO
126c8 6e 6c 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20  nly = 0;        
126c9 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
126ca 69 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 66  is a read-only f
126cb 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a 6f 75  ile */.  int jou
126cc 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20 20 20  rnalFileSize;   
126cd 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c    /* Bytes to al
126ce 6c 6f 63 61 74 65 20 66 6f 72 20 65 61 63 68 20  locate for each 
126cf 6a 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a 20 20  journal fd */.  
126d0 63 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d 65 20  char *zPathname 
126d1 3d 20 30 3b 20 20 20 20 20 2f 2a 20 46 75 6c 6c  = 0;     /* Full
126d2 20 70 61 74 68 20 74 6f 20 64 61 74 61 62 61 73   path to databas
126d3 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  e file */.  int 
126d4 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20  nPathname = 0;  
126d5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
126d6 66 20 62 79 74 65 73 20 69 6e 20 7a 50 61 74 68  f bytes in zPath
126d7 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73  name */.  int us
126d8 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67  eJournal = (flag
126d9 73 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a  s & PAGER_OMIT_J
126da 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20 46  OURNAL)==0; /* F
126db 61 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a 6f 75  alse to omit jou
126dc 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  rnal */.  int no
126dd 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67  Readlock = (flag
126de 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41  s & PAGER_NO_REA
126df 44 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a 20 54  DLOCK)!=0;  /* T
126e0 72 75 65 20 74 6f 20 6f 6d 69 74 20 72 65 61 64  rue to omit read
126e1 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 70  -lock */.  int p
126e2 63 61 63 68 65 53 69 7a 65 20 3d 20 73 71 6c 69  cacheSize = sqli
126e3 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 3b  te3PcacheSize();
126e4 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
126e5 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20  to allocate for 
126e6 50 43 61 63 68 65 20 2a 2f 0a 20 20 75 31 36 20  PCache */.  u16 
126e7 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c  szPageDflt = SQL
126e8 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  ITE_DEFAULT_PAGE
126e9 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66 61 75  _SIZE;  /* Defau
126ea 6c 74 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a  lt page size */.
126eb 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
126ec 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20   how much space 
126ed 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  is required for 
126ee 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  each journal fil
126ef 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 28 74  e-handle.  ** (t
126f0 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f 66 20  here are two of 
126f1 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e 20 6a  them, the main j
126f2 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 73  ournal and the s
126f3 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 68 69  ub-journal). Thi
126f4 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 6d 61  s.  ** is the ma
126f5 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65 71 75  ximum space requ
126f6 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d  ired for an in-m
126f7 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69  emory journal fi
126f8 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a 2a 20  le handle .  ** 
126f9 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20 6a 6f  and a regular jo
126fa 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c  urnal file-handl
126fb 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 20 22  e. Note that a "
126fc 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 2d  regular journal-
126fd 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d 61 79  handle".  ** may
126fe 20 62 65 20 61 20 77 72 61 70 70 65 72 20 63 61   be a wrapper ca
126ff 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69 6e 67  pable of caching
12700 20 74 68 65 20 66 69 72 73 74 20 70 6f 72 74 69   the first porti
12701 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  on of the journa
12702 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e 20 6d  l.  ** file in m
12703 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65 6d 65  emory to impleme
12704 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72  nt the atomic-wr
12705 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ite optimization
12706 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f 75 72   (see .  ** sour
12707 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e  ce file journal.
12708 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  c)..  */.  if( s
12709 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a  qlite3JournalSiz
1270a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65 33 4d  e(pVfs)>sqlite3M
1270b 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20  emJournalSize() 
1270c 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69  ){.    journalFi
1270d 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28  leSize = ROUND8(
1270e 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69  sqlite3JournalSi
1270f 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d 65 6c  ze(pVfs));.  }el
12710 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46  se{.    journalF
12711 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38  ileSize = ROUND8
12712 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e  (sqlite3MemJourn
12713 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d 0a 0a  alSize());.  }..
12714 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 74    /* Set the out
12715 70 75 74 20 76 61 72 69 61 62 6c 65 20 74 6f 20  put variable to 
12716 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61 6e 20  NULL in case an 
12717 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f  error occurs. */
12718 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 30 3b  .  *ppPager = 0;
12719 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 61  ..  /* Compute a
1271a 6e 64 20 73 74 6f 72 65 20 74 68 65 20 66 75 6c  nd store the ful
1271b 6c 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 61 6e  l pathname in an
1271c 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65   allocated buffe
1271d 72 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a 20 74  r pointed.  ** t
1271e 6f 20 62 79 20 7a 50 61 74 68 6e 61 6d 65 2c 20  o by zPathname, 
1271f 6c 65 6e 67 74 68 20 6e 50 61 74 68 6e 61 6d 65  length nPathname
12720 2e 20 4f 72 2c 20 69 66 20 74 68 69 73 20 69 73  . Or, if this is
12721 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
12722 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20 62 6f  e,.  ** leave bo
12723 74 68 20 6e 50 61 74 68 6e 61 6d 65 20 61 6e 64  th nPathname and
12724 20 7a 50 61 74 68 6e 61 6d 65 20 73 65 74 20 74   zPathname set t
12725 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  o 0..  */.  if( 
12726 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69  zFilename && zFi
12727 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20  lename[0] ){.   
12728 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70 56 66   nPathname = pVf
12729 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b  s->mxPathname+1;
1272a 0a 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 20 3d  .    zPathname =
1272b 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e   sqlite3Malloc(n
1272c 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 20 20  Pathname*2);.   
1272d 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 3d 3d   if( zPathname==
1272e 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
1272f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
12730 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
12731 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59  LITE_OMIT_MEMORY
12732 44 42 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d  DB.    if( strcm
12733 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d 65  p(zFilename,":me
12734 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a 20 20  mory:")==0 ){.  
12735 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a 20      memDb = 1;. 
12736 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30       zPathname[0
12737 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  ] = 0;.    }else
12738 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20  .#endif.    {.  
12739 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d      zPathname[0]
1273a 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20 73 75   = 0; /* Make su
1273b 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 65  re initialized e
1273c 76 65 6e 20 69 66 20 46 75 6c 6c 50 61 74 68 6e  ven if FullPathn
1273d 61 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f 0a 20  ame() fails */. 
1273e 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1273f 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  3OsFullPathname(
12740 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  pVfs, zFilename,
12741 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 61 74   nPathname, zPat
12742 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 0a 20  hname);.    }.. 
12743 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 73     nPathname = s
12744 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
12745 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 69  Pathname);.    i
12746 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12747 20 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b 38 3e   && nPathname+8>
12748 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
12749 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
1274a 73 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b 65  s branch is take
1274b 6e 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  n when the journ
1274c 61 6c 20 70 61 74 68 20 72 65 71 75 69 72 65 64  al path required
1274d 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   by.      ** the
1274e 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20   database being 
1274f 6f 70 65 6e 65 64 20 77 69 6c 6c 20 62 65 20 6d  opened will be m
12750 6f 72 65 20 74 68 61 6e 20 70 56 66 73 2d 3e 6d  ore than pVfs->m
12751 78 50 61 74 68 6e 61 6d 65 0a 20 20 20 20 20 20  xPathname.      
12752 2a 2a 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67  ** bytes in leng
12753 74 68 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74  th. This means t
12754 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e  he database cann
12755 6f 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a 20 20  ot be opened,.  
12756 20 20 20 20 2a 2a 20 61 73 20 69 74 20 77 69 6c      ** as it wil
12757 6c 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c  l not be possibl
12758 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 6a 6f  e to open the jo
12759 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 65 76  urnal file or ev
1275a 65 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68 65 63  en.      ** chec
1275b 6b 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72  k for a hot-jour
1275c 6e 61 6c 20 62 65 66 6f 72 65 20 72 65 61 64 69  nal before readi
1275d 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ng..      */.   
1275e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
1275f 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20  ANTOPEN;.    }. 
12760 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
12761 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
12762 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68  lite3_free(zPath
12763 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74  name);.      ret
12764 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
12765 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
12766 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20   memory for the 
12767 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 2c  Pager structure,
12768 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2c 20   PCache object, 
12769 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 20 66  the.  ** three f
1276a 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c  ile descriptors,
1276b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1276c 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20  le name and the 
1276d 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69  journal .  ** fi
1276e 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c 61 79  le name. The lay
1276f 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 69 73  out in memory is
12770 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a   as follows:.  *
12771 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 65 72  *.  **     Pager
12772 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20   object         
12773 20 20 20 20 20 20 20 20 20 20 20 28 73 69 7a 65             (size
12774 6f 66 28 50 61 67 65 72 29 20 62 79 74 65 73 29  of(Pager) bytes)
12775 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63 68 65  .  **     PCache
12776 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20   object         
12777 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 74            (sqlit
12778 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 20 62  e3PcacheSize() b
12779 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44  ytes).  **     D
1277a 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e  atabase file han
1277b 64 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 28  dle            (
1277c 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62  pVfs->szOsFile b
1277d 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 53  ytes).  **     S
1277e 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ub-journal file 
1277f 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 28  handle         (
12780 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20  journalFileSize 
12781 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20  bytes).  **     
12782 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  Main journal fil
12783 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20  e handle        
12784 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65  (journalFileSize
12785 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20   bytes).  **    
12786 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e   Database file n
12787 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ame             
12788 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 62 79   (nPathname+1 by
12789 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4a 6f  tes).  **     Jo
1278a 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
1278b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6e                (n
1278c 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 79 74  Pathname+8+1 byt
1278d 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 72 20  es).  */.  pPtr 
1278e 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 4d  = (u8 *)sqlite3M
1278f 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 52  allocZero(.    R
12790 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50  OUND8(sizeof(*pP
12791 61 67 65 72 29 29 20 2b 20 20 20 20 20 20 2f 2a  ager)) +      /*
12792 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65   Pager structure
12793 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70   */.    ROUND8(p
12794 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20 20 20  cacheSize) +    
12795 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63 68 65         /* PCache
12796 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 52   object */.    R
12797 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73  OUND8(pVfs->szOs
12798 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a  File) +       /*
12799 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c   The main db fil
1279a 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c  e */.    journal
1279b 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 20  FileSize * 2 +  
1279c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
1279d 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  wo journal files
1279e 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e 61   */ .    nPathna
1279f 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20 20 20  me + 1 +        
127a0 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65          /* zFile
127a1 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 61 74  name */.    nPat
127a2 68 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20 20 20  hname + 8 + 1   
127a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4a             /* zJ
127a4 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a 20  ournal */.  );. 
127a5 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42   assert( EIGHT_B
127a6 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 51  YTE_ALIGNMENT(SQ
127a7 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
127a8 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29  journalFileSize)
127a9 29 20 29 3b 0a 20 20 69 66 28 20 21 70 50 74 72  ) );.  if( !pPtr
127aa 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
127ab 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b  free(zPathname);
127ac 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
127ad 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
127ae 70 50 61 67 65 72 20 3d 20 20 20 20 20 20 20 20  pPager =        
127af 20 20 20 20 20 20 28 50 61 67 65 72 2a 29 28 70        (Pager*)(p
127b0 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  Ptr);.  pPager->
127b1 70 50 43 61 63 68 65 20 3d 20 20 20 20 28 50 43  pPCache =    (PC
127b2 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d 20 52  ache*)(pPtr += R
127b3 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50  OUND8(sizeof(*pP
127b4 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61 67 65  ager)));.  pPage
127b5 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c 69 74  r->fd =   (sqlit
127b6 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b  e3_file*)(pPtr +
127b7 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65 53  = ROUND8(pcacheS
127b8 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 72 2d  ize));.  pPager-
127b9 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 65 33  >sjfd = (sqlite3
127ba 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20  _file*)(pPtr += 
127bb 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f  ROUND8(pVfs->szO
127bc 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 67 65  sFile));.  pPage
127bd 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c 69 74  r->jfd =  (sqlit
127be 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b  e3_file*)(pPtr +
127bf 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a  = journalFileSiz
127c0 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46  e);.  pPager->zF
127c1 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 63 68  ilename =    (ch
127c2 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75  ar*)(pPtr += jou
127c3 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20  rnalFileSize);. 
127c4 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42   assert( EIGHT_B
127c5 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 50  YTE_ALIGNMENT(pP
127c6 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20  ager->jfd) );.. 
127c7 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20   /* Fill in the 
127c8 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65 20  Pager.zFilename 
127c9 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72 6e  and Pager.zJourn
127ca 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66 20 72  al buffers, if r
127cb 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66  equired. */.  if
127cc 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20  ( zPathname ){. 
127cd 20 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72     pPager->zJour
127ce 6e 61 6c 20 3d 20 20 20 28 63 68 61 72 2a 29 28  nal =   (char*)(
127cf 70 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e 61 6d  pPtr += nPathnam
127d0 65 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65 6d 63  e + 1);.    memc
127d1 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  py(pPager->zFile
127d2 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c  name, zPathname,
127d3 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20   nPathname);.   
127d4 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e   memcpy(pPager->
127d5 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74 68 6e  zJournal, zPathn
127d6 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b  ame, nPathname);
127d7 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61  .    memcpy(&pPa
127d8 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e 50  ger->zJournal[nP
127d9 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f 75 72  athname], "-jour
127da 6e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20 73 71  nal", 8);.    sq
127db 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68  lite3_free(zPath
127dc 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 50 61  name);.  }.  pPa
127dd 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 56 66 73  ger->pVfs = pVfs
127de 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76 66 73 46  ;.  pPager->vfsF
127df 6c 61 67 73 20 3d 20 76 66 73 46 6c 61 67 73 3b  lags = vfsFlags;
127e0 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
127e1 70 61 67 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f  pager file..  */
127e2 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65  .  if( zFilename
127e3 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d   && zFilename[0]
127e4 20 26 26 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20   && !memDb ){.  
127e5 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b 20    int fout = 0; 
127e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
127e7 20 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73 20     /* VFS flags 
127e8 72 65 74 75 72 6e 65 64 20 62 79 20 78 4f 70 65  returned by xOpe
127e9 6e 28 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  n() */.    rc = 
127ea 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56  sqlite3OsOpen(pV
127eb 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c  fs, pPager->zFil
127ec 65 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66  ename, pPager->f
127ed 64 2c 20 76 66 73 46 6c 61 67 73 2c 20 26 66 6f  d, vfsFlags, &fo
127ee 75 74 29 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c  ut);.    readOnl
127ef 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 54 45  y = (fout&SQLITE
127f0 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b  _OPEN_READONLY);
127f1 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
127f2 66 69 6c 65 20 77 61 73 20 73 75 63 63 65 73 73  file was success
127f3 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72  fully opened for
127f4 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65   read/write acce
127f5 73 73 2c 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73  ss,.    ** choos
127f6 65 20 61 20 64 65 66 61 75 6c 74 20 70 61 67 65  e a default page
127f7 20 73 69 7a 65 20 69 6e 20 63 61 73 65 20 77 65   size in case we
127f8 20 68 61 76 65 20 74 6f 20 63 72 65 61 74 65 20   have to create 
127f9 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  the.    ** datab
127fa 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 64 65  ase file. The de
127fb 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20  fault page size 
127fc 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f  is the maximum o
127fd 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  f:.    **.    **
127fe 20 20 20 20 2b 20 53 51 4c 49 54 45 5f 44 45 46      + SQLITE_DEF
127ff 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a  AULT_PAGE_SIZE,.
12800 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20      **    + The 
12801 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
12802 79 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f  y sqlite3OsSecto
12803 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20  rSize().    **  
12804 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20    + The largest 
12805 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63  page size that c
12806 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74  an be written at
12807 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f  omically..    */
12808 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
12809 49 54 45 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f  ITE_OK && !readO
1280a 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 73 65 74  nly ){.      set
1280b 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65  SectorSize(pPage
1280c 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  r);.      assert
1280d 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  (SQLITE_DEFAULT_
1280e 50 41 47 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54  PAGE_SIZE<=SQLIT
1280f 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
12810 47 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20  GE_SIZE);.      
12811 69 66 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 70  if( szPageDflt<p
12812 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
12813 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  e ){.        if(
12814 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53   pPager->sectorS
12815 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44  ize>SQLITE_MAX_D
12816 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
12817 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a   ){.          sz
12818 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54  PageDflt = SQLIT
12819 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
1281a 47 45 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20  GE_SIZE;.       
1281b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1281c 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 28    szPageDflt = (
1281d 75 31 36 29 70 50 61 67 65 72 2d 3e 73 65 63 74  u16)pPager->sect
1281e 6f 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20  orSize;.        
1281f 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66  }.      }.#ifdef
12820 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
12821 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20  TOMIC_WRITE.    
12822 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20    {.        int 
12823 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44  iDc = sqlite3OsD
12824 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
12825 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29  tics(pPager->fd)
12826 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 69  ;.        int ii
12827 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
12828 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54  (SQLITE_IOCAP_AT
12829 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38  OMIC512==(512>>8
1282a 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  ));.        asse
1282b 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  rt(SQLITE_IOCAP_
1282c 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33  ATOMIC64K==(6553
1282d 36 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20  6>>8));.        
1282e 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 4d 41  assert(SQLITE_MA
1282f 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
12830 49 5a 45 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20  IZE<=65536);.   
12831 20 20 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61       for(ii=szPa
12832 67 65 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49  geDflt; ii<=SQLI
12833 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50  TE_MAX_DEFAULT_P
12834 41 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a  AGE_SIZE; ii=ii*
12835 32 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  2){.          if
12836 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f  ( iDc&(SQLITE_IO
12837 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e  CAP_ATOMIC|(ii>>
12838 38 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  8)) ){.         
12839 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20     szPageDflt = 
1283a 69 69 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ii;.          }.
1283b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1283c 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20  }.#endif.    }. 
1283d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49   }else{.    /* I
1283e 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  f a temporary fi
1283f 6c 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  le is requested,
12840 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65   it is not opene
12841 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20  d immediately.. 
12842 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61     ** In this ca
12843 73 65 20 77 65 20 61 63 63 65 70 74 20 74 68 65  se we accept the
12844 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69   default page si
12845 7a 65 20 61 6e 64 20 64 65 6c 61 79 20 61 63 74  ze and delay act
12846 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65  ually.    ** ope
12847 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 75 6e  ning the file un
12848 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 63 61  til the first ca
12849 6c 6c 20 74 6f 20 4f 73 57 72 69 74 65 28 29 2e  ll to OsWrite().
1284a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1284b 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 61 6c  his branch is al
1284c 73 6f 20 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e  so run for an in
1284d 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
1284e 2e 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20  . An in-memory. 
1284f 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69     ** database i
12850 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 20  s the same as a 
12851 74 65 6d 70 2d 66 69 6c 65 20 74 68 61 74 20 69  temp-file that i
12852 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20  s never written 
12853 6f 75 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69  out to.    ** di
12854 73 6b 20 61 6e 64 20 75 73 65 73 20 61 6e 20 69  sk and uses an i
12855 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63  n-memory rollbac
12856 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a  k journal..    *
12857 2f 20 0a 20 20 20 20 74 65 6d 70 46 69 6c 65 20  / .    tempFile 
12858 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  = 1;.    pPager-
12859 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45  >state = PAGER_E
1285a 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 0a 20  XCLUSIVE;.  }.. 
1285b 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
1285c 67 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72 53  g call to PagerS
1285d 65 74 50 61 67 65 73 69 7a 65 28 29 20 73 65 72  etPagesize() ser
1285e 76 65 73 20 74 6f 20 73 65 74 20 74 68 65 20 76  ves to set the v
1285f 61 6c 75 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61  alue of .  ** Pa
12860 67 65 72 2e 70 61 67 65 53 69 7a 65 20 61 6e 64  ger.pageSize and
12861 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65   to allocate the
12862 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65   Pager.pTmpSpace
12863 20 62 75 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20   buffer..  */.  
12864 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12865 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  K ){.    assert(
12866 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d   pPager->memDb==
12867 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  0 );.    rc = sq
12868 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67  lite3PagerSetPag
12869 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 73  esize(pPager, &s
1286a 7a 50 61 67 65 44 66 6c 74 29 3b 0a 20 20 20 20  zPageDflt);.    
1286b 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51  testcase( rc!=SQ
1286c 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a  LITE_OK );.  }..
1286d 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72    /* If an error
1286e 20 6f 63 63 75 72 72 65 64 20 69 6e 20 65 69 74   occurred in eit
1286f 68 65 72 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b  her of the block
12870 73 20 61 62 6f 76 65 2c 20 66 72 65 65 20 74 68  s above, free th
12871 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 20 73 74  e .  ** Pager st
12872 72 75 63 74 75 72 65 20 61 6e 64 20 63 6c 6f 73  ructure and clos
12873 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f  e the file..  */
12874 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
12875 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  E_OK ){.    asse
12876 72 74 28 20 21 70 50 61 67 65 72 2d 3e 70 54 6d  rt( !pPager->pTm
12877 70 53 70 61 63 65 20 29 3b 0a 20 20 20 20 73 71  pSpace );.    sq
12878 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
12879 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73 71  ger->fd);.    sq
1287a 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65  lite3_free(pPage
1287b 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  r);.    return r
1287c 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69  c;.  }..  /* Ini
1287d 74 69 61 6c 69 7a 65 20 74 68 65 20 50 43 61 63  tialize the PCac
1287e 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20  he object. */.  
1287f 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e 44 38 28  nExtra = ROUND8(
12880 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 6c 69 74  nExtra);.  sqlit
12881 65 33 50 63 61 63 68 65 4f 70 65 6e 28 73 7a 50  e3PcacheOpen(szP
12882 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 72 61 2c  ageDflt, nExtra,
12883 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 20 20 20   !memDb,.       
12884 20 20 20 20 20 20 20 20 20 20 20 20 20 21 6d 65               !me
12885 6d 44 62 3f 70 61 67 65 72 53 74 72 65 73 73 3a  mDb?pagerStress:
12886 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67 65  0, (void *)pPage
12887 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63  r, pPager->pPCac
12888 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 54 52 41  he);..  PAGERTRA
12889 43 45 28 28 22 4f 50 45 4e 20 25 64 20 25 73 5c  CE(("OPEN %d %s\
1288a 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c 45 49 44  n", FILEHANDLEID
1288b 28 70 50 61 67 65 72 2d 3e 66 64 29 2c 20 70 50  (pPager->fd), pP
1288c 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29  ager->zFilename)
1288d 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4f  );.  IOTRACE(("O
1288e 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 70 50  PEN %p %s\n", pP
1288f 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 7a 46  ager, pPager->zF
12890 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 70 50 61  ilename))..  pPa
12891 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
12892 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61 6c  = (u8)useJournal
12893 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65  ;.  pPager->noRe
12894 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 65 61 64  adlock = (noRead
12895 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c 79  lock && readOnly
12896 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 70 50 61  ) ?1:0;.  /* pPa
12897 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20  ger->stmtOpen = 
12898 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65  0; */.  /* pPage
12899 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 30  r->stmtInUse = 0
1289a 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
1289b 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a 2f 0a 20  ->nRef = 0; */. 
1289c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
1289d 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 6d 44 62  alid = (u8)memDb
1289e 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73  ;.  /* pPager->s
1289f 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a  tmtSize = 0; */.
128a0 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d    /* pPager->stm
128a1 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20  tJSize = 0; */. 
128a2 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61 67   /* pPager->nPag
128a3 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  e = 0; */.  pPag
128a4 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 53 51 4c  er->mxPgno = SQL
128a5 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55  ITE_MAX_PAGE_COU
128a6 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  NT;.  /* pPager-
128a7 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 55  >state = PAGER_U
128a8 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 73 73 65  NLOCK; */.  asse
128a9 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
128aa 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20 3f  e == (tempFile ?
128ab 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
128ac 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 29   : PAGER_UNLOCK)
128ad 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d   );.  /* pPager-
128ae 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f  >errMask = 0; */
128af 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  .  pPager->tempF
128b0 69 6c 65 20 3d 20 28 75 38 29 74 65 6d 70 46 69  ile = (u8)tempFi
128b1 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 74 65  le;.  assert( te
128b2 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f  mpFile==PAGER_LO
128b3 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
128b4 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 74   .          || t
128b5 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c  empFile==PAGER_L
128b6 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
128b7 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74  SIVE );.  assert
128b8 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ( PAGER_LOCKINGM
128b9 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d 31  ODE_EXCLUSIVE==1
128ba 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 78   );.  pPager->ex
128bb 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75  clusiveMode = (u
128bc 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 70  8)tempFile; .  p
128bd 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
128be 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 2d  ntDone = pPager-
128bf 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61  >tempFile;.  pPa
128c0 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 28 75 38  ger->memDb = (u8
128c1 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65 72  )memDb;.  pPager
128c2 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 28 75 38  ->readOnly = (u8
128c3 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 20  )readOnly;.  /* 
128c4 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
128c5 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65   = 0; */.  pPage
128c6 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 28 70 50 61  r->noSync = (pPa
128c7 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 7c 7c  ger->tempFile ||
128c8 20 21 75 73 65 4a 6f 75 72 6e 61 6c 29 20 3f 31   !useJournal) ?1
128c9 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 75  :0;.  pPager->fu
128ca 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d  llSync = pPager-
128cb 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a 20 20  >noSync ?0:1;.  
128cc 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61  pPager->sync_fla
128cd 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43  gs = SQLITE_SYNC
128ce 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50  _NORMAL;.  /* pP
128cf 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 30  ager->pFirst = 0
128d0 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
128d1 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d  ->pFirstSynced =
128d2 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
128d3 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a  er->pLast = 0; *
128d4 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78 74  /.  pPager->nExt
128d5 72 61 20 3d 20 6e 45 78 74 72 61 3b 0a 20 20 70  ra = nExtra;.  p
128d6 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69  Pager->journalSi
128d7 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45  zeLimit = SQLITE
128d8 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c  _DEFAULT_JOURNAL
128d9 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 61  _SIZE_LIMIT;.  a
128da 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
128db 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 6d  ager->fd) || tem
128dc 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 65  pFile );.  setSe
128dd 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29  ctorSize(pPager)
128de 3b 0a 20 20 69 66 28 20 6d 65 6d 44 62 20 29 7b  ;.  if( memDb ){
128df 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
128e0 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52  rnalMode = PAGER
128e1 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
128e2 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50  ORY;.  }.  /* pP
128e3 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c  ager->xBusyHandl
128e4 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20  er = 0; */.  /* 
128e5 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e  pPager->pBusyHan
128e6 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f 0a  dlerArg = 0; */.
128e7 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 61 67    /* memset(pPag
128e8 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 73 69  er->aHash, 0, si
128e9 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 48 61  zeof(pPager->aHa
128ea 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70 50 61  sh)); */.  *ppPa
128eb 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20 20  ger = pPager;.  
128ec 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
128ed 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ;.}..../*.** Thi
128ee 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
128ef 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 6e 73  lled after trans
128f0 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41  itioning from PA
128f1 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a  GER_UNLOCK to.**
128f2 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 74   PAGER_SHARED st
128f3 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 69 66  ate. It tests if
128f4 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20   there is a hot 
128f5 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e 74 20  journal present 
128f6 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73  in.** the file-s
128f7 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 67 69  ystem for the gi
128f8 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 6f 74  ven pager. A hot
128f9 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20   journal is one 
128fa 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74  that .** needs t
128fb 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b  o be played back
128fc 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  . According to t
128fd 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20  his function, a 
128fe 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66  hot-journal.** f
128ff 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 74 68  ile exists if th
12900 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74  e following crit
12901 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a  eria are met:.**
12902 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f 75 72  .**   * The jour
12903 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20  nal file exists 
12904 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 73 74  in the file syst
12905 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e  em, and.**   * N
12906 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  o process holds 
12907 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67 72  a RESERVED or gr
12908 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68  eater lock on th
12909 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
1290a 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20   and.**   * The 
1290b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 74  database file it
1290c 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 72 20  self is greater 
1290d 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e 20  than 0 bytes in 
1290e 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a  size, and.**   *
1290f 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20   The first byte 
12910 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
12911 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 69  ile exists and i
12912 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a  s not 0x00..**.*
12913 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74  * If the current
12914 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
12915 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30 20  abase file is 0 
12916 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  but a journal fi
12917 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68  le.** exists, th
12918 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20 61  at is probably a
12919 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65  n old journal le
1291a 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20 70  ft over from a p
1291b 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73 65  rior.** database
1291c 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e   with the same n
1291d 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ame. In this cas
1291e 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
1291f 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65  le is.** just de
12920 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 44 65  leted using OsDe
12921 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 20 69  lete, *pExists i
12922 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53  s set to 0 and S
12923 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72  QLITE_OK.** is r
12924 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
12925 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  his routine does
12926 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 74 68   not check if th
12927 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 72 20  ere is a master 
12928 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
12929 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f  .** at the end o
1292a 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74  f the file. If t
1292b 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 68 61  here is, and tha
1292c 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  t master journal
1292d 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f   file.** does no
1292e 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 74 68  t exist, then th
1292f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
12930 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74  s not really hot
12931 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73  . In this.** cas
12932 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  e this routine w
12933 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 61 6c  ill return a fal
12934 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 68 65  se-positive. The
12935 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
12936 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c  ).** routine wil
12937 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 74 20  l discover that 
12938 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12939 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68   is not really h
1293a 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20  ot and .** will 
1293b 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b  not roll it back
1293c 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f  . .**.** If a ho
1293d 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  t-journal file i
1293e 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73 74  s found to exist
1293f 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65  , *pExists is se
12940 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53  t to 1 and .** S
12941 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
12942 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75  d. If no hot-jou
12943 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65  rnal file is pre
12944 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20 69  sent, *pExists i
12945 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61 6e  s.** set to 0 an
12946 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
12947 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65  rned. If an IO e
12948 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
12949 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64  e trying.** to d
1294a 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
1294b 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f   or not a hot-jo
1294c 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74  urnal file exist
1294d 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 0a  s, the IO error.
1294e 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  ** code is retur
1294f 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c 75  ned and the valu
12950 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 69 73  e of *pExists is
12951 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73   undefined..*/.s
12952 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 6f 74  tatic int hasHot
12953 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70  Journal(Pager *p
12954 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 78 69  Pager, int *pExi
12955 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  sts){.  sqlite3_
12956 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73  vfs * const pVfs
12957 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b   = pPager->pVfs;
12958 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
12959 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1295a 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
1295b 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b  */.  int exists;
1295c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1295d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20     /* True if a 
1295e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
1295f 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 61 73  present */..  as
12960 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20  sert( pPager!=0 
12961 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
12962 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
12963 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  );.  assert( isO
12964 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
12965 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73  );.  assert( !is
12966 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
12967 29 20 29 3b 0a 0a 20 20 2a 70 45 78 69 73 74 73  ) );..  *pExists
12968 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c   = 0;.  rc = sql
12969 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66  ite3OsAccess(pVf
1296a 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
1296b 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  nal, SQLITE_ACCE
1296c 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73  SS_EXISTS, &exis
1296d 74 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ts);.  if( rc==S
1296e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 78 69 73  QLITE_OK && exis
1296f 74 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6f  ts ){.    int lo
12970 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  cked;           
12971 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
12972 20 73 6f 6d 65 20 70 72 6f 63 65 73 73 20 68 6f   some process ho
12973 6c 64 73 20 61 20 52 45 53 45 52 56 45 44 20 6c  lds a RESERVED l
12974 6f 63 6b 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ock */.    rc = 
12975 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65  sqlite3OsCheckRe
12976 73 65 72 76 65 64 4c 6f 63 6b 28 70 50 61 67 65  servedLock(pPage
12977 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b 65 64 29 3b  r->fd, &locked);
12978 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
12979 49 54 45 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b 65  ITE_OK && !locke
1297a 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  d ){.      int n
1297b 50 61 67 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  Page;..      /* 
1297c 43 68 65 63 6b 20 74 68 65 20 73 69 7a 65 20 6f  Check the size o
1297d 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
1297e 69 6c 65 2e 20 49 66 20 69 74 20 63 6f 6e 73 69  ile. If it consi
1297f 73 74 73 20 6f 66 20 30 20 70 61 67 65 73 2c 0a  sts of 0 pages,.
12980 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 64 65        ** then de
12981 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lete the journal
12982 20 66 69 6c 65 2e 20 53 65 65 20 74 68 65 20 68   file. See the h
12983 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61 62  eader comment ab
12984 6f 76 65 20 66 6f 72 20 0a 20 20 20 20 20 20 2a  ove for .      *
12985 2a 20 74 68 65 20 72 65 61 73 6f 6e 69 6e 67 20  * the reasoning 
12986 68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  here..      */. 
12987 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
12988 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
12989 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b  pPager, &nPage);
1298a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
1298b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1298c 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30      if( nPage==0
1298d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
1298e 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65   = sqlite3OsDele
1298f 74 65 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d  te(pVfs, pPager-
12990 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20  >zJournal, 0);. 
12991 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
12992 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6a          /* The j
12993 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73  ournal file exis
12994 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20  ts and no other 
12995 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 61  connection has a
12996 20 72 65 73 65 72 76 65 64 0a 20 20 20 20 20 20   reserved.      
12997 20 20 20 20 2a 2a 20 6f 72 20 67 72 65 61 74 65      ** or greate
12998 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  r lock on the da
12999 74 61 62 61 73 65 20 66 69 6c 65 2e 20 4e 6f 77  tabase file. Now
1299a 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 72   check that ther
1299b 65 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a  e is.          *
1299c 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6e  * at least one n
1299d 6f 6e 2d 7a 65 72 6f 20 62 79 74 65 73 20 61 74  on-zero bytes at
1299e 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
1299f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
129a0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20            ** If 
129a1 74 68 65 72 65 20 69 73 2c 20 74 68 65 6e 20 77  there is, then w
129a2 65 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73 20  e consider this 
129a3 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 68 6f  journal to be ho
129a4 74 2e 20 49 66 20 6e 6f 74 2c 20 0a 20 20 20 20  t. If not, .    
129a5 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e 20        ** it can 
129a6 62 65 20 69 67 6e 6f 72 65 64 2e 0a 20 20 20 20  be ignored..    
129a7 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
129a8 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54     int f = SQLIT
129a9 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c  E_OPEN_READONLY|
129aa 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
129ab 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20  _JOURNAL;.      
129ac 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
129ad 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61  OsOpen(pVfs, pPa
129ae 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70  ger->zJournal, p
129af 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26  Pager->jfd, f, &
129b0 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  f);.          if
129b1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
129b2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75  ){.            u
129b3 38 20 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 20  8 first = 0;.   
129b4 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
129b5 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67  lite3OsRead(pPag
129b6 65 72 2d 3e 6a 66 64 2c 20 28 76 6f 69 64 20 2a  er->jfd, (void *
129b7 29 26 66 69 72 73 74 2c 20 31 2c 20 30 29 3b 0a  )&first, 1, 0);.
129b8 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
129b9 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
129ba 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20  _SHORT_READ ){. 
129bb 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20               rc 
129bc 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
129bd 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
129be 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73         sqlite3Os
129bf 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66  Close(pPager->jf
129c0 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  d);.            
129c1 2a 70 45 78 69 73 74 73 20 3d 20 28 66 69 72 73  *pExists = (firs
129c2 74 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 20  t!=0);.         
129c3 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
129c4 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
129c5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
129c6 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63  /*.** Read the c
129c7 6f 6e 74 65 6e 74 20 66 6f 72 20 70 61 67 65 20  ontent for page 
129c8 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64  pPg out of the d
129c9 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
129ca 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e 70   into .** pPg->p
129cb 44 61 74 61 2e 20 41 20 73 68 61 72 65 64 20 6c  Data. A shared l
129cc 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20 6d  ock or greater m
129cd 75 73 74 20 62 65 20 68 65 6c 64 20 6f 6e 20 74  ust be held on t
129ce 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66  he database.** f
129cf 69 6c 65 20 62 65 66 6f 72 65 20 74 68 69 73 20  ile before this 
129d0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
129d1 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 67  ed..**.** If pag
129d2 65 20 31 20 69 73 20 72 65 61 64 2c 20 74 68 65  e 1 is read, the
129d3 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50  n the value of P
129d4 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 5b  ager.dbFileVers[
129d5 5d 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 74  ] is set to.** t
129d6 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  he value read fr
129d7 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
129d8 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  file..**.** If a
129d9 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
129da 73 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65  s, then the IO e
129db 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
129dc 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   to the caller..
129dd 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51  ** Otherwise, SQ
129de 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
129df 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
129e0 6e 74 20 72 65 61 64 44 62 50 61 67 65 28 50 67  nt readDbPage(Pg
129e1 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67  Hdr *pPg){.  Pag
129e2 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
129e3 2d 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50 61 67  ->pPager; /* Pag
129e4 65 72 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69  er object associ
129e5 61 74 65 64 20 77 69 74 68 20 70 61 67 65 20 70  ated with page p
129e6 50 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  Pg */.  Pgno pgn
129e7 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 20 20  o = pPg->pgno;  
129e8 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
129e9 62 65 72 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  ber to read */. 
129ea 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
129eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
129ec 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
129ed 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20    i64 iOffset;  
129ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
129ef 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66  * Byte offset of
129f0 20 66 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72   file to read fr
129f1 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  om */..  assert(
129f2 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
129f3 50 41 47 45 52 5f 53 48 41 52 45 44 20 26 26 20  PAGER_SHARED && 
129f4 21 4d 45 4d 44 42 20 29 3b 0a 0a 20 20 69 66 28  !MEMDB );..  if(
129f5 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d   !isOpen(pPager-
129f6 3e 66 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65  >fd) ){.    asse
129f7 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  rt( pPager->temp
129f8 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73  File );.    mems
129f9 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30  et(pPg->pData, 0
129fa 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
129fb 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ze);.    return 
129fc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
129fd 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f   iOffset = (pgno
129fe 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d  -1)*(i64)pPager-
129ff 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63 20  >pageSize;.  rc 
12a00 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
12a01 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d  pPager->fd, pPg-
12a02 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e  >pData, pPager->
12a03 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66 73 65  pageSize, iOffse
12a04 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
12a05 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
12a06 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72 63 20  _READ ){.    rc 
12a07 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
12a08 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29  .  if( pgno==1 )
12a09 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69 6c 65  {.    u8 *dbFile
12a0a 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29 70 50  Vers = &((u8*)pP
12a0b 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b 0a 20  g->pData)[24];. 
12a0c 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65     memcpy(&pPage
12a0d 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64  r->dbFileVers, d
12a0e 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f  bFileVers, sizeo
12a0f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  f(pPager->dbFile
12a10 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f  Vers));.  }.  CO
12a11 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 67  DEC1(pPager, pPg
12a12 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33  ->pData, pgno, 3
12a13 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49 4e 43 52  );..  PAGER_INCR
12a14 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72  (sqlite3_pager_r
12a15 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20  eaddb_count);.  
12a16 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65  PAGER_INCR(pPage
12a17 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f 54  r->nRead);.  IOT
12a18 52 41 43 45 28 28 22 50 47 49 4e 20 25 70 20 25  RACE(("PGIN %p %
12a19 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67  d\n", pPager, pg
12a1a 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52 54 52 41  no));.  PAGERTRA
12a1b 43 45 28 28 22 46 45 54 43 48 20 25 64 20 70 61  CE(("FETCH %d pa
12a1c 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29  ge %d hash(%08x)
12a1d 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
12a1e 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67      PAGERID(pPag
12a1f 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72  er), pgno, pager
12a20 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 29 29  _pagehash(pPg)))
12a21 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  ;..  return rc;.
12a22 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
12a23 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
12a24 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 75 70   whenever the up
12a25 70 65 72 20 6c 61 79 65 72 20 72 65 71 75 65 73  per layer reques
12a26 74 73 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a  ts a database.**
12a27 20 70 61 67 65 20 69 73 20 72 65 71 75 65 73 74   page is request
12a28 65 64 2c 20 62 65 66 6f 72 65 20 74 68 65 20 63  ed, before the c
12a29 61 63 68 65 20 69 73 20 63 68 65 63 6b 65 64 20  ache is checked 
12a2a 66 6f 72 20 61 20 73 75 69 74 61 62 6c 65 20 70  for a suitable p
12a2b 61 67 65 0a 2a 2a 20 6f 72 20 61 6e 79 20 64 61  age.** or any da
12a2c 74 61 20 69 73 20 72 65 61 64 20 66 72 6f 6d 20  ta is read from 
12a2d 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 74  the database. It
12a2e 20 70 65 72 66 6f 72 6d 73 20 74 68 65 20 66 6f   performs the fo
12a2f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 77 6f 20 66  llowing.** two f
12a30 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20  unctions:.**.** 
12a31 20 20 31 29 20 49 66 20 74 68 65 20 70 61 67 65    1) If the page
12a32 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69  r is currently i
12a33 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73  n PAGER_UNLOCK s
12a34 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65  tate (no lock he
12a35 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68  ld.**      on th
12a36 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29  e database file)
12a37 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70  , then an attemp
12a38 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62 74  t is made to obt
12a39 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 53 48  ain a.**      SH
12a3a 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ARED lock on the
12a3b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
12a3c 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  Immediately afte
12a3d 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20  r obtaining.**  
12a3e 20 20 20 20 74 68 65 20 53 48 41 52 45 44 20 6c      the SHARED l
12a3f 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d 73 79  ock, the file-sy
12a40 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 20  stem is checked 
12a41 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  for a hot-journa
12a42 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 63 68  l,.**      which
12a43 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20   is played back 
12a44 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c  if present. Foll
12a45 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f  owing any hot-jo
12a46 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72  urnal .**      r
12a47 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e  ollback, the con
12a48 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63  tents of the cac
12a49 68 65 20 61 72 65 20 76 61 6c 69 64 61 74 65 64  he are validated
12a4a 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20   by checking.** 
12a4b 20 20 20 20 20 74 68 65 20 27 63 68 61 6e 67 65       the 'change
12a4c 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c 64 20  -counter' field 
12a4d 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
12a4e 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e 64 0a  file header and.
12a4f 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72 64 65  **      discarde
12a50 64 20 69 66 20 74 68 65 79 20 61 72 65 20 66 6f  d if they are fo
12a51 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69  und to be invali
12a52 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66  d..**.**   2) If
12a53 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75   the pager is ru
12a54 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69  nning in exclusi
12a55 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65  ve-mode, and the
12a56 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  re are currently
12a57 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73  .**      no outs
12a58 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63  tanding referenc
12a59 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 2c  es to any pages,
12a5a 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20 65   and is in the e
12a5b 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20  rror state,.**  
12a5c 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74 65      then an atte
12a5d 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63  mpt is made to c
12a5e 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73  lear the error s
12a5f 74 61 74 65 20 62 79 20 64 69 73 63 61 72 64 69  tate by discardi
12a60 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 63  ng.**      the c
12a61 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70  ontents of the p
12a62 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 72 6f  age cache and ro
12a63 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f  lling back any o
12a64 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20  pen journal.**  
12a65 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20      file..**.** 
12a66 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  If the operation
12a67 20 64 65 73 63 72 69 62 65 64 20 62 79 20 28 32   described by (2
12a68 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 61  ) above is not a
12a69 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20 69 66  ttempted, and if
12a6a 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 69 73   the.** pager is
12a6b 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61   in an error sta
12a6c 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51  te other than SQ
12a6d 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e 20 74  LITE_FULL when t
12a6e 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a  his is called,.*
12a6f 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  * the error stat
12a70 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
12a71 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69 73 20  returned. It is 
12a72 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 61  permitted to rea
12a73 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  d the.** databas
12a74 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49 54 45  e when in SQLITE
12a75 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74 61 74  _FULL error stat
12a76 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  e..**.** Otherwi
12a77 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e  se, if everythin
12a78 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  g is successful,
12a79 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
12a7a 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a  turned. If an.**
12a7b 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
12a7c 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 74   while locking t
12a7d 68 65 20 64 61 74 61 62 61 73 65 2c 20 63 68 65  he database, che
12a7e 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f 74 2d  cking for a hot-
12a7f 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20  journal.** file 
12a80 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  or rolling back 
12a81 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  a journal file, 
12a82 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64  the IO error cod
12a83 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
12a84 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
12a85 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 67  erSharedLock(Pag
12a86 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
12a87 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
12a88 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K;              
12a89 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
12a8a 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 6f   */.  int isErro
12a8b 72 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20 20  rReset = 0;     
12a8c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
12a8d 20 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20 66   if recovering f
12a8e 72 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65 20  rom error state 
12a8f 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  */..  /* If this
12a90 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
12a91 6e 65 64 20 66 6f 72 20 65 78 63 6c 75 73 69 76  ned for exclusiv
12a92 65 20 61 63 63 65 73 73 2c 20 68 61 73 20 6e 6f  e access, has no
12a93 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 0a 20 20   outstanding .  
12a94 2a 2a 20 70 61 67 65 20 72 65 66 65 72 65 6e 63  ** page referenc
12a95 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 6e 20  es and is in an 
12a96 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 68 69  error-state, thi
12a97 73 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 6f  s is a chance to
12a98 20 63 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65 20   clear.  ** the 
12a99 65 72 72 6f 72 2e 20 44 69 73 63 61 72 64 20 74  error. Discard t
12a9a 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
12a9b 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61  he pager-cache a
12a9c 6e 64 20 74 72 65 61 74 20 61 6e 79 0a 20 20 2a  nd treat any.  *
12a9d 2a 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66  * open journal f
12a9e 69 6c 65 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75  ile as a hot-jou
12a9f 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rnal..  */.  if(
12aa0 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65   !MEMDB && pPage
12aa1 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
12aa2 20 0a 20 20 20 26 26 20 73 71 6c 69 74 65 33 50   .   && sqlite3P
12aa3 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50  cacheRefCount(pP
12aa4 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d  ager->pPCache)==
12aa5 30 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72  0 && pPager->err
12aa6 43 6f 64 65 20 0a 20 20 29 7b 0a 20 20 20 20 69  Code .  ){.    i
12aa7 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  f( isOpen(pPager
12aa8 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20  ->jfd) ){.      
12aa9 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 31  isErrorReset = 1
12aaa 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67  ;.    }.    pPag
12aab 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51  er->errCode = SQ
12aac 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 67  LITE_OK;.    pag
12aad 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29  er_reset(pPager)
12aae 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
12aaf 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69 6c  he pager is stil
12ab0 6c 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74  l in an error st
12ab1 61 74 65 2c 20 64 6f 20 6e 6f 74 20 70 72 6f 63  ate, do not proc
12ab2 65 65 64 2e 20 54 68 65 20 65 72 72 6f 72 20 0a  eed. The error .
12ab3 20 20 2a 2a 20 73 74 61 74 65 20 77 69 6c 6c 20    ** state will 
12ab4 62 65 20 63 6c 65 61 72 65 64 20 61 74 20 73 6f  be cleared at so
12ab5 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20  me point in the 
12ab6 66 75 74 75 72 65 20 77 68 65 6e 20 61 6c 6c 20  future when all 
12ab7 70 61 67 65 20 0a 20 20 2a 2a 20 72 65 66 65 72  page .  ** refer
12ab8 65 6e 63 65 73 20 61 72 65 20 64 72 6f 70 70 65  ences are droppe
12ab9 64 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 20  d and the cache 
12aba 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65 64  can be discarded
12abb 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
12abc 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26 20  ger->errCode && 
12abd 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21  pPager->errCode!
12abe 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a  =SQLITE_FULL ){.
12abf 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65      return pPage
12ac0 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a  r->errCode;.  }.
12ac1 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
12ac2 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
12ac3 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52 65 73  CK || isErrorRes
12ac4 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  et ){.    sqlite
12ac5 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56  3_vfs * const pV
12ac6 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66  fs = pPager->pVf
12ac7 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48 6f 74  s;.    int isHot
12ac8 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20  Journal = 0;.   
12ac9 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
12aca 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
12acb 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
12acc 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
12acd 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20  ache)==0 );.    
12ace 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 52  if( !pPager->noR
12acf 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  eadlock ){.     
12ad0 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74   rc = pager_wait
12ad1 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c  _on_lock(pPager,
12ad2 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20   SHARED_LOCK);. 
12ad3 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
12ad4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12ad5 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12ad6 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55  ->state==PAGER_U
12ad7 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20  NLOCK );.       
12ad8 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72   return pager_er
12ad9 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b  ror(pPager, rc);
12ada 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
12adb 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  se if( pPager->s
12adc 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
12add 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67  CK ){.      pPag
12ade 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
12adf 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 7d 0a  R_SHARED;.    }.
12ae0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
12ae1 65 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 45  er->state>=SHARE
12ae2 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20 2f  D_LOCK );..    /
12ae3 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66  * If a journal f
12ae4 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64 20  ile exists, and 
12ae5 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53 45  there is no RESE
12ae6 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  RVED lock on the
12ae7 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
12ae8 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 65   file, then it e
12ae9 69 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20 62  ither needs to b
12aea 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 72  e played back or
12aeb 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f   deleted..    */
12aec 0a 20 20 20 20 69 66 28 20 21 69 73 45 72 72 6f  .    if( !isErro
12aed 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20  rReset ){.      
12aee 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e  rc = hasHotJourn
12aef 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 48 6f  al(pPager, &isHo
12af0 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20  tJournal);.     
12af1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12af2 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
12af3 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  to failed;.     
12af4 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
12af5 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 7c 7c   isErrorReset ||
12af6 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b   isHotJournal ){
12af7 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e  .      /* Get an
12af8 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
12af9 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
12afa 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f  file. At this po
12afb 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 20 20  int it is.      
12afc 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61  ** important tha
12afd 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  t a RESERVED loc
12afe 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65  k is not obtaine
12aff 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20  d on the way to 
12b00 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43  the.      ** EXC
12b01 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20  LUSIVE lock. If 
12b02 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72  it were, another
12b03 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f   process might o
12b04 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  pen the.      **
12b05 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
12b06 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45 52  detect the RESER
12b07 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f  VED lock, and co
12b08 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a  nclude that the.
12b09 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73        ** databas
12b0a 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 61  e is safe to rea
12b0b 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f  d while this pro
12b0c 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f  cess is still ro
12b0d 6c 6c 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20  lling the .     
12b0e 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20   ** hot-journal 
12b0f 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a  back..      ** .
12b10 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65        ** Because
12b11 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74   the intermediat
12b12 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  e RESERVED lock 
12b13 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64  is not requested
12b14 2c 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f  , any.      ** o
12b15 74 68 65 72 20 70 72 6f 63 65 73 73 20 61 74 74  ther process att
12b16 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73  empting to acces
12b17 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
12b18 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20  ile will get to 
12b19 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70  .      ** this p
12b1a 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65  oint in the code
12b1b 20 61 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74   and fail to obt
12b1c 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c  ain its own EXCL
12b1d 55 53 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20  USIVE lock .    
12b1e 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61    ** on the data
12b1f 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20  base file..     
12b20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50   */.      if( pP
12b21 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c  ager->state<EXCL
12b22 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20  USIVE_LOCK ){.  
12b23 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12b24 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d  e3OsLock(pPager-
12b25 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c  >fd, EXCLUSIVE_L
12b26 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66  OCK);.        if
12b27 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
12b28 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
12b29 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50  = pager_error(pP
12b2a 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20  ager, rc);.     
12b2b 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
12b2c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
12b2d 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
12b2e 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53  e = PAGER_EXCLUS
12b2f 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20  IVE;.      }. . 
12b30 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65       /* Open the
12b31 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 61   journal for rea
12b32 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e 20  d/write access. 
12b33 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20  This is because 
12b34 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 65 78 63  in .      ** exc
12b35 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f  lusive-access mo
12b36 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  de the file desc
12b37 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b  riptor will be k
12b38 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20  ept open and.   
12b39 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75     ** possibly u
12b3a 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61  sed for a transa
12b3b 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20  ction later on. 
12b3c 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c  On some systems,
12b3d 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73   the.      ** Os
12b3e 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c 6c 20  Truncate() call 
12b3f 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 69 76  used in exclusiv
12b40 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 61 6c  e-access mode al
12b41 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 20 20  so requires.    
12b42 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72 69 74    ** a read/writ
12b43 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20  e file handle.. 
12b44 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
12b45 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  ( !isOpen(pPager
12b46 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20  ->jfd) ){.      
12b47 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
12b48 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
12b49 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 50 61  sAccess(pVfs,pPa
12b4a 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51  ger->zJournal,SQ
12b4b 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
12b4c 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20  TS,&res);.      
12b4d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
12b4e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
12b4f 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20   if( res ){.    
12b50 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74          int fout
12b51 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
12b52 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45    int f = SQLITE
12b53 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c  _OPEN_READWRITE|
12b54 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
12b55 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20  _JOURNAL;.      
12b56 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
12b57 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
12b58 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  );.            r
12b59 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
12b5a 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  n(pVfs, pPager->
12b5b 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72  zJournal, pPager
12b5c 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29  ->jfd, f, &fout)
12b5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  ;.            as
12b5e 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
12b5f 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50  _OK || isOpen(pP
12b60 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20  ager->jfd) );.  
12b61 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
12b62 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66  ==SQLITE_OK && f
12b63 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  out&SQLITE_OPEN_
12b64 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20  READONLY ){.    
12b65 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
12b66 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
12b67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
12b68 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
12b69 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20  ger->jfd);.     
12b6a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12b6b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
12b6c 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
12b6d 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74  journal does not
12b6e 20 65 78 69 73 74 2c 20 74 68 61 74 20 6d 65 61   exist, that mea
12b6f 6e 73 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72  ns some other pr
12b70 6f 63 65 73 73 0a 20 20 20 20 20 20 20 20 20 20  ocess.          
12b71 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79    ** has already
12b72 20 72 6f 6c 6c 65 64 20 69 74 20 62 61 63 6b 20   rolled it back 
12b73 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  */.            r
12b74 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
12b75 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
12b76 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
12b77 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
12b78 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12b79 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20    goto failed;. 
12b7a 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
12b7b 20 54 4f 44 4f 3a 20 57 68 79 20 61 72 65 20 74   TODO: Why are t
12b7c 68 65 73 65 20 63 6c 65 61 72 65 64 20 68 65 72  hese cleared her
12b7d 65 3f 20 49 73 20 69 74 20 6e 65 63 65 73 73 61  e? Is it necessa
12b7e 72 79 3f 20 2a 2f 0a 20 20 20 20 20 20 70 50 61  ry? */.      pPa
12b7f 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72  ger->journalStar
12b80 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ted = 0;.      p
12b81 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
12b82 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61  f = 0;.      pPa
12b83 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d  ger->setMaster =
12b84 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   0;.      pPager
12b85 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30  ->journalHdr = 0
12b86 3b 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61  ;. .      /* Pla
12b87 79 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 65  yback and delete
12b88 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44   the journal.  D
12b89 72 6f 70 20 74 68 65 20 64 61 74 61 62 61 73 65  rop the database
12b8a 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20   write.      ** 
12b8b 6c 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75 69  lock and reacqui
12b8c 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b  re the read lock
12b8d 2e 20 50 75 72 67 65 20 74 68 65 20 63 61 63 68  . Purge the cach
12b8e 65 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 2a  e before.      *
12b8f 2a 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 74  * playing back t
12b90 68 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73  he hot-journal s
12b91 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20  o that we don't 
12b92 65 6e 64 20 75 70 20 77 69 74 68 0a 20 20 20 20  end up with.    
12b93 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73    ** an inconsis
12b94 74 65 6e 74 20 63 61 63 68 65 2e 0a 20 20 20 20  tent cache..    
12b95 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20    */.      rc = 
12b96 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70  pager_playback(p
12b97 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 20  Pager, 1);.     
12b98 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12b99 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
12b9a 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70   = pager_error(p
12b9b 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20  Pager, rc);.    
12b9c 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b      goto failed;
12b9d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
12b9e 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e  ssert( (pPager->
12b9f 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41  state==PAGER_SHA
12ba0 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 20 20  RED).           
12ba1 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c  || (pPager->excl
12ba2 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61  usiveMode && pPa
12ba3 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52  ger->state>PAGER
12ba4 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 29  _SHARED).      )
12ba5 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
12ba6 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70   pPager->pBackup
12ba7 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68   || sqlite3Pcach
12ba8 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  ePagecount(pPage
12ba9 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b  r->pPCache)>0 ){
12baa 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68  .      /* The sh
12bab 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75  ared-lock has ju
12bac 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 64  st been acquired
12bad 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
12bae 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61   file.      ** a
12baf 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c 72  nd there are alr
12bb0 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 68  eady pages in th
12bb1 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20  e cache (from a 
12bb2 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a  previous.      *
12bb3 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20  * read or write 
12bb4 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43  transaction).  C
12bb5 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
12bb6 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
12bb7 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f    ** has been mo
12bb8 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 20  dified.  If the 
12bb9 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68 61  database has cha
12bba 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a  nged, flush the.
12bbb 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a        ** cache..
12bbc 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
12bbd 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67  * Database chang
12bbe 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62  es is detected b
12bbf 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20  y looking at 15 
12bc0 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a  bytes beginning.
12bc1 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73        ** at offs
12bc2 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66  et 24 into the f
12bc3 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 20  ile.  The first 
12bc4 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 79  4 of these 16 by
12bc5 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a  tes are.      **
12bc6 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65   a 32-bit counte
12bc7 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d  r that is increm
12bc8 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20  ented with each 
12bc9 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20  change.  The.   
12bca 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65     ** other byte
12bcb 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c  s change randoml
12bcc 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c 65  y with each file
12bcd 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20   change when.   
12bce 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73     ** a codec is
12bcf 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a   in use..      *
12bd0 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72  * .      ** Ther
12bd1 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67  e is a vanishing
12bd2 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20  ly small chance 
12bd3 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 69  that a change wi
12bd4 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20  ll not be .     
12bd5 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54   ** detected.  T
12bd6 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20  he chance of an 
12bd7 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e 67  undetected chang
12bd8 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68  e is so small th
12bd9 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63  at.      ** it c
12bda 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e  an be neglected.
12bdb 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
12bdc 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b  char dbFileVers[
12bdd 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64  sizeof(pPager->d
12bde 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20  bFileVers)];.   
12bdf 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50     sqlite3PagerP
12be0 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
12be1 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20   0);..      if( 
12be2 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
12be3 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
12be4 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
12be5 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61  .        goto fa
12be6 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  iled;.      }.. 
12be7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
12be8 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
12be9 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50   );.      if( pP
12bea 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29  ager->dbSize>0 )
12beb 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43  {.        IOTRAC
12bec 45 28 28 22 43 4b 56 45 52 53 20 25 70 20 25 64  E(("CKVERS %p %d
12bed 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 69 7a  \n", pPager, siz
12bee 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29  eof(dbFileVers))
12bef 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
12bf0 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50  sqlite3OsRead(pP
12bf1 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c  ager->fd, &dbFil
12bf2 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62  eVers, sizeof(db
12bf3 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29 3b 0a  FileVers), 24);.
12bf4 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
12bf5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
12bf6 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c         goto fail
12bf7 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ed;.        }.  
12bf8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12bf9 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65     memset(dbFile
12bfa 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Vers, 0, sizeof(
12bfb 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20  dbFileVers));.  
12bfc 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
12bfd 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e   memcmp(pPager->
12bfe 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69  dbFileVers, dbFi
12bff 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64  leVers, sizeof(d
12c00 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20 29  bFileVers))!=0 )
12c01 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f  {.        pager_
12c02 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  reset(pPager);. 
12c03 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
12c04 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
12c05 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c  >exclusiveMode |
12c06 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  | pPager->state=
12c07 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b  =PAGER_SHARED );
12c08 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20  .  }.. failed:. 
12c09 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12c0a 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67  OK ){.    /* pag
12c0b 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61  er_unlock() is a
12c0c 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75   no-op for exclu
12c0d 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e  sive mode and in
12c0e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
12c0f 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f  s. */.    pager_
12c10 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a  unlock(pPager);.
12c11 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
12c12 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
12c13 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
12c14 20 68 61 73 20 72 65 61 63 68 65 64 20 7a 65 72   has reached zer
12c15 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20  o, rollback any 
12c16 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61  active.** transa
12c17 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b  ction and unlock
12c18 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a   the pager..**.*
12c19 2a 20 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63  * Except, in loc
12c1a 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53  king_mode=EXCLUS
12c1b 49 56 45 20 77 68 65 6e 20 74 68 65 72 65 20 69  IVE when there i
12c1c 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a  s nothing to in.
12c1d 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ** the rollback 
12c1e 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c  journal, the unl
12c1f 6f 63 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f  ock is not perfo
12c20 72 6d 65 64 20 61 6e 64 20 74 68 65 72 65 20 69  rmed and there i
12c21 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20  s.** nothing to 
12c22 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69  rollback, so thi
12c23 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
12c24 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  o-op..*/ .static
12c25 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63   void pagerUnloc
12c26 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 72 20  kIfUnused(Pager 
12c27 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20  *pPager){.  if( 
12c28 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65  (sqlite3PcacheRe
12c29 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
12c2a 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 26  PCache)==0).   &
12c2b 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  & (!pPager->excl
12c2c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61  usiveMode || pPa
12c2d 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e  ger->journalOff>
12c2e 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 67  0) .  ){.    pag
12c2f 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62  erUnlockAndRollb
12c30 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  ack(pPager);.  }
12c31 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61  .}../*.** Drop a
12c32 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 63   page from the c
12c33 61 63 68 65 20 75 73 69 6e 67 20 73 71 6c 69 74  ache using sqlit
12c34 65 33 50 63 61 63 68 65 44 72 6f 70 28 29 2e 0a  e3PcacheDrop()..
12c35 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d 65  **.** If this me
12c36 61 6e 73 20 74 68 65 72 65 20 61 72 65 20 6e 6f  ans there are no
12c37 77 20 6e 6f 20 70 61 67 65 73 20 77 69 74 68 20  w no pages with 
12c38 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
12c39 65 6d 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a  em, a rollback.*
12c3a 2a 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65  * occurs and the
12c3b 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
12c3c 61 62 61 73 65 20 69 73 20 72 65 6d 6f 76 65 64  abase is removed
12c3d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
12c3e 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 28 44   pagerDropPage(D
12c3f 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 50  bPage *pPg){.  P
12c40 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
12c41 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 71  Pg->pPager;.  sq
12c42 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28  lite3PcacheDrop(
12c43 70 50 67 29 3b 0a 20 20 70 61 67 65 72 55 6e 6c  pPg);.  pagerUnl
12c44 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67  ockIfUnused(pPag
12c45 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63  er);.}../*.** Ac
12c46 71 75 69 72 65 20 61 20 72 65 66 65 72 65 6e 63  quire a referenc
12c47 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72  e to page number
12c48 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70   pgno in pager p
12c49 50 61 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a  Pager (a page.**
12c4a 20 72 65 66 65 72 65 6e 63 65 20 68 61 73 20 74   reference has t
12c4b 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66  ype DbPage*). If
12c4c 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72   the requested r
12c4d 65 66 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20  eference is .** 
12c4e 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74  successfully obt
12c4f 61 69 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70  ained, it is cop
12c50 69 65 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61  ied to *ppPage a
12c51 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
12c52 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  urned..**.** Thi
12c53 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  s function calls
12c54 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b   pagerSharedLock
12c55 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 53  () to obtain a S
12c56 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a  HARED lock on.**
12c57 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
12c58 6c 65 20 69 66 20 73 75 63 68 20 61 20 6c 6f 63  le if such a loc
12c59 6b 20 6f 72 20 67 72 65 61 74 65 72 20 69 73 20  k or greater is 
12c5a 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64  not already held
12c5b 2e 0a 2a 2a 20 54 68 69 73 20 6d 61 79 20 63 61  ..** This may ca
12c5c 75 73 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  use hot-journal 
12c5d 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 63 61  rollback or a ca
12c5e 63 68 65 20 70 75 72 67 65 2e 20 53 65 65 20 63  che purge. See c
12c5f 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 61 62 6f 76 65  omments.** above
12c60 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 72 53   function pagerS
12c61 68 61 72 65 64 4c 6f 63 6b 28 29 20 66 6f 72 20  haredLock() for 
12c62 64 65 74 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49  details..**.** I
12c63 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
12c64 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20  page is already 
12c65 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 69 74  in the cache, it
12c66 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a   is returned. .*
12c67 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 6e  * Otherwise, a n
12c68 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69  ew page object i
12c69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  s allocated and 
12c6a 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 64  populated with d
12c6b 61 74 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d  ata.** read from
12c6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
12c6d 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65  le. In some case
12c6e 73 2c 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f  s, the pcache mo
12c6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f  dule may.** choo
12c70 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 61  se not to alloca
12c71 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62  te a new page ob
12c72 6a 65 63 74 20 61 6e 64 20 6d 61 79 20 72 65 75  ject and may reu
12c73 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a  se an existing.*
12c74 2a 20 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 6f  * object with no
12c75 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66   outstanding ref
12c76 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  erences..**.** T
12c77 68 65 20 65 78 74 72 61 20 64 61 74 61 20 61 70  he extra data ap
12c78 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 67 65  pended to a page
12c79 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 74 69   is always initi
12c7a 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20  alized to zeros 
12c7b 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 69  the .** first ti
12c7c 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61  me a page is loa
12c7d 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e  ded into memory.
12c7e 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65 71   If the page req
12c7f 75 65 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 6c  uested is .** al
12c80 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63  ready in the cac
12c81 68 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  he when this fun
12c82 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
12c83 20 74 68 65 6e 20 74 68 65 20 65 78 74 72 61 0a   then the extra.
12c84 2a 2a 20 64 61 74 61 20 69 73 20 6c 65 66 74 20  ** data is left 
12c85 61 73 20 69 74 20 77 61 73 20 77 68 65 6e 20 74  as it was when t
12c86 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 77  he page object w
12c87 61 73 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a 2a  as last used..**
12c88 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62  .** If the datab
12c89 61 73 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61  ase image is sma
12c8a 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 72 65  ller than the re
12c8b 71 75 65 73 74 65 64 20 70 61 67 65 20 6f 72 20  quested page or 
12c8c 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72  if a .** non-zer
12c8d 6f 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65  o value is passe
12c8e 64 20 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65  d as the noConte
12c8f 6e 74 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64  nt parameter and
12c90 20 74 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 74   the .** request
12c91 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  ed page is not a
12c92 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e  lready stored in
12c93 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e   the cache, then
12c94 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 64   no .** actual d
12c95 69 73 6b 20 72 65 61 64 20 6f 63 63 75 72 73 2e  isk read occurs.
12c96 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
12c97 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f  e memory image o
12c98 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 69  f the .** page i
12c99 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  s initialized to
12c9a 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a   all zeros. .**.
12c9b 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20  ** If noContent 
12c9c 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e  is true, it mean
12c9d 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74  s that we do not
12c9e 20 63 61 72 65 20 61 62 6f 75 74 20 74 68 65 20   care about the 
12c9f 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74  contents.** of t
12ca0 68 65 20 70 61 67 65 2e 20 54 68 69 73 20 6f 63  he page. This oc
12ca1 63 75 72 73 20 69 6e 20 74 77 6f 20 73 65 70 65  curs in two sepe
12ca2 72 61 74 65 20 73 63 65 6e 61 72 69 6f 73 3a 0a  rate scenarios:.
12ca3 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e 20  **.**   a) When 
12ca4 72 65 61 64 69 6e 67 20 61 20 66 72 65 65 2d 6c  reading a free-l
12ca5 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 66 72  ist leaf page fr
12ca6 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  om the database,
12ca7 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20   and.**.**   b) 
12ca8 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74  When a savepoint
12ca9 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64   is being rolled
12caa 20 62 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 65   back and we nee
12cab 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 20  d to load.**    
12cac 20 20 61 20 6e 65 77 20 70 61 67 65 20 69 6e 74    a new page int
12cad 6f 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 70  o the cache to p
12cae 6f 70 75 6c 61 74 65 20 77 69 74 68 20 74 68 65  opulate with the
12caf 20 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 20   data read.**   
12cb0 20 20 20 66 72 6f 6d 20 74 68 65 20 73 61 76 65     from the save
12cb1 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a  point journal..*
12cb2 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e  *.** If noConten
12cb3 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  t is true, then 
12cb4 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65  the data returne
12cb5 64 20 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74  d is zeroed inst
12cb6 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 20  ead of.** being 
12cb7 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61  read from the da
12cb8 74 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e  tabase. Addition
12cb9 61 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63  ally, the bits c
12cba 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20  orresponding.** 
12cbb 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72  to pgno in Pager
12cbc 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74  .pInJournal (bit
12cbd 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72  vec of pages alr
12cbe 65 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20  eady written to 
12cbf 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  the.** journal f
12cc0 69 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61 67  ile) and the Pag
12cc1 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53  erSavepoint.pInS
12cc2 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73  avepoint bitvecs
12cc3 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20   of any open.** 
12cc4 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73  savepoints are s
12cc5 65 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69  et. This means i
12cc6 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61  f the page is ma
12cc7 64 65 20 77 72 69 74 61 62 6c 65 20 61 74 20 61  de writable at a
12cc8 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 74  ny.** point in t
12cc9 68 65 20 66 75 74 75 72 65 2c 20 75 73 69 6e 67  he future, using
12cca 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
12ccb 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2c 20  e3PagerWrite(), 
12ccc 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20  its contents.** 
12ccd 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 72  will not be jour
12cce 6e 61 6c 65 64 2e 20 54 68 69 73 20 73 61 76 65  naled. This save
12ccf 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  s IO..**.** The 
12cd0 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68  acquisition migh
12cd1 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72  t fail for sever
12cd2 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20  al reasons.  In 
12cd3 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e  all cases,.** an
12cd4 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72   appropriate err
12cd5 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
12cd6 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20  ned and *ppPage 
12cd7 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a  is set to NULL..
12cd8 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73  **.** See also s
12cd9 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
12cda 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20  p().  Both this 
12cdb 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b  routine and Look
12cdc 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20  up() attempt.** 
12cdd 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20 69  to find a page i
12cde 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  n the in-memory 
12cdf 63 61 63 68 65 20 66 69 72 73 74 2e 20 20 49 66  cache first.  If
12ce0 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
12ce1 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d   already.** in m
12ce2 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74  emory, this rout
12ce3 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b  ine goes to disk
12ce4 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77   to read it in w
12ce5 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a  hereas Lookup().
12ce6 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20  ** just returns 
12ce7 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  0.  This routine
12ce8 20 61 63 71 75 69 72 65 73 20 61 20 72 65 61 64   acquires a read
12ce9 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20  -lock the first 
12cea 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74  time it.** has t
12ceb 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e  o go to disk, an
12cec 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61  d could also pla
12ced 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75  yback an old jou
12cee 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72  rnal if necessar
12cef 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b  y..** Since Look
12cf0 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20  up() never goes 
12cf1 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65  to disk, it neve
12cf2 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69  r has to deal wi
12cf3 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a  th locks.** or j
12cf4 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f  ournal files..*/
12cf5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12cf6 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
12cf7 41 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 72  Acquire(.  Pager
12cf8 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f   *pPager,      /
12cf9 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e  * The pager open
12cfa 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
12cfb 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20   file */.  Pgno 
12cfc 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f  pgno,          /
12cfd 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f  * Page number to
12cfe 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61   fetch */.  DbPa
12cff 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20  ge **ppPage,    
12d00 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74  /* Write a point
12d01 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 68  er to the page h
12d02 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43  ere */.  int noC
12d03 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20  ontent       /* 
12d04 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65  Do not bother re
12d05 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72  ading content fr
12d06 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20  om disk if true 
12d07 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70  */.){.  PgHdr *p
12d08 50 67 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  Pg = 0;.  int rc
12d09 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 73 73  ;..  assert( ass
12d0a 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28  ert_pager_state(
12d0b 70 50 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73  pPager) );.  ass
12d0c 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
12d0d 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  te==PAGER_UNLOCK
12d0e 20 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69   .       || sqli
12d0f 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e  te3PcacheRefCoun
12d10 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
12d11 65 29 3e 30 20 0a 20 20 20 20 20 20 20 7c 7c 20  e)>0 .       || 
12d12 70 67 6e 6f 3d 3d 31 0a 20 20 29 3b 0a 0a 20 20  pgno==1.  );..  
12d13 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 70  /* The maximum p
12d14 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 32 5e  age number is 2^
12d15 33 31 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54  31. Return SQLIT
12d16 45 5f 43 4f 52 52 55 50 54 20 69 66 20 61 20 70  E_CORRUPT if a p
12d17 61 67 65 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20  age.  ** number 
12d18 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 69  greater than thi
12d19 73 2c 20 6f 72 20 7a 65 72 6f 2c 20 69 73 20 72  s, or zero, is r
12d1a 65 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20  equested..  */. 
12d1b 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f   if( pgno>PAGER_
12d1c 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f  MAX_PGNO || pgno
12d1d 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47  ==0 || pgno==PAG
12d1e 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
12d1f 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  r) ){.    return
12d20 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
12d21 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  BKPT;.  }..  /* 
12d22 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76  Make sure we hav
12d23 65 20 6e 6f 74 20 68 69 74 20 61 6e 79 20 63 72  e not hit any cr
12d24 69 74 69 63 61 6c 20 65 72 72 6f 72 73 2e 0a 20  itical errors.. 
12d25 20 2a 2f 20 0a 20 20 61 73 73 65 72 74 28 20 70   */ .  assert( p
12d26 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 2a 70  Pager!=0 );.  *p
12d27 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 2f 2a  pPage = 0;..  /*
12d28 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
12d29 66 69 72 73 74 20 70 61 67 65 20 61 63 63 65 73  first page acces
12d2a 73 65 64 2c 20 74 68 65 6e 20 67 65 74 20 61 20  sed, then get a 
12d2b 53 48 41 52 45 44 20 6c 6f 63 6b 0a 20 20 2a 2a  SHARED lock.  **
12d2c 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
12d2d 20 66 69 6c 65 2e 20 70 61 67 65 72 53 68 61 72   file. pagerShar
12d2e 65 64 4c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f  edLock() is a no
12d2f 2d 6f 70 20 69 66 20 0a 20 20 2a 2a 20 61 20 64  -op if .  ** a d
12d30 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 69 73 20  atabase lock is 
12d31 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a 20 20  already held..  
12d32 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 53  */.  rc = pagerS
12d33 68 61 72 65 64 4c 6f 63 6b 28 70 50 61 67 65 72  haredLock(pPager
12d34 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
12d35 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
12d36 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61  turn rc;.  }.  a
12d37 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
12d38 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate!=PAGER_UNLO
12d39 43 4b 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71  CK );..  rc = sq
12d3a 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
12d3b 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
12d3c 2c 20 70 67 6e 6f 2c 20 31 2c 20 26 70 50 67 29  , pgno, 1, &pPg)
12d3d 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
12d3e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
12d3f 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73  urn rc;.  }.  as
12d40 73 65 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 3d  sert( pPg->pgno=
12d41 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72  =pgno );.  asser
12d42 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d  t( pPg->pPager==
12d43 70 50 61 67 65 72 20 7c 7c 20 70 50 67 2d 3e 70  pPager || pPg->p
12d44 50 61 67 65 72 3d 3d 30 20 29 3b 0a 20 20 69 66  Pager==0 );.  if
12d45 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 30  ( pPg->pPager==0
12d46 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70   ){.    /* The p
12d47 61 67 65 72 20 63 61 63 68 65 20 68 61 73 20 63  ager cache has c
12d48 72 65 61 74 65 64 20 61 20 6e 65 77 20 70 61 67  reated a new pag
12d49 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 20 6e  e. Its content n
12d4a 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20  eeds to .    ** 
12d4b 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a  be initialized..
12d4c 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e      */.    int n
12d4d 4d 61 78 3b 0a 20 20 20 20 50 41 47 45 52 5f 49  Max;.    PAGER_I
12d4e 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73  NCR(pPager->nMis
12d4f 73 29 3b 0a 20 20 20 20 70 50 67 2d 3e 70 50 61  s);.    pPg->pPa
12d50 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 0a 20  ger = pPager;.. 
12d51 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
12d52 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50  agerPagecount(pP
12d53 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20  ager, &nMax);.  
12d54 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12d55 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
12d56 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
12d57 50 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Pg);.      retur
12d58 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n rc;.    }..   
12d59 20 69 66 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70   if( nMax<(int)p
12d5a 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20  gno || MEMDB || 
12d5b 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20  noContent ){.   
12d5c 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67     if( pgno>pPag
12d5d 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20  er->mxPgno ){.  
12d5e 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
12d5f 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20  erUnref(pPg);.  
12d60 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
12d61 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20  ITE_FULL;.      
12d62 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 6f  }.      if( noCo
12d63 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20  ntent ){.       
12d64 20 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f 20 73   /* Failure to s
12d65 65 74 20 74 68 65 20 62 69 74 73 20 69 6e 20 74  et the bits in t
12d66 68 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74  he InJournal bit
12d67 2d 76 65 63 74 6f 72 73 20 69 73 20 62 65 6e 69  -vectors is beni
12d68 67 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49  gn..        ** I
12d69 74 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73 20 74  t merely means t
12d6a 68 61 74 20 77 65 20 6d 69 67 68 74 20 64 6f 20  hat we might do 
12d6b 73 6f 6d 65 20 65 78 74 72 61 20 77 6f 72 6b 20  some extra work 
12d6c 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a 20 20  to journal a .  
12d6d 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 74 68        ** page th
12d6e 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  at does not need
12d6f 20 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64   to be journaled
12d70 2e 20 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c  .  Nevertheless,
12d71 20 62 65 20 73 75 72 65 20 0a 20 20 20 20 20 20   be sure .      
12d72 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65    ** to test the
12d73 20 63 61 73 65 20 77 68 65 72 65 20 61 20 6d 61   case where a ma
12d74 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72  lloc error occur
12d75 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 20 74  s while trying t
12d76 6f 20 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a  o set .        *
12d77 2a 20 61 20 62 69 74 20 69 6e 20 61 20 62 69 74  * a bit in a bit
12d78 20 76 65 63 74 6f 72 2e 0a 20 20 20 20 20 20 20   vector..       
12d79 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
12d7a 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
12d7b 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20  lloc();.        
12d7c 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72  if( pgno<=pPager
12d7d 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a  ->dbOrigSize ){.
12d7e 20 20 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e            TESTON
12d7f 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c 69 74  LY( rc = ) sqlit
12d80 65 33 42 69 74 76 65 63 53 65 74 28 70 50 61 67  e3BitvecSet(pPag
12d81 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20  er->pInJournal, 
12d82 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20  pgno);.         
12d83 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53   testcase( rc==S
12d84 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
12d85 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12d86 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20   TESTONLY( rc = 
12d87 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74  ) addToSavepoint
12d88 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20  Bitvecs(pPager, 
12d89 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74  pgno);.        t
12d8a 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c  estcase( rc==SQL
12d8b 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20  ITE_NOMEM );.   
12d8c 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42       sqlite3EndB
12d8d 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
12d8e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
12d8f 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e      memset(pPg->
12d90 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72  pData, 0, pPager
12d91 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
12d92 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54 52 41     }.      IOTRA
12d93 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25 64 5c  CE(("ZERO %p %d\
12d94 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f  n", pPager, pgno
12d95 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ));.    }else{. 
12d96 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67       assert( pPg
12d97 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72  ->pPager==pPager
12d98 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   );.      rc = r
12d99 65 61 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a  eadDbPage(pPg);.
12d9a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
12d9b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
12d9c 20 20 20 70 61 67 65 72 44 72 6f 70 50 61 67 65     pagerDropPage
12d9d 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 72  (pPg);.        r
12d9e 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
12d9f 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53  }.    }.#ifdef S
12da0 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
12da1 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48  S.    pPg->pageH
12da2 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65  ash = pager_page
12da3 68 61 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69  hash(pPg);.#endi
12da4 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  f.  }else{.    /
12da5 2a 20 54 68 65 20 72 65 71 75 65 73 74 65 64 20  * The requested 
12da6 70 61 67 65 20 69 73 20 69 6e 20 74 68 65 20 70  page is in the p
12da7 61 67 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20  age cache. */.  
12da8 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61    PAGER_INCR(pPa
12da9 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 7d 0a  ger->nHit);.  }.
12daa 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 67  .  *ppPage = pPg
12dab 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
12dac 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
12dad 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69 66  cquire a page if
12dae 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 69   it is already i
12daf 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  n the in-memory 
12db0 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f  cache.  Do.** no
12db1 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65 20  t read the page 
12db2 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 75  from disk.  Retu
12db3 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
12db4 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20  the page,.** or 
12db5 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73  0 if the page is
12db6 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 41   not in cache. A
12db7 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 69 66  lso, return 0 if
12db8 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72 20 69   the .** pager i
12db9 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43  s in PAGER_UNLOC
12dba 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69  K state when thi
12dbb 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
12dbc 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74  lled,.** or if t
12dbd 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 61  he pager is in a
12dbe 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74  n error state ot
12dbf 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
12dc0 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  FULL..**.** See 
12dc1 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67 65  also sqlite3Page
12dc2 72 47 65 74 28 29 2e 20 20 54 68 65 20 64 69 66  rGet().  The dif
12dc3 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
12dc4 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
12dc5 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  and sqlite3Pager
12dc6 47 65 74 28 29 20 69 73 20 74 68 61 74 20 5f 67  Get() is that _g
12dc7 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20  et() will go to 
12dc8 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72 65 61  the disk and rea
12dc9 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65  d.** in the page
12dca 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
12dcb 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 63  not already in c
12dcc 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f 75 74  ache.  This rout
12dcd 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e  ine.** returns N
12dce 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67 65 20  ULL if the page 
12dcf 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20  is not in cache 
12dd0 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49 2f 4f  or if a disk I/O
12dd1 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65   error .** has e
12dd2 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f  ver happened..*/
12dd3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12dd4 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33 50  DbPage *sqlite3P
12dd5 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72  agerLookup(Pager
12dd6 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70   *pPager, Pgno p
12dd7 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70  gno){.  PgHdr *p
12dd8 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  Pg = 0;.  assert
12dd9 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20  ( pPager!=0 );. 
12dda 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30   assert( pgno!=0
12ddb 20 29 3b 0a 0a 20 20 69 66 28 20 28 70 50 61 67   );..  if( (pPag
12ddc 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52  er->state!=PAGER
12ddd 5f 55 4e 4c 4f 43 4b 29 0a 20 20 20 26 26 20 28  _UNLOCK).   && (
12dde 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d  pPager->errCode=
12ddf 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50  =SQLITE_OK || pP
12de0 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53  ager->errCode==S
12de1 51 4c 49 54 45 5f 46 55 4c 4c 29 0a 20 20 29 7b  QLITE_FULL).  ){
12de2 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
12de3 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e  heFetch(pPager->
12de4 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30  pPCache, pgno, 0
12de5 2c 20 26 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20  , &pPg);.  }..  
12de6 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f  return pPg;.}../
12de7 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70  *.** Release a p
12de8 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a  age reference..*
12de9 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62  *.** If the numb
12dea 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
12deb 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f   to the page dro
12dec 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20  p to zero, then 
12ded 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61  the.** page is a
12dee 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20  dded to the LRU 
12def 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20  list.  When all 
12df0 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c  references to al
12df1 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72  l pages.** are r
12df2 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62  eleased, a rollb
12df3 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74  ack occurs and t
12df4 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  he lock on the d
12df5 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65  atabase is.** re
12df6 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  moved..*/.SQLITE
12df7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
12df8 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
12df9 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
12dfa 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50  if( pPg ){.    P
12dfb 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
12dfc 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20  Pg->pPager;.    
12dfd 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c  sqlite3PcacheRel
12dfe 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70  ease(pPg);.    p
12dff 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73  agerUnlockIfUnus
12e00 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  ed(pPager);.  }.
12e01 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
12e02 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  main journal fil
12e03 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  e has already be
12e04 65 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 72  en opened, ensur
12e05 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 75  e that the.** su
12e06 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  b-journal file i
12e07 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 74  s open too. If t
12e08 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
12e09 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20  is not open,.** 
12e0a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
12e0b 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
12e0c 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
12e0d 75 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 68  urned if everyth
12e0e 69 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64 69  ing goes accordi
12e0f 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20  ng to plan. .** 
12e10 41 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  An SQLITE_IOERR_
12e11 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69  XXX error code i
12e12 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20  s returned if a 
12e13 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c 69  call to .** sqli
12e14 74 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 6c  te3OsOpen() fail
12e15 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
12e16 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28   openSubJournal(
12e17 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
12e18 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
12e19 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f 70  E_OK;.  if( isOp
12e1a 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
12e1b 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  && !isOpen(pPage
12e1c 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20  r->sjfd) ){.    
12e1d 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
12e1e 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
12e1f 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
12e20 59 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62  Y || pPager->sub
12e21 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20  jInMemory ){.   
12e22 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75     sqlite3MemJou
12e23 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d  rnalOpen(pPager-
12e24 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73  >sjfd);.    }els
12e25 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  e{.      rc = pa
12e26 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67  gerOpentemp(pPag
12e27 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 64  er, pPager->sjfd
12e28 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55  , SQLITE_OPEN_SU
12e29 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d  BJOURNAL);.    }
12e2a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
12e2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
12e2c 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
12e2d 65 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20  ed at the start 
12e2e 6f 66 20 65 76 65 72 79 20 77 72 69 74 65 20 74  of every write t
12e2f 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54  ransaction..** T
12e30 68 65 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64  here must alread
12e31 79 20 62 65 20 61 20 52 45 53 45 52 56 45 44 20  y be a RESERVED 
12e32 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  or EXCLUSIVE loc
12e33 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
12e34 65 20 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20  e .** file when 
12e35 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
12e36 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70  called..**.** Op
12e37 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  en the journal f
12e38 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50  ile for pager pP
12e39 61 67 65 72 20 61 6e 64 20 77 72 69 74 65 20 61  ager and write a
12e3a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a   journal header.
12e3b 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  ** to the start 
12e3c 6f 66 20 69 74 2e 20 49 66 20 74 68 65 72 65 20  of it. If there 
12e3d 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70  are active savep
12e3e 6f 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20  oints, open the 
12e3f 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61  sub-journal.** a
12e40 73 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e  s well. This fun
12e41 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73  ction is only us
12e42 65 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72  ed when the jour
12e43 6e 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e  nal file is bein
12e44 67 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20  g .** opened to 
12e45 77 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b  write a rollback
12e46 20 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73   log for a trans
12e47 61 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f  action. It is no
12e48 74 20 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20  t used .** when 
12e49 6f 70 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f  opening a hot jo
12e4a 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f  urnal file to ro
12e4b 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a  ll it back..**.*
12e4c 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * If the journal
12e4d 20 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79   file is already
12e4e 20 6f 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79   open (as it may
12e4f 20 62 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65   be in exclusive
12e50 20 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20   mode),.** then 
12e51 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75  this function ju
12e52 73 74 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72  st writes a jour
12e53 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68  nal header to th
12e54 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a  e start of the.*
12e55 2a 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66  * already open f
12e56 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74  ile. .**.** Whet
12e57 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a  her or not the j
12e58 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f  ournal file is o
12e59 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 66 75  pened by this fu
12e5a 6e 63 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50  nction, the.** P
12e5b 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ager.pInJournal 
12e5c 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65  bitvec structure
12e5d 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a   is allocated..*
12e5e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
12e5f 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68  TE_OK if everyth
12e60 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75  ing is successfu
12e61 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  l. Otherwise, re
12e62 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  turn .** SQLITE_
12e63 4e 4f 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74  NOMEM if the att
12e64 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65  empt to allocate
12e65 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61   Pager.pInJourna
12e66 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20  l fails, or .** 
12e67 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  an IO error code
12e68 20 69 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77   if opening or w
12e69 72 69 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e  riting the journ
12e6a 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a  al file fails..*
12e6b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
12e6c 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28  er_open_journal(
12e6d 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
12e6e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
12e6f 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
12e70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12e71 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
12e72 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63   sqlite3_vfs * c
12e73 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67  onst pVfs = pPag
12e74 65 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c  er->pVfs;   /* L
12e75 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66  ocal cache of vf
12e76 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20  s pointer */..  
12e77 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
12e78 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53  state>=PAGER_RES
12e79 45 52 56 45 44 20 29 3b 0a 20 20 61 73 73 65 72  ERVED );.  asser
12e7a 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f  t( pPager->useJo
12e7b 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72  urnal );.  asser
12e7c 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  t( pPager->pInJo
12e7d 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20  urnal==0 );.  . 
12e7e 20 2f 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69   /* If already i
12e7f 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  n the error stat
12e80 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  e, this function
12e81 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a   is a no-op. */.
12e82 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72    if( pPager->er
12e83 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 74  rCode ){.    ret
12e84 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43  urn pPager->errC
12e85 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  ode;.  }..  /* T
12e86 4f 44 4f 3a 20 49 73 20 69 74 20 72 65 61 6c 6c  ODO: Is it reall
12e87 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65  y possible to ge
12e88 74 20 68 65 72 65 20 77 69 74 68 20 64 62 53 69  t here with dbSi
12e89 7a 65 56 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e  zeValid==0? If n
12e8a 6f 74 2c 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c  ot,.  ** the cal
12e8b 6c 20 74 6f 20 50 61 67 65 72 50 61 67 65 63 6f  l to PagerPageco
12e8c 75 6e 74 28 29 20 63 61 6e 20 62 65 20 72 65 6d  unt() can be rem
12e8d 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73  oved..  */.  tes
12e8e 74 63 61 73 65 28 20 70 50 61 67 65 72 2d 3e 64  tcase( pPager->d
12e8f 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b  bSizeValid==0 );
12e90 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  .  sqlite3PagerP
12e91 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
12e92 20 30 29 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e   0);..  pPager->
12e93 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c  pInJournal = sql
12e94 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
12e95 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29  (pPager->dbSize)
12e96 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
12e97 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b  pInJournal==0 ){
12e98 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
12e99 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
12e9a 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75   /* Open the jou
12e9b 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20  rnal file if it 
12e9c 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f  is not already o
12e9d 70 65 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69  pen. */.  if( !i
12e9e 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
12e9f 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  d) ){.    if( pP
12ea0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
12ea1 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
12ea2 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20  MODE_MEMORY ){. 
12ea3 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a       sqlite3MemJ
12ea4 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65  ournalOpen(pPage
12ea5 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c  r->jfd);.    }el
12ea6 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  se{.      const 
12ea7 69 6e 74 20 66 6c 61 67 73 20 3d 20 20 20 20 20  int flags =     
12ea8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12ea9 20 56 46 53 20 66 6c 61 67 73 20 74 6f 20 6f 70   VFS flags to op
12eaa 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  en journal file 
12eab 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54  */.        SQLIT
12eac 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
12ead 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45  |SQLITE_OPEN_CRE
12eae 41 54 45 7c 0a 20 20 20 20 20 20 20 20 28 70 50  ATE|.        (pP
12eaf 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f  ager->tempFile ?
12eb0 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c   .          (SQL
12eb1 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
12eb2 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50  NCLOSE|SQLITE_OP
12eb3 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29  EN_TEMP_JOURNAL)
12eb4 3a 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c  :.          (SQL
12eb5 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
12eb6 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20 20 29  URNAL).        )
12eb7 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
12eb8 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
12eb9 49 54 45 0a 20 20 20 20 20 20 72 63 20 3d 20 73  ITE.      rc = s
12eba 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65  qlite3JournalOpe
12ebb 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70 56 66  n(.          pVf
12ebc 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
12ebd 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  nal, pPager->jfd
12ebe 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66  , flags, jrnlBuf
12ebf 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 0a  ferSize(pPager).
12ec0 20 20 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20        );.#else. 
12ec1 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
12ec2 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50  3OsOpen(pVfs, pP
12ec3 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
12ec4 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61  pPager->jfd, fla
12ec5 67 73 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20  gs, 0);.#endif. 
12ec6 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
12ec7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
12ec8 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  | isOpen(pPager-
12ec9 3e 6a 66 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20  >jfd) );.  }... 
12eca 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 69   /* Write the fi
12ecb 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  rst journal head
12ecc 65 72 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  er to the journa
12ecd 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20  l file and open 
12ece 0a 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f  .  ** the sub-jo
12ecf 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61  urnal if necessa
12ed0 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  ry..  */.  if( r
12ed1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
12ed2 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65      /* TODO: Che
12ed3 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65  ck if all of the
12ed4 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65  se are really re
12ed5 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70  quired. */.    p
12ed6 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a  Pager->dbOrigSiz
12ed7 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69  e = pPager->dbSi
12ed8 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  ze;.    pPager->
12ed9 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d  journalStarted =
12eda 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
12edb 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
12edc 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d    pPager->nRec =
12edd 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
12ede 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
12edf 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d      pPager->setM
12ee0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70  aster = 0;.    p
12ee1 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
12ee2 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  r = 0;.    rc = 
12ee3 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
12ee4 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69  pPager);.  }.  i
12ee5 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12ee6 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76   && pPager->nSav
12ee7 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 72 63  epoint ){.    rc
12ee8 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61   = openSubJourna
12ee9 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a  l(pPager);.  }..
12eea 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12eeb 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
12eec 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
12eed 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
12eee 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  al);.    pPager-
12eef 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b  >pInJournal = 0;
12ef0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
12ef1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e  ;.}../*.** Begin
12ef2 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   a write-transac
12ef3 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 70 65 63  tion on the spec
12ef4 69 66 69 65 64 20 70 61 67 65 72 20 6f 62 6a 65  ified pager obje
12ef5 63 74 2e 20 49 66 20 61 20 0a 2a 2a 20 77 72 69  ct. If a .** wri
12ef6 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68  te-transaction h
12ef7 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
12ef8 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 66 75 6e  opened, this fun
12ef9 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
12efa 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65  ..**.** If the e
12efb 78 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69  xFlag argument i
12efc 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 63  s false, then ac
12efd 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 20 61  quire at least a
12efe 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63   RESERVED.** loc
12eff 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
12f00 65 20 66 69 6c 65 2e 20 49 66 20 65 78 46 6c 61  e file. If exFla
12f01 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  g is true, then 
12f02 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 73 74  acquire at least
12f03 0a 2a 2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45  .** an EXCLUSIVE
12f04 20 6c 6f 63 6b 2e 20 49 66 20 73 75 63 68 20 61   lock. If such a
12f05 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79   lock is already
12f06 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e   held, no lockin
12f07 67 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  g .** functions 
12f08 6e 65 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a  need be called..
12f09 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73  **.** If this is
12f0a 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79   not a temporary
12f0b 20 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69   or in-memory fi
12f0c 6c 65 20 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72  le and, the jour
12f0d 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20  nal file is .** 
12f0e 6f 70 65 6e 65 64 20 69 66 20 69 74 20 68 61 73  opened if it has
12f0f 20 6e 6f 74 20 62 65 65 6e 20 61 6c 72 65 61 64   not been alread
12f10 79 2e 20 46 6f 72 20 61 20 74 65 6d 70 6f 72 61  y. For a tempora
12f11 72 79 20 66 69 6c 65 2c 20 74 68 65 20 6f 70 65  ry file, the ope
12f12 6e 69 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20  ning .** of the 
12f13 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
12f14 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74  deferred until t
12f15 68 65 72 65 20 69 73 20 61 6e 20 61 63 74 75 61  here is an actua
12f16 6c 20 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72  l need to .** wr
12f17 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ite to the journ
12f18 61 6c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 68 61  al. TODO: Why ha
12f19 6e 64 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 66  ndle temporary f
12f1a 69 6c 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79  iles differently
12f1b 3f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a  ?.**.** If the j
12f1c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f  ournal file is o
12f1d 70 65 6e 65 64 20 28 6f 72 20 69 66 20 69 74 20  pened (or if it 
12f1e 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29  is already open)
12f1f 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72  , then a.** jour
12f20 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72  nal-header is wr
12f21 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 74 61  itten to the sta
12f22 72 74 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  rt of it..**.** 
12f23 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d  If the subjInMem
12f24 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ory argument is 
12f25 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61  non-zero, then a
12f26 6e 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f  ny sub-journal o
12f27 70 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20  pened.** within 
12f28 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
12f29 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20   will be opened 
12f2a 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  as an in-memory 
12f2b 66 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61  file. This.** ha
12f2c 73 20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74  s no effect if t
12f2d 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69  he sub-journal i
12f2e 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64  s already opened
12f2f 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 77   (as it may be w
12f30 68 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69  hen.** running i
12f31 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
12f32 29 20 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e  ) or if the tran
12f33 73 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  saction does not
12f34 20 72 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75   require a.** su
12f35 62 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68  b-journal. If th
12f36 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61  e subjInMemory a
12f37 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c  rgument is zero,
12f38 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72   then any requir
12f39 65 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61  ed.** sub-journa
12f3a 6c 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  l is implemented
12f3b 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50   in-memory if pP
12f3c 61 67 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65  ager is an in-me
12f3d 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a  mory database, .
12f3e 2a 2a 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65  ** or using a te
12f3f 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68  mporary file oth
12f40 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erwise..*/.SQLIT
12f41 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
12f42 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28  lite3PagerBegin(
12f43 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
12f44 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73  nt exFlag, int s
12f45 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20  ubjInMemory){.  
12f46 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
12f47 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  OK;.  assert( pP
12f48 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
12f49 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70  ER_UNLOCK );.  p
12f4a 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d  Pager->subjInMem
12f4b 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a 49 6e  ory = (u8)subjIn
12f4c 4d 65 6d 6f 72 79 3b 0a 20 20 69 66 28 20 70 50  Memory;.  if( pP
12f4d 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
12f4e 45 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20  ER_SHARED ){.   
12f4f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
12f50 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29  >pInJournal==0 )
12f51 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d  ;.    assert( !M
12f52 45 4d 44 42 20 26 26 20 21 70 50 61 67 65 72 2d  EMDB && !pPager-
12f53 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20  >tempFile );..  
12f54 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45    /* Obtain a RE
12f55 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74  SERVED lock on t
12f56 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12f57 2e 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20  . If the exFlag 
12f58 70 61 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a  parameter.    **
12f59 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69   is true, then i
12f5a 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 67 72 61  mmediately upgra
12f5b 64 65 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58  de this to an EX
12f5c 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68  CLUSIVE lock. Th
12f5d 65 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61  e.    ** busy-ha
12f5e 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63  ndler callback c
12f5f 61 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20  an be used when 
12f60 75 70 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65  upgrading to the
12f61 20 45 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a   EXCLUSIVE.    *
12f62 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20  * lock, but not 
12f63 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74  when obtaining t
12f64 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b  he RESERVED lock
12f65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
12f66 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  = sqlite3OsLock(
12f67 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45  pPager->fd, RESE
12f68 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  RVED_LOCK);.    
12f69 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12f6a 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  K ){.      pPage
12f6b 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
12f6c 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20  _RESERVED;.     
12f6d 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20   if( exFlag ){. 
12f6e 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65         rc = page
12f6f 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70  r_wait_on_lock(p
12f70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45  Pager, EXCLUSIVE
12f71 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a  _LOCK);.      }.
12f72 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
12f73 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f   the required lo
12f74 63 6b 73 20 77 65 72 65 20 73 75 63 63 65 73 73  cks were success
12f75 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20  fully obtained, 
12f76 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  open the journal
12f77 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64  .    ** file and
12f78 20 77 72 69 74 65 20 74 68 65 20 66 69 72 73 74   write the first
12f79 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20   journal-header 
12f7a 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  to it..    */.  
12f7b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
12f7c 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 75  _OK && pPager->u
12f7d 73 65 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 26  seJournal.     &
12f7e 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
12f7f 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55  lMode!=PAGER_JOU
12f80 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20  RNALMODE_OFF .  
12f81 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20    ){.      rc = 
12f82 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e  pager_open_journ
12f83 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  al(pPager);.    
12f84 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73  }.  }else if( is
12f85 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
12f86 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ) && pPager->jou
12f87 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20  rnalOff==0 ){.  
12f88 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e    /* This happen
12f89 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72  s when the pager
12f8a 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76   was in exclusiv
12f8b 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68  e-access mode th
12f8c 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69  e last.    ** ti
12f8d 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20 77 72  me a (read or wr
12f8e 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e  ite) transaction
12f8f 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c   was successfull
12f90 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20  y concluded.    
12f91 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65  ** by this conne
12f92 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f  ction. Instead o
12f93 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a  f deleting the j
12f94 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77  ournal file it w
12f95 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20  as .    ** kept 
12f96 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20  open and either 
12f97 77 61 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f  was truncated to
12f98 20 30 20 62 79 74 65 73 20 6f 72 20 69 74 73 20   0 bytes or its 
12f99 68 65 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a  header was.    *
12f9a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69  * overwritten wi
12f9b 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f  th zeros..    */
12f9c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
12f9d 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a  ger->nRec==0 );.
12f9e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
12f9f 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d  er->dbOrigSize==
12fa0 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
12fa1 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
12fa2 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63  nal==0 );.    rc
12fa3 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f   = pager_open_jo
12fa4 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
12fa5 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45   }..  PAGERTRACE
12fa6 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25  (("TRANSACTION %
12fa7 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
12fa8 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72  ager)));.  asser
12fa9 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  t( !isOpen(pPage
12faa 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65  r->jfd) || pPage
12fab 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20  r->journalOff>0 
12fac 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  || rc!=SQLITE_OK
12fad 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
12fae 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61  .}../*.** Mark a
12faf 20 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67   single data pag
12fb0 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20  e as writeable. 
12fb1 54 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74  The page is writ
12fb2 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a  ten into the .**
12fb3 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72   main journal or
12fb4 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20   sub-journal as 
12fb5 72 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 65  required. If the
12fb6 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e   page is written
12fb7 20 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20   into.** one of 
12fb8 74 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68  the journals, th
12fb9 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
12fba 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68  bit is set in th
12fbb 65 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a  e .** Pager.pInJ
12fbc 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e  ournal bitvec an
12fbd 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70  d the PagerSavep
12fbe 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e  oint.pInSavepoin
12fbf 74 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20  t bitvecs.** of 
12fc0 61 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69  any open savepoi
12fc1 6e 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 61  nts as appropria
12fc2 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  te..*/.static in
12fc3 74 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 67  t pager_write(Pg
12fc4 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69  Hdr *pPg){.  voi
12fc5 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e  d *pData = pPg->
12fc6 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a  pData;.  Pager *
12fc7 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50  pPager = pPg->pP
12fc8 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d  ager;.  int rc =
12fc9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f   SQLITE_OK;..  /
12fca 2a 20 43 68 65 63 6b 20 66 6f 72 20 65 72 72 6f  * Check for erro
12fcb 72 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  rs.  */.  if( pP
12fcc 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b  ager->errCode ){
12fcd 20 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61   .    return pPa
12fce 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20  ger->errCode;.  
12fcf 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  }.  if( pPager->
12fd0 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20  readOnly ){.    
12fd1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45  return SQLITE_PE
12fd2 52 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  RM;.  }..  asser
12fd3 74 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d  t( !pPager->setM
12fd4 61 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43  aster );..  CHEC
12fd5 4b 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20  K_PAGE(pPg);..  
12fd6 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65  /* Mark the page
12fd7 20 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74   as dirty.  If t
12fd8 68 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65  he page has alre
12fd9 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ady been written
12fda 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75  .  ** to the jou
12fdb 72 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e  rnal then we can
12fdc 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77   return right aw
12fdd 61 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ay..  */.  sqlit
12fde 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74  e3PcacheMakeDirt
12fdf 79 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61  y(pPg);.  if( pa
12fe0 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29  geInJournal(pPg)
12fe1 20 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65   && !subjRequire
12fe2 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20  sPage(pPg) ){.  
12fe3 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69    pPager->dbModi
12fe4 66 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  fied = 1;.  }els
12fe5 65 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  e{..    /* If we
12fe6 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69   get this far, i
12fe7 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
12fe8 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62   page needs to b
12fe9 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e  e.    ** written
12fea 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74   to the transact
12feb 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74  ion journal or t
12fec 68 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f  he ckeckpoint jo
12fed 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20  urnal.    ** or 
12fee 62 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  both..    **.   
12fef 20 2a 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20   ** First check 
12ff0 74 6f 20 73 65 65 20 74 68 61 74 20 74 68 65 20  to see that the 
12ff1 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72  transaction jour
12ff2 6e 61 6c 20 65 78 69 73 74 73 20 61 6e 64 0a 20  nal exists and. 
12ff3 20 20 20 2a 2a 20 63 72 65 61 74 65 20 69 74 20     ** create it 
12ff4 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a  if it does not..
12ff5 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
12ff6 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
12ff7 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29  !=PAGER_UNLOCK )
12ff8 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
12ff9 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 50 61  e3PagerBegin(pPa
12ffa 67 65 72 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e  ger, 0, pPager->
12ffb 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20  subjInMemory);. 
12ffc 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
12ffd 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
12ffe 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
12fff 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
13000 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
13001 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 20 20  RESERVED );.    
13002 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67  if( !isOpen(pPag
13003 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67  er->jfd) && pPag
13004 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 0a 20  er->useJournal. 
13005 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 67           && pPag
13006 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21  er->journalMode!
13007 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
13008 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 20  DE_OFF ){.      
13009 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f  rc = pager_open_
1300a 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b  journal(pPager);
1300b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1300c 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
1300d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
1300e 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
1300f 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f  ed = 1;.  .    /
13010 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f  * The transactio
13011 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78  n journal now ex
13012 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 65  ists and we have
13013 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 61   a RESERVED or a
13014 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49  n.    ** EXCLUSI
13015 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d  VE lock on the m
13016 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
13017 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63 75  e.  Write the cu
13018 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20  rrent page to.  
13019 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63    ** the transac
1301a 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20  tion journal if 
1301b 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20  it is not there 
1301c 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a  already..    */.
1301d 20 20 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a      if( !pageInJ
1301e 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69  ournal(pPg) && i
1301f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
13020 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  d) ){.      if( 
13021 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65  pPg->pgno<=pPage
13022 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b  r->dbOrigSize ){
13023 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73  .        u32 cks
13024 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  um;.        char
13025 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20   *pData2;..     
13026 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20     /* We should 
13027 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 74  never write to t
13028 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
13029 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 20  the page that.  
1302a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e        ** contain
1302b 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  s the database l
1302c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  ocks.  The follo
1302d 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 69  wing assert veri
1302e 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  fies.        ** 
1302f 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20  that we do not. 
13030 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
13031 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41  t( pPg->pgno!=PA
13032 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
13033 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70  er) );.        p
13034 44 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70  Data2 = CODEC2(p
13035 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50  Pager, pData, pP
13036 67 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20  g->pgno, 7);.   
13037 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67       cksum = pag
13038 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c  er_cksum(pPager,
13039 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20   (u8*)pData2);. 
1303a 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74         rc = writ
1303b 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  e32bits(pPager->
1303c 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  jfd, pPager->jou
1303d 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67  rnalOff, pPg->pg
1303e 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  no);.        if(
1303f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13040 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
13041 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
13042 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61  pPager->jfd, pDa
13043 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  ta2, pPager->pag
13044 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20  eSize,.         
13045 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13046 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
13047 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20  rnalOff + 4);.  
13048 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
13049 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50  journalOff += pP
1304a 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34  ager->pageSize+4
1304b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1304c 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1304d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1304e 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62     rc = write32b
1304f 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
13050 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13051 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20  Off, cksum);.   
13052 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
13053 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a  ournalOff += 4;.
13054 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13055 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54    IOTRACE(("JOUT
13056 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e   %p %d %lld %d\n
13057 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e  ", pPager, pPg->
13058 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20  pgno, .         
13059 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
1305a 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67  journalOff, pPag
1305b 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a  er->pageSize));.
1305c 20 20 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e          PAGER_IN
1305d 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72  CR(sqlite3_pager
1305e 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a  _writej_count);.
1305f 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41          PAGERTRA
13060 43 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20  CE(("JOURNAL %d 
13061 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63  page %d needSync
13062 3d 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e  =%d hash(%08x)\n
13063 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
13064 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
13065 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20   pPg->pgno, .   
13066 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d            ((pPg-
13067 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
13068 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61  D_SYNC)?1:0), pa
13069 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
1306a 29 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  )));..        /*
1306b 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f 20 6f   Even if an IO o
1306c 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 6f 72  r diskfull error
1306d 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20   occurred while 
1306e 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a  journalling the.
1306f 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20          ** page 
13070 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f  in the block abo
13071 76 65 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64  ve, set the need
13072 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74  -sync flag for t
13073 68 65 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20  he page..       
13074 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77   ** Otherwise, w
13075 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
13076 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
13077 63 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e  ck, the logic in
13078 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79  .        ** play
13079 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20  back_one_page() 
1307a 77 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20  will think that 
1307b 74 68 65 20 70 61 67 65 20 6e 65 65 64 73 20 74  the page needs t
1307c 6f 20 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20  o be restored.  
1307d 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20        ** in the 
1307e 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41  database file. A
1307f 6e 64 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f  nd if an IO erro
13080 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64  r occurs while d
13081 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20  oing so,.       
13082 20 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74   ** then corrupt
13083 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a  ion may follow..
13084 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
13085 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e     if( !pPager->
13086 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20  noSync ){.      
13087 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c      pPg->flags |
13088 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  = PGHDR_NEED_SYN
13089 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  C;.          pPa
1308a 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20  ger->needSync = 
1308b 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  1;.        }..  
1308c 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f        /* An erro
1308d 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 77  r has occurred w
1308e 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f  riting to the jo
1308f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20  urnal file. The 
13090 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e  .        ** tran
13091 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  saction will be 
13092 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74  rolled back by t
13093 68 65 20 6c 61 79 65 72 20 61 62 6f 76 65 2e 0a  he layer above..
13094 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
13095 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
13096 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
13097 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
13098 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
13099 70 50 61 67 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a  pPager->nRec++;.
1309a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1309b 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
1309c 61 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  al!=0 );.       
1309d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74   rc = sqlite3Bit
1309e 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70  vecSet(pPager->p
1309f 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e  InJournal, pPg->
130a0 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74  pgno);.        t
130a1 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c  estcase( rc==SQL
130a2 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20  ITE_NOMEM );.   
130a3 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d       assert( rc=
130a4 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63  =SQLITE_OK || rc
130a5 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
130a6 3b 0a 20 20 20 20 20 20 20 20 72 63 20 7c 3d 20  ;.        rc |= 
130a7 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69  addToSavepointBi
130a8 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50  tvecs(pPager, pP
130a9 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  g->pgno);.      
130aa 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
130ab 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
130ac 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
130ad 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20  ITE_NOMEM );.   
130ae 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
130af 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
130b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
130b1 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f   if( !pPager->jo
130b2 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26 26 20  urnalStarted && 
130b3 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20  !pPager->noSync 
130b4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67  ){.          pPg
130b5 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
130b6 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20  _NEED_SYNC;.    
130b7 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65        pPager->ne
130b8 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
130b9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 50 41      }.        PA
130ba 47 45 52 54 52 41 43 45 28 28 22 41 50 50 45 4e  GERTRACE(("APPEN
130bb 44 20 25 64 20 70 61 67 65 20 25 64 20 6e 65 65  D %d page %d nee
130bc 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 20  dSync=%d\n",.   
130bd 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47               PAG
130be 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50  ERID(pPager), pP
130bf 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20  g->pgno,.       
130c0 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66          ((pPg->f
130c1 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f  lags&PGHDR_NEED_
130c2 53 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a 20 20  SYNC)?1:0)));.  
130c3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
130c4 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61     /* If the sta
130c5 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69  tement journal i
130c6 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70  s open and the p
130c7 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74  age is not in it
130c8 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72  ,.    ** then wr
130c9 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ite the current 
130ca 70 61 67 65 20 74 6f 20 74 68 65 20 73 74 61 74  page to the stat
130cb 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20  ement journal.  
130cc 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a  Note that.    **
130cd 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a   the statement j
130ce 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69  ournal format di
130cf 66 66 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73  ffers from the s
130d0 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20  tandard journal 
130d1 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e  format.    ** in
130d2 20 74 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74   that it omits t
130d3 68 65 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64  he checksums and
130d4 20 74 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20   the header..   
130d5 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 62 6a   */.    if( subj
130d6 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67  RequiresPage(pPg
130d7 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
130d8 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70  subjournalPage(p
130d9 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  Pg);.    }.  }..
130da 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
130db 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 61 6e  database size an
130dc 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20  d return..  */. 
130dd 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
130de 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48  >state>=PAGER_SH
130df 41 52 45 44 20 29 3b 0a 20 20 69 66 28 20 70 50  ARED );.  if( pP
130e0 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 67  ager->dbSize<pPg
130e1 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50  ->pgno ){.    pP
130e2 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70  ager->dbSize = p
130e3 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a 20 20  Pg->pgno;.  }.  
130e4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
130e5 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74 61 20  .** Mark a data 
130e6 70 61 67 65 20 61 73 20 77 72 69 74 65 61 62 6c  page as writeabl
130e7 65 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  e. This routine 
130e8 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62  must be called b
130e9 65 66 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69 6e 67  efore .** making
130ea 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 70 61   changes to a pa
130eb 67 65 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  ge. The caller m
130ec 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65  ust check the re
130ed 74 75 72 6e 20 76 61 6c 75 65 20 0a 2a 2a 20 6f  turn value .** o
130ee 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  f this function 
130ef 61 6e 64 20 62 65 20 63 61 72 65 66 75 6c 20 6e  and be careful n
130f0 6f 74 20 74 6f 20 63 68 61 6e 67 65 20 61 6e 79  ot to change any
130f1 20 70 61 67 65 20 64 61 74 61 20 75 6e 6c 65 73   page data unles
130f2 73 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  s .** this routi
130f3 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ne returns SQLIT
130f4 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  E_OK..**.** The 
130f5 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
130f6 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
130f7 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69 74 65   and pager_write
130f8 28 29 20 69 73 20 74 68 61 74 20 74 68 69 73 0a  () is that this.
130f9 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f  ** function also
130fa 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 20   deals with the 
130fb 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65  special case whe
130fc 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 61 67  re 2 or more pag
130fd 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73  es.** fit on a s
130fe 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f  ingle disk secto
130ff 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  r. In this case 
13100 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e 74 20  all co-resident 
13101 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61  pages.** must ha
13102 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  ve been written 
13103 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
13104 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74 75 72  ile before retur
13105 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ning..**.** If a
13106 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
13107 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20  SQLITE_NOMEM or 
13108 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  an IO error code
13109 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20   is returned.** 
1310a 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20  as appropriate. 
1310b 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54  Otherwise, SQLIT
1310c 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  E_OK..*/.SQLITE_
1310d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1310e 74 65 33 50 61 67 65 72 57 72 69 74 65 28 44 62  te3PagerWrite(Db
1310f 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a  Page *pDbPage){.
13110 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
13111 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a  E_OK;..  PgHdr *
13112 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b 0a 20  pPg = pDbPage;. 
13113 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
13114 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
13115 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53 65 63  Pgno nPagePerSec
13116 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d 3e 73  tor = (pPager->s
13117 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67 65 72  ectorSize/pPager
13118 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20  ->pageSize);..  
13119 69 66 28 20 6e 50 61 67 65 50 65 72 53 65 63 74  if( nPagePerSect
1311a 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f  or>1 ){.    Pgno
1311b 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20   nPageCount;    
1311c 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
1311d 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
1311e 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
1311f 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b  */.    Pgno pg1;
13120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13121 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f   /* First page o
13122 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67  f the sector pPg
13123 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20   is located on. 
13124 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65  */.    int nPage
13125 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
13126 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
13127 67 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 20  ges starting at 
13128 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a  pg1 to journal *
13129 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20  /.    int ii;   
1312a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1312b 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1312c 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 65 64 53  */.    int needS
1312d 79 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ync = 0;        
1312e 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20   /* True if any 
1312f 70 61 67 65 20 68 61 73 20 50 47 48 44 52 5f 4e  page has PGHDR_N
13130 45 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 20 20  EED_SYNC */..   
13131 20 2f 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f   /* Set the doNo
13132 74 53 79 6e 63 20 66 6c 61 67 20 74 6f 20 31 2e  tSync flag to 1.
13133 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
13134 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77   we cannot allow
13135 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a   a journal.    *
13136 2a 20 68 65 61 64 65 72 20 74 6f 20 62 65 20 77  * header to be w
13137 72 69 74 74 65 6e 20 62 65 74 77 65 65 6e 20 74  ritten between t
13138 68 65 20 70 61 67 65 73 20 6a 6f 75 72 6e 61 6c  he pages journal
13139 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
1313a 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
1313b 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29  assert( !MEMDB )
1313c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
1313d 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d  ager->doNotSync=
1313e 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72  =0 );.    pPager
1313f 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 31 3b  ->doNotSync = 1;
13140 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 74 72  ..    /* This tr
13141 69 63 6b 20 61 73 73 75 6d 65 73 20 74 68 61 74  ick assumes that
13142 20 62 6f 74 68 20 74 68 65 20 70 61 67 65 2d 73   both the page-s
13143 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73  ize and sector-s
13144 69 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 61  ize are.    ** a
13145 6e 20 69 6e 74 65 67 65 72 20 70 6f 77 65 72 20  n integer power 
13146 6f 66 20 32 2e 20 49 74 20 73 65 74 73 20 76 61  of 2. It sets va
13147 72 69 61 62 6c 65 20 70 67 31 20 74 6f 20 74 68  riable pg1 to th
13148 65 20 69 64 65 6e 74 69 66 69 65 72 0a 20 20 20  e identifier.   
13149 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74   ** of the first
1314a 20 70 61 67 65 20 6f 66 20 74 68 65 20 73 65 63   page of the sec
1314b 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74  tor pPg is locat
1314c 65 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ed on..    */.  
1314d 20 20 70 67 31 20 3d 20 28 28 70 50 67 2d 3e 70    pg1 = ((pPg->p
1314e 67 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61 67 65  gno-1) & ~(nPage
1314f 50 65 72 53 65 63 74 6f 72 2d 31 29 29 20 2b 20  PerSector-1)) + 
13150 31 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50  1;..    sqlite3P
13151 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50  agerPagecount(pP
13152 61 67 65 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50  ager, (int *)&nP
13153 61 67 65 43 6f 75 6e 74 29 3b 0a 20 20 20 20 69  ageCount);.    i
13154 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61  f( pPg->pgno>nPa
13155 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20  geCount ){.     
13156 20 6e 50 61 67 65 20 3d 20 28 70 50 67 2d 3e 70   nPage = (pPg->p
13157 67 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a 20 20  gno - pg1)+1;.  
13158 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 67 31    }else if( (pg1
13159 2b 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d  +nPagePerSector-
1315a 31 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b  1)>nPageCount ){
1315b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e  .      nPage = n
1315c 50 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67 31 3b  PageCount+1-pg1;
1315d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1315e 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 50    nPage = nPageP
1315f 65 72 53 65 63 74 6f 72 3b 0a 20 20 20 20 7d 0a  erSector;.    }.
13160 20 20 20 20 61 73 73 65 72 74 28 6e 50 61 67 65      assert(nPage
13161 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  >0);.    assert(
13162 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b  pg1<=pPg->pgno);
13163 0a 20 20 20 20 61 73 73 65 72 74 28 28 70 67 31  .    assert((pg1
13164 2b 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70 67 6e  +nPage)>pPg->pgn
13165 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 69 3d  o);..    for(ii=
13166 30 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 20 72  0; ii<nPage && r
13167 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69  c==SQLITE_OK; ii
13168 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20  ++){.      Pgno 
13169 70 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20 20 20  pg = pg1+ii;.   
1316a 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b     PgHdr *pPage;
1316b 0a 20 20 20 20 20 20 69 66 28 20 70 67 3d 3d 70  .      if( pg==p
1316c 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 71 6c  Pg->pgno || !sql
1316d 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
1316e 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
1316f 6c 2c 20 70 67 29 20 29 7b 0a 20 20 20 20 20 20  l, pg) ){.      
13170 20 20 69 66 28 20 70 67 21 3d 50 41 47 45 52 5f    if( pg!=PAGER_
13171 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20  MJ_PGNO(pPager) 
13172 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
13173 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
13174 74 28 70 50 61 67 65 72 2c 20 70 67 2c 20 26 70  t(pPager, pg, &p
13175 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
13176 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13177 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
13178 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69    rc = pager_wri
13179 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  te(pPage);.     
1317a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
1317b 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
1317c 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20  ED_SYNC ){.     
1317d 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e           needSyn
1317e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  c = 1;.         
1317f 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67       assert(pPag
13180 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20  er->needSync);. 
13181 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
13182 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
13183 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65  PagerUnref(pPage
13184 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
13185 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
13186 65 6c 73 65 20 69 66 28 20 28 70 50 61 67 65 20  else if( (pPage 
13187 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70  = pager_lookup(p
13188 50 61 67 65 72 2c 20 70 67 29 29 21 3d 30 20 29  Pager, pg))!=0 )
13189 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  {.        if( pP
1318a 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  age->flags&PGHDR
1318b 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20  _NEED_SYNC ){.  
1318c 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63          needSync
1318d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
1318e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
1318f 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29  agerUnref(pPage)
13190 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
13191 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50  .    /* If the P
13192 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66  GHDR_NEED_SYNC f
13193 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 20 61  lag is set for a
13194 6e 79 20 6f 66 20 74 68 65 20 6e 50 61 67 65 20  ny of the nPage 
13195 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 74  pages .    ** st
13196 61 72 74 69 6e 67 20 61 74 20 70 67 31 2c 20 74  arting at pg1, t
13197 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 6f 20  hen it needs to 
13198 62 65 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 6f  be set for all o
13199 66 20 74 68 65 6d 2e 20 42 65 63 61 75 73 65 0a  f them. Because.
1319a 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 74      ** writing t
1319b 6f 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 6e  o any of these n
1319c 50 61 67 65 20 70 61 67 65 73 20 6d 61 79 20 64  Page pages may d
1319d 61 6d 61 67 65 20 74 68 65 20 6f 74 68 65 72 73  amage the others
1319e 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f 75  , the.    ** jou
1319f 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 63  rnal file must c
131a0 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29 65 64 20  ontain sync()ed 
131a1 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 6f 66  copies of all of
131a2 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62 65 66   them.    ** bef
131a3 6f 72 65 20 61 6e 79 20 6f 66 20 74 68 65 6d 20  ore any of them 
131a4 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f  can be written o
131a5 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
131a6 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a  se file..    */.
131a7 20 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63      if( needSync
131a8 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
131a9 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67  ( !MEMDB && pPag
131aa 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b  er->noSync==0 );
131ab 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b  .      for(ii=0;
131ac 20 69 69 3c 6e 50 61 67 65 20 26 26 20 6e 65 65   ii<nPage && nee
131ad 64 53 79 6e 63 3b 20 69 69 2b 2b 29 7b 0a 20 20  dSync; ii++){.  
131ae 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61        PgHdr *pPa
131af 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75  ge = pager_looku
131b0 70 28 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69  p(pPager, pg1+ii
131b1 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
131b2 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Page ){.        
131b3 20 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c    pPage->flags |
131b4 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  = PGHDR_NEED_SYN
131b5 43 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  C;.          sql
131b6 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
131b7 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  Page);.        }
131b8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
131b9 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65  ssert(pPager->ne
131ba 65 64 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a  edSync);.    }..
131bb 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
131bc 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31  er->doNotSync==1
131bd 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   );.    pPager->
131be 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20  doNotSync = 0;. 
131bf 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
131c0 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 44 62   pager_write(pDb
131c1 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Page);.  }.  ret
131c2 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
131c3 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
131c4 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 20 69  the page given i
131c5 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77  n the argument w
131c6 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61  as previously pa
131c7 73 73 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  ssed.** to sqlit
131c8 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2e 20  e3PagerWrite(). 
131c9 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
131ca 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   return TRUE if 
131cb 69 74 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63  it is ok.** to c
131cc 68 61 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e  hange the conten
131cd 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a  t of the page..*
131ce 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
131cf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
131d0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
131d1 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 50 61  Iswriteable(DbPa
131d2 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75  ge *pPg){.  retu
131d3 72 6e 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  rn pPg->flags&PG
131d4 48 44 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e  HDR_DIRTY;.}.#en
131d5 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c  dif../*.** A cal
131d6 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  l to this routin
131d7 65 20 74 65 6c 6c 73 20 74 68 65 20 70 61 67 65  e tells the page
131d8 72 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74  r that it is not
131d9 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0a 2a 2a   necessary to.**
131da 20 77 72 69 74 65 20 74 68 65 20 69 6e 66 6f 72   write the infor
131db 6d 61 74 69 6f 6e 20 6f 6e 20 70 61 67 65 20 70  mation on page p
131dc 50 67 20 62 61 63 6b 20 74 6f 20 74 68 65 20 64  Pg back to the d
131dd 69 73 6b 2c 20 65 76 65 6e 20 74 68 6f 75 67 68  isk, even though
131de 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 20 6d 69  .** that page mi
131df 67 68 74 20 62 65 20 6d 61 72 6b 65 64 20 61 73  ght be marked as
131e0 20 64 69 72 74 79 2e 20 20 54 68 69 73 20 68 61   dirty.  This ha
131e1 70 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70  ppens, for examp
131e2 6c 65 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  le, when.** the 
131e3 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 61 64  page has been ad
131e4 64 65 64 20 61 73 20 61 20 6c 65 61 66 20 6f 66  ded as a leaf of
131e5 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 61 6e   the freelist an
131e6 64 20 73 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e 74  d so its.** cont
131e7 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61  ent no longer ma
131e8 74 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tters..**.** The
131e9 20 6f 76 65 72 6c 79 69 6e 67 20 73 6f 66 74 77   overlying softw
131ea 61 72 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 20  are layer calls 
131eb 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
131ec 6e 20 61 6c 6c 20 6f 66 20 74 68 65 20 64 61 74  n all of the dat
131ed 61 0a 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76 65  a.** on the give
131ee 6e 20 70 61 67 65 20 69 73 20 75 6e 75 73 65 64  n page is unused
131ef 2e 20 54 68 65 20 70 61 67 65 72 20 6d 61 72 6b  . The pager mark
131f0 73 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c  s the page as cl
131f1 65 61 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69  ean so.** that i
131f2 74 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 77  t does not get w
131f3 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a  ritten to disk..
131f4 2a 2a 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f 77  **.** Tests show
131f5 20 74 68 61 74 20 74 68 69 73 20 6f 70 74 69 6d   that this optim
131f6 69 7a 61 74 69 6f 6e 20 63 61 6e 20 71 75 61 64  ization can quad
131f7 72 75 70 6c 65 20 74 68 65 20 73 70 65 65 64 20  ruple the speed 
131f8 6f 66 20 6c 61 72 67 65 20 0a 2a 2a 20 44 45 4c  of large .** DEL
131f9 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a  ETE operations..
131fa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
131fb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
131fc 67 65 72 44 6f 6e 74 57 72 69 74 65 28 50 67 48  gerDontWrite(PgH
131fd 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65  dr *pPg){.  Page
131fe 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
131ff 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20 28  >pPager;.  if( (
13200 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
13201 5f 44 49 52 54 59 29 20 26 26 20 70 50 61 67 65  _DIRTY) && pPage
13202 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30  r->nSavepoint==0
13203 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41   ){.    PAGERTRA
13204 43 45 28 28 22 44 4f 4e 54 5f 57 52 49 54 45 20  CE(("DONT_WRITE 
13205 70 61 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22  page %d of %d\n"
13206 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47  , pPg->pgno, PAG
13207 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a  ERID(pPager)));.
13208 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c      IOTRACE(("CL
13209 45 41 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 50  EAN %p %d\n", pP
1320a 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29  ager, pPg->pgno)
1320b 29 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73  ).    pPg->flags
1320c 20 7c 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57   |= PGHDR_DONT_W
1320d 52 49 54 45 3b 0a 23 69 66 64 65 66 20 53 51 4c  RITE;.#ifdef SQL
1320e 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
1320f 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73      pPg->pageHas
13210 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61  h = pager_pageha
13211 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a  sh(pPg);.#endif.
13212 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
13213 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
13214 6c 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74  led to increment
13215 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
13216 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
13217 0a 2a 2a 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  .** change-count
13218 65 72 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20  er, stored as a 
13219 34 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61  4-byte big-endia
1321a 6e 20 69 6e 74 65 67 65 72 20 73 74 61 72 74 69  n integer starti
1321b 6e 67 20 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f  ng at .** byte o
1321c 66 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20  ffset 24 of the 
1321d 70 61 67 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  pager file..**.*
1321e 2a 20 49 66 20 74 68 65 20 69 73 44 69 72 65 63  * If the isDirec
1321f 74 20 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20  t flag is zero, 
13220 74 68 65 6e 20 74 68 69 73 20 69 73 20 64 6f 6e  then this is don
13221 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a  e by calling .**
13222 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
13223 74 65 28 29 20 6f 6e 20 70 61 67 65 20 31 2c 20  te() on page 1, 
13224 74 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74  then modifying t
13225 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
13226 68 65 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 2e  he.** page data.
13227 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
13228 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75  e file will be u
13229 70 64 61 74 65 64 20 77 68 65 6e 20 74 68 65 20  pdated when the 
1322a 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73  current.** trans
1322b 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74  action is commit
1322c 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ted..**.** The i
1322d 73 44 69 72 65 63 74 20 66 6c 61 67 20 6d 61 79  sDirect flag may
1322e 20 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72   only be non-zer
1322f 6f 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79  o if the library
13230 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a   was compiled.**
13231 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45   with the SQLITE
13232 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57  _ENABLE_ATOMIC_W
13233 52 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e  RITE macro defin
13234 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
13235 2c 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74  ,.** if isDirect
13236 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
13237 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
13238 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20  file is updated 
13239 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77  directly.** by w
1323a 72 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65  riting an update
1323b 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67  d version of pag
1323c 65 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c  e 1 using a call
1323d 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69   to the .** sqli
1323e 74 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e  te3OsWrite() fun
1323f 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
13240 20 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f   int pager_incr_
13241 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61  changecounter(Pa
13242 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
13243 20 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a   isDirectMode){.
13244 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
13245 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c  E_OK;..  /* Decl
13246 61 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  are and initiali
13247 7a 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65  ze constant inte
13248 67 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20  ger 'isDirect'. 
13249 49 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d  If the.  ** atom
1324a 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a  ic-write optimiz
1324b 61 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64  ation is enabled
1324c 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20   in this build, 
1324d 74 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20  then isDirect.  
1324e 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  ** is initialize
1324f 64 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70  d to the value p
13250 61 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44  assed as the isD
13251 69 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65  irectMode parame
13252 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73  ter.  ** to this
13253 20 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72   function. Other
13254 77 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61  wise, it is alwa
13255 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a  ys set to zero..
13256 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64    **.  ** The id
13257 65 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68  ea is that if th
13258 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f  e atomic-write o
13259 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e  ptimization is n
1325a 6f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20  ot.  ** enabled 
1325b 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c  at compile time,
1325c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61   the compiler ca
1325d 6e 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73  n omit the tests
1325e 20 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65   of.  ** 'isDire
1325f 63 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65  ct' below, as we
13260 6c 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20  ll as the block 
13261 65 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a  enclosed in the.
13262 20 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65    ** "if( isDire
13263 63 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e  ct )" condition.
13264 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
13265 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d  LITE_ENABLE_ATOM
13266 49 43 5f 57 52 49 54 45 0a 20 20 63 6f 6e 73 74  IC_WRITE.  const
13267 20 69 6e 74 20 69 73 44 69 72 65 63 74 20 3d 20   int isDirect = 
13268 30 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 44  0;.  assert( isD
13269 69 72 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a  irectMode==0 );.
1326a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
1326b 45 52 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29  ER(isDirectMode)
1326c 3b 0a 23 65 6c 73 65 0a 20 20 63 6f 6e 73 74 20  ;.#else.  const 
1326d 69 6e 74 20 69 73 44 69 72 65 63 74 20 3d 20 69  int isDirect = i
1326e 73 44 69 72 65 63 74 4d 6f 64 65 3b 0a 23 65 6e  sDirectMode;.#en
1326f 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70  dif..  assert( p
13270 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
13271 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a  GER_RESERVED );.
13272 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63    if( !pPager->c
13273 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26  hangeCountDone &
13274 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  & pPager->dbSize
13275 3e 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20  >0 ){.    PgHdr 
13276 2a 70 50 67 48 64 72 3b 20 20 20 20 20 20 20 20  *pPgHdr;        
13277 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72          /* Refer
13278 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 2a  ence to page 1 *
13279 2f 0a 20 20 20 20 75 33 32 20 63 68 61 6e 67 65  /.    u32 change
1327a 5f 63 6f 75 6e 74 65 72 3b 20 20 20 20 20 20 20  _counter;       
1327b 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 76      /* Initial v
1327c 61 6c 75 65 20 6f 66 20 63 68 61 6e 67 65 2d 63  alue of change-c
1327d 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f 0a  ounter field */.
1327e 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50  .    assert( !pP
1327f 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26  ager->tempFile &
13280 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  & isOpen(pPager-
13281 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  >fd) );..    /* 
13282 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20 74  Open page 1 of t
13283 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69 74  he file for writ
13284 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d  ing. */.    rc =
13285 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
13286 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50 67  (pPager, 1, &pPg
13287 48 64 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Hdr);.    assert
13288 28 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20 72  ( pPgHdr==0 || r
13289 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
1328a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20  .    /* If page 
1328b 6f 6e 65 20 77 61 73 20 66 65 74 63 68 65 64 20  one was fetched 
1328c 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 61 6e  successfully, an
1328d 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  d this function 
1328e 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f 70  is not.    ** op
1328f 65 72 61 74 69 6e 67 20 69 6e 20 64 69 72 65 63  erating in direc
13290 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61 67  t-mode, make pag
13291 65 20 31 20 77 72 69 74 61 62 6c 65 2e 0a 20 20  e 1 writable..  
13292 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
13293 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 69  =SQLITE_OK && !i
13294 73 44 69 72 65 63 74 20 29 7b 0a 20 20 20 20 20  sDirect ){.     
13295 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
13296 65 72 57 72 69 74 65 28 70 50 67 48 64 72 29 3b  erWrite(pPgHdr);
13297 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
13298 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
13299 0a 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d  .      /* Increm
1329a 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a 75  ent the value ju
1329b 73 74 20 72 65 61 64 20 61 6e 64 20 77 72 69 74  st read and writ
1329c 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79 74  e it back to byt
1329d 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63  e 24. */.      c
1329e 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20  hange_counter = 
1329f 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28  sqlite3Get4byte(
132a0 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 46  (u8*)pPager->dbF
132a1 69 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 20 20  ileVers);.      
132a2 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b  change_counter++
132a3 3b 0a 20 20 20 20 20 20 70 75 74 33 32 62 69 74  ;.      put32bit
132a4 73 28 28 28 63 68 61 72 2a 29 70 50 67 48 64 72  s(((char*)pPgHdr
132a5 2d 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68 61  ->pData)+24, cha
132a6 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20  nge_counter);.. 
132a7 20 20 20 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69       /* If runni
132a8 6e 67 20 69 6e 20 64 69 72 65 63 74 20 6d 6f 64  ng in direct mod
132a9 65 2c 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e  e, write the con
132aa 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 31 20  tents of page 1 
132ab 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a  to the file. */.
132ac 20 20 20 20 20 20 69 66 28 20 69 73 44 69 72 65        if( isDire
132ad 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  ct ){.        co
132ae 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d  nst void *zBuf =
132af 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a   pPgHdr->pData;.
132b0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
132b1 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69  pPager->dbFileSi
132b2 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20  ze>0 );.        
132b3 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
132b4 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ite(pPager->fd, 
132b5 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61  zBuf, pPager->pa
132b6 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20  geSize, 0);.    
132b7 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66    }..      /* If
132b8 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b   everything work
132b9 65 64 2c 20 73 65 74 20 74 68 65 20 63 68 61 6e  ed, set the chan
132ba 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67  geCountDone flag
132bb 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72  . */.      if( r
132bc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
132bd 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
132be 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20  changeCountDone 
132bf 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
132c0 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61   }..    /* Relea
132c1 73 65 20 74 68 65 20 70 61 67 65 20 72 65 66 65  se the page refe
132c2 72 65 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71  rence. */.    sq
132c3 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
132c4 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72  pPgHdr);.  }.  r
132c5 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
132c6 2a 2a 20 53 79 6e 63 20 74 68 65 20 70 61 67 65  ** Sync the page
132c7 72 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20  r file to disk. 
132c8 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20  This is a no-op 
132c9 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69  for in-memory fi
132ca 6c 65 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20  les.** or pages 
132cb 77 69 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e  with the Pager.n
132cc 6f 53 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a  oSync flag set..
132cd 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  **.** If success
132ce 66 75 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20 6f  ful, or called o
132cf 6e 20 61 20 70 61 67 65 72 20 66 6f 72 20 77 68  n a pager for wh
132d0 69 63 68 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f  ich it is a no-o
132d1 70 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74  p, this.** funct
132d2 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ion returns SQLI
132d3 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65  TE_OK. Otherwise
132d4 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f  , an IO error co
132d5 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
132d6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
132d7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
132d8 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50  erSync(Pager *pP
132d9 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b  ager){.  int rc;
132da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
132dc 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
132dd 20 20 69 66 28 20 4d 45 4d 44 42 20 7c 7c 20 70    if( MEMDB || p
132de 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b  Pager->noSync ){
132df 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
132e0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
132e1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
132e2 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c  Sync(pPager->fd,
132e3 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
132e4 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ags);.  }.  retu
132e5 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
132e6 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73  Sync the databas
132e7 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 70  e file for the p
132e8 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a 4d 61  ager pPager. zMa
132e9 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74  ster points to t
132ea 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20  he name.** of a 
132eb 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
132ec 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ile that should 
132ed 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  be written into 
132ee 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a  the individual.*
132ef 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  * journal file. 
132f0 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65 20 4e  zMaster may be N
132f1 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 69 6e  ULL, which is in
132f2 74 65 72 70 72 65 74 65 64 20 61 73 20 6e 6f 20  terpreted as no 
132f3 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61  master.** journa
132f4 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61 74 61  l (a single data
132f5 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  base transaction
132f6 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  )..**.** This ro
132f7 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68  utine ensures th
132f8 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68  at:.**.**   * Th
132f9 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
132fa 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69  change-counter i
132fb 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20 20 20  s updated,.**   
132fc 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  * the journal is
132fd 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 73 73 20   synced (unless 
132fe 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65  the atomic-write
132ff 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
13300 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 2a 20 61   used),.**   * a
13301 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 61  ll dirty pages a
13302 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  re written to th
13303 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
13304 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74   .**   * the dat
13305 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 74 72  abase file is tr
13306 75 6e 63 61 74 65 64 20 28 69 66 20 72 65 71 75  uncated (if requ
13307 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a 20 20 20  ired), and.**   
13308 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
13309 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a  ile synced. .**.
1330a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e  ** The only thin
1330b 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74  g that remains t
1330c 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61  o commit the tra
1330d 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f 20 66  nsaction is to f
1330e 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 64 65 6c  inalize .** (del
1330f 65 74 65 2c 20 74 72 75 6e 63 61 74 65 20 6f 72  ete, truncate or
13310 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73 74 20   zero the first 
13311 70 61 72 74 20 6f 66 29 20 74 68 65 20 6a 6f 75  part of) the jou
13312 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 0a 2a  rnal file (or .*
13313 2a 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73  * delete the mas
13314 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
13315 20 69 66 20 73 70 65 63 69 66 69 65 64 29 2e 0a   if specified)..
13316 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
13317 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 4c 4c  if zMaster==NULL
13318 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 20  , this does not 
13319 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 65 76  overwrite a prev
1331a 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 70 61  ious value.** pa
1331b 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c 69 74  ssed to an sqlit
1331c 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
1331d 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a 2a 2a  seOne() call..**
1331e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e 61 6c  .** If the final
1331f 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f 53   parameter - noS
13320 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c 20 74  ync - is true, t
13321 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
13322 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a 20   file itself.** 
13323 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20 54  is not synced. T
13324 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63  he caller must c
13325 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72  all sqlite3Pager
13326 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c 79 20  Sync() directly 
13327 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20 64  to.** sync the d
13328 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 66  atabase file bef
13329 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d  ore calling Comm
1332a 69 74 50 68 61 73 65 54 77 6f 28 29 20 74 6f 20  itPhaseTwo() to 
1332b 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a 6f  delete the.** jo
1332c 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74 68  urnal file in th
1332d 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49  is case..*/.SQLI
1332e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1332f 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
13330 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50 61 67  tPhaseOne(.  Pag
13331 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20  er *pPager,     
13332 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13333 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  Pager object */.
13334 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d    const char *zM
13335 61 73 74 65 72 2c 20 20 20 20 20 20 20 20 20 20  aster,          
13336 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c    /* If not NULL
13337 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  , the master jou
13338 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69  rnal name */.  i
13339 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 20 20 20  nt noSync       
1333a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1333b 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 74  * True to omit t
1333c 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 68 65 20  he xSync on the 
1333d 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20  db file */.){.  
1333e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1333f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
13340 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
13341 2f 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  /..  if( pPager-
13342 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20  >errCode ){.    
13343 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65  return pPager->e
13344 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 50  rrCode;.  }..  P
13345 41 47 45 52 54 52 41 43 45 28 28 22 44 41 54 41  AGERTRACE(("DATA
13346 42 41 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d  BASE SYNC: File=
13347 25 73 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 53  %s zMaster=%s nS
13348 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20  ize=%d\n", .    
13349 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e    pPager->zFilen
1334a 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50  ame, zMaster, pP
1334b 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 29 3b 0a  ager->dbSize));.
1334c 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
1334d 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62   an in-memory db
1334e 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61  , or no pages ha
1334f 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  ve been written 
13350 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20 2a 2a  to, or this.  **
13351 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c   function has al
13352 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65  ready been calle
13353 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70  d, it is a no-op
13354 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d  ..  */.  if( MEM
13355 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62  DB && pPager->db
13356 4d 6f 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20  Modified ){.    
13357 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73  sqlite3BackupRes
13358 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61  tart(pPager->pBa
13359 63 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69  ckup);.  }else i
1335a 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
1335b 21 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26  !=PAGER_SYNCED &
1335c 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69  & pPager->dbModi
1335d 66 69 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20  fied ){..    /* 
1335e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  The following bl
1335f 6f 63 6b 20 75 70 64 61 74 65 73 20 74 68 65 20  ock updates the 
13360 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20  change-counter. 
13361 45 78 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20  Exactly how it. 
13362 20 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20     ** does this 
13363 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68  depends on wheth
13364 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74  er or not the at
13365 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69  omic-update opti
13366 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20  mization.    ** 
13367 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63  was enabled at c
13368 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64  ompile time, and
13369 20 69 66 20 74 68 69 73 20 74 72 61 6e 73 61 63   if this transac
1336a 74 69 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a  tion meets the .
1336b 20 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63      ** runtime c
1336c 72 69 74 65 72 69 61 20 74 6f 20 75 73 65 20 74  riteria to use t
1336d 68 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20  he operation: . 
1336e 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
1336f 2a 20 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65  * The file-syste
13370 6d 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 61  m supports the a
13371 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70  tomic-write prop
13372 65 72 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20  erty for.    ** 
13373 20 20 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73       blocks of s
13374 69 7a 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61  ize page-size, a
13375 6e 64 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20  nd .    **    * 
13376 54 68 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e  This commit is n
13377 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c  ot part of a mul
13378 74 69 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74  ti-file transact
13379 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20  ion, and.    ** 
1337a 20 20 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65     * Exactly one
1337b 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d   page has been m
1337c 6f 64 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72  odified and stor
1337d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e in the journal
1337e 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20   file..    **.  
1337f 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69    ** If the opti
13380 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74  mization was not
13381 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70   enabled at comp
13382 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74  ile time, then t
13383 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f  he.    ** pager_
13384 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74  incr_changecount
13385 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  er() function is
13386 20 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74   called to updat
13387 65 20 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20  e the change.   
13388 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27   ** counter in '
13389 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20  indirect-mode'. 
1338a 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74  If the optimizat
1338b 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ion is compiled 
1338c 69 6e 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73  in but.    ** is
1338d 20 6e 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20   not applicable 
1338e 74 6f 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  to this transact
1338f 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65  ion, call sqlite
13390 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29  3JournalCreate()
13391 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20  .    ** to make 
13392 73 75 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  sure the journal
13393 20 66 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c   file has actual
13394 6c 79 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c  ly been created,
13395 20 74 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a   then call.    *
13396 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61  * pager_incr_cha
13397 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20  ngecounter() to 
13398 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
13399 65 2d 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64  e-counter in ind
1339a 69 72 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64  irect.    ** mod
1339b 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  e. .    **.    *
1339c 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  * Otherwise, if 
1339d 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
1339e 20 69 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64   is both enabled
1339f 20 61 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c   and applicable,
133a0 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c  .    ** then cal
133a1 6c 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61  l pager_incr_cha
133a2 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20  ngecounter() to 
133a3 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
133a4 65 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a  e-counter.    **
133a5 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64   in 'direct' mod
133a6 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
133a7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
133a8 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20   will never be. 
133a9 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f     ** created fo
133aa 72 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  r this transacti
133ab 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65  on..    */.#ifde
133ac 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
133ad 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20  ATOMIC_WRITE.   
133ae 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20   PgHdr *pPg;.   
133af 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
133b0 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20  pPager->jfd) || 
133b1 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
133b2 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
133b3 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20  ALMODE_OFF );.  
133b4 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26    if( !zMaster &
133b5 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  & isOpen(pPager-
133b6 3e 6a 66 64 29 20 0a 20 20 20 20 20 26 26 20 70  >jfd) .     && p
133b7 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
133b8 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a  f==jrnlBufferSiz
133b9 65 28 70 50 61 67 65 72 29 20 0a 20 20 20 20 20  e(pPager) .     
133ba 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  && pPager->dbSiz
133bb 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  e>=pPager->dbFil
133bc 65 53 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30  eSize.     && (0
133bd 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33  ==(pPg = sqlite3
133be 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28  PcacheDirtyList(
133bf 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
133c0 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69  ) || 0==pPg->pDi
133c1 72 74 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  rty).    ){.    
133c2 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
133c3 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63  db file change c
133c4 6f 75 6e 74 65 72 20 76 69 61 20 74 68 65 20 64  ounter via the d
133c5 69 72 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68  irect-write meth
133c6 6f 64 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a  od. The .      *
133c7 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c  * following call
133c8 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65   will modify the
133c9 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65   in-memory repre
133ca 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67  sentation of pag
133cb 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f  e 1 .      ** to
133cc 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64   include the upd
133cd 61 74 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e  ated change coun
133ce 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69  ter and then wri
133cf 74 65 20 70 61 67 65 20 31 20 0a 20 20 20 20 20  te page 1 .     
133d0 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20   ** directly to 
133d1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
133d2 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 68  e. Because of th
133d3 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a  e atomic-write .
133d4 20 20 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74        ** propert
133d5 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69  y of the host fi
133d6 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20  le-system, this 
133d7 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a  is safe..      *
133d8 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  /.      rc = pag
133d9 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f  er_incr_changeco
133da 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 31 29  unter(pPager, 1)
133db 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
133dc 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a     rc = sqlite3J
133dd 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61  ournalCreate(pPa
133de 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20  ger->jfd);.     
133df 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
133e0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
133e1 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68   = pager_incr_ch
133e2 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67  angecounter(pPag
133e3 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  er, 0);.      }.
133e4 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
133e5 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f  rc = pager_incr_
133e6 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50  changecounter(pP
133e7 61 67 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66  ager, 0);.#endif
133e8 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
133e9 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f  ITE_OK ) goto co
133ea 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65  mmit_phase_one_e
133eb 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  xit;..    /* If 
133ec 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
133ed 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61   has made the da
133ee 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20  tabase smaller, 
133ef 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20  then all pages. 
133f0 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63     ** being disc
133f1 61 72 64 65 64 20 62 79 20 74 68 65 20 74 72 75  arded by the tru
133f2 6e 63 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  ncation must be 
133f3 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
133f4 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69  ournal.    ** fi
133f5 6c 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c  le. This can onl
133f6 79 20 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f  y happen in auto
133f7 2d 76 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20  -vacuum mode..  
133f8 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f    **.    ** Befo
133f9 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 70  re reading the p
133fa 61 67 65 73 20 77 69 74 68 20 70 61 67 65 20 6e  ages with page n
133fb 75 6d 62 65 72 73 20 6c 61 72 67 65 72 20 74 68  umbers larger th
133fc 61 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  an the .    ** c
133fd 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
133fe 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65  Pager.dbSize, se
133ff 74 20 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f  t dbSize back to
13400 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a   the value.    *
13401 2a 20 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61  * that it took a
13402 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
13403 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  he transaction. 
13404 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20  Otherwise, the. 
13405 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73     ** calls to s
13406 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
13407 20 72 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70   return zeroed p
13408 61 67 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20  ages instead of 
13409 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20  .    ** reading 
1340a 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61  data from the da
1340b 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20  tabase file..   
1340c 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
1340d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1340e 55 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  UM.    if( pPage
1340f 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 72  r->dbSize<pPager
13410 2d 3e 64 62 4f 72 69 67 53 69 7a 65 0a 20 20 20  ->dbOrigSize.   
13411 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75    && pPager->jou
13412 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f  rnalMode!=PAGER_
13413 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20  JOURNALMODE_OFF 
13414 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 50 67  .    ){.      Pg
13415 6e 6f 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  no i;           
13416 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13417 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
13418 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  tor variable */.
13419 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f        const Pgno
1341a 20 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d   iSkip = PAGER_M
1341b 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b 20  J_PGNO(pPager); 
1341c 2f 2a 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b 20  /* Pending lock 
1341d 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f  page */.      co
1341e 6e 73 74 20 50 67 6e 6f 20 64 62 53 69 7a 65 20  nst Pgno dbSize 
1341f 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  = pPager->dbSize
13420 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62  ;       /* Datab
13421 61 73 65 20 69 6d 61 67 65 20 73 69 7a 65 20 2a  ase image size *
13422 2f 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  / .      pPager-
13423 3e 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72  >dbSize = pPager
13424 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20  ->dbOrigSize;.  
13425 20 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69 7a      for( i=dbSiz
13426 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d 3e  e+1; i<=pPager->
13427 64 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b 20  dbOrigSize; i++ 
13428 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ){.        if( !
13429 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
1342a 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  t(pPager->pInJou
1342b 72 6e 61 6c 2c 20 69 29 20 26 26 20 69 21 3d 69  rnal, i) && i!=i
1342c 53 6b 69 70 20 29 7b 0a 20 20 20 20 20 20 20 20  Skip ){.        
1342d 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b 20    PgHdr *pPage; 
1342e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1342f 61 67 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a  age to journal *
13430 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  /.          rc =
13431 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
13432 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 61  (pPager, i, &pPa
13433 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ge);.          i
13434 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13435 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70   ) goto commit_p
13436 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20  hase_one_exit;. 
13437 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
13438 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
13439 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  pPage);.        
1343a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
1343b 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ref(pPage);.    
1343c 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1343d 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63  LITE_OK ) goto c
1343e 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f  ommit_phase_one_
1343f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  exit;.        }.
13440 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 70        } .      p
13441 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
13442 64 62 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23 65  dbSize;.    }.#e
13443 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72 69  ndif..    /* Wri
13444 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
13445 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20  urnal name into 
13446 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13447 2e 20 49 66 20 61 20 6d 61 73 74 65 72 20 0a 20  . If a master . 
13448 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69     ** journal fi
13449 6c 65 20 6e 61 6d 65 20 68 61 73 20 61 6c 72 65  le name has alre
1344a 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ady been written
1344b 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
1344c 66 69 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f 72  file, .    ** or
1344d 20 69 66 20 7a 4d 61 73 74 65 72 20 69 73 20 4e   if zMaster is N
1344e 55 4c 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20 6a  ULL (no master j
1344f 6f 75 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74 68  ournal), then th
13450 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
13451 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  op..    */.    r
13452 63 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72 4a  c = writeMasterJ
13453 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a  ournal(pPager, z
13454 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28  Master);.    if(
13455 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13456 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61   goto commit_pha
13457 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20  se_one_exit;..  
13458 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f    /* Sync the jo
13459 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74  urnal file. If t
1345a 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 65  he atomic-update
1345b 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
1345c 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 73   being.    ** us
1345d 65 64 2c 20 74 68 69 73 20 63 61 6c 6c 20 77 69  ed, this call wi
1345e 6c 6c 20 6e 6f 74 20 63 72 65 61 74 65 20 74 68  ll not create th
1345f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  e journal file o
13460 72 20 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20 20  r perform any.  
13461 20 20 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20 20    ** real IO..  
13462 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 79    */.    rc = sy
13463 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  ncJournal(pPager
13464 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
13465 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
13466 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65  commit_phase_one
13467 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 57  _exit;..    /* W
13468 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70  rite all dirty p
13469 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61  ages to the data
1346a 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20  base file. */.  
1346b 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69    rc = pager_wri
1346c 74 65 5f 70 61 67 65 6c 69 73 74 28 73 71 6c 69  te_pagelist(sqli
1346d 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69  te3PcacheDirtyLi
1346e 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  st(pPager->pPCac
1346f 68 65 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63  he));.    if( rc
13470 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
13471 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21       assert( rc!
13472 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c  =SQLITE_IOERR_BL
13473 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 67  OCKED );.      g
13474 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65  oto commit_phase
13475 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d  _one_exit;.    }
13476 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
13477 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65  heCleanAll(pPage
13478 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20  r->pPCache);..  
13479 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65    /* If the file
1347a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 20   on disk is not 
1347b 74 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73  the same size as
1347c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d   the database im
1347d 61 67 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e  age,.    ** then
1347e 20 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e 63   use pager_trunc
1347f 61 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 73  ate to grow or s
13480 68 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 68  hrink the file h
13481 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ere..    */.    
13482 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  if( pPager->dbSi
13483 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64 62 46 69  ze!=pPager->dbFi
13484 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  leSize ){.      
13485 50 67 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61 67  Pgno nNew = pPag
13486 65 72 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70 50  er->dbSize - (pP
13487 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 41  ager->dbSize==PA
13488 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
13489 65 72 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65  er));.      asse
1348a 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
1348b 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49  e>=PAGER_EXCLUSI
1348c 56 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  VE );.      rc =
1348d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28   pager_truncate(
1348e 70 50 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a 20  pPager, nNew);. 
1348f 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13490 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f  ITE_OK ) goto co
13491 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65  mmit_phase_one_e
13492 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  xit;.    }..    
13493 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e 63  /* Finally, sync
13494 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13495 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21  le. */.    if( !
13496 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 26  pPager->noSync &
13497 26 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20  & !noSync ){.   
13498 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
13499 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64  sSync(pPager->fd
1349a 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66  , pPager->sync_f
1349b 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20  lags);.    }.   
1349c 20 49 4f 54 52 41 43 45 28 28 22 44 42 53 59 4e   IOTRACE(("DBSYN
1349d 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29  C %p\n", pPager)
1349e 29 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  )..    pPager->s
1349f 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59 4e  tate = PAGER_SYN
134a0 43 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69 74  CED;.  }..commit
134a1 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3a  _phase_one_exit:
134a2 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
134a3 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20  E_IOERR_BLOCKED 
134a4 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f  ){.    /* pager_
134a5 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74  incr_changecount
134a6 65 72 28 29 20 6d 61 79 20 61 74 74 65 6d 70 74  er() may attempt
134a7 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 20 65 78   to obtain an ex
134a8 63 6c 75 73 69 76 65 0a 20 20 20 20 2a 2a 20 6c  clusive.    ** l
134a9 6f 63 6b 20 74 6f 20 73 70 69 6c 6c 20 74 68 65  ock to spill the
134aa 20 63 61 63 68 65 20 61 6e 64 20 72 65 74 75 72   cache and retur
134ab 6e 20 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 2e  n IOERR_BLOCKED.
134ac 20 42 75 74 20 73 69 6e 63 65 20 0a 20 20 20 20   But since .    
134ad 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63  ** there is no c
134ae 68 61 6e 63 65 20 74 68 65 20 63 61 63 68 65 20  hance the cache 
134af 69 73 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2c  is inconsistent,
134b0 20 69 74 20 69 73 0a 20 20 20 20 2a 2a 20 62 65   it is.    ** be
134b1 74 74 65 72 20 74 6f 20 72 65 74 75 72 6e 20 53  tter to return S
134b2 51 4c 49 54 45 5f 42 55 53 59 2e 0a 20 20 20 20  QLITE_BUSY..    
134b3 2a 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  **/.    rc = SQL
134b4 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
134b5 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
134b6 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66  *.** When this f
134b7 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
134b8 64 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  d, the database 
134b9 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f  file has been co
134ba 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 75 70 64 61  mpletely.** upda
134bb 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74 20 74  ted to reflect t
134bc 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20  he changes made 
134bd 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  by the current t
134be 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 0a 2a  ransaction and.*
134bf 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b  * synced to disk
134c0 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  . The journal fi
134c1 6c 65 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20  le still exists 
134c2 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  in the file-syst
134c3 65 6d 20 0a 2a 2a 20 74 68 6f 75 67 68 2c 20 61  em .** though, a
134c4 6e 64 20 69 66 20 61 20 66 61 69 6c 75 72 65 20  nd if a failure 
134c5 6f 63 63 75 72 73 20 61 74 20 74 68 69 73 20 70  occurs at this p
134c6 6f 69 6e 74 20 69 74 20 77 69 6c 6c 20 65 76 65  oint it will eve
134c7 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 75 73  ntually.** be us
134c8 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72  ed as a hot-jour
134c9 6e 61 6c 20 61 6e 64 20 74 68 65 20 63 75 72 72  nal and the curr
134ca 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
134cb 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a  rolled back..**.
134cc 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
134cd 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a   finalizes the j
134ce 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 65 69 74  ournal file, eit
134cf 68 65 72 20 62 79 20 64 65 6c 65 74 69 6e 67 2c  her by deleting,
134d0 20 0a 2a 2a 20 74 72 75 6e 63 61 74 69 6e 67 20   .** truncating 
134d1 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 7a 65 72  or partially zer
134d2 6f 69 6e 67 20 69 74 2c 20 73 6f 20 74 68 61 74  oing it, so that
134d3 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 75 73   it cannot be us
134d4 65 64 20 0a 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a  ed .** for hot-j
134d5 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e  ournal rollback.
134d6 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 64 6f   Once this is do
134d7 6e 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ne the transacti
134d8 6f 6e 20 69 73 0a 2a 2a 20 69 72 72 65 76 6f 63  on is.** irrevoc
134d9 61 62 6c 79 20 63 6f 6d 6d 69 74 74 65 64 2e 0a  ably committed..
134da 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
134db 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20  r occurs, an IO 
134dc 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
134dd 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70  turned and the p
134de 61 67 65 72 0a 2a 2a 20 6d 6f 76 65 73 20 69 6e  ager.** moves in
134df 74 6f 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  to the error sta
134e0 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53  te. Otherwise, S
134e1 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
134e2 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
134e3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
134e4 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68  te3PagerCommitPh
134e5 61 73 65 54 77 6f 28 50 61 67 65 72 20 2a 70 50  aseTwo(Pager *pP
134e6 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  ager){.  int rc 
134e7 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
134e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
134e9 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
134ea 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 70 72 6f  .  /* Do not pro
134eb 63 65 65 64 20 69 66 20 74 68 65 20 70 61 67 65  ceed if the page
134ec 72 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  r is already in 
134ed 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e  the error state.
134ee 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72   */.  if( pPager
134ef 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20  ->errCode ){.   
134f0 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
134f1 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20  errCode;.  }..  
134f2 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
134f3 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63   should not be c
134f4 61 6c 6c 65 64 20 69 66 20 74 68 65 20 70 61 67  alled if the pag
134f5 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 74 20  er is not in at 
134f6 6c 65 61 73 74 0a 20 20 2a 2a 20 50 41 47 45 52  least.  ** PAGER
134f7 5f 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2e  _RESERVED state.
134f8 20 41 6e 64 20 69 6e 64 65 65 64 20 53 51 4c 69   And indeed SQLi
134f9 74 65 20 6e 65 76 65 72 20 64 6f 65 73 20 74 68  te never does th
134fa 69 73 2e 20 42 75 74 20 69 74 20 69 73 0a 20 20  is. But it is.  
134fb 2a 2a 20 6e 69 63 65 20 74 6f 20 68 61 76 65 20  ** nice to have 
134fc 74 68 69 73 20 64 65 66 65 6e 73 69 76 65 20 62  this defensive b
134fd 6c 6f 63 6b 20 68 65 72 65 20 61 6e 79 77 61 79  lock here anyway
134fe 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56  ..  */.  if( NEV
134ff 45 52 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65  ER(pPager->state
13500 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 29  <PAGER_RESERVED)
13501 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
13502 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
13503 0a 0a 20 20 2f 2a 20 41 6e 20 6f 70 74 69 6d 69  ..  /* An optimi
13504 7a 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 64  zation. If the d
13505 61 74 61 62 61 73 65 20 77 61 73 20 6e 6f 74 20  atabase was not 
13506 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 69 65  actually modifie
13507 64 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 74 68  d during.  ** th
13508 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  is transaction, 
13509 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 6e  the pager is run
1350a 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76  ning in exclusiv
1350b 65 2d 6d 6f 64 65 20 61 6e 64 20 69 73 0a 20 20  e-mode and is.  
1350c 2a 2a 20 75 73 69 6e 67 20 70 65 72 73 69 73 74  ** using persist
1350d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68  ent journals, th
1350e 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
1350f 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a   is a no-op..  *
13510 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 72 74  *.  ** The start
13511 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
13512 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63  file currently c
13513 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65  ontains a single
13514 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68   journal .  ** h
13515 65 61 64 65 72 20 77 69 74 68 20 74 68 65 20 6e  eader with the n
13516 52 65 63 20 66 69 65 6c 64 20 73 65 74 20 74 6f  Rec field set to
13517 20 30 2e 20 49 66 20 73 75 63 68 20 61 20 6a 6f   0. If such a jo
13518 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 61 73  urnal is used as
13519 0a 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72  .  ** a hot-jour
1351a 6e 61 6c 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a  nal during hot-j
1351b 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c  ournal rollback,
1351c 20 30 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20   0 changes will 
1351d 62 65 20 6d 61 64 65 0a 20 20 2a 2a 20 74 6f 20  be made.  ** to 
1351e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1351f 65 2e 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e  e. So there is n
13520 6f 20 6e 65 65 64 20 74 6f 20 7a 65 72 6f 20 74  o need to zero t
13521 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a  he journal .  **
13522 20 68 65 61 64 65 72 2e 20 53 69 6e 63 65 20 74   header. Since t
13523 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65  he pager is in e
13524 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74  xclusive mode, t
13525 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a  here is no need.
13526 20 20 2a 2a 20 74 6f 20 64 72 6f 70 20 61 6e 79    ** to drop any
13527 20 6c 6f 63 6b 73 20 65 69 74 68 65 72 2e 0a 20   locks either.. 
13528 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72   */.  if( pPager
13529 2d 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20  ->dbModified==0 
1352a 26 26 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75  && pPager->exclu
1352b 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20  siveMode .   && 
1352c 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
1352d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
1352e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20  ALMODE_PERSIST. 
1352f 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
13530 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
13531 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ff==JOURNAL_HDR_
13532 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  SZ(pPager) );.  
13533 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13534 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52  OK;.  }..  PAGER
13535 54 52 41 43 45 28 28 22 43 4f 4d 4d 49 54 20 25  TRACE(("COMMIT %
13536 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
13537 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72  ager)));.  asser
13538 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
13539 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c  ==PAGER_SYNCED |
1353a 7c 20 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67  | MEMDB || !pPag
1353b 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29  er->dbModified )
1353c 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65  ;.  rc = pager_e
1353d 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70  nd_transaction(p
1353e 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73  Pager, pPager->s
1353f 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74  etMaster);.  ret
13540 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28  urn pager_error(
13541 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a  pPager, rc);.}..
13542 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61  /*.** Rollback a
13543 6c 6c 20 63 68 61 6e 67 65 73 2e 20 54 68 65 20  ll changes. The 
13544 64 61 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62  database falls b
13545 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41  ack to PAGER_SHA
13546 52 45 44 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  RED mode..**.** 
13547 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65  This function pe
13548 72 66 6f 72 6d 73 20 74 77 6f 20 74 61 73 6b 73  rforms two tasks
13549 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20  :.**.**   1) It 
1354a 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 20 6a  rolls back the j
1354b 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73  ournal file, res
1354c 74 6f 72 69 6e 67 20 61 6c 6c 20 64 61 74 61 62  toring all datab
1354d 61 73 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a  ase file and .**
1354e 20 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20        in-memory 
1354f 63 61 63 68 65 20 70 61 67 65 73 20 74 6f 20 74  cache pages to t
13550 68 65 20 73 74 61 74 65 20 74 68 65 79 20 77 65  he state they we
13551 72 65 20 69 6e 20 77 68 65 6e 20 74 68 65 20 74  re in when the t
13552 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20  ransaction.**   
13553 20 20 20 77 61 73 20 6f 70 65 6e 65 64 2c 20 61     was opened, a
13554 6e 64 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69  nd.**   2) It fi
13555 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72  nalizes the jour
13556 6e 61 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61  nal file, so tha
13557 74 20 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64  t it is not used
13558 20 66 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20   for hot.**     
13559 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79   rollback at any
1355a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75   point in the fu
1355b 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a  ture..**.** subj
1355c 65 63 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  ect to the follo
1355d 77 69 6e 67 20 71 75 61 6c 69 66 69 63 61 74 69  wing qualificati
1355e 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20  ons:.**.** * If 
1355f 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13560 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e   is not yet open
13561 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
13562 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a  ion is called,.*
13563 2a 20 20 20 74 68 65 6e 20 6f 6e 6c 79 20 28 32  *   then only (2
13564 29 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20  ) is performed. 
13565 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
13566 72 65 20 69 73 20 6e 6f 20 6a 6f 75 72 6e 61 6c  re is no journal
13567 20 66 69 6c 65 0a 2a 2a 20 20 20 74 6f 20 72 6f   file.**   to ro
13568 6c 6c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a  ll back..**.** *
13569 20 49 66 20 69 6e 20 61 6e 20 65 72 72 6f 72 20   If in an error 
1356a 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e  state other than
1356b 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 74 68   SQLITE_FULL, th
1356c 65 6e 20 74 61 73 6b 20 28 31 29 20 69 73 20 0a  en task (1) is .
1356d 2a 2a 20 20 20 70 65 72 66 6f 72 6d 65 64 2e 20  **   performed. 
1356e 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74  If successful, t
1356f 61 73 6b 20 28 32 29 2e 20 52 65 67 61 72 64 6c  ask (2). Regardl
13570 65 73 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f  ess of the outco
13571 6d 65 0a 2a 2a 20 20 20 6f 66 20 65 69 74 68 65  me.**   of eithe
13572 72 2c 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  r, the error sta
13573 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  te error code is
13574 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
13575 20 63 61 6c 6c 65 72 0a 2a 2a 20 20 20 28 69 2e   caller.**   (i.
13576 65 2e 20 65 69 74 68 65 72 20 53 51 4c 49 54 45  e. either SQLITE
13577 5f 49 4f 45 52 52 20 6f 72 20 53 51 4c 49 54 45  _IOERR or SQLITE
13578 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a  _CORRUPT)..**.**
13579 20 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20   * If the pager 
1357a 69 73 20 69 6e 20 50 41 47 45 52 5f 52 45 53 45  is in PAGER_RESE
1357b 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e  RVED state, then
1357c 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20 57 68   attempt (1). Wh
1357d 65 74 68 65 72 0a 2a 2a 20 20 20 6f 72 20 6e 6f  ether.**   or no
1357e 74 20 28 31 29 20 69 73 20 73 75 63 63 75 73 73  t (1) is succuss
1357f 66 75 6c 2c 20 61 6c 73 6f 20 61 74 74 65 6d 70  ful, also attemp
13580 74 20 28 32 29 2e 20 49 66 20 73 75 63 63 65 73  t (2). If succes
13581 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20  sful, return.** 
13582 20 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68    SQLITE_OK. Oth
13583 65 72 77 69 73 65 2c 20 65 6e 74 65 72 20 74 68  erwise, enter th
13584 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 61 6e  e error state an
13585 64 20 72 65 74 75 72 6e 20 74 68 65 20 66 69 72  d return the fir
13586 73 74 20 0a 2a 2a 20 20 20 65 72 72 6f 72 20 63  st .**   error c
13587 6f 64 65 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e  ode encountered.
13588 20 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69   .**.**   In thi
13589 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20  s case there is 
1358a 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20 74  no chance that t
1358b 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20  he database was 
1358c 77 72 69 74 74 65 6e 20 74 6f 2e 20 0a 2a 2a 20  written to. .** 
1358d 20 20 53 6f 20 69 73 20 73 61 66 65 20 74 6f 20    So is safe to 
1358e 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75  finalize the jou
1358f 72 6e 61 6c 20 66 69 6c 65 20 65 76 65 6e 20 69  rnal file even i
13590 66 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 0a  f the playback .
13591 2a 2a 20 20 20 28 6f 70 65 72 61 74 69 6f 6e 20  **   (operation 
13592 31 29 20 66 61 69 6c 65 64 2e 20 48 6f 77 65 76  1) failed. Howev
13593 65 72 20 74 68 65 20 70 61 67 65 72 20 6d 75 73  er the pager mus
13594 74 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f  t enter the erro
13595 72 20 73 74 61 74 65 0a 2a 2a 20 20 20 61 73 20  r state.**   as 
13596 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
13597 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61  the in-memory ca
13598 63 68 65 20 61 72 65 20 6e 6f 77 20 73 75 73 70  che are now susp
13599 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 69 6e  ect..**.** * Fin
1359a 61 6c 6c 79 2c 20 69 66 20 69 6e 20 50 41 47 45  ally, if in PAGE
1359b 52 5f 45 58 43 4c 55 53 49 56 45 20 73 74 61 74  R_EXCLUSIVE stat
1359c 65 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20  e, then attempt 
1359d 28 31 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 20 61  (1). Only.**   a
1359e 74 74 65 6d 70 74 20 28 32 29 20 69 66 20 28 31  ttempt (2) if (1
1359f 29 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e  ) is successful.
135a0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
135a1 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c  K if successful,
135a2 0a 2a 2a 20 20 20 6f 74 68 65 72 77 69 73 65 20  .**   otherwise 
135a3 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20  enter the error 
135a4 73 74 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e  state and return
135a5 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
135a6 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 20 20 66  from the .**   f
135a7 61 69 6c 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ailing operation
135a8 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69  ..**.**   In thi
135a9 73 20 63 61 73 65 20 74 68 65 20 64 61 74 61 62  s case the datab
135aa 61 73 65 20 66 69 6c 65 20 6d 61 79 20 68 61 76  ase file may hav
135ab 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  e been written t
135ac 6f 2e 20 53 6f 20 69 66 20 74 68 65 0a 2a 2a 20  o. So if the.** 
135ad 20 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61    playback opera
135ae 74 69 6f 6e 20 64 69 64 20 6e 6f 74 20 73 75 63  tion did not suc
135af 63 65 65 64 20 69 74 20 77 6f 75 6c 64 20 6e 6f  ceed it would no
135b0 74 20 62 65 20 73 61 66 65 20 74 6f 20 66 69 6e  t be safe to fin
135b1 61 6c 69 7a 65 0a 2a 2a 20 20 20 74 68 65 20 6a  alize.**   the j
135b2 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74 20  ournal file. It 
135b3 6e 65 65 64 73 20 74 6f 20 62 65 20 6c 65 66 74  needs to be left
135b4 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73   in the file-sys
135b5 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20  tem so that.**  
135b6 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
135b7 65 73 73 20 63 61 6e 20 75 73 65 20 69 74 20 74  ess can use it t
135b8 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61  o restore the da
135b9 74 61 62 61 73 65 20 73 74 61 74 65 20 28 62 79  tabase state (by
135ba 0a 2a 2a 20 20 20 68 6f 74 2d 6a 6f 75 72 6e 61  .**   hot-journa
135bb 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 2a 2f 0a  l rollback)..*/.
135bc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
135bd 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
135be 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70  ollback(Pager *p
135bf 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  Pager){.  int rc
135c0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
135c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
135c2 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
135c3 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22  .  PAGERTRACE(("
135c4 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20  ROLLBACK %d\n", 
135c5 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29  PAGERID(pPager))
135c6 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72  );.  if( !pPager
135c7 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 7c 7c 20  ->dbModified || 
135c8 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
135c9 6a 66 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  jfd) ){.    rc =
135ca 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73   pager_end_trans
135cb 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70  action(pPager, p
135cc 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72  Pager->setMaster
135cd 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
135ce 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26  Pager->errCode &
135cf 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  & pPager->errCod
135d0 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29  e!=SQLITE_FULL )
135d1 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  {.    if( pPager
135d2 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45  ->state>=PAGER_E
135d3 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20  XCLUSIVE ){.    
135d4 20 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b    pager_playback
135d5 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20  (pPager, 0);.   
135d6 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67   }.    rc = pPag
135d7 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d  er->errCode;.  }
135d8 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 50  else{.    if( pP
135d9 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
135da 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20  ER_RESERVED ){. 
135db 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
135dc 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70      rc = pager_p
135dd 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20  layback(pPager, 
135de 30 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20  0);.      rc2 = 
135df 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61  pager_end_transa
135e0 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50  ction(pPager, pP
135e1 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29  ager->setMaster)
135e2 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
135e3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
135e4 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20       rc = rc2;. 
135e5 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
135e6 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  {.      rc = pag
135e7 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67  er_playback(pPag
135e8 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20  er, 0);.    }.. 
135e9 20 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b     if( !MEMDB ){
135ea 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64  .      pPager->d
135eb 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a  bSizeValid = 0;.
135ec 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
135ed 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
135ee 20 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41   during a ROLLBA
135ef 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f  CK, we can no lo
135f0 6e 67 65 72 20 74 72 75 73 74 20 74 68 65 20 70  nger trust the p
135f1 61 67 65 72 0a 20 20 20 20 2a 2a 20 63 61 63 68  ager.    ** cach
135f2 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 72  e. So call pager
135f3 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 20  _error() on the 
135f4 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20  way out to make 
135f5 61 6e 79 20 65 72 72 6f 72 20 0a 20 20 20 20 2a  any error .    *
135f6 2a 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20  * persistent..  
135f7 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61    */.    rc = pa
135f8 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
135f9 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74  , rc);.  }.  ret
135fa 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
135fb 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
135fc 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
135fd 65 20 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64  e is opened read
135fe 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46  -only.  Return F
135ff 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64  ALSE.** if the d
13600 61 74 61 62 61 73 65 20 69 73 20 28 69 6e 20 74  atabase is (in t
13601 68 65 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e  heory) writable.
13602 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13603 54 45 20 75 38 20 73 71 6c 69 74 65 33 50 61 67  TE u8 sqlite3Pag
13604 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67  erIsreadonly(Pag
13605 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
13606 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65  eturn pPager->re
13607 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  adOnly;.}../*.**
13608 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
13609 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
1360a 20 74 6f 20 74 68 65 20 70 61 67 65 72 2e 0a 2a   to the pager..*
1360b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1360c 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
1360d 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65 72 20  rRefcount(Pager 
1360e 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
1360f 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  rn sqlite3Pcache
13610 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
13611 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a  >pPCache);.}../*
13612 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
13613 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e  umber of referen
13614 63 65 73 20 74 6f 20 74 68 65 20 73 70 65 63 69  ces to the speci
13615 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51  fied page..*/.SQ
13616 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13617 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
13618 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65  eRefcount(DbPage
13619 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75   *pPage){.  retu
1361a 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  rn sqlite3Pcache
1361b 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61  PageRefcount(pPa
1361c 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  ge);.}..#ifdef S
1361d 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  QLITE_TEST./*.**
1361e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1361f 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
13620 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f  g and analysis o
13621 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly..*/.SQLITE_P
13622 52 49 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69  RIVATE int *sqli
13623 74 65 33 50 61 67 65 72 53 74 61 74 73 28 50 61  te3PagerStats(Pa
13624 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
13625 73 74 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d  static int a[11]
13626 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74  ;.  a[0] = sqlit
13627 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74  e3PcacheRefCount
13628 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
13629 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69  );.  a[1] = sqli
1362a 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75  te3PcachePagecou
1362b 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  nt(pPager->pPCac
1362c 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71  he);.  a[2] = sq
1362d 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61  lite3PcacheGetCa
1362e 63 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e  chesize(pPager->
1362f 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d  pPCache);.  a[3]
13630 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a   = pPager->dbSiz
13631 65 56 61 6c 69 64 20 3f 20 28 69 6e 74 29 20 70  eValid ? (int) p
13632 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3a 20  Pager->dbSize : 
13633 2d 31 3b 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61  -1;.  a[4] = pPa
13634 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b  ger->state;.  a[
13635 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72  5] = pPager->err
13636 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70  Code;.  a[6] = p
13637 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61  Pager->nHit;.  a
13638 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d  [7] = pPager->nM
13639 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b  iss;.  a[8] = 0;
1363a 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20    /* Used to be 
1363b 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f  pPager->nOvfl */
1363c 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72  .  a[9] = pPager
1363d 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d  ->nRead;.  a[10]
1363e 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57 72 69 74   = pPager->nWrit
1363f 65 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d  e;.  return a;.}
13640 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
13641 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
13642 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f  is is an in-memo
13643 72 79 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c  ry pager..*/.SQL
13644 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
13645 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65  sqlite3PagerIsMe
13646 6d 64 62 28 50 61 67 65 72 20 2a 70 50 61 67 65  mdb(Pager *pPage
13647 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 4d 45 4d  r){.  return MEM
13648 44 42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  DB;.}../*.** Che
13649 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 61 72  ck that there ar
1364a 65 20 61 74 20 6c 65 61 73 74 20 6e 53 61 76 65  e at least nSave
1364b 70 6f 69 6e 74 20 73 61 76 65 70 6f 69 6e 74 73  point savepoints
1364c 20 6f 70 65 6e 2e 20 49 66 20 74 68 65 72 65 20   open. If there 
1364d 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  are.** currently
1364e 20 6c 65 73 73 20 74 68 61 6e 20 6e 53 61 76 65   less than nSave
1364f 70 6f 69 6e 74 73 20 6f 70 65 6e 2c 20 74 68 65  points open, the
13650 6e 20 6f 70 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f  n open one or mo
13651 72 65 20 73 61 76 65 70 6f 69 6e 74 73 0a 2a 2a  re savepoints.**
13652 20 74 6f 20 6d 61 6b 65 20 75 70 20 74 68 65 20   to make up the 
13653 64 69 66 66 65 72 65 6e 63 65 2e 20 49 66 20 74  difference. If t
13654 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76  he number of sav
13655 65 70 6f 69 6e 74 73 20 69 73 20 61 6c 72 65 61  epoints is alrea
13656 64 79 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6e  dy.** equal to n
13657 53 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20  Savepoint, then 
13658 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
13659 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
1365a 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  If a memory allo
1365b 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 53 51  cation fails, SQ
1365c 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65  LITE_NOMEM is re
1365d 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72  turned. If an er
1365e 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 20 77  ror .** occurs w
1365f 68 69 6c 65 20 6f 70 65 6e 69 6e 67 20 74 68 65  hile opening the
13660 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   sub-journal fil
13661 65 2c 20 74 68 65 6e 20 61 6e 20 49 4f 20 65 72  e, then an IO er
13662 72 6f 72 20 63 6f 64 65 20 69 73 0a 2a 2a 20 72  ror code is.** r
13663 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69  eturned. Otherwi
13664 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  se, SQLITE_OK..*
13665 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13666 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
13667 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 50  rOpenSavepoint(P
13668 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
13669 74 20 6e 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20  t nSavepoint){. 
1366a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1366b 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
1366c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1366d 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69  turn code */.  i
1366e 6e 74 20 6e 43 75 72 72 65 6e 74 20 3d 20 70 50  nt nCurrent = pP
1366f 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
13670 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72  ;        /* Curr
13671 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 73 61  ent number of sa
13672 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 0a 20 20 69  vepoints */..  i
13673 66 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e 6e 43  f( nSavepoint>nC
13674 75 72 72 65 6e 74 20 26 26 20 70 50 61 67 65 72  urrent && pPager
13675 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a  ->useJournal ){.
13676 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20      int ii;     
13677 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13678 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
13679 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
1367a 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 53 61 76   */.    PagerSav
1367b 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 20 20 20  epoint *aNew;   
1367c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1367d 2f 2a 20 4e 65 77 20 50 61 67 65 72 2e 61 53 61  /* New Pager.aSa
1367e 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 2a 2f  vepoint array */
1367f 0a 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20  ..    /* Either 
13680 74 68 65 72 65 20 69 73 20 6e 6f 20 61 63 74 69  there is no acti
13681 76 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68  ve journal or th
13682 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73  e sub-journal is
13683 20 6f 70 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a   open or .    **
13684 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
13685 61 6c 77 61 79 73 20 73 74 6f 72 65 64 20 69 6e  always stored in
13686 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61   memory */.    a
13687 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e  ssert( pPager->n
13688 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20  Savepoint==0 || 
13689 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73  isOpen(pPager->s
1368a 6a 66 64 29 20 7c 7c 0a 20 20 20 20 20 20 20 20  jfd) ||.        
1368b 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
1368c 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
1368d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
1368e 59 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 6f  Y );..    /* Gro
1368f 77 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76  w the Pager.aSav
13690 65 70 6f 69 6e 74 20 61 72 72 61 79 20 75 73 69  epoint array usi
13691 6e 67 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65  ng realloc(). Re
13692 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
13693 4d 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 20  M.    ** if the 
13694 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73  allocation fails
13695 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 72  . Otherwise, zer
13696 6f 20 74 68 65 20 6e 65 77 20 70 6f 72 74 69 6f  o the new portio
13697 6e 20 69 6e 20 63 61 73 65 20 61 20 0a 20 20 20  n in case a .   
13698 20 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75   ** malloc failu
13699 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  re occurs while 
1369a 70 6f 70 75 6c 61 74 69 6e 67 20 69 74 20 69 6e  populating it in
1369b 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f   the for(...) lo
1369c 6f 70 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f  op below..    */
1369d 0a 20 20 20 20 61 4e 65 77 20 3d 20 28 50 61 67  .    aNew = (Pag
1369e 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 29 73 71  erSavepoint *)sq
1369f 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20  lite3Realloc(.  
136a0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53        pPager->aS
136a1 61 76 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66  avepoint, sizeof
136a2 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29  (PagerSavepoint)
136a3 2a 6e 53 61 76 65 70 6f 69 6e 74 0a 20 20 20 20  *nSavepoint.    
136a4 29 3b 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77  );.    if( !aNew
136a5 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
136a6 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
136a7 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28     }.    memset(
136a8 26 61 4e 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c  &aNew[nCurrent],
136a9 20 30 2c 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d   0, (nSavepoint-
136aa 6e 43 75 72 72 65 6e 74 29 20 2a 20 73 69 7a 65  nCurrent) * size
136ab 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e  of(PagerSavepoin
136ac 74 29 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  t));.    pPager-
136ad 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e  >aSavepoint = aN
136ae 65 77 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  ew;.    pPager->
136af 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e 53 61  nSavepoint = nSa
136b0 76 65 70 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f 2a  vepoint;..    /*
136b1 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 50 61   Populate the Pa
136b2 67 65 72 53 61 76 65 70 6f 69 6e 74 20 73 74 72  gerSavepoint str
136b3 75 63 74 75 72 65 73 20 6a 75 73 74 20 61 6c 6c  uctures just all
136b4 6f 63 61 74 65 64 2e 20 2a 2f 0a 20 20 20 20 66  ocated. */.    f
136b5 6f 72 28 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20  or(ii=nCurrent; 
136b6 69 69 3c 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69  ii<nSavepoint; i
136b7 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65  i++){.      asse
136b8 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  rt( pPager->dbSi
136b9 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20  zeValid );.     
136ba 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69 67 20   aNew[ii].nOrig 
136bb 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  = pPager->dbSize
136bc 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70  ;.      if( isOp
136bd 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
136be 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
136bf 61 6c 4f 66 66 3e 30 20 29 7b 0a 20 20 20 20 20  alOff>0 ){.     
136c0 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66     aNew[ii].iOff
136c1 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  set = pPager->jo
136c2 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 20 20  urnalOff;.      
136c3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
136c4 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20  New[ii].iOffset 
136c5 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  = JOURNAL_HDR_SZ
136c6 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
136c7 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d  }.      aNew[ii]
136c8 2e 69 53 75 62 52 65 63 20 3d 20 70 50 61 67 65  .iSubRec = pPage
136c9 72 2d 3e 6e 53 75 62 52 65 63 3b 0a 20 20 20 20  r->nSubRec;.    
136ca 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61    aNew[ii].pInSa
136cb 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c 69 74 65  vepoint = sqlite
136cc 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 50  3BitvecCreate(pP
136cd 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20  ager->dbSize);. 
136ce 20 20 20 20 20 69 66 28 20 21 61 4e 65 77 5b 69       if( !aNew[i
136cf 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20  i].pInSavepoint 
136d0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
136d1 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
136d2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
136d3 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73     /* Open the s
136d4 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69  ub-journal, if i
136d5 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  t is not already
136d6 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 20   opened. */.    
136d7 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72  rc = openSubJour
136d8 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  nal(pPager);.  }
136d9 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
136da 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
136db 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
136dc 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72  to rollback or r
136dd 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20  elease (commit) 
136de 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20  a savepoint..** 
136df 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f  The savepoint to
136e0 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c   release or roll
136e1 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62 65  back need not be
136e2 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
136e3 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 64 20 73  ly .** created s
136e4 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20  avepoint..**.** 
136e5 50 61 72 61 6d 65 74 65 72 20 6f 70 20 69 73 20  Parameter op is 
136e6 61 6c 77 61 79 73 20 65 69 74 68 65 72 20 53 41  always either SA
136e7 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
136e8 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 45   or SAVEPOINT_RE
136e9 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74 20  LEASE..** If it 
136ea 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  is SAVEPOINT_REL
136eb 45 41 53 45 2c 20 74 68 65 6e 20 72 65 6c 65 61  EASE, then relea
136ec 73 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 74  se and destroy t
136ed 68 65 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74  he savepoint wit
136ee 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 61 76 65  h.** index iSave
136ef 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 69 73 20  point. If it is 
136f0 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
136f1 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61 63  CK, then rollbac
136f2 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a  k all changes.**
136f3 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 72   that have occur
136f4 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 73 70  red since the sp
136f5 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 6e  ecified savepoin
136f6 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a  t was created..*
136f7 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 70 6f 69  *.** The savepoi
136f8 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f  nt to rollback o
136f9 72 20 72 65 6c 65 61 73 65 20 69 73 20 69 64 65  r release is ide
136fa 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ntified by param
136fb 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 65 70 6f  eter .** iSavepo
136fc 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 6f 66 20  int. A value of 
136fd 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72 61  0 means to opera
136fe 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 6d  te on the outerm
136ff 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a  ost savepoint.**
13700 20 28 74 68 65 20 66 69 72 73 74 20 63 72 65 61   (the first crea
13701 74 65 64 29 2e 20 41 20 76 61 6c 75 65 20 6f 66  ted). A value of
13702 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69   (Pager.nSavepoi
13703 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65 72  nt-1) means oper
13704 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 6f  ate.** on the mo
13705 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65 61  st recently crea
13706 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49  ted savepoint. I
13707 66 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20  f iSavepoint is 
13708 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20  greater than.** 
13709 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e  (Pager.nSavepoin
1370a 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 69 73 20  t-1), then this 
1370b 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
1370c 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  -op..**.** If a 
1370d 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 69  negative value i
1370e 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  s passed to this
1370f 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20   function, then 
13710 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74  the current.** t
13711 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
13712 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20  lled back. This 
13713 69 73 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20  is different to 
13714 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69  calling .** sqli
13715 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
13716 28 29 20 62 65 63 61 75 73 65 20 74 68 69 73 20  () because this 
13717 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  function does no
13718 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74  t terminate.** t
13719 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
1371a 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74  r unlock the dat
1371b 61 62 61 73 65 2c 20 69 74 20 6a 75 73 74 20 72  abase, it just r
1371c 65 73 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a 20  estores the .** 
1371d 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
1371e 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 20  database to its 
1371f 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 2e 20  original state. 
13720 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 61  .**.** In any ca
13721 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e  se, all savepoin
13722 74 73 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78  ts with an index
13723 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 53   greater than iS
13724 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 65  avepoint .** are
13725 20 64 65 73 74 72 6f 79 65 64 2e 20 49 66 20 74   destroyed. If t
13726 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 65  his is a release
13727 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d 3d   operation (op==
13728 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
13729 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76 65  E),.** then save
1372a 70 6f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74  point iSavepoint
1372b 20 69 73 20 61 6c 73 6f 20 64 65 73 74 72 6f 79   is also destroy
1372c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ed..**.** This f
1372d 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75  unction may retu
1372e 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  rn SQLITE_NOMEM 
1372f 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  if a memory allo
13730 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a  cation fails,.**
13731 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   or an IO error 
13732 63 6f 64 65 20 69 66 20 61 6e 20 49 4f 20 65 72  code if an IO er
13733 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ror occurs while
13734 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20   rolling back a 
13735 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20 49  .** savepoint. I
13736 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63 75  f no errors occu
13737 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  r, SQLITE_OK is 
13738 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 51  returned..*/ .SQ
13739 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1373a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76   sqlite3PagerSav
1373b 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50  epoint(Pager *pP
1373c 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  ager, int op, in
1373d 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20  t iSavepoint){. 
1373e 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1373f 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  _OK;..  assert( 
13740 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  op==SAVEPOINT_RE
13741 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56  LEASE || op==SAV
13742 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
13743 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 61  );.  assert( iSa
13744 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70  vepoint>=0 || op
13745 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  ==SAVEPOINT_ROLL
13746 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 69  BACK );..  if( i
13747 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 72  Savepoint<pPager
13748 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a  ->nSavepoint ){.
13749 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20      int ii;     
1374a 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
1374b 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  or variable */. 
1374c 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20     int nNew;    
1374d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1374e 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 73 61 76  of remaining sav
1374f 65 70 6f 69 6e 74 73 20 61 66 74 65 72 20 74 68  epoints after th
13750 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f  is op. */..    /
13751 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77  * Figure out how
13752 20 6d 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73   many savepoints
13753 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61   will still be a
13754 63 74 69 76 65 20 61 66 74 65 72 20 74 68 69 73  ctive after this
13755 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f  .    ** operatio
13756 6e 2e 20 53 74 6f 72 65 20 74 68 69 73 20 76 61  n. Store this va
13757 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65  lue in nNew. The
13758 6e 20 66 72 65 65 20 72 65 73 6f 75 72 63 65 73  n free resources
13759 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20 20 20   associated .   
1375a 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 73 61 76   ** with any sav
1375b 65 70 6f 69 6e 74 73 20 74 68 61 74 20 61 72 65  epoints that are
1375c 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 74 68   destroyed by th
1375d 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20  is operation..  
1375e 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20    */.    nNew = 
1375f 69 53 61 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70  iSavepoint + (op
13760 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  ==SAVEPOINT_ROLL
13761 42 41 43 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69  BACK);.    for(i
13762 69 3d 6e 4e 65 77 3b 20 69 69 3c 70 50 61 67 65  i=nNew; ii<pPage
13763 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69  r->nSavepoint; i
13764 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
13765 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
13766 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  (pPager->aSavepo
13767 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70  int[ii].pInSavep
13768 6f 69 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20  oint);.    }.   
13769 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f   pPager->nSavepo
1376a 69 6e 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20  int = nNew;..   
1376b 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
1376c 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74   rollback operat
1376d 69 6f 6e 2c 20 70 6c 61 79 62 61 63 6b 20 74 68  ion, playback th
1376e 65 20 73 70 65 63 69 66 69 65 64 20 73 61 76 65  e specified save
1376f 70 6f 69 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 66  point..    ** If
13770 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 2d   this is a temp-
13771 66 69 6c 65 2c 20 69 74 20 69 73 20 70 6f 73 73  file, it is poss
13772 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 6a 6f  ible that the jo
13773 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 0a 20  urnal file has. 
13774 20 20 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65     ** not yet be
13775 65 6e 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68  en opened. In th
13776 69 73 20 63 61 73 65 20 74 68 65 72 65 20 68 61  is case there ha
13777 76 65 20 62 65 65 6e 20 6e 6f 20 63 68 61 6e 67  ve been no chang
13778 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65  es to.    ** the
13779 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
1377a 73 6f 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20  so the playback 
1377b 6f 70 65 72 61 74 69 6f 6e 20 63 61 6e 20 62 65  operation can be
1377c 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 20 2a 2f   skipped..    */
1377d 0a 20 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 56  .    if( op==SAV
1377e 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1377f 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  && isOpen(pPager
13780 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20  ->jfd) ){.      
13781 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a  PagerSavepoint *
13782 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 28 6e 4e  pSavepoint = (nN
13783 65 77 3d 3d 30 29 3f 30 3a 26 70 50 61 67 65 72  ew==0)?0:&pPager
13784 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 6e 4e 65  ->aSavepoint[nNe
13785 77 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d  w-1];.      rc =
13786 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61   pagerPlaybackSa
13787 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2c 20  vepoint(pPager, 
13788 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20  pSavepoint);.   
13789 20 20 20 61 73 73 65 72 74 28 72 63 21 3d 53 51     assert(rc!=SQ
1378a 4c 49 54 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 20  LITE_DONE);.    
1378b 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74  }.  .    /* If t
1378c 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 65  his is a release
1378d 20 6f 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73   of the outermos
1378e 74 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 72 75  t savepoint, tru
1378f 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a 20 74 68  ncate .    ** th
13790 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 74 6f  e sub-journal to
13791 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73   zero bytes in s
13792 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ize. */.    if( 
13793 6e 4e 65 77 3d 3d 30 20 26 26 20 6f 70 3d 3d 53  nNew==0 && op==S
13794 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
13795 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65   && isOpen(pPage
13796 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20  r->sjfd) ){.    
13797 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
13798 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20  LITE_OK );.     
13799 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
1379a 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
1379b 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20  sjfd, 0);.      
1379c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20  pPager->nSubRec 
1379d 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
1379e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1379f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
137a0 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66  full pathname of
137a1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
137a2 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
137a3 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
137a4 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
137a5 6c 65 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50  lename(Pager *pP
137a6 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
137a7 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
137a8 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  e;.}../*.** Retu
137a9 72 6e 20 74 68 65 20 56 46 53 20 73 74 72 75 63  rn the VFS struc
137aa 74 75 72 65 20 66 6f 72 20 74 68 65 20 70 61 67  ture for the pag
137ab 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
137ac 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69  IVATE const sqli
137ad 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33  te3_vfs *sqlite3
137ae 50 61 67 65 72 56 66 73 28 50 61 67 65 72 20 2a  PagerVfs(Pager *
137af 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72  pPager){.  retur
137b0 6e 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a  n pPager->pVfs;.
137b1 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
137b2 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20  the file handle 
137b3 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
137b4 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64   file associated
137b5 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 70 61 67  .** with the pag
137b6 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20  er.  This might 
137b7 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74  return NULL if t
137b8 68 65 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e  he file has.** n
137b9 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e  ot yet been open
137ba 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
137bb 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69  IVATE sqlite3_fi
137bc 6c 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  le *sqlite3Pager
137bd 46 69 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67  File(Pager *pPag
137be 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
137bf 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a  ager->fd;.}../*.
137c0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75  ** Return the fu
137c1 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74  ll pathname of t
137c2 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
137c3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
137c4 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
137c5 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
137c6 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50  alname(Pager *pP
137c7 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
137c8 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
137c9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
137ca 6e 20 74 72 75 65 20 69 66 20 66 73 79 6e 63 28  n true if fsync(
137cb 29 20 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61  ) calls are disa
137cc 62 6c 65 64 20 66 6f 72 20 74 68 69 73 20 70 61  bled for this pa
137cd 67 65 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c  ger.  Return FAL
137ce 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29  SE.** if fsync()
137cf 73 20 61 72 65 20 65 78 65 63 75 74 65 64 20 6e  s are executed n
137d0 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  ormally..*/.SQLI
137d1 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
137d2 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e  qlite3PagerNosyn
137d3 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  c(Pager *pPager)
137d4 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
137d5 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69  r->noSync;.}..#i
137d6 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
137d7 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20  CODEC./*.** Set 
137d8 74 68 65 20 63 6f 64 65 63 20 66 6f 72 20 74 68  the codec for th
137d9 69 73 20 70 61 67 65 72 0a 2a 2f 0a 53 51 4c 49  is pager.*/.SQLI
137da 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
137db 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
137dc 6f 64 65 63 28 0a 20 20 50 61 67 65 72 20 2a 70  odec(.  Pager *p
137dd 50 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28  Pager,.  void *(
137de 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76  *xCodec)(void*,v
137df 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a  oid*,Pgno,int),.
137e0 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 41 72    void *pCodecAr
137e1 67 0a 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 78  g.){.  pPager->x
137e2 43 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a  Codec = xCodec;.
137e3 20 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63    pPager->pCodec
137e4 41 72 67 20 3d 20 70 43 6f 64 65 63 41 72 67 3b  Arg = pCodecArg;
137e5 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
137e6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
137e7 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
137e8 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50  Move the page pP
137e9 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67  g to location pg
137ea 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a  no in the file..
137eb 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74  **.** There must
137ec 20 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65   be no reference
137ed 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 72  s to the page pr
137ee 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64  eviously located
137ef 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69   at.** pgno (whi
137f0 63 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c  ch we call pPgOl
137f1 64 29 20 74 68 6f 75 67 68 20 74 68 61 74 20 70  d) though that p
137f2 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 74  age is allowed t
137f3 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65  o be.** in cache
137f4 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 70  .  If the page p
137f5 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65  reviously locate
137f6 64 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74  d at pgno is not
137f7 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74   already.** in t
137f8 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
137f9 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 70  nal, it is not p
137fa 75 74 20 74 68 65 72 65 20 62 79 20 62 79 20 74  ut there by by t
137fb 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a  his routine..**.
137fc 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 6f  ** References to
137fd 20 74 68 65 20 70 61 67 65 20 70 50 67 20 72 65   the page pPg re
137fe 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 61  main valid. Upda
137ff 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61  ting any.** meta
13800 2d 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64  -data associated
13801 20 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e 20   with pPg (i.e. 
13802 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74  data stored in t
13803 68 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a  he nExtra bytes.
13804 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f  ** allocated alo
13805 6e 67 20 77 69 74 68 20 74 68 65 20 70 61 67 65  ng with the page
13806 29 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73  ) is the respons
13807 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63  ibility of the c
13808 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74  aller..**.** A t
13809 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20  ransaction must 
1380a 62 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 74  be active when t
1380b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
1380c 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74  alled. It used t
1380d 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 64  o be.** required
1380e 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e   that a statemen
1380f 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61  t transaction wa
13810 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 75  s not active, bu
13811 74 20 74 68 69 73 20 72 65 73 74 72 69 63 74 69  t this restricti
13812 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 72  on.** has been r
13813 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 49  emoved (CREATE I
13814 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f  NDEX needs to mo
13815 76 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 61  ve a page when a
13816 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72   statement.** tr
13817 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
13818 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ive)..**.** If t
13819 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65  he fourth argume
1381a 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73  nt, isCommit, is
1381b 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
1381c 74 68 69 73 20 70 61 67 65 20 69 73 20 62 65 69  this page is bei
1381d 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70  ng.** moved as p
1381e 61 72 74 20 6f 66 20 61 20 64 61 74 61 62 61 73  art of a databas
1381f 65 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e  e reorganization
13820 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65   just before the
13821 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a   transaction .**
13822 20 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74   is being commit
13823 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ted. In this cas
13824 65 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e 74  e, it is guarant
13825 65 65 64 20 74 68 61 74 20 74 68 65 20 64 61 74  eed that the dat
13826 61 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 70  abase page .** p
13827 50 67 20 72 65 66 65 72 73 20 74 6f 20 77 69 6c  Pg refers to wil
13828 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e  l not be written
13829 20 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69 6e   to again within
1382a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
1382b 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  n..**.** This fu
1382c 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72  nction may retur
1382d 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f  n SQLITE_NOMEM o
1382e 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f  r an IO error co
1382f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a  de if an error.*
13830 2a 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 77  * occurs. Otherw
13831 69 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20  ise, it returns 
13832 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51  SQLITE_OK..*/.SQ
13833 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13834 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76   sqlite3PagerMov
13835 65 70 61 67 65 28 50 61 67 65 72 20 2a 70 50 61  epage(Pager *pPa
13836 67 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67  ger, DbPage *pPg
13837 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74  , Pgno pgno, int
13838 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67   isCommit){.  Pg
13839 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20  Hdr *pPgOld;    
1383a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1383b 65 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65  e page being ove
1383c 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50  rwritten. */.  P
1383d 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f  gno needSyncPgno
1383e 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f   = 0;       /* O
1383f 6c 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67 2d  ld value of pPg-
13840 3e 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69  >pgno, if sync i
13841 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  s required */.  
13842 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
13843 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13844 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
13845 20 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20   Pgno origPgno; 
13846 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13847 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61   The original pa
13848 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20  ge number */..  
13849 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65  assert( pPg->nRe
1384a 66 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  f>0 );..  /* If 
1384b 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d  the page being m
1384c 6f 76 65 64 20 69 73 20 64 69 72 74 79 20 61 6e  oved is dirty an
1384d 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73  d has not been s
1384e 61 76 65 64 20 62 79 20 74 68 65 20 6c 61 74 65  aved by the late
1384f 73 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e  st.  ** savepoin
13850 74 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68 65  t, then save the
13851 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74   current content
13852 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e  s of the page in
13853 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62  to the .  ** sub
13854 2d 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68  -journal now. Th
13855 69 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74  is is required t
13856 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c  o handle the fol
13857 6c 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a  lowing scenario:
13858 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47  .  **.  **   BEG
13859 49 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f  IN;.  **     <jo
1385a 75 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68  urnal page X, th
1385b 65 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20  en modify it in 
1385c 6d 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20  memory>.  **    
1385d 20 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a   SAVEPOINT one;.
1385e 20 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65    **       <Move
1385f 20 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74   page X to locat
13860 69 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20  ion Y>.  **     
13861 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b  ROLLBACK TO one;
13862 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61  .  **.  ** If pa
13863 67 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72  ge X were not wr
13864 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62  itten to the sub
13865 2d 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69  -journal here, i
13866 74 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a  t would not.  **
13867 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20   be possible to 
13868 72 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74  restore its cont
13869 65 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52  ents when the "R
1386a 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a  OLLBACK TO one".
1386b 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77    ** statement w
1386c 65 72 65 20 69 73 20 70 72 6f 63 65 73 73 65 64  ere is processed
1386d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a  ..  **.  ** subj
1386e 6f 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79  ournalPage() may
1386f 20 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74   need to allocat
13870 65 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65  e space to store
13871 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a   pPg->pgno into.
13872 20 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65    ** one or more
13873 20 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65   savepoint bitve
13874 63 73 2e 20 54 68 69 73 20 69 73 20 74 68 65 20  cs. This is the 
13875 72 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63  reason this func
13876 74 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65  tion.  ** may re
13877 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
13878 4d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  M..  */.  if( pP
13879 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
1387a 49 52 54 59 20 0a 20 20 20 26 26 20 73 75 62 6a  IRTY .   && subj
1387b 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67  RequiresPage(pPg
1387c 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f  ).   && SQLITE_O
1387d 4b 21 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72  K!=(rc = subjour
1387e 6e 61 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20  nalPage(pPg)).  
1387f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
13880 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52  ;.  }..  PAGERTR
13881 41 43 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61  ACE(("MOVE %d pa
13882 67 65 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d  ge %d (needSync=
13883 25 64 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c  %d) moves to %d\
13884 6e 22 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52  n", .      PAGER
13885 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
13886 3e 70 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61  >pgno, (pPg->fla
13887 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
13888 4e 43 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b  NC)?1:0, pgno));
13889 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56  .  IOTRACE(("MOV
1388a 45 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70  E %p %d %d\n", p
1388b 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
1388c 2c 20 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49  , pgno))..  /* I
1388d 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65  f the journal ne
1388e 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29  eds to be sync()
1388f 65 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70  ed before page p
13890 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a  Pg->pgno can.  *
13891 2a 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c  * be written to,
13892 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f   store pPg->pgno
13893 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   in local variab
13894 6c 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e  le needSyncPgno.
13895 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
13896 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20  e isCommit flag 
13897 69 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73  is set, there is
13898 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65   no need to reme
13899 6d 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74  mber that.  ** t
1389a 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73  he journal needs
1389b 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20   to be sync()ed 
1389c 62 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20  before database 
1389d 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a  page pPg->pgno .
1389e 20 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74    ** can be writ
1389f 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c  ten to. The call
138a0 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70  er has already p
138a1 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77  romised not to w
138a2 72 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f  rite to it..  */
138a3 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61  .  if( (pPg->fla
138a4 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
138a5 4e 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74  NC) && !isCommit
138a6 20 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63   ){.    needSync
138a7 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f  Pgno = pPg->pgno
138a8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61  ;.    assert( pa
138a9 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29  geInJournal(pPg)
138aa 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50   || pPg->pgno>pP
138ab 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
138ac 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
138ad 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
138ae 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73  _DIRTY );.    as
138af 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65  sert( pPager->ne
138b0 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20  edSync );.  }.. 
138b1 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65   /* If the cache
138b2 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65   contains a page
138b3 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65   with page-numbe
138b4 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69  r pgno, remove i
138b5 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20  t.  ** from its 
138b6 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f  hash chain. Also
138b7 2c 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e  , if the PgHdr.n
138b8 65 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20  eedSync was set 
138b9 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70  for .  ** page p
138ba 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27  gno before the '
138bb 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c  move' operation,
138bc 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20   it needs to be 
138bd 72 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66  retained .  ** f
138be 6f 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65  or the page move
138bf 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20  d there..  */.  
138c0 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50  pPg->flags &= ~P
138c1 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a  GHDR_NEED_SYNC;.
138c2 20 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72    pPgOld = pager
138c3 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20  _lookup(pPager, 
138c4 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28  pgno);.  assert(
138c5 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f   !pPgOld || pPgO
138c6 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20  ld->nRef==1 );. 
138c7 20 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20   if( pPgOld ){. 
138c8 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d     pPg->flags |=
138c9 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26   (pPgOld->flags&
138ca 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
138cb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  ;.    sqlite3Pca
138cc 63 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b  cheDrop(pPgOld);
138cd 0a 20 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e 6f  .  }..  origPgno
138ce 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20   = pPg->pgno;.  
138cf 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76  sqlite3PcacheMov
138d0 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 20  e(pPg, pgno);.  
138d1 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
138d2 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70  eDirty(pPg);.  p
138d3 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65  Pager->dbModifie
138d4 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65  d = 1;..  if( ne
138d5 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20  edSyncPgno ){.  
138d6 20 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63    /* If needSync
138d7 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  Pgno is non-zero
138d8 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
138d9 61 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f  al file needs to
138da 20 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63   be .    ** sync
138db 28 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20  ()ed before any 
138dc 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20  data is written 
138dd 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  to database file
138de 20 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67   page needSyncPg
138df 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65  no..    ** Curre
138e0 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61  ntly, no such pa
138e1 67 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65  ge exists in the
138e2 20 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20   page-cache and 
138e3 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20  the .    ** "is 
138e4 6a 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65  journaled" bitve
138e5 63 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20  c flag has been 
138e6 73 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20  set. This needs 
138e7 74 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 62  to be remedied b
138e8 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67  y.    ** loading
138e9 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74   the page into t
138ea 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61  he pager-cache a
138eb 6e 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 50  nd setting the P
138ec 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20  gHdr.needSync . 
138ed 20 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20     ** flag..    
138ee 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65  **.    ** If the
138ef 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64   attempt to load
138f0 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74   the page into t
138f1 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61  he page-cache fa
138f2 69 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a  ils, (due.    **
138f3 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f   to a malloc() o
138f4 72 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63  r IO failure), c
138f5 6c 65 61 72 20 74 68 65 20 62 69 74 20 69 6e 20  lear the bit in 
138f6 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d  the pInJournal[]
138f7 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f  .    ** array. O
138f8 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
138f9 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20   page is loaded 
138fa 61 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 69  and written agai
138fb 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73  n in.    ** this
138fc 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74   transaction, it
138fd 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20   may be written 
138fe 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
138ff 66 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20  file before.    
13900 2a 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 20  ** it is synced 
13901 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
13902 20 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c   file. This way,
13903 20 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69   it may end up i
13904 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75  n.    ** the jou
13905 72 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c  rnal file twice,
13906 20 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74   but that is not
13907 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20   a problem..    
13908 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71  **.    ** The sq
13909 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20  lite3PagerGet() 
1390a 63 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74  call may cause t
1390b 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79  he journal to sy
1390c 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20  nc. So make.    
1390d 2a 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 65  ** sure the Page
1390e 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20  r.needSync flag 
1390f 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20  is set too..    
13910 2a 2f 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50  */.    PgHdr *pP
13911 67 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74  gHdr;.    assert
13912 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79  ( pPager->needSy
13913 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  nc );.    rc = s
13914 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
13915 50 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50  Pager, needSyncP
13916 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20  gno, &pPgHdr);. 
13917 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
13918 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66  E_OK ){.      if
13919 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  ( pPager->pInJou
1391a 72 6e 61 6c 20 26 26 20 6e 65 65 64 53 79 6e 63  rnal && needSync
1391b 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62  Pgno<=pPager->db
1391c 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20  OrigSize ){.    
1391d 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65      sqlite3Bitve
1391e 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70  cClear(pPager->p
1391f 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 53  InJournal, needS
13920 79 6e 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20  yncPgno);.      
13921 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  }.      return r
13922 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  c;.    }.    pPa
13923 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20  ger->needSync = 
13924 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  1;.    assert( p
13925 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30  Pager->noSync==0
13926 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20   && !MEMDB );.  
13927 20 20 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20    pPgHdr->flags 
13928 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  |= PGHDR_NEED_SY
13929 4e 43 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  NC;.    sqlite3P
1392a 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70  cacheMakeDirty(p
1392b 50 67 48 64 72 29 3b 0a 20 20 20 20 73 71 6c 69  PgHdr);.    sqli
1392c 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
1392d 67 48 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  gHdr);.  }..  /*
1392e 0a 20 20 2a 2a 20 46 6f 72 20 61 6e 20 69 6e 2d  .  ** For an in-
1392f 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c  memory database,
13930 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f   make sure the o
13931 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e  riginal page con
13932 74 69 6e 75 65 73 0a 20 20 2a 2a 20 74 6f 20 65  tinues.  ** to e
13933 78 69 73 74 2c 20 69 6e 20 63 61 73 65 20 74 68  xist, in case th
13934 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65  e transaction ne
13935 65 64 73 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b  eds to roll back
13936 2e 20 20 57 65 20 61 6c 6c 6f 63 61 74 65 0a 20  .  We allocate. 
13937 20 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 6f 77   ** the page now
13938 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 61 74 20  , instead of at 
13939 72 6f 6c 6c 62 61 63 6b 2c 20 62 65 63 61 75 73  rollback, becaus
1393a 65 20 77 65 20 63 61 6e 20 62 65 74 74 65 72 20  e we can better 
1393b 64 65 61 6c 0a 20 20 2a 2a 20 77 69 74 68 20 61  deal.  ** with a
1393c 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20  n out-of-memory 
1393d 65 72 72 6f 72 20 6e 6f 77 2e 20 20 54 69 63 6b  error now.  Tick
1393e 65 74 20 23 33 37 36 31 2e 0a 20 20 2a 2f 0a 20  et #3761..  */. 
1393f 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20   if( MEMDB ){.  
13940 20 20 44 62 50 61 67 65 20 2a 70 4e 65 77 3b 0a    DbPage *pNew;.
13941 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13942 50 61 67 65 72 41 63 71 75 69 72 65 28 70 50 61  PagerAcquire(pPa
13943 67 65 72 2c 20 6f 72 69 67 50 67 6e 6f 2c 20 26  ger, origPgno, &
13944 70 4e 65 77 2c 20 31 29 3b 0a 20 20 20 20 69 66  pNew, 1);.    if
13945 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13946 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
13947 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
13948 65 66 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20  ef(pNew);.  }.. 
13949 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1394a 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  K;.}.#endif../*.
1394b 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
1394c 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61 20  ter to the data 
1394d 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
1394e 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54  d page..*/.SQLIT
1394f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
13950 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
13951 61 74 61 28 44 62 50 61 67 65 20 2a 70 50 67 29  ata(DbPage *pPg)
13952 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d  {.  assert( pPg-
13953 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e  >nRef>0 || pPg->
13954 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b  pPager->memDb );
13955 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70  .  return pPg->p
13956 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  Data;.}../*.** R
13957 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
13958 74 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78  to the Pager.nEx
13959 74 72 61 20 62 79 74 65 73 20 6f 66 20 22 65 78  tra bytes of "ex
1395a 74 72 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 61  tra" space .** a
1395b 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77  llocated along w
1395c 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 65  ith the specifie
1395d 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54  d page..*/.SQLIT
1395e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
1395f 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
13960 78 74 72 61 28 44 62 50 61 67 65 20 2a 70 50 67  xtra(DbPage *pPg
13961 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
13962 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
13963 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67  ;.  return (pPag
13964 65 72 3f 70 50 67 2d 3e 70 45 78 74 72 61 3a 30  er?pPg->pExtra:0
13965 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f  );.}../*.** Get/
13966 73 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d  set the locking-
13967 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61  mode for this pa
13968 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65  ger. Parameter e
13969 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65  Mode must be one
1396a 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43  .** of PAGER_LOC
1396b 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20  KINGMODE_QUERY, 
1396c 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
1396d 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20  E_NORMAL or .** 
1396e 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
1396f 45 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20  E_EXCLUSIVE. If 
13970 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  the parameter is
13971 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65   not _QUERY, the
13972 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67  n.** the locking
13973 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20  -mode is set to 
13974 74 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66  the value specif
13975 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ied..**.** The r
13976 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73  eturned value is
13977 20 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f   either PAGER_LO
13978 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
13979 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43   or.** PAGER_LOC
1397a 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
1397b 56 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74  VE, indicating t
1397c 68 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73  he current (poss
1397d 69 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a  ibly updated).**
1397e 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a   locking-mode..*
1397f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13980 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
13981 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67  rLockingMode(Pag
13982 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
13983 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74  eMode){.  assert
13984 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c  ( eMode==PAGER_L
13985 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59  OCKINGMODE_QUERY
13986 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
13987 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  eMode==PAGER_LOC
13988 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a  KINGMODE_NORMAL.
13989 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
1398a 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
1398b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
1398c 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50  E );.  assert( P
1398d 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
1398e 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73  _QUERY<0 );.  as
1398f 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b  sert( PAGER_LOCK
13990 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d  INGMODE_NORMAL>=
13991 30 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49  0 && PAGER_LOCKI
13992 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
13993 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f  >=0 );.  if( eMo
13994 64 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72  de>=0 && !pPager
13995 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20  ->tempFile ){.  
13996 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73    pPager->exclus
13997 69 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d  iveMode = (u8)eM
13998 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ode;.  }.  retur
13999 6e 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65  n (int)pPager->e
1399a 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a  xclusiveMode;.}.
1399b 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74  ./*.** Get/set t
1399c 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20  he journal-mode 
1399d 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20  for this pager. 
1399e 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20  Parameter eMode 
1399f 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a  must be one of:.
139a0 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a  **.**    PAGER_J
139a1 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59  OURNALMODE_QUERY
139a2 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55  .**    PAGER_JOU
139a3 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a  RNALMODE_DELETE.
139a4 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
139a5 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45  NALMODE_TRUNCATE
139a6 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55  .**    PAGER_JOU
139a7 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
139a8 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55  .**    PAGER_JOU
139a9 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20  RNALMODE_OFF.** 
139aa 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c     PAGER_JOURNAL
139ab 4d 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a  MODE_MEMORY.**.*
139ac 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74  * If the paramet
139ad 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59  er is not _QUERY
139ae 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
139af 61 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74  al_mode is set t
139b0 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73  o the.** value s
139b1 70 65 63 69 66 69 65 64 20 69 66 20 74 68 65 20  pecified if the 
139b2 63 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65  change is allowe
139b3 64 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20 69  d.  The change i
139b4 73 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20  s disallowed.** 
139b5 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  for the followin
139b6 67 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a  g reasons:.**.**
139b7 20 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f     *  An in-memo
139b8 72 79 20 64 61 74 61 62 61 73 65 20 63 61 6e 20  ry database can 
139b9 6f 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f  only have its jo
139ba 75 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74  urnal_mode set t
139bb 6f 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f  o _OFF.**      o
139bc 72 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a  r _MEMORY..**.**
139bd 20 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61     *  The journa
139be 6c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62  l mode may not b
139bf 65 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20  e changed while 
139c0 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
139c1 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54   active..**.** T
139c2 68 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69  he returned indi
139c3 63 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  cate the current
139c4 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74   (possibly updat
139c5 65 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65  ed) journal-mode
139c6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
139c7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
139c8 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  agerJournalMode(
139c9 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
139ca 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73  nt eMode){.  ass
139cb 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45  ert( eMode==PAGE
139cc 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
139cd 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20  ERY.            
139ce 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
139cf 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45  JOURNALMODE_DELE
139d0 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  TE.            |
139d1 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
139d2 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43  OURNALMODE_TRUNC
139d3 41 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20  ATE.            
139d4 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
139d5 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53  JOURNALMODE_PERS
139d6 49 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20  IST.            
139d7 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
139d8 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20  JOURNALMODE_OFF 
139d9 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
139da 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
139db 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
139dc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
139dd 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51  ER_JOURNALMODE_Q
139de 55 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20  UERY<0 );.  if( 
139df 65 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28  eMode>=0.   && (
139e0 21 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d  !MEMDB || eMode=
139e1 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
139e2 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20  DE_MEMORY .     
139e3 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64           || eMod
139e4 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
139e5 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20  MODE_OFF).   && 
139e6 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66  !pPager->dbModif
139e7 69 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70  ied.   && (!isOp
139e8 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
139e9 7c 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f  || 0==pPager->jo
139ea 75 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20  urnalOff).  ){. 
139eb 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50     if( isOpen(pP
139ec 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20  ager->jfd) ){.  
139ed 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
139ee 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  se(pPager->jfd);
139ef 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
139f0 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d  r->journalMode =
139f1 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a   (u8)eMode;.  }.
139f2 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50    return (int)pP
139f3 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
139f4 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f  e;.}../*.** Get/
139f5 73 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d  set the size-lim
139f6 69 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 73  it used for pers
139f7 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66  istent journal f
139f8 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74  iles..**.** Sett
139f9 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d  ing the size lim
139fa 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e  it to -1 means n
139fb 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72  o limit is enfor
139fc 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d  ced..** An attem
139fd 70 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69  pt to set a limi
139fe 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d  t smaller than -
139ff 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  1 is a no-op..*/
13a00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13a01 69 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72  i64 sqlite3Pager
13a02 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  JournalSizeLimit
13a03 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
13a04 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69  i64 iLimit){.  i
13a05 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b  f( iLimit>=-1 ){
13a06 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
13a07 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20  rnalSizeLimit = 
13a08 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65  iLimit;.  }.  re
13a09 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75  turn pPager->jou
13a0a 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d  rnalSizeLimit;.}
13a0b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
13a0c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
13a0d 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20  pPager->pBackup 
13a0e 76 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61  variable. The ba
13a0f 63 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69  ckup module.** i
13a10 6e 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74  n backup.c maint
13a11 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74  ains the content
13a12 20 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c   of this variabl
13a13 65 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a  e. This module.*
13a14 2a 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65  * uses it opaque
13a15 6c 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ly as an argumen
13a16 74 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b  t to sqlite3Back
13a17 75 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a  upRestart() and.
13a18 2a 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70  ** sqlite3Backup
13a19 55 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a  Update() only..*
13a1a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13a1b 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
13a1c 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61  **sqlite3PagerBa
13a1d 63 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70  ckupPtr(Pager *p
13a1e 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
13a1f 20 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75   &pPager->pBacku
13a20 70 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  p;.}..#endif /* 
13a21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
13a22 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  IO */../********
13a23 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61  ****** End of pa
13a24 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ger.c **********
13a25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a27 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
13a28 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
13a29 65 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a  e btmutex.c ****
13a2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a2c 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
13a2d 37 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a  7 August 27.**.*
13a2e 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
13a2f 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
13a30 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
13a31 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
13a32 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
13a33 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
13a34 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
13a35 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
13a36 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
13a37 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
13a38 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
13a39 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
13a3a 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
13a3b 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
13a3c 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
13a3d 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
13a3e 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
13a3f 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
13a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
13a44 20 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c   $Id: btmutex.c,
13a45 76 20 31 2e 31 35 20 32 30 30 39 2f 30 34 2f 31  v 1.15 2009/04/1
13a46 30 20 31 32 3a 35 35 3a 31 37 20 64 61 6e 69 65  0 12:55:17 danie
13a47 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a 0a  lk1977 Exp $.**.
13a48 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
13a49 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
13a4a 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74  to implement mut
13a4b 65 78 65 73 20 6f 6e 20 42 74 72 65 65 20 6f 62  exes on Btree ob
13a4c 6a 65 63 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63  jects..** This c
13a4d 6f 64 65 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e  ode really belon
13a4e 67 73 20 69 6e 20 62 74 72 65 65 2e 63 2e 20 20  gs in btree.c.  
13a4f 42 75 74 20 62 74 72 65 65 2e 63 20 69 73 20 67  But btree.c is g
13a50 65 74 74 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69  etting too.** bi
13a51 67 20 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f  g and we want to
13a52 20 62 72 65 61 6b 20 69 74 20 64 6f 77 6e 20 73   break it down s
13a53 6f 6d 65 2e 20 20 54 68 69 73 20 70 61 63 6b 61  ome.  This packa
13a54 67 65 64 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a  ged seemed like.
13a55 2a 2a 20 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f  ** a good breako
13a56 75 74 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ut..*/./********
13a57 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62  ****** Include b
13a58 74 72 65 65 49 6e 74 2e 68 20 69 6e 20 74 68 65  treeInt.h in the
13a59 20 6d 69 64 64 6c 65 20 6f 66 20 62 74 6d 75 74   middle of btmut
13a5a 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.c ***********
13a5b 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
13a5c 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
13a5d 65 20 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a  e btreeInt.h ***
13a5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a60 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
13a61 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20  4 April 6.**.** 
13a62 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
13a63 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
13a64 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
13a65 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
13a66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
13a67 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
13a68 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
13a69 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
13a6a 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
13a6b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
13a6c 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
13a6d 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
13a6e 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
13a6f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
13a70 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
13a71 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
13a72 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
13a73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a77 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a  ********.** $Id:
13a78 20 62 74 72 65 65 49 6e 74 2e 68 2c 76 20 31 2e   btreeInt.h,v 1.
13a79 34 36 20 32 30 30 39 2f 30 33 2f 32 30 20 31 34  46 2009/03/20 14
13a7a 3a 31 38 3a 35 32 20 64 61 6e 69 65 6c 6b 31 39  :18:52 danielk19
13a7b 37 37 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54  77 Exp $.**.** T
13a7c 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
13a7d 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28  nts a external (
13a7e 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61  disk-based) data
13a7f 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65  base using BTree
13a80 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61  s..** For a deta
13a81 69 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20  iled discussion 
13a82 6f 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 72  of BTrees, refer
13a83 20 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f   to.**.**     Do
13a84 6e 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54  nald E. Knuth, T
13a85 48 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54  HE ART OF COMPUT
13a86 45 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20  ER PROGRAMMING, 
13a87 56 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20  Volume 3:.**    
13a88 20 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65   "Sorting And Se
13a89 61 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20  arching", pages 
13a8a 34 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e  473-480. Addison
13a8b 2d 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50  -Wesley.**     P
13a8c 75 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e  ublishing Compan
13a8d 79 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73  y, Reading, Mass
13a8e 61 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a  achusetts..**.**
13a8f 20 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20   The basic idea 
13a90 69 73 20 74 68 61 74 20 65 61 63 68 20 70 61 67  is that each pag
13a91 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f  e of the file co
13a92 6e 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73  ntains N databas
13a93 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64  e.** entries and
13a94 20 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f   N+1 pointers to
13a95 20 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a   subpages..**.**
13a96 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
13a97 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13a98 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13a99 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13a9a 2d 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28  ---.**   |  Ptr(
13a9b 30 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74  0) | Key(0) | Pt
13a9c 72 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20  r(1) | Key(1) | 
13a9d 2e 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c  ... | Key(N-1) |
13a9e 20 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d   Ptr(N) |.**   -
13a9f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13aa0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13aa1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13aa2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
13aa3 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65  **.** All of the
13aa4 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67   keys on the pag
13aa5 65 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f  e that Ptr(0) po
13aa6 69 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c  ints to have val
13aa7 75 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e  ues less.** than
13aa8 20 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66   Key(0).  All of
13aa9 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67   the keys on pag
13aaa 65 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 73  e Ptr(1) and its
13aab 20 73 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a   subpages have.*
13aac 2a 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72  * values greater
13aad 20 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64   than Key(0) and
13aae 20 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31   less than Key(1
13aaf 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b  ).  All of the k
13ab0 65 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29  eys.** on Ptr(N)
13ab1 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65   and its subpage
13ab2 73 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 72  s have values gr
13ab3 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e  eater than Key(N
13ab4 2d 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20  -1).  And.** so 
13ab5 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e  forth..**.** Fin
13ab6 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61  ding a particula
13ab7 72 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 72  r key requires r
13ab8 65 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29  eading O(log(M))
13ab9 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20   pages from the 
13aba 0a 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d  .** disk where M
13abb 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
13abc 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  f entries in the
13abd 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20   tree..**.** In 
13abe 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  this implementat
13abf 69 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69  ion, a single fi
13ac0 6c 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20  le can hold one 
13ac1 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65  or more separate
13ac2 20 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61   .** BTrees.  Ea
13ac3 63 68 20 42 54 72 65 65 20 69 73 20 69 64 65 6e  ch BTree is iden
13ac4 74 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e  tified by the in
13ac5 64 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20  dex of its root 
13ac6 70 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65  page.  The.** ke
13ac7 79 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61  y and data for a
13ac8 6e 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d  ny entry are com
13ac9 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68  bined to form th
13aca 65 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a  e "payload".  A.
13acb 2a 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20  ** fixed amount 
13acc 6f 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62  of payload can b
13acd 65 20 63 61 72 72 69 65 64 20 64 69 72 65 63 74  e carried direct
13ace 6c 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ly on the databa
13acf 73 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20  se.** page.  If 
13ad0 74 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c  the payload is l
13ad1 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70  arger than the p
13ad2 72 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65  reset amount the
13ad3 6e 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74  n surplus.** byt
13ad4 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e  es are stored on
13ad5 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
13ad6 20 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f    The payload fo
13ad7 72 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e  r an entry.** an
13ad8 64 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20  d the preceding 
13ad9 70 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62  pointer are comb
13ada 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22  ined to form a "
13adb 43 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a  Cell".  Each .**
13adc 20 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c   page has a smal
13add 6c 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63  l header which c
13ade 6f 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28  ontains the Ptr(
13adf 4e 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f  N) pointer and o
13ae0 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  ther.** informat
13ae1 69 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 20  ion such as the 
13ae2 73 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20  size of key and 
13ae3 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d  data..**.** FORM
13ae4 41 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a  AT DETAILS.**.**
13ae5 20 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 76   The file is div
13ae6 69 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e  ided into pages.
13ae7 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65    The first page
13ae8 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20   is called page 
13ae9 31 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64  1,.** the second
13aea 20 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20   is page 2, and 
13aeb 73 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67  so forth.  A pag
13aec 65 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f  e number of zero
13aed 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e   indicates.** "n
13aee 6f 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 54  o such page".  T
13aef 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e  he page size can
13af0 20 62 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74   be anything bet
13af1 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35  ween 512 and 655
13af2 33 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65  36..** Each page
13af3 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61   can be either a
13af4 20 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66   btree page, a f
13af5 72 65 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20  reelist page or 
13af6 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70  an overflow.** p
13af7 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  age..**.** The f
13af8 69 72 73 74 20 70 61 67 65 20 69 73 20 61 6c 77  irst page is alw
13af9 61 79 73 20 61 20 62 74 72 65 65 20 70 61 67 65  ays a btree page
13afa 2e 20 20 54 68 65 20 66 69 72 73 74 20 31 30 30  .  The first 100
13afb 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 69   bytes of the fi
13afc 72 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74  rst.** page cont
13afd 61 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65  ain a special he
13afe 61 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 20  ader (the "file 
13aff 68 65 61 64 65 72 22 29 20 74 68 61 74 20 64 65  header") that de
13b00 73 63 72 69 62 65 73 20 74 68 65 20 66 69 6c 65  scribes the file
13b01 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20  ..** The format 
13b02 6f 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 64  of the file head
13b03 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  er is as follows
13b04 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54  :.**.**   OFFSET
13b05 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
13b06 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30  IPTION.**      0
13b07 20 20 20 20 20 20 31 36 20 20 20 20 20 48 65 61        16     Hea
13b08 64 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c  der string: "SQL
13b09 69 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30  ite format 3\000
13b0a 22 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20  ".**     16     
13b0b 20 20 32 20 20 20 20 20 50 61 67 65 20 73 69 7a    2     Page siz
13b0c 65 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a  e in bytes.  .**
13b0d 20 20 20 20 20 31 38 20 20 20 20 20 20 20 31 20       18       1 
13b0e 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20      File format 
13b0f 77 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a  write version.**
13b10 20 20 20 20 20 31 39 20 20 20 20 20 20 20 31 20       19       1 
13b11 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20      File format 
13b12 72 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20  read version.** 
13b13 20 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20      20       1  
13b14 20 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73     Bytes of unus
13b15 65 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20  ed space at the 
13b16 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65  end of each page
13b17 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20  .**     21      
13b18 20 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64   1     Max embed
13b19 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
13b1a 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20  tion.**     22  
13b1b 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65       1     Min e
13b1c 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20  mbedded payload 
13b1d 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20  fraction.**     
13b1e 32 33 20 20 20 20 20 20 20 31 20 20 20 20 20 4d  23       1     M
13b1f 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20  in leaf payload 
13b20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20  fraction.**     
13b21 32 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46  24       4     F
13b22 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  ile change count
13b23 65 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20  er.**     28    
13b24 20 20 20 34 20 20 20 20 20 52 65 73 65 72 76 65     4     Reserve
13b25 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65  d for future use
13b26 0a 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20  .**     32      
13b27 20 34 20 20 20 20 20 46 69 72 73 74 20 66 72 65   4     First fre
13b28 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20  elist page.**   
13b29 20 20 33 36 20 20 20 20 20 20 20 34 20 20 20 20    36       4    
13b2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c   Number of freel
13b2b 69 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 65  ist pages in the
13b2c 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20   file.**     40 
13b2d 20 20 20 20 20 36 30 20 20 20 20 20 31 35 20 34       60     15 4
13b2e 2d 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65  -byte meta value
13b2f 73 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 68  s passed to high
13b30 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20  er layers.**.** 
13b31 41 6c 6c 20 6f 66 20 74 68 65 20 69 6e 74 65 67  All of the integ
13b32 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 62 69  er values are bi
13b33 67 2d 65 6e 64 69 61 6e 20 28 6d 6f 73 74 20 73  g-endian (most s
13b34 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20  ignificant byte 
13b35 66 69 72 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  first)..**.** Th
13b36 65 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f  e file change co
13b37 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65  unter is increme
13b38 6e 74 65 64 20 77 68 65 6e 20 74 68 65 20 64 61  nted when the da
13b39 74 61 62 61 73 65 20 69 73 20 63 68 61 6e 67 65  tabase is change
13b3a 64 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65  d.** This counte
13b3b 72 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70  r allows other p
13b3c 72 6f 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77  rocesses to know
13b3d 20 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68   when the file h
13b3e 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e  as changed.** an
13b3f 64 20 74 68 75 73 20 77 68 65 6e 20 74 68 65 79  d thus when they
13b40 20 6e 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74   need to flush t
13b41 68 65 69 72 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a  heir cache..**.*
13b42 2a 20 54 68 65 20 6d 61 78 20 65 6d 62 65 64 64  * The max embedd
13b43 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
13b44 69 6f 6e 20 69 73 20 74 68 65 20 61 6d 6f 75 6e  ion is the amoun
13b45 74 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 75  t of the total u
13b46 73 61 62 6c 65 0a 2a 2a 20 73 70 61 63 65 20 69  sable.** space i
13b47 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 63 61  n a page that ca
13b48 6e 20 62 65 20 63 6f 6e 73 75 6d 65 64 20 62 79  n be consumed by
13b49 20 61 20 73 69 6e 67 6c 65 20 63 65 6c 6c 20 66   a single cell f
13b4a 6f 72 20 73 74 61 6e 64 61 72 64 0a 2a 2a 20 42  or standard.** B
13b4b 2d 74 72 65 65 20 28 6e 6f 6e 2d 4c 45 41 46 44  -tree (non-LEAFD
13b4c 41 54 41 29 20 74 61 62 6c 65 73 2e 20 20 41 20  ATA) tables.  A 
13b4d 76 61 6c 75 65 20 6f 66 20 32 35 35 20 6d 65 61  value of 255 mea
13b4e 6e 73 20 31 30 30 25 2e 20 20 54 68 65 20 64 65  ns 100%.  The de
13b4f 66 61 75 6c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c  fault.** is to l
13b50 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d  imit the maximum
13b51 20 63 65 6c 6c 20 73 69 7a 65 20 73 6f 20 74 68   cell size so th
13b52 61 74 20 61 74 20 6c 65 61 73 74 20 34 20 63 65  at at least 4 ce
13b53 6c 6c 73 20 77 69 6c 6c 20 66 69 74 0a 2a 2a 20  lls will fit.** 
13b54 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 54 68  on one page.  Th
13b55 75 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d  us the default m
13b56 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c  ax embedded payl
13b57 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20  oad fraction is 
13b58 36 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  64..**.** If the
13b59 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 20 63   payload for a c
13b5a 65 6c 6c 20 69 73 20 6c 61 72 67 65 72 20 74 68  ell is larger th
13b5b 61 6e 20 74 68 65 20 6d 61 78 20 70 61 79 6c 6f  an the max paylo
13b5c 61 64 2c 20 74 68 65 6e 20 65 78 74 72 61 0a 2a  ad, then extra.*
13b5d 2a 20 70 61 79 6c 6f 61 64 20 69 73 20 73 70 69  * payload is spi
13b5e 6c 6c 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77  lled to overflow
13b5f 20 70 61 67 65 73 2e 20 20 4f 6e 63 65 20 61 6e   pages.  Once an
13b60 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
13b61 73 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20  s allocated,.** 
13b62 61 73 20 6d 61 6e 79 20 62 79 74 65 73 20 61 73  as many bytes as
13b63 20 70 6f 73 73 69 62 6c 65 20 61 72 65 20 6d 6f   possible are mo
13b64 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 76 65  ved into the ove
13b65 72 66 6c 6f 77 20 70 61 67 65 73 20 77 69 74 68  rflow pages with
13b66 6f 75 74 20 6c 65 74 74 69 6e 67 0a 2a 2a 20 74  out letting.** t
13b67 68 65 20 63 65 6c 6c 20 73 69 7a 65 20 64 72 6f  he cell size dro
13b68 70 20 62 65 6c 6f 77 20 74 68 65 20 6d 69 6e 20  p below the min 
13b69 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
13b6a 20 66 72 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   fraction..**.**
13b6b 20 54 68 65 20 6d 69 6e 20 6c 65 61 66 20 70 61   The min leaf pa
13b6c 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69  yload fraction i
13b6d 73 20 6c 69 6b 65 20 74 68 65 20 6d 69 6e 20 65  s like the min e
13b6e 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20  mbedded payload 
13b6f 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 65 78 63 65  fraction.** exce
13b70 70 74 20 74 68 61 74 20 69 74 20 61 70 70 6c 69  pt that it appli
13b71 65 73 20 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73  es to leaf nodes
13b72 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74   in a LEAFDATA t
13b73 72 65 65 2e 20 20 54 68 65 20 6d 61 78 69 6d 75  ree.  The maximu
13b74 6d 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61  m.** payload fra
13b75 63 74 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46  ction for a LEAF
13b76 44 41 54 41 20 74 72 65 65 20 69 73 20 61 6c 77  DATA tree is alw
13b77 61 79 73 20 31 30 30 25 20 28 6f 72 20 32 35 35  ays 100% (or 255
13b78 29 20 61 6e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20  ) and it.** not 
13b79 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
13b7a 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45   header..**.** E
13b7b 61 63 68 20 62 74 72 65 65 20 70 61 67 65 73 20  ach btree pages 
13b7c 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20  is divided into 
13b7d 74 68 72 65 65 20 73 65 63 74 69 6f 6e 73 3a 20  three sections: 
13b7e 20 54 68 65 20 68 65 61 64 65 72 2c 20 74 68 65   The header, the
13b7f 0a 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  .** cell pointer
13b80 20 61 72 72 61 79 2c 20 61 6e 64 20 74 68 65 20   array, and the 
13b81 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
13b82 61 2e 20 20 50 61 67 65 20 31 20 61 6c 73 6f 20  a.  Page 1 also 
13b83 68 61 73 20 61 20 31 30 30 2d 62 79 74 65 0a 2a  has a 100-byte.*
13b84 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20 74 68  * file header th
13b85 61 74 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65  at occurs before
13b86 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
13b87 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d  ..**.**      |--
13b88 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a  --------------|.
13b89 2a 2a 20 20 20 20 20 20 7c 20 66 69 6c 65 20 68  **      | file h
13b8a 65 61 64 65 72 20 20 20 20 7c 20 20 20 31 30 30  eader    |   100
13b8b 20 62 79 74 65 73 2e 20 20 50 61 67 65 20 31 20   bytes.  Page 1 
13b8c 6f 6e 6c 79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d  only..**      |-
13b8d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c  ---------------|
13b8e 0a 2a 2a 20 20 20 20 20 20 7c 20 70 61 67 65 20  .**      | page 
13b8f 68 65 61 64 65 72 20 20 20 20 7c 20 20 20 38 20  header    |   8 
13b90 62 79 74 65 73 20 66 6f 72 20 6c 65 61 76 65 73  bytes for leaves
13b91 2e 20 20 31 32 20 62 79 74 65 73 20 66 6f 72 20  .  12 bytes for 
13b92 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a 2a  interior nodes.*
13b93 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
13b94 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
13b95 20 20 7c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72    | cell pointer
13b96 20 20 20 7c 20 20 20 7c 20 20 32 20 62 79 74 65     |   |  2 byte
13b97 73 20 70 65 72 20 63 65 6c 6c 2e 20 20 53 6f 72  s per cell.  Sor
13b98 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 20  ted order..**   
13b99 20 20 20 7c 20 61 72 72 61 79 20 20 20 20 20 20     | array      
13b9a 20 20 20 20 7c 20 20 20 7c 20 20 47 72 6f 77 73      |   |  Grows
13b9b 20 64 6f 77 6e 77 61 72 64 0a 2a 2a 20 20 20 20   downward.**    
13b9c 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20    |             
13b9d 20 20 20 7c 20 20 20 76 0a 2a 2a 20 20 20 20 20     |   v.**     
13b9e 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
13b9f 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e  --|.**      | un
13ba0 61 6c 6c 6f 63 61 74 65 64 20 20 20 20 7c 0a 2a  allocated    |.*
13ba1 2a 20 20 20 20 20 20 7c 20 73 70 61 63 65 20 20  *      | space  
13ba2 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20          |.**    
13ba3 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
13ba4 2d 2d 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20  ---|   ^  Grows 
13ba5 75 70 77 61 72 64 73 0a 2a 2a 20 20 20 20 20 20  upwards.**      
13ba6 7c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 20  | cell content  
13ba7 20 7c 20 20 20 7c 20 20 41 72 62 69 74 72 61 72   |   |  Arbitrar
13ba8 79 20 6f 72 64 65 72 20 69 6e 74 65 72 73 70 65  y order interspe
13ba9 72 73 65 64 20 77 69 74 68 20 66 72 65 65 62 6c  rsed with freebl
13baa 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 20  ocks..**      | 
13bab 61 72 65 61 20 20 20 20 20 20 20 20 20 20 20 7c  area           |
13bac 20 20 20 7c 20 20 61 6e 64 20 66 72 65 65 20 73     |  and free s
13bad 70 61 63 65 20 66 72 61 67 6d 65 6e 74 73 2e 0a  pace fragments..
13bae 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
13baf 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a  ---------|.**.**
13bb0 20 54 68 65 20 70 61 67 65 20 68 65 61 64 65 72   The page header
13bb1 73 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69  s looks like thi
13bb2 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45  s:.**.**   OFFSE
13bb3 54 20 20 20 53 49 5a 45 20 20 20 20 20 44 45 53  T   SIZE     DES
13bb4 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
13bb5 20 30 20 20 20 20 20 20 20 31 20 20 20 20 20 20   0       1      
13bb6 46 6c 61 67 73 2e 20 31 3a 20 69 6e 74 6b 65 79  Flags. 1: intkey
13bb7 2c 20 32 3a 20 7a 65 72 6f 64 61 74 61 2c 20 34  , 2: zerodata, 4
13bb8 3a 20 6c 65 61 66 64 61 74 61 2c 20 38 3a 20 6c  : leafdata, 8: l
13bb9 65 61 66 0a 2a 2a 20 20 20 20 20 20 31 20 20 20  eaf.**      1   
13bba 20 20 20 20 32 20 20 20 20 20 20 62 79 74 65 20      2      byte 
13bbb 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 66 69  offset to the fi
13bbc 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a  rst freeblock.**
13bbd 20 20 20 20 20 20 33 20 20 20 20 20 20 20 32 20        3       2 
13bbe 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 63       number of c
13bbf 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67  ells on this pag
13bc0 65 0a 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20  e.**      5     
13bc1 20 20 32 20 20 20 20 20 20 66 69 72 73 74 20 62    2      first b
13bc2 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  yte of the cell 
13bc3 63 6f 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20  content area.** 
13bc4 20 20 20 20 20 37 20 20 20 20 20 20 20 31 20 20       7       1  
13bc5 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 66 72      number of fr
13bc6 61 67 6d 65 6e 74 65 64 20 66 72 65 65 20 62 79  agmented free by
13bc7 74 65 73 0a 2a 2a 20 20 20 20 20 20 38 20 20 20  tes.**      8   
13bc8 20 20 20 20 34 20 20 20 20 20 20 52 69 67 68 74      4      Right
13bc9 20 63 68 69 6c 64 20 28 74 68 65 20 50 74 72 28   child (the Ptr(
13bca 4e 29 20 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74  N) value).  Omit
13bcb 74 65 64 20 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a  ted on leaves..*
13bcc 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64  *.** The flags d
13bcd 65 66 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74  efine the format
13bce 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 70   of this btree p
13bcf 61 67 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66  age.  The leaf f
13bd0 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a  lag means that.*
13bd1 2a 20 74 68 69 73 20 70 61 67 65 20 68 61 73 20  * this page has 
13bd2 6e 6f 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68  no children.  Th
13bd3 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20  e zerodata flag 
13bd4 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20  means that this 
13bd5 70 61 67 65 20 63 61 72 72 69 65 73 0a 2a 2a 20  page carries.** 
13bd6 6f 6e 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e 6f  only keys and no
13bd7 20 64 61 74 61 2e 20 20 54 68 65 20 69 6e 74 6b   data.  The intk
13bd8 65 79 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68  ey flag means th
13bd9 61 74 20 74 68 65 20 6b 65 79 20 69 73 20 61 20  at the key is a 
13bda 69 6e 74 65 67 65 72 0a 2a 2a 20 77 68 69 63 68  integer.** which
13bdb 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
13bdc 65 20 6b 65 79 20 73 69 7a 65 20 65 6e 74 72 79  e key size entry
13bdd 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 68 65 61   of the cell hea
13bde 64 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20  der rather than 
13bdf 69 6e 0a 2a 2a 20 74 68 65 20 70 61 79 6c 6f 61  in.** the payloa
13be0 64 20 61 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68  d area..**.** Th
13be1 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
13be2 72 72 61 79 20 62 65 67 69 6e 73 20 6f 6e 20 74  rray begins on t
13be3 68 65 20 66 69 72 73 74 20 62 79 74 65 20 61 66  he first byte af
13be4 74 65 72 20 74 68 65 20 70 61 67 65 20 68 65 61  ter the page hea
13be5 64 65 72 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c  der..** The cell
13be6 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 63   pointer array c
13be7 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 6f 72 20  ontains zero or 
13be8 6d 6f 72 65 20 32 2d 62 79 74 65 20 6e 75 6d 62  more 2-byte numb
13be9 65 72 73 20 77 68 69 63 68 20 61 72 65 0a 2a 2a  ers which are.**
13bea 20 6f 66 66 73 65 74 73 20 66 72 6f 6d 20 74 68   offsets from th
13beb 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
13bec 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 63  he page to the c
13bed 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74  ell content in t
13bee 68 65 20 63 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65  he cell.** conte
13bef 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20 63 65  nt area.  The ce
13bf0 6c 6c 20 70 6f 69 6e 74 65 72 73 20 6f 63 63 75  ll pointers occu
13bf1 72 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  r in sorted orde
13bf2 72 2e 20 20 54 68 65 20 73 79 73 74 65 6d 20 73  r.  The system s
13bf3 74 72 69 76 65 73 0a 2a 2a 20 74 6f 20 6b 65 65  trives.** to kee
13bf4 70 20 66 72 65 65 20 73 70 61 63 65 20 61 66 74  p free space aft
13bf5 65 72 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c  er the last cell
13bf6 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74   pointer so that
13bf7 20 6e 65 77 20 63 65 6c 6c 73 20 63 61 6e 0a 2a   new cells can.*
13bf8 2a 20 62 65 20 65 61 73 69 6c 79 20 61 64 64 65  * be easily adde
13bf9 64 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67  d without having
13bfa 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 74   to defragment t
13bfb 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43  he page..**.** C
13bfc 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73  ell content is s
13bfd 74 6f 72 65 64 20 61 74 20 74 68 65 20 76 65 72  tored at the ver
13bfe 79 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67  y end of the pag
13bff 65 20 61 6e 64 20 67 72 6f 77 73 20 74 6f 77 61  e and grows towa
13c00 72 64 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e  rd the.** beginn
13c01 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 2e  ing of the page.
13c02 0a 2a 2a 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70  .**.** Unused sp
13c03 61 63 65 20 77 69 74 68 69 6e 20 74 68 65 20 63  ace within the c
13c04 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
13c05 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e   is collected in
13c06 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  to a linked list
13c07 20 6f 66 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b   of.** freeblock
13c08 73 2e 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f  s.  Each freeblo
13c09 63 6b 20 69 73 20 61 74 20 6c 65 61 73 74 20 34  ck is at least 4
13c0a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20   bytes in size. 
13c0b 20 54 68 65 20 62 79 74 65 20 6f 66 66 73 65 74   The byte offset
13c0c 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74  .** to the first
13c0d 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 67 69   freeblock is gi
13c0e 76 65 6e 20 69 6e 20 74 68 65 20 68 65 61 64 65  ven in the heade
13c0f 72 2e 20 20 46 72 65 65 62 6c 6f 63 6b 73 20 6f  r.  Freeblocks o
13c10 63 63 75 72 20 69 6e 0a 2a 2a 20 69 6e 63 72 65  ccur in.** incre
13c11 61 73 69 6e 67 20 6f 72 64 65 72 2e 20 20 42 65  asing order.  Be
13c12 63 61 75 73 65 20 61 20 66 72 65 65 62 6c 6f 63  cause a freebloc
13c13 6b 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61  k must be at lea
13c14 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 69  st 4 bytes in si
13c15 7a 65 2c 0a 2a 2a 20 61 6e 79 20 67 72 6f 75 70  ze,.** any group
13c16 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 75   of 3 or fewer u
13c17 6e 75 73 65 64 20 62 79 74 65 73 20 69 6e 20 74  nused bytes in t
13c18 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
13c19 61 72 65 61 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65  area cannot.** e
13c1a 78 69 73 74 20 6f 6e 20 74 68 65 20 66 72 65 65  xist on the free
13c1b 62 6c 6f 63 6b 20 63 68 61 69 6e 2e 20 20 41 20  block chain.  A 
13c1c 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65  group of 3 or fe
13c1d 77 65 72 20 66 72 65 65 20 62 79 74 65 73 20 69  wer free bytes i
13c1e 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 66 72  s called.** a fr
13c1f 61 67 6d 65 6e 74 2e 20 20 54 68 65 20 74 6f 74  agment.  The tot
13c20 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  al number of byt
13c21 65 73 20 69 6e 20 61 6c 6c 20 66 72 61 67 6d 65  es in all fragme
13c22 6e 74 73 20 69 73 20 72 65 63 6f 72 64 65 64 2e  nts is recorded.
13c23 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20  .** in the page 
13c24 68 65 61 64 65 72 20 61 74 20 6f 66 66 73 65 74  header at offset
13c25 20 37 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a   7..**.**    SIZ
13c26 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e  E    DESCRIPTION
13c27 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42  .**      2     B
13c28 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68  yte offset of th
13c29 65 20 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b  e next freeblock
13c2a 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42  .**      2     B
13c2b 79 74 65 73 20 69 6e 20 74 68 69 73 20 66 72 65  ytes in this fre
13c2c 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c  eblock.**.** Cel
13c2d 6c 73 20 61 72 65 20 6f 66 20 76 61 72 69 61 62  ls are of variab
13c2e 6c 65 20 6c 65 6e 67 74 68 2e 20 20 43 65 6c 6c  le length.  Cell
13c2f 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  s are stored in 
13c30 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
13c31 20 61 72 65 61 20 61 74 0a 2a 2a 20 74 68 65 20   area at.** the 
13c32 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e  end of the page.
13c33 20 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68    Pointers to th
13c34 65 20 63 65 6c 6c 73 20 61 72 65 20 69 6e 20 74  e cells are in t
13c35 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
13c36 61 72 72 61 79 0a 2a 2a 20 74 68 61 74 20 69 6d  array.** that im
13c37 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77  mediately follow
13c38 73 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65  s the page heade
13c39 72 2e 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74  r.  Cells is not
13c3a 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20   necessarily.** 
13c3b 63 6f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e  contiguous or in
13c3c 20 6f 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c   order, but cell
13c3d 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f   pointers are co
13c3e 6e 74 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20  ntiguous and in 
13c3f 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c  order..**.** Cel
13c40 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73 20  l content makes 
13c41 75 73 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20  use of variable 
13c42 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e  length integers.
13c43 20 20 41 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20    A variable.** 
13c44 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 69  length integer i
13c45 73 20 31 20 74 6f 20 39 20 62 79 74 65 73 20 77  s 1 to 9 bytes w
13c46 68 65 72 65 20 74 68 65 20 6c 6f 77 65 72 20 37  here the lower 7
13c47 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 0a 2a   bits of each .*
13c48 2a 20 62 79 74 65 20 61 72 65 20 75 73 65 64 2e  * byte are used.
13c49 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 63 6f    The integer co
13c4a 6e 73 69 73 74 73 20 6f 66 20 61 6c 6c 20 62 79  nsists of all by
13c4b 74 65 73 20 74 68 61 74 20 68 61 76 65 20 62 69  tes that have bi
13c4c 74 20 38 20 73 65 74 20 61 6e 64 0a 2a 2a 20 74  t 8 set and.** t
13c4d 68 65 20 66 69 72 73 74 20 62 79 74 65 20 77 69  he first byte wi
13c4e 74 68 20 62 69 74 20 38 20 63 6c 65 61 72 2e 20  th bit 8 clear. 
13c4f 20 54 68 65 20 6d 6f 73 74 20 73 69 67 6e 69 66   The most signif
13c50 69 63 61 6e 74 20 62 79 74 65 20 6f 66 20 74 68  icant byte of th
13c51 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 70 70  e integer.** app
13c52 65 61 72 73 20 66 69 72 73 74 2e 20 20 41 20 76  ears first.  A v
13c53 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
13c54 6e 74 65 67 65 72 20 6d 61 79 20 6e 6f 74 20 62  nteger may not b
13c55 65 20 6d 6f 72 65 20 74 68 61 6e 20 39 20 62 79  e more than 9 by
13c56 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20  tes long..** As 
13c57 61 20 73 70 65 63 69 61 6c 20 63 61 73 65 2c 20  a special case, 
13c58 61 6c 6c 20 38 20 62 79 74 65 73 20 6f 66 20 74  all 8 bytes of t
13c59 68 65 20 39 74 68 20 62 79 74 65 20 61 72 65 20  he 9th byte are 
13c5a 75 73 65 64 20 61 73 20 64 61 74 61 2e 20 20 54  used as data.  T
13c5b 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 61 20  his.** allows a 
13c5c 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74  64-bit integer t
13c5d 6f 20 62 65 20 65 6e 63 6f 64 65 64 20 69 6e 20  o be encoded in 
13c5e 39 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  9 bytes..**.**  
13c5f 20 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20    0x00          
13c60 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f              beco
13c61 6d 65 73 20 20 30 78 30 30 30 30 30 30 30 30 0a  mes  0x00000000.
13c62 2a 2a 20 20 20 20 30 78 37 66 20 20 20 20 20 20  **    0x7f      
13c63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c64 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30  becomes  0x00000
13c65 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 31 20 30  07f.**    0x81 0
13c66 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20  x00             
13c67 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30      becomes  0x0
13c68 30 30 30 30 30 38 30 0a 2a 2a 20 20 20 20 30 78  0000080.**    0x
13c69 38 32 20 30 78 30 30 20 20 20 20 20 20 20 20 20  82 0x00         
13c6a 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20          becomes 
13c6b 20 30 78 30 30 30 30 30 31 30 30 0a 2a 2a 20 20   0x00000100.**  
13c6c 20 20 30 78 38 30 20 30 78 37 66 20 20 20 20 20    0x80 0x7f     
13c6d 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f              beco
13c6e 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a  mes  0x0000007f.
13c6f 2a 2a 20 20 20 20 30 78 38 61 20 30 78 39 31 20  **    0x8a 0x91 
13c70 30 78 64 31 20 30 78 61 63 20 30 78 37 38 20 20  0xd1 0xac 0x78  
13c71 62 65 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35  becomes  0x12345
13c72 36 37 38 0a 2a 2a 20 20 20 20 30 78 38 31 20 30  678.**    0x81 0
13c73 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78  x81 0x81 0x81 0x
13c74 30 31 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31  01  becomes  0x1
13c75 30 32 30 34 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61  0204081.**.** Va
13c76 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e  riable length in
13c77 74 65 67 65 72 73 20 61 72 65 20 75 73 65 64 20  tegers are used 
13c78 66 6f 72 20 72 6f 77 69 64 73 20 61 6e 64 20 74  for rowids and t
13c79 6f 20 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62 65  o hold the numbe
13c7a 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 6f 66  r of.** bytes of
13c7b 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 69 6e   key and data in
13c7c 20 61 20 62 74 72 65 65 20 63 65 6c 6c 2e 0a 2a   a btree cell..*
13c7d 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74  *.** The content
13c7e 20 6f 66 20 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73   of a cell looks
13c7f 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
13c80 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53  *    SIZE    DES
13c81 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
13c82 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62   4     Page numb
13c83 65 72 20 6f 66 20 74 68 65 20 6c 65 66 74 20 63  er of the left c
13c84 68 69 6c 64 2e 20 4f 6d 69 74 74 65 64 20 69 66  hild. Omitted if
13c85 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65   leaf flag is se
13c86 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20  t..**     var   
13c87 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
13c88 20 6f 66 20 64 61 74 61 2e 20 4f 6d 69 74 74 65   of data. Omitte
13c89 64 20 69 66 20 74 68 65 20 7a 65 72 6f 64 61 74  d if the zerodat
13c8a 61 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a  a flag is set..*
13c8b 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d  *     var    Num
13c8c 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
13c8d 6b 65 79 2e 20 4f 72 20 74 68 65 20 6b 65 79 20  key. Or the key 
13c8e 69 74 73 65 6c 66 20 69 66 20 69 6e 74 6b 65 79  itself if intkey
13c8f 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a   flag is set..**
13c90 20 20 20 20 20 20 2a 20 20 20 20 20 50 61 79 6c        *     Payl
13c91 6f 61 64 0a 2a 2a 20 20 20 20 20 20 34 20 20 20  oad.**      4   
13c92 20 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20    First page of 
13c93 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61  the overflow cha
13c94 69 6e 2e 20 20 4f 6d 69 74 74 65 64 20 69 66 20  in.  Omitted if 
13c95 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a  no overflow.**.*
13c96 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  * Overflow pages
13c97 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c   form a linked l
13c98 69 73 74 2e 20 20 45 61 63 68 20 70 61 67 65 20  ist.  Each page 
13c99 65 78 63 65 70 74 20 74 68 65 20 6c 61 73 74 20  except the last 
13c9a 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a  is completely.**
13c9b 20 66 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74   filled with dat
13c9c 61 20 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20  a (pagesize - 4 
13c9d 62 79 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73  bytes).  The las
13c9e 74 20 70 61 67 65 20 63 61 6e 20 68 61 76 65 20  t page can have 
13c9f 61 73 20 6c 69 74 74 6c 65 0a 2a 2a 20 61 73 20  as little.** as 
13ca0 31 20 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0a  1 byte of data..
13ca1 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20  **.**    SIZE   
13ca2 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20   DESCRIPTION.** 
13ca3 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20       4     Page 
13ca4 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 6f  number of next o
13ca5 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 2a 2a 20  verflow page.** 
13ca6 20 20 20 20 20 2a 20 20 20 20 20 44 61 74 61 0a       *     Data.
13ca7 2a 2a 0a 2a 2a 20 46 72 65 65 6c 69 73 74 20 70  **.** Freelist p
13ca8 61 67 65 73 20 63 6f 6d 65 20 69 6e 20 74 77 6f  ages come in two
13ca9 20 73 75 62 74 79 70 65 73 3a 20 74 72 75 6e 6b   subtypes: trunk
13caa 20 70 61 67 65 73 20 61 6e 64 20 6c 65 61 66 20   pages and leaf 
13cab 70 61 67 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66  pages.  The.** f
13cac 69 6c 65 20 68 65 61 64 65 72 20 70 6f 69 6e 74  ile header point
13cad 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69  s to the first i
13cae 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  n a linked list 
13caf 6f 66 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 20  of trunk page.  
13cb0 45 61 63 68 20 74 72 75 6e 6b 0a 2a 2a 20 70 61  Each trunk.** pa
13cb1 67 65 20 70 6f 69 6e 74 73 20 74 6f 20 6d 75 6c  ge points to mul
13cb2 74 69 70 6c 65 20 6c 65 61 66 20 70 61 67 65 73  tiple leaf pages
13cb3 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f  .  The content o
13cb4 66 20 61 20 6c 65 61 66 20 70 61 67 65 20 69 73  f a leaf page is
13cb5 0a 2a 2a 20 75 6e 73 70 65 63 69 66 69 65 64 2e  .** unspecified.
13cb6 20 20 41 20 74 72 75 6e 6b 20 70 61 67 65 20 6c    A trunk page l
13cb7 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ooks like this:.
13cb8 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20  **.**    SIZE   
13cb9 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20   DESCRIPTION.** 
13cba 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20       4     Page 
13cbb 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 74  number of next t
13cbc 72 75 6e 6b 20 70 61 67 65 0a 2a 2a 20 20 20 20  runk page.**    
13cbd 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f    4     Number o
13cbe 66 20 6c 65 61 66 20 70 6f 69 6e 74 65 72 73 20  f leaf pointers 
13cbf 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20  on this page.** 
13cc0 20 20 20 20 20 2a 20 20 20 20 20 7a 65 72 6f 20       *     zero 
13cc1 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 6e 75  or more pages nu
13cc2 6d 62 65 72 73 20 6f 66 20 6c 65 61 76 65 73 0a  mbers of leaves.
13cc3 2a 2f 0a 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c  */.../* The foll
13cc4 6f 77 69 6e 67 20 76 61 6c 75 65 20 69 73 20 74  owing value is t
13cc5 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20  he maximum cell 
13cc6 73 69 7a 65 20 61 73 73 75 6d 69 6e 67 20 61 20  size assuming a 
13cc7 6d 61 78 69 6d 75 6d 20 70 61 67 65 0a 2a 2a 20  maximum page.** 
13cc8 73 69 7a 65 20 67 69 76 65 20 61 62 6f 76 65 2e  size give above.
13cc9 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43  .*/.#define MX_C
13cca 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 20 28  ELL_SIZE(pBt)  (
13ccb 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29  pBt->pageSize-8)
13ccc 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  ../* The maximum
13ccd 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   number of cells
13cce 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67   on a single pag
13ccf 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
13cd0 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61 73 73 75  e.  This.** assu
13cd1 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65  mes a minimum ce
13cd2 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20 62 79 74  ll size of 6 byt
13cd3 65 73 20 20 28 34 20 62 79 74 65 73 20 66 6f 72  es  (4 bytes for
13cd4 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66   the cell itself
13cd5 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79 74 65 73  .** plus 2 bytes
13cd6 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74   for the index t
13cd7 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68  o the cell in th
13cd8 65 20 70 61 67 65 20 68 65 61 64 65 72 29 2e 20  e page header). 
13cd9 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63   Such.** small c
13cda 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20 72 61 72  ells will be rar
13cdb 65 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 20  e, but they are 
13cdc 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65  possible..*/.#de
13cdd 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28 70 42 74  fine MX_CELL(pBt
13cde 29 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a  ) ((pBt->pageSiz
13cdf 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77  e-8)/6)../* Forw
13ce0 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
13ce1 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75   */.typedef stru
13ce2 63 74 20 4d 65 6d 50 61 67 65 20 4d 65 6d 50 61  ct MemPage MemPa
13ce3 67 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ge;.typedef stru
13ce4 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b  ct BtLock BtLock
13ce5 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  ;../*.** This is
13ce6 20 61 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20   a magic string 
13ce7 74 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20  that appears at 
13ce8 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
13ce9 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65   every.** SQLite
13cea 20 64 61 74 61 62 61 73 65 20 69 6e 20 6f 72 64   database in ord
13ceb 65 72 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74  er to identify t
13cec 68 65 20 66 69 6c 65 20 61 73 20 61 20 72 65 61  he file as a rea
13ced 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  l database..**.*
13cee 2a 20 59 6f 75 20 63 61 6e 20 63 68 61 6e 67 65  * You can change
13cef 20 74 68 69 73 20 76 61 6c 75 65 20 61 74 20 63   this value at c
13cf0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 73  ompile-time by s
13cf1 70 65 63 69 66 79 69 6e 67 20 61 0a 2a 2a 20 2d  pecifying a.** -
13cf2 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41  DSQLITE_FILE_HEA
13cf3 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65  DER="..." on the
13cf4 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
13cf5 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20  d-line.  The.** 
13cf6 68 65 61 64 65 72 20 6d 75 73 74 20 62 65 20 65  header must be e
13cf7 78 61 63 74 6c 79 20 31 36 20 62 79 74 65 73 20  xactly 16 bytes 
13cf8 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 7a 65  including the ze
13cf9 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f  ro-terminator so
13cfa 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69  .** the string i
13cfb 74 73 65 6c 66 20 73 68 6f 75 6c 64 20 62 65 20  tself should be 
13cfc 31 35 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f  15 characters lo
13cfd 6e 67 2e 20 20 49 66 20 79 6f 75 20 63 68 61 6e  ng.  If you chan
13cfe 67 65 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72  ge.** the header
13cff 2c 20 74 68 65 6e 20 79 6f 75 72 20 63 75 73 74  , then your cust
13d00 6f 6d 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20  om library will 
13d01 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72  not be able to r
13d02 65 61 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  ead .** database
13d03 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74  s generated by t
13d04 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c  he standard tool
13d05 73 20 61 6e 64 20 74 68 65 20 73 74 61 6e 64 61  s and the standa
13d06 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c  rd tools.** will
13d07 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20   not be able to 
13d08 72 65 61 64 20 64 61 74 61 62 61 73 65 73 20 63  read databases c
13d09 72 65 61 74 65 64 20 62 79 20 79 6f 75 72 20 63  reated by your c
13d0a 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 2e 0a 2a  ustom library..*
13d0b 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
13d0c 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 2f 2a 20  _FILE_HEADER /* 
13d0d 31 32 33 34 35 36 37 38 39 20 31 32 33 34 35 36  123456789 123456
13d0e 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 53 51   */.#  define SQ
13d0f 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52  LITE_FILE_HEADER
13d10 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20   "SQLite format 
13d11 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  3".#endif../*.**
13d12 20 50 61 67 65 20 74 79 70 65 20 66 6c 61 67 73   Page type flags
13d13 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f 6d 62 69  .  An ORed combi
13d14 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  nation of these 
13d15 66 6c 61 67 73 20 61 70 70 65 61 72 20 61 73 20  flags appear as 
13d16 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 62 79 74  the.** first byt
13d17 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20 69 6d 61  e of on-disk ima
13d18 67 65 20 6f 66 20 65 76 65 72 79 20 42 54 72 65  ge of every BTre
13d19 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69  e page..*/.#defi
13d1a 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59 20 20 20  ne PTF_INTKEY   
13d1b 20 30 78 30 31 0a 23 64 65 66 69 6e 65 20 50 54   0x01.#define PT
13d1c 46 5f 5a 45 52 4f 44 41 54 41 20 20 30 78 30 32  F_ZERODATA  0x02
13d1d 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41  .#define PTF_LEA
13d1e 46 44 41 54 41 20 20 30 78 30 34 0a 23 64 65 66  FDATA  0x04.#def
13d1f 69 6e 65 20 50 54 46 5f 4c 45 41 46 20 20 20 20  ine PTF_LEAF    
13d20 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73    0x08../*.** As
13d21 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68   each page of th
13d22 65 20 66 69 6c 65 20 69 73 20 6c 6f 61 64 65 64  e file is loaded
13d23 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e   into memory, an
13d24 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
13d25 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74   following.** st
13d26 72 75 63 74 75 72 65 20 69 73 20 61 70 70 65 6e  ructure is appen
13d27 64 65 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ded and initiali
13d28 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68  zed to zero.  Th
13d29 69 73 20 73 74 72 75 63 74 75 72 65 20 73 74 6f  is structure sto
13d2a 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  res.** informati
13d2b 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 70 61 67  on about the pag
13d2c 65 20 74 68 61 74 20 69 73 20 64 65 63 6f 64 65  e that is decode
13d2d 64 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 66  d from the raw f
13d2e 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  ile page..**.** 
13d2f 54 68 65 20 70 50 61 72 65 6e 74 20 66 69 65 6c  The pParent fiel
13d30 64 20 70 6f 69 6e 74 73 20 62 61 63 6b 20 74 6f  d points back to
13d31 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
13d32 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 75  .  This allows u
13d33 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20 75 70 20  s to.** walk up 
13d34 74 68 65 20 42 54 72 65 65 20 66 72 6f 6d 20 61  the BTree from a
13d35 6e 79 20 6c 65 61 66 20 74 6f 20 74 68 65 20 72  ny leaf to the r
13d36 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75 73 74 20  oot.  Care must 
13d37 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75  be taken to.** u
13d38 6e 72 65 66 28 29 20 74 68 65 20 70 61 72 65 6e  nref() the paren
13d39 74 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20 77  t page pointer w
13d3a 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 69 73  hen this page is
13d3b 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 66 65 72   no longer refer
13d3c 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65 20 70 61  enced..** The pa
13d3d 67 65 44 65 73 74 72 75 63 74 6f 72 28 29 20 72  geDestructor() r
13d3e 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 74  outine handles t
13d3f 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a  hat chore..**.**
13d40 20 41 63 63 65 73 73 20 74 6f 20 61 6c 6c 20 66   Access to all f
13d41 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74  ields of this st
13d42 72 75 63 74 75 72 65 20 69 73 20 63 6f 6e 74 72  ructure is contr
13d43 6f 6c 6c 65 64 20 62 79 20 74 68 65 20 6d 75 74  olled by the mut
13d44 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  ex.** stored in 
13d45 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e 6d 75 74  MemPage.pBt->mut
13d46 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65  ex..*/.struct Me
13d47 6d 50 61 67 65 20 7b 0a 20 20 75 38 20 69 73 49  mPage {.  u8 isI
13d48 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  nit;           /
13d49 2a 20 54 72 75 65 20 69 66 20 70 72 65 76 69 6f  * True if previo
13d4a 75 73 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64  usly initialized
13d4b 2e 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 21  . MUST BE FIRST!
13d4c 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c   */.  u8 nOverfl
13d4d 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ow;        /* Nu
13d4e 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77  mber of overflow
13d4f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20   cell bodies in 
13d50 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20  aCell[] */.  u8 
13d51 69 6e 74 4b 65 79 3b 20 20 20 20 20 20 20 20 20  intKey;         
13d52 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74    /* True if int
13d53 6b 65 79 20 66 6c 61 67 20 69 73 20 73 65 74 20  key flag is set 
13d54 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b 20 20 20  */.  u8 leaf;   
13d55 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
13d56 65 20 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69  e if leaf flag i
13d57 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 68 61  s set */.  u8 ha
13d58 73 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  sData;          
13d59 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
13d5a 70 61 67 65 20 73 74 6f 72 65 73 20 64 61 74 61  page stores data
13d5b 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73   */.  u8 hdrOffs
13d5c 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30  et;        /* 10
13d5d 30 20 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30  0 for page 1.  0
13d5e 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20   otherwise */.  
13d5f 75 38 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b  u8 childPtrSize;
13d60 20 20 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61       /* 0 if lea
13d61 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66  f==1.  4 if leaf
13d62 3d 3d 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78  ==0 */.  u16 max
13d63 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a  Local;        /*
13d64 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65   Copy of BtShare
13d65 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74  d.maxLocal or Bt
13d66 53 68 61 72 65 64 2e 6d 61 78 4c 65 61 66 20 2a  Shared.maxLeaf *
13d67 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c  /.  u16 minLocal
13d68 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79  ;        /* Copy
13d69 20 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 69 6e   of BtShared.min
13d6a 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65  Local or BtShare
13d6b 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 20 75  d.minLeaf */.  u
13d6c 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20  16 cellOffset;  
13d6d 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
13d6e 61 44 61 74 61 20 6f 66 20 66 69 72 73 74 20 63  aData of first c
13d6f 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ell pointer */. 
13d70 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20   u16 nFree;     
13d71 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
13d72 6f 66 20 66 72 65 65 20 62 79 74 65 73 20 6f 6e  of free bytes on
13d73 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75   the page */.  u
13d74 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20  16 nCell;       
13d75 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
13d76 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70   cells on this p
13d77 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 20 6f  age, local and o
13d78 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20 6d 61 73  vfl */.  u16 mas
13d79 6b 50 61 67 65 3b 20 20 20 20 20 20 20 20 2f 2a  kPage;        /*
13d7a 20 4d 61 73 6b 20 66 6f 72 20 70 61 67 65 20 6f   Mask for page o
13d7b 66 66 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63  ffset */.  struc
13d7c 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 7b 20 20 20  t _OvflCell {   
13d7d 2f 2a 20 43 65 6c 6c 73 20 74 68 61 74 20 77 69  /* Cells that wi
13d7e 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 44  ll not fit on aD
13d7f 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20  ata[] */.    u8 
13d80 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20  *pCell;         
13d81 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20   /* Pointers to 
13d82 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20  the body of the 
13d83 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f  overflow cell */
13d84 0a 20 20 20 20 75 31 36 20 69 64 78 3b 20 20 20  .    u16 idx;   
13d85 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65           /* Inse
13d86 72 74 20 74 68 69 73 20 63 65 6c 6c 20 62 65 66  rt this cell bef
13d87 6f 72 65 20 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f  ore idx-th non-o
13d88 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a  verflow cell */.
13d89 20 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20    } aOvfl[5];.  
13d8a 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
13d8b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
13d8c 74 6f 20 42 74 53 68 61 72 65 64 20 74 68 61 74  to BtShared that
13d8d 20 74 68 69 73 20 70 61 67 65 20 69 73 20 70 61   this page is pa
13d8e 72 74 20 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61  rt of */.  u8 *a
13d8f 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
13d90 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 69  /* Pointer to di
13d91 73 6b 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20  sk image of the 
13d92 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 44  page data */.  D
13d93 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20  bPage *pDbPage; 
13d94 20 20 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67      /* Pager pag
13d95 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67  e handle */.  Pg
13d96 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20  no pgno;        
13d97 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
13d98 72 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 20  r for this page 
13d99 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
13d9a 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65   in-memory image
13d9b 20 6f 66 20 61 20 64 69 73 6b 20 70 61 67 65 20   of a disk page 
13d9c 68 61 73 20 74 68 65 20 61 75 78 69 6c 69 61 72  has the auxiliar
13d9d 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70  y information ap
13d9e 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65  pended.** to the
13d9f 20 65 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a   end.  EXTRA_SIZ
13da0 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  E is the number 
13da1 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  of bytes of spac
13da2 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64  e needed to hold
13da3 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 61 20 69  .** that extra i
13da4 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23  nformation..*/.#
13da5 64 65 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a  define EXTRA_SIZ
13da6 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65  E sizeof(MemPage
13da7 29 0a 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61  )../* A Btree ha
13da8 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74  ndle.**.** A dat
13da9 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
13daa 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e   contains a poin
13dab 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e  ter to an instan
13dac 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 62  ce of.** this ob
13dad 6a 65 63 74 20 66 6f 72 20 65 76 65 72 79 20 64  ject for every d
13dae 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 68 61  atabase file tha
13daf 74 20 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 20  t it has open.  
13db0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  This structure.*
13db1 2a 20 69 73 20 6f 70 61 71 75 65 20 74 6f 20 74  * is opaque to t
13db2 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
13db3 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 74  ection.  The dat
13db4 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
13db5 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 74   cannot.** see t
13db6 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20  he internals of 
13db7 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61  this structure a
13db8 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69  nd only deals wi
13db9 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a  th pointers to.*
13dba 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  * this structure
13dbb 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65  ..**.** For some
13dbc 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2c   database files,
13dbd 20 74 68 65 20 73 61 6d 65 20 75 6e 64 65 72 6c   the same underl
13dbe 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 61  ying database ca
13dbf 63 68 65 20 6d 69 67 68 74 20 62 65 20 0a 2a 2a  che might be .**
13dc0 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e 20   shared between 
13dc1 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 74  multiple connect
13dc2 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 63  ions.  In that c
13dc3 61 73 65 2c 20 65 61 63 68 20 63 6f 6e 74 65 63  ase, each contec
13dc4 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 6f  tion.** has it o
13dc5 77 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68  wn pointer to th
13dc6 69 73 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20  is object.  But 
13dc7 65 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66  each instance of
13dc8 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20   this object.** 
13dc9 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61  points to the sa
13dca 6d 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  me BtShared obje
13dcb 63 74 2e 20 20 54 68 65 20 64 61 74 61 62 61 73  ct.  The databas
13dcc 65 20 63 61 63 68 65 20 61 6e 64 20 74 68 65 0a  e cache and the.
13dcd 2a 2a 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69  ** schema associ
13dce 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61  ated with the da
13dcf 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20  tabase file are 
13dd0 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 69  all contained wi
13dd1 74 68 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 68  thin.** the BtSh
13dd2 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a  ared object..**.
13dd3 2a 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e  ** All fields in
13dd4 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
13dd5 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64  are accessed und
13dd6 65 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78  er sqlite3.mutex
13dd7 2e 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69  ..** The pBt poi
13dd8 6e 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20  nter itself may 
13dd9 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 77  not be changed w
13dda 68 69 6c 65 20 74 68 65 72 65 20 65 78 69 73 74  hile there exist
13ddb 73 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 6e  s cursors .** in
13ddc 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20   the referenced 
13ddd 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70 6f  BtShared that po
13dde 69 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69 73  int back to this
13ddf 20 42 74 72 65 65 20 73 69 6e 63 65 20 74 68 6f   Btree since tho
13de0 73 65 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 61  se.** cursors ha
13de1 76 65 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 6f  ve to do go thro
13de2 75 67 68 20 74 68 69 73 20 42 74 72 65 65 20 74  ugh this Btree t
13de3 6f 20 66 69 6e 64 20 74 68 65 69 72 20 42 74 53  o find their BtS
13de4 68 61 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65  hared and.** the
13de5 79 20 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 69  y often do so wi
13de6 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 71  thout holding sq
13de7 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a  lite3.mutex..*/.
13de8 73 74 72 75 63 74 20 42 74 72 65 65 20 7b 0a 20  struct Btree {. 
13de9 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
13dea 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
13deb 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  ase connection h
13dec 6f 6c 64 69 6e 67 20 74 68 69 73 20 62 74 72 65  olding this btre
13ded 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  e */.  BtShared 
13dee 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 61  *pBt;     /* Sha
13def 72 61 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f 66  rable content of
13df0 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20   this btree */. 
13df1 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20   u8 inTrans;    
13df2 20 20 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e      /* TRANS_NON
13df3 45 2c 20 54 52 41 4e 53 5f 52 45 41 44 20 6f 72  E, TRANS_READ or
13df4 20 54 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f 0a   TRANS_WRITE */.
13df5 20 20 75 38 20 73 68 61 72 61 62 6c 65 3b 20 20    u8 sharable;  
13df6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
13df7 77 65 20 63 61 6e 20 73 68 61 72 65 20 70 42 74  we can share pBt
13df8 20 77 69 74 68 20 61 6e 6f 74 68 65 72 20 64 62   with another db
13df9 20 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 3b   */.  u8 locked;
13dfa 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
13dfb 20 69 66 20 64 62 20 63 75 72 72 65 6e 74 6c 79   if db currently
13dfc 20 68 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 20   has pBt locked 
13dfd 2a 2f 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f 4c  */.  int wantToL
13dfe 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ock;    /* Numbe
13dff 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c  r of nested call
13e00 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  s to sqlite3Btre
13e01 65 45 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69 6e  eEnter() */.  in
13e02 74 20 6e 42 61 63 6b 75 70 3b 20 20 20 20 20 20  t nBackup;      
13e03 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61   /* Number of ba
13e04 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 73 20  ckup operations 
13e05 72 65 61 64 69 6e 67 20 74 68 69 73 20 62 74 72  reading this btr
13e06 65 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70  ee */.  Btree *p
13e07 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69  Next;      /* Li
13e08 73 74 20 6f 66 20 6f 74 68 65 72 20 73 68 61 72  st of other shar
13e09 61 62 6c 65 20 42 74 72 65 65 73 20 66 72 6f 6d  able Btrees from
13e0a 20 74 68 65 20 73 61 6d 65 20 64 62 20 2a 2f 0a   the same db */.
13e0b 20 20 42 74 72 65 65 20 2a 70 50 72 65 76 3b 20    Btree *pPrev; 
13e0c 20 20 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f 69       /* Back poi
13e0d 6e 74 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65  nter of the same
13e0e 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   list */.};../*.
13e0f 2a 2a 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73  ** Btree.inTrans
13e10 20 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66   may take one of
13e11 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   the following v
13e12 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  alues..**.** If 
13e13 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20  the shared-data 
13e14 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61  extension is ena
13e15 62 6c 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20  bled, there may 
13e16 62 65 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72  be multiple user
13e17 73 0a 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65  s.** of the Btre
13e18 65 20 73 74 72 75 63 74 75 72 65 2e 20 41 74 20  e structure. At 
13e19 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73  most one of thes
13e1a 65 20 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69  e may open a wri
13e1b 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a  te transaction,.
13e1c 2a 2a 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65  ** but any numbe
13e1d 72 20 6d 61 79 20 68 61 76 65 20 61 63 74 69 76  r may have activ
13e1e 65 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69  e read transacti
13e1f 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ons..*/.#define 
13e20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64  TRANS_NONE  0.#d
13e21 65 66 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44  efine TRANS_READ
13e22 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e    1.#define TRAN
13e23 53 5f 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a  S_WRITE 2../*.**
13e24 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
13e25 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72  this object repr
13e26 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20  esents a single 
13e27 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
13e28 2a 20 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64  * .** A single d
13e29 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e  atabase file can
13e2a 20 62 65 20 69 6e 20 75 73 65 20 61 73 20 74 68   be in use as th
13e2b 65 20 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74  e same time by t
13e2c 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61  wo.** or more da
13e2d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
13e2e 6e 73 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72  ns.  When two or
13e2f 20 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e   more connection
13e30 73 20 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67  s are.** sharing
13e31 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
13e32 73 65 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f  se file, each co
13e33 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20  nnection has it 
13e34 6f 77 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42  own.** private B
13e35 74 72 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20  tree object for 
13e36 74 68 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63  the file and eac
13e37 68 20 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65  h of those Btree
13e38 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74  s points.** to t
13e39 68 69 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64  his one BtShared
13e3a 20 6f 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72   object.  BtShar
13e3b 65 64 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e  ed.nRef is the n
13e3c 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e  umber of.** conn
13e3d 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c  ections currentl
13e3e 79 20 73 68 61 72 69 6e 67 20 74 68 69 73 20 64  y sharing this d
13e3f 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
13e40 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68  .** Fields in th
13e41 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  is structure are
13e42 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20   accessed under 
13e43 74 68 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74  the BtShared.mut
13e44 65 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63  ex.** mutex, exc
13e45 65 70 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64  ept for nRef and
13e46 20 70 4e 65 78 74 20 77 68 69 63 68 20 61 72 65   pNext which are
13e47 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20   accessed under 
13e48 74 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51  the.** global SQ
13e49 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
13e4a 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20  C_MASTER mutex. 
13e4b 20 54 68 65 20 70 50 61 67 65 72 20 66 69 65 6c   The pPager fiel
13e4c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20  d.** may not be 
13e4d 6d 6f 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74  modified once it
13e4e 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65   is initially se
13e4f 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65  t as long as nRe
13e50 66 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68  f>0..** The pSch
13e51 65 6d 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65  ema field may be
13e52 20 73 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20   set once under 
13e53 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61  BtShared.mutex a
13e54 6e 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72  nd.** thereafter
13e55 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73   is unchanged as
13e56 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e   long as nRef>0.
13e57 0a 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67  .**.** isPending
13e58 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42  :.**.**   If a B
13e59 74 53 68 61 72 65 64 20 63 6c 69 65 6e 74 20 66  tShared client f
13e5a 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61  ails to obtain a
13e5b 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61   write-lock on a
13e5c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 74   database.**   t
13e5d 61 62 6c 65 20 28 62 65 63 61 75 73 65 20 74 68  able (because th
13e5e 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f  ere exists one o
13e5f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b  r more read-lock
13e60 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c  s on the table),
13e61 0a 2a 2a 20 20 20 74 68 65 20 73 68 61 72 65 64  .**   the shared
13e62 2d 63 61 63 68 65 20 65 6e 74 65 72 73 20 27 70  -cache enters 'p
13e63 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61  ending-lock' sta
13e64 74 65 20 61 6e 64 20 69 73 50 65 6e 64 69 6e 67  te and isPending
13e65 20 69 73 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20   is.**   set to 
13e66 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68  true..**.**   Th
13e67 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c  e shared-cache l
13e68 65 61 76 65 73 20 74 68 65 20 27 70 65 6e 64 69  eaves the 'pendi
13e69 6e 67 20 6c 6f 63 6b 27 20 73 74 61 74 65 20 77  ng lock' state w
13e6a 68 65 6e 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a  hen either of.**
13e6b 20 20 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67     the following
13e6c 20 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20   occur:.**.**   
13e6d 20 20 31 29 20 54 68 65 20 63 75 72 72 65 6e 74    1) The current
13e6e 20 77 72 69 74 65 72 20 28 42 74 53 68 61 72 65   writer (BtShare
13e6f 64 2e 70 57 72 69 74 65 72 29 20 63 6f 6e 63 6c  d.pWriter) concl
13e70 75 64 65 73 20 69 74 73 20 74 72 61 6e 73 61 63  udes its transac
13e71 74 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20  tion, OR.**     
13e72 32 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  2) The number of
13e73 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 6f   locks held by o
13e74 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  ther connections
13e75 20 64 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a   drops to zero..
13e76 2a 2a 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e  **.**   while in
13e77 20 74 68 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f   the 'pending-lo
13e78 63 6b 27 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f  ck' state, no co
13e79 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 73 74 61  nnection may sta
13e7a 72 74 20 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72  rt a new.**   tr
13e7b 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
13e7c 20 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20     This feature 
13e7d 69 73 20 69 6e 63 6c 75 64 65 64 20 74 6f 20 68  is included to h
13e7e 65 6c 70 20 70 72 65 76 65 6e 74 20 77 72 69 74  elp prevent writ
13e7f 65 72 2d 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a  er-starvation..*
13e80 2f 0a 73 74 72 75 63 74 20 42 74 53 68 61 72 65  /.struct BtShare
13e81 64 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  d {.  Pager *pPa
13e82 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ger;        /* T
13e83 68 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f  he page cache */
13e84 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
13e85 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
13e86 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
13e87 63 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20  currently using 
13e88 74 68 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20  this Btree */.  
13e89 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  BtCursor *pCurso
13e8a 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20  r;    /* A list 
13e8b 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73  of all open curs
13e8c 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  ors */.  MemPage
13e8d 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f   *pPage1;      /
13e8e 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20  * First page of 
13e8f 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
13e90 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20    u8 readOnly;  
13e91 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
13e92 69 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  if the underlyin
13e93 67 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e  g file is readon
13e94 6c 79 20 2a 2f 0a 20 20 75 38 20 70 61 67 65 53  ly */.  u8 pageS
13e95 69 7a 65 46 69 78 65 64 3b 20 20 20 20 20 2f 2a  izeFixed;     /*
13e96 20 54 72 75 65 20 69 66 20 74 68 65 20 70 61 67   True if the pag
13e97 65 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f  e size can no lo
13e98 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 20  nger be changed 
13e99 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
13e9a 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
13e9b 4d 0a 20 20 75 38 20 61 75 74 6f 56 61 63 75 75  M.  u8 autoVacuu
13e9c 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  m;        /* Tru
13e9d 65 20 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d  e if auto-vacuum
13e9e 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20   is enabled */. 
13e9f 20 75 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20   u8 incrVacuum; 
13ea0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
13ea1 66 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73  f incr-vacuum is
13ea2 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64   enabled */.#end
13ea3 69 66 0a 20 20 75 31 36 20 70 61 67 65 53 69 7a  if.  u16 pageSiz
13ea4 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f  e;         /* To
13ea5 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  tal number of by
13ea6 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f  tes on a page */
13ea7 0a 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a  .  u16 usableSiz
13ea8 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  e;       /* Numb
13ea9 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74  er of usable byt
13eaa 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20  es on each page 
13eab 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61  */.  u16 maxLoca
13eac 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  l;         /* Ma
13ead 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c  ximum local payl
13eae 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44  oad in non-LEAFD
13eaf 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20  ATA tables */.  
13eb0 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20  u16 minLocal;   
13eb1 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d        /* Minimum
13eb2 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69   local payload i
13eb3 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74  n non-LEAFDATA t
13eb4 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d  ables */.  u16 m
13eb5 61 78 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20  axLeaf;         
13eb6 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61   /* Maximum loca
13eb7 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c  l payload in a L
13eb8 45 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f  EAFDATA table */
13eb9 0a 20 20 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20  .  u16 minLeaf; 
13eba 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69           /* Mini
13ebb 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61  mum local payloa
13ebc 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20  d in a LEAFDATA 
13ebd 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e  table */.  u8 in
13ebe 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20  Transaction;    
13ebf 20 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20   /* Transaction 
13ec0 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  state */.  int n
13ec1 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20  Transaction;    
13ec2 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70   /* Number of op
13ec3 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  en transactions 
13ec4 28 72 65 61 64 20 2b 20 77 72 69 74 65 29 20 2a  (read + write) *
13ec5 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 68 65 6d  /.  void *pSchem
13ec6 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  a;        /* Poi
13ec7 6e 74 65 72 20 74 6f 20 73 70 61 63 65 20 61 6c  nter to space al
13ec8 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74  located by sqlit
13ec9 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 20  e3BtreeSchema() 
13eca 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65  */.  void (*xFre
13ecb 65 53 63 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b  eSchema)(void*);
13ecc 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20    /* Destructor 
13ecd 66 6f 72 20 42 74 53 68 61 72 65 64 2e 70 53 63  for BtShared.pSc
13ece 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65  hema */.  sqlite
13ecf 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20  3_mutex *mutex; 
13ed0 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65  /* Non-recursive
13ed1 20 6d 75 74 65 78 20 72 65 71 75 69 72 65 64 20   mutex required 
13ed2 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 73  to access this s
13ed3 74 72 75 63 74 20 2a 2f 0a 20 20 42 69 74 76 65  truct */.  Bitve
13ed4 63 20 2a 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20  c *pHasContent; 
13ed5 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73   /* Set of pages
13ed6 20 6d 6f 76 65 64 20 74 6f 20 66 72 65 65 2d 6c   moved to free-l
13ed7 69 73 74 20 74 68 69 73 20 74 72 61 6e 73 61 63  ist this transac
13ed8 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20  tion */.#ifndef 
13ed9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
13eda 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e  ED_CACHE.  int n
13edb 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
13edc 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
13edd 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73  ferences to this
13ede 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
13edf 42 74 53 68 61 72 65 64 20 2a 70 4e 65 78 74 3b  BtShared *pNext;
13ee0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e        /* Next on
13ee1 20 61 20 6c 69 73 74 20 6f 66 20 73 68 61 72 61   a list of shara
13ee2 62 6c 65 20 42 74 53 68 61 72 65 64 20 73 74 72  ble BtShared str
13ee3 75 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b  ucts */.  BtLock
13ee4 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20   *pLock;        
13ee5 2f 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73  /* List of locks
13ee6 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 73 68   held on this sh
13ee7 61 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63  ared-btree struc
13ee8 74 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 57  t */.  Btree *pW
13ee9 72 69 74 65 72 3b 20 20 20 20 20 20 20 2f 2a 20  riter;       /* 
13eea 42 74 72 65 65 20 77 69 74 68 20 63 75 72 72 65  Btree with curre
13eeb 6e 74 6c 79 20 6f 70 65 6e 20 77 72 69 74 65 20  ntly open write 
13eec 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
13eed 20 75 38 20 69 73 45 78 63 6c 75 73 69 76 65 3b   u8 isExclusive;
13eee 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
13eef 66 20 70 57 72 69 74 65 72 20 68 61 73 20 61 6e  f pWriter has an
13ef0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
13ef1 6f 6e 20 74 68 65 20 64 62 20 2a 2f 0a 20 20 75  on the db */.  u
13ef2 38 20 69 73 50 65 6e 64 69 6e 67 3b 20 20 20 20  8 isPending;    
13ef3 20 20 20 20 20 2f 2a 20 49 66 20 77 61 69 74 69       /* If waiti
13ef4 6e 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b  ng for read-lock
13ef5 73 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65  s to clear */.#e
13ef6 6e 64 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53  ndif.  u8 *pTmpS
13ef7 70 61 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  pace;        /* 
13ef8 42 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a  BtShared.pageSiz
13ef9 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  e bytes of space
13efa 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a   for tmp use */.
13efb 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
13efc 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
13efd 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
13efe 20 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64   is used to hold
13eff 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
13f00 61 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54  about a cell.  T
13f01 68 65 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28  he parseCellPtr(
13f02 29 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73  ) function fills
13f03 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75   in this structu
13f04 72 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69  re.** based on i
13f05 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61  nformation extra
13f06 63 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20  ct from the raw 
13f07 64 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79  disk page..*/.ty
13f08 70 65 64 65 66 20 73 74 72 75 63 74 20 43 65 6c  pedef struct Cel
13f09 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a  lInfo CellInfo;.
13f0a 73 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20  struct CellInfo 
13f0b 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20  {.  u8 *pCell;  
13f0c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
13f0d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65   the start of ce
13f0e 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20  ll content */.  
13f0f 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f  i64 nKey;      /
13f10 2a 20 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e  * The key for IN
13f11 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20  TKEY tables, or 
13f12 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
13f13 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20  in key */.  u32 
13f14 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75  nData;     /* Nu
13f15 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
13f16 20 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e   data */.  u32 n
13f17 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74  Payload;  /* Tot
13f18 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79  al amount of pay
13f19 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48  load */.  u16 nH
13f1a 65 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65  eader;   /* Size
13f1b 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   of the cell con
13f1c 74 65 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62  tent header in b
13f1d 79 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c  ytes */.  u16 nL
13f1e 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75  ocal;    /* Amou
13f1f 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65  nt of payload he
13f20 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20  ld locally */.  
13f21 75 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f  u16 iOverflow; /
13f22 2a 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72  * Offset to over
13f23 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72  flow page number
13f24 2e 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76  .  Zero if no ov
13f25 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20  erflow */.  u16 
13f26 6e 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69  nSize;     /* Si
13f27 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  ze of the cell c
13f28 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61  ontent on the ma
13f29 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a  in b-tree page *
13f2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69  /.};../*.** Maxi
13f2b 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20  mum depth of an 
13f2c 53 51 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74  SQLite B-Tree st
13f2d 72 75 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54  ructure. Any B-T
13f2e 72 65 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a  ree deeper than.
13f2f 2a 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20  ** this will be 
13f30 64 65 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74  declared corrupt
13f31 2e 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20  . This value is 
13f32 63 61 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64  calculated based
13f33 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d   on a.** maximum
13f34 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f   database size o
13f35 66 20 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d  f 2^31 pages a m
13f36 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66  inimum fanout of
13f37 20 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74   2 for a.** root
13f38 2d 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20  -node and 3 for 
13f39 61 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e  all other intern
13f3a 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20  al nodes..**.** 
13f3b 49 66 20 61 20 74 72 65 65 20 74 68 61 74 20 61  If a tree that a
13f3c 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c  ppears to be tal
13f3d 6c 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 73  ler than this is
13f3e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74   encountered, it
13f3f 20 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74   is.** assumed t
13f40 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
13f41 20 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a   is corrupt..*/.
13f42 23 64 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52  #define BTCURSOR
13f43 5f 4d 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f  _MAX_DEPTH 20../
13f44 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73  *.** A cursor is
13f45 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
13f46 70 61 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79  particular entry
13f47 20 77 69 74 68 69 6e 20 61 20 70 61 72 74 69 63   within a partic
13f48 75 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77  ular.** b-tree w
13f49 69 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65  ithin a database
13f4a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   file..**.** The
13f4b 20 65 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69   entry is identi
13f4c 66 69 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50  fied by its MemP
13f4d 61 67 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65  age and the inde
13f4e 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e  x in.** MemPage.
13f4f 61 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65  aCell[] of the e
13f50 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  ntry..**.** When
13f51 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
13f52 73 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72  se file can shar
13f53 65 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64  ed by two more d
13f54 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
13f55 6f 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73  ons,.** but curs
13f56 6f 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68  ors cannot be sh
13f57 61 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73  ared.  Each curs
13f58 6f 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64  or is associated
13f59 20 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69   with a.** parti
13f5a 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63  cular database c
13f5b 6f 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69  onnection identi
13f5c 66 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42  fied BtCursor.pB
13f5d 74 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46  tree.db..**.** F
13f5e 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74  ields in this st
13f5f 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65  ructure are acce
13f60 73 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42  ssed under the B
13f61 74 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a  tShared.mutex.**
13f62 20 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e   found at self->
13f63 70 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a  pBt->mutex. .*/.
13f64 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20  struct BtCursor 
13f65 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65  {.  Btree *pBtre
13f66 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
13f67 20 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68   The Btree to wh
13f68 69 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20  ich this cursor 
13f69 62 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53  belongs */.  BtS
13f6a 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20  hared *pBt;     
13f6b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74         /* The Bt
13f6c 53 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73  Shared this curs
13f6d 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a  or points to */.
13f6e 20 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78    BtCursor *pNex
13f6f 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46  t, *pPrev;  /* F
13f70 6f 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69  orms a linked li
13f71 73 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72  st of all cursor
13f72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65  s */.  struct Ke
13f73 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
13f74 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73   /* Argument pas
13f75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f  sed to compariso
13f76 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  n function */.  
13f77 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20  Pgno pgnoRoot;  
13f78 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
13f79 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68   root page of th
13f7a 69 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c  is tree */.  sql
13f7b 69 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65  ite3_int64 cache
13f7c 64 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20  dRowid; /* Next 
13f7d 72 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30 20  rowid cache.  0 
13f7e 6d 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20  means not valid 
13f7f 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e  */.  CellInfo in
13f80 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  fo;            /
13f81 2a 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 65  * A parse of the
13f82 20 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69   cell we are poi
13f83 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38  nting at */.  u8
13f84 20 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20   wrFlag;        
13f85 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
13f86 69 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20  if writable */. 
13f87 20 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20   u8 atLast;     
13f88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
13f89 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f  rsor pointing to
13f8a 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
13f8b 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65  */.  u8 validNKe
13f8c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  y;             /
13f8d 2a 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e  * True if info.n
13f8e 4b 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a  Key is valid */.
13f8f 20 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 20    u8 eState;    
13f90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
13f91 6e 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52  ne of the CURSOR
13f92 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28  _XXX constants (
13f93 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20  see below) */.  
13f94 76 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20  void *pKey;     
13f95 20 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68   /* Saved key th
13f96 61 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 20  at was cursor's 
13f97 6c 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74  last known posit
13f98 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65  ion */.  i64 nKe
13f99 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a  y;        /* Siz
13f9a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61  e of pKey, or la
13f9b 73 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a  st integer key *
13f9c 2f 0a 20 20 69 6e 74 20 73 6b 69 70 3b 20 20 20  /.  int skip;   
13f9d 20 20 20 20 20 2f 2a 20 28 73 6b 69 70 3c 30 29       /* (skip<0)
13f9e 20 2d 3e 20 50 72 65 76 28 29 20 69 73 20 61 20   -> Prev() is a 
13f9f 6e 6f 2d 6f 70 2e 20 28 73 6b 69 70 3e 30 29 20  no-op. (skip>0) 
13fa0 2d 3e 20 4e 65 78 74 28 29 20 69 73 20 2a 2f 0a  -> Next() is */.
13fa1 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
13fa2 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 75  MIT_INCRBLOB.  u
13fa3 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  8 isIncrblobHand
13fa4 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  le;      /* True
13fa5 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 20   if this cursor 
13fa6 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 68  is an incr. io h
13fa7 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  andle */.  Pgno 
13fa8 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20  *aOverflow;     
13fa9 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f 66       /* Cache of
13faa 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c   overflow page l
13fab 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64  ocations */.#end
13fac 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  if.#ifndef NDEBU
13fad 47 0a 20 20 75 38 20 70 61 67 65 73 53 68 75 66  G.  u8 pagesShuf
13fae 66 6c 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a  fled;         /*
13faf 20 54 72 75 65 20 69 66 20 42 74 72 65 65 20 70   True if Btree p
13fb0 61 67 65 73 20 61 72 65 20 72 65 61 72 72 61 6e  ages are rearran
13fb1 67 65 64 20 62 79 20 62 61 6c 61 6e 63 65 28 29  ged by balance()
13fb2 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 31 36 20  */.#endif.  i16 
13fb3 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  iPage;          
13fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fb5 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75    /* Index of cu
13fb6 72 72 65 6e 74 20 70 61 67 65 20 69 6e 20 61 70  rrent page in ap
13fb7 50 61 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  Page */.  MemPag
13fb8 65 20 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53  e *apPage[BTCURS
13fb9 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20  OR_MAX_DEPTH];  
13fba 2f 2a 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f  /* Pages from ro
13fbb 6f 74 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61  ot to current pa
13fbc 67 65 20 2a 2f 0a 20 20 75 31 36 20 61 69 49 64  ge */.  u16 aiId
13fbd 78 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44  x[BTCURSOR_MAX_D
13fbe 45 50 54 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a  EPTH];        /*
13fbf 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69   Current index i
13fc0 6e 20 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d  n apPage[i] */.}
13fc1 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69  ;../*.** Potenti
13fc2 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74  al values for Bt
13fc3 43 75 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a  Cursor.eState..*
13fc4 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c 49  *.** CURSOR_VALI
13fc5 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 70  D:.**   Cursor p
13fc6 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c 69 64  oints to a valid
13fc7 20 65 6e 74 72 79 2e 20 67 65 74 50 61 79 6c 6f   entry. getPaylo
13fc8 61 64 28 29 20 65 74 63 2e 20 6d 61 79 20 62 65  ad() etc. may be
13fc9 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43   called..**.** C
13fca 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a  URSOR_INVALID:.*
13fcb 2a 20 20 20 43 75 72 73 6f 72 20 64 6f 65 73 20  *   Cursor does 
13fcc 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 76  not point to a v
13fcd 61 6c 69 64 20 65 6e 74 72 79 2e 20 54 68 69 73  alid entry. This
13fce 20 63 61 6e 20 68 61 70 70 65 6e 20 28 66 6f 72   can happen (for
13fcf 20 65 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 20   example) .**   
13fd0 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c  because the tabl
13fd1 65 20 69 73 20 65 6d 70 74 79 20 6f 72 20 62 65  e is empty or be
13fd2 63 61 75 73 65 20 42 74 72 65 65 43 75 72 73 6f  cause BtreeCurso
13fd3 72 46 69 72 73 74 28 29 20 68 61 73 20 6e 6f 74  rFirst() has not
13fd4 20 62 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65   been.**   calle
13fd5 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f  d..**.** CURSOR_
13fd6 52 45 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a 20  REQUIRESEEK:.** 
13fd7 20 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74    The table that
13fd8 20 74 68 69 73 20 63 75 72 73 6f 72 20 77 61 73   this cursor was
13fd9 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c 6c   opened on still
13fda 20 65 78 69 73 74 73 2c 20 62 75 74 20 68 61 73   exists, but has
13fdb 20 62 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69   been .**   modi
13fdc 66 69 65 64 20 73 69 6e 63 65 20 74 68 65 20 63  fied since the c
13fdd 75 72 73 6f 72 20 77 61 73 20 6c 61 73 74 20 75  ursor was last u
13fde 73 65 64 2e 20 54 68 65 20 63 75 72 73 6f 72 20  sed. The cursor 
13fdf 70 6f 73 69 74 69 6f 6e 20 69 73 20 73 61 76 65  position is save
13fe0 64 0a 2a 2a 20 20 20 69 6e 20 76 61 72 69 61 62  d.**   in variab
13fe1 6c 65 73 20 42 74 43 75 72 73 6f 72 2e 70 4b 65  les BtCursor.pKe
13fe2 79 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 6e  y and BtCursor.n
13fe3 4b 65 79 2e 20 57 68 65 6e 20 61 20 63 75 72 73  Key. When a curs
13fe4 6f 72 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20 74  or is in .**   t
13fe5 68 69 73 20 73 74 61 74 65 2c 20 72 65 73 74 6f  his state, resto
13fe6 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
13fe7 28 29 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64  () can be called
13fe8 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a   to attempt to.*
13fe9 2a 20 20 20 73 65 65 6b 20 74 68 65 20 63 75 72  *   seek the cur
13fea 73 6f 72 20 74 6f 20 74 68 65 20 73 61 76 65 64  sor to the saved
13feb 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   position..**.**
13fec 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a   CURSOR_FAULT:.*
13fed 2a 20 20 20 41 20 75 6e 72 65 63 6f 76 65 72 61  *   A unrecovera
13fee 62 6c 65 20 65 72 72 6f 72 20 28 61 6e 20 49 2f  ble error (an I/
13fef 4f 20 65 72 72 6f 72 20 6f 72 20 61 20 6d 61 6c  O error or a mal
13ff0 6c 6f 63 20 66 61 69 6c 75 72 65 29 20 68 61 73  loc failure) has
13ff1 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 20 20 6f   occurred.**   o
13ff2 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f  n a different co
13ff3 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68  nnection that sh
13ff4 61 72 65 73 20 74 68 65 20 42 74 53 68 61 72 65  ares the BtShare
13ff5 64 20 63 61 63 68 65 20 77 69 74 68 20 74 68 69  d cache with thi
13ff6 73 0a 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20 20  s.**   cursor.  
13ff7 54 68 65 20 65 72 72 6f 72 20 68 61 73 20 6c 65  The error has le
13ff8 66 74 20 74 68 65 20 63 61 63 68 65 20 69 6e 20  ft the cache in 
13ff9 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20  an inconsistent 
13ffa 73 74 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e  state..**   Do n
13ffb 6f 74 68 69 6e 67 20 65 6c 73 65 20 77 69 74 68  othing else with
13ffc 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 20 41   this cursor.  A
13ffd 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ny attempt to us
13ffe 65 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  e the cursor.** 
13fff 20 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20    should return 
14000 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73  the error code s
14001 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f  tored in BtCurso
14002 72 2e 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69 6e  r.skip.*/.#defin
14003 65 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  e CURSOR_INVALID
14004 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65             0.#de
14005 66 69 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c 49  fine CURSOR_VALI
14006 44 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a  D             1.
14007 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 52  #define CURSOR_R
14008 45 51 55 49 52 45 53 45 45 4b 20 20 20 20 20 20  EQUIRESEEK      
14009 20 32 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f   2.#define CURSO
1400a 52 5f 46 41 55 4c 54 20 20 20 20 20 20 20 20 20  R_FAULT         
1400b 20 20 20 20 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68      3../* .** Th
1400c 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  e database page 
1400d 74 68 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  the PENDING_BYTE
1400e 20 6f 63 63 75 70 69 65 73 2e 20 54 68 69 73 20   occupies. This 
1400f 70 61 67 65 20 69 73 20 6e 65 76 65 72 20 75 73  page is never us
14010 65 64 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20  ed..*/.# define 
14011 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
14012 45 28 70 42 74 29 20 50 41 47 45 52 5f 4d 4a 5f  E(pBt) PAGER_MJ_
14013 50 47 4e 4f 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a  PGNO(pBt)../*.**
14014 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   A linked list o
14015 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
14016 73 74 72 75 63 74 75 72 65 73 20 69 73 20 73 74  structures is st
14017 6f 72 65 64 20 61 74 20 42 74 53 68 61 72 65 64  ored at BtShared
14018 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73  .pLock..** Locks
14019 20 61 72 65 20 61 64 64 65 64 20 28 6f 72 20 75   are added (or u
1401a 70 67 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41  pgraded from REA
1401b 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f  D_LOCK to WRITE_
1401c 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72  LOCK) when a cur
1401d 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65  sor .** is opene
1401e 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77  d on the table w
1401f 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74  ith root page Bt
14020 53 68 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c  Shared.iTable. L
14021 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64  ocks are removed
14022 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69  .** from this li
14023 73 74 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61  st when a transa
14024 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74  ction is committ
14025 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63  ed or rolled bac
14026 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20  k, or when.** a 
14027 62 74 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20  btree handle is 
14028 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  closed..*/.struc
14029 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72  t BtLock {.  Btr
1402a 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20  ee *pBtree;     
1402b 20 20 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64     /* Btree hand
1402c 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  le holding this 
1402d 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  lock */.  Pgno i
1402e 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
1402f 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20  /* Root page of 
14030 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c  table */.  u8 eL
14031 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ock;            
14032 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72   /* READ_LOCK or
14033 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20   WRITE_LOCK */. 
14034 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20   BtLock *pNext; 
14035 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69         /* Next i
14036 6e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b  n BtShared.pLock
14037 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20   list */.};../* 
14038 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73  Candidate values
14039 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63   for BtLock.eLoc
1403a 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41  k */.#define REA
1403b 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65  D_LOCK     1.#de
1403c 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20  fine WRITE_LOCK 
1403d 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73     2../*.** Thes
1403e 65 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20  e macros define 
1403f 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  the location of 
14040 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
14041 65 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20  entry for a .** 
14042 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 54  database page. T
14043 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
14044 74 20 74 6f 20 65 61 63 68 20 69 73 20 74 68 65  t to each is the
14045 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c   number of usabl
14046 65 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61  e.** bytes on ea
14047 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 64  ch page of the d
14048 61 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31  atabase (often 1
14049 30 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64  024). The second
1404a 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20   is the.** page 
1404b 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75  number to look u
1404c 70 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72  p in the pointer
1404d 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d   map..**.** PTRM
1404e 41 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e  AP_PAGENO return
1404f 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70  s the database p
14050 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
14051 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a  e pointer-map.**
14052 20 70 61 67 65 20 74 68 61 74 20 73 74 6f 72 65   page that store
14053 73 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70  s the required p
14054 6f 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50  ointer. PTRMAP_P
14055 54 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73  TROFFSET returns
14056 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f  .** the offset o
14057 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
14058 6d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a  map entry..**.**
14059 20 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67   If the pgno arg
1405a 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20  ument passed to 
1405b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73  PTRMAP_PAGENO is
1405c 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70   a pointer-map p
1405d 61 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e  age,.** then pgn
1405e 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53  o is returned. S
1405f 6f 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f  o (pgno==PTRMAP_
14060 50 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e  PAGENO(pgsz, pgn
14061 6f 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73  o)) can be.** us
14062 65 64 20 74 6f 20 74 65 73 74 20 69 66 20 70 67  ed to test if pg
14063 6e 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d  no is a pointer-
14064 6d 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50  map page. PTRMAP
14065 5f 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e  _ISPAGE implemen
14066 74 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e  ts.** this test.
14067 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  .*/.#define PTRM
14068 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70  AP_PAGENO(pBt, p
14069 67 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e  gno) ptrmapPagen
1406a 6f 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65  o(pBt, pgno).#de
1406b 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f  fine PTRMAP_PTRO
1406c 46 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20  FFSET(pgptrmap, 
1406d 70 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70  pgno) (5*(pgno-p
1406e 67 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66  gptrmap-1)).#def
1406f 69 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47  ine PTRMAP_ISPAG
14070 45 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54  E(pBt, pgno) (PT
14071 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74  RMAP_PAGENO((pBt
14072 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f  ),(pgno))==(pgno
14073 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f  ))../*.** The po
14074 69 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c  inter map is a l
14075 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74  ookup table that
14076 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
14077 70 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a  parent page for.
14078 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 61  ** each child pa
14079 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ge in the databa
1407a 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61  se file.  The pa
1407b 72 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 65  rent page is the
1407c 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f   page that.** co
1407d 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
1407e 20 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20   to the child.  
1407f 45 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68  Every page in th
14080 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  e database conta
14081 69 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61  ins.** 0 or 1 pa
14082 72 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e  rent pages.  (In
14083 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64   this context 'd
14084 61 74 61 62 61 73 65 20 70 61 67 65 27 20 72 65  atabase page' re
14085 66 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70  fers.** to any p
14086 61 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20  age that is not 
14087 70 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e  part of the poin
14088 74 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29  ter map itself.)
14089 20 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d    Each pointer m
1408a 61 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73  ap.** entry cons
1408b 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65  ists of a single
1408c 20 62 79 74 65 20 27 74 79 70 65 27 20 61 6e 64   byte 'type' and
1408d 20 61 20 34 20 62 79 74 65 20 70 61 72 65 6e 74   a 4 byte parent
1408e 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a   page number..**
1408f 20 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20   The PTRMAP_XXX 
14090 69 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f  identifiers belo
14091 77 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20  w are the valid 
14092 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  types..**.** The
14093 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20   purpose of the 
14094 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74  pointer map is t
14095 6f 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e  o facility movin
14096 67 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65  g pages from one
14097 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  .** position in 
14098 74 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74  the file to anot
14099 68 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 61  her as part of a
1409a 75 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e  utovacuum.  When
1409b 20 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f   a page.** is mo
1409c 76 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72  ved, the pointer
1409d 20 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d   in its parent m
1409e 75 73 74 20 62 65 20 75 70 64 61 74 65 64 20 74  ust be updated t
1409f 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a  o point to the.*
140a0 2a 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20  * new location. 
140a1 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   The pointer map
140a2 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61   is used to loca
140a3 74 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  te the parent pa
140a4 67 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a  ge quickly..**.*
140a5 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  * PTRMAP_ROOTPAG
140a6 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  E: The database 
140a7 70 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70  page is a root-p
140a8 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75  age. The page-nu
140a9 6d 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20  mber is not.**  
140aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140ab 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73  used in this cas
140ac 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  e..**.** PTRMAP_
140ad 46 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 61  FREEPAGE: The da
140ae 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61  tabase page is a
140af 6e 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 20  n unused (free) 
140b0 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e  page. The page-n
140b1 75 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20  umber .**       
140b2 20 20 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f             is no
140b3 74 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63  t used in this c
140b4 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  ase..**.** PTRMA
140b5 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65  P_OVERFLOW1: The
140b6 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69   database page i
140b7 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  s the first page
140b8 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a   in a list of .*
140b9 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
140ba 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67      overflow pag
140bb 65 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d  es. The page num
140bc 62 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74  ber identifies t
140bd 68 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20  he page that.** 
140be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140bf 20 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63    contains the c
140c0 65 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74  ell with a point
140c1 65 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66  er to this overf
140c2 6c 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  low page..**.** 
140c3 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
140c4 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
140c5 61 67 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e  age is the secon
140c6 64 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20  d or later page 
140c7 69 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20  in a list of.** 
140c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140c9 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73    overflow pages
140ca 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65  . The page-numbe
140cb 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  r identifies the
140cc 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20   previous.**    
140cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
140ce 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66  age in the overf
140cf 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a  low page list..*
140d0 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45  *.** PTRMAP_BTRE
140d1 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  E: The database 
140d2 70 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f  page is a non-ro
140d3 6f 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 54  ot btree page. T
140d4 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a  he page number.*
140d5 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
140d6 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70  identifies the p
140d7 61 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68  arent page in th
140d8 65 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66  e btree..*/.#def
140d9 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50  ine PTRMAP_ROOTP
140da 41 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54  AGE 1.#define PT
140db 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a  RMAP_FREEPAGE 2.
140dc 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f  #define PTRMAP_O
140dd 56 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69  VERFLOW1 3.#defi
140de 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  ne PTRMAP_OVERFL
140df 4f 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54  OW2 4.#define PT
140e0 52 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a  RMAP_BTREE 5../*
140e1 20 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65   A bunch of asse
140e2 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
140e3 74 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 61  to check the tra
140e4 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76  nsaction state v
140e5 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68  ariables.** of h
140e6 61 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 74  andle p (type Bt
140e7 72 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e  ree*) are intern
140e8 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e  ally consistent.
140e9 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65  .*/.#define btre
140ea 65 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a  eIntegrity(p) \.
140eb 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74    assert( p->pBt
140ec 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21  ->inTransaction!
140ed 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70  =TRANS_NONE || p
140ee 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74  ->pBt->nTransact
140ef 69 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73  ion==0 ); \.  as
140f0 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e  sert( p->pBt->in
140f1 54 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e  Transaction>=p->
140f2 69 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a  inTrans ); .../*
140f3 0a 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41  .** The ISAUTOVA
140f4 43 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73  CUUM macro is us
140f5 65 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63  ed within balanc
140f6 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64  e_nonroot() to d
140f7 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74  etermine.** if t
140f8 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70  he database supp
140f9 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d  orts auto-vacuum
140fa 20 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65   or not. Because
140fb 20 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77   it is used.** w
140fc 69 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73  ithin an express
140fd 69 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61  ion that is an a
140fe 72 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68  rgument to anoth
140ff 65 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71  er macro .** (sq
14100 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20  liteMallocRaw), 
14101 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  it is not possib
14102 6c 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74  le to use condit
14103 69 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f  ional compilatio
14104 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d  n..** So, this m
14105 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20  acro is defined 
14106 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e  instead..*/.#ifn
14107 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14108 41 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69  AUTOVACUUM.#defi
14109 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  ne ISAUTOVACUUM 
1410a 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  (pBt->autoVacuum
1410b 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ).#else.#define 
1410c 49 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23  ISAUTOVACUUM 0.#
1410d 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
1410e 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
1410f 70 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68  passed around th
14110 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61  rough all the sa
14111 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f  nity checking ro
14112 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64  utines.** in ord
14113 65 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b  er to keep track
14114 20 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20   of some global 
14115 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  state informatio
14116 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
14117 72 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b  ruct IntegrityCk
14118 20 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74   IntegrityCk;.st
14119 72 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b  ruct IntegrityCk
1411a 20 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70   {.  BtShared *p
1411b 42 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72  Bt;    /* The tr
1411c 65 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64  ee being checked
1411d 20 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20   out */.  Pager 
1411e 2a 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54  *pPager;    /* T
1411f 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 70 61  he associated pa
14120 67 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73  ger.  Also acces
14121 73 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50  sible by pBt->pP
14122 61 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e  ager */.  Pgno n
14123 50 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e  Page;       /* N
14124 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
14125 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  n the database *
14126 2f 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20  /.  int *anRef; 
14127 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
14128 6f 66 20 74 69 6d 65 73 20 65 61 63 68 20 70 61  of times each pa
14129 67 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64  ge is referenced
1412a 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b   */.  int mxErr;
1412b 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20          /* Stop 
1412c 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72  accumulating err
1412d 6f 72 73 20 77 68 65 6e 20 74 68 69 73 20 72 65  ors when this re
1412e 61 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20  aches zero */.  
1412f 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20  int nErr;       
14130 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d    /* Number of m
14131 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20  essages written 
14132 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61  to zErrMsg so fa
14133 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f  r */.  int mallo
14134 63 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65  cFailed; /* A me
14135 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
14136 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
14137 65 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d  ed */.  StrAccum
14138 20 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63   errMsg;  /* Acc
14139 75 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f  umulate the erro
1413a 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 68  r message text h
1413b 65 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ere */.};../*.**
1413c 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61   Read or write a
1413d 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62   two- and four-b
1413e 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69  yte big-endian i
1413f 6e 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a  nteger values..*
14140 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 79  /.#define get2by
14141 74 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c  te(x)   ((x)[0]<
14142 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65  <8 | (x)[1]).#de
14143 66 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c  fine put2byte(p,
14144 76 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38  v) ((p)[0] = (u8
14145 29 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31  )((v)>>8), (p)[1
14146 5d 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65  ] = (u8)(v)).#de
14147 66 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71  fine get4byte sq
14148 6c 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64  lite3Get4byte.#d
14149 65 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73  efine put4byte s
1414a 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a  qlite3Put4byte..
1414b 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 72  /*.** Internal r
1414c 6f 75 74 69 6e 65 73 20 74 68 61 74 20 73 68 6f  outines that sho
1414d 75 6c 64 20 62 65 20 61 63 63 65 73 73 65 64 20  uld be accessed 
1414e 62 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 79  by the btree lay
1414f 65 72 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  er only..*/.SQLI
14150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
14151 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
14152 67 65 28 42 74 53 68 61 72 65 64 2a 2c 20 50 67  ge(BtShared*, Pg
14153 6e 6f 2c 20 4d 65 6d 50 61 67 65 2a 2a 2c 20 69  no, MemPage**, i
14154 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
14155 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
14156 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d  treeInitPage(Mem
14157 50 61 67 65 20 2a 70 50 61 67 65 29 3b 0a 53 51  Page *pPage);.SQ
14158 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
14159 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  d sqlite3BtreePa
1415a 72 73 65 43 65 6c 6c 50 74 72 28 4d 65 6d 50 61  rseCellPtr(MemPa
1415b 67 65 2a 2c 20 75 38 2a 2c 20 43 65 6c 6c 49 6e  ge*, u8*, CellIn
1415c 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  fo*);.SQLITE_PRI
1415d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1415e 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28  3BtreeParseCell(
1415f 4d 65 6d 50 61 67 65 2a 2c 20 69 6e 74 2c 20 43  MemPage*, int, C
14160 65 6c 6c 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54  ellInfo*);.SQLIT
14161 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14162 6c 69 74 65 33 42 74 72 65 65 52 65 73 74 6f 72  lite3BtreeRestor
14163 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
14164 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b  BtCursor *pCur);
14165 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14166 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
14167 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42  eGetTempCursor(B
14168 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42  tCursor *pCur, B
14169 74 43 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75  tCursor *pTempCu
1416a 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
1416b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
1416c 74 72 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43  treeReleaseTempC
1416d 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a  ursor(BtCursor *
1416e 70 43 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pCur);.SQLITE_PR
1416f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
14170 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72  e3BtreeMoveToPar
14171 65 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ent(BtCursor *pC
14172 75 72 29 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ur);../*********
14173 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72  ***** End of btr
14174 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  eeInt.h ********
14175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14176 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14177 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
14178 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
14179 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
1417a 66 66 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20  ff in btmutex.c 
1417b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1417c 2a 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ****/.#ifndef SQ
1417d 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
1417e 5f 43 41 43 48 45 0a 23 69 66 20 53 51 4c 49 54  _CACHE.#if SQLIT
1417f 45 5f 54 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a  E_THREADSAFE../*
14180 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 42  .** Obtain the B
14181 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 61 73  tShared mutex as
14182 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 42 2d  sociated with B-
14183 54 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 41  Tree handle p. A
14184 6c 73 6f 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68  lso,.** set BtSh
14185 61 72 65 64 2e 64 62 20 74 6f 20 74 68 65 20 64  ared.db to the d
14186 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 61  atabase handle a
14187 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70  ssociated with p
14188 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c   and the.** p->l
14189 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f  ocked boolean to
1418a 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   true..*/.static
1418b 20 76 6f 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d   void lockBtreeM
1418c 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a  utex(Btree *p){.
1418d 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63    assert( p->loc
1418e 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  ked==0 );.  asse
1418f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
14190 78 5f 6e 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74  x_notheld(p->pBt
14191 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
14192 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14193 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
14194 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c  mutex) );..  sql
14195 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
14196 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
14197 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20  .  p->pBt->db = 
14198 70 2d 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b  p->db;.  p->lock
14199 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ed = 1;.}../*.**
1419a 20 52 65 6c 65 61 73 65 20 74 68 65 20 42 74 53   Release the BtS
1419b 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f  hared mutex asso
1419c 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72  ciated with B-Tr
1419d 65 65 20 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a  ee handle p and.
1419e 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 70 2d 3e  ** clear the p->
1419f 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a  locked boolean..
141a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
141a1 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28  nlockBtreeMutex(
141a2 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
141a3 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d  ert( p->locked==
141a4 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  1 );.  assert( s
141a5 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
141a6 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  d(p->pBt->mutex)
141a7 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
141a8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
141a9 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
141aa 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64  ;.  assert( p->d
141ab 62 3d 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b  b==p->pBt->db );
141ac 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
141ad 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e  x_leave(p->pBt->
141ae 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63  mutex);.  p->loc
141af 6b 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  ked = 0;.}../*.*
141b0 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78 20  * Enter a mutex 
141b1 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54 72  on the given BTr
141b2 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a  ee object..**.**
141b3 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20 69   If the object i
141b4 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c 20  s not sharable, 
141b5 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69 73  then no mutex is
141b6 20 65 76 65 72 20 72 65 71 75 69 72 65 64 0a 2a   ever required.*
141b7 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69  * and this routi
141b8 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20  ne is a no-op.  
141b9 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d  The underlying m
141ba 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63 75  utex is non-recu
141bb 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77 65  rsive..** But we
141bc 20 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e 63   keep a referenc
141bd 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65 65  e count in Btree
141be 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 74  .wantToLock so t
141bf 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f  he behavior.** o
141c0 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65  f this interface
141c1 20 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a 2a   is recursive..*
141c2 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64 65  *.** To avoid de
141c3 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 6c  adlocks, multipl
141c4 65 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f 63  e Btrees are loc
141c5 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ked in the same 
141c6 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c 20  order.** by all 
141c7 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
141c8 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70 4e  ions.  The p->pN
141c9 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66  ext is a list of
141ca 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65 73   other.** Btrees
141cb 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68   belonging to th
141cc 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
141cd 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 68  connection as th
141ce 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68 69  e p Btree.** whi
141cf 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f  ch need to be lo
141d0 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20 49  cked after p.  I
141d1 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74 20  f we cannot get 
141d2 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c 20  a lock on.** p, 
141d3 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f 63  then first unloc
141d4 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68  k all of the oth
141d5 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 2c  ers on p->pNext,
141d6 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66 6f   then wait.** fo
141d7 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62 65  r the lock to be
141d8 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20 6f  come available o
141d9 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63 6b  n p, then relock
141da 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 73   all of the.** s
141db 75 62 73 65 71 75 65 6e 74 20 42 74 72 65 65 73  ubsequent Btrees
141dc 20 74 68 61 74 20 64 65 73 69 72 65 20 61 20 6c   that desire a l
141dd 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ock..*/.SQLITE_P
141de 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
141df 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74  te3BtreeEnter(Bt
141e0 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65 65  ree *p){.  Btree
141e1 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a 20   *pLater;..  /* 
141e2 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74  Some basic sanit
141e3 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68  y checking on th
141e4 65 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c 69  e Btree.  The li
141e5 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20 2a  st of Btrees.  *
141e6 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70  * connected by p
141e7 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76 20 73  Next and pPrev s
141e8 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72 74  hould be in sort
141e9 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a 2a  ed order by.  **
141ea 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75 65   Btree.pBt value
141eb 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f  . All elements o
141ec 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75 6c  f the list shoul
141ed 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a 2a  d belong to.  **
141ee 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63   the same connec
141ef 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 65  tion. Only share
141f0 64 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e 20  d Btrees are on 
141f1 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 61  the list. */.  a
141f2 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d  ssert( p->pNext=
141f3 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e  =0 || p->pNext->
141f4 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 20  pBt>p->pBt );.  
141f5 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76  assert( p->pPrev
141f6 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d  ==0 || p->pPrev-
141f7 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20  >pBt<p->pBt );. 
141f8 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78   assert( p->pNex
141f9 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74  t==0 || p->pNext
141fa 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20  ->db==p->db );. 
141fb 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65   assert( p->pPre
141fc 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76  v==0 || p->pPrev
141fd 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20  ->db==p->db );. 
141fe 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72   assert( p->shar
141ff 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 78  able || (p->pNex
14200 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 65 76  t==0 && p->pPrev
14201 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68  ==0) );..  /* Ch
14202 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20  eck for locking 
14203 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a 20  consistency */. 
14204 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63   assert( !p->loc
14205 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f  ked || p->wantTo
14206 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65  Lock>0 );.  asse
14207 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  rt( p->sharable 
14208 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  || p->wantToLock
14209 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 20  ==0 );..  /* We 
1420a 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68  should already h
1420b 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68  old a lock on th
1420c 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1420d 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72  ction */.  asser
1420e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1420f 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
14210 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c  ex) );..  /* Unl
14211 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
14212 20 69 73 20 73 68 61 72 61 62 6c 65 20 61 6e 64   is sharable and
14213 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20   unlocked, then 
14214 42 74 53 68 61 72 65 64 2e 64 62 0a 20 20 2a 2a  BtShared.db.  **
14215 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20   should already 
14216 62 65 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79  be set correctly
14217 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28  . */.  assert( (
14218 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20  p->locked==0 && 
14219 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c 20  p->sharable) || 
1421a 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64  p->pBt->db==p->d
1421b 62 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e  b );..  if( !p->
1421c 73 68 61 72 61 62 6c 65 20 29 20 72 65 74 75 72  sharable ) retur
1421d 6e 3b 0a 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  n;.  p->wantToLo
1421e 63 6b 2b 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c  ck++;.  if( p->l
1421f 6f 63 6b 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ocked ) return;.
14220 0a 20 20 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61  .  /* In most ca
14221 73 65 73 2c 20 77 65 20 73 68 6f 75 6c 64 20 62  ses, we should b
14222 65 20 61 62 6c 65 20 74 6f 20 61 63 71 75 69 72  e able to acquir
14223 65 20 74 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20  e the lock we.  
14224 2a 2a 20 77 61 6e 74 20 77 69 74 68 6f 75 74 20  ** want without 
14225 68 61 76 69 6e 67 20 74 6f 20 67 6f 20 74 68 72  having to go thr
14226 6f 75 67 68 74 20 74 68 65 20 61 73 63 65 6e 64  ought the ascend
14227 69 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72  ing lock.  ** pr
14228 6f 63 65 64 75 72 65 20 74 68 61 74 20 66 6f 6c  ocedure that fol
14229 6c 6f 77 73 2e 20 20 4a 75 73 74 20 62 65 20 73  lows.  Just be s
1422a 75 72 65 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b  ure not to block
1422b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
1422c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70  ite3_mutex_try(p
1422d 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53  ->pBt->mutex)==S
1422e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1422f 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e  p->pBt->db = p->
14230 64 62 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65  db;.    p->locke
14231 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  d = 1;.    retur
14232 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20  n;.  }..  /* To 
14233 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20  avoid deadlock, 
14234 66 69 72 73 74 20 72 65 6c 65 61 73 65 20 61 6c  first release al
14235 6c 20 6c 6f 63 6b 73 20 77 69 74 68 20 61 20 6c  l locks with a l
14236 61 72 67 65 72 0a 20 20 2a 2a 20 42 74 53 68 61  arger.  ** BtSha
14237 72 65 64 20 61 64 64 72 65 73 73 2e 20 20 54 68  red address.  Th
14238 65 6e 20 61 63 71 75 69 72 65 20 6f 75 72 20 6c  en acquire our l
14239 6f 63 6b 2e 20 20 54 68 65 6e 20 72 65 61 63 71  ock.  Then reacq
1423a 75 69 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74  uire.  ** the ot
1423b 68 65 72 20 42 74 53 68 61 72 65 64 20 6c 6f 63  her BtShared loc
1423c 6b 73 20 74 68 61 74 20 77 65 20 75 73 65 64 20  ks that we used 
1423d 74 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e  to hold in ascen
1423e 64 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e  ding.  ** order.
1423f 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74  .  */.  for(pLat
14240 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61  er=p->pNext; pLa
14241 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74  ter; pLater=pLat
14242 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  er->pNext){.    
14243 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e  assert( pLater->
14244 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 20 20  sharable );.    
14245 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e  assert( pLater->
14246 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74  pNext==0 || pLat
14247 65 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70  er->pNext->pBt>p
14248 4c 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20  Later->pBt );.  
14249 20 20 61 73 73 65 72 74 28 20 21 70 4c 61 74 65    assert( !pLate
1424a 72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61  r->locked || pLa
1424b 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e  ter->wantToLock>
1424c 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 61  0 );.    if( pLa
1424d 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20  ter->locked ){. 
1424e 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65       unlockBtree
1424f 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20  Mutex(pLater);. 
14250 20 20 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b 42     }.  }.  lockB
14251 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20  treeMutex(p);.  
14252 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e  for(pLater=p->pN
14253 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61  ext; pLater; pLa
14254 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78  ter=pLater->pNex
14255 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74  t){.    if( pLat
14256 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29  er->wantToLock )
14257 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65  {.      lockBtre
14258 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a  eMutex(pLater);.
14259 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
1425a 2a 2a 20 45 78 69 74 20 74 68 65 20 72 65 63 75  ** Exit the recu
1425b 72 73 69 76 65 20 6d 75 74 65 78 20 6f 6e 20 61  rsive mutex on a
1425c 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54   Btree..*/.SQLIT
1425d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1425e 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1425f 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 66  (Btree *p){.  if
14260 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b  ( p->sharable ){
14261 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
14262 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a  wantToLock>0 );.
14263 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
14264 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  k--;.    if( p->
14265 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b  wantToLock==0 ){
14266 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72  .      unlockBtr
14267 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14268 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66  }.  }.}..#ifndef
14269 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65   NDEBUG./*.** Re
1426a 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
1426b 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 20   BtShared mutex 
1426c 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 62  is held on the b
1426d 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65 0a  tree, or if the.
1426e 2a 2a 20 42 2d 54 72 65 65 20 69 73 20 6e 6f 74  ** B-Tree is not
1426f 20 6d 61 72 6b 65 64 20 61 73 20 73 68 61 72 61   marked as shara
14270 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ble..**.** This 
14271 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
14272 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e  only from within
14273 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
14274 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
14275 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14276 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
14277 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  ex(Btree *p){.  
14278 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61  assert( p->shara
14279 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63  ble==0 || p->loc
1427a 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 6e  ked==0 || p->wan
1427b 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61  tToLock>0 );.  a
1427c 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
1427d 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b  le==0 || p->lock
1427e 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d  ed==0 || p->db==
1427f 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20  p->pBt->db );.  
14280 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61  assert( p->shara
14281 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63  ble==0 || p->loc
14282 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ked==0 || sqlite
14283 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
14284 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
14285 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72   assert( p->shar
14286 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f  able==0 || p->lo
14287 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  cked==0 || sqlit
14288 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
14289 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  >db->mutex) );..
1428a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 73 68 61    return (p->sha
1428b 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c  rable==0 || p->l
1428c 6f 63 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64 69 66  ocked);.}.#endif
1428d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1428e 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
1428f 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20  /*.** Enter and 
14290 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20 6f 6e  leave a mutex on
14291 20 61 20 42 74 72 65 65 20 67 69 76 65 6e 20 61   a Btree given a
14292 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 62 79   cursor owned by
14293 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20   that.** Btree. 
14294 20 54 68 65 73 65 20 65 6e 74 72 79 20 70 6f 69   These entry poi
14295 6e 74 73 20 61 72 65 20 75 73 65 64 20 62 79 20  nts are used by 
14296 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20  incremental I/O 
14297 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d  and can be.** om
14298 69 74 74 65 64 20 69 66 20 74 68 61 74 20 6d 6f  itted if that mo
14299 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 65 64  dule is not used
1429a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1429b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1429c 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72  BtreeEnterCursor
1429d 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
1429e 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  {.  sqlite3Btree
1429f 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 74 72  Enter(pCur->pBtr
142a0 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ee);.}.SQLITE_PR
142a1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
142a2 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
142a3 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  or(BtCursor *pCu
142a4 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  r){.  sqlite3Btr
142a5 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e 70 42  eeLeave(pCur->pB
142a6 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  tree);.}.#endif 
142a7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  /* SQLITE_OMIT_I
142a8 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a  NCRBLOB */.../*.
142a9 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74  ** Enter the mut
142aa 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74 72 65  ex on every Btre
142ab 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
142ac 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  h a database.** 
142ad 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69  connection.  Thi
142ae 73 20 69 73 20 6e 65 65 64 65 64 20 28 66 6f 72  s is needed (for
142af 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f 72 20   example) prior 
142b0 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20  to parsing.** a 
142b1 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 65 20  statement since 
142b2 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61  we will be compa
142b3 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 63  ring table and c
142b4 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61  olumn names.** a
142b5 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d  gainst all schem
142b6 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74  as and we do not
142b7 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63 68 65   want those sche
142b8 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73  mas being.** res
142b9 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65  et out from unde
142ba 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  r us..**.** Ther
142bb 65 20 69 73 20 61 20 63 6f 72 72 65 73 70 6f 6e  e is a correspon
142bc 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70  ding leave-all p
142bd 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a  rocedures..**.**
142be 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78   Enter the mutex
142bf 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20  es in accending 
142c0 6f 72 64 65 72 20 62 79 20 42 74 53 68 61 72 65  order by BtShare
142c1 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72 65 73  d pointer addres
142c2 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74 68  s.** to avoid th
142c3 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66  e possibility of
142c4 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74   deadlock when t
142c5 77 6f 20 74 68 72 65 61 64 73 20 77 69 74 68 0a  wo threads with.
142c6 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62  ** two or more b
142c7 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20  trees in common 
142c8 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b  both try to lock
142c9 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72 65 65   all their btree
142ca 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65  s.** at the same
142cb 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c   instant..*/.SQL
142cc 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
142cd 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
142ce 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64  erAll(sqlite3 *d
142cf 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42  b){.  int i;.  B
142d0 74 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72  tree *p, *pLater
142d1 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
142d2 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
142d3 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  b->mutex) );.  f
142d4 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
142d5 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d  b; i++){.    p =
142d6 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
142d7 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 20  .    assert( !p 
142d8 7c 7c 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30  || (p->locked==0
142d9 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 29   && p->sharable)
142da 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d   || p->pBt->db==
142db 70 2d 3e 64 62 20 29 3b 0a 20 20 20 20 69 66 28  p->db );.    if(
142dc 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c   p && p->sharabl
142dd 65 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61  e ){.      p->wa
142de 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20  ntToLock++;.    
142df 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64    if( !p->locked
142e0 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
142e1 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  rt( p->wantToLoc
142e2 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20  k==1 );.        
142e3 77 68 69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20  while( p->pPrev 
142e4 29 20 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a  ) p = p->pPrev;.
142e5 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
142e6 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 70  ->locked && p->p
142e7 4e 65 78 74 20 29 20 70 20 3d 20 70 2d 3e 70 4e  Next ) p = p->pN
142e8 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  ext;.        for
142e9 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65  (pLater = p->pNe
142ea 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74  xt; pLater; pLat
142eb 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74  er=pLater->pNext
142ec 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
142ed 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20   pLater->locked 
142ee 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75  ){.            u
142ef 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28  nlockBtreeMutex(
142f0 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 20 20 20  pLater);.       
142f1 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
142f2 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 20         while( p 
142f3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63  ){.          loc
142f4 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a  kBtreeMutex(p);.
142f5 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d            p = p-
142f6 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
142f7 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
142f8 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49    }.}.SQLITE_PRI
142f9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
142fa 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73  3BtreeLeaveAll(s
142fb 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
142fc 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70  nt i;.  Btree *p
142fd 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
142fe 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
142ff 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  b->mutex) );.  f
14300 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
14301 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d  b; i++){.    p =
14302 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
14303 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d  .    if( p && p-
14304 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
14305 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61     assert( p->wa
14306 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20  ntToLock>0 );.  
14307 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
14308 6b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70  k--;.      if( p
14309 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20  ->wantToLock==0 
1430a 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c 6f 63  ){.        unloc
1430b 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a  kBtreeMutex(p);.
1430c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1430d 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  }.}..#ifndef NDE
1430e 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  BUG./*.** Return
1430f 20 74 72 75 65 20 69 66 20 74 68 65 20 63 75 72   true if the cur
14310 72 65 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64  rent thread hold
14311 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  s the database c
14312 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74  onnection.** mut
14313 65 78 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69  ex and all requi
14314 72 65 64 20 42 74 53 68 61 72 65 64 20 6d 75 74  red BtShared mut
14315 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  exes..**.** This
14316 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
14317 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
14318 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79   statements only
14319 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1431a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1431b 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
1431c 78 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  xes(sqlite3 *db)
1431d 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
1431e 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   !sqlite3_mutex_
1431f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
14320 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
14321 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
14322 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
14323 0a 20 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20  .    Btree *p;. 
14324 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69     p = db->aDb[i
14325 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70  ].pBt;.    if( p
14326 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20   && p->sharable 
14327 26 26 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e  &&.         (p->
14328 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c  wantToLock==0 ||
14329 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   !sqlite3_mutex_
1432a 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74  held(p->pBt->mut
1432b 65 78 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ex)) ){.      re
1432c 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
1432d 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
1432e 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
1432f 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61   */../*.** Add a
14330 20 6e 65 77 20 42 74 72 65 65 20 70 6f 69 6e 74   new Btree point
14331 65 72 20 74 6f 20 61 20 42 74 72 65 65 4d 75 74  er to a BtreeMut
14332 65 78 41 72 72 61 79 2e 20 0a 2a 2a 20 69 66 20  exArray. .** if 
14333 74 68 65 20 70 6f 69 6e 74 65 72 20 63 61 6e 20  the pointer can 
14334 70 6f 73 73 69 62 6c 79 20 62 65 20 73 68 61 72  possibly be shar
14335 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 6f 74 68  ed with.** anoth
14336 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  er database conn
14337 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ection..**.** Th
14338 65 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 6b  e pointers are k
14339 65 70 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72  ept in sorted or
1433a 64 65 72 20 62 79 20 70 42 74 72 65 65 2d 3e 70  der by pBtree->p
1433b 42 74 2e 20 20 54 68 61 74 0a 2a 2a 20 77 61 79  Bt.  That.** way
1433c 20 77 68 65 6e 20 77 65 20 67 6f 20 74 6f 20 65   when we go to e
1433d 6e 74 65 72 20 61 6c 6c 20 74 68 65 20 6d 75 74  nter all the mut
1433e 65 78 65 73 2c 20 77 65 20 63 61 6e 20 65 6e 74  exes, we can ent
1433f 65 72 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72  er them.** in or
14340 64 65 72 20 77 69 74 68 6f 75 74 20 65 76 65 72  der without ever
14341 79 20 68 61 76 69 6e 67 20 74 6f 20 62 61 63 6b  y having to back
14342 75 70 20 61 6e 64 20 72 65 74 72 79 20 61 6e 64  up and retry and
14343 20 77 69 74 68 6f 75 74 0a 2a 2a 20 77 6f 72 72   without.** worr
14344 79 69 6e 67 20 61 62 6f 75 74 20 64 65 61 64 6c  ying about deadl
14345 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ock..**.** The n
14346 75 6d 62 65 72 20 6f 66 20 73 68 61 72 65 64 20  umber of shared 
14347 62 74 72 65 65 73 20 77 69 6c 6c 20 61 6c 77 61  btrees will alwa
14348 79 73 20 62 65 20 73 6d 61 6c 6c 20 28 75 73 75  ys be small (usu
14349 61 6c 6c 79 20 30 20 6f 72 20 31 29 0a 2a 2a 20  ally 0 or 1).** 
1434a 73 6f 20 61 6e 20 69 6e 73 65 72 74 69 6f 6e 20  so an insertion 
1434b 73 6f 72 74 20 69 73 20 61 6e 20 61 64 65 71 75  sort is an adequ
1434c 61 74 65 20 61 6c 67 6f 72 69 74 68 6d 20 68 65  ate algorithm he
1434d 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  re..*/.SQLITE_PR
1434e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1434f 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
14350 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74  yInsert(BtreeMut
14351 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 2c  exArray *pArray,
14352 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 29 7b   Btree *pBtree){
14353 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 42  .  int i, j;.  B
14354 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20  tShared *pBt;.  
14355 69 66 28 20 70 42 74 72 65 65 3d 3d 30 20 7c 7c  if( pBtree==0 ||
14356 20 70 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c   pBtree->sharabl
14357 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23  e==0 ) return;.#
14358 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
14359 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
1435a 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b  <pArray->nMutex;
1435b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73   i++){.      ass
1435c 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74  ert( pArray->aBt
1435d 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29  ree[i]!=pBtree )
1435e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
1435f 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 41 72  if.  assert( pAr
14360 72 61 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 20 29  ray->nMutex>=0 )
14361 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 72  ;.  assert( pArr
14362 61 79 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 61 79  ay->nMutex<Array
14363 53 69 7a 65 28 70 41 72 72 61 79 2d 3e 61 42 74  Size(pArray->aBt
14364 72 65 65 29 2d 31 20 29 3b 0a 20 20 70 42 74 20  ree)-1 );.  pBt 
14365 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20  = pBtree->pBt;. 
14366 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72   for(i=0; i<pArr
14367 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29  ay->nMutex; i++)
14368 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41  {.    assert( pA
14369 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21  rray->aBtree[i]!
1436a 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69  =pBtree );.    i
1436b 66 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  f( pArray->aBtre
1436c 65 5b 69 5d 2d 3e 70 42 74 3e 70 42 74 20 29 7b  e[i]->pBt>pBt ){
1436d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 41 72  .      for(j=pAr
1436e 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69  ray->nMutex; j>i
1436f 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20  ; j--){.        
14370 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a  pArray->aBtree[j
14371 5d 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72  ] = pArray->aBtr
14372 65 65 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d  ee[j-1];.      }
14373 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61  .      pArray->a
14374 42 74 72 65 65 5b 69 5d 20 3d 20 70 42 74 72 65  Btree[i] = pBtre
14375 65 3b 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d  e;.      pArray-
14376 3e 6e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 20 20  >nMutex++;.     
14377 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
14378 20 7d 0a 20 20 70 41 72 72 61 79 2d 3e 61 42 74   }.  pArray->aBt
14379 72 65 65 5b 70 41 72 72 61 79 2d 3e 6e 4d 75 74  ree[pArray->nMut
1437a 65 78 2b 2b 5d 20 3d 20 70 42 74 72 65 65 3b 0a  ex++] = pBtree;.
1437b 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74  }../*.** Enter t
1437c 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72  he mutex of ever
1437d 79 20 62 74 72 65 65 20 69 6e 20 74 68 65 20 61  y btree in the a
1437e 72 72 61 79 2e 20 20 54 68 69 73 20 72 6f 75 74  rray.  This rout
1437f 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64  ine is.** called
14380 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
14381 67 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65  g of sqlite3Vdbe
14382 45 78 65 63 28 29 2e 20 20 54 68 65 20 6d 75 74  Exec().  The mut
14383 65 78 65 73 20 61 72 65 0a 2a 2a 20 65 78 69 74  exes are.** exit
14384 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ed at the end of
14385 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69   the same functi
14386 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
14387 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
14388 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
14389 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65  yEnter(BtreeMute
1438a 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b  xArray *pArray){
1438b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
1438c 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e  i=0; i<pArray->n
1438d 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20  Mutex; i++){.   
1438e 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72   Btree *p = pArr
1438f 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20  ay->aBtree[i];. 
14390 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63     /* Some basic
14391 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   sanity checking
14392 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
14393 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e  i==0 || pArray->
14394 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74  aBtree[i-1]->pBt
14395 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61  <p->pBt );.    a
14396 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65  ssert( !p->locke
14397 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  d || p->wantToLo
14398 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  ck>0 );..    /* 
14399 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64  We should alread
1439a 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e  y hold a lock on
1439b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
1439c 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  nnection */.    
1439d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1439e 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
1439f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20  ->mutex) );..   
143a0 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b   p->wantToLock++
143a1 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f  ;.    if( !p->lo
143a2 63 6b 65 64 20 26 26 20 70 2d 3e 73 68 61 72 61  cked && p->shara
143a3 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63  ble ){.      loc
143a4 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a  kBtreeMutex(p);.
143a5 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
143a6 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74  ** Leave the mut
143a7 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72 65  ex of every btre
143a8 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a  e in the group..
143a9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
143aa 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
143ab 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
143ac 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72 72  ve(BtreeMutexArr
143ad 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69  ay *pArray){.  i
143ae 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
143af 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65   i<pArray->nMute
143b0 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  x; i++){.    Btr
143b1 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e  ee *p = pArray->
143b2 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f  aBtree[i];.    /
143b3 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e  * Some basic san
143b4 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a  ity checking */.
143b5 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30      assert( i==0
143b6 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72   || pArray->aBtr
143b7 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e  ee[i-1]->pBt<p->
143b8 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
143b9 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20  t( p->locked || 
143ba 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a  !p->sharable );.
143bb 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77      assert( p->w
143bc 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a  antToLock>0 );..
143bd 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64      /* We should
143be 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20   already hold a 
143bf 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
143c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
143c1 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  */.    assert( s
143c2 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
143c3 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
143c4 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54  );..    p->wantT
143c5 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28  oLock--;.    if(
143c6 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d   p->wantToLock==
143c7 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 20 29  0 && p->locked )
143c8 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74  {.      unlockBt
143c9 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20  reeMutex(p);.   
143ca 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a   }.  }.}..#else.
143cb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
143cc 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
143cd 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b  Enter(Btree *p){
143ce 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20  .  p->pBt->db = 
143cf 70 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f  p->db;.}.SQLITE_
143d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
143d1 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
143d2 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  l(sqlite3 *db){.
143d3 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
143d4 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
143d5 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  ++){.    Btree *
143d6 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  p = db->aDb[i].p
143d7 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b  Bt;.    if( p ){
143d8 0a 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64  .      p->pBt->d
143d9 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d  b = p->db;.    }
143da 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
143db 20 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41   if SQLITE_THREA
143dc 44 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20  DSAFE */.#endif 
143dd 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /* ifndef SQLITE
143de 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
143df 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  HE */../********
143e0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74  ****** End of bt
143e1 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  mutex.c ********
143e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143e4 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
143e5 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
143e6 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a  e btree.c ******
143e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143e9 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
143ea 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20  4 April 6.**.** 
143eb 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
143ec 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
143ed 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
143ee 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
143ef 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
143f0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
143f1 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
143f2 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
143f3 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
143f4 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
143f5 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
143f6 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
143f7 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
143f8 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
143f9 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
143fa 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
143fb 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
143fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14400 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a  ********.** $Id:
14401 20 62 74 72 65 65 2e 63 2c 76 20 31 2e 36 30 38   btree.c,v 1.608
14402 20 32 30 30 39 2f 30 35 2f 30 36 20 31 38 3a 35   2009/05/06 18:5
14403 37 3a 31 30 20 73 68 61 6e 65 20 45 78 70 20 24  7:10 shane Exp $
14404 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
14405 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78   implements a ex
14406 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73  ternal (disk-bas
14407 65 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69  ed) database usi
14408 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65  ng BTrees..** Se
14409 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d  e the header com
1440a 6d 65 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e  ment on "btreeIn
1440b 74 2e 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f  t.h" for additio
1440c 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
1440d 0a 2a 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20  .** Including a 
1440e 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66  description of f
1440f 69 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61  ile format and a
14410 6e 20 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70  n overview of op
14411 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a  eration..*/../*.
14412 2a 2a 20 54 68 65 20 68 65 61 64 65 72 20 73 74  ** The header st
14413 72 69 6e 67 20 74 68 61 74 20 61 70 70 65 61 72  ring that appear
14414 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69  s at the beginni
14415 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53  ng of every.** S
14416 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 0a  QLite database..
14417 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
14418 63 68 61 72 20 7a 4d 61 67 69 63 48 65 61 64 65  char zMagicHeade
14419 72 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c  r[] = SQLITE_FIL
1441a 45 5f 48 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a  E_HEADER;../*.**
1441b 20 53 65 74 20 74 68 69 73 20 67 6c 6f 62 61 6c   Set this global
1441c 20 76 61 72 69 61 62 6c 65 20 74 6f 20 31 20 74   variable to 1 t
1441d 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67  o enable tracing
1441e 20 75 73 69 6e 67 20 74 68 65 20 54 52 41 43 45   using the TRACE
1441f 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69  .** macro..*/.#i
14420 66 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 42  f 0.int sqlite3B
14421 74 72 65 65 54 72 61 63 65 3d 30 3b 20 20 2f 2a  treeTrace=0;  /*
14422 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20   True to enable 
14423 74 72 61 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66  tracing */.# def
14424 69 6e 65 20 54 52 41 43 45 28 58 29 20 20 69 66  ine TRACE(X)  if
14425 28 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61  (sqlite3BtreeTra
14426 63 65 29 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c  ce){printf X;ffl
14427 75 73 68 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65  ush(stdout);}.#e
14428 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 54 52 41  lse.# define TRA
14429 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a  CE(X).#endif....
1442a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1442b 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1442c 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66  ./*.** A list of
1442d 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
1442e 73 20 74 68 61 74 20 61 72 65 20 65 6c 69 67 69  s that are eligi
1442f 62 6c 65 20 66 6f 72 20 70 61 72 74 69 63 69 70  ble for particip
14430 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72  ation.** in shar
14431 65 64 20 63 61 63 68 65 2e 20 20 54 68 69 73 20  ed cache.  This 
14432 76 61 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c  variable has fil
14433 65 20 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e  e scope during n
14434 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a  ormal builds,.**
14435 20 62 75 74 20 74 68 65 20 74 65 73 74 20 68 61   but the test ha
14436 72 6e 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61  rness needs to a
14437 63 63 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d  ccess it so we m
14438 61 6b 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f  ake it global fo
14439 72 20 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64  r .** test build
1443a 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20  s..**.** Access 
1443b 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  to this variable
1443c 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79   is protected by
1443d 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
1443e 41 54 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a  ATIC_MASTER..*/.
1443f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
14440 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
14441 45 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49  E BtShared *SQLI
14442 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68  TE_WSD sqlite3Sh
14443 61 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20  aredCacheList = 
14444 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20  0;.#else.static 
14445 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45  BtShared *SQLITE
14446 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72  _WSD sqlite3Shar
14447 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b  edCacheList = 0;
14448 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f  .#endif.#endif /
14449 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  * SQLITE_OMIT_SH
1444a 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23  ARED_CACHE */..#
1444b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1444c 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
1444d 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20  /*.** Enable or 
1444e 64 69 73 61 62 6c 65 20 74 68 65 20 73 68 61 72  disable the shar
1444f 65 64 20 70 61 67 65 72 20 61 6e 64 20 73 63 68  ed pager and sch
14450 65 6d 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a  ema features..**
14451 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14452 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f   has no effect o
14453 6e 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62  n existing datab
14454 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ase connections.
14455 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 63  .** The shared c
14456 61 63 68 65 20 73 65 74 74 69 6e 67 20 65 66 66  ache setting eff
14457 65 63 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65  ects only future
14458 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c   calls to.** sql
14459 69 74 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c  ite3_open(), sql
1445a 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f  ite3_open16(), o
1445b 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  r sqlite3_open_v
1445c 32 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  2()..*/.SQLITE_A
1445d 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
1445e 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63  nable_shared_cac
1445f 68 65 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a  he(int enable){.
14460 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
14461 6f 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68  onfig.sharedCach
14462 65 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c  eEnabled = enabl
14463 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  e;.  return SQLI
14464 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
14465 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20  ../*.** Forward 
14466 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2f 0a 73  declaration.*/.s
14467 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 46  tatic int checkF
14468 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28  orReadConflicts(
14469 42 74 72 65 65 2a 2c 20 50 67 6e 6f 2c 20 42 74  Btree*, Pgno, Bt
1446a 43 75 72 73 6f 72 2a 2c 20 69 36 34 29 3b 0a 0a  Cursor*, i64);..
1446b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
1446c 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1446d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66  .  /*.  ** The f
1446e 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53 68  unctions querySh
1446f 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
14470 63 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64 43  ck(), setSharedC
14471 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c  acheTableLock(),
14472 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 41  .  ** and clearA
14473 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62  llSharedCacheTab
14474 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20 6d  leLocks().  ** m
14475 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69 65  anipulate entrie
14476 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72 65  s in the BtShare
14477 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c  d.pLock linked l
14478 69 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f 72  ist used to stor
14479 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61  e.  ** shared-ca
1447a 63 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c 20  che table level 
1447b 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c 69  locks. If the li
1447c 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65  brary is compile
1447d 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20  d with the.  ** 
1447e 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 61  shared-cache fea
1447f 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74  ture disabled, t
14480 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 6c  hen there is onl
14481 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72 0a  y ever one user.
14482 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 53    ** of each BtS
14483 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20  hared structure 
14484 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 6b  and so this lock
14485 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  ing is not neces
14486 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 64  sary. .  ** So d
14487 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 72  efine the lock r
14488 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e 73  elated functions
14489 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f   as no-ops..  */
1448a 0a 20 20 23 64 65 66 69 6e 65 20 71 75 65 72 79  .  #define query
1448b 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
1448c 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49  Lock(a,b,c) SQLI
1448d 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20  TE_OK.  #define 
1448e 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61  setSharedCacheTa
1448f 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53  bleLock(a,b,c) S
14490 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69  QLITE_OK.  #defi
14491 6e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65  ne clearAllShare
14492 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73  dCacheTableLocks
14493 28 61 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  (a).#endif..#ifn
14494 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14495 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
14496 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65 20  ** Query to see 
14497 69 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20  if btree handle 
14498 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c  p may obtain a l
14499 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63  ock of type eLoc
1449a 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b  k .** (READ_LOCK
1449b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20   or WRITE_LOCK) 
1449c 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  on the table wit
1449d 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62  h root-page iTab
1449e 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  . Return.** SQLI
1449f 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63  TE_OK if the loc
144a0 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65  k may be obtaine
144a1 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a  d (by calling.**
144a2 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54   setSharedCacheT
144a3 61 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 20  ableLock()), or 
144a4 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66  SQLITE_LOCKED if
144a5 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   not..*/.static 
144a6 69 6e 74 20 71 75 65 72 79 53 68 61 72 65 64 43  int querySharedC
144a7 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74  acheTableLock(Bt
144a8 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61  ree *p, Pgno iTa
144a9 62 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20  b, u8 eLock){.  
144aa 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
144ab 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b  p->pBt;.  BtLock
144ac 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65   *pIter;..  asse
144ad 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
144ae 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b  HoldsMutex(p) );
144af 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b  .  assert( eLock
144b0 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65  ==READ_LOCK || e
144b1 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
144b2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
144b3 3e 64 62 21 3d 30 20 29 3b 0a 20 20 0a 20 20 2f  >db!=0 );.  .  /
144b4 2a 20 49 66 20 72 65 71 75 65 73 74 69 6e 67 20  * If requesting 
144b5 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 68  a write-lock, th
144b6 65 6e 20 74 68 65 20 42 74 72 65 65 20 6d 75 73  en the Btree mus
144b7 74 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77  t have an open w
144b8 72 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61  rite.  ** transa
144b9 63 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66 69  ction on this fi
144ba 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75 73  le. And, obvious
144bb 6c 79 2c 20 66 6f 72 20 74 68 69 73 20 74 6f 20  ly, for this to 
144bc 62 65 20 73 6f 20 74 68 65 72 65 20 0a 20 20 2a  be so there .  *
144bd 2a 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65  * must be an ope
144be 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  n write transact
144bf 69 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65 20  ion on the file 
144c0 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 61  itself..  */.  a
144c1 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45  ssert( eLock==RE
144c2 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d 70  AD_LOCK || (p==p
144c3 42 74 2d 3e 70 57 72 69 74 65 72 20 26 26 20 70  Bt->pWriter && p
144c4 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
144c5 5f 57 52 49 54 45 29 20 29 3b 0a 20 20 61 73 73  _WRITE) );.  ass
144c6 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44  ert( eLock==READ
144c7 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 6e  _LOCK || pBt->in
144c8 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
144c9 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a 20  NS_WRITE );.  . 
144ca 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f   /* This is a no
144cb 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 72 65  -op if the share
144cc 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20 65  d-cache is not e
144cd 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 20  nabled */.  if( 
144ce 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a  !p->sharable ){.
144cf 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
144d0 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
144d1 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f  If some other co
144d2 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64  nnection is hold
144d3 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ing an exclusive
144d4 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a 20   lock, the.  ** 
144d5 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 6d  requested lock m
144d6 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e  ay not be obtain
144d7 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ed..  */.  if( p
144d8 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 20 26  Bt->pWriter!=p &
144d9 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69  & pBt->isExclusi
144da 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ve ){.    sqlite
144db 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b  3ConnectionBlock
144dc 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70  ed(p->db, pBt->p
144dd 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 20  Writer->db);.   
144de 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
144df 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
144e0 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69  E;.  }..  /* Thi
144e1 73 20 28 61 6c 6f 6e 67 20 77 69 74 68 20 73 65  s (along with se
144e2 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  tSharedCacheTabl
144e3 65 4c 6f 63 6b 28 29 29 20 69 73 20 77 68 65 72  eLock()) is wher
144e4 65 0a 20 20 2a 2a 20 74 68 65 20 52 65 61 64 55  e.  ** the ReadU
144e5 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20  ncommitted flag 
144e6 69 73 20 64 65 61 6c 74 20 77 69 74 68 2e 0a 20  is dealt with.. 
144e7 20 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65   ** If the calle
144e8 72 20 69 73 20 71 75 65 72 79 69 6e 67 20 66 6f  r is querying fo
144e9 72 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  r a read-lock on
144ea 20 61 6e 79 20 74 61 62 6c 65 0a 20 20 2a 2a 20   any table.  ** 
144eb 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 73  other than the s
144ec 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
144ed 6c 65 20 28 74 61 62 6c 65 20 31 29 20 61 6e 64  le (table 1) and
144ee 20 69 66 20 74 68 65 20 52 65 61 64 55 6e 63 6f   if the ReadUnco
144ef 6d 6d 69 74 74 65 64 0a 20 20 2a 2a 20 66 6c 61  mmitted.  ** fla
144f0 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74  g is set, then t
144f1 68 65 20 6c 6f 63 6b 20 67 72 61 6e 74 65 64 20  he lock granted 
144f2 65 76 65 6e 20 69 66 20 74 68 65 72 65 20 61 72  even if there ar
144f3 65 20 77 72 69 74 65 2d 6c 6f 63 6b 73 0a 20 20  e write-locks.  
144f4 2a 2a 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e  ** on the table.
144f5 20 49 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b   If a write-lock
144f6 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74   is requested, t
144f7 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  he ReadUncommitt
144f8 65 64 20 66 6c 61 67 0a 20 20 2a 2a 20 69 73 20  ed flag.  ** is 
144f9 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a  not considered..
144fa 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 66 75 6e    **.  ** In fun
144fb 63 74 69 6f 6e 20 73 65 74 53 68 61 72 65 64 43  ction setSharedC
144fc 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c  acheTableLock(),
144fd 20 69 66 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20   if a read-lock 
144fe 69 73 20 64 65 6d 61 6e 64 65 64 20 61 6e 64 20  is demanded and 
144ff 74 68 65 20 0a 20 20 2a 2a 20 52 65 61 64 55 6e  the .  ** ReadUn
14500 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69  committed flag i
14501 73 20 73 65 74 2c 20 6e 6f 20 65 6e 74 72 79 20  s set, no entry 
14502 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  is added to the 
14503 6c 6f 63 6b 73 20 6c 69 73 74 20 0a 20 20 2a 2a  locks list .  **
14504 20 28 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b   (BtShared.pLock
14505 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20  )..  **.  ** To 
14506 73 75 6d 6d 61 72 69 7a 65 3a 20 49 66 20 74 68  summarize: If th
14507 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  e ReadUncommitte
14508 64 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  d flag is set, t
14509 68 65 6e 20 72 65 61 64 20 63 75 72 73 6f 72 73  hen read cursors
1450a 0a 20 20 2a 2a 20 6f 6e 20 6e 6f 6e 2d 73 63 68  .  ** on non-sch
1450b 65 6d 61 20 74 61 62 6c 65 73 20 64 6f 20 6e 6f  ema tables do no
1450c 74 20 63 72 65 61 74 65 20 6f 72 20 72 65 73 70  t create or resp
1450d 65 63 74 20 74 61 62 6c 65 20 6c 6f 63 6b 73 2e  ect table locks.
1450e 20 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a   The locking.  *
1450f 2a 20 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20  * procedure for 
14510 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72 20 64  a write-cursor d
14511 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 2e 0a  oes not change..
14512 20 20 2a 2f 0a 20 20 69 66 28 20 0a 20 20 20 20    */.  if( .    
14513 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73  0==(p->db->flags
14514 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f  &SQLITE_ReadUnco
14515 6d 6d 69 74 74 65 64 29 20 7c 7c 20 0a 20 20 20  mmitted) || .   
14516 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f   eLock==WRITE_LO
14517 43 4b 20 7c 7c 0a 20 20 20 20 69 54 61 62 3d 3d  CK ||.    iTab==
14518 4d 41 53 54 45 52 5f 52 4f 4f 54 0a 20 20 29 7b  MASTER_ROOT.  ){
14519 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 3d 70  .    for(pIter=p
1451a 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72  Bt->pLock; pIter
1451b 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70  ; pIter=pIter->p
1451c 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 2f 2a 20  Next){.      /* 
1451d 54 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 28 70  The condition (p
1451e 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f  Iter->eLock!=eLo
1451f 63 6b 29 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  ck) in the follo
14520 77 69 6e 67 20 69 66 28 2e 2e 2e 29 20 0a 20 20  wing if(...) .  
14521 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
14522 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69 63 61   is a simplifica
14523 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 20 20 2a  tion of:.      *
14524 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 28 65 4c  *.      **   (eL
14525 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20  ock==WRITE_LOCK 
14526 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d  || pIter->eLock=
14527 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20  =WRITE_LOCK).   
14528 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 73     **.      ** s
14529 69 6e 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61  ince we know tha
1452a 74 20 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54  t if eLock==WRIT
1452b 45 5f 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20  E_LOCK, then no 
1452c 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  other connection
1452d 0a 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 68 6f  .      ** may ho
1452e 6c 64 20 61 20 57 52 49 54 45 5f 4c 4f 43 4b 20  ld a WRITE_LOCK 
1452f 6f 6e 20 61 6e 79 20 74 61 62 6c 65 20 69 6e 20  on any table in 
14530 74 68 69 73 20 66 69 6c 65 20 28 73 69 6e 63 65  this file (since
14531 20 74 68 65 72 65 20 63 61 6e 0a 20 20 20 20 20   there can.     
14532 20 2a 2a 20 6f 6e 6c 79 20 62 65 20 61 20 73 69   ** only be a si
14533 6e 67 6c 65 20 77 72 69 74 65 72 29 2e 0a 20 20  ngle writer)..  
14534 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73      */.      ass
14535 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f 63  ert( pIter->eLoc
14536 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20  k==READ_LOCK || 
14537 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52  pIter->eLock==WR
14538 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  ITE_LOCK );.    
14539 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d    assert( eLock=
1453a 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49  =READ_LOCK || pI
1453b 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c  ter->pBtree==p |
1453c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d  | pIter->eLock==
1453d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  READ_LOCK);.    
1453e 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74    if( pIter->pBt
1453f 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d  ree!=p && pIter-
14540 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26  >iTable==iTab &&
14541 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65   pIter->eLock!=e
14542 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  Lock ){.        
14543 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
14544 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20  nBlocked(p->db, 
14545 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64  pIter->pBtree->d
14546 62 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  b);.        if( 
14547 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43  eLock==WRITE_LOC
14548 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  K ){.          a
14549 73 73 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e 70  ssert( p==pBt->p
1454a 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20  Writer );.      
1454b 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69      pBt->isPendi
1454c 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ng = 1;.        
1454d 7d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  }.        return
1454e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53   SQLITE_LOCKED_S
1454f 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20  HAREDCACHE;.    
14550 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
14551 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
14552 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
14553 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
14554 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e  D_CACHE */..#ifn
14555 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14556 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
14557 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e  ** Add a lock on
14558 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20   the table with 
14559 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65  root-page iTable
1455a 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62   to the shared-b
1455b 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20  tree used.** by 
1455c 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20  Btree handle p. 
1455d 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20  Parameter eLock 
1455e 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 52  must be either R
1455f 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20  EAD_LOCK or .** 
14560 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  WRITE_LOCK..**.*
14561 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
14562 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c  eturned if the l
14563 6f 63 6b 20 69 73 20 61 64 64 65 64 20 73 75 63  ock is added suc
14564 63 65 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54  cessfully. SQLIT
14565 45 5f 42 55 53 59 20 61 6e 64 0a 2a 2a 20 53 51  E_BUSY and.** SQ
14566 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61  LITE_NOMEM may a
14567 6c 73 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e  lso be returned.
14568 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
14569 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62  etSharedCacheTab
1456a 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c  leLock(Btree *p,
1456b 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38   Pgno iTable, u8
1456c 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61   eLock){.  BtSha
1456d 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1456e 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f  t;.  BtLock *pLo
1456f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b  ck = 0;.  BtLock
14570 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65   *pIter;..  asse
14571 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
14572 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b  HoldsMutex(p) );
14573 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b  .  assert( eLock
14574 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65  ==READ_LOCK || e
14575 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
14576 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
14577 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  >db!=0 );..  /* 
14578 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20  This is a no-op 
14579 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 61  if the shared-ca
1457a 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c  che is not enabl
1457b 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e  ed */.  if( !p->
1457c 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
1457d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1457e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
1457f 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72   SQLITE_OK==quer
14580 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  ySharedCacheTabl
14581 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c  eLock(p, iTable,
14582 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a   eLock) );..  /*
14583 20 49 66 20 74 68 65 20 72 65 61 64 2d 75 6e 63   If the read-unc
14584 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73  ommitted flag is
14585 20 73 65 74 20 61 6e 64 20 61 20 72 65 61 64 2d   set and a read-
14586 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65  lock is requeste
14587 64 20 6f 6e 0a 20 20 2a 2a 20 61 20 6e 6f 6e 2d  d on.  ** a non-
14588 73 63 68 65 6d 61 20 74 61 62 6c 65 2c 20 74 68  schema table, th
14589 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61  en the lock is a
1458a 6c 77 61 79 73 20 67 72 61 6e 74 65 64 2e 20 20  lways granted.  
1458b 52 65 74 75 72 6e 20 65 61 72 6c 79 0a 20 20 2a  Return early.  *
1458c 2a 20 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67  * without adding
1458d 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 65   an entry to the
1458e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20   BtShared.pLock 
1458f 6c 69 73 74 2e 20 53 65 65 0a 20 20 2a 2a 20 63  list. See.  ** c
14590 6f 6d 6d 65 6e 74 20 69 6e 20 66 75 6e 63 74 69  omment in functi
14591 6f 6e 20 71 75 65 72 79 53 68 61 72 65 64 43 61  on querySharedCa
14592 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 66  cheTableLock() f
14593 6f 72 20 6d 6f 72 65 20 69 6e 66 6f 0a 20 20 2a  or more info.  *
14594 2a 20 6f 6e 20 68 61 6e 64 6c 69 6e 67 20 74 68  * on handling th
14595 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  e ReadUncommitte
14596 64 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20 20 69  d flag..  */.  i
14597 66 28 20 0a 20 20 20 20 28 70 2d 3e 64 62 2d 3e  f( .    (p->db->
14598 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61  flags&SQLITE_Rea
14599 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 26 26  dUncommitted) &&
1459a 20 0a 20 20 20 20 28 65 4c 6f 63 6b 3d 3d 52 45   .    (eLock==RE
1459b 41 44 5f 4c 4f 43 4b 29 20 26 26 0a 20 20 20 20  AD_LOCK) &&.    
1459c 69 54 61 62 6c 65 21 3d 4d 41 53 54 45 52 5f 52  iTable!=MASTER_R
1459d 4f 4f 54 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  OOT.  ){.    ret
1459e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1459f 20 7d 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73   }..  /* First s
145a0 65 61 72 63 68 20 74 68 65 20 6c 69 73 74 20 66  earch the list f
145a1 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c  or an existing l
145a2 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c  ock on this tabl
145a3 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65  e. */.  for(pIte
145a4 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49  r=pBt->pLock; pI
145a5 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72  ter; pIter=pIter
145a6 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
145a7 28 20 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d  ( pIter->iTable=
145a8 3d 69 54 61 62 6c 65 20 26 26 20 70 49 74 65 72  =iTable && pIter
145a9 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20  ->pBtree==p ){. 
145aa 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74       pLock = pIt
145ab 65 72 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  er;.      break;
145ac 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
145ad 20 49 66 20 74 68 65 20 61 62 6f 76 65 20 73 65   If the above se
145ae 61 72 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e  arch did not fin
145af 64 20 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63  d a BtLock struc
145b0 74 20 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74  t associating Bt
145b1 72 65 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20  ree p.  ** with 
145b2 74 61 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c  table iTable, al
145b3 6c 6f 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c  locate one and l
145b4 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20  ink it into the 
145b5 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  list..  */.  if(
145b6 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70   !pLock ){.    p
145b7 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a  Lock = (BtLock *
145b8 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65  )sqlite3MallocZe
145b9 72 6f 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b  ro(sizeof(BtLock
145ba 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f  ));.    if( !pLo
145bb 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ck ){.      retu
145bc 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
145bd 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b  .    }.    pLock
145be 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c  ->iTable = iTabl
145bf 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42  e;.    pLock->pB
145c0 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c  tree = p;.    pL
145c1 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74  ock->pNext = pBt
145c2 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74  ->pLock;.    pBt
145c3 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b  ->pLock = pLock;
145c4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74  .  }..  /* Set t
145c5 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20  he BtLock.eLock 
145c6 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20  variable to the 
145c7 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63  maximum of the c
145c8 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a  urrent lock.  **
145c9 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74   and the request
145ca 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65  ed lock. This me
145cb 61 6e 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c  ans if a write-l
145cc 6f 63 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20  ock was already 
145cd 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20  held.  ** and a 
145ce 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73  read-lock reques
145cf 74 65 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e  ted, we don't in
145d0 63 6f 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72  correctly downgr
145d1 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20  ade the lock..  
145d2 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 57 52 49  */.  assert( WRI
145d3 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43  TE_LOCK>READ_LOC
145d4 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b  K );.  if( eLock
145d5 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b  >pLock->eLock ){
145d6 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63  .    pLock->eLoc
145d7 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a  k = eLock;.  }..
145d8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
145d9 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  OK;.}.#endif /* 
145da 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  !SQLITE_OMIT_SHA
145db 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69  RED_CACHE */..#i
145dc 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
145dd 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f  T_SHARED_CACHE./
145de 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c  *.** Release all
145df 20 74 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73   the table locks
145e0 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64   (locks obtained
145e1 20 76 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a   via calls to.**
145e2 20 74 68 65 20 73 65 74 53 68 61 72 65 64 43 61   the setSharedCa
145e3 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70  cheTableLock() p
145e4 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20 62  rocedure) held b
145e5 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70  y Btree handle p
145e6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
145e7 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ction assumes th
145e8 61 74 20 68 61 6e 64 6c 65 20 70 20 68 61 73 20  at handle p has 
145e9 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f 72 20  an open read or 
145ea 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e 73 61  write .** transa
145eb 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64 6f 65  ction. If it doe
145ec 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20  s not, then the 
145ed 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e 64 69  BtShared.isPendi
145ee 6e 67 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d  ng variable.** m
145ef 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c  ay be incorrectl
145f0 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74  y cleared..*/.st
145f1 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41  atic void clearA
145f2 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62  llSharedCacheTab
145f3 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70  leLocks(Btree *p
145f4 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
145f5 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42  Bt = p->pBt;.  B
145f6 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d  tLock **ppIter =
145f7 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20   &pBt->pLock;.. 
145f8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
145f9 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
145fa 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p) );.  assert( 
145fb 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30  p->sharable || 0
145fc 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61  ==*ppIter );.  a
145fd 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
145fe 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28  s>0 );..  while(
145ff 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20   *ppIter ){.    
14600 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20  BtLock *pLock = 
14601 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73  *ppIter;.    ass
14602 65 72 74 28 20 70 42 74 2d 3e 69 73 45 78 63 6c  ert( pBt->isExcl
14603 75 73 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d  usive==0 || pBt-
14604 3e 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d  >pWriter==pLock-
14605 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61  >pBtree );.    a
14606 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42  ssert( pLock->pB
14607 74 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70  tree->inTrans>=p
14608 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20  Lock->eLock );. 
14609 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42     if( pLock->pB
1460a 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20  tree==p ){.     
1460b 20 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b   *ppIter = pLock
1460c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73  ->pNext;.      s
1460d 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f 63  qlite3_free(pLoc
1460e 6b 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  k);.    }else{. 
1460f 20 20 20 20 20 70 70 49 74 65 72 20 3d 20 26 70       ppIter = &p
14610 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Lock->pNext;.   
14611 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74   }.  }..  assert
14612 28 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67  ( pBt->isPending
14613 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69  ==0 || pBt->pWri
14614 74 65 72 20 29 3b 0a 20 20 69 66 28 20 70 42 74  ter );.  if( pBt
14615 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a  ->pWriter==p ){.
14616 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72      pBt->pWriter
14617 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69   = 0;.    pBt->i
14618 73 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a  sExclusive = 0;.
14619 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69      pBt->isPendi
1461a 6e 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  ng = 0;.  }else 
1461b 69 66 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61  if( pBt->nTransa
1461c 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20  ction==2 ){.    
1461d 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
1461e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
1461f 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 69 73 20  connection p is 
14620 63 6f 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 0a  concluding its .
14621 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69      ** transacti
14622 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 63 75 72  on. If there cur
14623 72 65 6e 74 6c 79 20 65 78 69 73 74 73 20 61 20  rently exists a 
14624 77 72 69 74 65 72 2c 20 61 6e 64 20 70 20 69 73  writer, and p is
14625 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74   not.    ** that
14626 20 77 72 69 74 65 72 2c 20 74 68 65 6e 20 74 68   writer, then th
14627 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b  e number of lock
14628 73 20 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63  s held by connec
14629 74 69 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 20  tions other.    
1462a 2a 2a 20 74 68 61 6e 20 74 68 65 20 77 72 69 74  ** than the writ
1462b 65 72 20 6d 75 73 74 20 62 65 20 61 62 6f 75 74  er must be about
1462c 20 74 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f   to drop to zero
1462d 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20  . In this case. 
1462e 20 20 20 2a 2a 20 73 65 74 20 74 68 65 20 69 73     ** set the is
1462f 50 65 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f 20  Pending flag to 
14630 30 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  0..    **.    **
14631 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74   If there is not
14632 20 63 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69   currently a wri
14633 74 65 72 2c 20 74 68 65 6e 20 42 74 53 68 61 72  ter, then BtShar
14634 65 64 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73  ed.isPending mus
14635 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f  t.    ** be zero
14636 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69   already. So thi
14637 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68  s next line is h
14638 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20  armless in that 
14639 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  case..    */.   
1463a 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20   pBt->isPending 
1463b 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  = 0;.  }.}.#endi
1463c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1463d 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f  _SHARED_CACHE */
1463e 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  ..static void re
1463f 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67  leasePage(MemPag
14640 65 20 2a 70 50 61 67 65 29 3b 20 20 2f 2a 20 46  e *pPage);  /* F
14641 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65  orward reference
14642 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 69 66   */../*.** Verif
14643 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  y that the curso
14644 72 20 68 6f 6c 64 73 20 61 20 6d 75 74 65 78 20  r holds a mutex 
14645 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 0a  on the BtShared.
14646 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  */.#ifndef NDEBU
14647 47 0a 73 74 61 74 69 63 20 69 6e 74 20 63 75 72  G.static int cur
14648 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 42 74  sorHoldsMutex(Bt
14649 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 72 65  Cursor *p){.  re
1464a 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74  turn sqlite3_mut
1464b 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e  ex_held(p->pBt->
1464c 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66  mutex);.}.#endif
1464d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1464e 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
1464f 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65  /*.** Invalidate
14650 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
14651 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f  ge-list cache fo
14652 72 20 63 75 72 73 6f 72 20 70 43 75 72 2c 20 69  r cursor pCur, i
14653 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  f any..*/.static
14654 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65   void invalidate
14655 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 42 74  OverflowCache(Bt
14656 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
14657 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
14658 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
14659 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
1465a 65 28 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  e(pCur->aOverflo
1465b 77 29 3b 0a 20 20 70 43 75 72 2d 3e 61 4f 76 65  w);.  pCur->aOve
1465c 72 66 6c 6f 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  rflow = 0;.}../*
1465d 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74  .** Invalidate t
1465e 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
1465f 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20  -list cache for 
14660 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e  all cursors open
14661 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61  ed.** on the sha
14662 72 65 64 20 62 74 72 65 65 20 73 74 72 75 63 74  red btree struct
14663 75 72 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74  ure pBt..*/.stat
14664 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61  ic void invalida
14665 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63  teAllOverflowCac
14666 68 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  he(BtShared *pBt
14667 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  ){.  BtCursor *p
14668 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
14669 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1466a 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
1466b 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73  for(p=pBt->pCurs
1466c 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  or; p; p=p->pNex
1466d 74 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61  t){.    invalida
1466e 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  teOverflowCache(
1466f 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a  p);.  }.}.#else.
14670 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69    #define invali
14671 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68  dateOverflowCach
14672 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 69  e(x).  #define i
14673 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72  nvalidateAllOver
14674 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 23 65 6e  flowCache(x).#en
14675 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62  dif../*.** Set b
14676 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42  it pgno of the B
14677 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74  tShared.pHasCont
14678 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73  ent bitvec. This
14679 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77   is called .** w
1467a 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 74 20  hen a page that 
1467b 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61  previously conta
1467c 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65  ined data become
1467d 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65  s a free-list le
1467e 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a  af .** page..**.
1467f 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 2e  ** The BtShared.
14680 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76  pHasContent bitv
14681 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72  ec exists to wor
14682 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63  k around an obsc
14683 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65  ure.** bug cause
14684 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 61 63  d by the interac
14685 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66  tion of two usef
14686 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69  ul IO optimizati
14687 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a  ons surrounding.
14688 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61  ** free-list lea
14689 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  f pages:.**.**  
1468a 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74   1) When all dat
1468b 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f  a is deleted fro
1468c 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 68 65  m a page and the
1468d 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a   page becomes.**
1468e 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73        a free-lis
1468f 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65  t leaf page, the
14690 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69   page is not wri
14691 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61  tten to the data
14692 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73  base.**      (as
14693 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
14694 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f  pages contain no
14695 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61   meaningful data
14696 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20  ). Sometimes.** 
14697 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67 65       such a page
14698 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75   is not even jou
14699 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77  rnalled (as it w
1469a 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66  ill not be modif
1469b 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79  ied,.**      why
1469c 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c   bother journall
1469d 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20  ing it?)..**.** 
1469e 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 65 65    2) When a free
1469f 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20  -list leaf page 
146a0 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 20 63  is reused, its c
146a1 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65  ontent is not re
146a2 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20  ad.**      from 
146a3 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
146a4 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
146a5 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79  ournal file (why
146a6 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20   should it.**   
146a7 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 73 20     be, if it is 
146a8 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69  not at all meani
146a9 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42  ngful?)..**.** B
146aa 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68  y themselves, th
146ab 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ese optimization
146ac 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20  s work fine and 
146ad 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a  provide a handy.
146ae 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62  ** performance b
146af 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c  oost to bulk del
146b0 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70  ete or insert op
146b1 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65  erations. Howeve
146b2 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20  r, if.** a page 
146b3 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20  is moved to the 
146b4 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68  free-list and th
146b5 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 69 6e  en reused within
146b6 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61   the same.** tra
146b7 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62  nsaction, a prob
146b8 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66  lem comes up. If
146b9 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
146ba 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e   journalled when
146bb 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20  .** it is moved 
146bc 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  to the free-list
146bd 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20   and it is also 
146be 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77  not journalled w
146bf 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74  hen it.** is ext
146c0 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20  racted from the 
146c1 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65  free-list and re
146c2 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f  used, then the o
146c3 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20  riginal data.** 
146c4 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20  may be lost. In 
146c5 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72  the event of a r
146c6 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20  ollback, it may 
146c7 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a  not be possible.
146c8 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68  ** to restore th
146c9 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74  e database to it
146ca 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69  s original confi
146cb 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  guration..**.** 
146cc 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20  The solution is 
146cd 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61  the BtShared.pHa
146ce 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e  sContent bitvec.
146cf 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67 65   Whenever a page
146d0 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f   is .** moved to
146d1 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c   become a free-l
146d2 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74  ist leaf page, t
146d3 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
146d4 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69   bit is.** set i
146d5 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 57 68  n the bitvec. Wh
146d6 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 70 61  enever a leaf pa
146d7 67 65 20 69 73 20 65 78 74 72 61 63 74 65 64 20  ge is extracted 
146d8 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69  from the free-li
146d9 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74  st,.** optimizat
146da 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f  ion 2 above is o
146db 6d 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 63  mmitted if the c
146dc 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74  orresponding bit
146dd 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73   is already.** s
146de 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70  et in BtShared.p
146df 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20  HasContent. The 
146e0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
146e1 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 72  bitvec are clear
146e2 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64  ed.** at the end
146e3 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 61   of every transa
146e4 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
146e5 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 73   int btreeSetHas
146e6 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64  Content(BtShared
146e7 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f   *pBt, Pgno pgno
146e8 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
146e9 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21  LITE_OK;.  if( !
146ea 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74  pBt->pHasContent
146eb 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67   ){.    int nPag
146ec 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e;.    rc = sqli
146ed 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
146ee 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26  t(pBt->pPager, &
146ef 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  nPage);.    if( 
146f0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
146f1 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 48 61 73  .      pBt->pHas
146f2 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65  Content = sqlite
146f3 33 42 69 74 76 65 63 43 72 65 61 74 65 28 28 75  3BitvecCreate((u
146f4 33 32 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 20  32)nPage);.     
146f5 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43   if( !pBt->pHasC
146f6 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  ontent ){.      
146f7 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
146f8 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
146f9 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d   }.  }.  if( rc=
146fa 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 67  =SQLITE_OK && pg
146fb 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 69 74 76 65  no<=sqlite3Bitve
146fc 63 53 69 7a 65 28 70 42 74 2d 3e 70 48 61 73 43  cSize(pBt->pHasC
146fd 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 20 20 20 72  ontent) ){.    r
146fe 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  c = sqlite3Bitve
146ff 63 53 65 74 28 70 42 74 2d 3e 70 48 61 73 43 6f  cSet(pBt->pHasCo
14700 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20  ntent, pgno);.  
14701 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
14702 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68  ../*.** Query th
14703 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43  e BtShared.pHasC
14704 6f 6e 74 65 6e 74 20 76 65 63 74 6f 72 2e 0a 2a  ontent vector..*
14705 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
14706 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  on is called whe
14707 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65  n a free-list le
14708 61 66 20 70 61 67 65 20 69 73 20 72 65 6d 6f 76  af page is remov
14709 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 66  ed from the.** f
1470a 72 65 65 2d 6c 69 73 74 20 66 6f 72 20 72 65 75  ree-list for reu
1470b 73 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 66  se. It returns f
1470c 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 61  alse if it is sa
1470d 66 65 20 74 6f 20 72 65 74 72 69 65 76 65 20 74  fe to retrieve t
1470e 68 65 0a 2a 2a 20 70 61 67 65 20 66 72 6f 6d 20  he.** page from 
1470f 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 20  the pager layer 
14710 77 69 74 68 20 74 68 65 20 27 6e 6f 2d 63 6f 6e  with the 'no-con
14711 74 65 6e 74 27 20 66 6c 61 67 20 73 65 74 2e 20  tent' flag set. 
14712 54 72 75 65 20 6f 74 68 65 72 77 69 73 65 2e 0a  True otherwise..
14713 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74  */.static int bt
14714 72 65 65 47 65 74 48 61 73 43 6f 6e 74 65 6e 74  reeGetHasContent
14715 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
14716 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 42 69  Pgno pgno){.  Bi
14717 74 76 65 63 20 2a 70 20 3d 20 70 42 74 2d 3e 70  tvec *p = pBt->p
14718 48 61 73 43 6f 6e 74 65 6e 74 3b 0a 20 20 72 65  HasContent;.  re
14719 74 75 72 6e 20 28 70 20 26 26 20 28 70 67 6e 6f  turn (p && (pgno
1471a 3e 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69  >sqlite3BitvecSi
1471b 7a 65 28 70 29 20 7c 7c 20 73 71 6c 69 74 65 33  ze(p) || sqlite3
1471c 42 69 74 76 65 63 54 65 73 74 28 70 2c 20 70 67  BitvecTest(p, pg
1471d 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  no)));.}../*.** 
1471e 43 6c 65 61 72 20 28 64 65 73 74 72 6f 79 29 20  Clear (destroy) 
1471f 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61  the BtShared.pHa
14720 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e  sContent bitvec.
14721 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 0a   This should be.
14722 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68  ** invoked at th
14723 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20  e conclusion of 
14724 65 61 63 68 20 77 72 69 74 65 2d 74 72 61 6e 73  each write-trans
14725 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  action..*/.stati
14726 63 20 76 6f 69 64 20 62 74 72 65 65 43 6c 65 61  c void btreeClea
14727 72 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68  rHasContent(BtSh
14728 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71  ared *pBt){.  sq
14729 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
1472a 6f 79 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74  oy(pBt->pHasCont
1472b 65 6e 74 29 3b 0a 20 20 70 42 74 2d 3e 70 48 61  ent);.  pBt->pHa
1472c 73 43 6f 6e 74 65 6e 74 20 3d 20 30 3b 0a 7d 0a  sContent = 0;.}.
1472d 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20  ./*.** Save the 
1472e 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70  current cursor p
1472f 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 76  osition in the v
14730 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f  ariables BtCurso
14731 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 20 42  r.nKey .** and B
14732 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e 20 54 68  tCursor.pKey. Th
14733 65 20 63 75 72 73 6f 72 27 73 20 73 74 61 74 65  e cursor's state
14734 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f   is set to CURSO
14735 52 5f 52 45 51 55 49 52 45 53 45 45 4b 2e 0a 2a  R_REQUIRESEEK..*
14736 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76  /.static int sav
14737 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
14738 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
14739 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
1473a 73 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c  sert( CURSOR_VAL
1473b 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65  ID==pCur->eState
1473c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d   );.  assert( 0=
1473d 3d 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 20  =pCur->pKey );. 
1473e 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
1473f 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
14740 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  );..  rc = sqlit
14741 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70  e3BtreeKeySize(p
14742 43 75 72 2c 20 26 70 43 75 72 2d 3e 6e 4b 65 79  Cur, &pCur->nKey
14743 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  );..  /* If this
14744 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61   is an intKey ta
14745 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62  ble, then the ab
14746 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65  ove call to Btre
14747 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20  eKeySize().  ** 
14748 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67  stores the integ
14749 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e  er key in pCur->
1474a 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61  nKey. In this ca
1474b 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 73  se this value is
1474c 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69  .  ** all that i
1474d 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65  s required. Othe
1474e 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 69  rwise, if pCur i
1474f 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e  s not open on an
14750 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62   intKey.  ** tab
14751 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20  le, then malloc 
14752 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74  space for and st
14753 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b  ore the pCur->nK
14754 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20  ey bytes of key 
14755 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f  .  ** data..  */
14756 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
14757 45 5f 4f 4b 20 26 26 20 30 3d 3d 70 43 75 72 2d  E_OK && 0==pCur-
14758 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b  >apPage[0]->intK
14759 65 79 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70  ey){.    void *p
1475a 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Key = sqlite3Mal
1475b 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e  loc( (int)pCur->
1475c 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20  nKey );.    if( 
1475d 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63  pKey ){.      rc
1475e 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
1475f 65 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74  ey(pCur, 0, (int
14760 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65  )pCur->nKey, pKe
14761 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  y);.      if( rc
14762 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
14763 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65         pCur->pKe
14764 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20  y = pKey;.      
14765 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
14766 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79  qlite3_free(pKey
14767 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
14768 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
14769 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1476a 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
1476b 74 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65  t( !pCur->apPage
1476c 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21  [0]->intKey || !
1476d 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20  pCur->pKey );.. 
1476e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1476f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  OK ){.    int i;
14770 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
14771 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b  =pCur->iPage; i+
14772 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73  +){.      releas
14773 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61  ePage(pCur->apPa
14774 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43  ge[i]);.      pC
14775 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20  ur->apPage[i] = 
14776 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75  0;.    }.    pCu
14777 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20  r->iPage = -1;. 
14778 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
14779 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  = CURSOR_REQUIRE
1477a 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76  SEEK;.  }..  inv
1477b 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43  alidateOverflowC
1477c 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65  ache(pCur);.  re
1477d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1477e 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74  * Save the posit
1477f 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73  ions of all curs
14780 6f 72 73 20 65 78 63 65 70 74 20 70 45 78 63 65  ors except pExce
14781 70 74 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74  pt open on the t
14782 61 62 6c 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f  able .** with ro
14783 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55  ot-page iRoot. U
14784 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20  sually, this is 
14785 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f  called just befo
14786 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78  re cursor.** pEx
14787 63 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20  cept is used to 
14788 6d 6f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65  modify the table
14789 20 28 42 74 72 65 65 44 65 6c 65 74 65 28 29 20   (BtreeDelete() 
1478a 6f 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 29  or BtreeInsert()
1478b 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
1478c 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28   saveAllCursors(
1478d 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
1478e 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72  gno iRoot, BtCur
1478f 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20  sor *pExcept){. 
14790 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20   BtCursor *p;.  
14791 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14792 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
14793 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
14794 72 74 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c  rt( pExcept==0 |
14795 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d  | pExcept->pBt==
14796 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70  pBt );.  for(p=p
14797 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
14798 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
14799 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 20   if( p!=pExcept 
1479a 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20  && (0==iRoot || 
1479b 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f  p->pgnoRoot==iRo
1479c 6f 74 29 20 26 26 20 0a 20 20 20 20 20 20 20 20  ot) && .        
1479d 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  p->eState==CURSO
1479e 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20  R_VALID ){.     
1479f 20 69 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75   int rc = saveCu
147a0 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b  rsorPosition(p);
147a1 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
147a2 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK!=rc ){.    
147a3 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
147a4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
147a5 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
147a6 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
147a7 65 61 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  ear the current 
147a8 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e  cursor position.
147a9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
147aa 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
147ab 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28  treeClearCursor(
147ac 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
147ad 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
147ae 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
147af 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ) );.  sqlite3_f
147b0 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b  ree(pCur->pKey);
147b1 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20  .  pCur->pKey = 
147b2 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74  0;.  pCur->eStat
147b3 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  e = CURSOR_INVAL
147b4 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73  ID;.}../*.** Res
147b5 74 6f 72 65 20 74 68 65 20 63 75 72 73 6f 72 20  tore the cursor 
147b6 74 6f 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20  to the position 
147b7 69 74 20 77 61 73 20 69 6e 20 28 6f 72 20 61 73  it was in (or as
147b8 20 63 6c 6f 73 65 20 74 6f 20 61 73 20 70 6f 73   close to as pos
147b9 73 69 62 6c 65 29 0a 2a 2a 20 77 68 65 6e 20 73  sible).** when s
147ba 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  aveCursorPositio
147bb 6e 28 29 20 77 61 73 20 63 61 6c 6c 65 64 2e 20  n() was called. 
147bc 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 63  Note that this c
147bd 61 6c 6c 20 64 65 6c 65 74 65 73 20 74 68 65 20  all deletes the 
147be 0a 2a 2a 20 73 61 76 65 64 20 70 6f 73 69 74 69  .** saved positi
147bf 6f 6e 20 69 6e 66 6f 20 73 74 6f 72 65 64 20 62  on info stored b
147c0 79 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69  y saveCursorPosi
147c1 74 69 6f 6e 28 29 2c 20 73 6f 20 74 68 65 72 65  tion(), so there
147c2 20 63 61 6e 20 62 65 0a 2a 2a 20 61 74 20 6d 6f   can be.** at mo
147c3 73 74 20 6f 6e 65 20 65 66 66 65 63 74 69 76 65  st one effective
147c4 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
147c5 73 69 74 69 6f 6e 28 29 20 63 61 6c 6c 20 61 66  sition() call af
147c6 74 65 72 20 65 61 63 68 20 0a 2a 2a 20 73 61 76  ter each .** sav
147c7 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
147c8 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
147c9 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
147ca 42 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73  BtreeRestoreCurs
147cb 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72  orPosition(BtCur
147cc 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e  sor *pCur){.  in
147cd 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
147ce 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
147cf 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
147d0 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
147d1 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  >=CURSOR_REQUIRE
147d2 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43  SEEK );.  if( pC
147d3 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
147d4 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20  OR_FAULT ){.    
147d5 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69  return pCur->ski
147d6 70 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65  p;.  }.  pCur->e
147d7 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49  State = CURSOR_I
147d8 4e 56 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 73  NVALID;.  rc = s
147d9 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74  qlite3BtreeMovet
147da 6f 28 70 43 75 72 2c 20 70 43 75 72 2d 3e 70 4b  o(pCur, pCur->pK
147db 65 79 2c 20 70 43 75 72 2d 3e 6e 4b 65 79 2c 20  ey, pCur->nKey, 
147dc 30 2c 20 26 70 43 75 72 2d 3e 73 6b 69 70 29 3b  0, &pCur->skip);
147dd 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
147de 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
147df 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70  te3_free(pCur->p
147e0 4b 65 79 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e  Key);.    pCur->
147e1 70 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 61 73  pKey = 0;.    as
147e2 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
147e3 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
147e4 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65   || pCur->eState
147e5 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ==CURSOR_INVALID
147e6 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   );.  }.  return
147e7 20 72 63 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20   rc;.}..#define 
147e8 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  restoreCursorPos
147e9 69 74 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d  ition(p) \.  (p-
147ea 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f  >eState>=CURSOR_
147eb 52 45 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a  REQUIRESEEK ? \.
147ec 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
147ed 42 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73  BtreeRestoreCurs
147ee 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20 3a 20  orPosition(p) : 
147ef 5c 0a 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  \.         SQLIT
147f0 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44 65 74  E_OK)../*.** Det
147f1 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
147f2 72 20 6e 6f 74 20 61 20 63 75 72 73 6f 72 20 68  r not a cursor h
147f3 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68  as moved from th
147f4 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 0a 2a 2a  e position it.**
147f5 20 77 61 73 20 6c 61 73 74 20 70 6c 61 63 65 64   was last placed
147f6 20 61 74 2e 20 20 43 75 72 73 6f 72 73 20 63 61   at.  Cursors ca
147f7 6e 20 6d 6f 76 65 20 77 68 65 6e 20 74 68 65 20  n move when the 
147f8 72 6f 77 20 74 68 65 79 20 61 72 65 20 70 6f 69  row they are poi
147f9 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 69 73 20 64  nting.** at is d
147fa 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20  eleted out from 
147fb 75 6e 64 65 72 20 74 68 65 6d 2e 0a 2a 2a 0a 2a  under them..**.*
147fc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
147fd 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20  eturns an error 
147fe 63 6f 64 65 20 69 66 20 73 6f 6d 65 74 68 69 6e  code if somethin
147ff 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 54  g goes wrong.  T
14800 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 2a 70  he.** integer *p
14801 48 61 73 4d 6f 76 65 64 20 69 73 20 73 65 74 20  HasMoved is set 
14802 74 6f 20 6f 6e 65 20 69 66 20 74 68 65 20 63 75  to one if the cu
14803 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 61  rsor has moved a
14804 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a  nd 0 if not..*/.
14805 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
14806 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
14807 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74  ursorHasMoved(Bt
14808 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
14809 74 20 2a 70 48 61 73 4d 6f 76 65 64 29 7b 0a 20  t *pHasMoved){. 
1480a 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d   int rc;..  rc =
1480b 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
1480c 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20  sition(pCur);.  
1480d 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70  if( rc ){.    *p
1480e 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20  HasMoved = 1;.  
1480f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
14810 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
14811 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate!=CURSOR_VALI
14812 44 20 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 70 21  D || pCur->skip!
14813 3d 30 20 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d  =0 ){.    *pHasM
14814 6f 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  oved = 1;.  }els
14815 65 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65  e{.    *pHasMove
14816 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  d = 0;.  }.  ret
14817 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
14818 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
14819 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1481a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 70  ./*.** Given a p
1481b 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20  age number of a 
1481c 72 65 67 75 6c 61 72 20 64 61 74 61 62 61 73 65  regular database
1481d 20 70 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68   page, return th
1481e 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 65 72  e page.** number
1481f 20 66 6f 72 20 74 68 65 20 70 6f 69 6e 74 65 72   for the pointer
14820 2d 6d 61 70 20 70 61 67 65 20 74 68 61 74 20 63  -map page that c
14821 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72  ontains the entr
14822 79 20 66 6f 72 20 74 68 65 0a 2a 2a 20 69 6e 70  y for the.** inp
14823 75 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a  ut page number..
14824 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70  */.static Pgno p
14825 74 72 6d 61 70 50 61 67 65 6e 6f 28 42 74 53 68  trmapPageno(BtSh
14826 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20  ared *pBt, Pgno 
14827 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 6e 50 61  pgno){.  int nPa
14828 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20  gesPerMapPage;. 
14829 20 50 67 6e 6f 20 69 50 74 72 4d 61 70 2c 20 72   Pgno iPtrMap, r
1482a 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  et;.  assert( sq
1482b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1482c 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
1482d 20 20 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61    nPagesPerMapPa
1482e 67 65 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c  ge = (pBt->usabl
1482f 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20 20 69 50  eSize/5)+1;.  iP
14830 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f 2d 32 29  trMap = (pgno-2)
14831 2f 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67  /nPagesPerMapPag
14832 65 3b 0a 20 20 72 65 74 20 3d 20 28 69 50 74 72  e;.  ret = (iPtr
14833 4d 61 70 2a 6e 50 61 67 65 73 50 65 72 4d 61 70  Map*nPagesPerMap
14834 50 61 67 65 29 20 2b 20 32 3b 20 0a 20 20 69 66  Page) + 2; .  if
14835 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e 47 5f 42  ( ret==PENDING_B
14836 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
14837 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20 20 7d 0a  .    ret++;.  }.
14838 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a    return ret;.}.
14839 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20  ./*.** Write an 
1483a 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70  entry into the p
1483b 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a  ointer map..**.*
1483c 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75  * This routine u
1483d 70 64 61 74 65 73 20 74 68 65 20 70 6f 69 6e 74  pdates the point
1483e 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72  er map entry for
1483f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 6b 65   page number 'ke
14840 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74  y'.** so that it
14841 20 6d 61 70 73 20 74 6f 20 74 79 70 65 20 27 65   maps to type 'e
14842 54 79 70 65 27 20 61 6e 64 20 70 61 72 65 6e 74  Type' and parent
14843 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 70 67   page number 'pg
14844 6e 6f 27 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72  no'..** An error
14845 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
14846 64 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67  d if something g
14847 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72  oes wrong, other
14848 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  wise SQLITE_OK..
14849 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74  */.static int pt
1484a 72 6d 61 70 50 75 74 28 42 74 53 68 61 72 65 64  rmapPut(BtShared
1484b 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c   *pBt, Pgno key,
1484c 20 75 38 20 65 54 79 70 65 2c 20 50 67 6e 6f 20   u8 eType, Pgno 
1484d 70 61 72 65 6e 74 29 7b 0a 20 20 44 62 50 61 67  parent){.  DbPag
1484e 65 20 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20  e *pDbPage;  /* 
1484f 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
14850 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 50  page */.  u8 *pP
14851 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54  trmap;      /* T
14852 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64  he pointer map d
14853 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50  ata */.  Pgno iP
14854 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68  trmap;     /* Th
14855 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  e pointer map pa
14856 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69  ge number */.  i
14857 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20  nt offset;      
14858 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 70 6f   /* Offset in po
14859 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a  inter map page *
1485a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  /.  int rc;..  a
1485b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1485c 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
1485d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68  utex) );.  /* Th
1485e 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  e master-journal
1485f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6d 75 73   page number mus
14860 74 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20  t never be used 
14861 61 73 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70  as a pointer map
14862 20 70 61 67 65 20 2a 2f 0a 20 20 61 73 73 65 72   page */.  asser
14863 74 28 20 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50  t( 0==PTRMAP_ISP
14864 41 47 45 28 70 42 74 2c 20 50 45 4e 44 49 4e 47  AGE(pBt, PENDING
14865 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 29  _BYTE_PAGE(pBt))
14866 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
14867 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
14868 3b 0a 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29  ;.  if( key==0 )
14869 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1486a 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1486b 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d 61 70 20  ;.  }.  iPtrmap 
1486c 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  = PTRMAP_PAGENO(
1486d 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20  pBt, key);.  rc 
1486e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
1486f 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
14870 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65  Ptrmap, &pDbPage
14871 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
14872 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
14873 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 6f  turn rc;.  }.  o
14874 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50  ffset = PTRMAP_P
14875 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70  TROFFSET(iPtrmap
14876 2c 20 6b 65 79 29 3b 0a 20 20 70 50 74 72 6d 61  , key);.  pPtrma
14877 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65  p = (u8 *)sqlite
14878 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44  3PagerGetData(pD
14879 62 50 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65  bPage);..  if( e
1487a 54 79 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66  Type!=pPtrmap[of
1487b 66 73 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74  fset] || get4byt
1487c 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65  e(&pPtrmap[offse
1487d 74 2b 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b  t+1])!=parent ){
1487e 0a 20 20 20 20 54 52 41 43 45 28 28 22 50 54 52  .    TRACE(("PTR
1487f 4d 41 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e  MAP_UPDATE: %d->
14880 28 25 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c  (%d,%d)\n", key,
14881 20 65 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29   eType, parent))
14882 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
14883 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62  e3PagerWrite(pDb
14884 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
14885 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
14886 20 20 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66        pPtrmap[of
14887 66 73 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20  fset] = eType;. 
14888 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
14889 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d  Ptrmap[offset+1]
1488a 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d  , parent);.    }
1488b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 50  .  }..  sqlite3P
1488c 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67  agerUnref(pDbPag
1488d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
1488e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61  .}../*.** Read a
1488f 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  n entry from the
14890 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a   pointer map..**
14891 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14892 20 72 65 74 72 69 65 76 65 73 20 74 68 65 20 70   retrieves the p
14893 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79  ointer map entry
14894 20 66 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c   for page 'key',
14895 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20   writing.** the 
14896 74 79 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20  type and parent 
14897 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a  page number to *
14898 70 45 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e  pEType and *pPgn
14899 6f 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a  o respectively..
1489a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65  ** An error code
1489b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
1489c 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77  something goes w
1489d 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20  rong, otherwise 
1489e 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74  SQLITE_OK..*/.st
1489f 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47  atic int ptrmapG
148a0 65 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  et(BtShared *pBt
148a1 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a  , Pgno key, u8 *
148a2 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50  pEType, Pgno *pP
148a3 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a  gno){.  DbPage *
148a4 70 44 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68  pDbPage;   /* Th
148a5 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  e pointer map pa
148a6 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72  ge */.  int iPtr
148a7 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f  map;       /* Po
148a8 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 69  inter map page i
148a9 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50  ndex */.  u8 *pP
148aa 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20  trmap;       /* 
148ab 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  Pointer map page
148ac 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f   data */.  int o
148ad 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  ffset;        /*
148ae 20 4f 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79   Offset of entry
148af 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20   in pointer map 
148b0 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  */.  int rc;..  
148b1 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
148b2 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
148b3 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74  mutex) );..  iPt
148b4 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41  rmap = PTRMAP_PA
148b5 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a  GENO(pBt, key);.
148b6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
148b7 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67  gerGet(pBt->pPag
148b8 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44  er, iPtrmap, &pD
148b9 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  bPage);.  if( rc
148ba 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=0 ){.    retur
148bb 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72  n rc;.  }.  pPtr
148bc 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69  map = (u8 *)sqli
148bd 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
148be 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66  pDbPage);..  off
148bf 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52  set = PTRMAP_PTR
148c0 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20  OFFSET(iPtrmap, 
148c1 6b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  key);.  assert( 
148c2 70 45 54 79 70 65 21 3d 30 20 29 3b 0a 20 20 2a  pEType!=0 );.  *
148c3 70 45 54 79 70 65 20 3d 20 70 50 74 72 6d 61 70  pEType = pPtrmap
148c4 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69 66 28 20  [offset];.  if( 
148c5 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e 6f 20 3d  pPgno ) *pPgno =
148c6 20 67 65 74 34 62 79 74 65 28 26 70 50 74 72 6d   get4byte(&pPtrm
148c7 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 3b 0a 0a  ap[offset+1]);..
148c8 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
148c9 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ref(pDbPage);.  
148ca 69 66 28 20 2a 70 45 54 79 70 65 3c 31 20 7c 7c  if( *pEType<1 ||
148cb 20 2a 70 45 54 79 70 65 3e 35 20 29 20 72 65 74   *pEType>5 ) ret
148cc 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
148cd 50 54 5f 42 4b 50 54 3b 0a 20 20 72 65 74 75 72  PT_BKPT;.  retur
148ce 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
148cf 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69  #else /* if defi
148d0 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned SQLITE_OMIT_
148d1 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 20 20  AUTOVACUUM */.  
148d2 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75  #define ptrmapPu
148d3 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54  t(w,x,y,z) SQLIT
148d4 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70  E_OK.  #define p
148d5 74 72 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a  trmapGet(w,x,y,z
148d6 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64  ) SQLITE_OK.  #d
148d7 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 4f  efine ptrmapPutO
148d8 76 66 6c 28 79 2c 7a 29 20 53 51 4c 49 54 45 5f  vfl(y,z) SQLITE_
148d9 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  OK.#endif../*.**
148da 20 47 69 76 65 6e 20 61 20 62 74 72 65 65 20 70   Given a btree p
148db 61 67 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69  age and a cell i
148dc 6e 64 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68  ndex (0 means th
148dd 65 20 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a  e first cell on.
148de 2a 2a 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d  ** the page, 1 m
148df 65 61 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20  eans the second 
148e0 63 65 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72  cell, and so for
148e1 74 68 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69  th) return a poi
148e2 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63  nter.** to the c
148e3 65 6c 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a  ell content..**.
148e4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
148e5 77 6f 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70  works only for p
148e6 61 67 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74  ages that do not
148e7 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f   contain overflo
148e8 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66  w cells..*/.#def
148e9 69 6e 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49  ine findCell(P,I
148ea 29 20 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74  ) \.  ((P)->aDat
148eb 61 20 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61  a + ((P)->maskPa
148ec 67 65 20 26 20 67 65 74 32 62 79 74 65 28 26 28  ge & get2byte(&(
148ed 50 29 2d 3e 61 44 61 74 61 5b 28 50 29 2d 3e 63  P)->aData[(P)->c
148ee 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 28 49 29 5d  ellOffset+2*(I)]
148ef 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  )))../*.** This 
148f0 61 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 76  a more complex v
148f1 65 72 73 69 6f 6e 20 6f 66 20 66 69 6e 64 43 65  ersion of findCe
148f2 6c 6c 28 29 20 74 68 61 74 20 77 6f 72 6b 73 20  ll() that works 
148f3 66 6f 72 0a 2a 2a 20 70 61 67 65 73 20 74 68 61  for.** pages tha
148f4 74 20 64 6f 20 63 6f 6e 74 61 69 6e 20 6f 76 65  t do contain ove
148f5 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 20 20 53 65  rflow cells.  Se
148f6 65 20 69 6e 73 65 72 74 0a 2a 2f 0a 73 74 61 74  e insert.*/.stat
148f7 69 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66  ic u8 *findOverf
148f8 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20  lowCell(MemPage 
148f9 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c  *pPage, int iCel
148fa 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61  l){.  int i;.  a
148fb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
148fc 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
148fd 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
148fe 20 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e    for(i=pPage->n
148ff 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30  Overflow-1; i>=0
14900 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20  ; i--){.    int 
14901 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f  k;.    struct _O
14902 76 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a  vflCell *pOvfl;.
14903 20 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61      pOvfl = &pPa
14904 67 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20  ge->aOvfl[i];.  
14905 20 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78    k = pOvfl->idx
14906 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65  ;.    if( k<=iCe
14907 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
14908 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20  k==iCell ){.    
14909 20 20 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c      return pOvfl
1490a 2d 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d  ->pCell;.      }
1490b 0a 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a  .      iCell--;.
1490c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1490d 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67  rn findCell(pPag
1490e 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a  e, iCell);.}../*
1490f 0a 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c  .** Parse a cell
14910 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61   content block a
14911 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43  nd fill in the C
14912 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72  ellInfo structur
14913 65 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65  e.  There.** are
14914 20 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66   two versions of
14915 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20   this function. 
14916 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
14917 73 65 43 65 6c 6c 28 29 20 74 61 6b 65 73 20 61  seCell() takes a
14918 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 64 65 78 20   .** cell index 
14919 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
1491a 67 75 6d 65 6e 74 20 61 6e 64 20 73 71 6c 69 74  gument and sqlit
1491b 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
1491c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20  Ptr() .** takes 
1491d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1491e 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65 6c   body of the cel
1491f 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20  l as its second 
14920 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
14921 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c 65  Within this file
14922 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c 28  , the parseCell(
14923 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 63  ) macro can be c
14924 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f 66  alled instead of
14925 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65  .** sqlite3Btree
14926 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2e 20  ParseCellPtr(). 
14927 55 73 69 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 69  Using some compi
14928 6c 65 72 73 2c 20 74 68 69 73 20 77 69 6c 6c 20  lers, this will 
14929 62 65 20 66 61 73 74 65 72 2e 0a 2a 2f 0a 53 51  be faster..*/.SQ
1492a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1492b 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  d sqlite3BtreePa
1492c 72 73 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65  rseCellPtr(.  Me
1492d 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20  mPage *pPage,   
1492e 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f        /* Page co
1492f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c  ntaining the cel
14930 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c  l */.  u8 *pCell
14931 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
14932 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
14933 20 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20   cell text. */. 
14934 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f   CellInfo *pInfo
14935 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c           /* Fill
14936 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75   in this structu
14937 72 65 20 2a 2f 0a 29 7b 0a 20 20 75 31 36 20 6e  re */.){.  u16 n
14938 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
14939 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79 74     /* Number byt
1493a 65 73 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74 65  es in cell conte
1493b 6e 74 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75  nt header */.  u
1493c 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20  32 nPayload;    
1493d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1493e 20 6f 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c   of bytes of cel
1493f 6c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 20  l payload */..  
14940 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14941 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
14942 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
14943 0a 0a 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c  ..  pInfo->pCell
14944 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65   = pCell;.  asse
14945 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d  rt( pPage->leaf=
14946 3d 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61  =0 || pPage->lea
14947 66 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50  f==1 );.  n = pP
14948 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a  age->childPtrSiz
14949 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d  e;.  assert( n==
1494a 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20  4-4*pPage->leaf 
1494b 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  );.  if( pPage->
1494c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 69 66  intKey ){.    if
1494d 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61  ( pPage->hasData
1494e 20 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 67   ){.      n += g
1494f 65 74 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c  etVarint32(&pCel
14950 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b  l[n], nPayload);
14951 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
14952 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a    nPayload = 0;.
14953 20 20 20 20 7d 0a 20 20 20 20 6e 20 2b 3d 20 67      }.    n += g
14954 65 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b  etVarint(&pCell[
14955 6e 5d 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f  n], (u64*)&pInfo
14956 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 70 49 6e  ->nKey);.    pIn
14957 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 6e 50 61 79  fo->nData = nPay
14958 6c 6f 61 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  load;.  }else{. 
14959 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20     pInfo->nData 
1495a 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65  = 0;.    n += ge
1495b 74 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c  tVarint32(&pCell
1495c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a  [n], nPayload);.
1495d 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20      pInfo->nKey 
1495e 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 0a  = nPayload;.  }.
1495f 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61    pInfo->nPayloa
14960 64 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20  d = nPayload;.  
14961 70 49 6e 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d  pInfo->nHeader =
14962 20 6e 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79   n;.  if( likely
14963 28 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65  (nPayload<=pPage
14964 2d 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20  ->maxLocal) ){. 
14965 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68     /* This is th
14966 65 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20  e (easy) common 
14967 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65  case where the e
14968 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69  ntire payload fi
14969 74 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65  ts.    ** on the
1496a 20 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f   local page.  No
1496b 20 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71   overflow is req
1496c 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  uired..    */.  
1496d 20 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20    int nSize;    
1496e 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73        /* Total s
1496f 69 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74  ize of cell cont
14970 65 6e 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ent in bytes */.
14971 20 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79      nSize = nPay
14972 6c 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49  load + n;.    pI
14973 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75  nfo->nLocal = (u
14974 31 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20  16)nPayload;.   
14975 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f   pInfo->iOverflo
14976 77 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28  w = 0;.    if( (
14977 6e 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29  nSize & ~3)==0 )
14978 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20  {.      nSize = 
14979 34 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e  4;        /* Min
1497a 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69  imum cell size i
1497b 73 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  s 4 */.    }.   
1497c 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20   pInfo->nSize = 
1497d 28 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65  (u16)nSize;.  }e
1497e 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  lse{.    /* If t
1497f 68 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20  he payload will 
14980 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65  not fit complete
14981 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ly on the local 
14982 70 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20  page, we have.  
14983 20 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68    ** to decide h
14984 6f 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65  ow much to store
14985 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77   locally and how
14986 20 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f   much to spill o
14987 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66  nto.    ** overf
14988 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20  low pages.  The 
14989 73 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d  strategy is to m
1498a 69 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75  inimize the amou
1498b 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20  nt of unused.   
1498c 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65   ** space on ove
1498d 72 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c  rflow pages whil
1498e 65 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d  e keeping the am
1498f 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74  ount of local st
14990 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20  orage.    ** in 
14991 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c  between minLocal
14992 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20   and maxLocal.. 
14993 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72     **.    ** War
14994 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20  ning:  changing 
14995 74 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77  the way overflow
14996 20 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74   payload is dist
14997 72 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20  ributed in any. 
14998 20 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72     ** way will r
14999 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f  esult in an inco
1499a 6d 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f  mpatible file fo
1499b 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rmat..    */.   
1499c 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20   int minLocal;  
1499d 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e  /* Minimum amoun
1499e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c  t of payload hel
1499f 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20  d locally */.   
149a0 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20   int maxLocal;  
149a1 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e  /* Maximum amoun
149a2 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c  t of payload hel
149a3 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20  d locally */.   
149a4 20 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20   int surplus;   
149a5 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c  /* Overflow payl
149a6 6f 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  oad available fo
149a7 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20  r local storage 
149a8 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c  */..    minLocal
149a9 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63   = pPage->minLoc
149aa 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c  al;.    maxLocal
149ab 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63   = pPage->maxLoc
149ac 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20  al;.    surplus 
149ad 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50  = minLocal + (nP
149ae 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61  ayload - minLoca
149af 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  l)%(pPage->pBt->
149b0 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b  usableSize - 4);
149b1 0a 20 20 20 20 69 66 28 20 73 75 72 70 6c 75 73  .    if( surplus
149b2 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a   <= maxLocal ){.
149b3 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f        pInfo->nLo
149b4 63 61 6c 20 3d 20 28 75 31 36 29 73 75 72 70 6c  cal = (u16)surpl
149b5 75 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  us;.    }else{. 
149b6 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63       pInfo->nLoc
149b7 61 6c 20 3d 20 28 75 31 36 29 6d 69 6e 4c 6f 63  al = (u16)minLoc
149b8 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49  al;.    }.    pI
149b9 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d  nfo->iOverflow =
149ba 20 28 75 31 36 29 28 70 49 6e 66 6f 2d 3e 6e 4c   (u16)(pInfo->nL
149bb 6f 63 61 6c 20 2b 20 6e 29 3b 0a 20 20 20 20 70  ocal + n);.    p
149bc 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70 49  Info->nSize = pI
149bd 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 2b  nfo->iOverflow +
149be 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e   4;.  }.}.#defin
149bf 65 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67  e parseCell(pPag
149c0 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29  e, iCell, pInfo)
149c1 20 5c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65   \.  sqlite3Btre
149c2 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70  eParseCellPtr((p
149c3 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28  Page), findCell(
149c4 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29  (pPage), (iCell)
149c5 29 2c 20 28 70 49 6e 66 6f 29 29 0a 53 51 4c 49  ), (pInfo)).SQLI
149c6 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
149c7 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
149c8 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65  eCell(.  MemPage
149c9 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20   *pPage,        
149ca 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e   /* Page contain
149cb 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a  ing the cell */.
149cc 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20    int iCell,    
149cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
149ce 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69   cell index.  Fi
149cf 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f  rst cell is 0 */
149d0 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e  .  CellInfo *pIn
149d1 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  fo         /* Fi
149d2 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 63  ll in this struc
149d3 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72  ture */.){.  par
149d4 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  seCell(pPage, iC
149d5 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a  ell, pInfo);.}..
149d6 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
149d7 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
149d8 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20 43  f bytes that a C
149d9 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 65  ell needs in the
149da 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72   cell.** data ar
149db 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 2d  ea of the btree-
149dc 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 72  page.  The retur
149dd 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 65  n number include
149de 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61  s the cell.** da
149df 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 68  ta header and th
149e0 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c  e local payload,
149e1 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65   but not any ove
149e2 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a  rflow page or.**
149e3 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20   the space used 
149e4 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e  by the cell poin
149e5 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75  ter..*/.static u
149e6 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d  16 cellSizePtr(M
149e7 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75  emPage *pPage, u
149e8 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 20  8 *pCell){.  u8 
149e9 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c 5b  *pIter = &pCell[
149ea 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53  pPage->childPtrS
149eb 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 7a  ize];.  u32 nSiz
149ec 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  e;..#ifdef SQLIT
149ed 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65  E_DEBUG.  /* The
149ee 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
149ef 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
149f0 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62   should always b
149f1 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20 20  e the same as.  
149f2 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f  ** the (CellInfo
149f3 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66 6f  .nSize) value fo
149f4 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20 66  und by doing a f
149f5 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68 65  ull parse of the
149f6 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53  .  ** cell. If S
149f7 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64  QLITE_DEBUG is d
149f8 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72  efined, an asser
149f9 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74 6f  t() at the botto
149fa 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 66  m of.  ** this f
149fb 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65 73  unction verifies
149fc 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61 72   that this invar
149fd 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c  iant is not viol
149fe 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49  ated. */.  CellI
149ff 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20  nfo debuginfo;. 
14a00 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
14a01 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
14a02 20 70 43 65 6c 6c 2c 20 26 64 65 62 75 67 69 6e   pCell, &debugin
14a03 66 6f 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69  fo);.#endif..  i
14a04 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  f( pPage->intKey
14a05 20 29 7b 0a 20 20 20 20 75 38 20 2a 70 45 6e 64   ){.    u8 *pEnd
14a06 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d  ;.    if( pPage-
14a07 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20  >hasData ){.    
14a08 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61    pIter += getVa
14a09 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53  rint32(pIter, nS
14a0a 69 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ize);.    }else{
14a0b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 30  .      nSize = 0
14a0c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
14a0d 70 49 74 65 72 20 6e 6f 77 20 70 6f 69 6e 74 73  pIter now points
14a0e 20 61 74 20 74 68 65 20 36 34 2d 62 69 74 20 69   at the 64-bit i
14a0f 6e 74 65 67 65 72 20 6b 65 79 20 76 61 6c 75 65  nteger key value
14a10 2c 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e  , a variable len
14a11 67 74 68 20 0a 20 20 20 20 2a 2a 20 69 6e 74 65  gth .    ** inte
14a12 67 65 72 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ger. The followi
14a13 6e 67 20 62 6c 6f 63 6b 20 6d 6f 76 65 73 20 70  ng block moves p
14a14 49 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74  Iter to point at
14a15 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a   the first byte.
14a16 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20      ** past the 
14a17 65 6e 64 20 6f 66 20 74 68 65 20 6b 65 79 20 76  end of the key v
14a18 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 70 45 6e  alue. */.    pEn
14a19 64 20 3d 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20  d = &pIter[9];. 
14a1a 20 20 20 77 68 69 6c 65 28 20 28 2a 70 49 74 65     while( (*pIte
14a1b 72 2b 2b 29 26 30 78 38 30 20 26 26 20 70 49 74  r++)&0x80 && pIt
14a1c 65 72 3c 70 45 6e 64 20 29 3b 0a 20 20 7d 65 6c  er<pEnd );.  }el
14a1d 73 65 7b 0a 20 20 20 20 70 49 74 65 72 20 2b 3d  se{.    pIter +=
14a1e 20 67 65 74 56 61 72 69 6e 74 33 32 28 70 49 74   getVarint32(pIt
14a1f 65 72 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 7d 0a  er, nSize);.  }.
14a20 0a 20 20 69 66 28 20 6e 53 69 7a 65 3e 70 50 61  .  if( nSize>pPa
14a21 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a  ge->maxLocal ){.
14a22 20 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c      int minLocal
14a23 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63   = pPage->minLoc
14a24 61 6c 3b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20  al;.    nSize = 
14a25 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 53 69 7a  minLocal + (nSiz
14a26 65 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 20 25 20  e - minLocal) % 
14a27 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61  (pPage->pBt->usa
14a28 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20  bleSize - 4);.  
14a29 20 20 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 67    if( nSize>pPag
14a2a 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20  e->maxLocal ){. 
14a2b 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e       nSize = min
14a2c 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20  Local;.    }.   
14a2d 20 6e 53 69 7a 65 20 2b 3d 20 34 3b 0a 20 20 7d   nSize += 4;.  }
14a2e 0a 20 20 6e 53 69 7a 65 20 2b 3d 20 28 70 49 74  .  nSize += (pIt
14a2f 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20  er - pCell);..  
14a30 2f 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73  /* The minimum s
14a31 69 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20  ize of any cell 
14a32 69 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20  is 4 bytes. */. 
14a33 20 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a   if( nSize<4 ){.
14a34 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20      nSize = 4;. 
14a35 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53   }..  assert( nS
14a36 69 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e  ize==debuginfo.n
14a37 53 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e  Size );.  return
14a38 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 23   (u16)nSize;.}.#
14a39 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
14a3a 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a  atic u16 cellSiz
14a3b 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  e(MemPage *pPage
14a3c 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20  , int iCell){.  
14a3d 72 65 74 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50  return cellSizeP
14a3e 74 72 28 70 50 61 67 65 2c 20 66 69 6e 64 43 65  tr(pPage, findCe
14a3f 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29  ll(pPage, iCell)
14a40 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  );.}.#endif..#if
14a41 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14a42 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a  _AUTOVACUUM./*.*
14a43 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 70 43  * If the cell pC
14a44 65 6c 6c 2c 20 70 61 72 74 20 6f 66 20 70 61 67  ell, part of pag
14a45 65 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73  e pPage contains
14a46 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f   a pointer.** to
14a47 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
14a48 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74  e, insert an ent
14a49 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e  ry into the poin
14a4a 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74  ter-map.** for t
14a4b 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
14a4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
14a4d 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72  ptrmapPutOvflPtr
14a4e 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
14a4f 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 43   u8 *pCell){.  C
14a50 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20  ellInfo info;.  
14a51 61 73 73 65 72 74 28 20 70 43 65 6c 6c 21 3d 30  assert( pCell!=0
14a52 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   );.  sqlite3Btr
14a53 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
14a54 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e  Page, pCell, &in
14a55 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  fo);.  assert( (
14a56 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67  info.nData+(pPag
14a57 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f  e->intKey?0:info
14a58 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50  .nKey))==info.nP
14a59 61 79 6c 6f 61 64 20 29 3b 0a 20 20 69 66 28 20  ayload );.  if( 
14a5a 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61  (info.nData+(pPa
14a5b 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66  ge->intKey?0:inf
14a5c 6f 2e 6e 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c  o.nKey))>info.nL
14a5d 6f 63 61 6c 20 29 7b 0a 20 20 20 20 50 67 6e 6f  ocal ){.    Pgno
14a5e 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65   ovfl = get4byte
14a5f 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
14a60 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 72 65  erflow]);.    re
14a61 74 75 72 6e 20 70 74 72 6d 61 70 50 75 74 28 70  turn ptrmapPut(p
14a62 50 61 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c  Page->pBt, ovfl,
14a63 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
14a64 31 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b  1, pPage->pgno);
14a65 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
14a66 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a  LITE_OK;.}./*.**
14a67 20 49 66 20 74 68 65 20 63 65 6c 6c 20 77 69 74   If the cell wit
14a68 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20 6f 6e  h index iCell on
14a69 20 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74   page pPage cont
14a6a 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a  ains a pointer.*
14a6b 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77  * to an overflow
14a6c 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e   page, insert an
14a6d 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20   entry into the 
14a6e 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66  pointer-map.** f
14a6f 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  or the overflow 
14a70 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  page..*/.static 
14a71 69 6e 74 20 70 74 72 6d 61 70 50 75 74 4f 76 66  int ptrmapPutOvf
14a72 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  l(MemPage *pPage
14a73 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20  , int iCell){.  
14a74 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 61 73 73  u8 *pCell;.  ass
14a75 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14a76 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
14a77 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
14a78 70 43 65 6c 6c 20 3d 20 66 69 6e 64 4f 76 65 72  pCell = findOver
14a79 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20  flowCell(pPage, 
14a7a 69 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  iCell);.  return
14a7b 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74   ptrmapPutOvflPt
14a7c 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b  r(pPage, pCell);
14a7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
14a7e 2a 20 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65  * Defragment the
14a7f 20 70 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c   page given.  Al
14a80 6c 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65  l Cells are move
14a81 64 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20  d to the.** end 
14a82 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20  of the page and 
14a83 61 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 69  all free space i
14a84 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f  s collected into
14a85 20 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65   one.** big Free
14a86 42 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20  Blk that occurs 
14a87 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 68  in between the h
14a88 65 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a  eader and cell.*
14a89 2a 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20  * pointer array 
14a8a 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e  and the cell con
14a8b 74 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74  tent area..*/.st
14a8c 61 74 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d  atic int defragm
14a8d 65 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20  entPage(MemPage 
14a8e 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69  *pPage){.  int i
14a8f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
14a90 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
14a91 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70  unter */.  int p
14a92 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
14a93 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
14a94 20 6f 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20   of a i-th cell 
14a95 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20  */.  int addr;  
14a96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a97 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 66 69 72  /* Offset of fir
14a98 73 74 20 62 79 74 65 20 61 66 74 65 72 20 63 65  st byte after ce
14a99 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
14a9a 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20   */.  int hdr;  
14a9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a9c 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68   /* Offset to th
14a9d 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f  e page header */
14a9e 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20  .  int size;    
14a9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14aa0 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20   Size of a cell 
14aa1 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53  */.  int usableS
14aa2 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
14aa3 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61  /* Number of usa
14aa4 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70  ble bytes on a p
14aa5 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c  age */.  int cel
14aa6 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  lOffset;        
14aa7 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f      /* Offset to
14aa8 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65   the cell pointe
14aa9 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74  r array */.  int
14aaa 20 63 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20   cbrk;          
14aab 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
14aac 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f  t to the cell co
14aad 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20  ntent area */.  
14aae 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20  int nCell;      
14aaf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
14ab0 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e  mber of cells on
14ab1 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75   the page */.  u
14ab2 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61  nsigned char *da
14ab3 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ta;       /* The
14ab4 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20   page data */.  
14ab5 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74  unsigned char *t
14ab6 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65  emp;       /* Te
14ab7 6d 70 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c  mp area for cell
14ab8 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61   content */..  a
14ab9 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
14aba 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
14abb 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
14abc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
14abd 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61  e->pBt!=0 );.  a
14abe 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42  ssert( pPage->pB
14abf 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d  t->usableSize <=
14ac0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45   SQLITE_MAX_PAGE
14ac1 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72  _SIZE );.  asser
14ac2 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
14ac3 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  low==0 );.  asse
14ac4 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
14ac5 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
14ac6 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74  t->mutex) );.  t
14ac7 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67  emp = sqlite3Pag
14ac8 65 72 54 65 6d 70 53 70 61 63 65 28 70 50 61 67  erTempSpace(pPag
14ac9 65 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  e->pBt->pPager);
14aca 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  .  data = pPage-
14acb 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20  >aData;.  hdr = 
14acc 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
14acd 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d  ;.  cellOffset =
14ace 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73   pPage->cellOffs
14acf 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50  et;.  nCell = pP
14ad0 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73  age->nCell;.  as
14ad1 73 65 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74  sert( nCell==get
14ad2 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
14ad3 33 5d 29 20 29 3b 0a 20 20 75 73 61 62 6c 65 53  3]) );.  usableS
14ad4 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74  ize = pPage->pBt
14ad5 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  ->usableSize;.  
14ad6 63 62 72 6b 20 3d 20 67 65 74 32 62 79 74 65 28  cbrk = get2byte(
14ad7 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20  &data[hdr+5]);. 
14ad8 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b 63 62   memcpy(&temp[cb
14ad9 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d  rk], &data[cbrk]
14ada 2c 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 63  , usableSize - c
14adb 62 72 6b 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75  brk);.  cbrk = u
14adc 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 66 6f 72  sableSize;.  for
14add 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69  (i=0; i<nCell; i
14ade 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 64  ++){.    u8 *pAd
14adf 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69  dr;     /* The i
14ae0 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  -th cell pointer
14ae1 20 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d 20   */.    pAddr = 
14ae2 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74  &data[cellOffset
14ae3 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20   + i*2];.    pc 
14ae4 3d 20 67 65 74 32 62 79 74 65 28 70 41 64 64 72  = get2byte(pAddr
14ae5 29 3b 0a 20 20 20 20 69 66 28 20 70 63 3e 3d 75  );.    if( pc>=u
14ae6 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20  sableSize ){.   
14ae7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
14ae8 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
14ae9 20 20 20 7d 0a 20 20 20 20 73 69 7a 65 20 3d 20     }.    size = 
14aea 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67  cellSizePtr(pPag
14aeb 65 2c 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20  e, &temp[pc]);. 
14aec 20 20 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b     cbrk -= size;
14aed 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c 63 65  .    if( cbrk<ce
14aee 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c  llOffset+2*nCell
14aef 20 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62   || pc+size>usab
14af0 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  leSize ){.      
14af1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
14af2 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
14af3 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 62  }.    assert( cb
14af4 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53  rk+size<=usableS
14af5 69 7a 65 20 26 26 20 63 62 72 6b 3e 3d 30 20 29  ize && cbrk>=0 )
14af6 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61  ;.    memcpy(&da
14af7 74 61 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b  ta[cbrk], &temp[
14af8 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20  pc], size);.    
14af9 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c 20  put2byte(pAddr, 
14afa 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73  cbrk);.  }.  ass
14afb 65 72 74 28 20 63 62 72 6b 3e 3d 63 65 6c 6c 4f  ert( cbrk>=cellO
14afc 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 29 3b  ffset+2*nCell );
14afd 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
14afe 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b  a[hdr+5], cbrk);
14aff 0a 20 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d  .  data[hdr+1] =
14b00 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32   0;.  data[hdr+2
14b01 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64  ] = 0;.  data[hd
14b02 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 61 64 64 72  r+7] = 0;.  addr
14b03 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a   = cellOffset+2*
14b04 6e 43 65 6c 6c 3b 0a 20 20 6d 65 6d 73 65 74 28  nCell;.  memset(
14b05 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 30 2c 20  &data[addr], 0, 
14b06 63 62 72 6b 2d 61 64 64 72 29 3b 0a 20 20 61 73  cbrk-addr);.  as
14b07 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
14b08 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
14b09 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
14b0a 0a 20 20 69 66 28 20 63 62 72 6b 2d 61 64 64 72  .  if( cbrk-addr
14b0b 21 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29  !=pPage->nFree )
14b0c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
14b0d 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
14b0e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
14b0f 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
14b10 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74  ** Allocate nByt
14b11 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  e bytes of space
14b12 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
14b13 20 42 2d 54 72 65 65 20 70 61 67 65 20 70 61 73   B-Tree page pas
14b14 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69  sed.** as the fi
14b15 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 52 65  rst argument. Re
14b16 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69  turn the index i
14b17 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61  nto pPage->aData
14b18 5b 5d 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 69  [] of the .** fi
14b19 72 73 74 20 62 79 74 65 20 6f 66 20 61 6c 6c 6f  rst byte of allo
14b1a 63 61 74 65 64 20 73 70 61 63 65 2e 20 0a 2a 2a  cated space. .**
14b1b 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 67  .** The caller g
14b1c 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74  uarantees that t
14b1d 68 65 20 73 70 61 63 65 20 62 65 74 77 65 65 6e  he space between
14b1e 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
14b1f 63 65 6c 6c 2d 6f 66 66 73 65 74 20 0a 2a 2a 20  cell-offset .** 
14b20 61 72 72 61 79 20 61 6e 64 20 74 68 65 20 73 74  array and the st
14b21 61 72 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d  art of the cell-
14b22 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20  content area is 
14b23 61 74 20 6c 65 61 73 74 20 6e 42 79 74 65 20 62  at least nByte b
14b24 79 74 65 73 0a 2a 2a 20 69 6e 20 73 69 7a 65 2e  ytes.** in size.
14b25 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   So this routine
14b26 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e   can never fail.
14b27 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
14b28 61 72 65 20 61 6c 72 65 61 64 79 20 36 30 20 6f  are already 60 o
14b29 72 20 6d 6f 72 65 20 62 79 74 65 73 20 6f 66 20  r more bytes of 
14b2a 66 72 61 67 6d 65 6e 74 73 20 77 69 74 68 69 6e  fragments within
14b2b 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 74 68   the page,.** th
14b2c 65 20 70 61 67 65 20 69 73 20 64 65 66 72 61 67  e page is defrag
14b2d 6d 65 6e 74 65 64 20 62 65 66 6f 72 65 20 72 65  mented before re
14b2e 74 75 72 6e 69 6e 67 2e 20 49 66 20 74 68 69 73  turning. If this
14b2f 20 77 65 72 65 20 6e 6f 74 20 64 6f 6e 65 20 74   were not done t
14b30 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 63 68 61  here.** is a cha
14b31 6e 63 65 20 74 68 61 74 20 74 68 65 20 6e 75 6d  nce that the num
14b32 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65  ber of fragmente
14b33 64 20 62 79 74 65 73 20 63 6f 75 6c 64 20 65 76  d bytes could ev
14b34 65 6e 74 75 61 6c 6c 79 20 0a 2a 2a 20 6f 76 65  entually .** ove
14b35 72 66 6c 6f 77 20 74 68 65 20 73 69 6e 67 6c 65  rflow the single
14b36 2d 62 79 74 65 20 66 69 65 6c 64 20 6f 66 20 74  -byte field of t
14b37 68 65 20 70 61 67 65 2d 68 65 61 64 65 72 20 69  he page-header i
14b38 6e 20 77 68 69 63 68 20 74 68 69 73 20 76 61 6c  n which this val
14b39 75 65 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 2e  ue.** is stored.
14b3a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
14b3b 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 4d 65 6d  llocateSpace(Mem
14b3c 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74  Page *pPage, int
14b3d 20 6e 42 79 74 65 29 7b 0a 20 20 63 6f 6e 73 74   nByte){.  const
14b3e 20 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65   int hdr = pPage
14b3f 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20  ->hdrOffset;    
14b40 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f  /* Local cache o
14b41 66 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  f pPage->hdrOffs
14b42 65 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e  et */.  u8 * con
14b43 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  st data = pPage-
14b44 3e 61 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20  >aData;      /* 
14b45 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70  Local cache of p
14b46 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20  Page->aData */. 
14b47 20 69 6e 74 20 6e 46 72 61 67 3b 20 20 20 20 20   int nFrag;     
14b48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b49 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
14b4a 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79  of fragmented by
14b4b 74 65 73 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a  tes on pPage */.
14b4c 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 0a 20 20    int top;.  .  
14b4d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
14b4e 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
14b4f 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
14b50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
14b51 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73  ge->pBt );.  ass
14b52 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14b53 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
14b54 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
14b55 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30  assert( nByte>=0
14b56 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20   );  /* Minimum 
14b57 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a  cell size is 4 *
14b58 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  /.  assert( pPag
14b59 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20  e->nFree>=nByte 
14b5a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
14b5b 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  ge->nOverflow==0
14b5c 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74   );..  /* Assert
14b5d 20 74 68 61 74 20 74 68 65 20 73 70 61 63 65 20   that the space 
14b5e 62 65 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c  between the cell
14b5f 2d 6f 66 66 73 65 74 20 61 72 72 61 79 20 61 6e  -offset array an
14b60 64 20 74 68 65 20 0a 20 20 2a 2a 20 63 65 6c 6c  d the .  ** cell
14b61 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73  -content area is
14b62 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6e 42   greater than nB
14b63 79 74 65 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a  yte bytes..  */.
14b64 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 20    assert( nByte 
14b65 3c 3d 20 28 0a 20 20 20 20 20 20 67 65 74 32 62  <= (.      get2b
14b66 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
14b67 29 2d 28 68 64 72 2b 38 2b 28 70 50 61 67 65 2d  )-(hdr+8+(pPage-
14b68 3e 6c 65 61 66 3f 30 3a 34 29 2b 32 2a 67 65 74  >leaf?0:4)+2*get
14b69 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
14b6a 33 5d 29 29 0a 20 20 29 29 3b 0a 0a 20 20 70 50  3])).  ));..  pP
14b6b 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 75  age->nFree -= (u
14b6c 31 36 29 6e 42 79 74 65 3b 0a 20 20 6e 46 72 61  16)nByte;.  nFra
14b6d 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b  g = data[hdr+7];
14b6e 0a 20 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 30  .  if( nFrag>=60
14b6f 20 29 7b 0a 20 20 20 20 64 65 66 72 61 67 6d 65   ){.    defragme
14b70 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  ntPage(pPage);. 
14b71 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53   }else{.    /* S
14b72 65 61 72 63 68 20 74 68 65 20 66 72 65 65 6c 69  earch the freeli
14b73 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61  st looking for a
14b74 20 66 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65   free slot big e
14b75 6e 6f 75 67 68 20 74 6f 20 73 61 74 69 73 66 79  nough to satisfy
14b76 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 65 71   .    ** the req
14b77 75 65 73 74 2e 20 54 68 65 20 61 6c 6c 6f 63 61  uest. The alloca
14b78 74 69 6f 6e 20 69 73 20 6d 61 64 65 20 66 72 6f  tion is made fro
14b79 6d 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65  m the first free
14b7a 20 73 6c 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a   slot in .    **
14b7b 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 20 69   the list that i
14b7c 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74  s large enough t
14b7d 6f 20 61 63 63 6f 6d 61 64 61 74 65 20 69 74 2e  o accomadate it.
14b7e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
14b7f 70 63 2c 20 61 64 64 72 3b 0a 20 20 20 20 66 6f  pc, addr;.    fo
14b80 72 28 61 64 64 72 3d 68 64 72 2b 31 3b 20 28 70  r(addr=hdr+1; (p
14b81 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  c = get2byte(&da
14b82 74 61 5b 61 64 64 72 5d 29 29 3e 30 3b 20 61 64  ta[addr]))>0; ad
14b83 64 72 3d 70 63 29 7b 0a 20 20 20 20 20 20 69 6e  dr=pc){.      in
14b84 74 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74  t size = get2byt
14b85 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 20  e(&data[pc+2]); 
14b86 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 66      /* Size of f
14b87 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20  ree slot */.    
14b88 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74    if( size>=nByt
14b89 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  e ){.        int
14b8a 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74   x = size - nByt
14b8b 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78  e;.        if( x
14b8c 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <4 ){.          
14b8d 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 6c  /* Remove the sl
14b8e 6f 74 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65  ot from the free
14b8f 2d 6c 69 73 74 2e 20 55 70 64 61 74 65 20 74 68  -list. Update th
14b90 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20  e number of.    
14b91 20 20 20 20 20 20 2a 2a 20 66 72 61 67 6d 65 6e        ** fragmen
14b92 74 65 64 20 62 79 74 65 73 20 77 69 74 68 69 6e  ted bytes within
14b93 20 74 68 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20   the page. */.  
14b94 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
14b95 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 64 61 74  data[addr], &dat
14b96 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20  a[pc], 2);.     
14b97 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d       data[hdr+7]
14b98 20 3d 20 28 75 38 29 28 6e 46 72 61 67 20 2b 20   = (u8)(nFrag + 
14b99 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  x);.        }els
14b9a 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e{.          /* 
14b9b 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69 6e 73  The slot remains
14b9c 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   on the free-lis
14b9d 74 2e 20 52 65 64 75 63 65 20 69 74 73 20 73 69  t. Reduce its si
14b9e 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a 20 20  ze to account.  
14b9f 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74          ** for t
14ba0 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20  he portion used 
14ba1 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63  by the new alloc
14ba2 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20  ation. */.      
14ba3 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
14ba4 74 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20  ta[pc+2], x);.  
14ba5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
14ba6 72 65 74 75 72 6e 20 70 63 20 2b 20 78 3b 0a 20  return pc + x;. 
14ba7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
14ba8 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
14ba9 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20  memory from the 
14baa 67 61 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74  gap in between t
14bab 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
14bac 61 72 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74  array.  ** and t
14bad 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
14bae 61 72 65 61 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70  area..  */.  top
14baf 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
14bb0 61 5b 68 64 72 2b 35 5d 29 20 2d 20 6e 42 79 74  a[hdr+5]) - nByt
14bb1 65 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64  e;.  put2byte(&d
14bb2 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29  ata[hdr+5], top)
14bb3 3b 0a 20 20 72 65 74 75 72 6e 20 74 6f 70 3b 0a  ;.  return top;.
14bb4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
14bb5 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65  a section of the
14bb6 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f   pPage->aData to
14bb7 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a   the freelist..*
14bb8 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65  * The first byte
14bb9 20 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65   of the new free
14bba 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d   block is pPage-
14bbb 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a  >aDisk[start].**
14bbc 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66   and the size of
14bbd 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73   the block is "s
14bbe 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  ize" bytes..**.*
14bbf 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66  * Most of the ef
14bc0 66 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76  fort here is inv
14bc1 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69  olved in coalesi
14bc2 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66  ng adjacent.** f
14bc3 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20  ree blocks into 
14bc4 61 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65  a single big fre
14bc5 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74  e block..*/.stat
14bc6 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65  ic int freeSpace
14bc7 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
14bc8 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20   int start, int 
14bc9 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64  size){.  int add
14bca 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a  r, pbegin, hdr;.
14bcb 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
14bcc 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61  *data = pPage->a
14bcd 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28  Data;..  assert(
14bce 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29   pPage->pBt!=0 )
14bcf 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
14bd0 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
14bd1 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
14bd2 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
14bd3 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68   start>=pPage->h
14bd4 64 72 4f 66 66 73 65 74 2b 36 2b 28 70 50 61 67  drOffset+6+(pPag
14bd5 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 20 29 3b 0a  e->leaf?0:4) );.
14bd6 20 20 61 73 73 65 72 74 28 20 28 73 74 61 72 74    assert( (start
14bd7 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d   + size)<=pPage-
14bd8 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
14bd9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
14bda 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14bdb 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
14bdc 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
14bdd 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a   size>=0 );   /*
14bde 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69   Minimum cell si
14bdf 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64  ze is 4 */..#ifd
14be0 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45  ef SQLITE_SECURE
14be1 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65  _DELETE.  /* Ove
14be2 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 69  rwrite deleted i
14be3 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20  nformation with 
14be4 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53  zeros when the S
14be5 45 43 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20  ECURE_DELETE .  
14be6 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61  ** option is ena
14be7 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d  bled at compile-
14be8 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74  time */.  memset
14be9 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30  (&data[start], 0
14bea 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a  , size);.#endif.
14beb 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70  .  /* Add the sp
14bec 61 63 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  ace back into th
14bed 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  e linked list of
14bee 20 66 72 65 65 62 6c 6f 63 6b 73 20 2a 2f 0a 20   freeblocks */. 
14bef 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64   hdr = pPage->hd
14bf0 72 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20  rOffset;.  addr 
14bf1 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69  = hdr + 1;.  whi
14bf2 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65  le( (pbegin = ge
14bf3 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64  t2byte(&data[add
14bf4 72 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70 62  r]))<start && pb
14bf5 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73  egin>0 ){.    as
14bf6 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50  sert( pbegin<=pP
14bf7 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
14bf8 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 69 66  Size-4 );.    if
14bf9 28 20 70 62 65 67 69 6e 3c 3d 61 64 64 72 20 29  ( pbegin<=addr )
14bfa 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   {.      return 
14bfb 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
14bfc 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  KPT;.    }.    a
14bfd 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20  ddr = pbegin;.  
14bfe 7d 0a 20 20 69 66 20 28 20 70 62 65 67 69 6e 3e  }.  if ( pbegin>
14bff 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
14c00 6c 65 53 69 7a 65 2d 34 20 29 20 7b 0a 20 20 20  leSize-4 ) {.   
14c01 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
14c02 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
14c03 0a 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69  .  assert( pbegi
14c04 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 67 69 6e  n>addr || pbegin
14c05 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 62 79 74  ==0 );.  put2byt
14c06 65 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 73  e(&data[addr], s
14c07 74 61 72 74 29 3b 0a 20 20 70 75 74 32 62 79 74  tart);.  put2byt
14c08 65 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20  e(&data[start], 
14c09 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 74 32 62  pbegin);.  put2b
14c0a 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 2b  yte(&data[start+
14c0b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50 61  2], size);.  pPa
14c0c 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 28 75 31  ge->nFree += (u1
14c0d 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f  6)size;..  /* Co
14c0e 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20  alesce adjacent 
14c0f 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20  free blocks */. 
14c10 20 61 64 64 72 20 3d 20 70 50 61 67 65 2d 3e 68   addr = pPage->h
14c11 64 72 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20 20  drOffset + 1;.  
14c12 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d  while( (pbegin =
14c13 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
14c14 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20  addr]))>0 ){.   
14c15 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 69 7a   int pnext, psiz
14c16 65 2c 20 78 3b 0a 20 20 20 20 61 73 73 65 72 74  e, x;.    assert
14c17 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b  ( pbegin>addr );
14c18 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65  .    assert( pbe
14c19 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d  gin<=pPage->pBt-
14c1a 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b  >usableSize-4 );
14c1b 0a 20 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74  .    pnext = get
14c1c 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67  2byte(&data[pbeg
14c1d 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20  in]);.    psize 
14c1e 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
14c1f 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20  [pbegin+2]);.   
14c20 20 69 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73   if( pbegin + ps
14c21 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74  ize + 3 >= pnext
14c22 20 26 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20   && pnext>0 ){. 
14c23 20 20 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20       int frag = 
14c24 70 6e 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b  pnext - (pbegin+
14c25 70 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66  psize);.      if
14c26 28 20 28 66 72 61 67 3c 30 29 20 7c 7c 20 28 66  ( (frag<0) || (f
14c27 72 61 67 3e 28 69 6e 74 29 64 61 74 61 5b 70 50  rag>(int)data[pP
14c28 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37  age->hdrOffset+7
14c29 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ]) ){.        re
14c2a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
14c2b 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
14c2c 7d 0a 20 20 20 20 20 20 64 61 74 61 5b 70 50 61  }.      data[pPa
14c2d 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d  ge->hdrOffset+7]
14c2e 20 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20   -= (u8)frag;.  
14c2f 20 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65      x = get2byte
14c30 28 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a  (&data[pnext]);.
14c31 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26        put2byte(&
14c32 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29  data[pbegin], x)
14c33 3b 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78  ;.      x = pnex
14c34 74 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61  t + get2byte(&da
14c35 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70  ta[pnext+2]) - p
14c36 62 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74  begin;.      put
14c37 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67  2byte(&data[pbeg
14c38 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d  in+2], x);.    }
14c39 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72  else{.      addr
14c3a 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d   = pbegin;.    }
14c3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
14c3c 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  e cell content a
14c3d 72 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20  rea begins with 
14c3e 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d  a freeblock, rem
14c3f 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28  ove it. */.  if(
14c40 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61   data[hdr+1]==da
14c41 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74  ta[hdr+5] && dat
14c42 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68  a[hdr+2]==data[h
14c43 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74  dr+6] ){.    int
14c44 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e   top;.    pbegin
14c45 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
14c46 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d  a[hdr+1]);.    m
14c47 65 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b  emcpy(&data[hdr+
14c48 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e  1], &data[pbegin
14c49 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d  ], 2);.    top =
14c4a 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
14c4b 68 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79  hdr+5]) + get2by
14c4c 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b  te(&data[pbegin+
14c4d 32 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74  2]);.    put2byt
14c4e 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20  e(&data[hdr+5], 
14c4f 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  top);.  }.  asse
14c50 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
14c51 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
14c52 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
14c53 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
14c54 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f  K;.}../*.** Deco
14c55 64 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74  de the flags byt
14c56 65 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74  e (the first byt
14c57 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29  e of the header)
14c58 20 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61   for a page.** a
14c59 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69  nd initialize fi
14c5a 65 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50  elds of the MemP
14c5b 61 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63  age structure ac
14c5c 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a  cordingly..**.**
14c5d 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77   Only the follow
14c5e 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  ing combinations
14c5f 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20   are supported. 
14c60 20 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72   Anything differ
14c61 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73  ent.** indicates
14c62 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62   a corrupt datab
14c63 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a  ase files:.**.**
14c64 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52           PTF_ZER
14c65 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20  ODATA.**        
14c66 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20   PTF_ZERODATA | 
14c67 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20  PTF_LEAF.**     
14c68 20 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41      PTF_LEAFDATA
14c69 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a   | PTF_INTKEY.**
14c6a 20 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41           PTF_LEA
14c6b 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b  FDATA | PTF_INTK
14c6c 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f  EY | PTF_LEAF.*/
14c6d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f  .static int deco
14c6e 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20  deFlags(MemPage 
14c6f 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67  *pPage, int flag
14c70 42 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65  Byte){.  BtShare
14c71 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41  d *pBt;     /* A
14c72 20 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e   copy of pPage->
14c73 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  pBt */..  assert
14c74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  ( pPage->hdrOffs
14c75 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f  et==(pPage->pgno
14c76 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29  ==1 ? 100 : 0) )
14c77 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
14c78 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14c79 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
14c7a 29 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65  ) );.  pPage->le
14c7b 61 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79  af = (u8)(flagBy
14c7c 74 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28  te>>3);  assert(
14c7d 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c   PTF_LEAF == 1<<
14c7e 33 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20  3 );.  flagByte 
14c7f 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20  &= ~PTF_LEAF;.  
14c80 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53  pPage->childPtrS
14c81 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d  ize = 4-4*pPage-
14c82 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70  >leaf;.  pBt = p
14c83 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28  Page->pBt;.  if(
14c84 20 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f   flagByte==(PTF_
14c85 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49  LEAFDATA | PTF_I
14c86 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50  NTKEY) ){.    pP
14c87 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b  age->intKey = 1;
14c88 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44  .    pPage->hasD
14c89 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61  ata = pPage->lea
14c8a 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61  f;.    pPage->ma
14c8b 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61  xLocal = pBt->ma
14c8c 78 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65  xLeaf;.    pPage
14c8d 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74  ->minLocal = pBt
14c8e 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c  ->minLeaf;.  }el
14c8f 73 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d  se if( flagByte=
14c90 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b  =PTF_ZERODATA ){
14c91 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b  .    pPage->intK
14c92 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  ey = 0;.    pPag
14c93 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a  e->hasData = 0;.
14c94 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f      pPage->maxLo
14c95 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f  cal = pBt->maxLo
14c96 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  cal;.    pPage->
14c97 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e  minLocal = pBt->
14c98 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73  minLocal;.  }els
14c99 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e{.    return SQ
14c9a 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
14c9b 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  T;.  }.  return 
14c9c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
14c9d 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  .** Initialize t
14c9e 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66  he auxiliary inf
14c9f 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64  ormation for a d
14ca0 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a  isk block..**.**
14ca1 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
14ca2 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49  K on success.  I
14ca3 66 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68  f we see that th
14ca4 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e  e page does.** n
14ca5 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c  ot contain a wel
14ca6 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73  l-formed databas
14ca7 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74  e page, then ret
14ca8 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43  urn .** SQLITE_C
14ca9 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68  ORRUPT.  Note th
14caa 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53  at a return of S
14cab 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f  QLITE_OK does no
14cac 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74  t.** guarantee t
14cad 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20  hat the page is 
14cae 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74  well-formed.  It
14caf 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74   only shows that
14cb0 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f  .** we failed to
14cb1 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72   detect any corr
14cb2 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  uption..*/.SQLIT
14cb3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14cb4 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
14cb5 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ge(MemPage *pPag
14cb6 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  e){..  assert( p
14cb7 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a  Page->pBt!=0 );.
14cb8 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14cb9 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
14cba 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
14cbb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
14cbc 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65  ge->pgno==sqlite
14cbd 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72  3PagerPagenumber
14cbe 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
14cbf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
14cc0 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61  age == sqlite3Pa
14cc1 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67  gerGetExtra(pPag
14cc2 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
14cc3 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
14cc4 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33  aData == sqlite3
14cc5 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61  PagerGetData(pPa
14cc6 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
14cc7 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69  .  if( !pPage->i
14cc8 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36  sInit ){.    u16
14cc9 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   pc;            
14cca 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20  /* Address of a 
14ccb 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e  freeblock within
14ccc 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20   pPage->aData[] 
14ccd 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20  */.    u8 hdr;  
14cce 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
14ccf 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67  set to beginning
14cd0 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20   of page header 
14cd1 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b  */.    u8 *data;
14cd2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75            /* Equ
14cd3 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61  al to pPage->aDa
14cd4 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72  ta */.    BtShar
14cd5 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20  ed *pBt;        
14cd6 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65  /* The main btre
14cd7 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
14cd8 20 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a     u16 usableSiz
14cd9 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20  e;    /* Amount 
14cda 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20  of usable space 
14cdb 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  on each page */.
14cdc 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73      u16 cellOffs
14cdd 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74  et;    /* Offset
14cde 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70   from start of p
14cdf 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c  age to first cel
14ce0 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20  l pointer */.   
14ce1 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20   u16 nFree;     
14ce2 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14ce3 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e   unused bytes on
14ce4 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20   the page */.   
14ce5 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20   u16 top;       
14ce6 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
14ce7 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  e of the cell co
14ce8 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 0a 20  ntent area */.. 
14ce9 20 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e     pBt = pPage->
14cea 70 42 74 3b 0a 0a 20 20 20 20 68 64 72 20 3d 20  pBt;..    hdr = 
14ceb 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
14cec 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61  ;.    data = pPa
14ced 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69  ge->aData;.    i
14cee 66 28 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70  f( decodeFlags(p
14cef 50 61 67 65 2c 20 64 61 74 61 5b 68 64 72 5d 29  Page, data[hdr])
14cf0 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
14cf1 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
14cf2 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
14cf3 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26  pageSize>=512 &&
14cf4 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d   pBt->pageSize<=
14cf5 33 32 37 36 38 20 29 3b 0a 20 20 20 20 70 50 61  32768 );.    pPa
14cf6 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70  ge->maskPage = p
14cf7 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31  Bt->pageSize - 1
14cf8 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76  ;.    pPage->nOv
14cf9 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20  erflow = 0;.    
14cfa 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74  usableSize = pBt
14cfb 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  ->usableSize;.  
14cfc 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66    pPage->cellOff
14cfd 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74  set = cellOffset
14cfe 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a   = hdr + 12 - 4*
14cff 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20  pPage->leaf;.   
14d00 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28   top = get2byte(
14d01 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20  &data[hdr+5]);. 
14d02 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20     pPage->nCell 
14d03 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
14d04 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 69 66  [hdr+3]);.    if
14d05 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d  ( pPage->nCell>M
14d06 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20  X_CELL(pBt) ){. 
14d07 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20       /* To many 
14d08 63 65 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67  cells for a sing
14d09 6c 65 20 70 61 67 65 2e 20 20 54 68 65 20 70 61  le page.  The pa
14d0a 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75  ge must be corru
14d0b 70 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  pt */.      retu
14d0c 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
14d0d 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20  T_BKPT;.    }.  
14d0e 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  .    /* Compute 
14d0f 74 68 65 20 74 6f 74 61 6c 20 66 72 65 65 20 73  the total free s
14d10 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65  pace on the page
14d11 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 67 65 74   */.    pc = get
14d12 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
14d13 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65 65 20 3d  1]);.    nFree =
14d14 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 2b 20 74   data[hdr+7] + t
14d15 6f 70 20 2d 20 28 63 65 6c 6c 4f 66 66 73 65 74  op - (cellOffset
14d16 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c   + 2*pPage->nCel
14d17 6c 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70  l);.    while( p
14d18 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 31 36  c>0 ){.      u16
14d19 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20   next, size;.   
14d1a 20 20 20 69 66 28 20 70 63 3e 75 73 61 62 6c 65     if( pc>usable
14d1b 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20  Size-4 ){.      
14d1c 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 20    /* Free block 
14d1d 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20  is off the page 
14d1e 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
14d1f 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
14d20 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a  _BKPT; .      }.
14d21 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74        next = get
14d22 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29  2byte(&data[pc])
14d23 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67  ;.      size = g
14d24 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63  et2byte(&data[pc
14d25 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +2]);.      if( 
14d26 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c 3d  next>0 && next<=
14d27 70 63 2b 73 69 7a 65 2b 33 20 29 7b 0a 20 20 20  pc+size+3 ){.   
14d28 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f       /* Free blo
14d29 63 6b 73 20 6d 75 73 74 20 62 65 20 69 6e 20 61  cks must be in a
14d2a 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a  ccending order *
14d2b 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
14d2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
14d2d 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20  BKPT; .      }. 
14d2e 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 69       nFree += si
14d2f 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 3d 20 6e  ze;.      pc = n
14d30 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ext;.    }.    p
14d31 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75  Page->nFree = (u
14d32 31 36 29 6e 46 72 65 65 3b 0a 20 20 20 20 69 66  16)nFree;.    if
14d33 28 20 6e 46 72 65 65 3e 3d 75 73 61 62 6c 65 53  ( nFree>=usableS
14d34 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ize ){.      /* 
14d35 46 72 65 65 20 73 70 61 63 65 20 63 61 6e 6e 6f  Free space canno
14d36 74 20 65 78 63 65 65 64 20 74 6f 74 61 6c 20 70  t exceed total p
14d37 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20  age size */.    
14d38 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
14d39 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20  CORRUPT_BKPT; . 
14d3a 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 2f 2a     }..#if 0.  /*
14d3b 20 43 68 65 63 6b 20 74 68 61 74 20 61 6c 6c 20   Check that all 
14d3c 74 68 65 20 6f 66 66 73 65 74 73 20 69 6e 20 74  the offsets in t
14d3d 68 65 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61  he cell offset a
14d3e 72 72 61 79 20 61 72 65 20 77 69 74 68 69 6e 20  rray are within 
14d3f 72 61 6e 67 65 2e 20 0a 20 20 2a 2a 20 0a 20 20  range. .  ** .  
14d40 2a 2a 20 4f 6d 69 74 74 69 6e 67 20 74 68 69 73  ** Omitting this
14d41 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65   consistency che
14d42 63 6b 20 61 6e 64 20 75 73 69 6e 67 20 74 68 65  ck and using the
14d43 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65   pPage->maskPage
14d44 20 6d 61 73 6b 0a 20 20 2a 2a 20 74 6f 20 70 72   mask.  ** to pr
14d45 65 76 65 6e 74 20 6f 76 65 72 72 75 6e 6e 69 6e  event overrunnin
14d46 67 20 74 68 65 20 70 61 67 65 20 62 75 66 66 65  g the page buffe
14d47 72 20 69 6e 20 66 69 6e 64 43 65 6c 6c 28 29 20  r in findCell() 
14d48 72 65 73 75 6c 74 73 20 69 6e 20 61 0a 20 20 2a  results in a.  *
14d49 2a 20 32 2e 35 25 20 70 65 72 66 6f 72 6d 61 6e  * 2.5% performan
14d4a 63 65 20 67 61 69 6e 2e 0a 20 20 2a 2f 0a 20 20  ce gain..  */.  
14d4b 7b 0a 20 20 20 20 75 38 20 2a 70 4f 66 66 3b 20  {.    u8 *pOff; 
14d4c 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
14d4d 6f 72 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b  or used to check
14d4e 20 61 6c 6c 20 63 65 6c 6c 20 6f 66 66 73 65 74   all cell offset
14d4f 73 20 61 72 65 20 69 6e 20 72 61 6e 67 65 20 2a  s are in range *
14d50 2f 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 20  /.    u8 *pEnd; 
14d51 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
14d52 72 20 74 6f 20 65 6e 64 20 6f 66 20 63 65 6c 6c  r to end of cell
14d53 20 6f 66 66 73 65 74 20 61 72 72 61 79 20 2a 2f   offset array */
14d54 0a 20 20 20 20 75 38 20 6d 61 73 6b 3b 20 20 20  .    u8 mask;   
14d55 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
14d56 20 62 69 74 73 20 74 68 61 74 20 6d 75 73 74 20   bits that must 
14d57 62 65 20 7a 65 72 6f 20 69 6e 20 4d 53 42 20 6f  be zero in MSB o
14d58 66 20 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 2a  f cell offsets *
14d59 2f 0a 20 20 20 20 6d 61 73 6b 20 3d 20 7e 28 28  /.    mask = ~((
14d5a 28 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69  (u8)(pBt->pageSi
14d5b 7a 65 3e 3e 38 29 29 2d 31 29 3b 0a 20 20 20 20  ze>>8))-1);.    
14d5c 70 45 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 6c  pEnd = &data[cel
14d5d 6c 4f 66 66 73 65 74 20 2b 20 70 50 61 67 65 2d  lOffset + pPage-
14d5e 3e 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 20 20 66  >nCell*2];.    f
14d5f 6f 72 28 70 4f 66 66 3d 26 64 61 74 61 5b 63 65  or(pOff=&data[ce
14d60 6c 6c 4f 66 66 73 65 74 5d 3b 20 70 4f 66 66 21  llOffset]; pOff!
14d61 3d 70 45 6e 64 20 26 26 20 21 28 28 2a 70 4f 66  =pEnd && !((*pOf
14d62 66 29 26 6d 61 73 6b 29 3b 20 70 4f 66 66 2b 3d  f)&mask); pOff+=
14d63 32 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 66 66  2);.    if( pOff
14d64 21 3d 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20  !=pEnd ){.      
14d65 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
14d66 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
14d67 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
14d68 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
14d69 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
14d6a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
14d6b 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72  /*.** Set up a r
14d6c 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 74 20  aw page so that 
14d6d 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20  it looks like a 
14d6e 64 61 74 61 62 61 73 65 20 70 61 67 65 20 68 6f  database page ho
14d6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72  lding.** no entr
14d70 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ies..*/.static v
14d71 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d  oid zeroPage(Mem
14d72 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74  Page *pPage, int
14d73 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67   flags){.  unsig
14d74 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d  ned char *data =
14d75 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20   pPage->aData;. 
14d76 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
14d77 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75   pPage->pBt;.  u
14d78 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68  8 hdr = pPage->h
14d79 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31 36 20  drOffset;.  u16 
14d7a 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74  first;..  assert
14d7b 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  ( sqlite3PagerPa
14d7c 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e  genumber(pPage->
14d7d 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d  pDbPage)==pPage-
14d7e 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72  >pgno );.  asser
14d7f 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  t( sqlite3PagerG
14d80 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70  etExtra(pPage->p
14d81 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64  DbPage) == (void
14d82 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73  *)pPage );.  ass
14d83 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
14d84 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e  rGetData(pPage->
14d85 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61  pDbPage) == data
14d86 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
14d87 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
14d88 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
14d89 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72  Page) );.  asser
14d8a 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14d8b 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
14d8c 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28  ) );.  /*memset(
14d8d 26 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70  &data[hdr], 0, p
14d8e 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
14d8f 20 68 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b   hdr);*/.  data[
14d90 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c 61  hdr] = (char)fla
14d91 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 64  gs;.  first = hd
14d92 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 67  r + 8 + 4*((flag
14d93 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f  s&PTF_LEAF)==0 ?
14d94 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26  1:0);.  memset(&
14d95 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20  data[hdr+1], 0, 
14d96 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37  4);.  data[hdr+7
14d97 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 74  ] = 0;.  put2byt
14d98 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20  e(&data[hdr+5], 
14d99 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29  pBt->usableSize)
14d9a 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  ;.  pPage->nFree
14d9b 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
14d9c 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64 65  ze - first;.  de
14d9d 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c  codeFlags(pPage,
14d9e 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65   flags);.  pPage
14d9f 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68 64  ->hdrOffset = hd
14da0 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c  r;.  pPage->cell
14da1 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0a  Offset = first;.
14da2 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c    pPage->nOverfl
14da3 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ow = 0;.  assert
14da4 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e  ( pBt->pageSize>
14da5 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67  =512 && pBt->pag
14da6 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a  eSize<=32768 );.
14da7 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67    pPage->maskPag
14da8 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  e = pBt->pageSiz
14da9 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e  e - 1;.  pPage->
14daa 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61  nCell = 0;.  pPa
14dab 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a  ge->isInit = 1;.
14dac 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  }.../*.** Conver
14dad 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 69  t a DbPage obtai
14dae 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67  ned from the pag
14daf 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67  er into a MemPag
14db0 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 65  e used by.** the
14db1 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f   btree layer..*/
14db2 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65 20  .static MemPage 
14db3 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62  *btreePageFromDb
14db4 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44 62  Page(DbPage *pDb
14db5 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c  Page, Pgno pgno,
14db6 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b   BtShared *pBt){
14db7 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
14db8 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 71  e = (MemPage*)sq
14db9 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
14dba 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70  ra(pDbPage);.  p
14dbb 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71  Page->aData = sq
14dbc 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
14dbd 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50  a(pDbPage);.  pP
14dbe 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 70  age->pDbPage = p
14dbf 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65 2d  DbPage;.  pPage-
14dc0 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 50  >pBt = pBt;.  pP
14dc1 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f  age->pgno = pgno
14dc2 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  ;.  pPage->hdrOf
14dc3 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 67  fset = pPage->pg
14dc4 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b  no==1 ? 100 : 0;
14dc5 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 3b  .  return pPage;
14dc6 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61   .}../*.** Get a
14dc7 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70   page from the p
14dc8 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a  ager.  Initializ
14dc9 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42  e the MemPage.pB
14dca 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65  t and.** MemPage
14dcb 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20  .aData elements 
14dcc 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a  if needed..**.**
14dcd 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e   If the noConten
14dce 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 69  t flag is set, i
14dcf 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20  t means that we 
14dd0 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75  do not care abou
14dd1 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  t.** the content
14dd2 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 20   of the page at 
14dd3 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 64  this time.  So d
14dd4 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 20  o not go to the 
14dd5 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68  disk.** to fetch
14dd6 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a   the content.  J
14dd7 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 20  ust fill in the 
14dd8 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72  content with zer
14dd9 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49  os for now..** I
14dda 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 20  f in the future 
14ddb 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50  we call sqlite3P
14ddc 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74  agerWrite() on t
14ddd 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a 2a  his page, that.*
14dde 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 20  * means we have 
14ddf 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 6f  started to be co
14de0 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 6f  ncerned about co
14de1 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 69  ntent and the di
14de2 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c  sk.** read shoul
14de3 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 20  d occur at that 
14de4 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  point..*/.SQLITE
14de5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
14de6 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
14de7 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  (.  BtShared *pB
14de8 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  t,       /* The 
14de9 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  btree */.  Pgno 
14dea 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  pgno,           
14deb 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65  /* Number of the
14dec 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a   page to fetch *
14ded 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70  /.  MemPage **pp
14dee 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75  Page,    /* Retu
14def 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e 20 74  rn the page in t
14df0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  his parameter */
14df1 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74  .  int noContent
14df2 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
14df3 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74  t load page cont
14df4 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  ent if true */.)
14df5 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62  {.  int rc;.  Db
14df6 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a  Page *pDbPage;..
14df7 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14df8 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
14df9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63  ->mutex) );.  rc
14dfa 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 41   = sqlite3PagerA
14dfb 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67  cquire(pBt->pPag
14dfc 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67  er, pgno, (DbPag
14dfd 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f  e**)&pDbPage, no
14dfe 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20  Content);.  if( 
14dff 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
14e00 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65    *ppPage = btre
14e01 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28  ePageFromDbPage(
14e02 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70  pDbPage, pgno, p
14e03 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  Bt);.  return SQ
14e04 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
14e05 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 61 67  * Retrieve a pag
14e06 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72  e from the pager
14e07 20 63 61 63 68 65 2e 20 49 66 20 74 68 65 20 72   cache. If the r
14e08 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73  equested page is
14e09 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20   not.** already 
14e0a 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63  in the pager cac
14e0b 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  he return NULL. 
14e0c 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d  Initialize the M
14e0d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a  emPage.pBt and.*
14e0e 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20  * MemPage.aData 
14e0f 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64  elements if need
14e10 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65  ed..*/.static Me
14e11 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 67 65  mPage *btreePage
14e12 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20  Lookup(BtShared 
14e13 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  *pBt, Pgno pgno)
14e14 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50  {.  DbPage *pDbP
14e15 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73  age;.  assert( s
14e16 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14e17 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
14e18 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c  .  pDbPage = sql
14e19 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
14e1a 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e  pBt->pPager, pgn
14e1b 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67  o);.  if( pDbPag
14e1c 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
14e1d 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50  btreePageFromDbP
14e1e 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e  age(pDbPage, pgn
14e1f 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72  o, pBt);.  }.  r
14e20 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
14e21 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a  * Return the siz
14e22 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
14e23 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e  e file in pages.
14e24 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 79   If there is any
14e25 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f   kind of.** erro
14e26 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69  r, return ((unsi
14e27 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f  gned int)-1)..*/
14e28 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67  .static Pgno pag
14e29 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68  erPagecount(BtSh
14e2a 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e  ared *pBt){.  in
14e2b 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20  t nPage = -1;.  
14e2c 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
14e2d 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b  ( pBt->pPage1 );
14e2e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
14e2f 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
14e30 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67  t->pPager, &nPag
14e31 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  e);.  assert( rc
14e32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e  ==SQLITE_OK || n
14e33 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65  Page==-1 );.  re
14e34 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65  turn (Pgno)nPage
14e35 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61  ;.}../*.** Get a
14e36 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70   page from the p
14e37 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c  ager and initial
14e38 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f  ize it.  This ro
14e39 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6a 75 73 74  utine.** is just
14e3a 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77   a convenience w
14e3b 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65  rapper around se
14e3c 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 0a  parate calls to.
14e3d 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  ** sqlite3BtreeG
14e3e 65 74 50 61 67 65 28 29 20 61 6e 64 20 73 71 6c  etPage() and sql
14e3f 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
14e40 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  e()..*/.static i
14e41 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67  nt getAndInitPag
14e42 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
14e43 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Bt,          /* 
14e44 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
14e45 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
14e46 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
14e47 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
14e48 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65  e to get */.  Me
14e49 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20  mPage **ppPage  
14e4a 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
14e4b 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72  page pointer her
14e4c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
14e4d 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
14e4e 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ge;..  assert( s
14e4f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14e50 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
14e51 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29  .  if( pgno==0 )
14e52 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
14e53 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
14e54 3b 20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20  ; .  }..  /* It 
14e55 69 73 20 6f 66 74 65 6e 20 74 68 65 20 63 61 73  is often the cas
14e56 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
14e57 77 65 20 77 61 6e 74 20 69 73 20 61 6c 72 65 61  we want is alrea
14e58 64 79 20 69 6e 20 63 61 63 68 65 2e 0a 20 20 2a  dy in cache..  *
14e59 2a 20 49 66 20 73 6f 2c 20 67 65 74 20 69 74 20  * If so, get it 
14e5a 64 69 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20  directly.  This 
14e5b 73 61 76 65 73 20 75 73 20 66 72 6f 6d 20 68 61  saves us from ha
14e5c 76 69 6e 67 20 74 6f 20 63 61 6c 6c 0a 20 20 2a  ving to call.  *
14e5d 2a 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  * pagerPagecount
14e5e 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  () to make sure 
14e5f 70 67 6e 6f 20 69 73 20 77 69 74 68 69 6e 20 6c  pgno is within l
14e60 69 6d 69 74 73 2c 20 77 68 69 63 68 20 72 65 73  imits, which res
14e61 75 6c 74 73 0a 20 20 2a 2a 20 69 6e 20 61 20 6d  ults.  ** in a m
14e62 65 61 73 75 72 65 61 62 6c 65 20 70 65 72 66 6f  easureable perfo
14e63 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65  rmance improveme
14e64 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 2a 70 70 50  nts..  */.  *ppP
14e65 61 67 65 20 3d 20 70 50 61 67 65 20 3d 20 62 74  age = pPage = bt
14e66 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42  reePageLookup(pB
14e67 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20  t, pgno);.  if( 
14e68 70 50 61 67 65 20 29 7b 0a 20 20 20 20 2f 2a 20  pPage ){.    /* 
14e69 50 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20  Page is already 
14e6a 69 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 20 20  in cache */.    
14e6b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
14e6c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
14e6d 50 61 67 65 20 6e 6f 74 20 69 6e 20 63 61 63 68  Page not in cach
14e6e 65 2e 20 20 41 63 71 75 69 72 65 20 69 74 2e 20  e.  Acquire it. 
14e6f 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e  */.    if( pgno>
14e70 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
14e71 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  Bt) ){.      ret
14e72 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
14e73 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a  PT_BKPT; .    }.
14e74 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
14e75 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
14e76 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20  , pgno, ppPage, 
14e77 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  0);.    if( rc )
14e78 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
14e79 70 50 61 67 65 20 3d 20 2a 70 70 50 61 67 65 3b  pPage = *ppPage;
14e7a 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67  .  }.  if( !pPag
14e7b 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20  e->isInit ){.   
14e7c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
14e7d 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65  eeInitPage(pPage
14e7e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21  );.  }.  if( rc!
14e7f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14e80 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
14e81 61 67 65 29 3b 0a 20 20 20 20 2a 70 70 50 61 67  age);.    *ppPag
14e82 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  e = 0;.  }.  ret
14e83 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
14e84 20 52 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61   Release a MemPa
14e85 67 65 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64  ge.  This should
14e86 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   be called once 
14e87 66 6f 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a  for each prior.*
14e88 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  * call to sqlite
14e89 33 42 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a  3BtreeGetPage..*
14e8a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
14e8b 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67  leasePage(MemPag
14e8c 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28  e *pPage){.  if(
14e8d 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73   pPage ){.    as
14e8e 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
14e8f 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c  erflow==0 || sql
14e90 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66  ite3PagerPageRef
14e91 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62  count(pPage->pDb
14e92 50 61 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61  Page)>1 );.    a
14e93 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44  ssert( pPage->aD
14e94 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ata );.    asser
14e95 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b  t( pPage->pBt );
14e96 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
14e97 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72  ite3PagerGetExtr
14e98 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  a(pPage->pDbPage
14e99 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67  ) == (void*)pPag
14e9a 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  e );.    assert(
14e9b 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
14e9c 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50  Data(pPage->pDbP
14e9d 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61  age)==pPage->aDa
14e9e 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ta );.    assert
14e9f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
14ea0 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
14ea1 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73  >mutex) );.    s
14ea2 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
14ea3 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
14ea4 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
14ea5 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b  uring a rollback
14ea6 2c 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72  , when the pager
14ea7 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61   reloads informa
14ea8 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61  tion into the ca
14ea9 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74  che.** so that t
14eaa 68 65 20 63 61 63 68 65 20 69 73 20 72 65 73 74  he cache is rest
14eab 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67  ored to its orig
14eac 69 6e 61 6c 20 73 74 61 74 65 20 61 74 20 74 68  inal state at th
14ead 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68  e start of.** th
14eae 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66  e transaction, f
14eaf 6f 72 20 65 61 63 68 20 70 61 67 65 20 72 65 73  or each page res
14eb0 74 6f 72 65 64 20 74 68 69 73 20 72 6f 75 74 69  tored this routi
14eb1 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ne is called..**
14eb2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14eb3 20 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20   needs to reset 
14eb4 74 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73  the extra data s
14eb5 65 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e  ection at the en
14eb6 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65  d of the.** page
14eb7 20 74 6f 20 61 67 72 65 65 20 77 69 74 68 20 74   to agree with t
14eb8 68 65 20 72 65 73 74 6f 72 65 64 20 64 61 74 61  he restored data
14eb9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
14eba 20 70 61 67 65 52 65 69 6e 69 74 28 44 62 50 61   pageReinit(DbPa
14ebb 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65  ge *pData){.  Me
14ebc 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20  mPage *pPage;.  
14ebd 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65  pPage = (MemPage
14ebe 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
14ebf 65 74 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a  etExtra(pData);.
14ec0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14ec1 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75  3PagerPageRefcou
14ec2 6e 74 28 70 44 61 74 61 29 3e 30 20 29 3b 0a 20  nt(pData)>0 );. 
14ec3 20 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e   if( pPage->isIn
14ec4 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  it ){.    assert
14ec5 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
14ec6 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
14ec7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70  >mutex) );.    p
14ec8 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30  Page->isInit = 0
14ec9 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
14eca 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75  3PagerPageRefcou
14ecb 6e 74 28 70 44 61 74 61 29 3e 31 20 29 7b 0a 20  nt(pData)>1 ){. 
14ecc 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 6d 69       /* pPage mi
14ecd 67 68 74 20 6e 6f 74 20 62 65 20 61 20 62 74 72  ght not be a btr
14ece 65 65 20 70 61 67 65 3b 20 20 69 74 20 6d 69 67  ee page;  it mig
14ecf 68 74 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f  ht be an overflo
14ed0 77 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20  w page.      ** 
14ed1 6f 72 20 70 74 72 6d 61 70 20 70 61 67 65 20 6f  or ptrmap page o
14ed2 72 20 61 20 66 72 65 65 20 70 61 67 65 2e 20 20  r a free page.  
14ed3 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20  In those cases, 
14ed4 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
14ed5 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73      ** call to s
14ed6 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
14ed7 61 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 6c  age() will likel
14ed8 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  y return SQLITE_
14ed9 43 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20 2a  CORRUPT..      *
14eda 2a 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 73  * But no harm is
14edb 20 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 20   done by this.  
14edc 41 6e 64 20 69 74 20 69 73 20 76 65 72 79 20 69  And it is very i
14edd 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 20  mportant that.  
14ede 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74      ** sqlite3Bt
14edf 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 62 65  reeInitPage() be
14ee0 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 79   called on every
14ee1 20 62 74 72 65 65 20 70 61 67 65 20 73 6f 20 77   btree page so w
14ee2 65 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20  e make.      ** 
14ee3 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 65  the call for eve
14ee4 72 79 20 70 61 67 65 20 74 68 61 74 20 63 6f 6d  ry page that com
14ee5 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e 69  es in for re-ini
14ee6 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 73  ting. */.      s
14ee7 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
14ee8 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
14ee9 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  }.  }.}../*.** I
14eea 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68  nvoke the busy h
14eeb 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74 72  andler for a btr
14eec 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ee..*/.static in
14eed 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73  t btreeInvokeBus
14eee 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70  yHandler(void *p
14eef 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64  Arg){.  BtShared
14ef0 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72 65   *pBt = (BtShare
14ef1 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65 72  d*)pArg;.  asser
14ef2 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20  t( pBt->db );.  
14ef3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14ef4 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
14ef5 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
14ef6 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 6e  return sqlite3In
14ef7 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28  vokeBusyHandler(
14ef8 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61  &pBt->db->busyHa
14ef9 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ndler);.}../*.**
14efa 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73 65   Open a database
14efb 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46   file..** .** zF
14efc 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e  ilename is the n
14efd 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
14efe 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46  ase file.  If zF
14eff 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a  ilename is NULL.
14f00 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62 61 73  ** a new databas
14f01 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f 6d 20  e with a random 
14f02 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65 64 2e  name is created.
14f03 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20    This randomly 
14f04 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 73  named.** databas
14f05 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64  e file will be d
14f06 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71 6c 69  eleted when sqli
14f07 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 29 20  te3BtreeClose() 
14f08 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66  is called..** If
14f09 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a   zFilename is ":
14f0a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e  memory:" then an
14f0b 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
14f0c 61 73 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a  ase is created.*
14f0d 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61  * that is automa
14f0e 74 69 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 65  tically destroye
14f0f 64 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f  d when it is clo
14f10 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  sed..**.** If th
14f11 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6c  e database is al
14f12 72 65 61 64 79 20 6f 70 65 6e 65 64 20 69 6e 20  ready opened in 
14f13 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
14f14 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
14f15 61 6e 64 20 77 65 20 61 72 65 20 69 6e 20 73 68  and we are in sh
14f16 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 2c  ared cache mode,
14f17 20 74 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 77   then the open w
14f18 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e  ill fail with an
14f19 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  .** SQLITE_CONST
14f1a 52 41 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 65  RAINT error.  We
14f1b 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77   cannot allow tw
14f1c 6f 20 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 72  o or more BtShar
14f1d 65 64 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e  ed.** objects in
14f1e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
14f1f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69  se connection si
14f20 6e 63 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c  nce doing so wil
14f21 6c 20 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f  l lead.** to pro
14f22 62 6c 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69  blems with locki
14f23 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
14f24 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
14f25 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f  3BtreeOpen(.  co
14f26 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
14f27 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ame,  /* Name of
14f28 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69   the file contai
14f29 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 20 64  ning the BTree d
14f2a 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c  atabase */.  sql
14f2b 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
14f2c 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74       /* Associat
14f2d 65 64 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  ed database hand
14f2e 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a  le */.  Btree **
14f2f 70 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20  ppBtree,        
14f30 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65  /* Pointer to ne
14f31 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77  w Btree object w
14f32 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20  ritten here */. 
14f33 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20   int flags,     
14f34 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69           /* Opti
14f35 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73  ons */.  int vfs
14f36 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20  Flags           
14f37 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64   /* Flags passed
14f38 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69   through to sqli
14f39 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20  te3_vfs.xOpen() 
14f3a 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  */.){.  sqlite3_
14f3b 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20  vfs *pVfs;      
14f3c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46         /* The VF
14f3d 53 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69  S to use for thi
14f3e 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53  s btree */.  BtS
14f3f 68 61 72 65 64 20 2a 70 42 74 20 3d 20 30 3b 20  hared *pBt = 0; 
14f40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
14f41 68 61 72 65 64 20 70 61 72 74 20 6f 66 20 62 74  hared part of bt
14f42 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ree structure */
14f43 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20  .  Btree *p;    
14f44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14f45 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72    /* Handle to r
14f46 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  eturn */.  sqlit
14f47 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f  e3_mutex *mutexO
14f48 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65  pen = 0;  /* Pre
14f49 76 65 6e 74 73 20 61 20 72 61 63 65 20 63 6f 6e  vents a race con
14f4a 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23  dition. Ticket #
14f4b 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63  3537 */.  int rc
14f4c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
14f4d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
14f4e 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69  lt code from thi
14f4f 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  s function */.  
14f50 75 38 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20  u8 nReserve;    
14f51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14f52 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 73 65 64  * Byte of unused
14f53 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70   space on each p
14f54 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  age */.  unsigne
14f55 64 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 72  d char zDbHeader
14f56 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62  [100];  /* Datab
14f57 61 73 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65  ase header conte
14f58 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20  nt */..  /* Set 
14f59 74 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d  the variable isM
14f5a 65 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72  emdb to true for
14f5b 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
14f5c 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a  tabase, or .  **
14f5d 20 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c   false for a fil
14f5e 65 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 65  e-based database
14f5f 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73  . This symbol is
14f60 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69   only required i
14f61 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66  f.  ** either of
14f62 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 61   the shared-data
14f63 20 6f 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66   or autovacuum f
14f64 65 61 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70  eatures are comp
14f65 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20  iled .  ** into 
14f66 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a  the library..  *
14f67 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
14f68 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
14f69 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66  D_CACHE) || !def
14f6a 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
14f6b 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23  _AUTOVACUUM).  #
14f6c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
14f6d 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63  T_MEMORYDB.    c
14f6e 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62  onst int isMemdb
14f6f 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20   = 0;.  #else.  
14f70 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65    const int isMe
14f71 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20  mdb = zFilename 
14f72 26 26 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65  && !strcmp(zFile
14f73 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22  name, ":memory:"
14f74 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64  );.  #endif.#end
14f75 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62  if..  assert( db
14f76 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
14f77 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14f78 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
14f79 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e  ;..  pVfs = db->
14f7a 70 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69  pVfs;.  p = sqli
14f7b 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69  te3MallocZero(si
14f7c 7a 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20  zeof(Btree));.  
14f7d 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65  if( !p ){.    re
14f7e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
14f7f 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72  M;.  }.  p->inTr
14f80 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45  ans = TRANS_NONE
14f81 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a  ;.  p->db = db;.
14f82 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
14f83 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
14f84 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69  _CACHE) && !defi
14f85 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
14f86 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a  DISKIO).  /*.  *
14f87 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 65 20  * If this Btree 
14f88 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 66  is a candidate f
14f89 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 65 2c  or shared cache,
14f8a 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a   try to find an.
14f8b 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74    ** existing Bt
14f8c 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68  Shared object th
14f8d 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 65 20  at we can share 
14f8e 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20  with.  */.  if( 
14f8f 69 73 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a 46  isMemdb==0 && zF
14f90 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65  ilename && zFile
14f91 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69  name[0] ){.    i
14f92 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
14f93 43 6f 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63  Config.sharedCac
14f94 68 65 45 6e 61 62 6c 65 64 20 29 7b 0a 20 20 20  heEnabled ){.   
14f95 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68     int nFullPath
14f96 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50  name = pVfs->mxP
14f97 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20  athname+1;.     
14f98 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68   char *zFullPath
14f99 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61  name = sqlite3Ma
14f9a 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61  lloc(nFullPathna
14f9b 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  me);.      sqlit
14f9c 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53  e3_mutex *mutexS
14f9d 68 61 72 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e  hared;.      p->
14f9e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20  sharable = 1;.  
14f9f 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
14fa0 20 53 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61   SQLITE_SharedCa
14fa1 63 68 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21  che;.      if( !
14fa2 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29 7b  zFullPathname ){
14fa3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
14fa4 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  _free(p);.      
14fa5 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
14fa6 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
14fa7 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 75       sqlite3OsFu
14fa8 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c  llPathname(pVfs,
14fa9 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 46 75 6c   zFilename, nFul
14faa 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c  lPathname, zFull
14fab 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Pathname);.     
14fac 20 6d 75 74 65 78 4f 70 65 6e 20 3d 20 73 71 6c   mutexOpen = sql
14fad 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
14fae 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
14faf 49 43 5f 4f 50 45 4e 29 3b 0a 20 20 20 20 20 20  IC_OPEN);.      
14fb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
14fb1 74 65 72 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a  ter(mutexOpen);.
14fb2 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65        mutexShare
14fb3 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  d = sqlite3Mutex
14fb4 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
14fb5 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
14fb6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
14fb7 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74  _mutex_enter(mut
14fb8 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20  exShared);.     
14fb9 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 41 4c 28   for(pBt=GLOBAL(
14fba 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65  BtShared*,sqlite
14fbb 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
14fbc 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 42 74 2d  ); pBt; pBt=pBt-
14fbd 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20  >pNext){.       
14fbe 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 52   assert( pBt->nR
14fbf 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20  ef>0 );.        
14fc0 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 28 7a 46  if( 0==strcmp(zF
14fc1 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 73 71 6c  ullPathname, sql
14fc2 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d  ite3PagerFilenam
14fc3 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29 0a  e(pBt->pPager)).
14fc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14fc5 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 65 72   && sqlite3Pager
14fc6 56 66 73 28 70 42 74 2d 3e 70 50 61 67 65 72 29  Vfs(pBt->pPager)
14fc7 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20  ==pVfs ){.      
14fc8 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20      int iDb;.   
14fc9 20 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d 64         for(iDb=d
14fca 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 62 3e 3d 30  b->nDb-1; iDb>=0
14fcb 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20  ; iDb--){.      
14fcc 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 45 78        Btree *pEx
14fcd 69 73 74 69 6e 67 20 3d 20 64 62 2d 3e 61 44 62  isting = db->aDb
14fce 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 20  [iDb].pBt;.     
14fcf 20 20 20 20 20 20 20 69 66 28 20 70 45 78 69 73         if( pExis
14fd0 74 69 6e 67 20 26 26 20 70 45 78 69 73 74 69 6e  ting && pExistin
14fd1 67 2d 3e 70 42 74 3d 3d 70 42 74 20 29 7b 0a 20  g->pBt==pBt ){. 
14fd2 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
14fd3 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
14fd4 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20  (mutexShared);. 
14fd5 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
14fd6 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
14fd7 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20  (mutexOpen);.   
14fd8 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
14fd9 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74  e3_free(zFullPat
14fda 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  hname);.        
14fdb 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
14fdc 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20  ee(p);.         
14fdd 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
14fde 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20  TE_CONSTRAINT;. 
14fdf 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
14fe0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
14fe1 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b     p->pBt = pBt;
14fe2 0a 20 20 20 20 20 20 20 20 20 20 70 42 74 2d 3e  .          pBt->
14fe3 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nRef++;.        
14fe4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
14fe5 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
14fe6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
14fe7 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 64  eave(mutexShared
14fe8 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
14fe9 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e  _free(zFullPathn
14fea 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64  ame);.    }.#ifd
14feb 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
14fec 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20      else{.      
14fed 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d 6f 64 65  /* In debug mode
14fee 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c 20 70 65  , we mark all pe
14fef 72 73 69 73 74 65 6e 74 20 64 61 74 61 62 61 73  rsistent databas
14ff0 65 73 20 61 73 20 73 68 61 72 61 62 6c 65 0a 20  es as sharable. 
14ff1 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65       ** even whe
14ff2 6e 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20  n they are not. 
14ff3 20 54 68 69 73 20 65 78 65 72 63 69 73 65 73 20   This exercises 
14ff4 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 64 65  the locking code
14ff5 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 67 69   and.      ** gi
14ff6 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f 72 74 75  ves more opportu
14ff7 6e 69 74 79 20 66 6f 72 20 61 73 73 65 72 74 73  nity for asserts
14ff8 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68  (sqlite3_mutex_h
14ff9 65 6c 64 28 29 29 0a 20 20 20 20 20 20 2a 2a 20  eld()).      ** 
14ffa 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 66 69  statements to fi
14ffb 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 62 6c  nd locking probl
14ffc 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ems..      */.  
14ffd 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20      p->sharable 
14ffe 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  = 1;.    }.#endi
14fff 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69  f.  }.#endif.  i
15000 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20  f( pBt==0 ){.   
15001 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66   /*.    ** The f
15002 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 73  ollowing asserts
15003 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
15004 73 74 72 75 63 74 75 72 65 73 20 75 73 65 64 20  structures used 
15005 62 79 20 74 68 65 20 62 74 72 65 65 20 61 72 65  by the btree are
15006 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 69 67 68  .    ** the righ
15007 74 20 73 69 7a 65 2e 20 20 54 68 69 73 20 69 73  t size.  This is
15008 20 74 6f 20 67 75 61 72 64 20 61 67 61 69 6e 73   to guard agains
15009 74 20 73 69 7a 65 20 63 68 61 6e 67 65 73 20 74  t size changes t
1500a 68 61 74 20 72 65 73 75 6c 74 0a 20 20 20 20 2a  hat result.    *
1500b 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67  * when compiling
1500c 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20   on a different 
1500d 61 72 63 68 69 74 65 63 74 75 72 65 2e 0a 20 20  architecture..  
1500e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
1500f 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 38 20   sizeof(i64)==8 
15010 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d  || sizeof(i64)==
15011 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  4 );.    assert(
15012 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d 38 20   sizeof(u64)==8 
15013 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d  || sizeof(u64)==
15014 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  4 );.    assert(
15015 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20   sizeof(u32)==4 
15016 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
15017 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32 20 29 3b  izeof(u16)==2 );
15018 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a  .    assert( siz
15019 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 29 3b 0a  eof(Pgno)==4 );.
1501a 20 20 0a 20 20 20 20 70 42 74 20 3d 20 73 71 6c    .    pBt = sql
1501b 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20  ite3MallocZero( 
1501c 73 69 7a 65 6f 66 28 2a 70 42 74 29 20 29 3b 0a  sizeof(*pBt) );.
1501d 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29      if( pBt==0 )
1501e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1501f 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
15020 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e   goto btree_open
15021 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
15022 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15023 72 4f 70 65 6e 28 70 56 66 73 2c 20 26 70 42 74  rOpen(pVfs, &pBt
15024 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69 6c 65 6e  ->pPager, zFilen
15025 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ame,.           
15026 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
15027 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c 61 67 73  XTRA_SIZE, flags
15028 2c 20 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 20  , vfsFlags);.   
15029 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1502a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
1502b 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61   sqlite3PagerRea
1502c 64 46 69 6c 65 68 65 61 64 65 72 28 70 42 74 2d  dFileheader(pBt-
1502d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a  >pPager,sizeof(z
1502e 44 62 48 65 61 64 65 72 29 2c 7a 44 62 48 65 61  DbHeader),zDbHea
1502f 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  der);.    }.    
15030 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15031 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
15032 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a  btree_open_out;.
15033 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64      }.    pBt->d
15034 62 20 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69  b = db;.    sqli
15035 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68  te3PagerSetBusyh
15036 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67  andler(pBt->pPag
15037 65 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 42  er, btreeInvokeB
15038 75 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 29  usyHandler, pBt)
15039 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70  ;.    p->pBt = p
1503a 42 74 3b 0a 20 20 0a 20 20 20 20 73 71 6c 69 74  Bt;.  .    sqlit
1503b 65 33 50 61 67 65 72 53 65 74 52 65 69 6e 69 74  e3PagerSetReinit
1503c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  er(pBt->pPager, 
1503d 70 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 20  pageReinit);.   
1503e 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20   pBt->pCursor = 
1503f 30 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67  0;.    pBt->pPag
15040 65 31 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d  e1 = 0;.    pBt-
15041 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69  >readOnly = sqli
15042 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e  te3PagerIsreadon
15043 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  ly(pBt->pPager);
15044 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69  .    pBt->pageSi
15045 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 7a  ze = get2byte(&z
15046 44 62 48 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20  DbHeader[16]);. 
15047 20 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65     if( pBt->page
15048 53 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d  Size<512 || pBt-
15049 3e 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45  >pageSize>SQLITE
1504a 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20  _MAX_PAGE_SIZE. 
1504b 20 20 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74          || ((pBt
1504c 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42  ->pageSize-1)&pB
1504d 74 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20  t->pageSize)!=0 
1504e 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61  ){.      pBt->pa
1504f 67 65 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e  geSize = 0;.#ifn
15050 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
15051 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
15052 20 2f 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63   /* If the magic
15053 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22   name ":memory:"
15054 20 77 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20   will create an 
15055 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
15056 73 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a  se, then.      *
15057 2a 20 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f  * leave the auto
15058 56 61 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30  Vacuum mode at 0
15059 20 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61   (do not auto-va
1505a 63 75 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20  cuum), even if. 
1505b 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44       ** SQLITE_D
1505c 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55  EFAULT_AUTOVACUU
1505d 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68  M is true. On th
1505e 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66  e other hand, if
1505f 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45  .      ** SQLITE
15060 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68  _OMIT_MEMORYDB h
15061 61 73 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c  as been defined,
15062 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22   then ":memory:"
15063 20 69 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20   is just a.     
15064 20 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65   ** regular file
15065 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63  -name. In this c
15066 61 73 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63  ase the auto-vac
15067 75 75 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70  uum applies as p
15068 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20  er normal..     
15069 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46   */.      if( zF
1506a 69 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65  ilename && !isMe
1506b 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70  mdb ){.        p
1506c 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d  Bt->autoVacuum =
1506d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54   (SQLITE_DEFAULT
1506e 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20  _AUTOVACUUM ? 1 
1506f 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42  : 0);.        pB
15070 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20  t->incrVacuum = 
15071 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  (SQLITE_DEFAULT_
15072 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20  AUTOVACUUM==2 ? 
15073 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  1 : 0);.      }.
15074 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65  #endif.      nRe
15075 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d  serve = 0;.    }
15076 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52 65 73  else{.      nRes
15077 65 72 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72  erve = zDbHeader
15078 5b 32 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d  [20];.      pBt-
15079 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d  >pageSizeFixed =
1507a 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   1;.#ifndef SQLI
1507b 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1507c 55 4d 0a 20 20 20 20 20 20 70 42 74 2d 3e 61 75  UM.      pBt->au
1507d 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34  toVacuum = (get4
1507e 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b  byte(&zDbHeader[
1507f 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b  36 + 4*4])?1:0);
15080 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72  .      pBt->incr
15081 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79  Vacuum = (get4by
15082 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36  te(&zDbHeader[36
15083 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23   + 7*4])?1:0);.#
15084 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20  endif.    }.    
15085 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15086 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74  rSetPagesize(pBt
15087 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e  ->pPager, &pBt->
15088 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69  pageSize);.    i
15089 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 74 72  f( rc ) goto btr
1508a 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20  ee_open_out;.   
1508b 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
1508c 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65   = pBt->pageSize
1508d 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 20   - nReserve;.   
1508e 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e 70   assert( (pBt->p
1508f 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20  ageSize & 7)==0 
15090 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c  );  /* 8-byte al
15091 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 67 65 53  ignment of pageS
15092 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69 66 20 21  ize */.   .#if !
15093 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
15094 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
15095 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
15096 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
15097 29 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65  ).    /* Add the
15098 20 6e 65 77 20 42 74 53 68 61 72 65 64 20 6f 62   new BtShared ob
15099 6a 65 63 74 20 74 6f 20 74 68 65 20 6c 69 6e 6b  ject to the link
1509a 65 64 20 6c 69 73 74 20 73 68 61 72 61 62 6c 65  ed list sharable
1509b 20 42 74 53 68 61 72 65 64 73 2e 0a 20 20 20 20   BtShareds..    
1509c 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 68  */.    if( p->sh
1509d 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  arable ){.      
1509e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
1509f 75 74 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20  utexShared;.    
150a0 20 20 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b    pBt->nRef = 1;
150a1 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 72  .      mutexShar
150a2 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ed = sqlite3Mute
150a3 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
150a4 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
150a5 52 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51  R);.      if( SQ
150a6 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
150a7 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  && sqlite3Global
150a8 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65  Config.bCoreMute
150a9 78 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74  x ){.        pBt
150aa 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  ->mutex = sqlite
150ab 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
150ac 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a  TE_MUTEX_FAST);.
150ad 20 20 20 20 20 20 20 20 69 66 28 20 70 42 74 2d          if( pBt-
150ae 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20  >mutex==0 ){.   
150af 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
150b0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
150b1 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
150b2 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20  iled = 0;.      
150b3 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f      goto btree_o
150b4 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  pen_out;.       
150b5 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
150b6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
150b7 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 65 64  nter(mutexShared
150b8 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 4e  );.      pBt->pN
150b9 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53  ext = GLOBAL(BtS
150ba 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68  hared*,sqlite3Sh
150bb 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a  aredCacheList);.
150bc 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53        GLOBAL(BtS
150bd 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68  hared*,sqlite3Sh
150be 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d  aredCacheList) =
150bf 20 70 42 74 3b 0a 20 20 20 20 20 20 73 71 6c 69   pBt;.      sqli
150c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
150c1 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20  mutexShared);.  
150c2 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a    }.#endif.  }..
150c3 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
150c4 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
150c5 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e  CACHE) && !defin
150c6 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ed(SQLITE_OMIT_D
150c7 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74  ISKIO).  /* If t
150c8 68 65 20 6e 65 77 20 42 74 72 65 65 20 75 73 65  he new Btree use
150c9 73 20 61 20 73 68 61 72 61 62 6c 65 20 70 42 74  s a sharable pBt
150ca 53 68 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e  Shared, then lin
150cb 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42  k the new.  ** B
150cc 74 72 65 65 20 69 6e 74 6f 20 74 68 65 20 6c 69  tree into the li
150cd 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 62  st of all sharab
150ce 6c 65 20 42 74 72 65 65 73 20 66 6f 72 20 74 68  le Btrees for th
150cf 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f  e same connectio
150d0 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 74  n..  ** The list
150d1 20 69 73 20 6b 65 70 74 20 69 6e 20 61 73 63 65   is kept in asce
150d2 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70  nding order by p
150d3 42 74 20 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f  Bt address..  */
150d4 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62  .  if( p->sharab
150d5 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  le ){.    int i;
150d6 0a 20 20 20 20 42 74 72 65 65 20 2a 70 53 69 62  .    Btree *pSib
150d7 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
150d8 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
150d9 20 20 20 20 20 20 69 66 28 20 28 70 53 69 62 20        if( (pSib 
150da 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
150db 29 21 3d 30 20 26 26 20 70 53 69 62 2d 3e 73 68  )!=0 && pSib->sh
150dc 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  arable ){.      
150dd 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70    while( pSib->p
150de 50 72 65 76 20 29 7b 20 70 53 69 62 20 3d 20 70  Prev ){ pSib = p
150df 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 20  Sib->pPrev; }.  
150e0 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 42 74        if( p->pBt
150e1 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20  <pSib->pBt ){.  
150e2 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74          p->pNext
150e3 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20   = pSib;.       
150e4 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b     p->pPrev = 0;
150e5 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 62 2d  .          pSib-
150e6 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20  >pPrev = p;.    
150e7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
150e8 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62       while( pSib
150e9 2d 3e 70 4e 65 78 74 20 26 26 20 70 53 69 62 2d  ->pNext && pSib-
150ea 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70  >pNext->pBt<p->p
150eb 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Bt ){.          
150ec 20 20 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70    pSib = pSib->p
150ed 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20  Next;.          
150ee 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  }.          p->p
150ef 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65  Next = pSib->pNe
150f0 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d  xt;.          p-
150f1 3e 70 50 72 65 76 20 3d 20 70 53 69 62 3b 0a 20  >pPrev = pSib;. 
150f2 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e           if( p->
150f3 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20  pNext ){.       
150f4 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70       p->pNext->p
150f5 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20  Prev = p;.      
150f6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
150f7 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b  pSib->pNext = p;
150f8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
150f9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
150fa 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  }.    }.  }.#end
150fb 69 66 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20  if.  *ppBtree = 
150fc 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f  p;..btree_open_o
150fd 75 74 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ut:.  if( rc!=SQ
150fe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
150ff 66 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70  f( pBt && pBt->p
15100 50 61 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73  Pager ){.      s
15101 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65  qlite3PagerClose
15102 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  (pBt->pPager);. 
15103 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
15104 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20 20 20  _free(pBt);.    
15105 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
15106 0a 20 20 20 20 2a 70 70 42 74 72 65 65 20 3d 20  .    *ppBtree = 
15107 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75 74  0;.  }.  if( mut
15108 65 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61 73  exOpen ){.    as
15109 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1510a 74 65 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f 70  tex_held(mutexOp
1510b 65 6e 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  en) );.    sqlit
1510c 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
1510d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20  utexOpen);.  }. 
1510e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1510f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74  *.** Decrement t
15110 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66  he BtShared.nRef
15111 20 63 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20   counter.  When 
15112 69 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c  it reaches zero,
15113 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42  .** remove the B
15114 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72  tShared structur
15115 65 20 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69  e from the shari
15116 6e 67 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e  ng list.  Return
15117 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65 20  .** true if the 
15118 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f  BtShared.nRef co
15119 75 6e 74 65 72 20 72 65 61 63 68 65 73 20 7a 65  unter reaches ze
1511a 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a  ro and return.**
1511b 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 20   false if it is 
1511c 73 74 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a  still positive..
1511d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
1511e 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c  moveFromSharingL
1511f 69 73 74 28 42 74 53 68 61 72 65 64 20 2a 70 42  ist(BtShared *pB
15120 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  t){.#ifndef SQLI
15121 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
15122 41 43 48 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ACHE.  sqlite3_m
15123 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a 20  utex *pMaster;. 
15124 20 42 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74   BtShared *pList
15125 3b 0a 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20  ;.  int removed 
15126 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
15127 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
15128 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  theld(pBt->mutex
15129 29 20 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 3d  ) );.  pMaster =
1512a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
1512b 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
1512c 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
1512d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1512e 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a  enter(pMaster);.
1512f 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20    pBt->nRef--;. 
15130 20 69 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c 3d   if( pBt->nRef<=
15131 30 20 29 7b 0a 20 20 20 20 69 66 28 20 47 4c 4f  0 ){.    if( GLO
15132 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71  BAL(BtShared*,sq
15133 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
15134 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20 20  List)==pBt ){.  
15135 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61      GLOBAL(BtSha
15136 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
15137 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70  edCacheList) = p
15138 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  Bt->pNext;.    }
15139 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 69 73  else{.      pLis
1513a 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61  t = GLOBAL(BtSha
1513b 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
1513c 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20  edCacheList);.  
1513d 20 20 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59      while( ALWAY
1513e 53 28 70 4c 69 73 74 29 20 26 26 20 70 4c 69 73  S(pList) && pLis
1513f 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 7b  t->pNext!=pBt ){
15140 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 3d 70  .        pList=p
15141 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  List->pNext;.   
15142 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41     }.      if( A
15143 4c 57 41 59 53 28 70 4c 69 73 74 29 20 29 7b 0a  LWAYS(pList) ){.
15144 20 20 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 70          pList->p
15145 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78  Next = pBt->pNex
15146 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
15147 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
15148 54 48 52 45 41 44 53 41 46 45 20 29 7b 0a 20 20  THREADSAFE ){.  
15149 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1514a 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d 75 74 65  x_free(pBt->mute
1514b 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  x);.    }.    re
1514c 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  moved = 1;.  }. 
1514d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1514e 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 20  eave(pMaster);. 
1514f 20 72 65 74 75 72 6e 20 72 65 6d 6f 76 65 64 3b   return removed;
15150 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20  .#else.  return 
15151 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  1;.#endif.}../*.
15152 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 42 74  ** Make sure pBt
15153 2d 3e 70 54 6d 70 53 70 61 63 65 20 70 6f 69 6e  ->pTmpSpace poin
15154 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74  ts to an allocat
15155 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45  ion of .** MX_CE
15156 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 62 79 74  LL_SIZE(pBt) byt
15157 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es..*/.static vo
15158 69 64 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53  id allocateTempS
15159 70 61 63 65 28 42 74 53 68 61 72 65 64 20 2a 70  pace(BtShared *p
1515a 42 74 29 7b 0a 20 20 69 66 28 20 21 70 42 74 2d  Bt){.  if( !pBt-
1515b 3e 70 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20  >pTmpSpace ){.  
1515c 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65    pBt->pTmpSpace
1515d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61   = sqlite3PageMa
1515e 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53  lloc( pBt->pageS
1515f 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ize );.  }.}../*
15160 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 70 42 74  .** Free the pBt
15161 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 6c 6c 6f  ->pTmpSpace allo
15162 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63  cation.*/.static
15163 20 76 6f 69 64 20 66 72 65 65 54 65 6d 70 53 70   void freeTempSp
15164 61 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42  ace(BtShared *pB
15165 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 67  t){.  sqlite3Pag
15166 65 46 72 65 65 28 20 70 42 74 2d 3e 70 54 6d 70  eFree( pBt->pTmp
15167 53 70 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e 70  Space);.  pBt->p
15168 54 6d 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d 0a  TmpSpace = 0;.}.
15169 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20  ./*.** Close an 
1516a 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e  open database an
1516b 64 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c  d invalidate all
1516c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c   cursors..*/.SQL
1516d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1516e 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
1516f 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42  e(Btree *p){.  B
15170 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
15171 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72 73 6f  ->pBt;.  BtCurso
15172 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 43  r *pCur;..  /* C
15173 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73  lose all cursors
15174 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 69 73   opened via this
15175 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 61   handle.  */.  a
15176 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
15177 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d  utex_held(p->db-
15178 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
15179 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
1517a 29 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 74 2d  );.  pCur = pBt-
1517b 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 69 6c  >pCursor;.  whil
1517c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 42  e( pCur ){.    B
1517d 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d 20  tCursor *pTmp = 
1517e 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 20 3d  pCur;.    pCur =
1517f 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20   pCur->pNext;.  
15180 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 74 72    if( pTmp->pBtr
15181 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 73  ee==p ){.      s
15182 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
15183 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20 20  Cursor(pTmp);.  
15184 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f    }.  }..  /* Ro
15185 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 69 76  llback any activ
15186 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e  e transaction an
15187 64 20 66 72 65 65 20 74 68 65 20 68 61 6e 64 6c  d free the handl
15188 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a  e structure..  *
15189 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 71  * The call to sq
1518a 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
1518b 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 20 74  ck() drops any t
1518c 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20  able-locks held 
1518d 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 6e  by.  ** this han
1518e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  dle..  */.  sqli
1518f 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b  te3BtreeRollback
15190 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  (p);.  sqlite3Bt
15191 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 20  reeLeave(p);..  
15192 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
15193 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 73  still other outs
15194 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63  tanding referenc
15195 65 73 20 74 6f 20 74 68 65 20 73 68 61 72 65 64  es to the shared
15196 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 75  -btree.  ** stru
15197 63 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e 6f  cture, return no
15198 77 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65 72  w. The remainder
15199 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64 75   of this procedu
1519a 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a 20  re cleans .  ** 
1519b 75 70 20 74 68 65 20 73 68 61 72 65 64 2d 62 74  up the shared-bt
1519c 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ree..  */.  asse
1519d 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  rt( p->wantToLoc
1519e 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65  k==0 && p->locke
1519f 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70  d==0 );.  if( !p
151a0 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 65  ->sharable || re
151a1 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c  moveFromSharingL
151a2 69 73 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20  ist(pBt) ){.    
151a3 2f 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e 6f  /* The pBt is no
151a4 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 73   longer on the s
151a5 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f 20  haring list, so 
151a6 77 65 20 63 61 6e 20 61 63 63 65 73 73 0a 20 20  we can access.  
151a7 20 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 20    ** it without 
151a8 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 74  having to hold t
151a9 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a 2a  he mutex..    **
151aa 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f 75  .    ** Clean ou
151ab 74 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65  t and delete the
151ac 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
151ad 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
151ae 65 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72 73  ert( !pBt->pCurs
151af 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  or );.    sqlite
151b0 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d  3PagerClose(pBt-
151b1 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66  >pPager);.    if
151b2 28 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65  ( pBt->xFreeSche
151b3 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68 65  ma && pBt->pSche
151b4 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d  ma ){.      pBt-
151b5 3e 78 46 72 65 65 53 63 68 65 6d 61 28 70 42 74  >xFreeSchema(pBt
151b6 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  ->pSchema);.    
151b7 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
151b8 65 65 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 29  ee(pBt->pSchema)
151b9 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 70  ;.    freeTempSp
151ba 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71  ace(pBt);.    sq
151bb 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b  lite3_free(pBt);
151bc 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  .  }..#ifndef SQ
151bd 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
151be 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74 28  _CACHE.  assert(
151bf 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d   p->wantToLock==
151c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
151c1 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20  ->locked==0 );. 
151c2 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 20   if( p->pPrev ) 
151c3 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20  p->pPrev->pNext 
151c4 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 66  = p->pNext;.  if
151c5 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d 3e  ( p->pNext ) p->
151c6 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70  pNext->pPrev = p
151c7 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 0a  ->pPrev;.#endif.
151c8 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
151c9 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  p);.  return SQL
151ca 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
151cb 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d 69   Change the limi
151cc 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  t on the number 
151cd 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 64  of pages allowed
151ce 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a   in the cache..*
151cf 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
151d0 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68 65   number of cache
151d1 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74 6f   pages is set to
151d2 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a 2a   the absolute.**
151d3 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 65   value of mxPage
151d4 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73 20  .  If mxPage is 
151d5 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70 61  negative, the pa
151d6 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65 72  ger will.** oper
151d7 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73  ate asynchronous
151d8 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f 74  ly - it will not
151d9 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79 6e   stop to do fsyn
151da 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75 72  c()s.** to insur
151db 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65  e data is writte
151dc 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73 75  n to the disk su
151dd 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20  rface before.** 
151de 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72 61  continuing.  Tra
151df 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c 20  nsactions still 
151e0 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f 6e  work if synchron
151e1 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20 61  ous is off,.** a
151e2 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nd the database 
151e3 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75 70  cannot be corrup
151e4 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f 67  ted if this prog
151e5 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e 20  ram.** crashes. 
151e6 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65 72   But if the oper
151e7 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72 61  ating system cra
151e8 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69 73  shes or there is
151e9 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70 6f  .** an abrupt po
151ea 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65 6e  wer failure when
151eb 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20   synchronous is 
151ec 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61 73  off, the databas
151ed 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c 65  e.** could be le
151ee 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69  ft in an inconsi
151ef 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63 6f  stent and unreco
151f0 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a 2a  verable state..*
151f1 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73  * Synchronous is
151f2 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20 73   on by default s
151f3 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  o database corru
151f4 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20  ption is not.** 
151f5 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72 79  normally a worry
151f6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
151f7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
151f8 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65  treeSetCacheSize
151f9 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d  (Btree *p, int m
151fa 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61 72  xPage){.  BtShar
151fb 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
151fc 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
151fd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
151fe 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
151ff 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
15200 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65  ter(p);.  sqlite
15201 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73 69  3PagerSetCachesi
15202 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ze(pBt->pPager, 
15203 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74  mxPage);.  sqlit
15204 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
15205 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15206 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  _OK;.}../*.** Ch
15207 61 6e 67 65 20 74 68 65 20 77 61 79 20 64 61 74  ange the way dat
15208 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20 64  a is synced to d
15209 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  isk in order to 
1520a 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65 63 72  increase or decr
1520b 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c 6c  ease.** how well
1520c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65   the database re
1520d 73 69 73 74 73 20 64 61 6d 61 67 65 20 64 75 65  sists damage due
1520e 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 20 61   to OS crashes a
1520f 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c  nd power.** fail
15210 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 20 69  ures.  Level 1 i
15211 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 73  s the same as as
15212 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20 73  ynchronous (no s
15213 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61 6e 64  yncs() occur and
15214 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 68  .** there is a h
15215 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 20  igh probability 
15216 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65 76 65  of damage)  Leve
15217 6c 20 32 20 69 73 20 74 68 65 20 64 65 66 61 75  l 2 is the defau
15218 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69 73  lt.  There.** is
15219 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75 74 20   a very low but 
1521a 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69  non-zero probabi
1521b 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 2e 20  lity of damage. 
1521c 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63 65 73   Level 3 reduces
1521d 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69 6c   the.** probabil
1521e 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20 74 6f  ity of damage to
1521f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 20 77   near zero but w
15220 69 74 68 20 61 20 77 72 69 74 65 20 70 65 72 66  ith a write perf
15221 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74 69 6f  ormance reductio
15222 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  n..*/.#ifndef SQ
15223 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
15224 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50  PRAGMAS.SQLITE_P
15225 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15226 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79  e3BtreeSetSafety
15227 4c 65 76 65 6c 28 42 74 72 65 65 20 2a 70 2c 20  Level(Btree *p, 
15228 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 66  int level, int f
15229 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53 68  ullSync){.  BtSh
1522a 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
1522b 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  Bt;.  assert( sq
1522c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1522d 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
1522e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1522f 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69  Enter(p);.  sqli
15230 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74  te3PagerSetSafet
15231 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 67  yLevel(pBt->pPag
15232 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c 53  er, level, fullS
15233 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  ync);.  sqlite3B
15234 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
15235 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
15236 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
15237 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
15238 20 74 68 65 20 67 69 76 65 6e 20 62 74 72 65 65   the given btree
15239 20 69 73 20 73 65 74 20 74 6f 20 73 61 66 65 74   is set to safet
1523a 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20 6f  y level 1.  In o
1523b 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 72  ther.** words, r
1523c 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e 6f  eturn TRUE if no
1523d 20 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20 6f   sync() occurs o
1523e 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 73  n the disk files
1523f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
15240 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
15241 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64  treeSyncDisabled
15242 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74  (Btree *p){.  Bt
15243 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
15244 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  >pBt;.  int rc;.
15245 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15246 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
15247 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 0a  db->mutex) );  .
15248 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
15249 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74  ter(p);.  assert
1524a 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50  ( pBt && pBt->pP
1524b 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 73  ager );.  rc = s
1524c 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e  qlite3PagerNosyn
1524d 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a  c(pBt->pPager);.
1524e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1524f 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
15250 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66   rc;.}..#if !def
15251 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
15252 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20  _PAGER_PRAGMAS) 
15253 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
15254 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 0a  TE_OMIT_VACUUM).
15255 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
15256 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20 73   default pages s
15257 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d 62  ize and the numb
15258 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 62  er of reserved b
15259 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a 2a  ytes per page..*
1525a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20 70 61 67  * Or, if the pag
1525b 65 20 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61  e size has alrea
1525c 64 79 20 62 65 65 6e 20 66 69 78 65 64 2c 20 72  dy been fixed, r
1525d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41  eturn SQLITE_REA
1525e 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74 68 6f 75  DONLY .** withou
1525f 74 20 63 68 61 6e 67 69 6e 67 20 61 6e 79 74 68  t changing anyth
15260 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ing..**.** The p
15261 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65  age size must be
15262 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65   a power of 2 be
15263 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35  tween 512 and 65
15264 35 33 36 2e 20 20 49 66 20 74 68 65 20 70 61 67  536.  If the pag
15265 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c 69  e.** size suppli
15266 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65 74  ed does not meet
15267 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   this constraint
15268 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 73   then the page s
15269 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 68  ize is not.** ch
1526a 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 67  anged..**.** Pag
1526b 65 20 73 69 7a 65 73 20 61 72 65 20 63 6f 6e 73  e sizes are cons
1526c 74 72 61 69 6e 65 64 20 74 6f 20 62 65 20 61 20  trained to be a 
1526d 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 73 6f 20  power of two so 
1526e 74 68 61 74 20 74 68 65 20 72 65 67 69 6f 6e 0a  that the region.
1526f 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ** of the databa
15270 73 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72  se file used for
15271 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e 6e   locking (beginn
15272 69 6e 67 20 61 74 20 50 45 4e 44 49 4e 47 5f 42  ing at PENDING_B
15273 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72 73  YTE,.** the firs
15274 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20  t byte past the 
15275 31 47 42 20 62 6f 75 6e 64 61 72 79 2c 20 30 78  1GB boundary, 0x
15276 34 30 30 30 30 30 30 30 29 20 6e 65 65 64 73 20  40000000) needs 
15277 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74  to occur.** at t
15278 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
15279 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  a page..**.** If
1527a 20 70 61 72 61 6d 65 74 65 72 20 6e 52 65 73 65   parameter nRese
1527b 72 76 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e  rve is less than
1527c 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
1527d 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76  number of reserv
1527e 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 20  ed.** bytes per 
1527f 70 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63  page is left unc
15280 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  hanged..**.** If
15281 20 74 68 65 20 69 46 69 78 21 3d 30 20 74 68 65   the iFix!=0 the
15282 6e 20 74 68 65 20 70 61 67 65 53 69 7a 65 46 69  n the pageSizeFi
15283 78 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20  xed flag is set 
15284 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 67 65  so that the page
15285 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20 61 75 74   size.** and aut
15286 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20 63 61 6e  ovacuum mode can
15287 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68   no longer be ch
15288 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  anged..*/.SQLITE
15289 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1528a 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65  ite3BtreeSetPage
1528b 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69  Size(Btree *p, i
1528c 6e 74 20 70 61 67 65 53 69 7a 65 2c 20 69 6e 74  nt pageSize, int
1528d 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20 69   nReserve, int i
1528e 46 69 78 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Fix){.  int rc =
1528f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74   SQLITE_OK;.  Bt
15290 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
15291 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20  >pBt;.  assert( 
15292 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20 26 26 20  nReserve>=-1 && 
15293 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b  nReserve<=255 );
15294 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
15295 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70  nter(p);.  if( p
15296 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65  Bt->pageSizeFixe
15297 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
15298 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
15299 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1529a 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20  _READONLY;.  }. 
1529b 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20   if( nReserve<0 
1529c 29 7b 0a 20 20 20 20 6e 52 65 73 65 72 76 65 20  ){.    nReserve 
1529d 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  = pBt->pageSize 
1529e 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  - pBt->usableSiz
1529f 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  e;.  }.  assert(
152a0 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20   nReserve>=0 && 
152a1 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b  nReserve<=255 );
152a2 0a 20 20 69 66 28 20 70 61 67 65 53 69 7a 65 3e  .  if( pageSize>
152a3 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a 65  =512 && pageSize
152a4 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  <=SQLITE_MAX_PAG
152a5 45 5f 53 49 5a 45 20 26 26 0a 20 20 20 20 20 20  E_SIZE &&.      
152a6 20 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29 26    ((pageSize-1)&
152a7 70 61 67 65 53 69 7a 65 29 3d 3d 30 20 29 7b 0a  pageSize)==0 ){.
152a8 20 20 20 20 61 73 73 65 72 74 28 20 28 70 61 67      assert( (pag
152a9 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b  eSize & 7)==0 );
152aa 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42  .    assert( !pB
152ab 74 2d 3e 70 50 61 67 65 31 20 26 26 20 21 70 42  t->pPage1 && !pB
152ac 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20  t->pCursor );.  
152ad 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
152ae 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b  = (u16)pageSize;
152af 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 61  .    freeTempSpa
152b0 63 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20  ce(pBt);.    rc 
152b1 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  = sqlite3PagerSe
152b2 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70  tPagesize(pBt->p
152b3 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67  Pager, &pBt->pag
152b4 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 70 42  eSize);.  }.  pB
152b5 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20  t->usableSize = 
152b6 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20  pBt->pageSize - 
152b7 28 75 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20  (u16)nReserve;. 
152b8 20 69 66 28 20 69 46 69 78 20 29 20 70 42 74 2d   if( iFix ) pBt-
152b9 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d  >pageSizeFixed =
152ba 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   1;.  sqlite3Btr
152bb 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
152bc 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
152bd 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72  * Return the cur
152be 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70  rently defined p
152bf 61 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49  age size.*/.SQLI
152c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
152c1 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
152c2 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29  geSize(Btree *p)
152c3 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42  {.  return p->pB
152c4 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a  t->pageSize;.}..
152c5 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
152c6 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
152c7 20 6f 66 20 73 70 61 63 65 20 61 74 20 74 68 65   of space at the
152c8 20 65 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61   end of every pa
152c9 67 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69  ge that.** are i
152ca 6e 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20  ntentually left 
152cb 75 6e 75 73 65 64 2e 20 20 54 68 69 73 20 69 73  unused.  This is
152cc 20 74 68 65 20 22 72 65 73 65 72 76 65 64 22 20   the "reserved" 
152cd 73 70 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a  space that is.**
152ce 20 73 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20   sometimes used 
152cf 62 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a  by extensions..*
152d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
152d1 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
152d2 65 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65  eGetReserve(Btre
152d3 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a  e *p){.  int n;.
152d4 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
152d5 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d  ter(p);.  n = p-
152d6 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d  >pBt->pageSize -
152d7 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53   p->pBt->usableS
152d8 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ize;.  sqlite3Bt
152d9 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
152da 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
152db 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75  * Set the maximu
152dc 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72  m page count for
152dd 20 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d   a database if m
152de 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76  xPage is positiv
152df 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73  e..** No changes
152e0 20 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50   are made if mxP
152e1 61 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61  age is 0 or nega
152e2 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c  tive..** Regardl
152e3 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ess of the value
152e4 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75   of mxPage, retu
152e5 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70  rn the maximum p
152e6 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51  age count..*/.SQ
152e7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
152e8 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78   sqlite3BtreeMax
152e9 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20  PageCount(Btree 
152ea 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b  *p, int mxPage){
152eb 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69  .  int n;.  sqli
152ec 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
152ed 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50  ;.  n = sqlite3P
152ee 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74  agerMaxPageCount
152ef 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c  (p->pBt->pPager,
152f0 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69   mxPage);.  sqli
152f1 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
152f2 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a  ;.  return n;.}.
152f3 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
152f4 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ed(SQLITE_OMIT_P
152f5 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c  AGER_PRAGMAS) ||
152f6 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
152f7 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f  _OMIT_VACUUM) */
152f8 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
152f9 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27  he 'auto-vacuum'
152fa 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65   property of the
152fb 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68   database. If th
152fc 65 20 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a  e 'autoVacuum'.*
152fd 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  * parameter is n
152fe 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75  on-zero, then au
152ff 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69  to-vacuum mode i
15300 73 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65  s enabled. If ze
15301 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73  ro, it.** is dis
15302 61 62 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75  abled. The defau
15303 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
15304 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f   auto-vacuum pro
15305 70 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74  perty is .** det
15306 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53  ermined by the S
15307 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
15308 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a  TOVACUUM macro..
15309 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1530a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1530b 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeSetAutoVacuum(
1530c 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75  Btree *p, int au
1530d 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65  toVacuum){.#ifde
1530e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1530f 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72  TOVACUUM.  retur
15310 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  n SQLITE_READONL
15311 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61  Y;.#else.  BtSha
15312 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
15313 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
15314 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76  LITE_OK;.  u8 av
15315 20 3d 20 28 75 38 29 61 75 74 6f 56 61 63 75 75   = (u8)autoVacuu
15316 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72  m;..  sqlite3Btr
15317 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66  eeEnter(p);.  if
15318 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46  ( pBt->pageSizeF
15319 69 78 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30  ixed && (av ?1:0
1531a 29 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  )!=pBt->autoVacu
1531b 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  um ){.    rc = S
1531c 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a  QLITE_READONLY;.
1531d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74    }else{.    pBt
1531e 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61  ->autoVacuum = a
1531f 76 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d  v ?1:0;.    pBt-
15320 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76  >incrVacuum = av
15321 3d 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20  ==2 ?1:0;.  }.  
15322 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
15323 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
15324 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  c;.#endif.}../*.
15325 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61  ** Return the va
15326 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f  lue of the 'auto
15327 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74  -vacuum' propert
15328 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75  y. If auto-vacuu
15329 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64  m is .** enabled
1532a 20 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20   1 is returned. 
1532b 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a  Otherwise 0..*/.
1532c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1532d 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  nt sqlite3BtreeG
1532e 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72  etAutoVacuum(Btr
1532f 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53  ee *p){.#ifdef S
15330 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
15331 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42  ACUUM.  return B
15332 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
15333 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e  NONE;.#else.  in
15334 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42  t rc;.  sqlite3B
15335 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
15336 72 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e  rc = (.    (!p->
15337 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
15338 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55  ?BTREE_AUTOVACUU
15339 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d  M_NONE:.    (!p-
1533a 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d  >pBt->incrVacuum
1533b 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  )?BTREE_AUTOVACU
1533c 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52  UM_FULL:.    BTR
1533d 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e  EE_AUTOVACUUM_IN
1533e 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65  CR.  );.  sqlite
1533f 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
15340 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e    return rc;.#en
15341 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65  dif.}.../*.** Ge
15342 74 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  t a reference to
15343 20 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64   pPage1 of the d
15344 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
15345 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f  his will.** also
15346 20 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c   acquire a readl
15347 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65  ock on that file
15348 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  ..**.** SQLITE_O
15349 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  K is returned on
1534a 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68   success.  If th
1534b 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a  e file is not a.
1534c 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64  ** well-formed d
1534d 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
1534e 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  en SQLITE_CORRUP
1534f 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  T is returned..*
15350 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73  * SQLITE_BUSY is
15351 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
15352 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63   database is loc
15353 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d  ked.  SQLITE_NOM
15354 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  EM.** is returne
15355 64 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20  d if we run out 
15356 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73  of memory. .*/.s
15357 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74  tatic int lockBt
15358 72 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42  ree(BtShared *pB
15359 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
1535a 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b  MemPage *pPage1;
1535b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20  .  int nPage;.. 
1535c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1535d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
1535e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
1535f 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ert( pBt->pPage1
15360 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ==0 );.  rc = sq
15361 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
15362 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65  e(pBt, 1, &pPage
15363 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21  1, 0);.  if( rc!
15364 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
15365 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f  urn rc;..  /* Do
15366 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20 74   some checking t
15367 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74 68  o help insure th
15368 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65 64  e file we opened
15369 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a 20   really is.  ** 
1536a 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 73 65  a valid database
1536b 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 72   file. .  */.  r
1536c 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1536d 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70  Pagecount(pBt->p
1536e 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a  Pager, &nPage);.
1536f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
15370 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  _OK ){.    goto 
15371 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
15372 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  d;.  }else if( n
15373 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 69 6e  Page>0 ){.    in
15374 74 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  t pageSize;.    
15375 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a  int usableSize;.
15376 20 20 20 20 75 38 20 2a 70 61 67 65 31 20 3d 20      u8 *page1 = 
15377 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20  pPage1->aData;. 
15378 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
15379 4f 54 41 44 42 3b 0a 20 20 20 20 69 66 28 20 6d  OTADB;.    if( m
1537a 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61  emcmp(page1, zMa
1537b 67 69 63 48 65 61 64 65 72 2c 20 31 36 29 21 3d  gicHeader, 16)!=
1537c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
1537d 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
1537e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  d;.    }.    if(
1537f 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a   page1[18]>1 ){.
15380 20 20 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f        pBt->readO
15381 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  nly = 1;.    }. 
15382 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d     if( page1[19]
15383 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  >1 ){.      goto
15384 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
15385 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ed;.    }..    /
15386 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 65 6d  * The maximum em
15387 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20  bedded fraction 
15388 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20  must be exactly 
15389 32 35 25 2e 20 20 41 6e 64 20 74 68 65 20 6d 69  25%.  And the mi
1538a 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d 62  nimum.    ** emb
1538b 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d  edded fraction m
1538c 75 73 74 20 62 65 20 31 32 2e 35 25 20 66 6f 72  ust be 12.5% for
1538d 20 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 61 20   both leaf-data 
1538e 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74  and non-leaf-dat
1538f 61 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 72  a..    ** The or
15390 69 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 61 6c  iginal design al
15391 6c 6f 77 65 64 20 74 68 65 73 65 20 61 6d 6f 75  lowed these amou
15392 6e 74 73 20 74 6f 20 76 61 72 79 2c 20 62 75 74  nts to vary, but
15393 20 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76 65   as of.    ** ve
15394 72 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 20  rsion 3.6.0, we 
15395 72 65 71 75 69 72 65 20 74 68 65 6d 20 74 6f 20  require them to 
15396 62 65 20 66 69 78 65 64 2e 0a 20 20 20 20 2a 2f  be fixed..    */
15397 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28  .    if( memcmp(
15398 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31 30  &page1[21], "\10
15399 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 3d 30  0\040\040",3)!=0
1539a 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
1539b 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
1539c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65  ;.    }.    page
1539d 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28  Size = get2byte(
1539e 26 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20 20  &page1[16]);.   
1539f 20 69 66 28 20 28 28 70 61 67 65 53 69 7a 65 2d   if( ((pageSize-
153a0 31 29 26 70 61 67 65 53 69 7a 65 29 21 3d 30 20  1)&pageSize)!=0 
153a1 7c 7c 20 70 61 67 65 53 69 7a 65 3c 35 31 32 20  || pageSize<512 
153a2 7c 7c 0a 20 20 20 20 20 20 20 20 28 53 51 4c 49  ||.        (SQLI
153a3 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
153a4 3c 33 32 37 36 38 20 26 26 20 70 61 67 65 53 69  <32768 && pageSi
153a5 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ze>SQLITE_MAX_PA
153a6 47 45 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b 0a  GE_SIZE).    ){.
153a7 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31        goto page1
153a8 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20  _init_failed;.  
153a9 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
153aa 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d  (pageSize & 7)==
153ab 30 20 29 3b 0a 20 20 20 20 75 73 61 62 6c 65 53  0 );.    usableS
153ac 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 20 2d  ize = pageSize -
153ad 20 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20   page1[20];.    
153ae 69 66 28 20 70 61 67 65 53 69 7a 65 21 3d 70 42  if( pageSize!=pB
153af 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20  t->pageSize ){. 
153b0 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72 65       /* After re
153b1 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 74 20  ading the first 
153b2 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
153b3 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61 20  base assuming a 
153b4 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20 20  page size.      
153b5 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e 70  ** of BtShared.p
153b6 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76 65  ageSize, we have
153b7 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74   discovered that
153b8 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69   the page-size i
153b9 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75 61  s.      ** actua
153ba 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55 6e  lly pageSize. Un
153bb 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  lock the databas
153bc 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50  e, leave pBt->pP
153bd 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a 2a  age1 at.      **
153be 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
153bf 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20   SQLITE_OK. The 
153c0 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c  caller will call
153c1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20   this function. 
153c2 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69       ** again wi
153c3 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 20 70  th the correct p
153c4 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20  age-size..      
153c5 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  */.      release
153c6 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20  Page(pPage1);.  
153c7 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53      pBt->usableS
153c8 69 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c  ize = (u16)usabl
153c9 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 74  eSize;.      pBt
153ca 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31  ->pageSize = (u1
153cb 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  6)pageSize;.    
153cc 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28    freeTempSpace(
153cd 70 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  pBt);.      rc =
153ce 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
153cf 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50  Pagesize(pBt->pP
153d0 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65  ager, &pBt->page
153d1 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Size);.      if(
153d2 20 72 63 20 29 20 67 6f 74 6f 20 70 61 67 65 31   rc ) goto page1
153d3 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20  _init_failed;.  
153d4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
153d5 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
153d6 69 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 35  if( usableSize<5
153d7 30 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  00 ){.      goto
153d8 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
153d9 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  ed;.    }.    pB
153da 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75  t->pageSize = (u
153db 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  16)pageSize;.   
153dc 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
153dd 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 69   = (u16)usableSi
153de 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ze;.#ifndef SQLI
153df 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
153e0 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f  UM.    pBt->auto
153e1 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79  Vacuum = (get4by
153e2 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34  te(&page1[36 + 4
153e3 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70  *4])?1:0);.    p
153e4 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d  Bt->incrVacuum =
153e5 20 28 67 65 74 34 62 79 74 65 28 26 70 61 67 65   (get4byte(&page
153e6 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30  1[36 + 7*4])?1:0
153e7 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20  );.#endif.  }.. 
153e8 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20   /* maxLocal is 
153e9 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75  the maximum amou
153ea 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f  nt of payload to
153eb 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66   store locally f
153ec 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20  or.  ** a cell. 
153ed 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   Make sure it is
153ee 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f   small enough so
153ef 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d   that at least m
153f0 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65  inFanout.  ** ce
153f1 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74  lls can will fit
153f2 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57   on one page.  W
153f3 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79  e assume a 10-by
153f4 74 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a  te page header..
153f5 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65    ** Besides the
153f6 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65   payload, the ce
153f7 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20  ll must store:. 
153f8 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70   **     2-byte p
153f9 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65  ointer to the ce
153fa 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79  ll.  **     4-by
153fb 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72  te child pointer
153fc 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65  .  **     9-byte
153fd 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a   nKey value.  **
153fe 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74       4-byte nDat
153ff 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20  a value.  **    
15400 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77   4-byte overflow
15401 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20   page pointer.  
15402 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e  ** So a cell con
15403 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74  sists of a 2-byt
15404 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64  e poiner, a head
15405 65 72 20 77 68 69 63 68 20 69 73 20 61 73 20 6d  er which is as m
15406 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62  uch as.  ** 17 b
15407 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20  ytes long, 0 to 
15408 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f  N bytes of paylo
15409 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f  ad, and an optio
1540a 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66  nal 4 byte overf
1540b 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f  low.  ** page po
1540c 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42  inter..  */.  pB
1540d 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70  t->maxLocal = (p
1540e 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31  Bt->usableSize-1
1540f 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a  2)*64/255 - 23;.
15410 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20    pBt->minLocal 
15411 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  = (pBt->usableSi
15412 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20  ze-12)*32/255 - 
15413 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65  23;.  pBt->maxLe
15414 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  af = pBt->usable
15415 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74  Size - 35;.  pBt
15416 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74  ->minLeaf = (pBt
15417 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29  ->usableSize-12)
15418 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20  *32/255 - 23;.  
15419 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78  assert( pBt->max
1541a 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f  Leaf + 23 <= MX_
1541b 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29  CELL_SIZE(pBt) )
1541c 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20  ;.  pBt->pPage1 
1541d 3d 20 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75  = pPage1;.  retu
1541e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70  rn SQLITE_OK;..p
1541f 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
15420 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28  :.  releasePage(
15421 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e  pPage1);.  pBt->
15422 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65  pPage1 = 0;.  re
15423 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
15424 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
15425 6f 72 6b 73 20 6c 69 6b 65 20 6c 6f 63 6b 42 74  orks like lockBt
15426 72 65 65 28 29 20 65 78 63 65 70 74 20 74 68 61  ree() except tha
15427 74 20 69 74 20 61 6c 73 6f 20 69 6e 76 6f 6b 65  t it also invoke
15428 73 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61  s the.** busy ca
15429 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72 65 20  llback if there 
1542a 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69  is lock contenti
1542b 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
1542c 74 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52  t lockBtreeWithR
1542d 65 74 72 79 28 42 74 72 65 65 20 2a 70 52 65 66  etry(Btree *pRef
1542e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1542f 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65  LITE_OK;..  asse
15430 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
15431 48 6f 6c 64 73 4d 75 74 65 78 28 70 52 65 66 29  HoldsMutex(pRef)
15432 20 29 3b 0a 20 20 69 66 28 20 70 52 65 66 2d 3e   );.  if( pRef->
15433 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e  inTrans==TRANS_N
15434 4f 4e 45 20 29 7b 0a 20 20 20 20 75 38 20 69 6e  ONE ){.    u8 in
15435 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 52  Transaction = pR
15436 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73  ef->pBt->inTrans
15437 61 63 74 69 6f 6e 3b 0a 20 20 20 20 62 74 72 65  action;.    btre
15438 65 49 6e 74 65 67 72 69 74 79 28 70 52 65 66 29  eIntegrity(pRef)
15439 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1543a 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  e3BtreeBeginTran
1543b 73 28 70 52 65 66 2c 20 30 29 3b 0a 20 20 20 20  s(pRef, 0);.    
1543c 70 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61  pRef->pBt->inTra
1543d 6e 73 61 63 74 69 6f 6e 20 3d 20 69 6e 54 72 61  nsaction = inTra
1543e 6e 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20 70 52  nsaction;.    pR
1543f 65 66 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52  ef->inTrans = TR
15440 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 69 66  ANS_NONE;.    if
15441 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15442 29 7b 0a 20 20 20 20 20 20 70 52 65 66 2d 3e 70  ){.      pRef->p
15443 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e  Bt->nTransaction
15444 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 74  --;.    }.    bt
15445 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 52 65  reeIntegrity(pRe
15446 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  f);.  }.  return
15447 20 72 63 3b 0a 7d 0a 20 20 20 20 20 20 20 0a 0a   rc;.}.       ..
15448 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61  /*.** If there a
15449 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e  re no outstandin
1544a 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 77 65  g cursors and we
1544b 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20   are not in the 
1544c 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 74  middle.** of a t
1544d 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 74  ransaction but t
1544e 68 65 72 65 20 69 73 20 61 20 72 65 61 64 20 6c  here is a read l
1544f 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
15450 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69  ase, then.** thi
15451 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 73  s routine unrefs
15452 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
15453 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
15454 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 68  file which .** h
15455 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  as the effect of
15456 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 72   releasing the r
15457 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  ead lock..**.** 
15458 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  If there are any
15459 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72   outstanding cur
1545a 73 6f 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69  sors, this routi
1545b 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
1545c 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  *.** If there is
1545d 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
1545e 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 69 73  n progress, this
1545f 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
15460 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  -op..*/.static v
15461 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49  oid unlockBtreeI
15462 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 65 64  fUnused(BtShared
15463 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 72 74   *pBt){.  assert
15464 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
15465 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
15466 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69   );.  if( pBt->i
15467 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
15468 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d  ANS_NONE && pBt-
15469 3e 70 43 75 72 73 6f 72 3d 3d 30 20 26 26 20 70  >pCursor==0 && p
1546a 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 7b  Bt->pPage1!=0 ){
1546b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1546c 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42  PagerRefcount(pB
1546d 74 2d 3e 70 50 61 67 65 72 29 3e 3d 31 20 29 7b  t->pPager)>=1 ){
1546e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1546f 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
15470 61 20 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61  a );.      relea
15471 73 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67  sePage(pBt->pPag
15472 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  e1);.    }.    p
15473 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a  Bt->pPage1 = 0;.
15474 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65    }.}../*.** Cre
15475 61 74 65 20 61 20 6e 65 77 20 64 61 74 61 62 61  ate a new databa
15476 73 65 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69  se by initializi
15477 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67  ng the first pag
15478 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65  e of the.** file
15479 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1547a 6e 65 77 44 61 74 61 62 61 73 65 28 42 74 53 68  newDatabase(BtSh
1547b 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65  ared *pBt){.  Me
1547c 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e  mPage *pP1;.  un
1547d 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74  signed char *dat
1547e 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  a;.  int rc;.  i
1547f 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73  nt nPage;..  ass
15480 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15481 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
15482 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ex) );.  rc = sq
15483 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
15484 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  unt(pBt->pPager,
15485 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20   &nPage);.  if( 
15486 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
15487 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20   nPage>0 ){.    
15488 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
15489 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67   pP1 = pBt->pPag
1548a 65 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  e1;.  assert( pP
1548b 31 21 3d 30 20 29 3b 0a 20 20 64 61 74 61 20 3d  1!=0 );.  data =
1548c 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72   pP1->aData;.  r
1548d 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1548e 57 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50 61  Write(pP1->pDbPa
1548f 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  ge);.  if( rc ) 
15490 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d  return rc;.  mem
15491 63 70 79 28 64 61 74 61 2c 20 7a 4d 61 67 69 63  cpy(data, zMagic
15492 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a  Header, sizeof(z
15493 4d 61 67 69 63 48 65 61 64 65 72 29 29 3b 0a 20  MagicHeader));. 
15494 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
15495 7a 4d 61 67 69 63 48 65 61 64 65 72 29 3d 3d 31  zMagicHeader)==1
15496 36 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28  6 );.  put2byte(
15497 26 64 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e  &data[16], pBt->
15498 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74  pageSize);.  dat
15499 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74  a[18] = 1;.  dat
1549a 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73  a[19] = 1;.  ass
1549b 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65  ert( pBt->usable
1549c 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53  Size<=pBt->pageS
1549d 69 7a 65 20 26 26 20 70 42 74 2d 3e 75 73 61 62  ize && pBt->usab
1549e 6c 65 53 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d  leSize+255>=pBt-
1549f 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61  >pageSize);.  da
154a0 74 61 5b 32 30 5d 20 3d 20 28 75 38 29 28 70 42  ta[20] = (u8)(pB
154a1 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42  t->pageSize - pB
154a2 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a  t->usableSize);.
154a3 20 20 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b    data[21] = 64;
154a4 0a 20 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32  .  data[22] = 32
154a5 3b 0a 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33  ;.  data[23] = 3
154a6 32 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74  2;.  memset(&dat
154a7 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34  a[24], 0, 100-24
154a8 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 50  );.  zeroPage(pP
154a9 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54  1, PTF_INTKEY|PT
154aa 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 41 46 44  F_LEAF|PTF_LEAFD
154ab 41 54 41 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61  ATA );.  pBt->pa
154ac 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b  geSizeFixed = 1;
154ad 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
154ae 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
154af 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61    assert( pBt->a
154b0 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20  utoVacuum==1 || 
154b1 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d  pBt->autoVacuum=
154b2 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
154b3 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d  pBt->incrVacuum=
154b4 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56  =1 || pBt->incrV
154b5 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75  acuum==0 );.  pu
154b6 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 20  t4byte(&data[36 
154b7 2b 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74  + 4*4], pBt->aut
154b8 6f 56 61 63 75 75 6d 29 3b 0a 20 20 70 75 74 34  oVacuum);.  put4
154b9 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20  byte(&data[36 + 
154ba 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56  7*4], pBt->incrV
154bb 61 63 75 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20  acuum);.#endif. 
154bc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
154bd 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  K;.}../*.** Atte
154be 6d 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e  mpt to start a n
154bf 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ew transaction. 
154c0 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  A write-transact
154c1 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65  ion.** is starte
154c2 64 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20  d if the second 
154c3 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a  argument is nonz
154c4 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ero, otherwise a
154c5 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61   read-.** transa
154c6 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73  ction.  If the s
154c7 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
154c8 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20  s 2 or more and 
154c9 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61  exclusive.** tra
154ca 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72  nsaction is star
154cb 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61  ted, meaning tha
154cc 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65  t no other proce
154cd 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a  ss is allowed.**
154ce 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64   to access the d
154cf 61 74 61 62 61 73 65 2e 20 20 41 20 70 72 65 65  atabase.  A pree
154d0 78 69 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74  xisting transact
154d1 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a  ion may not be.*
154d2 2a 20 75 70 67 72 61 64 65 64 20 74 6f 20 65 78  * upgraded to ex
154d3 63 6c 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69  clusive by calli
154d4 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ng this routine 
154d5 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20  a second time - 
154d6 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69  the.** exclusivi
154d7 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72  ty flag only wor
154d8 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61  ks for a new tra
154d9 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
154da 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  A write-transact
154db 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72  ion must be star
154dc 74 65 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d  ted before attem
154dd 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68  pting any .** ch
154de 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74  anges to the dat
154df 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20  abase.  None of 
154e0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  the following ro
154e1 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20  utines .** will 
154e2 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72  work unless a tr
154e3 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61  ansaction is sta
154e4 72 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a  rted first:.**.*
154e5 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  *      sqlite3Bt
154e6 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29  reeCreateTable()
154e7 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
154e8 42 74 72 65 65 43 72 65 61 74 65 49 6e 64 65 78  BtreeCreateIndex
154e9 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  ().**      sqlit
154ea 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c  e3BtreeClearTabl
154eb 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
154ec 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c  te3BtreeDropTabl
154ed 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
154ee 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 29  te3BtreeInsert()
154ef 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
154f0 42 74 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a  BtreeDelete().**
154f1 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
154f2 65 65 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a  eeUpdateMeta().*
154f3 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69  *.** If an initi
154f4 61 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63  al attempt to ac
154f5 71 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66  quire the lock f
154f6 61 69 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20  ails because of 
154f7 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a  lock contention.
154f8 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62  ** and the datab
154f9 61 73 65 20 77 61 73 20 70 72 65 76 69 6f 75 73  ase was previous
154fa 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65  ly unlocked, the
154fb 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73  n invoke the bus
154fc 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20  y handler.** if 
154fd 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42  there is one.  B
154fe 75 74 20 69 66 20 74 68 65 72 65 20 77 61 73 20  ut if there was 
154ff 70 72 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61  previously a rea
15500 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a  d-lock, do not.*
15501 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73  * invoke the bus
15502 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74  y handler - just
15503 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
15504 55 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53  USY.  SQLITE_BUS
15505 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65  Y is .** returne
15506 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  d when there is 
15507 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c  already a read-l
15508 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ock in order to 
15509 61 76 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b  avoid a deadlock
1550a 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20  ..**.** Suppose 
1550b 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 70 72  there are two pr
1550c 6f 63 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e  ocesses A and B.
1550d 20 20 41 20 68 61 73 20 61 20 72 65 61 64 20 6c    A has a read l
1550e 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a  ock and B has.**
1550f 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
15510 2e 20 20 42 20 74 72 69 65 73 20 74 6f 20 70 72  .  B tries to pr
15511 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69  omote to exclusi
15512 76 65 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65  ve but is blocke
15513 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20  d because.** of 
15514 41 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20  A's read lock.  
15515 41 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f  A tries to promo
15516 74 65 20 74 6f 20 72 65 73 65 72 76 65 64 20 62  te to reserved b
15517 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79  ut is blocked by
15518 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68   B..** One or th
15519 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74  e other of the t
1551a 77 6f 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73  wo processes mus
1551b 74 20 67 69 76 65 20 77 61 79 20 6f 72 20 74 68  t give way or th
1551c 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f  ere can be.** no
1551d 20 70 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72   progress.  By r
1551e 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f  eturning SQLITE_
1551f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76  BUSY and not inv
15520 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 63  oking the busy c
15521 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20  allback.** when 
15522 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20  A already has a 
15523 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e  read lock, we en
15524 63 6f 75 72 61 67 65 20 41 20 74 6f 20 67 69 76  courage A to giv
15525 65 20 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a  e up and let B.*
15526 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51  * proceed..*/.SQ
15527 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15528 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
15529 69 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70  inTrans(Btree *p
1552a 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20  , int wrflag){. 
1552b 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b   sqlite3 *pBlock
1552c 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64   = 0;.  BtShared
1552d 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
1552e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1552f 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33  E_OK;..  sqlite3
15530 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
15531 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28   btreeIntegrity(
15532 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  p);..  /* If the
15533 20 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64   btree is alread
15534 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61  y in a write-tra
15535 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a  nsaction, or it.
15536 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20    ** is already 
15537 69 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61  in a read-transa
15538 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64  ction and a read
15539 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a  -transaction.  *
1553a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  * is requested, 
1553b 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  this is a no-op.
1553c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69  .  */.  if( p->i
1553d 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
1553e 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61  ITE || (p->inTra
1553f 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26  ns==TRANS_READ &
15540 26 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20  & !wrflag) ){.  
15541 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67    goto trans_beg
15542 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72  un;.  }..  /* Wr
15543 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  ite transactions
15544 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c   are not possibl
15545 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79  e on a read-only
15546 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
15547 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79  f( pBt->readOnly
15548 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20   && wrflag ){.  
15549 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45    rc = SQLITE_RE
1554a 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f  ADONLY;.    goto
1554b 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20   trans_begun;.  
1554c 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1554d 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1554e 43 48 45 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74  CHE.  /* If anot
1554f 68 65 72 20 64 61 74 61 62 61 73 65 20 68 61 6e  her database han
15550 64 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20  dle has already 
15551 6f 70 65 6e 65 64 20 61 20 77 72 69 74 65 20 74  opened a write t
15552 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a  ransaction .  **
15553 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d   on this shared-
15554 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  btree structure 
15555 61 6e 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69  and a second wri
15556 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te transaction i
15557 73 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64  s.  ** requested
15558 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
15559 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69  LOCKED..  */.  i
1555a 66 28 20 28 77 72 66 6c 61 67 20 26 26 20 70 42  f( (wrflag && pB
1555b 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
1555c 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c  ==TRANS_WRITE) |
1555d 7c 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67  | pBt->isPending
1555e 20 29 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d   ){.    pBlock =
1555f 20 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64   pBt->pWriter->d
15560 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77  b;.  }else if( w
15561 72 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42  rflag>1 ){.    B
15562 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20  tLock *pIter;.  
15563 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d    for(pIter=pBt-
15564 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70  >pLock; pIter; p
15565 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78  Iter=pIter->pNex
15566 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49  t){.      if( pI
15567 74 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29  ter->pBtree!=p )
15568 7b 0a 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b  {.        pBlock
15569 20 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65   = pIter->pBtree
1556a 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72  ->db;.        br
1556b 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1556c 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c   }.  }.  if( pBl
1556d 6f 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ock ){.    sqlit
1556e 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63  e3ConnectionBloc
1556f 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63  ked(p->db, pBloc
15570 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  k);.    rc = SQL
15571 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45  ITE_LOCKED_SHARE
15572 44 43 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f  DCACHE;.    goto
15573 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20   trans_begun;.  
15574 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64 6f 20 7b  }.#endif..  do {
15575 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63  .    /* Call loc
15576 6b 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65  kBtree() until e
15577 69 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65  ither pBt->pPage
15578 31 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f  1 is populated o
15579 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72  r.    ** lockBtr
1557a 65 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d  ee() returns som
1557b 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ething other tha
1557c 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63  n SQLITE_OK. loc
1557d 6b 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20  kBtree().    ** 
1557e 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54  may return SQLIT
1557f 45 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70  E_OK but leave p
15580 42 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74  Bt->pPage1 set t
15581 6f 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 20  o 0 if after.   
15582 20 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65   ** reading page
15583 20 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 20   1 it discovers 
15584 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69  that the page-si
15585 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
15586 73 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20  se .    ** file 
15587 69 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65  is not pBt->page
15588 53 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61  Size. In this ca
15589 73 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77  se lockBtree() w
1558a 69 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a  ill update.    *
1558b 2a 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  * pBt->pageSize 
1558c 74 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  to the page-size
1558d 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20   of the file on 
1558e 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  disk..    */.   
1558f 20 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61   while( pBt->pPa
15590 67 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45  ge1==0 && SQLITE
15591 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42  _OK==(rc = lockB
15592 74 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20  tree(pBt)) );.. 
15593 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
15594 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29  E_OK && wrflag )
15595 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d  {.      if( pBt-
15596 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20  >readOnly ){.   
15597 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
15598 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20  _READONLY;.     
15599 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1559a 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1559b 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67  rBegin(pBt->pPag
1559c 65 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69  er,wrflag>1,sqli
1559d 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28  te3TempInMemory(
1559e 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20  p->db));.       
1559f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
155a0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
155a1 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65  rc = newDatabase
155a2 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  (pBt);.        }
155a3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
155a4 20 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51   .    if( rc!=SQ
155a5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
155a6 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
155a7 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d  used(pBt);.    }
155a8 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53  .  }while( rc==S
155a9 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42  QLITE_BUSY && pB
155aa 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
155ab 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a  ==TRANS_NONE &&.
155ac 20 20 20 20 20 20 20 20 20 20 62 74 72 65 65 49            btreeI
155ad 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72  nvokeBusyHandler
155ae 28 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20  (pBt) );..  if( 
155af 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
155b0 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  .    if( p->inTr
155b1 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20  ans==TRANS_NONE 
155b2 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54  ){.      pBt->nT
155b3 72 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 20 20  ransaction++;.  
155b4 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61    }.    p->inTra
155b5 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 52 41  ns = (wrflag?TRA
155b6 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f 52  NS_WRITE:TRANS_R
155b7 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  EAD);.    if( p-
155b8 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e  >inTrans>pBt->in
155b9 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20  Transaction ){. 
155ba 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e       pBt->inTran
155bb 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54  saction = p->inT
155bc 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  rans;.    }.#ifn
155bd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
155be 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 20  SHARED_CACHE.   
155bf 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b 0a 20   if( wrflag ){. 
155c0 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42       assert( !pB
155c1 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 20  t->pWriter );.  
155c2 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72      pBt->pWriter
155c3 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 74 2d   = p;.      pBt-
155c4 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20 28  >isExclusive = (
155c5 75 38 29 28 77 72 66 6c 61 67 3e 31 29 3b 0a 20  u8)(wrflag>1);. 
155c6 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
155c7 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20  ..trans_begun:. 
155c8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
155c9 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a  OK && wrflag ){.
155ca 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c      /* This call
155cb 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61 74   makes sure that
155cc 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 74   the pager has t
155cd 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65  he correct numbe
155ce 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e  r of.    ** open
155cf 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 20   savepoints. If 
155d0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
155d1 65 74 65 72 20 69 73 20 67 72 65 61 74 65 72 20  eter is greater 
155d2 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a  than 0 and.    *
155d3 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  * the sub-journa
155d4 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  l is not already
155d5 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 77   open, then it w
155d6 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 65  ill be opened he
155d7 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  re..    */.    r
155d8 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
155d9 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42  OpenSavepoint(pB
155da 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 62  t->pPager, p->db
155db 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20  ->nSavepoint);. 
155dc 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67   }..  btreeInteg
155dd 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74  rity(p);.  sqlit
155de 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
155df 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
155e0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
155e1 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
155e2 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70  ./*.** Set the p
155e3 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
155e4 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64  es for all child
155e5 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61 67  ren of page pPag
155e6 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70  e. Also, if.** p
155e7 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 65  Page contains ce
155e8 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74  lls that point t
155e9 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
155ea 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74 65  , set the pointe
155eb 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73  r.** map entries
155ec 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f   for the overflo
155ed 77 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c 2e  w pages as well.
155ee 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
155ef 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 4d  etChildPtrmaps(M
155f0 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a  emPage *pPage){.
155f1 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
155f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
155f3 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20       /* Counter 
155f4 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  variable */.  in
155f5 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20  t nCell;        
155f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
155f7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65   /* Number of ce
155f8 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61 67  lls in page pPag
155f9 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
155fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
155fb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
155fc 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74  urn code */.  Bt
155fd 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50  Shared *pBt = pP
155fe 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69  age->pBt;.  u8 i
155ff 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67  sInitOrig = pPag
15600 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e  e->isInit;.  Pgn
15601 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e  o pgno = pPage->
15602 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28  pgno;..  assert(
15603 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15604 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
15605 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
15606 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69   sqlite3BtreeIni
15607 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  tPage(pPage);.  
15608 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15609 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65  K ){.    goto se
1560a 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f  t_child_ptrmaps_
1560b 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c  out;.  }.  nCell
1560c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   = pPage->nCell;
1560d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ..  for(i=0; i<n
1560e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Cell; i++){.    
1560f 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64  u8 *pCell = find
15610 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a  Cell(pPage, i);.
15611 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70  .    rc = ptrmap
15612 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65  PutOvflPtr(pPage
15613 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66  , pCell);.    if
15614 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
15615 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65  ){.      goto se
15616 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f  t_child_ptrmaps_
15617 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  out;.    }..    
15618 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
15619 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63   ){.      Pgno c
1561a 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34 62  hildPgno = get4b
1561b 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20 20 20 20  yte(pCell);.    
1561c 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
1561d 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c  (pBt, childPgno,
1561e 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70   PTRMAP_BTREE, p
1561f 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20  gno);.      if( 
15620 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
15621 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70  goto set_child_p
15622 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 20 20  trmaps_out;.    
15623 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50  }.  }..  if( !pP
15624 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
15625 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20   Pgno childPgno 
15626 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
15627 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
15628 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20  hdrOffset+8]);. 
15629 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
1562a 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f  t(pBt, childPgno
1562b 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20  , PTRMAP_BTREE, 
1562c 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f  pgno);.  }..set_
1562d 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75  child_ptrmaps_ou
1562e 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e  t:.  pPage->isIn
1562f 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b  it = isInitOrig;
15630 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
15631 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65  ./*.** Somewhere
15632 20 6f 6e 20 70 50 61 67 65 2c 20 77 68 69 63 68   on pPage, which
15633 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
15634 6f 20 62 65 20 61 20 62 74 72 65 65 20 70 61 67  o be a btree pag
15635 65 2c 20 6e 6f 74 20 61 6e 20 6f 76 65 72 66 6c  e, not an overfl
15636 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 69 73 20 61  ow.** page, is a
15637 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65   pointer to page
15638 20 69 46 72 6f 6d 2e 20 4d 6f 64 69 66 79 20 74   iFrom. Modify t
15639 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f 20 74  his pointer so t
1563a 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  hat it points to
1563b 0a 2a 2a 20 69 54 6f 2e 20 50 61 72 61 6d 65 74  .** iTo. Paramet
1563c 65 72 20 65 54 79 70 65 20 64 65 73 63 72 69 62  er eType describ
1563d 65 73 20 74 68 65 20 74 79 70 65 20 6f 66 20 70  es the type of p
1563e 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 6d 6f 64  ointer to be mod
1563f 69 66 69 65 64 2c 20 61 73 20 0a 2a 2a 20 66 6f  ified, as .** fo
15640 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52  llows:.**.** PTR
15641 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70  MAP_BTREE:     p
15642 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d  Page is a btree-
15643 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65  page. The pointe
15644 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68  r points at a ch
15645 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ild .**         
15646 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 6f            page o
15647 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  f pPage..**.** P
15648 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a  TRMAP_OVERFLOW1:
15649 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65   pPage is a btre
1564a 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e  e-page. The poin
1564b 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e  ter points at an
1564c 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20   overflow.**    
1564d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1564e 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  age pointed to b
1564f 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c  y one of the cel
15650 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a  ls on pPage..**.
15651 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  ** PTRMAP_OVERFL
15652 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e  OW2: pPage is an
15653 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20   overflow-page. 
15654 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e  The pointer poin
15655 74 73 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a  ts at the next.*
15656 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
15657 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67      overflow pag
15658 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a  e in the list..*
15659 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64  /.static int mod
1565a 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d  ifyPagePointer(M
1565b 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50  emPage *pPage, P
1565c 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20  gno iFrom, Pgno 
1565d 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a  iTo, u8 eType){.
1565e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1565f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
15660 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
15661 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
15662 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
15663 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
15664 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54  age) );.  if( eT
15665 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52  ype==PTRMAP_OVER
15666 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20  FLOW2 ){.    /* 
15667 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61  The pointer is a
15668 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20  lways the first 
15669 34 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70  4 bytes of the p
1566a 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65  age in this case
1566b 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65  .  */.    if( ge
1566c 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44  t4byte(pPage->aD
1566d 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20  ata)!=iFrom ){. 
1566e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1566f 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
15670 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62  .    }.    put4b
15671 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61  yte(pPage->aData
15672 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b  , iTo);.  }else{
15673 0a 20 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72  .    u8 isInitOr
15674 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e  ig = pPage->isIn
15675 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  it;.    int i;. 
15676 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20     int nCell;.. 
15677 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49     sqlite3BtreeI
15678 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a  nitPage(pPage);.
15679 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67      nCell = pPag
1567a 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66  e->nCell;..    f
1567b 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  or(i=0; i<nCell;
1567c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20   i++){.      u8 
1567d 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c  *pCell = findCel
1567e 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20  l(pPage, i);.   
1567f 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54     if( eType==PT
15680 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29  RMAP_OVERFLOW1 )
15681 7b 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e  {.        CellIn
15682 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20  fo info;.       
15683 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
15684 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
15685 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
15686 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f          if( info
15687 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  .iOverflow ){.  
15688 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f          if( iFro
15689 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65  m==get4byte(&pCe
1568a 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
1568b 77 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  w]) ){.         
1568c 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65     put4byte(&pCe
1568d 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
1568e 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20  w], iTo);.      
1568f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
15690 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
15691 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
15692 20 20 20 20 20 20 20 20 69 66 28 20 67 65 74 34          if( get4
15693 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72  byte(pCell)==iFr
15694 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  om ){.          
15695 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20  put4byte(pCell, 
15696 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iTo);.          
15697 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
15698 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
15699 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65   .    if( i==nCe
1569a 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
1569b 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54  eType!=PTRMAP_BT
1569c 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  REE || .        
1569d 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67    get4byte(&pPag
1569e 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
1569f 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69  hdrOffset+8])!=i
156a0 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  From ){.        
156a1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
156a2 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
156a3 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79    }.      put4by
156a4 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
156a5 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
156a6 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20  t+8], iTo);.    
156a7 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73  }..    pPage->is
156a8 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69  Init = isInitOri
156a9 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  g;.  }.  return 
156aa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
156ab 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70  *.** Move the op
156ac 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65  en database page
156ad 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61   pDbPage to loca
156ae 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69  tion iFreePage i
156af 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61  n the .** databa
156b0 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 20  se. The pDbPage 
156b1 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e  reference remain
156b2 73 20 76 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74  s valid..*/.stat
156b3 69 63 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50  ic int relocateP
156b4 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20  age(.  BtShared 
156b5 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20  *pBt,           
156b6 2f 2a 20 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65  /* Btree */.  Me
156b7 6d 50 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20  mPage *pDbPage, 
156b8 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70         /* Open p
156b9 61 67 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20  age to move */. 
156ba 20 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20   u8 eType,      
156bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
156bc 6e 74 65 72 20 6d 61 70 20 27 74 79 70 65 27 20  nter map 'type' 
156bd 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50 61 67  entry for pDbPag
156be 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72  e */.  Pgno iPtr
156bf 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  Page,           
156c0 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27  /* Pointer map '
156c1 70 61 67 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66  page-no' entry f
156c2 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20  or pDbPage */.  
156c3 50 67 6e 6f 20 69 46 72 65 65 50 61 67 65 2c 20  Pgno iFreePage, 
156c4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
156c5 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65  location to move
156c6 20 70 44 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20   pDbPage to */. 
156c7 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 0a 29 7b   int isCommit.){
156c8 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 74 72  .  MemPage *pPtr
156c9 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70  Page;   /* The p
156ca 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  age that contain
156cb 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70  s a pointer to p
156cc 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f  DbPage */.  Pgno
156cd 20 69 44 62 50 61 67 65 20 3d 20 70 44 62 50 61   iDbPage = pDbPa
156ce 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65  ge->pgno;.  Page
156cf 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d  r *pPager = pBt-
156d0 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72  >pPager;.  int r
156d1 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 54  c;..  assert( eT
156d2 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52  ype==PTRMAP_OVER
156d3 46 4c 4f 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d  FLOW2 || eType==
156d4 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31  PTRMAP_OVERFLOW1
156d5 20 7c 7c 20 0a 20 20 20 20 20 20 65 54 79 70 65   || .      eType
156d6 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c  ==PTRMAP_BTREE |
156d7 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  | eType==PTRMAP_
156d8 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 61 73  ROOTPAGE );.  as
156d9 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
156da 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
156db 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
156dc 28 20 70 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d  ( pDbPage->pBt==
156dd 70 42 74 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76  pBt );..  /* Mov
156de 65 20 70 61 67 65 20 69 44 62 50 61 67 65 20 66  e page iDbPage f
156df 72 6f 6d 20 69 74 73 20 63 75 72 72 65 6e 74 20  rom its current 
156e0 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70 61 67 65  location to page
156e1 20 6e 75 6d 62 65 72 20 69 46 72 65 65 50 61 67   number iFreePag
156e2 65 20 2a 2f 0a 20 20 54 52 41 43 45 28 28 22 41  e */.  TRACE(("A
156e3 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e  UTOVACUUM: Movin
156e4 67 20 25 64 20 74 6f 20 66 72 65 65 20 70 61 67  g %d to free pag
156e5 65 20 25 64 20 28 70 74 72 20 70 61 67 65 20 25  e %d (ptr page %
156e6 64 20 74 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a  d type %d)\n", .
156e7 20 20 20 20 20 20 69 44 62 50 61 67 65 2c 20 69        iDbPage, i
156e8 46 72 65 65 50 61 67 65 2c 20 69 50 74 72 50 61  FreePage, iPtrPa
156e9 67 65 2c 20 65 54 79 70 65 29 29 3b 0a 20 20 72  ge, eType));.  r
156ea 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
156eb 4d 6f 76 65 70 61 67 65 28 70 50 61 67 65 72 2c  Movepage(pPager,
156ec 20 70 44 62 50 61 67 65 2d 3e 70 44 62 50 61 67   pDbPage->pDbPag
156ed 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 73  e, iFreePage, is
156ee 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 66 28 20 72  Commit);.  if( r
156ef 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
156f0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
156f1 20 7d 0a 20 20 70 44 62 50 61 67 65 2d 3e 70 67   }.  pDbPage->pg
156f2 6e 6f 20 3d 20 69 46 72 65 65 50 61 67 65 3b 0a  no = iFreePage;.
156f3 0a 20 20 2f 2a 20 49 66 20 70 44 62 50 61 67 65  .  /* If pDbPage
156f4 20 77 61 73 20 61 20 62 74 72 65 65 2d 70 61 67   was a btree-pag
156f5 65 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20 68  e, then it may h
156f6 61 76 65 20 63 68 69 6c 64 20 70 61 67 65 73 20  ave child pages 
156f7 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a  and/or cells.  *
156f8 2a 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20  * that point to 
156f9 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
156fa 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
156fb 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20  entries for all 
156fc 74 68 65 73 65 0a 20 20 2a 2a 20 70 61 67 65 73  these.  ** pages
156fd 20 6e 65 65 64 20 74 6f 20 62 65 20 63 68 61 6e   need to be chan
156fe 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ged..  **.  ** I
156ff 66 20 70 44 62 50 61 67 65 20 69 73 20 61 6e 20  f pDbPage is an 
15700 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74  overflow page, t
15701 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 34 20  hen the first 4 
15702 62 79 74 65 73 20 6d 61 79 20 73 74 6f 72 65 20  bytes may store 
15703 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74  a.  ** pointer t
15704 6f 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 6f  o a subsequent o
15705 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66  verflow page. If
15706 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73   this is the cas
15707 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65  e, then.  ** the
15708 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 6e 65 65   pointer map nee
15709 64 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64  ds to be updated
1570a 20 66 6f 72 20 74 68 65 20 73 75 62 73 65 71 75   for the subsequ
1570b 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ent overflow pag
1570c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54  e..  */.  if( eT
1570d 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45  ype==PTRMAP_BTRE
1570e 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d  E || eType==PTRM
1570f 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20  AP_ROOTPAGE ){. 
15710 20 20 20 72 63 20 3d 20 73 65 74 43 68 69 6c 64     rc = setChild
15711 50 74 72 6d 61 70 73 28 70 44 62 50 61 67 65 29  Ptrmaps(pDbPage)
15712 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
15713 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
15714 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
15715 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50  }.  }else{.    P
15716 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67  gno nextOvfl = g
15717 65 74 34 62 79 74 65 28 70 44 62 50 61 67 65 2d  et4byte(pDbPage-
15718 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28  >aData);.    if(
15719 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a   nextOvfl!=0 ){.
1571a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
1571b 70 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76  pPut(pBt, nextOv
1571c 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46  fl, PTRMAP_OVERF
1571d 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 29  LOW2, iFreePage)
1571e 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
1571f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15720 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
15721 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
15722 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74 68 65 20  }..  /* Fix the 
15723 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72  database pointer
15724 20 6f 6e 20 70 61 67 65 20 69 50 74 72 50 61 67   on page iPtrPag
15725 65 20 74 68 61 74 20 70 6f 69 6e 74 65 64 20 61  e that pointed a
15726 74 20 69 44 62 50 61 67 65 20 73 6f 0a 20 20 2a  t iDbPage so.  *
15727 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73  * that it points
15728 20 61 74 20 69 46 72 65 65 50 61 67 65 2e 20 41   at iFreePage. A
15729 6c 73 6f 20 66 69 78 20 74 68 65 20 70 6f 69 6e  lso fix the poin
1572a 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f  ter map entry fo
1572b 72 0a 20 20 2a 2a 20 69 50 74 72 50 61 67 65 2e  r.  ** iPtrPage.
1572c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70  .  */.  if( eTyp
1572d 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  e!=PTRMAP_ROOTPA
1572e 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  GE ){.    rc = s
1572f 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
15730 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 67 65  ge(pBt, iPtrPage
15731 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 29 3b  , &pPtrPage, 0);
15732 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
15733 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
15734 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
15735 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
15736 33 50 61 67 65 72 57 72 69 74 65 28 70 50 74 72  3PagerWrite(pPtr
15737 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
15738 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
15739 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
1573a 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50  eleasePage(pPtrP
1573b 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75  age);.      retu
1573c 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
1573d 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 65   rc = modifyPage
1573e 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67 65  Pointer(pPtrPage
1573f 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65  , iDbPage, iFree
15740 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 20  Page, eType);.  
15741 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
15742 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  trPage);.    if(
15743 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
15744 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  {.      rc = ptr
15745 6d 61 70 50 75 74 28 70 42 74 2c 20 69 46 72 65  mapPut(pBt, iFre
15746 65 50 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50  ePage, eType, iP
15747 74 72 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  trPage);.    }. 
15748 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
15749 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  }../* Forward de
1574a 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69 72  claration requir
1574b 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 75 6d  ed by incrVacuum
1574c 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69  Step(). */.stati
1574d 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74  c int allocateBt
1574e 72 65 65 50 61 67 65 28 42 74 53 68 61 72 65 64  reePage(BtShared
1574f 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20   *, MemPage **, 
15750 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38  Pgno *, Pgno, u8
15751 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72  );../*.** Perfor
15752 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20  m a single step 
15753 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61  of an incrementa
15754 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75 63  l-vacuum. If suc
15755 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75  cessful,.** retu
15756 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66  rn SQLITE_OK. If
15757 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72   there is no wor
15758 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 65  k to do (and the
15759 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69  refore no.** poi
1575a 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68  nt in calling th
1575b 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69  is function agai
1575c 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  n), return SQLIT
1575d 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f  E_DONE..**.** Mo
1575e 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20 74  re specificly, t
1575f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  his function att
15760 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61  empts to re-orga
15761 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 74  nize the .** dat
15762 61 62 61 73 65 20 73 6f 20 74 68 61 74 20 74 68  abase so that th
15763 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74  e last page of t
15764 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c  he file currentl
15765 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e  y in use.** is n
15766 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e  o longer in use.
15767 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46  .**.** If the nF
15768 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  in parameter is 
15769 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 69 6d  non-zero, the im
1576a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73  plementation ass
1576b 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65  umes.** that the
1576c 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65   caller will kee
1576d 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61  p calling incrVa
1576e 63 75 75 6d 53 74 65 70 28 29 20 75 6e 74 69 6c  cuumStep() until
1576f 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e 73 20 53  .** it returns S
15770 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e  QLITE_DONE or an
15771 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 61 74   error, and that
15772 20 6e 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20   nFin is the.** 
15773 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
15774 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
15775 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61  e will contain a
15776 66 74 65 72 20 74 68 69 73 20 0a 2a 2a 20 70 72  fter this .** pr
15777 6f 63 65 73 73 20 69 73 20 63 6f 6d 70 6c 65 74  ocess is complet
15778 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
15779 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28   incrVacuumStep(
1577a 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
1577b 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e 6f 20 69  gno nFin, Pgno i
1577c 4c 61 73 74 50 67 29 7b 0a 20 20 50 67 6e 6f 20  LastPg){.  Pgno 
1577d 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 20 20 20  nFreeList;      
1577e 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1577f 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e  f pages still on
15780 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2a   the free-list *
15781 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  /..  assert( sql
15782 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15783 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
15784 20 61 73 73 65 72 74 28 20 69 4c 61 73 74 50 67   assert( iLastPg
15785 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 66 28 20  >nFin );..  if( 
15786 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70  !PTRMAP_ISPAGE(p
15787 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26 26 20  Bt, iLastPg) && 
15788 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e 47  iLastPg!=PENDING
15789 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
1578a 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  ){.    int rc;. 
1578b 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20     u8 eType;.   
1578c 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b 0a   Pgno iPtrPage;.
1578d 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 74 20 3d  .    nFreeList =
1578e 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e   get4byte(&pBt->
1578f 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
15790 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 46 72 65  ]);.    if( nFre
15791 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  eList==0 ){.    
15792 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15793 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  DONE;.    }..   
15794 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28   rc = ptrmapGet(
15795 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 65  pBt, iLastPg, &e
15796 54 79 70 65 2c 20 26 69 50 74 72 50 61 67 65 29  Type, &iPtrPage)
15797 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
15798 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
15799 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1579a 7d 0a 20 20 20 20 69 66 28 20 65 54 79 70 65 3d  }.    if( eType=
1579b 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45  =PTRMAP_ROOTPAGE
1579c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1579d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1579e 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  BKPT;.    }..   
1579f 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d   if( eType==PTRM
157a0 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a 20  AP_FREEPAGE ){. 
157a1 20 20 20 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30       if( nFin==0
157a2 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52   ){.        /* R
157a3 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 66  emove the page f
157a4 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 20 66 72  rom the files fr
157a5 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 69 73  ee-list. This is
157a6 20 6e 6f 74 20 72 65 71 75 69 72 65 64 0a 20 20   not required.  
157a7 20 20 20 20 20 20 2a 2a 20 69 66 20 6e 46 69 6e        ** if nFin
157a8 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 6e   is non-zero. In
157a9 20 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 20   that case, the 
157aa 66 72 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62  free-list will b
157ab 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 75  e.        ** tru
157ac 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 61  ncated to zero a
157ad 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69  fter this functi
157ae 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 6f 20 69  on returns, so i
157af 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 20 20 20  t doesn't .     
157b0 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 69 66 20     ** matter if 
157b1 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e  it still contain
157b2 73 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20 65  s some garbage e
157b3 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20 20  ntries..        
157b4 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20  */.        Pgno 
157b5 69 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20  iFreePg;.       
157b6 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50   MemPage *pFreeP
157b7 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  g;.        rc = 
157b8 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
157b9 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c  e(pBt, &pFreePg,
157ba 20 26 69 46 72 65 65 50 67 2c 20 69 4c 61 73 74   &iFreePg, iLast
157bb 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  Pg, 1);.        
157bc 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
157bd 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
157be 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
157bf 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65    }.        asse
157c0 72 74 28 20 69 46 72 65 65 50 67 3d 3d 69 4c 61  rt( iFreePg==iLa
157c1 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 20 20  stPg );.        
157c2 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65  releasePage(pFre
157c3 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ePg);.      }.  
157c4 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
157c5 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 20 20   Pgno iFreePg;  
157c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
157c7 64 65 78 20 6f 66 20 66 72 65 65 20 70 61 67 65  dex of free page
157c8 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 74 50 67   to move pLastPg
157c9 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d   to */.      Mem
157ca 50 61 67 65 20 2a 70 4c 61 73 74 50 67 3b 0a 0a  Page *pLastPg;..
157cb 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
157cc 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
157cd 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 4c  Bt, iLastPg, &pL
157ce 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 20  astPg, 0);.     
157cf 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
157d0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
157d1 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
157d2 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 46  ..      /* If nF
157d3 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73  in is zero, this
157d4 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74   loop runs exact
157d5 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65  ly once and page
157d6 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a   pLastPg.      *
157d7 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69 74  * is swapped wit
157d8 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65  h the first free
157d9 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66   page pulled off
157da 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a   the free list..
157db 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
157dc 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  * On the other h
157dd 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 20  and, if nFin is 
157de 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
157df 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 20  o, then keep.   
157e0 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e     ** looping un
157e1 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65 20  til a free-page 
157e2 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20 74  located within t
157e3 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70 61  he first nFin pa
157e4 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  ges.      ** of 
157e5 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75 6e  the file is foun
157e6 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
157e7 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 4d    do {.        M
157e8 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b  emPage *pFreePg;
157e9 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c  .        rc = al
157ea 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28  locateBtreePage(
157eb 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26  pBt, &pFreePg, &
157ec 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b 0a  iFreePg, 0, 0);.
157ed 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
157ee 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
157ef 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
157f0 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20  ge(pLastPg);.   
157f1 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
157f2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
157f3 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
157f4 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20  pFreePg);.      
157f5 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 20  }while( nFin!=0 
157f6 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e 20  && iFreePg>nFin 
157f7 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
157f8 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50 67   iFreePg<iLastPg
157f9 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20   );.      .     
157fa 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
157fb 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67 2d  erWrite(pLastPg-
157fc 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
157fd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
157fe 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
157ff 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28   = relocatePage(
15800 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54  pBt, pLastPg, eT
15801 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69  ype, iPtrPage, i
15802 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 29  FreePg, nFin!=0)
15803 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
15804 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73  releasePage(pLas
15805 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  tPg);.      if( 
15806 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
15807 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
15808 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
15809 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 69  }.  }..  if( nFi
1580a 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73  n==0 ){.    iLas
1580b 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c 65  tPg--;.    while
1580c 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 49  ( iLastPg==PENDI
1580d 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
1580e 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45  )||PTRMAP_ISPAGE
1580f 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29  (pBt, iLastPg) )
15810 7b 0a 20 20 20 20 20 20 69 66 28 20 50 54 52 4d  {.      if( PTRM
15811 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69  AP_ISPAGE(pBt, i
15812 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20 20  LastPg) ){.     
15813 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 3b     MemPage *pPg;
15814 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20  .        int rc 
15815 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
15816 74 50 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74  tPage(pBt, iLast
15817 50 67 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20  Pg, &pPg, 0);.  
15818 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
15819 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1581a 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1581b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1581c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1581d 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44  gerWrite(pPg->pD
1581e 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
1581f 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 29  releasePage(pPg)
15820 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
15821 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
15822 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
15823 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
15824 20 20 20 20 7d 0a 20 20 20 20 20 20 69 4c 61 73      }.      iLas
15825 74 50 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20  tPg--;.    }.   
15826 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75   sqlite3PagerTru
15827 6e 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e  ncateImage(pBt->
15828 70 50 61 67 65 72 2c 20 69 4c 61 73 74 50 67 29  pPager, iLastPg)
15829 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
1582a 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1582b 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73  ** A write-trans
1582c 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f  action must be o
1582d 70 65 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c  pened before cal
1582e 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69  ling this functi
1582f 6f 6e 2e 0a 2a 2a 20 49 74 20 70 65 72 66 6f 72  on..** It perfor
15830 6d 73 20 61 20 73 69 6e 67 6c 65 20 75 6e 69 74  ms a single unit
15831 20 6f 66 20 77 6f 72 6b 20 74 6f 77 61 72 64 73   of work towards
15832 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20   an incremental 
15833 76 61 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66  vacuum..**.** If
15834 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c   the incremental
15835 20 76 61 63 75 75 6d 20 69 73 20 66 69 6e 69 73   vacuum is finis
15836 68 65 64 20 61 66 74 65 72 20 74 68 69 73 20 66  hed after this f
15837 75 6e 63 74 69 6f 6e 20 68 61 73 20 72 75 6e 2c  unction has run,
15838 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  .** SQLITE_DONE 
15839 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
1583a 69 74 20 69 73 20 6e 6f 74 20 66 69 6e 69 73 68  it is not finish
1583b 65 64 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f 72  ed, but no error
1583c 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a 20 53 51   occurred,.** SQ
1583d 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
1583e 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20 61  ned. Otherwise a
1583f 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
15840 6f 64 65 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ode. .*/.SQLITE_
15841 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
15842 74 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75  te3BtreeIncrVacu
15843 75 6d 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  um(Btree *p){.  
15844 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72  int rc;.  BtShar
15845 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
15846 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
15847 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73  eEnter(p);.  ass
15848 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
15849 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
1584a 52 49 54 45 20 26 26 20 70 2d 3e 69 6e 54 72 61  RITE && p->inTra
1584b 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns==TRANS_WRITE 
1584c 29 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 61  );.  if( !pBt->a
1584d 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
1584e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e   rc = SQLITE_DON
1584f 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  E;.  }else{.    
15850 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65  invalidateAllOve
15851 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b  rflowCache(pBt);
15852 0a 20 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61  .    rc = incrVa
15853 63 75 75 6d 53 74 65 70 28 70 42 74 2c 20 30 2c  cuumStep(pBt, 0,
15854 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28   pagerPagecount(
15855 70 42 74 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  pBt));.  }.  sql
15856 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
15857 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
15858 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
15859 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
1585a 70 72 69 6f 72 20 74 6f 20 73 71 6c 69 74 65 33  prior to sqlite3
1585b 50 61 67 65 72 43 6f 6d 6d 69 74 20 77 68 65 6e  PagerCommit when
1585c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a   a transaction.*
1585d 2a 20 69 73 20 63 6f 6d 6d 69 74 65 64 20 66 6f  * is commited fo
1585e 72 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  r an auto-vacuum
1585f 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
15860 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73   If SQLITE_OK is
15861 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20   returned, then 
15862 2a 70 6e 54 72 75 6e 63 20 69 73 20 73 65 74 20  *pnTrunc is set 
15863 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
15864 20 70 61 67 65 73 0a 2a 2a 20 74 68 65 20 64 61   pages.** the da
15865 74 61 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75  tabase file shou
15866 6c 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 20  ld be truncated 
15867 74 6f 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f  to during the co
15868 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 20 0a 2a  mmit process. .*
15869 2a 20 69 2e 65 2e 20 74 68 65 20 64 61 74 61 62  * i.e. the datab
1586a 61 73 65 20 68 61 73 20 62 65 65 6e 20 72 65 6f  ase has been reo
1586b 72 67 61 6e 69 7a 65 64 20 73 6f 20 74 68 61 74  rganized so that
1586c 20 6f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20   only the first 
1586d 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20 70 61 67 65  *pnTrunc.** page
1586e 73 20 61 72 65 20 69 6e 20 75 73 65 2e 0a 2a 2f  s are in use..*/
1586f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f  .static int auto
15870 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 42 74 53  VacuumCommit(BtS
15871 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69  hared *pBt){.  i
15872 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
15873 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  K;.  Pager *pPag
15874 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72  er = pBt->pPager
15875 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e  ;.  VVA_ONLY( in
15876 74 20 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33  t nRef = sqlite3
15877 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50  PagerRefcount(pP
15878 61 67 65 72 29 20 29 3b 0a 0a 20 20 61 73 73 65  ager) );..  asse
15879 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1587a 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
1587b 78 29 20 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61  x) );.  invalida
1587c 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63  teAllOverflowCac
1587d 68 65 28 70 42 74 29 3b 0a 20 20 61 73 73 65 72  he(pBt);.  asser
1587e 74 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  t(pBt->autoVacuu
1587f 6d 29 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e  m);.  if( !pBt->
15880 69 6e 63 72 56 61 63 75 75 6d 20 29 7b 0a 20 20  incrVacuum ){.  
15881 20 20 50 67 6e 6f 20 6e 46 69 6e 3b 0a 20 20 20    Pgno nFin;.   
15882 20 50 67 6e 6f 20 6e 46 72 65 65 3b 0a 20 20 20   Pgno nFree;.   
15883 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 0a 20   Pgno nPtrmap;. 
15884 20 20 20 50 67 6e 6f 20 69 46 72 65 65 3b 0a 20     Pgno iFree;. 
15885 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 70 67 73     const int pgs
15886 7a 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  z = pBt->pageSiz
15887 65 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69  e;.    Pgno nOri
15888 67 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75  g = pagerPagecou
15889 6e 74 28 70 42 74 29 3b 0a 0a 20 20 20 20 69 66  nt(pBt);..    if
1588a 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28  ( PTRMAP_ISPAGE(
1588b 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e  pBt, nOrig) || n
1588c 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59  Orig==PENDING_BY
1588d 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a  TE_PAGE(pBt) ){.
1588e 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e        /* It is n
1588f 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63  ot possible to c
15890 72 65 61 74 65 20 61 20 64 61 74 61 62 61 73 65  reate a database
15891 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 66   for which the f
15892 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20  inal page.      
15893 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 70  ** is either a p
15894 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20  ointer-map page 
15895 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62  or the pending-b
15896 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65  yte page. If one
15897 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63  .      ** is enc
15898 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69  ountered, this i
15899 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75 70 74  ndicates corrupt
1589a 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion..      */.  
1589b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1589c 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1589d 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65      }..    nFree
1589e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74   = get4byte(&pBt
1589f 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
158a0 33 36 5d 29 3b 0a 20 20 20 20 6e 50 74 72 6d 61  36]);.    nPtrma
158a1 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67  p = (nFree-nOrig
158a2 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70  +PTRMAP_PAGENO(p
158a3 42 74 2c 20 6e 4f 72 69 67 29 2b 70 67 73 7a 2f  Bt, nOrig)+pgsz/
158a4 35 29 2f 28 70 67 73 7a 2f 35 29 3b 0a 20 20 20  5)/(pgsz/5);.   
158a5 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20   nFin = nOrig - 
158a6 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b  nFree - nPtrmap;
158a7 0a 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50  .    if( nOrig>P
158a8 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
158a9 28 70 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45  (pBt) && nFin<PE
158aa 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
158ab 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46  pBt) ){.      nF
158ac 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  in--;.    }.    
158ad 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53  while( PTRMAP_IS
158ae 50 41 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20  PAGE(pBt, nFin) 
158af 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47  || nFin==PENDING
158b0 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
158b1 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b  ){.      nFin--;
158b2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28  .    }..    for(
158b3 69 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 72  iFree=nOrig; iFr
158b4 65 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d 53  ee>nFin && rc==S
158b5 51 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 2d  QLITE_OK; iFree-
158b6 2d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69  -){.      rc = i
158b7 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42  ncrVacuumStep(pB
158b8 74 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 3b  t, nFin, iFree);
158b9 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28  .    }.    if( (
158ba 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
158bb 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  || rc==SQLITE_OK
158bc 29 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a  ) && nFree>0 ){.
158bd 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
158be 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20 3d  E_OK;.      rc =
158bf 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
158c0 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  te(pBt->pPage1->
158c1 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
158c2 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70  put4byte(&pBt->p
158c3 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
158c4 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 75 74 34  , 0);.      put4
158c5 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65  byte(&pBt->pPage
158c6 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29  1->aData[36], 0)
158c7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  ;.      sqlite3P
158c8 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67  agerTruncateImag
158c9 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e  e(pBt->pPager, n
158ca 46 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Fin);.    }.    
158cb 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
158cc 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
158cd 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
158ce 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20  pPager);.    }. 
158cf 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52   }..  assert( nR
158d0 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72  ef==sqlite3Pager
158d1 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29  Refcount(pPager)
158d2 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
158d3 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66  .}..#endif /* if
158d4 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
158d5 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a  _AUTOVACUUM */..
158d6 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
158d7 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73  ne does the firs
158d8 74 20 70 68 61 73 65 20 6f 66 20 61 20 74 77 6f  t phase of a two
158d9 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20  -phase commit.  
158da 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
158db 63 61 75 73 65 73 20 61 20 72 6f 6c 6c 62 61 63  causes a rollbac
158dc 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20  k journal to be 
158dd 63 72 65 61 74 65 64 20 28 69 66 20 69 74 20 64  created (if it d
158de 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20  oes not already 
158df 65 78 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f  exist).** and po
158e0 70 75 6c 61 74 65 64 20 77 69 74 68 20 65 6e 6f  pulated with eno
158e1 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ugh information 
158e2 73 6f 20 74 68 61 74 20 69 66 20 61 20 70 6f 77  so that if a pow
158e3 65 72 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a  er loss occurs.*
158e4 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  * the database c
158e5 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 74  an be restored t
158e6 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73  o its original s
158e7 74 61 74 65 20 62 79 20 70 6c 61 79 69 6e 67 20  tate by playing 
158e8 62 61 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72  back.** the jour
158e9 6e 61 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 63  nal.  Then the c
158ea 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6a  ontents of the j
158eb 6f 75 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 68  ournal are flush
158ec 65 64 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65  ed out to.** the
158ed 20 64 69 73 6b 2e 20 20 41 66 74 65 72 20 74 68   disk.  After th
158ee 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66  e journal is saf
158ef 65 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68  ely on oxide, th
158f0 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65  e changes to the
158f1 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 72 65  .** database are
158f2 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
158f3 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
158f4 61 6e 64 20 66 6c 75 73 68 65 64 20 74 6f 20 6f  and flushed to o
158f5 78 69 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20  xide..** At the 
158f6 65 6e 64 20 6f 66 20 74 68 69 73 20 63 61 6c 6c  end of this call
158f7 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  , the rollback j
158f8 6f 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69  ournal still exi
158f9 73 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69  sts on the.** di
158fa 73 6b 20 61 6e 64 20 77 65 20 61 72 65 20 73 74  sk and we are st
158fb 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20  ill holding all 
158fc 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 20 74 72  locks, so the tr
158fd 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f  ansaction has no
158fe 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20  t.** committed. 
158ff 20 53 65 65 20 73 71 6c 69 74 65 33 42 74 72 65   See sqlite3Btre
15900 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  eCommitPhaseTwo(
15901 29 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64  ) for the second
15902 20 70 68 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a   phase of the.**
15903 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e   commit process.
15904 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c  .**.** This call
15905 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e   is a no-op if n
15906 6f 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  o write-transact
15907 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ion is currently
15908 20 61 63 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a   active on pBt..
15909 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
1590a 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61   sync the databa
1590b 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  se file for the 
1590c 62 74 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 74  btree pBt. zMast
1590d 65 72 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20  er points to.** 
1590e 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61  the name of a ma
1590f 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
15910 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  e that should be
15911 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
15912 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20  e.** individual 
15913 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72  journal file, or
15914 20 69 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61   is NULL, indica
15915 74 69 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a  ting no master j
15916 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20  ournal file .** 
15917 28 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65  (single database
15918 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a   transaction)..*
15919 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69  *.** When this i
1591a 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61  s called, the ma
1591b 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f  ster journal sho
1591c 75 6c 64 20 61 6c 72 65 61 64 79 20 68 61 76 65  uld already have
1591d 20 62 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64   been.** created
1591e 2c 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68  , populated with
1591f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f   this journal po
15920 69 6e 74 65 72 20 61 6e 64 20 73 79 6e 63 65 64  inter and synced
15921 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20   to disk..**.** 
15922 4f 6e 63 65 20 74 68 69 73 20 69 73 20 72 6f 75  Once this is rou
15923 74 69 6e 65 20 68 61 73 20 72 65 74 75 72 6e 65  tine has returne
15924 64 2c 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e  d, the only thin
15925 67 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 6f  g required to co
15926 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 74  mmit.** the writ
15927 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f  e-transaction fo
15928 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  r this database 
15929 66 69 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 74  file is to delet
1592a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a  e the journal..*
1592b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1592c 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1592d 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  eCommitPhaseOne(
1592e 42 74 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20  Btree *p, const 
1592f 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a  char *zMaster){.
15930 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
15931 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69  E_OK;.  if( p->i
15932 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
15933 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61  ITE ){.    BtSha
15934 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
15935 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  t;.    sqlite3Bt
15936 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 23 69 66  reeEnter(p);.#if
15937 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15938 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
15939 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
1593a 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20  uum ){.      rc 
1593b 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d  = autoVacuumComm
1593c 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20 20 69  it(pBt);.      i
1593d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1593e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1593f 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
15940 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
15941 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
15942 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 63   }.#endif.    rc
15943 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43   = sqlite3PagerC
15944 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42  ommitPhaseOne(pB
15945 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61 73 74  t->pPager, zMast
15946 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  er, 0);.    sqli
15947 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
15948 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
15949 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d  c;.}../*.** Comm
1594a 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  it the transacti
1594b 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  on currently in 
1594c 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20  progress..**.** 
1594d 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
1594e 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65 63 6f  lements the seco
1594f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20 32 2d  nd phase of a 2-
15950 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54  phase commit.  T
15951 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72  he.** sqlite3Btr
15952 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
15953 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  () routine does 
15954 74 68 65 20 66 69 72 73 74 20 70 68 61 73 65 20  the first phase 
15955 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65  and should.** be
15956 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74   invoked prior t
15957 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  o calling this r
15958 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 71 6c  outine.  The sql
15959 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
1595a 68 61 73 65 4f 6e 65 28 29 0a 2a 2a 20 72 6f 75  haseOne().** rou
1595b 74 69 6e 65 20 64 69 64 20 61 6c 6c 20 74 68 65  tine did all the
1595c 20 77 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67   work of writing
1595d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74   information out
1595e 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75   to disk and flu
1595f 73 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e  shing the.** con
15960 74 65 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68  tents so that th
15961 65 79 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f  ey are written o
15962 6e 74 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61  nto the disk pla
15963 74 74 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a  tter.  All this.
15964 2a 2a 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74  ** routine has t
15965 6f 20 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f  o do is delete o
15966 72 20 74 72 75 6e 63 61 74 65 20 6f 72 20 7a 65  r truncate or ze
15967 72 6f 20 74 68 65 20 68 65 61 64 65 72 20 69 6e  ro the header in
15968 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c   the.** the roll
15969 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 28 77 68  back journal (wh
1596a 69 63 68 20 63 61 75 73 65 73 20 74 68 65 20 74  ich causes the t
1596b 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f  ransaction to co
1596c 6d 6d 69 74 29 20 61 6e 64 0a 2a 2a 20 64 72 6f  mmit) and.** dro
1596d 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54  p locks..**.** T
1596e 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65  his will release
1596f 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20   the write lock 
15970 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
15971 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a  file.  If there.
15972 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  ** are no active
15973 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73   cursors, it als
15974 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72  o releases the r
15975 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c  ead lock..*/.SQL
15976 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15977 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
15978 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65  itPhaseTwo(Btree
15979 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64   *p){.  BtShared
1597a 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
1597b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1597c 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 72 65 65  nter(p);.  btree
1597d 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20  Integrity(p);.. 
1597e 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c   /* If the handl
1597f 65 20 68 61 73 20 61 20 77 72 69 74 65 2d 74 72  e has a write-tr
15980 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20  ansaction open, 
15981 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61 72 65  commit the share
15982 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a 20 74  d-btrees .  ** t
15983 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 73  ransaction and s
15984 65 74 20 74 68 65 20 73 68 61 72 65 64 20 73 74  et the shared st
15985 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52 45 41  ate to TRANS_REA
15986 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  D..  */.  if( p-
15987 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
15988 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 6e 74  WRITE ){.    int
15989 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 74 28   rc;.    assert(
1598a 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
1598b 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
1598c 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1598d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f  pBt->nTransactio
1598e 6e 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  n>0 );.    rc = 
1598f 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
15990 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 2d 3e  itPhaseTwo(pBt->
15991 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28  pPager);.    if(
15992 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
15993 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  {.      sqlite3B
15994 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
15995 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
15996 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e     }.    pBt->in
15997 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52  Transaction = TR
15998 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20  ANS_READ;.  }.. 
15999 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c   /* If the handl
1599a 65 20 68 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f  e has any kind o
1599b 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  f transaction op
1599c 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68  en, decrement th
1599d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  e transaction.  
1599e 2a 2a 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20  ** count of the 
1599f 73 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66  shared btree. If
159a0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
159a1 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 30   count reaches 0
159a2 2c 20 73 65 74 0a 20 20 2a 2a 20 74 68 65 20 73  , set.  ** the s
159a3 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54  hared state to T
159a4 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75  RANS_NONE. The u
159a5 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
159a6 65 64 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a  ed() call below.
159a7 20 20 2a 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b    ** will unlock
159a8 20 74 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f   the pager..  */
159a9 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
159aa 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b  s!=TRANS_NONE ){
159ab 0a 20 20 20 20 63 6c 65 61 72 41 6c 6c 53 68 61  .    clearAllSha
159ac 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
159ad 6b 73 28 70 29 3b 0a 20 20 20 20 70 42 74 2d 3e  ks(p);.    pBt->
159ae 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a  nTransaction--;.
159af 20 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e      if( 0==pBt->
159b0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a  nTransaction ){.
159b1 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61        pBt->inTra
159b2 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53  nsaction = TRANS
159b3 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _NONE;.    }.  }
159b4 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 63  ..  /* Set the c
159b5 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
159b6 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e  on state to TRAN
159b7 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63  S_NONE and unloc
159b8 6b 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72  k.  ** the pager
159b9 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c   if this call cl
159ba 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65  osed the only re
159bb 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e  ad or write tran
159bc 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  saction..  */.  
159bd 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e  btreeClearHasCon
159be 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 70 2d 3e  tent(pBt);.  p->
159bf 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f  inTrans = TRANS_
159c0 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74  NONE;.  unlockBt
159c1 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
159c2 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72  ;..  btreeIntegr
159c3 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65  ity(p);.  sqlite
159c4 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
159c5 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
159c6 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20  OK;.}../*.** Do 
159c7 62 6f 74 68 20 70 68 61 73 65 73 20 6f 66 20 61  both phases of a
159c8 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49   commit..*/.SQLI
159c9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
159ca 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
159cb 74 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69  t(Btree *p){.  i
159cc 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
159cd 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
159ce 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
159cf 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
159d0 28 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  (p, 0);.  if( rc
159d1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
159d2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
159d3 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
159d4 77 6f 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  wo(p);.  }.  sql
159d5 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
159d6 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
159d7 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
159d8 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  G./*.** Return t
159d9 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 72 69  he number of wri
159da 74 65 2d 63 75 72 73 6f 72 73 20 6f 70 65 6e 20  te-cursors open 
159db 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20  on this handle. 
159dc 54 68 69 73 20 69 73 20 66 6f 72 20 75 73 65 0a  This is for use.
159dd 2a 2a 20 69 6e 20 61 73 73 65 72 74 28 29 20 65  ** in assert() e
159de 78 70 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 69  xpressions, so i
159df 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c  t is only compil
159e0 65 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20  ed if NDEBUG is 
159e1 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a  not.** defined..
159e2 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75  **.** For the pu
159e3 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72  rposes of this r
159e4 6f 75 74 69 6e 65 2c 20 61 20 77 72 69 74 65 2d  outine, a write-
159e5 63 75 72 73 6f 72 20 69 73 20 61 6e 79 20 63 75  cursor is any cu
159e6 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73 20  rsor that.** is 
159e7 63 61 70 61 62 6c 65 20 6f 66 20 77 72 69 74 69  capable of writi
159e8 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 73  ng to the databs
159e9 65 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74  e.  That means t
159ea 68 65 20 63 75 72 73 6f 72 20 77 61 73 0a 2a 2a  he cursor was.**
159eb 20 6f 72 69 67 69 6e 61 6c 6c 79 20 6f 70 65 6e   originally open
159ec 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61  ed for writing a
159ed 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 68 61  nd the cursor ha
159ee 73 20 6e 6f 74 20 62 65 20 64 69 73 61 62 6c 65  s not be disable
159ef 64 0a 2a 2a 20 62 79 20 68 61 76 69 6e 67 20 69  d.** by having i
159f0 74 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 64  ts state changed
159f1 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54   to CURSOR_FAULT
159f2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
159f3 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72  countWriteCursor
159f4 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  s(BtShared *pBt)
159f5 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  {.  BtCursor *pC
159f6 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b  ur;.  int r = 0;
159f7 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42 74 2d  .  for(pCur=pBt-
159f8 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72 3b 20  >pCursor; pCur; 
159f9 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 74  pCur=pCur->pNext
159fa 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  ){.    if( pCur-
159fb 3e 77 72 46 6c 61 67 20 26 26 20 70 43 75 72 2d  >wrFlag && pCur-
159fc 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f  >eState!=CURSOR_
159fd 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20 0a 20 20  FAULT ) r++; .  
159fe 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a  }.  return r;.}.
159ff 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
15a00 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
15a01 74 68 65 20 73 74 61 74 65 20 74 6f 20 43 55 52  the state to CUR
15a02 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64 20 74 68  SOR_FAULT and th
15a03 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20  e error.** code 
15a04 74 6f 20 65 72 72 43 6f 64 65 20 66 6f 72 20 65  to errCode for e
15a05 76 65 72 79 20 63 75 72 73 6f 72 20 6f 6e 20 42  very cursor on B
15a06 74 53 68 61 72 65 64 20 74 68 61 74 20 70 42 74  tShared that pBt
15a07 72 65 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65  ree.** reference
15a08 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 63  s..**.** Every c
15a09 75 72 73 6f 72 20 69 73 20 74 72 69 70 70 65 64  ursor is tripped
15a0a 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 72 73  , including curs
15a0b 6f 72 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 0a  ors that belong.
15a0c 2a 2a 20 74 6f 20 6f 74 68 65 72 20 64 61 74 61  ** to other data
15a0d 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
15a0e 20 74 68 61 74 20 68 61 70 70 65 6e 20 74 6f 20   that happen to 
15a0f 62 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 74 68  be sharing.** th
15a10 65 20 63 61 63 68 65 20 77 69 74 68 20 70 42 74  e cache with pBt
15a11 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ree..**.** This 
15a12 72 6f 75 74 69 6e 65 20 67 65 74 73 20 63 61 6c  routine gets cal
15a13 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62  led when a rollb
15a14 61 63 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 41  ack occurs..** A
15a15 6c 6c 20 63 75 72 73 6f 72 73 20 75 73 69 6e 67  ll cursors using
15a16 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 65 20   the same cache 
15a17 6d 75 73 74 20 62 65 20 74 72 69 70 70 65 64 0a  must be tripped.
15a18 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68  ** to prevent th
15a19 65 6d 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 74  em from trying t
15a1a 6f 20 75 73 65 20 74 68 65 20 62 74 72 65 65 20  o use the btree 
15a1b 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 72 6f 6c  after.** the rol
15a1c 6c 62 61 63 6b 2e 20 20 54 68 65 20 72 6f 6c 6c  lback.  The roll
15a1d 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65  back may have de
15a1e 6c 65 74 65 64 20 74 61 62 6c 65 73 0a 2a 2a 20  leted tables.** 
15a1f 6f 72 20 6d 6f 76 65 64 20 72 6f 6f 74 20 70 61  or moved root pa
15a20 67 65 73 2c 20 73 6f 20 69 74 20 69 73 20 6e 6f  ges, so it is no
15a21 74 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 0a  t sufficient to.
15a22 2a 2a 20 73 61 76 65 20 74 68 65 20 73 74 61 74  ** save the stat
15a23 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e  e of the cursor.
15a24 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73    The cursor mus
15a25 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61  t be.** invalida
15a26 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
15a27 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
15a28 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43  te3BtreeTripAllC
15a29 75 72 73 6f 72 73 28 42 74 72 65 65 20 2a 70 42  ursors(Btree *pB
15a2a 74 72 65 65 2c 20 69 6e 74 20 65 72 72 43 6f 64  tree, int errCod
15a2b 65 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  e){.  BtCursor *
15a2c 70 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  p;.  sqlite3Btre
15a2d 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a  eEnter(pBtree);.
15a2e 20 20 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e    for(p=pBtree->
15a2f 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b  pBt->pCursor; p;
15a30 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
15a31 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c    int i;.    sql
15a32 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75  ite3BtreeClearCu
15a33 72 73 6f 72 28 70 29 3b 0a 20 20 20 20 70 2d 3e  rsor(p);.    p->
15a34 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
15a35 46 41 55 4c 54 3b 0a 20 20 20 20 70 2d 3e 73 6b  FAULT;.    p->sk
15a36 69 70 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20  ip = errCode;.  
15a37 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d    for(i=0; i<=p-
15a38 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20  >iPage; i++){.  
15a39 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
15a3a 70 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20  p->apPage[i]);. 
15a3b 20 20 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69       p->apPage[i
15a3c 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  ] = 0;.    }.  }
15a3d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
15a3e 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a  eave(pBtree);.}.
15a3f 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20  ./*.** Rollback 
15a40 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
15a41 69 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c  in progress.  Al
15a42 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62  l cursors will b
15a43 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62  e.** invalided b
15a44 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  y this operation
15a45 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74  .  Any attempt t
15a46 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a  o use a cursor.*
15a47 2a 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20  * that was open 
15a48 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
15a49 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74 69   of this operati
15a4a 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a  on will result.*
15a4b 2a 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  * in an error..*
15a4c 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72  *.** This will r
15a4d 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65  elease the write
15a4e 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
15a4f 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20  abase file.  If 
15a50 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20  there.** are no 
15a51 61 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20  active cursors, 
15a52 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73  it also releases
15a53 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a   the read lock..
15a54 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
15a55 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
15a56 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65  eeRollback(Btree
15a57 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a   *p){.  int rc;.
15a58 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
15a59 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50  = p->pBt;.  MemP
15a5a 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20  age *pPage1;..  
15a5b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
15a5c 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76  r(p);.  rc = sav
15a5d 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c  eAllCursors(pBt,
15a5e 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20   0, 0);.#ifndef 
15a5f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15a60 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72  ED_CACHE.  if( r
15a61 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
15a62 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61      /* This is a
15a63 20 68 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74   horrible situat
15a64 69 6f 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61  ion. An IO or ma
15a65 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63  lloc() error occ
15a66 75 72 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20  urred whilst.   
15a67 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61   ** trying to sa
15a68 76 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  ve cursor positi
15a69 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73 20  ons. If this is 
15a6a 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c  an automatic rol
15a6b 6c 62 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a  lback (as.    **
15a6c 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61   the result of a
15a6d 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c   constraint, mal
15a6e 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72  loc() failure or
15a6f 20 49 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20   IO error) then 
15a70 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68  .    ** the cach
15a71 65 20 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61  e may be interna
15a72 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74  lly inconsistent
15a73 20 28 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61   (not contain va
15a74 6c 69 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20  lid trees) so.  
15a75 20 20 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73    ** we cannot s
15a76 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65  imply return the
15a77 20 65 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61   error to the ca
15a78 6c 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61  ller. Instead, a
15a79 62 6f 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c  bort .    ** all
15a7a 20 71 75 65 72 69 65 73 20 74 68 61 74 20 6d 61   queries that ma
15a7b 79 20 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f  y be using any o
15a7c 66 20 74 68 65 20 63 75 72 73 6f 72 73 20 74 68  f the cursors th
15a7d 61 74 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76  at failed to sav
15a7e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  e..    */.    sq
15a7f 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c  lite3BtreeTripAl
15a80 6c 43 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b  lCursors(p, rc);
15a81 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74  .  }.#endif.  bt
15a82 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b  reeIntegrity(p);
15a83 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61  ..  if( p->inTra
15a84 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns==TRANS_WRITE 
15a85 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a  ){.    int rc2;.
15a86 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 52 41  .    assert( TRA
15a87 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69  NS_WRITE==pBt->i
15a88 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a  nTransaction );.
15a89 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65      rc2 = sqlite
15a8a 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70  3PagerRollback(p
15a8b 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
15a8c 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45   if( rc2!=SQLITE
15a8d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
15a8e 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20  = rc2;.    }..  
15a8f 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63    /* The rollbac
15a90 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 74 72  k may have destr
15a91 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 31 2d  oyed the pPage1-
15a92 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53  >aData value.  S
15a93 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 73 71  o.    ** call sq
15a94 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
15a95 65 28 29 20 6f 6e 20 70 61 67 65 20 31 20 61 67  e() on page 1 ag
15a96 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20  ain to make.    
15a97 2a 2a 20 73 75 72 65 20 70 50 61 67 65 31 2d 3e  ** sure pPage1->
15a98 61 44 61 74 61 20 69 73 20 73 65 74 20 63 6f 72  aData is set cor
15a99 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69  rectly. */.    i
15a9a 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  f( sqlite3BtreeG
15a9b 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26  etPage(pBt, 1, &
15a9c 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49  pPage1, 0)==SQLI
15a9d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
15a9e 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
15a9f 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  1);.    }.    as
15aa0 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65  sert( countWrite
15aa1 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20  Cursors(pBt)==0 
15aa2 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72  );.    pBt->inTr
15aa3 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e  ansaction = TRAN
15aa4 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 69  S_READ;.  }..  i
15aa5 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54  f( p->inTrans!=T
15aa6 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20  RANS_NONE ){.   
15aa7 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43   clearAllSharedC
15aa8 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70  acheTableLocks(p
15aa9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
15aaa 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e  Bt->nTransaction
15aab 3e 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e  >0 );.    pBt->n
15aac 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20  Transaction--;. 
15aad 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e     if( 0==pBt->n
15aae 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20  Transaction ){. 
15aaf 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e       pBt->inTran
15ab0 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f  saction = TRANS_
15ab1 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  NONE;.    }.  }.
15ab2 0a 20 20 62 74 72 65 65 43 6c 65 61 72 48 61 73  .  btreeClearHas
15ab3 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 20  Content(pBt);.  
15ab4 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41  p->inTrans = TRA
15ab5 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f 63  NS_NONE;.  unloc
15ab6 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70  kBtreeIfUnused(p
15ab7 42 74 29 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74  Bt);..  btreeInt
15ab8 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c  egrity(p);.  sql
15ab9 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
15aba 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
15abb 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61  }../*.** Start a
15abc 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72   statement subtr
15abd 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73  ansaction. The s
15abe 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61  ubtransaction ca
15abf 6e 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a  n can be rolled.
15ac0 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64  ** back independ
15ac1 65 6e 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69  ently of the mai
15ac2 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59  n transaction. Y
15ac3 6f 75 20 6d 75 73 74 20 73 74 61 72 74 20 61 20  ou must start a 
15ac4 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20  transaction .** 
15ac5 62 65 66 6f 72 65 20 73 74 61 72 74 69 6e 67 20  before starting 
15ac6 61 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e  a subtransaction
15ac7 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63  . The subtransac
15ac8 74 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75  tion is ended au
15ac9 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20  tomatically .** 
15aca 69 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e  if the main tran
15acb 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20  saction commits 
15acc 6f 72 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a  or rolls back..*
15acd 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74 20 73  *.** Statement s
15ace 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61  ubtransactions a
15acf 72 65 20 75 73 65 64 20 61 72 6f 75 6e 64 20 69  re used around i
15ad0 6e 64 69 76 69 64 75 61 6c 20 53 51 4c 20 73 74  ndividual SQL st
15ad1 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74  atements.** that
15ad2 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77   are contained w
15ad3 69 74 68 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e  ithin a BEGIN...
15ad4 43 4f 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49  COMMIT block.  I
15ad5 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  f a constraint.*
15ad6 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  * error occurs w
15ad7 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d  ithin the statem
15ad8 65 6e 74 2c 20 74 68 65 20 65 66 66 65 63 74 20  ent, the effect 
15ad9 6f 66 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74  of that one stat
15ada 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20  ement.** can be 
15adb 72 6f 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68  rolled back with
15adc 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f  out having to ro
15add 6c 6c 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72  llback the entir
15ade 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  e transaction..*
15adf 2a 0a 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e 74  *.** A statement
15ae0 20 73 75 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e   sub-transaction
15ae1 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
15ae2 61 73 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20  as an anonymous 
15ae3 73 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a  savepoint. The.*
15ae4 2a 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  * value passed a
15ae5 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  s the second par
15ae6 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 74 6f  ameter is the to
15ae7 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 61  tal number of sa
15ae8 76 65 70 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63  vepoints,.** inc
15ae9 6c 75 64 69 6e 67 20 74 68 65 20 6e 65 77 20 61  luding the new a
15aea 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69  nonymous savepoi
15aeb 6e 74 2c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20  nt, open on the 
15aec 42 2d 54 72 65 65 2e 20 69 2e 65 2e 20 69 66 20  B-Tree. i.e. if 
15aed 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20  there.** are no 
15aee 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74  active savepoint
15aef 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73  s and no other s
15af0 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63  tatement-transac
15af1 74 69 6f 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69  tions open,.** i
15af2 53 74 61 74 65 6d 65 6e 74 20 69 73 20 31 2e 20  Statement is 1. 
15af3 54 68 69 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73  This anonymous s
15af4 61 76 65 70 6f 69 6e 74 20 63 61 6e 20 62 65 20  avepoint can be 
15af5 72 65 6c 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c  released or roll
15af6 65 64 20 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67  ed back.** using
15af7 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65   the sqlite3Btre
15af8 65 53 61 76 65 70 6f 69 6e 74 28 29 20 66 75 6e  eSavepoint() fun
15af9 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
15afa 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15afb 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74  ite3BtreeBeginSt
15afc 6d 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  mt(Btree *p, int
15afd 20 69 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20   iStatement){.  
15afe 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72  int rc;.  BtShar
15aff 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
15b00 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
15b01 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65  Enter(p);.  asse
15b02 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  rt( p->inTrans==
15b03 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
15b04 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65   assert( pBt->re
15b05 61 64 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61  adOnly==0 );.  a
15b06 73 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e  ssert( iStatemen
15b07 74 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  t>0 );.  assert(
15b08 20 69 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64   iStatement>p->d
15b09 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b  b->nSavepoint );
15b0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e  .  if( NEVER(p->
15b0b 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57  inTrans!=TRANS_W
15b0c 52 49 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61  RITE || pBt->rea
15b0d 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63  dOnly) ){.    rc
15b0e 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e   = SQLITE_INTERN
15b0f 41 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  AL;.  }else{.   
15b10 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e   assert( pBt->in
15b11 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
15b12 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20  NS_WRITE );.    
15b13 2f 2a 20 41 74 20 74 68 65 20 70 61 67 65 72 20  /* At the pager 
15b14 6c 65 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65  level, a stateme
15b15 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
15b16 73 20 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69  s a savepoint wi
15b17 74 68 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64  th.    ** an ind
15b18 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  ex greater than 
15b19 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63  all savepoints c
15b1a 72 65 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c  reated explicitl
15b1b 79 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53  y using.    ** S
15b1c 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49  QL statements. I
15b1d 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20  t is illegal to 
15b1e 6f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72  open, release or
15b1f 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20   rollback any.  
15b20 20 20 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f    ** such savepo
15b21 69 6e 74 73 20 77 68 69 6c 65 20 74 68 65 20 73  ints while the s
15b22 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
15b23 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69  tion savepoint i
15b24 73 20 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f  s active..    */
15b25 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
15b26 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f  3PagerOpenSavepo
15b27 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  int(pBt->pPager,
15b28 20 69 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20   iStatement);.  
15b29 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
15b2a 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
15b2b 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
15b2c 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
15b2d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
15b2e 74 69 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77  tion, op, is alw
15b2f 61 79 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  ays SAVEPOINT_RO
15b30 4c 4c 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56  LLBACK.** or SAV
15b31 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20  EPOINT_RELEASE. 
15b32 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69  This function ei
15b33 74 68 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72  ther releases or
15b34 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a   rolls back the.
15b35 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65  ** savepoint ide
15b36 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ntified by param
15b37 65 74 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c  eter iSavepoint,
15b38 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
15b39 65 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f  e value .** of o
15b3a 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c  p..**.** Normall
15b3b 79 2c 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73  y, iSavepoint is
15b3c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
15b3d 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20   equal to zero. 
15b3e 48 6f 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69  However, if op i
15b3f 73 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52  s.** SAVEPOINT_R
15b40 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53  OLLBACK, then iS
15b41 61 76 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73  avepoint may als
15b42 6f 20 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73  o be -1. In this
15b43 20 63 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f   case the .** co
15b44 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e  ntents of the en
15b45 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  tire transaction
15b46 20 61 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b   are rolled back
15b47 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 65 72  . This is differ
15b48 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f  ent.** from a no
15b49 72 6d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e  rmal transaction
15b4a 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f   rollback, as no
15b4b 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61   locks are relea
15b4c 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74  sed and the.** t
15b4d 72 61 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69  ransaction remai
15b4e 6e 73 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49  ns open..*/.SQLI
15b4f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
15b50 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70  qlite3BtreeSavep
15b51 6f 69 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69  oint(Btree *p, i
15b52 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65  nt op, int iSave
15b53 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63  point){.  int rc
15b54 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
15b55 69 66 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72  if( p && p->inTr
15b56 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
15b57 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64   ){.    BtShared
15b58 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
15b59 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d      assert( op==
15b5a 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
15b5b 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49  E || op==SAVEPOI
15b5c 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20  NT_ROLLBACK );. 
15b5d 20 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65     assert( iSave
15b5e 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61  point>=0 || (iSa
15b5f 76 65 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f  vepoint==-1 && o
15b60 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  p==SAVEPOINT_ROL
15b61 4c 42 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71  LBACK) );.    sq
15b62 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
15b63 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  p);.    rc = sql
15b64 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f 69  ite3PagerSavepoi
15b65 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  nt(pBt->pPager, 
15b66 6f 70 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b  op, iSavepoint);
15b67 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
15b68 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
15b69 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65  rc = newDatabase
15b6a 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20  (pBt);.    }.   
15b6b 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
15b6c 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ve(p);.  }.  ret
15b6d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
15b6e 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 75   Create a new cu
15b6f 72 73 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72  rsor for the BTr
15b70 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73  ee whose root is
15b71 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20   on the page.** 
15b72 69 54 61 62 6c 65 2e 20 20 54 68 65 20 61 63 74  iTable.  The act
15b73 20 6f 66 20 61 63 71 75 69 72 69 6e 67 20 61 20   of acquiring a 
15b74 63 75 72 73 6f 72 20 67 65 74 73 20 61 20 72 65  cursor gets a re
15b75 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 74  ad lock on .** t
15b76 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15b77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 6c 61  ..**.** If wrFla
15b78 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63  g==0, then the c
15b79 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62  ursor can only b
15b7a 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69  e used for readi
15b7b 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67  ng..** If wrFlag
15b7c 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75  ==1, then the cu
15b7d 72 73 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64  rsor can be used
15b7e 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20   for reading or 
15b7f 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69  for.** writing i
15b80 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f  f other conditio
15b81 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61  ns for writing a
15b82 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68  re also met.  Th
15b83 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 63  ese.** are the c
15b84 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d  onditions that m
15b85 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72  ust be met in or
15b86 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20  der for writing 
15b87 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 65 64  to.** be allowed
15b88 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20  :.**.** 1:  The 
15b89 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65  cursor must have
15b8a 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74   been opened wit
15b8b 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a  h wrFlag==1.**.*
15b8c 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61  * 2:  Other data
15b8d 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
15b8e 20 74 68 61 74 20 73 68 61 72 65 20 74 68 65 20   that share the 
15b8f 73 61 6d 65 20 70 61 67 65 72 20 63 61 63 68 65  same pager cache
15b90 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68 69 63  .**     but whic
15b91 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65  h are not in the
15b92 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45   READ_UNCOMMITTE
15b93 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20  D state may not 
15b94 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73  have.**     curs
15b95 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72  ors open with wr
15b96 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73  Flag==0 on the s
15b97 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65  ame table.  Othe
15b98 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65  rwise.**     the
15b99 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79   changes made by
15b9a 20 74 68 69 73 20 77 72 69 74 65 20 63 75 72 73   this write curs
15b9b 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69  or would be visi
15b9c 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68  ble to.**     th
15b9d 65 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 69  e read cursors i
15b9e 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61 74 61  n the other data
15b9f 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
15ba0 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64  .**.** 3:  The d
15ba1 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20  atabase must be 
15ba2 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e  writable (not on
15ba3 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61   read-only media
15ba4 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72  ).**.** 4:  Ther
15ba5 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74  e must be an act
15ba6 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ive transaction.
15ba7 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69  .**.** No checki
15ba8 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61  ng is done to ma
15ba9 6b 65 20 73 75 72 65 20 74 68 61 74 20 70 61 67  ke sure that pag
15baa 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20  e iTable really 
15bab 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70  is the.** root p
15bac 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65 65 2e  age of a b-tree.
15bad 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20    If it is not, 
15bae 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20  then the cursor 
15baf 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 69 6c 6c  acquired.** will
15bb0 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63   not work correc
15bb1 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  tly..**.** It is
15bb2 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
15bb3 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  e sqlite3BtreeCu
15bb4 72 73 6f 72 53 69 7a 65 28 29 20 62 79 74 65 73  rsorSize() bytes
15bb5 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70   of memory .** p
15bb6 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 43 75  ointed to by pCu
15bb7 72 20 68 61 76 65 20 62 65 65 6e 20 7a 65 72 6f  r have been zero
15bb8 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
15bb9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
15bba 62 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42  btreeCursor(.  B
15bbb 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20  tree *p,        
15bbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15bbd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72        /* The btr
15bbe 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  ee */.  int iTab
15bbf 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  le,             
15bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15bc1 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  * Root page of t
15bc2 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  able to open */.
15bc3 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20    int wrFlag,   
15bc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15bc5 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f           /* 1 to
15bc6 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f   write. 0 read-o
15bc7 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  nly */.  struct 
15bc8 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
15bc9 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o,              
15bca 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20  /* First arg to 
15bcb 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
15bcc 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f  ion */.  BtCurso
15bcd 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 20  r *pCur         
15bce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15bcf 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77  /* Space for new
15bd0 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20   cursor */.){.  
15bd1 69 6e 74 20 72 63 3b 0a 20 20 50 67 6e 6f 20 6e  int rc;.  Pgno n
15bd2 50 61 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64  Page;.  BtShared
15bd3 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
15bd4 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15bd5 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
15bd6 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74  x(p) );.  assert
15bd7 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 77  ( wrFlag==0 || w
15bd8 72 46 6c 61 67 3d 3d 31 20 29 3b 0a 20 20 69 66  rFlag==1 );.  if
15bd9 28 20 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20  ( wrFlag ){.    
15bda 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65  assert( !pBt->re
15bdb 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 20 20 69 66  adOnly );.    if
15bdc 28 20 4e 45 56 45 52 28 70 42 74 2d 3e 72 65 61  ( NEVER(pBt->rea
15bdd 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20  dOnly) ){.      
15bde 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45  return SQLITE_RE
15bdf 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20  ADONLY;.    }.  
15be0 20 20 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52    rc = checkForR
15be1 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20  eadConflicts(p, 
15be2 69 54 61 62 6c 65 2c 20 30 2c 20 30 29 3b 0a 20  iTable, 0, 0);. 
15be3 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
15be4 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73  E_OK ){.      as
15be5 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
15be6 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41  _LOCKED_SHAREDCA
15be7 43 48 45 20 29 3b 0a 20 20 20 20 20 20 72 65 74  CHE );.      ret
15be8 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
15be9 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 50  }..  if( pBt->pP
15bea 61 67 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72  age1==0 ){.    r
15beb 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 57 69 74  c = lockBtreeWit
15bec 68 52 65 74 72 79 28 70 29 3b 0a 20 20 20 20 69  hRetry(p);.    i
15bed 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
15bee 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
15bef 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   rc;.    }.  }. 
15bf0 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20   pCur->pgnoRoot 
15bf1 3d 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a  = (Pgno)iTable;.
15bf2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
15bf3 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
15bf4 2d 3e 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a  ->pPager, (int *
15bf5 29 26 6e 50 61 67 65 29 3b 20 0a 20 20 69 66 28  )&nPage); .  if(
15bf6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
15bf7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
15bf8 0a 20 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c  .  }.  if( iTabl
15bf9 65 3d 3d 31 20 26 26 20 6e 50 61 67 65 3d 3d 30  e==1 && nPage==0
15bfa 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
15bfb 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20 20 20 67  ITE_EMPTY;.    g
15bfc 6f 74 6f 20 63 72 65 61 74 65 5f 63 75 72 73 6f  oto create_curso
15bfd 72 5f 65 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d  r_exception;.  }
15bfe 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e  .  rc = getAndIn
15bff 69 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72  itPage(pBt, pCur
15c00 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75  ->pgnoRoot, &pCu
15c01 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20  r->apPage[0]);. 
15c02 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
15c03 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 63  OK ){.    goto c
15c04 72 65 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63  reate_cursor_exc
15c05 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  eption;.  }..  /
15c06 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74  * Now that no ot
15c07 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f  her errors can o
15c08 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c  ccur, finish fil
15c09 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75  ling in the BtCu
15c0a 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62  rsor.  ** variab
15c0b 6c 65 73 2c 20 6c 69 6e 6b 20 74 68 65 20 63 75  les, link the cu
15c0c 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 74  rsor into the Bt
15c0d 53 68 61 72 65 64 20 6c 69 73 74 20 61 6e 64 20  Shared list and 
15c0e 73 65 74 20 2a 70 70 43 75 72 20 28 74 68 65 0a  set *ppCur (the.
15c0f 20 20 2a 2a 20 6f 75 74 70 75 74 20 61 72 67 75    ** output argu
15c10 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
15c11 63 74 69 6f 6e 29 2e 0a 20 20 2a 2f 0a 20 20 70  ction)..  */.  p
15c12 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20  Cur->pKeyInfo = 
15c13 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72  pKeyInfo;.  pCur
15c14 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20  ->pBtree = p;.  
15c15 70 43 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b  pCur->pBt = pBt;
15c16 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20  .  pCur->wrFlag 
15c17 3d 20 28 75 38 29 77 72 46 6c 61 67 3b 0a 20 20  = (u8)wrFlag;.  
15c18 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42  pCur->pNext = pB
15c19 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66  t->pCursor;.  if
15c1a 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b  ( pCur->pNext ){
15c1b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74  .    pCur->pNext
15c1c 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 72 3b 0a  ->pPrev = pCur;.
15c1d 20 20 7d 0a 20 20 70 42 74 2d 3e 70 43 75 72 73    }.  pBt->pCurs
15c1e 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20 70 43 75  or = pCur;.  pCu
15c1f 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
15c20 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43  OR_INVALID;.  pC
15c21 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20  ur->cachedRowid 
15c22 3d 20 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  = 0;..  return S
15c23 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 63 72 65 61 74  QLITE_OK;..creat
15c24 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70 74 69  e_cursor_excepti
15c25 6f 6e 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67  on:.  releasePag
15c26 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  e(pCur->apPage[0
15c27 5d 29 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65  ]);.  unlockBtre
15c28 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
15c29 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
15c2a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
15c2b 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
15c2c 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70  rsor(.  Btree *p
15c2d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
15c2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c2f 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65      /* The btree
15c30 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
15c31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
15c32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c33 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
15c34 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  f table to open 
15c35 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c  */.  int wrFlag,
15c36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c38 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20   /* 1 to write. 
15c39 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20  0 read-only */. 
15c3a 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20   struct KeyInfo 
15c3b 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20  *pKeyInfo,      
15c3c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15c3d 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f  First arg to xCo
15c3e 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43  mpare() */.  BtC
15c3f 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20  ursor *pCur     
15c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c41 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
15c42 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72  e new cursor her
15c43 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
15c44 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
15c45 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d  Enter(p);.  rc =
15c46 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20   btreeCursor(p, 
15c47 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20  iTable, wrFlag, 
15c48 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b  pKeyInfo, pCur);
15c49 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
15c4a 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
15c4b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
15c4c 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f  eturn the size o
15c4d 66 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a  f a BtCursor obj
15c4e 65 63 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a  ect in bytes..**
15c4f 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
15c50 63 65 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f  ces is needed so
15c51 20 74 68 61 74 20 75 73 65 72 73 20 6f 66 20 63   that users of c
15c52 75 72 73 6f 72 73 20 63 61 6e 20 70 72 65 61 6c  ursors can preal
15c53 6c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 63  locate.** suffic
15c54 69 65 6e 74 20 73 74 6f 72 61 67 65 20 74 6f 20  ient storage to 
15c55 68 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 20  hold a cursor.  
15c56 54 68 65 20 42 74 43 75 72 73 6f 72 20 6f 62 6a  The BtCursor obj
15c57 65 63 74 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a  ect is opaque.**
15c58 20 74 6f 20 75 73 65 72 73 20 73 6f 20 74 68 65   to users so the
15c59 79 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20  y cannot do the 
15c5a 73 69 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 6c  sizeof() themsel
15c5b 76 65 73 20 2d 20 74 68 65 79 20 6d 75 73 74 20  ves - they must 
15c5c 63 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75  call.** this rou
15c5d 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tine..*/.SQLITE_
15c5e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
15c5f 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69  te3BtreeCursorSi
15c60 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75  ze(void){.  retu
15c61 72 6e 20 73 69 7a 65 6f 66 28 42 74 43 75 72 73  rn sizeof(BtCurs
15c62 6f 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  or);.}../*.** Se
15c63 74 20 74 68 65 20 63 61 63 68 65 64 20 72 6f 77  t the cached row
15c64 69 64 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72  id value of ever
15c65 79 20 63 75 72 73 6f 72 20 69 6e 20 74 68 65 20  y cursor in the 
15c66 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69  same database fi
15c67 6c 65 0a 2a 2a 20 61 73 20 70 43 75 72 20 61 6e  le.** as pCur an
15c68 64 20 68 61 76 69 6e 67 20 74 68 65 20 73 61 6d  d having the sam
15c69 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  e root page numb
15c6a 65 72 20 61 73 20 70 43 75 72 2e 20 20 54 68 65  er as pCur.  The
15c6b 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74   value is.** set
15c6c 20 74 6f 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a   to iRowid..**.*
15c6d 2a 20 4f 6e 6c 79 20 70 6f 73 69 74 69 76 65 20  * Only positive 
15c6e 72 6f 77 69 64 20 76 61 6c 75 65 73 20 61 72 65  rowid values are
15c6f 20 63 6f 6e 73 69 64 65 72 65 64 20 76 61 6c 69   considered vali
15c70 64 20 66 6f 72 20 74 68 69 73 20 63 61 63 68 65  d for this cache
15c71 2e 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69  ..** The cache i
15c72 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  s initialized to
15c73 20 7a 65 72 6f 2c 20 69 6e 64 69 63 61 74 69 6e   zero, indicatin
15c74 67 20 61 6e 20 69 6e 76 61 6c 69 64 20 63 61 63  g an invalid cac
15c75 68 65 2e 0a 2a 2a 20 41 20 62 74 72 65 65 20 77  he..** A btree w
15c76 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 20 77 69  ill work fine wi
15c77 74 68 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74  th zero or negat
15c78 69 76 65 20 72 6f 77 69 64 73 2e 20 20 57 65 20  ive rowids.  We 
15c79 6a 75 73 74 20 63 61 6e 6e 6f 74 0a 2a 2a 20 63  just cannot.** c
15c7a 61 63 68 65 20 7a 65 72 6f 20 6f 72 20 6e 65 67  ache zero or neg
15c7b 61 74 69 76 65 20 72 6f 77 69 64 73 2c 20 77 68  ative rowids, wh
15c7c 69 63 68 20 6d 65 61 6e 73 20 74 61 62 6c 65 73  ich means tables
15c7d 20 74 68 61 74 20 75 73 65 20 7a 65 72 6f 20 6f   that use zero o
15c7e 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 72 6f  r.** negative ro
15c7f 77 69 64 73 20 6d 69 67 68 74 20 72 75 6e 20 61  wids might run a
15c80 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20   little slower. 
15c81 20 42 75 74 20 69 6e 20 70 72 61 63 74 69 63 65   But in practice
15c82 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 6e 65 67  , zero.** or neg
15c83 61 74 69 76 65 20 72 6f 77 69 64 73 20 61 72 65  ative rowids are
15c84 20 76 65 72 79 20 75 6e 63 6f 6d 6d 6f 6e 20 73   very uncommon s
15c85 6f 20 74 68 69 73 20 73 68 6f 75 6c 64 20 6e 6f  o this should no
15c86 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a  t be a problem..
15c87 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
15c88 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
15c89 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69  reeSetCachedRowi
15c8a 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  d(BtCursor *pCur
15c8b 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
15c8c 69 52 6f 77 69 64 29 7b 0a 20 20 42 74 43 75 72  iRowid){.  BtCur
15c8d 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d  sor *p;.  for(p=
15c8e 70 43 75 72 2d 3e 70 42 74 2d 3e 70 43 75 72 73  pCur->pBt->pCurs
15c8f 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  or; p; p=p->pNex
15c90 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70  t){.    if( p->p
15c91 67 6e 6f 52 6f 6f 74 3d 3d 70 43 75 72 2d 3e 70  gnoRoot==pCur->p
15c92 67 6e 6f 52 6f 6f 74 20 29 20 70 2d 3e 63 61 63  gnoRoot ) p->cac
15c93 68 65 64 52 6f 77 69 64 20 3d 20 69 52 6f 77 69  hedRowid = iRowi
15c94 64 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  d;.  }.  assert(
15c95 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77   pCur->cachedRow
15c96 69 64 3d 3d 69 52 6f 77 69 64 20 29 3b 0a 7d 0a  id==iRowid );.}.
15c97 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
15c98 65 20 63 61 63 68 65 64 20 72 6f 77 69 64 20 66  e cached rowid f
15c99 6f 72 20 74 68 65 20 67 69 76 65 6e 20 63 75 72  or the given cur
15c9a 73 6f 72 2e 20 20 41 20 6e 65 67 61 74 69 76 65  sor.  A negative
15c9b 20 6f 72 20 7a 65 72 6f 0a 2a 2a 20 72 65 74 75   or zero.** retu
15c9c 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74  rn value indicat
15c9d 65 73 20 74 68 61 74 20 74 68 65 20 72 6f 77 69  es that the rowi
15c9e 64 20 63 61 63 68 65 20 69 73 20 69 6e 76 61 6c  d cache is inval
15c9f 69 64 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65  id and should be
15ca0 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 49 66  .** ignored.  If
15ca1 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65   the rowid cache
15ca2 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f 72   has never befor
15ca3 65 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e  e been set, then
15ca4 20 61 0a 2a 2a 20 7a 65 72 6f 20 69 73 20 72 65   a.** zero is re
15ca5 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
15ca6 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
15ca7 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 42  3_int64 sqlite3B
15ca8 74 72 65 65 47 65 74 43 61 63 68 65 64 52 6f 77  treeGetCachedRow
15ca9 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  id(BtCursor *pCu
15caa 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75  r){.  return pCu
15cab 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 3b 0a  r->cachedRowid;.
15cac 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
15cad 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 72 65   cursor.  The re
15cae 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ad lock on the d
15caf 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
15cb0 72 65 6c 65 61 73 65 64 0a 2a 2a 20 77 68 65 6e  released.** when
15cb1 20 74 68 65 20 6c 61 73 74 20 63 75 72 73 6f 72   the last cursor
15cb2 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 53   is closed..*/.S
15cb3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
15cb4 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  t sqlite3BtreeCl
15cb5 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75 72 73  oseCursor(BtCurs
15cb6 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 42 74 72  or *pCur){.  Btr
15cb7 65 65 20 2a 70 42 74 72 65 65 20 3d 20 70 43 75  ee *pBtree = pCu
15cb8 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 69 66 28  r->pBtree;.  if(
15cb9 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20 20 69   pBtree ){.    i
15cba 6e 74 20 69 3b 0a 20 20 20 20 42 74 53 68 61 72  nt i;.    BtShar
15cbb 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e  ed *pBt = pCur->
15cbc 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  pBt;.    sqlite3
15cbd 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65  BtreeEnter(pBtre
15cbe 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  e);.    sqlite3B
15cbf 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28  treeClearCursor(
15cc0 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28 20 70  pCur);.    if( p
15cc1 43 75 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20  Cur->pPrev ){.  
15cc2 20 20 20 20 70 43 75 72 2d 3e 70 50 72 65 76 2d      pCur->pPrev-
15cc3 3e 70 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e 70  >pNext = pCur->p
15cc4 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Next;.    }else{
15cc5 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 43 75 72  .      pBt->pCur
15cc6 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78  sor = pCur->pNex
15cc7 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
15cc8 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a   pCur->pNext ){.
15cc9 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78        pCur->pNex
15cca 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 72 2d  t->pPrev = pCur-
15ccb 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20  >pPrev;.    }.  
15ccc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43    for(i=0; i<=pC
15ccd 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b  ur->iPage; i++){
15cce 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
15ccf 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  ge(pCur->apPage[
15cd0 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75  i]);.    }.    u
15cd1 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
15cd2 65 64 28 70 42 74 29 3b 0a 20 20 20 20 69 6e 76  ed(pBt);.    inv
15cd3 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43  alidateOverflowC
15cd4 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 20 20  ache(pCur);.    
15cd5 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  /* sqlite3_free(
15cd6 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20 20 73 71  pCur); */.    sq
15cd7 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
15cd8 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72  pBtree);.  }.  r
15cd9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
15cda 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61  .}../*.** Make a
15cdb 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f   temporary curso
15cdc 72 20 62 79 20 66 69 6c 6c 69 6e 67 20 69 6e 20  r by filling in 
15cdd 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 70 54  the fields of pT
15cde 65 6d 70 43 75 72 2e 0a 2a 2a 20 54 68 65 20 74  empCur..** The t
15cdf 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20  emporary cursor 
15ce0 69 73 20 6e 6f 74 20 6f 6e 20 74 68 65 20 63 75  is not on the cu
15ce1 72 73 6f 72 20 6c 69 73 74 20 66 6f 72 20 74 68  rsor list for th
15ce2 65 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49  e Btree..*/.SQLI
15ce3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
15ce4 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 54  sqlite3BtreeGetT
15ce5 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 73  empCursor(BtCurs
15ce6 6f 72 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73  or *pCur, BtCurs
15ce7 6f 72 20 2a 70 54 65 6d 70 43 75 72 29 7b 0a 20  or *pTempCur){. 
15ce8 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
15ce9 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
15cea 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 6d 65  ex(pCur) );.  me
15ceb 6d 63 70 79 28 70 54 65 6d 70 43 75 72 2c 20 70  mcpy(pTempCur, p
15cec 43 75 72 2c 20 73 69 7a 65 6f 66 28 42 74 43 75  Cur, sizeof(BtCu
15ced 72 73 6f 72 29 29 3b 0a 20 20 70 54 65 6d 70 43  rsor));.  pTempC
15cee 75 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20  ur->pNext = 0;. 
15cef 20 70 54 65 6d 70 43 75 72 2d 3e 70 50 72 65 76   pTempCur->pPrev
15cf0 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b   = 0;.  for(i=0;
15cf1 20 69 3c 3d 70 54 65 6d 70 43 75 72 2d 3e 69 50   i<=pTempCur->iP
15cf2 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  age; i++){.    s
15cf3 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70  qlite3PagerRef(p
15cf4 54 65 6d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  TempCur->apPage[
15cf5 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  i]->pDbPage);.  
15cf6 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 65 6d  }.  assert( pTem
15cf7 70 43 75 72 2d 3e 70 4b 65 79 3d 3d 30 20 29 3b  pCur->pKey==0 );
15cf8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
15cf9 20 61 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72   a temporary cur
15cfa 73 6f 72 20 73 75 63 68 20 61 73 20 77 61 73 20  sor such as was 
15cfb 6d 61 64 65 20 62 79 20 74 68 65 20 43 72 65 61  made by the Crea
15cfc 74 65 54 65 6d 70 6f 72 61 72 79 43 75 72 73 6f  teTemporaryCurso
15cfd 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  r().** function 
15cfe 61 62 6f 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  above..*/.SQLITE
15cff 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
15d00 6c 69 74 65 33 42 74 72 65 65 52 65 6c 65 61 73  lite3BtreeReleas
15d01 65 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75  eTempCursor(BtCu
15d02 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69  rsor *pCur){.  i
15d03 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
15d04 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
15d05 28 70 43 75 72 29 20 29 3b 0a 20 20 66 6f 72 28  (pCur) );.  for(
15d06 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50  i=0; i<=pCur->iP
15d07 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  age; i++){.    s
15d08 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
15d09 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d  (pCur->apPage[i]
15d0a 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a  ->pDbPage);.  }.
15d0b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
15d0c 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 7d 0a 0a 0a  Cur->pKey);.}...
15d0d 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
15d0e 20 74 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67   the BtCursor* g
15d0f 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75  iven in the argu
15d10 6d 65 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64  ment has a valid
15d11 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66  .** BtCursor.inf
15d12 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66  o structure.  If
15d13 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61   it is not alrea
15d14 64 79 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a  dy valid, call.*
15d15 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  * sqlite3BtreePa
15d16 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c  rseCell() to fil
15d17 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42  l it in..**.** B
15d18 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20  tCursor.info is 
15d19 61 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 69  a cache of the i
15d1a 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68  nformation in th
15d1b 65 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a  e current cell..
15d1c 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 20 63 61  ** Using this ca
15d1d 63 68 65 20 72 65 64 75 63 65 73 20 74 68 65 20  che reduces the 
15d1e 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20  number of calls 
15d1f 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  to sqlite3BtreeP
15d20 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a  arseCell()..**.*
15d21 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54  * 2007-06-25:  T
15d22 68 65 72 65 20 69 73 20 61 20 62 75 67 20 69 6e  here is a bug in
15d23 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f   some versions o
15d24 66 20 4d 53 56 43 20 74 68 61 74 20 63 61 75 73  f MSVC that caus
15d25 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65  e the.** compile
15d26 72 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20  r to crash when 
15d27 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73  getCellInfo() is
15d28 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
15d29 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20  a macro..** But 
15d2a 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 73 75  there is a measu
15d2b 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 64 76  reable speed adv
15d2c 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20  antage to using 
15d2d 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63  the macro on gcc
15d2e 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63  .** (when less c
15d2f 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61  ompiler optimiza
15d30 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f  tions like -Os o
15d31 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61  r -O0 are used a
15d32 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c  nd the.** compil
15d33 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20  er is not doing 
15d34 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69  agressive inlini
15d35 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20  ng.)  So we use 
15d36 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a  a real function.
15d37 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20  ** for MSVC and 
15d38 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72  a macro for ever
15d39 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69  ything else.  Ti
15d3a 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23  cket #2457..*/.#
15d3b 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
15d3c 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65  static void asse
15d3d 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72  rtCellInfo(BtCur
15d3e 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20  sor *pCur){.    
15d3f 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
15d40 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70     int iPage = p
15d41 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20  Cur->iPage;.    
15d42 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c  memset(&info, 0,
15d43 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a   sizeof(info));.
15d44 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
15d45 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e  ParseCell(pCur->
15d46 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70  apPage[iPage], p
15d47 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65  Cur->aiIdx[iPage
15d48 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61  ], &info);.    a
15d49 73 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69  ssert( memcmp(&i
15d4a 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f  nfo, &pCur->info
15d4b 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d  , sizeof(info))=
15d4c 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  =0 );.  }.#else.
15d4d 20 20 23 64 65 66 69 6e 65 20 61 73 73 65 72 74    #define assert
15d4e 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64  CellInfo(x).#end
15d4f 69 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56  if.#ifdef _MSC_V
15d50 45 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65  ER.  /* Use a re
15d51 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d  al function in M
15d52 53 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75  SVC to work arou
15d53 6e 64 20 62 75 67 73 20 69 6e 20 74 68 61 74 20  nd bugs in that 
15d54 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73  compiler. */.  s
15d55 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 65  tatic void getCe
15d56 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20  llInfo(BtCursor 
15d57 2a 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20  *pCur){.    if( 
15d58 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
15d59 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ==0 ){.      int
15d5a 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69   iPage = pCur->i
15d5b 50 61 67 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  Page;.      sqli
15d5c 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
15d5d 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  l(pCur->apPage[i
15d5e 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64  Page],pCur->aiId
15d5f 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e  x[iPage],&pCur->
15d60 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75  info);.      pCu
15d61 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31  r->validNKey = 1
15d62 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
15d63 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66     assertCellInf
15d64 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20  o(pCur);.    }. 
15d65 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e   }.#else /* if n
15d66 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20  ot _MSC_VER */. 
15d67 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20   /* Use a macro 
15d68 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d  in all other com
15d69 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74  pilers so that t
15d6a 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  he function is i
15d6b 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e  nlined */.#defin
15d6c 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43  e getCellInfo(pC
15d6d 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20  ur)             
15d6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d70 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28           \.  if(
15d71 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
15d72 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20  e==0 ){         
15d73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d75 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
15d76 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72  int iPage = pCur
15d77 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20  ->iPage;        
15d78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d7a 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
15d7b 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
15d7c 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50  seCell(pCur->apP
15d7d 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d  age[iPage],pCur-
15d7e 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70  >aiIdx[iPage],&p
15d7f 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 20  Cur->info); \.  
15d80 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
15d81 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20  y = 1;          
15d82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d84 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
15d85 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20   }else{         
15d86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
15d8a 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e      assertCellIn
15d8b 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20 20  fo(pCur);       
15d8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
15d8f 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f  .  }.#endif /* _
15d90 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a  MSC_VER */../*.*
15d91 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20  * Set *pSize to 
15d92 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
15d93 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 74 6f  buffer needed to
15d94 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 20   hold the value 
15d95 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f  of.** the key fo
15d96 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 65 6e  r the current en
15d97 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 75 72  try.  If the cur
15d98 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74  sor is not point
15d99 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69  ing.** to a vali
15d9a 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20  d entry, *pSize 
15d9b 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a  is set to 0. .**
15d9c 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 20  .** For a table 
15d9d 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 59 20  with the INTKEY 
15d9e 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 20 72  flag set, this r
15d9f 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
15da0 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66  he key.** itself
15da1 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72  , not the number
15da2 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
15da3 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   key..*/.SQLITE_
15da4 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
15da5 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
15da6 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
15da7 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69  i64 *pSize){.  i
15da8 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
15da9 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
15daa 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
15dab 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
15dac 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
15dad 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
15dae 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
15daf 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
15db0 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
15db1 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  || pCur->eState=
15db2 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
15db3 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65  .    if( pCur->e
15db4 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
15db5 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2a  VALID ){.      *
15db6 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d  pSize = 0;.    }
15db7 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 74 43  else{.      getC
15db8 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20  ellInfo(pCur);. 
15db9 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 43       *pSize = pC
15dba 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20  ur->info.nKey;. 
15dbb 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
15dbc 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
15dbd 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65  et *pSize to the
15dbe 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
15dbf 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68 65 20   of data in the 
15dc0 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 72  entry the.** cur
15dc1 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  sor currently po
15dc2 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77 61 79 73  ints to.  Always
15dc3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
15dc4 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73  K..** Failure is
15dc5 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20   not possible.  
15dc6 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  If the cursor is
15dc7 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 0a 2a   not currently.*
15dc8 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e  * pointing to an
15dc9 20 65 6e 74 72 79 20 28 77 68 69 63 68 20 63 61   entry (which ca
15dca 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 20 65 78  n happen, for ex
15dcb 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 74 68 65  ample, if.** the
15dcc 20 64 61 74 61 62 61 73 65 20 69 73 20 65 6d 70   database is emp
15dcd 74 79 29 20 74 68 65 6e 20 2a 70 53 69 7a 65 20  ty) then *pSize 
15dce 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a  is set to 0..*/.
15dcf 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15dd0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
15dd1 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72  ataSize(BtCursor
15dd2 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69   *pCur, u32 *pSi
15dd3 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  ze){.  int rc;..
15dd4 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
15dd5 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
15dd6 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f   );.  rc = resto
15dd7 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
15dd8 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
15dd9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
15dda 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
15ddb 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
15ddc 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d  INVALID || pCur-
15ddd 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
15dde 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 28  VALID );.    if(
15ddf 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
15de0 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
15de1 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 70 6f  .      /* Not po
15de2 69 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69  inting at a vali
15de3 64 20 65 6e 74 72 79 20 2d 20 73 65 74 20 2a 70  d entry - set *p
15de4 53 69 7a 65 20 74 6f 20 30 2e 20 2a 2f 0a 20 20  Size to 0. */.  
15de5 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a      *pSize = 0;.
15de6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
15de7 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75   getCellInfo(pCu
15de8 72 29 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65  r);.      *pSize
15de9 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44   = pCur->info.nD
15dea 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ata;.    }.  }. 
15deb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
15dec 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70  *.** Given the p
15ded 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e  age number of an
15dee 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
15def 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28  n the database (
15df0 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66  parameter.** ovf
15df1 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  l), this functio
15df2 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67 65  n finds the page
15df3 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e   number of the n
15df4 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ext page in the 
15df5 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  .** linked list 
15df6 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  of overflow page
15df7 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20  s. If possible, 
15df8 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74 6f  it uses the auto
15df9 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74  -vacuum.** point
15dfa 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 74  er-map data inst
15dfb 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20 74  ead of reading t
15dfc 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61  he content of pa
15dfd 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f  ge ovfl to do so
15dfe 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  . .**.** If an e
15dff 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 53  rror occurs an S
15e00 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
15e01 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
15e02 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54  herwise:.**.** T
15e03 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  he page number o
15e04 66 20 74 68 65 20 6e 65 78 74 20 6f 76 65 72 66  f the next overf
15e05 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20  low page in the 
15e06 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 0a  linked list is .
15e07 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70  ** written to *p
15e08 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61 67  PgnoNext. If pag
15e09 65 20 6f 76 66 6c 20 69 73 20 74 68 65 20 6c 61  e ovfl is the la
15e0a 73 74 20 70 61 67 65 20 69 6e 20 69 74 73 20 6c  st page in its l
15e0b 69 6e 6b 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20  inked .** list, 
15e0c 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 73 65  *pPgnoNext is se
15e0d 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a  t to zero. .**.*
15e0e 2a 20 49 66 20 70 70 50 61 67 65 20 69 73 20 6e  * If ppPage is n
15e0f 6f 74 20 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72  ot NULL, and a r
15e10 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
15e11 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 63  MemPage object c
15e12 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20  orresponding.** 
15e13 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70  to page number p
15e14 4f 76 66 6c 20 77 61 73 20 6f 62 74 61 69 6e 65  Ovfl was obtaine
15e15 64 2c 20 74 68 65 6e 20 2a 70 70 50 61 67 65 20  d, then *ppPage 
15e16 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
15e17 74 6f 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 72  to that.** refer
15e18 65 6e 63 65 2e 20 49 74 20 69 73 20 74 68 65 20  ence. It is the 
15e19 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f  responsibility o
15e1a 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20  f the caller to 
15e1b 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 65  call releasePage
15e1c 28 29 0a 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65  ().** on *ppPage
15e1d 20 74 6f 20 66 72 65 65 20 74 68 65 20 72 65 66   to free the ref
15e1e 65 72 65 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65  erence. In no re
15e1f 66 65 72 65 6e 63 65 20 77 61 73 20 6f 62 74 61  ference was obta
15e20 69 6e 65 64 20 28 62 65 63 61 75 73 65 0a 2a 2a  ined (because.**
15e21 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
15e22 20 77 61 73 20 75 73 65 64 20 74 6f 20 6f 62 74   was used to obt
15e23 61 69 6e 20 74 68 65 20 76 61 6c 75 65 20 66 6f  ain the value fo
15e24 72 20 2a 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74  r *pPgnoNext), t
15e25 68 65 6e 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69  hen.** *ppPage i
15e26 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a  s set to zero..*
15e27 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
15e28 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 20  OverflowPage(.  
15e29 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a  BtShared *pBt, .
15e2a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20    Pgno ovfl,    
15e2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15e2c 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  * Overflow page 
15e2d 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70  */.  MemPage **p
15e2e 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  pPage,          
15e2f 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67    /* OUT: MemPag
15e30 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65  e handle (may be
15e31 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f   NULL) */.  Pgno
15e32 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20   *pPgnoNext     
15e33 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
15e34 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70   Next overflow p
15e35 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b  age number */.){
15e36 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30  .  Pgno next = 0
15e37 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
15e38 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  ge = 0;.  int rc
15e39 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
15e3a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15e3b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
15e3c 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
15e3d 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a  ert(pPgnoNext);.
15e3e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
15e3f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
15e40 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64    /* Try to find
15e41 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69   the next page i
15e42 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c  n the overflow l
15e43 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20  ist using the.  
15e44 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f  ** autovacuum po
15e45 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e  inter-map pages.
15e46 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20   Guess that the 
15e47 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20  next page in .  
15e48 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  ** the overflow 
15e49 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d  list is page num
15e4a 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66  ber (ovfl+1). If
15e4b 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e   that guess turn
15e4c 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62  s .  ** out to b
15e4d 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61  e wrong, fall ba
15e4e 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68  ck to loading th
15e4f 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a  e data of page .
15e50 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c    ** number ovfl
15e51 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
15e52 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62  e next page numb
15e53 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  er..  */.  if( p
15e54 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
15e55 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b  {.    Pgno pgno;
15e56 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73  .    Pgno iGuess
15e57 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75   = ovfl+1;.    u
15e58 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68  8 eType;..    wh
15e59 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41  ile( PTRMAP_ISPA
15e5a 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20  GE(pBt, iGuess) 
15e5b 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49  || iGuess==PENDI
15e5c 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
15e5d 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73  ) ){.      iGues
15e5e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  s++;.    }..    
15e5f 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65  if( iGuess<=page
15e60 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20  rPagecount(pBt) 
15e61 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74  ){.      rc = pt
15e62 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 75  rmapGet(pBt, iGu
15e63 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67  ess, &eType, &pg
15e64 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  no);.      if( r
15e65 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
15e66 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56  eType==PTRMAP_OV
15e67 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d  ERFLOW2 && pgno=
15e68 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20  =ovfl ){.       
15e69 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a   next = iGuess;.
15e6a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
15e6b 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20  ITE_DONE;.      
15e6c 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  }.    }.  }.#end
15e6d 69 66 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  if..  if( rc==SQ
15e6e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
15e6f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
15e70 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66  GetPage(pBt, ovf
15e71 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20  l, &pPage, 0);. 
15e72 20 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51     assert(rc==SQ
15e73 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65  LITE_OK || pPage
15e74 3d 3d 30 29 3b 0a 20 20 20 20 69 66 28 20 6e 65  ==0);.    if( ne
15e75 78 74 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c  xt==0 && rc==SQL
15e76 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
15e77 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74 65 28  next = get4byte(
15e78 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20  pPage->aData);. 
15e79 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50 67     }.  }..  *pPg
15e7a 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20  noNext = next;. 
15e7b 20 69 66 28 20 70 70 50 61 67 65 20 29 7b 0a 20   if( ppPage ){. 
15e7c 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61     *ppPage = pPa
15e7d 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ge;.  }else{.   
15e7e 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
15e7f 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ge);.  }.  retur
15e80 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  n (rc==SQLITE_DO
15e81 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  NE ? SQLITE_OK :
15e82 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   rc);.}../*.** C
15e83 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 61 20  opy data from a 
15e84 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 67 65  buffer to a page
15e85 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 67 65  , or from a page
15e86 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a   to a buffer..**
15e87 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 73 20  .** pPayload is 
15e88 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 61 74  a pointer to dat
15e89 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 74 61  a stored on data
15e8a 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67  base page pDbPag
15e8b 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e  e..** If argumen
15e8c 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 2c 20  t eOp is false, 
15e8d 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 65 73  then nByte bytes
15e8e 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70   of data are cop
15e8f 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 79  ied.** from pPay
15e90 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 66 66  load to the buff
15e91 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 62 79  er pointed at by
15e92 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 69 73   pBuf. If eOp is
15e93 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 73   true,.** then s
15e94 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
15e95 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20  () is called on 
15e96 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 79 74  pDbPage and nByt
15e97 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61  e bytes.** of da
15e98 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 72  ta are copied fr
15e99 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42  om the buffer pB
15e9a 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e 0a  uf to pPayload..
15e9b 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  **.** SQLITE_OK 
15e9c 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73  is returned on s
15e9d 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 69 73  uccess, otherwis
15e9e 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e  e an error code.
15e9f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
15ea0 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 76 6f  opyPayload(.  vo
15ea1 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20 20  id *pPayload,   
15ea2 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
15ea3 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61 20  er to page data 
15ea4 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c  */.  void *pBuf,
15ea5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15ea6 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66  * Pointer to buf
15ea7 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  fer */.  int nBy
15ea8 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
15ea9 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
15eaa 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f  bytes to copy */
15eab 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 20 20  .  int eOp,     
15eac 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15ead 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20 70  0 -> copy from p
15eae 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20 74  age, 1 -> copy t
15eaf 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 61  o page */.  DbPa
15eb0 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 20 20  ge *pDbPage     
15eb1 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f        /* Page co
15eb2 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f 61  ntaining pPayloa
15eb3 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65 4f  d */.){.  if( eO
15eb4 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79  p ){.    /* Copy
15eb5 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66 65   data from buffe
15eb6 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 72 69  r to page (a wri
15eb7 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f  te operation) */
15eb8 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71  .    int rc = sq
15eb9 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
15eba 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
15ebb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
15ebc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
15ebd 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65  rc;.    }.    me
15ebe 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70  mcpy(pPayload, p
15ebf 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d  Buf, nByte);.  }
15ec0 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 70  else{.    /* Cop
15ec1 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67 65  y data from page
15ec2 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 72 65   to buffer (a re
15ec3 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f  ad operation) */
15ec4 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75 66  .    memcpy(pBuf
15ec5 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74  , pPayload, nByt
15ec6 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
15ec7 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
15ec8 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
15ec9 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  on is used to re
15eca 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 65 20  ad or overwrite 
15ecb 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74  payload informat
15ecc 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65  ion.** for the e
15ecd 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 70 43  ntry that the pC
15ece 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69  ur cursor is poi
15ecf 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74 68 65  nting to. If the
15ed0 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74 65   eOp.** paramete
15ed1 72 20 69 73 20 30 2c 20 74 68 69 73 20 69 73 20  r is 0, this is 
15ed2 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e  a read operation
15ed3 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 69 6e   (data copied in
15ed4 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75  to.** buffer pBu
15ed5 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 6e  f). If it is non
15ed6 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65 20 28  -zero, a write (
15ed7 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d  data copied from
15ed8 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 29  .** buffer pBuf)
15ed9 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20  ..**.** A total 
15eda 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73 20 61  of "amt" bytes a
15edb 72 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74  re read or writt
15edc 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20  en beginning at 
15edd 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74  "offset"..** Dat
15ede 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f 72 20  a is read to or 
15edf 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20  from the buffer 
15ee0 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  pBuf..**.** This
15ee1 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f   routine does no
15ee2 74 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63  t make a distinc
15ee3 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 6b 65 79  tion between key
15ee4 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 49 74   and data..** It
15ee5 20 6a 75 73 74 20 72 65 61 64 73 20 6f 72 20 77   just reads or w
15ee6 72 69 74 65 73 20 62 79 74 65 73 20 66 72 6f 6d  rites bytes from
15ee7 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65   the payload are
15ee8 61 2e 20 20 44 61 74 61 20 6d 69 67 68 74 20 0a  a.  Data might .
15ee9 2a 2a 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65  ** appear on the
15eea 20 6d 61 69 6e 20 70 61 67 65 20 6f 72 20 62 65   main page or be
15eeb 20 73 63 61 74 74 65 72 65 64 20 6f 75 74 20 6f   scattered out o
15eec 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 66  n multiple overf
15eed 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 73 2e 0a 2a  low .** pages..*
15eee 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74 43 75  *.** If the BtCu
15eef 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62 48  rsor.isIncrblobH
15ef0 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65  andle flag is se
15ef1 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65  t, and the curre
15ef2 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e 74  nt.** cursor ent
15ef3 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 20 6d  ry uses one or m
15ef4 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ore overflow pag
15ef5 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  es, this functio
15ef6 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20 73  n.** allocates s
15ef7 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a  pace for and laz
15ef8 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20 74 68  ily popluates th
15ef9 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  e overflow page-
15efa 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65 20 61  list .** cache a
15efb 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 2e 61  rray (BtCursor.a
15efc 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62 73 65  Overflow). Subse
15efd 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 65 20  quent calls use 
15efe 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20 74 6f  this.** cache to
15eff 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f   make seeking to
15f00 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6f 66   the supplied of
15f01 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69 63 69  fset more effici
15f02 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20  ent..**.** Once 
15f03 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
15f04 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 20  -list cache has 
15f05 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20  been allocated, 
15f06 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 76  it may be.** inv
15f07 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f 6d 65  alidated if some
15f08 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 77 72   other cursor wr
15f09 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 6d 65  ites to the same
15f0a 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a   table, or if.**
15f0b 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d   the cursor is m
15f0c 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 65 72  oved to a differ
15f0d 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 69 6f  ent row. Additio
15f0e 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d 76  nally, in auto-v
15f0f 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74  acuum.** mode, t
15f10 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 65  he following eve
15f11 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 64 61  nts may invalida
15f12 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  te an overflow p
15f13 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 0a  age-list cache..
15f14 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e 63  **.**   * An inc
15f15 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2c  remental vacuum,
15f16 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69 74  .**   * A commit
15f17 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d 3d   in auto_vacuum=
15f18 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20  "full" mode,.** 
15f19 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 20 74    * Creating a t
15f1a 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 69 72  able (may requir
15f1b 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65 72  e moving an over
15f1c 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a 73  flow page)..*/.s
15f1d 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 73 73  tatic int access
15f1e 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72  Payload(.  BtCur
15f1f 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20  sor *pCur,      
15f20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69  /* Cursor pointi
15f21 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72  ng to entry to r
15f22 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33  ead from */.  u3
15f23 32 20 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20  2 offset,       
15f24 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64     /* Begin read
15f25 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 6e 74  ing this far int
15f26 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75  o payload */.  u
15f27 33 32 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  32 amt,         
15f28 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 69 73      /* Read this
15f29 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a 20   many bytes */. 
15f2a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
15f2b 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 20 74  pBuf, /* Write t
15f2c 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 74 68  he bytes into th
15f2d 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a 20 20  is buffer */ .  
15f2e 69 6e 74 20 73 6b 69 70 4b 65 79 2c 20 20 20 20  int skipKey,    
15f2f 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 62       /* offset b
15f30 65 67 69 6e 73 20 61 74 20 64 61 74 61 20 69 66  egins at data if
15f31 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f   this is true */
15f32 0a 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20  .  int eOp      
15f33 20 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20          /* zero 
15f34 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72  to read. non-zer
15f35 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29  o to write. */.)
15f36 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
15f37 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69  r *aPayload;.  i
15f38 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
15f39 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20  K;.  u32 nKey;. 
15f3a 20 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20   int iIdx = 0;. 
15f3b 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
15f3c 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
15f3d 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20  Cur->iPage]; /* 
15f3e 42 74 72 65 65 20 70 61 67 65 20 6f 66 20 63 75  Btree page of cu
15f3f 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20  rrent entry */. 
15f40 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15f41 20 70 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 20   pCur->pBt;     
15f42 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15f43 42 74 72 65 65 20 74 68 69 73 20 63 75 72 73 6f  Btree this curso
15f44 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a  r belongs to */.
15f45 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
15f46 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
15f47 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
15f48 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73  OR_VALID );.  as
15f49 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
15f4a 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
15f4b 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
15f4c 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
15f4d 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
15f4e 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66  );..  getCellInf
15f4f 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c  o(pCur);.  aPayl
15f50 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  oad = pCur->info
15f51 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69  .pCell + pCur->i
15f52 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 6e  nfo.nHeader;.  n
15f53 4b 65 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 6e  Key = (pPage->in
15f54 74 4b 65 79 20 3f 20 30 20 3a 20 28 69 6e 74 29  tKey ? 0 : (int)
15f55 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29  pCur->info.nKey)
15f56 3b 0a 0a 20 20 69 66 28 20 73 6b 69 70 4b 65 79  ;..  if( skipKey
15f57 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2b   ){.    offset +
15f58 3d 20 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66  = nKey;.  }.  if
15f59 28 20 6f 66 66 73 65 74 2b 61 6d 74 20 3e 20 6e  ( offset+amt > n
15f5a 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  Key+pCur->info.n
15f5b 44 61 74 61 20 0a 20 20 20 7c 7c 20 26 61 50 61  Data .   || &aPa
15f5c 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f  yload[pCur->info
15f5d 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 61 67  .nLocal] > &pPag
15f5e 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73  e->aData[pBt->us
15f5f 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29 7b 0a 20  ableSize].  ){. 
15f60 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20     /* Trying to 
15f61 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 70 61  read or write pa
15f62 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  st the end of th
15f63 65 20 64 61 74 61 20 69 73 20 61 6e 20 65 72 72  e data is an err
15f64 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  or */.    return
15f65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15f66 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  BKPT;.  }..  /* 
15f67 43 68 65 63 6b 20 69 66 20 64 61 74 61 20 6d 75  Check if data mu
15f68 73 74 20 62 65 20 72 65 61 64 2f 77 72 69 74 74  st be read/writt
15f69 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62  en to/from the b
15f6a 74 72 65 65 20 70 61 67 65 20 69 74 73 65 6c 66  tree page itself
15f6b 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 65  . */.  if( offse
15f6c 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  t<pCur->info.nLo
15f6d 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 61  cal ){.    int a
15f6e 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66 28 20   = amt;.    if( 
15f6f 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e 69  a+offset>pCur->i
15f70 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20  nfo.nLocal ){.  
15f71 20 20 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e      a = pCur->in
15f72 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73  fo.nLocal - offs
15f73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  et;.    }.    rc
15f74 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26   = copyPayload(&
15f75 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d  aPayload[offset]
15f76 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20  , pBuf, a, eOp, 
15f77 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
15f78 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b  .    offset = 0;
15f79 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a  .    pBuf += a;.
15f7a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20      amt -= a;.  
15f7b 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 65  }else{.    offse
15f7c 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  t -= pCur->info.
15f7d 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69  nLocal;.  }..  i
15f7e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15f7f 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20   && amt>0 ){.   
15f80 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 66 6c 53   const u32 ovflS
15f81 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c  ize = pBt->usabl
15f82 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42  eSize - 4;  /* B
15f83 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 72  ytes content per
15f84 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20   ovfl page */.  
15f85 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b    Pgno nextPage;
15f86 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d  ..    nextPage =
15f87 20 67 65 74 34 62 79 74 65 28 26 61 50 61 79 6c   get4byte(&aPayl
15f88 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  oad[pCur->info.n
15f89 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65  Local]);..#ifnde
15f8a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
15f8b 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66  CRBLOB.    /* If
15f8c 20 74 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 48   the isIncrblobH
15f8d 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65  andle flag is se
15f8e 74 20 61 6e 64 20 74 68 65 20 42 74 43 75 72 73  t and the BtCurs
15f8f 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20  or.aOverflow[]. 
15f90 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65     ** has not be
15f91 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c  en allocated, al
15f92 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54  locate it now. T
15f93 68 65 20 61 72 72 61 79 20 69 73 20 73 69 7a 65  he array is size
15f94 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20  d at.    ** one 
15f95 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f  entry for each o
15f96 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20  verflow page in 
15f97 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61  the overflow cha
15f98 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70  in. The.    ** p
15f99 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
15f9a 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77  e first overflow
15f9b 20 70 61 67 65 20 69 73 20 73 74 6f 72 65 64 20   page is stored 
15f9c 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c  in aOverflow[0],
15f9d 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20 41 20 76  .    ** etc. A v
15f9e 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65  alue of 0 in the
15f9f 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72   aOverflow[] arr
15fa0 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65  ay means "not ye
15fa1 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20  t known".    ** 
15fa2 28 74 68 65 20 63 61 63 68 65 20 69 73 20 6c 61  (the cache is la
15fa3 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e  zily populated).
15fa4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
15fa5 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62  pCur->isIncrblob
15fa6 48 61 6e 64 6c 65 20 26 26 20 21 70 43 75 72 2d  Handle && !pCur-
15fa7 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  >aOverflow ){.  
15fa8 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20      int nOvfl = 
15fa9 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79  (pCur->info.nPay
15faa 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e  load-pCur->info.
15fab 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d  nLocal+ovflSize-
15fac 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20  1)/ovflSize;.   
15fad 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c     pCur->aOverfl
15fae 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c  ow = (Pgno *)sql
15faf 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  ite3MallocZero(s
15fb0 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 66  izeof(Pgno)*nOvf
15fb1 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f  l);.      if( nO
15fb2 76 66 6c 20 26 26 20 21 70 43 75 72 2d 3e 61 4f  vfl && !pCur->aO
15fb3 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20  verflow ){.     
15fb4 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
15fb5 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
15fb6 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
15fb7 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
15fb8 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 20  -list cache has 
15fb9 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61  been allocated a
15fba 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e  nd the.    ** en
15fbb 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 72 73  try for the firs
15fbc 74 20 72 65 71 75 69 72 65 64 20 6f 76 65 72 66  t required overf
15fbd 6c 6f 77 20 70 61 67 65 20 69 73 20 76 61 6c 69  low page is vali
15fbe 64 2c 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64  d, skip.    ** d
15fbf 69 72 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20  irectly to it.. 
15fc0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43     */.    if( pC
15fc1 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26  ur->aOverflow &&
15fc2 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
15fc3 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65  [offset/ovflSize
15fc4 5d 20 29 7b 0a 20 20 20 20 20 20 69 49 64 78 20  ] ){.      iIdx 
15fc5 3d 20 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69  = (offset/ovflSi
15fc6 7a 65 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 50  ze);.      nextP
15fc7 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65  age = pCur->aOve
15fc8 72 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 20  rflow[iIdx];.   
15fc9 20 20 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 66     offset = (off
15fca 73 65 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a 20  set%ovflSize);. 
15fcb 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
15fcc 20 66 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49   for( ; rc==SQLI
15fcd 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26  TE_OK && amt>0 &
15fce 26 20 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78  & nextPage; iIdx
15fcf 2b 2b 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 51  ++){..#ifndef SQ
15fd0 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
15fd1 4f 42 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72  OB.      /* If r
15fd2 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74  equired, populat
15fd3 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  e the overflow p
15fd4 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20  age-list cache. 
15fd5 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 75  */.      if( pCu
15fd6 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a  r->aOverflow ){.
15fd7 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 21          assert(!
15fd8 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b  pCur->aOverflow[
15fd9 69 49 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e 61  iIdx] || pCur->a
15fda 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d  Overflow[iIdx]==
15fdb 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20  nextPage);.     
15fdc 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c     pCur->aOverfl
15fdd 6f 77 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50  ow[iIdx] = nextP
15fde 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  age;.      }.#en
15fdf 64 69 66 0a 0a 20 20 20 20 20 20 69 66 28 20 6f  dif..      if( o
15fe0 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20  ffset>=ovflSize 
15fe1 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
15fe2 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f  e only reason to
15fe3 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20   read this page 
15fe4 69 73 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  is to obtain the
15fe5 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a   page.        **
15fe6 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
15fe7 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65  next page in the
15fe8 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e   overflow chain.
15fe9 20 54 68 65 20 70 61 67 65 0a 20 20 20 20 20 20   The page.      
15fea 20 20 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74    ** data is not
15feb 20 72 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69   required. So fi
15fec 72 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75  rst try to looku
15fed 70 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20  p the overflow. 
15fee 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c         ** page-l
15fef 69 73 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e  ist cache, if an
15ff0 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63  y, then fall bac
15ff1 6b 20 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72  k to the getOver
15ff2 66 6c 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20  flowPage().     
15ff3 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a     ** function..
15ff4 20 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e 64          */.#ifnd
15ff5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
15ff6 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 20  NCRBLOB.        
15ff7 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66  if( pCur->aOverf
15ff8 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76  low && pCur->aOv
15ff9 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29  erflow[iIdx+1] )
15ffa 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74  {.          next
15ffb 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76  Page = pCur->aOv
15ffc 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a  erflow[iIdx+1];.
15ffd 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 0a          } else .
15ffe 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20  #endif.         
15fff 20 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f   rc = getOverflo
16000 77 50 61 67 65 28 70 42 74 2c 20 6e 65 78 74 50  wPage(pBt, nextP
16001 61 67 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 67  age, 0, &nextPag
16002 65 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73  e);.        offs
16003 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a  et -= ovflSize;.
16004 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
16005 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20       /* Need to 
16006 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 70  read this page p
16007 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74  roperly. It cont
16008 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65  ains some of the
16009 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67  .        ** rang
1600a 65 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 69  e of data that i
1600b 73 20 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f  s being read (eO
1600c 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e  p==0) or written
1600d 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20   (eOp!=0)..     
1600e 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 62     */.        Db
1600f 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20  Page *pDbPage;. 
16010 20 20 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61         int a = a
16011 6d 74 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  mt;.        rc =
16012 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
16013 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65  (pBt->pPager, ne
16014 78 74 50 61 67 65 2c 20 26 70 44 62 50 61 67 65  xtPage, &pDbPage
16015 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
16016 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16017 20 20 20 20 20 20 20 20 20 20 61 50 61 79 6c 6f            aPaylo
16018 61 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ad = sqlite3Page
16019 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65  rGetData(pDbPage
1601a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78  );.          nex
1601b 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65  tPage = get4byte
1601c 28 61 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20  (aPayload);.    
1601d 20 20 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66        if( a + of
1601e 66 73 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20  fset > ovflSize 
1601f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
16020 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66   = ovflSize - of
16021 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20  fset;.          
16022 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  }.          rc =
16023 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50   copyPayload(&aP
16024 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d  ayload[offset+4]
16025 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20  , pBuf, a, eOp, 
16026 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
16027 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
16028 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a  Unref(pDbPage);.
16029 20 20 20 20 20 20 20 20 20 20 6f 66 66 73 65 74            offset
1602a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
1602b 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20  amt -= a;.      
1602c 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20      pBuf += a;. 
1602d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1602e 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
1602f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16030 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20  && amt>0 ){.    
16031 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
16032 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a  RRUPT_BKPT;.  }.
16033 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
16034 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20  /*.** Read part 
16035 6f 66 20 74 68 65 20 6b 65 79 20 61 73 73 6f 63  of the key assoc
16036 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f  iated with curso
16037 72 20 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79  r pCur.  Exactly
16038 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20  .** "amt" bytes 
16039 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72  will be transfer
1603a 65 64 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20  ed into pBuf[]. 
1603b 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a   The transfer.**
1603c 20 62 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73   begins at "offs
1603d 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  et"..**.** Retur
1603e 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
1603f 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72  uccess or an err
16040 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68  or code if anyth
16041 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e  ing goes.** wron
16042 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20  g.  An error is 
16043 72 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66  returned if "off
16044 73 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67  set+amt" is larg
16045 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61  er than.** the a
16046 76 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64  vailable payload
16047 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16048 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
16049 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72  treeKey(BtCursor
1604a 20 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73   *pCur, u32 offs
1604b 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69  et, u32 amt, voi
1604c 64 20 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20  d *pBuf){.  int 
1604d 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63  rc;..  assert( c
1604e 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1604f 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20  pCur) );.  rc = 
16050 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  restoreCursorPos
16051 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69  ition(pCur);.  i
16052 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
16053 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
16054 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
16055 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
16056 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16057 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72  iPage>=0 && pCur
16058 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
16059 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 69 66 28  Page] );.    if(
1605a 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
1605b 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20  ->intKey ){.    
1605c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1605d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1605e 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1605f 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
16060 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61  ->iPage]<pCur->a
16061 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
16062 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  e]->nCell );.   
16063 20 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c   rc = accessPayl
16064 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74  oad(pCur, offset
16065 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64  , amt, (unsigned
16066 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30 2c 20   char*)pBuf, 0, 
16067 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
16068 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
16069 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 64  ad part of the d
1606a 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
1606b 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e  ith cursor pCur.
1606c 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d    Exactly.** "am
1606d 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65  t" bytes will be
1606e 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f   transfered into
1606f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72   pBuf[].  The tr
16070 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73  ansfer.** begins
16071 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a   at "offset"..**
16072 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
16073 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
16074 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  or an error code
16075 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65   if anything goe
16076 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20  s.** wrong.  An 
16077 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
16078 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74  d if "offset+amt
16079 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  " is larger than
1607a 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c  .** the availabl
1607b 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51  e payload..*/.SQ
1607c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1607d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
1607e 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  a(BtCursor *pCur
1607f 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33  , u32 offset, u3
16080 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75  2 amt, void *pBu
16081 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23  f){.  int rc;..#
16082 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16083 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66  IT_INCRBLOB.  if
16084 20 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d   ( pCur->eState=
16085 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
16086 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
16087 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a  LITE_ABORT;.  }.
16088 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74  #endif..  assert
16089 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
1608a 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
1608b 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
1608c 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
1608d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1608e 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
1608f 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
16090 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
16091 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
16092 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70  r->iPage>=0 && p
16093 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
16094 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20  ->iPage] );.    
16095 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69  assert( pCur->ai
16096 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
16097 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  <pCur->apPage[pC
16098 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
16099 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63  l );.    rc = ac
1609a 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72  cessPayload(pCur
1609b 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70  , offset, amt, p
1609c 42 75 66 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a  Buf, 1, 0);.  }.
1609d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1609e 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
1609f 6f 69 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f 61  ointer to payloa
160a0 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  d information fr
160a1 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  om the entry tha
160a2 74 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 63  t the .** pCur c
160a3 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e  ursor is pointin
160a4 67 20 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74  g to.  The point
160a5 65 72 20 69 73 20 74 6f 20 74 68 65 20 62 65 67  er is to the beg
160a6 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65  inning of.** the
160a7 20 6b 65 79 20 69 66 20 73 6b 69 70 4b 65 79 3d   key if skipKey=
160a8 3d 30 20 61 6e 64 20 69 74 20 70 6f 69 6e 74 73  =0 and it points
160a9 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   to the beginnin
160aa 67 20 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a 20  g of data if.** 
160ab 73 6b 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65  skipKey==1.  The
160ac 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
160ad 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6b 65   of available ke
160ae 79 2f 64 61 74 61 20 69 73 20 77 72 69 74 74 65  y/data is writte
160af 6e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e  n.** into *pAmt.
160b0 20 20 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74    If *pAmt==0, t
160b1 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
160b2 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20  turned will not 
160b3 62 65 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f  be.** a valid po
160b4 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  inter..**.** Thi
160b5 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20  s routine is an 
160b6 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49  optimization.  I
160b7 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20  t is common for 
160b8 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a  the entire key.*
160b9 2a 20 61 6e 64 20 64 61 74 61 20 74 6f 20 66 69  * and data to fi
160ba 74 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70  t on the local p
160bb 61 67 65 20 61 6e 64 20 66 6f 72 20 74 68 65 72  age and for ther
160bc 65 20 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66  e to be no overf
160bd 6c 6f 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57  low.** pages.  W
160be 68 65 6e 20 74 68 61 74 20 69 73 20 73 6f 2c 20  hen that is so, 
160bf 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e  this routine can
160c0 20 62 65 20 75 73 65 64 20 74 6f 20 61 63 63 65   be used to acce
160c1 73 73 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e  ss the.** key an
160c2 64 20 64 61 74 61 20 77 69 74 68 6f 75 74 20 6d  d data without m
160c3 61 6b 69 6e 67 20 61 20 63 6f 70 79 2e 20 20 49  aking a copy.  I
160c4 66 20 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72  f the key and/or
160c5 20 64 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20   data spills.** 
160c6 6f 6e 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61  onto overflow pa
160c7 67 65 73 2c 20 74 68 65 6e 20 61 63 63 65 73 73  ges, then access
160c8 50 61 79 6c 6f 61 64 28 29 20 6d 75 73 74 20 62  Payload() must b
160c9 65 20 75 73 65 64 20 74 6f 20 72 65 61 73 73 65  e used to reasse
160ca 6d 62 6c 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f  mble.** the key/
160cb 64 61 74 61 20 61 6e 64 20 63 6f 70 79 20 69 74  data and copy it
160cc 20 69 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63   into a prealloc
160cd 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a  ated buffer..**.
160ce 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72  ** The pointer r
160cf 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20  eturned by this 
160d0 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69  routine looks di
160d1 72 65 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20  rectly into the 
160d2 63 61 63 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f  cached.** page o
160d3 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  f the database. 
160d4 20 54 68 65 20 64 61 74 61 20 6d 69 67 68 74 20   The data might 
160d5 63 68 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74  change or move t
160d6 68 65 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20  he next time.** 
160d7 61 6e 79 20 62 74 72 65 65 20 72 6f 75 74 69 6e  any btree routin
160d8 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  e is called..*/.
160d9 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
160da 69 67 6e 65 64 20 63 68 61 72 20 2a 66 65 74 63  igned char *fetc
160db 68 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75  hPayload(.  BtCu
160dc 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20  rsor *pCur,     
160dd 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74   /* Cursor point
160de 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20  ing to entry to 
160df 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69  read from */.  i
160e0 6e 74 20 2a 70 41 6d 74 2c 20 20 20 20 20 20 20  nt *pAmt,       
160e1 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
160e2 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c   number of avail
160e3 61 62 6c 65 20 62 79 74 65 73 20 68 65 72 65 20  able bytes here 
160e4 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79  */.  int skipKey
160e5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 61            /* rea
160e6 64 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 64  d beginning at d
160e7 61 74 61 20 69 66 20 74 68 69 73 20 69 73 20 74  ata if this is t
160e8 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69  rue */.){.  unsi
160e9 67 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c  gned char *aPayl
160ea 6f 61 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  oad;.  MemPage *
160eb 70 50 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65  pPage;.  u32 nKe
160ec 79 3b 0a 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b  y;.  u32 nLocal;
160ed 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ..  assert( pCur
160ee 21 3d 30 20 26 26 20 70 43 75 72 2d 3e 69 50 61  !=0 && pCur->iPa
160ef 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61  ge>=0 && pCur->a
160f0 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
160f1 65 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e]);.  assert( p
160f2 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
160f3 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61  SOR_VALID );.  a
160f4 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
160f5 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
160f6 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
160f7 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
160f8 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  age];.  assert( 
160f9 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
160fa 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e  ->iPage]<pPage->
160fb 6e 43 65 6c 6c 20 29 3b 0a 20 20 67 65 74 43 65  nCell );.  getCe
160fc 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20  llInfo(pCur);.  
160fd 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d  aPayload = pCur-
160fe 3e 69 6e 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61  >info.pCell;.  a
160ff 50 61 79 6c 6f 61 64 20 2b 3d 20 70 43 75 72 2d  Payload += pCur-
16100 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20  >info.nHeader;. 
16101 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b   if( pPage->intK
16102 65 79 20 29 7b 0a 20 20 20 20 6e 4b 65 79 20 3d  ey ){.    nKey =
16103 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
16104 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29 70 43 75   nKey = (int)pCu
16105 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20  r->info.nKey;.  
16106 7d 0a 20 20 69 66 28 20 73 6b 69 70 4b 65 79 20  }.  if( skipKey 
16107 29 7b 0a 20 20 20 20 61 50 61 79 6c 6f 61 64 20  ){.    aPayload 
16108 2b 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 6e 4c 6f  += nKey;.    nLo
16109 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  cal = pCur->info
1610a 2e 6e 4c 6f 63 61 6c 20 2d 20 6e 4b 65 79 3b 0a  .nLocal - nKey;.
1610b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4c 6f    }else{.    nLo
1610c 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  cal = pCur->info
1610d 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 69 66 28  .nLocal;.    if(
1610e 20 6e 4c 6f 63 61 6c 3e 6e 4b 65 79 20 29 7b 0a   nLocal>nKey ){.
1610f 20 20 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 6e        nLocal = n
16110 4b 65 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  Key;.    }.  }. 
16111 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b   *pAmt = nLocal;
16112 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79 6c 6f  .  return aPaylo
16113 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f  ad;.}.../*.** Fo
16114 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  r the entry that
16115 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20   cursor pCur is 
16116 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e  point to, return
16117 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 65   as.** many byte
16118 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 20  s of the key or 
16119 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61 69  data as are avai
1611a 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63  lable on the loc
1611b 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 67  al.** b-tree pag
1611c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 6e 75  e.  Write the nu
1611d 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c  mber of availabl
1611e 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 41  e bytes into *pA
1611f 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  mt..**.** The po
16120 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 69  inter returned i
16121 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 68  s ephemeral.  Th
16122 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 6d  e key/data may m
16123 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 73  ove.** or be des
16124 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e 65  troyed on the ne
16125 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 42  xt call to any B
16126 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a  tree routine,.**
16127 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73   including calls
16128 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 65   from other thre
16129 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 65 20  ads against the 
1612a 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 48  same cache..** H
1612b 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 6f 6e  ence, a mutex on
1612c 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 68   the BtShared sh
1612d 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 72 69  ould be held pri
1612e 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a  or to calling.**
1612f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
16130 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
16131 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f 20 67  nes is used to g
16132 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 73 20  et quick access 
16133 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 0a  to key and data.
16134 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e  ** in the common
16135 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f 20 6f   case where no o
16136 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72  verflow pages ar
16137 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  e used..*/.SQLIT
16138 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
16139 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72  void *sqlite3Btr
1613a 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 72  eeKeyFetch(BtCur
1613b 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
1613c 70 41 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28  pAmt){.  assert(
1613d 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
1613e 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28  x(pCur) );.  if(
1613f 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16140 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20  URSOR_VALID ){. 
16141 20 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74     return (const
16142 20 76 6f 69 64 2a 29 66 65 74 63 68 50 61 79 6c   void*)fetchPayl
16143 6f 61 64 28 70 43 75 72 2c 20 70 41 6d 74 2c 20  oad(pCur, pAmt, 
16144 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
16145 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49   0;.}.SQLITE_PRI
16146 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20  VATE const void 
16147 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74  *sqlite3BtreeDat
16148 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 20  aFetch(BtCursor 
16149 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74  *pCur, int *pAmt
1614a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ){.  assert( cur
1614b 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
1614c 75 72 29 20 29 3b 0a 20 20 69 66 28 20 70 43 75  ur) );.  if( pCu
1614d 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
1614e 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72  R_VALID ){.    r
1614f 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 76 6f 69  eturn (const voi
16150 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28  d*)fetchPayload(
16151 70 43 75 72 2c 20 70 41 6d 74 2c 20 31 29 3b 0a  pCur, pAmt, 1);.
16152 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
16153 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74  }.../*.** Move t
16154 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74  he cursor down t
16155 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61  o a new child pa
16156 67 65 2e 20 20 54 68 65 20 6e 65 77 50 67 6e 6f  ge.  The newPgno
16157 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
16158 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20  .** page number 
16159 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 61 67  of the child pag
1615a 65 20 74 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2f  e to move to..*/
1615b 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65  .static int move
1615c 54 6f 43 68 69 6c 64 28 42 74 43 75 72 73 6f 72  ToChild(BtCursor
1615d 20 2a 70 43 75 72 2c 20 75 33 32 20 6e 65 77 50   *pCur, u32 newP
1615e 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  gno){.  int rc;.
1615f 20 20 69 6e 74 20 69 20 3d 20 70 43 75 72 2d 3e    int i = pCur->
16160 69 50 61 67 65 3b 0a 20 20 4d 65 6d 50 61 67 65  iPage;.  MemPage
16161 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20 20 42 74   *pNewPage;.  Bt
16162 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43  Shared *pBt = pC
16163 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65  ur->pBt;..  asse
16164 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
16165 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
16166 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
16167 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
16168 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ID );.  assert( 
16169 70 43 75 72 2d 3e 69 50 61 67 65 3c 42 54 43 55  pCur->iPage<BTCU
1616a 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 29  RSOR_MAX_DEPTH )
1616b 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50  ;.  if( pCur->iP
1616c 61 67 65 3e 3d 28 42 54 43 55 52 53 4f 52 5f 4d  age>=(BTCURSOR_M
1616d 41 58 5f 44 45 50 54 48 2d 31 29 20 29 7b 0a 20  AX_DEPTH-1) ){. 
1616e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1616f 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
16170 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64   }.  rc = getAnd
16171 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 6e 65  InitPage(pBt, ne
16172 77 50 67 6e 6f 2c 20 26 70 4e 65 77 50 61 67 65  wPgno, &pNewPage
16173 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
16174 74 75 72 6e 20 72 63 3b 0a 20 20 70 43 75 72 2d  turn rc;.  pCur-
16175 3e 61 70 50 61 67 65 5b 69 2b 31 5d 20 3d 20 70  >apPage[i+1] = p
16176 4e 65 77 50 61 67 65 3b 0a 20 20 70 43 75 72 2d  NewPage;.  pCur-
16177 3e 61 69 49 64 78 5b 69 2b 31 5d 20 3d 20 30 3b  >aiIdx[i+1] = 0;
16178 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b  .  pCur->iPage++
16179 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  ;..  pCur->info.
1617a 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75  nSize = 0;.  pCu
1617b 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30  r->validNKey = 0
1617c 3b 0a 20 20 69 66 28 20 70 4e 65 77 50 61 67 65  ;.  if( pNewPage
1617d 2d 3e 6e 43 65 6c 6c 3c 31 20 29 7b 0a 20 20 20  ->nCell<1 ){.   
1617e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1617f 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
16180 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
16181 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  _OK;.}..#ifndef 
16182 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 61 67  NDEBUG./*.** Pag
16183 65 20 70 50 61 72 65 6e 74 20 69 73 20 61 6e 20  e pParent is an 
16184 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65  internal (non-le
16185 61 66 29 20 74 72 65 65 20 70 61 67 65 2e 20 54  af) tree page. T
16186 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a  his function .**
16187 20 61 73 73 65 72 74 73 20 74 68 61 74 20 70 61   asserts that pa
16188 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64  ge number iChild
16189 20 69 73 20 74 68 65 20 6c 65 66 74 2d 63 68 69   is the left-chi
1618a 6c 64 20 69 66 20 74 68 65 20 69 49 64 78 27 74  ld if the iIdx't
1618b 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 67  h.** cell in pag
1618c 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 69  e pParent. Or, i
1618d 66 20 69 49 64 78 20 69 73 20 65 71 75 61 6c 20  f iIdx is equal 
1618e 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  to the total num
1618f 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20  ber of.** cells 
16190 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 61 74  in pParent, that
16191 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68   page number iCh
16192 69 6c 64 20 69 73 20 74 68 65 20 72 69 67 68 74  ild is the right
16193 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68 65  -child of.** the
16194 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   page..*/.static
16195 20 76 6f 69 64 20 61 73 73 65 72 74 50 61 72 65   void assertPare
16196 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 67 65 20  ntIndex(MemPage 
16197 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 49  *pParent, int iI
16198 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64 29  dx, Pgno iChild)
16199 7b 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 78  {.  assert( iIdx
1619a 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  <=pParent->nCell
1619b 20 29 3b 0a 20 20 69 66 28 20 69 49 64 78 3d 3d   );.  if( iIdx==
1619c 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29  pParent->nCell )
1619d 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65  {.    assert( ge
1619e 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d  t4byte(&pParent-
1619f 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e  >aData[pParent->
161a0 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 69  hdrOffset+8])==i
161a1 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73 65  Child );.  }else
161a2 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 65  {.    assert( ge
161a3 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28  t4byte(findCell(
161a4 70 50 61 72 65 6e 74 2c 20 69 49 64 78 29 29 3d  pParent, iIdx))=
161a5 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a 7d  =iChild );.  }.}
161a6 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  .#else.#  define
161a7 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64   assertParentInd
161a8 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64 69  ex(x,y,z) .#endi
161a9 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68  f../*.** Move th
161aa 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f 20 74  e cursor up to t
161ab 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a  he parent page..
161ac 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78 20  **.** pCur->idx 
161ad 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 65  is set to the ce
161ae 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20 63 6f  ll index that co
161af 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74  ntains the point
161b0 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 67  er.** to the pag
161b1 65 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20  e we are coming 
161b2 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 61 72 65  from.  If we are
161b3 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68 65   coming from the
161b4 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 63  .** right-most c
161b5 68 69 6c 64 20 70 61 67 65 20 74 68 65 6e 20 70  hild page then p
161b6 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20  Cur->idx is set 
161b7 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 6e  to one more than
161b8 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20  .** the largest 
161b9 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53  cell index..*/.S
161ba 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
161bb 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
161bc 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75  oveToParent(BtCu
161bd 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61  rsor *pCur){.  a
161be 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
161bf 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
161c0 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
161c1 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
161c2 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
161c3 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30  t( pCur->iPage>0
161c4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
161c5 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
161c6 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73  >iPage] );.  ass
161c7 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a  ertParentIndex(.
161c8 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65      pCur->apPage
161c9 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c  [pCur->iPage-1],
161ca 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64   .    pCur->aiId
161cb 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d  x[pCur->iPage-1]
161cc 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50  , .    pCur->apP
161cd 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
161ce 2d 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65  ->pgno.  );.  re
161cf 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e  leasePage(pCur->
161d0 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
161d1 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50  ge]);.  pCur->iP
161d2 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69  age--;.  pCur->i
161d3 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20  nfo.nSize = 0;. 
161d4 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
161d5 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d   = 0;.}../*.** M
161d6 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74  ove the cursor t
161d7 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 0a  o the root page.
161d8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f  */.static int mo
161d9 76 65 54 6f 52 6f 6f 74 28 42 74 43 75 72 73 6f  veToRoot(BtCurso
161da 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50  r *pCur){.  MemP
161db 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e  age *pRoot;.  in
161dc 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
161dd 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70  ;.  Btree *p = p
161de 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42  Cur->pBtree;.  B
161df 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
161e0 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74  ->pBt;..  assert
161e1 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
161e2 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
161e3 73 65 72 74 28 20 43 55 52 53 4f 52 5f 49 4e 56  sert( CURSOR_INV
161e4 41 4c 49 44 20 3c 20 43 55 52 53 4f 52 5f 52 45  ALID < CURSOR_RE
161e5 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61  QUIRESEEK );.  a
161e6 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41  ssert( CURSOR_VA
161e7 4c 49 44 20 20 20 3c 20 43 55 52 53 4f 52 5f 52  LID   < CURSOR_R
161e8 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20  EQUIRESEEK );.  
161e9 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 46  assert( CURSOR_F
161ea 41 55 4c 54 20 20 20 3e 20 43 55 52 53 4f 52 5f  AULT   > CURSOR_
161eb 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20  REQUIRESEEK );. 
161ec 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
161ed 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52  e>=CURSOR_REQUIR
161ee 45 53 45 45 4b 20 29 7b 0a 20 20 20 20 69 66 28  ESEEK ){.    if(
161ef 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
161f0 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20  URSOR_FAULT ){. 
161f1 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72       return pCur
161f2 2d 3e 73 6b 69 70 3b 0a 20 20 20 20 7d 0a 20 20  ->skip;.    }.  
161f3 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
161f4 65 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b  earCursor(pCur);
161f5 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 43 75 72  .  }..  if( pCur
161f6 2d 3e 69 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20  ->iPage>=0 ){.  
161f7 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
161f8 28 69 3d 31 3b 20 69 3c 3d 70 43 75 72 2d 3e 69  (i=1; i<=pCur->i
161f9 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Page; i++){.    
161fa 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43    releasePage(pC
161fb 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a  ur->apPage[i]);.
161fc 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
161fd 20 20 20 69 66 28 20 0a 20 20 20 20 20 20 53 51     if( .      SQ
161fe 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 67  LITE_OK!=(rc = g
161ff 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42  etAndInitPage(pB
16200 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  t, pCur->pgnoRoo
16201 74 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65  t, &pCur->apPage
16202 5b 30 5d 29 29 0a 20 20 20 20 29 7b 0a 20 20 20  [0])).    ){.   
16203 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
16204 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
16205 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
16206 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
16207 70 52 6f 6f 74 20 3d 20 70 43 75 72 2d 3e 61 70  pRoot = pCur->ap
16208 50 61 67 65 5b 30 5d 3b 0a 20 20 61 73 73 65 72  Page[0];.  asser
16209 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d  t( pRoot->pgno==
1620a 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29  pCur->pgnoRoot )
1620b 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20  ;.  pCur->iPage 
1620c 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49  = 0;.  pCur->aiI
1620d 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 70 43 75  dx[0] = 0;.  pCu
1620e 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20  r->info.nSize = 
1620f 30 3b 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73  0;.  pCur->atLas
16210 74 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76  t = 0;.  pCur->v
16211 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20  alidNKey = 0;.. 
16212 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c   if( pRoot->nCel
16213 6c 3d 3d 30 20 26 26 20 21 70 52 6f 6f 74 2d 3e  l==0 && !pRoot->
16214 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f  leaf ){.    Pgno
16215 20 73 75 62 70 61 67 65 3b 0a 20 20 20 20 61 73   subpage;.    as
16216 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e  sert( pRoot->pgn
16217 6f 3d 3d 31 20 29 3b 0a 20 20 20 20 73 75 62 70  o==1 );.    subp
16218 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26  age = get4byte(&
16219 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f  pRoot->aData[pRo
1621a 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ot->hdrOffset+8]
1621b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
1621c 75 62 70 61 67 65 3e 30 20 29 3b 0a 20 20 20 20  ubpage>0 );.    
1621d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
1621e 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20  URSOR_VALID;.   
1621f 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c   rc = moveToChil
16220 64 28 70 43 75 72 2c 20 73 75 62 70 61 67 65 29  d(pCur, subpage)
16221 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
16222 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 28 28  Cur->eState = ((
16223 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f  pRoot->nCell>0)?
16224 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 43 55 52  CURSOR_VALID:CUR
16225 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b 0a 20 20  SOR_INVALID);.  
16226 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
16227 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  ../*.** Move the
16228 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20   cursor down to 
16229 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65  the left-most le
1622a 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68  af entry beneath
1622b 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 20 74 6f   the.** entry to
1622c 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72   which it is cur
1622d 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e  rently pointing.
1622e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d  .**.** The left-
1622f 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 74 68 65  most leaf is the
16230 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 73 6d   one with the sm
16231 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 74 68 65  allest key - the
16232 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 61 73 63   first.** in asc
16233 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f  ending order..*/
16234 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65  .static int move
16235 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 43 75 72  ToLeftmost(BtCur
16236 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67  sor *pCur){.  Pg
16237 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72  no pgno;.  int r
16238 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
16239 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
1623a 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
1623b 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
1623c 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
1623d 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
1623e 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
1623f 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
16240 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 20  E_OK && !(pPage 
16241 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
16242 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65  Cur->iPage])->le
16243 61 66 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  af ){.    assert
16244 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  ( pCur->aiIdx[pC
16245 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65  ur->iPage]<pPage
16246 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 70  ->nCell );.    p
16247 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 66  gno = get4byte(f
16248 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70  indCell(pPage, p
16249 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
1624a 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 20 20 72  >iPage]));.    r
1624b 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28  c = moveToChild(
1624c 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d  pCur, pgno);.  }
1624d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1624e 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  ./*.** Move the 
1624f 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74  cursor down to t
16250 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65  he right-most le
16251 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68  af entry beneath
16252 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20   the.** page to 
16253 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72 72  which it is curr
16254 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 20  ently pointing. 
16255 20 4e 6f 74 69 63 65 20 74 68 65 20 64 69 66 66   Notice the diff
16256 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 77 65 65  erence.** betwee
16257 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74  n moveToLeftmost
16258 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f 52 69 67  () and moveToRig
16259 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 65 54  htmost().  moveT
1625a 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a 2a 20 66  oLeftmost().** f
1625b 69 6e 64 73 20 74 68 65 20 6c 65 66 74 2d 6d 6f  inds the left-mo
1625c 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68  st entry beneath
1625d 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 77 68 65   the *entry* whe
1625e 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 67 68 74  reas moveToRight
1625f 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20  most().** finds 
16260 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65  the right-most e
16261 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
16262 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54   *page*..**.** T
16263 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e  he right-most en
16264 74 72 79 20 69 73 20 74 68 65 20 6f 6e 65 20 77  try is the one w
16265 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20  ith the largest 
16266 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 74 0a 2a  key - the last.*
16267 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 6e 64 69  * key in ascendi
16268 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61  ng order..*/.sta
16269 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 69  tic int moveToRi
1626a 67 68 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72  ghtmost(BtCursor
1626b 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20   *pCur){.  Pgno 
1626c 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d  pgno;.  int rc =
1626d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65   SQLITE_OK;.  Me
1626e 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30  mPage *pPage = 0
1626f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
16270 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
16271 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
16272 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16273 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
16274 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49   while( rc==SQLI
16275 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65  TE_OK && !(pPage
16276 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
16277 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c  pCur->iPage])->l
16278 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20  eaf ){.    pgno 
16279 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
1627a 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
1627b 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20  hdrOffset+8]);. 
1627c 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70     pCur->aiIdx[p
1627d 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50  Cur->iPage] = pP
1627e 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20  age->nCell;.    
1627f 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64  rc = moveToChild
16280 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20  (pCur, pgno);.  
16281 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
16282 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 75  TE_OK ){.    pCu
16283 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
16284 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e 6e  Page] = pPage->n
16285 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 43 75 72  Cell-1;.    pCur
16286 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
16287 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69  ;.    pCur->vali
16288 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20  dNKey = 0;.  }. 
16289 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1628a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
1628b 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  r to the first e
1628c 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c  ntry in the tabl
1628d 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  e.  Return SQLIT
1628e 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65  E_OK.** on succe
1628f 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74  ss.  Set *pRes t
16290 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 73 6f  o 0 if the curso
16291 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74  r actually point
16292 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a  s to something.*
16293 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74  * or set *pRes t
16294 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 6c 65  o 1 if the table
16295 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51   is empty..*/.SQ
16296 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16297 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72   sqlite3BtreeFir
16298 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  st(BtCursor *pCu
16299 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20  r, int *pRes){. 
1629a 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65   int rc;..  asse
1629b 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
1629c 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
1629d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1629e 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d  mutex_held(pCur-
1629f 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74  >pBtree->db->mut
162a0 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f  ex) );.  rc = mo
162a1 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a  veToRoot(pCur);.
162a2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
162a3 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70  _OK ){.    if( p
162a4 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
162a5 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20  SOR_INVALID ){. 
162a6 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75       assert( pCu
162a7 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
162a8 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30  iPage]->nCell==0
162a9 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20   );.      *pRes 
162aa 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 1;.      rc = 
162ab 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
162ac 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
162ad 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65  rt( pCur->apPage
162ae 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
162af 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20  Cell>0 );.      
162b0 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20  *pRes = 0;.     
162b1 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74   rc = moveToLeft
162b2 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20  most(pCur);.    
162b3 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
162b4 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68  c;.}../* Move th
162b5 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
162b6 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  last entry in th
162b7 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e  e table.  Return
162b8 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e   SQLITE_OK.** on
162b9 20 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a   success.  Set *
162ba 70 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 65  pRes to 0 if the
162bb 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79   cursor actually
162bc 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74   points to somet
162bd 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a  hing.** or set *
162be 70 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 65  pRes to 1 if the
162bf 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e   table is empty.
162c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
162c1 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
162c2 72 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72  reeLast(BtCursor
162c3 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65   *pCur, int *pRe
162c4 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 0a  s){.  int rc;. .
162c5 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
162c6 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
162c7 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
162c8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
162c9 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64  (pCur->pBtree->d
162ca 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
162cb 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  /* If the cursor
162cc 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 73 20   already points 
162cd 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  to the last entr
162ce 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d  y, this is a no-
162cf 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 43 55 52  op. */.  if( CUR
162d0 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d  SOR_VALID==pCur-
162d1 3e 65 53 74 61 74 65 20 26 26 20 70 43 75 72 2d  >eState && pCur-
162d2 3e 61 74 4c 61 73 74 20 29 7b 0a 23 69 66 64 65  >atLast ){.#ifde
162d3 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
162d4 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b     /* This block
162d5 20 73 65 72 76 65 73 20 74 6f 20 61 73 73 65 72   serves to asser
162d6 74 28 29 20 74 68 61 74 20 74 68 65 20 63 75 72  t() that the cur
162d7 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 20  sor really does 
162d8 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74 6f  point .    ** to
162d9 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
162da 69 6e 20 74 68 65 20 62 2d 74 72 65 65 2e 20 2a  in the b-tree. *
162db 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20  /.    int ii;.  
162dc 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
162dd 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b 2b  Cur->iPage; ii++
162de 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
162df 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 69 5d   pCur->aiIdx[ii]
162e0 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  ==pCur->apPage[i
162e1 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  i]->nCell );.   
162e2 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
162e3 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
162e4 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e 61  >iPage]==pCur->a
162e5 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
162e6 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20  e]->nCell-1 );. 
162e7 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
162e8 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
162e9 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23 65  age]->leaf );.#e
162ea 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20  ndif.    return 
162eb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
162ec 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f    rc = moveToRoo
162ed 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  t(pCur);.  if( r
162ee 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
162ef 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49      if( CURSOR_I
162f0 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53  NVALID==pCur->eS
162f1 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73  tate ){.      as
162f2 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61  sert( pCur->apPa
162f3 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
162f4 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  >nCell==0 );.   
162f5 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20     *pRes = 1;.  
162f6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
162f7 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
162f8 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
162f9 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73  D );.      *pRes
162fa 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
162fb 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
162fc 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 43  (pCur);.      pC
162fd 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 72 63 3d  ur->atLast = rc=
162fe 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a 30 3b  =SQLITE_OK ?1:0;
162ff 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
16300 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f  urn rc;.}../* Mo
16301 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 73 6f  ve the cursor so
16302 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
16303 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65 61 72  to an entry near
16304 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73 70 65   the key .** spe
16305 63 69 66 69 65 64 20 62 79 20 70 49 64 78 4b 65  cified by pIdxKe
16306 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20 20 52  y or intKey.   R
16307 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20  eturn a success 
16308 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  code..**.** For 
16309 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 74  INTKEY tables, t
1630a 68 65 20 69 6e 74 4b 65 79 20 70 61 72 61 6d 65  he intKey parame
1630b 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 70 49  ter is used.  pI
1630c 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 20 62  dxKey .** must b
1630d 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 6e 64  e NULL.  For ind
1630e 65 78 20 74 61 62 6c 65 73 2c 20 70 49 64 78 4b  ex tables, pIdxK
1630f 65 79 20 69 73 20 75 73 65 64 20 61 6e 64 20 69  ey is used and i
16310 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67 6e 6f  ntKey.** is igno
16311 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  red..**.** If an
16312 20 65 78 61 63 74 20 6d 61 74 63 68 20 69 73 20   exact match is 
16313 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20  not found, then 
16314 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c  the cursor is al
16315 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70 6f 69  ways.** left poi
16316 6e 74 69 6e 67 20 61 74 20 61 20 6c 65 61 66 20  nting at a leaf 
16317 70 61 67 65 20 77 68 69 63 68 20 77 6f 75 6c 64  page which would
16318 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 72 79 20   hold the entry 
16319 69 66 20 69 74 0a 2a 2a 20 77 65 72 65 20 70 72  if it.** were pr
1631a 65 73 65 6e 74 2e 20 20 54 68 65 20 63 75 72 73  esent.  The curs
1631b 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74  or might point t
1631c 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20  o an entry that 
1631d 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 65 20  comes.** before 
1631e 6f 72 20 61 66 74 65 72 20 74 68 65 20 6b 65 79  or after the key
1631f 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 67  ..**.** An integ
16320 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  er is written in
16321 74 6f 20 2a 70 52 65 73 20 77 68 69 63 68 20 69  to *pRes which i
16322 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a  s the result of.
16323 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65  ** comparing the
16324 20 6b 65 79 20 77 69 74 68 20 74 68 65 20 65 6e   key with the en
16325 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 65  try to which the
16326 20 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a 20 70   cursor is .** p
16327 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65 20 6d 65  ointing.  The me
16328 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74  aning of the int
16329 65 67 65 72 20 77 72 69 74 74 65 6e 20 69 6e 74  eger written int
1632a 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20 61 73  o.** *pRes is as
1632b 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
1632c 20 20 20 20 2a 70 52 65 73 3c 30 20 20 20 20 20      *pRes<0     
1632d 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c   The cursor is l
1632e 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  eft pointing at 
1632f 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a  an entry that.**
16330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16331 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61    is smaller tha
16332 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79  n intKey/pIdxKey
16333 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65   or if the table
16334 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20   is empty.**    
16335 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
16336 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  d the cursor is 
16337 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70  therefore left p
16338 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e  oint to nothing.
16339 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73  .**.**     *pRes
1633a 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75 72 73  ==0     The curs
1633b 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  or is left point
1633c 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20  ing at an entry 
1633d 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  that.**         
1633e 20 20 20 20 20 20 20 20 20 65 78 61 63 74 6c 79           exactly
1633f 20 6d 61 74 63 68 65 73 20 69 6e 74 4b 65 79 2f   matches intKey/
16340 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 20  pIdxKey..**.**  
16341 20 20 20 2a 70 52 65 73 3e 30 20 20 20 20 20 20     *pRes>0      
16342 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
16343 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61  ft pointing at a
16344 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20  n entry that.** 
16345 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16346 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
16347 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a  intKey/pIdxKey..
16348 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  **.*/.SQLITE_PRI
16349 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1634a 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
1634b 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20  ked(.  BtCursor 
1634c 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20  *pCur,          
1634d 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f  /* The cursor to
1634e 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 20 55   be moved */.  U
1634f 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70  npackedRecord *p
16350 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 61 63  IdxKey, /* Unpac
16351 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f  ked index key */
16352 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c 20 20  .  i64 intKey,  
16353 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
16354 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a 2f 0a  he table key */.
16355 20 20 69 6e 74 20 62 69 61 73 52 69 67 68 74 2c    int biasRight,
16356 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
16357 20 74 72 75 65 2c 20 62 69 61 73 20 74 68 65 20   true, bias the 
16358 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 69  search to the hi
16359 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  gh end */.  int 
1635a 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20  *pRes           
1635b 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65       /* Write se
1635c 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72  arch results her
1635d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
1635e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
1635f 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
16360 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
16361 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
16362 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65  eld(pCur->pBtree
16363 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
16364 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72  .  /* If the cur
16365 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 70  sor is already p
16366 6f 73 69 74 69 6f 6e 65 64 20 61 74 20 74 68 65  ositioned at the
16367 20 70 6f 69 6e 74 20 77 65 20 61 72 65 20 74 72   point we are tr
16368 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d 6f 76  ying.  ** to mov
16369 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75 73 74 20  e to, then just 
1636a 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64  return without d
1636b 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 20 2a 2f  oing any work */
1636c 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
1636d 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
1636e 44 20 26 26 20 70 43 75 72 2d 3e 76 61 6c 69 64  D && pCur->valid
1636f 4e 4b 65 79 20 0a 20 20 20 26 26 20 70 43 75 72  NKey .   && pCur
16370 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74  ->apPage[0]->int
16371 4b 65 79 20 0a 20 20 29 7b 0a 20 20 20 20 69 66  Key .  ){.    if
16372 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65  ( pCur->info.nKe
16373 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  y==intKey ){.   
16374 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
16375 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16376 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
16377 69 66 28 20 70 43 75 72 2d 3e 61 74 4c 61 73 74  if( pCur->atLast
16378 20 26 26 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e   && pCur->info.n
16379 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 20  Key<intKey ){.  
1637a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a      *pRes = -1;.
1637b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1637c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
1637d 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  }..  rc = moveTo
1637e 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66  Root(pCur);.  if
1637f 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75  ( rc ){.    retu
16380 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73  rn rc;.  }.  ass
16381 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
16382 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29  e[pCur->iPage] )
16383 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16384 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
16385 50 61 67 65 5d 2d 3e 69 73 49 6e 69 74 20 29 3b  Page]->isInit );
16386 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
16387 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41  ate==CURSOR_INVA
16388 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 52 65 73  LID ){.    *pRes
16389 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73 65 72   = -1;.    asser
1638a 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  t( pCur->apPage[
1638b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43  pCur->iPage]->nC
1638c 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65  ell==0 );.    re
1638d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1638e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43    }.  assert( pC
1638f 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69  ur->apPage[0]->i
16390 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78 4b 65 79  ntKey || pIdxKey
16391 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20   );.  for(;;){. 
16392 20 20 20 69 6e 74 20 6c 77 72 2c 20 75 70 72 3b     int lwr, upr;
16393 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c 64 50 67  .    Pgno chldPg
16394 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  ;.    MemPage *p
16395 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
16396 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
16397 3b 0a 20 20 20 20 69 6e 74 20 63 20 3d 20 2d 31  ;.    int c = -1
16398 3b 20 20 2f 2a 20 70 52 65 73 20 72 65 74 75 72  ;  /* pRes retur
16399 6e 20 69 66 20 74 61 62 6c 65 20 69 73 20 65 6d  n if table is em
1639a 70 74 79 20 6d 75 73 74 20 62 65 20 2d 31 20 2a  pty must be -1 *
1639b 2f 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20  /.    lwr = 0;. 
1639c 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e     upr = pPage->
1639d 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28  nCell-1;.    if(
1639e 20 28 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79   (!pPage->intKey
1639f 20 26 26 20 70 49 64 78 4b 65 79 3d 3d 30 29 20   && pIdxKey==0) 
163a0 7c 7c 20 75 70 72 3c 30 20 29 7b 0a 20 20 20 20  || upr<0 ){.    
163a1 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
163a2 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
163a3 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69    goto moveto_fi
163a4 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nish;.    }.    
163a5 69 66 28 20 62 69 61 73 52 69 67 68 74 20 29 7b  if( biasRight ){
163a6 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49  .      pCur->aiI
163a7 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  dx[pCur->iPage] 
163a8 3d 20 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20  = (u16)upr;.    
163a9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75  }else{.      pCu
163aa 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
163ab 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75  Page] = (u16)((u
163ac 70 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20  pr+lwr)/2);.    
163ad 7d 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20  }.    for(;;){. 
163ae 20 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70       int idx = p
163af 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
163b0 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65  >iPage]; /* Inde
163b1 78 20 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c  x of current cel
163b2 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20  l in pPage */.  
163b3 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20      u8 *pCell;  
163b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
163b5 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
163b6 65 72 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65  er to current ce
163b7 6c 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a  ll in pPage */..
163b8 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f        pCur->info
163b9 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20  .nSize = 0;.    
163ba 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65    pCell = findCe
163bb 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b  ll(pPage, idx) +
163bc 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72   pPage->childPtr
163bd 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20  Size;.      if( 
163be 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
163bf 0a 20 20 20 20 20 20 20 20 69 36 34 20 6e 43 65  .        i64 nCe
163c0 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69  llKey;.        i
163c1 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74  f( pPage->hasDat
163c2 61 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75  a ){.          u
163c3 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20  32 dummy;.      
163c4 20 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74      pCell += get
163c5 56 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20  Varint32(pCell, 
163c6 64 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20  dummy);.        
163c7 7d 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72  }.        getVar
163c8 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a  int(pCell, (u64*
163c9 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20  )&nCellKey);.   
163ca 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65       if( nCellKe
163cb 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  y==intKey ){.   
163cc 20 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20         c = 0;.  
163cd 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
163ce 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20  nCellKey<intKey 
163cf 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d  ){.          c =
163d0 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c   -1;.        }el
163d1 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73  se{.          as
163d2 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69  sert( nCellKey>i
163d3 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20  ntKey );.       
163d4 20 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20     c = +1;.     
163d5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75     }.        pCu
163d6 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31  r->validNKey = 1
163d7 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  ;.        pCur->
163d8 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c  info.nKey = nCel
163d9 6c 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73  lKey;.      }els
163da 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e{.        /* Th
163db 65 20 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72  e maximum suppor
163dc 74 65 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73  ted page-size is
163dd 20 33 32 37 36 38 20 62 79 74 65 73 2e 20 54 68   32768 bytes. Th
163de 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20  is means that.  
163df 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78        ** the max
163e0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72  imum number of r
163e1 65 63 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72  ecord bytes stor
163e2 65 64 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42  ed on an index B
163e3 2d 54 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a  -Tree.        **
163e4 20 70 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74   page is at most
163e5 20 38 31 39 38 20 62 79 74 65 73 2c 20 77 68 69   8198 bytes, whi
163e6 63 68 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64  ch may be stored
163e7 20 61 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20   as a 2-byte.   
163e8 20 20 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20       ** varint. 
163e9 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  This information
163ea 20 69 73 20 75 73 65 64 20 74 6f 20 61 74 74 65   is used to atte
163eb 6d 70 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72  mpt to avoid par
163ec 73 69 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a  sing .        **
163ed 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c   the entire cell
163ee 20 62 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72   by checking for
163ef 20 74 68 65 20 63 61 73 65 73 20 77 68 65 72 65   the cases where
163f0 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a   the record is .
163f1 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65          ** store
163f2 64 20 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69  d entirely withi
163f3 6e 20 74 68 65 20 62 2d 74 72 65 65 20 70 61 67  n the b-tree pag
163f4 65 20 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20  e by inspecting 
163f5 74 68 65 20 66 69 72 73 74 20 0a 20 20 20 20 20  the first .     
163f6 20 20 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66     ** 2 bytes of
163f7 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20   the cell..     
163f8 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
163f9 74 20 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b  t nCell = pCell[
163fa 30 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0];.        if( 
163fb 21 28 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20  !(nCell & 0x80) 
163fc 26 26 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d  && nCell<=pPage-
163fd 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20  >maxLocal ){.   
163fe 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62         /* This b
163ff 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68  ranch runs if th
16400 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69  e record-size fi
16401 65 6c 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  eld of the cell 
16402 69 73 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a  is a.          *
16403 2a 20 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61  * single byte va
16404 72 69 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63  rint and the rec
16405 6f 72 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c  ord fits entirel
16406 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20  y on the main.  
16407 20 20 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65          ** b-tre
16408 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20  e page.  */.    
16409 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65        c = sqlite
1640a 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61  3VdbeRecordCompa
1640b 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a  re(nCell, (void*
1640c 29 26 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78  )&pCell[1], pIdx
1640d 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  Key);.        }e
1640e 6c 73 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b  lse if( !(pCell[
1640f 31 5d 20 26 20 30 78 38 30 29 20 0a 20 20 20 20  1] & 0x80) .    
16410 20 20 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20        && (nCell 
16411 3d 20 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c  = ((nCell&0x7f)<
16412 3c 37 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c  <7) + pCell[1])<
16413 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c  =pPage->maxLocal
16414 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20  .        ){.    
16415 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63        /* The rec
16416 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69  ord-size field i
16417 73 20 61 20 32 20 62 79 74 65 20 76 61 72 69 6e  s a 2 byte varin
16418 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64  t and the record
16419 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66   .          ** f
1641a 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20  its entirely on 
1641b 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20  the main b-tree 
1641c 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20  page.  */.      
1641d 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56      c = sqlite3V
1641e 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
1641f 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26  (nCell, (void*)&
16420 70 43 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65  pCell[2], pIdxKe
16421 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  y);.        }els
16422 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e{.          /* 
16423 54 68 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73  The record flows
16424 20 6f 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f   over onto one o
16425 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20  r more overflow 
16426 70 61 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20  pages. In.      
16427 20 20 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65      ** this case
16428 20 74 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20   the whole cell 
16429 6e 65 65 64 73 20 74 6f 20 62 65 20 70 61 72 73  needs to be pars
1642a 65 64 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c  ed, a buffer all
1642b 6f 63 61 74 65 64 0a 20 20 20 20 20 20 20 20 20  ocated.         
1642c 20 2a 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61   ** and accessPa
1642d 79 6c 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20  yload() used to 
1642e 72 65 74 72 69 65 76 65 20 74 68 65 20 72 65 63  retrieve the rec
1642f 6f 72 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20  ord into the.   
16430 20 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72         ** buffer
16431 20 62 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f   before VdbeReco
16432 72 64 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20  rdCompare() can 
16433 62 65 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20  be called. */.  
16434 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43          void *pC
16435 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  ellKey;.        
16436 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65    u8 * const pCe
16437 6c 6c 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d  llBody = pCell -
16438 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72   pPage->childPtr
16439 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Size;.          
1643a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
1643b 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20  eCellPtr(pPage, 
1643c 70 43 65 6c 6c 42 6f 64 79 2c 20 26 70 43 75 72  pCellBody, &pCur
1643d 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  ->info);.       
1643e 20 20 20 6e 43 65 6c 6c 20 3d 20 28 69 6e 74 29     nCell = (int)
1643f 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b  pCur->info.nKey;
16440 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c  .          pCell
16441 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Key = sqlite3Mal
16442 6c 6f 63 28 20 6e 43 65 6c 6c 20 29 3b 0a 20 20  loc( nCell );.  
16443 20 20 20 20 20 20 20 20 69 66 28 20 70 43 65 6c          if( pCel
16444 6c 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  lKey==0 ){.     
16445 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
16446 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
16447 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74        goto movet
16448 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20  o_finish;.      
16449 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1644a 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f  rc = accessPaylo
1644b 61 64 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 6c  ad(pCur, 0, nCel
1644c 6c 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61  l, (unsigned cha
1644d 72 2a 29 70 43 65 6c 6c 4b 65 79 2c 20 30 2c 20  r*)pCellKey, 0, 
1644e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20  0);.          c 
1644f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  = sqlite3VdbeRec
16450 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c  ordCompare(nCell
16451 2c 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78  , pCellKey, pIdx
16452 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Key);.          
16453 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65  sqlite3_free(pCe
16454 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20  llKey);.        
16455 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
16456 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
16457 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
16458 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20  .      if( c==0 
16459 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
1645a 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20  Page->intKey && 
1645b 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
1645c 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20            lwr = 
1645d 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75  idx;.          u
1645e 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20  pr = lwr - 1;.  
1645f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
16460 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
16461 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20          *pRes = 
16462 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  0;.          rc 
16463 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
16464 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65         goto move
16465 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20  to_finish;.     
16466 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
16467 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20     if( c<0 ){.  
16468 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b        lwr = idx+
16469 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
1646a 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64          upr = id
1646b 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  x-1;.      }.   
1646c 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29     if( lwr>upr )
1646d 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
1646e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1646f 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
16470 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28  >iPage] = (u16)(
16471 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20  (lwr+upr)/2);.  
16472 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
16473 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20  lwr==upr+1 );.  
16474 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
16475 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69  >isInit );.    i
16476 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  f( pPage->leaf )
16477 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d  {.      chldPg =
16478 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
16479 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43  ( lwr>=pPage->nC
1647a 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c  ell ){.      chl
1647b 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26  dPg = get4byte(&
1647c 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
1647d 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
1647e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1647f 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74      chldPg = get
16480 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70  4byte(findCell(p
16481 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20  Page, lwr));.   
16482 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50   }.    if( chldP
16483 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73  g==0 ){.      as
16484 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
16485 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
16486 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
16487 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20  ->iPage]->nCell 
16488 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 65  );.      if( pRe
16489 73 20 29 20 2a 70 52 65 73 20 3d 20 63 3b 0a 20  s ) *pRes = c;. 
1648a 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1648b 5f 4f 4b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  _OK;.      goto 
1648c 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
1648d 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 61     }.    pCur->a
1648e 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
1648f 5d 20 3d 20 28 75 31 36 29 6c 77 72 3b 0a 20 20  ] = (u16)lwr;.  
16490 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
16491 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72  ze = 0;.    pCur
16492 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b  ->validNKey = 0;
16493 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  .    rc = moveTo
16494 43 68 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64  Child(pCur, chld
16495 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  Pg);.    if( rc 
16496 29 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69  ) goto moveto_fi
16497 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f  nish;.  }.moveto
16498 5f 66 69 6e 69 73 68 3a 0a 20 20 72 65 74 75 72  _finish:.  retur
16499 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
1649a 6e 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  n this version o
1649b 66 20 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70  f BtreeMoveto, p
1649c 4b 65 79 20 69 73 20 61 20 70 61 63 6b 65 64 20  Key is a packed 
1649d 69 6e 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20  index record.** 
1649e 73 75 63 68 20 61 73 20 69 73 20 67 65 6e 65 72  such as is gener
1649f 61 74 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d  ated by the OP_M
164a0 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65  akeRecord opcode
164a1 2e 20 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a  .  Unpack the.**
164a2 20 72 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e   record and then
164a3 20 63 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74   call BtreeMovet
164a4 6f 55 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64  oUnpacked() to d
164a5 6f 20 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 53  o the work..*/.S
164a6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
164a7 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  t sqlite3BtreeMo
164a8 76 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72  veto(.  BtCursor
164a9 20 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43   *pCur,     /* C
164aa 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68  ursor open on th
164ab 65 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65  e btree to be se
164ac 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  arched */.  cons
164ad 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20  t void *pKey,   
164ae 2f 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66  /* Packed key if
164af 20 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e   the btree is an
164b0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20   index */.  i64 
164b1 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20  nKey,           
164b2 2f 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66  /* Integer key f
164b3 6f 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65  or tables.  Size
164b4 20 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64   of pKey for ind
164b5 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69  ices */.  int bi
164b6 61 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  as,           /*
164b7 20 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20   Bias search to 
164b8 74 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a  the high end */.
164b9 20 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20    int *pRes     
164ba 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73        /* Write s
164bb 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65  earch results he
164bc 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
164bd 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
164be 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20        /* Status 
164bf 63 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b  code */.  Unpack
164c0 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65  edRecord *pIdxKe
164c1 79 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64  y;   /* Unpacked
164c2 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20   index key */.  
164c3 63 68 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d  char aSpace[150]
164c4 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65  ;          /* Te
164c5 6d 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64  mp space for pId
164c6 78 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20  xKey - to avoid 
164c7 61 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 0a 20 20  a malloc */...  
164c8 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20  if( pKey ){.    
164c9 61 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69  assert( nKey==(i
164ca 36 34 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a  64)(int)nKey );.
164cb 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71      pIdxKey = sq
164cc 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55  lite3VdbeRecordU
164cd 6e 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79  npack(pCur->pKey
164ce 49 6e 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c  Info, (int)nKey,
164cf 20 70 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20   pKey,.         
164d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164d1 20 20 20 20 20 20 20 20 20 20 20 20 20 61 53 70               aSp
164d2 61 63 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61  ace, sizeof(aSpa
164d3 63 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49  ce));.    if( pI
164d4 64 78 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72  dxKey==0 ) retur
164d5 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
164d6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64    }else{.    pId
164d7 78 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  xKey = 0;.  }.  
164d8 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
164d9 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
164da 70 43 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e  pCur, pIdxKey, n
164db 4b 65 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29  Key, bias, pRes)
164dc 3b 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a  ;.  if( pKey ){.
164dd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44      sqlite3VdbeD
164de 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
164df 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20  ord(pIdxKey);.  
164e0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
164e1 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  .../*.** Return 
164e2 54 52 55 45 20 69 66 20 74 68 65 20 63 75 72 73  TRUE if the curs
164e3 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69  or is not pointi
164e4 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f  ng at an entry o
164e5 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  f the table..**.
164e6 2a 2a 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20  ** TRUE will be 
164e7 72 65 74 75 72 6e 65 64 20 61 66 74 65 72 20 61  returned after a
164e8 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
164e9 42 74 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65  BtreeNext() move
164ea 73 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61  s.** past the la
164eb 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
164ec 74 61 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33  table or sqlite3
164ed 42 74 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65  BtreePrev() move
164ee 73 20 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69  s past.** the fi
164ef 72 73 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45  rst entry.  TRUE
164f0 20 69 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65   is also returne
164f1 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  d if the table i
164f2 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49  s empty..*/.SQLI
164f3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
164f4 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 42  qlite3BtreeEof(B
164f5 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
164f6 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20    /* TODO: What 
164f7 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  if the cursor is
164f8 20 69 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49   in CURSOR_REQUI
164f9 52 45 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74  RESEEK but all t
164fa 61 62 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a  able entries.  *
164fb 2a 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65  * have been dele
164fc 74 65 64 3f 20 54 68 69 73 20 41 50 49 20 77 69  ted? This API wi
164fd 6c 6c 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67  ll need to chang
164fe 65 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65  e to return an e
164ff 72 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61  rror code.  ** a
16500 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f  s well as the bo
16501 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c  olean result val
16502 75 65 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72  ue..  */.  retur
16503 6e 20 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21  n (CURSOR_VALID!
16504 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a  =pCur->eState);.
16505 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
16506 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
16507 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66  nection handle f
16508 6f 72 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a  or a cursor..*/.
16509 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
1650a 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42  qlite3 *sqlite3B
1650b 74 72 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e  treeCursorDb(con
1650c 73 74 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  st BtCursor *pCu
1650d 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  r){.  assert( sq
1650e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1650f 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64  (pCur->pBtree->d
16510 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72  b->mutex) );.  r
16511 65 74 75 72 6e 20 70 43 75 72 2d 3e 70 42 74 72  eturn pCur->pBtr
16512 65 65 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ee->db;.}../*.**
16513 20 41 64 76 61 6e 63 65 20 74 68 65 20 63 75 72   Advance the cur
16514 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20  sor to the next 
16515 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74  entry in the dat
16516 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75  abase.  If.** su
16517 63 63 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65  ccessful then se
16518 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74  t *pRes=0.  If t
16519 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73  he cursor.** was
1651a 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e   already pointin
1651b 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e  g to the last en
1651c 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
1651d 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68  ase before.** th
1651e 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63  is routine was c
1651f 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20  alled, then set 
16520 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49  *pRes=1..*/.SQLI
16521 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16522 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28  qlite3BtreeNext(
16523 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
16524 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e  int *pRes){.  in
16525 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 64 78 3b  t rc;.  int idx;
16526 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
16527 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  e;..  assert( cu
16528 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
16529 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72  Cur) );.  rc = r
1652a 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
1652b 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66  tion(pCur);.  if
1652c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1652d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
1652e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1652f 70 52 65 73 21 3d 30 20 29 3b 0a 20 20 69 66 28  pRes!=0 );.  if(
16530 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d   CURSOR_INVALID=
16531 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b  =pCur->eState ){
16532 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a  .    *pRes = 1;.
16533 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16534 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  E_OK;.  }.  if( 
16535 70 43 75 72 2d 3e 73 6b 69 70 3e 30 20 29 7b 0a  pCur->skip>0 ){.
16536 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d      pCur->skip =
16537 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   0;.    *pRes = 
16538 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
16539 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70  LITE_OK;.  }.  p
1653a 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a  Cur->skip = 0;..
1653b 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
1653c 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1653d 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70  ge];.  idx = ++p
1653e 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
1653f 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72  >iPage];.  asser
16540 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  t( pPage->isInit
16541 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64   );.  assert( id
16542 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  x<=pPage->nCell 
16543 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f  );..  pCur->info
16544 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43  .nSize = 0;.  pC
16545 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20  ur->validNKey = 
16546 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50  0;.  if( idx>=pP
16547 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20  age->nCell ){.  
16548 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
16549 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  af ){.      rc =
1654a 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
1654b 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61  r, get4byte(&pPa
1654c 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d  ge->aData[pPage-
1654d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b  >hdrOffset+8]));
1654e 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
1654f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
16550 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74   rc = moveToLeft
16551 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20  most(pCur);.    
16552 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20    *pRes = 0;.   
16553 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
16554 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20    }.    do{.    
16555 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67    if( pCur->iPag
16556 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e==0 ){.        
16557 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20  *pRes = 1;.     
16558 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
16559 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
1655a 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
1655b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
1655c 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
1655d 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65  3BtreeMoveToPare
1655e 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20  nt(pCur);.      
1655f 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
16560 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
16561 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70  ];.    }while( p
16562 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
16563 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e  >iPage]>=pPage->
16564 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 2a 70 52  nCell );.    *pR
16565 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  es = 0;.    if( 
16566 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
16567 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
16568 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 75  te3BtreeNext(pCu
16569 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65  r, pRes);.    }e
1656a 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
1656b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
1656c 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
1656d 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b    }.  *pRes = 0;
1656e 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65  .  if( pPage->le
1656f 61 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  af ){.    return
16570 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
16571 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66    rc = moveToLef
16572 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 72  tmost(pCur);.  r
16573 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
16574 0a 2a 2a 20 53 74 65 70 20 74 68 65 20 63 75 72  .** Step the cur
16575 73 6f 72 20 74 6f 20 74 68 65 20 62 61 63 6b 20  sor to the back 
16576 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  to the previous 
16577 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74  entry in the dat
16578 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75  abase.  If.** su
16579 63 63 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65  ccessful then se
1657a 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74  t *pRes=0.  If t
1657b 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73  he cursor.** was
1657c 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e   already pointin
1657d 67 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  g to the first e
1657e 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61  ntry in the data
1657f 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74  base before.** t
16580 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20  his routine was 
16581 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74  called, then set
16582 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c   *pRes=1..*/.SQL
16583 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
16584 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
16585 69 6f 75 73 28 42 74 43 75 72 73 6f 72 20 2a 70  ious(BtCursor *p
16586 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b  Cur, int *pRes){
16587 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d  .  int rc;.  Mem
16588 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20  Page *pPage;..  
16589 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
1658a 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
1658b 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65  ;.  rc = restore
1658c 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
1658d 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  Cur);.  if( rc!=
1658e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1658f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
16590 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d    pCur->atLast =
16591 20 30 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52   0;.  if( CURSOR
16592 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e  _INVALID==pCur->
16593 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70  eState ){.    *p
16594 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74  Res = 1;.    ret
16595 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
16596 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73   }.  if( pCur->s
16597 6b 69 70 3c 30 20 29 7b 0a 20 20 20 20 70 43 75  kip<0 ){.    pCu
16598 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20  r->skip = 0;.   
16599 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20   *pRes = 0;.    
1659a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1659b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b  ;.  }.  pCur->sk
1659c 69 70 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65  ip = 0;..  pPage
1659d 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
1659e 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
1659f 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
165a0 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21  sInit );.  if( !
165a1 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
165a2 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75     int idx = pCu
165a3 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
165a4 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20  Page];.    rc = 
165a5 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
165a6 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43  , get4byte(findC
165a7 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29  ell(pPage, idx))
165a8 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
165a9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
165aa 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
165ab 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
165ac 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pCur);.  }else{
165ad 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72  .    while( pCur
165ae 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
165af 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  age]==0 ){.     
165b0 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65   if( pCur->iPage
165b1 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
165b2 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55  Cur->eState = CU
165b3 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20  RSOR_INVALID;.  
165b4 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b        *pRes = 1;
165b5 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
165b6 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
165b7 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
165b8 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e  BtreeMoveToParen
165b9 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20  t(pCur);.    }. 
165ba 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53     pCur->info.nS
165bb 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75  ize = 0;.    pCu
165bc 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30  r->validNKey = 0
165bd 3b 0a 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49  ;..    pCur->aiI
165be 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  dx[pCur->iPage]-
165bf 2d 3b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70  -;.    pPage = p
165c0 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
165c1 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66  ->iPage];.    if
165c2 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
165c3 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  && !pPage->leaf 
165c4 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
165c5 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f  lite3BtreePrevio
165c6 75 73 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a  us(pCur, pRes);.
165c7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
165c8 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
165c9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52  .    }.  }.  *pR
165ca 65 73 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  es = 0;.  return
165cb 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c   rc;.}../*.** Al
165cc 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67  locate a new pag
165cd 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  e from the datab
165ce 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
165cf 54 68 65 20 6e 65 77 20 70 61 67 65 20 69 73 20  The new page is 
165d0 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e  marked as dirty.
165d1 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64    (In other word
165d2 73 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  s, sqlite3PagerW
165d3 72 69 74 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c  rite().** has al
165d4 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65  ready been calle
165d5 64 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67  d on the new pag
165d6 65 2e 29 20 20 54 68 65 20 6e 65 77 20 70 61 67  e.)  The new pag
165d7 65 20 68 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65  e has also.** be
165d8 65 6e 20 72 65 66 65 72 65 6e 63 65 64 20 61 6e  en referenced an
165d9 64 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f  d the calling ro
165da 75 74 69 6e 65 20 69 73 20 72 65 73 70 6f 6e 73  utine is respons
165db 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67  ible for calling
165dc 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72  .** sqlite3Pager
165dd 55 6e 72 65 66 28 29 20 6f 6e 20 74 68 65 20 6e  Unref() on the n
165de 65 77 20 70 61 67 65 20 77 68 65 6e 20 69 74 20  ew page when it 
165df 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53  is done..**.** S
165e0 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
165e1 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e  rned on success.
165e2 20 20 41 6e 79 20 6f 74 68 65 72 20 72 65 74 75    Any other retu
165e3 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74  rn value indicat
165e4 65 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20  es.** an error. 
165e5 20 2a 70 70 50 61 67 65 20 61 6e 64 20 2a 70 50   *ppPage and *pP
165e6 67 6e 6f 20 61 72 65 20 75 6e 64 65 66 69 6e 65  gno are undefine
165e7 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f  d in the event o
165e8 66 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44  f an error..** D
165e9 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c  o not invoke sql
165ea 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29  ite3PagerUnref()
165eb 20 6f 6e 20 2a 70 70 50 61 67 65 20 69 66 20 61   on *ppPage if a
165ec 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
165ed 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ned..**.** If th
165ee 65 20 22 6e 65 61 72 62 79 22 20 70 61 72 61 6d  e "nearby" param
165ef 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74  eter is not 0, t
165f0 68 65 6e 20 61 20 28 66 65 65 62 6c 65 29 20 65  hen a (feeble) e
165f1 66 66 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f  ffort is made to
165f2 20 0a 2a 2a 20 6c 6f 63 61 74 65 20 61 20 70 61   .** locate a pa
165f3 67 65 20 63 6c 6f 73 65 20 74 6f 20 74 68 65 20  ge close to the 
165f4 70 61 67 65 20 6e 75 6d 62 65 72 20 22 6e 65 61  page number "nea
165f5 72 62 79 22 2e 20 20 54 68 69 73 20 63 61 6e 20  rby".  This can 
165f6 62 65 20 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a  be used in an.**
165f7 20 61 74 74 65 6d 70 74 20 74 6f 20 6b 65 65 70   attempt to keep
165f8 20 72 65 6c 61 74 65 64 20 70 61 67 65 73 20 63   related pages c
165f9 6c 6f 73 65 20 74 6f 20 65 61 63 68 20 6f 74 68  lose to each oth
165fa 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  er in the databa
165fb 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63  se file,.** whic
165fc 68 20 69 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61  h in turn can ma
165fd 6b 65 20 64 61 74 61 62 61 73 65 20 61 63 63 65  ke database acce
165fe 73 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a  ss faster..**.**
165ff 20 49 66 20 74 68 65 20 22 65 78 61 63 74 22 20   If the "exact" 
16600 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74  parameter is not
16601 20 30 2c 20 61 6e 64 20 74 68 65 20 70 61 67 65   0, and the page
16602 2d 6e 75 6d 62 65 72 20 6e 65 61 72 62 79 20 65  -number nearby e
16603 78 69 73 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65  xists .** anywhe
16604 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c  re on the free-l
16605 69 73 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ist, then it is 
16606 67 75 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65  guarenteed to be
16607 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a   returned. This.
16608 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  ** is only used 
16609 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64  by auto-vacuum d
1660a 61 74 61 62 61 73 65 73 20 77 68 65 6e 20 61 6c  atabases when al
1660b 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 74  locating a new t
1660c 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
1660d 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  int allocateBtre
1660e 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65  ePage(.  BtShare
1660f 64 20 2a 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61  d *pBt, .  MemPa
16610 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20  ge **ppPage, .  
16611 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20  Pgno *pPgno, .  
16612 50 67 6e 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75  Pgno nearby,.  u
16613 38 20 65 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d  8 exact.){.  Mem
16614 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20  Page *pPage1;.  
16615 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b  int rc;.  int n;
16616 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
16617 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66  f pages on the f
16618 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  reelist */.  int
16619 20 6b 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65   k;     /* Numbe
1661a 72 20 6f 66 20 6c 65 61 76 65 73 20 6f 6e 20 74  r of leaves on t
1661b 68 65 20 74 72 75 6e 6b 20 6f 66 20 74 68 65 20  he trunk of the 
1661c 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65  freelist */.  Me
1661d 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20  mPage *pTrunk = 
1661e 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  0;.  MemPage *pP
1661f 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20  revTrunk = 0;.. 
16620 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16621 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
16622 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61  >mutex) );.  pPa
16623 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65  ge1 = pBt->pPage
16624 31 3b 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74  1;.  n = get4byt
16625 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
16626 5b 33 36 5d 29 3b 0a 20 20 69 66 28 20 6e 3e 30  [36]);.  if( n>0
16627 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65   ){.    /* There
16628 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68   are pages on th
16629 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75  e freelist.  Reu
1662a 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20  se one of those 
1662b 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67  pages. */.    Pg
1662c 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75  no iTrunk;.    u
1662d 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30  8 searchList = 0
1662e 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65  ; /* If the free
1662f 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73 65  -list must be se
16630 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72  arched for 'near
16631 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20  by' */.    .    
16632 2f 2a 20 49 66 20 74 68 65 20 27 65 78 61 63 74  /* If the 'exact
16633 27 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 20  ' parameter was 
16634 74 72 75 65 20 61 6e 64 20 61 20 71 75 65 72 79  true and a query
16635 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   of the pointer-
16636 6d 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73  map.    ** shows
16637 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 27   that the page '
16638 6e 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77  nearby' is somew
16639 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65  here on the free
1663a 2d 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20  -list, then.    
1663b 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69  ** the entire-li
1663c 73 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63  st will be searc
1663d 68 65 64 20 66 6f 72 20 74 68 61 74 20 70 61 67  hed for that pag
1663e 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65  e..    */.#ifnde
1663f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
16640 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
16641 20 65 78 61 63 74 20 26 26 20 6e 65 61 72 62 79   exact && nearby
16642 3c 3d 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  <=pagerPagecount
16643 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 75  (pBt) ){.      u
16644 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 61  8 eType;.      a
16645 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e 30 20  ssert( nearby>0 
16646 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
16647 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
16648 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   );.      rc = p
16649 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 6e 65  trmapGet(pBt, ne
1664a 61 72 62 79 2c 20 26 65 54 79 70 65 2c 20 30 29  arby, &eType, 0)
1664b 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
1664c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1664d 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
1664e 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a  MAP_FREEPAGE ){.
1664f 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69          searchLi
16650 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  st = 1;.      }.
16651 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e        *pPgno = n
16652 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 6e  earby;.    }.#en
16653 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72  dif..    /* Decr
16654 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c  ement the free-l
16655 69 73 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20  ist count by 1. 
16656 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68  Set iTrunk to th
16657 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20  e index of the. 
16658 20 20 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65     ** first free
16659 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
1665a 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20  . iPrevTrunk is 
1665b 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20  initially 1..   
1665c 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
1665d 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
1665e 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b  Page1->pDbPage);
1665f 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
16660 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74  turn rc;.    put
16661 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
16662 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a  Data[36], n-1);.
16663 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65  .    /* The code
16664 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f   within this loo
16665 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e  p is run only on
16666 63 65 20 69 66 20 74 68 65 20 27 73 65 61 72 63  ce if the 'searc
16667 68 4c 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a  hList' variable.
16668 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72      ** is not tr
16669 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ue. Otherwise, i
1666a 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20  t runs once for 
1666b 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20  each trunk-page 
1666c 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72  on the.    ** fr
1666d 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68  ee-list until th
1666e 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20  e page 'nearby' 
1666f 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 20  is located..    
16670 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  */.    do {.    
16671 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 70    pPrevTrunk = p
16672 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28  Trunk;.      if(
16673 20 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20   pPrevTrunk ){. 
16674 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20         iTrunk = 
16675 67 65 74 34 62 79 74 65 28 26 70 50 72 65 76 54  get4byte(&pPrevT
16676 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b  runk->aData[0]);
16677 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
16678 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67        iTrunk = g
16679 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d  et4byte(&pPage1-
1667a 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20  >aData[32]);.   
1667b 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
1667c 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
1667d 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c  age(pBt, iTrunk,
1667e 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20   &pTrunk, 0);.  
1667f 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
16680 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30        pTrunk = 0
16681 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  ;.        goto e
16682 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
16683 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
16684 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70   k = get4byte(&p
16685 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29  Trunk->aData[4])
16686 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30  ;.      if( k==0
16687 20 26 26 20 21 73 65 61 72 63 68 4c 69 73 74 20   && !searchList 
16688 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
16689 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c  e trunk has no l
1668a 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69  eaves and the li
1668b 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  st is not being 
1668c 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20  searched. .     
1668d 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74     ** So extract
1668e 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20   the trunk page 
1668f 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69  itself and use i
16690 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a  t as the newly .
16691 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63          ** alloc
16692 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20  ated page */.   
16693 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72       assert( pPr
16694 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20  evTrunk==0 );.  
16695 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
16696 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72  e3PagerWrite(pTr
16697 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  unk->pDbPage);. 
16698 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
16699 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1669a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
1669b 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
1669c 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54       *pPgno = iT
1669d 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65  runk;.        me
1669e 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44  mcpy(&pPage1->aD
1669f 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b  ata[32], &pTrunk
166a0 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a  ->aData[0], 4);.
166a1 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 20          *ppPage 
166a2 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20  = pTrunk;.      
166a3 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20    pTrunk = 0;.  
166a4 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c        TRACE(("AL
166a5 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b  LOCATE: %d trunk
166a6 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73   - %d free pages
166a7 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f   left\n", *pPgno
166a8 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d  , n-1));.      }
166a9 65 6c 73 65 20 69 66 28 20 6b 3e 70 42 74 2d 3e  else if( k>pBt->
166aa 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32  usableSize/4 - 2
166ab 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 56   ){.        /* V
166ac 61 6c 75 65 20 6f 66 20 6b 20 69 73 20 6f 75 74  alue of k is out
166ad 20 6f 66 20 72 61 6e 67 65 2e 20 20 44 61 74 61   of range.  Data
166ae 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  base corruption 
166af 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  */.        rc = 
166b0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
166b1 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  KPT;.        got
166b2 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
166b3 61 67 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  age;.#ifndef SQL
166b4 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
166b5 55 55 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  UUM.      }else 
166b6 69 66 28 20 73 65 61 72 63 68 4c 69 73 74 20 26  if( searchList &
166b7 26 20 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b  & nearby==iTrunk
166b8 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
166b9 68 65 20 6c 69 73 74 20 69 73 20 62 65 69 6e 67  he list is being
166ba 20 73 65 61 72 63 68 65 64 20 61 6e 64 20 74 68   searched and th
166bb 69 73 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73  is trunk page is
166bc 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20   the page.      
166bd 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65    ** to allocate
166be 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  , regardless of 
166bf 77 68 65 74 68 65 72 20 69 74 20 68 61 73 20 6c  whether it has l
166c0 65 61 76 65 73 2e 0a 20 20 20 20 20 20 20 20 2a  eaves..        *
166c1 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
166c2 28 20 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b  ( *pPgno==iTrunk
166c3 20 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50   );.        *ppP
166c4 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20  age = pTrunk;.  
166c5 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74        searchList
166c6 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63   = 0;.        rc
166c7 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
166c8 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62  rite(pTrunk->pDb
166c9 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69  Page);.        i
166ca 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
166cb 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
166cc 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
166cd 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
166ce 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   k==0 ){.       
166cf 20 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75     if( !pPrevTru
166d0 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nk ){.          
166d1 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31    memcpy(&pPage1
166d2 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54  ->aData[32], &pT
166d3 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20  runk->aData[0], 
166d4 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  4);.          }e
166d5 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
166d6 20 6d 65 6d 63 70 79 28 26 70 50 72 65 76 54 72   memcpy(&pPrevTr
166d7 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26  unk->aData[0], &
166d8 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  pTrunk->aData[0]
166d9 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 4);.          
166da 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
166db 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
166dc 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20  e trunk page is 
166dd 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20  required by the 
166de 63 61 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f  caller but it co
166df 6e 74 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20  ntains .        
166e0 20 20 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f    ** pointers to
166e1 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65   free-list leave
166e2 73 2e 20 54 68 65 20 66 69 72 73 74 20 6c 65 61  s. The first lea
166e3 66 20 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e  f becomes a trun
166e4 6b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70  k.          ** p
166e5 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65  age in this case
166e6 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
166e7 20 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65           MemPage
166e8 20 2a 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20   *pNewTrunk;.   
166e9 20 20 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77         Pgno iNew
166ea 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65  Trunk = get4byte
166eb 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  (&pTrunk->aData[
166ec 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  8]);.          r
166ed 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
166ee 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4e 65  GetPage(pBt, iNe
166ef 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 75  wTrunk, &pNewTru
166f0 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  nk, 0);.        
166f1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
166f2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
166f3 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
166f4 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
166f5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
166f6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
166f7 65 72 57 72 69 74 65 28 70 4e 65 77 54 72 75 6e  erWrite(pNewTrun
166f8 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
166f9 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
166fa 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
166fb 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
166fc 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a  age(pNewTrunk);.
166fd 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
166fe 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61   end_allocate_pa
166ff 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ge;.          }.
16700 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
16701 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61  (&pNewTrunk->aDa
16702 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e  ta[0], &pTrunk->
16703 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20  aData[0], 4);.  
16704 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
16705 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61  (&pNewTrunk->aDa
16706 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20  ta[4], k-1);.   
16707 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70         memcpy(&p
16708 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  NewTrunk->aData[
16709 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61  8], &pTrunk->aDa
1670a 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 29  ta[12], (k-1)*4)
1670b 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65  ;.          rele
1670c 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e  asePage(pNewTrun
1670d 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  k);.          if
1670e 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b  ( !pPrevTrunk ){
1670f 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
16710 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
16711 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
16712 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  ge1->pDbPage) );
16713 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 74  .            put
16714 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
16715 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 72  Data[32], iNewTr
16716 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20  unk);.          
16717 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
16718 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
16719 61 67 65 72 57 72 69 74 65 28 70 50 72 65 76 54  agerWrite(pPrevT
1671a 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a  runk->pDbPage);.
1671b 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1671c 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  rc ){.          
1671d 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
1671e 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
1671f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
16720 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
16721 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74  pPrevTrunk->aDat
16722 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29  a[0], iNewTrunk)
16723 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
16724 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
16725 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20  pTrunk = 0;.    
16726 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f      TRACE(("ALLO
16727 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d  CATE: %d trunk -
16728 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c   %d free pages l
16729 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20  eft\n", *pPgno, 
1672a 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20  n-1));.#endif.  
1672b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1672c 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20     /* Extract a 
1672d 6c 65 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72  leaf from the tr
1672e 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  unk */.        i
1672f 6e 74 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20  nt closest;.    
16730 20 20 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a      Pgno iPage;.
16731 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64          unsigned
16732 20 63 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70   char *aData = p
16733 54 72 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20  Trunk->aData;.  
16734 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
16735 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72  e3PagerWrite(pTr
16736 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  unk->pDbPage);. 
16737 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
16738 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
16739 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
1673a 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
1673b 20 20 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e       if( nearby>
1673c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  0 ){.          i
1673d 6e 74 20 69 2c 20 64 69 73 74 3b 0a 20 20 20 20  nt i, dist;.    
1673e 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20        closest = 
1673f 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 69 73  0;.          dis
16740 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44  t = get4byte(&aD
16741 61 74 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79  ata[8]) - nearby
16742 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
16743 64 69 73 74 3c 30 20 29 20 64 69 73 74 20 3d 20  dist<0 ) dist = 
16744 2d 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20  -dist;.         
16745 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69   for(i=1; i<k; i
16746 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
16747 20 69 6e 74 20 64 32 20 3d 20 67 65 74 34 62 79   int d2 = get4by
16748 74 65 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d  te(&aData[8+i*4]
16749 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20  ) - nearby;.    
1674a 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 30          if( d2<0
1674b 20 29 20 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20   ) d2 = -d2;.   
1674c 20 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c           if( d2<
1674d 64 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  dist ){.        
1674e 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20        closest = 
1674f 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  i;.             
16750 20 64 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20   dist = d2;.    
16751 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
16752 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
16753 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
16754 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20  losest = 0;.    
16755 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69      }..        i
16756 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  Page = get4byte(
16757 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74  &aData[8+closest
16758 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66  *4]);.        if
16759 28 20 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c  ( !searchList ||
1675a 20 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29   iPage==nearby )
1675b 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
1675c 6e 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20  noContent;.     
1675d 20 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b       Pgno nPage;
1675e 0a 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e  .          *pPgn
1675f 6f 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20  o = iPage;.     
16760 20 20 20 20 20 6e 50 61 67 65 20 3d 20 70 61 67       nPage = pag
16761 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
16762 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
16763 2a 70 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b 0a  *pPgno>nPage ){.
16764 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
16765 72 65 65 20 70 61 67 65 20 6f 66 66 20 74 68 65  ree page off the
16766 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65   end of the file
16767 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
16768 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
16769 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
1676a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
1676b 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20  llocate_page;.  
1676c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1676d 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f      TRACE(("ALLO
1676e 43 41 54 45 3a 20 25 64 20 77 61 73 20 6c 65 61  CATE: %d was lea
1676f 66 20 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 72  f %d of %d on tr
16770 75 6e 6b 20 25 64 22 0a 20 20 20 20 20 20 20 20  unk %d".        
16771 20 20 20 20 20 20 20 20 20 22 3a 20 25 64 20 6d           ": %d m
16772 6f 72 65 20 66 72 65 65 20 70 61 67 65 73 5c 6e  ore free pages\n
16773 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
16774 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73      *pPgno, clos
16775 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b  est+1, k, pTrunk
16776 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20  ->pgno, n-1));. 
16777 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f           if( clo
16778 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20  sest<k-1 ){.    
16779 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
1677a 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a  aData[8+closest*
1677b 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34  4], &aData[4+k*4
1677c 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20  ], 4);.         
1677d 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 75 74   }.          put
1677e 34 62 79 74 65 28 26 61 44 61 74 61 5b 34 5d 2c  4byte(&aData[4],
1677f 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20   k-1);.         
16780 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16781 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
16782 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65  (pTrunk->pDbPage
16783 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) );.          n
16784 6f 43 6f 6e 74 65 6e 74 20 3d 20 21 62 74 72 65  oContent = !btre
16785 65 47 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70  eGetHasContent(p
16786 42 74 2c 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20  Bt, *pPgno);.   
16787 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
16788 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
16789 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50  pBt, *pPgno, ppP
1678a 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b  age, noContent);
1678b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
1678c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1678d 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
1678e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1678f 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44  te((*ppPage)->pD
16790 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
16791 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
16792 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
16793 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
16794 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20  ge(*ppPage);.   
16795 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
16796 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
16797 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b   searchList = 0;
16798 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
16799 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65   }.      release
1679a 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29  Page(pPrevTrunk)
1679b 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 75  ;.      pPrevTru
1679c 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69  nk = 0;.    }whi
1679d 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 20 29  le( searchList )
1679e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
1679f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70  * There are no p
167a0 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65  ages on the free
167a1 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20  list, so create 
167a2 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 74 68  a new page at th
167a3 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20  e.    ** end of 
167a4 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  the file */.    
167a5 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65  int nPage = page
167a6 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b  rPagecount(pBt);
167a7 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50  .    *pPgno = nP
167a8 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66  age + 1;..    if
167a9 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e  ( *pPgno==PENDIN
167aa 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
167ab 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e   ){.      (*pPgn
167ac 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  o)++;.    }..#if
167ad 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
167ae 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
167af 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
167b0 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53  uum && PTRMAP_IS
167b1 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f  PAGE(pBt, *pPgno
167b2 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  ) ){.      /* If
167b3 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74   *pPgno refers t
167b4 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  o a pointer-map 
167b5 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74  page, allocate t
167b6 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20  wo new pages.   
167b7 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64     ** at the end
167b8 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73   of the file ins
167b9 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65  tead of one. The
167ba 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64   first allocated
167bb 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62   page.      ** b
167bc 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69  ecomes a new poi
167bd 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74  nter-map page, t
167be 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65  he second is use
167bf 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e  d by the caller.
167c0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
167c1 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30  MemPage *pPg = 0
167c2 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22  ;.      TRACE(("
167c3 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f  ALLOCATE: %d fro
167c4 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70  m end of file (p
167c5 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29  ointer-map page)
167c6 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20  \n", *pPgno));. 
167c7 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50       assert( *pP
167c8 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54  gno!=PENDING_BYT
167c9 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20  E_PAGE(pBt) );. 
167ca 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
167cb 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
167cc 74 2c 20 2a 70 50 67 6e 6f 2c 20 26 70 50 67 2c  t, *pPgno, &pPg,
167cd 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
167ce 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
167cf 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
167d0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
167d1 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  Pg->pDbPage);.  
167d2 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
167d3 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a  e(pPg);.      }.
167d4 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
167d5 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
167d6 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20  (*pPgno)++;.    
167d7 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45    if( *pPgno==PE
167d8 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
167d9 70 42 74 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29  pBt) ){ (*pPgno)
167da 2b 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64  ++; }.    }.#end
167db 69 66 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  if..    assert( 
167dc 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f  *pPgno!=PENDING_
167dd 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
167de 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
167df 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
167e0 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61  Bt, *pPgno, ppPa
167e1 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ge, 0);.    if( 
167e2 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
167e3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
167e4 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 50  PagerWrite((*ppP
167e5 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a  age)->pDbPage);.
167e6 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
167e7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
167e8 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61  eleasePage(*ppPa
167e9 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54  ge);.    }.    T
167ea 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a  RACE(("ALLOCATE:
167eb 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20   %d from end of 
167ec 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29  file\n", *pPgno)
167ed 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  );.  }..  assert
167ee 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e  ( *pPgno!=PENDIN
167ef 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
167f0 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74   );..end_allocat
167f1 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 73  e_page:.  releas
167f2 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20  ePage(pTrunk);. 
167f3 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 72   releasePage(pPr
167f4 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 20  evTrunk);.  if( 
167f5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
167f6 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
167f7 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e  PagerPageRefcoun
167f8 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62  t((*ppPage)->pDb
167f9 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20 20 20  Page)>1 ){.     
167fa 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70   releasePage(*pp
167fb 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74  Page);.      ret
167fc 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
167fd 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20  PT_BKPT;.    }. 
167fe 20 20 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 73     (*ppPage)->is
167ff 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Init = 0;.  }.  
16800 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
16801 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
16802 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64  n is used to add
16803 20 70 61 67 65 20 69 50 61 67 65 20 74 6f 20 74   page iPage to t
16804 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
16805 20 66 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20   free-list. .** 
16806 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68  It is assumed th
16807 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  at the page is n
16808 6f 74 20 61 6c 72 65 61 64 79 20 61 20 70 61 72  ot already a par
16809 74 20 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69  t of the free-li
1680a 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61  st..**.** The va
1680b 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68  lue passed as th
1680c 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
1680d 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
1680e 6f 6e 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a  on is optional..
1680f 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72  ** If the caller
16810 20 68 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65   happens to have
16811 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
16812 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74  e MemPage object
16813 20 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69   .** correspondi
16814 6e 67 20 74 6f 20 70 61 67 65 20 69 50 61 67 65  ng to page iPage
16815 20 68 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70   handy, it may p
16816 61 73 73 20 69 74 20 61 73 20 74 68 65 20 73 65  ass it as the se
16817 63 6f 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20  cond value. .** 
16818 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61  Otherwise, it ma
16819 79 20 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  y pass NULL..**.
1681a 2a 2a 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20  ** If a pointer 
1681b 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a  to a MemPage obj
1681c 65 63 74 20 69 73 20 70 61 73 73 65 64 20 61 73  ect is passed as
1681d 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
1681e 6d 65 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66  ment,.** its ref
1681f 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20  erence count is 
16820 6e 6f 74 20 61 6c 74 65 72 65 64 20 62 79 20 74  not altered by t
16821 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  his function..*/
16822 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65  .static int free
16823 50 61 67 65 32 28 42 74 53 68 61 72 65 64 20 2a  Page2(BtShared *
16824 70 42 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d  pBt, MemPage *pM
16825 65 6d 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61  emPage, Pgno iPa
16826 67 65 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ge){.  MemPage *
16827 70 54 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20  pTrunk = 0;     
16828 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72             /* Fr
16829 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61  ee-list trunk pa
1682a 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72  ge */.  Pgno iTr
1682b 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20  unk = 0;        
1682c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1682d 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72  age number of fr
1682e 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61  ee-list trunk pa
1682f 67 65 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65  ge */ .  MemPage
16830 20 2a 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e   *pPage1 = pBt->
16831 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20  pPage1;      /* 
16832 4c 6f 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20  Local reference 
16833 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d  to page 1 */.  M
16834 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20  emPage *pPage;  
16835 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16836 20 20 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67     /* Page being
16837 20 66 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e   freed. May be N
16838 55 4c 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ULL. */.  int rc
16839 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1683a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1683b 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a   Return Code */.
1683c 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20    int nFree;    
1683d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1683e 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c        /* Initial
1683f 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
16840 20 6f 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f   on free-list */
16841 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
16842 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
16843 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
16844 61 73 73 65 72 74 28 20 69 50 61 67 65 3e 31 20  assert( iPage>1 
16845 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d  );.  assert( !pM
16846 65 6d 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61  emPage || pMemPa
16847 67 65 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20  ge->pgno==iPage 
16848 29 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61  );..  if( pMemPa
16849 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20  ge ){.    pPage 
1684a 3d 20 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20  = pMemPage;.    
1684b 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28  sqlite3PagerRef(
1684c 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
1684d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50  .  }else{.    pP
1684e 61 67 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c  age = btreePageL
1684f 6f 6f 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65  ookup(pBt, iPage
16850 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63  );.  }..  /* Inc
16851 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20  rement the free 
16852 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50  page count on pP
16853 61 67 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73  age1 */.  rc = s
16854 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
16855 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65  (pPage1->pDbPage
16856 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f  );.  if( rc ) go
16857 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b  to freepage_out;
16858 0a 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62  .  nFree = get4b
16859 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
1685a 74 61 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62  ta[36]);.  put4b
1685b 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
1685c 74 61 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29  ta[36], nFree+1)
1685d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
1685e 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20  _SECURE_DELETE. 
1685f 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54   /* If the SQLIT
16860 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20  E_SECURE_DELETE 
16861 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74  compile-time opt
16862 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ion is enabled, 
16863 74 68 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73  then.  ** always
16864 20 66 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65   fully overwrite
16865 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61   deleted informa
16866 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e  tion with zeros.
16867 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50  .  */.  if( (!pP
16868 61 67 65 20 26 26 20 28 72 63 20 3d 20 73 71 6c  age && (rc = sql
16869 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
1686a 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50  (pBt, iPage, &pP
1686b 61 67 65 2c 20 30 29 29 29 0a 20 20 20 7c 7c 20  age, 0))).   || 
1686c 20 20 20 20 20 20 20 20 20 20 20 28 72 63 20 3d             (rc =
1686d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1686e 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  te(pPage->pDbPag
1686f 65 29 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74  e)).  ){.    got
16870 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a  o freepage_out;.
16871 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 50 61    }.  memset(pPa
16872 67 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50  ge->aData, 0, pP
16873 61 67 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69  age->pBt->pageSi
16874 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  ze);.#endif..  /
16875 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
16876 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d  e supports auto-
16877 76 61 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e  vacuum, write an
16878 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f   entry in the po
16879 69 6e 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 74  inter-map.  ** t
1687a 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  o indicate that 
1687b 74 68 65 20 70 61 67 65 20 69 73 20 66 72 65 65  the page is free
1687c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 53 41  ..  */.  if( ISA
1687d 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  UTOVACUUM ){.   
1687e 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28   rc = ptrmapPut(
1687f 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d  pBt, iPage, PTRM
16880 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 29 3b  AP_FREEPAGE, 0);
16881 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
16882 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b  to freepage_out;
16883 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 6d  .  }..  /* Now m
16884 61 6e 69 70 75 6c 61 74 65 20 74 68 65 20 61 63  anipulate the ac
16885 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 66 72  tual database fr
16886 65 65 2d 6c 69 73 74 20 73 74 72 75 63 74 75 72  ee-list structur
16887 65 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 6f  e. There are two
16888 0a 20 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74  .  ** possibilit
16889 69 65 73 2e 20 49 66 20 74 68 65 20 66 72 65 65  ies. If the free
1688a 2d 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74  -list is current
1688b 6c 79 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 20  ly empty, or if 
1688c 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 74  the first.  ** t
1688d 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65  runk page in the
1688e 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75   free-list is fu
1688f 6c 6c 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61  ll, then this pa
16890 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61  ge will become a
16891 0a 20 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d 6c  .  ** new free-l
16892 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20  ist trunk page. 
16893 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 77 69  Otherwise, it wi
16894 6c 6c 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 66  ll become a leaf
16895 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 72   of the.  ** fir
16896 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e  st trunk page in
16897 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 65   the current fre
16898 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 62 6c 6f  e-list. This blo
16899 63 6b 20 74 65 73 74 73 20 69 66 20 69 74 0a 20  ck tests if it. 
1689a 20 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20   ** is possible 
1689b 74 6f 20 61 64 64 20 74 68 65 20 70 61 67 65 20  to add the page 
1689c 61 73 20 61 20 6e 65 77 20 66 72 65 65 2d 6c 69  as a new free-li
1689d 73 74 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 20  st leaf..  */.  
1689e 69 66 28 20 6e 46 72 65 65 21 3d 30 20 29 7b 0a  if( nFree!=0 ){.
1689f 20 20 20 20 69 6e 74 20 6e 4c 65 61 66 3b 20 20      int nLeaf;  
168a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
168a1 20 49 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20   Initial number 
168a2 6f 66 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e  of leaf cells on
168a3 20 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 0a   trunk page */..
168a4 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74      iTrunk = get
168a5 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
168a6 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 72  Data[32]);.    r
168a7 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
168a8 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72  GetPage(pBt, iTr
168a9 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29  unk, &pTrunk, 0)
168aa 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
168ab 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
168ac 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f   goto freepage_o
168ad 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e  ut;.    }..    n
168ae 4c 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28  Leaf = get4byte(
168af 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34  &pTrunk->aData[4
168b0 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61  ]);.    if( nLea
168b1 66 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  f<0 ){.      rc 
168b2 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
168b3 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74  _BKPT;.      got
168b4 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a  o freepage_out;.
168b5 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c      }.    if( nL
168b6 65 61 66 3c 70 42 74 2d 3e 75 73 61 62 6c 65 53  eaf<pBt->usableS
168b7 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20 20  ize/4 - 8 ){.   
168b8 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61     /* In this ca
168b9 73 65 20 74 68 65 72 65 20 69 73 20 72 6f 6f 6d  se there is room
168ba 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 70 61   on the trunk pa
168bb 67 65 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65  ge to insert the
168bc 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62   page.      ** b
168bd 65 69 6e 67 20 66 72 65 65 64 20 61 73 20 61 20  eing freed as a 
168be 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20 20 20 20  new leaf..      
168bf 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65  **.      ** Note
168c0 20 74 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20   that the trunk 
168c1 70 61 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c  page is not real
168c2 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74  ly full until it
168c3 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20   contains.      
168c4 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20  ** usableSize/4 
168c5 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74  - 2 entries, not
168c6 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20   usableSize/4 - 
168c7 38 20 65 6e 74 72 69 65 73 20 61 73 20 77 65 20  8 entries as we 
168c8 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f  have.      ** co
168c9 64 65 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f  ded.  But due to
168ca 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20   a coding error 
168cb 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  in versions of S
168cc 51 4c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20  QLite prior to. 
168cd 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64       ** 3.6.0, d
168ce 61 74 61 62 61 73 65 73 20 77 69 74 68 20 66 72  atabases with fr
168cf 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67  eelist trunk pag
168d0 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20  es holding more 
168d1 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73  than.      ** us
168d2 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65  ableSize/4 - 8 e
168d3 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72  ntries will be r
168d4 65 70 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75  eported as corru
168d5 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20  pt.  In order.  
168d6 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61      ** to mainta
168d7 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  in backwards com
168d8 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20  patibility with 
168d9 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f  older versions o
168da 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20  f SQLite,.      
168db 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61  ** we will conta
168dc 69 6e 20 74 6f 20 72 65 73 74 72 69 63 74 20 74  in to restrict t
168dd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
168de 72 69 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69  ries to usableSi
168df 7a 65 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a  ze/4 - 8.      *
168e0 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73  * for now.  At s
168e1 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  ome point in the
168e2 20 66 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76   future (once ev
168e3 65 72 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61  eryone has upgra
168e4 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ded.      ** to 
168e5 33 2e 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20  3.6.0 or later) 
168e6 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64  we should consid
168e7 65 72 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f  er fixing the co
168e8 6e 64 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a  nditional above.
168e9 20 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64        ** to read
168ea 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32   "usableSize/4-2
168eb 22 20 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73  " instead of "us
168ec 61 62 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20  ableSize/4-8".. 
168ed 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63       */.      rc
168ee 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
168ef 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62  rite(pTrunk->pDb
168f0 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
168f1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
168f2 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79  {.        put4by
168f3 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  te(&pTrunk->aDat
168f4 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b 31 29 3b 0a  a[4], nLeaf+1);.
168f5 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
168f6 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  (&pTrunk->aData[
168f7 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20 69 50 61 67  8+nLeaf*4], iPag
168f8 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  e);.#ifndef SQLI
168f9 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45  TE_SECURE_DELETE
168fa 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
168fb 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ge ){.          
168fc 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
168fd 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
168fe 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  Page);.        }
168ff 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
16900 72 63 20 3d 20 62 74 72 65 65 53 65 74 48 61 73  rc = btreeSetHas
16901 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 61  Content(pBt, iPa
16902 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ge);.      }.   
16903 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45 2d     TRACE(("FREE-
16904 50 41 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e  PAGE: %d leaf on
16905 20 74 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e   trunk page %d\n
16906 22 2c 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54  ",pPage->pgno,pT
16907 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20  runk->pgno));.  
16908 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67      goto freepag
16909 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  e_out;.    }.  }
1690a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f  ..  /* If contro
1690b 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20  l flows to this 
1690c 70 6f 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 77  point, then it w
1690d 61 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  as not possible 
1690e 74 6f 20 61 64 64 20 74 68 65 0a 20 20 2a 2a 20  to add the.  ** 
1690f 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 66  the page being f
16910 72 65 65 64 20 61 73 20 61 20 6c 65 61 66 20 70  reed as a leaf p
16911 61 67 65 20 6f 66 20 74 68 65 20 66 69 72 73 74  age of the first
16912 20 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72   trunk in the fr
16913 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f  ee-list..  ** Po
16914 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 74  ssibly because t
16915 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20  he free-list is 
16916 65 6d 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62  empty, or possib
16917 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 20 0a  ly because the .
16918 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b    ** first trunk
16919 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   in the free-lis
1691a 74 20 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65  t is full. Eithe
1691b 72 20 77 61 79 2c 20 74 68 65 20 70 61 67 65 20  r way, the page 
1691c 62 65 69 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a  being freed.  **
1691d 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65   will become the
1691e 20 6e 65 77 20 66 69 72 73 74 20 74 72 75 6e 6b   new first trunk
1691f 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 72 65   page in the fre
16920 65 2d 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69  e-list..  */.  i
16921 66 28 20 20 20 28 28 21 70 50 61 67 65 29 20 26  f(   ((!pPage) &
16922 26 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71  & (0 != (rc = sq
16923 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
16924 65 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70  e(pBt, iPage, &p
16925 50 61 67 65 2c 20 30 29 29 29 29 0a 20 20 20 20  Page, 0)))).    
16926 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20   || (0 != (rc = 
16927 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
16928 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
16929 29 29 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74  ))).  ){.    got
1692a 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a  o freepage_out;.
1692b 20 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70    }.  put4byte(p
1692c 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72  Page->aData, iTr
1692d 75 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65  unk);.  put4byte
1692e 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34  (&pPage->aData[4
1692f 5d 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74  ], 0);.  put4byt
16930 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
16931 5b 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20  [32], iPage);.  
16932 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47  TRACE(("FREE-PAG
16933 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20  E: %d new trunk 
16934 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25  page replacing %
16935 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e  d\n", pPage->pgn
16936 6f 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72  o, iTrunk));..fr
16937 65 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66  eepage_out:.  if
16938 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70  ( pPage ){.    p
16939 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30  Page->isInit = 0
1693a 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50  ;.  }.  releaseP
1693b 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65  age(pPage);.  re
1693c 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b  leasePage(pTrunk
1693d 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1693e 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65  }.static int fre
1693f 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  ePage(MemPage *p
16940 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  Page){.  return 
16941 66 72 65 65 50 61 67 65 32 28 70 50 61 67 65 2d  freePage2(pPage-
16942 3e 70 42 74 2c 20 70 50 61 67 65 2c 20 70 50 61  >pBt, pPage, pPa
16943 67 65 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a  ge->pgno);.}../*
16944 0a 2a 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65  .** Free any ove
16945 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f  rflow pages asso
16946 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
16947 67 69 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73  given Cell..*/.s
16948 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43  tatic int clearC
16949 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ell(MemPage *pPa
1694a 67 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ge, unsigned cha
1694b 72 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53  r *pCell){.  BtS
1694c 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61  hared *pBt = pPa
1694d 67 65 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49  ge->pBt;.  CellI
1694e 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f  nfo info;.  Pgno
1694f 20 6f 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74   ovflPgno;.  int
16950 20 72 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c   rc;.  int nOvfl
16951 3b 0a 20 20 75 31 36 20 6f 76 66 6c 50 61 67 65  ;.  u16 ovflPage
16952 53 69 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Size;..  assert(
16953 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
16954 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
16955 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69  mutex) );.  sqli
16956 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
16957 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  lPtr(pPage, pCel
16958 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28  l, &info);.  if(
16959 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d   info.iOverflow=
1695a 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1695b 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20   SQLITE_OK;  /* 
1695c 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  No overflow page
1695d 73 2e 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75  s. Return withou
1695e 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67  t doing anything
1695f 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67   */.  }.  ovflPg
16960 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
16961 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66  Cell[info.iOverf
16962 6c 6f 77 5d 29 3b 0a 20 20 61 73 73 65 72 74 28  low]);.  assert(
16963 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
16964 20 3e 20 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61   > 4 );.  ovflPa
16965 67 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73  geSize = pBt->us
16966 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20  ableSize - 4;.  
16967 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50  nOvfl = (info.nP
16968 61 79 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c  ayload - info.nL
16969 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53  ocal + ovflPageS
1696a 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67  ize - 1)/ovflPag
1696b 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28  eSize;.  assert(
1696c 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20   ovflPgno==0 || 
1696d 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69  nOvfl>0 );.  whi
1696e 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20  le( nOvfl-- ){. 
1696f 20 20 20 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20     Pgno iNext = 
16970 30 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  0;.    MemPage *
16971 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69  pOvfl = 0;.    i
16972 66 28 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 7c 7c  f( ovflPgno<2 ||
16973 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65 72 50   ovflPgno>pagerP
16974 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b  agecount(pBt) ){
16975 0a 20 20 20 20 20 20 2f 2a 20 30 20 69 73 20 6e  .      /* 0 is n
16976 6f 74 20 61 20 6c 65 67 61 6c 20 70 61 67 65 20  ot a legal page 
16977 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20  number and page 
16978 31 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 0a  1 cannot be an .
16979 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f        ** overflo
1697a 77 20 70 61 67 65 2e 20 54 68 65 72 65 66 6f 72  w page. Therefor
1697b 65 20 69 66 20 6f 76 66 6c 50 67 6e 6f 3c 32 20  e if ovflPgno<2 
1697c 6f 72 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  or past the end 
1697d 6f 66 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  of the .      **
1697e 20 66 69 6c 65 20 74 68 65 20 64 61 74 61 62 61   file the databa
1697f 73 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75  se must be corru
16980 70 74 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74  pt. */.      ret
16981 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
16982 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20  PT_BKPT;.    }. 
16983 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 29 7b 0a     if( nOvfl ){.
16984 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76        rc = getOv
16985 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20  erflowPage(pBt, 
16986 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c  ovflPgno, &pOvfl
16987 2c 20 26 69 4e 65 78 74 29 3b 0a 20 20 20 20 20  , &iNext);.     
16988 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
16989 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72   rc;.    }.    r
1698a 63 20 3d 20 66 72 65 65 50 61 67 65 32 28 70 42  c = freePage2(pB
1698b 74 2c 20 70 4f 76 66 6c 2c 20 6f 76 66 6c 50 67  t, pOvfl, ovflPg
1698c 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 76  no);.    if( pOv
1698d 66 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  fl ){.      sqli
1698e 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f  te3PagerUnref(pO
1698f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  vfl->pDbPage);. 
16990 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 20     }.    if( rc 
16991 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
16992 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 69 4e 65 78   ovflPgno = iNex
16993 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
16994 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
16995 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20 62  .** Create the b
16996 79 74 65 20 73 65 71 75 65 6e 63 65 20 75 73 65  yte sequence use
16997 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61  d to represent a
16998 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65 20 70 50   cell on page pP
16999 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65  age.** and write
1699a 20 74 68 61 74 20 62 79 74 65 20 73 65 71 75 65   that byte seque
1699b 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c 6c 5b 5d  nce into pCell[]
1699c 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65  .  Overflow page
1699d 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74  s are.** allocat
1699e 65 64 20 61 6e 64 20 66 69 6c 6c 65 64 20 69 6e  ed and filled in
1699f 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20 20   as necessary.  
169a0 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63  The calling proc
169a1 65 64 75 72 65 0a 2a 2a 20 69 73 20 72 65 73 70  edure.** is resp
169a2 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69  onsible for maki
169a3 6e 67 20 73 75 72 65 20 73 75 66 66 69 63 69 65  ng sure sufficie
169a4 6e 74 20 73 70 61 63 65 20 68 61 73 20 62 65 65  nt space has bee
169a5 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 66  n allocated.** f
169a6 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a  or pCell[]..**.*
169a7 2a 20 4e 6f 74 65 20 74 68 61 74 20 70 43 65 6c  * Note that pCel
169a8 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65 63 65 73  l does not neces
169a9 73 61 72 79 20 6e 65 65 64 20 74 6f 20 70 6f 69  sary need to poi
169aa 6e 74 20 74 6f 20 74 68 65 20 70 50 61 67 65 2d  nt to the pPage-
169ab 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65 61 2e 20  >aData.** area. 
169ac 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 70 6f 69   pCell might poi
169ad 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65 6d 70 6f  nt to some tempo
169ae 72 61 72 79 20 73 74 6f 72 61 67 65 2e 20 20 54  rary storage.  T
169af 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a 2a 2a 20  he cell will.** 
169b0 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 69  be constructed i
169b1 6e 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79  n this temporary
169b2 20 61 72 65 61 20 74 68 65 6e 20 63 6f 70 69 65   area then copie
169b3 64 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44  d into pPage->aD
169b4 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e 0a 2a 2f  ata.** later..*/
169b5 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 6c  .static int fill
169b6 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67  InCell(.  MemPag
169b7 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20  e *pPage,       
169b8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
169b9 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69  page that contai
169ba 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20  ns the cell */. 
169bb 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
169bc 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20  pCell,          
169bd 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74  /* Complete text
169be 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a   of the cell */.
169bf 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b    const void *pK
169c0 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20  ey, i64 nKey,   
169c1 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20   /* The key */. 
169c2 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61   const void *pDa
169c3 74 61 2c 69 6e 74 20 6e 44 61 74 61 2c 20 20 20  ta,int nData,   
169c4 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0a 20  /* The data */. 
169c5 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20   int nZero,     
169c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
169c7 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f 20 62 79  /* Extra zero by
169c8 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f  tes to append to
169c9 20 70 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20   pData */.  int 
169ca 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 20 20 20  *pnSize         
169cb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
169cc 69 74 65 20 63 65 6c 6c 20 73 69 7a 65 20 68 65  ite cell size he
169cd 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e  re */.){.  int n
169ce 50 61 79 6c 6f 61 64 3b 0a 20 20 63 6f 6e 73 74  Payload;.  const
169cf 20 75 38 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74   u8 *pSrc;.  int
169d0 20 6e 53 72 63 2c 20 6e 2c 20 72 63 3b 0a 20 20   nSrc, n, rc;.  
169d1 69 6e 74 20 73 70 61 63 65 4c 65 66 74 3b 0a 20  int spaceLeft;. 
169d2 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20   MemPage *pOvfl 
169d3 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  = 0;.  MemPage *
169d4 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 30 3b 0a  pToRelease = 0;.
169d5 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
169d6 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e 73 69 67  *pPrior;.  unsig
169d7 6e 65 64 20 63 68 61 72 20 2a 70 50 61 79 6c 6f  ned char *pPaylo
169d8 61 64 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ad;.  BtShared *
169d9 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74  pBt = pPage->pBt
169da 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66  ;.  Pgno pgnoOvf
169db 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 65  l = 0;.  int nHe
169dc 61 64 65 72 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f  ader;.  CellInfo
169dd 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 73 65 72 74   info;..  assert
169de 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
169df 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
169e0 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a  >mutex) );..  /*
169e1 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 6e 65   pPage is not ne
169e2 63 65 73 73 61 72 69 6c 79 20 77 72 69 74 65 61  cessarily writea
169e3 62 6c 65 20 73 69 6e 63 65 20 70 43 65 6c 6c 20  ble since pCell 
169e4 6d 69 67 68 74 20 62 65 20 61 75 78 69 6c 69 61  might be auxilia
169e5 72 79 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 73  ry.  ** buffer s
169e6 70 61 63 65 20 74 68 61 74 20 69 73 20 73 65 70  pace that is sep
169e7 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 70  arate from the p
169e8 50 61 67 65 20 62 75 66 66 65 72 20 61 72 65 61  Page buffer area
169e9 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43   */.  assert( pC
169ea 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 44 61 74 61  ell<pPage->aData
169eb 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70 50 61 67   || pCell>=&pPag
169ec 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61  e->aData[pBt->pa
169ed 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20  geSize].        
169ee 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61      || sqlite3Pa
169ef 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
169f0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
169f1 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20  ;..  /* Fill in 
169f2 74 68 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20  the header. */. 
169f3 20 6e 48 65 61 64 65 72 20 3d 20 30 3b 0a 20 20   nHeader = 0;.  
169f4 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
169f5 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20   ){.    nHeader 
169f6 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20  += 4;.  }.  if( 
169f7 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29  pPage->hasData )
169f8 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d  {.    nHeader +=
169f9 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c   putVarint(&pCel
169fa 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e 44 61 74  l[nHeader], nDat
169fb 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73  a+nZero);.  }els
169fc 65 7b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 6e  e{.    nData = n
169fd 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Zero = 0;.  }.  
169fe 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61  nHeader += putVa
169ff 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61  rint(&pCell[nHea
16a00 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b  der], *(u64*)&nK
16a01 65 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ey);.  sqlite3Bt
16a02 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
16a03 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69  pPage, pCell, &i
16a04 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20  nfo);.  assert( 
16a05 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48  info.nHeader==nH
16a06 65 61 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72  eader );.  asser
16a07 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b  t( info.nKey==nK
16a08 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ey );.  assert( 
16a09 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32  info.nData==(u32
16a0a 29 28 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29  )(nData+nZero) )
16a0b 3b 0a 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69  ;.  .  /* Fill i
16a0c 6e 20 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f  n the payload */
16a0d 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44  .  nPayload = nD
16a0e 61 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69  ata + nZero;.  i
16a0f 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  f( pPage->intKey
16a10 20 29 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70   ){.    pSrc = p
16a11 44 61 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d  Data;.    nSrc =
16a12 20 6e 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74   nData;.    nDat
16a13 61 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20  a = 0;.  }else{ 
16a14 0a 20 20 20 20 69 66 28 20 6e 4b 65 79 3e 30 78  .    if( nKey>0x
16a15 37 66 66 66 66 66 66 66 20 7c 7c 20 70 4b 65 79  7fffffff || pKey
16a16 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
16a17 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
16a18 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50  PT;.    }.    nP
16a19 61 79 6c 6f 61 64 20 2b 3d 20 28 69 6e 74 29 6e  ayload += (int)n
16a1a 4b 65 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20  Key;.    pSrc = 
16a1b 70 4b 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d  pKey;.    nSrc =
16a1c 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 7d 0a   (int)nKey;.  }.
16a1d 20 20 2a 70 6e 53 69 7a 65 20 3d 20 69 6e 66 6f    *pnSize = info
16a1e 2e 6e 53 69 7a 65 3b 0a 20 20 73 70 61 63 65 4c  .nSize;.  spaceL
16a1f 65 66 74 20 3d 20 69 6e 66 6f 2e 6e 4c 6f 63 61  eft = info.nLoca
16a20 6c 3b 0a 20 20 70 50 61 79 6c 6f 61 64 20 3d 20  l;.  pPayload = 
16a21 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 3b  &pCell[nHeader];
16a22 0a 20 20 70 50 72 69 6f 72 20 3d 20 26 70 43 65  .  pPrior = &pCe
16a23 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
16a24 77 5d 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 50  w];..  while( nP
16a25 61 79 6c 6f 61 64 3e 30 20 29 7b 0a 20 20 20 20  ayload>0 ){.    
16a26 69 66 28 20 73 70 61 63 65 4c 65 66 74 3d 3d 30  if( spaceLeft==0
16a27 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   ){.#ifndef SQLI
16a28 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
16a29 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67  UM.      Pgno pg
16a2a 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f  noPtrmap = pgnoO
16a2b 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77  vfl; /* Overflow
16a2c 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61   page pointer-ma
16a2d 70 20 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a  p entry page */.
16a2e 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61        if( pBt->a
16a2f 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
16a30 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20       do{.       
16a31 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20     pgnoOvfl++;. 
16a32 20 20 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20         } while( 
16a33 0a 20 20 20 20 20 20 20 20 20 20 50 54 52 4d 41  .          PTRMA
16a34 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 67  P_ISPAGE(pBt, pg
16a35 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f  noOvfl) || pgnoO
16a36 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  vfl==PENDING_BYT
16a37 45 5f 50 41 47 45 28 70 42 74 29 20 0a 20 20 20  E_PAGE(pBt) .   
16a38 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a       );.      }.
16a39 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20  #endif.      rc 
16a3a 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  = allocateBtreeP
16a3b 61 67 65 28 70 42 74 2c 20 26 70 4f 76 66 6c 2c  age(pBt, &pOvfl,
16a3c 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e 6f   &pgnoOvfl, pgno
16a3d 4f 76 66 6c 2c 20 30 29 3b 0a 23 69 66 6e 64 65  Ovfl, 0);.#ifnde
16a3e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
16a3f 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f  TOVACUUM.      /
16a40 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
16a41 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d  e supports auto-
16a42 76 61 63 75 75 6d 2c 20 61 6e 64 20 74 68 65 20  vacuum, and the 
16a43 73 65 63 6f 6e 64 20 6f 72 20 73 75 62 73 65 71  second or subseq
16a44 75 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f 76  uent.      ** ov
16a45 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 62  erflow page is b
16a46 65 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c 20  eing allocated, 
16a47 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20  add an entry to 
16a48 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a  the pointer-map.
16a49 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 61        ** for tha
16a4a 74 20 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20 20  t page now. .   
16a4b 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49     **.      ** I
16a4c 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69  f this is the fi
16a4d 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  rst overflow pag
16a4e 65 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 20  e, then write a 
16a4f 70 61 72 74 69 61 6c 20 65 6e 74 72 79 20 0a 20  partial entry . 
16a50 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 70       ** to the p
16a51 6f 69 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20 77  ointer-map. If w
16a52 65 20 77 72 69 74 65 20 6e 6f 74 68 69 6e 67 20  e write nothing 
16a53 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72 2d  to this pointer-
16a54 6d 61 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20 20  map slot,.      
16a55 2a 2a 20 74 68 65 6e 20 74 68 65 20 6f 70 74 69  ** then the opti
16a56 6d 69 73 74 69 63 20 6f 76 65 72 66 6c 6f 77 20  mistic overflow 
16a57 63 68 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67  chain processing
16a58 20 69 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29 0a   in clearCell().
16a59 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69 73        ** may mis
16a5a 69 6e 74 65 72 70 72 65 74 20 74 68 65 20 75 6e  interpret the un
16a5b 69 6e 69 74 69 61 6c 69 73 65 64 20 76 61 6c 75  initialised valu
16a5c 65 73 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68  es and delete th
16a5d 65 0a 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e 67  e.      ** wrong
16a5e 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20   pages from the 
16a5f 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20  database..      
16a60 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  */.      if( pBt
16a61 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20  ->autoVacuum && 
16a62 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
16a63 0a 20 20 20 20 20 20 20 20 75 38 20 65 54 79 70  .        u8 eTyp
16a64 65 20 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70 3f  e = (pgnoPtrmap?
16a65 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
16a66 3a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  :PTRMAP_OVERFLOW
16a67 31 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  1);.        rc =
16a68 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
16a69 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 79 70 65 2c  pgnoOvfl, eType,
16a6a 20 70 67 6e 6f 50 74 72 6d 61 70 29 3b 0a 20 20   pgnoPtrmap);.  
16a6b 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
16a6c 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73            releas
16a6d 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20  ePage(pOvfl);.  
16a6e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
16a6f 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28  #endif.      if(
16a70 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72   rc ){.        r
16a71 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52 65  eleasePage(pToRe
16a72 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20  lease);.        
16a73 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
16a74 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   }..      /* If 
16a75 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f  pToRelease is no
16a76 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 72 69  t zero than pPri
16a77 6f 72 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74  or points into t
16a78 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20  he data area.   
16a79 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65     ** of pToRele
16a7a 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20  ase.  Make sure 
16a7b 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74  pToRelease is st
16a7c 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a  ill writeable. *
16a7d 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
16a7e 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c  pToRelease==0 ||
16a7f 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
16a80 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65  riteable(pToRele
16a81 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  ase->pDbPage) );
16a82 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 50  ..      /* If pP
16a83 72 69 6f 72 20 69 73 20 70 61 72 74 20 6f 66 20  rior is part of 
16a84 74 68 65 20 64 61 74 61 20 61 72 65 61 20 6f 66  the data area of
16a85 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b   pPage, then mak
16a86 65 20 73 75 72 65 20 70 50 61 67 65 0a 20 20 20  e sure pPage.   
16a87 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77     ** is still w
16a88 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20  riteable */.    
16a89 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72    assert( pPrior
16a8a 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c  <pPage->aData ||
16a8b 20 70 50 72 69 6f 72 3e 3d 26 70 50 61 67 65 2d   pPrior>=&pPage-
16a8c 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65  >aData[pBt->page
16a8d 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20  Size].          
16a8e 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65    || sqlite3Page
16a8f 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
16a90 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
16a91 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28  .      put4byte(
16a92 70 50 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c  pPrior, pgnoOvfl
16a93 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  );.      release
16a94 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29  Page(pToRelease)
16a95 3b 0a 20 20 20 20 20 20 70 54 6f 52 65 6c 65 61  ;.      pToRelea
16a96 73 65 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 20 20  se = pOvfl;.    
16a97 20 20 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 6c    pPrior = pOvfl
16a98 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 70  ->aData;.      p
16a99 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20  ut4byte(pPrior, 
16a9a 30 29 3b 0a 20 20 20 20 20 20 70 50 61 79 6c 6f  0);.      pPaylo
16a9b 61 64 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 44 61  ad = &pOvfl->aDa
16a9c 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20 73 70 61  ta[4];.      spa
16a9d 63 65 4c 65 66 74 20 3d 20 70 42 74 2d 3e 75 73  ceLeft = pBt->us
16a9e 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20  ableSize - 4;.  
16a9f 20 20 7d 0a 20 20 20 20 6e 20 3d 20 6e 50 61 79    }.    n = nPay
16aa0 6c 6f 61 64 3b 0a 20 20 20 20 69 66 28 20 6e 3e  load;.    if( n>
16aa1 73 70 61 63 65 4c 65 66 74 20 29 20 6e 20 3d 20  spaceLeft ) n = 
16aa2 73 70 61 63 65 4c 65 66 74 3b 0a 0a 20 20 20 20  spaceLeft;..    
16aa3 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 73 65  /* If pToRelease
16aa4 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 61   is not zero tha
16aa5 6e 20 70 50 61 79 6c 6f 61 64 20 70 6f 69 6e 74  n pPayload point
16aa6 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20  s into the data 
16aa7 61 72 65 61 0a 20 20 20 20 2a 2a 20 6f 66 20 70  area.    ** of p
16aa8 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b 65  ToRelease.  Make
16aa9 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 73 65   sure pToRelease
16aaa 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61   is still writea
16aab 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65  ble. */.    asse
16aac 72 74 28 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d  rt( pToRelease==
16aad 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65  0 || sqlite3Page
16aae 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 54 6f  rIswriteable(pTo
16aaf 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 65  Release->pDbPage
16ab0 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  ) );..    /* If 
16ab1 70 50 61 79 6c 6f 61 64 20 69 73 20 70 61 72 74  pPayload is part
16ab2 20 6f 66 20 74 68 65 20 64 61 74 61 20 61 72 65   of the data are
16ab3 61 20 6f 66 20 70 50 61 67 65 2c 20 74 68 65 6e  a of pPage, then
16ab4 20 6d 61 6b 65 20 73 75 72 65 20 70 50 61 67 65   make sure pPage
16ab5 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c  .    ** is still
16ab6 20 77 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20   writeable */.  
16ab7 20 20 61 73 73 65 72 74 28 20 70 50 61 79 6c 6f    assert( pPaylo
16ab8 61 64 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20  ad<pPage->aData 
16ab9 7c 7c 20 70 50 61 79 6c 6f 61 64 3e 3d 26 70 50  || pPayload>=&pP
16aba 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e  age->aData[pBt->
16abb 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20  pageSize].      
16abc 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
16abd 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
16abe 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
16abf 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e 53 72   );..    if( nSr
16ac0 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  c>0 ){.      if(
16ac1 20 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53   n>nSrc ) n = nS
16ac2 72 63 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  rc;.      assert
16ac3 28 20 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20  ( pSrc );.      
16ac4 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c  memcpy(pPayload,
16ac5 20 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d   pSrc, n);.    }
16ac6 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73  else{.      mems
16ac7 65 74 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20  et(pPayload, 0, 
16ac8 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50  n);.    }.    nP
16ac9 61 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20  ayload -= n;.   
16aca 20 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a   pPayload += n;.
16acb 20 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20      pSrc += n;. 
16acc 20 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20     nSrc -= n;.  
16acd 20 20 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e    spaceLeft -= n
16ace 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d  ;.    if( nSrc==
16acf 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20  0 ){.      nSrc 
16ad0 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70  = nData;.      p
16ad1 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20  Src = pData;.   
16ad2 20 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65   }.  }.  release
16ad3 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29  Page(pToRelease)
16ad4 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
16ad5 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  E_OK;.}../*.** R
16ad6 65 6d 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63  emove the i-th c
16ad7 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20  ell from pPage. 
16ad8 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66   This routine ef
16ad9 66 65 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79  fects pPage only
16ada 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f  ..** The cell co
16adb 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65  ntent is not fre
16adc 65 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65  ed or deallocate
16add 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65  d.  It is assume
16ade 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65  d that.** the ce
16adf 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62  ll content has b
16ae0 65 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70  een copied somep
16ae1 6c 61 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73  lace else.  This
16ae2 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a   routine just.**
16ae3 20 72 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66   removes the ref
16ae4 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65  erence to the ce
16ae5 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a  ll from pPage..*
16ae6 2a 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62  *.** "sz" must b
16ae7 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
16ae8 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c  bytes in the cel
16ae9 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
16aea 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67   dropCell(MemPag
16aeb 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64  e *pPage, int id
16aec 78 2c 20 69 6e 74 20 73 7a 29 7b 0a 20 20 69 6e  x, int sz){.  in
16aed 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  t i;          /*
16aee 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
16aef 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20  .  int pc;      
16af0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20     /* Offset to 
16af1 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20  cell content of 
16af2 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74  cell being delet
16af3 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61  ed */.  u8 *data
16af4 3b 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65  ;       /* pPage
16af5 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75 38 20  ->aData */.  u8 
16af6 2a 70 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  *ptr;        /* 
16af7 55 73 65 64 20 74 6f 20 6d 6f 76 65 20 62 79 74  Used to move byt
16af8 65 73 20 61 72 6f 75 6e 64 20 77 69 74 68 69 6e  es around within
16af9 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74   data[] */.  int
16afa 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   rc;         /* 
16afb 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  The return code 
16afc 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64  */..  assert( id
16afd 78 3e 3d 30 20 26 26 20 69 64 78 3c 70 50 61 67  x>=0 && idx<pPag
16afe 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73  e->nCell );.  as
16aff 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69  sert( sz==cellSi
16b00 7a 65 28 70 50 61 67 65 2c 20 69 64 78 29 20 29  ze(pPage, idx) )
16b01 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
16b02 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
16b03 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
16b04 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
16b05 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
16b06 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
16b07 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 61 74 61  mutex) );.  data
16b08 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
16b09 0a 20 20 70 74 72 20 3d 20 26 64 61 74 61 5b 70  .  ptr = &data[p
16b0a 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74  Page->cellOffset
16b0b 20 2b 20 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20   + 2*idx];.  pc 
16b0c 3d 20 67 65 74 32 62 79 74 65 28 70 74 72 29 3b  = get2byte(ptr);
16b0d 0a 20 20 69 66 28 20 28 70 63 3c 70 50 61 67 65  .  if( (pc<pPage
16b0e 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b 28 70  ->hdrOffset+6+(p
16b0f 50 61 67 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 29  Page->leaf?0:4))
16b10 0a 20 20 20 20 20 7c 7c 20 28 70 63 2b 73 7a 3e  .     || (pc+sz>
16b11 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
16b12 6c 65 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 72  leSize) ){.    r
16b13 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
16b14 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
16b15 20 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 28   rc = freeSpace(
16b16 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a  pPage, pc, sz);.
16b17 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16b18 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
16b19 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  n rc;.  }.  for(
16b1a 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65  i=idx+1; i<pPage
16b1b 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74  ->nCell; i++, pt
16b1c 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30  r+=2){.    ptr[0
16b1d 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20  ] = ptr[2];.    
16b1e 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b  ptr[1] = ptr[3];
16b1f 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43  .  }.  pPage->nC
16b20 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74  ell--;.  put2byt
16b21 65 28 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68  e(&data[pPage->h
16b22 64 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61  drOffset+3], pPa
16b23 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50  ge->nCell);.  pP
16b24 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b  age->nFree += 2;
16b25 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
16b26 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  _OK;.}../*.** In
16b27 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20  sert a new cell 
16b28 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c  on pPage at cell
16b29 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65   index "i".  pCe
16b2a 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  ll points to the
16b2b 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  .** content of t
16b2c 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49  he cell..**.** I
16b2d 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  f the cell conte
16b2e 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74  nt will fit on t
16b2f 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75  he page, then pu
16b30 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 20  t it there.  If 
16b31 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66  it.** will not f
16b32 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20  it, then make a 
16b33 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c  copy of the cell
16b34 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54   content into pT
16b35 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20  emp if.** pTemp 
16b36 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65  is not null.  Re
16b37 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d  gardless of pTem
16b38 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  p, allocate a ne
16b39 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50  w entry.** in pP
16b3a 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64  age->aOvfl[] and
16b3b 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74   make it point t
16b3c 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  o the cell conte
16b3d 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e  nt (either.** in
16b3e 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72   pTemp or the or
16b3f 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e  iginal pCell) an
16b40 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74  d also record it
16b41 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c  s index. .** All
16b42 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e  ocating a new en
16b43 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43  try in pPage->aC
16b44 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68  ell[] implies th
16b45 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f  at .** pPage->nO
16b46 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65  verflow is incre
16b47 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  mented..**.** If
16b48 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65   nSkip is non-ze
16b49 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20  ro, then do not 
16b4a 63 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 6e  copy the first n
16b4b 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 68  Skip bytes of th
16b4c 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63  e.** cell. The c
16b4d 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77  aller will overw
16b4e 72 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 20  rite them after 
16b4f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
16b50 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b  turns. If.** nSk
16b51 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  ip is non-zero, 
16b52 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e  then pCell may n
16b53 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69  ot point to an i
16b54 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f  nvalid memory lo
16b55 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20  cation .** (but 
16b56 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61  pCell+nSkip is a
16b57 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f  lways valid)..*/
16b58 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 73 65  .static int inse
16b59 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67  rtCell(.  MemPag
16b5a 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50  e *pPage,   /* P
16b5b 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  age into which w
16b5c 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f  e are copying */
16b5d 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20  .  int i,       
16b5e 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c       /* New cell
16b5f 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74   becomes the i-t
16b60 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61  h cell of the pa
16b61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c  ge */.  u8 *pCel
16b62 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  l,        /* Con
16b63 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20  tent of the new 
16b64 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  cell */.  int sz
16b65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ,           /* B
16b66 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20  ytes of content 
16b67 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38  in pCell */.  u8
16b68 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20   *pTemp,        
16b69 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20  /* Temp storage 
16b6a 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c  space for pCell,
16b6b 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20   if needed */.  
16b6c 75 38 20 6e 53 6b 69 70 20 20 20 20 20 20 20 20  u8 nSkip        
16b6d 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69 74    /* Do not writ
16b6e 65 20 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69  e the first nSki
16b6f 70 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 63  p bytes of the c
16b70 65 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ell */.){.  int 
16b71 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  idx;          /*
16b72 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20   Where to write 
16b73 6e 65 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  new cell content
16b74 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20   in data[] */.  
16b75 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20  int j;          
16b76 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
16b77 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20  r */.  int top; 
16b78 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
16b79 74 20 62 79 74 65 20 6f 66 20 63 6f 6e 74 65 6e  t byte of conten
16b7a 74 20 66 6f 72 20 61 6e 79 20 63 65 6c 6c 20 69  t for any cell i
16b7b 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e  n data[] */.  in
16b7c 74 20 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20  t end;          
16b7d 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61  /* First byte pa
16b7e 73 74 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c  st the last cell
16b7f 20 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61   pointer in data
16b80 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b  [] */.  int ins;
16b81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
16b82 65 78 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65  ex in data[] whe
16b83 72 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e  re new cell poin
16b84 74 65 72 20 69 73 20 69 6e 73 65 72 74 65 64 20  ter is inserted 
16b85 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20  */.  int hdr;   
16b86 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
16b87 20 69 6e 74 6f 20 64 61 74 61 5b 5d 20 6f 66 20   into data[] of 
16b88 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20  the page header 
16b89 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66  */.  int cellOff
16b8a 73 65 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73  set;   /* Addres
16b8b 73 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20  s of first cell 
16b8c 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b  pointer in data[
16b8d 5d 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b  ] */.  u8 *data;
16b8e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
16b8f 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77  content of the w
16b90 68 6f 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75  hole page */.  u
16b91 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20  8 *ptr;         
16b92 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76   /* Used for mov
16b93 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
16b94 61 72 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d  around in data[]
16b95 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69   */..  assert( i
16b96 3e 3d 30 20 26 26 20 69 3c 3d 70 50 61 67 65 2d  >=0 && i<=pPage-
16b97 3e 6e 43 65 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f  >nCell+pPage->nO
16b98 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 61 73 73  verflow );.  ass
16b99 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c  ert( pPage->nCel
16b9a 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65  l<=MX_CELL(pPage
16b9b 2d 3e 70 42 74 29 20 26 26 20 4d 58 5f 43 45 4c  ->pBt) && MX_CEL
16b9c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35  L(pPage->pBt)<=5
16b9d 34 36 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  460 );.  assert(
16b9e 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
16b9f 77 3c 3d 41 72 72 61 79 53 69 7a 65 28 70 50 61  w<=ArraySize(pPa
16ba0 67 65 2d 3e 61 4f 76 66 6c 29 20 29 3b 0a 20 20  ge->aOvfl) );.  
16ba1 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c  assert( sz==cell
16ba2 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70  SizePtr(pPage, p
16ba3 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72  Cell) );.  asser
16ba4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
16ba5 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
16ba6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
16ba7 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
16ba8 6f 77 20 7c 7c 20 73 7a 2b 32 3e 70 50 61 67 65  ow || sz+2>pPage
16ba9 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 69  ->nFree ){.    i
16baa 66 28 20 70 54 65 6d 70 20 29 7b 0a 20 20 20 20  f( pTemp ){.    
16bab 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 2b 6e    memcpy(pTemp+n
16bac 53 6b 69 70 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69  Skip, pCell+nSki
16bad 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20  p, sz-nSkip);.  
16bae 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d      pCell = pTem
16baf 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 20 3d  p;.    }.    j =
16bb0 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
16bb1 77 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28  w++;.    assert(
16bb2 20 6a 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28   j<(int)(sizeof(
16bb3 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 2f 73 69  pPage->aOvfl)/si
16bb4 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66  zeof(pPage->aOvf
16bb5 6c 5b 30 5d 29 29 20 29 3b 0a 20 20 20 20 70 50  l[0])) );.    pP
16bb6 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43  age->aOvfl[j].pC
16bb7 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20  ell = pCell;.   
16bb8 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d   pPage->aOvfl[j]
16bb9 2e 69 64 78 20 3d 20 28 75 31 36 29 69 3b 0a 20  .idx = (u16)i;. 
16bba 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20     pPage->nFree 
16bbb 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
16bbc 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
16bbd 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
16bbe 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ge->pDbPage);.  
16bbf 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16bc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
16bc1 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
16bc2 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16bc3 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
16bc4 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
16bc5 29 20 29 3b 0a 20 20 20 20 64 61 74 61 20 3d 20  ) );.    data = 
16bc6 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20  pPage->aData;.  
16bc7 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68    hdr = pPage->h
16bc8 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 74 6f  drOffset;.    to
16bc9 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  p = get2byte(&da
16bca 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20  ta[hdr+5]);.    
16bcb 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61  cellOffset = pPa
16bcc 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a  ge->cellOffset;.
16bcd 20 20 20 20 65 6e 64 20 3d 20 63 65 6c 6c 4f 66      end = cellOf
16bce 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e  fset + 2*pPage->
16bcf 6e 43 65 6c 6c 20 2b 20 32 3b 0a 20 20 20 20 69  nCell + 2;.    i
16bd0 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20  ns = cellOffset 
16bd1 2b 20 32 2a 69 3b 0a 20 20 20 20 69 66 28 20 65  + 2*i;.    if( e
16bd2 6e 64 20 3e 20 74 6f 70 20 2d 20 73 7a 20 29 7b  nd > top - sz ){
16bd3 0a 20 20 20 20 20 20 72 63 20 3d 20 64 65 66 72  .      rc = defr
16bd4 61 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65  agmentPage(pPage
16bd5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
16bd6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16bd7 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
16bd8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74  .      }.      t
16bd9 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  op = get2byte(&d
16bda 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20  ata[hdr+5]);.   
16bdb 20 20 20 61 73 73 65 72 74 28 20 65 6e 64 20 2b     assert( end +
16bdc 20 73 7a 20 3c 3d 20 74 6f 70 20 29 3b 0a 20 20   sz <= top );.  
16bdd 20 20 7d 0a 20 20 20 20 69 64 78 20 3d 20 61 6c    }.    idx = al
16bde 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 67  locateSpace(pPag
16bdf 65 2c 20 73 7a 29 3b 0a 20 20 20 20 61 73 73 65  e, sz);.    asse
16be0 72 74 28 20 69 64 78 3e 30 20 29 3b 0a 20 20 20  rt( idx>0 );.   
16be1 20 61 73 73 65 72 74 28 20 65 6e 64 20 3c 3d 20   assert( end <= 
16be2 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
16be3 64 72 2b 35 5d 29 20 29 3b 0a 20 20 20 20 69 66  dr+5]) );.    if
16be4 20 28 69 64 78 2b 73 7a 20 3e 20 70 50 61 67 65   (idx+sz > pPage
16be5 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
16be6 65 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  e) {.      retur
16be7 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
16be8 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _BKPT;.    }.   
16be9 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b   pPage->nCell++;
16bea 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  .    pPage->nFre
16beb 65 20 2d 3d 20 32 3b 0a 20 20 20 20 6d 65 6d 63  e -= 2;.    memc
16bec 70 79 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b  py(&data[idx+nSk
16bed 69 70 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70  ip], pCell+nSkip
16bee 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20  , sz-nSkip);.   
16bef 20 66 6f 72 28 6a 3d 65 6e 64 2d 32 2c 20 70 74   for(j=end-2, pt
16bf0 72 3d 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e 69 6e  r=&data[j]; j>in
16bf1 73 3b 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d 32 29  s; j-=2, ptr-=2)
16bf2 7b 0a 20 20 20 20 20 20 70 74 72 5b 30 5d 20 3d  {.      ptr[0] =
16bf3 20 70 74 72 5b 2d 32 5d 3b 0a 20 20 20 20 20 20   ptr[-2];.      
16bf4 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 2d 31 5d  ptr[1] = ptr[-1]
16bf5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74 32  ;.    }.    put2
16bf6 62 79 74 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c  byte(&data[ins],
16bf7 20 69 64 78 29 3b 0a 20 20 20 20 70 75 74 32 62   idx);.    put2b
16bf8 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
16bf9 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b  , pPage->nCell);
16bfa 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
16bfb 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
16bfc 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70      if( pPage->p
16bfd 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
16bfe 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  {.      /* The c
16bff 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  ell may contain 
16c00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
16c01 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49  overflow page. I
16c02 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20  f so, write.    
16c03 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66    ** the entry f
16c04 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  or the overflow 
16c05 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f  page into the po
16c06 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20  inter map..     
16c07 20 2a 2f 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e   */.      CellIn
16c08 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 73  fo info;.      s
16c09 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
16c0a 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
16c0b 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  Cell, &info);.  
16c0c 20 20 20 20 61 73 73 65 72 74 28 20 28 69 6e 66      assert( (inf
16c0d 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e  o.nData+(pPage->
16c0e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b  intKey?0:info.nK
16c0f 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c  ey))==info.nPayl
16c10 6f 61 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28  oad );.      if(
16c11 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20   info.iOverflow 
16c12 29 7b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20  ){.        Pgno 
16c13 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62  pgnoOvfl = get4b
16c14 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e  yte(&pCell[info.
16c15 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20  iOverflow]);.   
16c16 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70       rc = ptrmap
16c17 50 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20  Put(pPage->pBt, 
16c18 70 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50  pgnoOvfl, PTRMAP
16c19 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 61 67  _OVERFLOW1, pPag
16c1a 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  e->pgno);.      
16c1b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16c1c 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
16c1d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
16c1e 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74  endif.  }..  ret
16c1f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
16c20 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69  ../*.** Add a li
16c21 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61  st of cells to a
16c22 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65   page.  The page
16c23 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69   should be initi
16c24 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54  ally empty..** T
16c25 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61  he cells are gua
16c26 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f  ranteed to fit o
16c27 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73  n the page..*/.s
16c28 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d  tatic void assem
16c29 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61  blePage(.  MemPa
16c2a 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20  ge *pPage,   /* 
16c2b 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 61  The page to be a
16c2c 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69  ssemblied */.  i
16c2d 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20  nt nCell,       
16c2e 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f   /* The number o
16c2f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74  f cells to add t
16c30 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  o this page */. 
16c31 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20   u8 **apCell,   
16c32 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74     /* Pointers t
16c33 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f  o cell bodies */
16c34 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20  .  u16 *aSize   
16c35 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66       /* Sizes of
16c36 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b   the cells */.){
16c37 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
16c38 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
16c39 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 43  nter */.  u8 *pC
16c3a 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 41  ellptr;     /* A
16c3b 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63  ddress of next c
16c3c 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ell pointer */. 
16c3d 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20   int cellbody;  
16c3e 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
16c3f 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20   next cell body 
16c40 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20  */.  u8 * const 
16c41 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
16c42 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
16c43 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64   /* Pointer to d
16c44 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a 2f  ata for pPage */
16c45 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72  .  const int hdr
16c46 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
16c47 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  set;           /
16c48 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64  * Offset of head
16c49 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20  er on pPage */. 
16c4a 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 62   const int nUsab
16c4b 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d  le = pPage->pBt-
16c4c 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20  >usableSize; /* 
16c4d 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 70  Usable size of p
16c4e 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  age */..  assert
16c4f 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
16c50 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ow==0 );.  asser
16c51 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
16c52 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
16c53 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
16c54 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 26  sert( nCell>=0 &
16c55 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c  & nCell<=MX_CELL
16c56 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20  (pPage->pBt) && 
16c57 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70  MX_CELL(pPage->p
16c58 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61  Bt)<=5460 );.  a
16c59 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
16c5a 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
16c5b 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
16c5c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  ;..  /* Check th
16c5d 61 74 20 74 68 65 20 70 61 67 65 20 68 61 73 20  at the page has 
16c5e 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 64  just been zeroed
16c5f 20 62 79 20 7a 65 72 6f 50 61 67 65 28 29 20 2a   by zeroPage() *
16c60 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  /.  assert( pPag
16c61 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20  e->nCell==0 );. 
16c62 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 74   assert( get2byt
16c63 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d  e(&data[hdr+5])=
16c64 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70  =nUsable );..  p
16c65 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 74 61 5b  Cellptr = &data[
16c66 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
16c67 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20  t + nCell*2];.  
16c68 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 62  cellbody = nUsab
16c69 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c  le;.  for(i=nCel
16c6a 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b  l-1; i>=0; i--){
16c6b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 20 2d 3d  .    pCellptr -=
16c6c 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64 79   2;.    cellbody
16c6d 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20   -= aSize[i];.  
16c6e 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c    put2byte(pCell
16c6f 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a  ptr, cellbody);.
16c70 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61      memcpy(&data
16c71 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 65  [cellbody], apCe
16c72 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d 29  ll[i], aSize[i])
16c73 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65  ;.  }.  put2byte
16c74 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e  (&data[hdr+3], n
16c75 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74  Cell);.  put2byt
16c76 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20  e(&data[hdr+5], 
16c77 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61  cellbody);.  pPa
16c78 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43  ge->nFree -= (nC
16c79 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20  ell*2 + nUsable 
16c7a 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70  - cellbody);.  p
16c7b 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75  Page->nCell = (u
16c7c 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  16)nCell;.}../*.
16c7d 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
16c7e 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65   parameters dete
16c7f 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61  rmine how many a
16c80 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65  djacent pages ge
16c81 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e  t involved.** in
16c82 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65   a balancing ope
16c83 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74  ration.  NN is t
16c84 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69  he number of nei
16c85 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72  ghbors on either
16c86 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20   side.** of the 
16c87 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63  page that partic
16c88 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c  ipate in the bal
16c89 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ancing operation
16c8a 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20  .  NB is the.** 
16c8b 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
16c8c 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69  pages that parti
16c8d 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e  cipate, includin
16c8e 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67  g the target pag
16c8f 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67  e and.** NN neig
16c90 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20  hbors on either 
16c91 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  side..**.** The 
16c92 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  minimum value of
16c93 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75   NN is 1 (of cou
16c94 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e  rse).  Increasin
16c95 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20  g NN above 1.** 
16c96 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65  (to 2 or 3) give
16c97 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f  s a modest impro
16c98 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54  vement in SELECT
16c99 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66   and DELETE perf
16c9a 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78  ormance.** in ex
16c9b 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72  change for a lar
16c9c 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20  ger degradation 
16c9d 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50  in INSERT and UP
16c9e 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65  DATE performance
16c9f 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f  ..** The value o
16ca0 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20  f NN appears to 
16ca1 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65  give the best re
16ca2 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a  sults overall..*
16ca3 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20  /.#define NN 1  
16ca4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
16ca5 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72  mber of neighbor
16ca6 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65  s on either side
16ca7 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65   of pPage */.#de
16ca8 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29  fine NB (NN*2+1)
16ca9 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70        /* Total p
16caa 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e  ages involved in
16cab 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a   the balance */.
16cac 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
16cad 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20  rence */.static 
16cae 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 74 43 75  int balance(BtCu
16caf 72 73 6f 72 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69  rsor*, int);..#i
16cb0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16cb1 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f  T_QUICKBALANCE./
16cb2 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f  *.** This versio
16cb3 6e 20 6f 66 20 62 61 6c 61 6e 63 65 28 29 20 68  n of balance() h
16cb4 61 6e 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f  andles the commo
16cb5 6e 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77  n special case w
16cb6 68 65 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e  here.** a new en
16cb7 74 72 79 20 69 73 20 62 65 69 6e 67 20 69 6e 73  try is being ins
16cb8 65 72 74 65 64 20 6f 6e 20 74 68 65 20 65 78 74  erted on the ext
16cb9 72 65 6d 65 20 72 69 67 68 74 2d 65 6e 64 20 6f  reme right-end o
16cba 66 20 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69  f the.** tree, i
16cbb 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77  n other words, w
16cbc 68 65 6e 20 74 68 65 20 6e 65 77 20 65 6e 74 72  hen the new entr
16cbd 79 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68  y will become th
16cbe 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74  e largest.** ent
16cbf 72 79 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a  ry in the tree..
16cc0 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66  **.** Instead of
16cc1 20 74 72 79 69 6e 67 20 62 61 6c 61 6e 63 65 20   trying balance 
16cc2 74 68 65 20 33 20 72 69 67 68 74 2d 6d 6f 73 74  the 3 right-most
16cc3 20 6c 65 61 66 20 70 61 67 65 73 2c 20 6a 75 73   leaf pages, jus
16cc4 74 20 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70  t add.** a new p
16cc5 61 67 65 20 74 6f 20 74 68 65 20 72 69 67 68 74  age to the right
16cc6 2d 68 61 6e 64 20 73 69 64 65 20 61 6e 64 20 70  -hand side and p
16cc7 75 74 20 74 68 65 20 6f 6e 65 20 6e 65 77 20 65  ut the one new e
16cc8 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20  ntry in.** that 
16cc9 70 61 67 65 2e 20 20 54 68 69 73 20 6c 65 61 76  page.  This leav
16cca 65 73 20 74 68 65 20 72 69 67 68 74 20 73 69 64  es the right sid
16ccb 65 20 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f  e of the tree so
16ccc 6d 65 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61  mewhat.** unbala
16ccd 6e 63 65 64 2e 20 20 42 75 74 20 6f 64 64 73 20  nced.  But odds 
16cce 61 72 65 20 74 68 61 74 20 77 65 20 77 69 6c 6c  are that we will
16ccf 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65   be inserting ne
16cd0 77 20 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20  w entries.** at 
16cd1 74 68 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74  the end soon aft
16cd2 65 72 77 61 72 64 73 20 73 6f 20 74 68 65 20 6e  erwards so the n
16cd3 65 61 72 6c 79 20 65 6d 70 74 79 20 70 61 67 65  early empty page
16cd4 20 77 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a   will quickly.**
16cd5 20 66 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76   fill up.  On av
16cd6 65 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61  erage..**.** pPa
16cd7 67 65 20 69 73 20 74 68 65 20 6c 65 61 66 20 70  ge is the leaf p
16cd8 61 67 65 20 77 68 69 63 68 20 69 73 20 74 68 65  age which is the
16cd9 20 72 69 67 68 74 2d 6d 6f 73 74 20 70 61 67 65   right-most page
16cda 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a   in the tree..**
16cdb 20 70 50 61 72 65 6e 74 20 69 73 20 69 74 73 20   pParent is its 
16cdc 70 61 72 65 6e 74 2e 20 20 70 50 61 67 65 20 6d  parent.  pPage m
16cdd 75 73 74 20 68 61 76 65 20 61 20 73 69 6e 67 6c  ust have a singl
16cde 65 20 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72 79  e overflow entry
16cdf 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61 6c 73  .** which is als
16ce0 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  o the right-most
16ce1 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 61   entry on the pa
16ce2 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ge..*/.static in
16ce3 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28  t balance_quick(
16ce4 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
16ce5 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d  .  int rc;.  Mem
16ce6 50 61 67 65 20 2a 70 4e 65 77 20 3d 20 30 3b 0a  Page *pNew = 0;.
16ce7 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 0a    Pgno pgnoNew;.
16ce8 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 75    u8 *pCell;.  u
16ce9 31 36 20 73 7a 43 65 6c 6c 3b 0a 20 20 43 65 6c  16 szCell;.  Cel
16cea 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 4d 65  lInfo info;.  Me
16ceb 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70  mPage *pPage = p
16cec 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
16ced 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 4d 65 6d 50  ->iPage];.  MemP
16cee 61 67 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  age *pParent = p
16cef 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
16cf0 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 42 74  ->iPage-1];.  Bt
16cf1 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50  Shared *pBt = pP
16cf2 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20  age->pBt;.  int 
16cf3 70 61 72 65 6e 74 49 64 78 20 3d 20 70 50 61 72  parentIdx = pPar
16cf4 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 20 20 20 2f 2a  ent->nCell;   /*
16cf5 20 70 50 61 72 65 6e 74 20 6e 65 77 20 64 69 76   pParent new div
16cf6 69 64 65 72 20 63 65 6c 6c 20 69 6e 64 65 78 20  ider cell index 
16cf7 2a 2f 0a 20 20 69 6e 74 20 70 61 72 65 6e 74 53  */.  int parentS
16cf8 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
16cf9 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
16cfa 66 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65  f new divider ce
16cfb 6c 6c 20 2a 2f 0a 20 20 75 38 20 70 61 72 65 6e  ll */.  u8 paren
16cfc 74 43 65 6c 6c 5b 36 34 5d 3b 20 20 20 20 20 20  tCell[64];      
16cfd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
16cfe 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64  ce for the new d
16cff 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 0a  ivider cell */..
16d00 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16d01 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
16d02 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
16d03 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  );..  /* Allocat
16d04 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 49 6e  e a new page. In
16d05 73 65 72 74 20 74 68 65 20 6f 76 65 72 66 6c 6f  sert the overflo
16d06 77 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67  w cell from pPag
16d07 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 69 74 2e 20  e.  ** into it. 
16d08 54 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20  Then remove the 
16d09 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72  overflow cell fr
16d0a 6f 6d 20 70 50 61 67 65 2e 0a 20 20 2a 2f 0a 20  om pPage..  */. 
16d0b 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74   rc = allocateBt
16d0c 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e  reePage(pBt, &pN
16d0d 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c  ew, &pgnoNew, 0,
16d0e 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   0);.  if( rc==S
16d0f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16d10 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61  pCell = pPage->a
16d11 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20  Ovfl[0].pCell;. 
16d12 20 20 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c     szCell = cell
16d13 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70  SizePtr(pPage, p
16d14 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73 73 65 72  Cell);.    asser
16d15 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
16d16 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77 2d  swriteable(pNew-
16d17 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20  >pDbPage) );.   
16d18 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20   zeroPage(pNew, 
16d19 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 29  pPage->aData[0])
16d1a 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61  ;.    assemblePa
16d1b 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 65  ge(pNew, 1, &pCe
16d1c 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20 20  ll, &szCell);.  
16d1d 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c    pPage->nOverfl
16d1e 6f 77 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 2f  ow = 0;.  .    /
16d1f 2a 20 70 50 61 67 65 20 69 73 20 63 75 72 72 65  * pPage is curre
16d20 6e 74 6c 79 20 74 68 65 20 72 69 67 68 74 2d 63  ntly the right-c
16d21 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 2e  hild of pParent.
16d22 20 43 68 61 6e 67 65 20 74 68 69 73 0a 20 20 20   Change this.   
16d23 20 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20   ** so that the 
16d24 72 69 67 68 74 2d 63 68 69 6c 64 20 69 73 20 74  right-child is t
16d25 68 65 20 6e 65 77 20 70 61 67 65 20 61 6c 6c 6f  he new page allo
16d26 63 61 74 65 64 20 61 62 6f 76 65 20 61 6e 64 0a  cated above and.
16d27 20 20 20 20 2a 2a 20 70 50 61 67 65 20 69 73 20      ** pPage is 
16d28 74 68 65 20 6e 65 78 74 2d 74 6f 2d 72 69 67 68  the next-to-righ
16d29 74 20 63 68 69 6c 64 2e 20 0a 20 20 20 20 2a 2a  t child. .    **
16d2a 0a 20 20 20 20 2a 2a 20 49 67 6e 6f 72 65 20 74  .    ** Ignore t
16d2b 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
16d2c 6f 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 66  of the call to f
16d2d 69 6c 6c 49 6e 43 65 6c 6c 28 29 2e 20 66 69 6c  illInCell(). fil
16d2e 6c 49 6e 43 65 6c 6c 28 29 0a 20 20 20 20 2a 2a  lInCell().    **
16d2f 20 6d 61 79 20 6f 6e 6c 79 20 72 65 74 75 72 6e   may only return
16d30 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49   other than SQLI
16d31 54 45 5f 4f 4b 20 69 66 20 69 74 20 69 73 20 72  TE_OK if it is r
16d32 65 71 75 69 72 65 64 20 74 6f 20 61 6c 6c 6f 63  equired to alloc
16d33 61 74 65 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6f  ate.    ** one o
16d34 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20  r more overflow 
16d35 70 61 67 65 73 2e 20 53 69 6e 63 65 20 61 6e 20  pages. Since an 
16d36 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 42  internal table B
16d37 2d 54 72 65 65 20 63 65 6c 6c 20 0a 20 20 20 20  -Tree cell .    
16d38 2a 2a 20 6d 61 79 20 6e 65 76 65 72 20 73 70 69  ** may never spi
16d39 6c 6c 20 6f 76 65 72 20 6f 6e 74 6f 20 61 6e 20  ll over onto an 
16d3a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 28 69  overflow page (i
16d3b 74 20 69 73 20 61 20 6d 61 78 69 6d 75 6d 20 6f  t is a maximum o
16d3c 66 20 0a 20 20 20 20 2a 2a 20 31 33 20 62 79 74  f .    ** 13 byt
16d3d 65 73 20 69 6e 20 73 69 7a 65 29 2c 20 69 74 20  es in size), it 
16d3e 69 73 20 6e 6f 74 20 6e 65 63 63 65 73 73 61 72  is not neccessar
16d3f 79 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72  y to check the r
16d40 65 74 75 72 6e 20 63 6f 64 65 2e 0a 20 20 20 20  eturn code..    
16d41 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c 61  **.    ** Simila
16d42 72 6c 79 2c 20 74 68 65 20 69 6e 73 65 72 74 43  rly, the insertC
16d43 65 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 63  ell() function c
16d44 61 6e 6e 6f 74 20 66 61 69 6c 20 69 66 20 74 68  annot fail if th
16d45 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 62 65  e page.    ** be
16d46 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74  ing inserted int
16d47 6f 20 69 73 20 61 6c 72 65 61 64 79 20 77 72 69  o is already wri
16d48 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 63 65  table and the ce
16d49 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 0a 20 20 20  ll does not .   
16d4a 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 6e 20 6f   ** contain an o
16d4b 76 65 72 66 6c 6f 77 20 70 6f 69 6e 74 65 72 2e  verflow pointer.
16d4c 20 53 6f 20 69 67 6e 6f 72 65 20 74 68 69 73 20   So ignore this 
16d4d 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f 6f 2e  return code too.
16d4e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
16d4f 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  rt( pPage->nCell
16d50 3e 30 20 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20  >0 );.    pCell 
16d51 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  = findCell(pPage
16d52 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31  , pPage->nCell-1
16d53 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
16d54 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
16d55 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69  pPage, pCell, &i
16d56 6e 66 6f 29 3b 0a 20 20 20 20 66 69 6c 6c 49 6e  nfo);.    fillIn
16d57 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61  Cell(pParent, pa
16d58 72 65 6e 74 43 65 6c 6c 2c 20 30 2c 20 69 6e 66  rentCell, 0, inf
16d59 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c 20 30 2c  o.nKey, 0, 0, 0,
16d5a 20 26 70 61 72 65 6e 74 53 69 7a 65 29 3b 0a 20   &parentSize);. 
16d5b 20 20 20 61 73 73 65 72 74 28 20 70 61 72 65 6e     assert( paren
16d5c 74 53 69 7a 65 3c 36 34 20 29 3b 0a 20 20 20 20  tSize<64 );.    
16d5d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
16d5e 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
16d5f 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65  pParent->pDbPage
16d60 29 20 29 3b 0a 20 20 20 20 69 6e 73 65 72 74 43  ) );.    insertC
16d61 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 72  ell(pParent, par
16d62 65 6e 74 49 64 78 2c 20 70 61 72 65 6e 74 43 65  entIdx, parentCe
16d63 6c 6c 2c 20 70 61 72 65 6e 74 53 69 7a 65 2c 20  ll, parentSize, 
16d64 30 2c 20 34 29 3b 0a 20 20 20 20 70 75 74 34 62  0, 4);.    put4b
16d65 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77  yte(findOverflow
16d66 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 70 61 72  Cell(pParent,par
16d67 65 6e 74 49 64 78 29 2c 20 70 50 61 67 65 2d 3e  entIdx), pPage->
16d68 70 67 6e 6f 29 3b 0a 20 20 20 20 70 75 74 34 62  pgno);.    put4b
16d69 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44  yte(&pParent->aD
16d6a 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72  ata[pParent->hdr
16d6b 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e  Offset+8], pgnoN
16d6c 65 77 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49  ew);.  .    /* I
16d6d 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74  f this is an aut
16d6e 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  o-vacuum databas
16d6f 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f  e, update the po
16d70 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a  inter map.    **
16d71 20 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f   with entries fo
16d72 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20  r the new page, 
16d73 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20  and any pointer 
16d74 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a  from the .    **
16d75 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67   cell on the pag
16d76 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77  e to an overflow
16d77 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   page..    */.  
16d78 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
16d79 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  UM ){.      rc =
16d7a 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
16d7b 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f  pgnoNew, PTRMAP_
16d7c 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e  BTREE, pParent->
16d7d 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28  pgno);.      if(
16d7e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
16d7f 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  {.        rc = p
16d80 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65  trmapPutOvfl(pNe
16d81 77 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  w, 0);.      }. 
16d82 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c     }..    /* Rel
16d83 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e  ease the referen
16d84 63 65 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61  ce to the new pa
16d85 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65 61  ge. */.    relea
16d86 73 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 20  sePage(pNew);.  
16d87 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20  }..  /* At this 
16d88 70 6f 69 6e 74 20 74 68 65 20 70 50 61 67 65 2d  point the pPage-
16d89 3e 6e 46 72 65 65 20 76 61 72 69 61 62 6c 65 20  >nFree variable 
16d8a 69 73 20 6e 6f 74 20 73 65 74 20 63 6f 72 72 65  is not set corre
16d8b 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20 72  ctly with.  ** r
16d8c 65 73 70 65 63 74 20 74 6f 20 74 68 65 20 63 6f  espect to the co
16d8d 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67  ntent of the pag
16d8e 65 20 28 62 65 63 61 75 73 65 20 69 74 20 77 61  e (because it wa
16d8f 73 20 73 65 74 20 74 6f 20 30 20 62 79 20 0a 20  s set to 0 by . 
16d90 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 29 2e   ** insertCell).
16d91 20 53 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   So call sqlite3
16d92 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20  BtreeInitPage() 
16d93 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  to make sure it 
16d94 69 73 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 2e  is.  ** correct.
16d95 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
16d96 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 20 65  has to be done e
16d97 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ven if an error 
16d98 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64  will be returned
16d99 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 0a 20  . Normally, if. 
16d9a 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   ** an error occ
16d9b 75 72 73 20 64 75 72 69 6e 67 20 74 72 65 65 20  urs during tree 
16d9c 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 20 63  balancing, the c
16d9d 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65 6d 50 61  ontents of MemPa
16d9e 67 65 20 61 72 65 0a 20 20 2a 2a 20 6e 6f 74 20  ge are.  ** not 
16d9f 69 6d 70 6f 72 74 61 6e 74 2c 20 61 73 20 74 68  important, as th
16da0 65 79 20 77 69 6c 6c 20 62 65 20 72 65 63 61 6c  ey will be recal
16da1 63 75 6c 61 74 65 64 20 77 68 65 6e 20 74 68 65  culated when the
16da2 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 0a   page is rolled.
16da3 20 20 2a 2a 20 62 61 63 6b 2e 20 42 75 74 20 68    ** back. But h
16da4 65 72 65 2c 20 69 6e 20 62 61 6c 61 6e 63 65 5f  ere, in balance_
16da5 71 75 69 63 6b 28 29 2c 20 69 74 20 69 73 20 70  quick(), it is p
16da6 6f 73 73 69 62 6c 65 20 74 68 61 74 20 70 50 61  ossible that pPa
16da7 67 65 20 68 61 73 20 0a 20 20 2a 2a 20 6e 6f 74  ge has .  ** not
16da8 20 79 65 74 20 62 65 65 6e 20 6d 61 72 6b 65 64   yet been marked
16da9 20 64 69 72 74 79 20 6f 72 20 77 72 69 74 74 65   dirty or writte
16daa 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  n into the journ
16dab 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 66 6f  al file. Therefo
16dac 72 65 0a 20 20 2a 2a 20 69 74 20 77 69 6c 6c 20  re.  ** it will 
16dad 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  not be rolled ba
16dae 63 6b 20 61 6e 64 20 73 6f 20 69 74 20 69 73 20  ck and so it is 
16daf 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6d 61 6b  important to mak
16db0 65 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a  e sure that.  **
16db1 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 61   the page data a
16db2 6e 64 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d  nd contents of M
16db3 65 6d 50 61 67 65 20 61 72 65 20 63 6f 6e 73 69  emPage are consi
16db4 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 50  stent..  */.  pP
16db5 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b  age->isInit = 0;
16db6 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  .  sqlite3BtreeI
16db7 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a  nitPage(pPage);.
16db8 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
16db9 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b  >nOverflow==0 );
16dba 0a 0a 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74  ..  /* If everyt
16dbb 68 69 6e 67 20 65 6c 73 65 20 73 75 63 63 65 65  hing else succee
16dbc 64 65 64 2c 20 62 61 6c 61 6e 63 65 20 74 68 65  ded, balance the
16dbd 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 69 6e   parent page, in
16dbe 20 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20   .  ** case the 
16dbf 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 73  divider cell ins
16dc0 65 72 74 65 64 20 63 61 75 73 65 64 20 69 74 20  erted caused it 
16dc1 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75  to become overfu
16dc2 6c 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  ll..  */.  if( r
16dc3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16dc4 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
16dc5 70 50 61 67 65 29 3b 0a 20 20 20 20 70 43 75 72  pPage);.    pCur
16dc6 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20 72  ->iPage--;.    r
16dc7 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72  c = balance(pCur
16dc8 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
16dc9 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20  rn rc;.}.#endif 
16dca 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51  /* SQLITE_OMIT_Q
16dcb 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a  UICKBALANCE */..
16dcc 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
16dcd 6e 65 20 72 65 64 69 73 74 72 69 62 75 74 65 73  ne redistributes
16dce 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 20   Cells on pPage 
16dcf 61 6e 64 20 75 70 20 74 6f 20 4e 4e 2a 32 20 73  and up to NN*2 s
16dd0 69 62 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20 70 50  iblings.** of pP
16dd1 61 67 65 20 73 6f 20 74 68 61 74 20 61 6c 6c 20  age so that all 
16dd2 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 74  pages have about
16dd3 20 74 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e 74   the same amount
16dd4 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e 0a   of free space..
16dd5 2a 2a 20 55 73 75 61 6c 6c 79 20 4e 4e 20 73 69  ** Usually NN si
16dd6 62 6c 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72  blings on either
16dd7 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 20 69   side of pPage i
16dd8 73 20 75 73 65 64 20 69 6e 20 74 68 65 20 62 61  s used in the ba
16dd9 6c 61 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68 6f 75  lancing,.** thou
16dda 67 68 20 6d 6f 72 65 20 73 69 62 6c 69 6e 67 73  gh more siblings
16ddb 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 72 6f 6d   might come from
16ddc 20 6f 6e 65 20 73 69 64 65 20 69 66 20 70 50 61   one side if pPa
16ddd 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74 0a  ge is the first.
16dde 2a 2a 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64  ** or last child
16ddf 20 6f 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20   of its parent. 
16de0 20 49 66 20 70 50 61 67 65 20 68 61 73 20 66 65   If pPage has fe
16de1 77 65 72 20 74 68 61 6e 20 32 2a 4e 4e 20 73 69  wer than 2*NN si
16de2 62 6c 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d 65 74  blings.** (somet
16de3 68 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f  hing which can o
16de4 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 70 50  nly happen if pP
16de5 61 67 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20  age is the root 
16de6 70 61 67 65 20 6f 72 20 61 20 0a 2a 2a 20 63 68  page or a .** ch
16de7 69 6c 64 20 6f 66 20 72 6f 6f 74 29 20 74 68 65  ild of root) the
16de8 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20  n all available 
16de9 73 69 62 6c 69 6e 67 73 20 70 61 72 74 69 63 69  siblings partici
16dea 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61  pate in the bala
16deb 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ncing..**.** The
16dec 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69   number of sibli
16ded 6e 67 73 20 6f 66 20 70 50 61 67 65 20 6d 69 67  ngs of pPage mig
16dee 68 74 20 62 65 20 69 6e 63 72 65 61 73 65 64 20  ht be increased 
16def 6f 72 20 64 65 63 72 65 61 73 65 64 20 62 79 20  or decreased by 
16df0 6f 6e 65 20 6f 72 0a 2a 2a 20 74 77 6f 20 69 6e  one or.** two in
16df1 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65   an effort to ke
16df2 65 70 20 70 61 67 65 73 20 6e 65 61 72 6c 79 20  ep pages nearly 
16df3 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65  full but not ove
16df4 72 20 66 75 6c 6c 2e 20 54 68 65 20 72 6f 6f 74  r full. The root
16df5 20 70 61 67 65 0a 2a 2a 20 69 73 20 73 70 65 63   page.** is spec
16df6 69 61 6c 20 61 6e 64 20 69 73 20 61 6c 6c 6f 77  ial and is allow
16df7 65 64 20 74 6f 20 62 65 20 6e 65 61 72 6c 79 20  ed to be nearly 
16df8 65 6d 70 74 79 2e 20 49 66 20 70 50 61 67 65 20  empty. If pPage 
16df9 69 73 20 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20  is .** the root 
16dfa 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 64  page, then the d
16dfb 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65  epth of the tree
16dfc 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61   might be increa
16dfd 73 65 64 0a 2a 2a 20 6f 72 20 64 65 63 72 65 61  sed.** or decrea
16dfe 73 65 64 20 62 79 20 6f 6e 65 2c 20 61 73 20 6e  sed by one, as n
16dff 65 63 65 73 73 61 72 79 2c 20 74 6f 20 6b 65 65  ecessary, to kee
16e00 70 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  p the root page 
16e01 66 72 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20 6f 76  from being.** ov
16e02 65 72 66 75 6c 6c 20 6f 72 20 63 6f 6d 70 6c 65  erfull or comple
16e03 74 65 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a  tely empty..**.*
16e04 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e  * Note that when
16e05 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
16e06 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66   called, some of
16e07 20 74 68 65 20 43 65 6c 6c 73 20 6f 6e 20 70 50   the Cells on pP
16e08 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74  age.** might not
16e09 20 61 63 74 75 61 6c 6c 79 20 62 65 20 73 74 6f   actually be sto
16e0a 72 65 64 20 69 6e 20 70 50 61 67 65 2d 3e 61 44  red in pPage->aD
16e0b 61 74 61 5b 5d 2e 20 20 54 68 69 73 20 63 61 6e  ata[].  This can
16e0c 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68   happen.** if th
16e0d 65 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 75  e page is overfu
16e0e 6c 6c 2e 20 20 50 61 72 74 20 6f 66 20 74 68 65  ll.  Part of the
16e0f 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75   job of this rou
16e10 74 69 6e 65 20 69 73 20 74 6f 0a 2a 2a 20 6d 61  tine is to.** ma
16e11 6b 65 20 73 75 72 65 20 61 6c 6c 20 43 65 6c 6c  ke sure all Cell
16e12 73 20 66 6f 72 20 70 50 61 67 65 20 6f 6e 63 65  s for pPage once
16e13 20 61 67 61 69 6e 20 66 69 74 20 69 6e 20 70 50   again fit in pP
16e14 61 67 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a 2a 2a  age->aData[]..**
16e15 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75 72 73  .** In the cours
16e16 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20 74  e of balancing t
16e17 68 65 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 70  he siblings of p
16e18 50 61 67 65 2c 20 74 68 65 20 70 61 72 65 6e 74  Page, the parent
16e19 20 6f 66 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67   of pPage.** mig
16e1a 68 74 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75  ht become overfu
16e1b 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e  ll or underfull.
16e1c 20 20 49 66 20 74 68 61 74 20 68 61 70 70 65 6e    If that happen
16e1d 73 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  s, then this rou
16e1e 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65  tine.** is calle
16e1f 64 20 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e  d recursively on
16e20 20 74 68 65 20 70 61 72 65 6e 74 2e 0a 2a 2a 0a   the parent..**.
16e21 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  ** If this routi
16e22 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79  ne fails for any
16e23 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68   reason, it migh
16e24 74 20 6c 65 61 76 65 20 74 68 65 20 64 61 74 61  t leave the data
16e25 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72  base.** in a cor
16e26 72 75 70 74 65 64 20 73 74 61 74 65 2e 20 20 53  rupted state.  S
16e27 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e  o if this routin
16e28 65 20 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74  e fails, the dat
16e29 61 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20  abase should.** 
16e2a 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a  be rolled back..
16e2b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61  */.static int ba
16e2c 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 42 74  lance_nonroot(Bt
16e2d 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
16e2e 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
16e2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16e30 20 54 68 65 20 6f 76 65 72 20 6f 72 20 75 6e 64   The over or und
16e31 65 72 66 75 6c 6c 20 70 61 67 65 20 74 6f 20 62  erfull page to b
16e32 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 4d 65 6d 50  alance */.  MemP
16e33 61 67 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20  age *pParent;   
16e34 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
16e35 70 61 72 65 6e 74 20 6f 66 20 70 50 61 67 65 20  parent of pPage 
16e36 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
16e37 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Bt;             
16e38 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64    /* The whole d
16e39 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74  atabase */.  int
16e3a 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20   nCell = 0;     
16e3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
16e3c 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20  ber of cells in 
16e3d 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e  apCell[] */.  in
16e3e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b  t nMaxCells = 0;
16e3f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c             /* Al
16e40 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20  located size of 
16e41 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20  apCell, szCell, 
16e42 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20  aFrom. */.  int 
16e43 6e 4f 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20  nOld = 0;       
16e44 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
16e45 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61  er of pages in a
16e46 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  pOld[] */.  int 
16e47 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20  nNew = 0;       
16e48 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
16e49 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61  er of pages in a
16e4a 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  pNew[] */.  int 
16e4b 6e 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20  nDiv;           
16e4c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
16e4d 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61  er of cells in a
16e4e 70 44 69 76 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  pDiv[] */.  int 
16e4f 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20  i, j, k;        
16e50 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
16e51 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
16e52 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20  nt idx;         
16e53 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
16e54 6e 64 65 78 20 6f 66 20 70 50 61 67 65 20 69 6e  ndex of pPage in
16e55 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b   pParent->aCell[
16e56 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69 76  ] */.  int nxDiv
16e57 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
16e58 20 20 20 20 2f 2a 20 4e 65 78 74 20 64 69 76 69      /* Next divi
16e59 64 65 72 20 73 6c 6f 74 20 69 6e 20 70 50 61 72  der slot in pPar
16e5a 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a  ent->aCell[] */.
16e5b 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
16e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16e5d 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64  * The return cod
16e5e 65 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 43  e */.  int leafC
16e5f 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 20  orrection;      
16e60 20 20 20 20 2f 2a 20 34 20 69 66 20 70 50 61 67      /* 4 if pPag
16e61 65 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20  e is a leaf.  0 
16e62 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 20  if not */.  int 
16e63 6c 65 61 66 44 61 74 61 3b 20 20 20 20 20 20 20  leafData;       
16e64 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
16e65 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c   if pPage is a l
16e66 65 61 66 20 6f 66 20 61 20 4c 45 41 46 44 41 54  eaf of a LEAFDAT
16e67 41 20 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20  A tree */.  int 
16e68 75 73 61 62 6c 65 53 70 61 63 65 3b 20 20 20 20  usableSpace;    
16e69 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
16e6a 73 20 69 6e 20 70 50 61 67 65 20 62 65 79 6f 6e  s in pPage beyon
16e6b 64 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a  d the header */.
16e6c 20 20 69 6e 74 20 70 61 67 65 46 6c 61 67 73 3b    int pageFlags;
16e6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16e6e 2a 20 56 61 6c 75 65 20 6f 66 20 70 50 61 67 65  * Value of pPage
16e6f 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 20  ->aData[0] */.  
16e70 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b 20 20 20  int subtotal;   
16e71 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16e72 53 75 62 74 6f 74 61 6c 20 6f 66 20 62 79 74 65  Subtotal of byte
16e73 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e  s in cells on on
16e74 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  e page */.  int 
16e75 69 53 70 61 63 65 31 20 3d 20 30 3b 20 20 20 20  iSpace1 = 0;    
16e76 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
16e77 74 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66  t unused byte of
16e78 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 20   aSpace1[] */.  
16e79 69 6e 74 20 69 53 70 61 63 65 32 20 3d 20 30 3b  int iSpace2 = 0;
16e7a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16e7b 46 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74  First unused byt
16e7c 65 20 6f 66 20 61 53 70 61 63 65 32 5b 5d 20 2a  e of aSpace2[] *
16e7d 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63  /.  int szScratc
16e7e 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
16e7f 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61   /* Size of scra
16e80 74 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65  tch memory reque
16e81 73 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  sted */.  MemPag
16e82 65 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20  e *apOld[NB];   
16e83 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20         /* pPage 
16e84 61 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69  and up to two si
16e85 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 50 67 6e 6f  blings */.  Pgno
16e86 20 70 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b 20 20 20   pgnoOld[NB];   
16e87 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
16e88 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63   numbers for eac
16e89 68 20 70 61 67 65 20 69 6e 20 61 70 4f 6c 64 5b  h page in apOld[
16e8a 5d 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ] */.  MemPage *
16e8b 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20  apCopy[NB];     
16e8c 20 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63      /* Private c
16e8d 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d  opies of apOld[]
16e8e 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50   pages */.  MemP
16e8f 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d  age *apNew[NB+2]
16e90 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67  ;        /* pPag
16e91 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73  e and up to NB s
16e92 69 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61  iblings after ba
16e93 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 50 67 6e  lancing */.  Pgn
16e94 6f 20 70 67 6e 6f 4e 65 77 5b 4e 42 2b 32 5d 3b  o pgnoNew[NB+2];
16e95 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
16e96 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61  e numbers for ea
16e97 63 68 20 70 61 67 65 20 69 6e 20 61 70 4e 65 77  ch page in apNew
16e98 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69  [] */.  u8 *apDi
16e99 76 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20  v[NB];          
16e9a 20 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72 20       /* Divider 
16e9b 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74  cells in pParent
16e9c 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77   */.  int cntNew
16e9d 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20  [NB+2];         
16e9e 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61     /* Index in a
16e9f 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61  Cell[] of cell a
16ea0 66 74 65 72 20 69 2d 74 68 20 70 61 67 65 20 2a  fter i-th page *
16ea1 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e 42  /.  int szNew[NB
16ea2 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  +2];            
16ea3 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 7a   /* Combined siz
16ea4 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 65  e of cells place
16ea5 20 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a 2f   on i-th page */
16ea6 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 3d  .  u8 **apCell =
16ea7 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
16ea8 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 67  /* All cells beg
16ea9 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20  in balanced */. 
16eaa 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20   u16 *szCell;   
16eab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16eac 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61   Local size of a
16ead 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65  ll cells in apCe
16eae 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 43  ll[] */.  u8 *aC
16eaf 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20  opy[NB];        
16eb0 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 68 6f   /* Space for ho
16eb1 6c 64 69 6e 67 20 64 61 74 61 20 6f 66 20 61 70  lding data of ap
16eb2 43 6f 70 79 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a  Copy[] */.  u8 *
16eb3 61 53 70 61 63 65 31 3b 20 20 20 20 20 20 20 20  aSpace1;        
16eb4 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20     /* Space for 
16eb5 63 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64 65  copies of divide
16eb6 72 73 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20  rs cells before 
16eb7 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20  balance */.  u8 
16eb8 2a 61 53 70 61 63 65 32 20 3d 20 30 3b 20 20 20  *aSpace2 = 0;   
16eb9 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72      /* Space for
16eba 20 6f 76 65 72 66 6c 6f 77 20 64 69 76 69 64 65   overflow divide
16ebb 72 73 20 63 65 6c 6c 73 20 61 66 74 65 72 20 62  rs cells after b
16ebc 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a  alance */.  u8 *
16ebd 61 46 72 6f 6d 20 3d 20 30 3b 0a 0a 20 20 70 50  aFrom = 0;..  pP
16ebe 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
16ebf 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
16ec0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16ec1 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
16ec2 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
16ec3 20 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20   );.  VVA_ONLY( 
16ec4 70 43 75 72 2d 3e 70 61 67 65 73 53 68 75 66 66  pCur->pagesShuff
16ec5 6c 65 64 20 3d 20 31 20 29 3b 0a 0a 20 20 2f 2a  led = 1 );..  /*
16ec6 20 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20   .  ** Find the 
16ec7 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 2a  parent page..  *
16ec8 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  /.  assert( pCur
16ec9 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61  ->iPage>0 );.  a
16eca 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73  ssert( pPage->is
16ecb 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74  Init );.  assert
16ecc 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
16ecd 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
16ece 3e 70 44 62 50 61 67 65 29 20 7c 7c 20 70 50 61  >pDbPage) || pPa
16ecf 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31  ge->nOverflow==1
16ed0 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67   );.  pBt = pPag
16ed1 65 2d 3e 70 42 74 3b 0a 20 20 70 50 61 72 65 6e  e->pBt;.  pParen
16ed2 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  t = pCur->apPage
16ed3 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b  [pCur->iPage-1];
16ed4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65  .  assert( pPare
16ed5 6e 74 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  nt );.  if( SQLI
16ed6 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
16ed7 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
16ed8 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29  Parent->pDbPage)
16ed9 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 61  ) ){.    goto ba
16eda 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
16edb 20 7d 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41   }..  TRACE(("BA
16edc 4c 41 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67  LANCE: begin pag
16edd 65 20 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64  e %d child of %d
16ede 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  \n", pPage->pgno
16edf 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29  , pParent->pgno)
16ee0 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
16ee1 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c  TE_OMIT_QUICKBAL
16ee2 41 4e 43 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41  ANCE.  /*.  ** A
16ee3 20 73 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20   special case:  
16ee4 49 66 20 61 20 6e 65 77 20 65 6e 74 72 79 20 68  If a new entry h
16ee5 61 73 20 6a 75 73 74 20 62 65 65 6e 20 69 6e 73  as just been ins
16ee6 65 72 74 65 64 20 69 6e 74 6f 20 61 0a 20 20 2a  erted into a.  *
16ee7 2a 20 74 61 62 6c 65 20 28 74 68 61 74 20 69 73  * table (that is
16ee8 2c 20 61 20 62 74 72 65 65 20 77 69 74 68 20 69  , a btree with i
16ee9 6e 74 65 67 65 72 20 6b 65 79 73 20 61 6e 64 20  nteger keys and 
16eea 61 6c 6c 20 64 61 74 61 20 61 74 20 74 68 65 20  all data at the 
16eeb 6c 65 61 76 65 73 29 0a 20 20 2a 2a 20 61 6e 64  leaves).  ** and
16eec 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69   the new entry i
16eed 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  s the right-most
16eee 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72   entry in the tr
16eef 65 65 20 28 69 74 20 68 61 73 20 74 68 65 0a 20  ee (it has the. 
16ef0 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b 65 79 29   ** largest key)
16ef1 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 73 70   then use the sp
16ef2 65 63 69 61 6c 20 62 61 6c 61 6e 63 65 5f 71 75  ecial balance_qu
16ef3 69 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 66 6f  ick() routine fo
16ef4 72 0a 20 20 2a 2a 20 62 61 6c 61 6e 63 69 6e 67  r.  ** balancing
16ef5 2e 20 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  .  balance_quick
16ef6 28 29 20 69 73 20 6d 75 63 68 20 66 61 73 74 65  () is much faste
16ef7 72 20 61 6e 64 20 72 65 73 75 6c 74 73 20 69 6e  r and results in
16ef8 20 61 20 74 69 67 68 74 65 72 0a 20 20 2a 2a 20   a tighter.  ** 
16ef9 70 61 63 6b 69 6e 67 20 6f 66 20 64 61 74 61 20  packing of data 
16efa 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61  in the common ca
16efb 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  se..  */.  if( p
16efc 50 61 67 65 2d 3e 6c 65 61 66 20 26 26 0a 20 20  Page->leaf &&.  
16efd 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65      pPage->intKe
16efe 79 20 26 26 0a 20 20 20 20 20 20 70 50 61 67 65  y &&.      pPage
16eff 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 26  ->nOverflow==1 &
16f00 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61  &.      pPage->a
16f01 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61  Ovfl[0].idx==pPa
16f02 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 0a 20 20 20  ge->nCell &&.   
16f03 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f     pParent->pgno
16f04 21 3d 31 20 26 26 0a 20 20 20 20 20 20 67 65 74  !=1 &&.      get
16f05 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e  4byte(&pParent->
16f06 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68  aData[pParent->h
16f07 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 70 50  drOffset+8])==pP
16f08 61 67 65 2d 3e 70 67 6e 6f 0a 20 20 29 7b 0a 20  age->pgno.  ){. 
16f09 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
16f0a 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20  ->intKey );.    
16f0b 2f 2a 0a 20 20 20 20 2a 2a 20 54 4f 44 4f 3a 20  /*.    ** TODO: 
16f0c 43 68 65 63 6b 20 74 68 65 20 73 69 62 6c 69 6e  Check the siblin
16f0d 67 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  gs to the left o
16f0e 66 20 70 50 61 67 65 2e 20 49 74 20 6d 61 79 20  f pPage. It may 
16f0f 62 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74  be that.    ** t
16f10 68 65 79 20 61 72 65 20 6e 6f 74 20 66 75 6c 6c  hey are not full
16f11 20 61 6e 64 20 6e 6f 20 6e 65 77 20 70 61 67 65   and no new page
16f12 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20   is required..  
16f13 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20    */.    return 
16f14 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70 43  balance_quick(pC
16f15 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ur);.  }.#endif.
16f16 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
16f17 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50  !=(rc = sqlite3P
16f18 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
16f19 3e 70 44 62 50 61 67 65 29 29 20 29 7b 0a 20 20  >pDbPage)) ){.  
16f1a 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63    goto balance_c
16f1b 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
16f1c 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20  *.  ** Find the 
16f1d 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65  cell in the pare
16f1e 6e 74 20 70 61 67 65 20 77 68 6f 73 65 20 6c 65  nt page whose le
16f1f 66 74 20 63 68 69 6c 64 20 70 6f 69 6e 74 73 20  ft child points 
16f20 62 61 63 6b 0a 20 20 2a 2a 20 74 6f 20 70 50 61  back.  ** to pPa
16f21 67 65 2e 20 20 54 68 65 20 22 69 64 78 22 20 76  ge.  The "idx" v
16f22 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 69  ariable is the i
16f23 6e 64 65 78 20 6f 66 20 74 68 61 74 20 63 65 6c  ndex of that cel
16f24 6c 2e 20 20 49 66 20 70 50 61 67 65 0a 20 20 2a  l.  If pPage.  *
16f25 2a 20 69 73 20 74 68 65 20 72 69 67 68 74 6d 6f  * is the rightmo
16f26 73 74 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72  st child of pPar
16f27 65 6e 74 20 74 68 65 6e 20 73 65 74 20 69 64 78  ent then set idx
16f28 20 74 6f 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65   to pParent->nCe
16f29 6c 6c 20 0a 20 20 2a 2f 0a 20 20 69 64 78 20 3d  ll .  */.  idx =
16f2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
16f2b 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 61  r->iPage-1];.  a
16f2c 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78  ssertParentIndex
16f2d 28 70 50 61 72 65 6e 74 2c 20 69 64 78 2c 20 70  (pParent, idx, p
16f2e 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20  Page->pgno);..  
16f2f 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 73 69 62  /*.  ** Find sib
16f30 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 70 50  ling pages to pP
16f31 61 67 65 20 61 6e 64 20 74 68 65 20 63 65 6c 6c  age and the cell
16f32 73 20 69 6e 20 70 50 61 72 65 6e 74 20 74 68 61  s in pParent tha
16f33 74 20 64 69 76 69 64 65 0a 20 20 2a 2a 20 74 68  t divide.  ** th
16f34 65 20 73 69 62 6c 69 6e 67 73 2e 20 20 41 6e 20  e siblings.  An 
16f35 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
16f36 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69  to find NN sibli
16f37 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 0a 20 20  ngs on either.  
16f38 2a 2a 20 73 69 64 65 20 6f 66 20 70 50 61 67 65  ** side of pPage
16f39 2e 20 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73  .  More siblings
16f3a 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20   are taken from 
16f3b 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65  one side, howeve
16f3c 72 2c 20 69 66 0a 20 20 2a 2a 20 70 50 61 67 65  r, if.  ** pPage
16f3d 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65 72   there are fewer
16f3e 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e 67   than NN sibling
16f3f 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 73  s on the other s
16f40 69 64 65 2e 20 20 49 66 20 70 50 61 72 65 6e 74  ide.  If pParent
16f41 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 20  .  ** has NB or 
16f42 66 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20 74  fewer children t
16f43 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e  hen all children
16f44 20 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65 20   of pParent are 
16f45 74 61 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 6e 78  taken..  */.  nx
16f46 44 69 76 20 3d 20 69 64 78 20 2d 20 4e 4e 3b 0a  Div = idx - NN;.
16f47 20 20 69 66 28 20 6e 78 44 69 76 20 2b 20 4e 42    if( nxDiv + NB
16f48 20 3e 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c   > pParent->nCel
16f49 6c 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 3d  l ){.    nxDiv =
16f4a 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20   pParent->nCell 
16f4b 2d 20 4e 42 20 2b 20 31 3b 0a 20 20 7d 0a 20 20  - NB + 1;.  }.  
16f4c 69 66 28 20 6e 78 44 69 76 3c 30 20 29 7b 0a 20  if( nxDiv<0 ){. 
16f4d 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20     nxDiv = 0;.  
16f4e 7d 0a 20 20 6e 44 69 76 20 3d 20 30 3b 0a 20 20  }.  nDiv = 0;.  
16f4f 66 6f 72 28 69 3d 30 2c 20 6b 3d 6e 78 44 69 76  for(i=0, k=nxDiv
16f50 3b 20 69 3c 4e 42 3b 20 69 2b 2b 2c 20 6b 2b 2b  ; i<NB; i++, k++
16f51 29 7b 0a 20 20 20 20 69 66 28 20 6b 3c 70 50 61  ){.    if( k<pPa
16f52 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  rent->nCell ){. 
16f53 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20       apDiv[i] = 
16f54 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74  findCell(pParent
16f55 2c 20 6b 29 3b 0a 20 20 20 20 20 20 6e 44 69 76  , k);.      nDiv
16f56 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ++;.      assert
16f57 28 20 21 70 50 61 72 65 6e 74 2d 3e 6c 65 61 66  ( !pParent->leaf
16f58 20 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4f 6c   );.      pgnoOl
16f59 64 5b 69 5d 20 3d 20 67 65 74 34 62 79 74 65 28  d[i] = get4byte(
16f5a 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 7d  apDiv[i]);.    }
16f5b 65 6c 73 65 20 69 66 28 20 6b 3d 3d 70 50 61 72  else if( k==pPar
16f5c 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20  ent->nCell ){.  
16f5d 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 3d      pgnoOld[i] =
16f5e 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65   get4byte(&pPare
16f5f 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e  nt->aData[pParen
16f60 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  t->hdrOffset+8])
16f61 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
16f62 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
16f63 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49      rc = getAndI
16f64 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e  nitPage(pBt, pgn
16f65 6f 4f 6c 64 5b 69 5d 2c 20 26 61 70 4f 6c 64 5b  oOld[i], &apOld[
16f66 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  i]);.    if( rc 
16f67 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63  ) goto balance_c
16f68 6c 65 61 6e 75 70 3b 0a 20 20 20 20 2f 2a 20 61  leanup;.    /* a
16f69 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72 65  pOld[i]->idxPare
16f6a 6e 74 20 3d 20 6b 3b 20 2a 2f 0a 20 20 20 20 61  nt = k; */.    a
16f6b 70 43 6f 70 79 5b 69 5d 20 3d 20 30 3b 0a 20 20  pCopy[i] = 0;.  
16f6c 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 4f 6c    assert( i==nOl
16f6d 64 20 29 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b 3b  d );.    nOld++;
16f6e 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b  .    nMaxCells +
16f6f 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43  = 1+apOld[i]->nC
16f70 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f  ell+apOld[i]->nO
16f71 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20 20  verflow;.  }..  
16f72 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c  /* Make nMaxCell
16f73 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20  s a multiple of 
16f74 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72  4 in order to pr
16f75 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20 20  eserve 8-byte.  
16f76 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a  ** alignment */.
16f77 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e    nMaxCells = (n
16f78 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e 33  MaxCells + 3)&~3
16f79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c  ;..  /*.  ** All
16f7a 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20  ocate space for 
16f7b 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
16f7c 73 0a 20 20 2a 2f 0a 20 20 73 7a 53 63 72 61 74  s.  */.  szScrat
16f7d 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78  ch =.       nMax
16f7e 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a  Cells*sizeof(u8*
16f7f 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
16f80 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c          /* apCel
16f81 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78  l */.     + nMax
16f82 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36  Cells*sizeof(u16
16f83 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
16f84 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c          /* szCel
16f85 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 28 52 4f 55  l */.     + (ROU
16f86 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61  ND8(sizeof(MemPa
16f87 67 65 29 29 2b 70 42 74 2d 3e 70 61 67 65 53 69  ge))+pBt->pageSi
16f88 7a 65 29 2a 4e 42 20 20 2f 2a 20 61 43 6f 70 79  ze)*NB  /* aCopy
16f89 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d 3e   */.     + pBt->
16f8a 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20 20  pageSize        
16f8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16f8c 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63 65         /* aSpace
16f8d 31 20 2a 2f 0a 20 20 20 20 20 2b 20 28 49 53 41  1 */.     + (ISA
16f8e 55 54 4f 56 41 43 55 55 4d 20 3f 20 6e 4d 61 78  UTOVACUUM ? nMax
16f8f 43 65 6c 6c 73 20 3a 20 30 29 3b 20 20 20 20 20  Cells : 0);     
16f90 20 20 20 20 20 20 20 20 2f 2a 20 61 46 72 6f 6d          /* aFrom
16f91 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73   */.  apCell = s
16f92 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c  qlite3ScratchMal
16f93 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20 29  loc( szScratch )
16f94 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d  ; .  if( apCell=
16f95 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
16f96 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
16f97 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
16f98 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43  eanup;.  }.  szC
16f99 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43  ell = (u16*)&apC
16f9a 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a  ell[nMaxCells];.
16f9b 20 20 61 43 6f 70 79 5b 30 5d 20 3d 20 28 75 38    aCopy[0] = (u8
16f9c 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65  *)&szCell[nMaxCe
16f9d 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  lls];.  assert( 
16f9e 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e  EIGHT_BYTE_ALIGN
16f9f 4d 45 4e 54 28 61 43 6f 70 79 5b 30 5d 29 20 29  MENT(aCopy[0]) )
16fa0 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e  ;.  for(i=1; i<N
16fa1 42 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 43 6f  B; i++){.    aCo
16fa2 70 79 5b 69 5d 20 3d 20 26 61 43 6f 70 79 5b 69  py[i] = &aCopy[i
16fa3 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a  -1][pBt->pageSiz
16fa4 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  e+ROUND8(sizeof(
16fa5 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 20 20  MemPage))];.    
16fa6 61 73 73 65 72 74 28 20 28 28 61 43 6f 70 79 5b  assert( ((aCopy[
16fa7 69 5d 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37  i] - (u8*)0) & 7
16fa8 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74  )==0 ); /* 8-byt
16fa9 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75  e alignment requ
16faa 69 72 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 53  ired */.  }.  aS
16fab 70 61 63 65 31 20 3d 20 26 61 43 6f 70 79 5b 4e  pace1 = &aCopy[N
16fac 42 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69  B-1][pBt->pageSi
16fad 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66  ze+ROUND8(sizeof
16fae 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 61  (MemPage))];.  a
16faf 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54  ssert( EIGHT_BYT
16fb0 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 61 53 70 61  E_ALIGNMENT(aSpa
16fb1 63 65 31 29 20 29 3b 0a 20 20 69 66 28 20 49 53  ce1) );.  if( IS
16fb2 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20  AUTOVACUUM ){.  
16fb3 20 20 61 46 72 6f 6d 20 3d 20 26 61 53 70 61 63    aFrom = &aSpac
16fb4 65 31 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  e1[pBt->pageSize
16fb5 5d 3b 0a 20 20 7d 0a 20 20 61 53 70 61 63 65 32  ];.  }.  aSpace2
16fb6 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61   = sqlite3PageMa
16fb7 6c 6c 6f 63 28 70 42 74 2d 3e 70 61 67 65 53 69  lloc(pBt->pageSi
16fb8 7a 65 29 3b 0a 20 20 69 66 28 20 61 53 70 61 63  ze);.  if( aSpac
16fb9 65 32 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  e2==0 ){.    rc 
16fba 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
16fbb 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65      goto balance
16fbc 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
16fbd 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20  .  /*.  ** Make 
16fbe 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 63 6f  copies of the co
16fbf 6e 74 65 6e 74 20 6f 66 20 70 50 61 67 65 20 61  ntent of pPage a
16fc0 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20  nd its siblings 
16fc1 69 6e 74 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a  into aOld[]..  *
16fc2 2a 20 54 68 65 20 72 65 73 74 20 6f 66 20 74 68  * The rest of th
16fc3 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c  is function will
16fc4 20 75 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74   use data from t
16fc5 68 65 20 63 6f 70 69 65 73 20 72 61 74 68 65 72  he copies rather
16fc6 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f  .  ** that the o
16fc7 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69  riginal pages si
16fc8 6e 63 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  nce the original
16fc9 20 70 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69   pages will be i
16fca 6e 20 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65  n the.  ** proce
16fcb 73 73 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72  ss of being over
16fcc 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20  written..  */.  
16fcd 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b  for(i=0; i<nOld;
16fce 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61   i++){.    MemPa
16fcf 67 65 20 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69  ge *p = apCopy[i
16fd0 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 61 43  ] = (MemPage*)aC
16fd1 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63  opy[i];.    memc
16fd2 70 79 28 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20  py(p, apOld[i], 
16fd3 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29  sizeof(MemPage))
16fd4 3b 0a 20 20 20 20 70 2d 3e 61 44 61 74 61 20 3d  ;.    p->aData =
16fd5 20 28 76 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20   (void*)&p[1];. 
16fd6 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61     memcpy(p->aDa
16fd7 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44  ta, apOld[i]->aD
16fd8 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69  ata, pBt->pageSi
16fd9 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  ze);.  }..  /*. 
16fda 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72   ** Load pointer
16fdb 73 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f  s to all cells o
16fdc 6e 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20  n sibling pages 
16fdd 61 6e 64 20 74 68 65 20 64 69 76 69 64 65 72 20  and the divider 
16fde 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20  cells.  ** into 
16fdf 74 68 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c  the local apCell
16fe0 5b 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20  [] array.  Make 
16fe1 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 64 69  copies of the di
16fe2 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a  vider cells.  **
16fe3 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61   into space obta
16fe4 69 6e 65 64 20 66 6f 72 6d 20 61 53 70 61 63 65  ined form aSpace
16fe5 31 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 74  1[] and remove t
16fe6 68 65 20 74 68 65 20 64 69 76 69 64 65 72 20 43  he the divider C
16fe7 65 6c 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70  ells.  ** from p
16fe8 50 61 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a  Parent..  **.  *
16fe9 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67  * If the sibling
16fea 73 20 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 61  s are on leaf pa
16feb 67 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68  ges, then the ch
16fec 69 6c 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 20  ild pointers of 
16fed 74 68 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 72  the.  ** divider
16fee 20 63 65 6c 6c 73 20 61 72 65 20 73 74 72 69 70   cells are strip
16fef 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 65 6c  ped from the cel
16ff0 6c 73 20 62 65 66 6f 72 65 20 74 68 65 79 20 61  ls before they a
16ff1 72 65 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69  re copied.  ** i
16ff2 6e 74 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20 20  nto aSpace1[].  
16ff3 49 6e 20 74 68 69 73 20 77 61 79 2c 20 61 6c 6c  In this way, all
16ff4 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c   cells in apCell
16ff5 5b 5d 20 61 72 65 20 77 69 74 68 6f 75 74 0a 20  [] are without. 
16ff6 20 2a 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 65   ** child pointe
16ff7 72 73 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 73  rs.  If siblings
16ff8 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c   are not leaves,
16ff9 20 74 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69   then all cell i
16ffa 6e 0a 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20  n.  ** apCell[] 
16ffb 69 6e 63 6c 75 64 65 20 63 68 69 6c 64 20 70 6f  include child po
16ffc 69 6e 74 65 72 73 2e 20 20 45 69 74 68 65 72 20  inters.  Either 
16ffd 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69  way, all cells i
16ffe 6e 20 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20  n apCell[].  ** 
16fff 61 72 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a  are alike..  **.
17000 20 20 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 74    ** leafCorrect
17001 69 6f 6e 3a 20 20 34 20 69 66 20 70 50 61 67 65  ion:  4 if pPage
17002 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69   is a leaf.  0 i
17003 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 61  f pPage is not a
17004 20 6c 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20   leaf..  **     
17005 20 20 6c 65 61 66 44 61 74 61 3a 20 20 31 20 69    leafData:  1 i
17006 66 20 70 50 61 67 65 20 68 6f 6c 64 73 20 6b 65  f pPage holds ke
17007 79 2b 64 61 74 61 20 61 6e 64 20 70 50 61 72 65  y+data and pPare
17008 6e 74 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65  nt holds only ke
17009 79 73 2e 0a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c  ys..  */.  nCell
1700a 20 3d 20 30 3b 0a 20 20 6c 65 61 66 43 6f 72 72   = 0;.  leafCorr
1700b 65 63 74 69 6f 6e 20 3d 20 70 50 61 67 65 2d 3e  ection = pPage->
1700c 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61  leaf*4;.  leafDa
1700d 74 61 20 3d 20 70 50 61 67 65 2d 3e 68 61 73 44  ta = pPage->hasD
1700e 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ata;.  for(i=0; 
1700f 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20  i<nOld; i++){.  
17010 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20    MemPage *pOld 
17011 3d 20 61 70 43 6f 70 79 5b 69 5d 3b 0a 20 20 20  = apCopy[i];.   
17012 20 69 6e 74 20 6c 69 6d 69 74 20 3d 20 70 4f 6c   int limit = pOl
17013 64 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e  d->nCell+pOld->n
17014 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f  Overflow;.    fo
17015 72 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20  r(j=0; j<limit; 
17016 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65  j++){.      asse
17017 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65  rt( nCell<nMaxCe
17018 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 61 70 43  lls );.      apC
17019 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e  ell[nCell] = fin
1701a 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f  dOverflowCell(pO
1701b 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a  ld, j);.      sz
1701c 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65  Cell[nCell] = ce
1701d 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20  llSizePtr(pOld, 
1701e 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a  apCell[nCell]);.
1701f 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f        if( ISAUTO
17020 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20  VACUUM ){.      
17021 20 20 69 6e 74 20 61 3b 0a 20 20 20 20 20 20 20    int a;.       
17022 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20   aFrom[nCell] = 
17023 28 75 38 29 69 3b 20 20 20 61 73 73 65 72 74 28  (u8)i;   assert(
17024 20 69 3e 3d 30 20 26 26 20 69 3c 36 20 29 3b 0a   i>=0 && i<6 );.
17025 20 20 20 20 20 20 20 20 66 6f 72 28 61 3d 30 3b          for(a=0;
17026 20 61 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c   a<pOld->nOverfl
17027 6f 77 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20  ow; a++){.      
17028 20 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f      if( pOld->aO
17029 76 66 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70  vfl[a].pCell==ap
1702a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20  Cell[nCell] ){. 
1702b 20 20 20 20 20 20 20 20 20 20 20 61 46 72 6f 6d             aFrom
1702c 5b 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a  [nCell] = 0xFF;.
1702d 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
1702e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
1702f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
17030 0a 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a  .      nCell++;.
17031 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c      }.    if( i<
17032 6e 4f 6c 64 2d 31 20 29 7b 0a 20 20 20 20 20 20  nOld-1 ){.      
17033 75 31 36 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a  u16 sz = cellSiz
17034 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70  ePtr(pParent, ap
17035 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69  Div[i]);.      i
17036 66 28 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20  f( leafData ){. 
17037 20 20 20 20 20 20 20 2f 2a 20 57 69 74 68 20 74         /* With t
17038 68 65 20 4c 45 41 46 44 41 54 41 20 66 6c 61 67  he LEAFDATA flag
17039 2c 20 70 50 61 72 65 6e 74 20 63 65 6c 6c 73 20  , pParent cells 
1703a 68 6f 6c 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59  hold only INTKEY
1703b 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a  s that.        *
1703c 2a 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73  * are duplicates
1703d 20 6f 66 20 6b 65 79 73 20 6f 6e 20 74 68 65 20   of keys on the 
1703e 63 68 69 6c 64 20 70 61 67 65 73 2e 20 20 57 65  child pages.  We
1703f 20 6e 65 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a   need to remove.
17040 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64          ** the d
17041 69 76 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f  ivider cells fro
17042 6d 20 70 50 61 72 65 6e 74 2c 20 62 75 74 20 74  m pParent, but t
17043 68 65 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c  he dividers cell
17044 73 20 61 72 65 20 6e 6f 74 0a 20 20 20 20 20 20  s are not.      
17045 20 20 2a 2a 20 61 64 64 65 64 20 74 6f 20 61 70    ** added to ap
17046 43 65 6c 6c 5b 5d 20 62 65 63 61 75 73 65 20 74  Cell[] because t
17047 68 65 79 20 61 72 65 20 64 75 70 6c 69 63 61 74  hey are duplicat
17048 65 73 20 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c  es of child cell
17049 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
1704a 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70        dropCell(p
1704b 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73  Parent, nxDiv, s
1704c 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  z);.      }else{
1704d 0a 20 20 20 20 20 20 20 20 75 38 20 2a 70 54 65  .        u8 *pTe
1704e 6d 70 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  mp;.        asse
1704f 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65  rt( nCell<nMaxCe
17050 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 73  lls );.        s
17051 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73  zCell[nCell] = s
17052 7a 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70  z;.        pTemp
17053 20 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61   = &aSpace1[iSpa
17054 63 65 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 53  ce1];.        iS
17055 70 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20  pace1 += sz;.   
17056 20 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c       assert( sz<
17057 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34  =pBt->pageSize/4
17058 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
17059 72 74 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74  rt( iSpace1<=pBt
1705a 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20  ->pageSize );.  
1705b 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65        memcpy(pTe
1705c 6d 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a  mp, apDiv[i], sz
1705d 29 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c  );.        apCel
1705e 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70  l[nCell] = pTemp
1705f 2b 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b  +leafCorrection;
17060 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 41  .        if( ISA
17061 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  UTOVACUUM ){.   
17062 20 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65         aFrom[nCe
17063 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20 20  ll] = 0xFF;.    
17064 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 72      }.        dr
17065 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  opCell(pParent, 
17066 6e 78 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20  nxDiv, sz);.    
17067 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66      assert( leaf
17068 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c  Correction==0 ||
17069 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d   leafCorrection=
1706a 3d 34 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a  =4 );.        sz
1706b 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 28  Cell[nCell] -= (
1706c 75 31 36 29 6c 65 61 66 43 6f 72 72 65 63 74 69  u16)leafCorrecti
1706d 6f 6e 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  on;.        asse
1706e 72 74 28 20 67 65 74 34 62 79 74 65 28 70 54 65  rt( get4byte(pTe
1706f 6d 70 29 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20  mp)==pgnoOld[i] 
17070 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  );.        if( !
17071 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  pOld->leaf ){.  
17072 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
17073 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d  leafCorrection==
17074 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f  0 );.          /
17075 2a 20 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e  * The right poin
17076 74 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64  ter of the child
17077 20 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d   page pOld becom
17078 65 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20  es the left.    
17079 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72        ** pointer
1707a 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20   of the divider 
1707b 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20  cell */.        
1707c 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b    memcpy(apCell[
1707d 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61  nCell], &pOld->a
1707e 44 61 74 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66  Data[pOld->hdrOf
1707f 66 73 65 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20  fset+8], 4);.   
17080 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
17081 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65        assert( le
17082 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20  afCorrection==4 
17083 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
17084 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34   szCell[nCell]<4
17085 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
17086 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20  /* Do not allow 
17087 61 6e 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65  any cells smalle
17088 72 20 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20  r than 4 bytes. 
17089 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  */.            s
1708a 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34  zCell[nCell] = 4
1708b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1708c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1708d 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d  nCell++;.      }
1708e 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1708f 0a 20 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74  .  ** Figure out
17090 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
17091 61 67 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68  ages needed to h
17092 6f 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65  old all nCell ce
17093 6c 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20  lls..  ** Store 
17094 74 68 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22  this number in "
17095 6b 22 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74  k".  Also comput
17096 65 20 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20  e szNew[] which 
17097 69 73 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a  is the total.  *
17098 2a 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65  * size of all ce
17099 6c 6c 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20  lls on the i-th 
1709a 70 61 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b  page and cntNew[
1709b 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69  ] which is the i
1709c 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43  ndex.  ** in apC
1709d 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c  ell[] of the cel
1709e 6c 20 74 68 61 74 20 64 69 76 69 64 65 73 20 70  l that divides p
1709f 61 67 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20  age i from page 
170a0 69 2b 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e  i+1.  .  ** cntN
170a1 65 77 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75  ew[k] should equ
170a2 61 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20  al nCell..  **. 
170a3 20 2a 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75   ** Values compu
170a4 74 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63  ted by this bloc
170a5 6b 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  k:.  **.  **    
170a6 20 20 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f         k: The to
170a7 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69  tal number of si
170a8 62 6c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a  bling pages.  **
170a9 20 20 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70      szNew[i]: Sp
170aa 61 63 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65  aced used on the
170ab 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61   i-th sibling pa
170ac 67 65 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65  ge..  **   cntNe
170ad 77 5b 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61  w[i]: Index in a
170ae 70 43 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65  pCell[] and szCe
170af 6c 6c 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72  ll[] for the fir
170b0 73 74 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20  st cell to.  ** 
170b1 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
170b2 20 72 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d   right of the i-
170b3 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e  th sibling page.
170b4 0a 20 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63  .  ** usableSpac
170b5 65 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  e: Number of byt
170b6 65 73 20 6f 66 20 73 70 61 63 65 20 61 76 61 69  es of space avai
170b7 6c 61 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69  lable on each si
170b8 62 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a  bling..  ** .  *
170b9 2f 0a 20 20 75 73 61 62 6c 65 53 70 61 63 65 20  /.  usableSpace 
170ba 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
170bb 65 20 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72  e - 12 + leafCor
170bc 72 65 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73  rection;.  for(s
170bd 75 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69  ubtotal=k=i=0; i
170be 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
170bf 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78    assert( i<nMax
170c0 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62  Cells );.    sub
170c1 74 6f 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b  total += szCell[
170c2 69 5d 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20  i] + 2;.    if( 
170c3 73 75 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c  subtotal > usabl
170c4 65 53 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eSpace ){.      
170c5 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f  szNew[k] = subto
170c6 74 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b  tal - szCell[i];
170c7 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d  .      cntNew[k]
170c8 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20   = i;.      if( 
170c9 6c 65 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b  leafData ){ i--;
170ca 20 7d 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61   }.      subtota
170cb 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b  l = 0;.      k++
170cc 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a  ;.    }.  }.  sz
170cd 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61  New[k] = subtota
170ce 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d  l;.  cntNew[k] =
170cf 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a   nCell;.  k++;..
170d0 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61    /*.  ** The pa
170d1 63 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62  cking computed b
170d2 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62  y the previous b
170d3 6c 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20 74  lock is biased t
170d4 6f 77 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e  oward the siblin
170d5 67 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c  gs.  ** on the l
170d6 65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20 6c  eft side.  The l
170d7 65 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72 65  eft siblings are
170d8 20 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 66   always nearly f
170d9 75 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a 20  ull, while the. 
170da 20 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 73   ** right-most s
170db 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20  ibling might be 
170dc 6e 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 54  nearly empty.  T
170dd 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  his block of cod
170de 65 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20  e attempts.  ** 
170df 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70 61  to adjust the pa
170e0 63 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67  cking of sibling
170e1 73 20 74 6f 20 67 65 74 20 61 20 62 65 74 74 65  s to get a bette
170e2 72 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a  r balance..  **.
170e3 20 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 74    ** This adjust
170e4 6d 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 61  ment is more tha
170e5 6e 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  n an optimizatio
170e6 6e 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67 20  n.  The packing 
170e7 61 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a  above might.  **
170e8 20 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 61   be so out of ba
170e9 6c 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20 69  lance as to be i
170ea 6c 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 61  llegal.  For exa
170eb 6d 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74 2d  mple, the right-
170ec 6d 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e  most.  ** siblin
170ed 67 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 6c  g might be compl
170ee 65 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 68  etely empty.  Th
170ef 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73  is adjustment is
170f0 20 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20   not optional.. 
170f1 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b   */.  for(i=k-1;
170f2 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20   i>0; i--){.    
170f3 69 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73 7a  int szRight = sz
170f4 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65  New[i];  /* Size
170f5 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74   of sibling on t
170f6 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20  he right */.    
170f7 69 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e  int szLeft = szN
170f8 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65  ew[i-1]; /* Size
170f9 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74   of sibling on t
170fa 68 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69  he left */.    i
170fb 6e 74 20 72 3b 20 20 20 20 20 20 20 20 20 20 20  nt r;           
170fc 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72     /* Index of r
170fd 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69  ight-most cell i
170fe 6e 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 2a  n left sibling *
170ff 2f 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20 20  /.    int d;    
17100 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
17101 65 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c  ex of first cell
17102 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20   to the left of 
17103 72 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f  right sibling */
17104 0a 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77  ..    r = cntNew
17105 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64  [i-1] - 1;.    d
17106 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44   = r + 1 - leafD
17107 61 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ata;.    assert(
17108 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a   d<nMaxCells );.
17109 20 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e 4d      assert( r<nM
1710a 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77  axCells );.    w
1710b 68 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d 30  hile( szRight==0
1710c 20 7c 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43 65   || szRight+szCe
1710d 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d  ll[d]+2<=szLeft-
1710e 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b  (szCell[r]+2) ){
1710f 0a 20 20 20 20 20 20 73 7a 52 69 67 68 74 20 2b  .      szRight +
17110 3d 20 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b  = szCell[d] + 2;
17111 0a 20 20 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d  .      szLeft -=
17112 20 73 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a   szCell[r] + 2;.
17113 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31        cntNew[i-1
17114 5d 2d 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20 63  ]--;.      r = c
17115 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a  ntNew[i-1] - 1;.
17116 20 20 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20        d = r + 1 
17117 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20  - leafData;.    
17118 7d 0a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d  }.    szNew[i] =
17119 20 73 7a 52 69 67 68 74 3b 0a 20 20 20 20 73 7a   szRight;.    sz
1711a 4e 65 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66  New[i-1] = szLef
1711b 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74  t;.  }..  /* Eit
1711c 68 65 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65  her we found one
1711d 20 6f 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28   or more cells (
1711e 63 6e 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72  cntnew[0])>0) or
1711f 20 77 65 20 61 72 65 20 74 68 65 0a 20 20 2a 2a   we are the.  **
17120 20 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20   a virtual root 
17121 70 61 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c  page.  A virtual
17122 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68   root page is wh
17123 65 6e 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74  en the real root
17124 0a 20 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61  .  ** page is pa
17125 67 65 20 31 20 61 6e 64 20 77 65 20 61 72 65 20  ge 1 and we are 
17126 74 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f  the only child o
17127 66 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a  f that page..  *
17128 2f 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e  /.  assert( cntN
17129 65 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72  ew[0]>0 || (pPar
1712a 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20  ent->pgno==1 && 
1712b 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d  pParent->nCell==
1712c 30 29 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  0) );..  /*.  **
1712d 20 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20   Allocate k new 
1712e 70 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c  pages.  Reuse ol
1712f 64 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f  d pages where po
17130 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61  ssible..  */.  a
17131 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67  ssert( pPage->pg
17132 6e 6f 3e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c  no>1 );.  pageFl
17133 61 67 73 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ags = pPage->aDa
17134 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30  ta[0];.  for(i=0
17135 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20  ; i<k; i++){.   
17136 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a   MemPage *pNew;.
17137 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29      if( i<nOld )
17138 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61  {.      pNew = a
17139 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b  pNew[i] = apOld[
1713a 69 5d 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65  i];.      pgnoNe
1713b 77 5b 69 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69  w[i] = pgnoOld[i
1713c 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69  ];.      apOld[i
1713d 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  ] = 0;.      rc 
1713e 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1713f 69 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67  ite(pNew->pDbPag
17140 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b  e);.      nNew++
17141 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
17142 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
17143 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65  eanup;.    }else
17144 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
17145 69 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20  i>0 );.      rc 
17146 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  = allocateBtreeP
17147 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20  age(pBt, &pNew, 
17148 26 70 67 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e  &pgnoNew[i], pgn
17149 6f 4e 65 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20  oNew[i-1], 0);. 
1714a 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f       if( rc ) go
1714b 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
1714c 75 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b  up;.      apNew[
1714d 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20  i] = pNew;.     
1714e 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20   nNew++;.    }. 
1714f 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e   }..  /* Free an
17150 79 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74  y old pages that
17151 20 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64   were not reused
17152 20 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20   as new pages.. 
17153 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e   */.  while( i<n
17154 4f 6c 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  Old ){.    rc = 
17155 66 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69  freePage(apOld[i
17156 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  ]);.    if( rc )
17157 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
17158 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61  eanup;.    relea
17159 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29  sePage(apOld[i])
1715a 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d  ;.    apOld[i] =
1715b 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d   0;.    i++;.  }
1715c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20  ..  /*.  ** Put 
1715d 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e  the new pages in
1715e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   accending order
1715f 2e 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f  .  This helps to
17160 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69  .  ** keep entri
17161 65 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66  es in the disk f
17162 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20  ile in order so 
17163 74 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a  that a scan.  **
17164 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   of the table is
17165 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74   a linear scan t
17166 68 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e  hrough the file.
17167 20 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74    That.  ** in t
17168 75 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70  urn helps the op
17169 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74  erating system t
1716a 6f 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a  o deliver pages.
1716b 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69    ** from the di
1716c 73 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e  sk more rapidly.
1716d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28  .  **.  ** An O(
1716e 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73  n^2) insertion s
1716f 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73  ort algorithm is
17170 20 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65   used, but since
17171 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72  .  ** n is never
17172 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61   more than NB (a
17173 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29   small constant)
17174 2c 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20  , that should.  
17175 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62  ** not be a prob
17176 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  lem..  **.  ** W
17177 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20  hen NB==3, this 
17178 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  one optimization
17179 20 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62   makes the datab
1717a 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32  ase.  ** about 2
1717b 35 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61  5% faster for la
1717c 72 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61  rge insertions a
1717d 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20  nd deletions..  
1717e 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
1717f 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  k-1; i++){.    i
17180 6e 74 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65  nt minV = pgnoNe
17181 77 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69  w[i];.    int mi
17182 6e 49 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28  nI = i;.    for(
17183 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29  j=i+1; j<k; j++)
17184 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f  {.      if( pgno
17185 4e 65 77 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64  New[j]<(unsigned
17186 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20  )minV ){.       
17187 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20   minI = j;.     
17188 20 20 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65     minV = pgnoNe
17189 77 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  w[j];.      }.  
1718a 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49    }.    if( minI
1718b 3e 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  >i ){.      int 
1718c 74 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65  t;.      MemPage
1718d 20 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20   *pT;.      t = 
1718e 70 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20  pgnoNew[i];.    
1718f 20 20 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b    pT = apNew[i];
17190 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69  .      pgnoNew[i
17191 5d 20 3d 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49  ] = pgnoNew[minI
17192 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69  ];.      apNew[i
17193 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b  ] = apNew[minI];
17194 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d  .      pgnoNew[m
17195 69 6e 49 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20  inI] = t;.      
17196 61 70 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54  apNew[minI] = pT
17197 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52  ;.    }.  }.  TR
17198 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f  ACE(("BALANCE: o
17199 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20 6e 65  ld: %d %d %d  ne
1719a 77 3a 20 25 64 28 25 64 29 20 25 64 28 25 64 29  w: %d(%d) %d(%d)
1719b 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25   %d(%d) %d(%d) %
1719c 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67  d(%d)\n",.    pg
1719d 6e 6f 4f 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e  noOld[0], .    n
1719e 4f 6c 64 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64  Old>=2 ? pgnoOld
1719f 5b 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c  [1] : 0,.    nOl
171a0 64 3e 3d 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32  d>=3 ? pgnoOld[2
171a1 5d 20 3a 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e  ] : 0,.    pgnoN
171a2 65 77 5b 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c  ew[0], szNew[0],
171a3 0a 20 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70  .    nNew>=2 ? p
171a4 67 6e 6f 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e  gnoNew[1] : 0, n
171a5 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31  New>=2 ? szNew[1
171a6 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e  ] : 0,.    nNew>
171a7 3d 33 20 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20  =3 ? pgnoNew[2] 
171a8 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73  : 0, nNew>=3 ? s
171a9 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20  zNew[2] : 0,.   
171aa 20 6e 4e 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e   nNew>=4 ? pgnoN
171ab 65 77 5b 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e  ew[3] : 0, nNew>
171ac 3d 34 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20  =4 ? szNew[3] : 
171ad 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f  0,.    nNew>=5 ?
171ae 20 70 67 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c   pgnoNew[4] : 0,
171af 20 6e 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77   nNew>=5 ? szNew
171b0 5b 34 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a  [4] : 0));..  /*
171b1 0a 20 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73  .  ** Evenly dis
171b2 74 72 69 62 75 74 65 20 74 68 65 20 64 61 74 61  tribute the data
171b3 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72   in apCell[] acr
171b4 6f 73 73 20 74 68 65 20 6e 65 77 20 70 61 67 65  oss the new page
171b5 73 2e 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64  s..  ** Insert d
171b6 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74  ivider cells int
171b7 6f 20 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63  o pParent as nec
171b8 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a  essary..  */.  j
171b9 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b   = 0;.  for(i=0;
171ba 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20   i<nNew; i++){. 
171bb 20 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74     /* Assemble t
171bc 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70  he new sibling p
171bd 61 67 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50  age. */.    MemP
171be 61 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65  age *pNew = apNe
171bf 77 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74  w[i];.    assert
171c0 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( j<nMaxCells );
171c1 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65  .    assert( pNe
171c2 77 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77  w->pgno==pgnoNew
171c3 5b 69 5d 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50  [i] );.    zeroP
171c4 61 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c  age(pNew, pageFl
171c5 61 67 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62  ags);.    assemb
171c6 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74  lePage(pNew, cnt
171c7 4e 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c  New[i]-j, &apCel
171c8 6c 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d  l[j], &szCell[j]
171c9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
171ca 4e 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20  New->nCell>0 || 
171cb 28 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e  (nNew==1 && cntN
171cc 65 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20  ew[0]==0) );.   
171cd 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e   assert( pNew->n
171ce 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a  Overflow==0 );..
171cf 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
171d0 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  s an auto-vacuum
171d1 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74   database, updat
171d2 65 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  e the pointer ma
171d3 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a  p entries.    **
171d4 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74   that point to t
171d5 68 65 20 73 69 62 6c 69 6e 67 73 20 74 68 61 74  he siblings that
171d6 20 77 65 72 65 20 72 65 61 72 72 61 6e 67 65 64   were rearranged
171d7 2e 20 54 68 65 73 65 20 63 61 6e 20 62 65 3a 20  . These can be: 
171d8 6c 65 66 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c  left.    ** chil
171d9 64 72 65 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74  dren of cells, t
171da 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f  he right-child o
171db 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 6f  f the page, or o
171dc 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20  verflow pages.  
171dd 20 20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20    ** pointed to 
171de 62 79 20 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f  by cells..    */
171df 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56  .    if( ISAUTOV
171e0 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 66  ACUUM ){.      f
171e1 6f 72 28 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77  or(k=j; k<cntNew
171e2 5b 69 5d 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20  [i]; k++){.     
171e3 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61     assert( k<nMa
171e4 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20  xCells );.      
171e5 20 20 69 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d    if( aFrom[k]==
171e6 30 78 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61  0xFF || apCopy[a
171e7 46 72 6f 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d  From[k]]->pgno!=
171e8 70 4e 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20  pNew->pgno ){.  
171e9 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72          rc = ptr
171ea 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c  mapPutOvfl(pNew,
171eb 20 6b 2d 6a 29 3b 0a 20 20 20 20 20 20 20 20 20   k-j);.         
171ec 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
171ed 4f 4b 20 26 26 20 6c 65 61 66 43 6f 72 72 65 63  OK && leafCorrec
171ee 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  tion==0 ){.     
171ef 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d         rc = ptrm
171f0 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62  apPut(pBt, get4b
171f1 79 74 65 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20  yte(apCell[k]), 
171f2 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e  PTRMAP_BTREE, pN
171f3 65 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  ew->pgno);.     
171f4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
171f5 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
171f6 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
171f7 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63    goto balance_c
171f8 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20  leanup;.        
171f9 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
171fa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
171fb 20 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a   j = cntNew[i];.
171fc 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73  .    /* If the s
171fd 69 62 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65  ibling page asse
171fe 6d 62 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20  mbled above was 
171ff 6e 6f 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f  not the right-mo
17200 73 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20  st sibling,.    
17201 2a 2a 20 69 6e 73 65 72 74 20 61 20 64 69 76 69  ** insert a divi
17202 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68  der cell into th
17203 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20  e parent page.. 
17204 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c     */.    if( i<
17205 6e 4e 65 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c  nNew-1 && j<nCel
17206 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70  l ){.      u8 *p
17207 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a  Cell;.      u8 *
17208 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74  pTemp;.      int
17209 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65   sz;..      asse
1720a 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20  rt( j<nMaxCells 
1720b 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d  );.      pCell =
1720c 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20   apCell[j];.    
1720d 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d    sz = szCell[j]
1720e 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f   + leafCorrectio
1720f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d  n;.      pTemp =
17210 20 26 61 53 70 61 63 65 32 5b 69 53 70 61 63 65   &aSpace2[iSpace
17211 32 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  2];.      if( !p
17212 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  New->leaf ){.   
17213 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65       memcpy(&pNe
17214 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65  w->aData[8], pCe
17215 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20  ll, 4);.        
17216 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d  if( ISAUTOVACUUM
17217 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 61   .         && (a
17218 46 72 6f 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c  From[j]==0xFF ||
17219 20 61 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d   apCopy[aFrom[j]
1721a 5d 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70  ]->pgno!=pNew->p
1721b 67 6e 6f 29 0a 20 20 20 20 20 20 20 20 29 7b 0a  gno).        ){.
1721c 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70            rc = p
1721d 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65  trmapPut(pBt, ge
1721e 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 50  t4byte(pCell), P
1721f 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65  TRMAP_BTREE, pNe
17220 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  w->pgno);.      
17221 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
17222 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
17223 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63       goto balanc
17224 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20  e_cleanup;.     
17225 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
17226 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
17227 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20   leafData ){.   
17228 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74       /* If the t
17229 72 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61  ree is a leaf-da
1722a 74 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65  ta tree, and the
1722b 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65   siblings are le
1722c 61 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a  aves, .        *
1722d 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  * then there is 
1722e 6e 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  no divider cell 
1722f 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73  in apCell[]. Ins
17230 74 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65  tead, the divide
17231 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65  r .        ** ce
17232 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74  ll consists of t
17233 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66  he integer key f
17234 6f 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  or the right-mos
17235 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20  t cell of .     
17236 20 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e     ** the siblin
17237 67 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64  g-page assembled
17238 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20   above only..   
17239 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1723a 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
1723b 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20         j--;.    
1723c 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1723d 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65  ParseCellPtr(pNe
1723e 77 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69  w, apCell[j], &i
1723f 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43  nfo);.        pC
17240 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20  ell = pTemp;.   
17241 20 20 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e       rc = fillIn
17242 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 43  Cell(pParent, pC
17243 65 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65  ell, 0, info.nKe
17244 79 2c 20 30 2c 20 30 2c 20 30 2c 20 26 73 7a 29  y, 0, 0, 0, &sz)
17245 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
17246 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
17247 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61           goto ba
17248 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
17249 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1724a 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20   pTemp = 0;.    
1724b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1724c 20 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20   pCell -= 4;.   
1724d 20 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20       /* Obscure 
1724e 63 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61  case for non-lea
1724f 66 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49 66  f-data trees: If
17250 20 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65   the cell at pCe
17251 6c 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a  ll was.        *
17252 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f  * previously sto
17253 72 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f  red on a leaf no
17254 64 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f  de, and its repo
17255 72 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a  rted size was 4.
17256 20 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73          ** bytes
17257 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63  , then it may ac
17258 74 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65  tually be smalle
17259 72 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20  r than this .   
1725a 20 20 20 20 20 2a 2a 20 28 73 65 65 20 73 71 6c       ** (see sql
1725b 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
1725c 6c 6c 50 74 72 28 29 2c 20 34 20 62 79 74 65 73  llPtr(), 4 bytes
1725d 20 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20   is the minimum 
1725e 73 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 20  size of.        
1725f 2a 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 75  ** any cell). Bu
17260 74 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  t it is importan
17261 74 20 74 6f 20 70 61 73 73 20 74 68 65 20 63 6f  t to pass the co
17262 72 72 65 63 74 20 73 69 7a 65 20 74 6f 20 0a 20  rrect size to . 
17263 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 74         ** insert
17264 43 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 72  Cell(), so repar
17265 73 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 2e  se the cell now.
17266 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
17267 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74      ** Note that
17268 20 74 68 69 73 20 63 61 6e 20 6e 65 76 65 72 20   this can never 
17269 68 61 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 4c  happen in an SQL
1726a 69 74 65 20 64 61 74 61 20 66 69 6c 65 2c 20 61  ite data file, a
1726b 73 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a  s all.        **
1726c 20 63 65 6c 6c 73 20 61 72 65 20 61 74 20 6c 65   cells are at le
1726d 61 73 74 20 34 20 62 79 74 65 73 2e 20 49 74 20  ast 4 bytes. It 
1726e 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 6e 20  only happens in 
1726f 62 2d 74 72 65 65 73 20 75 73 65 64 0a 20 20 20  b-trees used.   
17270 20 20 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c 75       ** to evalu
17271 61 74 65 20 22 49 4e 20 28 53 45 4c 45 43 54 20  ate "IN (SELECT 
17272 2e 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c 61  ...)" and simila
17273 72 20 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 20  r clauses..     
17274 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66     */.        if
17275 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29  ( szCell[j]==4 )
17276 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
17277 72 74 28 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  rt(leafCorrectio
17278 6e 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 20  n==4);.         
17279 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74   sz = cellSizePt
1727a 72 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c  r(pParent, pCell
1727b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1727c 20 20 20 7d 0a 20 20 20 20 20 20 69 53 70 61 63     }.      iSpac
1727d 65 32 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20  e2 += sz;.      
1727e 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d  assert( sz<=pBt-
1727f 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20  >pageSize/4 );. 
17280 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53 70       assert( iSp
17281 61 63 65 32 3c 3d 70 42 74 2d 3e 70 61 67 65 53  ace2<=pBt->pageS
17282 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20  ize );.      rc 
17283 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61  = insertCell(pPa
17284 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 65  rent, nxDiv, pCe
17285 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 34  ll, sz, pTemp, 4
17286 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
17287 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
17288 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
17289 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  p;.      assert(
1728a 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
1728b 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74  riteable(pParent
1728c 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
1728d 20 20 20 20 70 75 74 34 62 79 74 65 28 66 69 6e      put4byte(fin
1728e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50  dOverflowCell(pP
1728f 61 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 4e  arent,nxDiv), pN
17290 65 77 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20  ew->pgno);..    
17291 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
17292 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64  an auto-vacuum d
17293 61 74 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74  atabase, and not
17294 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65   a leaf-data tre
17295 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  e,.      ** then
17296 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e   update the poin
17297 74 65 72 20 6d 61 70 20 77 69 74 68 20 61 6e 20  ter map with an 
17298 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76  entry for the ov
17299 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20  erflow page.    
1729a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 65    ** that the ce
1729b 6c 6c 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64  ll just inserted
1729c 20 70 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 61   points to (if a
1729d 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ny)..      */.  
1729e 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
1729f 43 55 55 4d 20 26 26 20 21 6c 65 61 66 44 61 74  CUUM && !leafDat
172a0 61 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  a ){.        rc 
172a1 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28  = ptrmapPutOvfl(
172a2 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 3b  pParent, nxDiv);
172a3 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
172a4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
172a5 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c          goto bal
172a6 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
172a7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
172a8 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20        j++;.     
172a9 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a   nxDiv++;.    }.
172aa 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20  .    /* Set the 
172ab 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
172ac 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69  y for the new si
172ad 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20  bling page. */. 
172ae 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
172af 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20  UUM ){.      rc 
172b0 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c  = ptrmapPut(pBt,
172b1 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52   pNew->pgno, PTR
172b2 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65  MAP_BTREE, pPare
172b3 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  nt->pgno);.     
172b4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
172b5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
172b6 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
172b7 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  up;.      }.    
172b8 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
172b9 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73  j==nCell );.  as
172ba 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a  sert( nOld>0 );.
172bb 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30    assert( nNew>0
172bc 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46   );.  if( (pageF
172bd 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29  lags & PTF_LEAF)
172be 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a  ==0 ){.    u8 *z
172bf 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b  Child = &apCopy[
172c0 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38  nOld-1]->aData[8
172c1 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61  ];.    memcpy(&a
172c2 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44  pNew[nNew-1]->aD
172c3 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20  ata[8], zChild, 
172c4 34 29 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55  4);.    if( ISAU
172c5 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
172c6 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
172c7 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 7a  (pBt, get4byte(z
172c8 43 68 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f 42  Child), PTRMAP_B
172c9 54 52 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65 77  TREE, apNew[nNew
172ca 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20  -1]->pgno);.    
172cb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
172cc 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
172cd 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61  oto balance_clea
172ce 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nup;.      }.   
172cf 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
172d0 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
172d1 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74  riteable(pParent
172d2 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
172d3 69 66 28 20 6e 78 44 69 76 3d 3d 70 50 61 72 65  if( nxDiv==pPare
172d4 6e 74 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 65 6e  nt->nCell+pParen
172d5 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a  t->nOverflow ){.
172d6 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73      /* Right-mos
172d7 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65  t sibling is the
172d8 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c   right-most chil
172d9 64 20 6f 66 20 70 50 61 72 65 6e 74 20 2a 2f 0a  d of pParent */.
172da 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50      put4byte(&pP
172db 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61  arent->aData[pPa
172dc 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b  rent->hdrOffset+
172dd 38 5d 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77  8], pgnoNew[nNew
172de 2d 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  -1]);.  }else{. 
172df 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74     /* Right-most
172e0 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20   sibling is the 
172e1 6c 65 66 74 20 63 68 69 6c 64 20 6f 66 20 74 68  left child of th
172e2 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e  e first entry in
172e3 20 70 50 61 72 65 6e 74 0a 20 20 20 20 2a 2a 20   pParent.    ** 
172e4 70 61 73 74 20 74 68 65 20 72 69 67 68 74 2d 6d  past the right-m
172e5 6f 73 74 20 64 69 76 69 64 65 72 20 65 6e 74 72  ost divider entr
172e6 79 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79 74  y */.    put4byt
172e7 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65  e(findOverflowCe
172e8 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69  ll(pParent, nxDi
172e9 76 29 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77  v), pgnoNew[nNew
172ea 2d 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a  -1]);.  }..  /*.
172eb 20 20 2a 2a 20 42 61 6c 61 6e 63 65 20 74 68 65    ** Balance the
172ec 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20 4e   parent page.  N
172ed 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 75 72  ote that the cur
172ee 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 67 65  rent page (pPage
172ef 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 68 61 76  ) might.  ** hav
172f0 65 20 62 65 65 6e 20 61 64 64 65 64 20 74 6f 20  e been added to 
172f1 74 68 65 20 66 72 65 65 6c 69 73 74 20 73 6f 20  the freelist so 
172f2 69 74 20 6d 69 67 68 74 20 6e 6f 20 6c 6f 6e 67  it might no long
172f3 65 72 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  er be initialize
172f4 64 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20  d..  ** But the 
172f5 70 61 72 65 6e 74 20 70 61 67 65 20 77 69 6c 6c  parent page will
172f6 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74 69   always be initi
172f7 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 61  alized..  */.  a
172f8 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e  ssert( pParent->
172f9 69 73 49 6e 69 74 20 29 3b 0a 20 20 73 71 6c 69  isInit );.  sqli
172fa 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 61  te3ScratchFree(a
172fb 70 43 65 6c 6c 29 3b 0a 20 20 61 70 43 65 6c 6c  pCell);.  apCell
172fc 20 3d 20 30 3b 0a 20 20 54 52 41 43 45 28 28 22   = 0;.  TRACE(("
172fd 42 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65  BALANCE: finishe
172fe 64 20 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d 25  d with %d: old=%
172ff 64 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25  d new=%d cells=%
17300 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
17301 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f 6c  pPage->pgno, nOl
17302 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29  d, nNew, nCell))
17303 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  ;.  pPage->nOver
17304 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 72 65 6c 65  flow = 0;.  rele
17305 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
17306 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b    pCur->iPage--;
17307 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28  .  rc = balance(
17308 70 43 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20 2f  pCur, 0);.  .  /
17309 2a 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62  *.  ** Cleanup b
1730a 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
1730b 0a 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c  .  */.balance_cl
1730c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33  eanup:.  sqlite3
1730d 50 61 67 65 46 72 65 65 28 61 53 70 61 63 65 32  PageFree(aSpace2
1730e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61  );.  sqlite3Scra
1730f 74 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b  tchFree(apCell);
17310 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f  .  for(i=0; i<nO
17311 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65  ld; i++){.    re
17312 6c 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b  leasePage(apOld[
17313 69 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  i]);.  }.  for(i
17314 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29  =0; i<nNew; i++)
17315 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67  {.    releasePag
17316 65 28 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d  e(apNew[i]);.  }
17317 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  .  pCur->apPage[
17318 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f  pCur->iPage]->nO
17319 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20  verflow = 0;..  
1731a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1731b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1731c 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 74   is called for t
1731d 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
1731e 61 20 62 74 72 65 65 20 77 68 65 6e 20 74 68 65  a btree when the
1731f 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 63 6f   root.** page co
17320 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e  ntains no cells.
17321 20 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 70    This is an opp
17322 6f 72 74 75 6e 69 74 79 20 74 6f 20 6d 61 6b 65  ortunity to make
17323 20 74 68 65 20 74 72 65 65 0a 2a 2a 20 73 68 61   the tree.** sha
17324 6c 6c 6f 77 65 72 20 62 79 20 6f 6e 65 20 6c 65  llower by one le
17325 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  vel..*/.static i
17326 6e 74 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c  nt balance_shall
17327 6f 77 65 72 28 42 74 43 75 72 73 6f 72 20 2a 70  ower(BtCursor *p
17328 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  Cur){.  MemPage 
17329 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  *pPage;         
1732a 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
1732b 65 20 6f 66 20 42 2d 54 72 65 65 20 2a 2f 0a 20  e of B-Tree */. 
1732c 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64   MemPage *pChild
1732d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1732e 20 54 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20   The only child 
1732f 70 61 67 65 20 6f 66 20 70 50 61 67 65 20 2a 2f  page of pPage */
17330 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c  .  Pgno pgnoChil
17331 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
17332 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66  /* Page number f
17333 6f 72 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 69  or pChild */.  i
17334 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
17335 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  K;          /* R
17336 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
17337 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f  subprocedures */
17338 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
17339 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1733a 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 42     /* The main B
1733b 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  Tree structure *
1733c 2f 0a 20 20 69 6e 74 20 6d 78 43 65 6c 6c 50 65  /.  int mxCellPe
1733d 72 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  rPage;          
1733e 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62   /* Maximum numb
1733f 65 72 20 6f 66 20 63 65 6c 6c 73 20 70 65 72 20  er of cells per 
17340 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61  page */.  u8 **a
17341 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  pCell;          
17342 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65         /* All ce
17343 6c 6c 73 20 66 72 6f 6d 20 70 61 67 65 73 20 62  lls from pages b
17344 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f  eing balanced */
17345 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20  .  u16 *szCell; 
17346 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17347 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66  /* Local size of
17348 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a 20   all cells */.. 
17349 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
1734a 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61  Page==0 );.  pPa
1734b 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
1734c 65 5b 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28  e[0];..  assert(
1734d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30   pPage->nCell==0
1734e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
1734f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
17350 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
17351 65 78 29 20 29 3b 0a 20 20 70 42 74 20 3d 20 70  ex) );.  pBt = p
17352 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 6d 78 43  Page->pBt;.  mxC
17353 65 6c 6c 50 65 72 50 61 67 65 20 3d 20 4d 58 5f  ellPerPage = MX_
17354 43 45 4c 4c 28 70 42 74 29 3b 0a 20 20 61 70 43  CELL(pBt);.  apC
17355 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  ell = sqlite3Mal
17356 6c 6f 63 28 20 6d 78 43 65 6c 6c 50 65 72 50 61  loc( mxCellPerPa
17357 67 65 2a 28 73 69 7a 65 6f 66 28 75 38 2a 29 2b  ge*(sizeof(u8*)+
17358 73 69 7a 65 6f 66 28 75 31 36 29 29 20 29 3b 0a  sizeof(u16)) );.
17359 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20    if( apCell==0 
1735a 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1735b 4e 4f 4d 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c 20  NOMEM;.  szCell 
1735c 3d 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b  = (u16*)&apCell[
1735d 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 5d 3b 0a  mxCellPerPage];.
1735e 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61    if( pPage->lea
1735f 66 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  f ){.    /* The 
17360 74 61 62 6c 65 20 69 73 20 63 6f 6d 70 6c 65 74  table is complet
17361 65 6c 79 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20  ely empty */.   
17362 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45   TRACE(("BALANCE
17363 3a 20 65 6d 70 74 79 20 74 61 62 6c 65 20 25 64  : empty table %d
17364 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  \n", pPage->pgno
17365 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ));.  }else{.   
17366 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67   /* The root pag
17367 65 20 69 73 20 65 6d 70 74 79 20 62 75 74 20 68  e is empty but h
17368 61 73 20 6f 6e 65 20 63 68 69 6c 64 2e 20 20 54  as one child.  T
17369 72 61 6e 73 66 65 72 20 74 68 65 0a 20 20 20 20  ransfer the.    
1736a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ** information f
1736b 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 63 68 69  rom that one chi
1736c 6c 64 20 69 6e 74 6f 20 74 68 65 20 72 6f 6f 74  ld into the root
1736d 20 70 61 67 65 20 69 66 20 69 74 20 0a 20 20 20   page if it .   
1736e 20 2a 2a 20 77 69 6c 6c 20 66 69 74 2e 20 20 54   ** will fit.  T
1736f 68 69 73 20 72 65 64 75 63 65 73 20 74 68 65 20  his reduces the 
17370 64 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65  depth of the tre
17371 65 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a  e by one..    **
17372 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 72  .    ** If the r
17373 6f 6f 74 20 70 61 67 65 20 69 73 20 70 61 67 65  oot page is page
17374 20 31 2c 20 69 74 20 68 61 73 20 6c 65 73 73 20   1, it has less 
17375 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20  space available 
17376 74 68 61 6e 0a 20 20 20 20 2a 2a 20 69 74 73 20  than.    ** its 
17377 63 68 69 6c 64 20 28 64 75 65 20 74 6f 20 74 68  child (due to th
17378 65 20 31 30 30 20 62 79 74 65 20 68 65 61 64 65  e 100 byte heade
17379 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 74  r that occurs at
1737a 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20   the beginning. 
1737b 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 61 74     ** of the dat
1737c 61 62 61 73 65 20 66 6c 65 29 2c 20 73 6f 20 69  abase fle), so i
1737d 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61  t might not be a
1737e 62 6c 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20  ble to hold all 
1737f 6f 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 69  of the .    ** i
17380 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 75 72 72 65  nformation curre
17381 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 69  ntly contained i
17382 6e 20 74 68 65 20 63 68 69 6c 64 2e 20 20 49 66  n the child.  If
17383 20 74 68 69 73 20 69 73 20 74 68 65 20 0a 20 20   this is the .  
17384 20 20 2a 2a 20 63 61 73 65 2c 20 74 68 65 6e 20    ** case, then 
17385 64 6f 20 6e 6f 74 20 64 6f 20 74 68 65 20 74 72  do not do the tr
17386 61 6e 73 66 65 72 2e 20 20 4c 65 61 76 65 20 70  ansfer.  Leave p
17387 61 67 65 20 31 20 65 6d 70 74 79 20 65 78 63 65  age 1 empty exce
17388 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 68  pt.    ** for th
17389 65 20 72 69 67 68 74 2d 70 6f 69 6e 74 65 72 20  e right-pointer 
1738a 74 6f 20 74 68 65 20 63 68 69 6c 64 20 70 61 67  to the child pag
1738b 65 2e 20 20 54 68 65 20 63 68 69 6c 64 20 70 61  e.  The child pa
1738c 67 65 20 62 65 63 6f 6d 65 73 0a 20 20 20 20 2a  ge becomes.    *
1738d 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f  * the virtual ro
1738e 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a  ot of the tree..
1738f 20 20 20 20 2a 2f 0a 20 20 20 20 56 56 41 5f 4f      */.    VVA_O
17390 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73  NLY( pCur->pages
17391 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a  Shuffled = 1 );.
17392 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20      pgnoChild = 
17393 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
17394 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
17395 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20  rOffset+8]);.   
17396 20 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69   assert( pgnoChi
17397 6c 64 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  ld>0 );.    asse
17398 72 74 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d 70  rt( pgnoChild<=p
17399 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50  agerPagecount(pP
1739a 61 67 65 2d 3e 70 42 74 29 20 29 3b 0a 20 20 20  age->pBt) );.   
1739b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1739c 65 65 47 65 74 50 61 67 65 28 70 50 61 67 65 2d  eeGetPage(pPage-
1739d 3e 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c  >pBt, pgnoChild,
1739e 20 26 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20   &pChild, 0);.  
1739f 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
173a0 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61  end_shallow_bala
173a1 6e 63 65 3b 0a 20 20 20 20 69 66 28 20 70 50 61  nce;.    if( pPa
173a2 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20  ge->pgno==1 ){. 
173a3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
173a4 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70  3BtreeInitPage(p
173a5 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 69 66  Child);.      if
173a6 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f  ( rc ) goto end_
173a7 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b  shallow_balance;
173a8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
173a9 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77  Child->nOverflow
173aa 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ==0 );.      if(
173ab 20 70 43 68 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d   pChild->nFree>=
173ac 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  100 ){.        /
173ad 2a 20 54 68 65 20 63 68 69 6c 64 20 69 6e 66 6f  * The child info
173ae 72 6d 61 74 69 6f 6e 20 77 69 6c 6c 20 66 69 74  rmation will fit
173af 20 6f 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 67   on the root pag
173b0 65 2c 20 73 6f 20 64 6f 20 74 68 65 0a 20 20 20  e, so do the.   
173b1 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a       ** copy */.
173b2 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20          int i;. 
173b3 20 20 20 20 20 20 20 7a 65 72 6f 50 61 67 65 28         zeroPage(
173b4 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61  pPage, pChild->a
173b5 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20  Data[0]);.      
173b6 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68    for(i=0; i<pCh
173b7 69 6c 64 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29  ild->nCell; i++)
173b8 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 43 65  {.          apCe
173b9 6c 6c 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c  ll[i] = findCell
173ba 28 70 43 68 69 6c 64 2c 69 29 3b 0a 20 20 20 20  (pChild,i);.    
173bb 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 69 5d 20        szCell[i] 
173bc 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 43  = cellSizePtr(pC
173bd 68 69 6c 64 2c 20 61 70 43 65 6c 6c 5b 69 5d 29  hild, apCell[i])
173be 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
173bf 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65      assemblePage
173c0 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e  (pPage, pChild->
173c1 6e 43 65 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20 73  nCell, apCell, s
173c2 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20  zCell);.        
173c3 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 69 67 68  /* Copy the righ
173c4 74 2d 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65  t-pointer of the
173c5 20 63 68 69 6c 64 20 74 6f 20 74 68 65 20 70 61   child to the pa
173c6 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20  rent. */.       
173c7 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
173c8 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
173c9 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
173ca 20 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34   );.        put4
173cb 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
173cc 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
173cd 73 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20 20  set+8], .       
173ce 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26 70       get4byte(&p
173cf 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43 68  Child->aData[pCh
173d0 69 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  ild->hdrOffset+8
173d1 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ]));.        rc 
173d2 3d 20 66 72 65 65 50 61 67 65 28 70 43 68 69 6c  = freePage(pChil
173d3 64 29 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43  d);.        TRAC
173d4 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 69  E(("BALANCE: chi
173d5 6c 64 20 25 64 20 74 72 61 6e 73 66 65 72 20 74  ld %d transfer t
173d6 6f 20 70 61 67 65 20 31 5c 6e 22 2c 20 70 43 68  o page 1\n", pCh
173d7 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20  ild->pgno));.   
173d8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
173d9 20 20 2f 2a 20 54 68 65 20 63 68 69 6c 64 20 68    /* The child h
173da 61 73 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74  as more informat
173db 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 66 69  ion that will fi
173dc 74 20 6f 6e 20 74 68 65 20 72 6f 6f 74 2e 0a 20  t on the root.. 
173dd 20 20 20 20 20 20 20 2a 2a 20 54 68 65 20 74 72         ** The tr
173de 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 62 61  ee is already ba
173df 6c 61 6e 63 65 64 2e 20 20 44 6f 20 6e 6f 74 68  lanced.  Do noth
173e0 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ing. */.        
173e1 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a  TRACE(("BALANCE:
173e2 20 63 68 69 6c 64 20 25 64 20 77 69 6c 6c 20 6e   child %d will n
173e3 6f 74 20 66 69 74 20 6f 6e 20 70 61 67 65 20 31  ot fit on page 1
173e4 5c 6e 22 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e  \n", pChild->pgn
173e5 6f 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  o));.      }.   
173e6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65   }else{.      me
173e7 6d 63 70 79 28 70 50 61 67 65 2d 3e 61 44 61 74  mcpy(pPage->aDat
173e8 61 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61  a, pChild->aData
173e9 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  , pPage->pBt->us
173ea 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20 20  ableSize);.     
173eb 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d   pPage->isInit =
173ec 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   0;.      rc = s
173ed 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
173ee 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
173ef 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
173f0 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20  LITE_OK );.     
173f1 20 66 72 65 65 50 61 67 65 28 70 43 68 69 6c 64   freePage(pChild
173f2 29 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28  );.      TRACE((
173f3 22 42 41 4c 41 4e 43 45 3a 20 74 72 61 6e 73 66  "BALANCE: transf
173f4 65 72 20 63 68 69 6c 64 20 25 64 20 69 6e 74 6f  er child %d into
173f5 20 72 6f 6f 74 20 25 64 5c 6e 22 2c 0a 20 20 20   root %d\n",.   
173f6 20 20 20 20 20 20 20 20 20 20 20 70 43 68 69 6c             pChil
173f7 64 2d 3e 70 67 6e 6f 2c 20 70 50 61 67 65 2d 3e  d->pgno, pPage->
173f8 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20  pgno));.    }.  
173f9 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
173fa 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b  >nOverflow==0 );
173fb 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
173fc 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
173fd 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
173fe 43 55 55 4d 20 26 26 20 72 63 3d 3d 53 51 4c 49  CUUM && rc==SQLI
173ff 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
17400 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d  c = setChildPtrm
17401 61 70 73 28 70 50 61 67 65 29 3b 0a 20 20 20 20  aps(pPage);.    
17402 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 6c  }.#endif.    rel
17403 65 61 73 65 50 61 67 65 28 70 43 68 69 6c 64 29  easePage(pChild)
17404 3b 0a 20 20 7d 0a 65 6e 64 5f 73 68 61 6c 6c 6f  ;.  }.end_shallo
17405 77 5f 62 61 6c 61 6e 63 65 3a 0a 20 20 73 71 6c  w_balance:.  sql
17406 69 74 65 33 5f 66 72 65 65 28 61 70 43 65 6c 6c  ite3_free(apCell
17407 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
17408 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f  }.../*.** The ro
17409 6f 74 20 70 61 67 65 20 69 73 20 6f 76 65 72 66  ot page is overf
1740a 75 6c 6c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74  ull.**.** When t
1740b 68 69 73 20 68 61 70 70 65 6e 73 2c 20 43 72 65  his happens, Cre
1740c 61 74 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20  ate a new child 
1740d 70 61 67 65 20 61 6e 64 20 63 6f 70 79 20 74 68  page and copy th
1740e 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66  e.** contents of
1740f 20 74 68 65 20 72 6f 6f 74 20 69 6e 74 6f 20 74   the root into t
17410 68 65 20 63 68 69 6c 64 2e 20 20 54 68 65 6e 20  he child.  Then 
17411 6d 61 6b 65 20 74 68 65 20 72 6f 6f 74 0a 2a 2a  make the root.**
17412 20 70 61 67 65 20 61 6e 20 65 6d 70 74 79 20 70   page an empty p
17413 61 67 65 20 77 69 74 68 20 72 69 67 68 74 43 68  age with rightCh
17414 69 6c 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  ild pointing to 
17415 74 68 65 20 6e 65 77 0a 2a 2a 20 63 68 69 6c 64  the new.** child
17416 2e 20 20 20 46 69 6e 61 6c 6c 79 2c 20 63 61 6c  .   Finally, cal
17417 6c 20 62 61 6c 61 6e 63 65 5f 69 6e 74 65 72 6e  l balance_intern
17418 61 6c 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20  al() on the new 
17419 63 68 69 6c 64 0a 2a 2a 20 74 6f 20 63 61 75 73  child.** to caus
1741a 65 20 69 74 20 74 6f 20 73 70 6c 69 74 2e 0a 2a  e it to split..*
1741b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c  /.static int bal
1741c 61 6e 63 65 5f 64 65 65 70 65 72 28 42 74 43 75  ance_deeper(BtCu
1741d 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69  rsor *pCur){.  i
1741e 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
1741f 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c     /* Return val
17420 75 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65  ue from subproce
17421 64 75 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61  dures */.  MemPa
17422 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 2f  ge *pPage;     /
17423 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
17424 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20   root page */.  
17425 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b  MemPage *pChild;
17426 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
17427 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61  o a new child pa
17428 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  ge */.  Pgno pgn
17429 6f 43 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20 50  oChild;     /* P
1742a 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
1742b 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65  e new child page
1742c 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a   */.  BtShared *
1742d 70 42 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  pBt;         /* 
1742e 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 20 20 69  The BTree */.  i
1742f 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20  nt usableSize;  
17430 20 20 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61 62     /* Total usab
17431 6c 65 20 73 69 7a 65 20 6f 66 20 61 20 70 61 67  le size of a pag
17432 65 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b  e */.  u8 *data;
17433 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
17434 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72  ntent of the par
17435 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 75 38  ent page */.  u8
17436 20 2a 63 64 61 74 61 3b 20 20 20 20 20 20 20 20   *cdata;        
17437 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20    /* Content of 
17438 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 2a  the child page *
17439 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20  /.  int hdr;    
1743a 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
1743b 74 20 74 6f 20 70 61 67 65 20 68 65 61 64 65 72  t to page header
1743c 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20   in parent */.  
1743d 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20  int cbrk;       
1743e 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f      /* Offset to
1743f 20 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73   content of firs
17440 74 20 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e 74  t cell in parent
17441 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
17442 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 3b  Cur->iPage==0 );
17443 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
17444 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 6e 4f 76 65  >apPage[0]->nOve
17445 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 56 56  rflow>0 );..  VV
17446 41 5f 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61  A_ONLY( pCur->pa
17447 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20  gesShuffled = 1 
17448 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75  );.  pPage = pCu
17449 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20  r->apPage[0];.  
1744a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74  pBt = pPage->pBt
1744b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1744c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1744d 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
1744e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
1744f 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
17450 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
17451 29 3b 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  );.  rc = alloca
17452 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
17453 20 26 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43   &pChild, &pgnoC
17454 68 69 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e  hild, pPage->pgn
17455 6f 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  o, 0);.  if( rc 
17456 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61  ) return rc;.  a
17457 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
17458 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
17459 43 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20  Child->pDbPage) 
1745a 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20  );.  usableSize 
1745b 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
1745c 65 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67  e;.  data = pPag
1745d 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20  e->aData;.  hdr 
1745e 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
1745f 65 74 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74  et;.  cbrk = get
17460 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
17461 35 5d 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 70  5]);.  cdata = p
17462 43 68 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 20  Child->aData;.  
17463 6d 65 6d 63 70 79 28 63 64 61 74 61 2c 20 26 64  memcpy(cdata, &d
17464 61 74 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 2d  ata[hdr], pPage-
17465 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 50  >cellOffset+2*pP
17466 61 67 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b  age->nCell-hdr);
17467 0a 20 20 6d 65 6d 63 70 79 28 26 63 64 61 74 61  .  memcpy(&cdata
17468 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62  [cbrk], &data[cb
17469 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d  rk], usableSize-
1746a 63 62 72 6b 29 3b 0a 0a 20 20 61 73 73 65 72 74  cbrk);..  assert
1746b 28 20 70 43 68 69 6c 64 2d 3e 69 73 49 6e 69 74  ( pChild->isInit
1746c 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ==0 );.  rc = sq
1746d 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
1746e 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 69 66  ge(pChild);.  if
1746f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
17470 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79  ){.    int nCopy
17471 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66   = pPage->nOverf
17472 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 50 61 67 65  low*sizeof(pPage
17473 2d 3e 61 4f 76 66 6c 5b 30 5d 29 3b 0a 20 20 20  ->aOvfl[0]);.   
17474 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e   memcpy(pChild->
17475 61 4f 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61 4f  aOvfl, pPage->aO
17476 76 66 6c 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20  vfl, nCopy);.   
17477 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c   pChild->nOverfl
17478 6f 77 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ow = pPage->nOve
17479 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 70  rflow;.    if( p
1747a 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77  Child->nOverflow
1747b 20 29 7b 0a 20 20 20 20 20 20 70 43 68 69 6c 64   ){.      pChild
1747c 2d 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 20  ->nFree = 0;.   
1747d 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
1747e 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 50  Child->nCell==pP
1747f 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
17480 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17481 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
17482 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
17483 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67  ) );.    zeroPag
17484 65 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d  e(pPage, pChild-
17485 3e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46  >aData[0] & ~PTF
17486 5f 4c 45 41 46 29 3b 0a 20 20 20 20 70 75 74 34  _LEAF);.    put4
17487 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
17488 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
17489 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c  set+8], pgnoChil
1748a 64 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22  d);.    TRACE(("
1748b 42 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f  BALANCE: copy ro
1748c 6f 74 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22  ot %d into %d\n"
1748d 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70  , pPage->pgno, p
1748e 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20  Child->pgno));. 
1748f 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
17490 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20  UUM ){.      rc 
17491 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c  = ptrmapPut(pBt,
17492 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50   pChild->pgno, P
17493 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61  TRMAP_BTREE, pPa
17494 67 65 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64  ge->pgno);.#ifnd
17495 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
17496 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
17497 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
17498 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
17499 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70  = setChildPtrmap
1749a 73 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20  s(pChild);.     
1749b 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20   }.      if( rc 
1749c 29 7b 0a 20 20 20 20 20 20 20 20 70 43 68 69 6c  ){.        pChil
1749d 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30  d->nOverflow = 0
1749e 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
1749f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
174a0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
174a1 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61  ){.    pCur->iPa
174a2 67 65 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d 3e  ge++;.    pCur->
174a3 61 70 50 61 67 65 5b 31 5d 20 3d 20 70 43 68 69  apPage[1] = pChi
174a4 6c 64 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69  ld;.    pCur->ai
174a5 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20  Idx[0] = 0;.    
174a6 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e  rc = balance_non
174a7 72 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 65  root(pCur);.  }e
174a8 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  lse{.    release
174a9 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20  Page(pChild);.  
174aa 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
174ab 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67  }../*.** The pag
174ac 65 20 74 68 61 74 20 70 43 75 72 20 63 75 72 72  e that pCur curr
174ad 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20  ently points to 
174ae 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f  has just been mo
174af 64 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d  dified in.** som
174b0 65 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63  e way. This func
174b1 74 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74  tion figures out
174b2 20 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63   if this modific
174b3 61 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a  ation means the.
174b4 2a 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f  ** tree needs to
174b5 20 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e   be balanced, an
174b6 64 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68  d if so calls th
174b7 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 62 61  e appropriate ba
174b8 6c 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74  lancing .** rout
174b9 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72 61  ine..** .** Para
174ba 6d 65 74 65 72 20 69 73 49 6e 73 65 72 74 20 69  meter isInsert i
174bb 73 20 74 72 75 65 20 69 66 20 61 20 6e 65 77 20  s true if a new 
174bc 63 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e  cell was just in
174bd 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 0a  serted into the.
174be 2a 2a 20 70 61 67 65 2c 20 6f 72 20 66 61 6c 73  ** page, or fals
174bf 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a  e otherwise..*/.
174c0 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e  static int balan
174c1 63 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ce(BtCursor *pCu
174c2 72 2c 20 69 6e 74 20 69 73 49 6e 73 65 72 74 29  r, int isInsert)
174c3 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
174c4 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67  ITE_OK;.  MemPag
174c5 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d  e *pPage = pCur-
174c6 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
174c7 61 67 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28  age];..  assert(
174c8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
174c9 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
174ca 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
174cb 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29  pCur->iPage==0 )
174cc 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
174cd 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
174ce 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ge->pDbPage);.  
174cf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
174d0 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f  _OK && pPage->nO
174d1 76 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 20  verflow>0 ){.   
174d2 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f     rc = balance_
174d3 64 65 65 70 65 72 28 70 43 75 72 29 3b 0a 20 20  deeper(pCur);.  
174d4 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
174d5 2d 3e 61 70 50 61 67 65 5b 30 5d 3d 3d 70 50 61  ->apPage[0]==pPa
174d6 67 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ge );.      asse
174d7 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  rt( pPage->nOver
174d8 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63 21 3d 53  flow==0 || rc!=S
174d9 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
174da 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
174db 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65  LITE_OK && pPage
174dc 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20  ->nCell==0 ){.  
174dd 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
174de 5f 73 68 61 6c 6c 6f 77 65 72 28 70 43 75 72 29  _shallower(pCur)
174df 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
174e0 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3d  pCur->apPage[0]=
174e1 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20  =pPage );.      
174e2 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
174e3 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72  Overflow==0 || r
174e4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c!=SQLITE_OK );.
174e5 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
174e6 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f     if( pPage->nO
174e7 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 20  verflow>0 || .  
174e8 20 20 20 20 20 20 28 21 69 73 49 6e 73 65 72 74        (!isInsert
174e9 20 26 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65   && pPage->nFree
174ea 3e 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61  >pPage->pBt->usa
174eb 62 6c 65 53 69 7a 65 2a 32 2f 33 29 20 29 7b 0a  bleSize*2/3) ){.
174ec 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e        rc = balan
174ed 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 75 72 29  ce_nonroot(pCur)
174ee 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
174ef 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
174f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
174f1 68 65 63 6b 73 20 61 6c 6c 20 63 75 72 73 6f 72  hecks all cursor
174f2 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20  s that point to 
174f3 74 61 62 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a  table pgnoRoot..
174f4 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 6f  ** If any of tho
174f5 73 65 20 63 75 72 73 6f 72 73 20 77 65 72 65 20  se cursors were 
174f6 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c  opened with wrFl
174f7 61 67 3d 3d 30 20 69 6e 20 61 20 64 69 66 66 65  ag==0 in a diffe
174f8 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73 65  rent.** database
174f9 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 61 20 64   connection (a d
174fa 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
174fb 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74  on that shares t
174fc 68 65 20 70 61 67 65 72 0a 2a 2a 20 63 61 63 68  he pager.** cach
174fd 65 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65  e with the curre
174fe 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 61  nt connection) a
174ff 6e 64 20 74 68 61 74 20 6f 74 68 65 72 20 63 6f  nd that other co
17500 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20  nnection .** is 
17501 6e 6f 74 20 69 6e 20 74 68 65 20 52 65 61 64 55  not in the ReadU
17502 6e 63 6f 6d 6d 6d 69 74 74 65 64 20 73 74 61 74  ncommmitted stat
17503 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  e, then this rou
17504 74 69 6e 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a  tine returns .**
17505 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a   SQLITE_LOCKED..
17506 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73  **.** As well as
17507 20 63 75 72 73 6f 72 73 20 77 69 74 68 20 77 72   cursors with wr
17508 46 6c 61 67 3d 3d 30 2c 20 63 75 72 73 6f 72 73  Flag==0, cursors
17509 20 77 69 74 68 20 0a 2a 2a 20 69 73 49 6e 63 72   with .** isIncr
1750a 62 6c 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61 72  blobHandle==1 ar
1750b 65 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65  e also considere
1750c 64 20 27 72 65 61 64 27 20 63 75 72 73 6f 72 73  d 'read' cursors
1750d 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 6e 63 72   because.** incr
1750e 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72  emental blob cur
1750f 73 6f 72 73 20 61 72 65 20 75 73 65 64 20 66 6f  sors are used fo
17510 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61  r both reading a
17511 6e 64 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a  nd writing..**.*
17512 2a 20 57 68 65 6e 20 70 67 6e 6f 52 6f 6f 74 20  * When pgnoRoot 
17513 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  is the root page
17514 20 6f 66 20 61 6e 20 69 6e 74 6b 65 79 20 74 61   of an intkey ta
17515 62 6c 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ble, this functi
17516 6f 6e 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65  on is also.** re
17517 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e  sponsible for in
17518 76 61 6c 69 64 61 74 69 6e 67 20 69 6e 63 72 65  validating incre
17519 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73  mental blob curs
1751a 6f 72 73 20 77 68 65 6e 20 74 68 65 20 74 61 62  ors when the tab
1751b 6c 65 20 72 6f 77 0a 2a 2a 20 6f 6e 20 77 68 69  le row.** on whi
1751c 63 68 20 74 68 65 79 20 61 72 65 20 6f 70 65 6e  ch they are open
1751d 65 64 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72  ed is deleted or
1751e 20 6d 6f 64 69 66 69 65 64 2e 20 43 75 72 73 6f   modified. Curso
1751f 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74  rs are invalidat
17520 65 64 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20  ed.** according 
17521 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  to the following
17522 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20   rules:.**.**   
17523 31 29 20 57 68 65 6e 20 42 74 72 65 65 43 6c 65  1) When BtreeCle
17524 61 72 54 61 62 6c 65 28 29 20 69 73 20 63 61 6c  arTable() is cal
17525 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c  led to completel
17526 79 20 64 65 6c 65 74 65 20 74 68 65 20 63 6f 6e  y delete the con
17527 74 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 6f 66  tents.**      of
17528 20 61 20 42 2d 54 72 65 65 20 74 61 62 6c 65 2c   a B-Tree table,
17529 20 70 45 78 63 6c 75 64 65 20 69 73 20 73 65 74   pExclude is set
1752a 20 74 6f 20 7a 65 72 6f 20 61 6e 64 20 70 61 72   to zero and par
1752b 61 6d 65 74 65 72 20 69 52 6f 77 20 69 73 20 0a  ameter iRow is .
1752c 2a 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 6e  **      set to n
1752d 6f 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73  on-zero. In this
1752e 20 63 61 73 65 20 61 6c 6c 20 69 6e 63 72 65 6d   case all increm
1752f 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f  ental blob curso
17530 72 73 20 6f 70 65 6e 0a 2a 2a 20 20 20 20 20 20  rs open.**      
17531 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f  on the table roo
17532 74 65 64 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20  ted at pgnoRoot 
17533 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e  are invalidated.
17534 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e  .**.**   2) When
17535 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 2c 20   BtreeInsert(), 
17536 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72  BtreeDelete() or
17537 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29 20   BtreePutData() 
17538 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 0a 2a 2a  is called to .**
17539 20 20 20 20 20 20 6d 6f 64 69 66 79 20 61 20 74        modify a t
1753a 61 62 6c 65 20 72 6f 77 20 76 69 61 20 61 6e 20  able row via an 
1753b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 70  SQL statement, p
1753c 45 78 63 6c 75 64 65 20 69 73 20 73 65 74 20 74  Exclude is set t
1753d 6f 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 77  o the .**      w
1753e 72 69 74 65 20 63 75 72 73 6f 72 20 75 73 65 64  rite cursor used
1753f 20 74 6f 20 64 6f 20 74 68 65 20 6d 6f 64 69 66   to do the modif
17540 69 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 72 61  ication and para
17541 6d 65 74 65 72 20 69 52 6f 77 20 69 73 20 73 65  meter iRow is se
17542 74 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65  t.**      to the
17543 20 69 6e 74 65 67 65 72 20 72 6f 77 20 69 64 20   integer row id 
17544 6f 66 20 74 68 65 20 42 2d 54 72 65 65 20 65 6e  of the B-Tree en
17545 74 72 79 20 62 65 69 6e 67 20 6d 6f 64 69 66 69  try being modifi
17546 65 64 2e 20 55 6e 6c 65 73 73 0a 2a 2a 20 20 20  ed. Unless.**   
17547 20 20 20 70 45 78 63 6c 75 64 65 20 69 73 20 69     pExclude is i
17548 74 73 65 6c 66 20 61 6e 20 69 6e 63 72 65 6d 65  tself an increme
17549 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72  ntal blob cursor
1754a 2c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 63 72 65  , then all incre
1754b 6d 65 6e 74 61 6c 0a 2a 2a 20 20 20 20 20 20 62  mental.**      b
1754c 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e  lob cursors open
1754d 20 6f 6e 20 72 6f 77 20 69 52 6f 77 20 6f 66 20   on row iRow of 
1754e 74 68 65 20 42 2d 54 72 65 65 20 61 72 65 20 69  the B-Tree are i
1754f 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a  nvalidated..**.*
17550 2a 20 20 20 33 29 20 49 66 20 62 6f 74 68 20 70  *   3) If both p
17551 45 78 63 6c 75 64 65 20 61 6e 64 20 69 52 6f 77  Exclude and iRow
17552 20 61 72 65 20 73 65 74 20 74 6f 20 7a 65 72 6f   are set to zero
17553 2c 20 6e 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c  , no incremental
17554 20 62 6c 6f 62 20 0a 2a 2a 20 20 20 20 20 20 63   blob .**      c
17555 75 72 73 6f 72 73 20 61 72 65 20 69 6e 76 61 6c  ursors are inval
17556 69 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  idated..*/.stati
17557 63 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 52 65  c int checkForRe
17558 61 64 43 6f 6e 66 6c 69 63 74 73 28 0a 20 20 42  adConflicts(.  B
17559 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20  tree *pBtree,   
1755a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
1755b 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63  tabase file to c
1755c 68 65 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  heck */.  Pgno p
1755d 67 6e 6f 52 6f 6f 74 2c 20 20 20 20 20 20 20 20  gnoRoot,        
1755e 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 72 65    /* Look for re
1755f 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68  ad cursors on th
17560 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74  is btree */.  Bt
17561 43 75 72 73 6f 72 20 2a 70 45 78 63 6c 75 64 65  Cursor *pExclude
17562 2c 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20  ,     /* Ignore 
17563 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20  this cursor */. 
17564 20 69 36 34 20 69 52 6f 77 20 20 20 20 20 20 20   i64 iRow       
17565 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
17566 72 6f 77 69 64 20 74 68 61 74 20 6d 69 67 68 74  rowid that might
17567 20 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a   be changing */.
17568 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  ){.  BtCursor *p
17569 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
1756a 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b  t = pBtree->pBt;
1756b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1756c 20 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 61   pBtree->db;.  a
1756d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1756e 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42  reeHoldsMutex(pB
1756f 74 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70  tree) );.  for(p
17570 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70  =pBt->pCursor; p
17571 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20  ; p=p->pNext){. 
17572 20 20 20 69 66 28 20 70 3d 3d 70 45 78 63 6c 75     if( p==pExclu
17573 64 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  de ) continue;. 
17574 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f     if( p->pgnoRo
17575 6f 74 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 20 63  ot!=pgnoRoot ) c
17576 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66  ontinue;.#ifndef
17577 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
17578 52 42 4c 4f 42 0a 20 20 20 20 69 66 28 20 70 2d  RBLOB.    if( p-
17579 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c  >isIncrblobHandl
1757a 65 20 26 26 20 28 20 0a 20 20 20 20 20 20 20 20  e && ( .        
1757b 20 28 21 70 45 78 63 6c 75 64 65 20 26 26 20 69   (!pExclude && i
1757c 52 6f 77 29 0a 20 20 20 20 20 20 7c 7c 20 28 70  Row).      || (p
1757d 45 78 63 6c 75 64 65 20 26 26 20 21 70 45 78 63  Exclude && !pExc
1757e 6c 75 64 65 2d 3e 69 73 49 6e 63 72 62 6c 6f 62  lude->isIncrblob
1757f 48 61 6e 64 6c 65 20 26 26 20 70 2d 3e 69 6e 66  Handle && p->inf
17580 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 0a 20 20  o.nKey==iRow).  
17581 20 20 29 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65    )){.      p->e
17582 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49  State = CURSOR_I
17583 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 23 65  NVALID;.    }.#e
17584 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70 2d 3e  ndif.    if( p->
17585 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56  eState!=CURSOR_V
17586 41 4c 49 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ALID ) continue;
17587 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c  .    if( p->wrFl
17588 61 67 3d 3d 30 20 0a 23 69 66 6e 64 65 66 20 53  ag==0 .#ifndef S
17589 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
1758a 4c 4f 42 0a 20 20 20 20 20 7c 7c 20 70 2d 3e 69  LOB.     || p->i
1758b 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 0a  sIncrblobHandle.
1758c 23 65 6e 64 69 66 0a 20 20 20 20 29 7b 0a 20 20  #endif.    ){.  
1758d 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4f      sqlite3 *dbO
1758e 74 68 65 72 20 3d 20 70 2d 3e 70 42 74 72 65 65  ther = p->pBtree
1758f 2d 3e 64 62 3b 0a 20 20 20 20 20 20 61 73 73 65  ->db;.      asse
17590 72 74 28 64 62 4f 74 68 65 72 29 3b 0a 20 20 20  rt(dbOther);.   
17591 20 20 20 69 66 28 20 64 62 4f 74 68 65 72 21 3d     if( dbOther!=
17592 64 62 20 26 26 20 28 64 62 4f 74 68 65 72 2d 3e  db && (dbOther->
17593 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52  flags & SQLITE_R
17594 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 3d  eadUncommitted)=
17595 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
17596 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
17597 6c 6f 63 6b 65 64 28 64 62 2c 20 64 62 4f 74 68  locked(db, dbOth
17598 65 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  er);.        ret
17599 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
1759a 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20  D_SHAREDCACHE;. 
1759b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1759c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1759d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  _OK;.}../*.** In
1759e 73 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72  sert a new recor
1759f 64 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65  d into the BTree
175a0 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69  .  The key is gi
175a1 76 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65  ven by (pKey,nKe
175a2 79 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  y).** and the da
175a3 74 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28  ta is given by (
175a4 70 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54  pData,nData).  T
175a5 68 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65  he cursor is use
175a6 64 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66  d only to.** def
175a7 69 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74  ine what table t
175a8 68 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64  he record should
175a9 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74   be inserted int
175aa 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a  o.  The cursor.*
175ab 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  * is left pointi
175ac 6e 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c  ng at a random l
175ad 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46  ocation..**.** F
175ae 6f 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62  or an INTKEY tab
175af 6c 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65  le, only the nKe
175b0 79 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b  y value of the k
175b1 65 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65  ey is used.  pKe
175b2 79 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e  y is.** ignored.
175b3 20 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41    For a ZERODATA
175b4 20 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74   table, the pDat
175b5 61 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20  a and nData are 
175b6 62 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a  both ignored..**
175b7 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52  .** If the seekR
175b8 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20  esult parameter 
175b9 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
175ba 6e 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63  n a successful c
175bb 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
175bc 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 74  3BtreeMoveto() t
175bd 6f 20 73 65 65 6b 20 63 75 72 73 6f 72 20 70 43  o seek cursor pC
175be 75 72 20 74 6f 20 28 70 4b 65 79 2c 20 6e 4b 65  ur to (pKey, nKe
175bf 79 29 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a  y) has already.*
175c0 2a 20 62 65 65 6e 20 70 65 72 66 6f 72 6d 65 64  * been performed
175c1 2e 20 73 65 65 6b 52 65 73 75 6c 74 20 69 73 20  . seekResult is 
175c2 74 68 65 20 73 65 61 72 63 68 20 72 65 73 75 6c  the search resul
175c3 74 20 72 65 74 75 72 6e 65 64 20 28 61 20 6e 65  t returned (a ne
175c4 67 61 74 69 76 65 0a 2a 2a 20 6e 75 6d 62 65 72  gative.** number
175c5 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20   if pCur points 
175c6 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74  at an entry that
175c7 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e   is smaller than
175c8 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 2c 20 6f   (pKey, nKey), o
175c9 72 0a 2a 2a 20 61 20 70 6f 73 69 74 69 76 65 20  r.** a positive 
175ca 76 61 6c 75 65 20 69 66 20 70 43 75 72 20 70 6f  value if pCur po
175cb 69 6e 74 73 20 61 74 20 61 6e 20 65 74 72 79 20  ints at an etry 
175cc 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74  that is larger t
175cd 68 61 6e 20 0a 2a 2a 20 28 70 4b 65 79 2c 20 6e  han .** (pKey, n
175ce 4b 65 79 29 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  Key)). .**.** If
175cf 20 74 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20   the seekResult 
175d0 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20  parameter is 0, 
175d1 74 68 65 6e 20 63 75 72 73 6f 72 20 70 43 75 72  then cursor pCur
175d2 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 6e   may point to an
175d3 79 20 0a 2a 2a 20 65 6e 74 72 79 20 6f 72 20 74  y .** entry or t
175d4 6f 20 6e 6f 20 65 6e 74 72 79 20 61 74 20 61 6c  o no entry at al
175d5 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  l. In this case 
175d6 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61  this function ha
175d7 73 20 74 6f 20 73 65 65 6b 0a 2a 2a 20 74 68 65  s to seek.** the
175d8 20 63 75 72 73 6f 72 20 62 65 66 6f 72 65 20 74   cursor before t
175d9 68 65 20 6e 65 77 20 6b 65 79 20 63 61 6e 20 62  he new key can b
175da 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53  e inserted..*/.S
175db 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
175dc 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  t sqlite3BtreeIn
175dd 73 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f 72  sert(.  BtCursor
175de 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20   *pCur,         
175df 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74         /* Insert
175e0 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 74   data into the t
175e1 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72  able of this cur
175e2 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  sor */.  const v
175e3 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e  oid *pKey, i64 n
175e4 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b  Key,    /* The k
175e5 65 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65  ey of the new re
175e6 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  cord */.  const 
175e7 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74  void *pData, int
175e8 20 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 20   nData,  /* The 
175e9 64 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77 20  data of the new 
175ea 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20  record */.  int 
175eb 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20  nZero,          
175ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
175ed 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 30 20  mber of extra 0 
175ee 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20  bytes to append 
175ef 74 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74  to data */.  int
175f0 20 61 70 70 65 6e 64 42 69 61 73 2c 20 20 20 20   appendBias,    
175f1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
175f2 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c  rue if this is l
175f3 69 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 20  ikely an append 
175f4 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73  */.  int seekRes
175f5 75 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ult             
175f6 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66      /* Result of
175f7 20 70 72 69 6f 72 20 73 71 6c 69 74 65 33 42 74   prior sqlite3Bt
175f8 72 65 65 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c  reeMoveto() call
175f9 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
175fa 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65  .  int loc = see
175fb 6b 52 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 73  kResult;.  int s
175fc 7a 4e 65 77 3b 0a 20 20 69 6e 74 20 69 64 78 3b  zNew;.  int idx;
175fd 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
175fe 65 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20  e;.  Btree *p = 
175ff 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20  pCur->pBtree;.  
17600 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
17601 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e  p->pBt;.  unsign
17602 65 64 20 63 68 61 72 20 2a 6f 6c 64 43 65 6c 6c  ed char *oldCell
17603 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
17604 72 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 30 3b 0a  r *newCell = 0;.
17605 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
17606 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
17607 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
17608 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
17609 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  n==TRANS_WRITE )
1760a 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 42 74  ;.  assert( !pBt
1760b 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20  ->readOnly );.  
1760c 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72  assert( pCur->wr
1760d 46 6c 61 67 20 29 3b 0a 20 20 72 63 20 3d 20 63  Flag );.  rc = c
1760e 68 65 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c  heckForReadConfl
1760f 69 63 74 73 28 70 43 75 72 2d 3e 70 42 74 72 65  icts(pCur->pBtre
17610 65 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  e, pCur->pgnoRoo
17611 74 2c 20 70 43 75 72 2c 20 6e 4b 65 79 29 3b 0a  t, pCur, nKey);.
17612 20 20 69 66 28 20 72 63 20 29 7b 20 20 20 20 20    if( rc ){     
17613 20 20 20 20 20 20 20 20 0a 20 20 20 20 2f 2a 20          .    /* 
17614 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20 70  The table pCur p
17615 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20 72  oints to has a r
17616 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  ead lock */.    
17617 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
17618 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
17619 43 41 43 48 45 20 29 3b 0a 20 20 20 20 72 65 74  CACHE );.    ret
1761a 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66  urn rc;.  }.  if
1761b 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
1761c 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a  CURSOR_FAULT ){.
1761d 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d      return pCur-
1761e 3e 73 6b 69 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  >skip;.  }..  /*
1761f 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69   Save the positi
17620 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72  ons of any other
17621 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e   cursors open on
17622 20 74 68 69 73 20 74 61 62 6c 65 2e 0a 20 20 2a   this table..  *
17623 2a 0a 20 20 2a 2a 20 49 6e 20 73 6f 6d 65 20 63  *.  ** In some c
17624 61 73 65 73 2c 20 74 68 65 20 63 61 6c 6c 20 74  ases, the call t
17625 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  o sqlite3BtreeMo
17626 76 65 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20  veto() below is 
17627 61 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a  a no-op. For.  *
17628 2a 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20  * example, when 
17629 69 6e 73 65 72 74 69 6e 67 20 64 61 74 61 20 69  inserting data i
1762a 6e 74 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68  nto a table with
1762b 20 61 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20   auto-generated 
1762c 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79  integer.  ** key
1762d 73 2c 20 74 68 65 20 56 44 42 45 20 6c 61 79 65  s, the VDBE laye
1762e 72 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65  r invokes sqlite
1762f 33 42 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20  3BtreeLast() to 
17630 66 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a  figure out the .
17631 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79    ** integer key
17632 20 74 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e   to use. It then
17633 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63   calls this func
17634 74 69 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79  tion to actually
17635 20 69 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a   insert the .  *
17636 2a 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20  * data into the 
17637 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49  intkey B-Tree. I
17638 6e 20 74 68 69 73 20 63 61 73 65 20 73 71 6c 69  n this case sqli
17639 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29  te3BtreeMoveto()
1763a 20 72 65 63 6f 67 6e 69 7a 65 73 0a 20 20 2a 2a   recognizes.  **
1763b 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
1763c 20 69 73 20 61 6c 72 65 61 64 79 20 77 68 65 72   is already wher
1763d 65 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65  e it needs to be
1763e 20 61 6e 64 20 72 65 74 75 72 6e 73 20 77 69 74   and returns wit
1763f 68 6f 75 74 0a 20 20 2a 2a 20 64 6f 69 6e 67 20  hout.  ** doing 
17640 61 6e 79 20 77 6f 72 6b 2e 20 54 6f 20 61 76 6f  any work. To avo
17641 69 64 20 74 68 77 61 72 74 69 6e 67 20 74 68 65  id thwarting the
17642 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  se optimizations
17643 2c 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  , it is importan
17644 74 0a 20 20 2a 2a 20 6e 6f 74 20 74 6f 20 63 6c  t.  ** not to cl
17645 65 61 72 20 74 68 65 20 63 75 72 73 6f 72 20 68  ear the cursor h
17646 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 0a  ere..  */.  if(.
17647 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28      SQLITE_OK!=(
17648 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73  rc = saveAllCurs
17649 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70  ors(pBt, pCur->p
1764a 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 29 20  gnoRoot, pCur)) 
1764b 7c 7c 20 28 21 6c 6f 63 20 26 26 0a 20 20 20 20  || (!loc &&.    
1764c 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
1764d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1764e 65 74 6f 28 70 43 75 72 2c 20 70 4b 65 79 2c 20  eto(pCur, pKey, 
1764f 6e 4b 65 79 2c 20 61 70 70 65 6e 64 42 69 61 73  nKey, appendBias
17650 2c 20 26 6c 6f 63 29 29 0a 20 20 29 29 7b 0a 20  , &loc)).  )){. 
17651 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
17652 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75  }..  pPage = pCu
17653 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
17654 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74  iPage];.  assert
17655 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
17656 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20  || nKey>=0 );.  
17657 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c  assert( pPage->l
17658 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69  eaf || !pPage->i
17659 6e 74 4b 65 79 20 29 3b 0a 20 20 54 52 41 43 45  ntKey );.  TRACE
1765a 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65  (("INSERT: table
1765b 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64  =%d nkey=%lld nd
1765c 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25  ata=%d page=%d %
1765d 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
1765e 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20  pCur->pgnoRoot, 
1765f 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61  nKey, nData, pPa
17660 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20  ge->pgno,.      
17661 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76      loc==0 ? "ov
17662 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20  erwrite" : "new 
17663 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65  entry"));.  asse
17664 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  rt( pPage->isIni
17665 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54  t );.  allocateT
17666 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20  empSpace(pBt);. 
17667 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e   newCell = pBt->
17668 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28  pTmpSpace;.  if(
17669 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65   newCell==0 ) re
1766a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
1766b 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e  M;.  rc = fillIn
1766c 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43  Cell(pPage, newC
1766d 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c  ell, pKey, nKey,
1766e 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e   pData, nData, n
1766f 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20  Zero, &szNew);. 
17670 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65   if( rc ) goto e
17671 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73  nd_insert;.  ass
17672 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c  ert( szNew==cell
17673 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e  SizePtr(pPage, n
17674 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73  ewCell) );.  ass
17675 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43  ert( szNew<=MX_C
17676 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b  ELL_SIZE(pBt) );
17677 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61  .  idx = pCur->a
17678 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
17679 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20  ];.  if( loc==0 
1767a 26 26 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d  && CURSOR_VALID=
1767b 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b  =pCur->eState ){
1767c 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a  .    u16 szOld;.
1767d 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c      assert( idx<
1767e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  pPage->nCell );.
1767f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17680 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
17681 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
17682 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
17683 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b  goto end_insert;
17684 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65  .    }.    oldCe
17685 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
17686 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69  age, idx);.    i
17687 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  f( !pPage->leaf 
17688 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
17689 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c  newCell, oldCell
1768a 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 4);.    }.    
1768b 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65  szOld = cellSize
1768c 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65  Ptr(pPage, oldCe
1768d 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c  ll);.    rc = cl
1768e 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f  earCell(pPage, o
1768f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28  ldCell);.    if(
17690 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69   rc ) goto end_i
17691 6e 73 65 72 74 3b 0a 20 20 20 20 72 63 20 3d 20  nsert;.    rc = 
17692 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20  dropCell(pPage, 
17693 69 64 78 2c 20 73 7a 4f 6c 64 29 3b 0a 20 20 20  idx, szOld);.   
17694 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17695 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 67 6f 74  OK ) {.      got
17696 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20  o end_insert;.  
17697 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
17698 6c 6f 63 3c 30 20 26 26 20 70 50 61 67 65 2d 3e  loc<0 && pPage->
17699 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 61  nCell>0 ){.    a
1769a 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65  ssert( pPage->le
1769b 61 66 20 29 3b 0a 20 20 20 20 69 64 78 20 3d 20  af );.    idx = 
1769c 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  ++pCur->aiIdx[pC
1769d 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
1769e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
1769f 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e   = 0;.    pCur->
176a0 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
176a1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
176a2 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  rt( pPage->leaf 
176a3 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 69 6e  );.  }.  rc = in
176a4 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20  sertCell(pPage, 
176a5 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a  idx, newCell, sz
176a6 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20 61 73  New, 0, 0);.  as
176a7 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
176a8 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 43  _OK || pPage->nC
176a9 65 6c 6c 3e 30 20 7c 7c 20 70 50 61 67 65 2d 3e  ell>0 || pPage->
176aa 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a  nOverflow>0 );..
176ab 20 20 2f 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72    /* If no error
176ac 20 68 61 73 20 6f 63 63 75 72 65 64 2c 20 63 61   has occured, ca
176ad 6c 6c 20 62 61 6c 61 6e 63 65 28 29 20 74 6f 20  ll balance() to 
176ae 64 65 61 6c 20 77 69 74 68 20 61 6e 79 20 6f 76  deal with any ov
176af 65 72 66 6c 6f 77 20 61 6e 64 0a 20 20 2a 2a 20  erflow and.  ** 
176b0 6d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  move the cursor 
176b1 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
176b2 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c  root of the tabl
176b3 65 20 28 73 69 6e 63 65 20 62 61 6c 61 6e 63 65  e (since balance
176b4 20 6d 61 79 0a 20 20 2a 2a 20 68 61 76 65 20 72   may.  ** have r
176b5 65 61 72 72 61 6e 67 65 64 20 74 68 65 20 74 61  earranged the ta
176b6 62 6c 65 20 69 6e 20 73 75 63 68 20 61 20 77 61  ble in such a wa
176b7 79 20 61 73 20 74 6f 20 69 6e 76 61 6c 69 64 61  y as to invalida
176b8 74 65 20 42 74 43 75 72 73 6f 72 2e 61 70 50 61  te BtCursor.apPa
176b9 67 65 5b 5d 0a 20 20 2a 2a 20 6f 72 20 42 74 43  ge[].  ** or BtC
176ba 75 72 73 6f 72 2e 61 69 49 64 78 5b 5d 29 2e 0a  ursor.aiIdx[])..
176bb 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74    **.  ** Except
176bc 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20  , if all of the 
176bd 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72  following are tr
176be 75 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 3a 0a  ue, do nothing:.
176bf 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 49 6e    **.  **   * In
176c0 73 65 72 74 69 6e 67 20 74 68 65 20 6e 65 77 20  serting the new 
176c1 63 65 6c 6c 20 64 69 64 20 6e 6f 74 20 63 61 75  cell did not cau
176c2 73 65 20 6f 76 65 72 66 6c 6f 77 2c 0a 20 20 2a  se overflow,.  *
176c3 2a 0a 20 20 2a 2a 20 20 20 2a 20 42 65 66 6f 72  *.  **   * Befor
176c4 65 20 69 6e 73 65 72 74 69 6e 67 20 74 68 65 20  e inserting the 
176c5 6e 65 77 20 63 65 6c 6c 20 74 68 65 20 63 75 72  new cell the cur
176c6 73 6f 72 20 77 61 73 20 70 6f 69 6e 74 69 6e 67  sor was pointing
176c7 20 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 20 20   at the .  **   
176c8 20 20 6c 61 72 67 65 73 74 20 6b 65 79 20 69 6e    largest key in
176c9 20 61 6e 20 69 6e 74 6b 65 79 20 42 2d 54 72 65   an intkey B-Tre
176ca 65 2c 20 61 6e 64 0a 20 20 2a 2a 0a 20 20 2a 2a  e, and.  **.  **
176cb 20 20 20 2a 20 54 68 65 20 6b 65 79 20 76 61 6c     * The key val
176cc 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ue associated wi
176cd 74 68 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20  th the new cell 
176ce 69 73 20 6e 6f 77 20 74 68 65 20 6c 61 72 67 65  is now the large
176cf 73 74 20 0a 20 20 2a 2a 20 20 20 20 20 69 6e 20  st .  **     in 
176d0 74 68 65 20 42 2d 54 72 65 65 2e 0a 20 20 2a 2a  the B-Tree..  **
176d1 0a 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61  .  ** In this ca
176d2 73 65 20 74 68 65 20 63 75 72 73 6f 72 20 63 61  se the cursor ca
176d3 6e 20 62 65 20 73 61 66 65 6c 79 20 6c 65 66 74  n be safely left
176d4 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65   pointing at the
176d5 20 28 6e 65 77 29 20 0a 20 20 2a 2a 20 6c 61 72   (new) .  ** lar
176d6 67 65 73 74 20 6b 65 79 20 76 61 6c 75 65 20 69  gest key value i
176d7 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 44 6f  n the B-Tree. Do
176d8 69 6e 67 20 73 6f 20 73 70 65 65 64 73 20 75 70  ing so speeds up
176d9 20 69 6e 73 65 72 74 69 6e 67 20 61 20 73 65 74   inserting a set
176da 0a 20 20 2a 2a 20 6f 66 20 65 6e 74 72 69 65 73  .  ** of entries
176db 20 77 69 74 68 20 69 6e 63 72 65 61 73 69 6e 67   with increasing
176dc 20 69 6e 74 65 67 65 72 20 6b 65 79 20 76 61 6c   integer key val
176dd 75 65 73 20 76 69 61 20 61 20 73 69 6e 67 6c 65  ues via a single
176de 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 28 63 6f   cursor.  ** (co
176df 6d 65 73 20 75 70 20 77 69 74 68 20 22 49 4e 53  mes up with "INS
176e0 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c  ERT INTO ... SEL
176e1 45 43 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65  ECT ..." stateme
176e2 6e 74 73 29 2c 20 61 73 20 0a 20 20 2a 2a 20 74  nts), as .  ** t
176e3 68 65 20 6e 65 78 74 20 69 6e 73 65 72 74 20 6f  he next insert o
176e4 70 65 72 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20  peration is not 
176e5 72 65 71 75 69 72 65 64 20 74 6f 20 73 65 65 6b  required to seek
176e6 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a   the cursor..  *
176e7 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
176e8 54 45 5f 4f 4b 20 0a 20 20 20 26 26 20 28 70 50  TE_OK .   && (pP
176e9 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c  age->nOverflow |
176ea 7c 20 21 70 43 75 72 2d 3e 61 74 4c 61 73 74 20  | !pCur->atLast 
176eb 7c 7c 20 6c 6f 63 3e 3d 30 20 7c 7c 20 21 70 43  || loc>=0 || !pC
176ec 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69  ur->apPage[0]->i
176ed 6e 74 4b 65 79 29 0a 20 20 29 7b 0a 20 20 20 20  ntKey).  ){.    
176ee 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75  rc = balance(pCu
176ef 72 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72  r, 1);.    if( r
176f0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
176f1 20 20 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74        moveToRoot
176f2 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20  (pCur);.    }.  
176f3 7d 0a 20 20 0a 20 20 2f 2a 20 4d 75 73 74 20 6d  }.  .  /* Must m
176f4 61 6b 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c  ake sure nOverfl
176f5 6f 77 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a  ow is reset to z
176f6 65 72 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20  ero even if the 
176f7 62 61 6c 61 6e 63 65 28 29 0a 20 20 2a 2a 20 66  balance().  ** f
176f8 61 69 6c 73 2e 20 20 49 6e 74 65 72 6e 61 6c 20  ails.  Internal 
176f9 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63  data structure c
176fa 6f 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72  orruption will r
176fb 65 73 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e  esult otherwise.
176fc 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 61 70 50 61   */.  pCur->apPa
176fd 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
176fe 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a  >nOverflow = 0;.
176ff 0a 65 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72  .end_insert:.  r
17700 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
17701 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e  ** Delete the en
17702 74 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72  try that the cur
17703 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  sor is pointing 
17704 74 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a  to.  The cursor.
17705 2a 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  ** is left point
17706 69 6e 67 20 61 74 20 61 20 61 72 62 69 74 72 61  ing at a arbitra
17707 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a  ry location..*/.
17708 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
17709 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
1770a 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a  elete(BtCursor *
1770b 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65  pCur){.  MemPage
1770c 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e   *pPage = pCur->
1770d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1770e 67 65 5d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a  ge];.  int idx;.
1770f 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
17710 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63  *pCell;.  int rc
17711 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69  ;.  Pgno pgnoChi
17712 6c 64 20 3d 20 30 3b 0a 20 20 42 74 72 65 65 20  ld = 0;.  Btree 
17713 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65  *p = pCur->pBtre
17714 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e;.  BtShared *p
17715 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20  Bt = p->pBt;..  
17716 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
17717 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
17718 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
17719 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61  e->isInit );.  a
1771a 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72  ssert( pBt->inTr
1771b 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
1771c 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65  _WRITE );.  asse
1771d 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e  rt( !pBt->readOn
1771e 6c 79 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72  ly );.  if( pCur
1771f 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
17720 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65  _FAULT ){.    re
17721 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b  turn pCur->skip;
17722 0a 20 20 7d 0a 20 20 69 66 28 20 4e 45 56 45 52  .  }.  if( NEVER
17723 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75  (pCur->aiIdx[pCu
17724 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65  r->iPage]>=pPage
17725 2d 3e 6e 43 65 6c 6c 29 20 29 7b 0a 20 20 20 20  ->nCell) ){.    
17726 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
17727 52 4f 52 3b 20 20 2f 2a 20 54 68 65 20 63 75 72  ROR;  /* The cur
17728 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74  sor is not point
17729 69 6e 67 20 74 6f 20 61 6e 79 74 68 69 6e 67 20  ing to anything 
1772a 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  */.  }.  assert(
1772b 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b   pCur->wrFlag );
1772c 0a 20 20 72 63 20 3d 20 63 68 65 63 6b 46 6f 72  .  rc = checkFor
1772d 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c  ReadConflicts(p,
1772e 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
1772f 20 70 43 75 72 2c 20 70 43 75 72 2d 3e 69 6e 66   pCur, pCur->inf
17730 6f 2e 6e 4b 65 79 29 3b 0a 20 20 69 66 28 20 72  o.nKey);.  if( r
17731 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
17732 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
17733 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20   pCur points to 
17734 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  has a read lock 
17735 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  */.    assert( r
17736 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  c==SQLITE_LOCKED
17737 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a  _SHAREDCACHE );.
17738 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
17739 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65   }..  /* Restore
1773a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72   the current cur
1773b 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20  sor position (a 
1773c 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72  no-op if the cur
1773d 73 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20  sor is not in . 
1773e 20 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49   ** CURSOR_REQUI
1773f 52 45 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e  RESEEK state) an
17740 64 20 73 61 76 65 20 74 68 65 20 70 6f 73 69 74  d save the posit
17741 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65  ions of any othe
17742 72 20 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20  r cursors .  ** 
17743 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65  open on the same
17744 20 74 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c   table. Then cal
17745 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  l sqlite3PagerWr
17746 69 74 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67  ite() on the pag
17747 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20  e.  ** that the 
17748 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65  entry will be de
17749 6c 65 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f  leted from..  */
1774a 0a 20 20 69 66 28 20 0a 20 20 20 20 28 72 63 20  .  if( .    (rc 
1774b 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50  = restoreCursorP
1774c 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d  osition(pCur))!=
1774d 30 20 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73  0 ||.    (rc = s
1774e 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42  aveAllCursors(pB
1774f 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  t, pCur->pgnoRoo
17750 74 2c 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a  t, pCur))!=0 ||.
17751 20 20 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65      (rc = sqlite
17752 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
17753 65 2d 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a  e->pDbPage))!=0.
17754 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
17755 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f  rc;.  }..  /* Lo
17756 63 61 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69  cate the cell wi
17757 74 68 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e  thin its page an
17758 64 20 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f  d leave pCell po
17759 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20  inting to the.  
1775a 2a 2a 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65  ** data. The cle
1775b 61 72 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72  arCell() call fr
1775c 65 65 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77  ees any overflow
1775d 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65   pages associate
1775e 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20  d with the.  ** 
1775f 63 65 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69  cell. The cell i
17760 74 73 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69  tself is still i
17761 6e 74 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64  ntact..  */.  id
17762 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  x = pCur->aiIdx[
17763 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
17764 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
17765 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20  (pPage, idx);.  
17766 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
17767 20 29 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c   ){.    pgnoChil
17768 64 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65  d = get4byte(pCe
17769 6c 6c 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ll);.  }.  rc = 
1776a 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c  clearCell(pPage,
1776b 20 70 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72   pCell);.  if( r
1776c 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
1776d 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  rc;.  }..  if( !
1776e 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
1776f 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65     /*.    ** The
17770 20 65 6e 74 72 79 20 77 65 20 61 72 65 20 61 62   entry we are ab
17771 6f 75 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73  out to delete is
17772 20 6e 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69   not a leaf so i
17773 66 20 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20  f we do not.    
17774 2a 2a 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20  ** do something 
17775 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20  we will leave a 
17776 68 6f 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72  hole on an inter
17777 6e 61 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a  nal page..    **
17778 20 57 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c   We have to fill
17779 20 74 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76   the hole by mov
1777a 69 6e 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72  ing in a cell fr
1777b 6f 6d 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a  om a leaf.  The.
1777c 20 20 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c      ** next Cell
1777d 20 61 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74   after the one t
1777e 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 73 20  o be deleted is 
1777f 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78  guaranteed to ex
17780 69 73 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74  ist and.    ** t
17781 6f 20 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77  o be a leaf so w
17782 65 20 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20  e can use it..  
17783 20 20 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f    */.    BtCurso
17784 72 20 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d  r leafCur;.    M
17785 65 6d 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67  emPage *pLeafPag
17786 65 20 3d 20 30 3b 0a 0a 20 20 20 20 75 6e 73 69  e = 0;..    unsi
17787 67 6e 65 64 20 63 68 61 72 20 2a 70 4e 65 78 74  gned char *pNext
17788 3b 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65  ;.    int notUse
17789 64 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  d;.    unsigned 
1778a 63 68 61 72 20 2a 74 65 6d 70 43 65 6c 6c 20 3d  char *tempCell =
1778b 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   0;.    assert( 
1778c 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29  !pPage->intKey )
1778d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1778e 65 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28  eeGetTempCursor(
1778f 70 43 75 72 2c 20 26 6c 65 61 66 43 75 72 29 3b  pCur, &leafCur);
17790 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
17791 33 42 74 72 65 65 4e 65 78 74 28 26 6c 65 61 66  3BtreeNext(&leaf
17792 43 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a  Cur, &notUsed);.
17793 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
17794 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61  TE_OK ){.      a
17795 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61  ssert( leafCur.a
17796 69 49 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61  iIdx[leafCur.iPa
17797 67 65 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ge]==0 );.      
17798 70 4c 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66  pLeafPage = leaf
17799 43 75 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43  Cur.apPage[leafC
1779a 75 72 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20  ur.iPage];.     
1779b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1779c 65 72 57 72 69 74 65 28 70 4c 65 61 66 50 61 67  erWrite(pLeafPag
1779d 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
1779e 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
1779f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
177a0 20 20 69 6e 74 20 6c 65 61 66 43 75 72 73 6f 72    int leafCursor
177a1 49 6e 76 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  Invalid = 0;.   
177a2 20 20 20 75 31 36 20 73 7a 4e 65 78 74 3b 0a 20     u16 szNext;. 
177a3 20 20 20 20 20 54 52 41 43 45 28 28 22 44 45 4c       TRACE(("DEL
177a4 45 54 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65  ETE: table=%d de
177a5 6c 65 74 65 20 69 6e 74 65 72 6e 61 6c 20 66 72  lete internal fr
177a6 6f 6d 20 25 64 20 72 65 70 6c 61 63 65 20 66 72  om %d replace fr
177a7 6f 6d 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20  om leaf %d\n",. 
177a8 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67          pCur->pg
177a9 6e 6f 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70  noRoot, pPage->p
177aa 67 6e 6f 2c 20 70 4c 65 61 66 50 61 67 65 2d 3e  gno, pLeafPage->
177ab 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 64 72  pgno));.      dr
177ac 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64  opCell(pPage, id
177ad 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70  x, cellSizePtr(p
177ae 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20  Page, pCell));. 
177af 20 20 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e       pNext = fin
177b0 64 43 65 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c  dCell(pLeafPage,
177b1 20 30 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 78   0);.      szNex
177b2 74 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  t = cellSizePtr(
177b3 70 4c 65 61 66 50 61 67 65 2c 20 70 4e 65 78 74  pLeafPage, pNext
177b4 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
177b5 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42   MX_CELL_SIZE(pB
177b6 74 29 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 3b 0a  t)>=szNext+4 );.
177b7 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65        allocateTe
177b8 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20  mpSpace(pBt);.  
177b9 20 20 20 20 74 65 6d 70 43 65 6c 6c 20 3d 20 70      tempCell = p
177ba 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20  Bt->pTmpSpace;. 
177bb 20 20 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c       if( tempCel
177bc 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  l==0 ){.        
177bd 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
177be 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
177bf 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
177c0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
177c1 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50   = insertCell(pP
177c2 61 67 65 2c 20 69 64 78 2c 20 70 4e 65 78 74 2d  age, idx, pNext-
177c3 34 2c 20 73 7a 4e 65 78 74 2b 34 2c 20 74 65 6d  4, szNext+4, tem
177c4 70 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20  pCell, 0);.     
177c5 20 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68   }...      /* Th
177c6 65 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74  e "if" statement
177c7 20 69 6e 20 74 68 65 20 6e 65 78 74 20 63 6f 64   in the next cod
177c8 65 20 62 6c 6f 63 6b 20 69 73 20 63 72 69 74 69  e block is criti
177c9 63 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20  cal.  The.      
177ca 2a 2a 20 73 6c 69 67 68 74 65 73 74 20 65 72 72  ** slightest err
177cb 6f 72 20 69 6e 20 74 68 61 74 20 73 74 61 74 65  or in that state
177cc 6d 65 6e 74 20 77 6f 75 6c 64 20 61 6c 6c 6f 77  ment would allow
177cd 20 53 51 4c 69 74 65 20 74 6f 20 6f 70 65 72 61   SQLite to opera
177ce 74 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72  te.      ** corr
177cf 65 63 74 6c 79 20 6d 6f 73 74 20 6f 66 20 74 68  ectly most of th
177d0 65 20 74 69 6d 65 20 62 75 74 20 70 72 6f 64 75  e time but produ
177d1 63 65 20 76 65 72 79 20 72 61 72 65 20 66 61 69  ce very rare fai
177d2 6c 75 72 65 73 2e 20 20 54 6f 0a 20 20 20 20 20  lures.  To.     
177d3 20 2a 2a 20 67 75 61 72 64 20 61 67 61 69 6e 73   ** guard agains
177d4 74 20 74 68 69 73 2c 20 74 68 65 20 66 6f 6c 6c  t this, the foll
177d5 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 68 65 6c  owing macros hel
177d6 70 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74  p to verify that
177d7 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 22 69  .      ** the "i
177d8 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  f" statement is 
177d9 77 65 6c 6c 20 74 65 73 74 65 64 2e 0a 20 20 20  well tested..   
177da 20 20 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74     */.      test
177db 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76  case( pPage->nOv
177dc 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61  erflow==0 && pPa
177dd 67 65 2d 3e 6e 46 72 65 65 3c 70 42 74 2d 3e 75  ge->nFree<pBt->u
177de 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20  sableSize*2/3 . 
177df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
177e0 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46  && pLeafPage->nF
177e1 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70  ree+2+szNext > p
177e2 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32  Bt->usableSize*2
177e3 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  /3 );.      test
177e4 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76  case( pPage->nOv
177e5 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61  erflow==0 && pPa
177e6 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e  ge->nFree==pBt->
177e7 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a  usableSize*2/3 .
177e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
177e9 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e   && pLeafPage->n
177ea 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20  Free+2+szNext > 
177eb 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a  pBt->usableSize*
177ec 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73  2/3 );.      tes
177ed 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f  tcase( pPage->nO
177ee 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50  verflow==0 && pP
177ef 61 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d  age->nFree==pBt-
177f0 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b  >usableSize*2/3+
177f1 31 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1 .             
177f2 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65      && pLeafPage
177f3 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74  ->nFree+2+szNext
177f4 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   > pBt->usableSi
177f5 7a 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20  ze*2/3 );.      
177f6 74 65 73 74 63 61 73 65 28 20 70 50 61 67 65 2d  testcase( pPage-
177f7 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 26 26 20  >nOverflow>0 && 
177f8 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 70 42  pPage->nFree<=pB
177f9 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f  t->usableSize*2/
177fa 33 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  3.              
177fb 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d     && pLeafPage-
177fc 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20  >nFree+2+szNext 
177fd 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  > pBt->usableSiz
177fe 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74  e*2/3 );.      t
177ff 65 73 74 63 61 73 65 28 20 28 70 50 61 67 65 2d  estcase( (pPage-
17800 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20  >nOverflow>0 || 
17801 28 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20  (pPage->nFree > 
17802 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a  pBt->usableSize*
17803 32 2f 33 29 29 0a 20 20 20 20 20 20 20 20 20 20  2/3)).          
17804 20 20 20 20 20 20 20 26 26 20 70 4c 65 61 66 50         && pLeafP
17805 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e  age->nFree+2+szN
17806 65 78 74 20 3d 3d 20 70 42 74 2d 3e 75 73 61 62  ext == pBt->usab
17807 6c 65 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a  leSize*2/3 );...
17808 20 20 20 20 20 20 69 66 28 20 28 70 50 61 67 65        if( (pPage
17809 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c  ->nOverflow>0 ||
1780a 20 28 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e   (pPage->nFree >
1780b 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
1780c 2a 32 2f 33 29 29 20 26 26 0a 20 20 20 20 20 20  *2/3)) &&.      
1780d 20 20 20 20 28 70 4c 65 61 66 50 61 67 65 2d 3e      (pLeafPage->
1780e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e  nFree+2+szNext >
1780f 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
17810 2a 32 2f 33 29 0a 20 20 20 20 20 20 29 7b 0a 20  *2/3).      ){. 
17811 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62         /* This b
17812 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 69  ranch is taken i
17813 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e  f the internal n
17814 6f 64 65 20 69 73 20 6e 6f 77 20 65 69 74 68 65  ode is now eithe
17815 72 20 6f 76 65 72 66 6c 6f 77 69 6e 67 0a 20 20  r overflowing.  
17816 20 20 20 20 20 20 2a 2a 20 6f 72 20 75 6e 64 65        ** or unde
17817 72 66 75 6c 6c 20 61 6e 64 20 74 68 65 20 6c 65  rfull and the le
17818 61 66 20 6e 6f 64 65 20 77 69 6c 6c 20 62 65 20  af node will be 
17819 75 6e 64 65 72 66 75 6c 6c 20 61 66 74 65 72 20  underfull after 
1781a 74 68 65 20 6a 75 73 74 20 63 65 6c 6c 20 0a 20  the just cell . 
1781b 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64         ** copied
1781c 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   to the internal
1781d 20 6e 6f 64 65 20 69 73 20 64 65 6c 65 74 65 64   node is deleted
1781e 20 66 72 6f 6d 20 69 74 2e 20 54 68 69 73 20 69   from it. This i
1781f 73 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20  s a special.    
17820 20 20 20 20 2a 2a 20 63 61 73 65 20 62 65 63 61      ** case beca
17821 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  use the call to 
17822 62 61 6c 61 6e 63 65 28 29 20 74 6f 20 63 6f 72  balance() to cor
17823 72 65 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61  rect the interna
17824 6c 20 6e 6f 64 65 0a 20 20 20 20 20 20 20 20 2a  l node.        *
17825 2a 20 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65  * may change the
17826 20 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20   tree structure 
17827 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 74  and invalidate t
17828 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 20  he contents of. 
17829 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65         ** the le
1782a 61 66 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61  afCur.apPage[] a
1782b 6e 64 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78  nd leafCur.aiIdx
1782c 5b 5d 20 61 72 72 61 79 73 2c 20 77 68 69 63 68  [] arrays, which
1782d 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20   will be.       
1782e 20 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20   ** used by the 
1782f 62 61 6c 61 6e 63 65 28 29 20 72 65 71 75 69 72  balance() requir
17830 65 64 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68  ed to correct th
17831 65 20 75 6e 64 65 72 66 75 6c 6c 20 6c 65 61 66  e underfull leaf
17832 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65  .        ** node
17833 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
17834 20 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 72 6d       ** The form
17835 75 6c 61 20 75 73 65 64 20 69 6e 20 74 68 65 20  ula used in the 
17836 65 78 70 72 65 73 73 69 6f 6e 20 61 62 6f 76 65  expression above
17837 20 61 72 65 20 62 61 73 65 64 20 6f 6e 20 66 61   are based on fa
17838 63 65 74 73 20 6f 66 0a 20 20 20 20 20 20 20 20  cets of.        
17839 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 66 69  ** the SQLite fi
1783a 6c 65 2d 66 6f 72 6d 61 74 20 74 68 61 74 20 64  le-format that d
1783b 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65  o not change ove
1783c 72 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20  r time..        
1783d 2a 2f 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  */.        testc
1783e 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 46 72 65  ase( pPage->nFre
1783f 65 3d 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e==pBt->usableSi
17840 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 20 20  ze*2/3+1 );.    
17841 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c      testcase( pL
17842 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32  eafPage->nFree+2
17843 2b 73 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e 75 73  +szNext==pBt->us
17844 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 29  ableSize*2/3+1 )
17845 3b 0a 20 20 20 20 20 20 20 20 6c 65 61 66 43 75  ;.        leafCu
17846 72 73 6f 72 49 6e 76 61 6c 69 64 20 3d 20 31 3b  rsorInvalid = 1;
17847 0a 20 20 20 20 20 20 7d 20 20 20 20 20 20 20 20  .      }        
17848 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ..      if( rc==
17849 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1784a 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
1784b 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
1784c 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
1784d 61 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20  age) );.        
1784e 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65  put4byte(findOve
1784f 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c  rflowCell(pPage,
17850 20 69 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c 64   idx), pgnoChild
17851 29 3b 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f  );.        VVA_O
17852 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73  NLY( pCur->pages
17853 53 68 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a  Shuffled = 0 );.
17854 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c          rc = bal
17855 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20  ance(pCur, 0);. 
17856 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66       }..      if
17857 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
17858 26 26 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76  && leafCursorInv
17859 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20  alid ){.        
1785a 2f 2a 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64 65  /* The leaf-node
1785b 20 69 73 20 6e 6f 77 20 75 6e 64 65 72 66 75 6c   is now underful
1785c 6c 20 61 6e 64 20 73 6f 20 74 68 65 20 74 72 65  l and so the tre
1785d 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20  e needs to be . 
1785e 20 20 20 20 20 20 20 2a 2a 20 72 65 62 61 6c 61         ** rebala
1785f 6e 63 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74  nced. However, t
17860 68 65 20 62 61 6c 61 6e 63 65 28 29 20 6f 70 65  he balance() ope
17861 72 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e  ration on the in
17862 74 65 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a  ternal.        *
17863 2a 20 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61 79  * node above may
17864 20 68 61 76 65 20 6d 6f 64 69 66 69 65 64 20 74   have modified t
17865 68 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  he structure of 
17866 74 68 65 20 42 2d 54 72 65 65 20 61 6e 64 0a 20  the B-Tree and. 
17867 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 65         ** so the
17868 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74   current content
17869 73 20 6f 66 20 6c 65 61 66 43 75 72 2e 61 70 50  s of leafCur.apP
1786a 61 67 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43 75  age[] and leafCu
1786b 72 2e 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20  r.aiIdx[].      
1786c 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20    ** may not be 
1786d 74 72 75 73 74 65 64 2e 0a 20 20 20 20 20 20 20  trusted..       
1786e 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49   **.        ** I
1786f 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
17870 65 20 74 6f 20 63 6f 70 79 20 74 68 65 20 61 6e  e to copy the an
17871 63 65 73 74 72 79 20 66 72 6f 6d 20 70 43 75 72  cestry from pCur
17872 2c 20 61 73 20 74 68 65 20 73 61 6d 65 0a 20 20  , as the same.  
17873 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65        ** balance
17874 28 29 20 63 61 6c 6c 20 68 61 73 20 69 6e 76 61  () call has inva
17875 6c 69 64 61 74 65 64 20 74 68 65 20 70 43 75 72  lidated the pCur
17876 2d 3e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61  ->apPage[] and a
17877 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 2a  iIdx[].        *
17878 2a 20 61 72 72 61 79 73 2e 20 0a 20 20 20 20 20  * arrays. .     
17879 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
1787a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 61 76   The call to sav
1787b 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
1787c 29 20 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61 6c  ) below internal
1787d 6c 79 20 73 61 76 65 73 20 74 68 65 20 0a 20 20  ly saves the .  
1787e 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 74 68 61        ** key tha
1787f 74 20 6c 65 61 66 43 75 72 20 69 73 20 63 75 72  t leafCur is cur
17880 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
17881 74 6f 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20 74  to. Currently, t
17882 68 65 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  here.        ** 
17883 61 72 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f  are two copies o
17884 66 20 74 68 61 74 20 6b 65 79 20 69 6e 20 74 68  f that key in th
17885 65 20 74 72 65 65 20 2d 20 6f 6e 65 20 68 65 72  e tree - one her
17886 65 20 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20 20  e on the leaf.  
17887 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 61 6e        ** page an
17888 64 20 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69 6e  d one on some in
17889 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20 74  ternal node in t
1788a 68 65 20 74 72 65 65 2e 20 54 68 65 20 63 6f 70  he tree. The cop
1788b 79 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  y on.        ** 
1788c 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 69 73  the leaf node is
1788d 20 61 6c 77 61 79 73 20 74 68 65 20 6e 65 78 74   always the next
1788e 20 6b 65 79 20 69 6e 20 74 72 65 65 2d 6f 72 64   key in tree-ord
1788f 65 72 20 61 66 74 65 72 20 74 68 65 20 0a 20 20  er after the .  
17890 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 6e        ** copy on
17891 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   the internal no
17892 64 65 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c 6c  de. So, the call
17893 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
17894 4e 65 78 74 28 29 0a 20 20 20 20 20 20 20 20 2a  Next().        *
17895 2a 20 63 61 6c 6c 73 20 72 65 73 74 6f 72 65 43  * calls restoreC
17896 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20  ursorPosition() 
17897 74 6f 20 70 6f 69 6e 74 20 74 68 65 20 63 75 72  to point the cur
17898 73 6f 72 20 74 6f 20 74 68 65 20 63 6f 70 79 0a  sor to the copy.
17899 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65          ** store
1789a 64 20 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61  d on the interna
1789b 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64 76  l node, then adv
1789c 61 6e 63 65 73 20 74 6f 20 74 68 65 20 6e 65 78  ances to the nex
1789d 74 20 65 6e 74 72 79 2c 0a 20 20 20 20 20 20 20  t entry,.       
1789e 20 2a 2a 20 77 68 69 63 68 20 68 61 70 70 65 6e   ** which happen
1789f 73 20 74 6f 20 62 65 20 74 68 65 20 63 6f 70 79  s to be the copy
178a0 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 6e 20 74   of the key on t
178a1 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  he internal node
178a2 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 65 74  ..        ** Net
178a3 20 65 66 66 65 63 74 3a 20 6c 65 61 66 43 75 72   effect: leafCur
178a4 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 62 61 63   is pointing bac
178a5 6b 20 74 6f 20 74 68 65 20 64 75 70 6c 69 63 61  k to the duplica
178a6 74 65 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20  te cell.        
178a7 2a 2a 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f  ** that needs to
178a8 20 62 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e 64   be removed, and
178a9 20 74 68 65 20 6c 65 61 66 43 75 72 2e 61 70 50   the leafCur.apP
178aa 61 67 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20 20  age[] and.      
178ab 20 20 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69 49    ** leafCur.aiI
178ac 64 78 5b 5d 20 61 72 72 61 79 73 20 61 72 65 20  dx[] arrays are 
178ad 63 6f 72 72 65 63 74 2e 0a 20 20 20 20 20 20 20  correct..       
178ae 20 2a 2f 0a 20 20 20 20 20 20 20 20 56 56 41 5f   */.        VVA_
178af 4f 4e 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66 50  ONLY( Pgno leafP
178b0 67 6e 6f 20 3d 20 70 4c 65 61 66 50 61 67 65 2d  gno = pLeafPage-
178b1 3e 70 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20  >pgno );.       
178b2 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72   rc = saveCursor
178b3 50 6f 73 69 74 69 6f 6e 28 26 6c 65 61 66 43 75  Position(&leafCu
178b4 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  r);.        if( 
178b5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
178b6 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
178b7 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
178b8 28 26 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74 55  (&leafCur, &notU
178b9 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  sed);.        }.
178ba 20 20 20 20 20 20 20 20 70 4c 65 61 66 50 61 67          pLeafPag
178bb 65 20 3d 20 6c 65 61 66 43 75 72 2e 61 70 50 61  e = leafCur.apPa
178bc 67 65 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65  ge[leafCur.iPage
178bd 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ];.        asser
178be 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
178bf 20 7c 7c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70   || pLeafPage->p
178c0 67 6e 6f 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b  gno==leafPgno );
178c1 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
178c2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
178c3 7c 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b  | leafCur.aiIdx[
178c4 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3d 3d  leafCur.iPage]==
178c5 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  0 );.      }..  
178c6 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
178c7 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20 26 26 20  K==rc.       && 
178c8 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
178c9 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
178ca 74 65 28 70 4c 65 61 66 50 61 67 65 2d 3e 70 44  te(pLeafPage->pD
178cb 62 50 61 67 65 29 29 20 0a 20 20 20 20 20 20 29  bPage)) .      )
178cc 7b 0a 20 20 20 20 20 20 20 20 64 72 6f 70 43 65  {.        dropCe
178cd 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c 20 30 2c  ll(pLeafPage, 0,
178ce 20 73 7a 4e 65 78 74 29 3b 0a 20 20 20 20 20 20   szNext);.      
178cf 20 20 56 56 41 5f 4f 4e 4c 59 28 20 6c 65 61 66    VVA_ONLY( leaf
178d0 43 75 72 2e 70 61 67 65 73 53 68 75 66 66 6c 65  Cur.pagesShuffle
178d1 64 20 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 20  d = 0 );.       
178d2 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 26 6c   rc = balance(&l
178d3 65 61 66 43 75 72 2c 20 30 29 3b 0a 20 20 20 20  eafCur, 0);.    
178d4 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66      assert( leaf
178d5 43 75 72 73 6f 72 49 6e 76 61 6c 69 64 20 7c 7c  CursorInvalid ||
178d6 20 21 6c 65 61 66 43 75 72 2e 70 61 67 65 73 53   !leafCur.pagesS
178d7 68 75 66 66 6c 65 64 0a 20 20 20 20 20 20 20 20  huffled.        
178d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
178d9 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 21 70             || !p
178da 43 75 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c  Cur->pagesShuffl
178db 65 64 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ed );.      }.  
178dc 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42    }.    sqlite3B
178dd 74 72 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43  treeReleaseTempC
178de 75 72 73 6f 72 28 26 6c 65 61 66 43 75 72 29 3b  ursor(&leafCur);
178df 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 54 52  .  }else{.    TR
178e0 41 43 45 28 28 22 44 45 4c 45 54 45 3a 20 74 61  ACE(("DELETE: ta
178e1 62 6c 65 3d 25 64 20 64 65 6c 65 74 65 20 66 72  ble=%d delete fr
178e2 6f 6d 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20  om leaf %d\n",. 
178e3 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f        pCur->pgno
178e4 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e  Root, pPage->pgn
178e5 6f 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 72  o));.    rc = dr
178e6 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64  opCell(pPage, id
178e7 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70  x, cellSizePtr(p
178e8 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20  Page, pCell));. 
178e9 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
178ea 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
178eb 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c   = balance(pCur,
178ec 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   0);.    }.  }. 
178ed 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
178ee 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f  OK ){.    moveTo
178ef 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a  Root(pCur);.  }.
178f0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
178f1 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
178f2 65 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20  ew BTree table. 
178f3 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54   Write into *piT
178f4 61 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a  able the page.**
178f5 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
178f6 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65  root page of the
178f7 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   new table..**.*
178f8 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 74 79  * The type of ty
178f9 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  pe is determined
178fa 20 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61   by the flags pa
178fb 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74  rameter.  Only t
178fc 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  he.** following 
178fd 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20  values of flags 
178fe 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  are currently in
178ff 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c   use.  Other val
17900 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73  ues for.** flags
17901 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a   might not work:
17902 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45  .**.**     BTREE
17903 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45  _INTKEY|BTREE_LE
17904 41 46 44 41 54 41 20 20 20 20 20 55 73 65 64 20  AFDATA     Used 
17905 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77  for SQL tables w
17906 69 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a  ith rowid keys.*
17907 2a 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f  *     BTREE_ZERO
17908 44 41 54 41 20 20 20 20 20 20 20 20 20 20 20 20  DATA            
17909 20 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53        Used for S
1790a 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74  QL indices.*/.st
1790b 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 72  atic int btreeCr
1790c 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20  eateTable(Btree 
1790d 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65  *p, int *piTable
1790e 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
1790f 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
17910 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67  p->pBt;.  MemPag
17911 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f  e *pRoot;.  Pgno
17912 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74   pgnoRoot;.  int
17913 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
17914 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
17915 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61  sMutex(p) );.  a
17916 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72  ssert( pBt->inTr
17917 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
17918 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65  _WRITE );.  asse
17919 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e  rt( !pBt->readOn
1791a 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  ly );..#ifdef SQ
1791b 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1791c 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f  CUUM.  rc = allo
1791d 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
1791e 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f  t, &pRoot, &pgno
1791f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69  Root, 1, 0);.  i
17920 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74  f( rc ){.    ret
17921 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73  urn rc;.  }.#els
17922 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74  e.  if( pBt->aut
17923 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50  oVacuum ){.    P
17924 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20  gno pgnoMove;   
17925 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67     /* Move a pag
17926 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72  e here to make r
17927 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74  oom for the root
17928 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d  -page */.    Mem
17929 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b  Page *pPageMove;
1792a 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20   /* The page to 
1792b 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20  move to. */..   
1792c 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e   /* Creating a n
1792d 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f  ew table may pro
1792e 62 61 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f  bably require mo
1792f 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67  ving an existing
17930 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a   database.    **
17931 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f   to make room fo
17932 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73  r the new tables
17933 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63   root page. In c
17934 61 73 65 20 74 68 69 73 20 70 61 67 65 20 74 75  ase this page tu
17935 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74  rns.    ** out t
17936 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77  o be an overflow
17937 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c   page, delete al
17938 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  l overflow page-
17939 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a  map caches.    *
1793a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63  * held by open c
1793b 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20  ursors..    */. 
1793c 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c     invalidateAll
1793d 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42  OverflowCache(pB
1793e 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64  t);..    /* Read
1793f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65   the value of me
17940 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64  ta[3] from the d
17941 61 74 61 62 61 73 65 20 74 6f 20 64 65 74 65 72  atabase to deter
17942 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20  mine where the. 
17943 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20     ** root page 
17944 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
17945 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61   should go. meta
17946 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65  [3] is the large
17947 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20  st root-page.   
17948 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66   ** created so f
17949 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72  ar, so the new r
1794a 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74  oot-page is (met
1794b 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a  a[3]+1)..    */.
1794c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1794d 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20  BtreeGetMeta(p, 
1794e 34 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20  4, &pgnoRoot);. 
1794f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
17950 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
17951 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
17952 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a     pgnoRoot++;..
17953 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72      /* The new r
17954 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74  oot-page may not
17955 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e   be allocated on
17956 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70   a pointer-map p
17957 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20  age, or the.    
17958 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  ** PENDING_BYTE 
17959 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  page..    */.   
1795a 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74   while( pgnoRoot
1795b 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  ==PTRMAP_PAGENO(
1795c 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c  pBt, pgnoRoot) |
1795d 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f  |.        pgnoRo
1795e 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  ot==PENDING_BYTE
1795f 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20  _PAGE(pBt) ){.  
17960 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a      pgnoRoot++;.
17961 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
17962 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b  ( pgnoRoot>=3 );
17963 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  ..    /* Allocat
17964 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 61  e a page. The pa
17965 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c  ge that currentl
17966 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67 6e  y resides at pgn
17967 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a  oRoot will.    *
17968 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68  * be moved to th
17969 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65  e allocated page
1796a 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c   (unless the all
1796b 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70 70  ocated page happ
1796c 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65  ens.    ** to re
1796d 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74  side at pgnoRoot
1796e 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  )..    */.    rc
1796f 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
17970 50 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 65  Page(pBt, &pPage
17971 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c  Move, &pgnoMove,
17972 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20   pgnoRoot, 1);. 
17973 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
17974 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
17975 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a  turn rc;.    }..
17976 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65      if( pgnoMove
17977 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20  !=pgnoRoot ){.  
17978 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20      /* pgnoRoot 
17979 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74  is the page that
1797a 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f   will be used fo
1797b 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20  r the root-page 
1797c 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  of.      ** the 
1797d 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d  new table (assum
1797e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64  ing an error did
1797f 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74   not occur). But
17980 20 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a   we were.      *
17981 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f  * allocated pgno
17982 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 65  Move. If require
17983 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61  d (i.e. if it wa
17984 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a  s not allocated.
17985 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65        ** by exte
17986 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c  nding the file),
17987 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67   the current pag
17988 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67  e at position pg
17989 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20  noMove.      ** 
1798a 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e  is already journ
1798b 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  aled..      */. 
1798c 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20       u8 eType;. 
1798d 20 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61       Pgno iPtrPa
1798e 67 65 3b 0a 0a 20 20 20 20 20 20 72 65 6c 65 61  ge;..      relea
1798f 73 65 50 61 67 65 28 70 50 61 67 65 4d 6f 76 65  sePage(pPageMove
17990 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76  );..      /* Mov
17991 65 20 74 68 65 20 70 61 67 65 20 63 75 72 72 65  e the page curre
17992 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52 6f 6f 74  ntly at pgnoRoot
17993 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f   to pgnoMove. */
17994 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
17995 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
17996 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26  pBt, pgnoRoot, &
17997 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20  pRoot, 0);.     
17998 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17999 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
1799a 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
1799b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d  .      rc = ptrm
1799c 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52  apGet(pBt, pgnoR
1799d 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50  oot, &eType, &iP
1799e 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  trPage);.      i
1799f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
179a0 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41   || eType==PTRMA
179a1 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54  P_ROOTPAGE || eT
179a2 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45  ype==PTRMAP_FREE
179a3 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20  PAGE ){.        
179a4 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f  releasePage(pRoo
179a5 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  t);.        retu
179a6 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
179a7 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79       assert( eTy
179a8 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe!=PTRMAP_ROOTP
179a9 41 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73  AGE );.      ass
179aa 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d  ert( eType!=PTRM
179ab 41 50 5f 46 52 45 45 50 41 47 45 20 29 3b 0a 20  AP_FREEPAGE );. 
179ac 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61       rc = reloca
179ad 74 65 50 61 67 65 28 70 42 74 2c 20 70 52 6f 6f  tePage(pBt, pRoo
179ae 74 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61  t, eType, iPtrPa
179af 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29  ge, pgnoMove, 0)
179b0 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  ;.      releaseP
179b1 61 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20  age(pRoot);..   
179b2 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65     /* Obtain the
179b3 20 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f   page at pgnoRoo
179b4 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72  t */.      if( r
179b5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
179b6 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
179b7 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
179b8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
179b9 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 70  eeGetPage(pBt, p
179ba 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c  gnoRoot, &pRoot,
179bb 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
179bc 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
179bd 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
179be 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
179bf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
179c0 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70  erWrite(pRoot->p
179c1 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  DbPage);.      i
179c2 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
179c3 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65   ){.        rele
179c4 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a  asePage(pRoot);.
179c5 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
179c6 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
179c7 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f  else{.      pRoo
179c8 74 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20  t = pPageMove;. 
179c9 20 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70     } ..    /* Up
179ca 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72  date the pointer
179cb 2d 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61  -map and meta-da
179cc 74 61 20 77 69 74 68 20 74 68 65 20 6e 65 77 20  ta with the new 
179cd 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72  root-page number
179ce 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 74  . */.    rc = pt
179cf 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e  rmapPut(pBt, pgn
179d0 6f 52 6f 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f  oRoot, PTRMAP_RO
179d1 4f 54 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20  OTPAGE, 0);.    
179d2 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
179d3 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f  releasePage(pRoo
179d4 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  t);.      return
179d5 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72   rc;.    }.    r
179d6 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
179d7 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c  UpdateMeta(p, 4,
179d8 20 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20   pgnoRoot);.    
179d9 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
179da 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f  releasePage(pRoo
179db 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  t);.      return
179dc 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65   rc;.    }..  }e
179dd 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c  lse{.    rc = al
179de 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28  locateBtreePage(
179df 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67  pBt, &pRoot, &pg
179e0 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20  noRoot, 1, 0);. 
179e1 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
179e2 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69  rn rc;.  }.#endi
179e3 66 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  f.  assert( sqli
179e4 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
179e5 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61  ble(pRoot->pDbPa
179e6 67 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67  ge) );.  zeroPag
179e7 65 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c  e(pRoot, flags |
179e8 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71   PTF_LEAF);.  sq
179e9 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
179ea 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b  pRoot->pDbPage);
179eb 0a 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69  .  *piTable = (i
179ec 6e 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72  nt)pgnoRoot;.  r
179ed 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
179ee 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
179ef 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
179f0 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74  eeCreateTable(Bt
179f1 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54  ree *p, int *piT
179f2 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29  able, int flags)
179f3 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
179f4 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
179f5 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65  p);.  rc = btree
179f6 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70  CreateTable(p, p
179f7 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a  iTable, flags);.
179f8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
179f9 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
179fa 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72   rc;.}../*.** Er
179fb 61 73 65 20 74 68 65 20 67 69 76 65 6e 20 64 61  ase the given da
179fc 74 61 62 61 73 65 20 70 61 67 65 20 61 6e 64 20  tabase page and 
179fd 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e  all its children
179fe 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65  .  Return.** the
179ff 20 70 61 67 65 20 74 6f 20 74 68 65 20 66 72 65   page to the fre
17a00 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  elist..*/.static
17a01 20 69 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61   int clearDataba
17a02 73 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72  sePage(.  BtShar
17a03 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20  ed *pBt,        
17a04 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20     /* The BTree 
17a05 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
17a06 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e  e table */.  Pgn
17a07 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20  o pgno,         
17a08 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
17a09 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20  r to clear */.  
17a0a 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61 67  int freePageFlag
17a0b 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63  ,     /* Dealloc
17a0c 61 74 65 20 70 61 67 65 20 69 66 20 74 72 75 65  ate page if true
17a0d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61   */.  int *pnCha
17a0e 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65  nge.){.  MemPage
17a0f 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69   *pPage = 0;.  i
17a10 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65  nt rc;.  unsigne
17a11 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20  d char *pCell;. 
17a12 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72   int i;..  asser
17a13 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
17a14 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
17a15 29 20 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3e  ) );.  if( pgno>
17a16 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
17a17 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Bt) ){.    retur
17a18 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
17a19 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63  _BKPT;.  }..  rc
17a1a 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67   = getAndInitPag
17a1b 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50  e(pBt, pgno, &pP
17a1c 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
17a1d 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62   goto cleardatab
17a1e 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 66  asepage_out;.  f
17a1f 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d  or(i=0; i<pPage-
17a20 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  >nCell; i++){.  
17a21 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65    pCell = findCe
17a22 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20  ll(pPage, i);.  
17a23 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
17a24 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  af ){.      rc =
17a25 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61   clearDatabasePa
17a26 67 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65  ge(pBt, get4byte
17a27 28 70 43 65 6c 6c 29 2c 20 31 2c 20 70 6e 43 68  (pCell), 1, pnCh
17a28 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  ange);.      if(
17a29 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72   rc ) goto clear
17a2a 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74  databasepage_out
17a2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
17a2c 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65   clearCell(pPage
17a2d 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66  , pCell);.    if
17a2e 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61  ( rc ) goto clea
17a2f 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75  rdatabasepage_ou
17a30 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50  t;.  }.  if( !pP
17a31 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
17a32 20 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62   rc = clearDatab
17a33 61 73 65 50 61 67 65 28 70 42 74 2c 20 67 65 74  asePage(pBt, get
17a34 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  4byte(&pPage->aD
17a35 61 74 61 5b 38 5d 29 2c 20 31 2c 20 70 6e 43 68  ata[8]), 1, pnCh
17a36 61 6e 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  ange);.    if( r
17a37 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61  c ) goto clearda
17a38 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a  tabasepage_out;.
17a39 20 20 7d 65 6c 73 65 20 69 66 28 20 70 6e 43 68    }else if( pnCh
17a3a 61 6e 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65  ange ){.    asse
17a3b 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  rt( pPage->intKe
17a3c 79 20 29 3b 0a 20 20 20 20 2a 70 6e 43 68 61 6e  y );.    *pnChan
17a3d 67 65 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65  ge += pPage->nCe
17a3e 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 72  ll;.  }.  if( fr
17a3f 65 65 50 61 67 65 46 6c 61 67 20 29 7b 0a 20 20  eePageFlag ){.  
17a40 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28    rc = freePage(
17a41 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 20  pPage);.  }else 
17a42 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65  if( (rc = sqlite
17a43 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
17a44 65 2d 3e 70 44 62 50 61 67 65 29 29 3d 3d 30 20  e->pDbPage))==0 
17a45 29 7b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28  ){.    zeroPage(
17a46 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 61 44  pPage, pPage->aD
17a47 61 74 61 5b 30 5d 20 7c 20 50 54 46 5f 4c 45 41  ata[0] | PTF_LEA
17a48 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61 72 64 61  F);.  }..clearda
17a49 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3a 0a  tabasepage_out:.
17a4a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
17a4b 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  age);.  return r
17a4c 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  c;.}../*.** Dele
17a4d 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69  te all informati
17a4e 6f 6e 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65  on from a single
17a4f 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61   table in the da
17a50 74 61 62 61 73 65 2e 20 20 69 54 61 62 6c 65 20  tabase.  iTable 
17a51 69 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e  is.** the page n
17a52 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 6f 6f  umber of the roo
17a53 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20  t of the table. 
17a54 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74   After this rout
17a55 69 6e 65 20 72 65 74 75 72 6e 73 2c 0a 2a 2a 20  ine returns,.** 
17a56 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73  the root page is
17a57 20 65 6d 70 74 79 2c 20 62 75 74 20 73 74 69 6c   empty, but stil
17a58 6c 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20  l exists..**.** 
17a59 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
17a5a 6c 20 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49  l fail with SQLI
17a5b 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65  TE_LOCKED if the
17a5c 72 65 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a  re are any open.
17a5d 2a 2a 20 72 65 61 64 20 63 75 72 73 6f 72 73 20  ** read cursors 
17a5e 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 4f  on the table.  O
17a5f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72  pen write cursor
17a60 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74  s are moved to t
17a61 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66 20 74 68  he.** root of th
17a62 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  e table..**.** I
17a63 66 20 70 6e 43 68 61 6e 67 65 20 69 73 20 6e 6f  f pnChange is no
17a64 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 61 62  t NULL, then tab
17a65 6c 65 20 69 54 61 62 6c 65 20 6d 75 73 74 20 62  le iTable must b
17a66 65 20 61 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c  e an intkey tabl
17a67 65 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65  e. The.** intege
17a68 72 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 20  r value pointed 
17a69 74 6f 20 62 79 20 70 6e 43 68 61 6e 67 65 20 69  to by pnChange i
17a6a 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79  s incremented by
17a6b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
17a6c 2a 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  * entries in the
17a6d 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
17a6e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
17a6f 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54  lite3BtreeClearT
17a70 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69  able(Btree *p, i
17a71 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a  nt iTable, int *
17a72 70 6e 43 68 61 6e 67 65 29 7b 0a 20 20 69 6e 74  pnChange){.  int
17a73 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20   rc;.  BtShared 
17a74 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
17a75 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
17a76 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28  er(p);.  assert(
17a77 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
17a78 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66  NS_WRITE );.  if
17a79 28 20 28 72 63 20 3d 20 63 68 65 63 6b 46 6f 72  ( (rc = checkFor
17a7a 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c  ReadConflicts(p,
17a7b 20 69 54 61 62 6c 65 2c 20 30 2c 20 31 29 29 21   iTable, 0, 1))!
17a7c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17a7d 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20    /* nothing to 
17a7e 64 6f 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66  do */.  }else if
17a7f 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
17a80 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72   = saveAllCursor
17a81 73 28 70 42 74 2c 20 69 54 61 62 6c 65 2c 20 30  s(pBt, iTable, 0
17a82 29 29 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 6f 74  )) ){.    /* not
17a83 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20  hing to do */.  
17a84 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
17a85 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67  clearDatabasePag
17a86 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61  e(pBt, (Pgno)iTa
17a87 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65  ble, 0, pnChange
17a88 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
17a89 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
17a8a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
17a8b 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69  *.** Erase all i
17a8c 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20  nformation in a 
17a8d 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68  table and add th
17a8e 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61  e root of the ta
17a8f 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72  ble to.** the fr
17a90 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c  eelist.  Except,
17a91 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
17a92 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65   principle table
17a93 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20   (the one on.** 
17a94 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72  page 1) is never
17a95 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72   added to the fr
17a96 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  eelist..**.** Th
17a97 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
17a98 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45  fail with SQLITE
17a99 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65  _LOCKED if there
17a9a 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a   are any open.**
17a9b 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20   cursors on the 
17a9c 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  table..**.** If 
17a9d 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e  AUTOVACUUM is en
17a9e 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61  abled and the pa
17a9f 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73 20  ge at iTable is 
17aa0 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20  not the last.** 
17aa1 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65  root page in the
17aa2 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
17aa3 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f  then the last ro
17aa4 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74  ot page .** in t
17aa5 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
17aa6 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74   is moved into t
17aa7 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79  he slot formerly
17aa8 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20   occupied by.** 
17aa9 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20  iTable and that 
17aaa 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72  last slot former
17aab 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20 74  ly occupied by t
17aac 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67  he last root pag
17aad 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f  e.** is added to
17aae 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e   the freelist in
17aaf 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e  stead of iTable.
17ab0 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61    In this say, a
17ab1 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73  ll.** root pages
17ab2 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68 65   are kept at the
17ab3 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
17ab4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
17ab5 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63   which.** is nec
17ab6 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56  essary for AUTOV
17ab7 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69  ACUUM to work ri
17ab8 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69  ght.  *piMoved i
17ab9 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a  s set to the .**
17aba 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61   page number tha
17abb 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65  t used to be the
17abc 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20   last root page 
17abd 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f  in the file befo
17abe 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20  re.** the move. 
17abf 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73   If no page gets
17ac0 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64   moved, *piMoved
17ac1 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a   is set to 0..**
17ac2 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70   The last root p
17ac3 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64 20  age is recorded 
17ac4 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74  in meta[3] and t
17ac5 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d  he value of.** m
17ac6 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65  eta[3] is update
17ac7 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 64  d by this proced
17ac8 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
17ac9 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c  nt btreeDropTabl
17aca 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f  e(Btree *p, Pgno
17acb 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69   iTable, int *pi
17acc 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63  Moved){.  int rc
17acd 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
17ace 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72  ge = 0;.  BtShar
17acf 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
17ad0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
17ad1 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
17ad2 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
17ad3 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  rt( p->inTrans==
17ad4 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a  TRANS_WRITE );..
17ad5 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67    /* It is illeg
17ad6 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62  al to drop a tab
17ad7 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72  le if any cursor
17ad8 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68  s are open on th
17ad9 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e  e.  ** database.
17ada 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
17adb 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   in auto-vacuum 
17adc 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64  mode the backend
17add 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74   may.  ** need t
17ade 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72  o move another r
17adf 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c  oot-page to fill
17ae0 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74   a gap left by t
17ae1 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20  he deleted.  ** 
17ae2 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e  root page. If an
17ae3 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73   open cursor was
17ae4 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65   using this page
17ae5 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64   a problem would
17ae6 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20   .  ** occur..  
17ae7 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 43  */.  if( pBt->pC
17ae8 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c  ursor ){.    sql
17ae9 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c  ite3ConnectionBl
17aea 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74  ocked(p->db, pBt
17aeb 2d 3e 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65  ->pCursor->pBtre
17aec 65 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75  e->db);.    retu
17aed 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
17aee 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20  _SHAREDCACHE;.  
17aef 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  }..  rc = sqlite
17af0 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
17af1 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c  t, (Pgno)iTable,
17af2 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69   &pPage, 0);.  i
17af3 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
17af4 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  c;.  rc = sqlite
17af5 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65  3BtreeClearTable
17af6 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a  (p, iTable, 0);.
17af7 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
17af8 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
17af9 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  e);.    return r
17afa 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 4d 6f 76  c;.  }..  *piMov
17afb 65 64 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 69  ed = 0;..  if( i
17afc 54 61 62 6c 65 3e 31 20 29 7b 0a 23 69 66 64 65  Table>1 ){.#ifde
17afd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
17afe 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 72 63 20  TOVACUUM.    rc 
17aff 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67 65  = freePage(pPage
17b00 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  );.    releasePa
17b01 67 65 28 70 50 61 67 65 29 3b 0a 23 65 6c 73 65  ge(pPage);.#else
17b02 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75  .    if( pBt->au
17b03 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
17b04 20 20 50 67 6e 6f 20 6d 61 78 52 6f 6f 74 50 67    Pgno maxRootPg
17b05 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  no;.      rc = s
17b06 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
17b07 74 61 28 70 2c 20 34 2c 20 26 6d 61 78 52 6f 6f  ta(p, 4, &maxRoo
17b08 74 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66  tPgno);.      if
17b09 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
17b0a 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  ){.        relea
17b0b 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
17b0c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
17b0d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
17b0e 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 6d 61 78   if( iTable==max
17b0f 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a 20 20 20 20  RootPgno ){.    
17b10 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61      /* If the ta
17b11 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
17b12 64 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 77  d is the table w
17b13 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20  ith the largest 
17b14 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20  root-page.      
17b15 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74    ** number in t
17b16 68 65 20 64 61 74 61 62 61 73 65 2c 20 70 75 74  he database, put
17b17 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
17b18 6e 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e  n the free list.
17b19 20 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20   .        */.   
17b1a 20 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61       rc = freePa
17b1b 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ge(pPage);.     
17b1c 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
17b1d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69  Page);.        i
17b1e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
17b1f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
17b20 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
17b21 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
17b22 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
17b23 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70  able being dropp
17b24 65 64 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  ed does not have
17b25 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f   the largest roo
17b26 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a  t-page.        *
17b27 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20  * number in the 
17b28 64 61 74 61 62 61 73 65 2e 20 53 6f 20 6d 6f 76  database. So mov
17b29 65 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  e the page that 
17b2a 64 6f 65 73 20 69 6e 74 6f 20 74 68 65 20 0a 20  does into the . 
17b2b 20 20 20 20 20 20 20 2a 2a 20 67 61 70 20 6c 65         ** gap le
17b2c 66 74 20 62 79 20 74 68 65 20 64 65 6c 65 74 65  ft by the delete
17b2d 64 20 72 6f 6f 74 2d 70 61 67 65 2e 0a 20 20 20  d root-page..   
17b2e 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
17b2f 4d 65 6d 50 61 67 65 20 2a 70 4d 6f 76 65 3b 0a  MemPage *pMove;.
17b30 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
17b31 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
17b32 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17b33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
17b34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26  , maxRootPgno, &
17b35 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20  pMove, 0);.     
17b36 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
17b37 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
17b38 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
17b39 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72       }.        r
17b3a 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65  c = relocatePage
17b3b 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52  (pBt, pMove, PTR
17b3c 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c  MAP_ROOTPAGE, 0,
17b3d 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20   iTable, 0);.   
17b3e 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
17b3f 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20  (pMove);.       
17b40 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17b41 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
17b42 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
17b43 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20     }.        rc 
17b44 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
17b45 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f  tPage(pBt, maxRo
17b46 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20  otPgno, &pMove, 
17b47 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
17b48 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
17b49 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
17b4a 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
17b4b 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 72 65          rc = fre
17b4c 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20  ePage(pMove);.  
17b4d 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
17b4e 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20  e(pMove);.      
17b4f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
17b50 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
17b51 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
17b52 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70      }.        *p
17b53 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74  iMoved = maxRoot
17b54 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  Pgno;.      }.. 
17b55 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20       /* Set the 
17b56 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61  new 'max-root-pa
17b57 67 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65  ge' value in the
17b58 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72   database header
17b59 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20  . This.      ** 
17b5a 69 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65  is the old value
17b5b 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20   less one, less 
17b5c 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74  one more if that
17b5d 20 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20   happens to.    
17b5e 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70    ** be a root-p
17b5f 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73  age number, less
17b60 20 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68   one again if th
17b61 61 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20  at is the.      
17b62 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  ** PENDING_BYTE_
17b63 50 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  PAGE..      */. 
17b64 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f       maxRootPgno
17b65 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 61  --;.      if( ma
17b66 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e 44 49  xRootPgno==PENDI
17b67 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
17b68 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 78  ) ){.        max
17b69 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20  RootPgno--;.    
17b6a 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6d 61    }.      if( ma
17b6b 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 54 52 4d 41  xRootPgno==PTRMA
17b6c 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6d 61  P_PAGENO(pBt, ma
17b6d 78 52 6f 6f 74 50 67 6e 6f 29 20 29 7b 0a 20 20  xRootPgno) ){.  
17b6e 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e        maxRootPgn
17b6f 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  o--;.      }.   
17b70 20 20 20 61 73 73 65 72 74 28 20 6d 61 78 52 6f     assert( maxRo
17b71 6f 74 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f  otPgno!=PENDING_
17b72 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
17b73 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ;..      rc = sq
17b74 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
17b75 4d 65 74 61 28 70 2c 20 34 2c 20 6d 61 78 52 6f  Meta(p, 4, maxRo
17b76 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c  otPgno);.    }el
17b77 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66  se{.      rc = f
17b78 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  reePage(pPage);.
17b79 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
17b7a 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  e(pPage);.    }.
17b7b 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a  #endif.  }else{.
17b7c 20 20 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65      /* If sqlite
17b7d 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 20  3BtreeDropTable 
17b7e 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 61  was called on pa
17b7f 67 65 20 31 2e 20 2a 2f 0a 20 20 20 20 7a 65 72  ge 1. */.    zer
17b80 6f 50 61 67 65 28 70 50 61 67 65 2c 20 50 54 46  oPage(pPage, PTF
17b81 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46  _INTKEY|PTF_LEAF
17b82 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50   );.    releaseP
17b83 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a  age(pPage);.  }.
17b84 20 20 72 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d    return rc;  .}
17b85 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
17b86 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
17b87 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20  DropTable(Btree 
17b88 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  *p, int iTable, 
17b89 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20  int *piMoved){. 
17b8a 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
17b8b 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
17b8c 0a 20 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f  .  rc = btreeDro
17b8d 70 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65  pTable(p, iTable
17b8e 2c 20 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71  , piMoved);.  sq
17b8f 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
17b90 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
17b91 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  .}.../*.** Read 
17b92 74 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61  the meta-informa
17b93 74 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61  tion out of a da
17b94 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65  tabase file.  Me
17b95 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20  ta[0].** is the 
17b96 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70  number of free p
17b97 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69  ages currently i
17b98 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
17b99 20 4d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f   Meta[1].** thro
17b9a 75 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65  ugh meta[15] are
17b9b 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75   available for u
17b9c 73 65 20 62 79 20 68 69 67 68 65 72 20 6c 61 79  se by higher lay
17b9d 65 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a  ers.  Meta[0].**
17b9e 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74   is read-only, t
17b9f 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 72 65  he others are re
17ba0 61 64 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a  ad/write..** .**
17ba1 20 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65   The schema laye
17ba2 72 20 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76  r numbers meta v
17ba3 61 6c 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c  alues differentl
17ba4 79 2e 20 20 41 74 20 74 68 65 20 73 63 68 65 6d  y.  At the schem
17ba5 61 0a 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20  a.** layer (and 
17ba6 74 68 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e  the SetCookie an
17ba7 64 20 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63  d ReadCookie opc
17ba8 6f 64 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72  odes) the number
17ba9 20 6f 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65   of.** free page
17baa 73 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65  s is not visible
17bab 2e 20 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20  .  So Cookie[0] 
17bac 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d  is the same as M
17bad 65 74 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54  eta[1]..*/.SQLIT
17bae 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
17baf 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
17bb0 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  a(Btree *p, int 
17bb1 69 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29  idx, u32 *pMeta)
17bb2 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50  {.  DbPage *pDbP
17bb3 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  age = 0;.  int r
17bb4 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  c;.  unsigned ch
17bb5 61 72 20 2a 70 50 31 3b 0a 20 20 42 74 53 68 61  ar *pP1;.  BtSha
17bb6 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
17bb7 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72  t;..  sqlite3Btr
17bb8 65 65 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 2f  eeEnter(p);..  /
17bb9 2a 20 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61  * Reading a meta
17bba 2d 64 61 74 61 20 76 61 6c 75 65 20 72 65 71 75  -data value requ
17bbb 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b  ires a read-lock
17bbc 20 6f 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20   on page 1 (and 
17bbd 68 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73  hence.  ** the s
17bbe 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
17bbf 6c 65 2e 20 57 65 20 67 72 61 62 20 74 68 69 73  le. We grab this
17bc0 20 6c 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73   lock regardless
17bc1 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20   of whether or. 
17bc2 20 2a 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49   ** not the SQLI
17bc3 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
17bc4 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 28  ed flag is set (
17bc5 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64  the table rooted
17bc6 20 61 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20   at page.  ** 1 
17bc7 69 73 20 74 72 65 61 74 65 64 20 61 73 20 61 20  is treated as a 
17bc8 73 70 65 63 69 61 6c 20 63 61 73 65 20 62 79 20  special case by 
17bc9 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65  querySharedCache
17bca 54 61 62 6c 65 4c 6f 63 6b 28 29 0a 20 20 2a 2a  TableLock().  **
17bcb 20 61 6e 64 20 73 65 74 53 68 61 72 65 64 43 61   and setSharedCa
17bcc 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e  cheTableLock()).
17bcd 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65  .  */.  rc = que
17bce 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
17bcf 6c 65 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 41  leLock(p, 1, REA
17bd0 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72  D_LOCK);.  if( r
17bd1 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
17bd2 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
17bd3 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65  Leave(p);.    re
17bd4 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
17bd5 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26  assert( idx>=0 &
17bd6 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 69  & idx<=15 );.  i
17bd7 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29  f( pBt->pPage1 )
17bd8 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74  {.    /* The b-t
17bd9 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 68  ree is already h
17bda 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e  olding a referen
17bdb 63 65 20 74 6f 20 70 61 67 65 20 31 20 6f 66 20  ce to page 1 of 
17bdc 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20  the database.   
17bdd 20 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69   ** file. In thi
17bde 73 20 63 61 73 65 20 74 68 65 20 72 65 71 75 69  s case the requi
17bdf 72 65 64 20 6d 65 74 61 2d 64 61 74 61 20 76 61  red meta-data va
17be0 6c 75 65 20 63 61 6e 20 62 65 20 72 65 61 64 20  lue can be read 
17be1 64 69 72 65 63 74 6c 79 0a 20 20 20 20 2a 2a 20  directly.    ** 
17be2 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 64 61  from the page da
17be3 74 61 20 6f 66 20 74 68 69 73 20 72 65 66 65 72  ta of this refer
17be4 65 6e 63 65 2e 20 54 68 69 73 20 69 73 20 73 6c  ence. This is sl
17be5 69 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68  ightly faster th
17be6 61 6e 0a 20 20 20 20 2a 2a 20 72 65 71 75 65 73  an.    ** reques
17be7 74 69 6e 67 20 61 20 6e 65 77 20 72 65 66 65 72  ting a new refer
17be8 65 6e 63 65 20 66 72 6f 6d 20 74 68 65 20 70 61  ence from the pa
17be9 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 20 20 2a  ger layer..    *
17bea 2f 0a 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73  /.    pP1 = (uns
17beb 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 42 74  igned char *)pBt
17bec 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b  ->pPage1->aData;
17bed 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
17bee 20 54 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73   The b-tree does
17bef 20 6e 6f 74 20 68 61 76 65 20 61 20 72 65 66 65   not have a refe
17bf0 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20  rence to page 1 
17bf1 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
17bf2 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 20 4f 62 74  file..    ** Obt
17bf3 61 69 6e 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65  ain one from the
17bf4 20 70 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20   pager layer..  
17bf5 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
17bf6 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42  lite3PagerGet(pB
17bf7 74 2d 3e 70 50 61 67 65 72 2c 20 31 2c 20 26 70  t->pPager, 1, &p
17bf8 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  DbPage);.    if(
17bf9 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c   rc ){.      sql
17bfa 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
17bfb 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
17bfc 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  rc;.    }.    pP
17bfd 31 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  1 = (unsigned ch
17bfe 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  ar *)sqlite3Page
17bff 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65  rGetData(pDbPage
17c00 29 3b 0a 20 20 7d 0a 20 20 2a 70 4d 65 74 61 20  );.  }.  *pMeta 
17c01 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 31 5b  = get4byte(&pP1[
17c02 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20  36 + idx*4]);.. 
17c03 20 2f 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65   /* If the b-tre
17c04 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64 69 6e 67  e is not holding
17c05 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
17c06 70 61 67 65 20 31 2c 20 74 68 65 6e 20 6f 6e 65  page 1, then one
17c07 20 77 61 73 20 0a 20 20 2a 2a 20 72 65 71 75 65   was .  ** reque
17c08 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61  sted from the pa
17c09 67 65 72 20 6c 61 79 65 72 20 69 6e 20 74 68 65  ger layer in the
17c0a 20 61 62 6f 76 65 20 62 6c 6f 63 6b 2e 20 52 65   above block. Re
17c0b 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20  lease it now..  
17c0c 2a 2f 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70  */.  if( !pBt->p
17c0d 50 61 67 65 31 20 29 7b 0a 20 20 20 20 73 71 6c  Page1 ){.    sql
17c0e 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
17c0f 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20  DbPage);.  }..  
17c10 2f 2a 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d  /* If autovacuum
17c11 65 64 20 69 73 20 64 69 73 61 62 6c 65 64 20 69  ed is disabled i
17c12 6e 20 74 68 69 73 20 62 75 69 6c 64 20 62 75 74  n this build but
17c13 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74   we are trying t
17c14 6f 20 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61  o .  ** access a
17c15 6e 20 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64  n autovacuumed d
17c16 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61  atabase, then ma
17c17 6b 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ke the database 
17c18 72 65 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a  readonly. .  */.
17c19 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
17c1a 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
17c1b 69 66 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70  if( idx==4 && *p
17c1c 4d 65 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65  Meta>0 ) pBt->re
17c1d 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64  adOnly = 1;.#end
17c1e 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  if..  /* If ther
17c1f 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  e is currently a
17c20 6e 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69  n open transacti
17c21 6f 6e 2c 20 67 72 61 62 20 61 20 72 65 61 64 2d  on, grab a read-
17c22 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f 6e 20 70 61  lock .  ** on pa
17c23 67 65 20 31 20 6f 66 20 74 68 65 20 64 61 74 61  ge 1 of the data
17c24 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20  base file. This 
17c25 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20  is done to make 
17c26 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a 20 6e  sure that.  ** n
17c27 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  o other connecti
17c28 6f 6e 20 63 61 6e 20 6d 6f 64 69 66 79 20 74 68  on can modify th
17c29 65 20 6d 65 74 61 20 76 61 6c 75 65 20 6a 75 73  e meta value jus
17c2a 74 20 72 65 61 64 20 66 72 6f 6d 0a 20 20 2a 2a  t read from.  **
17c2b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 75 6e   the database un
17c2c 74 69 6c 20 74 68 65 20 74 72 61 6e 73 61 63 74  til the transact
17c2d 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64  ion is concluded
17c2e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
17c2f 69 6e 54 72 61 6e 73 3e 30 20 29 7b 0a 20 20 20  inTrans>0 ){.   
17c30 20 72 63 20 3d 20 73 65 74 53 68 61 72 65 64 43   rc = setSharedC
17c31 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  acheTableLock(p,
17c32 20 31 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a   1, READ_LOCK);.
17c33 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
17c34 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
17c35 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
17c36 2a 20 57 72 69 74 65 20 6d 65 74 61 2d 69 6e 66  * Write meta-inf
17c37 6f 72 6d 61 74 69 6f 6e 20 62 61 63 6b 20 69 6e  ormation back in
17c38 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
17c39 20 20 4d 65 74 61 5b 30 5d 20 69 73 0a 2a 2a 20    Meta[0] is.** 
17c3a 72 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 6d 61  read-only and ma
17c3b 79 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e  y not be written
17c3c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17c3d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
17c3e 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 42  treeUpdateMeta(B
17c3f 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78  tree *p, int idx
17c40 2c 20 75 33 32 20 69 4d 65 74 61 29 7b 0a 20 20  , u32 iMeta){.  
17c41 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
17c42 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e  p->pBt;.  unsign
17c43 65 64 20 63 68 61 72 20 2a 70 50 31 3b 0a 20 20  ed char *pP1;.  
17c44 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
17c45 28 20 69 64 78 3e 3d 31 20 26 26 20 69 64 78 3c  ( idx>=1 && idx<
17c46 3d 31 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  =15 );.  sqlite3
17c47 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
17c48 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
17c49 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
17c4a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   );.  assert( pB
17c4b 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 3b 0a  t->pPage1!=0 );.
17c4c 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61    pP1 = pBt->pPa
17c4d 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63  ge1->aData;.  rc
17c4e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
17c4f 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31  rite(pBt->pPage1
17c50 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66  ->pDbPage);.  if
17c51 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
17c52 29 7b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28  ){.    put4byte(
17c53 26 70 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d  &pP1[36 + idx*4]
17c54 2c 20 69 4d 65 74 61 29 3b 0a 23 69 66 6e 64 65  , iMeta);.#ifnde
17c55 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
17c56 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
17c57 20 69 64 78 3d 3d 37 20 29 7b 0a 20 20 20 20 20   idx==7 ){.     
17c58 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75   assert( pBt->au
17c59 74 6f 56 61 63 75 75 6d 20 7c 7c 20 69 4d 65 74  toVacuum || iMet
17c5a 61 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73  a==0 );.      as
17c5b 73 65 72 74 28 20 69 4d 65 74 61 3d 3d 30 20 7c  sert( iMeta==0 |
17c5c 7c 20 69 4d 65 74 61 3d 3d 31 20 29 3b 0a 20 20  | iMeta==1 );.  
17c5d 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63      pBt->incrVac
17c5e 75 75 6d 20 3d 20 28 75 38 29 69 4d 65 74 61 3b  uum = (u8)iMeta;
17c5f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
17c60 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
17c61 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
17c62 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
17c63 52 65 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20  Return the flag 
17c64 62 79 74 65 20 61 74 20 74 68 65 20 62 65 67 69  byte at the begi
17c65 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67  nning of the pag
17c66 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  e that the curso
17c67 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c  r.** is currentl
17c68 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a  y pointing to..*
17c69 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17c6a 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
17c6b 65 46 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20  eFlags(BtCursor 
17c6c 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44  *pCur){.  /* TOD
17c6d 4f 3a 20 57 68 61 74 20 61 62 6f 75 74 20 43 55  O: What about CU
17c6e 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
17c6f 20 73 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79   state? Probably
17c70 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20   need to call.  
17c71 2a 2a 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72  ** restoreCursor
17c72 50 6f 73 69 74 69 6f 6e 28 29 20 68 65 72 65 2e  Position() here.
17c73 0a 20 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  .  */.  MemPage 
17c74 2a 70 50 61 67 65 3b 0a 20 20 72 65 73 74 6f 72  *pPage;.  restor
17c75 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
17c76 70 43 75 72 29 3b 0a 20 20 70 50 61 67 65 20 3d  pCur);.  pPage =
17c77 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
17c78 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73  ur->iPage];.  as
17c79 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
17c7a 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
17c7b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 21    assert( pPage!
17c7c 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
17c7d 70 50 61 67 65 2d 3e 70 42 74 3d 3d 70 43 75 72  pPage->pBt==pCur
17c7e 2d 3e 70 42 74 20 29 3b 0a 20 20 72 65 74 75 72  ->pBt );.  retur
17c7f 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  n pPage->aData[p
17c80 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 5d  Page->hdrOffset]
17c81 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
17c82 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f  ITE_OMIT_BTREECO
17c83 55 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69  UNT./*.** The fi
17c84 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43  rst argument, pC
17c85 75 72 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20  ur, is a cursor 
17c86 6f 70 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62  opened on some b
17c87 2d 74 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65  -tree. Count the
17c88 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e  .** number of en
17c89 74 72 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74  tries in the b-t
17c8a 72 65 65 20 61 6e 64 20 77 72 69 74 65 20 74 68  ree and write th
17c8b 65 20 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45  e result to *pnE
17c8c 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  ntry..**.** SQLI
17c8d 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
17c8e 64 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69  d if the operati
17c8f 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  on is successful
17c90 6c 79 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a  ly executed. .**
17c91 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61   Otherwise, if a
17c92 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  n error is encou
17c93 6e 74 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20  ntered (i.e. an 
17c94 49 4f 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61  IO error or data
17c95 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69  base.** corrupti
17c96 6f 6e 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72  on) an SQLite er
17c97 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
17c98 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
17c99 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
17c9a 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74  te3BtreeCount(Bt
17c9b 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36  Cursor *pCur, i6
17c9c 34 20 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69  4 *pnEntry){.  i
17c9d 36 34 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20  64 nEntry = 0;  
17c9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c9f 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
17ca0 72 65 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74  return in *pnEnt
17ca1 72 79 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  ry */.  int rc; 
17ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17ca3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17ca4 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
17ca5 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74   rc = moveToRoot
17ca6 28 70 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e  (pCur);..  /* Un
17ca7 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63  less an error oc
17ca8 63 75 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  curs, the follow
17ca9 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e  ing loop runs on
17caa 65 20 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20  e iteration for 
17cab 65 61 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69  each.  ** page i
17cac 6e 20 74 68 65 20 42 2d 54 72 65 65 20 73 74 72  n the B-Tree str
17cad 75 63 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c  ucture (not incl
17cae 75 64 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70  uding overflow p
17caf 61 67 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77  ages). .  */.  w
17cb0 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
17cb1 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  _OK ){.    int i
17cb2 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
17cb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17cb4 20 49 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20   Index of child 
17cb5 6e 6f 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a  node in parent *
17cb6 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  /.    MemPage *p
17cb7 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
17cb8 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
17cb9 65 6e 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  ent page of the 
17cba 62 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f  b-tree */..    /
17cbb 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6c  * If this is a l
17cbc 65 61 66 20 70 61 67 65 20 6f 72 20 74 68 65 20  eaf page or the 
17cbd 74 72 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69  tree is not an i
17cbe 6e 74 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65  nt-key tree, the
17cbf 6e 20 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70  n .    ** this p
17cc0 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75  age contains cou
17cc1 6e 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20  ntable entries. 
17cc2 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e  Increment the en
17cc3 74 72 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20  try counter.    
17cc4 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a  ** accordingly..
17cc5 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65      */.    pPage
17cc6 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
17cc7 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
17cc8 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61    if( pPage->lea
17cc9 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74  f || !pPage->int
17cca 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e  Key ){.      nEn
17ccb 74 72 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43  try += pPage->nC
17ccc 65 6c 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ell;.    }..    
17ccd 2f 2a 20 70 50 61 67 65 20 69 73 20 61 20 6c 65  /* pPage is a le
17cce 61 66 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f  af node. This lo
17ccf 6f 70 20 6e 61 76 69 67 61 74 65 73 20 74 68 65  op navigates the
17cd0 20 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20   cursor so that 
17cd1 69 74 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74  it .    ** point
17cd2 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69  s to the first i
17cd3 6e 74 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61  nterior cell tha
17cd4 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  t it points to t
17cd5 68 65 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20  he parent of.   
17cd6 20 2a 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67   ** the next pag
17cd7 65 20 69 6e 20 74 68 65 20 74 72 65 65 20 74 68  e in the tree th
17cd8 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  at has not yet b
17cd9 65 65 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65  een visited. The
17cda 0a 20 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69  .    ** pCur->ai
17cdb 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
17cdc 20 76 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f   value is set to
17cdd 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
17cde 65 20 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20  e parent cell.  
17cdf 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65    ** of the page
17ce0 2c 20 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62  , or to the numb
17ce1 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74  er of cells in t
17ce2 68 65 20 70 61 67 65 20 69 66 20 74 68 65 20 6e  he page if the n
17ce3 65 78 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20  ext page.    ** 
17ce4 74 6f 20 76 69 73 69 74 20 69 73 20 74 68 65 20  to visit is the 
17ce5 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69  right-child of i
17ce6 74 73 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a  ts parent..    *
17ce7 2a 0a 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20  *.    ** If all 
17ce8 70 61 67 65 73 20 69 6e 20 74 68 65 20 74 72 65  pages in the tre
17ce9 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 69  e have been visi
17cea 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ted, return SQLI
17ceb 54 45 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20  TE_OK to the.   
17cec 20 2a 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20   ** caller..    
17ced 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  */.    if( pPage
17cee 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
17cef 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28  do {.        if(
17cf0 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20   pCur->iPage==0 
17cf1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
17cf2 41 6c 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65  All pages of the
17cf3 20 62 2d 74 72 65 65 20 68 61 76 65 20 62 65 65   b-tree have bee
17cf4 6e 20 76 69 73 69 74 65 64 2e 20 52 65 74 75 72  n visited. Retur
17cf5 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20  n successfully. 
17cf6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e  */.          *pn
17cf7 45 6e 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a  Entry = nEntry;.
17cf8 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
17cf9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
17cfa 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
17cfb 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f  lite3BtreeMoveTo
17cfc 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20  Parent(pCur);.  
17cfd 20 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75      }while ( pCu
17cfe 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
17cff 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50  Page]>=pCur->apP
17d00 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
17d01 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20  ->nCell );..    
17d02 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43    pCur->aiIdx[pC
17d03 75 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20  ur->iPage]++;.  
17d04 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72      pPage = pCur
17d05 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
17d06 50 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20  Page];.    }..  
17d07 20 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20    /* Descend to 
17d08 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f  the child node o
17d09 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20  f the cell that 
17d0a 74 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65  the cursor curre
17d0b 6e 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69  ntly .    ** poi
17d0c 6e 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20  nts at. This is 
17d0d 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20  the right-child 
17d0e 69 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d  if (iIdx==pPage-
17d0f 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a  >nCell)..    */.
17d10 20 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d      iIdx = pCur-
17d11 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
17d12 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64  ge];.    if( iId
17d13 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  x==pPage->nCell 
17d14 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f  ){.      rc = mo
17d15 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
17d16 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
17d17 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
17d18 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20  rOffset+8]));.  
17d19 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
17d1a 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28  c = moveToChild(
17d1b 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66  pCur, get4byte(f
17d1c 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  indCell(pPage, i
17d1d 49 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20  Idx)));.    }.  
17d1e 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72  }..  /* An error
17d1f 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52   has occurred. R
17d20 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
17d21 6f 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  ode. */.  return
17d22 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f   rc;.}.#endif../
17d23 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
17d24 70 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64  pager associated
17d25 20 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20   with a BTree.  
17d26 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
17d27 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74  used for.** test
17d28 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
17d29 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  g only..*/.SQLIT
17d2a 45 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20  E_PRIVATE Pager 
17d2b 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67  *sqlite3BtreePag
17d2c 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  er(Btree *p){.  
17d2d 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70  return p->pBt->p
17d2e 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65  Pager;.}..#ifnde
17d2f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
17d30 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a  TEGRITY_CHECK./*
17d31 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73  .** Append a mes
17d32 73 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f  sage to the erro
17d33 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67  r message string
17d34 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
17d35 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
17d36 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a  .  IntegrityCk *
17d37 70 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a  pCheck,.  char *
17d38 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63  zMsg1,.  const c
17d39 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20  har *zFormat,.  
17d3a 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74  ....){.  va_list
17d3b 20 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65   ap;.  if( !pChe
17d3c 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75  ck->mxErr ) retu
17d3d 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78  rn;.  pCheck->mx
17d3e 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d  Err--;.  pCheck-
17d3f 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74  >nErr++;.  va_st
17d40 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
17d41 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e  ;.  if( pCheck->
17d42 65 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a  errMsg.nChar ){.
17d43 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
17d44 63 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63  cumAppend(&pChec
17d45 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c  k->errMsg, "\n",
17d46 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a   1);.  }.  if( z
17d47 4d 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69  Msg1 ){.    sqli
17d48 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
17d49 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73  d(&pCheck->errMs
17d4a 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20  g, zMsg1, -1);. 
17d4b 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72   }.  sqlite3VXPr
17d4c 69 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72  intf(&pCheck->er
17d4d 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74  rMsg, 1, zFormat
17d4e 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
17d4f 61 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63  ap);.  if( pChec
17d50 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63  k->errMsg.malloc
17d51 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43  Failed ){.    pC
17d52 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  heck->mallocFail
17d53 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65  ed = 1;.  }.}.#e
17d54 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
17d55 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
17d56 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ECK */..#ifndef 
17d57 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
17d58 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a  GRITY_CHECK./*.*
17d59 2a 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72  * Add 1 to the r
17d5a 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66  eference count f
17d5b 6f 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20  or page iPage.  
17d5c 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73  If this is the s
17d5d 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e  econd.** referen
17d5e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20  ce to the page, 
17d5f 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  add an error mes
17d60 73 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e  sage to pCheck->
17d61 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75  zErrMsg..** Retu
17d62 72 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72  rn 1 if there ar
17d63 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66  e 2 ore more ref
17d64 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70  erences to the p
17d65 61 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20  age and 0 if.** 
17d66 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  if this is the f
17d67 69 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74  irst reference t
17d68 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a  o the page..**.*
17d69 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61  * Also check tha
17d6a 74 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  t the page numbe
17d6b 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a  r is in bounds..
17d6c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68  */.static int ch
17d6d 65 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79  eckRef(Integrity
17d6e 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f  Ck *pCheck, Pgno
17d6f 20 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43   iPage, char *zC
17d70 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69  ontext){.  if( i
17d71 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e  Page==0 ) return
17d72 20 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e   1;.  if( iPage>
17d73 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b  pCheck->nPage ){
17d74 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64  .    checkAppend
17d75 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
17d76 74 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70  text, "invalid p
17d77 61 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20  age number %d", 
17d78 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75  iPage);.    retu
17d79 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 1;.  }.  if( 
17d7a 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50  pCheck->anRef[iP
17d7b 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63  age]==1 ){.    c
17d7c 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
17d7d 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
17d7e 22 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74  "2nd reference t
17d7f 6f 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67  o page %d", iPag
17d80 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
17d81 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20  ;.  }.  return  
17d82 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69  (pCheck->anRef[i
17d83 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23  Page]++)>1;.}..#
17d84 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
17d85 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a  IT_AUTOVACUUM./*
17d86 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74  .** Check that t
17d87 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  he entry in the 
17d88 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20  pointer-map for 
17d89 70 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73  page iChild maps
17d8a 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61   to .** page iPa
17d8b 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79  rent, pointer ty
17d8c 70 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e  pe ptrType. If n
17d8d 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72  ot, append an er
17d8e 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74  ror message.** t
17d8f 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61  o pCheck..*/.sta
17d90 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74  tic void checkPt
17d91 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74  rmap(.  Integrit
17d92 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f  yCk *pCheck,   /
17d93 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63  * Integrity chec
17d94 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50  k context */.  P
17d95 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20  gno iChild,     
17d96 20 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70        /* Child p
17d97 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  age number */.  
17d98 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20  u8 eType,       
17d99 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74         /* Expect
17d9a 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74  ed pointer map t
17d9b 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50  ype */.  Pgno iP
17d9c 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20  arent,          
17d9d 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e  /* Expected poin
17d9e 74 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70  ter map parent p
17d9f 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  age number */.  
17da0 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20  char *zContext  
17da1 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
17da2 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75  t description (u
17da3 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73  sed for error ms
17da4 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  g) */.){.  int r
17da5 63 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54  c;.  u8 ePtrmapT
17da6 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72  ype;.  Pgno iPtr
17da7 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63  mapParent;..  rc
17da8 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68   = ptrmapGet(pCh
17da9 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64  eck->pBt, iChild
17daa 2c 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20  , &ePtrmapType, 
17dab 26 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b  &iPtrmapParent);
17dac 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
17dad 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
17dae 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
17daf 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
17db0 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68  OERR_NOMEM ) pCh
17db1 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  eck->mallocFaile
17db2 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b  d = 1;.    check
17db3 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
17db4 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69  , zContext, "Fai
17db5 6c 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d  led to read ptrm
17db6 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69  ap key=%d", iChi
17db7 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ld);.    return;
17db8 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72  .  }..  if( ePtr
17db9 6d 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c  mapType!=eType |
17dba 7c 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21  | iPtrmapParent!
17dbb 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  =iParent ){.    
17dbc 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
17dbd 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
17dbe 20 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72   .      "Bad ptr
17dbf 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25   map entry key=%
17dc0 64 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25  d expected=(%d,%
17dc1 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c  d) got=(%d,%d)",
17dc2 20 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20   .      iChild, 
17dc3 65 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20  eType, iParent, 
17dc4 65 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74  ePtrmapType, iPt
17dc5 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d  rmapParent);.  }
17dc6 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
17dc7 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67   Check the integ
17dc8 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65  rity of the free
17dc9 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76  list or of an ov
17dca 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74  erflow page list
17dcb 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74  ..** Verify that
17dcc 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
17dcd 61 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74  ages on the list
17dce 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63   is N..*/.static
17dcf 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28   void checkList(
17dd0 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a  .  IntegrityCk *
17dd1 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65  pCheck,  /* Inte
17dd2 67 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63  grity checking c
17dd3 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
17dd4 69 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20  isFreeList,     
17dd5 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
17dd6 66 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65  freelist.  False
17dd7 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61   for overflow pa
17dd8 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  ge list */.  int
17dd9 20 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20   iPage,         
17dda 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
17ddb 72 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65  r for first page
17ddc 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a   in the list */.
17ddd 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20    int N,        
17dde 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63          /* Expec
17ddf 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  ted number of pa
17de0 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20  ges in the list 
17de1 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74  */.  char *zCont
17de2 65 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ext        /* Co
17de3 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20  ntext for error 
17de4 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20  messages */.){. 
17de5 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78   int i;.  int ex
17de6 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e  pected = N;.  in
17de7 74 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65  t iFirst = iPage
17de8 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e  ;.  while( N-- >
17de9 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78   0 && pCheck->mx
17dea 45 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67  Err ){.    DbPag
17deb 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20  e *pOvflPage;.  
17dec 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
17ded 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20  *pOvflData;.    
17dee 69 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20  if( iPage<1 ){. 
17def 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
17df0 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
17df1 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22  text,.         "
17df2 25 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d  %d of %d pages m
17df3 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72  issing from over
17df4 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69  flow list starti
17df5 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20  ng at %d",.     
17df6 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74       N+1, expect
17df7 65 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20  ed, iFirst);.   
17df8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
17df9 20 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66      if( checkRef
17dfa 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20  (pCheck, iPage, 
17dfb 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61  zContext) ) brea
17dfc 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  k;.    if( sqlit
17dfd 65 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63  e3PagerGet(pChec
17dfe 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f  k->pPager, (Pgno
17dff 29 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61  )iPage, &pOvflPa
17e00 67 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65  ge) ){.      che
17e01 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
17e02 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66  ck, zContext, "f
17e03 61 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67  ailed to get pag
17e04 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20  e %d", iPage);. 
17e05 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
17e06 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20  }.    pOvflData 
17e07 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
17e08 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
17e09 65 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65  etData(pOvflPage
17e0a 29 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65  );.    if( isFre
17e0b 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69  eList ){.      i
17e0c 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28  nt n = get4byte(
17e0d 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a  &pOvflData[4]);.
17e0e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
17e0f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
17e10 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d       if( pCheck-
17e11 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  >pBt->autoVacuum
17e12 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
17e13 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
17e14 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52  iPage, PTRMAP_FR
17e15 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74  EEPAGE, 0, zCont
17e16 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  ext);.      }.#e
17e17 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e  ndif.      if( n
17e18 3e 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73  >pCheck->pBt->us
17e19 61 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a  ableSize/4-2 ){.
17e1a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70          checkApp
17e1b 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
17e1c 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20  Context,.       
17e1d 20 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65      "freelist le
17e1e 61 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67  af count too big
17e1f 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50   on page %d", iP
17e20 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d  age);.        N-
17e21 2d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  -;.      }else{.
17e22 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
17e23 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<n; i++){.    
17e24 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65        Pgno iFree
17e25 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  Page = get4byte(
17e26 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34  &pOvflData[8+i*4
17e27 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ]);.#ifndef SQLI
17e28 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
17e29 55 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  UM.          if(
17e2a 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75   pCheck->pBt->au
17e2b 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
17e2c 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72          checkPtr
17e2d 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65  map(pCheck, iFre
17e2e 65 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52  ePage, PTRMAP_FR
17e2f 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74  EEPAGE, 0, zCont
17e30 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ext);.          
17e31 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
17e32 20 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65     checkRef(pChe
17e33 63 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a  ck, iFreePage, z
17e34 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
17e35 20 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d    }.        N -=
17e36 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   n;.      }.    
17e37 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
17e38 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
17e39 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20  .    else{.     
17e3a 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61   /* If this data
17e3b 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75  base supports au
17e3c 74 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50  to-vacuum and iP
17e3d 61 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c  age is not the l
17e3e 61 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67  ast.      ** pag
17e3f 65 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c  e in this overfl
17e40 6f 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74  ow list, check t
17e41 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d  hat the pointer-
17e42 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20  map entry for.  
17e43 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f      ** the follo
17e44 77 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65  wing page matche
17e45 73 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a  s iPage..      *
17e46 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65  /.      if( pChe
17e47 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63  ck->pBt->autoVac
17e48 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20  uum && N>0 ){.  
17e49 20 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79        i = get4by
17e4a 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20  te(pOvflData);. 
17e4b 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
17e4c 61 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54  ap(pCheck, i, PT
17e4d 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20  RMAP_OVERFLOW2, 
17e4e 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29  iPage, zContext)
17e4f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
17e50 23 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65  #endif.    iPage
17e51 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66   = get4byte(pOvf
17e52 6c 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69  lData);.    sqli
17e53 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f  te3PagerUnref(pO
17e54 76 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a  vflPage);.  }.}.
17e55 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
17e56 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
17e57 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65  CHECK */..#ifnde
17e58 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
17e59 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a  TEGRITY_CHECK./*
17e5a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73  .** Do various s
17e5b 61 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20  anity checks on 
17e5c 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66  a single page of
17e5d 20 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e   a tree.  Return
17e5e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70  .** the tree dep
17e5f 74 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20  th.  Root pages 
17e60 72 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e  return 0.  Paren
17e61 74 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73  ts of root pages
17e62 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e  .** return 1, an
17e63 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a  d so forth..** .
17e64 2a 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20  ** These checks 
17e65 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20  are done:.**.** 
17e66 20 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75       1.  Make su
17e67 72 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e  re that cells an
17e68 64 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20  d freeblocks do 
17e69 6e 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20  not overlap.**  
17e6a 20 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62          but comb
17e6b 69 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c  ine to completel
17e6c 79 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65  y cover the page
17e6d 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61  ..**  NO  2.  Ma
17e6e 6b 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79  ke sure cell key
17e6f 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a  s are in order..
17e70 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65  **  NO  3.  Make
17e71 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20   sure no key is 
17e72 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75  less than or equ
17e73 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e  al to zLowerBoun
17e74 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d  d..**  NO  4.  M
17e75 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20  ake sure no key 
17e76 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
17e77 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70  or equal to zUpp
17e78 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20  erBound..**     
17e79 20 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69   5.  Check the i
17e7a 6e 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72  ntegrity of over
17e7b 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20  flow pages..**  
17e7c 20 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76      6.  Recursiv
17e7d 65 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72  ely call checkTr
17e7e 65 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68  eePage on all ch
17e7f 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20  ildren..**      
17e80 37 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20  7.  Verify that 
17e81 74 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c  the depth of all
17e82 20 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65   children is the
17e83 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38   same..**      8
17e84 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69  .  Make sure thi
17e85 73 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61  s page is at lea
17e86 73 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65  st 33% full or e
17e87 6c 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20  lse it is.**    
17e88 20 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f        the root o
17e89 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73  f the tree..*/.s
17e8a 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54  tatic int checkT
17e8b 72 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67  reePage(.  Integ
17e8c 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20  rityCk *pCheck, 
17e8d 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20   /* Context for 
17e8e 74 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b  the sanity check
17e8f 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c   */.  int iPage,
17e90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
17e91 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
17e92 65 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20  e page to check 
17e93 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65  */.  char *zPare
17e94 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61  ntContext  /* Pa
17e95 72 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rent context */.
17e96 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  ){.  MemPage *pP
17e97 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63  age;.  int i, rc
17e98 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e  , depth, d2, pgn
17e99 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64  o, cnt;.  int hd
17e9a 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20  r, cellStart;.  
17e9b 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20  int nCell;.  u8 
17e9c 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65  *data;.  BtShare
17e9d 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73  d *pBt;.  int us
17e9e 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72  ableSize;.  char
17e9f 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a   zContext[100];.
17ea0 20 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b    char *hit = 0;
17ea1 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ..  sqlite3_snpr
17ea2 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e  intf(sizeof(zCon
17ea3 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c  text), zContext,
17ea4 20 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50   "Page %d: ", iP
17ea5 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  age);..  /* Chec
17ea6 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  k that the page 
17ea7 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42  exists.  */.  pB
17ea8 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b  t = pCheck->pBt;
17ea9 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20  .  usableSize = 
17eaa 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b  pBt->usableSize;
17eab 0a 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20  .  if( iPage==0 
17eac 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
17ead 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63  ( checkRef(pChec
17eae 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e  k, iPage, zParen
17eaf 74 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75  tContext) ) retu
17eb0 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20  rn 0;.  if( (rc 
17eb1 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
17eb2 74 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f  tPage(pBt, (Pgno
17eb3 29 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20  )iPage, &pPage, 
17eb4 30 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66  0))!=0 ){.    if
17eb5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
17eb6 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45  EM || rc==SQLITE
17eb7 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70  _IOERR_NOMEM ) p
17eb8 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69  Check->mallocFai
17eb9 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65  led = 1;.    che
17eba 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
17ebb 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20  ck, zContext,.  
17ebc 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
17ebd 67 65 74 20 74 68 65 20 70 61 67 65 2e 20 65 72  get the page. er
17ebe 72 6f 72 20 63 6f 64 65 3d 25 64 22 2c 20 72 63  ror code=%d", rc
17ebf 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
17ec0 0a 20 20 7d 0a 20 20 69 66 28 20 28 72 63 20 3d  .  }.  if( (rc =
17ec1 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69   sqlite3BtreeIni
17ec2 74 50 61 67 65 28 70 50 61 67 65 29 29 21 3d 30  tPage(pPage))!=0
17ec3 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
17ec4 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55  rc==SQLITE_CORRU
17ec5 50 54 20 29 3b 20 20 2f 2a 20 54 68 65 20 6f 6e  PT );  /* The on
17ec6 6c 79 20 70 6f 73 73 69 62 6c 65 20 65 72 72 6f  ly possible erro
17ec7 72 20 66 72 6f 6d 20 49 6e 69 74 50 61 67 65 20  r from InitPage 
17ec8 2a 2f 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65  */.    checkAppe
17ec9 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
17eca 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20  ontext, .       
17ecb 20 20 20 20 20 20 20 20 20 20 20 20 22 73 71 6c              "sql
17ecc 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
17ecd 65 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f  e() returns erro
17ece 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b  r code %d", rc);
17ecf 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
17ed0 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74  (pPage);.    ret
17ed1 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
17ed2 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74   Check out all t
17ed3 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20  he cells..  */. 
17ed4 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f   depth = 0;.  fo
17ed5 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e  r(i=0; i<pPage->
17ed6 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d  nCell && pCheck-
17ed7 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20  >mxErr; i++){.  
17ed8 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20    u8 *pCell;.   
17ed9 20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c   u32 sz;.    Cel
17eda 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20  lInfo info;..   
17edb 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61   /* Check payloa
17edc 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  d overflow pages
17edd 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
17ede 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
17edf 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a  eof(zContext), z
17ee0 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20  Context,.       
17ee1 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70        "On tree p
17ee2 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20  age %d cell %d: 
17ee3 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20  ", iPage, i);.  
17ee4 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65    pCell = findCe
17ee5 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20  ll(pPage,i);.   
17ee6 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
17ee7 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
17ee8 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
17ee9 20 20 20 20 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44      sz = info.nD
17eea 61 74 61 3b 0a 20 20 20 20 69 66 28 20 21 70 50  ata;.    if( !pP
17eeb 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 20 73 7a  age->intKey ) sz
17eec 20 2b 3d 20 28 69 6e 74 29 69 6e 66 6f 2e 6e 4b   += (int)info.nK
17eed 65 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ey;.    assert( 
17eee 73 7a 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61  sz==info.nPayloa
17eef 64 20 29 3b 0a 20 20 20 20 69 66 28 20 28 73 7a  d );.    if( (sz
17ef0 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 29 20 0a 20  >info.nLocal) . 
17ef1 20 20 20 20 26 26 20 28 26 70 43 65 6c 6c 5b 69      && (&pCell[i
17ef2 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3c 3d  nfo.iOverflow]<=
17ef3 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42  &pPage->aData[pB
17ef4 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 29 0a  t->usableSize]).
17ef5 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 6e 74      ){.      int
17ef6 20 6e 50 61 67 65 20 3d 20 28 73 7a 20 2d 20 69   nPage = (sz - i
17ef7 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 75 73 61  nfo.nLocal + usa
17ef8 62 6c 65 53 69 7a 65 20 2d 20 35 29 2f 28 75 73  bleSize - 5)/(us
17ef9 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20  ableSize - 4);. 
17efa 20 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76       Pgno pgnoOv
17efb 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  fl = get4byte(&p
17efc 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66  Cell[info.iOverf
17efd 6c 6f 77 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53  low]);.#ifndef S
17efe 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
17eff 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20  ACUUM.      if( 
17f00 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
17f01 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
17f02 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70  Ptrmap(pCheck, p
17f03 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f  gnoOvfl, PTRMAP_
17f04 4f 56 45 52 46 4c 4f 57 31 2c 20 69 50 61 67 65  OVERFLOW1, iPage
17f05 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20  , zContext);.   
17f06 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
17f07 20 20 63 68 65 63 6b 4c 69 73 74 28 70 43 68 65    checkList(pChe
17f08 63 6b 2c 20 30 2c 20 70 67 6e 6f 4f 76 66 6c 2c  ck, 0, pgnoOvfl,
17f09 20 6e 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74   nPage, zContext
17f0a 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
17f0b 20 43 68 65 63 6b 20 73 61 6e 69 74 79 20 6f 66   Check sanity of
17f0c 20 6c 65 66 74 20 63 68 69 6c 64 20 70 61 67 65   left child page
17f0d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
17f0e 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
17f0f 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65  .      pgno = ge
17f10 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 23  t4byte(pCell);.#
17f11 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
17f12 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
17f13 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
17f14 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
17f15 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70     checkPtrmap(p
17f16 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52  Check, pgno, PTR
17f17 4d 41 50 5f 42 54 52 45 45 2c 20 69 50 61 67 65  MAP_BTREE, iPage
17f18 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20  , zContext);.   
17f19 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
17f1a 20 20 64 32 20 3d 20 63 68 65 63 6b 54 72 65 65    d2 = checkTree
17f1b 50 61 67 65 28 70 43 68 65 63 6b 2c 20 70 67 6e  Page(pCheck, pgn
17f1c 6f 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  o, zContext);.  
17f1d 20 20 20 20 69 66 28 20 69 3e 30 20 26 26 20 64      if( i>0 && d
17f1e 32 21 3d 64 65 70 74 68 20 29 7b 0a 20 20 20 20  2!=depth ){.    
17f1f 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
17f20 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
17f21 65 78 74 2c 20 22 43 68 69 6c 64 20 70 61 67 65  ext, "Child page
17f22 20 64 65 70 74 68 20 64 69 66 66 65 72 73 22 29   depth differs")
17f23 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
17f24 64 65 70 74 68 20 3d 20 64 32 3b 0a 20 20 20 20  depth = d2;.    
17f25 7d 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61  }.  }.  if( !pPa
17f26 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
17f27 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
17f28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
17f29 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
17f2a 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ]);.    sqlite3_
17f2b 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
17f2c 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74  zContext), zCont
17f2d 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ext, .          
17f2e 20 20 20 20 20 20 20 20 20 20 20 22 4f 6e 20 70             "On p
17f2f 61 67 65 20 25 64 20 61 74 20 72 69 67 68 74 20  age %d at right 
17f30 63 68 69 6c 64 3a 20 22 2c 20 69 50 61 67 65 29  child: ", iPage)
17f31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
17f32 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
17f33 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75  .    if( pBt->au
17f34 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
17f35 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43    checkPtrmap(pC
17f36 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d  heck, pgno, PTRM
17f37 41 50 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c  AP_BTREE, iPage,
17f38 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69   0);.    }.#endi
17f39 66 0a 20 20 20 20 63 68 65 63 6b 54 72 65 65 50  f.    checkTreeP
17f3a 61 67 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f  age(pCheck, pgno
17f3b 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d  , zContext);.  }
17f3c 0a 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f  . .  /* Check fo
17f3d 72 20 63 6f 6d 70 6c 65 74 65 20 63 6f 76 65 72  r complete cover
17f3e 61 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 0a  age of the page.
17f3f 20 20 2a 2f 0a 20 20 64 61 74 61 20 3d 20 70 50    */.  data = pP
17f40 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64  age->aData;.  hd
17f41 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
17f42 66 73 65 74 3b 0a 20 20 68 69 74 20 3d 20 73 71  fset;.  hit = sq
17f43 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
17f44 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29   pBt->pageSize )
17f45 3b 0a 20 20 69 66 28 20 68 69 74 3d 3d 30 20 29  ;.  if( hit==0 )
17f46 7b 0a 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61  {.    pCheck->ma
17f47 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
17f48 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 31 36    }else{.    u16
17f49 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 20 3d   contentOffset =
17f4a 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
17f4b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 69 66 20  hdr+5]);.    if 
17f4c 28 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 20 3e  (contentOffset >
17f4d 20 75 73 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20   usableSize) {. 
17f4e 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
17f4f 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a  Msg(pCheck, 0, .
17f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f51 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e       "Corruption
17f52 20 64 65 74 65 63 74 65 64 20 69 6e 20 68 65 61   detected in hea
17f53 64 65 72 20 6f 6e 20 70 61 67 65 20 25 64 22 2c  der on page %d",
17f54 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20  iPage,0);.      
17f55 67 6f 74 6f 20 63 68 65 63 6b 5f 70 61 67 65 5f  goto check_page_
17f56 61 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20  abort;.    }.   
17f57 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74   memset(hit+cont
17f58 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73  entOffset, 0, us
17f59 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74  ableSize-content
17f5a 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d  Offset);.    mem
17f5b 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74  set(hit, 1, cont
17f5c 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  entOffset);.    
17f5d 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65  nCell = get2byte
17f5e 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a  (&data[hdr+3]);.
17f5f 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20      cellStart = 
17f60 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61  hdr + 12 - 4*pPa
17f61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f  ge->leaf;.    fo
17f62 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
17f63 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  i++){.      int 
17f64 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  pc = get2byte(&d
17f65 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a  ata[cellStart+i*
17f66 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73  2]);.      u16 s
17f67 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20  ize = 1024;.    
17f68 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69    int j;.      i
17f69 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a  f( pc<=usableSiz
17f6a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 69 7a  e ){.        siz
17f6b 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  e = cellSizePtr(
17f6c 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70 63 5d  pPage, &data[pc]
17f6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
17f6e 20 69 66 28 20 28 70 63 2b 73 69 7a 65 2d 31 29   if( (pc+size-1)
17f6f 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20  >=usableSize || 
17f70 70 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  pc<0 ){.        
17f71 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
17f72 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20  Check, 0, .     
17f73 20 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69         "Corrupti
17f74 6f 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63  on detected in c
17f75 65 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25  ell %d on page %
17f76 64 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20  d",i,iPage,0);. 
17f77 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
17f78 20 20 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a      for(j=pc+siz
17f79 65 2d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29  e-1; j>=pc; j--)
17f7a 20 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20   hit[j]++;.     
17f7b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72   }.    }.    for
17f7c 28 63 6e 74 3d 30 2c 20 69 3d 67 65 74 32 62 79  (cnt=0, i=get2by
17f7d 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29  te(&data[hdr+1])
17f7e 3b 20 69 3e 30 20 26 26 20 69 3c 75 73 61 62 6c  ; i>0 && i<usabl
17f7f 65 53 69 7a 65 20 26 26 20 63 6e 74 3c 31 30 30  eSize && cnt<100
17f80 30 30 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20  00; .           
17f81 63 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e  cnt++){.      in
17f82 74 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74  t size = get2byt
17f83 65 28 26 64 61 74 61 5b 69 2b 32 5d 29 3b 0a 20  e(&data[i+2]);. 
17f84 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
17f85 20 20 69 66 28 20 28 69 2b 73 69 7a 65 2d 31 29    if( (i+size-1)
17f86 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20  >=usableSize || 
17f87 69 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  i<0 ){.        c
17f88 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
17f89 68 65 63 6b 2c 20 30 2c 20 20 0a 20 20 20 20 20  heck, 0,  .     
17f8a 20 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69         "Corrupti
17f8b 6f 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63  on detected in c
17f8c 65 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25  ell %d on page %
17f8d 64 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20  d",i,iPage,0);. 
17f8e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
17f8f 20 20 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65      for(j=i+size
17f90 2d 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68  -1; j>=i; j--) h
17f91 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d  it[j]++;.      }
17f92 0a 20 20 20 20 20 20 69 20 3d 20 67 65 74 32 62  .      i = get2b
17f93 79 74 65 28 26 64 61 74 61 5b 69 5d 29 3b 0a 20  yte(&data[i]);. 
17f94 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63     }.    for(i=c
17f95 6e 74 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69  nt=0; i<usableSi
17f96 7a 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ze; i++){.      
17f97 69 66 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b  if( hit[i]==0 ){
17f98 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a  .        cnt++;.
17f99 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
17f9a 68 69 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20  hit[i]>1 ){.    
17f9b 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
17f9c 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20  sg(pCheck, 0,.  
17f9d 20 20 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c          "Multipl
17f9e 65 20 75 73 65 73 20 66 6f 72 20 62 79 74 65 20  e uses for byte 
17f9f 25 64 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20  %d of page %d", 
17fa0 69 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20  i, iPage);.     
17fa1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
17fa2 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
17fa3 63 6e 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d  cnt!=data[hdr+7]
17fa4 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41   ){.      checkA
17fa5 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
17fa6 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22   0, .          "
17fa7 46 72 61 67 6d 65 6e 74 65 64 20 73 70 61 63 65  Fragmented space
17fa8 20 69 73 20 25 64 20 62 79 74 65 20 72 65 70 6f   is %d byte repo
17fa9 72 74 65 64 20 61 73 20 25 64 20 6f 6e 20 70 61  rted as %d on pa
17faa 67 65 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20  ge %d",.        
17fab 20 20 63 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b    cnt, data[hdr+
17fac 37 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20  7], iPage);.    
17fad 7d 0a 20 20 7d 0a 63 68 65 63 6b 5f 70 61 67 65  }.  }.check_page
17fae 5f 61 62 6f 72 74 3a 0a 20 20 69 66 20 28 68 69  _abort:.  if (hi
17faf 74 29 20 73 71 6c 69 74 65 33 50 61 67 65 46 72  t) sqlite3PageFr
17fb0 65 65 28 68 69 74 29 3b 0a 0a 20 20 72 65 6c 65  ee(hit);..  rele
17fb1 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
17fb2 20 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31    return depth+1
17fb3 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
17fb4 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
17fb5 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69  ITY_CHECK */..#i
17fb6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17fb7 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
17fb8 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  K./*.** This rou
17fb9 74 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70  tine does a comp
17fba 6c 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68  lete check of th
17fbb 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 66 69  e given BTree fi
17fbc 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a  le.  aRoot[] is.
17fbd 2a 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70  ** an array of p
17fbe 61 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72  ages numbers wer
17fbf 65 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62  e each page numb
17fc0 65 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70  er is the root p
17fc1 61 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c  age of.** a tabl
17fc2 65 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65  e.  nRoot is the
17fc3 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
17fc4 65 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a  es in aRoot..**.
17fc5 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d  ** Write the num
17fc6 62 65 72 20 6f 66 20 65 72 72 6f 72 20 73 65 65  ber of error see
17fc7 6e 20 69 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78  n in *pnErr.  Ex
17fc8 63 65 70 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65  cept for some me
17fc9 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  mory.** allocati
17fca 6f 6e 20 65 72 72 6f 72 73 2c 20 20 61 6e 20 65  on errors,  an e
17fcb 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 6c  rror message hel
17fcc 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  d in memory obta
17fcd 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c  ined from.** mal
17fce 6c 6f 63 20 69 73 20 72 65 74 75 72 6e 65 64 20  loc is returned 
17fcf 69 66 20 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e  if *pnErr is non
17fd0 2d 7a 65 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72  -zero.  If *pnEr
17fd1 72 3d 3d 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69  r==0 then NULL i
17fd2 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 20  s.** returned.  
17fd3 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  If a memory allo
17fd4 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63  cation error occ
17fd5 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74  urs, NULL is ret
17fd6 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
17fd7 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
17fd8 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67  qlite3BtreeInteg
17fd9 72 69 74 79 43 68 65 63 6b 28 0a 20 20 42 74 72  rityCheck(.  Btr
17fda 65 65 20 2a 70 2c 20 20 20 20 20 2f 2a 20 54 68  ee *p,     /* Th
17fdb 65 20 62 74 72 65 65 20 74 6f 20 62 65 20 63 68  e btree to be ch
17fdc 65 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  ecked */.  int *
17fdd 61 52 6f 6f 74 2c 20 20 20 2f 2a 20 41 6e 20 61  aRoot,   /* An a
17fde 72 72 61 79 20 6f 66 20 72 6f 6f 74 20 70 61 67  rray of root pag
17fdf 65 73 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 69  es numbers for i
17fe0 6e 64 69 76 69 64 75 61 6c 20 74 72 65 65 73 20  ndividual trees 
17fe1 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20  */.  int nRoot, 
17fe2 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
17fe3 65 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74  entries in aRoot
17fe4 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72  [] */.  int mxEr
17fe5 72 2c 20 20 20 20 2f 2a 20 53 74 6f 70 20 72 65  r,    /* Stop re
17fe6 70 6f 72 74 69 6e 67 20 65 72 72 6f 72 73 20 61  porting errors a
17fe7 66 74 65 72 20 74 68 69 73 20 6d 61 6e 79 20 2a  fter this many *
17fe8 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 72 72 20 20  /.  int *pnErr  
17fe9 20 20 2f 2a 20 57 72 69 74 65 20 6e 75 6d 62 65    /* Write numbe
17fea 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e  r of errors seen
17feb 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c   to this variabl
17fec 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 69  e */.){.  Pgno i
17fed 3b 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20  ;.  int nRef;.  
17fee 49 6e 74 65 67 72 69 74 79 43 6b 20 73 43 68 65  IntegrityCk sChe
17fef 63 6b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ck;.  BtShared *
17ff0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
17ff1 63 68 61 72 20 7a 45 72 72 5b 31 30 30 5d 3b 0a  char zErr[100];.
17ff2 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
17ff3 6e 74 65 72 28 70 29 3b 0a 20 20 6e 52 65 66 20  nter(p);.  nRef 
17ff4 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  = sqlite3PagerRe
17ff5 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  fcount(pBt->pPag
17ff6 65 72 29 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 42  er);.  if( lockB
17ff7 74 72 65 65 57 69 74 68 52 65 74 72 79 28 70 29  treeWithRetry(p)
17ff8 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
17ff9 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20     *pnErr = 1;. 
17ffa 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
17ffb 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74  eave(p);.    ret
17ffc 75 72 6e 20 73 71 6c 69 74 65 33 44 62 53 74 72  urn sqlite3DbStr
17ffd 44 75 70 28 30 2c 20 22 63 61 6e 6e 6f 74 20 61  Dup(0, "cannot a
17ffe 63 71 75 69 72 65 20 61 20 72 65 61 64 20 6c 6f  cquire a read lo
17fff 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
18000 73 65 22 29 3b 0a 20 20 7d 0a 20 20 73 43 68 65  se");.  }.  sChe
18001 63 6b 2e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20  ck.pBt = pBt;.  
18002 73 43 68 65 63 6b 2e 70 50 61 67 65 72 20 3d 20  sCheck.pPager = 
18003 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73  pBt->pPager;.  s
18004 43 68 65 63 6b 2e 6e 50 61 67 65 20 3d 20 70 61  Check.nPage = pa
18005 67 65 72 50 61 67 65 63 6f 75 6e 74 28 73 43 68  gerPagecount(sCh
18006 65 63 6b 2e 70 42 74 29 3b 0a 20 20 73 43 68 65  eck.pBt);.  sChe
18007 63 6b 2e 6d 78 45 72 72 20 3d 20 6d 78 45 72 72  ck.mxErr = mxErr
18008 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 45 72 72 20  ;.  sCheck.nErr 
18009 3d 20 30 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61  = 0;.  sCheck.ma
1800a 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
1800b 20 20 2a 70 6e 45 72 72 20 3d 20 30 3b 0a 20 20    *pnErr = 0;.  
1800c 69 66 28 20 73 43 68 65 63 6b 2e 6e 50 61 67 65  if( sCheck.nPage
1800d 3d 3d 30 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63  ==0 ){.    unloc
1800e 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70  kBtreeIfUnused(p
1800f 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Bt);.    sqlite3
18010 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
18011 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
18012 0a 20 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 20  .  sCheck.anRef 
18013 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
18014 20 28 73 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31   (sCheck.nPage+1
18015 29 2a 73 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e  )*sizeof(sCheck.
18016 61 6e 52 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69  anRef[0]) );.  i
18017 66 28 20 21 73 43 68 65 63 6b 2e 61 6e 52 65 66  f( !sCheck.anRef
18018 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74   ){.    unlockBt
18019 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
1801a 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31  ;.    *pnErr = 1
1801b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1801c 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20  eeLeave(p);.    
1801d 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1801e 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65  for(i=0; i<=sChe
1801f 63 6b 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20  ck.nPage; i++){ 
18020 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20  sCheck.anRef[i] 
18021 3d 20 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e  = 0; }.  i = PEN
18022 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
18023 42 74 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43  Bt);.  if( i<=sC
18024 68 65 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20  heck.nPage ){.  
18025 20 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69    sCheck.anRef[i
18026 5d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c  ] = 1;.  }.  sql
18027 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
18028 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c  (&sCheck.errMsg,
18029 20 7a 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45   zErr, sizeof(zE
1802a 72 72 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20  rr), 20000);..  
1802b 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74  /* Check the int
1802c 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72  egrity of the fr
1802d 65 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68  eelist.  */.  ch
1802e 65 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c  eckList(&sCheck,
1802f 20 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42   1, get4byte(&pB
18030 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
18031 5b 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20  [32]),.         
18032 20 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74     get4byte(&pBt
18033 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
18034 33 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65  36]), "Main free
18035 6c 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20  list: ");..  /* 
18036 43 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61  Check all the ta
18037 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  bles..  */.  for
18038 28 69 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f  (i=0; (int)i<nRo
18039 6f 74 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45  ot && sCheck.mxE
1803a 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  rr; i++){.    if
1803b 28 20 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20  ( aRoot[i]==0 ) 
1803c 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65  continue;.#ifnde
1803d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1803e 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
1803f 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
18040 20 26 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29   && aRoot[i]>1 )
18041 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72  {.      checkPtr
18042 6d 61 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f  map(&sCheck, aRo
18043 6f 74 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f  ot[i], PTRMAP_RO
18044 4f 54 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20  OTPAGE, 0, 0);. 
18045 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
18046 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 26 73  checkTreePage(&s
18047 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c  Check, aRoot[i],
18048 20 22 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72   "List of tree r
18049 6f 6f 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20  oots: ");.  }.. 
1804a 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76   /* Make sure ev
1804b 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
1804c 66 69 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63  file is referenc
1804d 65 64 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  ed.  */.  for(i=
1804e 31 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61  1; i<=sCheck.nPa
1804f 67 65 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45  ge && sCheck.mxE
18050 72 72 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66  rr; i++){.#ifdef
18051 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
18052 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20  OVACUUM.    if( 
18053 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d  sCheck.anRef[i]=
18054 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  =0 ){.      chec
18055 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65  kAppendMsg(&sChe
18056 63 6b 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20  ck, 0, "Page %d 
18057 69 73 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20  is never used", 
18058 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a  i);.    }.#else.
18059 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61      /* If the da
1805a 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20  tabase supports 
1805b 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b  auto-vacuum, mak
1805c 65 20 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73  e sure no tables
1805d 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20   contain.    ** 
1805e 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f  references to po
1805f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e  inter-map pages.
18060 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
18061 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d  sCheck.anRef[i]=
18062 3d 30 20 26 26 20 0a 20 20 20 20 20 20 20 28 50  =0 && .       (P
18063 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
18064 2c 20 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d  , i)!=i || !pBt-
18065 3e 61 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a  >autoVacuum) ){.
18066 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e        checkAppen
18067 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c  dMsg(&sCheck, 0,
18068 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 76   "Page %d is nev
18069 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20  er used", i);.  
1806a 20 20 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65    }.    if( sChe
1806b 63 6b 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26  ck.anRef[i]!=0 &
1806c 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41  & .       (PTRMA
1806d 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29  P_PAGENO(pBt, i)
1806e 3d 3d 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f  ==i && pBt->auto
1806f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20  Vacuum) ){.     
18070 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18071 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69  &sCheck, 0, "Poi
18072 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 25 64  nter map page %d
18073 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c   is referenced",
18074 20 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69   i);.    }.#endi
18075 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  f.  }..  /* Make
18076 20 73 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79   sure this analy
18077 73 69 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76  sis did not leav
18078 65 20 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61  e any unref() pa
18079 67 65 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69  ges..  ** This i
1807a 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f  s an internal co
1807b 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b  nsistency check;
1807c 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68   an integrity ch
1807d 65 63 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  eck.  ** of the 
1807e 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e  integrity check.
1807f 0a 20 20 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74  .  */.  unlockBt
18080 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
18081 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52  ;.  if( NEVER(nR
18082 65 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67  ef != sqlite3Pag
18083 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e  erRefcount(pBt->
18084 70 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20  pPager)) ){.    
18085 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26  checkAppendMsg(&
18086 73 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20  sCheck, 0, .    
18087 20 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70    "Outstanding p
18088 61 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66  age count goes f
18089 72 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72  rom %d to %d dur
1808a 69 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69  ing this analysi
1808b 73 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20  s",.      nRef, 
1808c 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
1808d 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
1808e 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20  ).    );.  }..  
1808f 2f 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64  /* Clean  up and
18090 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a   report errors..
18091 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74    */.  sqlite3Bt
18092 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73  reeLeave(p);.  s
18093 71 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65  qlite3_free(sChe
18094 63 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28  ck.anRef);.  if(
18095 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61   sCheck.mallocFa
18096 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  iled ){.    sqli
18097 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74  te3StrAccumReset
18098 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29  (&sCheck.errMsg)
18099 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73  ;.    *pnErr = s
1809a 43 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20  Check.nErr+1;.  
1809b 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1809c 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63    *pnErr = sChec
1809d 6b 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43  k.nErr;.  if( sC
1809e 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73  heck.nErr==0 ) s
1809f 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
180a0 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d  set(&sCheck.errM
180a1 73 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  sg);.  return sq
180a2 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
180a3 69 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d  ish(&sCheck.errM
180a4 73 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  sg);.}.#endif /*
180a5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
180a6 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a  EGRITY_CHECK */.
180a7 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
180a8 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20  e full pathname 
180a9 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
180aa 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  g database file.
180ab 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  .**.** The pager
180ac 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76   filename is inv
180ad 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61  ariant as long a
180ae 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a  s the pager is.*
180af 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20  * open so it is 
180b0 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77  safe to access w
180b1 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61  ithout the BtSha
180b2 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51  red mutex..*/.SQ
180b3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
180b4 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
180b5 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
180b6 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73  (Btree *p){.  as
180b7 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50  sert( p->pBt->pP
180b8 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74  ager!=0 );.  ret
180b9 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72  urn sqlite3Pager
180ba 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d  Filename(p->pBt-
180bb 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a  >pPager);.}../*.
180bc 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61  ** Return the pa
180bd 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  thname of the jo
180be 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74  urnal file for t
180bf 68 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68  his database. Th
180c0 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75  e return.** valu
180c1 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  e of this routin
180c2 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65  e is the same re
180c3 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
180c4 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  her the journal 
180c5 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e  file.** has been
180c6 20 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e   created or not.
180c7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  .**.** The pager
180c8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d   journal filenam
180c9 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61  e is invariant a
180ca 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61  s long as the pa
180cb 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73  ger is.** open s
180cc 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20  o it is safe to 
180cd 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74  access without t
180ce 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65  he BtShared mute
180cf 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  x..*/.SQLITE_PRI
180d0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
180d1 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74  *sqlite3BtreeGet
180d2 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65  Journalname(Btre
180d3 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
180d4 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21   p->pBt->pPager!
180d5 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  =0 );.  return s
180d6 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
180d7 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70  alname(p->pBt->p
180d8 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Pager);.}../*.**
180d9 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f   Return non-zero
180da 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   if a transactio
180db 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a  n is active..*/.
180dc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
180dd 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
180de 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a  sInTrans(Btree *
180df 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d  p){.  assert( p=
180e0 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
180e1 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
180e2 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75  mutex) );.  retu
180e3 72 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54  rn (p && (p->inT
180e4 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
180e5 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  E));.}../*.** Re
180e6 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66  turn non-zero if
180e7 20 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74   a read (or writ
180e8 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  e) transaction i
180e9 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c  s active..*/.SQL
180ea 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
180eb 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
180ec 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20  ReadTrans(Btree 
180ed 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
180ee 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
180ef 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
180f0 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
180f1 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e  ;.  return p->in
180f2 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e  Trans!=TRANS_NON
180f3 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  E;.}..SQLITE_PRI
180f4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
180f5 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28  BtreeIsInBackup(
180f6 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
180f7 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65  ert( p );.  asse
180f8 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
180f9 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
180fa 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  tex) );.  return
180fb 20 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a   p->nBackup!=0;.
180fc 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
180fd 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61  nction returns a
180fe 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c   pointer to a bl
180ff 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73  ob of memory ass
18100 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20  ociated with.** 
18101 61 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d  a single shared-
18102 62 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72  btree. The memor
18103 79 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69  y is used by cli
18104 65 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73  ent code for its
18105 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73   own.** purposes
18106 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74   (for example, t
18107 6f 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c  o store a high-l
18108 65 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f  evel schema asso
18109 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20  ciated with .** 
1810a 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65  the shared-btree
1810b 29 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79  ). The btree lay
1810c 65 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72  er manages refer
1810d 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73  ence counting is
1810e 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sues..**.** The 
1810f 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20  first time this 
18110 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73  is called on a s
18111 68 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79  hared-btree, nBy
18112 74 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  tes bytes of mem
18113 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63  ory.** are alloc
18114 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e  ated, zeroed, an
18115 64 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  d returned to th
18116 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61  e caller. For ea
18117 63 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a  ch subsequent .*
18118 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65  * call the nByte
18119 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69  s parameter is i
1811a 67 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69  gnored and a poi
1811b 6e 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65  nter to the same
1811c 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f   blob.** of memo
1811d 72 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a  ry returned. .**
1811e 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65  .** If the nByte
1811f 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30  s parameter is 0
18120 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66   and the blob of
18121 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20   memory has not 
18122 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f  yet been.** allo
18123 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f  cated, a null po
18124 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  inter is returne
18125 64 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68  d. If the blob h
18126 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a  as already been.
18127 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74  ** allocated, it
18128 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20   is returned as 
18129 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75  normal..**.** Ju
1812a 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68  st before the sh
1812b 61 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c  ared-btree is cl
1812c 6f 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69  osed, the functi
1812d 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65  on passed as the
1812e 20 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d   .** xFree argum
1812f 65 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d  ent when the mem
18130 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77  ory allocation w
18131 61 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b  as made is invok
18132 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c  ed on the .** bl
18133 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20  ob of allocated 
18134 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e  memory. This fun
18135 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74  ction should not
18136 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72   call sqlite3_fr
18137 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d  ee().** on the m
18138 65 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65  emory, the btree
18139 20 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74   layer does that
1813a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1813b 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
1813c 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72  3BtreeSchema(Btr
1813d 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65  ee *p, int nByte
1813e 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28  s, void(*xFree)(
1813f 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68  void *)){.  BtSh
18140 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
18141 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  Bt;.  sqlite3Btr
18142 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66  eeEnter(p);.  if
18143 28 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20  ( !pBt->pSchema 
18144 26 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20  && nBytes ){.   
18145 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20   pBt->pSchema = 
18146 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
18147 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70  o(nBytes);.    p
18148 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20  Bt->xFreeSchema 
18149 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73  = xFree;.  }.  s
1814a 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1814b 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42  (p);.  return pB
1814c 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f  t->pSchema;.}../
1814d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
1814e 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
1814f 43 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72  CACHE if another
18150 20 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d   user of the sam
18151 65 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72  e shared .** btr
18152 65 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65  ee as the argume
18153 6e 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20  nt handle holds 
18154 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
18155 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c  k on the .** sql
18156 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
18157 2e 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49  . Otherwise SQLI
18158 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45  TE_OK..*/.SQLITE
18159 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1815a 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c  ite3BtreeSchemaL
1815b 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b  ocked(Btree *p){
1815c 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73  .  int rc;.  ass
1815d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1815e 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
1815f 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
18160 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
18161 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61  .  rc = querySha
18162 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
18163 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  k(p, MASTER_ROOT
18164 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20  , READ_LOCK);.  
18165 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
18166 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c  TE_OK || rc==SQL
18167 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45  ITE_LOCKED_SHARE
18168 44 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69  DCACHE );.  sqli
18169 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
1816a 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1816b 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1816c 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1816d 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e  CHE./*.** Obtain
1816e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74   a lock on the t
1816f 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20  able whose root 
18170 70 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54  page is iTab.  T
18171 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20  he.** lock is a 
18172 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73  write lock if is
18173 57 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75  Writelock is tru
18174 65 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b  e or a read lock
18175 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c  .** if it is fal
18176 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
18177 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18178 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28  3BtreeLockTable(
18179 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54  Btree *p, int iT
1817a 61 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f  ab, u8 isWriteLo
1817b 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ck){.  int rc = 
1817c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
1817d 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a   p->sharable ){.
1817e 20 20 20 20 75 38 20 6c 6f 63 6b 54 79 70 65 20      u8 lockType 
1817f 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b 20 69 73  = READ_LOCK + is
18180 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 61  WriteLock;.    a
18181 73 73 65 72 74 28 20 52 45 41 44 5f 4c 4f 43 4b  ssert( READ_LOCK
18182 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29  +1==WRITE_LOCK )
18183 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73  ;.    assert( is
18184 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20  WriteLock==0 || 
18185 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29  isWriteLock==1 )
18186 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
18187 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20  eeEnter(p);.    
18188 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 64  rc = queryShared
18189 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70  CacheTableLock(p
1818a 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65  , iTab, lockType
1818b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
1818c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1818d 20 20 72 63 20 3d 20 73 65 74 53 68 61 72 65 64    rc = setShared
1818e 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70  CacheTableLock(p
1818f 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65  , iTab, lockType
18190 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
18191 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
18192 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
18193 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  rc;.}.#endif..#i
18194 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18195 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a  T_INCRBLOB./*.**
18196 20 41 72 67 75 6d 65 6e 74 20 70 43 73 72 20 6d   Argument pCsr m
18197 75 73 74 20 62 65 20 61 20 63 75 72 73 6f 72 20  ust be a cursor 
18198 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69  opened for writi
18199 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e 54  ng on an .** INT
1819a 4b 45 59 20 74 61 62 6c 65 20 63 75 72 72 65 6e  KEY table curren
1819b 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  tly pointing at 
1819c 61 20 76 61 6c 69 64 20 74 61 62 6c 65 20 65 6e  a valid table en
1819d 74 72 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75  try. .** This fu
1819e 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20  nction modifies 
1819f 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 20  the data stored 
181a0 61 73 20 70 61 72 74 20 6f 66 20 74 68 61 74 20  as part of that 
181a1 65 6e 74 72 79 2e 0a 2a 2a 20 4f 6e 6c 79 20 74  entry..** Only t
181a2 68 65 20 64 61 74 61 20 63 6f 6e 74 65 6e 74 20  he data content 
181a3 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64 69  may only be modi
181a4 66 69 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20  fied, it is not 
181a5 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 63  possible.** to c
181a6 68 61 6e 67 65 20 74 68 65 20 6c 65 6e 67 74 68  hange the length
181a7 20 6f 66 20 74 68 65 20 64 61 74 61 20 73 74 6f   of the data sto
181a8 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  red..*/.SQLITE_P
181a9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
181aa 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42  e3BtreePutData(B
181ab 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 75  tCursor *pCsr, u
181ac 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61  32 offset, u32 a
181ad 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20  mt, void *z){.  
181ae 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
181af 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
181b0 74 65 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61  tex(pCsr) );.  a
181b1 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
181b2 75 74 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e  utex_held(pCsr->
181b3 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65  pBtree->db->mute
181b4 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70  x) );.  assert(p
181b5 43 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48  Csr->isIncrblobH
181b6 61 6e 64 6c 65 29 3b 0a 0a 20 20 72 65 73 74 6f  andle);..  resto
181b7 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
181b8 28 70 43 73 72 29 3b 0a 20 20 61 73 73 65 72 74  (pCsr);.  assert
181b9 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d  ( pCsr->eState!=
181ba 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
181bb 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72  EK );.  if( pCsr
181bc 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52  ->eState!=CURSOR
181bd 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65  _VALID ){.    re
181be 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52  turn SQLITE_ABOR
181bf 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  T;.  }..  /* Che
181c0 63 6b 20 73 6f 6d 65 20 70 72 65 63 6f 6e 64 69  ck some precondi
181c1 74 69 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28  tions: .  **   (
181c2 61 29 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  a) the cursor is
181c3 20 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e   open for writin
181c4 67 2c 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68  g,.  **   (b) th
181c5 65 72 65 20 69 73 20 6e 6f 20 72 65 61 64 2d 6c  ere is no read-l
181c6 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ock on the table
181c7 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20   being modified 
181c8 61 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 74  and.  **   (c) t
181c9 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73  he cursor points
181ca 20 61 74 20 61 20 76 61 6c 69 64 20 72 6f 77 20   at a valid row 
181cb 6f 66 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62  of an intKey tab
181cc 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  le..  */.  if( !
181cd 70 43 73 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a  pCsr->wrFlag ){.
181ce 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
181cf 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a  E_READONLY;.  }.
181d0 20 20 61 73 73 65 72 74 28 20 21 70 43 73 72 2d    assert( !pCsr-
181d1 3e 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 0a  >pBt->readOnly .
181d2 20 20 20 20 20 20 20 20 20 20 26 26 20 70 43 73            && pCs
181d3 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  r->pBt->inTransa
181d4 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
181d5 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 63 68 65  TE );.  rc = che
181d6 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63  ckForReadConflic
181d7 74 73 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c  ts(pCsr->pBtree,
181d8 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCsr->pgnoRoot,
181d9 20 70 43 73 72 2c 20 30 29 3b 0a 20 20 69 66 28   pCsr, 0);.  if(
181da 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
181db 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  {.    /* The tab
181dc 6c 65 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74  le pCur points t
181dd 6f 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63  o has a read loc
181de 6b 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  k */.    assert(
181df 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b   rc==SQLITE_LOCK
181e0 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 29  ED_SHAREDCACHE )
181e1 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
181e2 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 73 72 2d  .  }.  if( pCsr-
181e3 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
181e4 49 4e 56 41 4c 49 44 20 7c 7c 20 21 70 43 73 72  INVALID || !pCsr
181e5 2d 3e 61 70 50 61 67 65 5b 70 43 73 72 2d 3e 69  ->apPage[pCsr->i
181e6 50 61 67 65 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b  Page]->intKey ){
181e7 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
181e8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
181e9 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61   return accessPa
181ea 79 6c 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73  yload(pCsr, offs
181eb 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e  et, amt, (unsign
181ec 65 64 20 63 68 61 72 20 2a 29 7a 2c 20 30 2c 20  ed char *)z, 0, 
181ed 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65  1);.}../* .** Se
181ee 74 20 61 20 66 6c 61 67 20 6f 6e 20 74 68 69 73  t a flag on this
181ef 20 63 75 72 73 6f 72 20 74 6f 20 63 61 63 68 65   cursor to cache
181f0 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 73 20 6f   the locations o
181f1 66 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65  f pages from the
181f2 20 0a 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6c 69   .** overflow li
181f3 73 74 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  st for the curre
181f4 6e 74 20 72 6f 77 2e 20 54 68 69 73 20 69 73 20  nt row. This is 
181f5 75 73 65 64 20 62 79 20 63 75 72 73 6f 72 73 20  used by cursors 
181f6 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 69 6e  opened.** for in
181f7 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49  cremental blob I
181f8 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  O only..**.** Th
181f9 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73  is function sets
181fa 20 61 20 66 6c 61 67 20 6f 6e 6c 79 2e 20 54 68   a flag only. Th
181fb 65 20 61 63 74 75 61 6c 20 70 61 67 65 20 6c 6f  e actual page lo
181fc 63 61 74 69 6f 6e 20 63 61 63 68 65 0a 2a 2a 20  cation cache.** 
181fd 28 73 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72  (stored in BtCur
181fe 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 29  sor.aOverflow[])
181ff 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
18200 64 20 75 73 65 64 20 62 79 20 66 75 6e 63 74 69  d used by functi
18201 6f 6e 0a 2a 2a 20 61 63 63 65 73 73 50 61 79 6c  on.** accessPayl
18202 6f 61 64 28 29 20 28 74 68 65 20 77 6f 72 6b 65  oad() (the worke
18203 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 73  r function for s
18204 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
18205 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ) and.** sqlite3
18206 42 74 72 65 65 50 75 74 44 61 74 61 28 29 29 2e  BtreePutData()).
18207 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18208 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
18209 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f  treeCacheOverflo
1820a 77 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  w(BtCursor *pCur
1820b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ){.  assert( cur
1820c 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
1820d 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
1820e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1820f 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65  eld(pCur->pBtree
18210 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
18211 20 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e    assert(!pCur->
18212 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65  isIncrblobHandle
18213 29 3b 0a 20 20 61 73 73 65 72 74 28 21 70 43 75  );.  assert(!pCu
18214 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20  r->aOverflow);. 
18215 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f   pCur->isIncrblo
18216 62 48 61 6e 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23  bHandle = 1;.}.#
18217 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
18218 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74  ****** End of bt
18219 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ree.c **********
1821a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1821b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1821c 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1821d 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1821e 65 20 62 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a  e backup.c *****
1821f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18221 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
18222 39 20 4a 61 6e 75 61 72 79 20 32 38 0a 2a 2a 0a  9 January 28.**.
18223 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
18224 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
18225 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
18226 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
18227 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
18228 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
18229 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1822a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1822b 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1822c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1822d 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1822e 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1822f 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
18230 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
18231 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
18232 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
18233 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
18234 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
18235 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18236 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18237 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18238 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
18239 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
1823a 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
1823b 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
1823c 74 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29  te3_backup_XXX()
1823d 20 0a 2a 2a 20 41 50 49 20 66 75 6e 63 74 69 6f   .** API functio
1823e 6e 73 20 61 6e 64 20 74 68 65 20 72 65 6c 61 74  ns and the relat
1823f 65 64 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a  ed features..**.
18240 2a 2a 20 24 49 64 3a 20 62 61 63 6b 75 70 2e 63  ** $Id: backup.c
18241 2c 76 20 31 2e 31 33 2e 32 2e 31 20 32 30 30 39  ,v 1.13.2.1 2009
18242 2f 30 35 2f 31 38 20 31 37 3a 31 31 3a 33 31 20  /05/18 17:11:31 
18243 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
18244 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74   Macro to find t
18245 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77  he minimum of tw
18246 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73  o numeric values
18247 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e  ..*/.#ifndef MIN
18248 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c  .# define MIN(x,
18249 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a  y) ((x)<(y)?(x):
1824a 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  (y)).#endif../*.
1824b 2a 2a 20 53 74 72 75 63 74 75 72 65 20 61 6c 6c  ** Structure all
1824c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  ocated for each 
1824d 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
1824e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
1824f 74 65 33 5f 62 61 63 6b 75 70 20 7b 0a 20 20 73  te3_backup {.  s
18250 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 3b  qlite3* pDestDb;
18251 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69          /* Desti
18252 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
18253 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65  handle */.  Btre
18254 65 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20  e *pDest;       
18255 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74       /* Destinat
18256 69 6f 6e 20 62 2d 74 72 65 65 20 66 69 6c 65 20  ion b-tree file 
18257 2a 2f 0a 20 20 75 33 32 20 69 44 65 73 74 53 63  */.  u32 iDestSc
18258 68 65 6d 61 3b 20 20 20 20 20 20 20 20 20 2f 2a  hema;         /*
18259 20 4f 72 69 67 69 6e 61 6c 20 73 63 68 65 6d 61   Original schema
1825a 20 63 6f 6f 6b 69 65 20 69 6e 20 64 65 73 74 69   cookie in desti
1825b 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nation */.  int 
1825c 62 44 65 73 74 4c 6f 63 6b 65 64 3b 20 20 20 20  bDestLocked;    
1825d 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63       /* True onc
1825e 65 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61  e a write-transa
1825f 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e  ction is open on
18260 20 70 44 65 73 74 20 2a 2f 0a 0a 20 20 50 67 6e   pDest */..  Pgn
18261 6f 20 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20  o iNext;        
18262 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
18263 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74  mber of the next
18264 20 73 6f 75 72 63 65 20 70 61 67 65 20 74 6f 20   source page to 
18265 63 6f 70 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65  copy */.  sqlite
18266 33 2a 20 70 53 72 63 44 62 3b 20 20 20 20 20 20  3* pSrcDb;      
18267 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74     /* Source dat
18268 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
18269 20 20 42 74 72 65 65 20 2a 70 53 72 63 3b 20 20    Btree *pSrc;  
1826a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
1826b 75 72 63 65 20 62 2d 74 72 65 65 20 66 69 6c 65  urce b-tree file
1826c 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 3b 20 20   */..  int rc;  
1826d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1826e 2f 2a 20 42 61 63 6b 75 70 20 70 72 6f 63 65 73  /* Backup proces
1826f 73 20 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a  s error code */.
18270 0a 20 20 2f 2a 20 54 68 65 73 65 20 74 77 6f 20  .  /* These two 
18271 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 73 65  variables are se
18272 74 20 62 79 20 65 76 65 72 79 20 63 61 6c 6c 20  t by every call 
18273 74 6f 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29  to backup_step()
18274 2e 20 54 68 65 79 20 61 72 65 0a 20 20 2a 2a 20  . They are.  ** 
18275 72 65 61 64 20 62 79 20 63 61 6c 6c 73 20 74 6f  read by calls to
18276 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e   backup_remainin
18277 67 28 29 20 61 6e 64 20 62 61 63 6b 75 70 5f 70  g() and backup_p
18278 61 67 65 63 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f  agecount()..  */
18279 0a 20 20 50 67 6e 6f 20 6e 52 65 6d 61 69 6e 69  .  Pgno nRemaini
1827a 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ng;         /* N
1827b 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6c  umber of pages l
1827c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20  eft to copy */. 
1827d 20 50 67 6e 6f 20 6e 50 61 67 65 63 6f 75 6e 74   Pgno nPagecount
1827e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74  ;         /* Tot
1827f 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
18280 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20  es to copy */.. 
18281 20 69 6e 74 20 69 73 41 74 74 61 63 68 65 64 3b   int isAttached;
18282 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
18283 65 20 6f 6e 63 65 20 62 61 63 6b 75 70 20 68 61  e once backup ha
18284 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  s been registere
18285 64 20 77 69 74 68 20 70 61 67 65 72 20 2a 2f 0a  d with pager */.
18286 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
18287 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65   *pNext;   /* Ne
18288 78 74 20 62 61 63 6b 75 70 20 61 73 73 6f 63 69  xt backup associ
18289 61 74 65 64 20 77 69 74 68 20 73 6f 75 72 63 65  ated with source
1828a 20 70 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   pager */.};../*
1828b 0a 2a 2a 20 54 48 52 45 41 44 20 53 41 46 45 54  .** THREAD SAFET
1828c 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 20  Y NOTES:.**.**  
1828d 20 4f 6e 63 65 20 69 74 20 68 61 73 20 62 65 65   Once it has bee
1828e 6e 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  n created using 
1828f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 61  backup_init(), a
18290 20 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 5f   single sqlite3_
18291 62 61 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72 75  backup.**   stru
18292 63 74 75 72 65 20 6d 61 79 20 62 65 20 61 63 63  cture may be acc
18293 65 73 73 65 64 20 76 69 61 20 74 77 6f 20 67 72  essed via two gr
18294 6f 75 70 73 20 6f 66 20 74 68 72 65 61 64 2d 73  oups of thread-s
18295 61 66 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  afe entry points
18296 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69  :.**.**     * Vi
18297 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61  a the sqlite3_ba
18298 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 66  ckup_XXX() API f
18299 75 6e 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f 73  unction backup_s
1829a 74 65 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20 20  tep() and .**   
1829b 20 20 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 73      backup_finis
1829c 68 28 29 2e 20 42 6f 74 68 20 74 68 65 73 65 20  h(). Both these 
1829d 66 75 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 6e  functions obtain
1829e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
1829f 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 68 61  base.**       ha
182a0 6e 64 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 74  ndle mutex and t
182a1 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61  he mutex associa
182a2 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75  ted with the sou
182a3 72 63 65 20 42 74 53 68 61 72 65 64 20 0a 2a 2a  rce BtShared .**
182a4 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72 65         structure
182a5 2c 20 69 6e 20 74 68 61 74 20 6f 72 64 65 72 2e  , in that order.
182a6 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61  .**.**     * Via
182a7 20 74 68 65 20 42 61 63 6b 75 70 55 70 64 61 74   the BackupUpdat
182a8 65 28 29 20 61 6e 64 20 42 61 63 6b 75 70 52 65  e() and BackupRe
182a9 73 74 61 72 74 28 29 20 66 75 6e 63 74 69 6f 6e  start() function
182aa 73 2c 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20  s, which are.** 
182ab 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 62 79        invoked by
182ac 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72   the pager layer
182ad 20 74 6f 20 72 65 70 6f 72 74 20 76 61 72 69 6f   to report vario
182ae 75 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 73  us state changes
182af 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68 65   in.**       the
182b0 20 70 61 67 65 20 63 61 63 68 65 20 61 73 73 6f   page cache asso
182b1 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
182b2 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e  source database.
182b3 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20   The mutex.**   
182b4 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 77      associated w
182b5 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 64  ith the source d
182b6 61 74 61 62 61 73 65 20 42 74 53 68 61 72 65 64  atabase BtShared
182b7 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20   structure will 
182b8 61 6c 77 61 79 73 20 0a 2a 2a 20 20 20 20 20 20  always .**      
182b9 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 65 69   be held when ei
182ba 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 66 75  ther of these fu
182bb 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f  nctions are invo
182bc 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65  ked..**.**   The
182bd 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f 62   other sqlite3_b
182be 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20  ackup_XXX() API 
182bf 66 75 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b 75  functions, backu
182c0 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e  p_remaining() an
182c1 64 0a 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70 61  d.**   backup_pa
182c2 67 65 63 6f 75 6e 74 28 29 20 61 72 65 20 6e 6f  gecount() are no
182c3 74 20 74 68 72 65 61 64 2d 73 61 66 65 20 66 75  t thread-safe fu
182c4 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 79  nctions. If they
182c5 20 61 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 20   are called.**  
182c6 20 77 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68 65   while some othe
182c7 72 20 74 68 72 65 61 64 20 69 73 20 63 61 6c 6c  r thread is call
182c8 69 6e 67 20 62 61 63 6b 75 70 5f 73 74 65 70 28  ing backup_step(
182c9 29 20 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e 69  ) or backup_fini
182ca 73 68 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20 76  sh(),.**   the v
182cb 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 6d  alues returned m
182cc 61 79 20 62 65 20 69 6e 76 61 6c 69 64 2e 20 54  ay be invalid. T
182cd 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66  here is no way f
182ce 6f 72 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  or a call to.** 
182cf 20 20 42 61 63 6b 75 70 55 70 64 61 74 65 28 29    BackupUpdate()
182d0 20 6f 72 20 42 61 63 6b 75 70 52 65 73 74 61 72   or BackupRestar
182d1 74 28 29 20 74 6f 20 69 6e 74 65 72 66 65 72 65  t() to interfere
182d2 20 77 69 74 68 20 62 61 63 6b 75 70 5f 72 65 6d   with backup_rem
182d3 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f 72  aining().**   or
182d4 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e   backup_pagecoun
182d5 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65 70  t()..**.**   Dep
182d6 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 51  ending on the SQ
182d7 4c 69 74 65 20 63 6f 6e 66 69 67 75 72 61 74 69  Lite configurati
182d8 6f 6e 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  on, the database
182d9 20 68 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 0a   handles and/or.
182da 2a 2a 20 20 20 74 68 65 20 42 74 72 65 65 20 6f  **   the Btree o
182db 62 6a 65 63 74 73 20 6d 61 79 20 68 61 76 65 20  bjects may have 
182dc 74 68 65 69 72 20 6f 77 6e 20 6d 75 74 65 78 65  their own mutexe
182dd 73 20 74 68 61 74 20 72 65 71 75 69 72 65 20 6c  s that require l
182de 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f 6e  ocking..**   Non
182df 2d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73  -sharable Btrees
182e0 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61   (in-memory data
182e1 62 61 73 65 73 20 66 6f 72 20 65 78 61 6d 70 6c  bases for exampl
182e2 65 29 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a  e), do not have.
182e3 2a 2a 20 20 20 61 73 73 6f 63 69 61 74 65 64 20  **   associated 
182e4 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a  mutexes..*/../*.
182e5 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
182e6 74 65 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ter correspondin
182e7 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 7a 44  g to database zD
182e8 62 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20  b (i.e. "main", 
182e9 22 74 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63 6f  "temp").** in co
182ea 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
182eb 70 44 62 2e 20 49 66 20 73 75 63 68 20 61 20 64  pDb. If such a d
182ec 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62  atabase cannot b
182ed 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 0a  e found, return.
182ee 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ** a NULL pointe
182ef 72 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20 65  r and write an e
182f0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20  rror message to 
182f1 70 45 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a 20  pErrorDb..**.** 
182f2 49 66 20 74 68 65 20 22 74 65 6d 70 22 20 64 61  If the "temp" da
182f3 74 61 62 61 73 65 20 69 73 20 72 65 71 75 65 73  tabase is reques
182f4 74 65 64 2c 20 69 74 20 6d 61 79 20 6e 65 65 64  ted, it may need
182f5 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 62 79   to be opened by
182f6 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   this .** functi
182f7 6f 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  on. If an error 
182f8 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69  occurs while doi
182f9 6e 67 20 73 6f 2c 20 72 65 74 75 72 6e 20 30 20  ng so, return 0 
182fa 61 6e 64 20 77 72 69 74 65 20 61 6e 20 0a 2a 2a  and write an .**
182fb 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
182fc 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a 73  o pErrorDb..*/.s
182fd 74 61 74 69 63 20 42 74 72 65 65 20 2a 66 69 6e  tatic Btree *fin
182fe 64 42 74 72 65 65 28 73 71 6c 69 74 65 33 20 2a  dBtree(sqlite3 *
182ff 70 45 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 65  pErrorDb, sqlite
18300 33 20 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 68  3 *pDb, const ch
18301 61 72 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74 20  ar *zDb){.  int 
18302 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44  i = sqlite3FindD
18303 62 4e 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 3b  bName(pDb, zDb);
18304 0a 0a 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a  ..  if( i==1 ){.
18305 20 20 20 20 50 61 72 73 65 20 73 50 61 72 73 65      Parse sParse
18306 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 50  ;.    memset(&sP
18307 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  arse, 0, sizeof(
18308 73 50 61 72 73 65 29 29 3b 0a 20 20 20 20 73 50  sParse));.    sP
18309 61 72 73 65 2e 64 62 20 3d 20 70 44 62 3b 0a 20  arse.db = pDb;. 
1830a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 70     if( sqlite3Op
1830b 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 26  enTempDatabase(&
1830c 73 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20  sParse) ){.     
1830d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65   sqlite3ErrorCle
1830e 61 72 28 26 73 50 61 72 73 65 29 3b 0a 20 20 20  ar(&sParse);.   
1830f 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
18310 70 45 72 72 6f 72 44 62 2c 20 73 50 61 72 73 65  pErrorDb, sParse
18311 2e 72 63 2c 20 22 25 73 22 2c 20 73 50 61 72 73  .rc, "%s", sPars
18312 65 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  e.zErrMsg);.    
18313 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
18314 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 50  }.    assert( sP
18315 61 72 73 65 2e 7a 45 72 72 4d 73 67 3d 3d 30 20  arse.zErrMsg==0 
18316 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3c  );.  }..  if( i<
18317 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
18318 45 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20  Error(pErrorDb, 
18319 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 75  SQLITE_ERROR, "u
1831a 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65 20  nknown database 
1831b 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 72  %s", zDb);.    r
1831c 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
1831d 72 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 62 5b  return pDb->aDb[
1831e 69 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  i].pBt;.}../*.**
1831f 20 43 72 65 61 74 65 20 61 6e 20 73 71 6c 69 74   Create an sqlit
18320 65 33 5f 62 61 63 6b 75 70 20 70 72 6f 63 65 73  e3_backup proces
18321 73 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f  s to copy the co
18322 6e 74 65 6e 74 73 20 6f 66 20 7a 53 72 63 44 62  ntents of zSrcDb
18323 20 66 72 6f 6d 0a 2a 2a 20 63 6f 6e 6e 65 63 74   from.** connect
18324 69 6f 6e 20 68 61 6e 64 6c 65 20 70 53 72 63 44  ion handle pSrcD
18325 62 20 74 6f 20 7a 44 65 73 74 44 62 20 69 6e 20  b to zDestDb in 
18326 70 44 65 73 74 44 62 2e 20 49 66 20 73 75 63 63  pDestDb. If succ
18327 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a  essful, return.*
18328 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  * a pointer to t
18329 68 65 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 62  he new sqlite3_b
1832a 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 0a 2a 2a  ackup object..**
1832b 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
1832c 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20  occurs, NULL is 
1832d 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20  returned and an 
1832e 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65  error code and e
1832f 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20  rror message.** 
18330 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61  stored in databa
18331 73 65 20 68 61 6e 64 6c 65 20 70 44 65 73 74 44  se handle pDestD
18332 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  b..*/.SQLITE_API
18333 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
18334 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  *sqlite3_backup_
18335 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 2a  init(.  sqlite3*
18336 20 70 44 65 73 74 44 62 2c 20 20 20 20 20 20 20   pDestDb,       
18337 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18338 20 44 61 74 61 62 61 73 65 20 74 6f 20 77 72 69   Database to wri
18339 74 65 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74  te to */.  const
1833a 20 63 68 61 72 20 2a 7a 44 65 73 74 44 62 2c 20   char *zDestDb, 
1833b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1833c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61   /* Name of data
1833d 62 61 73 65 20 77 69 74 68 69 6e 20 70 44 65 73  base within pDes
1833e 74 44 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  tDb */.  sqlite3
1833f 2a 20 70 53 72 63 44 62 2c 20 20 20 20 20 20 20  * pSrcDb,       
18340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18341 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
18342 63 74 69 6f 6e 20 74 6f 20 72 65 61 64 20 66 72  ction to read fr
18343 6f 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  om */.  const ch
18344 61 72 20 2a 7a 53 72 63 44 62 20 20 20 20 20 20  ar *zSrcDb      
18345 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18346 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
18347 65 20 77 69 74 68 69 6e 20 70 53 72 63 44 62 20  e within pSrcDb 
18348 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  */.){.  sqlite3_
18349 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 20  backup *p;      
1834a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1834b 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e   Value to return
1834c 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74   */..  /* Lock t
1834d 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
1834e 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64  se handle. The d
1834f 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
18350 61 73 65 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20  ase.  ** handle 
18351 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 69 6e  is not locked in
18352 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 62   this routine, b
18353 75 74 20 69 74 20 69 73 20 6c 6f 63 6b 65 64 20  ut it is locked 
18354 69 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  in.  ** sqlite3_
18355 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54  backup_step(). T
18356 68 65 20 75 73 65 72 20 69 73 20 72 65 71 75 69  he user is requi
18357 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68  red to ensure th
18358 61 74 20 6e 6f 0a 20 20 2a 2a 20 6f 74 68 65 72  at no.  ** other
18359 20 74 68 72 65 61 64 20 61 63 63 65 73 73 65 73   thread accesses
1835a 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
1835b 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20   handle for the 
1835c 64 75 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 6f 66  duration.  ** of
1835d 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72   the backup oper
1835e 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65  ation.  Any atte
1835f 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 64  mpt to use the d
18360 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20  estination.  ** 
18361 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
18362 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b  ion while a back
18363 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73  up is in progres
18364 73 20 6d 61 79 20 63 61 75 73 65 0a 20 20 2a 2a  s may cause.  **
18365 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 6f   a malfunction o
18366 72 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 20 20  r a deadlock..  
18367 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
18368 65 78 5f 65 6e 74 65 72 28 70 53 72 63 44 62 2d  ex_enter(pSrcDb-
18369 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74  >mutex);.  sqlit
1836a 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
1836b 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  DestDb->mutex);.
1836c 0a 20 20 69 66 28 20 70 53 72 63 44 62 3d 3d 70  .  if( pSrcDb==p
1836d 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71  DestDb ){.    sq
1836e 6c 69 74 65 33 45 72 72 6f 72 28 0a 20 20 20 20  lite3Error(.    
1836f 20 20 20 20 70 44 65 73 74 44 62 2c 20 53 51 4c      pDestDb, SQL
18370 49 54 45 5f 45 52 52 4f 52 2c 20 22 73 6f 75 72  ITE_ERROR, "sour
18371 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69  ce and destinati
18372 6f 6e 20 6d 75 73 74 20 62 65 20 64 69 73 74 69  on must be disti
18373 6e 63 74 22 0a 20 20 20 20 29 3b 0a 20 20 20 20  nct".    );.    
18374 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 7b  p = 0;.  }else {
18375 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  .    /* Allocate
18376 20 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65 77   space for a new
18377 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
18378 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 20  object */.    p 
18379 3d 20 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  = (sqlite3_backu
1837a 70 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  p *)sqlite3_mall
1837b 6f 63 28 73 69 7a 65 6f 66 28 73 71 6c 69 74 65  oc(sizeof(sqlite
1837c 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20 20  3_backup));.    
1837d 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20  if( !p ){.      
1837e 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 44 65  sqlite3Error(pDe
1837f 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d  stDb, SQLITE_NOM
18380 45 4d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  EM, 0);.    }.  
18381 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61  }..  /* If the a
18382 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 63 63 65 65  llocation succee
18383 64 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68  ded, populate th
18384 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 2a 2f  e new object. */
18385 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
18386 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a  memset(p, 0, siz
18387 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  eof(sqlite3_back
18388 75 70 29 29 3b 0a 20 20 20 20 70 2d 3e 70 53 72  up));.    p->pSr
18389 63 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44  c = findBtree(pD
1838a 65 73 74 44 62 2c 20 70 53 72 63 44 62 2c 20 7a  estDb, pSrcDb, z
1838b 53 72 63 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70  SrcDb);.    p->p
1838c 44 65 73 74 20 3d 20 66 69 6e 64 42 74 72 65 65  Dest = findBtree
1838d 28 70 44 65 73 74 44 62 2c 20 70 44 65 73 74 44  (pDestDb, pDestD
1838e 62 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20 20 20  b, zDestDb);.   
1838f 20 70 2d 3e 70 44 65 73 74 44 62 20 3d 20 70 44   p->pDestDb = pD
18390 65 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e 70 53  estDb;.    p->pS
18391 72 63 44 62 20 3d 20 70 53 72 63 44 62 3b 0a 20  rcDb = pSrcDb;. 
18392 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b     p->iNext = 1;
18393 0a 20 20 20 20 70 2d 3e 69 73 41 74 74 61 63 68  .    p->isAttach
18394 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28  ed = 0;..    if(
18395 20 30 3d 3d 70 2d 3e 70 53 72 63 20 7c 7c 20 30   0==p->pSrc || 0
18396 3d 3d 70 2d 3e 70 44 65 73 74 20 29 7b 0a 20 20  ==p->pDest ){.  
18397 20 20 20 20 2f 2a 20 4f 6e 65 20 28 6f 72 20 62      /* One (or b
18398 6f 74 68 29 20 6f 66 20 74 68 65 20 6e 61 6d 65  oth) of the name
18399 64 20 64 61 74 61 62 61 73 65 73 20 64 69 64 20  d databases did 
1839a 6e 6f 74 20 65 78 69 73 74 2e 20 41 6e 20 65 72  not exist. An er
1839b 72 6f 72 20 68 61 73 0a 20 20 20 20 20 20 2a 2a  ror has.      **
1839c 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72   already been wr
1839d 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 70  itten into the p
1839e 44 65 73 74 44 62 20 68 61 6e 64 6c 65 2e 20 41  DestDb handle. A
1839f 6c 6c 20 74 68 61 74 20 69 73 20 6c 65 66 74 0a  ll that is left.
183a0 20 20 20 20 20 20 2a 2a 20 74 6f 20 64 6f 20 68        ** to do h
183a1 65 72 65 20 69 73 20 66 72 65 65 20 74 68 65 20  ere is free the 
183a2 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73  sqlite3_backup s
183a3 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20  tructure..      
183a4 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
183a5 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  _free(p);.      
183a6 70 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  p = 0;.    }.  }
183a7 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
183a8 70 2d 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70  p->pSrc->nBackup
183a9 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  ++;.  }..  sqlit
183aa 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
183ab 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  DestDb->mutex);.
183ac 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
183ad 6c 65 61 76 65 28 70 53 72 63 44 62 2d 3e 6d 75  leave(pSrcDb->mu
183ae 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  tex);.  return p
183af 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  ;.}../*.** Argum
183b0 65 6e 74 20 72 63 20 69 73 20 61 6e 20 53 51 4c  ent rc is an SQL
183b1 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20  ite error code. 
183b2 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
183b3 68 69 73 20 65 72 72 6f 72 20 69 73 20 0a 2a 2a  his error is .**
183b4 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61   considered fata
183b5 6c 20 69 66 20 65 6e 63 6f 75 6e 74 65 72 65 64  l if encountered
183b6 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70   during a backup
183b7 20 6f 70 65 72 61 74 69 6f 6e 2e 20 41 6c 6c 20   operation. All 
183b8 65 72 72 6f 72 73 0a 2a 2a 20 61 72 65 20 63 6f  errors.** are co
183b9 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c 20 65  nsidered fatal e
183ba 78 63 65 70 74 20 66 6f 72 20 53 51 4c 49 54 45  xcept for SQLITE
183bb 5f 42 55 53 59 20 61 6e 64 20 53 51 4c 49 54 45  _BUSY and SQLITE
183bc 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 73 74 61 74  _LOCKED..*/.stat
183bd 69 63 20 69 6e 74 20 69 73 46 61 74 61 6c 45 72  ic int isFatalEr
183be 72 6f 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 72  ror(int rc){.  r
183bf 65 74 75 72 6e 20 28 72 63 21 3d 53 51 4c 49 54  eturn (rc!=SQLIT
183c0 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49  E_OK && rc!=SQLI
183c1 54 45 5f 42 55 53 59 20 26 26 20 72 63 21 3d 53  TE_BUSY && rc!=S
183c2 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 3b 0a 7d  QLITE_LOCKED);.}
183c3 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65  ../*.** Paramete
183c4 72 20 7a 53 72 63 44 61 74 61 20 70 6f 69 6e 74  r zSrcData point
183c5 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f  s to a buffer co
183c6 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 64 61 74  ntaining the dat
183c7 61 20 66 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69  a for .** page i
183c8 53 72 63 50 67 20 66 72 6f 6d 20 74 68 65 20 73  SrcPg from the s
183c9 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20  ource database. 
183ca 43 6f 70 79 20 74 68 69 73 20 64 61 74 61 20 69  Copy this data i
183cb 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 64 65 73 74  nto the .** dest
183cc 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
183cd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
183ce 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 73 71  backupOnePage(sq
183cf 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c  lite3_backup *p,
183d0 20 50 67 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f   Pgno iSrcPg, co
183d1 6e 73 74 20 75 38 20 2a 7a 53 72 63 44 61 74 61  nst u8 *zSrcData
183d2 29 7b 0a 20 20 50 61 67 65 72 20 2a 20 63 6f 6e  ){.  Pager * con
183d3 73 74 20 70 44 65 73 74 50 61 67 65 72 20 3d 20  st pDestPager = 
183d4 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
183d5 72 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63  r(p->pDest);.  c
183d6 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63 50 67 73  onst int nSrcPgs
183d7 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  z = sqlite3Btree
183d8 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70  GetPageSize(p->p
183d9 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73  Src);.  int nDes
183da 74 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42  tPgsz = sqlite3B
183db 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28  treeGetPageSize(
183dc 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e  p->pDest);.  con
183dd 73 74 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d  st int nCopy = M
183de 49 4e 28 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65  IN(nSrcPgsz, nDe
183df 73 74 50 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74  stPgsz);.  const
183e0 20 69 36 34 20 69 45 6e 64 20 3d 20 28 69 36 34   i64 iEnd = (i64
183e1 29 69 53 72 63 50 67 2a 28 69 36 34 29 6e 53 72  )iSrcPg*(i64)nSr
183e2 63 50 67 73 7a 3b 0a 0a 20 20 69 6e 74 20 72 63  cPgsz;..  int rc
183e3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
183e4 69 36 34 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73  i64 iOff;..  ass
183e5 65 72 74 28 20 70 2d 3e 62 44 65 73 74 4c 6f 63  ert( p->bDestLoc
183e6 6b 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ked );.  assert(
183e7 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 70   !isFatalError(p
183e8 2d 3e 72 63 29 20 29 3b 0a 20 20 61 73 73 65 72  ->rc) );.  asser
183e9 74 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44 49  t( iSrcPg!=PENDI
183ea 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e  NG_BYTE_PAGE(p->
183eb 70 53 72 63 2d 3e 70 42 74 29 20 29 3b 0a 20 20  pSrc->pBt) );.  
183ec 61 73 73 65 72 74 28 20 7a 53 72 63 44 61 74 61  assert( zSrcData
183ed 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 74 63 68 20   );..  /* Catch 
183ee 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74  the case where t
183ef 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69  he destination i
183f0 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  s an in-memory d
183f1 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 0a  atabase and the.
183f2 20 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 73 20    ** page sizes 
183f3 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e  of the source an
183f4 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 69  d destination di
183f5 66 66 65 72 2e 20 0a 20 20 2a 2f 0a 20 20 69 66  ffer. .  */.  if
183f6 28 20 6e 53 72 63 50 67 73 7a 21 3d 6e 44 65 73  ( nSrcPgsz!=nDes
183f7 74 50 67 73 7a 20 26 26 20 73 71 6c 69 74 65 33  tPgsz && sqlite3
183f8 50 61 67 65 72 49 73 4d 65 6d 64 62 28 73 71 6c  PagerIsMemdb(sql
183f9 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
183fa 2d 3e 70 44 65 73 74 29 29 20 29 7b 0a 20 20 20  ->pDest)) ){.   
183fb 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   rc = SQLITE_REA
183fc 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  DONLY;.  }..  /*
183fd 20 54 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20   This loop runs 
183fe 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 64 65  once for each de
183ff 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73  stination page s
18400 70 61 6e 6e 65 64 20 62 79 20 74 68 65 20 73 6f  panned by the so
18401 75 72 63 65 20 0a 20 20 2a 2a 20 70 61 67 65 2e  urce .  ** page.
18402 20 46 6f 72 20 65 61 63 68 20 69 74 65 72 61 74   For each iterat
18403 69 6f 6e 2c 20 76 61 72 69 61 62 6c 65 20 69 4f  ion, variable iO
18404 66 66 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ff is set to the
18405 20 62 79 74 65 20 6f 66 66 73 65 74 0a 20 20 2a   byte offset.  *
18406 2a 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61  * of the destina
18407 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 2a 2f 0a  tion page..  */.
18408 20 20 66 6f 72 28 69 4f 66 66 3d 69 45 6e 64 2d    for(iOff=iEnd-
18409 28 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 20 72  (i64)nSrcPgsz; r
1840a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1840b 69 4f 66 66 3c 69 45 6e 64 3b 20 69 4f 66 66 2b  iOff<iEnd; iOff+
1840c 3d 6e 44 65 73 74 50 67 73 7a 29 7b 0a 20 20 20  =nDestPgsz){.   
1840d 20 44 62 50 61 67 65 20 2a 70 44 65 73 74 50 67   DbPage *pDestPg
1840e 20 3d 20 30 3b 0a 20 20 20 20 50 67 6e 6f 20 69   = 0;.    Pgno i
1840f 44 65 73 74 20 3d 20 28 50 67 6e 6f 29 28 69 4f  Dest = (Pgno)(iO
18410 66 66 2f 6e 44 65 73 74 50 67 73 7a 29 2b 31 3b  ff/nDestPgsz)+1;
18411 0a 20 20 20 20 69 66 28 20 69 44 65 73 74 3d 3d  .    if( iDest==
18412 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
18413 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29  E(p->pDest->pBt)
18414 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
18415 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
18416 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67  (rc = sqlite3Pag
18417 65 72 47 65 74 28 70 44 65 73 74 50 61 67 65 72  erGet(pDestPager
18418 2c 20 69 44 65 73 74 2c 20 26 70 44 65 73 74 50  , iDest, &pDestP
18419 67 29 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49  g)).     && SQLI
1841a 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
1841b 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
1841c 44 65 73 74 50 67 29 29 0a 20 20 20 20 29 7b 0a  DestPg)).    ){.
1841d 20 20 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a        const u8 *
1841e 7a 49 6e 20 3d 20 26 7a 53 72 63 44 61 74 61 5b  zIn = &zSrcData[
1841f 69 4f 66 66 25 6e 53 72 63 50 67 73 7a 5d 3b 0a  iOff%nSrcPgsz];.
18420 20 20 20 20 20 20 75 38 20 2a 7a 44 65 73 74 44        u8 *zDestD
18421 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ata = sqlite3Pag
18422 65 72 47 65 74 44 61 74 61 28 70 44 65 73 74 50  erGetData(pDestP
18423 67 29 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f  g);.      u8 *zO
18424 75 74 20 3d 20 26 7a 44 65 73 74 44 61 74 61 5b  ut = &zDestData[
18425 69 4f 66 66 25 6e 44 65 73 74 50 67 73 7a 5d 3b  iOff%nDestPgsz];
18426 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20  ..      /* Copy 
18427 74 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68  the data from th
18428 65 20 73 6f 75 72 63 65 20 70 61 67 65 20 69 6e  e source page in
18429 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  to the destinati
1842a 6f 6e 20 70 61 67 65 2e 0a 20 20 20 20 20 20 2a  on page..      *
1842b 2a 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65  * Then clear the
1842c 20 42 74 72 65 65 20 6c 61 79 65 72 20 4d 65 6d   Btree layer Mem
1842d 50 61 67 65 2e 69 73 49 6e 69 74 20 66 6c 61 67  Page.isInit flag
1842e 2e 20 42 6f 74 68 20 74 68 69 73 20 6d 6f 64 75  . Both this modu
1842f 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  le.      ** and 
18430 74 68 65 20 70 61 67 65 72 20 63 6f 64 65 20 75  the pager code u
18431 73 65 20 74 68 69 73 20 74 72 69 63 6b 20 28 63  se this trick (c
18432 6c 65 61 72 69 6e 67 20 74 68 65 20 66 69 72 73  learing the firs
18433 74 20 62 79 74 65 0a 20 20 20 20 20 20 2a 2a 20  t byte.      ** 
18434 6f 66 20 74 68 65 20 70 61 67 65 20 27 65 78 74  of the page 'ext
18435 72 61 27 20 73 70 61 63 65 20 74 6f 20 69 6e 76  ra' space to inv
18436 61 6c 69 64 61 74 65 20 74 68 65 20 42 74 72 65  alidate the Btre
18437 65 20 6c 61 79 65 72 73 0a 20 20 20 20 20 20 2a  e layers.      *
18438 2a 20 63 61 63 68 65 64 20 70 61 72 73 65 20 6f  * cached parse o
18439 66 20 74 68 65 20 70 61 67 65 29 2e 20 4d 65 6d  f the page). Mem
1843a 50 61 67 65 2e 69 73 49 6e 69 74 20 69 73 20 6d  Page.isInit is m
1843b 61 72 6b 65 64 20 0a 20 20 20 20 20 20 2a 2a 20  arked .      ** 
1843c 22 4d 55 53 54 20 42 45 20 46 49 52 53 54 22 20  "MUST BE FIRST" 
1843d 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73 65  for this purpose
1843e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1843f 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 7a 49   memcpy(zOut, zI
18440 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20  n, nCopy);.     
18441 20 28 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50   ((u8 *)sqlite3P
18442 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 65  agerGetExtra(pDe
18443 73 74 50 67 29 29 5b 30 5d 20 3d 20 30 3b 0a 20  stPg))[0] = 0;. 
18444 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
18445 50 61 67 65 72 55 6e 72 65 66 28 70 44 65 73 74  PagerUnref(pDest
18446 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  Pg);.  }..  retu
18447 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
18448 49 66 20 70 46 69 6c 65 20 69 73 20 63 75 72 72  If pFile is curr
18449 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 61  ently larger tha
1844a 6e 20 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74  n iSize bytes, t
1844b 68 65 6e 20 74 72 75 6e 63 61 74 65 20 69 74 20  hen truncate it 
1844c 74 6f 0a 2a 2a 20 65 78 61 63 74 6c 79 20 69 53  to.** exactly iS
1844d 69 7a 65 20 62 79 74 65 73 2e 20 49 66 20 70 46  ize bytes. If pF
1844e 69 6c 65 20 69 73 20 6e 6f 74 20 6c 61 72 67 65  ile is not large
1844f 72 20 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74  r than iSize byt
18450 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73  es, then.** this
18451 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
18452 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  o-op..**.** Retu
18453 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
18454 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75  everything is su
18455 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20  ccessful, or an 
18456 53 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a  SQLite error .**
18457 20 63 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f   code if an erro
18458 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61  r occurs..*/.sta
18459 74 69 63 20 69 6e 74 20 62 61 63 6b 75 70 54 72  tic int backupTr
1845a 75 6e 63 61 74 65 46 69 6c 65 28 73 71 6c 69 74  uncateFile(sqlit
1845b 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20  e3_file *pFile, 
1845c 69 36 34 20 69 53 69 7a 65 29 7b 0a 20 20 69 36  i64 iSize){.  i6
1845d 34 20 69 43 75 72 72 65 6e 74 3b 0a 20 20 69 6e  4 iCurrent;.  in
1845e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73  t rc = sqlite3Os
1845f 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2c 20  FileSize(pFile, 
18460 26 69 43 75 72 72 65 6e 74 29 3b 0a 20 20 69 66  &iCurrent);.  if
18461 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
18462 26 26 20 69 43 75 72 72 65 6e 74 3e 69 53 69 7a  && iCurrent>iSiz
18463 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  e ){.    rc = sq
18464 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28  lite3OsTruncate(
18465 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20  pFile, iSize);. 
18466 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
18467 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
18468 72 20 74 68 69 73 20 62 61 63 6b 75 70 20 6f 62  r this backup ob
18469 6a 65 63 74 20 77 69 74 68 20 74 68 65 20 61 73  ject with the as
1846a 73 6f 63 69 61 74 65 64 20 73 6f 75 72 63 65 20  sociated source 
1846b 70 61 67 65 72 20 66 6f 72 0a 2a 2a 20 63 61 6c  pager for.** cal
1846c 6c 62 61 63 6b 73 20 77 68 65 6e 20 70 61 67 65  lbacks when page
1846d 73 20 61 72 65 20 63 68 61 6e 67 65 64 20 6f 72  s are changed or
1846e 20 74 68 65 20 63 61 63 68 65 20 69 6e 76 61 6c   the cache inval
1846f 69 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  idated..*/.stati
18470 63 20 76 6f 69 64 20 61 74 74 61 63 68 42 61 63  c void attachBac
18471 6b 75 70 4f 62 6a 65 63 74 28 73 71 6c 69 74 65  kupObject(sqlite
18472 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20  3_backup *p){.  
18473 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
18474 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73  *pp;.  assert( s
18475 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
18476 4d 75 74 65 78 28 70 2d 3e 70 53 72 63 29 20 29  Mutex(p->pSrc) )
18477 3b 0a 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33  ;.  pp = sqlite3
18478 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 73  PagerBackupPtr(s
18479 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
1847a 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20 70 2d  (p->pSrc));.  p-
1847b 3e 70 4e 65 78 74 20 3d 20 2a 70 70 3b 0a 20 20  >pNext = *pp;.  
1847c 2a 70 70 20 3d 20 70 3b 0a 20 20 70 2d 3e 69 73  *pp = p;.  p->is
1847d 41 74 74 61 63 68 65 64 20 3d 20 31 3b 0a 7d 0a  Attached = 1;.}.
1847e 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e 50 61 67  ./*.** Copy nPag
1847f 65 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65  e pages from the
18480 20 73 6f 75 72 63 65 20 62 2d 74 72 65 65 20 74   source b-tree t
18481 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  o the destinatio
18482 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
18483 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63   int sqlite3_bac
18484 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33  kup_step(sqlite3
18485 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20  _backup *p, int 
18486 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63  nPage){.  int rc
18487 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
18488 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63  ex_enter(p->pSrc
18489 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71  Db->mutex);.  sq
1848a 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
1848b 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 20  p->pSrc);.  if( 
1848c 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20  p->pDestDb ){.  
1848d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1848e 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 62  enter(p->pDestDb
1848f 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20  ->mutex);.  }.. 
18490 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 69   rc = p->rc;.  i
18491 66 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72  f( !isFatalError
18492 28 72 63 29 20 29 7b 0a 20 20 20 20 50 61 67 65  (rc) ){.    Page
18493 72 20 2a 20 63 6f 6e 73 74 20 70 53 72 63 50 61  r * const pSrcPa
18494 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
18495 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29  eePager(p->pSrc)
18496 3b 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20  ;     /* Source 
18497 70 61 67 65 72 20 2a 2f 0a 20 20 20 20 50 61 67  pager */.    Pag
18498 65 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73 74  er * const pDest
18499 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
1849a 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65  treePager(p->pDe
1849b 73 74 29 3b 20 20 20 2f 2a 20 44 65 73 74 20 70  st);   /* Dest p
1849c 61 67 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ager */.    int 
1849d 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ii;             
1849e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1849f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
184a0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ble */.    int n
184a1 53 72 63 50 61 67 65 20 3d 20 2d 31 3b 20 20 20  SrcPage = -1;   
184a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
184a3 20 53 69 7a 65 20 6f 66 20 73 6f 75 72 63 65 20   Size of source 
184a4 64 62 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20  db in pages */. 
184a5 20 20 20 69 6e 74 20 62 43 6c 6f 73 65 54 72 61     int bCloseTra
184a6 6e 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ns = 0;         
184a7 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
184a8 20 73 72 63 20 64 62 20 72 65 71 75 69 72 65 73   src db requires
184a9 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20   unlocking */.. 
184aa 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f 75     /* If the sou
184ab 72 63 65 20 70 61 67 65 72 20 69 73 20 63 75 72  rce pager is cur
184ac 72 65 6e 74 6c 79 20 69 6e 20 61 20 77 72 69 74  rently in a writ
184ad 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 72  e-transaction, r
184ae 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20 53 51 4c  eturn.    ** SQL
184af 49 54 45 5f 42 55 53 59 20 69 6d 6d 65 64 69 61  ITE_BUSY immedia
184b0 74 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tely..    */.   
184b1 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20   if( p->pDestDb 
184b2 26 26 20 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d  && p->pSrc->pBt-
184b3 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
184b4 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20  TRANS_WRITE ){. 
184b5 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
184b6 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65  _BUSY;.    }else
184b7 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
184b8 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20  ITE_OK;.    }.. 
184b9 20 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 64     /* Lock the d
184ba 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
184bb 61 73 65 2c 20 69 66 20 69 74 20 69 73 20 6e 6f  ase, if it is no
184bc 74 20 6c 6f 63 6b 65 64 20 61 6c 72 65 61 64 79  t locked already
184bd 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c  . */.    if( SQL
184be 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d  ITE_OK==rc && p-
184bf 3e 62 44 65 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a  >bDestLocked==0.
184c0 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f       && SQLITE_O
184c1 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
184c2 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28  BtreeBeginTrans(
184c3 70 2d 3e 70 44 65 73 74 2c 20 32 29 29 20 0a 20  p->pDest, 2)) . 
184c4 20 20 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 62     ){.      p->b
184c5 44 65 73 74 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a  DestLocked = 1;.
184c6 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
184c7 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70  e3BtreeGetMeta(p
184c8 2d 3e 70 44 65 73 74 2c 20 31 2c 20 26 70 2d 3e  ->pDest, 1, &p->
184c9 69 44 65 73 74 53 63 68 65 6d 61 29 3b 0a 20 20  iDestSchema);.  
184ca 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
184cb 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20  here is no open 
184cc 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e  read-transaction
184cd 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   on the source d
184ce 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 0a 20 20  atabase, open.  
184cf 20 20 2a 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49 66    ** one now. If
184d0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
184d1 73 20 6f 70 65 6e 65 64 20 68 65 72 65 2c 20 74  s opened here, t
184d2 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65 20 63  hen it will be c
184d3 6c 6f 73 65 64 0a 20 20 20 20 2a 2a 20 62 65 66  losed.    ** bef
184d4 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
184d5 6e 20 65 78 69 74 73 2e 0a 20 20 20 20 2a 2f 0a  n exits..    */.
184d6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
184d7 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69  TE_OK && 0==sqli
184d8 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64  te3BtreeIsInRead
184d9 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 29 20 29  Trans(p->pSrc) )
184da 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
184db 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72  ite3BtreeBeginTr
184dc 61 6e 73 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b  ans(p->pSrc, 0);
184dd 0a 20 20 20 20 20 20 62 43 6c 6f 73 65 54 72 61  .      bCloseTra
184de 6e 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ns = 1;.    }.  
184df 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74  .    /* Now that
184e0 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64   there is a read
184e1 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 6f 75  -lock on the sou
184e2 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 71 75  rce database, qu
184e3 65 72 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 73  ery the.    ** s
184e4 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f 72 20  ource pager for 
184e5 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
184e6 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
184e7 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
184e8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
184e9 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
184ea 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
184eb 63 6f 75 6e 74 28 70 53 72 63 50 61 67 65 72 2c  count(pSrcPager,
184ec 20 26 6e 53 72 63 50 61 67 65 29 3b 0a 20 20 20   &nSrcPage);.   
184ed 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b   }.    for(ii=0;
184ee 20 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69 69 3c   (nPage<0 || ii<
184ef 6e 50 61 67 65 29 20 26 26 20 70 2d 3e 69 4e 65  nPage) && p->iNe
184f0 78 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63 50 61  xt<=(Pgno)nSrcPa
184f1 67 65 20 26 26 20 21 72 63 3b 20 69 69 2b 2b 29  ge && !rc; ii++)
184f2 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67  {.      const Pg
184f3 6e 6f 20 69 53 72 63 50 67 20 3d 20 70 2d 3e 69  no iSrcPg = p->i
184f4 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  Next;           
184f5 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20        /* Source 
184f6 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  page number */. 
184f7 20 20 20 20 20 69 66 28 20 69 53 72 63 50 67 21       if( iSrcPg!
184f8 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
184f9 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29  GE(p->pSrc->pBt)
184fa 20 29 7b 0a 20 20 20 20 20 20 20 20 44 62 50 61   ){.        DbPa
184fb 67 65 20 2a 70 53 72 63 50 67 3b 20 20 20 20 20  ge *pSrcPg;     
184fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
184fd 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63          /* Sourc
184fe 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 2a 2f  e page object */
184ff 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
18500 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 53  lite3PagerGet(pS
18501 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 2c  rcPager, iSrcPg,
18502 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20   &pSrcPg);.     
18503 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
18504 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
18505 20 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65    rc = backupOne
18506 50 61 67 65 28 70 2c 20 69 53 72 63 50 67 2c 20  Page(p, iSrcPg, 
18507 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
18508 61 74 61 28 70 53 72 63 50 67 29 29 3b 0a 20 20  ata(pSrcPg));.  
18509 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
1850a 61 67 65 72 55 6e 72 65 66 28 70 53 72 63 50 67  agerUnref(pSrcPg
1850b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1850c 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 69 4e     }.      p->iN
1850d 65 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  ext++;.    }.   
1850e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1850f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e  OK ){.      p->n
18510 50 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53 72 63  Pagecount = nSrc
18511 50 61 67 65 3b 0a 20 20 20 20 20 20 70 2d 3e 6e  Page;.      p->n
18512 52 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53 72 63  Remaining = nSrc
18513 50 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 3b  Page+1-p->iNext;
18514 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 4e  .      if( p->iN
18515 65 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63 50 61  ext>(Pgno)nSrcPa
18516 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  ge ){.        rc
18517 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a   = SQLITE_DONE;.
18518 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
18519 21 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 29  !p->isAttached )
1851a 7b 0a 20 20 20 20 20 20 20 20 61 74 74 61 63 68  {.        attach
1851b 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 70 29 3b  BackupObject(p);
1851c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1851d 20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51   .    if( rc==SQ
1851e 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
1851f 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72     const int nSr
18520 63 50 61 67 65 73 69 7a 65 20 3d 20 73 71 6c 69  cPagesize = sqli
18521 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
18522 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20  ize(p->pSrc);.  
18523 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44      const int nD
18524 65 73 74 50 61 67 65 73 69 7a 65 20 3d 20 73 71  estPagesize = sq
18525 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
18526 65 53 69 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b  eSize(p->pDest);
18527 0a 20 20 20 20 20 20 69 6e 74 20 6e 44 65 73 74  .      int nDest
18528 54 72 75 6e 63 61 74 65 3b 0a 20 20 0a 20 20 20  Truncate;.  .   
18529 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
1852a 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20   schema version 
1852b 66 69 65 6c 64 20 69 6e 20 74 68 65 20 64 65 73  field in the des
1852c 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
1852d 65 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a  e. This.      **
1852e 20 69 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65   is to make sure
1852f 20 74 68 61 74 20 74 68 65 20 73 63 68 65 6d 61   that the schema
18530 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c 6c 79 20  -version really 
18531 64 6f 65 73 20 63 68 61 6e 67 65 20 69 6e 0a 20  does change in. 
18532 20 20 20 20 20 2a 2a 20 74 68 65 20 63 61 73 65       ** the case
18533 20 77 68 65 72 65 20 74 68 65 20 73 6f 75 72 63   where the sourc
18534 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f  e and destinatio
18535 6e 20 64 61 74 61 62 61 73 65 73 20 68 61 76 65  n databases have
18536 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 61   the.      ** sa
18537 6d 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f  me schema versio
18538 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n..      */.    
18539 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70    sqlite3BtreeUp
1853a 64 61 74 65 4d 65 74 61 28 70 2d 3e 70 44 65 73  dateMeta(p->pDes
1853b 74 2c 20 31 2c 20 70 2d 3e 69 44 65 73 74 53 63  t, 1, p->iDestSc
1853c 68 65 6d 61 2b 31 29 3b 0a 20 20 20 20 20 20 69  hema+1);.      i
1853d 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b  f( p->pDestDb ){
1853e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1853f 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
18540 65 6d 61 28 70 2d 3e 70 44 65 73 74 44 62 2c 20  ema(p->pDestDb, 
18541 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  0);.      }..   
18542 20 20 20 2f 2a 20 53 65 74 20 6e 44 65 73 74 54     /* Set nDestT
18543 72 75 6e 63 61 74 65 20 74 6f 20 74 68 65 20 66  runcate to the f
18544 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  inal number of p
18545 61 67 65 73 20 69 6e 20 74 68 65 20 64 65 73 74  ages in the dest
18546 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  ination.      **
18547 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 63   database. The c
18548 6f 6d 70 6c 69 63 61 74 69 6f 6e 20 68 65 72 65  omplication here
18549 20 69 73 20 74 68 61 74 20 74 68 65 20 64 65 73   is that the des
1854a 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 0a 20 20  tination page.  
1854b 20 20 20 20 2a 2a 20 73 69 7a 65 20 6d 61 79 20      ** size may 
1854c 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20  be different to 
1854d 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 20  the source page 
1854e 73 69 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2a 0a  size. .      **.
1854f 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20        ** If the 
18550 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a 65  source page size
18551 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e   is smaller than
18552 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
18553 20 70 61 67 65 20 73 69 7a 65 2c 20 0a 20 20 20   page size, .   
18554 20 20 20 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20     ** round up. 
18555 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
18556 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
18557 4f 73 54 72 75 6e 63 61 74 65 28 29 20 62 65 6c  OsTruncate() bel
18558 6f 77 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a  ow will.      **
18559 20 66 69 78 20 74 68 65 20 73 69 7a 65 20 6f 66   fix the size of
1855a 20 74 68 65 20 66 69 6c 65 2e 20 48 6f 77 65 76   the file. Howev
1855b 65 72 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61  er it is importa
1855c 6e 74 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20  nt to call.     
1855d 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72   ** sqlite3Pager
1855e 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20  TruncateImage() 
1855f 68 65 72 65 20 73 6f 20 74 68 61 74 20 61 6e 79  here so that any
18560 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 0a 20   pages in the . 
18561 20 20 20 20 20 2a 2a 20 64 65 73 74 69 6e 61 74       ** destinat
18562 69 6f 6e 20 66 69 6c 65 20 74 68 61 74 20 6c 69  ion file that li
18563 65 20 62 65 79 6f 6e 64 20 74 68 65 20 6e 44 65  e beyond the nDe
18564 73 74 54 72 75 6e 63 61 74 65 20 70 61 67 65 20  stTruncate page 
18565 6d 61 72 6b 20 61 72 65 0a 20 20 20 20 20 20 2a  mark are.      *
18566 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 79 20  * journalled by 
18567 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
18568 4f 6e 65 28 29 20 62 65 66 6f 72 65 20 74 68 65  One() before the
18569 79 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a  y are destroyed.
1856a 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20        ** by the 
1856b 66 69 6c 65 20 74 72 75 6e 63 61 74 69 6f 6e 2e  file truncation.
1856c 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1856d 69 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65  if( nSrcPagesize
1856e 3c 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 29  <nDestPagesize )
1856f 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 61  {.        int ra
18570 74 69 6f 20 3d 20 6e 44 65 73 74 50 61 67 65 73  tio = nDestPages
18571 69 7a 65 2f 6e 53 72 63 50 61 67 65 73 69 7a 65  ize/nSrcPagesize
18572 3b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74 54  ;.        nDestT
18573 72 75 6e 63 61 74 65 20 3d 20 28 6e 53 72 63 50  runcate = (nSrcP
18574 61 67 65 2b 72 61 74 69 6f 2d 31 29 2f 72 61 74  age+ratio-1)/rat
18575 69 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  io;.        if( 
18576 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 28  nDestTruncate==(
18577 69 6e 74 29 50 45 4e 44 49 4e 47 5f 42 59 54 45  int)PENDING_BYTE
18578 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e  _PAGE(p->pDest->
18579 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pBt) ){.        
1857a 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 2d    nDestTruncate-
1857b 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  -;.        }.   
1857c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1857d 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20    nDestTruncate 
1857e 3d 20 6e 53 72 63 50 61 67 65 20 2a 20 28 6e 53  = nSrcPage * (nS
1857f 72 63 50 61 67 65 73 69 7a 65 2f 6e 44 65 73 74  rcPagesize/nDest
18580 50 61 67 65 73 69 7a 65 29 3b 0a 20 20 20 20 20  Pagesize);.     
18581 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
18582 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61  PagerTruncateIma
18583 67 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 6e  ge(pDestPager, n
18584 44 65 73 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a  DestTruncate);..
18585 20 20 20 20 20 20 69 66 28 20 6e 53 72 63 50 61        if( nSrcPa
18586 67 65 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 65  gesize<nDestPage
18587 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20  size ){.        
18588 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65  /* If the source
18589 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 73 6d   page-size is sm
1858a 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64  aller than the d
1858b 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2d  estination page-
1858c 73 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a  size,.        **
1858d 20 74 77 6f 20 65 78 74 72 61 20 74 68 69 6e 67   two extra thing
1858e 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 68 61  s may need to ha
1858f 70 70 65 6e 3a 0a 20 20 20 20 20 20 20 20 2a 2a  ppen:.        **
18590 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a 20  .        **   * 
18591 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  The destination 
18592 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 74  may need to be t
18593 72 75 6e 63 61 74 65 64 2c 20 61 6e 64 0a 20 20  runcated, and.  
18594 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
18595 20 2a 2a 20 20 20 2a 20 44 61 74 61 20 73 74 6f   **   * Data sto
18596 72 65 64 20 6f 6e 20 74 68 65 20 70 61 67 65 73  red on the pages
18597 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c   immediately fol
18598 6c 6f 77 69 6e 67 20 74 68 65 20 0a 20 20 20 20  lowing the .    
18599 20 20 20 20 2a 2a 20 20 20 20 20 70 65 6e 64 69      **     pendi
1859a 6e 67 2d 62 79 74 65 20 70 61 67 65 20 69 6e 20  ng-byte page in 
1859b 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
1859c 61 73 65 20 6d 61 79 20 6e 65 65 64 20 74 6f 20  ase may need to 
1859d 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  be.        **   
1859e 20 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68    copied into th
1859f 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
185a0 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20  tabase..        
185a1 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  */.        const
185a2 20 69 36 34 20 69 53 69 7a 65 20 3d 20 28 69 36   i64 iSize = (i6
185a3 34 29 6e 53 72 63 50 61 67 65 73 69 7a 65 20 2a  4)nSrcPagesize *
185a4 20 28 69 36 34 29 6e 53 72 63 50 61 67 65 3b 0a   (i64)nSrcPage;.
185a5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
185a6 66 69 6c 65 20 2a 20 63 6f 6e 73 74 20 70 46 69  file * const pFi
185a7 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  le = sqlite3Page
185a8 72 46 69 6c 65 28 70 44 65 73 74 50 61 67 65 72  rFile(pDestPager
185a9 29 3b 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65  );..        asse
185aa 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 20  rt( pFile );.   
185ab 20 20 20 20 20 61 73 73 65 72 74 28 20 28 69 36       assert( (i6
185ac 34 29 6e 44 65 73 74 54 72 75 6e 63 61 74 65 2a  4)nDestTruncate*
185ad 28 69 36 34 29 6e 44 65 73 74 50 61 67 65 73 69  (i64)nDestPagesi
185ae 7a 65 20 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 28  ze >= iSize || (
185af 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e  .              n
185b0 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69  DestTruncate==(i
185b1 6e 74 29 28 50 45 4e 44 49 4e 47 5f 42 59 54 45  nt)(PENDING_BYTE
185b2 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e  _PAGE(p->pDest->
185b3 70 42 74 29 2d 31 29 0a 20 20 20 20 20 20 20 20  pBt)-1).        
185b4 20 20 20 26 26 20 69 53 69 7a 65 3e 3d 50 45 4e     && iSize>=PEN
185b5 44 49 4e 47 5f 42 59 54 45 20 26 26 20 69 53 69  DING_BYTE && iSi
185b6 7a 65 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  ze<=PENDING_BYTE
185b7 2b 6e 44 65 73 74 50 61 67 65 73 69 7a 65 0a 20  +nDestPagesize. 
185b8 20 20 20 20 20 20 20 29 29 3b 0a 20 20 20 20 20         ));.     
185b9 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
185ba 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50  ==(rc = sqlite3P
185bb 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f  agerCommitPhaseO
185bc 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 30  ne(pDestPager, 0
185bd 2c 20 31 29 29 0a 20 20 20 20 20 20 20 20 20 26  , 1)).         &
185be 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  & SQLITE_OK==(rc
185bf 20 3d 20 62 61 63 6b 75 70 54 72 75 6e 63 61 74   = backupTruncat
185c0 65 46 69 6c 65 28 70 46 69 6c 65 2c 20 69 53 69  eFile(pFile, iSi
185c1 7a 65 29 29 0a 20 20 20 20 20 20 20 20 20 26 26  ze)).         &&
185c2 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
185c3 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  = sqlite3PagerSy
185c4 6e 63 28 70 44 65 73 74 50 61 67 65 72 29 29 0a  nc(pDestPager)).
185c5 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
185c6 20 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a 20       i64 iOff;. 
185c7 20 20 20 20 20 20 20 20 20 69 36 34 20 69 45 6e           i64 iEn
185c8 64 20 3d 20 4d 49 4e 28 50 45 4e 44 49 4e 47 5f  d = MIN(PENDING_
185c9 42 59 54 45 20 2b 20 6e 44 65 73 74 50 61 67 65  BYTE + nDestPage
185ca 73 69 7a 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20  size, iSize);.  
185cb 20 20 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20          for(.   
185cc 20 20 20 20 20 20 20 20 20 69 4f 66 66 3d 50 45           iOff=PE
185cd 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 53 72 63 50  NDING_BYTE+nSrcP
185ce 61 67 65 73 69 7a 65 3b 20 0a 20 20 20 20 20 20  agesize; .      
185cf 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45        rc==SQLITE
185d0 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e 64  _OK && iOff<iEnd
185d1 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ; .            i
185d2 4f 66 66 2b 3d 6e 53 72 63 50 61 67 65 73 69 7a  Off+=nSrcPagesiz
185d3 65 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20  e.          ){. 
185d4 20 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72             PgHdr
185d5 20 2a 70 53 72 63 50 67 20 3d 20 30 3b 0a 20 20   *pSrcPg = 0;.  
185d6 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
185d7 50 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 28 50  Pgno iSrcPg = (P
185d8 67 6e 6f 29 28 28 69 4f 66 66 2f 6e 53 72 63 50  gno)((iOff/nSrcP
185d9 61 67 65 73 69 7a 65 29 2b 31 29 3b 0a 20 20 20  agesize)+1);.   
185da 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
185db 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 53  lite3PagerGet(pS
185dc 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 2c  rcPager, iSrcPg,
185dd 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20   &pSrcPg);.     
185de 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
185df 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
185e0 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 7a 44            u8 *zD
185e1 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ata = sqlite3Pag
185e2 65 72 47 65 74 44 61 74 61 28 70 53 72 63 50 67  erGetData(pSrcPg
185e3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
185e4 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
185e5 72 69 74 65 28 70 46 69 6c 65 2c 20 7a 44 61 74  rite(pFile, zDat
185e6 61 2c 20 6e 53 72 63 50 61 67 65 73 69 7a 65 2c  a, nSrcPagesize,
185e7 20 69 4f 66 66 29 3b 0a 20 20 20 20 20 20 20 20   iOff);.        
185e8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
185e9 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
185ea 72 65 66 28 70 53 72 63 50 67 29 3b 0a 20 20 20  ref(pSrcPg);.   
185eb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
185ec 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
185ed 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
185ee 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
185ef 68 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67  haseOne(pDestPag
185f0 65 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  er, 0, 0);.     
185f1 20 7d 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 46   }.  .      /* F
185f2 69 6e 69 73 68 20 63 6f 6d 6d 69 74 74 69 6e 67  inish committing
185f3 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
185f4 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74   to the destinat
185f5 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 20 2a 2f  ion database. */
185f6 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
185f7 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20  E_OK==rc.       
185f8 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
185f9 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
185fa 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70  CommitPhaseTwo(p
185fb 2d 3e 70 44 65 73 74 29 29 0a 20 20 20 20 20 20  ->pDest)).      
185fc 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
185fd 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
185fe 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
185ff 20 20 2f 2a 20 49 66 20 62 43 6c 6f 73 65 54 72    /* If bCloseTr
18600 61 6e 73 20 69 73 20 74 72 75 65 2c 20 74 68 65  ans is true, the
18601 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
18602 6f 70 65 6e 65 64 20 61 20 72 65 61 64 20 74 72  opened a read tr
18603 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a  ansaction.    **
18604 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   on the source d
18605 61 74 61 62 61 73 65 2e 20 43 6c 6f 73 65 20 74  atabase. Close t
18606 68 65 20 72 65 61 64 20 74 72 61 6e 73 61 63 74  he read transact
18607 69 6f 6e 20 68 65 72 65 2e 20 54 68 65 72 65 20  ion here. There 
18608 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 20 6e 65 65  is.    ** no nee
18609 64 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72  d to check the r
1860a 65 74 75 72 6e 20 76 61 6c 75 65 73 20 6f 66 20  eturn values of 
1860b 74 68 65 20 62 74 72 65 65 20 6d 65 74 68 6f 64  the btree method
1860c 73 20 68 65 72 65 2c 20 61 73 0a 20 20 20 20 2a  s here, as.    *
1860d 2a 20 22 63 6f 6d 6d 69 74 74 69 6e 67 22 20 61  * "committing" a
1860e 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73   read-only trans
1860f 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61  action cannot fa
18610 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  il..    */.    i
18611 66 28 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 29  f( bCloseTrans )
18612 7b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59  {.      TESTONLY
18613 28 20 69 6e 74 20 72 63 32 20 29 3b 0a 20 20 20  ( int rc2 );.   
18614 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32     TESTONLY( rc2
18615 20 20 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72    = ) sqlite3Btr
18616 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
18617 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20  (p->pSrc, 0);.  
18618 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63      TESTONLY( rc
18619 32 20 7c 3d 20 29 20 73 71 6c 69 74 65 33 42 74  2 |= ) sqlite3Bt
1861a 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  reeCommitPhaseTw
1861b 6f 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20  o(p->pSrc);.    
1861c 20 20 61 73 73 65 72 74 28 20 72 63 32 3d 3d 53    assert( rc2==S
1861d 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
1861e 7d 0a 20 20 0a 20 20 20 20 70 2d 3e 72 63 20 3d  }.  .    p->rc =
1861f 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70   rc;.  }.  if( p
18620 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20  ->pDestDb ){.   
18621 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
18622 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d  eave(p->pDestDb-
18623 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73  >mutex);.  }.  s
18624 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
18625 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c  (p->pSrc);.  sql
18626 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
18627 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65  (p->pSrcDb->mute
18628 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
18629 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73  .}../*.** Releas
1862a 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20  e all resources 
1862b 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1862c 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  an sqlite3_backu
1862d 70 2a 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  p* handle..*/.SQ
1862e 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1862f 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
18630 73 68 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  sh(sqlite3_backu
18631 70 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  p *p){.  sqlite3
18632 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20  _backup **pp;   
18633 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18634 20 50 74 72 20 74 6f 20 68 65 61 64 20 6f 66 20   Ptr to head of 
18635 70 61 67 65 72 73 20 62 61 63 6b 75 70 20 6c 69  pagers backup li
18636 73 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  st */.  sqlite3_
18637 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20  mutex *mutex;   
18638 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18639 4d 75 74 65 78 20 74 6f 20 70 72 6f 74 65 63 74  Mutex to protect
1863a 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
1863b 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
1863c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1863d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
1863e 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  lue to return */
1863f 0a 0a 20 20 2f 2a 20 45 6e 74 65 72 20 74 68 65  ..  /* Enter the
18640 20 6d 75 74 65 78 65 73 20 2a 2f 0a 20 20 73 71   mutexes */.  sq
18641 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
18642 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74  r(p->pSrcDb->mut
18643 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ex);.  sqlite3Bt
18644 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 53 72 63  reeEnter(p->pSrc
18645 29 3b 0a 20 20 6d 75 74 65 78 20 3d 20 70 2d 3e  );.  mutex = p->
18646 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 3b 0a 20  pSrcDb->mutex;. 
18647 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20   if( p->pDestDb 
18648 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
18649 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44  utex_enter(p->pD
1864a 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  estDb->mutex);. 
1864b 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 61 63 68 20   }..  /* Detach 
1864c 74 68 69 73 20 62 61 63 6b 75 70 20 66 72 6f 6d  this backup from
1864d 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65   the source page
1864e 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  r. */.  if( p->p
1864f 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 70 2d  DestDb ){.    p-
18650 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d  >pSrc->nBackup--
18651 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69  ;.  }.  if( p->i
18652 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20 20 20  sAttached ){.   
18653 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67   pp = sqlite3Pag
18654 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 6c 69  erBackupPtr(sqli
18655 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d  te3BtreePager(p-
18656 3e 70 53 72 63 29 29 3b 0a 20 20 20 20 77 68 69  >pSrc));.    whi
18657 6c 65 28 20 2a 70 70 21 3d 70 20 29 7b 0a 20 20  le( *pp!=p ){.  
18658 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d      pp = &(*pp)-
18659 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
1865a 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74    *pp = p->pNext
1865b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61  ;.  }..  /* If a
1865c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1865d 73 74 69 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68  still open on th
1865e 65 20 42 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74  e Btree, roll it
1865f 20 62 61 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69   back. */.  sqli
18660 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b  te3BtreeRollback
18661 28 70 2d 3e 70 44 65 73 74 29 3b 0a 0a 20 20 2f  (p->pDest);..  /
18662 2a 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20  * Set the error 
18663 63 6f 64 65 20 6f 66 20 74 68 65 20 64 65 73 74  code of the dest
18664 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
18665 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 72 63   handle. */.  rc
18666 20 3d 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54   = (p->rc==SQLIT
18667 45 5f 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45  E_DONE) ? SQLITE
18668 5f 4f 4b 20 3a 20 70 2d 3e 72 63 3b 0a 20 20 73  _OK : p->rc;.  s
18669 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 70  qlite3Error(p->p
1866a 44 65 73 74 44 62 2c 20 72 63 2c 20 30 29 3b 0a  DestDb, rc, 0);.
1866b 0a 20 20 2f 2a 20 45 78 69 74 20 74 68 65 20 6d  .  /* Exit the m
1866c 75 74 65 78 65 73 20 61 6e 64 20 66 72 65 65 20  utexes and free 
1866d 74 68 65 20 62 61 63 6b 75 70 20 63 6f 6e 74 65  the backup conte
1866e 78 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f  xt structure. */
1866f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44  .  if( p->pDestD
18670 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
18671 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
18672 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b  pDestDb->mutex);
18673 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
18674 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63  reeLeave(p->pSrc
18675 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73  );.  if( p->pDes
18676 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  tDb ){.    sqlit
18677 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a  e3_free(p);.  }.
18678 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
18679 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20  leave(mutex);.  
1867a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1867b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1867c 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73  umber of pages s
1867d 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b 65  till to be backe
1867e 64 20 75 70 20 61 73 20 6f 66 20 74 68 65 20 6d  d up as of the m
1867f 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61  ost recent.** ca
18680 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ll to sqlite3_ba
18681 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a  ckup_step()..*/.
18682 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
18683 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65  qlite3_backup_re
18684 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f  maining(sqlite3_
18685 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65  backup *p){.  re
18686 74 75 72 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69  turn p->nRemaini
18687 6e 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ng;.}../*.** Ret
18688 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  urn the total nu
18689 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
1868a 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
1868b 62 61 73 65 20 61 73 20 6f 66 20 74 68 65 20 6d  base as of the m
1868c 6f 73 74 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63  ost .** recent c
1868d 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  all to sqlite3_b
1868e 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f  ackup_step()..*/
1868f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
18690 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70  sqlite3_backup_p
18691 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  agecount(sqlite3
18692 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72  _backup *p){.  r
18693 65 74 75 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f  eturn p->nPageco
18694 75 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  unt;.}../*.** Th
18695 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
18696 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20  alled after the 
18697 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65  contents of page
18698 20 69 50 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a   iPage of the.**
18699 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
1869a 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66   have been modif
1869b 69 65 64 2e 20 49 66 20 70 61 67 65 20 69 50 61  ied. If page iPa
1869c 67 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ge has already b
1869d 65 65 6e 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69  een .** copied i
1869e 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74  nto the destinat
1869f 69 6f 6e 20 64 61 74 61 62 61 73 65 2c 20 74 68  ion database, th
186a0 65 6e 20 74 68 65 20 64 61 74 61 20 77 72 69 74  en the data writ
186a1 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65  ten to the.** de
186a2 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77  stination is now
186a3 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 54 68   invalidated. Th
186a4 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f  e destination co
186a5 70 79 20 6f 66 20 69 50 61 67 65 20 6e 65 65 64  py of iPage need
186a6 73 0a 2a 2a 20 74 6f 20 62 65 20 75 70 64 61 74  s.** to be updat
186a7 65 64 20 77 69 74 68 20 74 68 65 20 6e 65 77 20  ed with the new 
186a8 64 61 74 61 20 62 65 66 6f 72 65 20 74 68 65 20  data before the 
186a9 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
186aa 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e   is.** complete.
186ab 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73  .**.** It is ass
186ac 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 6d 75  umed that the mu
186ad 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  tex associated w
186ae 69 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64  ith the BtShared
186af 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65   object.** corre
186b0 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20  sponding to the 
186b1 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
186b2 69 73 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69  is held when thi
186b3 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a  s function is.**
186b4 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49   called..*/.SQLI
186b5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
186b6 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64  sqlite3BackupUpd
186b7 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ate(sqlite3_back
186b8 75 70 20 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e  up *pBackup, Pgn
186b9 6f 20 69 50 61 67 65 2c 20 63 6f 6e 73 74 20 75  o iPage, const u
186ba 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 73 71 6c  8 *aData){.  sql
186bb 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20  ite3_backup *p; 
186bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
186bd 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
186be 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28  riable */.  for(
186bf 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d  p=pBackup; p; p=
186c0 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61  p->pNext){.    a
186c1 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
186c2 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72  utex_held(p->pSr
186c3 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  c->pBt->mutex) )
186c4 3b 0a 20 20 20 20 69 66 28 20 21 69 73 46 61 74  ;.    if( !isFat
186c5 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 26  alError(p->rc) &
186c6 26 20 69 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74  & iPage<p->iNext
186c7 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65   ){.      /* The
186c8 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20   backup process 
186c9 70 20 68 61 73 20 61 6c 72 65 61 64 79 20 63 6f  p has already co
186ca 70 69 65 64 20 70 61 67 65 20 69 50 61 67 65 2e  pied page iPage.
186cb 20 42 75 74 20 6e 6f 77 20 69 74 0a 20 20 20 20   But now it.    
186cc 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f    ** has been mo
186cd 64 69 66 69 65 64 20 62 79 20 61 20 74 72 61 6e  dified by a tran
186ce 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73  saction on the s
186cf 6f 75 72 63 65 20 70 61 67 65 72 2e 20 43 6f 70  ource pager. Cop
186d0 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e  y.      ** the n
186d1 65 77 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65  ew data into the
186d2 20 62 61 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a   backup..      *
186d3 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d  /.      int rc =
186d4 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 70   backupOnePage(p
186d5 2c 20 69 50 61 67 65 2c 20 61 44 61 74 61 29 3b  , iPage, aData);
186d6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
186d7 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26  c!=SQLITE_BUSY &
186d8 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43  & rc!=SQLITE_LOC
186d9 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28  KED );.      if(
186da 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
186db 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20  {.        p->rc 
186dc 3d 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = rc;.      }.  
186dd 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
186de 20 52 65 73 74 61 72 74 20 74 68 65 20 62 61 63   Restart the bac
186df 6b 75 70 20 70 72 6f 63 65 73 73 2e 20 54 68 69  kup process. Thi
186e0 73 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  s is called when
186e1 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72   the pager layer
186e2 0a 2a 2a 20 64 65 74 65 63 74 73 20 74 68 61 74  .** detects that
186e3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
186e4 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  s been modified 
186e5 62 79 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 64  by an external d
186e6 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65  atabase.** conne
186e7 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63  ction. In this c
186e8 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ase there is no 
186e9 77 61 79 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77  way of knowing w
186ea 68 69 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70  hich of the.** p
186eb 61 67 65 73 20 74 68 61 74 20 68 61 76 65 20 62  ages that have b
186ec 65 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  een copied into 
186ed 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
186ee 64 61 74 61 62 61 73 65 20 61 72 65 20 73 74 69  database are sti
186ef 6c 6c 20 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64  ll .** valid and
186f0 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 2c 20   which are not, 
186f1 73 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 72  so the entire pr
186f2 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62  ocess needs to b
186f3 65 20 72 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a  e restarted..**.
186f4 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  ** It is assumed
186f5 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20   that the mutex 
186f6 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
186f7 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
186f8 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  ect.** correspon
186f9 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72  ding to the sour
186fa 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68  ce database is h
186fb 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75  eld when this fu
186fc 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c  nction is.** cal
186fd 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  led..*/.SQLITE_P
186fe 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
186ff 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74  te3BackupRestart
18700 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
18701 2a 70 42 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c  *pBackup){.  sql
18702 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20  ite3_backup *p; 
18703 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18704 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
18705 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28  riable */.  for(
18706 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d  p=pBackup; p; p=
18707 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61  p->pNext){.    a
18708 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
18709 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72  utex_held(p->pSr
1870a 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  c->pBt->mutex) )
1870b 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d  ;.    p->iNext =
1870c 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64   1;.  }.}..#ifnd
1870d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1870e 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79  ACUUM./*.** Copy
1870f 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f   the complete co
18710 6e 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d  ntent of pBtFrom
18711 20 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41 20   into pBtTo.  A 
18712 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d  transaction.** m
18713 75 73 74 20 62 65 20 61 63 74 69 76 65 20 66 6f  ust be active fo
18714 72 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a  r both files..**
18715 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20  .** The size of 
18716 66 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65 20  file pTo may be 
18717 72 65 64 75 63 65 64 20 62 79 20 74 68 69 73 20  reduced by this 
18718 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e  operation. If an
18719 79 74 68 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20  ything .** goes 
1871a 77 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73  wrong, the trans
1871b 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73  action on pTo is
1871c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66   rolled back. If
1871d 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65   successful, the
1871e 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e   .** transaction
1871f 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 65   is committed be
18720 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
18721 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18722 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
18723 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65  eeCopyFile(Btree
18724 20 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46   *pTo, Btree *pF
18725 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  rom){.  int rc;.
18726 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
18727 20 62 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   b;.  sqlite3Btr
18728 65 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a 20 20  eeEnter(pTo);.  
18729 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1872a 72 28 70 46 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20  r(pFrom);..  /* 
1872b 53 65 74 20 75 70 20 61 6e 20 73 71 6c 69 74 65  Set up an sqlite
1872c 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e  3_backup object.
1872d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2e   sqlite3_backup.
1872e 70 44 65 73 74 44 62 20 6d 75 73 74 20 62 65 20  pDestDb must be 
1872f 73 65 74 0a 20 20 2a 2a 20 74 6f 20 30 2e 20 54  set.  ** to 0. T
18730 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74  his is used by t
18731 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
18732 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 62 61  ns of sqlite3_ba
18733 63 6b 75 70 5f 73 74 65 70 28 29 0a 20 20 2a 2a  ckup_step().  **
18734 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63   and sqlite3_bac
18735 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20  kup_finish() to 
18736 64 65 74 65 63 74 20 74 68 61 74 20 74 68 65 79  detect that they
18737 20 61 72 65 20 62 65 69 6e 67 20 63 61 6c 6c 65   are being calle
18738 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 69 73  d.  ** from this
18739 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74 20 64   function, not d
1873a 69 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 75  irectly by the u
1873b 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73  ser..  */.  mems
1873c 65 74 28 26 62 2c 20 30 2c 20 73 69 7a 65 6f 66  et(&b, 0, sizeof
1873d 28 62 29 29 3b 0a 20 20 62 2e 70 53 72 63 44 62  (b));.  b.pSrcDb
1873e 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20   = pFrom->db;.  
1873f 62 2e 70 53 72 63 20 3d 20 70 46 72 6f 6d 3b 0a  b.pSrc = pFrom;.
18740 20 20 62 2e 70 44 65 73 74 20 3d 20 70 54 6f 3b    b.pDest = pTo;
18741 0a 20 20 62 2e 69 4e 65 78 74 20 3d 20 31 3b 0a  .  b.iNext = 1;.
18742 0a 20 20 2f 2a 20 30 78 37 46 46 46 46 46 46 46  .  /* 0x7FFFFFFF
18743 20 69 73 20 74 68 65 20 68 61 72 64 20 6c 69 6d   is the hard lim
18744 69 74 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65  it for the numbe
18745 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 20  r of pages in a 
18746 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69  database.  ** fi
18747 6c 65 2e 20 42 79 20 70 61 73 73 69 6e 67 20 74  le. By passing t
18748 68 69 73 20 61 73 20 74 68 65 20 6e 75 6d 62 65  his as the numbe
18749 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 63 6f  r of pages to co
1874a 70 79 20 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74  py to.  ** sqlit
1874b 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
1874c 2c 20 77 65 20 63 61 6e 20 67 75 61 72 61 6e 74  , we can guarant
1874d 65 65 20 74 68 61 74 20 74 68 65 20 63 6f 70 79  ee that the copy
1874e 20 66 69 6e 69 73 68 65 73 20 0a 20 20 2a 2a 20   finishes .  ** 
1874f 77 69 74 68 69 6e 20 61 20 73 69 6e 67 6c 65 20  within a single 
18750 63 61 6c 6c 20 28 75 6e 6c 65 73 73 20 61 6e 20  call (unless an 
18751 65 72 72 6f 72 20 6f 63 63 75 72 73 29 2e 20 54  error occurs). T
18752 68 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  he assert() stat
18753 65 6d 65 6e 74 0a 20 20 2a 2a 20 63 68 65 63 6b  ement.  ** check
18754 73 20 74 68 69 73 20 61 73 73 75 6d 70 74 69 6f  s this assumptio
18755 6e 20 2d 20 28 70 2d 3e 72 63 29 20 73 68 6f 75  n - (p->rc) shou
18756 6c 64 20 62 65 20 73 65 74 20 74 6f 20 65 69 74  ld be set to eit
18757 68 65 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  her SQLITE_DONE 
18758 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72 72 6f  .  ** or an erro
18759 72 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73  r code..  */.  s
1875a 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
1875b 65 70 28 26 62 2c 20 30 78 37 46 46 46 46 46 46  ep(&b, 0x7FFFFFF
1875c 46 29 3b 0a 20 20 61 73 73 65 72 74 28 20 62 2e  F);.  assert( b.
1875d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
1875e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
1875f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 26 62  backup_finish(&b
18760 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
18761 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 54  ITE_OK ){.    pT
18762 6f 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  o->pBt->pageSize
18763 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Fixed = 0;.  }..
18764 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
18765 61 76 65 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71  ave(pFrom);.  sq
18766 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
18767 70 54 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pTo);.  return r
18768 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  c;.}.#endif /* S
18769 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
1876a 4d 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  M */../*********
1876b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 61 63  ***** End of bac
1876c 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  kup.c **********
1876d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1876e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1876f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
18770 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
18771 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a   vdbemem.c *****
18772 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18773 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18774 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
18775 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68   May 26.**.** Th
18776 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
18777 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
18778 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
18779 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1877a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1877b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1877c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1877d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1877e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1877f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
18780 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
18781 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
18782 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
18783 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
18784 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
18785 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
18786 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
18787 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18788 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18789 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1878a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1878b 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
1878c 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1878d 63 6f 64 65 20 75 73 65 20 74 6f 20 6d 61 6e 69  code use to mani
1878e 70 75 6c 61 74 65 20 22 4d 65 6d 22 20 73 74 72  pulate "Mem" str
1878f 75 63 74 75 72 65 2e 20 20 41 20 22 4d 65 6d 22  ucture.  A "Mem"
18790 0a 2a 2a 20 73 74 6f 72 65 73 20 61 20 73 69 6e  .** stores a sin
18791 67 6c 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65  gle value in the
18792 20 56 44 42 45 2e 20 20 4d 65 6d 20 69 73 20 61   VDBE.  Mem is a
18793 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75  n opaque structu
18794 72 65 20 76 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e  re visible.** on
18795 6c 79 20 77 69 74 68 69 6e 20 74 68 65 20 56 44  ly within the VD
18796 42 45 2e 20 20 49 6e 74 65 72 66 61 63 65 20 72  BE.  Interface r
18797 6f 75 74 69 6e 65 73 20 72 65 66 65 72 20 74 6f  outines refer to
18798 20 61 20 4d 65 6d 20 75 73 69 6e 67 20 74 68 65   a Mem using the
18799 0a 2a 2a 20 6e 61 6d 65 20 73 71 6c 69 74 65 5f  .** name sqlite_
1879a 76 61 6c 75 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  value.**.** $Id:
1879b 20 76 64 62 65 6d 65 6d 2e 63 2c 76 20 31 2e 31   vdbemem.c,v 1.1
1879c 34 34 20 32 30 30 39 2f 30 35 2f 30 35 20 31 32  44 2009/05/05 12
1879d 3a 35 34 3a 35 30 20 64 72 68 20 45 78 70 20 24  :54:50 drh Exp $
1879e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  .*/../*.** Call 
1879f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78  sqlite3VdbeMemEx
187a0 70 61 6e 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68  pandBlob() on th
187a1 65 20 73 75 70 70 6c 69 65 64 20 76 61 6c 75 65  e supplied value
187a2 20 28 74 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20   (type Mem*).** 
187a3 50 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a  P if required..*
187a4 2f 0a 23 64 65 66 69 6e 65 20 65 78 70 61 6e 64  /.#define expand
187a5 42 6c 6f 62 28 50 29 20 28 28 28 50 29 2d 3e 66  Blob(P) (((P)->f
187a6 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73  lags&MEM_Zero)?s
187a7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
187a8 61 6e 64 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f  andBlob(P):0)../
187a9 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20  *.** If pMem is 
187aa 61 6e 20 6f 62 6a 65 63 74 20 77 69 74 68 20 61  an object with a
187ab 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 72 65   valid string re
187ac 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68  presentation, th
187ad 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e  is routine.** en
187ae 73 75 72 65 73 20 74 68 65 20 69 6e 74 65 72 6e  sures the intern
187af 61 6c 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20  al encoding for 
187b0 74 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65  the string repre
187b1 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20  sentation is.** 
187b2 27 64 65 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e  'desiredEnc', on
187b3 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 38  e of SQLITE_UTF8
187b4 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
187b5 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36   or SQLITE_UTF16
187b6 42 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65  BE..**.** If pMe
187b7 6d 20 69 73 20 6e 6f 74 20 61 20 73 74 72 69 6e  m is not a strin
187b8 67 20 6f 62 6a 65 63 74 2c 20 6f 72 20 74 68 65  g object, or the
187b9 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
187ba 20 73 74 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65   string.** repre
187bb 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 6c 72  sentation is alr
187bc 65 61 64 79 20 73 74 6f 72 65 64 20 75 73 69 6e  eady stored usin
187bd 67 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  g the requested 
187be 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74  encoding, then t
187bf 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69  his.** routine i
187c0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
187c1 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
187c2 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 63 6f  turned if the co
187c3 6e 76 65 72 73 69 6f 6e 20 69 73 20 73 75 63 63  nversion is succ
187c4 65 73 73 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72  essful (or not r
187c5 65 71 75 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c  equired)..** SQL
187c6 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65  ITE_NOMEM may be
187c7 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d   returned if a m
187c8 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64 75  alloc() fails du
187c9 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a  ring conversion.
187ca 2a 2a 20 62 65 74 77 65 65 6e 20 66 6f 72 6d 61  ** between forma
187cb 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ts..*/.SQLITE_PR
187cc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
187cd 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
187ce 69 6e 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  ing(Mem *pMem, i
187cf 6e 74 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a  nt desiredEnc){.
187d0 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
187d1 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
187d2 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20  &MEM_RowSet)==0 
187d3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73  );.  assert( des
187d4 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
187d5 55 54 46 38 20 7c 7c 20 64 65 73 69 72 65 64 45  UTF8 || desiredE
187d6 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
187d7 4c 45 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c  LE.           ||
187d8 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
187d9 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20  ITE_UTF16BE );. 
187da 20 69 66 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61   if( !(pMem->fla
187db 67 73 26 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70  gs&MEM_Str) || p
187dc 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65  Mem->enc==desire
187dd 64 45 6e 63 20 29 7b 0a 20 20 20 20 72 65 74 75  dEnc ){.    retu
187de 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
187df 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  }.  assert( pMem
187e0 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
187e1 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
187e2 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
187e3 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
187e4 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 72 65 74  OMIT_UTF16.  ret
187e5 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
187e6 3b 0a 23 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65  ;.#else..  /* Me
187e7 6d 54 72 61 6e 73 6c 61 74 65 28 29 20 6d 61 79  mTranslate() may
187e8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
187e9 4b 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  K or SQLITE_NOME
187ea 4d 2e 20 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72  M. If NOMEM is r
187eb 65 74 75 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68  eturned,.  ** th
187ec 65 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  en the encoding 
187ed 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6d 61 79  of the value may
187ee 20 6e 6f 74 20 68 61 76 65 20 63 68 61 6e 67 65   not have change
187ef 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73  d..  */.  rc = s
187f0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61  qlite3VdbeMemTra
187f1 6e 73 6c 61 74 65 28 70 4d 65 6d 2c 20 28 75 38  nslate(pMem, (u8
187f2 29 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20  )desiredEnc);.  
187f3 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54  assert(rc==SQLIT
187f4 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53  E_OK    || rc==S
187f5 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20  QLITE_NOMEM);.  
187f6 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54  assert(rc==SQLIT
187f7 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d  E_OK    || pMem-
187f8 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63  >enc!=desiredEnc
187f9 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d  );.  assert(rc==
187fa 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
187fb 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72  pMem->enc==desir
187fc 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e  edEnc);.  return
187fd 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f   rc;.#endif.}../
187fe 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70  *.** Make sure p
187ff 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f  Mem->z points to
18800 20 61 20 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f   a writable allo
18801 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61  cation of at lea
18802 73 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a  st .** n bytes..
18803 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d  **.** If the mem
18804 6f 72 79 20 63 65 6c 6c 20 63 75 72 72 65 6e 74  ory cell current
18805 6c 79 20 63 6f 6e 74 61 69 6e 73 20 73 74 72 69  ly contains stri
18806 6e 67 20 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a  ng or blob data.
18807 2a 2a 20 61 6e 64 20 74 68 65 20 74 68 69 72 64  ** and the third
18808 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
18809 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
1880a 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 0a  n is true, the .
1880b 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65  ** current conte
1880c 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69  nt of the cell i
1880d 73 20 70 72 65 73 65 72 76 65 64 2e 20 4f 74 68  s preserved. Oth
1880e 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a  erwise, it may.*
1880f 2a 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 20  * be discarded. 
18810 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e   .**.** This fun
18811 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 4d  ction sets the M
18812 45 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e 64 20  EM_Dyn flag and 
18813 63 6c 65 61 72 73 20 61 6e 79 20 78 44 65 6c 20  clears any xDel 
18814 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20  callback..** It 
18815 61 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f  also clears MEM_
18816 45 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74  Ephem and MEM_St
18817 61 74 69 63 2e 20 49 66 20 74 68 65 20 70 72 65  atic. If the pre
18818 73 65 72 76 65 20 66 6c 61 67 20 69 73 20 0a 2a  serve flag is .*
18819 2a 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e  * not set, Mem.n
1881a 20 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53   is zeroed..*/.S
1881b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1881c 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
1881d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  Grow(Mem *pMem, 
1881e 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65  int n, int prese
1881f 72 76 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rve){.  assert( 
18820 31 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d  1 >=.    ((pMem-
18821 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d  >zMalloc && pMem
18822 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d  ->zMalloc==pMem-
18823 3e 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20  >z) ? 1 : 0) +. 
18824 20 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67     (((pMem->flag
18825 73 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d  s&MEM_Dyn)&&pMem
18826 2d 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20 30 29  ->xDel) ? 1 : 0)
18827 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e   + .    ((pMem->
18828 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29  flags&MEM_Ephem)
18829 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20   ? 1 : 0) + .   
1882a 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d   ((pMem->flags&M
1882b 45 4d 5f 53 74 61 74 69 63 29 20 3f 20 31 20 3a  EM_Static) ? 1 :
1882c 20 30 29 0a 20 20 29 3b 0a 20 20 61 73 73 65 72   0).  );.  asser
1882d 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  t( (pMem->flags&
1882e 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
1882f 3b 0a 0a 20 20 69 66 28 20 6e 3c 33 32 20 29 20  ;..  if( n<32 ) 
18830 6e 20 3d 20 33 32 3b 0a 20 20 69 66 28 20 73 71  n = 32;.  if( sq
18831 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
18832 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d  e(pMem->db, pMem
18833 2d 3e 7a 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a  ->zMalloc)<n ){.
18834 20 20 20 20 69 66 28 20 70 72 65 73 65 72 76 65      if( preserve
18835 20 26 26 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65   && pMem->z==pMe
18836 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20  m->zMalloc ){.  
18837 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d      pMem->z = pM
18838 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71  em->zMalloc = sq
18839 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72  lite3DbReallocOr
1883a 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70  Free(pMem->db, p
1883b 4d 65 6d 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20  Mem->z, n);.    
1883c 20 20 70 72 65 73 65 72 76 65 20 3d 20 30 3b 0a    preserve = 0;.
1883d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1883e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
1883f 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Mem->db, pMem->z
18840 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70  Malloc);.      p
18841 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73  Mem->zMalloc = s
18842 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
18843 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a  w(pMem->db, n);.
18844 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
18845 20 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65   preserve && pMe
18846 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d  m->z && pMem->zM
18847 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a  alloc && pMem->z
18848 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  !=pMem->zMalloc 
18849 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d  ){.    memcpy(pM
1884a 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65  em->zMalloc, pMe
1884b 6d 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a  m->z, pMem->n);.
1884c 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e    }.  if( pMem->
1884d 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26  flags&MEM_Dyn &&
1884e 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20   pMem->xDel ){. 
1884f 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76     pMem->xDel((v
18850 6f 69 64 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29  oid *)(pMem->z))
18851 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a  ;.  }..  pMem->z
18852 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63   = pMem->zMalloc
18853 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d  ;.  if( pMem->z=
18854 3d 30 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  =0 ){.    pMem->
18855 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
18856 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
18857 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Mem->flags &= ~(
18858 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
18859 61 74 69 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65  atic);.  }.  pMe
1885a 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72  m->xDel = 0;.  r
1885b 65 74 75 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f  eturn (pMem->z ?
1885c 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c   SQLITE_OK : SQL
1885d 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f  ITE_NOMEM);.}../
1885e 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68 65 20 67 69  *.** Make the gi
1885f 76 65 6e 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d  ven Mem object M
18860 45 4d 5f 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65  EM_Dyn.  In othe
18861 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74  r words, make it
18862 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20   so.** that any 
18863 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e  TEXT or BLOB con
18864 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69  tent is stored i
18865 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
18866 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63  d from.** malloc
18867 28 29 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79  ().  In this way
18868 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  , we know that t
18869 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 61 66  he memory is saf
1886a 65 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77  e to be.** overw
1886b 72 69 74 74 65 6e 20 6f 72 20 61 6c 74 65 72 65  ritten or altere
1886c 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  d..**.** Return 
1886d 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
1886e 63 65 73 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e  cess or SQLITE_N
1886f 4f 4d 45 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66  OMEM if malloc f
18870 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ails..*/.SQLITE_
18871 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18872 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72  te3VdbeMemMakeWr
18873 69 74 65 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65  iteable(Mem *pMe
18874 6d 29 7b 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61  m){.  int f;.  a
18875 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
18876 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
18877 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
18878 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
18879 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
1887a 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  ags&MEM_RowSet)=
1887b 3d 30 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c  =0 );.  expandBl
1887c 6f 62 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20  ob(pMem);.  f = 
1887d 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69  pMem->flags;.  i
1887e 66 28 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d  f( (f&(MEM_Str|M
1887f 45 4d 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65  EM_Blob)) && pMe
18880 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c  m->z!=pMem->zMal
18881 6c 6f 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73  loc ){.    if( s
18882 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
18883 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20  w(pMem, pMem->n 
18884 2b 20 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20  + 2, 1) ){.     
18885 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
18886 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
18887 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d  pMem->z[pMem->n]
18888 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e   = 0;.    pMem->
18889 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30  z[pMem->n+1] = 0
1888a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
1888b 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20  s |= MEM_Term;. 
1888c 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
1888d 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
1888e 20 49 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65   If the given Me
1888f 6d 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69  m* has a zero-fi
18890 6c 6c 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20  lled tail, turn 
18891 69 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e  it into an ordin
18892 61 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72  ary.** blob stor
18893 65 64 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c  ed in dynamicall
18894 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63  y allocated spac
18895 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
18896 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
18897 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  OB.SQLITE_PRIVAT
18898 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
18899 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d  eMemExpandBlob(M
1889a 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28  em *pMem){.  if(
1889b 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
1889c 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69  EM_Zero ){.    i
1889d 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73  nt nByte;.    as
1889e 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  sert( pMem->flag
1889f 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20  s&MEM_Blob );.  
188a0 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
188a1 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65  >flags&MEM_RowSe
188a2 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  t)==0 );.    ass
188a3 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30  ert( pMem->db==0
188a4 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
188a5 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d  x_held(pMem->db-
188a6 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20  >mutex) );..    
188a7 2f 2a 20 53 65 74 20 6e 42 79 74 65 20 74 6f 20  /* Set nByte to 
188a8 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
188a9 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20  tes required to 
188aa 73 74 6f 72 65 20 74 68 65 20 65 78 70 61 6e 64  store the expand
188ab 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20  ed blob. */.    
188ac 6e 42 79 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20  nByte = pMem->n 
188ad 2b 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b  + pMem->u.nZero;
188ae 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d  .    if( nByte<=
188af 30 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65  0 ){.      nByte
188b0 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
188b1 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
188b2 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79  emGrow(pMem, nBy
188b3 74 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20  te, 1) ){.      
188b4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
188b5 4d 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  MEM;.    }..    
188b6 6d 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b  memset(&pMem->z[
188b7 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65  pMem->n], 0, pMe
188b8 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20  m->u.nZero);.   
188b9 20 70 4d 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d   pMem->n += pMem
188ba 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70  ->u.nZero;.    p
188bb 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Mem->flags &= ~(
188bc 4d 45 4d 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72  MEM_Zero|MEM_Ter
188bd 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  m);.  }.  return
188be 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
188bf 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  ndif.../*.** Mak
188c0 65 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e  e sure the given
188c1 20 4d 65 6d 20 69 73 20 5c 75 30 30 30 30 20 74   Mem is \u0000 t
188c2 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51  erminated..*/.SQ
188c3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
188c4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
188c5 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20  ulTerminate(Mem 
188c6 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74  *pMem){.  assert
188c7 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
188c8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
188c9 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
188ca 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 28 70  tex) );.  if( (p
188cb 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
188cc 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d  _Term)!=0 || (pM
188cd 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
188ce 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Str)==0 ){.    r
188cf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
188d0 20 20 20 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f     /* Nothing to
188d1 20 64 6f 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28   do */.  }.  if(
188d2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
188d3 72 6f 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e  row(pMem, pMem->
188d4 6e 2b 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 72  n+2, 1) ){.    r
188d5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
188d6 45 4d 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e  EM;.  }.  pMem->
188d7 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a  z[pMem->n] = 0;.
188d8 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
188d9 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d  n+1] = 0;.  pMem
188da 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54  ->flags |= MEM_T
188db 65 72 6d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  erm;.  return SQ
188dc 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
188dd 2a 20 41 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f  * Add MEM_Str to
188de 20 74 68 65 20 73 65 74 20 6f 66 20 72 65 70 72   the set of repr
188df 65 73 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20  esentations for 
188e0 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20  the given Mem.  
188e1 4e 75 6d 62 65 72 73 0a 2a 2a 20 61 72 65 20 63  Numbers.** are c
188e2 6f 6e 76 65 72 74 65 64 20 75 73 69 6e 67 20 73  onverted using s
188e3 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
188e4 29 2e 20 20 43 6f 6e 76 65 72 74 69 6e 67 20 61  ).  Converting a
188e5 20 42 4c 4f 42 20 74 6f 20 61 20 73 74 72 69 6e   BLOB to a strin
188e6 67 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  g.** is a no-op.
188e7 0a 2a 2a 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20  .**.** Existing 
188e8 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20  representations 
188e9 4d 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f  MEM_Int and MEM_
188ea 52 65 61 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69  Real are *not* i
188eb 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a  nvalidated..**.*
188ec 2a 20 41 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c  * A MEM_Null val
188ed 75 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  ue will never be
188ee 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
188ef 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 66  function. This f
188f0 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73  unction is.** us
188f1 65 64 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e  ed for convertin
188f2 67 20 76 61 6c 75 65 73 20 74 6f 20 74 65 78 74  g values to text
188f3 20 66 6f 72 20 72 65 74 75 72 6e 69 6e 67 20 74   for returning t
188f4 6f 20 74 68 65 20 75 73 65 72 20 28 69 2e 65 2e  o the user (i.e.
188f5 20 76 69 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   via.** sqlite3_
188f6 76 61 6c 75 65 5f 74 65 78 74 28 29 29 2c 20 6f  value_text()), o
188f7 72 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74  r for ensuring t
188f8 68 61 74 20 76 61 6c 75 65 73 20 74 6f 20 62 65  hat values to be
188f9 20 75 73 65 64 20 61 73 20 62 74 72 65 65 0a 2a   used as btree.*
188fa 2a 20 6b 65 79 73 20 61 72 65 20 73 74 72 69 6e  * keys are strin
188fb 67 73 2e 20 49 6e 20 74 68 65 20 66 6f 72 6d 65  gs. In the forme
188fc 72 20 63 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f  r case a NULL po
188fd 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  inter is returne
188fe 64 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e  d the.** user an
188ff 64 20 74 68 65 20 6c 61 74 65 72 20 69 73 20 61  d the later is a
18900 6e 20 69 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72  n internal progr
18901 61 6d 6d 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f  amming error..*/
18902 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18903 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
18904 65 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 20  emStringify(Mem 
18905 2a 70 4d 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b  *pMem, int enc){
18906 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
18907 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20  TE_OK;.  int fg 
18908 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20  = pMem->flags;. 
18909 20 63 6f 6e 73 74 20 69 6e 74 20 6e 42 79 74 65   const int nByte
1890a 20 3d 20 33 32 3b 0a 0a 20 20 61 73 73 65 72 74   = 32;..  assert
1890b 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
1890c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1890d 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
1890e 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
1890f 28 20 21 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29  ( !(fg&MEM_Zero)
18910 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28   );.  assert( !(
18911 66 67 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  fg&(MEM_Str|MEM_
18912 42 6c 6f 62 29 29 20 29 3b 0a 20 20 61 73 73 65  Blob)) );.  asse
18913 72 74 28 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c  rt( fg&(MEM_Int|
18914 4d 45 4d 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61  MEM_Real) );.  a
18915 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
18916 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  ags&MEM_RowSet)=
18917 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
18918 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e  EIGHT_BYTE_ALIGN
18919 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a  MENT(pMem) );...
1891a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1891b 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e  eMemGrow(pMem, n
1891c 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Byte, 0) ){.    
1891d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1891e 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  MEM;.  }..  /* F
1891f 6f 72 20 61 20 52 65 61 6c 20 6f 72 20 49 6e 74  or a Real or Int
18920 65 67 65 72 2c 20 75 73 65 20 73 71 6c 69 74 65  eger, use sqlite
18921 33 5f 6d 70 72 69 6e 74 66 28 29 20 74 6f 20 70  3_mprintf() to p
18922 72 6f 64 75 63 65 20 74 68 65 20 55 54 46 2d 38  roduce the UTF-8
18923 0a 20 20 2a 2a 20 73 74 72 69 6e 67 20 72 65 70  .  ** string rep
18924 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
18925 68 65 20 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20  he value. Then, 
18926 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20  if the required 
18927 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73  encoding.  ** is
18928 20 55 54 46 2d 31 36 6c 65 20 6f 72 20 55 54 46   UTF-16le or UTF
18929 2d 31 36 62 65 20 64 6f 20 61 20 74 72 61 6e 73  -16be do a trans
1892a 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20  lation..  ** .  
1892b 2a 2a 20 46 49 58 20 4d 45 3a 20 49 74 20 77 6f  ** FIX ME: It wo
1892c 75 6c 64 20 62 65 20 62 65 74 74 65 72 20 69 66  uld be better if
1892d 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1892e 66 28 29 20 63 6f 75 6c 64 20 64 6f 20 55 54 46  f() could do UTF
1892f 2d 31 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  -16..  */.  if( 
18930 66 67 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a  fg & MEM_Int ){.
18931 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
18932 69 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d  intf(nByte, pMem
18933 2d 3e 7a 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65  ->z, "%lld", pMe
18934 6d 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65  m->u.i);.  }else
18935 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 66 67  {.    assert( fg
18936 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20   & MEM_Real );. 
18937 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
18938 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d  ntf(nByte, pMem-
18939 3e 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 4d  >z, "%!.15g", pM
1893a 65 6d 2d 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d  em->r);.  }.  pM
1893b 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53  em->n = sqlite3S
1893c 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29  trlen30(pMem->z)
1893d 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20  ;.  pMem->enc = 
1893e 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 70  SQLITE_UTF8;.  p
1893f 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
18940 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
18941 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
18942 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
18943 2c 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e  , enc);.  return
18944 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65   rc;.}../*.** Me
18945 6d 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63  mory cell pMem c
18946 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74  ontains the cont
18947 65 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65 67  ext of an aggreg
18948 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  ate function..**
18949 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
1894a 6c 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a 65  lls the finalize
1894b 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74   method for that
1894c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a   function.  The.
1894d 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  ** result of the
1894e 20 61 67 67 72 65 67 61 74 65 20 69 73 20 73 74   aggregate is st
1894f 6f 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 70  ored back into p
18950 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  Mem..**.** Retur
18951 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69  n SQLITE_ERROR i
18952 66 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20  f the finalizer 
18953 72 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f 72  reports an error
18954 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20  .  SQLITE_OK.** 
18955 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51  otherwise..*/.SQ
18956 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
18957 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
18958 69 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65  inalize(Mem *pMe
18959 6d 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e  m, FuncDef *pFun
1895a 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  c){.  int rc = S
1895b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
1895c 70 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e  pFunc && pFunc->
1895d 78 46 69 6e 61 6c 69 7a 65 20 29 7b 0a 20 20 20  xFinalize ){.   
1895e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1895f 20 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74   ctx;.    assert
18960 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
18961 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c   MEM_Null)!=0 ||
18962 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e   pFunc==pMem->u.
18963 70 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 65  pDef );.    asse
18964 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
18965 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
18966 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
18967 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d 65  mutex) );.    me
18968 6d 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73 69  mset(&ctx, 0, si
18969 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20 20  zeof(ctx));.    
1896a 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ctx.s.flags = ME
1896b 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e  M_Null;.    ctx.
1896c 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b  s.db = pMem->db;
1896d 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20  .    ctx.pMem = 
1896e 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46  pMem;.    ctx.pF
1896f 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20  unc = pFunc;.   
18970 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a   pFunc->xFinaliz
18971 65 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73 73  e(&ctx);.    ass
18972 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66  ert( 0==(pMem->f
18973 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26  lags&MEM_Dyn) &&
18974 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a   !pMem->xDel );.
18975 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
18976 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d  e(pMem->db, pMem
18977 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20  ->zMalloc);.    
18978 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 74  memcpy(pMem, &ct
18979 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e  x.s, sizeof(ctx.
1897a 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 28 63  s));.    rc = (c
1897b 74 78 2e 69 73 45 72 72 6f 72 3f 53 51 4c 49 54  tx.isError?SQLIT
1897c 45 5f 45 52 52 4f 52 3a 53 51 4c 49 54 45 5f 4f  E_ERROR:SQLITE_O
1897d 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  K);.  }.  return
1897e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66   rc;.}../*.** If
1897f 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
18980 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 74 72 69   contains a stri
18981 6e 67 20 76 61 6c 75 65 20 74 68 61 74 20 6d 75  ng value that mu
18982 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a  st be freed by.*
18983 2a 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 65 78  * invoking an ex
18984 74 65 72 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 2c  ternal callback,
18985 20 66 72 65 65 20 69 74 20 6e 6f 77 2e 20 43 61   free it now. Ca
18986 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74  lling this funct
18987 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20  ion.** does not 
18988 66 72 65 65 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61  free any Mem.zMa
18989 6c 6c 6f 63 20 62 75 66 66 65 72 2e 0a 2a 2f 0a  lloc buffer..*/.
1898a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1898b 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
1898c 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
1898d 6c 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73  l(Mem *p){.  ass
1898e 65 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c  ert( p->db==0 ||
1898f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
18990 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
18991 29 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c  ) );.  if( p->fl
18992 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d  ags&(MEM_Agg|MEM
18993 5f 44 79 6e 7c 4d 45 4d 5f 52 6f 77 53 65 74 29  _Dyn|MEM_RowSet)
18994 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66   ){.    if( p->f
18995 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a  lags&MEM_Agg ){.
18996 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
18997 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20  eMemFinalize(p, 
18998 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20  p->u.pDef);.    
18999 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c    assert( (p->fl
1899a 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d  ags & MEM_Agg)==
1899b 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
1899c 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1899d 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  (p);.    }else i
1899e 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  f( p->flags&MEM_
1899f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29  Dyn && p->xDel )
189a0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
189a1 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f  (p->flags&MEM_Ro
189a2 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20  wSet)==0 );.    
189a3 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20    p->xDel((void 
189a4 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70  *)p->z);.      p
189a5 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20  ->xDel = 0;.    
189a6 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61  }else if( p->fla
189a7 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b  gs&MEM_RowSet ){
189a8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f  .      sqlite3Ro
189a9 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70  wSetClear(p->u.p
189aa 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20  RowSet);.    }. 
189ab 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65   }.}../*.** Rele
189ac 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79 20 68  ase any memory h
189ad 65 6c 64 20 62 79 20 74 68 65 20 4d 65 6d 2e 20  eld by the Mem. 
189ae 54 68 69 73 20 6d 61 79 20 6c 65 61 76 65 20 74  This may leave t
189af 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a 2a 20  he Mem in an.** 
189b0 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61  inconsistent sta
189b1 74 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20  te, for example 
189b2 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30 29 20  with (Mem.z==0) 
189b3 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79 70 65  and.** (Mem.type
189b4 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 29 2e 0a  ==SQLITE_TEXT)..
189b5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
189b6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
189b7 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d  beMemRelease(Mem
189b8 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 56   *p){.  sqlite3V
189b9 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74  dbeMemReleaseExt
189ba 65 72 6e 61 6c 28 70 29 3b 0a 20 20 73 71 6c 69  ernal(p);.  sqli
189bb 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c  te3DbFree(p->db,
189bc 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20   p->zMalloc);.  
189bd 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a  p->z = 0;.  p->z
189be 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 70 2d  Malloc = 0;.  p-
189bf 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  >xDel = 0;.}../*
189c0 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 36 34  .** Convert a 64
189c1 2d 62 69 74 20 49 45 45 45 20 64 6f 75 62 6c 65  -bit IEEE double
189c2 20 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73   into a 64-bit s
189c3 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a  igned integer..*
189c4 2a 20 49 66 20 74 68 65 20 64 6f 75 62 6c 65 20  * If the double 
189c5 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 72 65  is too large, re
189c6 74 75 72 6e 20 30 78 38 30 30 30 30 30 30 30 30  turn 0x800000000
189c7 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 2a 20 4d  0000000..**.** M
189c8 6f 73 74 20 73 79 73 74 65 6d 73 20 61 70 70 65  ost systems appe
189c9 61 72 20 74 6f 20 64 6f 20 74 68 69 73 20 73 69  ar to do this si
189ca 6d 70 6c 79 20 62 79 20 61 73 73 69 67 6e 69 6e  mply by assignin
189cb 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61  g.** variables a
189cc 6e 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 65  nd without the e
189cd 78 74 72 61 20 72 61 6e 67 65 20 74 65 73 74 73  xtra range tests
189ce 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 72 65 20  .  But.** there 
189cf 61 72 65 20 72 65 70 6f 72 74 73 20 74 68 61 74  are reports that
189d0 20 77 69 6e 64 6f 77 73 20 74 68 72 6f 77 73 20   windows throws 
189d1 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a 2a 2a 20  an expection.** 
189d2 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20  if the floating 
189d3 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 6f  point value is o
189d4 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 28 53 65  ut of range. (Se
189d5 65 20 74 69 63 6b 65 74 20 23 32 38 38 30 2e 29  e ticket #2880.)
189d6 0a 2a 2a 20 42 65 63 61 75 73 65 20 77 65 20 64  .** Because we d
189d7 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79  o not completely
189d8 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20   understand the 
189d9 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77 69 6c 6c  problem, we will
189da 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 63 6f 6e  .** take the con
189db 73 65 72 76 61 74 69 76 65 20 61 70 70 72 6f 61  servative approa
189dc 63 68 20 61 6e 64 20 61 6c 77 61 79 73 20 64 6f  ch and always do
189dd 20 72 61 6e 67 65 20 74 65 73 74 73 0a 2a 2a 20   range tests.** 
189de 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 6e  before attemptin
189df 67 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  g the conversion
189e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20  ..*/.static i64 
189e1 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 64 6f  doubleToInt64(do
189e2 75 62 6c 65 20 72 29 7b 0a 20 20 2f 2a 0a 20 20  uble r){.  /*.  
189e3 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 6c 65 72  ** Many compiler
189e4 73 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 20 64  s we encounter d
189e5 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20 63 6f 6e  o not define con
189e6 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 0a 20  stants for the. 
189e7 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 6e 64 20   ** minimum and 
189e8 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69 74 20 69  maximum 64-bit i
189e9 6e 74 65 67 65 72 73 2c 20 6f 72 20 74 68 65 79  ntegers, or they
189ea 20 64 65 66 69 6e 65 20 74 68 65 6d 0a 20 20 2a   define them.  *
189eb 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 6c 79  * inconsistently
189ec 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64 6f 20 6e  .  And many do n
189ed 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68  ot understand th
189ee 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69 6f 6e 2e  e "LL" notation.
189ef 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64 65 66 69  .  ** So we defi
189f0 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74 61 74 69  ne our own stati
189f1 63 20 63 6f 6e 73 74 61 6e 74 73 20 68 65 72 65  c constants here
189f2 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e 67 0a 20   using nothing. 
189f3 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20   ** larger than 
189f4 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  a 32-bit integer
189f5 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 2a 2f 0a   constant..  */.
189f6 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
189f7 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c 41 52 47  64 maxInt = LARG
189f8 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20 73 74 61  EST_INT64;.  sta
189f9 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20 6d 69  tic const i64 mi
189fa 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 53 54 5f  nInt = SMALLEST_
189fb 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28 20 72 3c  INT64;..  if( r<
189fc 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e 74 20 29  (double)minInt )
189fd 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e  {.    return min
189fe 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  Int;.  }else if(
189ff 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61 78 49 6e   r>(double)maxIn
18a00 74 20 29 7b 0a 20 20 20 20 2f 2a 20 6d 69 6e 49  t ){.    /* minI
18a01 6e 74 20 69 73 20 63 6f 72 72 65 63 74 20 68 65  nt is correct he
18a02 72 65 20 2d 20 6e 6f 74 20 6d 61 78 49 6e 74 2e  re - not maxInt.
18a03 20 20 49 74 20 74 75 72 6e 73 20 6f 75 74 20 74    It turns out t
18a04 68 61 74 20 61 73 73 69 67 6e 69 6e 67 0a 20 20  hat assigning.  
18a05 20 20 2a 2a 20 61 20 76 65 72 79 20 6c 61 72 67    ** a very larg
18a06 65 20 70 6f 73 69 74 69 76 65 20 6e 75 6d 62 65  e positive numbe
18a07 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  r to an integer 
18a08 72 65 73 75 6c 74 73 20 69 6e 20 61 20 76 65 72  results in a ver
18a09 79 20 6c 61 72 67 65 0a 20 20 20 20 2a 2a 20 6e  y large.    ** n
18a0a 65 67 61 74 69 76 65 20 69 6e 74 65 67 65 72 2e  egative integer.
18a0b 20 20 54 68 69 73 20 6d 61 6b 65 73 20 6e 6f 20    This makes no 
18a0c 73 65 6e 73 65 2c 20 62 75 74 20 69 74 20 69 73  sense, but it is
18a0d 20 77 68 61 74 20 78 38 36 20 68 61 72 64 77 61   what x86 hardwa
18a0e 72 65 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 73  re.    ** does s
18a0f 6f 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c  o for compatibil
18a10 69 74 79 20 77 65 20 77 69 6c 6c 20 64 6f 20 74  ity we will do t
18a11 68 65 20 73 61 6d 65 20 69 6e 20 73 6f 66 74 77  he same in softw
18a12 61 72 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  are. */.    retu
18a13 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c  rn minInt;.  }el
18a14 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28  se{.    return (
18a15 69 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  i64)r;.  }.}../*
18a16 0a 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d 65 20  .** Return some 
18a17 6b 69 6e 64 20 6f 66 20 69 6e 74 65 67 65 72 20  kind of integer 
18a18 76 61 6c 75 65 20 77 68 69 63 68 20 69 73 20 74  value which is t
18a19 68 65 20 62 65 73 74 20 77 65 20 63 61 6e 20 64  he best we can d
18a1a 6f 0a 2a 2a 20 61 74 20 72 65 70 72 65 73 65 6e  o.** at represen
18a1b 74 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 74  ting the value t
18a1c 68 61 74 20 2a 70 4d 65 6d 20 64 65 73 63 72 69  hat *pMem descri
18a1d 62 65 73 20 61 73 20 61 6e 20 69 6e 74 65 67 65  bes as an intege
18a1e 72 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73  r..** If pMem is
18a1f 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 68 65   an integer, the
18a20 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 65  n the value is e
18a21 78 61 63 74 2e 20 20 49 66 20 70 4d 65 6d 20 69  xact.  If pMem i
18a22 73 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e 67 2d  s.** a floating-
18a23 70 6f 69 6e 74 20 74 68 65 6e 20 74 68 65 20 76  point then the v
18a24 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
18a25 20 74 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   the integer par
18a26 74 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73  t..** If pMem is
18a27 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
18a28 62 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b 65 20  b, then we make 
18a29 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f  an attempt to co
18a2a 6e 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e 74 6f  nvert.** it into
18a2b 20 61 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72   a integer and r
18a2c 65 74 75 72 6e 20 74 68 61 74 2e 20 20 49 66 20  eturn that.  If 
18a2d 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 73 20  pMem represents 
18a2e 61 6e 0a 2a 2a 20 61 6e 20 53 51 4c 2d 4e 55 4c  an.** an SQL-NUL
18a2f 4c 20 76 61 6c 75 65 2c 20 72 65 74 75 72 6e 20  L value, return 
18a30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d  0..**.** If pMem
18a31 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 74   represents a st
18a32 72 69 6e 67 20 76 61 6c 75 65 2c 20 69 74 73 20  ring value, its 
18a33 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20 62  encoding might b
18a34 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51  e changed..*/.SQ
18a35 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34  LITE_PRIVATE i64
18a36 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
18a37 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  alue(Mem *pMem){
18a38 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20  .  int flags;.  
18a39 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
18a3a 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
18a3b 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
18a3c 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
18a3d 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
18a3e 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65  TE_ALIGNMENT(pMe
18a3f 6d 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20  m) );.  flags = 
18a40 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69  pMem->flags;.  i
18a41 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  f( flags & MEM_I
18a42 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  nt ){.    return
18a43 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65   pMem->u.i;.  }e
18a44 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
18a45 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
18a46 72 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 6f 49  return doubleToI
18a47 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 20  nt64(pMem->r);. 
18a48 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73   }else if( flags
18a49 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f   & (MEM_Str|MEM_
18a4a 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 36 34  Blob) ){.    i64
18a4b 20 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 65 6d   value;.    pMem
18a4c 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53  ->flags |= MEM_S
18a4d 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  tr;.    if( sqli
18a4e 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
18a4f 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49  oding(pMem, SQLI
18a50 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20  TE_UTF8).       
18a51 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  || sqlite3VdbeMe
18a52 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d  mNulTerminate(pM
18a53 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  em) ){.      ret
18a54 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 0;.    }.   
18a55 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a   assert( pMem->z
18a56 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41   );.    sqlite3A
18a57 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 20 26  toi64(pMem->z, &
18a58 76 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 74 75  value);.    retu
18a59 72 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 6c 73  rn value;.  }els
18a5a 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  e{.    return 0;
18a5b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
18a5c 74 75 72 6e 20 74 68 65 20 62 65 73 74 20 72 65  turn the best re
18a5d 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
18a5e 70 4d 65 6d 20 74 68 61 74 20 77 65 20 63 61 6e  pMem that we can
18a5f 20 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a 20 64   get into a.** d
18a60 6f 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 6d 20  ouble.  If pMem 
18a61 69 73 20 61 6c 72 65 61 64 79 20 61 20 64 6f 75  is already a dou
18a62 62 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 67 65  ble or an intege
18a63 72 2c 20 72 65 74 75 72 6e 20 69 74 73 0a 2a 2a  r, return its.**
18a64 20 76 61 6c 75 65 2e 20 20 49 66 20 69 74 20 69   value.  If it i
18a65 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c  s a string or bl
18a66 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65  ob, try to conve
18a67 72 74 20 69 74 20 74 6f 20 61 20 64 6f 75 62 6c  rt it to a doubl
18a68 65 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 61  e..** If it is a
18a69 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e   NULL, return 0.
18a6a 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  0..*/.SQLITE_PRI
18a6b 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69  VATE double sqli
18a6c 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65  te3VdbeRealValue
18a6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61  (Mem *pMem){.  a
18a6e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
18a6f 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
18a70 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
18a71 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
18a72 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54  ssert( EIGHT_BYT
18a73 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d  E_ALIGNMENT(pMem
18a74 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d  ) );.  if( pMem-
18a75 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
18a76 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
18a77 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c 73 65  pMem->r;.  }else
18a78 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
18a79 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20   & MEM_Int ){.  
18a7a 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65    return (double
18a7b 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65  )pMem->u.i;.  }e
18a7c 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c  lse if( pMem->fl
18a7d 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d  ags & (MEM_Str|M
18a7e 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20  EM_Blob) ){.    
18a7f 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20  /* (double)0 In 
18a80 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f  case of SQLITE_O
18a81 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
18a82 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 64 6f 75  NT... */.    dou
18a83 62 6c 65 20 76 61 6c 20 3d 20 28 64 6f 75 62 6c  ble val = (doubl
18a84 65 29 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66  e)0;.    pMem->f
18a85 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b  lags |= MEM_Str;
18a86 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
18a87 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
18a88 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f  ng(pMem, SQLITE_
18a89 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c 7c 20  UTF8).       || 
18a8a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
18a8b 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29  lTerminate(pMem)
18a8c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f   ){.      /* (do
18a8d 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f  uble)0 In case o
18a8e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
18a8f 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20  OATING_POINT... 
18a90 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  */.      return 
18a91 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 7d  (double)0;.    }
18a92 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
18a93 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69  m->z );.    sqli
18a94 74 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e 7a 2c  te3AtoF(pMem->z,
18a95 20 26 76 61 6c 29 3b 0a 20 20 20 20 72 65 74 75   &val);.    retu
18a96 72 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b  rn val;.  }else{
18a97 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29  .    /* (double)
18a98 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c  0 In case of SQL
18a99 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
18a9a 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20  G_POINT... */.  
18a9b 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65    return (double
18a9c 29 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  )0;.  }.}../*.**
18a9d 20 54 68 65 20 4d 45 4d 20 73 74 72 75 63 74 75   The MEM structu
18a9e 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
18a9f 4d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79 20 74  MEM_Real.  Try t
18aa0 6f 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74 20 61  o also make it a
18aa1 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66 20 77  .** MEM_Int if w
18aa2 65 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  e can..*/.SQLITE
18aa3 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
18aa4 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
18aa5 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 4d  Affinity(Mem *pM
18aa6 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  em){.  assert( p
18aa7 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
18aa8 5f 52 65 61 6c 20 29 3b 0a 20 20 61 73 73 65 72  _Real );.  asser
18aa9 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
18aaa 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
18aab 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
18aac 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
18aad 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
18aae 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
18aaf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49   );.  assert( EI
18ab0 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
18ab1 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20 70  NT(pMem) );..  p
18ab2 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75 62 6c  Mem->u.i = doubl
18ab3 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72  eToInt64(pMem->r
18ab4 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72  );.  if( pMem->r
18ab5 3d 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e  ==(double)pMem->
18ab6 75 2e 69 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  u.i ){.    pMem-
18ab7 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e  >flags |= MEM_In
18ab8 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t;.  }.}../*.** 
18ab9 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20  Convert pMem to 
18aba 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49  type integer.  I
18abb 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72  nvalidate any pr
18abc 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ior representati
18abd 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
18abe 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18abf 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
18ac0 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ify(Mem *pMem){.
18ac1 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
18ac2 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
18ac3 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
18ac4 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
18ac5 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
18ac6 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
18ac7 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Set)==0 );.  ass
18ac8 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
18ac9 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20  ALIGNMENT(pMem) 
18aca 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20  );..  pMem->u.i 
18acb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
18acc 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d  Value(pMem);.  M
18acd 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d  emSetTypeFlag(pM
18ace 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  em, MEM_Int);.  
18acf 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
18ad0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
18ad1 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20  rt pMem so that 
18ad2 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45  it is of type ME
18ad3 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c  M_Real..** Inval
18ad4 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
18ad5 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
18ad6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18ad7 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
18ad8 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d  beMemRealify(Mem
18ad9 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72   *pMem){.  asser
18ada 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
18adb 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
18adc 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
18add 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
18ade 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c  t( EIGHT_BYTE_AL
18adf 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b  IGNMENT(pMem) );
18ae0 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71  ..  pMem->r = sq
18ae1 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
18ae2 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53  ue(pMem);.  MemS
18ae3 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c  etTypeFlag(pMem,
18ae4 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65   MEM_Real);.  re
18ae5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
18ae6 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
18ae7 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74   pMem so that it
18ae8 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52   has types MEM_R
18ae9 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f  eal or MEM_Int o
18aea 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c  r both..** Inval
18aeb 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
18aec 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
18aed 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18aee 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
18aef 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65  beMemNumerify(Me
18af0 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62  m *pMem){.  doub
18af1 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34  le r1, r2;.  i64
18af2 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   i;.  assert( (p
18af3 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Mem->flags & (ME
18af4 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d  M_Int|MEM_Real|M
18af5 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a  EM_Null))==0 );.
18af6 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
18af7 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c  >flags & (MEM_Bl
18af8 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20  ob|MEM_Str))!=0 
18af9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
18afa 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
18afb 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
18afc 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
18afd 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65  );.  r1 = sqlite
18afe 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
18aff 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62  Mem);.  i = doub
18b00 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20  leToInt64(r1);. 
18b01 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b   r2 = (double)i;
18b02 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b  .  if( r1==r2 ){
18b03 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
18b04 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d  MemIntegerify(pM
18b05 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  em);.  }else{.  
18b06 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a    pMem->r = r1;.
18b07 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
18b08 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61  ag(pMem, MEM_Rea
18b09 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
18b0a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
18b0b 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20  *.** Delete any 
18b0c 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61  previous value a
18b0d 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65  nd set the value
18b0e 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d   stored in *pMem
18b0f 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c   to NULL..*/.SQL
18b10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
18b11 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
18b12 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d  etNull(Mem *pMem
18b13 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66  ){.  if( pMem->f
18b14 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
18b15 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
18b16 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d  RowSetClear(pMem
18b17 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20  ->u.pRowSet);.  
18b18 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  }.  MemSetTypeFl
18b19 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c  ag(pMem, MEM_Nul
18b1a 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65  l);.  pMem->type
18b1b 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a   = SQLITE_NULL;.
18b1c 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
18b1d 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c  any previous val
18b1e 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76  ue and set the v
18b1f 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c 4f  alue to be a BLO
18b20 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e  B of length.** n
18b21 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20   containing all 
18b22 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  zeros..*/.SQLITE
18b23 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
18b24 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a  lite3VdbeMemSetZ
18b25 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65  eroBlob(Mem *pMe
18b26 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c  m, int n){.  sql
18b27 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
18b28 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d  se(pMem);.  pMem
18b29 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c  ->flags = MEM_Bl
18b2a 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70  ob|MEM_Zero;.  p
18b2b 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
18b2c 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d  TE_BLOB;.  pMem-
18b2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c  >n = 0;.  if( n<
18b2e 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65  0 ) n = 0;.  pMe
18b2f 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a  m->u.nZero = n;.
18b30 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51    pMem->enc = SQ
18b31 4c 49 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a  LITE_UTF8;.}../*
18b32 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70  .** Delete any p
18b33 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e  revious value an
18b34 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  d set the value 
18b35 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20  stored in *pMem 
18b36 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66  to val,.** manif
18b37 65 73 74 20 74 79 70 65 20 49 4e 54 45 47 45 52  est type INTEGER
18b38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18b39 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
18b3a 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28  VdbeMemSetInt64(
18b3b 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76  Mem *pMem, i64 v
18b3c 61 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64  al){.  sqlite3Vd
18b3d 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
18b3e 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20  m);.  pMem->u.i 
18b3f 3d 20 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66  = val;.  pMem->f
18b40 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
18b41 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
18b42 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d  QLITE_INTEGER;.}
18b43 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
18b44 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75  ny previous valu
18b45 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61  e and set the va
18b46 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70  lue stored in *p
18b47 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d  Mem to val,.** m
18b48 61 6e 69 66 65 73 74 20 74 79 70 65 20 52 45 41  anifest type REA
18b49 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  L..*/.SQLITE_PRI
18b4a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
18b4b 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c  3VdbeMemSetDoubl
18b4c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75  e(Mem *pMem, dou
18b4d 62 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20  ble val){.  if( 
18b4e 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c  sqlite3IsNaN(val
18b4f 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
18b50 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70  VdbeMemSetNull(p
18b51 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Mem);.  }else{. 
18b52 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
18b53 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
18b54 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61      pMem->r = va
18b55 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  l;.    pMem->fla
18b56 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20  gs = MEM_Real;. 
18b57 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
18b58 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20  SQLITE_FLOAT;.  
18b59 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  }.}../*.** Delet
18b5a 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76  e any previous v
18b5b 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65  alue and set the
18b5c 20 76 61 6c 75 65 20 6f 66 20 70 4d 65 6d 20 74   value of pMem t
18b5d 6f 20 62 65 20 61 6e 0a 2a 2a 20 65 6d 70 74 79  o be an.** empty
18b5e 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 2e 0a   boolean index..
18b5f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18b60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
18b61 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d  beMemSetRowSet(M
18b62 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 73 71 6c  em *pMem){.  sql
18b63 69 74 65 33 20 2a 64 62 20 3d 20 70 4d 65 6d 2d  ite3 *db = pMem-
18b64 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64  >db;.  assert( d
18b65 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4d  b!=0 );.  if( pM
18b66 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
18b67 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 73 71  RowSet ){.    sq
18b68 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72  lite3RowSetClear
18b69 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74  (pMem->u.pRowSet
18b6a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
18b6b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
18b6c 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20  lease(pMem);.   
18b6d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d   pMem->zMalloc =
18b6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
18b6f 52 61 77 28 64 62 2c 20 36 34 29 3b 0a 20 20 7d  Raw(db, 64);.  }
18b70 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
18b71 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70  cFailed ){.    p
18b72 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
18b73 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _Null;.  }else{.
18b74 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
18b75 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20  ->zMalloc );.   
18b76 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74   pMem->u.pRowSet
18b77 20 3d 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74   = sqlite3RowSet
18b78 49 6e 69 74 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Init(db, pMem->z
18b79 4d 61 6c 6c 6f 63 2c 20 0a 20 20 20 20 20 20 20  Malloc, .       
18b7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b7c 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53  sqlite3DbMallocS
18b7d 69 7a 65 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d  ize(db, pMem->zM
18b7e 61 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 61 73 73  alloc));.    ass
18b7f 65 72 74 28 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f  ert( pMem->u.pRo
18b80 77 53 65 74 21 3d 30 20 29 3b 0a 20 20 20 20 70  wSet!=0 );.    p
18b81 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
18b82 5f 52 6f 77 53 65 74 3b 0a 20 20 7d 0a 7d 0a 0a  _RowSet;.  }.}..
18b83 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
18b84 65 20 69 66 20 74 68 65 20 4d 65 6d 20 6f 62 6a  e if the Mem obj
18b85 65 63 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 54  ect contains a T
18b86 45 58 54 20 6f 72 20 42 4c 4f 42 20 74 68 61 74  EXT or BLOB that
18b87 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72 67 65   is.** too large
18b88 20 2d 20 77 68 6f 73 65 20 73 69 7a 65 20 65 78   - whose size ex
18b89 63 65 65 64 73 20 53 51 4c 49 54 45 5f 4d 41 58  ceeds SQLITE_MAX
18b8a 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51 4c 49  _LENGTH..*/.SQLI
18b8b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18b8c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f  qlite3VdbeMemToo
18b8d 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61  Big(Mem *p){.  a
18b8e 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20  ssert( p->db!=0 
18b8f 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67  );.  if( p->flag
18b90 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  s & (MEM_Str|MEM
18b91 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 6e  _Blob) ){.    in
18b92 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 20 20  t n = p->n;.    
18b93 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d  if( p->flags & M
18b94 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20  EM_Zero ){.     
18b95 20 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a 65 72 6f   n += p->u.nZero
18b96 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
18b97 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d  rn n>p->db->aLim
18b98 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
18b99 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72  LENGTH];.  }.  r
18b9a 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a  eturn 0; .}../*.
18b9b 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63  ** Size of struc
18b9c 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64  t Mem not includ
18b9d 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c  ing the Mem.zMal
18b9e 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23  loc member..*/.#
18b9f 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49  define MEMCELLSI
18ba0 5a 45 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28  ZE (size_t)(&(((
18ba1 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f  Mem *)0)->zMallo
18ba2 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  c))../*.** Make 
18ba3 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20  an shallow copy 
18ba4 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54  of pFrom into pT
18ba5 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e  o.  Prior conten
18ba6 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65  ts of.** pTo are
18ba7 20 66 72 65 65 64 2e 20 20 54 68 65 20 70 46 72   freed.  The pFr
18ba8 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e  om->z field is n
18ba9 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20  ot duplicated.  
18baa 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69  If.** pFrom->z i
18bab 73 20 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f  s used, then pTo
18bac 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ->z points to th
18bad 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20  e same thing as 
18bae 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20  pFrom->z.** and 
18baf 66 6c 61 67 73 20 67 65 74 73 20 73 72 63 54 79  flags gets srcTy
18bb0 70 65 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45  pe (either MEM_E
18bb1 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74  phem or MEM_Stat
18bb2 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ic)..*/.SQLITE_P
18bb3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
18bb4 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f  te3VdbeMemShallo
18bb5 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20  wCopy(Mem *pTo, 
18bb6 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d  const Mem *pFrom
18bb7 2c 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a  , int srcType){.
18bb8 20 20 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d    assert( (pFrom
18bb9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
18bba 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71  wSet)==0 );.  sq
18bbb 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
18bbc 61 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29  aseExternal(pTo)
18bbd 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20  ;.  memcpy(pTo, 
18bbe 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49  pFrom, MEMCELLSI
18bbf 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78 44 65 6c  ZE);.  pTo->xDel
18bc0 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 70 46 72   = 0;.  if( (pFr
18bc1 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  om->flags&MEM_Dy
18bc2 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e  n)!=0 || pFrom->
18bc3 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f  z==pFrom->zMallo
18bc4 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c  c ){.    pTo->fl
18bc5 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 79 6e  ags &= ~(MEM_Dyn
18bc6 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  |MEM_Static|MEM_
18bc7 45 70 68 65 6d 29 3b 0a 20 20 20 20 61 73 73 65  Ephem);.    asse
18bc8 72 74 28 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d  rt( srcType==MEM
18bc9 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63 54 79 70  _Ephem || srcTyp
18bca 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63 20 29 3b  e==MEM_Static );
18bcb 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20  .    pTo->flags 
18bcc 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20 20 7d 0a  |= srcType;.  }.
18bcd 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20  }../*.** Make a 
18bce 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20 70 46 72  full copy of pFr
18bcf 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72  om into pTo.  Pr
18bd0 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  ior contents of 
18bd1 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72 65 65 64  pTo are.** freed
18bd2 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79   before the copy
18bd3 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a 53 51 4c   is made..*/.SQL
18bd4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
18bd5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f  sqlite3VdbeMemCo
18bd6 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e  py(Mem *pTo, con
18bd7 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a  st Mem *pFrom){.
18bd8 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
18bd9 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28  E_OK;..  assert(
18bda 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26   (pFrom->flags &
18bdb 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
18bdc 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
18bdd 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
18bde 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70  al(pTo);.  memcp
18bdf 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45  y(pTo, pFrom, ME
18be0 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54  MCELLSIZE);.  pT
18be1 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d  o->flags &= ~MEM
18be2 5f 44 79 6e 3b 0a 0a 20 20 69 66 28 20 70 54 6f  _Dyn;..  if( pTo
18be3 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72  ->flags&(MEM_Str
18be4 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20  |MEM_Blob) ){.  
18be5 20 20 69 66 28 20 30 3d 3d 28 70 46 72 6f 6d 2d    if( 0==(pFrom-
18be6 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69  >flags&MEM_Stati
18be7 63 29 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 2d  c) ){.      pTo-
18be8 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70  >flags |= MEM_Ep
18be9 68 65 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  hem;.      rc = 
18bea 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61  sqlite3VdbeMemMa
18beb 6b 65 57 72 69 74 65 61 62 6c 65 28 70 54 6f 29  keWriteable(pTo)
18bec 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
18bed 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
18bee 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65 20  ** Transfer the 
18bef 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 46 72 6f  contents of pFro
18bf0 6d 20 74 6f 20 70 54 6f 2e 20 41 6e 79 20 65 78  m to pTo. Any ex
18bf1 69 73 74 69 6e 67 20 76 61 6c 75 65 20 69 6e 20  isting value in 
18bf2 70 54 6f 20 69 73 0a 2a 2a 20 66 72 65 65 64 2e  pTo is.** freed.
18bf3 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69   If pFrom contai
18bf4 6e 73 20 65 70 68 65 6d 65 72 61 6c 20 64 61 74  ns ephemeral dat
18bf5 61 2c 20 61 20 63 6f 70 79 20 69 73 20 6d 61 64  a, a copy is mad
18bf6 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f 6d 20 63  e..**.** pFrom c
18bf7 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e  ontains an SQL N
18bf8 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 20 72 6f  ULL when this ro
18bf9 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a  utine returns..*
18bfa 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18bfb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
18bfc 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a 70 54  eMemMove(Mem *pT
18bfd 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a  o, Mem *pFrom){.
18bfe 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
18bff 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
18c00 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 46 72  3_mutex_held(pFr
18c01 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  om->db->mutex) )
18c02 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d  ;.  assert( pTo-
18c03 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
18c04 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 54 6f  3_mutex_held(pTo
18c05 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
18c06 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
18c07 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f 2d 3e 64  >db==0 || pTo->d
18c08 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 64  b==0 || pFrom->d
18c09 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b 0a 0a 20  b==pTo->db );.. 
18c0a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
18c0b 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20 20 6d  elease(pTo);.  m
18c0c 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d  emcpy(pTo, pFrom
18c0d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 29 3b 0a  , sizeof(Mem));.
18c0e 20 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 3d    pFrom->flags =
18c0f 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70 46 72   MEM_Null;.  pFr
18c10 6f 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20  om->xDel = 0;.  
18c11 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d  pFrom->zMalloc =
18c12 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61   0;.}../*.** Cha
18c13 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
18c14 20 61 20 4d 65 6d 20 74 6f 20 62 65 20 61 20 73   a Mem to be a s
18c15 74 72 69 6e 67 20 6f 72 20 61 20 42 4c 4f 42 2e  tring or a BLOB.
18c16 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72  .**.** The memor
18c17 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 72  y management str
18c18 61 74 65 67 79 20 64 65 70 65 6e 64 73 20 6f 6e  ategy depends on
18c19 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
18c1a 65 20 78 44 65 6c 0a 2a 2a 20 70 61 72 61 6d 65  e xDel.** parame
18c1b 74 65 72 2e 20 49 66 20 74 68 65 20 76 61 6c 75  ter. If the valu
18c1c 65 20 70 61 73 73 65 64 20 69 73 20 53 51 4c 49  e passed is SQLI
18c1d 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 20 74 68  TE_TRANSIENT, th
18c1e 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74 72 69 6e  en the .** strin
18c1f 67 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f  g is copied into
18c20 20 61 20 28 70 6f 73 73 69 62 6c 79 20 65 78 69   a (possibly exi
18c21 73 74 69 6e 67 29 20 62 75 66 66 65 72 20 6d 61  sting) buffer ma
18c22 6e 61 67 65 64 20 62 79 20 74 68 65 20 0a 2a 2a  naged by the .**
18c23 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20   Mem structure. 
18c24 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 65  Otherwise, any e
18c25 78 69 73 74 69 6e 67 20 62 75 66 66 65 72 20 69  xisting buffer i
18c26 73 20 66 72 65 65 64 20 61 6e 64 20 74 68 65 0a  s freed and the.
18c27 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f 70 69 65  ** pointer copie
18c28 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
18c29 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
18c2a 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20  VdbeMemSetStr(. 
18c2b 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20   Mem *pMem,     
18c2c 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
18c2d 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74  ell to set to st
18c2e 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20  ring value */.  
18c2f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20  const char *z,  
18c30 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f      /* String po
18c31 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  inter */.  int n
18c32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
18c33 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e  * Bytes in strin
18c34 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a  g, or negative *
18c35 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20  /.  u8 enc,     
18c36 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64          /* Encod
18c37 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72  ing of z.  0 for
18c38 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64   BLOBs */.  void
18c39 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20   (*xDel)(void*) 
18c3a 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75  /* Destructor fu
18c3b 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69  nction */.){.  i
18c3c 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20  nt nByte = n;   
18c3d 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20     /* New value 
18c3e 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20  for pMem->n */. 
18c3f 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20   int iLimit;    
18c40 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
18c41 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f  allowed string o
18c42 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20  r blob size */. 
18c43 20 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20   u16 flags = 0; 
18c44 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75       /* New valu
18c45 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67  e for pMem->flag
18c46 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  s */..  assert( 
18c47 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
18c48 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
18c49 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
18c4a 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
18c4b 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d  (pMem->flags & M
18c4c 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
18c4d 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61  ..  /* If z is a
18c4e 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73   NULL pointer, s
18c4f 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61  et pMem to conta
18c50 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20  in an SQL NULL. 
18c51 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20  */.  if( !z ){. 
18c52 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
18c53 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a  mSetNull(pMem);.
18c54 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
18c55 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  E_OK;.  }..  if(
18c56 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20   pMem->db ){.   
18c57 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e   iLimit = pMem->
18c58 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
18c59 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b  E_LIMIT_LENGTH];
18c5a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c  .  }else{.    iL
18c5b 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41  imit = SQLITE_MA
18c5c 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20  X_LENGTH;.  }.  
18c5d 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f  flags = (enc==0?
18c5e 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72  MEM_Blob:MEM_Str
18c5f 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30  );.  if( nByte<0
18c60 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
18c61 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66  enc!=0 );.    if
18c62 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ( enc==SQLITE_UT
18c63 46 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  F8 ){.      for(
18c64 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d  nByte=0; nByte<=
18c65 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74  iLimit && z[nByt
18c66 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20  e]; nByte++){}. 
18c67 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
18c68 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79  for(nByte=0; nBy
18c69 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a  te<=iLimit && (z
18c6a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74  [nByte] | z[nByt
18c6b 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29  e+1]); nByte+=2)
18c6c 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61  {}.    }.    fla
18c6d 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a  gs |= MEM_Term;.
18c6e 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f    }..  /* The fo
18c6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65  llowing block se
18c70 74 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65  ts the new value
18c71 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d  s of Mem.z and M
18c72 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a  em.xDel. It.  **
18c73 20 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61   also sets a fla
18c74 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61  g in local varia
18c75 62 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69  ble "flags" to i
18c76 6e 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f  ndicate the memo
18c77 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65  ry.  ** manageme
18c78 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44  nt (one of MEM_D
18c79 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63  yn or MEM_Static
18c7a 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44  )..  */.  if( xD
18c7b 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53  el==SQLITE_TRANS
18c7c 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20  IENT ){.    int 
18c7d 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a  nAlloc = nByte;.
18c7e 20 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45      if( flags&ME
18c7f 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  M_Term ){.      
18c80 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d  nAlloc += (enc==
18c81 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29  SQLITE_UTF8?1:2)
18c82 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
18c83 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a  nByte>iLimit ){.
18c84 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
18c85 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20  ITE_TOOBIG;.    
18c86 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
18c87 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
18c88 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b  m, nAlloc, 0) ){
18c89 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
18c8a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
18c8b 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65  }.    memcpy(pMe
18c8c 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29  m->z, z, nAlloc)
18c8d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44  ;.  }else if( xD
18c8e 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d  el==SQLITE_DYNAM
18c8f 49 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  IC ){.    sqlite
18c90 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
18c91 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d  pMem);.    pMem-
18c92 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d  >zMalloc = pMem-
18c93 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a  >z = (char *)z;.
18c94 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d      pMem->xDel =
18c95 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
18c96 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
18c97 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
18c98 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61    pMem->z = (cha
18c99 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d  r *)z;.    pMem-
18c9a 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20  >xDel = xDel;.  
18c9b 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65    flags |= ((xDe
18c9c 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43  l==SQLITE_STATIC
18c9d 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d  )?MEM_Static:MEM
18c9e 5f 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28  _Dyn);.  }.  if(
18c9f 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b   nByte>iLimit ){
18ca0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
18ca1 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a  TE_TOOBIG;.  }..
18ca2 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74    pMem->n = nByt
18ca3 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  e;.  pMem->flags
18ca4 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d   = flags;.  pMem
18ca5 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20  ->enc = (enc==0 
18ca6 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20  ? SQLITE_UTF8 : 
18ca7 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79  enc);.  pMem->ty
18ca8 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53  pe = (enc==0 ? S
18ca9 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c  QLITE_BLOB : SQL
18caa 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e  ITE_TEXT);..#ifn
18cab 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18cac 55 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d  UTF16.  if( pMem
18cad 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54  ->enc!=SQLITE_UT
18cae 46 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  F8 && sqlite3Vdb
18caf 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d  eMemHandleBom(pM
18cb0 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  em) ){.    retur
18cb1 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
18cb2 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65    }.#endif..  re
18cb3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
18cb4 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
18cb5 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74   the values cont
18cb6 61 69 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f  ained by the two
18cb7 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72   memory cells, r
18cb8 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61  eturning.** nega
18cb9 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f  tive, zero or po
18cba 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20  sitive if pMem1 
18cbb 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71  is less than, eq
18cbc 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
18cbd 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32  er.** than pMem2
18cbe 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20  . Sorting order 
18cbf 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c  is NULL's first,
18cc0 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d   followed by num
18cc1 62 65 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a  bers (integers.*
18cc2 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72  * and reals) sor
18cc3 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c  ted numerically,
18cc4 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78   followed by tex
18cc5 74 20 6f 72 64 65 72 65 64 20 62 79 20 74 68 65  t ordered by the
18cc6 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65   collating.** se
18cc7 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64  quence pColl and
18cc8 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20   finally blob's 
18cc9 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d  ordered by memcm
18cca 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e  p()..**.** Two N
18ccb 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63  ULL values are c
18ccc 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20  onsidered equal 
18ccd 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
18cce 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18ccf 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
18cd0 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  emCompare(const 
18cd1 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73  Mem *pMem1, cons
18cd2 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f  t Mem *pMem2, co
18cd3 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  nst CollSeq *pCo
18cd4 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ll){.  int rc;. 
18cd5 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69   int f1, f2;.  i
18cd6 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  nt combined_flag
18cd7 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68  s;..  /* Interch
18cd8 61 6e 67 65 20 70 4d 65 6d 31 20 61 6e 64 20 70  ange pMem1 and p
18cd9 4d 65 6d 32 20 69 66 20 74 68 65 20 63 6f 6c 6c  Mem2 if the coll
18cda 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73  ating sequence s
18cdb 70 65 63 69 66 69 65 73 0a 20 20 2a 2a 20 44 45  pecifies.  ** DE
18cdc 53 43 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20  SC order..  */. 
18cdd 20 66 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61   f1 = pMem1->fla
18cde 67 73 3b 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32  gs;.  f2 = pMem2
18cdf 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69  ->flags;.  combi
18ce0 6e 65 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66  ned_flags = f1|f
18ce1 32 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 6f  2;.  assert( (co
18ce2 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 26 20 4d  mbined_flags & M
18ce3 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
18ce4 0a 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76  . .  /* If one v
18ce5 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74  alue is NULL, it
18ce6 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68   is less than th
18ce7 65 20 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68  e other. If both
18ce8 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65   values.  ** are
18ce9 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e   NULL, return 0.
18cea 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62  .  */.  if( comb
18ceb 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e  ined_flags&MEM_N
18cec 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ull ){.    retur
18ced 6e 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20  n (f2&MEM_Null) 
18cee 2d 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b  - (f1&MEM_Null);
18cef 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e  .  }..  /* If on
18cf0 65 20 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d  e value is a num
18cf1 62 65 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65  ber and the othe
18cf2 72 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75  r is not, the nu
18cf3 6d 62 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20  mber is less..  
18cf4 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e  ** If both are n
18cf5 75 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20  umbers, compare 
18cf6 61 73 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20  as reals if one 
18cf7 69 73 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73  is a real, or as
18cf8 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69   integers.  ** i
18cf9 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72  f both values ar
18cfa 65 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f  e integers..  */
18cfb 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f  .  if( combined_
18cfc 66 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d  flags&(MEM_Int|M
18cfd 45 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20  EM_Real) ){.    
18cfe 69 66 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e  if( !(f1&(MEM_In
18cff 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a  t|MEM_Real)) ){.
18d00 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
18d01 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28      }.    if( !(
18d02 66 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  f2&(MEM_Int|MEM_
18d03 52 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20  Real)) ){.      
18d04 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d  return -1;.    }
18d05 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 66  .    if( (f1 & f
18d06 32 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20  2 & MEM_Int)==0 
18d07 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  ){.      double 
18d08 72 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66  r1, r2;.      if
18d09 28 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d  ( (f1&MEM_Real)=
18d0a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31  =0 ){.        r1
18d0b 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 31   = (double)pMem1
18d0c 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c  ->u.i;.      }el
18d0d 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d  se{.        r1 =
18d0e 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20   pMem1->r;.     
18d0f 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 66 32   }.      if( (f2
18d10 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b  &MEM_Real)==0 ){
18d11 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 28 64  .        r2 = (d
18d12 6f 75 62 6c 65 29 70 4d 65 6d 32 2d 3e 75 2e 69  ouble)pMem2->u.i
18d13 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
18d14 20 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d         r2 = pMem
18d15 32 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  2->r;.      }.  
18d16 20 20 20 20 69 66 28 20 72 31 3c 72 32 20 29 20      if( r1<r2 ) 
18d17 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20  return -1;.     
18d18 20 69 66 28 20 72 31 3e 72 32 20 29 20 72 65 74   if( r1>r2 ) ret
18d19 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74  urn 1;.      ret
18d1a 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  urn 0;.    }else
18d1b 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
18d1c 66 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20  f1&MEM_Int );.  
18d1d 20 20 20 20 61 73 73 65 72 74 28 20 66 32 26 4d      assert( f2&M
18d1e 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20  EM_Int );.      
18d1f 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c  if( pMem1->u.i <
18d20 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65   pMem2->u.i ) re
18d21 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69  turn -1;.      i
18d22 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20  f( pMem1->u.i > 
18d23 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74  pMem2->u.i ) ret
18d24 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74  urn 1;.      ret
18d25 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
18d26 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61  ..  /* If one va
18d27 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20  lue is a string 
18d28 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 69 73  and the other is
18d29 20 61 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72   a blob, the str
18d2a 69 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a  ing is less..  *
18d2b 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 73 74  * If both are st
18d2c 72 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75  rings, compare u
18d2d 73 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69  sing the collati
18d2e 6e 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20  ng functions..  
18d2f 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65  */.  if( combine
18d30 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20  d_flags&MEM_Str 
18d31 29 7b 0a 20 20 20 20 69 66 28 20 28 66 31 20 26  ){.    if( (f1 &
18d32 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a   MEM_Str)==0 ){.
18d33 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
18d34 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66      }.    if( (f
18d35 32 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20  2 & MEM_Str)==0 
18d36 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
18d37 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61  -1;.    }..    a
18d38 73 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e  ssert( pMem1->en
18d39 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b  c==pMem2->enc );
18d3a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
18d3b 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  m1->enc==SQLITE_
18d3c 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20  UTF8 || .       
18d3d 20 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d       pMem1->enc=
18d3e 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
18d3f 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53  || pMem1->enc==S
18d40 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b  QLITE_UTF16BE );
18d41 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c  ..    /* The col
18d42 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
18d43 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20  must be defined 
18d44 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65  at this point, e
18d45 76 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68  ven if.    ** th
18d46 65 20 75 73 65 72 20 64 65 6c 65 74 65 73 20 74  e user deletes t
18d47 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
18d48 75 65 6e 63 65 20 61 66 74 65 72 20 74 68 65 20  uence after the 
18d49 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a  vdbe program is.
18d4a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20      ** compiled 
18d4b 28 74 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c  (this was not al
18d4c 77 61 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a  ways the case)..
18d4d 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
18d4e 74 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f  t( !pColl || pCo
18d4f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20  ll->xCmp );..   
18d50 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
18d51 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65      if( pMem1->e
18d52 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29  nc==pColl->enc )
18d53 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
18d54 20 73 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72   strings are alr
18d55 65 61 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72  eady in the corr
18d56 65 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43  ect encoding.  C
18d57 61 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20  all the.        
18d58 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75  ** comparison fu
18d59 6e 63 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20  nction directly 
18d5a 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
18d5b 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43  n pColl->xCmp(pC
18d5c 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31  oll->pUser,pMem1
18d5d 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65  ->n,pMem1->z,pMe
18d5e 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b  m2->n,pMem2->z);
18d5f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
18d60 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64        const void
18d61 20 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20   *v1, *v2;.     
18d62 20 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20     int n1, n2;. 
18d63 20 20 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20         Mem c1;. 
18d64 20 20 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20         Mem c2;. 
18d65 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63         memset(&c
18d66 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29  1, 0, sizeof(c1)
18d67 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65  );.        memse
18d68 74 28 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66  t(&c2, 0, sizeof
18d69 28 63 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73  (c2));.        s
18d6a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
18d6b 6c 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d  llowCopy(&c1, pM
18d6c 65 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b  em1, MEM_Ephem);
18d6d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
18d6e 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
18d6f 70 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d  py(&c2, pMem2, M
18d70 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20  EM_Ephem);.     
18d71 20 20 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56     v1 = sqlite3V
18d72 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65  alueText((sqlite
18d73 33 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43  3_value*)&c1, pC
18d74 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20  oll->enc);.     
18d75 20 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20     n1 = v1==0 ? 
18d76 30 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20  0 : c1.n;.      
18d77 20 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61    v2 = sqlite3Va
18d78 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33  lueText((sqlite3
18d79 5f 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f  _value*)&c2, pCo
18d7a 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20  ll->enc);.      
18d7b 20 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30    n2 = v2==0 ? 0
18d7c 20 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20   : c2.n;.       
18d7d 20 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d   rc = pColl->xCm
18d7e 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20  p(pColl->pUser, 
18d7f 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b  n1, v1, n2, v2);
18d80 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
18d81 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
18d82 63 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  c1);.        sql
18d83 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
18d84 73 65 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20  se(&c2);.       
18d85 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
18d86 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a    }.    }.    /*
18d87 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   If a NULL point
18d88 65 72 20 77 61 73 20 70 61 73 73 65 64 20 61 73  er was passed as
18d89 20 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e   the collate fun
18d8a 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f  ction, fall thro
18d8b 75 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68  ugh.    ** to th
18d8c 65 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20  e blob case and 
18d8d 75 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a  use memcmp().  *
18d8e 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74  /.  }. .  /* Bot
18d8f 68 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65  h values must be
18d90 20 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65   blobs.  Compare
18d91 20 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e   using memcmp().
18d92 20 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63    */.  rc = memc
18d93 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65  mp(pMem1->z, pMe
18d94 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e  m2->z, (pMem1->n
18d95 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32  >pMem2->n)?pMem2
18d96 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20  ->n:pMem1->n);. 
18d97 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20   if( rc==0 ){.  
18d98 20 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20    rc = pMem1->n 
18d99 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a  - pMem2->n;.  }.
18d9a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
18d9b 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20  /*.** Move data 
18d9c 6f 75 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b  out of a btree k
18d9d 65 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64  ey or data field
18d9e 20 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20   and into a Mem 
18d9f 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68  structure..** Th
18da0 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73  e data or key is
18da1 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20   taken from the 
18da2 65 6e 74 72 79 20 74 68 61 74 20 70 43 75 72 20  entry that pCur 
18da3 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
18da4 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66  nting.** to.  of
18da5 66 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74  fset and amt det
18da6 65 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74  ermine what port
18da7 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20  ion of the data 
18da8 6f 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65  or key to retrie
18da9 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72  ve..** key is tr
18daa 75 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65  ue to get the ke
18dab 79 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65  y or false to ge
18dac 74 20 64 61 74 61 2e 20 20 54 68 65 20 72 65 73  t data.  The res
18dad 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a  ult is written.*
18dae 2a 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20  * into the pMem 
18daf 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  element..**.** T
18db0 68 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72  he pMem structur
18db1 65 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  e is assumed to 
18db2 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  be uninitialized
18db3 2e 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e  .  Any prior con
18db4 74 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77  tent.** is overw
18db5 72 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62  ritten without b
18db6 65 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a  eing freed..**.*
18db7 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * If this routin
18db8 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20  e fails for any 
18db9 72 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72  reason (malloc r
18dba 65 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75  eturns NULL or u
18dbb 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64  nable.** to read
18dbc 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20   from the disk) 
18dbd 74 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73  then the pMem is
18dbe 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f   left in an inco
18dbf 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a  nsistent state..
18dc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18dc1 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
18dc2 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20  eMemFromBtree(. 
18dc3 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c   BtCursor *pCur,
18dc4 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69     /* Cursor poi
18dc5 6e 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20  nting at record 
18dc6 74 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a  to retrieve. */.
18dc7 20 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20    int offset,   
18dc8 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72      /* Offset fr
18dc9 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  om the start of 
18dca 64 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62  data to return b
18dcb 79 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20  ytes from. */.  
18dcc 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20  int amt,        
18dcd 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
18dce 79 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20  ytes to return. 
18dcf 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20  */.  int key,   
18dd0 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75         /* If tru
18dd1 65 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d  e, retrieve from
18dd2 20 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20   the btree key, 
18dd3 6e 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d  not data. */.  M
18dd4 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20  em *pMem        
18dd5 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20   /* OUT: Return 
18dd6 64 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d  data in this Mem
18dd7 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29   structure. */.)
18dd8 7b 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b  {.  char *zData;
18dd9 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66         /* Data f
18dda 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61  rom the btree la
18ddb 79 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61  yer */.  int ava
18ddc 69 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e  ilable = 0; /* N
18ddd 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61  umber of bytes a
18dde 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
18ddf 6c 6f 63 61 6c 20 62 74 72 65 65 20 70 61 67 65  local btree page
18de0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
18de1 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  b;       /* Data
18de2 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
18de3 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
18de4 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d  LITE_OK;..  db =
18de5 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
18de6 73 6f 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61  sorDb(pCur);.  a
18de7 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
18de8 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
18de9 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
18dea 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
18deb 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
18dec 29 3b 0a 20 20 69 66 28 20 6b 65 79 20 29 7b 0a  );.  if( key ){.
18ded 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61      zData = (cha
18dee 72 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65  r *)sqlite3Btree
18def 4b 65 79 46 65 74 63 68 28 70 43 75 72 2c 20 26  KeyFetch(pCur, &
18df0 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 65  available);.  }e
18df1 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d  lse{.    zData =
18df2 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
18df3 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28 70  BtreeDataFetch(p
18df4 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29  Cur, &available)
18df5 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
18df6 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20 20 69  zData!=0 );..  i
18df7 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 61  f( offset+amt<=a
18df8 76 61 69 6c 61 62 6c 65 20 26 26 20 28 28 70 4d  vailable && ((pM
18df9 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  em->flags&MEM_Dy
18dfa 6e 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d 3e 78  n)==0 || pMem->x
18dfb 44 65 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  Del) ){.    sqli
18dfc 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
18dfd 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65  e(pMem);.    pMe
18dfe 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 5b 6f 66  m->z = &zData[of
18dff 66 73 65 74 5d 3b 0a 20 20 20 20 70 4d 65 6d 2d  fset];.    pMem-
18e00 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f  >flags = MEM_Blo
18e01 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d  b|MEM_Ephem;.  }
18e02 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f  else if( SQLITE_
18e03 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
18e04 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
18e05 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 29 20 29 7b  m, amt+2, 0)) ){
18e06 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
18e07 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f   = MEM_Blob|MEM_
18e08 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20  Dyn|MEM_Term;.  
18e09 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 30 3b    pMem->enc = 0;
18e0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  .    pMem->type 
18e0b 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20  = SQLITE_BLOB;. 
18e0c 20 20 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20     if( key ){.  
18e0d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
18e0e 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 6f  BtreeKey(pCur, o
18e0f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d  ffset, amt, pMem
18e10 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ->z);.    }else{
18e11 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
18e12 74 65 33 42 74 72 65 65 44 61 74 61 28 70 43 75  te3BtreeData(pCu
18e13 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20  r, offset, amt, 
18e14 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a  pMem->z);.    }.
18e15 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 5d      pMem->z[amt]
18e16 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e   = 0;.    pMem->
18e17 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a 20 20  z[amt+1] = 0;.  
18e18 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18e19 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
18e1a 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
18e1b 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a  se(pMem);.    }.
18e1c 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20    }.  pMem->n = 
18e1d 61 6d 74 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72  amt;..  return r
18e1e 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75  c;.}../* This fu
18e1f 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61  nction is only a
18e20 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 72 6e 61  vailable interna
18e21 6c 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 70  lly, it is not p
18e22 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78  art of the.** ex
18e23 74 65 72 6e 61 6c 20 41 50 49 2e 20 49 74 20 77  ternal API. It w
18e24 6f 72 6b 73 20 69 6e 20 61 20 73 69 6d 69 6c 61  orks in a simila
18e25 72 20 77 61 79 20 74 6f 20 73 71 6c 69 74 65 33  r way to sqlite3
18e26 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 2c 0a 2a  _value_text(),.*
18e27 2a 20 65 78 63 65 70 74 20 74 68 65 20 64 61 74  * except the dat
18e28 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e  a returned is in
18e29 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 73 70   the encoding sp
18e2a 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 73  ecified by the s
18e2b 65 63 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74  econd.** paramet
18e2c 65 72 2c 20 77 68 69 63 68 20 6d 75 73 74 20 62  er, which must b
18e2d 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f  e one of SQLITE_
18e2e 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f  UTF16BE, SQLITE_
18e2f 55 54 46 31 36 4c 45 20 6f 72 0a 2a 2a 20 53 51  UTF16LE or.** SQ
18e30 4c 49 54 45 5f 55 54 46 38 2e 0a 2a 2a 0a 2a 2a  LITE_UTF8..**.**
18e31 20 28 32 30 30 36 2d 30 32 2d 31 36 3a 29 20 20   (2006-02-16:)  
18e32 54 68 65 20 65 6e 63 20 76 61 6c 75 65 20 63 61  The enc value ca
18e33 6e 20 62 65 20 6f 72 2d 65 64 20 77 69 74 68 20  n be or-ed with 
18e34 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
18e35 47 4e 45 44 2e 0a 2a 2a 20 49 66 20 74 68 61 74  GNED..** If that
18e36 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
18e37 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6d 75  en the result mu
18e38 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20 6f 6e  st be aligned on
18e39 20 61 6e 20 65 76 65 6e 20 62 79 74 65 0a 2a 2a   an even byte.**
18e3a 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 53 51   boundary..*/.SQ
18e3b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
18e3c 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
18e3d 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65  ValueText(sqlite
18e3e 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 2c 20 75  3_value* pVal, u
18e3f 38 20 65 6e 63 29 7b 0a 20 20 69 66 28 20 21 70  8 enc){.  if( !p
18e40 56 61 6c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  Val ) return 0;.
18e41 0a 20 20 61 73 73 65 72 74 28 20 70 56 61 6c 2d  .  assert( pVal-
18e42 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
18e43 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 56 61  3_mutex_held(pVa
18e44 6c 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  l->db->mutex) );
18e45 0a 20 20 61 73 73 65 72 74 28 20 28 65 6e 63 26  .  assert( (enc&
18e46 33 29 3d 3d 28 65 6e 63 26 7e 53 51 4c 49 54 45  3)==(enc&~SQLITE
18e47 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20  _UTF16_ALIGNED) 
18e48 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 56  );.  assert( (pV
18e49 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  al->flags & MEM_
18e4a 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20  RowSet)==0 );.. 
18e4b 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73   if( pVal->flags
18e4c 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20  &MEM_Null ){.   
18e4d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
18e4e 20 61 73 73 65 72 74 28 20 28 4d 45 4d 5f 42 6c   assert( (MEM_Bl
18e4f 6f 62 3e 3e 33 29 20 3d 3d 20 4d 45 4d 5f 53 74  ob>>3) == MEM_St
18e50 72 20 29 3b 0a 20 20 70 56 61 6c 2d 3e 66 6c 61  r );.  pVal->fla
18e51 67 73 20 7c 3d 20 28 70 56 61 6c 2d 3e 66 6c 61  gs |= (pVal->fla
18e52 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e  gs & MEM_Blob)>>
18e53 33 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28  3;.  expandBlob(
18e54 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 70 56 61  pVal);.  if( pVa
18e55 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72  l->flags&MEM_Str
18e56 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
18e57 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
18e58 67 28 70 56 61 6c 2c 20 65 6e 63 20 26 20 7e 53  g(pVal, enc & ~S
18e59 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
18e5a 4e 45 44 29 3b 0a 20 20 20 20 69 66 28 20 28 65  NED);.    if( (e
18e5b 6e 63 20 26 20 53 51 4c 49 54 45 5f 55 54 46 31  nc & SQLITE_UTF1
18e5c 36 5f 41 4c 49 47 4e 45 44 29 21 3d 30 20 26 26  6_ALIGNED)!=0 &&
18e5d 20 31 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54   1==(1&SQLITE_PT
18e5e 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a  R_TO_INT(pVal->z
18e5f 29 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  )) ){.      asse
18e60 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73  rt( (pVal->flags
18e61 20 26 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45   & (MEM_Ephem|ME
18e62 4d 5f 53 74 61 74 69 63 29 29 21 3d 30 20 29 3b  M_Static))!=0 );
18e63 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
18e64 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69  e3VdbeMemMakeWri
18e65 74 65 61 62 6c 65 28 70 56 61 6c 29 21 3d 53 51  teable(pVal)!=SQ
18e66 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
18e67 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
18e68 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
18e69 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c  qlite3VdbeMemNul
18e6a 54 65 72 6d 69 6e 61 74 65 28 70 56 61 6c 29 3b  Terminate(pVal);
18e6b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
18e6c 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61  sert( (pVal->fla
18e6d 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20  gs&MEM_Blob)==0 
18e6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
18e6f 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70  beMemStringify(p
18e70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 61  Val, enc);.    a
18e71 73 73 65 72 74 28 20 30 3d 3d 28 31 26 53 51 4c  ssert( 0==(1&SQL
18e72 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70  ITE_PTR_TO_INT(p
18e73 56 61 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20 7d 0a  Val->z)) );.  }.
18e74 20 20 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65    assert(pVal->e
18e75 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49  nc==(enc & ~SQLI
18e76 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
18e77 29 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30  ) || pVal->db==0
18e78 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c  .              |
18e79 7c 20 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c  | pVal->db->mall
18e7a 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66  ocFailed );.  if
18e7b 28 20 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e  ( pVal->enc==(en
18e7c 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31  c & ~SQLITE_UTF1
18e7d 36 5f 41 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20  6_ALIGNED) ){.  
18e7e 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a    return pVal->z
18e7f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
18e80 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  eturn 0;.  }.}..
18e81 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
18e82 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ew sqlite3_value
18e83 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49   object..*/.SQLI
18e84 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
18e85 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65  e3_value *sqlite
18e86 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65  3ValueNew(sqlite
18e87 33 20 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70  3 *db){.  Mem *p
18e88 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
18e89 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
18e8a 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 70 20  f(*p));.  if( p 
18e8b 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  ){.    p->flags 
18e8c 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
18e8d 70 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  p->type = SQLITE
18e8e 5f 4e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62  _NULL;.    p->db
18e8f 20 3d 20 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74   = db;.  }.  ret
18e90 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
18e91 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c  Create a new sql
18e92 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
18e93 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  t, containing th
18e94 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72  e value of pExpr
18e95 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c  ..**.** This onl
18e96 79 20 77 6f 72 6b 73 20 66 6f 72 20 76 65 72 79  y works for very
18e97 20 73 69 6d 70 6c 65 20 65 78 70 72 65 73 73 69   simple expressi
18e98 6f 6e 73 20 74 68 61 74 20 63 6f 6e 73 69 73 74  ons that consist
18e99 20 6f 66 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74   of one constant
18e9a 0a 2a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20  .** token (i.e. 
18e9b 22 35 22 2c 20 22 35 2e 31 22 2c 20 22 27 61 20  "5", "5.1", "'a 
18e9c 73 74 72 69 6e 67 27 22 29 2e 20 49 66 20 74 68  string'"). If th
18e9d 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e  e expression can
18e9e 0a 2a 2a 20 62 65 20 63 6f 6e 76 65 72 74 65 64  .** be converted
18e9f 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 61   directly into a
18ea0 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65   value, then the
18ea1 20 76 61 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61   value is alloca
18ea2 74 65 64 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69  ted and.** a poi
18ea3 6e 74 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20  nter written to 
18ea4 2a 70 70 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c  *ppVal. The call
18ea5 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  er is responsibl
18ea6 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69  e for deallocati
18ea7 6e 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  ng.** the value 
18ea8 62 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f  by passing it to
18ea9 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
18eaa 65 28 29 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66  e() later on. If
18eab 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a   the expression.
18eac 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e  ** cannot be con
18ead 76 65 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75  verted to a valu
18eae 65 2c 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69  e, then *ppVal i
18eaf 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  s set to NULL..*
18eb0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18eb1 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75   int sqlite3Valu
18eb2 65 46 72 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c  eFromExpr(.  sql
18eb3 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
18eb4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
18eb5 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
18eb6 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  n */.  Expr *pEx
18eb7 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pr,             
18eb8 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
18eb9 6f 6e 20 74 6f 20 65 76 61 6c 75 61 74 65 20 2a  on to evaluate *
18eba 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20  /.  u8 enc,     
18ebb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18ebc 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65   Encoding to use
18ebd 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74   */.  u8 affinit
18ebe 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
18ebf 2f 2a 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75  /* Affinity to u
18ec0 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  se */.  sqlite3_
18ec1 76 61 6c 75 65 20 2a 2a 70 70 56 61 6c 20 20 20  value **ppVal   
18ec2 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e    /* Write the n
18ec3 65 77 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f  ew value here */
18ec4 0a 29 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20  .){.  int op;.  
18ec5 63 68 61 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a  char *zVal = 0;.
18ec6 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
18ec7 2a 70 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66  *pVal = 0;..  if
18ec8 28 20 21 70 45 78 70 72 20 29 7b 0a 20 20 20 20  ( !pExpr ){.    
18ec9 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20  *ppVal = 0;.    
18eca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
18ecb 3b 0a 20 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78  ;.  }.  op = pEx
18ecc 70 72 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f  pr->op;..  if( o
18ecd 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20  p==TK_STRING || 
18ece 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20  op==TK_FLOAT || 
18ecf 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29  op==TK_INTEGER )
18ed0 7b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c  {.    zVal = sql
18ed1 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62  ite3DbStrNDup(db
18ed2 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e  , (char*)pExpr->
18ed3 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e  token.z, pExpr->
18ed4 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56  token.n);.    pV
18ed5 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  al = sqlite3Valu
18ed6 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66  eNew(db);.    if
18ed7 28 20 21 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c  ( !zVal || !pVal
18ed8 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
18ed9 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
18eda 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c  SetStr(pVal, -1,
18edb 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54   zVal, SQLITE_UT
18edc 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  F8, SQLITE_DYNAM
18edd 49 43 29 3b 0a 20 20 20 20 69 66 28 20 28 6f 70  IC);.    if( (op
18ede 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20  ==TK_INTEGER || 
18edf 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26  op==TK_FLOAT ) &
18ee0 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49  & affinity==SQLI
18ee1 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20  TE_AFF_NONE ){. 
18ee2 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
18ee3 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70  eApplyAffinity(p
18ee4 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  Val, SQLITE_AFF_
18ee5 4e 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f  NUMERIC, SQLITE_
18ee6 55 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65  UTF8);.    }else
18ee7 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
18ee8 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74  alueApplyAffinit
18ee9 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79  y(pVal, affinity
18eea 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
18eeb 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e      }.    if( en
18eec 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c!=SQLITE_UTF8 )
18eed 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
18eee 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
18eef 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20  g(pVal, enc);.  
18ef0 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
18ef1 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20  op==TK_UMINUS ) 
18ef2 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  {.    if( SQLITE
18ef3 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75  _OK==sqlite3Valu
18ef4 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 70 45 78  eFromExpr(db,pEx
18ef5 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66  pr->pLeft,enc,af
18ef6 66 69 6e 69 74 79 2c 26 70 56 61 6c 29 20 29 7b  finity,&pVal) ){
18ef7 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69  .      pVal->u.i
18ef8 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e   = -1 * pVal->u.
18ef9 69 3b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75  i;.      /* (dou
18efa 62 6c 65 29 2d 31 20 49 6e 20 63 61 73 65 20 6f  ble)-1 In case o
18efb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
18efc 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20  OATING_POINT... 
18efd 2a 2f 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 72  */.      pVal->r
18efe 20 3d 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20   = (double)-1 * 
18eff 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20  pVal->r;.    }. 
18f00 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
18f01 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
18f02 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 6f  RAL.  else if( o
18f03 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20  p==TK_BLOB ){.  
18f04 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20    int nVal;.    
18f05 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74  assert( pExpr->t
18f06 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20  oken.n>=3 );.   
18f07 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
18f08 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20  token.z[0]=='x' 
18f09 7c 7c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  || pExpr->token.
18f0a 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20  z[0]=='X' );.   
18f0b 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
18f0c 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27  token.z[1]=='\''
18f0d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
18f0e 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70  pExpr->token.z[p
18f0f 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d  Expr->token.n-1]
18f10 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70 56  =='\'' );.    pV
18f11 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  al = sqlite3Valu
18f12 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66  eNew(db);.    if
18f13 28 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e  ( !pVal ) goto n
18f14 6f 5f 6d 65 6d 3b 0a 20 20 20 20 6e 56 61 6c 20  o_mem;.    nVal 
18f15 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  = pExpr->token.n
18f16 20 2d 20 33 3b 0a 20 20 20 20 7a 56 61 6c 20 3d   - 3;.    zVal =
18f17 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74   (char*)pExpr->t
18f18 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20  oken.z + 2;.    
18f19 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
18f1a 74 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74  tStr(pVal, sqlit
18f1b 65 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20  e3HexToBlob(db, 
18f1c 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61  zVal, nVal), nVa
18f1d 6c 2f 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20  l/2,.           
18f1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
18f1f 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
18f20 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
18f21 2a 70 70 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20  *ppVal = pVal;. 
18f22 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
18f23 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62  K;..no_mem:.  db
18f24 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
18f25 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46   1;.  sqlite3DbF
18f26 72 65 65 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20  ree(db, zVal);. 
18f27 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
18f28 65 28 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61  e(pVal);.  *ppVa
18f29 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  l = 0;.  return 
18f2a 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a  SQLITE_NOMEM;.}.
18f2b 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
18f2c 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f  e string value o
18f2d 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  f an sqlite3_val
18f2e 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c  ue object.*/.SQL
18f2f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
18f30 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
18f31 53 74 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  Str(.  sqlite3_v
18f32 61 6c 75 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20  alue *v,     /* 
18f33 56 61 6c 75 65 20 74 6f 20 62 65 20 73 65 74 20  Value to be set 
18f34 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20  */.  int n,     
18f35 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
18f36 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a  ngth of string z
18f37 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
18f38 20 2a 7a 2c 20 20 20 20 20 20 20 20 2f 2a 20 54   *z,        /* T
18f39 65 78 74 20 6f 66 20 74 68 65 20 6e 65 77 20 73  ext of the new s
18f3a 74 72 69 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e  tring */.  u8 en
18f3b 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
18f3c 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20   /* Encoding to 
18f3d 75 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  use */.  void (*
18f3e 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f  xDel)(void*)   /
18f3f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72  * Destructor for
18f40 20 74 68 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29   the string */.)
18f41 7b 0a 20 20 69 66 28 20 76 20 29 20 73 71 6c 69  {.  if( v ) sqli
18f42 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
18f43 28 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c  ((Mem *)v, z, n,
18f44 20 65 6e 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a   enc, xDel);.}..
18f45 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 73 71  /*.** Free an sq
18f46 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
18f47 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ct.*/.SQLITE_PRI
18f48 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
18f49 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74  3ValueFree(sqlit
18f4a 65 33 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20  e3_value *v){.  
18f4b 69 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b  if( !v ) return;
18f4c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
18f4d 6d 52 65 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29  mRelease((Mem *)
18f4e 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  v);.  sqlite3DbF
18f4f 72 65 65 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64  ree(((Mem*)v)->d
18f50 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  b, v);.}../*.** 
18f51 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
18f52 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
18f53 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  e sqlite3_value 
18f54 6f 62 6a 65 63 74 20 61 73 73 75 6d 69 6e 67 0a  object assuming.
18f55 2a 2a 20 74 68 61 74 20 69 74 20 75 73 65 73 20  ** that it uses 
18f56 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e  the encoding "en
18f57 63 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  c".*/.SQLITE_PRI
18f58 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
18f59 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74  ValueBytes(sqlit
18f5a 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20  e3_value *pVal, 
18f5b 75 38 20 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a  u8 enc){.  Mem *
18f5c 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a  p = (Mem*)pVal;.
18f5d 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20    if( (p->flags 
18f5e 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c  & MEM_Blob)!=0 |
18f5f 7c 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  | sqlite3ValueTe
18f60 78 74 28 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b  xt(pVal, enc) ){
18f61 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67  .    if( p->flag
18f62 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a  s & MEM_Zero ){.
18f63 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e        return p->
18f64 6e 20 2b 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a  n + p->u.nZero;.
18f65 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
18f66 20 72 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20   return p->n;.  
18f67 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
18f68 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   0;.}../********
18f69 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64  ****** End of vd
18f6a 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  bemem.c ********
18f6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f6d 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
18f6e 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
18f6f 65 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a  e vdbeaux.c ****
18f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f72 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
18f73 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a  3 September 6.**
18f74 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
18f75 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
18f76 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
18f77 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
18f78 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
18f79 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
18f7a 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
18f7b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
18f7c 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
18f7d 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
18f7e 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
18f7f 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
18f80 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
18f81 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
18f82 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
18f83 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
18f84 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
18f85 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
18f86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
18f8a 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
18f8b 6e 73 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72  ns code used for
18f8c 20 63 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72   creating, destr
18f8d 6f 79 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c  oying, and popul
18f8e 61 74 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20  ating.** a VDBE 
18f8f 28 6f 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f  (or an "sqlite3_
18f90 73 74 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b  stmt" as it is k
18f91 6e 6f 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73  nown to the outs
18f92 69 64 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69  ide world.)  Pri
18f93 6f 72 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e  or.** to version
18f94 20 32 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73   2.8.7, all this
18f95 20 63 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e   code was combin
18f96 65 64 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65  ed into the vdbe
18f97 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a  .c source file..
18f98 2a 2a 20 42 75 74 20 74 68 61 74 20 66 69 6c 65  ** But that file
18f99 20 77 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f   was getting too
18f9a 20 62 69 67 20 73 6f 20 74 68 69 73 20 73 75 62   big so this sub
18f9b 72 6f 75 74 69 6e 65 73 20 77 65 72 65 20 73 70  routines were sp
18f9c 6c 69 74 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24  lit out..**.** $
18f9d 49 64 3a 20 76 64 62 65 61 75 78 2e 63 2c 76 20  Id: vdbeaux.c,v 
18f9e 31 2e 34 35 37 20 32 30 30 39 2f 30 35 2f 30 36  1.457 2009/05/06
18f9f 20 31 38 3a 35 37 3a 31 30 20 73 68 61 6e 65 20   18:57:10 shane 
18fa0 45 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a  Exp $.*/..../*.*
18fa1 2a 20 57 68 65 6e 20 64 65 62 75 67 67 69 6e 67  * When debugging
18fa2 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
18fa3 74 6f 72 20 69 6e 20 61 20 73 79 6d 62 6f 6c 69  tor in a symboli
18fa4 63 20 64 65 62 75 67 67 65 72 2c 20 6f 6e 65 20  c debugger, one 
18fa5 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68 65 20 73  can.** set the s
18fa6 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54  qlite3VdbeAddopT
18fa7 72 61 63 65 20 74 6f 20 31 20 61 6e 64 20 61 6c  race to 1 and al
18fa8 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c 6c 20 62  l opcodes will b
18fa9 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20 61 73 20  e printed.** as 
18faa 74 68 65 79 20 61 72 65 20 61 64 64 65 64 20 74  they are added t
18fab 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f  o the instructio
18fac 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66  n stream..*/.#if
18fad 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
18fae 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18faf 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
18fb0 64 64 6f 70 54 72 61 63 65 20 3d 20 30 3b 0a 23  ddopTrace = 0;.#
18fb1 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72  endif.../*.** Cr
18fb2 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75  eate a new virtu
18fb3 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  al database engi
18fb4 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ne..*/.SQLITE_PR
18fb5 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69  IVATE Vdbe *sqli
18fb6 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71  te3VdbeCreate(sq
18fb7 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64  lite3 *db){.  Vd
18fb8 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  be *p;.  p = sql
18fb9 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
18fba 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 64 62 65  (db, sizeof(Vdbe
18fbb 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  ) );.  if( p==0 
18fbc 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 2d  ) return 0;.  p-
18fbd 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20  >db = db;.  if( 
18fbe 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20  db->pVdbe ){.   
18fbf 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70 50 72 65   db->pVdbe->pPre
18fc0 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e  v = p;.  }.  p->
18fc1 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 56 64 62  pNext = db->pVdb
18fc2 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76 20 3d 20  e;.  p->pPrev = 
18fc3 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62 65 20 3d  0;.  db->pVdbe =
18fc4 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d   p;.  p->magic =
18fc5 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54   VDBE_MAGIC_INIT
18fc6 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
18fc7 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20  ./*.** Remember 
18fc8 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20 66  the SQL string f
18fc9 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74  or a prepared st
18fca 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
18fcb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
18fcc 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 71  sqlite3VdbeSetSq
18fcd 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74  l(Vdbe *p, const
18fce 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
18fcf 20 69 6e 74 20 69 73 50 72 65 70 61 72 65 56 32   int isPrepareV2
18fd0 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  ){.  if( p==0 ) 
18fd1 72 65 74 75 72 6e 3b 0a 23 69 66 64 65 66 20 53  return;.#ifdef S
18fd2 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
18fd3 0a 20 20 69 66 28 20 21 69 73 50 72 65 70 61 72  .  if( !isPrepar
18fd4 65 56 32 20 29 20 72 65 74 75 72 6e 3b 0a 23 65  eV2 ) return;.#e
18fd5 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70  ndif.  assert( p
18fd6 2d 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70  ->zSql==0 );.  p
18fd7 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33  ->zSql = sqlite3
18fd8 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c  DbStrNDup(p->db,
18fd9 20 7a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73 50   z, n);.  p->isP
18fda 72 65 70 61 72 65 56 32 20 3d 20 69 73 50 72 65  repareV2 = isPre
18fdb 70 61 72 65 56 32 20 3f 20 31 20 3a 20 30 3b 0a  pareV2 ? 1 : 0;.
18fdc 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
18fdd 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61 74  the SQL associat
18fde 65 64 20 77 69 74 68 20 61 20 70 72 65 70 61 72  ed with a prepar
18fdf 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a  ed statement.*/.
18fe0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
18fe1 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73   char *sqlite3_s
18fe2 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ql(sqlite3_stmt 
18fe3 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
18fe4 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
18fe5 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  mt;.  return (p-
18fe6 3e 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 70  >isPrepareV2 ? p
18fe7 2d 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a  ->zSql : 0);.}..
18fe8 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63  /*.** Swap all c
18fe9 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74  ontent between t
18fea 77 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72  wo VDBE structur
18feb 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
18fec 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
18fed 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20  e3VdbeSwap(Vdbe 
18fee 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a  *pA, Vdbe *pB){.
18fef 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d    Vdbe tmp, *pTm
18ff0 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b  p;.  char *zTmp;
18ff1 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20  .  tmp = *pA;.  
18ff2 2a 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42  *pA = *pB;.  *pB
18ff3 20 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d   = tmp;.  pTmp =
18ff4 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41   pA->pNext;.  pA
18ff5 2d 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e  ->pNext = pB->pN
18ff6 65 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74  ext;.  pB->pNext
18ff7 20 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20   = pTmp;.  pTmp 
18ff8 3d 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70  = pA->pPrev;.  p
18ff9 41 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70  A->pPrev = pB->p
18ffa 50 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65  Prev;.  pB->pPre
18ffb 76 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70  v = pTmp;.  zTmp
18ffc 20 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70   = pA->zSql;.  p
18ffd 41 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53  A->zSql = pB->zS
18ffe 71 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d  ql;.  pB->zSql =
18fff 20 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50   zTmp;.  pB->isP
19000 72 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69  repareV2 = pA->i
19001 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23  sPrepareV2;.}..#
19002 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
19003 55 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72  UG./*.** Turn tr
19004 61 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a  acing on or off.
19005 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19006 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
19007 62 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c  beTrace(Vdbe *p,
19008 20 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20   FILE *trace){. 
19009 20 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63   p->trace = trac
1900a 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e;.}.#endif../*.
1900b 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64  ** Resize the Vd
1900c 62 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20  be.aOp array so 
1900d 74 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65  that it is at le
1900e 61 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65  ast one op large
1900f 72 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61  r than .** it wa
19010 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f  s..**.** If an o
19011 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72  ut-of-memory err
19012 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
19013 72 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72  resizing the arr
19014 61 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  ay, return.** SQ
19015 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74  LITE_NOMEM. In t
19016 68 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f  his case Vdbe.aO
19017 70 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c  p and Vdbe.nOpAl
19018 6c 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75  loc remain .** u
19019 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69  nchanged (this i
1901a 73 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70  s so that any op
1901b 63 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c  codes already al
1901c 6c 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a  located can be .
1901d 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61  ** correctly dea
1901e 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77  llocated along w
1901f 69 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20  ith the rest of 
19020 74 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74  the Vdbe)..*/.st
19021 61 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41  atic int growOpA
19022 72 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20  rray(Vdbe *p){. 
19023 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20   VdbeOp *pNew;. 
19024 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e   int nNew = (p->
19025 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f  nOpAlloc ? p->nO
19026 70 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29  pAlloc*2 : (int)
19027 28 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29  (1024/sizeof(Op)
19028 29 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  ));.  pNew = sql
19029 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d  ite3DbRealloc(p-
1902a 3e 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65  >db, p->aOp, nNe
1902b 77 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20  w*sizeof(Op));. 
1902c 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
1902d 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73   p->nOpAlloc = s
1902e 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
1902f 7a 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f  ze(p->db, pNew)/
19030 73 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20  sizeof(Op);.    
19031 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20  p->aOp = pNew;. 
19032 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65   }.  return (pNe
19033 77 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  w ? SQLITE_OK : 
19034 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d  SQLITE_NOMEM);.}
19035 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
19036 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f  w instruction to
19037 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73   the list of ins
19038 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  tructions curren
19039 74 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45  t in the.** VDBE
1903a 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64  .  Return the ad
1903b 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77  dress of the new
1903c 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
1903d 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a  .** Parameters:.
1903e 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20  **.**    p      
1903f 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72           Pointer
19040 20 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a   to the VDBE.**.
19041 2a 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20  **    op        
19042 20 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65        The opcode
19043 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75   for this instru
19044 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70  ction.**.**    p
19045 31 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f  1, p2, p3      O
19046 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73  perands.**.** Us
19047 65 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62  e the sqlite3Vdb
19048 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20  eResolveLabel() 
19049 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20  function to fix 
1904a 61 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a  an address and.*
1904b 2a 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62  * the sqlite3Vdb
1904c 65 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63  eChangeP4() func
1904d 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74  tion to change t
1904e 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1904f 50 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a  P4.** operand..*
19050 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19051 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
19052 41 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20  AddOp3(Vdbe *p, 
19053 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20  int op, int p1, 
19054 69 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b  int p2, int p3){
19055 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65  .  int i;.  Vdbe
19056 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20  Op *pOp;..  i = 
19057 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74  p->nOp;.  assert
19058 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  ( p->magic==VDBE
19059 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
1905a 20 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26   assert( op>0 &&
1905b 20 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66   op<0xff );.  if
1905c 28 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69  ( p->nOpAlloc<=i
1905d 20 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77   ){.    if( grow
1905e 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20  OpArray(p) ){.  
1905f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
19060 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70    }.  }.  p->nOp
19061 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e  ++;.  pOp = &p->
19062 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f  aOp[i];.  pOp->o
19063 70 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a  pcode = (u8)op;.
19064 20 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20    pOp->p5 = 0;. 
19065 20 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20   pOp->p1 = p1;. 
19066 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20   pOp->p2 = p2;. 
19067 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20   pOp->p3 = p3;. 
19068 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a   pOp->p4.p = 0;.
19069 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
1906a 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d  P4_NOTUSED;.  p-
1906b 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 23 69  >expired = 0;.#i
1906c 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1906d 47 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e  G.  pOp->zCommen
1906e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c  t = 0;.  if( sql
1906f 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61  ite3VdbeAddopTra
19070 63 65 20 29 20 73 71 6c 69 74 65 33 56 64 62 65  ce ) sqlite3Vdbe
19071 50 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70  PrintOp(0, i, &p
19072 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69  ->aOp[i]);.#endi
19073 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  f.#ifdef VDBE_PR
19074 4f 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63  OFILE.  pOp->cyc
19075 6c 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e  les = 0;.  pOp->
19076 63 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  cnt = 0;.#endif.
19077 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51    return i;.}.SQ
19078 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19079 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1907a 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  p0(Vdbe *p, int 
1907b 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  op){.  return sq
1907c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1907d 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b  p, op, 0, 0, 0);
1907e 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
1907f 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19080 65 41 64 64 4f 70 31 28 56 64 62 65 20 2a 70 2c  eAddOp1(Vdbe *p,
19081 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 29   int op, int p1)
19082 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
19083 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20  e3VdbeAddOp3(p, 
19084 6f 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d  op, p1, 0, 0);.}
19085 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19086 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
19087 64 64 4f 70 32 28 56 64 62 65 20 2a 70 2c 20 69  ddOp2(Vdbe *p, i
19088 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69  nt op, int p1, i
19089 6e 74 20 70 32 29 7b 0a 20 20 72 65 74 75 72 6e  nt p2){.  return
1908a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1908b 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32  p3(p, op, p1, p2
1908c 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  , 0);.}.../*.** 
1908d 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68  Add an opcode th
1908e 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20  at includes the 
1908f 70 34 20 76 61 6c 75 65 20 61 73 20 61 20 70 6f  p4 value as a po
19090 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
19091 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19092 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a  ite3VdbeAddOp4(.
19093 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20    Vdbe *p,      
19094 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65        /* Add the
19095 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73 20   opcode to this 
19096 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20  VM */.  int op, 
19097 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
19098 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f  he new opcode */
19099 0a 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20 20  .  int p1,      
1909a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 31         /* The P1
1909b 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
1909c 74 20 70 32 2c 20 20 20 20 20 20 20 20 20 20 20  t p2,           
1909d 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65 72    /* The P2 oper
1909e 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c  and */.  int p3,
1909f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
190a0 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 2a  The P3 operand *
190a1 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
190a2 7a 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 20 50  zP4,    /* The P
190a3 34 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  4 operand */.  i
190a4 6e 74 20 70 34 74 79 70 65 20 20 20 20 20 20 20  nt p4type       
190a5 20 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64     /* P4 operand
190a6 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   type */.){.  in
190a7 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33  t addr = sqlite3
190a8 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70  VdbeAddOp3(p, op
190a9 2c 20 70 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20  , p1, p2, p3);. 
190aa 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
190ab 67 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 50  geP4(p, addr, zP
190ac 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 72 65  4, p4type);.  re
190ad 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a  turn addr;.}../*
190ae 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
190af 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20   symbolic label 
190b0 66 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69  for an instructi
190b1 6f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20  on that has yet 
190b2 74 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20  to be.** coded. 
190b3 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61   The symbolic la
190b4 62 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75  bel is really ju
190b5 73 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75  st a negative nu
190b6 6d 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61  mber.  The.** la
190b7 62 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20  bel can be used 
190b8 61 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20  as the P2 value 
190b9 6f 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e  of an operation.
190ba 20 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a    Later, when.**
190bb 20 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65   the label is re
190bc 73 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63  solved to a spec
190bd 69 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68  ific address, th
190be 65 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e  e VDBE will scan
190bf 0a 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20  .** through its 
190c0 6f 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61  operation list a
190c1 6e 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61  nd change all va
190c2 6c 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68  lues of P2 which
190c3 20 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61   match.** the la
190c4 62 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73  bel into the res
190c5 6f 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a  olved address..*
190c6 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e  *.** The VDBE kn
190c7 6f 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61  ows that a P2 va
190c8 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62  lue is a label b
190c9 65 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72  ecause labels ar
190ca 65 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61  e.** always nega
190cb 74 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75  tive and P2 valu
190cc 65 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74  es are suppose t
190cd 6f 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  o be non-negativ
190ce 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e  e..** Hence, a n
190cf 65 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65  egative P2 value
190d0 20 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74   is a label that
190d1 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72   has yet to be r
190d2 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a  esolved..**.** Z
190d3 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20  ero is returned 
190d4 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  if a malloc() fa
190d5 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ils..*/.SQLITE_P
190d6 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
190d7 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
190d8 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
190d9 69 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62  i;.  i = p->nLab
190da 65 6c 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20  el++;.  assert( 
190db 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
190dc 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
190dd 66 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41  f( i>=p->nLabelA
190de 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20  lloc ){.    int 
190df 6e 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c  n = p->nLabelAll
190e0 6f 63 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d  oc*2 + 5;.    p-
190e1 3e 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65  >aLabel = sqlite
190e2 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
190e3 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65  (p->db, p->aLabe
190e4 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
190e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
190e6 20 20 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65            n*size
190e7 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29  of(p->aLabel[0])
190e8 29 3b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c  );.    p->nLabel
190e9 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  Alloc = sqlite3D
190ea 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64  bMallocSize(p->d
190eb 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69  b, p->aLabel)/si
190ec 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30  zeof(p->aLabel[0
190ed 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ]);.  }.  if( p-
190ee 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70  >aLabel ){.    p
190ef 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31  ->aLabel[i] = -1
190f0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d  ;.  }.  return -
190f1 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  1-i;.}../*.** Re
190f2 73 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20  solve label "x" 
190f3 74 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73  to be the addres
190f4 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e  s of the next in
190f5 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20  struction to.** 
190f6 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68  be inserted.  Th
190f7 65 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20  e parameter "x" 
190f8 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f  must have been o
190f9 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
190fa 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  a prior call to 
190fb 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
190fc 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  abel()..*/.SQLIT
190fd 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
190fe 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
190ff 65 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20  eLabel(Vdbe *p, 
19100 69 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20  int x){.  int j 
19101 3d 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74  = -1-x;.  assert
19102 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  ( p->magic==VDBE
19103 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
19104 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26   assert( j>=0 &&
19105 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a   j<p->nLabel );.
19106 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20    if( p->aLabel 
19107 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c  ){.    p->aLabel
19108 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20  [j] = p->nOp;.  
19109 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20  }.}../*.** Loop 
1910a 74 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67  through the prog
1910b 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  ram looking for 
1910c 50 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61  P2 values that a
1910d 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f  re negative.** o
1910e 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69  n jump instructi
1910f 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20  ons.  Each such 
19110 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c  value is a label
19111 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a  .  Resolve the.*
19112 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69  * label by setti
19113 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20  ng the P2 value 
19114 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e  to its correct n
19115 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a  on-zero value..*
19116 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
19117 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65  e is called once
19118 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64   after all opcod
19119 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73  es have been ins
1911a 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72  erted..**.** Var
1911b 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41  iable *pMaxFuncA
1911c 72 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68  rgs is set to th
1911d 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20  e maximum value 
1911e 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65  of any P2 argume
1911f 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f  nt .** to an OP_
19120 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67  Function, OP_Agg
19121 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74  Step or OP_VFilt
19122 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20  er opcode. This 
19123 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73  is used by .** s
19124 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65  qlite3VdbeMakeRe
19125 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68  ady() to size th
19126 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61  e Vdbe.apArg[] a
19127 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rray..**.** This
19128 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f   routine also do
19129 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
1912a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a 20 20   optimization:  
1912b 49 74 20 73 63 61 6e 73 20 66 6f 72 0a 2a 2a 20  It scans for.** 
1912c 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61  instructions tha
1912d 74 20 6d 69 67 68 74 20 63 61 75 73 65 20 61 20  t might cause a 
1912e 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61  statement rollba
1912f 63 6b 2e 20 20 53 75 63 68 20 69 6e 73 74 72 75  ck.  Such instru
19130 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 3a 0a 2a  ctions.** are:.*
19131 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c  *.**   *  OP_Hal
19132 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45  t with P1=SQLITE
19133 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20  _CONSTRAINT and 
19134 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20  P2=OE_Abort..** 
19135 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a    *  OP_Destroy.
19136 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61  **   *  OP_VUpda
19137 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52  te.**   *  OP_VR
19138 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  ename.**.** If n
19139 6f 20 73 75 63 68 20 69 6e 73 74 72 75 63 74 69  o such instructi
1913a 6f 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65  on is found, the
1913b 6e 20 65 76 65 72 79 20 53 74 61 74 65 6d 65 6e  n every Statemen
1913c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 2a  t instruction .*
1913d 2a 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20  * is changed to 
1913e 61 20 4e 6f 6f 70 2e 20 20 49 6e 20 74 68 69 73  a Noop.  In this
1913f 20 77 61 79 2c 20 77 65 20 61 76 6f 69 64 20 63   way, we avoid c
19140 72 65 61 74 69 6e 67 20 74 68 65 20 73 74 61 74  reating the stat
19141 65 6d 65 6e 74 20 0a 2a 2a 20 6a 6f 75 72 6e 61  ement .** journa
19142 6c 20 66 69 6c 65 20 75 6e 6e 65 63 65 73 73 61  l file unnecessa
19143 72 69 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rily..*/.static 
19144 76 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61  void resolveP2Va
19145 6c 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e  lues(Vdbe *p, in
19146 74 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29  t *pMaxFuncArgs)
19147 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
19148 20 6e 4d 61 78 41 72 67 73 20 3d 20 30 3b 0a 20   nMaxArgs = 0;. 
19149 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20   Op *pOp;.  int 
1914a 2a 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61  *aLabel = p->aLa
1914b 62 65 6c 3b 0a 20 20 69 6e 74 20 64 6f 65 73 53  bel;.  int doesS
1914c 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b  tatementRollback
1914d 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73 53   = 0;.  int hasS
1914e 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20  tatementBegin = 
1914f 30 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79  0;.  p->readOnly
19150 20 3d 20 31 3b 0a 20 20 70 2d 3e 75 73 65 73 53   = 1;.  p->usesS
19151 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a  tmtJournal = 0;.
19152 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70    for(pOp=p->aOp
19153 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e  , i=p->nOp-1; i>
19154 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b  =0; i--, pOp++){
19155 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d  .    u8 opcode =
19156 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20   pOp->opcode;.. 
19157 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f     if( opcode==O
19158 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70  P_Function || op
19159 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70  code==OP_AggStep
1915a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   ){.      if( pO
1915b 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29  p->p5>nMaxArgs )
1915c 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d   nMaxArgs = pOp-
1915d 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  >p5;.#ifndef SQL
1915e 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1915f 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20  TABLE.    }else 
19160 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56  if( opcode==OP_V
19161 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Update ){.      
19162 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78  if( pOp->p2>nMax
19163 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20  Args ) nMaxArgs 
19164 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69  = pOp->p2;.#endi
19165 66 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  f.    }.    if( 
19166 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20  opcode==OP_Halt 
19167 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  ){.      if( pOp
19168 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e  ->p1==SQLITE_CON
19169 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e  STRAINT && pOp->
1916a 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a  p2==OE_Abort ){.
1916b 20 20 20 20 20 20 20 20 64 6f 65 73 53 74 61 74          doesStat
1916c 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20  ementRollback = 
1916d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
1916e 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d  else if( opcode=
1916f 3d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29 7b  =OP_Statement ){
19170 0a 20 20 20 20 20 20 68 61 73 53 74 61 74 65 6d  .      hasStatem
19171 65 6e 74 42 65 67 69 6e 20 3d 20 31 3b 0a 20 20  entBegin = 1;.  
19172 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a      p->usesStmtJ
19173 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20 20 20  ournal = 1;.    
19174 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65  }else if( opcode
19175 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 29 7b 0a  ==OP_Destroy ){.
19176 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d        doesStatem
19177 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b  entRollback = 1;
19178 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  .    }else if( o
19179 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61  pcode==OP_Transa
1917a 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32  ction && pOp->p2
1917b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  !=0 ){.      p->
1917c 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69  readOnly = 0;.#i
1917d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1917e 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
1917f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63     }else if( opc
19180 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20  ode==OP_VUpdate 
19181 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52  || opcode==OP_VR
19182 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 64  ename ){.      d
19183 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c  oesStatementRoll
19184 62 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65  back = 1;.    }e
19185 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d  lse if( opcode==
19186 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a 20 20  OP_VFilter ){.  
19187 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20      int n;.     
19188 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 20   assert( p->nOp 
19189 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 20 20  - i >= 3 );.    
1918a 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31    assert( pOp[-1
1918b 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 74  ].opcode==OP_Int
1918c 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20 6e 20  eger );.      n 
1918d 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a 20 20  = pOp[-1].p1;.  
1918e 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 41 72      if( n>nMaxAr
1918f 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20  gs ) nMaxArgs = 
19190 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  n;.#endif.    }.
19191 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
19192 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f  VdbeOpcodeHasPro
19193 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 4f 50  perty(opcode, OP
19194 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 4f 70  FLG_JUMP) && pOp
19195 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20  ->p2<0 ){.      
19196 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d 3e  assert( -1-pOp->
19197 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a  p2<p->nLabel );.
19198 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20        pOp->p2 = 
19199 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e 70  aLabel[-1-pOp->p
1919a 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  2];.    }.  }.  
1919b 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d  sqlite3DbFree(p-
1919c 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b  >db, p->aLabel);
1919d 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 30  .  p->aLabel = 0
1919e 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41 72  ;..  *pMaxFuncAr
1919f 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a 0a  gs = nMaxArgs;..
191a0 20 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65 72    /* If we never
191a1 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61 74   rollback a stat
191a2 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
191a3 6e 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65 6e  n, then statemen
191a4 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  t.  ** transacti
191a5 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65 64  ons are not need
191a6 65 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20 65  ed.  So change e
191a7 76 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65 6e  very OP_Statemen
191a8 74 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69 6e  t.  ** opcode in
191a9 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20  to an OP_Noop.  
191aa 54 68 69 73 20 61 76 6f 69 64 20 61 20 63 61 6c  This avoid a cal
191ab 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70  l to sqlite3OsOp
191ac 65 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20 20  enExclusive().  
191ad 2a 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  ** which can be 
191ae 65 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f 6d  expensive on som
191af 65 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20 2a  e platforms..  *
191b0 2f 0a 20 20 69 66 28 20 68 61 73 53 74 61 74 65  /.  if( hasState
191b1 6d 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64 6f  mentBegin && !do
191b2 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62  esStatementRollb
191b3 61 63 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 75 73  ack ){.    p->us
191b4 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20  esStmtJournal = 
191b5 30 3b 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 70  0;.    for(pOp=p
191b6 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d  ->aOp, i=p->nOp-
191b7 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f  1; i>=0; i--, pO
191b8 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  p++){.      if( 
191b9 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
191ba 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20  Statement ){.   
191bb 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65       pOp->opcode
191bc 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20   = OP_Noop;.    
191bd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
191be 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
191bf 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
191c0 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
191c1 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  n to be inserted
191c2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
191c3 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
191c4 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56  dbeCurrentAddr(V
191c5 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  dbe *p){.  asser
191c6 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
191c7 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
191c8 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f 70 3b    return p->nOp;
191c9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
191ca 77 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70  whole list of op
191cb 65 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20  erations to the 
191cc 6f 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e  operation stack.
191cd 20 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20    Return the.** 
191ce 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66  address of the f
191cf 69 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61  irst operation a
191d0 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dded..*/.SQLITE_
191d1 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
191d2 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
191d3 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f  (Vdbe *p, int nO
191d4 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f  p, VdbeOpList co
191d5 6e 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74  nst *aOp){.  int
191d6 20 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28   addr;.  assert(
191d7 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
191d8 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
191d9 69 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70  if( p->nOp + nOp
191da 20 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26   > p->nOpAlloc &
191db 26 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29  & growOpArray(p)
191dc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
191dd 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 70  ;.  }.  addr = p
191de 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70  ->nOp;.  if( nOp
191df 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  >0 ){.    int i;
191e0 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20  .    VdbeOpList 
191e1 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70  const *pIn = aOp
191e2 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
191e3 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b  <nOp; i++, pIn++
191e4 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20  ){.      int p2 
191e5 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20  = pIn->p2;.     
191e6 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20   VdbeOp *pOut = 
191e7 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b  &p->aOp[i+addr];
191e8 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63  .      pOut->opc
191e9 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64  ode = pIn->opcod
191ea 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  e;.      pOut->p
191eb 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20  1 = pIn->p1;.   
191ec 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73     if( p2<0 && s
191ed 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65  qlite3VdbeOpcode
191ee 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74  HasProperty(pOut
191ef 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f  ->opcode, OPFLG_
191f0 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20  JUMP) ){.       
191f1 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72   pOut->p2 = addr
191f2 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20   + ADDR(p2);.   
191f3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
191f4 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b    pOut->p2 = p2;
191f5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
191f6 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70  Out->p3 = pIn->p
191f7 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  3;.      pOut->p
191f8 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53  4type = P4_NOTUS
191f9 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  ED;.      pOut->
191fa 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20  p4.p = 0;.      
191fb 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69  pOut->p5 = 0;.#i
191fc 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
191fd 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43  G.      pOut->zC
191fe 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20  omment = 0;.    
191ff 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
19200 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20  eAddopTrace ){. 
19201 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
19202 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61  bePrintOp(0, i+a
19203 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61  ddr, &p->aOp[i+a
19204 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23  ddr]);.      }.#
19205 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20  endif.    }.    
19206 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20  p->nOp += nOp;. 
19207 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72   }.  return addr
19208 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
19209 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
1920a 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f  he P1 operand fo
1920b 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73  r a specific ins
1920c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  truction..** Thi
1920d 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
1920e 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65  ful when a large
1920f 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64   program is load
19210 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61  ed from a.** sta
19211 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20  tic array using 
19212 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19213 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74  List but we want
19214 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65   to make a.** fe
19215 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20  w minor changes 
19216 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a  to the program..
19217 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19218 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
19219 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20  beChangeP1(Vdbe 
1921a 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e  *p, int addr, in
1921b 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74  t val){.  assert
1921c 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67  ( p==0 || p->mag
1921d 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
1921e 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26  NIT );.  if( p &
1921f 26 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e  & addr>=0 && p->
19220 6e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61  nOp>addr && p->a
19221 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70  Op ){.    p->aOp
19222 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b  [addr].p1 = val;
19223 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  .  }.}../*.** Ch
19224 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
19225 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64  f the P2 operand
19226 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20   for a specific 
19227 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20  instruction..** 
19228 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
19229 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69  useful for setti
1922a 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e  ng a jump destin
1922b 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
1922c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1922d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1922e 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  2(Vdbe *p, int a
1922f 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20  ddr, int val){. 
19230 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c   assert( p==0 ||
19231 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
19232 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
19233 69 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30  if( p && addr>=0
19234 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20   && p->nOp>addr 
19235 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20  && p->aOp ){.   
19236 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32   p->aOp[addr].p2
19237 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f   = val;.  }.}../
19238 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
19239 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20  value of the P3 
1923a 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70  operand for a sp
1923b 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69  ecific instructi
1923c 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
1923d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1923e 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56  e3VdbeChangeP3(V
1923f 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72  dbe *p, int addr
19240 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73  , int val){.  as
19241 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d  sert( p==0 || p-
19242 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
19243 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
19244 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20 26 26   p && addr>=0 &&
19245 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 26 20   p->nOp>addr && 
19246 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 70 2d  p->aOp ){.    p-
19247 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20  >aOp[addr].p3 = 
19248 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  val;.  }.}../*.*
19249 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
1924a 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65  ue of the P5 ope
1924b 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73  rand for the mos
1924c 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64  t recently.** ad
1924d 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a  ded operation..*
1924e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1924f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
19250 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a  eChangeP5(Vdbe *
19251 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73  p, u8 val){.  as
19252 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d  sert( p==0 || p-
19253 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
19254 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
19255 20 70 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a   p && p->aOp ){.
19256 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
19257 4f 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61  Op>0 );.    p->a
19258 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20  Op[p->nOp-1].p5 
19259 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  = val;.  }.}../*
1925a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50  .** Change the P
1925b 32 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73  2 operand of ins
1925c 74 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f  truction addr so
1925d 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
1925e 74 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73  to.** the addres
1925f 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e  s of the next in
19260 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20  struction to be 
19261 63 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  coded..*/.SQLITE
19262 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19263 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
19264 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  e(Vdbe *p, int a
19265 64 64 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 56  ddr){.  sqlite3V
19266 64 62 65 43 68 61 6e 67 65 50 32 28 70 2c 20 61  dbeChangeP2(p, a
19267 64 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a  ddr, p->nOp);.}.
19268 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69  ../*.** If the i
19269 6e 70 75 74 20 46 75 6e 63 44 65 66 20 73 74 72  nput FuncDef str
1926a 75 63 74 75 72 65 20 69 73 20 65 70 68 65 6d 65  ucture is epheme
1926b 72 61 6c 2c 20 74 68 65 6e 20 66 72 65 65 20 69  ral, then free i
1926c 74 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 46 75  t.  If.** the Fu
1926d 6e 63 44 65 66 20 69 73 20 6e 6f 74 20 65 70 68  ncDef is not eph
1926e 65 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e  ermal, then do n
1926f 6f 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  othing..*/.stati
19270 63 20 76 6f 69 64 20 66 72 65 65 45 70 68 65 6d  c void freeEphem
19271 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c  eralFunction(sql
19272 69 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44 65  ite3 *db, FuncDe
19273 66 20 2a 70 44 65 66 29 7b 0a 20 20 69 66 28 20  f *pDef){.  if( 
19274 70 44 65 66 20 26 26 20 28 70 44 65 66 2d 3e 66  pDef && (pDef->f
19275 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55  lags & SQLITE_FU
19276 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a  NC_EPHEM)!=0 ){.
19277 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
19278 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d  e(db, pDef);.  }
19279 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
1927a 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e   a P4 value if n
1927b 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61  ecessary..*/.sta
1927c 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28  tic void freeP4(
1927d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
1927e 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70   p4type, void *p
1927f 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a  4){.  if( p4 ){.
19280 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79      switch( p4ty
19281 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  pe ){.      case
19282 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20   P4_REAL:.      
19283 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20  case P4_INT64:. 
19284 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52       case P4_MPR
19285 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65  INTF:.      case
19286 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20   P4_DYNAMIC:.   
19287 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e     case P4_KEYIN
19288 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50  FO:.      case P
19289 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20  4_INTARRAY:.    
1928a 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46    case P4_KEYINF
1928b 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20  O_HANDOFF: {.   
1928c 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1928d 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20  ee(db, p4);.    
1928e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1928f 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34   }.      case P4
19290 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20  _VDBEFUNC: {.   
19291 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70       VdbeFunc *p
19292 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65  VdbeFunc = (Vdbe
19293 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20  Func *)p4;.     
19294 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c     freeEphemeral
19295 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64  Function(db, pVd
19296 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a  beFunc->pFunc);.
19297 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
19298 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61  dbeDeleteAuxData
19299 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a  (pVdbeFunc, 0);.
1929a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1929b 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46  bFree(db, pVdbeF
1929c 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72  unc);.        br
1929d 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1929e 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44     case P4_FUNCD
1929f 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72  EF: {.        fr
192a0 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74  eeEphemeralFunct
192a1 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66  ion(db, (FuncDef
192a2 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62  *)p4);.        b
192a3 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
192a4 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a      case P4_MEM:
192a5 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
192a6 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c  e3ValueFree((sql
192a7 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b  ite3_value*)p4);
192a8 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
192a9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
192aa 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  }.}.../*.** Chan
192ab 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61  ge N opcodes sta
192ac 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f  rting at addr to
192ad 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49   No-ops..*/.SQLI
192ae 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
192af 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
192b0 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c  eToNoop(Vdbe *p,
192b1 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e   int addr, int N
192b2 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  ){.  if( p && p-
192b3 3e 61 4f 70 20 29 7b 0a 20 20 20 20 56 64 62 65  >aOp ){.    Vdbe
192b4 4f 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f  Op *pOp = &p->aO
192b5 70 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c  p[addr];.    sql
192b6 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
192b7 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d  ;.    while( N--
192b8 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34   ){.      freeP4
192b9 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65  (db, pOp->p4type
192ba 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20  , pOp->p4.p);.  
192bb 20 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20      memset(pOp, 
192bc 30 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d  0, sizeof(pOp[0]
192bd 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f  ));.      pOp->o
192be 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b  pcode = OP_Noop;
192bf 0a 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20  .      pOp++;.  
192c0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
192c1 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   Change the valu
192c2 65 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65 72  e of the P4 oper
192c3 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66  and for a specif
192c4 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ic instruction..
192c5 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
192c6 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61  is useful when a
192c7 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69   large program i
192c8 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a  s loaded from a.
192c9 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20  ** static array 
192ca 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62  using sqlite3Vdb
192cb 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77  eAddOpList but w
192cc 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61  e want to make a
192cd 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68  .** few minor ch
192ce 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f  anges to the pro
192cf 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  gram..**.** If n
192d0 3e 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34 20  >=0 then the P4 
192d1 6f 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61 6d  operand is dynam
192d2 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74  ic, meaning that
192d3 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68   a copy of.** th
192d4 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64 65  e string is made
192d5 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74   into memory obt
192d6 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
192d7 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20  e3_malloc()..** 
192d8 41 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20  A value of n==0 
192d9 6d 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65 73  means copy bytes
192da 20 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61 6e   of zP4 up to an
192db 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a  d including the.
192dc 2a 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62 79  ** first null by
192dd 74 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65 6e  te.  If n>0 then
192de 20 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73 20   copy n+1 bytes 
192df 6f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66  of zP4..**.** If
192e0 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69   n==P4_KEYINFO i
192e1 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50 34  t means that zP4
192e2 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
192e3 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
192e4 74 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20  ture..** A copy 
192e5 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 4b  is made of the K
192e6 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
192e7 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74   into memory obt
192e8 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71  ained from.** sq
192e9 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f  lite3_malloc, to
192ea 20 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 74   be freed when t
192eb 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c  he Vdbe is final
192ec 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b  ized..** n==P4_K
192ed 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69  EYINFO_HANDOFF i
192ee 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 7a 50  ndicates that zP
192ef 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65  4 points to a Ke
192f0 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a  yInfo structure.
192f1 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d  ** stored in mem
192f2 6f 72 79 20 74 68 61 74 20 74 68 65 20 63 61 6c  ory that the cal
192f3 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64  ler has obtained
192f4 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
192f5 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61  lloc. The .** ca
192f6 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20  ller should not 
192f7 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 74  free the allocat
192f8 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 20  ion, it will be 
192f9 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56  freed when the V
192fa 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69  dbe is.** finali
192fb 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65  zed..** .** Othe
192fc 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50  r values of n (P
192fd 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c  4_STATIC, P4_COL
192fe 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63  LSEQ etc.) indic
192ff 61 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f 69  ate that zP4 poi
19300 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69  nts.** to a stri
19301 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 20  ng or structure 
19302 74 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 65  that is guarante
19303 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 20  ed to exist for 
19304 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a  the lifetime of.
19305 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e 20  ** the Vdbe. In 
19306 74 68 65 73 65 20 63 61 73 65 73 20 77 65 20 63  these cases we c
19307 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65  an just copy the
19308 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
19309 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 63  If addr<0 then c
1930a 68 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65 20  hange P4 on the 
1930b 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e  most recently in
1930c 73 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 69  serted instructi
1930d 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
1930e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1930f 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56  e3VdbeChangeP4(V
19310 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72  dbe *p, int addr
19311 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50  , const char *zP
19312 34 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20  4, int n){.  Op 
19313 2a 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33 20  *pOp;.  sqlite3 
19314 2a 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70  *db;.  assert( p
19315 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d  !=0 );.  db = p-
19316 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70  >db;.  assert( p
19317 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
19318 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66  GIC_INIT );.  if
19319 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64  ( p->aOp==0 || d
1931a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1931b 29 7b 0a 20 20 20 20 69 66 20 28 6e 20 21 3d 20  ){.    if (n != 
1931c 50 34 5f 4b 45 59 49 4e 46 4f 29 20 7b 0a 20 20  P4_KEYINFO) {.  
1931d 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 6e      freeP4(db, n
1931e 2c 20 28 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a  , (void*)*(char*
1931f 2a 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20  *)&zP4);.    }. 
19320 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
19321 20 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d   assert( addr<p-
19322 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64  >nOp );.  if( ad
19323 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72  dr<0 ){.    addr
19324 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20   = p->nOp - 1;. 
19325 20 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 20     if( addr<0 ) 
19326 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 4f  return;.  }.  pO
19327 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72  p = &p->aOp[addr
19328 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 62 2c 20  ];.  freeP4(db, 
19329 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70  pOp->p4type, pOp
1932a 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 2d 3e  ->p4.p);.  pOp->
1932b 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  p4.p = 0;.  if( 
1932c 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20  n==P4_INT32 ){. 
1932d 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 69 73     /* Note: this
1932e 20 63 61 73 74 20 69 73 20 73 61 66 65 2c 20 62   cast is safe, b
1932f 65 63 61 75 73 65 20 74 68 65 20 6f 72 69 67 69  ecause the origi
19330 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 61 73  n data point was
19331 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a 20 74   an int.    ** t
19332 68 61 74 20 77 61 73 20 63 61 73 74 20 74 6f 20  hat was cast to 
19333 61 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  a (const char *)
19334 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e 70 34  . */.    pOp->p4
19335 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f  .i = SQLITE_PTR_
19336 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20 20 20  TO_INT(zP4);.   
19337 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
19338 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 6c 73 65  4_INT32;.  }else
19339 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20   if( zP4==0 ){. 
1933a 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30     pOp->p4.p = 0
1933b 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  ;.    pOp->p4typ
1933c 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a  e = P4_NOTUSED;.
1933d 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50    }else if( n==P
1933e 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20  4_KEYINFO ){.   
1933f 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
19340 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 65  fo;.    int nFie
19341 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20 20  ld, nByte;..    
19342 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49 6e  nField = ((KeyIn
19343 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c 64  fo*)zP4)->nField
19344 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69  ;.    nByte = si
19345 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 20  zeof(*pKeyInfo) 
19346 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69 7a  + (nField-1)*siz
19347 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  eof(pKeyInfo->aC
19348 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c 64  oll[0]) + nField
19349 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d  ;.    pKeyInfo =
1934a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20   sqlite3Malloc( 
1934b 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f 70  nByte );.    pOp
1934c 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d 20  ->p4.pKeyInfo = 
1934d 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 66  pKeyInfo;.    if
1934e 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20  ( pKeyInfo ){.  
1934f 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64      u8 *aSortOrd
19350 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  er;.      memcpy
19351 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c 20  (pKeyInfo, zP4, 
19352 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 53  nByte);.      aS
19353 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49  ortOrder = pKeyI
19354 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 3b  nfo->aSortOrder;
19355 0a 20 20 20 20 20 20 69 66 28 20 61 53 6f 72 74  .      if( aSort
19356 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20 20  Order ){.       
19357 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
19358 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e 65  Order = (unsigne
19359 64 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e 66  d char*)&pKeyInf
1935a 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64 5d  o->aColl[nField]
1935b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  ;.        memcpy
1935c 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74  (pKeyInfo->aSort
1935d 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64 65  Order, aSortOrde
1935e 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20 20  r, nField);.    
1935f 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70    }.      pOp->p
19360 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e  4type = P4_KEYIN
19361 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  FO;.    }else{. 
19362 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c       p->db->mall
19363 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
19364 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
19365 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20  = P4_NOTUSED;.  
19366 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
19367 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  n==P4_KEYINFO_HA
19368 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f 70  NDOFF ){.    pOp
19369 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29  ->p4.p = (void*)
1936a 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  zP4;.    pOp->p4
1936b 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 46  type = P4_KEYINF
1936c 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  O;.  }else if( n
1936d 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70  <0 ){.    pOp->p
1936e 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34  4.p = (void*)zP4
1936f 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  ;.    pOp->p4typ
19370 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72  e = (signed char
19371 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  )n;.  }else{.   
19372 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20   if( n==0 ) n = 
19373 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
19374 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  zP4);.    pOp->p
19375 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53  4.z = sqlite3DbS
19376 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a 50  trNDup(p->db, zP
19377 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  4, n);.    pOp->
19378 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41  p4type = P4_DYNA
19379 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  MIC;.  }.}..#ifn
1937a 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a  def NDEBUG./*.**
1937b 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f 6d 6d   Change the comm
1937c 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 20 6d  ent on the the m
1937d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64  ost recently cod
1937e 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  ed instruction. 
1937f 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 61 20   Or.** insert a 
19380 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 74 68  No-op and add th
19381 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 68 61  e comment to tha
19382 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f  t new instructio
19383 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 6b 65  n.  This.** make
19384 73 20 74 68 65 20 63 6f 64 65 20 65 61 73 69 65  s the code easie
19385 72 20 74 6f 20 72 65 61 64 20 64 75 72 69 6e 67  r to read during
19386 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e 6f 6e   debugging.  Non
19387 65 20 6f 66 20 74 68 69 73 20 68 61 70 70 65 6e  e of this happen
19388 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 75 63  s.** in a produc
19389 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f 0a 53  tion build..*/.S
1938a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1938b 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f  id sqlite3VdbeCo
1938c 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63  mment(Vdbe *p, c
1938d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
1938e 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
1938f 69 73 74 20 61 70 3b 0a 20 20 61 73 73 65 72 74  ist ap;.  assert
19390 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d  ( p->nOp>0 || p-
19391 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73  >aOp==0 );.  ass
19392 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c  ert( p->aOp==0 |
19393 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d  | p->aOp[p->nOp-
19394 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c  1].zComment==0 |
19395 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  | p->db->mallocF
19396 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70  ailed );.  if( p
19397 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61  ->nOp ){.    cha
19398 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70  r **pz = &p->aOp
19399 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d  [p->nOp-1].zComm
1939a 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72  ent;.    va_star
1939b 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
1939c 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1939d 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20  e(p->db, *pz);. 
1939e 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33     *pz = sqlite3
1939f 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20  VMPrintf(p->db, 
193a0 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
193a1 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
193a2 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  }.}.SQLITE_PRIVA
193a3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
193a4 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56  dbeNoopComment(V
193a5 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  dbe *p, const ch
193a6 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
193a7 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
193a8 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
193a9 64 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29  dOp0(p, OP_Noop)
193aa 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
193ab 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d  Op>0 || p->aOp==
193ac 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
193ad 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61  ->aOp==0 || p->a
193ae 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f  Op[p->nOp-1].zCo
193af 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64  mment==0 || p->d
193b0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
193b1 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20  );.  if( p->nOp 
193b2 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a  ){.    char **pz
193b3 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f   = &p->aOp[p->nO
193b4 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20  p-1].zComment;. 
193b5 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20     va_start(ap, 
193b6 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71  zFormat);.    sq
193b7 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64  lite3DbFree(p->d
193b8 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a  b, *pz);.    *pz
193b9 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
193ba 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61  tf(p->db, zForma
193bb 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65  t, ap);.    va_e
193bc 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65  nd(ap);.  }.}.#e
193bd 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20  ndif  /* NDEBUG 
193be 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
193bf 20 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20   the opcode for 
193c0 61 20 67 69 76 65 6e 20 61 64 64 72 65 73 73 2e  a given address.
193c1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
193c2 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74  TE VdbeOp *sqlit
193c3 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65  e3VdbeGetOp(Vdbe
193c4 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a   *p, int addr){.
193c5 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
193c6 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
193c7 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28  NIT );.  assert(
193c8 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64   (addr>=0 && add
193c9 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e  r<p->nOp) || p->
193ca 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
193cb 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 61   );.  return ((a
193cc 64 64 72 3e 3d 30 20 26 26 20 61 64 64 72 3c 70  ddr>=0 && addr<p
193cd 2d 3e 6e 4f 70 29 3f 28 26 70 2d 3e 61 4f 70 5b  ->nOp)?(&p->aOp[
193ce 61 64 64 72 5d 29 3a 30 29 3b 0a 7d 0a 0a 23 69  addr]):0);.}..#i
193cf 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
193d0 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20  E_OMIT_EXPLAIN) 
193d1 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  || !defined(NDEB
193d2 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65  UG) \.     || de
193d3 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49  fined(VDBE_PROFI
193d4 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  LE) || defined(S
193d5 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a  QLITE_DEBUG)./*.
193d6 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72  ** Compute a str
193d7 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62  ing that describ
193d8 65 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65  es the P4 parame
193d9 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64  ter for an opcod
193da 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20  e..** Use zTemp 
193db 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72 65 64  for any required
193dc 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65   temporary buffe
193dd 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74  r space..*/.stat
193de 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79  ic char *display
193df 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72  P4(Op *pOp, char
193e0 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65   *zTemp, int nTe
193e1 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34  mp){.  char *zP4
193e2 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65   = zTemp;.  asse
193e3 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b  rt( nTemp>=20 );
193e4 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e  .  switch( pOp->
193e5 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61  p4type ){.    ca
193e6 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54  se P4_KEYINFO_ST
193e7 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50  ATIC:.    case P
193e8 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20  4_KEYINFO: {.   
193e9 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20     int i, j;.   
193ea 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
193eb 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Info = pOp->p4.p
193ec 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73  KeyInfo;.      s
193ed 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
193ee 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b  nTemp, zTemp, "k
193ef 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79  eyinfo(%d", pKey
193f0 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20  Info->nField);. 
193f1 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74 65 33       i = sqlite3
193f2 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 29 3b  Strlen30(zTemp);
193f3 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  .      for(j=0; 
193f4 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  j<pKeyInfo->nFie
193f5 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ld; j++){.      
193f6 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
193f7 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f   = pKeyInfo->aCo
193f8 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69  ll[j];.        i
193f9 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
193fa 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71        int n = sq
193fb 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43  lite3Strlen30(pC
193fc 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  oll->zName);.   
193fd 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e         if( i+n>n
193fe 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20  Temp-6 ){.      
193ff 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54        memcpy(&zT
19400 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29  emp[i],",...",4)
19401 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
19402 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
19403 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70  .          zTemp
19404 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20  [i++] = ',';.   
19405 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49         if( pKeyI
19406 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  nfo->aSortOrder 
19407 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f  && pKeyInfo->aSo
19408 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20  rtOrder[j] ){.  
19409 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b            zTemp[
1940a 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20  i++] = '-';.    
1940b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1940c 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b    memcpy(&zTemp[
1940d 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  i], pColl->zName
1940e 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  ,n+1);.         
1940f 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20   i += n;.       
19410 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e   }else if( i+4<n
19411 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20  Temp-6 ){.      
19412 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d      memcpy(&zTem
19413 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a  p[i],",nil",4);.
19414 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34            i += 4
19415 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
19416 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b    }.      zTemp[
19417 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20  i++] = ')';.    
19418 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a    zTemp[i] = 0;.
19419 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c        assert( i<
1941a 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62  nTemp );.      b
1941b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1941c 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a  case P4_COLLSEQ:
1941d 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71   {.      CollSeq
1941e 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70   *pColl = pOp->p
1941f 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73  4.pColl;.      s
19420 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
19421 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63  nTemp, zTemp, "c
19422 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20  ollseq(%.20s)", 
19423 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pColl->zName);. 
19424 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
19425 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55  }.    case P4_FU
19426 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46  NCDEF: {.      F
19427 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70  uncDef *pDef = p
19428 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20  Op->p4.pFunc;.  
19429 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1942a 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
1942b 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65  p, "%s(%d)", pDe
1942c 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e  f->zName, pDef->
1942d 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65  nArg);.      bre
1942e 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1942f 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20  se P4_INT64: {. 
19430 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
19431 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
19432 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70  mp, "%lld", *pOp
19433 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20  ->p4.pI64);.    
19434 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
19435 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32     case P4_INT32
19436 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
19437 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
19438 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70  , zTemp, "%d", p
19439 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20  Op->p4.i);.     
1943a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1943b 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20    case P4_REAL: 
1943c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1943d 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
1943e 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20  zTemp, "%.16g", 
1943f 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b  *pOp->p4.pReal);
19440 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
19441 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f    }.    case P4_
19442 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d  MEM: {.      Mem
19443 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34   *pMem = pOp->p4
19444 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73  .pMem;.      ass
19445 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  ert( (pMem->flag
19446 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
19447 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d   );.      if( pM
19448 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
19449 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  Str ){.        z
1944a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20 20  P4 = pMem->z;.  
1944b 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d      }else if( pM
1944c 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
1944d 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Int ){.        s
1944e 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1944f 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25  nTemp, zTemp, "%
19450 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29  lld", pMem->u.i)
19451 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
19452 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
19453 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
19454 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
19455 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
19456 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d 65 6d  p, "%.16g", pMem
19457 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ->r);.      }.  
19458 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
19459 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1945a 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1945b 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 54  E.    case P4_VT
1945c 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  AB: {.      sqli
1945d 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20  te3_vtab *pVtab 
1945e 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b  = pOp->p4.pVtab;
1945f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
19460 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
19461 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a 25  Temp, "vtab:%p:%
19462 70 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61 62  p", pVtab, pVtab
19463 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20  ->pModule);.    
19464 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
19465 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 50  endif.    case P
19466 34 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20  4_INTARRAY: {.  
19467 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
19468 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
19469 70 2c 20 22 69 6e 74 61 72 72 61 79 22 29 3b 0a  p, "intarray");.
1946a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1946b 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
1946c 7b 0a 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4f  {.      zP4 = pO
1946d 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69  p->p4.z;.      i
1946e 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20  f( zP4==0 ){.   
1946f 20 20 20 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70       zP4 = zTemp
19470 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b  ;.        zTemp[
19471 30 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  0] = 0;.      }.
19472 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65      }.  }.  asse
19473 72 74 28 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20  rt( zP4!=0 );.  
19474 72 65 74 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65  return zP4;.}.#e
19475 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c  ndif../*.** Decl
19476 61 72 65 20 74 6f 20 74 68 65 20 56 64 62 65 20  are to the Vdbe 
19477 74 68 61 74 20 74 68 65 20 42 54 72 65 65 20 6f  that the BTree o
19478 62 6a 65 63 74 20 61 74 20 64 62 2d 3e 61 44 62  bject at db->aDb
19479 5b 69 5d 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a  [i] is used..**.
1947a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1947b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1947c 62 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65  beUsesBtree(Vdbe
1947d 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69   *p, int i){.  i
1947e 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73 73 65 72  nt mask;.  asser
1947f 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  t( i>=0 && i<p->
19480 64 62 2d 3e 6e 44 62 20 26 26 20 69 3c 73 69 7a  db->nDb && i<siz
19481 65 6f 66 28 75 33 32 29 2a 38 20 29 3b 0a 20 20  eof(u32)*8 );.  
19482 61 73 73 65 72 74 28 20 69 3c 28 69 6e 74 29 73  assert( i<(int)s
19483 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65 65 4d 61  izeof(p->btreeMa
19484 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61 73 6b 20  sk)*8 );.  mask 
19485 3d 20 28 28 75 33 32 29 31 29 3c 3c 69 3b 0a 20  = ((u32)1)<<i;. 
19486 20 69 66 28 20 28 70 2d 3e 62 74 72 65 65 4d 61   if( (p->btreeMa
19487 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b  sk & mask)==0 ){
19488 0a 20 20 20 20 70 2d 3e 62 74 72 65 65 4d 61 73  .    p->btreeMas
19489 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 73  k |= mask;.    s
1948a 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
1948b 41 72 72 61 79 49 6e 73 65 72 74 28 26 70 2d 3e  ArrayInsert(&p->
1948c 61 4d 75 74 65 78 2c 20 70 2d 3e 64 62 2d 3e 61  aMutex, p->db->a
1948d 44 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 7d 0a  Db[i].pBt);.  }.
1948e 7d 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  }...#if defined(
1948f 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c  VDBE_PROFILE) ||
19490 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
19491 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50 72 69  DEBUG)./*.** Pri
19492 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f 70 63 6f  nt a single opco
19493 64 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  de.  This routin
19494 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65  e is used for de
19495 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f  bugging only..*/
19496 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19497 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
19498 50 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a 70 4f  PrintOp(FILE *pO
19499 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70 20 2a  ut, int pc, Op *
1949a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50  pOp){.  char *zP
1949b 34 3b 0a 20 20 63 68 61 72 20 7a 50 74 72 5b 35  4;.  char zPtr[5
1949c 30 5d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  0];.  static con
1949d 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
1949e 31 20 3d 20 22 25 34 64 20 25 2d 31 33 73 20 25  1 = "%4d %-13s %
1949f 34 64 20 25 34 64 20 25 34 64 20 25 2d 34 73 20  4d %4d %4d %-4s 
194a0 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20 69 66  %.2X %s\n";.  if
194a1 28 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f 75 74  ( pOut==0 ) pOut
194a2 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a 50 34   = stdout;.  zP4
194a3 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70   = displayP4(pOp
194a4 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66 28 7a  , zPtr, sizeof(z
194a5 50 74 72 29 29 3b 0a 20 20 66 70 72 69 6e 74 66  Ptr));.  fprintf
194a6 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74 31 2c  (pOut, zFormat1,
194a7 20 70 63 2c 20 0a 20 20 20 20 20 20 73 71 6c 69   pc, .      sqli
194a8 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f  te3OpcodeName(pO
194a9 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f 70 2d  p->opcode), pOp-
194aa 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f  >p1, pOp->p2, pO
194ab 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f 70 2d  p->p3, zP4, pOp-
194ac 3e 70 35 2c 0a 23 69 66 64 65 66 20 53 51 4c 49  >p5,.#ifdef SQLI
194ad 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70  TE_DEBUG.      p
194ae 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f 20 70  Op->zComment ? p
194af 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a 20 22  Op->zComment : "
194b0 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 22 22  ".#else.      ""
194b1 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20 66  .#endif.  );.  f
194b2 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d 0a 23  flush(pOut);.}.#
194b3 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c  endif../*.** Rel
194b4 65 61 73 65 20 61 6e 20 61 72 72 61 79 20 6f 66  ease an array of
194b5 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 73 0a   N Mem elements.
194b6 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
194b7 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 4d  eleaseMemArray(M
194b8 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a 20  em *p, int N){. 
194b9 20 69 66 28 20 70 20 26 26 20 4e 20 29 7b 0a 20   if( p && N ){. 
194ba 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a 20 20     Mem *pEnd;.  
194bb 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
194bc 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38 20 6d 61  p->db;.    u8 ma
194bd 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20 64 62  lloc_failed = db
194be 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 0a  ->mallocFailed;.
194bf 20 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70 5b      for(pEnd=&p[
194c0 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b 29  N]; p<pEnd; p++)
194c1 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
194c2 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 20 7c 7c  (&p[1])==pEnd ||
194c3 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64   p[0].db==p[1].d
194c4 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54  b );..      /* T
194c5 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 72 65 61  his block is rea
194c6 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 64 20 76  lly an inlined v
194c7 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  ersion of sqlite
194c8 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
194c9 29 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20  ).      ** that 
194ca 74 61 6b 65 73 20 61 64 76 61 6e 74 61 67 65 20  takes advantage 
194cb 6f 66 20 74 68 65 20 66 61 63 74 20 74 68 61 74  of the fact that
194cc 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
194cd 20 76 61 6c 75 65 20 69 73 20 0a 20 20 20 20 20   value is .     
194ce 20 2a 2a 20 62 65 69 6e 67 20 73 65 74 20 74 6f   ** being set to
194cf 20 4e 55 4c 4c 20 61 66 74 65 72 20 72 65 6c 65   NULL after rele
194d0 61 73 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d 69  asing any dynami
194d1 63 20 72 65 73 6f 75 72 63 65 73 2e 0a 20 20 20  c resources..   
194d2 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54     **.      ** T
194d3 68 65 20 6a 75 73 74 69 66 69 63 61 74 69 6f 6e  he justification
194d4 20 66 6f 72 20 64 75 70 6c 69 63 61 74 69 6e 67   for duplicating
194d5 20 63 6f 64 65 20 69 73 20 74 68 61 74 20 61 63   code is that ac
194d6 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20 20 20 20  cording to .    
194d7 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e 64 2c 20    ** callgrind, 
194d8 74 68 69 73 20 63 61 75 73 65 73 20 61 20 63 65  this causes a ce
194d9 72 74 61 69 6e 20 74 65 73 74 20 63 61 73 65 20  rtain test case 
194da 74 6f 20 68 69 74 20 74 68 65 20 43 50 55 20 34  to hit the CPU 4
194db 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20 70 65 72  .7 .      ** per
194dc 63 65 6e 74 20 6c 65 73 73 20 28 78 38 36 20 6c  cent less (x86 l
194dd 69 6e 75 78 2c 20 67 63 63 20 76 65 72 73 69 6f  inux, gcc versio
194de 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 20 74 68  n 4.1.2, -O6) th
194df 61 6e 20 69 66 20 0a 20 20 20 20 20 20 2a 2a 20  an if .      ** 
194e0 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c 65 61 73  sqlite3MemReleas
194e1 65 28 29 20 77 65 72 65 20 63 61 6c 6c 65 64 20  e() were called 
194e2 66 72 6f 6d 20 68 65 72 65 2e 20 57 69 74 68 20  from here. With 
194e3 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d 70 73 0a  -O2, this jumps.
194e4 20 20 20 20 20 20 2a 2a 20 74 6f 20 36 2e 36 20        ** to 6.6 
194e5 70 65 72 63 65 6e 74 2e 20 54 68 65 20 74 65 73  percent. The tes
194e6 74 20 63 61 73 65 20 69 73 20 69 6e 73 65 72 74  t case is insert
194e7 69 6e 67 20 31 30 30 30 20 72 6f 77 73 20 69 6e  ing 1000 rows in
194e8 74 6f 20 61 20 74 61 62 6c 65 20 0a 20 20 20 20  to a table .    
194e9 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20 69 6e 64    ** with no ind
194ea 65 78 65 73 20 75 73 69 6e 67 20 61 20 73 69 6e  exes using a sin
194eb 67 6c 65 20 70 72 65 70 61 72 65 64 20 49 4e 53  gle prepared INS
194ec 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 62  ERT statement, b
194ed 69 6e 64 28 29 20 0a 20 20 20 20 20 20 2a 2a 20  ind() .      ** 
194ee 61 6e 64 20 72 65 73 65 74 28 29 2e 20 49 6e 73  and reset(). Ins
194ef 65 72 74 73 20 61 72 65 20 67 72 6f 75 70 65 64  erts are grouped
194f0 20 69 6e 74 6f 20 61 20 74 72 61 6e 73 61 63 74   into a transact
194f1 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion..      */.  
194f2 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73      if( p->flags
194f3 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79  &(MEM_Agg|MEM_Dy
194f4 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  n) ){.        sq
194f5 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
194f6 61 73 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65  ase(p);.      }e
194f7 6c 73 65 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c  lse if( p->zMall
194f8 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  oc ){.        sq
194f9 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
194fa 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20  p->zMalloc);.   
194fb 20 20 20 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20       p->zMalloc 
194fc 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  = 0;.      }..  
194fd 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d      p->flags = M
194fe 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20  EM_Null;.    }. 
194ff 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
19500 6c 65 64 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69  led = malloc_fai
19501 6c 65 64 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64  led;.  }.}..#ifd
19502 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
19503 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
19504 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  NT.SQLITE_PRIVAT
19505 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19506 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28  eReleaseBuffers(
19507 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
19508 69 69 3b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20  ii;.  int nFree 
19509 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 73  = 0;.  assert( s
1950a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1950b 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
1950c 29 3b 0a 20 20 66 6f 72 28 69 69 3d 31 3b 20 69  );.  for(ii=1; i
1950d 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 69 2b 2b  i<=p->nMem; ii++
1950e 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d  ){.    Mem *pMem
1950f 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 69 69 5d 3b   = &p->aMem[ii];
19510 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66  .    if( pMem->f
19511 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
19512 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
19513 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d  e3RowSetClear(pM
19514 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a  em->u.pRowSet);.
19515 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4d      }.    if( pM
19516 65 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 66  em->z && pMem->f
19517 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 29 7b 0a  lags&MEM_Dyn ){.
19518 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
19519 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20  Mem->xDel );.   
1951a 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69     nFree += sqli
1951b 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
1951c 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e  pMem->db, pMem->
1951d 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  z);.      sqlite
1951e 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1951f 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pMem);.    }.  }
19520 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b  .  return nFree;
19521 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
19522 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  ef SQLITE_OMIT_E
19523 58 50 4c 41 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76  XPLAIN./*.** Giv
19524 65 20 61 20 6c 69 73 74 69 6e 67 20 6f 66 20 74  e a listing of t
19525 68 65 20 70 72 6f 67 72 61 6d 20 69 6e 20 74 68  he program in th
19526 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
19527 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74  e..**.** The int
19528 65 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61  erface is the sa
19529 6d 65 20 61 73 20 73 71 6c 69 74 65 33 56 64 62  me as sqlite3Vdb
1952a 65 45 78 65 63 28 29 2e 20 20 42 75 74 20 69 6e  eExec().  But in
1952b 73 74 65 61 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e  stead of.** runn
1952c 69 6e 67 20 74 68 65 20 63 6f 64 65 2c 20 69 74  ing the code, it
1952d 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 61 6c   invokes the cal
1952e 6c 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65  lback once for e
1952f 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ach instruction.
19530 0a 2a 2a 20 54 68 69 73 20 66 65 61 74 75 72 65  .** This feature
19531 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c   is used to impl
19532 65 6d 65 6e 74 20 22 45 58 50 4c 41 49 4e 22 2e  ement "EXPLAIN".
19533 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 2d 3e 65  .**.** When p->e
19534 78 70 6c 61 69 6e 3d 3d 31 2c 20 65 61 63 68 20  xplain==1, each 
19535 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6c  instruction is l
19536 69 73 74 65 64 2e 20 20 57 68 65 6e 0a 2a 2a 20  isted.  When.** 
19537 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f  p->explain==2, o
19538 6e 6c 79 20 4f 50 5f 45 78 70 6c 61 69 6e 20 69  nly OP_Explain i
19539 6e 73 74 72 75 63 74 69 6f 6e 73 20 61 72 65 20  nstructions are 
1953a 6c 69 73 74 65 64 20 61 6e 64 20 74 68 65 73 65  listed and these
1953b 0a 2a 2a 20 61 72 65 20 73 68 6f 77 6e 20 69 6e  .** are shown in
1953c 20 61 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72   a different for
1953d 6d 61 74 2e 20 20 70 2d 3e 65 78 70 6c 61 69 6e  mat.  p->explain
1953e 3d 3d 32 20 69 73 20 75 73 65 64 20 74 6f 20 69  ==2 is used to i
1953f 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c  mplement.** EXPL
19540 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 2e 0a  AIN QUERY PLAN..
19541 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19542 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19543 65 4c 69 73 74 28 0a 20 20 56 64 62 65 20 2a 70  eList(.  Vdbe *p
19544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19545 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a     /* The VDBE *
19546 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
19547 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e  db = p->db;.  in
19548 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t i;.  int rc = 
19549 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d  SQLITE_OK;.  Mem
1954a 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73   *pMem = p->pRes
1954b 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65  ultSet = &p->aMe
1954c 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28  m[1];..  assert(
1954d 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20   p->explain );. 
1954e 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56   if( p->magic!=V
1954f 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 20  DBE_MAGIC_RUN ) 
19550 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
19551 53 55 53 45 3b 0a 20 20 61 73 73 65 72 74 28 20  SUSE;.  assert( 
19552 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
19553 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a  E_MAGIC_BUSY );.
19554 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d    assert( p->rc=
19555 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d  =SQLITE_OK || p-
19556 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59  >rc==SQLITE_BUSY
19557 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54   || p->rc==SQLIT
19558 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a  E_NOMEM );..  /*
19559 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69   Even though thi
1955a 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f  s opcode does no
1955b 74 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74  t use dynamic st
1955c 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74  rings for.  ** t
1955d 68 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c  he result, resul
1955e 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65  t columns may be
1955f 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20  come dynamic if 
19560 74 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20  the user calls. 
19561 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   ** sqlite3_colu
19562 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75  mn_text16(), cau
19563 73 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69  sing a translati
19564 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63  on to UTF-16 enc
19565 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65  oding..  */.  re
19566 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d  leaseMemArray(pM
19567 65 6d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 0a 20  em, p->nMem);.. 
19568 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
19569 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
1956a 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
1956b 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e  if a malloc() in
1956c 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73  side a call to s
1956d 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1956e 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73  xt() or.    ** s
1956f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
19570 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20  xt16() failed.  
19571 2a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  */.    db->mallo
19572 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
19573 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
19574 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b  RROR;.  }..  do{
19575 0a 20 20 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b  .    i = p->pc++
19576 3b 0a 20 20 7d 77 68 69 6c 65 28 20 69 3c 70 2d  ;.  }while( i<p-
19577 3e 6e 4f 70 20 26 26 20 70 2d 3e 65 78 70 6c 61  >nOp && p->expla
19578 69 6e 3d 3d 32 20 26 26 20 70 2d 3e 61 4f 70 5b  in==2 && p->aOp[
19579 69 5d 2e 6f 70 63 6f 64 65 21 3d 4f 50 5f 45 78  i].opcode!=OP_Ex
1957a 70 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28 20 69  plain );.  if( i
1957b 3e 3d 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20  >=p->nOp ){.    
1957c 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  p->rc = SQLITE_O
1957d 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  K;.    rc = SQLI
1957e 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65  TE_DONE;.  }else
1957f 20 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e   if( db->u1.isIn
19580 74 65 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20  terrupted ){.   
19581 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
19582 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 20 72  INTERRUPT;.    r
19583 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
19584 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  ;.    sqlite3Set
19585 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
19586 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
19587 6c 69 74 65 33 45 72 72 53 74 72 28 70 2d 3e 72  lite3ErrStr(p->r
19588 63 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  c));.  }else{.  
19589 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 4f    char *z;.    O
1958a 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70  p *pOp = &p->aOp
1958b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  [i];.    if( p->
1958c 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20  explain==1 ){.  
1958d 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1958e 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
1958f 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
19590 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20  LITE_INTEGER;.  
19591 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
19592 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
19593 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19594 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75    /* Program cou
19595 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d  nter */.      pM
19596 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70  em++;.  .      p
19597 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
19598 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c  _Static|MEM_Str|
19599 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20  MEM_Term;.      
1959a 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a  pMem->z = (char*
1959b 29 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61  )sqlite3OpcodeNa
1959c 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b  me(pOp->opcode);
1959d 20 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20    /* Opcode */. 
1959e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65       assert( pMe
1959f 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20  m->z!=0 );.     
195a0 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74   pMem->n = sqlit
195a1 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d  e3Strlen30(pMem-
195a2 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  >z);.      pMem-
195a3 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54  >type = SQLITE_T
195a4 45 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  EXT;.      pMem-
195a5 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  >enc = SQLITE_UT
195a6 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b  F8;.      pMem++
195a7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 65  ;.    }..    pMe
195a8 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
195a9 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  nt;.    pMem->u.
195aa 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20 20 20  i = pOp->p1;    
195ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
195ac 20 20 20 20 20 20 2f 2a 20 50 31 20 2a 2f 0a 20        /* P1 */. 
195ad 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
195ae 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a  SQLITE_INTEGER;.
195af 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20      pMem++;..   
195b0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
195b1 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d  EM_Int;.    pMem
195b2 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 32 3b  ->u.i = pOp->p2;
195b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
195b4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 32 20            /* P2 
195b5 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  */.    pMem->typ
195b6 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  e = SQLITE_INTEG
195b7 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a  ER;.    pMem++;.
195b8 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c  .    if( p->expl
195b9 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ain==1 ){.      
195ba 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
195bb 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d 65  M_Int;.      pMe
195bc 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 33  m->u.i = pOp->p3
195bd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
195be 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 33             /* P3
195bf 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
195c0 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e  type = SQLITE_IN
195c1 54 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65  TEGER;.      pMe
195c2 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  m++;.    }..    
195c3 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
195c4 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 33 32 2c  emGrow(pMem, 32,
195c5 20 30 29 20 29 7b 20 20 20 20 20 20 20 20 20 20   0) ){          
195c6 20 20 2f 2a 20 50 34 20 2a 2f 0a 20 20 20 20 20    /* P4 */.     
195c7 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e   assert( p->db->
195c8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
195c9 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
195ca 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d  ITE_ERROR;.    }
195cb 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
195cc 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53   = MEM_Dyn|MEM_S
195cd 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  tr|MEM_Term;.   
195ce 20 7a 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70   z = displayP4(p
195cf 4f 70 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29  Op, pMem->z, 32)
195d0 3b 0a 20 20 20 20 69 66 28 20 7a 21 3d 70 4d 65  ;.    if( z!=pMe
195d1 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 73 71  m->z ){.      sq
195d2 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
195d3 74 72 28 70 4d 65 6d 2c 20 7a 2c 20 2d 31 2c 20  tr(pMem, z, -1, 
195d4 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b  SQLITE_UTF8, 0);
195d5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
195d6 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
195d7 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d  z!=0 );.      pM
195d8 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53  em->n = sqlite3S
195d9 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29  trlen30(pMem->z)
195da 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  ;.      pMem->en
195db 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
195dc 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
195dd 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54  >type = SQLITE_T
195de 45 58 54 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b  EXT;.    pMem++;
195df 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70  ..    if( p->exp
195e0 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20  lain==1 ){.     
195e1 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
195e2 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 34 2c  MemGrow(pMem, 4,
195e3 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61   0) ){.        a
195e4 73 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e 6d 61  ssert( p->db->ma
195e5 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
195e6 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
195e7 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
195e8 20 7d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66   }.      pMem->f
195e9 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d  lags = MEM_Dyn|M
195ea 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b  EM_Str|MEM_Term;
195eb 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
195ec 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   2;.      sqlite
195ed 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 70 4d  3_snprintf(3, pM
195ee 65 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 2c 20 70  em->z, "%.2x", p
195ef 4f 70 2d 3e 70 35 29 3b 20 20 20 2f 2a 20 50 35  Op->p5);   /* P5
195f0 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
195f1 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45  type = SQLITE_TE
195f2 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  XT;.      pMem->
195f3 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
195f4 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  8;.      pMem++;
195f5 0a 20 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .  .#ifdef SQLIT
195f6 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66  E_DEBUG.      if
195f7 28 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20  ( pOp->zComment 
195f8 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  ){.        pMem-
195f9 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72  >flags = MEM_Str
195fa 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20  |MEM_Term;.     
195fb 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70     pMem->z = pOp
195fc 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20  ->zComment;.    
195fd 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71      pMem->n = sq
195fe 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d  lite3Strlen30(pM
195ff 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 20 20  em->z);.        
19600 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
19601 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 20  TE_UTF8;.       
19602 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
19603 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20  LITE_TEXT;.     
19604 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
19605 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 4d      {.        pM
19606 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
19607 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Null;           
19608 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
19609 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20  omment */.      
1960a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
1960b 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20  QLITE_NULL;.    
1960c 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70    }.    }..    p
1960d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 38  ->nResColumn = 8
1960e 20 2d 20 35 2a 28 70 2d 3e 65 78 70 6c 61 69 6e   - 5*(p->explain
1960f 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  -1);.    p->rc =
19610 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
19611 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b  rc = SQLITE_ROW;
19612 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
19613 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
19614 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
19615 4e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  N */..#ifdef SQL
19616 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
19617 50 72 69 6e 74 20 74 68 65 20 53 51 4c 20 74 68  Print the SQL th
19618 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 67  at was used to g
19619 65 6e 65 72 61 74 65 20 61 20 56 44 42 45 20 70  enerate a VDBE p
1961a 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  rogram..*/.SQLIT
1961b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1961c 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53  qlite3VdbePrintS
1961d 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69  ql(Vdbe *p){.  i
1961e 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b  nt nOp = p->nOp;
1961f 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a  .  VdbeOp *pOp;.
19620 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65    if( nOp<1 ) re
19621 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70  turn;.  pOp = &p
19622 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20  ->aOp[0];.  if( 
19623 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
19624 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34  Trace && pOp->p4
19625 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e  .z!=0 ){.    con
19626 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70  st char *z = pOp
19627 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 68 69 6c  ->p4.z;.    whil
19628 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  e( sqlite3Isspac
19629 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20  e(*z) ) z++;.   
1962a 20 70 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25   printf("SQL: [%
1962b 73 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d  s]\n", z);.  }.}
1962c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65  .#endif..#if !de
1962d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1962e 54 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69  T_TRACE) && defi
1962f 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
19630 45 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a  E_IOTRACE)./*.**
19631 20 50 72 69 6e 74 20 61 6e 20 49 4f 54 52 41 43   Print an IOTRAC
19632 45 20 6d 65 73 73 61 67 65 20 73 68 6f 77 69 6e  E message showin
19633 67 20 53 51 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a  g SQL content..*
19634 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19635 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
19636 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65  eIOTraceSql(Vdbe
19637 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20   *p){.  int nOp 
19638 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65  = p->nOp;.  Vdbe
19639 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 73  Op *pOp;.  if( s
1963a 71 6c 69 74 65 33 49 6f 54 72 61 63 65 3d 3d 30  qlite3IoTrace==0
1963b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
1963c 20 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b   nOp<1 ) return;
1963d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70  .  pOp = &p->aOp
1963e 5b 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  [0];.  if( pOp->
1963f 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65  opcode==OP_Trace
19640 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30   && pOp->p4.z!=0
19641 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a   ){.    int i, j
19642 3b 0a 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30  ;.    char z[100
19643 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0];.    sqlite3_
19644 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
19645 7a 29 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70  z), z, "%s", pOp
19646 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 66 6f 72  ->p4.z);.    for
19647 28 69 3d 30 3b 20 73 71 6c 69 74 65 33 49 73 73  (i=0; sqlite3Iss
19648 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  pace(z[i]); i++)
19649 7b 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  {}.    for(j=0; 
1964a 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  z[i]; i++){.    
1964b 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 73    if( sqlite3Iss
1964c 70 61 63 65 28 7a 5b 69 5d 29 20 29 7b 0a 20 20  pace(z[i]) ){.  
1964d 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2d 31 5d        if( z[i-1]
1964e 21 3d 27 20 27 20 29 7b 0a 20 20 20 20 20 20 20  !=' ' ){.       
1964f 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b     z[j++] = ' ';
19650 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
19651 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
19652 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20  z[j++] = z[i];. 
19653 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
19654 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73   z[j] = 0;.    s
19655 71 6c 69 74 65 33 49 6f 54 72 61 63 65 28 22 53  qlite3IoTrace("S
19656 51 4c 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20  QL %s\n", z);.  
19657 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  }.}.#endif /* !S
19658 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
19659 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
1965a 45 5f 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a  E_IOTRACE */../*
1965b 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61  .** Allocate spa
1965c 63 65 20 66 72 6f 6d 20 61 20 66 69 78 65 64 20  ce from a fixed 
1965d 73 69 7a 65 20 62 75 66 66 65 72 2e 20 20 4d 61  size buffer.  Ma
1965e 6b 65 20 2a 70 70 20 70 6f 69 6e 74 20 74 6f 20  ke *pp point to 
1965f 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  the.** allocated
19660 20 73 70 61 63 65 2e 20 20 28 4e 6f 74 65 3a 20   space.  (Note: 
19661 20 70 70 20 69 73 20 61 20 63 68 61 72 2a 20 72   pp is a char* r
19662 61 74 68 65 72 20 74 68 61 6e 20 61 20 76 6f 69  ather than a voi
19663 64 2a 2a 20 74 6f 0a 2a 2a 20 77 6f 72 6b 20 61  d** to.** work a
19664 72 6f 75 6e 64 20 74 68 65 20 70 6f 69 6e 74 65  round the pointe
19665 72 20 61 6c 69 61 73 69 6e 67 20 72 75 6c 65 73  r aliasing rules
19666 20 6f 66 20 43 2e 29 20 20 2a 70 70 20 73 68 6f   of C.)  *pp sho
19667 75 6c 64 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a  uld initially.**
19668 20 62 65 20 7a 65 72 6f 2e 20 20 49 66 20 2a 70   be zero.  If *p
19669 70 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74  p is not zero, t
1966a 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74  hat means that t
1966b 68 65 20 73 70 61 63 65 20 68 61 73 20 61 6c 72  he space has alr
1966c 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 61 6c 6c  eady.** been all
1966d 6f 63 61 74 65 64 20 61 6e 64 20 74 68 69 73 20  ocated and this 
1966e 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 6f  routine is a noo
1966f 70 2e 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 20 69  p..**.** nByte i
19670 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
19671 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6e  bytes of space n
19672 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70  eeded..**.** *pp
19673 46 72 6f 6d 20 70 6f 69 6e 74 20 74 6f 20 61 76  From point to av
19674 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 61 6e  ailable space an
19675 64 20 70 45 6e 64 20 70 6f 69 6e 74 73 20 74 6f  d pEnd points to
19676 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a   the end of the.
19677 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61  ** available spa
19678 63 65 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 42 79 74  ce..**.** *pnByt
19679 65 20 69 73 20 61 20 63 6f 75 6e 74 65 72 20 6f  e is a counter o
1967a 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
1967b 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 74  bytes of space t
1967c 68 61 74 20 68 61 76 65 20 66 61 69 6c 65 64 0a  hat have failed.
1967d 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2e 20  ** to allocate. 
1967e 20 49 66 20 74 68 65 72 65 20 69 73 20 69 6e 73   If there is ins
1967f 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20  ufficient space 
19680 69 6e 20 2a 70 70 46 72 6f 6d 20 74 6f 20 73 61  in *ppFrom to sa
19681 74 69 73 66 79 20 74 68 65 0a 2a 2a 20 72 65 71  tisfy the.** req
19682 75 65 73 74 2c 20 74 68 65 6e 20 69 6e 63 72 65  uest, then incre
19683 6d 65 6e 74 20 2a 70 6e 42 79 74 65 20 62 79 20  ment *pnByte by 
19684 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68  the amount of th
19685 65 20 72 65 71 75 65 73 74 2e 0a 2a 2f 0a 73 74  e request..*/.st
19686 61 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 53  atic void allocS
19687 70 61 63 65 28 0a 20 20 63 68 61 72 20 2a 70 70  pace(.  char *pp
19688 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
19689 49 4e 2f 4f 55 54 3a 20 53 65 74 20 2a 70 70 20  IN/OUT: Set *pp 
1968a 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 6c 6c 6f  to point to allo
1968b 63 61 74 65 64 20 62 75 66 66 65 72 20 2a 2f 0a  cated buffer */.
1968c 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
1968d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1968e 20 6f 66 20 62 79 74 65 73 20 74 6f 20 61 6c 6c   of bytes to all
1968f 6f 63 61 74 65 20 2a 2f 0a 20 20 75 38 20 2a 2a  ocate */.  u8 **
19690 70 70 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20  ppFrom,         
19691 2f 2a 20 49 4e 2f 4f 55 54 3a 20 41 6c 6c 6f 63  /* IN/OUT: Alloc
19692 61 74 65 20 66 72 6f 6d 20 2a 70 70 46 72 6f 6d  ate from *ppFrom
19693 20 2a 2f 0a 20 20 75 38 20 2a 70 45 6e 64 2c 20   */.  u8 *pEnd, 
19694 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
19695 69 6e 74 65 72 20 74 6f 20 31 20 62 79 74 65 20  inter to 1 byte 
19696 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
19697 2a 70 70 46 72 6f 6d 20 62 75 66 66 65 72 20 2a  *ppFrom buffer *
19698 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 20  /.  int *pnByte 
19699 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61           /* If a
1969a 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 6e 6f 74  llocation cannot
1969b 20 62 65 20 6d 61 64 65 2c 20 69 6e 63 72 65 6d   be made, increm
1969c 65 6e 74 20 2a 70 6e 42 79 74 65 20 2a 2f 0a 29  ent *pnByte */.)
1969d 7b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48  {.  assert( EIGH
1969e 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54  T_BYTE_ALIGNMENT
1969f 28 2a 70 70 46 72 6f 6d 29 20 29 3b 0a 20 20 69  (*ppFrom) );.  i
196a0 66 28 20 28 2a 28 76 6f 69 64 2a 2a 29 70 70 29  f( (*(void**)pp)
196a1 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 42 79 74 65  ==0 ){.    nByte
196a2 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29   = ROUND8(nByte)
196a3 3b 0a 20 20 20 20 69 66 28 20 28 70 45 6e 64 20  ;.    if( (pEnd 
196a4 2d 20 2a 70 70 46 72 6f 6d 29 3e 3d 6e 42 79 74  - *ppFrom)>=nByt
196a5 65 20 29 7b 0a 20 20 20 20 20 20 2a 28 76 6f 69  e ){.      *(voi
196a6 64 2a 2a 29 70 70 20 3d 20 28 76 6f 69 64 20 2a  d**)pp = (void *
196a7 29 2a 70 70 46 72 6f 6d 3b 0a 20 20 20 20 20 20  )*ppFrom;.      
196a8 2a 70 70 46 72 6f 6d 20 2b 3d 20 6e 42 79 74 65  *ppFrom += nByte
196a9 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
196aa 20 20 20 2a 70 6e 42 79 74 65 20 2b 3d 20 6e 42     *pnByte += nB
196ab 79 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  yte;.    }.  }.}
196ac 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20  ../*.** Prepare 
196ad 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  a virtual machin
196ae 65 20 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 2e  e for execution.
196af 20 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 20    This involves 
196b0 74 68 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20 61  things such.** a
196b1 73 20 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61  s allocating sta
196b2 63 6b 20 73 70 61 63 65 20 61 6e 64 20 69 6e 69  ck space and ini
196b3 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 72  tializing the pr
196b4 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a  ogram counter..*
196b5 2a 20 41 66 74 65 72 20 74 68 65 20 56 44 42 45  * After the VDBE
196b6 20 68 61 73 20 62 65 20 70 72 65 70 70 65 64 2c   has be prepped,
196b7 20 69 74 20 63 61 6e 20 62 65 20 65 78 65 63 75   it can be execu
196b8 74 65 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f  ted by one or mo
196b9 72 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73  re.** calls to s
196ba 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29  qlite3VdbeExec()
196bb 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  .  .**.** This i
196bc 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74  s the only way t
196bd 6f 20 6d 6f 76 65 20 61 20 56 44 42 45 20 66 72  o move a VDBE fr
196be 6f 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  om VDBE_MAGIC_IN
196bf 49 54 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41  IT to.** VDBE_MA
196c0 47 49 43 5f 52 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54  GIC_RUN..**.** T
196c1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
196c2 20 62 65 20 63 61 6c 6c 65 64 20 6d 6f 72 65 20   be called more 
196c3 74 68 61 6e 20 6f 6e 63 65 20 6f 6e 20 61 20 73  than once on a s
196c4 69 6e 67 6c 65 20 76 69 72 74 75 61 6c 20 6d 61  ingle virtual ma
196c5 63 68 69 6e 65 2e 0a 2a 2a 20 54 68 65 20 66 69  chine..** The fi
196c6 72 73 74 20 63 61 6c 6c 20 69 73 20 6d 61 64 65  rst call is made
196c7 20 77 68 69 6c 65 20 63 6f 6d 70 69 6c 69 6e 67   while compiling
196c8 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
196c9 6e 74 2e 20 53 75 62 73 65 71 75 65 6e 74 0a 2a  nt. Subsequent.*
196ca 2a 20 63 61 6c 6c 73 20 61 72 65 20 6d 61 64 65  * calls are made
196cb 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
196cc 70 72 6f 63 65 73 73 20 6f 66 20 72 65 73 65 74  process of reset
196cd 74 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74  ting a statement
196ce 20 74 6f 20 62 65 0a 2a 2a 20 72 65 2d 65 78 65   to be.** re-exe
196cf 63 75 74 65 64 20 28 66 72 6f 6d 20 61 20 63 61  cuted (from a ca
196d0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  ll to sqlite3_re
196d1 73 65 74 28 29 29 2e 20 54 68 65 20 6e 56 61 72  set()). The nVar
196d2 2c 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20  , nMem, nCursor 
196d3 0a 2a 2a 20 61 6e 64 20 69 73 45 78 70 6c 61 69  .** and isExplai
196d4 6e 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  n parameters are
196d5 20 6f 6e 6c 79 20 70 61 73 73 65 64 20 63 6f 72   only passed cor
196d6 72 65 63 74 20 76 61 6c 75 65 73 20 74 68 65 20  rect values the 
196d7 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68  first time.** th
196d8 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  e function is ca
196d9 6c 6c 65 64 2e 20 4f 6e 20 73 75 62 73 65 71 75  lled. On subsequ
196da 65 6e 74 20 63 61 6c 6c 73 2c 20 66 72 6f 6d 20  ent calls, from 
196db 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c  sqlite3_reset(),
196dc 20 6e 56 61 72 0a 2a 2a 20 69 73 20 70 61 73 73   nVar.** is pass
196dd 65 64 20 2d 31 20 61 6e 64 20 6e 4d 65 6d 2c 20  ed -1 and nMem, 
196de 6e 43 75 72 73 6f 72 20 61 6e 64 20 69 73 45 78  nCursor and isEx
196df 70 6c 61 69 6e 20 61 72 65 20 61 6c 6c 20 70 61  plain are all pa
196e0 73 73 65 64 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51  ssed zero..*/.SQ
196e1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
196e2 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  d sqlite3VdbeMak
196e3 65 52 65 61 64 79 28 0a 20 20 56 64 62 65 20 2a  eReady(.  Vdbe *
196e4 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
196e5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
196e6 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 56  VDBE */.  int nV
196e7 61 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ar,             
196e8 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
196e9 65 72 20 6f 66 20 27 3f 27 20 73 65 65 20 69 6e  er of '?' see in
196ea 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
196eb 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d  nt */.  int nMem
196ec 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
196ed 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
196ee 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73   of memory cells
196ef 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a   to allocate */.
196f0 20 20 69 6e 74 20 6e 43 75 72 73 6f 72 2c 20 20    int nCursor,  
196f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
196f2 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75   /* Number of cu
196f3 72 73 6f 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74  rsors to allocat
196f4 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 78 70  e */.  int isExp
196f5 6c 61 69 6e 20 20 20 20 20 20 20 20 20 20 20 20  lain            
196f6 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
196f7 20 74 68 65 20 45 58 50 4c 41 49 4e 20 6b 65 79   the EXPLAIN key
196f8 77 6f 72 64 73 20 69 73 20 70 72 65 73 65 6e 74  words is present
196f9 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a   */.){.  int n;.
196fa 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
196fb 70 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74  p->db;..  assert
196fc 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( p!=0 );.  asse
196fd 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44  rt( p->magic==VD
196fe 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b  BE_MAGIC_INIT );
196ff 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f  ..  /* There sho
19700 75 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20  uld be at least 
19701 6f 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f  one opcode..  */
19702 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f  .  assert( p->nO
19703 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74  p>0 );..  /* Set
19704 20 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44   the magic to VD
19705 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f  BE_MAGIC_RUN soo
19706 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20  ner rather than 
19707 6c 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d  later. */.  p->m
19708 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
19709 43 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72  C_RUN;..  /* For
1970a 20 65 61 63 68 20 63 75 72 73 6f 72 20 72 65 71   each cursor req
1970b 75 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f  uired, also allo
1970c 63 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63 65  cate a memory ce
1970d 6c 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20  ll. Memory.  ** 
1970e 63 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43  cells (nMem+1-nC
1970f 75 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e  ursor)..nMem, in
19710 63 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65  clusive, will ne
19711 76 65 72 20 62 65 20 75 73 65 64 20 62 79 0a 20  ver be used by. 
19712 20 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72 6f   ** the vdbe pro
19713 67 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74 68  gram. Instead th
19714 65 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 61  ey are used to a
19715 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
19716 72 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f  r.  ** VdbeCurso
19717 72 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75 63  r/BtCursor struc
19718 74 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62 20  tures. The blob 
19719 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69  of memory associ
1971a 61 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a 20  ated with .  ** 
1971b 63 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f 72  cursor 0 is stor
1971c 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c  ed in memory cel
1971d 6c 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63  l nMem. Memory c
1971e 65 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a  ell (nMem-1).  *
1971f 2a 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c 6f  * stores the blo
19720 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f  b of memory asso
19721 63 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73  ciated with curs
19722 6f 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a  or 1, etc..  **.
19723 20 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61    ** See also: a
19724 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29 2e  llocateCursor().
19725 0a 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20  .  */.  nMem += 
19726 6e 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41  nCursor;..  /* A
19727 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
19728 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65  r memory registe
19729 72 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c 65  rs, SQL variable
1972a 73 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73 20  s, VDBE cursors 
1972b 61 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72  and .  ** an arr
1972c 61 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53 51  ay to marshal SQ
1972d 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  L function argum
1972e 65 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69 73  ents in. This is
1972f 20 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20   only done the. 
19730 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 74   ** first time t
19731 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
19732 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69 76  called for a giv
19733 65 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68 65  en VDBE, not whe
19734 6e 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65 69  n it is.  ** bei
19735 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73  ng called from s
19736 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 74  qlite3_reset() t
19737 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72 74  o reset the virt
19738 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a  ual machine..  *
19739 2f 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30 20  /.  if( nVar>=0 
1973a 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  && !db->mallocFa
1973b 69 6c 65 64 20 29 7b 0a 20 20 20 20 75 38 20 2a  iled ){.    u8 *
1973c 7a 43 73 72 20 3d 20 28 75 38 20 2a 29 26 70 2d  zCsr = (u8 *)&p-
1973d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 5d 3b 0a 20 20  >aOp[p->nOp];.  
1973e 20 20 75 38 20 2a 7a 45 6e 64 20 3d 20 28 75 38    u8 *zEnd = (u8
1973f 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f   *)&p->aOp[p->nO
19740 70 41 6c 6c 6f 63 5d 3b 0a 20 20 20 20 69 6e 74  pAlloc];.    int
19741 20 6e 42 79 74 65 3b 0a 20 20 20 20 69 6e 74 20   nByte;.    int 
19742 6e 41 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d  nArg;       /* M
19743 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
19744 20 61 72 67 73 20 70 61 73 73 65 64 20 74 6f 20   args passed to 
19745 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e  a user function.
19746 20 2a 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50   */.    resolveP
19747 32 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67  2Values(p, &nArg
19748 29 3b 0a 20 20 20 20 69 66 28 20 69 73 45 78 70  );.    if( isExp
19749 6c 61 69 6e 20 26 26 20 6e 4d 65 6d 3c 31 30 20  lain && nMem<10 
1974a 29 7b 0a 20 20 20 20 20 20 6e 4d 65 6d 20 3d 20  ){.      nMem = 
1974b 31 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43  10;.    }.    zC
1974c 73 72 20 2b 3d 20 28 7a 43 73 72 20 2d 20 28 75  sr += (zCsr - (u
1974d 38 2a 29 30 29 26 37 3b 0a 20 20 20 20 61 73 73  8*)0)&7;.    ass
1974e 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
1974f 41 4c 49 47 4e 4d 45 4e 54 28 7a 43 73 72 29 20  ALIGNMENT(zCsr) 
19750 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 6e 64 3c  );.    if( zEnd<
19751 7a 43 73 72 20 29 20 7a 45 6e 64 20 3d 20 7a 43  zCsr ) zEnd = zC
19752 73 72 3b 0a 0a 20 20 20 20 64 6f 20 7b 0a 20 20  sr;..    do {.  
19753 20 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72 2c      memset(zCsr,
19754 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b 0a   0, zEnd-zCsr);.
19755 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 30 3b        nByte = 0;
19756 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63  .      allocSpac
19757 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 4d 65  e((char*)&p->aMe
19758 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66 28 4d  m, nMem*sizeof(M
19759 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64  em), &zCsr, zEnd
1975a 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  , &nByte);.     
1975b 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61   allocSpace((cha
1975c 72 2a 29 26 70 2d 3e 61 56 61 72 2c 20 6e 56 61  r*)&p->aVar, nVa
1975d 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26  r*sizeof(Mem), &
1975e 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79  zCsr, zEnd, &nBy
1975f 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63  te);.      alloc
19760 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d  Space((char*)&p-
19761 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a 73 69 7a  >apArg, nArg*siz
19762 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a 43 73 72  eof(Mem*), &zCsr
19763 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b  , zEnd, &nByte);
19764 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63  .      allocSpac
19765 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 7a 56  e((char*)&p->azV
19766 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28  ar, nVar*sizeof(
19767 63 68 61 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a  char*), &zCsr, z
19768 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20  End, &nByte);.  
19769 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28      allocSpace((
1976a 63 68 61 72 2a 29 26 70 2d 3e 61 70 43 73 72 2c  char*)&p->apCsr,
1976b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1976c 20 20 20 6e 43 75 72 73 6f 72 2a 73 69 7a 65 6f     nCursor*sizeo
1976d 66 28 56 64 62 65 43 75 72 73 6f 72 2a 29 2c 20  f(VdbeCursor*), 
1976e 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42  &zCsr, zEnd, &nB
1976f 79 74 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  yte.      );.   
19770 20 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b 0a     if( nByte ){.
19771 20 20 20 20 20 20 20 20 70 2d 3e 70 46 72 65 65          p->pFree
19772 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
19773 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 29  ocRaw(db, nByte)
19774 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
19775 7a 43 73 72 20 3d 20 70 2d 3e 70 46 72 65 65 3b  zCsr = p->pFree;
19776 0a 20 20 20 20 20 20 7a 45 6e 64 20 3d 20 26 7a  .      zEnd = &z
19777 43 73 72 5b 6e 42 79 74 65 5d 3b 0a 20 20 20 20  Csr[nByte];.    
19778 7d 77 68 69 6c 65 28 20 6e 42 79 74 65 20 26 26  }while( nByte &&
19779 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
1977a 65 64 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 6e 43  ed );..    p->nC
1977b 75 72 73 6f 72 20 3d 20 6e 43 75 72 73 6f 72 3b  ursor = nCursor;
1977c 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72  .    if( p->aVar
1977d 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61   ){.      p->nVa
1977e 72 20 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20 20  r = nVar;.      
1977f 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b  for(n=0; n<nVar;
19780 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70   n++){.        p
19781 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20  ->aVar[n].flags 
19782 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
19783 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64      p->aVar[n].d
19784 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a  b = db;.      }.
19785 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d      }.    if( p-
19786 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70  >aMem ){.      p
19787 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20  ->aMem--;       
19788 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19789 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 72  * aMem[] goes fr
1978a 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 20  om 1..nMem */.  
1978b 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d      p->nMem = nM
1978c 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
1978d 20 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74      /*       not
1978e 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20   from 0..nMem-1 
1978f 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d 31  */.      for(n=1
19790 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b  ; n<=nMem; n++){
19791 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d  .        p->aMem
19792 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  [n].flags = MEM_
19793 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d  Null;.        p-
19794 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62  >aMem[n].db = db
19795 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
19796 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
19797 45 5f 44 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d  E_DEBUG.  for(n=
19798 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b  1; n<p->nMem; n+
19799 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
1979a 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64  p->aMem[n].db==d
1979b 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  b );.  }.#endif.
1979c 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20  .  p->pc = -1;. 
1979d 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
1979e 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63  OK;.  p->errorAc
1979f 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  tion = OE_Abort;
197a0 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d  .  p->explain |=
197a1 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d   isExplain;.  p-
197a2 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41  >magic = VDBE_MA
197a3 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43  GIC_RUN;.  p->nC
197a4 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  hange = 0;.  p->
197a5 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20  cacheCtr = 1;.  
197a6 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46  p->minWriteFileF
197a7 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70  ormat = 255;.  p
197a8 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30  ->iStatement = 0
197a9 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  ;.#ifdef VDBE_PR
197aa 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 69 6e  OFILE.  {.    in
197ab 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
197ac 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29  ; i<p->nOp; i++)
197ad 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69  {.      p->aOp[i
197ae 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  ].cnt = 0;.     
197af 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65   p->aOp[i].cycle
197b0 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  s = 0;.    }.  }
197b1 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
197b2 20 43 6c 6f 73 65 20 61 20 56 44 42 45 20 63 75   Close a VDBE cu
197b3 72 73 6f 72 20 61 6e 64 20 72 65 6c 65 61 73 65  rsor and release
197b4 20 61 6c 6c 20 74 68 65 20 72 65 73 6f 75 72 63   all the resourc
197b5 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 0a  es that cursor .
197b6 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f 20 68 6f  ** happens to ho
197b7 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ld..*/.SQLITE_PR
197b8 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
197b9 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72  e3VdbeFreeCursor
197ba 28 56 64 62 65 20 2a 70 2c 20 56 64 62 65 43 75  (Vdbe *p, VdbeCu
197bb 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 69 66  rsor *pCx){.  if
197bc 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 20 20  ( pCx==0 ){.    
197bd 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66  return;.  }.  if
197be 28 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20  ( pCx->pBt ){.  
197bf 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
197c0 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20  ose(pCx->pBt);. 
197c1 20 20 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70     /* The pCx->p
197c2 43 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 63  Cursor will be c
197c3 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  lose automatical
197c4 6c 79 2c 20 69 66 20 69 74 20 65 78 69 73 74 73  ly, if it exists
197c5 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 20  , by.    ** the 
197c6 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20  call above. */. 
197c7 20 7d 65 6c 73 65 20 69 66 28 20 70 43 78 2d 3e   }else if( pCx->
197c8 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73  pCursor ){.    s
197c9 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
197ca 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 43 75 72  Cursor(pCx->pCur
197cb 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  sor);.  }.#ifnde
197cc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
197cd 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
197ce 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f   pCx->pVtabCurso
197cf 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
197d0 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56  _vtab_cursor *pV
197d1 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43 78 2d  tabCursor = pCx-
197d2 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20  >pVtabCursor;.  
197d3 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
197d4 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20  module *pModule 
197d5 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a  = pCx->pModule;.
197d6 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74      p->inVtabMet
197d7 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28 76 6f  hod = 1;.    (vo
197d8 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
197d9 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20  Off(p->db);.    
197da 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28  pModule->xClose(
197db 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20  pVtabCursor);.  
197dc 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
197dd 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29 3b 0a  afetyOn(p->db);.
197de 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74      p->inVtabMet
197df 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e  hod = 0;.  }.#en
197e0 64 69 66 0a 20 20 69 66 28 20 21 70 43 78 2d 3e  dif.  if( !pCx->
197e1 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65  ephemPseudoTable
197e2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
197e3 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 78  bFree(p->db, pCx
197e4 2d 3e 70 44 61 74 61 29 3b 0a 20 20 7d 0a 7d 0a  ->pData);.  }.}.
197e5 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c  ./*.** Close all
197e6 20 63 75 72 73 6f 72 73 20 65 78 63 65 70 74 20   cursors except 
197e7 66 6f 72 20 56 54 61 62 20 63 75 72 73 6f 72 73  for VTab cursors
197e8 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e   that are curren
197e9 74 6c 79 0a 2a 2a 20 69 6e 20 75 73 65 2e 0a 2a  tly.** in use..*
197ea 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c  /.static void cl
197eb 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63  oseAllCursorsExc
197ec 65 70 74 41 63 74 69 76 65 56 74 61 62 73 28 56  eptActiveVtabs(V
197ed 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  dbe *p){.  int i
197ee 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72  ;.  if( p->apCsr
197ef 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
197f0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43  for(i=0; i<p->nC
197f1 75 72 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  ursor; i++){.   
197f2 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20   VdbeCursor *pC 
197f3 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20  = p->apCsr[i];. 
197f4 20 20 20 69 66 28 20 70 43 20 26 26 20 28 21 70     if( pC && (!p
197f5 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 7c  ->inVtabMethod |
197f6 7c 20 21 70 43 2d 3e 70 56 74 61 62 43 75 72 73  | !pC->pVtabCurs
197f7 6f 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  or) ){.      sql
197f8 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73  ite3VdbeFreeCurs
197f9 6f 72 28 70 2c 20 70 43 29 3b 0a 20 20 20 20 20  or(p, pC);.     
197fa 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20 30   p->apCsr[i] = 0
197fb 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
197fc 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74 68  *.** Clean up th
197fd 65 20 56 4d 20 61 66 74 65 72 20 65 78 65 63 75  e VM after execu
197fe 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tion..**.** This
197ff 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 75   routine will au
19800 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73  tomatically clos
19801 65 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20 6c  e any cursors, l
19802 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20  ists, and/or.** 
19803 73 6f 72 74 65 72 73 20 74 68 61 74 20 77 65 72  sorters that wer
19804 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49 74  e left open.  It
19805 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74 68   also deletes th
19806 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20 76  e values of.** v
19807 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65 20  ariables in the 
19808 61 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f  aVar[] array..*/
19809 0a 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c 65  .static void Cle
1980a 61 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a 20  anup(Vdbe *p){. 
1980b 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65   int i;.  sqlite
1980c 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  3 *db = p->db;. 
1980d 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 63 6c   Mem *pMem;.  cl
1980e 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63  oseAllCursorsExc
1980f 65 70 74 41 63 74 69 76 65 56 74 61 62 73 28 70  eptActiveVtabs(p
19810 29 3b 0a 20 20 66 6f 72 28 70 4d 65 6d 3d 26 70  );.  for(pMem=&p
19811 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 69 3d 31 3b 20  ->aMem[1], i=1; 
19812 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b 2b 2c  i<=p->nMem; i++,
19813 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66   pMem++){.    if
19814 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
19815 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20  MEM_RowSet ){.  
19816 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65      sqlite3RowSe
19817 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e 70  tClear(pMem->u.p
19818 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20  RowSet);.    }. 
19819 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1981a 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c  g(pMem, MEM_Null
1981b 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65  );.  }.  release
1981c 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d 65  MemArray(&p->aMe
1981d 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a  m[1], p->nMem);.
1981e 20 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74    if( p->context
1981f 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c  Stack ){.    sql
19820 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
19821 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 29 3b  ->contextStack);
19822 0a 20 20 7d 0a 20 20 70 2d 3e 63 6f 6e 74 65 78  .  }.  p->contex
19823 74 53 74 61 63 6b 20 3d 20 30 3b 0a 20 20 70 2d  tStack = 0;.  p-
19824 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70  >contextStackDep
19825 74 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e  th = 0;.  p->con
19826 74 65 78 74 53 74 61 63 6b 54 6f 70 20 3d 20 30  textStackTop = 0
19827 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
19828 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
19829 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  );.  p->zErrMsg 
1982a 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c  = 0;.  p->pResul
1982b 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  tSet = 0;.}../*.
1982c 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65  ** Set the numbe
1982d 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75  r of result colu
1982e 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65  mns that will be
1982f 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69   returned by thi
19830 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65  s SQL.** stateme
19831 6e 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 77 20  nt. This is now 
19832 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74  set at compile t
19833 69 6d 65 2c 20 72 61 74 68 65 72 20 74 68 61 6e  ime, rather than
19834 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65 63 75   during.** execu
19835 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 62 65  tion of the vdbe
19836 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 61 74   program so that
19837 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
19838 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 62  count() can.** b
19839 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 53  e called on an S
1983a 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 66  QL statement bef
1983b 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ore sqlite3_step
1983c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
1983d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1983e 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
1983f 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 52  (Vdbe *p, int nR
19840 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 6d  esColumn){.  Mem
19841 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 6e   *pColName;.  in
19842 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  t n;.  sqlite3 *
19843 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 72  db = p->db;..  r
19844 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70  eleaseMemArray(p
19845 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e  ->aColName, p->n
19846 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d  ResColumn*COLNAM
19847 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  E_N);.  sqlite3D
19848 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f  bFree(db, p->aCo
19849 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e 52  lName);.  n = nR
1984a 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45  esColumn*COLNAME
1984b 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43 6f 6c  _N;.  p->nResCol
1984c 75 6d 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e  umn = nResColumn
1984d 3b 0a 20 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 20  ;.  p->aColName 
1984e 3d 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 28 4d 65  = pColName = (Me
1984f 6d 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  m*)sqlite3DbMall
19850 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
19851 66 28 4d 65 6d 29 2a 6e 20 29 3b 0a 20 20 69 66  f(Mem)*n );.  if
19852 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 3d 3d 30  ( p->aColName==0
19853 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69   ) return;.  whi
19854 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20  le( n-- > 0 ){. 
19855 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61     pColName->fla
19856 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
19857 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 64 62 20     pColName->db 
19858 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70 43 6f  = p->db;.    pCo
19859 6c 4e 61 6d 65 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a  lName++;.  }.}..
1985a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 61  /*.** Set the na
1985b 6d 65 20 6f 66 20 74 68 65 20 69 64 78 27 74 68  me of the idx'th
1985c 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 72 65   column to be re
1985d 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 53 51  turned by the SQ
1985e 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  L statement..** 
1985f 7a 4e 61 6d 65 20 6d 75 73 74 20 62 65 20 61 20  zName must be a 
19860 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 75 6c  pointer to a nul
19861 20 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69   terminated stri
19862 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63  ng..**.** This c
19863 61 6c 6c 20 6d 75 73 74 20 62 65 20 6d 61 64 65  all must be made
19864 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f   after a call to
19865 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
19866 75 6d 43 6f 6c 73 28 29 2e 0a 2a 2a 0a 2a 2a 20  umCols()..**.** 
19867 54 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65  The final parame
19868 74 65 72 2c 20 78 44 65 6c 2c 20 6d 75 73 74 20  ter, xDel, must 
19869 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45  be one of SQLITE
1986a 5f 44 59 4e 41 4d 49 43 2c 20 53 51 4c 49 54 45  _DYNAMIC, SQLITE
1986b 5f 53 54 41 54 49 43 0a 2a 2a 20 6f 72 20 53 51  _STATIC.** or SQ
1986c 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 20  LITE_TRANSIENT. 
1986d 49 66 20 69 74 20 69 73 20 53 51 4c 49 54 45 5f  If it is SQLITE_
1986e 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 74 68  DYNAMIC, then th
1986f 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64  e buffer pointed
19870 0a 2a 2a 20 74 6f 20 62 79 20 7a 4e 61 6d 65 20  .** to by zName 
19871 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 62 79  will be freed by
19872 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29   sqlite3DbFree()
19873 20 77 68 65 6e 20 74 68 65 20 76 64 62 65 20 69   when the vdbe i
19874 73 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a  s destroyed..*/.
19875 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19876 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  nt sqlite3VdbeSe
19877 74 43 6f 6c 4e 61 6d 65 28 0a 20 20 56 64 62 65  tColName(.  Vdbe
19878 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
19879 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1987a 56 64 62 65 20 62 65 69 6e 67 20 63 6f 6e 66 69  Vdbe being confi
1987b 67 75 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  gured */.  int i
1987c 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  dx,             
1987d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1987e 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 7a  ndex of column z
1987f 4e 61 6d 65 20 61 70 70 6c 69 65 73 20 74 6f 20  Name applies to 
19880 2a 2f 0a 20 20 69 6e 74 20 76 61 72 2c 20 20 20  */.  int var,   
19881 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19882 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
19883 74 68 65 20 43 4f 4c 4e 41 4d 45 5f 2a 20 63 6f  the COLNAME_* co
19884 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 63 6f 6e  nstants */.  con
19885 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
19886 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19887 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66   Pointer to buff
19888 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 61  er containing na
19889 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  me */.  void (*x
1988a 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 20  Del)(void*)     
1988b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f           /* Memo
1988c 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74  ry management st
1988d 72 61 74 65 67 79 20 66 6f 72 20 7a 4e 61 6d 65  rategy for zName
1988e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
1988f 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65  .  Mem *pColName
19890 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c  ;.  assert( idx<
19891 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 29 3b  p->nResColumn );
19892 0a 20 20 61 73 73 65 72 74 28 20 76 61 72 3c 43  .  assert( var<C
19893 4f 4c 4e 41 4d 45 5f 4e 20 29 3b 0a 20 20 69 66  OLNAME_N );.  if
19894 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ( p->db->mallocF
19895 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 61 73 73  ailed ){.    ass
19896 65 72 74 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 78  ert( !zName || x
19897 44 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 4e 41  Del!=SQLITE_DYNA
19898 4d 49 43 20 29 3b 0a 20 20 20 20 72 65 74 75 72  MIC );.    retur
19899 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
1989a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d    }.  assert( p-
1989b 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30 20 29 3b 0a  >aColName!=0 );.
1989c 20 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 26 28 70    pColName = &(p
1989d 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69 64 78 2b 76  ->aColName[idx+v
1989e 61 72 2a 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e  ar*p->nResColumn
1989f 5d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ]);.  rc = sqlit
198a0 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
198a1 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c  pColName, zName,
198a2 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38   -1, SQLITE_UTF8
198a3 2c 20 78 44 65 6c 29 3b 0a 20 20 61 73 73 65 72  , xDel);.  asser
198a4 74 28 20 72 63 21 3d 30 20 7c 7c 20 21 7a 4e 61  t( rc!=0 || !zNa
198a5 6d 65 20 7c 7c 20 28 70 43 6f 6c 4e 61 6d 65 2d  me || (pColName-
198a6 3e 66 6c 61 67 73 26 4d 45 4d 5f 54 65 72 6d 29  >flags&MEM_Term)
198a7 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  !=0 );.  return 
198a8 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 72  rc;.}../*.** A r
198a9 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61  ead or write tra
198aa 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6f 72 20  nsaction may or 
198ab 6d 61 79 20 6e 6f 74 20 62 65 20 61 63 74 69 76  may not be activ
198ac 65 20 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61  e on database ha
198ad 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 49 66 20 61  ndle.** db. If a
198ae 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
198af 61 63 74 69 76 65 2c 20 63 6f 6d 6d 69 74 20 69  active, commit i
198b0 74 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61  t. If there is a
198b1 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e 73 61  .** write-transa
198b2 63 74 69 6f 6e 20 73 70 61 6e 6e 69 6e 67 20 6d  ction spanning m
198b3 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74  ore than one dat
198b4 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 69 73  abase file, this
198b5 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 61 6b 65   routine.** take
198b6 73 20 63 61 72 65 20 6f 66 20 74 68 65 20 6d 61  s care of the ma
198b7 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 74 72 69  ster journal tri
198b8 63 6b 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ckery..*/.static
198b9 20 69 6e 74 20 76 64 62 65 43 6f 6d 6d 69 74 28   int vdbeCommit(
198ba 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64 62  sqlite3 *db, Vdb
198bb 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  e *p){.  int i;.
198bc 20 20 69 6e 74 20 6e 54 72 61 6e 73 20 3d 20 30    int nTrans = 0
198bd 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  ;  /* Number of 
198be 64 61 74 61 62 61 73 65 73 20 77 69 74 68 20 61  databases with a
198bf 6e 20 61 63 74 69 76 65 20 77 72 69 74 65 2d 74  n active write-t
198c0 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
198c1 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
198c2 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65 65 64 58 63  OK;.  int needXc
198c3 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a  ommit = 0;..  /*
198c4 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e   Before doing an
198c5 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c  ything else, cal
198c6 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 61  l the xSync() ca
198c7 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20  llback for any. 
198c8 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75   ** virtual modu
198c9 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 65  le tables writte
198ca 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61  n in this transa
198cb 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 20  ction. This has 
198cc 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20  to.  ** be done 
198cd 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e 69  before determini
198ce 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 73  ng whether a mas
198cf 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
198d0 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 72   is .  ** requir
198d1 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 28  ed, as an xSync(
198d2 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61  ) callback may a
198d3 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  dd an attached d
198d4 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20  atabase.  ** to 
198d5 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
198d6 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
198d7 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 2c  ite3VtabSync(db,
198d8 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20   &p->zErrMsg);. 
198d9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
198da 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
198db 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54   rc;.  }..  /* T
198dc 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69  his loop determi
198dd 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 20 63  nes (a) if the c
198de 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c  ommit hook shoul
198df 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64  d be invoked and
198e0 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61  .  ** (b) how ma
198e1 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ny database file
198e2 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 74  s have open writ
198e3 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20  e transactions, 
198e4 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64  not .  ** includ
198e5 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 74  ing the temp dat
198e6 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 6d  abase. (b) is im
198e7 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20  portant because 
198e8 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20  if more than .  
198e9 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  ** one database 
198ea 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 6e  file has an open
198eb 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
198ec 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  on, a master jou
198ed 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69  rnal.  ** file i
198ee 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61  s required for a
198ef 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e  n atomic commit.
198f0 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30  .  */ .  for(i=0
198f1 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
198f2 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a 70  ){ .    Btree *p
198f3 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
198f4 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  pBt;.    if( sql
198f5 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
198f6 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  ns(pBt) ){.     
198f7 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31   needXcommit = 1
198f8 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 31  ;.      if( i!=1
198f9 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20   ) nTrans++;.   
198fa 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
198fb 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 72  there are any wr
198fc 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73  ite-transactions
198fd 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20   at all, invoke 
198fe 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20  the commit hook 
198ff 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f  */.  if( needXco
19900 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d  mmit && db->xCom
19901 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20  mitCallback ){. 
19902 20 20 20 61 73 73 65 72 74 28 20 28 64 62 2d 3e     assert( (db->
19903 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
19904 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 20 29 3b  ommitBusy)==0 );
19905 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  .    db->flags |
19906 3d 20 53 51 4c 49 54 45 5f 43 6f 6d 6d 69 74 42  = SQLITE_CommitB
19907 75 73 79 3b 0a 20 20 20 20 28 76 6f 69 64 29 73  usy;.    (void)s
19908 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
19909 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62  db);.    rc = db
1990a 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63  ->xCommitCallbac
1990b 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67  k(db->pCommitArg
1990c 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c  );.    (void)sql
1990d 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1990e 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  ;.    db->flags 
1990f 26 3d 20 7e 53 51 4c 49 54 45 5f 43 6f 6d 6d 69  &= ~SQLITE_Commi
19910 74 42 75 73 79 3b 0a 20 20 20 20 69 66 28 20 72  tBusy;.    if( r
19911 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  c ){.      retur
19912 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  n SQLITE_CONSTRA
19913 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  INT;.    }.  }..
19914 20 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20    /* The simple 
19915 63 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74  case - no more t
19916 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65  han one database
19917 20 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74   file (not count
19918 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d  ing the.  ** TEM
19919 50 20 64 61 74 61 62 61 73 65 29 20 68 61 73 20  P database) has 
1991a 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63  a transaction ac
1991b 74 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 73  tive.   There is
1991c 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65   no need for the
1991d 0a 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75  .  ** master-jou
1991e 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rnal..  **.  ** 
1991f 49 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  If the return va
19920 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74  lue of sqlite3Bt
19921 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29  reeGetFilename()
19922 20 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74   is a zero lengt
19923 68 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69  h.  ** string, i
19924 74 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e  t means the main
19925 20 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65   database is :me
19926 6d 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20  mory: or a temp 
19927 66 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20  file.  In .  ** 
19928 74 68 61 74 20 63 61 73 65 20 77 65 20 64 6f 20  that case we do 
19929 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d  not support atom
1992a 69 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f  ic multi-file co
1992b 6d 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68  mmits, so use th
1992c 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63  e .  ** simple c
1992d 61 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20  ase then too..  
1992e 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69  */.  if( 0==sqli
1992f 74 65 33 53 74 72 6c 65 6e 33 30 28 73 71 6c 69  te3Strlen30(sqli
19930 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e  te3BtreeGetFilen
19931 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ame(db->aDb[0].p
19932 42 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72 61 6e  Bt)).   || nTran
19933 73 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20 66 6f  s<=1.  ){.    fo
19934 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
19935 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44  E_OK && i<db->nD
19936 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42  b; i++){.      B
19937 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
19938 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
19939 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
1993a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1993b 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
1993c 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20 20  eOne(pBt, 0);.  
1993d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
1993e 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d 69   /* Do the commi
1993f 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64 61  t only if all da
19940 74 61 62 61 73 65 73 20 73 75 63 63 65 73 73 66  tabases successf
19941 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70 68  ully complete ph
19942 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20 49  ase 1. .    ** I
19943 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74 72  f one of the Btr
19944 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
19945 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c 20  () calls fails, 
19946 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 61  this indicates a
19947 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72 6f  n.    ** IO erro
19948 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e 67  r while deleting
19949 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 61   or truncating a
1994a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49   journal file. I
1994b 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a 20  t is unlikely,. 
1994c 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64 20     ** but could 
1994d 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73 20  happen. In this 
1994e 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72 6f  case abandon pro
1994f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75  cessing and retu
19950 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 20  rn the error..  
19951 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
19952 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
19953 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  && i<db->nDb; i+
19954 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20  +){.      Btree 
19955 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *pBt = db->aDb[i
19956 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28  ].pBt;.      if(
19957 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20   pBt ){.        
19958 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
19959 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  eCommitPhaseTwo(
1995a 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  pBt);.      }.  
1995b 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
1995c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1995d 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f     sqlite3VtabCo
1995e 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a  mmit(db);.    }.
1995f 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63 6f    }..  /* The co
19960 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68 65  mplex case - The
19961 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66 69  re is a multi-fi
19962 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  le write-transac
19963 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20 2a  tion active..  *
19964 2a 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20  * This requires 
19965 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
19966 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65 20   file to ensure 
19967 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
19968 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 65  is.  ** committe
19969 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a 2f  d atomicly..  */
1996a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1996b 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65 6c  OMIT_DISKIO.  el
1996c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
1996d 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e  vfs *pVfs = db->
1996e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e 65  pVfs;.    int ne
1996f 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20  edSync = 0;.    
19970 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20  char *zMaster = 
19971 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61 6d  0;   /* File-nam
19972 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65 72  e for the master
19973 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20   journal */.    
19974 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 69  char const *zMai
19975 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42  nFile = sqlite3B
19976 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
19977 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b  db->aDb[0].pBt);
19978 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  .    sqlite3_fil
19979 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b 0a  e *pMaster = 0;.
1997a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d      i64 offset =
1997b 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b   0;.    int res;
1997c 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20  ..    /* Select 
1997d 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
1997e 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20   file name */.  
1997f 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33 32    do {.      u32
19980 20 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 20   iRandom;.      
19981 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
19982 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  , zMaster);.    
19983 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
19984 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61 6e  ness(sizeof(iRan
19985 64 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29 3b  dom), &iRandom);
19986 0a 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d  .      zMaster =
19987 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
19988 64 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c  db, "%s-mj%08X",
19989 20 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52 61 6e   zMainFile, iRan
1998a 64 6f 6d 26 30 78 37 66 66 66 66 66 66 66 29 3b  dom&0x7fffffff);
1998b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73  .      if( !zMas
1998c 74 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ter ){.        r
1998d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1998e 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EM;.      }.    
1998f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
19990 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61  Access(pVfs, zMa
19991 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43  ster, SQLITE_ACC
19992 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73  ESS_EXISTS, &res
19993 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72  );.    }while( r
19994 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
19995 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72  res );.    if( r
19996 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
19997 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68        /* Open th
19998 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
19999 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  . */.      rc = 
1999a 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c  sqlite3OsOpenMal
1999b 6c 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65  loc(pVfs, zMaste
1999c 72 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20  r, &pMaster, .  
1999d 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
1999e 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51  PEN_READWRITE|SQ
1999f 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
199a0 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  |.          SQLI
199a1 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
199a2 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  E|SQLITE_OPEN_MA
199a3 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a  STER_JOURNAL, 0.
199a4 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20        );.    }. 
199a5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
199a6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
199a7 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
199a8 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  zMaster);.      
199a9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
199aa 0a 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20  . .    /* Write 
199ab 74 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68  the name of each
199ac 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
199ad 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  n the transactio
199ae 6e 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20  n into the new. 
199af 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75     ** master jou
199b0 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e  rnal file. If an
199b1 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74   error occurs at
199b2 20 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73   this point clos
199b3 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c  e.    ** and del
199b4 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ete the master j
199b5 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c  ournal file. All
199b6 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20   the individual 
199b7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20  journal files.  
199b8 20 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20    ** still have 
199b9 27 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61  'null' as the ma
199ba 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69  ster journal poi
199bb 6e 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69  nter, so they wi
199bc 6c 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62  ll roll.    ** b
199bd 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c  ack independentl
199be 79 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f  y if a failure o
199bf 63 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ccurs..    */.  
199c0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
199c1 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
199c2 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
199c3 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
199c4 20 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29 20       if( i==1 ) 
199c5 63 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49  continue;   /* I
199c6 67 6e 6f 72 65 20 74 68 65 20 54 45 4d 50 20 64  gnore the TEMP d
199c7 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20  atabase */.     
199c8 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65   if( sqlite3Btre
199c9 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20  eIsInTrans(pBt) 
199ca 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
199cb 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73  const *zFile = s
199cc 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f  qlite3BtreeGetJo
199cd 75 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a  urnalname(pBt);.
199ce 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c          if( zFil
199cf 65 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  e[0]==0 ) contin
199d0 75 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a  ue;  /* Ignore :
199d1 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65  memory: database
199d2 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  s */.        if(
199d3 20 21 6e 65 65 64 53 79 6e 63 20 26 26 20 21 73   !needSync && !s
199d4 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44  qlite3BtreeSyncD
199d5 69 73 61 62 6c 65 64 28 70 42 74 29 20 29 7b 0a  isabled(pBt) ){.
199d6 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79            needSy
199d7 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  nc = 1;.        
199d8 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  }.        rc = s
199d9 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 4d  qlite3OsWrite(pM
199da 61 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 71  aster, zFile, sq
199db 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46  lite3Strlen30(zF
199dc 69 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 29 3b  ile)+1, offset);
199dd 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20  .        offset 
199de 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
199df 33 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20 20 20  30(zFile)+1;.   
199e0 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
199e1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
199e2 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
199e3 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b  seFree(pMaster);
199e4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
199e5 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c  e3OsDelete(pVfs,
199e6 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20   zMaster, 0);.  
199e7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
199e8 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65  bFree(db, zMaste
199e9 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  r);.          re
199ea 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
199eb 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
199ec 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68  ..    /* Sync th
199ed 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
199ee 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 49 4f   file. If the IO
199ef 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 64  CAP_SEQUENTIAL d
199f0 65 76 69 63 65 0a 20 20 20 20 2a 2a 20 66 6c 61  evice.    ** fla
199f1 67 20 69 73 20 73 65 74 20 74 68 69 73 20 69 73  g is set this is
199f2 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 20   not required.. 
199f3 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65     */.    if( ne
199f4 65 64 53 79 6e 63 20 0a 20 20 20 20 20 26 26 20  edSync .     && 
199f5 30 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65 76  0==(sqlite3OsDev
199f6 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
199f7 63 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c 49  cs(pMaster)&SQLI
199f8 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  TE_IOCAP_SEQUENT
199f9 49 41 4c 29 0a 20 20 20 20 20 26 26 20 53 51 4c  IAL).     && SQL
199fa 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
199fb 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 4d 61 73  lite3OsSync(pMas
199fc 74 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43  ter, SQLITE_SYNC
199fd 5f 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20 29 7b  _NORMAL)).    ){
199fe 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
199ff 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65  CloseFree(pMaste
19a00 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r);.      sqlite
19a01 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20  3OsDelete(pVfs, 
19a02 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20  zMaster, 0);.   
19a03 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
19a04 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  (db, zMaster);. 
19a05 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
19a06 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79      }..    /* Sy
19a07 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66 69  nc all the db fi
19a08 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20  les involved in 
19a09 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
19a0a 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20   The same call. 
19a0b 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20 6d     ** sets the m
19a0c 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f  aster journal po
19a0d 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69 6e  inter in each in
19a0e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c  dividual journal
19a0f 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65  . If.    ** an e
19a10 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72 65  rror occurs here
19a11 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20  , do not delete 
19a12 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
19a13 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a  al file..    **.
19a14 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65 72      ** If the er
19a15 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
19a16 67 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  g the first call
19a17 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   to.    ** sqlit
19a18 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
19a19 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74 68  seOne(), then th
19a1a 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20  ere is a chance 
19a1b 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  that the.    ** 
19a1c 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
19a1d 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70 68  ile will be orph
19a1e 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61 6e  aned. But we can
19a1f 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a 20  not delete it,. 
19a20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68     ** in case th
19a21 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
19a22 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20 77   file name was w
19a23 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
19a24 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66  journal.    ** f
19a25 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20 66  ile before the f
19a26 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 2e  ailure occurred.
19a27 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28  .    */.    for(
19a28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
19a29 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b  OK && i<db->nDb;
19a2a 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74   i++){ .      Bt
19a2b 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
19a2c 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20  Db[i].pBt;.     
19a2d 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20   if( pBt ){.    
19a2e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
19a2f 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
19a30 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 72  One(pBt, zMaster
19a31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
19a32 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
19a33 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29  oseFree(pMaster)
19a34 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
19a35 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
19a36 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
19a37 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  b, zMaster);.   
19a38 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
19a39 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65    }..    /* Dele
19a3a 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
19a3b 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73  urnal file. This
19a3c 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61   commits the tra
19a3d 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a  nsaction. After.
19a3e 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69      ** doing thi
19a3f 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  s the directory 
19a40 69 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20  is synced again 
19a41 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76  before any indiv
19a42 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61  idual.    ** tra
19a43 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61  nsaction files a
19a44 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20  re deleted..    
19a45 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  */.    rc = sqli
19a46 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73  te3OsDelete(pVfs
19a47 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a 20  , zMaster, 1);. 
19a48 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
19a49 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  (db, zMaster);. 
19a4a 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a     zMaster = 0;.
19a4b 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
19a4c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
19a4d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c     }..    /* All
19a4e 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 63   files and direc
19a4f 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 65  tories have alre
19a50 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2c  ady been synced,
19a51 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   so the followin
19a52 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74  g.    ** calls t
19a53 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  o sqlite3BtreeCo
19a54 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61  mmitPhaseTwo() a
19a55 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 20  re only closing 
19a56 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a 2a  files and.    **
19a57 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75   deleting or tru
19a58 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 73  ncating journals
19a59 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 67  . If something g
19a5a 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 0a  oes wrong while.
19a5b 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68      ** this is h
19a5c 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e 27  appening we don'
19a5d 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 54  t really care. T
19a5e 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20  he integrity of 
19a5f 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  the.    ** trans
19a60 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64  action is alread
19a61 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 75  y guaranteed, bu
19a62 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 6f  t some stray 'co
19a63 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20  ld' journals.   
19a64 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e 67   ** may be lying
19a65 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e 69   around. Returni
19a66 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  ng an error code
19a67 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 74   won't help matt
19a68 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ers..    */.    
19a69 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65  disable_simulate
19a6a 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20  d_io_errors();. 
19a6b 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42     sqlite3BeginB
19a6c 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
19a6d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
19a6e 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
19a6f 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
19a70 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
19a71 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29  .      if( pBt )
19a72 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
19a73 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
19a74 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20  eTwo(pBt);.     
19a75 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
19a76 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
19a77 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c  loc();.    enabl
19a78 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
19a79 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71  rrors();..    sq
19a7a 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28  lite3VtabCommit(
19a7b 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  db);.  }.#endif.
19a7c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
19a7d 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ./* .** This rou
19a7e 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74  tine checks that
19a7f 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74   the sqlite3.act
19a80 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74  iveVdbeCnt count
19a81 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74   variable.** mat
19a82 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20  ches the number 
19a83 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65  of vdbe's in the
19a84 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56   list sqlite3.pV
19a85 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20  dbe that are.** 
19a86 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65  currently active
19a87 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66  . An assertion f
19a88 61 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20  ails if the two 
19a89 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61  counts do not ma
19a8a 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  tch..** This is 
19a8b 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66  an internal self
19a8c 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74  -check only - it
19a8d 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e   is not an essen
19a8e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a  tial processing.
19a8f 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54  ** step..**.** T
19a90 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  his is a no-op i
19a91 66 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69  f NDEBUG is defi
19a92 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ned..*/.#ifndef 
19a93 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f  NDEBUG.static vo
19a94 69 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64  id checkActiveVd
19a95 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64  beCnt(sqlite3 *d
19a96 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20  b){.  Vdbe *p;. 
19a97 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
19a98 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a  int nWrite = 0;.
19a99 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b    p = db->pVdbe;
19a9a 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20  .  while( p ){. 
19a9b 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d     if( p->magic=
19a9c 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
19a9d 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  && p->pc>=0 ){. 
19a9e 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
19a9f 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c    if( p->readOnl
19aa0 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b  y==0 ) nWrite++;
19aa1 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70  .    }.    p = p
19aa2 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61  ->pNext;.  }.  a
19aa3 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e  ssert( cnt==db->
19aa4 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b  activeVdbeCnt );
19aa5 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74  .  assert( nWrit
19aa6 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65  e==db->writeVdbe
19aa7 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23  Cnt );.}.#else.#
19aa8 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69  define checkActi
19aa9 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e  veVdbeCnt(x).#en
19aaa 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65  dif../*.** For e
19aab 76 65 72 79 20 42 74 72 65 65 20 74 68 61 74 20  very Btree that 
19aac 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  in database conn
19aad 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 20  ection db which 
19aae 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64  .** has been mod
19aaf 69 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f 72  ified, "trip" or
19ab0 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 68   invalidate each
19ab1 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 68   cursor in.** th
19ab2 61 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 68  at Btree might h
19ab3 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  ave been modifie
19ab4 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 75  d so that the cu
19ab5 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 65  rsor.** can neve
19ab6 72 20 62 65 20 75 73 65 64 20 61 67 61 69 6e 2e  r be used again.
19ab7 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77    This happens w
19ab8 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a  hen a rollback.*
19ab9 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 68  ** occurs.  We h
19aba 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c 20  ave to trip all 
19abb 74 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72  the other cursor
19abc 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 6f  s, even.** curso
19abd 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d 73  r from other VMs
19abe 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 61   in different da
19abf 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
19ac0 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6e  ns,.** so that n
19ac1 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 20  one of them try 
19ac2 74 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 20  to use the data 
19ac3 61 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a  at which they.**
19ac4 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 61   were pointing a
19ac5 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 79  nd which now may
19ac6 20 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e 67   have been chang
19ac7 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 65  ed due.** to the
19ac8 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
19ac9 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 61   Remember that a
19aca 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 65   rollback can de
19acb 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d 70  lete tables comp
19acc 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f 72  lete and.** reor
19acd 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 20  der rootpages.  
19ace 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66  So it is not suf
19acf 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f 20  ficient just to 
19ad0 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 74  save.** the stat
19ad1 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e  e of the cursor.
19ad2 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76    We have to inv
19ad3 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73  alidate the curs
19ad4 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74  or.** so that it
19ad5 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61   is never used a
19ad6 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  gain..*/.static 
19ad7 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 43  void invalidateC
19ad8 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64  ursorsOnModified
19ad9 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 2a  Btrees(sqlite3 *
19ada 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  db){.  int i;.  
19adb 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
19adc 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74  Db; i++){.    Bt
19add 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62  ree *p = db->aDb
19ade 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
19adf 20 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 72   p && sqlite3Btr
19ae0 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 29  eeIsInTrans(p) )
19ae1 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  {.      sqlite3B
19ae2 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f  treeTripAllCurso
19ae3 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 4f  rs(p, SQLITE_ABO
19ae4 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  RT);.    }.  }.}
19ae5 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 56  ../*.** If the V
19ae6 64 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68  dbe passed as th
19ae7 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
19ae8 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d   opened a statem
19ae9 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c  ent-transaction,
19aea 0a 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e 6f 77  .** close it now
19aeb 2e 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 20 6d  . Argument eOp m
19aec 75 73 74 20 62 65 20 65 69 74 68 65 72 20 53 41  ust be either SA
19aed 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
19aee 20 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54   or.** SAVEPOINT
19aef 5f 52 45 4c 45 41 53 45 2e 20 49 66 20 69 74 20  _RELEASE. If it 
19af0 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  is SAVEPOINT_ROL
19af1 4c 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 65 20  LBACK, then the 
19af2 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61  statement.** tra
19af3 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
19af4 65 64 20 62 61 63 6b 2e 20 49 66 20 65 4f 70 20  ed back. If eOp 
19af5 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  is SAVEPOINT_REL
19af6 45 41 53 45 2c 20 74 68 65 6e 20 74 68 65 20 0a  EASE, then the .
19af7 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  ** statement tra
19af8 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d  nsaction is comm
19af9 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  tted..**.** If a
19afa 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
19afb 73 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f 45  s, an SQLITE_IOE
19afc 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64  RR_XXX error cod
19afd 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a  e is returned. .
19afe 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c  ** Otherwise SQL
19aff 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54  ITE_OK..*/.SQLIT
19b00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19b01 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74  lite3VdbeCloseSt
19b02 61 74 65 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c  atement(Vdbe *p,
19b03 20 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 71 6c   int eOp){.  sql
19b04 69 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 20 3d  ite3 *const db =
19b05 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63   p->db;.  int rc
19b06 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
19b07 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  if( p->iStatemen
19b08 74 20 26 26 20 64 62 2d 3e 6e 53 74 61 74 65 6d  t && db->nStatem
19b09 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ent ){.    int i
19b0a 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  ;.    const int 
19b0b 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d 3e  iSavepoint = p->
19b0c 69 53 74 61 74 65 6d 65 6e 74 2d 31 3b 0a 0a 20  iStatement-1;.. 
19b0d 20 20 20 61 73 73 65 72 74 28 20 65 4f 70 3d 3d     assert( eOp==
19b0e 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
19b0f 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53 41 56 45 50  CK || eOp==SAVEP
19b10 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a 20  OINT_RELEASE);. 
19b11 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e     assert( db->n
19b12 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20  Statement>0 );. 
19b13 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 53     assert( p->iS
19b14 74 61 74 65 6d 65 6e 74 3d 3d 28 64 62 2d 3e 6e  tatement==(db->n
19b15 53 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e 6e 53  Statement+db->nS
19b16 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 0a 20 20  avepoint) );..  
19b17 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
19b18 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20  >nDb; i++){ .   
19b19 20 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c     int rc2 = SQL
19b1a 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 42 74  ITE_OK;.      Bt
19b1b 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
19b1c 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20  Db[i].pBt;.     
19b1d 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20   if( pBt ){.    
19b1e 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41 56      if( eOp==SAV
19b1f 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
19b20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 32  ){.          rc2
19b21 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53   = sqlite3BtreeS
19b22 61 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53 41  avepoint(pBt, SA
19b23 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
19b24 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20  , iSavepoint);. 
19b25 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
19b26 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45   if( rc2==SQLITE
19b27 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
19b28 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 74   rc2 = sqlite3Bt
19b29 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 74  reeSavepoint(pBt
19b2a 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  , SAVEPOINT_RELE
19b2b 41 53 45 2c 20 69 53 61 76 65 70 6f 69 6e 74 29  ASE, iSavepoint)
19b2c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
19b2d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
19b2e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
19b2f 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20     rc = rc2;.   
19b30 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
19b31 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6e 53 74     }.    db->nSt
19b32 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20 20 20 20 70  atement--;.    p
19b33 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30  ->iStatement = 0
19b34 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
19b35 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53  c;.}../*.** If S
19b36 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
19b37 64 20 74 6f 20 73 75 70 70 6f 72 74 20 73 68 61  d to support sha
19b38 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 61  red-cache mode a
19b39 6e 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73  nd to be threads
19b3a 61 66 65 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75  afe,.** this rou
19b3b 74 69 6e 65 20 6f 62 74 61 69 6e 73 20 74 68 65  tine obtains the
19b3c 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65   mutex associate
19b3d 64 20 77 69 74 68 20 65 61 63 68 20 42 74 53 68  d with each BtSh
19b3e 61 72 65 64 20 73 74 72 75 63 74 75 72 65 0a 2a  ared structure.*
19b3f 2a 20 74 68 61 74 20 6d 61 79 20 62 65 20 61 63  * that may be ac
19b40 63 65 73 73 65 64 20 62 79 20 74 68 65 20 56 4d  cessed by the VM
19b41 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72   passed as an ar
19b42 67 75 6d 65 6e 74 2e 20 49 6e 20 64 6f 69 6e 67  gument. In doing
19b43 20 73 6f 20 69 74 0a 2a 2a 20 73 65 74 73 20 74   so it.** sets t
19b44 68 65 20 42 74 53 68 61 72 65 64 2e 64 62 20 6d  he BtShared.db m
19b45 65 6d 62 65 72 20 6f 66 20 65 61 63 68 20 6f 66  ember of each of
19b46 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74   the BtShared st
19b47 72 75 63 74 75 72 65 73 2c 20 65 6e 73 75 72 69  ructures, ensuri
19b48 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63  ng.** that the c
19b49 6f 72 72 65 63 74 20 62 75 73 79 2d 68 61 6e 64  orrect busy-hand
19b4a 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ler callback is 
19b4b 69 6e 76 6f 6b 65 64 20 69 66 20 72 65 71 75 69  invoked if requi
19b4c 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51  red..**.** If SQ
19b4d 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72 65  Lite is not thre
19b4e 61 64 73 61 66 65 20 62 75 74 20 64 6f 65 73 20  adsafe but does 
19b4f 73 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63  support shared-c
19b50 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 0a  ache mode, then.
19b51 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  ** sqlite3BtreeE
19b52 6e 74 65 72 41 6c 6c 28 29 20 69 73 20 69 6e 76  nterAll() is inv
19b53 6f 6b 65 64 20 74 6f 20 73 65 74 20 74 68 65 20  oked to set the 
19b54 42 74 53 68 61 72 65 64 2e 64 62 20 76 61 72 69  BtShared.db vari
19b55 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 61 6c 6c 20  ables.** of all 
19b56 6f 66 20 42 74 53 68 61 72 65 64 20 73 74 72 75  of BtShared stru
19b57 63 74 75 72 65 73 20 61 63 63 65 73 73 69 62 6c  ctures accessibl
19b58 65 20 76 69 61 20 74 68 65 20 64 61 74 61 62 61  e via the databa
19b59 73 65 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 61 73  se handle .** as
19b5a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
19b5b 65 20 56 4d 2e 20 4f 66 20 63 6f 75 72 73 65 20  e VM. Of course 
19b5c 6f 6e 6c 79 20 61 20 73 75 62 73 65 74 20 6f 66  only a subset of
19b5d 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65   these structure
19b5e 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 61 63 63  s.** will be acc
19b5f 65 73 73 65 64 20 62 79 20 74 68 65 20 56 4d 2c  essed by the VM,
19b60 20 61 6e 64 20 77 65 20 63 6f 75 6c 64 20 75 73   and we could us
19b61 65 20 56 64 62 65 2e 62 74 72 65 65 4d 61 73 6b  e Vdbe.btreeMask
19b62 20 74 6f 20 66 69 67 75 72 65 0a 2a 2a 20 74 68   to figure.** th
19b63 61 74 20 73 75 62 73 65 74 20 6f 75 74 2c 20 62  at subset out, b
19b64 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 61  ut there is no a
19b65 64 76 61 6e 74 61 67 65 20 74 6f 20 64 6f 69 6e  dvantage to doin
19b66 67 20 73 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53  g so..**.** If S
19b67 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72  QLite is not thr
19b68 65 61 64 73 61 66 65 20 61 6e 64 20 64 6f 65 73  eadsafe and does
19b69 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 68 61   not support sha
19b6a 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20  red-cache mode, 
19b6b 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  this.** function
19b6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
19b6d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
19b6e 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
19b6f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19b70 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
19b71 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28  MutexArrayEnter(
19b72 56 64 62 65 20 2a 70 29 7b 0a 23 69 66 20 53 51  Vdbe *p){.#if SQ
19b73 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
19b74 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75    sqlite3BtreeMu
19b75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 26 70  texArrayEnter(&p
19b76 2d 3e 61 4d 75 74 65 78 29 3b 0a 23 65 6c 73 65  ->aMutex);.#else
19b77 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
19b78 6e 74 65 72 41 6c 6c 28 70 2d 3e 64 62 29 3b 0a  nterAll(p->db);.
19b79 23 65 6e 64 69 66 0a 7d 0a 23 65 6e 64 69 66 0a  #endif.}.#endif.
19b7a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
19b7b 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 68  ine is called th
19b7c 65 20 77 68 65 6e 20 61 20 56 44 42 45 20 74 72  e when a VDBE tr
19b7d 69 65 73 20 74 6f 20 68 61 6c 74 2e 20 20 49 66  ies to halt.  If
19b7e 20 74 68 65 20 56 44 42 45 0a 2a 2a 20 68 61 73   the VDBE.** has
19b7f 20 6d 61 64 65 20 63 68 61 6e 67 65 73 20 61 6e   made changes an
19b80 64 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d 6d  d is in autocomm
19b81 69 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20 63 6f  it mode, then co
19b82 6d 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20 63 68  mmit those.** ch
19b83 61 6e 67 65 73 2e 20 20 49 66 20 61 20 72 6f 6c  anges.  If a rol
19b84 6c 62 61 63 6b 20 69 73 20 6e 65 65 64 65 64 2c  lback is needed,
19b85 20 74 68 65 6e 20 64 6f 20 74 68 65 20 72 6f 6c   then do the rol
19b86 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  lback..**.** Thi
19b87 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65  s routine is the
19b88 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76   only way to mov
19b89 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 61  e the state of a
19b8a 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51 4c 49   VM from.** SQLI
19b8b 54 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74 6f 20  TE_MAGIC_RUN to 
19b8c 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c  SQLITE_MAGIC_HAL
19b8d 54 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c 65  T.  It is harmle
19b8e 73 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20 74 68  ss to.** call th
19b8f 69 73 20 6f 6e 20 61 20 56 4d 20 74 68 61 74 20  is on a VM that 
19b90 69 73 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45  is in the SQLITE
19b91 5f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74 61 74  _MAGIC_HALT stat
19b92 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  e..**.** Return 
19b93 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20  an error code.  
19b94 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 6f  If the commit co
19b95 75 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65  uld not complete
19b96 20 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20 6c   because of.** l
19b97 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2c 20  ock contention, 
19b98 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
19b99 53 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 42  SY.  If SQLITE_B
19b9a 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 2c  USY is returned,
19b9b 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 65   it.** means the
19b9c 20 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 20 68   close did not h
19b9d 61 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 73 20  appen and needs 
19b9e 74 6f 20 62 65 20 72 65 70 65 61 74 65 64 2e 0a  to be repeated..
19b9f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19ba0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19ba1 65 48 61 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a  eHalt(Vdbe *p){.
19ba2 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
19ba3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ba4 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 73 74 6f    /* Used to sto
19ba5 72 65 20 74 72 61 6e 73 69 65 6e 74 20 72 65 74  re transient ret
19ba6 75 72 6e 20 63 6f 64 65 73 20 2a 2f 0a 20 20 73  urn codes */.  s
19ba7 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
19ba8 64 62 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66  db;..  /* This f
19ba9 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  unction contains
19baa 20 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74 20   the logic that 
19bab 64 65 74 65 72 6d 69 6e 65 73 20 69 66 20 61 20  determines if a 
19bac 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20 2a  statement or.  *
19bad 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69  * transaction wi
19bae 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 20  ll be committed 
19baf 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  or rolled back a
19bb0 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68  s a result of th
19bb1 65 0a 20 20 2a 2a 20 65 78 65 63 75 74 69 6f 6e  e.  ** execution
19bb2 20 6f 66 20 74 68 69 73 20 76 69 72 74 75 61 6c   of this virtual
19bb3 20 6d 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a 0a   machine. .  **.
19bb4 20 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74    ** If any of t
19bb5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72  he following err
19bb6 6f 72 73 20 6f 63 63 75 72 3a 0a 20 20 2a 2a 0a  ors occur:.  **.
19bb7 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f    **     SQLITE_
19bb8 4e 4f 4d 45 4d 0a 20 20 2a 2a 20 20 20 20 20 53  NOMEM.  **     S
19bb9 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 2a 2a  QLITE_IOERR.  **
19bba 20 20 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c       SQLITE_FULL
19bbb 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  .  **     SQLITE
19bbc 5f 49 4e 54 45 52 52 55 50 54 0a 20 20 2a 2a 0a  _INTERRUPT.  **.
19bbd 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69 6e    ** Then the in
19bbe 74 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69 67  ternal cache mig
19bbf 68 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65 66  ht have been lef
19bc0 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73  t in an inconsis
19bc1 74 65 6e 74 0a 20 20 2a 2a 20 73 74 61 74 65 2e  tent.  ** state.
19bc2 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72 6f 6c    We need to rol
19bc3 6c 62 61 63 6b 20 74 68 65 20 73 74 61 74 65 6d  lback the statem
19bc4 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  ent transaction,
19bc5 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 2a   if there is.  *
19bc6 2a 20 6f 6e 65 2c 20 6f 72 20 74 68 65 20 63 6f  * one, or the co
19bc7 6d 70 6c 65 74 65 20 74 72 61 6e 73 61 63 74 69  mplete transacti
19bc8 6f 6e 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  on if there is n
19bc9 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  o statement tran
19bca 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a 20  saction..  */.. 
19bcb 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c   if( p->db->mall
19bcc 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
19bcd 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  p->rc = SQLITE_N
19bce 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 63 6c 6f 73  OMEM;.  }.  clos
19bcf 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65 70  eAllCursorsExcep
19bd0 74 41 63 74 69 76 65 56 74 61 62 73 28 70 29 3b  tActiveVtabs(p);
19bd1 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21  .  if( p->magic!
19bd2 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
19bd3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
19bd4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 63  LITE_OK;.  }.  c
19bd5 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e  heckActiveVdbeCn
19bd6 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 20  t(db);..  /* No 
19bd7 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61  commit or rollba
19bd8 63 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68 65  ck needed if the
19bd9 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20 73   program never s
19bda 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28 20  tarted */.  if( 
19bdb 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20  p->pc>=0 ){.    
19bdc 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50 72  int mrc;   /* Pr
19bdd 69 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64 65  imary error code
19bde 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a 20   from p->rc */. 
19bdf 20 20 20 69 6e 74 20 65 53 74 61 74 65 6d 65 6e     int eStatemen
19be0 74 4f 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  tOp = 0;.    int
19be1 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b   isSpecialError;
19be2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
19be3 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 20  et to true if a 
19be4 27 73 70 65 63 69 61 6c 27 20 65 72 72 6f 72 20  'special' error 
19be5 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20  */..    /* Lock 
19be6 61 6c 6c 20 62 74 72 65 65 73 20 75 73 65 64 20  all btrees used 
19be7 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  by the statement
19be8 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
19be9 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  dbeMutexArrayEnt
19bea 65 72 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  er(p);..    /* C
19beb 68 65 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20  heck for one of 
19bec 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f  the special erro
19bed 72 73 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20  rs */.    mrc = 
19bee 70 2d 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20  p->rc & 0xff;.  
19bef 20 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72    isSpecialError
19bf0 20 3d 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   = mrc==SQLITE_N
19bf1 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c  OMEM || mrc==SQL
19bf2 49 54 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20  ITE_IOERR.      
19bf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
19bf4 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e  | mrc==SQLITE_IN
19bf5 54 45 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d  TERRUPT || mrc==
19bf6 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20  SQLITE_FULL;.   
19bf7 20 69 66 28 20 69 73 53 70 65 63 69 61 6c 45 72   if( isSpecialEr
19bf8 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ror ){.      /* 
19bf9 49 66 20 74 68 65 20 71 75 65 72 79 20 77 61 73  If the query was
19bfa 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e   read-only, we n
19bfb 65 65 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61  eed do no rollba
19bfc 63 6b 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72  ck at all. Other
19bfd 77 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70  wise,.      ** p
19bfe 72 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 20  roceed with the 
19bff 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67  special handling
19c00 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
19c01 20 69 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c   if( !p->readOnl
19c02 79 20 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45  y || mrc!=SQLITE
19c03 5f 49 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20  _INTERRUPT ){.  
19c04 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d        if( p->rc=
19c05 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c  =SQLITE_IOERR_BL
19c06 4f 43 4b 45 44 20 26 26 20 70 2d 3e 75 73 65 73  OCKED && p->uses
19c07 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  StmtJournal ){. 
19c08 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d           eStatem
19c09 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e  entOp = SAVEPOIN
19c0a 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20  T_ROLLBACK;.    
19c0b 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51        p->rc = SQ
19c0c 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20  LITE_BUSY;.     
19c0d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6d 72     }else if( (mr
19c0e 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
19c0f 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46  || mrc==SQLITE_F
19c10 55 4c 4c 29 0a 20 20 20 20 20 20 20 20 20 20 20  ULL).           
19c11 20 20 20 20 20 20 20 20 26 26 20 70 2d 3e 75 73          && p->us
19c12 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b  esStmtJournal ){
19c13 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74  .          eStat
19c14 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f  ementOp = SAVEPO
19c15 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20  INT_ROLLBACK;.  
19c16 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
19c17 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65         /* We are
19c18 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20   forced to roll 
19c19 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20  back the active 
19c1a 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66  transaction. Bef
19c1b 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20  ore doing.      
19c1c 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74      ** so, abort
19c1d 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65   any other state
19c1e 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c  ments this handl
19c1f 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20  e currently has 
19c20 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20  active..        
19c21 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69    */.          i
19c22 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73  nvalidateCursors
19c23 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73  OnModifiedBtrees
19c24 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (db);.          
19c25 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41  sqlite3RollbackA
19c26 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ll(db);.        
19c27 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61    sqlite3CloseSa
19c28 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20  vepoints(db);.  
19c29 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f          db->auto
19c2a 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  Commit = 1;.    
19c2b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
19c2c 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66    }.  .    /* If
19c2d 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74   the auto-commit
19c2e 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64   flag is set and
19c2f 20 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c   this is the onl
19c30 79 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20  y active writer 
19c31 0a 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e  .    ** VM, then
19c32 20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20   we do either a 
19c33 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61  commit or rollba
19c34 63 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ck of the curren
19c35 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a  t transaction. .
19c36 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f      **.    ** No
19c37 74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61  te: This block a
19c38 6c 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20  lso runs if one 
19c39 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65  of the special e
19c3a 72 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20  rrors handled . 
19c3b 20 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20     ** above has 
19c3c 6f 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a  occurred. .    *
19c3d 2f 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74  /.    if( !sqlit
19c3e 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29  e3VtabInSync(db)
19c3f 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75   .     && db->au
19c40 74 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26  toCommit .     &
19c41 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  & db->writeVdbeC
19c42 6e 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79  nt==(p->readOnly
19c43 3d 3d 30 29 20 0a 20 20 20 20 20 26 26 20 28 64  ==0) .     && (d
19c44 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
19c45 45 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30  E_CommitBusy)==0
19c46 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66  .    ){.      if
19c47 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
19c48 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41  OK || (p->errorA
19c49 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26  ction==OE_Fail &
19c4a 26 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f  & !isSpecialErro
19c4b 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  r) ){.        /*
19c4c 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74   The auto-commit
19c4d 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61   flag is true, a
19c4e 6e 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  nd the vdbe prog
19c4f 72 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20  ram was .       
19c50 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f   ** successful o
19c51 72 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49  r hit an 'OR FAI
19c52 4c 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54  L' constraint. T
19c53 68 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d  his means a comm
19c54 69 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  it .        ** i
19c55 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  s required..    
19c56 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72      */.        r
19c57 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64  c = vdbeCommit(d
19c58 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69  b, p);.        i
19c59 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  f( rc==SQLITE_BU
19c5a 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  SY ){.          
19c5b 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
19c5c 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e  xArrayLeave(&p->
19c5d 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20  aMutex);.       
19c5e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
19c5f 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d  _BUSY;.        }
19c60 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c  else if( rc!=SQL
19c61 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
19c62 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a      p->rc = rc;.
19c63 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
19c64 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29  3RollbackAll(db)
19c65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
19c66 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
19c67 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c  e3CommitInternal
19c68 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20  Changes(db);.   
19c69 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
19c6a 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
19c6b 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64  te3RollbackAll(d
19c6c 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  b);.      }.    
19c6d 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74    db->nStatement
19c6e 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
19c6f 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70  if( eStatementOp
19c70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
19c71 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
19c72 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74  K || p->errorAct
19c73 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a  ion==OE_Fail ){.
19c74 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65          eStateme
19c75 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54  ntOp = SAVEPOINT
19c76 5f 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20  _RELEASE;.      
19c77 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72  }else if( p->err
19c78 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f  orAction==OE_Abo
19c79 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53  rt ){.        eS
19c7a 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56  tatementOp = SAV
19c7b 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b  EPOINT_ROLLBACK;
19c7c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19c7d 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65        invalidate
19c7e 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65  CursorsOnModifie
19c7f 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20  dBtrees(db);.   
19c80 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c       sqlite3Roll
19c81 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20  backAll(db);.   
19c82 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73       sqlite3Clos
19c83 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b  eSavepoints(db);
19c84 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74  .        db->aut
19c85 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  oCommit = 1;.   
19c86 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
19c87 20 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65    /* If eStateme
19c88 6e 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  ntOp is non-zero
19c89 2c 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65  , then a stateme
19c8a 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e  nt transaction n
19c8b 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62  eeds to.    ** b
19c8c 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72  e committed or r
19c8d 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c  olled back. Call
19c8e 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73   sqlite3VdbeClos
19c8f 65 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a  eStatement() to.
19c90 20 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66      ** do so. If
19c91 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
19c92 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
19c93 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  , and the curren
19c94 74 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20  t statement.    
19c95 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  ** error code is
19c96 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51   SQLITE_OK or SQ
19c97 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c  LITE_CONSTRAINT,
19c98 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72   then set the er
19c99 72 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20  ror.    ** code 
19c9a 74 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65  to the new value
19c9b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
19c9c 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b   eStatementOp ){
19c9d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
19c9e 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74  te3VdbeCloseStat
19c9f 65 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d  ement(p, eStatem
19ca0 65 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66  entOp);.      if
19ca1 28 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d  ( rc && (p->rc==
19ca2 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e  SQLITE_OK || p->
19ca3 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54  rc==SQLITE_CONST
19ca4 52 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20  RAINT) ){.      
19ca5 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
19ca6 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
19ca7 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
19ca8 73 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  sg);.        p->
19ca9 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
19caa 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
19cab 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73    /* If this was
19cac 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41   an INSERT, UPDA
19cad 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64  TE or DELETE and
19cae 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72   no statement tr
19caf 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a  ansaction.    **
19cb0 20 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64   has been rolled
19cb1 20 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68   back, update th
19cb2 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
19cb3 63 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75  ction change-cou
19cb4 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  nter. .    */.  
19cb5 20 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43    if( p->changeC
19cb6 6e 74 4f 6e 20 26 26 20 70 2d 3e 70 63 3e 3d 30  ntOn && p->pc>=0
19cb7 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53   ){.      if( eS
19cb8 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45  tatementOp!=SAVE
19cb9 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29  POINT_ROLLBACK )
19cba 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
19cbb 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
19cbc 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b  db, p->nChange);
19cbd 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19cbe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19cbf 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20  eSetChanges(db, 
19cc0 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
19cc1 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30    p->nChange = 0
19cc2 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
19cc3 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 6f  * Rollback or co
19cc4 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 20  mmit any schema 
19cc5 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 63  changes that occ
19cc6 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66  urred. */.    if
19cc7 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  ( p->rc!=SQLITE_
19cc8 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26  OK && db->flags&
19cc9 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
19cca 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 71  nges ){.      sq
19ccb 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
19ccc 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b  alSchema(db, 0);
19ccd 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  .      db->flags
19cce 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c 20   = (db->flags | 
19ccf 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
19cd0 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  nges);.    }..  
19cd1 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65    /* Release the
19cd2 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 71   locks */.    sq
19cd3 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
19cd4 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d  rrayLeave(&p->aM
19cd5 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  utex);.  }..  /*
19cd6 20 57 65 20 68 61 76 65 20 73 75 63 63 65 73 73   We have success
19cd7 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e 64  fully halted and
19cd8 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e 20   closed the VM. 
19cd9 20 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 63   Record this fac
19cda 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  t. */.  if( p->p
19cdb 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e  c>=0 ){.    db->
19cdc 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d 3b  activeVdbeCnt--;
19cdd 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61  .    if( !p->rea
19cde 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 64  dOnly ){.      d
19cdf 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2d  b->writeVdbeCnt-
19ce0 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  -;.    }.    ass
19ce1 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56  ert( db->activeV
19ce2 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 74  dbeCnt>=db->writ
19ce3 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d 0a  eVdbeCnt );.  }.
19ce4 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42    p->magic = VDB
19ce5 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 20  E_MAGIC_HALT;.  
19ce6 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43  checkActiveVdbeC
19ce7 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 2d  nt(db);.  if( p-
19ce8 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
19ce9 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  d ){.    p->rc =
19cea 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
19ceb 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
19cec 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67  auto-commit flag
19ced 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 2c   is set to true,
19cee 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 20   then any locks 
19cef 74 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a 20  that were held. 
19cf0 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f   ** by connectio
19cf1 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 65  n db have now be
19cf2 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 6c  en released. Cal
19cf3 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74  l sqlite3Connect
19cf4 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a 20  ionUnlocked() . 
19cf5 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 6e   ** to invoke an
19cf6 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f 63  y required unloc
19cf7 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
19cf8 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ks..  */.  if( d
19cf9 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
19cfa 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e  .    sqlite3Conn
19cfb 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64  ectionUnlocked(d
19cfc 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  b);.  }..  asser
19cfd 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  t( db->activeVdb
19cfe 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75  eCnt>0 || db->au
19cff 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64  toCommit==0 || d
19d00 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30  b->nStatement==0
19d01 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   );.  return SQL
19d02 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ITE_OK;.}.../*.*
19d03 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c 64  * Each VDBE hold
19d04 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  s the result of 
19d05 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
19d06 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63  sqlite3_step() c
19d07 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e  all.** in p->rc.
19d08 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73    This routine s
19d09 65 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 20  ets that result 
19d0a 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4f  back to SQLITE_O
19d0b 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  K..*/.SQLITE_PRI
19d0c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
19d0d 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65  3VdbeResetStepRe
19d0e 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20  sult(Vdbe *p){. 
19d0f 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
19d10 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  OK;.}../*.** Cle
19d11 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 74  an up a VDBE aft
19d12 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 74  er execution but
19d13 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74   do not delete t
19d14 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 74  he VDBE just yet
19d15 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 65  ..** Write any e
19d16 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e  rror messages in
19d17 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52  to *pzErrMsg.  R
19d18 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74  eturn the result
19d19 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 74   code..**.** Aft
19d1a 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  er this routine 
19d1b 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45  is run, the VDBE
19d1c 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79   should be ready
19d1d 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0a   to be executed.
19d1e 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20  ** again..**.** 
19d1f 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 6e  To look at it an
19d20 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 20  other way, this 
19d21 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 74  routine resets t
19d22 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 0a  he state of the.
19d23 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  ** virtual machi
19d24 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47  ne from VDBE_MAG
19d25 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f 4d  IC_RUN or VDBE_M
19d26 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 74  AGIC_HALT back t
19d27 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f  o.** VDBE_MAGIC_
19d28 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  INIT..*/.SQLITE_
19d29 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19d2a 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62  te3VdbeReset(Vdb
19d2b 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  e *p){.  sqlite3
19d2c 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3e   *db;.  db = p->
19d2d 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  db;..  /* If the
19d2e 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e 20   VM did not run 
19d2f 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72  to completion or
19d30 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 72   if it encounter
19d31 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f 72  ed an.  ** error
19d32 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20  , then it might 
19d33 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 61  not have been ha
19d34 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 20  lted properly.  
19d35 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 20  So halt.  ** it 
19d36 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69  now..  */.  (voi
19d37 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
19d38 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  n(db);.  sqlite3
19d39 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 28  VdbeHalt(p);.  (
19d3a 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
19d3b 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a  tyOff(db);..  /*
19d3c 20 49 66 20 74 68 65 20 56 44 42 45 20 68 61 73   If the VDBE has
19d3d 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 72   be run even par
19d3e 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61  tially, then tra
19d3f 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 20  nsfer the error 
19d40 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72  code.  ** and er
19d41 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d  ror message from
19d42 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 74   the VDBE into t
19d43 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
19d44 20 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 74   structure.  But
19d45 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 42  .  ** if the VDB
19d46 45 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20  E has just been 
19d47 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 68  set to run but h
19d48 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20  as not actually 
19d49 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 2a  executed any.  *
19d4a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 79  * instructions y
19d4b 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d 61  et, leave the ma
19d4c 69 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 6f  in database erro
19d4d 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e  r information un
19d4e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20  changed..  */.  
19d4f 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a  if( p->pc>=0 ){.
19d50 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 4d      if( p->zErrM
19d51 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  sg ){.      sqli
19d52 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
19d53 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71  lloc();.      sq
19d54 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
19d55 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d 3e  (db->pErr,-1,p->
19d56 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f 55  zErrMsg,SQLITE_U
19d57 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53  TF8,SQLITE_TRANS
19d58 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 6c  IENT);.      sql
19d59 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
19d5a 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 2d  loc();.      db-
19d5b 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63  >errCode = p->rc
19d5c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
19d5d 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
19d5e 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d 3e  rMsg);.      p->
19d5f 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
19d60 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63   }else if( p->rc
19d61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
19d62 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63  3Error(db, p->rc
19d63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
19d64 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
19d65 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  ror(db, SQLITE_O
19d66 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  K, 0);.    }.  }
19d67 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 26  else if( p->rc &
19d68 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a  & p->expired ){.
19d69 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 72      /* The expir
19d6a 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 20  ed flag was set 
19d6b 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 6f  on the VDBE befo
19d6c 72 65 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  re the first cal
19d6d 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69  l.    ** to sqli
19d6e 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 20  te3_step(). For 
19d6f 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 6e  consistency (sin
19d70 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  ce sqlite3_step(
19d71 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 6c  ) was.    ** cal
19d72 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 61  led), set the da
19d73 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 20  tabase error in 
19d74 74 68 69 73 20 63 61 73 65 20 61 73 20 77 65 6c  this case as wel
19d75 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  l..    */.    sq
19d76 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70  lite3Error(db, p
19d77 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71  ->rc, 0);.    sq
19d78 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
19d79 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 70  (db->pErr, -1, p
19d7a 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 54  ->zErrMsg, SQLIT
19d7b 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54  E_UTF8, SQLITE_T
19d7c 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73  RANSIENT);.    s
19d7d 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
19d7e 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
19d7f 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30    p->zErrMsg = 0
19d80 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c  ;.  }..  /* Recl
19d81 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75  aim all memory u
19d82 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45 0a  sed by the VDBE.
19d83 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 70    */.  Cleanup(p
19d84 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 72  );..  /* Save pr
19d85 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 74  ofiling informat
19d86 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 44  ion from this VD
19d87 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 66  BE run..  */.#if
19d88 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45  def VDBE_PROFILE
19d89 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a 6f  .  {.    FILE *o
19d8a 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 65  ut = fopen("vdbe
19d8b 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 22  _profile.out", "
19d8c 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74  a");.    if( out
19d8d 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
19d8e 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
19d8f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 20  ut, "---- ");.  
19d90 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
19d91 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nOp; i++){.   
19d92 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
19d93 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f 70  , "%02x", p->aOp
19d94 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 20  [i].opcode);.   
19d95 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 6e     }.      fprin
19d96 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20  tf(out, "\n");. 
19d97 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
19d98 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20  p->nOp; i++){.  
19d99 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
19d9a 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 25  t, "%6d %10lld %
19d9b 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 20  8lld ",.        
19d9c 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74     p->aOp[i].cnt
19d9d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ,.           p->
19d9e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a 20  aOp[i].cycles,. 
19d9f 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70            p->aOp
19da0 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e 61  [i].cnt>0 ? p->a
19da1 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d 3e  Op[i].cycles/p->
19da2 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a 20  aOp[i].cnt : 0. 
19da3 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
19da4 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
19da5 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 2d  ntOp(out, i, &p-
19da6 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 20  >aOp[i]);.      
19da7 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 6f  }.      fclose(o
19da8 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  ut);.    }.  }.#
19da9 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 63  endif.  p->magic
19daa 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e   = VDBE_MAGIC_IN
19dab 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  IT;.  return p->
19dac 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b  rc & db->errMask
19dad 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  ;.}. ./*.** Clea
19dae 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 20  n up and delete 
19daf 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65  a VDBE after exe
19db0 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20  cution.  Return 
19db1 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68  an integer which
19db2 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c   is.** the resul
19db3 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 61  t code.  Write a
19db4 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ny error message
19db5 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72   text into *pzEr
19db6 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rMsg..*/.SQLITE_
19db7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19db8 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
19db9 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
19dba 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
19dbb 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d    if( p->magic==
19dbc 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c  VDBE_MAGIC_RUN |
19dbd 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  | p->magic==VDBE
19dbe 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20  _MAGIC_HALT ){. 
19dbf 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
19dc0 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 20  dbeReset(p);.   
19dc1 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20 70   assert( (rc & p
19dc2 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d  ->db->errMask)==
19dc3 72 63 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rc );.  }else if
19dc4 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45  ( p->magic!=VDBE
19dc5 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 7b 0a 20  _MAGIC_INIT ){. 
19dc6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
19dc7 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73  _MISUSE;.  }.  s
19dc8 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
19dc9 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
19dca 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  ;.}../*.** Call 
19dcb 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  the destructor f
19dcc 6f 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20  or each auxdata 
19dcd 65 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75  entry in pVdbeFu
19dce 6e 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20  nc for which.** 
19dcf 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
19dd0 67 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73  g bit in mask is
19dd1 20 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61   clear.  Auxdata
19dd2 20 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20   entries beyond 
19dd3 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73  31.** are always
19dd4 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20   destroyed.  To 
19dd5 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64  destroy all auxd
19dd6 61 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c  ata entries, cal
19dd7 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  l this.** routin
19dd8 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a  e with mask==0..
19dd9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19dda 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
19ddb 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28  beDeleteAuxData(
19ddc 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46  VdbeFunc *pVdbeF
19ddd 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a  unc, int mask){.
19dde 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
19ddf 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d  =0; i<pVdbeFunc-
19de0 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nAux; i++){.   
19de1 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20   struct AuxData 
19de2 2a 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75  *pAux = &pVdbeFu
19de3 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20  nc->apAux[i];.  
19de4 20 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21    if( (i>31 || !
19de5 28 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c  (mask&(((u32)1)<
19de6 3c 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70  <i))) && pAux->p
19de7 41 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Aux ){.      if(
19de8 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29   pAux->xDelete )
19de9 7b 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e  {.        pAux->
19dea 78 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41  xDelete(pAux->pA
19deb 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ux);.      }.   
19dec 20 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20     pAux->pAux = 
19ded 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
19dee 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20  /*.** Delete an 
19def 65 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a  entire VDBE..*/.
19df0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19df1 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
19df2 65 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a  elete(Vdbe *p){.
19df3 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74    int i;.  sqlit
19df4 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70  e3 *db;..  if( p
19df5 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
19df6 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66  db = p->db;.  if
19df7 28 20 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20  ( p->pPrev ){.  
19df8 20 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78    p->pPrev->pNex
19df9 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  t = p->pNext;.  
19dfa 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
19dfb 74 28 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20  t( db->pVdbe==p 
19dfc 29 3b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65  );.    db->pVdbe
19dfd 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d   = p->pNext;.  }
19dfe 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20  .  if( p->pNext 
19dff 29 7b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d  ){.    p->pNext-
19e00 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65  >pPrev = p->pPre
19e01 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  v;.  }.  if( p->
19e02 61 4f 70 20 29 7b 0a 20 20 20 20 4f 70 20 2a 70  aOp ){.    Op *p
19e03 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20  Op = p->aOp;.   
19e04 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
19e05 4f 70 3b 20 69 2b 2b 2c 20 70 4f 70 2b 2b 29 7b  Op; i++, pOp++){
19e06 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62  .      freeP4(db
19e07 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70  , pOp->p4type, p
19e08 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65  Op->p4.p);.#ifde
19e09 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
19e0a 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
19e0b 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d  ee(db, pOp->zCom
19e0c 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20  ment);.#endif   
19e0d 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72    .    }.  }.  r
19e0e 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70  eleaseMemArray(p
19e0f 2d 3e 61 56 61 72 2c 20 70 2d 3e 6e 56 61 72 29  ->aVar, p->nVar)
19e10 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
19e11 65 28 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29  e(db, p->aLabel)
19e12 3b 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72  ;.  releaseMemAr
19e13 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c  ray(p->aColName,
19e14 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43   p->nResColumn*C
19e15 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c  OLNAME_N);.  sql
19e16 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
19e17 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73  ->aColName);.  s
19e18 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
19e19 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e   p->zSql);.  p->
19e1a 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47  magic = VDBE_MAG
19e1b 49 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74  IC_DEAD;.  sqlit
19e1c 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
19e1d 61 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  aOp);.  sqlite3D
19e1e 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72  bFree(db, p->pFr
19e1f 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  ee);.  sqlite3Db
19e20 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a  Free(db, p);.}..
19e21 2f 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54  /*.** If a MoveT
19e22 6f 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70  o operation is p
19e23 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 67 69  ending on the gi
19e24 76 65 6e 20 63 75 72 73 6f 72 2c 20 74 68 65 6e  ven cursor, then
19e25 20 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65   do that.** Move
19e26 54 6f 20 6e 6f 77 2e 20 20 52 65 74 75 72 6e 20  To now.  Return 
19e27 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20  an error code.  
19e28 49 66 20 6e 6f 20 4d 6f 76 65 54 6f 20 69 73 20  If no MoveTo is 
19e29 70 65 6e 64 69 6e 67 2c 20 74 68 69 73 0a 2a 2a  pending, this.**
19e2a 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f   routine does no
19e2b 74 68 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e  thing and return
19e2c 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a  s SQLITE_OK..*/.
19e2d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19e2e 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  nt sqlite3VdbeCu
19e2f 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43  rsorMoveto(VdbeC
19e30 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28  ursor *p){.  if(
19e31 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65   p->deferredMove
19e32 74 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  to ){.    int re
19e33 73 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 53 51  s, rc;.#ifdef SQ
19e34 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 65 78  LITE_TEST.    ex
19e35 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
19e36 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23  _search_count;.#
19e37 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74  endif.    assert
19e38 28 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a  ( p->isTable );.
19e39 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
19e3a 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
19e3b 6b 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20  ked(p->pCursor, 
19e3c 30 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67  0, p->movetoTarg
19e3d 65 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20  et, 0, &res);.  
19e3e 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
19e3f 6e 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73  n rc;.    p->las
19e40 74 52 6f 77 69 64 20 3d 20 6b 65 79 54 6f 49 6e  tRowid = keyToIn
19e41 74 28 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65  t(p->movetoTarge
19e42 74 29 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64  t);.    p->rowid
19e43 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53  IsValid = ALWAYS
19e44 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20  (res==0) ?1:0;. 
19e45 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 65 73     if( NEVER(res
19e46 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  <0) ){.      rc 
19e47 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  = sqlite3BtreeNe
19e48 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26  xt(p->pCursor, &
19e49 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20  res);.      if( 
19e4a 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
19e4b 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c      }.#ifdef SQL
19e4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c  ITE_TEST.    sql
19e4d 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e  ite3_search_coun
19e4e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  t++;.#endif.    
19e4f 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  p->deferredMovet
19e50 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61  o = 0;.    p->ca
19e51 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
19e52 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65  E_STALE;.  }else
19e53 20 69 66 28 20 70 2d 3e 70 43 75 72 73 6f 72 20   if( p->pCursor 
19e54 29 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 4d 6f  ){.    int hasMo
19e55 76 65 64 3b 0a 20 20 20 20 69 6e 74 20 72 63 20  ved;.    int rc 
19e56 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  = sqlite3BtreeCu
19e57 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 70 2d 3e  rsorHasMoved(p->
19e58 70 43 75 72 73 6f 72 2c 20 26 68 61 73 4d 6f 76  pCursor, &hasMov
19e59 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  ed);.    if( rc 
19e5a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
19e5b 20 69 66 28 20 68 61 73 4d 6f 76 65 64 20 29 7b   if( hasMoved ){
19e5c 0a 20 20 20 20 20 20 70 2d 3e 63 61 63 68 65 53  .      p->cacheS
19e5d 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
19e5e 41 4c 45 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 75  ALE;.      p->nu
19e5f 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d  llRow = 1;.    }
19e60 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
19e61 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
19e62 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
19e63 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a  functions:.**.**
19e64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
19e65 61 6c 54 79 70 65 28 29 0a 2a 2a 20 73 71 6c 69  alType().** sqli
19e66 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
19e67 65 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65  eLen().** sqlite
19e68 33 56 64 62 65 53 65 72 69 61 6c 4c 65 6e 28 29  3VdbeSerialLen()
19e69 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53  .** sqlite3VdbeS
19e6a 65 72 69 61 6c 50 75 74 28 29 0a 2a 2a 20 73 71  erialPut().** sq
19e6b 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47  lite3VdbeSerialG
19e6c 65 74 28 29 0a 2a 2a 0a 2a 2a 20 65 6e 63 61 70  et().**.** encap
19e6d 73 75 6c 61 74 65 20 74 68 65 20 63 6f 64 65 20  sulate the code 
19e6e 74 68 61 74 20 73 65 72 69 61 6c 69 7a 65 73 20  that serializes 
19e6f 76 61 6c 75 65 73 20 66 6f 72 20 73 74 6f 72 61  values for stora
19e70 67 65 20 69 6e 20 53 51 4c 69 74 65 0a 2a 2a 20  ge in SQLite.** 
19e71 64 61 74 61 20 61 6e 64 20 69 6e 64 65 78 20 72  data and index r
19e72 65 63 6f 72 64 73 2e 20 45 61 63 68 20 73 65 72  ecords. Each ser
19e73 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 20 63 6f  ialized value co
19e74 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20 27  nsists of a.** '
19e75 73 65 72 69 61 6c 2d 74 79 70 65 27 20 61 6e 64  serial-type' and
19e76 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 2e   a blob of data.
19e77 20 54 68 65 20 73 65 72 69 61 6c 20 74 79 70 65   The serial type
19e78 20 69 73 20 61 6e 20 38 2d 62 79 74 65 20 75 6e   is an 8-byte un
19e79 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65  signed.** intege
19e7a 72 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 76  r, stored as a v
19e7b 61 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  arint..**.** In 
19e7c 61 6e 20 53 51 4c 69 74 65 20 69 6e 64 65 78 20  an SQLite index 
19e7d 72 65 63 6f 72 64 2c 20 74 68 65 20 73 65 72 69  record, the seri
19e7e 61 6c 20 74 79 70 65 20 69 73 20 73 74 6f 72 65  al type is store
19e7f 64 20 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72  d directly befor
19e80 65 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 6f 66  e.** the blob of
19e81 20 64 61 74 61 20 74 68 61 74 20 69 74 20 63 6f   data that it co
19e82 72 72 65 73 70 6f 6e 64 73 20 74 6f 2e 20 49 6e  rresponds to. In
19e83 20 61 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2c   a table record,
19e84 20 61 6c 6c 20 73 65 72 69 61 6c 0a 2a 2a 20 74   all serial.** t
19e85 79 70 65 73 20 61 72 65 20 73 74 6f 72 65 64 20  ypes are stored 
19e86 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
19e87 74 68 65 20 72 65 63 6f 72 64 2c 20 61 6e 64 20  the record, and 
19e88 74 68 65 20 62 6c 6f 62 73 20 6f 66 20 64 61 74  the blobs of dat
19e89 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 2e  a at.** the end.
19e8a 20 48 65 6e 63 65 20 74 68 65 73 65 20 66 75 6e   Hence these fun
19e8b 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 20 74 68 65  ctions allow the
19e8c 20 63 61 6c 6c 65 72 20 74 6f 20 68 61 6e 64 6c   caller to handl
19e8d 65 20 74 68 65 0a 2a 2a 20 73 65 72 69 61 6c 2d  e the.** serial-
19e8e 74 79 70 65 20 61 6e 64 20 64 61 74 61 20 62 6c  type and data bl
19e8f 6f 62 20 73 65 70 65 72 61 74 65 6c 79 2e 0a 2a  ob seperately..*
19e90 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
19e91 6e 67 20 74 61 62 6c 65 20 64 65 73 63 72 69 62  ng table describ
19e92 65 73 20 74 68 65 20 76 61 72 69 6f 75 73 20 73  es the various s
19e93 74 6f 72 61 67 65 20 63 6c 61 73 73 65 73 20 66  torage classes f
19e94 6f 72 20 64 61 74 61 3a 0a 2a 2a 0a 2a 2a 20 20  or data:.**.**  
19e95 20 73 65 72 69 61 6c 20 74 79 70 65 20 20 20 20   serial type    
19e96 20 20 20 20 62 79 74 65 73 20 6f 66 20 64 61 74      bytes of dat
19e97 61 20 20 20 20 20 20 74 79 70 65 0a 2a 2a 20 20  a      type.**  
19e98 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20   -------------- 
19e99 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      ------------
19e9a 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ---    ---------
19e9b 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 30  ------.**      0
19e9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e9d 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
19e9e 20 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 31    NULL.**      1
19e9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ea0 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20       1          
19ea1 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72    signed integer
19ea2 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 20  .**      2      
19ea3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
19ea4 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e              sign
19ea5 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  ed integer.**   
19ea6 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20     3            
19ea7 20 20 20 20 20 20 20 20 20 33 20 20 20 20 20 20           3      
19ea8 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74        signed int
19ea9 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 34 20 20  eger.**      4  
19eaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19eab 20 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20     4            
19eac 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a  signed integer.*
19ead 2a 20 20 20 20 20 20 35 20 20 20 20 20 20 20 20  *      5        
19eae 20 20 20 20 20 20 20 20 20 20 20 20 20 36 20 20               6  
19eaf 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64            signed
19eb0 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20   integer.**     
19eb1 20 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20   6              
19eb2 20 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20         8        
19eb3 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67      signed integ
19eb4 65 72 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20  er.**      7    
19eb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19eb6 20 38 20 20 20 20 20 20 20 20 20 20 20 20 49 45   8            IE
19eb7 45 45 20 66 6c 6f 61 74 0a 2a 2a 20 20 20 20 20  EE float.**     
19eb8 20 38 20 20 20 20 20 20 20 20 20 20 20 20 20 20   8              
19eb9 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20         0        
19eba 20 20 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73      Integer cons
19ebb 74 61 6e 74 20 30 0a 2a 2a 20 20 20 20 20 20 39  tant 0.**      9
19ebc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ebd 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
19ebe 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61    Integer consta
19ebf 6e 74 20 31 0a 2a 2a 20 20 20 20 20 31 30 2c 31  nt 1.**     10,1
19ec0 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
19ec1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ec2 72 65 73 65 72 76 65 64 20 66 6f 72 20 65 78 70  reserved for exp
19ec3 61 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 4e 3e 3d  ansion.**    N>=
19ec4 31 32 20 61 6e 64 20 65 76 65 6e 20 20 20 20 20  12 and even     
19ec5 20 20 28 4e 2d 31 32 29 2f 32 20 20 20 20 20 20    (N-12)/2      
19ec6 20 20 42 4c 4f 42 0a 2a 2a 20 20 20 20 4e 3e 3d    BLOB.**    N>=
19ec7 31 33 20 61 6e 64 20 6f 64 64 20 20 20 20 20 20  13 and odd      
19ec8 20 20 28 4e 2d 31 33 29 2f 32 20 20 20 20 20 20    (N-13)/2      
19ec9 20 20 74 65 78 74 0a 2a 2a 0a 2a 2a 20 54 68 65    text.**.** The
19eca 20 38 20 61 6e 64 20 39 20 74 79 70 65 73 20 77   8 and 9 types w
19ecb 65 72 65 20 61 64 64 65 64 20 69 6e 20 33 2e 33  ere added in 3.3
19ecc 2e 30 2c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  .0, file format 
19ecd 34 2e 20 20 50 72 69 6f 72 20 76 65 72 73 69 6f  4.  Prior versio
19ece 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20  ns.** of SQLite 
19ecf 77 69 6c 6c 20 6e 6f 74 20 75 6e 64 65 72 73 74  will not underst
19ed0 61 6e 64 20 74 68 6f 73 65 20 73 65 72 69 61 6c  and those serial
19ed1 20 74 79 70 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a   types..*/../*.*
19ed2 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 72  * Return the ser
19ed3 69 61 6c 2d 74 79 70 65 20 66 6f 72 20 74 68 65  ial-type for the
19ed4 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
19ed5 20 70 4d 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45   pMem..*/.SQLITE
19ed6 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
19ed7 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
19ed8 70 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e  pe(Mem *pMem, in
19ed9 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a  t file_format){.
19eda 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d    int flags = pM
19edb 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 6e 74  em->flags;.  int
19edc 20 6e 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 73   n;..  if( flags
19edd 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20  &MEM_Null ){.   
19ede 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
19edf 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 49   if( flags&MEM_I
19ee0 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67  nt ){.    /* Fig
19ee1 75 72 65 20 6f 75 74 20 77 68 65 74 68 65 72 20  ure out whether 
19ee2 74 6f 20 75 73 65 20 31 2c 20 32 2c 20 34 2c 20  to use 1, 2, 4, 
19ee3 36 20 6f 72 20 38 20 62 79 74 65 73 2e 20 2a 2f  6 or 8 bytes. */
19ee4 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f  .#   define MAX_
19ee5 36 42 59 54 45 20 28 28 28 28 69 36 34 29 30 78  6BYTE ((((i64)0x
19ee6 30 30 30 30 38 30 30 30 29 3c 3c 33 32 29 2d 31  00008000)<<32)-1
19ee7 29 0a 20 20 20 20 69 36 34 20 69 20 3d 20 70 4d  ).    i64 i = pM
19ee8 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 36 34  em->u.i;.    u64
19ee9 20 75 3b 0a 20 20 20 20 69 66 28 20 66 69 6c 65   u;.    if( file
19eea 5f 66 6f 72 6d 61 74 3e 3d 34 20 26 26 20 28 69  _format>=4 && (i
19eeb 26 31 29 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20  &1)==i ){.      
19eec 72 65 74 75 72 6e 20 38 2b 28 75 33 32 29 69 3b  return 8+(u32)i;
19eed 0a 20 20 20 20 7d 0a 20 20 20 20 75 20 3d 20 69  .    }.    u = i
19eee 3c 30 20 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20  <0 ? -i : i;.   
19eef 20 69 66 28 20 75 3c 3d 31 32 37 20 29 20 72 65   if( u<=127 ) re
19ef0 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20  turn 1;.    if( 
19ef1 75 3c 3d 33 32 37 36 37 20 29 20 72 65 74 75 72  u<=32767 ) retur
19ef2 6e 20 32 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d  n 2;.    if( u<=
19ef3 38 33 38 38 36 30 37 20 29 20 72 65 74 75 72 6e  8388607 ) return
19ef4 20 33 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 32   3;.    if( u<=2
19ef5 31 34 37 34 38 33 36 34 37 20 29 20 72 65 74 75  147483647 ) retu
19ef6 72 6e 20 34 3b 0a 20 20 20 20 69 66 28 20 75 3c  rn 4;.    if( u<
19ef7 3d 4d 41 58 5f 36 42 59 54 45 20 29 20 72 65 74  =MAX_6BYTE ) ret
19ef8 75 72 6e 20 35 3b 0a 20 20 20 20 72 65 74 75 72  urn 5;.    retur
19ef9 6e 20 36 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66  n 6;.  }.  if( f
19efa 6c 61 67 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b  lags&MEM_Real ){
19efb 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20  .    return 7;. 
19efc 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65   }.  assert( pMe
19efd 6d 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  m->db->mallocFai
19efe 6c 65 64 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45  led || flags&(ME
19eff 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
19f00 29 3b 0a 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e  );.  n = pMem->n
19f01 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ;.  if( flags & 
19f02 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20  MEM_Zero ){.    
19f03 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65  n += pMem->u.nZe
19f04 72 6f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ro;.  }.  assert
19f05 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75  ( n>=0 );.  retu
19f06 72 6e 20 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b  rn ((n*2) + 12 +
19f07 20 28 28 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72   ((flags&MEM_Str
19f08 29 21 3d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  )!=0));.}../*.**
19f09 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67   Return the leng
19f0a 74 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 63  th of the data c
19f0b 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
19f0c 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 65 72  the supplied ser
19f0d 69 61 6c 2d 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c  ial-type..*/.SQL
19f0e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19f0f 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
19f10 6c 54 79 70 65 4c 65 6e 28 75 33 32 20 73 65 72  lTypeLen(u32 ser
19f11 69 61 6c 5f 74 79 70 65 29 7b 0a 20 20 69 66 28  ial_type){.  if(
19f12 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32   serial_type>=12
19f13 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
19f14 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f  serial_type-12)/
19f15 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  2;.  }else{.    
19f16 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
19f17 61 53 69 7a 65 5b 5d 20 3d 20 7b 20 30 2c 20 31  aSize[] = { 0, 1
19f18 2c 20 32 2c 20 33 2c 20 34 2c 20 36 2c 20 38 2c  , 2, 3, 4, 6, 8,
19f19 20 38 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d   8, 0, 0, 0, 0 }
19f1a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 53 69  ;.    return aSi
19f1b 7a 65 5b 73 65 72 69 61 6c 5f 74 79 70 65 5d 3b  ze[serial_type];
19f1c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
19f1d 20 77 65 20 61 72 65 20 6f 6e 20 61 6e 20 61 72   we are on an ar
19f1e 63 68 69 74 65 63 74 75 72 65 20 77 69 74 68 20  chitecture with 
19f1f 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f  mixed-endian flo
19f20 61 74 69 6e 67 20 0a 2a 2a 20 70 6f 69 6e 74 73  ating .** points
19f21 20 28 65 78 3a 20 41 52 4d 37 29 20 74 68 65 6e   (ex: ARM7) then
19f22 20 73 77 61 70 20 74 68 65 20 6c 6f 77 65 72 20   swap the lower 
19f23 34 20 62 79 74 65 73 20 77 69 74 68 20 74 68 65  4 bytes with the
19f24 20 0a 2a 2a 20 75 70 70 65 72 20 34 20 62 79 74   .** upper 4 byt
19f25 65 73 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  es.  Return the 
19f26 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f  result..**.** Fo
19f27 72 20 6d 6f 73 74 20 61 72 63 68 69 74 65 63 74  r most architect
19f28 75 72 65 73 2c 20 74 68 69 73 20 69 73 20 61 20  ures, this is a 
19f29 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61  no-op..**.** (la
19f2a 74 65 72 29 3a 20 20 49 74 20 69 73 20 72 65 70  ter):  It is rep
19f2b 6f 72 74 65 64 20 74 6f 20 6d 65 20 74 68 61 74  orted to me that
19f2c 20 74 68 65 20 6d 69 78 65 64 2d 65 6e 64 69 61   the mixed-endia
19f2d 6e 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20  n problem.** on 
19f2e 41 52 4d 37 20 69 73 20 61 6e 20 69 73 73 75 65  ARM7 is an issue
19f2f 20 77 69 74 68 20 47 43 43 2c 20 6e 6f 74 20 77   with GCC, not w
19f30 69 74 68 20 74 68 65 20 41 52 4d 37 20 63 68 69  ith the ARM7 chi
19f31 70 2e 20 20 49 74 20 73 65 65 6d 73 0a 2a 2a 20  p.  It seems.** 
19f32 74 68 61 74 20 65 61 72 6c 79 20 76 65 72 73 69  that early versi
19f33 6f 6e 73 20 6f 66 20 47 43 43 20 73 74 6f 72 65  ons of GCC store
19f34 64 20 74 68 65 20 74 77 6f 20 77 6f 72 64 73 20  d the two words 
19f35 6f 66 20 61 20 36 34 2d 62 69 74 0a 2a 2a 20 66  of a 64-bit.** f
19f36 6c 6f 61 74 20 69 6e 20 74 68 65 20 77 72 6f 6e  loat in the wron
19f37 67 20 6f 72 64 65 72 2e 20 20 41 6e 64 20 74 68  g order.  And th
19f38 61 74 20 65 72 72 6f 72 20 68 61 73 20 62 65 65  at error has bee
19f39 6e 20 70 72 6f 70 61 67 61 74 65 64 0a 2a 2a 20  n propagated.** 
19f3a 65 76 65 72 20 73 69 6e 63 65 2e 20 20 54 68 65  ever since.  The
19f3b 20 62 6c 61 6d 65 20 69 73 20 6e 6f 74 20 6e 65   blame is not ne
19f3c 63 65 73 73 61 72 69 6c 79 20 77 69 74 68 20 47  cessarily with G
19f3d 43 43 2c 20 74 68 6f 75 67 68 2e 0a 2a 2a 20 47  CC, though..** G
19f3e 43 43 20 6d 69 67 68 74 20 68 61 76 65 20 6a 75  CC might have ju
19f3f 73 74 20 63 6f 70 79 69 6e 67 20 74 68 65 20 70  st copying the p
19f40 72 6f 62 6c 65 6d 20 66 72 6f 6d 20 61 20 70 72  roblem from a pr
19f41 69 6f 72 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a  ior compiler..**
19f42 20 49 20 61 6d 20 61 6c 73 6f 20 74 6f 6c 64 20   I am also told 
19f43 74 68 61 74 20 6e 65 77 65 72 20 76 65 72 73 69  that newer versi
19f44 6f 6e 73 20 6f 66 20 47 43 43 20 74 68 61 74 20  ons of GCC that 
19f45 66 6f 6c 6c 6f 77 20 61 20 64 69 66 66 65 72 65  follow a differe
19f46 6e 74 0a 2a 2a 20 41 42 49 20 67 65 74 20 74 68  nt.** ABI get th
19f47 65 20 62 79 74 65 20 6f 72 64 65 72 20 72 69 67  e byte order rig
19f48 68 74 2e 0a 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f  ht..**.** Develo
19f49 70 65 72 73 20 75 73 69 6e 67 20 53 51 4c 69 74  pers using SQLit
19f4a 65 20 6f 6e 20 61 6e 20 41 52 4d 37 20 73 68 6f  e on an ARM7 sho
19f4b 75 6c 64 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20  uld compile and 
19f4c 72 75 6e 20 74 68 65 69 72 0a 2a 2a 20 61 70 70  run their.** app
19f4d 6c 69 63 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d  lication using -
19f4e 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 20  DSQLITE_DEBUG=1 
19f4f 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20  at least once.  
19f50 57 69 74 68 20 44 45 42 55 47 0a 2a 2a 20 65 6e  With DEBUG.** en
19f51 61 62 6c 65 64 2c 20 73 6f 6d 65 20 61 73 73 65  abled, some asse
19f52 72 74 73 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65  rts below will e
19f53 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 62  nsure that the b
19f54 79 74 65 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20  yte order of.** 
19f55 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
19f56 61 6c 75 65 73 20 69 73 20 63 6f 72 72 65 63 74  alues is correct
19f57 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 37 2d 30 38  ..**.** (2007-08
19f58 2d 33 30 29 20 20 46 72 61 6e 6b 20 76 61 6e 20  -30)  Frank van 
19f59 56 75 67 74 20 68 61 73 20 73 74 75 64 69 65 64  Vugt has studied
19f5a 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 63 6c   this problem cl
19f5b 6f 73 65 6c 79 0a 2a 2a 20 61 6e 64 20 68 61 73  osely.** and has
19f5c 20 73 65 6e 64 20 68 69 73 20 66 69 6e 64 69 6e   send his findin
19f5d 67 73 20 74 6f 20 74 68 65 20 53 51 4c 69 74 65  gs to the SQLite
19f5e 20 64 65 76 65 6c 6f 70 65 72 73 2e 20 20 46 72   developers.  Fr
19f5f 61 6e 6b 0a 2a 2a 20 77 72 69 74 65 73 20 74 68  ank.** writes th
19f60 61 74 20 73 6f 6d 65 20 4c 69 6e 75 78 20 6b 65  at some Linux ke
19f61 72 6e 65 6c 73 20 6f 66 66 65 72 20 66 6c 6f 61  rnels offer floa
19f62 74 69 6e 67 20 70 6f 69 6e 74 20 68 61 72 64 77  ting point hardw
19f63 61 72 65 0a 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e  are.** emulation
19f64 20 74 68 61 74 20 75 73 65 73 20 6f 6e 6c 79 20   that uses only 
19f65 33 32 2d 62 69 74 20 6d 61 6e 74 69 73 73 61 73  32-bit mantissas
19f66 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 75   instead of a fu
19f67 6c 6c 20 0a 2a 2a 20 34 38 2d 62 69 74 73 20 61  ll .** 48-bits a
19f68 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68  s required by th
19f69 65 20 49 45 45 45 20 73 74 61 6e 64 61 72 64 2e  e IEEE standard.
19f6a 20 20 28 54 68 69 73 20 69 73 20 74 68 65 0a 2a    (This is the.*
19f6b 2a 20 43 4f 4e 46 49 47 5f 46 50 45 5f 46 41 53  * CONFIG_FPE_FAS
19f6c 54 46 50 45 20 6f 70 74 69 6f 6e 2e 29 20 20 4f  TFPE option.)  O
19f6d 6e 20 73 75 63 68 20 73 79 73 74 65 6d 73 2c 20  n such systems, 
19f6e 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a  floating point.*
19f6f 2a 20 62 79 74 65 20 73 77 61 70 70 69 6e 67 20  * byte swapping 
19f70 62 65 63 6f 6d 65 73 20 76 65 72 79 20 63 6f 6d  becomes very com
19f71 70 6c 69 63 61 74 65 64 2e 20 20 54 6f 20 61 76  plicated.  To av
19f72 6f 69 64 20 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a  oid problems,.**
19f73 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 62   the necessary b
19f74 79 74 65 20 73 77 61 70 70 69 6e 67 20 69 73 20  yte swapping is 
19f75 63 61 72 72 69 65 64 20 6f 75 74 20 75 73 69 6e  carried out usin
19f76 67 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67  g a 64-bit integ
19f77 65 72 0a 2a 2a 20 72 61 74 68 65 72 20 74 68 61  er.** rather tha
19f78 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74  n a 64-bit float
19f79 2e 20 20 46 72 61 6e 6b 20 61 73 73 75 72 65 73  .  Frank assures
19f7a 20 75 73 20 74 68 61 74 20 74 68 65 20 63 6f 64   us that the cod
19f7b 65 20 68 65 72 65 0a 2a 2a 20 77 6f 72 6b 73 20  e here.** works 
19f7c 66 6f 72 20 68 69 6d 2e 20 20 57 65 2c 20 74 68  for him.  We, th
19f7d 65 20 64 65 76 65 6c 6f 70 65 72 73 2c 20 68 61  e developers, ha
19f7e 76 65 20 6e 6f 20 77 61 79 20 74 6f 20 69 6e 64  ve no way to ind
19f7f 65 70 65 6e 64 65 6e 74 6c 79 0a 2a 2a 20 76 65  ependently.** ve
19f80 72 69 66 79 20 74 68 69 73 2c 20 62 75 74 20 46  rify this, but F
19f81 72 61 6e 6b 20 73 65 65 6d 73 20 74 6f 20 6b 6e  rank seems to kn
19f82 6f 77 20 77 68 61 74 20 68 65 20 69 73 20 74 61  ow what he is ta
19f83 6c 6b 69 6e 67 20 61 62 6f 75 74 0a 2a 2a 20 73  lking about.** s
19f84 6f 20 77 65 20 74 72 75 73 74 20 68 69 6d 2e 0a  o we trust him..
19f85 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
19f86 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34  _MIXED_ENDIAN_64
19f87 42 49 54 5f 46 4c 4f 41 54 0a 73 74 61 74 69 63  BIT_FLOAT.static
19f88 20 75 36 34 20 66 6c 6f 61 74 53 77 61 70 28 75   u64 floatSwap(u
19f89 36 34 20 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20  64 in){.  union 
19f8a 7b 0a 20 20 20 20 75 36 34 20 72 3b 0a 20 20 20  {.    u64 r;.   
19f8b 20 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d 20 75   u32 i[2];.  } u
19f8c 3b 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20 75 2e  ;.  u32 t;..  u.
19f8d 72 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20 75 2e  r = in;.  t = u.
19f8e 69 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d  i[0];.  u.i[0] =
19f8f 20 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31   u.i[1];.  u.i[1
19f90 5d 20 3d 20 74 3b 0a 20 20 72 65 74 75 72 6e 20  ] = t;.  return 
19f91 75 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20  u.r;.}.# define 
19f92 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46  swapMixedEndianF
19f93 6c 6f 61 74 28 58 29 20 20 58 20 3d 20 66 6c 6f  loat(X)  X = flo
19f94 61 74 53 77 61 70 28 58 29 0a 23 65 6c 73 65 0a  atSwap(X).#else.
19f95 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78  # define swapMix
19f96 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29  edEndianFloat(X)
19f97 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
19f98 72 69 74 65 20 74 68 65 20 73 65 72 69 61 6c 69  rite the seriali
19f99 7a 65 64 20 64 61 74 61 20 62 6c 6f 62 20 66 6f  zed data blob fo
19f9a 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  r the value stor
19f9b 65 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74 6f 20  ed in pMem into 
19f9c 0a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73 20 61  .** buf. It is a
19f9d 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20  ssumed that the 
19f9e 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c 6f 63  caller has alloc
19f9f 61 74 65 64 20 73 75 66 66 69 63 69 65 6e 74 20  ated sufficient 
19fa0 73 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75 72 6e  space..** Return
19fa1 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
19fa2 79 74 65 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a  ytes written..**
19fa3 0a 2a 2a 20 6e 42 75 66 20 69 73 20 74 68 65 20  .** nBuf is the 
19fa4 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20  amount of space 
19fa5 6c 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e 20 20  left in buf[].  
19fa6 6e 42 75 66 20 6d 75 73 74 20 61 6c 77 61 79 73  nBuf must always
19fa7 20 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65 6e 6f   be.** large eno
19fa8 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ugh to hold the 
19fa9 65 6e 74 69 72 65 20 66 69 65 6c 64 2e 20 20 45  entire field.  E
19faa 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 66 69  xcept, if the fi
19fab 65 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c 6f 62  eld is.** a blob
19fac 20 77 69 74 68 20 61 20 7a 65 72 6f 2d 66 69 6c   with a zero-fil
19fad 6c 65 64 20 74 61 69 6c 2c 20 74 68 65 6e 20 62  led tail, then b
19fae 75 66 5b 5d 20 6d 69 67 68 74 20 62 65 20 6a 75  uf[] might be ju
19faf 73 74 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20  st the right.** 
19fb0 73 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65 76 65  size to hold eve
19fb1 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 66  rything except f
19fb2 6f 72 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c  or the zero-fill
19fb3 65 64 20 74 61 69 6c 2e 20 20 49 66 20 62 75 66  ed tail.  If buf
19fb4 5b 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 62 69  [].** is only bi
19fb5 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  g enough to hold
19fb6 20 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72   the non-zero pr
19fb7 65 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c 79 20  efix, then only 
19fb8 77 72 69 74 65 20 74 68 61 74 0a 2a 2a 20 70 72  write that.** pr
19fb9 65 66 69 78 20 69 6e 74 6f 20 62 75 66 5b 5d 2e  efix into buf[].
19fba 20 20 42 75 74 20 69 66 20 62 75 66 5b 5d 20 69    But if buf[] i
19fbb 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74  s large enough t
19fbc 6f 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 65 0a  o hold both the.
19fbd 2a 2a 20 70 72 65 66 69 78 20 61 6e 64 20 74 68  ** prefix and th
19fbe 65 20 74 61 69 6c 20 74 68 65 6e 20 77 72 69 74  e tail then writ
19fbf 65 20 74 68 65 20 70 72 65 66 69 78 20 61 6e 64  e the prefix and
19fc0 20 73 65 74 20 74 68 65 20 74 61 69 6c 20 74 6f   set the tail to
19fc1 20 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a   all.** zeros..*
19fc2 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
19fc3 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
19fc4 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e  actually written
19fc5 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68   into buf[].  Th
19fc6 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62  e number.** of b
19fc7 79 74 65 73 20 69 6e 20 74 68 65 20 7a 65 72 6f  ytes in the zero
19fc8 2d 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 73 20  -filled tail is 
19fc9 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20  included in the 
19fca 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c  return value onl
19fcb 79 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 62 79  y.** if those by
19fcc 74 65 73 20 77 65 72 65 20 7a 65 72 6f 65 64 20  tes were zeroed 
19fcd 69 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51  in buf[]..*/ .SQ
19fce 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19fcf 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
19fd0 61 6c 50 75 74 28 75 38 20 2a 62 75 66 2c 20 69  alPut(u8 *buf, i
19fd1 6e 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a 70 4d  nt nBuf, Mem *pM
19fd2 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72  em, int file_for
19fd3 6d 61 74 29 7b 0a 20 20 75 33 32 20 73 65 72 69  mat){.  u32 seri
19fd4 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65  al_type = sqlite
19fd5 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28  3VdbeSerialType(
19fd6 70 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d 61  pMem, file_forma
19fd7 74 29 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a  t);.  int len;..
19fd8 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 61 6e 64    /* Integer and
19fd9 20 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28 20 73   Real */.  if( s
19fda 65 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20 26 26  erial_type<=7 &&
19fdb 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 30 20 29   serial_type>0 )
19fdc 7b 0a 20 20 20 20 75 36 34 20 76 3b 0a 20 20 20  {.    u64 v;.   
19fdd 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28 20   int i;.    if( 
19fde 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37 20 29  serial_type==7 )
19fdf 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
19fe0 73 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a 65 6f  sizeof(v)==sizeo
19fe1 66 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20 20  f(pMem->r) );.  
19fe2 20 20 20 20 6d 65 6d 63 70 79 28 26 76 2c 20 26      memcpy(&v, &
19fe3 70 4d 65 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28  pMem->r, sizeof(
19fe4 76 29 29 3b 0a 20 20 20 20 20 20 73 77 61 70 4d  v));.      swapM
19fe5 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28  ixedEndianFloat(
19fe6 76 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  v);.    }else{. 
19fe7 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75       v = pMem->u
19fe8 2e 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65  .i;.    }.    le
19fe9 6e 20 3d 20 69 20 3d 20 73 71 6c 69 74 65 33 56  n = i = sqlite3V
19fea 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
19feb 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20  (serial_type);. 
19fec 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3c 3d     assert( len<=
19fed 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c  nBuf );.    whil
19fee 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20  e( i-- ){.      
19fef 62 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26  buf[i] = (u8)(v&
19ff0 30 78 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e  0xFF);.      v >
19ff1 3e 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20  >= 8;.    }.    
19ff2 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a  return len;.  }.
19ff3 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20  .  /* String or 
19ff4 62 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65  blob */.  if( se
19ff5 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b  rial_type>=12 ){
19ff6 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
19ff7 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66  m->n + ((pMem->f
19ff8 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29  lags & MEM_Zero)
19ff9 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30  ?pMem->u.nZero:0
19ffa 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d  ).             =
19ffb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
19ffc 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61  ialTypeLen(seria
19ffd 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20 20 20 61  l_type) );.    a
19ffe 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3c 3d  ssert( pMem->n<=
19fff 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c 65 6e 20  nBuf );.    len 
1a000 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 20 6d  = pMem->n;.    m
1a001 65 6d 63 70 79 28 62 75 66 2c 20 70 4d 65 6d 2d  emcpy(buf, pMem-
1a002 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 69 66  >z, len);.    if
1a003 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
1a004 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20  MEM_Zero ){.    
1a005 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75    len += pMem->u
1a006 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20 69 66  .nZero;.      if
1a007 28 20 6c 65 6e 3e 6e 42 75 66 20 29 7b 0a 20 20  ( len>nBuf ){.  
1a008 20 20 20 20 20 20 6c 65 6e 20 3d 20 6e 42 75 66        len = nBuf
1a009 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1a00a 6d 65 6d 73 65 74 28 26 62 75 66 5b 70 4d 65 6d  memset(&buf[pMem
1a00b 2d 3e 6e 5d 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65  ->n], 0, len-pMe
1a00c 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20  m->n);.    }.   
1a00d 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d   return len;.  }
1a00e 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63  ..  /* NULL or c
1a00f 6f 6e 73 74 61 6e 74 73 20 30 20 6f 72 20 31 20  onstants 0 or 1 
1a010 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  */.  return 0;.}
1a011 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c  ../*.** Deserial
1a012 69 7a 65 20 74 68 65 20 64 61 74 61 20 62 6c 6f  ize the data blo
1a013 62 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  b pointed to by 
1a014 62 75 66 20 61 73 20 73 65 72 69 61 6c 20 74 79  buf as serial ty
1a015 70 65 20 73 65 72 69 61 6c 5f 74 79 70 65 0a 2a  pe serial_type.*
1a016 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  * and store the 
1a017 72 65 73 75 6c 74 20 69 6e 20 70 4d 65 6d 2e 20  result in pMem. 
1a018 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1a019 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64  er of bytes read
1a01a 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/ .SQLITE_PRI
1a01b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1a01c 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 0a 20  VdbeSerialGet(. 
1a01d 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1a01e 63 68 61 72 20 2a 62 75 66 2c 20 20 20 20 20 2f  char *buf,     /
1a01f 2a 20 42 75 66 66 65 72 20 74 6f 20 64 65 73 65  * Buffer to dese
1a020 72 69 61 6c 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a  rialize from */.
1a021 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70    u32 serial_typ
1a022 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
1a023 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 74  /* Serial type t
1a024 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 2a 2f  o deserialize */
1a025 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20  .  Mem *pMem    
1a026 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a027 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20   /* Memory cell 
1a028 74 6f 20 77 72 69 74 65 20 76 61 6c 75 65 20 69  to write value i
1a029 6e 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74  nto */.){.  swit
1a02a 63 68 28 20 73 65 72 69 61 6c 5f 74 79 70 65 20  ch( serial_type 
1a02b 29 7b 0a 20 20 20 20 63 61 73 65 20 31 30 3a 20  ){.    case 10: 
1a02c 20 20 2f 2a 20 52 65 73 65 72 76 65 64 20 66 6f    /* Reserved fo
1a02d 72 20 66 75 74 75 72 65 20 75 73 65 20 2a 2f 0a  r future use */.
1a02e 20 20 20 20 63 61 73 65 20 31 31 3a 20 20 20 2f      case 11:   /
1a02f 2a 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66  * Reserved for f
1a030 75 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20  uture use */.   
1a031 20 63 61 73 65 20 30 3a 20 7b 20 20 2f 2a 20 4e   case 0: {  /* N
1a032 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ULL */.      pMe
1a033 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  m->flags = MEM_N
1a034 75 6c 6c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ull;.      break
1a035 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1a036 20 31 3a 20 7b 20 2f 2a 20 31 2d 62 79 74 65 20   1: { /* 1-byte 
1a037 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
1a038 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  /.      pMem->u.
1a039 69 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72  i = (signed char
1a03a 29 62 75 66 5b 30 5d 3b 0a 20 20 20 20 20 20 70  )buf[0];.      p
1a03b 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1a03c 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75  _Int;.      retu
1a03d 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
1a03e 63 61 73 65 20 32 3a 20 7b 20 2f 2a 20 32 2d 62  case 2: { /* 2-b
1a03f 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
1a040 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d  er */.      pMem
1a041 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65  ->u.i = (((signe
1a042 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c  d char)buf[0])<<
1a043 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20  8) | buf[1];.   
1a044 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a045 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1a046 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 7d 0a  return 2;.    }.
1a047 20 20 20 20 63 61 73 65 20 33 3a 20 7b 20 2f 2a      case 3: { /*
1a048 20 33 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   3-byte signed i
1a049 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
1a04a 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 73  pMem->u.i = (((s
1a04b 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30  igned char)buf[0
1a04c 5d 29 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 31  ])<<16) | (buf[1
1a04d 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 32 5d 3b 0a  ]<<8) | buf[2];.
1a04e 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1a04f 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1a050 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20     return 3;.   
1a051 20 7d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7b   }.    case 4: {
1a052 20 2f 2a 20 34 2d 62 79 74 65 20 73 69 67 6e 65   /* 4-byte signe
1a053 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20  d integer */.   
1a054 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28     pMem->u.i = (
1a055 62 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62  buf[0]<<24) | (b
1a056 75 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75  uf[1]<<16) | (bu
1a057 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33  f[2]<<8) | buf[3
1a058 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ];.      pMem->f
1a059 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1a05a 20 20 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a        return 4;.
1a05b 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 35      }.    case 5
1a05c 3a 20 7b 20 2f 2a 20 36 2d 62 79 74 65 20 73 69  : { /* 6-byte si
1a05d 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
1a05e 20 20 20 20 20 20 75 36 34 20 78 20 3d 20 28 28        u64 x = ((
1a05f 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66  (signed char)buf
1a060 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31  [0])<<8) | buf[1
1a061 5d 3b 0a 20 20 20 20 20 20 75 33 32 20 79 20 3d  ];.      u32 y =
1a062 20 28 62 75 66 5b 32 5d 3c 3c 32 34 29 20 7c 20   (buf[2]<<24) | 
1a063 28 62 75 66 5b 33 5d 3c 3c 31 36 29 20 7c 20 28  (buf[3]<<16) | (
1a064 62 75 66 5b 34 5d 3c 3c 38 29 20 7c 20 62 75 66  buf[4]<<8) | buf
1a065 5b 35 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28  [5];.      x = (
1a066 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20  x<<32) | y;.    
1a067 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28    pMem->u.i = *(
1a068 69 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 70  i64*)&x;.      p
1a069 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1a06a 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75  _Int;.      retu
1a06b 72 6e 20 36 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 6;.    }.    
1a06c 63 61 73 65 20 36 3a 20 20 20 2f 2a 20 38 2d 62  case 6:   /* 8-b
1a06d 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
1a06e 65 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 37  er */.    case 7
1a06f 3a 20 7b 20 2f 2a 20 49 45 45 45 20 66 6c 6f 61  : { /* IEEE floa
1a070 74 69 6e 67 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  ting point */.  
1a071 20 20 20 20 75 36 34 20 78 3b 0a 20 20 20 20 20      u64 x;.     
1a072 20 75 33 32 20 79 3b 0a 23 69 66 20 21 64 65 66   u32 y;.#if !def
1a073 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20  ined(NDEBUG) && 
1a074 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1a075 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
1a076 49 4e 54 29 0a 20 20 20 20 20 20 2f 2a 20 56 65  INT).      /* Ve
1a077 72 69 66 79 20 74 68 61 74 20 69 6e 74 65 67 65  rify that intege
1a078 72 73 20 61 6e 64 20 66 6c 6f 61 74 69 6e 67 20  rs and floating 
1a079 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 75 73 65  point values use
1a07a 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20 20   the same.      
1a07b 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20  ** byte order.  
1a07c 4f 72 2c 20 74 68 61 74 20 69 66 20 53 51 4c 49  Or, that if SQLI
1a07d 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f  TE_MIXED_ENDIAN_
1a07e 36 34 42 49 54 5f 46 4c 4f 41 54 20 69 73 0a 20  64BIT_FLOAT is. 
1a07f 20 20 20 20 20 2a 2a 20 64 65 66 69 6e 65 64 20       ** defined 
1a080 74 68 61 74 20 36 34 2d 62 69 74 20 66 6c 6f 61  that 64-bit floa
1a081 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
1a082 73 20 72 65 61 6c 6c 79 20 61 72 65 20 6d 69 78  s really are mix
1a083 65 64 0a 20 20 20 20 20 20 2a 2a 20 65 6e 64 69  ed.      ** endi
1a084 61 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  an..      */.   
1a085 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
1a086 75 36 34 20 74 31 20 3d 20 28 28 75 36 34 29 30  u64 t1 = ((u64)0
1a087 78 33 66 66 30 30 30 30 30 29 3c 3c 33 32 3b 0a  x3ff00000)<<32;.
1a088 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
1a089 73 74 20 64 6f 75 62 6c 65 20 72 31 20 3d 20 31  st double r1 = 1
1a08a 2e 30 3b 0a 20 20 20 20 20 20 75 36 34 20 74 32  .0;.      u64 t2
1a08b 20 3d 20 74 31 3b 0a 20 20 20 20 20 20 73 77 61   = t1;.      swa
1a08c 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61  pMixedEndianFloa
1a08d 74 28 74 32 29 3b 0a 20 20 20 20 20 20 61 73 73  t(t2);.      ass
1a08e 65 72 74 28 20 73 69 7a 65 6f 66 28 72 31 29 3d  ert( sizeof(r1)=
1a08f 3d 73 69 7a 65 6f 66 28 74 32 29 20 26 26 20 6d  =sizeof(t2) && m
1a090 65 6d 63 6d 70 28 26 72 31 2c 20 26 74 32 2c 20  emcmp(&r1, &t2, 
1a091 73 69 7a 65 6f 66 28 72 31 29 29 3d 3d 30 20 29  sizeof(r1))==0 )
1a092 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20  ;.#endif..      
1a093 78 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29  x = (buf[0]<<24)
1a094 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20   | (buf[1]<<16) 
1a095 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20  | (buf[2]<<8) | 
1a096 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 79 20  buf[3];.      y 
1a097 3d 20 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c  = (buf[4]<<24) |
1a098 20 28 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20   (buf[5]<<16) | 
1a099 28 62 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75  (buf[6]<<8) | bu
1a09a 66 5b 37 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20  f[7];.      x = 
1a09b 28 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20  (x<<32) | y;.   
1a09c 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79     if( serial_ty
1a09d 70 65 3d 3d 36 20 29 7b 0a 20 20 20 20 20 20 20  pe==6 ){.       
1a09e 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69   pMem->u.i = *(i
1a09f 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 20 20  64*)&x;.        
1a0a0 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1a0a1 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c  M_Int;.      }el
1a0a2 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  se{.        asse
1a0a3 72 74 28 20 73 69 7a 65 6f 66 28 78 29 3d 3d 38  rt( sizeof(x)==8
1a0a4 20 26 26 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d   && sizeof(pMem-
1a0a5 3e 72 29 3d 3d 38 20 29 3b 0a 20 20 20 20 20 20  >r)==8 );.      
1a0a6 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61    swapMixedEndia
1a0a7 6e 46 6c 6f 61 74 28 78 29 3b 0a 20 20 20 20 20  nFloat(x);.     
1a0a8 20 20 20 6d 65 6d 63 70 79 28 26 70 4d 65 6d 2d     memcpy(&pMem-
1a0a9 3e 72 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78  >r, &x, sizeof(x
1a0aa 29 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d  ));.        pMem
1a0ab 2d 3e 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65  ->flags = sqlite
1a0ac 33 49 73 4e 61 4e 28 70 4d 65 6d 2d 3e 72 29 20  3IsNaN(pMem->r) 
1a0ad 3f 20 4d 45 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d  ? MEM_Null : MEM
1a0ae 5f 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  _Real;.      }. 
1a0af 20 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20       return 8;. 
1a0b0 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38 3a     }.    case 8:
1a0b1 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 30      /* Integer 0
1a0b2 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a 20   */.    case 9: 
1a0b3 7b 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31 20  {  /* Integer 1 
1a0b4 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  */.      pMem->u
1a0b5 2e 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70 65  .i = serial_type
1a0b6 2d 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  -8;.      pMem->
1a0b7 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1a0b8 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
1a0b9 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
1a0ba 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  lt: {.      int 
1a0bb 6c 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74 79  len = (serial_ty
1a0bc 70 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20 20  pe-12)/2;.      
1a0bd 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20  pMem->z = (char 
1a0be 2a 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d 65  *)buf;.      pMe
1a0bf 6d 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20 20  m->n = len;.    
1a0c0 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30    pMem->xDel = 0
1a0c1 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69  ;.      if( seri
1a0c2 61 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b 0a  al_type&0x01 ){.
1a0c3 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c          pMem->fl
1a0c4 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c 20  ags = MEM_Str | 
1a0c5 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20  MEM_Ephem;.     
1a0c6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1a0c7 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1a0c8 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70 68  M_Blob | MEM_Eph
1a0c9 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  em;.      }.    
1a0ca 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20    return len;.  
1a0cb 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1a0cc 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69   0;.}.../*.** Gi
1a0cd 76 65 6e 20 74 68 65 20 6e 4b 65 79 2d 62 79 74  ven the nKey-byt
1a0ce 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20  e encoding of a 
1a0cf 72 65 63 6f 72 64 20 69 6e 20 70 4b 65 79 5b 5d  record in pKey[]
1a0d0 2c 20 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 72  , parse the.** r
1a0d1 65 63 6f 72 64 20 69 6e 74 6f 20 61 20 55 6e 70  ecord into a Unp
1a0d2 61 63 6b 65 64 52 65 63 6f 72 64 20 73 74 72 75  ackedRecord stru
1a0d3 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 61  cture.  Return a
1a0d4 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74   pointer to.** t
1a0d5 68 61 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  hat structure..*
1a0d6 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67  *.** The calling
1a0d7 20 66 75 6e 63 74 69 6f 6e 20 6d 69 67 68 74 20   function might 
1a0d8 70 72 6f 76 69 64 65 20 73 7a 53 70 61 63 65 20  provide szSpace 
1a0d9 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a  bytes of memory.
1a0da 2a 2a 20 73 70 61 63 65 20 61 74 20 70 53 70 61  ** space at pSpa
1a0db 63 65 2e 20 20 54 68 69 73 20 73 70 61 63 65 20  ce.  This space 
1a0dc 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 68  can be used to h
1a0dd 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64  old the returned
1a0de 0a 2a 2a 20 56 44 62 65 50 61 72 73 65 64 52 65  .** VDbeParsedRe
1a0df 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 20 69  cord structure i
1a0e0 66 20 69 74 20 69 73 20 6c 61 72 67 65 20 65 6e  f it is large en
1a0e1 6f 75 67 68 2e 20 20 49 66 20 69 74 20 69 73 0a  ough.  If it is.
1a0e2 2a 2a 20 6e 6f 74 20 62 69 67 20 65 6e 6f 75 67  ** not big enoug
1a0e3 68 2c 20 73 70 61 63 65 20 69 73 20 6f 62 74 61  h, space is obta
1a0e4 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
1a0e5 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a  3_malloc()..**.*
1a0e6 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73  * The returned s
1a0e7 74 72 75 63 74 75 72 65 20 73 68 6f 75 6c 64 20  tructure should 
1a0e8 62 65 20 63 6c 6f 73 65 64 20 62 79 20 61 20 63  be closed by a c
1a0e9 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
1a0ea 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63  3VdbeDeleteUnpac
1a0eb 6b 65 64 52 65 63 6f 72 64 28 29 2e 0a 2a 2f 20  kedRecord()..*/ 
1a0ec 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a0ed 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
1a0ee 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
1a0ef 64 55 6e 70 61 63 6b 28 0a 20 20 4b 65 79 49 6e  dUnpack(.  KeyIn
1a0f0 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20  fo *pKeyInfo,   
1a0f1 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
1a0f2 20 61 62 6f 75 74 20 74 68 65 20 72 65 63 6f 72   about the recor
1a0f3 64 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 69 6e  d format */.  in
1a0f4 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20  t nKey,         
1a0f5 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1a0f6 74 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72  the binary recor
1a0f7 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  d */.  const voi
1a0f8 64 20 2a 70 4b 65 79 2c 20 20 20 20 20 20 2f 2a  d *pKey,      /*
1a0f9 20 54 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f   The binary reco
1a0fa 72 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 53  rd */.  char *pS
1a0fb 70 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 2f  pace,          /
1a0fc 2a 20 55 6e 61 6c 69 67 6e 65 64 20 73 70 61 63  * Unaligned spac
1a0fd 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68  e available to h
1a0fe 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 20 2a  old the object *
1a0ff 2f 0a 20 20 69 6e 74 20 73 7a 53 70 61 63 65 20  /.  int szSpace 
1a100 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
1a101 7a 65 20 6f 66 20 70 53 70 61 63 65 5b 5d 20 69  ze of pSpace[] i
1a102 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20  n bytes */.){.  
1a103 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1a104 68 61 72 20 2a 61 4b 65 79 20 3d 20 28 63 6f 6e  har *aKey = (con
1a105 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1a106 20 2a 29 70 4b 65 79 3b 0a 20 20 55 6e 70 61 63   *)pKey;.  Unpac
1a107 6b 65 64 52 65 63 6f 72 64 20 2a 70 3b 20 20 2f  kedRecord *p;  /
1a108 2a 20 54 68 65 20 75 6e 70 61 63 6b 65 64 20 72  * The unpacked r
1a109 65 63 6f 72 64 20 74 68 61 74 20 77 65 20 77 69  ecord that we wi
1a10a 6c 6c 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69  ll return */.  i
1a10b 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20  nt nByte;       
1a10c 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 73 70 61     /* Memory spa
1a10d 63 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c  ce needed to hol
1a10e 64 20 70 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f  d p, in bytes */
1a10f 0a 20 20 69 6e 74 20 64 3b 0a 20 20 75 33 32 20  .  int d;.  u32 
1a110 69 64 78 3b 0a 20 20 75 31 36 20 75 3b 20 20 20  idx;.  u16 u;   
1a111 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e             /* Un
1a112 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e  signed loop coun
1a113 74 65 72 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48  ter */.  u32 szH
1a114 64 72 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b  dr;.  Mem *pMem;
1a115 0a 20 20 69 6e 74 20 6e 4f 66 66 3b 20 20 20 20  .  int nOff;    
1a116 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 61         /* Increa
1a117 73 65 20 70 53 70 61 63 65 20 62 79 20 74 68 69  se pSpace by thi
1a118 73 20 6d 75 63 68 20 74 6f 20 38 2d 62 79 74 65  s much to 8-byte
1a119 20 61 6c 69 67 6e 20 69 74 20 2a 2f 0a 20 20 0a   align it */.  .
1a11a 20 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 77 61 6e    /*.  ** We wan
1a11b 74 20 74 6f 20 73 68 69 66 74 20 74 68 65 20 70  t to shift the p
1a11c 6f 69 6e 74 65 72 20 70 53 70 61 63 65 20 75 70  ointer pSpace up
1a11d 20 73 75 63 68 20 74 68 61 74 20 69 74 20 69 73   such that it is
1a11e 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 2e   8-byte aligned.
1a11f 0a 20 20 2a 2a 20 54 68 75 73 2c 20 77 65 20 6e  .  ** Thus, we n
1a120 65 65 64 20 74 6f 20 63 61 6c 63 75 6c 61 74 65  eed to calculate
1a121 20 61 20 76 61 6c 75 65 2c 20 6e 4f 66 66 2c 20   a value, nOff, 
1a122 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 37 2c  between 0 and 7,
1a123 20 74 6f 20 73 68 69 66 74 20 0a 20 20 2a 2a 20   to shift .  ** 
1a124 69 74 20 62 79 2e 20 20 49 66 20 70 53 70 61 63  it by.  If pSpac
1a125 65 20 69 73 20 61 6c 72 65 61 64 79 20 38 2d 62  e is already 8-b
1a126 79 74 65 20 61 6c 69 67 6e 65 64 2c 20 6e 4f 66  yte aligned, nOf
1a127 66 20 73 68 6f 75 6c 64 20 62 65 20 7a 65 72 6f  f should be zero
1a128 2e 0a 20 20 2a 2f 0a 20 20 6e 4f 66 66 20 3d 20  ..  */.  nOff = 
1a129 28 38 20 2d 20 28 53 51 4c 49 54 45 5f 50 54 52  (8 - (SQLITE_PTR
1a12a 5f 54 4f 5f 49 4e 54 28 70 53 70 61 63 65 29 20  _TO_INT(pSpace) 
1a12b 26 20 37 29 29 20 26 20 37 3b 0a 20 20 70 53 70  & 7)) & 7;.  pSp
1a12c 61 63 65 20 2b 3d 20 6e 4f 66 66 3b 0a 20 20 73  ace += nOff;.  s
1a12d 7a 53 70 61 63 65 20 2d 3d 20 6e 4f 66 66 3b 0a  zSpace -= nOff;.
1a12e 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38    nByte = ROUND8
1a12f 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64  (sizeof(Unpacked
1a130 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f  Record)) + sizeo
1a131 66 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e 66 6f  f(Mem)*(pKeyInfo
1a132 2d 3e 6e 46 69 65 6c 64 2b 31 29 3b 0a 20 20 69  ->nField+1);.  i
1a133 66 28 20 6e 42 79 74 65 3e 73 7a 53 70 61 63 65  f( nByte>szSpace
1a134 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69   ){.    p = sqli
1a135 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
1a136 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79  KeyInfo->db, nBy
1a137 74 65 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d  te);.    if( p==
1a138 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
1a139 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50    p->flags = UNP
1a13a 41 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20  ACKED_NEED_FREE 
1a13b 7c 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f  | UNPACKED_NEED_
1a13c 44 45 53 54 52 4f 59 3b 0a 20 20 7d 65 6c 73 65  DESTROY;.  }else
1a13d 7b 0a 20 20 20 20 70 20 3d 20 28 55 6e 70 61 63  {.    p = (Unpac
1a13e 6b 65 64 52 65 63 6f 72 64 2a 29 70 53 70 61 63  kedRecord*)pSpac
1a13f 65 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  e;.    p->flags 
1a140 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f  = UNPACKED_NEED_
1a141 44 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20 20 70  DESTROY;.  }.  p
1a142 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65  ->pKeyInfo = pKe
1a143 79 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65  yInfo;.  p->nFie
1a144 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e  ld = pKeyInfo->n
1a145 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70 2d 3e  Field + 1;.  p->
1a146 61 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 28 4d  aMem = pMem = (M
1a147 65 6d 2a 29 26 28 28 63 68 61 72 2a 29 70 29 5b  em*)&((char*)p)[
1a148 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e  ROUND8(sizeof(Un
1a149 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 5d 3b  packedRecord))];
1a14a 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54  .  assert( EIGHT
1a14b 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
1a14c 70 4d 65 6d 29 20 29 3b 0a 20 20 69 64 78 20 3d  pMem) );.  idx =
1a14d 20 67 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65   getVarint32(aKe
1a14e 79 2c 20 73 7a 48 64 72 29 3b 0a 20 20 64 20 3d  y, szHdr);.  d =
1a14f 20 73 7a 48 64 72 3b 0a 20 20 75 20 3d 20 30 3b   szHdr;.  u = 0;
1a150 0a 20 20 77 68 69 6c 65 28 20 69 64 78 3c 73 7a  .  while( idx<sz
1a151 48 64 72 20 26 26 20 75 3c 70 2d 3e 6e 46 69 65  Hdr && u<p->nFie
1a152 6c 64 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65  ld ){.    u32 se
1a153 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20 20 20  rial_type;..    
1a154 69 64 78 20 2b 3d 20 67 65 74 56 61 72 69 6e 74  idx += getVarint
1a155 33 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c 20 73  32(&aKey[idx], s
1a156 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20  erial_type);.   
1a157 20 69 66 28 20 64 3e 3d 6e 4b 65 79 20 26 26 20   if( d>=nKey && 
1a158 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1a159 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f  lTypeLen(serial_
1a15a 74 79 70 65 29 3e 30 20 29 20 62 72 65 61 6b 3b  type)>0 ) break;
1a15b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d  .    pMem->enc =
1a15c 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a   pKeyInfo->enc;.
1a15d 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70      pMem->db = p
1a15e 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20  KeyInfo->db;.   
1a15f 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30   pMem->flags = 0
1a160 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c  ;.    pMem->zMal
1a161 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b  loc = 0;.    d +
1a162 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
1a163 69 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c  ialGet(&aKey[d],
1a164 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d   serial_type, pM
1a165 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b  em);.    pMem++;
1a166 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20  .    u++;.  }.  
1a167 61 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49  assert( u<=pKeyI
1a168 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20  nfo->nField + 1 
1a169 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d  );.  p->nField =
1a16a 20 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f   u;.  return (vo
1a16b 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  id*)p;.}../*.** 
1a16c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73  This routine des
1a16d 74 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64  troys a Unpacked
1a16e 52 65 63 6f 72 64 20 6f 62 6a 65 63 74 0a 2a 2f  Record object.*/
1a16f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a170 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1a171 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65  DeleteUnpackedRe
1a172 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63  cord(UnpackedRec
1a173 6f 72 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  ord *p){.  if( p
1a174 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66   ){.    if( p->f
1a175 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
1a176 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29 7b 0a  NEED_DESTROY ){.
1a177 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
1a178 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20     Mem *pMem;.  
1a179 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65      for(i=0, pMe
1a17a 6d 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e  m=p->aMem; i<p->
1a17b 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65  nField; i++, pMe
1a17c 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  m++){.        if
1a17d 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  ( pMem->zMalloc 
1a17e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1a17f 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1a180 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20  se(pMem);.      
1a181 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1a182 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61  }.    if( p->fla
1a183 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45  gs & UNPACKED_NE
1a184 45 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 20  ED_FREE ){.     
1a185 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
1a186 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20  ->pKeyInfo->db, 
1a187 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  p);.    }.  }.}.
1a188 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
1a189 74 69 6f 6e 20 63 6f 6d 70 61 72 65 73 20 74 68  tion compares th
1a18a 65 20 74 77 6f 20 74 61 62 6c 65 20 72 6f 77 73  e two table rows
1a18b 20 6f 72 20 69 6e 64 65 78 20 72 65 63 6f 72 64   or index record
1a18c 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62  s.** specified b
1a18d 79 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d  y {nKey1, pKey1}
1a18e 20 61 6e 64 20 70 50 4b 65 79 32 2e 20 20 49 74   and pPKey2.  It
1a18f 20 72 65 74 75 72 6e 73 20 61 20 6e 65 67 61 74   returns a negat
1a190 69 76 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20  ive, zero.** or 
1a191 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72  positive integer
1a192 20 69 66 20 6b 65 79 31 20 69 73 20 6c 65 73 73   if key1 is less
1a193 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20   than, equal to 
1a194 6f 72 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74  or .** greater t
1a195 68 61 6e 20 6b 65 79 32 2e 20 20 54 68 65 20 7b  han key2.  The {
1a196 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 6b 65  nKey1, pKey1} ke
1a197 79 20 6d 75 73 74 20 62 65 20 61 20 62 6c 6f 62  y must be a blob
1a198 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 74  .** created by t
1a199 68 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20  h OP_MakeRecord 
1a19a 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44  opcode of the VD
1a19b 42 45 2e 20 20 54 68 65 20 70 50 4b 65 79 32 0a  BE.  The pPKey2.
1a19c 2a 2a 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61  ** key must be a
1a19d 20 70 61 72 73 65 64 20 6b 65 79 20 73 75 63 68   parsed key such
1a19e 20 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   as obtained fro
1a19f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  m.** sqlite3Vdbe
1a1a0 50 61 72 73 65 52 65 63 6f 72 64 2e 0a 2a 2a 0a  ParseRecord..**.
1a1a1 2a 2a 20 4b 65 79 31 20 61 6e 64 20 4b 65 79 32  ** Key1 and Key2
1a1a2 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20   do not have to 
1a1a3 63 6f 6e 74 61 69 6e 20 74 68 65 20 73 61 6d 65  contain the same
1a1a4 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64   number of field
1a1a5 73 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 77 69  s..** The key wi
1a1a6 74 68 20 66 65 77 65 72 20 66 69 65 6c 64 73 20  th fewer fields 
1a1a7 69 73 20 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61  is usually compa
1a1a8 72 65 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68  res less than th
1a1a9 65 20 0a 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79  e .** longer key
1a1aa 2e 20 20 48 6f 77 65 76 65 72 20 69 66 20 74 68  .  However if th
1a1ab 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b  e UNPACKED_INCRK
1a1ac 45 59 20 66 6c 61 67 73 20 69 6e 20 70 50 4b 65  EY flags in pPKe
1a1ad 79 32 20 69 73 20 73 65 74 0a 2a 2a 20 61 6e 64  y2 is set.** and
1a1ae 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66   the common pref
1a1af 69 78 65 73 20 61 72 65 20 65 71 75 61 6c 2c 20  ixes are equal, 
1a1b0 74 68 65 6e 20 6b 65 79 31 20 69 73 20 6c 65 73  then key1 is les
1a1b1 73 20 74 68 61 6e 20 6b 65 79 32 2e 0a 2a 2a 20  s than key2..** 
1a1b2 4f 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b  Or if the UNPACK
1a1b3 45 44 5f 4d 41 54 43 48 5f 50 52 45 46 49 58 20  ED_MATCH_PREFIX 
1a1b4 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20  flag is set and 
1a1b5 74 68 65 20 70 72 65 66 69 78 65 73 20 61 72 65  the prefixes are
1a1b6 0a 2a 2a 20 65 71 75 61 6c 2c 20 74 68 65 6e 20  .** equal, then 
1a1b7 74 68 65 20 6b 65 79 73 20 61 72 65 20 63 6f 6e  the keys are con
1a1b8 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 65 71  sidered to be eq
1a1b9 75 61 6c 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70  ual and.** the p
1a1ba 61 72 74 73 20 62 65 79 6f 6e 64 20 74 68 65 20  arts beyond the 
1a1bb 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20 61 72  common prefix ar
1a1bc 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  e ignored..**.**
1a1bd 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44   If the UNPACKED
1a1be 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 66 6c  _IGNORE_ROWID fl
1a1bf 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  ag is set, then 
1a1c0 74 68 65 20 6c 61 73 74 20 62 79 74 65 20 6f 66  the last byte of
1a1c1 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 6f  .** the header o
1a1c2 66 20 70 4b 65 79 31 20 69 73 20 69 67 6e 6f 72  f pKey1 is ignor
1a1c3 65 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d  ed.  It is assum
1a1c4 65 64 20 74 68 61 74 20 70 4b 65 79 31 20 69 73  ed that pKey1 is
1a1c5 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 20 6b 65 79  .** an index key
1a1c6 2c 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 20  , and thus ends 
1a1c7 77 69 74 68 20 61 20 72 6f 77 69 64 20 76 61 6c  with a rowid val
1a1c8 75 65 2e 20 20 54 68 65 20 6c 61 73 74 20 62 79  ue.  The last by
1a1c9 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 68 65 61  te.** of the hea
1a1ca 64 65 72 20 77 69 6c 6c 20 74 68 65 72 65 66 6f  der will therefo
1a1cb 72 65 20 62 65 20 74 68 65 20 73 65 72 69 61 6c  re be the serial
1a1cc 20 74 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77   type of the row
1a1cd 69 64 3a 0a 2a 2a 20 6f 6e 65 20 6f 66 20 31 2c  id:.** one of 1,
1a1ce 20 32 2c 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20   2, 3, 4, 5, 6, 
1a1cf 38 2c 20 6f 72 20 39 20 2d 20 74 68 65 20 69 6e  8, or 9 - the in
1a1d0 74 65 67 65 72 20 73 65 72 69 61 6c 20 74 79 70  teger serial typ
1a1d1 65 73 2e 0a 2a 2a 20 54 68 65 20 73 65 72 69 61  es..** The seria
1a1d2 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 69  l type of the fi
1a1d3 6e 61 6c 20 72 6f 77 69 64 20 77 69 6c 6c 20 61  nal rowid will a
1a1d4 6c 77 61 79 73 20 62 65 20 61 20 73 69 6e 67 6c  lways be a singl
1a1d5 65 20 62 79 74 65 2e 0a 2a 2a 20 42 79 20 69 67  e byte..** By ig
1a1d6 6e 6f 72 69 6e 67 20 74 68 69 73 20 6c 61 73 74  noring this last
1a1d7 20 62 79 74 65 20 6f 66 20 74 68 65 20 68 65 61   byte of the hea
1a1d8 64 65 72 2c 20 77 65 20 66 6f 72 63 65 20 74 68  der, we force th
1a1d9 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20  e comparison.** 
1a1da 74 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 72 6f  to ignore the ro
1a1db 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f  wid at the end o
1a1dc 66 20 6b 65 79 31 2e 0a 2a 2f 0a 53 51 4c 49 54  f key1..*/.SQLIT
1a1dd 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1a1de 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
1a1df 6f 6d 70 61 72 65 28 0a 20 20 69 6e 74 20 6e 4b  ompare(.  int nK
1a1e0 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  ey1, const void 
1a1e1 2a 70 4b 65 79 31 2c 20 2f 2a 20 4c 65 66 74 20  *pKey1, /* Left 
1a1e2 6b 65 79 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65  key */.  Unpacke
1a1e3 64 52 65 63 6f 72 64 20 2a 70 50 4b 65 79 32 20  dRecord *pPKey2 
1a1e4 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20         /* Right 
1a1e5 6b 65 79 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  key */.){.  int 
1a1e6 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  d1;            /
1a1e7 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b  * Offset into aK
1a1e8 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64 61 74  ey[] of next dat
1a1e9 61 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75  a element */.  u
1a1ea 33 32 20 69 64 78 31 3b 20 20 20 20 20 20 20 20  32 idx1;        
1a1eb 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
1a1ec 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20   aKey[] of next 
1a1ed 68 65 61 64 65 72 20 65 6c 65 6d 65 6e 74 20 2a  header element *
1a1ee 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 31 3b 20  /.  u32 szHdr1; 
1a1ef 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1a1f0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 68 65 61   of bytes in hea
1a1f1 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  der */.  int i =
1a1f2 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64   0;.  int nField
1a1f3 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a  ;.  int rc = 0;.
1a1f4 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
1a1f5 20 63 68 61 72 20 2a 61 4b 65 79 31 20 3d 20 28   char *aKey1 = (
1a1f6 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1a1f7 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a 20 20 4b  har *)pKey1;.  K
1a1f8 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
1a1f9 3b 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b 0a 0a 20  ;.  Mem mem1;.. 
1a1fa 20 70 4b 65 79 49 6e 66 6f 20 3d 20 70 50 4b 65   pKeyInfo = pPKe
1a1fb 79 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  y2->pKeyInfo;.  
1a1fc 6d 65 6d 31 2e 65 6e 63 20 3d 20 70 4b 65 79 49  mem1.enc = pKeyI
1a1fd 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 31  nfo->enc;.  mem1
1a1fe 2e 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  .db = pKeyInfo->
1a1ff 64 62 3b 0a 20 20 6d 65 6d 31 2e 66 6c 61 67 73  db;.  mem1.flags
1a200 20 3d 20 30 3b 0a 20 20 6d 65 6d 31 2e 75 2e 69   = 0;.  mem1.u.i
1a201 20 3d 20 30 3b 20 20 2f 2a 20 6e 6f 74 20 6e 65   = 0;  /* not ne
1a202 65 64 65 64 2c 20 68 65 72 65 20 74 6f 20 73 69  eded, here to si
1a203 6c 65 6e 63 65 20 63 6f 6d 70 69 6c 65 72 20 77  lence compiler w
1a204 61 72 6e 69 6e 67 20 2a 2f 0a 20 20 6d 65 6d 31  arning */.  mem1
1a205 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20  .zMalloc = 0;.  
1a206 0a 20 20 69 64 78 31 20 3d 20 67 65 74 56 61 72  .  idx1 = getVar
1a207 69 6e 74 33 32 28 61 4b 65 79 31 2c 20 73 7a 48  int32(aKey1, szH
1a208 64 72 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48  dr1);.  d1 = szH
1a209 64 72 31 3b 0a 20 20 69 66 28 20 70 50 4b 65 79  dr1;.  if( pPKey
1a20a 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43  2->flags & UNPAC
1a20b 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44  KED_IGNORE_ROWID
1a20c 20 29 7b 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d   ){.    szHdr1--
1a20d 3b 0a 20 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d  ;.  }.  nField =
1a20e 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c   pKeyInfo->nFiel
1a20f 64 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31  d;.  while( idx1
1a210 3c 73 7a 48 64 72 31 20 26 26 20 69 3c 70 50 4b  <szHdr1 && i<pPK
1a211 65 79 32 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20  ey2->nField ){. 
1a212 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79     u32 serial_ty
1a213 70 65 31 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61  pe1;..    /* Rea
1a214 64 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70  d the serial typ
1a215 65 73 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20  es for the next 
1a216 65 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63 68 20  element in each 
1a217 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31  key. */.    idx1
1a218 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28   += getVarint32(
1a219 20 61 4b 65 79 31 2b 69 64 78 31 2c 20 73 65 72   aKey1+idx1, ser
1a21a 69 61 6c 5f 74 79 70 65 31 20 29 3b 0a 20 20 20  ial_type1 );.   
1a21b 20 69 66 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26   if( d1>=nKey1 &
1a21c 26 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  & sqlite3VdbeSer
1a21d 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61  ialTypeLen(seria
1a21e 6c 5f 74 79 70 65 31 29 3e 30 20 29 20 62 72 65  l_type1)>0 ) bre
1a21f 61 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72  ak;..    /* Extr
1a220 61 63 74 20 74 68 65 20 76 61 6c 75 65 73 20 74  act the values t
1a221 6f 20 62 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20  o be compared.. 
1a222 20 20 20 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20     */.    d1 += 
1a223 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1a224 6c 47 65 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c  lGet(&aKey1[d1],
1a225 20 73 65 72 69 61 6c 5f 74 79 70 65 31 2c 20 26   serial_type1, &
1a226 6d 65 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  mem1);..    /* D
1a227 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  o the comparison
1a228 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
1a229 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61   sqlite3MemCompa
1a22a 72 65 28 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79  re(&mem1, &pPKey
1a22b 32 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20  2->aMem[i],.    
1a22c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a22d 20 20 20 20 20 20 20 69 3c 6e 46 69 65 6c 64 20         i<nField 
1a22e 3f 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c  ? pKeyInfo->aCol
1a22f 6c 5b 69 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69  l[i] : 0);.    i
1a230 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20  f( rc!=0 ){.    
1a231 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1a232 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66     i++;.  }.  if
1a233 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 29  ( mem1.zMalloc )
1a234 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1a235 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a  elease(&mem1);..
1a236 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46    /* If the PREF
1a237 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69  IX_SEARCH flag i
1a238 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69  s set and all fi
1a239 65 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20  elds except the 
1a23a 66 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77 69 64  final.  ** rowid
1a23b 20 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61   field were equa
1a23c 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68  l, then clear th
1a23d 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20  e PREFIX_SEARCH 
1a23e 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20  flag and set .  
1a23f 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64  ** pPKey2->rowid
1a240 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
1a241 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64   the rowid field
1a242 20 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b 65 79   in (pKey1, nKey
1a243 31 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73  1)..  ** This is
1a244 20 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f   used by the OP_
1a245 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e  IsUnique opcode.
1a246 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 4b  .  */.  if( (pPK
1a247 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50  ey2->flags & UNP
1a248 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1a249 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65  RCH) && i==(pPKe
1a24a 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b  y2->nField-1) ){
1a24b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78  .    assert( idx
1a24c 31 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 63 20  1==szHdr1 && rc 
1a24d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  );.    assert( m
1a24e 65 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em1.flags & MEM_
1a24f 49 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b 65 79  Int );.    pPKey
1a250 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50  2->flags &= ~UNP
1a251 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1a252 52 43 48 3b 0a 20 20 20 20 70 50 4b 65 79 32 2d  RCH;.    pPKey2-
1a253 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e  >rowid = mem1.u.
1a254 69 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  i;.  }..  if( rc
1a255 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72 63  ==0 ){.    /* rc
1a256 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20 74  ==0 here means t
1a257 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 6b  hat one of the k
1a258 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66  eys ran out of f
1a259 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a 2a  ields and.    **
1a25a 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 20   all the fields 
1a25b 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e 74  up to that point
1a25c 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20   were equal. If 
1a25d 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43  the UNPACKED_INC
1a25e 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61 67  RKEY.    ** flag
1a25f 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62 72   is set, then br
1a260 65 61 6b 20 74 68 65 20 74 69 65 20 62 79 20 74  eak the tie by t
1a261 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73 20  reating key2 as 
1a262 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20 49  larger..    ** I
1a263 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 52  f the UPACKED_PR
1a264 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 20  EFIX_MATCH flag 
1a265 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 79  is set, then key
1a266 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 72  s with common pr
1a267 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61 72  efixes.    ** ar
1a268 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  e considered to 
1a269 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72  be equal.  Other
1a26a 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72  wise, the longer
1a26b 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 20   key is the .   
1a26c 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20   ** larger.  As 
1a26d 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20  it happens, the 
1a26e 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61  pPKey2 will alwa
1a26f 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72  ys be the longer
1a270 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65  .    ** if there
1a271 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65   is a difference
1a272 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1a273 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26   pPKey2->flags &
1a274 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45   UNPACKED_INCRKE
1a275 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  Y ){.      rc = 
1a276 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  -1;.    }else if
1a277 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20  ( pPKey2->flags 
1a278 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  & UNPACKED_PREFI
1a279 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 20  X_MATCH ){.     
1a27a 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 20   /* Leave rc==0 
1a27b 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  */.    }else if(
1a27c 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b 0a   idx1<szHdr1 ){.
1a27d 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20        rc = 1;.  
1a27e 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
1a27f 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f  pKeyInfo->aSortO
1a280 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 6e  rder && i<pKeyIn
1a281 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 20  fo->nField.     
1a282 20 20 20 20 20 20 20 20 20 20 26 26 20 70 4b 65            && pKe
1a283 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65  yInfo->aSortOrde
1a284 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d  r[i] ){.    rc =
1a285 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74   -rc;.  }..  ret
1a286 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a  urn rc;.}. ../*.
1a287 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61  ** pCur points a
1a288 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79  t an index entry
1a289 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74   created using t
1a28a 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  he OP_MakeRecord
1a28b 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61 64   opcode..** Read
1a28c 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 20   the rowid (the 
1a28d 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 68  last field in th
1a28e 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 74  e record) and st
1a28f 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 64  ore it in *rowid
1a290 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  ..** Return SQLI
1a291 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68  TE_OK if everyth
1a292 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61 6e  ing works, or an
1a293 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 65   error code othe
1a294 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75  rwise..**.** pCu
1a295 72 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74  r might be point
1a296 69 6e 67 20 74 6f 20 74 65 78 74 20 6f 62 74 61  ing to text obta
1a297 69 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f 72 72  ined from a corr
1a298 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c  upt database fil
1a299 65 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63 6f 6e  e..** So the con
1a29a 74 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 74  tent cannot be t
1a29b 72 75 73 74 65 64 2e 20 20 44 6f 20 61 70 70 72  rusted.  Do appr
1a29c 6f 70 72 69 61 74 65 20 63 68 65 63 6b 73 20 6f  opriate checks o
1a29d 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a  n the content..*
1a29e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a29f 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1a2a0 49 64 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f  IdxRowid(BtCurso
1a2a1 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f  r *pCur, i64 *ro
1a2a2 77 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c  wid){.  i64 nCel
1a2a3 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20  lKey = 0;.  int 
1a2a4 72 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b  rc;.  u32 szHdr;
1a2a5 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
1a2a6 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f  of the header */
1a2a7 0a 20 20 75 33 32 20 74 79 70 65 52 6f 77 69 64  .  u32 typeRowid
1a2a8 3b 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74  ;    /* Serial t
1a2a9 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64  ype of the rowid
1a2aa 20 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77   */.  u32 lenRow
1a2ab 69 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20  id;     /* Size 
1a2ac 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a  of the rowid */.
1a2ad 20 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 2f    Mem m, v;..  /
1a2ae 2a 20 47 65 74 20 74 68 65 20 73 69 7a 65 20 6f  * Get the size o
1a2af 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  f the index entr
1a2b0 79 2e 20 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73  y.  Only indices
1a2b1 20 65 6e 74 72 69 65 73 20 6f 66 20 6c 65 73 73   entries of less
1a2b2 0a 20 20 2a 2a 20 74 68 61 6e 20 32 47 69 42 20  .  ** than 2GiB 
1a2b3 61 72 65 20 73 75 70 70 6f 72 74 20 2d 20 61 6e  are support - an
1a2b4 79 74 68 69 6e 67 20 6c 61 72 67 65 20 6d 75 73  ything large mus
1a2b5 74 20 62 65 20 64 61 74 61 62 61 73 65 20 63 6f  t be database co
1a2b6 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 73 71  rruption */.  sq
1a2b7 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a  lite3BtreeKeySiz
1a2b8 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65  e(pCur, &nCellKe
1a2b9 79 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65  y);.  if( unlike
1a2ba 6c 79 28 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c  ly(nCellKey<=0 |
1a2bb 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66  | nCellKey>0x7ff
1a2bc 66 66 66 66 66 29 20 29 7b 0a 20 20 20 20 72 65  fffff) ){.    re
1a2bd 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1a2be 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20  UPT_BKPT;.  }.. 
1a2bf 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20   /* Read in the 
1a2c0 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74  complete content
1a2c1 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e   of the index en
1a2c2 74 72 79 20 2a 2f 0a 20 20 6d 2e 66 6c 61 67 73  try */.  m.flags
1a2c3 20 3d 20 30 3b 0a 20 20 6d 2e 64 62 20 3d 20 30   = 0;.  m.db = 0
1a2c4 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20  ;.  m.zMalloc = 
1a2c5 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  0;.  rc = sqlite
1a2c6 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
1a2c7 65 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29  e(pCur, 0, (int)
1a2c8 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29  nCellKey, 1, &m)
1a2c9 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
1a2ca 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
1a2cb 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78  ..  /* The index
1a2cc 20 65 6e 74 72 79 20 6d 75 73 74 20 62 65 67 69   entry must begi
1a2cd 6e 20 77 69 74 68 20 61 20 68 65 61 64 65 72 20  n with a header 
1a2ce 73 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69 64 29  size */.  (void)
1a2cf 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a  getVarint32((u8*
1a2d0 29 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a 20 20  )m.z, szHdr);.  
1a2d1 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d  testcase( szHdr=
1a2d2 3d 32 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  =2 );.  testcase
1a2d3 28 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a  ( szHdr==m.n );.
1a2d4 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 73    if( unlikely(s
1a2d5 7a 48 64 72 3c 32 20 7c 7c 20 28 69 6e 74 29 73  zHdr<2 || (int)s
1a2d6 7a 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20  zHdr>m.n) ){.   
1a2d7 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f   goto idx_rowid_
1a2d8 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a  corruption;.  }.
1a2d9 0a 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 66  .  /* The last f
1a2da 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65  ield of the inde
1a2db 78 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 69  x should be an i
1a2dc 6e 74 65 67 65 72 20 2d 20 74 68 65 20 52 4f 57  nteger - the ROW
1a2dd 49 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66 79 20  ID..  ** Verify 
1a2de 74 68 61 74 20 74 68 65 20 6c 61 73 74 20 65 6e  that the last en
1a2df 74 72 79 20 72 65 61 6c 6c 79 20 69 73 20 61 6e  try really is an
1a2e0 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 28   integer. */.  (
1a2e1 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32  void)getVarint32
1a2e2 28 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72  ((u8*)&m.z[szHdr
1a2e3 2d 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 29 3b  -1], typeRowid);
1a2e4 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1a2e5 65 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20 20 74  eRowid==1 );.  t
1a2e6 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1a2e7 69 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73 74 63  id==2 );.  testc
1a2e8 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1a2e9 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  3 );.  testcase(
1a2ea 20 74 79 70 65 52 6f 77 69 64 3d 3d 34 20 29 3b   typeRowid==4 );
1a2eb 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1a2ec 65 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20 20 74  eRowid==5 );.  t
1a2ed 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1a2ee 69 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73 74 63  id==6 );.  testc
1a2ef 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1a2f0 38 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  8 );.  testcase(
1a2f1 20 74 79 70 65 52 6f 77 69 64 3d 3d 39 20 29 3b   typeRowid==9 );
1a2f2 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28  .  if( unlikely(
1a2f3 74 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c 20 74  typeRowid<1 || t
1a2f4 79 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20 74 79  ypeRowid>9 || ty
1a2f5 70 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b 0a 20  peRowid==7) ){. 
1a2f6 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69     goto idx_rowi
1a2f7 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20  d_corruption;.  
1a2f8 7d 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73  }.  lenRowid = s
1a2f9 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1a2fa 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f 77 69  TypeLen(typeRowi
1a2fb 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20  d);.  testcase( 
1a2fc 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 3d 3d 73 7a  m.n-lenRowid==sz
1a2fd 48 64 72 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c  Hdr );.  if( unl
1a2fe 69 6b 65 6c 79 28 6d 2e 6e 2d 6c 65 6e 52 6f 77  ikely(m.n-lenRow
1a2ff 69 64 3c 73 7a 48 64 72 29 20 29 7b 0a 20 20 20  id<szHdr) ){.   
1a300 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f   goto idx_rowid_
1a301 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a  corruption;.  }.
1a302 0a 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65 20  .  /* Fetch the 
1a303 69 6e 74 65 67 65 72 20 6f 66 66 20 74 68 65 20  integer off the 
1a304 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78  end of the index
1a305 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71 6c   record */.  sql
1a306 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
1a307 74 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d  t((u8*)&m.z[m.n-
1a308 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 65 52  lenRowid], typeR
1a309 6f 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a 72 6f  owid, &v);.  *ro
1a30a 77 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 20 73  wid = v.u.i;.  s
1a30b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1a30c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75  ease(&m);.  retu
1a30d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20  rn SQLITE_OK;.. 
1a30e 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66   /* Jump here if
1a30f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
1a310 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64  tion is detected
1a311 20 61 66 74 65 72 20 6d 20 68 61 73 20 62 65 65   after m has bee
1a312 6e 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  n.  ** allocated
1a313 2e 20 20 46 72 65 65 20 74 68 65 20 6d 20 6f 62  .  Free the m ob
1a314 6a 65 63 74 20 61 6e 64 20 72 65 74 75 72 6e 20  ject and return 
1a315 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 20  SQLITE_CORRUPT. 
1a316 2a 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72  */.idx_rowid_cor
1a317 72 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73 74 63  ruption:.  testc
1a318 61 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d  ase( m.zMalloc!=
1a319 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  0 );.  sqlite3Vd
1a31a 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29  beMemRelease(&m)
1a31b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1a31c 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1a31d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
1a31e 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20   the key of the 
1a31f 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74  index entry that
1a320 20 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 6f   cursor pC is po
1a321 69 6e 74 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a  int to against.*
1a322 2a 20 74 68 65 20 6b 65 79 20 73 74 72 69 6e 67  * the key string
1a323 20 69 6e 20 70 4b 65 79 20 28 6f 66 20 6c 65 6e   in pKey (of len
1a324 67 74 68 20 6e 4b 65 79 29 2e 20 20 57 72 69 74  gth nKey).  Writ
1a325 65 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20 6e  e into *pRes a n
1a326 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69 73  umber.** that is
1a327 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c   negative, zero,
1a328 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20   or positive if 
1a329 70 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  pC is less than,
1a32a 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f 72   equal to,.** or
1a32b 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 4b   greater than pK
1a32c 65 79 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  ey.  Return SQLI
1a32d 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
1a32e 2e 0a 2a 2a 0a 2a 2a 20 70 4b 65 79 20 69 73 20  ..**.** pKey is 
1a32f 65 69 74 68 65 72 20 63 72 65 61 74 65 64 20 77  either created w
1a330 69 74 68 6f 75 74 20 61 20 72 6f 77 69 64 20 6f  ithout a rowid o
1a331 72 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 73  r is truncated s
1a332 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69  o that it.** omi
1a333 74 73 20 74 68 65 20 72 6f 77 69 64 20 61 74 20  ts the rowid at 
1a334 74 68 65 20 65 6e 64 2e 20 20 54 68 65 20 72 6f  the end.  The ro
1a335 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f  wid at the end o
1a336 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  f the index entr
1a337 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 20  y.** is ignored 
1a338 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c  as well.  Hence,
1a339 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   this routine on
1a33a 6c 79 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20  ly compares the 
1a33b 70 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20  prefixes .** of 
1a33c 74 68 65 20 6b 65 79 73 20 70 72 69 6f 72 20 74  the keys prior t
1a33d 6f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69  o the final rowi
1a33e 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74 69 72  d, not the entir
1a33f 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e  e key..**.** pUn
1a340 70 61 63 6b 65 64 20 6d 61 79 20 62 65 20 61 6e  packed may be an
1a341 20 75 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f   unpacked versio
1a342 6e 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 20  n of pKey,nKey. 
1a343 20 49 66 20 70 55 6e 70 61 63 6b 65 64 20 69 73   If pUnpacked is
1a344 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20 69 74 20  .** supplied it 
1a345 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  is used in place
1a346 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 0a 2a   of pKey,nKey..*
1a347 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a348 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1a349 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20  IdxKeyCompare(. 
1a34a 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 2c   VdbeCursor *pC,
1a34b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a34c 54 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f  The cursor to co
1a34d 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f  mpare against */
1a34e 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
1a34f 64 20 2a 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f  d *pUnpacked,  /
1a350 2a 20 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69  * Unpacked versi
1a351 6f 6e 20 6f 66 20 70 4b 65 79 20 61 6e 64 20 6e  on of pKey and n
1a352 4b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 65  Key */.  int *re
1a353 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
1a354 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
1a355 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73  e comparison res
1a356 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ult here */.){. 
1a357 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20   i64 nCellKey = 
1a358 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  0;.  int rc;.  B
1a359 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20  tCursor *pCur = 
1a35a 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 4d  pC->pCursor;.  M
1a35b 65 6d 20 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33  em m;..  sqlite3
1a35c 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75  BtreeKeySize(pCu
1a35d 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20  r, &nCellKey);. 
1a35e 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30   if( nCellKey<=0
1a35f 20 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37   || nCellKey>0x7
1a360 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 2a  fffffff ){.    *
1a361 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  res = 0;.    ret
1a362 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1a363 20 7d 0a 20 20 6d 2e 64 62 20 3d 20 30 3b 0a 20   }.  m.db = 0;. 
1a364 20 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20   m.flags = 0;.  
1a365 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  m.zMalloc = 0;. 
1a366 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1a367 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43  eMemFromBtree(pC
1a368 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 69  ->pCursor, 0, (i
1a369 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20  nt)nCellKey, 1, 
1a36a 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  &m);.  if( rc ){
1a36b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
1a36c 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55    }.  assert( pU
1a36d 6e 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26  npacked->flags &
1a36e 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45   UNPACKED_IGNORE
1a36f 5f 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73  _ROWID );.  *res
1a370 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1a371 63 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c  cordCompare(m.n,
1a372 20 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29   m.z, pUnpacked)
1a373 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1a374 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20  emRelease(&m);. 
1a375 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1a376 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  K;.}../*.** This
1a377 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
1a378 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65  e value to be re
1a379 74 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71  turned by subseq
1a37a 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a  uent calls to.**
1a37b 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
1a37c 28 29 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  () on the databa
1a37d 73 65 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20  se handle 'db'. 
1a37e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a37f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a380 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71  dbeSetChanges(sq
1a381 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e  lite3 *db, int n
1a382 43 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72  Change){.  asser
1a383 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1a384 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
1a385 20 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67   );.  db->nChang
1a386 65 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64  e = nChange;.  d
1a387 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20  b->nTotalChange 
1a388 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f  += nChange;.}../
1a389 2a 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20  *.** Set a flag 
1a38a 69 6e 20 74 68 65 20 76 64 62 65 20 74 6f 20 75  in the vdbe to u
1a38b 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65  pdate the change
1a38c 20 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74   counter when it
1a38d 20 69 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a   is finalised.**
1a38e 20 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51   or reset..*/.SQ
1a38f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a390 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75  d sqlite3VdbeCou
1a391 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a  ntChanges(Vdbe *
1a392 76 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43  v){.  v->changeC
1a393 6e 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  ntOn = 1;.}../*.
1a394 2a 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72  ** Mark every pr
1a395 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1a396 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1a397 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e   a database conn
1a398 65 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70  ection.** as exp
1a399 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  ired..**.** An e
1a39a 78 70 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74  xpired statement
1a39b 20 6d 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f   means that reco
1a39c 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65  mpilation of the
1a39d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a   statement is.**
1a39e 20 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61   recommend.  Sta
1a39f 74 65 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77  tements expire w
1a3a0 68 65 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65  hen things happe
1a3a1 6e 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69  n that make thei
1a3a2 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62  r.** programs ob
1a3a3 73 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e  solete.  Removin
1a3a4 67 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66  g user-defined f
1a3a5 75 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c  unctions or coll
1a3a6 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63  ating.** sequenc
1a3a7 65 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20  es, or changing 
1a3a8 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  an authorization
1a3a9 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68   function are th
1a3aa 65 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68  e types of.** th
1a3ab 69 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70  ings that make p
1a3ac 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1a3ad 74 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a  ts obsolete..*/.
1a3ae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a3af 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72  oid sqlite3Expir
1a3b0 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65  ePreparedStateme
1a3b1 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  nts(sqlite3 *db)
1a3b2 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66  {.  Vdbe *p;.  f
1a3b3 6f 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65  or(p = db->pVdbe
1a3b4 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
1a3b5 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64  {.    p->expired
1a3b6 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 1;.  }.}../*.
1a3b7 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61  ** Return the da
1a3b8 74 61 62 61 73 65 20 61 73 73 6f 63 69 61 74 65  tabase associate
1a3b9 64 20 77 69 74 68 20 74 68 65 20 56 64 62 65 2e  d with the Vdbe.
1a3ba 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a3bb 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  TE sqlite3 *sqli
1a3bc 74 65 33 56 64 62 65 44 62 28 56 64 62 65 20 2a  te3VdbeDb(Vdbe *
1a3bd 76 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e  v){.  return v->
1a3be 64 62 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  db;.}../********
1a3bf 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64  ****** End of vd
1a3c0 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  beaux.c ********
1a3c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3c3 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1a3c4 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1a3c5 65 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a  e vdbeapi.c ****
1a3c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3c8 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1a3c9 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54  4 May 26.**.** T
1a3ca 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1a3cb 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1a3cc 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1a3cd 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1a3ce 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1a3cf 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1a3d0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1a3d1 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1a3d2 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1a3d3 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1a3d4 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1a3d5 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1a3d6 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1a3d7 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1a3d8 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1a3d9 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1a3da 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1a3db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3df 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
1a3e0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1a3e1 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70   code use to imp
1a3e2 6c 65 6d 65 6e 74 20 41 50 49 73 20 74 68 61 74  lement APIs that
1a3e3 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65   are part of the
1a3e4 0a 2a 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20  .** VDBE..**.** 
1a3e5 24 49 64 3a 20 76 64 62 65 61 70 69 2e 63 2c 76  $Id: vdbeapi.c,v
1a3e6 20 31 2e 31 36 34 20 32 30 30 39 2f 30 34 2f 32   1.164 2009/04/2
1a3e7 37 20 31 38 3a 34 36 3a 30 36 20 64 72 68 20 45  7 18:46:06 drh E
1a3e8 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 30 20 26  xp $.*/..#if 0 &
1a3e9 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
1a3ea 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
1a3eb 41 4e 41 47 45 4d 45 4e 54 29 0a 2f 2a 0a 2a 2a  ANAGEMENT)./*.**
1a3ec 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
1a3ed 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
1a3ee 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  s pointers to th
1a3ef 65 20 65 6e 64 20 70 6f 69 6e 74 73 20 6f 66 20  e end points of 
1a3f0 61 0a 2a 2a 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b  a.** doubly-link
1a3f1 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63  ed list of all c
1a3f2 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74  ompiled SQL stat
1a3f3 65 6d 65 6e 74 73 20 74 68 61 74 20 6d 61 79 20  ements that may 
1a3f4 62 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 62 75  be holding.** bu
1a3f5 66 66 65 72 73 20 65 6c 69 67 69 62 6c 65 20 66  ffers eligible f
1a3f6 6f 72 20 72 65 6c 65 61 73 65 20 77 68 65 6e 20  or release when 
1a3f7 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  the sqlite3_rele
1a3f8 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74  ase_memory() int
1a3f9 65 72 66 61 63 65 20 69 73 0a 2a 2a 20 69 6e 76  erface is.** inv
1a3fa 6f 6b 65 64 2e 20 41 63 63 65 73 73 20 74 6f 20  oked. Access to 
1a3fb 74 68 69 73 20 6c 69 73 74 20 69 73 20 70 72 6f  this list is pro
1a3fc 74 65 63 74 65 64 20 62 79 20 74 68 65 20 53 51  tected by the SQ
1a3fd 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
1a3fe 43 5f 4c 52 55 32 0a 2a 2a 20 6d 75 74 65 78 2e  C_LRU2.** mutex.
1a3ff 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74  .**.** Statement
1a400 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74  s are added to t
1a401 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 6c  he end of this l
1a402 69 73 74 20 77 68 65 6e 20 73 71 6c 69 74 65 33  ist when sqlite3
1a403 5f 72 65 73 65 74 28 29 20 69 73 0a 2a 2a 20 63  _reset() is.** c
1a404 61 6c 6c 65 64 2e 20 54 68 65 79 20 61 72 65 20  alled. They are 
1a405 72 65 6d 6f 76 65 64 20 65 69 74 68 65 72 20 77  removed either w
1a406 68 65 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70  hen sqlite3_step
1a407 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69  () or sqlite3_fi
1a408 6e 61 6c 69 7a 65 28 29 0a 2a 2a 20 69 73 20 63  nalize().** is c
1a409 61 6c 6c 65 64 2e 20 57 68 65 6e 20 73 74 61 74  alled. When stat
1a40a 65 6d 65 6e 74 73 20 61 72 65 20 61 64 64 65 64  ements are added
1a40b 20 74 6f 20 74 68 69 73 20 6c 69 73 74 2c 20 74   to this list, t
1a40c 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a  he associated .*
1a40d 2a 20 72 65 67 69 73 74 65 72 20 61 72 72 61 79  * register array
1a40e 20 28 70 2d 3e 61 4d 65 6d 5b 31 2e 2e 70 2d 3e   (p->aMem[1..p->
1a40f 6e 4d 65 6d 5d 29 20 6d 61 79 20 63 6f 6e 74 61  nMem]) may conta
1a410 69 6e 20 64 79 6e 61 6d 69 63 20 62 75 66 66 65  in dynamic buffe
1a411 72 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62  rs that.** can b
1a412 65 20 66 72 65 65 64 20 75 73 69 6e 67 20 73 71  e freed using sq
1a413 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65  lite3VdbeRelease
1a414 4d 65 6d 6f 72 79 28 29 2e 0a 2a 2a 0a 2a 2a 20  Memory()..**.** 
1a415 57 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 73 20  When statements 
1a416 61 72 65 20 61 64 64 65 64 20 6f 72 20 72 65 6d  are added or rem
1a417 6f 76 65 64 20 66 72 6f 6d 20 74 68 69 73 20 6c  oved from this l
1a418 69 73 74 2c 20 74 68 65 20 6d 75 74 65 78 0a 2a  ist, the mutex.*
1a419 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
1a41a 68 20 74 68 65 20 56 64 62 65 20 62 65 69 6e 67  h the Vdbe being
1a41b 20 61 64 64 65 64 20 6f 72 20 72 65 6d 6f 76 65   added or remove
1a41c 64 20 28 56 64 62 65 2e 64 62 2d 3e 6d 75 74 65  d (Vdbe.db->mute
1a41d 78 29 20 69 73 0a 2a 2a 20 61 6c 72 65 61 64 79  x) is.** already
1a41e 20 68 65 6c 64 2e 20 54 68 65 20 4c 52 55 32 20   held. The LRU2 
1a41f 6d 75 74 65 78 20 69 73 20 74 68 65 6e 20 6f 62  mutex is then ob
1a420 74 61 69 6e 65 64 2c 20 62 6c 6f 63 6b 69 6e 67  tained, blocking
1a421 20 69 66 20 6e 65 63 65 73 73 61 72 79 2c 0a 2a   if necessary,.*
1a422 2a 20 74 68 65 20 6c 69 6e 6b 65 64 2d 6c 69 73  * the linked-lis
1a423 74 20 70 6f 69 6e 74 65 72 73 20 6d 61 6e 69 70  t pointers manip
1a424 75 6c 61 74 65 64 20 61 6e 64 20 74 68 65 20 4c  ulated and the L
1a425 52 55 32 20 6d 75 74 65 78 20 72 65 6c 69 6e 71  RU2 mutex relinq
1a426 75 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  uished..*/.struc
1a427 74 20 53 74 61 74 65 6d 65 6e 74 4c 72 75 4c 69  t StatementLruLi
1a428 73 74 20 7b 0a 20 20 56 64 62 65 20 2a 70 46 69  st {.  Vdbe *pFi
1a429 72 73 74 3b 0a 20 20 56 64 62 65 20 2a 70 4c 61  rst;.  Vdbe *pLa
1a42a 73 74 3b 0a 7d 3b 0a 73 74 61 74 69 63 20 73 74  st;.};.static st
1a42b 72 75 63 74 20 53 74 61 74 65 6d 65 6e 74 4c 72  ruct StatementLr
1a42c 75 4c 69 73 74 20 73 71 6c 69 74 65 33 4c 72 75  uList sqlite3Lru
1a42d 53 74 61 74 65 6d 65 6e 74 73 3b 0a 0a 2f 2a 0a  Statements;../*.
1a42e 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  ** Check that th
1a42f 65 20 6c 69 73 74 20 6c 6f 6f 6b 73 20 74 6f 20  e list looks to 
1a430 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f  be internally co
1a431 6e 73 69 73 74 65 6e 74 2e 20 54 68 69 73 20 69  nsistent. This i
1a432 73 20 75 73 65 64 0a 2a 2a 20 61 73 20 70 61 72  s used.** as par
1a433 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 28 29  t of an assert()
1a434 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20 66 6f   statement as fo
1a435 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61  llows:.**.**   a
1a436 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68  ssert( stmtLruCh
1a437 65 63 6b 28 29 20 29 3b 0a 2a 2f 0a 23 69 66 6e  eck() );.*/.#ifn
1a438 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69  def NDEBUG.stati
1a439 63 20 69 6e 74 20 73 74 6d 74 4c 72 75 43 68 65  c int stmtLruChe
1a43a 63 6b 28 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b  ck(){.  Vdbe *p;
1a43b 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33  .  for(p=sqlite3
1a43c 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46  LruStatements.pF
1a43d 69 72 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c  irst; p; p=p->pL
1a43e 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73  ruNext){.    ass
1a43f 65 72 74 28 70 2d 3e 70 4c 72 75 4e 65 78 74 20  ert(p->pLruNext 
1a440 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75  || p==sqlite3Lru
1a441 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74  Statements.pLast
1a442 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 70  );.    assert(!p
1a443 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 2d  ->pLruNext || p-
1a444 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50  >pLruNext->pLruP
1a445 72 65 76 3d 3d 70 29 3b 0a 20 20 20 20 61 73 73  rev==p);.    ass
1a446 65 72 74 28 70 2d 3e 70 4c 72 75 50 72 65 76 20  ert(p->pLruPrev 
1a447 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75  || p==sqlite3Lru
1a448 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73  Statements.pFirs
1a449 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21  t);.    assert(!
1a44a 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70  p->pLruPrev || p
1a44b 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75  ->pLruPrev->pLru
1a44c 4e 65 78 74 3d 3d 70 29 3b 0a 20 20 7d 0a 20 20  Next==p);.  }.  
1a44d 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
1a44e 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 76 64  if../*.** Add vd
1a44f 62 65 20 70 20 74 6f 20 74 68 65 20 65 6e 64 20  be p to the end 
1a450 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
1a451 20 6c 72 75 20 6c 69 73 74 2e 20 49 74 20 69 73   lru list. It is
1a452 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a   assumed that.**
1a453 20 70 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64   p is not alread
1a454 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69  y part of the li
1a455 73 74 20 77 68 65 6e 20 74 68 69 73 20 69 73 20  st when this is 
1a456 63 61 6c 6c 65 64 2e 20 54 68 65 20 6c 72 75 20  called. The lru 
1a457 6c 69 73 74 0a 2a 2a 20 69 73 20 70 72 6f 74 65  list.** is prote
1a458 63 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 49  cted by the SQLI
1a459 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
1a45a 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74  LRU mutex..*/.st
1a45b 61 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c 72  atic void stmtLr
1a45c 75 41 64 64 28 56 64 62 65 20 2a 70 29 7b 0a 20  uAdd(Vdbe *p){. 
1a45d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1a45e 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65  nter(sqlite3Mute
1a45f 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
1a460 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29  TEX_STATIC_LRU2)
1a461 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 70 4c 72  );..  if( p->pLr
1a462 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75  uPrev || p->pLru
1a463 4e 65 78 74 20 7c 7c 20 73 71 6c 69 74 65 33 4c  Next || sqlite3L
1a464 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69  ruStatements.pFi
1a465 72 73 74 3d 3d 70 20 29 7b 0a 20 20 20 20 73 71  rst==p ){.    sq
1a466 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1a467 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  e(sqlite3MutexAl
1a468 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
1a469 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a  _STATIC_LRU2));.
1a46a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
1a46b 0a 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c  .  assert( stmtL
1a46c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 0a 20 20  ruCheck() );..  
1a46d 69 66 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53  if( !sqlite3LruS
1a46e 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74  tatements.pFirst
1a46f 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1a470 21 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65  !sqlite3LruState
1a471 6d 65 6e 74 73 2e 70 4c 61 73 74 20 29 3b 0a 20  ments.pLast );. 
1a472 20 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61     sqlite3LruSta
1a473 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 3d  tements.pFirst =
1a474 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c   p;.    sqlite3L
1a475 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61  ruStatements.pLa
1a476 73 74 20 3d 20 70 3b 0a 20 20 7d 65 6c 73 65 7b  st = p;.  }else{
1a477 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71  .    assert( !sq
1a478 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e  lite3LruStatemen
1a479 74 73 2e 70 4c 61 73 74 2d 3e 70 4c 72 75 4e 65  ts.pLast->pLruNe
1a47a 78 74 20 29 3b 0a 20 20 20 20 70 2d 3e 70 4c 72  xt );.    p->pLr
1a47b 75 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33 4c  uPrev = sqlite3L
1a47c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61  ruStatements.pLa
1a47d 73 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c  st;.    sqlite3L
1a47e 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61  ruStatements.pLa
1a47f 73 74 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70  st->pLruNext = p
1a480 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72 75  ;.    sqlite3Lru
1a481 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74  Statements.pLast
1a482 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73   = p;.  }..  ass
1a483 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63  ert( stmtLruChec
1a484 6b 28 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65  k() );..  sqlite
1a485 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71  3_mutex_leave(sq
1a486 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
1a487 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
1a488 54 49 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a 0a 2f  TIC_LRU2));.}../
1a489 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68  *.** Assuming th
1a48a 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
1a48b 54 41 54 49 43 5f 4c 52 55 32 20 6d 75 74 65 78  TATIC_LRU2 mutex
1a48c 74 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c  t is already hel
1a48d 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20 73 74 61  d, remove.** sta
1a48e 74 65 6d 65 6e 74 20 70 20 66 72 6f 6d 20 74 68  tement p from th
1a48f 65 20 6c 65 61 73 74 2d 72 65 63 65 6e 74 6c 79  e least-recently
1a490 2d 75 73 65 64 20 73 74 61 74 65 6d 65 6e 74 20  -used statement 
1a491 6c 69 73 74 2e 20 49 66 20 74 68 65 20 0a 2a 2a  list. If the .**
1a492 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f   statement is no
1a493 74 20 63 75 72 72 65 6e 74 6c 79 20 70 61 72 74  t currently part
1a494 20 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68   of the list, th
1a495 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
1a496 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  op..*/.static vo
1a497 69 64 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65  id stmtLruRemove
1a498 4e 6f 6d 75 74 65 78 28 56 64 62 65 20 2a 70 29  Nomutex(Vdbe *p)
1a499 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 50  {.  if( p->pLruP
1a49a 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e 65  rev || p->pLruNe
1a49b 78 74 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33  xt || p==sqlite3
1a49c 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46  LruStatements.pF
1a49d 69 72 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65  irst ){.    asse
1a49e 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b  rt( stmtLruCheck
1a49f 28 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  () );.    if( p-
1a4a0 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20  >pLruNext ){.   
1a4a1 20 20 20 70 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e     p->pLruNext->
1a4a2 70 4c 72 75 50 72 65 76 20 3d 20 70 2d 3e 70 4c  pLruPrev = p->pL
1a4a3 72 75 50 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73  ruPrev;.    }els
1a4a4 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
1a4a5 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c  LruStatements.pL
1a4a6 61 73 74 20 3d 20 70 2d 3e 70 4c 72 75 50 72 65  ast = p->pLruPre
1a4a7 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  v;.    }.    if(
1a4a8 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 29 7b 0a   p->pLruPrev ){.
1a4a9 20 20 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65        p->pLruPre
1a4aa 76 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 2d  v->pLruNext = p-
1a4ab 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d  >pLruNext;.    }
1a4ac 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
1a4ad 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
1a4ae 2e 70 46 69 72 73 74 20 3d 20 70 2d 3e 70 4c 72  .pFirst = p->pLr
1a4af 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  uNext;.    }.   
1a4b0 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30   p->pLruNext = 0
1a4b1 3b 0a 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65  ;.    p->pLruPre
1a4b2 76 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72  v = 0;.    asser
1a4b3 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28  t( stmtLruCheck(
1a4b4 29 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ) );.  }.}../*.*
1a4b5 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 53  * Assuming the S
1a4b6 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
1a4b7 49 43 5f 4c 52 55 32 20 6d 75 74 65 78 74 20 69  IC_LRU2 mutext i
1a4b8 73 20 6e 6f 74 20 68 65 6c 64 2c 20 72 65 6d 6f  s not held, remo
1a4b9 76 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  ve.** statement 
1a4ba 70 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 73 74  p from the least
1a4bb 2d 72 65 63 65 6e 74 6c 79 2d 75 73 65 64 20 73  -recently-used s
1a4bc 74 61 74 65 6d 65 6e 74 20 6c 69 73 74 2e 20 49  tatement list. I
1a4bd 66 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d  f the .** statem
1a4be 65 6e 74 20 69 73 20 6e 6f 74 20 63 75 72 72 65  ent is not curre
1a4bf 6e 74 6c 79 20 70 61 72 74 20 6f 66 20 74 68 65  ntly part of the
1a4c0 20 6c 69 73 74 2c 20 74 68 69 73 20 63 61 6c 6c   list, this call
1a4c1 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
1a4c2 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 6d 74  static void stmt
1a4c3 4c 72 75 52 65 6d 6f 76 65 28 56 64 62 65 20 2a  LruRemove(Vdbe *
1a4c4 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  p){.  sqlite3_mu
1a4c5 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
1a4c6 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
1a4c7 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
1a4c8 4c 52 55 32 29 29 3b 0a 20 20 73 74 6d 74 4c 72  LRU2));.  stmtLr
1a4c9 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 70  uRemoveNomutex(p
1a4ca 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
1a4cb 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33  ex_leave(sqlite3
1a4cc 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
1a4cd 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
1a4ce 52 55 32 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  RU2));.}../*.** 
1a4cf 54 72 79 20 74 6f 20 72 65 6c 65 61 73 65 20 6e  Try to release n
1a4d0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
1a4d1 20 62 79 20 66 72 65 65 69 6e 67 20 62 75 66 66   by freeing buff
1a4d2 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 0a  ers associated .
1a4d3 2a 2a 20 77 69 74 68 20 74 68 65 20 6d 65 6d 6f  ** with the memo
1a4d4 72 79 20 72 65 67 69 73 74 65 72 73 20 6f 66 20  ry registers of 
1a4d5 63 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64  currently unused
1a4d6 20 76 64 62 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   vdbes..*/.SQLIT
1a4d7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1a4d8 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65  lite3VdbeRelease
1a4d9 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 20  Memory(int n){. 
1a4da 20 56 64 62 65 20 2a 70 3b 0a 20 20 56 64 62 65   Vdbe *p;.  Vdbe
1a4db 20 2a 70 4e 65 78 74 3b 0a 20 20 69 6e 74 20 6e   *pNext;.  int n
1a4dc 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20 73 71 6c  Free = 0;..  sql
1a4dd 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1a4de 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
1a4df 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
1a4e0 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20  STATIC_LRU2));. 
1a4e1 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 4c 72   for(p=sqlite3Lr
1a4e2 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72  uStatements.pFir
1a4e3 73 74 3b 20 70 20 26 26 20 6e 46 72 65 65 3c 6e  st; p && nFree<n
1a4e4 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20  ; p=pNext){.    
1a4e5 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4c 72 75 4e  pNext = p->pLruN
1a4e6 65 78 74 3b 0a 0a 20 20 20 20 2f 2a 20 46 6f 72  ext;..    /* For
1a4e7 20 65 61 63 68 20 73 74 61 74 65 6d 65 6e 74 20   each statement 
1a4e8 68 61 6e 64 6c 65 20 69 6e 20 74 68 65 20 6c 72  handle in the lr
1a4e9 75 20 6c 69 73 74 2c 20 61 74 74 65 6d 70 74 20  u list, attempt 
1a4ea 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 0a 20 20  to obtain the.  
1a4eb 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20    ** associated 
1a4ec 64 61 74 61 62 61 73 65 20 6d 75 74 65 78 2e 20  database mutex. 
1a4ed 49 66 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20  If it cannot be 
1a4ee 6f 62 74 61 69 6e 65 64 2c 20 63 6f 6e 74 69 6e  obtained, contin
1a4ef 75 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  ue.    ** to the
1a4f0 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20   next statement 
1a4f1 68 61 6e 64 6c 65 2e 20 49 74 20 69 73 20 6e 6f  handle. It is no
1a4f2 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 62 6c  t possible to bl
1a4f3 6f 63 6b 20 6f 6e 0a 20 20 20 20 2a 2a 20 74 68  ock on.    ** th
1a4f4 65 20 64 61 74 61 62 61 73 65 20 6d 75 74 65 78  e database mutex
1a4f5 20 2d 20 74 68 61 74 20 63 6f 75 6c 64 20 63 61   - that could ca
1a4f6 75 73 65 20 64 65 61 64 6c 6f 63 6b 2e 0a 20 20  use deadlock..  
1a4f7 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c    */.    if( SQL
1a4f8 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 5f  ITE_OK==sqlite3_
1a4f9 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 64 62 2d  mutex_try(p->db-
1a4fa 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 20  >mutex) ){.     
1a4fb 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65   nFree += sqlite
1a4fc 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 66  3VdbeReleaseBuff
1a4fd 65 72 73 28 70 29 3b 0a 20 20 20 20 20 20 73 74  ers(p);.      st
1a4fe 6d 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74  mtLruRemoveNomut
1a4ff 65 78 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c  ex(p);.      sql
1a500 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1a501 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1a502 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
1a503 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1a504 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
1a505 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
1a506 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 0a 20  TATIC_LRU2));.. 
1a507 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d   return nFree;.}
1a508 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c  ../*.** Call sql
1a509 69 74 65 33 52 65 70 72 65 70 61 72 65 28 29 20  ite3Reprepare() 
1a50a 6f 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  on the statement
1a50b 2e 20 52 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d  . Remove it from
1a50c 20 74 68 65 0a 2a 2a 20 6c 72 75 20 6c 69 73 74   the.** lru list
1a50d 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f   before doing so
1a50e 2c 20 61 73 20 52 65 70 72 65 70 61 72 65 28 29  , as Reprepare()
1a50f 20 77 69 6c 6c 20 66 72 65 65 20 61 6c 6c 20 74   will free all t
1a510 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 65 67  he.** memory reg
1a511 69 73 74 65 72 20 62 75 66 66 65 72 73 20 61 6e  ister buffers an
1a512 79 77 61 79 2e 0a 2a 2f 0a 69 6e 74 20 76 64 62  yway..*/.int vdb
1a513 65 52 65 70 72 65 70 61 72 65 28 56 64 62 65 20  eReprepare(Vdbe 
1a514 2a 70 29 7b 0a 20 20 73 74 6d 74 4c 72 75 52 65  *p){.  stmtLruRe
1a515 6d 6f 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  move(p);.  retur
1a516 6e 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61  n sqlite3Reprepa
1a517 72 65 28 70 29 3b 0a 7d 0a 0a 23 65 6c 73 65 20  re(p);.}..#else 
1a518 20 20 20 20 20 20 2f 2a 20 21 53 51 4c 49 54 45        /* !SQLITE
1a519 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
1a51a 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 20 20 23  ANAGEMENT */.  #
1a51b 64 65 66 69 6e 65 20 73 74 6d 74 4c 72 75 52 65  define stmtLruRe
1a51c 6d 6f 76 65 28 78 29 0a 20 20 23 64 65 66 69 6e  move(x).  #defin
1a51d 65 20 73 74 6d 74 4c 72 75 41 64 64 28 78 29 0a  e stmtLruAdd(x).
1a51e 20 20 23 64 65 66 69 6e 65 20 76 64 62 65 52 65    #define vdbeRe
1a51f 70 72 65 70 61 72 65 28 78 29 20 73 71 6c 69 74  prepare(x) sqlit
1a520 65 33 52 65 70 72 65 70 61 72 65 28 78 29 0a 23  e3Reprepare(x).#
1a521 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20  endif...#ifndef 
1a522 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
1a523 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74  ECATED./*.** Ret
1a524 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65  urn TRUE (non-ze
1a525 72 6f 29 20 6f 66 20 74 68 65 20 73 74 61 74 65  ro) of the state
1a526 6d 65 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73  ment supplied as
1a527 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65   an argument nee
1a528 64 73 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f  ds.** to be reco
1a529 6d 70 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65  mpiled.  A state
1a52a 6d 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65  ment needs to be
1a52b 20 72 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e   recompiled when
1a52c 65 76 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63  ever the.** exec
1a52d 75 74 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e  ution environmen
1a52e 74 20 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77  t changes in a w
1a52f 61 79 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c  ay that would al
1a530 74 65 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a  ter the program.
1a531 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f  ** that sqlite3_
1a532 70 72 65 70 61 72 65 28 29 20 67 65 6e 65 72 61  prepare() genera
1a533 74 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  tes.  For exampl
1a534 65 2c 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69  e, if new functi
1a535 6f 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74  ons or.** collat
1a536 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72  ing sequences ar
1a537 65 20 72 65 67 69 73 74 65 72 65 64 20 6f 72 20  e registered or 
1a538 69 66 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72  if an authorizer
1a539 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20   function is.** 
1a53a 61 64 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64  added or changed
1a53b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1a53c 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69  int sqlite3_expi
1a53d 72 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  red(sqlite3_stmt
1a53e 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65   *pStmt){.  Vdbe
1a53f 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74   *p = (Vdbe*)pSt
1a540 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d  mt;.  return p==
1a541 30 20 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b  0 || p->expired;
1a542 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1a543 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
1a544 6f 75 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20  outine destroys 
1a545 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  a virtual machin
1a546 65 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65  e that is create
1a547 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69  d by.** the sqli
1a548 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f  te3_compile() ro
1a549 75 74 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67  utine. The integ
1a54a 65 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 61  er returned is a
1a54b 6e 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63  n SQLITE_.** suc
1a54c 63 65 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64  cess/failure cod
1a54d 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
1a54e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65   the result of e
1a54f 78 65 63 75 74 69 6e 67 20 74 68 65 20 76 69 72  xecuting the vir
1a550 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e  tual.** machine.
1a551 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1a552 69 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72  ine sets the err
1a553 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69  or code and stri
1a554 6e 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  ng returned by.*
1a555 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  * sqlite3_errcod
1a556 65 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72  e(), sqlite3_err
1a557 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  msg() and sqlite
1a558 33 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f  3_errmsg16()..*/
1a559 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1a55a 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
1a55b 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1a55c 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Stmt){.  int rc;
1a55d 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20  .  if( pStmt==0 
1a55e 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
1a55f 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
1a560 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56      Vdbe *v = (V
1a561 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20  dbe*)pStmt;.    
1a562 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d  sqlite3 *db = v-
1a563 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  >db;.#if SQLITE_
1a564 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73  THREADSAFE.    s
1a565 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
1a566 74 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74  tex = v->db->mut
1a567 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73  ex;.#endif.    s
1a568 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1a569 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73  er(mutex);.    s
1a56a 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 76 29 3b  tmtLruRemove(v);
1a56b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1a56c 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29  3VdbeFinalize(v)
1a56d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1a56e 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
1a56f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
1a570 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
1a571 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1a572 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72  rc;.}../*.** Ter
1a573 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 65  minate the curre
1a574 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  nt execution of 
1a575 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
1a576 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a   and reset it.**
1a577 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61   back to its sta
1a578 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74  rting state so t
1a579 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65  hat it can be re
1a57a 75 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 20  used. A success 
1a57b 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  code from.** the
1a57c 20 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e   prior execution
1a57d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
1a57e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1a57f 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20   sets the error 
1a580 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20  code and string 
1a581 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
1a582 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
1a583 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
1a584 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65  () and sqlite3_e
1a585 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51  rrmsg16()..*/.SQ
1a586 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1a587 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74  ite3_reset(sqlit
1a588 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1a589 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
1a58a 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20   pStmt==0 ){.   
1a58b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1a58c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64  .  }else{.    Vd
1a58d 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70  be *v = (Vdbe*)p
1a58e 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Stmt;.    sqlite
1a58f 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 2d  3_mutex_enter(v-
1a590 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
1a591 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1a592 65 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20 73  eReset(v);.    s
1a593 74 6d 74 4c 72 75 41 64 64 28 76 29 3b 0a 20 20  tmtLruAdd(v);.  
1a594 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b    sqlite3VdbeMak
1a595 65 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c  eReady(v, -1, 0,
1a596 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65   0, 0);.    asse
1a597 72 74 28 20 28 72 63 20 26 20 28 76 2d 3e 64 62  rt( (rc & (v->db
1a598 2d 3e 65 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20  ->errMask))==rc 
1a599 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1a59a 74 65 33 41 70 69 45 78 69 74 28 76 2d 3e 64 62  te3ApiExit(v->db
1a59b 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74  , rc);.    sqlit
1a59c 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 76  e3_mutex_leave(v
1a59d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1a59e 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1a59f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 6c 6c 20  ../*.** Set all 
1a5a0 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69  the parameters i
1a5a1 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 53  n the compiled S
1a5a2 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  QL statement to 
1a5a3 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
1a5a4 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1a5a5 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73  clear_bindings(s
1a5a6 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1a5a7 6d 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  mt){.  int i;.  
1a5a8 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1a5a9 4f 4b 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  OK;.  Vdbe *p = 
1a5aa 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 23 69  (Vdbe*)pStmt;.#i
1a5ab 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
1a5ac 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  AFE.  sqlite3_mu
1a5ad 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 28 28 56  tex *mutex = ((V
1a5ae 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 2d  dbe*)pStmt)->db-
1a5af 3e 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20  >mutex;.#endif. 
1a5b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1a5b1 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66  nter(mutex);.  f
1a5b2 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61  or(i=0; i<p->nVa
1a5b3 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  r; i++){.    sql
1a5b4 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1a5b5 73 65 28 26 70 2d 3e 61 56 61 72 5b 69 5d 29 3b  se(&p->aVar[i]);
1a5b6 0a 20 20 20 20 70 2d 3e 61 56 61 72 5b 69 5d 2e  .    p->aVar[i].
1a5b7 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1a5b8 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
1a5b9 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
1a5ba 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
1a5bb 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}.../**********
1a5bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a5bd 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
1a5be 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  _  *************
1a5bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a5c0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
1a5c1 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 65 78 74  ing routines ext
1a5c2 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ract information
1a5c3 20 66 72 6f 6d 20 61 20 4d 65 6d 20 6f 72 20 73   from a Mem or s
1a5c4 71 6c 69 74 65 33 5f 76 61 6c 75 65 0a 2a 2a 20  qlite3_value.** 
1a5c5 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
1a5c6 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
1a5c7 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  oid *sqlite3_val
1a5c8 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  ue_blob(sqlite3_
1a5c9 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
1a5ca 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70  Mem *p = (Mem*)p
1a5cb 56 61 6c 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c  Val;.  if( p->fl
1a5cc 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c  ags & (MEM_Blob|
1a5cd 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20  MEM_Str) ){.    
1a5ce 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78  sqlite3VdbeMemEx
1a5cf 70 61 6e 64 42 6c 6f 62 28 70 29 3b 0a 20 20 20  pandBlob(p);.   
1a5d0 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45   p->flags &= ~ME
1a5d1 4d 5f 53 74 72 3b 0a 20 20 20 20 70 2d 3e 66 6c  M_Str;.    p->fl
1a5d2 61 67 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b  ags |= MEM_Blob;
1a5d3 0a 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 7a  .    return p->z
1a5d4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
1a5d5 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61  eturn sqlite3_va
1a5d6 6c 75 65 5f 74 65 78 74 28 70 56 61 6c 29 3b 0a  lue_text(pVal);.
1a5d7 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49    }.}.SQLITE_API
1a5d8 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
1a5d9 75 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  ue_bytes(sqlite3
1a5da 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20  _value *pVal){. 
1a5db 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1a5dc 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20  alueBytes(pVal, 
1a5dd 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a  SQLITE_UTF8);.}.
1a5de 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1a5df 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
1a5e0 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c  es16(sqlite3_val
1a5e1 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue *pVal){.  ret
1a5e2 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65  urn sqlite3Value
1a5e3 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49  Bytes(pVal, SQLI
1a5e4 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b  TE_UTF16NATIVE);
1a5e5 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f  .}.SQLITE_API do
1a5e6 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  uble sqlite3_val
1a5e7 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  ue_double(sqlite
1a5e8 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
1a5e9 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1a5ea 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 28 4d  VdbeRealValue((M
1a5eb 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c  em*)pVal);.}.SQL
1a5ec 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1a5ed 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71  te3_value_int(sq
1a5ee 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1a5ef 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e  l){.  return (in
1a5f0 74 29 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  t)sqlite3VdbeInt
1a5f1 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c  Value((Mem*)pVal
1a5f2 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1a5f3 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c  sqlite_int64 sql
1a5f4 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
1a5f5 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1a5f6 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
1a5f7 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1a5f8 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b  lue((Mem*)pVal);
1a5f9 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  .}.SQLITE_API co
1a5fa 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1a5fb 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r *sqlite3_value
1a5fc 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61  _text(sqlite3_va
1a5fd 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65  lue *pVal){.  re
1a5fe 74 75 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73 69  turn (const unsi
1a5ff 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69  gned char *)sqli
1a600 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
1a601 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
1a602 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1a603 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1a604 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1a605 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  id *sqlite3_valu
1a606 65 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33  e_text16(sqlite3
1a607 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20  _value* pVal){. 
1a608 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1a609 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53  alueText(pVal, S
1a60a 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
1a60b 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  E);.}.SQLITE_API
1a60c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
1a60d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1a60e 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  6be(sqlite3_valu
1a60f 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
1a610 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54  rn sqlite3ValueT
1a611 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45  ext(pVal, SQLITE
1a612 5f 55 54 46 31 36 42 45 29 3b 0a 7d 0a 53 51 4c  _UTF16BE);.}.SQL
1a613 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1a614 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  id *sqlite3_valu
1a615 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74  e_text16le(sqlit
1a616 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
1a617 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1a618 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c  3ValueText(pVal,
1a619 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29   SQLITE_UTF16LE)
1a61a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1a61b 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
1a61c 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1a61d 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
1a61e 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  type(sqlite3_val
1a61f 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue* pVal){.  ret
1a620 75 72 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b 0a  urn pVal->type;.
1a621 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
1a622 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a623 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a624 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    **************
1a625 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a626 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1a627 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  ng routines are 
1a628 75 73 65 64 20 62 79 20 75 73 65 72 2d 64 65 66  used by user-def
1a629 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74  ined functions t
1a62a 6f 20 73 70 65 63 69 66 79 0a 2a 2a 20 74 68 65  o specify.** the
1a62b 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74   function result
1a62c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1a62d 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1a62e 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69  ult_blob(.  sqli
1a62f 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1a630 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  x, .  const void
1a631 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a   *z, .  int n, .
1a632 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
1a633 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65  oid *).){.  asse
1a634 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73  rt( n>=0 );.  as
1a635 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1a636 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1a637 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1a638 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1a639 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
1a63a 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a  z, n, 0, xDel);.
1a63b 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  }.SQLITE_API voi
1a63c 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1a63d 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f  _double(sqlite3_
1a63e 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 64  context *pCtx, d
1a63f 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20 61  ouble rVal){.  a
1a640 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1a641 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1a642 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1a643 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1a644 53 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 2d  SetDouble(&pCtx-
1a645 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c  >s, rVal);.}.SQL
1a646 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1a647 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
1a648 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  r(sqlite3_contex
1a649 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 63  t *pCtx, const c
1a64a 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a  har *z, int n){.
1a64b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1a64c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1a64d 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1a64e 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72  );.  pCtx->isErr
1a64f 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  or = SQLITE_ERRO
1a650 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  R;.  sqlite3Vdbe
1a651 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d  MemSetStr(&pCtx-
1a652 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45  >s, z, n, SQLITE
1a653 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52  _UTF8, SQLITE_TR
1a654 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e  ANSIENT);.}.#ifn
1a655 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a656 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
1a657 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1a658 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c  sult_error16(sql
1a659 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1a65a 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  tx, const void *
1a65b 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73  z, int n){.  ass
1a65c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1a65d 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1a65e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1a65f 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20  pCtx->isError = 
1a660 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1a661 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1a662 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a  tStr(&pCtx->s, z
1a663 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  , n, SQLITE_UTF1
1a664 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f  6NATIVE, SQLITE_
1a665 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65  TRANSIENT);.}.#e
1a666 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20  ndif.SQLITE_API 
1a667 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1a668 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f  ult_int(sqlite3_
1a669 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69  context *pCtx, i
1a66a 6e 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65  nt iVal){.  asse
1a66b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1a66c 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1a66d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
1a66e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1a66f 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20  Int64(&pCtx->s, 
1a670 28 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 51  (i64)iVal);.}.SQ
1a671 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1a672 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
1a673 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  64(sqlite3_conte
1a674 78 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69 56  xt *pCtx, i64 iV
1a675 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  al){.  assert( s
1a676 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1a677 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
1a678 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
1a679 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34  3VdbeMemSetInt64
1a67a 28 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c 29  (&pCtx->s, iVal)
1a67b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
1a67c 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1a67d 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f  lt_null(sqlite3_
1a67e 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a  context *pCtx){.
1a67f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1a680 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1a681 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1a682 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1a683 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78  MemSetNull(&pCtx
1a684 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  ->s);.}.SQLITE_A
1a685 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1a686 72 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20 73  result_text(.  s
1a687 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a688 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 63  pCtx, .  const c
1a689 68 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e  har *z, .  int n
1a68a 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29  ,.  void (*xDel)
1a68b 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73  (void *).){.  as
1a68c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1a68d 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1a68e 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1a68f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1a690 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
1a691 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
1a692 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 69 66 6e  8, xDel);.}.#ifn
1a693 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a694 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
1a695 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1a696 73 75 6c 74 5f 74 65 78 74 31 36 28 0a 20 20 73  sult_text16(.  s
1a697 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a698 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76  pCtx, .  const v
1a699 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e  oid *z, .  int n
1a69a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  , .  void (*xDel
1a69b 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61  )(void *).){.  a
1a69c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1a69d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1a69e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1a69f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1a6a0 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
1a6a1 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
1a6a2 46 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29  F16NATIVE, xDel)
1a6a3 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
1a6a4 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1a6a5 6c 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73  lt_text16be(.  s
1a6a6 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a6a7 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76  pCtx, .  const v
1a6a8 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e  oid *z, .  int n
1a6a9 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  , .  void (*xDel
1a6aa 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61  )(void *).){.  a
1a6ab 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1a6ac 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1a6ad 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1a6ae 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1a6af 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
1a6b0 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
1a6b1 46 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a  F16BE, xDel);.}.
1a6b2 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1a6b3 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
1a6b4 65 78 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74  ext16le(.  sqlit
1a6b5 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1a6b6 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
1a6b7 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
1a6b8 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
1a6b9 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72  id *).){.  asser
1a6ba 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1a6bb 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1a6bc 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
1a6bd 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
1a6be 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20  tr(&pCtx->s, z, 
1a6bf 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  n, SQLITE_UTF16L
1a6c0 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 65 6e 64  E, xDel);.}.#end
1a6c1 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1a6c2 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54  T_UTF16 */.SQLIT
1a6c3 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1a6c4 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
1a6c5 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1a6c6 2a 70 43 74 78 2c 20 73 71 6c 69 74 65 33 5f 76  *pCtx, sqlite3_v
1a6c7 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20  alue *pValue){. 
1a6c8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1a6c9 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
1a6ca 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
1a6cb 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1a6cc 65 6d 43 6f 70 79 28 26 70 43 74 78 2d 3e 73 2c  emCopy(&pCtx->s,
1a6cd 20 70 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49   pValue);.}.SQLI
1a6ce 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1a6cf 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62  te3_result_zerob
1a6d0 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  lob(sqlite3_cont
1a6d1 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 6e  ext *pCtx, int n
1a6d2 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
1a6d3 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1a6d4 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
1a6d5 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
1a6d6 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f  dbeMemSetZeroBlo
1a6d7 62 28 26 70 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a  b(&pCtx->s, n);.
1a6d8 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  }.SQLITE_API voi
1a6d9 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1a6da 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69  _error_code(sqli
1a6db 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1a6dc 78 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 7b  x, int errCode){
1a6dd 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72  .  pCtx->isError
1a6de 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 69 66   = errCode;.  if
1a6df 28 20 70 43 74 78 2d 3e 73 2e 66 6c 61 67 73 20  ( pCtx->s.flags 
1a6e0 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
1a6e1 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1a6e2 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
1a6e3 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 65   sqlite3ErrStr(e
1a6e4 72 72 43 6f 64 65 29 2c 20 2d 31 2c 20 0a 20 20  rrCode), -1, .  
1a6e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6e6 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54         SQLITE_UT
1a6e7 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  F8, SQLITE_STATI
1a6e8 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 46 6f  C);.  }.}../* Fo
1a6e9 72 63 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f  rce an SQLITE_TO
1a6ea 4f 42 49 47 20 65 72 72 6f 72 2e 20 2a 2f 0a 53  OBIG error. */.S
1a6eb 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1a6ec 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1a6ed 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74  ror_toobig(sqlit
1a6ee 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1a6ef 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
1a6f0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1a6f1 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
1a6f2 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73  x) );.  pCtx->is
1a6f3 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 54  Error = SQLITE_T
1a6f4 4f 4f 42 49 47 3b 0a 20 20 73 71 6c 69 74 65 33  OOBIG;.  sqlite3
1a6f5 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70  VdbeMemSetStr(&p
1a6f6 43 74 78 2d 3e 73 2c 20 22 73 74 72 69 6e 67 20  Ctx->s, "string 
1a6f7 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22  or blob too big"
1a6f8 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20  , -1, .         
1a6f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
1a6fa 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
1a6fb 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a  E_STATIC);.}../*
1a6fc 20 41 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   An SQLITE_NOMEM
1a6fd 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54   error. */.SQLIT
1a6fe 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1a6ff 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
1a700 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f  nomem(sqlite3_co
1a701 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20  ntext *pCtx){.  
1a702 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1a703 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1a704 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1a705 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1a706 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e  mSetNull(&pCtx->
1a707 73 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72  s);.  pCtx->isEr
1a708 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  ror = SQLITE_NOM
1a709 45 4d 3b 0a 20 20 70 43 74 78 2d 3e 73 2e 64 62  EM;.  pCtx->s.db
1a70a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1a70b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65   1;.}../*.** Exe
1a70c 63 75 74 65 20 74 68 65 20 73 74 61 74 65 6d 65  cute the stateme
1a70d 6e 74 20 70 53 74 6d 74 2c 20 65 69 74 68 65 72  nt pStmt, either
1a70e 20 75 6e 74 69 6c 20 61 20 72 6f 77 20 6f 66 20   until a row of 
1a70f 64 61 74 61 20 69 73 20 72 65 61 64 79 2c 20 74  data is ready, t
1a710 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  he.** statement 
1a711 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78  is completely ex
1a712 65 63 75 74 65 64 20 6f 72 20 61 6e 20 65 72 72  ecuted or an err
1a713 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a  or occurs..**.**
1a714 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d   This routine im
1a715 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 62 75 6c  plements the bul
1a716 6b 20 6f 66 20 74 68 65 20 6c 6f 67 69 63 20 62  k of the logic b
1a717 65 68 69 6e 64 20 74 68 65 20 73 71 6c 69 74 65  ehind the sqlite
1a718 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 2e 20  _step().** API. 
1a719 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   The only thing 
1a71a 6f 6d 69 74 74 65 64 20 69 73 20 74 68 65 20 61  omitted is the a
1a71b 75 74 6f 6d 61 74 69 63 20 72 65 63 6f 6d 70 69  utomatic recompi
1a71c 6c 65 20 69 66 20 61 20 0a 2a 2a 20 73 63 68 65  le if a .** sche
1a71d 6d 61 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63  ma change has oc
1a71e 63 75 72 72 65 64 2e 20 20 54 68 61 74 20 64 65  curred.  That de
1a71f 74 61 69 6c 20 69 73 20 68 61 6e 64 6c 65 64 20  tail is handled 
1a720 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 20  by the.** outer 
1a721 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 77  sqlite3_step() w
1a722 72 61 70 70 65 72 20 70 72 6f 63 65 64 75 72 65  rapper procedure
1a723 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1a724 73 71 6c 69 74 65 33 53 74 65 70 28 56 64 62 65  sqlite3Step(Vdbe
1a725 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20   *p){.  sqlite3 
1a726 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  *db;.  int rc;..
1a727 20 20 61 73 73 65 72 74 28 70 29 3b 0a 20 20 69    assert(p);.  i
1a728 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42  f( p->magic!=VDB
1a729 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20  E_MAGIC_RUN ){. 
1a72a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1a72b 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20  _MISUSE;.  }..  
1a72c 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 6d  /* Assert that m
1a72d 61 6c 6c 6f 63 28 29 20 68 61 73 20 6e 6f 74 20  alloc() has not 
1a72e 66 61 69 6c 65 64 20 2a 2f 0a 20 20 64 62 20 3d  failed */.  db =
1a72f 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62   p->db;.  if( db
1a730 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1a731 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1a732 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a  ITE_NOMEM;.  }..
1a733 20 20 69 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26    if( p->pc<=0 &
1a734 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a  & p->expired ){.
1a735 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70      if( ALWAYS(p
1a736 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29  ->rc==SQLITE_OK)
1a737 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20   ){.      p->rc 
1a738 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b  = SQLITE_SCHEMA;
1a739 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1a73a 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1a73b 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74    goto end_of_st
1a73c 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  ep;.  }.  if( sq
1a73d 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1a73e 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  ) ){.    p->rc =
1a73f 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
1a740 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1a741 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
1a742 69 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20  if( p->pc<0 ){. 
1a743 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61     /* If there a
1a744 72 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74  re no other stat
1a745 65 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79  ements currently
1a746 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20   running, then. 
1a747 20 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20     ** reset the 
1a748 69 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20  interrupt flag. 
1a749 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61   This prevents a
1a74a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
1a74b 5f 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a  _interrupt.    *
1a74c 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74  * from interrupt
1a74d 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20  ing a statement 
1a74e 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74  that has not yet
1a74f 20 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f   started..    */
1a750 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74  .    if( db->act
1a751 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b  iveVdbeCnt==0 ){
1a752 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73  .      db->u1.is
1a753 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b  Interrupted = 0;
1a754 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  .    }..#ifndef 
1a755 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1a756 45 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50  E.    if( db->xP
1a757 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69  rofile && !db->i
1a758 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
1a759 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20    double rNow;. 
1a75a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75       sqlite3OsCu
1a75b 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56  rrentTime(db->pV
1a75c 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20  fs, &rNow);.    
1a75d 20 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d    p->startTime =
1a75e 20 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28   (u64)((rNow - (
1a75f 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30  int)rNow)*3600.0
1a760 2a 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30  *24.0*1000000000
1a761 2e 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  .0);.    }.#endi
1a762 66 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76  f..    db->activ
1a763 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20  eVdbeCnt++;.    
1a764 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d  if( p->readOnly=
1a765 3d 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64  =0 ) db->writeVd
1a766 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e  beCnt++;.    p->
1a767 70 63 20 3d 20 30 3b 0a 20 20 20 20 73 74 6d 74  pc = 0;.    stmt
1a768 4c 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20  LruRemove(p);.  
1a769 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1a76a 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20  _OMIT_EXPLAIN.  
1a76b 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29  if( p->explain )
1a76c 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1a76d 65 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20  e3VdbeList(p);. 
1a76e 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a   }else.#endif /*
1a76f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50   SQLITE_OMIT_EXP
1a770 4c 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20  LAIN */.  {.    
1a771 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1a772 45 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20  Exec(p);.  }..  
1a773 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1a774 79 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20  yOff(db) ){.    
1a775 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55  rc = SQLITE_MISU
1a776 53 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  SE;.  }..#ifndef
1a777 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
1a778 43 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74  CE.  /* Invoke t
1a779 68 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62  he profile callb
1a77a 61 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20  ack if there is 
1a77b 6f 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  one.  */.  if( r
1a77c 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26  c!=SQLITE_ROW &&
1a77d 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26   db->xProfile &&
1a77e 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
1a77f 26 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20  && p->zSql ){.  
1a780 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20    double rNow;. 
1a781 20 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d     u64 elapseTim
1a782 65 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  e;..    sqlite3O
1a783 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d  sCurrentTime(db-
1a784 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20  >pVfs, &rNow);. 
1a785 20 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20     elapseTime = 
1a786 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69  (u64)((rNow - (i
1a787 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a  nt)rNow)*3600.0*
1a788 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e  24.0*1000000000.
1a789 30 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69  0);.    elapseTi
1a78a 6d 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69  me -= p->startTi
1a78b 6d 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f  me;.    db->xPro
1a78c 66 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c  file(db->pProfil
1a78d 65 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65  eArg, p->zSql, e
1a78e 6c 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a  lapseTime);.  }.
1a78f 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72  #endif..  db->er
1a790 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66  rCode = rc;.  if
1a791 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d  ( SQLITE_NOMEM==
1a792 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70  sqlite3ApiExit(p
1a793 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a  ->db, p->rc) ){.
1a794 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49      p->rc = SQLI
1a795 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e  TE_NOMEM;.  }.en
1a796 64 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20  d_of_step:.  /* 
1a797 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f  At this point lo
1a798 63 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20  cal variable rc 
1a799 68 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20  holds the value 
1a79a 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a  that should be .
1a79b 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66    ** returned if
1a79c 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   this statement 
1a79d 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69  was compiled usi
1a79e 6e 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20  ng the legacy . 
1a79f 20 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70   ** sqlite3_prep
1a7a0 61 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e  are() interface.
1a7a1 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   According to th
1a7a2 65 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e  e docs, this can
1a7a3 20 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e   only.  ** be on
1a7a4 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20  e of the values 
1a7a5 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73  in the first ass
1a7a6 65 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72  ert() below. Var
1a7a7 69 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a  iable p->rc .  *
1a7a8 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76  * contains the v
1a7a9 61 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20  alue that would 
1a7aa 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73  be returned if s
1a7ab 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1a7ac 29 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c  ) .  ** were cal
1a7ad 6c 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74  led on statement
1a7ae 20 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72   p..  */.  asser
1a7af 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f  t( rc==SQLITE_RO
1a7b0 57 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45  W  || rc==SQLITE
1a7b1 5f 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53  _DONE   || rc==S
1a7b2 51 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20  QLITE_ERROR .   
1a7b3 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54      || rc==SQLIT
1a7b4 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51  E_BUSY || rc==SQ
1a7b5 4c 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b  LITE_MISUSE.  );
1a7b6 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63  .  assert( p->rc
1a7b7 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20  !=SQLITE_ROW && 
1a7b8 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  p->rc!=SQLITE_DO
1a7b9 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69  NE );.  if( p->i
1a7ba 73 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63  sPrepareV2 && rc
1a7bb 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20  !=SQLITE_ROW && 
1a7bc 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
1a7bd 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ){.    /* If thi
1a7be 73 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20  s statement was 
1a7bf 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73  prepared using s
1a7c0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
1a7c1 32 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20  2(), and an.    
1a7c2 2a 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  ** error has occ
1a7c3 75 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72  ured, then retur
1a7c4 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  n the error code
1a7c5 20 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65   in p->rc to the
1a7c6 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20  .    ** caller. 
1a7c7 53 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f  Set the error co
1a7c8 64 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  de in the databa
1a7c9 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65  se handle to the
1a7ca 20 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20   same value..   
1a7cb 20 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62   */ .    rc = db
1a7cc 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72  ->errCode = p->r
1a7cd 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  c;.  }.  return 
1a7ce 28 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29  (rc&db->errMask)
1a7cf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
1a7d0 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c  is the top-level
1a7d1 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
1a7d2 6f 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  of sqlite3_step(
1a7d3 29 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69  ).  Call.** sqli
1a7d4 74 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20  te3Step() to do 
1a7d5 6d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b  most of the work
1a7d6 2e 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65  .  If a schema e
1a7d7 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20  rror occurs,.** 
1a7d8 63 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72  call sqlite3Repr
1a7d9 65 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20  epare() and try 
1a7da 61 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66  again..*/.#ifdef
1a7db 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52   SQLITE_OMIT_PAR
1a7dc 53 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  SER.SQLITE_API i
1a7dd 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  nt sqlite3_step(
1a7de 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1a7df 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  tmt){.  int rc =
1a7e0 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
1a7e1 20 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20    if( pStmt ){. 
1a7e2 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20     Vdbe *v;.    
1a7e3 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  v = (Vdbe*)pStmt
1a7e4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1a7e5 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d  tex_enter(v->db-
1a7e6 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20  >mutex);.    rc 
1a7e7 3d 20 73 71 6c 69 74 65 33 53 74 65 70 28 76 29  = sqlite3Step(v)
1a7e8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1a7e9 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d  tex_leave(v->db-
1a7ea 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
1a7eb 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73  eturn rc;.}.#els
1a7ec 65 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  e.SQLITE_API int
1a7ed 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71   sqlite3_step(sq
1a7ee 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1a7ef 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
1a7f0 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
1a7f1 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20  if( pStmt ){.   
1a7f2 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
1a7f3 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62    Vdbe *v = (Vdb
1a7f4 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71  e*)pStmt;.    sq
1a7f5 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64  lite3 *db = v->d
1a7f6 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  b;.    sqlite3_m
1a7f7 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
1a7f8 75 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65  utex);.    while
1a7f9 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53  ( (rc = sqlite3S
1a7fa 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f  tep(v))==SQLITE_
1a7fb 53 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20  SCHEMA.         
1a7fc 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20    && cnt++ < 5. 
1a7fd 20 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63            && (rc
1a7fe 20 3d 20 76 64 62 65 52 65 70 72 65 70 61 72 65   = vdbeReprepare
1a7ff 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  (v))==SQLITE_OK 
1a800 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1a801 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20  _reset(pStmt);. 
1a802 20 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20       v->expired 
1a803 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
1a804 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43  f( rc==SQLITE_SC
1a805 48 45 4d 41 20 26 26 20 41 4c 57 41 59 53 28 76  HEMA && ALWAYS(v
1a806 2d 3e 69 73 50 72 65 70 61 72 65 56 32 29 20 26  ->isPrepareV2) &
1a807 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 70 45 72  & ALWAYS(db->pEr
1a808 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  r) ){.      /* T
1a809 68 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20  his case occurs 
1a80a 61 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f  after failing to
1a80b 20 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71   recompile an sq
1a80c 6c 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20  l statement. .  
1a80d 20 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72      ** The error
1a80e 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68   message from th
1a80f 65 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68  e SQL compiler h
1a810 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
1a811 6c 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a  loaded .      **
1a812 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
1a813 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20  se handle. This 
1a814 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65  block copies the
1a815 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a   error message .
1a816 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68        ** from th
1a817 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1a818 65 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65  e into the state
1a819 6d 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68  ment and sets th
1a81a 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20  e statement.    
1a81b 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75    ** program cou
1a81c 6e 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73  nter to 0 to ens
1a81d 75 72 65 20 74 68 61 74 20 77 68 65 6e 20 74 68  ure that when th
1a81e 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a  e statement is .
1a81f 20 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a        ** finaliz
1a820 65 64 20 6f 72 20 72 65 73 65 74 20 74 68 65 20  ed or reset the 
1a821 70 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73  parser error mes
1a822 73 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c  sage is availabl
1a823 65 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73  e via.      ** s
1a824 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20  qlite3_errmsg() 
1a825 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63  and sqlite3_errc
1a826 6f 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a  ode()..      */.
1a827 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1a828 20 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20   *zErr = (const 
1a829 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
1a82a 61 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45  alue_text(db->pE
1a82b 72 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69  rr); .      sqli
1a82c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d  te3DbFree(db, v-
1a82d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  >zErrMsg);.     
1a82e 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
1a82f 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
1a830 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73    v->zErrMsg = s
1a831 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
1a832 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  b, zErr);.      
1a833 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20  } else {.       
1a834 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b   v->zErrMsg = 0;
1a835 0a 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d  .        v->rc =
1a836 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1a837 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1a838 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
1a839 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
1a83a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1a83b 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
1a83c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1a83d 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  c;.}.#endif../*.
1a83e 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75  ** Extract the u
1a83f 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20  ser data from a 
1a840 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1a841 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65  structure and re
1a842 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65  turn a.** pointe
1a843 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49  r to it..*/.SQLI
1a844 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
1a845 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73  ite3_user_data(s
1a846 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a847 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
1a848 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20  && p->pFunc );. 
1a849 20 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63   return p->pFunc
1a84a 2d 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a  ->pUserData;.}..
1a84b 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
1a84c 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d  e user data from
1a84d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   a sqlite3_conte
1a84e 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  xt structure and
1a84f 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69   return a.** poi
1a850 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53  nter to it..*/.S
1a851 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
1a852 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  3 *sqlite3_conte
1a853 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c  xt_db_handle(sql
1a854 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29  ite3_context *p)
1a855 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26  {.  assert( p &&
1a856 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72   p->pFunc );.  r
1a857 65 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d  eturn p->s.db;.}
1a858 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
1a859 6f 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70  owing is the imp
1a85a 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
1a85b 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74  n SQL function t
1a85c 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61  hat always.** fa
1a85d 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ils with an erro
1a85e 72 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e  r message statin
1a85f 67 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74  g that the funct
1a860 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74  ion is used in t
1a861 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74  he.** wrong cont
1a862 65 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ext.  The sqlite
1a863 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
1a864 69 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20  ion() API might 
1a865 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c  construct.** SQL
1a866 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75   function that u
1a867 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  se this routine 
1a868 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63  so that the func
1a869 74 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74  tions will exist
1a86a 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73  .** for name res
1a86b 6f 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20  olution but are 
1a86c 61 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61  actually overloa
1a86d 64 65 64 20 62 79 20 74 68 65 20 78 46 69 6e 64  ded by the xFind
1a86e 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68  Function.** meth
1a86f 6f 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61  od of virtual ta
1a870 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  bles..*/.SQLITE_
1a871 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a872 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74  ite3InvalidFunct
1a873 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ion(.  sqlite3_c
1a874 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
1a875 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f    /* The functio
1a876 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78  n calling contex
1a877 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73  t */.  int NotUs
1a878 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ed,             
1a879 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
1a87a 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
1a87b 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71  function */.  sq
1a87c 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f  lite3_value **No
1a87d 74 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75  tUsed2   /* Valu
1a87e 65 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65  e of each argume
1a87f 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  nt */.){.  const
1a880 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63   char *zName = c
1a881 6f 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a  ontext->pFunc->z
1a882 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45  Name;.  char *zE
1a883 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  rr;.  UNUSED_PAR
1a884 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
1a885 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45   NotUsed2);.  zE
1a886 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  rr = sqlite3_mpr
1a887 69 6e 74 66 28 0a 20 20 20 20 20 20 22 75 6e 61  intf(.      "una
1a888 62 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74  ble to use funct
1a889 69 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65  ion %s in the re
1a88a 71 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22  quested context"
1a88b 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  , zName);.  sqli
1a88c 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
1a88d 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20  (context, zErr, 
1a88e 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  -1);.  sqlite3_f
1a88f 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a  ree(zErr);.}../*
1a890 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20  .** Allocate or 
1a891 72 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65  return the aggre
1a892 67 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  gate context for
1a893 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e   a user function
1a894 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74  .  A new.** cont
1a895 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ext is allocated
1a896 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61   on the first ca
1a897 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20  ll.  Subsequent 
1a898 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65  calls return the
1a899 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74  .** same context
1a89a 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e   that was return
1a89b 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c  ed on prior call
1a89c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
1a89d 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61   void *sqlite3_a
1a89e 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
1a89f 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1a8a0 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b   *p, int nByte){
1a8a1 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20  .  Mem *pMem;.  
1a8a2 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e  assert( p && p->
1a8a3 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e  pFunc && p->pFun
1a8a4 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73  c->xStep );.  as
1a8a5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1a8a6 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62  tex_held(p->s.db
1a8a7 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d  ->mutex) );.  pM
1a8a8 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20  em = p->pMem;.  
1a8a9 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  if( (pMem->flags
1a8aa 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29   & MEM_Agg)==0 )
1a8ab 7b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d  {.    if( nByte=
1a8ac 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
1a8ad 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1a8ae 65 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b  eExternal(pMem);
1a8af 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
1a8b0 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1a8b1 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30       pMem->z = 0
1a8b2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1a8b3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1a8b4 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74  mGrow(pMem, nByt
1a8b5 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65  e, 0);.      pMe
1a8b6 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41  m->flags = MEM_A
1a8b7 67 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  gg;.      pMem->
1a8b8 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e  u.pDef = p->pFun
1a8b9 63 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65  c;.      if( pMe
1a8ba 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  m->z ){.        
1a8bb 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20  memset(pMem->z, 
1a8bc 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  0, nByte);.     
1a8bd 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
1a8be 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65  eturn (void*)pMe
1a8bf 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  m->z;.}../*.** R
1a8c0 65 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61  eturn the auxila
1a8c1 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c  ry data pointer,
1a8c2 20 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65   if any, for the
1a8c3 20 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e   iArg'th argumen
1a8c4 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72  t to.** the user
1a8c5 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65  -function define
1a8c6 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51  d by pCtx..*/.SQ
1a8c7 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
1a8c8 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61  qlite3_get_auxda
1a8c9 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ta(sqlite3_conte
1a8ca 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41  xt *pCtx, int iA
1a8cb 72 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20  rg){.  VdbeFunc 
1a8cc 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61  *pVdbeFunc;..  a
1a8cd 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1a8ce 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1a8cf 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1a8d0 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43    pVdbeFunc = pC
1a8d1 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20  tx->pVdbeFunc;. 
1a8d2 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20   if( !pVdbeFunc 
1a8d3 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75  || iArg>=pVdbeFu
1a8d4 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67  nc->nAux || iArg
1a8d5 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  <0 ){.    return
1a8d6 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
1a8d7 20 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75   pVdbeFunc->apAu
1a8d8 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a  x[iArg].pAux;.}.
1a8d9 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61  ./*.** Set the a
1a8da 75 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69  uxilary data poi
1a8db 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20  nter and delete 
1a8dc 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68  function, for th
1a8dd 65 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67  e iArg'th.** arg
1a8de 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65  ument to the use
1a8df 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  r-function defin
1a8e0 65 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20  ed by pCtx. Any 
1a8e1 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69  previous value i
1a8e2 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20  s.** deleted by 
1a8e3 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65  calling the dele
1a8e4 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63  te function spec
1a8e5 69 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61  ified when it wa
1a8e6 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  s set..*/.SQLITE
1a8e7 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1a8e8 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20  3_set_auxdata(. 
1a8e9 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1a8ea 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69   *pCtx, .  int i
1a8eb 41 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41  Arg, .  void *pA
1a8ec 75 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44  ux, .  void (*xD
1a8ed 65 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b  elete)(void*).){
1a8ee 0a 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74  .  struct AuxDat
1a8ef 61 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56  a *pAuxData;.  V
1a8f0 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75  dbeFunc *pVdbeFu
1a8f1 6e 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30  nc;.  if( iArg<0
1a8f2 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a   ) goto failed;.
1a8f3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1a8f4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1a8f5 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1a8f6 20 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20   );.  pVdbeFunc 
1a8f7 3d 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e  = pCtx->pVdbeFun
1a8f8 63 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46  c;.  if( !pVdbeF
1a8f9 75 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63  unc || pVdbeFunc
1a8fa 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a  ->nAux<=iArg ){.
1a8fb 20 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28      int nAux = (
1a8fc 70 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62  pVdbeFunc ? pVdb
1a8fd 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29  eFunc->nAux : 0)
1a8fe 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f  ;.    int nMallo
1a8ff 63 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46  c = sizeof(VdbeF
1a900 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74  unc) + sizeof(st
1a901 72 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41  ruct AuxData)*iA
1a902 72 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e  rg;.    pVdbeFun
1a903 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  c = sqlite3DbRea
1a904 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c  lloc(pCtx->s.db,
1a905 20 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c   pVdbeFunc, nMal
1a906 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70  loc);.    if( !p
1a907 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20  VdbeFunc ){.    
1a908 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20    goto failed;. 
1a909 20 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70     }.    pCtx->p
1a90a 56 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65  VdbeFunc = pVdbe
1a90b 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74  Func;.    memset
1a90c 28 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41  (&pVdbeFunc->apA
1a90d 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a  ux[nAux], 0, siz
1a90e 65 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61  eof(struct AuxDa
1a90f 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78  ta)*(iArg+1-nAux
1a910 29 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e  ));.    pVdbeFun
1a911 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31  c->nAux = iArg+1
1a912 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d  ;.    pVdbeFunc-
1a913 3e 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70  >pFunc = pCtx->p
1a914 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75  Func;.  }..  pAu
1a915 78 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75  xData = &pVdbeFu
1a916 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b  nc->apAux[iArg];
1a917 0a 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d  .  if( pAuxData-
1a918 3e 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74  >pAux && pAuxDat
1a919 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20  a->xDelete ){.  
1a91a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c    pAuxData->xDel
1a91b 65 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41  ete(pAuxData->pA
1a91c 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44  ux);.  }.  pAuxD
1a91d 61 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78  ata->pAux = pAux
1a91e 3b 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44  ;.  pAuxData->xD
1a91f 65 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b  elete = xDelete;
1a920 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c  .  return;..fail
1a921 65 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74  ed:.  if( xDelet
1a922 65 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65  e ){.    xDelete
1a923 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23  (pAux);.  }.}..#
1a924 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a925 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a  IT_DEPRECATED./*
1a926 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1a927 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74  umber of times t
1a928 68 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e  he Step function
1a929 20 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20   of a aggregate 
1a92a 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c  has been .** cal
1a92b 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  led..**.** This 
1a92c 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72  function is depr
1a92d 65 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20  ecated.  Do not 
1a92e 75 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63  use it for new c
1a92f 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70  ode.  It is.** p
1a930 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61  rovide only to a
1a931 76 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65  void breaking le
1a932 67 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20  gacy code.  New 
1a933 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
1a934 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  on.** implementa
1a935 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65  tions should kee
1a936 70 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e  p their own coun
1a937 74 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20  ts within their 
1a938 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e  aggregate.** con
1a939 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  text..*/.SQLITE_
1a93a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1a93b 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28  aggregate_count(
1a93c 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1a93d 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
1a93e 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70   && p->pMem && p
1a93f 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46  ->pFunc && p->pF
1a940 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20  unc->xStep );.  
1a941 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e  return p->pMem->
1a942 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  n;.}.#endif../*.
1a943 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1a944 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1a945 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
1a946 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d  t for the statem
1a947 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51  ent pStmt..*/.SQ
1a948 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1a949 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
1a94a 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
1a94b 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a  pStmt){.  Vdbe *
1a94c 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53  pVm = (Vdbe *)pS
1a94d 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56  tmt;.  return pV
1a94e 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c  m ? pVm->nResCol
1a94f 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  umn : 0;.}../*.*
1a950 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1a951 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76  ber of values av
1a952 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65  ailable from the
1a953 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
1a954 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  the.** currently
1a955 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65   executing state
1a956 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53  ment pStmt..*/.S
1a957 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1a958 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74  lite3_data_count
1a959 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1a95a 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70  Stmt){.  Vdbe *p
1a95b 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  Vm = (Vdbe *)pSt
1a95c 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30  mt;.  if( pVm==0
1a95d 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74   || pVm->pResult
1a95e 53 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Set==0 ) return 
1a95f 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d  0;.  return pVm-
1a960 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a  >nResColumn;.}..
1a961 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
1a962 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43  see if column iC
1a963 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20  ol of the given 
1a964 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c  statement is val
1a965 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73  id.  If.** it is
1a966 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  , return a point
1a967 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f  er to the Mem fo
1a968 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  r the value of t
1a969 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49  hat column..** I
1a96a 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61  f iCol is not va
1a96b 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f  lid, return a po
1a96c 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77  inter to a Mem w
1a96d 68 69 63 68 20 68 61 73 20 61 20 76 61 6c 75 65  hich has a value
1a96e 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a  .** of NULL..*/.
1a96f 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75  static Mem *colu
1a970 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74  mnMem(sqlite3_st
1a971 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1a972 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a  ){.  Vdbe *pVm;.
1a973 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65    int vals;.  Me
1a974 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20  m *pOut;..  pVm 
1a975 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
1a976 0a 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56  .  if( pVm && pV
1a977 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30  m->pResultSet!=0
1a978 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43   && i<pVm->nResC
1a979 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b  olumn && i>=0 ){
1a97a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1a97b 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62  ex_enter(pVm->db
1a97c 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61  ->mutex);.    va
1a97d 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74  ls = sqlite3_dat
1a97e 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a  a_count(pStmt);.
1a97f 20 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d      pOut = &pVm-
1a980 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a  >pResultSet[i];.
1a981 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1a982 28 28 64 6f 75 62 6c 65 29 30 29 20 49 6e 20 63  ((double)0) In c
1a983 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d  ase of SQLITE_OM
1a984 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
1a985 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 73 74 61 74  T... */.    stat
1a986 69 63 20 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c  ic const Mem nul
1a987 6c 4d 65 6d 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f  lMem = {{0}, (do
1a988 75 62 6c 65 29 30 2c 20 30 2c 20 22 22 2c 20 30  uble)0, 0, "", 0
1a989 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49  , MEM_Null, SQLI
1a98a 54 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30  TE_NULL, 0, 0, 0
1a98b 20 7d 3b 0a 20 20 20 20 69 66 28 20 70 56 6d 20   };.    if( pVm 
1a98c 26 26 20 41 4c 57 41 59 53 28 70 56 6d 2d 3e 64  && ALWAYS(pVm->d
1a98d 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  b) ){.      sqli
1a98e 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1a98f 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  pVm->db->mutex);
1a990 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1a991 72 6f 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c  ror(pVm->db, SQL
1a992 49 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20  ITE_RANGE, 0);. 
1a993 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20     }.    pOut = 
1a994 28 4d 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a  (Mem*)&nullMem;.
1a995 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75    }.  return pOu
1a996 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  t;.}../*.** This
1a997 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
1a998 6c 65 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69  led after invoki
1a999 6e 67 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61  ng an sqlite3_va
1a99a 6c 75 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e  lue_XXX function
1a99b 20 6f 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e   on a .** column
1a99c 20 76 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76   value (i.e. a v
1a99d 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
1a99e 20 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53   evaluating an S
1a99f 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  QL expression in
1a9a0 20 74 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c   the.** select l
1a9a1 69 73 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20  ist of a SELECT 
1a9a2 73 74 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20  statement) that 
1a9a3 6d 61 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c  may cause a mall
1a9a4 6f 63 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66  oc() failure. If
1a9a5 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61   .** malloc() ha
1a9a6 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68  s failed, the th
1a9a7 72 65 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c  reads mallocFail
1a9a8 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72  ed flag is clear
1a9a9 65 64 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c  ed and the resul
1a9aa 74 0a 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61  t.** code of sta
1a9ab 74 65 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74  tement pStmt set
1a9ac 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   to SQLITE_NOMEM
1a9ad 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63  ..**.** Specific
1a9ae 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61  ally, this is ca
1a9af 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
1a9b0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69  :.**.**     sqli
1a9b1 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29  te3_column_int()
1a9b2 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .**     sqlite3_
1a9b3 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a  column_int64().*
1a9b4 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
1a9b5 6c 75 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20  lumn_text().**  
1a9b6 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
1a9b7 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20  n_text16().**   
1a9b8 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
1a9b9 5f 72 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73  _real().**     s
1a9ba 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
1a9bb 74 65 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  tes().**     sql
1a9bc 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
1a9bd 73 31 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20  s16().**.** But 
1a9be 6e 6f 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  not for sqlite3_
1a9bf 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77  column_blob(), w
1a9c0 68 69 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73  hich never calls
1a9c1 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74   malloc()..*/.st
1a9c2 61 74 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e  atic void column
1a9c3 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71  MallocFailure(sq
1a9c4 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1a9c5 74 29 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c  t).{.  /* If mal
1a9c6 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72  loc() failed dur
1a9c7 69 6e 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20  ing an encoding 
1a9c8 63 6f 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69  conversion withi
1a9c9 6e 20 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65  n an.  ** sqlite
1a9ca 33 5f 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49  3_column_XXX API
1a9cb 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 72  , then set the r
1a9cc 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68  eturn code of th
1a9cd 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20  e statement to. 
1a9ce 20 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   ** SQLITE_NOMEM
1a9cf 2e 20 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  . The next call 
1a9d0 74 6f 20 5f 73 74 65 70 28 29 20 28 69 66 20 61  to _step() (if a
1a9d1 6e 79 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ny) will return 
1a9d2 53 51 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a  SQLITE_ERROR.  *
1a9d3 2a 20 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28  * and _finalize(
1a9d4 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f  ) will return NO
1a9d5 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65  MEM..  */.  Vdbe
1a9d6 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53   *p = (Vdbe *)pS
1a9d7 74 6d 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  tmt;.  if( p ){.
1a9d8 20 20 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69      p->rc = sqli
1a9d9 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62  te3ApiExit(p->db
1a9da 2c 20 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71  , p->rc);.    sq
1a9db 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1a9dc 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
1a9dd 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  .  }.}../*******
1a9de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9df 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  ***** sqlite3_co
1a9e0 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lumn_  *********
1a9e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9e2 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ******.** The fo
1a9e3 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73  llowing routines
1a9e4 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 63 63   are used to acc
1a9e5 65 73 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  ess elements of 
1a9e6 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a  the current row.
1a9e7 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ** in the result
1a9e8 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   set..*/.SQLITE_
1a9e9 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1a9ea 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
1a9eb 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  lob(sqlite3_stmt
1a9ec 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b   *pStmt, int i){
1a9ed 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76  .  const void *v
1a9ee 61 6c 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69  al;.  val = sqli
1a9ef 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20  te3_value_blob( 
1a9f0 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c  columnMem(pStmt,
1a9f1 69 29 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20  i) );.  /* Even 
1a9f2 74 68 6f 75 67 68 20 74 68 65 72 65 20 69 73 20  though there is 
1a9f3 6e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76  no encoding conv
1a9f4 65 72 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c  ersion, value_bl
1a9f5 6f 62 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20  ob() might.  ** 
1a9f6 6e 65 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c  need to call mal
1a9f7 6c 6f 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20  loc() to expand 
1a9f8 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20  the result of a 
1a9f9 7a 65 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a  zeroblob() .  **
1a9fa 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20   expression. .  
1a9fb 2a 2f 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  */.  columnMallo
1a9fc 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
1a9fd 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
1a9fe 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1a9ff 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
1aa00 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ytes(sqlite3_stm
1aa01 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1aa02 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71  {.  int val = sq
1aa03 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1aa04 73 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  s( columnMem(pSt
1aa05 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
1aa06 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1aa07 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1aa08 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  val;.}.SQLITE_AP
1aa09 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
1aa0a 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c  lumn_bytes16(sql
1aa0b 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1aa0c 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
1aa0d 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
1aa0e 6c 75 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c  lue_bytes16( col
1aa0f 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
1aa10 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
1aa11 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
1aa12 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
1aa13 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62  .SQLITE_API doub
1aa14 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  le sqlite3_colum
1aa15 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  n_double(sqlite3
1aa16 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1aa17 74 20 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76  t i){.  double v
1aa18 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
1aa19 75 65 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d  ue_double( colum
1aa1a 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1aa1b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1aa1c 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1aa1d 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1aa1e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1aa1f 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
1aa20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1aa21 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
1aa22 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  int val = sqlite
1aa23 33 5f 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c  3_value_int( col
1aa24 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
1aa25 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
1aa26 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
1aa27 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
1aa28 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1aa29 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  te_int64 sqlite3
1aa2a 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71  _column_int64(sq
1aa2b 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1aa2c 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c  t, int i){.  sql
1aa2d 69 74 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20  ite_int64 val = 
1aa2e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
1aa2f 74 36 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  t64( columnMem(p
1aa30 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c  Stmt,i) );.  col
1aa31 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
1aa32 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
1aa33 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
1aa34 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  API const unsign
1aa35 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  ed char *sqlite3
1aa36 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c  _column_text(sql
1aa37 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1aa38 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73  , int i){.  cons
1aa39 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1aa3a 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76  *val = sqlite3_v
1aa3b 61 6c 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d  alue_text( colum
1aa3c 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1aa3d 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1aa3e 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1aa3f 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1aa40 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
1aa41 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33  3_value *sqlite3
1aa42 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71  _column_value(sq
1aa43 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1aa44 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d  t, int i){.  Mem
1aa45 20 2a 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d   *pOut = columnM
1aa46 65 6d 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20  em(pStmt, i);.  
1aa47 69 66 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26  if( pOut->flags&
1aa48 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20  MEM_Static ){.  
1aa49 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d    pOut->flags &=
1aa4a 20 7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20   ~MEM_Static;.  
1aa4b 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d    pOut->flags |=
1aa4c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a   MEM_Ephem;.  }.
1aa4d 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61    columnMallocFa
1aa4e 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20  ilure(pStmt);.  
1aa4f 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f  return (sqlite3_
1aa50 76 61 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a  value *)pOut;.}.
1aa51 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1aa52 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
1aa53 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
1aa54 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1aa55 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73  text16(sqlite3_s
1aa56 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1aa57 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  i){.  const void
1aa58 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f   *val = sqlite3_
1aa59 76 61 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f  value_text16( co
1aa5a 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
1aa5b 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   );.  columnMall
1aa5c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
1aa5d 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a  ;.  return val;.
1aa5e 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1aa5f 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
1aa60 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1aa61 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
1aa62 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ype(sqlite3_stmt
1aa63 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b   *pStmt, int i){
1aa64 0a 20 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73  .  int iType = s
1aa65 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
1aa66 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  e( columnMem(pSt
1aa67 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
1aa68 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1aa69 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1aa6a 69 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65  iType;.}../* The
1aa6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74   following funct
1aa6c 69 6f 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e  ion is experimen
1aa6d 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20  tal and subject 
1aa6e 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20  to change or.** 
1aa6f 72 65 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74  removal */./*int
1aa70 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1aa71 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c  numeric_type(sql
1aa72 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1aa73 2c 20 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65  , int i){.**  re
1aa74 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  turn sqlite3_val
1aa75 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
1aa76 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1aa77 2c 69 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f  ,i) );.**}.*/../
1aa78 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65  *.** Convert the
1aa79 20 4e 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66   N-th element of
1aa7a 20 70 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65   pStmt->pColName
1aa7b 5b 5d 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67  [] into a string
1aa7c 20 75 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28   using.** xFunc(
1aa7d 29 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68  ) then return th
1aa7e 61 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e  at string.  If N
1aa7f 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
1aa80 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a  , return 0..**.*
1aa81 2a 20 54 68 65 72 65 20 61 72 65 20 75 70 20 74  * There are up t
1aa82 6f 20 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61  o 5 names for ea
1aa83 63 68 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54  ch column.  useT
1aa84 79 70 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77  ype determines w
1aa85 68 69 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20  hich.** name is 
1aa86 72 65 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20  returned.  Here 
1aa87 61 72 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a  are the names:.*
1aa88 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54  *.**    0      T
1aa89 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61  he column name a
1aa8a 73 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64  s it should be d
1aa8b 69 73 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74  isplayed for out
1aa8c 70 75 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20  put.**    1     
1aa8d 20 54 68 65 20 64 61 74 61 74 79 70 65 20 6e 61   The datatype na
1aa8e 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  me for the colum
1aa8f 6e 0a 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54  n.**    2      T
1aa90 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  he name of the d
1aa91 61 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65  atabase that the
1aa92 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20   column derives 
1aa93 66 72 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20  from.**    3    
1aa94 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68    The name of th
1aa95 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65  e table that the
1aa96 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20   column derives 
1aa97 66 72 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20  from.**    4    
1aa98 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68    The name of th
1aa99 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74  e table column t
1aa9a 68 61 74 20 74 68 65 20 72 65 73 75 6c 74 20 63  hat the result c
1aa9b 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72  olumn derives fr
1aa9c 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  om.**.** If the 
1aa9d 72 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20  result is not a 
1aa9e 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65  simple column re
1aa9f 66 65 72 65 6e 63 65 20 28 69 66 20 69 74 20 69  ference (if it i
1aaa0 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a  s an expression.
1aaa1 2a 2a 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74  ** or a constant
1aaa2 29 20 74 68 65 6e 20 75 73 65 54 79 70 65 73 20  ) then useTypes 
1aaa3 32 2c 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75  2, 3, and 4 retu
1aaa4 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74  rn NULL..*/.stat
1aaa5 69 63 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63  ic const void *c
1aaa6 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c  olumnName(.  sql
1aaa7 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1aaa8 2c 0a 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e  ,.  int N,.  con
1aaa9 73 74 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63  st void *(*xFunc
1aaaa 29 28 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75  )(Mem*),.  int u
1aaab 73 65 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73  seType.){.  cons
1aaac 74 20 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b  t void *ret = 0;
1aaad 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
1aaae 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e  be *)pStmt;.  in
1aaaf 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  t n;.  sqlite3 *
1aab0 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20  db = p->db;.  . 
1aab1 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29   assert( db!=0 )
1aab2 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  n = sqlite3_
1aab3 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74  column_count(pSt
1aab4 6d 74 29 3b 0a 20 20 69 66 28 20 4e 3c 6e 20 26  mt);.  if( N<n &
1aab5 26 20 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 4e 20  & N>=0 ){.    N 
1aab6 2b 3d 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20  += useType*n;.  
1aab7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1aab8 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
1aab9 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  ;.    assert( db
1aaba 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d  ->mallocFailed==
1aabb 30 20 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 78  0 );.    ret = x
1aabc 46 75 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d  Func(&p->aColNam
1aabd 65 5b 4e 5d 29 3b 0a 20 20 20 20 20 2f 2a 20 41  e[N]);.     /* A
1aabe 20 6d 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65   malloc may have
1aabf 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f   failed inside o
1aac0 66 20 74 68 65 20 78 46 75 6e 63 28 29 20 63 61  f the xFunc() ca
1aac1 6c 6c 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20  ll. If this.    
1aac2 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  ** is the case, 
1aac3 63 6c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63  clear the malloc
1aac4 46 61 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20  Failed flag and 
1aac5 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20  return NULL..   
1aac6 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   */.    if( db->
1aac7 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1aac8 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
1aac9 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20  Failed = 0;.    
1aaca 20 20 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 7d    ret = 0;.    }
1aacb 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1aacc 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
1aacd 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
1aace 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n ret;.}../*.** 
1aacf 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  Return the name 
1aad0 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d  of the Nth colum
1aad1 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  n of the result 
1aad2 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20  set returned by 
1aad3 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  SQL.** statement
1aad4 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54   pStmt..*/.SQLIT
1aad5 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
1aad6 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1aad7 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
1aad8 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
1aad9 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75  ){.  return colu
1aada 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53  mnName(.      pS
1aadb 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76  tmt, N, (const v
1aadc 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71  oid*(*)(Mem*))sq
1aadd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1aade 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b  , COLNAME_NAME);
1aadf 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1aae0 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1aae1 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1aae2 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
1aae3 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  mn_name16(sqlite
1aae4 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1aae5 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
1aae6 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20  columnName(.    
1aae7 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e    pStmt, N, (con
1aae8 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a  st void*(*)(Mem*
1aae9 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ))sqlite3_value_
1aaea 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f  text16, COLNAME_
1aaeb 4e 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  NAME);.}.#endif.
1aaec 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e  ./*.** Constrain
1aaed 74 3a 20 20 49 66 20 79 6f 75 20 68 61 76 65 20  t:  If you have 
1aaee 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
1aaef 54 41 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20  TADATA then you 
1aaf0 6d 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69  must.** not defi
1aaf1 6e 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45  ne OMIT_DECLTYPE
1aaf2 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
1aaf3 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43  (SQLITE_OMIT_DEC
1aaf4 4c 54 59 50 45 29 20 26 26 20 64 65 66 69 6e 65  LTYPE) && define
1aaf5 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
1aaf6 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29  COLUMN_METADATA)
1aaf7 0a 23 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e  .# error "Must n
1aaf8 6f 74 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53  ot define both S
1aaf9 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54  QLITE_OMIT_DECLT
1aafa 59 50 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61  YPE \.         a
1aafb 6e 64 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  nd SQLITE_ENABLE
1aafc 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
1aafd 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  ".#endif..#ifnde
1aafe 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
1aaff 43 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74  CLTYPE./*.** Ret
1ab00 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64  urn the column d
1ab01 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
1ab02 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20  (if applicable) 
1ab03 6f 66 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c  of the 'i'th col
1ab04 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65  umn.** of the re
1ab05 73 75 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20  sult set of SQL 
1ab06 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e  statement pStmt.
1ab07 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
1ab08 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
1ab09 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
1ab0a 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  pe(sqlite3_stmt 
1ab0b 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
1ab0c 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
1ab0d 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
1ab0e 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
1ab0f 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
1ab10 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43  e3_value_text, C
1ab11 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29  OLNAME_DECLTYPE)
1ab12 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1ab13 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
1ab14 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
1ab15 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
1ab16 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73  umn_decltype16(s
1ab17 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1ab18 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1ab19 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1ab1a 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1ab1b 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1ab1c 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1ab1d 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
1ab1e 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a  NAME_DECLTYPE);.
1ab1f 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1ab20 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
1ab21 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1ab22 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20  E_OMIT_DECLTYPE 
1ab23 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
1ab24 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f  E_ENABLE_COLUMN_
1ab25 4d 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52  METADATA./*.** R
1ab26 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f  eturn the name o
1ab27 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
1ab28 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75  rom which a resu
1ab29 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65  lt column derive
1ab2a 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65  s..** NULL is re
1ab2b 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65  turned if the re
1ab2c 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61  sult column is a
1ab2d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20  n expression or 
1ab2e 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61  constant or.** a
1ab2f 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69  nything else whi
1ab30 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61  ch is not an una
1ab31 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63  biguous referenc
1ab32 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  e to a database 
1ab33 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  column..*/.SQLIT
1ab34 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
1ab35 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1ab36 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73  _database_name(s
1ab37 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1ab38 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1ab39 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1ab3a 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1ab3b 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1ab3c 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1ab3d 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41  alue_text, COLNA
1ab3e 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a  ME_DATABASE);.}.
1ab3f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ab40 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
1ab41 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
1ab42 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1ab43 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28  database_name16(
1ab44 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1ab45 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
1ab46 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
1ab47 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
1ab48 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
1ab49 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
1ab4a 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f  value_text16, CO
1ab4b 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b  LNAME_DATABASE);
1ab4c 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1ab4d 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
1ab4e 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
1ab4f 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1ab50 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68  table from which
1ab51 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e   a result column
1ab52 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c   derives..** NUL
1ab53 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  L is returned if
1ab54 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75   the result colu
1ab55 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73  mn is an express
1ab56 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20  ion or constant 
1ab57 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65  or.** anything e
1ab58 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74  lse which is not
1ab59 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72   an unabiguous r
1ab5a 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61  eference to a da
1ab5b 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a  tabase column..*
1ab5c 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
1ab5d 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
1ab5e 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
1ab5f 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  me(sqlite3_stmt 
1ab60 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
1ab61 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
1ab62 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
1ab63 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
1ab64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
1ab65 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43  e3_value_text, C
1ab66 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d  OLNAME_TABLE);.}
1ab67 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ab68 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
1ab69 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
1ab6a 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1ab6b 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71  _table_name16(sq
1ab6c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1ab6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1ab6e 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1ab6f 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1ab70 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1ab71 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1ab72 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e  lue_text16, COLN
1ab73 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65  AME_TABLE);.}.#e
1ab74 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1ab75 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
1ab76 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1ab77 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1ab78 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69   column from whi
1ab79 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75  ch a result colu
1ab7a 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e  mn derives..** N
1ab7b 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
1ab7c 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  if the result co
1ab7d 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65  lumn is an expre
1ab7e 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e  ssion or constan
1ab7f 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67  t or.** anything
1ab80 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e   else which is n
1ab81 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73  ot an unabiguous
1ab82 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
1ab83 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e  database column.
1ab84 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
1ab85 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
1ab86 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
1ab87 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
1ab88 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
1ab89 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75  ){.  return colu
1ab8a 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53  mnName(.      pS
1ab8b 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76  tmt, N, (const v
1ab8c 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71  oid*(*)(Mem*))sq
1ab8d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1ab8e 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e  , COLNAME_COLUMN
1ab8f 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
1ab90 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
1ab91 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1ab92 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
1ab93 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
1ab94 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  16(sqlite3_stmt 
1ab95 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
1ab96 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
1ab97 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
1ab98 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
1ab99 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
1ab9a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c  e3_value_text16,
1ab9b 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29   COLNAME_COLUMN)
1ab9c 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1ab9d 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
1ab9e 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  */.#endif /* SQL
1ab9f 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
1aba0 4e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a  N_METADATA */...
1aba1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
1aba2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aba3 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20   sqlite3_bind_  
1aba4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aba5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a  ***********.** .
1aba6 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64  ** Routines used
1aba7 20 74 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65   to attach value
1aba8 73 20 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69  s to wildcards i
1aba9 6e 20 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c  n a compiled SQL
1abaa 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f   statement..*/./
1abab 2a 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20  *.** Unbind the 
1abac 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76  value bound to v
1abad 61 72 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72  ariable i in vir
1abae 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20  tual machine p. 
1abaf 54 68 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20  This is the .** 
1abb0 74 68 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64  the same as bind
1abb1 69 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65  ing a NULL value
1abb2 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20   to the column. 
1abb3 49 66 20 74 68 65 20 22 69 22 20 70 61 72 61 6d  If the "i" param
1abb4 65 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f  eter is.** out o
1abb5 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51  f range, then SQ
1abb6 4c 49 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65  LITE_RANGE is re
1abb7 74 75 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65  turned. Othewise
1abb8 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a   SQLITE_OK..**.*
1abb9 2a 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 65  * A successful e
1abba 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69  valuation of thi
1abbb 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72  s routine acquir
1abbc 65 73 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20  es the mutex on 
1abbd 70 2e 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20  p..** the mutex 
1abbe 69 73 20 72 65 6c 65 61 73 65 64 20 69 66 20 61  is released if a
1abbf 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72  ny kind of error
1abc0 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54   occurs..**.** T
1abc1 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74  he error code st
1abc2 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65  ored in database
1abc3 20 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72   p->db is overwr
1abc4 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 72  itten with the r
1abc5 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69  eturn.** value i
1abc6 6e 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73  n any case..*/.s
1abc7 74 61 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e  tatic int vdbeUn
1abc8 62 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e  bind(Vdbe *p, in
1abc9 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61  t i){.  Mem *pVa
1abca 72 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  r;.  if( p==0 ) 
1abcb 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
1abcc 53 55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f  SUSE;.  sqlite3_
1abcd 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64  mutex_enter(p->d
1abce 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28  b->mutex);.  if(
1abcf 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f   p->magic!=VDBE_
1abd0 4d 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e  MAGIC_RUN || p->
1abd1 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  pc>=0 ){.    sql
1abd2 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c  ite3Error(p->db,
1abd3 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20   SQLITE_MISUSE, 
1abd4 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
1abd5 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64  mutex_leave(p->d
1abd6 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72  b->mutex);.    r
1abd7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1abd8 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  USE;.  }.  if( i
1abd9 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20  <1 || i>p->nVar 
1abda 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1abdb 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54  ror(p->db, SQLIT
1abdc 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20  E_RANGE, 0);.   
1abdd 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1abde 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
1abdf 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  x);.    return S
1abe0 51 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d  QLITE_RANGE;.  }
1abe1 0a 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d  .  i--;.  pVar =
1abe2 20 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20   &p->aVar[i];.  
1abe3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1abe4 6c 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70  lease(pVar);.  p
1abe5 56 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Var->flags = MEM
1abe6 5f 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33  _Null;.  sqlite3
1abe7 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c  Error(p->db, SQL
1abe8 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65  ITE_OK, 0);.  re
1abe9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1abea 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20  }../*.** Bind a 
1abeb 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c  text or BLOB val
1abec 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ue..*/.static in
1abed 74 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71  t bindText(.  sq
1abee 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1abef 74 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74  t,   /* The stat
1abf0 65 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67  ement to bind ag
1abf1 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ainst */.  int i
1abf2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1abf3 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
1abf4 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62  e parameter to b
1abf5 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ind */.  const v
1abf6 6f 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20  oid *zData,     
1abf7 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
1abf8 65 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75  e data to be bou
1abf9 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74  nd */.  int nDat
1abfa 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  a,             /
1abfb 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1abfc 73 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20  s of data to be 
1abfd 62 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20  bound */.  void 
1abfe 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20  (*xDel)(void*), 
1abff 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20    /* Destructor 
1ac00 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a  for the data */.
1ac01 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 20 20    u8 encoding   
1ac02 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f           /* Enco
1ac03 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74  ding for the dat
1ac04 61 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  a */.){.  Vdbe *
1ac05 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  p = (Vdbe *)pStm
1ac06 74 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a  t;.  Mem *pVar;.
1ac07 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20    int rc;..  rc 
1ac08 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20  = vdbeUnbind(p, 
1ac09 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  i);.  if( rc==SQ
1ac0a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
1ac0b 66 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20  f( zData!=0 ){. 
1ac0c 20 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e       pVar = &p->
1ac0d 61 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20  aVar[i-1];.     
1ac0e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1ac0f 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c  eMemSetStr(pVar,
1ac10 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65   zData, nData, e
1ac11 6e 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a  ncoding, xDel);.
1ac12 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1ac13 4c 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64  LITE_OK && encod
1ac14 69 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ing!=0 ){.      
1ac15 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1ac16 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
1ac17 28 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62  (pVar, ENC(p->db
1ac18 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
1ac19 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70    sqlite3Error(p
1ac1a 2d 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20  ->db, rc, 0);.  
1ac1b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ac1c 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72  ApiExit(p->db, r
1ac1d 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  c);.    }.    sq
1ac1e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1ac1f 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
1ac20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1ac21 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64  ;.}.../*.** Bind
1ac22 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f   a blob value to
1ac23 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
1ac24 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53  t variable..*/.S
1ac25 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1ac26 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
1ac27 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
1ac28 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69  *pStmt, .  int i
1ac29 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
1ac2a 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e  *zData, .  int n
1ac2b 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a  Data, .  void (*
1ac2c 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  xDel)(void*).){.
1ac2d 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78    return bindTex
1ac2e 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74  t(pStmt, i, zDat
1ac2f 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20  a, nData, xDel, 
1ac30 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  0);.}.SQLITE_API
1ac31 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1ac32 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  d_double(sqlite3
1ac33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1ac34 74 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c  t i, double rVal
1ac35 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ue){.  int rc;. 
1ac36 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
1ac37 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d   *)pStmt;.  rc =
1ac38 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69   vdbeUnbind(p, i
1ac39 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1ac3a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
1ac3b 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44  lite3VdbeMemSetD
1ac3c 6f 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69  ouble(&p->aVar[i
1ac3d 2d 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20  -1], rValue);.  
1ac3e 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1ac3f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
1ac40 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
1ac41 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  n rc;.}.SQLITE_A
1ac42 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1ac43 69 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f  ind_int(sqlite3_
1ac44 73 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20  stmt *p, int i, 
1ac45 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72  int iValue){.  r
1ac46 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69  eturn sqlite3_bi
1ac47 6e 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28  nd_int64(p, i, (
1ac48 69 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53  i64)iValue);.}.S
1ac49 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1ac4a 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
1ac4b 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1ac4c 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c  Stmt, int i, sql
1ac4d 69 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65  ite_int64 iValue
1ac4e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56  ){.  int rc;.  V
1ac4f 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a  dbe *p = (Vdbe *
1ac50 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76  )pStmt;.  rc = v
1ac51 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b  dbeUnbind(p, i);
1ac52 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1ac53 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
1ac54 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
1ac55 36 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d  64(&p->aVar[i-1]
1ac56 2c 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73  , iValue);.    s
1ac57 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1ac58 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
1ac59 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1ac5a 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  c;.}.SQLITE_API 
1ac5b 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1ac5c 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74  _null(sqlite3_st
1ac5d 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1ac5e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56  ){.  int rc;.  V
1ac5f 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29  dbe *p = (Vdbe*)
1ac60 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64  pStmt;.  rc = vd
1ac61 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a  beUnbind(p, i);.
1ac62 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1ac63 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
1ac64 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
1ac65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1ac66 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1ac67 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1ac68 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
1ac69 74 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  t( .  sqlite3_st
1ac6a 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e  mt *pStmt, .  in
1ac6b 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  t i, .  const ch
1ac6c 61 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e  ar *zData, .  in
1ac6d 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64  t nData, .  void
1ac6e 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a   (*xDel)(void*).
1ac6f 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64  ){.  return bind
1ac70 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a  Text(pStmt, i, z
1ac71 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65  Data, nData, xDe
1ac72 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
1ac73 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1ac74 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1ac75 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1ac76 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28  te3_bind_text16(
1ac77 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
1ac78 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69  *pStmt, .  int i
1ac79 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
1ac7a 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e  *zData, .  int n
1ac7b 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a  Data, .  void (*
1ac7c 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  xDel)(void*).){.
1ac7d 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78    return bindTex
1ac7e 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74  t(pStmt, i, zDat
1ac7f 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20  a, nData, xDel, 
1ac80 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
1ac81 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  VE);.}.#endif /*
1ac82 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1ac83 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  16 */.SQLITE_API
1ac84 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1ac85 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  d_value(sqlite3_
1ac86 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1ac87 20 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65   i, const sqlite
1ac88 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29  3_value *pValue)
1ac89 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 77  {.  int rc;.  sw
1ac8a 69 74 63 68 28 20 70 56 61 6c 75 65 2d 3e 74 79  itch( pValue->ty
1ac8b 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  pe ){.    case S
1ac8c 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b  QLITE_INTEGER: {
1ac8d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1ac8e 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
1ac8f 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d  Stmt, i, pValue-
1ac90 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 62 72 65  >u.i);.      bre
1ac91 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1ac92 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
1ac93 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   {.      rc = sq
1ac94 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c  lite3_bind_doubl
1ac95 65 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c  e(pStmt, i, pVal
1ac96 75 65 2d 3e 72 29 3b 0a 20 20 20 20 20 20 62 72  ue->r);.      br
1ac97 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1ac98 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a  ase SQLITE_BLOB:
1ac99 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 56 61   {.      if( pVa
1ac9a 6c 75 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  lue->flags & MEM
1ac9b 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 20  _Zero ){.       
1ac9c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
1ac9d 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 70 53 74 6d  nd_zeroblob(pStm
1ac9e 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e  t, i, pValue->u.
1ac9f 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d 65  nZero);.      }e
1aca0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20  lse{.        rc 
1aca1 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62  = sqlite3_bind_b
1aca2 6c 6f 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 56  lob(pStmt, i, pV
1aca3 61 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d  alue->z, pValue-
1aca4 3e 6e 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  >n,SQLITE_TRANSI
1aca5 45 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ENT);.      }.  
1aca6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1aca7 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
1aca8 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 72  _TEXT: {.      r
1aca9 63 20 3d 20 62 69 6e 64 54 65 78 74 28 70 53 74  c = bindText(pSt
1acaa 6d 74 2c 69 2c 20 20 70 56 61 6c 75 65 2d 3e 7a  mt,i,  pValue->z
1acab 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 20 53 51 4c  , pValue->n, SQL
1acac 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 0a 20  ITE_TRANSIENT,. 
1acad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1acae 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56 61               pVa
1acaf 6c 75 65 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20  lue->enc);.     
1acb0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1acb1 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1acb2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
1acb3 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d 74 2c  bind_null(pStmt,
1acb4 20 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   i);.      break
1acb5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1acb6 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
1acb7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1acb8 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28  3_bind_zeroblob(
1acb9 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1acba 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20  tmt, int i, int 
1acbb 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  n){.  int rc;.  
1acbc 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20  Vdbe *p = (Vdbe 
1acbd 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20  *)pStmt;.  rc = 
1acbe 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29  vdbeUnbind(p, i)
1acbf 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1acc0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
1acc1 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65  ite3VdbeMemSetZe
1acc2 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72 5b  roBlob(&p->aVar[
1acc3 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73 71  i-1], n);.    sq
1acc4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1acc5 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
1acc6 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1acc7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1acc8 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1acc9 77 69 6c 64 63 61 72 64 73 20 74 68 61 74 20 63  wildcards that c
1acca 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c 6c  an be potentiall
1accb 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54  y bound to..** T
1accc 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
1accd 64 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20  dded to support 
1acce 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a 2a  DBD::SQLite.  .*
1accf 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1acd0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
1acd1 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71  rameter_count(sq
1acd2 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1acd3 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  t){.  Vdbe *p = 
1acd4 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
1acd5 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 56  return p ? p->nV
1acd6 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ar : 0;.}../*.**
1acd7 20 43 72 65 61 74 65 20 61 20 6d 61 70 70 69 6e   Create a mappin
1acd8 67 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 20  g from variable 
1acd9 6e 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69 61  numbers to varia
1acda 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20  ble names.** in 
1acdb 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d  the Vdbe.azVar[]
1acdc 20 61 72 72 61 79 2c 20 69 66 20 73 75 63 68 20   array, if such 
1acdd 61 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 6e  a mapping does n
1acde 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 78  ot already.** ex
1acdf 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ist..*/.static v
1ace0 6f 69 64 20 63 72 65 61 74 65 56 61 72 4d 61 70  oid createVarMap
1ace1 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28  (Vdbe *p){.  if(
1ace2 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20   !p->okVar ){.  
1ace3 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 4f 70 20    int j;.    Op 
1ace4 2a 70 4f 70 3b 0a 20 20 20 20 73 71 6c 69 74 65  *pOp;.    sqlite
1ace5 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
1ace6 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
1ace7 20 2f 2a 20 54 68 65 20 72 61 63 65 20 63 6f 6e   /* The race con
1ace8 64 69 74 69 6f 6e 20 68 65 72 65 20 69 73 20 68  dition here is h
1ace9 61 72 6d 6c 65 73 73 2e 20 20 49 66 20 74 77 6f  armless.  If two
1acea 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 74 68   threads call th
1aceb 69 73 0a 20 20 20 20 2a 2a 20 72 6f 75 74 69 6e  is.    ** routin
1acec 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 56 64  e on the same Vd
1aced 62 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  be at the same t
1acee 69 6d 65 2c 20 74 68 65 79 20 62 6f 74 68 20 6d  ime, they both m
1acef 69 67 68 74 20 65 6e 64 0a 20 20 20 20 2a 2a 20  ight end.    ** 
1acf0 75 70 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  up initializing 
1acf1 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d  the Vdbe.azVar[]
1acf2 20 61 72 72 61 79 2e 20 20 54 68 61 74 20 69 73   array.  That is
1acf3 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 0a   a little extra.
1acf4 20 20 20 20 2a 2a 20 77 6f 72 6b 20 62 75 74 20      ** work but 
1acf5 69 74 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68  it results in th
1acf6 65 20 73 61 6d 65 20 61 6e 73 77 65 72 2e 0a 20  e same answer.. 
1acf7 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d     */.    for(j=
1acf8 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a  0, pOp=p->aOp; j
1acf9 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f  <p->nOp; j++, pO
1acfa 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  p++){.      if( 
1acfb 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
1acfc 56 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20  Variable ){.    
1acfd 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1acfe 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31  >p1>0 && pOp->p1
1acff 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 20  <=p->nVar );.   
1ad00 20 20 20 20 20 70 2d 3e 61 7a 56 61 72 5b 70 4f       p->azVar[pO
1ad01 70 2d 3e 70 31 2d 31 5d 20 3d 20 70 4f 70 2d 3e  p->p1-1] = pOp->
1ad02 70 34 2e 7a 3b 0a 20 20 20 20 20 20 7d 0a 20 20  p4.z;.      }.  
1ad03 20 20 7d 0a 20 20 20 20 70 2d 3e 6f 6b 56 61 72    }.    p->okVar
1ad04 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 1;.    sqlite
1ad05 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
1ad06 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
1ad07 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1ad08 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77   the name of a w
1ad09 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65  ildcard paramete
1ad0a 72 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  r.  Return NULL 
1ad0b 69 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20  if the index.** 
1ad0c 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  is out of range 
1ad0d 6f 72 20 69 66 20 74 68 65 20 77 69 6c 64 63 61  or if the wildca
1ad0e 72 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a  rd is unnamed..*
1ad0f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20  *.** The result 
1ad10 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e  is always UTF-8.
1ad11 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
1ad12 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
1ad13 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
1ad14 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  r_name(sqlite3_s
1ad15 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1ad16 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  i){.  Vdbe *p = 
1ad17 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
1ad18 69 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20  if( p==0 || i<1 
1ad19 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a  || i>p->nVar ){.
1ad1a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1ad1b 7d 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70  }.  createVarMap
1ad1c 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d  (p);.  return p-
1ad1d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a  >azVar[i-1];.}..
1ad1e 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69  /*.** Given a wi
1ad1f 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72  ldcard parameter
1ad20 20 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68   name, return th
1ad21 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76  e index of the v
1ad22 61 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20  ariable.** with 
1ad23 74 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74  that name.  If t
1ad24 68 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61  here is no varia
1ad25 62 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76  ble with the giv
1ad26 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75  en name,.** retu
1ad27 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rn 0..*/.SQLITE_
1ad28 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1ad29 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
1ad2a 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d  ndex(sqlite3_stm
1ad2b 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20  t *pStmt, const 
1ad2c 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
1ad2d 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
1ad2e 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b  )pStmt;.  int i;
1ad2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20  .  if( p==0 ){. 
1ad30 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1ad31 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28  .  createVarMap(
1ad32 70 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65  p); .  if( zName
1ad33 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
1ad34 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29   i<p->nVar; i++)
1ad35 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
1ad36 61 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72  ar *z = p->azVar
1ad37 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  [i];.      if( z
1ad38 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61   && strcmp(z,zNa
1ad39 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
1ad3a 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20    return i+1;.  
1ad3b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1ad3c 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
1ad3d 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c  *.** Transfer al
1ad3e 6c 20 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20  l bindings from 
1ad3f 74 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d  the first statem
1ad40 65 6e 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20  ent over to the 
1ad41 73 65 63 6f 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54  second..*/.SQLIT
1ad42 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1ad43 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e  lite3TransferBin
1ad44 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74  dings(sqlite3_st
1ad45 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73  mt *pFromStmt, s
1ad46 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f  qlite3_stmt *pTo
1ad47 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70  Stmt){.  Vdbe *p
1ad48 46 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46  From = (Vdbe*)pF
1ad49 72 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20  romStmt;.  Vdbe 
1ad4a 2a 70 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54  *pTo = (Vdbe*)pT
1ad4b 6f 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a  oStmt;.  int i;.
1ad4c 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 64    assert( pTo->d
1ad4d 62 3d 3d 70 46 72 6f 6d 2d 3e 64 62 20 29 3b 0a  b==pFrom->db );.
1ad4e 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 6e    assert( pTo->n
1ad4f 56 61 72 3d 3d 70 46 72 6f 6d 2d 3e 6e 56 61 72  Var==pFrom->nVar
1ad50 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   );.  sqlite3_mu
1ad51 74 65 78 5f 65 6e 74 65 72 28 70 54 6f 2d 3e 64  tex_enter(pTo->d
1ad52 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72  b->mutex);.  for
1ad53 28 69 3d 30 3b 20 69 3c 70 46 72 6f 6d 2d 3e 6e  (i=0; i<pFrom->n
1ad54 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  Var; i++){.    s
1ad55 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1ad56 65 28 26 70 54 6f 2d 3e 61 56 61 72 5b 69 5d 2c  e(&pTo->aVar[i],
1ad57 20 26 70 46 72 6f 6d 2d 3e 61 56 61 72 5b 69 5d   &pFrom->aVar[i]
1ad58 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
1ad59 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 54 6f  _mutex_leave(pTo
1ad5a 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1ad5b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1ad5c 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
1ad5d 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41  ITE_OMIT_DEPRECA
1ad5e 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63  TED./*.** Deprec
1ad5f 61 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e  ated external in
1ad60 74 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e  terface.  Intern
1ad61 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63  al/core SQLite c
1ad62 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61  ode.** should ca
1ad63 6c 6c 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66  ll sqlite3Transf
1ad64 65 72 42 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a  erBindings..**.*
1ad65 2a 20 49 73 20 69 73 20 6d 69 73 75 73 65 20 74  * Is is misuse t
1ad66 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  o call this rout
1ad67 69 6e 65 20 77 69 74 68 20 73 74 61 74 65 6d 65  ine with stateme
1ad68 6e 74 73 20 66 72 6f 6d 20 64 69 66 66 65 72 65  nts from differe
1ad69 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  nt.** database c
1ad6a 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74  onnections.  But
1ad6b 20 61 73 20 74 68 69 73 20 69 73 20 61 20 64 65   as this is a de
1ad6c 70 72 65 63 61 74 65 64 20 69 6e 74 65 72 66 61  precated interfa
1ad6d 63 65 2c 20 77 65 0a 2a 2a 20 77 69 6c 6c 20 6e  ce, we.** will n
1ad6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 63 68 65  ot bother to che
1ad6f 63 6b 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 64  ck for that cond
1ad70 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ition..**.** If 
1ad71 74 68 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e  the two statemen
1ad72 74 73 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66  ts contain a dif
1ad73 66 65 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  ferent number of
1ad74 20 62 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a   bindings, then.
1ad75 2a 2a 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52  ** an SQLITE_ERR
1ad76 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  OR is returned. 
1ad77 20 4e 6f 74 68 69 6e 67 20 65 6c 73 65 20 63 61   Nothing else ca
1ad78 6e 20 67 6f 20 77 72 6f 6e 67 2c 20 73 6f 20 6f  n go wrong, so o
1ad79 74 68 65 72 77 69 73 65 0a 2a 2a 20 53 51 4c 49  therwise.** SQLI
1ad7a 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
1ad7b 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
1ad7c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61   int sqlite3_tra
1ad7d 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73  nsfer_bindings(s
1ad7e 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 72  qlite3_stmt *pFr
1ad7f 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f  omStmt, sqlite3_
1ad80 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a  stmt *pToStmt){.
1ad81 20 20 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20    Vdbe *pFrom = 
1ad82 28 56 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74  (Vdbe*)pFromStmt
1ad83 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f 20 3d 20  ;.  Vdbe *pTo = 
1ad84 28 56 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a  (Vdbe*)pToStmt;.
1ad85 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61    if( pFrom->nVa
1ad86 72 21 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a  r!=pTo->nVar ){.
1ad87 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1ad88 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72  E_ERROR;.  }.  r
1ad89 65 74 75 72 6e 20 73 71 6c 69 74 65 33 54 72 61  eturn sqlite3Tra
1ad8a 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70 46  nsferBindings(pF
1ad8b 72 6f 6d 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74  romStmt, pToStmt
1ad8c 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
1ad8d 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 71  ** Return the sq
1ad8e 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20  lite3* database 
1ad8f 68 61 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20  handle to which 
1ad90 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
1ad91 74 65 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20  tement given.** 
1ad92 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  in the argument 
1ad93 62 65 6c 6f 6e 67 73 2e 20 20 54 68 69 73 20 69  belongs.  This i
1ad94 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  s the same datab
1ad95 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20  ase handle that 
1ad96 77 61 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  was.** the first
1ad97 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
1ad98 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1ad99 28 29 20 74 68 61 74 20 77 61 73 20 75 73 65 64  () that was used
1ad9a 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68   to create.** th
1ad9b 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74  e statement in t
1ad9c 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a  he first place..
1ad9d 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
1ad9e 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64  lite3 *sqlite3_d
1ad9f 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33  b_handle(sqlite3
1ada0 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
1ada1 20 72 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20   return pStmt ? 
1ada2 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e  ((Vdbe*)pStmt)->
1ada3 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  db : 0;.}../*.**
1ada4 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
1ada5 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70 72  r to the next pr
1ada6 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1ada7 20 61 66 74 65 72 20 70 53 74 6d 74 20 61 73 73   after pStmt ass
1ada8 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20  ociated.** with 
1ada9 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1adaa 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 70 53 74  ion pDb.  If pSt
1adab 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75  mt is NULL, retu
1adac 72 6e 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  rn the first.** 
1adad 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1adae 6e 74 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  nt for the datab
1adaf 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ase connection. 
1adb0 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
1adb1 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20  there.** are no 
1adb2 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  more..*/.SQLITE_
1adb3 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  API sqlite3_stmt
1adb4 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73   *sqlite3_next_s
1adb5 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62  tmt(sqlite3 *pDb
1adb6 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  , sqlite3_stmt *
1adb7 70 53 74 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65  pStmt){.  sqlite
1adb8 33 5f 73 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20  3_stmt *pNext;. 
1adb9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1adba 6e 74 65 72 28 70 44 62 2d 3e 6d 75 74 65 78 29  nter(pDb->mutex)
1adbb 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30  ;.  if( pStmt==0
1adbc 20 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20   ){.    pNext = 
1adbd 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70  (sqlite3_stmt*)p
1adbe 44 62 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c  Db->pVdbe;.  }el
1adbf 73 65 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20  se{.    pNext = 
1adc0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28  (sqlite3_stmt*)(
1adc1 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70  (Vdbe*)pStmt)->p
1adc2 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Next;.  }.  sqli
1adc3 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1adc4 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  pDb->mutex);.  r
1adc5 65 74 75 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a  eturn pNext;.}..
1adc6 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1adc7 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74   value of a stat
1adc8 75 73 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 61  us counter for a
1adc9 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1adca 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ent.*/.SQLITE_AP
1adcb 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  I int sqlite3_st
1adcc 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65  mt_status(sqlite
1adcd 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1adce 6e 74 20 6f 70 2c 20 69 6e 74 20 72 65 73 65 74  nt op, int reset
1adcf 46 6c 61 67 29 7b 0a 20 20 56 64 62 65 20 2a 70  Flag){.  Vdbe *p
1add0 56 64 62 65 20 3d 20 28 56 64 62 65 2a 29 70 53  Vdbe = (Vdbe*)pS
1add1 74 6d 74 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70  tmt;.  int v = p
1add2 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f  Vdbe->aCounter[o
1add3 70 2d 31 5d 3b 0a 20 20 69 66 28 20 72 65 73 65  p-1];.  if( rese
1add4 74 46 6c 61 67 20 29 20 70 56 64 62 65 2d 3e 61  tFlag ) pVdbe->a
1add5 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20  Counter[op-1] = 
1add6 30 3b 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d  0;.  return v;.}
1add7 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1add8 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61 70 69  * End of vdbeapi
1add9 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1adda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1addb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1addc 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1addd 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
1adde 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
1addf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ade0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ade1 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
1ade2 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
1ade3 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
1ade4 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
1ade5 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
1ade6 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
1ade7 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
1ade8 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
1ade9 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
1adea 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
1adeb 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
1adec 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
1aded 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
1adee 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
1adef 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
1adf0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
1adf1 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
1adf2 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
1adf3 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
1adf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adf8 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
1adf9 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
1adfa 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65  e implements exe
1adfb 63 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66  cution method of
1adfc 20 74 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c   the .** Virtual
1adfd 20 44 61 74 61 62 61 73 65 20 45 6e 67 69 6e 65   Database Engine
1adfe 20 28 56 44 42 45 29 2e 20 20 41 20 73 65 70 61   (VDBE).  A sepa
1adff 72 61 74 65 20 66 69 6c 65 20 28 22 76 64 62 65  rate file ("vdbe
1ae00 61 75 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c  aux.c").** handl
1ae01 65 73 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 20  es housekeeping 
1ae02 64 65 74 61 69 6c 73 20 73 75 63 68 20 61 73 20  details such as 
1ae03 63 72 65 61 74 69 6e 67 20 61 6e 64 20 64 65 6c  creating and del
1ae04 65 74 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e  eting.** VDBE in
1ae05 73 74 61 6e 63 65 73 2e 20 20 54 68 69 73 20 66  stances.  This f
1ae06 69 6c 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e  ile is solely in
1ae07 74 65 72 65 73 74 65 64 20 69 6e 20 65 78 65 63  terested in exec
1ae08 75 74 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42  uting.** the VDB
1ae09 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a  E program..**.**
1ae0a 20 49 6e 20 74 68 65 20 65 78 74 65 72 6e 61 6c   In the external
1ae0b 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 22   interface, an "
1ae0c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 69  sqlite3_stmt*" i
1ae0d 73 20 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e  s an opaque poin
1ae0e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45  ter.** to a VDBE
1ae0f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20  ..**.** The SQL 
1ae10 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 65 73  parser generates
1ae11 20 61 20 70 72 6f 67 72 61 6d 20 77 68 69 63 68   a program which
1ae12 20 69 73 20 74 68 65 6e 20 65 78 65 63 75 74 65   is then execute
1ae13 64 20 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 45  d by.** the VDBE
1ae14 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20   to do the work 
1ae15 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  of the SQL state
1ae16 6d 65 6e 74 2e 20 20 56 44 42 45 20 70 72 6f 67  ment.  VDBE prog
1ae17 72 61 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d  rams are .** sim
1ae18 69 6c 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20  ilar in form to 
1ae19 61 73 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67  assembly languag
1ae1a 65 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20  e.  The program 
1ae1b 63 6f 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61  consists of.** a
1ae1c 20 6c 69 6e 65 61 72 20 73 65 71 75 65 6e 63 65   linear sequence
1ae1d 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20   of operations. 
1ae1e 20 45 61 63 68 20 6f 70 65 72 61 74 69 6f 6e 20   Each operation 
1ae1f 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a  has an opcode .*
1ae20 2a 20 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73  * and 5 operands
1ae21 2e 20 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20  .  Operands P1, 
1ae22 50 32 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69  P2, and P3 are i
1ae23 6e 74 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e  ntegers.  Operan
1ae24 64 20 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75  d P4 .** is a nu
1ae25 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74  ll-terminated st
1ae26 72 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50  ring.  Operand P
1ae27 35 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64  5 is an unsigned
1ae28 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46   character..** F
1ae29 65 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61  ew opcodes use a
1ae2a 6c 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a  ll 5 operands..*
1ae2b 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e  *.** Computation
1ae2c 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f   results are sto
1ae2d 72 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 20  red on a set of 
1ae2e 72 65 67 69 73 74 65 72 73 20 6e 75 6d 62 65 72  registers number
1ae2f 65 64 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20  ed beginning.** 
1ae30 77 69 74 68 20 31 20 61 6e 64 20 67 6f 69 6e 67  with 1 and going
1ae31 20 75 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d   up to Vdbe.nMem
1ae32 2e 20 20 45 61 63 68 20 72 65 67 69 73 74 65 72  .  Each register
1ae33 20 63 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69   can store.** ei
1ae34 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 2c  ther an integer,
1ae35 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74   a null-terminat
1ae36 65 64 20 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f  ed string, a flo
1ae37 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e  ating point.** n
1ae38 75 6d 62 65 72 2c 20 6f 72 20 74 68 65 20 53 51  umber, or the SQ
1ae39 4c 20 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20  L "NULL" value. 
1ae3a 20 41 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e   An implicit con
1ae3b 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65  version from one
1ae3c 0a 2a 2a 20 74 79 70 65 20 74 6f 20 74 68 65 20  .** type to the 
1ae3d 6f 74 68 65 72 20 6f 63 63 75 72 73 20 61 73 20  other occurs as 
1ae3e 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a  necessary..** .*
1ae3f 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f  * Most of the co
1ae40 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
1ae41 69 73 20 74 61 6b 65 6e 20 75 70 20 62 79 20 74  is taken up by t
1ae42 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  he sqlite3VdbeEx
1ae43 65 63 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ec().** function
1ae44 20 77 68 69 63 68 20 64 6f 65 73 20 74 68 65 20   which does the 
1ae45 77 6f 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 65  work of interpre
1ae46 74 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67  ting a VDBE prog
1ae47 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65  ram..** But othe
1ae48 72 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61  r routines are a
1ae49 6c 73 6f 20 70 72 6f 76 69 64 65 64 20 74 6f 20  lso provided to 
1ae4a 68 65 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e 67  help in building
1ae4b 20 75 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d   up.** a program
1ae4c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 20   instruction by 
1ae4d 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a  instruction..**.
1ae4e 2a 2a 20 56 61 72 69 6f 75 73 20 73 63 72 69 70  ** Various scrip
1ae4f 74 73 20 73 63 61 6e 20 74 68 69 73 20 73 6f 75  ts scan this sou
1ae50 72 63 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 65  rce file in orde
1ae51 72 20 74 6f 20 67 65 6e 65 72 61 74 65 20 48 54  r to generate HT
1ae52 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74  ML.** documentat
1ae53 69 6f 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c  ion, headers fil
1ae54 65 73 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72  es, or other der
1ae55 69 76 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65  ived files.  The
1ae56 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f   formatting.** o
1ae57 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68  f the code in th
1ae58 69 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72  is file is, ther
1ae59 65 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74  efore, important
1ae5a 2e 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d  .  See other com
1ae5b 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73  ments.** in this
1ae5c 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c   file for detail
1ae5d 73 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c  s.  If in doubt,
1ae5e 20 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20   do not deviate 
1ae5f 66 72 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a  from existing.**
1ae60 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20   commenting and 
1ae61 69 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 63  indentation prac
1ae62 74 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e 67  tices when chang
1ae63 69 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63 6f  ing or adding co
1ae64 64 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76  de..**.** $Id: v
1ae65 64 62 65 2e 63 2c 76 20 31 2e 38 34 32 2e 32 2e  dbe.c,v 1.842.2.
1ae66 31 20 32 30 30 39 2f 30 35 2f 31 38 20 31 36 3a  1 2009/05/18 16:
1ae67 31 34 3a 32 35 20 64 72 68 20 45 78 70 20 24 0a  14:25 drh Exp $.
1ae68 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  */../*.** The fo
1ae69 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76  llowing global v
1ae6a 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65  ariable is incre
1ae6b 6d 65 6e 74 65 64 20 65 76 65 72 79 20 74 69 6d  mented every tim
1ae6c 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f  e a cursor.** mo
1ae6d 76 65 73 2c 20 65 69 74 68 65 72 20 62 79 20 74  ves, either by t
1ae6e 68 65 20 4f 50 5f 53 65 65 6b 58 58 2c 20 4f 50  he OP_SeekXX, OP
1ae6f 5f 4e 65 78 74 2c 20 6f 72 20 4f 50 5f 50 72 65  _Next, or OP_Pre
1ae70 76 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20  v opcodes.  The 
1ae71 74 65 73 74 0a 2a 2a 20 70 72 6f 63 65 64 75 72  test.** procedur
1ae72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f  es use this info
1ae73 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20  rmation to make 
1ae74 73 75 72 65 20 74 68 61 74 20 69 6e 64 69 63 65  sure that indice
1ae75 73 20 61 72 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67  s are.** working
1ae76 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 68 69   correctly.  Thi
1ae77 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e  s variable has n
1ae78 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72  o function other
1ae79 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70   than to.** help
1ae7a 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72   verify the corr
1ae7b 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  ect operation of
1ae7c 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f   the library..*/
1ae7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1ae7e 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
1ae7f 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63  nt sqlite3_searc
1ae80 68 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  h_count = 0;.#en
1ae81 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  dif../*.** When 
1ae82 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69  this global vari
1ae83 61 62 6c 65 20 69 73 20 70 6f 73 69 74 69 76 65  able is positive
1ae84 2c 20 69 74 20 67 65 74 73 20 64 65 63 72 65 6d  , it gets decrem
1ae85 65 6e 74 65 64 20 6f 6e 63 65 20 62 65 66 6f 72  ented once befor
1ae86 65 0a 2a 2a 20 65 61 63 68 20 69 6e 73 74 72 75  e.** each instru
1ae87 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 56 44 42  ction in the VDB
1ae88 45 2e 20 20 57 68 65 6e 20 72 65 61 63 68 65 73  E.  When reaches
1ae89 20 7a 65 72 6f 2c 20 74 68 65 20 75 31 2e 69 73   zero, the u1.is
1ae8a 49 6e 74 65 72 72 75 70 74 65 64 0a 2a 2a 20 66  Interrupted.** f
1ae8b 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69  ield of the sqli
1ae8c 74 65 33 20 73 74 72 75 63 74 75 72 65 20 69 73  te3 structure is
1ae8d 20 73 65 74 20 69 6e 20 6f 72 64 65 72 20 74 6f   set in order to
1ae8e 20 73 69 6d 75 6c 61 74 65 20 61 6e 64 20 69 6e   simulate and in
1ae8f 74 65 72 72 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54  terrupt..**.** T
1ae90 68 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20  his facility is 
1ae91 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
1ae92 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20   purposes only. 
1ae93 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 66 75 6e   It does not fun
1ae94 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f  ction.** in an o
1ae95 72 64 69 6e 61 72 79 20 62 75 69 6c 64 2e 0a 2a  rdinary build..*
1ae96 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
1ae97 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
1ae98 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  int sqlite3_inte
1ae99 72 72 75 70 74 5f 63 6f 75 6e 74 20 3d 20 30 3b  rrupt_count = 0;
1ae9a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1ae9b 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76  he next global v
1ae9c 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65  ariable is incre
1ae9d 6d 65 6e 74 65 64 20 65 61 63 68 20 74 79 70 65  mented each type
1ae9e 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63   the OP_Sort opc
1ae9f 6f 64 65 0a 2a 2a 20 69 73 20 65 78 65 63 75 74  ode.** is execut
1aea0 65 64 2e 20 20 54 68 65 20 74 65 73 74 20 70 72  ed.  The test pr
1aea1 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 68 69  ocedures use thi
1aea2 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  s information to
1aea3 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a   make sure that.
1aea4 2a 2a 20 73 6f 72 74 69 6e 67 20 69 73 20 6f 63  ** sorting is oc
1aea5 63 75 72 72 69 6e 67 20 6f 72 20 6e 6f 74 20 6f  curring or not o
1aea6 63 63 75 72 72 69 6e 67 20 61 74 20 61 70 70 72  ccurring at appr
1aea7 6f 70 72 69 61 74 65 20 74 69 6d 65 73 2e 20 20  opriate times.  
1aea8 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a   This variable.*
1aea9 2a 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f  * has no functio
1aeaa 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 20  n other than to 
1aeab 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65 20  help verify the 
1aeac 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f  correct operatio
1aead 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 62 72  n of the.** libr
1aeae 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ary..*/.#ifdef S
1aeaf 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
1aeb0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1aeb1 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 20 3d 20 30  3_sort_count = 0
1aeb2 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
1aeb3 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20  The next global 
1aeb4 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72 64 73  variable records
1aeb5 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1aeb6 20 6c 61 72 67 65 73 74 20 4d 45 4d 5f 42 6c 6f   largest MEM_Blo
1aeb7 62 0a 2a 2a 20 6f 72 20 4d 45 4d 5f 53 74 72 20  b.** or MEM_Str 
1aeb8 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 75 73  that has been us
1aeb9 65 64 20 62 79 20 61 20 56 44 42 45 20 6f 70 63  ed by a VDBE opc
1aeba 6f 64 65 2e 20 20 54 68 65 20 74 65 73 74 20 70  ode.  The test p
1aebb 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 75 73 65  rocedures.** use
1aebc 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   this informatio
1aebd 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  n to make sure t
1aebe 68 61 74 20 74 68 65 20 7a 65 72 6f 2d 62 6c 6f  hat the zero-blo
1aebf 62 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a  b functionality.
1aec0 2a 2a 20 69 73 20 77 6f 72 6b 69 6e 67 20 63 6f  ** is working co
1aec1 72 72 65 63 74 6c 79 2e 20 20 20 54 68 69 73 20  rrectly.   This 
1aec2 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20  variable has no 
1aec3 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74  function other t
1aec4 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76  han to.** help v
1aec5 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63  erify the correc
1aec6 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74  t operation of t
1aec7 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23  he library..*/.#
1aec8 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1aec9 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
1aeca 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f   sqlite3_max_blo
1aecb 62 73 69 7a 65 20 3d 20 30 3b 0a 73 74 61 74 69  bsize = 0;.stati
1aecc 63 20 76 6f 69 64 20 75 70 64 61 74 65 4d 61 78  c void updateMax
1aecd 42 6c 6f 62 73 69 7a 65 28 4d 65 6d 20 2a 70 29  Blobsize(Mem *p)
1aece 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67  {.  if( (p->flag
1aecf 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  s & (MEM_Str|MEM
1aed0 5f 42 6c 6f 62 29 29 21 3d 30 20 26 26 20 70 2d  _Blob))!=0 && p-
1aed1 3e 6e 3e 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62  >n>sqlite3_max_b
1aed2 6c 6f 62 73 69 7a 65 20 29 7b 0a 20 20 20 20 73  lobsize ){.    s
1aed3 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73  qlite3_max_blobs
1aed4 69 7a 65 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a  ize = p->n;.  }.
1aed5 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1aed6 54 65 73 74 20 61 20 72 65 67 69 73 74 65 72 20  Test a register 
1aed7 74 6f 20 73 65 65 20 69 66 20 69 74 20 65 78 63  to see if it exc
1aed8 65 65 64 73 20 74 68 65 20 63 75 72 72 65 6e 74  eeds the current
1aed9 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69   maximum blob si
1aeda 7a 65 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65  ze..** If it doe
1aedb 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65  s, record the ne
1aedc 77 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73  w maximum blob s
1aedd 69 7a 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  ize..*/.#if defi
1aede 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
1aedf 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
1aee0 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
1aee1 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20  _TEST).# define 
1aee2 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1aee3 49 5a 45 28 50 29 20 20 75 70 64 61 74 65 4d 61  IZE(P)  updateMa
1aee4 78 42 6c 6f 62 73 69 7a 65 28 50 29 0a 23 65 6c  xBlobsize(P).#el
1aee5 73 65 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41  se.# define UPDA
1aee6 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1aee7 50 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  P).#endif../*.**
1aee8 20 43 6f 6e 76 65 72 74 20 74 68 65 20 67 69 76   Convert the giv
1aee9 65 6e 20 72 65 67 69 73 74 65 72 20 69 6e 74 6f  en register into
1aeea 20 61 20 73 74 72 69 6e 67 20 69 66 20 69 74 20   a string if it 
1aeeb 69 73 6e 27 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72  isn't one.** alr
1aeec 65 61 64 79 2e 20 52 65 74 75 72 6e 20 6e 6f 6e  eady. Return non
1aeed 2d 7a 65 72 6f 20 69 66 20 61 20 6d 61 6c 6c 6f  -zero if a mallo
1aeee 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64  c() fails..*/.#d
1aeef 65 66 69 6e 65 20 53 74 72 69 6e 67 69 66 79 28  efine Stringify(
1aef0 50 2c 20 65 6e 63 29 20 5c 0a 20 20 20 69 66 28  P, enc) \.   if(
1aef1 28 28 50 29 2d 3e 66 6c 61 67 73 26 28 4d 45 4d  ((P)->flags&(MEM
1aef2 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d  _Str|MEM_Blob))=
1aef3 3d 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  =0 && sqlite3Vdb
1aef4 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 50 2c  eMemStringify(P,
1aef5 65 6e 63 29 29 20 5c 0a 20 20 20 20 20 7b 20 67  enc)) \.     { g
1aef6 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f  oto no_mem; }../
1aef7 2a 0a 2a 2a 20 41 6e 20 65 70 68 65 6d 65 72 61  *.** An ephemera
1aef8 6c 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 28  l string value (
1aef9 73 69 67 6e 69 66 69 65 64 20 62 79 20 74 68 65  signified by the
1aefa 20 4d 45 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29   MEM_Ephem flag)
1aefb 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70   contains.** a p
1aefc 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 79 6e 61  ointer to a dyna
1aefd 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
1aefe 64 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 73  d string where s
1aeff 6f 6d 65 20 6f 74 68 65 72 20 65 6e 74 69 74 79  ome other entity
1af00 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62  .** is responsib
1af01 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74  le for deallocat
1af02 69 6e 67 20 74 68 61 74 20 73 74 72 69 6e 67 2e  ing that string.
1af03 20 20 42 65 63 61 75 73 65 20 74 68 65 20 72 65    Because the re
1af04 67 69 73 74 65 72 0a 2a 2a 20 64 6f 65 73 20 6e  gister.** does n
1af05 6f 74 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 73  ot control the s
1af06 74 72 69 6e 67 2c 20 69 74 20 6d 69 67 68 74 20  tring, it might 
1af07 62 65 20 64 65 6c 65 74 65 64 20 77 69 74 68 6f  be deleted witho
1af08 75 74 20 74 68 65 20 72 65 67 69 73 74 65 72 0a  ut the register.
1af09 2a 2a 20 6b 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a  ** knowing it..*
1af0a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1af0b 65 20 63 6f 6e 76 65 72 74 73 20 61 6e 20 65 70  e converts an ep
1af0c 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 69  hemeral string i
1af0d 6e 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c  nto a dynamicall
1af0e 79 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73  y allocated.** s
1af0f 74 72 69 6e 67 20 74 68 61 74 20 74 68 65 20 72  tring that the r
1af10 65 67 69 73 74 65 72 20 69 74 73 65 6c 66 20 63  egister itself c
1af11 6f 6e 74 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68  ontrols.  In oth
1af12 65 72 20 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20  er words, it.** 
1af13 63 6f 6e 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f  converts an MEM_
1af14 45 70 68 65 6d 20 73 74 72 69 6e 67 20 69 6e 74  Ephem string int
1af15 6f 20 61 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72  o an MEM_Dyn str
1af16 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ing..*/.#define 
1af17 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 50  Deephemeralize(P
1af18 29 20 5c 0a 20 20 20 69 66 28 20 28 28 50 29 2d  ) \.   if( ((P)-
1af19 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d  >flags&MEM_Ephem
1af1a 29 21 3d 30 20 5c 0a 20 20 20 20 20 20 20 26 26  )!=0 \.       &&
1af1b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
1af1c 61 6b 65 57 72 69 74 65 61 62 6c 65 28 50 29 20  akeWriteable(P) 
1af1d 29 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d  ){ goto no_mem;}
1af1e 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c  ../*.** Call sql
1af1f 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e  ite3VdbeMemExpan
1af20 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73  dBlob() on the s
1af21 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74  upplied value (t
1af22 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69  ype Mem*).** P i
1af23 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23  f required..*/.#
1af24 64 65 66 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f  define ExpandBlo
1af25 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67  b(P) (((P)->flag
1af26 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69  s&MEM_Zero)?sqli
1af27 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
1af28 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a  Blob(P):0)../*.*
1af29 2a 20 41 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20  * Argument pMem 
1af2a 70 6f 69 6e 74 73 20 61 74 20 61 20 72 65 67 69  points at a regi
1af2b 73 74 65 72 20 74 68 61 74 20 77 69 6c 6c 20 62  ster that will b
1af2c 65 20 70 61 73 73 65 64 20 74 6f 20 61 0a 2a 2a  e passed to a.**
1af2d 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75   user-defined fu
1af2e 6e 63 74 69 6f 6e 20 6f 72 20 72 65 74 75 72 6e  nction or return
1af2f 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 20 61  ed to the user a
1af30 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  s the result of 
1af31 61 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20  a query..** The 
1af32 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
1af33 20 27 64 62 5f 65 6e 63 27 20 69 73 20 74 68 65   'db_enc' is the
1af34 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75   text encoding u
1af35 73 65 64 20 62 79 20 74 68 65 20 76 64 62 65 20  sed by the vdbe 
1af36 66 6f 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  for.** register 
1af37 76 61 72 69 61 62 6c 65 73 2e 20 20 54 68 69 73  variables.  This
1af38 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
1af39 65 20 70 4d 65 6d 2d 3e 65 6e 63 20 61 6e 64 20  e pMem->enc and 
1af3a 70 4d 65 6d 2d 3e 74 79 70 65 0a 2a 2a 20 76 61  pMem->type.** va
1af3b 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20  riables used by 
1af3c 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  the sqlite3_valu
1af3d 65 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a  e_*() routines..
1af3e 2a 2f 0a 23 64 65 66 69 6e 65 20 73 74 6f 72 65  */.#define store
1af3f 54 79 70 65 49 6e 66 6f 28 41 2c 42 29 20 5f 73  TypeInfo(A,B) _s
1af40 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 41 29 0a  toreTypeInfo(A).
1af41 73 74 61 74 69 63 20 76 6f 69 64 20 5f 73 74 6f  static void _sto
1af42 72 65 54 79 70 65 49 6e 66 6f 28 4d 65 6d 20 2a  reTypeInfo(Mem *
1af43 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61  pMem){.  int fla
1af44 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  gs = pMem->flags
1af45 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ;.  if( flags & 
1af46 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1af47 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1af48 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20  ITE_NULL;.  }.  
1af49 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
1af4a 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
1af4b 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1af4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d  ITE_INTEGER;.  }
1af4d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67  .  else if( flag
1af4e 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a  s & MEM_Real ){.
1af4f 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1af50 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20   SQLITE_FLOAT;. 
1af51 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c   }.  else if( fl
1af52 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b  ags & MEM_Str ){
1af53 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  .    pMem->type 
1af54 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20  = SQLITE_TEXT;. 
1af55 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d   }else{.    pMem
1af56 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
1af57 42 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  BLOB;.  }.}../*.
1af58 2a 2a 20 50 72 6f 70 65 72 74 69 65 73 20 6f 66  ** Properties of
1af59 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20 4f   opcodes.  The O
1af5a 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52  PFLG_INITIALIZER
1af5b 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65   macro is.** cre
1af5c 61 74 65 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65  ated by mkopcode
1af5d 68 2e 61 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d  h.awk during com
1af5e 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61 74 61 20  pilation.  Data 
1af5f 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  is obtained.** f
1af60 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73  rom the comments
1af61 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22   following the "
1af62 63 61 73 65 20 4f 50 5f 78 78 78 78 3a 22 20 73  case OP_xxxx:" s
1af63 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20  tatements in.** 
1af64 74 68 69 73 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a  this file.  .*/.
1af65 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
1af66 69 67 6e 65 64 20 63 68 61 72 20 6f 70 63 6f 64  igned char opcod
1af67 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50  eProperty[] = OP
1af68 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b  FLG_INITIALIZER;
1af69 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1af6a 72 75 65 20 69 66 20 61 6e 20 6f 70 63 6f 64 65  rue if an opcode
1af6b 20 68 61 73 20 61 6e 79 20 6f 66 20 74 68 65 20   has any of the 
1af6c 4f 50 46 4c 47 5f 78 78 78 20 70 72 6f 70 65 72  OPFLG_xxx proper
1af6d 74 69 65 73 0a 2a 2a 20 73 70 65 63 69 66 69 65  ties.** specifie
1af6e 64 20 62 79 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51  d by mask..*/.SQ
1af6f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1af70 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f   sqlite3VdbeOpco
1af71 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e  deHasProperty(in
1af72 74 20 6f 70 63 6f 64 65 2c 20 69 6e 74 20 6d 61  t opcode, int ma
1af73 73 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6f  sk){.  assert( o
1af74 70 63 6f 64 65 3e 30 20 26 26 20 6f 70 63 6f 64  pcode>0 && opcod
1af75 65 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 6f 70  e<(int)sizeof(op
1af76 63 6f 64 65 50 72 6f 70 65 72 74 79 29 20 29 3b  codeProperty) );
1af77 0a 20 20 72 65 74 75 72 6e 20 28 6f 70 63 6f 64  .  return (opcod
1af78 65 50 72 6f 70 65 72 74 79 5b 6f 70 63 6f 64 65  eProperty[opcode
1af79 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f  ]&mask)!=0;.}../
1af7a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64  *.** Allocate Vd
1af7b 62 65 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  beCursor number 
1af7c 69 43 75 72 2e 20 20 52 65 74 75 72 6e 20 61 20  iCur.  Return a 
1af7d 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20  pointer to it.  
1af7e 52 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69  Return NULL.** i
1af7f 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20  f we run out of 
1af80 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  memory..*/.stati
1af81 63 20 56 64 62 65 43 75 72 73 6f 72 20 2a 61 6c  c VdbeCursor *al
1af82 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 0a 20 20  locateCursor(.  
1af83 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20  Vdbe *p,        
1af84 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
1af85 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a  tual machine */.
1af86 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20    int iCur,     
1af87 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
1af88 20 6f 66 20 74 68 65 20 6e 65 77 20 56 64 62 65   of the new Vdbe
1af89 43 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  Cursor */.  int 
1af8a 6e 46 69 65 6c 64 2c 20 20 20 20 20 20 20 20 20  nField,         
1af8b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
1af8c 69 65 6c 64 73 20 69 6e 20 74 68 65 20 74 61 62  ields in the tab
1af8d 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20  le or index */. 
1af8e 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20   int iDb,       
1af8f 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 64         /* When d
1af90 61 74 61 62 61 73 65 20 74 68 65 20 63 75 72 73  atabase the curs
1af91 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f  or belongs to, o
1af92 72 20 2d 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73  r -1 */.  int is
1af93 42 74 72 65 65 43 75 72 73 6f 72 20 20 20 20 20  BtreeCursor     
1af94 2f 2a 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69  /* */.){.  /* Fi
1af95 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65  nd the memory ce
1af96 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ll that will be 
1af97 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
1af98 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79  e blob of memory
1af99 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 66  .  ** required f
1af9a 6f 72 20 74 68 69 73 20 56 64 62 65 43 75 72 73  or this VdbeCurs
1af9b 6f 72 20 73 74 72 75 63 74 75 72 65 2e 20 49 74  or structure. It
1af9c 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74   is convenient t
1af9d 6f 20 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 64  o use a .  ** vd
1af9e 62 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74  be memory cell t
1af9f 6f 20 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d  o manage the mem
1afa0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
1afa1 65 71 75 69 72 65 64 20 66 6f 72 20 61 0a 20 20  equired for a.  
1afa2 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 20 73 74  ** VdbeCursor st
1afa3 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20  ructure for the 
1afa4 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e  following reason
1afa5 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a  s:.  **.  **   *
1afa6 20 53 6f 6d 65 74 69 6d 65 73 20 63 75 72 73 6f   Sometimes curso
1afa7 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73  r numbers are us
1afa8 65 64 20 66 6f 72 20 61 20 63 6f 75 70 6c 65 20  ed for a couple 
1afa9 6f 66 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a  of different.  *
1afaa 2a 20 20 20 20 20 70 75 72 70 6f 73 65 73 20 69  *     purposes i
1afab 6e 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d  n a vdbe program
1afac 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 74 20  . The different 
1afad 75 73 65 73 20 6d 69 67 68 74 20 72 65 71 75 69  uses might requi
1afae 72 65 0a 20 20 2a 2a 20 20 20 20 20 64 69 66 66  re.  **     diff
1afaf 65 72 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f  erent sized allo
1afb0 63 61 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20  cations. Memory 
1afb1 63 65 6c 6c 73 20 70 72 6f 76 69 64 65 20 67 72  cells provide gr
1afb2 6f 77 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20  owable.  **     
1afb3 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a  allocations..  *
1afb4 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20  *.  **   * When 
1afb5 75 73 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d  using ENABLE_MEM
1afb6 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20  ORY_MANAGEMENT, 
1afb7 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66 66  memory cell buff
1afb8 65 72 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20  ers can.  **    
1afb9 20 62 65 20 66 72 65 65 64 20 6c 61 7a 69 6c 79   be freed lazily
1afba 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33   via the sqlite3
1afbb 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
1afbc 29 20 41 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a  ) API. This.  **
1afbd 20 20 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74       minimizes t
1afbe 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c  he number of mal
1afbf 6c 6f 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 62  loc calls made b
1afc0 79 20 74 68 65 20 73 79 73 74 65 6d 2e 0a 20 20  y the system..  
1afc1 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63  **.  ** Memory c
1afc2 65 6c 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 73  ells for cursors
1afc3 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61   are allocated a
1afc4 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  t the top of the
1afc5 20 61 64 64 72 65 73 73 0a 20 20 2a 2a 20 73 70   address.  ** sp
1afc6 61 63 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c  ace. Memory cell
1afc7 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65   (p->nMem) corre
1afc8 73 70 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f 72  sponds to cursor
1afc9 20 30 2e 20 53 70 61 63 65 20 66 6f 72 0a 20 20   0. Space for.  
1afca 2a 2a 20 63 75 72 73 6f 72 20 31 20 69 73 20 6d  ** cursor 1 is m
1afcb 61 6e 61 67 65 64 20 62 79 20 6d 65 6d 6f 72 79  anaged by memory
1afcc 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31   cell (p->nMem-1
1afcd 29 2c 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d  ), etc..  */.  M
1afce 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61  em *pMem = &p->a
1afcf 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72  Mem[p->nMem-iCur
1afd0 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b  ];..  int nByte;
1afd1 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1afd2 43 78 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20  Cx = 0;.  nByte 
1afd3 3d 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28  = .      sizeof(
1afd4 56 64 62 65 43 75 72 73 6f 72 29 20 2b 20 0a 20  VdbeCursor) + . 
1afd5 20 20 20 20 20 28 69 73 42 74 72 65 65 43 75 72       (isBtreeCur
1afd6 73 6f 72 3f 73 71 6c 69 74 65 33 42 74 72 65 65  sor?sqlite3Btree
1afd7 43 75 72 73 6f 72 53 69 7a 65 28 29 3a 30 29 20  CursorSize():0) 
1afd8 2b 20 0a 20 20 20 20 20 20 32 2a 6e 46 69 65 6c  + .      2*nFiel
1afd9 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a  d*sizeof(u32);..
1afda 20 20 61 73 73 65 72 74 28 20 69 43 75 72 3c 70    assert( iCur<p
1afdb 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 69  ->nCursor );.  i
1afdc 66 28 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72  f( p->apCsr[iCur
1afdd 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
1afde 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70  VdbeFreeCursor(p
1afdf 2c 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d  , p->apCsr[iCur]
1afe0 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b  );.    p->apCsr[
1afe1 69 43 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20  iCur] = 0;.  }. 
1afe2 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
1afe3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1afe4 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20  ow(pMem, nByte, 
1afe5 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43  0) ){.    p->apC
1afe6 73 72 5b 69 43 75 72 5d 20 3d 20 70 43 78 20 3d  sr[iCur] = pCx =
1afe7 20 28 56 64 62 65 43 75 72 73 6f 72 2a 29 70 4d   (VdbeCursor*)pM
1afe8 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65  em->z;.    memse
1afe9 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42  t(pMem->z, 0, nB
1afea 79 74 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69  yte);.    pCx->i
1afeb 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43  Db = iDb;.    pC
1afec 78 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65  x->nField = nFie
1afed 6c 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65  ld;.    if( nFie
1afee 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d  ld ){.      pCx-
1afef 3e 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29  >aType = (u32 *)
1aff0 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28  &pMem->z[sizeof(
1aff1 56 64 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20  VdbeCursor)];.  
1aff2 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 42 74    }.    if( isBt
1aff3 72 65 65 43 75 72 73 6f 72 20 29 7b 0a 20 20 20  reeCursor ){.   
1aff4 20 20 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20     pCx->pCursor 
1aff5 3d 20 28 42 74 43 75 72 73 6f 72 2a 29 0a 20 20  = (BtCursor*).  
1aff6 20 20 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a          &pMem->z
1aff7 5b 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73  [sizeof(VdbeCurs
1aff8 6f 72 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a  or)+2*nField*siz
1aff9 65 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d  eof(u32)];.    }
1affa 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43  .  }.  return pC
1affb 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20  x;.}../*.** Try 
1affc 74 6f 20 63 6f 6e 76 65 72 74 20 61 20 76 61 6c  to convert a val
1affd 75 65 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69  ue into a numeri
1affe 63 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  c representation
1afff 20 69 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f   if we can.** do
1b000 20 73 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73 73   so without loss
1b001 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e   of information.
1b002 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
1b003 2c 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a  , if the string.
1b004 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20  ** looks like a 
1b005 6e 75 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74 20  number, convert 
1b006 69 74 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72  it into a number
1b007 2e 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f  .  If it does no
1b008 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61  t.** look like a
1b009 20 6e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20 69   number, leave i
1b00a 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74  t alone..*/.stat
1b00b 69 63 20 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d  ic void applyNum
1b00c 65 72 69 63 41 66 66 69 6e 69 74 79 28 4d 65 6d  ericAffinity(Mem
1b00d 20 2a 70 52 65 63 29 7b 0a 20 20 69 66 28 20 28   *pRec){.  if( (
1b00e 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pRec->flags & (M
1b00f 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29  EM_Real|MEM_Int)
1b010 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
1b011 72 65 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c  realnum;.    sql
1b012 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
1b013 72 6d 69 6e 61 74 65 28 70 52 65 63 29 3b 0a 20  rminate(pRec);. 
1b014 20 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c     if( (pRec->fl
1b015 61 67 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20  ags&MEM_Str).   
1b016 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
1b017 49 73 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a  IsNumber(pRec->z
1b018 2c 20 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63  , &realnum, pRec
1b019 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20 20  ->enc) ){.      
1b01a 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20  i64 value;.     
1b01b 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1b01c 67 65 45 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c  geEncoding(pRec,
1b01d 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20   SQLITE_UTF8);. 
1b01e 20 20 20 20 20 69 66 28 20 21 72 65 61 6c 6e 75       if( !realnu
1b01f 6d 20 26 26 20 73 71 6c 69 74 65 33 41 74 6f 69  m && sqlite3Atoi
1b020 36 34 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c  64(pRec->z, &val
1b021 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ue) ){.        p
1b022 52 65 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65  Rec->u.i = value
1b023 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 65 74  ;.        MemSet
1b024 54 79 70 65 46 6c 61 67 28 70 52 65 63 2c 20 4d  TypeFlag(pRec, M
1b025 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d  EM_Int);.      }
1b026 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
1b027 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c  lite3VdbeMemReal
1b028 69 66 79 28 70 52 65 63 29 3b 0a 20 20 20 20 20  ify(pRec);.     
1b029 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
1b02a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67  /*.** Processing
1b02b 20 69 73 20 64 65 74 65 72 6d 69 6e 65 20 62 79   is determine by
1b02c 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 70 61   the affinity pa
1b02d 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53  rameter:.**.** S
1b02e 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
1b02f 52 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46  R:.** SQLITE_AFF
1b030 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45  _REAL:.** SQLITE
1b031 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a  _AFF_NUMERIC:.**
1b032 20 20 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65      Try to conve
1b033 72 74 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e  rt pRec to an in
1b034 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61  teger representa
1b035 74 69 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20  tion or a .**   
1b036 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20   floating-point 
1b037 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69  representation i
1b038 66 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70  f an integer rep
1b039 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20  resentation.**  
1b03a 20 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c    is not possibl
1b03b 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68  e.  Note that th
1b03c 65 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73  e integer repres
1b03d 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20  entation is.**  
1b03e 20 20 61 6c 77 61 79 73 20 70 72 65 66 65 72 72    always preferr
1b03f 65 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20  ed, even if the 
1b040 61 66 66 69 6e 69 74 79 20 69 73 20 52 45 41 4c  affinity is REAL
1b041 2c 20 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 20  , because.**    
1b042 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65  an integer repre
1b043 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72  sentation is mor
1b044 65 20 73 70 61 63 65 20 65 66 66 69 63 69 65 6e  e space efficien
1b045 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  t on disk..**.**
1b046 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
1b047 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 20  :.**    Convert 
1b048 70 52 65 63 20 74 6f 20 61 20 74 65 78 74 20 72  pRec to a text r
1b049 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a  epresentation..*
1b04a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  *.** SQLITE_AFF_
1b04b 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f  NONE:.**    No-o
1b04c 70 2e 20 20 70 52 65 63 20 69 73 20 75 6e 63 68  p.  pRec is unch
1b04d 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  anged..*/.static
1b04e 20 76 6f 69 64 20 61 70 70 6c 79 41 66 66 69 6e   void applyAffin
1b04f 69 74 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63  ity(.  Mem *pRec
1b050 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
1b051 65 20 76 61 6c 75 65 20 74 6f 20 61 70 70 6c 79  e value to apply
1b052 20 61 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a   affinity to */.
1b053 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c    char affinity,
1b054 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66        /* The aff
1b055 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c  inity to be appl
1b056 69 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20  ied */.  u8 enc 
1b057 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b058 55 73 65 20 74 68 69 73 20 74 65 78 74 20 65 6e  Use this text en
1b059 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69  coding */.){.  i
1b05a 66 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  f( affinity==SQL
1b05b 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a  ITE_AFF_TEXT ){.
1b05c 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65      /* Only atte
1b05d 6d 70 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69  mpt the conversi
1b05e 6f 6e 20 74 6f 20 54 45 58 54 20 69 66 20 74 68  on to TEXT if th
1b05f 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65  ere is an intege
1b060 72 20 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a  r or real.    **
1b061 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1b062 28 62 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64  (blob and NULL d
1b063 6f 20 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 72  o not get conver
1b064 74 65 64 29 20 62 75 74 20 6e 6f 20 73 74 72 69  ted) but no stri
1b065 6e 67 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73  ng.    ** repres
1b066 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  entation..    */
1b067 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 52 65  .    if( 0==(pRe
1b068 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72  c->flags&MEM_Str
1b069 29 20 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 67  ) && (pRec->flag
1b06a 73 26 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f  s&(MEM_Real|MEM_
1b06b 49 6e 74 29 29 20 29 7b 0a 20 20 20 20 20 20 73  Int)) ){.      s
1b06c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
1b06d 69 6e 67 69 66 79 28 70 52 65 63 2c 20 65 6e 63  ingify(pRec, enc
1b06e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65  );.    }.    pRe
1b06f 63 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45  c->flags &= ~(ME
1b070 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b  M_Real|MEM_Int);
1b071 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 66 66  .  }else if( aff
1b072 69 6e 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 46  inity!=SQLITE_AF
1b073 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73  F_NONE ){.    as
1b074 73 65 72 74 28 20 61 66 66 69 6e 69 74 79 3d 3d  sert( affinity==
1b075 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
1b076 45 52 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d  ER || affinity==
1b077 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a  SQLITE_AFF_REAL.
1b078 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
1b079 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
1b07a 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a  _AFF_NUMERIC );.
1b07b 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63      applyNumeric
1b07c 41 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a  Affinity(pRec);.
1b07d 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c      if( pRec->fl
1b07e 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29  ags & MEM_Real )
1b07f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1b080 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69  dbeIntegerAffini
1b081 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a  ty(pRec);.    }.
1b082 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79    }.}../*.** Try
1b083 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20   to convert the 
1b084 74 79 70 65 20 6f 66 20 61 20 66 75 6e 63 74 69  type of a functi
1b085 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 61  on argument or a
1b086 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a   result column.*
1b087 2a 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63  * into a numeric
1b088 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e   representation.
1b089 20 20 55 73 65 20 65 69 74 68 65 72 20 49 4e 54    Use either INT
1b08a 45 47 45 52 20 6f 72 20 52 45 41 4c 20 77 68 69  EGER or REAL whi
1b08b 63 68 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 70  chever.** is app
1b08c 72 6f 70 72 69 61 74 65 2e 20 20 42 75 74 20 6f  ropriate.  But o
1b08d 6e 6c 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 65  nly do the conve
1b08e 72 73 69 6f 6e 20 69 66 20 69 74 20 69 73 20 70  rsion if it is p
1b08f 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 0a  ossible without.
1b090 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72  ** loss of infor
1b091 6d 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72  mation and retur
1b092 6e 20 74 68 65 20 72 65 76 69 73 65 64 20 74 79  n the revised ty
1b093 70 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65  pe of the argume
1b094 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  nt..**.** This i
1b095 73 20 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 41  s an EXPERIMENTA
1b096 4c 20 61 70 69 20 61 6e 64 20 69 73 20 73 75 62  L api and is sub
1b097 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f  ject to change o
1b098 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51  r removal..*/.SQ
1b099 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1b09a 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72  ite3_value_numer
1b09b 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f  ic_type(sqlite3_
1b09c 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
1b09d 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d  Mem *pMem = (Mem
1b09e 2a 29 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e  *)pVal;.  applyN
1b09f 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70  umericAffinity(p
1b0a0 4d 65 6d 29 3b 0a 20 20 73 74 6f 72 65 54 79 70  Mem);.  storeTyp
1b0a1 65 49 6e 66 6f 28 70 4d 65 6d 2c 20 30 29 3b 0a  eInfo(pMem, 0);.
1b0a2 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74    return pMem->t
1b0a3 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78  ype;.}../*.** Ex
1b0a4 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e 20 6f  ported version o
1b0a5 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28  f applyAffinity(
1b0a6 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b  ). This one work
1b0a7 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  s on sqlite3_val
1b0a8 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65  ue*, .** not the
1b0a9 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74   internal Mem* t
1b0aa 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ype..*/.SQLITE_P
1b0ab 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1b0ac 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66  te3ValueApplyAff
1b0ad 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 65 33  inity(.  sqlite3
1b0ae 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20  _value *pVal, . 
1b0af 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 0a 20   u8 affinity, . 
1b0b0 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70   u8 enc.){.  app
1b0b1 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20  lyAffinity((Mem 
1b0b2 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79  *)pVal, affinity
1b0b3 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65  , enc);.}..#ifde
1b0b4 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
1b0b5 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63  *.** Write a nic
1b0b6 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  e string represe
1b0b7 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63  ntation of the c
1b0b8 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20  ontents of cell 
1b0b9 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66  pMem.** into buf
1b0ba 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68  fer zBuf, length
1b0bb 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45   nBuf..*/.SQLITE
1b0bc 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1b0bd 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74  lite3VdbeMemPret
1b0be 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65  tyPrint(Mem *pMe
1b0bf 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  m, char *zBuf){.
1b0c0 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a    char *zCsr = z
1b0c1 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70  Buf;.  int f = p
1b0c2 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73  Mem->flags;..  s
1b0c3 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
1b0c4 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73   *const encnames
1b0c5 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38  [] = {"(X)", "(8
1b0c6 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28  )", "(16LE)", "(
1b0c7 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20  16BE)"};..  if( 
1b0c8 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20  f&MEM_Blob ){.  
1b0c9 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61    int i;.    cha
1b0ca 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26  r c;.    if( f &
1b0cb 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20   MEM_Dyn ){.    
1b0cc 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20    c = 'z';.     
1b0cd 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
1b0ce 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70  EM_Static|MEM_Ep
1b0cf 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  hem))==0 );.    
1b0d0 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45  }else if( f & ME
1b0d1 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  M_Static ){.    
1b0d2 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20    c = 't';.     
1b0d3 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
1b0d4 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d  EM_Dyn|MEM_Ephem
1b0d5 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c  ))==0 );.    }el
1b0d6 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45  se if( f & MEM_E
1b0d7 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20  phem ){.      c 
1b0d8 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73  = 'e';.      ass
1b0d9 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53  ert( (f & (MEM_S
1b0da 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d  tatic|MEM_Dyn))=
1b0db 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  =0 );.    }else{
1b0dc 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a  .      c = 's';.
1b0dd 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
1b0de 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c  e3_snprintf(100,
1b0df 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b   zCsr, "%c", c);
1b0e0 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c  .    zCsr += sql
1b0e1 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73  ite3Strlen30(zCs
1b0e2 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  r);.    sqlite3_
1b0e3 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43  snprintf(100, zC
1b0e4 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d  sr, "%d[", pMem-
1b0e5 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d  >n);.    zCsr +=
1b0e6 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1b0e7 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 72 28  (zCsr);.    for(
1b0e8 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c 70  i=0; i<16 && i<p
1b0e9 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Mem->n; i++){.  
1b0ea 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1b0eb 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20  intf(100, zCsr, 
1b0ec 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29 70 4d  "%02X", ((int)pM
1b0ed 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 46 29  em->z[i] & 0xFF)
1b0ee 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d  );.      zCsr +=
1b0ef 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1b0f0 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20  (zCsr);.    }.  
1b0f1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20    for(i=0; i<16 
1b0f2 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b  && i<pMem->n; i+
1b0f3 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a  +){.      char z
1b0f4 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20   = pMem->z[i];. 
1b0f5 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20 7c 7c       if( z<32 ||
1b0f6 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 2b 2b   z>126 ) *zCsr++
1b0f7 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 65 6c   = '.';.      el
1b0f8 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b 0a  se *zCsr++ = z;.
1b0f9 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
1b0fa 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c  e3_snprintf(100,
1b0fb 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 65 6e   zCsr, "]%s", en
1b0fc 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63  cnames[pMem->enc
1b0fd 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20  ]);.    zCsr += 
1b0fe 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1b0ff 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 66  zCsr);.    if( f
1b100 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20   & MEM_Zero ){. 
1b101 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1b102 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c  rintf(100, zCsr,
1b103 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 6e  "+%dz",pMem->u.n
1b104 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a 43 73  Zero);.      zCs
1b105 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  r += sqlite3Strl
1b106 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20  en30(zCsr);.    
1b107 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c  }.    *zCsr = '\
1b108 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  0';.  }else if( 
1b109 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20  f & MEM_Str ){. 
1b10a 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20     int j, k;.   
1b10b 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a   zBuf[0] = ' ';.
1b10c 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f      if( f & MEM_
1b10d 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75  Dyn ){.      zBu
1b10e 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20  f[1] = 'z';.    
1b10f 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28    assert( (f & (
1b110 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45  MEM_Static|MEM_E
1b111 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20  phem))==0 );.   
1b112 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d   }else if( f & M
1b113 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20  EM_Static ){.   
1b114 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27     zBuf[1] = 't'
1b115 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1b116 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45  (f & (MEM_Dyn|ME
1b117 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a  M_Ephem))==0 );.
1b118 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20      }else if( f 
1b119 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20  & MEM_Ephem ){. 
1b11a 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27       zBuf[1] = '
1b11b 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  e';.      assert
1b11c 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74  ( (f & (MEM_Stat
1b11d 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20  ic|MEM_Dyn))==0 
1b11e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1b11f 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73      zBuf[1] = 's
1b120 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d  ';.    }.    k =
1b121 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   2;.    sqlite3_
1b122 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a  snprintf(100, &z
1b123 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d  Buf[k], "%d", pM
1b124 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d  em->n);.    k +=
1b125 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1b126 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20  (&zBuf[k]);.    
1b127 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b  zBuf[k++] = '[';
1b128 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
1b129 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b  15 && j<pMem->n;
1b12a 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20   j++){.      u8 
1b12b 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a  c = pMem->z[j];.
1b12c 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30 78 32        if( c>=0x2
1b12d 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b 0a 20  0 && c<0x7f ){. 
1b12e 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d         zBuf[k++]
1b12f 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = c;.      }els
1b130 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b  e{.        zBuf[
1b131 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20  k++] = '.';.    
1b132 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42    }.    }.    zB
1b133 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20  uf[k++] = ']';. 
1b134 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1b135 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d  ntf(100,&zBuf[k]
1b136 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d  , encnames[pMem-
1b137 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d  >enc]);.    k +=
1b138 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1b139 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20  (&zBuf[k]);.    
1b13a 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20  zBuf[k++] = 0;. 
1b13b 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66   }.}.#endif..#if
1b13c 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1b13d 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65  ./*.** Print the
1b13e 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65 67 69   value of a regi
1b13f 73 74 65 72 20 66 6f 72 20 74 72 61 63 69 6e 67  ster for tracing
1b140 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74   purposes:.*/.st
1b141 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 72 61  atic void memTra
1b142 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75  cePrint(FILE *ou
1b143 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66  t, Mem *p){.  if
1b144 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
1b145 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72  _Null ){.    fpr
1b146 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c  intf(out, " NULL
1b147 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ");.  }else if( 
1b148 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  (p->flags & (MEM
1b149 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d  _Int|MEM_Str))==
1b14a 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72  (MEM_Int|MEM_Str
1b14b 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66  ) ){.    fprintf
1b14c 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22  (out, " si:%lld"
1b14d 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c  , p->u.i);.  }el
1b14e 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  se if( p->flags 
1b14f 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
1b150 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
1b151 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29  i:%lld", p->u.i)
1b152 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
1b153 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
1b154 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66  l ){.    fprintf
1b155 28 6f 75 74 2c 20 22 20 72 3a 25 67 22 2c 20 70  (out, " r:%g", p
1b156 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ->r);.  }else if
1b157 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
1b158 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 66  _RowSet ){.    f
1b159 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 28 72  printf(out, " (r
1b15a 6f 77 73 65 74 29 22 29 3b 0a 20 20 7d 65 6c 73  owset)");.  }els
1b15b 65 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66  e{.    char zBuf
1b15c 5b 32 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74  [200];.    sqlit
1b15d 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
1b15e 72 69 6e 74 28 70 2c 20 7a 42 75 66 29 3b 0a 20  rint(p, zBuf);. 
1b15f 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
1b160 22 20 22 29 3b 0a 20 20 20 20 66 70 72 69 6e 74  " ");.    fprint
1b161 66 28 6f 75 74 2c 20 22 25 73 22 2c 20 7a 42 75  f(out, "%s", zBu
1b162 66 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  f);.  }.}.static
1b163 20 76 6f 69 64 20 72 65 67 69 73 74 65 72 54 72   void registerTr
1b164 61 63 65 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69  ace(FILE *out, i
1b165 6e 74 20 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29  nt iReg, Mem *p)
1b166 7b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  {.  fprintf(out,
1b167 20 22 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69   "REG[%d] = ", i
1b168 52 65 67 29 3b 0a 20 20 6d 65 6d 54 72 61 63 65  Reg);.  memTrace
1b169 50 72 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0a 20  Print(out, p);. 
1b16a 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c   fprintf(out, "\
1b16b 6e 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  n");.}.#endif..#
1b16c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1b16d 55 47 0a 23 20 20 64 65 66 69 6e 65 20 52 45 47  UG.#  define REG
1b16e 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d 29  ISTER_TRACE(R,M)
1b16f 20 69 66 28 70 2d 3e 74 72 61 63 65 29 72 65 67   if(p->trace)reg
1b170 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72  isterTrace(p->tr
1b171 61 63 65 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a 23  ace,R,M).#else.#
1b172 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45    define REGISTE
1b173 52 5f 54 52 41 43 45 28 52 2c 4d 29 0a 23 65 6e  R_TRACE(R,M).#en
1b174 64 69 66 0a 0a 0a 23 69 66 64 65 66 20 56 44 42  dif...#ifdef VDB
1b175 45 5f 50 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a  E_PROFILE../* .*
1b176 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61  * hwtime.h conta
1b177 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d  ins inline assem
1b178 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d  bler code for im
1b179 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68  plementing .** h
1b17a 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20  igh-performance 
1b17b 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e  timing routines.
1b17c 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
1b17d 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69  *** Include hwti
1b17e 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  me.h in the midd
1b17f 6c 65 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a  le of vdbe.c ***
1b180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b181 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1b182 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
1b183 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
1b184 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b185 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b186 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d  **/./*.** 2008 M
1b187 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 27.**.** The 
1b188 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1b189 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1b18a 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1b18b 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1b18c 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1b18d 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1b18e 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1b18f 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1b190 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1b191 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1b192 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1b193 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1b194 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1b195 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1b196 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1b197 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1b198 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1b199 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b19a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b19b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b19c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b19d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1b19e 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1b19f 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f  ns inline asm co
1b1a0 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e  de for retrievin
1b1a1 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61  g "high-performa
1b1a2 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73  nce".** counters
1b1a3 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43   for x86 class C
1b1a4 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  PUs..**.** $Id: 
1b1a5 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32  hwtime.h,v 1.3 2
1b1a6 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a  008/08/01 14:33:
1b1a7 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  15 shane Exp $.*
1b1a8 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d  /.#ifndef _HWTIM
1b1a9 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57  E_H_.#define _HW
1b1aa 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54  TIME_H_../*.** T
1b1ab 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
1b1ac 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tine only works 
1b1ad 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73  on pentium-class
1b1ae 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63   (or newer) proc
1b1af 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73  essors..** It us
1b1b0 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63  es the RDTSC opc
1b1b1 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20  ode to read the 
1b1b2 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75  cycle count valu
1b1b3 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20  e out of the.** 
1b1b4 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65  processor and re
1b1b5 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65  turns that value
1b1b6 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  .  This can be u
1b1b7 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73  sed for high-res
1b1b8 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a  .** profiling..*
1b1b9 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f  /.#if (defined(_
1b1ba 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69  _GNUC__) || defi
1b1bb 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26  ned(_MSC_VER)) &
1b1bc 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e  & \.      (defin
1b1bd 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69  ed(i386) || defi
1b1be 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c  ned(__i386__) ||
1b1bf 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36   defined(_M_IX86
1b1c0 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65  ))..  #if define
1b1c1 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f  d(__GNUC__)..  _
1b1c2 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
1b1c3 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
1b1c4 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
1b1c5 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c    unsigned int l
1b1c6 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73  o, hi;.     __as
1b1c7 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
1b1c8 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22   ("rdtsc" : "=a"
1b1c9 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29   (lo), "=d" (hi)
1b1ca 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28  );.     return (
1b1cb 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69  sqlite_uint64)hi
1b1cc 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d   << 32 | lo;.  }
1b1cd 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65  ..  #elif define
1b1ce 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f  d(_MSC_VER)..  _
1b1cf 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29  _declspec(naked)
1b1d0 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65   __inline sqlite
1b1d1 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20  _uint64 __cdecl 
1b1d2 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
1b1d3 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20  id){.     __asm 
1b1d4 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a  {.        rdtsc.
1b1d5 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20          ret     
1b1d6 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65    ; return value
1b1d7 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20   at EDX:EAX.    
1b1d8 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66   }.  }..  #endif
1b1d9 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64  ..#elif (defined
1b1da 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65  (__GNUC__) && de
1b1db 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f  fined(__x86_64__
1b1dc 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ))..  __inline__
1b1dd 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
1b1de 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
1b1df 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  d){.      unsign
1b1e0 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20  ed long val;.   
1b1e1 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
1b1e2 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
1b1e3 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a   : "=A" (val));.
1b1e4 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c        return val
1b1e5 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64  ;.  }. .#elif (d
1b1e6 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
1b1e7 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70   && defined(__pp
1b1e8 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  c__))..  __inlin
1b1e9 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
1b1ea 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
1b1eb 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
1b1ec 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
1b1ed 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e  retval;.      un
1b1ee 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b  signed long junk
1b1ef 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
1b1f0 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c  __volatile__ ("\
1b1f1 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20  n\.          1: 
1b1f2 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c       mftbu   %1\
1b1f3 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
1b1f4 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30       mftb    %L0
1b1f5 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
1b1f6 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30        mftbu   %0
1b1f7 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
1b1f8 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30        cmpw    %0
1b1f9 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  ,%1\n\.         
1b1fa 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20           bne    
1b1fb 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20   1b".           
1b1fc 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72         : "=r" (r
1b1fd 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75  etval), "=r" (ju
1b1fe 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75  nk));.      retu
1b1ff 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a  rn retval;.  }..
1b200 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20  #else..  #error 
1b201 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74  Need implementat
1b202 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77  ion of sqlite3Hw
1b203 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
1b204 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a  platform...  /*.
1b205 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20    ** To compile 
1b206 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e  without implemen
1b207 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69  ting sqlite3Hwti
1b208 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
1b209 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75  atform,.  ** you
1b20a 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20   can remove the 
1b20b 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64  above #error and
1b20c 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69   use the followi
1b20d 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e  ng.  ** stub fun
1b20e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c  ction.  You will
1b20f 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70   lose timing sup
1b210 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20  port for many.  
1b211 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67  ** of the debugg
1b212 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20  ing and testing 
1b213 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69  utilities, but i
1b214 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a  t should at.  **
1b215 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61   least compile a
1b216 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c  nd run..  */.SQL
1b217 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71  ITE_PRIVATE   sq
1b218 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
1b219 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
1b21a 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65   return ((sqlite
1b21b 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23  _uint64)0); }..#
1b21c 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
1b21d 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d   !defined(_HWTIM
1b21e 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  E_H_) */../*****
1b21f 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1b220 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a   hwtime.h ******
1b221 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b222 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b223 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1b224 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
1b225 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
1b226 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 63  ft off in vdbe.c
1b227 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1b228 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69  ********/..#endi
1b229 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45  f../*.** The CHE
1b22a 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54  CK_FOR_INTERRUPT
1b22b 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 68   macro defined h
1b22c 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65  ere looks to see
1b22d 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74   if the.** sqlit
1b22e 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72  e3_interrupt() r
1b22f 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20  outine has been 
1b230 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 20 68  called.  If it h
1b231 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a  as been, then.**
1b232 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74   processing of t
1b233 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20  he VDBE program 
1b234 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 2e 0a  is interrupted..
1b235 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f  **.** This macro
1b236 20 61 64 64 65 64 20 74 6f 20 65 76 65 72 79 20   added to every 
1b237 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74  instruction that
1b238 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e 20   does a jump in 
1b239 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c  order to.** impl
1b23a 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54  ement a loop.  T
1b23b 68 69 73 20 74 65 73 74 20 75 73 65 64 20 74 6f  his test used to
1b23c 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73 69 6e   be on every sin
1b23d 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c  gle instruction,
1b23e 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d 65 61  .** but that mea
1b23f 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73 74 69  nt we more testi
1b240 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65 64 65  ng that we neede
1b241 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 73 74  d.  By only test
1b242 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20  ing the.** flag 
1b243 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74  on jump instruct
1b244 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 20 28  ions, we get a (
1b245 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 6d 70  small) speed imp
1b246 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65  rovement..*/.#de
1b247 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49  fine CHECK_FOR_I
1b248 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20 69 66  NTERRUPT \.   if
1b249 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72  ( db->u1.isInter
1b24a 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20 61 62  rupted ) goto ab
1b24b 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72  ort_due_to_inter
1b24c 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 53 51  rupt;..#ifdef SQ
1b24d 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69  LITE_DEBUG.stati
1b24e 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73 74 73  c int fileExists
1b24f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
1b250 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 29  nst char *zFile)
1b251 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b  {.  int res = 0;
1b252 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1b253 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51  TE_OK;.#ifdef SQ
1b254 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49  LITE_TEST.  /* I
1b255 66 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74  f we are current
1b256 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20 65 72  ly testing IO er
1b257 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  rors, then do no
1b258 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 73 28  t call OsAccess(
1b259 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66  ) to.  ** test f
1b25a 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63 65 20  or the presence 
1b25b 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 20 69  of zFile. This i
1b25c 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 49 4f  s because any IO
1b25d 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a   error that.  **
1b25e 20 6f 63 63 75 72 73 20 68 65 72 65 20 77 69 6c   occurs here wil
1b25f 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 74 65  l not be reporte
1b260 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65 20 74  d, causing the t
1b261 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a  est to fail..  *
1b262 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73  /.  extern int s
1b263 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
1b264 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20 73  pending;.  if( s
1b265 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
1b266 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e  pending<=0 ).#en
1b267 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  dif.    rc = sql
1b268 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d  ite3OsAccess(db-
1b269 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51  >pVfs, zFile, SQ
1b26a 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
1b26b 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 65 74  TS, &res);.  ret
1b26c 75 72 6e 20 28 72 65 73 20 26 26 20 72 63 3d 3d  urn (res && rc==
1b26d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65  SQLITE_OK);.}.#e
1b26e 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44  ndif..#ifndef ND
1b26f 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EBUG./*.** This 
1b270 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  function is only
1b271 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
1b272 68 69 6e 20 61 6e 20 61 73 73 65 72 74 28 29 20  hin an assert() 
1b273 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a  expression. It.*
1b274 2a 20 63 68 65 63 6b 73 20 74 68 61 74 20 74 68  * checks that th
1b275 65 20 73 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73  e sqlite3.nTrans
1b276 61 63 74 69 6f 6e 20 76 61 72 69 61 62 6c 65 20  action variable 
1b277 69 73 20 63 6f 72 72 65 63 74 6c 79 20 73 65 74  is correctly set
1b278 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65   to.** the numbe
1b279 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63  r of non-transac
1b27a 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73 20  tion savepoints 
1b27b 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65  currently in the
1b27c 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74   .** linked list
1b27d 20 73 74 61 72 74 69 6e 67 20 61 74 20 73 71 6c   starting at sql
1b27e 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e  ite3.pSavepoint.
1b27f 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a  .** .** Usage:.*
1b280 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28  *.**     assert(
1b281 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43   checkSavepointC
1b282 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73  ount(db) );.*/.s
1b283 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 53  tatic int checkS
1b284 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71  avepointCount(sq
1b285 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e  lite3 *db){.  in
1b286 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76 65 70  t n = 0;.  Savep
1b287 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70  oint *p;.  for(p
1b288 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b  =db->pSavepoint;
1b289 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20   p; p=p->pNext) 
1b28a 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  n++;.  assert( n
1b28b 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e  ==(db->nSavepoin
1b28c 74 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e 73 61  t + db->isTransa
1b28d 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20  ctionSavepoint) 
1b28e 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  );.  return 1;.}
1b28f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45  .#endif../*.** E
1b290 78 65 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f  xecute as much o
1b291 66 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d  f a VDBE program
1b292 20 61 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20   as we can then 
1b293 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71  return..**.** sq
1b294 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61  lite3VdbeMakeRea
1b295 64 79 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c  dy() must be cal
1b296 6c 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20  led before this 
1b297 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72  routine in order
1b298 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65   to.** close the
1b299 20 70 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20   program with a 
1b29a 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e  final OP_Halt an
1b29b 64 20 74 6f 20 73 65 74 20 75 70 20 74 68 65 20  d to set up the 
1b29c 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64  callbacks.** and
1b29d 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
1b29e 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  ge pointer..**.*
1b29f 2a 20 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77  * Whenever a row
1b2a0 20 6f 72 20 72 65 73 75 6c 74 20 64 61 74 61 20   or result data 
1b2a1 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68  is available, th
1b2a2 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1b2a3 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65  either.** invoke
1b2a4 20 74 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c   the result call
1b2a5 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69  back (if there i
1b2a6 73 20 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e  s one) or return
1b2a7 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f   with.** SQLITE_
1b2a8 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ROW..**.** If an
1b2a9 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
1b2aa 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65   to open a locke
1b2ab 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e  d database, then
1b2ac 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
1b2ad 20 77 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76   will either inv
1b2ae 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63 61 6c  oke the busy cal
1b2af 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20  lback (if there 
1b2b0 69 73 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69  is one) or it wi
1b2b1 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c  ll.** return SQL
1b2b2 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20  ITE_BUSY..**.** 
1b2b3 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
1b2b4 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  rs, an error mes
1b2b5 73 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20  sage is written 
1b2b6 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
1b2b7 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  ed.** from sqlit
1b2b8 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  e3_malloc() and 
1b2b9 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61  p->zErrMsg is ma
1b2ba 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  de to point to t
1b2bb 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54  hat memory..** T
1b2bc 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  he error code is
1b2bd 20 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63   stored in p->rc
1b2be 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
1b2bf 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
1b2c0 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66  _ERROR..**.** If
1b2c1 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76   the callback ev
1b2c2 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  er returns non-z
1b2c3 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72  ero, then the pr
1b2c4 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69  ogram exits.** i
1b2c5 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65  mmediately.  The
1b2c6 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72  re will be no er
1b2c7 72 6f 72 20 6d 65 73 73 61 67 65 20 62 75 74 20  ror message but 
1b2c8 74 68 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20  the p->rc field 
1b2c9 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c  is.** set to SQL
1b2ca 49 54 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68  ITE_ABORT and th
1b2cb 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1b2cc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1b2cd 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d  ROR..**.** A mem
1b2ce 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
1b2cf 72 72 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72  rror causes p->r
1b2d0 63 20 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53  c to be set to S
1b2d1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20  QLITE_NOMEM and 
1b2d2 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
1b2d3 74 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  to return SQLITE
1b2d4 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74  _ERROR..**.** Ot
1b2d5 68 65 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73  her fatal errors
1b2d6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1b2d7 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  RROR..**.** Afte
1b2d8 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68  r this routine h
1b2d9 61 73 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c  as finished, sql
1b2da 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
1b2db 28 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20  () should be.** 
1b2dc 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70  used to clean up
1b2dd 20 74 68 65 20 6d 65 73 73 20 74 68 61 74 20 77   the mess that w
1b2de 61 73 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a  as left behind..
1b2df 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b2e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
1b2e1 65 45 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70  eExec(.  Vdbe *p
1b2e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b2e3 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
1b2e4 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20  */.){.  int pc; 
1b2e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b2e6 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61     /* The progra
1b2e7 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f  m counter */.  O
1b2e8 70 20 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20  p *pOp;         
1b2e9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
1b2ea 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a  rent operation *
1b2eb 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
1b2ec 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f  ITE_OK;        /
1b2ed 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
1b2ee 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  n */.  sqlite3 *
1b2ef 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20  db = p->db;     
1b2f0 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1b2f1 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69  e */.  u8 encodi
1b2f2 6e 67 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20  ng = ENC(db);   
1b2f3 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1b2f4 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  e encoding */.  
1b2f5 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20  Mem *pIn1 = 0;  
1b2f6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73             /* 1s
1b2f7 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20  t input operand 
1b2f8 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d  */.  Mem *pIn2 =
1b2f9 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1b2fa 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f 70 65  /* 2nd input ope
1b2fb 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  rand */.  Mem *p
1b2fc 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 20 20 20  In3 = 0;        
1b2fd 20 20 20 20 20 2f 2a 20 33 72 64 20 69 6e 70 75       /* 3rd inpu
1b2fe 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d  t operand */.  M
1b2ff 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20  em *pOut = 0;   
1b300 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
1b301 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  put operand */. 
1b302 20 75 38 20 6f 70 50 72 6f 70 65 72 74 79 3b 0a   u8 opProperty;.
1b303 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d    int iCompare =
1b304 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
1b305 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f  Result of last O
1b306 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 74  P_Compare operat
1b307 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50  ion */.  int *aP
1b308 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 20  ermute = 0;     
1b309 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 69      /* Permutati
1b30a 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f  on of columns fo
1b30b 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a  r OP_Compare */.
1b30c 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1b30d 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b  ILE.  u64 start;
1b30e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b30f 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f   /* CPU clock co
1b310 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66 20  unt at start of 
1b311 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  opcode */.  int 
1b312 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20 20  origPc;         
1b313 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61         /* Progra
1b314 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61  m counter at sta
1b315 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a  rt of opcode */.
1b316 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
1b317 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52  QLITE_OMIT_PROGR
1b318 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69  ESS_CALLBACK.  i
1b319 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20  nt nProgressOps 
1b31a 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63  = 0;      /* Opc
1b31b 6f 64 65 73 20 65 78 65 63 75 74 65 64 20 73 69  odes executed si
1b31c 6e 63 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  nce progress cal
1b31d 6c 62 61 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66  lback. */.#endif
1b31e 0a 0a 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79  ..  /* Temporary
1b31f 20 73 70 61 63 65 20 69 6e 74 6f 20 77 68 69 63   space into whic
1b320 68 20 74 6f 20 75 6e 70 61 63 6b 20 61 20 72 65  h to unpack a re
1b321 63 6f 72 64 2e 20 2a 2f 0a 20 20 63 68 61 72 20  cord. */.  char 
1b322 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28  aTempRec[ROUND8(
1b323 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52  sizeof(UnpackedR
1b324 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66  ecord)) + sizeof
1b325 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 0a 20  (Mem)*3 + 7];.. 
1b326 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
1b327 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
1b328 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33  N );  /* sqlite3
1b329 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65 73  _step() verifies
1b32a 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65 72   this */.  asser
1b32b 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  t( db->magic==SQ
1b32c 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
1b32d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1b32e 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28  MutexArrayEnter(
1b32f 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d  p);.  if( p->rc=
1b330 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
1b331 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70  .    /* This hap
1b332 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63  pens if a malloc
1b333 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c 6c  () inside a call
1b334 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
1b335 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 20  mn_text() or.   
1b336 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   ** sqlite3_colu
1b337 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c  mn_text16() fail
1b338 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f  ed.  */.    goto
1b339 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61   no_mem;.  }.  a
1b33a 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51  ssert( p->rc==SQ
1b33b 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63  LITE_OK || p->rc
1b33c 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b  ==SQLITE_BUSY );
1b33d 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54  .  p->rc = SQLIT
1b33e 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
1b33f 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b  p->explain==0 );
1b340 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74  .  p->pResultSet
1b341 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79   = 0;.  db->busy
1b342 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20  Handler.nBusy = 
1b343 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49  0;.  CHECK_FOR_I
1b344 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71 6c 69  NTERRUPT;.  sqli
1b345 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71  te3VdbeIOTraceSq
1b346 6c 28 70 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  l(p);.#ifdef SQL
1b347 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71 6c 69  ITE_DEBUG.  sqli
1b348 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
1b349 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 70 2d  lloc();.  if( p-
1b34a 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20 28 28  >pc==0 .   && ((
1b34b 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  p->db->flags & S
1b34c 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e  QLITE_VdbeListin
1b34d 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73 74 73  g) || fileExists
1b34e 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70 6c 61  (db, "vdbe_expla
1b34f 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20 20 69  in")).  ){.    i
1b350 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 66  nt i;.    printf
1b351 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20 4c  ("VDBE Program L
1b352 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20  isting:\n");.   
1b353 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
1b354 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72  tSql(p);.    for
1b355 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20  (i=0; i<p->nOp; 
1b356 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
1b357 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73  te3VdbePrintOp(s
1b358 74 64 6f 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f  tdout, i, &p->aO
1b359 70 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  p[i]);.    }.  }
1b35a 0a 20 20 69 66 28 20 66 69 6c 65 45 78 69 73 74  .  if( fileExist
1b35b 73 28 64 62 2c 20 22 76 64 62 65 5f 74 72 61 63  s(db, "vdbe_trac
1b35c 65 22 29 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72  e") ){.    p->tr
1b35d 61 63 65 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20  ace = stdout;.  
1b35e 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65  }.  sqlite3EndBe
1b35f 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65  nignMalloc();.#e
1b360 6e 64 69 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d  ndif.  for(pc=p-
1b361 3e 70 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  >pc; rc==SQLITE_
1b362 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61  OK; pc++){.    a
1b363 73 73 65 72 74 28 20 70 63 3e 3d 30 20 26 26 20  ssert( pc>=0 && 
1b364 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20  pc<p->nOp );.   
1b365 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1b366 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f  ailed ) goto no_
1b367 6d 65 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45  mem;.#ifdef VDBE
1b368 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69  _PROFILE.    ori
1b369 67 50 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74  gPc = pc;.    st
1b36a 61 72 74 20 3d 20 73 71 6c 69 74 65 33 48 77 74  art = sqlite3Hwt
1b36b 69 6d 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20  ime();.#endif.  
1b36c 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b    pOp = &p->aOp[
1b36d 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c  pc];..    /* Onl
1b36e 79 20 61 6c 6c 6f 77 20 74 72 61 63 69 6e 67 20  y allow tracing 
1b36f 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  if SQLITE_DEBUG 
1b370 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20  is defined..    
1b371 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1b372 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 70  _DEBUG.    if( p
1b373 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20  ->trace ){.     
1b374 20 69 66 28 20 70 63 3d 3d 30 20 29 7b 0a 20 20   if( pc==0 ){.  
1b375 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 56 44        printf("VD
1b376 42 45 20 45 78 65 63 75 74 69 6f 6e 20 54 72 61  BE Execution Tra
1b377 63 65 3a 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20  ce:\n");.       
1b378 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
1b379 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d  tSql(p);.      }
1b37a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1b37b 62 65 50 72 69 6e 74 4f 70 28 70 2d 3e 74 72 61  bePrintOp(p->tra
1b37c 63 65 2c 20 70 63 2c 20 70 4f 70 29 3b 0a 20 20  ce, pc, pOp);.  
1b37d 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 74    }.    if( p->t
1b37e 72 61 63 65 3d 3d 30 20 26 26 20 70 63 3d 3d 30  race==0 && pc==0
1b37f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1b380 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
1b381 6f 63 28 29 3b 0a 20 20 20 20 20 20 69 66 28 20  oc();.      if( 
1b382 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20 22  fileExists(db, "
1b383 76 64 62 65 5f 73 71 6c 74 72 61 63 65 22 29 20  vdbe_sqltrace") 
1b384 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1b385 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70  e3VdbePrintSql(p
1b386 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1b387 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
1b388 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 7d  nMalloc();.    }
1b389 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 0a 0a  .#endif.      ..
1b38a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20      /* Check to 
1b38b 73 65 65 20 69 66 20 77 65 20 6e 65 65 64 20 74  see if we need t
1b38c 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6e  o simulate an in
1b38d 74 65 72 72 75 70 74 2e 20 20 54 68 69 73 20 6f  terrupt.  This o
1b38e 6e 6c 79 20 68 61 70 70 65 6e 73 0a 20 20 20 20  nly happens.    
1b38f 2a 2a 20 69 66 20 77 65 20 68 61 76 65 20 61 20  ** if we have a 
1b390 73 70 65 63 69 61 6c 20 74 65 73 74 20 62 75 69  special test bui
1b391 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65  ld..    */.#ifde
1b392 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
1b393 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
1b394 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3e 30 20  terrupt_count>0 
1b395 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b396 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74  _interrupt_count
1b397 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71  --;.      if( sq
1b398 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f  lite3_interrupt_
1b399 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  count==0 ){.    
1b39a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65      sqlite3_inte
1b39b 72 72 75 70 74 28 64 62 29 3b 0a 20 20 20 20 20  rrupt(db);.     
1b39c 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
1b39d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b39e 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41  OMIT_PROGRESS_CA
1b39f 4c 4c 42 41 43 4b 0a 20 20 20 20 2f 2a 20 43 61  LLBACK.    /* Ca
1b3a0 6c 6c 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  ll the progress 
1b3a1 63 61 6c 6c 62 61 63 6b 20 69 66 20 69 74 20 69  callback if it i
1b3a2 73 20 63 6f 6e 66 69 67 75 72 65 64 20 61 6e 64  s configured and
1b3a3 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6e 75   the required nu
1b3a4 6d 62 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 56  mber.    ** of V
1b3a5 44 42 45 20 6f 70 73 20 68 61 76 65 20 62 65 65  DBE ops have bee
1b3a6 6e 20 65 78 65 63 75 74 65 64 20 28 65 69 74 68  n executed (eith
1b3a7 65 72 20 73 69 6e 63 65 20 74 68 69 73 20 69 6e  er since this in
1b3a8 76 6f 63 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20  vocation of.    
1b3a9 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  ** sqlite3VdbeEx
1b3aa 65 63 28 29 20 6f 72 20 73 69 6e 63 65 20 6c 61  ec() or since la
1b3ab 73 74 20 74 69 6d 65 20 74 68 65 20 70 72 6f 67  st time the prog
1b3ac 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 77 61  ress callback wa
1b3ad 73 20 63 61 6c 6c 65 64 29 2e 0a 20 20 20 20 2a  s called)..    *
1b3ae 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73  * If the progres
1b3af 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  s callback retur
1b3b0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 65 78 69  ns non-zero, exi
1b3b1 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  t the virtual ma
1b3b2 63 68 69 6e 65 20 77 69 74 68 0a 20 20 20 20 2a  chine with.    *
1b3b3 2a 20 61 20 72 65 74 75 72 6e 20 63 6f 64 65 20  * a return code 
1b3b4 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0a 20 20  SQLITE_ABORT..  
1b3b5 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d    */.    if( db-
1b3b6 3e 78 50 72 6f 67 72 65 73 73 20 29 7b 0a 20 20  >xProgress ){.  
1b3b7 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 50 72 6f      if( db->nPro
1b3b8 67 72 65 73 73 4f 70 73 3d 3d 6e 50 72 6f 67 72  gressOps==nProgr
1b3b9 65 73 73 4f 70 73 20 29 7b 0a 20 20 20 20 20 20  essOps ){.      
1b3ba 20 20 69 6e 74 20 70 72 63 3b 0a 20 20 20 20 20    int prc;.     
1b3bb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1b3bc 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1b3bd 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1b3be 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20  misuse;.        
1b3bf 70 72 63 20 3d 64 62 2d 3e 78 50 72 6f 67 72 65  prc =db->xProgre
1b3c0 73 73 28 64 62 2d 3e 70 50 72 6f 67 72 65 73 73  ss(db->pProgress
1b3c1 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Arg);.        if
1b3c2 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1b3c3 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  n(db) ) goto abo
1b3c4 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1b3c5 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  ;.        if( pr
1b3c6 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  c!=0 ){.        
1b3c7 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e    rc = SQLITE_IN
1b3c8 54 45 52 52 55 50 54 3b 0a 20 20 20 20 20 20 20  TERRUPT;.       
1b3c9 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72     goto vdbe_err
1b3ca 6f 72 5f 68 61 6c 74 3b 0a 20 20 20 20 20 20 20  or_halt;.       
1b3cb 20 7d 0a 20 20 20 20 20 20 20 20 6e 50 72 6f 67   }.        nProg
1b3cc 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20  ressOps = 0;.   
1b3cd 20 20 20 7d 0a 20 20 20 20 20 20 6e 50 72 6f 67     }.      nProg
1b3ce 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20 20 20 7d  ressOps++;.    }
1b3cf 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
1b3d0 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20  Do common setup 
1b3d1 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61  processing for a
1b3d2 6e 79 20 6f 70 63 6f 64 65 20 74 68 61 74 20 69  ny opcode that i
1b3d3 73 20 6d 61 72 6b 65 64 0a 20 20 20 20 2a 2a 20  s marked.    ** 
1b3d4 77 69 74 68 20 74 68 65 20 22 6f 75 74 32 2d 70  with the "out2-p
1b3d5 72 65 72 65 6c 65 61 73 65 22 20 74 61 67 2e 20  rerelease" tag. 
1b3d6 20 53 75 63 68 20 6f 70 63 6f 64 65 73 20 68 61   Such opcodes ha
1b3d7 76 65 20 61 20 73 69 6e 67 6c 65 0a 20 20 20 20  ve a single.    
1b3d8 2a 2a 20 6f 75 74 70 75 74 20 77 68 69 63 68 20  ** output which 
1b3d9 69 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  is specified by 
1b3da 74 68 65 20 50 32 20 70 61 72 61 6d 65 74 65 72  the P2 parameter
1b3db 2e 20 20 54 68 65 20 50 32 20 72 65 67 69 73 74  .  The P2 regist
1b3dc 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 69 6e 69  er.    ** is ini
1b3dd 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 4e 55  tialized to a NU
1b3de 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6f  LL..    */.    o
1b3df 70 50 72 6f 70 65 72 74 79 20 3d 20 6f 70 63 6f  pProperty = opco
1b3e0 64 65 50 72 6f 70 65 72 74 79 5b 70 4f 70 2d 3e  deProperty[pOp->
1b3e1 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28  opcode];.    if(
1b3e2 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f   (opProperty & O
1b3e3 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c  PFLG_OUT2_PREREL
1b3e4 45 41 53 45 29 21 3d 30 20 29 7b 0a 20 20 20 20  EASE)!=0 ){.    
1b3e5 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1b3e6 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  2>0 );.      ass
1b3e7 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d  ert( pOp->p2<=p-
1b3e8 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70  >nMem );.      p
1b3e9 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Out = &p->aMem[p
1b3ea 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 73  Op->p2];.      s
1b3eb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1b3ec 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75  easeExternal(pOu
1b3ed 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  t);.      pOut->
1b3ee 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1b3ef 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6e 20  ;.      pOut->n 
1b3f0 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 20  = 0;.    }else. 
1b3f1 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f  .    /* Do commo
1b3f2 6e 20 73 65 74 75 70 20 66 6f 72 20 6f 70 63 6f  n setup for opco
1b3f3 64 65 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20  des marked with 
1b3f4 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
1b3f5 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 6f 6d 62  wing.    ** comb
1b3f6 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70 72 6f 70  inations of prop
1b3f7 65 72 74 69 65 73 2e 0a 20 20 20 20 2a 2a 0a 20  erties..    **. 
1b3f8 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
1b3f9 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  in1.    **      
1b3fa 20 20 20 20 20 69 6e 31 20 69 6e 32 0a 20 20 20       in1 in2.   
1b3fb 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e   **           in
1b3fc 31 20 69 6e 32 20 6f 75 74 33 0a 20 20 20 20 2a  1 in2 out3.    *
1b3fd 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 20  *           in1 
1b3fe 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  in3.    **.    *
1b3ff 2a 20 56 61 72 69 61 62 6c 65 73 20 70 49 6e 31  * Variables pIn1
1b400 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70 49 6e 33  , pIn2, and pIn3
1b401 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 6f 69   are made to poi
1b402 6e 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74  nt to appropriat
1b403 65 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65  e.    ** registe
1b404 72 73 20 66 6f 72 20 69 6e 70 75 74 73 2e 20 20  rs for inputs.  
1b405 56 61 72 69 61 62 6c 65 20 70 4f 75 74 20 70 6f  Variable pOut po
1b406 69 6e 74 73 20 74 6f 20 74 68 65 20 6f 75 74 70  ints to the outp
1b407 75 74 20 72 65 67 69 73 74 65 72 2e 0a 20 20 20  ut register..   
1b408 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6f 70 50   */.    if( (opP
1b409 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f  roperty & OPFLG_
1b40a 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  IN1)!=0 ){.     
1b40b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1b40c 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  >0 );.      asse
1b40d 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e  rt( pOp->p1<=p->
1b40e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49  nMem );.      pI
1b40f 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  n1 = &p->aMem[pO
1b410 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 20 52 45  p->p1];.      RE
1b411 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1b412 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 20  ->p1, pIn1);.   
1b413 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72     if( (opProper
1b414 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21  ty & OPFLG_IN2)!
1b415 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  =0 ){.        as
1b416 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1b417 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
1b418 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e  t( pOp->p2<=p->n
1b419 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70  Mem );.        p
1b41a 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In2 = &p->aMem[p
1b41b 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20  Op->p2];.       
1b41c 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1b41d 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a  pOp->p2, pIn2);.
1b41e 20 20 20 20 20 20 20 20 69 66 28 20 28 6f 70 50          if( (opP
1b41f 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f  roperty & OPFLG_
1b420 4f 55 54 33 29 21 3d 30 20 29 7b 0a 20 20 20 20  OUT3)!=0 ){.    
1b421 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1b422 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20  p->p3>0 );.     
1b423 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1b424 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
1b425 0a 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 20  .          pOut 
1b426 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1b427 70 33 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  p3];.        }. 
1b428 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28       }else if( (
1b429 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
1b42a 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20  LG_IN3)!=0 ){.  
1b42b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1b42c 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20  p->p3>0 );.     
1b42d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1b42e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p3<=p->nMem );. 
1b42f 20 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 70         pIn3 = &p
1b430 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1b431 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 54 45  .        REGISTE
1b432 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c  R_TRACE(pOp->p3,
1b433 20 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d 0a   pIn3);.      }.
1b434 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f      }else if( (o
1b435 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
1b436 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 20  G_IN2)!=0 ){.   
1b437 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1b438 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73  p2>0 );.      as
1b439 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70  sert( pOp->p2<=p
1b43a 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20  ->nMem );.      
1b43b 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn2 = &p->aMem[
1b43c 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20  pOp->p2];.      
1b43d 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1b43e 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a 20  Op->p2, pIn2);. 
1b43f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70     }else if( (op
1b440 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
1b441 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20  _IN3)!=0 ){.    
1b442 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1b443 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  3>0 );.      ass
1b444 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  ert( pOp->p3<=p-
1b445 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70  >nMem );.      p
1b446 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In3 = &p->aMem[p
1b447 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 52  Op->p3];.      R
1b448 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1b449 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20  p->p3, pIn3);.  
1b44a 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28    }..    switch(
1b44b 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a   pOp->opcode ){.
1b44c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1b44d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b44e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b44f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1b451 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20  ** What follows 
1b452 69 73 20 61 20 6d 61 73 73 69 76 65 20 73 77 69  is a massive swi
1b453 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 68  tch statement wh
1b454 65 72 65 20 65 61 63 68 20 63 61 73 65 20 69 6d  ere each case im
1b455 70 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65  plements a.** se
1b456 70 61 72 61 74 65 20 69 6e 73 74 72 75 63 74 69  parate instructi
1b457 6f 6e 20 69 6e 20 74 68 65 20 76 69 72 74 75 61  on in the virtua
1b458 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 66 20 77  l machine.  If w
1b459 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75  e follow the usu
1b45a 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f  al.** indentatio
1b45b 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65  n conventions, e
1b45c 61 63 68 20 63 61 73 65 20 73 68 6f 75 6c 64 20  ach case should 
1b45d 62 65 20 69 6e 64 65 6e 74 65 64 20 62 79 20 36  be indented by 6
1b45e 20 73 70 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a   spaces.  But.**
1b45f 20 74 68 61 74 20 69 73 20 61 20 6c 6f 74 20 6f   that is a lot o
1b460 66 20 77 61 73 74 65 64 20 73 70 61 63 65 20 6f  f wasted space o
1b461 6e 20 74 68 65 20 6c 65 66 74 20 6d 61 72 67 69  n the left margi
1b462 6e 2e 20 20 53 6f 20 74 68 65 20 63 6f 64 65 20  n.  So the code 
1b463 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77  within.** the sw
1b464 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77  itch statement w
1b465 69 6c 6c 20 62 72 65 61 6b 20 77 69 74 68 20 63  ill break with c
1b466 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65  onvention and be
1b467 20 66 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f   flush-left. Ano
1b468 74 68 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d  ther.** big comm
1b469 65 6e 74 20 28 73 69 6d 69 6c 61 72 20 74 6f 20  ent (similar to 
1b46a 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d  this one) will m
1b46b 61 72 6b 20 74 68 65 20 70 6f 69 6e 74 20 69 6e  ark the point in
1b46c 20 74 68 65 20 63 6f 64 65 20 77 68 65 72 65 0a   the code where.
1b46d 2a 2a 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e  ** we transition
1b46e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20   back to normal 
1b46f 69 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a  indentation..**.
1b470 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e  ** The formattin
1b471 67 20 6f 66 20 65 61 63 68 20 63 61 73 65 20 69  g of each case i
1b472 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68  s important.  Th
1b473 65 20 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20 53  e makefile for S
1b474 51 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74  QLite.** generat
1b475 65 73 20 74 77 6f 20 43 20 66 69 6c 65 73 20 22  es two C files "
1b476 6f 70 63 6f 64 65 73 2e 68 22 20 61 6e 64 20 22  opcodes.h" and "
1b477 6f 70 63 6f 64 65 73 2e 63 22 20 62 79 20 73 63  opcodes.c" by sc
1b478 61 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66  anning this.** f
1b479 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  ile looking for 
1b47a 6c 69 6e 65 73 20 74 68 61 74 20 62 65 67 69 6e  lines that begin
1b47b 20 77 69 74 68 20 22 63 61 73 65 20 4f 50 5f 22   with "case OP_"
1b47c 2e 20 20 54 68 65 20 6f 70 63 6f 64 65 73 2e 68  .  The opcodes.h
1b47d 20 66 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62   files.** will b
1b47e 65 20 66 69 6c 6c 65 64 20 77 69 74 68 20 23 64  e filled with #d
1b47f 65 66 69 6e 65 73 20 74 68 61 74 20 67 69 76 65  efines that give
1b480 20 75 6e 69 71 75 65 20 69 6e 74 65 67 65 72 20   unique integer 
1b481 76 61 6c 75 65 73 20 74 6f 20 65 61 63 68 0a 2a  values to each.*
1b482 2a 20 6f 70 63 6f 64 65 20 61 6e 64 20 74 68 65  * opcode and the
1b483 20 6f 70 63 6f 64 65 73 2e 63 20 66 69 6c 65 20   opcodes.c file 
1b484 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 61  is filled with a
1b485 6e 20 61 72 72 61 79 20 6f 66 20 73 74 72 69 6e  n array of strin
1b486 67 73 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68  gs where.** each
1b487 20 73 74 72 69 6e 67 20 69 73 20 74 68 65 20 73   string is the s
1b488 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72  ymbolic name for
1b489 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1b48a 6e 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20 74  ng opcode.  If t
1b48b 68 65 0a 2a 2a 20 63 61 73 65 20 73 74 61 74 65  he.** case state
1b48c 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64  ment is followed
1b48d 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66   by a comment of
1b48e 20 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73 61   the form "/# sa
1b48f 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a  me as ... #/".**
1b490 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73   that comment is
1b491 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
1b492 6e 65 20 74 68 65 20 70 61 72 74 69 63 75 6c 61  ne the particula
1b493 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f  r value of the o
1b494 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  pcode..**.** Oth
1b495 65 72 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 74  er keywords in t
1b496 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20  he comment that 
1b497 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20 63 61 73  follows each cas
1b498 65 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a  e are used to.**
1b499 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 4f   construct the O
1b49a 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52  PFLG_INITIALIZER
1b49b 20 76 61 6c 75 65 20 74 68 61 74 20 69 6e 69 74   value that init
1b49c 69 61 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50 72  ializes opcodePr
1b49d 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79  operty[]..** Key
1b49e 77 6f 72 64 73 20 69 6e 63 6c 75 64 65 3a 20 69  words include: i
1b49f 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75  n1, in2, in3, ou
1b4a0 74 32 5f 70 72 65 72 65 6c 65 61 73 65 2c 20 6f  t2_prerelease, o
1b4a1 75 74 32 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a  ut2, out3.  See.
1b4a2 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68  ** the mkopcodeh
1b4a3 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20  .awk script for 
1b4a4 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
1b4a5 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f  mation..**.** Do
1b4a6 63 75 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75  cumentation abou
1b4a7 74 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 69  t VDBE opcodes i
1b4a8 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 73  s generated by s
1b4a9 63 61 6e 6e 69 6e 67 20 74 68 69 73 20 66 69 6c  canning this fil
1b4aa 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f  e.** for lines o
1b4ab 66 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 22  f that contain "
1b4ac 4f 70 63 6f 64 65 3a 22 2e 20 20 54 68 61 74 20  Opcode:".  That 
1b4ad 6c 69 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75 62  line and all sub
1b4ae 73 65 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65  sequent.** comme
1b4af 6e 74 20 6c 69 6e 65 73 20 61 72 65 20 75 73 65  nt lines are use
1b4b0 64 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61 74  d in the generat
1b4b1 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f 64  ion of the opcod
1b4b2 65 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61  e.html documenta
1b4b3 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a  tion.** file..**
1b4b4 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a  .** SUMMARY:.**.
1b4b5 2a 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69 6e  **     Formattin
1b4b6 67 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  g is important t
1b4b7 6f 20 73 63 72 69 70 74 73 20 74 68 61 74 20 73  o scripts that s
1b4b8 63 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a  can this file..*
1b4b9 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 76  *     Do not dev
1b4ba 69 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66 6f  iate from the fo
1b4bb 72 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20 63  rmatting style c
1b4bc 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e  urrently in use.
1b4bd 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1b4be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4c2 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  */../* Opcode:  
1b4c3 47 6f 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a  Goto * P2 * * *.
1b4c4 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69  **.** An uncondi
1b4c5 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61  tional jump to a
1b4c6 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68  ddress P2..** Th
1b4c7 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1b4c8 6f 6e 20 65 78 65 63 75 74 65 64 20 77 69 6c 6c  on executed will
1b4c9 20 62 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20   be .** the one 
1b4ca 61 74 20 69 6e 64 65 78 20 50 32 20 66 72 6f 6d  at index P2 from
1b4cb 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
1b4cc 66 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d  f.** the program
1b4cd 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74  ..*/.case OP_Got
1b4ce 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  o: {            
1b4cf 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48   /* jump */.  CH
1b4d0 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50  ECK_FOR_INTERRUP
1b4d1 54 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  T;.  pc = pOp->p
1b4d2 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a  2 - 1;.  break;.
1b4d3 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47  }../* Opcode:  G
1b4d4 6f 73 75 62 20 50 31 20 50 32 20 2a 20 2a 20 2a  osub P1 P2 * * *
1b4d5 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  .**.** Write the
1b4d6 20 63 75 72 72 65 6e 74 20 61 64 64 72 65 73 73   current address
1b4d7 20 6f 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   onto register P
1b4d8 31 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75  1.** and then ju
1b4d9 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 32  mp to address P2
1b4da 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73  ..*/.case OP_Gos
1b4db 75 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ub: {           
1b4dc 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61 73   /* jump */.  as
1b4dd 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20  sert( pOp->p1>0 
1b4de 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1b4df 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p1<=p->nMem );
1b4e0 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d  .  pIn1 = &p->aM
1b4e1 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  em[pOp->p1];.  a
1b4e2 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c  ssert( (pIn1->fl
1b4e3 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d  ags & MEM_Dyn)==
1b4e4 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61  0 );.  pIn1->fla
1b4e5 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1b4e6 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a  pIn1->u.i = pc;.
1b4e7 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1b4e8 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b  (pOp->p1, pIn1);
1b4e9 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20  .  pc = pOp->p2 
1b4ea 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  - 1;.  break;.}.
1b4eb 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65 74  ./* Opcode:  Ret
1b4ec 75 72 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  urn P1 * * * *.*
1b4ed 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65  *.** Jump to the
1b4ee 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1b4ef 6e 20 61 66 74 65 72 20 74 68 65 20 61 64 64 72  n after the addr
1b4f0 65 73 73 20 69 6e 20 72 65 67 69 73 74 65 72 20  ess in register 
1b4f1 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  P1..*/.case OP_R
1b4f2 65 74 75 72 6e 3a 20 7b 20 20 20 20 20 20 20 20  eturn: {        
1b4f3 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61     /* in1 */.  a
1b4f4 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61  ssert( pIn1->fla
1b4f5 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a  gs & MEM_Int );.
1b4f6 20 20 70 63 20 3d 20 28 69 6e 74 29 70 49 6e 31    pc = (int)pIn1
1b4f7 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 0a  ->u.i;.  break;.
1b4f8 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 59  }../* Opcode:  Y
1b4f9 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  ield P1 * * * *.
1b4fa 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 65 20 70  **.** Swap the p
1b4fb 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 77  rogram counter w
1b4fc 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 69 6e  ith the value in
1b4fd 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f   register P1..*/
1b4fe 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a 20  .case OP_Yield: 
1b4ff 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1b500 69 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44  in1 */.  int pcD
1b501 65 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 28  est;.  assert( (
1b502 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1b503 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70  M_Dyn)==0 );.  p
1b504 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  In1->flags = MEM
1b505 5f 49 6e 74 3b 0a 20 20 70 63 44 65 73 74 20 3d  _Int;.  pcDest =
1b506 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b   (int)pIn1->u.i;
1b507 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70  .  pIn1->u.i = p
1b508 63 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  c;.  REGISTER_TR
1b509 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e  ACE(pOp->p1, pIn
1b50a 31 29 3b 0a 20 20 70 63 20 3d 20 70 63 44 65 73  1);.  pc = pcDes
1b50b 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  t;.  break;.}../
1b50c 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 49  * Opcode:  HaltI
1b50d 66 4e 75 6c 6c 20 20 50 31 20 50 32 20 50 33 20  fNull  P1 P2 P3 
1b50e 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b  P4 *.**.** Check
1b50f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1b510 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 69  gister P3.  If i
1b511 73 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48  s is NULL then H
1b512 61 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72  alt using.** par
1b513 61 6d 65 74 65 72 20 50 31 2c 20 50 32 2c 20 61  ameter P1, P2, a
1b514 6e 64 20 50 34 20 61 73 20 69 66 20 74 68 69 73  nd P4 as if this
1b515 20 77 65 72 65 20 61 20 48 61 6c 74 20 69 6e 73   were a Halt ins
1b516 74 72 75 63 74 69 6f 6e 2e 20 20 49 66 20 74 68  truction.  If th
1b517 65 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 72 65  e.** value in re
1b518 67 69 73 74 65 72 20 50 33 20 69 73 20 6e 6f 74  gister P3 is not
1b519 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73   NULL, then this
1b51a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
1b51b 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  -op..*/.case OP_
1b51c 48 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20  HaltIfNull: {   
1b51d 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 69     /* in3 */.  i
1b51e 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20  f( (pIn3->flags 
1b51f 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29  & MEM_Null)==0 )
1b520 20 62 72 65 61 6b 3b 0a 20 20 2f 2a 20 46 61 6c   break;.  /* Fal
1b521 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f  l through into O
1b522 50 5f 48 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f 2a 20  P_Halt */.}../* 
1b523 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 20 50 31  Opcode:  Halt P1
1b524 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a   P2 * P4 *.**.**
1b525 20 45 78 69 74 20 69 6d 6d 65 64 69 61 74 65 6c   Exit immediatel
1b526 79 2e 20 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72  y.  All open cur
1b527 73 6f 72 73 2c 20 65 74 63 20 61 72 65 20 63 6c  sors, etc are cl
1b528 6f 73 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69  osed.** automati
1b529 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20  cally..**.** P1 
1b52a 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  is the result co
1b52b 64 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  de returned by s
1b52c 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2c 20 73  qlite3_exec(), s
1b52d 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c 0a  qlite3_reset(),.
1b52e 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69  ** or sqlite3_fi
1b52f 6e 61 6c 69 7a 65 28 29 2e 20 20 46 6f 72 20 61  nalize().  For a
1b530 20 6e 6f 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68   normal halt, th
1b531 69 73 20 73 68 6f 75 6c 64 20 62 65 20 53 51 4c  is should be SQL
1b532 49 54 45 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46  ITE_OK (0)..** F
1b533 6f 72 20 65 72 72 6f 72 73 2c 20 69 74 20 63 61  or errors, it ca
1b534 6e 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 20  n be some other 
1b535 76 61 6c 75 65 2e 20 20 49 66 20 50 31 21 3d 30  value.  If P1!=0
1b536 20 74 68 65 6e 20 50 32 20 77 69 6c 6c 20 64 65   then P2 will de
1b537 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68  termine.** wheth
1b538 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c  er or not to rol
1b539 6c 62 61 63 6b 20 74 68 65 20 63 75 72 72 65 6e  lback the curren
1b53a 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  t transaction.  
1b53b 44 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a  Do not rollback.
1b53c 2a 2a 20 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69  ** if P2==OE_Fai
1b53d 6c 2e 20 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61  l. Do the rollba
1b53e 63 6b 20 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c  ck if P2==OE_Rol
1b53f 6c 62 61 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f  lback.  If P2==O
1b540 45 5f 41 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e  E_Abort,.** then
1b541 20 62 61 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68   back out all ch
1b542 61 6e 67 65 73 20 74 68 61 74 20 68 61 76 65 20  anges that have 
1b543 6f 63 63 75 72 72 65 64 20 64 75 72 69 6e 67 20  occurred during 
1b544 74 68 69 73 20 65 78 65 63 75 74 69 6f 6e 20 6f  this execution o
1b545 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62  f the.** VDBE, b
1b546 75 74 20 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61  ut do not rollba
1b547 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ck the transacti
1b548 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34  on. .**.** If P4
1b549 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65   is not null the
1b54a 6e 20 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72  n it is an error
1b54b 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e   message string.
1b54c 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  .**.** There is 
1b54d 61 6e 20 69 6d 70 6c 69 65 64 20 22 48 61 6c 74  an implied "Halt
1b54e 20 30 20 30 20 30 22 20 69 6e 73 74 72 75 63 74   0 0 0" instruct
1b54f 69 6f 6e 20 69 6e 73 65 72 74 65 64 20 61 74 20  ion inserted at 
1b550 74 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66 0a  the very end of.
1b551 2a 2a 20 65 76 65 72 79 20 70 72 6f 67 72 61 6d  ** every program
1b552 2e 20 20 53 6f 20 61 20 6a 75 6d 70 20 70 61 73  .  So a jump pas
1b553 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72  t the last instr
1b554 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 72  uction of the pr
1b555 6f 67 72 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20  ogram.** is the 
1b556 73 61 6d 65 20 61 73 20 65 78 65 63 75 74 69 6e  same as executin
1b557 67 20 48 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20  g Halt..*/.case 
1b558 4f 50 5f 48 61 6c 74 3a 20 7b 0a 20 20 70 2d 3e  OP_Halt: {.  p->
1b559 72 63 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  rc = pOp->p1;.  
1b55a 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 70 2d  p->pc = pc;.  p-
1b55b 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 70  >errorAction = p
1b55c 4f 70 2d 3e 70 32 3b 0a 20 20 69 66 28 20 70 4f  Op->p2;.  if( pO
1b55d 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73  p->p4.z ){.    s
1b55e 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1b55f 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1b560 20 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a   "%s", pOp->p4.z
1b561 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71  );.  }.  rc = sq
1b562 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29  lite3VdbeHalt(p)
1b563 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
1b564 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72  SQLITE_BUSY || r
1b565 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
1b566 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1b567 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e  _BUSY ){.    p->
1b568 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45  rc = rc = SQLITE
1b569 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _BUSY;.  }else{.
1b56a 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f      rc = p->rc ?
1b56b 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20   SQLITE_ERROR : 
1b56c 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
1b56d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74  .  goto vdbe_ret
1b56e 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  urn;.}../* Opcod
1b56f 65 3a 20 49 6e 74 65 67 65 72 20 50 31 20 50 32  e: Integer P1 P2
1b570 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65   * * *.**.** The
1b571 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
1b572 76 61 6c 75 65 20 50 31 20 69 73 20 77 72 69 74  value P1 is writ
1b573 74 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ten into registe
1b574 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P2..*/.case OP
1b575 5f 49 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20  _Integer: {     
1b576 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1b577 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74  elease */.  pOut
1b578 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1b579 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  t;.  pOut->u.i =
1b57a 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61   pOp->p1;.  brea
1b57b 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1b57c 20 49 6e 74 36 34 20 2a 20 50 32 20 2a 20 50 34   Int64 * P2 * P4
1b57d 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61   *.**.** P4 is a
1b57e 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34   pointer to a 64
1b57f 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c  -bit integer val
1b580 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61  ue..** Write tha
1b581 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67  t value into reg
1b582 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73  ister P2..*/.cas
1b583 65 20 4f 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20  e OP_Int64: {   
1b584 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1b585 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1b586 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
1b587 70 49 36 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75  pI64!=0 );.  pOu
1b588 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  t->flags = MEM_I
1b589 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20  nt;.  pOut->u.i 
1b58a 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b  = *pOp->p4.pI64;
1b58b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1b58c 4f 70 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50  Opcode: Real * P
1b58d 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50  2 * P4 *.**.** P
1b58e 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
1b58f 6f 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74  o a 64-bit float
1b590 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e  ing point value.
1b591 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76  .** Write that v
1b592 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74  alue into regist
1b593 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P2..*/.case O
1b594 50 5f 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20  P_Real: {       
1b595 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1b596 54 4b 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70  TK_FLOAT, out2-p
1b597 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70  rerelease */.  p
1b598 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
1b599 5f 52 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28  _Real;.  assert(
1b59a 20 21 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a   !sqlite3IsNaN(*
1b59b 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29  pOp->p4.pReal) )
1b59c 3b 0a 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70  ;.  pOut->r = *p
1b59d 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20  Op->p4.pReal;.  
1b59e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1b59f 6f 64 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50  ode: String8 * P
1b5a0 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50  2 * P4 *.**.** P
1b5a1 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75  4 points to a nu
1b5a2 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  l terminated UTF
1b5a3 2d 38 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20  -8 string. This 
1b5a4 6f 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66  opcode is transf
1b5a5 6f 72 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61  ormed .** into a
1b5a6 6e 20 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f  n OP_String befo
1b5a7 72 65 20 69 74 20 69 73 20 65 78 65 63 75 74 65  re it is execute
1b5a8 64 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  d for the first 
1b5a9 74 69 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  time..*/.case OP
1b5aa 5f 53 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20  _String8: {     
1b5ab 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1b5ac 4b 5f 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70  K_STRING, out2-p
1b5ad 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61  rerelease */.  a
1b5ae 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a  ssert( pOp->p4.z
1b5af 21 3d 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70  !=0 );.  pOp->op
1b5b0 63 6f 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67  code = OP_String
1b5b1 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71  ;.  pOp->p1 = sq
1b5b2 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f  lite3Strlen30(pO
1b5b3 70 2d 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64  p->p4.z);..#ifnd
1b5b4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
1b5b5 54 46 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64  TF16.  if( encod
1b5b6 69 6e 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38  ing!=SQLITE_UTF8
1b5b7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1b5b8 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75  dbeMemSetStr(pOu
1b5b9 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 2d 31  t, pOp->p4.z, -1
1b5ba 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
1b5bb 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
1b5bc 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
1b5bd 21 3d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  !=sqlite3VdbeCha
1b5be 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74  ngeEncoding(pOut
1b5bf 2c 20 65 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f  , encoding) ) go
1b5c0 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69  to no_mem;.    i
1b5c1 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
1b5c2 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
1b5c3 57 72 69 74 65 61 62 6c 65 28 70 4f 75 74 29 20  Writeable(pOut) 
1b5c4 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
1b5c5 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63     pOut->zMalloc
1b5c6 20 3d 20 30 3b 0a 20 20 20 20 70 4f 75 74 2d 3e   = 0;.    pOut->
1b5c7 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 61  flags |= MEM_Sta
1b5c8 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66  tic;.    pOut->f
1b5c9 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e  lags &= ~MEM_Dyn
1b5ca 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  ;.    if( pOp->p
1b5cb 34 74 79 70 65 3d 3d 50 34 5f 44 59 4e 41 4d 49  4type==P4_DYNAMI
1b5cc 43 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  C ){.      sqlit
1b5cd 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70  e3DbFree(db, pOp
1b5ce 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 20  ->p4.z);.    }. 
1b5cf 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d     pOp->p4type =
1b5d0 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 20   P4_DYNAMIC;.   
1b5d1 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f 75   pOp->p4.z = pOu
1b5d2 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  t->z;.    pOp->p
1b5d3 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20 20  1 = pOut->n;.   
1b5d4 20 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d   if( pOp->p1>db-
1b5d5 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1b5d6 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
1b5d7 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62        goto too_b
1b5d8 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 55 50  ig;.    }.    UP
1b5d9 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1b5da 45 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65  E(pOut);.    bre
1b5db 61 6b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ak;.  }.#endif. 
1b5dc 20 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d   if( pOp->p1>db-
1b5dd 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1b5de 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
1b5df 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
1b5e0 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20  ;.  }.  /* Fall 
1b5e1 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
1b5e2 65 78 74 20 63 61 73 65 2c 20 4f 50 5f 53 74 72  ext case, OP_Str
1b5e3 69 6e 67 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f  ing */.}.  ./* O
1b5e4 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 20 50 31  pcode: String P1
1b5e5 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a   P2 * P4 *.**.**
1b5e6 20 54 68 65 20 73 74 72 69 6e 67 20 76 61 6c 75   The string valu
1b5e7 65 20 50 34 20 6f 66 20 6c 65 6e 67 74 68 20 50  e P4 of length P
1b5e8 31 20 28 62 79 74 65 73 29 20 69 73 20 73 74 6f  1 (bytes) is sto
1b5e9 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1b5ea 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  P2..*/.case OP_S
1b5eb 74 72 69 6e 67 3a 20 7b 20 20 20 20 20 20 20 20  tring: {        
1b5ec 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1b5ed 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74  ease */.  assert
1b5ee 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29  ( pOp->p4.z!=0 )
1b5ef 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20  ;.  pOut->flags 
1b5f0 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74  = MEM_Str|MEM_St
1b5f1 61 74 69 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20  atic|MEM_Term;. 
1b5f2 20 70 4f 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e   pOut->z = pOp->
1b5f3 70 34 2e 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20  p4.z;.  pOut->n 
1b5f4 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75  = pOp->p1;.  pOu
1b5f5 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e  t->enc = encodin
1b5f6 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  g;.  UPDATE_MAX_
1b5f7 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1b5f8 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1b5f9 70 63 6f 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32  pcode: Null * P2
1b5fa 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69   * * *.**.** Wri
1b5fb 74 65 20 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72  te a NULL into r
1b5fc 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63  egister P2..*/.c
1b5fd 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20  ase OP_Null: {  
1b5fe 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1b5ff 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20  -prerelease */. 
1b600 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f   break;.}.../* O
1b601 70 63 6f 64 65 3a 20 42 6c 6f 62 20 50 31 20 50  pcode: Blob P1 P
1b602 32 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20  2 * P4.**.** P4 
1b603 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 6c 6f 62  points to a blob
1b604 20 6f 66 20 64 61 74 61 20 50 31 20 62 79 74 65   of data P1 byte
1b605 73 20 6c 6f 6e 67 2e 20 20 53 74 6f 72 65 20 74  s long.  Store t
1b606 68 69 73 0a 2a 2a 20 62 6c 6f 62 20 69 6e 20 72  his.** blob in r
1b607 65 67 69 73 74 65 72 20 50 32 2e 20 54 68 69 73  egister P2. This
1b608 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
1b609 6e 6f 74 20 63 6f 64 65 64 20 64 69 72 65 63 74  not coded direct
1b60a 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 63 6f 6d  ly.** by the com
1b60b 70 69 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20  piler. Instead, 
1b60c 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 6c 61 79  the compiler lay
1b60d 65 72 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20  er specifies.** 
1b60e 61 6e 20 4f 50 5f 48 65 78 42 6c 6f 62 20 6f 70  an OP_HexBlob op
1b60f 63 6f 64 65 2c 20 77 69 74 68 20 74 68 65 20 68  code, with the h
1b610 65 78 20 73 74 72 69 6e 67 20 72 65 70 72 65 73  ex string repres
1b611 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74  entation of.** t
1b612 68 65 20 62 6c 6f 62 20 61 73 20 50 34 2e 20 54  he blob as P4. T
1b613 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72  his opcode is tr
1b614 61 6e 73 66 6f 72 6d 65 64 20 74 6f 20 61 6e 20  ansformed to an 
1b615 4f 50 5f 42 6c 6f 62 0a 2a 2a 20 74 68 65 20 66  OP_Blob.** the f
1b616 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 20  irst time it is 
1b617 65 78 65 63 75 74 65 64 2e 0a 2a 2f 0a 63 61 73  executed..*/.cas
1b618 65 20 4f 50 5f 42 6c 6f 62 3a 20 7b 20 20 20 20  e OP_Blob: {    
1b619 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f              /* o
1b61a 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1b61b 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
1b61c 3e 70 31 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41  >p1 <= SQLITE_MA
1b61d 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 73 71  X_LENGTH );.  sq
1b61e 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
1b61f 74 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34  tr(pOut, pOp->p4
1b620 2e 7a 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20  .z, pOp->p1, 0, 
1b621 30 29 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20  0);.  pOut->enc 
1b622 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50  = encoding;.  UP
1b623 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1b624 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  E(pOut);.  break
1b625 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1b626 56 61 72 69 61 62 6c 65 20 50 31 20 50 32 20 50  Variable P1 P2 P
1b627 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 61  3 P4 *.**.** Tra
1b628 6e 73 66 65 72 20 74 68 65 20 76 61 6c 75 65 73  nsfer the values
1b629 20 6f 66 20 62 6f 75 6e 64 20 70 61 72 61 6d 65   of bound parame
1b62a 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 33 2d 31  ters P1..P1+P3-1
1b62b 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 0a   into registers.
1b62c 2a 2a 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 0a  ** P2..P2+P3-1..
1b62d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72  **.** If the par
1b62e 61 6d 65 74 65 72 20 69 73 20 6e 61 6d 65 64 2c  ameter is named,
1b62f 20 74 68 65 6e 20 69 74 73 20 6e 61 6d 65 20 61   then its name a
1b630 70 70 65 61 72 73 20 69 6e 20 50 34 20 61 6e 64  ppears in P4 and
1b631 20 50 33 3d 3d 31 2e 0a 2a 2a 20 54 68 65 20 50   P3==1..** The P
1b632 34 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20  4 value is used 
1b633 62 79 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  by sqlite3_bind_
1b634 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29  parameter_name()
1b635 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 61 72  ..*/.case OP_Var
1b636 69 61 62 6c 65 3a 20 7b 0a 20 20 69 6e 74 20 6a  iable: {.  int j
1b637 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a   = pOp->p1 - 1;.
1b638 20 20 69 6e 74 20 6b 20 3d 20 70 4f 70 2d 3e 70    int k = pOp->p
1b639 32 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a  2;.  Mem *pVar;.
1b63a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70    int n = pOp->p
1b63b 33 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d  3;.  assert( j>=
1b63c 30 20 26 26 20 6a 2b 6e 3c 3d 70 2d 3e 6e 56 61  0 && j+n<=p->nVa
1b63d 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b  r );.  assert( k
1b63e 3e 3d 31 20 26 26 20 6b 2b 6e 2d 31 3c 3d 70 2d  >=1 && k+n-1<=p-
1b63f 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72  >nMem );.  asser
1b640 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20  t( pOp->p4.z==0 
1b641 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 29 3b  || pOp->p3==1 );
1b642 0a 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e  ..  while( n-- >
1b643 20 30 20 29 7b 0a 20 20 20 20 70 56 61 72 20 3d   0 ){.    pVar =
1b644 20 26 70 2d 3e 61 56 61 72 5b 6a 2b 2b 5d 3b 0a   &p->aVar[j++];.
1b645 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
1b646 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 56 61  dbeMemTooBig(pVa
1b647 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  r) ){.      goto
1b648 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a   too_big;.    }.
1b649 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61      pOut = &p->a
1b64a 4d 65 6d 5b 6b 2b 2b 5d 3b 0a 20 20 20 20 73 71  Mem[k++];.    sq
1b64b 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1b64c 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74  aseExternal(pOut
1b64d 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61  );.    pOut->fla
1b64e 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1b64f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1b650 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75  mShallowCopy(pOu
1b651 74 2c 20 70 56 61 72 2c 20 4d 45 4d 5f 53 74 61  t, pVar, MEM_Sta
1b652 74 69 63 29 3b 0a 20 20 20 20 55 50 44 41 54 45  tic);.    UPDATE
1b653 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f  _MAX_BLOBSIZE(pO
1b654 75 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  ut);.  }.  break
1b655 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1b656 4d 6f 76 65 20 50 31 20 50 32 20 50 33 20 2a 20  Move P1 P2 P3 * 
1b657 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  *.**.** Move the
1b658 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73   values in regis
1b659 74 65 72 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20  ter P1..P1+P3-1 
1b65a 6f 76 65 72 20 69 6e 74 6f 0a 2a 2a 20 72 65 67  over into.** reg
1b65b 69 73 74 65 72 73 20 50 32 2e 2e 50 32 2b 50 33  isters P2..P2+P3
1b65c 2d 31 2e 20 20 52 65 67 69 73 74 65 72 73 20 50  -1.  Registers P
1b65d 31 2e 2e 50 31 2b 50 31 2d 31 20 61 72 65 0a 2a  1..P1+P1-1 are.*
1b65e 2a 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20 61  * left holding a
1b65f 20 4e 55 4c 4c 2e 20 20 49 74 20 69 73 20 61 6e   NULL.  It is an
1b660 20 65 72 72 6f 72 20 66 6f 72 20 72 65 67 69 73   error for regis
1b661 74 65 72 20 72 61 6e 67 65 73 0a 2a 2a 20 50 31  ter ranges.** P1
1b662 2e 2e 50 31 2b 50 33 2d 31 20 61 6e 64 20 50 32  ..P1+P3-1 and P2
1b663 2e 2e 50 32 2b 50 33 2d 31 20 74 6f 20 6f 76 65  ..P2+P3-1 to ove
1b664 72 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  rlap..*/.case OP
1b665 5f 4d 6f 76 65 3a 20 7b 0a 20 20 63 68 61 72 20  _Move: {.  char 
1b666 2a 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 6e 74 20  *zMalloc;.  int 
1b667 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69  n = pOp->p3;.  i
1b668 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b  nt p1 = pOp->p1;
1b669 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d  .  int p2 = pOp-
1b66a 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  >p2;.  assert( n
1b66b 3e 30 20 26 26 20 70 31 3e 30 20 26 26 20 70 32  >0 && p1>0 && p2
1b66c 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
1b66d 70 31 2b 6e 3c 3d 70 32 20 7c 7c 20 70 32 2b 6e  p1+n<=p2 || p2+n
1b66e 3c 3d 70 31 20 29 3b 0a 0a 20 20 70 49 6e 31 20  <=p1 );..  pIn1 
1b66f 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 31 5d 3b 0a  = &p->aMem[p1];.
1b670 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65    pOut = &p->aMe
1b671 6d 5b 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20  m[p2];.  while( 
1b672 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73 65 72  n-- ){.    asser
1b673 74 28 20 70 4f 75 74 3c 3d 26 70 2d 3e 61 4d 65  t( pOut<=&p->aMe
1b674 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20  m[p->nMem] );.  
1b675 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d    assert( pIn1<=
1b676 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d  &p->aMem[p->nMem
1b677 5d 20 29 3b 0a 20 20 20 20 7a 4d 61 6c 6c 6f 63  ] );.    zMalloc
1b678 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63   = pOut->zMalloc
1b679 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c  ;.    pOut->zMal
1b67a 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  loc = 0;.    sql
1b67b 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
1b67c 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20 20  pOut, pIn1);.   
1b67d 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d   pIn1->zMalloc =
1b67e 20 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 52 45   zMalloc;.    RE
1b67f 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b  GISTER_TRACE(p2+
1b680 2b 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 70 49  +, pOut);.    pI
1b681 6e 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b 2b  n1++;.    pOut++
1b682 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1b683 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 70  ../* Opcode: Cop
1b684 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  y P1 P2 * * *.**
1b685 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20  .** Make a copy 
1b686 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69  of register P1 i
1b687 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e  nto register P2.
1b688 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
1b689 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20  ruction makes a 
1b68a 64 65 65 70 20 63 6f 70 79 20 6f 66 20 74 68 65  deep copy of the
1b68b 20 76 61 6c 75 65 2e 20 20 41 20 64 75 70 6c 69   value.  A dupli
1b68c 63 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65 20  cate.** is made 
1b68d 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72  of any string or
1b68e 20 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e 20   blob constant. 
1b68f 20 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43 6f   See also OP_SCo
1b690 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  py..*/.case OP_C
1b691 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20  opy: {          
1b692 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61     /* in1 */.  a
1b693 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
1b694 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1b695 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p2<=p->nMem )
1b696 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  ;.  pOut = &p->a
1b697 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1b698 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49  assert( pOut!=pI
1b699 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  n1 );.  sqlite3V
1b69a 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
1b69b 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45  y(pOut, pIn1, ME
1b69c 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 65 70  M_Ephem);.  Deep
1b69d 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 29  hemeralize(pOut)
1b69e 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41  ;.  REGISTER_TRA
1b69f 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74  CE(pOp->p2, pOut
1b6a0 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1b6a1 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79 20  * Opcode: SCopy 
1b6a2 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1b6a3 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f 77  * Make a shallow
1b6a4 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65   copy of registe
1b6a5 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74  r P1 into regist
1b6a6 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  er P2..**.** Thi
1b6a7 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61  s instruction ma
1b6a8 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f  kes a shallow co
1b6a9 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e  py of the value.
1b6aa 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a    If the value.*
1b6ab 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72  * is a string or
1b6ac 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 20   blob, then the 
1b6ad 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 70  copy is only a p
1b6ae 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a  ointer to the.**
1b6af 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 65   original and he
1b6b0 6e 63 65 20 69 66 20 74 68 65 20 6f 72 69 67 69  nce if the origi
1b6b1 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 77  nal changes so w
1b6b2 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a  ill the copy..**
1b6b3 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20 6f   Worse, if the o
1b6b4 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c 6c  riginal is deall
1b6b5 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70 79  ocated, the copy
1b6b6 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 64   becomes invalid
1b6b7 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70 72  ..** Thus the pr
1b6b8 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72 61  ogram must guara
1b6b9 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 6f 72  ntee that the or
1b6ba 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20  iginal will not 
1b6bb 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e 67  change.** during
1b6bc 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66   the lifetime of
1b6bd 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73 65 20   the copy.  Use 
1b6be 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65 20  OP_Copy to make 
1b6bf 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f  a complete.** co
1b6c0 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  py..*/.case OP_S
1b6c1 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20  Copy: {         
1b6c2 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 52     /* in1 */.  R
1b6c3 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1b6c4 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20  p->p1, pIn1);.  
1b6c5 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e  assert( pOp->p2>
1b6c6 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1b6c7 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p2<=p->nMem 
1b6c8 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e  );.  pOut = &p->
1b6c9 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1b6ca 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70   assert( pOut!=p
1b6cb 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  In1 );.  sqlite3
1b6cc 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
1b6cd 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d  py(pOut, pIn1, M
1b6ce 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45 47  EM_Ephem);.  REG
1b6cf 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1b6d0 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72  >p2, pOut);.  br
1b6d1 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1b6d2 65 3a 20 52 65 73 75 6c 74 52 6f 77 20 50 31 20  e: ResultRow P1 
1b6d3 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  P2 * * *.**.** T
1b6d4 68 65 20 72 65 67 69 73 74 65 72 73 20 50 31 20  he registers P1 
1b6d5 74 68 72 6f 75 67 68 20 50 31 2b 50 32 2d 31 20  through P1+P2-1 
1b6d6 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65  contain a single
1b6d7 20 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 6c   row of.** resul
1b6d8 74 73 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20  ts. This opcode 
1b6d9 63 61 75 73 65 73 20 74 68 65 20 73 71 6c 69 74  causes the sqlit
1b6da 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74  e3_step() call t
1b6db 6f 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77  o terminate.** w
1b6dc 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f  ith an SQLITE_RO
1b6dd 57 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e  W return code an
1b6de 64 20 69 74 20 73 65 74 73 20 75 70 20 74 68 65  d it sets up the
1b6df 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a   sqlite3_stmt.**
1b6e0 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 70 72   structure to pr
1b6e1 6f 76 69 64 65 20 61 63 63 65 73 73 20 74 6f 20  ovide access to 
1b6e2 74 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65  the top P1 value
1b6e3 73 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a  s as the result.
1b6e4 2a 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20  ** row..*/.case 
1b6e5 4f 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0a  OP_ResultRow: {.
1b6e6 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69    Mem *pMem;.  i
1b6e7 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
1b6e8 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70  p->nResColumn==p
1b6e9 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65  Op->p2 );.  asse
1b6ea 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b  rt( pOp->p1>0 );
1b6eb 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1b6ec 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e  p1+pOp->p2<=p->n
1b6ed 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49  Mem+1 );..  /* I
1b6ee 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 6f 75  f the SQLITE_Cou
1b6ef 6e 74 52 6f 77 73 20 66 6c 61 67 20 69 73 20 73  ntRows flag is s
1b6f0 65 74 20 69 6e 20 73 71 6c 69 74 65 33 2e 66 6c  et in sqlite3.fl
1b6f1 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 0a  ags mask, then .
1b6f2 20 20 2a 2a 20 44 4d 4c 20 73 74 61 74 65 6d 65    ** DML stateme
1b6f3 6e 74 73 20 69 6e 76 6f 6b 65 20 74 68 69 73 20  nts invoke this 
1b6f4 6f 70 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e  opcode to return
1b6f5 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
1b6f6 6f 77 73 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69  ows .  ** modifi
1b6f7 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  ed to the user. 
1b6f8 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79  This is the only
1b6f9 20 77 61 79 20 74 68 61 74 20 61 20 56 4d 20 74   way that a VM t
1b6fa 68 61 74 0a 20 20 2a 2a 20 6f 70 65 6e 73 20 61  hat.  ** opens a
1b6fb 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
1b6fc 61 63 74 69 6f 6e 20 6d 61 79 20 69 6e 76 6f 6b  action may invok
1b6fd 65 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 20  e this opcode.. 
1b6fe 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63 61 73 65   **.  ** In case
1b6ff 20 74 68 69 73 20 69 73 20 73 75 63 68 20 61 20   this is such a 
1b700 73 74 61 74 65 6d 65 6e 74 2c 20 63 6c 6f 73 65  statement, close
1b701 20 61 6e 79 20 73 74 61 74 65 6d 65 6e 74 20 74   any statement t
1b702 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20  ransaction.  ** 
1b703 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 56  opened by this V
1b704 4d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  M before returni
1b705 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68  ng control to th
1b706 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20  e user. This is 
1b707 74 6f 0a 20 20 2a 2a 20 65 6e 73 75 72 65 20 74  to.  ** ensure t
1b708 68 61 74 20 73 74 61 74 65 6d 65 6e 74 2d 74 72  hat statement-tr
1b709 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 61  ansactions are a
1b70a 6c 77 61 79 73 20 6e 65 73 74 65 64 2c 20 6e 6f  lways nested, no
1b70b 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 2e 0a 20  t overlapping.. 
1b70c 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 6e 20   ** If the open 
1b70d 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61  statement-transa
1b70e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6c 6f  ction is not clo
1b70f 73 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 74  sed here, then t
1b710 68 65 20 75 73 65 72 0a 20 20 2a 2a 20 6d 61 79  he user.  ** may
1b711 20 73 74 65 70 20 61 6e 6f 74 68 65 72 20 56 4d   step another VM
1b712 20 74 68 61 74 20 6f 70 65 6e 73 20 69 74 73 20   that opens its 
1b713 6f 77 6e 20 73 74 61 74 65 6d 65 6e 74 20 74 72  own statement tr
1b714 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 0a  ansaction. This.
1b715 20 20 2a 2a 20 6d 61 79 20 6c 65 61 64 20 74 6f    ** may lead to
1b716 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 73 74 61   overlapping sta
1b717 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1b718 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ons..  */.  asse
1b719 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  rt( p->iStatemen
1b71a 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66 6c 61 67  t==0 || db->flag
1b71b 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  s&SQLITE_CountRo
1b71c 77 73 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  ws );.  if( SQLI
1b71d 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
1b71e 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61  ite3VdbeCloseSta
1b71f 74 65 6d 65 6e 74 28 70 2c 20 53 41 56 45 50 4f  tement(p, SAVEPO
1b720 49 4e 54 5f 52 45 4c 45 41 53 45 29 29 20 29 7b  INT_RELEASE)) ){
1b721 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1b722 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65  .  /* Invalidate
1b723 20 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63   all ephemeral c
1b724 75 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73  ursor row caches
1b725 20 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74   */.  p->cacheCt
1b726 72 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72  r = (p->cacheCtr
1b727 20 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d   + 2)|1;..  /* M
1b728 61 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73  ake sure the res
1b729 75 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72  ults of the curr
1b72a 65 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30  ent row are \000
1b72b 20 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a   terminated.  **
1b72c 20 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73   and have an ass
1b72d 69 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65  igned type.  The
1b72e 20 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d   results are de-
1b72f 65 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73  ephemeralized as
1b730 0a 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66  .  ** as side ef
1b731 66 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 70 4d 65  fect..  */.  pMe
1b732 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 65  m = p->pResultSe
1b733 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t = &p->aMem[pOp
1b734 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30  ->p1];.  for(i=0
1b735 3b 20 69 3c 70 4f 70 2d 3e 70 32 3b 20 69 2b 2b  ; i<pOp->p2; i++
1b736 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1b737 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74  beMemNulTerminat
1b738 65 28 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 20  e(&pMem[i]);.   
1b739 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 26   storeTypeInfo(&
1b73a 70 4d 65 6d 5b 69 5d 2c 20 65 6e 63 6f 64 69 6e  pMem[i], encodin
1b73b 67 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52  g);.    REGISTER
1b73c 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 69  _TRACE(pOp->p1+i
1b73d 2c 20 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 7d  , &pMem[i]);.  }
1b73e 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
1b73f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e  cFailed ) goto n
1b740 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74  o_mem;..  /* Ret
1b741 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20  urn SQLITE_ROW. 
1b742 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63   */.  p->pc = pc
1b743 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c   + 1;.  rc = SQL
1b744 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20  ITE_ROW;.  goto 
1b745 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a  vdbe_return;.}..
1b746 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61  /* Opcode: Conca
1b747 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  t P1 P2 P3 * *.*
1b748 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 74 65 78  *.** Add the tex
1b749 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  t in register P1
1b74a 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66   onto the end of
1b74b 20 74 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20   the text in.** 
1b74c 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20  register P2 and 
1b74d 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
1b74e 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1b74f 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 74 68  .** If either th
1b750 65 20 50 31 20 6f 72 20 50 32 20 74 65 78 74 20  e P1 or P2 text 
1b751 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74  are NULL then st
1b752 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a  ore NULL in P3..
1b753 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 20  **.**   P3 = P2 
1b754 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69  || P1.**.** It i
1b755 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31  s illegal for P1
1b756 20 61 6e 64 20 50 33 20 74 6f 20 62 65 20 74 68   and P3 to be th
1b757 65 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e  e same register.
1b758 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69   Sometimes,.** i
1b759 66 20 50 33 20 69 73 20 74 68 65 20 73 61 6d 65  f P3 is the same
1b75a 20 72 65 67 69 73 74 65 72 20 61 73 20 50 32 2c   register as P2,
1b75b 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
1b75c 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74  ion is able.** t
1b75d 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 63 70 79  o avoid a memcpy
1b75e 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  ()..*/.case OP_C
1b75f 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20 20 20 20  oncat: {        
1b760 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1b761 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e  _CONCAT, in1, in
1b762 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69 36 34  2, out3 */.  i64
1b763 20 6e 42 79 74 65 3b 0a 0a 20 20 61 73 73 65 72   nByte;..  asser
1b764 74 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b  t( pIn1!=pOut );
1b765 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
1b766 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67  ags | pIn2->flag
1b767 73 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b  s) & MEM_Null ){
1b768 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b769 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29  MemSetNull(pOut)
1b76a 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  ;.    break;.  }
1b76b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49  .  ExpandBlob(pI
1b76c 6e 31 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 79  n1);.  Stringify
1b76d 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29  (pIn1, encoding)
1b76e 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70  ;.  ExpandBlob(p
1b76f 49 6e 32 29 3b 0a 20 20 53 74 72 69 6e 67 69 66  In2);.  Stringif
1b770 79 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e 67  y(pIn2, encoding
1b771 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 70 49 6e  );.  nByte = pIn
1b772 31 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a  1->n + pIn2->n;.
1b773 20 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e    if( nByte>db->
1b774 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1b775 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
1b776 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1b777 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70  .  }.  MemSetTyp
1b778 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1b779 53 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  Str);.  if( sqli
1b77a 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
1b77b 4f 75 74 2c 20 28 69 6e 74 29 6e 42 79 74 65 2b  Out, (int)nByte+
1b77c 32 2c 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29  2, pOut==pIn2) )
1b77d 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
1b77e 6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75  m;.  }.  if( pOu
1b77f 74 21 3d 70 49 6e 32 20 29 7b 0a 20 20 20 20 6d  t!=pIn2 ){.    m
1b780 65 6d 63 70 79 28 70 4f 75 74 2d 3e 7a 2c 20 70  emcpy(pOut->z, p
1b781 49 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29  In2->z, pIn2->n)
1b782 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26  ;.  }.  memcpy(&
1b783 70 4f 75 74 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d  pOut->z[pIn2->n]
1b784 2c 20 70 49 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d  , pIn1->z, pIn1-
1b785 3e 6e 29 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 6e  >n);.  pOut->z[n
1b786 42 79 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75  Byte] = 0;.  pOu
1b787 74 2d 3e 7a 5b 6e 42 79 74 65 2b 31 5d 20 3d 20  t->z[nByte+1] = 
1b788 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  0;.  pOut->flags
1b789 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20   |= MEM_Term;.  
1b78a 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 6e  pOut->n = (int)n
1b78b 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e  Byte;.  pOut->en
1b78c 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20  c = encoding;.  
1b78d 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1b78e 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65  IZE(pOut);.  bre
1b78f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1b790 3a 20 41 64 64 20 50 31 20 50 32 20 50 33 20 2a  : Add P1 P2 P3 *
1b791 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65   *.**.** Add the
1b792 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1b793 65 72 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c  er P1 to the val
1b794 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1b795 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74  2.** and store t
1b796 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
1b797 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20  ister P3..** If 
1b798 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
1b799 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
1b79a 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20   is NULL..*/./* 
1b79b 4f 70 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79  Opcode: Multiply
1b79c 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1b79d 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20  .**.** Multiply 
1b79e 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1b79f 69 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20  ister P1 by the 
1b7a0 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1b7a1 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72  r P2.** and stor
1b7a2 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  e the result in 
1b7a3 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20  register P3..** 
1b7a4 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
1b7a5 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73  is NULL, the res
1b7a6 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ult is NULL..*/.
1b7a7 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72  /* Opcode: Subtr
1b7a8 61 63 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a  act P1 P2 P3 * *
1b7a9 0a 2a 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20  .**.** Subtract 
1b7aa 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1b7ab 69 73 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68  ister P1 from th
1b7ac 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1b7ad 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74  ter P2.** and st
1b7ae 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1b7af 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1b7b0 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
1b7b1 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1b7b2 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1b7b3 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76  /./* Opcode: Div
1b7b4 69 64 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ide P1 P2 P3 * *
1b7b5 0a 2a 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68  .**.** Divide th
1b7b6 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1b7b7 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61  ter P1 by the va
1b7b8 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1b7b9 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20  P2.** and store 
1b7ba 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  the result in re
1b7bb 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 74  gister P3.  If t
1b7bc 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1b7bd 73 74 65 72 20 50 32 0a 2a 2a 20 69 73 20 7a 65  ster P2.** is ze
1b7be 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ro, then the res
1b7bf 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20  ult is NULL..** 
1b7c0 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
1b7c1 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73  is NULL, the res
1b7c2 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ult is NULL..*/.
1b7c3 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 69  /* Opcode: Remai
1b7c4 6e 64 65 72 20 50 31 20 50 32 20 50 33 20 2a 20  nder P1 P2 P3 * 
1b7c5 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  *.**.** Compute 
1b7c6 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 61 66  the remainder af
1b7c7 74 65 72 20 69 6e 74 65 67 65 72 20 64 69 76 69  ter integer divi
1b7c8 73 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75  sion of the valu
1b7c9 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72  e in.** register
1b7ca 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 65   P1 by the value
1b7cb 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20   in register P2 
1b7cc 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65  and store the re
1b7cd 73 75 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a 20  sult in P3. .** 
1b7ce 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  If the value in 
1b7cf 72 65 67 69 73 74 65 72 20 50 32 20 69 73 20 7a  register P2 is z
1b7d0 65 72 6f 20 74 68 65 20 72 65 73 75 6c 74 20 69  ero the result i
1b7d1 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69  s NULL..** If ei
1b7d2 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
1b7d3 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
1b7d4 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73   is NULL..*/.cas
1b7d5 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20  e OP_Add:       
1b7d6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1b7d7 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c 20  ame as TK_PLUS, 
1b7d8 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1b7d9 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61  /.case OP_Subtra
1b7da 63 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  ct:             
1b7db 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d   /* same as TK_M
1b7dc 49 4e 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  INUS, in1, in2, 
1b7dd 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  out3 */.case OP_
1b7de 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 20 20 20  Multiply:       
1b7df 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1b7e0 73 20 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c 20  s TK_STAR, in1, 
1b7e1 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73  in2, out3 */.cas
1b7e2 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 20  e OP_Divide:    
1b7e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1b7e4 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 2c  ame as TK_SLASH,
1b7e5 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
1b7e6 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 69  */.case OP_Remai
1b7e7 6e 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20  nder: {         
1b7e8 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1b7e9 52 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  REM, in1, in2, o
1b7ea 75 74 33 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  ut3 */.  int fla
1b7eb 67 73 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72  gs;.  applyNumer
1b7ec 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 31 29  icAffinity(pIn1)
1b7ed 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63  ;.  applyNumeric
1b7ee 41 66 66 69 6e 69 74 79 28 70 49 6e 32 29 3b 0a  Affinity(pIn2);.
1b7ef 20 20 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e    flags = pIn1->
1b7f0 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c  flags | pIn2->fl
1b7f1 61 67 73 3b 0a 20 20 69 66 28 20 28 66 6c 61 67  ags;.  if( (flag
1b7f2 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30  s & MEM_Null)!=0
1b7f3 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74   ) goto arithmet
1b7f4 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
1b7f5 6c 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  l;.  if( (pIn1->
1b7f6 66 6c 61 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c  flags & pIn2->fl
1b7f7 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d  ags & MEM_Int)==
1b7f8 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 69  MEM_Int ){.    i
1b7f9 36 34 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d  64 a, b;.    a =
1b7fa 20 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20   pIn1->u.i;.    
1b7fb 62 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20  b = pIn2->u.i;. 
1b7fc 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e     switch( pOp->
1b7fd 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  opcode ){.      
1b7fe 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20  case OP_Add:    
1b7ff 20 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20       b += a;    
1b800 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1b801 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74  case OP_Subtract
1b802 3a 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20  :    b -= a;    
1b803 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1b804 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79  case OP_Multiply
1b805 3a 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20  :    b *= a;    
1b806 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1b807 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20  case OP_Divide: 
1b808 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 3d  {.        if( a=
1b809 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d  =0 ) goto arithm
1b80a 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e  etic_result_is_n
1b80b 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ull;.        /* 
1b80c 44 69 76 69 64 69 6e 67 20 74 68 65 20 6c 61 72  Dividing the lar
1b80d 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e 65  gest possible ne
1b80e 67 61 74 69 76 65 20 36 34 2d 62 69 74 20 69 6e  gative 64-bit in
1b80f 74 65 67 65 72 20 28 31 3c 3c 36 33 29 20 62 79  teger (1<<63) by
1b810 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20   .        ** -1 
1b811 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67  returns an integ
1b812 65 72 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20  er too large to 
1b813 73 74 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69  store in a 64-bi
1b814 74 20 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a  t data-type. On.
1b815 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20          ** some 
1b816 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74  architectures, t
1b817 68 65 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f  he value overflo
1b818 77 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f  ws to (1<<63). O
1b819 6e 20 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20  n others,.      
1b81a 20 20 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73    ** a SIGFPE is
1b81b 20 69 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c   issued. The fol
1b81c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  lowing statement
1b81d 20 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73   normalizes this
1b81e 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61  .        ** beha
1b81f 76 69 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c  vior so that all
1b820 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 62   architectures b
1b821 65 68 61 76 65 20 61 73 20 69 66 20 69 6e 74 65  ehave as if inte
1b822 67 65 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ger .        ** 
1b823 6f 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65  overflow occurre
1b824 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  d..        */.  
1b825 20 20 20 20 20 20 69 66 28 20 61 3d 3d 2d 31 20        if( a==-1 
1b826 26 26 20 62 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49  && b==SMALLEST_I
1b827 4e 54 36 34 20 29 20 61 20 3d 20 31 3b 0a 20 20  NT64 ) a = 1;.  
1b828 20 20 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20 20        b /= a;.  
1b829 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1b82a 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75     }.      defau
1b82b 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66  lt: {.        if
1b82c 28 20 61 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72  ( a==0 ) goto ar
1b82d 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f  ithmetic_result_
1b82e 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20  is_null;.       
1b82f 20 69 66 28 20 61 3d 3d 2d 31 20 29 20 61 20 3d   if( a==-1 ) a =
1b830 20 31 3b 0a 20 20 20 20 20 20 20 20 62 20 25 3d   1;.        b %=
1b831 20 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   a;.        brea
1b832 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1b833 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  .    pOut->u.i =
1b834 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79   b;.    MemSetTy
1b835 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1b836 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _Int);.  }else{.
1b837 20 20 20 20 64 6f 75 62 6c 65 20 61 2c 20 62 3b      double a, b;
1b838 0a 20 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33  .    a = sqlite3
1b839 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49  VdbeRealValue(pI
1b83a 6e 31 29 3b 0a 20 20 20 20 62 20 3d 20 73 71 6c  n1);.    b = sql
1b83b 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
1b83c 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69  e(pIn2);.    swi
1b83d 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  tch( pOp->opcode
1b83e 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f   ){.      case O
1b83f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 62  P_Add:         b
1b840 20 2b 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65   += a;       bre
1b841 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ak;.      case O
1b842 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20 62  P_Subtract:    b
1b843 20 2d 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65   -= a;       bre
1b844 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ak;.      case O
1b845 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 62  P_Multiply:    b
1b846 20 2a 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65   *= a;       bre
1b847 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ak;.      case O
1b848 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20  P_Divide: {.    
1b849 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30      /* (double)0
1b84a 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
1b84b 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
1b84c 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20  _POINT... */.   
1b84d 20 20 20 20 20 69 66 28 20 61 3d 3d 28 64 6f 75       if( a==(dou
1b84e 62 6c 65 29 30 20 29 20 67 6f 74 6f 20 61 72 69  ble)0 ) goto ari
1b84f 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
1b850 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  s_null;.        
1b851 62 20 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20  b /= a;.        
1b852 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1b853 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
1b854 20 20 20 20 20 20 20 20 69 36 34 20 69 61 20 3d          i64 ia =
1b855 20 28 69 36 34 29 61 3b 0a 20 20 20 20 20 20 20   (i64)a;.       
1b856 20 69 36 34 20 69 62 20 3d 20 28 69 36 34 29 62   i64 ib = (i64)b
1b857 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 61  ;.        if( ia
1b858 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68  ==0 ) goto arith
1b859 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
1b85a 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66  null;.        if
1b85b 28 20 69 61 3d 3d 2d 31 20 29 20 69 61 20 3d 20  ( ia==-1 ) ia = 
1b85c 31 3b 0a 20 20 20 20 20 20 20 20 62 20 3d 20 28  1;.        b = (
1b85d 64 6f 75 62 6c 65 29 28 69 62 20 25 20 69 61 29  double)(ib % ia)
1b85e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1b85f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1b860 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
1b861 4e 61 4e 28 62 29 20 29 7b 0a 20 20 20 20 20 20  NaN(b) ){.      
1b862 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f  goto arithmetic_
1b863 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a  result_is_null;.
1b864 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e      }.    pOut->
1b865 72 20 3d 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65  r = b;.    MemSe
1b866 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1b867 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69  MEM_Real);.    i
1b868 66 28 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f  f( (flags & MEM_
1b869 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Real)==0 ){.    
1b86a 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74    sqlite3VdbeInt
1b86b 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75  egerAffinity(pOu
1b86c 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  t);.    }.  }.  
1b86d 62 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74  break;..arithmet
1b86e 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
1b86f 6c 3a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  l:.  sqlite3Vdbe
1b870 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29  MemSetNull(pOut)
1b871 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1b872 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71   Opcode: CollSeq
1b873 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34   * * P4.**.** P4
1b874 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1b875 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63   a CollSeq struc
1b876 74 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 63  t. If the next c
1b877 61 6c 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75  all to a user fu
1b878 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67  nction.** or agg
1b879 72 65 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c  regate calls sql
1b87a 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
1b87b 65 71 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61  eq(), this colla
1b87c 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69  tion sequence wi
1b87d 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65  ll.** be returne
1b87e 64 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  d. This is used 
1b87f 62 79 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  by the built-in 
1b880 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64  min(), max() and
1b881 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e   nullif().** fun
1b882 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ctions..**.** Th
1b883 65 20 69 6e 74 65 72 66 61 63 65 20 75 73 65 64  e interface used
1b884 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   by the implemen
1b885 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66  tation of the af
1b886 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e  orementioned fun
1b887 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74  ctions.** to ret
1b888 72 69 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74  rieve the collat
1b889 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74  ion sequence set
1b88a 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20   by this opcode 
1b88b 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
1b88c 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e  .** publicly, on
1b88d 6c 79 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74  ly to user funct
1b88e 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20  ions defined in 
1b88f 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20  func.c..*/.case 
1b890 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20  OP_CollSeq: {.  
1b891 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1b892 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20  ype==P4_COLLSEQ 
1b893 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1b894 2a 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69  * Opcode: Functi
1b895 6f 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50  on P1 P2 P3 P4 P
1b896 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61  5.**.** Invoke a
1b897 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28   user function (
1b898 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
1b899 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74  to a Function st
1b89a 72 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20  ructure that.** 
1b89b 64 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63  defines the func
1b89c 74 69 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72  tion) with P5 ar
1b89d 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72  guments taken fr
1b89e 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61  om register P2 a
1b89f 6e 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73  nd.** successors
1b8a0 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66  .  The result of
1b8a1 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
1b8a2 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1b8a3 74 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73  ter P3..** Regis
1b8a4 74 65 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20  ter P3 must not 
1b8a5 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75  be one of the fu
1b8a6 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a  nction inputs..*
1b8a7 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d  *.** P1 is a 32-
1b8a8 62 69 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69  bit bitmask indi
1b8a9 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f  cating whether o
1b8aa 72 20 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d  r not each argum
1b8ab 65 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66  ent to the .** f
1b8ac 75 6e 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65  unction was dete
1b8ad 72 6d 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e  rmined to be con
1b8ae 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65  stant at compile
1b8af 20 74 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69   time. If the fi
1b8b0 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  rst.** argument 
1b8b1 77 61 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65  was constant the
1b8b2 6e 20 62 69 74 20 30 20 6f 66 20 50 31 20 69 73  n bit 0 of P1 is
1b8b3 20 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73   set. This is us
1b8b4 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a  ed to determine.
1b8b5 2a 2a 20 77 68 65 74 68 65 72 20 6d 65 74 61 20  ** whether meta 
1b8b6 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20  data associated 
1b8b7 77 69 74 68 20 61 20 75 73 65 72 20 66 75 6e 63  with a user func
1b8b8 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73  tion argument us
1b8b9 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  ing the.** sqlit
1b8ba 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29  e3_set_auxdata()
1b8bb 20 41 50 49 20 6d 61 79 20 62 65 20 73 61 66 65   API may be safe
1b8bc 6c 79 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69  ly retained unti
1b8bd 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e  l the next.** in
1b8be 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  vocation of this
1b8bf 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53   opcode..**.** S
1b8c0 65 65 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70  ee also: AggStep
1b8c1 20 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f   and AggFinal.*/
1b8c2 0a 63 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f  .case OP_Functio
1b8c3 6e 3a 20 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  n: {.  int i;.  
1b8c4 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c  Mem *pArg;.  sql
1b8c5 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78  ite3_context ctx
1b8c6 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ;.  sqlite3_valu
1b8c7 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74  e **apVal;.  int
1b8c8 20 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 0a 20   n = pOp->p5;.. 
1b8c9 20 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72   apVal = p->apAr
1b8ca 67 3b 0a 20 20 61 73 73 65 72 74 28 20 61 70 56  g;.  assert( apV
1b8cb 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20  al || n==0 );.. 
1b8cc 20 61 73 73 65 72 74 28 20 6e 3d 3d 30 20 7c 7c   assert( n==0 ||
1b8cd 20 28 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70   (pOp->p2>0 && p
1b8ce 4f 70 2d 3e 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65  Op->p2+n<=p->nMe
1b8cf 6d 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74  m+1) );.  assert
1b8d0 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70  ( pOp->p3<pOp->p
1b8d1 32 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f  2 || pOp->p3>=pO
1b8d2 70 2d 3e 70 32 2b 6e 20 29 3b 0a 20 20 70 41 72  p->p2+n );.  pAr
1b8d3 67 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  g = &p->aMem[pOp
1b8d4 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 69 3d 30  ->p2];.  for(i=0
1b8d5 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 41 72 67  ; i<n; i++, pArg
1b8d6 2b 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b 69  ++){.    apVal[i
1b8d7 5d 20 3d 20 70 41 72 67 3b 0a 20 20 20 20 73 74  ] = pArg;.    st
1b8d8 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 41 72 67  oreTypeInfo(pArg
1b8d9 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20  , encoding);.   
1b8da 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1b8db 70 4f 70 2d 3e 70 32 2c 20 70 41 72 67 29 3b 0a  pOp->p2, pArg);.
1b8dc 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70    }..  assert( p
1b8dd 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46  Op->p4type==P4_F
1b8de 55 4e 43 44 45 46 20 7c 7c 20 70 4f 70 2d 3e 70  UNCDEF || pOp->p
1b8df 34 74 79 70 65 3d 3d 50 34 5f 56 44 42 45 46 55  4type==P4_VDBEFU
1b8e0 4e 43 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d  NC );.  if( pOp-
1b8e1 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43  >p4type==P4_FUNC
1b8e2 44 45 46 20 29 7b 0a 20 20 20 20 63 74 78 2e 70  DEF ){.    ctx.p
1b8e3 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Func = pOp->p4.p
1b8e4 46 75 6e 63 3b 0a 20 20 20 20 63 74 78 2e 70 56  Func;.    ctx.pV
1b8e5 64 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d  dbeFunc = 0;.  }
1b8e6 65 6c 73 65 7b 0a 20 20 20 20 63 74 78 2e 70 56  else{.    ctx.pV
1b8e7 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 46  dbeFunc = (VdbeF
1b8e8 75 6e 63 2a 29 70 4f 70 2d 3e 70 34 2e 70 56 64  unc*)pOp->p4.pVd
1b8e9 62 65 46 75 6e 63 3b 0a 20 20 20 20 63 74 78 2e  beFunc;.    ctx.
1b8ea 70 46 75 6e 63 20 3d 20 63 74 78 2e 70 56 64 62  pFunc = ctx.pVdb
1b8eb 65 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20  eFunc->pFunc;.  
1b8ec 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  }..  assert( pOp
1b8ed 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
1b8ee 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1b8ef 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
1b8f0 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 63 74 78 2e  pOp->p3];.  ctx.
1b8f1 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  s.flags = MEM_Nu
1b8f2 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 64 62 20 3d  ll;.  ctx.s.db =
1b8f3 20 64 62 3b 0a 20 20 63 74 78 2e 73 2e 78 44 65   db;.  ctx.s.xDe
1b8f4 6c 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a  l = 0;.  ctx.s.z
1b8f5 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f  Malloc = 0;..  /
1b8f6 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c  * The output cel
1b8f7 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61  l may already ha
1b8f8 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f  ve a buffer allo
1b8f9 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a  cated. Move.  **
1b8fa 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
1b8fb 63 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 73 65  ctx.s so in case
1b8fc 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69   the user-functi
1b8fd 6f 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20  on can use.  ** 
1b8fe 74 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f  the already allo
1b8ff 63 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73  cated buffer ins
1b900 74 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69  tead of allocati
1b901 6e 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20  ng a new one..  
1b902 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
1b903 4d 65 6d 4d 6f 76 65 28 26 63 74 78 2e 73 2c 20  MemMove(&ctx.s, 
1b904 70 4f 75 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54  pOut);.  MemSetT
1b905 79 70 65 46 6c 61 67 28 26 63 74 78 2e 73 2c 20  ypeFlag(&ctx.s, 
1b906 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 63 74  MEM_Null);..  ct
1b907 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20  x.isError = 0;. 
1b908 20 69 66 28 20 63 74 78 2e 70 46 75 6e 63 2d 3e   if( ctx.pFunc->
1b909 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
1b90a 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a  UNC_NEEDCOLL ){.
1b90b 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e      assert( pOp>
1b90c 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 73  p->aOp );.    as
1b90d 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34  sert( pOp[-1].p4
1b90e 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51  type==P4_COLLSEQ
1b90f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1b910 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d  pOp[-1].opcode==
1b911 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20  OP_CollSeq );.  
1b912 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f    ctx.pColl = pO
1b913 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a  p[-1].p4.pColl;.
1b914 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
1b915 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1b916 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1b917 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 28 2a 63  to_misuse;.  (*c
1b918 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29  tx.pFunc->xFunc)
1b919 28 26 63 74 78 2c 20 6e 2c 20 61 70 56 61 6c 29  (&ctx, n, apVal)
1b91a 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
1b91b 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20  afetyOn(db) ){. 
1b91c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1b91d 6d 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29  mRelease(&ctx.s)
1b91e 3b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74  ;.    goto abort
1b91f 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1b920 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
1b921 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1b922 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68    /* Even though
1b923 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20   a malloc() has 
1b924 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70 6c  failed, the impl
1b925 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
1b926 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66 75  e.    ** user fu
1b927 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65 20  nction may have 
1b928 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74 65  called an sqlite
1b929 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20 66  3_result_XXX() f
1b92a 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 74  unction.    ** t
1b92b 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75 65  o return a value
1b92c 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
1b92d 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 6e  call releases an
1b92e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20 20  y resources.    
1b92f 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ** associated wi
1b930 74 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 2e  th such a value.
1b931 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e  .    **.    ** N
1b932 6f 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52 65  ote: Maybe MemRe
1b933 6c 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20 62  lease() should b
1b934 65 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c 69  e called if sqli
1b935 74 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20 20  te3SafetyOn().  
1b936 20 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f 20    ** fails also 
1b937 28 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74 61  (the if(...) sta
1b938 74 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20 42  tement above). B
1b939 75 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72 65  ut if people are
1b93a 0a 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e 67  .    ** misusing
1b93b 20 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68 61   sqlite, they ha
1b93c 76 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c 65  ve bigger proble
1b93d 6d 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65 64  ms than a leaked
1b93e 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20   value..    */. 
1b93f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1b940 6d 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29  mRelease(&ctx.s)
1b941 3b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ;.    goto no_me
1b942 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  m;.  }..  /* If 
1b943 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20 64 61  any auxiliary da
1b944 74 61 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76  ta functions hav
1b945 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 62 79  e been called by
1b946 20 74 68 69 73 20 75 73 65 72 20 66 75 6e 63 74   this user funct
1b947 69 6f 6e 2c 0a 20 20 2a 2a 20 69 6d 6d 65 64 69  ion,.  ** immedi
1b948 61 74 65 6c 79 20 63 61 6c 6c 20 74 68 65 20 64  ately call the d
1b949 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 61 6e  estructor for an
1b94a 79 20 6e 6f 6e 2d 73 74 61 74 69 63 20 76 61 6c  y non-static val
1b94b 75 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ues..  */.  if( 
1b94c 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20 29 7b  ctx.pVdbeFunc ){
1b94d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b94e 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 63 74  DeleteAuxData(ct
1b94f 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70  x.pVdbeFunc, pOp
1b950 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  ->p1);.    pOp->
1b951 70 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 63  p4.pVdbeFunc = c
1b952 74 78 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20  tx.pVdbeFunc;.  
1b953 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
1b954 50 34 5f 56 44 42 45 46 55 4e 43 3b 0a 20 20 7d  P4_VDBEFUNC;.  }
1b955 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 75  ..  /* If the fu
1b956 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64 20  nction returned 
1b957 61 6e 20 65 72 72 6f 72 2c 20 74 68 72 6f 77 20  an error, throw 
1b958 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a  an exception */.
1b959 20 20 69 66 28 20 63 74 78 2e 69 73 45 72 72 6f    if( ctx.isErro
1b95a 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
1b95b 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1b95c 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
1b95d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1b95e 65 78 74 28 26 63 74 78 2e 73 29 29 3b 0a 20 20  ext(&ctx.s));.  
1b95f 20 20 72 63 20 3d 20 63 74 78 2e 69 73 45 72 72    rc = ctx.isErr
1b960 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f  or;.  }..  /* Co
1b961 70 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  py the result of
1b962 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e   the function in
1b963 74 6f 20 72 65 67 69 73 74 65 72 20 50 33 20 2a  to register P3 *
1b964 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  /.  sqlite3VdbeC
1b965 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 63  hangeEncoding(&c
1b966 74 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  tx.s, encoding);
1b967 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1b968 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 26 63 74 78  mMove(pOut, &ctx
1b969 2e 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  .s);.  if( sqlit
1b96a 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28  e3VdbeMemTooBig(
1b96b 70 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74  pOut) ){.    got
1b96c 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20  o too_big;.  }. 
1b96d 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1b96e 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a  pOp->p3, pOut);.
1b96f 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1b970 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62  BSIZE(pOut);.  b
1b971 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1b972 64 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32  de: BitAnd P1 P2
1b973 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61   P3 * *.**.** Ta
1b974 6b 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 20  ke the bit-wise 
1b975 41 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65  AND of the value
1b976 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  s in register P1
1b977 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73   and P2 and.** s
1b978 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1b979 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a  in register P3..
1b97a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70  ** If either inp
1b97b 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20  ut is NULL, the 
1b97c 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a  result is NULL..
1b97d 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69  */./* Opcode: Bi
1b97e 74 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a  tOr P1 P2 P3 * *
1b97f 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20  .**.** Take the 
1b980 62 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74  bit-wise OR of t
1b981 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67  he values in reg
1b982 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20  ister P1 and P2 
1b983 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65  and.** store the
1b984 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73   result in regis
1b985 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69  ter P3..** If ei
1b986 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55  ther input is NU
1b987 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69  LL, the result i
1b988 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70  s NULL..*/./* Op
1b989 63 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20  code: ShiftLeft 
1b98a 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1b98b 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74  ** Shift the int
1b98c 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65  eger value in re
1b98d 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65  gister P2 to the
1b98e 20 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20   left by the.** 
1b98f 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73  number of bits s
1b990 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20  pecified by the 
1b991 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73  integer in regis
1b992 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20  er P1..** Store 
1b993 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  the result in re
1b994 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66  gister P3..** If
1b995 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73   either input is
1b996 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c   NULL, the resul
1b997 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a  t is NULL..*/./*
1b998 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69   Opcode: ShiftRi
1b999 67 68 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ght P1 P2 P3 * *
1b99a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65  .**.** Shift the
1b99b 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69   integer value i
1b99c 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f  n register P2 to
1b99d 20 74 68 65 20 72 69 67 68 74 20 62 79 20 74 68   the right by th
1b99e 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62  e.** number of b
1b99f 69 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79  its specified by
1b9a0 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20   the integer in 
1b9a1 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20  register P1..** 
1b9a2 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  Store the result
1b9a3 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1b9a4 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e  .** If either in
1b9a5 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  put is NULL, the
1b9a6 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
1b9a7 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41  .*/.case OP_BitA
1b9a8 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  nd:             
1b9a9 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1b9aa 4b 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69  K_BITAND, in1, i
1b9ab 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
1b9ac 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20   OP_BitOr:      
1b9ad 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1b9ae 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c  ame as TK_BITOR,
1b9af 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
1b9b0 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74  */.case OP_Shift
1b9b1 4c 65 66 74 3a 20 20 20 20 20 20 20 20 20 20 20  Left:           
1b9b2 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1b9b3 5f 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e  _LSHIFT, in1, in
1b9b4 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20  2, out3 */.case 
1b9b5 4f 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b  OP_ShiftRight: {
1b9b6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1b9b7 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c  me as TK_RSHIFT,
1b9b8 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
1b9b9 2a 2f 0a 20 20 69 36 34 20 61 2c 20 62 3b 0a 0a  */.  i64 a, b;..
1b9ba 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1b9bb 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73  gs | pIn2->flags
1b9bc 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  ) & MEM_Null ){.
1b9bd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1b9be 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b  emSetNull(pOut);
1b9bf 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1b9c0 20 20 61 20 3d 20 73 71 6c 69 74 65 33 56 64 62    a = sqlite3Vdb
1b9c1 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b  eIntValue(pIn2);
1b9c2 0a 20 20 62 20 3d 20 73 71 6c 69 74 65 33 56 64  .  b = sqlite3Vd
1b9c3 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29  beIntValue(pIn1)
1b9c4 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d  ;.  switch( pOp-
1b9c5 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63  >opcode ){.    c
1b9c6 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20  ase OP_BitAnd:  
1b9c7 20 20 20 20 61 20 26 3d 20 62 3b 20 20 20 20 20      a &= b;     
1b9c8 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
1b9c9 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20  OP_BitOr:       
1b9ca 61 20 7c 3d 20 62 3b 20 20 20 20 20 62 72 65 61  a |= b;     brea
1b9cb 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 53  k;.    case OP_S
1b9cc 68 69 66 74 4c 65 66 74 3a 20 20 20 61 20 3c 3c  hiftLeft:   a <<
1b9cd 3d 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20  = b;    break;. 
1b9ce 20 20 20 64 65 66 61 75 6c 74 3a 20 20 61 73 73     default:  ass
1b9cf 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  ert( pOp->opcode
1b9d0 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20  ==OP_ShiftRight 
1b9d1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
1b9d2 20 20 20 20 20 20 20 20 20 20 20 20 61 20 3e 3e              a >>
1b9d3 3d 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20  = b;    break;. 
1b9d4 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d   }.  pOut->u.i =
1b9d5 20 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65   a;.  MemSetType
1b9d6 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1b9d7 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  nt);.  break;.}.
1b9d8 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 49  ./* Opcode: AddI
1b9d9 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  mm  P1 P2 * * *.
1b9da 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68 65 20 63  ** .** Add the c
1b9db 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f 20 74 68  onstant P2 to th
1b9dc 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1b9dd 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68 65 20 72  ter P1..** The r
1b9de 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20  esult is always 
1b9df 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a  an integer..**.*
1b9e0 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e 79 20 72  * To force any r
1b9e1 65 67 69 73 74 65 72 20 74 6f 20 62 65 20 61 6e  egister to be an
1b9e2 20 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 61   integer, just a
1b9e3 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  dd 0..*/.case OP
1b9e4 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20  _AddImm: {      
1b9e5 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a        /* in1 */.
1b9e6 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b9e7 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29  Integerify(pIn1)
1b9e8 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d  ;.  pIn1->u.i +=
1b9e9 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 62 72 65 61   pOp->p2;.  brea
1b9ea 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1b9eb 20 4d 75 73 74 42 65 49 6e 74 20 50 31 20 50 32   MustBeInt P1 P2
1b9ec 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f   * * *.** .** Fo
1b9ed 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  rce the value in
1b9ee 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1b9ef 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  be an integer.  
1b9f0 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  If the value.** 
1b9f1 69 6e 20 50 31 20 69 73 20 6e 6f 74 20 61 6e 20  in P1 is not an 
1b9f2 69 6e 74 65 67 65 72 20 61 6e 64 20 63 61 6e 6e  integer and cann
1b9f3 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20  ot be converted 
1b9f4 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a  into an integer.
1b9f5 2a 2a 20 77 69 74 68 6f 75 74 20 64 61 74 61 20  ** without data 
1b9f6 6c 6f 73 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20  loss, then jump 
1b9f7 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50  immediately to P
1b9f8 32 2c 20 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a  2, or if P2==0.*
1b9f9 2a 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54  * raise an SQLIT
1b9fa 45 5f 4d 49 53 4d 41 54 43 48 20 65 78 63 65 70  E_MISMATCH excep
1b9fb 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
1b9fc 5f 4d 75 73 74 42 65 49 6e 74 3a 20 7b 20 20 20  _MustBeInt: {   
1b9fd 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
1b9fe 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 70 70 6c 79  , in1 */.  apply
1b9ff 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53  Affinity(pIn1, S
1ba00 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
1ba01 43 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  C, encoding);.  
1ba02 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1ba03 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29   & MEM_Int)==0 )
1ba04 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  {.    if( pOp->p
1ba05 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  2==0 ){.      rc
1ba06 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54   = SQLITE_MISMAT
1ba07 43 48 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61  CH;.      goto a
1ba08 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1ba09 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  r;.    }else{.  
1ba0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1ba0b 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   - 1;.    }.  }e
1ba0c 6c 73 65 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54  lse{.    MemSetT
1ba0d 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45  ypeFlag(pIn1, ME
1ba0e 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72  M_Int);.  }.  br
1ba0f 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1ba10 65 3a 20 52 65 61 6c 41 66 66 69 6e 69 74 79 20  e: RealAffinity 
1ba11 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1ba12 20 49 66 20 72 65 67 69 73 74 65 72 20 50 31 20   If register P1 
1ba13 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72  holds an integer
1ba14 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
1ba15 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a   real value..**.
1ba16 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  ** This opcode i
1ba17 73 20 75 73 65 64 20 77 68 65 6e 20 65 78 74 72  s used when extr
1ba18 61 63 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  acting informati
1ba19 6f 6e 20 66 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e  on from a column
1ba1a 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 52 45 41   that.** has REA
1ba1b 4c 20 61 66 66 69 6e 69 74 79 2e 20 20 53 75 63  L affinity.  Suc
1ba1c 68 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20  h column values 
1ba1d 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 73 74 6f  may still be sto
1ba1e 72 65 64 20 61 73 0a 2a 2a 20 69 6e 74 65 67 65  red as.** intege
1ba1f 72 73 2c 20 66 6f 72 20 73 70 61 63 65 20 65 66  rs, for space ef
1ba20 66 69 63 69 65 6e 63 79 2c 20 62 75 74 20 61 66  ficiency, but af
1ba21 74 65 72 20 65 78 74 72 61 63 74 69 6f 6e 20 77  ter extraction w
1ba22 65 20 77 61 6e 74 20 74 68 65 6d 0a 2a 2a 20 74  e want them.** t
1ba23 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 72 65  o have only a re
1ba24 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73  al value..*/.cas
1ba25 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74  e OP_RealAffinit
1ba26 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  y: {            
1ba27 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a        /* in1 */.
1ba28 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67    if( pIn1->flag
1ba29 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
1ba2a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1ba2b 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a  mRealify(pIn1);.
1ba2c 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1ba2d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ba2e 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f  MIT_CAST./* Opco
1ba2f 64 65 3a 20 54 6f 54 65 78 74 20 50 31 20 2a 20  de: ToText P1 * 
1ba30 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63  * * *.**.** Forc
1ba31 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1ba32 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65  egister P1 to be
1ba33 20 74 65 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65   text..** If the
1ba34 20 76 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69   value is numeri
1ba35 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f  c, convert it to
1ba36 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 20   a string using 
1ba37 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e  the.** equivalen
1ba38 74 20 6f 66 20 70 72 69 6e 74 66 28 29 2e 20 20  t of printf().  
1ba39 42 6c 6f 62 20 76 61 6c 75 65 73 20 61 72 65 20  Blob values are 
1ba3a 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a  unchanged and.**
1ba3b 20 61 72 65 20 61 66 74 65 72 77 61 72 64 73 20   are afterwards 
1ba3c 73 69 6d 70 6c 79 20 69 6e 74 65 72 70 72 65 74  simply interpret
1ba3d 65 64 20 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a  ed as text..**.*
1ba3e 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69  * A NULL value i
1ba3f 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79  s not changed by
1ba40 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
1ba41 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e  It remains NULL.
1ba42 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65  .*/.case OP_ToTe
1ba43 78 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  xt: {           
1ba44 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1ba45 73 20 54 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e  s TK_TO_TEXT, in
1ba46 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d  1 */.  if( pIn1-
1ba47 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1ba48 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 61 73 73  l ) break;.  ass
1ba49 65 72 74 28 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d  ert( MEM_Str==(M
1ba4a 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20  EM_Blob>>3) );. 
1ba4b 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 3d 20   pIn1->flags |= 
1ba4c 28 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pIn1->flags&MEM
1ba4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 61 70 70  _Blob)>>3;.  app
1ba4e 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c  lyAffinity(pIn1,
1ba4f 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
1ba50 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 72  , encoding);.  r
1ba51 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70  c = ExpandBlob(p
1ba52 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20  In1);.  assert( 
1ba53 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1ba54 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  M_Str || db->mal
1ba55 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 70  locFailed );.  p
1ba56 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  In1->flags &= ~(
1ba57 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c  MEM_Int|MEM_Real
1ba58 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65  |MEM_Blob|MEM_Ze
1ba59 72 6f 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41  ro);.  UPDATE_MA
1ba5a 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29  X_BLOBSIZE(pIn1)
1ba5b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1ba5c 20 4f 70 63 6f 64 65 3a 20 54 6f 42 6c 6f 62 20   Opcode: ToBlob 
1ba5d 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1ba5e 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
1ba5f 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1ba60 74 6f 20 62 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a  to be a BLOB..**
1ba61 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73   If the value is
1ba62 20 6e 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72   numeric, conver
1ba63 74 20 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67  t it to a string
1ba64 20 66 69 72 73 74 2e 0a 2a 2a 20 53 74 72 69 6e   first..** Strin
1ba65 67 73 20 61 72 65 20 73 69 6d 70 6c 79 20 72 65  gs are simply re
1ba66 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 62  interpreted as b
1ba67 6c 6f 62 73 20 77 69 74 68 20 6e 6f 20 63 68 61  lobs with no cha
1ba68 6e 67 65 0a 2a 2a 20 74 6f 20 74 68 65 20 75 6e  nge.** to the un
1ba69 64 65 72 6c 79 69 6e 67 20 64 61 74 61 2e 0a 2a  derlying data..*
1ba6a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75  *.** A NULL valu
1ba6b 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64  e is not changed
1ba6c 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
1ba6d 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55  .  It remains NU
1ba6e 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  LL..*/.case OP_T
1ba6f 6f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20  oBlob: {        
1ba70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1ba71 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c  e as TK_TO_BLOB,
1ba72 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49   in1 */.  if( pI
1ba73 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1ba74 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20  Null ) break;.  
1ba75 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1ba76 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20   & MEM_Blob)==0 
1ba77 29 7b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69  ){.    applyAffi
1ba78 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54  nity(pIn1, SQLIT
1ba79 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f  E_AFF_TEXT, enco
1ba7a 64 69 6e 67 29 3b 0a 20 20 20 20 61 73 73 65 72  ding);.    asser
1ba7b 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  t( pIn1->flags &
1ba7c 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e   MEM_Str || db->
1ba7d 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
1ba7e 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
1ba7f 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f  ag(pIn1, MEM_Blo
1ba80 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  b);.  }else{.   
1ba81 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20   pIn1->flags &= 
1ba82 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e  ~(MEM_TypeMask&~
1ba83 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20  MEM_Blob);.  }. 
1ba84 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1ba85 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72  SIZE(pIn1);.  br
1ba86 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1ba87 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 31 20  e: ToNumeric P1 
1ba88 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f  * * * *.**.** Fo
1ba89 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  rce the value in
1ba8a 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1ba8b 62 65 20 6e 75 6d 65 72 69 63 20 28 65 69 74 68  be numeric (eith
1ba8c 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72  er an.** integer
1ba8d 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70   or a floating-p
1ba8e 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a  oint number.).**
1ba8f 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73   If the value is
1ba90 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74   text or blob, t
1ba91 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74  ry to convert it
1ba92 20 74 6f 20 61 6e 20 75 73 69 6e 67 20 74 68 65   to an using the
1ba93 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f  .** equivalent o
1ba94 66 20 61 74 6f 69 28 29 20 6f 72 20 61 74 6f 66  f atoi() or atof
1ba95 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69  () and store 0 i
1ba96 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72  f no such conver
1ba97 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73  sion .** is poss
1ba98 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  ible..**.** A NU
1ba99 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
1ba9a 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
1ba9b 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
1ba9c 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  ains NULL..*/.ca
1ba9d 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a  se OP_ToNumeric:
1ba9e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
1ba9f 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1baa0 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e  K_TO_NUMERIC, in
1baa1 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31  1 */.  if( (pIn1
1baa2 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 4e  ->flags & (MEM_N
1baa3 75 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  ull|MEM_Int|MEM_
1baa4 52 65 61 6c 29 29 3d 3d 30 20 29 7b 0a 20 20 20  Real))==0 ){.   
1baa5 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
1baa6 75 6d 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20  umerify(pIn1);. 
1baa7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1baa8 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1baa9 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20  MIT_CAST */../* 
1baaa 4f 70 63 6f 64 65 3a 20 54 6f 49 6e 74 20 50 31  Opcode: ToInt P1
1baab 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46   * * * *.**.** F
1baac 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69  orce the value i
1baad 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 65  n register P1 be
1baae 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66   an integer.  If
1baaf 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73  .** The value is
1bab0 20 63 75 72 72 65 6e 74 6c 79 20 61 20 72 65 61   currently a rea
1bab1 6c 20 6e 75 6d 62 65 72 2c 20 64 72 6f 70 20 69  l number, drop i
1bab2 74 73 20 66 72 61 63 74 69 6f 6e 61 6c 20 70 61  ts fractional pa
1bab3 72 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61  rt..** If the va
1bab4 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62  lue is text or b
1bab5 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76  lob, try to conv
1bab6 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74  ert it to an int
1bab7 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a  eger using the.*
1bab8 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20  * equivalent of 
1bab9 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65  atoi() and store
1baba 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f   0 if no such co
1babb 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73  nversion is poss
1babc 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  ible..**.** A NU
1babd 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
1babe 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
1babf 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
1bac0 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  ains NULL..*/.ca
1bac1 73 65 20 4f 50 5f 54 6f 49 6e 74 3a 20 7b 20 20  se OP_ToInt: {  
1bac2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bac3 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
1bac4 5f 49 4e 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69  _INT, in1 */.  i
1bac5 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
1bac6 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29  & MEM_Null)==0 )
1bac7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1bac8 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
1bac9 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  In1);.  }.  brea
1baca 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  k;.}..#ifndef SQ
1bacb 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f  LITE_OMIT_CAST./
1bacc 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 61 6c  * Opcode: ToReal
1bacd 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
1bace 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75  * Force the valu
1bacf 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1bad0 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e   to be a floatin
1bad1 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a  g point number..
1bad2 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75 65 20  ** If The value 
1bad3 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20  is currently an 
1bad4 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 72 74  integer, convert
1bad5 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76   it..** If the v
1bad6 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20  alue is text or 
1bad7 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e  blob, try to con
1bad8 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e  vert it to an in
1bad9 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a  teger using the.
1bada 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66  ** equivalent of
1badb 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72   atoi() and stor
1badc 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 63 68  e 0.0 if no such
1badd 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70   conversion is p
1bade 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  ossible..**.** A
1badf 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
1bae0 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
1bae1 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
1bae2 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
1bae3 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a  .case OP_ToReal:
1bae4 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
1bae5 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1bae6 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 20 2a  K_TO_REAL, in1 *
1bae7 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66  /.  if( (pIn1->f
1bae8 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1bae9 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1baea 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79  e3VdbeMemRealify
1baeb 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72  (pIn1);.  }.  br
1baec 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1baed 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53   SQLITE_OMIT_CAS
1baee 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  T */../* Opcode:
1baef 20 4c 74 20 50 31 20 50 32 20 50 33 20 50 34 20   Lt P1 P2 P3 P4 
1baf0 50 35 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  P5.**.** Compare
1baf1 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72   the values in r
1baf2 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50  egister P1 and P
1baf3 33 2e 20 20 49 66 20 72 65 67 28 50 33 29 3c 72  3.  If reg(P3)<r
1baf4 65 67 28 50 31 29 20 74 68 65 6e 0a 2a 2a 20 6a  eg(P1) then.** j
1baf5 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50  ump to address P
1baf6 32 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  2.  .**.** If th
1baf7 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  e SQLITE_JUMPIFN
1baf8 55 4c 4c 20 62 69 74 20 6f 66 20 50 35 20 69 73  ULL bit of P5 is
1baf9 20 73 65 74 20 61 6e 64 20 65 69 74 68 65 72 20   set and either 
1bafa 72 65 67 28 50 31 29 20 6f 72 0a 2a 2a 20 72 65  reg(P1) or.** re
1bafb 67 28 50 33 29 20 69 73 20 4e 55 4c 4c 20 74 68  g(P3) is NULL th
1bafc 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  en take the jump
1bafd 2e 20 20 49 66 20 74 68 65 20 53 51 4c 49 54 45  .  If the SQLITE
1bafe 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 0a 2a 2a 20  _JUMPIFNULL .** 
1baff 62 69 74 20 69 73 20 63 6c 65 61 72 20 74 68 65  bit is clear the
1bb00 6e 20 66 61 6c 6c 20 74 68 72 75 20 69 66 20 65  n fall thru if e
1bb01 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1bb02 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   NULL..**.** The
1bb03 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b   SQLITE_AFF_MASK
1bb04 20 70 6f 72 74 69 6f 6e 20 6f 66 20 50 35 20 6d   portion of P5 m
1bb05 75 73 74 20 62 65 20 61 6e 20 61 66 66 69 6e 69  ust be an affini
1bb06 74 79 20 63 68 61 72 61 63 74 65 72 20 2d 0a 2a  ty character -.*
1bb07 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  * SQLITE_AFF_TEX
1bb08 54 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  T, SQLITE_AFF_IN
1bb09 54 45 47 45 52 2c 20 61 6e 64 20 73 6f 20 66 6f  TEGER, and so fo
1bb0a 72 74 68 2e 20 41 6e 20 61 74 74 65 6d 70 74 20  rth. An attempt 
1bb0b 69 73 20 6d 61 64 65 20 0a 2a 2a 20 74 6f 20 63  is made .** to c
1bb0c 6f 65 72 63 65 20 62 6f 74 68 20 69 6e 70 75 74  oerce both input
1bb0d 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  s according to t
1bb0e 68 69 73 20 61 66 66 69 6e 69 74 79 20 62 65 66  his affinity bef
1bb0f 6f 72 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 61  ore the.** compa
1bb10 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 2e 20 49  rison is made. I
1bb11 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46  f the SQLITE_AFF
1bb12 5f 4d 41 53 4b 20 69 73 20 30 78 30 30 2c 20 74  _MASK is 0x00, t
1bb13 68 65 6e 20 6e 75 6d 65 72 69 63 0a 2a 2a 20 61  hen numeric.** a
1bb14 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 2e  ffinity is used.
1bb15 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61   Note that the a
1bb16 66 66 69 6e 69 74 79 20 63 6f 6e 76 65 72 73 69  ffinity conversi
1bb17 6f 6e 73 20 61 72 65 20 73 74 6f 72 65 64 0a 2a  ons are stored.*
1bb18 2a 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  * back into the 
1bb19 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73 20  input registers 
1bb1a 50 31 20 61 6e 64 20 50 33 2e 20 20 53 6f 20 74  P1 and P3.  So t
1bb1b 68 69 73 20 6f 70 63 6f 64 65 20 63 61 6e 20 63  his opcode can c
1bb1c 61 75 73 65 0a 2a 2a 20 70 65 72 73 69 73 74 65  ause.** persiste
1bb1d 6e 74 20 63 68 61 6e 67 65 73 20 74 6f 20 72 65  nt changes to re
1bb1e 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50  gisters P1 and P
1bb1f 33 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e  3..**.** Once an
1bb20 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61  y conversions ha
1bb21 76 65 20 74 61 6b 65 6e 20 70 6c 61 63 65 2c 20  ve taken place, 
1bb22 61 6e 64 20 6e 65 69 74 68 65 72 20 76 61 6c 75  and neither valu
1bb23 65 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a 2a 20 74  e is NULL, .** t
1bb24 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f  he values are co
1bb25 6d 70 61 72 65 64 2e 20 49 66 20 62 6f 74 68 20  mpared. If both 
1bb26 76 61 6c 75 65 73 20 61 72 65 20 62 6c 6f 62 73  values are blobs
1bb27 20 74 68 65 6e 20 6d 65 6d 63 6d 70 28 29 20 69   then memcmp() i
1bb28 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74  s.** used to det
1bb29 65 72 6d 69 6e 65 20 74 68 65 20 72 65 73 75 6c  ermine the resul
1bb2a 74 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72  ts of the compar
1bb2b 69 73 6f 6e 2e 20 20 49 66 20 62 6f 74 68 20 76  ison.  If both v
1bb2c 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 74 65 78  alues.** are tex
1bb2d 74 2c 20 74 68 65 6e 20 74 68 65 20 61 70 70 72  t, then the appr
1bb2e 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e  opriate collatin
1bb2f 67 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69  g function speci
1bb30 66 69 65 64 20 69 6e 0a 2a 2a 20 50 34 20 69 73  fied in.** P4 is
1bb31 20 20 75 73 65 64 20 74 6f 20 64 6f 20 74 68 65    used to do the
1bb32 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66   comparison.  If
1bb33 20 50 34 20 69 73 20 6e 6f 74 20 73 70 65 63 69   P4 is not speci
1bb34 66 69 65 64 20 74 68 65 6e 0a 2a 2a 20 6d 65 6d  fied then.** mem
1bb35 63 6d 70 28 29 20 69 73 20 75 73 65 64 20 74 6f  cmp() is used to
1bb36 20 63 6f 6d 70 61 72 65 20 74 65 78 74 20 73 74   compare text st
1bb37 72 69 6e 67 2e 20 20 49 66 20 62 6f 74 68 20 76  ring.  If both v
1bb38 61 6c 75 65 73 20 61 72 65 0a 2a 2a 20 6e 75 6d  alues are.** num
1bb39 65 72 69 63 2c 20 74 68 65 6e 20 61 20 6e 75 6d  eric, then a num
1bb3a 65 72 69 63 20 63 6f 6d 70 61 72 69 73 6f 6e 20  eric comparison 
1bb3b 69 73 20 75 73 65 64 2e 20 49 66 20 74 68 65 20  is used. If the 
1bb3c 74 77 6f 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72  two values.** ar
1bb3d 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 74  e of different t
1bb3e 79 70 65 73 2c 20 74 68 65 6e 20 6e 75 6d 62 65  ypes, then numbe
1bb3f 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  rs are considere
1bb40 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 73  d less than.** s
1bb41 74 72 69 6e 67 73 20 61 6e 64 20 73 74 72 69 6e  trings and strin
1bb42 67 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  gs are considere
1bb43 64 20 6c 65 73 73 20 74 68 61 6e 20 62 6c 6f 62  d less than blob
1bb44 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  s..**.** If the 
1bb45 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 62  SQLITE_STOREP2 b
1bb46 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c  it of P5 is set,
1bb47 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 75 6d   then do not jum
1bb48 70 2e 20 20 49 6e 73 74 65 61 64 2c 0a 2a 2a 20  p.  Instead,.** 
1bb49 73 74 6f 72 65 20 61 20 62 6f 6f 6c 65 61 6e 20  store a boolean 
1bb4a 72 65 73 75 6c 74 20 28 65 69 74 68 65 72 20 30  result (either 0
1bb4b 2c 20 6f 72 20 31 2c 20 6f 72 20 4e 55 4c 4c 29  , or 1, or NULL)
1bb4c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
1bb4d 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .*/./* Opcode: N
1bb4e 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  e P1 P2 P3 P4 P5
1bb4f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b  .**.** This work
1bb50 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20  s just like the 
1bb51 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74  Lt opcode except
1bb52 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69   that the jump i
1bb53 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68  s taken if.** th
1bb54 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 65  e operands in re
1bb55 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50  gisters P1 and P
1bb56 33 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 2e  3 are not equal.
1bb57 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63    See the Lt opc
1bb58 6f 64 65 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74  ode for.** addit
1bb59 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
1bb5a 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  n..*/./* Opcode:
1bb5b 20 45 71 20 50 31 20 50 32 20 50 33 20 50 34 20   Eq P1 P2 P3 P4 
1bb5c 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f  P5.**.** This wo
1bb5d 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  rks just like th
1bb5e 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65  e Lt opcode exce
1bb5f 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70  pt that the jump
1bb60 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20   is taken if.** 
1bb61 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20  the operands in 
1bb62 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64  registers P1 and
1bb63 20 50 33 20 61 72 65 20 65 71 75 61 6c 2e 0a 2a   P3 are equal..*
1bb64 2a 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63  * See the Lt opc
1bb65 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ode for addition
1bb66 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
1bb67 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65  */./* Opcode: Le
1bb68 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1bb69 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73  **.** This works
1bb6a 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c   just like the L
1bb6b 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20  t opcode except 
1bb6c 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73  that the jump is
1bb6d 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65   taken if.** the
1bb6e 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1bb6f 73 74 65 72 20 50 33 20 69 73 20 6c 65 73 73 20  ster P3 is less 
1bb70 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
1bb71 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a   the content of.
1bb72 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e 20  ** register P1. 
1bb73 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f   See the Lt opco
1bb74 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  de for additiona
1bb75 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
1bb76 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20  /./* Opcode: Gt 
1bb77 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1bb78 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  *.** This works 
1bb79 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74  just like the Lt
1bb7a 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74   opcode except t
1bb7b 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  hat the jump is 
1bb7c 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20  taken if.** the 
1bb7d 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73  content of regis
1bb7e 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74 65  ter P3 is greate
1bb7f 72 20 74 68 61 6e 20 74 68 65 20 63 6f 6e 74 65  r than the conte
1bb80 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65  nt of.** registe
1bb81 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c  r P1.  See the L
1bb82 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64  t opcode for add
1bb83 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
1bb84 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ion..*/./* Opcod
1bb85 65 3a 20 47 65 20 50 31 20 50 32 20 50 33 20 50  e: Ge P1 P2 P3 P
1bb86 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  4 P5.**.** This 
1bb87 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20  works just like 
1bb88 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78  the Lt opcode ex
1bb89 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75  cept that the ju
1bb8a 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a  mp is taken if.*
1bb8b 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
1bb8c 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20   register P3 is 
1bb8d 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
1bb8e 65 71 75 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e  equal to the con
1bb8f 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73  tent of.** regis
1bb90 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 65  ter P1.  See the
1bb91 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61   Lt opcode for a
1bb92 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1bb93 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ation..*/.case O
1bb94 50 5f 45 71 3a 20 20 20 20 20 20 20 20 20 20 20  P_Eq:           
1bb95 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1bb96 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c  K_EQ, jump, in1,
1bb97 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in3 */.case OP_
1bb98 4e 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  Ne:             
1bb99 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1bb9a 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69  NE, jump, in1, i
1bb9b 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74  n3 */.case OP_Lt
1bb9c 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
1bb9d 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54  /* same as TK_LT
1bb9e 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33  , jump, in1, in3
1bb9f 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20   */.case OP_Le: 
1bba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1bba1 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20   same as TK_LE, 
1bba2 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
1bba3 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20  /.case OP_Gt:   
1bba4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1bba5 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75  ame as TK_GT, ju
1bba6 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
1bba7 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20  case OP_Ge: {   
1bba8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1bba9 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70  e as TK_GE, jump
1bbaa 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 20 20  , in1, in3 */.  
1bbab 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 69 6e 74  int flags;.  int
1bbac 20 72 65 73 3b 0a 20 20 63 68 61 72 20 61 66 66   res;.  char aff
1bbad 69 6e 69 74 79 3b 0a 0a 20 20 66 6c 61 67 73 20  inity;..  flags 
1bbae 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 7c 70 49  = pIn1->flags|pI
1bbaf 6e 33 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 69 66  n3->flags;..  if
1bbb0 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c  ( flags&MEM_Null
1bbb1 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 65 69   ){.    /* If ei
1bbb2 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
1bbb3 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 65  NULL then the re
1bbb4 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 4e  sult is always N
1bbb5 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  ULL..    ** The 
1bbb6 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66  jump is taken if
1bbb7 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50   the SQLITE_JUMP
1bbb8 49 46 4e 55 4c 4c 20 62 69 74 20 69 73 20 73 65  IFNULL bit is se
1bbb9 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  t..    */.    if
1bbba 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49  ( pOp->p5 & SQLI
1bbbb 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20  TE_STOREP2 ){.  
1bbbc 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61      pOut = &p->a
1bbbd 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1bbbe 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
1bbbf 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c  ag(pOut, MEM_Nul
1bbc0 6c 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54  l);.      REGIST
1bbc1 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32  ER_TRACE(pOp->p2
1bbc2 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 7d 65 6c  , pOut);.    }el
1bbc3 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26  se if( pOp->p5 &
1bbc4 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
1bbc5 4c 4c 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  LL ){.      pc =
1bbc6 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 20   pOp->p2-1;.    
1bbc7 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  }.    break;.  }
1bbc8 0a 0a 20 20 61 66 66 69 6e 69 74 79 20 3d 20 70  ..  affinity = p
1bbc9 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f  Op->p5 & SQLITE_
1bbca 41 46 46 5f 4d 41 53 4b 3b 0a 20 20 69 66 28 20  AFF_MASK;.  if( 
1bbcb 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20  affinity ){.    
1bbcc 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49  applyAffinity(pI
1bbcd 6e 31 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e  n1, affinity, en
1bbce 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 70 70  coding);.    app
1bbcf 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c  lyAffinity(pIn3,
1bbd0 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64   affinity, encod
1bbd1 69 6e 67 29 3b 0a 20 20 20 20 69 66 28 20 64 62  ing);.    if( db
1bbd2 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1bbd3 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1bbd4 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  }..  assert( pOp
1bbd5 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c  ->p4type==P4_COL
1bbd6 4c 53 45 51 20 7c 7c 20 70 4f 70 2d 3e 70 34 2e  LSEQ || pOp->p4.
1bbd7 70 43 6f 6c 6c 3d 3d 30 20 29 3b 0a 20 20 45 78  pColl==0 );.  Ex
1bbd8 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a  pandBlob(pIn1);.
1bbd9 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e    ExpandBlob(pIn
1bbda 33 29 3b 0a 20 20 72 65 73 20 3d 20 73 71 6c 69  3);.  res = sqli
1bbdb 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 49  te3MemCompare(pI
1bbdc 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e 70  n3, pIn1, pOp->p
1bbdd 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 73 77 69 74  4.pColl);.  swit
1bbde 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20  ch( pOp->opcode 
1bbdf 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 45  ){.    case OP_E
1bbe0 71 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3d  q:    res = res=
1bbe1 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  =0;     break;. 
1bbe2 20 20 20 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20     case OP_Ne:  
1bbe3 20 20 72 65 73 20 3d 20 72 65 73 21 3d 30 3b 20    res = res!=0; 
1bbe4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
1bbe5 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20 72 65  ase OP_Lt:    re
1bbe6 73 20 3d 20 72 65 73 3c 30 3b 20 20 20 20 20 20  s = res<0;      
1bbe7 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
1bbe8 4f 50 5f 4c 65 3a 20 20 20 20 72 65 73 20 3d 20  OP_Le:    res = 
1bbe9 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 65 61  res<=0;     brea
1bbea 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 47  k;.    case OP_G
1bbeb 74 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3e  t:    res = res>
1bbec 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  0;      break;. 
1bbed 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20     default:     
1bbee 20 20 72 65 73 20 3d 20 72 65 73 3e 3d 30 3b 20    res = res>=0; 
1bbef 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
1bbf0 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20    if( pOp->p5 & 
1bbf1 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29  SQLITE_STOREP2 )
1bbf2 7b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d  {.    pOut = &p-
1bbf3 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  >aMem[pOp->p2];.
1bbf4 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
1bbf5 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74  ag(pOut, MEM_Int
1bbf6 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69  );.    pOut->u.i
1bbf7 20 3d 20 72 65 73 3b 0a 20 20 20 20 52 45 47 49   = res;.    REGI
1bbf8 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1bbf9 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 7d 65 6c  p2, pOut);.  }el
1bbfa 73 65 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20  se if( res ){.  
1bbfb 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31    pc = pOp->p2-1
1bbfc 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1bbfd 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 65 72  ../* Opcode: Per
1bbfe 6d 75 74 61 74 69 6f 6e 20 2a 20 2a 20 2a 20 50  mutation * * * P
1bbff 34 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68  4 *.**.** Set th
1bc00 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73  e permutation us
1bc01 65 64 20 62 79 20 74 68 65 20 4f 50 5f 43 6f 6d  ed by the OP_Com
1bc02 70 61 72 65 20 6f 70 65 72 61 74 6f 72 20 74 6f  pare operator to
1bc03 20 62 65 20 74 68 65 20 61 72 72 61 79 0a 2a 2a   be the array.**
1bc04 20 6f 66 20 69 6e 74 65 67 65 72 73 20 69 6e 20   of integers in 
1bc05 50 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65  P4..**.** The pe
1bc06 72 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f 6e 6c  rmutation is onl
1bc07 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68  y valid until th
1bc08 65 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d 75 74  e next OP_Permut
1bc09 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72  ation, OP_Compar
1bc0a 65 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f  e,.** OP_Halt, o
1bc0b 72 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 20  r OP_ResultRow. 
1bc0c 20 54 79 70 69 63 61 6c 6c 79 20 74 68 65 20 4f   Typically the O
1bc0d 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 73 68  P_Permutation sh
1bc0e 6f 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20 69 6d  ould occur.** im
1bc0f 6d 65 64 69 61 74 65 6c 79 20 70 72 69 6f 72 20  mediately prior 
1bc10 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72  to the OP_Compar
1bc11 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 65  e..*/.case OP_Pe
1bc12 72 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20 20 61  rmutation: {.  a
1bc13 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1bc14 70 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41 59 20  pe==P4_INTARRAY 
1bc15 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1bc16 2d 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61 50 65  ->p4.ai );.  aPe
1bc17 72 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70 34 2e  rmute = pOp->p4.
1bc18 61 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  ai;.  break;.}..
1bc19 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d 70 61  /* Opcode: Compa
1bc1a 72 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  re P1 P2 P3 P4 *
1bc1b 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74  .**.** Compare t
1bc1c 6f 20 76 65 63 74 6f 72 73 20 6f 66 20 72 65 67  o vectors of reg
1bc1d 69 73 74 65 72 73 20 69 6e 20 72 65 67 28 50 31  isters in reg(P1
1bc1e 29 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31 29 20  )..reg(P1+P3-1) 
1bc1f 28 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f 6e 65  (all this.** one
1bc20 20 22 41 22 29 20 61 6e 64 20 69 6e 20 72 65 67   "A") and in reg
1bc21 28 50 32 29 2e 2e 72 65 67 28 50 32 2b 50 33 2d  (P2)..reg(P2+P3-
1bc22 31 29 20 28 22 42 22 29 2e 20 20 53 61 76 65 20  1) ("B").  Save 
1bc23 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
1bc24 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
1bc25 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 6e  for use by the n
1bc26 65 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e 73 74  ext OP_Jump inst
1bc27 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69  ruct..**.** P4 i
1bc28 73 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  s a KeyInfo stru
1bc29 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e  cture that defin
1bc2a 65 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  es collating seq
1bc2b 75 65 6e 63 65 73 20 61 6e 64 20 73 6f 72 74 0a  uences and sort.
1bc2c 2a 2a 20 6f 72 64 65 72 73 20 66 6f 72 20 74 68  ** orders for th
1bc2d 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54  e comparison.  T
1bc2e 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61  he permutation a
1bc2f 70 70 6c 69 65 73 20 74 6f 20 72 65 67 69 73 74  pplies to regist
1bc30 65 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68  ers.** only.  Th
1bc31 65 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d 65 6e  e KeyInfo elemen
1bc32 74 73 20 61 72 65 20 75 73 65 64 20 73 65 71 75  ts are used sequ
1bc33 65 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20  entially..**.** 
1bc34 54 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69  The comparison i
1bc35 73 20 61 20 73 6f 72 74 20 63 6f 6d 70 61 72 69  s a sort compari
1bc36 73 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 63 6f  son, so NULLs co
1bc37 6d 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a 2a 20  mpare equal,.** 
1bc38 4e 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 20 74  NULLs are less t
1bc39 68 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e 75 6d  han numbers, num
1bc3a 62 65 72 73 20 61 72 65 20 6c 65 73 73 20 74 68  bers are less th
1bc3b 61 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 61  an strings,.** a
1bc3c 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 6c  nd strings are l
1bc3d 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a  ess than blobs..
1bc3e 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d 70 61  */.case OP_Compa
1bc3f 72 65 3a 20 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  re: {.  int n = 
1bc40 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e 74 20 69  pOp->p3;.  int i
1bc41 2c 20 70 31 2c 20 70 32 3b 0a 20 20 63 6f 6e 73  , p1, p2;.  cons
1bc42 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49  t KeyInfo *pKeyI
1bc43 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b  nfo = pOp->p4.pK
1bc44 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72 74  eyInfo;.  assert
1bc45 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ( n>0 );.  asser
1bc46 74 28 20 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29  t( pKeyInfo!=0 )
1bc47 3b 0a 20 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31  ;.  p1 = pOp->p1
1bc48 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31 3e 30  ;.  assert( p1>0
1bc49 20 26 26 20 70 31 2b 6e 3c 3d 70 2d 3e 6e 4d 65   && p1+n<=p->nMe
1bc4a 6d 2b 31 20 29 3b 0a 20 20 70 32 20 3d 20 70 4f  m+1 );.  p2 = pO
1bc4b 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28  p->p2;.  assert(
1bc4c 20 70 32 3e 30 20 26 26 20 70 32 2b 6e 3c 3d 70   p2>0 && p2+n<=p
1bc4d 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 66 6f  ->nMem+1 );.  fo
1bc4e 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29  r(i=0; i<n; i++)
1bc4f 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20  {.    int idx = 
1bc50 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d  aPermute ? aPerm
1bc51 75 74 65 5b 69 5d 20 3a 20 69 3b 0a 20 20 20 20  ute[i] : i;.    
1bc52 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
1bc53 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20     /* Collating 
1bc54 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 20  sequence to use 
1bc55 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a  on this term */.
1bc56 20 20 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20      int bRev;   
1bc57 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
1bc58 6f 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f  or DESCENDING so
1bc59 72 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20  rt order */.    
1bc5a 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1bc5b 31 2b 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b  1+idx, &p->aMem[
1bc5c 70 31 2b 69 64 78 5d 29 3b 0a 20 20 20 20 52 45  p1+idx]);.    RE
1bc5d 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b  GISTER_TRACE(p2+
1bc5e 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 70 32  idx, &p->aMem[p2
1bc5f 2b 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65  +idx]);.    asse
1bc60 72 74 28 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e  rt( i<pKeyInfo->
1bc61 6e 46 69 65 6c 64 20 29 3b 0a 20 20 20 20 70 43  nField );.    pC
1bc62 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  oll = pKeyInfo->
1bc63 61 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 62 52  aColl[i];.    bR
1bc64 65 76 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ev = pKeyInfo->a
1bc65 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20  SortOrder[i];.  
1bc66 20 20 69 43 6f 6d 70 61 72 65 20 3d 20 73 71 6c    iCompare = sql
1bc67 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26  ite3MemCompare(&
1bc68 70 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d 2c  p->aMem[p1+idx],
1bc69 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78   &p->aMem[p2+idx
1bc6a 5d 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69  ], pColl);.    i
1bc6b 66 28 20 69 43 6f 6d 70 61 72 65 20 29 7b 0a 20  f( iCompare ){. 
1bc6c 20 20 20 20 20 69 66 28 20 62 52 65 76 20 29 20       if( bRev ) 
1bc6d 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d  iCompare = -iCom
1bc6e 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61  pare;.      brea
1bc6f 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  k;.    }.  }.  a
1bc70 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62  Permute = 0;.  b
1bc71 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1bc72 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50  de: Jump P1 P2 P
1bc73 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
1bc74 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74   to the instruct
1bc75 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50  ion at address P
1bc76 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70  1, P2, or P3 dep
1bc77 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
1bc78 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74  r.** in the most
1bc79 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61   recent OP_Compa
1bc7a 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  re instruction t
1bc7b 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73  he P1 vector was
1bc7c 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71   less than.** eq
1bc7d 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
1bc7e 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76  er than the P2 v
1bc7f 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76  ector, respectiv
1bc80 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ely..*/.case OP_
1bc81 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20  Jump: {         
1bc82 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
1bc83 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20   if( iCompare<0 
1bc84 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1bc85 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65  >p1 - 1;.  }else
1bc86 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30   if( iCompare==0
1bc87 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
1bc88 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73  ->p2 - 1;.  }els
1bc89 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  e{.    pc = pOp-
1bc8a 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p3 - 1;.  }.  b
1bc8b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1bc8c 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33  de: And P1 P2 P3
1bc8d 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20   * *.**.** Take 
1bc8e 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20  the logical AND 
1bc8f 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
1bc90 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1bc91 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74  d P2 and.** writ
1bc92 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74  e the result int
1bc93 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  o register P3..*
1bc94 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50  *.** If either P
1bc95 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61  1 or P2 is 0 (fa
1bc96 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65  lse) then the re
1bc97 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69  sult is 0 even i
1bc98 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69  f.** the other i
1bc99 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41  nput is NULL.  A
1bc9a 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f   NULL and true o
1bc9b 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65  r two NULLs give
1bc9c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75  .** a NULL outpu
1bc9d 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  t..*/./* Opcode:
1bc9e 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a   Or P1 P2 P3 * *
1bc9f 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20  .**.** Take the 
1bca0 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68  logical OR of th
1bca1 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1bca2 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
1bca3 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  nd.** store the 
1bca4 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74  answer in regist
1bca5 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P3..**.** If 
1bca6 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20  either P1 or P2 
1bca7 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65  is nonzero (true
1bca8 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  ) then the resul
1bca9 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a  t is 1 (true).**
1bcaa 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68   even if the oth
1bcab 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1bcac 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61  .  A NULL and fa
1bcad 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73  lse or two NULLs
1bcae 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20  .** give a NULL 
1bcaf 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20  output..*/.case 
1bcb0 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20  OP_And:         
1bcb1 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1bcb2 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32  TK_AND, in1, in2
1bcb3 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
1bcb4 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20  P_Or: {         
1bcb5 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1bcb6 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  K_OR, in1, in2, 
1bcb7 6f 75 74 33 20 2a 2f 0a 20 20 69 6e 74 20 76 31  out3 */.  int v1
1bcb8 2c 20 76 32 3b 20 20 20 20 2f 2a 20 30 3d 3d 46  , v2;    /* 0==F
1bcb9 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
1bcba 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
1bcbb 4c 20 2a 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31  L */..  if( pIn1
1bcbc 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1bcbd 6c 6c 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 32  ll ){.    v1 = 2
1bcbe 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76  ;.  }else{.    v
1bcbf 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  1 = sqlite3VdbeI
1bcc0 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30  ntValue(pIn1)!=0
1bcc1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32  ;.  }.  if( pIn2
1bcc2 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1bcc3 6c 6c 20 29 7b 0a 20 20 20 20 76 32 20 3d 20 32  ll ){.    v2 = 2
1bcc4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76  ;.  }else{.    v
1bcc5 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  2 = sqlite3VdbeI
1bcc6 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d 30  ntValue(pIn2)!=0
1bcc7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d  ;.  }.  if( pOp-
1bcc8 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20  >opcode==OP_And 
1bcc9 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ){.    static co
1bcca 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1bccb 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20  r and_logic[] = 
1bccc 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c  { 0, 0, 0, 0, 1,
1bccd 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20   2, 0, 2, 2 };. 
1bcce 20 20 20 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69     v1 = and_logi
1bccf 63 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 65  c[v1*3+v2];.  }e
1bcd0 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20  lse{.    static 
1bcd1 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1bcd2 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d  har or_logic[] =
1bcd3 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31   { 0, 1, 2, 1, 1
1bcd4 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a  , 1, 2, 1, 2 };.
1bcd5 20 20 20 20 76 31 20 3d 20 6f 72 5f 6c 6f 67 69      v1 = or_logi
1bcd6 63 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 0a  c[v1*3+v2];.  }.
1bcd7 20 20 69 66 28 20 76 31 3d 3d 32 20 29 7b 0a 20    if( v1==2 ){. 
1bcd8 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1bcd9 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c  g(pOut, MEM_Null
1bcda 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1bcdb 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 31 3b 0a  pOut->u.i = v1;.
1bcdc 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
1bcdd 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74  ag(pOut, MEM_Int
1bcde 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1bcdf 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  }../* Opcode: No
1bce0 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  t P1 P2 * * *.**
1bce1 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68  .** Interpret th
1bce2 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1bce3 74 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f 6c  ter P1 as a bool
1bce4 65 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f 72  ean value.  Stor
1bce5 65 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61 6e  e the.** boolean
1bce6 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20 72   complement in r
1bce7 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20  egister P2.  If 
1bce8 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1bce9 69 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a 20  ister P1 is .** 
1bcea 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55 4c  NULL, then a NUL
1bceb 4c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 50  L is stored in P
1bcec 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f  2..*/.case OP_No
1bced 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  t: {            
1bcee 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1bcef 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20 20  K_NOT, in1 */.  
1bcf0 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
1bcf1 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20  pOp->p2];.  if( 
1bcf2 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1bcf3 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71  M_Null ){.    sq
1bcf4 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
1bcf5 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c  ull(pOut);.  }el
1bcf6 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  se{.    sqlite3V
1bcf7 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70  dbeMemSetInt64(p
1bcf8 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 62  Out, !sqlite3Vdb
1bcf9 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29  eIntValue(pIn1))
1bcfa 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1bcfb 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74  ../* Opcode: Bit
1bcfc 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Not P1 P2 * * *.
1bcfd 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  **.** Interpret 
1bcfe 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1bcff 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e  egister P1 as an
1bd00 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 65   integer.  Store
1bd01 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d   the.** ones-com
1bd02 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 50  plement of the P
1bd03 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67  1 value into reg
1bd04 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 31  ister P2.  If P1
1bd05 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c   holds.** a NULL
1bd06 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e 55   then store a NU
1bd07 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73  LL in P2..*/.cas
1bd08 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20  e OP_BitNot: {  
1bd09 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1bd0a 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c  me as TK_BITNOT,
1bd0b 20 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d   in1 */.  pOut =
1bd0c 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1bd0d 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e  2];.  if( pIn1->
1bd0e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1bd0f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1bd10 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f  dbeMemSetNull(pO
1bd11 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ut);.  }else{.  
1bd12 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1bd13 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 7e  SetInt64(pOut, ~
1bd14 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1bd15 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a  lue(pIn1));.  }.
1bd16 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1bd17 70 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32 20  pcode: If P1 P2 
1bd18 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d  P3 * *.**.** Jum
1bd19 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76  p to P2 if the v
1bd1a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1bd1b 20 50 31 20 69 73 20 74 72 75 65 2e 20 20 54 68   P1 is true.  Th
1bd1c 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73  e value is.** is
1bd1d 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 65   considered true
1bd1e 20 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72 69   if it is numeri
1bd1f 63 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  c and non-zero. 
1bd20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a   If the value.**
1bd21 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74   in P1 is NULL t
1bd22 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d  hen take the jum
1bd23 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e  p if P3 is true.
1bd24 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .*/./* Opcode: I
1bd25 66 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a 20  fNot P1 P2 P3 * 
1bd26 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20  *.**.** Jump to 
1bd27 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  P2 if the value 
1bd28 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69  in register P1 i
1bd29 73 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76 61  s False.  The va
1bd2a 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e  lue is.** is con
1bd2b 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20  sidered true if 
1bd2c 69 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69 63  it has a numeric
1bd2d 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e 20   value of zero. 
1bd2e 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a   If the value.**
1bd2f 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74   in P1 is NULL t
1bd30 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d  hen take the jum
1bd31 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e  p if P3 is true.
1bd32 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a 20  .*/.case OP_If: 
1bd33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bd34 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  /* jump, in1 */.
1bd35 63 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b  case OP_IfNot: {
1bd36 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a              /* j
1bd37 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e  ump, in1 */.  in
1bd38 74 20 63 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d  t c;.  if( pIn1-
1bd39 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1bd3a 6c 20 29 7b 0a 20 20 20 20 63 20 3d 20 70 4f 70  l ){.    c = pOp
1bd3b 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23  ->p3;.  }else{.#
1bd3c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1bd3d 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
1bd3e 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
1bd3f 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e  VdbeIntValue(pIn
1bd40 31 29 21 3d 30 3b 0a 23 65 6c 73 65 0a 20 20 20  1)!=0;.#else.   
1bd41 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65   c = sqlite3Vdbe
1bd42 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 21  RealValue(pIn1)!
1bd43 3d 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20  =0.0;.#endif.   
1bd44 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1bd45 3d 3d 4f 50 5f 49 66 4e 6f 74 20 29 20 63 20 3d  ==OP_IfNot ) c =
1bd46 20 21 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63   !c;.  }.  if( c
1bd47 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
1bd48 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72  ->p2-1;.  }.  br
1bd49 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1bd4a 65 3a 20 49 73 4e 75 6c 6c 20 50 31 20 50 32 20  e: IsNull P1 P2 
1bd4b 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d  P3 * *.**.** Jum
1bd4c 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76  p to P2 if the v
1bd4d 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1bd4e 20 50 31 20 69 73 20 4e 55 4c 4c 2e 20 20 49 66   P1 is NULL.  If
1bd4f 20 50 33 20 69 73 20 67 72 65 61 74 65 72 0a 2a   P3 is greater.*
1bd50 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  * than zero, the
1bd51 6e 20 63 68 65 63 6b 20 61 6c 6c 20 76 61 6c 75  n check all valu
1bd52 65 73 20 72 65 67 28 50 31 29 2c 20 72 65 67 28  es reg(P1), reg(
1bd53 50 31 2b 31 29 2c 20 0a 2a 2a 20 72 65 67 28 50  P1+1), .** reg(P
1bd54 31 2b 32 29 2c 20 2e 2e 2e 2c 20 72 65 67 28 50  1+2), ..., reg(P
1bd55 31 2b 50 33 2d 31 29 2e 0a 2a 2f 0a 63 61 73 65  1+P3-1)..*/.case
1bd56 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20   OP_IsNull: {   
1bd57 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1bd58 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a   as TK_ISNULL, j
1bd59 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e  ump, in1 */.  in
1bd5a 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20  t n = pOp->p3;. 
1bd5b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1bd5c 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 31 3e 30  ==0 || pOp->p1>0
1bd5d 20 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 66   );.  do{.    if
1bd5e 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1bd5f 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b   MEM_Null)!=0 ){
1bd60 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
1bd61 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62  >p2 - 1;.      b
1bd62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1bd63 70 49 6e 31 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65  pIn1++;.  }while
1bd64 28 20 2d 2d 6e 20 3e 20 30 20 29 3b 0a 20 20 62  ( --n > 0 );.  b
1bd65 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1bd66 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31 20 50  de: NotNull P1 P
1bd67 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75  2 * * *.**.** Ju
1bd68 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20  mp to P2 if the 
1bd69 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1bd6a 72 20 50 31 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  r P1 is not NULL
1bd6b 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e  .  .*/.case OP_N
1bd6c 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20  otNull: {       
1bd6d 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1bd6e 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75 6d 70  TK_NOTNULL, jump
1bd6f 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28  , in1 */.  if( (
1bd70 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1bd71 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Null)==0 ){.  
1bd72 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1bd73 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1bd74 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
1bd75 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 2a 20 50  etNumColumns * P
1bd76 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68  2 * * *.**.** Th
1bd77 69 73 20 6f 70 63 6f 64 65 20 73 65 74 73 20 74  is opcode sets t
1bd78 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1bd79 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 63 75 72  umns for the cur
1bd7a 73 6f 72 20 6f 70 65 6e 65 64 20 62 79 20 74 68  sor opened by th
1bd7b 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69  e.** following i
1bd7c 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 50 32  nstruction to P2
1bd7d 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 4f 50 5f 53 65  ..**.** An OP_Se
1bd7e 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 73 20 6f  tNumColumns is o
1bd7f 6e 6c 79 20 75 73 65 66 75 6c 20 69 66 20 69 74  nly useful if it
1bd80 20 6f 63 63 75 72 73 20 69 6d 6d 65 64 69 61 74   occurs immediat
1bd81 65 6c 79 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6f  ely before .** o
1bd82 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
1bd83 69 6e 67 20 6f 70 63 6f 64 65 73 3a 0a 2a 2a 0a  ing opcodes:.**.
1bd84 2a 2a 20 20 20 20 20 4f 70 65 6e 52 65 61 64 0a  **     OpenRead.
1bd85 2a 2a 20 20 20 20 20 4f 70 65 6e 57 72 69 74 65  **     OpenWrite
1bd86 0a 2a 2a 20 20 20 20 20 4f 70 65 6e 50 73 65 75  .**     OpenPseu
1bd87 64 6f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  do.**.** If the 
1bd88 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65  OP_Column opcode
1bd89 20 69 73 20 74 6f 20 62 65 20 65 78 65 63 75 74   is to be execut
1bd8a 65 64 20 6f 6e 20 61 20 63 75 72 73 6f 72 2c 20  ed on a cursor, 
1bd8b 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 6f 70 63  then.** this opc
1bd8c 6f 64 65 20 6d 75 73 74 20 62 65 20 70 72 65 73  ode must be pres
1bd8d 65 6e 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ent immediately 
1bd8e 62 65 66 6f 72 65 20 74 68 65 20 6f 70 63 6f 64  before the opcod
1bd8f 65 20 74 68 61 74 0a 2a 2a 20 6f 70 65 6e 73 20  e that.** opens 
1bd90 74 68 65 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 23  the cursor..*/.#
1bd91 69 66 20 30 0a 63 61 73 65 20 4f 50 5f 53 65 74  if 0.case OP_Set
1bd92 4e 75 6d 43 6f 6c 75 6d 6e 73 3a 20 7b 0a 20 20  NumColumns: {.  
1bd93 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  break;.}.#endif.
1bd94 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75  ./* Opcode: Colu
1bd95 6d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  mn P1 P2 P3 P4 *
1bd96 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74  .**.** Interpret
1bd97 20 74 68 65 20 64 61 74 61 20 74 68 61 74 20 63   the data that c
1bd98 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20  ursor P1 points 
1bd99 74 6f 20 61 73 20 61 20 73 74 72 75 63 74 75 72  to as a structur
1bd9a 65 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a  e built using.**
1bd9b 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20   the MakeRecord 
1bd9c 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53  instruction.  (S
1bd9d 65 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72  ee the MakeRecor
1bd9e 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64  d opcode for add
1bd9f 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72  itional.** infor
1bda0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
1bda1 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64   format of the d
1bda2 61 74 61 2e 29 20 20 45 78 74 72 61 63 74 20 74  ata.)  Extract t
1bda3 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a  he P2-th column.
1bda4 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63  ** from this rec
1bda5 6f 72 64 2e 20 20 49 66 20 74 68 65 72 65 20 61  ord.  If there a
1bda6 72 65 20 6c 65 73 73 20 74 68 61 74 20 28 50 32  re less that (P2
1bda7 2b 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69  +1) .** values i
1bda8 6e 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78  n the record, ex
1bda9 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a  tract a NULL..**
1bdaa 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78  .** The value ex
1bdab 74 72 61 63 74 65 64 20 69 73 20 73 74 6f 72 65  tracted is store
1bdac 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
1bdad 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  ..**.** If the c
1bdae 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66  olumn contains f
1bdaf 65 77 65 72 20 74 68 61 6e 20 50 32 20 66 69 65  ewer than P2 fie
1bdb0 6c 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63  lds, then extrac
1bdb1 74 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a  t a NULL.  Or,.*
1bdb2 2a 20 69 66 20 74 68 65 20 50 34 20 61 72 67 75  * if the P4 argu
1bdb3 6d 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d  ment is a P4_MEM
1bdb4 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f   use the value o
1bdb5 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e  f the P4 argumen
1bdb6 74 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75  t as.** the resu
1bdb7 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  lt..*/.case OP_C
1bdb8 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 69 6e 74 20 70  olumn: {.  int p
1bdb9 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a  ayloadSize;   /*
1bdba 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1bdbb 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a   in the record *
1bdbc 2f 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70  /.  int p1 = pOp
1bdbd 2d 3e 70 31 3b 20 20 2f 2a 20 50 31 20 76 61 6c  ->p1;  /* P1 val
1bdbe 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65  ue of the opcode
1bdbf 20 2a 2f 0a 20 20 69 6e 74 20 70 32 20 3d 20 70   */.  int p2 = p
1bdc0 4f 70 2d 3e 70 32 3b 20 20 2f 2a 20 63 6f 6c 75  Op->p2;  /* colu
1bdc1 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74  mn number to ret
1bdc2 72 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43  rieve */.  VdbeC
1bdc3 75 72 73 6f 72 20 2a 70 43 20 3d 20 30 3b 2f 2a  ursor *pC = 0;/*
1bdc4 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
1bdc5 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63   */.  char *zRec
1bdc6 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
1bdc7 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20  ter to complete 
1bdc8 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20  record-data */. 
1bdc9 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1bdca 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65  ;   /* The BTree
1bdcb 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32   cursor */.  u32
1bdcc 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20   *aType;        
1bdcd 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64  /* aType[i] hold
1bdce 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79  s the numeric ty
1bdcf 70 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63  pe of the i-th c
1bdd0 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a  olumn */.  u32 *
1bdd1 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a  aOffset;      /*
1bdd2 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f   aOffset[i] is o
1bdd3 66 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f  ffset to start o
1bdd4 66 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20  f data for i-th 
1bdd5 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20  column */.  int 
1bdd6 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f  nField;        /
1bdd7 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * number of fiel
1bdd8 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ds in the record
1bdd9 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20   */.  int len;  
1bdda 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1bddb 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65  length of the se
1bddc 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f  rialized data fo
1bddd 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  r the column */.
1bdde 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
1bddf 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
1bde0 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a  nter */.  char *
1bde1 7a 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20  zData;       /* 
1bde2 50 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f  Part of the reco
1bde3 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64  rd being decoded
1bde4 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74   */.  Mem *pDest
1bde5 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72  ;        /* Wher
1bde6 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65  e to write the e
1bde7 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a  xtracted value *
1bde8 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20  /.  Mem sMem;   
1bde9 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74         /* For st
1bdea 6f 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  oring the record
1bdeb 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a   being decoded *
1bdec 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 4d 65  /..  memset(&sMe
1bded 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4d 65  m, 0, sizeof(sMe
1bdee 6d 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  m));.  assert( p
1bdef 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1bdf0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1bdf1 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d  3>0 && pOp->p3<=
1bdf2 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 44 65  p->nMem );.  pDe
1bdf3 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  st = &p->aMem[pO
1bdf4 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53 65 74  p->p3];.  MemSet
1bdf5 54 79 70 65 46 6c 61 67 28 70 44 65 73 74 2c 20  TypeFlag(pDest, 
1bdf6 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 2f 2a  MEM_Null);..  /*
1bdf7 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 74 73   This block sets
1bdf8 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 70 61   the variable pa
1bdf9 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65 20  yloadSize to be 
1bdfa 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
1bdfb 20 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 20 69   of.  ** bytes i
1bdfc 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20  n the record..  
1bdfd 2a 2a 0a 20 20 2a 2a 20 7a 52 65 63 20 69 73 20  **.  ** zRec is 
1bdfe 73 65 74 20 74 6f 20 62 65 20 74 68 65 20 63 6f  set to be the co
1bdff 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74  mplete text of t
1be00 68 65 20 72 65 63 6f 72 64 20 69 66 20 69 74 20  he record if it 
1be01 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20  is available..  
1be02 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  ** The complete 
1be03 72 65 63 6f 72 64 20 74 65 78 74 20 69 73 20 61  record text is a
1be04 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c 65 20  lways available 
1be05 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  for pseudo-table
1be06 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65  s.  ** If the re
1be07 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69  cord is stored i
1be08 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68 65 20  n a cursor, the 
1be09 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 20  complete record 
1be0a 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20  text.  ** might 
1be0b 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  be available in 
1be0c 74 68 65 20 20 70 43 2d 3e 61 52 6f 77 20 63 61  the  pC->aRow ca
1be0d 63 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67 68  che.  Or it migh
1be0e 74 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49  t not be..  ** I
1be0f 66 20 74 68 65 20 64 61 74 61 20 69 73 20 75 6e  f the data is un
1be10 61 76 61 69 6c 61 62 6c 65 2c 20 20 7a 52 65 63  available,  zRec
1be11 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
1be12 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c  .  **.  ** We al
1be13 73 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e  so compute the n
1be14 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1be15 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 20   in the record. 
1be16 20 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a 20 20   For cursors,.  
1be17 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
1be18 20 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72   columns is stor
1be19 65 64 20 69 6e 20 74 68 65 20 56 64 62 65 43 75  ed in the VdbeCu
1be1a 72 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65 6d  rsor.nField elem
1be1b 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 20 3d  ent..  */.  pC =
1be1c 20 70 2d 3e 61 70 43 73 72 5b 70 31 5d 3b 0a 20   p->apCsr[p1];. 
1be1d 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29   assert( pC!=0 )
1be1e 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
1be1f 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1be20 4c 45 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d  LE.  assert( pC-
1be21 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20  >pVtabCursor==0 
1be22 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20  );.#endif.  if( 
1be23 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29  pC->pCursor!=0 )
1be24 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63  {.    /* The rec
1be25 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ord is stored in
1be26 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20   a B-Tree */.   
1be27 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1be28 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43  eCursorMoveto(pC
1be29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
1be2a 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1be2b 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7a 52 65  o_error;.    zRe
1be2c 63 20 3d 20 30 3b 0a 20 20 20 20 70 43 72 73 72  c = 0;.    pCrsr
1be2d 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a   = pC->pCursor;.
1be2e 20 20 20 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c      if( pC->null
1be2f 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 70 61 79  Row ){.      pay
1be30 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20  loadSize = 0;.  
1be31 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e    }else if( pC->
1be32 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e  cacheStatus==p->
1be33 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20  cacheCtr ){.    
1be34 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20    payloadSize = 
1be35 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b  pC->payloadSize;
1be36 0a 20 20 20 20 20 20 7a 52 65 63 20 3d 20 28 63  .      zRec = (c
1be37 68 61 72 2a 29 70 43 2d 3e 61 52 6f 77 3b 0a 20  har*)pC->aRow;. 
1be38 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d     }else if( pC-
1be39 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  >isIndex ){.    
1be3a 20 20 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a    i64 payloadSiz
1be3b 65 36 34 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  e64;.      sqlit
1be3c 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70  e3BtreeKeySize(p
1be3d 43 72 73 72 2c 20 26 70 61 79 6c 6f 61 64 53 69  Crsr, &payloadSi
1be3e 7a 65 36 34 29 3b 0a 20 20 20 20 20 20 70 61 79  ze64);.      pay
1be3f 6c 6f 61 64 53 69 7a 65 20 3d 20 28 69 6e 74 29  loadSize = (int)
1be40 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20  payloadSize64;. 
1be41 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1be42 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
1be43 53 69 7a 65 28 70 43 72 73 72 2c 20 28 75 33 32  Size(pCrsr, (u32
1be44 20 2a 29 26 70 61 79 6c 6f 61 64 53 69 7a 65 29   *)&payloadSize)
1be45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 46 69 65  ;.    }.    nFie
1be46 6c 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c 64 3b  ld = pC->nField;
1be47 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
1be48 73 65 72 74 28 20 70 43 2d 3e 70 73 65 75 64 6f  sert( pC->pseudo
1be49 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 2f 2a 20  Table );.    /* 
1be4a 54 68 65 20 72 65 63 6f 72 64 20 69 73 20 74 68  The record is th
1be4b 65 20 73 6f 6c 65 20 65 6e 74 72 79 20 6f 66 20  e sole entry of 
1be4c 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a  a pseudo-table *
1be4d 2f 0a 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a  /.    payloadSiz
1be4e 65 20 3d 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20  e = pC->nData;. 
1be4f 20 20 20 7a 52 65 63 20 3d 20 70 43 2d 3e 70 44     zRec = pC->pD
1be50 61 74 61 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63  ata;.    pC->cac
1be51 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1be52 5f 53 54 41 4c 45 3b 0a 20 20 20 20 61 73 73 65  _STALE;.    asse
1be53 72 74 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d  rt( payloadSize=
1be54 3d 30 20 7c 7c 20 7a 52 65 63 21 3d 30 20 29 3b  =0 || zRec!=0 );
1be55 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43  .    nField = pC
1be56 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 43  ->nField;.    pC
1be57 72 73 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  rsr = 0;.  }..  
1be58 2f 2a 20 49 66 20 70 61 79 6c 6f 61 64 53 69 7a  /* If payloadSiz
1be59 65 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73  e is 0, then jus
1be5a 74 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a  t store a NULL *
1be5b 2f 0a 20 20 69 66 28 20 70 61 79 6c 6f 61 64 53  /.  if( payloadS
1be5c 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  ize==0 ){.    as
1be5d 73 65 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61  sert( pDest->fla
1be5e 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20  gs&MEM_Null );. 
1be5f 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d     goto op_colum
1be60 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28  n_out;.  }.  if(
1be61 20 70 61 79 6c 6f 61 64 53 69 7a 65 3e 64 62 2d   payloadSize>db-
1be62 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1be63 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
1be64 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
1be65 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
1be66 20 70 32 3c 6e 46 69 65 6c 64 20 29 3b 0a 0a 20   p2<nField );.. 
1be67 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72   /* Read and par
1be68 73 65 20 74 68 65 20 74 61 62 6c 65 20 68 65 61  se the table hea
1be69 64 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20  der.  Store the 
1be6a 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 70  results of the p
1be6b 61 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 74  arse.  ** into t
1be6c 68 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72  he record header
1be6d 20 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f 66   cache fields of
1be6e 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a   the cursor..  *
1be6f 2f 0a 20 20 61 54 79 70 65 20 3d 20 70 43 2d 3e  /.  aType = pC->
1be70 61 54 79 70 65 3b 0a 20 20 69 66 28 20 70 43 2d  aType;.  if( pC-
1be71 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d  >cacheStatus==p-
1be72 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20  >cacheCtr ){.   
1be73 20 61 4f 66 66 73 65 74 20 3d 20 70 43 2d 3e 61   aOffset = pC->a
1be74 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b  Offset;.  }else{
1be75 0a 20 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 20  .    u8 *zIdx;  
1be76 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
1be77 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  nto header */.  
1be78 20 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20    u8 *zEndHdr;  
1be79 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1be7a 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65   first byte afte
1be7b 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a  r the header */.
1be7c 20 20 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20      int offset; 
1be7d 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
1be7e 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a  nto the data */.
1be7f 20 20 20 20 69 6e 74 20 73 7a 48 64 72 53 7a 3b      int szHdrSz;
1be80 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1be81 74 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20  the header size 
1be82 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f  field at start o
1be83 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  f record */.    
1be84 69 6e 74 20 61 76 61 69 6c 20 3d 20 30 3b 20 20  int avail = 0;  
1be85 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1be86 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65  tes of available
1be87 20 64 61 74 61 20 2a 2f 0a 0a 20 20 20 20 61 73   data */..    as
1be88 73 65 72 74 28 61 54 79 70 65 29 3b 0a 20 20 20  sert(aType);.   
1be89 20 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61   pC->aOffset = a
1be8a 4f 66 66 73 65 74 20 3d 20 26 61 54 79 70 65 5b  Offset = &aType[
1be8b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d  nField];.    pC-
1be8c 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70  >payloadSize = p
1be8d 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20  ayloadSize;.    
1be8e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1be8f 3d 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a  = p->cacheCtr;..
1be90 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
1be91 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73  t how many bytes
1be92 20 61 72 65 20 69 6e 20 74 68 65 20 68 65 61 64   are in the head
1be93 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52  er */.    if( zR
1be94 65 63 20 29 7b 0a 20 20 20 20 20 20 7a 44 61 74  ec ){.      zDat
1be95 61 20 3d 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65  a = zRec;.    }e
1be96 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70  lse{.      if( p
1be97 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20  C->isIndex ){.  
1be98 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63        zData = (c
1be99 68 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65  har*)sqlite3Btre
1be9a 65 4b 65 79 46 65 74 63 68 28 70 43 72 73 72 2c  eKeyFetch(pCrsr,
1be9b 20 26 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20   &avail);.      
1be9c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a  }else{.        z
1be9d 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71  Data = (char*)sq
1be9e 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65  lite3BtreeDataFe
1be9f 74 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69  tch(pCrsr, &avai
1bea0 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  l);.      }.    
1bea1 20 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68    /* If KeyFetch
1bea2 28 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d  ()/DataFetch() m
1bea3 61 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68  anaged to get th
1bea4 65 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64  e entire payload
1bea5 2c 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20  ,.      ** save 
1bea6 74 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74  the payload in t
1bea7 68 65 20 70 43 2d 3e 61 52 6f 77 20 63 61 63 68  he pC->aRow cach
1bea8 65 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 73 61  e.  That will sa
1bea9 76 65 20 75 73 20 66 72 6f 6d 0a 20 20 20 20 20  ve us from.     
1beaa 20 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61   ** having to ma
1beab 6b 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 61  ke additional ca
1beac 6c 6c 73 20 74 6f 20 66 65 74 63 68 20 74 68 65  lls to fetch the
1bead 20 63 6f 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e   content portion
1beae 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   of.      ** the
1beaf 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a   record..      *
1beb0 2f 0a 20 20 20 20 20 20 69 66 28 20 61 76 61 69  /.      if( avai
1beb1 6c 3e 3d 70 61 79 6c 6f 61 64 53 69 7a 65 20 29  l>=payloadSize )
1beb2 7b 0a 20 20 20 20 20 20 20 20 7a 52 65 63 20 3d  {.        zRec =
1beb3 20 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20   zData;.        
1beb4 70 43 2d 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29  pC->aRow = (u8*)
1beb5 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c  zData;.      }el
1beb6 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e  se{.        pC->
1beb7 61 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 20 20  aRow = 0;.      
1beb8 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54  }.    }.    /* T
1beb9 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73  he following ass
1beba 65 72 74 20 69 73 20 74 72 75 65 20 69 6e 20 61  ert is true in a
1bebb 6c 6c 20 63 61 73 65 73 20 61 63 63 65 70 74 20  ll cases accept 
1bebc 77 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  when.    ** the 
1bebd 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
1bebe 73 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64  s been corrupted
1bebf 20 65 78 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20   externally..   
1bec0 20 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 7a   **    assert( z
1bec1 52 65 63 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e  Rec!=0 || avail>
1bec2 3d 70 61 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20  =payloadSize || 
1bec3 61 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20  avail>=9 ); */. 
1bec4 20 20 20 73 7a 48 64 72 53 7a 20 3d 20 67 65 74     szHdrSz = get
1bec5 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29 7a 44  Varint32((u8*)zD
1bec6 61 74 61 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20  ata, offset);.. 
1bec7 20 20 20 2f 2a 20 54 68 65 20 4b 65 79 46 65 74     /* The KeyFet
1bec8 63 68 28 29 20 6f 72 20 44 61 74 61 46 65 74 63  ch() or DataFetc
1bec9 68 28 29 20 61 62 6f 76 65 20 61 72 65 20 66 61  h() above are fa
1beca 73 74 20 61 6e 64 20 77 69 6c 6c 20 67 65 74 20  st and will get 
1becb 74 68 65 20 65 6e 74 69 72 65 0a 20 20 20 20 2a  the entire.    *
1becc 2a 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20  * record header 
1becd 69 6e 20 6d 6f 73 74 20 63 61 73 65 73 2e 20 20  in most cases.  
1bece 42 75 74 20 74 68 65 79 20 77 69 6c 6c 20 66 61  But they will fa
1becf 69 6c 20 74 6f 20 67 65 74 20 74 68 65 20 63 6f  il to get the co
1bed0 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 72 65  mplete.    ** re
1bed1 63 6f 72 64 20 68 65 61 64 65 72 20 69 66 20 74  cord header if t
1bed2 68 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72  he record header
1bed3 20 64 6f 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e   does not fit on
1bed4 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 0a 20   a single page. 
1bed5 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54     ** in the B-T
1bed6 72 65 65 2e 20 20 57 68 65 6e 20 74 68 61 74 20  ree.  When that 
1bed7 68 61 70 70 65 6e 73 2c 20 75 73 65 20 73 71 6c  happens, use sql
1bed8 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42  ite3VdbeMemFromB
1bed9 74 72 65 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a  tree() to.    **
1beda 20 61 63 71 75 69 72 65 20 74 68 65 20 63 6f 6d   acquire the com
1bedb 70 6c 65 74 65 20 68 65 61 64 65 72 20 74 65 78  plete header tex
1bedc 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  t..    */.    if
1bedd 28 20 21 7a 52 65 63 20 26 26 20 61 76 61 69 6c  ( !zRec && avail
1bede 3c 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20  <offset ){.     
1bedf 20 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b   sMem.flags = 0;
1bee0 0a 20 20 20 20 20 20 73 4d 65 6d 2e 64 62 20 3d  .      sMem.db =
1bee1 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   0;.      rc = s
1bee2 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
1bee3 6d 42 74 72 65 65 28 70 43 72 73 72 2c 20 30 2c  mBtree(pCrsr, 0,
1bee4 20 6f 66 66 73 65 74 2c 20 70 43 2d 3e 69 73 49   offset, pC->isI
1bee5 6e 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20  ndex, &sMem);.  
1bee6 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1bee7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1bee8 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f   goto op_column_
1bee9 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  out;.      }.   
1beea 20 20 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e     zData = sMem.
1beeb 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 6e  z;.    }.    zEn
1beec 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 7a 44  dHdr = (u8 *)&zD
1beed 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20  ata[offset];.   
1beee 20 7a 49 64 78 20 3d 20 28 75 38 20 2a 29 26 7a   zIdx = (u8 *)&z
1beef 44 61 74 61 5b 73 7a 48 64 72 53 7a 5d 3b 0a 0a  Data[szHdrSz];..
1bef0 20 20 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20      /* Scan the 
1bef1 68 65 61 64 65 72 20 61 6e 64 20 75 73 65 20 69  header and use i
1bef2 74 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65  t to fill in the
1bef3 20 61 54 79 70 65 5b 5d 20 61 6e 64 20 61 4f 66   aType[] and aOf
1bef4 66 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72  fset[].    ** ar
1bef5 72 61 79 73 2e 20 20 61 54 79 70 65 5b 69 5d 20  rays.  aType[i] 
1bef6 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65  will contain the
1bef7 20 74 79 70 65 20 69 6e 74 65 67 65 72 20 66 6f   type integer fo
1bef8 72 20 74 68 65 20 69 2d 74 68 0a 20 20 20 20 2a  r the i-th.    *
1bef9 2a 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 61 4f 66  * column and aOf
1befa 66 73 65 74 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e  fset[i] will con
1befb 74 61 69 6e 20 74 68 65 20 6f 66 66 73 65 74 20  tain the offset 
1befc 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69  from the beginni
1befd 6e 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  ng.    ** of the
1befe 20 72 65 63 6f 72 64 20 74 6f 20 74 68 65 20 73   record to the s
1beff 74 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  tart of the data
1bf00 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 6f   for the i-th co
1bf01 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  lumn.    */.    
1bf02 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 69 65 6c  for(i=0; i<nFiel
1bf03 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  d; i++){.      i
1bf04 66 28 20 7a 49 64 78 3c 7a 45 6e 64 48 64 72 20  f( zIdx<zEndHdr 
1bf05 29 7b 0a 20 20 20 20 20 20 20 20 61 4f 66 66 73  ){.        aOffs
1bf06 65 74 5b 69 5d 20 3d 20 6f 66 66 73 65 74 3b 0a  et[i] = offset;.
1bf07 20 20 20 20 20 20 20 20 7a 49 64 78 20 2b 3d 20          zIdx += 
1bf08 67 65 74 56 61 72 69 6e 74 33 32 28 7a 49 64 78  getVarint32(zIdx
1bf09 2c 20 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20  , aType[i]);.   
1bf0a 20 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 73       offset += s
1bf0b 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1bf0c 54 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 69 5d  TypeLen(aType[i]
1bf0d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1bf0e 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 20          /* If i 
1bf0f 69 73 20 6c 65 73 73 20 74 68 61 74 20 6e 46 69  is less that nFi
1bf10 65 6c 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20  eld, then there 
1bf11 61 72 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20  are less fields 
1bf12 69 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20  in this.        
1bf13 2a 2a 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53  ** record than S
1bf14 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64  etNumColumns ind
1bf15 69 63 61 74 65 64 20 74 68 65 72 65 20 61 72 65  icated there are
1bf16 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a   columns in the.
1bf17 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65          ** table
1bf18 2e 20 53 65 74 20 74 68 65 20 6f 66 66 73 65 74  . Set the offset
1bf19 20 66 6f 72 20 61 6e 79 20 65 78 74 72 61 20 63   for any extra c
1bf1a 6f 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65  olumns not prese
1bf1b 6e 74 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a  nt in.        **
1bf1c 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 30   the record to 0
1bf1d 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 63 6f 64  . This tells cod
1bf1e 65 20 62 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65  e below to store
1bf1f 20 61 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20   a NULL.        
1bf20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65  ** instead of de
1bf21 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61  serializing a va
1bf22 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63  lue from the rec
1bf23 6f 72 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ord..        */.
1bf24 20 20 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b          aOffset[
1bf25 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  i] = 0;.      }.
1bf26 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1bf27 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1bf28 26 73 4d 65 6d 29 3b 0a 20 20 20 20 73 4d 65 6d  &sMem);.    sMem
1bf29 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  .flags = MEM_Nul
1bf2a 6c 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  l;..    /* If we
1bf2b 20 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20   have read more 
1bf2c 68 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e  header data than
1bf2d 20 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69   was contained i
1bf2e 6e 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20  n the header,.  
1bf2f 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65    ** or if the e
1bf30 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66  nd of the last f
1bf31 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20  ield appears to 
1bf32 62 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  be past the end 
1bf33 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65  of the.    ** re
1bf34 63 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20  cord, or if the 
1bf35 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20  end of the last 
1bf36 66 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f  field appears to
1bf37 20 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65   be before the e
1bf38 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  nd.    ** of the
1bf39 20 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c   record (when al
1bf3a 6c 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74  l fields present
1bf3b 29 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20  ), then we must 
1bf3c 62 65 20 64 65 61 6c 69 6e 67 20 0a 20 20 20 20  be dealing .    
1bf3d 2a 2a 20 77 69 74 68 20 61 20 63 6f 72 72 75 70  ** with a corrup
1bf3e 74 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  t database..    
1bf3f 2a 2f 0a 20 20 20 20 69 66 28 20 7a 49 64 78 3e  */.    if( zIdx>
1bf40 7a 45 6e 64 48 64 72 20 7c 7c 20 6f 66 66 73 65  zEndHdr || offse
1bf41 74 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 0a 20  t>payloadSize . 
1bf42 20 20 20 20 7c 7c 20 28 7a 49 64 78 3d 3d 7a 45      || (zIdx==zE
1bf43 6e 64 48 64 72 20 26 26 20 6f 66 66 73 65 74 21  ndHdr && offset!
1bf44 3d 70 61 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b  =payloadSize) ){
1bf45 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1bf46 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1bf47 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63  .      goto op_c
1bf48 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d  olumn_out;.    }
1bf49 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74  .  }..  /* Get t
1bf4a 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d  he column inform
1bf4b 61 74 69 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65  ation. If aOffse
1bf4c 74 5b 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72  t[p2] is non-zer
1bf4d 6f 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65  o, then .  ** de
1bf4e 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61  serialize the va
1bf4f 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63  lue from the rec
1bf50 6f 72 64 2e 20 49 66 20 61 4f 66 66 73 65 74 5b  ord. If aOffset[
1bf51 70 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a  p2] is zero,.  *
1bf52 2a 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65  * then there are
1bf53 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c   not enough fiel
1bf54 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ds in the record
1bf55 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a   to satisfy the.
1bf56 20 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49    ** request.  I
1bf57 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74  n this case, set
1bf58 20 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20   the value NULL 
1bf59 6f 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69  or to P4 if P4 i
1bf5a 73 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72  s.  ** a pointer
1bf5b 20 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74   to a Mem object
1bf5c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66  ..  */.  if( aOf
1bf5d 66 73 65 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20  fset[p2] ){.    
1bf5e 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1bf5f 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28  TE_OK );.    if(
1bf60 20 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73   zRec ){.      s
1bf61 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1bf62 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 44 65  easeExternal(pDe
1bf63 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  st);.      sqlit
1bf64 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28  e3VdbeSerialGet(
1bf65 28 75 38 20 2a 29 26 7a 52 65 63 5b 61 4f 66 66  (u8 *)&zRec[aOff
1bf66 73 65 74 5b 70 32 5d 5d 2c 20 61 54 79 70 65 5b  set[p2]], aType[
1bf67 70 32 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20  p2], pDest);.   
1bf68 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 65   }else{.      le
1bf69 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  n = sqlite3VdbeS
1bf6a 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 61 54 79  erialTypeLen(aTy
1bf6b 70 65 5b 70 32 5d 29 3b 0a 20 20 20 20 20 20 73  pe[p2]);.      s
1bf6c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1bf6d 65 28 26 73 4d 65 6d 2c 20 70 44 65 73 74 29 3b  e(&sMem, pDest);
1bf6e 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1bf6f 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1bf70 72 65 65 28 70 43 72 73 72 2c 20 61 4f 66 66 73  ree(pCrsr, aOffs
1bf71 65 74 5b 70 32 5d 2c 20 6c 65 6e 2c 20 70 43 2d  et[p2], len, pC-
1bf72 3e 69 73 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29  >isIndex, &sMem)
1bf73 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
1bf74 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1bf75 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c       goto op_col
1bf76 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d  umn_out;.      }
1bf77 0a 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 73  .      zData = s
1bf78 4d 65 6d 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c  Mem.z;.      sql
1bf79 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
1bf7a 74 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 61 54  t((u8*)zData, aT
1bf7b 79 70 65 5b 70 32 5d 2c 20 70 44 65 73 74 29 3b  ype[p2], pDest);
1bf7c 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 65 73 74  .    }.    pDest
1bf7d 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67  ->enc = encoding
1bf7e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
1bf7f 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  f( pOp->p4type==
1bf80 50 34 5f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  P4_MEM ){.      
1bf81 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68  sqlite3VdbeMemSh
1bf82 61 6c 6c 6f 77 43 6f 70 79 28 70 44 65 73 74 2c  allowCopy(pDest,
1bf83 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d   pOp->p4.pMem, M
1bf84 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20  EM_Static);.    
1bf85 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
1bf86 65 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67  ert( pDest->flag
1bf87 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20  s&MEM_Null );.  
1bf88 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
1bf89 20 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20   we dynamically 
1bf8a 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20  allocated space 
1bf8b 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61  to hold the data
1bf8c 20 28 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71   (in the.  ** sq
1bf8d 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d  lite3VdbeMemFrom
1bf8e 42 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f  Btree() call abo
1bf8f 76 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65  ve) then transfe
1bf90 72 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61  r control of tha
1bf91 74 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c  t.  ** dynamical
1bf92 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61  ly allocated spa
1bf93 63 65 20 6f 76 65 72 20 74 6f 20 74 68 65 20 70  ce over to the p
1bf94 44 65 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a  Dest structure..
1bf95 20 20 2a 2a 20 54 68 69 73 20 70 72 65 76 65 6e    ** This preven
1bf96 74 73 20 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79  ts a memory copy
1bf97 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 4d 65  ..  */.  if( sMe
1bf98 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20  m.zMalloc ){.   
1bf99 20 61 73 73 65 72 74 28 20 73 4d 65 6d 2e 7a 3d   assert( sMem.z=
1bf9a 3d 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b  =sMem.zMalloc );
1bf9b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 70  .    assert( !(p
1bf9c 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45  Dest->flags & ME
1bf9d 4d 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73  M_Dyn) );.    as
1bf9e 73 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e 66  sert( !(pDest->f
1bf9f 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62  lags & (MEM_Blob
1bfa0 7c 4d 45 4d 5f 53 74 72 29 29 20 7c 7c 20 70 44  |MEM_Str)) || pD
1bfa1 65 73 74 2d 3e 7a 3d 3d 73 4d 65 6d 2e 7a 20 29  est->z==sMem.z )
1bfa2 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 66 6c 61  ;.    pDest->fla
1bfa3 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65  gs &= ~(MEM_Ephe
1bfa4 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20  m|MEM_Static);. 
1bfa5 20 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 20     pDest->flags 
1bfa6 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  |= MEM_Term;.   
1bfa7 20 70 44 65 73 74 2d 3e 7a 20 3d 20 73 4d 65 6d   pDest->z = sMem
1bfa8 2e 7a 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 7a  .z;.    pDest->z
1bfa9 4d 61 6c 6c 6f 63 20 3d 20 73 4d 65 6d 2e 7a 4d  Malloc = sMem.zM
1bfaa 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 63  alloc;.  }..  rc
1bfab 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
1bfac 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70  mMakeWriteable(p
1bfad 44 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d  Dest);..op_colum
1bfae 6e 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45 5f  n_out:.  UPDATE_
1bfaf 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65  MAX_BLOBSIZE(pDe
1bfb0 73 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  st);.  REGISTER_
1bfb1 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70  TRACE(pOp->p3, p
1bfb2 44 65 73 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  Dest);.  break;.
1bfb3 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66  }../* Opcode: Af
1bfb4 66 69 6e 69 74 79 20 50 31 20 50 32 20 2a 20 50  finity P1 P2 * P
1bfb5 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20  4 *.**.** Apply 
1bfb6 61 66 66 69 6e 69 74 69 65 73 20 74 6f 20 61 20  affinities to a 
1bfb7 72 61 6e 67 65 20 6f 66 20 50 32 20 72 65 67 69  range of P2 regi
1bfb8 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77  sters starting w
1bfb9 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34  ith P1..**.** P4
1bfba 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61   is a string tha
1bfbb 74 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65  t is P2 characte
1bfbc 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68  rs long. The nth
1bfbd 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68   character of th
1bfbe 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69  e.** string indi
1bfbf 63 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e  cates the column
1bfc0 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73   affinity that s
1bfc1 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f  hould be used fo
1bfc2 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d  r the nth.** mem
1bfc3 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 65 20  ory cell in the 
1bfc4 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  range..*/.case O
1bfc5 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b 0a 20 20  P_Affinity: {.  
1bfc6 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 20  char *zAffinity 
1bfc7 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 4d  = pOp->p4.z;.  M
1bfc8 65 6d 20 2a 70 44 61 74 61 30 20 3d 20 26 70 2d  em *pData0 = &p-
1bfc9 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
1bfca 20 20 4d 65 6d 20 2a 70 4c 61 73 74 20 3d 20 26    Mem *pLast = &
1bfcb 70 44 61 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31  pData0[pOp->p2-1
1bfcc 5d 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a  ];.  Mem *pRec;.
1bfcd 0a 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74  .  for(pRec=pDat
1bfce 61 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b  a0; pRec<=pLast;
1bfcf 20 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78   pRec++){.    Ex
1bfd0 70 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a  pandBlob(pRec);.
1bfd1 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74      applyAffinit
1bfd2 79 28 70 52 65 63 2c 20 7a 41 66 66 69 6e 69 74  y(pRec, zAffinit
1bfd3 79 5b 70 52 65 63 2d 70 44 61 74 61 30 5d 2c 20  y[pRec-pData0], 
1bfd4 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20  encoding);.  }. 
1bfd5 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1bfd6 63 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64  code: MakeRecord
1bfd7 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1bfd8 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32 20  *.** Convert P2 
1bfd9 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e  registers beginn
1bfda 69 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74 6f  ing with P1 into
1bfdb 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a   a single entry.
1bfdc 2a 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20  ** suitable for 
1bfdd 75 73 65 20 61 73 20 61 20 64 61 74 61 20 72 65  use as a data re
1bfde 63 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 61  cord in a databa
1bfdf 73 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 61  se table or as a
1bfe0 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e   key.** in an in
1bfe1 64 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 6c  dex.  The detail
1bfe2 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20  s of the format 
1bfe3 61 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20 61  are irrelevant a
1bfe4 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65  s long as.** the
1bfe5 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64   OP_Column opcod
1bfe6 65 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 65  e can decode the
1bfe7 20 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a 2a   record later..*
1bfe8 2a 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72 63  * Refer to sourc
1bfe9 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20  e code comments 
1bfea 66 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73 20  for the details 
1bfeb 6f 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a  of the record.**
1bfec 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 50   format..**.** P
1bfed 34 20 6d 61 79 20 62 65 20 61 20 73 74 72 69 6e  4 may be a strin
1bfee 67 20 74 68 61 74 20 69 73 20 50 32 20 63 68 61  g that is P2 cha
1bfef 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54  racters long.  T
1bff0 68 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 72  he nth character
1bff1 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e   of the.** strin
1bff2 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20  g indicates the 
1bff3 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
1bff4 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75  that should be u
1bff5 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a  sed for the nth.
1bff6 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  ** field of the 
1bff7 69 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a  index key..**.**
1bff8 20 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72 6f   The mapping fro
1bff9 6d 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61  m character to a
1bffa 66 66 69 6e 69 74 79 20 69 73 20 67 69 76 65 6e  ffinity is given
1bffb 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 41   by the SQLITE_A
1bffc 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 65  FF_.** macros de
1bffd 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49  fined in sqliteI
1bffe 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  nt.h..**.** If P
1bfff 34 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61  4 is NULL then a
1c000 6c 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73 20  ll index fields 
1c001 68 61 76 65 20 74 68 65 20 61 66 66 69 6e 69 74  have the affinit
1c002 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 20  y NONE..*/.case 
1c003 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b  OP_MakeRecord: {
1c004 0a 20 20 2f 2a 20 41 73 73 75 6d 69 6e 67 20 74  .  /* Assuming t
1c005 68 65 20 72 65 63 6f 72 64 20 63 6f 6e 74 61 69  he record contai
1c006 6e 73 20 4e 20 66 69 65 6c 64 73 2c 20 74 68 65  ns N fields, the
1c007 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 6c   record format l
1c008 6f 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b 65 20 74  ooks.  ** like t
1c009 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2d  his:.  **.  ** -
1c00a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c00b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c00c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c00d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c00e 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 7c 20 68  -------.  ** | h
1c00f 64 72 2d 73 69 7a 65 20 7c 20 74 79 70 65 20 30  dr-size | type 0
1c010 20 7c 20 74 79 70 65 20 31 20 7c 20 2e 2e 2e 20   | type 1 | ... 
1c011 7c 20 74 79 70 65 20 4e 2d 31 20 7c 20 64 61 74  | type N-1 | dat
1c012 61 30 20 7c 20 2e 2e 2e 20 7c 20 64 61 74 61 20  a0 | ... | data 
1c013 4e 2d 31 20 7c 20 0a 20 20 2a 2a 20 2d 2d 2d 2d  N-1 | .  ** ----
1c014 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c015 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c016 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c017 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c018 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44  ----.  **.  ** D
1c019 61 74 61 28 30 29 20 69 73 20 74 61 6b 65 6e 20  ata(0) is taken 
1c01a 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31  from register P1
1c01b 2e 20 20 44 61 74 61 28 31 29 20 63 6f 6d 65 73  .  Data(1) comes
1c01c 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50   from register P
1c01d 31 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20  1+1.  ** and so 
1c01e 66 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  froth..  **.  **
1c01f 20 45 61 63 68 20 74 79 70 65 20 66 69 65 6c 64   Each type field
1c020 20 69 73 20 61 20 76 61 72 69 6e 74 20 72 65 70   is a varint rep
1c021 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65  resenting the se
1c022 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65  rial type of the
1c023 20 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e   .  ** correspon
1c024 64 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e  ding data elemen
1c025 74 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 64  t (see sqlite3Vd
1c026 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e  beSerialType()).
1c027 20 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69   The.  ** hdr-si
1c028 7a 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f  ze field is also
1c029 20 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 20   a varint which 
1c02a 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 72  is the offset fr
1c02b 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  om the beginning
1c02c 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63  .  ** of the rec
1c02d 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 20  ord to data0..  
1c02e 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63  */.  u8 *zNewRec
1c02f 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ord;        /* A
1c030 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20   buffer to hold 
1c031 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65  the data for the
1c032 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20   new record */. 
1c033 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20   Mem *pRec;     
1c034 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
1c035 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75  ew record */.  u
1c036 36 34 20 6e 44 61 74 61 20 3d 20 30 3b 20 20 20  64 nData = 0;   
1c037 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1c038 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
1c039 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20   space */.  int 
1c03a 6e 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20  nHdr = 0;       
1c03b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c03c 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20  bytes of header 
1c03d 73 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e  space */.  i64 n
1c03e 42 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20  Byte = 0;       
1c03f 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20    /* Data space 
1c040 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69  required for thi
1c041 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  s record */.  in
1c042 74 20 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20  t nZero = 0;    
1c043 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c044 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20  f zero bytes at 
1c045 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72  the end of the r
1c046 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
1c047 56 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20  Varint;         
1c048 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1c049 79 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74  ytes in a varint
1c04a 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c   */.  u32 serial
1c04b 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20  _type;       /* 
1c04c 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20  Type field */.  
1c04d 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20  Mem *pData0;    
1c04e 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
1c04f 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62  field to be comb
1c050 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65  ined into the re
1c051 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  cord */.  Mem *p
1c052 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Last;           
1c053 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f   /* Last field o
1c054 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  f the record */.
1c055 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20    int nField;   
1c056 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1c057 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
1c058 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1c059 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b  char *zAffinity;
1c05a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66         /* The af
1c05b 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f  finity string fo
1c05c 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  r the record */.
1c05d 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61    int file_forma
1c05e 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65  t;       /* File
1c05f 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66   format to use f
1c060 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  or encoding */. 
1c061 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1c062 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
1c063 20 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63   used in zNewRec
1c064 6f 72 64 5b 5d 20 2a 2f 0a 0a 20 20 6e 46 69 65  ord[] */..  nFie
1c065 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  ld = pOp->p1;.  
1c066 7a 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d  zAffinity = pOp-
1c067 3e 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28  >p4.z;.  assert(
1c068 20 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f 70   nField>0 && pOp
1c069 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p2>0 && pOp->p
1c06a 32 2b 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65  2+nField<=p->nMe
1c06b 6d 2b 31 20 29 3b 0a 20 20 70 44 61 74 61 30 20  m+1 );.  pData0 
1c06c 3d 20 26 70 2d 3e 61 4d 65 6d 5b 6e 46 69 65 6c  = &p->aMem[nFiel
1c06d 64 5d 3b 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70  d];.  nField = p
1c06e 4f 70 2d 3e 70 32 3b 0a 20 20 70 4c 61 73 74 20  Op->p2;.  pLast 
1c06f 3d 20 26 70 44 61 74 61 30 5b 6e 46 69 65 6c 64  = &pData0[nField
1c070 2d 31 5d 3b 0a 20 20 66 69 6c 65 5f 66 6f 72 6d  -1];.  file_form
1c071 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 65  at = p->minWrite
1c072 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 2f  FileFormat;..  /
1c073 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
1c074 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74  he elements that
1c075 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74 68   will make up th
1c076 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67 75  e record to figu
1c077 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77 20  re.  ** out how 
1c078 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65  much space is re
1c079 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 6e  quired for the n
1c07a 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a  ew record..  */.
1c07b 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61    for(pRec=pData
1c07c 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20  0; pRec<=pLast; 
1c07d 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  pRec++){.    int
1c07e 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20 7a 41   len;.    if( zA
1c07f 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20  ffinity ){.     
1c080 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70   applyAffinity(p
1c081 52 65 63 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70  Rec, zAffinity[p
1c082 52 65 63 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63  Rec-pData0], enc
1c083 6f 64 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20  oding);.    }.  
1c084 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67    if( pRec->flag
1c085 73 26 4d 45 4d 5f 5a 65 72 6f 20 26 26 20 70 52  s&MEM_Zero && pR
1c086 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20  ec->n>0 ){.     
1c087 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45   sqlite3VdbeMemE
1c088 78 70 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b  xpandBlob(pRec);
1c089 0a 20 20 20 20 7d 0a 20 20 20 20 73 65 72 69 61  .    }.    seria
1c08a 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33  l_type = sqlite3
1c08b 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70  VdbeSerialType(p
1c08c 52 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74  Rec, file_format
1c08d 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c  );.    len = sql
1c08e 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
1c08f 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70  peLen(serial_typ
1c090 65 29 3b 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d  e);.    nData +=
1c091 20 6c 65 6e 3b 0a 20 20 20 20 6e 48 64 72 20 2b   len;.    nHdr +
1c092 3d 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c  = sqlite3VarintL
1c093 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b  en(serial_type);
1c094 0a 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66  .    if( pRec->f
1c095 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
1c096 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79  ){.      /* Only
1c097 20 70 75 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65   pure zero-fille
1c098 64 20 42 4c 4f 42 73 20 63 61 6e 20 62 65 20 69  d BLOBs can be i
1c099 6e 70 75 74 20 74 6f 20 74 68 69 73 20 4f 70 63  nput to this Opc
1c09a 6f 64 65 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65  ode..      ** We
1c09b 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c   do not allow bl
1c09c 6f 62 73 20 77 69 74 68 20 61 20 70 72 65 66 69  obs with a prefi
1c09d 78 20 61 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c  x and a zero-fil
1c09e 6c 65 64 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20  led tail. */.   
1c09f 20 20 20 6e 5a 65 72 6f 20 2b 3d 20 70 52 65 63     nZero += pRec
1c0a0 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d  ->u.nZero;.    }
1c0a1 65 6c 73 65 20 69 66 28 20 6c 65 6e 20 29 7b 0a  else if( len ){.
1c0a2 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d 20 30 3b        nZero = 0;
1c0a3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1c0a4 20 41 64 64 20 74 68 65 20 69 6e 69 74 69 61 6c   Add the initial
1c0a5 20 68 65 61 64 65 72 20 76 61 72 69 6e 74 20 61   header varint a
1c0a6 6e 64 20 74 6f 74 61 6c 20 74 68 65 20 73 69 7a  nd total the siz
1c0a7 65 20 2a 2f 0a 20 20 6e 48 64 72 20 2b 3d 20 6e  e */.  nHdr += n
1c0a8 56 61 72 69 6e 74 20 3d 20 73 71 6c 69 74 65 33  Varint = sqlite3
1c0a9 56 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72 29 3b  VarintLen(nHdr);
1c0aa 0a 20 20 69 66 28 20 6e 56 61 72 69 6e 74 3c 73  .  if( nVarint<s
1c0ab 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
1c0ac 6e 48 64 72 29 20 29 7b 0a 20 20 20 20 6e 48 64  nHdr) ){.    nHd
1c0ad 72 2b 2b 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65  r++;.  }.  nByte
1c0ae 20 3d 20 6e 48 64 72 2b 6e 44 61 74 61 2d 6e 5a   = nHdr+nData-nZ
1c0af 65 72 6f 3b 0a 20 20 69 66 28 20 6e 42 79 74 65  ero;.  if( nByte
1c0b0 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
1c0b1 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
1c0b2 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f   ){.    goto too
1c0b3 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  _big;.  }..  /* 
1c0b4 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 75  Make sure the ou
1c0b5 74 70 75 74 20 72 65 67 69 73 74 65 72 20 68 61  tput register ha
1c0b6 73 20 61 20 62 75 66 66 65 72 20 6c 61 72 67 65  s a buffer large
1c0b7 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f 72 65   enough to store
1c0b8 20 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 72   .  ** the new r
1c0b9 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70 75  ecord. The outpu
1c0ba 74 20 72 65 67 69 73 74 65 72 20 28 70 4f 70 2d  t register (pOp-
1c0bb 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c 6f  >p3) is not allo
1c0bc 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20 6f  wed to.  ** be o
1c0bd 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20  ne of the input 
1c0be 72 65 67 69 73 74 65 72 73 20 28 62 65 63 61 75  registers (becau
1c0bf 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  se the following
1c0c0 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73 71   call to.  ** sq
1c0c1 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
1c0c2 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65 72  () could clobber
1c0c3 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f 72   the value befor
1c0c4 65 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a 20  e it is used).. 
1c0c5 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
1c0c6 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c 7c  p->p3<pOp->p1 ||
1c0c7 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70   pOp->p3>=pOp->p
1c0c8 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 70  1+pOp->p2 );.  p
1c0c9 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Out = &p->aMem[p
1c0ca 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 73  Op->p3];.  if( s
1c0cb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
1c0cc 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 6e 42 79  w(pOut, (int)nBy
1c0cd 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f  te, 0) ){.    go
1c0ce 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
1c0cf 20 7a 4e 65 77 52 65 63 6f 72 64 20 3d 20 28 75   zNewRecord = (u
1c0d0 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a 0a 20 20  8 *)pOut->z;..  
1c0d1 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 63  /* Write the rec
1c0d2 6f 72 64 20 2a 2f 0a 20 20 69 20 3d 20 70 75 74  ord */.  i = put
1c0d3 56 61 72 69 6e 74 33 32 28 7a 4e 65 77 52 65 63  Varint32(zNewRec
1c0d4 6f 72 64 2c 20 6e 48 64 72 29 3b 0a 20 20 66 6f  ord, nHdr);.  fo
1c0d5 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70  r(pRec=pData0; p
1c0d6 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63  Rec<=pLast; pRec
1c0d7 2b 2b 29 7b 0a 20 20 20 20 73 65 72 69 61 6c 5f  ++){.    serial_
1c0d8 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64  type = sqlite3Vd
1c0d9 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 52 65  beSerialType(pRe
1c0da 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b  c, file_format);
1c0db 0a 20 20 20 20 69 20 2b 3d 20 70 75 74 56 61 72  .    i += putVar
1c0dc 69 6e 74 33 32 28 26 7a 4e 65 77 52 65 63 6f 72  int32(&zNewRecor
1c0dd 64 5b 69 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70  d[i], serial_typ
1c0de 65 29 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 69  e);      /* seri
1c0df 61 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a 20  al type */.  }. 
1c0e0 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30   for(pRec=pData0
1c0e1 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70  ; pRec<=pLast; p
1c0e2 52 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69  Rec++){  /* seri
1c0e3 61 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69  al data */.    i
1c0e4 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53   += sqlite3VdbeS
1c0e5 65 72 69 61 6c 50 75 74 28 26 7a 4e 65 77 52 65  erialPut(&zNewRe
1c0e6 63 6f 72 64 5b 69 5d 2c 20 28 69 6e 74 29 28 6e  cord[i], (int)(n
1c0e7 42 79 74 65 2d 69 29 2c 20 70 52 65 63 2c 66 69  Byte-i), pRec,fi
1c0e8 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a  le_format);.  }.
1c0e9 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 42 79    assert( i==nBy
1c0ea 74 65 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  te );..  assert(
1c0eb 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f   pOp->p3>0 && pO
1c0ec 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1c0ed 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69  ;.  pOut->n = (i
1c0ee 6e 74 29 6e 42 79 74 65 3b 0a 20 20 70 4f 75 74  nt)nByte;.  pOut
1c0ef 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c  ->flags = MEM_Bl
1c0f0 6f 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20  ob | MEM_Dyn;.  
1c0f1 70 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a  pOut->xDel = 0;.
1c0f2 20 20 69 66 28 20 6e 5a 65 72 6f 20 29 7b 0a 20    if( nZero ){. 
1c0f3 20 20 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72 6f     pOut->u.nZero
1c0f4 20 3d 20 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4f   = nZero;.    pO
1c0f5 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  ut->flags |= MEM
1c0f6 5f 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 70 4f 75  _Zero;.  }.  pOu
1c0f7 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  t->enc = SQLITE_
1c0f8 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73  UTF8;  /* In cas
1c0f9 65 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76  e the blob is ev
1c0fa 65 72 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  er converted to 
1c0fb 74 65 78 74 20 2a 2f 0a 20 20 52 45 47 49 53 54  text */.  REGIST
1c0fc 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33  ER_TRACE(pOp->p3
1c0fd 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 44 41 54  , pOut);.  UPDAT
1c0fe 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1c0ff 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1c100 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 75  ../* Opcode: Cou
1c101 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  nt P1 P2 * * *.*
1c102 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 6e  *.** Store the n
1c103 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
1c104 20 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c   (an integer val
1c105 75 65 29 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ue) in the table
1c106 20 6f 72 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 70   or index .** op
1c107 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20 50  ened by cursor P
1c108 31 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  1 in register P2
1c109 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1c10a 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55  TE_OMIT_BTREECOU
1c10b 4e 54 0a 63 61 73 65 20 4f 50 5f 43 6f 75 6e 74  NT.case OP_Count
1c10c 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6f  : {         /* o
1c10d 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1c10e 2f 0a 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a  /.  i64 nEntry;.
1c10f 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1c110 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  r = p->apCsr[pOp
1c111 2d 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a  ->p1]->pCursor;.
1c112 20 20 69 66 28 20 70 43 72 73 72 20 29 7b 0a 20    if( pCrsr ){. 
1c113 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1c114 74 72 65 65 43 6f 75 6e 74 28 70 43 72 73 72 2c  treeCount(pCrsr,
1c115 20 26 6e 45 6e 74 72 79 29 3b 0a 20 20 7d 65 6c   &nEntry);.  }el
1c116 73 65 7b 0a 20 20 20 20 6e 45 6e 74 72 79 20 3d  se{.    nEntry =
1c117 20 30 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e   0;.  }.  pOut->
1c118 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1c119 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 6e  .  pOut->u.i = n
1c11a 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a  Entry;.  break;.
1c11b 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63  }.#endif../* Opc
1c11c 6f 64 65 3a 20 53 74 61 74 65 6d 65 6e 74 20 50  ode: Statement P
1c11d 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
1c11e 42 65 67 69 6e 20 61 6e 20 69 6e 64 69 76 69 64  Begin an individ
1c11f 75 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 72  ual statement tr
1c120 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 63 68 20  ansaction which 
1c121 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c 61 72  is part of a lar
1c122 67 65 72 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  ger.** transacti
1c123 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65  on.  This is nee
1c124 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ded so that the 
1c125 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e  statement.** can
1c126 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   be rolled back 
1c127 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 77  after an error w
1c128 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f  ithout having to
1c129 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a   roll back the.*
1c12a 2a 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63  * entire transac
1c12b 74 69 6f 6e 2e 20 20 54 68 65 20 73 74 61 74 65  tion.  The state
1c12c 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1c12d 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61   will automatica
1c12e 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 77 68  lly.** commit wh
1c12f 65 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74  en the VDBE halt
1c130 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  s..**.** If the 
1c131 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1c132 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ion is currently
1c133 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d   in autocommit m
1c134 6f 64 65 20 28 74 68 61 74 20 0a 2a 2a 20 69 73  ode (that .** is
1c135 20 74 6f 20 73 61 79 2c 20 69 66 20 69 74 20 69   to say, if it i
1c136 73 20 69 6e 20 62 65 74 77 65 65 6e 20 42 45 47  s in between BEG
1c137 49 4e 20 61 6e 64 20 43 4f 4d 4d 49 54 29 0a 2a  IN and COMMIT).*
1c138 2a 20 61 6e 64 20 69 66 20 74 68 65 72 65 20 61  * and if there a
1c139 72 65 20 6e 6f 20 6f 74 68 65 72 20 61 63 74 69  re no other acti
1c13a 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e  ve statements on
1c13b 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
1c13c 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
1c13d 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 65 72  , then this oper
1c13e 61 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ation is a no-op
1c13f 2e 20 20 4e 6f 20 73 74 61 74 65 6d 65 6e 74 20  .  No statement 
1c140 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69  transaction.** i
1c141 73 20 6e 65 65 64 65 64 20 73 69 6e 63 65 20 61  s needed since a
1c142 6e 79 20 65 72 72 6f 72 20 63 61 6e 20 75 73 65  ny error can use
1c143 20 74 68 65 20 6e 6f 72 6d 61 6c 20 52 4f 4c 4c   the normal ROLL
1c144 42 41 43 4b 20 70 72 6f 63 65 73 73 20 74 6f 0a  BACK process to.
1c145 2a 2a 20 75 6e 64 6f 20 63 68 61 6e 67 65 73 2e  ** undo changes.
1c146 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 74 61 74  .**.** If a stat
1c147 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1c148 6e 20 69 73 20 73 74 61 72 74 65 64 2c 20 74 68  n is started, th
1c149 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a  en a statement j
1c14a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 77  ournal file.** w
1c14b 69 6c 6c 20 62 65 20 61 6c 6c 6f 63 61 74 65 64  ill be allocated
1c14c 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64   and initialized
1c14d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61 74  ..**.** The stat
1c14e 65 6d 65 6e 74 20 69 73 20 62 65 67 75 6e 20 6f  ement is begun o
1c14f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
1c150 69 6c 65 20 77 69 74 68 20 69 6e 64 65 78 20 50  ile with index P
1c151 31 2e 20 20 54 68 65 20 6d 61 69 6e 0a 2a 2a 20  1.  The main.** 
1c152 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
1c153 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20  s an index of 0 
1c154 61 6e 64 20 74 68 65 20 66 69 6c 65 20 75 73 65  and the file use
1c155 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20  d for temporary 
1c156 74 61 62 6c 65 73 0a 2a 2a 20 68 61 73 20 61 6e  tables.** has an
1c157 20 69 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2f 0a   index of 1..*/.
1c158 63 61 73 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e  case OP_Statemen
1c159 74 3a 20 7b 0a 20 20 69 66 28 20 64 62 2d 3e 61  t: {.  if( db->a
1c15a 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20  utoCommit==0 || 
1c15b 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
1c15c 74 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  t>1 ){.    int i
1c15d 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20   = pOp->p1;.    
1c15e 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20  Btree *pBt;.    
1c15f 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
1c160 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20  i<db->nDb );.   
1c161 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
1c162 5b 69 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20  [i].pBt!=0 );.  
1c163 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b    pBt = db->aDb[
1c164 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 73 65  i].pBt;.    asse
1c165 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
1c166 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29  IsInTrans(pBt) )
1c167 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
1c168 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1c169 3c 3c 69 29 29 21 3d 30 20 29 3b 0a 20 20 20 20  <<i))!=0 );.    
1c16a 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  if( p->iStatemen
1c16b 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73  t==0 ){.      as
1c16c 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65  sert( db->nState
1c16d 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e 6e  ment>=0 && db->n
1c16e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b 0a  Savepoint>=0 );.
1c16f 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65        db->nState
1c170 6d 65 6e 74 2b 2b 3b 20 0a 20 20 20 20 20 20 70  ment++; .      p
1c171 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 64  ->iStatement = d
1c172 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b 20  b->nSavepoint + 
1c173 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3b 0a  db->nStatement;.
1c174 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
1c175 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e  qlite3BtreeBegin
1c176 53 74 6d 74 28 70 42 74 2c 20 70 2d 3e 69 53 74  Stmt(pBt, p->iSt
1c177 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20  atement);.  }.  
1c178 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1c179 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 20 50  ode: Savepoint P
1c17a 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  1 * * P4 *.**.**
1c17b 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f   Open, release o
1c17c 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73  r rollback the s
1c17d 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62  avepoint named b
1c17e 79 20 70 61 72 61 6d 65 74 65 72 20 50 34 2c 20  y parameter P4, 
1c17f 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20  depending.** on 
1c180 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 31 2e  the value of P1.
1c181 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 73   To open a new s
1c182 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e  avepoint, P1==0.
1c183 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d   To release (com
1c184 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74  mit) an.** exist
1c185 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c 20 50  ing savepoint, P
1c186 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c  1==1, or to roll
1c187 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69 6e 67  back an existing
1c188 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32   savepoint P1==2
1c189 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76  ..*/.case OP_Sav
1c18a 65 70 6f 69 6e 74 3a 20 7b 0a 20 20 69 6e 74 20  epoint: {.  int 
1c18b 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  p1 = pOp->p1;.  
1c18c 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 4f  char *zName = pO
1c18d 70 2d 3e 70 34 2e 7a 3b 20 20 20 20 20 20 20 20  p->p4.z;        
1c18e 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 65   /* Name of save
1c18f 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 41  point */..  /* A
1c190 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 70  ssert that the p
1c191 31 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 76  1 parameter is v
1c192 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61 74 20  alid. Also that 
1c193 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f  if there is no o
1c194 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  pen.  ** transac
1c195 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72 65  tion, then there
1c196 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73   cannot be any s
1c197 61 76 65 70 6f 69 6e 74 73 2e 20 0a 20 20 2a 2f  avepoints. .  */
1c198 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70  .  assert( db->p
1c199 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20  Savepoint==0 || 
1c19a 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d  db->autoCommit==
1c19b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1c19c 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47  1==SAVEPOINT_BEG
1c19d 49 4e 7c 7c 70 31 3d 3d 53 41 56 45 50 4f 49 4e  IN||p1==SAVEPOIN
1c19e 54 5f 52 45 4c 45 41 53 45 7c 7c 70 31 3d 3d 53  T_RELEASE||p1==S
1c19f 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
1c1a0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  K );.  assert( d
1c1a1 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c 7c  b->pSavepoint ||
1c1a2 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69   db->isTransacti
1c1a3 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29  onSavepoint==0 )
1c1a4 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68 65 63  ;.  assert( chec
1c1a5 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28  kSavepointCount(
1c1a6 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20 70 31  db) );..  if( p1
1c1a7 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49  ==SAVEPOINT_BEGI
1c1a8 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d  N ){.    if( db-
1c1a9 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20  >writeVdbeCnt>0 
1c1aa 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 65  ){.      /* A ne
1c1ab 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e 6e  w savepoint cann
1c1ac 6f 74 20 62 65 20 63 72 65 61 74 65 64 20 69 66  ot be created if
1c1ad 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
1c1ae 65 20 77 72 69 74 65 20 0a 20 20 20 20 20 20 2a  e write .      *
1c1af 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28 69 2e  * statements (i.
1c1b0 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 72 69  e. open read/wri
1c1b1 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62  te incremental b
1c1b2 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20  lob handles)..  
1c1b3 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
1c1b4 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1c1b5 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1c1b6 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65  cannot open save
1c1b7 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 20 20  point - ".      
1c1b8 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74    "SQL statement
1c1b9 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b  s in progress");
1c1ba 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1c1bb 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c  TE_BUSY;.    }el
1c1bc 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e  se{.      int nN
1c1bd 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
1c1be 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20  len30(zName);.  
1c1bf 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70      Savepoint *p
1c1c0 4e 65 77 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43  New;..      /* C
1c1c1 72 65 61 74 65 20 61 20 6e 65 77 20 73 61 76 65  reate a new save
1c1c2 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 2e  point structure.
1c1c3 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 20 3d   */.      pNew =
1c1c4 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1c1c5 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 53  Raw(db, sizeof(S
1c1c6 61 76 65 70 6f 69 6e 74 29 2b 6e 4e 61 6d 65 2b  avepoint)+nName+
1c1c7 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e  1);.      if( pN
1c1c8 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e  ew ){.        pN
1c1c9 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  ew->zName = (cha
1c1ca 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20  r *)&pNew[1];.  
1c1cb 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65        memcpy(pNe
1c1cc 77 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c  w->zName, zName,
1c1cd 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 0a   nName+1);.    .
1c1ce 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
1c1cf 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 74  ere is no open t
1c1d0 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e  ransaction, then
1c1d1 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 61 20   mark this as a 
1c1d2 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20 20  special.        
1c1d3 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f 6e 20  ** "transaction 
1c1d4 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f 0a 20  savepoint". */. 
1c1d5 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61         if( db->a
1c1d6 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  utoCommit ){.   
1c1d7 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1c1d8 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  ommit = 0;.     
1c1d9 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 6e 73       db->isTrans
1c1da 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20  actionSavepoint 
1c1db 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 1;.        }el
1c1dc 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62  se{.          db
1c1dd 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a  ->nSavepoint++;.
1c1de 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 0a 20          }.    . 
1c1df 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20 74         /* Link t
1c1e0 68 65 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74  he new savepoint
1c1e1 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
1c1e2 73 65 20 68 61 6e 64 6c 65 27 73 20 6c 69 73 74  se handle's list
1c1e3 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65  . */.        pNe
1c1e4 77 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70  w->pNext = db->p
1c1e5 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20  Savepoint;.     
1c1e6 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e     db->pSavepoin
1c1e7 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20  t = pNew;.      
1c1e8 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  }.    }.  }else{
1c1e9 0a 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a  .    Savepoint *
1c1ea 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20  pSavepoint;.    
1c1eb 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 3d  int iSavepoint =
1c1ec 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64   0;..    /* Find
1c1ed 20 74 68 65 20 6e 61 6d 65 64 20 73 61 76 65 70   the named savep
1c1ee 6f 69 6e 74 2e 20 49 66 20 74 68 65 72 65 20 69  oint. If there i
1c1ef 73 20 6e 6f 20 73 75 63 68 20 73 61 76 65 70 6f  s no such savepo
1c1f0 69 6e 74 2c 20 74 68 65 6e 20 61 6e 0a 20 20 20  int, then an.   
1c1f1 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20   ** an error is 
1c1f2 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
1c1f3 75 73 65 72 2e 20 20 2a 2f 0a 20 20 20 20 66 6f  user.  */.    fo
1c1f4 72 28 0a 20 20 20 20 20 20 70 53 61 76 65 70 6f  r(.      pSavepo
1c1f5 69 6e 74 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69  int=db->pSavepoi
1c1f6 6e 74 3b 20 0a 20 20 20 20 20 20 70 53 61 76 65  nt; .      pSave
1c1f7 70 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 65 33  point && sqlite3
1c1f8 53 74 72 49 43 6d 70 28 70 53 61 76 65 70 6f 69  StrICmp(pSavepoi
1c1f9 6e 74 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  nt->zName, zName
1c1fa 29 3b 0a 20 20 20 20 20 20 70 53 61 76 65 70 6f  );.      pSavepo
1c1fb 69 6e 74 3d 70 53 61 76 65 70 6f 69 6e 74 2d 3e  int=pSavepoint->
1c1fc 70 4e 65 78 74 0a 20 20 20 20 29 7b 0a 20 20 20  pNext.    ){.   
1c1fd 20 20 20 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b     iSavepoint++;
1c1fe 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
1c1ff 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20  pSavepoint ){.  
1c200 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1c201 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1c202 2c 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 73  , db, "no such s
1c203 61 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c 20 7a  avepoint: %s", z
1c204 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 63 20  Name);.      rc 
1c205 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1c206 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 0a 20      }else if( . 
1c207 20 20 20 20 20 20 20 64 62 2d 3e 77 72 69 74 65         db->write
1c208 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 28 70 31  VdbeCnt>0 || (p1
1c209 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  ==SAVEPOINT_ROLL
1c20a 42 41 43 4b 20 26 26 20 64 62 2d 3e 61 63 74 69  BACK && db->acti
1c20b 76 65 56 64 62 65 43 6e 74 3e 31 29 20 0a 20 20  veVdbeCnt>1) .  
1c20c 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74    ){.      /* It
1c20d 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
1c20e 20 74 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d   to release (com
1c20f 6d 69 74 29 20 61 20 73 61 76 65 70 6f 69 6e 74  mit) a savepoint
1c210 20 69 66 20 74 68 65 72 65 20 61 72 65 20 0a 20   if there are . 
1c211 20 20 20 20 20 2a 2a 20 61 63 74 69 76 65 20 77       ** active w
1c212 72 69 74 65 20 73 74 61 74 65 6d 65 6e 74 73 2e  rite statements.
1c213 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   It is not possi
1c214 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20  ble to rollback 
1c215 61 20 73 61 76 65 70 6f 69 6e 74 0a 20 20 20 20  a savepoint.    
1c216 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72    ** if there ar
1c217 65 20 61 6e 79 20 61 63 74 69 76 65 20 73 74 61  e any active sta
1c218 74 65 6d 65 6e 74 73 20 61 74 20 61 6c 6c 2e 0a  tements at all..
1c219 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
1c21a 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1c21b 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1c21c 20 0a 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f   .        "canno
1c21d 74 20 25 73 20 73 61 76 65 70 6f 69 6e 74 20 2d  t %s savepoint -
1c21e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
1c21f 69 6e 20 70 72 6f 67 72 65 73 73 22 2c 0a 20 20  in progress",.  
1c220 20 20 20 20 20 20 28 70 31 3d 3d 53 41 56 45 50        (p1==SAVEP
1c221 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 3f 20  OINT_ROLLBACK ? 
1c222 22 72 6f 6c 6c 62 61 63 6b 22 3a 20 22 72 65 6c  "rollback": "rel
1c223 65 61 73 65 22 29 0a 20 20 20 20 20 20 29 3b 0a  ease").      );.
1c224 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1c225 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73  E_BUSY;.    }els
1c226 65 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 65 74  e{..      /* Det
1c227 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
1c228 72 20 6e 6f 74 20 74 68 69 73 20 69 73 20 61 20  r not this is a 
1c229 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65  transaction save
1c22a 70 6f 69 6e 74 2e 20 49 66 20 73 6f 2c 0a 20 20  point. If so,.  
1c22b 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 69 73 20      ** and this 
1c22c 69 73 20 61 20 52 45 4c 45 41 53 45 20 63 6f 6d  is a RELEASE com
1c22d 6d 61 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 63  mand, then the c
1c22e 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
1c22f 6f 6e 20 0a 20 20 20 20 20 20 2a 2a 20 69 73 20  on .      ** is 
1c230 63 6f 6d 6d 69 74 74 65 64 2e 20 0a 20 20 20 20  committed. .    
1c231 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69    */.      int i
1c232 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70  sTransaction = p
1c233 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74  Savepoint->pNext
1c234 3d 3d 30 20 26 26 20 64 62 2d 3e 69 73 54 72 61  ==0 && db->isTra
1c235 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e  nsactionSavepoin
1c236 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 54  t;.      if( isT
1c237 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 70 31  ransaction && p1
1c238 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  ==SAVEPOINT_RELE
1c239 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ASE ){.        d
1c23a 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
1c23b 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  1;.        if( s
1c23c 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70  qlite3VdbeHalt(p
1c23d 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29  )==SQLITE_BUSY )
1c23e 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  {.          p->p
1c23f 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 20 20  c = pc;.        
1c240 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74    db->autoCommit
1c241 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
1c242 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c  p->rc = rc = SQL
1c243 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
1c244 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65      goto vdbe_re
1c245 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a  turn;.        }.
1c246 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72          db->isTr
1c247 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69  ansactionSavepoi
1c248 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  nt = 0;.        
1c249 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20  rc = p->rc;.    
1c24a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1c24b 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20   int ii;.       
1c24c 20 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 64 62   iSavepoint = db
1c24d 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2d 20 69  ->nSavepoint - i
1c24e 53 61 76 65 70 6f 69 6e 74 20 2d 20 31 3b 0a 20  Savepoint - 1;. 
1c24f 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b         for(ii=0;
1c250 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b   ii<db->nDb; ii+
1c251 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63  +){.          rc
1c252 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53   = sqlite3BtreeS
1c253 61 76 65 70 6f 69 6e 74 28 64 62 2d 3e 61 44 62  avepoint(db->aDb
1c254 5b 69 69 5d 2e 70 42 74 2c 20 70 31 2c 20 69 53  [ii].pBt, p1, iS
1c255 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20  avepoint);.     
1c256 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1c257 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1c258 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
1c259 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1c25a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1c25b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1c25c 20 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52   p1==SAVEPOINT_R
1c25d 4f 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 2d 3e  OLLBACK && (db->
1c25e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
1c25f 65 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 20 29  ernChanges)!=0 )
1c260 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
1c261 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
1c262 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b  dStatements(db);
1c263 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1c264 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
1c265 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
1c266 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1c267 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 67 61    .      /* Rega
1c268 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
1c269 72 20 74 68 69 73 20 69 73 20 61 20 52 45 4c 45  r this is a RELE
1c26a 41 53 45 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c  ASE or ROLLBACK,
1c26b 20 64 65 73 74 72 6f 79 20 61 6c 6c 20 0a 20 20   destroy all .  
1c26c 20 20 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74      ** savepoint
1c26d 73 20 6e 65 73 74 65 64 20 69 6e 73 69 64 65 20  s nested inside 
1c26e 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74  of the savepoint
1c26f 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20   being operated 
1c270 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  on. */.      whi
1c271 6c 65 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69  le( db->pSavepoi
1c272 6e 74 21 3d 70 53 61 76 65 70 6f 69 6e 74 20 29  nt!=pSavepoint )
1c273 7b 0a 20 20 20 20 20 20 20 20 53 61 76 65 70 6f  {.        Savepo
1c274 69 6e 74 20 2a 70 54 6d 70 20 3d 20 64 62 2d 3e  int *pTmp = db->
1c275 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20  pSavepoint;.    
1c276 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69      db->pSavepoi
1c277 6e 74 20 3d 20 70 54 6d 70 2d 3e 70 4e 65 78 74  nt = pTmp->pNext
1c278 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1c279 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70  3DbFree(db, pTmp
1c27a 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e  );.        db->n
1c27b 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20  Savepoint--;.   
1c27c 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49     }..      /* I
1c27d 66 20 69 74 20 69 73 20 61 20 52 45 4c 45 41 53  f it is a RELEAS
1c27e 45 2c 20 74 68 65 6e 20 64 65 73 74 72 6f 79 20  E, then destroy 
1c27f 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 62 65  the savepoint be
1c280 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20  ing operated on 
1c281 74 6f 6f 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  too */.      if(
1c282 20 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52   p1==SAVEPOINT_R
1c283 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20  ELEASE ){.      
1c284 20 20 61 73 73 65 72 74 28 20 70 53 61 76 65 70    assert( pSavep
1c285 6f 69 6e 74 3d 3d 64 62 2d 3e 70 53 61 76 65 70  oint==db->pSavep
1c286 6f 69 6e 74 20 29 3b 0a 20 20 20 20 20 20 20 20  oint );.        
1c287 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d  db->pSavepoint =
1c288 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65   pSavepoint->pNe
1c289 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  xt;.        sqli
1c28a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53  te3DbFree(db, pS
1c28b 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20  avepoint);.     
1c28c 20 20 20 69 66 28 20 21 69 73 54 72 61 6e 73 61     if( !isTransa
1c28d 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20  ction ){.       
1c28e 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e     db->nSavepoin
1c28f 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  t--;.        }. 
1c290 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1c291 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ..  break;.}../*
1c292 20 4f 70 63 6f 64 65 3a 20 41 75 74 6f 43 6f 6d   Opcode: AutoCom
1c293 6d 69 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  mit P1 P2 * * *.
1c294 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 64 61  **.** Set the da
1c295 74 61 62 61 73 65 20 61 75 74 6f 2d 63 6f 6d 6d  tabase auto-comm
1c296 69 74 20 66 6c 61 67 20 74 6f 20 50 31 20 28 31  it flag to P1 (1
1c297 20 6f 72 20 30 29 2e 20 49 66 20 50 32 20 69 73   or 0). If P2 is
1c298 20 74 72 75 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62   true, roll.** b
1c299 61 63 6b 20 61 6e 79 20 63 75 72 72 65 6e 74 6c  ack any currentl
1c29a 79 20 61 63 74 69 76 65 20 62 74 72 65 65 20 74  y active btree t
1c29b 72 61 6e 73 61 63 74 69 6f 6e 73 2e 20 49 66 20  ransactions. If 
1c29c 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 63  there are any ac
1c29d 74 69 76 65 0a 2a 2a 20 56 4d 73 20 28 61 70 61  tive.** VMs (apa
1c29e 72 74 20 66 72 6f 6d 20 74 68 69 73 20 6f 6e 65  rt from this one
1c29f 29 2c 20 74 68 65 6e 20 61 20 52 4f 4c 4c 42 41  ), then a ROLLBA
1c2a0 43 4b 20 66 61 69 6c 73 2e 20 20 41 20 43 4f 4d  CK fails.  A COM
1c2a1 4d 49 54 20 66 61 69 6c 73 20 69 66 0a 2a 2a 20  MIT fails if.** 
1c2a2 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65  there are active
1c2a3 20 77 72 69 74 69 6e 67 20 56 4d 73 20 6f 72 20   writing VMs or 
1c2a4 61 63 74 69 76 65 20 56 4d 73 20 74 68 61 74 20  active VMs that 
1c2a5 75 73 65 20 73 68 61 72 65 64 20 63 61 63 68 65  use shared cache
1c2a6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73  ..**.** This ins
1c2a7 74 72 75 63 74 69 6f 6e 20 63 61 75 73 65 73 20  truction causes 
1c2a8 74 68 65 20 56 4d 20 74 6f 20 68 61 6c 74 2e 0a  the VM to halt..
1c2a9 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 75 74 6f 43  */.case OP_AutoC
1c2aa 6f 6d 6d 69 74 3a 20 7b 0a 20 20 69 6e 74 20 64  ommit: {.  int d
1c2ab 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1c2ac 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e   = pOp->p1;.  in
1c2ad 74 20 72 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f 70  t rollback = pOp
1c2ae 2d 3e 70 32 3b 0a 20 20 69 6e 74 20 74 75 72 6e  ->p2;.  int turn
1c2af 4f 6e 41 43 20 3d 20 64 65 73 69 72 65 64 41 75  OnAC = desiredAu
1c2b0 74 6f 43 6f 6d 6d 69 74 20 26 26 20 21 64 62 2d  toCommit && !db-
1c2b1 3e 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 0a 20 20  >autoCommit;..  
1c2b2 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 41  assert( desiredA
1c2b3 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20  utoCommit==1 || 
1c2b4 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
1c2b5 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  t==0 );.  assert
1c2b6 28 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  ( desiredAutoCom
1c2b7 6d 69 74 3d 3d 31 20 7c 7c 20 72 6f 6c 6c 62 61  mit==1 || rollba
1c2b8 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 61 73 73 65  ck==0 );..  asse
1c2b9 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  rt( db->activeVd
1c2ba 62 65 43 6e 74 3e 30 20 29 3b 20 20 2f 2a 20 41  beCnt>0 );  /* A
1c2bb 74 20 6c 65 61 73 74 20 74 68 69 73 20 6f 6e 65  t least this one
1c2bc 20 56 4d 20 69 73 20 61 63 74 69 76 65 20 2a 2f   VM is active */
1c2bd 0a 0a 20 20 69 66 28 20 74 75 72 6e 4f 6e 41 43  ..  if( turnOnAC
1c2be 20 26 26 20 72 6f 6c 6c 62 61 63 6b 20 26 26 20   && rollback && 
1c2bf 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
1c2c0 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  t>1 ){.    /* If
1c2c1 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1c2c2 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 52  n implements a R
1c2c3 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 6f 74 68 65  OLLBACK and othe
1c2c4 72 20 56 4d 73 20 61 72 65 0a 20 20 20 20 2a 2a  r VMs are.    **
1c2c5 20 73 74 69 6c 6c 20 72 75 6e 6e 69 6e 67 2c 20   still running, 
1c2c6 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f  and a transactio
1c2c7 6e 20 69 73 20 61 63 74 69 76 65 2c 20 72 65 74  n is active, ret
1c2c8 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 64  urn an error ind
1c2c9 69 63 61 74 69 6e 67 0a 20 20 20 20 2a 2a 20 74  icating.    ** t
1c2ca 68 61 74 20 74 68 65 20 6f 74 68 65 72 20 56 4d  hat the other VM
1c2cb 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74 65 20  s must complete 
1c2cc 66 69 72 73 74 2e 20 0a 20 20 20 20 2a 2f 0a 20  first. .    */. 
1c2cd 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1c2ce 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1c2cf 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 72 6f 6c   db, "cannot rol
1c2d0 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74 69 6f  lback transactio
1c2d1 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20 22 53  n - ".        "S
1c2d2 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  QL statements in
1c2d3 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 20   progress");.   
1c2d4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
1c2d5 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 74  Y;.  }else if( t
1c2d6 75 72 6e 4f 6e 41 43 20 26 26 20 21 72 6f 6c 6c  urnOnAC && !roll
1c2d7 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74  back && db->writ
1c2d8 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20  eVdbeCnt>1 ){.  
1c2d9 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73    /* If this ins
1c2da 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65  truction impleme
1c2db 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64  nts a COMMIT and
1c2dc 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20 77   other VMs are w
1c2dd 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65  riting.    ** re
1c2de 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e  turn an error in
1c2df 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68  dicating that th
1c2e0 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74  e other VMs must
1c2e1 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e   complete first.
1c2e2 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c   .    */.    sql
1c2e3 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1c2e4 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1c2e5 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 72  cannot commit tr
1c2e6 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20  ansaction - ".  
1c2e7 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65        "SQL state
1c2e8 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73  ments in progres
1c2e9 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  s");.    rc = SQ
1c2ea 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c  LITE_BUSY;.  }el
1c2eb 73 65 20 69 66 28 20 64 65 73 69 72 65 64 41 75  se if( desiredAu
1c2ec 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d 3e 61 75  toCommit!=db->au
1c2ed 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20  toCommit ){.    
1c2ee 69 66 28 20 72 6f 6c 6c 62 61 63 6b 20 29 7b 0a  if( rollback ){.
1c2ef 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 65        assert( de
1c2f0 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d  siredAutoCommit=
1c2f1 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =1 );.      sqli
1c2f2 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64  te3RollbackAll(d
1c2f3 62 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75  b);.      db->au
1c2f4 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20  toCommit = 1;.  
1c2f5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
1c2f6 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
1c2f7 28 75 38 29 64 65 73 69 72 65 64 41 75 74 6f 43  (u8)desiredAutoC
1c2f8 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 69 66 28  ommit;.      if(
1c2f9 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74   sqlite3VdbeHalt
1c2fa 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59  (p)==SQLITE_BUSY
1c2fb 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70   ){.        p->p
1c2fc 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 20 20  c = pc;.        
1c2fd 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d  db->autoCommit =
1c2fe 20 28 75 38 29 28 31 2d 64 65 73 69 72 65 64 41   (u8)(1-desiredA
1c2ff 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20  utoCommit);.    
1c300 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d      p->rc = rc =
1c301 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
1c302 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f        goto vdbe_
1c303 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
1c304 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
1c305 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74  ( db->nStatement
1c306 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==0 );.    sqlit
1c307 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74  e3CloseSavepoint
1c308 73 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70  s(db);.    if( p
1c309 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
1c30a 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
1c30b 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d  LITE_DONE;.    }
1c30c 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
1c30d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1c30e 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64     }.    goto vd
1c30f 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c  be_return;.  }el
1c310 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  se{.    sqlite3S
1c311 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1c312 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20  rMsg, db,.      
1c313 20 20 28 21 64 65 73 69 72 65 64 41 75 74 6f 43    (!desiredAutoC
1c314 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20 73  ommit)?"cannot s
1c315 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69  tart a transacti
1c316 6f 6e 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e  on within a tran
1c317 73 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20 20  saction":(.     
1c318 20 20 20 28 72 6f 6c 6c 62 61 63 6b 29 3f 22 63     (rollback)?"c
1c319 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 2d  annot rollback -
1c31a 20 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   no transaction 
1c31b 69 73 20 61 63 74 69 76 65 22 3a 0a 20 20 20 20  is active":.    
1c31c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
1c31d 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 2d 20  cannot commit - 
1c31e 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  no transaction i
1c31f 73 20 61 63 74 69 76 65 22 29 29 3b 0a 20 20 20  s active"));.   
1c320 20 20 20 20 20 20 0a 20 20 20 20 72 63 20 3d 20        .    rc = 
1c321 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1c322 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1c323 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 63   Opcode: Transac
1c324 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 2a  tion P1 P2 * * *
1c325 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74  .**.** Begin a t
1c326 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 65  ransaction.  The
1c327 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e 64   transaction end
1c328 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 20  s when a Commit 
1c329 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f  or Rollback.** o
1c32a 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e 74  pcode is encount
1c32b 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e 67  ered.  Depending
1c32c 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c   on the ON CONFL
1c32d 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68 65  ICT setting, the
1c32e 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
1c32f 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72 6f  might also be ro
1c330 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20  lled back if an 
1c331 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
1c332 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69  ered..**.** P1 i
1c333 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
1c334 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1c335 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74 72   on which the tr
1c336 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20  ansaction is.** 
1c337 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78 20  started.  Index 
1c338 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61  0 is the main da
1c339 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
1c33a 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a 2a  index 1 is the.*
1c33b 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20  * file used for 
1c33c 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1c33d 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32 20  .  Indices of 2 
1c33e 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65 64  or more are used
1c33f 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 64   for.** attached
1c340 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a   databases..**.*
1c341 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d 7a  * If P2 is non-z
1c342 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 74  ero, then a writ
1c343 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e-transaction is
1c344 20 73 74 61 72 74 65 64 2e 20 20 41 20 52 45 53   started.  A RES
1c345 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a  ERVED lock is.**
1c346 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65   obtained on the
1c347 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
1c348 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e  hen a write-tran
1c349 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74  saction is start
1c34a 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72  ed.  No.** other
1c34b 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74 61   process can sta
1c34c 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74 65  rt another write
1c34d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69   transaction whi
1c34e 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  le this transact
1c34f 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 77  ion is.** underw
1c350 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61 20  ay.  Starting a 
1c351 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
1c352 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20 61  n also creates a
1c353 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
1c354 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 72  l. A.** write tr
1c355 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
1c356 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65  e started before
1c357 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61 6e   any changes can
1c358 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a   be made to the.
1c359 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49 66  ** database.  If
1c35a 20 50 32 20 69 73 20 32 20 6f 72 20 67 72 65 61   P2 is 2 or grea
1c35b 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43 4c  ter then an EXCL
1c35c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c  USIVE lock is al
1c35d 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f  so obtained.** o
1c35e 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  n the file..**.*
1c35f 2a 20 49 66 20 50 32 20 69 73 20 7a 65 72 6f 2c  * If P2 is zero,
1c360 20 74 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f 63   then a read-loc
1c361 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e  k is obtained on
1c362 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1c363 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  le..*/.case OP_T
1c364 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 20 20  ransaction: {.  
1c365 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
1c366 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a  .  Btree *pBt;..
1c367 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1c368 26 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  & i<db->nDb );. 
1c369 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
1c36a 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29  eeMask & (1<<i))
1c36b 21 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64  !=0 );.  pBt = d
1c36c 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 0a  b->aDb[i].pBt;..
1c36d 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
1c36e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1c36f 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 42 74  eeBeginTrans(pBt
1c370 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 20  , pOp->p2);.    
1c371 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42  if( rc==SQLITE_B
1c372 55 53 59 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  USY ){.      p->
1c373 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 70  pc = pc;.      p
1c374 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49  ->rc = rc = SQLI
1c375 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 67  TE_BUSY;.      g
1c376 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b  oto vdbe_return;
1c377 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
1c378 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c!=SQLITE_OK && 
1c379 72 63 21 3d 53 51 4c 49 54 45 5f 52 45 41 44 4f  rc!=SQLITE_READO
1c37a 4e 4c 59 20 2f 2a 20 26 26 20 72 63 21 3d 53 51  NLY /* && rc!=SQ
1c37b 4c 49 54 45 5f 42 55 53 59 20 2a 2f 20 29 7b 0a  LITE_BUSY */ ){.
1c37c 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
1c37d 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1c37e 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b     }.  }.  break
1c37f 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1c380 52 65 61 64 43 6f 6f 6b 69 65 20 50 31 20 50 32  ReadCookie P1 P2
1c381 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65   P3 * *.**.** Re
1c382 61 64 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72  ad cookie number
1c383 20 50 33 20 66 72 6f 6d 20 64 61 74 61 62 61 73   P3 from databas
1c384 65 20 50 31 20 61 6e 64 20 77 72 69 74 65 20 69  e P1 and write i
1c385 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  t into register 
1c386 50 32 2e 0a 2a 2a 20 50 33 3d 3d 30 20 69 73 20  P2..** P3==0 is 
1c387 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69  the schema versi
1c388 6f 6e 2e 20 20 50 33 3d 3d 31 20 69 73 20 74 68  on.  P3==1 is th
1c389 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61  e database forma
1c38a 74 2e 0a 2a 2a 20 50 33 3d 3d 32 20 69 73 20 74  t..** P3==2 is t
1c38b 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70  he recommended p
1c38c 61 67 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c  ager cache size,
1c38d 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20   and so forth.  
1c38e 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20  P1==0 is.** the 
1c38f 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1c390 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20  le and P1==1 is 
1c391 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1c392 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a  e used to store.
1c393 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ** temporary tab
1c394 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31  les..**.** If P1
1c395 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
1c396 65 6e 20 74 68 69 73 20 69 73 20 61 20 72 65 71  en this is a req
1c397 75 65 73 74 20 74 6f 20 72 65 61 64 20 74 68 65  uest to read the
1c398 20 73 69 7a 65 20 6f 66 20 61 0a 2a 2a 20 64 61   size of a.** da
1c399 74 61 62 61 73 65 73 20 66 72 65 65 2d 6c 69 73  tabases free-lis
1c39a 74 2e 20 50 33 20 6d 75 73 74 20 62 65 20 73 65  t. P3 must be se
1c39b 74 20 74 6f 20 31 20 69 6e 20 74 68 69 73 20 63  t to 1 in this c
1c39c 61 73 65 2e 20 54 68 65 20 61 63 74 75 61 6c 0a  ase. The actual.
1c39d 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 63 65  ** database acce
1c39e 73 73 65 64 20 69 73 20 28 28 50 31 2b 31 29 2a  ssed is ((P1+1)*
1c39f 2d 31 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  -1). For example
1c3a0 2c 20 61 20 50 31 20 70 61 72 61 6d 65 74 65 72  , a P1 parameter
1c3a1 20 6f 66 20 2d 31 0a 2a 2a 20 63 6f 72 72 65 73   of -1.** corres
1c3a2 70 6f 6e 64 73 20 74 6f 20 64 61 74 61 62 61 73  ponds to databas
1c3a3 65 20 30 20 28 22 6d 61 69 6e 22 29 2c 20 61 20  e 0 ("main"), a 
1c3a4 50 31 20 6f 66 20 2d 32 20 69 73 20 64 61 74 61  P1 of -2 is data
1c3a5 62 61 73 65 20 31 20 28 22 74 65 6d 70 22 29 2e  base 1 ("temp").
1c3a6 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
1c3a7 74 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b  t be a read-lock
1c3a8 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1c3a9 20 28 65 69 74 68 65 72 20 61 20 74 72 61 6e 73   (either a trans
1c3aa 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62  action.** must b
1c3ab 65 20 73 74 61 72 74 65 64 20 6f 72 20 74 68 65  e started or the
1c3ac 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70  re must be an op
1c3ad 65 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72  en cursor) befor
1c3ae 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74  e.** executing t
1c3af 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  his instruction.
1c3b0 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64  .*/.case OP_Read
1c3b1 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20  Cookie: {       
1c3b2 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1c3b3 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1c3b4 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74  int iMeta;.  int
1c3b5 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a   iDb = pOp->p1;.
1c3b6 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 20 3d 20    int iCookie = 
1c3b7 70 4f 70 2d 3e 70 33 3b 0a 0a 20 20 61 73 73 65  pOp->p3;..  asse
1c3b8 72 74 28 20 70 4f 70 2d 3e 70 33 3c 53 51 4c 49  rt( pOp->p3<SQLI
1c3b9 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20  TE_N_BTREE_META 
1c3ba 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29  );.  if( iDb<0 )
1c3bb 7b 0a 20 20 20 20 69 44 62 20 3d 20 28 2d 31 2a  {.    iDb = (-1*
1c3bc 28 69 44 62 2b 31 29 29 3b 0a 20 20 20 20 69 43  (iDb+1));.    iC
1c3bd 6f 6f 6b 69 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d  ookie *= -1;.  }
1c3be 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
1c3bf 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
1c3c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62   );.  assert( db
1c3c1 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d  ->aDb[iDb].pBt!=
1c3c2 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  0 );.  assert( (
1c3c3 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1c3c4 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a 20  1<<iDb))!=0 );. 
1c3c5 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 69 6e 67   /* The indexing
1c3c6 20 6f 66 20 6d 65 74 61 20 76 61 6c 75 65 73 20   of meta values 
1c3c7 61 74 20 74 68 65 20 73 63 68 65 6d 61 20 6c 61  at the schema la
1c3c8 79 65 72 20 69 73 20 6f 66 66 20 62 79 20 6f 6e  yer is off by on
1c3c9 65 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20  e from.  ** the 
1c3ca 69 6e 64 65 78 69 6e 67 20 69 6e 20 74 68 65 20  indexing in the 
1c3cb 62 74 72 65 65 20 6c 61 79 65 72 2e 20 20 54 68  btree layer.  Th
1c3cc 65 20 62 74 72 65 65 20 63 6f 6e 73 69 64 65 72  e btree consider
1c3cd 73 20 6d 65 74 61 5b 30 5d 20 74 6f 0a 20 20 2a  s meta[0] to.  *
1c3ce 2a 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20  * be the number 
1c3cf 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 69 6e  of free pages in
1c3d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 61   the database (a
1c3d1 20 72 65 61 64 2d 6f 6e 6c 79 20 76 61 6c 75 65   read-only value
1c3d2 29 0a 20 20 2a 2a 20 61 6e 64 20 6d 65 74 61 5b  ).  ** and meta[
1c3d3 31 5d 20 74 6f 20 62 65 20 74 68 65 20 73 63 68  1] to be the sch
1c3d4 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65  ema cookie.  The
1c3d5 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 63 6f   schema layer co
1c3d6 6e 73 69 64 65 72 73 0a 20 20 2a 2a 20 6d 65 74  nsiders.  ** met
1c3d7 61 5b 31 5d 20 74 6f 20 62 65 20 74 68 65 20 73  a[1] to be the s
1c3d8 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 53  chema cookie.  S
1c3d9 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73 68 69  o we have to shi
1c3da 66 74 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a  ft the index.  *
1c3db 2a 20 62 79 20 6f 6e 65 20 69 6e 20 74 68 65 20  * by one in the 
1c3dc 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d  following statem
1c3dd 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ent..  */.  rc =
1c3de 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
1c3df 4d 65 74 61 28 64 62 2d 3e 61 44 62 5b 69 44 62  Meta(db->aDb[iDb
1c3e0 5d 2e 70 42 74 2c 20 31 20 2b 20 69 43 6f 6f 6b  ].pBt, 1 + iCook
1c3e1 69 65 2c 20 28 75 33 32 20 2a 29 26 69 4d 65 74  ie, (u32 *)&iMet
1c3e2 61 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20  a);.  pOut->u.i 
1c3e3 3d 20 69 4d 65 74 61 3b 0a 20 20 4d 65 6d 53 65  = iMeta;.  MemSe
1c3e4 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1c3e5 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61  MEM_Int);.  brea
1c3e6 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1c3e7 20 53 65 74 43 6f 6f 6b 69 65 20 50 31 20 50 32   SetCookie P1 P2
1c3e8 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72   P3 * *.**.** Wr
1c3e9 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ite the content 
1c3ea 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 28  of register P3 (
1c3eb 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61  interpreted as a
1c3ec 6e 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20 69 6e  n integer).** in
1c3ed 74 6f 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72  to cookie number
1c3ee 20 50 32 20 6f 66 20 64 61 74 61 62 61 73 65 20   P2 of database 
1c3ef 50 31 2e 0a 2a 2a 20 50 32 3d 3d 30 20 69 73 20  P1..** P2==0 is 
1c3f0 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69  the schema versi
1c3f1 6f 6e 2e 20 20 50 32 3d 3d 31 20 69 73 20 74 68  on.  P2==1 is th
1c3f2 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61  e database forma
1c3f3 74 2e 0a 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74  t..** P2==2 is t
1c3f4 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70  he recommended p
1c3f5 61 67 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c  ager cache size,
1c3f6 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20   and so forth.  
1c3f7 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20  P1==0 is.** the 
1c3f8 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1c3f9 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20  le and P1==1 is 
1c3fa 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1c3fb 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a  e used to store.
1c3fc 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ** temporary tab
1c3fd 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61  les..**.** A tra
1c3fe 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  nsaction must be
1c3ff 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20   started before 
1c400 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 6f  executing this o
1c401 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  pcode..*/.case O
1c402 50 5f 53 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20  P_SetCookie: {  
1c403 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20       /* in3 */. 
1c404 20 44 62 20 2a 70 44 62 3b 0a 20 20 61 73 73 65   Db *pDb;.  asse
1c405 72 74 28 20 70 4f 70 2d 3e 70 32 3c 53 51 4c 49  rt( pOp->p2<SQLI
1c406 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20  TE_N_BTREE_META 
1c407 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1c408 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1c409 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  p1<db->nDb );.  
1c40a 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
1c40b 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d  eMask & (1<<pOp-
1c40c 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 44  >p1))!=0 );.  pD
1c40d 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70  b = &db->aDb[pOp
1c40e 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1c40f 20 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a   pDb->pBt!=0 );.
1c410 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1c411 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 33 29  Integerify(pIn3)
1c412 3b 0a 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65 20  ;.  /* See note 
1c413 61 62 6f 75 74 20 69 6e 64 65 78 20 73 68 69 66  about index shif
1c414 74 69 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64 43  ting on OP_ReadC
1c415 6f 6f 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d 20  ookie */.  rc = 
1c416 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61  sqlite3BtreeUpda
1c417 74 65 4d 65 74 61 28 70 44 62 2d 3e 70 42 74 2c  teMeta(pDb->pBt,
1c418 20 31 2b 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74   1+pOp->p2, (int
1c419 29 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69  )pIn3->u.i);.  i
1c41a 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b  f( pOp->p2==0 ){
1c41b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65  .    /* When the
1c41c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63   schema cookie c
1c41d 68 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74  hanges, record t
1c41e 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e  he new cookie in
1c41f 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20  ternally */.    
1c420 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63  pDb->pSchema->sc
1c421 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 28 69  hema_cookie = (i
1c422 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20  nt)pIn3->u.i;.  
1c423 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
1c424 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
1c425 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ges;.  }else if(
1c426 20 70 4f 70 2d 3e 70 32 3d 3d 31 20 29 7b 0a 20   pOp->p2==1 ){. 
1c427 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63 68 61     /* Record cha
1c428 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65  nges in the file
1c429 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20 70   format */.    p
1c42a 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c  Db->pSchema->fil
1c42b 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29 70  e_format = (u8)p
1c42c 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20  In3->u.i;.  }.  
1c42d 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 29  if( pOp->p1==1 )
1c42e 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69 64  {.    /* Invalid
1c42f 61 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 64  ate all prepared
1c430 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65 6e   statements when
1c431 65 76 65 72 20 74 68 65 20 54 45 4d 50 20 64 61  ever the TEMP da
1c432 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 73 63  tabase.    ** sc
1c433 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64 2e  hema is changed.
1c434 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20 2a    Ticket #1644 *
1c435 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  /.    sqlite3Exp
1c436 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
1c437 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 0a 20  ments(db);.  }. 
1c438 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1c439 63 6f 64 65 3a 20 56 65 72 69 66 79 43 6f 6f 6b  code: VerifyCook
1c43a 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  ie P1 P2 *.**.**
1c43b 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65   Check the value
1c43c 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 62   of global datab
1c43d 61 73 65 20 70 61 72 61 6d 65 74 65 72 20 6e 75  ase parameter nu
1c43e 6d 62 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 73  mber 0 (the.** s
1c43f 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 61  chema version) a
1c440 6e 64 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  nd make sure it 
1c441 69 73 20 65 71 75 61 6c 20 74 6f 20 50 32 2e 20  is equal to P2. 
1c442 20 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 64   .** P1 is the d
1c443 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 77  atabase number w
1c444 68 69 63 68 20 69 73 20 30 20 66 6f 72 20 74 68  hich is 0 for th
1c445 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1c446 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66 6f  file.** and 1 fo
1c447 72 20 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 69  r the file holdi
1c448 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ng temporary tab
1c449 6c 65 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 67  les and some hig
1c44a 68 65 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f  her number.** fo
1c44b 72 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  r auxiliary data
1c44c 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  bases..**.** The
1c44d 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 20   cookie changes 
1c44e 69 74 73 20 76 61 6c 75 65 20 77 68 65 6e 65 76  its value whenev
1c44f 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  er the database 
1c450 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 0a  schema changes..
1c451 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f  ** This operatio
1c452 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74  n is used to det
1c453 65 63 74 20 77 68 65 6e 20 74 68 61 74 20 74 68  ect when that th
1c454 65 20 63 6f 6f 6b 69 65 20 68 61 73 20 63 68 61  e cookie has cha
1c455 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74  nged.** and that
1c456 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f   the current pro
1c457 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 72 65  cess needs to re
1c458 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e  read the schema.
1c459 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61 20  .**.** Either a 
1c45a 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64  transaction need
1c45b 73 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 73  s to have been s
1c45c 74 61 72 74 65 64 20 6f 72 20 61 6e 20 4f 50 5f  tarted or an OP_
1c45d 4f 70 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 6f  Open needs.** to
1c45e 20 62 65 20 65 78 65 63 75 74 65 64 20 28 74 6f   be executed (to
1c45f 20 65 73 74 61 62 6c 69 73 68 20 61 20 72 65 61   establish a rea
1c460 64 20 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20 74  d lock) before t
1c461 68 69 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a 2a  his opcode is.**
1c462 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73   invoked..*/.cas
1c463 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69  e OP_VerifyCooki
1c464 65 3a 20 7b 0a 20 20 69 6e 74 20 69 4d 65 74 61  e: {.  int iMeta
1c465 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a  ;.  Btree *pBt;.
1c466 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c467 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1c468 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
1c469 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
1c46a 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31  sk & (1<<pOp->p1
1c46b 29 29 21 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d  ))!=0 );.  pBt =
1c46c 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31   db->aDb[pOp->p1
1c46d 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20 70 42 74  ].pBt;.  if( pBt
1c46e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1c46f 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
1c470 28 70 42 74 2c 20 31 2c 20 28 75 33 32 20 2a 29  (pBt, 1, (u32 *)
1c471 26 69 4d 65 74 61 29 3b 0a 20 20 7d 65 6c 73 65  &iMeta);.  }else
1c472 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1c473 45 5f 4f 4b 3b 0a 20 20 20 20 69 4d 65 74 61 20  E_OK;.    iMeta 
1c474 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  = 0;.  }.  if( r
1c475 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1c476 69 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20 29  iMeta!=pOp->p2 )
1c477 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
1c478 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1c479 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72  sg);.    p->zErr
1c47a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53  Msg = sqlite3DbS
1c47b 74 72 44 75 70 28 64 62 2c 20 22 64 61 74 61 62  trDup(db, "datab
1c47c 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 63  ase schema has c
1c47d 68 61 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f 2a  hanged");.    /*
1c47e 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 2d 63   If the schema-c
1c47f 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64  ookie from the d
1c480 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 74  atabase file mat
1c481 63 68 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 20  ches the cookie 
1c482 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 77  .    ** stored w
1c483 69 74 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ith the in-memor
1c484 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  y representation
1c485 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2c 20   of the schema, 
1c486 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 65  do.    ** not re
1c487 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 20  load the schema 
1c488 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1c489 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20  e file..    **. 
1c48a 20 20 20 2a 2a 20 49 66 20 76 69 72 74 75 61 6c     ** If virtual
1c48b 2d 74 61 62 6c 65 73 20 61 72 65 20 69 6e 20 75  -tables are in u
1c48c 73 65 2c 20 74 68 69 73 20 69 73 20 6e 6f 74 20  se, this is not 
1c48d 6a 75 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61  just an optimiza
1c48e 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 74  tion..    ** Oft
1c48f 65 6e 2c 20 76 2d 74 61 62 6c 65 73 20 73 74 6f  en, v-tables sto
1c490 72 65 20 74 68 65 69 72 20 64 61 74 61 20 69 6e  re their data in
1c491 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 74 61   other SQLite ta
1c492 62 6c 65 73 2c 20 77 68 69 63 68 0a 20 20 20 20  bles, which.    
1c493 2a 2a 20 61 72 65 20 71 75 65 72 69 65 64 20 66  ** are queried f
1c494 72 6f 6d 20 77 69 74 68 69 6e 20 78 4e 65 78 74  rom within xNext
1c495 28 29 20 61 6e 64 20 6f 74 68 65 72 20 76 2d 74  () and other v-t
1c496 61 62 6c 65 20 6d 65 74 68 6f 64 73 20 75 73 69  able methods usi
1c497 6e 67 0a 20 20 20 20 2a 2a 20 70 72 65 70 61 72  ng.    ** prepar
1c498 65 64 20 71 75 65 72 69 65 73 2e 20 49 66 20 73  ed queries. If s
1c499 75 63 68 20 61 20 71 75 65 72 79 20 69 73 20 6f  uch a query is o
1c49a 75 74 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20 64  ut-of-date, we d
1c49b 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20 20  o not want to.  
1c49c 20 20 2a 2a 20 64 69 73 63 61 72 64 20 74 68 65    ** discard the
1c49d 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1c49e 2c 20 61 73 20 74 68 65 20 75 73 65 72 20 63 6f  , as the user co
1c49f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  de implementing 
1c4a0 74 68 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61 62  the.    ** v-tab
1c4a1 6c 65 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f  le would have to
1c4a2 20 62 65 20 72 65 61 64 79 20 66 6f 72 20 74 68   be ready for th
1c4a3 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  e sqlite3_vtab s
1c4a4 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 0a  tructure itself.
1c4a5 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76      ** to be inv
1c4a6 61 6c 69 64 61 74 65 64 20 77 68 65 6e 65 76 65  alidated wheneve
1c4a7 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  r sqlite3_step()
1c4a8 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   is called from 
1c4a9 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 61  within .    ** a
1c4aa 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e   v-table method.
1c4ab 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1c4ac 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d  db->aDb[pOp->p1]
1c4ad 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  .pSchema->schema
1c4ae 5f 63 6f 6f 6b 69 65 21 3d 69 4d 65 74 61 20 29  _cookie!=iMeta )
1c4af 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
1c4b0 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
1c4b1 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b  ma(db, pOp->p1);
1c4b2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
1c4b3 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
1c4b4 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b  dStatements(db);
1c4b5 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1c4b6 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 62  _SCHEMA;.  }.  b
1c4b7 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1c4b8 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 31 20  de: OpenRead P1 
1c4b9 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1c4ba 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e  * Open a read-on
1c4bb 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68  ly cursor for th
1c4bc 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  e database table
1c4bd 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65   whose root page
1c4be 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 20 64   is.** P2 in a d
1c4bf 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
1c4c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1c4c1 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
1c4c2 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 20  y P3. .** P3==0 
1c4c3 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 64  means the main d
1c4c4 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 20 6d  atabase, P3==1 m
1c4c5 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73  eans the databas
1c4c6 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a 20 74  e used for .** t
1c4c7 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2c  emporary tables,
1c4c8 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e 73 20   and P3>1 means 
1c4c9 75 73 65 64 20 74 68 65 20 63 6f 72 72 65 73 70  used the corresp
1c4ca 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65 64 0a  onding attached.
1c4cb 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 47 69  ** database.  Gi
1c4cc 76 65 20 74 68 65 20 6e 65 77 20 63 75 72 73 6f  ve the new curso
1c4cd 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20  r an identifier 
1c4ce 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31 0a 2a  of P1.  The P1.*
1c4cf 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20 6e 6f  * values need no
1c4d0 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 73 20  t be contiguous 
1c4d1 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c 75 65  but all P1 value
1c4d2 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d 61 6c  s should be smal
1c4d3 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 49  l integers..** I
1c4d4 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66 6f  t is an error fo
1c4d5 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67 61 74  r P1 to be negat
1c4d6 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35  ive..**.** If P5
1c4d7 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74 68 65  !=0 then use the
1c4d8 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1c4d9 73 74 65 72 20 50 32 20 61 73 20 74 68 65 20 72  ster P2 as the r
1c4da 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a 2a 2a  oot page, not.**
1c4db 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 32   the value of P2
1c4dc 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 54   itself..**.** T
1c4dd 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61 20 72  here will be a r
1c4de 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead lock on the 
1c4df 64 61 74 61 62 61 73 65 20 77 68 65 6e 65 76 65  database wheneve
1c4e0 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a  r there is an.**
1c4e1 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 20 49   open cursor.  I
1c4e2 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  f the database w
1c4e3 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f  as unlocked prio
1c4e4 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74 72 75  r to this instru
1c4e5 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20  ction.** then a 
1c4e6 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 63 71  read lock is acq
1c4e7 75 69 72 65 64 20 61 73 20 70 61 72 74 20 6f 66  uired as part of
1c4e8 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1c4e9 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f  n.  A read.** lo
1c4ea 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20  ck allows other 
1c4eb 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65 61  processes to rea
1c4ec 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62  d the database b
1c4ed 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20  ut prohibits.** 
1c4ee 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
1c4ef 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67  s from modifying
1c4f0 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
1c4f1 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 69 73  The read lock is
1c4f2 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68 65  .** released whe
1c4f3 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72  n all cursors ar
1c4f4 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68  e closed.  If th
1c4f5 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61  is instruction a
1c4f6 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65  ttempts.** to ge
1c4f7 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 62 75  t a read lock bu
1c4f8 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73 63 72  t fails, the scr
1c4f9 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73 20 77  ipt terminates w
1c4fa 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45  ith an.** SQLITE
1c4fb 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f 64 65  _BUSY error code
1c4fc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76  ..**.** The P4 v
1c4fd 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68  alue may be eith
1c4fe 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50  er an integer (P
1c4ff 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f  4_INT32) or a po
1c500 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65  inter to.** a Ke
1c501 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
1c502 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66  (P4_KEYINFO). If
1c503 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72   it is a pointer
1c504 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a   to a KeyInfo .*
1c505 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65  * structure, the
1c506 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 72 65  n said structure
1c507 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f 6e   defines the con
1c508 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69  tent and collati
1c509 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ng .** sequence 
1c50a 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 65 69  of the index bei
1c50b 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72  ng opened. Other
1c50c 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 20 61  wise, if P4 is a
1c50d 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61  n integer .** va
1c50e 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 20 74  lue, it is set t
1c50f 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
1c510 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74  columns in the t
1c511 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  able..**.** See 
1c512 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a  also OpenWrite..
1c513 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70  */./* Opcode: Op
1c514 65 6e 57 72 69 74 65 20 50 31 20 50 32 20 50 33  enWrite P1 P2 P3
1c515 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65   P4 P5.**.** Ope
1c516 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63  n a read/write c
1c517 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f  ursor named P1 o
1c518 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  n the table or i
1c519 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a  ndex whose root.
1c51a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e 20 20  ** page is P2.  
1c51b 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73 65 20  Or if P5!=0 use 
1c51c 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1c51d 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 66 69  egister P2 to fi
1c51e 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70  nd the.** root p
1c51f 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50  age..**.** The P
1c520 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65  4 value may be e
1c521 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72  ither an integer
1c522 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61   (P4_INT32) or a
1c523 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61   pointer to.** a
1c524 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
1c525 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e  re (P4_KEYINFO).
1c526 20 49 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e   If it is a poin
1c527 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f  ter to a KeyInfo
1c528 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20   .** structure, 
1c529 74 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 74  then said struct
1c52a 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ure defines the 
1c52b 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c  content and coll
1c52c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e  ating .** sequen
1c52d 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ce of the index 
1c52e 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74  being opened. Ot
1c52f 68 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69  herwise, if P4 i
1c530 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a  s an integer .**
1c531 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65   value, it is se
1c532 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  t to the number 
1c533 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
1c534 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  e table..**.** T
1c535 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1c536 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20  works just like 
1c537 4f 70 65 6e 52 65 61 64 20 65 78 63 65 70 74 20  OpenRead except 
1c538 74 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68  that it opens th
1c539 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72  e cursor.** in r
1c53a 65 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20  ead/write mode. 
1c53b 20 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62   For a given tab
1c53c 6c 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65  le, there can be
1c53d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61   one or more rea
1c53e 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72  d-only.** cursor
1c53f 73 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65  s or a single re
1c540 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20  ad/write cursor 
1c541 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a  but not both..**
1c542 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65  .** See also Ope
1c543 6e 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f  nRead..*/.case O
1c544 50 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65  P_OpenRead:.case
1c545 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b   OP_OpenWrite: {
1c546 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 20 3d 20  .  int nField = 
1c547 30 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  0;.  KeyInfo *pK
1c548 65 79 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 69 6e  eyInfo = 0;.  in
1c549 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
1c54a 20 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e 70   int p2 = pOp->p
1c54b 32 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70  2;.  int iDb = p
1c54c 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e 74 20 77 72  Op->p3;.  int wr
1c54d 46 6c 61 67 3b 0a 20 20 42 74 72 65 65 20 2a 70  Flag;.  Btree *p
1c54e 58 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  X;.  VdbeCursor 
1c54f 2a 70 43 75 72 3b 0a 20 20 44 62 20 2a 70 44 62  *pCur;.  Db *pDb
1c550 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 69  ;.  .  assert( i
1c551 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d  Db>=0 && iDb<db-
1c552 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1c553 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1c554 26 20 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 29  & (1<<iDb))!=0 )
1c555 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  ;.  pDb = &db->a
1c556 44 62 5b 69 44 62 5d 3b 0a 20 20 70 58 20 3d 20  Db[iDb];.  pX = 
1c557 70 44 62 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65  pDb->pBt;.  asse
1c558 72 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 69  rt( pX!=0 );.  i
1c559 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
1c55a 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 29 7b 0a  OP_OpenWrite ){.
1c55b 20 20 20 20 77 72 46 6c 61 67 20 3d 20 31 3b 0a      wrFlag = 1;.
1c55c 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63      if( pDb->pSc
1c55d 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
1c55e 74 20 3c 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46  t < p->minWriteF
1c55f 69 6c 65 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20  ileFormat ){.   
1c560 20 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69     p->minWriteFi
1c561 6c 65 46 6f 72 6d 61 74 20 3d 20 70 44 62 2d 3e  leFormat = pDb->
1c562 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f  pSchema->file_fo
1c563 72 6d 61 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  rmat;.    }.  }e
1c564 6c 73 65 7b 0a 20 20 20 20 77 72 46 6c 61 67 20  lse{.    wrFlag 
1c565 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  = 0;.  }.  if( p
1c566 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 61 73  Op->p5 ){.    as
1c567 73 65 72 74 28 20 70 32 3e 30 20 29 3b 0a 20 20  sert( p2>0 );.  
1c568 20 20 61 73 73 65 72 74 28 20 70 32 3c 3d 70 2d    assert( p2<=p-
1c569 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 70 49 6e  >nMem );.    pIn
1c56a 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 5d  2 = &p->aMem[p2]
1c56b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1c56c 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
1c56d 49 6e 32 29 3b 0a 20 20 20 20 70 32 20 3d 20 28  In2);.    p2 = (
1c56e 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20  int)pIn2->u.i;. 
1c56f 20 20 20 69 66 28 20 70 32 3c 32 20 29 20 7b 0a     if( p2<2 ) {.
1c570 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1c571 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1c572 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
1c573 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1c574 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
1c575 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 69 66 28  t( i>=0 );.  if(
1c576 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
1c577 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20  _KEYINFO ){.    
1c578 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e  pKeyInfo = pOp->
1c579 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  p4.pKeyInfo;.   
1c57a 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d   pKeyInfo->enc =
1c57b 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20   ENC(p->db);.   
1c57c 20 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e   nField = pKeyIn
1c57d 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b 0a 20 20  fo->nField+1;.  
1c57e 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70  }else if( pOp->p
1c57f 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20  4type==P4_INT32 
1c580 29 7b 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20  ){.    nField = 
1c581 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 7d 0a 20  pOp->p4.i;.  }. 
1c582 20 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65   pCur = allocate
1c583 43 75 72 73 6f 72 28 70 2c 20 69 2c 20 6e 46 69  Cursor(p, i, nFi
1c584 65 6c 64 2c 20 69 44 62 2c 20 31 29 3b 0a 20 20  eld, iDb, 1);.  
1c585 69 66 28 20 70 43 75 72 3d 3d 30 20 29 20 67 6f  if( pCur==0 ) go
1c586 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 75  to no_mem;.  pCu
1c587 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a  r->nullRow = 1;.
1c588 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1c589 72 65 65 43 75 72 73 6f 72 28 70 58 2c 20 70 32  reeCursor(pX, p2
1c58a 2c 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e  , wrFlag, pKeyIn
1c58b 66 6f 2c 20 70 43 75 72 2d 3e 70 43 75 72 73 6f  fo, pCur->pCurso
1c58c 72 29 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79  r);.  pCur->pKey
1c58d 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b  Info = pKeyInfo;
1c58e 0a 0a 20 20 73 77 69 74 63 68 28 20 72 63 20 29  ..  switch( rc )
1c58f 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
1c590 45 5f 42 55 53 59 3a 20 7b 0a 20 20 20 20 20 20  E_BUSY: {.      
1c591 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20  p->pc = pc;.    
1c592 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53    p->rc = rc = S
1c593 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
1c594 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75    goto vdbe_retu
1c595 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  rn;.    }.    ca
1c596 73 65 20 53 51 4c 49 54 45 5f 4f 4b 3a 20 7b 0a  se SQLITE_OK: {.
1c597 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20        int flags 
1c598 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c  = sqlite3BtreeFl
1c599 61 67 73 28 70 43 75 72 2d 3e 70 43 75 72 73 6f  ags(pCur->pCurso
1c59a 72 29 3b 0a 20 20 20 20 20 20 2f 2a 20 53 61 6e  r);.      /* San
1c59b 69 74 79 20 63 68 65 63 6b 69 6e 67 2e 20 20 4f  ity checking.  O
1c59c 6e 6c 79 20 74 68 65 20 6c 6f 77 65 72 20 66 6f  nly the lower fo
1c59d 75 72 20 62 69 74 73 20 6f 66 20 74 68 65 20 66  ur bits of the f
1c59e 6c 61 67 73 20 62 79 74 65 20 73 68 6f 75 6c 64  lags byte should
1c59f 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65  .      ** be use
1c5a0 64 2e 20 20 42 69 74 20 33 20 28 6d 61 73 6b 20  d.  Bit 3 (mask 
1c5a1 30 78 30 38 29 20 69 73 20 75 6e 70 72 65 64 69  0x08) is unpredi
1c5a2 63 74 61 62 6c 65 2e 20 20 54 68 65 20 6c 6f 77  ctable.  The low
1c5a3 65 72 20 33 20 62 69 74 73 0a 20 20 20 20 20 20  er 3 bits.      
1c5a4 2a 2a 20 28 6d 61 73 6b 20 30 78 30 37 29 20 73  ** (mask 0x07) s
1c5a5 68 6f 75 6c 64 20 62 65 20 65 69 74 68 65 72 20  hould be either 
1c5a6 35 20 28 69 6e 74 6b 65 79 2b 6c 65 61 66 64 61  5 (intkey+leafda
1c5a7 74 61 20 66 6f 72 20 74 61 62 6c 65 73 29 20 6f  ta for tables) o
1c5a8 72 0a 20 20 20 20 20 20 2a 2a 20 32 20 28 7a 65  r.      ** 2 (ze
1c5a9 72 6f 64 61 74 61 20 66 6f 72 20 69 6e 64 69 63  rodata for indic
1c5aa 65 73 29 2e 20 20 49 66 20 74 68 65 73 65 20 63  es).  If these c
1c5ab 6f 6e 64 69 74 69 6f 6e 73 20 61 72 65 20 6e 6f  onditions are no
1c5ac 74 20 6d 65 74 20 69 74 20 63 61 6e 0a 20 20 20  t met it can.   
1c5ad 20 20 20 2a 2a 20 6f 6e 6c 79 20 6d 65 61 6e 20     ** only mean 
1c5ae 74 68 61 74 20 77 65 20 61 72 65 20 64 65 61 6c  that we are deal
1c5af 69 6e 67 20 77 69 74 68 20 61 20 63 6f 72 72 75  ing with a corru
1c5b0 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  pt database file
1c5b1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1c5b2 69 66 28 20 28 66 6c 61 67 73 20 26 20 30 78 66  if( (flags & 0xf
1c5b3 30 29 21 3d 30 20 7c 7c 20 28 28 66 6c 61 67 73  0)!=0 || ((flags
1c5b4 20 26 20 30 78 30 37 29 21 3d 35 20 26 26 20 28   & 0x07)!=5 && (
1c5b5 66 6c 61 67 73 20 26 20 30 78 30 37 29 21 3d 32  flags & 0x07)!=2
1c5b6 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  ) ){.        rc 
1c5b7 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
1c5b8 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67  _BKPT;.        g
1c5b9 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1c5ba 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a  _error;.      }.
1c5bb 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 54 61        pCur->isTa
1c5bc 62 6c 65 20 3d 20 28 66 6c 61 67 73 20 26 20 42  ble = (flags & B
1c5bd 54 52 45 45 5f 49 4e 54 4b 45 59 29 21 3d 30 20  TREE_INTKEY)!=0 
1c5be 3f 31 3a 30 3b 0a 20 20 20 20 20 20 70 43 75 72  ?1:0;.      pCur
1c5bf 2d 3e 69 73 49 6e 64 65 78 20 3d 20 28 66 6c 61  ->isIndex = (fla
1c5c0 67 73 20 26 20 42 54 52 45 45 5f 5a 45 52 4f 44  gs & BTREE_ZEROD
1c5c1 41 54 41 29 21 3d 30 20 3f 31 3a 30 3b 0a 20 20  ATA)!=0 ?1:0;.  
1c5c2 20 20 20 20 2f 2a 20 49 66 20 50 34 3d 3d 30 20      /* If P4==0 
1c5c3 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20  it means we are 
1c5c4 65 78 70 65 63 74 65 64 20 74 6f 20 6f 70 65 6e  expected to open
1c5c5 20 61 20 74 61 62 6c 65 2e 20 20 49 66 20 50 34   a table.  If P4
1c5c6 21 3d 30 20 74 68 65 6e 0a 20 20 20 20 20 20 2a  !=0 then.      *
1c5c7 2a 20 77 65 20 65 78 70 65 63 74 20 74 6f 20 62  * we expect to b
1c5c8 65 20 6f 70 65 6e 69 6e 67 20 61 6e 20 69 6e 64  e opening an ind
1c5c9 65 78 2e 20 20 49 66 20 74 68 69 73 20 69 73 20  ex.  If this is 
1c5ca 6e 6f 74 20 77 68 61 74 20 68 61 70 70 65 6e 65  not what happene
1c5cb 64 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  d,.      ** then
1c5cc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
1c5cd 20 63 6f 72 72 75 70 74 0a 20 20 20 20 20 20 2a   corrupt.      *
1c5ce 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70 43 75  /.      if( (pCu
1c5cf 72 2d 3e 69 73 54 61 62 6c 65 20 26 26 20 70 4f  r->isTable && pO
1c5d0 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45  p->p4type==P4_KE
1c5d1 59 49 4e 46 4f 29 0a 20 20 20 20 20 20 20 7c 7c  YINFO).       ||
1c5d2 20 28 70 43 75 72 2d 3e 69 73 49 6e 64 65 78 20   (pCur->isIndex 
1c5d3 26 26 20 70 4f 70 2d 3e 70 34 74 79 70 65 21 3d  && pOp->p4type!=
1c5d4 50 34 5f 4b 45 59 49 4e 46 4f 29 20 29 7b 0a 20  P4_KEYINFO) ){. 
1c5d5 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
1c5d6 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1c5d7 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
1c5d8 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1c5d9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1c5da 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1c5db 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4d 50   case SQLITE_EMP
1c5dc 54 59 3a 20 7b 0a 20 20 20 20 20 20 70 43 75 72  TY: {.      pCur
1c5dd 2d 3e 69 73 54 61 62 6c 65 20 3d 20 70 4f 70 2d  ->isTable = pOp-
1c5de 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 59 49  >p4type!=P4_KEYI
1c5df 4e 46 4f 3b 0a 20 20 20 20 20 20 70 43 75 72 2d  NFO;.      pCur-
1c5e0 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 43 75 72  >isIndex = !pCur
1c5e1 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 20 20 20  ->isTable;.     
1c5e2 20 70 43 75 72 2d 3e 70 43 75 72 73 6f 72 20 3d   pCur->pCursor =
1c5e3 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   0;.      rc = S
1c5e4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
1c5e5 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1c5e6 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
1c5e7 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
1c5e8 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  _to_error;.    }
1c5e9 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1c5ea 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e  ./* Opcode: Open
1c5eb 45 70 68 65 6d 65 72 61 6c 20 50 31 20 50 32 20  Ephemeral P1 P2 
1c5ec 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65  * P4 *.**.** Ope
1c5ed 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 50  n a new cursor P
1c5ee 31 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74  1 to a transient
1c5ef 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63   table..** The c
1c5f0 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79 73 20  ursor is always 
1c5f1 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72 69 74  opened read/writ
1c5f2 65 20 65 76 65 6e 20 69 66 20 0a 2a 2a 20 74 68  e even if .** th
1c5f3 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1c5f4 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 54  is read-only.  T
1c5f5 68 65 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 20  he transient or 
1c5f6 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65  virtual.** table
1c5f7 20 69 73 20 64 65 6c 65 74 65 64 20 61 75 74 6f   is deleted auto
1c5f8 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74  matically when t
1c5f9 68 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f  he cursor is clo
1c5fa 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73  sed..**.** P2 is
1c5fb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
1c5fc 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 76 69  olumns in the vi
1c5fd 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20  rtual table..** 
1c5fe 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74  The cursor point
1c5ff 73 20 74 6f 20 61 20 42 54 72 65 65 20 74 61 62  s to a BTree tab
1c600 6c 65 20 69 66 20 50 34 3d 3d 30 20 61 6e 64 20  le if P4==0 and 
1c601 74 6f 20 61 20 42 54 72 65 65 20 69 6e 64 65 78  to a BTree index
1c602 0a 2a 2a 20 69 66 20 50 34 20 69 73 20 6e 6f 74  .** if P4 is not
1c603 20 30 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f   0.  If P4 is no
1c604 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74  t NULL, it point
1c605 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73  s to a KeyInfo s
1c606 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74  tructure.** that
1c607 20 64 65 66 69 6e 65 73 20 74 68 65 20 66 6f 72   defines the for
1c608 6d 61 74 20 6f 66 20 6b 65 79 73 20 69 6e 20 74  mat of keys in t
1c609 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  he index..**.** 
1c60a 54 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20  This opcode was 
1c60b 6f 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70 65 6e  once called Open
1c60c 54 65 6d 70 2e 20 20 42 75 74 20 74 68 61 74 20  Temp.  But that 
1c60d 63 72 65 61 74 65 64 0a 2a 2a 20 63 6f 6e 66 75  created.** confu
1c60e 73 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65  sion because the
1c60f 20 74 65 72 6d 20 22 74 65 6d 70 20 74 61 62 6c   term "temp tabl
1c610 65 22 2c 20 6d 69 67 68 74 20 72 65 66 65 72 20  e", might refer 
1c611 65 69 74 68 65 72 0a 2a 2a 20 74 6f 20 61 20 54  either.** to a T
1c612 45 4d 50 20 74 61 62 6c 65 20 61 74 20 74 68 65  EMP table at the
1c613 20 53 51 4c 20 6c 65 76 65 6c 2c 20 6f 72 20 74   SQL level, or t
1c614 6f 20 61 20 74 61 62 6c 65 20 6f 70 65 6e 65 64  o a table opened
1c615 20 62 79 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f   by.** this opco
1c616 64 65 2e 20 20 54 68 65 6e 20 74 68 69 73 20 6f  de.  Then this o
1c617 70 63 6f 64 65 20 77 61 73 20 63 61 6c 6c 20 4f  pcode was call O
1c618 70 65 6e 56 69 72 74 75 61 6c 2e 20 20 42 75 74  penVirtual.  But
1c619 0a 2a 2a 20 74 68 61 74 20 63 72 65 61 74 65 64  .** that created
1c61a 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20   confusion with 
1c61b 74 68 65 20 77 68 6f 6c 65 20 76 69 72 74 75 61  the whole virtua
1c61c 6c 2d 74 61 62 6c 65 20 69 64 65 61 2e 0a 2a 2f  l-table idea..*/
1c61d 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 45 70 68  .case OP_OpenEph
1c61e 65 6d 65 72 61 6c 3a 20 7b 0a 20 20 69 6e 74 20  emeral: {.  int 
1c61f 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56  i = pOp->p1;.  V
1c620 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a  dbeCursor *pCx;.
1c621 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
1c622 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d 20 0a  nt openFlags = .
1c623 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
1c624 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20  N_READWRITE |.  
1c625 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
1c626 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 53  CREATE |.      S
1c627 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
1c628 53 49 56 45 20 7c 0a 20 20 20 20 20 20 53 51 4c  SIVE |.      SQL
1c629 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
1c62a 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 53  NCLOSE |.      S
1c62b 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53  QLITE_OPEN_TRANS
1c62c 49 45 4e 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65  IENT_DB;..  asse
1c62d 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43  rt( i>=0 );.  pC
1c62e 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73  x = allocateCurs
1c62f 6f 72 28 70 2c 20 69 2c 20 70 4f 70 2d 3e 70 32  or(p, i, pOp->p2
1c630 2c 20 2d 31 2c 20 31 29 3b 0a 20 20 69 66 28 20  , -1, 1);.  if( 
1c631 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f  pCx==0 ) goto no
1c632 5f 6d 65 6d 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c  _mem;.  pCx->nul
1c633 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d  lRow = 1;.  rc =
1c634 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63   sqlite3BtreeFac
1c635 74 6f 72 79 28 64 62 2c 20 30 2c 20 31 2c 20 53  tory(db, 0, 1, S
1c636 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45  QLITE_DEFAULT_TE
1c637 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 6f  MP_CACHE_SIZE, o
1c638 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20  penFlags,.      
1c639 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c63a 20 20 20 20 20 26 70 43 78 2d 3e 70 42 74 29 3b       &pCx->pBt);
1c63b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1c63c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
1c63d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
1c63e 69 6e 54 72 61 6e 73 28 70 43 78 2d 3e 70 42 74  inTrans(pCx->pBt
1c63f 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  , 1);.  }.  if( 
1c640 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1c641 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 72 61  .    /* If a tra
1c642 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 69 73 20  nsient index is 
1c643 72 65 71 75 69 72 65 64 2c 20 63 72 65 61 74 65  required, create
1c644 20 69 74 20 62 79 20 63 61 6c 6c 69 6e 67 0a 20   it by calling. 
1c645 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72     ** sqlite3Btr
1c646 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 20  eeCreateTable() 
1c647 77 69 74 68 20 74 68 65 20 42 54 52 45 45 5f 5a  with the BTREE_Z
1c648 45 52 4f 44 41 54 41 20 66 6c 61 67 20 62 65 66  ERODATA flag bef
1c649 6f 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69  ore.    ** openi
1c64a 6e 67 20 69 74 2e 20 49 66 20 61 20 74 72 61 6e  ng it. If a tran
1c64b 73 69 65 6e 74 20 74 61 62 6c 65 20 69 73 20 72  sient table is r
1c64c 65 71 75 69 72 65 64 2c 20 6a 75 73 74 20 75 73  equired, just us
1c64d 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 75 74  e the.    ** aut
1c64e 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74  omatically creat
1c64f 65 64 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  ed table with ro
1c650 6f 74 2d 70 61 67 65 20 31 20 28 61 6e 20 49 4e  ot-page 1 (an IN
1c651 54 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20 20 20  TKEY table)..   
1c652 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   */.    if( pOp-
1c653 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0a  >p4.pKeyInfo ){.
1c654 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a        int pgno;.
1c655 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1c656 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45  p->p4type==P4_KE
1c657 59 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20 20 72  YINFO );.      r
1c658 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1c659 43 72 65 61 74 65 54 61 62 6c 65 28 70 43 78 2d  CreateTable(pCx-
1c65a 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20 42 54 52  >pBt, &pgno, BTR
1c65b 45 45 5f 5a 45 52 4f 44 41 54 41 29 3b 20 0a 20  EE_ZERODATA); . 
1c65c 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1c65d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1c65e 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3d 3d    assert( pgno==
1c65f 4d 41 53 54 45 52 5f 52 4f 4f 54 2b 31 20 29 3b  MASTER_ROOT+1 );
1c660 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1c661 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
1c662 28 70 43 78 2d 3e 70 42 74 2c 20 70 67 6e 6f 2c  (pCx->pBt, pgno,
1c663 20 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   1, .           
1c664 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c665 20 20 20 20 20 28 4b 65 79 49 6e 66 6f 2a 29 70       (KeyInfo*)p
1c666 4f 70 2d 3e 70 34 2e 7a 2c 20 70 43 78 2d 3e 70  Op->p4.z, pCx->p
1c667 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20  Cursor);.       
1c668 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d   pCx->pKeyInfo =
1c669 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66   pOp->p4.pKeyInf
1c66a 6f 3b 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e  o;.        pCx->
1c66b 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20  pKeyInfo->enc = 
1c66c 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20  ENC(p->db);.    
1c66d 20 20 7d 0a 20 20 20 20 20 20 70 43 78 2d 3e 69    }.      pCx->i
1c66e 73 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20  sTable = 0;.    
1c66f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
1c670 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  = sqlite3BtreeCu
1c671 72 73 6f 72 28 70 43 78 2d 3e 70 42 74 2c 20 4d  rsor(pCx->pBt, M
1c672 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 30  ASTER_ROOT, 1, 0
1c673 2c 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b  , pCx->pCursor);
1c674 0a 20 20 20 20 20 20 70 43 78 2d 3e 69 73 54 61  .      pCx->isTa
1c675 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ble = 1;.    }. 
1c676 20 7d 0a 20 20 70 43 78 2d 3e 69 73 49 6e 64 65   }.  pCx->isInde
1c677 78 20 3d 20 21 70 43 78 2d 3e 69 73 54 61 62 6c  x = !pCx->isTabl
1c678 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  e;.  break;.}../
1c679 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 50 73  * Opcode: OpenPs
1c67a 65 75 64 6f 20 50 31 20 50 32 20 50 33 20 2a 20  eudo P1 P2 P3 * 
1c67b 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e  *.**.** Open a n
1c67c 65 77 20 63 75 72 73 6f 72 20 74 68 61 74 20 70  ew cursor that p
1c67d 6f 69 6e 74 73 20 74 6f 20 61 20 66 61 6b 65 20  oints to a fake 
1c67e 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61  table that conta
1c67f 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  ins a single.** 
1c680 72 6f 77 20 6f 66 20 64 61 74 61 2e 20 20 41 6e  row of data.  An
1c681 79 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69  y attempt to wri
1c682 74 65 20 61 20 73 65 63 6f 6e 64 20 72 6f 77 20  te a second row 
1c683 6f 66 20 64 61 74 61 20 63 61 75 73 65 73 20 74  of data causes t
1c684 68 65 0a 2a 2a 20 66 69 72 73 74 20 72 6f 77 20  he.** first row 
1c685 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 20  to be deleted.  
1c686 41 6c 6c 20 64 61 74 61 20 69 73 20 64 65 6c 65  All data is dele
1c687 74 65 64 20 77 68 65 6e 20 74 68 65 20 63 75 72  ted when the cur
1c688 73 6f 72 20 69 73 0a 2a 2a 20 63 6c 6f 73 65 64  sor is.** closed
1c689 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 73 65 75 64 6f  ..**.** A pseudo
1c68a 2d 74 61 62 6c 65 20 63 72 65 61 74 65 64 20 62  -table created b
1c68b 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73  y this opcode is
1c68c 20 75 73 65 66 75 6c 20 66 6f 72 20 68 6f 6c 64   useful for hold
1c68d 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 45 57 20 6f  ing the.** NEW o
1c68e 72 20 4f 4c 44 20 74 61 62 6c 65 73 20 69 6e 20  r OLD tables in 
1c68f 61 20 74 72 69 67 67 65 72 2e 20 20 41 6c 73 6f  a trigger.  Also
1c690 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68   used to hold th
1c691 65 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f  e a single.** ro
1c692 77 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 74 68  w output from th
1c693 65 20 73 6f 72 74 65 72 20 73 6f 20 74 68 61 74  e sorter so that
1c694 20 74 68 65 20 72 6f 77 20 63 61 6e 20 62 65 20   the row can be 
1c695 64 65 63 6f 6d 70 6f 73 65 64 20 69 6e 74 6f 0a  decomposed into.
1c696 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 63 6f  ** individual co
1c697 6c 75 6d 6e 73 20 75 73 69 6e 67 20 74 68 65 20  lumns using the 
1c698 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65  OP_Column opcode
1c699 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 4f 50 5f  ..**.** When OP_
1c69a 49 6e 73 65 72 74 20 69 73 20 65 78 65 63 75 74  Insert is execut
1c69b 65 64 20 74 6f 20 69 6e 73 65 72 74 20 61 20 72  ed to insert a r
1c69c 6f 77 20 69 6e 20 74 6f 20 74 68 65 20 70 73 65  ow in to the pse
1c69d 75 64 6f 20 74 61 62 6c 65 2c 0a 2a 2a 20 74 68  udo table,.** th
1c69e 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63  e pseudo-table c
1c69f 75 72 73 6f 72 20 6d 61 79 20 6f 72 20 6d 61 79  ursor may or may
1c6a0 20 6e 6f 74 20 6d 61 6b 65 20 69 74 27 73 20 6f   not make it's o
1c6a1 77 6e 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a  wn copy of the.*
1c6a2 2a 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 20 64  * original row d
1c6a3 61 74 61 2e 20 49 66 20 50 32 20 69 73 20 30 2c  ata. If P2 is 0,
1c6a4 20 74 68 65 6e 20 74 68 65 20 70 73 65 75 64 6f   then the pseudo
1c6a5 2d 74 61 62 6c 65 20 77 69 6c 6c 20 63 6f 70 79  -table will copy
1c6a6 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   the.** original
1c6a7 20 72 6f 77 20 64 61 74 61 2e 20 4f 74 68 65 72   row data. Other
1c6a8 77 69 73 65 2c 20 61 20 70 6f 69 6e 74 65 72 20  wise, a pointer 
1c6a9 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  to the original 
1c6aa 6d 65 6d 6f 72 79 20 63 65 6c 6c 0a 2a 2a 20 69  memory cell.** i
1c6ab 73 20 73 74 6f 72 65 64 2e 20 49 6e 20 74 68 69  s stored. In thi
1c6ac 73 20 63 61 73 65 2c 20 74 68 65 20 76 64 62 65  s case, the vdbe
1c6ad 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 65 6e   program must en
1c6ae 73 75 72 65 20 74 68 61 74 20 74 68 65 20 0a 2a  sure that the .*
1c6af 2a 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f  * memory cell co
1c6b0 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77  ntaining the row
1c6b1 20 64 61 74 61 20 69 73 20 6e 6f 74 20 6f 76 65   data is not ove
1c6b2 72 77 72 69 74 74 65 6e 20 75 6e 74 69 6c 20 74  rwritten until t
1c6b3 68 65 0a 2a 2a 20 70 73 65 75 64 6f 20 74 61 62  he.** pseudo tab
1c6b4 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 28 6f 72  le is closed (or
1c6b5 20 61 20 6e 65 77 20 72 6f 77 20 69 73 20 69 6e   a new row is in
1c6b6 73 65 72 74 65 64 20 69 6e 74 6f 20 69 74 29 2e  serted into it).
1c6b7 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74 68 65  .**.** P3 is the
1c6b8 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64   number of field
1c6b9 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73  s in the records
1c6ba 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74   that will be st
1c6bb 6f 72 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70  ored by.** the p
1c6bc 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a  seudo-table..*/.
1c6bd 63 61 73 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75  case OP_OpenPseu
1c6be 64 6f 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20  do: {.  int i = 
1c6bf 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43  pOp->p1;.  VdbeC
1c6c0 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 61 73  ursor *pCx;.  as
1c6c1 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20  sert( i>=0 );.  
1c6c2 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75  pCx = allocateCu
1c6c3 72 73 6f 72 28 70 2c 20 69 2c 20 70 4f 70 2d 3e  rsor(p, i, pOp->
1c6c4 70 33 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 69 66  p3, -1, 0);.  if
1c6c5 28 20 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20  ( pCx==0 ) goto 
1c6c6 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 78 2d 3e 6e  no_mem;.  pCx->n
1c6c7 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 70 43  ullRow = 1;.  pC
1c6c8 78 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 3d  x->pseudoTable =
1c6c9 20 31 3b 0a 20 20 70 43 78 2d 3e 65 70 68 65 6d   1;.  pCx->ephem
1c6ca 50 73 65 75 64 6f 54 61 62 6c 65 20 3d 20 28 75  PseudoTable = (u
1c6cb 38 29 70 4f 70 2d 3e 70 32 3b 0a 20 20 70 43 78  8)pOp->p2;.  pCx
1c6cc 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20  ->isTable = 1;. 
1c6cd 20 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20   pCx->isIndex = 
1c6ce 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  0;.  break;.}../
1c6cf 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f 73 65 20  * Opcode: Close 
1c6d0 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1c6d1 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20   Close a cursor 
1c6d2 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65  previously opene
1c6d3 64 20 61 73 20 50 31 2e 20 20 49 66 20 50 31 20  d as P1.  If P1 
1c6d4 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72 72 65 6e  is not.** curren
1c6d5 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69 73 20 69  tly open, this i
1c6d6 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 20  nstruction is a 
1c6d7 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f  no-op..*/.case O
1c6d8 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20 69 6e 74  P_Close: {.  int
1c6d9 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20   i = pOp->p1;.  
1c6da 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
1c6db 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  i<p->nCursor );.
1c6dc 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65    sqlite3VdbeFre
1c6dd 65 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70  eCursor(p, p->ap
1c6de 43 73 72 5b 69 5d 29 3b 0a 20 20 70 2d 3e 61 70  Csr[i]);.  p->ap
1c6df 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 62 72  Csr[i] = 0;.  br
1c6e0 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1c6e1 65 3a 20 53 65 65 6b 47 65 20 50 31 20 50 32 20  e: SeekGe P1 P2 
1c6e2 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66  P3 P4 *.**.** If
1c6e3 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72   cursor P1 refer
1c6e4 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c  s to an SQL tabl
1c6e5 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75  e (B-Tree that u
1c6e6 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73  ses integer keys
1c6e7 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76  ), .** use the v
1c6e8 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1c6e9 20 50 33 20 61 73 20 74 68 65 20 6b 65 79 2e 20   P3 as the key. 
1c6ea 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65   If cursor P1 re
1c6eb 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53  fers .** to an S
1c6ec 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50  QL index, then P
1c6ed 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69  3 is the first i
1c6ee 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34  n an array of P4
1c6ef 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74   registers .** t
1c6f0 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20  hat are used as 
1c6f1 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65  an unpacked inde
1c6f2 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65  x key. .**.** Re
1c6f3 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20  position cursor 
1c6f4 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 70  P1 so that  it p
1c6f5 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d 61  oints to the sma
1c6f6 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61 74  llest entry that
1c6f7 20 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20   .** is greater 
1c6f8 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
1c6f9 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20   the key value. 
1c6fa 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
1c6fb 72 65 63 6f 72 64 73 20 0a 2a 2a 20 67 72 65 61  records .** grea
1c6fc 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
1c6fd 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64  l to the key and
1c6fe 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c   P2 is not zero,
1c6ff 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
1c700 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1c701 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e  : Found, NotFoun
1c702 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65  d, Distinct, See
1c703 6b 4c 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65  kLt, SeekGt, See
1c704 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  kLe.*/./* Opcode
1c705 3a 20 53 65 65 6b 47 74 20 50 31 20 50 32 20 50  : SeekGt P1 P2 P
1c706 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20  3 P4 *.**.** If 
1c707 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73  cursor P1 refers
1c708 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65   to an SQL table
1c709 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73   (B-Tree that us
1c70a 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29  es integer keys)
1c70b 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61  , .** use the va
1c70c 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1c70d 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20  P3 as a key. If 
1c70e 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73  cursor P1 refers
1c70f 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69   .** to an SQL i
1c710 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73  ndex, then P3 is
1c711 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e   the first in an
1c712 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67   array of P4 reg
1c713 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20  isters .** that 
1c714 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75  are used as an u
1c715 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
1c716 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69  y. .**.** Reposi
1c717 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73  tion cursor P1 s
1c718 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74  o that  it point
1c719 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73  s to the smalles
1c71a 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a  t entry that .**
1c71b 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
1c71c 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20   the key value. 
1c71d 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
1c71e 72 65 63 6f 72 64 73 20 67 72 65 61 74 65 72 20  records greater 
1c71f 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79  than .** the key
1c720 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a   and P2 is not z
1c721 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74  ero, then jump t
1c722 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  o P2..**.** See 
1c723 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74  also: Found, Not
1c724 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c  Found, Distinct,
1c725 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 65 2c   SeekLt, SeekGe,
1c726 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70   SeekLe.*/./* Op
1c727 63 6f 64 65 3a 20 53 65 65 6b 4c 74 20 50 31 20  code: SeekLt P1 
1c728 50 32 20 50 33 20 50 34 20 2a 20 0a 2a 2a 0a 2a  P2 P3 P4 * .**.*
1c729 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72  * If cursor P1 r
1c72a 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20  efers to an SQL 
1c72b 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68  table (B-Tree th
1c72c 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20  at uses integer 
1c72d 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74  keys), .** use t
1c72e 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1c72f 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 79  ster P3 as a key
1c730 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72  . If cursor P1 r
1c731 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20  efers .** to an 
1c732 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20  SQL index, then 
1c733 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20  P3 is the first 
1c734 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50  in an array of P
1c735 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20  4 registers .** 
1c736 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73  that are used as
1c737 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64   an unpacked ind
1c738 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52  ex key. .**.** R
1c739 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72  eposition cursor
1c73a 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20   P1 so that  it 
1c73b 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61  points to the la
1c73c 72 67 65 73 74 20 65 6e 74 72 79 20 74 68 61 74  rgest entry that
1c73d 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61   .** is less tha
1c73e 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e  n the key value.
1c73f 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1c740 20 72 65 63 6f 72 64 73 20 6c 65 73 73 20 74 68   records less th
1c741 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61  an .** the key a
1c742 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72  nd P2 is not zer
1c743 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  o, then jump to 
1c744 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  P2..**.** See al
1c745 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f  so: Found, NotFo
1c746 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53  und, Distinct, S
1c747 65 65 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53  eekGt, SeekGe, S
1c748 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  eekLe.*/./* Opco
1c749 64 65 3a 20 53 65 65 6b 4c 65 20 50 31 20 50 32  de: SeekLe P1 P2
1c74a 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49   P3 P4 *.**.** I
1c74b 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65  f cursor P1 refe
1c74c 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62  rs to an SQL tab
1c74d 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20  le (B-Tree that 
1c74e 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79  uses integer key
1c74f 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20  s), .** use the 
1c750 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1c751 72 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49  r P3 as a key. I
1c752 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65  f cursor P1 refe
1c753 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c  rs .** to an SQL
1c754 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20   index, then P3 
1c755 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20  is the first in 
1c756 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 72  an array of P4 r
1c757 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61  egisters .** tha
1c758 74 20 61 72 65 20 75 73 65 64 20 61 73 20 61 6e  t are used as an
1c759 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20   unpacked index 
1c75a 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f  key. .**.** Repo
1c75b 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31  sition cursor P1
1c75c 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e   so that it poin
1c75d 74 73 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73  ts to the larges
1c75e 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a  t entry that .**
1c75f 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   is less than or
1c760 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65   equal to the ke
1c761 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72  y value. If ther
1c762 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73  e are no records
1c763 20 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f   .** less than o
1c764 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b  r equal to the k
1c765 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74  ey and P2 is not
1c766 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70   zero, then jump
1c767 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65   to P2..**.** Se
1c768 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e  e also: Found, N
1c769 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63  otFound, Distinc
1c76a 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b 47  t, SeekGt, SeekG
1c76b 65 2c 20 53 65 65 6b 4c 74 0a 2a 2f 0a 63 61 73  e, SeekLt.*/.cas
1c76c 65 20 4f 50 5f 53 65 65 6b 4c 74 3a 20 20 20 20  e OP_SeekLt:    
1c76d 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1c76e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65  3 */.case OP_See
1c76f 6b 4c 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20  kLe:         /* 
1c770 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73  jump, in3 */.cas
1c771 65 20 4f 50 5f 53 65 65 6b 47 65 3a 20 20 20 20  e OP_SeekGe:    
1c772 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1c773 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65  3 */.case OP_See
1c774 6b 47 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20  kGt: {       /* 
1c775 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69  jump, in3 */.  i
1c776 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
1c777 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c778 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  ;..  assert( i>=
1c779 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
1c77a 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
1c77b 4f 70 2d 3e 70 32 21 3d 30 20 29 3b 0a 20 20 70  Op->p2!=0 );.  p
1c77c 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b  C = p->apCsr[i];
1c77d 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30  .  assert( pC!=0
1c77e 20 29 3b 0a 20 20 69 66 28 20 70 43 2d 3e 70 43   );.  if( pC->pC
1c77f 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20  ursor!=0 ){.    
1c780 69 6e 74 20 72 65 73 2c 20 6f 63 3b 0a 20 20 20  int res, oc;.   
1c781 20 6f 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64   oc = pOp->opcod
1c782 65 3b 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52  e;.    pC->nullR
1c783 6f 77 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  ow = 0;.    if( 
1c784 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 7b 0a 20  pC->isTable ){. 
1c785 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20       i64 iKey;  
1c786 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64      /* The rowid
1c787 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20   we are to seek 
1c788 74 6f 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20  to */..      /* 
1c789 54 68 65 20 69 6e 70 75 74 20 76 61 6c 75 65 20  The input value 
1c78a 69 6e 20 50 33 20 6d 69 67 68 74 20 62 65 20 6f  in P3 might be o
1c78b 66 20 61 6e 79 20 74 79 70 65 3a 20 69 6e 74 65  f any type: inte
1c78c 67 65 72 2c 20 72 65 61 6c 2c 20 73 74 72 69 6e  ger, real, strin
1c78d 67 2c 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 62  g,.      ** blob
1c78e 2c 20 6f 72 20 4e 55 4c 4c 2e 20 20 42 75 74 20  , or NULL.  But 
1c78f 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61  it needs to be a
1c790 6e 20 69 6e 74 65 67 65 72 20 62 65 66 6f 72 65  n integer before
1c791 20 77 65 20 63 61 6e 20 64 6f 0a 20 20 20 20 20   we can do.     
1c792 20 2a 2a 20 74 68 65 20 73 65 65 6b 2c 20 73 6f   ** the seek, so
1c793 20 63 6f 76 65 72 74 20 69 74 2e 20 2a 2f 0a 20   covert it. */. 
1c794 20 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69       applyNumeri
1c795 63 41 66 66 69 6e 69 74 79 28 70 49 6e 33 29 3b  cAffinity(pIn3);
1c796 0a 20 20 20 20 20 20 69 4b 65 79 20 3d 20 73 71  .      iKey = sq
1c797 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1c798 65 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 70  e(pIn3);.      p
1c799 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1c79a 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49  = 0;..      /* I
1c79b 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 63  f the P3 value c
1c79c 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e 76  ould not be conv
1c79d 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e  erted into an in
1c79e 74 65 67 65 72 20 77 69 74 68 6f 75 74 0a 20 20  teger without.  
1c79f 20 20 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 69      ** loss of i
1c7a0 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 6e  nformation, then
1c7a1 20 73 70 65 63 69 61 6c 20 70 72 6f 63 65 73 73   special process
1c7a2 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 2e  ing is required.
1c7a3 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  .. */.      if( 
1c7a4 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn3->flags & M
1c7a5 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20  EM_Int)==0 ){.  
1c7a6 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d        if( (pIn3-
1c7a7 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
1c7a8 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
1c7a9 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33 20     /* If the P3 
1c7aa 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 20  value cannot be 
1c7ab 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61  converted into a
1c7ac 6e 79 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75 6d  ny kind of a num
1c7ad 62 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 2a  ber,.          *
1c7ae 2a 20 74 68 65 6e 20 74 68 65 20 73 65 65 6b 20  * then the seek 
1c7af 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c  is not possible,
1c7b0 20 73 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a   so jump to P2 *
1c7b1 2f 0a 20 20 20 20 20 20 20 20 20 20 70 63 20 3d  /.          pc =
1c7b2 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1c7b3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1c7b4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1c7b5 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20   /* If we reach 
1c7b6 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e  this point, then
1c7b7 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 6d 75   the P3 value mu
1c7b8 73 74 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67  st be a floating
1c7b9 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e  .        ** poin
1c7ba 74 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20  t number. */.   
1c7bb 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 49       assert( (pI
1c7bc 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n3->flags & MEM_
1c7bd 52 65 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 20  Real)!=0 );..   
1c7be 20 20 20 20 20 69 66 28 20 69 4b 65 79 3d 3d 53       if( iKey==S
1c7bf 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 26 26  MALLEST_INT64 &&
1c7c0 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f 75 62 6c   (pIn3->r<(doubl
1c7c1 65 29 69 4b 65 79 20 7c 7c 20 70 49 6e 33 2d 3e  e)iKey || pIn3->
1c7c2 72 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  r>0) ){.        
1c7c3 20 20 2f 2a 20 54 68 65 20 50 33 20 76 61 6c 75    /* The P3 valu
1c7c4 65 20 69 73 20 74 6f 20 6c 61 72 67 65 20 69 6e  e is to large in
1c7c5 20 6d 61 67 6e 69 74 75 64 65 20 74 6f 20 62 65   magnitude to be
1c7c6 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 6e   expressed as an
1c7c7 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  .          ** in
1c7c8 74 65 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20  teger. */.      
1c7c9 20 20 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 20      res = 1;.   
1c7ca 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d         if( pIn3-
1c7cb 3e 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  >r<0 ){.        
1c7cc 20 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53      if( oc==OP_S
1c7cd 65 65 6b 47 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f  eekGt || oc==OP_
1c7ce 53 65 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20  SeekGe ){.      
1c7cf 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1c7d0 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 70  ite3BtreeFirst(p
1c7d1 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73  C->pCursor, &res
1c7d2 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
1c7d3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1c7d4 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  OK ) goto abort_
1c7d5 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1c7d6 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1c7d7 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1c7d8 20 20 20 20 20 20 20 20 20 69 66 28 20 6f 63 3d           if( oc=
1c7d9 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f 63  =OP_SeekLt || oc
1c7da 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20  ==OP_SeekLe ){. 
1c7db 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20               rc 
1c7dc 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61  = sqlite3BtreeLa
1c7dd 73 74 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  st(pC->pCursor, 
1c7de 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20  &res);.         
1c7df 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1c7e0 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62  ITE_OK ) goto ab
1c7e1 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1c7e2 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
1c7e3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1c7e4 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29 7b        if( res ){
1c7e5 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 63 20  .            pc 
1c7e6 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1c7e7 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1c7e8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1c7e9 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 63      }else if( oc
1c7ea 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f  ==OP_SeekLt || o
1c7eb 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 0a  c==OP_SeekGe ){.
1c7ec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
1c7ed 20 74 68 65 20 63 65 69 6c 69 6e 67 28 29 20 66   the ceiling() f
1c7ee 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65  unction to conve
1c7ef 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a  rt real->int */.
1c7f0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49            if( pI
1c7f1 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 62 6c 65 29  n3->r > (double)
1c7f2 69 4b 65 79 20 29 20 69 4b 65 79 2b 2b 3b 0a 20  iKey ) iKey++;. 
1c7f3 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1c7f4 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74          /* Use t
1c7f5 68 65 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74  he floor() funct
1c7f6 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72  ion to convert r
1c7f7 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20  eal->int */.    
1c7f8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63        assert( oc
1c7f9 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c 7c 20 6f  ==OP_SeekLe || o
1c7fa 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b 0a  c==OP_SeekGt );.
1c7fb 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49            if( pI
1c7fc 6e 33 2d 3e 72 20 3c 20 28 64 6f 75 62 6c 65 29  n3->r < (double)
1c7fd 69 4b 65 79 20 29 20 69 4b 65 79 2d 2d 3b 0a 20  iKey ) iKey--;. 
1c7fe 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1c7ff 20 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c   .      rc = sql
1c800 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
1c801 6e 70 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72  npacked(pC->pCur
1c802 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 69 4b 65  sor, 0, (u64)iKe
1c803 79 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20  y, 0, &res);.   
1c804 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1c805 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1c806 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1c807 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d  o_error;.      }
1c808 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 3d 3d  .      if( res==
1c809 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 2d  0 ){.        pC-
1c80a 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1c80b 31 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6c  1;.        pC->l
1c80c 61 73 74 52 6f 77 69 64 20 3d 20 69 4b 65 79 3b  astRowid = iKey;
1c80d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
1c80e 73 65 7b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b  se{.      Unpack
1c80f 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20  edRecord r;.    
1c810 20 20 69 6e 74 20 6e 46 69 65 6c 64 20 3d 20 70    int nField = p
1c811 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20  Op->p4.i;.      
1c812 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1c813 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b  ype==P4_INT32 );
1c814 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
1c815 46 69 65 6c 64 3e 30 20 29 3b 0a 20 20 20 20 20  Field>0 );.     
1c816 20 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43   r.pKeyInfo = pC
1c817 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20  ->pKeyInfo;.    
1c818 20 20 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31    r.nField = (u1
1c819 36 29 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20  6)nField;.      
1c81a 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47  if( oc==OP_SeekG
1c81b 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b  t || oc==OP_Seek
1c81c 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 2e  Le ){.        r.
1c81d 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44  flags = UNPACKED
1c81e 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20 20 20 20  _INCRKEY;.      
1c81f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
1c820 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20  .flags = 0;.    
1c821 20 20 7d 0a 20 20 20 20 20 20 72 2e 61 4d 65 6d    }.      r.aMem
1c822 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1c823 3e 70 33 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d  >p3];.      rc =
1c824 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1c825 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 2d 3e  etoUnpacked(pC->
1c826 70 43 75 72 73 6f 72 2c 20 26 72 2c 20 30 2c 20  pCursor, &r, 0, 
1c827 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20  0, &res);.      
1c828 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1c829 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
1c82a 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1c82b 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rror;.      }.  
1c82c 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56      pC->rowidIsV
1c82d 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  alid = 0;.    }.
1c82e 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64      pC->deferred
1c82f 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20  Moveto = 0;.    
1c830 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1c831 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 23  = CACHE_STALE;.#
1c832 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1c833 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65  T.    sqlite3_se
1c834 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  arch_count++;.#e
1c835 6e 64 69 66 0a 20 20 20 20 69 66 28 20 6f 63 3d  ndif.    if( oc=
1c836 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 6f 63  =OP_SeekGe || oc
1c837 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 7b 0a 20  ==OP_SeekGt ){. 
1c838 20 20 20 20 20 69 66 28 20 72 65 73 3c 30 20 7c       if( res<0 |
1c839 7c 20 28 72 65 73 3d 3d 30 20 26 26 20 6f 63 3d  | (res==0 && oc=
1c83a 3d 4f 50 5f 53 65 65 6b 47 74 29 20 29 7b 0a 20  =OP_SeekGt) ){. 
1c83b 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1c83c 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43 2d  te3BtreeNext(pC-
1c83d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b  >pCursor, &res);
1c83e 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
1c83f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
1c840 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1c841 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 70 43  rror;.        pC
1c842 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
1c843 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   0;.      }else{
1c844 0a 20 20 20 20 20 20 20 20 72 65 73 20 3d 20 30  .        res = 0
1c845 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
1c846 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
1c847 74 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74  t( oc==OP_SeekLt
1c848 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c   || oc==OP_SeekL
1c849 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  e );.      if( r
1c84a 65 73 3e 30 20 7c 7c 20 28 72 65 73 3d 3d 30 20  es>0 || (res==0 
1c84b 26 26 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74  && oc==OP_SeekLt
1c84c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  ) ){.        rc 
1c84d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72  = sqlite3BtreePr
1c84e 65 76 69 6f 75 73 28 70 43 2d 3e 70 43 75 72 73  evious(pC->pCurs
1c84f 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  or, &res);.     
1c850 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1c851 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72  E_OK ) goto abor
1c852 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1c853 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69          pC->rowi
1c854 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1c855 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1c856 20 20 20 2f 2a 20 72 65 73 20 6d 69 67 68 74 20     /* res might 
1c857 62 65 20 6e 65 67 61 74 69 76 65 20 62 65 63 61  be negative beca
1c858 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73  use the table is
1c859 20 65 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74   empty.  Check t
1c85a 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65  o.        ** see
1c85b 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20   if this is the 
1c85c 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  case..        */
1c85d 0a 20 20 20 20 20 20 20 20 72 65 73 20 3d 20 73  .        res = s
1c85e 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 70  qlite3BtreeEof(p
1c85f 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20  C->pCursor);.   
1c860 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
1c861 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
1c862 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20   );.    if( res 
1c863 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
1c864 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d  p->p2 - 1;.    }
1c865 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 43  .  }else if( !pC
1c866 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 7b  ->pseudoTable ){
1c867 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70  .    /* This hap
1c868 70 65 6e 73 20 77 68 65 6e 20 61 74 74 65 6d 70  pens when attemp
1c869 74 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65  ting to open the
1c86a 20 73 71 6c 69 74 65 33 5f 6d 61 73 74 65 72 20   sqlite3_master 
1c86b 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72  table.    ** for
1c86c 20 72 65 61 64 20 61 63 63 65 73 73 20 72 65 74   read access ret
1c86d 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54  urns SQLITE_EMPT
1c86e 59 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  Y. In this case 
1c86f 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 74 61  always.    ** ta
1c870 6b 65 20 74 68 65 20 6a 75 6d 70 20 28 73 69 6e  ke the jump (sin
1c871 63 65 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  ce there are no 
1c872 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74  records in the t
1c873 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  able)..    */.  
1c874 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1c875 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1c876 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
1c877 65 65 6b 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  eek P1 P2 * * *.
1c878 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 6f  **.** P1 is an o
1c879 70 65 6e 20 74 61 62 6c 65 20 63 75 72 73 6f 72  pen table cursor
1c87a 20 61 6e 64 20 50 32 20 69 73 20 61 20 72 6f 77   and P2 is a row
1c87b 69 64 20 69 6e 74 65 67 65 72 2e 20 20 41 72 72  id integer.  Arr
1c87c 61 6e 67 65 0a 2a 2a 20 66 6f 72 20 50 31 20 74  ange.** for P1 t
1c87d 6f 20 6d 6f 76 65 20 73 6f 20 74 68 61 74 20 69  o move so that i
1c87e 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
1c87f 72 6f 77 69 64 20 67 69 76 65 6e 20 62 79 20 50  rowid given by P
1c880 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  2..**.** This is
1c881 20 61 63 74 75 61 6c 6c 79 20 61 20 64 65 66 65   actually a defe
1c882 72 72 65 64 20 73 65 65 6b 2e 20 20 4e 6f 74 68  rred seek.  Noth
1c883 69 6e 67 20 61 63 74 75 61 6c 6c 79 20 68 61 70  ing actually hap
1c884 70 65 6e 73 20 75 6e 74 69 6c 0a 2a 2a 20 74 68  pens until.** th
1c885 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64  e cursor is used
1c886 20 74 6f 20 72 65 61 64 20 61 20 72 65 63 6f 72   to read a recor
1c887 64 2e 20 20 54 68 61 74 20 77 61 79 2c 20 69 66  d.  That way, if
1c888 20 6e 6f 20 72 65 61 64 73 0a 2a 2a 20 6f 63 63   no reads.** occ
1c889 75 72 2c 20 6e 6f 20 75 6e 6e 65 63 65 73 73 61  ur, no unnecessa
1c88a 72 79 20 49 2f 4f 20 68 61 70 70 65 6e 73 2e 0a  ry I/O happens..
1c88b 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 3a  */.case OP_Seek:
1c88c 20 7b 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a   {    /* in2 */.
1c88d 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
1c88e 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  1;.  VdbeCursor 
1c88f 2a 70 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  *pC;..  assert( 
1c890 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
1c891 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70  rsor );.  pC = p
1c892 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73  ->apCsr[i];.  as
1c893 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20  sert( pC!=0 );. 
1c894 20 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72   if( pC->pCursor
1c895 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  !=0 ){.    asser
1c896 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 29  t( pC->isTable )
1c897 3b 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f  ;.    pC->nullRo
1c898 77 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 6d  w = 0;.    pC->m
1c899 6f 76 65 74 6f 54 61 72 67 65 74 20 3d 20 73 71  ovetoTarget = sq
1c89a 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1c89b 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 70 43 2d  e(pIn2);.    pC-
1c89c 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1c89d 30 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72  0;.    pC->defer
1c89e 72 65 64 4d 6f 76 65 74 6f 20 3d 20 31 3b 0a 20  redMoveto = 1;. 
1c89f 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 20 20   }.  break;.}.  
1c8a0 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75  ../* Opcode: Fou
1c8a1 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  nd P1 P2 P3 * *.
1c8a2 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50  **.** Register P
1c8a3 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63  3 holds a blob c
1c8a4 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d 61  onstructed by Ma
1c8a5 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73  keRecord.  P1 is
1c8a6 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66   an index..** If
1c8a7 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 6d   an entry that m
1c8a8 61 74 63 68 65 73 20 74 68 65 20 76 61 6c 75 65  atches the value
1c8a9 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 33 20   in register p3 
1c8aa 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 65  exists in P1 the
1c8ab 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e  n.** jump to P2.
1c8ac 20 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75    If the P3 valu
1c8ad 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  e does not match
1c8ae 20 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31   any entry in P1
1c8af 0a 2a 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68  .** then fall th
1c8b0 72 75 2e 20 20 54 68 65 20 50 31 20 63 75 72 73  ru.  The P1 curs
1c8b1 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  or is left point
1c8b2 69 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68  ing at the match
1c8b3 69 6e 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20  ing entry.** if 
1c8b4 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a  it exists..**.**
1c8b5 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1c8b6 6e 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70  n is used to imp
1c8b7 6c 65 6d 65 6e 74 20 74 68 65 20 49 4e 20 6f 70  lement the IN op
1c8b8 65 72 61 74 6f 72 20 77 68 65 72 65 20 74 68 65  erator where the
1c8b9 0a 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69  .** left-hand si
1c8ba 64 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73  de is a SELECT s
1c8bb 74 61 74 65 6d 65 6e 74 2e 20 20 50 31 20 6d 61  tatement.  P1 ma
1c8bc 79 20 62 65 20 61 20 74 72 75 65 20 69 6e 64 65  y be a true inde
1c8bd 78 2c 20 6f 72 20 69 74 0a 2a 2a 20 6d 61 79 20  x, or it.** may 
1c8be 62 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69  be a temporary i
1c8bf 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20  ndex that holds 
1c8c0 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  the results of t
1c8c1 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61  he SELECT.** sta
1c8c2 74 65 6d 65 6e 74 2e 20 20 20 54 68 69 73 20 69  tement.   This i
1c8c3 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c  nstruction is al
1c8c4 73 6f 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  so used to imple
1c8c5 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 44 49 53 54  ment the.** DIST
1c8c6 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 6e 20  INCT keyword in 
1c8c7 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1c8c8 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  s..**.** This in
1c8c9 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73  struction checks
1c8ca 20 69 66 20 69 6e 64 65 78 20 50 31 20 63 6f 6e   if index P1 con
1c8cb 74 61 69 6e 73 20 61 20 72 65 63 6f 72 64 20 66  tains a record f
1c8cc 6f 72 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 65  or which .** the
1c8cd 20 66 69 72 73 74 20 4e 20 73 65 72 69 61 6c 69   first N seriali
1c8ce 7a 65 64 20 76 61 6c 75 65 73 20 65 78 61 63 74  zed values exact
1c8cf 6c 79 20 6d 61 74 63 68 20 74 68 65 20 4e 20 73  ly match the N s
1c8d0 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73  erialized values
1c8d1 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 63 6f 72  .** in the recor
1c8d2 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
1c8d3 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
1c8d4 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
1c8d5 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 74 68   values in.** th
1c8d6 65 20 50 33 20 72 65 63 6f 72 64 20 28 74 68 65  e P3 record (the
1c8d7 20 50 33 20 72 65 63 6f 72 64 20 69 73 20 61 20   P3 record is a 
1c8d8 70 72 65 66 69 78 20 6f 66 20 74 68 65 20 50 31  prefix of the P1
1c8d9 20 72 65 63 6f 72 64 29 2e 20 0a 2a 2a 0a 2a 2a   record). .**.**
1c8da 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f   See also: NotFo
1c8db 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 2c 20 4e  und, IsUnique, N
1c8dc 6f 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20 4f  otExists.*/./* O
1c8dd 70 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20  pcode: NotFound 
1c8de 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1c8df 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 68  ** Register P3 h
1c8e0 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73  olds a blob cons
1c8e1 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52  tructed by MakeR
1c8e2 65 63 6f 72 64 2e 20 20 50 31 20 69 73 0a 2a 2a  ecord.  P1 is.**
1c8e3 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20 6e   an index.  If n
1c8e4 6f 20 65 6e 74 72 79 20 65 78 69 73 74 73 20 69  o entry exists i
1c8e5 6e 20 50 31 20 74 68 61 74 20 6d 61 74 63 68 65  n P1 that matche
1c8e6 73 20 74 68 65 20 62 6c 6f 62 20 74 68 65 6e 20  s the blob then 
1c8e7 6a 75 6d 70 0a 2a 2a 20 74 6f 20 50 32 2e 20 20  jump.** to P2.  
1c8e8 49 66 20 61 6e 20 65 6e 74 72 79 20 64 6f 65 73  If an entry does
1c8e9 20 65 78 69 73 74 69 6e 67 2c 20 66 61 6c 6c 20   existing, fall 
1c8ea 74 68 72 6f 75 67 68 2e 20 20 54 68 65 20 63 75  through.  The cu
1c8eb 72 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a 2a 20  rsor is left.** 
1c8ec 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
1c8ed 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68  entry that match
1c8ee 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  es..**.** See al
1c8ef 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78  so: Found, NotEx
1c8f0 69 73 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a  ists, IsUnique.*
1c8f1 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75  /.case OP_NotFou
1c8f2 6e 64 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  nd:       /* jum
1c8f3 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  p, in3 */.case O
1c8f4 50 5f 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20  P_Found: {      
1c8f5 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1c8f6 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  /.  int i = pOp-
1c8f7 3e 70 31 3b 0a 20 20 69 6e 74 20 61 6c 72 65 61  >p1;.  int alrea
1c8f8 64 79 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20  dyExists = 0;.  
1c8f9 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c8fa 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1c8fb 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1c8fc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
1c8fd 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  pCsr[i]!=0 );.  
1c8fe 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43  if( (pC = p->apC
1c8ff 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21  sr[i])->pCursor!
1c900 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  =0 ){.    int re
1c901 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52  s;.    UnpackedR
1c902 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a  ecord *pIdxKey;.
1c903 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1c904 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  >isTable==0 );. 
1c905 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d     assert( pIn3-
1c906 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
1c907 62 20 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79  b );.    pIdxKey
1c908 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1c909 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 2d 3e 70  cordUnpack(pC->p
1c90a 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e  KeyInfo, pIn3->n
1c90b 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20  , pIn3->z,.     
1c90c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c90d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c90e 20 61 54 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f   aTempRec, sizeo
1c90f 66 28 61 54 65 6d 70 52 65 63 29 29 3b 0a 20 20  f(aTempRec));.  
1c910 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30    if( pIdxKey==0
1c911 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e   ){.      goto n
1c912 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  o_mem;.    }.   
1c913 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1c914 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20  ==OP_Found ){.  
1c915 20 20 20 20 70 49 64 78 4b 65 79 2d 3e 66 6c 61      pIdxKey->fla
1c916 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f 50  gs |= UNPACKED_P
1c917 52 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20  REFIX_MATCH;.   
1c918 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
1c919 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
1c91a 70 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73  packed(pC->pCurs
1c91b 6f 72 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20  or, pIdxKey, 0, 
1c91c 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 73 71  0, &res);.    sq
1c91d 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55  lite3VdbeDeleteU
1c91e 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49  npackedRecord(pI
1c91f 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20  dxKey);.    if( 
1c920 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1c921 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1c922 20 20 7d 0a 20 20 20 20 61 6c 72 65 61 64 79 45    }.    alreadyE
1c923 78 69 73 74 73 20 3d 20 28 72 65 73 3d 3d 30 29  xists = (res==0)
1c924 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72  ;.    pC->deferr
1c925 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20  edMoveto = 0;.  
1c926 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75    pC->cacheStatu
1c927 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1c928 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e  .  }.  if( pOp->
1c929 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64  opcode==OP_Found
1c92a 20 29 7b 0a 20 20 20 20 69 66 28 20 61 6c 72 65   ){.    if( alre
1c92b 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20 3d  adyExists ) pc =
1c92c 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1c92d 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 21  }else{.    if( !
1c92e 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20  alreadyExists ) 
1c92f 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1c930 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1c931 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55  ../* Opcode: IsU
1c932 6e 69 71 75 65 20 50 31 20 50 32 20 50 33 20 50  nique P1 P2 P3 P
1c933 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 75 72 73 6f 72  4 *.**.** Cursor
1c934 20 50 31 20 69 73 20 6f 70 65 6e 20 6f 6e 20 61   P1 is open on a
1c935 6e 20 69 6e 64 65 78 2e 20 20 53 6f 20 69 74 20  n index.  So it 
1c936 68 61 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20  has no data and 
1c937 69 74 73 20 6b 65 79 20 63 6f 6e 73 69 73 74 73  its key consists
1c938 20 0a 2a 2a 20 6f 66 20 61 20 72 65 63 6f 72 64   .** of a record
1c939 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 4f 50   generated by OP
1c93a 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77 68 65 72  _MakeRecord wher
1c93b 65 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64  e the last field
1c93c 20 69 73 20 74 68 65 20 0a 2a 2a 20 72 6f 77 69   is the .** rowi
1c93d 64 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 74  d of the entry t
1c93e 68 61 74 20 74 68 65 20 69 6e 64 65 78 20 72 65  hat the index re
1c93f 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54  fers to..**.** T
1c940 68 65 20 50 33 20 72 65 67 69 73 74 65 72 20 63  he P3 register c
1c941 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e 74 65 67  ontains an integ
1c942 65 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  er record number
1c943 2e 20 43 61 6c 6c 20 74 68 69 73 20 72 65 63 6f  . Call this reco
1c944 72 64 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 2e  rd .** number R.
1c945 20 52 65 67 69 73 74 65 72 20 50 34 20 69 73 20   Register P4 is 
1c946 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 73  the first in a s
1c947 65 74 20 6f 66 20 4e 20 63 6f 6e 74 69 67 75 6f  et of N contiguo
1c948 75 73 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20  us registers.** 
1c949 74 68 61 74 20 6d 61 6b 65 20 75 70 20 61 6e 20  that make up an 
1c94a 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b  unpacked index k
1c94b 65 79 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  ey that can be u
1c94c 73 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20  sed with cursor 
1c94d 50 31 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  P1..** The value
1c94e 20 6f 66 20 4e 20 63 61 6e 20 62 65 20 69 6e 66   of N can be inf
1c94f 65 72 72 65 64 20 66 72 6f 6d 20 74 68 65 20 63  erred from the c
1c950 75 72 73 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 65  ursor. N include
1c951 73 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 76  s the rowid.** v
1c952 61 6c 75 65 20 61 70 70 65 6e 64 65 64 20 74 6f  alue appended to
1c953 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1c954 69 6e 64 65 78 20 72 65 63 6f 72 64 2e 20 54 68  index record. Th
1c955 69 73 20 72 6f 77 69 64 20 76 61 6c 75 65 20 6d  is rowid value m
1c956 61 79 0a 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74  ay.** or may not
1c957 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20   be the same as 
1c958 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20  R..**.** If any 
1c959 6f 66 20 74 68 65 20 4e 20 72 65 67 69 73 74 65  of the N registe
1c95a 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  rs beginning wit
1c95b 68 20 72 65 67 69 73 74 65 72 20 50 34 20 63 6f  h register P4 co
1c95c 6e 74 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a 2a  ntains a NULL.**
1c95d 20 76 61 6c 75 65 2c 20 6a 75 6d 70 20 69 6d 6d   value, jump imm
1c95e 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
1c95f 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
1c960 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1c961 6e 20 63 68 65 63 6b 73 20 69 66 20 63 75 72 73  n checks if curs
1c962 6f 72 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61  or P1 contains a
1c963 6e 20 65 6e 74 72 79 0a 2a 2a 20 77 68 65 72 65  n entry.** where
1c964 20 74 68 65 20 66 69 72 73 74 20 28 4e 2d 31 29   the first (N-1)
1c965 20 66 69 65 6c 64 73 20 6d 61 74 63 68 20 62 75   fields match bu
1c966 74 20 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75  t the rowid valu
1c967 65 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20  e at the end.** 
1c968 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74  of the index ent
1c969 72 79 20 69 73 20 6e 6f 74 20 52 2e 20 49 66 20  ry is not R. If 
1c96a 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68  there is no such
1c96b 20 65 6e 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20   entry, control 
1c96c 6a 75 6d 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 74  jumps.** to inst
1c96d 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65  ruction P2. Othe
1c96e 72 77 69 73 65 2c 20 74 68 65 20 72 6f 77 69 64  rwise, the rowid
1c96f 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74   of the conflict
1c970 69 6e 67 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74  ing index.** ent
1c971 72 79 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20  ry is copied to 
1c972 72 65 67 69 73 74 65 72 20 50 33 20 61 6e 64 20  register P3 and 
1c973 63 6f 6e 74 72 6f 6c 20 66 61 6c 6c 73 20 74 68  control falls th
1c974 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
1c975 74 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e  t.** instruction
1c976 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1c977 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45  : NotFound, NotE
1c978 78 69 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a  xists, Found.*/.
1c979 63 61 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 65  case OP_IsUnique
1c97a 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1c97b 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 75 31 36  mp, in3 */.  u16
1c97c 20 69 69 3b 0a 20 20 56 64 62 65 43 75 72 73 6f   ii;.  VdbeCurso
1c97d 72 20 2a 70 43 78 3b 0a 20 20 42 74 43 75 72 73  r *pCx;.  BtCurs
1c97e 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 75 31 36  or *pCrsr;.  u16
1c97f 20 6e 46 69 65 6c 64 3b 0a 20 20 4d 65 6d 20 2a   nField;.  Mem *
1c980 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  aMem = &p->aMem[
1c981 70 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 0a 20 20 2f  pOp->p4.i];..  /
1c982 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68  * Assert that th
1c983 65 20 76 61 6c 75 65 73 20 6f 66 20 70 61 72 61  e values of para
1c984 6d 65 74 65 72 73 20 50 31 20 61 6e 64 20 50 34  meters P1 and P4
1c985 20 61 72 65 20 69 6e 20 72 61 6e 67 65 2e 20 2a   are in range. *
1c986 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
1c987 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33  >p4type==P4_INT3
1c988 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  2 );.  assert( p
1c989 4f 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70 4f  Op->p4.i>0 && pO
1c98a 70 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65 6d  p->p4.i<=p->nMem
1c98b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1c98c 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1c98d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
1c98e 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65  ;..  /* Find the
1c98f 20 69 6e 64 65 78 20 63 75 72 73 6f 72 2e 20 2a   index cursor. *
1c990 2f 0a 20 20 70 43 78 20 3d 20 70 2d 3e 61 70 43  /.  pCx = p->apC
1c991 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
1c992 73 73 65 72 74 28 20 70 43 78 2d 3e 64 65 66 65  ssert( pCx->defe
1c993 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b  rredMoveto==0 );
1c994 0a 20 20 70 43 78 2d 3e 73 65 65 6b 52 65 73 75  .  pCx->seekResu
1c995 6c 74 20 3d 20 30 3b 0a 20 20 70 43 78 2d 3e 63  lt = 0;.  pCx->c
1c996 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
1c997 48 45 5f 53 54 41 4c 45 3b 0a 20 20 70 43 72 73  HE_STALE;.  pCrs
1c998 72 20 3d 20 70 43 78 2d 3e 70 43 75 72 73 6f 72  r = pCx->pCursor
1c999 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 6f  ;..  /* If any o
1c99a 66 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65  f the values are
1c99b 20 4e 55 4c 4c 2c 20 74 61 6b 65 20 74 68 65 20   NULL, take the 
1c99c 6a 75 6d 70 2e 20 2a 2f 0a 20 20 6e 46 69 65 6c  jump. */.  nFiel
1c99d 64 20 3d 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66  d = pCx->pKeyInf
1c99e 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f 72  o->nField;.  for
1c99f 28 69 69 3d 30 3b 20 69 69 3c 6e 46 69 65 6c 64  (ii=0; ii<nField
1c9a0 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ; ii++){.    if(
1c9a1 20 61 4d 65 6d 5b 69 69 5d 2e 66 6c 61 67 73 20   aMem[ii].flags 
1c9a2 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
1c9a3 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1c9a4 20 2d 20 31 3b 0a 20 20 20 20 20 20 70 43 72 73   - 1;.      pCrs
1c9a5 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65  r = 0;.      bre
1c9a6 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
1c9a7 61 73 73 65 72 74 28 20 28 61 4d 65 6d 5b 6e 46  assert( (aMem[nF
1c9a8 69 65 6c 64 5d 2e 66 6c 61 67 73 20 26 20 4d 45  ield].flags & ME
1c9a9 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 0a 20  M_Null)==0 );.. 
1c9aa 20 69 66 28 20 70 43 72 73 72 21 3d 30 20 29 7b   if( pCrsr!=0 ){
1c9ab 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63  .    UnpackedRec
1c9ac 6f 72 64 20 72 3b 20 20 20 20 20 20 20 20 20 20  ord r;          
1c9ad 20 20 20 20 20 20 20 20 2f 2a 20 42 2d 54 72 65          /* B-Tre
1c9ae 65 20 69 6e 64 65 78 20 73 65 61 72 63 68 20 6b  e index search k
1c9af 65 79 20 2a 2f 0a 20 20 20 20 69 36 34 20 52 3b  ey */.    i64 R;
1c9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c9b1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c9b2 52 6f 77 69 64 20 73 74 6f 72 65 64 20 69 6e 20  Rowid stored in 
1c9b3 72 65 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 0a  register P3 */..
1c9b4 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20      /* Populate 
1c9b5 74 68 65 20 69 6e 64 65 78 20 73 65 61 72 63 68  the index search
1c9b6 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 72 2e 70   key. */.    r.p
1c9b7 4b 65 79 49 6e 66 6f 20 3d 20 70 43 78 2d 3e 70  KeyInfo = pCx->p
1c9b8 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72 2e 6e  KeyInfo;.    r.n
1c9b9 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 20 2b  Field = nField +
1c9ba 20 31 3b 0a 20 20 20 20 72 2e 66 6c 61 67 73 20   1;.    r.flags 
1c9bb 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  = UNPACKED_PREFI
1c9bc 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 20 72 2e  X_SEARCH;.    r.
1c9bd 61 4d 65 6d 20 3d 20 61 4d 65 6d 3b 0a 0a 20 20  aMem = aMem;..  
1c9be 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68 65    /* Extract the
1c9bf 20 76 61 6c 75 65 20 6f 66 20 52 20 66 72 6f 6d   value of R from
1c9c0 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 2a 2f   register P3. */
1c9c1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1c9c2 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49  MemIntegerify(pI
1c9c3 6e 33 29 3b 0a 20 20 20 20 52 20 3d 20 70 49 6e  n3);.    R = pIn
1c9c4 33 2d 3e 75 2e 69 3b 0a 0a 20 20 20 20 2f 2a 20  3->u.i;..    /* 
1c9c5 53 65 61 72 63 68 20 74 68 65 20 42 2d 54 72 65  Search the B-Tre
1c9c6 65 20 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 63  e index. If no c
1c9c7 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72  onflicting recor
1c9c8 64 20 69 73 20 66 6f 75 6e 64 2c 20 6a 75 6d 70  d is found, jump
1c9c9 0a 20 20 20 20 2a 2a 20 74 6f 20 50 32 2e 20 4f  .    ** to P2. O
1c9ca 74 68 65 72 77 69 73 65 2c 20 63 6f 70 79 20 74  therwise, copy t
1c9cb 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
1c9cc 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f  conflicting reco
1c9cd 72 64 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 67  rd to.    ** reg
1c9ce 69 73 74 65 72 20 50 33 20 61 6e 64 20 66 61 6c  ister P3 and fal
1c9cf 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
1c9d0 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1c9d1 6e 2e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  n.  */.    rc = 
1c9d2 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1c9d3 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 72 73 72  toUnpacked(pCrsr
1c9d4 2c 20 26 72 2c 20 30 2c 20 30 2c 20 26 70 43 78  , &r, 0, 0, &pCx
1c9d5 2d 3e 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 20  ->seekResult);. 
1c9d6 20 20 20 69 66 28 20 28 72 2e 66 6c 61 67 73 20     if( (r.flags 
1c9d7 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  & UNPACKED_PREFI
1c9d8 58 5f 53 45 41 52 43 48 29 20 7c 7c 20 72 2e 72  X_SEARCH) || r.r
1c9d9 6f 77 69 64 3d 3d 52 20 29 7b 0a 20 20 20 20 20  owid==R ){.     
1c9da 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1c9db 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
1c9dc 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69 20 3d 20      pIn3->u.i = 
1c9dd 72 2e 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20  r.rowid;.    }. 
1c9de 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1c9df 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 78 69  * Opcode: NotExi
1c9e0 73 74 73 20 50 31 20 50 32 20 50 33 20 2a 20 2a  sts P1 P2 P3 * *
1c9e1 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 63  .**.** Use the c
1c9e2 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74  ontent of regist
1c9e3 65 72 20 50 33 20 61 73 20 61 20 69 6e 74 65 67  er P3 as a integ
1c9e4 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20 72 65  er key.  If a re
1c9e5 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20 74 68  cord .** with th
1c9e6 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20  at key does not 
1c9e7 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65 20 6f  exist in table o
1c9e8 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d 70 20  f P1, then jump 
1c9e9 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20 74 68  to P2. .** If th
1c9ea 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20 65 78  e record does ex
1c9eb 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74  ist, then fall t
1c9ec 68 72 75 2e 20 20 54 68 65 20 63 75 72 73 6f 72  hru.  The cursor
1c9ed 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70 6f 69   is left .** poi
1c9ee 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 65 63  nting to the rec
1c9ef 6f 72 64 20 69 66 20 69 74 20 65 78 69 73 74 73  ord if it exists
1c9f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66  ..**.** The diff
1c9f1 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74  erence between t
1c9f2 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e  his operation an
1c9f3 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74 68  d NotFound is th
1c9f4 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61  at this.** opera
1c9f5 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65  tion assumes the
1c9f6 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 65 67   key is an integ
1c9f7 65 72 20 61 6e 64 20 74 68 61 74 20 50 31 20 69  er and that P1 i
1c9f8 73 20 61 20 74 61 62 6c 65 20 77 68 65 72 65 61  s a table wherea
1c9f9 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61 73  s.** NotFound as
1c9fa 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61 20 62  sumes key is a b
1c9fb 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20  lob constructed 
1c9fc 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64 20  from MakeRecord 
1c9fd 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20  and.** P1 is an 
1c9fe 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  index..**.** See
1c9ff 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f   also: Found, No
1ca00 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75 65  tFound, IsUnique
1ca01 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 45  .*/.case OP_NotE
1ca02 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20 20 20  xists: {        
1ca03 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a  /* jump, in3 */.
1ca04 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
1ca05 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  1;.  VdbeCursor 
1ca06 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  *pC;.  BtCursor 
1ca07 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74  *pCrsr;.  assert
1ca08 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
1ca09 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65  Cursor );.  asse
1ca0a 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21  rt( p->apCsr[i]!
1ca0b 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72  =0 );.  if( (pCr
1ca0c 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70  sr = (pC = p->ap
1ca0d 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72  Csr[i])->pCursor
1ca0e 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )!=0 ){.    int 
1ca0f 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 36 34  res = 0;.    u64
1ca10 20 69 4b 65 79 3b 0a 20 20 20 20 61 73 73 65 72   iKey;.    asser
1ca11 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26  t( pIn3->flags &
1ca12 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20   MEM_Int );.    
1ca13 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
1ca14 5b 69 5d 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a  [i]->isTable );.
1ca15 20 20 20 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f      iKey = intTo
1ca16 4b 65 79 28 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a  Key(pIn3->u.i);.
1ca17 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ca18 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
1ca19 6b 65 64 28 70 43 72 73 72 2c 20 30 2c 20 69 4b  ked(pCrsr, 0, iK
1ca1a 65 79 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20  ey, 0, &res);.  
1ca1b 20 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20    pC->lastRowid 
1ca1c 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20  = pIn3->u.i;.   
1ca1d 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69   pC->rowidIsVali
1ca1e 64 20 3d 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b  d = res==0 ?1:0;
1ca1f 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77  .    pC->nullRow
1ca20 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61   = 0;.    pC->ca
1ca21 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1ca22 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 70 43 2d  E_STALE;.    pC-
1ca23 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1ca24 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 65 73  = 0;.    if( res
1ca25 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20  !=0 ){.      pc 
1ca26 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1ca27 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d       assert( pC-
1ca28 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30  >rowidIsValid==0
1ca29 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43   );.    }.    pC
1ca2a 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 72  ->seekResult = r
1ca2b 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  es;.  }else if( 
1ca2c 21 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65  !pC->pseudoTable
1ca2d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
1ca2e 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 6e 20  happens when an 
1ca2f 61 74 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20  attempt to open 
1ca30 61 20 72 65 61 64 20 63 75 72 73 6f 72 20 6f 6e  a read cursor on
1ca31 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 71 6c   the .    ** sql
1ca32 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
1ca33 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
1ca34 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f 0a 20 20  EMPTY..    */.  
1ca35 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73    assert( pC->is
1ca36 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 63 20  Table );.    pc 
1ca37 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1ca38 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72     assert( pC->r
1ca39 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29  owidIsValid==0 )
1ca3a 3b 0a 20 20 20 20 70 43 2d 3e 73 65 65 6b 52 65  ;.    pC->seekRe
1ca3b 73 75 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  sult = 0;.  }.  
1ca3c 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1ca3d 6f 64 65 3a 20 53 65 71 75 65 6e 63 65 20 50 31  ode: Sequence P1
1ca3e 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1ca3f 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 61 76  Find the next av
1ca40 61 69 6c 61 62 6c 65 20 73 65 71 75 65 6e 63 65  ailable sequence
1ca41 20 6e 75 6d 62 65 72 20 66 6f 72 20 63 75 72 73   number for curs
1ca42 6f 72 20 50 31 2e 0a 2a 2a 20 57 72 69 74 65 20  or P1..** Write 
1ca43 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d  the sequence num
1ca44 62 65 72 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ber into registe
1ca45 72 20 50 32 2e 0a 2a 2a 20 54 68 65 20 73 65 71  r P2..** The seq
1ca46 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 6f 6e 20  uence number on 
1ca47 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e  the cursor is in
1ca48 63 72 65 6d 65 6e 74 65 64 20 61 66 74 65 72 20  cremented after 
1ca49 74 68 69 73 0a 2a 2a 20 69 6e 73 74 72 75 63 74  this.** instruct
1ca4a 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f  ion.  .*/.case O
1ca4b 50 5f 53 65 71 75 65 6e 63 65 3a 20 7b 20 20 20  P_Sequence: {   
1ca4c 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1ca4d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1ca4e 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
1ca4f 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1ca50 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
1ca51 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
1ca52 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20  apCsr[i]!=0 );. 
1ca53 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 2d 3e   pOut->u.i = p->
1ca54 61 70 43 73 72 5b 69 5d 2d 3e 73 65 71 43 6f 75  apCsr[i]->seqCou
1ca55 6e 74 2b 2b 3b 0a 20 20 4d 65 6d 53 65 74 54 79  nt++;.  MemSetTy
1ca56 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1ca57 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  _Int);.  break;.
1ca58 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  }.../* Opcode: N
1ca59 65 77 52 6f 77 69 64 20 50 31 20 50 32 20 50 33  ewRowid P1 P2 P3
1ca5a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20 61   * *.**.** Get a
1ca5b 20 6e 65 77 20 69 6e 74 65 67 65 72 20 72 65 63   new integer rec
1ca5c 6f 72 64 20 6e 75 6d 62 65 72 20 28 61 2e 6b 2e  ord number (a.k.
1ca5d 61 20 22 72 6f 77 69 64 22 29 20 75 73 65 64 20  a "rowid") used 
1ca5e 61 73 20 74 68 65 20 6b 65 79 20 74 6f 20 61 20  as the key to a 
1ca5f 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 72 65  table..** The re
1ca60 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 6e  cord number is n
1ca61 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 75 73  ot previously us
1ca62 65 64 20 61 73 20 61 20 6b 65 79 20 69 6e 20 74  ed as a key in t
1ca63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74  he database.** t
1ca64 61 62 6c 65 20 74 68 61 74 20 63 75 72 73 6f 72  able that cursor
1ca65 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20   P1 points to.  
1ca66 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e  The new record n
1ca67 75 6d 62 65 72 20 69 73 20 77 72 69 74 74 65 6e  umber is written
1ca68 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 72  .** written to r
1ca69 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a  egister P2..**.*
1ca6a 2a 20 49 66 20 50 33 3e 30 20 74 68 65 6e 20 50  * If P3>0 then P
1ca6b 33 20 69 73 20 61 20 72 65 67 69 73 74 65 72 20  3 is a register 
1ca6c 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6c  that holds the l
1ca6d 61 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c  argest previousl
1ca6e 79 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 72  y.** generated r
1ca6f 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 4e  ecord number.  N
1ca70 6f 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d  o new record num
1ca71 62 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64  bers are allowed
1ca72 20 74 6f 20 62 65 20 6c 65 73 73 0a 2a 2a 20 74   to be less.** t
1ca73 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2e 20  han this value. 
1ca74 20 57 68 65 6e 20 74 68 69 73 20 76 61 6c 75 65   When this value
1ca75 20 72 65 61 63 68 65 73 20 69 74 73 20 6d 61 78   reaches its max
1ca76 69 6d 75 6d 2c 20 61 20 53 51 4c 49 54 45 5f 46  imum, a SQLITE_F
1ca77 55 4c 4c 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20  ULL.** error is 
1ca78 67 65 6e 65 72 61 74 65 64 2e 20 20 54 68 65 20  generated.  The 
1ca79 50 33 20 72 65 67 69 73 74 65 72 20 69 73 20 75  P3 register is u
1ca7a 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20  pdated with the 
1ca7b 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 72 65 63  generated.** rec
1ca7c 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 54 68 69  ord number.  Thi
1ca7d 73 20 50 33 20 6d 65 63 68 61 6e 69 73 6d 20 69  s P3 mechanism i
1ca7e 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69  s used to help i
1ca7f 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20  mplement the.** 
1ca80 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 66 65  AUTOINCREMENT fe
1ca81 61 74 75 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ature..*/.case O
1ca82 50 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 20 20 20  P_NewRowid: {   
1ca83 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1ca84 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1ca85 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
1ca86 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 20  .  i64 v = 0;.  
1ca87 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1ca88 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1ca89 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1ca8a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
1ca8b 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  pCsr[i]!=0 );.  
1ca8c 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43  if( (pC = p->apC
1ca8d 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 3d  sr[i])->pCursor=
1ca8e 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  =0 ){.    /* The
1ca8f 20 7a 65 72 6f 20 69 6e 69 74 69 61 6c 69 7a 61   zero initializa
1ca90 74 69 6f 6e 20 61 62 6f 76 65 20 69 73 20 61 6c  tion above is al
1ca91 6c 20 74 68 61 74 20 69 73 20 6e 65 65 64 65 64  l that is needed
1ca92 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   */.  }else{.   
1ca93 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 72 6f 77   /* The next row
1ca94 69 64 20 6f 72 20 72 65 63 6f 72 64 20 6e 75 6d  id or record num
1ca95 62 65 72 20 28 64 69 66 66 65 72 65 6e 74 20 74  ber (different t
1ca96 65 72 6d 73 20 66 6f 72 20 74 68 65 20 73 61 6d  erms for the sam
1ca97 65 0a 20 20 20 20 2a 2a 20 74 68 69 6e 67 29 20  e.    ** thing) 
1ca98 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 61  is obtained in a
1ca99 20 74 77 6f 2d 73 74 65 70 20 61 6c 67 6f 72 69   two-step algori
1ca9a 74 68 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  thm..    **.    
1ca9b 2a 2a 20 46 69 72 73 74 20 77 65 20 61 74 74 65  ** First we atte
1ca9c 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 68 65 20  mpt to find the 
1ca9d 6c 61 72 67 65 73 74 20 65 78 69 73 74 69 6e 67  largest existing
1ca9e 20 72 6f 77 69 64 20 61 6e 64 20 61 64 64 20 6f   rowid and add o
1ca9f 6e 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 61  ne.    ** to tha
1caa0 74 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6c  t.  But if the l
1caa1 61 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20  argest existing 
1caa2 72 6f 77 69 64 20 69 73 20 61 6c 72 65 61 64 79  rowid is already
1caa3 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20   the maximum.   
1caa4 20 2a 2a 20 70 6f 73 69 74 69 76 65 20 69 6e 74   ** positive int
1caa5 65 67 65 72 2c 20 77 65 20 68 61 76 65 20 74 6f  eger, we have to
1caa6 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   fall through to
1caa7 20 74 68 65 20 73 65 63 6f 6e 64 0a 20 20 20 20   the second.    
1caa8 2a 2a 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63  ** probabilistic
1caa9 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 20 20 2a   algorithm.    *
1caaa 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63  *.    ** The sec
1caab 6f 6e 64 20 61 6c 67 6f 72 69 74 68 6d 20 69 73  ond algorithm is
1caac 20 74 6f 20 73 65 6c 65 63 74 20 61 20 72 6f 77   to select a row
1caad 69 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e 64  id at random and
1caae 20 73 65 65 20 69 66 0a 20 20 20 20 2a 2a 20 69   see if.    ** i
1caaf 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  t already exists
1cab0 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20   in the table.  
1cab1 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65  If it does not e
1cab2 78 69 73 74 2c 20 77 65 20 68 61 76 65 0a 20 20  xist, we have.  
1cab3 20 20 2a 2a 20 73 75 63 63 65 65 64 65 64 2e 20    ** succeeded. 
1cab4 20 49 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 72   If the random r
1cab5 6f 77 69 64 20 64 6f 65 73 20 65 78 69 73 74 2c  owid does exist,
1cab6 20 77 65 20 73 65 6c 65 63 74 20 61 20 6e 65 77   we select a new
1cab7 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20   one.    ** and 
1cab8 74 72 79 20 61 67 61 69 6e 2c 20 75 70 20 74 6f  try again, up to
1cab9 20 31 30 30 30 20 74 69 6d 65 73 2e 0a 20 20 20   1000 times..   
1caba 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 20 61   **.    ** For a
1cabb 20 74 61 62 6c 65 20 77 69 74 68 20 6c 65 73 73   table with less
1cabc 20 74 68 61 6e 20 32 20 62 69 6c 6c 69 6f 6e 20   than 2 billion 
1cabd 65 6e 74 72 69 65 73 2c 20 74 68 65 20 70 72 6f  entries, the pro
1cabe 62 61 62 69 6c 69 74 79 0a 20 20 20 20 2a 2a 20  bability.    ** 
1cabf 6f 66 20 6e 6f 74 20 66 69 6e 64 69 6e 67 20 61  of not finding a
1cac0 20 75 6e 75 73 65 64 20 72 6f 77 69 64 20 69 73   unused rowid is
1cac1 20 61 62 6f 75 74 20 31 2e 30 65 2d 33 30 30 2e   about 1.0e-300.
1cac2 20 20 54 68 69 73 20 69 73 20 61 20 0a 20 20 20    This is a .   
1cac3 20 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f   ** non-zero pro
1cac4 62 61 62 69 6c 69 74 79 2c 20 62 75 74 20 69 74  bability, but it
1cac5 20 69 73 20 73 74 69 6c 6c 20 76 61 6e 69 73 68   is still vanish
1cac6 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 61 6e 64 20  ingly small and 
1cac7 73 68 6f 75 6c 64 0a 20 20 20 20 2a 2a 20 6e 65  should.    ** ne
1cac8 76 65 72 20 63 61 75 73 65 20 61 20 70 72 6f 62  ver cause a prob
1cac9 6c 65 6d 2e 20 20 59 6f 75 20 61 72 65 20 6d 75  lem.  You are mu
1caca 63 68 2c 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69  ch, much more li
1cacb 6b 65 6c 79 20 74 6f 20 68 61 76 65 20 61 0a 20  kely to have a. 
1cacc 20 20 20 2a 2a 20 68 61 72 64 77 61 72 65 20 66     ** hardware f
1cacd 61 69 6c 75 72 65 20 74 68 61 6e 20 66 6f 72 20  ailure than for 
1cace 74 68 69 73 20 61 6c 67 6f 72 69 74 68 6d 20 74  this algorithm t
1cacf 6f 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2a 0a 20  o fail..    **. 
1cad0 20 20 20 2a 2a 20 54 68 65 20 61 6e 61 6c 79 73     ** The analys
1cad1 69 73 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f  is in the previo
1cad2 75 73 20 70 61 72 61 67 72 61 70 68 20 61 73 73  us paragraph ass
1cad3 75 6d 65 73 20 74 68 61 74 20 79 6f 75 20 68 61  umes that you ha
1cad4 76 65 20 61 20 67 6f 6f 64 0a 20 20 20 20 2a 2a  ve a good.    **
1cad5 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 6f   source of rando
1cad6 6d 20 6e 75 6d 62 65 72 73 2e 20 20 49 73 20 61  m numbers.  Is a
1cad7 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f   library functio
1cad8 6e 20 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29  n like lrand48()
1cad9 0a 20 20 20 20 2a 2a 20 67 6f 6f 64 20 65 6e 6f  .    ** good eno
1cada 75 67 68 3f 20 20 4d 61 79 62 65 2e 20 4d 61 79  ugh?  Maybe. May
1cadb 62 65 20 6e 6f 74 2e 20 49 74 27 73 20 68 61 72  be not. It's har
1cadc 64 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65  d to know whethe
1cadd 72 20 74 68 65 72 65 0a 20 20 20 20 2a 2a 20 6d  r there.    ** m
1cade 69 67 68 74 20 62 65 20 73 75 62 74 6c 65 20 62  ight be subtle b
1cadf 75 67 73 20 69 73 20 73 6f 6d 65 20 69 6d 70 6c  ugs is some impl
1cae0 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 6c  ementations of l
1cae1 72 61 6e 64 34 38 28 29 20 74 68 61 74 0a 20 20  rand48() that.  
1cae2 20 20 2a 2a 20 63 6f 75 6c 64 20 63 61 75 73 65    ** could cause
1cae3 20 70 72 6f 62 6c 65 6d 73 2e 20 54 6f 20 61 76   problems. To av
1cae4 6f 69 64 20 75 6e 63 65 72 74 61 69 6e 74 79 2c  oid uncertainty,
1cae5 20 53 51 4c 69 74 65 20 75 73 65 73 20 69 74 73   SQLite uses its
1cae6 20 6f 77 6e 20 0a 20 20 20 20 2a 2a 20 72 61 6e   own .    ** ran
1cae7 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72  dom number gener
1cae8 61 74 6f 72 20 62 61 73 65 64 20 6f 6e 20 74 68  ator based on th
1cae9 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 6d 2e  e RC4 algorithm.
1caea 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1caeb 6f 20 70 72 6f 6d 6f 74 65 20 6c 6f 63 61 6c 69  o promote locali
1caec 74 79 20 6f 66 20 72 65 66 65 72 65 6e 63 65 20  ty of reference 
1caed 66 6f 72 20 72 65 70 65 74 69 74 69 76 65 20 69  for repetitive i
1caee 6e 73 65 72 74 73 2c 20 74 68 65 0a 20 20 20 20  nserts, the.    
1caef 2a 2a 20 66 69 72 73 74 20 66 65 77 20 61 74 74  ** first few att
1caf0 65 6d 70 74 73 20 61 74 20 63 68 6f 6f 73 69 6e  empts at choosin
1caf1 67 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64  g a random rowid
1caf2 20 70 69 63 6b 20 76 61 6c 75 65 73 20 6a 75 73   pick values jus
1caf3 74 20 61 20 6c 69 74 74 6c 65 0a 20 20 20 20 2a  t a little.    *
1caf4 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  * larger than th
1caf5 65 20 70 72 65 76 69 6f 75 73 20 72 6f 77 69 64  e previous rowid
1caf6 2e 20 20 54 68 69 73 20 68 61 73 20 62 65 65 6e  .  This has been
1caf7 20 73 68 6f 77 6e 20 65 78 70 65 72 69 6d 65 6e   shown experimen
1caf8 74 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 74 6f 20  tally.    ** to 
1caf9 64 6f 75 62 6c 65 20 74 68 65 20 73 70 65 65 64  double the speed
1cafa 20 6f 66 20 74 68 65 20 43 4f 50 59 20 6f 70 65   of the COPY ope
1cafb 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20  ration..    */. 
1cafc 20 20 20 69 6e 74 20 72 65 73 3d 30 2c 20 72 78     int res=0, rx
1cafd 3d 53 51 4c 49 54 45 5f 4f 4b 2c 20 63 6e 74 3b  =SQLITE_OK, cnt;
1cafe 0a 20 20 20 20 69 36 34 20 78 3b 0a 20 20 20 20  .    i64 x;.    
1caff 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  cnt = 0;.    if(
1cb00 20 28 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c   (sqlite3BtreeFl
1cb01 61 67 73 28 70 43 2d 3e 70 43 75 72 73 6f 72 29  ags(pC->pCursor)
1cb02 26 28 42 54 52 45 45 5f 49 4e 54 4b 45 59 7c 42  &(BTREE_INTKEY|B
1cb03 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 29 20  TREE_ZERODATA)) 
1cb04 21 3d 0a 20 20 20 20 20 20 20 20 20 20 42 54 52  !=.          BTR
1cb05 45 45 5f 49 4e 54 4b 45 59 20 29 7b 0a 20 20 20  EE_INTKEY ){.   
1cb06 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
1cb07 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1cb08 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1cb09 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1cb0a 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73  }.    assert( (s
1cb0b 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73  qlite3BtreeFlags
1cb0c 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 20 26 20  (pC->pCursor) & 
1cb0d 42 54 52 45 45 5f 49 4e 54 4b 45 59 29 21 3d 30  BTREE_INTKEY)!=0
1cb0e 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1cb0f 28 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61  (sqlite3BtreeFla
1cb10 67 73 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 20  gs(pC->pCursor) 
1cb11 26 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41  & BTREE_ZERODATA
1cb12 29 3d 3d 30 20 29 3b 0a 0a 23 69 66 64 65 66 20  )==0 );..#ifdef 
1cb13 53 51 4c 49 54 45 5f 33 32 42 49 54 5f 52 4f 57  SQLITE_32BIT_ROW
1cb14 49 44 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41  ID.#   define MA
1cb15 58 5f 52 4f 57 49 44 20 30 78 37 66 66 66 66 66  X_ROWID 0x7fffff
1cb16 66 66 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20  ff.#else.    /* 
1cb17 53 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 20 63  Some compilers c
1cb18 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 63 6f  omplain about co
1cb19 6e 73 74 61 6e 74 73 20 6f 66 20 74 68 65 20 66  nstants of the f
1cb1a 6f 72 6d 20 30 78 37 66 66 66 66 66 66 66 66 66  orm 0x7fffffffff
1cb1b 66 66 66 66 66 66 2e 0a 20 20 20 20 2a 2a 20 4f  ffffff..    ** O
1cb1c 74 68 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61  thers complain a
1cb1d 62 6f 75 74 20 30 78 37 66 66 66 66 66 66 66 66  bout 0x7ffffffff
1cb1e 66 66 66 66 66 66 66 66 4c 4c 2e 20 20 54 68 65  ffffffffLL.  The
1cb1f 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
1cb20 20 73 65 65 6d 73 0a 20 20 20 20 2a 2a 20 74 6f   seems.    ** to
1cb21 20 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f 6e   provide the con
1cb22 73 74 61 6e 74 20 77 68 69 6c 65 20 6d 61 6b 69  stant while maki
1cb23 6e 67 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 73  ng all compilers
1cb24 20 68 61 70 70 79 2e 0a 20 20 20 20 2a 2f 0a 23   happy..    */.#
1cb25 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f     define MAX_RO
1cb26 57 49 44 20 20 28 69 36 34 29 28 20 28 28 28 75  WID  (i64)( (((u
1cb27 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c  64)0x7fffffff)<<
1cb28 33 32 29 20 7c 20 28 75 36 34 29 30 78 66 66 66  32) | (u64)0xfff
1cb29 66 66 66 66 66 20 29 0a 23 65 6e 64 69 66 0a 0a  fffff ).#endif..
1cb2a 20 20 20 20 69 66 28 20 21 70 43 2d 3e 75 73 65      if( !pC->use
1cb2b 52 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20  RandomRowid ){. 
1cb2c 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33       v = sqlite3
1cb2d 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52 6f  BtreeGetCachedRo
1cb2e 77 69 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 29  wid(pC->pCursor)
1cb2f 3b 0a 20 20 20 20 20 20 69 66 28 20 76 3d 3d 30  ;.      if( v==0
1cb30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1cb31 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73   sqlite3BtreeLas
1cb32 74 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26  t(pC->pCursor, &
1cb33 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66  res);.        if
1cb34 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1cb35 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
1cb36 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1cb37 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rror;.        }.
1cb38 20 20 20 20 20 20 20 20 69 66 28 20 72 65 73 20          if( res 
1cb39 29 7b 0a 20 20 20 20 20 20 20 20 20 20 76 20 3d  ){.          v =
1cb3a 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   1;.        }els
1cb3b 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  e{.          sql
1cb3c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
1cb3d 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 76  (pC->pCursor, &v
1cb3e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 76 20 3d  );.          v =
1cb3f 20 6b 65 79 54 6f 49 6e 74 28 76 29 3b 0a 20 20   keyToInt(v);.  
1cb40 20 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d 4d          if( v==M
1cb41 41 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20  AX_ROWID ){.    
1cb42 20 20 20 20 20 20 20 20 70 43 2d 3e 75 73 65 52          pC->useR
1cb43 61 6e 64 6f 6d 52 6f 77 69 64 20 3d 20 31 3b 0a  andomRowid = 1;.
1cb44 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
1cb45 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 2b 2b  .            v++
1cb46 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1cb47 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1cb48 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1cb49 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45  OMIT_AUTOINCREME
1cb4a 4e 54 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  NT.      if( pOp
1cb4b 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20 20 20  ->p3 ){.        
1cb4c 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20  Mem *pMem;.     
1cb4d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1cb4e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
1cb4f 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 20 2f 2a 20 50  =p->nMem ); /* P
1cb50 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d  3 is a valid mem
1cb51 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20  ory cell */.    
1cb52 20 20 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61      pMem = &p->a
1cb53 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 09 52  Mem[pOp->p3];..R
1cb54 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1cb55 70 2d 3e 70 33 2c 20 70 4d 65 6d 29 3b 0a 20 20  p->p3, pMem);.  
1cb56 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1cb57 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
1cb58 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61 73  Mem);.        as
1cb59 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
1cb5a 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30  gs & MEM_Int)!=0
1cb5b 20 29 3b 20 20 2f 2a 20 6d 65 6d 28 50 33 29 20   );  /* mem(P3) 
1cb5c 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72  holds an integer
1cb5d 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
1cb5e 70 4d 65 6d 2d 3e 75 2e 69 3d 3d 4d 41 58 5f 52  pMem->u.i==MAX_R
1cb5f 4f 57 49 44 20 7c 7c 20 70 43 2d 3e 75 73 65 52  OWID || pC->useR
1cb60 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20  andomRowid ){.  
1cb61 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1cb62 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20  ITE_FULL;.      
1cb63 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1cb64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1cb65 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1cb66 66 28 20 76 3c 70 4d 65 6d 2d 3e 75 2e 69 2b 31  f( v<pMem->u.i+1
1cb67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 76 20   ){.          v 
1cb68 3d 20 70 4d 65 6d 2d 3e 75 2e 69 20 2b 20 31 3b  = pMem->u.i + 1;
1cb69 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1cb6a 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76     pMem->u.i = v
1cb6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
1cb6c 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ..      sqlite3B
1cb6d 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77  treeSetCachedRow
1cb6e 69 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  id(pC->pCursor, 
1cb6f 76 3c 4d 41 58 5f 52 4f 57 49 44 20 3f 20 76 2b  v<MAX_ROWID ? v+
1cb70 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  1 : 0);.    }.  
1cb71 20 20 69 66 28 20 70 43 2d 3e 75 73 65 52 61 6e    if( pC->useRan
1cb72 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  domRowid ){.    
1cb73 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1cb74 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 51 4c 49  3==0 );  /* SQLI
1cb75 54 45 5f 46 55 4c 4c 20 6d 75 73 74 20 68 61 76  TE_FULL must hav
1cb76 65 20 6f 63 63 75 72 72 65 64 20 70 72 69 6f 72  e occurred prior
1cb77 20 74 6f 20 74 68 69 73 20 2a 2f 0a 20 20 20 20   to this */.    
1cb78 20 20 76 20 3d 20 64 62 2d 3e 70 72 69 6f 72 4e    v = db->priorN
1cb79 65 77 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 63  ewRowid;.      c
1cb7a 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f  nt = 0;.      do
1cb7b 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6e  {.        if( cn
1cb7c 74 3d 3d 30 20 26 26 20 28 76 26 30 78 66 66 66  t==0 && (v&0xfff
1cb7d 66 66 66 29 3d 3d 76 20 29 7b 0a 20 20 20 20 20  fff)==v ){.     
1cb7e 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20       v++;.      
1cb7f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1cb80 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f     sqlite3_rando
1cb81 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 76 29 2c  mness(sizeof(v),
1cb82 20 26 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20   &v);.          
1cb83 69 66 28 20 63 6e 74 3c 35 20 29 20 76 20 26 3d  if( cnt<5 ) v &=
1cb84 20 30 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20   0xffffff;.     
1cb85 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1cb86 20 76 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65   v==0 ) continue
1cb87 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 69 6e  ;.        x = in
1cb88 74 54 6f 4b 65 79 28 76 29 3b 0a 20 20 20 20 20  tToKey(v);.     
1cb89 20 20 20 72 78 20 3d 20 73 71 6c 69 74 65 33 42     rx = sqlite3B
1cb8a 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
1cb8b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  ed(pC->pCursor, 
1cb8c 30 2c 20 28 75 36 34 29 78 2c 20 30 2c 20 26 72  0, (u64)x, 0, &r
1cb8d 65 73 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74  es);.        cnt
1cb8e 2b 2b 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65  ++;.      }while
1cb8f 28 20 63 6e 74 3c 31 30 30 20 26 26 20 72 78 3d  ( cnt<100 && rx=
1cb90 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65  =SQLITE_OK && re
1cb91 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62  s==0 );.      db
1cb92 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 20  ->priorNewRowid 
1cb93 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 72  = v;.      if( r
1cb94 78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  x==SQLITE_OK && 
1cb95 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  res==0 ){.      
1cb96 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55    rc = SQLITE_FU
1cb97 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  LL;.        goto
1cb98 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1cb99 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
1cb9a 20 7d 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64   }.    pC->rowid
1cb9b 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
1cb9c 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
1cb9d 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d  eto = 0;.    pC-
1cb9e 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
1cb9f 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a  ACHE_STALE;.  }.
1cba0 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1cba1 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1cba2 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76  .  pOut->u.i = v
1cba3 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1cba4 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 20   Opcode: Insert 
1cba5 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1cba6 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e  *.** Write an en
1cba7 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 61 62  try into the tab
1cba8 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 31 2e  le of cursor P1.
1cba9 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73    A new entry is
1cbaa 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 20 69  .** created if i
1cbab 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61 64  t doesn't alread
1cbac 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 20 64  y exist or the d
1cbad 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 73 74  ata for an exist
1cbae 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20  ing.** entry is 
1cbaf 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 54 68  overwritten.  Th
1cbb0 65 20 64 61 74 61 20 69 73 20 74 68 65 20 76 61  e data is the va
1cbb1 6c 75 65 20 73 74 6f 72 65 64 20 72 65 67 69 73  lue stored regis
1cbb2 74 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32  ter.** number P2
1cbb3 2e 20 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f  . The key is sto
1cbb4 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1cbb5 50 33 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74  P3. The key must
1cbb6 0a 2a 2a 20 62 65 20 61 6e 20 69 6e 74 65 67 65  .** be an intege
1cbb7 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
1cbb8 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66  OPFLAG_NCHANGE f
1cbb9 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74  lag of P5 is set
1cbba 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63  , then the row c
1cbbb 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a  hange count is.*
1cbbc 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f  * incremented (o
1cbbd 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 20 20  therwise not).  
1cbbe 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41  If the OPFLAG_LA
1cbbf 53 54 52 4f 57 49 44 20 66 6c 61 67 20 6f 66 20  STROWID flag of 
1cbc0 50 35 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68  P5 is set,.** th
1cbc1 65 6e 20 72 6f 77 69 64 20 69 73 20 73 74 6f 72  en rowid is stor
1cbc2 65 64 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e  ed for subsequen
1cbc3 74 20 72 65 74 75 72 6e 20 62 79 20 74 68 65 0a  t return by the.
1cbc4 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  ** sqlite3_last_
1cbc5 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 66  insert_rowid() f
1cbc6 75 6e 63 74 69 6f 6e 20 28 6f 74 68 65 72 77 69  unction (otherwi
1cbc7 73 65 20 69 74 20 69 73 20 75 6e 6d 6f 64 69 66  se it is unmodif
1cbc8 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61  ied)..**.** Para
1cbc9 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69  meter P4 may poi
1cbca 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63  nt to a string c
1cbcb 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
1cbcc 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20  ble-name, or.** 
1cbcd 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20  may be NULL. If 
1cbce 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  it is not NULL, 
1cbcf 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d  then the update-
1cbd0 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65  hook .** (sqlite
1cbd1 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63  3.xUpdateCallbac
1cbd2 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f  k) is invoked fo
1cbd3 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73  llowing a succes
1cbd4 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a  sful insert..**.
1cbd5 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f  ** (WARNING/TODO
1cbd6 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65  : If P1 is a pse
1cbd7 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50  udo-cursor and P
1cbd8 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79  2 is dynamically
1cbd9 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74  .** allocated, t
1cbda 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66  hen ownership of
1cbdb 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72   P2 is transferr
1cbdc 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f  ed to the pseudo
1cbdd 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72  -cursor.** and r
1cbde 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d  egister P2 becom
1cbdf 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49  es ephemeral.  I
1cbe0 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
1cbe1 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20  changed, the.** 
1cbe2 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65  value of registe
1cbe3 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63  r P2 will then c
1cbe4 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72  hange.  Make sur
1cbe5 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a  e this does not.
1cbe6 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f  ** cause any pro
1cbe7 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68  blems.).**.** Th
1cbe8 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  is instruction o
1cbe9 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62  nly works on tab
1cbea 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61  les.  The equiva
1cbeb 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  lent instruction
1cbec 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20  .** for indices 
1cbed 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e  is OP_IdxInsert.
1cbee 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65  .*/.case OP_Inse
1cbef 72 74 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 44 61  rt: {.  Mem *pDa
1cbf0 74 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ta = &p->aMem[pO
1cbf1 70 2d 3e 70 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70  p->p2];.  Mem *p
1cbf2 4b 65 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Key = &p->aMem[p
1cbf3 4f 70 2d 3e 70 33 5d 3b 0a 0a 20 20 69 36 34 20  Op->p3];..  i64 
1cbf4 69 4b 65 79 3b 20 20 20 2f 2a 20 54 68 65 20 69  iKey;   /* The i
1cbf5 6e 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20  nteger ROWID or 
1cbf6 6b 65 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f  key for the reco
1cbf7 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  rd to be inserte
1cbf8 64 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  d */.  int i = p
1cbf9 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75  Op->p1;.  VdbeCu
1cbfa 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
1cbfb 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1cbfc 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43  >nCursor );.  pC
1cbfd 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a   = p->apCsr[i];.
1cbfe 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20    assert( pC!=0 
1cbff 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d  );.  assert( pC-
1cc00 3e 70 43 75 72 73 6f 72 21 3d 30 20 7c 7c 20 70  >pCursor!=0 || p
1cc01 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29  C->pseudoTable )
1cc02 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79  ;.  assert( pKey
1cc03 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1cc04 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t );.  assert( p
1cc05 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1cc06 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1cc07 4f 70 2d 3e 70 32 2c 20 70 44 61 74 61 29 3b 0a  Op->p2, pData);.
1cc08 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1cc09 28 70 4f 70 2d 3e 70 33 2c 20 70 4b 65 79 29 3b  (pOp->p3, pKey);
1cc0a 0a 0a 20 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f  ..  iKey = intTo
1cc0b 4b 65 79 28 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a  Key(pKey->u.i);.
1cc0c 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20    if( pOp->p5 & 
1cc0d 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29  OPFLAG_NCHANGE )
1cc0e 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20   p->nChange++;. 
1cc0f 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f   if( pOp->p5 & O
1cc10 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20  PFLAG_LASTROWID 
1cc11 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20  ) db->lastRowid 
1cc12 3d 20 70 4b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69  = pKey->u.i;.  i
1cc13 66 28 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20  f( pData->flags 
1cc14 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
1cc15 20 20 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a    pData->z = 0;.
1cc16 20 20 20 20 70 44 61 74 61 2d 3e 6e 20 3d 20 30      pData->n = 0
1cc17 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
1cc18 73 73 65 72 74 28 20 70 44 61 74 61 2d 3e 66 6c  ssert( pData->fl
1cc19 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c  ags & (MEM_Blob|
1cc1a 4d 45 4d 5f 53 74 72 29 20 29 3b 0a 20 20 7d 0a  MEM_Str) );.  }.
1cc1b 20 20 69 66 28 20 70 43 2d 3e 70 73 65 75 64 6f    if( pC->pseudo
1cc1c 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 66 28  Table ){.    if(
1cc1d 20 21 70 43 2d 3e 65 70 68 65 6d 50 73 65 75 64   !pC->ephemPseud
1cc1e 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  oTable ){.      
1cc1f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1cc20 2c 20 70 43 2d 3e 70 44 61 74 61 29 3b 0a 20 20  , pC->pData);.  
1cc21 20 20 7d 0a 20 20 20 20 70 43 2d 3e 69 4b 65 79    }.    pC->iKey
1cc22 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 70 43 2d   = iKey;.    pC-
1cc23 3e 6e 44 61 74 61 20 3d 20 70 44 61 74 61 2d 3e  >nData = pData->
1cc24 6e 3b 0a 20 20 20 20 69 66 28 20 70 44 61 74 61  n;.    if( pData
1cc25 2d 3e 7a 3d 3d 70 44 61 74 61 2d 3e 7a 4d 61 6c  ->z==pData->zMal
1cc26 6c 6f 63 20 7c 7c 20 70 43 2d 3e 65 70 68 65 6d  loc || pC->ephem
1cc27 50 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20  PseudoTable ){. 
1cc28 20 20 20 20 20 70 43 2d 3e 70 44 61 74 61 20 3d       pC->pData =
1cc29 20 70 44 61 74 61 2d 3e 7a 3b 0a 20 20 20 20 20   pData->z;.     
1cc2a 20 69 66 28 20 21 70 43 2d 3e 65 70 68 65 6d 50   if( !pC->ephemP
1cc2b 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20  seudoTable ){.  
1cc2c 20 20 20 20 20 20 70 44 61 74 61 2d 3e 66 6c 61        pData->fla
1cc2d 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a  gs &= ~MEM_Dyn;.
1cc2e 20 20 20 20 20 20 20 20 70 44 61 74 61 2d 3e 66          pData->f
1cc2f 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65  lags |= MEM_Ephe
1cc30 6d 3b 0a 20 20 20 20 20 20 20 20 70 44 61 74 61  m;.        pData
1cc31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  ->zMalloc = 0;. 
1cc32 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
1cc33 7b 0a 20 20 20 20 20 20 70 43 2d 3e 70 44 61 74  {.      pC->pDat
1cc34 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  a = sqlite3Mallo
1cc35 63 28 20 70 43 2d 3e 6e 44 61 74 61 2b 32 20 29  c( pC->nData+2 )
1cc36 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 2d  ;.      if( !pC-
1cc37 3e 70 44 61 74 61 20 29 20 67 6f 74 6f 20 6e 6f  >pData ) goto no
1cc38 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 6d 65 6d 63  _mem;.      memc
1cc39 70 79 28 70 43 2d 3e 70 44 61 74 61 2c 20 70 44  py(pC->pData, pD
1cc3a 61 74 61 2d 3e 7a 2c 20 70 43 2d 3e 6e 44 61 74  ata->z, pC->nDat
1cc3b 61 29 3b 0a 20 20 20 20 20 20 70 43 2d 3e 70 44  a);.      pC->pD
1cc3c 61 74 61 5b 70 43 2d 3e 6e 44 61 74 61 5d 20 3d  ata[pC->nData] =
1cc3d 20 30 3b 0a 20 20 20 20 20 20 70 43 2d 3e 70 44   0;.      pC->pD
1cc3e 61 74 61 5b 70 43 2d 3e 6e 44 61 74 61 2b 31 5d  ata[pC->nData+1]
1cc3f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
1cc40 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b  pC->nullRow = 0;
1cc41 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
1cc42 74 20 6e 5a 65 72 6f 3b 0a 20 20 20 20 69 6e 74  t nZero;.    int
1cc43 20 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 28   seekResult = ((
1cc44 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47  pOp->p5 & OPFLAG
1cc45 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20  _USESEEKRESULT) 
1cc46 3f 20 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74  ? pC->seekResult
1cc47 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70   : 0);.    if( p
1cc48 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45  Data->flags & ME
1cc49 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
1cc4a 6e 5a 65 72 6f 20 3d 20 70 44 61 74 61 2d 3e 75  nZero = pData->u
1cc4b 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73  .nZero;.    }els
1cc4c 65 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d  e{.      nZero =
1cc4d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71   0;.    }.    sq
1cc4e 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
1cc4f 68 65 64 52 6f 77 69 64 28 70 43 2d 3e 70 43 75  hedRowid(pC->pCu
1cc50 72 73 6f 72 2c 20 30 29 3b 0a 20 20 20 20 72 63  rsor, 0);.    rc
1cc51 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49   = sqlite3BtreeI
1cc52 6e 73 65 72 74 28 70 43 2d 3e 70 43 75 72 73 6f  nsert(pC->pCurso
1cc53 72 2c 20 30 2c 20 69 4b 65 79 2c 0a 20 20 20 20  r, 0, iKey,.    
1cc54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cc55 20 20 20 20 20 20 20 20 70 44 61 74 61 2d 3e 7a          pData->z
1cc56 2c 20 70 44 61 74 61 2d 3e 6e 2c 20 6e 5a 65 72  , pData->n, nZer
1cc57 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o,.             
1cc58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1cc59 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
1cc5a 41 50 50 45 4e 44 2c 20 73 65 65 6b 52 65 73 75  APPEND, seekResu
1cc5b 6c 74 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20  lt.    );.  }.  
1cc5c 0a 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  .  pC->rowidIsVa
1cc5d 6c 69 64 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 64  lid = 0;.  pC->d
1cc5e 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
1cc5f 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74  0;.  pC->cacheSt
1cc60 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1cc61 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65  LE;..  /* Invoke
1cc62 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b   the update-hook
1cc63 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f   if required. */
1cc64 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1cc65 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64  E_OK && db->xUpd
1cc66 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70  ateCallback && p
1cc67 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20  Op->p4.z ){.    
1cc68 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
1cc69 3d 20 64 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 44  = db->aDb[pC->iD
1cc6a 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f  b].zName;.    co
1cc6b 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d  nst char *zTbl =
1cc6c 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20   pOp->p4.z;.    
1cc6d 69 6e 74 20 6f 70 20 3d 20 28 28 70 4f 70 2d 3e  int op = ((pOp->
1cc6e 70 35 20 26 20 4f 50 46 4c 41 47 5f 49 53 55 50  p5 & OPFLAG_ISUP
1cc6f 44 41 54 45 29 20 3f 20 53 51 4c 49 54 45 5f 55  DATE) ? SQLITE_U
1cc70 50 44 41 54 45 20 3a 20 53 51 4c 49 54 45 5f 49  PDATE : SQLITE_I
1cc71 4e 53 45 52 54 29 3b 0a 20 20 20 20 61 73 73 65  NSERT);.    asse
1cc72 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20  rt( pC->isTable 
1cc73 29 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61  );.    db->xUpda
1cc74 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70  teCallback(db->p
1cc75 55 70 64 61 74 65 41 72 67 2c 20 6f 70 2c 20 7a  UpdateArg, op, z
1cc76 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b  Db, zTbl, iKey);
1cc77 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1cc78 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20  >iDb>=0 );.  }. 
1cc79 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cc7a 63 6f 64 65 3a 20 44 65 6c 65 74 65 20 50 31 20  code: Delete P1 
1cc7b 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  P2 * P4 *.**.** 
1cc7c 44 65 6c 65 74 65 20 74 68 65 20 72 65 63 6f 72  Delete the recor
1cc7d 64 20 61 74 20 77 68 69 63 68 20 74 68 65 20 50  d at which the P
1cc7e 31 20 63 75 72 73 6f 72 20 69 73 20 63 75 72 72  1 cursor is curr
1cc7f 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a  ently pointing..
1cc80 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72  **.** The cursor
1cc81 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 70 6f   will be left po
1cc82 69 6e 74 69 6e 67 20 61 74 20 65 69 74 68 65 72  inting at either
1cc83 20 74 68 65 20 6e 65 78 74 20 6f 72 20 74 68 65   the next or the
1cc84 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 72 65 63   previous.** rec
1cc85 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ord in the table
1cc86 2e 20 49 66 20 69 74 20 69 73 20 6c 65 66 74 20  . If it is left 
1cc87 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20  pointing at the 
1cc88 6e 65 78 74 20 72 65 63 6f 72 64 2c 20 74 68 65  next record, the
1cc89 6e 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 4e 65  n.** the next Ne
1cc8a 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77  xt instruction w
1cc8b 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20  ill be a no-op. 
1cc8c 20 48 65 6e 63 65 20 69 74 20 69 73 20 4f 4b 20   Hence it is OK 
1cc8d 74 6f 20 64 65 6c 65 74 65 0a 2a 2a 20 61 20 72  to delete.** a r
1cc8e 65 63 6f 72 64 20 66 72 6f 6d 20 77 69 74 68 69  ecord from withi
1cc8f 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e 0a  n an Next loop..
1cc90 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46  **.** If the OPF
1cc91 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67  LAG_NCHANGE flag
1cc92 20 6f 66 20 50 32 20 69 73 20 73 65 74 2c 20 74   of P2 is set, t
1cc93 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e  hen the row chan
1cc94 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69  ge count is.** i
1cc95 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65  ncremented (othe
1cc96 72 77 69 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a 2a  rwise not)..**.*
1cc97 2a 20 50 31 20 6d 75 73 74 20 6e 6f 74 20 62 65  * P1 must not be
1cc98 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20 20   pseudo-table.  
1cc99 49 74 20 68 61 73 20 74 6f 20 62 65 20 61 20 72  It has to be a r
1cc9a 65 61 6c 20 74 61 62 6c 65 20 77 69 74 68 0a 2a  eal table with.*
1cc9b 2a 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 2e  * multiple rows.
1cc9c 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20  .**.** If P4 is 
1cc9d 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69  not NULL, then i
1cc9e 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  t is the name of
1cc9f 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
1cca0 50 31 20 69 73 0a 2a 2a 20 70 6f 69 6e 74 69 6e  P1 is.** pointin
1cca1 67 20 74 6f 2e 20 20 54 68 65 20 75 70 64 61 74  g to.  The updat
1cca2 65 20 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69  e hook will be i
1cca3 6e 76 6f 6b 65 64 2c 20 69 66 20 69 74 20 65 78  nvoked, if it ex
1cca4 69 73 74 73 2e 0a 2a 2a 20 49 66 20 50 34 20 69  ists..** If P4 i
1cca5 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20  s not NULL then 
1cca6 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75  the P1 cursor mu
1cca7 73 74 20 68 61 76 65 20 62 65 65 6e 20 70 6f 73  st have been pos
1cca8 69 74 69 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e 67  itioned.** using
1cca9 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 70 72 69   OP_NotFound pri
1ccaa 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74  or to invoking t
1ccab 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63  his opcode..*/.c
1ccac 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a 20 7b  ase OP_Delete: {
1ccad 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
1ccae 70 31 3b 0a 20 20 69 36 34 20 69 4b 65 79 20 3d  p1;.  i64 iKey =
1ccaf 20 30 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72   0;.  VdbeCursor
1ccb0 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65 72 74 28   *pC;..  assert(
1ccb1 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43   i>=0 && i<p->nC
1ccb2 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20  ursor );.  pC = 
1ccb3 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61  p->apCsr[i];.  a
1ccb4 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a  ssert( pC!=0 );.
1ccb5 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 43    assert( pC->pC
1ccb6 75 72 73 6f 72 21 3d 30 20 29 3b 20 20 2f 2a 20  ursor!=0 );  /* 
1ccb7 4f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 72  Only valid for r
1ccb8 65 61 6c 20 74 61 62 6c 65 73 2c 20 6e 6f 20 70  eal tables, no p
1ccb9 73 65 75 64 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a  seudotables */..
1ccba 20 20 2f 2a 20 49 66 20 74 68 65 20 75 70 64 61    /* If the upda
1ccbb 74 65 2d 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20  te-hook will be 
1ccbc 69 6e 76 6f 6b 65 64 2c 20 73 65 74 20 69 4b 65  invoked, set iKe
1ccbd 79 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 6f  y to the rowid o
1ccbe 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62  f the.  ** row b
1ccbf 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20  eing deleted..  
1ccc0 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70  */.  if( db->xUp
1ccc1 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20  dateCallback && 
1ccc2 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20  pOp->p4.z ){.   
1ccc3 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54   assert( pC->isT
1ccc4 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65  able );.    asse
1ccc5 72 74 28 20 70 43 2d 3e 72 6f 77 69 64 49 73 56  rt( pC->rowidIsV
1ccc6 61 6c 69 64 20 29 3b 20 20 2f 2a 20 6c 61 73 74  alid );  /* last
1ccc7 52 6f 77 69 64 20 73 65 74 20 62 79 20 70 72 65  Rowid set by pre
1ccc8 76 69 6f 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 6e  vious OP_NotFoun
1ccc9 64 20 2a 2f 0a 20 20 20 20 69 4b 65 79 20 3d 20  d */.    iKey = 
1ccca 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20  pC->lastRowid;. 
1cccb 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
1cccc 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
1cccd 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72 63  to(pC);.  if( rc
1ccce 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1cccf 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 73 71  e_to_error;.  sq
1ccd0 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
1ccd1 68 65 64 52 6f 77 69 64 28 70 43 2d 3e 70 43 75  hedRowid(pC->pCu
1ccd2 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d  rsor, 0);.  rc =
1ccd3 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c   sqlite3BtreeDel
1ccd4 65 74 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 29  ete(pC->pCursor)
1ccd5 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61  ;.  pC->cacheSta
1ccd6 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1ccd7 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  E;..  /* Invoke 
1ccd8 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20  the update-hook 
1ccd9 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a  if required. */.
1ccda 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1ccdb 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61  _OK && db->xUpda
1ccdc 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f  teCallback && pO
1ccdd 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63  p->p4.z ){.    c
1ccde 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
1ccdf 20 64 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62   db->aDb[pC->iDb
1cce0 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e  ].zName;.    con
1cce1 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20  st char *zTbl = 
1cce2 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 64  pOp->p4.z;.    d
1cce3 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61  b->xUpdateCallba
1cce4 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 72  ck(db->pUpdateAr
1cce5 67 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  g, SQLITE_DELETE
1cce6 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65  , zDb, zTbl, iKe
1cce7 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  y);.    assert( 
1cce8 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20  pC->iDb>=0 );.  
1cce9 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20  }.  if( pOp->p2 
1ccea 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  & OPFLAG_NCHANGE
1cceb 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b   ) p->nChange++;
1ccec 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1cced 4f 70 63 6f 64 65 3a 20 52 65 73 65 74 43 6f 75  Opcode: ResetCou
1ccee 6e 74 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  nt P1 * *.**.** 
1ccef 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 73 65  This opcode rese
1ccf0 74 73 20 74 68 65 20 56 4d 73 20 69 6e 74 65 72  ts the VMs inter
1ccf1 6e 61 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  nal change count
1ccf2 65 72 20 74 6f 20 30 2e 20 49 66 20 50 31 20 69  er to 0. If P1 i
1ccf3 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20  s true,.** then 
1ccf4 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1ccf5 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1ccf6 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65  is copied to the
1ccf7 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1ccf8 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  .** change count
1ccf9 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79 20  er (returned by 
1ccfa 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1ccfb 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61 6e   to sqlite3_chan
1ccfc 67 65 73 28 29 29 0a 2a 2a 20 62 65 66 6f 72 65  ges()).** before
1ccfd 20 69 74 20 69 73 20 72 65 73 65 74 2e 20 54 68   it is reset. Th
1ccfe 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 72  is is used by tr
1ccff 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a  igger programs..
1cd00 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 65 74  */.case OP_Reset
1cd01 43 6f 75 6e 74 3a 20 7b 0a 20 20 69 66 28 20 70  Count: {.  if( p
1cd02 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 71  Op->p1 ){.    sq
1cd03 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
1cd04 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e  ges(db, p->nChan
1cd05 67 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 43  ge);.  }.  p->nC
1cd06 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 62 72 65  hange = 0;.  bre
1cd07 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1cd08 3a 20 52 6f 77 44 61 74 61 20 50 31 20 50 32 20  : RowData P1 P2 
1cd09 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74  * * *.**.** Writ
1cd0a 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
1cd0b 50 32 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  P2 the complete 
1cd0c 72 6f 77 20 64 61 74 61 20 66 6f 72 20 63 75 72  row data for cur
1cd0d 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65  sor P1..** There
1cd0e 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65 74   is no interpret
1cd0f 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74  ation of the dat
1cd10 61 2e 20 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75  a.  .** It is ju
1cd11 73 74 20 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74  st copied onto t
1cd12 68 65 20 50 32 20 72 65 67 69 73 74 65 72 20 65  he P2 register e
1cd13 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74  xactly as .** it
1cd14 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65   is found in the
1cd15 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
1cd16 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20  **.** If the P1 
1cd17 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 70  cursor must be p
1cd18 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 6c  ointing to a val
1cd19 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55  id row (not a NU
1cd1a 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20  LL row).** of a 
1cd1b 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20  real table, not 
1cd1c 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a  a pseudo-table..
1cd1d 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f  */./* Opcode: Ro
1cd1e 77 4b 65 79 20 50 31 20 50 32 20 2a 20 2a 20 2a  wKey P1 P2 * * *
1cd1f 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74  .**.** Write int
1cd20 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68  o register P2 th
1cd21 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b  e complete row k
1cd22 65 79 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31  ey for cursor P1
1cd23 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f  ..** There is no
1cd24 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20   interpretation 
1cd25 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a  of the data.  .*
1cd26 2a 20 54 68 65 20 6b 65 79 20 69 73 20 63 6f 70  * The key is cop
1cd27 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 33 20  ied onto the P3 
1cd28 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c 79  register exactly
1cd29 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f   as .** it is fo
1cd2a 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62  und in the datab
1cd2b 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
1cd2c 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f 72  If the P1 cursor
1cd2d 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e   must be pointin
1cd2e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77  g to a valid row
1cd2f 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77   (not a NULL row
1cd30 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74  ).** of a real t
1cd31 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75  able, not a pseu
1cd32 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73  do-table..*/.cas
1cd33 65 20 4f 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73  e OP_RowKey:.cas
1cd34 65 20 4f 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a  e OP_RowData: {.
1cd35 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
1cd36 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  1;.  VdbeCursor 
1cd37 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  *pC;.  BtCursor 
1cd38 2a 70 43 72 73 72 3b 0a 20 20 75 33 32 20 6e 3b  *pCrsr;.  u32 n;
1cd39 0a 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  ..  pOut = &p->a
1cd3a 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20  Mem[pOp->p2];.. 
1cd3b 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 52 6f   /* Note that Ro
1cd3c 77 4b 65 79 20 61 6e 64 20 52 6f 77 44 61 74 61  wKey and RowData
1cd3d 20 61 72 65 20 72 65 61 6c 6c 79 20 65 78 61 63   are really exac
1cd3e 74 6c 79 20 74 68 65 20 73 61 6d 65 20 69 6e 73  tly the same ins
1cd3f 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73  truction */.  as
1cd40 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1cd41 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1cd42 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
1cd43 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e  ;.  assert( pC->
1cd44 69 73 54 61 62 6c 65 20 7c 7c 20 70 4f 70 2d 3e  isTable || pOp->
1cd45 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b 65  opcode==OP_RowKe
1cd46 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  y );.  assert( p
1cd47 43 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f  C->isIndex || pO
1cd48 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f  p->opcode==OP_Ro
1cd49 77 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72  wData );.  asser
1cd4a 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61 73  t( pC!=0 );.  as
1cd4b 73 65 72 74 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f  sert( pC->nullRo
1cd4c 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  w==0 );.  assert
1cd4d 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c  ( pC->pseudoTabl
1cd4e 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  e==0 );.  assert
1cd4f 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30  ( pC->pCursor!=0
1cd50 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 43   );.  pCrsr = pC
1cd51 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 72 63 20  ->pCursor;.  rc 
1cd52 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
1cd53 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20  sorMoveto(pC);. 
1cd54 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61   if( rc ) goto a
1cd55 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1cd56 72 3b 0a 20 20 69 66 28 20 70 43 2d 3e 69 73 49  r;.  if( pC->isI
1cd57 6e 64 65 78 20 29 7b 0a 20 20 20 20 69 36 34 20  ndex ){.    i64 
1cd58 6e 36 34 3b 0a 20 20 20 20 61 73 73 65 72 74 28  n64;.    assert(
1cd59 20 21 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b   !pC->isTable );
1cd5a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1cd5b 65 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c 20  eKeySize(pCrsr, 
1cd5c 26 6e 36 34 29 3b 0a 20 20 20 20 69 66 28 20 6e  &n64);.    if( n
1cd5d 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  64>db->aLimit[SQ
1cd5e 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1cd5f 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  H] ){.      goto
1cd60 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a   too_big;.    }.
1cd61 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 6e 36 34      n = (int)n64
1cd62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
1cd63 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53  qlite3BtreeDataS
1cd64 69 7a 65 28 70 43 72 73 72 2c 20 26 6e 29 3b 0a  ize(pCrsr, &n);.
1cd65 20 20 20 20 69 66 28 20 28 69 6e 74 29 6e 3e 64      if( (int)n>d
1cd66 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
1cd67 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
1cd68 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f  {.      goto too
1cd69 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  _big;.    }.  }.
1cd6a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1cd6b 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e  eMemGrow(pOut, n
1cd6c 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  , 0) ){.    goto
1cd6d 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70   no_mem;.  }.  p
1cd6e 4f 75 74 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 4d 65  Out->n = n;.  Me
1cd6f 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
1cd70 74 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20  t, MEM_Blob);.  
1cd71 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20  if( pC->isIndex 
1cd72 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1cd73 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 72 73  te3BtreeKey(pCrs
1cd74 72 2c 20 30 2c 20 6e 2c 20 70 4f 75 74 2d 3e 7a  r, 0, n, pOut->z
1cd75 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1cd76 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1cd77 65 44 61 74 61 28 70 43 72 73 72 2c 20 30 2c 20  eData(pCrsr, 0, 
1cd78 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d  n, pOut->z);.  }
1cd79 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53  .  pOut->enc = S
1cd7a 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20  QLITE_UTF8;  /* 
1cd7b 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62  In case the blob
1cd7c 20 69 73 20 65 76 65 72 20 63 61 73 74 20 74 6f   is ever cast to
1cd7d 20 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 54   text */.  UPDAT
1cd7e 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1cd7f 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1cd80 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77  ../* Opcode: Row
1cd81 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  id P1 P2 * * *.*
1cd82 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 65  *.** Store in re
1cd83 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e 74  gister P2 an int
1cd84 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
1cd85 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61 62  e key of the tab
1cd86 6c 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a  le entry that.**
1cd87 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79   P1 is currently
1cd88 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a 2a   point to..**.**
1cd89 20 50 31 20 63 61 6e 20 62 65 20 65 69 74 68 65   P1 can be eithe
1cd8a 72 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 74 61  r an ordinary ta
1cd8b 62 6c 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c  ble or a virtual
1cd8c 20 74 61 62 6c 65 2e 20 20 54 68 65 72 65 20 75   table.  There u
1cd8d 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 61 20 73  sed to.** be a s
1cd8e 65 70 61 72 61 74 65 20 4f 50 5f 56 52 6f 77 69  eparate OP_VRowi
1cd8f 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 75 73 65  d opcode for use
1cd90 20 77 69 74 68 20 76 69 72 74 75 61 6c 20 74 61   with virtual ta
1cd91 62 6c 65 73 2c 20 62 75 74 20 74 68 69 73 0a 2a  bles, but this.*
1cd92 2a 20 6f 6e 65 20 6f 70 63 6f 64 65 20 6e 6f 77  * one opcode now
1cd93 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20   works for both 
1cd94 74 61 62 6c 65 20 74 79 70 65 73 2e 0a 2a 2f 0a  table types..*/.
1cd95 63 61 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b  case OP_Rowid: {
1cd96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd97 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1cd98 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  ase */.  int i =
1cd99 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65   pOp->p1;.  Vdbe
1cd9a 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36  Cursor *pC;.  i6
1cd9b 34 20 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  4 v;..  assert( 
1cd9c 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
1cd9d 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70  rsor );.  pC = p
1cd9e 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73  ->apCsr[i];.  as
1cd9f 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20  sert( pC!=0 );. 
1cda0 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77   if( pC->nullRow
1cda1 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f   ){.    /* Do no
1cda2 74 68 69 6e 67 20 73 6f 20 74 68 61 74 20 72 65  thing so that re
1cda3 67 5b 50 32 5d 20 72 65 6d 61 69 6e 73 20 4e 55  g[P2] remains NU
1cda4 4c 4c 20 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b  LL */.    break;
1cda5 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d  .  }else if( pC-
1cda6 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1cda7 29 7b 0a 20 20 20 20 76 20 3d 20 70 43 2d 3e 6d  ){.    v = pC->m
1cda8 6f 76 65 74 6f 54 61 72 67 65 74 3b 0a 20 20 7d  ovetoTarget;.  }
1cda9 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 70 73 65  else if( pC->pse
1cdaa 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  udoTable ){.    
1cdab 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28 70 43 2d  v = keyToInt(pC-
1cdac 3e 69 4b 65 79 29 3b 0a 23 69 66 6e 64 65 66 20  >iKey);.#ifndef 
1cdad 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1cdae 55 41 4c 54 41 42 4c 45 0a 20 20 7d 65 6c 73 65  UALTABLE.  }else
1cdaf 20 69 66 28 20 70 43 2d 3e 70 56 74 61 62 43 75   if( pC->pVtabCu
1cdb0 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  rsor ){.    sqli
1cdb1 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1cdb2 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  .    const sqlit
1cdb3 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1cdb4 6c 65 3b 0a 20 20 20 20 70 56 74 61 62 20 3d 20  le;.    pVtab = 
1cdb5 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d  pC->pVtabCursor-
1cdb6 3e 70 56 74 61 62 3b 0a 20 20 20 20 70 4d 6f 64  >pVtab;.    pMod
1cdb7 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f  ule = pVtab->pMo
1cdb8 64 75 6c 65 3b 0a 20 20 20 20 61 73 73 65 72 74  dule;.    assert
1cdb9 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69  ( pModule->xRowi
1cdba 64 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  d );.    if( sql
1cdbb 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1cdbc 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1cdbd 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
1cdbe 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
1cdbf 78 52 6f 77 69 64 28 70 43 2d 3e 70 56 74 61 62  xRowid(pC->pVtab
1cdc0 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20  Cursor, &v);.   
1cdc1 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1cdc2 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  b, p->zErrMsg);.
1cdc3 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d      p->zErrMsg =
1cdc4 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b   pVtab->zErrMsg;
1cdc5 0a 20 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72  .    pVtab->zErr
1cdc6 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  Msg = 0;.    if(
1cdc7 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
1cdc8 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1cdc9 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1cdca 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1cdcb 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1cdcc 42 4c 45 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a  BLE */.  }else{.
1cdcd 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1cdce 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f  VdbeCursorMoveto
1cdcf 28 70 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63  (pC);.    if( rc
1cdd0 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1cdd1 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1cdd2 69 66 28 20 70 43 2d 3e 72 6f 77 69 64 49 73 56  if( pC->rowidIsV
1cdd3 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 76 20  alid ){.      v 
1cdd4 3d 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b  = pC->lastRowid;
1cdd5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1cdd6 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 43    assert( pC->pC
1cdd7 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 20 20  ursor!=0 );.    
1cdd8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65    sqlite3BtreeKe
1cdd9 79 53 69 7a 65 28 70 43 2d 3e 70 43 75 72 73 6f  ySize(pC->pCurso
1cdda 72 2c 20 26 76 29 3b 0a 20 20 20 20 20 20 76 20  r, &v);.      v 
1cddb 3d 20 6b 65 79 54 6f 49 6e 74 28 76 29 3b 0a 20  = keyToInt(v);. 
1cddc 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f 75 74 2d     }.  }.  pOut-
1cddd 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 4d 65 6d 53  >u.i = v;.  MemS
1cdde 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1cddf 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65   MEM_Int);.  bre
1cde0 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1cde1 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a  : NullRow P1 * *
1cde2 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20   * *.**.** Move 
1cde3 74 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 6f  the cursor P1 to
1cde4 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e   a null row.  An
1cde5 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72  y OP_Column oper
1cde6 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f  ations.** that o
1cde7 63 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 63  ccur while the c
1cde8 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 20  ursor is on the 
1cde9 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c  null row will al
1cdea 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 20  ways.** write a 
1cdeb 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
1cdec 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 20 20 69 6e  _NullRow: {.  in
1cded 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
1cdee 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1cdef 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ..  assert( i>=0
1cdf0 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72   && i<p->nCursor
1cdf1 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70   );.  pC = p->ap
1cdf2 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74  Csr[i];.  assert
1cdf3 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 70 43 2d  ( pC!=0 );.  pC-
1cdf4 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20  >nullRow = 1;.  
1cdf5 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1cdf6 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 2d 3e   = 0;.  if( pC->
1cdf7 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73  pCursor ){.    s
1cdf8 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72  qlite3BtreeClear
1cdf9 43 75 72 73 6f 72 28 70 43 2d 3e 70 43 75 72 73  Cursor(pC->pCurs
1cdfa 6f 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  or);.  }.  break
1cdfb 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cdfc 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Last P1 P2 * * *
1cdfd 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  .**.** The next 
1cdfe 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64  use of the Rowid
1cdff 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65   or Column or Ne
1ce00 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66  xt instruction f
1ce01 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72  or P1 .** will r
1ce02 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74  efer to the last
1ce03 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61   entry in the da
1ce04 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
1ce05 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65  index..** If the
1ce06 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1ce07 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e  is empty and P2>
1ce08 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d  0, then jump imm
1ce09 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
1ce0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72  ** If P2 is 0 or
1ce0b 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   if the table or
1ce0c 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d   index is not em
1ce0d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  pty, fall throug
1ce0e 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
1ce0f 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
1ce10 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61  n..*/.case OP_La
1ce11 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  st: {        /* 
1ce12 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20  jump */.  int i 
1ce13 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62  = pOp->p1;.  Vdb
1ce14 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1ce15 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1ce16 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73    int res;..  as
1ce17 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1ce18 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1ce19 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
1ce1a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d  ;.  assert( pC!=
1ce1b 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 70  0 );.  pCrsr = p
1ce1c 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73  C->pCursor;.  as
1ce1d 73 65 72 74 28 20 70 43 72 73 72 21 3d 30 20 29  sert( pCrsr!=0 )
1ce1e 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1ce1f 42 74 72 65 65 4c 61 73 74 28 70 43 72 73 72 2c  BtreeLast(pCrsr,
1ce20 20 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75   &res);.  pC->nu
1ce21 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72 65 73 3b  llRow = (u8)res;
1ce22 0a 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  .  pC->deferredM
1ce23 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 70 43 2d  oveto = 0;.  pC-
1ce24 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1ce25 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74  0;.  pC->cacheSt
1ce26 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1ce27 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 20 26 26  LE;.  if( res &&
1ce28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 7b 0a 20 20   pOp->p2>0 ){.  
1ce29 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1ce2a 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1ce2b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  .}.../* Opcode: 
1ce2c 53 6f 72 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Sort P1 P2 * * *
1ce2d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1ce2e 64 65 20 64 6f 65 73 20 65 78 61 63 74 6c 79 20  de does exactly 
1ce2f 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61  the same thing a
1ce30 73 20 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 65  s OP_Rewind exce
1ce31 70 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e  pt that.** it in
1ce32 63 72 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f  crements an undo
1ce33 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20  cumented global 
1ce34 76 61 72 69 61 62 6c 65 20 75 73 65 64 20 66 6f  variable used fo
1ce35 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a  r testing..**.**
1ce36 20 53 6f 72 74 69 6e 67 20 69 73 20 61 63 63 6f   Sorting is acco
1ce37 6d 70 6c 69 73 68 65 64 20 62 79 20 77 72 69 74  mplished by writ
1ce38 69 6e 67 20 72 65 63 6f 72 64 73 20 69 6e 74 6f  ing records into
1ce39 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78   a sorting index
1ce3a 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e 64  ,.** then rewind
1ce3b 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 61  ing that index a
1ce3c 6e 64 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61  nd playing it ba
1ce3d 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e  ck from beginnin
1ce3e 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65  g to.** end.  We
1ce3f 20 75 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 74   use the OP_Sort
1ce40 20 6f 70 63 6f 64 65 20 69 6e 73 74 65 61 64 20   opcode instead 
1ce41 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20  of OP_Rewind to 
1ce42 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64  do the.** rewind
1ce43 69 6e 67 20 73 6f 20 74 68 61 74 20 74 68 65 20  ing so that the 
1ce44 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1ce45 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e  will be incremen
1ce46 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65  ted and.** regre
1ce47 73 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20  ssion tests can 
1ce48 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65  determine whethe
1ce49 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74  r or not the opt
1ce4a 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72  imizer is.** cor
1ce4b 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e  rectly optimizin
1ce4c 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a  g out sorts..*/.
1ce4d 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20  case OP_Sort: { 
1ce4e 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
1ce4f 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
1ce50 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 73  TEST.  sqlite3_s
1ce51 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73  ort_count++;.  s
1ce52 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f  qlite3_search_co
1ce53 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20  unt--;.#endif.  
1ce54 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49  p->aCounter[SQLI
1ce55 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f  TE_STMTSTATUS_SO
1ce56 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61  RT-1]++;.  /* Fa
1ce57 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20  ll through into 
1ce58 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f  OP_Rewind */.}./
1ce59 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64  * Opcode: Rewind
1ce5a 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1ce5b 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 20  ** The next use 
1ce5c 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20  of the Rowid or 
1ce5d 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69  Column or Next i
1ce5e 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50  nstruction for P
1ce5f 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72  1 .** will refer
1ce60 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e   to the first en
1ce61 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
1ce62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  ase table or ind
1ce63 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61  ex..** If the ta
1ce64 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20  ble or index is 
1ce65 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20  empty and P2>0, 
1ce66 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69  then jump immedi
1ce67 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20  ately to P2..** 
1ce68 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66  If P2 is 0 or if
1ce69 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
1ce6a 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79  dex is not empty
1ce6b 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a  , fall through.*
1ce6c 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * to the followi
1ce6d 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ng instruction..
1ce6e 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 77 69 6e  */.case OP_Rewin
1ce6f 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  d: {        /* j
1ce70 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  ump */.  int i =
1ce71 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65   pOp->p1;.  Vdbe
1ce72 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74  Cursor *pC;.  Bt
1ce73 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1ce74 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73 73   int res;..  ass
1ce75 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
1ce76 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70  ->nCursor );.  p
1ce77 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b  C = p->apCsr[i];
1ce78 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30  .  assert( pC!=0
1ce79 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72   );.  if( (pCrsr
1ce7a 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 29 21   = pC->pCursor)!
1ce7b 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  =0 ){.    rc = s
1ce7c 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74  qlite3BtreeFirst
1ce7d 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a 20  (pCrsr, &res);. 
1ce7e 20 20 20 70 43 2d 3e 61 74 46 69 72 73 74 20 3d     pC->atFirst =
1ce7f 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20   res==0 ?1:0;.  
1ce80 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f    pC->deferredMo
1ce81 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43  veto = 0;.    pC
1ce82 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1ce83 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20  CACHE_STALE;.   
1ce84 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69   pC->rowidIsVali
1ce85 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  d = 0;.  }else{.
1ce86 20 20 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 7d      res = 1;.  }
1ce87 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d  .  pC->nullRow =
1ce88 20 28 75 38 29 72 65 73 3b 0a 20 20 61 73 73 65   (u8)res;.  asse
1ce89 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26  rt( pOp->p2>0 &&
1ce8a 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20   pOp->p2<p->nOp 
1ce8b 29 3b 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a  );.  if( res ){.
1ce8c 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1ce8d 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1ce8e 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1ce8f 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20   Next P1 P2 * * 
1ce90 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20  *.**.** Advance 
1ce91 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61  cursor P1 so tha
1ce92 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  t it points to t
1ce93 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61 74 61  he next key/data
1ce94 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20   pair in its.** 
1ce95 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20  table or index. 
1ce96 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1ce97 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65 20   more key/value 
1ce98 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c 20  pairs then fall 
1ce99 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68  through.** to th
1ce9a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74  e following inst
1ce9b 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 66  ruction.  But if
1ce9c 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 76 61   the cursor adva
1ce9d 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 73 66  nce was successf
1ce9e 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65  ul,.** jump imme
1ce9f 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a  diately to P2..*
1cea0 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72 73  *.** The P1 curs
1cea1 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20 61  or must be for a
1cea2 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74   real table, not
1cea3 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e   a pseudo-table.
1cea4 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1cea5 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f   Prev.*/./* Opco
1cea6 64 65 3a 20 50 72 65 76 20 50 31 20 50 32 20 2a  de: Prev P1 P2 *
1cea7 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20   * *.**.** Back 
1cea8 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20  up cursor P1 so 
1cea9 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
1ceaa 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6b  o the previous k
1ceab 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e 20  ey/data pair in 
1ceac 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20  its.** table or 
1cead 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 65  index.  If there
1ceae 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73 20   is no previous 
1ceaf 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20  key/value pairs 
1ceb0 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67  then fall throug
1ceb1 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
1ceb2 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
1ceb3 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63  n.  But if the c
1ceb4 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 61 73  ursor backup was
1ceb5 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20   successful,.** 
1ceb6 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
1ceb7 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68   to P2..**.** Th
1ceb8 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74  e P1 cursor must
1ceb9 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74   be for a real t
1ceba 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75  able, not a pseu
1cebb 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73  do-table..*/.cas
1cebc 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 20 20  e OP_Prev:      
1cebd 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63      /* jump */.c
1cebe 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 20  ase OP_Next: {  
1cebf 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
1cec0 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1cec1 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
1cec2 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b  Crsr;.  int res;
1cec3 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e  ..  CHECK_FOR_IN
1cec4 54 45 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72  TERRUPT;.  asser
1cec5 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1cec6 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1cec7 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d  sor );.  pC = p-
1cec8 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1cec9 0a 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b 0a  .  if( pC==0 ){.
1ceca 20 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53      break;  /* S
1cecb 65 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 20  ee ticket #2273 
1cecc 2a 2f 0a 20 20 7d 0a 20 20 70 43 72 73 72 20 3d  */.  }.  pCrsr =
1cecd 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20   pC->pCursor;.  
1cece 61 73 73 65 72 74 28 20 70 43 72 73 72 20 29 3b  assert( pCrsr );
1cecf 0a 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 61 73  .  res = 1;.  as
1ced0 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72  sert( pC->deferr
1ced1 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20  edMoveto==0 );. 
1ced2 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64   rc = pOp->opcod
1ced3 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c  e==OP_Next ? sql
1ced4 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43  ite3BtreeNext(pC
1ced5 72 73 72 2c 20 26 72 65 73 29 20 3a 0a 20 20 20  rsr, &res) :.   
1ced6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ced7 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1ced8 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
1ced9 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20  pCrsr, &res);.  
1ceda 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75  pC->nullRow = (u
1cedb 38 29 72 65 73 3b 0a 20 20 70 43 2d 3e 63 61 63  8)res;.  pC->cac
1cedc 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1cedd 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 72 65  _STALE;.  if( re
1cede 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d  s==0 ){.    pc =
1cedf 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1cee0 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 20    if( pOp->p5 ) 
1cee1 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d  p->aCounter[pOp-
1cee2 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66  >p5-1]++;.#ifdef
1cee3 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20   SQLITE_TEST.   
1cee4 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f   sqlite3_search_
1cee5 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
1cee6 20 20 7d 0a 20 20 70 43 2d 3e 72 6f 77 69 64 49    }.  pC->rowidI
1cee7 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 62 72  sValid = 0;.  br
1cee8 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cee9 65 3a 20 49 64 78 49 6e 73 65 72 74 20 50 31 20  e: IdxInsert P1 
1ceea 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a  P2 P3 * P5.**.**
1ceeb 20 52 65 67 69 73 74 65 72 20 50 32 20 68 6f 6c   Register P2 hol
1ceec 64 73 20 61 20 53 51 4c 20 69 6e 64 65 78 20 6b  ds a SQL index k
1ceed 65 79 20 6d 61 64 65 20 75 73 69 6e 67 20 74 68  ey made using th
1ceee 65 0a 2a 2a 20 4d 61 6b 65 52 65 63 6f 72 64 20  e.** MakeRecord 
1ceef 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 54  instructions.  T
1cef0 68 69 73 20 6f 70 63 6f 64 65 20 77 72 69 74 65  his opcode write
1cef1 73 20 74 68 61 74 20 6b 65 79 0a 2a 2a 20 69 6e  s that key.** in
1cef2 74 6f 20 74 68 65 20 69 6e 64 65 78 20 50 31 2e  to the index P1.
1cef3 20 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 65    Data for the e
1cef4 6e 74 72 79 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a  ntry is nil..**.
1cef5 2a 2a 20 50 33 20 69 73 20 61 20 66 6c 61 67 20  ** P3 is a flag 
1cef6 74 68 61 74 20 70 72 6f 76 69 64 65 73 20 61 20  that provides a 
1cef7 68 69 6e 74 20 74 6f 20 74 68 65 20 62 2d 74 72  hint to the b-tr
1cef8 65 65 20 6c 61 79 65 72 20 74 68 61 74 20 74 68  ee layer that th
1cef9 69 73 0a 2a 2a 20 69 6e 73 65 72 74 20 69 73 20  is.** insert is 
1cefa 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20  likely to be an 
1cefb 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  append..**.** Th
1cefc 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  is instruction o
1cefd 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 69 6e  nly works for in
1cefe 64 69 63 65 73 2e 20 20 54 68 65 20 65 71 75 69  dices.  The equi
1ceff 76 61 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69  valent instructi
1cf00 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61 62 6c 65 73  on.** for tables
1cf01 20 69 73 20 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a   is OP_Insert..*
1cf02 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 49 6e 73  /.case OP_IdxIns
1cf03 65 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  ert: {        /*
1cf04 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20 69 20   in2 */.  int i 
1cf05 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62  = pOp->p1;.  Vdb
1cf06 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1cf07 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1cf08 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1cf09 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1cf0a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
1cf0b 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  pCsr[i]!=0 );.  
1cf0c 61 73 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c  assert( pIn2->fl
1cf0d 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29  ags & MEM_Blob )
1cf0e 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d  ;.  if( (pCrsr =
1cf0f 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b   (pC = p->apCsr[
1cf10 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30  i])->pCursor)!=0
1cf11 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1cf12 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29  pC->isTable==0 )
1cf13 3b 0a 20 20 20 20 72 63 20 3d 20 45 78 70 61 6e  ;.    rc = Expan
1cf14 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 20  dBlob(pIn2);.   
1cf15 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1cf16 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
1cf17 6e 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0a  nKey = pIn2->n;.
1cf18 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1cf19 20 2a 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a   *zKey = pIn2->z
1cf1a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
1cf1b 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28  ite3BtreeInsert(
1cf1c 70 43 72 73 72 2c 20 7a 4b 65 79 2c 20 6e 4b 65  pCrsr, zKey, nKe
1cf1d 79 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70  y, "", 0, 0, pOp
1cf1e 2d 3e 70 33 2c 20 0a 20 20 20 20 20 20 20 20 20  ->p3, .         
1cf1f 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46   ((pOp->p5 & OPF
1cf20 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
1cf21 54 29 20 3f 20 70 43 2d 3e 73 65 65 6b 52 65 73  T) ? pC->seekRes
1cf22 75 6c 74 20 3a 20 30 29 0a 20 20 20 20 20 20 29  ult : 0).      )
1cf23 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1cf24 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1cf25 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  to==0 );.      p
1cf26 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1cf27 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1cf28 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b    }.  }.  break;
1cf29 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
1cf2a 64 78 44 65 6c 65 74 65 20 50 31 20 50 32 20 50  dxDelete P1 P2 P
1cf2b 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  3 * *.**.** The 
1cf2c 63 6f 6e 74 65 6e 74 20 6f 66 20 50 33 20 72 65  content of P3 re
1cf2d 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67  gisters starting
1cf2e 20 61 74 20 72 65 67 69 73 74 65 72 20 50 32 20   at register P2 
1cf2f 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63  form.** an unpac
1cf30 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 54  ked index key. T
1cf31 68 69 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76  his opcode remov
1cf32 65 73 20 74 68 61 74 20 65 6e 74 72 79 20 66 72  es that entry fr
1cf33 6f 6d 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78  om the .** index
1cf34 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f   opened by curso
1cf35 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P1..*/.case OP
1cf36 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b 0a 20 20  _IdxDelete: {.  
1cf37 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
1cf38 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1cf39 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
1cf3a 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20  Crsr;.  assert( 
1cf3b 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 61  pOp->p3>0 );.  a
1cf3c 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
1cf3d 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d   && pOp->p2+pOp-
1cf3e 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29  >p3<=p->nMem+1 )
1cf3f 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ;.  assert( i>=0
1cf40 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72   && i<p->nCursor
1cf41 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1cf42 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a  >apCsr[i]!=0 );.
1cf43 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 28    if( (pCrsr = (
1cf44 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
1cf45 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29  )->pCursor)!=0 )
1cf46 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20  {.    int res;. 
1cf47 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72     UnpackedRecor
1cf48 64 20 72 3b 0a 20 20 20 20 72 2e 70 4b 65 79 49  d r;.    r.pKeyI
1cf49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e  nfo = pC->pKeyIn
1cf4a 66 6f 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64  fo;.    r.nField
1cf4b 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 33 3b   = (u16)pOp->p3;
1cf4c 0a 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 30  .    r.flags = 0
1cf4d 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26  ;.    r.aMem = &
1cf4e 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  p->aMem[pOp->p2]
1cf4f 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1cf50 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
1cf51 61 63 6b 65 64 28 70 43 72 73 72 2c 20 26 72 2c  acked(pCrsr, &r,
1cf52 20 30 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20   0, 0, &res);.  
1cf53 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1cf54 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 20 29 7b  _OK && res==0 ){
1cf55 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1cf56 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 70  te3BtreeDelete(p
1cf57 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Crsr);.    }.   
1cf58 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66   assert( pC->def
1cf59 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
1cf5a 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53  ;.    pC->cacheS
1cf5b 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1cf5c 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  ALE;.  }.  break
1cf5d 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cf5e 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 20 2a  IdxRowid P1 P2 *
1cf5f 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65   * *.**.** Write
1cf60 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1cf61 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  2 an integer whi
1cf62 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 20 65  ch is the last e
1cf63 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 63 6f  ntry in the reco
1cf64 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64  rd at.** the end
1cf65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b 65   of the index ke
1cf66 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  y pointed to by 
1cf67 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 69 73  cursor P1.  This
1cf68 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c 64 20   integer should 
1cf69 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20  be.** the rowid 
1cf6a 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74  of the table ent
1cf6b 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 69 73  ry to which this
1cf6c 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 6f 69   index entry poi
1cf6d 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  nts..**.** See a
1cf6e 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 6b 65  lso: Rowid, Make
1cf6f 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65 20  Record..*/.case 
1cf70 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b 20 20  OP_IdxRowid: {  
1cf71 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f              /* o
1cf72 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1cf73 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  /.  int i = pOp-
1cf74 3e 70 31 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  >p1;.  BtCursor 
1cf75 2a 70 43 72 73 72 3b 0a 20 20 56 64 62 65 43 75  *pCrsr;.  VdbeCu
1cf76 72 73 6f 72 20 2a 70 43 3b 0a 0a 0a 20 20 61 73  rsor *pC;...  as
1cf77 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1cf78 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1cf79 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
1cf7a 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  [i]!=0 );.  if( 
1cf7b 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20 70  (pCrsr = (pC = p
1cf7c 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75  ->apCsr[i])->pCu
1cf7d 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20  rsor)!=0 ){.    
1cf7e 69 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 72  i64 rowid;.    r
1cf7f 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  c = sqlite3VdbeC
1cf80 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b  ursorMoveto(pC);
1cf81 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
1cf82 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1cf83 65 72 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72  error;.    asser
1cf84 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  t( pC->deferredM
1cf85 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20  oveto==0 );.    
1cf86 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61  assert( pC->isTa
1cf87 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66  ble==0 );.    if
1cf88 28 20 21 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29  ( !pC->nullRow )
1cf89 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1cf8a 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64  ite3VdbeIdxRowid
1cf8b 28 70 43 72 73 72 2c 20 26 72 6f 77 69 64 29 3b  (pCrsr, &rowid);
1cf8c 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1cf8d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1cf8e 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1cf8f 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1cf90 20 20 20 7d 0a 20 20 20 20 20 20 4d 65 6d 53 65     }.      MemSe
1cf91 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1cf92 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20  MEM_Int);.      
1cf93 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 72 6f 77 69  pOut->u.i = rowi
1cf94 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  d;.    }.  }.  b
1cf95 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cf96 64 65 3a 20 49 64 78 47 45 20 50 31 20 50 32 20  de: IdxGE P1 P2 
1cf97 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54  P3 P4 P5.**.** T
1cf98 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20 76  he P4 register v
1cf99 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20  alues beginning 
1cf9a 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20  with P3 form an 
1cf9b 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a  unpacked index .
1cf9c 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74  ** key that omit
1cf9d 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f  s the ROWID.  Co
1cf9e 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 76  mpare this key v
1cf9f 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 65  alue against the
1cfa0 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20   index .** that 
1cfa1 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  P1 is currently 
1cfa2 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e  pointing to, ign
1cfa3 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20  oring the ROWID 
1cfa4 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e  on the P1 index.
1cfa5 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31  .**.** If the P1
1cfa6 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20   index entry is 
1cfa7 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
1cfa8 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79  equal to the key
1cfa9 20 76 61 6c 75 65 0a 2a 2a 20 74 68 65 6e 20 6a   value.** then j
1cfaa 75 6d 70 20 74 6f 20 50 32 2e 20 20 4f 74 68 65  ump to P2.  Othe
1cfab 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75  rwise fall throu
1cfac 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  gh to the next i
1cfad 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
1cfae 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a  * If P5 is non-z
1cfaf 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79  ero then the key
1cfb0 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61   value is increa
1cfb1 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f  sed by an epsilo
1cfb2 6e 20 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74  n .** prior to t
1cfb3 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20  he comparison.  
1cfb4 54 68 69 73 20 6d 61 6b 65 20 74 68 65 20 6f 70  This make the op
1cfb5 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49  code work like I
1cfb6 64 78 47 54 20 65 78 63 65 70 74 0a 2a 2a 20 74  dxGT except.** t
1cfb7 68 61 74 20 69 66 20 74 68 65 20 6b 65 79 20 66  hat if the key f
1cfb8 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 33 20  rom register P3 
1cfb9 69 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 74  is a prefix of t
1cfba 68 65 20 6b 65 79 20 69 6e 20 74 68 65 20 63 75  he key in the cu
1cfbb 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 20 72 65 73  rsor,.** the res
1cfbc 75 6c 74 20 69 73 20 66 61 6c 73 65 20 77 68 65  ult is false whe
1cfbd 72 65 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65  reas it would be
1cfbe 20 74 72 75 65 20 77 69 74 68 20 49 64 78 47 54   true with IdxGT
1cfbf 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1cfc0 49 64 78 4c 54 20 50 31 20 50 32 20 50 33 20 2a  IdxLT P1 P2 P3 *
1cfc1 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34   P5.**.** The P4
1cfc2 20 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73   register values
1cfc3 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
1cfc4 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63  P3 form an unpac
1cfc5 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65  ked index .** ke
1cfc6 79 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65  y that omits the
1cfc7 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65   ROWID.  Compare
1cfc8 20 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20   this key value 
1cfc9 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65  against the inde
1cfca 78 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73  x .** that P1 is
1cfcb 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1cfcc 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67  ing to, ignoring
1cfcd 20 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68   the ROWID on th
1cfce 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  e P1 index..**.*
1cfcf 2a 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65  * If the P1 inde
1cfd0 78 20 65 6e 74 72 79 20 69 73 20 6c 65 73 73 20  x entry is less 
1cfd1 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c  than the key val
1cfd2 75 65 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  ue then jump to 
1cfd3 50 32 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  P2..** Otherwise
1cfd4 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   fall through to
1cfd5 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
1cfd6 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ction..**.** If 
1cfd7 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74  P5 is non-zero t
1cfd8 68 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75  hen the key valu
1cfd9 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62  e is increased b
1cfda 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20 70 72 69  y an epsilon pri
1cfdb 6f 72 20 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f  or .** to the co
1cfdc 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20  mparison.  This 
1cfdd 6d 61 6b 65 73 20 74 68 65 20 6f 70 63 6f 64 65  makes the opcode
1cfde 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78 4c 45   work like IdxLE
1cfdf 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78  ..*/.case OP_Idx
1cfe0 4c 54 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  LT:          /* 
1cfe1 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73  jump, in3 */.cas
1cfe2 65 20 4f 50 5f 49 64 78 47 45 3a 20 7b 20 20 20  e OP_IdxGE: {   
1cfe3 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1cfe4 33 20 2a 2f 0a 20 20 69 6e 74 20 69 3d 20 70 4f  3 */.  int i= pO
1cfe5 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72  p->p1;.  VdbeCur
1cfe6 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65  sor *pC;..  asse
1cfe7 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1cfe8 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
1cfe9 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69  sert( p->apCsr[i
1cfea 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70  ]!=0 );.  if( (p
1cfeb 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
1cfec 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a  ->pCursor!=0 ){.
1cfed 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1cfee 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1cfef 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  r;.    assert( p
1cff0 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1cff1 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  o==0 );.    asse
1cff2 72 74 28 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 7c  rt( pOp->p5==0 |
1cff3 7c 20 70 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b 0a  | pOp->p5==1 );.
1cff4 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1cff5 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33  >p4type==P4_INT3
1cff6 32 20 29 3b 0a 20 20 20 20 72 2e 70 4b 65 79 49  2 );.    r.pKeyI
1cff7 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e  nfo = pC->pKeyIn
1cff8 66 6f 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64  fo;.    r.nField
1cff9 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 34 2e   = (u16)pOp->p4.
1cffa 69 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  i;.    if( pOp->
1cffb 70 35 20 29 7b 0a 20 20 20 20 20 20 72 2e 66 6c  p5 ){.      r.fl
1cffc 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49  ags = UNPACKED_I
1cffd 4e 43 52 4b 45 59 20 7c 20 55 4e 50 41 43 4b 45  NCRKEY | UNPACKE
1cffe 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a  D_IGNORE_ROWID;.
1cfff 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1d000 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43   r.flags = UNPAC
1d001 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44  KED_IGNORE_ROWID
1d002 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 2e 61 4d  ;.    }.    r.aM
1d003 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  em = &p->aMem[pO
1d004 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 72 63 20 3d  p->p3];.    rc =
1d005 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b   sqlite3VdbeIdxK
1d006 65 79 43 6f 6d 70 61 72 65 28 70 43 2c 20 26 72  eyCompare(pC, &r
1d007 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28  , &res);.    if(
1d008 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1d009 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 20 20 20  _IdxLT ){.      
1d00a 72 65 73 20 3d 20 2d 72 65 73 3b 0a 20 20 20 20  res = -res;.    
1d00b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
1d00c 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  ert( pOp->opcode
1d00d 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20 20  ==OP_IdxGE );.  
1d00e 20 20 20 20 72 65 73 2b 2b 3b 0a 20 20 20 20 7d      res++;.    }
1d00f 0a 20 20 20 20 69 66 28 20 72 65 73 3e 30 20 29  .    if( res>0 )
1d010 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
1d011 2d 3e 70 32 20 2d 20 31 20 3b 0a 20 20 20 20 7d  ->p2 - 1 ;.    }
1d012 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1d013 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 73 74  ./* Opcode: Dest
1d014 72 6f 79 20 50 31 20 50 32 20 50 33 20 2a 20 2a  roy P1 P2 P3 * *
1d015 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  .**.** Delete an
1d016 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65   entire database
1d017 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1d018 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20  whose root page 
1d019 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  in the database.
1d01a 2a 2a 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e  ** file is given
1d01b 20 62 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68   by P1..**.** Th
1d01c 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 65  e table being de
1d01d 73 74 72 6f 79 65 64 20 69 73 20 69 6e 20 74 68  stroyed is in th
1d01e 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1d01f 66 69 6c 65 20 69 66 20 50 33 3d 3d 30 2e 20 20  file if P3==0.  
1d020 49 66 0a 2a 2a 20 50 33 3d 3d 31 20 74 68 65 6e  If.** P3==1 then
1d021 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
1d022 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65   clear is in the
1d023 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
1d024 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74  ase file.** that
1d025 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72   is used to stor
1d026 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 20  e tables create 
1d027 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d  using CREATE TEM
1d028 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a  PORARY TABLE..**
1d029 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55  .** If AUTOVACUU
1d02a 4d 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68 65  M is enabled the
1d02b 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  n it is possible
1d02c 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 72 6f   that another ro
1d02d 6f 74 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74  ot page.** might
1d02e 20 62 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74   be moved into t
1d02f 68 65 20 6e 65 77 6c 79 20 64 65 6c 65 74 65 64  he newly deleted
1d030 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 6f 72   root page in or
1d031 64 65 72 20 74 6f 20 6b 65 65 70 20 61 6c 6c 0a  der to keep all.
1d032 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 63 6f  ** root pages co
1d033 6e 74 69 67 75 6f 75 73 20 61 74 20 74 68 65 20  ntiguous at the 
1d034 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
1d035 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20   database.  The 
1d036 66 6f 72 6d 65 72 0a 2a 2a 20 76 61 6c 75 65 20  former.** value 
1d037 6f 66 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  of the root page
1d038 20 74 68 61 74 20 6d 6f 76 65 64 20 2d 20 69 74   that moved - it
1d039 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 74  s value before t
1d03a 68 65 20 6d 6f 76 65 20 6f 63 63 75 72 72 65 64  he move occurred
1d03b 20 2d 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20   -.** is stored 
1d03c 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20  in register P2. 
1d03d 20 49 66 20 6e 6f 20 70 61 67 65 20 0a 2a 2a 20   If no page .** 
1d03e 6d 6f 76 65 6d 65 6e 74 20 77 61 73 20 72 65 71  movement was req
1d03f 75 69 72 65 64 20 28 62 65 63 61 75 73 65 20 74  uired (because t
1d040 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64  he table being d
1d041 72 6f 70 70 65 64 20 77 61 73 20 61 6c 72 65 61  ropped was alrea
1d042 64 79 20 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20  dy .** the last 
1d043 6f 6e 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  one in the datab
1d044 61 73 65 29 20 74 68 65 6e 20 61 20 7a 65 72 6f  ase) then a zero
1d045 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65   is stored in re
1d046 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 49 66  gister P2..** If
1d047 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 64   AUTOVACUUM is d
1d048 69 73 61 62 6c 65 64 20 74 68 65 6e 20 61 20 7a  isabled then a z
1d049 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ero is stored in
1d04a 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a   register P2..**
1d04b 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 6c  .** See also: Cl
1d04c 65 61 72 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44  ear.*/.case OP_D
1d04d 65 73 74 72 6f 79 3a 20 7b 20 20 20 20 20 2f 2a  estroy: {     /*
1d04e 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1d04f 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f 76 65 64   */.  int iMoved
1d050 3b 0a 20 20 69 6e 74 20 69 43 6e 74 3b 0a 23 69  ;.  int iCnt;.#i
1d051 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1d052 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
1d053 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 20   Vdbe *pVdbe;.  
1d054 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28  iCnt = 0;.  for(
1d055 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 62 65 3b  pVdbe=db->pVdbe;
1d056 20 70 56 64 62 65 3b 20 70 56 64 62 65 3d 70 56   pVdbe; pVdbe=pV
1d057 64 62 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  dbe->pNext){.   
1d058 20 69 66 28 20 70 56 64 62 65 2d 3e 6d 61 67 69   if( pVdbe->magi
1d059 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
1d05a 4e 20 26 26 20 70 56 64 62 65 2d 3e 69 6e 56 74  N && pVdbe->inVt
1d05b 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 70 56  abMethod<2 && pV
1d05c 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  dbe->pc>=0 ){.  
1d05d 20 20 20 20 69 43 6e 74 2b 2b 3b 0a 20 20 20 20      iCnt++;.    
1d05e 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 43  }.  }.#else.  iC
1d05f 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 56  nt = db->activeV
1d060 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20  dbeCnt;.#endif. 
1d061 20 69 66 28 20 69 43 6e 74 3e 31 20 29 7b 0a 20   if( iCnt>1 ){. 
1d062 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c     rc = SQLITE_L
1d063 4f 43 4b 45 44 3b 0a 20 20 20 20 70 2d 3e 65 72  OCKED;.    p->er
1d064 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41  rorAction = OE_A
1d065 62 6f 72 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  bort;.  }else{. 
1d066 20 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70     int iDb = pOp
1d067 2d 3e 70 33 3b 0a 20 20 20 20 61 73 73 65 72 74  ->p3;.    assert
1d068 28 20 69 43 6e 74 3d 3d 31 20 29 3b 0a 20 20 20  ( iCnt==1 );.   
1d069 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
1d06a 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 62  eeMask & (1<<iDb
1d06b 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20  ))!=0 );.    rc 
1d06c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72  = sqlite3BtreeDr
1d06d 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b  opTable(db->aDb[
1d06e 69 44 62 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70  iDb].pBt, pOp->p
1d06f 31 2c 20 26 69 4d 6f 76 65 64 29 3b 0a 20 20 20  1, &iMoved);.   
1d070 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1d071 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a  pOut, MEM_Int);.
1d072 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20      pOut->u.i = 
1d073 69 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20  iMoved;.#ifndef 
1d074 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
1d075 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72  VACUUM.    if( r
1d076 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1d077 69 4d 6f 76 65 64 21 3d 30 20 29 7b 0a 20 20 20  iMoved!=0 ){.   
1d078 20 20 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61     sqlite3RootPa
1d079 67 65 4d 6f 76 65 64 28 26 64 62 2d 3e 61 44 62  geMoved(&db->aDb
1d07a 5b 69 44 62 5d 2c 20 69 4d 6f 76 65 64 2c 20 70  [iDb], iMoved, p
1d07b 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 23  Op->p1);.    }.#
1d07c 65 6e 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 61  endif.  }.  brea
1d07d 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d07e 20 43 6c 65 61 72 20 50 31 20 50 32 20 50 33 0a   Clear P1 P2 P3.
1d07f 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c  **.** Delete all
1d080 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
1d081 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
1d082 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72  or index whose r
1d083 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74  oot page.** in t
1d084 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1d085 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e   is given by P1.
1d086 20 20 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65    But, unlike De
1d087 73 74 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a  stroy, do not.**
1d088 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c   remove the tabl
1d089 65 20 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d 20  e or index from 
1d08a 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1d08b 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62  e..**.** The tab
1d08c 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 20 69  le being clear i
1d08d 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  s in the main da
1d08e 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50  tabase file if P
1d08f 32 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d  2==0.  If.** P2=
1d090 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c  =1 then the tabl
1d091 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73  e to be clear is
1d092 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72   in the auxiliar
1d093 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  y database file.
1d094 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20  ** that is used 
1d095 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20  to store tables 
1d096 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45  create using CRE
1d097 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41  ATE TEMPORARY TA
1d098 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  BLE..**.** If th
1d099 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 6e 6f  e P3 value is no
1d09a 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n-zero, then the
1d09b 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20   table referred 
1d09c 74 6f 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a  to must be an.**
1d09d 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61   intkey table (a
1d09e 6e 20 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74  n SQL table, not
1d09f 20 61 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74   an index). In t
1d0a0 68 69 73 20 63 61 73 65 20 74 68 65 20 72 6f 77  his case the row
1d0a1 20 63 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e   change .** coun
1d0a2 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  t is incremented
1d0a3 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
1d0a4 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61  f rows in the ta
1d0a5 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65  ble being cleare
1d0a6 64 2e 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20  d. .** If P3 is 
1d0a7 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
1d0a8 6f 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  o, then the valu
1d0a9 65 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  e stored in regi
1d0aa 73 74 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c  ster P3 is.** al
1d0ab 73 6f 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62  so incremented b
1d0ac 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  y the number of 
1d0ad 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c  rows in the tabl
1d0ae 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e  e being cleared.
1d0af 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1d0b0 20 44 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65   Destroy.*/.case
1d0b1 20 4f 50 5f 43 6c 65 61 72 3a 20 7b 0a 20 20 69   OP_Clear: {.  i
1d0b2 6e 74 20 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a  nt nChange = 0;.
1d0b3 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1d0b4 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
1d0b5 70 2d 3e 70 32 29 29 21 3d 30 20 29 3b 0a 20 20  p->p2))!=0 );.  
1d0b6 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1d0b7 65 43 6c 65 61 72 54 61 62 6c 65 28 0a 20 20 20  eClearTable(.   
1d0b8 20 20 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e     db->aDb[pOp->
1d0b9 70 32 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31  p2].pBt, pOp->p1
1d0ba 2c 20 28 70 4f 70 2d 3e 70 33 20 3f 20 26 6e 43  , (pOp->p3 ? &nC
1d0bb 68 61 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a  hange : 0).  );.
1d0bc 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b    if( pOp->p3 ){
1d0bd 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20  .    p->nChange 
1d0be 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20  += nChange;.    
1d0bf 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 7b  if( pOp->p3>0 ){
1d0c0 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 70  .      p->aMem[p
1d0c1 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 6e  Op->p3].u.i += n
1d0c2 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d 0a 20 20  Change;.    }.  
1d0c3 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1d0c4 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 54   Opcode: CreateT
1d0c5 61 62 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a  able P1 P2 * * *
1d0c6 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  .**.** Allocate 
1d0c7 61 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 74  a new table in t
1d0c8 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1d0c9 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f   file if P1==0 o
1d0ca 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69  r in the.** auxi
1d0cb 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66  liary database f
1d0cc 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20  ile if P1==1 or 
1d0cd 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  in an attached d
1d0ce 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31  atabase if.** P1
1d0cf 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72  >1.  Write the r
1d0d0 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
1d0d1 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
1d0d2 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65   into.** registe
1d0d3 72 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  r P2.**.** The d
1d0d4 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
1d0d5 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 6e  n a table and an
1d0d6 20 69 6e 64 65 78 20 69 73 20 74 68 69 73 3a 20   index is this: 
1d0d7 20 41 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a   A table must.**
1d0d8 20 68 61 76 65 20 61 20 34 2d 62 79 74 65 20 69   have a 4-byte i
1d0d9 6e 74 65 67 65 72 20 6b 65 79 20 61 6e 64 20 63  nteger key and c
1d0da 61 6e 20 68 61 76 65 20 61 72 62 69 74 72 61 72  an have arbitrar
1d0db 79 20 64 61 74 61 2e 20 20 41 6e 20 69 6e 64 65  y data.  An inde
1d0dc 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72 62 69  x.** has an arbi
1d0dd 74 72 61 72 79 20 6b 65 79 20 62 75 74 20 6e 6f  trary key but no
1d0de 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 65 65   data..**.** See
1d0df 20 61 6c 73 6f 3a 20 43 72 65 61 74 65 49 6e 64   also: CreateInd
1d0e0 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ex.*/./* Opcode:
1d0e1 20 43 72 65 61 74 65 49 6e 64 65 78 20 50 31 20   CreateIndex P1 
1d0e2 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41  P2 * * *.**.** A
1d0e3 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 69 6e  llocate a new in
1d0e4 64 65 78 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  dex in the main 
1d0e5 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66  database file if
1d0e6 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65   P1==0 or in the
1d0e7 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61  .** auxiliary da
1d0e8 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50  tabase file if P
1d0e9 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74  1==1 or in an at
1d0ea 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20  tached database 
1d0eb 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69  if.** P1>1.  Wri
1d0ec 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  te the root page
1d0ed 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e   number of the n
1d0ee 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a  ew table into.**
1d0ef 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a   register P2..**
1d0f0 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 6e 74  .** See document
1d0f1 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 65 61  ation on OP_Crea
1d0f2 74 65 54 61 62 6c 65 20 66 6f 72 20 61 64 64 69  teTable for addi
1d0f3 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
1d0f4 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  on..*/.case OP_C
1d0f5 72 65 61 74 65 49 6e 64 65 78 3a 20 20 20 20 20  reateIndex:     
1d0f6 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1d0f7 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 63 61 73  rerelease */.cas
1d0f8 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65  e OP_CreateTable
1d0f9 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  : {          /* 
1d0fa 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1d0fb 2a 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 20 3d 20  */.  int pgno = 
1d0fc 30 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a  0;.  int flags;.
1d0fd 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 61 73 73    Db *pDb;.  ass
1d0fe 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1d0ff 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e  && pOp->p1<db->n
1d100 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
1d101 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
1d102 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30  (1<<pOp->p1))!=0
1d103 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d   );.  pDb = &db-
1d104 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  >aDb[pOp->p1];. 
1d105 20 61 73 73 65 72 74 28 20 70 44 62 2d 3e 70 42   assert( pDb->pB
1d106 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f  t!=0 );.  if( pO
1d107 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 72  p->opcode==OP_Cr
1d108 65 61 74 65 54 61 62 6c 65 20 29 7b 0a 20 20 20  eateTable ){.   
1d109 20 2f 2a 20 66 6c 61 67 73 20 3d 20 42 54 52 45   /* flags = BTRE
1d10a 45 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20  E_INTKEY; */.   
1d10b 20 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 4c   flags = BTREE_L
1d10c 45 41 46 44 41 54 41 7c 42 54 52 45 45 5f 49 4e  EAFDATA|BTREE_IN
1d10d 54 4b 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  TKEY;.  }else{. 
1d10e 20 20 20 66 6c 61 67 73 20 3d 20 42 54 52 45 45     flags = BTREE
1d10f 5f 5a 45 52 4f 44 41 54 41 3b 0a 20 20 7d 0a 20  _ZERODATA;.  }. 
1d110 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1d111 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 44  eeCreateTable(pD
1d112 62 2d 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20 66  b->pBt, &pgno, f
1d113 6c 61 67 73 29 3b 0a 20 20 70 4f 75 74 2d 3e 75  lags);.  pOut->u
1d114 2e 69 20 3d 20 70 67 6e 6f 3b 0a 20 20 4d 65 6d  .i = pgno;.  Mem
1d115 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1d116 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72  , MEM_Int);.  br
1d117 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d118 65 3a 20 50 61 72 73 65 53 63 68 65 6d 61 20 50  e: ParseSchema P
1d119 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  1 P2 * P4 *.**.*
1d11a 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72 73 65  * Read and parse
1d11b 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f   all entries fro
1d11c 6d 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53  m the SQLITE_MAS
1d11d 54 45 52 20 74 61 62 6c 65 20 6f 66 20 64 61 74  TER table of dat
1d11e 61 62 61 73 65 20 50 31 0a 2a 2a 20 74 68 61 74  abase P1.** that
1d11f 20 6d 61 74 63 68 20 74 68 65 20 57 48 45 52 45   match the WHERE
1d120 20 63 6c 61 75 73 65 20 50 34 2e 20 20 50 32 20   clause P4.  P2 
1d121 69 73 20 74 68 65 20 22 66 6f 72 63 65 22 20 66  is the "force" f
1d122 6c 61 67 2e 20 20 20 41 6c 77 61 79 73 20 64 6f  lag.   Always do
1d123 0a 2a 2a 20 74 68 65 20 70 61 72 73 69 6e 67 20  .** the parsing 
1d124 69 66 20 50 32 20 69 73 20 74 72 75 65 2e 20 20  if P2 is true.  
1d125 49 66 20 50 32 20 69 73 20 66 61 6c 73 65 2c 20  If P2 is false, 
1d126 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
1d127 65 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20  e is a.** no-op 
1d128 69 66 20 74 68 65 20 73 63 68 65 6d 61 20 69 73  if the schema is
1d129 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6c   not currently l
1d12a 6f 61 64 65 64 2e 20 20 49 6e 20 6f 74 68 65 72  oaded.  In other
1d12b 20 77 6f 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a   words, if P2.**
1d12c 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 20 53   is false, the S
1d12d 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
1d12e 6c 65 20 69 73 20 6f 6e 6c 79 20 70 61 72 73 65  le is only parse
1d12f 64 20 69 66 20 74 68 65 20 72 65 73 74 20 6f 66  d if the rest of
1d130 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69   the.** schema i
1d131 73 20 61 6c 72 65 61 64 79 20 6c 6f 61 64 65 64  s already loaded
1d132 20 69 6e 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c   into the symbol
1d133 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
1d134 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65  is opcode invoke
1d135 73 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20  s the parser to 
1d136 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 72  create a new vir
1d137 74 75 61 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a  tual machine,.**
1d138 20 74 68 65 6e 20 72 75 6e 73 20 74 68 65 20 6e   then runs the n
1d139 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  ew virtual machi
1d13a 6e 65 2e 20 20 49 74 20 69 73 20 74 68 75 73 20  ne.  It is thus 
1d13b 61 20 72 65 2d 65 6e 74 72 61 6e 74 20 6f 70 63  a re-entrant opc
1d13c 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ode..*/.case OP_
1d13d 50 61 72 73 65 53 63 68 65 6d 61 3a 20 7b 0a 20  ParseSchema: {. 
1d13e 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e   int iDb = pOp->
1d13f 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  p1;.  assert( iD
1d140 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e  b>=0 && iDb<db->
1d141 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  nDb );..  /* If 
1d142 70 4f 70 2d 3e 70 32 20 69 73 20 30 2c 20 74 68  pOp->p2 is 0, th
1d143 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69  en this opcode i
1d144 73 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64  s being executed
1d145 20 74 6f 20 72 65 61 64 20 61 0a 20 20 2a 2a 20   to read a.  ** 
1d146 73 69 6e 67 6c 65 20 72 6f 77 2c 20 66 6f 72 20  single row, for 
1d147 65 78 61 6d 70 6c 65 20 74 68 65 20 72 6f 77 20  example the row 
1d148 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
1d149 20 61 20 6e 65 77 20 69 6e 64 65 78 0a 20 20 2a   a new index.  *
1d14a 2a 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69  * created by thi
1d14b 73 20 56 44 42 45 2c 20 66 72 6f 6d 20 74 68 65  s VDBE, from the
1d14c 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
1d14d 61 62 6c 65 2e 20 49 74 20 6f 6e 6c 79 0a 20 20  able. It only.  
1d14e 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 69 66 20  ** does this if 
1d14f 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1d150 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 63 68 65  g in-memory sche
1d151 6d 61 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a  ma is currently.
1d152 20 20 2a 2a 20 6c 6f 61 64 65 64 2e 20 4f 74 68    ** loaded. Oth
1d153 65 72 77 69 73 65 2c 20 74 68 65 20 6e 65 77 20  erwise, the new 
1d154 69 6e 64 65 78 20 64 65 66 69 6e 69 74 69 6f 6e  index definition
1d155 20 63 61 6e 20 62 65 20 6c 6f 61 64 65 64 20 61   can be loaded a
1d156 6c 6f 6e 67 0a 20 20 2a 2a 20 77 69 74 68 20 74  long.  ** with t
1d157 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 73  he rest of the s
1d158 63 68 65 6d 61 20 77 68 65 6e 20 69 74 20 69 73  chema when it is
1d159 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a 0a   required..  **.
1d15a 20 20 2a 2a 20 41 6c 74 68 6f 75 67 68 20 74 68    ** Although th
1d15b 65 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42  e mutex on the B
1d15c 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74  tShared object t
1d15d 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
1d15e 74 6f 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  to.  ** database
1d15f 20 69 44 62 20 28 74 68 65 20 64 61 74 61 62 61   iDb (the databa
1d160 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  se containing th
1d161 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
1d162 74 61 62 6c 65 0a 20 20 2a 2a 20 72 65 61 64 20  table.  ** read 
1d163 62 79 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  by this instruct
1d164 69 6f 6e 29 20 69 73 20 63 75 72 72 65 6e 74 6c  ion) is currentl
1d165 79 20 68 65 6c 64 2c 20 69 74 20 69 73 20 6e 65  y held, it is ne
1d166 63 65 73 73 61 72 79 20 74 6f 0a 20 20 2a 2a 20  cessary to.  ** 
1d167 6f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78  obtain the mutex
1d168 65 73 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68  es on all attach
1d169 65 64 20 64 61 74 61 62 61 73 65 73 20 62 65 66  ed databases bef
1d16a 6f 72 65 20 63 68 65 63 6b 69 6e 67 20 69 66 0a  ore checking if.
1d16b 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20    ** the schema 
1d16c 6f 66 20 69 44 62 20 69 73 20 6c 6f 61 64 65 64  of iDb is loaded
1d16d 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73  . This is becaus
1d16e 65 2c 20 61 74 20 74 68 65 20 73 74 61 72 74 20  e, at the start 
1d16f 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69  of.  ** the sqli
1d170 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20  te3_exec() call 
1d171 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65 20 77 69  below, SQLite wi
1d172 6c 6c 20 69 6e 76 6f 6b 65 20 0a 20 20 2a 2a 20  ll invoke .  ** 
1d173 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1d174 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c 20 6d  rAll(). If all m
1d175 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61  utexes are not a
1d176 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 65  lready held, the
1d177 0a 20 20 2a 2a 20 69 44 62 20 6d 75 74 65 78 20  .  ** iDb mutex 
1d178 6d 61 79 20 62 65 20 74 65 6d 70 6f 72 61 72 69  may be temporari
1d179 6c 79 20 72 65 6c 65 61 73 65 64 20 74 6f 20 61  ly released to a
1d17a 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2e 20 49  void deadlock. I
1d17b 66 20 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 70  f .  ** this hap
1d17c 70 65 6e 73 2c 20 74 68 65 6e 20 73 6f 6d 65 20  pens, then some 
1d17d 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d 61 79  other thread may
1d17e 20 64 65 6c 65 74 65 20 74 68 65 20 69 6e 2d 6d   delete the in-m
1d17f 65 6d 6f 72 79 20 0a 20 20 2a 2a 20 73 63 68 65  emory .  ** sche
1d180 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 69  ma of database i
1d181 44 62 20 62 65 66 6f 72 65 20 74 68 65 20 53 51  Db before the SQ
1d182 4c 20 73 74 61 74 65 6d 65 6e 74 20 72 75 6e 73  L statement runs
1d183 2e 20 54 68 65 20 73 63 68 65 6d 61 0a 20 20 2a  . The schema.  *
1d184 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65  * will not be re
1d185 6c 6f 61 64 65 64 20 62 65 63 75 61 73 65 20 74  loaded becuase t
1d186 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  he db->init.busy
1d187 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68   flag is set. Th
1d188 69 73 0a 20 20 2a 2a 20 63 61 6e 20 72 65 73 75  is.  ** can resu
1d189 6c 74 20 69 6e 20 61 20 22 6e 6f 20 73 75 63 68  lt in a "no such
1d18a 20 74 61 62 6c 65 3a 20 73 71 6c 69 74 65 5f 6d   table: sqlite_m
1d18b 61 73 74 65 72 22 20 6f 72 20 22 6d 61 6c 66 6f  aster" or "malfo
1d18c 72 6d 65 64 0a 20 20 2a 2a 20 64 61 74 61 62 61  rmed.  ** databa
1d18d 73 65 20 73 63 68 65 6d 61 22 20 65 72 72 6f 72  se schema" error
1d18e 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 20   being returned 
1d18f 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a  to the user..  *
1d190 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  /.  assert( sqli
1d191 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
1d192 65 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  ex(db->aDb[iDb].
1d193 70 42 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  pBt) );.  sqlite
1d194 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64  3BtreeEnterAll(d
1d195 62 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70  b);.  if( pOp->p
1d196 32 20 7c 7c 20 44 62 48 61 73 50 72 6f 70 65 72  2 || DbHasProper
1d197 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 53  ty(db, iDb, DB_S
1d198 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a  chemaLoaded) ){.
1d199 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1d19a 7a 4d 61 73 74 65 72 20 3d 20 53 43 48 45 4d 41  zMaster = SCHEMA
1d19b 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20  _TABLE(iDb);.   
1d19c 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20   char *zSql;.   
1d19d 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61   InitData initDa
1d19e 74 61 3b 0a 20 20 20 20 69 6e 69 74 44 61 74 61  ta;.    initData
1d19f 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 69 6e  .db = db;.    in
1d1a0 69 74 44 61 74 61 2e 69 44 62 20 3d 20 70 4f 70  itData.iDb = pOp
1d1a1 2d 3e 70 31 3b 0a 20 20 20 20 69 6e 69 74 44 61  ->p1;.    initDa
1d1a2 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d 20 26 70  ta.pzErrMsg = &p
1d1a3 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 7a  ->zErrMsg;.    z
1d1a4 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Sql = sqlite3MPr
1d1a5 69 6e 74 66 28 64 62 2c 0a 20 20 20 20 20 20 20  intf(db,.       
1d1a6 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f  "SELECT name, ro
1d1a7 6f 74 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d  otpage, sql FROM
1d1a8 20 27 25 71 27 2e 25 73 20 57 48 45 52 45 20 25   '%q'.%s WHERE %
1d1a9 73 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61  s",.       db->a
1d1aa 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a  Db[iDb].zName, z
1d1ab 4d 61 73 74 65 72 2c 20 70 4f 70 2d 3e 70 34 2e  Master, pOp->p4.
1d1ac 7a 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 71 6c  z);.    if( zSql
1d1ad 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==0 ){.      rc 
1d1ae 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1d1af 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1d1b0 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1d1b1 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
1d1b2 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69     assert( db->i
1d1b3 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20  nit.busy==0 );. 
1d1b4 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75       db->init.bu
1d1b5 73 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 6e  sy = 1;.      in
1d1b6 69 74 44 61 74 61 2e 72 63 20 3d 20 53 51 4c 49  itData.rc = SQLI
1d1b7 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 61 73 73  TE_OK;.      ass
1d1b8 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  ert( !db->malloc
1d1b9 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20  Failed );.      
1d1ba 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
1d1bb 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69  c(db, zSql, sqli
1d1bc 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c  te3InitCallback,
1d1bd 20 26 69 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a   &initData, 0);.
1d1be 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1d1bf 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69  LITE_OK ) rc = i
1d1c0 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20  nitData.rc;.    
1d1c1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1d1c2 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 20 20 20  db, zSql);.     
1d1c3 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d   db->init.busy =
1d1c4 20 30 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29   0;.      (void)
1d1c5 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1d1c6 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  db);.    }.  }. 
1d1c7 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1d1c8 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28  veAll(db);.  if(
1d1c9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
1d1ca 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f  M ){.    goto no
1d1cb 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 61  _mem;.  }.  brea
1d1cc 6b 3b 20 20 0a 7d 0a 0a 23 69 66 20 21 64 65 66  k;  .}..#if !def
1d1cd 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1d1ce 5f 41 4e 41 4c 59 5a 45 29 20 26 26 20 21 64 65  _ANALYZE) && !de
1d1cf 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1d1d0 54 5f 50 41 52 53 45 52 29 0a 2f 2a 20 4f 70 63  T_PARSER)./* Opc
1d1d1 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 69  ode: LoadAnalysi
1d1d2 73 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  s P1 * * * *.**.
1d1d3 2a 2a 20 52 65 61 64 20 74 68 65 20 73 71 6c 69  ** Read the sqli
1d1d4 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 66  te_stat1 table f
1d1d5 6f 72 20 64 61 74 61 62 61 73 65 20 50 31 20 61  or database P1 a
1d1d6 6e 64 20 6c 6f 61 64 20 74 68 65 20 63 6f 6e 74  nd load the cont
1d1d7 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74  ent.** of that t
1d1d8 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69 6e  able into the in
1d1d9 74 65 72 6e 61 6c 20 69 6e 64 65 78 20 68 61 73  ternal index has
1d1da 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 77  h table.  This w
1d1db 69 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 74 68 65  ill cause.** the
1d1dc 20 61 6e 61 6c 79 73 69 73 20 74 6f 20 62 65 20   analysis to be 
1d1dd 75 73 65 64 20 77 68 65 6e 20 70 72 65 70 61 72  used when prepar
1d1de 69 6e 67 20 61 6c 6c 20 73 75 62 73 65 71 75 65  ing all subseque
1d1df 6e 74 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 63  nt queries..*/.c
1d1e0 61 73 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79  ase OP_LoadAnaly
1d1e1 73 69 73 3a 20 7b 0a 20 20 69 6e 74 20 69 44 62  sis: {.  int iDb
1d1e2 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73   = pOp->p1;.  as
1d1e3 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
1d1e4 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
1d1e5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 6e 61   rc = sqlite3Ana
1d1e6 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 69 44  lysisLoad(db, iD
1d1e7 62 29 3b 0a 20 20 62 72 65 61 6b 3b 20 20 0a 7d  b);.  break;  .}
1d1e8 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
1d1e9 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1d1ea 41 4e 41 4c 59 5a 45 29 20 26 26 20 21 64 65 66  ANALYZE) && !def
1d1eb 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1d1ec 5f 50 41 52 53 45 52 29 20 20 2a 2f 0a 0a 2f 2a  _PARSER)  */../*
1d1ed 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 61 62   Opcode: DropTab
1d1ee 6c 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a  le P1 * * P4 *.*
1d1ef 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20  *.** Remove the 
1d1f0 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d  internal (in-mem
1d1f1 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74  ory) data struct
1d1f2 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69  ures that descri
1d1f3 62 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  be.** the table 
1d1f4 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 61  named P4 in data
1d1f5 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 69  base P1.  This i
1d1f6 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  s called after a
1d1f7 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 64 72 6f   table.** is dro
1d1f8 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f  pped in order to
1d1f9 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e   keep the intern
1d1fa 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  al representatio
1d1fb 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65  n of the.** sche
1d1fc 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69  ma consistent wi
1d1fd 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69  th what is on di
1d1fe 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44  sk..*/.case OP_D
1d1ff 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20 20 73 71  ropTable: {.  sq
1d200 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
1d201 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 4f  leteTable(db, pO
1d202 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a  p->p1, pOp->p4.z
1d203 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1d204 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 49 6e  * Opcode: DropIn
1d205 64 65 78 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a  dex P1 * * P4 *.
1d206 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  **.** Remove the
1d207 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65   internal (in-me
1d208 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63  mory) data struc
1d209 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72  tures that descr
1d20a 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78  ibe.** the index
1d20b 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74   named P4 in dat
1d20c 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20  abase P1.  This 
1d20d 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
1d20e 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20 64  an index.** is d
1d20f 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20  ropped in order 
1d210 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65  to keep the inte
1d211 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74  rnal representat
1d212 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63  ion of the.** sc
1d213 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20  hema consistent 
1d214 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20  with what is on 
1d215 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  disk..*/.case OP
1d216 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b 0a 20 20  _DropIndex: {.  
1d217 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
1d218 44 65 6c 65 74 65 49 6e 64 65 78 28 64 62 2c 20  DeleteIndex(db, 
1d219 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34  pOp->p1, pOp->p4
1d21a 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .z);.  break;.}.
1d21b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70  ./* Opcode: Drop
1d21c 54 72 69 67 67 65 72 20 50 31 20 2a 20 2a 20 50  Trigger P1 * * P
1d21d 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65  4 *.**.** Remove
1d21e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69   the internal (i
1d21f 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73  n-memory) data s
1d220 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64  tructures that d
1d221 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 74  escribe.** the t
1d222 72 69 67 67 65 72 20 6e 61 6d 65 64 20 50 34 20  rigger named P4 
1d223 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20  in database P1. 
1d224 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   This is called 
1d225 61 66 74 65 72 20 61 20 74 72 69 67 67 65 72 0a  after a trigger.
1d226 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e  ** is dropped in
1d227 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74   order to keep t
1d228 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72  he internal repr
1d229 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
1d22a 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73  e.** schema cons
1d22b 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 74  istent with what
1d22c 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a   is on disk..*/.
1d22d 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 72 69 67  case OP_DropTrig
1d22e 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33  ger: {.  sqlite3
1d22f 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
1d230 72 69 67 67 65 72 28 64 62 2c 20 70 4f 70 2d 3e  rigger(db, pOp->
1d231 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a  p1, pOp->p4.z);.
1d232 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66    break;.}...#if
1d233 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d234 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
1d235 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 65  ./* Opcode: Inte
1d236 67 72 69 74 79 43 6b 20 50 31 20 50 32 20 50 33  grityCk P1 P2 P3
1d237 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44 6f 20 61   * P5.**.** Do a
1d238 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 74 68  n analysis of th
1d239 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e  e currently open
1d23a 20 64 61 74 61 62 61 73 65 2e 20 20 53 74 6f 72   database.  Stor
1d23b 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72  e in.** register
1d23c 20 50 31 20 74 68 65 20 74 65 78 74 20 6f 66 20   P1 the text of 
1d23d 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1d23e 20 64 65 73 63 72 69 62 69 6e 67 20 61 6e 79 20   describing any 
1d23f 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 49 66 20  problems..** If 
1d240 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 72 65 20  no problems are 
1d241 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20 61 20 4e  found, store a N
1d242 55 4c 4c 20 69 6e 20 72 65 67 69 73 74 65 72 20  ULL in register 
1d243 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  P1..**.** The re
1d244 67 69 73 74 65 72 20 50 33 20 63 6f 6e 74 61 69  gister P3 contai
1d245 6e 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ns the maximum n
1d246 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 77 65 64  umber of allowed
1d247 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41 74 20 6d   errors..** At m
1d248 6f 73 74 20 72 65 67 28 50 33 29 20 65 72 72 6f  ost reg(P3) erro
1d249 72 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72  rs will be repor
1d24a 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72  ted..** In other
1d24b 20 77 6f 72 64 73 2c 20 74 68 65 20 61 6e 61 6c   words, the anal
1d24c 79 73 69 73 20 73 74 6f 70 73 20 61 73 20 73 6f  ysis stops as so
1d24d 6f 6e 20 61 73 20 72 65 67 28 50 31 29 20 65 72  on as reg(P1) er
1d24e 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20 73 65 65  rors are .** see
1d24f 6e 2e 20 20 52 65 67 28 50 31 29 20 69 73 20 75  n.  Reg(P1) is u
1d250 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20  pdated with the 
1d251 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
1d252 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a 2a 0a 2a   remaining..**.*
1d253 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20  * The root page 
1d254 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c 6c 20 74  numbers of all t
1d255 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61 74  ables in the dat
1d256 61 62 61 73 65 20 61 72 65 20 69 6e 74 65 67 65  abase are intege
1d257 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 72  r.** stored in r
1d258 65 67 28 50 31 29 2c 20 72 65 67 28 50 31 2b 31  eg(P1), reg(P1+1
1d259 29 2c 20 72 65 67 28 50 31 2b 32 29 2c 20 2e 2e  ), reg(P1+2), ..
1d25a 2e 2e 20 20 54 68 65 72 65 20 61 72 65 20 50 32  ..  There are P2
1d25b 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f 74 61 6c   tables.** total
1d25c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73  ..**.** If P5 is
1d25d 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 20 63   not zero, the c
1d25e 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f 6e 20  heck is done on 
1d25f 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61  the auxiliary da
1d260 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 2c 20  tabase.** file, 
1d261 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64 61 74  not the main dat
1d262 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
1d263 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73  * This opcode is
1d264 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
1d265 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69 74 79  nt the integrity
1d266 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e 0a 2a  _check pragma..*
1d267 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67 72  /.case OP_Integr
1d268 69 74 79 43 6b 3a 20 7b 0a 20 20 69 6e 74 20 6e  ityCk: {.  int n
1d269 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75  Root;      /* Nu
1d26a 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74  mber of tables t
1d26b 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65  o check.  (Numbe
1d26c 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e  r of root pages.
1d26d 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f  ) */.  int *aRoo
1d26e 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20  t;     /* Array 
1d26f 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62  of rootpage numb
1d270 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74  ers for tables t
1d271 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a  o be checked */.
1d272 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20    int j;        
1d273 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1d274 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b  r */.  int nErr;
1d275 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1d276 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70 6f 72   of errors repor
1d277 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ted */.  char *z
1d278 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74  ;        /* Text
1d279 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72 65   of the error re
1d27a 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  port */.  Mem *p
1d27b 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67  nErr;     /* Reg
1d27c 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72  ister keeping tr
1d27d 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65  ack of errors re
1d27e 6d 61 69 6e 69 6e 67 20 2a 2f 0a 20 20 0a 20 20  maining */.  .  
1d27f 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 32 3b  nRoot = pOp->p2;
1d280 0a 20 20 61 73 73 65 72 74 28 20 6e 52 6f 6f 74  .  assert( nRoot
1d281 3e 30 20 29 3b 0a 20 20 61 52 6f 6f 74 20 3d 20  >0 );.  aRoot = 
1d282 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
1d283 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e  aw(db, sizeof(in
1d284 74 29 2a 28 6e 52 6f 6f 74 2b 31 29 20 29 3b 0a  t)*(nRoot+1) );.
1d285 20 20 69 66 28 20 61 52 6f 6f 74 3d 3d 30 20 29    if( aRoot==0 )
1d286 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1d287 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
1d288 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
1d289 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 6e 45 72 72  >nMem );.  pnErr
1d28a 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1d28b 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p3];.  assert( 
1d28c 28 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20  (pnErr->flags & 
1d28d 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20  MEM_Int)!=0 );. 
1d28e 20 61 73 73 65 72 74 28 20 28 70 6e 45 72 72 2d   assert( (pnErr-
1d28f 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74  >flags & (MEM_St
1d290 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20  r|MEM_Blob))==0 
1d291 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e  );.  pIn1 = &p->
1d292 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1d293 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 52 6f 6f   for(j=0; j<nRoo
1d294 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 61 52 6f  t; j++){.    aRo
1d295 6f 74 5b 6a 5d 20 3d 20 28 69 6e 74 29 73 71 6c  ot[j] = (int)sql
1d296 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
1d297 28 26 70 49 6e 31 5b 6a 5d 29 3b 0a 20 20 7d 0a  (&pIn1[j]);.  }.
1d298 20 20 61 52 6f 6f 74 5b 6a 5d 20 3d 20 30 3b 0a    aRoot[j] = 0;.
1d299 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1d29a 35 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61  5<db->nDb );.  a
1d29b 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65  ssert( (p->btree
1d29c 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e  Mask & (1<<pOp->
1d29d 70 35 29 29 21 3d 30 20 29 3b 0a 20 20 7a 20 3d  p5))!=0 );.  z =
1d29e 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74   sqlite3BtreeInt
1d29f 65 67 72 69 74 79 43 68 65 63 6b 28 64 62 2d 3e  egrityCheck(db->
1d2a0 61 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e 70 42 74  aDb[pOp->p5].pBt
1d2a1 2c 20 61 52 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 0a  , aRoot, nRoot,.
1d2a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d2a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d2a4 20 28 69 6e 74 29 70 6e 45 72 72 2d 3e 75 2e 69   (int)pnErr->u.i
1d2a5 2c 20 26 6e 45 72 72 29 3b 0a 20 20 73 71 6c 69  , &nErr);.  sqli
1d2a6 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 52  te3DbFree(db, aR
1d2a7 6f 6f 74 29 3b 0a 20 20 70 6e 45 72 72 2d 3e 75  oot);.  pnErr->u
1d2a8 2e 69 20 2d 3d 20 6e 45 72 72 3b 0a 20 20 73 71  .i -= nErr;.  sq
1d2a9 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
1d2aa 75 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28  ull(pIn1);.  if(
1d2ab 20 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20   nErr==0 ){.    
1d2ac 61 73 73 65 72 74 28 20 7a 3d 3d 30 20 29 3b 0a  assert( z==0 );.
1d2ad 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 3d 3d 30    }else if( z==0
1d2ae 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f   ){.    goto no_
1d2af 6d 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  mem;.  }else{.  
1d2b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d2b1 53 65 74 53 74 72 28 70 49 6e 31 2c 20 7a 2c 20  SetStr(pIn1, z, 
1d2b2 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  -1, SQLITE_UTF8,
1d2b3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
1d2b4 20 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58    }.  UPDATE_MAX
1d2b5 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b  _BLOBSIZE(pIn1);
1d2b6 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
1d2b7 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e  angeEncoding(pIn
1d2b8 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  1, encoding);.  
1d2b9 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1d2ba 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  /* SQLITE_OMIT_I
1d2bb 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a  NTEGRITY_CHECK *
1d2bc 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f  /../* Opcode: Ro
1d2bd 77 53 65 74 41 64 64 20 50 31 20 50 32 20 2a 20  wSetAdd P1 P2 * 
1d2be 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 73 65 72 74  * *.**.** Insert
1d2bf 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
1d2c0 75 65 20 68 65 6c 64 20 62 79 20 72 65 67 69 73  ue held by regis
1d2c1 74 65 72 20 50 32 20 69 6e 74 6f 20 61 20 62 6f  ter P2 into a bo
1d2c2 6f 6c 65 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 68  olean index.** h
1d2c3 65 6c 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  eld in register 
1d2c4 50 31 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 73 73  P1..**.** An ass
1d2c5 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20  ertion fails if 
1d2c6 50 32 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74  P2 is not an int
1d2c7 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  eger..*/.case OP
1d2c8 5f 52 6f 77 53 65 74 41 64 64 3a 20 7b 20 20 20  _RowSetAdd: {   
1d2c9 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20      /* in2 */.  
1d2ca 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 4d 65 6d  Mem *pIdx;.  Mem
1d2cb 20 2a 70 56 61 6c 3b 0a 20 20 61 73 73 65 72 74   *pVal;.  assert
1d2cc 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70  ( pOp->p1>0 && p
1d2cd 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1d2ce 29 3b 0a 20 20 70 49 64 78 20 3d 20 26 70 2d 3e  );.  pIdx = &p->
1d2cf 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1d2d0 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1d2d1 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 3d 70  >0 && pOp->p2<=p
1d2d2 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 56 61 6c  ->nMem );.  pVal
1d2d3 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1d2d4 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p2];.  assert( 
1d2d5 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d  (pVal->flags & M
1d2d6 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20  EM_Int)!=0 );.  
1d2d7 69 66 28 20 28 70 49 64 78 2d 3e 66 6c 61 67 73  if( (pIdx->flags
1d2d8 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
1d2d9 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1d2da 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74  VdbeMemSetRowSet
1d2db 28 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28 20  (pIdx);.    if( 
1d2dc 28 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIdx->flags & M
1d2dd 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 20  EM_RowSet)==0 ) 
1d2de 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d  goto no_mem;.  }
1d2df 0a 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  .  sqlite3RowSet
1d2e0 49 6e 73 65 72 74 28 70 49 64 78 2d 3e 75 2e 70  Insert(pIdx->u.p
1d2e1 52 6f 77 53 65 74 2c 20 70 56 61 6c 2d 3e 75 2e  RowSet, pVal->u.
1d2e2 69 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  i);.  break;.}..
1d2e3 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65  /* Opcode: RowSe
1d2e4 74 52 65 61 64 20 50 31 20 50 32 20 50 33 20 2a  tRead P1 P2 P3 *
1d2e5 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74   *.**.** Extract
1d2e6 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61   the smallest va
1d2e7 6c 75 65 20 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e  lue from boolean
1d2e8 20 69 6e 64 65 78 20 50 31 20 61 6e 64 20 70 75   index P1 and pu
1d2e9 74 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74  t that value int
1d2ea 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33  o.** register P3
1d2eb 2e 20 20 4f 72 2c 20 69 66 20 62 6f 6f 6c 65 61  .  Or, if boolea
1d2ec 6e 20 69 6e 64 65 78 20 50 31 20 69 73 20 69 6e  n index P1 is in
1d2ed 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2c 20 6c  itially empty, l
1d2ee 65 61 76 65 20 50 33 0a 2a 2a 20 75 6e 63 68 61  eave P3.** uncha
1d2ef 6e 67 65 64 20 61 6e 64 20 6a 75 6d 70 20 74 6f  nged and jump to
1d2f0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e   instruction P2.
1d2f1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53  .*/.case OP_RowS
1d2f2 65 74 52 65 61 64 3a 20 7b 20 20 20 20 20 20 20  etRead: {       
1d2f3 2f 2a 20 6a 75 6d 70 2c 20 6f 75 74 33 20 2a 2f  /* jump, out3 */
1d2f4 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20  .  Mem *pIdx;.  
1d2f5 69 36 34 20 76 61 6c 3b 0a 20 20 61 73 73 65 72  i64 val;.  asser
1d2f6 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20  t( pOp->p1>0 && 
1d2f7 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p1<=p->nMem
1d2f8 20 29 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f   );.  CHECK_FOR_
1d2f9 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 49 64  INTERRUPT;.  pId
1d2fa 78 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  x = &p->aMem[pOp
1d2fb 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20  ->p1];.  pOut = 
1d2fc 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
1d2fd 5d 3b 0a 20 20 69 66 28 20 28 70 49 64 78 2d 3e  ];.  if( (pIdx->
1d2fe 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
1d2ff 65 74 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71  et)==0 .   || sq
1d300 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28  lite3RowSetNext(
1d301 70 49 64 78 2d 3e 75 2e 70 52 6f 77 53 65 74 2c  pIdx->u.pRowSet,
1d302 20 26 76 61 6c 29 3d 3d 30 0a 20 20 29 7b 0a 20   &val)==0.  ){. 
1d303 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f 6c 65 61     /* The boolea
1d304 6e 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79  n index is empty
1d305 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
1d306 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49  dbeMemSetNull(pI
1d307 64 78 29 3b 0a 20 20 20 20 70 63 20 3d 20 70 4f  dx);.    pc = pO
1d308 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c  p->p2 - 1;.  }el
1d309 73 65 7b 0a 20 20 20 20 2f 2a 20 41 20 76 61 6c  se{.    /* A val
1d30a 75 65 20 77 61 73 20 70 75 6c 6c 65 64 20 66 72  ue was pulled fr
1d30b 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a  om the index */.
1d30c 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1d30d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33  >p3>0 && pOp->p3
1d30e 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  <=p->nMem );.   
1d30f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1d310 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 76 61  etInt64(pOut, va
1d311 6c 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  l);.  }.  break;
1d312 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .}../* Opcode: R
1d313 6f 77 53 65 74 54 65 73 74 20 50 31 20 50 32 20  owSetTest P1 P2 
1d314 50 33 20 50 34 0a 2a 2a 0a 2a 2a 20 52 65 67 69  P3 P4.**.** Regi
1d315 73 74 65 72 20 50 33 20 69 73 20 61 73 73 75 6d  ster P3 is assum
1d316 65 64 20 74 6f 20 68 6f 6c 64 20 61 20 36 34 2d  ed to hold a 64-
1d317 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75  bit integer valu
1d318 65 2e 20 49 66 20 72 65 67 69 73 74 65 72 20 50  e. If register P
1d319 31 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20  1.** contains a 
1d31a 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 61 6e  RowSet object an
1d31b 64 20 74 68 61 74 20 52 6f 77 53 65 74 20 6f 62  d that RowSet ob
1d31c 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a  ject contains.**
1d31d 20 74 68 65 20 76 61 6c 75 65 20 68 65 6c 64 20   the value held 
1d31e 69 6e 20 50 33 2c 20 6a 75 6d 70 20 74 6f 20 72  in P3, jump to r
1d31f 65 67 69 73 74 65 72 20 50 32 2e 20 4f 74 68 65  egister P2. Othe
1d320 72 77 69 73 65 2c 20 69 6e 73 65 72 74 20 74 68  rwise, insert th
1d321 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 6e 20  e.** integer in 
1d322 50 33 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53  P3 into the RowS
1d323 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 65 20  et and continue 
1d324 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78  on to the.** nex
1d325 74 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  t opcode..**.** 
1d326 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a 65 63  The RowSet objec
1d327 74 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66  t is optimized f
1d328 6f 72 20 74 68 65 20 63 61 73 65 20 77 68 65 72  or the case wher
1d329 65 20 73 75 63 63 65 73 73 69 76 65 20 73 65 74  e successive set
1d32a 73 0a 2a 2a 20 6f 66 20 69 6e 74 65 67 65 72 73  s.** of integers
1d32b 2c 20 77 68 65 72 65 20 65 61 63 68 20 73 65 74  , where each set
1d32c 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 75 70   contains no dup
1d32d 6c 69 63 61 74 65 73 2e 20 45 61 63 68 20 73 65  licates. Each se
1d32e 74 0a 2a 2a 20 6f 66 20 76 61 6c 75 65 73 20 69  t.** of values i
1d32f 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  s identified by 
1d330 61 20 75 6e 69 71 75 65 20 50 34 20 76 61 6c 75  a unique P4 valu
1d331 65 2e 20 54 68 65 20 66 69 72 73 74 20 73 65 74  e. The first set
1d332 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 50 34  .** must have P4
1d333 3d 3d 30 2c 20 74 68 65 20 66 69 6e 61 6c 20 73  ==0, the final s
1d334 65 74 20 50 34 3d 2d 31 2e 20 20 50 34 20 6d 75  et P4=-1.  P4 mu
1d335 73 74 20 62 65 20 65 69 74 68 65 72 20 2d 31 20  st be either -1 
1d336 6f 72 0a 2a 2a 20 6e 6f 6e 2d 6e 65 67 61 74 69  or.** non-negati
1d337 76 65 2e 20 20 46 6f 72 20 6e 6f 6e 2d 6e 65 67  ve.  For non-neg
1d338 61 74 69 76 65 20 76 61 6c 75 65 73 20 6f 66 20  ative values of 
1d339 50 34 20 6f 6e 6c 79 20 74 68 65 20 6c 6f 77 65  P4 only the lowe
1d33a 72 20 34 0a 2a 2a 20 62 69 74 73 20 61 72 65 20  r 4.** bits are 
1d33b 73 69 67 6e 69 66 69 63 61 6e 74 2e 0a 2a 2a 0a  significant..**.
1d33c 2a 2a 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 6f  ** This allows o
1d33d 70 74 69 6d 69 7a 61 74 69 6f 6e 73 3a 20 28 61  ptimizations: (a
1d33e 29 20 77 68 65 6e 20 50 34 3d 3d 30 20 74 68 65  ) when P4==0 the
1d33f 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f  re is no need to
1d340 20 74 65 73 74 0a 2a 2a 20 74 68 65 20 72 6f 77   test.** the row
1d341 73 65 74 20 6f 62 6a 65 63 74 20 66 6f 72 20 50  set object for P
1d342 33 2c 20 61 73 20 69 74 20 69 73 20 67 75 61 72  3, as it is guar
1d343 61 6e 74 65 65 64 20 6e 6f 74 20 74 6f 20 63 6f  anteed not to co
1d344 6e 74 61 69 6e 20 69 74 2c 0a 2a 2a 20 28 62 29  ntain it,.** (b)
1d345 20 77 68 65 6e 20 50 34 3d 3d 2d 31 20 74 68 65   when P4==-1 the
1d346 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f  re is no need to
1d347 20 69 6e 73 65 72 74 20 74 68 65 20 76 61 6c 75   insert the valu
1d348 65 2c 20 61 73 20 69 74 20 77 69 6c 6c 0a 2a 2a  e, as it will.**
1d349 20 6e 65 76 65 72 20 62 65 20 74 65 73 74 65 64   never be tested
1d34a 20 66 6f 72 2c 20 61 6e 64 20 28 63 29 20 77 68   for, and (c) wh
1d34b 65 6e 20 61 20 76 61 6c 75 65 20 74 68 61 74 20  en a value that 
1d34c 69 73 20 70 61 72 74 20 6f 66 20 73 65 74 20 58  is part of set X
1d34d 20 69 73 0a 2a 2a 20 69 6e 73 65 72 74 65 64 2c   is.** inserted,
1d34e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
1d34f 64 20 74 6f 20 73 65 61 72 63 68 20 74 6f 20 73  d to search to s
1d350 65 65 20 69 66 20 74 68 65 20 73 61 6d 65 20 76  ee if the same v
1d351 61 6c 75 65 20 77 61 73 0a 2a 2a 20 70 72 65 76  alue was.** prev
1d352 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 64 20  iously inserted 
1d353 61 73 20 70 61 72 74 20 6f 66 20 73 65 74 20 58  as part of set X
1d354 20 28 6f 6e 6c 79 20 69 66 20 69 74 20 77 61 73   (only if it was
1d355 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 69   previously.** i
1d356 6e 73 65 72 74 65 64 20 61 73 20 70 61 72 74 20  nserted as part 
1d357 6f 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 65  of some other se
1d358 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  t)..*/.case OP_R
1d359 6f 77 53 65 74 54 65 73 74 3a 20 7b 20 20 20 20  owSetTest: {    
1d35a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d35b 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69   /* jump, in1, i
1d35c 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69 53 65 74  n3 */.  int iSet
1d35d 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20   = pOp->p4.i;.  
1d35e 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c  assert( pIn3->fl
1d35f 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 0a  ags&MEM_Int );..
1d360 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
1d361 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 20   anything other 
1d362 74 68 61 6e 20 61 20 72 6f 77 73 65 74 20 6f 62  than a rowset ob
1d363 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63  ject in memory c
1d364 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20 64 65 6c  ell P1,.  ** del
1d365 65 74 65 20 69 74 20 6e 6f 77 20 61 6e 64 20 69  ete it now and i
1d366 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 77 69 74  nitialize P1 wit
1d367 68 20 61 6e 20 65 6d 70 74 79 20 72 6f 77 73 65  h an empty rowse
1d368 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 49  t.  */.  if( (pI
1d369 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1d36a 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20  RowSet)==0 ){.  
1d36b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d36c 53 65 74 52 6f 77 53 65 74 28 70 49 6e 31 29 3b  SetRowSet(pIn1);
1d36d 0a 20 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  .    if( (pIn1->
1d36e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
1d36f 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f  et)==0 ) goto no
1d370 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  _mem;.  }..  ass
1d371 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1d372 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20  ==P4_INT32 );.  
1d373 61 73 73 65 72 74 28 20 69 53 65 74 3d 3d 2d 31  assert( iSet==-1
1d374 20 7c 7c 20 69 53 65 74 3e 3d 30 20 29 3b 0a 20   || iSet>=0 );. 
1d375 20 69 66 28 20 69 53 65 74 20 29 7b 0a 20 20 20   if( iSet ){.   
1d376 20 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20   int exists;.   
1d377 20 65 78 69 73 74 73 20 3d 20 73 71 6c 69 74 65   exists = sqlite
1d378 33 52 6f 77 53 65 74 54 65 73 74 28 70 49 6e 31  3RowSetTest(pIn1
1d379 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 0a 20 20  ->u.pRowSet, .  
1d37a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d37b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 75 38               (u8
1d37c 29 28 69 53 65 74 3e 3d 30 20 3f 20 69 53 65 74  )(iSet>=0 ? iSet
1d37d 20 26 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a   & 0xf : 0xff),.
1d37e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d37f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1d380 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69  In3->u.i);.    i
1d381 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20 20 20  f( exists ){.   
1d382 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1d383 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  - 1;.      break
1d384 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
1d385 28 20 69 53 65 74 3e 3d 30 20 29 7b 0a 20 20 20  ( iSet>=0 ){.   
1d386 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e   sqlite3RowSetIn
1d387 73 65 72 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f  sert(pIn1->u.pRo
1d388 77 53 65 74 2c 20 70 49 6e 33 2d 3e 75 2e 69 29  wSet, pIn3->u.i)
1d389 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d38a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1d38b 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f  E_OMIT_TRIGGER./
1d38c 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 65 78  * Opcode: Contex
1d38d 74 50 75 73 68 20 2a 20 2a 20 2a 20 0a 2a 2a 0a  tPush * * * .**.
1d38e 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72  ** Save the curr
1d38f 65 6e 74 20 56 64 62 65 20 63 6f 6e 74 65 78 74  ent Vdbe context
1d390 20 73 75 63 68 20 74 68 61 74 20 69 74 20 63 61   such that it ca
1d391 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 62 79  n be restored by
1d392 20 61 20 43 6f 6e 74 65 78 74 50 6f 70 0a 2a 2a   a ContextPop.**
1d393 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 63 6f 6e   opcode. The con
1d394 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 20  text stores the 
1d395 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 20  last insert row 
1d396 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61  id, the last sta
1d397 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 0a 2a 2a  tement change.**
1d398 20 63 6f 75 6e 74 2c 20 61 6e 64 20 74 68 65 20   count, and the 
1d399 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e  current statemen
1d39a 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2e 0a  t change count..
1d39b 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 74 65  */.case OP_Conte
1d39c 78 74 50 75 73 68 3a 20 7b 0a 20 20 69 6e 74 20  xtPush: {.  int 
1d39d 69 20 3d 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74  i = p->contextSt
1d39e 61 63 6b 54 6f 70 2b 2b 3b 0a 20 20 43 6f 6e 74  ackTop++;.  Cont
1d39f 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 3b 0a 0a  ext *pContext;..
1d3a0 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29    assert( i>=0 )
1d3a1 3b 0a 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 54  ;.  /* FIX ME: T
1d3a2 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 61 6c  his should be al
1d3a3 6c 6f 63 61 74 65 64 20 61 73 20 70 61 72 74 20  located as part 
1d3a4 6f 66 20 74 68 65 20 76 64 62 65 20 61 74 20 63  of the vdbe at c
1d3a5 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20  ompile-time */. 
1d3a6 20 69 66 28 20 69 3e 3d 70 2d 3e 63 6f 6e 74 65   if( i>=p->conte
1d3a7 78 74 53 74 61 63 6b 44 65 70 74 68 20 29 7b 0a  xtStackDepth ){.
1d3a8 20 20 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74      p->contextSt
1d3a9 61 63 6b 44 65 70 74 68 20 3d 20 69 2b 31 3b 0a  ackDepth = i+1;.
1d3aa 20 20 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74      p->contextSt
1d3ab 61 63 6b 20 3d 20 73 71 6c 69 74 65 33 44 62 52  ack = sqlite3DbR
1d3ac 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c  eallocOrFree(db,
1d3ad 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b   p->contextStack
1d3ae 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1d3af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d3b0 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65              size
1d3b1 6f 66 28 43 6f 6e 74 65 78 74 29 2a 28 69 2b 31  of(Context)*(i+1
1d3b2 29 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 63  ));.    if( p->c
1d3b3 6f 6e 74 65 78 74 53 74 61 63 6b 3d 3d 30 20 29  ontextStack==0 )
1d3b4 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1d3b5 7d 0a 20 20 70 43 6f 6e 74 65 78 74 20 3d 20 26  }.  pContext = &
1d3b6 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b  p->contextStack[
1d3b7 69 5d 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e  i];.  pContext->
1d3b8 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e  lastRowid = db->
1d3b9 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 70 43 6f  lastRowid;.  pCo
1d3ba 6e 74 65 78 74 2d 3e 6e 43 68 61 6e 67 65 20 3d  ntext->nChange =
1d3bb 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 62   p->nChange;.  b
1d3bc 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d3bd 64 65 3a 20 43 6f 6e 74 65 78 74 50 6f 70 20 2a  de: ContextPop *
1d3be 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a 20 52 65 73 74   * * .**.** Rest
1d3bf 6f 72 65 20 74 68 65 20 56 64 62 65 20 63 6f 6e  ore the Vdbe con
1d3c0 74 65 78 74 20 74 6f 20 74 68 65 20 73 74 61 74  text to the stat
1d3c1 65 20 69 74 20 77 61 73 20 69 6e 20 77 68 65 6e  e it was in when
1d3c2 20 63 6f 6e 74 65 78 74 50 75 73 68 20 77 61 73   contextPush was
1d3c3 20 6c 61 73 74 0a 2a 2a 20 65 78 65 63 75 74 65   last.** execute
1d3c4 64 2e 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73  d. The context s
1d3c5 74 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69  tores the last i
1d3c6 6e 73 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68  nsert row id, th
1d3c7 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74  e last statement
1d3c8 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  .** change count
1d3c9 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  , and the curren
1d3ca 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e  t statement chan
1d3cb 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73  ge count..*/.cas
1d3cc 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 3a  e OP_ContextPop:
1d3cd 20 7b 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43   {.  Context *pC
1d3ce 6f 6e 74 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e  ontext = &p->con
1d3cf 74 65 78 74 53 74 61 63 6b 5b 2d 2d 70 2d 3e 63  textStack[--p->c
1d3d0 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 5d 3b  ontextStackTop];
1d3d1 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6f  .  assert( p->co
1d3d2 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 3e 3d 30  ntextStackTop>=0
1d3d3 20 29 3b 0a 20 20 64 62 2d 3e 6c 61 73 74 52 6f   );.  db->lastRo
1d3d4 77 69 64 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e  wid = pContext->
1d3d5 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 70 2d 3e  lastRowid;.  p->
1d3d6 6e 43 68 61 6e 67 65 20 3d 20 70 43 6f 6e 74 65  nChange = pConte
1d3d7 78 74 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 62  xt->nChange;.  b
1d3d8 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1d3d9 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  * #ifndef SQLITE
1d3da 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f  _OMIT_TRIGGER */
1d3db 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1d3dc 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
1d3dd 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d  ENT./* Opcode: M
1d3de 65 6d 4d 61 78 20 50 31 20 50 32 20 2a 20 2a 20  emMax P1 P2 * * 
1d3df 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  *.**.** Set the 
1d3e0 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65  value of registe
1d3e1 72 20 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69  r P1 to the maxi
1d3e2 6d 75 6d 20 6f 66 20 69 74 73 20 63 75 72 72 65  mum of its curre
1d3e3 6e 74 20 76 61 6c 75 65 0a 2a 2a 20 61 6e 64 20  nt value.** and 
1d3e4 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1d3e5 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ister P2..**.** 
1d3e6 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1d3e7 20 74 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72   throws an error
1d3e8 20 69 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63   if the memory c
1d3e9 65 6c 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69  ell is not initi
1d3ea 61 6c 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67  ally.** an integ
1d3eb 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d  er..*/.case OP_M
1d3ec 65 6d 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20  emMax: {        
1d3ed 2f 2a 20 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a 20  /* in1, in2 */. 
1d3ee 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1d3ef 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b  ntegerify(pIn1);
1d3f0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1d3f1 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32  mIntegerify(pIn2
1d3f2 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75  );.  if( pIn1->u
1d3f3 2e 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20  .i<pIn2->u.i){. 
1d3f4 20 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70     pIn1->u.i = p
1d3f5 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20  In2->u.i;.  }.  
1d3f6 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1d3f7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  /* SQLITE_OMIT_A
1d3f8 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a  UTOINCREMENT */.
1d3f9 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f  ./* Opcode: IfPo
1d3fa 73 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  s P1 P2 * * *.**
1d3fb 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1d3fc 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20   of register P1 
1d3fd 69 73 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c  is 1 or greater,
1d3fe 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a   jump to P2..**.
1d3ff 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c  ** It is illegal
1d400 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73   to use this ins
1d401 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65  truction on a re
1d402 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73  gister that does
1d403 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  .** not contain 
1d404 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20  an integer.  An 
1d405 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
1d406 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79  will result if y
1d407 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20  ou try..*/.case 
1d408 4f 50 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20  OP_IfPos: {     
1d409 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20     /* jump, in1 
1d40a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  */.  assert( pIn
1d40b 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74  1->flags&MEM_Int
1d40c 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   );.  if( pIn1->
1d40d 75 2e 69 3e 30 20 29 7b 0a 20 20 20 20 20 70 63  u.i>0 ){.     pc
1d40e 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1d40f 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1d410 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67  /* Opcode: IfNeg
1d411 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1d412 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
1d413 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69  of register P1 i
1d414 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f  s less than zero
1d415 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a  , jump to P2. .*
1d416 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67  *.** It is illeg
1d417 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69  al to use this i
1d418 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20  nstruction on a 
1d419 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64 6f  register that do
1d41a 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69  es.** not contai
1d41b 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41  n an integer.  A
1d41c 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c  n assertion faul
1d41d 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66  t will result if
1d41e 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73   you try..*/.cas
1d41f 65 20 4f 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20  e OP_IfNeg: {   
1d420 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1d421 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  1 */.  assert( p
1d422 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49  In1->flags&MEM_I
1d423 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31  nt );.  if( pIn1
1d424 2d 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20  ->u.i<0 ){.     
1d425 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1d426 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d427 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a  ../* Opcode: IfZ
1d428 65 72 6f 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ero P1 P2 * * *.
1d429 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c  **.** If the val
1d42a 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  ue of register P
1d42b 31 20 69 73 20 65 78 61 63 74 6c 79 20 30 2c 20  1 is exactly 0, 
1d42c 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a  jump to P2. .**.
1d42d 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c  ** It is illegal
1d42e 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73   to use this ins
1d42f 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65  truction on a re
1d430 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73  gister that does
1d431 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  .** not contain 
1d432 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20  an integer.  An 
1d433 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
1d434 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79  will result if y
1d435 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20  ou try..*/.case 
1d436 4f 50 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20  OP_IfZero: {    
1d437 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
1d438 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49   */.  assert( pI
1d439 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e  n1->flags&MEM_In
1d43a 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d  t );.  if( pIn1-
1d43b 3e 75 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20  >u.i==0 ){.     
1d43c 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1d43d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d43e 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67  ../* Opcode: Agg
1d43f 53 74 65 70 20 2a 20 50 32 20 50 33 20 50 34 20  Step * P2 P3 P4 
1d440 50 35 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65  P5.**.** Execute
1d441 20 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69   the step functi
1d442 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67  on for an aggreg
1d443 61 74 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e  ate.  The.** fun
1d444 63 74 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67  ction has P5 arg
1d445 75 6d 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20  uments.   P4 is 
1d446 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1d447 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75   FuncDef.** stru
1d448 63 74 75 72 65 20 74 68 61 74 20 73 70 65 63 69  cture that speci
1d449 66 69 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f  fies the functio
1d44a 6e 2e 20 20 55 73 65 20 72 65 67 69 73 74 65 72  n.  Use register
1d44b 0a 2a 2a 20 50 33 20 61 73 20 74 68 65 20 61 63  .** P3 as the ac
1d44c 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  cumulator..**.**
1d44d 20 54 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74   The P5 argument
1d44e 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d  s are taken from
1d44f 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64   register P2 and
1d450 20 69 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f   its.** successo
1d451 72 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41  rs..*/.case OP_A
1d452 67 67 53 74 65 70 3a 20 7b 0a 20 20 69 6e 74 20  ggStep: {.  int 
1d453 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 69  n = pOp->p5;.  i
1d454 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65  nt i;.  Mem *pMe
1d455 6d 2c 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69  m, *pRec;.  sqli
1d456 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b  te3_context ctx;
1d457 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
1d458 20 2a 2a 61 70 56 61 6c 3b 0a 0a 20 20 61 73 73   **apVal;..  ass
1d459 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 70  ert( n>=0 );.  p
1d45a 52 65 63 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Rec = &p->aMem[p
1d45b 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 70 56 61 6c  Op->p2];.  apVal
1d45c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61   = p->apArg;.  a
1d45d 73 73 65 72 74 28 20 61 70 56 61 6c 20 7c 7c 20  ssert( apVal || 
1d45e 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d  n==0 );.  for(i=
1d45f 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 52 65  0; i<n; i++, pRe
1d460 63 2b 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b  c++){.    apVal[
1d461 69 5d 20 3d 20 70 52 65 63 3b 0a 20 20 20 20 73  i] = pRec;.    s
1d462 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 52 65  toreTypeInfo(pRe
1d463 63 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  c, encoding);.  
1d464 7d 0a 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20  }.  ctx.pFunc = 
1d465 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20  pOp->p4.pFunc;. 
1d466 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1d467 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70  >0 && pOp->p3<=p
1d468 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 63 74 78 2e  ->nMem );.  ctx.
1d469 70 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 70  pMem = pMem = &p
1d46a 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1d46b 0a 20 20 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20  .  pMem->n++;.  
1d46c 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ctx.s.flags = ME
1d46d 4d 5f 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e  M_Null;.  ctx.s.
1d46e 7a 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a  z = 0;.  ctx.s.z
1d46f 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 63 74  Malloc = 0;.  ct
1d470 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20  x.s.xDel = 0;.  
1d471 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20  ctx.s.db = db;. 
1d472 20 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30   ctx.isError = 0
1d473 3b 0a 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20  ;.  ctx.pColl = 
1d474 30 3b 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75  0;.  if( ctx.pFu
1d475 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  nc->flags & SQLI
1d476 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
1d477 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1d478 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20  pOp>p->aOp );.  
1d479 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31    assert( pOp[-1
1d47a 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c  ].p4type==P4_COL
1d47b 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65  LSEQ );.    asse
1d47c 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f  rt( pOp[-1].opco
1d47d 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29  de==OP_CollSeq )
1d47e 3b 0a 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20  ;.    ctx.pColl 
1d47f 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f  = pOp[-1].p4.pCo
1d480 6c 6c 3b 0a 20 20 7d 0a 20 20 28 63 74 78 2e 70  ll;.  }.  (ctx.p
1d481 46 75 6e 63 2d 3e 78 53 74 65 70 29 28 26 63 74  Func->xStep)(&ct
1d482 78 2c 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20  x, n, apVal);.  
1d483 69 66 28 20 63 74 78 2e 69 73 45 72 72 6f 72 20  if( ctx.isError 
1d484 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
1d485 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1d486 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73  Msg, db, "%s", s
1d487 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1d488 74 28 26 63 74 78 2e 73 29 29 3b 0a 20 20 20 20  t(&ctx.s));.    
1d489 72 63 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72  rc = ctx.isError
1d48a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
1d48b 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63  dbeMemRelease(&c
1d48c 74 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  tx.s);.  break;.
1d48d 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67  }../* Opcode: Ag
1d48e 67 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50  gFinal P1 P2 * P
1d48f 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74  4 *.**.** Execut
1d490 65 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20  e the finalizer 
1d491 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20  function for an 
1d492 61 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69  aggregate.  P1 i
1d493 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20  s.** the memory 
1d494 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73  location that is
1d495 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
1d496 20 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61   for the aggrega
1d497 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20  te..**.** P2 is 
1d498 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  the number of ar
1d499 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65  guments that the
1d49a 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74   step function t
1d49b 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69  akes and.** P4 i
1d49c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
1d49d 68 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74  he FuncDef for t
1d49e 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  his function.  T
1d49f 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e  he P2.** argumen
1d4a0 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79  t is not used by
1d4a1 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49   this opcode.  I
1d4a2 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20  t is only there 
1d4a3 74 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a  to disambiguate.
1d4a4 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ** functions tha
1d4a5 74 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69  t can take varyi
1d4a6 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72  ng numbers of ar
1d4a7 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a  guments.  The.**
1d4a8 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20   P4 argument is 
1d4a9 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20  only needed for 
1d4aa 74 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63  the degenerate c
1d4ab 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65  ase where.** the
1d4ac 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77   step function w
1d4ad 61 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c  as not previousl
1d4ae 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73  y called..*/.cas
1d4af 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b  e OP_AggFinal: {
1d4b0 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20  .  Mem *pMem;.  
1d4b1 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1d4b2 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d  0 && pOp->p1<=p-
1d4b3 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20  >nMem );.  pMem 
1d4b4 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1d4b5 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28  p1];.  assert( (
1d4b6 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e 28  pMem->flags & ~(
1d4b7 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67  MEM_Null|MEM_Agg
1d4b8 29 29 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20  ))==0 );.  rc = 
1d4b9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69  sqlite3VdbeMemFi
1d4ba 6e 61 6c 69 7a 65 28 70 4d 65 6d 2c 20 70 4f 70  nalize(pMem, pOp
1d4bb 2d 3e 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69  ->p4.pFunc);.  i
1d4bc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52  f( rc==SQLITE_ER
1d4bd 52 4f 52 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ROR ){.    sqlit
1d4be 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1d4bf 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1d4c0 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ", sqlite3_value
1d4c1 5f 74 65 78 74 28 70 4d 65 6d 29 29 3b 0a 20 20  _text(pMem));.  
1d4c2 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  }.  sqlite3VdbeC
1d4c3 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d  hangeEncoding(pM
1d4c4 65 6d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  em, encoding);. 
1d4c5 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1d4c6 53 49 5a 45 28 70 4d 65 6d 29 3b 0a 20 20 69 66  SIZE(pMem);.  if
1d4c7 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
1d4c8 54 6f 6f 42 69 67 28 70 4d 65 6d 29 20 29 7b 0a  TooBig(pMem) ){.
1d4c9 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
1d4ca 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d4cb 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  ...#if !defined(
1d4cc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55  SQLITE_OMIT_VACU
1d4cd 55 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  UM) && !defined(
1d4ce 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41  SQLITE_OMIT_ATTA
1d4cf 43 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  CH)./* Opcode: V
1d4d0 61 63 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a  acuum * * * * *.
1d4d1 2a 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65  **.** Vacuum the
1d4d2 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65   entire database
1d4d3 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77  .  This opcode w
1d4d4 69 6c 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20  ill cause other 
1d4d5 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69  virtual.** machi
1d4d6 6e 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65  nes to be create
1d4d7 64 20 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d  d and run.  It m
1d4d8 61 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64  ay not be called
1d4d9 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20   from within.** 
1d4da 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  a transaction..*
1d4db 2f 0a 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d  /.case OP_Vacuum
1d4dc 3a 20 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  : {.  if( sqlite
1d4dd 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1d4de 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1d4df 74 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63  to_misuse; .  rc
1d4e0 20 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63   = sqlite3RunVac
1d4e1 75 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  uum(&p->zErrMsg,
1d4e2 20 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69   db);.  if( sqli
1d4e3 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1d4e4 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1d4e5 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72  _to_misuse;.  br
1d4e6 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  eak;.}.#endif..#
1d4e7 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
1d4e8 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1d4e9 55 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  UM)./* Opcode: I
1d4ea 6e 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20  ncrVacuum P1 P2 
1d4eb 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66  * * *.**.** Perf
1d4ec 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65  orm a single ste
1d4ed 70 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65  p of the increme
1d4ee 6e 74 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63  ntal vacuum proc
1d4ef 65 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20  edure on.** the 
1d4f0 50 31 20 64 61 74 61 62 61 73 65 2e 20 49 66 20  P1 database. If 
1d4f1 74 68 65 20 76 61 63 75 75 6d 20 68 61 73 20 66  the vacuum has f
1d4f2 69 6e 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f  inished, jump to
1d4f3 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20   instruction.** 
1d4f4 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66  P2. Otherwise, f
1d4f5 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
1d4f6 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
1d4f7 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
1d4f8 49 6e 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20  IncrVacuum: {   
1d4f9 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1d4fa 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20    Btree *pBt;.. 
1d4fb 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1d4fc 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64  >=0 && pOp->p1<d
1d4fd 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65  b->nDb );.  asse
1d4fe 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
1d4ff 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29  k & (1<<pOp->p1)
1d500 29 21 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20  )!=0 );.  pBt = 
1d501 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d  db->aDb[pOp->p1]
1d502 2e 70 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c  .pBt;.  rc = sql
1d503 69 74 65 33 42 74 72 65 65 49 6e 63 72 56 61 63  ite3BtreeIncrVac
1d504 75 75 6d 28 70 42 74 29 3b 0a 20 20 69 66 28 20  uum(pBt);.  if( 
1d505 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
1d506 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1d507 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20  >p2 - 1;.    rc 
1d508 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
1d509 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1d50a 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45  if../* Opcode: E
1d50b 78 70 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a  xpire P1 * * * *
1d50c 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65  .**.** Cause pre
1d50d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65  compiled stateme
1d50e 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78  nts to become ex
1d50f 70 69 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65  pired. An expire
1d510 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66  d statement.** f
1d511 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72  ails with an err
1d512 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54  or code of SQLIT
1d513 45 5f 53 43 48 45 4d 41 20 69 66 20 69 74 20 69  E_SCHEMA if it i
1d514 73 20 65 76 65 72 20 65 78 65 63 75 74 65 64 20  s ever executed 
1d515 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33  .** (via sqlite3
1d516 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a  _step())..** .**
1d517 20 49 66 20 50 31 20 69 73 20 30 2c 20 74 68 65   If P1 is 0, the
1d518 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d  n all SQL statem
1d519 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69  ents become expi
1d51a 72 65 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f  red. If P1 is no
1d51b 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20  n-zero,.** then 
1d51c 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74  only the current
1d51d 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61  ly executing sta
1d51e 74 65 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74  tement is affect
1d51f 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ed. .*/.case OP_
1d520 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20  Expire: {.  if( 
1d521 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20  !pOp->p1 ){.    
1d522 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
1d523 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
1d524 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  db);.  }else{.  
1d525 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31    p->expired = 1
1d526 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d527 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1d528 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1d529 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61  HE./* Opcode: Ta
1d52a 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33  bleLock P1 P2 P3
1d52b 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61   P4 *.**.** Obta
1d52c 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70  in a lock on a p
1d52d 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e  articular table.
1d52e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1d52f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77  n is only used w
1d530 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65  hen.** the share
1d531 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20  d-cache feature 
1d532 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a  is enabled. .**.
1d533 2a 2a 20 49 66 20 50 31 20 69 73 20 20 74 68 65  ** If P1 is  the
1d534 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   index of the da
1d535 74 61 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65  tabase in sqlite
1d536 33 2e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64  3.aDb[] of the d
1d537 61 74 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68  atabase.** on wh
1d538 69 63 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  ich the lock is 
1d539 61 63 71 75 69 72 65 64 2e 20 20 41 20 72 65 61  acquired.  A rea
1d53a 64 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65  dlock is obtaine
1d53b 64 20 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a  d if P3==0 or.**
1d53c 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66   a write lock if
1d53d 20 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32   P3==1..**.** P2
1d53e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f   contains the ro
1d53f 6f 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74  ot-page of the t
1d540 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a  able to lock..**
1d541 0a 2a 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20  .** P4 contains 
1d542 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1d543 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
1d544 6c 65 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e  le being locked.
1d545 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a   This is only.**
1d546 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74   used to generat
1d547 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
1d548 67 65 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63  ge if the lock c
1d549 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65  annot be obtaine
1d54a 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61  d..*/.case OP_Ta
1d54b 62 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 69 6e 74  bleLock: {.  int
1d54c 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a   p1 = pOp->p1; .
1d54d 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b    u8 isWriteLock
1d54e 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70 33 3b 0a   = (u8)pOp->p3;.
1d54f 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20    assert( p1>=0 
1d550 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  && p1<db->nDb );
1d551 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62  .  assert( (p->b
1d552 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70  treeMask & (1<<p
1d553 31 29 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  1))!=0 );.  asse
1d554 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d  rt( isWriteLock=
1d555 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63  =0 || isWriteLoc
1d556 6b 3d 3d 31 20 29 3b 0a 20 20 72 63 20 3d 20 73  k==1 );.  rc = s
1d557 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54  qlite3BtreeLockT
1d558 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 70 31 5d  able(db->aDb[p1]
1d559 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 69  .pBt, pOp->p2, i
1d55a 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 69  sWriteLock);.  i
1d55b 66 28 20 28 72 63 26 30 78 46 46 29 3d 3d 53 51  f( (rc&0xFF)==SQ
1d55c 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20  LITE_LOCKED ){. 
1d55d 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1d55e 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20   = pOp->p4.z;.  
1d55f 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1d560 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1d561 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 74 61  db, "database ta
1d562 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25  ble is locked: %
1d563 73 22 2c 20 7a 29 3b 0a 20 20 7d 0a 20 20 62 72  s", z);.  }.  br
1d564 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1d565 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1d566 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69  RED_CACHE */..#i
1d567 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1d568 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
1d569 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 67 69 6e  * Opcode: VBegin
1d56a 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a   * * * P4 *.**.*
1d56b 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20 70 6f  * P4 may be a po
1d56c 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69  inter to an sqli
1d56d 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
1d56e 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 6c 6c 20  re. If so, call 
1d56f 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 6e 20 6d  the .** xBegin m
1d570 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 74  ethod for that t
1d571 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f  able..**.** Also
1d572 2c 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  , whether or not
1d573 20 50 34 20 69 73 20 73 65 74 2c 20 63 68 65 63   P4 is set, chec
1d574 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 20 6e  k that this is n
1d575 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20  ot being called 
1d576 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 6e 20 61  from.** within a
1d577 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 61 20 76   callback to a v
1d578 69 72 74 75 61 6c 20 74 61 62 6c 65 20 78 53 79  irtual table xSy
1d579 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 49 66 20  nc() method. If 
1d57a 69 74 20 69 73 2c 20 74 68 65 20 65 72 72 6f 72  it is, the error
1d57b 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 62 65  .** code will be
1d57c 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c   set to SQLITE_L
1d57d 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65 20 4f  OCKED..*/.case O
1d57e 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 20 20 73 71  P_VBegin: {.  sq
1d57f 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1d580 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61  b = pOp->p4.pVta
1d581 62 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  b;.  rc = sqlite
1d582 33 56 74 61 62 42 65 67 69 6e 28 64 62 2c 20 70  3VtabBegin(db, p
1d583 56 74 61 62 29 3b 0a 20 20 69 66 28 20 70 56 74  Vtab);.  if( pVt
1d584 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ab ){.    sqlite
1d585 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1d586 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e  ErrMsg);.    p->
1d587 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d  zErrMsg = pVtab-
1d588 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 56  >zErrMsg;.    pV
1d589 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30  tab->zErrMsg = 0
1d58a 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d58b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1d58c 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1d58d 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BLE */..#ifndef 
1d58e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1d58f 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f  UALTABLE./* Opco
1d590 64 65 3a 20 56 43 72 65 61 74 65 20 50 31 20 2a  de: VCreate P1 *
1d591 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34   * P4 *.**.** P4
1d592 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
1d593 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
1d594 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20  in database P1. 
1d595 43 61 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65  Call the xCreate
1d596 20 6d 65 74 68 6f 64 0a 2a 2a 20 66 6f 72 20 74   method.** for t
1d597 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61  hat table..*/.ca
1d598 73 65 20 4f 50 5f 56 43 72 65 61 74 65 3a 20 7b  se OP_VCreate: {
1d599 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
1d59a 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 64 62  tabCallCreate(db
1d59b 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
1d59c 70 34 2e 7a 2c 20 26 70 2d 3e 7a 45 72 72 4d 73  p4.z, &p->zErrMs
1d59d 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  g);.  break;.}.#
1d59e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1d59f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1d5a0 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
1d5a1 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1d5a2 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
1d5a3 3a 20 56 44 65 73 74 72 6f 79 20 50 31 20 2a 20  : VDestroy P1 * 
1d5a4 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1d5a5 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  is the name of a
1d5a6 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
1d5a7 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20  n database P1.  
1d5a8 43 61 6c 6c 20 74 68 65 20 78 44 65 73 74 72 6f  Call the xDestro
1d5a9 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74  y method.** of t
1d5aa 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61  hat table..*/.ca
1d5ab 73 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 3a 20  se OP_VDestroy: 
1d5ac 7b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74  {.  p->inVtabMet
1d5ad 68 6f 64 20 3d 20 32 3b 0a 20 20 72 63 20 3d 20  hod = 2;.  rc = 
1d5ae 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44  sqlite3VtabCallD
1d5af 65 73 74 72 6f 79 28 64 62 2c 20 70 4f 70 2d 3e  estroy(db, pOp->
1d5b0 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a  p1, pOp->p4.z);.
1d5b1 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
1d5b2 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a  d = 0;.  break;.
1d5b3 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1d5b4 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1d5b5 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ABLE */..#ifndef
1d5b6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1d5b7 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63  TUALTABLE./* Opc
1d5b8 6f 64 65 3a 20 56 4f 70 65 6e 20 50 31 20 2a 20  ode: VOpen P1 * 
1d5b9 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1d5ba 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1d5bb 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
1d5bc 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74  object, an sqlit
1d5bd 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72  e3_vtab structur
1d5be 65 2e 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 75  e..** P1 is a cu
1d5bf 72 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20 54 68  rsor number.  Th
1d5c0 69 73 20 6f 70 63 6f 64 65 20 6f 70 65 6e 73 20  is opcode opens 
1d5c1 61 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  a cursor to the 
1d5c2 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65  virtual.** table
1d5c3 20 61 6e 64 20 73 74 6f 72 65 73 20 74 68 61 74   and stores that
1d5c4 20 63 75 72 73 6f 72 20 69 6e 20 50 31 2e 0a 2a   cursor in P1..*
1d5c5 2f 0a 63 61 73 65 20 4f 50 5f 56 4f 70 65 6e 3a  /.case OP_VOpen:
1d5c6 20 7b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20   {.  VdbeCursor 
1d5c7 2a 70 43 75 72 20 3d 20 30 3b 0a 20 20 73 71 6c  *pCur = 0;.  sql
1d5c8 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
1d5c9 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20   *pVtabCursor = 
1d5ca 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  0;..  sqlite3_vt
1d5cb 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d  ab *pVtab = pOp-
1d5cc 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 73 71 6c  >p4.pVtab;.  sql
1d5cd 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1d5ce 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f  dule = (sqlite3_
1d5cf 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e  module *)pVtab->
1d5d0 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 61 73 73 65  pModule;..  asse
1d5d1 72 74 28 70 56 74 61 62 20 26 26 20 70 4d 6f 64  rt(pVtab && pMod
1d5d2 75 6c 65 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  ule);.  if( sqli
1d5d3 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
1d5d4 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1d5d5 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72  e_to_misuse;.  r
1d5d6 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70  c = pModule->xOp
1d5d7 65 6e 28 70 56 74 61 62 2c 20 26 70 56 74 61 62  en(pVtab, &pVtab
1d5d8 43 75 72 73 6f 72 29 3b 0a 20 20 73 71 6c 69 74  Cursor);.  sqlit
1d5d9 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1d5da 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a  zErrMsg);.  p->z
1d5db 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e  ErrMsg = pVtab->
1d5dc 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62  zErrMsg;.  pVtab
1d5dd 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1d5de 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1d5df 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20  tyOn(db) ) goto 
1d5e0 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1d5e1 75 73 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  use;.  if( SQLIT
1d5e2 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK==rc ){.    
1d5e3 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71  /* Initialize sq
1d5e4 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1d5e5 72 20 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a  r base class */.
1d5e6 20 20 20 20 70 56 74 61 62 43 75 72 73 6f 72 2d      pVtabCursor-
1d5e7 3e 70 56 74 61 62 20 3d 20 70 56 74 61 62 3b 0a  >pVtab = pVtab;.
1d5e8 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  .    /* Initiali
1d5e9 73 65 20 76 64 62 65 20 63 75 72 73 6f 72 20 6f  se vdbe cursor o
1d5ea 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 43 75  bject */.    pCu
1d5eb 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73  r = allocateCurs
1d5ec 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 30  or(p, pOp->p1, 0
1d5ed 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 69 66  , -1, 0);.    if
1d5ee 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 20 20  ( pCur ){.      
1d5ef 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f  pCur->pVtabCurso
1d5f0 72 20 3d 20 70 56 74 61 62 43 75 72 73 6f 72 3b  r = pVtabCursor;
1d5f1 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4d 6f  .      pCur->pMo
1d5f2 64 75 6c 65 20 3d 20 70 56 74 61 62 43 75 72 73  dule = pVtabCurs
1d5f3 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  or->pVtab->pModu
1d5f4 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  le;.    }else{. 
1d5f5 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
1d5f6 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
1d5f7 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65   pModule->xClose
1d5f8 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  (pVtabCursor);. 
1d5f9 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b     }.  }.  break
1d5fa 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1d5fb 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1d5fc 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
1d5fd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1d5fe 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
1d5ff 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50  pcode: VFilter P
1d600 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1d601 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f  ** P1 is a curso
1d602 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56  r opened using V
1d603 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20  Open.  P2 is an 
1d604 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20  address to jump 
1d605 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c  to if.** the fil
1d606 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74  tered result set
1d607 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a   is empty..**.**
1d608 20 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55   P4 is either NU
1d609 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74  LL or a string t
1d60a 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65  hat was generate
1d60b 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e  d by the xBestIn
1d60c 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66  dex.** method of
1d60d 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68   the module.  Th
1d60e 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e  e interpretation
1d60f 20 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e   of the P4 strin
1d610 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20  g is left.** to 
1d611 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  the module imple
1d612 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  mentation..**.**
1d613 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
1d614 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72  okes the xFilter
1d615 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76   method on the v
1d616 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65  irtual table spe
1d617 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e  cified.** by P1.
1d618 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75    The integer qu
1d619 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74  ery plan paramet
1d61a 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73  er to xFilter is
1d61b 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1d61c 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73  ter.** P3. Regis
1d61d 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20  ter P3+1 stores 
1d61e 74 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74  the argc paramet
1d61f 65 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20  er to be passed 
1d620 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65  to the.** xFilte
1d621 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74  r method. Regist
1d622 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61  ers P3+2..P3+1+a
1d623 72 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63  rgc are the argc
1d624 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70  .** additional p
1d625 61 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20  arameters which 
1d626 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a  are passed to.**
1d627 20 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76   xFilter as argv
1d628 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20  . Register P3+2 
1d629 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20  becomes argv[0] 
1d62a 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78  when passed to x
1d62b 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  Filter..**.** A 
1d62c 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20  jump is made to 
1d62d 50 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  P2 if the result
1d62e 20 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65   set after filte
1d62f 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d  ring would be em
1d630 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  pty..*/.case OP_
1d631 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20  VFilter: {   /* 
1d632 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  jump */.  int nA
1d633 72 67 3b 0a 20 20 69 6e 74 20 69 51 75 65 72 79  rg;.  int iQuery
1d634 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
1d635 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
1d636 65 3b 0a 20 20 4d 65 6d 20 2a 70 51 75 65 72 79  e;.  Mem *pQuery
1d637 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1d638 3e 70 33 5d 3b 0a 20 20 4d 65 6d 20 2a 70 41 72  >p3];.  Mem *pAr
1d639 67 63 20 3d 20 26 70 51 75 65 72 79 5b 31 5d 3b  gc = &pQuery[1];
1d63a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
1d63b 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
1d63c 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  sor;.  sqlite3_v
1d63d 74 61 62 20 2a 70 56 74 61 62 3b 0a 0a 20 20 56  tab *pVtab;..  V
1d63e 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20  dbeCursor *pCur 
1d63f 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1d640 70 31 5d 3b 0a 0a 20 20 52 45 47 49 53 54 45 52  p1];..  REGISTER
1d641 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20  _TRACE(pOp->p3, 
1d642 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 72  pQuery);.  asser
1d643 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75  t( pCur->pVtabCu
1d644 72 73 6f 72 20 29 3b 0a 20 20 70 56 74 61 62 43  rsor );.  pVtabC
1d645 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 56  ursor = pCur->pV
1d646 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 70 56 74  tabCursor;.  pVt
1d647 61 62 20 3d 20 70 56 74 61 62 43 75 72 73 6f 72  ab = pVtabCursor
1d648 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75  ->pVtab;.  pModu
1d649 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64  le = pVtab->pMod
1d64a 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 61 62 20  ule;..  /* Grab 
1d64b 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72  the index number
1d64c 20 61 6e 64 20 61 72 67 63 20 70 61 72 61 6d 65   and argc parame
1d64d 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74  ters */.  assert
1d64e 28 20 28 70 51 75 65 72 79 2d 3e 66 6c 61 67 73  ( (pQuery->flags
1d64f 26 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 26 26 20  &MEM_Int)!=0 && 
1d650 70 41 72 67 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45  pArgc->flags==ME
1d651 4d 5f 49 6e 74 20 29 3b 0a 20 20 6e 41 72 67 20  M_Int );.  nArg 
1d652 3d 20 28 69 6e 74 29 70 41 72 67 63 2d 3e 75 2e  = (int)pArgc->u.
1d653 69 3b 0a 20 20 69 51 75 65 72 79 20 3d 20 28 69  i;.  iQuery = (i
1d654 6e 74 29 70 51 75 65 72 79 2d 3e 75 2e 69 3b 0a  nt)pQuery->u.i;.
1d655 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65  .  /* Invoke the
1d656 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20   xFilter method 
1d657 2a 2f 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 72  */.  {.    int r
1d658 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  es = 0;.    int 
1d659 69 3b 0a 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41  i;.    Mem **apA
1d65a 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20  rg = p->apArg;. 
1d65b 20 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 3c     for(i = 0; i<
1d65c 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nArg; i++){.    
1d65d 20 20 61 70 41 72 67 5b 69 5d 20 3d 20 26 70 41    apArg[i] = &pA
1d65e 72 67 63 5b 69 2b 31 5d 3b 0a 20 20 20 20 20 20  rgc[i+1];.      
1d65f 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 61 70  storeTypeInfo(ap
1d660 41 72 67 5b 69 5d 2c 20 30 29 3b 0a 20 20 20 20  Arg[i], 0);.    
1d661 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  }..    if( sqlit
1d662 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1d663 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1d664 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20  _to_misuse;.    
1d665 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28  sqlite3VtabLock(
1d666 70 56 74 61 62 29 3b 0a 20 20 20 20 70 2d 3e 69  pVtab);.    p->i
1d667 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b  nVtabMethod = 1;
1d668 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  .    rc = pModul
1d669 65 2d 3e 78 46 69 6c 74 65 72 28 70 56 74 61 62  e->xFilter(pVtab
1d66a 43 75 72 73 6f 72 2c 20 69 51 75 65 72 79 2c 20  Cursor, iQuery, 
1d66b 70 4f 70 2d 3e 70 34 2e 7a 2c 20 6e 41 72 67 2c  pOp->p4.z, nArg,
1d66c 20 61 70 41 72 67 29 3b 0a 20 20 20 20 70 2d 3e   apArg);.    p->
1d66d 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30  inVtabMethod = 0
1d66e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
1d66f 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1d670 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72  sg);.    p->zErr
1d671 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72  Msg = pVtab->zEr
1d672 72 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62 2d  rMsg;.    pVtab-
1d673 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1d674 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
1d675 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a  ock(db, pVtab);.
1d676 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1d677 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
1d678 65 73 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 45  es = pModule->xE
1d679 6f 66 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b  of(pVtabCursor);
1d67a 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
1d67b 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1d67c 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1d67d 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a  due_to_misuse;..
1d67e 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20      if( res ){. 
1d67f 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1d680 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  2 - 1;.    }.  }
1d681 0a 20 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77  .  pCur->nullRow
1d682 20 3d 20 30 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a   = 0;..  break;.
1d683 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1d684 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1d685 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ABLE */..#ifndef
1d686 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1d687 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63  TUALTABLE./* Opc
1d688 6f 64 65 3a 20 56 43 6f 6c 75 6d 6e 20 50 31 20  ode: VColumn P1 
1d689 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1d68a 53 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20  Store the value 
1d68b 6f 66 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c  of the P2-th col
1d68c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f  umn of.** the ro
1d68d 77 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  w of the virtual
1d68e 2d 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20  -table that the 
1d68f 0a 2a 2a 20 50 31 20 63 75 72 73 6f 72 20 69 73  .** P1 cursor is
1d690 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 74   pointing to int
1d691 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  o register P3..*
1d692 2f 0a 63 61 73 65 20 4f 50 5f 56 43 6f 6c 75 6d  /.case OP_VColum
1d693 6e 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  n: {.  sqlite3_v
1d694 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f  tab *pVtab;.  co
1d695 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
1d696 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d  le *pModule;.  M
1d697 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 73 71 6c  em *pDest;.  sql
1d698 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 43 6f  ite3_context sCo
1d699 6e 74 65 78 74 3b 0a 0a 20 20 56 64 62 65 43 75  ntext;..  VdbeCu
1d69a 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e  rsor *pCur = p->
1d69b 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1d69c 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1d69d 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20  pVtabCursor );. 
1d69e 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1d69f 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70  >0 && pOp->p3<=p
1d6a0 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 44 65 73  ->nMem );.  pDes
1d6a1 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t = &p->aMem[pOp
1d6a2 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 70 43 75  ->p3];.  if( pCu
1d6a3 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20  r->nullRow ){.  
1d6a4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d6a5 53 65 74 4e 75 6c 6c 28 70 44 65 73 74 29 3b 0a  SetNull(pDest);.
1d6a6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
1d6a7 20 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70   pVtab = pCur->p
1d6a8 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61  VtabCursor->pVta
1d6a9 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70  b;.  pModule = p
1d6aa 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  Vtab->pModule;. 
1d6ab 20 61 73 73 65 72 74 28 20 70 4d 6f 64 75 6c 65   assert( pModule
1d6ac 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d  ->xColumn );.  m
1d6ad 65 6d 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c  emset(&sContext,
1d6ae 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74   0, sizeof(sCont
1d6af 65 78 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  ext));..  /* The
1d6b0 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79   output cell may
1d6b1 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20   already have a 
1d6b2 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64  buffer allocated
1d6b3 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20  . Move.  ** the 
1d6b4 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73  current contents
1d6b5 20 74 6f 20 73 43 6f 6e 74 65 78 74 2e 73 20 73   to sContext.s s
1d6b6 6f 20 69 6e 20 63 61 73 65 20 74 68 65 20 75 73  o in case the us
1d6b7 65 72 2d 66 75 6e 63 74 69 6f 6e 20 0a 20 20 2a  er-function .  *
1d6b8 2a 20 63 61 6e 20 75 73 65 20 74 68 65 20 61 6c  * can use the al
1d6b9 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20  ready allocated 
1d6ba 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f  buffer instead o
1d6bb 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 0a  f allocating a .
1d6bc 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 20    ** new one..  
1d6bd 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
1d6be 4d 65 6d 4d 6f 76 65 28 26 73 43 6f 6e 74 65 78  MemMove(&sContex
1d6bf 74 2e 73 2c 20 70 44 65 73 74 29 3b 0a 20 20 4d  t.s, pDest);.  M
1d6c0 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 26 73  emSetTypeFlag(&s
1d6c1 43 6f 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e  Context.s, MEM_N
1d6c2 75 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c  ull);..  if( sql
1d6c3 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1d6c4 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1d6c5 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
1d6c6 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  rc = pModule->xC
1d6c7 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61  olumn(pCur->pVta
1d6c8 62 43 75 72 73 6f 72 2c 20 26 73 43 6f 6e 74 65  bCursor, &sConte
1d6c9 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20  xt, pOp->p2);.  
1d6ca 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1d6cb 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1d6cc 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56   p->zErrMsg = pV
1d6cd 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  tab->zErrMsg;.  
1d6ce 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
1d6cf 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74   0;..  /* Copy t
1d6d0 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1d6d1 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65   function to the
1d6d2 20 50 33 20 72 65 67 69 73 74 65 72 2e 20 57 65   P3 register. We
1d6d3 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 72 65  .  ** do this re
1d6d4 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
1d6d5 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72  her or not an er
1d6d6 72 6f 72 20 6f 63 63 75 72 72 65 64 20 74 6f 20  ror occurred to 
1d6d7 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a 2a 20  ensure any.  ** 
1d6d8 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69  dynamic allocati
1d6d9 6f 6e 20 69 6e 20 73 43 6f 6e 74 65 78 74 2e 73  on in sContext.s
1d6da 20 28 61 20 4d 65 6d 20 73 74 72 75 63 74 29 20   (a Mem struct) 
1d6db 69 73 20 20 72 65 6c 65 61 73 65 64 2e 0a 20 20  is  released..  
1d6dc 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
1d6dd 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26  ChangeEncoding(&
1d6de 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e 63 6f  sContext.s, enco
1d6df 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53 54 45  ding);.  REGISTE
1d6e0 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c  R_TRACE(pOp->p3,
1d6e1 20 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 69 74   pDest);.  sqlit
1d6e2 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 44  e3VdbeMemMove(pD
1d6e3 65 73 74 2c 20 26 73 43 6f 6e 74 65 78 74 2e 73  est, &sContext.s
1d6e4 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  );.  UPDATE_MAX_
1d6e5 42 4c 4f 42 53 49 5a 45 28 70 44 65 73 74 29 3b  BLOBSIZE(pDest);
1d6e6 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
1d6e7 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20  afetyOn(db) ){. 
1d6e8 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1d6e9 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d  e_to_misuse;.  }
1d6ea 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
1d6eb 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 44 65 73  beMemTooBig(pDes
1d6ec 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  t) ){.    goto t
1d6ed 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62 72  oo_big;.  }.  br
1d6ee 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1d6ef 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1d6f0 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69  TUALTABLE */..#i
1d6f1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1d6f2 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
1d6f3 2a 20 4f 70 63 6f 64 65 3a 20 56 4e 65 78 74 20  * Opcode: VNext 
1d6f4 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1d6f5 2a 20 41 64 76 61 6e 63 65 20 76 69 72 74 75 61  * Advance virtua
1d6f6 6c 20 74 61 62 6c 65 20 50 31 20 74 6f 20 74 68  l table P1 to th
1d6f7 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 20 69 74  e next row in it
1d6f8 73 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64  s result set and
1d6f9 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74  .** jump to inst
1d6fa 72 75 63 74 69 6f 6e 20 50 32 2e 20 20 4f 72 2c  ruction P2.  Or,
1d6fb 20 69 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   if the virtual 
1d6fc 74 61 62 6c 65 20 68 61 73 20 72 65 61 63 68 65  table has reache
1d6fd 64 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20  d.** the end of 
1d6fe 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 2c 20  its result set, 
1d6ff 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67  then fall throug
1d700 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e  h to the next in
1d701 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  struction..*/.ca
1d702 73 65 20 4f 50 5f 56 4e 65 78 74 3a 20 7b 20 20  se OP_VNext: {  
1d703 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 73 71   /* jump */.  sq
1d704 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1d705 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  b;.  const sqlit
1d706 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1d707 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20  le;.  int res = 
1d708 30 3b 0a 0a 20 20 56 64 62 65 43 75 72 73 6f 72  0;..  VdbeCursor
1d709 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73   *pCur = p->apCs
1d70a 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  r[pOp->p1];.  as
1d70b 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56 74 61  sert( pCur->pVta
1d70c 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28  bCursor );.  if(
1d70d 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29   pCur->nullRow )
1d70e 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  {.    break;.  }
1d70f 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75 72 2d  .  pVtab = pCur-
1d710 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56  >pVtabCursor->pV
1d711 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d  tab;.  pModule =
1d712 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b   pVtab->pModule;
1d713 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f 64 75  .  assert( pModu
1d714 6c 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a 20 20  le->xNext );..  
1d715 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 4e  /* Invoke the xN
1d716 65 78 74 28 29 20 6d 65 74 68 6f 64 20 6f 66 20  ext() method of 
1d717 74 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68 65 72  the module. Ther
1d718 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20  e is no way for 
1d719 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79  the.  ** underly
1d71a 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ing implementati
1d71b 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20  on to return an 
1d71c 65 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63  error if one occ
1d71d 75 72 73 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20  urs during.  ** 
1d71e 78 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 61 64  xNext(). Instead
1d71f 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  , if an error oc
1d720 63 75 72 73 2c 20 74 72 75 65 20 69 73 20 72 65  curs, true is re
1d721 74 75 72 6e 65 64 20 28 69 6e 64 69 63 61 74 69  turned (indicati
1d722 6e 67 20 74 68 61 74 20 0a 20 20 2a 2a 20 64 61  ng that .  ** da
1d723 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29  ta is available)
1d724 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63   and the error c
1d725 6f 64 65 20 72 65 74 75 72 6e 65 64 20 77 68 65  ode returned whe
1d726 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a  n xColumn or.  *
1d727 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74  * some other met
1d728 68 6f 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f  hod is next invo
1d729 6b 65 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20  ked on the save 
1d72a 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
1d72b 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rsor..  */.  if(
1d72c 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1d72d 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1d72e 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1d72f 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c  ;.  sqlite3VtabL
1d730 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 70 2d  ock(pVtab);.  p-
1d731 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20  >inVtabMethod = 
1d732 31 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  1;.  rc = pModul
1d733 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 2d 3e 70  e->xNext(pCur->p
1d734 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 70  VtabCursor);.  p
1d735 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
1d736 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46   0;.  sqlite3DbF
1d737 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1d738 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73  sg);.  p->zErrMs
1d739 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d  g = pVtab->zErrM
1d73a 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72  sg;.  pVtab->zEr
1d73b 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69  rMsg = 0;.  sqli
1d73c 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62  te3VtabUnlock(db
1d73d 2c 20 70 56 74 61 62 29 3b 0a 20 20 69 66 28 20  , pVtab);.  if( 
1d73e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1d73f 0a 20 20 20 20 72 65 73 20 3d 20 70 4d 6f 64 75  .    res = pModu
1d740 6c 65 2d 3e 78 45 6f 66 28 70 43 75 72 2d 3e 70  le->xEof(pCur->p
1d741 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 7d  VtabCursor);.  }
1d742 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1d743 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1d744 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1d745 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20 21 72  isuse;..  if( !r
1d746 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  es ){.    /* If 
1d747 74 68 65 72 65 20 69 73 20 64 61 74 61 2c 20 6a  there is data, j
1d748 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20  ump to P2 */.   
1d749 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1d74a 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1d74b 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1d74c 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1d74d 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ABLE */..#ifndef
1d74e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1d74f 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63  TUALTABLE./* Opc
1d750 6f 64 65 3a 20 56 52 65 6e 61 6d 65 20 50 31 20  ode: VRename P1 
1d751 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50  * * P4 *.**.** P
1d752 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
1d753 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  o a virtual tabl
1d754 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c  e object, an sql
1d755 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
1d756 75 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ure..** This opc
1d757 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ode invokes the 
1d758 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 52  corresponding xR
1d759 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68  ename method. Th
1d75a 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 72 65  e value.** in re
1d75b 67 69 73 74 65 72 20 50 31 20 69 73 20 70 61 73  gister P1 is pas
1d75c 73 65 64 20 61 73 20 74 68 65 20 7a 4e 61 6d 65  sed as the zName
1d75d 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
1d75e 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e   xRename method.
1d75f 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52 65 6e  .*/.case OP_VRen
1d760 61 6d 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33  ame: {.  sqlite3
1d761 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70  _vtab *pVtab = p
1d762 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20  Op->p4.pVtab;.  
1d763 4d 65 6d 20 2a 70 4e 61 6d 65 20 3d 20 26 70 2d  Mem *pName = &p-
1d764 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
1d765 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62 2d    assert( pVtab-
1d766 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d  >pModule->xRenam
1d767 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  e );.  REGISTER_
1d768 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70  TRACE(pOp->p1, p
1d769 4e 61 6d 65 29 3b 0a 0a 20 20 53 74 72 69 6e 67  Name);..  String
1d76a 69 66 79 28 70 4e 61 6d 65 2c 20 65 6e 63 6f 64  ify(pName, encod
1d76b 69 6e 67 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c  ing);..  if( sql
1d76c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1d76d 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1d76e 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
1d76f 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28  sqlite3VtabLock(
1d770 70 56 74 61 62 29 3b 0a 20 20 72 63 20 3d 20 70  pVtab);.  rc = p
1d771 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  Vtab->pModule->x
1d772 52 65 6e 61 6d 65 28 70 56 74 61 62 2c 20 70 4e  Rename(pVtab, pN
1d773 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74  ame->z);.  sqlit
1d774 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1d775 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a  zErrMsg);.  p->z
1d776 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e  ErrMsg = pVtab->
1d777 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62  zErrMsg;.  pVtab
1d778 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1d779 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f   sqlite3VtabUnlo
1d77a 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20  ck(db, pVtab);. 
1d77b 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1d77c 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20  tyOn(db) ) goto 
1d77d 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1d77e 75 73 65 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d  use;..  break;.}
1d77f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
1d780 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1d781 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63  TUALTABLE./* Opc
1d782 6f 64 65 3a 20 56 55 70 64 61 74 65 20 50 31 20  ode: VUpdate P1 
1d783 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  P2 P3 P4 *.**.**
1d784 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
1d785 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61   to a virtual ta
1d786 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73  ble object, an s
1d787 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
1d788 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f  cture..** This o
1d789 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68  pcode invokes th
1d78a 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
1d78b 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 2e 20  xUpdate method. 
1d78c 50 32 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65  P2 values.** are
1d78d 20 63 6f 6e 74 69 67 75 6f 75 73 20 6d 65 6d 6f   contiguous memo
1d78e 72 79 20 63 65 6c 6c 73 20 73 74 61 72 74 69 6e  ry cells startin
1d78f 67 20 61 74 20 50 33 20 74 6f 20 70 61 73 73 20  g at P3 to pass 
1d790 74 6f 20 74 68 65 20 78 55 70 64 61 74 65 20 0a  to the xUpdate .
1d791 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 54  ** invocation. T
1d792 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1d793 73 74 65 72 20 28 50 33 2b 50 32 2d 31 29 20 63  ster (P3+P2-1) c
1d794 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68  orresponds to th
1d795 65 20 0a 2a 2a 20 70 32 74 68 20 65 6c 65 6d 65  e .** p2th eleme
1d796 6e 74 20 6f 66 20 74 68 65 20 61 72 67 76 20 61  nt of the argv a
1d797 72 72 61 79 20 70 61 73 73 65 64 20 74 6f 20 78  rray passed to x
1d798 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  Update..**.** Th
1d799 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64  e xUpdate method
1d79a 20 77 69 6c 6c 20 64 6f 20 61 20 44 45 4c 45 54   will do a DELET
1d79b 45 20 6f 72 20 61 6e 20 49 4e 53 45 52 54 20 6f  E or an INSERT o
1d79c 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 68 65 20 61  r both..** The a
1d79d 72 67 76 5b 30 5d 20 65 6c 65 6d 65 6e 74 20 28  rgv[0] element (
1d79e 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64  which correspond
1d79f 73 20 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  s to memory cell
1d7a0 20 50 33 29 0a 2a 2a 20 69 73 20 74 68 65 20 72   P3).** is the r
1d7a1 6f 77 69 64 20 6f 66 20 61 20 72 6f 77 20 74 6f  owid of a row to
1d7a2 20 64 65 6c 65 74 65 2e 20 20 49 66 20 61 72 67   delete.  If arg
1d7a3 76 5b 30 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65  v[0] is NULL the
1d7a4 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c 65 74 69 6f  n no .** deletio
1d7a5 6e 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 61  n occurs.  The a
1d7a6 72 67 76 5b 31 5d 20 65 6c 65 6d 65 6e 74 20 69  rgv[1] element i
1d7a7 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74  s the rowid of t
1d7a8 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 2e 20  he new .** row. 
1d7a9 20 54 68 69 73 20 63 61 6e 20 62 65 20 4e 55 4c   This can be NUL
1d7aa 4c 20 74 6f 20 68 61 76 65 20 74 68 65 20 76 69  L to have the vi
1d7ab 72 74 75 61 6c 20 74 61 62 6c 65 20 73 65 6c 65  rtual table sele
1d7ac 63 74 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72  ct the new .** r
1d7ad 6f 77 69 64 20 66 6f 72 20 69 74 73 65 6c 66 2e  owid for itself.
1d7ae 20 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74    The subsequent
1d7af 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65   elements in the
1d7b0 20 61 72 72 61 79 20 61 72 65 20 0a 2a 2a 20 74   array are .** t
1d7b1 68 65 20 76 61 6c 75 65 73 20 6f 66 20 63 6f 6c  he values of col
1d7b2 75 6d 6e 73 20 69 6e 20 74 68 65 20 6e 65 77 20  umns in the new 
1d7b3 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32  row..**.** If P2
1d7b4 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 69 6e 73 65  ==1 then no inse
1d7b5 72 74 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e  rt is performed.
1d7b6 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 68 65    argv[0] is the
1d7b7 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 61 20 72   rowid of.** a r
1d7b8 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a  ow to delete..**
1d7b9 0a 2a 2a 20 50 31 20 69 73 20 61 20 62 6f 6f 6c  .** P1 is a bool
1d7ba 65 61 6e 20 66 6c 61 67 2e 20 49 66 20 69 74 20  ean flag. If it 
1d7bb 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 61  is set to true a
1d7bc 6e 64 20 74 68 65 20 78 55 70 64 61 74 65 20 63  nd the xUpdate c
1d7bd 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 63 63 65 73  all.** is succes
1d7be 73 66 75 6c 2c 20 74 68 65 6e 20 74 68 65 20 76  sful, then the v
1d7bf 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
1d7c0 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
1d7c1 73 65 72 74 5f 72 6f 77 69 64 28 29 20 0a 2a 2a  sert_rowid() .**
1d7c2 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76   is set to the v
1d7c3 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 77 69  alue of the rowi
1d7c4 64 20 66 6f 72 20 74 68 65 20 72 6f 77 20 6a 75  d for the row ju
1d7c5 73 74 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a  st inserted..*/.
1d7c6 63 61 73 65 20 4f 50 5f 56 55 70 64 61 74 65 3a  case OP_VUpdate:
1d7c7 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61   {.  sqlite3_vta
1d7c8 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e  b *pVtab = pOp->
1d7c9 70 34 2e 70 56 74 61 62 3b 0a 20 20 73 71 6c 69  p4.pVtab;.  sqli
1d7ca 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1d7cb 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d  ule = (sqlite3_m
1d7cc 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70  odule *)pVtab->p
1d7cd 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 6e 41  Module;.  int nA
1d7ce 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20  rg = pOp->p2;.  
1d7cf 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1d7d0 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a  ype==P4_VTAB );.
1d7d1 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78    if( pModule->x
1d7d2 55 70 64 61 74 65 3d 3d 30 20 29 7b 0a 20 20 20  Update==0 ){.   
1d7d3 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1d7d4 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1d7d5 62 2c 20 22 72 65 61 64 2d 6f 6e 6c 79 20 74 61  b, "read-only ta
1d7d6 62 6c 65 22 29 3b 0a 20 20 20 20 72 63 20 3d 20  ble");.    rc = 
1d7d7 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1d7d8 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69  }else{.    int i
1d7d9 3b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74  ;.    sqlite_int
1d7da 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 4d 65  64 rowid;.    Me
1d7db 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d 3e 61  m **apArg = p->a
1d7dc 70 41 72 67 3b 0a 20 20 20 20 4d 65 6d 20 2a 70  pArg;.    Mem *p
1d7dd 58 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  X = &p->aMem[pOp
1d7de 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 6f 72 28 69  ->p3];.    for(i
1d7df 3d 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b 2b 29  =0; i<nArg; i++)
1d7e0 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54 79 70  {.      storeTyp
1d7e1 65 49 6e 66 6f 28 70 58 2c 20 30 29 3b 0a 20 20  eInfo(pX, 0);.  
1d7e2 20 20 20 20 61 70 41 72 67 5b 69 5d 20 3d 20 70      apArg[i] = p
1d7e3 58 3b 0a 20 20 20 20 20 20 70 58 2b 2b 3b 0a 20  X;.      pX++;. 
1d7e4 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
1d7e5 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1d7e6 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1d7e7 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
1d7e8 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63    sqlite3VtabLoc
1d7e9 6b 28 70 56 74 61 62 29 3b 0a 20 20 20 20 72 63  k(pVtab);.    rc
1d7ea 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64   = pModule->xUpd
1d7eb 61 74 65 28 70 56 74 61 62 2c 20 6e 41 72 67 2c  ate(pVtab, nArg,
1d7ec 20 61 70 41 72 67 2c 20 26 72 6f 77 69 64 29 3b   apArg, &rowid);
1d7ed 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1d7ee 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1d7ef 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d  g);.    p->zErrM
1d7f0 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72  sg = pVtab->zErr
1d7f1 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e  Msg;.    pVtab->
1d7f2 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
1d7f3 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f   sqlite3VtabUnlo
1d7f4 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20  ck(db, pVtab);. 
1d7f5 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1d7f6 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1d7f7 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1d7f8 69 73 75 73 65 3b 0a 20 20 20 20 69 66 28 20 70  isuse;.    if( p
1d7f9 4f 70 2d 3e 70 31 20 26 26 20 72 63 3d 3d 53 51  Op->p1 && rc==SQ
1d7fa 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1d7fb 20 61 73 73 65 72 74 28 20 6e 41 72 67 3e 31 20   assert( nArg>1 
1d7fc 26 26 20 61 70 41 72 67 5b 30 5d 20 26 26 20 28  && apArg[0] && (
1d7fd 61 70 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26  apArg[0]->flags&
1d7fe 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20  MEM_Null) );.   
1d7ff 20 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64     db->lastRowid
1d800 20 3d 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a   = rowid;.    }.
1d801 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b      p->nChange++
1d802 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d803 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1d804 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1d805 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BLE */..#ifndef 
1d806 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
1d807 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 20 4f 70  ER_PRAGMAS./* Op
1d808 63 6f 64 65 3a 20 50 61 67 65 63 6f 75 6e 74 20  code: Pagecount 
1d809 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1d80a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72  * Write the curr
1d80b 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  ent number of pa
1d80c 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 20  ges in database 
1d80d 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c  P1 to memory cel
1d80e 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  l P2..*/.case OP
1d80f 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b 20 20 20  _Pagecount: {   
1d810 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1d811 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20  -prerelease */. 
1d812 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70   int p1 = pOp->p
1d813 31 3b 20 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b  1; .  int nPage;
1d814 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
1d815 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
1d816 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 70 31 5d  ager(db->aDb[p1]
1d817 2e 70 42 74 29 3b 0a 0a 20 20 72 63 20 3d 20 73  .pBt);..  rc = s
1d818 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
1d819 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50  ount(pPager, &nP
1d81a 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  age);.  if( rc==
1d81b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1d81c 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1d81d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4f 75 74  EM_Int;.    pOut
1d81e 2d 3e 75 2e 69 20 3d 20 6e 50 61 67 65 3b 0a 20  ->u.i = nPage;. 
1d81f 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1d820 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
1d821 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a  LITE_OMIT_TRACE.
1d822 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 63 65  /* Opcode: Trace
1d823 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a   * * * P4 *.**.*
1d824 2a 20 49 66 20 74 72 61 63 69 6e 67 20 69 73 20  * If tracing is 
1d825 65 6e 61 62 6c 65 64 20 28 62 79 20 74 68 65 20  enabled (by the 
1d826 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 29  sqlite3_trace())
1d827 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 6e   interface, then
1d828 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38 20 73 74  .** the UTF-8 st
1d829 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65 64 20 69  ring contained i
1d82a 6e 20 50 34 20 69 73 20 65 6d 69 74 74 65 64 20  n P4 is emitted 
1d82b 6f 6e 20 74 68 65 20 74 72 61 63 65 20 63 61 6c  on the trace cal
1d82c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f  lback..*/.case O
1d82d 50 5f 54 72 61 63 65 3a 20 7b 0a 20 20 63 68 61  P_Trace: {.  cha
1d82e 72 20 2a 7a 54 72 61 63 65 20 3d 20 28 70 4f 70  r *zTrace = (pOp
1d82f 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70 2d 3e 70 34  ->p4.z ? pOp->p4
1d830 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20  .z : p->zSql);. 
1d831 20 69 66 28 20 7a 54 72 61 63 65 20 29 7b 0a 20   if( zTrace ){. 
1d832 20 20 20 69 66 28 20 64 62 2d 3e 78 54 72 61 63     if( db->xTrac
1d833 65 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 78  e ){.      db->x
1d834 54 72 61 63 65 28 64 62 2d 3e 70 54 72 61 63 65  Trace(db->pTrace
1d835 41 72 67 2c 20 7a 54 72 61 63 65 29 3b 0a 20 20  Arg, zTrace);.  
1d836 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
1d837 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20  E_DEBUG.    if( 
1d838 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
1d839 49 54 45 5f 53 71 6c 54 72 61 63 65 29 21 3d 30  ITE_SqlTrace)!=0
1d83a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1d83b 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 53 51  3DebugPrintf("SQ
1d83c 4c 2d 74 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20  L-trace: %s\n", 
1d83d 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23  zTrace);.    }.#
1d83e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1d83f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 20 62  DEBUG */.  }.  b
1d840 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  reak;.}.#endif..
1d841 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70  ./* Opcode: Noop
1d842 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a   * * * * *.**.**
1d843 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68   Do nothing.  Th
1d844 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
1d845 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c 20 61  s often useful a
1d846 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 73 74  s a jump.** dest
1d847 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a  ination..*/./*.*
1d848 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78 70 6c  * The magic Expl
1d849 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65 20 6f  ain opcode are o
1d84a 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77 68 65  nly inserted whe
1d84b 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28 77 68  n explain==2 (wh
1d84c 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73 61 79  ich.** is to say
1d84d 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c 41 49   when the EXPLAI
1d84e 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73 79 6e  N QUERY PLAN syn
1d84f 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a 2a 2a  tax is used.).**
1d850 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 63   This opcode rec
1d851 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ords information
1d852 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69 6d 69   from the optimi
1d853 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68 65 0a  zer.  It is the.
1d854 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 61  ** the same as a
1d855 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 6f 70   no-op.  This op
1d856 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70 65 61  codesnever appea
1d857 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56 4d 20  rs in a real VM 
1d858 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61  program..*/.defa
1d859 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ult: {          
1d85a 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c  /* This is reall
1d85b 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50  y OP_Noop and OP
1d85c 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 62 72  _Explain */.  br
1d85d 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  eak;.}../*******
1d85e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d85f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d861 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d862 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  ******.** The ca
1d863 73 65 73 20 6f 66 20 74 68 65 20 73 77 69 74 63  ses of the switc
1d864 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76  h statement abov
1d865 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68 6f 75  e this line shou
1d866 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65 6e 74  ld all be indent
1d867 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61 63 65  ed.** by 6 space
1d868 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65 66 74  s.  But the left
1d869 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73 20 68  -most 6 spaces h
1d86a 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64  ave been removed
1d86b 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68 65 0a   to improve the.
1d86c 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79 2e 20  ** readability. 
1d86d 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69 6e 74   From this point
1d86e 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20 6e 6f   on down, the no
1d86f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e  rmal indentation
1d870 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20 72 65   rules are.** re
1d871 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a  stored..********
1d872 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d873 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d874 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d875 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d876 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a 23 69  *****/.    }..#i
1d877 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
1d878 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 75 36  E.    {.      u6
1d879 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71 6c 69  4 elapsed = sqli
1d87a 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20 73 74  te3Hwtime() - st
1d87b 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e  art;.      pOp->
1d87c 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73 65  cycles += elapse
1d87d 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 6e  d;.      pOp->cn
1d87e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 20 20  t++;.#if 0.     
1d87f 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75     fprintf(stdou
1d880 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 65 6c  t, "%10llu ", el
1d881 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20  apsed);.        
1d882 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74  sqlite3VdbePrint
1d883 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69 67 50  Op(stdout, origP
1d884 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72 69 67 50  c, &p->aOp[origP
1d885 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  c]);.#endif.    
1d886 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
1d887 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
1d888 6f 64 65 20 61 64 64 73 20 6e 6f 74 68 69 6e 67  ode adds nothing
1d889 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66   to the actual f
1d88a 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20 20 20  unctionality.   
1d88b 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f 67 72   ** of the progr
1d88c 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20  am.  It is only 
1d88d 68 65 72 65 20 66 6f 72 20 74 65 73 74 69 6e 67  here for testing
1d88e 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 2e 0a   and debugging..
1d88f 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74      ** On the ot
1d890 68 65 72 20 68 61 6e 64 2c 20 69 74 20 64 6f 65  her hand, it doe
1d891 73 20 62 75 72 6e 20 43 50 55 20 63 79 63 6c 65  s burn CPU cycle
1d892 73 20 65 76 65 72 79 20 74 69 6d 65 20 74 68 72  s every time thr
1d893 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 68 65 20  ough.    ** the 
1d894 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70 2e 20  evaluator loop. 
1d895 20 53 6f 20 77 65 20 63 61 6e 20 6c 65 61 76 65   So we can leave
1d896 20 69 74 20 6f 75 74 20 77 68 65 6e 20 4e 44 45   it out when NDE
1d897 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a  BUG is defined..
1d898 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e      */.#ifndef N
1d899 44 45 42 55 47 0a 20 20 20 20 61 73 73 65 72 74  DEBUG.    assert
1d89a 28 20 70 63 3e 3d 2d 31 20 26 26 20 70 63 3c 70  ( pc>=-1 && pc<p
1d89b 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64 65 66  ->nOp );..#ifdef
1d89c 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
1d89d 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29    if( p->trace )
1d89e 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  {.      if( rc!=
1d89f 30 20 29 20 66 70 72 69 6e 74 66 28 70 2d 3e 74  0 ) fprintf(p->t
1d8a0 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22 2c 72  race,"rc=%d\n",r
1d8a1 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70  c);.      if( op
1d8a2 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
1d8a3 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45  _OUT2_PRERELEASE
1d8a4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69   ){.        regi
1d8a5 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61  sterTrace(p->tra
1d8a6 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f 75  ce, pOp->p2, pOu
1d8a7 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
1d8a8 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74 79    if( opProperty
1d8a9 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 20 29 7b   & OPFLG_OUT3 ){
1d8aa 0a 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65  .        registe
1d8ab 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c  rTrace(p->trace,
1d8ac 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b   pOp->p3, pOut);
1d8ad 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
1d8ae 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45  endif  /* SQLITE
1d8af 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 69 66  _DEBUG */.#endif
1d8b0 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 20    /* NDEBUG */. 
1d8b1 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f   }  /* The end o
1d8b2 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 6c 6f  f the for(;;) lo
1d8b3 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 68 72  op the loops thr
1d8b4 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a 2f 0a  ough opcodes */.
1d8b5 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63  .  /* If we reac
1d8b6 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74  h this point, it
1d8b7 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65 63   means that exec
1d8b8 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 65  ution is finishe
1d8b9 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e 20 65  d with.  ** an e
1d8ba 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b 69 6e  rror of some kin
1d8bb 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 72 72  d..  */.vdbe_err
1d8bc 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 65 72  or_halt:.  asser
1d8bd 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e 72 63  t( rc );.  p->rc
1d8be 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33   = rc;.  sqlite3
1d8bf 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 69  VdbeHalt(p);.  i
1d8c0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  f( rc==SQLITE_IO
1d8c1 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e  ERR_NOMEM ) db->
1d8c2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
1d8c3 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  ;.  rc = SQLITE_
1d8c4 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 68 69  ERROR;..  /* Thi
1d8c5 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61  s is the only wa
1d8c6 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20 70 72  y out of this pr
1d8c7 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68 61 76  ocedure.  We hav
1d8c8 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 61 73  e to.  ** releas
1d8c9 65 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e  e the mutexes on
1d8ca 20 62 74 72 65 65 73 20 74 68 61 74 20 77 65 72   btrees that wer
1d8cb 65 20 61 63 71 75 69 72 65 64 20 61 74 20 74 68  e acquired at th
1d8cc 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f 0a 76  e.  ** top. */.v
1d8cd 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 73 71  dbe_return:.  sq
1d8ce 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
1d8cf 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d  rrayLeave(&p->aM
1d8d0 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
1d8d1 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74  rc;..  /* Jump t
1d8d2 6f 20 68 65 72 65 20 69 66 20 61 20 73 74 72 69  o here if a stri
1d8d3 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72 67 65  ng or blob large
1d8d4 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41  r than SQLITE_MA
1d8d5 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 69 73  X_LENGTH.  ** is
1d8d6 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 20 20   encountered..  
1d8d7 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 73 71  */.too_big:.  sq
1d8d8 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1d8d9 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1d8da 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20  "string or blob 
1d8db 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72 63 20  too big");.  rc 
1d8dc 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b  = SQLITE_TOOBIG;
1d8dd 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72  .  goto vdbe_err
1d8de 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a  or_halt;..  /* J
1d8df 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 61  ump to here if a
1d8e0 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e   malloc() fails.
1d8e1 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20  .  */.no_mem:.  
1d8e2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1d8e3 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 53   = 1;.  sqlite3S
1d8e4 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1d8e5 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74 20 6f  rMsg, db, "out o
1d8e6 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 72 63  f memory");.  rc
1d8e7 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1d8e8 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72  .  goto vdbe_err
1d8e9 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a  or_halt;..  /* J
1d8ea 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72 20  ump to here for 
1d8eb 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  an SQLITE_MISUSE
1d8ec 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62 6f   error..  */.abo
1d8ed 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1d8ee 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  :.  rc = SQLITE_
1d8ef 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61 6c  MISUSE;.  /* Fal
1d8f0 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62 6f 72  l thru into abor
1d8f1 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20 2a  t_due_to_error *
1d8f2 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  /..  /* Jump to 
1d8f3 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74 68  here for any oth
1d8f4 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61 6c  er kind of fatal
1d8f5 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72 63   error.  The "rc
1d8f6 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20  " variable.  ** 
1d8f7 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20  should hold the 
1d8f8 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20 20  error number..  
1d8f9 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  */.abort_due_to_
1d8fa 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28  error:.  assert(
1d8fb 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29   p->zErrMsg==0 )
1d8fc 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
1d8fd 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 3d 20  ocFailed ) rc = 
1d8fe 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1d8ff 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49  if( rc!=SQLITE_I
1d900 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  OERR_NOMEM ){.  
1d901 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1d902 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1d903 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
1d904 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
1d905 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72  }.  goto vdbe_er
1d906 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20  ror_halt;..  /* 
1d907 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20  Jump to here if 
1d908 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  the sqlite3_inte
1d909 72 72 75 70 74 28 29 20 41 50 49 20 73 65 74 73  rrupt() API sets
1d90a 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 0a 20   the interrupt. 
1d90b 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61   ** flag..  */.a
1d90c 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65  bort_due_to_inte
1d90d 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72 74 28  rrupt:.  assert(
1d90e 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
1d90f 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20 3d 20  upted );.  rc = 
1d910 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
1d911 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a  ;.  p->rc = rc;.
1d912 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1d913 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1d914 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
1d915 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
1d916 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f  goto vdbe_error_
1d917 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  halt;.}../******
1d918 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1d919 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vdbe.c *********
1d91a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d91b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d91c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1d91d 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1d91e 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a  ile vdbeblob.c *
1d91f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d921 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1d922 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 20  007 May 1.**.** 
1d923 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
1d924 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
1d925 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
1d926 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
1d927 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
1d928 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
1d929 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
1d92a 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
1d92b 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
1d92c 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
1d92d 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
1d92e 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
1d92f 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
1d930 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
1d931 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
1d932 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
1d933 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
1d934 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d935 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d936 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d937 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d938 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
1d939 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
1d93a 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69  s code used to i
1d93b 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d 65  mplement increme
1d93c 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a 2a  ntal BLOB I/O..*
1d93d 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 62 6c  *.** $Id: vdbebl
1d93e 6f 62 2e 63 2c 76 20 31 2e 33 31 20 32 30 30 39  ob.c,v 1.31 2009
1d93f 2f 30 33 2f 32 34 20 31 35 3a 30 38 3a 31 30 20  /03/24 15:08:10 
1d940 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23  drh Exp $.*/...#
1d941 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1d942 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f 2a 0a  IT_INCRBLOB../*.
1d943 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65 33  ** Valid sqlite3
1d944 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20 70  _blob* handles p
1d945 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f 62  oint to Incrblob
1d946 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a   structures..*/.
1d947 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49  typedef struct I
1d948 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c 6f 62  ncrblob Incrblob
1d949 3b 0a 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f  ;.struct Incrblo
1d94a 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b  b {.  int flags;
1d94b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d94c 20 43 6f 70 79 20 6f 66 20 22 66 6c 61 67 73 22   Copy of "flags"
1d94d 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74   passed to sqlit
1d94e 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 20 2a  e3_blob_open() *
1d94f 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20  /.  int nByte;  
1d950 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1d951 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c 6f 62  ize of open blob
1d952 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  , in bytes */.  
1d953 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20  int iOffset;    
1d954 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20          /* Byte 
1d955 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f 62 20 69  offset of blob i
1d956 6e 20 63 75 72 73 6f 72 20 64 61 74 61 20 2a 2f  n cursor data */
1d957 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 73  .  BtCursor *pCs
1d958 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  r;         /* Cu
1d959 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74  rsor pointing at
1d95a 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20 20 73   blob row */.  s
1d95b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1d95c 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61 74 65 6d  mt;    /* Statem
1d95d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75 72 73  ent holding curs
1d95e 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c  or open */.  sql
1d95f 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
1d960 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f       /* The asso
1d961 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20  ciated database 
1d962 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  */.};../*.** Ope
1d963 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e  n a blob handle.
1d964 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1d965 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
1d966 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a  open(.  sqlite3*
1d967 20 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20   db,            
1d968 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1d969 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
1d96a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c  const char *zDb,
1d96b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61          /* The a
1d96c 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
1d96d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1d96e 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  blob */.  const 
1d96f 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20 20 20  char *zTable,   
1d970 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 63    /* The table c
1d971 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c  ontaining the bl
1d972 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ob */.  const ch
1d973 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20 20 20  ar *zColumn,    
1d974 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 63 6f  /* The column co
1d975 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f  ntaining the blo
1d976 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  b */.  sqlite_in
1d977 74 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 2f  t64 iRow,      /
1d978 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e 74 61 69  * The row contai
1d979 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 20 2a 2f  ning the glob */
1d97a 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
1d97b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
1d97c 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 69 74 65  ue -> read/write
1d97d 20 61 63 63 65 73 73 2c 20 66 61 6c 73 65 20 2d   access, false -
1d97e 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20  > read-only */. 
1d97f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a   sqlite3_blob **
1d980 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61 6e 64  ppBlob   /* Hand
1d981 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67  le for accessing
1d982 20 74 68 65 20 62 6c 6f 62 20 72 65 74 75 72 6e   the blob return
1d983 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ed here */.){.  
1d984 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20 30  int nAttempt = 0
1d985 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20  ;.  int iCol;   
1d986 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1d987 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e 20  ndex of zColumn 
1d988 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a 2f  in row-record */
1d989 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 44 42 45  ..  /* This VDBE
1d98a 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73 20 61   program seeks a
1d98b 20 62 74 72 65 65 20 63 75 72 73 6f 72 20 74 6f   btree cursor to
1d98c 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 64 20   the identified 
1d98d 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65 2f 72  .  ** db/table/r
1d98e 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 20 72 65  ow entry. The re
1d98f 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 61  ason for using a
1d990 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 6e   vdbe program in
1d991 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 77 72  stead.  ** of wr
1d992 69 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75 73  iting code to us
1d993 65 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79  e the b-tree lay
1d994 65 72 20 64 69 72 65 63 74 6c 79 20 69 73 20 74  er directly is t
1d995 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 76 64 62  hat the.  ** vdb
1d996 65 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 74  e program will t
1d997 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66  ake advantage of
1d998 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 72 61   the various tra
1d999 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 6c  nsaction,.  ** l
1d99a 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 72 6f 72  ocking and error
1d99b 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72 61 73   handling infras
1d99c 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 69  tructure built i
1d99d 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 0a 20 20  nto the vdbe..  
1d99e 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20 73 65  **.  ** After se
1d99f 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72  eking the cursor
1d9a0 2c 20 74 68 65 20 76 64 62 65 20 65 78 65 63 75  , the vdbe execu
1d9a1 74 65 73 20 61 6e 20 4f 50 5f 52 65 73 75 6c 74  tes an OP_Result
1d9a2 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 65 20 65  Row..  ** Code e
1d9a3 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20 56  xternal to the V
1d9a4 64 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f 77  dbe then "borrow
1d9a5 73 22 20 74 68 65 20 62 2d 74 72 65 65 20 63 75  s" the b-tree cu
1d9a6 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75 73  rsor and.  ** us
1d9a7 65 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d 65  es it to impleme
1d9a8 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61 64  nt the blob_read
1d9a9 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28 29  (), blob_write()
1d9aa 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62 5f   and .  ** blob_
1d9ab 62 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f 6e  bytes() function
1d9ac 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  s..  **.  ** The
1d9ad 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c   sqlite3_blob_cl
1d9ae 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ose() function f
1d9af 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64 62  inalizes the vdb
1d9b0 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a 20  e program,.  ** 
1d9b1 77 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68 65  which closes the
1d9b2 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61   b-tree cursor a
1d9b3 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63 6f  nd (possibly) co
1d9b4 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a 20  mmits the .  ** 
1d9b5 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a  transaction..  *
1d9b6 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
1d9b7 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65 6e   VdbeOpList open
1d9b8 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b  Blob[] = {.    {
1d9b9 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20  OP_Transaction, 
1d9ba 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f 2a  0, 0, 0},     /*
1d9bb 20 30 3a 20 53 74 61 72 74 20 61 20 74 72 61 6e   0: Start a tran
1d9bc 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 7b  saction */.    {
1d9bd 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c  OP_VerifyCookie,
1d9be 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f 2a   0, 0, 0},    /*
1d9bf 20 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73 63   1: Check the sc
1d9c0 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 0a  hema cookie */..
1d9c1 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
1d9c2 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20  e following two 
1d9c3 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 73 20  instructions is 
1d9c4 72 65 70 6c 61 63 65 64 20 62 79 20 61 6e 0a 20  replaced by an. 
1d9c5 20 20 20 2a 2a 20 4f 50 5f 4e 6f 6f 70 20 62 65     ** OP_Noop be
1d9c6 66 6f 72 65 20 65 78 65 63 74 69 6f 6e 2e 0a 20  fore exection.. 
1d9c7 20 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70     */.    {OP_Op
1d9c8 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30 7d  enRead, 0, 0, 0}
1d9c9 2c 20 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 4f  ,        /* 2: O
1d9ca 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72  pen cursor 0 for
1d9cb 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20 20   reading */.    
1d9cc 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30  {OP_OpenWrite, 0
1d9cd 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f  , 0, 0},       /
1d9ce 2a 20 33 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72  * 3: Open cursor
1d9cf 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74   0 for read/writ
1d9d0 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56 61  e */..    {OP_Va
1d9d1 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 31 7d  riable, 1, 1, 1}
1d9d2 2c 20 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 50  ,        /* 4: P
1d9d3 75 73 68 20 74 68 65 20 72 6f 77 69 64 20 74 6f  ush the rowid to
1d9d4 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20   the stack */.  
1d9d5 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c    {OP_NotExists,
1d9d6 20 30 2c 20 38 2c 20 31 7d 2c 20 20 20 20 20 20   0, 8, 1},      
1d9d7 20 2f 2a 20 35 3a 20 53 65 65 6b 20 74 68 65 20   /* 5: Seek the 
1d9d8 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f  cursor */.    {O
1d9d9 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20  P_Column, 0, 0, 
1d9da 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  1},          /* 
1d9db 36 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52 65  6  */.    {OP_Re
1d9dc 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20 30  sultRow, 1, 0, 0
1d9dd 7d 2c 20 20 20 20 20 20 20 2f 2a 20 37 20 20 2a  },       /* 7  *
1d9de 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 2c  /.    {OP_Close,
1d9df 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20   0, 0, 0},      
1d9e0 20 20 20 20 20 2f 2a 20 38 20 20 2a 2f 0a 20 20       /* 8  */.  
1d9e1 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30    {OP_Halt, 0, 0
1d9e2 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20  , 0},           
1d9e3 20 2f 2a 20 39 20 2a 2f 0a 20 20 7d 3b 0a 0a 20   /* 9 */.  };.. 
1d9e4 20 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20   Vdbe *v = 0;.  
1d9e5 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1d9e6 4f 4b 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b  OK;.  char zErr[
1d9e7 31 32 38 5d 3b 0a 0a 20 20 7a 45 72 72 5b 30 5d  128];..  zErr[0]
1d9e8 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
1d9e9 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
1d9ea 6d 75 74 65 78 29 3b 0a 20 20 64 6f 20 7b 0a 20  mutex);.  do {. 
1d9eb 20 20 20 50 61 72 73 65 20 73 50 61 72 73 65 3b     Parse sParse;
1d9ec 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
1d9ed 3b 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73  ;..    memset(&s
1d9ee 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66  Parse, 0, sizeof
1d9ef 28 50 61 72 73 65 29 29 3b 0a 20 20 20 20 73 50  (Parse));.    sP
1d9f0 61 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 0a 20  arse.db = db;.. 
1d9f1 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1d9f2 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20  fetyOn(db) ){.  
1d9f3 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1d9f4 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
1d9f5 78 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  x);.      return
1d9f6 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
1d9f7 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
1d9f8 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
1d9f9 64 62 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20  db);.    pTab = 
1d9fa 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
1d9fb 6c 65 28 26 73 50 61 72 73 65 2c 20 30 2c 20 7a  le(&sParse, 0, z
1d9fc 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20 20 20  Table, zDb);.   
1d9fd 20 69 66 28 20 70 54 61 62 20 26 26 20 49 73 56   if( pTab && IsV
1d9fe 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
1d9ff 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a        pTab = 0;.
1da00 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1da01 6f 72 4d 73 67 28 26 73 50 61 72 73 65 2c 20 22  orMsg(&sParse, "
1da02 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 72 74  cannot open virt
1da03 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 2c 20  ual table: %s", 
1da04 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23  zTable);.    }.#
1da05 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1da06 49 54 5f 56 49 45 57 0a 20 20 20 20 69 66 28 20  IT_VIEW.    if( 
1da07 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53  pTab && pTab->pS
1da08 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 70  elect ){.      p
1da09 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 73  Tab = 0;.      s
1da0a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 26  qlite3ErrorMsg(&
1da0b 73 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  sParse, "cannot 
1da0c 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 2c 20  open view: %s", 
1da0d 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23  zTable);.    }.#
1da0e 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21 70  endif.    if( !p
1da0f 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Tab ){.      if(
1da10 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 20   sParse.zErrMsg 
1da11 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1da12 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
1da13 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20  of(zErr), zErr, 
1da14 22 25 73 22 2c 20 73 50 61 72 73 65 2e 7a 45 72  "%s", sParse.zEr
1da15 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rMsg);.      }. 
1da16 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1da17 65 65 28 64 62 2c 20 73 50 61 72 73 65 2e 7a 45  ee(db, sParse.zE
1da18 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 72 63  rrMsg);.      rc
1da19 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
1da1a 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c  .      (void)sql
1da1b 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1da1c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1da1d 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
1da1e 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c  );.      goto bl
1da1f 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20  ob_open_out;.   
1da20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 73   }..    /* Now s
1da21 65 61 72 63 68 20 70 54 61 62 20 66 6f 72 20 74  earch pTab for t
1da22 68 65 20 65 78 61 63 74 20 63 6f 6c 75 6d 6e 2e  he exact column.
1da23 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c   */.    for(iCol
1da24 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61 62 2d  =0; iCol < pTab-
1da25 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b  >nCol; iCol++) {
1da26 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1da27 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e  e3StrICmp(pTab->
1da28 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
1da29 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b  , zColumn)==0 ){
1da2a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1da2b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1da2c 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62    if( iCol==pTab
1da2d 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
1da2e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1da2f 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1da30 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f  Err, "no such co
1da31 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 7a  lumn: \"%s\"", z
1da32 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 72  Column);.      r
1da33 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1da34 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71  ;.      (void)sq
1da35 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1da36 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
1da37 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
1da38 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  b);.      goto b
1da39 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20  lob_open_out;.  
1da3a 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
1da3b 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69 6e  he value is bein
1da3c 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69  g opened for wri
1da3d 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61 74  ting, check that
1da3e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75   the.    ** colu
1da3f 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78 65  mn is not indexe
1da40 64 2e 20 49 74 20 69 73 20 61 67 61 69 6e 73 74  d. It is against
1da41 20 74 68 65 20 72 75 6c 65 73 20 74 6f 20 6f 70   the rules to op
1da42 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 69 6e 64  en an.    ** ind
1da43 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66 6f 72 20  exed column for 
1da44 77 72 69 74 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a  writing..    */.
1da45 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 29 7b      if( flags ){
1da46 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49  .      Index *pI
1da47 64 78 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 49  dx;.      for(pI
1da48 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
1da49 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
1da4a 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
1da4b 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
1da4c 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78   for(j=0; j<pIdx
1da4d 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b  ->nColumn; j++){
1da4e 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
1da4f 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
1da50 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ==iCol ){.      
1da51 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1da52 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
1da53 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20  rr), zErr,.     
1da54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1da55 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20          "cannot 
1da56 6f 70 65 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c  open indexed col
1da57 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 22  umn for writing"
1da58 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  );.            r
1da59 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1da5a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 28 76  ;.            (v
1da5b 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
1da5c 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
1da5d 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1da5e 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
1da5f 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
1da60 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a   blob_open_out;.
1da61 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1da62 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1da63 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c    }..    v = sql
1da64 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28 64  ite3VdbeCreate(d
1da65 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20 29 7b  b);.    if( v ){
1da66 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d  .      int iDb =
1da67 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1da68 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
1da69 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
1da6a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1da6b 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66 28 6f  List(v, sizeof(o
1da6c 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f 66 28  penBlob)/sizeof(
1da6d 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f 70 65  VdbeOpList), ope
1da6e 6e 42 6c 6f 62 29 3b 0a 0a 20 20 20 20 20 20 2f  nBlob);..      /
1da6f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20  * Configure the 
1da70 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 2a  OP_Transaction *
1da71 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
1da72 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 30  dbeChangeP1(v, 0
1da73 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
1da74 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1da75 32 28 76 2c 20 30 2c 20 28 66 6c 61 67 73 20 3f  2(v, 0, (flags ?
1da76 20 31 20 3a 20 30 29 29 3b 0a 0a 20 20 20 20 20   1 : 0));..     
1da77 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68   /* Configure th
1da78 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69  e OP_VerifyCooki
1da79 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  e */.      sqlit
1da7a 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
1da7b 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  , 1, iDb);.     
1da7c 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1da7d 67 65 50 32 28 76 2c 20 31 2c 20 70 54 61 62 2d  geP2(v, 1, pTab-
1da7e 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  >pSchema->schema
1da7f 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20  _cookie);..     
1da80 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20   /* Make sure a 
1da81 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e  mutex is held on
1da82 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
1da83 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 20   accessed */.   
1da84 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73     sqlite3VdbeUs
1da85 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b  esBtree(v, iDb);
1da86 20 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f   ..      /* Remo
1da87 76 65 20 65 69 74 68 65 72 20 74 68 65 20 4f 50  ve either the OP
1da88 5f 4f 70 65 6e 57 72 69 74 65 20 6f 72 20 4f 70  _OpenWrite or Op
1da89 65 6e 52 65 61 64 2e 20 53 65 74 20 74 68 65 20  enRead. Set the 
1da8a 50 32 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 72  P2 .      ** par
1da8b 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 6f 74  ameter of the ot
1da8c 68 65 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e 75  her to pTab->tnu
1da8d 6d 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  m. .      */.   
1da8e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1da8f 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 28 66  angeToNoop(v, (f
1da90 6c 61 67 73 20 3f 20 32 20 3a 20 33 29 2c 20 31  lags ? 2 : 3), 1
1da91 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1da92 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
1da93 28 66 6c 61 67 73 20 3f 20 33 20 3a 20 32 29 2c  (flags ? 3 : 2),
1da94 20 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20   pTab->tnum);.  
1da95 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1da96 68 61 6e 67 65 50 33 28 76 2c 20 28 66 6c 61 67  hangeP3(v, (flag
1da97 73 20 3f 20 33 20 3a 20 32 29 2c 20 69 44 62 29  s ? 3 : 2), iDb)
1da98 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66  ;..      /* Conf
1da99 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72  igure the number
1da9a 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f 6e   of columns. Con
1da9b 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73 6f  figure the curso
1da9c 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  r to.      ** th
1da9d 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61 62  ink that the tab
1da9e 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 20  le has one more 
1da9f 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 72  column than it r
1daa0 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 64  eally.      ** d
1daa1 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 6d  oes. An OP_Colum
1daa2 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  n to retrieve th
1daa3 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f 6c  is imaginary col
1daa4 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a  umn will.      *
1daa5 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20  * always return 
1daa6 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 69  an SQL NULL. Thi
1daa7 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63 61  s is useful beca
1daa8 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 20  use it means.   
1daa9 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e 76     ** we can inv
1daaa 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f  oke OP_Column to
1daab 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 62   fill in the vdb
1daac 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20 0a  e cursors type .
1daad 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 66        ** and off
1daae 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f 75  set cache withou
1daaf 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49 4f  t causing any IO
1dab0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1dab1 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1dab2 67 65 50 34 28 76 2c 20 66 6c 61 67 73 20 3f 20  geP4(v, flags ? 
1dab3 33 20 3a 20 32 2c 20 53 51 4c 49 54 45 5f 49 4e  3 : 2, SQLITE_IN
1dab4 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e  T_TO_PTR(pTab->n
1dab5 43 6f 6c 2b 31 29 2c 20 50 34 5f 49 4e 54 33 32  Col+1), P4_INT32
1dab6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1dab7 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
1dab8 36 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a  6, pTab->nCol);.
1dab9 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
1daba 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1dabb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1dabc 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31  beMakeReady(v, 1
1dabd 2c 20 31 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  , 1, 1, 0);.    
1dabe 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20    }.    }.   .  
1dabf 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1dac0 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
1dac1 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65  rc = sqlite3Safe
1dac2 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69  tyOff(db);.    i
1dac3 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1dac4 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1dac5 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 67 6f  iled ){.      go
1dac6 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74  to blob_open_out
1dac7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
1dac8 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
1dac9 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
1daca 76 2c 20 31 2c 20 69 52 6f 77 29 3b 0a 20 20 20  v, 1, iRow);.   
1dacb 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
1dacc 65 70 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ep((sqlite3_stmt
1dacd 20 2a 29 76 29 3b 0a 20 20 20 20 69 66 28 20 72   *)v);.    if( r
1dace 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c!=SQLITE_ROW ){
1dacf 0a 20 20 20 20 20 20 6e 41 74 74 65 6d 70 74 2b  .      nAttempt+
1dad0 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  +;.      rc = sq
1dad1 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28  lite3_finalize((
1dad2 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76  sqlite3_stmt *)v
1dad3 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1dad4 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
1dad5 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 73 71  (zErr), zErr, sq
1dad6 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29  lite3_errmsg(db)
1dad7 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b 0a  );.      v = 0;.
1dad8 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28      }.  } while(
1dad9 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20 72   nAttempt<5 && r
1dada 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  c==SQLITE_SCHEMA
1dadb 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53   );..  if( rc==S
1dadc 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
1dadd 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 6f   /* The row-reco
1dade 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e  rd has been open
1dadf 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e  ed successfully.
1dae0 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a   Check that the.
1dae1 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e      ** column in
1dae2 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 69   question contai
1dae3 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c 6f  ns text or a blo
1dae4 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69 6e  b. If it contain
1dae5 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 69  s.    ** text, i
1dae6 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20 63  t is up to the c
1dae7 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68 65  aller to get the
1dae8 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 2e   encoding right.
1dae9 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 72  .    */.    Incr
1daea 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 20  blob *pBlob;.   
1daeb 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e 61   u32 type = v->a
1daec 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b 69  pCsr[0]->aType[i
1daed 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 74  Col];..    if( t
1daee 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 20  ype<12 ){.      
1daef 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1daf0 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1daf1 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65  Err, "cannot ope
1daf2 6e 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20  n value of type 
1daf3 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74  %s",.          t
1daf4 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74  ype==0?"null": t
1daf5 79 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22  ype==7?"real": "
1daf6 69 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20 29  integer".      )
1daf7 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
1daf8 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
1daf9 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f   goto blob_open_
1dafa 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  out;.    }.    p
1dafb 42 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62  Blob = (Incrblob
1dafc 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   *)sqlite3DbMall
1dafd 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
1dafe 66 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20  f(Incrblob));.  
1daff 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
1db00 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
1db01 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1db02 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20  , pBlob);.      
1db03 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f  goto blob_open_o
1db04 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  ut;.    }.    pB
1db05 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61  lob->flags = fla
1db06 67 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70  gs;.    pBlob->p
1db07 43 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b  Csr =  v->apCsr[
1db08 30 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20  0]->pCursor;.   
1db09 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1db0a 65 72 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e  erCursor(pBlob->
1db0b 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  pCsr);.    sqlit
1db0c 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72  e3BtreeCacheOver
1db0d 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72  flow(pBlob->pCsr
1db0e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
1db0f 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70  reeLeaveCursor(p
1db10 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20  Blob->pCsr);.   
1db11 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20   pBlob->pStmt = 
1db12 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
1db13 76 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f  v;.    pBlob->iO
1db14 66 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72  ffset = v->apCsr
1db15 5b 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f  [0]->aOffset[iCo
1db16 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e  l];.    pBlob->n
1db17 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64  Byte = sqlite3Vd
1db18 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
1db19 74 79 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62  type);.    pBlob
1db1a 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a  ->db = db;.    *
1db1b 70 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65  ppBlob = (sqlite
1db1c 33 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a  3_blob *)pBlob;.
1db1d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1db1e 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  OK;.  }else if( 
1db1f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1db20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1db21 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
1db22 72 29 2c 20 7a 45 72 72 2c 20 22 6e 6f 20 73 75  r), zErr, "no su
1db23 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22 2c  ch rowid: %lld",
1db24 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d   iRow);.    rc =
1db25 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1db26 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75   }..blob_open_ou
1db27 74 3a 0a 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66  t:.  zErr[sizeof
1db28 28 7a 45 72 72 29 2d 31 5d 20 3d 20 27 5c 30 27  (zErr)-1] = '\0'
1db29 3b 0a 20 20 69 66 28 20 76 20 26 26 20 28 72 63  ;.  if( v && (rc
1db2a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64  !=SQLITE_OK || d
1db2b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
1db2c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1db2d 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a  dbeFinalize(v);.
1db2e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72    }.  sqlite3Err
1db2f 6f 72 28 64 62 2c 20 72 63 2c 20 28 72 63 3d 3d  or(db, rc, (rc==
1db30 53 51 4c 49 54 45 5f 4f 4b 3f 30 3a 7a 45 72 72  SQLITE_OK?0:zErr
1db31 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ));.  rc = sqlit
1db32 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
1db33 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
1db34 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
1db35 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
1db36 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
1db37 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74   a blob handle t
1db38 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 73  hat was previous
1db39 6c 79 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  ly created using
1db3a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  .** sqlite3_blob
1db3b 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49  _open()..*/.SQLI
1db3c 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1db3d 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71  e3_blob_close(sq
1db3e 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f  lite3_blob *pBlo
1db3f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a  b){.  Incrblob *
1db40 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29  p = (Incrblob *)
1db41 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63 3b  pBlob;.  int rc;
1db42 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1db43 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  .  db = p->db;. 
1db44 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1db45 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
1db46 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
1db47 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53 74 6d  finalize(p->pStm
1db48 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  t);.  sqlite3DbF
1db49 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 73 71  ree(db, p);.  sq
1db4a 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1db4b 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1db4c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1db4d 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 65  .** Perform a re
1db4e 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65 72  ad or write oper
1db4f 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 0a  ation on a blob.
1db50 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 6c  */.static int bl
1db51 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20 73  obReadWrite(.  s
1db52 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c  qlite3_blob *pBl
1db53 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c 20  ob, .  void *z, 
1db54 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e 74  .  int n, .  int
1db55 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e 74   iOffset, .  int
1db56 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72 73   (*xCall)(BtCurs
1db57 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20 76  or*, u32, u32, v
1db58 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72  oid*).){.  int r
1db59 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70  c;.  Incrblob *p
1db5a 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70   = (Incrblob *)p
1db5b 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b  Blob;.  Vdbe *v;
1db5c 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1db5d 20 70 2d 3e 64 62 3b 20 20 0a 0a 20 20 73 71 6c   p->db;  ..  sql
1db5e 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1db5f 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76  (db->mutex);.  v
1db60 20 3d 20 28 56 64 62 65 2a 29 70 2d 3e 70 53 74   = (Vdbe*)p->pSt
1db61 6d 74 3b 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c  mt;..  if( n<0 |
1db62 7c 20 69 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28  | iOffset<0 || (
1db63 69 4f 66 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42  iOffset+n)>p->nB
1db64 79 74 65 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65  yte ){.    /* Re
1db65 71 75 65 73 74 20 69 73 20 6f 75 74 20 6f 66 20  quest is out of 
1db66 72 61 6e 67 65 2e 20 52 65 74 75 72 6e 20 61 20  range. Return a 
1db67 74 72 61 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e  transient error.
1db68 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   */.    rc = SQL
1db69 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73  ITE_ERROR;.    s
1db6a 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
1db6b 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 30 29  SQLITE_ERROR, 0)
1db6c 3b 0a 20 20 7d 20 65 6c 73 65 20 69 66 28 20 76  ;.  } else if( v
1db6d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  ==0 ){.    /* If
1db6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61   there is no sta
1db6f 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74  tement handle, t
1db70 68 65 6e 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e  hen the blob-han
1db71 64 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 61  dle has.    ** a
1db72 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 76 61  lready been inva
1db73 6c 69 64 61 74 65 64 2e 20 52 65 74 75 72 6e 20  lidated. Return 
1db74 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 6e 20  SQLITE_ABORT in 
1db75 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 2a  this case..    *
1db76 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  /.    rc = SQLIT
1db77 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65  E_ABORT;.  }else
1db78 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69  {.    /* Call ei
1db79 74 68 65 72 20 42 74 72 65 65 44 61 74 61 28 29  ther BtreeData()
1db7a 20 6f 72 20 42 74 72 65 65 50 75 74 44 61 74 61   or BtreePutData
1db7b 28 29 2e 20 49 66 20 53 51 4c 49 54 45 5f 41 42  (). If SQLITE_AB
1db7c 4f 52 54 20 69 73 0a 20 20 20 20 2a 2a 20 72 65  ORT is.    ** re
1db7d 74 75 72 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70  turned, clean-up
1db7e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68   the statement h
1db7f 61 6e 64 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  andle..    */.  
1db80 20 20 61 73 73 65 72 74 28 20 64 62 20 3d 3d 20    assert( db == 
1db81 76 2d 3e 64 62 20 29 3b 0a 20 20 20 20 73 71 6c  v->db );.    sql
1db82 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 43 75  ite3BtreeEnterCu
1db83 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20  rsor(p->pCsr);. 
1db84 20 20 20 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d     rc = xCall(p-
1db85 3e 70 43 73 72 2c 20 69 4f 66 66 73 65 74 2b 70  >pCsr, iOffset+p
1db86 2d 3e 69 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29  ->iOffset, n, z)
1db87 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1db88 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 2d  eeLeaveCursor(p-
1db89 3e 70 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20  >pCsr);.    if( 
1db8a 72 63 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54  rc==SQLITE_ABORT
1db8b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1db8c 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29  3VdbeFinalize(v)
1db8d 3b 0a 20 20 20 20 20 20 70 2d 3e 70 53 74 6d 74  ;.      p->pStmt
1db8e 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
1db8f 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f  .      db->errCo
1db90 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 76  de = rc;.      v
1db91 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d  ->rc = rc;.    }
1db92 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
1db93 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
1db94 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
1db95 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
1db96 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
1db97 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  c;.}../*.** Read
1db98 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f   data from a blo
1db99 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c  b handle..*/.SQL
1db9a 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1db9b 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71  te3_blob_read(sq
1db9c 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f  lite3_blob *pBlo
1db9d 62 2c 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20  b, void *z, int 
1db9e 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b  n, int iOffset){
1db9f 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65  .  return blobRe
1dba0 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a  adWrite(pBlob, z
1dba1 2c 20 6e 2c 20 69 4f 66 66 73 65 74 2c 20 73 71  , n, iOffset, sq
1dba2 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 29 3b  lite3BtreeData);
1dba3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
1dba4 64 61 74 61 20 74 6f 20 61 20 62 6c 6f 62 20 68  data to a blob h
1dba5 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  andle..*/.SQLITE
1dba6 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1dba7 5f 62 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c 69  _blob_write(sqli
1dba8 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c  te3_blob *pBlob,
1dba9 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20   const void *z, 
1dbaa 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73  int n, int iOffs
1dbab 65 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c  et){.  return bl
1dbac 6f 62 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f  obReadWrite(pBlo
1dbad 62 2c 20 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c  b, (void *)z, n,
1dbae 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65   iOffset, sqlite
1dbaf 33 42 74 72 65 65 50 75 74 44 61 74 61 29 3b 0a  3BtreePutData);.
1dbb0 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61  }../*.** Query a
1dbb1 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72   blob handle for
1dbb2 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1dbb3 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   data..**.** The
1dbb4 20 49 6e 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20   Incrblob.nByte 
1dbb5 66 69 65 6c 64 20 69 73 20 66 69 78 65 64 20 66  field is fixed f
1dbb6 6f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20  or the lifetime 
1dbb7 6f 66 20 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a  of the Incrblob.
1dbb8 2a 2a 20 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69  ** so no mutex i
1dbb9 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61  s required for a
1dbba 63 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ccess..*/.SQLITE
1dbbb 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1dbbc 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69  _blob_bytes(sqli
1dbbd 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29  te3_blob *pBlob)
1dbbe 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20  {.  Incrblob *p 
1dbbf 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42  = (Incrblob *)pB
1dbc0 6c 6f 62 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d  lob;.  return p-
1dbc1 3e 6e 42 79 74 65 3b 0a 7d 0a 0a 23 65 6e 64 69  >nByte;.}..#endi
1dbc2 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
1dbc3 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
1dbc4 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  B */../*********
1dbc5 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
1dbc6 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  eblob.c ********
1dbc7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbc9 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1dbca 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1dbcb 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a   journal.c *****
1dbcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbce 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
1dbcf 20 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a   August 22.**.**
1dbd0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1dbd1 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1dbd2 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1dbd3 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1dbd4 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1dbd5 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1dbd6 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1dbd7 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1dbd8 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1dbd9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1dbda 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1dbdb 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1dbdc 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1dbdd 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1dbde 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1dbdf 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1dbe0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1dbe1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbe2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbe3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1dbe6 40 28 23 29 20 24 49 64 3a 20 6a 6f 75 72 6e 61  @(#) $Id: journa
1dbe7 6c 2e 63 2c 76 20 31 2e 39 20 32 30 30 39 2f 30  l.c,v 1.9 2009/0
1dbe8 31 2f 32 30 20 31 37 3a 30 36 3a 32 37 20 64 61  1/20 17:06:27 da
1dbe9 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
1dbea 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
1dbeb 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
1dbec 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  WRITE../*.** Thi
1dbed 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
1dbee 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64  s a special kind
1dbef 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
1dbf0 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20   object used.** 
1dbf1 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65  by SQLite to cre
1dbf2 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ate journal file
1dbf3 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d  s if the atomic-
1dbf4 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
1dbf5 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64  on.** is enabled
1dbf6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74  ..**.** The dist
1dbf7 69 6e 63 74 69 76 65 20 63 68 61 72 61 63 74 65  inctive characte
1dbf8 72 69 73 74 69 63 20 6f 66 20 74 68 69 73 20 73  ristic of this s
1dbf9 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74  qlite3_file is t
1dbfa 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61  hat the.** actua
1dbfb 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69  l on disk file i
1dbfc 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79  s created lazily
1dbfd 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20  . When the file 
1dbfe 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74  is created,.** t
1dbff 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66  he caller specif
1dc00 69 65 73 20 61 20 62 75 66 66 65 72 20 73 69 7a  ies a buffer siz
1dc01 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  e for an in-memo
1dc02 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20  ry buffer to.** 
1dc03 62 65 20 75 73 65 64 20 74 6f 20 73 65 72 76 69  be used to servi
1dc04 63 65 20 72 65 61 64 28 29 20 61 6e 64 20 77 72  ce read() and wr
1dc05 69 74 65 28 29 20 72 65 71 75 65 73 74 73 2e 20  ite() requests. 
1dc06 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a  The actual file.
1dc07 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f  ** on disk is no
1dc08 74 20 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70  t created or pop
1dc09 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74  ulated until eit
1dc0a 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20  her:.**.**   1) 
1dc0b 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  The in-memory re
1dc0c 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f  presentation gro
1dc0d 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72  ws too large for
1dc0e 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a   the allocated .
1dc0f 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72 2c 20  **      buffer, 
1dc10 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 73  or.**   2) The s
1dc11 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65  qlite3JournalCre
1dc12 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ate() function i
1dc13 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a  s called..*/....
1dc14 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c 46  /*.** A JournalF
1dc15 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 61 20  ile object is a 
1dc16 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69  subclass of sqli
1dc17 74 65 33 5f 66 69 6c 65 20 75 73 65 64 20 62 79  te3_file used by
1dc18 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e 20 66  .** as an open f
1dc19 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 6a  ile handle for j
1dc1a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f  ournal files..*/
1dc1b 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46  .struct JournalF
1dc1c 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ile {.  sqlite3_
1dc1d 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74  io_methods *pMet
1dc1e 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f 20 6d  hod;    /* I/O m
1dc1f 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e 61  ethods on journa
1dc20 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69 6e 74  l files */.  int
1dc21 20 6e 42 75 66 3b 20 20 20 20 20 20 20 20 20 20   nBuf;          
1dc22 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1dc23 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d 20 69  Size of zBuf[] i
1dc24 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61  n bytes */.  cha
1dc25 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 20 20 20  r *zBuf;        
1dc26 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1dc27 53 70 61 63 65 20 74 6f 20 62 75 66 66 65 72 20  Space to buffer 
1dc28 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 73 20 2a  journal writes *
1dc29 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b 20 20  /.  int iSize;  
1dc2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dc2b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66      /* Amount of
1dc2c 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e 74 6c   zBuf[] currentl
1dc2d 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20  y used */.  int 
1dc2e 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
1dc2f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1dc30 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20  Open flags */.  
1dc31 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1dc32 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
1dc33 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e  /* The "real" un
1dc34 64 65 72 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0a  derlying VFS */.
1dc35 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1dc36 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 20 20  pReal;          
1dc37 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20    /* The "real" 
1dc38 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20  underlying file 
1dc39 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20  descriptor */.  
1dc3a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75  const char *zJou
1dc3b 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  rnal;           
1dc3c 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a  /* Name of the j
1dc3d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 7d  ournal file */.}
1dc3e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
1dc3f 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a 6f 75   JournalFile Jou
1dc40 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a  rnalFile;../*.**
1dc41 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   If it does not 
1dc42 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c 20  already exists, 
1dc43 63 72 65 61 74 65 20 61 6e 64 20 70 6f 70 75 6c  create and popul
1dc44 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 73 6b 20  ate the on-disk 
1dc45 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f 75  file .** for Jou
1dc46 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a 73  rnalFile p..*/.s
1dc47 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74 65  tatic int create
1dc48 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  File(JournalFile
1dc49 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d   *p){.  int rc =
1dc4a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
1dc4b 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  ( !p->pReal ){. 
1dc4c 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20     sqlite3_file 
1dc4d 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c 69 74 65  *pReal = (sqlite
1dc4e 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d 3b 0a  3_file *)&p[1];.
1dc4f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1dc50 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73 2c 20  OsOpen(p->pVfs, 
1dc51 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52 65  p->zJournal, pRe
1dc52 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20 30 29  al, p->flags, 0)
1dc53 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1dc54 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1dc55 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52 65 61   p->pReal = pRea
1dc56 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  l;.      if( p->
1dc57 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20  iSize>0 ){.     
1dc58 20 20 20 61 73 73 65 72 74 28 70 2d 3e 69 53 69     assert(p->iSi
1dc59 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20 20  ze<=p->nBuf);.  
1dc5a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1dc5b 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52 65  e3OsWrite(p->pRe
1dc5c 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e  al, p->zBuf, p->
1dc5d 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20  iSize, 0);.     
1dc5e 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
1dc5f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1dc60 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c  ** Close the fil
1dc61 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1dc62 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74   jrnlClose(sqlit
1dc63 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a  e3_file *pJfd){.
1dc64 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70    JournalFile *p
1dc65 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20   = (JournalFile 
1dc66 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d  *)pJfd;.  if( p-
1dc67 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71  >pReal ){.    sq
1dc68 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e  lite3OsClose(p->
1dc69 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 73 71  pReal);.  }.  sq
1dc6a 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42  lite3_free(p->zB
1dc6b 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  uf);.  return SQ
1dc6c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1dc6d 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d  * Read data from
1dc6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74   the file..*/.st
1dc6f 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52 65 61  atic int jrnlRea
1dc70 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  d(.  sqlite3_fil
1dc71 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54  e *pJfd,    /* T
1dc72 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1dc73 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65  from which to re
1dc74 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42  ad */.  void *zB
1dc75 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  uf,            /
1dc76 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c 74  * Put the result
1dc77 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  s here */.  int 
1dc78 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20  iAmt,           
1dc79 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1dc7a 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f  bytes to read */
1dc7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
1dc7c 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67  iOfst     /* Beg
1dc7d 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68  in reading at th
1dc7e 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a  is offset */.){.
1dc7f 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1dc80 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46  E_OK;.  JournalF
1dc81 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
1dc82 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
1dc83 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a  if( p->pReal ){.
1dc84 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1dc85 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c  OsRead(p->pReal,
1dc86 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66   zBuf, iAmt, iOf
1dc87 73 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  st);.  }else if(
1dc88 20 28 69 41 6d 74 2b 69 4f 66 73 74 29 3e 70 2d   (iAmt+iOfst)>p-
1dc89 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63  >iSize ){.    rc
1dc8a 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
1dc8b 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 65  SHORT_READ;.  }e
1dc8c 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  lse{.    memcpy(
1dc8d 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b 69  zBuf, &p->zBuf[i
1dc8e 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20 20  Ofst], iAmt);.  
1dc8f 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1dc90 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
1dc91 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a  ta to the file..
1dc92 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1dc93 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  nlWrite(.  sqlit
1dc94 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1dc95 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1dc96 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68   file into which
1dc97 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63   to write */.  c
1dc98 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c  onst void *zBuf,
1dc99 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61        /* Take da
1dc9a 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  ta to be written
1dc9b 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20   from here */.  
1dc9c 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1dc9d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1dc9e 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69   of bytes to wri
1dc9f 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  te */.  sqlite_i
1dca0 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f  nt64 iOfst     /
1dca1 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20  * Begin writing 
1dca2 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69  at this offset i
1dca3 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  nto the file */.
1dca4 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1dca5 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e  LITE_OK;.  Journ
1dca6 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1dca7 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1dca8 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c  .  if( !p->pReal
1dca9 20 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74 29   && (iOfst+iAmt)
1dcaa 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20 20  >p->nBuf ){.    
1dcab 72 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65 28  rc = createFile(
1dcac 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  p);.  }.  if( rc
1dcad 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1dcae 20 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20     if( p->pReal 
1dcaf 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1dcb0 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e  lite3OsWrite(p->
1dcb1 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d  pReal, zBuf, iAm
1dcb2 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20 7d  t, iOfst);.    }
1dcb3 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63  else{.      memc
1dcb4 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73  py(&p->zBuf[iOfs
1dcb5 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29 3b  t], zBuf, iAmt);
1dcb6 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53  .      if( p->iS
1dcb7 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74 29  ize<(iOfst+iAmt)
1dcb8 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
1dcb9 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69 41  Size = (iOfst+iA
1dcba 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  mt);.      }.   
1dcbb 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1dcbc 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  rc;.}../*.** Tru
1dcbd 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a  ncate the file..
1dcbe 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1dcbf 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  nlTruncate(sqlit
1dcc0 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73  e3_file *pJfd, s
1dcc1 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65  qlite_int64 size
1dcc2 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1dcc3 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e  LITE_OK;.  Journ
1dcc4 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1dcc5 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1dcc6 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20  .  if( p->pReal 
1dcc7 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1dcc8 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 2d  te3OsTruncate(p-
1dcc9 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a 20  >pReal, size);. 
1dcca 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 3c   }else if( size<
1dccb 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20  p->iSize ){.    
1dccc 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65 3b  p->iSize = size;
1dccd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1dcce 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20  ;.}../*.** Sync 
1dccf 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1dcd0 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e 63  tic int jrnlSync
1dcd1 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1dcd2 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  Jfd, int flags){
1dcd3 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f 75  .  int rc;.  Jou
1dcd4 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
1dcd5 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
1dcd6 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61  d;.  if( p->pRea
1dcd7 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
1dcd8 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e 70  lite3OsSync(p->p
1dcd9 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20 20  Real, flags);.  
1dcda 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1dcdb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1dcdc 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1dcdd 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73  *.** Query the s
1dcde 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
1dcdf 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  in bytes..*/.sta
1dce0 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c 65  tic int jrnlFile
1dce1 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  Size(sqlite3_fil
1dce2 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f  e *pJfd, sqlite_
1dce3 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20  int64 *pSize){. 
1dce4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1dce5 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69  _OK;.  JournalFi
1dce6 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
1dce7 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69  File *)pJfd;.  i
1dce8 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  f( p->pReal ){. 
1dce9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1dcea 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52 65  sFileSize(p->pRe
1dceb 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d 65  al, pSize);.  }e
1dcec 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20  lse{.    *pSize 
1dced 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  = (sqlite_int64)
1dcee 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a 20   p->iSize;.  }. 
1dcef 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1dcf0 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65  *.** Table of me
1dcf1 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e 61  thods for Journa
1dcf2 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69  lFile sqlite3_fi
1dcf3 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74  le object..*/.st
1dcf4 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c 69  atic struct sqli
1dcf5 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a  te3_io_methods J
1dcf6 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64  ournalFileMethod
1dcf7 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20  s = {.  1,      
1dcf8 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
1dcf9 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f 73  on */.  jrnlClos
1dcfa 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65  e,     /* xClose
1dcfb 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c 20   */.  jrnlRead, 
1dcfc 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f       /* xRead */
1dcfd 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20  .  jrnlWrite,   
1dcfe 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20    /* xWrite */. 
1dcff 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20   jrnlTruncate,  
1dd00 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a  /* xTruncate */.
1dd01 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20    jrnlSync,     
1dd02 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6a   /* xSync */.  j
1dd03 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a  rnlFileSize,  /*
1dd04 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20   xFileSize */.  
1dd05 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1dd06 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20  * xLock */.  0, 
1dd07 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1dd08 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20  Unlock */.  0,  
1dd09 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
1dd0a 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
1dd0b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1dd0c 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e       /* xFileCon
1dd0d 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20  trol */.  0,    
1dd0e 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63           /* xSec
1dd0f 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20  torSize */.  0  
1dd10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1dd11 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
1dd12 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  stics */.};../* 
1dd13 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e  .** Open a journ
1dd14 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  al file..*/.SQLI
1dd15 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1dd16 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65  qlite3JournalOpe
1dd17 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  n(.  sqlite3_vfs
1dd18 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20   *pVfs,         
1dd19 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 73  /* The VFS to us
1dd1a 65 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69 6c  e for actual fil
1dd1b 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73 74  e I/O */.  const
1dd1c 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
1dd1d 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1dd1e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1dd1f 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  e */.  sqlite3_f
1dd20 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20 20  ile *pJfd,      
1dd21 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65    /* Preallocate
1dd22 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68 61  d, blank file ha
1dd23 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  ndle */.  int fl
1dd24 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
1dd25 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20       /* Opening 
1dd26 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  flags */.  int n
1dd27 42 75 66 20 20 20 20 20 20 20 20 20 20 20 20 20  Buf             
1dd28 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 62        /* Bytes b
1dd29 75 66 66 65 72 65 64 20 62 65 66 6f 72 65 20 6f  uffered before o
1dd2a 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20  pening the file 
1dd2b 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46  */.){.  JournalF
1dd2c 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
1dd2d 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
1dd2e 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c  memset(p, 0, sql
1dd2f 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  ite3JournalSize(
1dd30 70 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e 42  pVfs));.  if( nB
1dd31 75 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e 7a  uf>0 ){.    p->z
1dd32 42 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Buf = sqlite3Mal
1dd33 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a 20  locZero(nBuf);. 
1dd34 20 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66 20     if( !p->zBuf 
1dd35 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1dd36 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1dd37 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
1dd38 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f   return sqlite3O
1dd39 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d  sOpen(pVfs, zNam
1dd3a 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c 20  e, pJfd, flags, 
1dd3b 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d 65  0);.  }.  p->pMe
1dd3c 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c 46  thod = &JournalF
1dd3d 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70 2d  ileMethods;.  p-
1dd3e 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 20  >nBuf = nBuf;.  
1dd3f 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73  p->flags = flags
1dd40 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20  ;.  p->zJournal 
1dd41 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70 56  = zName;.  p->pV
1dd42 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65 74  fs = pVfs;.  ret
1dd43 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1dd44 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61  ../*.** If the a
1dd45 72 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74 73  rgument p points
1dd46 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c   to a JournalFil
1dd47 65 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e 64  e structure, and
1dd48 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a   the underlying.
1dd49 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20  ** file has not 
1dd4a 79 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64  yet been created
1dd4b 2c 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77 2e  , create it now.
1dd4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1dd4d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f  TE int sqlite3Jo
1dd4e 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69  urnalCreate(sqli
1dd4f 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 20  te3_file *p){.  
1dd50 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73 21  if( p->pMethods!
1dd51 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74  =&JournalFileMet
1dd52 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75  hods ){.    retu
1dd53 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1dd54 7d 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74  }.  return creat
1dd55 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46 69  eFile((JournalFi
1dd56 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a  le *)p);.}../* .
1dd57 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1dd58 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
1dd59 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20  quired to store 
1dd5a 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68  a JournalFile th
1dd5b 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70  at uses vfs.** p
1dd5c 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68  Vfs to create th
1dd5d 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d  e underlying on-
1dd5e 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53  disk files..*/.S
1dd5f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1dd60 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  t sqlite3Journal
1dd61 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73  Size(sqlite3_vfs
1dd62 20 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 72   *pVfs){.  retur
1dd63 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  n (pVfs->szOsFil
1dd64 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c  e+sizeof(Journal
1dd65 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 66  File));.}.#endif
1dd66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1dd67 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 6c  * End of journal
1dd68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1dd69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd6b 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1dd6c 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d  * Begin file mem
1dd6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a  journal.c ******
1dd6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd70 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74  /./*.** 2008 Oct
1dd71 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65  ober 7.**.** The
1dd72 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1dd73 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1dd74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1dd75 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1dd76 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1dd77 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1dd78 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1dd79 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1dd7a 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1dd7b 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1dd7c 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1dd7d 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1dd7e 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1dd7f 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1dd80 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1dd81 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1dd82 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1dd83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd87 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1dd88 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
1dd89 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65  ode use to imple
1dd8a 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  ment an in-memor
1dd8b 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  y rollback journ
1dd8c 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65  al..** The in-me
1dd8d 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  mory rollback jo
1dd8e 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 6f  urnal is used to
1dd8f 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 63   journal transac
1dd90 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a 6d  tions for.** ":m
1dd91 65 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 65  emory:" database
1dd92 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 6a  s and when the j
1dd93 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f  ournal_mode=MEMO
1dd94 52 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 65  RY pragma is use
1dd95 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  d..**.** @(#) $I
1dd96 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 2c  d: memjournal.c,
1dd97 76 20 31 2e 31 32 20 32 30 30 39 2f 30 35 2f 30  v 1.12 2009/05/0
1dd98 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65  4 11:42:30 danie
1dd99 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
1dd9a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
1dd9b 72 65 6e 63 65 73 20 74 6f 20 69 6e 74 65 72 6e  rences to intern
1dd9c 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f  al structures */
1dd9d 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1dd9e 4d 65 6d 4a 6f 75 72 6e 61 6c 20 4d 65 6d 4a 6f  MemJournal MemJo
1dd9f 75 72 6e 61 6c 3b 0a 74 79 70 65 64 65 66 20 73  urnal;.typedef s
1dda0 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20  truct FilePoint 
1dda1 46 69 6c 65 50 6f 69 6e 74 3b 0a 74 79 70 65 64  FilePoint;.typed
1dda2 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 43 68  ef struct FileCh
1dda3 75 6e 6b 20 46 69 6c 65 43 68 75 6e 6b 3b 0a 0a  unk FileChunk;..
1dda4 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  /* Space to hold
1dda5 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
1dda6 75 72 6e 61 6c 20 69 73 20 61 6c 6c 6f 63 61 74  urnal is allocat
1dda7 65 64 20 69 6e 20 69 6e 63 72 65 6d 65 6e 74 73  ed in increments
1dda8 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6d 61 6e 79   of.** this many
1dda9 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   bytes..**.** Th
1ddaa 65 20 73 69 7a 65 20 63 68 6f 73 65 6e 20 69 73  e size chosen is
1ddab 20 61 20 6c 69 74 74 6c 65 20 6c 65 73 73 20 74   a little less t
1ddac 68 61 6e 20 61 20 70 6f 77 65 72 20 6f 66 20 74  han a power of t
1ddad 77 6f 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a  wo.  That way,.*
1ddae 2a 20 74 68 65 20 46 69 6c 65 43 68 75 6e 6b 20  * the FileChunk 
1ddaf 6f 62 6a 65 63 74 20 77 69 6c 6c 20 68 61 76 65  object will have
1ddb0 20 61 20 73 69 7a 65 20 74 68 61 74 20 61 6c 6d   a size that alm
1ddb1 6f 73 74 20 65 78 61 63 74 6c 79 20 66 69 6c 6c  ost exactly fill
1ddb2 73 0a 2a 2a 20 61 20 70 6f 77 65 72 2d 6f 66 2d  s.** a power-of-
1ddb3 74 77 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  two allocation. 
1ddb4 20 54 68 69 73 20 6d 69 6d 69 6d 69 7a 65 73 20   This mimimizes 
1ddb5 77 61 73 74 65 64 20 73 70 61 63 65 20 69 6e 20  wasted space in 
1ddb6 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 0a 2a 2a 20  power-of-two.** 
1ddb7 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
1ddb8 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f  s..*/.#define JO
1ddb9 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20  URNAL_CHUNKSIZE 
1ddba 28 28 69 6e 74 29 28 31 30 32 34 2d 73 69 7a 65  ((int)(1024-size
1ddbb 6f 66 28 46 69 6c 65 43 68 75 6e 6b 2a 29 29 29  of(FileChunk*)))
1ddbc 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69  ../* Macro to fi
1ddbd 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f  nd the minimum o
1ddbe 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61  f two numeric va
1ddbf 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  lues..*/.#ifndef
1ddc0 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49   MIN.# define MI
1ddc1 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f  N(x,y) ((x)<(y)?
1ddc2 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a  (x):(y)).#endif.
1ddc3 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62  ./*.** The rollb
1ddc4 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63  ack journal is c
1ddc5 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e  omposed of a lin
1ddc6 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73  ked list of thes
1ddc7 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  e structures..*/
1ddc8 0a 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e  .struct FileChun
1ddc9 6b 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20  k {.  FileChunk 
1ddca 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
1ddcb 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68        /* Next ch
1ddcc 75 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  unk in the journ
1ddcd 61 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e  al */.  u8 zChun
1ddce 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53  k[JOURNAL_CHUNKS
1ddcf 49 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65  IZE];   /* Conte
1ddd0 6e 74 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b  nt of this chunk
1ddd1 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
1ddd2 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
1ddd3 73 20 6f 62 6a 65 63 74 20 73 65 72 76 65 73 20  s object serves 
1ddd4 61 73 20 61 20 63 75 72 73 6f 72 20 69 6e 74 6f  as a cursor into
1ddd5 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
1ddd6 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75  urnal..** The cu
1ddd7 72 73 6f 72 20 63 61 6e 20 62 65 20 65 69 74 68  rsor can be eith
1ddd8 65 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f  er for reading o
1ddd9 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74  r writing..*/.st
1ddda 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b  ruct FilePoint {
1dddb 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
1dddc 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20   iOffset;       
1dddd 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f     /* Offset fro
1ddde 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
1dddf 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20  of the file */. 
1dde0 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75   FileChunk *pChu
1dde1 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nk;             
1dde2 20 2f 2a 20 53 70 65 63 69 66 69 63 20 63 68 75   /* Specific chu
1dde3 6e 6b 20 69 6e 74 6f 20 77 68 69 63 68 20 63 75  nk into which cu
1dde4 72 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d  rsor points */.}
1dde5 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75  ;../*.** This su
1dde6 62 63 6c 61 73 73 20 69 73 20 61 20 73 75 62 63  bclass is a subc
1dde7 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  lass of sqlite3_
1dde8 66 69 6c 65 2e 20 20 45 61 63 68 20 6f 70 65 6e  file.  Each open
1dde9 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a   memory-journal.
1ddea 2a 2a 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ** is an instanc
1ddeb 65 20 6f 66 20 74 68 69 73 20 63 6c 61 73 73 2e  e of this class.
1ddec 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f  .*/.struct MemJo
1dded 75 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65  urnal {.  sqlite
1ddee 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
1ddef 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72  ethod;    /* Par
1ddf0 65 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53 54 20  ent class. MUST 
1ddf1 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 46 69  BE FIRST */.  Fi
1ddf2 6c 65 43 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b  leChunk *pFirst;
1ddf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ddf4 20 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f   Head of in-memo
1ddf5 72 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f  ry chunk-list */
1ddf6 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64  .  FilePoint end
1ddf7 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  point;          
1ddf8 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1ddf9 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1ddfa 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f  file */.  FilePo
1ddfb 69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b 20 20  int readpoint;  
1ddfc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
1ddfd 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20  nter to the end 
1ddfe 6f 66 20 74 68 65 20 6c 61 73 74 20 78 52 65 61  of the last xRea
1ddff 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  d() */.};../*.**
1de00 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20   Read data from 
1de01 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f  the in-memory jo
1de02 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 54 68 69  urnal file.  Thi
1de03 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  s is the impleme
1de04 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68  ntation.** of th
1de05 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 52  e sqlite3_vfs.xR
1de06 65 61 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ead method..*/.s
1de07 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e  tatic int memjrn
1de08 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33  lRead(.  sqlite3
1de09 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20  _file *pJfd,    
1de0a 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
1de0b 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ile from which t
1de0c 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64  o read */.  void
1de0d 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20   *zBuf,         
1de0e 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65     /* Put the re
1de0f 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
1de10 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1de11 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1de12 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61   of bytes to rea
1de13 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  d */.  sqlite_in
1de14 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
1de15 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
1de16 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
1de17 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c  .){.  MemJournal
1de18 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61   *p = (MemJourna
1de19 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a  l *)pJfd;.  u8 *
1de1a 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69  zOut = zBuf;.  i
1de1b 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b  nt nRead = iAmt;
1de1c 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66  .  int iChunkOff
1de1d 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b  set;.  FileChunk
1de1e 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 2f 2a 20   *pChunk;..  /* 
1de1f 53 51 4c 69 74 65 20 6e 65 76 65 72 20 74 72 69  SQLite never tri
1de20 65 73 20 74 6f 20 72 65 61 64 20 70 61 73 74 20  es to read past 
1de21 74 68 65 20 65 6e 64 20 6f 66 20 61 20 72 6f 6c  the end of a rol
1de22 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69  lback journal fi
1de23 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  le */.  assert( 
1de24 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65  iOfst+iAmt<=p->e
1de25 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  ndpoint.iOffset 
1de26 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 65 61  );..  if( p->rea
1de27 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d  dpoint.iOffset!=
1de28 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d  iOfst || iOfst==
1de29 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1de2a 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20 30 3b  _int64 iOff = 0;
1de2b 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d  .    for(pChunk=
1de2c 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20 20 20  p->pFirst; .    
1de2d 20 20 20 20 41 4c 57 41 59 53 28 70 43 68 75 6e      ALWAYS(pChun
1de2e 6b 29 20 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52  k) && (iOff+JOUR
1de2f 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d  NAL_CHUNKSIZE)<=
1de30 69 4f 66 73 74 3b 0a 20 20 20 20 20 20 20 20 70  iOfst;.        p
1de31 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e  Chunk=pChunk->pN
1de32 65 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ext.    ){.     
1de33 20 69 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c   iOff += JOURNAL
1de34 5f 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20  _CHUNKSIZE;.    
1de35 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  }.  }else{.    p
1de36 43 68 75 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70  Chunk = p->readp
1de37 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d  oint.pChunk;.  }
1de38 0a 0a 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  ..  iChunkOffset
1de39 20 3d 20 28 69 6e 74 29 28 69 4f 66 73 74 25 4a   = (int)(iOfst%J
1de3a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
1de3b 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e  );.  do {.    in
1de3c 74 20 69 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e  t iSpace = JOURN
1de3d 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69  AL_CHUNKSIZE - i
1de3e 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20  ChunkOffset;.   
1de3f 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e   int nCopy = MIN
1de40 28 6e 52 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c  (nRead, (JOURNAL
1de41 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68  _CHUNKSIZE - iCh
1de42 75 6e 6b 4f 66 66 73 65 74 29 29 3b 0a 20 20 20  unkOffset));.   
1de43 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70   memcpy(zOut, &p
1de44 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43  Chunk->zChunk[iC
1de45 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f  hunkOffset], nCo
1de46 70 79 29 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d  py);.    zOut +=
1de47 20 6e 43 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61   nCopy;.    nRea
1de48 64 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20  d -= iSpace;.   
1de49 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20   iChunkOffset = 
1de4a 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 52  0;.  } while( nR
1de4b 65 61 64 3e 3d 30 20 26 26 20 28 70 43 68 75 6e  ead>=0 && (pChun
1de4c 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29  k=pChunk->pNext)
1de4d 21 3d 30 20 26 26 20 6e 52 65 61 64 3e 30 20 29  !=0 && nRead>0 )
1de4e 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74  ;.  p->readpoint
1de4f 2e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74  .iOffset = iOfst
1de50 2b 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64  +iAmt;.  p->read
1de51 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70  point.pChunk = p
1de52 43 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e  Chunk;..  return
1de53 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1de54 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  *.** Write data 
1de55 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  to the file..*/.
1de56 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72  static int memjr
1de57 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  nlWrite(.  sqlit
1de58 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1de59 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1de5a 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68   file into which
1de5b 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63   to write */.  c
1de5c 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c  onst void *zBuf,
1de5d 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61        /* Take da
1de5e 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  ta to be written
1de5f 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20   from here */.  
1de60 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1de61 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1de62 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69   of bytes to wri
1de63 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  te */.  sqlite_i
1de64 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f  nt64 iOfst     /
1de65 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20  * Begin writing 
1de66 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69  at this offset i
1de67 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  nto the file */.
1de68 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ){.  MemJournal 
1de69 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c  *p = (MemJournal
1de6a 20 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e   *)pJfd;.  int n
1de6b 57 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20  Write = iAmt;.  
1de6c 75 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38  u8 *zWrite = (u8
1de6d 20 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41   *)zBuf;..  /* A
1de6e 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72  n in-memory jour
1de6f 6e 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20  nal file should 
1de70 6f 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70  only ever be app
1de71 65 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d  ended to. Random
1de72 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69  .  ** access wri
1de73 74 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75  tes are not requ
1de74 69 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a  ired by sqlite..
1de75 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69    */.  assert( i
1de76 4f 66 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e  Ofst==p->endpoin
1de77 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 20 20 55  t.iOffset );.  U
1de78 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
1de79 69 4f 66 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65  iOfst);..  while
1de7a 28 20 6e 57 72 69 74 65 3e 30 20 29 7b 0a 20 20  ( nWrite>0 ){.  
1de7b 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68    FileChunk *pCh
1de7c 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e  unk = p->endpoin
1de7d 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e  t.pChunk;.    in
1de7e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d  t iChunkOffset =
1de7f 20 28 69 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69   (int)(p->endpoi
1de80 6e 74 2e 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e  nt.iOffset%JOURN
1de81 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20  AL_CHUNKSIZE);. 
1de82 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20     int iSpace = 
1de83 4d 49 4e 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52  MIN(nWrite, JOUR
1de84 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20  NAL_CHUNKSIZE - 
1de85 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a  iChunkOffset);..
1de86 20 20 20 20 69 66 28 20 69 43 68 75 6e 6b 4f 66      if( iChunkOf
1de87 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  fset==0 ){.     
1de88 20 2f 2a 20 4e 65 77 20 63 68 75 6e 6b 20 69 73   /* New chunk is
1de89 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 74   required to ext
1de8a 65 6e 64 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f  end the file. */
1de8b 0a 20 20 20 20 20 20 46 69 6c 65 43 68 75 6e 6b  .      FileChunk
1de8c 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
1de8d 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46  _malloc(sizeof(F
1de8e 69 6c 65 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20  ileChunk));.    
1de8f 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20    if( !pNew ){. 
1de90 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
1de91 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
1de92 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1de93 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  pNew->pNext = 0;
1de94 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 75 6e  .      if( pChun
1de95 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  k ){.        ass
1de96 65 72 74 28 20 70 2d 3e 70 46 69 72 73 74 20 29  ert( p->pFirst )
1de97 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b  ;.        pChunk
1de98 2d 3e 70 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a  ->pNext = pNew;.
1de99 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1de9a 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 2d       assert( !p-
1de9b 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20  >pFirst );.     
1de9c 20 20 20 70 2d 3e 70 46 69 72 73 74 20 3d 20 70     p->pFirst = p
1de9d 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  New;.      }.   
1de9e 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70     p->endpoint.p
1de9f 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20  Chunk = pNew;.  
1dea0 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28    }..    memcpy(
1dea1 26 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68  &p->endpoint.pCh
1dea2 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75  unk->zChunk[iChu
1dea3 6e 6b 4f 66 66 73 65 74 5d 2c 20 7a 57 72 69 74  nkOffset], zWrit
1dea4 65 2c 20 69 53 70 61 63 65 29 3b 0a 20 20 20 20  e, iSpace);.    
1dea5 7a 57 72 69 74 65 20 2b 3d 20 69 53 70 61 63 65  zWrite += iSpace
1dea6 3b 0a 20 20 20 20 6e 57 72 69 74 65 20 2d 3d 20  ;.    nWrite -= 
1dea7 69 53 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65  iSpace;.    p->e
1dea8 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  ndpoint.iOffset 
1dea9 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a  += iSpace;.  }..
1deaa 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1deab 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  OK;.}../*.** Tru
1deac 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e 0a  ncate the file..
1dead 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
1deae 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71  mjrnlTruncate(sq
1deaf 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1deb0 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73  , sqlite_int64 s
1deb1 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e  ize){.  MemJourn
1deb2 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72  al *p = (MemJour
1deb3 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69  nal *)pJfd;.  Fi
1deb4 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b  leChunk *pChunk;
1deb5 0a 20 20 61 73 73 65 72 74 28 73 69 7a 65 3d 3d  .  assert(size==
1deb6 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  0);.  UNUSED_PAR
1deb7 41 4d 45 54 45 52 28 73 69 7a 65 29 3b 0a 20 20  AMETER(size);.  
1deb8 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72  pChunk = p->pFir
1deb9 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 68  st;.  while( pCh
1deba 75 6e 6b 20 29 7b 0a 20 20 20 20 46 69 6c 65 43  unk ){.    FileC
1debb 68 75 6e 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68  hunk *pTmp = pCh
1debc 75 6e 6b 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20  unk;.    pChunk 
1debd 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b  = pChunk->pNext;
1debe 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
1debf 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73  e(pTmp);.  }.  s
1dec0 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c  qlite3MemJournal
1dec1 4f 70 65 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65  Open(pJfd);.  re
1dec2 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1dec3 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74  }../*.** Close t
1dec4 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
1dec5 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c  ic int memjrnlCl
1dec6 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
1dec7 20 2a 70 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72   *pJfd){.  memjr
1dec8 6e 6c 54 72 75 6e 63 61 74 65 28 70 4a 66 64 2c  nlTruncate(pJfd,
1dec9 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51   0);.  return SQ
1deca 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
1decb 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65  ** Sync the file
1decc 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e 63 69 6e 67 20  ..**.** Syncing 
1decd 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  an in-memory jou
1dece 72 6e 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  rnal is a no-op.
1decf 20 20 41 6e 64 2c 20 69 6e 20 66 61 63 74 2c 20    And, in fact, 
1ded0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1ded1 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20  is never called 
1ded2 69 6e 20 61 20 77 6f 72 6b 69 6e 67 20 69 6d 70  in a working imp
1ded3 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
1ded4 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
1ded5 6e 0a 2a 2a 20 65 78 69 73 74 73 20 70 75 72 65  n.** exists pure
1ded6 6c 79 20 61 73 20 61 20 63 6f 6e 74 69 6e 67 65  ly as a continge
1ded7 6e 63 79 2c 20 69 6e 20 63 61 73 65 20 73 6f 6d  ncy, in case som
1ded8 65 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 69 6e  e malfunction in
1ded9 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70   some other.** p
1deda 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20 63 61  art of SQLite ca
1dedb 75 73 65 73 20 53 79 6e 63 20 74 6f 20 62 65 20  uses Sync to be 
1dedc 63 61 6c 6c 65 64 20 62 79 20 6d 69 73 74 61 6b  called by mistak
1dedd 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1dede 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 28 73 71 6c   memjrnlSync(sql
1dedf 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73  ite3_file *NotUs
1dee0 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32  ed, int NotUsed2
1dee1 29 7b 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f  ){   /*NO_TEST*/
1dee2 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1dee3 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
1dee4 74 55 73 65 64 32 29 3b 20 20 20 20 20 20 20 20  tUsed2);        
1dee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dee6 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 61 73  /*NO_TEST*/.  as
1dee7 73 65 72 74 28 20 30 20 29 3b 20 20 20 20 20 20  sert( 0 );      
1dee8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dee9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1deea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
1deeb 54 45 53 54 2a 2f 0a 20 20 72 65 74 75 72 6e 20  TEST*/.  return 
1deec 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
1deed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1deee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1deef 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
1def0 2f 0a 7d 20 20 20 20 20 20 20 20 20 20 20 20 20  /.}             
1def1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1def2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1def3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1def4 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 2f 2a   /*NO_TEST*/../*
1def5 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69  .** Query the si
1def6 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
1def7 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  n bytes..*/.stat
1def8 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69  ic int memjrnlFi
1def9 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66  leSize(sqlite3_f
1defa 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74  ile *pJfd, sqlit
1defb 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b  e_int64 *pSize){
1defc 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70  .  MemJournal *p
1defd 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a   = (MemJournal *
1defe 29 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20  )pJfd;.  *pSize 
1deff 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  = (sqlite_int64)
1df00 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66   p->endpoint.iOf
1df01 66 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53  fset;.  return S
1df02 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1df03 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68  ** Table of meth
1df04 6f 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e  ods for MemJourn
1df05 61 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  al sqlite3_file 
1df06 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69  object..*/.stati
1df07 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  c struct sqlite3
1df08 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a  _io_methods MemJ
1df09 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20  ournalMethods = 
1df0a 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  {.  1,          
1df0b 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
1df0c 6e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c  n */.  memjrnlCl
1df0d 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f  ose,     /* xClo
1df0e 73 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52  se */.  memjrnlR
1df0f 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65  ead,      /* xRe
1df10 61 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57  ad */.  memjrnlW
1df11 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72  rite,     /* xWr
1df12 69 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c  ite */.  memjrnl
1df13 54 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54  Truncate,  /* xT
1df14 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d  runcate */.  mem
1df15 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f  jrnlSync,      /
1df16 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d  * xSync */.  mem
1df17 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f  jrnlFileSize,  /
1df18 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20  * xFileSize */. 
1df19 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1df1a 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20     /* xLock */. 
1df1b 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1df1c 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f     /* xUnlock */
1df1d 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
1df1e 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
1df1f 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20  servedLock */.  
1df20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1df21 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f    /* xFileContro
1df22 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  l */.  0,       
1df23 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63           /* xSec
1df24 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20  torSize */.  0  
1df25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1df26 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74  * xDeviceCharact
1df27 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a  eristics */.};..
1df28 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f  /* .** Open a jo
1df29 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53  urnal file..*/.S
1df2a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1df2b 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  id sqlite3MemJou
1df2c 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33  rnalOpen(sqlite3
1df2d 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20  _file *pJfd){.  
1df2e 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20  MemJournal *p = 
1df2f 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a  (MemJournal *)pJ
1df30 66 64 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49  fd;.  assert( EI
1df31 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
1df32 4e 54 28 70 29 20 29 3b 0a 20 20 6d 65 6d 73 65  NT(p) );.  memse
1df33 74 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d  t(p, 0, sqlite3M
1df34 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29  emJournalSize())
1df35 3b 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d  ;.  p->pMethod =
1df36 20 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68   &MemJournalMeth
1df37 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ods;.}../*.** Re
1df38 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
1df39 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 70 61 73   file-handle pas
1df3a 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65  sed as an argume
1df3b 6e 74 20 69 73 20 0a 2a 2a 20 61 6e 20 69 6e 2d  nt is .** an in-
1df3c 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 0a  memory journal .
1df3d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1df3e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d  E int sqlite3IsM
1df3f 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65  emJournal(sqlite
1df40 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20  3_file *pJfd){. 
1df41 20 72 65 74 75 72 6e 20 70 4a 66 64 2d 3e 70 4d   return pJfd->pM
1df42 65 74 68 6f 64 73 3d 3d 26 4d 65 6d 4a 6f 75 72  ethods==&MemJour
1df43 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f  nalMethods;.}../
1df44 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  * .** Return the
1df45 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1df46 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
1df47 72 65 20 61 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  re a MemJournal 
1df48 74 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a  that uses vfs.**
1df49 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 65 20   pVfs to create 
1df4a 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f  the underlying o
1df4b 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f  n-disk files..*/
1df4c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1df4d 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f  int sqlite3MemJo
1df4e 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 7b  urnalSize(void){
1df4f 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66  .  return sizeof
1df50 28 4d 65 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a  (MemJournal);.}.
1df51 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1df52 20 45 6e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e   End of memjourn
1df53 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  al.c ***********
1df54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1df56 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1df57 20 42 65 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b   Begin file walk
1df58 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
1df59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1df5b 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75  ./*.** 2008 Augu
1df5c 73 74 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 16.**.** The 
1df5d 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1df5e 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1df5f 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1df60 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1df61 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1df62 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1df63 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1df64 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1df65 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1df66 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1df67 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1df68 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1df69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1df6a 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1df6b 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1df6c 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1df6d 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1df6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df72 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
1df73 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69  e contains routi
1df74 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c  nes used for wal
1df75 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20  king the parser 
1df76 74 72 65 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53  tree for.** an S
1df77 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  QL statement..**
1df78 0a 2a 2a 20 24 49 64 3a 20 77 61 6c 6b 65 72 2e  .** $Id: walker.
1df79 63 2c 76 20 31 2e 34 20 32 30 30 39 2f 30 34 2f  c,v 1.4 2009/04/
1df7a 30 38 20 31 33 3a 35 31 3a 35 32 20 64 72 68 20  08 13:51:52 drh 
1df7b 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  Exp $.*/.../*.**
1df7c 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73   Walk an express
1df7d 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f 6b  ion tree.  Invok
1df7e 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f  e the callback o
1df7f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e 6f 64  nce for each nod
1df80 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72  e.** of the expr
1df81 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64 65  ession, while de
1df82 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f 74  cending.  (In ot
1df83 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63  her words, the c
1df84 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e  allback.** is in
1df85 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76 69 73  voked before vis
1df86 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e 29  iting children.)
1df87 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
1df88 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65  n value from the
1df89 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64   callback should
1df8a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 57   be one of the W
1df8b 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74  RC_*.** constant
1df8c 73 20 74 6f 20 73 70 65 63 69 66 79 20 68 6f 77  s to specify how
1df8d 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69 74 68   to proceed with
1df8e 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a   the walk..**.**
1df8f 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65      WRC_Continue
1df90 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65 20 64        Continue d
1df91 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20 74  escending down t
1df92 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 20  he tree..**.**  
1df93 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20    WRC_Prune     
1df94 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 65      Do not desce
1df95 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e 6f  nd into child no
1df96 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 0a  des.  But allow.
1df97 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1df98 20 20 20 20 20 20 20 20 74 68 65 20 77 61 6c 6b          the walk
1df99 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
1df9a 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 2e  h sibling nodes.
1df9b 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41 62  .**.**    WRC_Ab
1df9c 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f 20 6e  ort         Do n
1df9d 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b 73  o more callbacks
1df9e 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 73 74  .  Unwind the st
1df9f 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ack and.**      
1dfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dfa1 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70 2d 6c  return the top-l
1dfa2 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e 0a  evel walk call..
1dfa3 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
1dfa4 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73   value from this
1dfa5 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52 43 5f   routine is WRC_
1dfa6 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f 6e  Abort to abandon
1dfa7 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 0a 2a   the tree walk.*
1dfa8 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69 6e  * and WRC_Contin
1dfa9 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a  ue to continue..
1dfaa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1dfab 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c  E int sqlite3Wal
1dfac 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57  kExpr(Walker *pW
1dfad 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78  alker, Expr *pEx
1dfae 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  pr){.  int rc;. 
1dfaf 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
1dfb0 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
1dfb1 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73 65 28  nue;.  testcase(
1dfb2 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
1dfb3 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e  (pExpr, EP_Token
1dfb4 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73 74 63  Only) );.  testc
1dfb5 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f 70  ase( ExprHasProp
1dfb6 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 53  erty(pExpr, EP_S
1dfb7 70 61 6e 54 6f 6b 65 6e 29 20 29 3b 0a 20 20 74  panToken) );.  t
1dfb8 65 73 74 63 61 73 65 28 20 45 78 70 72 48 61 73  estcase( ExprHas
1dfb9 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
1dfba 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20  EP_Reduced) );. 
1dfbb 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78   rc = pWalker->x
1dfbc 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70 57 61  ExprCallback(pWa
1dfbd 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a 20 20  lker, pExpr);.  
1dfbe 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f 6e 74  if( rc==WRC_Cont
1dfbf 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20 20 20  inue.           
1dfc0 20 20 20 26 26 20 21 45 78 70 72 48 61 73 41 6e     && !ExprHasAn
1dfc1 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  yProperty(pExpr,
1dfc2 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f  EP_TokenOnly|EP_
1dfc3 53 70 61 6e 54 6f 6b 65 6e 29 20 29 7b 0a 20 20  SpanToken) ){.  
1dfc4 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1dfc5 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70  kExpr(pWalker, p
1dfc6 45 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72  Expr->pLeft) ) r
1dfc7 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1dfc8 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1dfc9 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72  WalkExpr(pWalker
1dfca 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
1dfcb 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1dfcc 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78 70  ort;.    if( Exp
1dfcd 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
1dfce 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
1dfcf 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ) ){.      if( s
1dfd0 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
1dfd1 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d  (pWalker, pExpr-
1dfd2 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65  >x.pSelect) ) re
1dfd3 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1dfd4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1dfd5 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1dfd6 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72  ExprList(pWalker
1dfd7 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  , pExpr->x.pList
1dfd8 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1dfd9 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  bort;.    }.  }.
1dfda 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52    return rc & WR
1dfdb 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  C_Abort;.}../*.*
1dfdc 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61  * Call sqlite3Wa
1dfdd 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65  lkExpr() for eve
1dfde 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ry expression in
1dfdf 20 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69 6c   list p or until
1dfe0 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65 71  .** an abort req
1dfe1 75 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a 2f  uest is seen..*/
1dfe2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1dfe3 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  int sqlite3WalkE
1dfe4 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20 2a  xprList(Walker *
1dfe5 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69 73  pWalker, ExprLis
1dfe6 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 2c 20  t *p){.  int i, 
1dfe7 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75  rc = WRC_Continu
1dfe8 65 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  e;.  struct Expr
1dfe9 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1dfea 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
1dfeb 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72 2c   for(i=p->nExpr,
1dfec 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30   pItem=p->a; i>0
1dfed 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i--, pItem++){
1dfee 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1dfef 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b  e3WalkExpr(pWalk
1dff0 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  er, pItem->pExpr
1dff1 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1dff2 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  bort;.    }.  }.
1dff3 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52    return rc & WR
1dff4 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f  C_Continue;.}../
1dff5 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6c 6c 20 65 78  *.** Walk all ex
1dff6 70 72 65 73 73 69 6f 6e 73 20 61 73 73 6f 63 69  pressions associ
1dff7 61 74 65 64 20 77 69 74 68 20 53 45 4c 45 43 54  ated with SELECT
1dff8 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20 20 44   statement p.  D
1dff9 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 6f 6b 65 20  o.** not invoke 
1dffa 74 68 65 20 53 45 4c 45 43 54 20 63 61 6c 6c 62  the SELECT callb
1dffb 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20 64 6f  ack on p, but do
1dffc 20 28 6f 66 20 63 6f 75 72 73 65 29 20 69 6e 76   (of course) inv
1dffd 6f 6b 65 0a 2a 2a 20 61 6e 79 20 65 78 70 72 20  oke.** any expr 
1dffe 63 61 6c 6c 62 61 63 6b 73 20 61 6e 64 20 53 45  callbacks and SE
1dfff 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 73 20 74  LECT callbacks t
1e000 68 61 74 20 63 6f 6d 65 20 66 72 6f 6d 20 73 75  hat come from su
1e001 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 52 65 74  bqueries..** Ret
1e002 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 6f 72  urn WRC_Abort or
1e003 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a   WRC_Continue..*
1e004 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1e005 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b   int sqlite3Walk
1e006 53 65 6c 65 63 74 45 78 70 72 28 57 61 6c 6b 65  SelectExpr(Walke
1e007 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
1e008 63 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 73 71  ct *p){.  if( sq
1e009 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
1e00a 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 45  t(pWalker, p->pE
1e00b 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57  List) ) return W
1e00c 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20  RC_Abort;.  if( 
1e00d 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1e00e 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 57 68 65  pWalker, p->pWhe
1e00f 72 65 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  re) ) return WRC
1e010 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71  _Abort;.  if( sq
1e011 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
1e012 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 47  t(pWalker, p->pG
1e013 72 6f 75 70 42 79 29 20 29 20 72 65 74 75 72 6e  roupBy) ) return
1e014 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66   WRC_Abort;.  if
1e015 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70  ( sqlite3WalkExp
1e016 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 48  r(pWalker, p->pH
1e017 61 76 69 6e 67 29 20 29 20 72 65 74 75 72 6e 20  aving) ) return 
1e018 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28  WRC_Abort;.  if(
1e019 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
1e01a 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d  List(pWalker, p-
1e01b 3e 70 4f 72 64 65 72 42 79 29 20 29 20 72 65 74  >pOrderBy) ) ret
1e01c 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1e01d 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1e01e 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d  Expr(pWalker, p-
1e01f 3e 70 4c 69 6d 69 74 29 20 29 20 72 65 74 75 72  >pLimit) ) retur
1e020 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69  n WRC_Abort;.  i
1e021 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  f( sqlite3WalkEx
1e022 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70  pr(pWalker, p->p
1e023 4f 66 66 73 65 74 29 20 29 20 72 65 74 75 72 6e  Offset) ) return
1e024 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 72 65   WRC_Abort;.  re
1e025 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
1e026 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b  e;.}../*.** Walk
1e027 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65 73   the parse trees
1e028 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1e029 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65 73 20   all subqueries 
1e02a 69 6e 20 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63  in the.** FROM c
1e02b 6c 61 75 73 65 20 6f 66 20 53 45 4c 45 43 54 20  lause of SELECT 
1e02c 73 74 61 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f  statement p.  Do
1e02d 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20   not invoke the 
1e02e 73 65 6c 65 63 74 0a 2a 2a 20 63 61 6c 6c 62 61  select.** callba
1e02f 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20  ck on p, but do 
1e030 69 6e 76 6f 6b 65 20 69 74 20 6f 6e 20 65 61 63  invoke it on eac
1e031 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 73 75  h FROM clause su
1e032 62 71 75 65 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e  bquery.** and on
1e033 20 61 6e 79 20 73 75 62 71 75 65 72 69 65 73 20   any subqueries 
1e034 66 75 72 74 68 65 72 20 64 6f 77 6e 20 69 6e 20  further down in 
1e035 74 68 65 20 74 72 65 65 2e 20 20 52 65 74 75 72  the tree.  Retur
1e036 6e 20 0a 2a 2a 20 57 52 43 5f 41 62 6f 72 74 20  n .** WRC_Abort 
1e037 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  or WRC_Continue;
1e038 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1e039 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
1e03a 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 61 6c  lkSelectFrom(Wal
1e03b 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65  ker *pWalker, Se
1e03c 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c  lect *p){.  SrcL
1e03d 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74  ist *pSrc;.  int
1e03e 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63   i;.  struct Src
1e03f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1e040 3b 0a 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70  ;..  pSrc = p->p
1e041 53 72 63 3b 0a 20 20 69 66 28 20 70 53 72 63 20  Src;.  if( pSrc 
1e042 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 72  ){.    for(i=pSr
1e043 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70  c->nSrc, pItem=p
1e044 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d  Src->a; i>0; i--
1e045 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
1e046 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1e047 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c  kSelect(pWalker,
1e048 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29   pItem->pSelect)
1e049 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
1e04a 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1e04b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1e04c 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e    return WRC_Con
1e04d 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a 2a  tinue;.} ../*.**
1e04e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c   Call sqlite3Wal
1e04f 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72  kExpr() for ever
1e050 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  y expression in 
1e051 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  Select statement
1e052 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71   p..** Invoke sq
1e053 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
1e054 29 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73  ) for subqueries
1e055 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
1e056 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74 68  use and.** on th
1e057 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  e compound selec
1e058 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72 69  t chain, p->pPri
1e059 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  or..**.** Return
1e05a 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75 6e   WRC_Continue un
1e05b 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64 69  der normal condi
1e05c 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20 57  tions.  Return W
1e05d 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20 74  RC_Abort if.** t
1e05e 68 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72 74  here is an abort
1e05f 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a 20   request..**.** 
1e060 49 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64 6f  If the Walker do
1e061 65 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 78  es not have an x
1e062 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 29  SelectCallback()
1e063 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
1e064 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70  ne.** is a no-op
1e065 20 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f 43   returning WRC_C
1e066 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49  ontinue..*/.SQLI
1e067 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1e068 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
1e069 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
1e06a 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
1e06b 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 3d  int rc;.  if( p=
1e06c 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e 78  =0 || pWalker->x
1e06d 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d 3d  SelectCallback==
1e06e 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43  0 ) return WRC_C
1e06f 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20  ontinue;.  rc = 
1e070 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  WRC_Continue;.  
1e071 77 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20 20  while( p  ){.   
1e072 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78   rc = pWalker->x
1e073 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 70  SelectCallback(p
1e074 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20 20  Walker, p);.    
1e075 69 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a  if( rc ) break;.
1e076 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
1e077 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 57  alkSelectExpr(pW
1e078 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75  alker, p) ) retu
1e079 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1e07a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1e07b 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61 6c  kSelectFrom(pWal
1e07c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 6e  ker, p) ) return
1e07d 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
1e07e 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20  p = p->pPrior;. 
1e07f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26   }.  return rc &
1e080 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f   WRC_Abort;.}../
1e081 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
1e082 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 2a  nd of walker.c *
1e083 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e084 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e085 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1e086 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1e087 65 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c 76  egin file resolv
1e088 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
1e089 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e08a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1e08b 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74  *.** 2008 August
1e08c 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   18.**.** The au
1e08d 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1e08e 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1e08f 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1e090 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
1e091 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1e092 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
1e093 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
1e094 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
1e095 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
1e096 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
1e097 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
1e098 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
1e099 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
1e09a 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
1e09b 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
1e09c 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
1e09d 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
1e09e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e09f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e0a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e0a2 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
1e0a3 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74  le contains rout
1e0a4 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61  ines used for wa
1e0a5 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72  lking the parser
1e0a6 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 73   tree and.** res
1e0a7 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 66  olve all identif
1e0a8 69 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 74  iers by associat
1e0a9 69 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 20  ing them with a 
1e0aa 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 61  particular.** ta
1e0ab 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a  ble and column..
1e0ac 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 65 73 6f 6c  **.** $Id: resol
1e0ad 76 65 2e 63 2c 76 20 31 2e 32 32 20 32 30 30 39  ve.c,v 1.22 2009
1e0ae 2f 30 35 2f 30 35 20 31 35 3a 34 36 3a 34 33 20  /05/05 15:46:43 
1e0af 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
1e0b0 0a 2a 2a 20 54 75 72 6e 20 74 68 65 20 70 45 78  .** Turn the pEx
1e0b1 70 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  pr expression in
1e0b2 74 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20  to an alias for 
1e0b3 74 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75  the iCol-th colu
1e0b4 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73  mn of the.** res
1e0b5 75 6c 74 20 73 65 74 20 69 6e 20 70 45 4c 69 73  ult set in pELis
1e0b6 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
1e0b7 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d  result set colum
1e0b8 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 20 63 6f  n is a simple co
1e0b9 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 2c 20  lumn reference, 
1e0ba 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
1e0bb 65 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20 65 78  e.** makes an ex
1e0bc 61 63 74 20 63 6f 70 79 2e 20 20 42 75 74 20 66  act copy.  But f
1e0bd 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e  or any other kin
1e0be 64 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 2c  d of expression,
1e0bf 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
1e0c0 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20   make a copy of 
1e0c1 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63  the result set c
1e0c2 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 61 72 67  olumn as the arg
1e0c3 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20  ument to the.** 
1e0c4 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 2e 20  TK_AS operator. 
1e0c5 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61   The TK_AS opera
1e0c6 74 6f 72 20 63 61 75 73 65 73 20 74 68 65 20 65  tor causes the e
1e0c7 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 0a  xpression to be.
1e0c8 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6a 75 73  ** evaluated jus
1e0c9 74 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 6e 20  t once and then 
1e0ca 72 65 75 73 65 64 20 66 6f 72 20 65 61 63 68 20  reused for each 
1e0cb 61 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  alias..**.** The
1e0cc 20 72 65 61 73 6f 6e 20 66 6f 72 20 73 75 70 70   reason for supp
1e0cd 72 65 73 73 69 6e 67 20 74 68 65 20 54 4b 5f 41  ressing the TK_A
1e0ce 53 20 74 65 72 6d 20 77 68 65 6e 20 74 68 65 20  S term when the 
1e0cf 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
1e0d0 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e  simple.** column
1e0d1 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 73 6f   reference is so
1e0d2 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e   that the column
1e0d3 20 72 65 66 65 72 65 6e 63 65 20 77 69 6c 6c 20   reference will 
1e0d4 62 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 61 73  be recognized as
1e0d5 0a 2a 2a 20 75 73 61 62 6c 65 20 62 79 20 69 6e  .** usable by in
1e0d6 64 69 63 65 73 20 77 69 74 68 69 6e 20 74 68 65  dices within the
1e0d7 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72   WHERE clause pr
1e0d8 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 2e 20  ocessing logic. 
1e0d9 0a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20 54 68  .**.** Hack:  Th
1e0da 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72  e TK_AS operator
1e0db 20 69 73 20 69 6e 68 69 62 69 74 65 64 20 69 66   is inhibited if
1e0dc 20 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27 2e 20   zType[0]=='G'. 
1e0dd 20 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74   This means.** t
1e0de 68 61 74 20 69 6e 20 61 20 47 52 4f 55 50 20 42  hat in a GROUP B
1e0df 59 20 63 6c 61 75 73 65 2c 20 74 68 65 20 65 78  Y clause, the ex
1e0e0 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c  pression is eval
1e0e1 75 61 74 65 64 20 74 77 69 63 65 2e 20 20 48 65  uated twice.  He
1e0e2 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  nce:.**.**     S
1e0e3 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 35  ELECT random()%5
1e0e4 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20   AS x, count(*) 
1e0e5 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 42  FROM tab GROUP B
1e0e6 59 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65 71 75  Y x.**.** Is equ
1e0e7 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a  ivalent to:.**.*
1e0e8 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e  *     SELECT ran
1e0e9 64 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f  dom()%5 AS x, co
1e0ea 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20  unt(*) FROM tab 
1e0eb 47 52 4f 55 50 20 42 59 20 72 61 6e 64 6f 6d 28  GROUP BY random(
1e0ec 29 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  )%5.**.** The re
1e0ed 73 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d 28 29  sult of random()
1e0ee 25 35 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20  %5 in the GROUP 
1e0ef 42 59 20 63 6c 61 75 73 65 20 69 73 20 70 72 6f  BY clause is pro
1e0f0 62 61 62 6c 79 20 64 69 66 66 65 72 65 6e 74 0a  bably different.
1e0f1 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75  ** from the resu
1e0f2 6c 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  lt in the result
1e0f3 2d 73 65 74 2e 20 20 57 65 20 6d 69 67 68 74 20  -set.  We might 
1e0f4 66 69 78 20 74 68 69 73 20 73 6f 6d 65 64 61 79  fix this someday
1e0f5 2e 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20 61 67  .  Or.** then ag
1e0f6 61 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 6e 6f  ain, we might no
1e0f7 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  t....*/.static v
1e0f8 6f 69 64 20 72 65 73 6f 6c 76 65 41 6c 69 61 73  oid resolveAlias
1e0f9 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1e0fa 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  e,         /* Pa
1e0fb 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
1e0fc 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
1e0fd 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 72  ist,      /* A r
1e0fe 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69  esult set */.  i
1e0ff 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20  nt iCol,        
1e100 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c 75 6d        /* A colum
1e101 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  n in the result 
1e102 73 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73 74 2d  set.  0..pEList-
1e103 3e 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20 45 78  >nExpr-1 */.  Ex
1e104 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20  pr *pExpr,      
1e105 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 6f 72       /* Transfor
1e106 6d 20 74 68 69 73 20 69 6e 74 6f 20 61 6e 20 61  m this into an a
1e107 6c 69 61 73 20 74 6f 20 74 68 65 20 72 65 73 75  lias to the resu
1e108 6c 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f 6e 73  lt set */.  cons
1e109 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20  t char *zType   
1e10a 20 20 20 2f 2a 20 22 47 52 4f 55 50 22 20 6f 72     /* "GROUP" or
1e10b 20 22 4f 52 44 45 52 22 20 6f 72 20 22 22 20 2a   "ORDER" or "" *
1e10c 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4f 72  /.){.  Expr *pOr
1e10d 69 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ig;           /*
1e10e 20 54 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c   The iCol-th col
1e10f 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c  umn of the resul
1e110 74 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 72 20  t set */.  Expr 
1e111 2a 70 44 75 70 3b 20 20 20 20 20 20 20 20 20 20  *pDup;          
1e112 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 4f 72    /* Copy of pOr
1e113 69 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ig */.  sqlite3 
1e114 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f  *db;           /
1e115 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
1e116 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  onnection */..  
1e117 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20  assert( iCol>=0 
1e118 26 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74 2d 3e  && iCol<pEList->
1e119 6e 45 78 70 72 20 29 3b 0a 20 20 70 4f 72 69 67  nExpr );.  pOrig
1e11a 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f   = pEList->a[iCo
1e11b 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65  l].pExpr;.  asse
1e11c 72 74 28 20 70 4f 72 69 67 21 3d 30 20 29 3b 0a  rt( pOrig!=0 );.
1e11d 20 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 2d    assert( pOrig-
1e11e 3e 66 6c 61 67 73 20 26 20 45 50 5f 52 65 73 6f  >flags & EP_Reso
1e11f 6c 76 65 64 20 29 3b 0a 20 20 64 62 20 3d 20 70  lved );.  db = p
1e120 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 44 75  Parse->db;.  pDu
1e121 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
1e122 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29  up(db, pOrig, 0)
1e123 3b 0a 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20  ;.  if( pDup==0 
1e124 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
1e125 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c  te3TokenCopy(db,
1e126 20 26 70 44 75 70 2d 3e 74 6f 6b 65 6e 2c 20 26   &pDup->token, &
1e127 70 4f 72 69 67 2d 3e 74 6f 6b 65 6e 29 3b 0a 20  pOrig->token);. 
1e128 20 69 66 28 20 70 44 75 70 2d 3e 6f 70 21 3d 54   if( pDup->op!=T
1e129 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54 79 70  K_COLUMN && zTyp
1e12a 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20 20 20  e[0]!='G' ){.   
1e12b 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 50   pDup = sqlite3P
1e12c 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
1e12d 41 53 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b  AS, pDup, 0, 0);
1e12e 0a 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30  .    if( pDup==0
1e12f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69   ) return;.    i
1e130 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f  f( pEList->a[iCo
1e131 6c 5d 2e 69 41 6c 69 61 73 3d 3d 30 20 29 7b 0a  l].iAlias==0 ){.
1e132 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b        pEList->a[
1e133 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 20 3d 20 28  iCol].iAlias = (
1e134 75 31 36 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e  u16)(++pParse->n
1e135 41 6c 69 61 73 29 3b 0a 20 20 20 20 7d 0a 20 20  Alias);.    }.  
1e136 20 20 70 44 75 70 2d 3e 69 54 61 62 6c 65 20 3d    pDup->iTable =
1e137 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d   pEList->a[iCol]
1e138 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 69  .iAlias;.  }.  i
1e139 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  f( pExpr->flags 
1e13a 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  & EP_ExpCollate 
1e13b 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e 70 43 6f  ){.    pDup->pCo
1e13c 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c  ll = pExpr->pCol
1e13d 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e 66 6c 61  l;.    pDup->fla
1e13e 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c  gs |= EP_ExpColl
1e13f 61 74 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ate;.  }.  sqlit
1e140 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 2c 20  e3ExprClear(db, 
1e141 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d 63 70 79  pExpr);.  memcpy
1e142 28 70 45 78 70 72 2c 20 70 44 75 70 2c 20 73 69  (pExpr, pDup, si
1e143 7a 65 6f 66 28 2a 70 45 78 70 72 29 29 3b 0a 20  zeof(*pExpr));. 
1e144 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1e145 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a  b, pDup);.}../*.
1e146 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d  ** Given the nam
1e147 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66  e of a column of
1e148 20 74 68 65 20 66 6f 72 6d 20 58 2e 59 2e 5a 20   the form X.Y.Z 
1e149 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a  or Y.Z or just Z
1e14a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74 68 61  , look up.** tha
1e14b 74 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 65  t name in the se
1e14c 74 20 6f 66 20 73 6f 75 72 63 65 20 74 61 62 6c  t of source tabl
1e14d 65 73 20 69 6e 20 70 53 72 63 4c 69 73 74 20 61  es in pSrcList a
1e14e 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 45 78 70  nd make the pExp
1e14f 72 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  r .** expression
1e150 20 6e 6f 64 65 20 72 65 66 65 72 20 62 61 63 6b   node refer back
1e151 20 74 6f 20 74 68 61 74 20 73 6f 75 72 63 65 20   to that source 
1e152 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 66 6f 6c  column.  The fol
1e153 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 0a 2a  lowing changes.*
1e154 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 45  * are made to pE
1e155 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45  xpr:.**.**    pE
1e156 78 70 72 2d 3e 69 44 62 20 20 20 20 20 20 20 20  xpr->iDb        
1e157 20 20 20 53 65 74 20 74 68 65 20 69 6e 64 65 78     Set the index
1e158 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 6f 66   in db->aDb[] of
1e159 20 74 68 65 20 64 61 74 61 62 61 73 65 20 58 0a   the database X.
1e15a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1e15b 20 20 20 20 20 20 20 20 20 20 20 28 65 76 65 6e             (even
1e15c 20 69 66 20 58 20 69 73 20 69 6d 70 6c 69 65 64   if X is implied
1e15d 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e  )..**    pExpr->
1e15e 69 54 61 62 6c 65 20 20 20 20 20 20 20 20 53 65  iTable        Se
1e15f 74 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72 20  t to the cursor 
1e160 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74  number for the t
1e161 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  able obtained.**
1e162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e163 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 70 53           from pS
1e164 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 20 70 45  rcList..**    pE
1e165 78 70 72 2d 3e 70 54 61 62 20 20 20 20 20 20 20  xpr->pTab       
1e166 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 68 65     Points to the
1e167 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
1e168 20 6f 66 20 58 2e 59 20 28 65 76 65 6e 20 69 66   of X.Y (even if
1e169 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1e16a 20 20 20 20 20 20 20 20 20 20 20 20 58 20 61 6e              X an
1e16b 64 2f 6f 72 20 59 20 61 72 65 20 69 6d 70 6c 69  d/or Y are impli
1e16c 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45 78 70 72  ed.).**    pExpr
1e16d 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20  ->iColumn       
1e16e 53 65 74 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d  Set to the colum
1e16f 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20  n number within 
1e170 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20  the table..**   
1e171 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20   pExpr->op      
1e172 20 20 20 20 20 20 53 65 74 20 74 6f 20 54 4b 5f        Set to TK_
1e173 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45  COLUMN..**    pE
1e174 78 70 72 2d 3e 70 4c 65 66 74 20 20 20 20 20 20  xpr->pLeft      
1e175 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f     Any expressio
1e176 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f  n this points to
1e177 20 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a 20 20   is deleted.**  
1e178 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20    pExpr->pRight 
1e179 20 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 65         Any expre
1e17a 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74  ssion this point
1e17b 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 2e  s to is deleted.
1e17c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 44 62 54 6f  .**.** The pDbTo
1e17d 6b 65 6e 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ken is the name 
1e17e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1e17f 28 74 68 65 20 22 58 22 29 2e 20 20 54 68 69 73  (the "X").  This
1e180 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a 2a   value may be.**
1e181 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68   NULL meaning th
1e182 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 68  at name is of th
1e183 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e  e form Y.Z or Z.
1e184 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 20    Any available 
1e185 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20  database.** can 
1e186 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 70 54  be used.  The pT
1e187 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20 74 68 65  ableToken is the
1e188 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
1e189 6c 65 20 28 74 68 65 20 22 59 22 29 2e 20 20 54  le (the "Y").  T
1e18a 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20 63 61 6e  his.** value can
1e18b 20 62 65 20 4e 55 4c 4c 20 69 66 20 70 44 62 54   be NULL if pDbT
1e18c 6f 6b 65 6e 20 69 73 20 61 6c 73 6f 20 4e 55 4c  oken is also NUL
1e18d 4c 2e 20 20 49 66 20 70 54 61 62 6c 65 54 6f 6b  L.  If pTableTok
1e18e 65 6e 20 69 73 20 4e 55 4c 4c 20 69 74 0a 2a 2a  en is NULL it.**
1e18f 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
1e190 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e 61 6d 65  form of the name
1e191 20 69 73 20 5a 20 61 6e 64 20 74 68 61 74 20 63   is Z and that c
1e192 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61 6e 79 20  olumns from any 
1e193 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20 62 65 20  table.** can be 
1e194 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  used..**.** If t
1e195 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f 74 20 62  he name cannot b
1e196 65 20 72 65 73 6f 6c 76 65 64 20 75 6e 61 6d 62  e resolved unamb
1e197 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61 76 65 20  iguously, leave 
1e198 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1e199 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 20 61 6e  .** in pParse an
1e19a 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  d return non-zer
1e19b 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 72 6f 20  o.  Return zero 
1e19c 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73  on success..*/.s
1e19d 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 70  tatic int lookup
1e19e 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70  Name(.  Parse *p
1e19f 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
1e1a0 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
1e1a1 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ext */.  Token *
1e1a2 70 44 62 54 6f 6b 65 6e 2c 20 20 20 20 20 2f 2a  pDbToken,     /*
1e1a3 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
1e1a4 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
1e1a5 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c 20   table, or NULL 
1e1a6 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62  */.  Token *pTab
1e1a7 6c 65 54 6f 6b 65 6e 2c 20 20 2f 2a 20 4e 61 6d  leToken,  /* Nam
1e1a8 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74 61  e of table conta
1e1a9 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f 72  ining column, or
1e1aa 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e   NULL */.  Token
1e1ab 20 2a 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 2c 20   *pColumnToken, 
1e1ac 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
1e1ad 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d 65  olumn. */.  Name
1e1ae 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20  Context *pNC,   
1e1af 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e   /* The name con
1e1b0 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 73  text used to res
1e1b1 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f  olve the name */
1e1b2 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20  .  Expr *pExpr  
1e1b3 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20          /* Make 
1e1b4 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20 70  this EXPR node p
1e1b5 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c 65  oint to the sele
1e1b6 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 29  cted column */.)
1e1b7 7b 0a 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  {.  char *zDb = 
1e1b8 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  0;       /* Name
1e1b9 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1e1ba 2e 20 20 54 68 65 20 22 58 22 20 69 6e 20 58 2e  .  The "X" in X.
1e1bb 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  Y.Z */.  char *z
1e1bc 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  Tab = 0;      /*
1e1bd 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   Name of the tab
1e1be 6c 65 2e 20 20 54 68 65 20 22 59 22 20 69 6e 20  le.  The "Y" in 
1e1bf 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 2a 2f 0a  X.Y.Z or Y.Z */.
1e1c0 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 30    char *zCol = 0
1e1c1 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ;      /* Name o
1e1c2 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 20 54  f the column.  T
1e1c3 68 65 20 22 5a 22 20 2a 2f 0a 20 20 69 6e 74 20  he "Z" */.  int 
1e1c4 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  i, j;           
1e1c5 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
1e1c6 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 20 3d  s */.  int cnt =
1e1c7 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1e1c8 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1e1c9 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 63  er of matching c
1e1ca 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a 20  olumn names */. 
1e1cb 20 69 6e 74 20 63 6e 74 54 61 62 20 3d 20 30 3b   int cntTab = 0;
1e1cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e1cd 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1e1ce 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 20 6e  matching table n
1e1cf 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ames */.  sqlite
1e1d0 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1e1d1 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  db;         /* T
1e1d2 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1e1d3 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75  ection */.  stru
1e1d4 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
1e1d5 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 20 2f 2a  *pItem;       /*
1e1d6 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 69 6e 67   Use for looping
1e1d7 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74 20 69   over pSrcList i
1e1d8 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  tems */.  struct
1e1d9 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
1e1da 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f 2a 20 54  Match = 0;  /* T
1e1db 68 65 20 6d 61 74 63 68 69 6e 67 20 70 53 72 63  he matching pSrc
1e1dc 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a 20 20 4e  List item */.  N
1e1dd 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 54 6f 70  ameContext *pTop
1e1de 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 20 20 20  NC = pNC;       
1e1df 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d 65 63 6f   /* First nameco
1e1e0 6e 74 65 78 74 20 69 6e 20 74 68 65 20 6c 69 73  ntext in the lis
1e1e1 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70  t */.  Schema *p
1e1e2 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 20 20 20  Schema = 0;     
1e1e3 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65           /* Sche
1e1e4 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ma of the expres
1e1e5 73 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72  sion */..  asser
1e1e6 74 28 20 70 4e 43 20 29 3b 20 2f 2a 20 74 68 65  t( pNC ); /* the
1e1e7 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 63 61   name context ca
1e1e8 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f  nnot be NULL. */
1e1e9 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 75  .  assert( pColu
1e1ea 6d 6e 54 6f 6b 65 6e 20 26 26 20 70 43 6f 6c 75  mnToken && pColu
1e1eb 6d 6e 54 6f 6b 65 6e 2d 3e 7a 20 29 3b 20 2f 2a  mnToken->z ); /*
1e1ec 20 54 68 65 20 5a 20 69 6e 20 58 2e 59 2e 5a 20   The Z in X.Y.Z 
1e1ed 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 20 2a  cannot be NULL *
1e1ee 2f 0a 0a 20 20 2f 2a 20 44 65 71 75 6f 74 65 20  /..  /* Dequote 
1e1ef 61 6e 64 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  and zero-termina
1e1f0 74 65 20 74 68 65 20 6e 61 6d 65 73 20 2a 2f 0a  te the names */.
1e1f1 20 20 7a 44 62 20 3d 20 73 71 6c 69 74 65 33 4e    zDb = sqlite3N
1e1f2 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
1e1f3 20 70 44 62 54 6f 6b 65 6e 29 3b 0a 20 20 7a 54   pDbToken);.  zT
1e1f4 61 62 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  ab = sqlite3Name
1e1f5 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54  FromToken(db, pT
1e1f6 61 62 6c 65 54 6f 6b 65 6e 29 3b 0a 20 20 7a 43  ableToken);.  zC
1e1f7 6f 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  ol = sqlite3Name
1e1f8 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43  FromToken(db, pC
1e1f9 6f 6c 75 6d 6e 54 6f 6b 65 6e 29 3b 0a 20 20 69  olumnToken);.  i
1e1fa 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
1e1fb 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
1e1fc 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3b 0a  lookupname_end;.
1e1fd 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61    }..  /* Initia
1e1fe 6c 69 7a 65 20 74 68 65 20 6e 6f 64 65 20 74 6f  lize the node to
1e1ff 20 6e 6f 2d 6d 61 74 63 68 20 2a 2f 0a 20 20 70   no-match */.  p
1e200 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 2d  Expr->iTable = -
1e201 31 3b 0a 20 20 70 45 78 70 72 2d 3e 70 54 61 62  1;.  pExpr->pTab
1e202 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 53 74 61 72   = 0;..  /* Star
1e203 74 20 61 74 20 74 68 65 20 69 6e 6e 65 72 2d 6d  t at the inner-m
1e204 6f 73 74 20 63 6f 6e 74 65 78 74 20 61 6e 64 20  ost context and 
1e205 6d 6f 76 65 20 6f 75 74 77 61 72 64 20 75 6e 74  move outward unt
1e206 69 6c 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f  il a match is fo
1e207 75 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  und */.  while( 
1e208 70 4e 43 20 26 26 20 63 6e 74 3d 3d 30 20 29 7b  pNC && cnt==0 ){
1e209 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70  .    ExprList *p
1e20a 45 4c 69 73 74 3b 0a 20 20 20 20 53 72 63 4c 69  EList;.    SrcLi
1e20b 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70  st *pSrcList = p
1e20c 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 0a 20  NC->pSrcList;.. 
1e20d 20 20 20 69 66 28 20 70 53 72 63 4c 69 73 74 20     if( pSrcList 
1e20e 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  ){.      for(i=0
1e20f 2c 20 70 49 74 65 6d 3d 70 53 72 63 4c 69 73 74  , pItem=pSrcList
1e210 2d 3e 61 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d  ->a; i<pSrcList-
1e211 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65  >nSrc; i++, pIte
1e212 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 54 61  m++){.        Ta
1e213 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20  ble *pTab;.     
1e214 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20     int iDb;.    
1e215 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c      Column *pCol
1e216 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 70 54 61  ;.  .        pTa
1e217 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b  b = pItem->pTab;
1e218 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1e219 20 70 54 61 62 21 3d 30 20 26 26 20 70 54 61 62   pTab!=0 && pTab
1e21a 2d 3e 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20  ->zName!=0 );.  
1e21b 20 20 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69        iDb = sqli
1e21c 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
1e21d 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
1e21e 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  ma);.        ass
1e21f 65 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e  ert( pTab->nCol>
1e220 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  0 );.        if(
1e221 20 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20   zTab ){.       
1e222 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41     if( pItem->zA
1e223 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20  lias ){.        
1e224 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61      char *zTabNa
1e225 6d 65 20 3d 20 70 49 74 65 6d 2d 3e 7a 41 6c 69  me = pItem->zAli
1e226 61 73 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  as;.            
1e227 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1e228 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61  mp(zTabName, zTa
1e229 62 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  b)!=0 ) continue
1e22a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
1e22b 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  e{.            c
1e22c 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20  har *zTabName = 
1e22d 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20  pTab->zName;.   
1e22e 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61           if( zTa
1e22f 62 4e 61 6d 65 3d 3d 30 20 7c 7c 20 73 71 6c 69  bName==0 || sqli
1e230 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61 62 4e  te3StrICmp(zTabN
1e231 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 29 20  ame, zTab)!=0 ) 
1e232 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
1e233 20 20 20 20 20 20 69 66 28 20 7a 44 62 21 3d 30        if( zDb!=0
1e234 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1e235 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  mp(db->aDb[iDb].
1e236 7a 4e 61 6d 65 2c 20 7a 44 62 29 21 3d 30 20 29  zName, zDb)!=0 )
1e237 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1e238 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
1e239 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e23a 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1e23b 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 63 6e        if( 0==(cn
1e23c 74 54 61 62 2b 2b 29 20 29 7b 0a 20 20 20 20 20  tTab++) ){.     
1e23d 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
1e23e 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72  le = pItem->iCur
1e23f 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70  sor;.          p
1e240 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61  Expr->pTab = pTa
1e241 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 63  b;.          pSc
1e242 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
1e243 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20  hema;.          
1e244 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a  pMatch = pItem;.
1e245 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e246 20 20 66 6f 72 28 6a 3d 30 2c 20 70 43 6f 6c 3d    for(j=0, pCol=
1e247 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54  pTab->aCol; j<pT
1e248 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70  ab->nCol; j++, p
1e249 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Col++){.        
1e24a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
1e24b 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  ICmp(pCol->zName
1e24c 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20  , zCol)==0 ){.  
1e24d 20 20 20 20 20 20 20 20 20 20 49 64 4c 69 73 74            IdList
1e24e 20 2a 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20   *pUsing;.      
1e24f 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
1e250 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
1e251 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e  iTable = pItem->
1e252 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20  iCursor;.       
1e253 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62       pExpr->pTab
1e254 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20   = pTab;.       
1e255 20 20 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49       pMatch = pI
1e256 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  tem;.           
1e257 20 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d   pSchema = pTab-
1e258 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20  >pSchema;.      
1e259 20 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74        /* Substit
1e25a 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 28 63  ute the rowid (c
1e25b 6f 6c 75 6d 6e 20 2d 31 29 20 66 6f 72 20 74 68  olumn -1) for th
1e25c 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  e INTEGER PRIMAR
1e25d 59 20 4b 45 59 20 2a 2f 0a 20 20 20 20 20 20 20  Y KEY */.       
1e25e 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
1e25f 75 6d 6e 20 3d 20 6a 3d 3d 70 54 61 62 2d 3e 69  umn = j==pTab->i
1e260 50 4b 65 79 20 3f 20 2d 31 20 3a 20 6a 3b 0a 20  PKey ? -1 : j;. 
1e261 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69             if( i
1e262 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d  <pSrcList->nSrc-
1e263 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  1 ){.           
1e264 20 20 20 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e     if( pItem[1].
1e265 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41  jointype & JT_NA
1e266 54 55 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 20  TURAL ){.       
1e267 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74           /* If t
1e268 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 72  his match occurr
1e269 65 64 20 69 6e 20 74 68 65 20 6c 65 66 74 20 74  ed in the left t
1e26a 61 62 6c 65 20 6f 66 20 61 20 6e 61 74 75 72 61  able of a natura
1e26b 6c 20 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20  l join,.        
1e26c 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20          ** then 
1e26d 73 6b 69 70 20 74 68 65 20 72 69 67 68 74 20 74  skip the right t
1e26e 61 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 20  able to avoid a 
1e26f 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20  duplicate match 
1e270 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
1e271 20 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20     pItem++;.    
1e272 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b              i++;
1e273 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
1e274 65 6c 73 65 20 69 66 28 20 28 70 55 73 69 6e 67  else if( (pUsing
1e275 20 3d 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69   = pItem[1].pUsi
1e276 6e 67 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ng)!=0 ){.      
1e277 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
1e278 74 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72  this match occur
1e279 73 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68  s on a column th
1e27a 61 74 20 69 73 20 69 6e 20 74 68 65 20 55 53 49  at is in the USI
1e27b 4e 47 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20  NG clause.      
1e27c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20            ** of 
1e27d 61 20 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65  a join, skip the
1e27e 20 73 65 61 72 63 68 20 6f 66 20 74 68 65 20 72   search of the r
1e27f 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68  ight table of th
1e280 65 20 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20 20  e join.         
1e281 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f         ** to avo
1e282 69 64 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d  id a duplicate m
1e283 61 74 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20  atch there. */. 
1e284 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
1e285 6e 74 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  nt k;.          
1e286 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b        for(k=0; k
1e287 3c 70 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b  <pUsing->nId; k+
1e288 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
1e289 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1e28a 33 53 74 72 49 43 6d 70 28 70 55 73 69 6e 67 2d  3StrICmp(pUsing-
1e28b 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f  >a[k].zName, zCo
1e28c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
1e28d 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74               pIt
1e28e 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  em++;.          
1e28f 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20            i++;. 
1e290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e291 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1e292 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1e293 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
1e294 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
1e295 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1e296 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
1e297 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1e298 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1e299 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51     }..#ifndef SQ
1e29a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1e29b 52 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68  R.    /* If we h
1e29c 61 76 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20  ave not already 
1e29d 72 65 73 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d  resolved the nam
1e29e 65 2c 20 74 68 65 6e 20 6d 61 79 62 65 20 0a 20  e, then maybe . 
1e29f 20 20 20 2a 2a 20 69 74 20 69 73 20 61 20 6e 65     ** it is a ne
1e2a0 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69  w.* or old.* tri
1e2a1 67 67 65 72 20 61 72 67 75 6d 65 6e 74 20 72 65  gger argument re
1e2a2 66 65 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20  ference.    */. 
1e2a3 20 20 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 26     if( zDb==0 &&
1e2a4 20 7a 54 61 62 21 3d 30 20 26 26 20 63 6e 74 3d   zTab!=0 && cnt=
1e2a5 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 74 72  =0 && pParse->tr
1e2a6 69 67 53 74 61 63 6b 21 3d 30 20 29 7b 0a 20 20  igStack!=0 ){.  
1e2a7 20 20 20 20 54 72 69 67 67 65 72 53 74 61 63 6b      TriggerStack
1e2a8 20 2a 70 54 72 69 67 67 65 72 53 74 61 63 6b 20   *pTriggerStack 
1e2a9 3d 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74  = pParse->trigSt
1e2aa 61 63 6b 3b 0a 20 20 20 20 20 20 54 61 62 6c 65  ack;.      Table
1e2ab 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20   *pTab = 0;.    
1e2ac 20 20 75 33 32 20 2a 70 69 43 6f 6c 4d 61 73 6b    u32 *piColMask
1e2ad 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
1e2ae 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e  pTriggerStack->n
1e2af 65 77 49 64 78 20 21 3d 20 2d 31 20 26 26 20 73  ewIdx != -1 && s
1e2b0 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6e  qlite3StrICmp("n
1e2b1 65 77 22 2c 20 7a 54 61 62 29 20 3d 3d 20 30 20  ew", zTab) == 0 
1e2b2 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  ){.        pExpr
1e2b3 2d 3e 69 54 61 62 6c 65 20 3d 20 70 54 72 69 67  ->iTable = pTrig
1e2b4 67 65 72 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78  gerStack->newIdx
1e2b5 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1e2b6 28 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  ( pTriggerStack-
1e2b7 3e 70 54 61 62 20 29 3b 0a 20 20 20 20 20 20 20  >pTab );.       
1e2b8 20 70 54 61 62 20 3d 20 70 54 72 69 67 67 65 72   pTab = pTrigger
1e2b9 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 20  Stack->pTab;.   
1e2ba 20 20 20 20 20 70 69 43 6f 6c 4d 61 73 6b 20 3d       piColMask =
1e2bb 20 26 28 70 54 72 69 67 67 65 72 53 74 61 63 6b   &(pTriggerStack
1e2bc 2d 3e 6e 65 77 43 6f 6c 4d 61 73 6b 29 3b 0a 20  ->newColMask);. 
1e2bd 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
1e2be 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c  TriggerStack->ol
1e2bf 64 49 64 78 20 21 3d 20 2d 31 20 26 26 20 73 71  dIdx != -1 && sq
1e2c0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6f 6c  lite3StrICmp("ol
1e2c1 64 22 2c 20 7a 54 61 62 29 3d 3d 30 20 29 7b 0a  d", zTab)==0 ){.
1e2c2 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1e2c3 54 61 62 6c 65 20 3d 20 70 54 72 69 67 67 65 72  Table = pTrigger
1e2c4 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 3b 0a 20  Stack->oldIdx;. 
1e2c5 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1e2c6 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54  TriggerStack->pT
1e2c7 61 62 20 29 3b 0a 20 20 20 20 20 20 20 20 70 54  ab );.        pT
1e2c8 61 62 20 3d 20 70 54 72 69 67 67 65 72 53 74 61  ab = pTriggerSta
1e2c9 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20  ck->pTab;.      
1e2ca 20 20 70 69 43 6f 6c 4d 61 73 6b 20 3d 20 26 28    piColMask = &(
1e2cb 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f  pTriggerStack->o
1e2cc 6c 64 43 6f 6c 4d 61 73 6b 29 3b 0a 20 20 20 20  ldColMask);.    
1e2cd 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70    }..      if( p
1e2ce 54 61 62 20 29 7b 20 0a 20 20 20 20 20 20 20 20  Tab ){ .        
1e2cf 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20  int iCol;.      
1e2d0 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d    Column *pCol =
1e2d1 20 70 54 61 62 2d 3e 61 43 6f 6c 3b 0a 0a 20 20   pTab->aCol;..  
1e2d2 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20        pSchema = 
1e2d3 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20  pTab->pSchema;. 
1e2d4 20 20 20 20 20 20 20 63 6e 74 54 61 62 2b 2b 3b         cntTab++;
1e2d5 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 43 6f  .        for(iCo
1e2d6 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61 62  l=0; iCol < pTab
1e2d7 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 2c 20  ->nCol; iCol++, 
1e2d8 70 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20 20 20 20  pCol++) {.      
1e2d9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1e2da 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61  trICmp(pCol->zNa
1e2db 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a  me, zCol)==0 ){.
1e2dc 20 20 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b              cnt+
1e2dd 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  +;.            p
1e2de 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  Expr->iColumn = 
1e2df 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  iCol==pTab->iPKe
1e2e0 79 20 3f 20 2d 31 20 3a 20 69 43 6f 6c 3b 0a 20  y ? -1 : iCol;. 
1e2e1 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
1e2e2 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20  ->pTab = pTab;. 
1e2e3 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69             if( i
1e2e4 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  Col>=0 ){.      
1e2e5 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
1e2e6 28 20 69 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 20  ( iCol==31 );.  
1e2e7 20 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74              test
1e2e8 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 32 20 29  case( iCol==32 )
1e2e9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1e2ea 69 66 28 20 69 43 6f 6c 3e 3d 33 32 20 29 7b 0a  if( iCol>=32 ){.
1e2eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e2ec 2a 70 69 43 6f 6c 4d 61 73 6b 20 3d 20 30 78 66  *piColMask = 0xf
1e2ed 66 66 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20  fffffff;.       
1e2ee 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1e2ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70                *p
1e2f0 69 43 6f 6c 4d 61 73 6b 20 7c 3d 20 28 28 75 33  iColMask |= ((u3
1e2f1 32 29 31 29 3c 3c 69 43 6f 6c 3b 0a 20 20 20 20  2)1)<<iCol;.    
1e2f2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1e2f3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e2f4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e2f5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1e2f6 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1e2f7 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
1e2f8 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1e2f9 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20 20 20  TRIGGER) */..   
1e2fa 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72 68 61   /*.    ** Perha
1e2fb 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73 20 61  ps the name is a
1e2fc 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
1e2fd 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f 0a 20  e ROWID.    */. 
1e2fe 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26     if( cnt==0 &&
1e2ff 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20 73 71   cntTab==1 && sq
1e300 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 6f  lite3IsRowid(zCo
1e301 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 20  l) ){.      cnt 
1e302 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72  = 1;.      pExpr
1e303 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a  ->iColumn = -1;.
1e304 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66        pExpr->aff
1e305 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
1e306 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  FF_INTEGER;.    
1e307 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  }..    /*.    **
1e308 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 69 73   If the input is
1e309 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a 20 28   of the form Z (
1e30a 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59 2e 5a  not Y.Z or X.Y.Z
1e30b 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d 65 20  ) then the name 
1e30c 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 72  Z.    ** might r
1e30d 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73 75 6c  efer to an resul
1e30e 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20 54 68  t-set alias.  Th
1e30f 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72 20  is happens, for 
1e310 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a 20 20  example, when.  
1e311 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65 73 6f    ** we are reso
1e312 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 74  lving names in t
1e313 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1e314 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
1e315 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20 2a 2a   command:.    **
1e316 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  .    **     SELE
1e317 43 54 20 61 2b 62 20 41 53 20 78 20 46 52 4f 4d  CT a+b AS x FROM
1e318 20 74 61 62 6c 65 20 57 48 45 52 45 20 78 3c 31   table WHERE x<1
1e319 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  0;.    **.    **
1e31a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65 20 74   In cases like t
1e31b 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70 45 78  his, replace pEx
1e31c 70 72 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f  pr with a copy o
1e31d 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1e31e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66 6f 72   that.    ** for
1e31f 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ms the result se
1e320 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22 20 69  t entry ("a+b" i
1e321 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29 20 61  n the example) a
1e322 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69  nd return immedi
1e323 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 4e 6f  ately..    ** No
1e324 74 65 20 74 68 61 74 20 74 68 65 20 65 78 70 72  te that the expr
1e325 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72 65  ession in the re
1e326 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c 64 20  sult set should 
1e327 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
1e328 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76 65  n.    ** resolve
1e329 64 20 62 79 20 74 68 65 20 74 69 6d 65 20 74 68  d by the time th
1e32a 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69  e WHERE clause i
1e32b 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20  s resolved..    
1e32c 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d  */.    if( cnt==
1e32d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d 20 70  0 && (pEList = p
1e32e 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30 20 26  NC->pEList)!=0 &
1e32f 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20  & zTab==0 ){.   
1e330 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 45     for(j=0; j<pE
1e331 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b  List->nExpr; j++
1e332 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
1e333 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61  *zAs = pEList->a
1e334 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [j].zName;.     
1e335 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 26 26     if( zAs!=0 &&
1e336 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1e337 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b  zAs, zCol)==0 ){
1e338 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20  .          Expr 
1e339 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 20 20  *pOrig;.        
1e33a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1e33b 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78  >pLeft==0 && pEx
1e33c 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b  pr->pRight==0 );
1e33d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1e33e 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  t( pExpr->x.pLis
1e33f 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  t==0 );.        
1e340 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1e341 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b  >x.pSelect==0 );
1e342 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 69 67  .          pOrig
1e343 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e   = pEList->a[j].
1e344 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20  pExpr;.         
1e345 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77   if( !pNC->allow
1e346 41 67 67 20 26 26 20 45 78 70 72 48 61 73 50 72  Agg && ExprHasPr
1e347 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45 50  operty(pOrig, EP
1e348 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20  _Agg) ){.       
1e349 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1e34a 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69  rMsg(pParse, "mi
1e34b 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64 20  suse of aliased 
1e34c 61 67 67 72 65 67 61 74 65 20 25 73 22 2c 20 7a  aggregate %s", z
1e34d 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  As);.           
1e34e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1e34f 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20  b, zCol);.      
1e350 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
1e351 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1e352 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69        resolveAli
1e353 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73  as(pParse, pELis
1e354 74 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29  t, j, pExpr, "")
1e355 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20  ;.          cnt 
1e356 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70  = 1;.          p
1e357 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20  Match = 0;.     
1e358 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61       assert( zTa
1e359 62 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29  b==0 && zDb==0 )
1e35a 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
1e35b 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f   lookupname_end_
1e35c 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  2;.        }.   
1e35d 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20     } .    }..   
1e35e 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74   /* Advance to t
1e35f 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e  he next name con
1e360 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20  text.  The loop 
1e361 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 65  will exit when e
1e362 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 65 20  ither.    ** we 
1e363 68 61 76 65 20 61 20 6d 61 74 63 68 20 28 63 6e  have a match (cn
1e364 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65 20  t>0) or when we 
1e365 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20  run out of name 
1e366 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f  contexts..    */
1e367 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20  .    if( cnt==0 
1e368 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 20 70  ){.      pNC = p
1e369 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  NC->pNext;.    }
1e36a 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
1e36b 49 66 20 58 20 61 6e 64 20 59 20 61 72 65 20 4e  If X and Y are N
1e36c 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 6f  ULL (in other wo
1e36d 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 65 20  rds if only the 
1e36e 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73  column name Z is
1e36f 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 29 20  .  ** supplied) 
1e370 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66  and the value of
1e371 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 20 69   Z is enclosed i
1e372 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c  n double-quotes,
1e373 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20   then.  ** Z is 
1e374 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c  a string literal
1e375 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 6d   if it doesn't m
1e376 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20  atch any column 
1e377 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 74 0a  names.  In that.
1e378 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e 65    ** case, we ne
1e379 65 64 20 74 6f 20 72 65 74 75 72 6e 20 72 69 67  ed to return rig
1e37a 68 74 20 61 77 61 79 20 61 6e 64 20 6e 6f 74 20  ht away and not 
1e37b 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73  make any changes
1e37c 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a   to.  ** pExpr..
1e37d 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73    **.  ** Becaus
1e37e 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77  e no reference w
1e37f 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 72  as made to outer
1e380 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 70   contexts, the p
1e381 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69  NC->nRef.  ** fi
1e382 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68 61  elds are not cha
1e383 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74  nged in any cont
1e384 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ext..  */.  if( 
1e385 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d  cnt==0 && zTab==
1e386 30 20 26 26 20 45 78 70 72 48 61 73 50 72 6f 70  0 && ExprHasProp
1e387 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f 44 62  erty(pExpr,EP_Db
1e388 6c 51 75 6f 74 65 64 29 20 29 7b 0a 20 20 20 20  lQuoted) ){.    
1e389 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1e38a 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 70 45 78  , zCol);.    pEx
1e38b 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49  pr->op = TK_STRI
1e38c 4e 47 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70  NG;.    pExpr->p
1e38d 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  Tab = 0;.    ret
1e38e 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
1e38f 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61  .  ** cnt==0 mea
1e390 6e 73 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74  ns there was not
1e391 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d   match.  cnt>1 m
1e392 65 61 6e 73 20 74 68 65 72 65 20 77 65 72 65 20  eans there were 
1e393 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65  two or.  ** more
1e394 20 6d 61 74 63 68 65 73 2e 20 20 45 69 74 68 65   matches.  Eithe
1e395 72 20 77 61 79 2c 20 77 65 20 68 61 76 65 20 61  r way, we have a
1e396 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20  n error..  */.  
1e397 69 66 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20  if( cnt!=1 ){.  
1e398 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
1e399 72 72 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63  rr;.    zErr = c
1e39a 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68  nt==0 ? "no such
1e39b 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69   column" : "ambi
1e39c 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d  guous column nam
1e39d 65 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20  e";.    if( zDb 
1e39e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1e39f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1e3a0 20 22 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c   "%s: %s.%s.%s",
1e3a1 20 7a 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62   zErr, zDb, zTab
1e3a2 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c  , zCol);.    }el
1e3a3 73 65 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20  se if( zTab ){. 
1e3a4 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1e3a5 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73  rMsg(pParse, "%s
1e3a6 3a 20 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20  : %s.%s", zErr, 
1e3a7 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20  zTab, zCol);.   
1e3a8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
1e3a9 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1e3aa 61 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20  arse, "%s: %s", 
1e3ab 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20  zErr, zCol);.   
1e3ac 20 7d 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e   }.    pTopNC->n
1e3ad 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Err++;.  }..  /*
1e3ae 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f   If a column fro
1e3af 6d 20 61 20 74 61 62 6c 65 20 69 6e 20 70 53 72  m a table in pSr
1e3b0 63 4c 69 73 74 20 69 73 20 72 65 66 65 72 65 6e  cList is referen
1e3b1 63 65 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 64  ced, then record
1e3b2 0a 20 20 2a 2a 20 74 68 69 73 20 66 61 63 74 20  .  ** this fact 
1e3b3 69 6e 20 74 68 65 20 70 53 72 63 4c 69 73 74 2e  in the pSrcList.
1e3b4 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d  a[].colUsed bitm
1e3b5 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63  ask.  Column 0 c
1e3b6 61 75 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 30  auses.  ** bit 0
1e3b7 20 74 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 6c   to be set.  Col
1e3b8 75 6d 6e 20 31 20 73 65 74 73 20 62 69 74 20 31  umn 1 sets bit 1
1e3b9 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e  .  And so forth.
1e3ba 20 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f    If the.  ** co
1e3bb 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 67  lumn number is g
1e3bc 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
1e3bd 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69  number of bits i
1e3be 6e 20 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20  n the bitmask.  
1e3bf 2a 2a 20 74 68 65 6e 20 73 65 74 20 74 68 65 20  ** then set the 
1e3c0 68 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20 6f  high-order bit o
1e3c1 66 20 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20  f the bitmask.. 
1e3c2 20 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d   */.  if( pExpr-
1e3c3 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70  >iColumn>=0 && p
1e3c4 4d 61 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 20  Match!=0 ){.    
1e3c5 69 6e 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69  int n = pExpr->i
1e3c6 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74  Column;.    test
1e3c7 63 61 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29  case( n==BMS-1 )
1e3c8 3b 0a 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53  ;.    if( n>=BMS
1e3c9 20 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d   ){.      n = BM
1e3ca 53 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  S-1;.    }.    a
1e3cb 73 73 65 72 74 28 20 70 4d 61 74 63 68 2d 3e 69  ssert( pMatch->i
1e3cc 43 75 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69  Cursor==pExpr->i
1e3cd 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61  Table );.    pMa
1e3ce 74 63 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20  tch->colUsed |= 
1e3cf 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b  ((Bitmask)1)<<n;
1e3d0 0a 20 20 7d 0a 0a 6c 6f 6f 6b 75 70 6e 61 6d 65  .  }..lookupname
1e3d1 5f 65 6e 64 3a 0a 20 20 2f 2a 20 43 6c 65 61 6e  _end:.  /* Clean
1e3d2 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 0a 20   up and return. 
1e3d3 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46   */.  sqlite3DbF
1e3d4 72 65 65 28 64 62 2c 20 7a 44 62 29 3b 0a 20 20  ree(db, zDb);.  
1e3d5 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1e3d6 2c 20 7a 54 61 62 29 3b 0a 20 20 73 71 6c 69 74  , zTab);.  sqlit
1e3d7 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
1e3d8 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a   pExpr->pLeft);.
1e3d9 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d    pExpr->pLeft =
1e3da 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70   0;.  sqlite3Exp
1e3db 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70  rDelete(db, pExp
1e3dc 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 70 45  r->pRight);.  pE
1e3dd 78 70 72 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b  xpr->pRight = 0;
1e3de 0a 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54  .  pExpr->op = T
1e3df 4b 5f 43 4f 4c 55 4d 4e 3b 0a 6c 6f 6f 6b 75 70  K_COLUMN;.lookup
1e3e0 6e 61 6d 65 5f 65 6e 64 5f 32 3a 0a 20 20 73 71  name_end_2:.  sq
1e3e1 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1e3e2 7a 43 6f 6c 29 3b 0a 20 20 69 66 28 20 63 6e 74  zCol);.  if( cnt
1e3e3 3d 3d 31 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==1 ){.    asser
1e3e4 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20  t( pNC!=0 );.   
1e3e5 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64   sqlite3AuthRead
1e3e6 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
1e3e7 70 53 63 68 65 6d 61 2c 20 70 4e 43 2d 3e 70 53  pSchema, pNC->pS
1e3e8 72 63 4c 69 73 74 29 3b 0a 20 20 20 20 2f 2a 20  rcList);.    /* 
1e3e9 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 6e 52  Increment the nR
1e3ea 65 66 20 76 61 6c 75 65 20 6f 6e 20 61 6c 6c 20  ef value on all 
1e3eb 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 20 66 72  name contexts fr
1e3ec 6f 6d 20 54 6f 70 4e 43 20 75 70 20 74 6f 0a 20  om TopNC up to. 
1e3ed 20 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 20     ** the point 
1e3ee 77 68 65 72 65 20 74 68 65 20 6e 61 6d 65 20 6d  where the name m
1e3ef 61 74 63 68 65 64 2e 20 2a 2f 0a 20 20 20 20 66  atched. */.    f
1e3f0 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 61 73  or(;;){.      as
1e3f1 73 65 72 74 28 20 70 54 6f 70 4e 43 21 3d 30 20  sert( pTopNC!=0 
1e3f2 29 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 2d  );.      pTopNC-
1e3f3 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 69  >nRef++;.      i
1e3f4 66 28 20 70 54 6f 70 4e 43 3d 3d 70 4e 43 20 29  f( pTopNC==pNC )
1e3f5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70 54   break;.      pT
1e3f6 6f 70 4e 43 20 3d 20 70 54 6f 70 4e 43 2d 3e 70  opNC = pTopNC->p
1e3f7 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
1e3f8 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 65 6c  return 0;.  } el
1e3f9 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  se {.    return 
1e3fa 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
1e3fb 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1e3fc 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 73 71 6c  callback for sql
1e3fd 69 74 65 33 57 61 6c 6b 45 78 70 72 28 29 2e 0a  ite3WalkExpr()..
1e3fe 2a 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79  **.** Resolve sy
1e3ff 6d 62 6f 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74  mbolic names int
1e400 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72  o TK_COLUMN oper
1e401 61 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63 75  ators for the cu
1e402 72 72 65 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 6e  rrent.** node in
1e403 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1e404 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 30 20  tree.  Return 0 
1e405 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 68 65 20  to continue the 
1e406 73 65 61 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 74  search down.** t
1e407 68 65 20 74 72 65 65 20 6f 72 20 32 20 74 6f 20  he tree or 2 to 
1e408 61 62 6f 72 74 20 74 68 65 20 74 72 65 65 20 77  abort the tree w
1e409 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  alk..**.** This 
1e40a 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65  routine also doe
1e40b 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67  s error checking
1e40c 20 61 6e 64 20 6e 61 6d 65 20 72 65 73 6f 6c 75   and name resolu
1e40d 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e 63  tion for.** func
1e40e 74 69 6f 6e 20 6e 61 6d 65 73 2e 20 20 54 68 65  tion names.  The
1e40f 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20 61 67   operator for ag
1e410 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
1e411 73 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20  s is changed.** 
1e412 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  to TK_AGG_FUNCTI
1e413 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ON..*/.static in
1e414 74 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65  t resolveExprSte
1e415 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  p(Walker *pWalke
1e416 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  r, Expr *pExpr){
1e417 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  .  NameContext *
1e418 70 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50  pNC;.  Parse *pP
1e419 61 72 73 65 3b 0a 0a 20 20 70 4e 43 20 3d 20 70  arse;..  pNC = p
1e41a 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20  Walker->u.pNC;. 
1e41b 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20   assert( pNC!=0 
1e41c 29 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 4e  );.  pParse = pN
1e41d 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 61 73 73  C->pParse;.  ass
1e41e 65 72 74 28 20 70 50 61 72 73 65 3d 3d 70 57 61  ert( pParse==pWa
1e41f 6c 6b 65 72 2d 3e 70 50 61 72 73 65 20 29 3b 0a  lker->pParse );.
1e420 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e  .  if( ExprHasAn
1e421 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  yProperty(pExpr,
1e422 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 20 29 20   EP_Resolved) ) 
1e423 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65  return WRC_Prune
1e424 3b 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65  ;.  ExprSetPrope
1e425 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65  rty(pExpr, EP_Re
1e426 73 6f 6c 76 65 64 29 3b 0a 23 69 66 6e 64 65 66  solved);.#ifndef
1e427 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 70 4e   NDEBUG.  if( pN
1e428 43 2d 3e 70 53 72 63 4c 69 73 74 20 26 26 20 70  C->pSrcList && p
1e429 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 41  NC->pSrcList->nA
1e42a 6c 6c 6f 63 3e 30 20 29 7b 0a 20 20 20 20 53 72  lloc>0 ){.    Sr
1e42b 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20  cList *pSrcList 
1e42c 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b  = pNC->pSrcList;
1e42d 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
1e42e 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 43 2d 3e  for(i=0; i<pNC->
1e42f 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pSrcList->nSrc; 
1e430 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65  i++){.      asse
1e431 72 74 28 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b  rt( pSrcList->a[
1e432 69 5d 2e 69 43 75 72 73 6f 72 3e 3d 30 20 26 26  i].iCursor>=0 &&
1e433 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e   pSrcList->a[i].
1e434 69 43 75 72 73 6f 72 3c 70 50 61 72 73 65 2d 3e  iCursor<pParse->
1e435 6e 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  nTab);.    }.  }
1e436 0a 23 65 6e 64 69 66 0a 20 20 73 77 69 74 63 68  .#endif.  switch
1e437 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 0a  ( pExpr->op ){..
1e438 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
1e439 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45  TE_ENABLE_UPDATE
1e43a 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26  _DELETE_LIMIT) &
1e43b 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
1e43c 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
1e43d 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63  .    /* The spec
1e43e 69 61 6c 20 6f 70 65 72 61 74 6f 72 20 54 4b 5f  ial operator TK_
1e43f 52 4f 57 20 6d 65 61 6e 73 20 75 73 65 20 74 68  ROW means use th
1e440 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  e rowid for the 
1e441 66 69 72 73 74 0a 20 20 20 20 2a 2a 20 63 6f 6c  first.    ** col
1e442 75 6d 6e 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  umn in the FROM 
1e443 63 6c 61 75 73 65 2e 20 20 54 68 69 73 20 69 73  clause.  This is
1e444 20 75 73 65 64 20 62 79 20 74 68 65 20 4c 49 4d   used by the LIM
1e445 49 54 20 61 6e 64 20 4f 52 44 45 52 20 42 59 0a  IT and ORDER BY.
1e446 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 70 72      ** clause pr
1e447 6f 63 65 73 73 69 6e 67 20 6f 6e 20 55 50 44 41  ocessing on UPDA
1e448 54 45 20 61 6e 64 20 44 45 4c 45 54 45 20 73 74  TE and DELETE st
1e449 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f  atements..    */
1e44a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 4f 57  .    case TK_ROW
1e44b 3a 20 7b 0a 20 20 20 20 20 20 53 72 63 4c 69 73  : {.      SrcLis
1e44c 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e  t *pSrcList = pN
1e44d 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 20  C->pSrcList;.   
1e44e 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
1e44f 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
1e450 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72       assert( pSr
1e451 63 4c 69 73 74 20 26 26 20 70 53 72 63 4c 69 73  cList && pSrcLis
1e452 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  t->nSrc==1 );.  
1e453 20 20 20 20 70 49 74 65 6d 20 3d 20 70 53 72 63      pItem = pSrc
1e454 4c 69 73 74 2d 3e 61 3b 20 0a 20 20 20 20 20 20  List->a; .      
1e455 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43  pExpr->op = TK_C
1e456 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 70 45 78  OLUMN;.      pEx
1e457 70 72 2d 3e 70 54 61 62 20 3d 20 70 49 74 65 6d  pr->pTab = pItem
1e458 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 70 45  ->pTab;.      pE
1e459 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49  xpr->iTable = pI
1e45a 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
1e45b 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
1e45c 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70  mn = -1;.      p
1e45d 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d  Expr->affinity =
1e45e 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
1e45f 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  GER;.      break
1e460 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
1e461 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  * defined(SQLITE
1e462 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44  _ENABLE_UPDATE_D
1e463 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20  ELETE_LIMIT) && 
1e464 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1e465 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a  OMIT_SUBQUERY) *
1e466 2f 0a 0a 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65  /..    /* A lone
1e467 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 20 74   identifier is t
1e468 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c  he name of a col
1e469 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  umn..    */.    
1e46a 63 61 73 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20  case TK_ID: {.  
1e46b 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70      lookupName(p
1e46c 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 26 70 45  Parse, 0, 0, &pE
1e46d 78 70 72 2d 3e 74 6f 6b 65 6e 2c 20 70 4e 43 2c  xpr->token, pNC,
1e46e 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 72   pExpr);.      r
1e46f 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
1e470 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
1e471 20 41 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e   A table name an
1e472 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20  d column name:  
1e473 20 20 20 49 44 2e 49 44 0a 20 20 20 20 2a 2a 20     ID.ID.    ** 
1e474 4f 72 20 61 20 64 61 74 61 62 61 73 65 2c 20 74  Or a database, t
1e475 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a  able and column:
1e476 20 20 49 44 2e 49 44 2e 49 44 0a 20 20 20 20 2a    ID.ID.ID.    *
1e477 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 44 4f  /.    case TK_DO
1e478 54 3a 20 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e  T: {.      Token
1e479 20 2a 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20   *pColumn;.     
1e47a 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 3b 0a   Token *pTable;.
1e47b 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 44 62        Token *pDb
1e47c 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52  ;.      Expr *pR
1e47d 69 67 68 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ight;..      /* 
1e47e 69 66 28 20 70 53 72 63 4c 69 73 74 3d 3d 30 20  if( pSrcList==0 
1e47f 29 20 62 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 20  ) break; */.    
1e480 20 20 70 52 69 67 68 74 20 3d 20 70 45 78 70 72    pRight = pExpr
1e481 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ->pRight;.      
1e482 69 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d  if( pRight->op==
1e483 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 20  TK_ID ){.       
1e484 20 70 44 62 20 3d 20 30 3b 0a 20 20 20 20 20 20   pDb = 0;.      
1e485 20 20 70 54 61 62 6c 65 20 3d 20 26 70 45 78 70    pTable = &pExp
1e486 72 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b  r->pLeft->token;
1e487 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e  .        pColumn
1e488 20 3d 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65   = &pRight->toke
1e489 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  n;.      }else{.
1e48a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1e48b 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 44  pRight->op==TK_D
1e48c 4f 54 20 29 3b 0a 20 20 20 20 20 20 20 20 70 44  OT );.        pD
1e48d 62 20 3d 20 26 70 45 78 70 72 2d 3e 70 4c 65 66  b = &pExpr->pLef
1e48e 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  t->token;.      
1e48f 20 20 70 54 61 62 6c 65 20 3d 20 26 70 52 69 67    pTable = &pRig
1e490 68 74 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e  ht->pLeft->token
1e491 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 75 6d  ;.        pColum
1e492 6e 20 3d 20 26 70 52 69 67 68 74 2d 3e 70 52 69  n = &pRight->pRi
1e493 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20  ght->token;.    
1e494 20 20 7d 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70    }.      lookup
1e495 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 44 62  Name(pParse, pDb
1e496 2c 20 70 54 61 62 6c 65 2c 20 70 43 6f 6c 75 6d  , pTable, pColum
1e497 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a  n, pNC, pExpr);.
1e498 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1e499 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 0a 20  _Prune;.    }.. 
1e49a 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75     /* Resolve fu
1e49b 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20  nction names.   
1e49c 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
1e49d 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20  CONST_FUNC:.    
1e49e 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  case TK_FUNCTION
1e49f 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69  : {.      ExprLi
1e4a0 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70  st *pList = pExp
1e4a1 72 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 2f  r->x.pList;    /
1e4a2 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6c  * The argument l
1e4a3 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ist */.      int
1e4a4 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69   n = pList ? pLi
1e4a5 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 20  st->nExpr : 0;  
1e4a6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
1e4a7 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  rguments */.    
1e4a8 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75    int no_such_fu
1e4a9 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  nc = 0;       /*
1e4aa 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68   True if no such
1e4ab 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73   function exists
1e4ac 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72   */.      int wr
1e4ad 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30  ong_num_args = 0
1e4ae 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
1e4af 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66   wrong number of
1e4b0 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
1e4b1 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d      int is_agg =
1e4b2 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1e4b3 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e  /* True if is an
1e4b4 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1e4b5 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ion */.      int
1e4b6 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20   auth;          
1e4b7 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68           /* Auth
1e4b8 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65  orization to use
1e4b9 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
1e4ba 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20  .      int nId; 
1e4bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e4bc 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1e4bd 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75  characters in fu
1e4be 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20  nction name */. 
1e4bf 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1e4c0 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20  *zId;           
1e4c1 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e   /* The function
1e4c2 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20   name. */.      
1e4c3 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20  FuncDef *pDef;  
1e4c4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1e4c5 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
1e4c6 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
1e4c7 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20  .      u8 enc = 
1e4c8 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b  ENC(pParse->db);
1e4c9 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1e4ca 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a  se encoding */..
1e4cb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
1e4cc 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
1e4cd 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
1e4ce 63 74 29 20 29 3b 0a 20 20 20 20 20 20 7a 49 64  ct) );.      zId
1e4cf 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d   = (char*)pExpr-
1e4d0 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20  >token.z;.      
1e4d1 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b  nId = pExpr->tok
1e4d2 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44 65 66  en.n;.      pDef
1e4d3 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
1e4d4 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64  nction(pParse->d
1e4d5 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 2c 20  b, zId, nId, n, 
1e4d6 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  enc, 0);.      i
1e4d7 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20  f( pDef==0 ){.  
1e4d8 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c        pDef = sql
1e4d9 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
1e4da 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64  (pParse->db, zId
1e4db 2c 20 6e 49 64 2c 20 2d 31 2c 20 65 6e 63 2c 20  , nId, -1, enc, 
1e4dc 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
1e4dd 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pDef==0 ){.     
1e4de 20 20 20 20 20 6e 6f 5f 73 75 63 68 5f 66 75 6e       no_such_fun
1e4df 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  c = 1;.        }
1e4e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1e4e1 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d  wrong_num_args =
1e4e2 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
1e4e3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e4e4 20 20 20 69 73 5f 61 67 67 20 3d 20 70 44 65 66     is_agg = pDef
1e4e5 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a 20 20 20 20  ->xFunc==0;.    
1e4e6 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
1e4e7 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
1e4e8 41 54 49 4f 4e 0a 20 20 20 20 20 20 69 66 28 20  ATION.      if( 
1e4e9 70 44 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20  pDef ){.        
1e4ea 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75  auth = sqlite3Au
1e4eb 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
1e4ec 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 2c  SQLITE_FUNCTION,
1e4ed 20 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c   0, pDef->zName,
1e4ee 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
1e4ef 20 61 75 74 68 21 3d 53 51 4c 49 54 45 5f 4f 4b   auth!=SQLITE_OK
1e4f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
1e4f1 28 20 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44  ( auth==SQLITE_D
1e4f2 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ENY ){.         
1e4f3 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1e4f4 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20  sg(pParse, "not 
1e4f5 61 75 74 68 6f 72 69 7a 65 64 20 74 6f 20 75 73  authorized to us
1e4f6 65 20 66 75 6e 63 74 69 6f 6e 3a 20 25 73 22 2c  e function: %s",
1e4f7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1e4f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e4f9 20 20 20 20 20 70 44 65 66 2d 3e 7a 4e 61 6d 65       pDef->zName
1e4fa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  );.            p
1e4fb 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  NC->nErr++;.    
1e4fc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e4fd 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
1e4fe 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20  _NULL;.         
1e4ff 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
1e500 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
1e501 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
1e502 20 20 69 66 28 20 69 73 5f 61 67 67 20 26 26 20    if( is_agg && 
1e503 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 29  !pNC->allowAgg )
1e504 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1e505 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1e506 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67  , "misuse of agg
1e507 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
1e508 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 7a 49 64  %.*s()", nId,zId
1e509 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e  );.        pNC->
1e50a 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nErr++;.        
1e50b 69 73 5f 61 67 67 20 3d 20 30 3b 0a 20 20 20 20  is_agg = 0;.    
1e50c 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f 5f 73    }else if( no_s
1e50d 75 63 68 5f 66 75 6e 63 20 29 7b 0a 20 20 20 20  uch_func ){.    
1e50e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1e50f 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
1e510 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a 20 25  such function: %
1e511 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b  .*s", nId, zId);
1e512 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45  .        pNC->nE
1e513 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rr++;.      }els
1e514 65 20 69 66 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f  e if( wrong_num_
1e515 61 72 67 73 20 29 7b 0a 20 20 20 20 20 20 20 20  args ){.        
1e516 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1e517 70 50 61 72 73 65 2c 22 77 72 6f 6e 67 20 6e 75  pParse,"wrong nu
1e518 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
1e519 73 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 25 2e  s to function %.
1e51a 2a 73 28 29 22 2c 0a 20 20 20 20 20 20 20 20 20  *s()",.         
1e51b 20 20 20 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20      nId, zId);. 
1e51c 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72         pNC->nErr
1e51d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
1e51e 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 7b 0a    if( is_agg ){.
1e51f 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f          pExpr->o
1e520 70 20 3d 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  p = TK_AGG_FUNCT
1e521 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 70 4e 43  ION;.        pNC
1e522 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20  ->hasAgg = 1;.  
1e523 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1e524 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c  is_agg ) pNC->al
1e525 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20  lowAgg = 0;.    
1e526 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70    sqlite3WalkExp
1e527 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70  rList(pWalker, p
1e528 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28  List);.      if(
1e529 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61   is_agg ) pNC->a
1e52a 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 20  llowAgg = 1;.   
1e52b 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 20 43     /* FIX ME:  C
1e52c 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e 61 66  ompute pExpr->af
1e52d 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f 6e 20  finity based on 
1e52e 74 68 65 20 65 78 70 65 63 74 65 64 20 72 65 74  the expected ret
1e52f 75 72 6e 0a 20 20 20 20 20 20 2a 2a 20 74 79 70  urn.      ** typ
1e530 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  e of the functio
1e531 6e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n .      */.    
1e532 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
1e533 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ne;.    }.#ifnde
1e534 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
1e535 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65 20  BQUERY.    case 
1e536 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20 63  TK_SELECT:.    c
1e537 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 23  ase TK_EXISTS:.#
1e538 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54  endif.    case T
1e539 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66  K_IN: {.      if
1e53a 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
1e53b 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
1e53c 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20  elect) ){.      
1e53d 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 70 4e 43    int nRef = pNC
1e53e 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64 65 66 20  ->nRef;.#ifndef 
1e53f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
1e540 4b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e  K.        if( pN
1e541 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20 20  C->isCheck ){.  
1e542 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1e543 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
1e544 73 75 62 71 75 65 72 69 65 73 20 70 72 6f 68 69  subqueries prohi
1e545 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b 20 63  bited in CHECK c
1e546 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a 20 20  onstraints");.  
1e547 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
1e548 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61         sqlite3Wa
1e549 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72  lkSelect(pWalker
1e54a 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  , pExpr->x.pSele
1e54b 63 74 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  ct);.        ass
1e54c 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 66 3e 3d  ert( pNC->nRef>=
1e54d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 20 20 20  nRef );.        
1e54e 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 2d 3e 6e  if( nRef!=pNC->n
1e54f 52 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Ref ){.         
1e550 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
1e551 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72 53 65  (pExpr, EP_VarSe
1e552 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  lect);.        }
1e553 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1e554 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  reak;.    }.#ifn
1e555 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1e556 43 48 45 43 4b 0a 20 20 20 20 63 61 73 65 20 54  CHECK.    case T
1e557 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20  K_VARIABLE: {.  
1e558 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 43      if( pNC->isC
1e559 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  heck ){.        
1e55a 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1e55b 70 50 61 72 73 65 2c 22 70 61 72 61 6d 65 74 65  pParse,"paramete
1e55c 72 73 20 70 72 6f 68 69 62 69 74 65 64 20 69 6e  rs prohibited in
1e55d 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
1e55e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ts");.      }.  
1e55f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1e560 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65  .#endif.  }.  re
1e561 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45  turn (pParse->nE
1e562 72 72 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62  rr || pParse->db
1e563 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20  ->mallocFailed) 
1e564 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 52  ? WRC_Abort : WR
1e565 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f  C_Continue;.}../
1e566 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73 20 61  *.** pEList is a
1e567 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
1e568 69 6f 6e 73 20 77 68 69 63 68 20 61 72 65 20 72  ions which are r
1e569 65 61 6c 6c 79 20 74 68 65 20 72 65 73 75 6c 74  eally the result
1e56a 20 73 65 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61   set of the.** a
1e56b 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1e56c 74 2e 20 20 70 45 20 69 73 20 61 20 74 65 72 6d  t.  pE is a term
1e56d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20   in an ORDER BY 
1e56e 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  or GROUP BY clau
1e56f 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  se..** This rout
1e570 69 6e 65 20 63 68 65 63 6b 73 20 74 6f 20 73 65  ine checks to se
1e571 65 20 69 66 20 70 45 20 69 73 20 61 20 73 69 6d  e if pE is a sim
1e572 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 20 77  ple identifier w
1e573 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73  hich corresponds
1e574 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 2d 6e 61  .** to the AS-na
1e575 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65  me of one of the
1e576 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 65 78   terms of the ex
1e577 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20  pression list.  
1e578 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20 74 68 69  If it is,.** thi
1e579 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
1e57a 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77   an integer betw
1e57b 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 68 65 72  een 1 and N wher
1e57c 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
1e57d 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73  r of.** elements
1e57e 20 69 6e 20 70 45 4c 69 73 74 2c 20 63 6f 72 72   in pEList, corr
1e57f 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
1e580 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79 2e   matching entry.
1e581 20 20 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a    If there is.**
1e582 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72 20 69 66   no match, or if
1e583 20 70 45 20 69 73 20 6e 6f 74 20 61 20 73 69 6d   pE is not a sim
1e584 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2c 20  ple identifier, 
1e585 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
1e586 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a  e.** return 0..*
1e587 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68 61 73 20  *.** pEList has 
1e588 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20  been resolved.  
1e589 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a 2f 0a 73  pE has not..*/.s
1e58a 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76  tatic int resolv
1e58b 65 41 73 4e 61 6d 65 28 0a 20 20 50 61 72 73 65  eAsName(.  Parse
1e58c 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
1e58d 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
1e58e 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61   for error messa
1e58f 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ges */.  ExprLis
1e590 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f 2a 20 4c  t *pEList,  /* L
1e591 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ist of expressio
1e592 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20  ns to scan */.  
1e593 45 78 70 72 20 2a 70 45 20 20 20 20 20 20 20 20  Expr *pE        
1e594 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
1e595 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74   we are trying t
1e596 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20  o match */.){.  
1e597 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
1e598 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1e599 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 70 45 2d  er */..  if( pE-
1e59a 3e 6f 70 3d 3d 54 4b 5f 49 44 20 7c 7c 20 28 70  >op==TK_ID || (p
1e59b 45 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47  E->op==TK_STRING
1e59c 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e 2e 7a 5b   && pE->token.z[
1e59d 30 5d 21 3d 27 5c 27 27 29 20 29 7b 0a 20 20 20  0]!='\'') ){.   
1e59e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1e59f 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 63  Parse->db;.    c
1e5a0 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 73 71 6c 69  har *zCol = sqli
1e5a1 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
1e5a2 28 64 62 2c 20 26 70 45 2d 3e 74 6f 6b 65 6e 29  (db, &pE->token)
1e5a3 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f 6c 3d 3d  ;.    if( zCol==
1e5a4 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
1e5a5 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n -1;.    }.    
1e5a6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73  for(i=0; i<pELis
1e5a7 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
1e5a8 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20        char *zAs 
1e5a9 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  = pEList->a[i].z
1e5aa 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 20  Name;.      if( 
1e5ab 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65  zAs!=0 && sqlite
1e5ac 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43  3StrICmp(zAs, zC
1e5ad 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
1e5ae 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1e5af 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20  db, zCol);.     
1e5b0 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20     return i+1;. 
1e5b1 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1e5b2 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1e5b3 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20  b, zCol);.  }.  
1e5b4 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
1e5b5 2a 2a 20 70 45 20 69 73 20 61 20 70 6f 69 6e 74  ** pE is a point
1e5b6 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73  er to an express
1e5b7 69 6f 6e 20 77 68 69 63 68 20 69 73 20 61 20 73  ion which is a s
1e5b8 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 74 68  ingle term in th
1e5b9 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 6f 66  e.** ORDER BY of
1e5ba 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
1e5bb 43 54 2e 20 20 54 68 65 20 65 78 70 72 65 73 73  CT.  The express
1e5bc 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ion has not been
1e5bd 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c 76 65  .** name resolve
1e5be 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20  d..**.** At the 
1e5bf 70 6f 69 6e 74 20 74 68 69 73 20 72 6f 75 74 69  point this routi
1e5c0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65  ne is called, we
1e5c1 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68   already know th
1e5c2 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20  at the.** ORDER 
1e5c3 42 59 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 61  BY term is not a
1e5c4 6e 20 69 6e 74 65 67 65 72 20 69 6e 64 65 78 20  n integer index 
1e5c5 69 6e 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20  into the result 
1e5c6 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a 20 63 61  set.  That.** ca
1e5c7 73 65 20 69 73 20 68 61 6e 64 6c 65 64 20 62 79  se is handled by
1e5c8 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75   the calling rou
1e5c9 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 74 65  tine..**.** Atte
1e5ca 6d 70 74 20 74 6f 20 6d 61 74 63 68 20 70 45 20  mpt to match pE 
1e5cb 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73  against result s
1e5cc 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  et columns in th
1e5cd 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a 2a 20 53  e left-most.** S
1e5ce 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
1e5cf 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64    Return the ind
1e5d0 65 78 20 69 20 6f 66 20 74 68 65 20 6d 61 74 63  ex i of the matc
1e5d1 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20  hing column,.** 
1e5d2 61 73 20 61 6e 20 69 6e 64 69 63 61 74 69 6f 6e  as an indication
1e5d3 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 74   to the caller t
1e5d4 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 73 6f  hat it should so
1e5d5 72 74 20 62 79 20 74 68 65 20 69 2d 74 68 20 63  rt by the i-th c
1e5d6 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65 20 6c 65  olumn..** The le
1e5d7 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69  ft-most column i
1e5d8 73 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  s 1.  In other w
1e5d9 6f 72 64 73 2c 20 74 68 65 20 76 61 6c 75 65 20  ords, the value 
1e5da 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 0a  returned is the.
1e5db 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20  ** same integer 
1e5dc 76 61 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64  value that would
1e5dd 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 65 20   be used in the 
1e5de 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  SQL statement to
1e5df 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 65   indicate.** the
1e5e0 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49   column..**.** I
1e5e1 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
1e5e2 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 20  tch, return 0.  
1e5e3 52 65 74 75 72 6e 20 2d 31 20 69 66 20 61 6e 20  Return -1 if an 
1e5e4 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f  error occurs..*/
1e5e5 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f  .static int reso
1e5e6 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f  lveOrderByTermTo
1e5e7 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73  ExprList(.  Pars
1e5e8 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
1e5e9 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1e5ea 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73  t for error mess
1e5eb 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ages */.  Select
1e5ec 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20   *pSelect,   /* 
1e5ed 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
1e5ee 6d 65 6e 74 20 77 69 74 68 20 74 68 65 20 4f 52  ment with the OR
1e5ef 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
1e5f0 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20  .  Expr *pE     
1e5f1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65        /* The spe
1e5f2 63 69 66 69 63 20 4f 52 44 45 52 20 42 59 20 74  cific ORDER BY t
1e5f3 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  erm */.){.  int 
1e5f4 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  i;             /
1e5f5 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1e5f6 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
1e5f7 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 20 63 6f  List;  /* The co
1e5f8 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 72 65 73  lumns of the res
1e5f9 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 4e 61 6d  ult set */.  Nam
1e5fa 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 20 20 20  eContext nc;    
1e5fb 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
1e5fc 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 20 70 45  for resolving pE
1e5fd 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
1e5fe 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65  qlite3ExprIsInte
1e5ff 67 65 72 28 70 45 2c 20 26 69 29 3d 3d 30 20 29  ger(pE, &i)==0 )
1e600 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65  ;.  pEList = pSe
1e601 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20  lect->pEList;.. 
1e602 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20   /* Resolve all 
1e603 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 4f 52 44  names in the ORD
1e604 45 52 20 42 59 20 74 65 72 6d 20 65 78 70 72 65  ER BY term expre
1e605 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d  ssion.  */.  mem
1e606 73 65 74 28 26 6e 63 2c 20 30 2c 20 73 69 7a 65  set(&nc, 0, size
1e607 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63 2e 70 50  of(nc));.  nc.pP
1e608 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
1e609 20 6e 63 2e 70 53 72 63 4c 69 73 74 20 3d 20 70   nc.pSrcList = p
1e60a 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20  Select->pSrc;.  
1e60b 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70 45 4c 69  nc.pEList = pELi
1e60c 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f 77 41 67  st;.  nc.allowAg
1e60d 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e 45 72 72  g = 1;.  nc.nErr
1e60e 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69   = 0;.  if( sqli
1e60f 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
1e610 6d 65 73 28 26 6e 63 2c 20 70 45 29 20 29 7b 0a  mes(&nc, pE) ){.
1e611 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1e612 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20  Clear(pParse);. 
1e613 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1e614 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 6d 61  ..  /* Try to ma
1e615 74 63 68 20 74 68 65 20 4f 52 44 45 52 20 42 59  tch the ORDER BY
1e616 20 65 78 70 72 65 73 73 69 6f 6e 20 61 67 61 69   expression agai
1e617 6e 73 74 20 61 6e 20 65 78 70 72 65 73 73 69 6f  nst an expressio
1e618 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65  n.  ** in the re
1e619 73 75 6c 74 20 73 65 74 2e 20 20 52 65 74 75 72  sult set.  Retur
1e61a 6e 20 61 6e 20 31 2d 62 61 73 65 64 20 69 6e 64  n an 1-based ind
1e61b 65 78 20 6f 66 20 74 68 65 20 6d 61 74 63 68 69  ex of the matchi
1e61c 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73  ng.  ** result-s
1e61d 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a 2f 0a 20  et entry..  */. 
1e61e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69   for(i=0; i<pELi
1e61f 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
1e620 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1e621 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45 4c 69  ExprCompare(pELi
1e622 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20  st->a[i].pExpr, 
1e623 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  pE) ){.      ret
1e624 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 7d 0a 20  urn i+1;.    }. 
1e625 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 6d   }..  /* If no m
1e626 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e 20  atch, return 0. 
1e627 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  */.  return 0;.}
1e628 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1e629 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20   an ORDER BY or 
1e62a 47 52 4f 55 50 20 42 59 20 74 65 72 6d 20 6f 75  GROUP BY term ou
1e62b 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72 72 6f 72  t-of-range error
1e62c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1e62d 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e   resolveOutOfRan
1e62e 67 65 45 72 72 6f 72 28 0a 20 20 50 61 72 73 65  geError(.  Parse
1e62f 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
1e630 20 20 2f 2a 20 54 68 65 20 65 72 72 6f 72 20 63    /* The error c
1e631 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77 68 69 63  ontext into whic
1e632 68 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65  h to write the e
1e633 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rror */.  const 
1e634 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 20 20  char *zType,    
1e635 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22   /* "ORDER" or "
1e636 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69 6e 74 20  GROUP" */.  int 
1e637 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i,              
1e638 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20     /* The index 
1e639 28 31 2d 62 61 73 65 64 29 20 6f 66 20 74 68 65  (1-based) of the
1e63a 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e   term out of ran
1e63b 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 20 20  ge */.  int mx  
1e63c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e63d 2a 20 4c 61 72 67 65 73 74 20 70 65 72 6d 69 73  * Largest permis
1e63e 73 69 62 6c 65 20 76 61 6c 75 65 20 6f 66 20 69  sible value of i
1e63f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
1e640 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1e641 20 0a 20 20 20 20 22 25 72 20 25 73 20 42 59 20   .    "%r %s BY 
1e642 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67  term out of rang
1e643 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 20 22 0a  e - should be ".
1e644 20 20 20 20 22 62 65 74 77 65 65 6e 20 31 20 61      "between 1 a
1e645 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54 79 70 65  nd %d", i, zType
1e646 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  , mx);.}../*.** 
1e647 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f 52 44 45  Analyze the ORDE
1e648 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20 61  R BY clause in a
1e649 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
1e64a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20 4d 6f   statement.   Mo
1e64b 64 69 66 79 0a 2a 2a 20 65 61 63 68 20 74 65 72  dify.** each ter
1e64c 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  m of the ORDER B
1e64d 59 20 63 6c 61 75 73 65 20 69 73 20 61 20 63 6f  Y clause is a co
1e64e 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72 20 62  nstant integer b
1e64f 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61 6e 64 20  etween 1.** and 
1e650 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  N where N is the
1e651 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1e652 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 6f 75  ns in the compou
1e653 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a  nd SELECT..**.**
1e654 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
1e655 74 68 61 74 20 61 72 65 20 61 6c 72 65 61 64 79  that are already
1e656 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77   an integer betw
1e657 65 65 6e 20 31 20 61 6e 64 20 4e 20 61 72 65 0a  een 1 and N are.
1e658 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64 2e 20 20  ** unmodified.  
1e659 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74  ORDER BY terms t
1e65a 68 61 74 20 61 72 65 20 69 6e 74 65 67 65 72 73  hat are integers
1e65b 20 6f 75 74 73 69 64 65 20 74 68 65 20 72 61 6e   outside the ran
1e65c 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68 72 6f 75  ge of.** 1 throu
1e65d 67 68 20 4e 20 67 65 6e 65 72 61 74 65 20 61 6e  gh N generate an
1e65e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45 52 20 42   error.  ORDER B
1e65f 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65  Y terms that are
1e660 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20   expressions.** 
1e661 61 72 65 20 6d 61 74 63 68 65 64 20 61 67 61 69  are matched agai
1e662 6e 73 74 20 72 65 73 75 6c 74 20 73 65 74 20 65  nst result set e
1e663 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 63 6f  xpressions of co
1e664 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 0a 2a 2a  mpound SELECT.**
1e665 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
1e666 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 53 45  the left-most SE
1e667 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b 69 6e 67  LECT and working
1e668 20 74 6f 77 61 72 64 20 74 68 65 20 72 69 67 68   toward the righ
1e669 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20 66 69 72  t..** At the fir
1e66a 73 74 20 6d 61 74 63 68 2c 20 74 68 65 20 4f 52  st match, the OR
1e66b 44 45 52 20 42 59 20 65 78 70 72 65 73 73 69 6f  DER BY expressio
1e66c 6e 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64  n is transformed
1e66d 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 69 6e 74   into.** the int
1e66e 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62  eger column numb
1e66f 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  er..**.** Return
1e670 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
1e671 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a 2f 0a 73  rrors seen..*/.s
1e672 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76  tatic int resolv
1e673 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79  eCompoundOrderBy
1e674 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1e675 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
1e676 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c  sing context.  L
1e677 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61  eave error messa
1e678 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65  ges here */.  Se
1e679 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20  lect *pSelect   
1e67a 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
1e67b 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74  T statement cont
1e67c 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 44 45 52  aining the ORDER
1e67d 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20   BY */.){.  int 
1e67e 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  i;.  ExprList *p
1e67f 4f 72 64 65 72 42 79 3b 0a 20 20 45 78 70 72 4c  OrderBy;.  ExprL
1e680 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73  ist *pEList;.  s
1e681 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e  qlite3 *db;.  in
1e682 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a  t moreToDo = 1;.
1e683 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 53  .  pOrderBy = pS
1e684 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 42 79 3b  elect->pOrderBy;
1e685 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d  .  if( pOrderBy=
1e686 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
1e687 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
1e688 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ;.#if SQLITE_MAX
1e689 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f  _COLUMN.  if( pO
1e68a 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62  rderBy->nExpr>db
1e68b 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1e68c 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b  LIMIT_COLUMN] ){
1e68d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1e68e 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f  rMsg(pParse, "to
1e68f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20  o many terms in 
1e690 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 22  ORDER BY clause"
1e691 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
1e692 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 66 6f  .  }.#endif.  fo
1e693 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42  r(i=0; i<pOrderB
1e694 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  y->nExpr; i++){.
1e695 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b      pOrderBy->a[
1e696 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d  i].done = 0;.  }
1e697 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78  .  pSelect->pNex
1e698 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  t = 0;.  while( 
1e699 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20  pSelect->pPrior 
1e69a 29 7b 0a 20 20 20 20 70 53 65 6c 65 63 74 2d 3e  ){.    pSelect->
1e69b 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 20 3d 20  pPrior->pNext = 
1e69c 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70 53 65  pSelect;.    pSe
1e69d 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  lect = pSelect->
1e69e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 77 68  pPrior;.  }.  wh
1e69f 69 6c 65 28 20 70 53 65 6c 65 63 74 20 26 26 20  ile( pSelect && 
1e6a0 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 20 20 20  moreToDo ){.    
1e6a1 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1e6a2 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20  item *pItem;.   
1e6a3 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 3b 0a 20   moreToDo = 0;. 
1e6a4 20 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c     pEList = pSel
1e6a5 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20  ect->pEList;.   
1e6a6 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
1e6a7 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  =0 );.    for(i=
1e6a8 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42  0, pItem=pOrderB
1e6a9 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79  y->a; i<pOrderBy
1e6aa 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49  ->nExpr; i++, pI
1e6ab 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e  tem++){.      in
1e6ac 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20  t iCol = -1;.   
1e6ad 20 20 20 45 78 70 72 20 2a 70 45 2c 20 2a 70 44     Expr *pE, *pD
1e6ae 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  up;.      if( pI
1e6af 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 6f 6e 74  tem->done ) cont
1e6b0 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 45 20 3d  inue;.      pE =
1e6b1 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20   pItem->pExpr;. 
1e6b2 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1e6b3 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45  ExprIsInteger(pE
1e6b4 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20  , &iCol) ){.    
1e6b5 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 7c      if( iCol<0 |
1e6b6 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e  | iCol>pEList->n
1e6b7 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20  Expr ){.        
1e6b8 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61    resolveOutOfRa
1e6b9 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c  ngeError(pParse,
1e6ba 20 22 4f 52 44 45 52 22 2c 20 69 2b 31 2c 20 70   "ORDER", i+1, p
1e6bb 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
1e6bc 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
1e6bd 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
1e6be 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1e6bf 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65    iCol = resolve
1e6c0 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  AsName(pParse, p
1e6c1 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20  EList, pE);.    
1e6c2 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 30 20      if( iCol==0 
1e6c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 44 75  ){.          pDu
1e6c4 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
1e6c5 75 70 28 64 62 2c 20 70 45 2c 20 30 29 3b 0a 20  up(db, pE, 0);. 
1e6c6 20 20 20 20 20 20 20 20 20 69 66 28 20 21 64 62           if( !db
1e6c7 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1e6c8 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  {.            as
1e6c9 73 65 72 74 28 70 44 75 70 29 3b 0a 20 20 20 20  sert(pDup);.    
1e6ca 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72          iCol = r
1e6cb 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72  esolveOrderByTer
1e6cc 6d 54 6f 45 78 70 72 4c 69 73 74 28 70 50 61 72  mToExprList(pPar
1e6cd 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 44 75  se, pSelect, pDu
1e6ce 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  p);.          }.
1e6cf 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1e6d0 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
1e6d1 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 7d  pDup);.        }
1e6d2 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
1e6d3 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  l<0 ){.         
1e6d4 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
1e6d5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1e6d6 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b     if( iCol>0 ){
1e6d7 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  .        CollSeq
1e6d8 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43   *pColl = pE->pC
1e6d9 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  oll;.        int
1e6da 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61   flags = pE->fla
1e6db 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
1e6dc 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  te;.        sqli
1e6dd 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1e6de 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70  , pE);.        p
1e6df 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45  Item->pExpr = pE
1e6e0 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
1e6e1 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30  b, TK_INTEGER, 0
1e6e2 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
1e6e3 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72 65 74   if( pE==0 ) ret
1e6e4 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 70  urn 1;.        p
1e6e5 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c  E->pColl = pColl
1e6e6 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 66 6c  ;.        pE->fl
1e6e7 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c  ags |= EP_IntVal
1e6e8 75 65 20 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20  ue | flags;.    
1e6e9 20 20 20 20 70 45 2d 3e 69 54 61 62 6c 65 20 3d      pE->iTable =
1e6ea 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70   iCol;.        p
1e6eb 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31  Item->iCol = (u1
1e6ec 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  6)iCol;.        
1e6ed 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b  pItem->done = 1;
1e6ee 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1e6ef 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d        moreToDo =
1e6f0 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
1e6f1 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20  }.    pSelect = 
1e6f2 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 3b 0a  pSelect->pNext;.
1e6f3 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
1e6f4 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  <pOrderBy->nExpr
1e6f5 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
1e6f6 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64  pOrderBy->a[i].d
1e6f7 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  one==0 ){.      
1e6f8 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1e6f9 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52 44 45  pParse, "%r ORDE
1e6fa 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73 20 6e  R BY term does n
1e6fb 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22 0a 20  ot match any ". 
1e6fc 20 20 20 20 20 20 20 20 20 20 20 22 63 6f 6c 75             "colu
1e6fd 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  mn in the result
1e6fe 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20 20 20   set", i+1);.   
1e6ff 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1e700 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1e701 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  0;.}../*.** Chec
1e702 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20  k every term in 
1e703 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20  the ORDER BY or 
1e704 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1e705 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a 20 74  pOrderBy of.** t
1e706 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
1e707 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20 49 66  ent pSelect.  If
1e708 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72 65 66   any term is ref
1e709 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a 20 72  erence to a.** r
1e70a 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73  esult set expres
1e70b 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72 6d 69  sion (as determi
1e70c 6e 65 64 20 62 79 20 74 68 65 20 45 78 70 72 4c  ned by the ExprL
1e70d 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65 6c 64  ist.a.iCol field
1e70e 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76 65 72  ).** then conver
1e70f 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e 74 6f  t that term into
1e710 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63   a copy of the c
1e711 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 65 73  orresponding res
1e712 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d  ult set.** colum
1e713 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20  n..**.** If any 
1e714 65 72 72 6f 72 73 20 61 72 65 20 64 65 74 65 63  errors are detec
1e715 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72 72 6f  ted, add an erro
1e716 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61  r message to pPa
1e717 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72  rse and.** retur
1e718 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74  n non-zero.  Ret
1e719 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f 20 65  urn zero if no e
1e71a 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e 2e 0a  rrors are seen..
1e71b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e71c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73  E int sqlite3Res
1e71d 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
1e71e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1e71f 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
1e720 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c  sing context.  L
1e721 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61  eave error messa
1e722 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65  ges here */.  Se
1e723 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
1e724 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
1e725 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74  T statement cont
1e726 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61 75 73  aining the claus
1e727 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
1e728 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20  *pOrderBy,   /* 
1e729 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20  The ORDER BY or 
1e72a 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1e72b 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65 64 20  to be processed 
1e72c 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1e72d 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 22 4f  *zType     /* "O
1e72e 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22  RDER" or "GROUP"
1e72f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
1e730 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1e731 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 45 78  pParse->db;.  Ex
1e732 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
1e733 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1e734 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a  t_item *pItem;..
1e735 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d    if( pOrderBy==
1e736 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d  0 || pParse->db-
1e737 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
1e738 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51  return 0;.#if SQ
1e739 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a  LITE_MAX_COLUMN.
1e73a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e    if( pOrderBy->
1e73b 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74  nExpr>db->aLimit
1e73c 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
1e73d 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c  LUMN] ){.    sql
1e73e 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1e73f 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74  rse, "too many t
1e740 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20 63 6c  erms in %s BY cl
1e741 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b 0a 20  ause", zType);. 
1e742 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
1e743 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69 73 74  .#endif.  pEList
1e744 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69   = pSelect->pELi
1e745 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45  st;.  assert( pE
1e746 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 73  List!=0 );  /* s
1e747 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
1e748 29 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 69  ) guarantees thi
1e749 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20  s */.  for(i=0, 
1e74a 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e  pItem=pOrderBy->
1e74b 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e  a; i<pOrderBy->n
1e74c 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
1e74d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74  ++){.    if( pIt
1e74e 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20 20 20  em->iCol ){.    
1e74f 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f    if( pItem->iCo
1e750 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  l>pEList->nExpr 
1e751 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c  ){.        resol
1e752 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f  veOutOfRangeErro
1e753 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c  r(pParse, zType,
1e754 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45   i+1, pEList->nE
1e755 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65  xpr);.        re
1e756 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
1e757 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69        resolveAli
1e758 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73  as(pParse, pELis
1e759 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31  t, pItem->iCol-1
1e75a 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20  , pItem->pExpr, 
1e75b 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20  zType);.    }.  
1e75c 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
1e75d 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20  ./*.** pOrderBy 
1e75e 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f  is an ORDER BY o
1e75f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
1e760 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 74  e in SELECT stat
1e761 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 0a 2a  ement pSelect..*
1e762 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e 74 65  * The Name conte
1e763 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  xt of the SELECT
1e764 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 4e   statement is pN
1e765 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65 69 74  C.  zType is eit
1e766 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22 20 6f  her.** "ORDER" o
1e767 72 20 22 47 52 4f 55 50 22 20 64 65 70 65 6e 64  r "GROUP" depend
1e768 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74 79 70  ing on which typ
1e769 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f 72 64  e of clause pOrd
1e76a 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54  erBy is..**.** T
1e76b 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 6f  his routine reso
1e76c 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d 20 6f  lves each term o
1e76d 66 20 74 68 65 20 63 6c 61 75 73 65 20 69 6e 74  f the clause int
1e76e 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e  o an expression.
1e76f 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64 65 72  .** If the order
1e770 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69  -by term is an i
1e771 6e 74 65 67 65 72 20 49 20 62 65 74 77 65 65 6e  nteger I between
1e772 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72 65 20   1 and N (where 
1e773 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  N is the.** numb
1e774 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
1e775 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1e776 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29 20 74  of the SELECT) t
1e777 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69  hen the expressi
1e778 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73  on.** in the res
1e779 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63 6f 70  olution is a cop
1e77a 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20 72 65  y of the I-th re
1e77b 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
1e77c 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20  ion.  If.** the 
1e77d 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73  order-by term is
1e77e 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74 68 61   an identify tha
1e77f 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  t corresponds to
1e780 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 0a   the AS-name of.
1e781 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65 74 20  ** a result-set 
1e782 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e  expression, then
1e783 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f 6c 76   the term resolv
1e784 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20  es to a copy of 
1e785 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65  the.** result-se
1e786 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4f  t expression.  O
1e787 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 65 78  therwise, the ex
1e788 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 73 6f  pression is reso
1e789 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 75  lved in.** the u
1e78a 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69 6e 67  sual way - using
1e78b 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
1e78c 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a  xprNames()..**.*
1e78d 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
1e78e 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
1e78f 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66  r of errors.  If
1e790 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74   errors occur, t
1e791 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70  hen.** an approp
1e792 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73  riate error mess
1e793 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c 65 66  age might be lef
1e794 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20 28 4f  t in pParse.  (O
1e795 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65 78 63  OM errors.** exc
1e796 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69  epted.).*/.stati
1e797 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64  c int resolveOrd
1e798 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e 61 6d  erGroupBy(.  Nam
1e799 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20  eContext *pNC,  
1e79a 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63     /* The name c
1e79b 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53 45  ontext of the SE
1e79c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
1e79d 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
1e79e 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  ect,      /* The
1e79f 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1e7a0 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64 65 72  t holding pOrder
1e7a1 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  By */.  ExprList
1e7a2 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a   *pOrderBy,   /*
1e7a3 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20   An ORDER BY or 
1e7a4 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1e7a5 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a 20 20  to resolve */.  
1e7a6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70  const char *zTyp
1e7a7 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20  e     /* Either 
1e7a8 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55  "ORDER" or "GROU
1e7a9 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61  P", as appropria
1e7aa 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  te */.){.  int i
1e7ab 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1e7ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1e7ad 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
1e7ae 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
1e7af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e7b0 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a   Column number *
1e7b1 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  /.  struct ExprL
1e7b2 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
1e7b3 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20     /* A term of 
1e7b4 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
1e7b5 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  use */.  Parse *
1e7b6 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
1e7b7 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
1e7b8 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1e7b9 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20 20 20  int nResult;    
1e7ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e7bb 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
1e7bc 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
1e7bd 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4f  set */..  if( pO
1e7be 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75  rderBy==0 ) retu
1e7bf 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c 74 20  rn 0;.  nResult 
1e7c0 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  = pSelect->pELis
1e7c1 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50 61 72  t->nExpr;.  pPar
1e7c2 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
1e7c3 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74  ;.  for(i=0, pIt
1e7c4 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
1e7c5 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
1e7c6 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  r; i++, pItem++)
1e7c7 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d  {.    Expr *pE =
1e7c8 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20   pItem->pExpr;. 
1e7c9 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76     iCol = resolv
1e7ca 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  eAsName(pParse, 
1e7cb 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c  pSelect->pEList,
1e7cc 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20 69 43   pE);.    if( iC
1e7cd 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 65  ol<0 ){.      re
1e7ce 74 75 72 6e 20 31 3b 20 20 2f 2a 20 4f 4f 4d 20  turn 1;  /* OOM 
1e7cf 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 7d 0a 20  error */.    }. 
1e7d0 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b     if( iCol>0 ){
1e7d1 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20  .      /* If an 
1e7d2 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 20 69 73  AS-name match is
1e7d3 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 74 68 69   found, mark thi
1e7d4 73 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 75 6d  s ORDER BY colum
1e7d5 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 20 20 20  n as being.     
1e7d6 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68   ** a copy of th
1e7d7 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74  e iCol-th result
1e7d8 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 68  -set column.  Th
1e7d9 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  e subsequent cal
1e7da 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 71  l to.      ** sq
1e7db 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65  lite3ResolveOrde
1e7dc 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c 6c 20  rGroupBy() will 
1e7dd 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72  convert the expr
1e7de 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 20 20 20  ession to a.    
1e7df 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 68 65    ** copy of the
1e7e0 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c 74 2d   iCol-th result-
1e7e1 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  set expression. 
1e7e2 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  */.      pItem->
1e7e3 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c  iCol = (u16)iCol
1e7e4 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  ;.      continue
1e7e5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1e7e6 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74  sqlite3ExprIsInt
1e7e7 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c 29 20  eger(pE, &iCol) 
1e7e8 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ){.      /* The 
1e7e9 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 69 73  ORDER BY term is
1e7ea 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e 73   an integer cons
1e7eb 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c 20 73 65  tant.  Again, se
1e7ec 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20  t the column.   
1e7ed 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 73 6f 20     ** number so 
1e7ee 74 68 61 74 20 73 71 6c 69 74 65 33 52 65 73 6f  that sqlite3Reso
1e7ef 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
1e7f0 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74  ) will convert t
1e7f1 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65  he.      ** orde
1e7f2 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 61 20 63  r-by term to a c
1e7f3 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c  opy of the resul
1e7f4 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
1e7f5 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 43   */.      if( iC
1e7f6 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 20 20 20  ol<1 ){.        
1e7f7 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67  resolveOutOfRang
1e7f8 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 7a  eError(pParse, z
1e7f9 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 65 73 75  Type, i+1, nResu
1e7fa 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  lt);.        ret
1e7fb 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
1e7fc 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c       pItem->iCol
1e7fd 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20   = (u16)iCol;.  
1e7fe 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
1e7ff 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 74 68 65    }..    /* Othe
1e800 72 77 69 73 65 2c 20 74 72 65 61 74 20 74 68 65  rwise, treat the
1e801 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 61   ORDER BY term a
1e802 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 65 78  s an ordinary ex
1e803 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20  pression */.    
1e804 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 30 3b  pItem->iCol = 0;
1e805 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1e806 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
1e807 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a 20 20 20  (pNC, pE) ){.   
1e808 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1e809 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1e80a 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72  sqlite3ResolveOr
1e80b 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73  derGroupBy(pPars
1e80c 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 4f 72 64  e, pSelect, pOrd
1e80d 65 72 42 79 2c 20 7a 54 79 70 65 29 3b 0a 7d 0a  erBy, zType);.}.
1e80e 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6e  ./*.** Resolve n
1e80f 61 6d 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45  ames in the SELE
1e810 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 20 61  CT statement p a
1e811 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 65  nd all of its de
1e812 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f 0a 73 74  scendents..*/.st
1e813 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65  atic int resolve
1e814 53 65 6c 65 63 74 53 74 65 70 28 57 61 6c 6b 65  SelectStep(Walke
1e815 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
1e816 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d 65 43 6f  ct *p){.  NameCo
1e817 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b  ntext *pOuterNC;
1e818 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 74 68 61    /* Context tha
1e819 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20  t contains this 
1e81a 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e 61 6d 65  SELECT */.  Name
1e81b 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
1e81c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74      /* Name cont
1e81d 65 78 74 20 6f 66 20 74 68 69 73 20 53 45 4c 45  ext of this SELE
1e81e 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f  CT */.  int isCo
1e81f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20  mpound;         
1e820 2f 2a 20 54 72 75 65 20 69 66 20 70 20 69 73 20  /* True if p is 
1e821 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  a compound selec
1e822 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6d 70  t */.  int nComp
1e823 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f  ound;          /
1e824 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70  * Number of comp
1e825 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 6f 63 65  ound terms proce
1e826 73 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20  ssed so far */. 
1e827 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
1e828 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
1e829 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
1e82a 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
1e82b 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75  t;       /* Resu
1e82c 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt set expressio
1e82d 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  n list */.  int 
1e82e 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
1e82f 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1e830 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ter */.  ExprLis
1e831 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20  t *pGroupBy;    
1e832 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59   /* The GROUP BY
1e833 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c   clause */.  Sel
1e834 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73 74 3b 20  ect *pLeftmost; 
1e835 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d 6d 6f 73       /* Left-mos
1e836 74 20 6f 66 20 53 45 4c 45 43 54 20 6f 66 20 61  t of SELECT of a
1e837 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 73   compound */.  s
1e838 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
1e839 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
1e83a 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
1e83b 0a 20 20 0a 0a 20 20 61 73 73 65 72 74 28 20 70  .  ..  assert( p
1e83c 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  !=0 );.  if( p->
1e83d 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65  selFlags & SF_Re
1e83e 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20 20 72 65  solved ){.    re
1e83f 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
1e840 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d    }.  pOuterNC =
1e841 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b   pWalker->u.pNC;
1e842 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c  .  pParse = pWal
1e843 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 64  ker->pParse;.  d
1e844 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1e845 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 20 73  .  /* Normally s
1e846 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61  qlite3SelectExpa
1e847 6e 64 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c  nd() will be cal
1e848 6c 65 64 20 66 69 72 73 74 20 61 6e 64 20 77 69  led first and wi
1e849 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72  ll have.  ** alr
1e84a 65 61 64 79 20 65 78 70 61 6e 64 65 64 20 74 68  eady expanded th
1e84b 69 73 20 53 45 4c 45 43 54 2e 20 20 48 6f 77 65  is SELECT.  Howe
1e84c 76 65 72 2c 20 69 66 20 74 68 69 73 20 69 73 20  ver, if this is 
1e84d 61 20 73 75 62 71 75 65 72 79 20 77 69 74 68 69  a subquery withi
1e84e 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73  n.  ** an expres
1e84f 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33 52 65 73  sion, sqlite3Res
1e850 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20  olveExprNames() 
1e851 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 77  will be called w
1e852 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a 20 70 72  ithout a.  ** pr
1e853 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ior call to sqli
1e854 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
1e855 29 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61  ).  When that ha
1e856 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20 2a 2a 20  ppens, let.  ** 
1e857 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65  sqlite3SelectPre
1e858 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66 20 74 68  p() do all of th
1e859 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72  e processing for
1e85a 20 74 68 69 73 20 53 45 4c 45 43 54 2e 0a 20 20   this SELECT..  
1e85b 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ** sqlite3Select
1e85c 50 72 65 70 28 29 20 77 69 6c 6c 20 69 6e 76 6f  Prep() will invo
1e85d 6b 65 20 62 6f 74 68 20 73 71 6c 69 74 65 33 53  ke both sqlite3S
1e85e 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 61 6e  electExpand() an
1e85f 64 0a 20 20 2a 2a 20 74 68 69 73 20 72 6f 75 74  d.  ** this rout
1e860 69 6e 65 20 69 6e 20 74 68 65 20 63 6f 72 72 65  ine in the corre
1e861 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20  ct order..  */. 
1e862 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67   if( (p->selFlag
1e863 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29  s & SF_Expanded)
1e864 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1e865 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61  e3SelectPrep(pPa
1e866 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43  rse, p, pOuterNC
1e867 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70  );.    return (p
1e868 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
1e869 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
1e86a 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57   ? WRC_Abort : W
1e86b 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20  RC_Prune;.  }.. 
1e86c 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d   isCompound = p-
1e86d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20 20 6e 43  >pPrior!=0;.  nC
1e86e 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 70  ompound = 0;.  p
1e86f 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20  Leftmost = p;.  
1e870 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
1e871 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46  assert( (p->selF
1e872 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64  lags & SF_Expand
1e873 65 64 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  ed)!=0 );.    as
1e874 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61  sert( (p->selFla
1e875 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64  gs & SF_Resolved
1e876 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 73  )==0 );.    p->s
1e877 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 52 65  elFlags |= SF_Re
1e878 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20 2f 2a 20  solved;..    /* 
1e879 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72  Resolve the expr
1e87a 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 4c  essions in the L
1e87b 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20  IMIT and OFFSET 
1e87c 63 6c 61 75 73 65 73 2e 20 54 68 65 73 65 0a 20  clauses. These. 
1e87d 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c     ** are not al
1e87e 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74  lowed to refer t
1e87f 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20  o any names, so 
1e880 70 61 73 73 20 61 6e 20 65 6d 70 74 79 20 4e 61  pass an empty Na
1e881 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a  meContext..    *
1e882 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e  /.    memset(&sN
1e883 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
1e884 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72  ));.    sNC.pPar
1e885 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20  se = pParse;.   
1e886 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
1e887 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
1e888 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c  C, p->pLimit) ||
1e889 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1e88a 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
1e88b 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65  (&sNC, p->pOffse
1e88c 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  t) ){.      retu
1e88d 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1e88e 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 53 65    }.  .    /* Se
1e88f 74 20 75 70 20 74 68 65 20 6c 6f 63 61 6c 20 6e  t up the local n
1e890 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 70  ame-context to p
1e891 61 73 73 20 74 6f 20 73 71 6c 69 74 65 33 52 65  ass to sqlite3Re
1e892 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29  solveExprNames()
1e893 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c   to.    ** resol
1e894 76 65 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65  ve the result-se
1e895 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  t expression lis
1e896 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e  t..    */.    sN
1e897 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a  C.allowAgg = 1;.
1e898 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74      sNC.pSrcList
1e899 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20   = p->pSrc;.    
1e89a 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4f 75 74  sNC.pNext = pOut
1e89b 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20 2f 2a 20  erNC;.  .    /* 
1e89c 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e  Resolve names in
1e89d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
1e89e 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73 74 20 3d   */.    pEList =
1e89f 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20   p->pEList;.    
1e8a0 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d  assert( pEList!=
1e8a1 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  0 );.    for(i=0
1e8a2 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; i<pEList->nExp
1e8a3 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45  r; i++){.      E
1e8a4 78 70 72 20 2a 70 58 20 3d 20 70 45 4c 69 73 74  xpr *pX = pEList
1e8a5 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
1e8a6 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
1e8a7 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
1e8a8 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a 20 20 20  &sNC, pX) ){.   
1e8a9 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
1e8aa 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20  Abort;.      }. 
1e8ab 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52     }.  .    /* R
1e8ac 65 63 75 72 73 69 76 65 6c 79 20 72 65 73 6f 6c  ecursively resol
1e8ad 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20  ve names in all 
1e8ae 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20 2a  subqueries.    *
1e8af 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
1e8b0 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20  <p->pSrc->nSrc; 
1e8b1 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75  i++){.      stru
1e8b2 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
1e8b3 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72  *pItem = &p->pSr
1e8b4 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 69  c->a[i];.      i
1e8b5 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  f( pItem->pSelec
1e8b6 74 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e  t ){.        con
1e8b7 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65 64 43  st char *zSavedC
1e8b8 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d  ontext = pParse-
1e8b9 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20  >zAuthContext;. 
1e8ba 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
1e8bb 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61 72 73 65  ->zName ) pParse
1e8bc 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d  ->zAuthContext =
1e8bd 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20   pItem->zName;. 
1e8be 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
1e8bf 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73  solveSelectNames
1e8c0 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e  (pParse, pItem->
1e8c1 70 53 65 6c 65 63 74 2c 20 70 4f 75 74 65 72 4e  pSelect, pOuterN
1e8c2 43 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72  C);.        pPar
1e8c3 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
1e8c4 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74 65 78 74   = zSavedContext
1e8c5 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  ;.        if( pP
1e8c6 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
1e8c7 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1e8c8 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1e8c9 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
1e8ca 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  .  .    /* If th
1e8cb 65 72 65 20 61 72 65 20 6e 6f 20 61 67 67 72 65  ere are no aggre
1e8cc 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69  gate functions i
1e8cd 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74  n the result-set
1e8ce 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55 50 20 42  , and no GROUP B
1e8cf 59 20 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73  Y .    ** expres
1e8d0 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20 61 6c 6c  sion, do not all
1e8d1 6f 77 20 61 67 67 72 65 67 61 74 65 73 20 69 6e  ow aggregates in
1e8d2 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65   any of the othe
1e8d3 72 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20  r expressions.. 
1e8d4 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
1e8d5 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  ( (p->selFlags &
1e8d6 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 3d 3d   SF_Aggregate)==
1e8d7 30 20 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 42  0 );.    pGroupB
1e8d8 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b  y = p->pGroupBy;
1e8d9 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42  .    if( pGroupB
1e8da 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41 67 67 20  y || sNC.hasAgg 
1e8db 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73 65 6c 46  ){.      p->selF
1e8dc 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 67 72 65  lags |= SF_Aggre
1e8dd 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  gate;.    }else{
1e8de 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77  .      sNC.allow
1e8df 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Agg = 0;.    }. 
1e8e0 20 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 48 41   .    /* If a HA
1e8e1 56 49 4e 47 20 63 6c 61 75 73 65 20 69 73 20 70  VING clause is p
1e8e2 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74 68 65  resent, then the
1e8e3 72 65 20 6d 75 73 74 20 62 65 20 61 20 47 52 4f  re must be a GRO
1e8e4 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 20 20  UP BY clause..  
1e8e5 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e    */.    if( p->
1e8e6 70 48 61 76 69 6e 67 20 26 26 20 21 70 47 72 6f  pHaving && !pGro
1e8e7 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 73 71  upBy ){.      sq
1e8e8 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1e8e9 61 72 73 65 2c 20 22 61 20 47 52 4f 55 50 20 42  arse, "a GROUP B
1e8ea 59 20 63 6c 61 75 73 65 20 69 73 20 72 65 71 75  Y clause is requ
1e8eb 69 72 65 64 20 62 65 66 6f 72 65 20 48 41 56 49  ired before HAVI
1e8ec 4e 47 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75  NG");.      retu
1e8ed 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1e8ee 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 64    }.  .    /* Ad
1e8ef 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  d the expression
1e8f0 20 6c 69 73 74 20 74 6f 20 74 68 65 20 6e 61 6d   list to the nam
1e8f1 65 2d 63 6f 6e 74 65 78 74 20 62 65 66 6f 72 65  e-context before
1e8f2 20 70 61 72 73 69 6e 67 20 74 68 65 0a 20 20 20   parsing the.   
1e8f3 20 2a 2a 20 6f 74 68 65 72 20 65 78 70 72 65 73   ** other expres
1e8f4 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 4c  sions in the SEL
1e8f5 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  ECT statement. T
1e8f6 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 0a 20  his is so that. 
1e8f7 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e     ** expression
1e8f8 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  s in the WHERE c
1e8f9 6c 61 75 73 65 20 28 65 74 63 2e 29 20 63 61 6e  lause (etc.) can
1e8fa 20 72 65 66 65 72 20 74 6f 20 65 78 70 72 65 73   refer to expres
1e8fb 73 69 6f 6e 73 20 62 79 0a 20 20 20 20 2a 2a 20  sions by.    ** 
1e8fc 61 6c 69 61 73 65 73 20 69 6e 20 74 68 65 20 72  aliases in the r
1e8fd 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 20 20 2a  esult set..    *
1e8fe 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f 72 20 70  *.    ** Minor p
1e8ff 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 20 69 73  oint: If this is
1e900 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20   the case, then 
1e901 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 77  the expression w
1e902 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20 72 65  ill be.    ** re
1e903 2d 65 76 61 6c 75 61 74 65 64 20 66 6f 72 20 65  -evaluated for e
1e904 61 63 68 20 72 65 66 65 72 65 6e 63 65 20 74 6f  ach reference to
1e905 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   it..    */.    
1e906 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20 70 2d 3e  sNC.pEList = p->
1e907 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 66 28 20  pEList;.    if( 
1e908 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
1e909 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d  prNames(&sNC, p-
1e90a 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20 20 20 20  >pWhere) ||.    
1e90b 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76     sqlite3Resolv
1e90c 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
1e90d 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a 20 20 20   p->pHaving).   
1e90e 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1e90f 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
1e910 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  }..    /* The OR
1e911 44 45 52 20 42 59 20 61 6e 64 20 47 52 4f 55 50  DER BY and GROUP
1e912 20 42 59 20 63 6c 61 75 73 65 73 20 6d 61 79 20   BY clauses may 
1e913 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 74 65 72  not refer to ter
1e914 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20 6f 75 74  ms in.    ** out
1e915 65 72 20 71 75 65 72 69 65 73 20 0a 20 20 20 20  er queries .    
1e916 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 74  */.    sNC.pNext
1e917 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43 2e 61 6c   = 0;.    sNC.al
1e918 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a 20 20 20  lowAgg = 1;..   
1e919 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65 20   /* Process the 
1e91a 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
1e91b 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e 20 53 45  for singleton SE
1e91c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e  LECT statements.
1e91d 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f 52 44 45  .    ** The ORDE
1e91e 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20  R BY clause for 
1e91f 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c 45 43 54  compounds SELECT
1e920 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20 68   statements is h
1e921 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a 20 62 65  andled.    ** be
1e922 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f  low, after all o
1e923 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74  f the result-set
1e924 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65  s for all of the
1e925 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a 20 20 20   elements of.   
1e926 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   ** the compound
1e927 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c   have been resol
1e928 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ved..    */.    
1e929 69 66 28 20 21 69 73 43 6f 6d 70 6f 75 6e 64 20  if( !isCompound 
1e92a 26 26 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47  && resolveOrderG
1e92b 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 2c 20  roupBy(&sNC, p, 
1e92c 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52  p->pOrderBy, "OR
1e92d 44 45 52 22 29 20 29 7b 0a 20 20 20 20 20 20 72  DER") ){.      r
1e92e 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1e92f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
1e930 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1e931 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1e932 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d  WRC_Abort;.    }
1e933 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c  .  .    /* Resol
1e934 76 65 20 74 68 65 20 47 52 4f 55 50 20 42 59 20  ve the GROUP BY 
1e935 63 6c 61 75 73 65 2e 20 20 41 74 20 74 68 65 20  clause.  At the 
1e936 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61 6b 65 20  same time, make 
1e937 73 75 72 65 20 0a 20 20 20 20 2a 2a 20 74 68 65  sure .    ** the
1e938 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
1e939 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69   does not contai
1e93a 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  n aggregate func
1e93b 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  tions..    */.  
1e93c 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29    if( pGroupBy )
1e93d 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  {.      struct E
1e93e 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
1e93f 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20 20 20 20  tem;.    .      
1e940 69 66 28 20 72 65 73 6f 6c 76 65 4f 72 64 65 72  if( resolveOrder
1e941 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 2c  GroupBy(&sNC, p,
1e942 20 70 47 72 6f 75 70 42 79 2c 20 22 47 52 4f 55   pGroupBy, "GROU
1e943 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  P") || db->mallo
1e944 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
1e945 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1e946 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ort;.      }.   
1e947 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
1e948 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b 20 69  m=pGroupBy->a; i
1e949 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72  <pGroupBy->nExpr
1e94a 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
1e94b 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70  .        if( Exp
1e94c 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 49 74  rHasProperty(pIt
1e94d 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50 5f 41 67  em->pExpr, EP_Ag
1e94e 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  g) ){.          
1e94f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1e950 70 50 61 72 73 65 2c 20 22 61 67 67 72 65 67 61  pParse, "aggrega
1e951 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  te functions are
1e952 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20   not allowed in 
1e953 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
1e954 22 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c  "the GROUP BY cl
1e955 61 75 73 65 22 29 3b 0a 20 20 20 20 20 20 20 20  ause");.        
1e956 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
1e957 72 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rt;.        }.  
1e958 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
1e959 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74   /* Advance to t
1e95a 68 65 20 6e 65 78 74 20 74 65 72 6d 20 6f 66 20  he next term of 
1e95b 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a 20 20 20  the compound.   
1e95c 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70   */.    p = p->p
1e95d 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43 6f 6d 70  Prior;.    nComp
1e95e 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f  ound++;.  }..  /
1e95f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 4f 52  * Resolve the OR
1e960 44 45 52 20 42 59 20 6f 6e 20 61 20 63 6f 6d 70  DER BY on a comp
1e961 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 66 74 65  ound SELECT afte
1e962 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 0a 20  r all terms of. 
1e963 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   ** the compound
1e964 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c   have been resol
1e965 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ved..  */.  if( 
1e966 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65  isCompound && re
1e967 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64  solveCompoundOrd
1e968 65 72 42 79 28 70 50 61 72 73 65 2c 20 70 4c 65  erBy(pParse, pLe
1e969 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20 20 20 72  ftmost) ){.    r
1e96a 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1e96b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 57  .  }..  return W
1e96c 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a 2f 2a 0a  RC_Prune;.}../*.
1e96d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1e96e 77 61 6c 6b 73 20 61 6e 20 65 78 70 72 65 73 73  walks an express
1e96f 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 72 65 73  ion tree and res
1e970 6f 6c 76 65 73 20 72 65 66 65 72 65 6e 63 65 73  olves references
1e971 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 63 6f 6c   to.** table col
1e972 75 6d 6e 73 20 61 6e 64 20 72 65 73 75 6c 74 2d  umns and result-
1e973 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20 20 41 74  set columns.  At
1e974 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20   the same time, 
1e975 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63 68 65 63  do error.** chec
1e976 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74 69 6f 6e  king on function
1e977 20 75 73 61 67 65 20 61 6e 64 20 73 65 74 20 61   usage and set a
1e978 20 66 6c 61 67 20 69 66 20 61 6e 79 20 61 67 67   flag if any agg
1e979 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
1e97a 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2a  .** are seen..**
1e97b 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 20 74  .** To resolve t
1e97c 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 72 65 66  able columns ref
1e97d 65 72 65 6e 63 65 73 20 77 65 20 6c 6f 6f 6b 20  erences we look 
1e97e 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72 20 73 75  for nodes (or su
1e97f 62 74 72 65 65 73 29 20 6f 66 20 74 68 65 20 0a  btrees) of the .
1e980 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72  ** form X.Y.Z or
1e981 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 20 77   Y.Z or just Z w
1e982 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  here.**.**      
1e983 58 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66  X:   The name of
1e984 20 61 20 64 61 74 61 62 61 73 65 2e 20 20 45 78   a database.  Ex
1e985 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20 22 74 65  :  "main" or "te
1e986 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  mp" or.**       
1e987 20 20 20 20 74 68 65 20 73 79 6d 62 6f 6c 69 63      the symbolic
1e988 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74   name assigned t
1e989 6f 20 61 6e 20 41 54 54 41 43 48 2d 65 64 20 64  o an ATTACH-ed d
1e98a 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20  atabase..**.**  
1e98b 20 20 20 20 59 3a 20 20 20 54 68 65 20 6e 61 6d      Y:   The nam
1e98c 65 20 6f 66 20 61 20 74 61 62 6c 65 20 69 6e 20  e of a table in 
1e98d 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  a FROM clause.  
1e98e 4f 72 20 69 6e 20 61 20 74 72 69 67 67 65 72 0a  Or in a trigger.
1e98f 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 6e 65  **           one
1e990 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20   of the special 
1e991 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f 72 20 22  names "old" or "
1e992 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  new"..**.**     
1e993 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f   Z:   The name o
1e994 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61  f a column in ta
1e995 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ble Y..**.** The
1e996 20 6e 6f 64 65 20 61 74 20 74 68 65 20 72 6f 6f   node at the roo
1e997 74 20 6f 66 20 74 68 65 20 73 75 62 74 72 65 65  t of the subtree
1e998 20 69 73 20 6d 6f 64 69 66 69 65 64 20 61 73 20   is modified as 
1e999 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
1e99a 20 20 45 78 70 72 2e 6f 70 20 20 20 20 20 20 20    Expr.op       
1e99b 20 43 68 61 6e 67 65 64 20 74 6f 20 54 4b 5f 43   Changed to TK_C
1e99c 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45 78 70 72  OLUMN.**    Expr
1e99d 2e 70 54 61 62 20 20 20 20 20 20 50 6f 69 6e 74  .pTab      Point
1e99e 73 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 6f  s to the Table o
1e99f 62 6a 65 63 74 20 66 6f 72 20 58 2e 59 0a 2a 2a  bject for X.Y.**
1e9a0 20 20 20 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e      Expr.iColumn
1e9a1 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 69 6e     The column in
1e9a2 64 65 78 20 69 6e 20 58 2e 59 2e 20 20 2d 31 20  dex in X.Y.  -1 
1e9a3 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a  for the rowid..*
1e9a4 2a 20 20 20 20 45 78 70 72 2e 69 54 61 62 6c 65  *    Expr.iTable
1e9a5 20 20 20 20 54 68 65 20 56 44 42 45 20 63 75 72      The VDBE cur
1e9a6 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 58  sor number for X
1e9a7 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 6f 20 72  .Y.**.**.** To r
1e9a8 65 73 6f 6c 76 65 20 72 65 73 75 6c 74 2d 73 65  esolve result-se
1e9a9 74 20 72 65 66 65 72 65 6e 63 65 73 2c 20 6c 6f  t references, lo
1e9aa 6f 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f  ok for expressio
1e9ab 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 0a 2a  n nodes of the.*
1e9ac 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74 68 20 6e  * form Z (with n
1e9ad 6f 20 58 20 61 6e 64 20 59 20 70 72 65 66 69 78  o X and Y prefix
1e9ae 29 20 77 68 65 72 65 20 74 68 65 20 5a 20 6d 61  ) where the Z ma
1e9af 74 63 68 65 73 20 74 68 65 20 72 69 67 68 74 2d  tches the right-
1e9b0 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  hand.** size of 
1e9b1 61 6e 20 41 53 20 63 6c 61 75 73 65 20 69 6e 20  an AS clause in 
1e9b2 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 6f  the result-set o
1e9b3 66 20 61 20 53 45 4c 45 43 54 2e 20 20 54 68 65  f a SELECT.  The
1e9b4 20 5a 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a   Z expression.**
1e9b5 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20   is replaced by 
1e9b6 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6c 65  a copy of the le
1e9b7 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ft-hand side of 
1e9b8 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65  the result-set e
1e9b9 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 61  xpression..** Ta
1e9ba 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20 66 75 6e  ble-name and fun
1e9bb 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74 69 6f 6e  ction resolution
1e9bc 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 20 73   occurs on the s
1e9bd 75 62 73 74 69 74 75 74 65 64 20 65 78 70 72 65  ubstituted expre
1e9be 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 20 20  ssion.** tree.  
1e9bf 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e 3a  For example, in:
1e9c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45  .**.**      SELE
1e9c1 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64  CT a+b AS x, c+d
1e9c2 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52   AS y FROM t1 OR
1e9c3 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a 2a 2a 20  DER BY x;.**.** 
1e9c4 54 68 65 20 22 78 22 20 74 65 72 6d 20 6f 66 20  The "x" term of 
1e9c5 74 68 65 20 6f 72 64 65 72 20 62 79 20 69 73 20  the order by is 
1e9c6 72 65 70 6c 61 63 65 64 20 62 79 20 22 61 2b 62  replaced by "a+b
1e9c7 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a 2a 2a 0a  " to render:.**.
1e9c8 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54 20 61  **      SELECT a
1e9c9 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20  +b AS x, c+d AS 
1e9ca 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20  y FROM t1 ORDER 
1e9cb 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a 20 46 75  BY a+b;.**.** Fu
1e9cc 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 61 72 65  nction calls are
1e9cd 20 63 68 65 63 6b 65 64 20 74 6f 20 6d 61 6b 65   checked to make
1e9ce 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 66   sure that the f
1e9cf 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a 2a 20 64  unction is .** d
1e9d0 65 66 69 6e 65 64 20 61 6e 64 20 74 68 61 74 20  efined and that 
1e9d1 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62  the correct numb
1e9d2 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
1e9d3 61 72 65 20 73 70 65 63 69 66 69 65 64 2e 0a 2a  are specified..*
1e9d4 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * If the functio
1e9d5 6e 20 69 73 20 61 6e 20 61 67 67 72 65 67 61 74  n is an aggregat
1e9d6 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e  e function, then
1e9d7 20 74 68 65 20 70 4e 43 2d 3e 68 61 73 41 67 67   the pNC->hasAgg
1e9d8 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e 64 20 74   is.** set and t
1e9d9 68 65 20 6f 70 63 6f 64 65 20 69 73 20 63 68 61  he opcode is cha
1e9da 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f 46 55 4e  nged from TK_FUN
1e9db 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41 47 47 5f  CTION to TK_AGG_
1e9dc 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20 49 66 20  FUNCTION..** If 
1e9dd 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f  an expression co
1e9de 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65  ntains aggregate
1e9df 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 65 6e 20   functions then 
1e9e0 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a 20 70 72  the EP_Agg.** pr
1e9e1 6f 70 65 72 74 79 20 6f 6e 20 74 68 65 20 65 78  operty on the ex
1e9e2 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 74 2e  pression is set.
1e9e3 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20  .**.** An error 
1e9e4 6d 65 73 73 61 67 65 20 69 73 20 6c 65 66 74 20  message is left 
1e9e5 69 6e 20 70 50 61 72 73 65 20 69 66 20 61 6e 79  in pParse if any
1e9e6 74 68 69 6e 67 20 69 73 20 61 6d 69 73 73 2e 20  thing is amiss. 
1e9e7 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 69   The number.** i
1e9e8 66 20 65 72 72 6f 72 73 20 69 73 20 72 65 74 75  f errors is retu
1e9e9 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
1e9ea 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1e9eb 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
1e9ec 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43 6f 6e 74  mes( .  NameCont
1e9ed 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 20 20  ext *pNC,       
1e9ee 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 74 6f 20  /* Namespace to 
1e9ef 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69  resolve expressi
1e9f0 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20 45 78 70  ons in. */.  Exp
1e9f1 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
1e9f2 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
1e9f3 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61  ession to be ana
1e9f4 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a 20 20 69  lyzed. */.){.  i
1e9f5 6e 74 20 73 61 76 65 64 48 61 73 41 67 67 3b 0a  nt savedHasAgg;.
1e9f6 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 69    Walker w;..  i
1e9f7 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65  f( pExpr==0 ) re
1e9f8 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49  turn 0;.#if SQLI
1e9f9 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
1e9fa 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50 61 72 73  H>0.  {.    Pars
1e9fb 65 20 2a 70 50 61 72 73 65 20 3d 20 70 4e 43 2d  e *pParse = pNC-
1e9fc 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 69 66 28  >pParse;.    if(
1e9fd 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63   sqlite3ExprChec
1e9fe 6b 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  kHeight(pParse, 
1e9ff 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 2b 70  pExpr->nHeight+p
1ea00 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69  NC->pParse->nHei
1ea01 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ght) ){.      re
1ea02 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
1ea03 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68    pParse->nHeigh
1ea04 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69  t += pExpr->nHei
1ea05 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ght;.  }.#endif.
1ea06 20 20 73 61 76 65 64 48 61 73 41 67 67 20 3d 20    savedHasAgg = 
1ea07 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a 20 20 70  pNC->hasAgg;.  p
1ea08 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 30 3b 0a  NC->hasAgg = 0;.
1ea09 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63    w.xExprCallbac
1ea0a 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53  k = resolveExprS
1ea0b 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74  tep;.  w.xSelect
1ea0c 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c  Callback = resol
1ea0d 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20  veSelectStep;.  
1ea0e 77 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e  w.pParse = pNC->
1ea0f 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e  pParse;.  w.u.pN
1ea10 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71 6c 69 74  C = pNC;.  sqlit
1ea11 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70  e3WalkExpr(&w, p
1ea12 45 78 70 72 29 3b 0a 23 69 66 20 53 51 4c 49 54  Expr);.#if SQLIT
1ea13 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
1ea14 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 72 73 65  >0.  pNC->pParse
1ea15 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 70 45 78  ->nHeight -= pEx
1ea16 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23 65 6e  pr->nHeight;.#en
1ea17 64 69 66 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e  dif.  if( pNC->n
1ea18 45 72 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70  Err>0 ){.    Exp
1ea19 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78  rSetProperty(pEx
1ea1a 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20  pr, EP_Error);. 
1ea1b 20 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68 61   }.  if( pNC->ha
1ea1c 73 41 67 67 20 29 7b 0a 20 20 20 20 45 78 70 72  sAgg ){.    Expr
1ea1d 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70  SetProperty(pExp
1ea1e 72 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65  r, EP_Agg);.  }e
1ea1f 6c 73 65 20 69 66 28 20 73 61 76 65 64 48 61 73  lse if( savedHas
1ea20 41 67 67 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e  Agg ){.    pNC->
1ea21 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a  hasAgg = 1;.  }.
1ea22 20 20 72 65 74 75 72 6e 20 45 78 70 72 48 61 73    return ExprHas
1ea23 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
1ea24 45 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f  EP_Error);.}.../
1ea25 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c  *.** Resolve all
1ea26 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 65 78   names in all ex
1ea27 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 20 53  pressions of a S
1ea28 45 4c 45 43 54 20 61 6e 64 20 69 6e 20 61 6c 6c  ELECT and in all
1ea29 0a 2a 2a 20 64 65 63 65 6e 64 65 6e 74 73 20 6f  .** decendents o
1ea2a 66 20 74 68 65 20 53 45 4c 45 43 54 2c 20 69 6e  f the SELECT, in
1ea2b 63 6c 75 64 69 6e 67 20 63 6f 6d 70 6f 75 6e 64  cluding compound
1ea2c 73 20 6f 66 66 20 6f 66 20 70 2d 3e 70 50 72 69  s off of p->pPri
1ea2d 6f 72 2c 0a 2a 2a 20 73 75 62 71 75 65 72 69 65  or,.** subquerie
1ea2e 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73  s in expressions
1ea2f 2c 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73  , and subqueries
1ea30 20 75 73 65 64 20 61 73 20 46 52 4f 4d 20 63 6c   used as FROM cl
1ea31 61 75 73 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a 2a  ause.** terms..*
1ea32 2a 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65 33  *.** See sqlite3
1ea33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
1ea34 28 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70  () for a descrip
1ea35 74 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64  tion of the kind
1ea36 73 20 6f 66 0a 2a 2a 20 74 72 61 6e 73 66 6f 72  s of.** transfor
1ea37 6d 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63 63  mations that occ
1ea38 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 45  ur..**.** All SE
1ea39 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
1ea3a 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e  should have been
1ea3b 20 65 78 70 61 6e 64 65 64 20 75 73 69 6e 67 0a   expanded using.
1ea3c 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ** sqlite3Select
1ea3d 45 78 70 61 6e 64 28 29 20 70 72 69 6f 72 20 74  Expand() prior t
1ea3e 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20  o invoking this 
1ea3f 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49  routine..*/.SQLI
1ea40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1ea41 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65  sqlite3ResolveSe
1ea42 6c 65 63 74 4e 61 6d 65 73 28 0a 20 20 50 61 72  lectNames(.  Par
1ea43 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
1ea44 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
1ea45 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
1ea46 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
1ea47 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
1ea48 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65  ECT statement be
1ea49 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20  ing coded. */.  
1ea4a 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75  NameContext *pOu
1ea4b 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63  terNC  /* Name c
1ea4c 6f 6e 74 65 78 74 20 66 6f 72 20 70 61 72 65 6e  ontext for paren
1ea4d 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  t SELECT stateme
1ea4e 6e 74 20 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b 65  nt */.){.  Walke
1ea4f 72 20 77 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  r w;..  assert( 
1ea50 70 21 3d 30 20 29 3b 0a 20 20 77 2e 78 45 78 70  p!=0 );.  w.xExp
1ea51 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f  rCallback = reso
1ea52 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 77  lveExprStep;.  w
1ea53 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
1ea54 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74   = resolveSelect
1ea55 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 65  Step;.  w.pParse
1ea56 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 77 2e 75   = pParse;.  w.u
1ea57 2e 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b  .pNC = pOuterNC;
1ea58 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  .  sqlite3WalkSe
1ea59 6c 65 63 74 28 26 77 2c 20 70 29 3b 0a 7d 0a 0a  lect(&w, p);.}..
1ea5a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1ea5b 45 6e 64 20 6f 66 20 72 65 73 6f 6c 76 65 2e 63  End of resolve.c
1ea5c 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1ea5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1ea5f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1ea60 42 65 67 69 6e 20 66 69 6c 65 20 65 78 70 72 2e  Begin file expr.
1ea61 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1ea62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1ea64 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
1ea65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
1ea66 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1ea67 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1ea68 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1ea69 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1ea6a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1ea6b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1ea6c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1ea6d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1ea6e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1ea6f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1ea70 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1ea71 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1ea72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1ea73 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1ea74 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1ea75 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1ea76 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1ea77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea7b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
1ea7c 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75  ile contains rou
1ea7d 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 61  tines used for a
1ea7e 6e 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73 73  nalyzing express
1ea7f 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20  ions and.** for 
1ea80 67 65 6e 65 72 61 74 69 6e 67 20 56 44 42 45 20  generating VDBE 
1ea81 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 61  code that evalua
1ea82 74 65 73 20 65 78 70 72 65 73 73 69 6f 6e 73 20  tes expressions 
1ea83 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  in SQLite..**.**
1ea84 20 24 49 64 3a 20 65 78 70 72 2e 63 2c 76 20 31   $Id: expr.c,v 1
1ea85 2e 34 33 32 2e 32 2e 31 20 32 30 30 39 2f 30 35  .432.2.1 2009/05
1ea86 2f 32 35 20 31 32 3a 30 32 3a 32 34 20 64 72 68  /25 12:02:24 drh
1ea87 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
1ea88 20 52 65 74 75 72 6e 20 74 68 65 20 27 61 66 66   Return the 'aff
1ea89 69 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65 78  inity' of the ex
1ea8a 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69  pression pExpr i
1ea8b 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  f any..**.** If 
1ea8c 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d  pExpr is a colum
1ea8d 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  n, a reference t
1ea8e 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 61  o a column via a
1ea8f 6e 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a 2a  n 'AS' alias,.**
1ea90 20 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63 74   or a sub-select
1ea91 20 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 61   with a column a
1ea92 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  s the return val
1ea93 75 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a  ue, then the .**
1ea94 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61   affinity of tha
1ea95 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75  t column is retu
1ea96 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
1ea97 20 30 78 30 30 20 69 73 20 72 65 74 75 72 6e 65   0x00 is returne
1ea98 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67  d,.** indicating
1ea99 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f 72   no affinity for
1ea9a 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e   the expression.
1ea9b 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20  .**.** i.e. the 
1ea9c 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70  WHERE clause exp
1ea9d 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  resssions in the
1ea9e 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65   following state
1ea9f 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 76  ments all.** hav
1eaa0 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a 2a  e an affinity:.*
1eaa1 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c  *.** CREATE TABL
1eaa2 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c 45  E t1(a);.** SELE
1eaa3 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45  CT * FROM t1 WHE
1eaa4 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 20  RE a;.** SELECT 
1eaa5 61 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20 57  a AS b FROM t1 W
1eaa6 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 43  HERE b;.** SELEC
1eaa7 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52  T * FROM t1 WHER
1eaa8 45 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f 6d  E (select a from
1eaa9 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f   t1);.*/.SQLITE_
1eaaa 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c  PRIVATE char sql
1eaab 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
1eaac 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20  (Expr *pExpr){. 
1eaad 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 2d   int op = pExpr-
1eaae 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54  >op;.  if( op==T
1eaaf 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20  K_SELECT ){.    
1eab0 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 66  assert( pExpr->f
1eab1 6c 61 67 73 26 45 50 5f 78 49 73 53 65 6c 65 63  lags&EP_xIsSelec
1eab2 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  t );.    return 
1eab3 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
1eab4 69 74 79 28 70 45 78 70 72 2d 3e 78 2e 70 53 65  ity(pExpr->x.pSe
1eab5 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  lect->pEList->a[
1eab6 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a 23  0].pExpr);.  }.#
1eab7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1eab8 49 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f 70  IT_CAST.  if( op
1eab9 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20 20  ==TK_CAST ){.   
1eaba 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41   return sqlite3A
1eabb 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45 78  ffinityType(&pEx
1eabc 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d 0a  pr->token);.  }.
1eabd 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f 70  #endif.  if( (op
1eabe 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  ==TK_AGG_COLUMN 
1eabf 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  || op==TK_COLUMN
1eac0 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53   || op==TK_REGIS
1eac1 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78 70  TER) .   && pExp
1eac2 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b 0a  r->pTab!=0.  ){.
1eac3 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45      /* op==TK_RE
1eac4 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72 2d  GISTER && pExpr-
1eac5 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73  >pTab!=0 happens
1eac6 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20   when pExpr was 
1eac7 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 2a  originally.    *
1eac8 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75  * a TK_COLUMN bu
1eac9 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  t was previously
1eaca 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63   evaluated and c
1eacb 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 73  ached in a regis
1eacc 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6a  ter */.    int j
1eacd 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   = pExpr->iColum
1eace 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20 29  n;.    if( j<0 )
1eacf 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41   return SQLITE_A
1ead0 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  FF_INTEGER;.    
1ead1 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
1ead2 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d 3e  Tab && j<pExpr->
1ead3 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20  pTab->nCol );.  
1ead4 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d 3e    return pExpr->
1ead5 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61 66  pTab->aCol[j].af
1ead6 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72 65  finity;.  }.  re
1ead7 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66 69  turn pExpr->affi
1ead8 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  nity;.}../*.** S
1ead9 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  et the collating
1eada 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 78   sequence for ex
1eadb 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 74  pression pExpr t
1eadc 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74 69  o be the collati
1eadd 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6e  ng.** sequence n
1eade 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e 20  amed by pToken. 
1eadf 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
1eae0 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73 65  er to the revise
1eae1 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  d expression..**
1eae2 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   The collating s
1eae3 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b 65  equence is marke
1eae4 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22 20  d as "explicit" 
1eae5 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78 70  using the EP_Exp
1eae6 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67 2e  Collate.** flag.
1eae7 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63 6f    An explicit co
1eae8 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
1eae9 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20 69   will override i
1eaea 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c 61  mplicit.** colla
1eaeb 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e 0a  ting sequences..
1eaec 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1eaed 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
1eaee 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65  xprSetColl(Parse
1eaef 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
1eaf0 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 43  pExpr, Token *pC
1eaf1 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72  ollName){.  char
1eaf2 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 20   *zColl = 0;    
1eaf3 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f          /* Dequo
1eaf4 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c  ted name of coll
1eaf5 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a  ation sequence *
1eaf6 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  /.  CollSeq *pCo
1eaf7 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ll;.  sqlite3 *d
1eaf8 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1eaf9 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    zColl = sqlite
1eafa 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
1eafb 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a 20  b, pCollName);. 
1eafc 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a 43   if( pExpr && zC
1eafd 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c  oll ){.    pColl
1eafe 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
1eaff 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
1eb00 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20 20 20 20  zColl, -1);.    
1eb01 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
1eb02 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20     pExpr->pColl 
1eb03 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70  = pColl;.      p
1eb04 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Expr->flags |= E
1eb05 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
1eb06 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
1eb07 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c  3DbFree(db, zCol
1eb08 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 45 78  l);.  return pEx
1eb09 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  pr;.}../*.** Ret
1eb0a 75 72 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20  urn the default 
1eb0b 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
1eb0c 63 65 20 66 6f 72 20 74 68 65 20 65 78 70 72 65  ce for the expre
1eb0d 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 49 66 0a  ssion pExpr. If.
1eb0e 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64  ** there is no d
1eb0f 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e  efault collation
1eb10 20 74 79 70 65 2c 20 72 65 74 75 72 6e 20 30 2e   type, return 0.
1eb11 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1eb12 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
1eb13 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 50  te3ExprCollSeq(P
1eb14 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
1eb15 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 43 6f  pr *pExpr){.  Co
1eb16 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30  llSeq *pColl = 0
1eb17 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 70 45  ;.  Expr *p = pE
1eb18 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 20  xpr;.  while( p 
1eb19 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0a 20  ){.    int op;. 
1eb1a 20 20 20 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70 43     pColl = p->pC
1eb1b 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 20 70 43 6f  oll;.    if( pCo
1eb1c 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  ll ) break;.    
1eb1d 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20 20  op = p->op;.    
1eb1e 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47 5f  if( (op==TK_AGG_
1eb1f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b  COLUMN || op==TK
1eb20 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54  _COLUMN || op==T
1eb21 4b 5f 52 45 47 49 53 54 45 52 29 20 26 26 20 70  K_REGISTER) && p
1eb22 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20 20  ->pTab!=0 ){.   
1eb23 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45 47     /* op==TK_REG
1eb24 49 53 54 45 52 20 26 26 20 70 2d 3e 70 54 61 62  ISTER && p->pTab
1eb25 21 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e  !=0 happens when
1eb26 20 70 45 78 70 72 20 77 61 73 20 6f 72 69 67 69   pExpr was origi
1eb27 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 61  nally.      ** a
1eb28 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 77   TK_COLUMN but w
1eb29 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65 76  as previously ev
1eb2a 61 6c 75 61 74 65 64 20 61 6e 64 20 63 61 63 68  aluated and cach
1eb2b 65 64 20 69 6e 20 61 20 72 65 67 69 73 74 65 72  ed in a register
1eb2c 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   */.      const 
1eb2d 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20  char *zColl;.   
1eb2e 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d 3e 69 43     int j = p->iC
1eb2f 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66 28  olumn;.      if(
1eb30 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   j>=0 ){.       
1eb31 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1eb32 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20  Parse->db;.     
1eb33 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d 3e 70 54     zColl = p->pT
1eb34 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c  ab->aCol[j].zCol
1eb35 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  l;.        pColl
1eb36 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
1eb37 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62  llSeq(db, ENC(db
1eb38 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30 29  ), zColl, -1, 0)
1eb39 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  ;.        pExpr-
1eb3a 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a  >pColl = pColl;.
1eb3b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1eb3c 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
1eb3d 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 26  f( op!=TK_CAST &
1eb3e 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 29  & op!=TK_UPLUS )
1eb3f 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  {.      break;. 
1eb40 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e     }.    p = p->
1eb41 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 28  pLeft;.  }.  if(
1eb42 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c   sqlite3CheckCol
1eb43 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f  lSeq(pParse, pCo
1eb44 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f 6c  ll) ){ .    pCol
1eb45 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  l = 0;.  }.  ret
1eb46 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a  urn pColl;.}../*
1eb47 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e 20  .** pExpr is an 
1eb48 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f 6d  operand of a com
1eb49 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
1eb4a 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a 2a  .  aff2 is the.*
1eb4b 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20  * type affinity 
1eb4c 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 65  of the other ope
1eb4d 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74  rand.  This rout
1eb4e 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a  ine returns the.
1eb4f 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79  ** type affinity
1eb50 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
1eb51 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d  used for the com
1eb52 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
1eb53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1eb54 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
1eb55 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
1eb56 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
1eb57 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 20  r aff2){.  char 
1eb58 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 78  aff1 = sqlite3Ex
1eb59 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  prAffinity(pExpr
1eb5a 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 26  );.  if( aff1 &&
1eb5b 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20   aff2 ){.    /* 
1eb5c 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 68  Both sides of th
1eb5d 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65  e comparison are
1eb5e 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65   columns. If one
1eb5f 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 20   has numeric.   
1eb60 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 73   ** affinity, us
1eb61 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 73  e that. Otherwis
1eb62 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 74  e use no affinit
1eb63 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  y..    */.    if
1eb64 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72  ( sqlite3IsNumer
1eb65 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 29  icAffinity(aff1)
1eb66 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 6d   || sqlite3IsNum
1eb67 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66  ericAffinity(aff
1eb68 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  2) ){.      retu
1eb69 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  rn SQLITE_AFF_NU
1eb6a 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 65  MERIC;.    }else
1eb6b 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
1eb6c 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
1eb6d 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
1eb6e 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66 32  ( !aff1 && !aff2
1eb6f 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68   ){.    /* Neith
1eb70 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 63  er side of the c
1eb71 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 63  omparison is a c
1eb72 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 20  olumn.  Compare 
1eb73 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c  the.    ** resul
1eb74 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20  ts directly..   
1eb75 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53   */.    return S
1eb76 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
1eb77 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1eb78 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 6f  One side is a co
1eb79 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 20  lumn, the other 
1eb7a 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 20  is not. Use the 
1eb7b 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79  columns affinity
1eb7c 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
1eb7d 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32   aff1==0 || aff2
1eb7e 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ==0 );.    retur
1eb7f 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 3b  n (aff1 + aff2);
1eb80 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45  .  }.}../*.** pE
1eb81 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69  xpr is a compari
1eb82 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 52  son operator.  R
1eb83 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 61  eturn the type a
1eb84 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f  ffinity that sho
1eb85 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 65  uld.** be applie
1eb86 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 6e  d to both operan
1eb87 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e  ds prior to doin
1eb88 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  g the comparison
1eb89 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
1eb8a 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e   comparisonAffin
1eb8b 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29  ity(Expr *pExpr)
1eb8c 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 20  {.  char aff;.  
1eb8d 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
1eb8e 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 70  p==TK_EQ || pExp
1eb8f 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20  r->op==TK_IN || 
1eb90 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54  pExpr->op==TK_LT
1eb91 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45   ||.          pE
1eb92 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 7c  xpr->op==TK_GT |
1eb93 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  | pExpr->op==TK_
1eb94 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d  GE || pExpr->op=
1eb95 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 20  =TK_LE ||.      
1eb96 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54      pExpr->op==T
1eb97 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74  K_NE );.  assert
1eb98 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 29  ( pExpr->pLeft )
1eb99 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65  ;.  aff = sqlite
1eb9a 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45  3ExprAffinity(pE
1eb9b 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69  xpr->pLeft);.  i
1eb9c 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  f( pExpr->pRight
1eb9d 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 71   ){.    aff = sq
1eb9e 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
1eb9f 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 67  nity(pExpr->pRig
1eba0 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73  ht, aff);.  }els
1eba1 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f  e if( ExprHasPro
1eba2 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1eba3 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
1eba4 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43    aff = sqlite3C
1eba5 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
1eba6 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d  Expr->x.pSelect-
1eba7 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
1eba8 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c  xpr, aff);.  }el
1eba9 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20  se if( !aff ){. 
1ebaa 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
1ebab 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20  AFF_NONE;.  }.  
1ebac 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f  return aff;.}../
1ebad 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20  *.** pExpr is a 
1ebae 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65  comparison expre
1ebaf 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20  ssion, eg. '=', 
1ebb0 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63  '<', IN(...) etc
1ebb1 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74  ..** idx_affinit
1ebb2 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74  y is the affinit
1ebb3 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20  y of an indexed 
1ebb4 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74  column. Return t
1ebb5 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e  rue.** if the in
1ebb6 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74  dex with affinit
1ebb7 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d  y idx_affinity m
1ebb8 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d  ay be used to im
1ebb9 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63  plement.** the c
1ebba 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78  omparison in pEx
1ebbb 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  pr..*/.SQLITE_PR
1ebbc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1ebbd 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b  3IndexAffinityOk
1ebbe 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
1ebbf 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29  ar idx_affinity)
1ebc0 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63  {.  char aff = c
1ebc1 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74  omparisonAffinit
1ebc2 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74  y(pExpr);.  swit
1ebc3 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63  ch( aff ){.    c
1ebc4 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ase SQLITE_AFF_N
1ebc5 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72  ONE:.      retur
1ebc6 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51  n 1;.    case SQ
1ebc7 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20  LITE_AFF_TEXT:. 
1ebc8 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f       return idx_
1ebc9 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
1ebca 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64  _AFF_TEXT;.    d
1ebcb 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65  efault:.      re
1ebcc 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75  turn sqlite3IsNu
1ebcd 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64  mericAffinity(id
1ebce 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d  x_affinity);.  }
1ebcf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1ebd0 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 68   the P5 value th
1ebd1 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  at should be use
1ebd2 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63  d for a binary c
1ebd3 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63  omparison.** opc
1ebd4 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47  ode (OP_Eq, OP_G
1ebd5 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20  e etc.) used to 
1ebd6 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61  compare pExpr1 a
1ebd7 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 74  nd pExpr2..*/.st
1ebd8 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 6f  atic u8 binaryCo
1ebd9 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 45  mpareP5(Expr *pE
1ebda 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 70  xpr1, Expr *pExp
1ebdb 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75  r2, int jumpIfNu
1ebdc 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d 20  ll){.  u8 aff = 
1ebdd 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 70  (char)sqlite3Exp
1ebde 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32  rAffinity(pExpr2
1ebdf 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 73  );.  aff = (u8)s
1ebe0 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
1ebe1 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66  inity(pExpr1, af
1ebe2 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 4e  f) | (u8)jumpIfN
1ebe3 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 66  ull;.  return af
1ebe4 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  f;.}../*.** Retu
1ebe5 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
1ebe6 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
1ebe7 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75  quence that shou
1ebe8 6c 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a  ld be used by.**
1ebe9 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72   a binary compar
1ebea 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f  ison operator co
1ebeb 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e  mparing pLeft an
1ebec 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20  d pRight..**.** 
1ebed 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64  If the left hand
1ebee 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20   expression has 
1ebef 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  a collating sequ
1ebf0 65 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20  ence type, then 
1ebf1 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f  it is.** used. O
1ebf2 74 68 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c  therwise the col
1ebf3 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
1ebf4 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 61  for the right ha
1ebf5 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  nd expression.**
1ebf6 20 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65   is used, or the
1ebf7 20 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59   default (BINARY
1ebf8 29 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 70  ) if neither exp
1ebf9 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f  ression has a co
1ebfa 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e  llating.** type.
1ebfb 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  .**.** Argument 
1ebfc 70 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20  pRight (but not 
1ebfd 70 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 20  pLeft) may be a 
1ebfe 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e  null pointer. In
1ebff 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69   this case,.** i
1ec00 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65  t is not conside
1ec01 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  red..*/.SQLITE_P
1ec02 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
1ec03 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
1ec04 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50  pareCollSeq(.  P
1ec05 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20  arse *pParse, . 
1ec06 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20   Expr *pLeft, . 
1ec07 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b   Expr *pRight.){
1ec08 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
1ec09 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65  l;.  assert( pLe
1ec0a 66 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66  ft );.  if( pLef
1ec0b 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  t->flags & EP_Ex
1ec0c 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
1ec0d 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70  assert( pLeft->p
1ec0e 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c  Coll );.    pCol
1ec0f 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c  l = pLeft->pColl
1ec10 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52  ;.  }else if( pR
1ec11 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e  ight && pRight->
1ec12 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
1ec13 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73  llate ){.    ass
1ec14 65 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f  ert( pRight->pCo
1ec15 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20  ll );.    pColl 
1ec16 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b  = pRight->pColl;
1ec17 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43  .  }else{.    pC
1ec18 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
1ec19 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
1ec1a 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28   pLeft);.    if(
1ec1b 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
1ec1c 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
1ec1d 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
1ec1e 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20  se, pRight);.   
1ec1f 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1ec20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pColl;.}../*.** 
1ec21 47 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 65  Generate the ope
1ec22 72 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d 70  rands for a comp
1ec23 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f 6e  arison operation
1ec24 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 6e  .  Before.** gen
1ec25 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65  erating the code
1ec26 20 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 6e   for each operan
1ec27 64 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 6e  d, set the EP_An
1ec28 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20  yAff.** flag on 
1ec29 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73  the expression s
1ec2a 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62  o that it will b
1ec2b 65 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 61  e able to used a
1ec2c 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 6d  .** cached colum
1ec2d 6e 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 73  n value that has
1ec2e 20 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 65   previously unde
1ec2f 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 69  rgone an.** affi
1ec30 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a  nity change..*/.
1ec31 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
1ec32 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28  CompareOperands(
1ec33 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1ec34 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  ,    /* Parsing 
1ec35 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74  and code generat
1ec36 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
1ec37 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20   Expr *pLeft,   
1ec38 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f     /* The left o
1ec39 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
1ec3a 2a 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f 2a  *pRegLeft,    /*
1ec3b 20 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20   Register where 
1ec3c 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20  left operand is 
1ec3d 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  stored */.  int 
1ec3e 2a 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f 2a  *pFreeLeft,   /*
1ec3f 20 46 72 65 65 20 74 68 69 73 20 72 65 67 69 73   Free this regis
1ec40 74 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a 2f  ter when done */
1ec41 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c  .  Expr *pRight,
1ec42 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68       /* The righ
1ec43 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  t operand */.  i
1ec44 6e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 20  nt *pRegRight,  
1ec45 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65   /* Register whe
1ec46 72 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64  re right operand
1ec47 20 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20   is stored */.  
1ec48 69 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 20  int *pFreeRight 
1ec49 20 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 20    /* Write temp 
1ec4a 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 67  register for rig
1ec4b 68 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 65  ht operand there
1ec4c 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20   */.){.  while( 
1ec4d 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50  pLeft->op==TK_UP
1ec4e 4c 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 4c  LUS ) pLeft = pL
1ec4f 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 4c  eft->pLeft;.  pL
1ec50 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  eft->flags |= EP
1ec51 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67  _AnyAff;.  *pReg
1ec52 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78  Left = sqlite3Ex
1ec53 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
1ec54 65 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 4c  e, pLeft, pFreeL
1ec55 65 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 70  eft);.  while( p
1ec56 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50  Right->op==TK_UP
1ec57 4c 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 70  LUS ) pRight = p
1ec58 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 20  Right->pLeft;.  
1ec59 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c 3d  pRight->flags |=
1ec5a 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70   EP_AnyAff;.  *p
1ec5b 52 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 74  RegRight = sqlit
1ec5c 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
1ec5d 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 70  Parse, pRight, p
1ec5e 46 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f  FreeRight);.}../
1ec5f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
1ec60 64 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69  de for a compari
1ec61 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f  son operator..*/
1ec62 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65  .static int code
1ec63 43 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 65  Compare(.  Parse
1ec64 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20   *pParse,    /* 
1ec65 54 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e 64  The parsing (and
1ec66 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67   code generating
1ec67 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  ) context */.  E
1ec68 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20  xpr *pLeft,     
1ec69 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65   /* The left ope
1ec6a 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a  rand */.  Expr *
1ec6b 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 54  pRight,     /* T
1ec6c 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64  he right operand
1ec6d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65   */.  int opcode
1ec6e 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  ,       /* The c
1ec6f 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 65  omparison opcode
1ec70 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 69   */.  int in1, i
1ec71 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 73  nt in2, /* Regis
1ec72 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 72  ter holding oper
1ec73 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 65  ands */.  int de
1ec74 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a  st,         /* J
1ec75 75 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 65  ump here if true
1ec76 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70  .  */.  int jump
1ec77 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 20  IfNull    /* If 
1ec78 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 69  true, jump if ei
1ec79 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
1ec7a 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  NULL */.){.  int
1ec7b 20 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b   p5;.  int addr;
1ec7c 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a  .  CollSeq *p4;.
1ec7d 0a 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 42  .  p4 = sqlite3B
1ec7e 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
1ec7f 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66  Seq(pParse, pLef
1ec80 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 35  t, pRight);.  p5
1ec81 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65   = binaryCompare
1ec82 50 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 74  P5(pLeft, pRight
1ec83 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
1ec84 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
1ec85 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 65  dbeAddOp4(pParse
1ec86 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 2c  ->pVdbe, opcode,
1ec87 20 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 2c   in2, dest, in1,
1ec88 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1ec89 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69              (voi
1ec8a 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 45  d*)p4, P4_COLLSE
1ec8b 51 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  Q);.  sqlite3Vdb
1ec8c 65 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 65  eChangeP5(pParse
1ec8d 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 29  ->pVdbe, (u8)p5)
1ec8e 3b 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 51  ;.  if( (p5 & SQ
1ec8f 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 3d  LITE_AFF_MASK)!=
1ec90 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
1ec91 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
1ec92 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
1ec93 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e  hange(pParse, in
1ec94 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  1, 1);.    sqlit
1ec95 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
1ec96 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
1ec97 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a 20  , in2, 1);.  }. 
1ec98 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a   return addr;.}.
1ec99 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
1ec9a 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a  EXPR_DEPTH>0./*.
1ec9b 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 72  ** Check that ar
1ec9c 67 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 69  gument nHeight i
1ec9d 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
1ec9e 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69  qual to the maxi
1ec9f 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  mum.** expressio
1eca0 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 2e  n depth allowed.
1eca1 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 6c   If it is not, l
1eca2 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  eave an error me
1eca3 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 72  ssage in.** pPar
1eca4 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
1eca5 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1eca6 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74  3ExprCheckHeight
1eca7 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1eca8 69 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 20  int nHeight){.  
1eca9 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1ecaa 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 67  OK;.  int mxHeig
1ecab 68 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d  ht = pParse->db-
1ecac 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1ecad 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d  IMIT_EXPR_DEPTH]
1ecae 3b 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e  ;.  if( nHeight>
1ecaf 6d 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20  mxHeight ){.    
1ecb0 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1ecb1 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
1ecb2 22 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65  "Expression tree
1ecb3 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 6d   is too large (m
1ecb4 61 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 29  aximum depth %d)
1ecb5 22 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 20  ", mxHeight.    
1ecb6 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
1ecb7 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
1ecb8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1ecb9 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
1ecba 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20  hree functions, 
1ecbb 68 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20  heightOfExpr(), 
1ecbc 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74  heightOfExprList
1ecbd 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74  ().** and height
1ecbe 4f 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20  OfSelect(), are 
1ecbf 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
1ecc0 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65  e the maximum he
1ecc1 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65  ight.** of any e
1ecc2 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72  xpression tree r
1ecc3 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65  eferenced by the
1ecc4 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65   structure passe
1ecc5 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73  d as the.** firs
1ecc6 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  t argument..**.*
1ecc7 2a 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75  * If this maximu
1ecc8 6d 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61  m height is grea
1ecc9 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72  ter than the cur
1ecca 72 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74  rent value point
1eccb 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65  ed.** to by pnHe
1eccc 69 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64  ight, the second
1eccd 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e   parameter, then
1ecce 20 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74   set *pnHeight t
1eccf 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e  o that.** value.
1ecd0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1ecd1 68 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 70  heightOfExpr(Exp
1ecd2 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69  r *p, int *pnHei
1ecd3 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b  ght){.  if( p ){
1ecd4 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69  .    if( p->nHei
1ecd5 67 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b  ght>*pnHeight ){
1ecd6 0a 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74  .      *pnHeight
1ecd7 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20   = p->nHeight;. 
1ecd8 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69     }.  }.}.stati
1ecd9 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45  c void heightOfE
1ecda 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74  xprList(ExprList
1ecdb 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67   *p, int *pnHeig
1ecdc 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  ht){.  if( p ){.
1ecdd 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
1ecde 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78  or(i=0; i<p->nEx
1ecdf 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
1ece0 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e  heightOfExpr(p->
1ece1 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65  a[i].pExpr, pnHe
1ece2 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ight);.    }.  }
1ece3 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68  .}.static void h
1ece4 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65  eightOfSelect(Se
1ece5 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e  lect *p, int *pn
1ece6 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70  Height){.  if( p
1ece7 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66   ){.    heightOf
1ece8 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20  Expr(p->pWhere, 
1ece9 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
1ecea 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70  eightOfExpr(p->p
1eceb 48 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74  Having, pnHeight
1ecec 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45  );.    heightOfE
1eced 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70  xpr(p->pLimit, p
1ecee 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
1ecef 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f  ightOfExpr(p->pO
1ecf0 66 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29  ffset, pnHeight)
1ecf1 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  ;.    heightOfEx
1ecf2 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74  prList(p->pEList
1ecf3 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20  , pnHeight);.   
1ecf4 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73   heightOfExprLis
1ecf5 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70  t(p->pGroupBy, p
1ecf6 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
1ecf7 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70  ightOfExprList(p
1ecf8 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65  ->pOrderBy, pnHe
1ecf9 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68  ight);.    heigh
1ecfa 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72  tOfSelect(p->pPr
1ecfb 69 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a  ior, pnHeight);.
1ecfc 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74    }.}../*.** Set
1ecfd 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68   the Expr.nHeigh
1ecfe 74 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68  t variable in th
1ecff 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  e structure pass
1ed00 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67  ed as an .** arg
1ed01 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73  ument. An expres
1ed02 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69  sion with no chi
1ed03 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73  ldren, Expr.pLis
1ed04 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53  t or .** Expr.pS
1ed05 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73  elect member has
1ed06 20 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20   a height of 1. 
1ed07 41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 73  Any other expres
1ed08 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65  sion.** has a he
1ed09 69 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68  ight equal to th
1ed0a 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74  e maximum height
1ed0b 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a   of any other .*
1ed0c 2a 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 70  * referenced Exp
1ed0d 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73  r plus one..*/.s
1ed0e 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 53  tatic void exprS
1ed0f 65 74 48 65 69 67 68 74 28 45 78 70 72 20 2a 70  etHeight(Expr *p
1ed10 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74  ){.  int nHeight
1ed11 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66   = 0;.  heightOf
1ed12 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26  Expr(p->pLeft, &
1ed13 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 67  nHeight);.  heig
1ed14 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 67  htOfExpr(p->pRig
1ed15 68 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20  ht, &nHeight);. 
1ed16 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
1ed17 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65  erty(p, EP_xIsSe
1ed18 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65 69  lect) ){.    hei
1ed19 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 78  ghtOfSelect(p->x
1ed1a 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 67  .pSelect, &nHeig
1ed1b 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ht);.  }else{.  
1ed1c 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69    heightOfExprLi
1ed1d 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 26  st(p->x.pList, &
1ed1e 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20  nHeight);.  }.  
1ed1f 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65  p->nHeight = nHe
1ed20 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a  ight + 1;.}../*.
1ed21 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e  ** Set the Expr.
1ed22 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65  nHeight variable
1ed23 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 53   using the exprS
1ed24 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 74  etHeight() funct
1ed25 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 68  ion. If.** the h
1ed26 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72  eight is greater
1ed27 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75   than the maximu
1ed28 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 73  m allowed expres
1ed29 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 6c  sion depth,.** l
1ed2a 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e  eave an error in
1ed2b 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49   pParse..*/.SQLI
1ed2c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1ed2d 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
1ed2e 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72  ight(Parse *pPar
1ed2f 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20  se, Expr *p){.  
1ed30 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 29  exprSetHeight(p)
1ed31 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
1ed32 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73  heckHeight(pPars
1ed33 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b 0a  e, p->nHeight);.
1ed34 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1ed35 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67  the maximum heig
1ed36 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73  ht of any expres
1ed37 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65  sion tree refere
1ed38 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73  nced.** by the s
1ed39 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
1ed3a 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
1ed3b 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ument..*/.SQLITE
1ed3c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1ed3d 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65  ite3SelectExprHe
1ed3e 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b  ight(Select *p){
1ed3f 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d  .  int nHeight =
1ed40 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65   0;.  heightOfSe
1ed41 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 74  lect(p, &nHeight
1ed42 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 69  );.  return nHei
1ed43 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23  ght;.}.#else.  #
1ed44 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 65  define exprSetHe
1ed45 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 2f  ight(y).#endif /
1ed46 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  * SQLITE_MAX_EXP
1ed47 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a  R_DEPTH>0 */../*
1ed48 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20  .** Construct a 
1ed49 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  new expression n
1ed4a 6f 64 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ode and return a
1ed4b 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
1ed4c 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f 72 20 74   Memory.** for t
1ed4d 68 69 73 20 6e 6f 64 65 20 69 73 20 6f 62 74 61  his node is obta
1ed4e 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
1ed4f 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65  3_malloc().  The
1ed50 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
1ed51 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69  n.** is responsi
1ed52 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73  ble for making s
1ed53 75 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65  ure the node eve
1ed54 6e 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65  ntually gets fre
1ed55 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1ed56 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
1ed57 74 65 33 45 78 70 72 28 0a 20 20 73 71 6c 69 74  te3Expr(.  sqlit
1ed58 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
1ed59 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72     /* Handle for
1ed5a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1ed5b 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e  Zero() (may be n
1ed5c 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  ull) */.  int op
1ed5d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1ed5e 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
1ed5f 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72  opcode */.  Expr
1ed60 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20   *pLeft,        
1ed61 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72      /* Left oper
1ed62 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  and */.  Expr *p
1ed63 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20  Right,          
1ed64 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e   /* Right operan
1ed65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b  d */.  const Tok
1ed66 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f  en *pToken     /
1ed67 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e  * Argument token
1ed68 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
1ed69 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  New;.  pNew = sq
1ed6a 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
1ed6b 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70  o(db, sizeof(Exp
1ed6c 72 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d  r));.  if( pNew=
1ed6d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65  =0 ){.    /* Whe
1ed6e 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20  n malloc fails, 
1ed6f 64 65 6c 65 74 65 20 70 4c 65 66 74 20 61 6e 64  delete pLeft and
1ed70 20 70 52 69 67 68 74 2e 20 45 78 70 72 65 73 73   pRight. Express
1ed71 69 6f 6e 73 20 70 61 73 73 65 64 20 74 6f 20 0a  ions passed to .
1ed72 20 20 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63      ** this func
1ed73 74 69 6f 6e 20 6d 75 73 74 20 61 6c 77 61 79 73  tion must always
1ed74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 77 69   be allocated wi
1ed75 74 68 20 73 71 6c 69 74 65 33 45 78 70 72 28 29  th sqlite3Expr()
1ed76 20 66 6f 72 20 74 68 69 73 20 0a 20 20 20 20 2a   for this .    *
1ed77 2a 20 72 65 61 73 6f 6e 2e 20 0a 20 20 20 20 2a  * reason. .    *
1ed78 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  /.    sqlite3Exp
1ed79 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 65 66  rDelete(db, pLef
1ed7a 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  t);.    sqlite3E
1ed7b 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 52  xprDelete(db, pR
1ed7c 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72  ight);.    retur
1ed7d 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d  n 0;.  }.  pNew-
1ed7e 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20  >op = (u8)op;.  
1ed7f 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70 4c  pNew->pLeft = pL
1ed80 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69  eft;.  pNew->pRi
1ed81 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20  ght = pRight;.  
1ed82 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b  pNew->iAgg = -1;
1ed83 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20  .  pNew->span.z 
1ed84 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 69 66 28  = (u8*)"";.  if(
1ed85 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69   pToken ){.    i
1ed86 6e 74 20 63 3b 0a 20 20 20 20 61 73 73 65 72 74  nt c;.    assert
1ed87 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d 30  ( pToken->dyn==0
1ed88 20 29 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 73 70   );.    pNew->sp
1ed89 61 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20  an = *pToken;.  
1ed8a 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e    if( pToken->n>
1ed8b 3d 32 20 0a 20 20 20 20 20 20 20 20 20 26 26 20  =2 .         && 
1ed8c 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e 7a 5b  ((c = pToken->z[
1ed8d 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 63 3d 3d  0])=='\'' || c==
1ed8e 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 20 7c 7c  '"' || c=='[' ||
1ed8f 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 20 20 20   c=='`') ){.    
1ed90 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f    sqlite3TokenCo
1ed91 70 79 28 64 62 2c 20 26 70 4e 65 77 2d 3e 74 6f  py(db, &pNew->to
1ed92 6b 65 6e 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20  ken, pToken);.  
1ed93 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 74 6f      if( pNew->to
1ed94 6b 65 6e 2e 7a 20 29 7b 0a 20 20 20 20 20 20 20  ken.z ){.       
1ed95 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d   pNew->token.n =
1ed96 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28   sqlite3Dequote(
1ed97 28 63 68 61 72 2a 29 70 4e 65 77 2d 3e 74 6f 6b  (char*)pNew->tok
1ed98 65 6e 2e 7a 29 3b 0a 20 20 20 20 20 20 20 20 61  en.z);.        a
1ed99 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 74 6f 6b  ssert( pNew->tok
1ed9a 65 6e 2e 6e 3d 3d 28 75 6e 73 69 67 6e 65 64 29  en.n==(unsigned)
1ed9b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1ed9c 28 63 68 61 72 2a 29 70 4e 65 77 2d 3e 74 6f 6b  (char*)pNew->tok
1ed9d 65 6e 2e 7a 29 20 29 3b 0a 20 20 20 20 20 20 7d  en.z) );.      }
1ed9e 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 22  .      if( c=='"
1ed9f 27 20 29 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20  ' ) pNew->flags 
1eda0 7c 3d 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 3b  |= EP_DblQuoted;
1eda1 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1eda2 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 20 3d 20    pNew->token = 
1eda3 2a 70 54 6f 6b 65 6e 3b 0a 20 20 20 20 7d 0a 20  *pToken;.    }. 
1eda4 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 71     pNew->token.q
1eda5 75 6f 74 65 64 20 3d 20 30 3b 0a 20 20 7d 65 6c  uoted = 0;.  }el
1eda6 73 65 20 69 66 28 20 70 4c 65 66 74 20 29 7b 0a  se if( pLeft ){.
1eda7 20 20 20 20 69 66 28 20 70 52 69 67 68 74 20 29      if( pRight )
1eda8 7b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67  {.      if( pRig
1eda9 68 74 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d 30 20  ht->span.dyn==0 
1edaa 26 26 20 70 4c 65 66 74 2d 3e 73 70 61 6e 2e 64  && pLeft->span.d
1edab 79 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  yn==0 ){.       
1edac 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
1edad 28 70 4e 65 77 2c 20 26 70 4c 65 66 74 2d 3e 73  (pNew, &pLeft->s
1edae 70 61 6e 2c 20 26 70 52 69 67 68 74 2d 3e 73 70  pan, &pRight->sp
1edaf 61 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  an);.      }.   
1edb0 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66     if( pRight->f
1edb1 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
1edb2 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  late ){.        
1edb3 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  pNew->flags |= E
1edb4 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
1edb5 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c        pNew->pCol
1edb6 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c  l = pRight->pCol
1edb7 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  l;.      }.    }
1edb8 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e  .    if( pLeft->
1edb9 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
1edba 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 70  llate ){.      p
1edbb 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  New->flags |= EP
1edbc 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20  _ExpCollate;.   
1edbd 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d     pNew->pColl =
1edbe 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20   pLeft->pColl;. 
1edbf 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 65 78 70 72     }.  }..  expr
1edc0 53 65 74 48 65 69 67 68 74 28 70 4e 65 77 29 3b  SetHeight(pNew);
1edc1 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
1edc2 7d 0a 0a 2f 2a 0a 2a 2a 20 57 6f 72 6b 73 20 6c  }../*.** Works l
1edc3 69 6b 65 20 73 71 6c 69 74 65 33 45 78 70 72 28  ike sqlite3Expr(
1edc4 29 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  ) except that it
1edc5 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72 61 20   takes an extra 
1edc6 50 61 72 73 65 2a 0a 2a 2a 20 61 72 67 75 6d 65  Parse*.** argume
1edc7 6e 74 20 61 6e 64 20 6e 6f 74 69 66 69 65 73 20  nt and notifies 
1edc8 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 63  the associated c
1edc9 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74  onnection object
1edca 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73   if malloc fails
1edcb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1edcc 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
1edcd 33 50 45 78 70 72 28 0a 20 20 50 61 72 73 65 20  3PExpr(.  Parse 
1edce 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
1edcf 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
1edd0 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  text */.  int op
1edd1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1edd2 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
1edd3 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72  opcode */.  Expr
1edd4 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20   *pLeft,        
1edd5 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72      /* Left oper
1edd6 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  and */.  Expr *p
1edd7 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20  Right,          
1edd8 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e   /* Right operan
1edd9 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b  d */.  const Tok
1edda 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f  en *pToken     /
1eddb 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e  * Argument token
1eddc 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
1eddd 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 70   = sqlite3Expr(p
1edde 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c 20 70  Parse->db, op, p
1eddf 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 70 54  Left, pRight, pT
1ede0 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70 20 29  oken);.  if( p )
1ede1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
1ede2 72 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61  rCheckHeight(pPa
1ede3 72 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29  rse, p->nHeight)
1ede4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
1ede5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  ;.}../*.** When 
1ede6 64 6f 69 6e 67 20 61 20 6e 65 73 74 65 64 20 70  doing a nested p
1ede7 61 72 73 65 2c 20 79 6f 75 20 63 61 6e 20 69 6e  arse, you can in
1ede8 63 6c 75 64 65 20 74 65 72 6d 73 20 69 6e 20 61  clude terms in a
1ede9 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  n expression.** 
1edea 74 68 61 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74  that look like t
1edeb 68 69 73 3a 20 20 20 23 31 20 23 32 20 2e 2e 2e  his:   #1 #2 ...
1edec 20 20 54 68 65 73 65 20 74 65 72 6d 73 20 72 65    These terms re
1eded 66 65 72 20 74 6f 20 72 65 67 69 73 74 65 72 73  fer to registers
1edee 0a 2a 2a 20 69 6e 20 74 68 65 20 76 69 72 74 75  .** in the virtu
1edef 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 23 4e 20  al machine.  #N 
1edf0 69 73 20 74 68 65 20 4e 2d 74 68 20 72 65 67 69  is the N-th regi
1edf1 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ster..**.** This
1edf2 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
1edf3 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
1edf4 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6f 6e   to deal with on
1edf5 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73 2e   of those terms.
1edf6 0a 2a 2a 20 49 74 20 69 6d 6d 65 64 69 61 74 65  .** It immediate
1edf7 6c 79 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64  ly generates cod
1edf8 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 76  e to store the v
1edf9 61 6c 75 65 20 69 6e 20 61 20 6d 65 6d 6f 72 79  alue in a memory
1edfa 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68   location..** Th
1edfb 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 78 70  e returns an exp
1edfc 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 69 6c  ression that wil
1edfd 6c 20 63 6f 64 65 20 74 6f 20 65 78 74 72 61 63  l code to extrac
1edfe 74 20 74 68 65 20 76 61 6c 75 65 20 66 72 6f 6d  t the value from
1edff 0a 2a 2a 20 74 68 61 74 20 6d 65 6d 6f 72 79 20  .** that memory 
1ee00 6c 6f 63 61 74 69 6f 6e 20 61 73 20 6e 65 65 64  location as need
1ee01 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1ee02 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
1ee03 74 65 33 52 65 67 69 73 74 65 72 45 78 70 72 28  te3RegisterExpr(
1ee04 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
1ee05 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20  oken *pToken){. 
1ee06 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
1ee07 65 2d 3e 70 56 64 62 65 3b 0a 20 20 45 78 70 72  e->pVdbe;.  Expr
1ee08 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73   *p;.  if( pPars
1ee09 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a  e->nested==0 ){.
1ee0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1ee0b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 65 61  Msg(pParse, "nea
1ee0c 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61 78  r \"%T\": syntax
1ee0d 20 65 72 72 6f 72 22 2c 20 70 54 6f 6b 65 6e 29   error", pToken)
1ee0e 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  ;.    return sql
1ee0f 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
1ee10 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c  , TK_NULL, 0, 0,
1ee11 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76   0);.  }.  if( v
1ee12 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1ee13 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78    p = sqlite3PEx
1ee14 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 45  pr(pParse, TK_RE
1ee15 47 49 53 54 45 52 2c 20 30 2c 20 30 2c 20 70 54  GISTER, 0, 0, pT
1ee16 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70 3d 3d  oken);.  if( p==
1ee17 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1ee18 30 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 66 61  0;  /* Malloc fa
1ee19 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 70 2d  iled */.  }.  p-
1ee1a 3e 69 54 61 62 6c 65 20 3d 20 61 74 6f 69 28 28  >iTable = atoi((
1ee1b 63 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e 7a  char*)&pToken->z
1ee1c 5b 31 5d 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  [1]);.  return p
1ee1d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69 6e 20  ;.}../*.** Join 
1ee1e 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20  two expressions 
1ee1f 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f 70 65  using an AND ope
1ee20 72 61 74 6f 72 2e 20 20 49 66 20 65 69 74 68 65  rator.  If eithe
1ee21 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 0a  r expression is.
1ee22 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 6a 75  ** NULL, then ju
1ee23 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 6f 74  st return the ot
1ee24 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  her expression..
1ee25 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1ee26 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
1ee27 78 70 72 41 6e 64 28 73 71 6c 69 74 65 33 20 2a  xprAnd(sqlite3 *
1ee28 64 62 2c 20 45 78 70 72 20 2a 70 4c 65 66 74 2c  db, Expr *pLeft,
1ee29 20 45 78 70 72 20 2a 70 52 69 67 68 74 29 7b 0a   Expr *pRight){.
1ee2a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d 30 20 29    if( pLeft==0 )
1ee2b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 52 69  {.    return pRi
1ee2c 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ght;.  }else if(
1ee2d 20 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20   pRight==0 ){.  
1ee2e 20 20 72 65 74 75 72 6e 20 70 4c 65 66 74 3b 0a    return pLeft;.
1ee2f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
1ee30 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 28  urn sqlite3Expr(
1ee31 64 62 2c 20 54 4b 5f 41 4e 44 2c 20 70 4c 65 66  db, TK_AND, pLef
1ee32 74 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20  t, pRight, 0);. 
1ee33 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20   }.}../*.** Set 
1ee34 74 68 65 20 45 78 70 72 2e 73 70 61 6e 20 66 69  the Expr.span fi
1ee35 65 6c 64 20 6f 66 20 74 68 65 20 67 69 76 65 6e  eld of the given
1ee36 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73   expression to s
1ee37 70 61 6e 20 61 6c 6c 0a 2a 2a 20 74 65 78 74 20  pan all.** text 
1ee38 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20  between the two 
1ee39 67 69 76 65 6e 20 74 6f 6b 65 6e 73 2e 20 20 42  given tokens.  B
1ee3a 6f 74 68 20 74 6f 6b 65 6e 73 20 6d 75 73 74 20  oth tokens must 
1ee3b 62 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61  be pointing.** a
1ee3c 74 20 74 68 65 20 73 61 6d 65 20 73 74 72 69 6e  t the same strin
1ee3d 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
1ee3e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1ee3f 33 45 78 70 72 53 70 61 6e 28 45 78 70 72 20 2a  3ExprSpan(Expr *
1ee40 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 4c  pExpr, Token *pL
1ee41 65 66 74 2c 20 54 6f 6b 65 6e 20 2a 70 52 69 67  eft, Token *pRig
1ee42 68 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  ht){.  assert( p
1ee43 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 61 73  Right!=0 );.  as
1ee44 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30 20 29  sert( pLeft!=0 )
1ee45 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 29 7b  ;.  if( pExpr ){
1ee46 0a 20 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e  .    pExpr->span
1ee47 2e 7a 20 3d 20 70 4c 65 66 74 2d 3e 7a 3b 0a 20  .z = pLeft->z;. 
1ee48 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77     /* The follow
1ee49 69 6e 67 20 61 73 73 65 72 74 28 29 20 6d 61 79  ing assert() may
1ee4a 20 66 61 69 6c 20 77 68 65 6e 20 74 68 69 73 20   fail when this 
1ee4b 69 73 20 63 61 6c 6c 65 64 20 0a 20 20 20 20 2a  is called .    *
1ee4c 2a 20 76 69 61 20 73 71 6c 69 74 65 33 50 45 78  * via sqlite3PEx
1ee4d 70 72 28 29 2f 73 71 6c 69 74 65 33 45 78 70 72  pr()/sqlite3Expr
1ee4e 28 29 20 66 72 6f 6d 20 61 64 64 57 68 65 72 65  () from addWhere
1ee4f 54 65 72 6d 28 29 2e 20 2a 2f 0a 20 20 20 20 2f  Term(). */.    /
1ee50 2a 20 61 73 73 65 72 74 28 70 52 69 67 68 74 2d  * assert(pRight-
1ee51 3e 7a 20 3e 3d 20 70 4c 65 66 74 2d 3e 7a 29 3b  >z >= pLeft->z);
1ee52 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d 3e 73   */.    pExpr->s
1ee53 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68 74 2d 3e  pan.n = pRight->
1ee54 6e 20 2b 20 28 75 6e 73 69 67 6e 65 64 29 28 70  n + (unsigned)(p
1ee55 52 69 67 68 74 2d 3e 7a 20 2d 20 70 4c 65 66 74  Right->z - pLeft
1ee56 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ->z);.  }.}../*.
1ee57 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 6e  ** Construct a n
1ee58 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  ew expression no
1ee59 64 65 20 66 6f 72 20 61 20 66 75 6e 63 74 69 6f  de for a functio
1ee5a 6e 20 77 69 74 68 20 6d 75 6c 74 69 70 6c 65 0a  n with multiple.
1ee5b 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f  ** arguments..*/
1ee5c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1ee5d 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
1ee5e 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 73 65 20  rFunction(Parse 
1ee5f 2a 70 50 61 72 73 65 2c 20 45 78 70 72 4c 69 73  *pParse, ExprLis
1ee60 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20  t *pList, Token 
1ee61 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45 78 70 72  *pToken){.  Expr
1ee62 20 2a 70 4e 65 77 3b 0a 20 20 73 71 6c 69 74 65   *pNew;.  sqlite
1ee63 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1ee64 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54  db;.  assert( pT
1ee65 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e 65 77 20 3d  oken );.  pNew =
1ee66 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1ee67 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
1ee68 45 78 70 72 29 20 29 3b 0a 20 20 69 66 28 20 70  Expr) );.  if( p
1ee69 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  New==0 ){.    sq
1ee6a 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
1ee6b 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 20  ete(db, pList); 
1ee6c 2f 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72 79 20  /* Avoid memory 
1ee6d 6c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c 6f 63  leak when malloc
1ee6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65   fails */.    re
1ee6f 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e  turn 0;.  }.  pN
1ee70 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 46 55 4e 43  ew->op = TK_FUNC
1ee71 54 49 4f 4e 3b 0a 20 20 70 4e 65 77 2d 3e 78 2e  TION;.  pNew->x.
1ee72 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20  pList = pList;. 
1ee73 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
1ee74 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20  sProperty(pNew, 
1ee75 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
1ee76 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65  .  assert( pToke
1ee77 6e 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 70  n->dyn==0 );.  p
1ee78 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 2a 70 54 6f  New->span = *pTo
1ee79 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65 33 54 6f  ken;.  sqlite3To
1ee7a 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 4e 65  kenCopy(db, &pNe
1ee7b 77 2d 3e 74 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e  w->token, pToken
1ee7c 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
1ee7d 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65  SetHeight(pParse
1ee7e 2c 20 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72  , pNew);.  retur
1ee7f 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
1ee80 20 41 73 73 69 67 6e 20 61 20 76 61 72 69 61 62   Assign a variab
1ee81 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20  le number to an 
1ee82 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
1ee83 65 6e 63 6f 64 65 73 20 61 20 77 69 6c 64 63 61  encodes a wildca
1ee84 72 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69  rd.** in the ori
1ee85 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d  ginal SQL statem
1ee86 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c  ent.  .**.** Wil
1ee87 64 63 61 72 64 73 20 63 6f 6e 73 69 73 74 69 6e  dcards consistin
1ee88 67 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 22 3f  g of a single "?
1ee89 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74  " are assigned t
1ee8a 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e 74 69  he next sequenti
1ee8b 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e  al.** variable n
1ee8c 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c  umber..**.** Wil
1ee8d 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f  dcards of the fo
1ee8e 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73  rm "?nnn" are as
1ee8f 73 69 67 6e 65 64 20 74 68 65 20 6e 75 6d 62 65  signed the numbe
1ee90 72 20 22 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b  r "nnn".  We mak
1ee91 65 0a 2a 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20  e.** sure "nnn" 
1ee92 69 73 20 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f  is not too be to
1ee93 20 61 76 6f 69 64 20 61 20 64 65 6e 69 61 6c 20   avoid a denial 
1ee94 6f 66 20 73 65 72 76 69 63 65 20 61 74 74 61 63  of service attac
1ee95 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51  k when.** the SQ
1ee96 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65  L statement come
1ee97 73 20 66 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e  s from an extern
1ee98 61 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a  al source..**.**
1ee99 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68   Wildcards of th
1ee9a 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 20 6f 72  e form ":aaa" or
1ee9b 20 22 24 61 61 61 22 20 61 72 65 20 61 73 73 69   "$aaa" are assi
1ee9c 67 6e 65 64 20 74 68 65 20 73 61 6d 65 20 6e 75  gned the same nu
1ee9d 6d 62 65 72 0a 2a 2a 20 61 73 20 74 68 65 20 70  mber.** as the p
1ee9e 72 65 76 69 6f 75 73 20 69 6e 73 74 61 6e 63 65  revious instance
1ee9f 20 6f 66 20 74 68 65 20 73 61 6d 65 20 77 69 6c   of the same wil
1eea0 64 63 61 72 64 2e 20 20 4f 72 20 69 66 20 74 68  dcard.  Or if th
1eea1 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 0a  is is the first.
1eea2 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ** instance of t
1eea3 68 65 20 77 69 6c 64 63 61 72 64 2c 20 74 68 65  he wildcard, the
1eea4 20 6e 65 78 74 20 73 65 71 75 65 6e 69 61 6c 20   next sequenial 
1eea5 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20  variable number 
1eea6 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64 2e 0a  is.** assigned..
1eea7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1eea8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
1eea9 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65  prAssignVarNumbe
1eeaa 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  r(Parse *pParse,
1eeab 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
1eeac 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 0a   Token *pToken;.
1eead 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1eeae 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69  pParse->db;..  i
1eeaf 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65  f( pExpr==0 ) re
1eeb0 74 75 72 6e 3b 0a 20 20 70 54 6f 6b 65 6e 20 3d  turn;.  pToken =
1eeb1 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 3b 0a   &pExpr->token;.
1eeb2 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e    assert( pToken
1eeb3 2d 3e 6e 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65  ->n>=1 );.  asse
1eeb4 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 21 3d 30  rt( pToken->z!=0
1eeb5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
1eeb6 6f 6b 65 6e 2d 3e 7a 5b 30 5d 21 3d 30 20 29 3b  oken->z[0]!=0 );
1eeb7 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 6e  .  if( pToken->n
1eeb8 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 69  ==1 ){.    /* Wi
1eeb9 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66 6f  ldcard of the fo
1eeba 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 6e 20  rm "?".  Assign 
1eebb 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 62 6c  the next variabl
1eebc 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  e number */.    
1eebd 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
1eebe 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b 0a  ++pParse->nVar;.
1eebf 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 6f 6b    }else if( pTok
1eec0 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 3f 27 20 29 7b  en->z[0]=='?' ){
1eec1 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64  .    /* Wildcard
1eec2 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e   of the form "?n
1eec3 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20 22 6e  nn".  Convert "n
1eec4 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65 67 65  nn" to an intege
1eec5 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75 73 65  r and.    ** use
1eec6 20 69 74 20 61 73 20 74 68 65 20 76 61 72 69 61   it as the varia
1eec7 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ble number */.  
1eec8 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 45 78    int i;.    pEx
1eec9 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 20 3d  pr->iTable = i =
1eeca 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70 54   atoi((char*)&pT
1eecb 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20 20  oken->z[1]);.   
1eecc 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 30 20   testcase( i==0 
1eecd 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
1eece 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 73   i==1 );.    tes
1eecf 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c  tcase( i==db->aL
1eed0 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1eed1 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
1eed2 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74 65 73 74  R]-1 );.    test
1eed3 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69  case( i==db->aLi
1eed4 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
1eed5 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
1eed6 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 69 3c 31  ] );.    if( i<1
1eed7 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d 69 74   || i>db->aLimit
1eed8 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41  [SQLITE_LIMIT_VA
1eed9 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29  RIABLE_NUMBER] )
1eeda 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1eedb 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1eedc 22 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72  "variable number
1eedd 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e   must be between
1eede 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c 0a 20 20   ?1 and ?%d",.  
1eedf 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d          db->aLim
1eee0 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1eee1 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d  VARIABLE_NUMBER]
1eee2 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
1eee3 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56 61 72 20   i>pParse->nVar 
1eee4 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
1eee5 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20 20 20 7d  >nVar = i;.    }
1eee6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1eee7 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68   Wildcards of th
1eee8 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 20 6f 72  e form ":aaa" or
1eee9 20 22 24 61 61 61 22 2e 20 20 52 65 75 73 65 20   "$aaa".  Reuse 
1eeea 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c  the same variabl
1eeeb 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20  e.    ** number 
1eeec 61 73 20 74 68 65 20 70 72 69 6f 72 20 61 70 70  as the prior app
1eeed 65 61 72 61 6e 63 65 20 6f 66 20 74 68 65 20 73  earance of the s
1eeee 61 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69 66 20  ame name, or if 
1eeef 74 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a 2a 20  the name.    ** 
1eef0 68 61 73 20 6e 65 76 65 72 20 61 70 70 65 61 72  has never appear
1eef1 65 64 20 62 65 66 6f 72 65 2c 20 72 65 75 73 65  ed before, reuse
1eef2 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 62   the same variab
1eef3 6c 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2f  le number.    */
1eef4 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
1eef5 75 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 70  u32 n;.    n = p
1eef6 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20 20 20 20 66 6f  Token->n;.    fo
1eef7 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d  r(i=0; i<pParse-
1eef8 3e 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b 29 7b  >nVarExpr; i++){
1eef9 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 3b  .      Expr *pE;
1eefa 0a 20 20 20 20 20 20 69 66 28 20 28 70 45 20 3d  .      if( (pE =
1eefb 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
1eefc 70 72 5b 69 5d 29 21 3d 30 0a 20 20 20 20 20 20  pr[i])!=0.      
1eefd 20 20 20 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e      && pE->token
1eefe 2e 6e 3d 3d 6e 0a 20 20 20 20 20 20 20 20 20 20  .n==n.          
1eeff 26 26 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 74 6f  && memcmp(pE->to
1ef00 6b 65 6e 2e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 7a  ken.z, pToken->z
1ef01 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , n)==0 ){.     
1ef02 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1ef03 20 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b 0a 20   = pE->iTable;. 
1ef04 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1ef05 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1ef06 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e  if( i>=pParse->n
1ef07 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20 20 20  VarExpr ){.     
1ef08 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
1ef09 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b   ++pParse->nVar;
1ef0a 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73  .      if( pPars
1ef0b 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 50 61  e->nVarExpr>=pPa
1ef0c 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c  rse->nVarExprAll
1ef0d 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  oc-1 ){.        
1ef0e 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
1ef0f 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 65 2d  Alloc += pParse-
1ef10 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b  >nVarExprAlloc +
1ef11 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70 50 61   10;.        pPa
1ef12 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 20 3d  rse->apVarExpr =
1ef13 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1ef14 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
1ef15 72 65 65 28 0a 20 20 20 20 20 20 20 20 20 20 20  ree(.           
1ef16 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 20 20     db,.         
1ef17 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56       pParse->apV
1ef18 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20  arExpr,.        
1ef19 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56        pParse->nV
1ef1a 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 7a 65  arExprAlloc*size
1ef1b 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56 61 72  of(pParse->apVar
1ef1c 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 20 20  Expr[0]).       
1ef1d 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a       );.      }.
1ef1e 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
1ef1f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1ef20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1ef21 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
1ef22 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70  !=0 );.        p
1ef23 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
1ef24 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70  [pParse->nVarExp
1ef25 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a 20 20  r++] = pExpr;.  
1ef26 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20      }.    }.  } 
1ef27 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
1ef28 6e 45 72 72 20 26 26 20 70 50 61 72 73 65 2d 3e  nErr && pParse->
1ef29 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  nVar>db->aLimit[
1ef2a 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
1ef2b 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b  IABLE_NUMBER] ){
1ef2c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1ef2d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f  rMsg(pParse, "to
1ef2e 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 69 61  o many SQL varia
1ef2f 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  bles");.  }.}../
1ef30 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 65 78  *.** Clear an ex
1ef31 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75  pression structu
1ef32 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74  re without delet
1ef33 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 72  ing the structur
1ef34 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 75 62  e itself..** Sub
1ef35 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c  structure is del
1ef36 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eted..*/.SQLITE_
1ef37 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1ef38 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71  ite3ExprClear(sq
1ef39 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20  lite3 *db, Expr 
1ef3a 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f  *p){.  if( p->to
1ef3b 6b 65 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65  ken.dyn ) sqlite
1ef3c 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 61  3DbFree(db, (cha
1ef3d 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a  r*)p->token.z);.
1ef3e 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e    if( !ExprHasAn
1ef3f 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  yProperty(p, EP_
1ef40 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61  TokenOnly|EP_Spa
1ef41 6e 54 6f 6b 65 6e 29 20 29 7b 0a 20 20 20 20 69  nToken) ){.    i
1ef42 66 28 20 70 2d 3e 73 70 61 6e 2e 64 79 6e 20 29  f( p->span.dyn )
1ef43 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1ef44 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73 70 61  b, (char*)p->spa
1ef45 6e 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20 45 78  n.z);.    if( Ex
1ef46 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
1ef47 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 7b 0a   EP_Reduced) ){.
1ef48 20 20 20 20 20 20 2f 2a 20 53 75 62 74 72 65 65        /* Subtree
1ef49 73 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68  s are part of th
1ef4a 65 20 73 61 6d 65 20 6d 65 6d 6f 72 79 20 61 6c  e same memory al
1ef4b 6c 6f 63 61 74 69 6f 6e 20 77 68 65 6e 20 45 50  location when EP
1ef4c 5f 52 65 64 75 63 65 64 20 73 65 74 20 2a 2f 0a  _Reduced set */.
1ef4d 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65        if( p->pLe
1ef4e 66 74 20 29 20 73 71 6c 69 74 65 33 45 78 70 72  ft ) sqlite3Expr
1ef4f 43 6c 65 61 72 28 64 62 2c 20 70 2d 3e 70 4c 65  Clear(db, p->pLe
1ef50 66 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ft);.      if( p
1ef51 2d 3e 70 52 69 67 68 74 20 29 20 73 71 6c 69 74  ->pRight ) sqlit
1ef52 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 2c 20  e3ExprClear(db, 
1ef53 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20  p->pRight);.    
1ef54 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
1ef55 53 75 62 74 72 65 65 73 20 61 72 65 20 73 65 70  Subtrees are sep
1ef56 61 72 61 74 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  arate allocation
1ef57 73 20 77 68 65 6e 20 45 50 5f 52 65 64 75 63 65  s when EP_Reduce
1ef58 64 20 69 73 20 63 6c 65 61 72 20 2a 2f 0a 20 20  d is clear */.  
1ef59 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
1ef5a 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 65  elete(db, p->pLe
1ef5b 66 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ft);.      sqlit
1ef5c 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
1ef5d 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20   p->pRight);.   
1ef5e 20 7d 0a 20 20 20 20 2f 2a 20 78 2e 70 53 65 6c   }.    /* x.pSel
1ef5f 65 63 74 20 61 6e 64 20 78 2e 70 4c 69 73 74 20  ect and x.pList 
1ef60 61 72 65 20 61 6c 77 61 79 73 20 73 65 70 61 72  are always separ
1ef61 61 74 65 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ately allocated 
1ef62 2a 2f 0a 20 20 20 20 69 66 28 20 45 78 70 72 48  */.    if( ExprH
1ef63 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  asProperty(p, EP
1ef64 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
1ef65 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
1ef66 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  ctDelete(db, p->
1ef67 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  x.pSelect);.    
1ef68 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
1ef69 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
1ef6a 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73  te(db, p->x.pLis
1ef6b 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  t);.    }.  }.}.
1ef6c 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76 65  ./*.** Recursive
1ef6d 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78 70  ly delete an exp
1ef6e 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a 2f  ression tree..*/
1ef6f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1ef70 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1ef71 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a  Delete(sqlite3 *
1ef72 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20  db, Expr *p){.  
1ef73 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1ef74 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  n;.  sqlite3Expr
1ef75 43 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a 20 20  Clear(db, p);.  
1ef76 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1ef77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  , p);.}../*.** R
1ef78 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
1ef79 20 6f 66 20 62 79 74 65 73 20 61 6c 6c 6f 63 61   of bytes alloca
1ef7a 74 65 64 20 66 6f 72 20 74 68 65 20 65 78 70 72  ted for the expr
1ef7b 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72 65  ession structure
1ef7c 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74   .** passed as t
1ef7d 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
1ef7e 74 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 79  t. This is alway
1ef7f 73 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55  s one of EXPR_FU
1ef80 4c 4c 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f  LLSIZE,.** EXPR_
1ef81 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 20 45  REDUCEDSIZE or E
1ef82 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a  XPR_TOKENONLYSIZ
1ef83 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  E..*/.static int
1ef84 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65 28   exprStructSize(
1ef85 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  Expr *p){.  if( 
1ef86 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
1ef87 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29  p, EP_TokenOnly)
1ef88 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f 54   ) return EXPR_T
1ef89 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20  OKENONLYSIZE;.  
1ef8a 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
1ef8b 72 74 79 28 70 2c 20 45 50 5f 53 70 61 6e 54 6f  rty(p, EP_SpanTo
1ef8c 6b 65 6e 29 20 29 20 72 65 74 75 72 6e 20 45 58  ken) ) return EX
1ef8d 50 52 5f 53 50 41 4e 54 4f 4b 45 4e 53 49 5a 45  PR_SPANTOKENSIZE
1ef8e 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  ;.  if( ExprHasP
1ef8f 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65  roperty(p, EP_Re
1ef90 64 75 63 65 64 29 20 29 20 72 65 74 75 72 6e 20  duced) ) return 
1ef91 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45  EXPR_REDUCEDSIZE
1ef92 3b 0a 20 20 72 65 74 75 72 6e 20 45 58 50 52 5f  ;.  return EXPR_
1ef93 46 55 4c 4c 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a  FULLSIZE;.}../*.
1ef94 2a 2a 20 73 71 6c 69 74 65 33 45 78 70 72 44 75  ** sqlite3ExprDu
1ef95 70 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c  p() has been cal
1ef96 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  led to create a 
1ef97 63 6f 70 79 20 6f 66 20 65 78 70 72 65 73 73 69  copy of expressi
1ef98 6f 6e 20 70 20 77 69 74 68 0a 2a 2a 20 74 68 65  on p with.** the
1ef99 20 45 58 50 52 44 55 50 5f 58 58 58 20 66 6c 61   EXPRDUP_XXX fla
1ef9a 67 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  gs passed as the
1ef9b 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
1ef9c 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
1ef9d 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 68 65 20  .** returns the 
1ef9e 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66  space required f
1ef9f 6f 72 20 74 68 65 20 63 6f 70 79 20 6f 66 20 74  or the copy of t
1efa0 68 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72  he Expr structur
1efa1 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 69 73 20  e only..** This 
1efa2 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66  is always one of
1efa3 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 20   EXPR_FULLSIZE, 
1efa4 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45  EXPR_REDUCEDSIZE
1efa5 20 6f 72 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e   or EXPR_TOKENON
1efa6 4c 59 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69  LYSIZE..*/.stati
1efa7 63 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53  c int dupedExprS
1efa8 74 72 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a  tructSize(Expr *
1efa9 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  p, int flags){. 
1efaa 20 69 6e 74 20 6e 53 69 7a 65 3b 0a 20 20 69 66   int nSize;.  if
1efab 28 20 30 3d 3d 28 66 6c 61 67 73 26 45 58 50 52  ( 0==(flags&EXPR
1efac 44 55 50 5f 52 45 44 55 43 45 29 20 29 7b 0a 20  DUP_REDUCE) ){. 
1efad 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f     nSize = EXPR_
1efae 46 55 4c 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c 73  FULLSIZE;.  }els
1efaf 65 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 20 7c  e if( p->pLeft |
1efb0 7c 20 70 2d 3e 70 52 69 67 68 74 20 7c 7c 20 70  | p->pRight || p
1efb1 2d 3e 70 43 6f 6c 6c 20 7c 7c 20 70 2d 3e 78 2e  ->pColl || p->x.
1efb2 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 6e 53 69  pList ){.    nSi
1efb3 7a 65 20 3d 20 45 58 50 52 5f 52 45 44 55 43 45  ze = EXPR_REDUCE
1efb4 44 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 20 69  DSIZE;.  }else i
1efb5 66 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50  f( flags&EXPRDUP
1efb6 5f 53 50 41 4e 20 29 7b 0a 20 20 20 20 6e 53 69  _SPAN ){.    nSi
1efb7 7a 65 20 3d 20 45 58 50 52 5f 53 50 41 4e 54 4f  ze = EXPR_SPANTO
1efb8 4b 45 4e 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65  KENSIZE;.  }else
1efb9 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58  {.    nSize = EX
1efba 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45  PR_TOKENONLYSIZE
1efbb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
1efbc 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73  Size;.}../*.** s
1efbd 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 20  qlite3ExprDup() 
1efbe 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  has been called 
1efbf 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79  to create a copy
1efc0 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70   of expression p
1efc1 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 45 58 50   with.** the EXP
1efc2 52 44 55 50 5f 58 58 58 20 70 61 73 73 65 64 20  RDUP_XXX passed 
1efc3 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
1efc4 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 66 75 6e  gument. This fun
1efc5 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a 2a 2a  ction returns.**
1efc6 20 74 68 65 20 73 70 61 63 65 20 69 6e 20 62 79   the space in by
1efc7 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20  tes required to 
1efc8 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20 6f  store the copy o
1efc9 66 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63  f the Expr struc
1efca 74 75 72 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ture.** and the 
1efcb 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 45 78  copies of the Ex
1efcc 70 72 2e 74 6f 6b 65 6e 2e 7a 20 61 6e 64 20 45  pr.token.z and E
1efcd 78 70 72 2e 73 70 61 6e 2e 7a 20 28 69 66 20 61  xpr.span.z (if a
1efce 70 70 6c 69 63 61 62 6c 65 29 0a 2a 2a 20 73 74  pplicable).** st
1efcf 72 69 6e 67 20 62 75 66 66 65 72 73 2e 0a 2a 2f  ring buffers..*/
1efd0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65  .static int dupe
1efd1 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 45 78  dExprNodeSize(Ex
1efd2 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73  pr *p, int flags
1efd3 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d  ){.  int nByte =
1efd4 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74   dupedExprStruct
1efd5 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 20 2b  Size(p, flags) +
1efd6 20 28 70 2d 3e 74 6f 6b 65 6e 2e 7a 20 3f 20 70   (p->token.z ? p
1efd7 2d 3e 74 6f 6b 65 6e 2e 6e 20 2b 20 31 20 3a 20  ->token.n + 1 : 
1efd8 30 29 3b 0a 20 20 69 66 28 20 28 66 6c 61 67 73  0);.  if( (flags
1efd9 26 45 58 50 52 44 55 50 5f 53 50 41 4e 29 21 3d  &EXPRDUP_SPAN)!=
1efda 30 0a 20 20 20 26 26 20 28 70 2d 3e 74 6f 6b 65  0.   && (p->toke
1efdb 6e 2e 7a 21 3d 70 2d 3e 73 70 61 6e 2e 7a 20 7c  n.z!=p->span.z |
1efdc 7c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 2d  | p->token.n!=p-
1efdd 3e 73 70 61 6e 2e 6e 29 0a 20 20 29 7b 0a 20 20  >span.n).  ){.  
1efde 20 20 6e 42 79 74 65 20 2b 3d 20 70 2d 3e 73 70    nByte += p->sp
1efdf 61 6e 2e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75  an.n;.  }.  retu
1efe0 72 6e 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29  rn ROUND8(nByte)
1efe1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1efe2 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1efe3 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74  bytes required t
1efe4 6f 20 63 72 65 61 74 65 20 61 20 64 75 70 6c 69  o create a dupli
1efe5 63 61 74 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20  cate of the .** 
1efe6 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73 73 65  expression passe
1efe7 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
1efe8 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 73 65 63  rgument. The sec
1efe9 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
1efea 61 0a 2a 2a 20 6d 61 73 6b 20 63 6f 6e 74 61 69  a.** mask contai
1efeb 6e 69 6e 67 20 45 58 50 52 44 55 50 5f 58 58 58  ning EXPRDUP_XXX
1efec 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   flags..**.** Th
1efed 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
1efee 20 69 6e 63 6c 75 64 65 73 20 73 70 61 63 65 20   includes space 
1efef 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79  to create a copy
1eff0 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72   of the Expr str
1eff1 75 63 74 0a 2a 2a 20 69 74 73 65 6c 66 20 61 6e  uct.** itself an
1eff2 64 20 74 68 65 20 62 75 66 66 65 72 20 72 65 66  d the buffer ref
1eff3 65 72 72 65 64 20 74 6f 20 62 79 20 45 78 70 72  erred to by Expr
1eff4 2e 74 6f 6b 65 6e 2c 20 69 66 20 61 6e 79 2e 20  .token, if any. 
1eff5 49 66 20 74 68 65 20 0a 2a 2a 20 45 58 50 52 44  If the .** EXPRD
1eff6 55 50 5f 53 50 41 4e 20 66 6c 61 67 20 69 73 20  UP_SPAN flag is 
1eff7 73 65 74 2c 20 74 68 65 6e 20 73 70 61 63 65 20  set, then space 
1eff8 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79  to create a copy
1eff9 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a   of the buffer.*
1effa 2a 20 72 65 66 65 72 65 64 20 74 6f 20 62 79 20  * refered to by 
1effb 45 78 70 72 2e 73 70 61 6e 20 69 73 20 61 6c 73  Expr.span is als
1effc 6f 20 69 6e 63 6c 75 64 65 64 2e 0a 2a 2a 0a 2a  o included..**.*
1effd 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50  * If the EXPRDUP
1effe 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20  _REDUCE flag is 
1efff 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65  set, then the re
1f000 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75  turn value inclu
1f001 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f  des .** space to
1f002 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45   duplicate all E
1f003 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65  xpr nodes in the
1f004 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20   tree formed by 
1f005 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61  Expr.pLeft .** a
1f006 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 20 76  nd Expr.pRight v
1f007 61 72 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f  ariables (but no
1f008 74 20 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74  t for any struct
1f009 75 72 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20  ures pointed to 
1f00a 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64  or .** descended
1f00b 20 66 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78   from the Expr.x
1f00c 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78  .pList or Expr.x
1f00d 2e 70 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c  .pSelect variabl
1f00e 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  es)..*/.static i
1f00f 6e 74 20 64 75 70 65 64 45 78 70 72 53 69 7a 65  nt dupedExprSize
1f010 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c  (Expr *p, int fl
1f011 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ags){.  int nByt
1f012 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29  e = 0;.  if( p )
1f013 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75  {.    nByte = du
1f014 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28  pedExprNodeSize(
1f015 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69  p, flags);.    i
1f016 66 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50  f( flags&EXPRDUP
1f017 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20  _REDUCE ){.     
1f018 20 69 6e 74 20 66 20 3d 20 66 6c 61 67 73 26 28   int f = flags&(
1f019 7e 45 58 50 52 44 55 50 5f 53 50 41 4e 29 3b 0a  ~EXPRDUP_SPAN);.
1f01a 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 64        nByte += d
1f01b 75 70 65 64 45 78 70 72 53 69 7a 65 28 70 2d 3e  upedExprSize(p->
1f01c 70 4c 65 66 74 2c 20 66 29 20 2b 20 64 75 70 65  pLeft, f) + dupe
1f01d 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 52 69  dExprSize(p->pRi
1f01e 67 68 74 2c 20 66 29 3b 0a 20 20 20 20 7d 0a 20  ght, f);.    }. 
1f01f 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 42 79 74   }.  return nByt
1f020 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  e;.}../*.** This
1f021 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 69 6d   function is sim
1f022 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 45  ilar to sqlite3E
1f023 78 70 72 44 75 70 28 29 2c 20 65 78 63 65 70 74  xprDup(), except
1f024 20 74 68 61 74 20 69 66 20 70 7a 42 75 66 66 65   that if pzBuffe
1f025 72 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c  r .** is not NUL
1f026 4c 20 74 68 65 6e 20 2a 70 7a 42 75 66 66 65 72  L then *pzBuffer
1f027 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 70   is assumed to p
1f028 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
1f029 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 0a 2a   large enough .*
1f02a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63  * to store the c
1f02b 6f 70 79 20 6f 66 20 65 78 70 72 65 73 73 69 6f  opy of expressio
1f02c 6e 20 70 2c 20 74 68 65 20 63 6f 70 69 65 73 20  n p, the copies 
1f02d 6f 66 20 70 2d 3e 74 6f 6b 65 6e 20 61 6e 64 20  of p->token and 
1f02e 70 2d 3e 73 70 61 6e 20 0a 2a 2a 20 28 69 66 20  p->span .** (if 
1f02f 61 70 70 6c 69 63 61 62 6c 65 29 2c 20 61 6e 64  applicable), and
1f030 20 74 68 65 20 63 6f 70 69 65 73 20 6f 66 20 74   the copies of t
1f031 68 65 20 70 2d 3e 70 4c 65 66 74 20 61 6e 64 20  he p->pLeft and 
1f032 70 2d 3e 70 52 69 67 68 74 20 65 78 70 72 65 73  p->pRight expres
1f033 73 69 6f 6e 73 2c 0a 2a 2a 20 69 66 20 61 6e 79  sions,.** if any
1f034 2e 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69  . Before returni
1f035 6e 67 2c 20 2a 70 7a 42 75 66 66 65 72 20 69 73  ng, *pzBuffer is
1f036 20 73 65 74 20 74 6f 20 74 68 65 20 66 69 72 73   set to the firs
1f037 74 20 62 79 74 65 20 70 61 73 73 65 64 20 74 68  t byte passed th
1f038 65 0a 2a 2a 20 70 6f 72 74 69 6f 6e 20 6f 66 20  e.** portion of 
1f039 74 68 65 20 62 75 66 66 65 72 20 63 6f 70 69 65  the buffer copie
1f03a 64 20 69 6e 74 6f 20 62 79 20 74 68 69 73 20 66  d into by this f
1f03b 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
1f03c 69 63 20 45 78 70 72 20 2a 65 78 70 72 44 75 70  ic Expr *exprDup
1f03d 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78  (sqlite3 *db, Ex
1f03e 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73  pr *p, int flags
1f03f 2c 20 75 38 20 2a 2a 70 7a 42 75 66 66 65 72 29  , u8 **pzBuffer)
1f040 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 20 3d  {.  Expr *pNew =
1f041 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1f042 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
1f043 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20  e to return */. 
1f044 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63 6f   if( p ){.    co
1f045 6e 73 74 20 69 6e 74 20 69 73 52 65 71 75 69 72  nst int isRequir
1f046 65 53 70 61 6e 20 3d 20 28 66 6c 61 67 73 26 45  eSpan = (flags&E
1f047 58 50 52 44 55 50 5f 53 50 41 4e 29 3b 0a 20 20  XPRDUP_SPAN);.  
1f048 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 52 65    const int isRe
1f049 64 75 63 65 64 20 3d 20 28 66 6c 61 67 73 26 45  duced = (flags&E
1f04a 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
1f04b 20 20 20 20 75 38 20 2a 7a 41 6c 6c 6f 63 3b 0a      u8 *zAlloc;.
1f04c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 42  .    assert( pzB
1f04d 75 66 66 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65  uffer==0 || isRe
1f04e 64 75 63 65 64 20 29 3b 0a 0a 20 20 20 20 2f 2a  duced );..    /*
1f04f 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 72   Figure out wher
1f050 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6e  e to write the n
1f051 65 77 20 45 78 70 72 20 73 74 72 75 63 74 75 72  ew Expr structur
1f052 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 7a  e. */.    if( pz
1f053 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20  Buffer ){.      
1f054 7a 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75 66 66  zAlloc = *pzBuff
1f055 65 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  er;.    }else{. 
1f056 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d 20 73 71       zAlloc = sq
1f057 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
1f058 28 64 62 2c 20 64 75 70 65 64 45 78 70 72 53 69  (db, dupedExprSi
1f059 7a 65 28 70 2c 20 66 6c 61 67 73 29 29 3b 0a 20  ze(p, flags));. 
1f05a 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20 3d 20     }.    pNew = 
1f05b 28 45 78 70 72 20 2a 29 7a 41 6c 6c 6f 63 3b 0a  (Expr *)zAlloc;.
1f05c 0a 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b  .    if( pNew ){
1f05d 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 4e  .      /* Set nN
1f05e 65 77 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69  ewSize to the si
1f05f 7a 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  ze allocated for
1f060 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 70   the structure p
1f061 6f 69 6e 74 65 64 20 74 6f 0a 20 20 20 20 20 20  ointed to.      
1f062 2a 2a 20 62 79 20 70 4e 65 77 2e 20 54 68 69 73  ** by pNew. This
1f063 20 69 73 20 65 69 74 68 65 72 20 45 58 50 52 5f   is either EXPR_
1f064 46 55 4c 4c 53 49 5a 45 2c 20 45 58 50 52 5f 52  FULLSIZE, EXPR_R
1f065 45 44 55 43 45 44 53 49 5a 45 20 6f 72 0a 20 20  EDUCEDSIZE or.  
1f066 20 20 20 20 2a 2a 20 45 58 50 52 5f 54 4f 4b 45      ** EXPR_TOKE
1f067 4e 4f 4e 4c 59 53 49 5a 45 2e 20 6e 54 6f 6b 65  NONLYSIZE. nToke
1f068 6e 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  n is set to the 
1f069 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1f06a 63 6f 6e 73 75 6d 65 64 0a 20 20 20 20 20 20 2a  consumed.      *
1f06b 2a 20 62 79 20 74 68 65 20 63 6f 70 79 20 6f 66  * by the copy of
1f06c 20 74 68 65 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 20   the p->token.z 
1f06d 73 74 72 69 6e 67 20 28 69 66 20 61 6e 79 29 2e  string (if any).
1f06e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1f06f 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53 69  const int nNewSi
1f070 7a 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74  ze = dupedExprSt
1f071 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67  ructSize(p, flag
1f072 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  s);.      const 
1f073 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 28 70 2d  int nToken = (p-
1f074 3e 74 6f 6b 65 6e 2e 7a 20 3f 20 70 2d 3e 74 6f  >token.z ? p->to
1f075 6b 65 6e 2e 6e 20 2b 20 31 20 3a 20 30 29 3b 0a  ken.n + 1 : 0);.
1f076 20 20 20 20 20 20 69 66 28 20 69 73 52 65 64 75        if( isRedu
1f077 63 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ced ){.        a
1f078 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72  ssert( ExprHasPr
1f079 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64  operty(p, EP_Red
1f07a 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20  uced)==0 );.    
1f07b 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f      memcpy(zAllo
1f07c 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65 29 3b  c, p, nNewSize);
1f07d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1f07e 20 20 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 20        int nSize 
1f07f 3d 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65  = exprStructSize
1f080 28 70 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  (p);.        mem
1f081 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e  cpy(zAlloc, p, n
1f082 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 6d  Size);.        m
1f083 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b 6e 53  emset(&zAlloc[nS
1f084 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f 46 55  ize], 0, EXPR_FU
1f085 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b 0a 20  LLSIZE-nSize);. 
1f086 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
1f087 20 53 65 74 20 74 68 65 20 45 50 5f 52 65 64 75   Set the EP_Redu
1f088 63 65 64 20 61 6e 64 20 45 50 5f 54 6f 6b 65 6e  ced and EP_Token
1f089 4f 6e 6c 79 20 66 6c 61 67 73 20 61 70 70 72 6f  Only flags appro
1f08a 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20  priately. */.   
1f08b 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 26     pNew->flags &
1f08c 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64 7c 45  = ~(EP_Reduced|E
1f08d 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53  P_TokenOnly|EP_S
1f08e 70 61 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  panToken);.     
1f08f 20 73 77 69 74 63 68 28 20 6e 4e 65 77 53 69 7a   switch( nNewSiz
1f090 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73  e ){.        cas
1f091 65 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49  e EXPR_REDUCEDSI
1f092 5a 45 3a 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67  ZE:   pNew->flag
1f093 73 20 7c 3d 20 45 50 5f 52 65 64 75 63 65 64 3b  s |= EP_Reduced;
1f094 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1f095 63 61 73 65 20 45 58 50 52 5f 54 4f 4b 45 4e 4f  case EXPR_TOKENO
1f096 4e 4c 59 53 49 5a 45 3a 20 70 4e 65 77 2d 3e 66  NLYSIZE: pNew->f
1f097 6c 61 67 73 20 7c 3d 20 45 50 5f 54 6f 6b 65 6e  lags |= EP_Token
1f098 4f 6e 6c 79 3b 20 62 72 65 61 6b 3b 0a 20 20 20  Only; break;.   
1f099 20 20 20 20 20 63 61 73 65 20 45 58 50 52 5f 53       case EXPR_S
1f09a 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 3a 20 70 4e  PANTOKENSIZE: pN
1f09b 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  ew->flags |= EP_
1f09c 53 70 61 6e 54 6f 6b 65 6e 3b 20 62 72 65 61 6b  SpanToken; break
1f09d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
1f09e 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 70 2d 3e   /* Copy the p->
1f09f 74 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69 66  token string, if
1f0a0 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69   any. */.      i
1f0a1 66 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20  f( nToken ){.   
1f0a2 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68       unsigned ch
1f0a3 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 26 7a 41  ar *zToken = &zA
1f0a4 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a  lloc[nNewSize];.
1f0a5 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a          memcpy(z
1f0a6 54 6f 6b 65 6e 2c 20 70 2d 3e 74 6f 6b 65 6e 2e  Token, p->token.
1f0a7 7a 2c 20 6e 54 6f 6b 65 6e 2d 31 29 3b 0a 20 20  z, nToken-1);.  
1f0a8 20 20 20 20 20 20 7a 54 6f 6b 65 6e 5b 6e 54 6f        zToken[nTo
1f0a9 6b 65 6e 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20  ken-1] = '\0';. 
1f0aa 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b         pNew->tok
1f0ab 65 6e 2e 64 79 6e 20 3d 20 30 3b 0a 20 20 20 20  en.dyn = 0;.    
1f0ac 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e      pNew->token.
1f0ad 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  z = zToken;.    
1f0ae 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30    }..      if( 0
1f0af 3d 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65  ==((p->flags|pNe
1f0b0 77 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f 54  w->flags) & EP_T
1f0b1 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20  okenOnly) ){.   
1f0b2 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20       /* Fill in 
1f0b3 74 68 65 20 70 4e 65 77 2d 3e 73 70 61 6e 20 74  the pNew->span t
1f0b4 6f 6b 65 6e 2c 20 69 66 20 72 65 71 75 69 72 65  oken, if require
1f0b5 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  d. */.        if
1f0b6 28 20 69 73 52 65 71 75 69 72 65 53 70 61 6e 20  ( isRequireSpan 
1f0b7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
1f0b8 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 70 2d 3e   p->token.z!=p->
1f0b9 73 70 61 6e 2e 7a 20 7c 7c 20 70 2d 3e 74 6f 6b  span.z || p->tok
1f0ba 65 6e 2e 6e 21 3d 70 2d 3e 73 70 61 6e 2e 6e 20  en.n!=p->span.n 
1f0bb 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
1f0bc 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20 26 7a  New->span.z = &z
1f0bd 41 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 2b 6e  Alloc[nNewSize+n
1f0be 54 6f 6b 65 6e 5d 3b 0a 20 20 20 20 20 20 20 20  Token];.        
1f0bf 20 20 20 20 6d 65 6d 63 70 79 28 28 63 68 61 72      memcpy((char
1f0c0 20 2a 29 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 2c   *)pNew->span.z,
1f0c1 20 70 2d 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73   p->span.z, p->s
1f0c2 70 61 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 20 20  pan.n);.        
1f0c3 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 64      pNew->span.d
1f0c4 79 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  yn = 0;.        
1f0c5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1f0c6 20 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e       pNew->span.
1f0c7 7a 20 3d 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e  z = pNew->token.
1f0c8 7a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  z;.            p
1f0c9 4e 65 77 2d 3e 73 70 61 6e 2e 6e 20 3d 20 70 4e  New->span.n = pN
1f0ca 65 77 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20  ew->token.n;.   
1f0cb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1f0cc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1f0cd 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d    pNew->span.z =
1f0ce 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e   0;.          pN
1f0cf 65 77 2d 3e 73 70 61 6e 2e 6e 20 3d 20 30 3b 0a  ew->span.n = 0;.
1f0d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1f0d1 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d  }..      if( 0==
1f0d2 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d  ((p->flags|pNew-
1f0d3 3e 66 6c 61 67 73 29 20 26 20 28 45 50 5f 54 6f  >flags) & (EP_To
1f0d4 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54  kenOnly|EP_SpanT
1f0d5 6f 6b 65 6e 29 29 20 29 7b 0a 20 20 20 20 20 20  oken)) ){.      
1f0d6 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65    /* Fill in the
1f0d7 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74   pNew->x.pSelect
1f0d8 20 6f 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73   or pNew->x.pLis
1f0d9 74 20 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20 20  t member. */.   
1f0da 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73       if( ExprHas
1f0db 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78  Property(p, EP_x
1f0dc 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20  IsSelect) ){.   
1f0dd 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70         pNew->x.p
1f0de 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
1f0df 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d  SelectDup(db, p-
1f0e0 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 73 52 65  >x.pSelect, isRe
1f0e1 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20  duced);.        
1f0e2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1f0e3 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d   pNew->x.pList =
1f0e4 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
1f0e5 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69  Dup(db, p->x.pLi
1f0e6 73 74 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a  st, isReduced);.
1f0e7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1f0e8 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c  }..      /* Fill
1f0e9 20 69 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20   in pNew->pLeft 
1f0ea 61 6e 64 20 70 4e 65 77 2d 3e 70 52 69 67 68 74  and pNew->pRight
1f0eb 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 45  . */.      if( E
1f0ec 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
1f0ed 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63  y(pNew, EP_Reduc
1f0ee 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  ed|EP_TokenOnly|
1f0ef 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20 29 7b  EP_SpanToken) ){
1f0f0 0a 20 20 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20  .        zAlloc 
1f0f1 2b 3d 20 64 75 70 65 64 45 78 70 72 4e 6f 64 65  += dupedExprNode
1f0f2 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a  Size(p, flags);.
1f0f3 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70 72          if( Expr
1f0f4 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77  HasProperty(pNew
1f0f5 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 7b  , EP_Reduced) ){
1f0f6 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d  .          pNew-
1f0f7 3e 70 4c 65 66 74 20 3d 20 65 78 70 72 44 75 70  >pLeft = exprDup
1f0f8 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 45  (db, p->pLeft, E
1f0f9 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 26  XPRDUP_REDUCE, &
1f0fa 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20  zAlloc);.       
1f0fb 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20     pNew->pRight 
1f0fc 3d 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d  = exprDup(db, p-
1f0fd 3e 70 52 69 67 68 74 2c 20 45 58 50 52 44 55 50  >pRight, EXPRDUP
1f0fe 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63  _REDUCE, &zAlloc
1f0ff 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1f100 20 20 20 20 20 69 66 28 20 70 7a 42 75 66 66 65       if( pzBuffe
1f101 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  r ){.          *
1f102 70 7a 42 75 66 66 65 72 20 3d 20 7a 41 6c 6c 6f  pzBuffer = zAllo
1f103 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
1f104 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 45 78     }else if( !Ex
1f105 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
1f106 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  (p, EP_TokenOnly
1f107 7c 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20 29  |EP_SpanToken) )
1f108 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  {.        pNew->
1f109 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45  pLeft = sqlite3E
1f10a 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c  xprDup(db, p->pL
1f10b 65 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  eft, 0);.       
1f10c 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20   pNew->pRight = 
1f10d 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
1f10e 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 30 29  b, p->pRight, 0)
1f10f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1f110 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
1f111 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  w;.}../*.** The 
1f112 66 6f 6c 6c 6f 77 69 6e 67 20 67 72 6f 75 70 20  following group 
1f113 6f 66 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65  of routines make
1f114 20 64 65 65 70 20 63 6f 70 69 65 73 20 6f 66 20   deep copies of 
1f115 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20  expressions,.** 
1f116 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 73  expression lists
1f117 2c 20 49 44 20 6c 69 73 74 73 2c 20 61 6e 64 20  , ID lists, and 
1f118 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
1f119 73 2e 20 20 54 68 65 20 63 6f 70 69 65 73 20 63  s.  The copies c
1f11a 61 6e 0a 2a 2a 20 62 65 20 64 65 6c 65 74 65 64  an.** be deleted
1f11b 20 28 62 79 20 62 65 69 6e 67 20 70 61 73 73 65   (by being passe
1f11c 64 20 74 6f 20 74 68 65 69 72 20 72 65 73 70 65  d to their respe
1f11d 63 74 69 76 65 20 2e 2e 2e 44 65 6c 65 74 65 28  ctive ...Delete(
1f11e 29 20 72 6f 75 74 69 6e 65 73 29 0a 2a 2a 20 77  ) routines).** w
1f11f 69 74 68 6f 75 74 20 65 66 66 65 63 74 69 6e 67  ithout effecting
1f120 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 73 2e 0a   the originals..
1f121 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 65 73  **.** The expres
1f122 73 69 6f 6e 20 6c 69 73 74 2c 20 49 44 2c 20 61  sion list, ID, a
1f123 6e 64 20 73 6f 75 72 63 65 20 6c 69 73 74 73 20  nd source lists 
1f124 72 65 74 75 72 6e 20 62 79 20 73 71 6c 69 74 65  return by sqlite
1f125 33 45 78 70 72 4c 69 73 74 44 75 70 28 29 2c 0a  3ExprListDup(),.
1f126 2a 2a 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  ** sqlite3IdList
1f127 44 75 70 28 29 2c 20 61 6e 64 20 73 71 6c 69 74  Dup(), and sqlit
1f128 65 33 53 72 63 4c 69 73 74 44 75 70 28 29 20 63  e3SrcListDup() c
1f129 61 6e 20 6e 6f 74 20 62 65 20 66 75 72 74 68 65  an not be furthe
1f12a 72 20 65 78 70 61 6e 64 65 64 20 0a 2a 2a 20 62  r expanded .** b
1f12b 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  y subsequent cal
1f12c 6c 73 20 74 6f 20 73 71 6c 69 74 65 2a 4c 69 73  ls to sqlite*Lis
1f12d 74 41 70 70 65 6e 64 28 29 20 72 6f 75 74 69 6e  tAppend() routin
1f12e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61  es..**.** Any ta
1f12f 62 6c 65 73 20 74 68 61 74 20 74 68 65 20 53 72  bles that the Sr
1f130 63 4c 69 73 74 20 6d 69 67 68 74 20 70 6f 69 6e  cList might poin
1f131 74 20 74 6f 20 61 72 65 20 6e 6f 74 20 64 75 70  t to are not dup
1f132 6c 69 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  licated..**.** T
1f133 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74  he flags paramet
1f134 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f  er contains a co
1f135 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65  mbination of the
1f136 20 45 58 50 52 44 55 50 5f 58 58 58 20 66 6c 61   EXPRDUP_XXX fla
1f137 67 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 45 58  gs. If.** the EX
1f138 50 52 44 55 50 5f 53 50 41 4e 20 66 6c 61 67 20  PRDUP_SPAN flag 
1f139 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 61 72  is set in the ar
1f13a 67 75 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72  gument parameter
1f13b 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 45  , then the .** E
1f13c 78 70 72 2e 73 70 61 6e 20 66 69 65 6c 64 20 6f  xpr.span field o
1f13d 66 20 74 68 65 20 69 6e 70 75 74 20 65 78 70 72  f the input expr
1f13e 65 73 73 69 6f 6e 20 69 73 20 63 6f 70 69 65 64  ession is copied
1f13f 2e 20 49 66 20 45 58 50 52 44 55 50 5f 53 50 41  . If EXPRDUP_SPA
1f140 4e 20 69 73 0a 2a 2a 20 63 6c 65 61 72 2c 20 74  N is.** clear, t
1f141 68 65 6e 20 74 68 65 20 45 78 70 72 2e 73 70 61  hen the Expr.spa
1f142 6e 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 72  n field of the r
1f143 65 74 75 72 6e 65 64 20 65 78 70 72 65 73 73 69  eturned expressi
1f144 6f 6e 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  on structure.** 
1f145 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a  is zeroed..**.**
1f146 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f   If the EXPRDUP_
1f147 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73  REDUCE flag is s
1f148 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72  et, then the str
1f149 75 63 74 75 72 65 20 72 65 74 75 72 6e 65 64 20  ucture returned 
1f14a 69 73 20 61 0a 2a 2a 20 74 72 75 6e 63 61 74 65  is a.** truncate
1f14b 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  d version of the
1f14c 20 75 73 75 61 6c 20 45 78 70 72 20 73 74 72 75   usual Expr stru
1f14d 63 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20  cture that will 
1f14e 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20  be stored as.** 
1f14f 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d  part of the in-m
1f150 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61  emory representa
1f151 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61  tion of the data
1f152 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a  base schema..*/.
1f153 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
1f154 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72  xpr *sqlite3Expr
1f155 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Dup(sqlite3 *db,
1f156 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c   Expr *p, int fl
1f157 61 67 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 65  ags){.  return e
1f158 78 70 72 44 75 70 28 64 62 2c 20 70 2c 20 66 6c  xprDup(db, p, fl
1f159 61 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54  ags, 0);.}.SQLIT
1f15a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1f15b 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28  qlite3TokenCopy(
1f15c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b  sqlite3 *db, Tok
1f15d 65 6e 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 54  en *pTo, const T
1f15e 6f 6b 65 6e 20 2a 70 46 72 6f 6d 29 7b 0a 20 20  oken *pFrom){.  
1f15f 69 66 28 20 70 54 6f 2d 3e 64 79 6e 20 29 20 73  if( pTo->dyn ) s
1f160 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f161 20 28 63 68 61 72 2a 29 70 54 6f 2d 3e 7a 29 3b   (char*)pTo->z);
1f162 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 20  .  if( pFrom->z 
1f163 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 6e 20 3d 20  ){.    pTo->n = 
1f164 70 46 72 6f 6d 2d 3e 6e 3b 0a 20 20 20 20 70 54  pFrom->n;.    pT
1f165 6f 2d 3e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69  o->z = (u8*)sqli
1f166 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
1f167 20 28 63 68 61 72 2a 29 70 46 72 6f 6d 2d 3e 7a   (char*)pFrom->z
1f168 2c 20 70 46 72 6f 6d 2d 3e 6e 29 3b 0a 20 20 20  , pFrom->n);.   
1f169 20 70 54 6f 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20   pTo->dyn = 1;. 
1f16a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 2d   }else{.    pTo-
1f16b 3e 7a 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 53 51  >z = 0;.  }.}.SQ
1f16c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
1f16d 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78  rList *sqlite3Ex
1f16e 70 72 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  prListDup(sqlite
1f16f 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20  3 *db, ExprList 
1f170 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  *p, int flags){.
1f171 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77    ExprList *pNew
1f172 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  ;.  struct ExprL
1f173 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c  ist_item *pItem,
1f174 20 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e   *pOldItem;.  in
1f175 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  t i;.  if( p==0 
1f176 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
1f177 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
1f178 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
1f179 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69  of(*pNew) );.  i
1f17a 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74  f( pNew==0 ) ret
1f17b 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69  urn 0;.  pNew->i
1f17c 45 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70  ECursor = 0;.  p
1f17d 4e 65 77 2d 3e 6e 45 78 70 72 20 3d 20 70 4e 65  New->nExpr = pNe
1f17e 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e  w->nAlloc = p->n
1f17f 45 78 70 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 20  Expr;.  pNew->a 
1f180 3d 20 70 49 74 65 6d 20 3d 20 73 71 6c 69 74 65  = pItem = sqlite
1f181 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
1f182 20 20 70 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 6f    p->nExpr*sizeo
1f183 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20  f(p->a[0]) );.  
1f184 69 66 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a  if( pItem==0 ){.
1f185 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f186 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20  e(db, pNew);.   
1f187 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a   return 0;.  } .
1f188 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e    pOldItem = p->
1f189 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  a;.  for(i=0; i<
1f18a 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  p->nExpr; i++, p
1f18b 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d  Item++, pOldItem
1f18c 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
1f18d 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 45 78 70  NewExpr;.    Exp
1f18e 72 20 2a 70 4f 6c 64 45 78 70 72 20 3d 20 70 4f  r *pOldExpr = pO
1f18f 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20  ldItem->pExpr;. 
1f190 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20     pItem->pExpr 
1f191 3d 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c  = pNewExpr = sql
1f192 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
1f193 70 4f 6c 64 45 78 70 72 2c 20 66 6c 61 67 73 29  pOldExpr, flags)
1f194 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61  ;.    pItem->zNa
1f195 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
1f196 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
1f197 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70  m->zName);.    p
1f198 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20  Item->sortOrder 
1f199 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 73 6f 72 74  = pOldItem->sort
1f19a 4f 72 64 65 72 3b 0a 20 20 20 20 70 49 74 65 6d  Order;.    pItem
1f19b 2d 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20  ->done = 0;.    
1f19c 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 70 4f  pItem->iCol = pO
1f19d 6c 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b 0a 20 20  ldItem->iCol;.  
1f19e 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20    pItem->iAlias 
1f19f 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 41 6c 69  = pOldItem->iAli
1f1a0 61 73 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  as;.  }.  return
1f1a1 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
1f1a2 49 66 20 63 75 72 73 6f 72 73 2c 20 74 72 69 67  If cursors, trig
1f1a3 67 65 72 73 2c 20 76 69 65 77 73 20 61 6e 64 20  gers, views and 
1f1a4 73 75 62 71 75 65 72 69 65 73 20 61 72 65 20 61  subqueries are a
1f1a5 6c 6c 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a  ll omitted from.
1f1a6 2a 2a 20 74 68 65 20 62 75 69 6c 64 2c 20 74 68  ** the build, th
1f1a7 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66  en none of the f
1f1a8 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
1f1a9 73 2c 20 65 78 63 65 70 74 20 66 6f 72 20 0a 2a  s, except for .*
1f1aa 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  * sqlite3SelectD
1f1ab 75 70 28 29 2c 20 63 61 6e 20 62 65 20 63 61 6c  up(), can be cal
1f1ac 6c 65 64 2e 20 73 71 6c 69 74 65 33 53 65 6c 65  led. sqlite3Sele
1f1ad 63 74 44 75 70 28 29 20 69 73 20 73 6f 6d 65 74  ctDup() is somet
1f1ae 69 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77  imes.** called w
1f1af 69 74 68 20 61 20 4e 55 4c 4c 20 61 72 67 75 6d  ith a NULL argum
1f1b0 65 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ent..*/.#if !def
1f1b1 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1f1b2 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
1f1b3 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
1f1b4 52 49 47 47 45 52 29 20 5c 0a 20 7c 7c 20 21 64  RIGGER) \. || !d
1f1b5 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1f1b6 49 54 5f 53 55 42 51 55 45 52 59 29 0a 53 51 4c  IT_SUBQUERY).SQL
1f1b7 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c  ITE_PRIVATE SrcL
1f1b8 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ist *sqlite3SrcL
1f1b9 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a  istDup(sqlite3 *
1f1ba 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20  db, SrcList *p, 
1f1bb 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 72  int flags){.  Sr
1f1bc 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69  cList *pNew;.  i
1f1bd 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74  nt i;.  int nByt
1f1be 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  e;.  if( p==0 ) 
1f1bf 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74  return 0;.  nByt
1f1c0 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b  e = sizeof(*p) +
1f1c1 20 28 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69   (p->nSrc>0 ? si
1f1c2 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20  zeof(p->a[0]) * 
1f1c3 28 70 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29  (p->nSrc-1) : 0)
1f1c4 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
1f1c5 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
1f1c6 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28  , nByte );.  if(
1f1c7 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72   pNew==0 ) retur
1f1c8 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 53 72  n 0;.  pNew->nSr
1f1c9 63 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63  c = pNew->nAlloc
1f1ca 20 3d 20 70 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f   = p->nSrc;.  fo
1f1cb 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 53 72 63  r(i=0; i<p->nSrc
1f1cc 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
1f1cd 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
1f1ce 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65  *pNewItem = &pNe
1f1cf 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72  w->a[i];.    str
1f1d0 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
1f1d1 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d   *pOldItem = &p-
1f1d2 3e 61 5b 69 5d 3b 0a 20 20 20 20 54 61 62 6c 65  >a[i];.    Table
1f1d3 20 2a 70 54 61 62 3b 0a 20 20 20 20 70 4e 65 77   *pTab;.    pNew
1f1d4 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20  Item->zDatabase 
1f1d5 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
1f1d6 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
1f1d7 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
1f1d8 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20  pNewItem->zName 
1f1d9 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
1f1da 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
1f1db 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77  zName);.    pNew
1f1dc 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73  Item->zAlias = s
1f1dd 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
1f1de 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c  b, pOldItem->zAl
1f1df 69 61 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74  ias);.    pNewIt
1f1e0 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 3d 20 70  em->jointype = p
1f1e1 4f 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70  OldItem->jointyp
1f1e2 65 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  e;.    pNewItem-
1f1e3 3e 69 43 75 72 73 6f 72 20 3d 20 70 4f 6c 64 49  >iCursor = pOldI
1f1e4 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
1f1e5 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 73 50 6f    pNewItem->isPo
1f1e6 70 75 6c 61 74 65 64 20 3d 20 70 4f 6c 64 49 74  pulated = pOldIt
1f1e7 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3b  em->isPopulated;
1f1e8 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a  .    pNewItem->z
1f1e9 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44  Index = sqlite3D
1f1ea 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  bStrDup(db, pOld
1f1eb 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20  Item->zIndex);. 
1f1ec 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74     pNewItem->not
1f1ed 49 6e 64 65 78 65 64 20 3d 20 70 4f 6c 64 49 74  Indexed = pOldIt
1f1ee 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3b 0a  em->notIndexed;.
1f1ef 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 49      pNewItem->pI
1f1f0 6e 64 65 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  ndex = pOldItem-
1f1f1 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 54 61  >pIndex;.    pTa
1f1f2 62 20 3d 20 70 4e 65 77 49 74 65 6d 2d 3e 70 54  b = pNewItem->pT
1f1f3 61 62 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70  ab = pOldItem->p
1f1f4 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Tab;.    if( pTa
1f1f5 62 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d  b ){.      pTab-
1f1f6 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20  >nRef++;.    }. 
1f1f7 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 53 65     pNewItem->pSe
1f1f8 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
1f1f9 6c 65 63 74 44 75 70 28 64 62 2c 20 70 4f 6c 64  lectDup(db, pOld
1f1fa 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 66  Item->pSelect, f
1f1fb 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49  lags);.    pNewI
1f1fc 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74  tem->pOn = sqlit
1f1fd 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f  e3ExprDup(db, pO
1f1fe 6c 64 49 74 65 6d 2d 3e 70 4f 6e 2c 20 66 6c 61  ldItem->pOn, fla
1f1ff 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  gs);.    pNewIte
1f200 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 73 71 6c 69  m->pUsing = sqli
1f201 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c  te3IdListDup(db,
1f202 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 73 69 6e   pOldItem->pUsin
1f203 67 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  g);.    pNewItem
1f204 2d 3e 63 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64  ->colUsed = pOld
1f205 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20  Item->colUsed;. 
1f206 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
1f207 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
1f208 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74  TE IdList *sqlit
1f209 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69  e3IdListDup(sqli
1f20a 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20  te3 *db, IdList 
1f20b 2a 70 29 7b 0a 20 20 49 64 4c 69 73 74 20 2a 70  *p){.  IdList *p
1f20c 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  New;.  int i;.  
1f20d 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1f20e 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  n 0;.  pNew = sq
1f20f 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
1f210 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65  (db, sizeof(*pNe
1f211 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  w) );.  if( pNew
1f212 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1f213 20 20 70 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e    pNew->nId = pN
1f214 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e  ew->nAlloc = p->
1f215 6e 49 64 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d  nId;.  pNew->a =
1f216 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1f217 52 61 77 28 64 62 2c 20 70 2d 3e 6e 49 64 2a 73  Raw(db, p->nId*s
1f218 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29  izeof(p->a[0]) )
1f219 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 61 3d  ;.  if( pNew->a=
1f21a 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1f21b 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77  3DbFree(db, pNew
1f21c 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
1f21d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
1f21e 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a  i<p->nId; i++){.
1f21f 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73      struct IdLis
1f220 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d  t_item *pNewItem
1f221 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a   = &pNew->a[i];.
1f222 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73      struct IdLis
1f223 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d  t_item *pOldItem
1f224 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20   = &p->a[i];.   
1f225 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65   pNewItem->zName
1f226 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
1f227 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
1f228 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65  >zName);.    pNe
1f229 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c  wItem->idx = pOl
1f22a 64 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a  dItem->idx;.  }.
1f22b 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
1f22c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f22d 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53  Select *sqlite3S
1f22e 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33  electDup(sqlite3
1f22f 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c   *db, Select *p,
1f230 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53   int flags){.  S
1f231 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 69  elect *pNew;.  i
1f232 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
1f233 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c   0;.  pNew = sql
1f234 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
1f235 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 29  db, sizeof(*p) )
1f236 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
1f237 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 2f 2a  ) return 0;.  /*
1f238 20 41 6c 77 61 79 73 20 6d 61 6b 65 20 61 20 63   Always make a c
1f239 6f 70 79 20 6f 66 20 74 68 65 20 73 70 61 6e 20  opy of the span 
1f23a 66 6f 72 20 74 6f 70 2d 6c 65 76 65 6c 20 65 78  for top-level ex
1f23b 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  pressions in the
1f23c 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  .  ** expression
1f23d 20 6c 69 73 74 2e 20 20 54 68 65 20 6c 6f 67 69   list.  The logi
1f23e 63 20 69 6e 20 53 45 4c 45 43 54 20 70 72 6f 63  c in SELECT proc
1f23f 65 73 73 69 6e 67 20 74 68 61 74 20 64 65 74 65  essing that dete
1f240 72 6d 69 6e 65 73 0a 20 20 2a 2a 20 74 68 65 20  rmines.  ** the 
1f241 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73  names of columns
1f242 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1f243 65 74 20 6e 65 65 64 73 20 74 68 69 73 20 69 6e  et needs this in
1f244 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 70  formation */.  p
1f245 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71  New->pEList = sq
1f246 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
1f247 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20  (db, p->pEList, 
1f248 66 6c 61 67 73 7c 45 58 50 52 44 55 50 5f 53 50  flags|EXPRDUP_SP
1f249 41 4e 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 53 72  AN);.  pNew->pSr
1f24a 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  c = sqlite3SrcLi
1f24b 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 53 72  stDup(db, p->pSr
1f24c 63 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65  c, flags);.  pNe
1f24d 77 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c 69  w->pWhere = sqli
1f24e 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
1f24f 2d 3e 70 57 68 65 72 65 2c 20 66 6c 61 67 73 29  ->pWhere, flags)
1f250 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70  ;.  pNew->pGroup
1f251 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  By = sqlite3Expr
1f252 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70  ListDup(db, p->p
1f253 47 72 6f 75 70 42 79 2c 20 66 6c 61 67 73 29 3b  GroupBy, flags);
1f254 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69 6e 67  .  pNew->pHaving
1f255 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
1f256 70 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e 67  p(db, p->pHaving
1f257 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77  , flags);.  pNew
1f258 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c  ->pOrderBy = sql
1f259 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
1f25a 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c  db, p->pOrderBy,
1f25b 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d   flags);.  pNew-
1f25c 3e 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 70  >op = p->op;.  p
1f25d 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20 73 71  New->pPrior = sq
1f25e 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
1f25f 62 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 66 6c  b, p->pPrior, fl
1f260 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c  ags);.  pNew->pL
1f261 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 45 78  imit = sqlite3Ex
1f262 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 69  prDup(db, p->pLi
1f263 6d 69 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  mit, flags);.  p
1f264 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20 73  New->pOffset = s
1f265 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
1f266 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20 66 6c  , p->pOffset, fl
1f267 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 69 4c  ags);.  pNew->iL
1f268 69 6d 69 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77  imit = 0;.  pNew
1f269 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ->iOffset = 0;. 
1f26a 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73 20   pNew->selFlags 
1f26b 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  = p->selFlags & 
1f26c 7e 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61  ~SF_UsesEphemera
1f26d 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69 67 68  l;.  pNew->pRigh
1f26e 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65  tmost = 0;.  pNe
1f26f 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  w->addrOpenEphm[
1f270 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d  0] = -1;.  pNew-
1f271 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d  >addrOpenEphm[1]
1f272 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61   = -1;.  pNew->a
1f273 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d  ddrOpenEphm[2] =
1f274 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e   -1;.  return pN
1f275 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c 49  ew;.}.#else.SQLI
1f276 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63  TE_PRIVATE Selec
1f277 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74  t *sqlite3Select
1f278 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Dup(sqlite3 *db,
1f279 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20   Select *p, int 
1f27a 66 6c 61 67 73 29 7b 0a 20 20 61 73 73 65 72 74  flags){.  assert
1f27b 28 20 70 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75  ( p==0 );.  retu
1f27c 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 0;.}.#endif..
1f27d 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
1f27e 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20   element to the 
1f27f 65 6e 64 20 6f 66 20 61 6e 20 65 78 70 72 65 73  end of an expres
1f280 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 70  sion list.  If p
1f281 4c 69 73 74 20 69 73 0a 2a 2a 20 69 6e 69 74 69  List is.** initi
1f282 61 6c 6c 79 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  ally NULL, then 
1f283 63 72 65 61 74 65 20 61 20 6e 65 77 20 65 78 70  create a new exp
1f284 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f  ression list..*/
1f285 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f286 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65  ExprList *sqlite
1f287 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
1f288 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1f289 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
1f28a 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
1f28b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
1f28c 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  st,        /* Li
1f28d 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61  st to which to a
1f28e 70 70 65 6e 64 2e 20 4d 69 67 68 74 20 62 65 20  ppend. Might be 
1f28f 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a  NULL */.  Expr *
1f290 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20  pExpr,          
1f291 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
1f292 74 6f 20 62 65 20 61 70 70 65 6e 64 65 64 20 2a  to be appended *
1f293 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
1f294 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
1f295 53 20 6b 65 79 77 6f 72 64 20 66 6f 72 20 74 68  S keyword for th
1f296 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  e expression */.
1f297 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1f298 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1f299 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b   if( pList==0 ){
1f29a 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  .    pList = sql
1f29b 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
1f29c 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72  (db, sizeof(Expr
1f29d 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28  List) );.    if(
1f29e 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20   pList==0 ){.   
1f29f 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
1f2a0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
1f2a1 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d  ( pList->nAlloc=
1f2a2 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  =0 );.  }.  if( 
1f2a3 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70  pList->nAlloc<=p
1f2a4 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20  List->nExpr ){. 
1f2a5 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
1f2a6 73 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20 20 20  st_item *a;.    
1f2a7 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e  int n = pList->n
1f2a8 41 6c 6c 6f 63 2a 32 20 2b 20 34 3b 0a 20 20 20  Alloc*2 + 4;.   
1f2a9 20 61 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65   a = sqlite3DbRe
1f2aa 61 6c 6c 6f 63 28 64 62 2c 20 70 4c 69 73 74 2d  alloc(db, pList-
1f2ab 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 28 70 4c 69  >a, n*sizeof(pLi
1f2ac 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 20  st->a[0]));.    
1f2ad 69 66 28 20 61 3d 3d 30 20 29 7b 0a 20 20 20 20  if( a==0 ){.    
1f2ae 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1f2af 20 20 20 7d 0a 20 20 20 20 70 4c 69 73 74 2d 3e     }.    pList->
1f2b0 61 20 3d 20 61 3b 0a 20 20 20 20 70 4c 69 73 74  a = a;.    pList
1f2b1 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  ->nAlloc = sqlit
1f2b2 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64  e3DbMallocSize(d
1f2b3 62 2c 20 61 29 2f 73 69 7a 65 6f 66 28 61 5b 30  b, a)/sizeof(a[0
1f2b4 5d 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ]);.  }.  assert
1f2b5 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 29 3b  ( pList->a!=0 );
1f2b6 0a 20 20 69 66 28 20 70 45 78 70 72 20 7c 7c 20  .  if( pExpr || 
1f2b7 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 73 74 72  pName ){.    str
1f2b8 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1f2b9 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 73  m *pItem = &pLis
1f2ba 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70  t->a[pList->nExp
1f2bb 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74  r++];.    memset
1f2bc 28 70 49 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f  (pItem, 0, sizeo
1f2bd 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20 20 20 20  f(*pItem));.    
1f2be 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73  pItem->zName = s
1f2bf 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1f2c0 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ken(db, pName);.
1f2c1 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72      pItem->pExpr
1f2c2 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 70 49   = pExpr;.    pI
1f2c3 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b  tem->iAlias = 0;
1f2c4 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c  .  }.  return pL
1f2c5 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20  ist;..no_mem:   
1f2c6 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65    .  /* Avoid le
1f2c7 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20  aking memory if 
1f2c8 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c 65  malloc has faile
1f2c9 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45  d. */.  sqlite3E
1f2ca 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45  xprDelete(db, pE
1f2cb 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  xpr);.  sqlite3E
1f2cc 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
1f2cd 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75  , pList);.  retu
1f2ce 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  rn 0;.}../*.** I
1f2cf 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1f2d0 20 6c 69 73 74 20 70 45 4c 69 73 74 20 63 6f 6e   list pEList con
1f2d1 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20  tains more than 
1f2d2 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74 73 2c  iLimit elements,
1f2d3 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 72  .** leave an err
1f2d4 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
1f2d5 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  arse..*/.SQLITE_
1f2d6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1f2d7 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63  ite3ExprListChec
1f2d8 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 72 73 65  kLength(.  Parse
1f2d9 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 78 70 72   *pParse,.  Expr
1f2da 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a 20 20  List *pEList,.  
1f2db 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a  const char *zObj
1f2dc 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d 78 20  ect.){.  int mx 
1f2dd 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c  = pParse->db->aL
1f2de 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1f2df 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73  T_COLUMN];.  tes
1f2e0 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26 26  tcase( pEList &&
1f2e1 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d   pEList->nExpr==
1f2e2 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  mx );.  testcase
1f2e3 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69  ( pEList && pELi
1f2e4 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31 20  st->nExpr==mx+1 
1f2e5 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20  );.  if( pEList 
1f2e6 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  && pEList->nExpr
1f2e7 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >mx ){.    sqlit
1f2e8 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1f2e9 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c  e, "too many col
1f2ea 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a 4f 62  umns in %s", zOb
1f2eb 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ject);.  }.}../*
1f2ec 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e  .** Delete an en
1f2ed 74 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e 20  tire expression 
1f2ee 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  list..*/.SQLITE_
1f2ef 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1f2f0 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
1f2f1 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
1f2f2 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
1f2f3 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
1f2f4 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1f2f5 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20  m *pItem;.  if( 
1f2f6 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
1f2f7 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  n;.  assert( pLi
1f2f8 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70 4c 69  st->a!=0 || (pLi
1f2f9 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26 26 20  st->nExpr==0 && 
1f2fa 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30  pList->nAlloc==0
1f2fb 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
1f2fc 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69  List->nExpr<=pLi
1f2fd 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20  st->nAlloc );.  
1f2fe 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d  for(pItem=pList-
1f2ff 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74  >a, i=0; i<pList
1f300 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49  ->nExpr; i++, pI
1f301 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  tem++){.    sqli
1f302 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1f303 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b  , pItem->pExpr);
1f304 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1f305 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e  ee(db, pItem->zN
1f306 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ame);.  }.  sqli
1f307 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
1f308 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74  ist->a);.  sqlit
1f309 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69  e3DbFree(db, pLi
1f30a 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  st);.}../*.** Th
1f30b 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
1f30c 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63 6b   Walker callback
1f30d 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e 70 69 20  s.  Walker.u.pi 
1f30e 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  is a pointer.** 
1f30f 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  to an integer.  
1f310 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
1f311 72 65 20 63 68 65 63 6b 69 6e 67 20 61 6e 20 65  re checking an e
1f312 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 65 65  xpression to see
1f313 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 20 63  .** if it is a c
1f314 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74 20 2a 57  onstant.  Set *W
1f315 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f 20 30 20  alker.u.pi to 0 
1f316 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
1f317 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 73  n is.** not cons
1f318 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  tant..**.** Thes
1f319 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69  e callback routi
1f31a 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  nes are used to 
1f31b 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f  implement the fo
1f31c 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
1f31d 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73     sqlite3ExprIs
1f31e 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a 20 20 20  Constant().**   
1f31f 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43    sqlite3ExprIsC
1f320 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29  onstantNotJoin()
1f321 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45  .**     sqlite3E
1f322 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46  xprIsConstantOrF
1f323 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a  unction().**.*/.
1f324 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72 4e  static int exprN
1f325 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61  odeIsConstant(Wa
1f326 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45  lker *pWalker, E
1f327 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 0a 20 20  xpr *pExpr){..  
1f328 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72 2d 3e 75  /* If pWalker->u
1f329 2e 69 20 69 73 20 33 20 74 68 65 6e 20 61 6e 79  .i is 3 then any
1f32a 20 74 65 72 6d 20 6f 66 20 74 68 65 20 65 78 70   term of the exp
1f32b 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6d  ression that com
1f32c 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65  es from.  ** the
1f32d 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
1f32e 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 20 64  uses of a join d
1f32f 69 73 71 75 61 6c 69 66 69 65 73 20 74 68 65 20  isqualifies the 
1f330 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20  expression.  ** 
1f331 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f 6e 73 69  from being consi
1f332 64 65 72 65 64 20 63 6f 6e 73 74 61 6e 74 2e 20  dered constant. 
1f333 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c 6b 65 72  */.  if( pWalker
1f334 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45 78 70 72  ->u.i==3 && Expr
1f335 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
1f336 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69  Expr, EP_FromJoi
1f337 6e 29 20 29 7b 0a 20 20 20 20 70 57 61 6c 6b 65  n) ){.    pWalke
1f338 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20  r->u.i = 0;.    
1f339 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1f33a 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28  ;.  }..  switch(
1f33b 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20   pExpr->op ){.  
1f33c 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66 75    /* Consider fu
1f33d 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 63 6f  nctions to be co
1f33e 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c 20 74 68  nstant if all th
1f33f 65 69 72 20 61 72 67 75 6d 65 6e 74 73 20 61 72  eir arguments ar
1f340 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 2a  e constant.    *
1f341 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e 75  * and pWalker->u
1f342 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 73  .i==2 */.    cas
1f343 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20  e TK_FUNCTION:. 
1f344 20 20 20 20 20 69 66 28 20 70 57 61 6c 6b 65 72       if( pWalker
1f345 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 74 75 72  ->u.i==2 ) retur
1f346 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61  n 0;.      /* Fa
1f347 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20  ll through */.  
1f348 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20 20    case TK_ID:.  
1f349 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e    case TK_COLUMN
1f34a 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  :.    case TK_AG
1f34b 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20  G_FUNCTION:.    
1f34c 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  case TK_AGG_COLU
1f34d 4d 4e 3a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  MN:.#ifndef SQLI
1f34e 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
1f34f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c  .    case TK_SEL
1f350 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  ECT:.    case TK
1f351 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 74  _EXISTS:.      t
1f352 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
1f353 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b  op==TK_SELECT );
1f354 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1f355 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45   pExpr->op==TK_E
1f356 58 49 53 54 53 20 29 3b 0a 23 65 6e 64 69 66 0a  XISTS );.#endif.
1f357 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1f358 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44  pExpr->op==TK_ID
1f359 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f35a 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
1f35b 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20  K_COLUMN );.    
1f35c 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
1f35d 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 46 55  r->op==TK_AGG_FU
1f35e 4e 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20  NCTION );.      
1f35f 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
1f360 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55  >op==TK_AGG_COLU
1f361 4d 4e 20 29 3b 0a 20 20 20 20 20 20 70 57 61 6c  MN );.      pWal
1f362 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20  ker->u.i = 0;.  
1f363 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
1f364 62 6f 72 74 3b 0a 20 20 20 20 64 65 66 61 75 6c  bort;.    defaul
1f365 74 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  t:.      return 
1f366 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  WRC_Continue;.  
1f367 7d 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  }.}.static int s
1f368 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74  electNodeIsConst
1f369 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  ant(Walker *pWal
1f36a 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74  ker, Select *Not
1f36b 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
1f36c 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
1f36d 64 29 3b 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75  d);.  pWalker->u
1f36e 2e 69 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  .i = 0;.  return
1f36f 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74   WRC_Abort;.}.st
1f370 61 74 69 63 20 69 6e 74 20 65 78 70 72 49 73 43  atic int exprIsC
1f371 6f 6e 73 74 28 45 78 70 72 20 2a 70 2c 20 69 6e  onst(Expr *p, in
1f372 74 20 69 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57  t initFlag){.  W
1f373 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69  alker w;.  w.u.i
1f374 20 3d 20 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77   = initFlag;.  w
1f375 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
1f376 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74   exprNodeIsConst
1f377 61 6e 74 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74  ant;.  w.xSelect
1f378 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63  Callback = selec
1f379 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b  tNodeIsConstant;
1f37a 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  .  sqlite3WalkEx
1f37b 70 72 28 26 77 2c 20 70 29 3b 0a 20 20 72 65 74  pr(&w, p);.  ret
1f37c 75 72 6e 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a  urn w.u.i;.}../*
1f37d 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72  .** Walk an expr
1f37e 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65  ession tree.  Re
1f37f 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78  turn 1 if the ex
1f380 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73  pression is cons
1f381 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66  tant.** and 0 if
1f382 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72   it involves var
1f383 69 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69  iables or functi
1f384 6f 6e 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20  on calls..**.** 
1f385 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
1f386 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
1f387 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74  n, a double-quot
1f388 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 22  ed string (ex: "
1f389 61 62 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73  abc").** is cons
1f38a 69 64 65 72 65 64 20 61 20 76 61 72 69 61 62 6c  idered a variabl
1f38b 65 20 62 75 74 20 61 20 73 69 6e 67 6c 65 2d 71  e but a single-q
1f38c 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 78  uoted string (ex
1f38d 3a 20 27 61 62 63 27 29 20 69 73 0a 2a 2a 20 61  : 'abc') is.** a
1f38e 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51   constant..*/.SQ
1f38f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1f390 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
1f391 6e 73 74 61 6e 74 28 45 78 70 72 20 2a 70 29 7b  nstant(Expr *p){
1f392 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73  .  return exprIs
1f393 43 6f 6e 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a  Const(p, 1);.}..
1f394 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78  /*.** Walk an ex
1f395 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
1f396 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
1f397 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f  expression is co
1f398 6e 73 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64  nstant.** that d
1f399 6f 65 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65  oes no originate
1f39a 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20   from the ON or 
1f39b 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66  USING clauses of
1f39c 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75   a join..** Retu
1f39d 72 6e 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c  rn 0 if it invol
1f39e 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72  ves variables or
1f39f 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20   function calls 
1f3a0 6f 72 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a  or terms from.**
1f3a1 20 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20   an ON or USING 
1f3a2 63 6c 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  clause..*/.SQLIT
1f3a3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1f3a4 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
1f3a5 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20  antNotJoin(Expr 
1f3a6 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78  *p){.  return ex
1f3a7 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b  prIsConst(p, 3);
1f3a8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61  .}../*.** Walk a
1f3a9 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
1f3aa 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  e.  Return 1 if 
1f3ab 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
1f3ac 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72  s constant.** or
1f3ad 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c   a function call
1f3ae 20 77 69 74 68 20 63 6f 6e 73 74 61 6e 74 20 61   with constant a
1f3af 72 67 75 6d 65 6e 74 73 2e 20 20 52 65 74 75 72  rguments.  Retur
1f3b0 6e 20 61 6e 64 20 30 20 69 66 20 74 68 65 72 65  n and 0 if there
1f3b1 0a 2a 2a 20 61 72 65 20 61 6e 79 20 76 61 72 69  .** are any vari
1f3b2 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  ables..**.** For
1f3b3 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66   the purposes of
1f3b4 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
1f3b5 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20  a double-quoted 
1f3b6 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63  string (ex: "abc
1f3b7 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  ").** is conside
1f3b8 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62  red a variable b
1f3b9 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74  ut a single-quot
1f3ba 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27  ed string (ex: '
1f3bb 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f  abc') is.** a co
1f3bc 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  nstant..*/.SQLIT
1f3bd 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1f3be 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
1f3bf 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78  antOrFunction(Ex
1f3c0 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  pr *p){.  return
1f3c1 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20   exprIsConst(p, 
1f3c2 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  2);.}../*.** If 
1f3c3 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70  the expression p
1f3c4 20 63 6f 64 65 73 20 61 20 63 6f 6e 73 74 61 6e   codes a constan
1f3c5 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69  t integer that i
1f3c6 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a  s small enough.*
1f3c7 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 33 32  * to fit in a 32
1f3c8 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65  -bit integer, re
1f3c9 74 75 72 6e 20 31 20 61 6e 64 20 70 75 74 20 74  turn 1 and put t
1f3ca 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1f3cb 69 6e 74 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70  integer.** in *p
1f3cc 56 61 6c 75 65 2e 20 20 49 66 20 74 68 65 20 65  Value.  If the e
1f3cd 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74  xpression is not
1f3ce 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 69   an integer or i
1f3cf 66 20 69 74 20 69 73 20 74 6f 6f 20 62 69 67 0a  f it is too big.
1f3d0 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 73  ** to fit in a s
1f3d1 69 67 6e 65 64 20 33 32 2d 62 69 74 20 69 6e 74  igned 32-bit int
1f3d2 65 67 65 72 2c 20 72 65 74 75 72 6e 20 30 20 61  eger, return 0 a
1f3d3 6e 64 20 6c 65 61 76 65 20 2a 70 56 61 6c 75 65  nd leave *pValue
1f3d4 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53   unchanged..*/.S
1f3d5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1f3d6 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  t sqlite3ExprIsI
1f3d7 6e 74 65 67 65 72 28 45 78 70 72 20 2a 70 2c 20  nteger(Expr *p, 
1f3d8 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20  int *pValue){.  
1f3d9 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 69 66  int rc = 0;.  if
1f3da 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  ( p->flags & EP_
1f3db 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20  IntValue ){.    
1f3dc 2a 70 56 61 6c 75 65 20 3d 20 70 2d 3e 69 54 61  *pValue = p->iTa
1f3dd 62 6c 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ble;.    return 
1f3de 31 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28  1;.  }.  switch(
1f3df 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61   p->op ){.    ca
1f3e0 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b  se TK_INTEGER: {
1f3e1 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1f3e2 74 65 33 47 65 74 49 6e 74 33 32 28 28 63 68 61  te3GetInt32((cha
1f3e3 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70  r*)p->token.z, p
1f3e4 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 62 72  Value);.      br
1f3e5 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1f3e6 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a  ase TK_UPLUS: {.
1f3e7 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1f3e8 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
1f3e9 70 2d 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75 65  p->pLeft, pValue
1f3ea 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1f3eb 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
1f3ec 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20  K_UMINUS: {.    
1f3ed 20 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20 69    int v;.      i
1f3ee 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  f( sqlite3ExprIs
1f3ef 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74  Integer(p->pLeft
1f3f0 2c 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20 20  , &v) ){.       
1f3f1 20 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20   *pValue = -v;. 
1f3f2 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20         rc = 1;. 
1f3f3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
1f3f4 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
1f3f5 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20  fault: break;.  
1f3f6 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  }.  if( rc ){.  
1f3f7 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54    p->op = TK_INT
1f3f8 45 47 45 52 3b 0a 20 20 20 20 70 2d 3e 66 6c 61  EGER;.    p->fla
1f3f9 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75  gs |= EP_IntValu
1f3fa 65 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 6c 65  e;.    p->iTable
1f3fb 20 3d 20 2a 70 56 61 6c 75 65 3b 0a 20 20 7d 0a   = *pValue;.  }.
1f3fc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1f3fd 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
1f3fe 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 73  E if the given s
1f3ff 74 72 69 6e 67 20 69 73 20 61 20 72 6f 77 2d 69  tring is a row-i
1f400 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a  d column name..*
1f401 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f402 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f   int sqlite3IsRo
1f403 77 69 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  wid(const char *
1f404 7a 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  z){.  if( sqlite
1f405 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 5f 52 4f  3StrICmp(z, "_RO
1f406 57 49 44 5f 22 29 3d 3d 30 20 29 20 72 65 74 75  WID_")==0 ) retu
1f407 72 6e 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69  rn 1;.  if( sqli
1f408 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 52  te3StrICmp(z, "R
1f409 4f 57 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75  OWID")==0 ) retu
1f40a 72 6e 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69  rn 1;.  if( sqli
1f40b 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 4f  te3StrICmp(z, "O
1f40c 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  ID")==0 ) return
1f40d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a   1;.  return 0;.
1f40e 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1f40f 74 72 75 65 20 69 66 20 74 68 65 20 49 4e 20 6f  true if the IN o
1f410 70 65 72 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61  perator optimiza
1f411 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20  tion is enabled 
1f412 61 6e 64 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43  and.** the SELEC
1f413 54 20 73 74 61 74 65 6d 65 6e 74 20 70 20 65 78  T statement p ex
1f414 69 73 74 73 20 61 6e 64 20 69 73 20 6f 66 20 74  ists and is of t
1f415 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 20 66 6f 72  he.** simple for
1f416 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c  m:.**.**     SEL
1f417 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f  ECT <column> FRO
1f418 4d 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20  M <table>.**.** 
1f419 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
1f41a 61 73 65 2c 20 69 74 20 6d 61 79 20 62 65 20 70  ase, it may be p
1f41b 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61  ossible to use a
1f41c 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65  n existing table
1f41d 0a 2a 2a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73  .** or index ins
1f41e 74 65 61 64 20 6f 66 20 67 65 6e 65 72 61 74 69  tead of generati
1f41f 6e 67 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20  ng an epheremal 
1f420 74 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65  table..*/.#ifnde
1f421 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
1f422 42 51 55 45 52 59 0a 73 74 61 74 69 63 20 69 6e  BQUERY.static in
1f423 74 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72  t isCandidateFor
1f424 49 6e 4f 70 74 28 53 65 6c 65 63 74 20 2a 70 29  InOpt(Select *p)
1f425 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  {.  SrcList *pSr
1f426 63 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  c;.  ExprList *p
1f427 45 4c 69 73 74 3b 0a 20 20 54 61 62 6c 65 20 2a  EList;.  Table *
1f428 70 54 61 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30  pTab;.  if( p==0
1f429 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
1f42a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f42b 2a 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  * right-hand sid
1f42c 65 20 6f 66 20 49 4e 20 69 73 20 53 45 4c 45 43  e of IN is SELEC
1f42d 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50  T */.  if( p->pP
1f42e 72 69 6f 72 20 29 20 72 65 74 75 72 6e 20 30 3b  rior ) return 0;
1f42f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1f430 20 4e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 20   Not a compound 
1f431 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20  SELECT */.  if( 
1f432 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53  p->selFlags & (S
1f433 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67  F_Distinct|SF_Ag
1f434 67 72 65 67 61 74 65 29 20 29 7b 0a 20 20 20 20  gregate) ){.    
1f435 20 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 4e    return 0; /* N
1f436 6f 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f  o DISTINCT keywo
1f437 72 64 20 61 6e 64 20 6e 6f 20 61 67 67 72 65 67  rd and no aggreg
1f438 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f  ate functions */
1f439 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47  .  }.  if( p->pG
1f43a 72 6f 75 70 42 79 20 29 20 72 65 74 75 72 6e 20  roupBy ) return 
1f43b 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  0;            /*
1f43c 20 48 61 73 20 6e 6f 20 47 52 4f 55 50 20 42 59   Has no GROUP BY
1f43d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28   clause */.  if(
1f43e 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 65 74   p->pLimit ) ret
1f43f 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
1f440 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49      /* Has no LI
1f441 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  MIT clause */.  
1f442 69 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 20 29  if( p->pOffset )
1f443 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
1f444 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74   p->pWhere ) ret
1f445 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
1f446 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48      /* Has no WH
1f447 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
1f448 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  pSrc = p->pSrc;.
1f449 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21 3d    assert( pSrc!=
1f44a 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63 2d  0 );.  if( pSrc-
1f44b 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75 72  >nSrc!=1 ) retur
1f44c 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  n 0;          /*
1f44d 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20   Single term in 
1f44e 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
1f44f 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e   if( pSrc->a[0].
1f450 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72 6e  pSelect ) return
1f451 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20   0;     /* FROM 
1f452 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61 20  clause is not a 
1f453 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 70 54  subquery */.  pT
1f454 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e  ab = pSrc->a[0].
1f455 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61 62  pTab;.  if( pTab
1f456 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1f457 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c    if( pTab->pSel
1f458 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ect ) return 0; 
1f459 20 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d           /* FROM
1f45a 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61   clause is not a
1f45b 20 76 69 65 77 20 2a 2f 0a 20 20 69 66 28 20 49   view */.  if( I
1f45c 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
1f45d 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
1f45e 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
1f45f 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74   not a virtual t
1f460 61 62 6c 65 20 2a 2f 0a 20 20 70 45 4c 69 73 74  able */.  pEList
1f461 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20   = p->pEList;.  
1f462 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  if( pEList->nExp
1f463 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b  r!=1 ) return 0;
1f464 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 6f         /* One co
1f465 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75  lumn in the resu
1f466 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 66 28 20  lt set */.  if( 
1f467 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
1f468 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  pr->op!=TK_COLUM
1f469 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a  N ) return 0; /*
1f46a 20 52 65 73 75 6c 74 20 69 73 20 61 20 63 6f 6c   Result is a col
1f46b 75 6d 6e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  umn */.  return 
1f46c 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  1;.}.#endif /* S
1f46d 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1f46e 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ERY */../*.** Th
1f46f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
1f470 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65  sed by the imple
1f471 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
1f472 20 49 4e 20 28 2e 2e 2e 29 20 6f 70 65 72 61 74   IN (...) operat
1f473 6f 72 2e 0a 2a 2a 20 49 74 27 73 20 6a 6f 62 20  or..** It's job 
1f474 69 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63 72  is to find or cr
1f475 65 61 74 65 20 61 20 62 2d 74 72 65 65 20 73 74  eate a b-tree st
1f476 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79  ructure that may
1f477 20 62 65 20 75 73 65 64 0a 2a 2a 20 65 69 74 68   be used.** eith
1f478 65 72 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6d  er to test for m
1f479 65 6d 62 65 72 73 68 69 70 20 6f 66 20 74 68 65  embership of the
1f47a 20 28 2e 2e 2e 29 20 73 65 74 20 6f 72 20 74 6f   (...) set or to
1f47b 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
1f47c 0a 2a 2a 20 69 74 73 20 6d 65 6d 62 65 72 73 2c  .** its members,
1f47d 20 73 6b 69 70 70 69 6e 67 20 64 75 70 6c 69 63   skipping duplic
1f47e 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ates..**.** The 
1f47f 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 6f 6e  cursor opened on
1f480 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 28   the structure (
1f481 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c 20  database table, 
1f482 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 0a  database index .
1f483 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c 20 74  ** or ephermal t
1f484 61 62 6c 65 29 20 69 73 20 73 74 6f 72 65 64 20  able) is stored 
1f485 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 62 65  in pX->iTable be
1f486 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69  fore this functi
1f487 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20 54  on returns..** T
1f488 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
1f489 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20  e indicates the 
1f48a 73 74 72 75 63 74 75 72 65 20 74 79 70 65 2c 20  structure type, 
1f48b 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
1f48c 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57  *   IN_INDEX_ROW
1f48d 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20  ID - The cursor 
1f48e 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20  was opened on a 
1f48f 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a  database table..
1f490 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e  **   IN_INDEX_IN
1f491 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f 72  DEX - The cursor
1f492 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61   was opened on a
1f493 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 2e   database index.
1f494 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45  .**   IN_INDEX_E
1f495 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73 6f  PH -   The curso
1f496 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20  r was opened on 
1f497 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65 61  a specially crea
1f498 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ted and.**      
1f499 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f                po
1f49a 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d 61  pulated epherema
1f49b 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  l table..**.** A
1f49c 6e 20 65 78 69 73 74 69 6e 67 20 73 74 72 75 63  n existing struc
1f49d 74 75 72 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65  ture may only be
1f49e 20 75 73 65 64 20 69 66 20 74 68 65 20 53 45 4c   used if the SEL
1f49f 45 43 54 20 69 73 20 6f 66 20 74 68 65 20 73 69  ECT is of the si
1f4a0 6d 70 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a  mple.** form:.**
1f4a1 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c  .**     SELECT <
1f4a2 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61  column> FROM <ta
1f4a3 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 72  ble>.**.** If pr
1f4a4 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74  NotFound paramet
1f4a5 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68  er is 0, then th
1f4a6 65 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c  e structure will
1f4a7 20 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72   be used to iter
1f4a8 61 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74  ate.** through t
1f4a9 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20  he set members, 
1f4aa 73 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70  skipping any dup
1f4ab 6c 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73  licates. In this
1f4ac 20 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65   case an.** ephe
1f4ad 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74  remal table must
1f4ae 20 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20   be used unless 
1f4af 74 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f  the selected <co
1f4b0 6c 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74  lumn> is guarant
1f4b1 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69  eed.** to be uni
1f4b2 71 75 65 20 2d 20 65 69 74 68 65 72 20 62 65 63  que - either bec
1f4b3 61 75 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e  ause it is an IN
1f4b4 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
1f4b5 59 20 6f 72 20 69 74 0a 2a 2a 20 69 73 20 75 6e  Y or it.** is un
1f4b6 69 71 75 65 20 62 79 20 76 69 72 74 75 65 20 6f  ique by virtue o
1f4b7 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f  f a constraint o
1f4b8 72 20 69 6d 70 6c 69 63 69 74 20 69 6e 64 65 78  r implicit index
1f4b9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
1f4ba 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65  rNotFound parame
1f4bb 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68  ter is not 0, th
1f4bc 65 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65  en the structure
1f4bd 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a   will be used .*
1f4be 2a 20 66 6f 72 20 66 61 73 74 20 73 65 74 20 6d  * for fast set m
1f4bf 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 73 2e  embership tests.
1f4c0 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e   In this case an
1f4c1 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65   epheremal table
1f4c2 20 6d 75 73 74 20 0a 2a 2a 20 62 65 20 75 73 65   must .** be use
1f4c3 64 20 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e  d unless <column
1f4c4 3e 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20  > is an INTEGER 
1f4c5 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 61  PRIMARY KEY or a
1f4c6 6e 20 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20  n index can .** 
1f4c7 62 65 20 66 6f 75 6e 64 20 77 69 74 68 20 3c 63  be found with <c
1f4c8 6f 6c 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65  olumn> as its le
1f4c9 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a  ft-most column..
1f4ca 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73  **.** When the s
1f4cb 74 72 75 63 74 75 72 65 20 69 73 20 62 65 69 6e  tructure is bein
1f4cc 67 20 75 73 65 64 20 66 6f 72 20 73 65 74 20 6d  g used for set m
1f4cd 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 73 2c  embership tests,
1f4ce 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 6e 65 65   the user.** nee
1f4cf 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68  ds to know wheth
1f4d0 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73 74  er or not the st
1f4d1 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
1f4d2 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a   an SQL NULL .**
1f4d3 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20   value in order 
1f4d4 74 6f 20 63 6f 72 72 65 63 74 6c 79 20 65 76 61  to correctly eva
1f4d5 6c 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e  luate expression
1f4d6 73 20 6c 69 6b 65 20 22 58 20 49 4e 20 28 59 2c  s like "X IN (Y,
1f4d7 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68 65 72   Z)"..** If ther
1f4d8 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 68  e is a chance th
1f4d9 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72 65  at the structure
1f4da 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20 4e   may contain a N
1f4db 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a 2a 20  ULL value at.** 
1f4dc 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20 61 20  runtime, then a 
1f4dd 72 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c 6f  register is allo
1f4de 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 72 65  cated and the re
1f4df 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 72  gister number wr
1f4e0 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e  itten.** to *prN
1f4e1 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 68 65 72  otFound. If ther
1f4e2 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74  e is no chance t
1f4e3 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72  hat the structur
1f4e4 65 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20  e contains a.** 
1f4e5 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e  NULL value, then
1f4e6 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20   *prNotFound is 
1f4e7 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a  left unchanged..
1f4e8 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73  **.** If a regis
1f4e9 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ter is allocated
1f4ea 20 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f   and its locatio
1f4eb 6e 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e  n stored in *prN
1f4ec 6f 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a  otFound, then.**
1f4ed 20 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c   its initial val
1f4ee 75 65 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20 74  ue is NULL. If t
1f4ef 68 65 20 73 74 72 75 63 74 75 72 65 20 64 6f 65  he structure doe
1f4f0 73 20 6e 6f 74 20 72 65 6d 61 69 6e 20 63 6f 6e  s not remain con
1f4f1 73 74 61 6e 74 0a 2a 2a 20 66 6f 72 20 74 68 65  stant.** for the
1f4f2 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 65   duration of the
1f4f3 20 71 75 65 72 79 20 28 69 2e 65 2e 20 74 68 65   query (i.e. the
1f4f4 20 73 65 74 20 69 73 20 61 20 63 6f 72 72 65 6c   set is a correl
1f4f5 61 74 65 64 20 73 75 62 2d 73 65 6c 65 63 74 29  ated sub-select)
1f4f6 2c 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  , .** the value 
1f4f7 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  of the allocated
1f4f8 20 72 65 67 69 73 74 65 72 20 69 73 20 72 65 73   register is res
1f4f9 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20  et to NULL each 
1f4fa 74 69 6d 65 20 74 68 65 20 0a 2a 2a 20 73 74 72  time the .** str
1f4fb 75 63 74 75 72 65 20 69 73 20 72 65 70 6f 70 75  ucture is repopu
1f4fc 6c 61 74 65 64 2e 20 54 68 69 73 20 61 6c 6c 6f  lated. This allo
1f4fd 77 73 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  ws the caller to
1f4fe 20 75 73 65 20 76 64 62 65 20 63 6f 64 65 20 0a   use vdbe code .
1f4ff 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  ** equivalent to
1f500 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
1f501 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 72 65 67 69  **.**   if( regi
1f502 73 74 65 72 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a  ster==NULL ){.**
1f503 20 20 20 20 20 68 61 73 5f 6e 75 6c 6c 20 3d 20       has_null = 
1f504 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 73 74  <test if data st
1f505 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
1f506 20 6e 75 6c 6c 3e 0a 2a 2a 20 20 20 20 20 72 65   null>.**     re
1f507 67 69 73 74 65 72 20 3d 20 31 0a 2a 2a 20 20 20  gister = 1.**   
1f508 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  }.**.** in order
1f509 20 74 6f 20 61 76 6f 69 64 20 72 75 6e 6e 69 6e   to avoid runnin
1f50a 67 20 74 68 65 20 3c 74 65 73 74 20 69 66 20 64  g the <test if d
1f50b 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f  ata structure co
1f50c 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20  ntains null>.** 
1f50d 74 65 73 74 20 6d 6f 72 65 20 6f 66 74 65 6e 20  test more often 
1f50e 74 68 61 6e 20 69 73 20 6e 65 63 65 73 73 61 72  than is necessar
1f50f 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  y..*/.#ifndef SQ
1f510 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
1f511 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  RY.SQLITE_PRIVAT
1f512 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e  E int sqlite3Fin
1f513 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20 2a  dInIndex(Parse *
1f514 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 58  pParse, Expr *pX
1f515 2c 20 69 6e 74 20 2a 70 72 4e 6f 74 46 6f 75 6e  , int *prNotFoun
1f516 64 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 3b  d){.  Select *p;
1f517 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 30  .  int eType = 0
1f518 3b 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70  ;.  int iTab = p
1f519 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
1f51a 20 69 6e 74 20 6d 75 73 74 42 65 55 6e 69 71 75   int mustBeUniqu
1f51b 65 20 3d 20 21 70 72 4e 6f 74 46 6f 75 6e 64 3b  e = !prNotFound;
1f51c 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 77  ..  /* The follw
1f51d 69 6e 67 20 69 66 28 2e 2e 2e 29 20 65 78 70 72  ing if(...) expr
1f51e 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 69  ession is true i
1f51f 66 20 74 68 65 20 53 45 4c 45 43 54 20 69 73 20  f the SELECT is 
1f520 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d  of the .  ** sim
1f521 70 6c 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20  ple form:.  **. 
1f522 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c   **     SELECT <
1f523 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61  column> FROM <ta
1f524 62 6c 65 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ble>.  **.  ** I
1f525 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
1f526 73 65 2c 20 69 74 20 6d 61 79 20 62 65 20 70 6f  se, it may be po
1f527 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e  ssible to use an
1f528 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 0a   existing table.
1f529 20 20 2a 2a 20 6f 72 20 69 6e 64 65 78 20 69 6e    ** or index in
1f52a 73 74 65 61 64 20 6f 66 20 67 65 6e 65 72 61 74  stead of generat
1f52b 69 6e 67 20 61 6e 20 65 70 68 65 72 65 6d 61 6c  ing an epheremal
1f52c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70   table..  */.  p
1f52d 20 3d 20 28 45 78 70 72 48 61 73 50 72 6f 70 65   = (ExprHasPrope
1f52e 72 74 79 28 70 58 2c 20 45 50 5f 78 49 73 53 65  rty(pX, EP_xIsSe
1f52f 6c 65 63 74 29 20 3f 20 70 58 2d 3e 78 2e 70 53  lect) ? pX->x.pS
1f530 65 6c 65 63 74 20 3a 20 30 29 3b 0a 20 20 69 66  elect : 0);.  if
1f531 28 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72  ( isCandidateFor
1f532 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20  InOpt(p) ){.    
1f533 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1f534 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20  arse->db;       
1f535 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
1f536 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
1f537 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72  .    Expr *pExpr
1f538 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b   = p->pEList->a[
1f539 30 5d 2e 70 45 78 70 72 3b 20 20 20 2f 2a 20 45  0].pExpr;   /* E
1f53a 78 70 72 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d  xpression <colum
1f53b 6e 3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  n> */.    int iC
1f53c 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c  ol = pExpr->iCol
1f53d 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
1f53e 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
1f53f 75 6d 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a  umn <column> */.
1f540 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71      Vdbe *v = sq
1f541 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
1f542 72 73 65 29 3b 20 20 20 20 20 20 2f 2a 20 56 69  rse);      /* Vi
1f543 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 65  rtual machine be
1f544 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20  ing coded */.   
1f545 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
1f546 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61  ->pSrc->a[0].pTa
1f547 62 3b 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65  b;      /* Table
1f548 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20   <table>. */.   
1f549 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
1f54a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f54b 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
1f54c 61 73 65 20 69 64 78 20 66 6f 72 20 70 54 61 62  ase idx for pTab
1f54d 20 2a 2f 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43   */.   .    /* C
1f54e 6f 64 65 20 61 6e 20 4f 50 5f 56 65 72 69 66 79  ode an OP_Verify
1f54f 43 6f 6f 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61  Cookie and OP_Ta
1f550 62 6c 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62  bleLock for <tab
1f551 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20  le>. */.    iDb 
1f552 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
1f553 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
1f554 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 73  >pSchema);.    s
1f555 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
1f556 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69  Schema(pParse, i
1f557 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
1f558 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65  TableLock(pParse
1f559 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75  , iDb, pTab->tnu
1f55a 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  m, 0, pTab->zNam
1f55b 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73  e);..    /* This
1f55c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c   function is onl
1f55d 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77  y called from tw
1f55e 6f 20 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74  o places. In bot
1f55f 68 20 63 61 73 65 73 20 74 68 65 20 76 64 62 65  h cases the vdbe
1f560 0a 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65  .    ** has alre
1f561 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ady been allocat
1f562 65 64 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 71  ed. So assume sq
1f563 6c 69 74 65 33 47 65 74 56 64 62 65 28 29 20 69  lite3GetVdbe() i
1f564 73 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20  s always.    ** 
1f565 73 75 63 63 65 73 73 66 75 6c 20 68 65 72 65 2e  successful here.
1f566 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
1f567 72 74 28 76 29 3b 0a 20 20 20 20 69 66 28 20 69  rt(v);.    if( i
1f568 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 69  Col<0 ){.      i
1f569 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72  nt iMem = ++pPar
1f56a 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
1f56b 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20  int iAddr;.     
1f56c 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
1f56d 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 0a  Btree(v, iDb);..
1f56e 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71        iAddr = sq
1f56f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
1f570 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b  v, OP_If, iMem);
1f571 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1f572 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
1f573 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29  nteger, 1, iMem)
1f574 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
1f575 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
1f576 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54 61  , iTab, iDb, pTa
1f577 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b  b, OP_OpenRead);
1f578 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 49  .      eType = I
1f579 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 0a  N_INDEX_ROWID;..
1f57a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f57b 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64  eJumpHere(v, iAd
1f57c 64 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  dr);.    }else{.
1f57d 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
1f57e 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
1f57f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
1f580 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
1f581 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  */..      /* The
1f582 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
1f583 6e 63 65 20 75 73 65 64 20 62 79 20 74 68 65 20  nce used by the 
1f584 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20 61  comparison. If a
1f585 6e 20 69 6e 64 65 78 20 69 73 20 74 6f 20 0a 20  n index is to . 
1f586 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65 64 20       ** be used 
1f587 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 65  in place of a te
1f588 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20 6d 75 73  mp-table, it mus
1f589 74 20 62 65 20 6f 72 64 65 72 65 64 20 61 63 63  t be ordered acc
1f58a 6f 72 64 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  ording.      ** 
1f58b 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f  to this collatio
1f58c 6e 20 73 65 71 75 65 6e 63 65 2e 20 20 2a 2f 0a  n sequence.  */.
1f58d 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
1f58e 52 65 71 20 3d 20 73 71 6c 69 74 65 33 42 69 6e  Req = sqlite3Bin
1f58f 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
1f590 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c  q(pParse, pX->pL
1f591 65 66 74 2c 20 70 45 78 70 72 29 3b 0a 0a 20 20  eft, pExpr);..  
1f592 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61      /* Check tha
1f593 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 74  t the affinity t
1f594 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64  hat will be used
1f595 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 20   to perform the 
1f596 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72  .      ** compar
1f597 69 73 6f 6e 20 69 73 20 74 68 65 20 73 61 6d 65  ison is the same
1f598 20 61 73 20 74 68 65 20 61 66 66 69 6e 69 74 79   as the affinity
1f599 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20   of the column. 
1f59a 49 66 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69  If.      ** it i
1f59b 73 20 6e 6f 74 2c 20 69 74 20 69 73 20 6e 6f 74  s not, it is not
1f59c 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65   possible to use
1f59d 20 61 6e 79 20 69 6e 64 65 78 2e 0a 20 20 20 20   any index..    
1f59e 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20    */.      char 
1f59f 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e  aff = comparison
1f5a0 41 66 66 69 6e 69 74 79 28 70 58 29 3b 0a 20 20  Affinity(pX);.  
1f5a1 20 20 20 20 69 6e 74 20 61 66 66 69 6e 69 74 79      int affinity
1f5a2 5f 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f  _ok = (pTab->aCo
1f5a3 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79  l[iCol].affinity
1f5a4 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49  ==aff||aff==SQLI
1f5a5 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20  TE_AFF_NONE);.. 
1f5a6 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54       for(pIdx=pT
1f5a7 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
1f5a8 20 26 26 20 65 54 79 70 65 3d 3d 30 20 26 26 20   && eType==0 && 
1f5a9 61 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64  affinity_ok; pId
1f5aa 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
1f5ab 20 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64          if( (pId
1f5ac 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d  x->aiColumn[0]==
1f5ad 69 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 26  iCol).         &
1f5ae 26 20 28 70 52 65 71 3d 3d 73 71 6c 69 74 65 33  & (pReq==sqlite3
1f5af 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
1f5b0 45 4e 43 28 64 62 29 2c 20 70 49 64 78 2d 3e 61  ENC(db), pIdx->a
1f5b1 7a 43 6f 6c 6c 5b 30 5d 2c 20 2d 31 2c 20 30 29  zColl[0], -1, 0)
1f5b2 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 21  ).         && (!
1f5b3 6d 75 73 74 42 65 55 6e 69 71 75 65 20 7c 7c 20  mustBeUnique || 
1f5b4 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d  (pIdx->nColumn==
1f5b5 31 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72  1 && pIdx->onErr
1f5b6 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20  or!=OE_None)).  
1f5b7 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
1f5b8 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b     int iMem = ++
1f5b9 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
1f5ba 20 20 20 20 20 20 20 20 69 6e 74 20 69 41 64 64          int iAdd
1f5bb 72 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  r;.          cha
1f5bc 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 20 20 20  r *pKey;.  .    
1f5bd 20 20 20 20 20 20 70 4b 65 79 20 3d 20 28 63 68        pKey = (ch
1f5be 61 72 20 2a 29 73 71 6c 69 74 65 33 49 6e 64 65  ar *)sqlite3Inde
1f5bf 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
1f5c0 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20   pIdx);.        
1f5c1 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
1f5c2 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
1f5c3 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 29 3b   pIdx->pSchema);
1f5c4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1f5c5 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
1f5c6 76 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20  v, iDb);..      
1f5c7 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69      iAddr = sqli
1f5c8 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
1f5c9 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20   OP_If, iMem);. 
1f5ca 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1f5cb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1f5cc 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65  _Integer, 1, iMe
1f5cd 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20  m);.  .         
1f5ce 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f5cf 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  p4(v, OP_OpenRea
1f5d0 64 2c 20 69 54 61 62 2c 20 70 49 64 78 2d 3e 74  d, iTab, pIdx->t
1f5d1 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20  num, iDb,.      
1f5d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5d3 20 20 20 20 20 20 20 20 20 70 4b 65 79 2c 50 34           pKey,P4
1f5d4 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
1f5d5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64 62  );.          Vdb
1f5d6 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73  eComment((v, "%s
1f5d7 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29  ", pIdx->zName))
1f5d8 3b 0a 20 20 20 20 20 20 20 20 20 20 65 54 79 70  ;.          eTyp
1f5d9 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44  e = IN_INDEX_IND
1f5da 45 58 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 73  EX;..          s
1f5db 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
1f5dc 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20  re(v, iAddr);.  
1f5dd 20 20 20 20 20 20 20 20 69 66 28 20 70 72 4e 6f          if( prNo
1f5de 74 46 6f 75 6e 64 20 26 26 20 21 70 54 61 62 2d  tFound && !pTab-
1f5df 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e  >aCol[iCol].notN
1f5e0 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ull ){.         
1f5e1 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d     *prNotFound =
1f5e2 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
1f5e3 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1f5e4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1f5e5 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
1f5e6 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20  eType==0 ){.    
1f5e7 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c  int rMayHaveNull
1f5e8 20 3d 20 30 3b 0a 20 20 20 20 65 54 79 70 65 20   = 0;.    eType 
1f5e9 3d 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 3b 0a  = IN_INDEX_EPH;.
1f5ea 20 20 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75      if( prNotFou
1f5eb 6e 64 20 29 7b 0a 20 20 20 20 20 20 2a 70 72 4e  nd ){.      *prN
1f5ec 6f 74 46 6f 75 6e 64 20 3d 20 72 4d 61 79 48 61  otFound = rMayHa
1f5ed 76 65 4e 75 6c 6c 20 3d 20 2b 2b 70 50 61 72 73  veNull = ++pPars
1f5ee 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c  e->nMem;.    }el
1f5ef 73 65 20 69 66 28 20 70 58 2d 3e 70 4c 65 66 74  se if( pX->pLeft
1f5f0 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21  ->iColumn<0 && !
1f5f1 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
1f5f2 74 79 28 70 58 2c 20 45 50 5f 78 49 73 53 65 6c  ty(pX, EP_xIsSel
1f5f3 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 65 54  ect) ){.      eT
1f5f4 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52  ype = IN_INDEX_R
1f5f5 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OWID;.    }.    
1f5f6 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65  sqlite3CodeSubse
1f5f7 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c  lect(pParse, pX,
1f5f8 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65   rMayHaveNull, e
1f5f9 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52  Type==IN_INDEX_R
1f5fa 4f 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  OWID);.  }else{.
1f5fb 20 20 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d      pX->iTable =
1f5fc 20 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74   iTab;.  }.  ret
1f5fd 75 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e  urn eType;.}.#en
1f5fe 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  dif../*.** Gener
1f5ff 61 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61  ate code for sca
1f600 6c 61 72 20 73 75 62 71 75 65 72 69 65 73 20 75  lar subqueries u
1f601 73 65 64 20 61 73 20 61 6e 20 65 78 70 72 65 73  sed as an expres
1f602 73 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f  sion.** and IN o
1f603 70 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70  perators.  Examp
1f604 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  les:.**.**     (
1f605 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29  SELECT a FROM b)
1f606 20 20 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62            -- sub
1f607 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49  query.**     EXI
1f608 53 54 53 20 28 53 45 4c 45 43 54 20 61 20 46 52  STS (SELECT a FR
1f609 4f 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54  OM b)   -- EXIST
1f60a 53 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20  S subquery.**   
1f60b 20 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20    x IN (4,5,11) 
1f60c 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
1f60d 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68  IN operator with
1f60e 20 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68   list on right-h
1f60f 61 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20  and side.**     
1f610 78 20 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46  x IN (SELECT a F
1f611 52 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e  ROM b)     -- IN
1f612 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 73   operator with s
1f613 75 62 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72  ubquery on the r
1f614 69 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ight.**.** The p
1f615 45 78 70 72 20 70 61 72 61 6d 65 74 65 72 20 64  Expr parameter d
1f616 65 73 63 72 69 62 65 73 20 74 68 65 20 65 78 70  escribes the exp
1f617 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e  ression that con
1f618 74 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20  tains the IN.** 
1f619 6f 70 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71  operator or subq
1f61a 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  uery..**.** If p
1f61b 61 72 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64  arameter isRowid
1f61c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
1f61d 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  en expression pE
1f61e 78 70 72 20 69 73 20 67 75 61 72 61 6e 74 65 65  xpr is guarantee
1f61f 64 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68  d.** to be of th
1f620 65 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20  e form "<rowid> 
1f621 49 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77  IN (?, ?, ?)", w
1f622 68 65 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20  here <rowid> is 
1f623 61 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74  a reference.** t
1f624 6f 20 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b  o some integer k
1f625 65 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74  ey column of a t
1f626 61 62 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20  able B-Tree. In 
1f627 74 68 69 73 20 63 61 73 65 2c 20 75 73 65 20 61  this case, use a
1f628 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72  n.** intkey B-Tr
1f629 65 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ee to store the 
1f62a 73 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76  set of IN(...) v
1f62b 61 6c 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66  alues instead of
1f62c 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73   the usual.** (s
1f62d 6c 6f 77 65 72 29 20 76 61 72 69 61 62 6c 65 20  lower) variable 
1f62e 6c 65 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72  length keys B-Tr
1f62f 65 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ee..*/.#ifndef S
1f630 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1f631 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ERY.SQLITE_PRIVA
1f632 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
1f633 6f 64 65 53 75 62 73 65 6c 65 63 74 28 0a 20 20  odeSubselect(.  
1f634 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a  Parse *pParse, .
1f635 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 0a    Expr *pExpr, .
1f636 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75    int rMayHaveNu
1f637 6c 6c 2c 0a 20 20 69 6e 74 20 69 73 52 6f 77 69  ll,.  int isRowi
1f638 64 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41  d.){.  int testA
1f639 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ddr = 0;        
1f63a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f63b 2a 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20  * One-time test 
1f63c 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64 62  address */.  Vdb
1f63d 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
1f63e 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
1f63f 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
1f640 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  rn;.  sqlite3Exp
1f641 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73  rCachePush(pPars
1f642 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63  e);..  /* This c
1f643 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e 20  ode must be run 
1f644 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79 20  in its entirety 
1f645 65 76 65 72 79 20 74 69 6d 65 20 69 74 20 69 73  every time it is
1f646 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a   encountered.  *
1f647 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20  * if any of the 
1f648 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75  following is tru
1f649 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  e:.  **.  **    
1f64a 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e  *  The right-han
1f64b 64 20 73 69 64 65 20 69 73 20 61 20 63 6f 72 72  d side is a corr
1f64c 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 0a  elated subquery.
1f64d 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72    **    *  The r
1f64e 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69  ight-hand side i
1f64f 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s an expression 
1f650 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20  list containing 
1f651 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20  variables.  **  
1f652 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69    *  We are insi
1f653 64 65 20 61 20 74 72 69 67 67 65 72 0a 20 20 2a  de a trigger.  *
1f654 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66  *.  ** If all of
1f655 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 66   the above are f
1f656 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61  alse, then we ca
1f657 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20  n run this code 
1f658 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73  just once.  ** s
1f659 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 2c  ave the results,
1f65a 20 61 6e 64 20 72 65 75 73 65 20 74 68 65 20 73   and reuse the s
1f65b 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 75  ame result on su
1f65c 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74  bsequent invocat
1f65d 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ions..  */.  if(
1f65e 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   !ExprHasAnyProp
1f65f 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56  erty(pExpr, EP_V
1f660 61 72 53 65 6c 65 63 74 29 20 26 26 20 21 70 50  arSelect) && !pP
1f661 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20  arse->trigStack 
1f662 29 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d  ){.    int mem =
1f663 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
1f664 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1f665 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c  AddOp1(v, OP_If,
1f666 20 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 41   mem);.    testA
1f667 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1f668 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
1f669 74 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b 0a  teger, 1, mem);.
1f66a 20 20 20 20 61 73 73 65 72 74 28 20 74 65 73 74      assert( test
1f66b 41 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 65  Addr>0 || pParse
1f66c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1f66d 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  ed );.  }..  swi
1f66e 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
1f66f 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  {.    case TK_IN
1f670 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 61  : {.      char a
1f671 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 4b  ffinity;.      K
1f672 65 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 0a  eyInfo keyInfo;.
1f673 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20        int addr; 
1f674 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
1f675 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65  s of OP_OpenEphe
1f676 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f  meral instructio
1f677 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20  n */.      Expr 
1f678 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e  *pLeft = pExpr->
1f679 70 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 66  pLeft;..      if
1f67a 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29  ( rMayHaveNull )
1f67b 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1f67c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1f67d 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 48  P_Null, 0, rMayH
1f67e 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  aveNull);.      
1f67f 7d 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74  }..      affinit
1f680 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  y = sqlite3ExprA
1f681 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b 0a  ffinity(pLeft);.
1f682 0a 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68 65  .      /* Whethe
1f683 72 20 74 68 69 73 20 69 73 20 61 6e 20 27 78 20  r this is an 'x 
1f684 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 6f  IN(SELECT...)' o
1f685 72 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 72  r an 'x IN(<expr
1f686 6c 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a 2a  list>)'.      **
1f687 20 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20 69   expression it i
1f688 73 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73 61  s handled the sa
1f689 6d 65 20 77 61 79 2e 20 41 20 76 69 72 74 75 61  me way. A virtua
1f68a 6c 20 74 61 62 6c 65 20 69 73 20 0a 20 20 20 20  l table is .    
1f68b 20 20 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68    ** filled with
1f68c 20 73 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69 6e   single-field in
1f68d 64 65 78 20 6b 65 79 73 20 72 65 70 72 65 73 65  dex keys represe
1f68e 6e 74 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74  nting the result
1f68f 73 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20  s.      ** from 
1f690 74 68 65 20 53 45 4c 45 43 54 20 6f 72 20 74 68  the SELECT or th
1f691 65 20 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20 20  e <exprlist>..  
1f692 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
1f693 49 66 20 74 68 65 20 27 78 27 20 65 78 70 72 65  If the 'x' expre
1f694 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d  ssion is a colum
1f695 6e 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 20  n value, or the 
1f696 53 45 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20 20  SELECT....      
1f697 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74  ** statement ret
1f698 75 72 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61  urns a column va
1f699 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 61 66  lue, then the af
1f69a 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 0a 20  finity of that. 
1f69b 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69       ** column i
1f69c 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20  s used to build 
1f69d 74 68 65 20 69 6e 64 65 78 20 6b 65 79 73 2e 20  the index keys. 
1f69e 49 66 20 62 6f 74 68 20 27 78 27 20 61 6e 64 20  If both 'x' and 
1f69f 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 53 45 4c  the.      ** SEL
1f6a0 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74  ECT... statement
1f6a1 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68   are columns, th
1f6a2 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e  en numeric affin
1f6a3 69 74 79 20 69 73 20 75 73 65 64 0a 20 20 20 20  ity is used.    
1f6a4 20 20 2a 2a 20 69 66 20 65 69 74 68 65 72 20 63    ** if either c
1f6a5 6f 6c 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52 49  olumn has NUMERI
1f6a6 43 20 6f 72 20 49 4e 54 45 47 45 52 20 61 66 66  C or INTEGER aff
1f6a7 69 6e 69 74 79 2e 20 49 66 20 6e 65 69 74 68 65  inity. If neithe
1f6a8 72 0a 20 20 20 20 20 20 2a 2a 20 27 78 27 20 6e  r.      ** 'x' n
1f6a9 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e  or the SELECT...
1f6aa 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63   statement are c
1f6ab 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d  olumns, then num
1f6ac 65 72 69 63 20 61 66 66 69 6e 69 74 79 0a 20 20  eric affinity.  
1f6ad 20 20 20 20 2a 2a 20 69 73 20 75 73 65 64 2e 0a      ** is used..
1f6ae 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70        */.      p
1f6af 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70  Expr->iTable = p
1f6b0 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
1f6b1 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
1f6b2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1f6b3 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
1f6b4 6c 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  l, pExpr->iTable
1f6b5 2c 20 21 69 73 52 6f 77 69 64 29 3b 0a 20 20 20  , !isRowid);.   
1f6b6 20 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e     memset(&keyIn
1f6b7 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65  fo, 0, sizeof(ke
1f6b8 79 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b  yInfo));.      k
1f6b9 65 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20  eyInfo.nField = 
1f6ba 31 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 45 78  1;..      if( Ex
1f6bb 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
1f6bc 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
1f6bd 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  t) ){.        /*
1f6be 20 43 61 73 65 20 31 3a 20 20 20 20 20 65 78 70   Case 1:     exp
1f6bf 72 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e  r IN (SELECT ...
1f6c0 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ).        **.   
1f6c1 20 20 20 20 20 2a 2a 20 47 65 6e 65 72 61 74 65       ** Generate
1f6c2 20 63 6f 64 65 20 74 6f 20 77 72 69 74 65 20 74   code to write t
1f6c3 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  he results of th
1f6c4 65 20 73 65 6c 65 63 74 20 69 6e 74 6f 20 74 68  e select into th
1f6c5 65 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20  e temporary.    
1f6c6 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 61 6c 6c      ** table all
1f6c7 6f 63 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65  ocated and opene
1f6c8 64 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20  d above..       
1f6c9 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65   */.        Sele
1f6ca 63 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20  ctDest dest;.   
1f6cb 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
1f6cc 45 4c 69 73 74 3b 0a 0a 20 20 20 20 20 20 20 20  EList;..        
1f6cd 61 73 73 65 72 74 28 20 21 69 73 52 6f 77 69 64  assert( !isRowid
1f6ce 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
1f6cf 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
1f6d0 74 28 26 64 65 73 74 2c 20 53 52 54 5f 53 65 74  t(&dest, SRT_Set
1f6d1 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29  , pExpr->iTable)
1f6d2 3b 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 61  ;.        dest.a
1f6d3 66 66 69 6e 69 74 79 20 3d 20 28 75 38 29 61 66  ffinity = (u8)af
1f6d4 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20  finity;.        
1f6d5 61 73 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e  assert( (pExpr->
1f6d6 69 54 61 62 6c 65 26 30 78 30 30 30 30 46 46 46  iTable&0x0000FFF
1f6d7 46 29 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c  F)==pExpr->iTabl
1f6d8 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  e );.        if(
1f6d9 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
1f6da 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 78 2e  Parse, pExpr->x.
1f6db 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29 20  pSelect, &dest) 
1f6dc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
1f6dd 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  urn;.        }. 
1f6de 20 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20         pEList = 
1f6df 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
1f6e0 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 20  ->pEList;.      
1f6e1 20 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20    if( pEList && 
1f6e2 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20  pEList->nExpr>0 
1f6e3 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 6b 65  ){ .          ke
1f6e4 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d  yInfo.aColl[0] =
1f6e5 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f   sqlite3BinaryCo
1f6e6 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61  mpareCollSeq(pPa
1f6e7 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1f6e8 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
1f6e9 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
1f6ea 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  xpr);.        }.
1f6eb 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1f6ec 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 29  pExpr->x.pList )
1f6ed 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73  {.        /* Cas
1f6ee 65 20 32 3a 20 20 20 20 20 65 78 70 72 20 49 4e  e 2:     expr IN
1f6ef 20 28 65 78 70 72 6c 69 73 74 29 0a 20 20 20 20   (exprlist).    
1f6f0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
1f6f1 2a 20 46 6f 72 20 65 61 63 68 20 65 78 70 72 65  * For each expre
1f6f2 73 73 69 6f 6e 2c 20 62 75 69 6c 64 20 61 6e 20  ssion, build an 
1f6f3 69 6e 64 65 78 20 6b 65 79 20 66 72 6f 6d 20 74  index key from t
1f6f4 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 61 6e  he evaluation an
1f6f5 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f  d.        ** sto
1f6f6 72 65 20 69 74 20 69 6e 20 74 68 65 20 74 65 6d  re it in the tem
1f6f7 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 49 66  porary table. If
1f6f8 20 3c 65 78 70 72 3e 20 69 73 20 61 20 63 6f 6c   <expr> is a col
1f6f9 75 6d 6e 2c 20 74 68 65 6e 20 75 73 65 0a 20 20  umn, then use.  
1f6fa 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 63 6f        ** that co
1f6fb 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 20 77  lumns affinity w
1f6fc 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 69 6e 64  hen building ind
1f6fd 65 78 20 6b 65 79 73 2e 20 49 66 20 3c 65 78 70  ex keys. If <exp
1f6fe 72 3e 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20  r> is not.      
1f6ff 20 20 2a 2a 20 61 20 63 6f 6c 75 6d 6e 2c 20 75    ** a column, u
1f700 73 65 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e  se numeric affin
1f701 69 74 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ity..        */.
1f702 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20          int i;. 
1f703 20 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20         ExprList 
1f704 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  *pList = pExpr->
1f705 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20  x.pList;.       
1f706 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
1f707 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
1f708 20 20 20 20 20 20 69 6e 74 20 72 31 2c 20 72 32        int r1, r2
1f709 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20 20 20 69  , r3;..        i
1f70a 66 28 20 21 61 66 66 69 6e 69 74 79 20 29 7b 0a  f( !affinity ){.
1f70b 20 20 20 20 20 20 20 20 20 20 61 66 66 69 6e 69            affini
1f70c 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ty = SQLITE_AFF_
1f70d 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  NONE;.        }.
1f70e 20 20 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e          keyInfo.
1f70f 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74  aColl[0] = sqlit
1f710 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50  e3ExprCollSeq(pP
1f711 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
1f712 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  ft);..        /*
1f713 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65 61   Loop through ea
1f714 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ch expression in
1f715 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20 2a 2f 0a   <exprlist>. */.
1f716 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c          r1 = sql
1f717 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
1f718 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
1f719 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r2 = sqlite3GetT
1f71a 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
1f71b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1f71c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1f71d 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b 0a 20 20  Null, 0, r2);.  
1f71e 20 20 20 20 20 20 66 6f 72 28 69 3d 70 4c 69 73        for(i=pLis
1f71f 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d  t->nExpr, pItem=
1f720 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 3b 20 69  pList->a; i>0; i
1f721 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
1f722 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45          Expr *pE
1f723 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  2 = pItem->pExpr
1f724 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;..          /* 
1f725 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
1f726 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e  n is not constan
1f727 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e  t then we will n
1f728 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20  eed to.         
1f729 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 65 20   ** disable the 
1f72a 74 65 73 74 20 74 68 61 74 20 77 61 73 20 67 65  test that was ge
1f72b 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20 74 68  nerated above th
1f72c 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a 20 20  at makes sure.  
1f72d 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20          ** this 
1f72e 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63 75 74  code only execut
1f72f 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61 75 73  es once.  Becaus
1f730 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f 6e 73  e for a non-cons
1f731 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a  tant.          *
1f732 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77 65 20  * expression we 
1f733 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20 74 68  need to rerun th
1f734 69 73 20 63 6f 64 65 20 65 61 63 68 20 74 69 6d  is code each tim
1f735 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
1f736 20 20 20 20 20 20 20 20 20 20 69 66 28 20 74 65            if( te
1f737 73 74 41 64 64 72 20 26 26 20 21 73 71 6c 69 74  stAddr && !sqlit
1f738 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
1f739 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20 20 20  (pE2) ){.       
1f73a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f73b 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20  ChangeToNoop(v, 
1f73c 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29 3b 0a  testAddr-1, 2);.
1f73d 20 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74              test
1f73e 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  Addr = 0;.      
1f73f 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
1f740 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65   /* Evaluate the
1f741 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20   expression and 
1f742 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f 20 74  insert it into t
1f743 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f  he temp table */
1f744 0a 20 20 20 20 20 20 20 20 20 20 72 33 20 3d 20  .          r3 = 
1f745 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1f746 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45  arget(pParse, pE
1f747 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  2, r1);.        
1f748 20 20 69 66 28 20 69 73 52 6f 77 69 64 20 29 7b    if( isRowid ){
1f749 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1f74a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1f74b 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
1f74c 72 33 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43  r3, sqlite3VdbeC
1f74d 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32 29  urrentAddr(v)+2)
1f74e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
1f74f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1f750 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 70 45  v, OP_Insert, pE
1f751 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 2c  xpr->iTable, r2,
1f752 20 72 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20   r3);.          
1f753 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1f754 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1f755 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp4(v, OP_MakeR
1f756 65 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20 72 32  ecord, r3, 1, r2
1f757 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b  , &affinity, 1);
1f758 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1f759 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
1f75a 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
1f75b 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 20 20  se, r3, 1);.    
1f75c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1f75d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1f75e 49 64 78 49 6e 73 65 72 74 2c 20 70 45 78 70 72  IdxInsert, pExpr
1f75f 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b 0a 20  ->iTable, r2);. 
1f760 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1f761 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
1f762 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
1f763 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
1f764 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
1f765 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
1f766 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20  arse, r2);.     
1f767 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 69 73   }.      if( !is
1f768 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Rowid ){.       
1f769 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1f76a 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28 76  geP4(v, addr, (v
1f76b 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20  oid *)&keyInfo, 
1f76c 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20  P4_KEYINFO);.   
1f76d 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
1f76e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73  ;.    }..    cas
1f76f 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20  e TK_EXISTS:.   
1f770 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a   case TK_SELECT:
1f771 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73   {.      /* This
1f772 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 63 61   has to be a sca
1f773 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 65 6e  lar SELECT.  Gen
1f774 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 70 75  erate code to pu
1f775 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 76  t the.      ** v
1f776 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 65 6c  alue of this sel
1f777 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20  ect in a memory 
1f778 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64 20  cell and record 
1f779 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 20  the number.     
1f77a 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72   ** of the memor
1f77b 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d  y cell in iColum
1f77c 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n..      */.    
1f77d 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 54    static const T
1f77e 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 28 75 38  oken one = { (u8
1f77f 2a 29 22 31 22 2c 20 30 2c 20 30 2c 20 31 20 7d  *)"1", 0, 0, 1 }
1f780 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  ;.      Select *
1f781 70 53 65 6c 3b 0a 20 20 20 20 20 20 53 65 6c 65  pSel;.      Sele
1f782 63 74 44 65 73 74 20 64 65 73 74 3b 0a 0a 20 20  ctDest dest;..  
1f783 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72      assert( Expr
1f784 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
1f785 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
1f786 20 29 3b 0a 20 20 20 20 20 20 70 53 65 6c 20 3d   );.      pSel =
1f787 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63   pExpr->x.pSelec
1f788 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
1f789 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
1f78a 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61 72 73  dest, 0, ++pPars
1f78b 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20  e->nMem);.      
1f78c 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  if( pExpr->op==T
1f78d 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20  K_SELECT ){.    
1f78e 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d      dest.eDest =
1f78f 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 20 20   SRT_Mem;.      
1f790 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f791 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
1f792 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a  0, dest.iParm);.
1f793 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d          VdbeComm
1f794 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 73 75  ent((v, "Init su
1f795 62 71 75 65 72 79 20 72 65 73 75 6c 74 22 29 29  bquery result"))
1f796 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1f797 20 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73         dest.eDes
1f798 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73 3b 0a  t = SRT_Exists;.
1f799 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1f79a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1f79b 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65 73 74  Integer, 0, dest
1f79c 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20  .iParm);.       
1f79d 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
1f79e 20 22 49 6e 69 74 20 45 58 49 53 54 53 20 72 65   "Init EXISTS re
1f79f 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d  sult"));.      }
1f7a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
1f7a1 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  prDelete(pParse-
1f7a2 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69 6d 69  >db, pSel->pLimi
1f7a3 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c 2d 3e  t);.      pSel->
1f7a4 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33  pLimit = sqlite3
1f7a5 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
1f7a6 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 2c 20  _INTEGER, 0, 0, 
1f7a7 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69 66 28  &one);.      if(
1f7a8 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
1f7a9 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26 64 65  Parse, pSel, &de
1f7aa 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  st) ){.        r
1f7ab 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
1f7ac 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
1f7ad 75 6d 6e 20 3d 20 64 65 73 74 2e 69 50 61 72 6d  umn = dest.iParm
1f7ae 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1f7af 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
1f7b0 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20 20 20  testAddr ){.    
1f7b1 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1f7b2 65 72 65 28 76 2c 20 74 65 73 74 41 64 64 72 2d  ere(v, testAddr-
1f7b3 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  1);.  }.  sqlite
1f7b4 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50  3ExprCachePop(pP
1f7b5 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20 72 65 74  arse, 1);..  ret
1f7b6 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  urn;.}.#endif /*
1f7b7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
1f7b8 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  QUERY */../*.** 
1f7b9 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62  Duplicate an 8-b
1f7ba 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61  yte value.*/.sta
1f7bb 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62 79  tic char *dup8by
1f7bc 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e  tes(Vdbe *v, con
1f7bd 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20  st char *in){.  
1f7be 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69  char *out = sqli
1f7bf 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73  te3DbMallocRaw(s
1f7c0 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c  qlite3VdbeDb(v),
1f7c1 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29   8);.  if( out )
1f7c2 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74  {.    memcpy(out
1f7c3 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20  , in, 8);.  }.  
1f7c4 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f  return out;.}../
1f7c5 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  *.** Generate an
1f7c6 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
1f7c7 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66  t will put the f
1f7c8 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a  loating point.**
1f7c9 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65 64   value described
1f7ca 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e   by z[0..n-1] in
1f7cb 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d  to register iMem
1f7cc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20  ..**.** The z[] 
1f7cd 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62  string will prob
1f7ce 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f  ably not be zero
1f7cf 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75  -terminated.  Bu
1f7d0 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63  t the .** z[n] c
1f7d1 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72  haracter is guar
1f7d2 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d  anteed to be som
1f7d3 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73  ething that does
1f7d4 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b   not look.** lik
1f7d5 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69  e the continuati
1f7d6 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  on of the number
1f7d7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1f7d8 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a   codeReal(Vdbe *
1f7d9 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  v, const char *z
1f7da 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6e 65 67  , int n, int neg
1f7db 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65  ateFlag, int iMe
1f7dc 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 20  m){.  assert( z 
1f7dd 7c 7c 20 76 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  || v==0 || sqlit
1f7de 65 33 56 64 62 65 44 62 28 76 29 2d 3e 6d 61 6c  e3VdbeDb(v)->mal
1f7df 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61  locFailed );.  a
1f7e0 73 73 65 72 74 28 20 21 7a 20 7c 7c 20 21 73 71  ssert( !z || !sq
1f7e1 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 6e  lite3Isdigit(z[n
1f7e2 5d 29 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ]) );.  UNUSED_P
1f7e3 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20 69  ARAMETER(n);.  i
1f7e4 66 28 20 7a 20 29 7b 0a 20 20 20 20 64 6f 75 62  f( z ){.    doub
1f7e5 6c 65 20 76 61 6c 75 65 3b 0a 20 20 20 20 63 68  le value;.    ch
1f7e6 61 72 20 2a 7a 56 3b 0a 20 20 20 20 73 71 6c 69  ar *zV;.    sqli
1f7e7 74 65 33 41 74 6f 46 28 7a 2c 20 26 76 61 6c 75  te3AtoF(z, &valu
1f7e8 65 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  e);.    if( sqli
1f7e9 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 65 29 20  te3IsNaN(value) 
1f7ea 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1f7eb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1f7ec 5f 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 29 3b  _Null, 0, iMem);
1f7ed 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1f7ee 20 20 69 66 28 20 6e 65 67 61 74 65 46 6c 61 67    if( negateFlag
1f7ef 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c 75   ) value = -valu
1f7f0 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64 75  e;.      zV = du
1f7f1 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 72  p8bytes(v, (char
1f7f2 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20  *)&value);.     
1f7f3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f7f4 70 34 28 76 2c 20 4f 50 5f 52 65 61 6c 2c 20 30  p4(v, OP_Real, 0
1f7f5 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50  , iMem, 0, zV, P
1f7f6 34 5f 52 45 41 4c 29 3b 0a 20 20 20 20 7d 0a 20  4_REAL);.    }. 
1f7f7 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   }.}.../*.** Gen
1f7f8 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63  erate an instruc
1f7f9 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70  tion that will p
1f7fa 75 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 64  ut the integer d
1f7fb 65 73 63 72 69 62 65 20 62 79 0a 2a 2a 20 74 65  escribe by.** te
1f7fc 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74  xt z[0..n-1] int
1f7fd 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e  o register iMem.
1f7fe 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73  .**.** The z[] s
1f7ff 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61  tring will proba
1f800 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d  bly not be zero-
1f801 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74  terminated.  But
1f802 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68   the .** z[n] ch
1f803 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72 61  aracter is guara
1f804 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65  nteed to be some
1f805 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20  thing that does 
1f806 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65  not look.** like
1f807 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f   the continuatio
1f808 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e  n of the number.
1f809 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1f80a 63 6f 64 65 49 6e 74 65 67 65 72 28 56 64 62 65  codeInteger(Vdbe
1f80b 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 78 70 72   *v, Expr *pExpr
1f80c 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 2c 20 69  , int negFlag, i
1f80d 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 63 6f 6e 73  nt iMem){.  cons
1f80e 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28  t char *z;.  if(
1f80f 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20   pExpr->flags & 
1f810 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20  EP_IntValue ){. 
1f811 20 20 20 69 6e 74 20 69 20 3d 20 70 45 78 70 72     int i = pExpr
1f812 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 69 66  ->iTable;.    if
1f813 28 20 6e 65 67 46 6c 61 67 20 29 20 69 20 3d 20  ( negFlag ) i = 
1f814 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  -i;.    sqlite3V
1f815 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1f816 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d 65 6d  Integer, i, iMem
1f817 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28  );.  }else if( (
1f818 7a 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72  z = (char*)pExpr
1f819 2d 3e 74 6f 6b 65 6e 2e 7a 29 21 3d 30 20 29 7b  ->token.z)!=0 ){
1f81a 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
1f81b 69 6e 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 74  int n = pExpr->t
1f81c 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 61 73 73 65  oken.n;.    asse
1f81d 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 64 69  rt( !sqlite3Isdi
1f81e 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 20 20  git(z[n]) );.   
1f81f 20 69 66 28 20 73 71 6c 69 74 65 33 47 65 74 49   if( sqlite3GetI
1f820 6e 74 33 32 28 7a 2c 20 26 69 29 20 29 7b 0a 20  nt32(z, &i) ){. 
1f821 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 67       if( negFlag
1f822 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20 20 20   ) i = -i;.     
1f823 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f824 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
1f825 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20  , i, iMem);.    
1f826 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65  }else if( sqlite
1f827 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 7a 2c  3FitsIn64Bits(z,
1f828 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20 20 20   negFlag) ){.   
1f829 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20     i64 value;.  
1f82a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20      char *zV;.  
1f82b 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 69 36      sqlite3Atoi6
1f82c 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20  4(z, &value);.  
1f82d 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 67 20      if( negFlag 
1f82e 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c 75 65  ) value = -value
1f82f 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64 75 70  ;.      zV = dup
1f830 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 72 2a  8bytes(v, (char*
1f831 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20 20  )&value);.      
1f832 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f833 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30  4(v, OP_Int64, 0
1f834 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50  , iMem, 0, zV, P
1f835 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20 7d 65  4_INT64);.    }e
1f836 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64 65 52  lse{.      codeR
1f837 65 61 6c 28 76 2c 20 7a 2c 20 6e 2c 20 6e 65 67  eal(v, z, n, neg
1f838 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20  Flag, iMem);.   
1f839 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
1f83a 43 6c 65 61 72 20 61 20 63 61 63 68 65 20 65 6e  Clear a cache en
1f83b 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  try..*/.static v
1f83c 6f 69 64 20 63 61 63 68 65 45 6e 74 72 79 43 6c  oid cacheEntryCl
1f83d 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  ear(Parse *pPars
1f83e 65 2c 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61  e, struct yColCa
1f83f 63 68 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  che *p){.  if( p
1f840 2d 3e 74 65 6d 70 52 65 67 20 29 7b 0a 20 20 20  ->tempReg ){.   
1f841 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65   if( pParse->nTe
1f842 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28  mpReg<ArraySize(
1f843 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67  pParse->aTempReg
1f844 29 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  ) ){.      pPars
1f845 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 72  e->aTempReg[pPar
1f846 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d 20  se->nTempReg++] 
1f847 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d  = p->iReg;.    }
1f848 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20  .    p->tempReg 
1f849 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a  = 0;.  }.}.../*.
1f84a 2a 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65  ** Record in the
1f84b 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 74 68   column cache th
1f84c 61 74 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  at a particular 
1f84d 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a 2a 2a  column from a.**
1f84e 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c   particular tabl
1f84f 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  e is stored in a
1f850 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 69   particular regi
1f851 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ster..*/.SQLITE_
1f852 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1f853 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f  ite3ExprCacheSto
1f854 72 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  re(Parse *pParse
1f855 2c 20 69 6e 74 20 69 54 61 62 2c 20 69 6e 74 20  , int iTab, int 
1f856 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65 67 29 7b  iCol, int iReg){
1f857 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1f858 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e 74 20 69 64  minLru;.  int id
1f859 78 4c 72 75 3b 0a 20 20 73 74 72 75 63 74 20 79  xLru;.  struct y
1f85a 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20  ColCache *p;..  
1f85b 2f 2a 20 46 69 72 73 74 20 72 65 70 6c 61 63 65  /* First replace
1f85c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 65 6e   any existing en
1f85d 74 72 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  try */.  for(i=0
1f85e 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c  , p=pParse->aCol
1f85f 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f  Cache; i<SQLITE_
1f860 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c  N_COLCACHE; i++,
1f861 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   p++){.    if( p
1f862 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e 69 54 61  ->iReg && p->iTa
1f863 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 2d 3e  ble==iTab && p->
1f864 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 20 29 7b  iColumn==iCol ){
1f865 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72  .      cacheEntr
1f866 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70  yClear(pParse, p
1f867 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65 76  );.      p->iLev
1f868 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61  el = pParse->iCa
1f869 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20  cheLevel;.      
1f86a 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a  p->iReg = iReg;.
1f86b 20 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e        p->affChan
1f86c 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  ge = 0;.      p-
1f86d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69  >lru = pParse->i
1f86e 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20  CacheCnt++;.    
1f86f 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
1f870 20 20 7d 0a 20 20 69 66 28 20 69 52 65 67 3c 3d    }.  if( iReg<=
1f871 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f  0 ) return;..  /
1f872 2a 20 46 69 6e 64 20 61 6e 20 65 6d 70 74 79 20  * Find an empty 
1f873 73 6c 6f 74 20 61 6e 64 20 72 65 70 6c 61 63 65  slot and replace
1f874 20 69 74 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30   it */.  for(i=0
1f875 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c  , p=pParse->aCol
1f876 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f  Cache; i<SQLITE_
1f877 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c  N_COLCACHE; i++,
1f878 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   p++){.    if( p
1f879 2d 3e 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20  ->iReg==0 ){.   
1f87a 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70     p->iLevel = p
1f87b 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76  Parse->iCacheLev
1f87c 65 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69 54 61  el;.      p->iTa
1f87d 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 20 20  ble = iTab;.    
1f87e 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69    p->iColumn = i
1f87f 43 6f 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52  Col;.      p->iR
1f880 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 20  eg = iReg;.     
1f881 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20   p->affChange = 
1f882 30 3b 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70  0;.      p->temp
1f883 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  Reg = 0;.      p
1f884 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e  ->lru = pParse->
1f885 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20  iCacheCnt++;.   
1f886 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
1f887 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 70 6c 61  .  }..  /* Repla
1f888 63 65 20 74 68 65 20 6c 61 73 74 20 72 65 63 65  ce the last rece
1f889 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 6d  ntly used */.  m
1f88a 69 6e 4c 72 75 20 3d 20 30 78 37 66 66 66 66 66  inLru = 0x7fffff
1f88b 66 66 3b 0a 20 20 69 64 78 4c 72 75 20 3d 20 2d  ff;.  idxLru = -
1f88c 31 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d  1;.  for(i=0, p=
1f88d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
1f88e 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f  e; i<SQLITE_N_CO
1f88f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b  LCACHE; i++, p++
1f890 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6c 72  ){.    if( p->lr
1f891 75 3c 6d 69 6e 4c 72 75 20 29 7b 0a 20 20 20 20  u<minLru ){.    
1f892 20 20 69 64 78 4c 72 75 20 3d 20 69 3b 0a 20 20    idxLru = i;.  
1f893 20 20 20 20 6d 69 6e 4c 72 75 20 3d 20 70 2d 3e      minLru = p->
1f894 6c 72 75 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  lru;.    }.  }. 
1f895 20 69 66 28 20 69 64 78 4c 72 75 3e 3d 30 20 29   if( idxLru>=0 )
1f896 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72 73  {.    p = &pPars
1f897 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 64 78  e->aColCache[idx
1f898 4c 72 75 5d 3b 0a 20 20 20 20 70 2d 3e 69 4c 65  Lru];.    p->iLe
1f899 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43  vel = pParse->iC
1f89a 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 70  acheLevel;.    p
1f89b 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b  ->iTable = iTab;
1f89c 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20  .    p->iColumn 
1f89d 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e 69  = iCol;.    p->i
1f89e 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20  Reg = iReg;.    
1f89f 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30  p->affChange = 0
1f8a0 3b 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67  ;.    p->tempReg
1f8a1 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 6c 72 75   = 0;.    p->lru
1f8a2 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68   = pParse->iCach
1f8a3 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 72 65 74 75  eCnt++;.    retu
1f8a4 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn;.  }.}../*.**
1f8a5 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20 61   Indicate that a
1f8a6 20 72 65 67 69 73 74 65 72 20 69 73 20 62 65 69   register is bei
1f8a7 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20  ng overwritten. 
1f8a8 20 50 75 72 67 65 20 74 68 65 20 72 65 67 69 73   Purge the regis
1f8a9 74 65 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ter.** from the 
1f8aa 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f  column cache..*/
1f8ab 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f8ac 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1f8ad 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72 73  CacheRemove(Pars
1f8ae 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
1f8af 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  Reg){.  int i;. 
1f8b0 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68   struct yColCach
1f8b1 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  e *p;.  for(i=0,
1f8b2 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   p=pParse->aColC
1f8b3 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e  ache; i<SQLITE_N
1f8b4 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20  _COLCACHE; i++, 
1f8b5 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  p++){.    if( p-
1f8b6 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20  >iReg==iReg ){. 
1f8b7 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43       cacheEntryC
1f8b8 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b  lear(pParse, p);
1f8b9 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d  .      p->iReg =
1f8ba 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   0;.    }.  }.}.
1f8bb 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20  ./*.** Remember 
1f8bc 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6c 75  the current colu
1f8bd 6d 6e 20 63 61 63 68 65 20 63 6f 6e 74 65 78 74  mn cache context
1f8be 2e 20 20 41 6e 79 20 6e 65 77 20 65 6e 74 72 69  .  Any new entri
1f8bf 65 73 20 61 64 64 65 64 0a 2a 2a 20 61 64 64 65  es added.** adde
1f8c0 64 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20  d to the column 
1f8c1 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69 73  cache after this
1f8c2 20 63 61 6c 6c 20 61 72 65 20 72 65 6d 6f 76 65   call are remove
1f8c3 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 63 6f  d when the.** co
1f8c4 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 70 20  rresponding pop 
1f8c5 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  occurs..*/.SQLIT
1f8c6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1f8c7 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
1f8c8 75 73 68 28 50 61 72 73 65 20 2a 70 50 61 72 73  ush(Parse *pPars
1f8c9 65 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43  e){.  pParse->iC
1f8ca 61 63 68 65 4c 65 76 65 6c 2b 2b 3b 0a 7d 0a 0a  acheLevel++;.}..
1f8cb 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 66 72 6f  /*.** Remove fro
1f8cc 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63  m the column cac
1f8cd 68 65 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74  he any entries t
1f8ce 68 61 74 20 77 65 72 65 20 61 64 64 65 64 20 73  hat were added s
1f8cf 69 6e 63 65 20 74 68 65 0a 2a 2a 20 74 68 65 20  ince the.** the 
1f8d0 70 72 65 76 69 6f 75 73 20 4e 20 50 75 73 68 20  previous N Push 
1f8d1 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 49 6e 20  operations.  In 
1f8d2 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 73  other words, res
1f8d3 74 6f 72 65 20 74 68 65 20 63 61 63 68 65 0a 2a  tore the cache.*
1f8d4 2a 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 69  * to the state i
1f8d5 74 20 77 61 73 20 69 6e 20 4e 20 50 75 73 68 65  t was in N Pushe
1f8d6 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45  s ago..*/.SQLITE
1f8d7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1f8d8 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
1f8d9 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  p(Parse *pParse,
1f8da 20 69 6e 74 20 4e 29 7b 0a 20 20 69 6e 74 20 69   int N){.  int i
1f8db 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
1f8dc 61 63 68 65 20 2a 70 3b 0a 20 20 61 73 73 65 72  ache *p;.  asser
1f8dd 74 28 20 4e 3e 30 20 29 3b 0a 20 20 61 73 73 65  t( N>0 );.  asse
1f8de 72 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63  rt( pParse->iCac
1f8df 68 65 4c 65 76 65 6c 3e 3d 4e 20 29 3b 0a 20 20  heLevel>=N );.  
1f8e0 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65  pParse->iCacheLe
1f8e1 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20 66 6f 72 28  vel -= N;.  for(
1f8e2 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
1f8e3 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
1f8e4 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
1f8e5 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
1f8e6 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e  ( p->iReg && p->
1f8e7 69 4c 65 76 65 6c 3e 70 50 61 72 73 65 2d 3e 69  iLevel>pParse->i
1f8e8 43 61 63 68 65 4c 65 76 65 6c 20 29 7b 0a 20 20  CacheLevel ){.  
1f8e9 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c      cacheEntryCl
1f8ea 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a  ear(pParse, p);.
1f8eb 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20        p->iReg = 
1f8ec 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
1f8ed 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 63  /*.** When a cac
1f8ee 68 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65  hed column is re
1f8ef 75 73 65 64 2c 20 6d 61 6b 65 20 73 75 72 65 20  used, make sure 
1f8f0 74 68 61 74 20 69 74 73 20 72 65 67 69 73 74 65  that its registe
1f8f1 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65  r is.** no longe
1f8f2 72 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20 61  r available as a
1f8f3 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 2e 20   temp register. 
1f8f4 20 74 69 63 6b 65 74 20 23 33 38 37 39 3a 20 20   ticket #3879:  
1f8f5 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 72 65 67  that same.** reg
1f8f6 69 73 74 65 72 20 6d 69 67 68 74 20 62 65 20 69  ister might be i
1f8f7 6e 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 6d  n the cache in m
1f8f8 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 73 2c 20  ultiple places, 
1f8f9 73 6f 20 62 65 20 73 75 72 65 20 74 6f 0a 2a 2a  so be sure to.**
1f8fa 20 67 65 74 20 74 68 65 6d 20 61 6c 6c 2e 0a 2a   get them all..*
1f8fb 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
1f8fc 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 69  lite3ExprCachePi
1f8fd 6e 52 65 67 69 73 74 65 72 28 50 61 72 73 65 20  nRegister(Parse 
1f8fe 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65  *pParse, int iRe
1f8ff 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  g){.  int i;.  s
1f900 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
1f901 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  *p;.  for(i=0, p
1f902 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63  =pParse->aColCac
1f903 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43  he; i<SQLITE_N_C
1f904 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b  OLCACHE; i++, p+
1f905 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  +){.    if( p->i
1f906 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20  Reg==iReg ){.   
1f907 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20     p->tempReg = 
1f908 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
1f909 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
1f90a 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 78  ode that will ex
1f90b 74 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75 6d  tract the iColum
1f90c 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d  n-th column from
1f90d 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 61  .** table pTab a
1f90e 6e 64 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c  nd store the col
1f90f 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 61 20 72  umn value in a r
1f910 65 67 69 73 74 65 72 2e 20 20 41 6e 20 65 66 66  egister.  An eff
1f911 6f 72 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74  ort.** is made t
1f912 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75  o store the colu
1f913 6d 6e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  mn value in regi
1f914 73 74 65 72 20 69 52 65 67 2c 20 62 75 74 20 74  ster iReg, but t
1f915 68 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75  his is.** not gu
1f916 61 72 61 6e 74 65 65 64 2e 20 20 54 68 65 20 6c  aranteed.  The l
1f917 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63  ocation of the c
1f918 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20 72  olumn value is r
1f919 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
1f91a 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20  here must be an 
1f91b 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 6f 20 70  open cursor to p
1f91c 54 61 62 20 69 6e 20 69 54 61 62 6c 65 20 77 68  Tab in iTable wh
1f91d 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  en this routine.
1f91e 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 49  ** is called.  I
1f91f 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e  f iColumn<0 then
1f920 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   code is generat
1f921 65 64 20 74 68 61 74 20 65 78 74 72 61 63 74 73  ed that extracts
1f922 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a   the rowid..**.*
1f923 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  * This routine m
1f924 69 67 68 74 20 61 74 74 65 6d 70 74 20 74 6f 20  ight attempt to 
1f925 72 65 75 73 65 20 74 68 65 20 76 61 6c 75 65 20  reuse the value 
1f926 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68  of the column th
1f927 61 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64  at.** has alread
1f928 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e  y been loaded in
1f929 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20  to a register.  
1f92a 54 68 65 20 76 61 6c 75 65 20 77 69 6c 6c 20 61  The value will a
1f92b 6c 77 61 79 73 0a 2a 2a 20 62 65 20 75 73 65 64  lways.** be used
1f92c 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 75   if it has not u
1f92d 6e 64 65 72 67 6f 6e 65 20 61 6e 79 20 61 66 66  ndergone any aff
1f92e 69 6e 69 74 79 20 63 68 61 6e 67 65 73 2e 20 20  inity changes.  
1f92f 42 75 74 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66  But if.** an aff
1f930 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73  inity change has
1f931 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20   occurred, then 
1f932 74 68 65 20 63 61 63 68 65 64 20 76 61 6c 75 65  the cached value
1f933 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a   will only be.**
1f934 20 75 73 65 64 20 69 66 20 61 6c 6c 6f 77 41 66   used if allowAf
1f935 66 43 68 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a  fChng is true..*
1f936 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f937 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
1f938 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20  CodeGetColumn(. 
1f939 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1f93a 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64    /* Parsing and
1f93b 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67   code generating
1f93c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
1f93d 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f  ble *pTab,     /
1f93e 2a 20 44 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  * Description of
1f93f 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72   the table we ar
1f940 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a  e reading from *
1f941 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c  /.  int iColumn,
1f942 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1f943 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d   the table colum
1f944 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c  n */.  int iTabl
1f945 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  e,      /* The c
1f946 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74  ursor pointing t
1f947 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  o the table */. 
1f948 20 69 6e 74 20 69 52 65 67 2c 20 20 20 20 20 20   int iReg,      
1f949 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c    /* Store resul
1f94a 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  ts here */.  int
1f94b 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a   allowAffChng /*
1f94c 20 54 72 75 65 20 69 66 20 70 72 69 6f 72 20 61   True if prior a
1f94d 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 20  ffinity changes 
1f94e 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56  are OK */.){.  V
1f94f 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
1f950 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b  >pVdbe;.  int i;
1f951 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61  .  struct yColCa
1f952 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69  che *p;..  for(i
1f953 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43  =0, p=pParse->aC
1f954 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54  olCache; i<SQLIT
1f955 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b  E_N_COLCACHE; i+
1f956 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, p++){.    if(
1f957 20 70 2d 3e 69 52 65 67 3e 30 20 26 26 20 70 2d   p->iReg>0 && p-
1f958 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20  >iTable==iTable 
1f959 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69  && p->iColumn==i
1f95a 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20  Column.         
1f95b 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68 61    && (!p->affCha
1f95c 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66 43  nge || allowAffC
1f95d 68 6e 67 29 20 29 7b 0a 23 69 66 20 30 0a 20 20  hng) ){.#if 0.  
1f95e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f95f 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f 6f 70  ddOp0(v, OP_Noop
1f960 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
1f961 6d 65 6e 74 28 28 76 2c 20 22 4f 50 54 3a 20 74  ment((v, "OPT: t
1f962 61 62 25 64 2e 63 6f 6c 25 64 20 2d 3e 20 72 25  ab%d.col%d -> r%
1f963 64 22 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c  d", iTable, iCol
1f964 75 6d 6e 2c 20 70 2d 3e 69 52 65 67 29 29 3b 0a  umn, p->iReg));.
1f965 23 65 6e 64 69 66 0a 20 20 20 20 20 20 70 2d 3e  #endif.      p->
1f966 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43  lru = pParse->iC
1f967 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20  acheCnt++;.     
1f968 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
1f969 65 50 69 6e 52 65 67 69 73 74 65 72 28 70 50 61  ePinRegister(pPa
1f96a 72 73 65 2c 20 70 2d 3e 69 52 65 67 29 3b 0a 20  rse, p->iReg);. 
1f96b 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 69       return p->i
1f96c 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 20  Reg;.    }.  }  
1f96d 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
1f96e 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e  );.  if( iColumn
1f96f 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  <0 ){.    sqlite
1f970 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1f971 50 5f 52 6f 77 69 64 2c 20 69 54 61 62 6c 65 2c  P_Rowid, iTable,
1f972 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 20   iReg);.  }else 
1f973 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20  if( pTab==0 ){. 
1f974 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1f975 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
1f976 6e 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75  n, iTable, iColu
1f977 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c  mn, iReg);.  }el
1f978 73 65 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20 3d  se{.    int op =
1f979 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
1f97a 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a 20   ? OP_VColumn : 
1f97b 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73  OP_Column;.    s
1f97c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1f97d 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c 20  (v, op, iTable, 
1f97e 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a  iColumn, iReg);.
1f97f 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d      sqlite3Colum
1f980 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62  nDefault(v, pTab
1f981 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 23 69 66 6e  , iColumn);.#ifn
1f982 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f983 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20  FLOATING_POINT. 
1f984 20 20 20 69 66 28 20 70 54 61 62 2d 3e 61 43 6f     if( pTab->aCo
1f985 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e  l[iColumn].affin
1f986 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
1f987 52 45 41 4c 20 29 7b 0a 20 20 20 20 20 20 73 71  REAL ){.      sq
1f988 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
1f989 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69  v, OP_RealAffini
1f98a 74 79 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d  ty, iReg);.    }
1f98b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71  .#endif.  }.  sq
1f98c 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74  lite3ExprCacheSt
1f98d 6f 72 65 28 70 50 61 72 73 65 2c 20 69 54 61 62  ore(pParse, iTab
1f98e 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65  le, iColumn, iRe
1f98f 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 52 65  g);.  return iRe
1f990 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  g;.}../*.** Clea
1f991 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 63  r all column cac
1f992 68 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 53  he entries..*/.S
1f993 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1f994 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
1f995 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 20 2a  cheClear(Parse *
1f996 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 69  pParse){.  int i
1f997 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
1f998 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28  ache *p;..  for(
1f999 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
1f99a 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
1f99b 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
1f99c 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
1f99d 28 20 70 2d 3e 69 52 65 67 20 29 7b 0a 20 20 20  ( p->iReg ){.   
1f99e 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65     cacheEntryCle
1f99f 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20  ar(pParse, p);. 
1f9a0 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 30       p->iReg = 0
1f9a1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
1f9a2 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20  *.** Record the 
1f9a3 66 61 63 74 20 74 68 61 74 20 61 6e 20 61 66 66  fact that an aff
1f9a4 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73  inity change has
1f9a5 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43 6f   occurred on iCo
1f9a6 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72 73  unt.** registers
1f9a7 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 69   starting with i
1f9a8 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Start..*/.SQLITE
1f9a9 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1f9aa 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
1f9ab 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72  finityChange(Par
1f9ac 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
1f9ad 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f 75  iStart, int iCou
1f9ae 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64 20  nt){.  int iEnd 
1f9af 3d 20 69 53 74 61 72 74 20 2b 20 69 43 6f 75 6e  = iStart + iCoun
1f9b0 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b 0a  t - 1;.  int i;.
1f9b1 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63    struct yColCac
1f9b2 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30  he *p;.  for(i=0
1f9b3 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c  , p=pParse->aCol
1f9b4 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f  Cache; i<SQLITE_
1f9b5 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c  N_COLCACHE; i++,
1f9b6 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72   p++){.    int r
1f9b7 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20   = p->iReg;.    
1f9b8 69 66 28 20 72 3e 3d 69 53 74 61 72 74 20 26 26  if( r>=iStart &&
1f9b9 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20 20   r<=iEnd ){.    
1f9ba 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d    p->affChange =
1f9bb 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   1;.    }.  }.}.
1f9bc 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1f9bd 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 63 6f 6e  code to move con
1f9be 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 74  tent from regist
1f9bf 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f  ers iFrom...iFro
1f9c0 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 72  m+nReg-1.** over
1f9c1 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65   to iTo..iTo+nRe
1f9c2 67 2d 31 2e 20 4b 65 65 70 20 74 68 65 20 63 6f  g-1. Keep the co
1f9c3 6c 75 6d 6e 20 63 61 63 68 65 20 75 70 2d 74 6f  lumn cache up-to
1f9c4 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  -date..*/.SQLITE
1f9c5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1f9c6 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
1f9c7 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
1f9c8 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20   int iFrom, int 
1f9c9 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a  iTo, int nReg){.
1f9ca 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
1f9cb 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
1f9cc 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f    if( iFrom==iTo
1f9cd 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c   ) return;.  sql
1f9ce 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70  ite3VdbeAddOp3(p
1f9cf 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50  Parse->pVdbe, OP
1f9d0 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20 69 54  _Move, iFrom, iT
1f9d1 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f 72 28  o, nReg);.  for(
1f9d2 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
1f9d3 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
1f9d4 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
1f9d5 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e  ++, p++){.    in
1f9d6 74 20 78 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20  t x = p->iReg;. 
1f9d7 20 20 20 69 66 28 20 78 3e 3d 69 46 72 6f 6d 20     if( x>=iFrom 
1f9d8 26 26 20 78 3c 69 46 72 6f 6d 2b 6e 52 65 67 20  && x<iFrom+nReg 
1f9d9 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67  ){.      p->iReg
1f9da 20 2b 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20   += iTo-iFrom;. 
1f9db 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
1f9dc 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1f9dd 74 6f 20 63 6f 70 79 20 63 6f 6e 74 65 6e 74 20  to copy content 
1f9de 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69  from registers i
1f9df 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65  From...iFrom+nRe
1f9e0 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69  g-1.** over to i
1f9e1 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a  To..iTo+nReg-1..
1f9e2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f9e3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
1f9e4 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65  prCodeCopy(Parse
1f9e5 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46   *pParse, int iF
1f9e6 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e  rom, int iTo, in
1f9e7 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69  t nReg){.  int i
1f9e8 3b 0a 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69  ;.  if( iFrom==i
1f9e9 54 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  To ) return;.  f
1f9ea 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20  or(i=0; i<nReg; 
1f9eb 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
1f9ec 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72  3VdbeAddOp2(pPar
1f9ed 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f  se->pVdbe, OP_Co
1f9ee 70 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f  py, iFrom+i, iTo
1f9ef 2b 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  +i);.  }.}../*.*
1f9f0 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
1f9f1 20 61 6e 79 20 72 65 67 69 73 74 65 72 20 69 6e   any register in
1f9f2 20 74 68 65 20 72 61 6e 67 65 20 69 46 72 6f 6d   the range iFrom
1f9f3 2e 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69 76 65  ..iTo (inclusive
1f9f4 29 0a 2a 2a 20 69 73 20 75 73 65 64 20 61 73 20  ).** is used as 
1f9f5 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6c 75  part of the colu
1f9f6 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61  mn cache..*/.sta
1f9f7 74 69 63 20 69 6e 74 20 75 73 65 64 41 73 43 6f  tic int usedAsCo
1f9f8 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 73 65 20  lumnCache(Parse 
1f9f9 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72  *pParse, int iFr
1f9fa 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20  om, int iTo){.  
1f9fb 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
1f9fc 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20  yColCache *p;.  
1f9fd 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73  for(i=0, p=pPars
1f9fe 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c  e->aColCache; i<
1f9ff 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
1fa00 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20  E; i++, p++){.  
1fa01 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65    int r = p->iRe
1fa02 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 46  g;.    if( r>=iF
1fa03 72 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 20  rom && r<=iTo ) 
1fa04 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
1fa05 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
1fa06 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20 69  ** If the last i
1fa07 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 65 64  nstruction coded
1fa08 20 69 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c   is an ephemeral
1fa09 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f 66 0a   copy of any of.
1fa0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 73  ** the registers
1fa0b 20 69 6e 20 74 68 65 20 6e 52 65 67 20 72 65 67   in the nReg reg
1fa0c 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67  isters beginning
1fa0d 20 77 69 74 68 20 69 52 65 67 2c 20 74 68 65 6e   with iReg, then
1fa0e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 65 20  .** convert the 
1fa0f 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  last instruction
1fa10 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 74   from OP_SCopy t
1fa11 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51  o OP_Copy..*/.SQ
1fa12 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1fa13 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72  d sqlite3ExprHar
1fa14 64 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61  dCopy(Parse *pPa
1fa15 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69  rse, int iReg, i
1fa16 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt nReg){.  int 
1fa17 61 64 64 72 3b 0a 20 20 56 64 62 65 4f 70 20 2a  addr;.  VdbeOp *
1fa18 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  pOp;.  Vdbe *v;.
1fa19 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  .  v = pParse->p
1fa1a 56 64 62 65 3b 0a 20 20 61 64 64 72 20 3d 20 73  Vdbe;.  addr = s
1fa1b 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
1fa1c 74 41 64 64 72 28 76 29 3b 0a 20 20 70 4f 70 20  tAddr(v);.  pOp 
1fa1d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74  = sqlite3VdbeGet
1fa1e 4f 70 28 76 2c 20 61 64 64 72 2d 31 29 3b 0a 20  Op(v, addr-1);. 
1fa1f 20 61 73 73 65 72 74 28 20 70 4f 70 20 7c 7c 20   assert( pOp || 
1fa20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
1fa21 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66  ocFailed );.  if
1fa22 28 20 70 4f 70 20 26 26 20 70 4f 70 2d 3e 6f 70  ( pOp && pOp->op
1fa23 63 6f 64 65 3d 3d 4f 50 5f 53 43 6f 70 79 20 26  code==OP_SCopy &
1fa24 26 20 70 4f 70 2d 3e 70 31 3e 3d 69 52 65 67 20  & pOp->p1>=iReg 
1fa25 26 26 20 70 4f 70 2d 3e 70 31 3c 69 52 65 67 2b  && pOp->p1<iReg+
1fa26 6e 52 65 67 20 29 7b 0a 20 20 20 20 70 4f 70 2d  nReg ){.    pOp-
1fa27 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 43 6f 70  >opcode = OP_Cop
1fa28 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  y;.  }.}../*.** 
1fa29 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1fa2a 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65   store the value
1fa2b 20 6f 66 20 74 68 65 20 69 41 6c 69 61 73 2d 74   of the iAlias-t
1fa2c 68 20 61 6c 69 61 73 20 69 6e 20 72 65 67 69 73  h alias in regis
1fa2d 74 65 72 0a 2a 2a 20 74 61 72 67 65 74 2e 20 20  ter.** target.  
1fa2e 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  The first time t
1fa2f 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 70  his is called, p
1fa30 45 78 70 72 20 69 73 20 65 76 61 6c 75 61 74 65  Expr is evaluate
1fa31 64 20 74 6f 20 63 6f 6d 70 75 74 65 0a 2a 2a 20  d to compute.** 
1fa32 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1fa33 20 61 6c 69 61 73 2e 20 20 54 68 65 20 76 61 6c   alias.  The val
1fa34 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ue is stored in 
1fa35 61 6e 20 61 75 78 69 6c 69 61 72 79 20 72 65 67  an auxiliary reg
1fa36 69 73 74 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65  ister.** and the
1fa37 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 61 74 20   number of that 
1fa38 72 65 67 69 73 74 65 72 20 69 73 20 72 65 74 75  register is retu
1fa39 72 6e 65 64 2e 20 20 4f 6e 20 73 75 62 73 65 71  rned.  On subseq
1fa3a 75 65 6e 74 20 63 61 6c 6c 73 2c 0a 2a 2a 20 74  uent calls,.** t
1fa3b 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62  he register numb
1fa3c 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 77  er is returned w
1fa3d 69 74 68 6f 75 74 20 67 65 6e 65 72 61 74 69 6e  ithout generatin
1fa3e 67 20 61 6e 79 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  g any code..**.*
1fa3f 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 6e 20 6f  * Note that in o
1fa40 72 64 65 72 20 66 6f 72 20 74 68 69 73 20 74 6f  rder for this to
1fa41 20 77 6f 72 6b 2c 20 63 6f 64 65 20 6d 75 73 74   work, code must
1fa42 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 69 6e   be generated in
1fa43 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6f 72 64   the.** same ord
1fa44 65 72 20 74 68 61 74 20 69 74 20 69 73 20 65 78  er that it is ex
1fa45 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c  ecuted..**.** Al
1fa46 69 61 73 65 73 20 61 72 65 20 6e 75 6d 62 65 72  iases are number
1fa47 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68  ed starting with
1fa48 20 31 2e 20 20 53 6f 20 69 41 6c 69 61 73 20 69   1.  So iAlias i
1fa49 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 0a 2a  s in the range.*
1fa4a 2a 20 6f 66 20 31 20 74 6f 20 70 50 61 72 73 65  * of 1 to pParse
1fa4b 2d 3e 6e 41 6c 69 61 73 20 69 6e 63 6c 75 73 69  ->nAlias inclusi
1fa4c 76 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 70 50 61 72  ve.  .**.** pPar
1fa4d 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61  se->aAlias[iAlia
1fa4e 73 2d 31 5d 20 72 65 63 6f 72 64 73 20 74 68 65  s-1] records the
1fa4f 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72   register number
1fa50 20 77 68 65 72 65 20 74 68 65 20 76 61 6c 75 65   where the value
1fa51 0a 2a 2a 20 6f 66 20 74 68 65 20 69 41 6c 69 61  .** of the iAlia
1fa52 73 2d 74 68 20 61 6c 69 61 73 20 69 73 20 73 74  s-th alias is st
1fa53 6f 72 65 64 2e 20 20 49 66 20 7a 65 72 6f 2c 20  ored.  If zero, 
1fa54 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  that means that 
1fa55 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20 68 61 73  the.** alias has
1fa56 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 6f   not yet been co
1fa57 6d 70 75 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  mputed..*/.stati
1fa58 63 20 69 6e 74 20 63 6f 64 65 41 6c 69 61 73 28  c int codeAlias(
1fa59 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
1fa5a 6e 74 20 69 41 6c 69 61 73 2c 20 45 78 70 72 20  nt iAlias, Expr 
1fa5b 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67  *pExpr, int targ
1fa5c 65 74 29 7b 0a 23 69 66 20 30 0a 20 20 73 71 6c  et){.#if 0.  sql
1fa5d 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1fa5e 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 52 65  e->db;.  int iRe
1fa5f 67 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  g;.  if( pParse-
1fa60 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c 70 50 61  >nAliasAlloc<pPa
1fa61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 7b 0a 20  rse->nAlias ){. 
1fa62 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61     pParse->aAlia
1fa63 73 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  s = sqlite3DbRea
1fa64 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70  llocOrFree(db, p
1fa65 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 2c 0a 20  Parse->aAlias,. 
1fa66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa68 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61  sizeof(pParse->a
1fa69 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65  Alias[0])*pParse
1fa6a 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20  ->nAlias );.    
1fa6b 74 65 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61  testcase( db->ma
1fa6c 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 70 50  llocFailed && pP
1fa6d 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f  arse->nAliasAllo
1fa6e 63 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 64  c>0 );.    if( d
1fa6f 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1fa70 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
1fa71 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e  memset(&pParse->
1fa72 61 41 6c 69 61 73 5b 70 50 61 72 73 65 2d 3e 6e  aAlias[pParse->n
1fa73 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20 30 2c 0a  AliasAlloc], 0,.
1fa74 20 20 20 20 20 20 20 20 20 20 20 28 70 50 61 72             (pPar
1fa75 73 65 2d 3e 6e 41 6c 69 61 73 2d 70 50 61 72 73  se->nAlias-pPars
1fa76 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 29 2a  e->nAliasAlloc)*
1fa77 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61  sizeof(pParse->a
1fa78 41 6c 69 61 73 5b 30 5d 29 29 3b 0a 20 20 20 20  Alias[0]));.    
1fa79 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c  pParse->nAliasAl
1fa7a 6c 6f 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 41  loc = pParse->nA
1fa7b 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65  lias;.  }.  asse
1fa7c 72 74 28 20 69 41 6c 69 61 73 3e 30 20 26 26 20  rt( iAlias>0 && 
1fa7d 69 41 6c 69 61 73 3c 3d 70 50 61 72 73 65 2d 3e  iAlias<=pParse->
1fa7e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 69 52 65 67  nAlias );.  iReg
1fa7f 20 3d 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61   = pParse->aAlia
1fa80 73 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a 20 20 69  s[iAlias-1];.  i
1fa81 66 28 20 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20  f( iReg==0 ){.  
1fa82 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 69 43    if( pParse->iC
1fa83 61 63 68 65 4c 65 76 65 6c 3e 30 20 29 7b 0a 20  acheLevel>0 ){. 
1fa84 20 20 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69       iReg = sqli
1fa85 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65  te3ExprCodeTarge
1fa86 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  t(pParse, pExpr,
1fa87 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 7d 65   target);.    }e
1fa88 6c 73 65 7b 0a 20 20 20 20 20 20 69 52 65 67 20  lse{.      iReg 
1fa89 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
1fa8a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
1fa8b 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
1fa8c 70 45 78 70 72 2c 20 69 52 65 67 29 3b 0a 20 20  pExpr, iReg);.  
1fa8d 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69      pParse->aAli
1fa8e 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 3d 20 69  as[iAlias-1] = i
1fa8f 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  Reg;.    }.  }. 
1fa90 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 23 65   return iReg;.#e
1fa91 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  lse.  UNUSED_PAR
1fa92 41 4d 45 54 45 52 28 69 41 6c 69 61 73 29 3b 0a  AMETER(iAlias);.
1fa93 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1fa94 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
1fa95 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61  Parse, pExpr, ta
1fa96 72 67 65 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  rget);.#endif.}.
1fa97 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1fa98 63 6f 64 65 20 69 6e 74 6f 20 74 68 65 20 63 75  code into the cu
1fa99 72 72 65 6e 74 20 56 64 62 65 20 74 6f 20 65 76  rrent Vdbe to ev
1fa9a 61 6c 75 61 74 65 20 74 68 65 20 67 69 76 65 6e  aluate the given
1fa9b 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  .** expression. 
1fa9c 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74 6f 72   Attempt to stor
1fa9d 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e  e the results in
1fa9e 20 72 65 67 69 73 74 65 72 20 22 74 61 72 67 65   register "targe
1fa9f 74 22 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  t"..** Return th
1faa0 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65  e register where
1faa1 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f   results are sto
1faa2 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20  red..**.** With 
1faa3 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68  this routine, th
1faa4 65 72 65 20 69 73 20 6e 6f 20 67 75 61 72 61 6e  ere is no guaran
1faa5 74 65 65 20 74 68 61 74 20 72 65 73 75 6c 74 73  tee that results
1faa6 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 73 74 6f 72   will.** be stor
1faa7 65 64 20 69 6e 20 74 61 72 67 65 74 2e 20 20 54  ed in target.  T
1faa8 68 65 20 72 65 73 75 6c 74 20 6d 69 67 68 74 20  he result might 
1faa9 62 65 20 73 74 6f 72 65 64 20 69 6e 20 73 6f 6d  be stored in som
1faaa 65 20 6f 74 68 65 72 0a 2a 2a 20 72 65 67 69 73  e other.** regis
1faab 74 65 72 20 69 66 20 69 74 20 69 73 20 63 6f 6e  ter if it is con
1faac 76 65 6e 69 65 6e 74 20 74 6f 20 64 6f 20 73 6f  venient to do so
1faad 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66  .  The calling f
1faae 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20  unction.** must 
1faaf 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e  check the return
1fab0 20 63 6f 64 65 20 61 6e 64 20 6d 6f 76 65 20 74   code and move t
1fab1 68 65 20 72 65 73 75 6c 74 73 20 74 6f 20 74 68  he results to th
1fab2 65 20 64 65 73 69 72 65 64 0a 2a 2a 20 72 65 67  e desired.** reg
1fab3 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ister..*/.SQLITE
1fab4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1fab5 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67  ite3ExprCodeTarg
1fab6 65 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  et(Parse *pParse
1fab7 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69  , Expr *pExpr, i
1fab8 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 56 64  nt target){.  Vd
1fab9 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
1faba 70 56 64 62 65 3b 20 20 2f 2a 20 54 68 65 20 56  pVdbe;  /* The V
1fabb 4d 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  M under construc
1fabc 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  tion */.  int op
1fabd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1fabe 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64      /* The opcod
1fabf 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f  e being coded */
1fac0 0a 20 20 69 6e 74 20 69 6e 52 65 67 20 3d 20 74  .  int inReg = t
1fac1 61 72 67 65 74 3b 20 20 20 20 20 20 20 2f 2a 20  arget;       /* 
1fac2 52 65 73 75 6c 74 73 20 73 74 6f 72 65 64 20 69  Results stored i
1fac3 6e 20 72 65 67 69 73 74 65 72 20 69 6e 52 65 67  n register inReg
1fac4 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65   */.  int regFre
1fac5 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  e1 = 0;         
1fac6 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66  /* If non-zero f
1fac7 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61  ree this tempora
1fac8 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  ry register */. 
1fac9 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20   int regFree2 = 
1faca 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  0;         /* If
1facb 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74   non-zero free t
1facc 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65  his temporary re
1facd 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
1face 72 31 2c 20 72 32 2c 20 72 33 2c 20 72 34 3b 20  r1, r2, r3, r4; 
1facf 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73        /* Various
1fad0 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72   register number
1fad1 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  s */.  sqlite3 *
1fad2 64 62 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72  db;..  db = pPar
1fad3 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74  se->db;.  assert
1fad4 28 20 76 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61  ( v!=0 || db->ma
1fad5 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
1fad6 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30  assert( target>0
1fad7 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 72   && target<=pPar
1fad8 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 66  se->nMem );.  if
1fad9 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( v==0 ) return 
1fada 30 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d  0;..  if( pExpr=
1fadb 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20 54  =0 ){.    op = T
1fadc 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b  K_NULL;.  }else{
1fadd 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70 72 2d  .    op = pExpr-
1fade 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63  >op;.  }.  switc
1fadf 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  h( op ){.    cas
1fae0 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a  e TK_AGG_COLUMN:
1fae1 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 6f   {.      AggInfo
1fae2 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 45 78   *pAggInfo = pEx
1fae3 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20  pr->pAggInfo;.  
1fae4 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49 6e      struct AggIn
1fae5 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26  fo_col *pCol = &
1fae6 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 70  pAggInfo->aCol[p
1fae7 45 78 70 72 2d 3e 69 41 67 67 5d 3b 0a 20 20 20  Expr->iAgg];.   
1fae8 20 20 20 69 66 28 20 21 70 41 67 67 49 6e 66 6f     if( !pAggInfo
1fae9 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 29 7b 0a  ->directMode ){.
1faea 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1faeb 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 20 29 3b 0a  pCol->iMem>0 );.
1faec 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20          inReg = 
1faed 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a 20 20 20 20  pCol->iMem;.    
1faee 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1faef 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67 67 49   }else if( pAggI
1faf0 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e 67 49  nfo->useSortingI
1faf1 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  dx ){.        sq
1faf2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1faf3 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41  v, OP_Column, pA
1faf4 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e 67 49  ggInfo->sortingI
1faf5 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  dx,.            
1faf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1faf7 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43    pCol->iSorterC
1faf8 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29 3b 0a  olumn, target);.
1faf9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1fafa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
1fafb 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20  Otherwise, fall 
1fafc 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 54 4b  thru into the TK
1fafd 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a 2f 0a  _COLUMN case */.
1fafe 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
1faff 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20  K_COLUMN: {.    
1fb00 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61    if( pExpr->iTa
1fb01 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ble<0 ){.       
1fb02 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61   /* This only ha
1fb03 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 69 6e  ppens when codin
1fb04 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69  g check constrai
1fb05 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  nts */.        a
1fb06 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 63  ssert( pParse->c
1fb07 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20 20 20  kBase>0 );.     
1fb08 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72     inReg = pExpr
1fb09 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50 61 72  ->iColumn + pPar
1fb0a 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20 20 20  se->ckBase;.    
1fb0b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1fb0c 20 74 65 73 74 63 61 73 65 28 20 28 70 45 78 70   testcase( (pExp
1fb0d 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 6e  r->flags & EP_An
1fb0e 79 41 66 66 29 21 3d 30 20 29 3b 0a 20 20 20 20  yAff)!=0 );.    
1fb0f 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69      inReg = sqli
1fb10 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
1fb11 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 45 78  lumn(pParse, pEx
1fb12 70 72 2d 3e 70 54 61 62 2c 0a 20 20 20 20 20 20  pr->pTab,.      
1fb13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fb14 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
1fb15 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45 78 70 72  ->iColumn, pExpr
1fb16 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 65 74  ->iTable, target
1fb17 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1fb18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fb19 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20     pExpr->flags 
1fb1a 26 20 45 50 5f 41 6e 79 41 66 66 29 3b 0a 20 20  & EP_AnyAff);.  
1fb1b 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
1fb1c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1fb1d 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a  e TK_INTEGER: {.
1fb1e 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65        codeIntege
1fb1f 72 28 76 2c 20 70 45 78 70 72 2c 20 30 2c 20 74  r(v, pExpr, 0, t
1fb20 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72  arget);.      br
1fb21 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1fb22 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0a  ase TK_FLOAT: {.
1fb23 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76        codeReal(v
1fb24 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e  , (char*)pExpr->
1fb25 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e  token.z, pExpr->
1fb26 74 6f 6b 65 6e 2e 6e 2c 20 30 2c 20 74 61 72 67  token.n, 0, targ
1fb27 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  et);.      break
1fb28 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1fb29 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20   TK_STRING: {.  
1fb2a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fb2b 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
1fb2c 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c 20  ng8, 0, target, 
1fb2d 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
1fb2e 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
1fb2f 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  *)pExpr->token.z
1fb30 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  , pExpr->token.n
1fb31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1fb32 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
1fb33 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  K_NULL: {.      
1fb34 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fb35 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
1fb36 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
1fb37 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66  break;.    }.#if
1fb38 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1fb39 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20  _BLOB_LITERAL.  
1fb3a 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 20    case TK_BLOB: 
1fb3b 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20  {.      int n;. 
1fb3c 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1fb3d 2a 7a 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  *z;.      char *
1fb3e 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 20 61 73 73  zBlob;.      ass
1fb3f 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65  ert( pExpr->toke
1fb40 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 20 20 20  n.n>=3 );.      
1fb41 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74  assert( pExpr->t
1fb42 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20 7c  oken.z[0]=='x' |
1fb43 7c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  | pExpr->token.z
1fb44 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20  [0]=='X' );.    
1fb45 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1fb46 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27  >token.z[1]=='\'
1fb47 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ' );.      asser
1fb48 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  t( pExpr->token.
1fb49 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  z[pExpr->token.n
1fb4a 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20  -1]=='\'' );.   
1fb4b 20 20 20 6e 20 3d 20 70 45 78 70 72 2d 3e 74 6f     n = pExpr->to
1fb4c 6b 65 6e 2e 6e 20 2d 20 33 3b 0a 20 20 20 20 20  ken.n - 3;.     
1fb4d 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70   z = (char*)pExp
1fb4e 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a  r->token.z + 2;.
1fb4f 20 20 20 20 20 20 7a 42 6c 6f 62 20 3d 20 73 71        zBlob = sq
1fb50 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73  lite3HexToBlob(s
1fb51 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c  qlite3VdbeDb(v),
1fb52 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71   z, n);.      sq
1fb53 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
1fb54 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c  v, OP_Blob, n/2,
1fb55 20 74 61 72 67 65 74 2c 20 30 2c 20 7a 42 6c 6f   target, 0, zBlo
1fb56 62 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a  b, P4_DYNAMIC);.
1fb57 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1fb58 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61   }.#endif.    ca
1fb59 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20  se TK_VARIABLE: 
1fb5a 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 50 72 69  {.      int iPri
1fb5b 6f 72 3b 0a 20 20 20 20 20 20 56 64 62 65 4f 70  or;.      VdbeOp
1fb5c 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 69 66 28   *pOp;.      if(
1fb5d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3c   pExpr->token.n<
1fb5e 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  =1.         && (
1fb5f 69 50 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 33  iPrior = sqlite3
1fb60 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
1fb61 76 29 2d 31 29 3e 3d 30 0a 20 20 20 20 20 20 20  v)-1)>=0.       
1fb62 20 20 26 26 20 28 70 4f 70 20 3d 20 73 71 6c 69    && (pOp = sqli
1fb63 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20  te3VdbeGetOp(v, 
1fb64 69 50 72 69 6f 72 29 29 2d 3e 6f 70 63 6f 64 65  iPrior))->opcode
1fb65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 0a 20 20  ==OP_Variable.  
1fb66 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70         && pOp->p
1fb67 31 2b 70 4f 70 2d 3e 70 33 3d 3d 70 45 78 70 72  1+pOp->p3==pExpr
1fb68 2d 3e 69 54 61 62 6c 65 0a 20 20 20 20 20 20 20  ->iTable.       
1fb69 20 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70    && pOp->p2+pOp
1fb6a 2d 3e 70 33 3d 3d 74 61 72 67 65 74 0a 20 20 20  ->p3==target.   
1fb6b 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 34        && pOp->p4
1fb6c 2e 7a 3d 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20  .z==0.      ){. 
1fb6d 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
1fb6e 20 70 72 65 76 69 6f 75 73 20 69 6e 73 74 72 75   previous instru
1fb6f 63 74 69 6f 6e 20 77 61 73 20 61 20 63 6f 70 79  ction was a copy
1fb70 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   of the previous
1fb71 20 75 6e 6e 61 6d 65 64 0a 20 20 20 20 20 20 20   unnamed.       
1fb72 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e   ** parameter in
1fb73 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  to the previous 
1fb74 72 65 67 69 73 74 65 72 2c 20 74 68 65 6e 20 73  register, then s
1fb75 69 6d 70 6c 79 20 69 6e 63 72 65 6d 65 6e 74 20  imply increment 
1fb76 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72  the.        ** r
1fb77 65 70 65 61 74 20 63 6f 75 6e 74 20 6f 6e 20 74  epeat count on t
1fb78 68 65 20 70 72 69 6f 72 20 69 6e 73 74 72 75 63  he prior instruc
1fb79 74 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61 6e  tion rather than
1fb7a 20 6d 61 6b 69 6e 67 20 61 20 6e 65 77 0a 20 20   making a new.  
1fb7b 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63        ** instruc
1fb7c 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f  tion..        */
1fb7d 0a 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 33  .        pOp->p3
1fb7e 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ++;.      }else{
1fb7f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1fb80 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1fb81 5f 56 61 72 69 61 62 6c 65 2c 20 70 45 78 70 72  _Variable, pExpr
1fb82 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 65 74  ->iTable, target
1fb83 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 1);.        if
1fb84 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  ( pExpr->token.n
1fb85 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >1 ){.          
1fb86 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1fb87 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72  eP4(v, -1, (char
1fb88 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  *)pExpr->token.z
1fb89 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  , pExpr->token.n
1fb8a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1fb8b 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
1fb8c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1fb8d 20 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b 0a   TK_REGISTER: {.
1fb8e 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45        inReg = pE
1fb8f 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20  xpr->iTable;.   
1fb90 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1fb91 20 20 20 20 63 61 73 65 20 54 4b 5f 41 53 3a 20      case TK_AS: 
1fb92 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20  {.      inReg = 
1fb93 63 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65  codeAlias(pParse
1fb94 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
1fb95 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74   pExpr->pLeft, t
1fb96 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72  arget);.      br
1fb97 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  eak;.    }.#ifnd
1fb98 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
1fb99 41 53 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  AST.    case TK_
1fb9a 43 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  CAST: {.      /*
1fb9b 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20   Expressions of 
1fb9c 74 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54  the form:   CAST
1fb9d 28 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29  (pLeft AS token)
1fb9e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 66   */.      int af
1fb9f 66 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 20 20  f, to_op;.      
1fba0 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45  inReg = sqlite3E
1fba1 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
1fba2 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
1fba3 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  ft, target);.   
1fba4 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33     aff = sqlite3
1fba5 41 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45  AffinityType(&pE
1fba6 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20  xpr->token);.   
1fba7 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 2d     to_op = aff -
1fba8 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
1fba9 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 20   + OP_ToText;.  
1fbaa 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f      assert( to_o
1fbab 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 20  p==OP_ToText    
1fbac 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41  || aff!=SQLITE_A
1fbad 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 20  FF_TEXT    );.  
1fbae 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f      assert( to_o
1fbaf 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20  p==OP_ToBlob    
1fbb0 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41  || aff!=SQLITE_A
1fbb1 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 20  FF_NONE    );.  
1fbb2 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f      assert( to_o
1fbb3 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20  p==OP_ToNumeric 
1fbb4 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41  || aff!=SQLITE_A
1fbb5 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20  FF_NUMERIC );.  
1fbb6 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f      assert( to_o
1fbb7 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20  p==OP_ToInt     
1fbb8 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41  || aff!=SQLITE_A
1fbb9 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20  FF_INTEGER );.  
1fbba 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f      assert( to_o
1fbbb 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20  p==OP_ToReal    
1fbbc 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41  || aff!=SQLITE_A
1fbbd 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 20  FF_REAL    );.  
1fbbe 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f      testcase( to
1fbbf 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 29  _op==OP_ToText )
1fbc0 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1fbc1 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c  ( to_op==OP_ToBl
1fbc2 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ob );.      test
1fbc3 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  case( to_op==OP_
1fbc4 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 20  ToNumeric );.   
1fbc5 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f     testcase( to_
1fbc6 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b 0a  op==OP_ToInt );.
1fbc7 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1fbc8 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c  to_op==OP_ToReal
1fbc9 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e   );.      if( in
1fbca 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a 20  Reg!=target ){. 
1fbcb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1fbcc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
1fbcd 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72  Copy, inReg, tar
1fbce 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 6e  get);.        in
1fbcf 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20  Reg = target;.  
1fbd0 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1fbd1 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
1fbd2 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b 0a   to_op, inReg);.
1fbd3 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1fbd4 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68  usedAsColumnCach
1fbd5 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c  e(pParse, inReg,
1fbd6 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 20   inReg) );.     
1fbd7 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
1fbd8 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
1fbd9 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 31  pParse, inReg, 1
1fbda 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1fbdb 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
1fbdc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
1fbdd 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
1fbde 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  LT:.    case TK_
1fbdf 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  LE:.    case TK_
1fbe0 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  GT:.    case TK_
1fbe1 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  GE:.    case TK_
1fbe2 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  NE:.    case TK_
1fbe3 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  EQ: {.      asse
1fbe4 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74  rt( TK_LT==OP_Lt
1fbe5 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1fbe6 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29  ( TK_LE==OP_Le )
1fbe7 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1fbe8 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a  TK_GT==OP_Gt );.
1fbe9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1fbea 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20  _GE==OP_Ge );.  
1fbeb 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45      assert( TK_E
1fbec 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20  Q==OP_Eq );.    
1fbed 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d    assert( TK_NE=
1fbee 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20  =OP_Ne );.      
1fbef 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1fbf0 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _LT );.      tes
1fbf1 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45  tcase( op==TK_LE
1fbf2 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1fbf3 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b  se( op==TK_GT );
1fbf4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1fbf5 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20   op==TK_GE );.  
1fbf6 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1fbf7 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20  ==TK_EQ );.     
1fbf8 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1fbf9 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 6f  K_NE );.      co
1fbfa 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64  deCompareOperand
1fbfb 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  s(pParse, pExpr-
1fbfc 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65  >pLeft, &r1, &re
1fbfd 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20  gFree1,.        
1fbfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbff 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1fc00 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72  >pRight, &r2, &r
1fc01 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
1fc02 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
1fc03 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
1fc04 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
1fc05 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20   op,.           
1fc06 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 69         r1, r2, i
1fc07 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f  nReg, SQLITE_STO
1fc08 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65 73  REP2);.      tes
1fc09 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
1fc0a 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
1fc0b 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
1fc0c 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
1fc0d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1fc0e 20 54 4b 5f 41 4e 44 3a 0a 20 20 20 20 63 61 73   TK_AND:.    cas
1fc0f 65 20 54 4b 5f 4f 52 3a 0a 20 20 20 20 63 61 73  e TK_OR:.    cas
1fc10 65 20 54 4b 5f 50 4c 55 53 3a 0a 20 20 20 20 63  e TK_PLUS:.    c
1fc11 61 73 65 20 54 4b 5f 53 54 41 52 3a 0a 20 20 20  ase TK_STAR:.   
1fc12 20 63 61 73 65 20 54 4b 5f 4d 49 4e 55 53 3a 0a   case TK_MINUS:.
1fc13 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 4d 3a      case TK_REM:
1fc14 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54  .    case TK_BIT
1fc15 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  AND:.    case TK
1fc16 5f 42 49 54 4f 52 3a 0a 20 20 20 20 63 61 73 65  _BITOR:.    case
1fc17 20 54 4b 5f 53 4c 41 53 48 3a 0a 20 20 20 20 63   TK_SLASH:.    c
1fc18 61 73 65 20 54 4b 5f 4c 53 48 49 46 54 3a 0a 20  ase TK_LSHIFT:. 
1fc19 20 20 20 63 61 73 65 20 54 4b 5f 52 53 48 49 46     case TK_RSHIF
1fc1a 54 3a 20 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  T: .    case TK_
1fc1b 43 4f 4e 43 41 54 3a 20 7b 0a 20 20 20 20 20 20  CONCAT: {.      
1fc1c 61 73 73 65 72 74 28 20 54 4b 5f 41 4e 44 3d 3d  assert( TK_AND==
1fc1d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20 20 20 20 20  OP_And );.      
1fc1e 61 73 73 65 72 74 28 20 54 4b 5f 4f 52 3d 3d 4f  assert( TK_OR==O
1fc1f 50 5f 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Or );.      as
1fc20 73 65 72 74 28 20 54 4b 5f 50 4c 55 53 3d 3d 4f  sert( TK_PLUS==O
1fc21 50 5f 41 64 64 20 29 3b 0a 20 20 20 20 20 20 61  P_Add );.      a
1fc22 73 73 65 72 74 28 20 54 4b 5f 4d 49 4e 55 53 3d  ssert( TK_MINUS=
1fc23 3d 4f 50 5f 53 75 62 74 72 61 63 74 20 29 3b 0a  =OP_Subtract );.
1fc24 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1fc25 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d 61 69 6e 64  _REM==OP_Remaind
1fc26 65 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  er );.      asse
1fc27 72 74 28 20 54 4b 5f 42 49 54 41 4e 44 3d 3d 4f  rt( TK_BITAND==O
1fc28 50 5f 42 69 74 41 6e 64 20 29 3b 0a 20 20 20 20  P_BitAnd );.    
1fc29 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54    assert( TK_BIT
1fc2a 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72 20 29 3b 0a  OR==OP_BitOr );.
1fc2b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1fc2c 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44 69 76 69 64  _SLASH==OP_Divid
1fc2d 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  e );.      asser
1fc2e 74 28 20 54 4b 5f 4c 53 48 49 46 54 3d 3d 4f 50  t( TK_LSHIFT==OP
1fc2f 5f 53 68 69 66 74 4c 65 66 74 20 29 3b 0a 20 20  _ShiftLeft );.  
1fc30 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 52      assert( TK_R
1fc31 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 52  SHIFT==OP_ShiftR
1fc32 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 61 73  ight );.      as
1fc33 73 65 72 74 28 20 54 4b 5f 43 4f 4e 43 41 54 3d  sert( TK_CONCAT=
1fc34 3d 4f 50 5f 43 6f 6e 63 61 74 20 29 3b 0a 20 20  =OP_Concat );.  
1fc35 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1fc36 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 20  ==TK_AND );.    
1fc37 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1fc38 54 4b 5f 4f 52 20 29 3b 0a 20 20 20 20 20 20 74  TK_OR );.      t
1fc39 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1fc3a 50 4c 55 53 20 29 3b 0a 20 20 20 20 20 20 74 65  PLUS );.      te
1fc3b 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d  stcase( op==TK_M
1fc3c 49 4e 55 53 20 29 3b 0a 20 20 20 20 20 20 74 65  INUS );.      te
1fc3d 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52  stcase( op==TK_R
1fc3e 45 4d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  EM );.      test
1fc3f 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54  case( op==TK_BIT
1fc40 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73  AND );.      tes
1fc41 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49  tcase( op==TK_BI
1fc42 54 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73  TOR );.      tes
1fc43 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 4c  tcase( op==TK_SL
1fc44 41 53 48 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ASH );.      tes
1fc45 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 53  tcase( op==TK_LS
1fc46 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74 65  HIFT );.      te
1fc47 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52  stcase( op==TK_R
1fc48 53 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74  SHIFT );.      t
1fc49 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1fc4a 43 4f 4e 43 41 54 20 29 3b 0a 20 20 20 20 20 20  CONCAT );.      
1fc4b 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r1 = sqlite3Expr
1fc4c 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
1fc4d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
1fc4e 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
1fc4f 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r2 = sqlite3Exp
1fc50 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
1fc51 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
1fc52 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20   &regFree2);.   
1fc53 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1fc54 64 4f 70 33 28 76 2c 20 6f 70 2c 20 72 32 2c 20  dOp3(v, op, r2, 
1fc55 72 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  r1, target);.   
1fc56 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1fc57 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
1fc58 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
1fc59 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree2==0 );.     
1fc5a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1fc5b 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53    case TK_UMINUS
1fc5c 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  : {.      Expr *
1fc5d 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70  pLeft = pExpr->p
1fc5e 4c 65 66 74 3b 0a 20 20 20 20 20 20 61 73 73 65  Left;.      asse
1fc5f 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20 20 20  rt( pLeft );.   
1fc60 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70     if( pLeft->op
1fc61 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 7b 0a 20 20  ==TK_FLOAT ){.  
1fc62 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76        codeReal(v
1fc63 2c 20 28 63 68 61 72 2a 29 70 4c 65 66 74 2d 3e  , (char*)pLeft->
1fc64 74 6f 6b 65 6e 2e 7a 2c 20 70 4c 65 66 74 2d 3e  token.z, pLeft->
1fc65 74 6f 6b 65 6e 2e 6e 2c 20 31 2c 20 74 61 72 67  token.n, 1, targ
1fc66 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  et);.      }else
1fc67 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d   if( pLeft->op==
1fc68 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20  TK_INTEGER ){.  
1fc69 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65        codeIntege
1fc6a 72 28 76 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74  r(v, pLeft, 1, t
1fc6b 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65  arget);.      }e
1fc6c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 65 67  lse{.        reg
1fc6d 46 72 65 65 31 20 3d 20 72 31 20 3d 20 73 71 6c  Free1 = r1 = sql
1fc6e 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
1fc6f 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
1fc70 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fc71 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
1fc72 20 30 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20   0, r1);.       
1fc73 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r2 = sqlite3Exp
1fc74 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
1fc75 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
1fc76 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20  &regFree2);.    
1fc77 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fc78 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74  ddOp3(v, OP_Subt
1fc79 72 61 63 74 2c 20 72 32 2c 20 72 31 2c 20 74 61  ract, r2, r1, ta
1fc7a 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 74  rget);.        t
1fc7b 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
1fc7c 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a  2==0 );.      }.
1fc7d 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61        inReg = ta
1fc7e 72 67 65 74 3b 0a 20 20 20 20 20 20 62 72 65 61  rget;.      brea
1fc7f 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1fc80 65 20 54 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20  e TK_BITNOT:.   
1fc81 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a   case TK_NOT: {.
1fc82 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1fc83 5f 42 49 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e  _BITNOT==OP_BitN
1fc84 6f 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ot );.      asse
1fc85 72 74 28 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e  rt( TK_NOT==OP_N
1fc86 6f 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ot );.      test
1fc87 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54  case( op==TK_BIT
1fc88 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  NOT );.      tes
1fc89 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f  tcase( op==TK_NO
1fc8a 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  T );.      r1 = 
1fc8b 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1fc8c 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
1fc8d 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
1fc8e 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ee1);.      test
1fc8f 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
1fc90 30 20 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67  0 );.      inReg
1fc91 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20   = target;.     
1fc92 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1fc93 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 69 6e  p2(v, op, r1, in
1fc94 52 65 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61  Reg);.      brea
1fc95 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1fc96 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20  e TK_ISNULL:.   
1fc97 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c   case TK_NOTNULL
1fc98 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64  : {.      int ad
1fc99 64 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  dr;.      assert
1fc9a 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f  ( TK_ISNULL==OP_
1fc9b 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20  IsNull );.      
1fc9c 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55  assert( TK_NOTNU
1fc9d 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29  LL==OP_NotNull )
1fc9e 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1fc9f 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
1fca0 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1fca1 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c  e( op==TK_NOTNUL
1fca2 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  L );.      sqlit
1fca3 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1fca4 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74  OP_Integer, 1, t
1fca5 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 72 31  arget);.      r1
1fca6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1fca7 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
1fca8 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65  Expr->pLeft, &re
1fca9 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74  gFree1);.      t
1fcaa 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
1fcab 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 64  1==0 );.      ad
1fcac 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
1fcad 41 64 64 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31  AddOp1(v, op, r1
1fcae 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1fcaf 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1fcb0 5f 41 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c  _AddImm, target,
1fcb1 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   -1);.      sqli
1fcb2 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1fcb3 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20  v, addr);.      
1fcb4 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1fcb5 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e   case TK_AGG_FUN
1fcb6 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41  CTION: {.      A
1fcb7 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20  ggInfo *pInfo = 
1fcb8 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b  pExpr->pAggInfo;
1fcb9 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f  .      if( pInfo
1fcba 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
1fcbb 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1fcbc 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f  Parse, "misuse o
1fcbd 66 20 61 67 67 72 65 67 61 74 65 3a 20 25 54 22  f aggregate: %T"
1fcbe 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 70  ,.            &p
1fcbf 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  Expr->span);.   
1fcc0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1fcc1 20 20 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d    inReg = pInfo-
1fcc2 3e 61 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41  >aFunc[pExpr->iA
1fcc3 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20  gg].iMem;.      
1fcc4 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1fcc5 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
1fcc6 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20  _CONST_FUNC:.   
1fcc7 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f   case TK_FUNCTIO
1fcc8 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c  N: {.      ExprL
1fcc9 69 73 74 20 2a 70 46 61 72 67 3b 20 20 20 20 20  ist *pFarg;     
1fcca 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 75 6e    /* List of fun
1fccb 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ction arguments 
1fccc 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 61  */.      int nFa
1fccd 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
1fcce 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6e  /* Number of fun
1fccf 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ction arguments 
1fcd0 2a 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66  */.      FuncDef
1fcd1 20 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20   *pDef;         
1fcd2 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  /* The function 
1fcd3 64 65 66 69 6e 69 74 69 6f 6e 20 6f 62 6a 65 63  definition objec
1fcd4 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  t */.      int n
1fcd5 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Id;             
1fcd6 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74    /* Length of t
1fcd7 68 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  he function name
1fcd8 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20   in bytes */.   
1fcd9 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1fcda 49 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  Id;       /* The
1fcdb 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a   function name *
1fcdc 2f 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73  /.      int cons
1fcdd 74 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 2f  tMask = 0;     /
1fcde 2a 20 4d 61 73 6b 20 6f 66 20 66 75 6e 63 74 69  * Mask of functi
1fcdf 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61  on arguments tha
1fce0 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 2a  t are constant *
1fce1 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20  /.      int i;  
1fce2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fce3 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1fce4 2f 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d  /.      u8 enc =
1fce5 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20 20 2f   ENC(db);      /
1fce6 2a 20 54 68 65 20 74 65 78 74 20 65 6e 63 6f 64  * The text encod
1fce7 69 6e 67 20 75 73 65 64 20 62 79 20 74 68 69 73  ing used by this
1fce8 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
1fce9 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
1fcea 6c 20 3d 20 30 3b 20 20 20 20 2f 2a 20 41 20 63  l = 0;    /* A c
1fceb 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1fcec 65 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65  e */..      asse
1fced 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
1fcee 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
1fcef 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
1fcf0 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1fcf1 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29  =TK_CONST_FUNC )
1fcf2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1fcf3 28 20 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f  ( op==TK_FUNCTIO
1fcf4 4e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45  N );.      if( E
1fcf5 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
1fcf6 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65  y(pExpr, EP_Toke
1fcf7 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 6f 6b  nOnly|EP_SpanTok
1fcf8 65 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  en) ){.        p
1fcf9 46 61 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  Farg = 0;.      
1fcfa 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
1fcfb 46 61 72 67 20 3d 20 70 45 78 70 72 2d 3e 78 2e  Farg = pExpr->x.
1fcfc 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pList;.      }. 
1fcfd 20 20 20 20 20 6e 46 61 72 67 20 3d 20 70 46 61       nFarg = pFa
1fcfe 72 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45 78 70  rg ? pFarg->nExp
1fcff 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 7a 49 64  r : 0;.      zId
1fd00 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d   = (char*)pExpr-
1fd01 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20  >token.z;.      
1fd02 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b  nId = pExpr->tok
1fd03 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44 65 66  en.n;.      pDef
1fd04 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
1fd05 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49 64 2c 20  nction(db, zId, 
1fd06 6e 49 64 2c 20 6e 46 61 72 67 2c 20 65 6e 63 2c  nId, nFarg, enc,
1fd07 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72   0);.      asser
1fd08 74 28 20 70 44 65 66 21 3d 30 20 29 3b 0a 20 20  t( pDef!=0 );.  
1fd09 20 20 20 20 69 66 28 20 70 46 61 72 67 20 29 7b      if( pFarg ){
1fd0a 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 71  .        r1 = sq
1fd0b 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
1fd0c 65 28 70 50 61 72 73 65 2c 20 6e 46 61 72 67 29  e(pParse, nFarg)
1fd0d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1fd0e 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
1fd0f 74 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 2c  t(pParse, pFarg,
1fd10 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d   r1, 1);.      }
1fd11 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31  else{.        r1
1fd12 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69   = 0;.      }.#i
1fd13 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1fd14 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
1fd15 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79       /* Possibly
1fd16 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 66 75   overload the fu
1fd17 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20 66 69  nction if the fi
1fd18 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 0a  rst argument is.
1fd19 20 20 20 20 20 20 2a 2a 20 61 20 76 69 72 74 75        ** a virtu
1fd1a 61 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 2e  al table column.
1fd1b 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
1fd1c 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66 75 6e  ** For infix fun
1fd1d 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20 47 4c  ctions (LIKE, GL
1fd1e 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e 64 20  OB, REGEXP, and 
1fd1f 4d 41 54 43 48 29 20 75 73 65 20 74 68 65 0a 20  MATCH) use the. 
1fd20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 20 61       ** second a
1fd21 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74 68 65  rgument, not the
1fd22 20 66 69 72 73 74 2c 20 61 73 20 74 68 65 20 61   first, as the a
1fd23 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73 74 20  rgument to test 
1fd24 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65 65 20  to.      ** see 
1fd25 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c 75 6d  if it is a colum
1fd26 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 74  n in a virtual t
1fd27 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 20 64  able.  This is d
1fd28 6f 6e 65 20 62 65 63 61 75 73 65 0a 20 20 20 20  one because.    
1fd29 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 20 6f 70    ** the left op
1fd2a 65 72 61 6e 64 20 6f 66 20 69 6e 66 69 78 20 66  erand of infix f
1fd2b 75 6e 63 74 69 6f 6e 73 20 28 74 68 65 20 6f 70  unctions (the op
1fd2c 65 72 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f  erand we want to
1fd2d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 72 6f  .      ** contro
1fd2e 6c 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29 20 65  l overloading) e
1fd2f 6e 64 73 20 75 70 20 61 73 20 74 68 65 20 73 65  nds up as the se
1fd30 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
1fd31 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 66 75   the.      ** fu
1fd32 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 65 78 70  nction.  The exp
1fd33 72 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f 62 20  ression "A glob 
1fd34 42 22 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  B" is equivalent
1fd35 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 22 67   to .      ** "g
1fd36 6c 6f 62 28 42 2c 41 29 2e 20 20 57 65 20 77 61  lob(B,A).  We wa
1fd37 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 41 20  nt to use the A 
1fd38 69 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 74 6f  in "A glob B" to
1fd39 20 74 65 73 74 0a 20 20 20 20 20 20 2a 2a 20 66   test.      ** f
1fd3a 6f 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76 65 72  or function over
1fd3b 6c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20 77 65  loading.  But we
1fd3c 20 75 73 65 20 74 68 65 20 42 20 74 65 72 6d 20   use the B term 
1fd3d 69 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22 2e 0a  in "glob(B,A)"..
1fd3e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
1fd3f 66 28 20 6e 46 61 72 67 3e 3d 32 20 26 26 20 28  f( nFarg>=2 && (
1fd40 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45  pExpr->flags & E
1fd41 50 5f 49 6e 66 69 78 46 75 6e 63 29 20 29 7b 0a  P_InfixFunc) ){.
1fd42 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73          pDef = s
1fd43 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f  qlite3VtabOverlo
1fd44 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70  adFunction(db, p
1fd45 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61 72  Def, nFarg, pFar
1fd46 67 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29 3b 0a  g->a[1].pExpr);.
1fd47 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1fd48 6e 46 61 72 67 3e 30 20 29 7b 0a 20 20 20 20 20  nFarg>0 ){.     
1fd49 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65     pDef = sqlite
1fd4a 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e  3VtabOverloadFun
1fd4b 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20  ction(db, pDef, 
1fd4c 6e 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61 5b  nFarg, pFarg->a[
1fd4d 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  0].pExpr);.     
1fd4e 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
1fd4f 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61 72 67  for(i=0; i<nFarg
1fd50 20 26 26 20 69 3c 33 32 3b 20 69 2b 2b 29 7b 0a   && i<32; i++){.
1fd51 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
1fd52 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
1fd53 74 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45  t(pFarg->a[i].pE
1fd54 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20  xpr) ){.        
1fd55 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d 20 28    constMask |= (
1fd56 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20 20 7d  1<<i);.        }
1fd57 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70 44  .        if( (pD
1fd58 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  ef->flags & SQLI
1fd59 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
1fd5a 29 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c 20 29  )!=0 && !pColl )
1fd5b 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c  {.          pCol
1fd5c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
1fd5d 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
1fd5e 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  Farg->a[i].pExpr
1fd5f 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1fd60 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
1fd61 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  Def->flags & SQL
1fd62 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c  ITE_FUNC_NEEDCOL
1fd63 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  L ){.        if(
1fd64 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20   !pColl ) pColl 
1fd65 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b  = db->pDfltColl;
1fd66 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65   .        sqlite
1fd67 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1fd68 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30 2c  P_CollSeq, 0, 0,
1fd69 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c   0, (char *)pCol
1fd6a 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a  l, P4_COLLSEQ);.
1fd6b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
1fd6c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
1fd6d 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20  v, OP_Function, 
1fd6e 63 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c 20 74  constMask, r1, t
1fd6f 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 20 20  arget,.         
1fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1fd71 63 68 61 72 2a 29 70 44 65 66 2c 20 50 34 5f 46  char*)pDef, P4_F
1fd72 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20 20 73  UNCDEF);.      s
1fd73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1fd74 50 35 28 76 2c 20 28 75 38 29 6e 46 61 72 67 29  P5(v, (u8)nFarg)
1fd75 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 61 72  ;.      if( nFar
1fd76 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  g ){.        sql
1fd77 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
1fd78 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c  ange(pParse, r1,
1fd79 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 7d   nFarg);.      }
1fd7a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
1fd7b 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
1fd7c 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31  hange(pParse, r1
1fd7d 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20  , nFarg);.      
1fd7e 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66  break;.    }.#if
1fd7f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1fd80 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61  _SUBQUERY.    ca
1fd81 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20  se TK_EXISTS:.  
1fd82 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54    case TK_SELECT
1fd83 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61  : {.      testca
1fd84 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 58 49 53 54  se( op==TK_EXIST
1fd85 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  S );.      testc
1fd86 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45  ase( op==TK_SELE
1fd87 43 54 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  CT );.      if( 
1fd88 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d  pExpr->iColumn==
1fd89 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
1fd8a 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63  ite3CodeSubselec
1fd8b 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  t(pParse, pExpr,
1fd8c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a   0, 0);.      }.
1fd8d 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45        inReg = pE
1fd8e 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  xpr->iColumn;.  
1fd8f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1fd90 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a  .    case TK_IN:
1fd91 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 4e 6f   {.      int rNo
1fd92 74 46 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 20 20  tFound = 0;.    
1fd93 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75    int rMayHaveNu
1fd94 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e  ll = 0;.      in
1fd95 74 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35  t j2, j3, j4, j5
1fd96 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66  ;.      char aff
1fd97 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 69 6e 74  inity;.      int
1fd98 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 20 20 56   eType;..      V
1fd99 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
1fd9a 76 2c 20 22 62 65 67 69 6e 20 49 4e 20 65 78 70  v, "begin IN exp
1fd9b 72 20 72 25 64 22 2c 20 74 61 72 67 65 74 29 29  r r%d", target))
1fd9c 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d 20  ;.      eType = 
1fd9d 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64  sqlite3FindInInd
1fd9e 65 78 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ex(pParse, pExpr
1fd9f 2c 20 26 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29  , &rMayHaveNull)
1fda0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 4d 61 79  ;.      if( rMay
1fda1 48 61 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  HaveNull ){.    
1fda2 20 20 20 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20      rNotFound = 
1fda3 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
1fda4 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
1fda5 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65  * Figure out the
1fda6 20 61 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65   affinity to use
1fda7 20 74 6f 20 63 72 65 61 74 65 20 61 20 6b 65 79   to create a key
1fda8 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c 74   from the result
1fda9 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68  s.      ** of th
1fdaa 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66  e expression. af
1fdab 66 69 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73  finityStr stores
1fdac 20 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67   a static string
1fdad 20 73 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 20   suitable for.  
1fdae 20 20 20 20 2a 2a 20 50 34 20 6f 66 20 4f 50 5f      ** P4 of OP_
1fdaf 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 20 20 20 20  MakeRecord..    
1fdb0 20 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 69 6e    */.      affin
1fdb1 69 74 79 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e  ity = comparison
1fdb2 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b  Affinity(pExpr);
1fdb3 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65  ...      /* Code
1fdb4 20 74 68 65 20 3c 65 78 70 72 3e 20 66 72 6f 6d   the <expr> from
1fdb5 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 2e 2e 2e   "<expr> IN (...
1fdb6 29 22 2e 20 54 68 65 20 74 65 6d 70 6f 72 61 72  )". The temporar
1fdb7 79 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  y table.      **
1fdb8 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 63   pExpr->iTable c
1fdb9 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75  ontains the valu
1fdba 65 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20  es that make up 
1fdbb 74 68 65 20 28 2e 2e 2e 29 20 73 65 74 2e 0a 20  the (...) set.. 
1fdbc 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
1fdbd 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75  lite3ExprCachePu
1fdbe 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  sh(pParse);.    
1fdbf 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
1fdc0 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
1fdc1 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b  >pLeft, target);
1fdc2 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c 69  .      j2 = sqli
1fdc3 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
1fdc4 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 74 61 72 67   OP_IsNull, targ
1fdc5 65 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65  et);.      if( e
1fdc6 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52  Type==IN_INDEX_R
1fdc7 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20  OWID ){.        
1fdc8 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j3 = sqlite3Vdbe
1fdc9 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73  AddOp1(v, OP_Mus
1fdca 74 42 65 49 6e 74 2c 20 74 61 72 67 65 74 29 3b  tBeInt, target);
1fdcb 0a 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73 71  .        j4 = sq
1fdcc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1fdcd 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
1fdce 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20   pExpr->iTable, 
1fdcf 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
1fdd0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fdd1 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
1fdd2 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b  ger, 1, target);
1fdd3 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73 71  .        j5 = sq
1fdd4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
1fdd5 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 20  v, OP_Goto);.   
1fdd6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1fdd7 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b  JumpHere(v, j3);
1fdd8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1fdd9 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
1fdda 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  j4);.        sql
1fddb 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1fddc 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
1fddd 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
1fdde 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
1fddf 32 20 3d 20 72 65 67 46 72 65 65 32 20 3d 20 73  2 = regFree2 = s
1fde0 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
1fde1 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 20  (pParse);..     
1fde2 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 72     /* Create a r
1fde3 65 63 6f 72 64 20 61 6e 64 20 74 65 73 74 20 66  ecord and test f
1fde4 6f 72 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69  or set membershi
1fde5 70 2e 20 49 66 20 74 68 65 20 73 65 74 20 63 6f  p. If the set co
1fde6 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 20 20 2a  ntains.        *
1fde7 2a 20 74 68 65 20 76 61 6c 75 65 2c 20 74 68 65  * the value, the
1fde8 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e  n jump to the en
1fde9 64 20 6f 66 20 74 68 65 20 74 65 73 74 20 63 6f  d of the test co
1fdea 64 65 2e 20 54 68 65 20 74 61 72 67 65 74 0a 20  de. The target. 
1fdeb 20 20 20 20 20 20 20 2a 2a 20 72 65 67 69 73 74         ** regist
1fdec 65 72 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e  er still contain
1fded 73 20 74 68 65 20 74 72 75 65 20 28 31 29 20 76  s the true (1) v
1fdee 61 6c 75 65 20 77 72 69 74 74 65 6e 20 74 6f 20  alue written to 
1fdef 69 74 20 65 61 72 6c 69 65 72 2e 0a 20 20 20 20  it earlier..    
1fdf0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73      */.        s
1fdf1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1fdf2 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
1fdf3 64 2c 20 74 61 72 67 65 74 2c 20 31 2c 20 72 32  d, target, 1, r2
1fdf4 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b  , &affinity, 1);
1fdf5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1fdf6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1fdf7 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72  _Integer, 1, tar
1fdf8 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 35  get);.        j5
1fdf9 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1fdfa 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64  dOp3(v, OP_Found
1fdfb 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
1fdfc 20 30 2c 20 72 32 29 3b 0a 0a 20 20 20 20 20 20   0, r2);..      
1fdfd 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 74 20    /* If the set 
1fdfe 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 20  membership test 
1fdff 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 65 20  fails, then the 
1fe00 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 0a 20  result of the . 
1fe01 20 20 20 20 20 20 20 2a 2a 20 22 78 20 49 4e 20         ** "x IN 
1fe02 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73 69 6f  (...)" expressio
1fe03 6e 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  n must be either
1fe04 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 74   0 or NULL. If t
1fe05 68 65 20 73 65 74 0a 20 20 20 20 20 20 20 20 2a  he set.        *
1fe06 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55  * contains no NU
1fe07 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20  LL values, then 
1fe08 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 30 2e  the result is 0.
1fe09 20 49 66 20 74 68 65 20 73 65 74 20 0a 20 20 20   If the set .   
1fe0a 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73       ** contains
1fe0b 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 4e 55 4c   one or more NUL
1fe0c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20 74  L values, then t
1fe0d 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1fe0e 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72  .        ** expr
1fe0f 65 73 73 69 6f 6e 20 69 73 20 61 6c 73 6f 20 4e  ession is also N
1fe10 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ULL..        */.
1fe11 20 20 20 20 20 20 20 20 69 66 28 20 72 4e 6f 74          if( rNot
1fe12 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a 20 20 20 20  Found==0 ){.    
1fe13 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72        /* This br
1fe14 61 6e 63 68 20 72 75 6e 73 20 69 66 20 69 74 20  anch runs if it 
1fe15 69 73 20 6b 6e 6f 77 6e 20 61 74 20 63 6f 6d 70  is known at comp
1fe16 69 6c 65 20 74 69 6d 65 20 28 6e 6f 77 29 20 74  ile time (now) t
1fe17 68 61 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a  hat .          *
1fe18 2a 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61 69  * the set contai
1fe19 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65  ns no NULL value
1fe1a 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  s. This happens 
1fe1b 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20  as the result.  
1fe1c 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20          ** of a 
1fe1d 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74  "NOT NULL" const
1fe1e 72 61 69 6e 74 20 69 6e 20 74 68 65 20 64 61 74  raint in the dat
1fe1f 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 4e 6f  abase schema. No
1fe20 20 6e 65 65 64 0a 20 20 20 20 20 20 20 20 20 20   need.          
1fe21 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 64  ** to test the d
1fe22 61 74 61 20 73 74 72 75 63 74 75 72 65 20 61 74  ata structure at
1fe23 20 72 75 6e 74 69 6d 65 20 69 6e 20 74 68 69 73   runtime in this
1fe24 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20   case..         
1fe25 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71   */.          sq
1fe26 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1fe27 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
1fe28 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
1fe29 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1fe2a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63      /* This bloc
1fe2b 6b 20 70 6f 70 75 6c 61 74 65 73 20 74 68 65 20  k populates the 
1fe2c 72 4e 6f 74 46 6f 75 6e 64 20 72 65 67 69 73 74  rNotFound regist
1fe2d 65 72 20 77 69 74 68 20 65 69 74 68 65 72 20 4e  er with either N
1fe2e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ULL.          **
1fe2f 20 6f 72 20 30 20 28 61 6e 20 69 6e 74 65 67 65   or 0 (an intege
1fe30 72 20 76 61 6c 75 65 29 2e 20 49 66 20 74 68 65  r value). If the
1fe31 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
1fe32 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 0a 20 20 20  contains one.   
1fe33 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f 72         ** or mor
1fe34 65 20 4e 55 4c 4c 73 2c 20 74 68 65 6e 20 73 65  e NULLs, then se
1fe35 74 20 72 4e 6f 74 46 6f 75 6e 64 20 74 6f 20 4e  t rNotFound to N
1fe36 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ULL. Otherwise, 
1fe37 73 65 74 20 69 74 0a 20 20 20 20 20 20 20 20 20  set it.         
1fe38 20 2a 2a 20 74 6f 20 30 2e 20 49 66 20 72 65 67   ** to 0. If reg
1fe39 69 73 74 65 72 20 72 4d 61 79 48 61 76 65 4e 75  ister rMayHaveNu
1fe3a 6c 6c 20 69 73 20 61 6c 72 65 61 64 79 20 73 65  ll is already se
1fe3b 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c 75 65 0a  t to some value.
1fe3c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 74 68            ** oth
1fe3d 65 72 20 74 68 61 6e 20 4e 55 4c 4c 2c 20 74 68  er than NULL, th
1fe3e 65 6e 20 74 68 65 20 74 65 73 74 20 68 61 73 20  en the test has 
1fe3f 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 75 6e  already been run
1fe40 20 61 6e 64 20 0a 20 20 20 20 20 20 20 20 20 20   and .          
1fe41 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20  ** rNotFound is 
1fe42 61 6c 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65  already populate
1fe43 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  d..          */.
1fe44 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63            static
1fe45 20 63 6f 6e 73 74 20 63 68 61 72 20 6e 75 6c 6c   const char null
1fe46 52 65 63 6f 72 64 5b 5d 20 3d 20 7b 20 30 78 30  Record[] = { 0x0
1fe47 32 2c 20 30 78 30 30 20 7d 3b 0a 20 20 20 20 20  2, 0x00 };.     
1fe48 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65       j3 = sqlite
1fe49 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
1fe4a 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 4d 61 79 48  P_NotNull, rMayH
1fe4b 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  aveNull);.      
1fe4c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fe4d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
1fe4e 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 3b  , 0, rNotFound);
1fe4f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1fe50 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
1fe51 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20 72 4d 61 79  OP_Blob, 2, rMay
1fe52 48 61 76 65 4e 75 6c 6c 2c 20 30 2c 20 0a 20 20  HaveNull, 0, .  
1fe53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fe54 20 20 20 20 20 20 20 20 20 20 20 6e 75 6c 6c 52             nullR
1fe55 65 63 6f 72 64 2c 20 50 34 5f 53 54 41 54 49 43  ecord, P4_STATIC
1fe56 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 34 20  );.          j4 
1fe57 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1fe58 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Op3(v, OP_Found,
1fe59 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20   pExpr->iTable, 
1fe5a 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29  0, rMayHaveNull)
1fe5b 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1fe5c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1fe5d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
1fe5e 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20 20  rNotFound);.    
1fe5f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1fe60 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29  eJumpHere(v, j4)
1fe61 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1fe62 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1fe63 76 2c 20 6a 33 29 3b 0a 0a 20 20 20 20 20 20 20  v, j3);..       
1fe64 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 76     /* Copy the v
1fe65 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72  alue of register
1fe66 20 72 4e 6f 74 46 6f 75 6e 64 20 28 77 68 69 63   rNotFound (whic
1fe67 68 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c 4c  h is either NULL
1fe68 20 6f 72 20 30 29 0a 20 20 20 20 20 20 20 20 20   or 0).         
1fe69 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 74 61 72   ** into the tar
1fe6a 67 65 74 20 72 65 67 69 73 74 65 72 2e 20 54 68  get register. Th
1fe6b 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 72  is will be the r
1fe6c 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20  esult of the.   
1fe6d 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73         ** expres
1fe6e 73 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 20 20  sion..          
1fe6f 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  */.          sql
1fe70 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1fe71 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 4e 6f 74 46  , OP_Copy, rNotF
1fe72 6f 75 6e 64 2c 20 74 61 72 67 65 74 29 3b 0a 20  ound, target);. 
1fe73 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1fe74 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1fe75 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32  beJumpHere(v, j2
1fe76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1fe77 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
1fe78 6a 35 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  j5);.      sqlit
1fe79 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 70  e3ExprCachePop(p
1fe7a 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 20  Parse, 1);.     
1fe7b 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
1fe7c 20 22 65 6e 64 20 49 4e 20 65 78 70 72 20 72 25   "end IN expr r%
1fe7d 64 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20  d", target));.  
1fe7e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1fe7f 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 0a 20  .#endif.    /*. 
1fe80 20 20 20 2a 2a 20 20 20 20 78 20 42 45 54 57 45     **    x BETWE
1fe81 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 2a  EN y AND z.    *
1fe82 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73  *.    ** This is
1fe83 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 20   equivalent to. 
1fe84 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
1fe85 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20  x>=y AND x<=z.  
1fe86 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 69 73    **.    ** X is
1fe87 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72   stored in pExpr
1fe88 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20  ->pLeft..    ** 
1fe89 59 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70  Y is stored in p
1fe8a 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30  Expr->pList->a[0
1fe8b 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2a 20  ].pExpr..    ** 
1fe8c 5a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70  Z is stored in p
1fe8d 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31  Expr->pList->a[1
1fe8e 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2f 0a  ].pExpr..    */.
1fe8f 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57      case TK_BETW
1fe90 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70  EEN: {.      Exp
1fe91 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72  r *pLeft = pExpr
1fe92 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 73  ->pLeft;.      s
1fe93 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
1fe94 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d 20 70 45  tem *pLItem = pE
1fe95 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 3b  xpr->x.pList->a;
1fe96 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 69  .      Expr *pRi
1fe97 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45  ght = pLItem->pE
1fe98 78 70 72 3b 0a 0a 20 20 20 20 20 20 63 6f 64 65  xpr;..      code
1fe99 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28  CompareOperands(
1fe9a 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 26  pParse, pLeft, &
1fe9b 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20  r1, &regFree1,. 
1fe9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fe9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fe9e 20 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72   pRight, &r2, &r
1fe9f 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
1fea0 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1fea1 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  e1==0 );.      t
1fea2 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
1fea3 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 33  2==0 );.      r3
1fea4 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
1fea5 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
1fea6 20 20 20 20 72 34 20 3d 20 73 71 6c 69 74 65 33      r4 = sqlite3
1fea7 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
1fea8 65 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f  e);.      codeCo
1fea9 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c  mpare(pParse, pL
1feaa 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f  eft, pRight, OP_
1feab 47 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Ge,.            
1feac 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 72 33        r1, r2, r3
1fead 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  , SQLITE_STOREP2
1feae 29 3b 0a 20 20 20 20 20 20 70 4c 49 74 65 6d 2b  );.      pLItem+
1feaf 2b 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 20  +;.      pRight 
1feb0 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b  = pLItem->pExpr;
1feb1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
1feb2 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
1feb3 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a  rse, regFree2);.
1feb4 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74        r2 = sqlit
1feb5 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
1feb6 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 26  Parse, pRight, &
1feb7 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
1feb8 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
1feb9 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee2==0 );.      
1feba 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
1febb 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  se, pLeft, pRigh
1febc 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 2c 20 72 32  t, OP_Le, r1, r2
1febd 2c 20 72 34 2c 20 53 51 4c 49 54 45 5f 53 54 4f  , r4, SQLITE_STO
1febe 52 45 50 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  REP2);.      sql
1febf 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1fec0 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 2c 20 72 34  , OP_And, r3, r4
1fec1 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
1fec2 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
1fec3 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
1fec4 33 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  3);.      sqlite
1fec5 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1fec6 70 50 61 72 73 65 2c 20 72 34 29 3b 0a 20 20 20  pParse, r4);.   
1fec7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1fec8 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 4c 55      case TK_UPLU
1fec9 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67  S: {.      inReg
1feca 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1fecb 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
1fecc 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74   pExpr->pLeft, t
1fecd 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72  arget);.      br
1fece 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
1fecf 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41  /*.    ** Form A
1fed0 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20  :.    **   CASE 
1fed1 78 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72  x WHEN e1 THEN r
1fed2 31 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72  1 WHEN e2 THEN r
1fed3 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48  2 ... WHEN eN TH
1fed4 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44  EN rN ELSE y END
1fed5 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
1fed6 6f 72 6d 20 42 3a 0a 20 20 20 20 2a 2a 20 20 20  orm B:.    **   
1fed7 43 41 53 45 20 57 48 45 4e 20 65 31 20 54 48 45  CASE WHEN e1 THE
1fed8 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 45  N r1 WHEN e2 THE
1fed9 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e  N r2 ... WHEN eN
1feda 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20   THEN rN ELSE y 
1fedb 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  END.    **.    *
1fedc 2a 20 46 6f 72 6d 20 41 20 69 73 20 63 61 6e 20  * Form A is can 
1fedd 62 65 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69  be transformed i
1fede 6e 74 6f 20 74 68 65 20 65 71 75 69 76 61 6c 65  nto the equivale
1fedf 6e 74 20 66 6f 72 6d 20 42 20 61 73 20 66 6f 6c  nt form B as fol
1fee0 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 20 20 20 43  lows:.    **   C
1fee1 41 53 45 20 57 48 45 4e 20 78 3d 65 31 20 54 48  ASE WHEN x=e1 TH
1fee2 45 4e 20 72 31 20 57 48 45 4e 20 78 3d 65 32 20  EN r1 WHEN x=e2 
1fee3 54 48 45 4e 20 72 32 20 2e 2e 2e 0a 20 20 20 20  THEN r2 ....    
1fee4 2a 2a 20 20 20 20 20 20 20 20 57 48 45 4e 20 78  **        WHEN x
1fee5 3d 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45  =eN THEN rN ELSE
1fee6 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20   y END.    **.  
1fee7 20 20 2a 2a 20 58 20 28 69 66 20 69 74 20 65 78    ** X (if it ex
1fee8 69 73 74 73 29 20 69 73 20 69 6e 20 70 45 78 70  ists) is in pExp
1fee9 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a  r->pLeft..    **
1feea 20 59 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e   Y is in pExpr->
1feeb 70 52 69 67 68 74 2e 20 20 54 68 65 20 59 20 69  pRight.  The Y i
1feec 73 20 61 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e  s also optional.
1feed 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
1feee 0a 20 20 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61  .    ** ELSE cla
1feef 75 73 65 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72  use and no other
1fef0 20 74 65 72 6d 20 6d 61 74 63 68 65 73 2c 20 74   term matches, t
1fef1 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f  hen the result o
1fef2 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 70  f the.    ** exp
1fef3 72 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a  rssion is NULL..
1fef4 20 20 20 20 2a 2a 20 45 69 20 69 73 20 69 6e 20      ** Ei is in 
1fef5 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b  pExpr->pList->a[
1fef6 69 2a 32 5d 20 61 6e 64 20 52 69 20 69 73 20 70  i*2] and Ri is p
1fef7 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69  Expr->pList->a[i
1fef8 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20  *2+1]..    **.  
1fef9 20 20 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20    ** The result 
1fefa 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
1fefb 6e 20 69 73 20 74 68 65 20 52 69 20 66 6f 72 20  n is the Ri for 
1fefc 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69  the first matchi
1fefd 6e 67 20 45 69 2c 0a 20 20 20 20 2a 2a 20 6f 72  ng Ei,.    ** or
1fefe 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
1feff 6d 61 74 63 68 69 6e 67 20 45 69 2c 20 74 68 65  matching Ei, the
1ff00 20 45 4c 53 45 20 74 65 72 6d 20 59 2c 20 6f 72   ELSE term Y, or
1ff01 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 20   if there is.   
1ff02 20 2a 2a 20 6e 6f 20 45 4c 53 45 20 74 65 72 6d   ** no ELSE term
1ff03 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20  , NULL..    */. 
1ff04 20 20 20 63 61 73 65 20 54 4b 5f 43 41 53 45 3a     case TK_CASE:
1ff05 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 6e 64   {.      int end
1ff06 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20  Label;          
1ff07 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f             /* GO
1ff08 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 65 6e 64  TO label for end
1ff09 20 6f 66 20 43 41 53 45 20 73 74 6d 74 20 2a 2f   of CASE stmt */
1ff0a 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 78 74 43  .      int nextC
1ff0b 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ase;            
1ff0c 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f           /* GOTO
1ff0d 20 6c 61 62 65 6c 20 66 6f 72 20 6e 65 78 74 20   label for next 
1ff0e 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20  WHEN clause */. 
1ff0f 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72 3b 20       int nExpr; 
1ff10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff11 20 20 20 20 20 20 20 2f 2a 20 32 78 20 6e 75 6d         /* 2x num
1ff12 62 65 72 20 6f 66 20 57 48 45 4e 20 74 65 72 6d  ber of WHEN term
1ff13 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69  s */.      int i
1ff14 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1ff15 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ff16 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
1ff17 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
1ff18 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20  pEList;         
1ff19 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
1ff1a 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f  of WHEN terms */
1ff1b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78  .      struct Ex
1ff1c 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 4c 69  prList_item *aLi
1ff1d 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 72 61  stelem;  /* Arra
1ff1e 79 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20  y of WHEN terms 
1ff1f 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 6f 70  */.      Expr op
1ff20 43 6f 6d 70 61 72 65 3b 20 20 20 20 20 20 20 20  Compare;        
1ff21 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1ff22 65 20 58 3d 3d 45 69 20 65 78 70 72 65 73 73 69  e X==Ei expressi
1ff23 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  on */.      Expr
1ff24 20 63 61 63 68 65 58 3b 20 20 20 20 20 20 20 20   cacheX;        
1ff25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ff26 20 43 61 63 68 65 64 20 65 78 70 72 65 73 73 69   Cached expressi
1ff27 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 20 20 45 78  on X */.      Ex
1ff28 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20 20 20  pr *pX;         
1ff29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff2a 2f 2a 20 54 68 65 20 58 20 65 78 70 72 65 73 73  /* The X express
1ff2b 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70  ion */.      Exp
1ff2c 72 20 2a 70 54 65 73 74 20 3d 20 30 3b 20 20 20  r *pTest = 0;   
1ff2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ff2e 2a 20 58 3d 3d 45 69 20 28 66 6f 72 6d 20 41 29  * X==Ei (form A)
1ff2f 20 6f 72 20 6a 75 73 74 20 45 69 20 28 66 6f 72   or just Ei (for
1ff30 6d 20 42 29 20 2a 2f 0a 20 20 20 20 20 20 56 56  m B) */.      VV
1ff31 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 69 43 61 63  A_ONLY( int iCac
1ff32 68 65 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65  heLevel = pParse
1ff33 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 29  ->iCacheLevel; )
1ff34 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
1ff35 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
1ff36 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
1ff37 6c 65 63 74 29 20 26 26 20 70 45 78 70 72 2d 3e  lect) && pExpr->
1ff38 78 2e 70 4c 69 73 74 20 29 3b 0a 20 20 20 20 20  x.pList );.     
1ff39 20 61 73 73 65 72 74 28 28 70 45 78 70 72 2d 3e   assert((pExpr->
1ff3a 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 25  x.pList->nExpr %
1ff3b 20 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20 20 20   2) == 0);.     
1ff3c 20 61 73 73 65 72 74 28 70 45 78 70 72 2d 3e 78   assert(pExpr->x
1ff3d 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3e 20  .pList->nExpr > 
1ff3e 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 73 74  0);.      pEList
1ff3f 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
1ff40 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74 65 6c  t;.      aListel
1ff41 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a  em = pEList->a;.
1ff42 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70 45        nExpr = pE
1ff43 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  List->nExpr;.   
1ff44 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71     endLabel = sq
1ff45 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1ff46 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28  el(v);.      if(
1ff47 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c   (pX = pExpr->pL
1ff48 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  eft)!=0 ){.     
1ff49 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70 58 3b     cacheX = *pX;
1ff4a 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
1ff4b 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  e( pX->op==TK_CO
1ff4c 4c 55 4d 4e 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d  LUMN || pX->op==
1ff4d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20  TK_REGISTER );. 
1ff4e 20 20 20 20 20 20 20 63 61 63 68 65 58 2e 69 54         cacheX.iT
1ff4f 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78  able = sqlite3Ex
1ff50 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
1ff51 65 2c 20 70 58 2c 20 26 72 65 67 46 72 65 65 31  e, pX, &regFree1
1ff52 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
1ff53 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
1ff54 20 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68   );.        cach
1ff55 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53  eX.op = TK_REGIS
1ff56 54 45 52 3b 0a 20 20 20 20 20 20 20 20 6f 70 43  TER;.        opC
1ff57 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45  ompare.op = TK_E
1ff58 51 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d  Q;.        opCom
1ff59 70 61 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61  pare.pLeft = &ca
1ff5a 63 68 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54  cheX;.        pT
1ff5b 65 73 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65  est = &opCompare
1ff5c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1ff5d 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72  for(i=0; i<nExpr
1ff5e 3b 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20  ; i=i+2){.      
1ff5f 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
1ff60 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a  hePush(pParse);.
1ff61 20 20 20 20 20 20 20 20 69 66 28 20 70 58 20 29          if( pX )
1ff62 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
1ff63 72 74 28 20 70 54 65 73 74 21 3d 30 20 29 3b 0a  rt( pTest!=0 );.
1ff64 20 20 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70            opComp
1ff65 61 72 65 2e 70 52 69 67 68 74 20 3d 20 61 4c 69  are.pRight = aLi
1ff66 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b  stelem[i].pExpr;
1ff67 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1ff68 20 20 20 20 20 20 20 20 20 20 70 54 65 73 74 20            pTest 
1ff69 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70  = aListelem[i].p
1ff6a 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Expr;.        }.
1ff6b 20 20 20 20 20 20 20 20 6e 65 78 74 43 61 73 65          nextCase
1ff6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
1ff6d 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
1ff6e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
1ff6f 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  est->op==TK_COLU
1ff70 4d 4e 20 7c 7c 20 70 54 65 73 74 2d 3e 6f 70 3d  MN || pTest->op=
1ff71 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a  =TK_REGISTER );.
1ff72 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1ff73 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
1ff74 65 2c 20 70 54 65 73 74 2c 20 6e 65 78 74 43 61  e, pTest, nextCa
1ff75 73 65 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  se, SQLITE_JUMPI
1ff76 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20  FNULL);.        
1ff77 74 65 73 74 63 61 73 65 28 20 61 4c 69 73 74 65  testcase( aListe
1ff78 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e  lem[i+1].pExpr->
1ff79 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b  op==TK_COLUMN );
1ff7a 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
1ff7b 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31  e( aListelem[i+1
1ff7c 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ].pExpr->op==TK_
1ff7d 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20  REGISTER );.    
1ff7e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
1ff7f 6f 64 65 28 70 50 61 72 73 65 2c 20 61 4c 69 73  ode(pParse, aLis
1ff80 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72  telem[i+1].pExpr
1ff81 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
1ff82 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ff83 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
1ff84 20 30 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20   0, endLabel);. 
1ff85 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
1ff86 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
1ff87 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  e, 1);.        s
1ff88 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
1ff89 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78 74 43 61  eLabel(v, nextCa
1ff8a 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  se);.      }.   
1ff8b 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52     if( pExpr->pR
1ff8c 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20  ight ){.        
1ff8d 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
1ff8e 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20  Push(pParse);.  
1ff8f 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1ff90 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
1ff91 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 74 61 72  xpr->pRight, tar
1ff92 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  get);.        sq
1ff93 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
1ff94 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20  p(pParse, 1);.  
1ff95 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ff96 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ff97 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
1ff98 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   0, target);.   
1ff99 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
1ff9a 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
1ff9b 6c 65 64 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e  led || pParse->n
1ff9c 45 72 72 3e 30 20 0a 20 20 20 20 20 20 20 20 20  Err>0 .         
1ff9d 20 20 7c 7c 20 70 50 61 72 73 65 2d 3e 69 43 61    || pParse->iCa
1ff9e 63 68 65 4c 65 76 65 6c 3d 3d 69 43 61 63 68 65  cheLevel==iCache
1ff9f 4c 65 76 65 6c 20 29 3b 0a 20 20 20 20 20 20 73  Level );.      s
1ffa0 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
1ffa1 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61 62  eLabel(v, endLab
1ffa2 65 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  el);.      break
1ffa3 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
1ffa4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1ffa5 47 45 52 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  GER.    case TK_
1ffa6 52 41 49 53 45 3a 20 7b 0a 20 20 20 20 20 20 69  RAISE: {.      i
1ffa7 66 28 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67  f( !pParse->trig
1ffa8 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 20  Stack ){.       
1ffa9 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1ffaa 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
1ffab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ffac 22 52 41 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c  "RAISE() may onl
1ffad 79 20 62 65 20 75 73 65 64 20 77 69 74 68 69 6e  y be used within
1ffae 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72   a trigger-progr
1ffaf 61 6d 22 29 3b 0a 20 20 20 20 20 20 20 20 72 65  am");.        re
1ffb0 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
1ffb1 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
1ffb2 3e 61 66 66 69 6e 69 74 79 21 3d 4f 45 5f 49 67  >affinity!=OE_Ig
1ffb3 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20  nore ){.        
1ffb4 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1ffb5 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 52 6f 6c  affinity==OE_Rol
1ffb6 6c 62 61 63 6b 20 7c 7c 0a 20 20 20 20 20 20 20  lback ||.       
1ffb7 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1ffb8 3e 61 66 66 69 6e 69 74 79 20 3d 3d 20 4f 45 5f  >affinity == OE_
1ffb9 41 62 6f 72 74 20 7c 7c 0a 20 20 20 20 20 20 20  Abort ||.       
1ffba 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1ffbb 3e 61 66 66 69 6e 69 74 79 20 3d 3d 20 4f 45 5f  >affinity == OE_
1ffbc 46 61 69 6c 20 29 3b 0a 20 20 20 20 20 20 20 20  Fail );.        
1ffbd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ffbe 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p4(v, OP_Halt, S
1ffbf 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
1ffc0 2c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  , pExpr->affinit
1ffc1 79 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  y, 0,.          
1ffc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
1ffc3 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
1ffc4 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65  n.z, pExpr->toke
1ffc5 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c  n.n);.      } el
1ffc6 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 61 73  se {.         as
1ffc7 73 65 72 74 28 20 70 45 78 70 72 2d 3e 61 66 66  sert( pExpr->aff
1ffc8 69 6e 69 74 79 20 3d 3d 20 4f 45 5f 49 67 6e 6f  inity == OE_Igno
1ffc9 72 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 73  re );.         s
1ffca 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1ffcb 28 76 2c 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f  (v, OP_ContextPo
1ffcc 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  p, 0, 0);.      
1ffcd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ffce 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
1ffcf 20 30 2c 20 70 50 61 72 73 65 2d 3e 74 72 69 67   0, pParse->trig
1ffd0 53 74 61 63 6b 2d 3e 69 67 6e 6f 72 65 4a 75 6d  Stack->ignoreJum
1ffd1 70 29 3b 0a 20 20 20 20 20 20 20 20 20 56 64 62  p);.         Vdb
1ffd2 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 61  eComment((v, "ra
1ffd3 69 73 65 28 49 47 4e 4f 52 45 29 22 29 29 3b 0a  ise(IGNORE)"));.
1ffd4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1ffd5 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
1ffd6 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  f.  }.  sqlite3R
1ffd7 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
1ffd8 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b  arse, regFree1);
1ffd9 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
1ffda 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
1ffdb 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 65   regFree2);.  re
1ffdc 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f  turn inReg;.}../
1ffdd 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
1ffde 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61  de to evaluate a
1ffdf 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  n expression and
1ffe0 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
1ffe1 74 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67  ts.** into a reg
1ffe2 69 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74  ister.  Return t
1ffe3 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62  he register numb
1ffe4 65 72 20 77 68 65 72 65 20 74 68 65 20 72 65 73  er where the res
1ffe5 75 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72  ults.** are stor
1ffe6 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
1ffe7 20 72 65 67 69 73 74 65 72 20 69 73 20 61 20 74   register is a t
1ffe8 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
1ffe9 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 65  r that can be de
1ffea 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68  allocated,.** th
1ffeb 65 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 6d  en write its num
1ffec 62 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20  ber into *pReg. 
1ffed 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 72   If the result r
1ffee 65 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a  egister is not.*
1ffef 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 74  * a temporary, t
1fff0 68 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 6f  hen set *pReg to
1fff1 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45   zero..*/.SQLITE
1fff2 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1fff3 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
1fff4 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1fff5 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
1fff6 20 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 72   *pReg){.  int r
1fff7 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
1fff8 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
1fff9 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65   int r2 = sqlite
1fffa 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
1fffb 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72  pParse, pExpr, r
1fffc 31 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 31  1);.  if( r2==r1
1fffd 20 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20   ){.    *pReg = 
1fffe 72 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r1;.  }else{.   
1ffff 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
20000 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
20001 31 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20  1);.    *pReg = 
20002 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
20003 72 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  r2;.}../*.** Gen
20004 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
20005 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 78  will evaluate ex
20006 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61  pression pExpr a
20007 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20  nd store the.** 
20008 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73  results in regis
20009 74 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 65  ter target.  The
2000a 20 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 61   results are gua
2000b 72 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 61  ranteed to appea
2000c 72 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72  r.** in register
2000d 20 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49   target..*/.SQLI
2000e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2000f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 50  qlite3ExprCode(P
20010 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
20011 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74  pr *pExpr, int t
20012 61 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e  arget){.  int in
20013 52 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  Reg;..  assert( 
20014 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67  target>0 && targ
20015 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  et<=pParse->nMem
20016 20 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71   );.  inReg = sq
20017 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72  lite3ExprCodeTar
20018 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70  get(pParse, pExp
20019 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73  r, target);.  as
2001a 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56  sert( pParse->pV
2001b 64 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  dbe || pParse->d
2001c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2001d 29 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 3d  );.  if( inReg!=
2001e 74 61 72 67 65 74 20 26 26 20 70 50 61 72 73 65  target && pParse
2001f 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73  ->pVdbe ){.    s
20020 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
20021 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20  (pParse->pVdbe, 
20022 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c  OP_SCopy, inReg,
20023 20 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20   target);.  }.  
20024 72 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d  return target;.}
20025 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
20026 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75   code that evalu
20027 74 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 78  tes the given ex
20028 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 74  pression and put
20029 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  s the result.** 
2002a 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67  in register targ
2002b 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d  et..**.** Also m
2002c 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
2002d 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 73  e expression res
2002e 75 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65  ults into anothe
2002f 72 20 22 63 61 63 68 65 22 20 72 65 67 69 73 74  r "cache" regist
20030 65 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79  er.** and modify
20031 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20032 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74  so that the next
20033 20 74 69 6d 65 20 69 74 20 69 73 20 65 76 61 6c   time it is eval
20034 75 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65  uated,.** the re
20035 73 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20 6f  sult is a copy o
20036 66 20 74 68 65 20 63 61 63 68 65 20 72 65 67 69  f the cache regi
20037 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ster..**.** This
20038 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
20039 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73   for expressions
2003a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 6d   that are used m
2003b 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65  ultiple .** time
2003c 73 2e 20 20 54 68 65 79 20 61 72 65 20 65 76 61  s.  They are eva
2003d 6c 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64 20  luated once and 
2003e 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  the results of t
2003f 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  he expression.**
20040 20 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a   are reused..*/.
20041 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
20042 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
20043 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65  deAndCache(Parse
20044 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
20045 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65  pExpr, int targe
20046 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  t){.  Vdbe *v = 
20047 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
20048 20 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e   int inReg;.  in
20049 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Reg = sqlite3Exp
2004a 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
2004b 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  xpr, target);.  
2004c 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30  assert( target>0
2004d 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d   );.  if( pExpr-
2004e 3e 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52  >op!=TK_REGISTER
2004f 20 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69 4d   ){  .    int iM
20050 65 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20 2b  em;.    iMem = +
20051 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
20052 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20053 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c  dOp2(v, OP_Copy,
20054 20 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a 20   inReg, iMem);. 
20055 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
20056 20 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45 78   = iMem;.    pEx
20057 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49  pr->op = TK_REGI
20058 53 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75  STER;.  }.  retu
20059 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a  rn inReg;.}../*.
2005a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
2005b 66 20 70 45 78 70 72 20 69 73 20 61 6e 20 63 6f  f pExpr is an co
2005c 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f  nstant expressio
2005d 6e 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70  n that is approp
2005e 72 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63  riate.** for fac
2005f 74 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20  toring out of a 
20060 6c 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61  loop.  Appropria
20061 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  te expressions a
20062 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  re:.**.**    *  
20063 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74  Any expression t
20064 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f  hat evaluates to
20065 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63   two or more opc
20066 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  odes..**.**    *
20067 20 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72    Any OP_Integer
20068 2c 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74  , OP_Real, OP_St
20069 72 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f  ring, OP_Blob, O
2006a 50 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20  P_Null, .**     
2006b 20 20 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c 65    or OP_Variable
2006c 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e   that does not n
2006d 65 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64  eed to be placed
2006e 20 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20 20   in a .**       
2006f 73 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65  specific registe
20070 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69  r..**.** There i
20071 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61  s no point in fa
20072 63 74 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e 67  ctoring out sing
20073 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63  le-instruction c
20074 6f 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65  onstant.** expre
20075 73 73 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64  ssions that need
20076 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e   to be placed in
20077 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65   a particular re
20078 67 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20  gister.  .** We 
20079 63 6f 75 6c 64 20 66 61 63 74 6f 72 20 74 68 65  could factor the
2007a 6d 20 6f 75 74 2c 20 62 75 74 20 74 68 65 6e 20  m out, but then 
2007b 77 65 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20  we would end up 
2007c 61 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f  adding an.** OP_
2007d 53 43 6f 70 79 20 69 6e 73 74 72 75 63 74 69 6f  SCopy instructio
2007e 6e 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61  n to move the va
2007f 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 63 6f 72  lue into the cor
20080 72 65 63 74 20 72 65 67 69 73 74 65 72 0a 2a 2a  rect register.**
20081 20 6c 61 74 65 72 2e 20 20 57 65 20 6d 69 67 68   later.  We migh
20082 74 20 61 73 20 77 65 6c 6c 20 6a 75 73 74 20 75  t as well just u
20083 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  se the original 
20084 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a  instruction and.
20085 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 4f 50 5f  ** avoid the OP_
20086 53 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  SCopy..*/.static
20087 20 69 6e 74 20 69 73 41 70 70 72 6f 70 72 69 61   int isAppropria
20088 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 45  teForFactoring(E
20089 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21  xpr *p){.  if( !
2008a 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
2008b 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20  stantNotJoin(p) 
2008c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2008d 20 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61    /* Only consta
2008e 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  nt expressions a
2008f 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  re appropriate f
20090 6f 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a  or factoring */.
20091 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c    }.  if( (p->fl
20092 61 67 73 20 26 20 45 50 5f 46 69 78 65 64 44 65  ags & EP_FixedDe
20093 73 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  st)==0 ){.    re
20094 74 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20  turn 1;  /* Any 
20095 63 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f 75 74  constant without
20096 20 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61   a fixed destina
20097 74 69 6f 6e 20 69 73 20 61 70 70 72 6f 70 72 69  tion is appropri
20098 61 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69  ate */.  }.  whi
20099 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50  le( p->op==TK_UP
2009a 4c 55 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65  LUS ) p = p->pLe
2009b 66 74 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d  ft;.  switch( p-
2009c 3e 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  >op ){.#ifndef S
2009d 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
2009e 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65  LITERAL.    case
2009f 20 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66   TK_BLOB:.#endif
200a0 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52  .    case TK_VAR
200a1 49 41 42 4c 45 3a 0a 20 20 20 20 63 61 73 65 20  IABLE:.    case 
200a2 54 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20  TK_INTEGER:.    
200a3 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20  case TK_FLOAT:. 
200a4 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a     case TK_NULL:
200a5 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52  .    case TK_STR
200a6 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  ING: {.      tes
200a7 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b  tcase( p->op==TK
200a8 5f 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20 74  _BLOB );.      t
200a9 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d  estcase( p->op==
200aa 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a 20  TK_VARIABLE );. 
200ab 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
200ac 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52  ->op==TK_INTEGER
200ad 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
200ae 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c  se( p->op==TK_FL
200af 4f 41 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  OAT );.      tes
200b0 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b  tcase( p->op==TK
200b1 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74  _NULL );.      t
200b2 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d  estcase( p->op==
200b3 54 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20 20  TK_STRING );.   
200b4 20 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73     /* Single-ins
200b5 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e  truction constan
200b6 74 73 20 77 69 74 68 20 61 20 66 69 78 65 64 20  ts with a fixed 
200b7 64 65 73 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a  destination are.
200b8 20 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20        ** better 
200b9 64 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49  done in-line.  I
200ba 66 20 77 65 20 66 61 63 74 6f 72 20 74 68 65 6d  f we factor them
200bb 2c 20 74 68 65 79 20 77 69 6c 6c 20 6a 75 73 74  , they will just
200bc 20 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75 70   end.      ** up
200bd 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f   generating an O
200be 50 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20  P_SCopy to move 
200bf 74 68 65 20 76 61 6c 75 65 20 74 6f 20 74 68 65  the value to the
200c0 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20   destination.   
200c1 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 2e 20     ** register. 
200c2 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  */.      return 
200c3 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  0;.    }.    cas
200c4 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20  e TK_UMINUS: {. 
200c5 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65        if( p->pLe
200c6 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54  ft->op==TK_FLOAT
200c7 20 7c 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70   || p->pLeft->op
200c8 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a  ==TK_INTEGER ){.
200c9 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
200ca 30 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20  0;.       }.    
200cb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
200cc 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
200cd 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
200ce 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
200cf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 45  ;.}../*.** If pE
200d0 78 70 72 20 69 73 20 61 20 63 6f 6e 73 74 61 6e  xpr is a constan
200d1 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  t expression tha
200d2 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  t is appropriate
200d3 20 66 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69 6e   for.** factorin
200d4 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2c  g out of a loop,
200d5 20 74 68 65 6e 20 65 76 61 6c 75 61 74 65 20 74   then evaluate t
200d6 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  he expression.**
200d7 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72   into a register
200d8 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 68 65   and convert the
200d9 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f   expression into
200da 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 2a   a TK_REGISTER.*
200db 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f  * expression..*/
200dc 0a 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c  .static int eval
200dd 43 6f 6e 73 74 45 78 70 72 28 57 61 6c 6b 65 72  ConstExpr(Walker
200de 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20   *pWalker, Expr 
200df 2a 70 45 78 70 72 29 7b 0a 20 20 50 61 72 73 65  *pExpr){.  Parse
200e0 20 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b   *pParse = pWalk
200e1 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 77  er->pParse;.  sw
200e2 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
200e3 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52  ){.    case TK_R
200e4 45 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20  EGISTER: {.     
200e5 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
200e6 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e  .    case TK_FUN
200e7 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20  CTION:.    case 
200e8 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a  TK_AGG_FUNCTION:
200e9 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e  .    case TK_CON
200ea 53 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20  ST_FUNC: {.     
200eb 20 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74   /* The argument
200ec 73 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20  s to a function 
200ed 68 61 76 65 20 61 20 66 69 78 65 64 20 64 65 73  have a fixed des
200ee 74 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20  tination..      
200ef 2a 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 69  ** Mark them thi
200f0 73 20 77 61 79 20 74 6f 20 61 76 6f 69 64 20 67  s way to avoid g
200f1 65 6e 65 72 61 74 65 64 20 75 6e 6e 65 65 64 65  enerated unneede
200f2 64 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 20  d OP_SCopy.     
200f3 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73   ** instructions
200f4 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  . .      */.    
200f5 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
200f6 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
200f7 73 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  st;.      assert
200f8 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
200f9 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
200fa 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
200fb 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20   if( pList ){.  
200fc 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4c        int i = pL
200fd 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  ist->nExpr;.    
200fe 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
200ff 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
20100 3d 20 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20  = pList->a;.    
20101 20 20 20 20 66 6f 72 28 3b 20 69 3e 30 3b 20 69      for(; i>0; i
20102 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
20103 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65          if( pIte
20104 6d 2d 3e 70 45 78 70 72 20 29 20 70 49 74 65 6d  m->pExpr ) pItem
20105 2d 3e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c  ->pExpr->flags |
20106 3d 20 45 50 5f 46 69 78 65 64 44 65 73 74 3b 0a  = EP_FixedDest;.
20107 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
20108 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
20109 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69     }.  }.  if( i
2010a 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46  sAppropriateForF
2010b 61 63 74 6f 72 69 6e 67 28 70 45 78 70 72 29 20  actoring(pExpr) 
2010c 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d 20  ){.    int r1 = 
2010d 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2010e 20 20 20 20 69 6e 74 20 72 32 3b 0a 20 20 20 20      int r2;.    
2010f 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r2 = sqlite3Expr
20110 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
20111 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20  e, pExpr, r1);. 
20112 20 20 20 69 66 28 20 72 31 21 3d 72 32 20 29 20     if( r1!=r2 ) 
20113 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
20114 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
20115 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  );.    pExpr->op
20116 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a   = TK_REGISTER;.
20117 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
20118 65 20 3d 20 72 32 3b 0a 20 20 20 20 72 65 74 75  e = r2;.    retu
20119 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
2011a 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  }.  return WRC_C
2011b 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ontinue;.}../*.*
2011c 2a 20 50 72 65 65 76 61 6c 75 61 74 65 20 63 6f  * Preevaluate co
2011d 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65 73  nstant subexpres
2011e 73 69 6f 6e 73 20 77 69 74 68 69 6e 20 70 45 78  sions within pEx
2011f 70 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  pr and store the
20120 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 72  .** results in r
20121 65 67 69 73 74 65 72 73 2e 20 20 4d 6f 64 69 66  egisters.  Modif
20122 79 20 70 45 78 70 72 20 73 6f 20 74 68 61 74 20  y pExpr so that 
20123 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62  the constant sub
20124 65 78 70 72 65 73 69 6f 6e 73 0a 2a 2a 20 61 72  expresions.** ar
20125 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20 6f 70  e TK_REGISTER op
20126 63 6f 64 65 73 20 74 68 61 74 20 72 65 66 65 72  codes that refer
20127 20 74 6f 20 74 68 65 20 70 72 65 63 6f 6d 70 75   to the precompu
20128 74 65 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53  ted values..*/.S
20129 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2012a 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  id sqlite3ExprCo
2012b 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73  deConstants(Pars
2012c 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
2012d 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65  *pExpr){.  Walke
2012e 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  r w;.  w.xExprCa
2012f 6c 6c 62 61 63 6b 20 3d 20 65 76 61 6c 43 6f 6e  llback = evalCon
20130 73 74 45 78 70 72 3b 0a 20 20 77 2e 78 53 65 6c  stExpr;.  w.xSel
20131 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b  ectCallback = 0;
20132 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50  .  w.pParse = pP
20133 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57  arse;.  sqlite3W
20134 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70  alkExpr(&w, pExp
20135 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65  r);.}.../*.** Ge
20136 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
20137 20 70 75 73 68 65 73 20 74 68 65 20 76 61 6c 75   pushes the valu
20138 65 20 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65  e of every eleme
20139 6e 74 20 6f 66 20 74 68 65 20 67 69 76 65 6e 0a  nt of the given.
2013a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  ** expression li
2013b 73 74 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e  st into a sequen
2013c 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ce of registers 
2013d 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 74 61 72  beginning at tar
2013e 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  get..**.** Retur
2013f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
20140 65 6c 65 6d 65 6e 74 73 20 65 76 61 6c 75 61 74  elements evaluat
20141 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
20142 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
20143 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
20144 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
20145 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
20146 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
20147 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
20148 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
20149 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 62 65 20  sion list to be 
2014a 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74  coded */.  int t
2014b 61 72 67 65 74 2c 20 20 20 20 20 20 20 20 2f 2a  arget,        /*
2014c 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20   Where to write 
2014d 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74  results */.  int
2014e 20 64 6f 48 61 72 64 43 6f 70 79 20 20 20 20 20   doHardCopy     
2014f 2f 2a 20 4d 61 6b 65 20 61 20 68 61 72 64 20 63  /* Make a hard c
20150 6f 70 79 20 6f 66 20 65 76 65 72 79 20 65 6c 65  opy of every ele
20151 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72  ment */.){.  str
20152 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
20153 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20  m *pItem;.  int 
20154 69 2c 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  i, n;.  assert( 
20155 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 61 73  pList!=0 );.  as
20156 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 29  sert( target>0 )
20157 3b 0a 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e  ;.  n = pList->n
20158 45 78 70 72 3b 0a 20 20 66 6f 72 28 70 49 74 65  Expr;.  for(pIte
20159 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b  m=pList->a, i=0;
2015a 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d   i<n; i++, pItem
2015b 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74  ++){.    if( pIt
2015c 65 6d 2d 3e 69 41 6c 69 61 73 20 29 7b 0a 20 20  em->iAlias ){.  
2015d 20 20 20 20 69 6e 74 20 69 52 65 67 20 3d 20 63      int iReg = c
2015e 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c  odeAlias(pParse,
2015f 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 2c 20   pItem->iAlias, 
20160 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61  pItem->pExpr, ta
20161 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 20 56  rget+i);.      V
20162 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
20163 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
20164 0a 20 20 20 20 20 20 69 66 28 20 69 52 65 67 21  .      if( iReg!
20165 3d 74 61 72 67 65 74 2b 69 20 29 7b 0a 20 20 20  =target+i ){.   
20166 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20167 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
20168 70 79 2c 20 69 52 65 67 2c 20 74 61 72 67 65 74  py, iReg, target
20169 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  +i);.      }.   
2016a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
2016b 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2016c 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78  arse, pItem->pEx
2016d 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20  pr, target+i);. 
2016e 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 6f 48     }.    if( doH
2016f 61 72 64 43 6f 70 79 20 29 7b 0a 20 20 20 20 20  ardCopy ){.     
20170 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 64   sqlite3ExprHard
20171 43 6f 70 79 28 70 50 61 72 73 65 2c 20 74 61 72  Copy(pParse, tar
20172 67 65 74 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20  get, n);.    }. 
20173 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d   }.  return n;.}
20174 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
20175 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c   code for a bool
20176 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73  ean expression s
20177 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20  uch that a jump 
20178 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68  is made.** to th
20179 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69  e label "dest" i
2017a 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
2017b 20 69 73 20 74 72 75 65 20 62 75 74 20 65 78 65   is true but exe
2017c 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e  cution.** contin
2017d 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72  ues straight thr
2017e 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  u if the express
2017f 69 6f 6e 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a  ion is false..**
20180 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
20181 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20  ssion evaluates 
20182 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72  to NULL (neither
20183 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29   true nor false)
20184 2c 20 74 68 65 6e 0a 2a 2a 20 74 61 6b 65 20 74  , then.** take t
20185 68 65 20 6a 75 6d 70 20 69 66 20 74 68 65 20 6a  he jump if the j
20186 75 6d 70 49 66 4e 75 6c 6c 20 66 6c 61 67 20 69  umpIfNull flag i
20187 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  s SQLITE_JUMPIFN
20188 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ULL..**.** This 
20189 63 6f 64 65 20 64 65 70 65 6e 64 73 20 6f 6e 20  code depends on 
2018a 74 68 65 20 66 61 63 74 20 74 68 61 74 20 63 65  the fact that ce
2018b 72 74 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c 75  rtain token valu
2018c 65 73 20 28 65 78 3a 20 54 4b 5f 45 51 29 0a 2a  es (ex: TK_EQ).*
2018d 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61  * are the same a
2018e 73 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20  s opcode values 
2018f 28 65 78 3a 20 4f 50 5f 45 71 29 20 74 68 61 74  (ex: OP_Eq) that
20190 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63   implement the c
20191 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20  orresponding.** 
20192 6f 70 65 72 61 74 69 6f 6e 2e 20 20 53 70 65 63  operation.  Spec
20193 69 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20  ial comments in 
20194 76 64 62 65 2e 63 20 61 6e 64 20 74 68 65 20 6d  vdbe.c and the m
20195 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72  kopcodeh.awk scr
20196 69 70 74 20 69 6e 0a 2a 2a 20 74 68 65 20 6d 61  ipt in.** the ma
20197 6b 65 20 70 72 6f 63 65 73 73 20 63 61 75 73 65  ke process cause
20198 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 74 6f   these values to
20199 20 61 6c 69 67 6e 2e 20 20 41 73 73 65 72 74 28   align.  Assert(
2019a 29 73 20 69 6e 20 74 68 65 20 63 6f 64 65 0a 2a  )s in the code.*
2019b 2a 20 62 65 6c 6f 77 20 76 65 72 69 66 79 20 74  * below verify t
2019c 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 73 20  hat the numbers 
2019d 61 72 65 20 61 6c 69 67 6e 65 64 20 63 6f 72 72  are aligned corr
2019e 65 63 74 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ectly..*/.SQLITE
2019f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
201a0 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
201a1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
201a2 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
201a3 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 66  dest, int jumpIf
201a4 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a 76  Null){.  Vdbe *v
201a5 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
201a6 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 0a  ;.  int op = 0;.
201a7 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d    int regFree1 =
201a8 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65   0;.  int regFre
201a9 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 31  e2 = 0;.  int r1
201aa 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 28  , r2;..  assert(
201ab 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c   jumpIfNull==SQL
201ac 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c  ITE_JUMPIFNULL |
201ad 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  | jumpIfNull==0 
201ae 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c  );.  if( v==0 ||
201af 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75   pExpr==0 ) retu
201b0 72 6e 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72  rn;.  op = pExpr
201b1 2d 3e 6f 70 3b 0a 20 20 73 77 69 74 63 68 28 20  ->op;.  switch( 
201b2 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
201b3 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 69  K_AND: {.      i
201b4 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56  nt d2 = sqlite3V
201b5 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
201b6 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
201b7 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
201b8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
201b9 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61  xprCachePush(pPa
201ba 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rse);.      sqli
201bb 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
201bc 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
201bd 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75  eft, d2,jumpIfNu
201be 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  ll^SQLITE_JUMPIF
201bf 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c  NULL);.      sql
201c0 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
201c1 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
201c2 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  ight, dest, jump
201c3 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73  IfNull);.      s
201c4 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
201c5 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20  eLabel(v, d2);. 
201c6 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
201c7 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c  CachePop(pParse,
201c8 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   1);.      break
201c9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
201ca 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20   TK_OR: {.      
201cb 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
201cc 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Null==0 );.     
201cd 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72   sqlite3ExprIfTr
201ce 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ue(pParse, pExpr
201cf 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a  ->pLeft, dest, j
201d0 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
201d1 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54    sqlite3ExprIfT
201d2 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  rue(pParse, pExp
201d3 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c  r->pRight, dest,
201d4 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
201d5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
201d6 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
201d7 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61  : {.      testca
201d8 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
201d9 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
201da 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
201db 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
201dc 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ft, dest, jumpIf
201dd 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Null);.      bre
201de 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
201df 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61  se TK_LT:.    ca
201e0 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61  se TK_LE:.    ca
201e1 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61  se TK_GT:.    ca
201e2 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61  se TK_GE:.    ca
201e3 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61  se TK_NE:.    ca
201e4 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20  se TK_EQ: {.    
201e5 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d    assert( TK_LT=
201e6 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20  =OP_Lt );.      
201e7 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f  assert( TK_LE==O
201e8 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Le );.      as
201e9 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f  sert( TK_GT==OP_
201ea 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Gt );.      asse
201eb 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65  rt( TK_GE==OP_Ge
201ec 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
201ed 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29  ( TK_EQ==OP_Eq )
201ee 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
201ef 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a  TK_NE==OP_Ne );.
201f0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
201f1 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20  op==TK_LT );.   
201f2 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
201f3 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20  =TK_LE );.      
201f4 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
201f5 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _GT );.      tes
201f6 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45  tcase( op==TK_GE
201f7 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
201f8 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b  se( op==TK_EQ );
201f9 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
201fa 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20   op==TK_NE );.  
201fb 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
201fc 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
201fd 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
201fe 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c  Operands(pParse,
201ff 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
20200 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20  r1, &regFree1,. 
20201 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20202 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20203 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
20204 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b  &r2, &regFree2);
20205 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
20206 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  re(pParse, pExpr
20207 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e  ->pLeft, pExpr->
20208 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20  pRight, op,.    
20209 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31                r1
2020a 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70  , r2, dest, jump
2020b 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74  IfNull);.      t
2020c 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
2020d 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  1==0 );.      te
2020e 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
2020f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
20210 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
20211 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20  se TK_ISNULL:.  
20212 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c    case TK_NOTNUL
20213 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  L: {.      asser
20214 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50  t( TK_ISNULL==OP
20215 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20  _IsNull );.     
20216 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e   assert( TK_NOTN
20217 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20  ULL==OP_NotNull 
20218 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
20219 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c  e( op==TK_ISNULL
2021a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2021b 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55  se( op==TK_NOTNU
2021c 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  LL );.      r1 =
2021d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2021e 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
2021f 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
20220 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ree1);.      sql
20221 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
20222 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b  , op, r1, dest);
20223 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
20224 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
20225 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
20226 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42   }.    case TK_B
20227 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20  ETWEEN: {.      
20228 2f 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20  /*    x BETWEEN 
20229 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a  y AND z.      **
2022a 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75  .      ** Is equ
2022b 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20  ivalent to .    
2022c 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20    **.      **   
2022d 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20   x>=y AND x<=z. 
2022e 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
2022f 20 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68   Code it as such
20230 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f  , taking care to
20231 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73   do the common s
20232 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20  ubexpression.   
20233 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69     ** elementati
20234 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a  on of x..      *
20235 2f 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70  /.      Expr exp
20236 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72  rAnd;.      Expr
20237 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20   compLeft;.     
20238 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b   Expr compRight;
20239 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72  .      Expr expr
2023a 58 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74  X;..      assert
2023b 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2023c 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
2023d 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
2023e 20 65 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d   exprX = *pExpr-
2023f 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78  >pLeft;.      ex
20240 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e  prAnd.op = TK_AN
20241 44 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64  D;.      exprAnd
20242 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65  .pLeft = &compLe
20243 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e  ft;.      exprAn
20244 64 2e 70 52 69 67 68 74 20 3d 20 26 63 6f 6d 70  d.pRight = &comp
20245 52 69 67 68 74 3b 0a 20 20 20 20 20 20 63 6f 6d  Right;.      com
20246 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45  pLeft.op = TK_GE
20247 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74  ;.      compLeft
20248 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b  .pLeft = &exprX;
20249 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e  .      compLeft.
2024a 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
2024b 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  x.pList->a[0].pE
2024c 78 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52  xpr;.      compR
2024d 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b  ight.op = TK_LE;
2024e 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74  .      compRight
2024f 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b  .pLeft = &exprX;
20250 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74  .      compRight
20251 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  .pRight = pExpr-
20252 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  >x.pList->a[1].p
20253 45 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72  Expr;.      expr
20254 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74  X.iTable = sqlit
20255 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
20256 50 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26  Parse, &exprX, &
20257 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
20258 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
20259 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
2025a 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45  exprX.op = TK_RE
2025b 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65  GISTER;.      te
2025c 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
2025d 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  ll==0 );.      s
2025e 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65  qlite3ExprIfTrue
2025f 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e  (pParse, &exprAn
20260 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  d, dest, jumpIfN
20261 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ull);.      brea
20262 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
20263 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 31  ault: {.      r1
20264 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
20265 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
20266 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 29  Expr, &regFree1)
20267 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
20268 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
20269 49 66 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a 75  If, r1, dest, ju
2026a 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a 20 20  mpIfNull!=0);.  
2026b 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
2026c 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
2026d 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
2026e 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
2026f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
20270 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65  .  }.  sqlite3Re
20271 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
20272 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a  rse, regFree1);.
20273 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
20274 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
20275 72 65 67 46 72 65 65 32 29 3b 20 20 0a 7d 0a 0a  regFree2);  .}..
20276 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
20277 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61  ode for a boolea
20278 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 75 63  n expression suc
20279 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20 69 73  h that a jump is
2027a 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20   made.** to the 
2027b 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69 66 20  label "dest" if 
2027c 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
2027d 73 20 66 61 6c 73 65 20 62 75 74 20 65 78 65 63  s false but exec
2027e 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75  ution.** continu
2027f 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72 75  es straight thru
20280 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
20281 6f 6e 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a  on is true..**.*
20282 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
20283 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 74 6f  ion evaluates to
20284 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 20 74   NULL (neither t
20285 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 20 74  rue nor false) t
20286 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69 66 20 6a  hen.** jump if j
20287 75 6d 70 49 66 4e 75 6c 6c 20 69 73 20 53 51 4c  umpIfNull is SQL
20288 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 6f  ITE_JUMPIFNULL o
20289 72 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 69  r fall through i
2028a 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 0a 2a 2a 20  f jumpIfNull.** 
2028b 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  is 0..*/.SQLITE_
2028c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2028d 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
2028e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
2028f 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
20290 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 66  dest, int jumpIf
20291 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a 76  Null){.  Vdbe *v
20292 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
20293 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 0a  ;.  int op = 0;.
20294 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d    int regFree1 =
20295 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65   0;.  int regFre
20296 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 31  e2 = 0;.  int r1
20297 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 28  , r2;..  assert(
20298 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c   jumpIfNull==SQL
20299 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c  ITE_JUMPIFNULL |
2029a 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  | jumpIfNull==0 
2029b 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c  );.  if( v==0 ||
2029c 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75   pExpr==0 ) retu
2029d 72 6e 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 76 61  rn;..  /* The va
2029e 6c 75 65 20 6f 66 20 70 45 78 70 72 2d 3e 6f 70  lue of pExpr->op
2029f 20 61 6e 64 20 6f 70 20 61 72 65 20 72 65 6c 61   and op are rela
202a0 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ted as follows:.
202a1 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
202a2 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20  pExpr->op       
202a3 20 20 20 20 20 6f 70 0a 20 20 2a 2a 20 20 20 20       op.  **    
202a4 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20     ---------    
202a5 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d        ----------
202a6 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 49  .  **       TK_I
202a7 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 4f  SNULL          O
202a8 50 5f 4e 6f 74 4e 75 6c 6c 0a 20 20 2a 2a 20 20  P_NotNull.  **  
202a9 20 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20       TK_NOTNULL 
202aa 20 20 20 20 20 20 20 20 4f 50 5f 49 73 4e 75 6c          OP_IsNul
202ab 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f  l.  **       TK_
202ac 4e 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NE              
202ad 4f 50 5f 45 71 0a 20 20 2a 2a 20 20 20 20 20 20  OP_Eq.  **      
202ae 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20 20   TK_EQ          
202af 20 20 20 20 4f 50 5f 4e 65 0a 20 20 2a 2a 20 20      OP_Ne.  **  
202b0 20 20 20 20 20 54 4b 5f 47 54 20 20 20 20 20 20       TK_GT      
202b1 20 20 20 20 20 20 20 20 4f 50 5f 4c 65 0a 20 20          OP_Le.  
202b2 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 45 20 20  **       TK_LE  
202b3 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47              OP_G
202b4 74 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f  t.  **       TK_
202b5 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  GE              
202b6 4f 50 5f 4c 74 0a 20 20 2a 2a 20 20 20 20 20 20  OP_Lt.  **      
202b7 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 20   TK_LT          
202b8 20 20 20 20 4f 50 5f 47 65 0a 20 20 2a 2a 0a 20      OP_Ge.  **. 
202b9 20 2a 2a 20 46 6f 72 20 6f 74 68 65 72 20 76 61   ** For other va
202ba 6c 75 65 73 20 6f 66 20 70 45 78 70 72 2d 3e 6f  lues of pExpr->o
202bb 70 2c 20 6f 70 20 69 73 20 75 6e 64 65 66 69 6e  p, op is undefin
202bc 65 64 20 61 6e 64 20 75 6e 75 73 65 64 2e 0a 20  ed and unused.. 
202bd 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66   ** The value of
202be 20 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e   TK_ and OP_ con
202bf 73 74 61 6e 74 73 20 61 72 65 20 61 72 72 61 6e  stants are arran
202c0 67 65 64 20 73 75 63 68 20 74 68 61 74 20 77 65  ged such that we
202c1 0a 20 20 2a 2a 20 63 61 6e 20 63 6f 6d 70 75 74  .  ** can comput
202c2 65 20 74 68 65 20 6d 61 70 70 69 6e 67 20 61 62  e the mapping ab
202c3 6f 76 65 20 75 73 69 6e 67 20 74 68 65 20 66 6f  ove using the fo
202c4 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 73 69  llowing expressi
202c5 6f 6e 2e 0a 20 20 2a 2a 20 41 73 73 65 72 74 28  on..  ** Assert(
202c6 29 73 20 76 65 72 69 66 79 20 74 68 61 74 20 74  )s verify that t
202c7 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 69  he computation i
202c8 73 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2f 0a  s correct..  */.
202c9 20 20 6f 70 20 3d 20 28 28 70 45 78 70 72 2d 3e    op = ((pExpr->
202ca 6f 70 2b 28 54 4b 5f 49 53 4e 55 4c 4c 26 31 29  op+(TK_ISNULL&1)
202cb 29 5e 31 29 2d 28 54 4b 5f 49 53 4e 55 4c 4c 26  )^1)-(TK_ISNULL&
202cc 31 29 3b 0a 0a 20 20 2f 2a 20 56 65 72 69 66 79  1);..  /* Verify
202cd 20 63 6f 72 72 65 63 74 20 61 6c 69 67 6e 6d 65   correct alignme
202ce 6e 74 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50  nt of TK_ and OP
202cf 5f 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a 2f  _ constants.  */
202d0 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72  .  assert( pExpr
202d1 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ->op!=TK_ISNULL 
202d2 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c  || op==OP_NotNul
202d3 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  l );.  assert( p
202d4 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 4f 54  Expr->op!=TK_NOT
202d5 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 49  NULL || op==OP_I
202d6 73 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72  sNull );.  asser
202d7 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
202d8 5f 4e 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 45 71  _NE || op==OP_Eq
202d9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
202da 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 45 51 20 7c  xpr->op!=TK_EQ |
202db 7c 20 6f 70 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20  | op==OP_Ne );. 
202dc 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
202dd 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20 6f 70 3d  op!=TK_LT || op=
202de 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 61 73 73 65  =OP_Ge );.  asse
202df 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
202e0 4b 5f 4c 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47  K_LE || op==OP_G
202e1 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t );.  assert( p
202e2 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 54 20  Expr->op!=TK_GT 
202e3 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 65 20 29 3b 0a  || op==OP_Le );.
202e4 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
202e5 3e 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 6f 70  >op!=TK_GE || op
202e6 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 0a 20 20 73 77  ==OP_Lt );..  sw
202e7 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
202e8 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  ){.    case TK_A
202e9 4e 44 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  ND: {.      test
202ea 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
202eb 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
202ec 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
202ed 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
202ee 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  Left, dest, jump
202ef 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73  IfNull);.      s
202f0 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
202f1 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
202f2 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a  >pRight, dest, j
202f3 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
202f4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
202f5 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 20 7b     case TK_OR: {
202f6 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20  .      int d2 = 
202f7 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
202f8 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 74  abel(v);.      t
202f9 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
202fa 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
202fb 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
202fc 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20  Push(pParse);.  
202fd 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
202fe 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45  fTrue(pParse, pE
202ff 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 32 2c 20  xpr->pLeft, d2, 
20300 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 4c 49 54  jumpIfNull^SQLIT
20301 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
20302 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
20303 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
20304 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64  pExpr->pRight, d
20305 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
20306 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
20307 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
20308 76 2c 20 64 32 29 3b 0a 20 20 20 20 20 20 73 71  v, d2);.      sq
20309 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
2030a 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20  p(pParse, 1);.  
2030b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2030c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
2030d 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
2030e 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
2030f 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
20310 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
20311 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ll);.      break
20312 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
20313 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65   TK_LT:.    case
20314 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65   TK_LE:.    case
20315 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65   TK_GT:.    case
20316 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65   TK_GE:.    case
20317 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65   TK_NE:.    case
20318 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20   TK_EQ: {.      
20319 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
2031a 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _LT );.      tes
2031b 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45  tcase( op==TK_LE
2031c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2031d 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b  se( op==TK_GT );
2031e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2031f 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20   op==TK_GE );.  
20320 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
20321 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20  ==TK_EQ );.     
20322 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
20323 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_NE );.      te
20324 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
20325 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63  ll==0 );.      c
20326 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
20327 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ds(pParse, pExpr
20328 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72  ->pLeft, &r1, &r
20329 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20  egFree1,.       
2032a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2032b 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
2032c 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26  ->pRight, &r2, &
2032d 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
2032e 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61   codeCompare(pPa
2032f 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
20330 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
20331 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20  , op,.          
20332 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20          r1, r2, 
20333 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
20334 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
20335 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
20336 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
20337 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b  ( regFree2==0 );
20338 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
20339 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
2033a 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65  ISNULL:.    case
2033b 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20   TK_NOTNULL: {. 
2033c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
2033d 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a  p==TK_ISNULL );.
2033e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2033f 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29  op==TK_NOTNULL )
20340 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
20341 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
20342 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
20343 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31  pLeft, &regFree1
20344 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
20345 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70  VdbeAddOp2(v, op
20346 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a 20 20 20  , r1, dest);.   
20347 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
20348 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
20349 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2034a 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45     case TK_BETWE
2034b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20  EN: {.      /*  
2034c 20 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e    x BETWEEN y AN
2034d 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  D z.      **.   
2034e 20 20 20 2a 2a 20 49 73 20 65 71 75 69 76 61 6c     ** Is equival
2034f 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a  ent to .      **
20350 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d  .      **    x>=
20351 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20  y AND x<=z.     
20352 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64   **.      ** Cod
20353 65 20 69 74 20 61 73 20 73 75 63 68 2c 20 74 61  e it as such, ta
20354 6b 69 6e 67 20 63 61 72 65 20 74 6f 20 64 6f 20  king care to do 
20355 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78  the common subex
20356 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a  pression.      *
20357 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  * elementation o
20358 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  f x..      */.  
20359 20 20 20 20 45 78 70 72 20 65 78 70 72 41 6e 64      Expr exprAnd
2035a 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d  ;.      Expr com
2035b 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 45 78 70  pLeft;.      Exp
2035c 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20  r compRight;.   
2035d 20 20 20 45 78 70 72 20 65 78 70 72 58 3b 0a 0a     Expr exprX;..
2035e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
2035f 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20360 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
20361 63 74 29 20 29 3b 0a 20 20 20 20 20 20 65 78 70  ct) );.      exp
20362 72 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65  rX = *pExpr->pLe
20363 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e  ft;.      exprAn
20364 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20  d.op = TK_AND;. 
20365 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 4c 65       exprAnd.pLe
20366 66 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a  ft = &compLeft;.
20367 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 52        exprAnd.pR
20368 69 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67 68  ight = &compRigh
20369 74 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66  t;.      compLef
2036a 74 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20  t.op = TK_GE;.  
2036b 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65      compLeft.pLe
2036c 66 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20  ft = &exprX;.   
2036d 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67     compLeft.pRig
2036e 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ht = pExpr->x.pL
2036f 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b  ist->a[0].pExpr;
20370 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74  .      compRight
20371 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20  .op = TK_LE;.   
20372 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65     compRight.pLe
20373 66 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20  ft = &exprX;.   
20374 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 70 52 69     compRight.pRi
20375 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ght = pExpr->x.p
20376 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72  List->a[1].pExpr
20377 3b 0a 20 20 20 20 20 20 65 78 70 72 58 2e 69 54  ;.      exprX.iT
20378 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78  able = sqlite3Ex
20379 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
2037a 65 2c 20 26 65 78 70 72 58 2c 20 26 72 65 67 46  e, &exprX, &regF
2037b 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73  ree1);.      tes
2037c 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
2037d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72  =0 );.      expr
2037e 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54  X.op = TK_REGIST
2037f 45 52 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  ER;.      testca
20380 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
20381 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
20382 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
20383 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64 2c 20  arse, &exprAnd, 
20384 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
20385 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
20386 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
20387 74 3a 20 7b 0a 20 20 20 20 20 20 72 31 20 3d 20  t: {.      r1 = 
20388 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
20389 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
2038a 72 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  r, &regFree1);. 
2038b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2038c 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66 4e  AddOp3(v, OP_IfN
2038d 6f 74 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a 75  ot, r1, dest, ju
2038e 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a 20 20  mpIfNull!=0);.  
2038f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
20390 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
20391 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
20392 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
20393 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
20394 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65  .  }.  sqlite3Re
20395 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
20396 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a  rse, regFree1);.
20397 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
20398 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
20399 72 65 67 46 72 65 65 32 29 3b 0a 7d 0a 0a 2f 2a  regFree2);.}../*
2039a 0a 2a 2a 20 44 6f 20 61 20 64 65 65 70 20 63 6f  .** Do a deep co
2039b 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20  mparison of two 
2039c 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73  expression trees
2039d 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45 20 28  .  Return TRUE (
2039e 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a 20 69 66 20  non-zero).** if 
2039f 74 68 65 79 20 61 72 65 20 69 64 65 6e 74 69 63  they are identic
203a0 61 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 46 41  al and return FA
203a1 4c 53 45 20 69 66 20 74 68 65 79 20 64 69 66 66  LSE if they diff
203a2 65 72 20 69 6e 20 61 6e 79 20 77 61 79 2e 0a 2a  er in any way..*
203a3 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 74  *.** Sometimes t
203a4 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
203a5 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 65 76   return FALSE ev
203a6 65 6e 20 69 66 20 74 68 65 20 74 77 6f 20 65 78  en if the two ex
203a7 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 72 65 61  pressions.** rea
203a8 6c 6c 79 20 61 72 65 20 65 71 75 69 76 61 6c 65  lly are equivale
203a9 6e 74 2e 20 20 49 66 20 77 65 20 63 61 6e 6e 6f  nt.  If we canno
203aa 74 20 70 72 6f 76 65 20 74 68 61 74 20 74 68 65  t prove that the
203ab 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
203ac 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2c 20 77  .** identical, w
203ad 65 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 6a  e return FALSE j
203ae 75 73 74 20 74 6f 20 62 65 20 73 61 66 65 2e 20  ust to be safe. 
203af 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74   So if this rout
203b0 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 66  ine.** returns f
203b1 61 6c 73 65 2c 20 74 68 65 6e 20 79 6f 75 20 64  alse, then you d
203b2 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6b 6e 6f  o not really kno
203b3 77 20 66 6f 72 20 63 65 72 74 61 69 6e 20 69 66  w for certain if
203b4 20 74 68 65 20 74 77 6f 0a 2a 2a 20 65 78 70 72   the two.** expr
203b5 65 73 73 69 6f 6e 73 20 61 72 65 20 74 68 65 20  essions are the 
203b6 73 61 6d 65 2e 20 20 42 75 74 20 69 66 20 79 6f  same.  But if yo
203b7 75 20 67 65 74 20 61 20 54 52 55 45 20 72 65 74  u get a TRUE ret
203b8 75 72 6e 2c 20 74 68 65 6e 20 79 6f 75 0a 2a 2a  urn, then you.**
203b9 20 63 61 6e 20 62 65 20 73 75 72 65 20 74 68 65   can be sure the
203ba 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
203bb 20 74 68 65 20 73 61 6d 65 2e 20 20 49 6e 20 74   the same.  In t
203bc 68 65 20 70 6c 61 63 65 73 20 77 68 65 72 65 0a  he places where.
203bd 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
203be 69 73 20 75 73 65 64 2c 20 69 74 20 64 6f 65 73  is used, it does
203bf 20 6e 6f 74 20 68 75 72 74 20 74 6f 20 67 65 74   not hurt to get
203c0 20 61 6e 20 65 78 74 72 61 20 46 41 4c 53 45 20   an extra FALSE 
203c1 2d 20 74 68 61 74 0a 2a 2a 20 6a 75 73 74 20 6d  - that.** just m
203c2 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 73  ight result in s
203c3 6f 6d 65 20 73 6c 69 67 68 74 6c 79 20 73 6c 6f  ome slightly slo
203c4 77 65 72 20 63 6f 64 65 2e 20 20 42 75 74 20 72  wer code.  But r
203c5 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 61 6e 20 69  eturning.** an i
203c6 6e 63 6f 72 72 65 63 74 20 54 52 55 45 20 63 6f  ncorrect TRUE co
203c7 75 6c 64 20 6c 65 61 64 20 74 6f 20 61 20 6d 61  uld lead to a ma
203c8 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  lfunction..*/.SQ
203c9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
203ca 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70   sqlite3ExprComp
203cb 61 72 65 28 45 78 70 72 20 2a 70 41 2c 20 45 78  are(Expr *pA, Ex
203cc 70 72 20 2a 70 42 29 7b 0a 20 20 69 6e 74 20 69  pr *pB){.  int i
203cd 3b 0a 20 20 69 66 28 20 70 41 3d 3d 30 7c 7c 70  ;.  if( pA==0||p
203ce 42 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  B==0 ){.    retu
203cf 72 6e 20 70 42 3d 3d 70 41 3b 0a 20 20 7d 0a 20  rn pB==pA;.  }. 
203d0 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
203d1 65 72 74 79 28 70 41 2c 20 45 50 5f 78 49 73 53  erty(pA, EP_xIsS
203d2 65 6c 65 63 74 29 20 7c 7c 20 45 78 70 72 48 61  elect) || ExprHa
203d3 73 50 72 6f 70 65 72 74 79 28 70 42 2c 20 45 50  sProperty(pB, EP
203d4 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
203d5 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
203d6 0a 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 67  .  if( (pA->flag
203d7 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29  s & EP_Distinct)
203d8 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20 26 20 45  !=(pB->flags & E
203d9 50 5f 44 69 73 74 69 6e 63 74 29 20 29 20 72 65  P_Distinct) ) re
203da 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41  turn 0;.  if( pA
203db 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72  ->op!=pB->op ) r
203dc 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21  eturn 0;.  if( !
203dd 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
203de 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70 42  re(pA->pLeft, pB
203df 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74 75 72  ->pLeft) ) retur
203e0 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  n 0;.  if( !sqli
203e1 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
203e2 41 2d 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e 70  A->pRight, pB->p
203e3 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
203e4 30 3b 0a 0a 20 20 69 66 28 20 70 41 2d 3e 78 2e  0;..  if( pA->x.
203e5 70 4c 69 73 74 20 26 26 20 70 42 2d 3e 78 2e 70  pList && pB->x.p
203e6 4c 69 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20  List ){.    if( 
203e7 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78  pA->x.pList->nEx
203e8 70 72 21 3d 70 42 2d 3e 78 2e 70 4c 69 73 74 2d  pr!=pB->x.pList-
203e9 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e 20  >nExpr ) return 
203ea 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
203eb 69 3c 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e  i<pA->x.pList->n
203ec 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
203ed 20 20 45 78 70 72 20 2a 70 45 78 70 72 41 20 3d    Expr *pExprA =
203ee 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b   pA->x.pList->a[
203ef 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
203f0 45 78 70 72 20 2a 70 45 78 70 72 42 20 3d 20 70  Expr *pExprB = p
203f1 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 69 5d  B->x.pList->a[i]
203f2 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66  .pExpr;.      if
203f3 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f  ( !sqlite3ExprCo
203f4 6d 70 61 72 65 28 70 45 78 70 72 41 2c 20 70 45  mpare(pExprA, pE
203f5 78 70 72 42 29 20 29 20 72 65 74 75 72 6e 20 30  xprB) ) return 0
203f6 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
203f7 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 20  if( pA->x.pList 
203f8 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69 73 74 20 29  || pB->x.pList )
203f9 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
203fa 20 20 7d 0a 0a 20 20 69 66 28 20 70 41 2d 3e 69    }..  if( pA->i
203fb 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c  Table!=pB->iTabl
203fc 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e  e || pA->iColumn
203fd 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20  !=pB->iColumn ) 
203fe 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
203ff 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  pA->op!=TK_COLUM
20400 4e 20 26 26 20 70 41 2d 3e 74 6f 6b 65 6e 2e 7a  N && pA->token.z
20401 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e   ){.    if( pB->
20402 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 29 20 72 65 74  token.z==0 ) ret
20403 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 70  urn 0;.    if( p
20404 42 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 41 2d 3e  B->token.n!=pA->
20405 74 6f 6b 65 6e 2e 6e 20 29 20 72 65 74 75 72 6e  token.n ) return
20406 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   0;.    if( sqli
20407 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61  te3StrNICmp((cha
20408 72 2a 29 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 2c 28  r*)pA->token.z,(
20409 63 68 61 72 2a 29 70 42 2d 3e 74 6f 6b 65 6e 2e  char*)pB->token.
2040a 7a 2c 70 42 2d 3e 74 6f 6b 65 6e 2e 6e 29 21 3d  z,pB->token.n)!=
2040b 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
2040c 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 0;.    }.  }. 
2040d 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f   return 1;.}.../
2040e 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65  *.** Add a new e
2040f 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 41  lement to the pA
20410 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61  ggInfo->aCol[] a
20411 72 72 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68  rray.  Return th
20412 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68  e index of.** th
20413 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20  e new element.  
20414 52 65 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76  Return a negativ
20415 65 20 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c  e number if mall
20416 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61  oc fails..*/.sta
20417 74 69 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e  tic int addAggIn
20418 66 6f 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33  foColumn(sqlite3
20419 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70   *db, AggInfo *p
2041a 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  Info){.  int i;.
2041b 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20    pInfo->aCol = 
2041c 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f  sqlite3ArrayAllo
2041d 63 61 74 65 28 0a 20 20 20 20 20 20 20 64 62 2c  cate(.       db,
2041e 0a 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61  .       pInfo->a
2041f 43 6f 6c 2c 0a 20 20 20 20 20 20 20 73 69 7a 65  Col,.       size
20420 6f 66 28 70 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30  of(pInfo->aCol[0
20421 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20 20  ]),.       3,.  
20422 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f       &pInfo->nCo
20423 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 26 70 49  lumn,.       &pI
20424 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f  nfo->nColumnAllo
20425 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20 20 29  c,.       &i.  )
20426 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20  ;.  return i;.} 
20427 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61     ../*.** Add a
20428 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20   new element to 
20429 74 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46  the pAggInfo->aF
2042a 75 6e 63 5b 5d 20 61 72 72 61 79 2e 20 20 52 65  unc[] array.  Re
2042b 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f  turn the index o
2042c 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c 65  f.** the new ele
2042d 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20  ment.  Return a 
2042e 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20  negative number 
2042f 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e  if malloc fails.
20430 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
20431 64 64 41 67 67 49 6e 66 6f 46 75 6e 63 28 73 71  ddAggInfoFunc(sq
20432 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 67 49 6e  lite3 *db, AggIn
20433 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e  fo *pInfo){.  in
20434 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46  t i;.  pInfo->aF
20435 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 41 72 72  unc = sqlite3Arr
20436 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20  ayAllocate(.    
20437 20 20 20 64 62 2c 20 0a 20 20 20 20 20 20 20 70     db, .       p
20438 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 0a 20 20 20  Info->aFunc,.   
20439 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f      sizeof(pInfo
2043a 2d 3e 61 46 75 6e 63 5b 30 5d 29 2c 0a 20 20 20  ->aFunc[0]),.   
2043b 20 20 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70      3,.       &p
2043c 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20  Info->nFunc,.   
2043d 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e      &pInfo->nFun
2043e 63 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26  cAlloc,.       &
2043f 69 0a 20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  i.  );.  return 
20440 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20  i;.}    ../*.** 
20441 54 68 69 73 20 69 73 20 74 68 65 20 78 45 78 70  This is the xExp
20442 72 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20  rCallback for a 
20443 74 72 65 65 20 77 61 6c 6b 65 72 2e 20 20 49 74  tree walker.  It
20444 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69   is used to.** i
20445 6d 70 6c 65 6d 65 6e 74 20 73 71 6c 69 74 65 33  mplement sqlite3
20446 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65  ExprAnalyzeAggre
20447 67 61 74 65 73 28 29 2e 20 20 53 65 65 20 73 71  gates().  See sq
20448 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
20449 41 67 67 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f  Aggregates.** fo
2044a 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
2044b 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  ormation..*/.sta
2044c 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41  tic int analyzeA
2044d 67 67 72 65 67 61 74 65 28 57 61 6c 6b 65 72 20  ggregate(Walker 
2044e 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a  *pWalker, Expr *
2044f 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b  pExpr){.  int i;
20450 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  .  NameContext *
20451 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75  pNC = pWalker->u
20452 2e 70 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70  .pNC;.  Parse *p
20453 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61  Parse = pNC->pPa
20454 72 73 65 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a  rse;.  SrcList *
20455 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e  pSrcList = pNC->
20456 70 53 72 63 4c 69 73 74 3b 0a 20 20 41 67 67 49  pSrcList;.  AggI
20457 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20  nfo *pAggInfo = 
20458 70 4e 43 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a  pNC->pAggInfo;..
20459 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
2045a 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
2045b 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20  TK_AGG_COLUMN:. 
2045c 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d     case TK_COLUM
2045d 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  N: {.      testc
2045e 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ase( pExpr->op==
2045f 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b  TK_AGG_COLUMN );
20460 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
20461 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43   pExpr->op==TK_C
20462 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f  OLUMN );.      /
20463 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
20464 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20  f the column is 
20465 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 61  in one of the ta
20466 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  bles in the FROM
20467 0a 20 20 20 20 20 20 2a 2a 20 63 6c 61 75 73 65  .      ** clause
20468 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74   of the aggregat
20469 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 20  e query */.     
2046a 20 69 66 28 20 70 53 72 63 4c 69 73 74 20 29 7b   if( pSrcList ){
2046b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20  .        struct 
2046c 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
2046d 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e  tem = pSrcList->
2046e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  a;.        for(i
2046f 3d 30 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e  =0; i<pSrcList->
20470 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  nSrc; i++, pItem
20471 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ++){.          s
20472 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f  truct AggInfo_co
20473 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20  l *pCol;.       
20474 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54     if( pExpr->iT
20475 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75  able==pItem->iCu
20476 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20  rsor ){.        
20477 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61      /* If we rea
20478 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69  ch this point, i
20479 74 20 6d 65 61 6e 73 20 74 68 61 74 20 70 45 78  t means that pEx
2047a 70 72 20 72 65 66 65 72 73 20 74 6f 20 61 20 74  pr refers to a t
2047b 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20  able.           
2047c 20 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 20 74   ** that is in t
2047d 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
2047e 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  f the aggregate 
2047f 71 75 65 72 79 2e 20 20 0a 20 20 20 20 20 20 20  query.  .       
20480 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
20481 20 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65      ** Make an e
20482 6e 74 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c  ntry for the col
20483 75 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d  umn in pAggInfo-
20484 3e 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65  >aCol[] if there
20485 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
20486 69 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20  is not an entry 
20487 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20  there already.. 
20488 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
20489 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b            int k;
2048a 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f  .            pCo
2048b 6c 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43  l = pAggInfo->aC
2048c 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ol;.            
2048d 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49  for(k=0; k<pAggI
2048e 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b  nfo->nColumn; k+
2048f 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  +, pCol++){.    
20490 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
20491 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70  ol->iTable==pExp
20492 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20  r->iTable &&.   
20493 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
20494 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45  Col->iColumn==pE
20495 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a  xpr->iColumn ){.
20496 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20497 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
20498 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
20499 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
2049a 20 69 66 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66   if( (k>=pAggInf
2049b 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20  o->nColumn).    
2049c 20 20 20 20 20 20 20 20 20 26 26 20 28 6b 20 3d           && (k =
2049d 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d   addAggInfoColum
2049e 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 41  n(pParse->db, pA
2049f 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20  ggInfo))>=0 .   
204a0 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20           ){.    
204a1 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d            pCol =
204a2 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c   &pAggInfo->aCol
204a3 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  [k];.           
204a4 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20     pCol->pTab = 
204a5 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20  pExpr->pTab;.   
204a6 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
204a7 3e 69 54 61 62 6c 65 20 3d 20 70 45 78 70 72 2d  >iTable = pExpr-
204a8 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20  >iTable;.       
204a9 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f         pCol->iCo
204aa 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43  lumn = pExpr->iC
204ab 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20  olumn;.         
204ac 20 20 20 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20       pCol->iMem 
204ad 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
204ae 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
204af 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c  pCol->iSorterCol
204b0 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  umn = -1;.      
204b1 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45          pCol->pE
204b2 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20  xpr = pExpr;.   
204b3 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
204b4 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42  AggInfo->pGroupB
204b5 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  y ){.           
204b6 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20       int j, n;. 
204b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
204b8 78 70 72 4c 69 73 74 20 2a 70 47 42 20 3d 20 70  xprList *pGB = p
204b9 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42  AggInfo->pGroupB
204ba 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y;.             
204bb 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
204bc 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d  st_item *pTerm =
204bd 20 70 47 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20   pGB->a;.       
204be 20 20 20 20 20 20 20 20 20 6e 20 3d 20 70 47 42           n = pGB
204bf 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ->nExpr;.       
204c0 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30           for(j=0
204c1 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72  ; j<n; j++, pTer
204c2 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
204c3 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45          Expr *pE
204c4 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b   = pTerm->pExpr;
204c5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
204c6 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54     if( pE->op==T
204c7 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e  K_COLUMN && pE->
204c8 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69  iTable==pExpr->i
204c9 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20  Table &&.       
204ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
204cb 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70  E->iColumn==pExp
204cc 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  r->iColumn ){.  
204cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
204ce 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43    pCol->iSorterC
204cf 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20  olumn = j;.     
204d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
204d1 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
204d2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
204d3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
204d4 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
204d5 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
204d6 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d  ol->iSorterColum
204d7 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  n<0 ){.         
204d8 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f         pCol->iSo
204d9 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67  rterColumn = pAg
204da 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43  gInfo->nSortingC
204db 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20  olumn++;.       
204dc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
204dd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
204de 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e     /* There is n
204df 6f 77 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20  ow an entry for 
204e0 70 45 78 70 72 20 69 6e 20 70 41 67 67 49 6e 66  pExpr in pAggInf
204e1 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65  o->aCol[] (eithe
204e2 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  r.            **
204e3 20 62 65 63 61 75 73 65 20 69 74 20 77 61 73 20   because it was 
204e4 74 68 65 72 65 20 62 65 66 6f 72 65 20 6f 72 20  there before or 
204e5 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 20  because we just 
204e6 63 72 65 61 74 65 64 20 69 74 29 2e 0a 20 20 20  created it)..   
204e7 20 20 20 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76           ** Conv
204e8 65 72 74 20 74 68 65 20 70 45 78 70 72 20 74 6f  ert the pExpr to
204e9 20 62 65 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c   be a TK_AGG_COL
204ea 55 4d 4e 20 72 65 66 65 72 72 69 6e 67 20 74 6f  UMN referring to
204eb 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 20 20   that.          
204ec 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61    ** pAggInfo->a
204ed 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20  Col[] entry..   
204ee 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
204ef 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
204f0 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e  AggInfo = pAggIn
204f1 66 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  fo;.            
204f2 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41  pExpr->op = TK_A
204f3 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20  GG_COLUMN;.     
204f4 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41         pExpr->iA
204f5 67 67 20 3d 20 6b 3b 0a 20 20 20 20 20 20 20 20  gg = k;.        
204f6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
204f7 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20       } /* endif 
204f8 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70  pExpr->iTable==p
204f9 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f  Item->iCursor */
204fa 0a 20 20 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e  .        } /* en
204fb 64 20 6c 6f 6f 70 20 6f 76 65 72 20 70 53 72 63  d loop over pSrc
204fc 4c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  List */.      }.
204fd 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
204fe 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20  _Prune;.    }.  
204ff 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55    case TK_AGG_FU
20500 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20  NCTION: {.      
20501 2f 2a 20 54 68 65 20 70 4e 43 2d 3e 6e 44 65 70  /* The pNC->nDep
20502 74 68 3d 3d 30 20 74 65 73 74 20 63 61 75 73 65  th==0 test cause
20503 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  s aggregate func
20504 74 69 6f 6e 73 20 69 6e 20 73 75 62 71 75 65 72  tions in subquer
20505 69 65 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ies.      ** to 
20506 62 65 20 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20  be ignored */.  
20507 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65      if( pNC->nDe
20508 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pth==0 ){.      
20509 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
2050a 65 20 69 66 20 70 45 78 70 72 20 69 73 20 61 20  e if pExpr is a 
2050b 64 75 70 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f  duplicate of ano
2050c 74 68 65 72 20 61 67 67 72 65 67 61 74 65 20 0a  ther aggregate .
2050d 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74          ** funct
2050e 69 6f 6e 20 74 68 61 74 20 69 73 20 61 6c 72 65  ion that is alre
2050f 61 64 79 20 69 6e 20 74 68 65 20 70 41 67 67 49  ady in the pAggI
20510 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 20 20  nfo structure.  
20511 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
20512 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
20513 66 75 6e 63 20 2a 70 49 74 65 6d 20 3d 20 70 41  func *pItem = pA
20514 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20  ggInfo->aFunc;. 
20515 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
20516 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e  i<pAggInfo->nFun
20517 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  c; i++, pItem++)
20518 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
20519 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
2051a 72 65 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c  re(pItem->pExpr,
2051b 20 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20   pExpr) ){.     
2051c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2051d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2051e 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2051f 69 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75  i>=pAggInfo->nFu
20520 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nc ){.          
20521 2f 2a 20 70 45 78 70 72 20 69 73 20 6f 72 69 67  /* pExpr is orig
20522 69 6e 61 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65  inal.  Make a ne
20523 77 20 65 6e 74 72 79 20 69 6e 20 70 41 67 67 49  w entry in pAggI
20524 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20  nfo->aFunc[].   
20525 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
20526 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43      u8 enc = ENC
20527 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20  (pParse->db);.  
20528 20 20 20 20 20 20 20 20 69 20 3d 20 61 64 64 41          i = addA
20529 67 67 49 6e 66 6f 46 75 6e 63 28 70 50 61 72 73  ggInfoFunc(pPars
2052a 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e 66 6f 29  e->db, pAggInfo)
2052b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
2052c 69 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  i>=0 ){.        
2052d 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2052e 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
2052f 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
20530 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ) );.           
20531 20 70 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e   pItem = &pAggIn
20532 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20  fo->aFunc[i];.  
20533 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d            pItem-
20534 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a  >pExpr = pExpr;.
20535 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65              pIte
20536 6d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72  m->iMem = ++pPar
20537 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
20538 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 46 75        pItem->pFu
20539 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  nc = sqlite3Find
2053a 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d  Function(pParse-
2053b 3e 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  >db,.           
2053c 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
2053d 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70  Expr->token.z, p
2053e 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 0a 20  Expr->token.n,. 
2053f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20540 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74    pExpr->x.pList
20541 20 3f 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   ? pExpr->x.pLis
20542 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 65 6e  t->nExpr : 0, en
20543 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  c, 0);.         
20544 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c     if( pExpr->fl
20545 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63  ags & EP_Distinc
20546 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
20547 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69     pItem->iDisti
20548 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  nct = pParse->nT
20549 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  ab++;.          
2054a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2054b 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44         pItem->iD
2054c 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20  istinct = -1;.  
2054d 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2054e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2054f 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b  }.        /* Mak
20550 65 20 70 45 78 70 72 20 70 6f 69 6e 74 20 74 6f  e pExpr point to
20551 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
20552 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63   pAggInfo->aFunc
20553 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20 20 20 20  [] entry.       
20554 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 45 78 70   */.        pExp
20555 72 2d 3e 69 41 67 67 20 3d 20 69 3b 0a 20 20 20  r->iAgg = i;.   
20556 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67       pExpr->pAgg
20557 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b  Info = pAggInfo;
20558 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
20559 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20  WRC_Prune;.     
2055a 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
2055b 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
2055c 75 65 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ue;.}.static int
2055d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74   analyzeAggregat
2055e 65 73 49 6e 53 65 6c 65 63 74 28 57 61 6c 6b 65  esInSelect(Walke
2055f 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
20560 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20  ct *pSelect){.  
20561 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
20562 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e   = pWalker->u.pN
20563 43 3b 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44  C;.  if( pNC->nD
20564 65 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 70  epth==0 ){.    p
20565 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b 0a 20 20  NC->nDepth++;.  
20566 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c    sqlite3WalkSel
20567 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 53 65  ect(pWalker, pSe
20568 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e 43 2d 3e  lect);.    pNC->
20569 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 20 20 72 65  nDepth--;.    re
2056a 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
2056b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
2056c 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  urn WRC_Continue
2056d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
2056e 6e 61 6c 79 7a 65 20 74 68 65 20 67 69 76 65 6e  nalyze the given
2056f 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b   expression look
20570 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74  ing for aggregat
20571 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 0a  e functions and.
20572 2a 2a 20 66 6f 72 20 76 61 72 69 61 62 6c 65 73  ** for variables
20573 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65   that need to be
20574 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 50   added to the pP
20575 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72  arse->aAgg[] arr
20576 61 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61 64 64 69  ay..** Make addi
20577 74 69 6f 6e 61 6c 20 65 6e 74 72 69 65 73 20 74  tional entries t
20578 6f 20 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41  o the pParse->aA
20579 67 67 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65  gg[] array as ne
2057a 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54  cessary..**.** T
2057b 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
2057c 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ld only be calle
2057d 64 20 61 66 74 65 72 20 74 68 65 20 65 78 70 72  d after the expr
2057e 65 73 73 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a  ession has been.
2057f 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 62 79 20 73  ** analyzed by s
20580 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
20581 72 4e 61 6d 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c  rNames()..*/.SQL
20582 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
20583 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
20584 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e 61  yzeAggregates(Na
20585 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20  meContext *pNC, 
20586 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20  Expr *pExpr){.  
20587 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 45  Walker w;.  w.xE
20588 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e  xprCallback = an
20589 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 3b 0a  alyzeAggregate;.
2058a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62    w.xSelectCallb
2058b 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67  ack = analyzeAgg
2058c 72 65 67 61 74 65 73 49 6e 53 65 6c 65 63 74 3b  regatesInSelect;
2058d 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43  .  w.u.pNC = pNC
2058e 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  ;.  sqlite3WalkE
2058f 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a  xpr(&w, pExpr);.
20590 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71  }../*.** Call sq
20591 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
20592 41 67 67 72 65 67 61 74 65 73 28 29 20 66 6f 72  Aggregates() for
20593 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f   every expressio
20594 6e 20 69 6e 20 61 6e 0a 2a 2a 20 65 78 70 72 65  n in an.** expre
20595 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 52 65 74  ssion list.  Ret
20596 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
20597 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  f errors..**.** 
20598 49 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 66  If an error is f
20599 6f 75 6e 64 2c 20 74 68 65 20 61 6e 61 6c 79 73  ound, the analys
2059a 69 73 20 69 73 20 63 75 74 20 73 68 6f 72 74 2e  is is cut short.
2059b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2059c 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
2059d 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73  xprAnalyzeAggLis
2059e 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70  t(NameContext *p
2059f 4e 43 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  NC, ExprList *pL
205a0 69 73 74 29 7b 0a 20 20 73 74 72 75 63 74 20 45  ist){.  struct E
205a1 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
205a2 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  tem;.  int i;.  
205a3 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20  if( pList ){.   
205a4 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74   for(pItem=pList
205a5 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73  ->a, i=0; i<pLis
205a6 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  t->nExpr; i++, p
205a7 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 73  Item++){.      s
205a8 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
205a9 65 41 67 67 72 65 67 61 74 65 73 28 70 4e 43 2c  eAggregates(pNC,
205aa 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a   pItem->pExpr);.
205ab 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
205ac 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 73 69  ** Allocate a si
205ad 6e 67 6c 65 20 6e 65 77 20 72 65 67 69 73 74 65  ngle new registe
205ae 72 20 66 6f 72 20 75 73 65 20 74 6f 20 68 6f 6c  r for use to hol
205af 64 20 73 6f 6d 65 20 69 6e 74 65 72 6d 65 64 69  d some intermedi
205b0 61 74 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 53  ate result..*/.S
205b1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
205b2 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  t sqlite3GetTemp
205b3 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72 73  Reg(Parse *pPars
205b4 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65  e){.  if( pParse
205b5 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30 20 29 7b  ->nTempReg==0 ){
205b6 0a 20 20 20 20 72 65 74 75 72 6e 20 2b 2b 70 50  .    return ++pP
205b7 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a  arse->nMem;.  }.
205b8 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 2d    return pParse-
205b9 3e 61 54 65 6d 70 52 65 67 5b 2d 2d 70 50 61 72  >aTempReg[--pPar
205ba 73 65 2d 3e 6e 54 65 6d 70 52 65 67 5d 3b 0a 7d  se->nTempReg];.}
205bb 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61  ../*.** Dealloca
205bc 74 65 20 61 20 72 65 67 69 73 74 65 72 2c 20 6d  te a register, m
205bd 61 6b 69 6e 67 20 61 76 61 69 6c 61 62 6c 65 20  aking available 
205be 66 6f 72 20 72 65 75 73 65 20 66 6f 72 20 73 6f  for reuse for so
205bf 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70  me other.** purp
205c0 6f 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ose..**.** If a 
205c1 72 65 67 69 73 74 65 72 20 69 73 20 63 75 72 72  register is curr
205c2 65 6e 74 6c 79 20 62 65 69 6e 67 20 75 73 65 64  ently being used
205c3 20 62 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63   by the column c
205c4 61 63 68 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  ache, then.** th
205c5 65 20 64 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  e dallocation is
205c6 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20   deferred until 
205c7 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  the column cache
205c8 20 6c 69 6e 65 20 74 68 61 74 20 75 73 65 73 0a   line that uses.
205c9 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20  ** the register 
205ca 62 65 63 6f 6d 65 73 20 73 74 61 6c 65 2e 0a 2a  becomes stale..*
205cb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
205cc 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c   void sqlite3Rel
205cd 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72 73  easeTempReg(Pars
205ce 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
205cf 52 65 67 29 7b 0a 20 20 69 66 28 20 69 52 65 67  Reg){.  if( iReg
205d0 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d   && pParse->nTem
205d1 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70  pReg<ArraySize(p
205d2 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29  Parse->aTempReg)
205d3 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
205d4 20 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61     struct yColCa
205d5 63 68 65 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28  che *p;.    for(
205d6 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
205d7 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
205d8 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
205d9 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20  ++, p++){.      
205da 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65  if( p->iReg==iRe
205db 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  g ){.        p->
205dc 74 65 6d 70 52 65 67 20 3d 20 31 3b 0a 20 20 20  tempReg = 1;.   
205dd 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
205de 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
205df 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b  Parse->aTempReg[
205e0 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67  pParse->nTempReg
205e1 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a  ++] = iReg;.  }.
205e2 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
205e3 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 20  e or deallocate 
205e4 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52 65 67 20  a block of nReg 
205e5 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69  consecutive regi
205e6 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sters.*/.SQLITE_
205e7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
205e8 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
205e9 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
205ea 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt nReg){.  int 
205eb 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70 50 61 72  i, n;.  i = pPar
205ec 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0a 20  se->iRangeReg;. 
205ed 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 52 61   n = pParse->nRa
205ee 6e 67 65 52 65 67 3b 0a 20 20 69 66 28 20 6e 52  ngeReg;.  if( nR
205ef 65 67 3c 3d 6e 20 26 26 20 21 75 73 65 64 41 73  eg<=n && !usedAs
205f0 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72  ColumnCache(pPar
205f1 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29 7b  se, i, i+n-1) ){
205f2 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52 61  .    pParse->iRa
205f3 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b 0a  ngeReg += nReg;.
205f4 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e      pParse->nRan
205f5 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b 0a 20  geReg -= nReg;. 
205f6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 20 3d 20   }else{.    i = 
205f7 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a  pParse->nMem+1;.
205f8 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
205f9 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a 20 20   += nReg;.  }.  
205fa 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49  return i;.}.SQLI
205fb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
205fc 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
205fd 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70  mpRange(Parse *p
205fe 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c  Parse, int iReg,
205ff 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 66   int nReg){.  if
20600 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d 3e 6e  ( nReg>pParse->n
20601 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20 20 20  RangeReg ){.    
20602 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65  pParse->nRangeRe
20603 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70 50  g = nReg;.    pP
20604 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 20  arse->iRangeReg 
20605 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f  = iReg;.  }.}../
20606 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
20607 6e 64 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a 2a  nd of expr.c ***
20608 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2060a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2060b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
2060c 65 67 69 6e 20 66 69 6c 65 20 61 6c 74 65 72 2e  egin file alter.
2060d 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2060e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2060f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
20610 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75 61  *.** 2005 Februa
20611 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ry 15.**.** The 
20612 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
20613 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
20614 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
20615 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
20616 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
20617 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
20618 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
20619 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2061a 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2061b 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2061c 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2061d 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2061e 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2061f 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
20620 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
20621 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
20622 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
20623 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20624 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20625 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20626 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20627 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
20628 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
20629 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  e routines that 
2062a 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  used to generate
2062b 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74 68   VDBE code.** th
2062c 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
2062d 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f  e ALTER TABLE co
2062e 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  mmand..**.** $Id
2062f 3a 20 61 6c 74 65 72 2e 63 2c 76 20 31 2e 35 37  : alter.c,v 1.57
20630 20 32 30 30 39 2f 30 34 2f 31 36 20 31 36 3a 33   2009/04/16 16:3
20631 30 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a  0:18 drh Exp $.*
20632 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  /../*.** The cod
20633 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f  e in this file o
20634 6e 6c 79 20 65 78 69 73 74 73 20 69 66 20 77 65  nly exists if we
20635 20 61 72 65 20 6e 6f 74 20 6f 6d 69 74 74 69 6e   are not omittin
20636 67 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54  g the.** ALTER T
20637 41 42 4c 45 20 6c 6f 67 69 63 20 66 72 6f 6d 20  ABLE logic from 
20638 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69  the build..*/.#i
20639 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2063a 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 0a 0a 2f  T_ALTERTABLE.../
2063b 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2063c 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 53 51  on is used by SQ
2063d 4c 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 69  L generated to i
2063e 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 0a 2a 2a  mplement the .**
2063f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d   ALTER TABLE com
20640 6d 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74 20  mand. The first 
20641 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
20642 74 65 78 74 20 6f 66 20 61 20 43 52 45 41 54 45  text of a CREATE
20643 20 54 41 42 4c 45 20 6f 72 0a 2a 2a 20 43 52 45   TABLE or.** CRE
20644 41 54 45 20 49 4e 44 45 58 20 63 6f 6d 6d 61 6e  ATE INDEX comman
20645 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73  d. The second is
20646 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54   a table name. T
20647 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e  he table name in
20648 20 0a 2a 2a 20 74 68 65 20 43 52 45 41 54 45 20   .** the CREATE 
20649 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20  TABLE or CREATE 
2064a 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20  INDEX statement 
2064b 69 73 20 72 65 70 6c 61 63 65 64 20 77 69 74 68  is replaced with
2064c 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 72   the third.** ar
2064d 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72  gument and the r
2064e 65 73 75 6c 74 20 72 65 74 75 72 6e 65 64 2e 20  esult returned. 
2064f 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  Examples:.**.** 
20650 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61  sqlite_rename_ta
20651 62 6c 65 28 27 43 52 45 41 54 45 20 54 41 42 4c  ble('CREATE TABL
20652 45 20 61 62 63 28 61 2c 20 62 2c 20 63 29 27 2c  E abc(a, b, c)',
20653 20 27 64 65 66 27 29 0a 2a 2a 20 20 20 20 20 2d   'def').**     -
20654 3e 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 20  > 'CREATE TABLE 
20655 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2a  def(a, b, c)'.**
20656 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d  .** sqlite_renam
20657 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45 20  e_table('CREATE 
20658 49 4e 44 45 58 20 69 20 4f 4e 20 61 62 63 28 61  INDEX i ON abc(a
20659 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20 20  )', 'def').**   
2065a 20 20 2d 3e 20 27 43 52 45 41 54 45 20 49 4e 44    -> 'CREATE IND
2065b 45 58 20 69 20 4f 4e 20 64 65 66 28 61 2c 20 62  EX i ON def(a, b
2065c 2c 20 63 29 27 0a 2a 2f 0a 73 74 61 74 69 63 20  , c)'.*/.static 
2065d 76 6f 69 64 20 72 65 6e 61 6d 65 54 61 62 6c 65  void renameTable
2065e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
2065f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
20660 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
20661 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
20662 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73   **argv.){.  uns
20663 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74  igned char const
20664 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33   *zSql = sqlite3
20665 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
20666 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64  [0]);.  unsigned
20667 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61   char const *zTa
20668 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  bleName = sqlite
20669 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
2066a 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f  v[1]);..  int to
2066b 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61  ken;.  Token tna
2066c 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  me;.  unsigned c
2066d 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20  har const *zCsr 
2066e 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65  = zSql;.  int le
2066f 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a  n = 0;.  char *z
20670 52 65 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 20  Ret;..  sqlite3 
20671 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
20672 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
20673 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55  context);..  UNU
20674 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
20675 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68  tUsed);..  /* Th
20676 65 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64  e principle used
20677 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74   to locate the t
20678 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65  able name in the
20679 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 0a 20   CREATE TABLE . 
2067a 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73   ** statement is
2067b 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20   that the table 
2067c 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73  name is the firs
2067d 74 20 6e 6f 6e 2d 73 70 61 63 65 20 74 6f 6b 65  t non-space toke
2067e 6e 20 74 68 61 74 0a 20 20 2a 2a 20 69 73 20 69  n that.  ** is i
2067f 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f  mmediately follo
20680 77 65 64 20 62 79 20 61 20 54 4b 5f 4c 50 20 6f  wed by a TK_LP o
20681 72 20 54 4b 5f 55 53 49 4e 47 20 74 6f 6b 65 6e  r TK_USING token
20682 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53 71  ..  */.  if( zSq
20683 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20  l ){.    do {.  
20684 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72 20 29      if( !*zCsr )
20685 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 61 6e  {.        /* Ran
20686 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 62 65   out of input be
20687 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 61 6e 20  fore finding an 
20688 6f 70 65 6e 69 6e 67 20 62 72 61 63 6b 65 74 2e  opening bracket.
20689 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f   Return NULL. */
2068a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b  .        return;
2068b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
2068c 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b  /* Store the tok
2068d 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69  en that zCsr poi
2068e 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e  nts to in tname.
2068f 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e   */.      tname.
20690 7a 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 20  z = zCsr;.      
20691 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a  tname.n = len;..
20692 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65        /* Advance
20693 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78   zCsr to the nex
20694 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74  t token. Store t
20695 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69  hat token type i
20696 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20  n 'token',.     
20697 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67   ** and its leng
20698 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20  th in 'len' (to 
20699 62 65 20 75 73 65 64 20 6e 65 78 74 20 69 74 65  be used next ite
2069a 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c  ration of this l
2069b 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  oop)..      */. 
2069c 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20       do {.      
2069d 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20    zCsr += len;. 
2069e 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c         len = sql
2069f 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73  ite3GetToken(zCs
206a0 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20  r, &token);.    
206a1 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e    } while( token
206a2 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20  ==TK_SPACE );.  
206a3 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e      assert( len>
206a4 30 20 29 3b 0a 20 20 20 20 7d 20 77 68 69 6c 65  0 );.    } while
206a5 28 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20 26  ( token!=TK_LP &
206a6 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49 4e  & token!=TK_USIN
206a7 47 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74 20 3d  G );..    zRet =
206a8 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
206a9 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25  db, "%.*s\"%w\"%
206aa 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 7a 53  s", tname.z - zS
206ab 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20  ql, zSql, .     
206ac 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e    zTableName, tn
206ad 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a  ame.z+tname.n);.
206ae 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
206af 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
206b0 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54   zRet, -1, SQLIT
206b1 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a  E_DYNAMIC);.  }.
206b2 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
206b3 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f  E_OMIT_TRIGGER./
206b4 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
206b5 69 73 20 75 73 65 64 20 62 79 20 53 51 4c 20 67  is used by SQL g
206b6 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c  enerated to impl
206b7 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c 54  ement the.** ALT
206b8 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64  ER TABLE command
206b9 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  . The first argu
206ba 6d 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74  ment is the text
206bb 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49   of a CREATE TRI
206bc 47 47 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d 65  GGER .** stateme
206bd 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69  nt. The second i
206be 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  s a table name. 
206bf 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  The table name i
206c0 6e 20 74 68 65 20 43 52 45 41 54 45 20 0a 2a 2a  n the CREATE .**
206c1 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
206c2 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 77  nt is replaced w
206c3 69 74 68 20 74 68 65 20 74 68 69 72 64 20 61 72  ith the third ar
206c4 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72  gument and the r
206c5 65 73 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72 6e  esult .** return
206c6 65 64 2e 20 54 68 69 73 20 69 73 20 61 6e 61 6c  ed. This is anal
206c7 61 67 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65 54  agous to renameT
206c8 61 62 6c 65 46 75 6e 63 28 29 20 61 62 6f 76 65  ableFunc() above
206c9 2c 20 65 78 63 65 70 74 20 66 6f 72 20 43 52 45  , except for CRE
206ca 41 54 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c 20  ATE.** TRIGGER, 
206cb 6e 6f 74 20 43 52 45 41 54 45 20 49 4e 44 45 58  not CREATE INDEX
206cc 20 61 6e 64 20 43 52 45 41 54 45 20 54 41 42 4c   and CREATE TABL
206cd 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  E..*/.static voi
206ce 64 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46  d renameTriggerF
206cf 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
206d0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
206d1 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
206d2 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
206d3 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69  **argv.){.  unsi
206d4 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
206d5 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f  *zSql = sqlite3_
206d6 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
206d7 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  0]);.  unsigned 
206d8 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62  char const *zTab
206d9 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  leName = sqlite3
206da 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
206db 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b  [1]);..  int tok
206dc 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d  en;.  Token tnam
206dd 65 3b 0a 20 20 69 6e 74 20 64 69 73 74 20 3d 20  e;.  int dist = 
206de 33 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  3;.  unsigned ch
206df 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d  ar const *zCsr =
206e0 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e   zSql;.  int len
206e1 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52   = 0;.  char *zR
206e2 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  et;.  sqlite3 *d
206e3 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
206e4 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
206e5 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45  ntext);..  UNUSE
206e6 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
206e7 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  sed);..  /* The 
206e8 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 74  principle used t
206e9 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 62  o locate the tab
206ea 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43  le name in the C
206eb 52 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 20  REATE TRIGGER . 
206ec 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73   ** statement is
206ed 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20   that the table 
206ee 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73  name is the firs
206ef 74 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20  t token that is 
206f0 69 6d 6d 65 64 69 61 74 65 64 6c 79 0a 20 20 2a  immediatedly.  *
206f1 2a 20 70 72 65 63 65 64 65 64 20 62 79 20 65 69  * preceded by ei
206f2 74 68 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b  ther TK_ON or TK
206f3 5f 44 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69 61  _DOT and immedia
206f4 74 65 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62  tedly followed b
206f5 79 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54 4b  y one.  ** of TK
206f6 5f 57 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e 20  _WHEN, TK_BEGIN 
206f7 6f 72 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a  or TK_FOR..  */.
206f8 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20    if( zSql ){.  
206f9 20 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69 66    do {..      if
206fa 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20  ( !*zCsr ){.    
206fb 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f      /* Ran out o
206fc 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66  f input before f
206fd 69 6e 64 69 6e 67 20 74 68 65 20 74 61 62 6c 65  inding the table
206fe 20 6e 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e 55   name. Return NU
206ff 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  LL. */.        r
20700 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a  eturn;.      }..
20701 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74        /* Store t
20702 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43  he token that zC
20703 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20  sr points to in 
20704 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20  tname. */.      
20705 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72 3b 0a  tname.z = zCsr;.
20706 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d 20        tname.n = 
20707 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41  len;..      /* A
20708 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f 20 74  dvance zCsr to t
20709 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53  he next token. S
2070a 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 6e 20  tore that token 
2070b 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c  type in 'token',
2070c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 74  .      ** and it
2070d 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e  s length in 'len
2070e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65  ' (to be used ne
2070f 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  xt iteration of 
20710 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20  this loop)..    
20711 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a    */.      do {.
20712 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20          zCsr += 
20713 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e  len;.        len
20714 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b   = sqlite3GetTok
20715 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29  en(zCsr, &token)
20716 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20  ;.      }while( 
20717 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20  token==TK_SPACE 
20718 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
20719 20 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 20   len>0 );..     
2071a 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 27 64 69   /* Variable 'di
2071b 73 74 27 20 73 74 6f 72 65 73 20 74 68 65 20 6e  st' stores the n
2071c 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
2071d 72 65 61 64 20 73 69 6e 63 65 20 74 68 65 20 6d  read since the m
2071e 6f 73 74 0a 20 20 20 20 20 20 2a 2a 20 72 65 63  ost.      ** rec
2071f 65 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b  ent TK_DOT or TK
20720 5f 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  _ON. This means 
20721 74 68 61 74 20 77 68 65 6e 20 61 20 57 48 45 4e  that when a WHEN
20722 2c 20 46 4f 52 20 6f 72 20 42 45 47 49 4e 20 0a  , FOR or BEGIN .
20723 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69        ** token i
20724 73 20 72 65 61 64 20 61 6e 64 20 27 64 69 73 74  s read and 'dist
20725 27 20 65 71 75 61 6c 73 20 32 2c 20 74 68 65 20  ' equals 2, the 
20726 63 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 64  condition stated
20727 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20   above.      ** 
20728 74 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20 20 20  to be met..     
20729 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74   **.      ** Not
2072a 65 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f 74  e that ON cannot
2072b 20 62 65 20 61 20 64 61 74 61 62 61 73 65 2c 20   be a database, 
2072c 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20  table or column 
2072d 6e 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20 2a  name, so.      *
2072e 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  * there is no ne
2072f 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75  ed to worry abou
20730 74 20 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a 20  t syntax like . 
20731 20 20 20 20 20 2a 2a 20 22 43 52 45 41 54 45 20       ** "CREATE 
20732 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 4f  TRIGGER ... ON O
20733 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 20  N.ON BEGIN ..." 
20734 65 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  etc..      */.  
20735 20 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20 20      dist++;.    
20736 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f    if( token==TK_
20737 44 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 4b  DOT || token==TK
20738 5f 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 64  _ON ){.        d
20739 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ist = 0;.      }
2073a 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 64 69  .    } while( di
2073b 73 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e 21  st!=2 || (token!
2073c 3d 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b 65  =TK_WHEN && toke
2073d 6e 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f 6b  n!=TK_FOR && tok
2073e 65 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29 3b  en!=TK_BEGIN) );
2073f 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c  ..    /* Variabl
20740 65 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e 74  e tname now cont
20741 61 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20 74  ains the token t
20742 68 61 74 20 69 73 20 74 68 65 20 6f 6c 64 20 74  hat is the old t
20743 61 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a 2a  able-name.    **
20744 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54   in the CREATE T
20745 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
20746 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52 65  ..    */.    zRe
20747 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
20748 74 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77  tf(db, "%.*s\"%w
20749 5c 22 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d  \"%s", tname.z -
2074a 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20   zSql, zSql, .  
2074b 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c       zTableName,
2074c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e   tname.z+tname.n
2074d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
2074e 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
2074f 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51  xt, zRet, -1, SQ
20750 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
20751 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a   }.}.#endif   /*
20752 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52   !SQLITE_OMIT_TR
20753 49 47 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  IGGER */../*.** 
20754 52 65 67 69 73 74 65 72 20 62 75 69 6c 74 2d 69  Register built-i
20755 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64  n functions used
20756 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65   to help impleme
20757 6e 74 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a  nt ALTER TABLE.*
20758 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20759 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74   void sqlite3Alt
2075a 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  erFunctions(sqli
2075b 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69  te3 *db){.  sqli
2075c 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
2075d 2c 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65  , "sqlite_rename
2075e 5f 74 61 62 6c 65 22 2c 20 32 2c 20 53 51 4c 49  _table", 2, SQLI
2075f 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20  TE_UTF8, 0,.    
20760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20761 20 20 20 20 20 72 65 6e 61 6d 65 54 61 62 6c 65       renameTable
20762 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 69 66  Func, 0, 0);.#if
20763 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
20764 5f 54 52 49 47 47 45 52 0a 20 20 73 71 6c 69 74  _TRIGGER.  sqlit
20765 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
20766 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f   "sqlite_rename_
20767 74 72 69 67 67 65 72 22 2c 20 32 2c 20 53 51 4c  trigger", 2, SQL
20768 49 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20  ITE_UTF8, 0,.   
20769 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2076a 20 20 20 20 20 20 72 65 6e 61 6d 65 54 72 69 67        renameTrig
2076b 67 65 72 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a  gerFunc, 0, 0);.
2076c 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
2076d 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78  Generate the tex
2076e 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78 70  t of a WHERE exp
2076f 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63 61  ression which ca
20770 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c  n be used to sel
20771 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 6f  ect all.** tempo
20772 72 61 72 79 20 74 72 69 67 67 65 72 73 20 6f 6e  rary triggers on
20773 20 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f 6d   table pTab from
20774 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70   the sqlite_temp
20775 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49  _master table. I
20776 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20  f.** table pTab 
20777 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 79  has no temporary
20778 20 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69 73   triggers, or is
20779 20 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20 69   itself stored i
2077a 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f 72  n the .** tempor
2077b 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e 55  ary database, NU
2077c 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  LL is returned..
2077d 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
2077e 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72  whereTempTrigger
2077f 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  s(Parse *pParse,
20780 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
20781 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b   Trigger *pTrig;
20782 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20  .  char *zWhere 
20783 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 74 6d 70  = 0;.  char *tmp
20784 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63   = 0;.  const Sc
20785 68 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 6d  hema *pTempSchem
20786 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  a = pParse->db->
20787 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20  aDb[1].pSchema; 
20788 2f 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65 6d  /* Temp db schem
20789 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68  a */..  /* If th
2078a 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c  e table is not l
2078b 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 74 65  ocated in the te
2078c 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68 20  mp-db (in which 
2078d 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20  case NULL is .  
2078e 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f  ** returned, loo
2078f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61  p through the ta
20790 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72 69  bles list of tri
20791 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68 20  ggers. For each 
20792 74 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68 61  trigger.  ** tha
20793 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
20794 20 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63 68   the temp-db sch
20795 65 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75 73  ema, add a claus
20796 65 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 0a  e to the WHERE .
20797 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
20798 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 69  being built up i
20799 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20  n zWhere..  */. 
2079a 20 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65   if( pTab->pSche
2079b 6d 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61 20  ma!=pTempSchema 
2079c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
2079d 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2079e 0a 20 20 20 20 66 6f 72 28 70 54 72 69 67 3d 73  .    for(pTrig=s
2079f 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73  qlite3TriggerLis
207a0 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  t(pParse, pTab);
207a1 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54   pTrig; pTrig=pT
207a2 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  rig->pNext){.   
207a3 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 53     if( pTrig->pS
207a4 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65  chema==pTempSche
207a5 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ma ){.        if
207a6 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20 20  ( !zWhere ){.   
207a7 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20         zWhere = 
207a8 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
207a9 62 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 70 54  b, "name=%Q", pT
207aa 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20  rig->name);.    
207ab 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
207ac 20 20 20 20 20 74 6d 70 20 3d 20 7a 57 68 65 72       tmp = zWher
207ad 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 57 68  e;.          zWh
207ae 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ere = sqlite3MPr
207af 69 6e 74 66 28 64 62 2c 20 22 25 73 20 4f 52 20  intf(db, "%s OR 
207b0 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65 72 65  name=%Q", zWhere
207b1 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a  , pTrig->name);.
207b2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
207b3 33 44 62 46 72 65 65 28 64 62 2c 20 74 6d 70 29  3DbFree(db, tmp)
207b4 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
207b5 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
207b6 72 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a 7d  return zWhere;.}
207b7 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
207b8 20 63 6f 64 65 20 74 6f 20 64 72 6f 70 20 61 6e   code to drop an
207b9 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e 74  d reload the int
207ba 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
207bb 74 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a 2a 2a  tion of table.**
207bc 20 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20 64   pTab from the d
207bd 61 74 61 62 61 73 65 2c 20 69 6e 63 6c 75 64 69  atabase, includi
207be 6e 67 20 74 72 69 67 67 65 72 73 20 61 6e 64 20  ng triggers and 
207bf 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65  temporary trigge
207c0 72 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  rs..** Argument 
207c1 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  zName is the nam
207c2 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69  e of the table i
207c3 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
207c4 63 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68 65 20  chema at.** the 
207c5 74 69 6d 65 20 74 68 65 20 67 65 6e 65 72 61 74  time the generat
207c6 65 64 20 63 6f 64 65 20 69 73 20 65 78 65 63 75  ed code is execu
207c7 74 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65  ted. This can be
207c8 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a   different from.
207c9 2a 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 69  ** pTab->zName i
207ca 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  f this function 
207cb 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20  is being called 
207cc 74 6f 20 63 6f 64 65 20 70 61 72 74 20 6f 66 20  to code part of 
207cd 61 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20 54 41  an .** "ALTER TA
207ce 42 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22 20 73  BLE RENAME TO" s
207cf 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61  tatement..*/.sta
207d0 74 69 63 20 76 6f 69 64 20 72 65 6c 6f 61 64 54  tic void reloadT
207d1 61 62 6c 65 53 63 68 65 6d 61 28 50 61 72 73 65  ableSchema(Parse
207d2 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20   *pParse, Table 
207d3 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61  *pTab, const cha
207d4 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62  r *zName){.  Vdb
207d5 65 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a 7a 57  e *v;.  char *zW
207d6 68 65 72 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b  here;.  int iDb;
207d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
207d8 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64     /* Index of d
207d9 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
207da 6e 67 20 70 54 61 62 20 2a 2f 0a 23 69 66 6e 64  ng pTab */.#ifnd
207db 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
207dc 52 49 47 47 45 52 0a 20 20 54 72 69 67 67 65 72  RIGGER.  Trigger
207dd 20 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69 66 0a   *pTrig;.#endif.
207de 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
207df 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
207e0 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29   if( NEVER(v==0)
207e1 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
207e2 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
207e3 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
207e4 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a  (pParse->db) );.
207e5 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
207e6 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
207e7 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
207e8 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72  Schema);.  asser
207e9 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 23 69  t( iDb>=0 );..#i
207ea 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
207eb 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 44  T_TRIGGER.  /* D
207ec 72 6f 70 20 61 6e 79 20 74 61 62 6c 65 20 74 72  rop any table tr
207ed 69 67 67 65 72 73 20 66 72 6f 6d 20 74 68 65 20  iggers from the 
207ee 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e  internal schema.
207ef 20 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69 67 3d   */.  for(pTrig=
207f0 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69  sqlite3TriggerLi
207f1 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  st(pParse, pTab)
207f2 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70  ; pTrig; pTrig=p
207f3 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  Trig->pNext){.  
207f4 20 20 69 6e 74 20 69 54 72 69 67 44 62 20 3d 20    int iTrigDb = 
207f5 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
207f6 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
207f7 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29   pTrig->pSchema)
207f8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 54  ;.    assert( iT
207f9 72 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54  rigDb==iDb || iT
207fa 72 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20  rigDb==1 );.    
207fb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
207fc 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67  4(v, OP_DropTrig
207fd 67 65 72 2c 20 69 54 72 69 67 44 62 2c 20 30 2c  ger, iTrigDb, 0,
207fe 20 30 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 2c   0, pTrig->name,
207ff 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a   0);.  }.#endif.
20800 0a 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74  .  /* Drop the t
20801 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 66  able and index f
20802 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  rom the internal
20803 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c   schema */.  sql
20804 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
20805 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20  , OP_DropTable, 
20806 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d  iDb, 0, 0, pTab-
20807 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 20 20 2f  >zName, 0);..  /
20808 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 74 61 62  * Reload the tab
20809 6c 65 2c 20 69 6e 64 65 78 20 61 6e 64 20 70 65  le, index and pe
2080a 72 6d 61 6e 65 6e 74 20 74 72 69 67 67 65 72 20  rmanent trigger 
2080b 73 63 68 65 6d 61 73 2e 20 2a 2f 0a 20 20 7a 57  schemas. */.  zW
2080c 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  here = sqlite3MP
2080d 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62  rintf(pParse->db
2080e 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 25 51 22 2c  , "tbl_name=%Q",
2080f 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21   zName);.  if( !
20810 7a 57 68 65 72 65 20 29 20 72 65 74 75 72 6e 3b  zWhere ) return;
20811 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
20812 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65  dOp4(v, OP_Parse
20813 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20  Schema, iDb, 0, 
20814 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59  0, zWhere, P4_DY
20815 4e 41 4d 49 43 29 3b 0a 0a 23 69 66 6e 64 65 66  NAMIC);..#ifndef
20816 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
20817 47 47 45 52 0a 20 20 2f 2a 20 4e 6f 77 2c 20 69  GGER.  /* Now, i
20818 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e  f the table is n
20819 6f 74 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ot stored in the
2081a 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2c 20   temp database, 
2081b 72 65 6c 6f 61 64 20 61 6e 79 20 74 65 6d 70 20  reload any temp 
2081c 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20  .  ** triggers. 
2081d 44 6f 6e 27 74 20 75 73 65 20 49 4e 28 2e 2e 2e  Don't use IN(...
2081e 29 20 69 6e 20 63 61 73 65 20 53 51 4c 49 54 45  ) in case SQLITE
2081f 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 69  _OMIT_SUBQUERY i
20820 73 20 64 65 66 69 6e 65 64 2e 20 0a 20 20 2a 2f  s defined. .  */
20821 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77  .  if( (zWhere=w
20822 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73  hereTempTriggers
20823 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21  (pParse, pTab))!
20824 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
20825 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
20826 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 31  P_ParseSchema, 1
20827 2c 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20  , 0, 0, zWhere, 
20828 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d  P4_DYNAMIC);.  }
20829 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
2082a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2082b 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
2082c 22 41 4c 54 45 52 20 54 41 42 4c 45 20 78 78 78  "ALTER TABLE xxx
2082d 20 52 45 4e 41 4d 45 20 54 4f 20 79 79 79 22 20   RENAME TO yyy" 
2082e 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f  .** command. .*/
2082f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20830 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
20831 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 0a 20 20  rRenameTable(.  
20832 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
20833 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
20834 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a  ser context. */.
20835 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
20836 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
20837 68 65 20 74 61 62 6c 65 20 74 6f 20 72 65 6e 61  he table to rena
20838 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  me. */.  Token *
20839 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20  pName           
2083a 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 61     /* The new ta
2083b 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 29 7b 0a  ble name. */.){.
2083c 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
2083d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2083e 61 74 61 62 61 73 65 20 74 68 61 74 20 63 6f 6e  atabase that con
2083f 74 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65 20  tains the table 
20840 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20  */.  char *zDb; 
20841 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
20842 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
20843 73 65 20 69 44 62 20 2a 2f 0a 20 20 54 61 62 6c  se iDb */.  Tabl
20844 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
20845 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62        /* Table b
20846 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a  eing renamed */.
20847 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
20848 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  0;          /* N
20849 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 20 76  ULL-terminated v
2084a 65 72 73 69 6f 6e 20 6f 66 20 70 4e 61 6d 65 20  ersion of pName 
2084b 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  */ .  sqlite3 *d
2084c 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2084d 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
2084e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ection */.  int 
2084f 6e 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 20 20  nTabName;       
20850 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
20851 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  of UTF-8 charact
20852 65 72 73 20 69 6e 20 7a 54 61 62 4e 61 6d 65 20  ers in zTabName 
20853 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
20854 2a 7a 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f  *zTabName;     /
20855 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20  * Original name 
20856 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
20857 20 20 56 64 62 65 20 2a 76 3b 0a 23 69 66 6e 64    Vdbe *v;.#ifnd
20858 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
20859 52 49 47 47 45 52 0a 20 20 63 68 61 72 20 2a 7a  RIGGER.  char *z
2085a 57 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20 20  Where = 0;      
2085b 20 20 20 2f 2a 20 57 68 65 72 65 20 63 6c 61 75     /* Where clau
2085c 73 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 65 6d  se to locate tem
2085d 70 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 65  p triggers */.#e
2085e 6e 64 69 66 0a 20 20 69 6e 74 20 69 73 56 69 72  ndif.  int isVir
2085f 74 75 61 6c 52 65 6e 61 6d 65 20 3d 20 30 3b 20  tualRename = 0; 
20860 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
20861 20 69 73 20 61 20 76 2d 74 61 62 6c 65 20 77 69   is a v-table wi
20862 74 68 20 61 6e 20 78 52 65 6e 61 6d 65 28 29 20  th an xRename() 
20863 2a 2f 0a 20 20 0a 20 20 69 66 28 20 4e 45 56 45  */.  .  if( NEVE
20864 52 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  R(db->mallocFail
20865 65 64 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  ed) ) goto exit_
20866 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
20867 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 6e 53  assert( pSrc->nS
20868 72 63 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72  rc==1 );.  asser
20869 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
2086a 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70  oldsAllMutexes(p
2086b 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 0a 20  Parse->db) );.. 
2086c 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
2086d 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
2086e 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b 30 5d  e, 0, pSrc->a[0]
2086f 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b  .zName, pSrc->a[
20870 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
20871 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74   if( !pTab ) got
20872 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
20873 62 6c 65 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  ble;.  iDb = sql
20874 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
20875 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
20876 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
20877 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
20878 62 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20  b].zName;..  /* 
20879 47 65 74 20 61 20 4e 55 4c 4c 20 74 65 72 6d 69  Get a NULL termi
2087a 6e 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  nated version of
2087b 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   the new table n
2087c 61 6d 65 2e 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20  ame. */.  zName 
2087d 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
2087e 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65  mToken(db, pName
2087f 29 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20  );.  if( !zName 
20880 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61  ) goto exit_rena
20881 6d 65 5f 74 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20  me_table;..  /* 
20882 43 68 65 63 6b 20 74 68 61 74 20 61 20 74 61 62  Check that a tab
20883 6c 65 20 6f 72 20 69 6e 64 65 78 20 6e 61 6d 65  le or index name
20884 64 20 27 7a 4e 61 6d 65 27 20 64 6f 65 73 20 6e  d 'zName' does n
20885 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
20886 0a 20 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73  .  ** in databas
20887 65 20 69 44 62 2e 20 49 66 20 73 6f 2c 20 74 68  e iDb. If so, th
20888 69 73 20 69 73 20 61 6e 20 65 72 72 6f 72 2e 0a  is is an error..
20889 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
2088a 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
2088b 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 7c 7c 20 73  zName, zDb) || s
2088c 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
2088d 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 20  db, zName, zDb) 
2088e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2088f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
20890 20 20 20 20 20 20 20 20 22 74 68 65 72 65 20 69          "there i
20891 73 20 61 6c 72 65 61 64 79 20 61 6e 6f 74 68 65  s already anothe
20892 72 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  r table or index
20893 20 77 69 74 68 20 74 68 69 73 20 6e 61 6d 65 3a   with this name:
20894 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
20895 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61    goto exit_rena
20896 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20  me_table;.  }.. 
20897 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 74   /* Make sure it
20898 20 69 73 20 6e 6f 74 20 61 20 73 79 73 74 65 6d   is not a system
20899 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74   table being alt
2089a 65 72 65 64 2c 20 6f 72 20 61 20 72 65 73 65 72  ered, or a reser
2089b 76 65 64 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 68  ved name.  ** th
2089c 61 74 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  at the table is 
2089d 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 74 6f  being renamed to
2089e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
2089f 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61  ite3Strlen30(pTa
208a0 62 2d 3e 7a 4e 61 6d 65 29 3e 36 20 0a 20 20 20  b->zName)>6 .   
208a1 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  && 0==sqlite3Str
208a2 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d  NICmp(pTab->zNam
208a3 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29  e, "sqlite_", 7)
208a4 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  .  ){.    sqlite
208a5 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
208a6 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20  , "table %s may 
208a7 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c  not be altered",
208a8 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
208a9 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e     goto exit_ren
208aa 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20  ame_table;.  }. 
208ab 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
208ac 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65  sqlite3CheckObje
208ad 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a  ctName(pParse, z
208ae 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  Name) ){.    got
208af 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
208b0 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  ble;.  }..#ifnde
208b1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
208b2 45 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  EW.  if( pTab->p
208b3 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71  Select ){.    sq
208b4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
208b5 61 72 73 65 2c 20 22 76 69 65 77 20 25 73 20 6d  arse, "view %s m
208b6 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 72 65  ay not be altere
208b7 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  d", pTab->zName)
208b8 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
208b9 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
208ba 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
208bb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
208bc 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a  THORIZATION.  /*
208bd 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68   Invoke the auth
208be 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61  orization callba
208bf 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ck. */.  if( sql
208c0 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
208c1 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54  arse, SQLITE_ALT
208c2 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70  ER_TABLE, zDb, p
208c3 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29  Tab->zName, 0) )
208c4 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
208c5 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
208c6 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
208c7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
208c8 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
208c9 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
208ca 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
208cb 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20  e, pTab) ){.    
208cc 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
208cd 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66  _table;.  }.  if
208ce 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
208cf 29 20 26 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d  ) && pTab->pMod-
208d0 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d  >pModule->xRenam
208d1 65 20 29 7b 0a 20 20 20 20 69 73 56 69 72 74 75  e ){.    isVirtu
208d2 61 6c 52 65 6e 61 6d 65 20 3d 20 31 3b 0a 20 20  alRename = 1;.  
208d3 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42  }.#endif..  /* B
208d4 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  egin a transacti
208d5 6f 6e 20 61 6e 64 20 63 6f 64 65 20 74 68 65 20  on and code the 
208d6 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72  VerifyCookie for
208d7 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a   database iDb. .
208d8 20 20 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79    ** Then modify
208d9 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
208da 69 65 20 28 73 69 6e 63 65 20 74 68 65 20 41 4c  ie (since the AL
208db 54 45 52 20 54 41 42 4c 45 20 6d 6f 64 69 66 69  TER TABLE modifi
208dc 65 73 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65  es the.  ** sche
208dd 6d 61 29 2e 20 4f 70 65 6e 20 61 20 73 74 61 74  ma). Open a stat
208de 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
208df 6e 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  n if the table i
208e0 73 20 61 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a  s a virtual.  **
208e1 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76   table..  */.  v
208e2 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
208e3 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
208e4 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74   v==0 ){.    got
208e5 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
208e6 62 6c 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ble;.  }.  sqlit
208e7 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
208e8 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 69 73  ation(pParse, is
208e9 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 2c 20 69  VirtualRename, i
208ea 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 68  Db);.  sqlite3Ch
208eb 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
208ec 65 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49  e, iDb);..  /* I
208ed 66 20 74 68 69 73 20 69 73 20 61 20 76 69 72 74  f this is a virt
208ee 75 61 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b  ual table, invok
208ef 65 20 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20  e the xRename() 
208f0 66 75 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a  function if.  **
208f1 20 6f 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e   one is defined.
208f2 20 54 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63   The xRename() c
208f3 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64  allback will mod
208f4 69 66 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20  ify the names.  
208f5 2a 2a 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72  ** of any resour
208f6 63 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  ces used by the 
208f7 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  v-table implemen
208f8 74 61 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e  tation (includin
208f9 67 20 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c  g other.  ** SQL
208fa 69 74 65 20 74 61 62 6c 65 73 29 20 74 68 61 74  ite tables) that
208fb 20 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20   are identified 
208fc 62 79 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  by the name of t
208fd 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
208fe 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
208ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
20900 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 69 73  ALTABLE.  if( is
20901 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 20 29 7b  VirtualRename ){
20902 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 2b 2b 70  .    int i = ++p
20903 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
20904 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20905 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
20906 2c 20 30 2c 20 69 2c 20 30 2c 20 7a 4e 61 6d 65  , 0, i, 0, zName
20907 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
20908 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
20909 50 5f 56 52 65 6e 61 6d 65 2c 20 69 2c 20 30 2c  P_VRename, i, 0,
2090a 20 30 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   0,(const char*)
2090b 70 54 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f  pTab->pVtab, P4_
2090c 56 54 41 42 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  VTAB);.  }.#endi
2090d 66 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f  f..  /* figure o
2090e 75 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d  ut how many UTF-
2090f 38 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  8 characters are
20910 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a   in zName */.  z
20911 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e  TabName = pTab->
20912 7a 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d  zName;.  nTabNam
20913 65 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43  e = sqlite3Utf8C
20914 68 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c  harLen(zTabName,
20915 20 2d 31 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 64 69   -1);..  /* Modi
20916 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  fy the sqlite_ma
20917 73 74 65 72 20 74 61 62 6c 65 20 74 6f 20 75 73  ster table to us
20918 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  e the new table 
20919 6e 61 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  name. */.  sqlit
2091a 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
2091b 61 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44  arse,.      "UPD
2091c 41 54 45 20 25 51 2e 25 73 20 53 45 54 20 22 0a  ATE %Q.%s SET ".
2091d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
2091e 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20  IT_TRIGGER.     
2091f 20 20 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69       "sql = sqli
20920 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28  te_rename_table(
20921 73 71 6c 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73  sql, %Q), ".#els
20922 65 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c  e.          "sql
20923 20 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20 20   = CASE ".      
20924 20 20 20 20 20 20 22 57 48 45 4e 20 74 79 70 65        "WHEN type
20925 20 3d 20 27 74 72 69 67 67 65 72 27 20 54 48 45   = 'trigger' THE
20926 4e 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f  N sqlite_rename_
20927 74 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29  trigger(sql, %Q)
20928 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45  ".            "E
20929 4c 53 45 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d  LSE sqlite_renam
2092a 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29  e_table(sql, %Q)
2092b 20 45 4e 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20   END, ".#endif. 
2092c 20 20 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61           "tbl_na
2092d 6d 65 20 3d 20 25 51 2c 20 22 0a 20 20 20 20 20  me = %Q, ".     
2092e 20 20 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53       "name = CAS
2092f 45 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  E ".            
20930 22 57 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c  "WHEN type='tabl
20931 65 27 20 54 48 45 4e 20 25 51 20 22 0a 20 20 20  e' THEN %Q ".   
20932 20 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e           "WHEN n
20933 61 6d 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65  ame LIKE 'sqlite
20934 5f 61 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e  _autoindex%%' AN
20935 44 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54  D type='index' T
20936 48 45 4e 20 22 0a 20 20 20 20 20 20 20 20 20 20  HEN ".          
20937 20 20 20 22 27 73 71 6c 69 74 65 5f 61 75 74 6f     "'sqlite_auto
20938 69 6e 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c  index_' || %Q ||
20939 20 73 75 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b   substr(name,%d+
2093a 31 38 29 20 22 0a 20 20 20 20 20 20 20 20 20 20  18) ".          
2093b 20 20 22 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44    "ELSE name END
2093c 20 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20   ".      "WHERE 
2093d 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20  tbl_name=%Q AND 
2093e 22 0a 20 20 20 20 20 20 20 20 20 20 22 28 74 79  ".          "(ty
2093f 70 65 3d 27 74 61 62 6c 65 27 20 4f 52 20 74 79  pe='table' OR ty
20940 70 65 3d 27 69 6e 64 65 78 27 20 4f 52 20 74 79  pe='index' OR ty
20941 70 65 3d 27 74 72 69 67 67 65 72 27 29 3b 22 2c  pe='trigger');",
20942 20 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48   .      zDb, SCH
20943 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20  EMA_TABLE(iDb), 
20944 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e  zName, zName, zN
20945 61 6d 65 2c 20 0a 23 69 66 6e 64 65 66 20 53 51  ame, .#ifndef SQ
20946 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
20947 52 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23  R.      zName,.#
20948 65 6e 64 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d  endif.      zNam
20949 65 2c 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61  e, nTabName, zTa
2094a 62 4e 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e  bName.  );..#ifn
2094b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2094c 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
2094d 2f 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65  /* If the sqlite
2094e 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
2094f 65 78 69 73 74 73 20 69 6e 20 74 68 69 73 20 64  exists in this d
20950 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70  atabase, then up
20951 64 61 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69  date .  ** it wi
20952 74 68 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  th the new table
20953 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   name..  */.  if
20954 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  ( sqlite3FindTab
20955 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73  le(db, "sqlite_s
20956 65 71 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29  equence", zDb) )
20957 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  {.    sqlite3Nes
20958 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
20959 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45  .        "UPDATE
2095a 20 5c 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73   \"%w\".sqlite_s
2095b 65 71 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65  equence set name
2095c 20 3d 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65   = %Q WHERE name
2095d 20 3d 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20   = %Q",.        
2095e 7a 44 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62  zDb, zName, pTab
2095f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65  ->zName);.  }.#e
20960 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
20961 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
20962 52 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  R.  /* If there 
20963 61 72 65 20 54 45 4d 50 20 74 72 69 67 67 65 72  are TEMP trigger
20964 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c  s on this table,
20965 20 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69   modify the sqli
20966 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20  te_temp_master. 
20967 20 2a 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74   ** table. Don't
20968 20 64 6f 20 74 68 69 73 20 69 66 20 74 68 65 20   do this if the 
20969 74 61 62 6c 65 20 62 65 69 6e 67 20 41 4c 54 45  table being ALTE
2096a 52 65 64 20 69 73 20 69 74 73 65 6c 66 20 6c 6f  Red is itself lo
2096b 63 61 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68  cated in.  ** th
2096c 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e  e temp database.
2096d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68  .  */.  if( (zWh
2096e 65 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69  ere=whereTempTri
2096f 67 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54  ggers(pParse, pT
20970 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  ab))!=0 ){.    s
20971 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
20972 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  e(pParse, .     
20973 20 20 20 22 55 50 44 41 54 45 20 73 71 6c 69 74     "UPDATE sqlit
20974 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45  e_temp_master SE
20975 54 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  T ".            
20976 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65  "sql = sqlite_re
20977 6e 61 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c  name_trigger(sql
20978 2c 20 25 51 29 2c 20 22 0a 20 20 20 20 20 20 20  , %Q), ".       
20979 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d       "tbl_name =
2097a 20 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20   %Q ".          
2097b 20 20 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a    "WHERE %s;", z
2097c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68  Name, zName, zWh
2097d 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ere);.    sqlite
2097e 33 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65  3DbFree(db, zWhe
2097f 72 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  re);.  }.#endif.
20980 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72  .  /* Drop and r
20981 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e  eload the intern
20982 61 6c 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e  al table schema.
20983 20 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c   */.  reloadTabl
20984 65 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  eSchema(pParse, 
20985 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65  pTab, zName);..e
20986 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
20987 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
20988 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72  stDelete(db, pSr
20989 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  c);.  sqlite3DbF
2098a 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  ree(db, zName);.
2098b 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  }.../*.** This f
2098c 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
2098d 64 20 61 66 74 65 72 20 61 6e 20 22 41 4c 54 45  d after an "ALTE
2098e 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22  R TABLE ... ADD"
2098f 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68 61   statement.** ha
20990 73 20 62 65 65 6e 20 70 61 72 73 65 64 2e 20 41  s been parsed. A
20991 72 67 75 6d 65 6e 74 20 70 43 6f 6c 44 65 66 20  rgument pColDef 
20992 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78  contains the tex
20993 74 20 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a 20  t of the new.** 
20994 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f  column definitio
20995 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61 62  n..**.** The Tab
20996 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 50 61  le structure pPa
20997 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 77  rse->pNewTable w
20998 61 73 20 65 78 74 65 6e 64 65 64 20 74 6f 20 69  as extended to i
20999 6e 63 6c 75 64 65 0a 2a 2a 20 74 68 65 20 6e 65  nclude.** the ne
2099a 77 20 63 6f 6c 75 6d 6e 20 64 75 72 69 6e 67 20  w column during 
2099b 70 61 72 73 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  parsing..*/.SQLI
2099c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2099d 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69  sqlite3AlterFini
2099e 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73  shAddColumn(Pars
2099f 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
209a0 20 2a 70 43 6f 6c 44 65 66 29 7b 0a 20 20 54 61   *pColDef){.  Ta
209a1 62 6c 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20  ble *pNew;      
209a2 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
209a3 6f 66 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  of pParse->pNewT
209a4 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20  able */.  Table 
209a5 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20  *pTab;          
209a6 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69      /* Table bei
209a7 6e 67 20 61 6c 74 65 72 65 64 20 2a 2f 0a 20 20  ng altered */.  
209a8 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
209a9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
209aa 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  abase number */.
209ab 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
209ac 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  b;          /* D
209ad 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a  atabase name */.
209ae 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
209af 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ab;         /* T
209b0 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63  able name */.  c
209b1 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20  har *zCol;      
209b2 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 6c           /* Null
209b3 2d 74 65 72 6d 69 6e 61 74 65 64 20 63 6f 6c 75  -terminated colu
209b4 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f  mn definition */
209b5 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b  .  Column *pCol;
209b6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
209b7 54 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a  The new column *
209b8 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b  /.  Expr *pDflt;
209b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
209ba 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66   Default value f
209bb 6f 72 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d  or the new colum
209bc 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  n */.  sqlite3 *
209bd 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
209be 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
209bf 20 63 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0a   connection; */.
209c0 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
209c1 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  db;.  if( pParse
209c2 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
209c3 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
209c4 75 72 6e 3b 0a 20 20 70 4e 65 77 20 3d 20 70 50  urn;.  pNew = pP
209c5 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
209c6 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 20  .  assert( pNew 
209c7 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  );..  assert( sq
209c8 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
209c9 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b  llMutexes(db) );
209ca 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
209cb 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
209cc 2c 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 29  , pNew->pSchema)
209cd 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ;.  zDb = db->aD
209ce 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
209cf 7a 54 61 62 20 3d 20 26 70 4e 65 77 2d 3e 7a 4e  zTab = &pNew->zN
209d0 61 6d 65 5b 31 36 5d 3b 20 20 2f 2a 20 53 6b 69  ame[16];  /* Ski
209d1 70 20 74 68 65 20 22 73 71 6c 69 74 65 5f 61 6c  p the "sqlite_al
209d2 74 65 72 74 61 62 5f 22 20 70 72 65 66 69 78 20  tertab_" prefix 
209d3 6f 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20  on the name */. 
209d4 20 70 43 6f 6c 20 3d 20 26 70 4e 65 77 2d 3e 61   pCol = &pNew->a
209d5 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31  Col[pNew->nCol-1
209d6 5d 3b 0a 20 20 70 44 66 6c 74 20 3d 20 70 43 6f  ];.  pDflt = pCo
209d7 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20 70 54 61 62  l->pDflt;.  pTab
209d8 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
209d9 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 7a 44  ble(db, zTab, zD
209da 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54  b);.  assert( pT
209db 61 62 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ab );..#ifndef S
209dc 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
209dd 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e  RIZATION.  /* In
209de 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69  voke the authori
209df 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e  zation callback.
209e0 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
209e1 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
209e2 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f  e, SQLITE_ALTER_
209e3 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62  TABLE, zDb, pTab
209e4 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20  ->zName, 0) ){. 
209e5 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
209e6 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74  endif..  /* If t
209e7 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
209e8 20 66 6f 72 20 74 68 65 20 6e 65 77 20 63 6f 6c   for the new col
209e9 75 6d 6e 20 77 61 73 20 73 70 65 63 69 66 69 65  umn was specifie
209ea 64 20 77 69 74 68 20 61 20 0a 20 20 2a 2a 20 6c  d with a .  ** l
209eb 69 74 65 72 61 6c 20 4e 55 4c 4c 2c 20 74 68 65  iteral NULL, the
209ec 6e 20 73 65 74 20 70 44 66 6c 74 20 74 6f 20 30  n set pDflt to 0
209ed 2e 20 54 68 69 73 20 73 69 6d 70 6c 69 66 69 65  . This simplifie
209ee 73 20 63 68 65 63 6b 69 6e 67 0a 20 20 2a 2a 20  s checking.  ** 
209ef 66 6f 72 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20  for an SQL NULL 
209f0 64 65 66 61 75 6c 74 20 62 65 6c 6f 77 2e 0a 20  default below.. 
209f1 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 20   */.  if( pDflt 
209f2 26 26 20 70 44 66 6c 74 2d 3e 6f 70 3d 3d 54 4b  && pDflt->op==TK
209f3 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 44 66  _NULL ){.    pDf
209f4 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  lt = 0;.  }..  /
209f5 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
209f6 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 6e   new column is n
209f7 6f 74 20 73 70 65 63 69 66 69 65 64 20 61 73 20  ot specified as 
209f8 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55  PRIMARY KEY or U
209f9 4e 49 51 55 45 2e 0a 20 20 2a 2a 20 49 66 20 74  NIQUE..  ** If t
209fa 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20 4e 55  here is a NOT NU
209fb 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 74  LL constraint, t
209fc 68 65 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20  hen the default 
209fd 76 61 6c 75 65 20 66 6f 72 20 74 68 65 0a 20 20  value for the.  
209fe 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75 73 74 20 6e  ** column must n
209ff 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f  ot be NULL..  */
20a00 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 73 50  .  if( pCol->isP
20a01 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20 20 73 71  rimKey ){.    sq
20a02 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
20a03 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64  arse, "Cannot ad
20a04 64 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20  d a PRIMARY KEY 
20a05 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20 20 72 65  column");.    re
20a06 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  turn;.  }.  if( 
20a07 70 4e 65 77 2d 3e 70 49 6e 64 65 78 20 29 7b 0a  pNew->pIndex ){.
20a08 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
20a09 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e  Msg(pParse, "Can
20a0a 6e 6f 74 20 61 64 64 20 61 20 55 4e 49 51 55 45  not add a UNIQUE
20a0b 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20 20 72   column");.    r
20a0c 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  eturn;.  }.  if(
20a0d 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26   pCol->notNull &
20a0e 26 20 21 70 44 66 6c 74 20 29 7b 0a 20 20 20 20  & !pDflt ){.    
20a0f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
20a10 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
20a11 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 4e   "Cannot add a N
20a12 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 6d 6e 20 77  OT NULL column w
20a13 69 74 68 20 64 65 66 61 75 6c 74 20 76 61 6c 75  ith default valu
20a14 65 20 4e 55 4c 4c 22 29 3b 0a 20 20 20 20 72 65  e NULL");.    re
20a15 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  turn;.  }..  /* 
20a16 45 6e 73 75 72 65 20 74 68 65 20 64 65 66 61 75  Ensure the defau
20a17 6c 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  lt expression is
20a18 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20   something that 
20a19 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d  sqlite3ValueFrom
20a1a 45 78 70 72 28 29 0a 20 20 2a 2a 20 63 61 6e 20  Expr().  ** can 
20a1b 68 61 6e 64 6c 65 20 28 69 2e 65 2e 20 6e 6f 74  handle (i.e. not
20a1c 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 20 65 74   CURRENT_TIME et
20a1d 63 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  c.).  */.  if( p
20a1e 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Dflt ){.    sqli
20a1f 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b  te3_value *pVal;
20a20 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
20a21 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64 62  ValueFromExpr(db
20a22 2c 20 70 44 66 6c 74 2c 20 53 51 4c 49 54 45 5f  , pDflt, SQLITE_
20a23 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 41 46 46  UTF8, SQLITE_AFF
20a24 5f 4e 4f 4e 45 2c 20 26 70 56 61 6c 29 20 29 7b  _NONE, &pVal) ){
20a25 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
20a26 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
20a27 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
20a28 0a 20 20 20 20 69 66 28 20 21 70 56 61 6c 20 29  .    if( !pVal )
20a29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
20a2a 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
20a2b 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f  "Cannot add a co
20a2c 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 63 6f  lumn with non-co
20a2d 6e 73 74 61 6e 74 20 64 65 66 61 75 6c 74 22 29  nstant default")
20a2e 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
20a2f 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
20a30 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29  3ValueFree(pVal)
20a31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69  ;.  }..  /* Modi
20a32 66 79 20 74 68 65 20 43 52 45 41 54 45 20 54 41  fy the CREATE TA
20a33 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a  BLE statement. *
20a34 2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 71 6c 69 74  /.  zCol = sqlit
20a35 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
20a36 28 63 68 61 72 2a 29 70 43 6f 6c 44 65 66 2d 3e  (char*)pColDef->
20a37 7a 2c 20 70 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a  z, pColDef->n);.
20a38 20 20 69 66 28 20 7a 43 6f 6c 20 29 7b 0a 20 20    if( zCol ){.  
20a39 20 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 26    char *zEnd = &
20a3a 7a 43 6f 6c 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d  zCol[pColDef->n-
20a3b 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a  1];.    while( z
20a3c 45 6e 64 3e 7a 43 6f 6c 20 26 26 20 28 2a 7a 45  End>zCol && (*zE
20a3d 6e 64 3d 3d 27 3b 27 20 7c 7c 20 73 71 6c 69 74  nd==';' || sqlit
20a3e 65 33 49 73 73 70 61 63 65 28 2a 7a 45 6e 64 29  e3Isspace(*zEnd)
20a3f 29 20 29 7b 0a 20 20 20 20 20 20 2a 7a 45 6e 64  ) ){.      *zEnd
20a40 2d 2d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 7d  -- = '\0';.    }
20a41 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
20a42 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20  edParse(pParse, 
20a43 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45  .        "UPDATE
20a44 20 5c 22 25 77 5c 22 2e 25 73 20 53 45 54 20 22   \"%w\".%s SET "
20a45 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20  .          "sql 
20a46 3d 20 73 75 62 73 74 72 28 73 71 6c 2c 31 2c 25  = substr(sql,1,%
20a47 64 29 20 7c 7c 20 27 2c 20 27 20 7c 7c 20 25 51  d) || ', ' || %Q
20a48 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 25   || substr(sql,%
20a49 64 29 20 22 0a 20 20 20 20 20 20 20 20 22 57 48  d) ".        "WH
20a4a 45 52 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c  ERE type = 'tabl
20a4b 65 27 20 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51  e' AND name = %Q
20a4c 22 2c 20 0a 20 20 20 20 20 20 7a 44 62 2c 20 53  ", .      zDb, S
20a4d 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
20a4e 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66  , pNew->addColOf
20a4f 66 73 65 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77  fset, zCol, pNew
20a50 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31  ->addColOffset+1
20a51 2c 0a 20 20 20 20 20 20 7a 54 61 62 0a 20 20 20  ,.      zTab.   
20a52 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   );.    sqlite3D
20a53 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b  bFree(db, zCol);
20a54 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
20a55 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20  e default value 
20a56 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d  of the new colum
20a57 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  n is NULL, then 
20a58 73 65 74 20 74 68 65 20 66 69 6c 65 0a 20 20 2a  set the file.  *
20a59 2a 20 66 6f 72 6d 61 74 20 74 6f 20 32 2e 20 49  * format to 2. I
20a5a 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  f the default va
20a5b 6c 75 65 20 6f 66 20 74 68 65 20 6e 65 77 20 63  lue of the new c
20a5c 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 4e 55 4c  olumn is not NUL
20a5d 4c 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 6c 65  L,.  ** the file
20a5e 20 66 6f 72 6d 61 74 20 62 65 63 6f 6d 65 73 20   format becomes 
20a5f 33 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  3..  */.  sqlite
20a60 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d  3MinimumFileForm
20a61 61 74 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  at(pParse, iDb, 
20a62 70 44 66 6c 74 20 3f 20 33 20 3a 20 32 29 3b 0a  pDflt ? 3 : 2);.
20a63 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65  .  /* Reload the
20a64 20 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 6d   schema of the m
20a65 6f 64 69 66 69 65 64 20 74 61 62 6c 65 2e 20 2a  odified table. *
20a66 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 53  /.  reloadTableS
20a67 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70 54  chema(pParse, pT
20a68 61 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  ab, pTab->zName)
20a69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
20a6a 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
20a6b 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
20a6c 20 61 66 74 65 72 20 74 68 65 20 74 61 62 6c 65   after the table
20a6d 2d 6e 61 6d 65 20 69 6e 0a 2a 2a 20 61 6e 20 22  -name in.** an "
20a6e 41 4c 54 45 52 20 54 41 42 4c 45 20 3c 74 61 62  ALTER TABLE <tab
20a6f 6c 65 2d 6e 61 6d 65 3e 20 41 44 44 22 20 73 74  le-name> ADD" st
20a70 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65  atement is parse
20a71 64 2e 20 41 72 67 75 6d 65 6e 74 20 0a 2a 2a 20  d. Argument .** 
20a72 70 53 72 63 20 69 73 20 74 68 65 20 66 75 6c 6c  pSrc is the full
20a73 2d 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62  -name of the tab
20a74 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64  le being altered
20a75 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
20a76 74 69 6e 65 20 6d 61 6b 65 73 20 61 20 28 70 61  tine makes a (pa
20a77 72 74 69 61 6c 29 20 63 6f 70 79 20 6f 66 20 74  rtial) copy of t
20a78 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
20a79 72 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61  re.** for the ta
20a7a 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65  ble being altere
20a7b 64 20 61 6e 64 20 73 65 74 73 20 50 61 72 73 65  d and sets Parse
20a7c 2e 70 4e 65 77 54 61 62 6c 65 20 74 6f 20 70 6f  .pNewTable to po
20a7d 69 6e 74 0a 2a 2a 20 74 6f 20 69 74 2e 20 52 6f  int.** to it. Ro
20a7e 75 74 69 6e 65 73 20 63 61 6c 6c 65 64 20 62 79  utines called by
20a7f 20 74 68 65 20 70 61 72 73 65 72 20 61 73 20 74   the parser as t
20a80 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69  he column defini
20a81 74 69 6f 6e 0a 2a 2a 20 69 73 20 70 61 72 73 65  tion.** is parse
20a82 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 33 41  d (i.e. sqlite3A
20a83 64 64 43 6f 6c 75 6d 6e 28 29 29 20 61 64 64 20  ddColumn()) add 
20a84 74 68 65 20 6e 65 77 20 43 6f 6c 75 6d 6e 20 64  the new Column d
20a85 61 74 61 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63  ata to .** the c
20a86 6f 70 79 2e 20 54 68 65 20 63 6f 70 79 20 6f 66  opy. The copy of
20a87 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
20a88 74 75 72 65 20 69 73 20 64 65 6c 65 74 65 64 20  ture is deleted 
20a89 62 79 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 0a 2a  by tokenize.c .*
20a8a 2a 20 61 66 74 65 72 20 70 61 72 73 69 6e 67 20  * after parsing 
20a8b 69 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2a 0a  is finished..**.
20a8c 2a 2a 20 52 6f 75 74 69 6e 65 20 73 71 6c 69 74  ** Routine sqlit
20a8d 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64  e3AlterFinishAdd
20a8e 43 6f 6c 75 6d 6e 28 29 20 77 69 6c 6c 20 62 65  Column() will be
20a8f 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c   called to compl
20a90 65 74 65 0a 2a 2a 20 63 6f 64 69 6e 67 20 74 68  ete.** coding th
20a91 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e  e "ALTER TABLE .
20a92 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e  .. ADD" statemen
20a93 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
20a94 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
20a95 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f  3AlterBeginAddCo
20a96 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72  lumn(Parse *pPar
20a97 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72  se, SrcList *pSr
20a98 63 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65  c){.  Table *pNe
20a99 77 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  w;.  Table *pTab
20a9a 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  ;.  Vdbe *v;.  i
20a9b 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b  nt iDb;.  int i;
20a9c 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20  .  int nAlloc;. 
20a9d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
20a9e 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a  Parse->db;..  /*
20a9f 20 4c 6f 6f 6b 20 75 70 20 74 68 65 20 74 61 62   Look up the tab
20aa0 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64  le being altered
20aa1 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  . */.  assert( p
20aa2 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
20aa3 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
20aa4 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
20aa5 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29  dsAllMutexes(db)
20aa6 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
20aa7 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
20aa8 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  o exit_begin_add
20aa9 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 54 61 62 20  _column;.  pTab 
20aaa 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54  = sqlite3LocateT
20aab 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20  able(pParse, 0, 
20aac 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  pSrc->a[0].zName
20aad 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61  , pSrc->a[0].zDa
20aae 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21  tabase);.  if( !
20aaf 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74  pTab ) goto exit
20ab0 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d  _begin_add_colum
20ab1 6e 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  n;..#ifndef SQLI
20ab2 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
20ab3 41 42 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72  ABLE.  if( IsVir
20ab4 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
20ab5 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
20ab6 67 28 70 50 61 72 73 65 2c 20 22 76 69 72 74 75  g(pParse, "virtu
20ab7 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f  al tables may no
20ab8 74 20 62 65 20 61 6c 74 65 72 65 64 22 29 3b 0a  t be altered");.
20ab9 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65      goto exit_be
20aba 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
20abb 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
20abc 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20   Make sure this 
20abd 69 73 20 6e 6f 74 20 61 6e 20 61 74 74 65 6d 70  is not an attemp
20abe 74 20 74 6f 20 41 4c 54 45 52 20 61 20 76 69 65  t to ALTER a vie
20abf 77 2e 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  w. */.  if( pTab
20ac0 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
20ac1 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
20ac2 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74  (pParse, "Cannot
20ac3 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20 74 6f   add a column to
20ac4 20 61 20 76 69 65 77 22 29 3b 0a 20 20 20 20 67   a view");.    g
20ac5 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
20ac6 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a  dd_column;.  }..
20ac7 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
20ac8 61 64 64 43 6f 6c 4f 66 66 73 65 74 3e 30 20 29  addColOffset>0 )
20ac9 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
20aca 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
20acb 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
20acc 29 3b 0a 0a 20 20 2f 2a 20 50 75 74 20 61 20 63  );..  /* Put a c
20acd 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65  opy of the Table
20ace 20 73 74 72 75 63 74 20 69 6e 20 50 61 72 73 65   struct in Parse
20acf 2e 70 4e 65 77 54 61 62 6c 65 20 66 6f 72 20 74  .pNewTable for t
20ad0 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 41  he.  ** sqlite3A
20ad1 64 64 43 6f 6c 75 6d 6e 28 29 20 66 75 6e 63 74  ddColumn() funct
20ad2 69 6f 6e 20 61 6e 64 20 66 72 69 65 6e 64 73 20  ion and friends 
20ad3 74 6f 20 6d 6f 64 69 66 79 2e 20 20 42 75 74 20  to modify.  But 
20ad4 6d 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20  modify.  ** the 
20ad5 6e 61 6d 65 20 62 79 20 61 64 64 69 6e 67 20 61  name by adding a
20ad6 6e 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72 74  n "sqlite_altert
20ad7 61 62 5f 22 20 70 72 65 66 69 78 2e 20 20 42 79  ab_" prefix.  By
20ad8 20 61 64 64 69 6e 67 20 74 68 69 73 0a 20 20 2a   adding this.  *
20ad9 2a 20 70 72 65 66 69 78 2c 20 77 65 20 69 6e 73  * prefix, we ins
20ada 75 72 65 20 74 68 61 74 20 74 68 65 20 6e 61 6d  ure that the nam
20adb 65 20 77 69 6c 6c 20 6e 6f 74 20 63 6f 6c 6c 69  e will not colli
20adc 64 65 20 77 69 74 68 20 61 6e 20 65 78 69 73 74  de with an exist
20add 69 6e 67 0a 20 20 2a 2a 20 74 61 62 6c 65 20 62  ing.  ** table b
20ade 65 63 61 75 73 65 20 75 73 65 72 20 74 61 62 6c  ecause user tabl
20adf 65 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65  e are not allowe
20ae0 64 20 74 6f 20 68 61 76 65 20 74 68 65 20 22 73  d to have the "s
20ae1 71 6c 69 74 65 5f 22 0a 20 20 2a 2a 20 70 72 65  qlite_".  ** pre
20ae2 66 69 78 20 6f 6e 20 74 68 65 69 72 20 6e 61 6d  fix on their nam
20ae3 65 2e 0a 20 20 2a 2f 0a 20 20 70 4e 65 77 20 3d  e..  */.  pNew =
20ae4 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 33   (Table*)sqlite3
20ae5 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
20ae6 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b   sizeof(Table));
20ae7 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 20 67  .  if( !pNew ) g
20ae8 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
20ae9 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 50 61  dd_column;.  pPa
20aea 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d  rse->pNewTable =
20aeb 20 70 4e 65 77 3b 0a 20 20 70 4e 65 77 2d 3e 6e   pNew;.  pNew->n
20aec 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d  Ref = 1;.  pNew-
20aed 3e 64 62 4d 65 6d 20 3d 20 70 54 61 62 2d 3e 64  >dbMem = pTab->d
20aee 62 4d 65 6d 3b 0a 20 20 70 4e 65 77 2d 3e 6e 43  bMem;.  pNew->nC
20aef 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b  ol = pTab->nCol;
20af0 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d  .  assert( pNew-
20af1 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 6e 41 6c  >nCol>0 );.  nAl
20af2 6c 6f 63 20 3d 20 28 28 28 70 4e 65 77 2d 3e 6e  loc = (((pNew->n
20af3 43 6f 6c 2d 31 29 2f 38 29 2a 38 29 2b 38 3b 0a  Col-1)/8)*8)+8;.
20af4 20 20 61 73 73 65 72 74 28 20 6e 41 6c 6c 6f 63    assert( nAlloc
20af5 3e 3d 70 4e 65 77 2d 3e 6e 43 6f 6c 20 26 26 20  >=pNew->nCol && 
20af6 6e 41 6c 6c 6f 63 25 38 3d 3d 30 20 26 26 20 6e  nAlloc%8==0 && n
20af7 41 6c 6c 6f 63 2d 70 4e 65 77 2d 3e 6e 43 6f 6c  Alloc-pNew->nCol
20af8 3c 38 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 61 43  <8 );.  pNew->aC
20af9 6f 6c 20 3d 20 28 43 6f 6c 75 6d 6e 2a 29 73 71  ol = (Column*)sq
20afa 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
20afb 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 43 6f 6c  o(db, sizeof(Col
20afc 75 6d 6e 29 2a 6e 41 6c 6c 6f 63 29 3b 0a 20 20  umn)*nAlloc);.  
20afd 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71  pNew->zName = sq
20afe 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
20aff 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 61   "sqlite_alterta
20b00 62 5f 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  b_%s", pTab->zNa
20b01 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77  me);.  if( !pNew
20b02 2d 3e 61 43 6f 6c 20 7c 7c 20 21 70 4e 65 77 2d  ->aCol || !pNew-
20b03 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 64 62  >zName ){.    db
20b04 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
20b05 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   1;.    goto exi
20b06 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75  t_begin_add_colu
20b07 6d 6e 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79  mn;.  }.  memcpy
20b08 28 70 4e 65 77 2d 3e 61 43 6f 6c 2c 20 70 54 61  (pNew->aCol, pTa
20b09 62 2d 3e 61 43 6f 6c 2c 20 73 69 7a 65 6f 66 28  b->aCol, sizeof(
20b0a 43 6f 6c 75 6d 6e 29 2a 70 4e 65 77 2d 3e 6e 43  Column)*pNew->nC
20b0b 6f 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ol);.  for(i=0; 
20b0c 69 3c 70 4e 65 77 2d 3e 6e 43 6f 6c 3b 20 69 2b  i<pNew->nCol; i+
20b0d 2b 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a  +){.    Column *
20b0e 70 43 6f 6c 20 3d 20 26 70 4e 65 77 2d 3e 61 43  pCol = &pNew->aC
20b0f 6f 6c 5b 69 5d 3b 0a 20 20 20 20 70 43 6f 6c 2d  ol[i];.    pCol-
20b10 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
20b11 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 43 6f  DbStrDup(db, pCo
20b12 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70  l->zName);.    p
20b13 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 30 3b 0a  Col->zColl = 0;.
20b14 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20      pCol->zType 
20b15 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 70  = 0;.    pCol->p
20b16 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Dflt = 0;.  }.  
20b17 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20  pNew->pSchema = 
20b18 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
20b19 68 65 6d 61 3b 0a 20 20 70 4e 65 77 2d 3e 61 64  hema;.  pNew->ad
20b1a 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 70 54 61  dColOffset = pTa
20b1b 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b  b->addColOffset;
20b1c 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20  .  pNew->nRef = 
20b1d 31 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61  1;..  /* Begin a
20b1e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
20b1f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73   increment the s
20b20 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 2a  chema cookie.  *
20b21 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  /.  sqlite3Begin
20b22 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
20b23 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
20b24 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
20b25 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
20b26 69 66 28 20 21 76 20 29 20 67 6f 74 6f 20 65 78  if( !v ) goto ex
20b27 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c  it_begin_add_col
20b28 75 6d 6e 3b 0a 20 20 73 71 6c 69 74 65 33 43 68  umn;.  sqlite3Ch
20b29 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
20b2a 65 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f 62  e, iDb);..exit_b
20b2b 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3a  egin_add_column:
20b2c 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
20b2d 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63  tDelete(db, pSrc
20b2e 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23  );.  return;.}.#
20b2f 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45  endif  /* SQLITE
20b30 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a  _ALTER_TABLE */.
20b31 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
20b32 20 45 6e 64 20 6f 66 20 61 6c 74 65 72 2e 63 20   End of alter.c 
20b33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
20b36 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
20b37 20 42 65 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c   Begin file anal
20b38 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  yze.c **********
20b39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
20b3b 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79  ./*.** 2005 July
20b3c 20 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   8.**.** The aut
20b3d 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
20b3e 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
20b3f 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
20b40 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
20b41 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
20b42 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
20b43 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
20b44 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
20b45 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
20b46 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
20b47 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
20b48 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
20b49 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
20b4a 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
20b4b 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
20b4c 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
20b4d 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
20b4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b52 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
20b53 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 61 73 73  ontains code ass
20b54 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
20b55 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64   ANALYZE command
20b56 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
20b57 3a 20 61 6e 61 6c 79 7a 65 2e 63 2c 76 20 31 2e  : analyze.c,v 1.
20b58 35 32 20 32 30 30 39 2f 30 34 2f 31 36 20 31 37  52 2009/04/16 17
20b59 3a 34 35 3a 34 38 20 64 72 68 20 45 78 70 20 24  :45:48 drh Exp $
20b5a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
20b5b 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a  TE_OMIT_ANALYZE.
20b5c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
20b5d 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
20b5e 64 65 20 74 68 61 74 20 6f 70 65 6e 73 20 74 68  de that opens th
20b5f 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
20b60 61 62 6c 65 20 6f 6e 20 63 75 72 73 6f 72 0a 2a  able on cursor.*
20b61 2a 20 69 53 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a  * iStatCur..**.*
20b62 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f  * If the sqlite_
20b63 73 74 61 74 31 20 74 61 62 6c 65 73 20 64 6f 65  stat1 tables doe
20b64 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79  s not previously
20b65 20 65 78 69 73 74 2c 20 69 74 20 69 73 20 63 72   exist, it is cr
20b66 65 61 74 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20  eated..** If it 
20b67 64 6f 65 73 20 70 72 65 76 69 6f 75 73 6c 79 20  does previously 
20b68 65 78 69 73 74 2c 20 61 6c 6c 20 65 6e 74 69 72  exist, all entir
20b69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
20b6a 74 68 20 74 61 62 6c 65 20 7a 57 68 65 72 65 0a  th table zWhere.
20b6b 2a 2a 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 20  ** are removed. 
20b6c 20 49 66 20 7a 57 68 65 72 65 3d 3d 30 20 74 68   If zWhere==0 th
20b6d 65 6e 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 61  en all entries a
20b6e 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73  re removed..*/.s
20b6f 74 61 74 69 63 20 76 6f 69 64 20 6f 70 65 6e 53  tatic void openS
20b70 74 61 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73  tatTable(.  Pars
20b71 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
20b72 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
20b73 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
20b74 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  iDb,            
20b75 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
20b76 61 73 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69  ase we are looki
20b77 6e 67 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ng in */.  int i
20b78 53 74 61 74 43 75 72 2c 20 20 20 20 20 20 20 20  StatCur,        
20b79 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73     /* Open the s
20b7a 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
20b7b 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72  e on this cursor
20b7c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
20b7d 20 2a 7a 57 68 65 72 65 20 20 20 20 20 20 2f 2a   *zWhere      /*
20b7e 20 44 65 6c 65 74 65 20 65 6e 74 72 69 65 73 20   Delete entries 
20b7f 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
20b80 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b  this table */.){
20b81 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
20b82 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44   pParse->db;.  D
20b83 62 20 2a 70 44 62 3b 0a 20 20 69 6e 74 20 69 52  b *pDb;.  int iR
20b84 6f 6f 74 50 61 67 65 3b 0a 20 20 75 38 20 63 72  ootPage;.  u8 cr
20b85 65 61 74 65 53 74 61 74 31 20 3d 20 30 3b 0a 20  eateStat1 = 0;. 
20b86 20 54 61 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20   Table *pStat;. 
20b87 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
20b88 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
20b89 29 3b 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  );..  if( v==0 )
20b8a 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
20b8b 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
20b8c 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64  oldsAllMutexes(d
20b8d 62 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  b) );.  assert( 
20b8e 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
20b8f 3d 3d 64 62 20 29 3b 0a 20 20 70 44 62 20 3d 20  ==db );.  pDb = 
20b90 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20  &db->aDb[iDb];. 
20b91 20 69 66 28 20 28 70 53 74 61 74 20 3d 20 73 71   if( (pStat = sq
20b92 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
20b93 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31  b, "sqlite_stat1
20b94 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d  ", pDb->zName))=
20b95 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  =0 ){.    /* The
20b96 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
20b97 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74 20 65 78  bles does not ex
20b98 69 73 74 2e 20 20 43 72 65 61 74 65 20 69 74 2e  ist.  Create it.
20b99 20 20 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74    .    ** Note t
20b9a 68 61 74 20 61 20 73 69 64 65 2d 65 66 66 65 63  hat a side-effec
20b9b 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20  t of the CREATE 
20b9c 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
20b9d 69 73 20 74 6f 20 6c 65 61 76 65 0a 20 20 20 20  is to leave.    
20b9e 2a 2a 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20  ** the rootpage 
20b9f 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
20ba0 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61   in register pPa
20ba1 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 20 20 54  rse->regRoot.  T
20ba2 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 69 6d  his is.    ** im
20ba3 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20  portant because 
20ba4 74 68 65 20 4f 70 65 6e 57 72 69 74 65 20 6f 70  the OpenWrite op
20ba5 63 6f 64 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20  code below will 
20ba6 62 65 20 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a  be needing it. *
20ba7 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  /.    sqlite3Nes
20ba8 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
20ba9 0a 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54  .      "CREATE T
20baa 41 42 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73  ABLE %Q.sqlite_s
20bab 74 61 74 31 28 74 62 6c 2c 69 64 78 2c 73 74 61  tat1(tbl,idx,sta
20bac 74 29 22 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e  t)",.      pDb->
20bad 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20  zName.    );.   
20bae 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 50 61   iRootPage = pPa
20baf 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20  rse->regRoot;.  
20bb0 20 20 63 72 65 61 74 65 53 74 61 74 31 20 3d 20    createStat1 = 
20bb1 31 3b 20 20 2f 2a 20 43 61 75 73 65 20 72 6f 6f  1;  /* Cause roo
20bb2 74 70 61 67 65 20 74 6f 20 62 65 20 74 61 6b 65  tpage to be take
20bb3 6e 20 66 72 6f 6d 20 74 6f 70 20 6f 66 20 73 74  n from top of st
20bb4 61 63 6b 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69  ack */.  }else i
20bb5 66 28 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20  f( zWhere ){.   
20bb6 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73   /* The sqlite_s
20bb7 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74  tat1 table exist
20bb8 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 65  s.  Delete all e
20bb9 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65  ntries associate
20bba 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68  d with.    ** th
20bbb 65 20 74 61 62 6c 65 20 7a 57 68 65 72 65 2e 20  e table zWhere. 
20bbc 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  */.    sqlite3Ne
20bbd 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
20bbe 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45 54 45  ,.       "DELETE
20bbf 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f   FROM %Q.sqlite_
20bc0 73 74 61 74 31 20 57 48 45 52 45 20 74 62 6c 3d  stat1 WHERE tbl=
20bc1 25 51 22 2c 0a 20 20 20 20 20 20 20 70 44 62 2d  %Q",.       pDb-
20bc2 3e 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 0a 20  >zName, zWhere. 
20bc3 20 20 20 29 3b 0a 20 20 20 20 69 52 6f 6f 74 50     );.    iRootP
20bc4 61 67 65 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75  age = pStat->tnu
20bc5 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  m;.  }else{.    
20bc6 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74  /* The sqlite_st
20bc7 61 74 31 20 74 61 62 6c 65 20 61 6c 72 65 61 64  at1 table alread
20bc8 79 20 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74  y exists.  Delet
20bc9 65 20 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20  e all rows. */. 
20bca 20 20 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70     iRootPage = p
20bcb 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20  Stat->tnum;.    
20bcc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20bcd 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70  2(v, OP_Clear, p
20bce 53 74 61 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  Stat->tnum, iDb)
20bcf 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e  ;.  }..  /* Open
20bd0 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
20bd1 31 20 74 61 62 6c 65 20 66 6f 72 20 77 72 69 74  1 table for writ
20bd2 69 6e 67 2e 20 55 6e 6c 65 73 73 20 69 74 20 77  ing. Unless it w
20bd3 61 73 20 63 72 65 61 74 65 64 0a 20 20 2a 2a 20  as created.  ** 
20bd4 62 79 20 74 68 69 73 20 76 64 62 65 20 70 72 6f  by this vdbe pro
20bd5 67 72 61 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f  gram, lock it fo
20bd6 72 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 65  r writing at the
20bd7 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65   shared-cache le
20bd8 76 65 6c 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68  vel. .  ** If th
20bd9 69 73 20 76 64 62 65 20 64 69 64 20 63 72 65 61  is vdbe did crea
20bda 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  te the sqlite_st
20bdb 61 74 31 20 74 61 62 6c 65 2c 20 74 68 65 6e 20  at1 table, then 
20bdc 69 74 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20  it must have .  
20bdd 2a 2a 20 61 6c 72 65 61 64 79 20 6f 62 74 61 69  ** already obtai
20bde 6e 65 64 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63  ned a schema-loc
20bdf 6b 2c 20 6d 61 6b 69 6e 67 20 74 68 65 20 77 72  k, making the wr
20be0 69 74 65 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61  ite-lock redunda
20be1 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  nt..  */.  if( !
20be2 63 72 65 61 74 65 53 74 61 74 31 20 29 7b 0a 20  createStat1 ){. 
20be3 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c     sqlite3TableL
20be4 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ock(pParse, iDb,
20be5 20 69 52 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22   iRootPage, 1, "
20be6 73 71 6c 69 74 65 5f 73 74 61 74 31 22 29 3b 0a  sqlite_stat1");.
20be7 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
20be8 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70  eAddOp3(v, OP_Op
20be9 65 6e 57 72 69 74 65 2c 20 69 53 74 61 74 43 75  enWrite, iStatCu
20bea 72 2c 20 69 52 6f 6f 74 50 61 67 65 2c 20 69 44  r, iRootPage, iD
20beb 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  b);.  sqlite3Vdb
20bec 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
20bed 20 28 63 68 61 72 20 2a 29 33 2c 20 50 34 5f 49   (char *)3, P4_I
20bee 4e 54 33 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  NT32);.  sqlite3
20bef 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
20bf0 63 72 65 61 74 65 53 74 61 74 31 29 3b 0a 7d 0a  createStat1);.}.
20bf1 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
20bf2 63 6f 64 65 20 74 6f 20 64 6f 20 61 6e 20 61 6e  code to do an an
20bf3 61 6c 79 73 69 73 20 6f 66 20 61 6c 6c 20 69 6e  alysis of all in
20bf4 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 64  dices associated
20bf5 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c   with.** a singl
20bf6 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  e table..*/.stat
20bf7 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 4f  ic void analyzeO
20bf8 6e 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  neTable(.  Parse
20bf9 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50   *pParse,   /* P
20bfa 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
20bfb 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
20bfc 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 77 68 6f      /* Table who
20bfd 73 65 20 69 6e 64 69 63 65 73 20 61 72 65 20 74  se indices are t
20bfe 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  o be analyzed */
20bff 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 2c  .  int iStatCur,
20c00 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
20c01 56 64 62 65 43 75 72 73 6f 72 20 74 68 61 74 20  VdbeCursor that 
20c02 77 72 69 74 65 73 20 74 68 65 20 73 71 6c 69 74  writes the sqlit
20c03 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f  e_stat1 table */
20c04 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20  .  int iMem     
20c05 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65      /* Available
20c06 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
20c07 73 20 62 65 67 69 6e 20 68 65 72 65 20 2a 2f 0a  s begin here */.
20c08 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  ){.  Index *pIdx
20c09 3b 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65  ;     /* An inde
20c0a 78 20 74 6f 20 62 65 69 6e 67 20 61 6e 61 6c 79  x to being analy
20c0b 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64  zed */.  int iId
20c0c 78 43 75 72 3b 20 20 20 20 20 2f 2a 20 49 6e 64  xCur;     /* Ind
20c0d 65 78 20 6f 66 20 56 64 62 65 43 75 72 73 6f 72  ex of VdbeCursor
20c0e 20 66 6f 72 20 69 6e 64 65 78 20 62 65 69 6e 67   for index being
20c0f 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69   analyzed */.  i
20c10 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt nCol;        
20c11 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
20c12 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65  umns in the inde
20c13 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  x */.  Vdbe *v; 
20c14 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
20c15 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62  irtual machine b
20c16 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 2a 2f  eing built up */
20c17 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
20c18 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
20c19 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70  ter */.  int top
20c1a 4f 66 4c 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65  OfLoop;   /* The
20c1b 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   top of the loop
20c1c 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f 66 4c   */.  int endOfL
20c1d 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65 20 65 6e  oop;   /* The en
20c1e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  d of the loop */
20c1f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20  .  int addr;    
20c20 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64 72 65      /* The addre
20c21 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72 75 63  ss of an instruc
20c22 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44  tion */.  int iD
20c23 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  b;         /* In
20c24 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20  dex of database 
20c25 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20  containing pTab 
20c26 2a 2f 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  */..  v = sqlite
20c27 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
20c28 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c 20  ;.  if( v==0 || 
20c29 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20 7c  NEVER(pTab==0) |
20c2a 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d  | pTab->pIndex==
20c2b 30 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e  0 ){.    /* Do n
20c2c 6f 20 61 6e 61 6c 79 73 69 73 20 66 6f 72 20 74  o analysis for t
20c2d 61 62 6c 65 73 20 74 68 61 74 20 68 61 76 65 20  ables that have 
20c2e 6e 6f 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  no indices */.  
20c2f 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
20c30 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
20c31 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
20c32 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20  xes(pParse->db) 
20c33 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  );.  iDb = sqlit
20c34 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
20c35 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
20c36 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
20c37 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a  sert( iDb>=0 );.
20c38 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
20c39 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
20c3a 4e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  N.  if( sqlite3A
20c3b 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
20c3c 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45 2c   SQLITE_ANALYZE,
20c3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c   pTab->zName, 0,
20c3e 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64  .      pParse->d
20c3f 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
20c40 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  e ) ){.    retur
20c41 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  n;.  }.#endif.. 
20c42 20 2f 2a 20 45 73 74 61 62 6c 69 73 68 20 61 20   /* Establish a 
20c43 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  read-lock on the
20c44 20 74 61 62 6c 65 20 61 74 20 74 68 65 20 73 68   table at the sh
20c45 61 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65 6c  ared-cache level
20c46 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61  . */.  sqlite3Ta
20c47 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20  bleLock(pParse, 
20c48 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  iDb, pTab->tnum,
20c49 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29   0, pTab->zName)
20c4a 3b 0a 0a 20 20 69 49 64 78 43 75 72 20 3d 20 70  ;..  iIdxCur = p
20c4b 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
20c4c 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
20c4d 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
20c4e 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
20c4f 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  .    KeyInfo *pK
20c50 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
20c51 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
20c52 20 70 49 64 78 29 3b 0a 20 20 20 20 69 6e 74 20   pIdx);.    int 
20c53 72 65 67 46 69 65 6c 64 73 3b 20 20 20 20 2f 2a  regFields;    /*
20c54 20 52 65 67 69 73 74 65 72 20 62 6c 6f 63 6b 20   Register block 
20c55 66 6f 72 20 62 75 69 6c 64 69 6e 67 20 72 65 63  for building rec
20c56 6f 72 64 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ords */.    int 
20c57 72 65 67 52 65 63 3b 20 20 20 20 20 20 20 2f 2a  regRec;       /*
20c58 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
20c59 67 20 63 6f 6d 70 6c 65 74 65 64 20 72 65 63 6f  g completed reco
20c5a 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65  rd */.    int re
20c5b 67 54 65 6d 70 3b 20 20 20 20 20 20 2f 2a 20 54  gTemp;      /* T
20c5c 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65 67  emporary use reg
20c5d 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  ister */.    int
20c5e 20 72 65 67 43 6f 6c 3b 20 20 20 20 20 20 20 2f   regCol;       /
20c5f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63  * Content of a c
20c60 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20 74  olumn from the t
20c61 61 62 6c 65 20 62 65 69 6e 67 20 61 6e 61 6c 79  able being analy
20c62 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72  zed */.    int r
20c63 65 67 52 6f 77 69 64 3b 20 20 20 20 20 2f 2a 20  egRowid;     /* 
20c64 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20 69 6e  Rowid for the in
20c65 73 65 72 74 65 64 20 72 65 63 6f 72 64 20 2a 2f  serted record */
20c66 0a 20 20 20 20 69 6e 74 20 72 65 67 46 32 3b 0a  .    int regF2;.
20c67 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20 63  .    /* Open a c
20c68 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 6e 64  ursor to the ind
20c69 65 78 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  ex to be analyze
20c6a 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  d.    */.    ass
20c6b 65 72 74 28 20 69 44 62 3d 3d 73 71 6c 69 74 65  ert( iDb==sqlite
20c6c 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
20c6d 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 64 78 2d  Parse->db, pIdx-
20c6e 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 20 20  >pSchema) );.   
20c6f 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43   nCol = pIdx->nC
20c70 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74  olumn;.    sqlit
20c71 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
20c72 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64  OP_OpenRead, iId
20c73 78 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d  xCur, pIdx->tnum
20c74 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 28  , iDb,.        (
20c75 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 50 34 5f  char *)pKey, P4_
20c76 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
20c77 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
20c78 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78  t((v, "%s", pIdx
20c79 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 72  ->zName));.    r
20c7a 65 67 46 69 65 6c 64 73 20 3d 20 69 4d 65 6d 2b  egFields = iMem+
20c7b 6e 43 6f 6c 2a 32 3b 0a 20 20 20 20 72 65 67 54  nCol*2;.    regT
20c7c 65 6d 70 20 3d 20 72 65 67 52 6f 77 69 64 20 3d  emp = regRowid =
20c7d 20 72 65 67 43 6f 6c 20 3d 20 72 65 67 46 69 65   regCol = regFie
20c7e 6c 64 73 2b 33 3b 0a 20 20 20 20 72 65 67 52 65  lds+3;.    regRe
20c7f 63 20 3d 20 72 65 67 43 6f 6c 2b 31 3b 0a 20 20  c = regCol+1;.  
20c80 20 20 69 66 28 20 72 65 67 52 65 63 3e 70 50 61    if( regRec>pPa
20c81 72 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20 20 20  rse->nMem ){.   
20c82 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
20c83 3d 20 72 65 67 52 65 63 3b 0a 20 20 20 20 7d 0a  = regRec;.    }.
20c84 0a 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63  .    /* Memory c
20c85 65 6c 6c 73 20 61 72 65 20 75 73 65 64 20 61 73  ells are used as
20c86 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a   follows:.    **
20c87 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69  .    **    mem[i
20c88 4d 65 6d 5d 3a 20 20 20 20 20 20 20 20 20 20 20  Mem]:           
20c89 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62    The total numb
20c8a 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
20c8b 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20  e table..    ** 
20c8c 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 31 5d 3a 20     mem[iMem+1]: 
20c8d 20 20 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72            Number
20c8e 20 6f 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c   of distinct val
20c8f 75 65 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 31 0a  ues in column 1.
20c90 20 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20      **    ....  
20c91 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d    **    mem[iMem
20c92 2b 6e 43 6f 6c 5d 3a 20 20 20 20 20 20 20 20 4e  +nCol]:        N
20c93 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63  umber of distinc
20c94 74 20 76 61 6c 75 65 73 20 69 6e 20 63 6f 6c 75  t values in colu
20c95 6d 6e 20 4e 0a 20 20 20 20 2a 2a 20 20 20 20 6d  mn N.    **    m
20c96 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 2b 31 5d 20  em[iMem+nCol+1] 
20c97 20 20 20 20 20 20 4c 61 73 74 20 6f 62 73 65 72        Last obser
20c98 76 65 64 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c  ved value of col
20c99 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 20 20 20  umn 1.    **    
20c9a 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65  ....    **    me
20c9b 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 2b 6e 43 6f 6c  m[iMem+nCol+nCol
20c9c 5d 3a 20 20 20 4c 61 73 74 20 6f 62 73 65 72 76  ]:   Last observ
20c9d 65 64 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75  ed value of colu
20c9e 6d 6e 20 4e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  mn N.    **.    
20c9f 2a 2a 20 43 65 6c 6c 73 20 69 4d 65 6d 20 74 68  ** Cells iMem th
20ca0 72 6f 75 67 68 20 69 4d 65 6d 2b 6e 43 6f 6c 20  rough iMem+nCol 
20ca1 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  are initialized 
20ca2 74 6f 20 30 2e 20 20 54 68 65 20 6f 74 68 65 72  to 0.  The other
20ca3 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 69 6e 69  s.    ** are ini
20ca4 74 69 61 6c 69 7a 65 64 20 74 6f 20 4e 55 4c 4c  tialized to NULL
20ca5 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
20ca6 28 69 3d 30 3b 20 69 3c 3d 6e 43 6f 6c 3b 20 69  (i=0; i<=nCol; i
20ca7 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
20ca8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
20ca9 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69  OP_Integer, 0, i
20caa 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20  Mem+i);.    }.  
20cab 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
20cac 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  l; i++){.      s
20cad 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
20cae 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
20caf 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a  iMem+nCol+i+1);.
20cb0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f      }..    /* Do
20cb1 20 74 68 65 20 61 6e 61 6c 79 73 69 73 2e 0a 20   the analysis.. 
20cb2 20 20 20 2a 2f 0a 20 20 20 20 65 6e 64 4f 66 4c     */.    endOfL
20cb3 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oop = sqlite3Vdb
20cb4 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
20cb5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20cb6 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
20cb7 64 2c 20 69 49 64 78 43 75 72 2c 20 65 6e 64 4f  d, iIdxCur, endO
20cb8 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 74 6f 70 4f  fLoop);.    topO
20cb9 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  fLoop = sqlite3V
20cba 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
20cbb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
20cbc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
20cbd 64 64 49 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29 3b  ddImm, iMem, 1);
20cbe 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
20cbf 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
20cc0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20cc1 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
20cc2 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20 72 65  , iIdxCur, i, re
20cc3 67 43 6f 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c  gCol);.      sql
20cc4 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
20cc5 2c 20 4f 50 5f 4e 65 2c 20 72 65 67 43 6f 6c 2c  , OP_Ne, regCol,
20cc6 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b   0, iMem+nCol+i+
20cc7 31 29 3b 0a 20 20 20 20 20 20 2f 2a 2a 2a 2a 20  1);.      /**** 
20cc8 54 4f 44 4f 3a 20 20 61 64 64 20 63 6f 6c 6c 61  TODO:  add colla
20cc9 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2a  ting sequence **
20cca 2a 2a 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  ***/.      sqlit
20ccb 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
20ccc 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
20ccd 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ULL);.    }.    
20cce 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20ccf 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
20cd0 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20   endOfLoop);.   
20cd1 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
20cd2 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
20cd3 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
20cd4 65 28 76 2c 20 74 6f 70 4f 66 4c 6f 6f 70 20 2b  e(v, topOfLoop +
20cd5 20 32 2a 28 69 20 2b 20 31 29 29 3b 0a 20 20 20   2*(i + 1));.   
20cd6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20cd7 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
20cd8 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 31 29 3b  m, iMem+i+1, 1);
20cd9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
20cda 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
20cdb 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20  olumn, iIdxCur, 
20cdc 69 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31  i, iMem+nCol+i+1
20cdd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
20cde 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
20cdf 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f  abel(v, endOfLoo
20ce0 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  p);.    sqlite3V
20ce1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
20ce2 4e 65 78 74 2c 20 69 49 64 78 43 75 72 2c 20 74  Next, iIdxCur, t
20ce3 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73  opOfLoop);.    s
20ce4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
20ce5 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49  (v, OP_Close, iI
20ce6 64 78 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20  dxCur);..    /* 
20ce7 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  Store the result
20ce8 73 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20  s.  .    **.    
20ce9 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73  ** The result is
20cea 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
20ceb 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
20cec 31 20 74 61 62 6c 65 2e 20 20 54 68 65 20 66 69  1 table.  The fi
20ced 72 73 74 0a 20 20 20 20 2a 2a 20 74 77 6f 20 63  rst.    ** two c
20cee 6f 6c 75 6d 6e 73 20 61 72 65 20 74 68 65 20 6e  olumns are the n
20cef 61 6d 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ames of the tabl
20cf0 65 20 61 6e 64 20 69 6e 64 65 78 2e 20 20 54 68  e and index.  Th
20cf1 65 20 74 68 69 72 64 20 63 6f 6c 75 6d 6e 0a 20  e third column. 
20cf2 20 20 20 2a 2a 20 69 73 20 61 20 73 74 72 69 6e     ** is a strin
20cf3 67 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20  g composed of a 
20cf4 6c 69 73 74 20 6f 66 20 69 6e 74 65 67 65 72 20  list of integer 
20cf5 73 74 61 74 69 73 74 69 63 73 20 61 62 6f 75 74  statistics about
20cf6 20 74 68 65 0a 20 20 20 20 2a 2a 20 69 6e 64 65   the.    ** inde
20cf7 78 2e 20 20 54 68 65 20 66 69 72 73 74 20 69 6e  x.  The first in
20cf8 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73  teger in the lis
20cf9 74 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  t is the total n
20cfa 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
20cfb 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 69  .    ** in the i
20cfc 6e 64 65 78 2e 20 20 54 68 65 72 65 20 69 73 20  ndex.  There is 
20cfd 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  one additional i
20cfe 6e 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69  nteger in the li
20cff 73 74 20 66 6f 72 20 65 61 63 68 0a 20 20 20 20  st for each.    
20d00 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  ** column of the
20d01 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 61 64   table.  This ad
20d02 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72  ditional integer
20d03 20 69 73 20 61 20 67 75 65 73 73 20 6f 66 20 68   is a guess of h
20d04 6f 77 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 72  ow many.    ** r
20d05 6f 77 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ows of the table
20d06 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c 20   the index will 
20d07 73 65 6c 65 63 74 2e 20 20 49 66 20 44 20 69 73  select.  If D is
20d08 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 64 69   the count of di
20d09 73 74 69 6e 63 74 0a 20 20 20 20 2a 2a 20 76 61  stinct.    ** va
20d0a 6c 75 65 73 20 61 6e 64 20 4b 20 69 73 20 74 68  lues and K is th
20d0b 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
20d0c 66 20 72 6f 77 73 2c 20 74 68 65 6e 20 74 68 65  f rows, then the
20d0d 20 69 6e 74 65 67 65 72 20 69 73 20 63 6f 6d 70   integer is comp
20d0e 75 74 65 64 0a 20 20 20 20 2a 2a 20 61 73 3a 0a  uted.    ** as:.
20d0f 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
20d10 20 20 20 20 20 49 20 3d 20 28 4b 2b 44 2d 31 29       I = (K+D-1)
20d11 2f 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  /D.    **.    **
20d12 20 49 66 20 4b 3d 3d 30 20 74 68 65 6e 20 6e 6f   If K==0 then no
20d13 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69   entry is made i
20d14 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73  nto the sqlite_s
20d15 74 61 74 31 20 74 61 62 6c 65 2e 20 20 0a 20 20  tat1 table.  .  
20d16 20 20 2a 2a 20 49 66 20 4b 3e 30 20 74 68 65 6e    ** If K>0 then
20d17 20 69 74 20 69 73 20 61 6c 77 61 79 73 20 74 68   it is always th
20d18 65 20 63 61 73 65 20 74 68 65 20 44 3e 30 20 73  e case the D>0 s
20d19 6f 20 64 69 76 69 73 69 6f 6e 20 62 79 20 7a 65  o division by ze
20d1a 72 6f 0a 20 20 20 20 2a 2a 20 69 73 20 6e 65 76  ro.    ** is nev
20d1b 65 72 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20  er possible..   
20d1c 20 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 73   */.    addr = s
20d1d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
20d1e 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 69 4d  (v, OP_IfNot, iM
20d1f 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  em);.    sqlite3
20d20 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
20d21 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67  _String8, 0, reg
20d22 46 69 65 6c 64 73 2c 20 30 2c 20 70 54 61 62 2d  Fields, 0, pTab-
20d23 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
20d24 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20d25 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
20d26 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2b 31 2c   0, regFields+1,
20d27 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c   0, pIdx->zName,
20d28 20 30 29 3b 0a 20 20 20 20 72 65 67 46 32 20 3d   0);.    regF2 =
20d29 20 72 65 67 46 69 65 6c 64 73 2b 32 3b 0a 20 20   regFields+2;.  
20d2a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20d2b 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
20d2c 20 69 4d 65 6d 2c 20 72 65 67 46 32 29 3b 0a 20   iMem, regF2);. 
20d2d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
20d2e 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
20d2f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20d30 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
20d31 20 30 2c 20 72 65 67 54 65 6d 70 2c 20 30 2c 20   0, regTemp, 0, 
20d32 22 20 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 73  " ", 0);.      s
20d33 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
20d34 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 72  (v, OP_Concat, r
20d35 65 67 54 65 6d 70 2c 20 72 65 67 46 32 2c 20 72  egTemp, regF2, r
20d36 65 67 46 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  egF2);.      sql
20d37 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
20d38 2c 20 4f 50 5f 41 64 64 2c 20 69 4d 65 6d 2c 20  , OP_Add, iMem, 
20d39 69 4d 65 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d  iMem+i+1, regTem
20d3a 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
20d3b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
20d3c 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 54 65 6d  P_AddImm, regTem
20d3d 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71  p, -1);.      sq
20d3e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
20d3f 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 69 4d  v, OP_Divide, iM
20d40 65 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d 70 2c  em+i+1, regTemp,
20d41 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20   regTemp);.     
20d42 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20d43 70 31 28 76 2c 20 4f 50 5f 54 6f 49 6e 74 2c 20  p1(v, OP_ToInt, 
20d44 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20  regTemp);.      
20d45 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20d46 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20  3(v, OP_Concat, 
20d47 72 65 67 54 65 6d 70 2c 20 72 65 67 46 32 2c 20  regTemp, regF2, 
20d48 72 65 67 46 32 29 3b 0a 20 20 20 20 7d 0a 20 20  regF2);.    }.  
20d49 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20d4a 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
20d4b 63 6f 72 64 2c 20 72 65 67 46 69 65 6c 64 73 2c  cord, regFields,
20d4c 20 33 2c 20 72 65 67 52 65 63 2c 20 22 61 61 61   3, regRec, "aaa
20d4d 22 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  ", 0);.    sqlit
20d4e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
20d4f 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53 74  OP_NewRowid, iSt
20d50 61 74 43 75 72 2c 20 72 65 67 52 6f 77 69 64 29  atCur, regRowid)
20d51 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
20d52 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
20d53 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2c 20  sert, iStatCur, 
20d54 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64  regRec, regRowid
20d55 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
20d56 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
20d57 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
20d58 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
20d59 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
20d5a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e    }.}../*.** Gen
20d5b 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
20d5c 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20 6d  will cause the m
20d5d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 64 65 78  ost recent index
20d5e 20 61 6e 61 6c 79 73 69 73 20 74 6f 0a 2a 2a 20   analysis to.** 
20d5f 62 65 20 6c 61 6f 64 65 64 20 69 6e 74 6f 20 69  be laoded into i
20d60 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62  nternal hash tab
20d61 6c 65 73 20 77 68 65 72 65 20 69 73 20 63 61 6e  les where is can
20d62 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61   be used..*/.sta
20d63 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 41 6e 61  tic void loadAna
20d64 6c 79 73 69 73 28 50 61 72 73 65 20 2a 70 50 61  lysis(Parse *pPa
20d65 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20  rse, int iDb){. 
20d66 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
20d67 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
20d68 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
20d69 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20d6a 4f 70 31 28 76 2c 20 4f 50 5f 4c 6f 61 64 41 6e  Op1(v, OP_LoadAn
20d6b 61 6c 79 73 69 73 2c 20 69 44 62 29 3b 0a 20 20  alysis, iDb);.  
20d6c 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
20d6d 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
20d6e 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69  ll do an analysi
20d6f 73 20 6f 66 20 61 6e 20 65 6e 74 69 72 65 20 64  s of an entire d
20d70 61 74 61 62 61 73 65 0a 2a 2f 0a 73 74 61 74 69  atabase.*/.stati
20d71 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 44 61  c void analyzeDa
20d72 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 70 50  tabase(Parse *pP
20d73 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a  arse, int iDb){.
20d74 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
20d75 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 53 63  pParse->db;.  Sc
20d76 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20  hema *pSchema = 
20d77 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
20d78 68 65 6d 61 3b 20 20 20 20 2f 2a 20 53 63 68 65  hema;    /* Sche
20d79 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 69  ma of database i
20d7a 44 62 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  Db */.  HashElem
20d7b 20 2a 6b 3b 0a 20 20 69 6e 74 20 69 53 74 61 74   *k;.  int iStat
20d7c 43 75 72 3b 0a 20 20 69 6e 74 20 69 4d 65 6d 3b  Cur;.  int iMem;
20d7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  ..  sqlite3Begin
20d7e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
20d7f 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
20d80 20 20 69 53 74 61 74 43 75 72 20 3d 20 70 50 61    iStatCur = pPa
20d81 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 6f  rse->nTab++;.  o
20d82 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50 61  penStatTable(pPa
20d83 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74 43  rse, iDb, iStatC
20d84 75 72 2c 20 30 29 3b 0a 20 20 69 4d 65 6d 20 3d  ur, 0);.  iMem =
20d85 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b   pParse->nMem+1;
20d86 0a 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74 65 48  .  for(k=sqliteH
20d87 61 73 68 46 69 72 73 74 28 26 70 53 63 68 65 6d  ashFirst(&pSchem
20d88 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 6b 3b 20  a->tblHash); k; 
20d89 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  k=sqliteHashNext
20d8a 28 6b 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20  (k)){.    Table 
20d8b 2a 70 54 61 62 20 3d 20 28 54 61 62 6c 65 2a 29  *pTab = (Table*)
20d8c 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 6b  sqliteHashData(k
20d8d 29 3b 0a 20 20 20 20 61 6e 61 6c 79 7a 65 4f 6e  );.    analyzeOn
20d8e 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  eTable(pParse, p
20d8f 54 61 62 2c 20 69 53 74 61 74 43 75 72 2c 20 69  Tab, iStatCur, i
20d90 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 6c 6f 61 64  Mem);.  }.  load
20d91 41 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c  Analysis(pParse,
20d92 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   iDb);.}../*.** 
20d93 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
20d94 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e  at will do an an
20d95 61 6c 79 73 69 73 20 6f 66 20 61 20 73 69 6e 67  alysis of a sing
20d96 6c 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 61  le table in.** a
20d97 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74   database..*/.st
20d98 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a  atic void analyz
20d99 65 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  eTable(Parse *pP
20d9a 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
20d9b 62 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20  b){.  int iDb;. 
20d9c 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 0a   int iStatCur;..
20d9d 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d    assert( pTab!=
20d9e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
20d9f 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
20da0 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73  AllMutexes(pPars
20da1 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44 62 20  e->db) );.  iDb 
20da2 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
20da3 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
20da4 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
20da5 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  );.  sqlite3Begi
20da6 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
20da7 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
20da8 0a 20 20 69 53 74 61 74 43 75 72 20 3d 20 70 50  .  iStatCur = pP
20da9 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
20daa 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50  openStatTable(pP
20dab 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74  arse, iDb, iStat
20dac 43 75 72 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  Cur, pTab->zName
20dad 29 3b 0a 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54  );.  analyzeOneT
20dae 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
20daf 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 50 61  b, iStatCur, pPa
20db0 72 73 65 2d 3e 6e 4d 65 6d 2b 31 29 3b 0a 20 20  rse->nMem+1);.  
20db1 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61  loadAnalysis(pPa
20db2 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a  rse, iDb);.}../*
20db3 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
20db4 65 20 66 6f 72 20 74 68 65 20 41 4e 41 4c 59 5a  e for the ANALYZ
20db5 45 20 63 6f 6d 6d 61 6e 64 2e 20 20 54 68 65 20  E command.  The 
20db6 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
20db7 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 68 65  s routine.** whe
20db8 6e 20 69 74 20 72 65 63 6f 67 6e 69 7a 65 73 20  n it recognizes 
20db9 61 6e 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  an ANALYZE comma
20dba 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  nd..**.**       
20dbb 20 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20   ANALYZE        
20dbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20dbd 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20      -- 1.**     
20dbe 20 20 20 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74     ANALYZE  <dat
20dbf 61 62 61 73 65 3e 20 20 20 20 20 20 20 20 20 20  abase>          
20dc0 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20        -- 2.**   
20dc1 20 20 20 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c       ANALYZE  ?<
20dc2 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c  database>.?<tabl
20dc3 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a  ename>  -- 3.**.
20dc4 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73  ** Form 1 causes
20dc5 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20   all indices in 
20dc6 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
20dc7 61 62 61 73 65 73 20 74 6f 20 62 65 20 61 6e 61  abases to be ana
20dc8 6c 79 7a 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32  lyzed..** Form 2
20dc9 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e   analyzes all in
20dca 64 69 63 65 73 20 74 68 65 20 73 69 6e 67 6c 65  dices the single
20dcb 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 2e   database named.
20dcc 0a 2a 2a 20 46 6f 72 6d 20 33 20 61 6e 61 6c 79  .** Form 3 analy
20dcd 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  zes all indices 
20dce 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
20dcf 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e  the named table.
20dd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20dd1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
20dd2 6e 61 6c 79 7a 65 28 50 61 72 73 65 20 2a 70 50  nalyze(Parse *pP
20dd3 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  arse, Token *pNa
20dd4 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  me1, Token *pNam
20dd5 65 32 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  e2){.  sqlite3 *
20dd6 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
20dd7 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e  .  int iDb;.  in
20dd8 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20  t i;.  char *z, 
20dd9 2a 7a 44 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70  *zDb;.  Table *p
20dda 54 61 62 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  Tab;.  Token *pT
20ddb 61 62 6c 65 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20  ableName;..  /* 
20ddc 52 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73  Read the databas
20ddd 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20  e schema. If an 
20dde 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65  error occurs, le
20ddf 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
20de0 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f  sage.  ** and co
20de1 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64  de in pParse and
20de2 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f   return NULL. */
20de3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
20de4 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
20de5 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64  utexes(pParse->d
20de6 62 29 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  b) );.  if( SQLI
20de7 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65  TE_OK!=sqlite3Re
20de8 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
20de9 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
20dea 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70    }..  assert( p
20deb 4e 61 6d 65 32 21 3d 30 20 7c 7c 20 70 4e 61 6d  Name2!=0 || pNam
20dec 65 31 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  e1==0 );.  if( p
20ded 4e 61 6d 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20  Name1==0 ){.    
20dee 2f 2a 20 46 6f 72 6d 20 31 3a 20 20 41 6e 61 6c  /* Form 1:  Anal
20def 79 7a 65 20 65 76 65 72 79 74 68 69 6e 67 20 2a  yze everything *
20df0 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
20df1 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
20df2 20 20 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29        if( i==1 )
20df3 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 44   continue;  /* D
20df4 6f 20 6e 6f 74 20 61 6e 61 6c 79 7a 65 20 74 68  o not analyze th
20df5 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20  e TEMP database 
20df6 2a 2f 0a 20 20 20 20 20 20 61 6e 61 6c 79 7a 65  */.      analyze
20df7 44 61 74 61 62 61 73 65 28 70 50 61 72 73 65 2c  Database(pParse,
20df8 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   i);.    }.  }el
20df9 73 65 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e  se if( pName2->n
20dfa 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f  ==0 ){.    /* Fo
20dfb 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65 20 74  rm 2:  Analyze t
20dfc 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 74  he database or t
20dfd 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a 20 20  able named */.  
20dfe 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46    iDb = sqlite3F
20dff 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 31  indDb(db, pName1
20e00 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d  );.    if( iDb>=
20e01 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61 6c 79  0 ){.      analy
20e02 7a 65 44 61 74 61 62 61 73 65 28 70 50 61 72 73  zeDatabase(pPars
20e03 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 65 6c  e, iDb);.    }el
20e04 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71  se{.      z = sq
20e05 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
20e06 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a  en(db, pName1);.
20e07 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20        if( z ){. 
20e08 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 73 71         pTab = sq
20e09 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
20e0a 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20 30  (pParse, 0, z, 0
20e0b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
20e0c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b  e3DbFree(db, z);
20e0d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 61  .        if( pTa
20e0e 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  b ){.          a
20e0f 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 72  nalyzeTable(pPar
20e10 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  se, pTab);.     
20e11 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
20e12 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
20e13 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61 6c 79  /* Form 3: Analy
20e14 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71 75 61  ze the fully qua
20e15 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d  lified table nam
20e16 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73  e */.    iDb = s
20e17 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
20e18 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
20e19 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61 62 6c  , pName2, &pTabl
20e1a 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  eName);.    if( 
20e1b 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  iDb>=0 ){.      
20e1c 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
20e1d 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  b].zName;.      
20e1e 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  z = sqlite3NameF
20e1f 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61  romToken(db, pTa
20e20 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  bleName);.      
20e21 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20  if( z ){.       
20e22 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
20e23 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
20e24 65 2c 20 30 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20  e, 0, z, zDb);. 
20e25 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
20e26 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20  Free(db, z);.   
20e27 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b       if( pTab ){
20e28 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79  .          analy
20e29 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  zeTable(pParse, 
20e2a 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 7d  pTab);.        }
20e2b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 20  .      }.    }  
20e2c 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55   .  }.}../*.** U
20e2d 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e 66 6f  sed to pass info
20e2e 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65  rmation from the
20e2f 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64 65 72   analyzer reader
20e30 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 0a   through to the.
20e31 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74  ** callback rout
20e32 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ine..*/.typedef 
20e33 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69 73 49  struct analysisI
20e34 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f  nfo analysisInfo
20e35 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69  ;.struct analysi
20e36 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65  sInfo {.  sqlite
20e37 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63  3 *db;.  const c
20e38 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b 0a  har *zDatabase;.
20e39 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 63  };../*.** This c
20e3a 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
20e3b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
20e3c 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65 61 64   index when read
20e3d 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  ing the.** sqlit
20e3e 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e 20 20  e_stat1 table.  
20e3f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b  .**.**     argv[
20e40 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74 68 65  0] = name of the
20e41 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 61 72   index.**     ar
20e42 67 76 5b 31 5d 20 3d 20 72 65 73 75 6c 74 73 20  gv[1] = results 
20e43 6f 66 20 61 6e 61 6c 79 73 69 73 20 2d 20 6f 6e  of analysis - on
20e44 20 69 6e 74 65 67 65 72 20 66 6f 72 20 65 61 63   integer for eac
20e45 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74  h column.*/.stat
20e46 69 63 20 69 6e 74 20 61 6e 61 6c 79 73 69 73 4c  ic int analysisL
20e47 6f 61 64 65 72 28 76 6f 69 64 20 2a 70 44 61 74  oader(void *pDat
20e48 61 2c 20 69 6e 74 20 61 72 67 63 2c 20 63 68 61  a, int argc, cha
20e49 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 20 2a  r **argv, char *
20e4a 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 61 6e 61  *NotUsed){.  ana
20e4b 6c 79 73 69 73 49 6e 66 6f 20 2a 70 49 6e 66 6f  lysisInfo *pInfo
20e4c 20 3d 20 28 61 6e 61 6c 79 73 69 73 49 6e 66 6f   = (analysisInfo
20e4d 2a 29 70 44 61 74 61 3b 0a 20 20 49 6e 64 65 78  *)pData;.  Index
20e4e 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20   *pIndex;.  int 
20e4f 69 2c 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64  i, c;.  unsigned
20e50 20 69 6e 74 20 76 3b 0a 20 20 63 6f 6e 73 74 20   int v;.  const 
20e51 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65  char *z;..  asse
20e52 72 74 28 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20  rt( argc==2 );. 
20e53 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
20e54 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 67 63  R2(NotUsed, argc
20e55 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 76 3d 3d  );..  if( argv==
20e56 30 20 7c 7c 20 61 72 67 76 5b 30 5d 3d 3d 30 20  0 || argv[0]==0 
20e57 7c 7c 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b  || argv[1]==0 ){
20e58 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
20e59 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71   }.  pIndex = sq
20e5a 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 70  lite3FindIndex(p
20e5b 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 5b 30  Info->db, argv[0
20e5c 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 61 62  ], pInfo->zDatab
20e5d 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49 6e 64  ase);.  if( pInd
20e5e 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ex==0 ){.    ret
20e5f 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 3d  urn 0;.  }.  z =
20e60 20 61 72 67 76 5b 31 5d 3b 0a 20 20 66 6f 72 28   argv[1];.  for(
20e61 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d 70 49  i=0; *z && i<=pI
20e62 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  ndex->nColumn; i
20e63 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 30 3b 0a  ++){.    v = 0;.
20e64 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b      while( (c=z[
20e65 30 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27  0])>='0' && c<='
20e66 39 27 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20  9' ){.      v = 
20e67 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a  v*10 + c - '0';.
20e68 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d        z++;.    }
20e69 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 52  .    pIndex->aiR
20e6a 6f 77 45 73 74 5b 69 5d 20 3d 20 76 3b 0a 20 20  owEst[i] = v;.  
20e6b 20 20 69 66 28 20 2a 7a 3d 3d 27 20 27 20 29 20    if( *z==' ' ) 
20e6c 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  z++;.  }.  retur
20e6d 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  n 0;.}../*.** Lo
20e6e 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ad the content o
20e6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
20e70 74 31 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68  t1 table into th
20e71 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62  e index hash tab
20e72 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  les..*/.SQLITE_P
20e73 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
20e74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73  e3AnalysisLoad(s
20e75 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
20e76 69 44 62 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73  iDb){.  analysis
20e77 49 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20 20 48 61  Info sInfo;.  Ha
20e78 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 63 68 61  shElem *i;.  cha
20e79 72 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 72  r *zSql;.  int r
20e7a 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44  c;..  assert( iD
20e7b 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e  b>=0 && iDb<db->
20e7c 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nDb );.  assert(
20e7d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42   db->aDb[iDb].pB
20e7e 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  t!=0 );.  assert
20e7f 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
20e80 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62  ldsMutex(db->aDb
20e81 5b 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20  [iDb].pBt) );.. 
20e82 20 2f 2a 20 43 6c 65 61 72 20 61 6e 79 20 70 72   /* Clear any pr
20e83 69 6f 72 20 73 74 61 74 69 73 74 69 63 73 20 2a  ior statistics *
20e84 2f 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65  /.  for(i=sqlite
20e85 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61  HashFirst(&db->a
20e86 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
20e87 3e 69 64 78 48 61 73 68 29 3b 69 3b 69 3d 73 71  >idxHash);i;i=sq
20e88 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 29  liteHashNext(i))
20e89 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  {.    Index *pId
20e8a 78 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61  x = sqliteHashDa
20e8b 74 61 28 69 29 3b 0a 20 20 20 20 73 71 6c 69 74  ta(i);.    sqlit
20e8c 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28  e3DefaultRowEst(
20e8d 70 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  pIdx);.  }..  /*
20e8e 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73   Check to make s
20e8f 75 72 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73  ure the sqlite_s
20e90 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74  tat1 table exist
20e91 73 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e 64 62  ss */.  sInfo.db
20e92 20 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f 2e 7a   = db;.  sInfo.z
20e93 44 61 74 61 62 61 73 65 20 3d 20 64 62 2d 3e 61  Database = db->a
20e94 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
20e95 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
20e96 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74  Table(db, "sqlit
20e97 65 5f 73 74 61 74 31 22 2c 20 73 49 6e 66 6f 2e  e_stat1", sInfo.
20e98 7a 44 61 74 61 62 61 73 65 29 3d 3d 30 20 29 7b  zDatabase)==0 ){
20e99 0a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  .     return SQL
20e9a 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
20e9b 0a 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77 20 73  .  /* Load new s
20e9c 74 61 74 69 73 74 69 63 73 20 6f 75 74 20 6f 66  tatistics out of
20e9d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
20e9e 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 53 71  1 table */.  zSq
20e9f 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  l = sqlite3MPrin
20ea0 74 66 28 64 62 2c 20 22 53 45 4c 45 43 54 20 69  tf(db, "SELECT i
20ea1 64 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 25 51  dx, stat FROM %Q
20ea2 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 0a  .sqlite_stat1",.
20ea3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ea4 20 20 20 20 20 20 20 20 73 49 6e 66 6f 2e 7a 44          sInfo.zD
20ea5 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20  atabase);.  if( 
20ea6 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72  zSql==0 ){.    r
20ea7 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
20ea8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 28  ;.  }else{.    (
20ea9 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
20eaa 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72  tyOff(db);.    r
20eab 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
20eac 28 64 62 2c 20 7a 53 71 6c 2c 20 61 6e 61 6c 79  (db, zSql, analy
20ead 73 69 73 4c 6f 61 64 65 72 2c 20 26 73 49 6e 66  sisLoader, &sInf
20eae 6f 2c 20 30 29 3b 0a 20 20 20 20 28 76 6f 69 64  o, 0);.    (void
20eaf 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
20eb0 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (db);.    sqlite
20eb1 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c  3DbFree(db, zSql
20eb2 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
20eb3 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 20 64 62  QLITE_NOMEM ) db
20eb4 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
20eb5 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
20eb6 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20   rc;.}...#endif 
20eb7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  /* SQLITE_OMIT_A
20eb8 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  NALYZE */../****
20eb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
20eba 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a  f analyze.c ****
20ebb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ebc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ebd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
20ebe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
20ebf 20 66 69 6c 65 20 61 74 74 61 63 68 2e 63 20 2a   file attach.c *
20ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ec1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
20ec3 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a   2003 April 6.**
20ec4 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
20ec5 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
20ec6 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
20ec7 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
20ec8 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
20ec9 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
20eca 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
20ecb 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
20ecc 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
20ecd 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
20ece 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
20ecf 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
20ed0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
20ed1 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
20ed2 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
20ed3 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
20ed4 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
20ed5 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
20ed6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ed7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
20eda 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
20edb 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
20edc 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 41 54  implement the AT
20edd 54 41 43 48 20 61 6e 64 20 44 45 54 41 43 48 20  TACH and DETACH 
20ede 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20  commands..**.** 
20edf 24 49 64 3a 20 61 74 74 61 63 68 2e 63 2c 76 20  $Id: attach.c,v 
20ee0 31 2e 39 30 20 32 30 30 39 2f 30 35 2f 30 31 20  1.90 2009/05/01 
20ee1 30 36 3a 31 39 3a 32 31 20 64 61 6e 69 65 6c 6b  06:19:21 danielk
20ee2 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23  1977 Exp $.*/..#
20ee3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20ee4 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a 2a 20  IT_ATTACH./*.** 
20ee5 52 65 73 6f 6c 76 65 20 61 6e 20 65 78 70 72 65  Resolve an expre
20ee6 73 73 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70  ssion that was p
20ee7 61 72 74 20 6f 66 20 61 6e 20 41 54 54 41 43 48  art of an ATTACH
20ee8 20 6f 72 20 44 45 54 41 43 48 20 73 74 61 74 65   or DETACH state
20ee9 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a 20 69 73  ment. This.** is
20eea 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72   slightly differ
20eeb 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f 6c 76 69  ent from resolvi
20eec 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c 20  ng a normal SQL 
20eed 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 65 63 61  expression, beca
20eee 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 69 64  use simple.** id
20eef 65 6e 74 69 66 69 65 72 73 20 61 72 65 20 74 72  entifiers are tr
20ef0 65 61 74 65 64 20 61 73 20 73 74 72 69 6e 67 73  eated as strings
20ef1 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 63  , not possible c
20ef2 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 20 61  olumn names or a
20ef3 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e  liases..**.** i.
20ef4 65 2e 20 69 66 20 74 68 65 20 70 61 72 73 65 72  e. if the parser
20ef5 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   sees:.**.**    
20ef6 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 45   ATTACH DATABASE
20ef7 20 61 62 63 20 41 53 20 64 65 66 0a 2a 2a 0a 2a   abc AS def.**.*
20ef8 2a 20 69 74 20 74 72 65 61 74 73 20 74 68 65 20  * it treats the 
20ef9 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20  two expressions 
20efa 61 73 20 6c 69 74 65 72 61 6c 20 73 74 72 69 6e  as literal strin
20efb 67 73 20 27 61 62 63 27 20 61 6e 64 20 27 64 65  gs 'abc' and 'de
20efc 66 27 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a  f' instead of.**
20efd 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 63 6f 6c   looking for col
20efe 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  umns of the same
20eff 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   name..**.** Thi
20f00 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74  s only applies t
20f01 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20  o the root node 
20f02 6f 66 20 70 45 78 70 72 2c 20 73 6f 20 74 68 65  of pExpr, so the
20f03 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a   statement:.**.*
20f04 2a 20 20 20 20 20 41 54 54 41 43 48 20 44 41 54  *     ATTACH DAT
20f05 41 42 41 53 45 20 61 62 63 7c 7c 64 65 66 20 41  ABASE abc||def A
20f06 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20 77 69  S 'db2'.**.** wi
20f07 6c 6c 20 66 61 69 6c 20 62 65 63 61 75 73 65 20  ll fail because 
20f08 6e 65 69 74 68 65 72 20 61 62 63 20 6f 72 20 64  neither abc or d
20f09 65 66 20 63 61 6e 20 62 65 20 72 65 73 6f 6c 76  ef can be resolv
20f0a 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
20f0b 74 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45  t resolveAttachE
20f0c 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20  xpr(NameContext 
20f0d 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20 2a 70 45  *pName, Expr *pE
20f0e 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20 72 63 20  xpr).{.  int rc 
20f0f 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
20f10 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20  f( pExpr ){.    
20f11 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
20f12 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 72 63  K_ID ){.      rc
20f13 20 3d 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76   = sqlite3Resolv
20f14 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 61 6d 65  eExprNames(pName
20f15 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  , pExpr);.      
20f16 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
20f17 4b 20 26 26 20 21 73 71 6c 69 74 65 33 45 78 70  K && !sqlite3Exp
20f18 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 45 78 70  rIsConstant(pExp
20f19 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  r) ){.        sq
20f1a 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 4e  lite3ErrorMsg(pN
20f1b 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20 22 69 6e  ame->pParse, "in
20f1c 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c 22 25 54  valid name: \"%T
20f1d 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e 73 70 61  \"", &pExpr->spa
20f1e 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  n);.        retu
20f1f 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
20f20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
20f21 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  se{.      pExpr-
20f22 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b  >op = TK_STRING;
20f23 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
20f24 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
20f25 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 6e   An SQL user-fun
20f26 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
20f27 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20   to do the work 
20f28 6f 66 20 61 6e 20 41 54 54 41 43 48 20 73 74 61  of an ATTACH sta
20f29 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 74  tement. The.** t
20f2a 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74  hree arguments t
20f2b 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63  o the function c
20f2c 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f  ome directly fro
20f2d 6d 20 61 6e 20 61 74 74 61 63 68 20 73 74 61 74  m an attach stat
20f2e 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ement:.**.**    
20f2f 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 45   ATTACH DATABASE
20f30 20 78 20 41 53 20 79 20 4b 45 59 20 7a 0a 2a 2a   x AS y KEY z.**
20f31 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73  .**     SELECT s
20f32 71 6c 69 74 65 5f 61 74 74 61 63 68 28 78 2c 20  qlite_attach(x, 
20f33 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 20 74  y, z).**.** If t
20f34 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 4b 45 59  he optional "KEY
20f35 20 7a 22 20 73 79 6e 74 61 78 20 69 73 20 6f 6d   z" syntax is om
20f36 69 74 74 65 64 2c 20 61 6e 20 53 51 4c 20 4e 55  itted, an SQL NU
20f37 4c 4c 20 69 73 20 70 61 73 73 65 64 20 61 73 20  LL is passed as 
20f38 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67  the.** third arg
20f39 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ument..*/.static
20f3a 20 76 6f 69 64 20 61 74 74 61 63 68 46 75 6e 63   void attachFunc
20f3b 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
20f3c 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
20f3d 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
20f3e 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
20f3f 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rgv.){.  int i;.
20f40 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20    int rc = 0;.  
20f41 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
20f42 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
20f43 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
20f44 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
20f45 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63  zName;.  const c
20f46 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 44 62  har *zFile;.  Db
20f47 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61 72 20 2a   *aNew;.  char *
20f48 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a 0a 20 20  zErrDyn = 0;..  
20f49 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
20f4a 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 7a 46  (NotUsed);..  zF
20f4b 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ile = (const cha
20f4c 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
20f4d 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
20f4e 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73  .  zName = (cons
20f4f 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
20f50 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
20f51 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c  [1]);.  if( zFil
20f52 65 3d 3d 30 20 29 20 7a 46 69 6c 65 20 3d 20 22  e==0 ) zFile = "
20f53 22 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d  ";.  if( zName==
20f54 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a  0 ) zName = "";.
20f55 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20  .  /* Check for 
20f56 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72  the following er
20f57 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rors:.  **.  ** 
20f58 20 20 20 20 2a 20 54 6f 6f 20 6d 61 6e 79 20 61      * Too many a
20f59 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
20f5a 73 2c 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54 72  s,.  **     * Tr
20f5b 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 65 6e  ansaction curren
20f5c 74 6c 79 20 6f 70 65 6e 0a 20 20 2a 2a 20 20 20  tly open.  **   
20f5d 20 20 2a 20 53 70 65 63 69 66 69 65 64 20 64 61    * Specified da
20f5e 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6c 72 65  tabase name alre
20f5f 61 64 79 20 62 65 69 6e 67 20 75 73 65 64 2e 0a  ady being used..
20f60 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e    */.  if( db->n
20f61 44 62 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  Db>=db->aLimit[S
20f62 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41  QLITE_LIMIT_ATTA
20f63 43 48 45 44 5d 2b 32 20 29 7b 0a 20 20 20 20 7a  CHED]+2 ){.    z
20f64 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33  ErrDyn = sqlite3
20f65 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 6f 6f  MPrintf(db, "too
20f66 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20 64   many attached d
20f67 61 74 61 62 61 73 65 73 20 2d 20 6d 61 78 20 25  atabases - max %
20f68 64 22 2c 20 0a 20 20 20 20 20 20 64 62 2d 3e 61  d", .      db->a
20f69 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
20f6a 49 54 5f 41 54 54 41 43 48 45 44 5d 0a 20 20 20  IT_ATTACHED].   
20f6b 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74   );.    goto att
20f6c 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20  ach_error;.  }. 
20f6d 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f   if( !db->autoCo
20f6e 6d 6d 69 74 20 29 7b 0a 20 20 20 20 7a 45 72 72  mmit ){.    zErr
20f6f 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Dyn = sqlite3MPr
20f70 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f 74  intf(db, "cannot
20f71 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65   ATTACH database
20f72 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74   within transact
20f73 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  ion");.    goto 
20f74 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  attach_error;.  
20f75 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64  }.  for(i=0; i<d
20f76 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
20f77 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d 3e    char *z = db->
20f78 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  aDb[i].zName;.  
20f79 20 20 61 73 73 65 72 74 28 20 7a 20 26 26 20 7a    assert( z && z
20f7a 4e 61 6d 65 20 29 3b 0a 20 20 20 20 69 66 28 20  Name );.    if( 
20f7b 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
20f7c 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  , zName)==0 ){. 
20f7d 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73       zErrDyn = s
20f7e 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
20f7f 2c 20 22 64 61 74 61 62 61 73 65 20 25 73 20 69  , "database %s i
20f80 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73 65  s already in use
20f81 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ", zName);.     
20f82 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72   goto attach_err
20f83 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  or;.    }.  }.. 
20f84 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65   /* Allocate the
20f85 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 74 68   new entry in th
20f86 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61  e db->aDb[] arra
20f87 79 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 65  y and initialise
20f88 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a 2a   the schema.  **
20f89 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 20 20   hash tables..  
20f8a 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62  */.  if( db->aDb
20f8b 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20  ==db->aDbStatic 
20f8c 29 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71  ){.    aNew = sq
20f8d 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
20f8e 28 64 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e  (db, sizeof(db->
20f8f 61 44 62 5b 30 5d 29 2a 33 20 29 3b 0a 20 20 20  aDb[0])*3 );.   
20f90 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 72   if( aNew==0 ) r
20f91 65 74 75 72 6e 3b 0a 20 20 20 20 6d 65 6d 63 70  eturn;.    memcp
20f92 79 28 61 4e 65 77 2c 20 64 62 2d 3e 61 44 62 2c  y(aNew, db->aDb,
20f93 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b   sizeof(db->aDb[
20f94 30 5d 29 2a 32 29 3b 0a 20 20 7d 65 6c 73 65 7b  0])*2);.  }else{
20f95 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69  .    aNew = sqli
20f96 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c  te3DbRealloc(db,
20f97 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65 6f 66   db->aDb, sizeof
20f98 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 28 64 62  (db->aDb[0])*(db
20f99 2d 3e 6e 44 62 2b 31 29 20 29 3b 0a 20 20 20 20  ->nDb+1) );.    
20f9a 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 72 65  if( aNew==0 ) re
20f9b 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  turn;.  }.  db->
20f9c 61 44 62 20 3d 20 61 4e 65 77 3b 0a 20 20 61 4e  aDb = aNew;.  aN
20f9d 65 77 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64 62  ew = &db->aDb[db
20f9e 2d 3e 6e 44 62 5d 3b 0a 20 20 6d 65 6d 73 65 74  ->nDb];.  memset
20f9f 28 61 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66  (aNew, 0, sizeof
20fa0 28 2a 61 4e 65 77 29 29 3b 0a 0a 20 20 2f 2a 20  (*aNew));..  /* 
20fa1 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  Open the databas
20fa2 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 62  e file. If the b
20fa3 74 72 65 65 20 69 73 20 73 75 63 63 65 73 73 66  tree is successf
20fa4 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 75 73 65  ully opened, use
20fa5 0a 20 20 2a 2a 20 69 74 20 74 6f 20 6f 62 74 61  .  ** it to obta
20fa6 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
20fa7 73 63 68 65 6d 61 2e 20 41 74 20 74 68 69 73 20  schema. At this 
20fa8 70 6f 69 6e 74 20 74 68 65 20 73 63 68 65 6d 61  point the schema
20fa9 20 6d 61 79 0a 20 20 2a 2a 20 6f 72 20 6d 61 79   may.  ** or may
20faa 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69   not be initiali
20fab 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  sed..  */.  rc =
20fac 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63   sqlite3BtreeFac
20fad 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c 65 2c 20  tory(db, zFile, 
20fae 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  0, SQLITE_DEFAUL
20faf 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 0a 20 20  T_CACHE_SIZE,.  
20fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20fb1 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6f 70 65           db->ope
20fb2 6e 46 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f  nFlags | SQLITE_
20fb3 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20  OPEN_MAIN_DB,.  
20fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20fb5 20 20 20 20 20 20 20 20 20 26 61 4e 65 77 2d 3e           &aNew->
20fb6 70 42 74 29 3b 0a 20 20 64 62 2d 3e 6e 44 62 2b  pBt);.  db->nDb+
20fb7 2b 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  +;.  if( rc==SQL
20fb8 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29  ITE_CONSTRAINT )
20fb9 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
20fba 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7a 45 72  E_ERROR;.    zEr
20fbb 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50  rDyn = sqlite3MP
20fbc 72 69 6e 74 66 28 64 62 2c 20 22 64 61 74 61 62  rintf(db, "datab
20fbd 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ase is already a
20fbe 74 74 61 63 68 65 64 22 29 3b 0a 20 20 7d 65 6c  ttached");.  }el
20fbf 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  se if( rc==SQLIT
20fc0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 61 67 65  E_OK ){.    Page
20fc1 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 61  r *pPager;.    a
20fc2 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73  New->pSchema = s
20fc3 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28  qlite3SchemaGet(
20fc4 64 62 2c 20 61 4e 65 77 2d 3e 70 42 74 29 3b 0a  db, aNew->pBt);.
20fc5 20 20 20 20 69 66 28 20 21 61 4e 65 77 2d 3e 70      if( !aNew->p
20fc6 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20  Schema ){.      
20fc7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
20fc8 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  M;.    }else if(
20fc9 20 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d 3e   aNew->pSchema->
20fca 66 69 6c 65 5f 66 6f 72 6d 61 74 20 26 26 20 61  file_format && a
20fcb 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 65 6e  New->pSchema->en
20fcc 63 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a 20 20  c!=ENC(db) ){.  
20fcd 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71      zErrDyn = sq
20fce 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
20fcf 20 0a 20 20 20 20 20 20 20 20 22 61 74 74 61 63   .        "attac
20fd0 68 65 64 20 64 61 74 61 62 61 73 65 73 20 6d 75  hed databases mu
20fd1 73 74 20 75 73 65 20 74 68 65 20 73 61 6d 65 20  st use the same 
20fd2 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73  text encoding as
20fd3 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 22 29   main database")
20fd4 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
20fd5 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d  ITE_ERROR;.    }
20fd6 0a 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71  .    pPager = sq
20fd7 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
20fd8 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20  aNew->pBt);.    
20fd9 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
20fda 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20  ingMode(pPager, 
20fdb 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65  db->dfltLockMode
20fdc 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  );.    sqlite3Pa
20fdd 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70  gerJournalMode(p
20fde 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4a  Pager, db->dfltJ
20fdf 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d  ournalMode);.  }
20fe0 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d  .  aNew->zName =
20fe1 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
20fe2 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 61  (db, zName);.  a
20fe3 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65  New->safety_leve
20fe4 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53 51 4c 49  l = 3;..#if SQLI
20fe5 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 7b  TE_HAS_CODEC.  {
20fe6 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20  .    extern int 
20fe7 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61  sqlite3CodecAtta
20fe8 63 68 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ch(sqlite3*, int
20fe9 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
20fea 6e 74 29 3b 0a 20 20 20 20 65 78 74 65 72 6e 20  nt);.    extern 
20feb 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65  void sqlite3Code
20fec 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33 2a  cGetKey(sqlite3*
20fed 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69  , int, void**, i
20fee 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b  nt*);.    int nK
20fef 65 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b  ey;.    char *zK
20ff0 65 79 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d 20  ey;.    int t = 
20ff1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
20ff2 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  pe(argv[2]);.   
20ff3 20 73 77 69 74 63 68 28 20 74 20 29 7b 0a 20 20   switch( t ){.  
20ff4 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
20ff5 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 20 20 63  INTEGER:.      c
20ff6 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ase SQLITE_FLOAT
20ff7 3a 0a 20 20 20 20 20 20 20 20 7a 45 72 72 44 79  :.        zErrDy
20ff8 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  n = sqlite3DbStr
20ff9 44 75 70 28 64 62 2c 20 22 49 6e 76 61 6c 69 64  Dup(db, "Invalid
20ffa 20 6b 65 79 20 76 61 6c 75 65 22 29 3b 0a 20 20   key value");.  
20ffb 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
20ffc 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20  E_ERROR;.       
20ffd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
20ffe 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49  .      case SQLI
20fff 54 45 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 63  TE_TEXT:.      c
21000 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a  ase SQLITE_BLOB:
21001 0a 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d 20  .        nKey = 
21002 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
21003 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  tes(argv[2]);.  
21004 20 20 20 20 20 20 7a 4b 65 79 20 3d 20 28 63 68        zKey = (ch
21005 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
21006 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d 29  ue_blob(argv[2])
21007 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21008 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c  3CodecAttach(db,
21009 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79   db->nDb-1, zKey
2100a 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  , nKey);.       
2100b 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 63   break;..      c
2100c 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a  ase SQLITE_NULL:
2100d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 6b  .        /* No k
2100e 65 79 20 73 70 65 63 69 66 69 65 64 2e 20 20 55  ey specified.  U
2100f 73 65 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20  se the key from 
21010 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
21011 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c  e */.        sql
21012 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28  ite3CodecGetKey(
21013 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26  db, 0, (void**)&
21014 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20  zKey, &nKey);.  
21015 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64        sqlite3Cod
21016 65 63 41 74 74 61 63 68 28 64 62 2c 20 64 62 2d  ecAttach(db, db-
21017 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e 4b  >nDb-1, zKey, nK
21018 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ey);.        bre
21019 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  ak;.    }.  }.#e
2101a 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ndif..  /* If th
2101b 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65  e file was opene
2101c 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20  d successfully, 
2101d 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 20  read the schema 
2101e 66 6f 72 20 74 68 65 20 6e 65 77 20 64 61 74 61  for the new data
2101f 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68  base..  ** If th
21020 69 73 20 66 61 69 6c 73 2c 20 6f 72 20 69 66 20  is fails, or if 
21021 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65  opening the file
21022 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 63 6c   failed, then cl
21023 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e 64  ose the file and
21024 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 68   .  ** remove th
21025 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  e entry from the
21026 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61 79   db->aDb[] array
21027 2e 20 69 2e 65 2e 20 70 75 74 20 65 76 65 72 79  . i.e. put every
21028 74 68 69 6e 67 20 62 61 63 6b 20 74 68 65 20 77  thing back the w
21029 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e 64  ay.  ** we found
2102a 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
2102b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2102c 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
2102d 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
2102e 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
2102f 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20  EnterAll(db);.  
21030 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e    rc = sqlite3In
21031 69 74 28 64 62 2c 20 26 7a 45 72 72 44 79 6e 29  it(db, &zErrDyn)
21032 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
21033 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
21034 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
21035 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
21036 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a    }.  if( rc ){.
21037 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 64 62      int iDb = db
21038 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20 20 20 61  ->nDb - 1;.    a
21039 73 73 65 72 74 28 20 69 44 62 3e 3d 32 20 29 3b  ssert( iDb>=2 );
2103a 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62  .    if( db->aDb
2103b 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a 20 20 20  [iDb].pBt ){.   
2103c 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
2103d 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 69 44 62  lose(db->aDb[iDb
2103e 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62  ].pBt);.      db
2103f 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 3d  ->aDb[iDb].pBt =
21040 20 30 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44   0;.      db->aD
21041 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 3d  b[iDb].pSchema =
21042 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71   0;.    }.    sq
21043 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
21044 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b  alSchema(db, 0);
21045 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20 3d 20 69  .    db->nDb = i
21046 44 62 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  Db;.    if( rc==
21047 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
21048 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
21049 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
2104a 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2104b 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 1;.      sqli
2104c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45  te3DbFree(db, zE
2104d 72 72 44 79 6e 29 3b 0a 20 20 20 20 20 20 7a 45  rrDyn);.      zE
2104e 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d  rrDyn = sqlite3M
2104f 50 72 69 6e 74 66 28 64 62 2c 20 22 6f 75 74 20  Printf(db, "out 
21050 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20  of memory");.   
21051 20 7d 65 6c 73 65 20 69 66 28 20 7a 45 72 72 44   }else if( zErrD
21052 79 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a  yn==0 ){.      z
21053 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33  ErrDyn = sqlite3
21054 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 75 6e 61  MPrintf(db, "una
21055 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61  ble to open data
21056 62 61 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c 65  base: %s", zFile
21057 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74  );.    }.    got
21058 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a  o attach_error;.
21059 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 3b    }.  .  return;
2105a 0a 0a 61 74 74 61 63 68 5f 65 72 72 6f 72 3a 0a  ..attach_error:.
2105b 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65    /* Return an e
2105c 72 72 6f 72 20 69 66 20 77 65 20 67 65 74 20 68  rror if we get h
2105d 65 72 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45 72  ere */.  if( zEr
2105e 72 44 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69  rDyn ){.    sqli
2105f 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
21060 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79  (context, zErrDy
21061 6e 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69  n, -1);.    sqli
21062 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45  te3DbFree(db, zE
21063 72 72 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 66  rrDyn);.  }.  if
21064 28 20 72 63 20 29 20 73 71 6c 69 74 65 33 5f 72  ( rc ) sqlite3_r
21065 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
21066 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a 7d  (context, rc);.}
21067 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 75  ../*.** An SQL u
21068 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 67  ser-function reg
21069 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20 74 68  istered to do th
2106a 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 44 45 54  e work of an DET
2106b 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  ACH statement. T
2106c 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 67 75  he.** three argu
2106d 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e  ments to the fun
2106e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65 63  ction come direc
2106f 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 74 61 63  tly from a detac
21070 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a  h statement:.**.
21071 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 44 41  **     DETACH DA
21072 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a 20 20  TABASE x.**.**  
21073 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65     SELECT sqlite
21074 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f 0a 73 74  _detach(x).*/.st
21075 61 74 69 63 20 76 6f 69 64 20 64 65 74 61 63 68  atic void detach
21076 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
21077 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
21078 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
21079 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2107a 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e   **argv.){.  con
2107b 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  st char *zName =
2107c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
2107d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2107e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71  t(argv[0]);.  sq
2107f 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
21080 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
21081 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
21082 20 20 69 6e 74 20 69 3b 0a 20 20 44 62 20 2a 70    int i;.  Db *p
21083 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a  Db = 0;.  char z
21084 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 55 4e 55  Err[128];..  UNU
21085 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
21086 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 7a  tUsed);..  if( z
21087 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20  Name==0 ) zName 
21088 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  = "";.  for(i=0;
21089 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
2108a 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d  {.    pDb = &db-
2108b 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28  >aDb[i];.    if(
2108c 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 20 63   pDb->pBt==0 ) c
2108d 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28  ontinue;.    if(
2108e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2108f 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  pDb->zName, zNam
21090 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  e)==0 ) break;. 
21091 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d 64 62 2d   }..  if( i>=db-
21092 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69  >nDb ){.    sqli
21093 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
21094 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20  eof(zErr),zErr, 
21095 22 6e 6f 20 73 75 63 68 20 64 61 74 61 62 61 73  "no such databas
21096 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  e: %s", zName);.
21097 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f      goto detach_
21098 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28  error;.  }.  if(
21099 20 69 3c 32 20 29 7b 0a 20 20 20 20 73 71 6c 69   i<2 ){.    sqli
2109a 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
2109b 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20  eof(zErr),zErr, 
2109c 22 63 61 6e 6e 6f 74 20 64 65 74 61 63 68 20 64  "cannot detach d
2109d 61 74 61 62 61 73 65 20 25 73 22 2c 20 7a 4e 61  atabase %s", zNa
2109e 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65  me);.    goto de
2109f 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
210a0 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43    if( !db->autoC
210a1 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c  ommit ){.    sql
210a2 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
210a3 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72  zeof(zErr), zErr
210a4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
210a5 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 44         "cannot D
210a6 45 54 41 43 48 20 64 61 74 61 62 61 73 65 20 77  ETACH database w
210a7 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 69 6f  ithin transactio
210a8 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65  n");.    goto de
210a9 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
210aa 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72    if( sqlite3Btr
210ab 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28  eeIsInReadTrans(
210ac 70 44 62 2d 3e 70 42 74 29 20 7c 7c 20 73 71 6c  pDb->pBt) || sql
210ad 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63  ite3BtreeIsInBac
210ae 6b 75 70 28 70 44 62 2d 3e 70 42 74 29 20 29 7b  kup(pDb->pBt) ){
210af 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
210b0 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
210b1 72 29 2c 7a 45 72 72 2c 20 22 64 61 74 61 62 61  r),zErr, "databa
210b2 73 65 20 25 73 20 69 73 20 6c 6f 63 6b 65 64 22  se %s is locked"
210b3 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  , zName);.    go
210b4 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b  to detach_error;
210b5 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42  .  }..  sqlite3B
210b6 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70  treeClose(pDb->p
210b7 42 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42 74 20  Bt);.  pDb->pBt 
210b8 3d 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53 63 68  = 0;.  pDb->pSch
210b9 65 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  ema = 0;.  sqlit
210ba 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
210bb 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
210bc 72 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63 68 5f  return;..detach_
210bd 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33  error:.  sqlite3
210be 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
210bf 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29  ntext, zErr, -1)
210c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
210c1 70 72 6f 63 65 64 75 72 65 20 67 65 6e 65 72 61  procedure genera
210c2 74 65 73 20 56 44 42 45 20 63 6f 64 65 20 66 6f  tes VDBE code fo
210c3 72 20 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f 63  r a single invoc
210c4 61 74 69 6f 6e 20 6f 66 20 65 69 74 68 65 72 20  ation of either 
210c5 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 64 65  the.** sqlite_de
210c6 74 61 63 68 28 29 20 6f 72 20 73 71 6c 69 74 65  tach() or sqlite
210c7 5f 61 74 74 61 63 68 28 29 20 53 51 4c 20 75 73  _attach() SQL us
210c8 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f  er functions..*/
210c9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
210ca 65 41 74 74 61 63 68 28 0a 20 20 50 61 72 73 65  eAttach(.  Parse
210cb 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
210cc 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
210cd 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 74  ntext */.  int t
210ce 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ype,            
210cf 2f 2a 20 45 69 74 68 65 72 20 53 51 4c 49 54 45  /* Either SQLITE
210d0 5f 41 54 54 41 43 48 20 6f 72 20 53 51 4c 49 54  _ATTACH or SQLIT
210d1 45 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20 46 75  E_DETACH */.  Fu
210d2 6e 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 20 20  ncDef *pFunc,   
210d3 20 20 20 2f 2a 20 46 75 6e 63 44 65 66 20 77 72     /* FuncDef wr
210d4 61 70 70 65 72 20 66 6f 72 20 64 65 74 61 63 68  apper for detach
210d5 46 75 6e 63 28 29 20 6f 72 20 61 74 74 61 63 68  Func() or attach
210d6 46 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78 70 72  Func() */.  Expr
210d7 20 2a 70 41 75 74 68 41 72 67 2c 20 20 20 20 20   *pAuthArg,     
210d8 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74   /* Expression t
210d9 6f 20 70 61 73 73 20 74 6f 20 61 75 74 68 6f 72  o pass to author
210da 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
210db 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46 69 6c   */.  Expr *pFil
210dc 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61  ename,     /* Na
210dd 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66  me of database f
210de 69 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ile */.  Expr *p
210df 44 62 6e 61 6d 65 2c 20 20 20 20 20 20 20 2f 2a  Dbname,       /*
210e0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
210e1 61 62 61 73 65 20 74 6f 20 75 73 65 20 69 6e 74  abase to use int
210e2 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 78 70  ernally */.  Exp
210e3 72 20 2a 70 4b 65 79 20 20 20 20 20 20 20 20 20  r *pKey         
210e4 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6b 65    /* Database ke
210e5 79 20 66 6f 72 20 65 6e 63 72 79 70 74 69 6f 6e  y for encryption
210e6 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 29 7b   extension */.){
210e7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e 61 6d  .  int rc;.  Nam
210e8 65 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 3b 0a  eContext sName;.
210e9 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c    Vdbe *v;.  sql
210ea 69 74 65 33 2a 20 64 62 20 3d 20 70 50 61 72 73  ite3* db = pPars
210eb 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 65 67  e->db;.  int reg
210ec 41 72 67 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53  Args;..#ifndef S
210ed 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
210ee 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72  RIZATION.  asser
210ef 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
210f0 6c 65 64 20 7c 7c 20 70 41 75 74 68 41 72 67 20  led || pAuthArg 
210f1 29 3b 0a 20 20 69 66 28 20 70 41 75 74 68 41 72  );.  if( pAuthAr
210f2 67 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  g ){.    char *z
210f3 41 75 74 68 41 72 67 20 3d 20 73 71 6c 69 74 65  AuthArg = sqlite
210f4 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
210f5 62 2c 20 26 70 41 75 74 68 41 72 67 2d 3e 73 70  b, &pAuthArg->sp
210f6 61 6e 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 41  an);.    if( !zA
210f7 75 74 68 41 72 67 20 29 7b 0a 20 20 20 20 20 20  uthArg ){.      
210f8 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b  goto attach_end;
210f9 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
210fa 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
210fb 28 70 50 61 72 73 65 2c 20 74 79 70 65 2c 20 7a  (pParse, type, z
210fc 41 75 74 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a  AuthArg, 0, 0);.
210fd 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
210fe 65 28 64 62 2c 20 7a 41 75 74 68 41 72 67 29 3b  e(db, zAuthArg);
210ff 0a 20 20 20 20 69 66 28 72 63 21 3d 53 51 4c 49  .    if(rc!=SQLI
21100 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67  TE_OK ){.      g
21101 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a  oto attach_end;.
21102 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
21103 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
21104 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f  AUTHORIZATION */
21105 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 61 6d  ..  memset(&sNam
21106 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d  e, 0, sizeof(Nam
21107 65 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e  eContext));.  sN
21108 61 6d 65 2e 70 50 61 72 73 65 20 3d 20 70 50 61  ame.pParse = pPa
21109 72 73 65 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20  rse;..  if( .   
2110a 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72     SQLITE_OK!=(r
2110b 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63  c = resolveAttac
2110c 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 46  hExpr(&sName, pF
2110d 69 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20  ilename)) ||.   
2110e 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72     SQLITE_OK!=(r
2110f 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63  c = resolveAttac
21110 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 44  hExpr(&sName, pD
21111 62 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20  bname)) ||.     
21112 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
21113 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45  = resolveAttachE
21114 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 4b 65 79  xpr(&sName, pKey
21115 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 72  )).  ){.    pPar
21116 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  se->nErr++;.    
21117 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b  goto attach_end;
21118 0a 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69  .  }..  v = sqli
21119 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2111a 65 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d 20  e);.  regArgs = 
2111b 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61  sqlite3GetTempRa
2111c 6e 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b 0a  nge(pParse, 4);.
2111d 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2111e 65 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e  e(pParse, pFilen
2111f 61 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a 20  ame, regArgs);. 
21120 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21121 28 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65  (pParse, pDbname
21122 2c 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20 20  , regArgs+1);.  
21123 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
21124 70 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72 65  pParse, pKey, re
21125 67 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73 73  gArgs+2);..  ass
21126 65 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61  ert( v || db->ma
21127 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
21128 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
21129 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2112a 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30  , OP_Function, 0
2112b 2c 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75 6e  , regArgs+3-pFun
2112c 63 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67 73  c->nArg, regArgs
2112d 2b 33 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  +3);.    assert(
2112e 20 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d 2d 31   pFunc->nArg==-1
2112f 20 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 72 67   || (pFunc->nArg
21130 26 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d 3e 6e  &0xff)==pFunc->n
21131 41 72 67 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  Arg );.    sqlit
21132 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
21133 2c 20 28 75 38 29 28 70 46 75 6e 63 2d 3e 6e 41  , (u8)(pFunc->nA
21134 72 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rg));.    sqlite
21135 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
21136 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 46 75   -1, (char *)pFu
21137 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b  nc, P4_FUNCDEF);
21138 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e  ..    /* Code an
21139 20 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f 72 20   OP_Expire. For 
2113a 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 65 6d  an ATTACH statem
2113b 65 6e 74 2c 20 73 65 74 20 50 31 20 74 6f 20 74  ent, set P1 to t
2113c 72 75 65 20 28 65 78 70 69 72 65 20 74 68 69 73  rue (expire this
2113d 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  .    ** statemen
2113e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45 54  t only). For DET
2113f 41 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20 66  ACH, set it to f
21140 61 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c 6c  alse (expire all
21141 20 65 78 69 73 74 69 6e 67 0a 20 20 20 20 2a 2a   existing.    **
21142 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 20 20   statements)..  
21143 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
21144 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
21145 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d 3d  _Expire, (type==
21146 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29 29 3b  SQLITE_ATTACH));
21147 0a 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 5f 65  .  }.  .attach_e
21148 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70  nd:.  sqlite3Exp
21149 72 44 65 6c 65 74 65 28 64 62 2c 20 70 46 69 6c  rDelete(db, pFil
2114a 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  ename);.  sqlite
2114b 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2114c 70 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  pDbname);.  sqli
2114d 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2114e 2c 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , pKey);.}../*.*
2114f 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20  * Called by the 
21150 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c  parser to compil
21151 65 20 61 20 44 45 54 41 43 48 20 73 74 61 74 65  e a DETACH state
21152 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ment..**.**     
21153 44 45 54 41 43 48 20 70 44 62 6e 61 6d 65 0a 2a  DETACH pDbname.*
21154 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21155 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74   void sqlite3Det
21156 61 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73  ach(Parse *pPars
21157 65 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65  e, Expr *pDbname
21158 29 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63  ){.  static Func
21159 44 65 66 20 64 65 74 61 63 68 5f 66 75 6e 63 20  Def detach_func 
2115a 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20  = {.    1,      
2115b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72            /* nAr
2115c 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f  g */.    SQLITE_
2115d 55 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50  UTF8,      /* iP
2115e 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c  refEnc */.    0,
2115f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21160 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20  /* flags */.    
21161 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
21162 20 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a    /* pUserData *
21163 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
21164 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
21165 20 2a 2f 0a 20 20 20 20 64 65 74 61 63 68 46 75   */.    detachFu
21166 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75  nc,       /* xFu
21167 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  nc */.    0,    
21168 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
21169 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Step */.    0,  
2116a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2116b 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20   xFinalize */.  
2116c 20 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68    "sqlite_detach
2116d 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a  ",  /* zName */.
2116e 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
2116f 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a        /* pHash *
21170 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74  /.  };.  codeAtt
21171 61 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49  ach(pParse, SQLI
21172 54 45 5f 44 45 54 41 43 48 2c 20 26 64 65 74 61  TE_DETACH, &deta
21173 63 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 6d 65  ch_func, pDbname
21174 2c 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d 65 29  , 0, 0, pDbname)
21175 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65  ;.}../*.** Calle
21176 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
21177 74 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41 54  to compile an AT
21178 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a  TACH statement..
21179 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48  **.**     ATTACH
2117a 20 70 20 41 53 20 70 44 62 6e 61 6d 65 20 4b 45   p AS pDbname KE
2117b 59 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 54 45  Y pKey.*/.SQLITE
2117c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2117d 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73  lite3Attach(Pars
2117e 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
2117f 2a 70 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d  *p, Expr *pDbnam
21180 65 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 7b 0a  e, Expr *pKey){.
21181 20 20 73 74 61 74 69 63 20 46 75 6e 63 44 65 66    static FuncDef
21182 20 61 74 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b   attach_func = {
21183 0a 20 20 20 20 33 2c 20 20 20 20 20 20 20 20 20  .    3,         
21184 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a         /* nArg *
21185 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46  /.    SQLITE_UTF
21186 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66  8,      /* iPref
21187 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  Enc */.    0,   
21188 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21189 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20  flags */.    0, 
2118a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2118b 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20  * pUserData */. 
2118c 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
2118d 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f       /* pNext */
2118e 0a 20 20 20 20 61 74 74 61 63 68 46 75 6e 63 2c  .    attachFunc,
2118f 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20         /* xFunc 
21190 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
21191 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65           /* xSte
21192 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  p */.    0,     
21193 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
21194 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22  inalize */.    "
21195 73 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20  sqlite_attach", 
21196 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20   /* zName */.   
21197 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
21198 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20     /* pHash */. 
21199 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68   };.  codeAttach
2119a 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2119b 41 54 54 41 43 48 2c 20 26 61 74 74 61 63 68 5f  ATTACH, &attach_
2119c 66 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 62 6e  func, p, p, pDbn
2119d 61 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23 65  ame, pKey);.}.#e
2119e 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2119f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f  MIT_ATTACH */../
211a0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
211a1 61 20 44 62 46 69 78 65 72 20 73 74 72 75 63 74  a DbFixer struct
211a2 75 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ure.  This routi
211a3 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  ne must be calle
211a4 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70 61  d prior.** to pa
211a5 73 73 69 6e 67 20 74 68 65 20 73 74 72 75 63 74  ssing the struct
211a6 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68  ure to one of th
211a7 65 20 73 71 6c 69 74 65 46 69 78 41 41 41 41 28  e sqliteFixAAAA(
211a8 29 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77  ) routines below
211a9 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
211aa 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74  rn value indicat
211ab 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  es whether or no
211ac 74 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72 65  t fixation is re
211ad 71 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a 2a  quired.  TRUE.**
211ae 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65 65   means we do nee
211af 64 20 74 6f 20 66 69 78 20 74 68 65 20 64 61 74  d to fix the dat
211b0 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73  abase references
211b1 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77 65  , FALSE means we
211b2 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49   do not..*/.SQLI
211b3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
211b4 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a 20  qlite3FixInit(. 
211b5 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20   DbFixer *pFix, 
211b6 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78 65       /* The fixe
211b7 72 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69  r to be initiali
211b8 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  zed */.  Parse *
211b9 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
211ba 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77  Error messages w
211bb 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 68  ill be written h
211bc 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ere */.  int iDb
211bd 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
211be 54 68 69 73 20 69 73 20 74 68 65 20 64 61 74 61  This is the data
211bf 62 61 73 65 20 74 68 61 74 20 6d 75 73 74 20 62  base that must b
211c0 65 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  e used */.  cons
211c1 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20  t char *zType,  
211c2 2f 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69 67  /* "view", "trig
211c3 67 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78 22  ger", or "index"
211c4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65   */.  const Toke
211c5 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d  n *pName  /* Nam
211c6 65 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20 74  e of the view, t
211c7 72 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65 78  rigger, or index
211c8 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
211c9 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 4e 45 56   *db;..  if( NEV
211ca 45 52 28 69 44 62 3c 30 29 20 7c 7c 20 69 44 62  ER(iDb<0) || iDb
211cb 3d 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==1 ) return 0;.
211cc 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
211cd 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  b;.  assert( db-
211ce 3e 6e 44 62 3e 69 44 62 20 29 3b 0a 20 20 70 46  >nDb>iDb );.  pF
211cf 69 78 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61  ix->pParse = pPa
211d0 72 73 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 44 62  rse;.  pFix->zDb
211d1 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
211d2 7a 4e 61 6d 65 3b 0a 20 20 70 46 69 78 2d 3e 7a  zName;.  pFix->z
211d3 54 79 70 65 20 3d 20 7a 54 79 70 65 3b 0a 20 20  Type = zType;.  
211d4 70 46 69 78 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e  pFix->pName = pN
211d5 61 6d 65 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  ame;.  return 1;
211d6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
211d7 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72  llowing set of r
211d8 6f 75 74 69 6e 65 73 20 77 61 6c 6b 20 74 68 72  outines walk thr
211d9 6f 75 67 68 20 74 68 65 20 70 61 72 73 65 20 74  ough the parse t
211da 72 65 65 20 61 6e 64 20 61 73 73 69 67 6e 0a 2a  ree and assign.*
211db 2a 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74  * a specific dat
211dc 61 62 61 73 65 20 74 6f 20 61 6c 6c 20 74 61 62  abase to all tab
211dd 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68  le references wh
211de 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ere the database
211df 20 6e 61 6d 65 0a 2a 2a 20 77 61 73 20 6c 65 66   name.** was lef
211e0 74 20 75 6e 73 70 65 63 69 66 69 65 64 20 69 6e  t unspecified in
211e1 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
211e2 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  L statement.  Th
211e3 65 20 70 46 69 78 20 73 74 72 75 63 74 75 72 65  e pFix structure
211e4 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65  .** must have be
211e5 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62  en initialized b
211e6 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74  y a prior call t
211e7 6f 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  o sqlite3FixInit
211e8 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ()..**.** These 
211e9 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
211ea 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  d to make sure t
211eb 68 61 74 20 61 6e 20 69 6e 64 65 78 2c 20 74 72  hat an index, tr
211ec 69 67 67 65 72 2c 20 6f 72 0a 2a 2a 20 76 69 65  igger, or.** vie
211ed 77 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  w in one databas
211ee 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 66 65 72  e does not refer
211ef 20 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 61   to objects in a
211f0 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62   different datab
211f1 61 73 65 2e 0a 2a 2a 20 28 45 78 63 65 70 74 69  ase..** (Excepti
211f2 6f 6e 3a 20 69 6e 64 69 63 65 73 2c 20 74 72 69  on: indices, tri
211f3 67 67 65 72 73 2c 20 61 6e 64 20 76 69 65 77 73  ggers, and views
211f4 20 69 6e 20 74 68 65 20 54 45 4d 50 20 64 61 74   in the TEMP dat
211f5 61 62 61 73 65 20 61 72 65 0a 2a 2a 20 61 6c 6c  abase are.** all
211f6 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f  owed to refer to
211f7 20 61 6e 79 74 68 69 6e 67 2e 29 20 20 49 66 20   anything.)  If 
211f8 61 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 65  a reference is e
211f9 78 70 6c 69 63 69 74 6c 79 20 6d 61 64 65 0a 2a  xplicitly made.*
211fa 2a 20 74 6f 20 61 6e 20 6f 62 6a 65 63 74 20 69  * to an object i
211fb 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 61  n a different da
211fc 74 61 62 61 73 65 2c 20 61 6e 20 65 72 72 6f 72  tabase, an error
211fd 20 6d 65 73 73 61 67 65 20 69 73 20 61 64 64 65   message is adde
211fe 64 20 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2d 3e  d to.** pParse->
211ff 7a 45 72 72 4d 73 67 20 61 6e 64 20 74 68 65 73  zErrMsg and thes
21200 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  e routines retur
21201 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20  n non-zero.  If 
21202 65 76 65 72 79 74 68 69 6e 67 0a 2a 2a 20 63 68  everything.** ch
21203 65 63 6b 73 20 6f 75 74 2c 20 74 68 65 73 65 20  ecks out, these 
21204 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
21205 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  0..*/.SQLITE_PRI
21206 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
21207 46 69 78 53 72 63 4c 69 73 74 28 0a 20 20 44 62  FixSrcList(.  Db
21208 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20  Fixer *pFix,    
21209 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66     /* Context of
2120a 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f   the fixation */
2120b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73  .  SrcList *pLis
2120c 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53  t       /* The S
2120d 6f 75 72 63 65 20 6c 69 73 74 20 74 6f 20 63 68  ource list to ch
2120e 65 63 6b 20 61 6e 64 20 6d 6f 64 69 66 79 20 2a  eck and modify *
2120f 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
21210 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
21211 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
21212 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a  t_item *pItem;..
21213 20 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69 73    if( NEVER(pLis
21214 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 30  t==0) ) return 0
21215 3b 0a 20 20 7a 44 62 20 3d 20 70 46 69 78 2d 3e  ;.  zDb = pFix->
21216 7a 44 62 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  zDb;.  for(i=0, 
21217 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20  pItem=pList->a; 
21218 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  i<pList->nSrc; i
21219 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
2121a 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61    if( pItem->zDa
2121b 74 61 62 61 73 65 3d 3d 30 20 29 7b 0a 20 20 20  tabase==0 ){.   
2121c 20 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62     pItem->zDatab
2121d 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ase = sqlite3DbS
2121e 74 72 44 75 70 28 70 46 69 78 2d 3e 70 50 61 72  trDup(pFix->pPar
2121f 73 65 2d 3e 64 62 2c 20 7a 44 62 29 3b 0a 20 20  se->db, zDb);.  
21220 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
21221 74 65 33 53 74 72 49 43 6d 70 28 70 49 74 65 6d  te3StrICmp(pItem
21222 2d 3e 7a 44 61 74 61 62 61 73 65 2c 7a 44 62 29  ->zDatabase,zDb)
21223 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=0 ){.      sql
21224 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46 69  ite3ErrorMsg(pFi
21225 78 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20 20  x->pParse,.     
21226 20 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e 6f      "%s %T canno
21227 74 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a 65  t reference obje
21228 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65 20  cts in database 
21229 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70 46  %s",.         pF
2122a 69 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78 2d  ix->zType, pFix-
2122b 3e 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a  >pName, pItem->z
2122c 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 20  Database);.     
2122d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2122e 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2122f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
21230 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
21231 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
21232 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
21233 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20  FixSelect(pFix, 
21234 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20  pItem->pSelect) 
21235 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
21236 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
21237 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e  pr(pFix, pItem->
21238 70 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31 3b  pOn) ) return 1;
21239 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65  .#endif.  }.  re
2123a 74 75 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21 64  turn 0;.}.#if !d
2123b 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2123c 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66  IT_VIEW) || !def
2123d 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2123e 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 54 45  _TRIGGER).SQLITE
2123f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
21240 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 0a 20  ite3FixSelect(. 
21241 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20   DbFixer *pFix, 
21242 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
21243 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e   of the fixation
21244 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
21245 65 6c 65 63 74 20 20 20 20 20 20 2f 2a 20 54 68  elect      /* Th
21246 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
21247 6e 74 20 74 6f 20 62 65 20 66 69 78 65 64 20 74  nt to be fixed t
21248 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a  o one database *
21249 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53  /.){.  while( pS
2124a 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 69 66 28  elect ){.    if(
2124b 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c   sqlite3FixExprL
2124c 69 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63  ist(pFix, pSelec
2124d 74 2d 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20 20  t->pEList) ){.  
2124e 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2124f 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
21250 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 70 46  te3FixSrcList(pF
21251 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  ix, pSelect->pSr
21252 63 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  c) ){.      retu
21253 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
21254 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
21255 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74  pr(pFix, pSelect
21256 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20  ->pWhere) ){.   
21257 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
21258 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
21259 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20  e3FixExpr(pFix, 
2125a 70 53 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e 67  pSelect->pHaving
2125b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
2125c 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  n 1;.    }.    p
2125d 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74  Select = pSelect
2125e 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20  ->pPrior;.  }.  
2125f 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49  return 0;.}.SQLI
21260 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
21261 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 0a 20  qlite3FixExpr(. 
21262 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20   DbFixer *pFix, 
21263 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f      /* Context o
21264 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a  f the fixation *
21265 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  /.  Expr *pExpr 
21266 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
21267 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66  pression to be f
21268 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61  ixed to one data
21269 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69  base */.){.  whi
2126a 6c 65 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20  le( pExpr ){.   
2126b 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50   if( ExprHasAnyP
2126c 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2126d 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53  P_TokenOnly|EP_S
2126e 70 61 6e 54 6f 6b 65 6e 29 20 29 20 62 72 65 61  panToken) ) brea
2126f 6b 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72 48  k;.    if( ExprH
21270 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
21271 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
21272 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
21273 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46  ite3FixSelect(pF
21274 69 78 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  ix, pExpr->x.pSe
21275 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e 20 31  lect) ) return 1
21276 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
21277 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
21278 78 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20  xExprList(pFix, 
21279 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 20  pExpr->x.pList) 
2127a 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
2127b 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
2127c 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
2127d 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20 29 7b  Expr->pRight) ){
2127e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2127f 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70 72  .    }.    pExpr
21280 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b   = pExpr->pLeft;
21281 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
21282 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
21283 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
21284 45 78 70 72 4c 69 73 74 28 0a 20 20 44 62 46 69  ExprList(.  DbFi
21285 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f  xer *pFix,     /
21286 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
21287 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45   fixation */.  E
21288 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 20  xprList *pList  
21289 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
2128a 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20  ion to be fixed 
2128b 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  to one database 
2128c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  */.){.  int i;. 
2128d 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
2128e 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
2128f 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
21290 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69  eturn 0;.  for(i
21291 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d  =0, pItem=pList-
21292 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  >a; i<pList->nEx
21293 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
21294 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
21295 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20  e3FixExpr(pFix, 
21296 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 7b  pItem->pExpr) ){
21297 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
21298 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
21299 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 0;.}.#endif.
2129a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2129b 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c  OMIT_TRIGGER.SQL
2129c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2129d 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65  sqlite3FixTrigge
2129e 72 53 74 65 70 28 0a 20 20 44 62 46 69 78 65 72  rStep(.  DbFixer
2129f 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43   *pFix,     /* C
212a0 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69  ontext of the fi
212a1 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 54 72 69 67  xation */.  Trig
212a2 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 2f  gerStep *pStep /
212a3 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 73 74  * The trigger st
212a4 65 70 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f  ep be fixed to o
212a5 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29  ne database */.)
212a6 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65 70  {.  while( pStep
212a7 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
212a8 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46 69  te3FixSelect(pFi
212a9 78 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63  x, pStep->pSelec
212aa 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  t) ){.      retu
212ab 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
212ac 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
212ad 70 72 28 70 46 69 78 2c 20 70 53 74 65 70 2d 3e  pr(pFix, pStep->
212ae 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 20  pWhere) ){.     
212af 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
212b0 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
212b1 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 78  FixExprList(pFix
212b2 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  , pStep->pExprLi
212b3 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  st) ){.      ret
212b4 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
212b5 20 70 53 74 65 70 20 3d 20 70 53 74 65 70 2d 3e   pStep = pStep->
212b6 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74  pNext;.  }.  ret
212b7 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 0;.}.#endif.
212b8 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
212b9 20 45 6e 64 20 6f 66 20 61 74 74 61 63 68 2e 63   End of attach.c
212ba 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
212bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
212bd 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
212be 20 42 65 67 69 6e 20 66 69 6c 65 20 61 75 74 68   Begin file auth
212bf 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
212c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
212c2 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a 61 6e 75  ./*.** 2003 Janu
212c3 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65  ary 11.**.** The
212c4 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
212c5 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
212c6 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
212c7 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
212c8 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
212c9 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
212ca 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
212cb 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
212cc 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
212cd 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
212ce 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
212cf 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
212d0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
212d1 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
212d2 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
212d3 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
212d4 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
212d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212d9 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
212da 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
212db 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
212dc 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  nt the sqlite3_s
212dd 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 0a  et_authorizer().
212de 2a 2a 20 41 50 49 2e 20 20 54 68 69 73 20 66 61  ** API.  This fa
212df 63 69 6c 69 74 79 20 69 73 20 61 6e 20 6f 70 74  cility is an opt
212e0 69 6f 6e 61 6c 20 66 65 61 74 75 72 65 20 6f 66  ional feature of
212e1 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 45   the library.  E
212e2 6d 62 65 64 64 65 64 0a 2a 2a 20 73 79 73 74 65  mbedded.** syste
212e3 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 6e  ms that do not n
212e4 65 65 64 20 74 68 69 73 20 66 61 63 69 6c 69 74  eed this facilit
212e5 79 20 6d 61 79 20 6f 6d 69 74 20 69 74 20 62 79  y may omit it by
212e6 20 72 65 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20   recompiling.** 
212e7 74 68 65 20 6c 69 62 72 61 72 79 20 77 69 74 68  the library with
212e8 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41   -DSQLITE_OMIT_A
212e9 55 54 48 4f 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a  UTHORIZATION=1.*
212ea 2a 0a 2a 2a 20 24 49 64 3a 20 61 75 74 68 2e 63  *.** $Id: auth.c
212eb 2c 76 20 31 2e 33 31 20 32 30 30 39 2f 30 35 2f  ,v 1.31 2009/05/
212ec 30 34 20 31 38 3a 30 31 3a 34 30 20 64 72 68 20  04 18:01:40 drh 
212ed 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
212ee 41 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20  All of the code 
212ef 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6d 61 79  in this file may
212f0 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79 20 64   be omitted by d
212f1 65 66 69 6e 69 6e 67 20 61 20 73 69 6e 67 6c 65  efining a single
212f2 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69  .** macro..*/.#i
212f3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
212f4 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
212f5 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 63 6c  ./*.** Set or cl
212f6 65 61 72 20 74 68 65 20 61 63 63 65 73 73 20 61  ear the access a
212f7 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e  uthorization fun
212f8 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ction..**.** The
212f9 20 61 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a   access authoriz
212fa 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 69  ation function i
212fb 73 20 62 65 20 63 61 6c 6c 65 64 20 64 75 72 69  s be called duri
212fc 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69  ng the compilati
212fd 6f 6e 0a 2a 2a 20 70 68 61 73 65 20 74 6f 20 76  on.** phase to v
212fe 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 75  erify that the u
212ff 73 65 72 20 68 61 73 20 72 65 61 64 20 61 6e 64  ser has read and
21300 2f 6f 72 20 77 72 69 74 65 20 61 63 63 65 73 73  /or write access
21301 20 70 65 72 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a   permission on.*
21302 2a 20 76 61 72 69 6f 75 73 20 66 69 65 6c 64 73  * various fields
21303 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
21304 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
21305 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 75 74  ument to the aut
21306 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73  h function.** is
21307 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 33   a copy of the 3
21308 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  rd argument to t
21309 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68  his routine.  Th
2130a 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
2130b 74 0a 2a 2a 20 74 6f 20 74 68 65 20 61 75 74 68  t.** to the auth
2130c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 65   function is one
2130d 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 73 74 61   of these consta
2130e 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  nts:.**.**      
2130f 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49   SQLITE_CREATE_I
21310 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51  NDEX.**       SQ
21311 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c  LITE_CREATE_TABL
21312 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  E.**       SQLIT
21313 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e  E_CREATE_TEMP_IN
21314 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  DEX.**       SQL
21315 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
21316 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53  TABLE.**       S
21317 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
21318 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20  P_TRIGGER.**    
21319 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
2131a 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20  _TEMP_VIEW.**   
2131b 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54      SQLITE_CREAT
2131c 45 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20  E_TRIGGER.**    
2131d 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
2131e 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53  _VIEW.**       S
2131f 51 4c 49 54 45 5f 44 45 4c 45 54 45 0a 2a 2a 20  QLITE_DELETE.** 
21320 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
21321 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20  P_INDEX.**      
21322 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42   SQLITE_DROP_TAB
21323 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  LE.**       SQLI
21324 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44  TE_DROP_TEMP_IND
21325 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  EX.**       SQLI
21326 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42  TE_DROP_TEMP_TAB
21327 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  LE.**       SQLI
21328 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49  TE_DROP_TEMP_TRI
21329 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51  GGER.**       SQ
2132a 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56  LITE_DROP_TEMP_V
2132b 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  IEW.**       SQL
2132c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52  ITE_DROP_TRIGGER
2132d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
2132e 5f 44 52 4f 50 5f 56 49 45 57 0a 2a 2a 20 20 20  _DROP_VIEW.**   
2132f 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 53 45 52      SQLITE_INSER
21330 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  T.**       SQLIT
21331 45 5f 50 52 41 47 4d 41 0a 2a 2a 20 20 20 20 20  E_PRAGMA.**     
21332 20 20 53 51 4c 49 54 45 5f 52 45 41 44 0a 2a 2a    SQLITE_READ.**
21333 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 45         SQLITE_SE
21334 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 53 51  LECT.**       SQ
21335 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e  LITE_TRANSACTION
21336 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
21337 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68  _UPDATE.**.** Th
21338 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72  e third and four
21339 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  th arguments to 
2133a 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f  the auth functio
2133b 6e 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f  n are the name o
2133c 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 61  f.** the table a
2133d 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68  nd the column th
2133e 61 74 20 61 72 65 20 62 65 69 6e 67 20 61 63 63  at are being acc
2133f 65 73 73 65 64 2e 20 20 54 68 65 20 61 75 74 68  essed.  The auth
21340 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f   function.** sho
21341 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68 65  uld return eithe
21342 72 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c  r SQLITE_OK, SQL
21343 49 54 45 5f 44 45 4e 59 2c 20 6f 72 20 53 51 4c  ITE_DENY, or SQL
21344 49 54 45 5f 49 47 4e 4f 52 45 2e 20 20 49 66 0a  ITE_IGNORE.  If.
21345 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
21346 72 65 74 75 72 6e 65 64 2c 20 69 74 20 6d 65 61  returned, it mea
21347 6e 73 20 74 68 61 74 20 61 63 63 65 73 73 20 69  ns that access i
21348 73 20 61 6c 6c 6f 77 65 64 2e 20 20 53 51 4c 49  s allowed.  SQLI
21349 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73  TE_DENY.** means
2134a 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74   that the SQL st
2134b 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 6e 65 76  atement will nev
2134c 65 72 2d 72 75 6e 20 2d 20 74 68 65 20 73 71 6c  er-run - the sql
2134d 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c  ite3_exec() call
2134e 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  .** will return 
2134f 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20  with an error.  
21350 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6d 65  SQLITE_IGNORE me
21351 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51 4c  ans that the SQL
21352 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73 68   statement.** sh
21353 6f 75 6c 64 20 72 75 6e 20 62 75 74 20 61 74 74  ould run but att
21354 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 74 68  empts to read th
21355 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75  e specified colu
21356 6d 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e  mn will return N
21357 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61 74 74 65 6d  ULL.** and attem
21358 70 74 73 20 74 6f 20 77 72 69 74 65 20 74 68 65  pts to write the
21359 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 62 65 20   column will be 
2135a 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53  ignored..**.** S
2135b 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 68 20  etting the auth 
2135c 66 75 6e 63 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c  function to NULL
2135d 20 64 69 73 61 62 6c 65 73 20 74 68 69 73 20 68   disables this h
2135e 6f 6f 6b 2e 20 20 54 68 65 20 64 65 66 61 75 6c  ook.  The defaul
2135f 74 0a 2a 2a 20 73 65 74 74 69 6e 67 20 6f 66 20  t.** setting of 
21360 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f  the auth functio
21361 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51  n is NULL..*/.SQ
21362 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
21363 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
21364 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  zer(.  sqlite3 *
21365 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 41 75 74  db,.  int (*xAut
21366 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  h)(void*,int,con
21367 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
21368 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
21369 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 0a 20  ,const char*),. 
2136a 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20   void *pArg.){. 
2136b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2136c 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2136d 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78  .  db->xAuth = x
2136e 41 75 74 68 3b 0a 20 20 64 62 2d 3e 70 41 75 74  Auth;.  db->pAut
2136f 68 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73  hArg = pArg;.  s
21370 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
21371 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
21372 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  b);.  sqlite3_mu
21373 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
21374 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  tex);.  return S
21375 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
21376 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 72 72 6f  ** Write an erro
21377 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 70  r message into p
21378 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 74  Parse->zErrMsg t
21379 68 61 74 20 65 78 70 6c 61 69 6e 73 20 74 68 61  hat explains tha
2137a 74 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 73 75  t the.** user-su
2137b 70 70 6c 69 65 64 20 61 75 74 68 6f 72 69 7a 61  pplied authoriza
2137c 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 72 65  tion function re
2137d 74 75 72 6e 65 64 20 61 6e 20 69 6c 6c 65 67 61  turned an illega
2137e 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  l value..*/.stat
2137f 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 41 75  ic void sqliteAu
21380 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28  thBadReturnCode(
21381 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
21382 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
21383 67 28 70 50 61 72 73 65 2c 20 22 61 75 74 68 6f  g(pParse, "autho
21384 72 69 7a 65 72 20 6d 61 6c 66 75 6e 63 74 69 6f  rizer malfunctio
21385 6e 22 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72  n");.  pParse->r
21386 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
21387 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  ;.}../*.** The p
21388 45 78 70 72 20 73 68 6f 75 6c 64 20 62 65 20 61  Expr should be a
21389 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65   TK_COLUMN expre
2138a 73 73 69 6f 6e 2e 20 20 54 68 65 20 74 61 62 6c  ssion.  The tabl
2138b 65 20 72 65 66 65 72 72 65 64 20 74 6f 0a 2a 2a  e referred to.**
2138c 20 69 73 20 69 6e 20 70 54 61 62 4c 69 73 74 20   is in pTabList 
2138d 6f 72 20 65 6c 73 65 20 69 74 20 69 73 20 74 68  or else it is th
2138e 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62  e NEW or OLD tab
2138f 6c 65 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e  le of a trigger.
21390 20 20 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73    .** Check to s
21391 65 65 20 69 66 20 69 74 20 69 73 20 4f 4b 20 74  ee if it is OK t
21392 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 72 74  o read this part
21393 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 2e 0a 2a  icular column..*
21394 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 75 74 68  *.** If the auth
21395 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
21396 73 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c  s SQLITE_IGNORE,
21397 20 63 68 61 6e 67 65 20 74 68 65 20 54 4b 5f 43   change the TK_C
21398 4f 4c 55 4d 4e 20 0a 2a 2a 20 69 6e 73 74 72 75  OLUMN .** instru
21399 63 74 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f  ction into a TK_
2139a 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 61 75  NULL.  If the au
2139b 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  th function retu
2139c 72 6e 73 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c  rns SQLITE_DENY,
2139d 0a 2a 2a 20 74 68 65 6e 20 67 65 6e 65 72 61 74  .** then generat
2139e 65 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53  e an error..*/.S
2139f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
213a0 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 52 65  id sqlite3AuthRe
213a1 61 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ad(.  Parse *pPa
213a2 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  rse,        /* T
213a3 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
213a4 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  t */.  Expr *pEx
213a5 70 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  pr,          /* 
213a6 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  The expression t
213a7 6f 20 63 68 65 63 6b 20 61 75 74 68 6f 72 69 7a  o check authoriz
213a8 61 74 69 6f 6e 20 6f 6e 20 2a 2f 0a 20 20 53 63  ation on */.  Sc
213a9 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 2c 20 20  hema *pSchema,  
213aa 20 20 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d      /* The schem
213ab 61 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  a of the express
213ac 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ion */.  SrcList
213ad 20 2a 70 54 61 62 4c 69 73 74 20 20 20 20 20 2f   *pTabList     /
213ae 2a 20 41 6c 6c 20 74 61 62 6c 65 20 74 68 61 74  * All table that
213af 20 70 45 78 70 72 20 6d 69 67 68 74 20 72 65 66   pExpr might ref
213b0 65 72 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71  er to */.){.  sq
213b1 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
213b2 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63  se->db;.  int rc
213b3 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  ;.  Table *pTab 
213b4 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  = 0;      /* The
213b5 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65 61   table being rea
213b6 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
213b7 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f 2a 20  r *zCol;     /* 
213b8 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  Name of the colu
213b9 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  mn of the table 
213ba 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 3b 20 20  */.  int iSrc;  
213bb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
213bc 64 65 78 20 69 6e 20 70 54 61 62 4c 69 73 74 2d  dex in pTabList-
213bd 3e 61 5b 5d 20 6f 66 20 74 61 62 6c 65 20 62 65  >a[] of table be
213be 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f  ing read */.  co
213bf 6e 73 74 20 63 68 61 72 20 2a 7a 44 42 61 73 65  nst char *zDBase
213c0 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64  ;   /* Name of d
213c1 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 61 63  atabase being ac
213c2 63 65 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67  cessed */.  Trig
213c3 67 65 72 53 74 61 63 6b 20 2a 70 53 74 61 63 6b  gerStack *pStack
213c4 3b 20 2f 2a 20 54 68 65 20 73 74 61 63 6b 20 6f  ; /* The stack o
213c5 66 20 63 75 72 72 65 6e 74 20 74 72 69 67 67 65  f current trigge
213c6 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  rs */.  int iDb;
213c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
213c8 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   The index of th
213c9 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20 65  e database the e
213ca 78 70 72 65 73 73 69 6f 6e 20 72 65 66 65 72 73  xpression refers
213cb 20 74 6f 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62   to */..  if( db
213cc 2d 3e 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74  ->xAuth==0 ) ret
213cd 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70  urn;.  assert( p
213ce 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Expr->op==TK_COL
213cf 55 4d 4e 20 29 3b 0a 20 20 69 44 62 20 3d 20 73  UMN );.  iDb = s
213d0 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
213d1 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
213d2 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20  pSchema);.  if( 
213d3 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20  iDb<0 ){.    /* 
213d4 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65  An attempt to re
213d5 61 64 20 61 20 63 6f 6c 75 6d 6e 20 6f 75 74 20  ad a column out 
213d6 6f 66 20 61 20 73 75 62 71 75 65 72 79 20 6f 72  of a subquery or
213d7 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 65   other.    ** te
213d8 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 2a  mporary table. *
213d9 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  /.    return;.  
213da 7d 0a 20 20 69 66 28 20 70 54 61 62 4c 69 73 74  }.  if( pTabList
213db 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 53 72 63   ){.    for(iSrc
213dc 3d 30 3b 20 41 4c 57 41 59 53 28 69 53 72 63 3c  =0; ALWAYS(iSrc<
213dd 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b  pTabList->nSrc);
213de 20 69 53 72 63 2b 2b 29 7b 0a 20 20 20 20 20 20   iSrc++){.      
213df 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  if( pExpr->iTabl
213e0 65 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  e==pTabList->a[i
213e1 53 72 63 5d 2e 69 43 75 72 73 6f 72 20 29 20 62  Src].iCursor ) b
213e2 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
213e3 61 73 73 65 72 74 28 20 69 53 72 63 3c 70 54 61  assert( iSrc<pTa
213e4 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20  bList->nSrc );. 
213e5 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69     pTab = pTabLi
213e6 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 70 54 61 62  st->a[iSrc].pTab
213e7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
213e8 53 74 61 63 6b 20 3d 20 70 50 61 72 73 65 2d 3e  Stack = pParse->
213e9 74 72 69 67 53 74 61 63 6b 3b 0a 20 20 20 20 69  trigStack;.    i
213ea 66 28 20 41 4c 57 41 59 53 28 70 53 74 61 63 6b  f( ALWAYS(pStack
213eb 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  ) ){.      /* Th
213ec 69 73 20 6d 75 73 74 20 62 65 20 61 6e 20 61 74  is must be an at
213ed 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68  tempt to read th
213ee 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65  e NEW or OLD pse
213ef 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 20 20 20  udo-tables.     
213f0 20 2a 2a 20 6f 66 20 61 20 74 72 69 67 67 65 72   ** of a trigger
213f1 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
213f2 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
213f3 69 54 61 62 6c 65 3d 3d 70 53 74 61 63 6b 2d 3e  iTable==pStack->
213f4 6e 65 77 49 64 78 20 7c 7c 20 70 45 78 70 72 2d  newIdx || pExpr-
213f5 3e 69 54 61 62 6c 65 3d 3d 70 53 74 61 63 6b 2d  >iTable==pStack-
213f6 3e 6f 6c 64 49 64 78 20 29 3b 0a 20 20 20 20 20  >oldIdx );.     
213f7 20 70 54 61 62 20 3d 20 70 53 74 61 63 6b 2d 3e   pTab = pStack->
213f8 70 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  pTab;.    }.  }.
213f9 20 20 69 66 28 20 4e 45 56 45 52 28 70 54 61 62    if( NEVER(pTab
213fa 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20  ==0) ) return;. 
213fb 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c   if( pExpr->iCol
213fc 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73  umn>=0 ){.    as
213fd 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f  sert( pExpr->iCo
213fe 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20  lumn<pTab->nCol 
213ff 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54  );.    zCol = pT
21400 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e  ab->aCol[pExpr->
21401 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d 65 3b 0a  iColumn].zName;.
21402 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 61 62    }else if( pTab
21403 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20  ->iPKey>=0 ){.  
21404 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
21405 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e 43 6f 6c  iPKey<pTab->nCol
21406 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 70   );.    zCol = p
21407 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e  Tab->aCol[pTab->
21408 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  iPKey].zName;.  
21409 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6c 20  }else{.    zCol 
2140a 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20 7d 0a 20  = "ROWID";.  }. 
2140b 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
2140c 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
2140d 3b 0a 20 20 7a 44 42 61 73 65 20 3d 20 64 62 2d  ;.  zDBase = db-
2140e 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
2140f 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74  .  rc = db->xAut
21410 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20  h(db->pAuthArg, 
21411 53 51 4c 49 54 45 5f 52 45 41 44 2c 20 70 54 61  SQLITE_READ, pTa
21412 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c 20  b->zName, zCol, 
21413 7a 44 42 61 73 65 2c 20 0a 20 20 20 20 20 20 20  zDBase, .       
21414 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
21415 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b  ->zAuthContext);
21416 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
21417 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20  E_IGNORE ){.    
21418 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e  pExpr->op = TK_N
21419 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ULL;.  }else if(
2141a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59   rc==SQLITE_DENY
2141b 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
2141c 6e 44 62 3e 32 20 7c 7c 20 69 44 62 21 3d 30 20  nDb>2 || iDb!=0 
2141d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2141e 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2141f 20 22 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25   "access to %s.%
21420 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69 74  s.%s is prohibit
21421 65 64 22 2c 20 0a 20 20 20 20 20 20 20 20 20 7a  ed", .         z
21422 44 42 61 73 65 2c 20 70 54 61 62 2d 3e 7a 4e 61  DBase, pTab->zNa
21423 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  me, zCol);.    }
21424 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
21425 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
21426 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25  se, "access to %
21427 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69 74  s.%s is prohibit
21428 65 64 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  ed",pTab->zName,
21429 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  zCol);.    }.   
2142a 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
2142b 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c  LITE_AUTH;.  }el
2142c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  se if( rc!=SQLIT
2142d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
2142e 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43  teAuthBadReturnC
2142f 6f 64 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d  ode(pParse);.  }
21430 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20  .}../*.** Do an 
21431 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68  authorization ch
21432 65 63 6b 20 75 73 69 6e 67 20 74 68 65 20 63 6f  eck using the co
21433 64 65 20 61 6e 64 20 61 72 67 75 6d 65 6e 74 73  de and arguments
21434 20 67 69 76 65 6e 2e 20 20 52 65 74 75 72 6e 0a   given.  Return.
21435 2a 2a 20 65 69 74 68 65 72 20 53 51 4c 49 54 45  ** either SQLITE
21436 5f 4f 4b 20 28 7a 65 72 6f 29 20 6f 72 20 53 51  _OK (zero) or SQ
21437 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6f 72 20 53  LITE_IGNORE or S
21438 51 4c 49 54 45 5f 44 45 4e 59 2e 20 20 49 66 20  QLITE_DENY.  If 
21439 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 69  SQLITE_DENY.** i
2143a 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e  s returned, then
2143b 20 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74   the error count
2143c 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61   and error messa
2143d 67 65 20 69 6e 20 70 50 61 72 73 65 20 61 72 65  ge in pParse are
2143e 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 70 70  .** modified app
2143f 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53  ropriately..*/.S
21440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
21441 74 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  t sqlite3AuthChe
21442 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ck(.  Parse *pPa
21443 72 73 65 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c  rse,.  int code,
21444 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
21445 41 72 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Arg1,.  const ch
21446 61 72 20 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e  ar *zArg2,.  con
21447 73 74 20 63 68 61 72 20 2a 7a 41 72 67 33 0a 29  st char *zArg3.)
21448 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
21449 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2144a 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f  int rc;..  /* Do
2144b 6e 27 74 20 64 6f 20 61 6e 79 20 61 75 74 68 6f  n't do any autho
2144c 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 73 20  rization checks 
2144d 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  if the database 
2144e 69 73 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a  is initialising.
2144f 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 70    ** or if the p
21450 61 72 73 65 72 20 69 73 20 62 65 69 6e 67 20 69  arser is being i
21451 6e 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68  nvoked from with
21452 69 6e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61  in sqlite3_decla
21453 72 65 5f 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20  re_vtab..  */.  
21454 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
21455 79 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f  y || IN_DECLARE_
21456 56 54 41 42 20 29 7b 0a 20 20 20 20 72 65 74 75  VTAB ){.    retu
21457 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
21458 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75  }..  if( db->xAu
21459 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  th==0 ){.    ret
2145a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
2145b 20 7d 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41   }.  rc = db->xA
2145c 75 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67  uth(db->pAuthArg
2145d 2c 20 63 6f 64 65 2c 20 7a 41 72 67 31 2c 20 7a  , code, zArg1, z
2145e 41 72 67 32 2c 20 7a 41 72 67 33 2c 20 70 50 61  Arg2, zArg3, pPa
2145f 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
21460 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
21461 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20  LITE_DENY ){.   
21462 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
21463 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75  (pParse, "not au
21464 74 68 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20 20  thorized");.    
21465 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
21466 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73  ITE_AUTH;.  }els
21467 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45  e if( rc!=SQLITE
21468 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54  _OK && rc!=SQLIT
21469 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20  E_IGNORE ){.    
2146a 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 45 4e 59  rc = SQLITE_DENY
2146b 3b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 68  ;.    sqliteAuth
2146c 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50  BadReturnCode(pP
2146d 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  arse);.  }.  ret
2146e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2146f 20 50 75 73 68 20 61 6e 20 61 75 74 68 6f 72 69   Push an authori
21470 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 2e 20  zation context. 
21471 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74   After this rout
21472 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  ine is called, t
21473 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61 72 67 75  he.** zArg3 argu
21474 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f 72 69 7a  ment to authoriz
21475 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 20  ation callbacks 
21476 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65 78 74  will be zContext
21477 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 70 65 64   until.** popped
21478 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73 65 3d  .  Or if pParse=
21479 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  =0, this routine
2147a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
2147b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2147c 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 43  oid sqlite3AuthC
2147d 6f 6e 74 65 78 74 50 75 73 68 28 0a 20 20 50 61  ontextPush(.  Pa
2147e 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 41  rse *pParse,.  A
2147f 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  uthContext *pCon
21480 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63  text, .  const c
21481 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 0a 29 7b  har *zContext.){
21482 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
21483 65 20 29 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d  e );.  pContext-
21484 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  >pParse = pParse
21485 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41  ;.  pContext->zA
21486 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 61  uthContext = pPa
21487 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
21488 74 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41 75  t;.  pParse->zAu
21489 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 43 6f 6e  thContext = zCon
2148a 74 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  text;.}../*.** P
2148b 6f 70 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74  op an authorizat
2148c 69 6f 6e 20 63 6f 6e 74 65 78 74 20 74 68 61 74  ion context that
2148d 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20   was previously 
2148e 70 75 73 68 65 64 0a 2a 2a 20 62 79 20 73 71 6c  pushed.** by sql
2148f 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
21490 75 73 68 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ush.*/.SQLITE_PR
21491 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21492 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70  e3AuthContextPop
21493 28 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43  (AuthContext *pC
21494 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 70  ontext){.  if( p
21495 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20  Context->pParse 
21496 29 7b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d  ){.    pContext-
21497 3e 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f  >pParse->zAuthCo
21498 6e 74 65 78 74 20 3d 20 70 43 6f 6e 74 65 78 74  ntext = pContext
21499 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a  ->zAuthContext;.
2149a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50      pContext->pP
2149b 61 72 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  arse = 0;.  }.}.
2149c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2149d 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
2149e 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  TION */../******
2149f 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
214a0 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  auth.c *********
214a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
214a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
214a3 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
214a4 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
214a5 69 6c 65 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a  ile build.c ****
214a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
214a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
214a8 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
214a9 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
214aa 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
214ab 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
214ac 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
214ad 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
214ae 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
214af 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
214b0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
214b1 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
214b2 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
214b3 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
214b4 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
214b5 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
214b6 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
214b7 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
214b8 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
214b9 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
214ba 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
214bb 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
214bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
214bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
214be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
214bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
214c0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
214c1 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75  tains C code rou
214c2 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63  tines that are c
214c3 61 6c 6c 65 64 20 62 79 20 74 68 65 20 53 51 4c  alled by the SQL
214c4 69 74 65 20 70 61 72 73 65 72 0a 2a 2a 20 77 68  ite parser.** wh
214c5 65 6e 20 73 79 6e 74 61 78 20 72 75 6c 65 73 20  en syntax rules 
214c6 61 72 65 20 72 65 64 75 63 65 64 2e 20 20 54 68  are reduced.  Th
214c7 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  e routines in th
214c8 69 73 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 74  is file handle t
214c9 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  he.** following 
214ca 6b 69 6e 64 73 20 6f 66 20 53 51 4c 20 73 79 6e  kinds of SQL syn
214cb 74 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43  tax:.**.**     C
214cc 52 45 41 54 45 20 54 41 42 4c 45 0a 2a 2a 20 20  REATE TABLE.**  
214cd 20 20 20 44 52 4f 50 20 54 41 42 4c 45 0a 2a 2a     DROP TABLE.**
214ce 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45       CREATE INDE
214cf 58 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 49 4e  X.**     DROP IN
214d0 44 45 58 0a 2a 2a 20 20 20 20 20 63 72 65 61 74  DEX.**     creat
214d1 69 6e 67 20 49 44 20 6c 69 73 74 73 0a 2a 2a 20  ing ID lists.** 
214d2 20 20 20 20 42 45 47 49 4e 20 54 52 41 4e 53 41      BEGIN TRANSA
214d3 43 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 43 4f 4d  CTION.**     COM
214d4 4d 49 54 0a 2a 2a 20 20 20 20 20 52 4f 4c 4c 42  MIT.**     ROLLB
214d5 41 43 4b 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62  ACK.**.** $Id: b
214d6 75 69 6c 64 2e 63 2c 76 20 31 2e 35 33 37 20 32  uild.c,v 1.537 2
214d7 30 30 39 2f 30 35 2f 30 36 20 31 38 3a 34 32 3a  009/05/06 18:42:
214d8 32 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  21 drh Exp $.*/.
214d9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
214da 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ine is called wh
214db 65 6e 20 61 20 6e 65 77 20 53 51 4c 20 73 74 61  en a new SQL sta
214dc 74 65 6d 65 6e 74 20 69 73 20 62 65 67 69 6e 6e  tement is beginn
214dd 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 70 61 72  ing to.** be par
214de 73 65 64 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65  sed.  Initialize
214df 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75   the pParse stru
214e0 63 74 75 72 65 20 61 73 20 6e 65 65 64 65 64 2e  cture as needed.
214e1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
214e2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
214e3 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 20  eginParse(Parse 
214e4 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 65 78 70  *pParse, int exp
214e5 6c 61 69 6e 46 6c 61 67 29 7b 0a 20 20 70 50 61  lainFlag){.  pPa
214e6 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 3d 20 28  rse->explain = (
214e7 75 38 29 65 78 70 6c 61 69 6e 46 6c 61 67 3b 0a  u8)explainFlag;.
214e8 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d    pParse->nVar =
214e9 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53   0;.}..#ifndef S
214ea 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
214eb 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 54 68  D_CACHE./*.** Th
214ec 65 20 54 61 62 6c 65 4c 6f 63 6b 20 73 74 72 75  e TableLock stru
214ed 63 74 75 72 65 20 69 73 20 6f 6e 6c 79 20 75 73  cture is only us
214ee 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ed by the sqlite
214ef 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64  3TableLock() and
214f0 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63  .** codeTableLoc
214f1 6b 73 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  ks() functions..
214f2 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 4c  */.struct TableL
214f3 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 69 44 62 3b  ock {.  int iDb;
214f4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
214f5 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
214f6 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c  taining the tabl
214f7 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
214f8 2f 0a 20 20 69 6e 74 20 69 54 61 62 3b 20 20 20  /.  int iTab;   
214f9 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
214fa 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65  root page of the
214fb 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63   table to be loc
214fc 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 73 57 72  ked */.  u8 isWr
214fd 69 74 65 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a  iteLock;      /*
214fe 20 54 72 75 65 20 66 6f 72 20 77 72 69 74 65 20   True for write 
214ff 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 66 6f 72  lock.  False for
21500 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a   a read lock */.
21501 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
21502 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ame;   /* Name o
21503 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d  f the table */.}
21504 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20  ;../*.** Record 
21505 74 68 65 20 66 61 63 74 20 74 68 61 74 20 77 65  the fact that we
21506 20 77 61 6e 74 20 74 6f 20 6c 6f 63 6b 20 61 20   want to lock a 
21507 74 61 62 6c 65 20 61 74 20 72 75 6e 2d 74 69 6d  table at run-tim
21508 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  e.  .**.** The t
21509 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65  able to be locke
2150a 64 20 68 61 73 20 72 6f 6f 74 20 70 61 67 65 20  d has root page 
2150b 69 54 61 62 20 61 6e 64 20 69 73 20 66 6f 75 6e  iTab and is foun
2150c 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44  d in database iD
2150d 62 2e 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20  b..** A read or 
2150e 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 63 61 6e  a write lock can
2150f 20 62 65 20 74 61 6b 65 6e 20 64 65 70 65 6e 64   be taken depend
21510 69 6e 67 20 6f 6e 20 69 73 57 72 69 74 65 6c 6f  ing on isWritelo
21511 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ck..**.** This r
21512 6f 75 74 69 6e 65 20 6a 75 73 74 20 72 65 63 6f  outine just reco
21513 72 64 73 20 74 68 65 20 66 61 63 74 20 74 68 61  rds the fact tha
21514 74 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 65  t the lock is de
21515 73 69 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20 63  sired.  The.** c
21516 6f 64 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 20  ode to make the 
21517 6c 6f 63 6b 20 6f 63 63 75 72 20 69 73 20 67 65  lock occur is ge
21518 6e 65 72 61 74 65 64 20 62 79 20 61 20 6c 61 74  nerated by a lat
21519 65 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 63 6f  er call to.** co
2151a 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 77  deTableLocks() w
2151b 68 69 63 68 20 6f 63 63 75 72 73 20 64 75 72 69  hich occurs duri
2151c 6e 67 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  ng sqlite3Finish
2151d 43 6f 64 69 6e 67 28 29 2e 0a 2a 2f 0a 53 51 4c  Coding()..*/.SQL
2151e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2151f 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
21520 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  k(.  Parse *pPar
21521 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
21522 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
21523 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20  int iDb,        
21524 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
21525 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
21526 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
21527 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e   to lock */.  in
21528 74 20 69 54 61 62 2c 20 20 20 20 20 20 20 20 20  t iTab,         
21529 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6e 75   /* Root page nu
2152a 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c  mber of the tabl
2152b 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
2152c 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f  /.  u8 isWriteLo
2152d 63 6b 2c 20 20 20 20 2f 2a 20 54 72 75 65 20 66  ck,    /* True f
2152e 6f 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20  or a write lock 
2152f 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
21530 2a 7a 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20  *zName  /* Name 
21531 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
21532 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 29 7b 0a  be locked */.){.
21533 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
21534 42 79 74 65 73 3b 0a 20 20 54 61 62 6c 65 4c 6f  Bytes;.  TableLo
21535 63 6b 20 2a 70 3b 0a 0a 20 20 69 66 28 20 69 44  ck *p;..  if( iD
21536 62 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  b<0 ){.    retur
21537 6e 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d  n;.  }..  for(i=
21538 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61  0; i<pParse->nTa
21539 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20  bleLock; i++){. 
2153a 20 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e     p = &pParse->
2153b 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20  aTableLock[i];. 
2153c 20 20 20 69 66 28 20 70 2d 3e 69 44 62 3d 3d 69     if( p->iDb==i
2153d 44 62 20 26 26 20 70 2d 3e 69 54 61 62 3d 3d 69  Db && p->iTab==i
2153e 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  Tab ){.      p->
2153f 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 70  isWriteLock = (p
21540 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c  ->isWriteLock ||
21541 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20   isWriteLock);. 
21542 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
21543 20 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74 65 73   }.  }..  nBytes
21544 20 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 4c   = sizeof(TableL
21545 6f 63 6b 29 20 2a 20 28 70 50 61 72 73 65 2d 3e  ock) * (pParse->
21546 6e 54 61 62 6c 65 4c 6f 63 6b 2b 31 29 3b 0a 20  nTableLock+1);. 
21547 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c   pParse->aTableL
21548 6f 63 6b 20 3d 20 0a 20 20 20 20 20 20 73 71 6c  ock = .      sql
21549 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
2154a 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ree(pParse->db, 
2154b 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f  pParse->aTableLo
2154c 63 6b 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69  ck, nBytes);.  i
2154d 66 28 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c  f( pParse->aTabl
2154e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 20 3d  eLock ){.    p =
2154f 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65   &pParse->aTable
21550 4c 6f 63 6b 5b 70 50 61 72 73 65 2d 3e 6e 54 61  Lock[pParse->nTa
21551 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b 0a 20 20 20 20  bleLock++];.    
21552 70 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20  p->iDb = iDb;.  
21553 20 20 70 2d 3e 69 54 61 62 20 3d 20 69 54 61 62    p->iTab = iTab
21554 3b 0a 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65  ;.    p->isWrite
21555 4c 6f 63 6b 20 3d 20 69 73 57 72 69 74 65 4c 6f  Lock = isWriteLo
21556 63 6b 3b 0a 20 20 20 20 70 2d 3e 7a 4e 61 6d 65  ck;.    p->zName
21557 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73   = zName;.  }els
21558 65 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  e{.    pParse->n
21559 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20  TableLock = 0;. 
2155a 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d     pParse->db->m
2155b 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2155c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .  }.}../*.** Co
2155d 64 65 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c 6f  de an OP_TableLo
2155e 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66  ck instruction f
2155f 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 6c 6f  or each table lo
21560 63 6b 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73  cked by the.** s
21561 74 61 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69 67  tatement (config
21562 75 72 65 64 20 62 79 20 63 61 6c 6c 73 20 74 6f  ured by calls to
21563 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
21564 6b 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  k())..*/.static 
21565 76 6f 69 64 20 63 6f 64 65 54 61 62 6c 65 4c 6f  void codeTableLo
21566 63 6b 73 28 50 61 72 73 65 20 2a 70 50 61 72 73  cks(Parse *pPars
21567 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56  e){.  int i;.  V
21568 64 62 65 20 2a 70 56 64 62 65 3b 20 0a 0a 20 20  dbe *pVdbe; ..  
21569 69 66 28 20 30 3d 3d 28 70 56 64 62 65 20 3d 20  if( 0==(pVdbe = 
2156a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2156b 50 61 72 73 65 29 29 20 29 7b 0a 20 20 20 20 72  Parse)) ){.    r
2156c 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 66 6f  eturn;.  }..  fo
2156d 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d  r(i=0; i<pParse-
2156e 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b  >nTableLock; i++
2156f 29 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b  ){.    TableLock
21570 20 2a 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61   *p = &pParse->a
21571 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20  TableLock[i];.  
21572 20 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44    int p1 = p->iD
21573 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  b;.    sqlite3Vd
21574 62 65 41 64 64 4f 70 34 28 70 56 64 62 65 2c 20  beAddOp4(pVdbe, 
21575 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 70 31  OP_TableLock, p1
21576 2c 20 70 2d 3e 69 54 61 62 2c 20 70 2d 3e 69 73  , p->iTab, p->is
21577 57 72 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 20 20  WriteLock,.     
21578 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21579 20 70 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54   p->zName, P4_ST
2157a 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c  ATIC);.  }.}.#el
2157b 73 65 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64  se.  #define cod
2157c 65 54 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a 23  eTableLocks(x).#
2157d 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
2157e 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
2157f 6c 65 64 20 61 66 74 65 72 20 61 20 73 69 6e 67  led after a sing
21580 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  le SQL statement
21581 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72   has been.** par
21582 73 65 64 20 61 6e 64 20 61 20 56 44 42 45 20 70  sed and a VDBE p
21583 72 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74  rogram to execut
21584 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  e that statement
21585 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65   has been.** pre
21586 70 61 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75  pared.  This rou
21587 74 69 6e 65 20 70 75 74 73 20 74 68 65 20 66 69  tine puts the fi
21588 6e 69 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20  nishing touches 
21589 6f 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70  on the.** VDBE p
2158a 72 6f 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74  rogram and reset
2158b 73 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72  s the pParse str
2158c 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e  ucture for the n
2158d 65 78 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a  ext.** parse..**
2158e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66  .** Note that if
2158f 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72   an error occurr
21590 65 64 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20  ed, it might be 
21591 74 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a  the case that.**
21592 20 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61   no VDBE code wa
21593 73 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a  s generated..*/.
21594 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
21595 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73  oid sqlite3Finis
21596 68 43 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70  hCoding(Parse *p
21597 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
21598 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76  3 *db;.  Vdbe *v
21599 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  ;..  db = pParse
2159a 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ->db;.  if( db->
2159b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
2159c 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61  eturn;.  if( pPa
2159d 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65  rse->nested ) re
2159e 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72  turn;.  if( pPar
2159f 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72  se->nErr ) retur
215a0 6e 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62  n;..  /* Begin b
215a1 79 20 67 65 6e 65 72 61 74 69 6e 67 20 73 6f 6d  y generating som
215a2 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f  e termination co
215a3 64 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  de at the end of
215a4 20 74 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70   the.  ** vdbe p
215a5 72 6f 67 72 61 6d 0a 20 20 2a 2f 0a 20 20 76 20  rogram.  */.  v 
215a6 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
215a7 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
215a8 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
215a9 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
215aa 5f 48 61 6c 74 29 3b 0a 0a 20 20 20 20 2f 2a 20  _Halt);..    /* 
215ab 54 68 65 20 63 6f 6f 6b 69 65 20 6d 61 73 6b 20  The cookie mask 
215ac 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 62 69 74  contains one bit
215ad 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61   for each databa
215ae 73 65 20 66 69 6c 65 20 6f 70 65 6e 2e 0a 20 20  se file open..  
215af 20 20 2a 2a 20 28 42 69 74 20 30 20 69 73 20 66    ** (Bit 0 is f
215b0 6f 72 20 6d 61 69 6e 2c 20 62 69 74 20 31 20 69  or main, bit 1 i
215b1 73 20 66 6f 72 20 74 65 6d 70 2c 20 61 6e 64 20  s for temp, and 
215b2 73 6f 20 66 6f 72 74 68 2e 29 20 20 42 69 74 73  so forth.)  Bits
215b3 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 65 74 20   are.    ** set 
215b4 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73  for each databas
215b5 65 20 74 68 61 74 20 69 73 20 75 73 65 64 2e 20  e that is used. 
215b6 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
215b7 6f 20 73 74 61 72 74 20 61 0a 20 20 20 20 2a 2a  o start a.    **
215b8 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20   transaction on 
215b9 65 61 63 68 20 75 73 65 64 20 64 61 74 61 62 61  each used databa
215ba 73 65 20 61 6e 64 20 74 6f 20 76 65 72 69 66 79  se and to verify
215bb 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
215bc 69 65 0a 20 20 20 20 2a 2a 20 6f 6e 20 65 61 63  ie.    ** on eac
215bd 68 20 75 73 65 64 20 64 61 74 61 62 61 73 65 2e  h used database.
215be 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
215bf 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f  pParse->cookieGo
215c0 74 6f 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 33  to>0 ){.      u3
215c1 32 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 69 6e  2 mask;.      in
215c2 74 20 69 44 62 3b 0a 20 20 20 20 20 20 73 71 6c  t iDb;.      sql
215c3 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
215c4 28 76 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  (v, pParse->cook
215c5 69 65 47 6f 74 6f 2d 31 29 3b 0a 20 20 20 20 20  ieGoto-1);.     
215c6 20 66 6f 72 28 69 44 62 3d 30 2c 20 6d 61 73 6b   for(iDb=0, mask
215c7 3d 31 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b  =1; iDb<db->nDb;
215c8 20 6d 61 73 6b 3c 3c 3d 31 2c 20 69 44 62 2b 2b   mask<<=1, iDb++
215c9 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28  ){.        if( (
215ca 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e 63  mask & pParse->c
215cb 6f 6f 6b 69 65 4d 61 73 6b 29 3d 3d 30 20 29 20  ookieMask)==0 ) 
215cc 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
215cd 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
215ce 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
215cf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
215d0 64 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 54  dbeAddOp2(v,OP_T
215d1 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 44 62 2c  ransaction, iDb,
215d2 20 28 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d   (mask & pParse-
215d3 3e 77 72 69 74 65 4d 61 73 6b 29 21 3d 30 29 3b  >writeMask)!=0);
215d4 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d  .        if( db-
215d5 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b  >init.busy==0 ){
215d6 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
215d7 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 4f  e3VdbeAddOp2(v,O
215d8 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c 20  P_VerifyCookie, 
215d9 69 44 62 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f  iDb, pParse->coo
215da 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d 29 3b 0a  kieValue[iDb]);.
215db 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
215dc 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
215dd 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
215de 4c 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  LE.      {.     
215df 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
215e0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
215e1 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20  rse->nVtabLock; 
215e2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
215e3 63 68 61 72 20 2a 76 74 61 62 20 3d 20 28 63 68  char *vtab = (ch
215e4 61 72 20 2a 29 70 50 61 72 73 65 2d 3e 61 70 56  ar *)pParse->apV
215e5 74 61 62 4c 6f 63 6b 5b 69 5d 2d 3e 70 56 74 61  tabLock[i]->pVta
215e6 62 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  b;.          sql
215e7 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
215e8 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c 20  , OP_VBegin, 0, 
215e9 30 2c 20 30 2c 20 76 74 61 62 2c 20 50 34 5f 56  0, 0, vtab, P4_V
215ea 54 41 42 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  TAB);.        }.
215eb 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
215ec 6e 56 74 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a 20  nVtabLock = 0;. 
215ed 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20       }.#endif.. 
215ee 20 20 20 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c       /* Once all
215ef 20 74 68 65 20 63 6f 6f 6b 69 65 73 20 68 61 76   the cookies hav
215f0 65 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 20  e been verified 
215f1 61 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  and transactions
215f2 20 6f 70 65 6e 65 64 2c 20 0a 20 20 20 20 20 20   opened, .      
215f3 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 72 65  ** obtain the re
215f4 71 75 69 72 65 64 20 74 61 62 6c 65 2d 6c 6f 63  quired table-loc
215f5 6b 73 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f  ks. This is a no
215f6 2d 6f 70 20 75 6e 6c 65 73 73 20 74 68 65 20 0a  -op unless the .
215f7 20 20 20 20 20 20 2a 2a 20 73 68 61 72 65 64 2d        ** shared-
215f8 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73  cache feature is
215f9 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20 20 20 20   enabled..      
215fa 2a 2f 0a 20 20 20 20 20 20 63 6f 64 65 54 61 62  */.      codeTab
215fb 6c 65 4c 6f 63 6b 73 28 70 50 61 72 73 65 29 3b  leLocks(pParse);
215fc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
215fd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
215fe 6f 74 6f 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e  oto, 0, pParse->
215ff 63 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a 20 20 20  cookieGoto);.   
21600 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 47 65   }.  }...  /* Ge
21601 74 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72  t the VDBE progr
21602 61 6d 20 72 65 61 64 79 20 66 6f 72 20 65 78 65  am ready for exe
21603 63 75 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 66  cution.  */.  if
21604 28 20 76 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  ( v && pParse->n
21605 45 72 72 3d 3d 30 20 26 26 20 21 64 62 2d 3e 6d  Err==0 && !db->m
21606 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 23  allocFailed ){.#
21607 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
21608 55 47 0a 20 20 20 20 46 49 4c 45 20 2a 74 72 61  UG.    FILE *tra
21609 63 65 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20  ce = (db->flags 
2160a 26 20 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61  & SQLITE_VdbeTra
2160b 63 65 29 21 3d 30 20 3f 20 73 74 64 6f 75 74 20  ce)!=0 ? stdout 
2160c 3a 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  : 0;.    sqlite3
2160d 56 64 62 65 54 72 61 63 65 28 76 2c 20 74 72 61  VdbeTrace(v, tra
2160e 63 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ce);.#endif.    
2160f 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
21610 69 43 61 63 68 65 4c 65 76 65 6c 3d 3d 30 20 29  iCacheLevel==0 )
21611 3b 20 20 2f 2a 20 44 69 73 61 62 6c 65 73 20 61  ;  /* Disables a
21612 6e 64 20 72 65 2d 65 6e 61 62 6c 65 73 20 6d 61  nd re-enables ma
21613 74 63 68 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  tch */.    sqlit
21614 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28  e3VdbeMakeReady(
21615 76 2c 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c  v, pParse->nVar,
21616 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2c 0a 20   pParse->nMem,. 
21617 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21618 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
21619 6e 54 61 62 2c 20 70 50 61 72 73 65 2d 3e 65 78  nTab, pParse->ex
2161a 70 6c 61 69 6e 29 3b 0a 20 20 20 20 70 50 61 72  plain);.    pPar
2161b 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
2161c 44 4f 4e 45 3b 0a 20 20 20 20 70 50 61 72 73 65  DONE;.    pParse
2161d 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20  ->colNamesSet = 
2161e 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  0;.  }else if( p
2161f 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54  Parse->rc==SQLIT
21620 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72  E_OK ){.    pPar
21621 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
21622 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61  ERROR;.  }.  pPa
21623 72 73 65 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20  rse->nTab = 0;. 
21624 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
21625 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 53 65  0;.  pParse->nSe
21626 74 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d  t = 0;.  pParse-
21627 3e 6e 56 61 72 20 3d 20 30 3b 0a 20 20 70 50 61  >nVar = 0;.  pPa
21628 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20  rse->cookieMask 
21629 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63  = 0;.  pParse->c
2162a 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d  ookieGoto = 0;.}
2162b 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20  ../*.** Run the 
2162c 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 65 20  parser and code 
2162d 67 65 6e 65 72 61 74 6f 72 20 72 65 63 75 72 73  generator recurs
2162e 69 76 65 6c 79 20 69 6e 20 6f 72 64 65 72 20 74  ively in order t
2162f 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f  o generate.** co
21630 64 65 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73  de for the SQL s
21631 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f  tatement given o
21632 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74  nto the end of t
21633 68 65 20 70 50 61 72 73 65 20 63 6f 6e 74 65 78  he pParse contex
21634 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75  t.** currently u
21635 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
21636 6e 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 72  n.  When the par
21637 73 65 72 20 69 73 20 72 75 6e 20 72 65 63 75 72  ser is run recur
21638 73 69 76 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77  sively.** this w
21639 61 79 2c 20 74 68 65 20 66 69 6e 61 6c 20 4f 50  ay, the final OP
2163a 5f 48 61 6c 74 20 69 73 20 6e 6f 74 20 61 70 70  _Halt is not app
2163b 65 6e 64 65 64 20 61 6e 64 20 6f 74 68 65 72 20  ended and other 
2163c 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a  initialization.*
2163d 2a 20 61 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69  * and finalizati
2163e 6f 6e 20 73 74 65 70 73 20 61 72 65 20 6f 6d 69  on steps are omi
2163f 74 74 65 64 20 62 65 63 61 75 73 65 20 74 68 6f  tted because tho
21640 73 65 20 61 72 65 20 68 61 6e 64 6c 69 6e 67 20  se are handling 
21641 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d  by the.** outerm
21642 6f 73 74 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a  ost parser..**.*
21643 2a 20 4e 6f 74 20 65 76 65 72 79 74 68 69 6e 67  * Not everything
21644 20 69 73 20 6e 65 73 74 61 62 6c 65 2e 20 20 54   is nestable.  T
21645 68 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20  his facility is 
21646 64 65 73 69 67 6e 65 64 20 74 6f 20 70 65 72 6d  designed to perm
21647 69 74 0a 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50  it.** INSERT, UP
21648 44 41 54 45 2c 20 61 6e 64 20 44 45 4c 45 54 45  DATE, and DELETE
21649 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 67 61 69   operations agai
2164a 6e 73 74 20 53 51 4c 49 54 45 5f 4d 41 53 54 45  nst SQLITE_MASTE
2164b 52 2e 20 20 55 73 65 0a 2a 2a 20 63 61 72 65 20  R.  Use.** care 
2164c 69 66 20 79 6f 75 20 64 65 63 69 64 65 20 74 6f  if you decide to
2164d 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 69 73   try to use this
2164e 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d   routine for som
2164f 65 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73  e other purposes
21650 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
21651 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
21652 4e 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73  NestedParse(Pars
21653 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
21654 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
21655 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
21656 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  ap;.  char *zSql
21657 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  ;.  char *zErrMs
21658 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  g = 0;.  sqlite3
21659 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2165a 62 3b 0a 23 20 64 65 66 69 6e 65 20 53 41 56 45  b;.# define SAVE
2165b 5f 53 5a 20 20 28 73 69 7a 65 6f 66 28 50 61 72  _SZ  (sizeof(Par
2165c 73 65 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 50  se) - offsetof(P
2165d 61 72 73 65 2c 6e 56 61 72 29 29 0a 20 20 63 68  arse,nVar)).  ch
2165e 61 72 20 73 61 76 65 42 75 66 5b 53 41 56 45 5f  ar saveBuf[SAVE_
2165f 53 5a 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61 72  SZ];..  if( pPar
21660 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72  se->nErr ) retur
21661 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  n;.  assert( pPa
21662 72 73 65 2d 3e 6e 65 73 74 65 64 3c 31 30 20 29  rse->nested<10 )
21663 3b 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 73 68  ;  /* Nesting sh
21664 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 6f 66 20  ould only be of 
21665 6c 69 6d 69 74 65 64 20 64 65 70 74 68 20 2a 2f  limited depth */
21666 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20  .  va_start(ap, 
21667 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 53 71 6c  zFormat);.  zSql
21668 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
21669 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  tf(db, zFormat, 
2166a 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
2166b 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30  );.  if( zSql==0
2166c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20   ){.    return; 
2166d 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75    /* A malloc mu
2166e 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a  st have failed *
2166f 2f 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e  /.  }.  pParse->
21670 6e 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63  nested++;.  memc
21671 70 79 28 73 61 76 65 42 75 66 2c 20 26 70 50 61  py(saveBuf, &pPa
21672 72 73 65 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f  rse->nVar, SAVE_
21673 53 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70  SZ);.  memset(&p
21674 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20  Parse->nVar, 0, 
21675 53 41 56 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69  SAVE_SZ);.  sqli
21676 74 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61  te3RunParser(pPa
21677 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72  rse, zSql, &zErr
21678 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Msg);.  sqlite3D
21679 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73  bFree(db, zErrMs
2167a 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  g);.  sqlite3DbF
2167b 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20  ree(db, zSql);. 
2167c 20 6d 65 6d 63 70 79 28 26 70 50 61 72 73 65 2d   memcpy(&pParse-
2167d 3e 6e 56 61 72 2c 20 73 61 76 65 42 75 66 2c 20  >nVar, saveBuf, 
2167e 53 41 56 45 5f 53 5a 29 3b 0a 20 20 70 50 61 72  SAVE_SZ);.  pPar
2167f 73 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a  se->nested--;.}.
21680 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68  ./*.** Locate th
21681 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75  e in-memory stru
21682 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72  cture that descr
21683 69 62 65 73 20 61 20 70 61 72 74 69 63 75 6c 61  ibes a particula
21684 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61  r database.** ta
21685 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61  ble given the na
21686 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65  me of that table
21687 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79   and (optionally
21688 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  ) the name of th
21689 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  e.** database co
2168a 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62  ntaining the tab
2168b 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c  le.  Return NULL
2168c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a   if not found..*
2168d 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73  *.** If zDatabas
2168e 65 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61  e is 0, all data
2168f 62 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68  bases are search
21690 65 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ed for the table
21691 20 61 6e 64 20 74 68 65 0a 2a 2a 20 66 69 72 73   and the.** firs
21692 74 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65  t matching table
21693 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28   is returned.  (
21694 4e 6f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20  No checking for 
21695 64 75 70 6c 69 63 61 74 65 20 74 61 62 6c 65 0a  duplicate table.
21696 2a 2a 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65  ** names is done
21697 2e 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f  .)  The search o
21698 72 64 65 72 20 69 73 20 54 45 4d 50 20 66 69 72  rder is TEMP fir
21699 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74  st, then MAIN, t
2169a 68 65 6e 20 61 6e 79 0a 2a 2a 20 61 75 78 69 6c  hen any.** auxil
2169b 69 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61  iary databases a
2169c 64 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 41  dded using the A
2169d 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  TTACH command..*
2169e 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71  *.** See also sq
2169f 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
216a0 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
216a1 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
216a2 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71  ite3FindTable(sq
216a3 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
216a4 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f   char *zName, co
216a5 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 62  nst char *zDatab
216a6 61 73 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ase){.  Table *p
216a7 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   = 0;.  int i;. 
216a8 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 61 73   int nName;.  as
216a9 73 65 72 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29  sert( zName!=0 )
216aa 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  ;.  nName = sqli
216ab 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
216ac 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54  e);.  for(i=OMIT
216ad 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e  _TEMPDB; i<db->n
216ae 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  Db; i++){.    in
216af 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e  t j = (i<2) ? i^
216b0 31 20 3a 20 69 3b 20 20 20 2f 2a 20 53 65 61 72  1 : i;   /* Sear
216b1 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d  ch TEMP before M
216b2 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a  AIN */.    if( z
216b3 44 61 74 61 62 61 73 65 21 3d 30 20 26 26 20 73  Database!=0 && s
216b4 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44  qlite3StrICmp(zD
216b5 61 74 61 62 61 73 65 2c 20 64 62 2d 3e 61 44 62  atabase, db->aDb
216b6 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e  [j].zName) ) con
216b7 74 69 6e 75 65 3b 0a 20 20 20 20 70 20 3d 20 73  tinue;.    p = s
216b8 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
216b9 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65  db->aDb[j].pSche
216ba 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e 61  ma->tblHash, zNa
216bb 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
216bc 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a 20  if( p ) break;. 
216bd 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
216be 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74  ../*.** Locate t
216bf 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72  he in-memory str
216c0 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63  ucture that desc
216c1 72 69 62 65 73 20 61 20 70 61 72 74 69 63 75 6c  ribes a particul
216c2 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74  ar database.** t
216c3 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e  able given the n
216c4 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c  ame of that tabl
216c5 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c  e and (optionall
216c6 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  y) the name of t
216c7 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  he.** database c
216c8 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
216c9 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  ble.  Return NUL
216ca 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 20  L if not found. 
216cb 20 41 6c 73 6f 20 6c 65 61 76 65 20 61 6e 0a 2a   Also leave an.*
216cc 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  * error message 
216cd 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  in pParse->zErrM
216ce 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69  sg..**.** The di
216cf 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
216d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6e   this routine an
216d1 64 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  d sqlite3FindTab
216d2 6c 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69  le() is that thi
216d3 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6c 65 61  s.** routine lea
216d4 76 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ves an error mes
216d5 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e  sage in pParse->
216d6 7a 45 72 72 4d 73 67 20 77 68 65 72 65 0a 2a 2a  zErrMsg where.**
216d7 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
216d8 65 28 29 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f  e() does not..*/
216d9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
216da 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f  Table *sqlite3Lo
216db 63 61 74 65 54 61 62 6c 65 28 0a 20 20 50 61 72  cateTable(.  Par
216dc 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
216dd 20 20 20 20 2f 2a 20 63 6f 6e 74 65 78 74 20 69      /* context i
216de 6e 20 77 68 69 63 68 20 74 6f 20 72 65 70 6f 72  n which to repor
216df 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e  t errors */.  in
216e0 74 20 69 73 56 69 65 77 2c 20 20 20 20 20 20 20  t isView,       
216e1 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
216e2 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 56 49  looking for a VI
216e3 45 57 20 72 61 74 68 65 72 20 74 68 61 6e 20 61  EW rather than a
216e4 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 6f 6e 73   TABLE */.  cons
216e5 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
216e6 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
216e7 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 6c  e table we are l
216e8 6f 6f 6b 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20  ooking for */.  
216e9 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 61  const char *zDba
216ea 73 65 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  se     /* Name o
216eb 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  f the database. 
216ec 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a   Might be NULL *
216ed 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  /.){.  Table *p;
216ee 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20  ..  /* Read the 
216ef 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
216f0 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
216f1 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 72  urs, leave an er
216f2 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a  ror message.  **
216f3 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 61   and code in pPa
216f4 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  rse and return N
216f5 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51  ULL. */.  if( SQ
216f6 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
216f7 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
216f8 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  e) ){.    return
216f9 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73   0;.  }..  p = s
216fa 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
216fb 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d  pParse->db, zNam
216fc 65 2c 20 7a 44 62 61 73 65 29 3b 0a 20 20 69 66  e, zDbase);.  if
216fd 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f  ( p==0 ){.    co
216fe 6e 73 74 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d  nst char *zMsg =
216ff 20 69 73 56 69 65 77 20 3f 20 22 6e 6f 20 73 75   isView ? "no su
21700 63 68 20 76 69 65 77 22 20 3a 20 22 6e 6f 20 73  ch view" : "no s
21701 75 63 68 20 74 61 62 6c 65 22 3b 0a 20 20 20 20  uch table";.    
21702 69 66 28 20 7a 44 62 61 73 65 20 29 7b 0a 20 20  if( zDbase ){.  
21703 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
21704 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a  Msg(pParse, "%s:
21705 20 25 73 2e 25 73 22 2c 20 7a 4d 73 67 2c 20 7a   %s.%s", zMsg, z
21706 44 62 61 73 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20  Dbase, zName);. 
21707 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21708 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
21709 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 22  pParse, "%s: %s"
2170a 2c 20 7a 4d 73 67 2c 20 7a 4e 61 6d 65 29 3b 0a  , zMsg, zName);.
2170b 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65      }.    pParse
2170c 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20  ->checkSchema = 
2170d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
2170e 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61  p;.}../*.** Loca
2170f 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  te the in-memory
21710 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
21711 64 65 73 63 72 69 62 65 73 20 0a 2a 2a 20 61 20  describes .** a 
21712 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65 78  particular index
21713 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20   given the name 
21714 6f 66 20 74 68 61 74 20 69 6e 64 65 78 0a 2a 2a  of that index.**
21715 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f 66   and the name of
21716 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68   the database th
21717 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
21718 69 6e 64 65 78 2e 0a 2a 2a 20 52 65 74 75 72 6e  index..** Return
21719 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75   NULL if not fou
2171a 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44 61  nd..**.** If zDa
2171b 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 6c 6c  tabase is 0, all
2171c 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20 73   databases are s
2171d 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 65 0a  earched for the.
2171e 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65  ** table and the
2171f 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20   first matching 
21720 69 6e 64 65 78 20 69 73 20 72 65 74 75 72 6e 65  index is returne
21721 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e 67  d.  (No checking
21722 0a 2a 2a 20 66 6f 72 20 64 75 70 6c 69 63 61 74  .** for duplicat
21723 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 69 73  e index names is
21724 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 65 61   done.)  The sea
21725 72 63 68 20 6f 72 64 65 72 20 69 73 0a 2a 2a 20  rch order is.** 
21726 54 45 4d 50 20 66 69 72 73 74 2c 20 74 68 65 6e  TEMP first, then
21727 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79 20   MAIN, then any 
21728 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
21729 73 65 73 20 61 64 64 65 64 0a 2a 2a 20 75 73 69  ses added.** usi
2172a 6e 67 20 74 68 65 20 41 54 54 41 43 48 20 63 6f  ng the ATTACH co
2172b 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  mmand..*/.SQLITE
2172c 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a  _PRIVATE Index *
2172d 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78  sqlite3FindIndex
2172e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
2172f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
21730 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
21731 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 20 3d 20  ){.  Index *p = 
21732 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  0;.  int i;.  in
21733 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  t nName = sqlite
21734 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
21735 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54  ;.  for(i=OMIT_T
21736 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62  EMPDB; i<db->nDb
21737 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  ; i++){.    int 
21738 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20  j = (i<2) ? i^1 
21739 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68 20  : i;  /* Search 
2173a 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e  TEMP before MAIN
2173b 20 2a 2f 0a 20 20 20 20 53 63 68 65 6d 61 20 2a   */.    Schema *
2173c 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44  pSchema = db->aD
2173d 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20  b[j].pSchema;.  
2173e 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c    if( zDb && sql
2173f 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 62 2c  ite3StrICmp(zDb,
21740 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d   db->aDb[j].zNam
21741 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  e) ) continue;. 
21742 20 20 20 61 73 73 65 72 74 28 20 70 53 63 68 65     assert( pSche
21743 6d 61 20 7c 7c 20 28 6a 3d 3d 31 20 26 26 20 21  ma || (j==1 && !
21744 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 20  db->aDb[1].pBt) 
21745 29 3b 0a 20 20 20 20 69 66 28 20 70 53 63 68 65  );.    if( pSche
21746 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20  ma ){.      p = 
21747 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
21748 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73  &pSchema->idxHas
21749 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
2174a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2174b 70 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20  p ) break;.  }. 
2174c 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
2174d 0a 2a 2a 20 52 65 63 6c 61 69 6d 20 74 68 65 20  .** Reclaim the 
2174e 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61  memory used by a
2174f 6e 20 69 6e 64 65 78 0a 2a 2f 0a 73 74 61 74 69  n index.*/.stati
21750 63 20 76 6f 69 64 20 66 72 65 65 49 6e 64 65 78  c void freeIndex
21751 28 49 6e 64 65 78 20 2a 70 29 7b 0a 20 20 73 71  (Index *p){.  sq
21752 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 70  lite3 *db = p->p
21753 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20  Table->dbMem;.  
21754 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
21755 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20  , p->zColAff);. 
21756 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
21757 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  b, p);.}../*.** 
21758 52 65 6d 6f 76 65 20 74 68 65 20 67 69 76 65 6e  Remove the given
21759 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20   index from the 
2175a 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65  index hash table
2175b 2c 20 61 6e 64 20 66 72 65 65 0a 2a 2a 20 69 74  , and free.** it
2175c 73 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75  s memory structu
2175d 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  res..**.** The i
2175e 6e 64 65 78 20 69 73 20 72 65 6d 6f 76 65 64 20  ndex is removed 
2175f 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
21760 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 62 75  e hash tables bu
21761 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 75  t.** it is not u
21762 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74 68 65  nlinked from the
21763 20 54 61 62 6c 65 20 74 68 61 74 20 69 74 20 69   Table that it i
21764 6e 64 65 78 65 73 2e 0a 2a 2a 20 55 6e 6c 69 6e  ndexes..** Unlin
21765 6b 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 54 61  king from the Ta
21766 62 6c 65 20 6d 75 73 74 20 62 65 20 64 6f 6e 65  ble must be done
21767 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   by the calling 
21768 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
21769 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
2176a 44 65 6c 65 74 65 49 6e 64 65 78 28 49 6e 64 65  DeleteIndex(Inde
2176b 78 20 2a 70 29 7b 0a 20 20 49 6e 64 65 78 20 2a  x *p){.  Index *
2176c 70 4f 6c 64 3b 0a 20 20 63 6f 6e 73 74 20 63 68  pOld;.  const ch
2176d 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d 3e 7a  ar *zName = p->z
2176e 4e 61 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20 3d 20  Name;..  pOld = 
2176f 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
21770 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d 3e 69  t(&p->pSchema->i
21771 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 0a 20  dxHash, zName,. 
21772 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21773 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
21774 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
21775 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20  , 0);.  assert( 
21776 70 4f 6c 64 3d 3d 30 20 7c 7c 20 70 4f 6c 64 3d  pOld==0 || pOld=
21777 3d 70 20 29 3b 0a 20 20 66 72 65 65 49 6e 64 65  =p );.  freeInde
21778 78 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  x(p);.}../*.** F
21779 6f 72 20 74 68 65 20 69 6e 64 65 78 20 63 61 6c  or the index cal
2177a 6c 65 64 20 7a 49 64 78 4e 61 6d 65 20 77 68 69  led zIdxName whi
2177b 63 68 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74  ch is found in t
2177c 68 65 20 64 61 74 61 62 61 73 65 20 69 44 62 2c  he database iDb,
2177d 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74 68 61 74 20  .** unlike that 
2177e 69 6e 64 65 78 20 66 72 6f 6d 20 69 74 73 20 54  index from its T
2177f 61 62 6c 65 20 74 68 65 6e 20 72 65 6d 6f 76 65  able then remove
21780 20 74 68 65 20 69 6e 64 65 78 20 66 72 6f 6d 0a   the index from.
21781 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 68 61 73  ** the index has
21782 68 20 74 61 62 6c 65 20 61 6e 64 20 66 72 65 65  h table and free
21783 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 73 74 72 75   all memory stru
21784 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74 65  ctures associate
21785 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 69 6e  d.** with the in
21786 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  dex..*/.SQLITE_P
21787 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21788 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
21789 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20  teIndex(sqlite3 
2178a 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f  *db, int iDb, co
2178b 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 78 4e 61  nst char *zIdxNa
2178c 6d 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49  me){.  Index *pI
2178d 6e 64 65 78 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b  ndex;.  int len;
2178e 0a 20 20 48 61 73 68 20 2a 70 48 61 73 68 20 3d  .  Hash *pHash =
2178f 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70   &db->aDb[iDb].p
21790 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b  Schema->idxHash;
21791 0a 0a 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65  ..  len = sqlite
21792 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 78 4e 61  3Strlen30(zIdxNa
21793 6d 65 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20  me);.  pIndex = 
21794 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
21795 74 28 70 48 61 73 68 2c 20 7a 49 64 78 4e 61 6d  t(pHash, zIdxNam
21796 65 2c 20 6c 65 6e 2c 20 30 29 3b 0a 20 20 69 66  e, len, 0);.  if
21797 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  ( pIndex ){.    
21798 69 66 28 20 70 49 6e 64 65 78 2d 3e 70 54 61 62  if( pIndex->pTab
21799 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 6e 64  le->pIndex==pInd
2179a 65 78 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64  ex ){.      pInd
2179b 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64  ex->pTable->pInd
2179c 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65  ex = pIndex->pNe
2179d 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  xt;.    }else{. 
2179e 20 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20       Index *p;. 
2179f 20 20 20 20 20 66 6f 72 28 70 3d 70 49 6e 64 65       for(p=pInde
217a0 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65  x->pTable->pInde
217a1 78 3b 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74  x; p && p->pNext
217a2 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70 2d 3e 70  !=pIndex; p=p->p
217a3 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20 20 69 66  Next){}.      if
217a4 28 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d  ( p && p->pNext=
217a5 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20  =pIndex ){.     
217a6 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 49     p->pNext = pI
217a7 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ndex->pNext;.   
217a8 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66     }.    }.    f
217a9 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29  reeIndex(pIndex)
217aa 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67  ;.  }.  db->flag
217ab 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65  s |= SQLITE_Inte
217ac 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a  rnChanges;.}../*
217ad 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 73 63  .** Erase all sc
217ae 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hema information
217af 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d   from the in-mem
217b0 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65 73 20  ory hash tables 
217b1 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64  of.** a single d
217b2 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72  atabase.  This r
217b3 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
217b4 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f   to reclaim memo
217b5 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65  ry.** before the
217b6 20 64 61 74 61 62 61 73 65 20 63 6c 6f 73 65 73   database closes
217b7 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 20 63 61  .  It is also ca
217b8 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 72 6f  lled during a ro
217b9 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 74 68 65  llback.** if the
217ba 72 65 20 77 65 72 65 20 73 63 68 65 6d 61 20 63  re were schema c
217bb 68 61 6e 67 65 73 20 64 75 72 69 6e 67 20 74 68  hanges during th
217bc 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72  e transaction or
217bd 20 69 66 20 61 0a 2a 2a 20 73 63 68 65 6d 61 2d   if a.** schema-
217be 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74 63 68 20  cookie mismatch 
217bf 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  occurs..**.** If
217c0 20 69 44 62 3d 3d 30 20 74 68 65 6e 20 72 65 73   iDb==0 then res
217c1 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  et the internal 
217c2 73 63 68 65 6d 61 20 74 61 62 6c 65 73 20 66 6f  schema tables fo
217c3 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a  r all database.*
217c4 2a 20 66 69 6c 65 73 2e 20 20 49 66 20 69 44 62  * files.  If iDb
217c5 3e 3d 31 20 74 68 65 6e 20 72 65 73 65 74 20 74  >=1 then reset t
217c6 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  he internal sche
217c7 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74 68 65 0a  ma for only the.
217c8 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 69  ** single file i
217c9 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c  ndicated..*/.SQL
217ca 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
217cb 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
217cc 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69  ernalSchema(sqli
217cd 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
217ce 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  ){.  int i, j;. 
217cf 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
217d0 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
217d1 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20  ;..  if( iDb==0 
217d2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
217d3 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
217d4 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 44 62  .  }.  for(i=iDb
217d5 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
217d6 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d  ){.    Db *pDb =
217d7 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20   &db->aDb[i];.  
217d8 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65    if( pDb->pSche
217d9 6d 61 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  ma ){.      asse
217da 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70 44 62 2d  rt(i==1 || (pDb-
217db 3e 70 42 74 20 26 26 20 73 71 6c 69 74 65 33 42  >pBt && sqlite3B
217dc 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
217dd 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20 20 20 20  Db->pBt)));.    
217de 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46    sqlite3SchemaF
217df 72 65 65 28 70 44 62 2d 3e 70 53 63 68 65 6d 61  ree(pDb->pSchema
217e0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
217e1 20 69 44 62 3e 30 20 29 20 72 65 74 75 72 6e 3b   iDb>0 ) return;
217e2 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
217e3 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62 2d 3e 66  Db==0 );.  db->f
217e4 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f  lags &= ~SQLITE_
217e5 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
217e6 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
217e7 76 65 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 2f 2a  veAll(db);..  /*
217e8 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   If one or more 
217e9 6f 66 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  of the auxiliary
217ea 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
217eb 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 2c  has been closed,
217ec 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 6d 6f 76  .  ** then remov
217ed 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20  e them from the 
217ee 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
217ef 73 65 20 6c 69 73 74 2e 20 20 57 65 20 74 61 6b  se list.  We tak
217f0 65 20 74 68 65 0a 20 20 2a 2a 20 6f 70 70 6f 72  e the.  ** oppor
217f1 74 75 6e 69 74 79 20 74 6f 20 64 6f 20 74 68 69  tunity to do thi
217f2 73 20 68 65 72 65 20 73 69 6e 63 65 20 77 65 20  s here since we 
217f3 68 61 76 65 20 6a 75 73 74 20 64 65 6c 65 74 65  have just delete
217f4 64 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 2a  d all of the.  *
217f5 2a 20 73 63 68 65 6d 61 20 68 61 73 68 20 74 61  * schema hash ta
217f6 62 6c 65 73 20 61 6e 64 20 74 68 65 72 65 66 6f  bles and therefo
217f7 72 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74  re do not have t
217f8 6f 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67  o make any chang
217f9 65 73 0a 20 20 2a 2a 20 74 6f 20 61 6e 79 20 6f  es.  ** to any o
217fa 66 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a  f those tables..
217fb 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
217fc 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
217fd 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 20 2a  .    struct Db *
217fe 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
217ff 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e  ];.    if( pDb->
21800 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pBt==0 ){.      
21801 69 66 28 20 70 44 62 2d 3e 70 41 75 78 20 26 26  if( pDb->pAux &&
21802 20 70 44 62 2d 3e 78 46 72 65 65 41 75 78 20 29   pDb->xFreeAux )
21803 20 70 44 62 2d 3e 78 46 72 65 65 41 75 78 28 70   pDb->xFreeAux(p
21804 44 62 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 20  Db->pAux);.     
21805 20 70 44 62 2d 3e 70 41 75 78 20 3d 20 30 3b 0a   pDb->pAux = 0;.
21806 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28      }.  }.  for(
21807 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e 6e 44 62  i=j=2; i<db->nDb
21808 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
21809 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62  ct Db *pDb = &db
2180a 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66  ->aDb[i];.    if
2180b 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b  ( pDb->pBt==0 ){
2180c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2180d 46 72 65 65 28 64 62 2c 20 70 44 62 2d 3e 7a 4e  Free(db, pDb->zN
2180e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 44 62 2d  ame);.      pDb-
2180f 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20  >zName = 0;.    
21810 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
21811 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69 20 29 7b  }.    if( j<i ){
21812 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 6a  .      db->aDb[j
21813 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a  ] = db->aDb[i];.
21814 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20      }.    j++;. 
21815 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 64 62 2d   }.  memset(&db-
21816 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28 64 62 2d  >aDb[j], 0, (db-
21817 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f 66 28 64  >nDb-j)*sizeof(d
21818 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a 20 20 64  b->aDb[j]));.  d
21819 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 20 69 66  b->nDb = j;.  if
2181a 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 26 26 20  ( db->nDb<=2 && 
2181b 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e 61 44 62  db->aDb!=db->aDb
2181c 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 6d 65  Static ){.    me
2181d 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 74 61 74  mcpy(db->aDbStat
2181e 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 32 2a 73  ic, db->aDb, 2*s
2181f 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d  izeof(db->aDb[0]
21820 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  ));.    sqlite3D
21821 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 44  bFree(db, db->aD
21822 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 20  b);.    db->aDb 
21823 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b  = db->aDbStatic;
21824 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
21825 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
21826 6c 6c 65 64 20 77 68 65 6e 20 61 20 63 6f 6d 6d  lled when a comm
21827 69 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51  it occurs..*/.SQ
21828 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
21829 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49  d sqlite3CommitI
2182a 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73  nternalChanges(s
2182b 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64  qlite3 *db){.  d
2182c 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
2182d 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
2182e 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  s;.}../*.** Clea
2182f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  r the column nam
21830 65 73 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20  es from a table 
21831 6f 72 20 76 69 65 77 2e 0a 2a 2f 0a 73 74 61 74  or view..*/.stat
21832 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 52 65  ic void sqliteRe
21833 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 54  setColumnNames(T
21834 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20  able *pTable){. 
21835 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e   int i;.  Column
21836 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65   *pCol;.  sqlite
21837 33 20 2a 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e  3 *db = pTable->
21838 64 62 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  dbMem;.  assert(
21839 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20   pTable!=0 );.  
2183a 69 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62  if( (pCol = pTab
2183b 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a  le->aCol)!=0 ){.
2183c 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2183d 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  Table->nCol; i++
2183e 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
2183f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
21840 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b  b, pCol->zName);
21841 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
21842 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  prDelete(db, pCo
21843 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20  l->pDflt);.     
21844 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
21845 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b  b, pCol->zType);
21846 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
21847 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a  Free(db, pCol->z
21848 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Coll);.    }.   
21849 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2184a 62 2c 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29  b, pTable->aCol)
2184b 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e  ;.  }.  pTable->
2184c 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62  aCol = 0;.  pTab
2184d 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a  le->nCol = 0;.}.
2184e 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  ./*.** Remove th
2184f 65 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74  e memory data st
21850 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61  ructures associa
21851 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76  ted with the giv
21852 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f  en.** Table.  No
21853 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64   changes are mad
21854 65 20 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69  e to disk by thi
21855 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a  s routine..**.**
21856 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75   This routine ju
21857 73 74 20 64 65 6c 65 74 65 73 20 74 68 65 20 64  st deletes the d
21858 61 74 61 20 73 74 72 75 63 74 75 72 65 2e 20 20  ata structure.  
21859 49 74 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69  It does not unli
2185a 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  nk.** the table 
2185b 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 66  data structure f
2185c 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62  rom the hash tab
2185d 6c 65 2e 20 20 42 75 74 20 69 74 20 64 6f 65 73  le.  But it does
2185e 20 64 65 73 74 72 6f 79 0a 2a 2a 20 6d 65 6d 6f   destroy.** memo
2185f 72 79 20 73 74 72 75 63 74 75 72 65 73 20 6f 66  ry structures of
21860 20 74 68 65 20 69 6e 64 69 63 65 73 20 61 6e 64   the indices and
21861 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73   foreign keys as
21862 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a  sociated with .*
21863 2a 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  * the table..*/.
21864 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
21865 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
21866 65 54 61 62 6c 65 28 54 61 62 6c 65 20 2a 70 54  eTable(Table *pT
21867 61 62 6c 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a  able){.  Index *
21868 70 49 6e 64 65 78 2c 20 2a 70 4e 65 78 74 3b 0a  pIndex, *pNext;.
21869 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 2a    FKey *pFKey, *
2186a 70 4e 65 78 74 46 4b 65 79 3b 0a 20 20 73 71 6c  pNextFKey;.  sql
2186b 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28  ite3 *db;..  if(
2186c 20 70 54 61 62 6c 65 3d 3d 30 20 29 20 72 65 74   pTable==0 ) ret
2186d 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 54 61 62  urn;.  db = pTab
2186e 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 0a 20 20 2f 2a  le->dbMem;..  /*
2186f 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74   Do not delete t
21870 68 65 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 74  he table until t
21871 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
21872 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e  nt reaches zero.
21873 20 2a 2f 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52   */.  pTable->nR
21874 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 54 61 62  ef--;.  if( pTab
21875 6c 65 2d 3e 6e 52 65 66 3e 30 20 29 7b 0a 20 20  le->nRef>0 ){.  
21876 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
21877 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e  assert( pTable->
21878 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a  nRef==0 );..  /*
21879 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69   Delete all indi
2187a 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
2187b 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 0a 20  ith this table. 
2187c 20 2a 2f 0a 20 20 66 6f 72 28 70 49 6e 64 65 78   */.  for(pIndex
2187d 20 3d 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65   = pTable->pInde
2187e 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  x; pIndex; pInde
2187f 78 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e  x=pNext){.    pN
21880 65 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e  ext = pIndex->pN
21881 65 78 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ext;.    assert(
21882 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61   pIndex->pSchema
21883 3d 3d 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d  ==pTable->pSchem
21884 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  a );.    sqlite3
21885 44 65 6c 65 74 65 49 6e 64 65 78 28 70 49 6e 64  DeleteIndex(pInd
21886 65 78 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  ex);.  }..#ifnde
21887 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
21888 52 45 49 47 4e 5f 4b 45 59 0a 20 20 2f 2a 20 44  REIGN_KEY.  /* D
21889 65 6c 65 74 65 20 61 6c 6c 20 66 6f 72 65 69 67  elete all foreig
2188a 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 74 65  n keys associate
2188b 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c  d with this tabl
2188c 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65  e. */.  for(pFKe
2188d 79 3d 70 54 61 62 6c 65 2d 3e 70 46 4b 65 79 3b  y=pTable->pFKey;
2188e 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 4e   pFKey; pFKey=pN
2188f 65 78 74 46 4b 65 79 29 7b 0a 20 20 20 20 70 4e  extFKey){.    pN
21890 65 78 74 46 4b 65 79 20 3d 20 70 46 4b 65 79 2d  extFKey = pFKey-
21891 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20  >pNextFrom;.    
21892 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
21893 2c 20 70 46 4b 65 79 29 3b 0a 20 20 7d 0a 23 65  , pFKey);.  }.#e
21894 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 65 6c 65 74  ndif..  /* Delet
21895 65 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  e the Table stru
21896 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 20 20  cture itself..  
21897 2a 2f 0a 20 20 73 71 6c 69 74 65 52 65 73 65 74  */.  sqliteReset
21898 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62  ColumnNames(pTab
21899 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  le);.  sqlite3Db
2189a 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d  Free(db, pTable-
2189b 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  >zName);.  sqlit
2189c 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61  e3DbFree(db, pTa
2189d 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20  ble->zColAff);. 
2189e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2189f 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 2d  lete(db, pTable-
218a0 3e 70 53 65 6c 65 63 74 29 3b 0a 23 69 66 6e 64  >pSelect);.#ifnd
218a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
218a2 48 45 43 4b 0a 20 20 73 71 6c 69 74 65 33 45 78  HECK.  sqlite3Ex
218a3 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61  prDelete(db, pTa
218a4 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b 0a 23 65  ble->pCheck);.#e
218a5 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 56 74  ndif.  sqlite3Vt
218a6 61 62 43 6c 65 61 72 28 70 54 61 62 6c 65 29 3b  abClear(pTable);
218a7 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
218a8 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 7d 0a  (db, pTable);.}.
218a9 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68  ./*.** Unlink th
218aa 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20 66 72  e given table fr
218ab 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
218ac 65 73 20 61 6e 64 20 74 68 65 20 64 65 6c 65 74  es and the delet
218ad 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 73  e the.** table s
218ae 74 72 75 63 74 75 72 65 20 77 69 74 68 20 61 6c  tructure with al
218af 6c 20 69 74 73 20 69 6e 64 69 63 65 73 20 61 6e  l its indices an
218b0 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0a  d foreign keys..
218b1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
218b2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e  E void sqlite3Un
218b3 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62  linkAndDeleteTab
218b4 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  le(sqlite3 *db, 
218b5 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63  int iDb, const c
218b6 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 29 7b 0a  har *zTabName){.
218b7 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 44 62    Table *p;.  Db
218b8 20 2a 70 44 62 3b 0a 0a 20 20 61 73 73 65 72 74   *pDb;..  assert
218b9 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73  ( db!=0 );.  ass
218ba 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
218bb 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
218bc 61 73 73 65 72 74 28 20 7a 54 61 62 4e 61 6d 65  assert( zTabName
218bd 20 26 26 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 20   && zTabName[0] 
218be 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  );.  pDb = &db->
218bf 61 44 62 5b 69 44 62 5d 3b 0a 20 20 70 20 3d 20  aDb[iDb];.  p = 
218c0 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
218c1 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  t(&pDb->pSchema-
218c2 3e 74 62 6c 48 61 73 68 2c 20 7a 54 61 62 4e 61  >tblHash, zTabNa
218c3 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  me,.            
218c4 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
218c5 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 61 62  te3Strlen30(zTab
218c6 4e 61 6d 65 29 2c 30 29 3b 0a 20 20 73 71 6c 69  Name),0);.  sqli
218c7 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
218c8 29 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  );.  db->flags |
218c9 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  = SQLITE_InternC
218ca 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  hanges;.}../*.**
218cb 20 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20   Given a token, 
218cc 72 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 20  return a string 
218cd 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f 66  that consists of
218ce 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 61   the text of tha
218cf 74 0a 2a 2a 20 74 6f 6b 65 6e 2e 20 20 53 70 61  t.** token.  Spa
218d0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
218d1 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a  eturned string.*
218d2 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  * is obtained fr
218d3 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
218d4 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72  ) and must be fr
218d5 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69  eed by the calli
218d6 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a  ng.** function..
218d7 2a 2a 0a 2a 2a 20 41 6e 79 20 71 75 6f 74 61 74  **.** Any quotat
218d8 69 6f 6e 20 6d 61 72 6b 73 20 28 65 78 3a 20 20  ion marks (ex:  
218d9 22 6e 61 6d 65 22 2c 20 27 6e 61 6d 65 27 2c 20  "name", 'name', 
218da 5b 6e 61 6d 65 5d 2c 20 6f 72 20 60 6e 61 6d 65  [name], or `name
218db 60 29 20 74 68 61 74 0a 2a 2a 20 73 75 72 72 6f  `) that.** surro
218dc 75 6e 64 20 74 68 65 20 62 6f 64 79 20 6f 66 20  und the body of 
218dd 74 68 65 20 74 6f 6b 65 6e 20 61 72 65 20 72 65  the token are re
218de 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b  moved..**.** Tok
218df 65 6e 73 20 61 72 65 20 6f 66 74 65 6e 20 6a 75  ens are often ju
218e0 73 74 20 70 6f 69 6e 74 65 72 73 20 69 6e 74 6f  st pointers into
218e1 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
218e2 4c 20 74 65 78 74 20 61 6e 64 20 73 6f 0a 2a 2a  L text and so.**
218e3 20 61 72 65 20 6e 6f 74 20 5c 30 30 30 20 74 65   are not \000 te
218e4 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 61 72 65  rminated and are
218e5 20 6e 6f 74 20 70 65 72 73 69 73 74 65 6e 74 2e   not persistent.
218e6 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73    The returned s
218e7 74 72 69 6e 67 0a 2a 2a 20 69 73 20 5c 30 30 30  tring.** is \000
218e8 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20   terminated and 
218e9 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 2a  is persistent..*
218ea 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
218eb 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61   char *sqlite3Na
218ec 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69  meFromToken(sqli
218ed 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a  te3 *db, Token *
218ee 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a  pName){.  char *
218ef 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 70 4e 61  zName;.  if( pNa
218f0 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20  me ){.    zName 
218f1 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  = sqlite3DbStrND
218f2 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 4e  up(db, (char*)pN
218f3 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e  ame->z, pName->n
218f4 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 61 6d 65  );.    if( pName
218f5 2d 3e 71 75 6f 74 65 64 20 29 20 73 71 6c 69 74  ->quoted ) sqlit
218f6 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65 29  e3Dequote(zName)
218f7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
218f8 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Name = 0;.  }.  
218f9 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a  return zName;.}.
218fa 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20  ./*.** Open the 
218fb 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
218fc 62 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61  ble stored in da
218fd 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 69 44  tabase number iD
218fe 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  b for.** writing
218ff 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6f  . The table is o
21900 70 65 6e 65 64 20 75 73 69 6e 67 20 63 75 72 73  pened using curs
21901 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  or 0..*/.SQLITE_
21902 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21903 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61  ite3OpenMasterTa
21904 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20 69 6e  ble(Parse *p, in
21905 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a  t iDb){.  Vdbe *
21906 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
21907 62 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  be(p);.  sqlite3
21908 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44 62  TableLock(p, iDb
21909 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31  , MASTER_ROOT, 1
2190a 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
2190b 44 62 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  Db));.  sqlite3V
2190c 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2190d 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c 20 4d 41  OpenWrite, 0, MA
2190e 53 54 45 52 5f 52 4f 4f 54 2c 20 69 44 62 29 3b  STER_ROOT, iDb);
2190f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
21910 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63  angeP4(v, -1, (c
21911 68 61 72 20 2a 29 35 2c 20 50 34 5f 49 4e 54 33  har *)5, P4_INT3
21912 32 29 3b 20 20 2f 2a 20 35 20 63 6f 6c 75 6d 6e  2);  /* 5 column
21913 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20   table */.  if( 
21914 70 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b 0a 20 20  p->nTab==0 ){.  
21915 20 20 70 2d 3e 6e 54 61 62 20 3d 20 31 3b 0a 20    p->nTab = 1;. 
21916 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61   }.}../*.** Para
21917 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f 69 6e  meter zName poin
21918 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74 65 72 6d  ts to a nul-term
21919 69 6e 61 74 65 64 20 62 75 66 66 65 72 20 63 6f  inated buffer co
2191a 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 61 6d  ntaining the nam
2191b 65 0a 2a 2a 20 6f 66 20 61 20 64 61 74 61 62 61  e.** of a databa
2191c 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d  se ("main", "tem
2191d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 6f  p" or the name o
2191e 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 62  f an attached db
2191f 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74  ). This.** funct
21920 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  ion returns the 
21921 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d  index of the nam
21922 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64  ed database in d
21923 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a 2a 2a 20  b->aDb[], or.** 
21924 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20  -1 if the named 
21925 64 62 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  db cannot be fou
21926 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nd..*/.SQLITE_PR
21927 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
21928 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69  3FindDbName(sqli
21929 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
2192a 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69  har *zName){.  i
2192b 6e 74 20 69 20 3d 20 2d 31 3b 20 20 20 20 20 20  nt i = -1;      
2192c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e     /* Database n
2192d 75 6d 62 65 72 20 2a 2f 0a 20 20 69 66 28 20 7a  umber */.  if( z
2192e 4e 61 6d 65 20 29 7b 0a 20 20 20 20 44 62 20 2a  Name ){.    Db *
2192f 70 44 62 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  pDb;.    int n =
21930 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
21931 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66 6f 72  (zName);.    for
21932 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 29 2c 20  (i=(db->nDb-1), 
21933 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b 69 5d 3b  pDb=&db->aDb[i];
21934 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 44 62 2d   i>=0; i--, pDb-
21935 2d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 21  -){.      if( (!
21936 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c 7c 20 69  OMIT_TEMPDB || i
21937 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73 71 6c 69  !=1 ) && n==sqli
21938 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44 62 2d  te3Strlen30(pDb-
21939 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20  >zName) && .    
2193a 20 20 20 20 20 20 30 3d 3d 73 71 6c 69 74 65 33        0==sqlite3
2193b 53 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61  StrICmp(pDb->zNa
2193c 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  me, zName) ){.  
2193d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2193e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
2193f 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a   return i;.}../*
21940 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 6e 20 2a 70  .** The token *p
21941 4e 61 6d 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  Name contains th
21942 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61  e name of a data
21943 62 61 73 65 20 28 65 69 74 68 65 72 20 22 6d 61  base (either "ma
21944 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 65 6d 70 22  in" or.** "temp"
21945 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   or the name of 
21946 61 6e 20 61 74 74 61 63 68 65 64 20 64 62 29 2e  an attached db).
21947 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
21948 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64  turns the.** ind
21949 65 78 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20  ex of the named 
2194a 64 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e  database in db->
2194b 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 20 69 66 20  aDb[], or -1 if 
2194c 74 68 65 20 6e 61 6d 65 64 20 64 62 20 0a 2a 2a  the named db .**
2194d 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e   does not exist.
2194e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2194f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
21950 6e 64 44 62 28 73 71 6c 69 74 65 33 20 2a 64 62  ndDb(sqlite3 *db
21951 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b  , Token *pName){
21952 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
21953 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21954 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
21955 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ase number */.  
21956 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
21957 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21958 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 77 65 20       /* Name we 
21959 61 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f  are searching fo
2195a 72 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73  r */.  zName = s
2195b 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
2195c 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ken(db, pName);.
2195d 20 20 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    i = sqlite3Fin
2195e 64 44 62 4e 61 6d 65 28 64 62 2c 20 7a 4e 61 6d  dDbName(db, zNam
2195f 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e);.  sqlite3DbF
21960 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  ree(db, zName);.
21961 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f    return i;.}../
21962 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76  * The table or v
21963 69 65 77 20 6f 72 20 74 72 69 67 67 65 72 20 6e  iew or trigger n
21964 61 6d 65 20 69 73 20 70 61 73 73 65 64 20 74 6f  ame is passed to
21965 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69   this routine vi
21966 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70 4e 61 6d  a tokens.** pNam
21967 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49  e1 and pName2. I
21968 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  f the table name
21969 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69   was fully quali
2196a 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c  fied, for exampl
2196b 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20  e:.**.** CREATE 
2196c 54 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e  TABLE xxx.yyy (.
2196d 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 6e  ..);.** .** Then
2196e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74   pName1 is set t
2196f 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d  o "xxx" and pNam
21970 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65  e2 "yyy". On the
21971 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a 2a   other hand if.*
21972 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  * the table name
21973 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75   is not fully qu
21974 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 2a  alified, i.e.:.*
21975 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c  *.** CREATE TABL
21976 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a  E yyy(...);.**.*
21977 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73  * Then pName1 is
21978 20 73 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e   set to "yyy" an
21979 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a  d pName2 is ""..
2197a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2197b 6e 65 20 73 65 74 73 20 74 68 65 20 2a 70 70 55  ne sets the *ppU
2197c 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72 20 74 6f  nqual pointer to
2197d 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f   point at the to
2197e 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 2a  ken (pName1 or.*
2197f 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73  * pName2) that s
21980 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c  tores the unqual
21981 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65  ified table name
21982 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  .  The index of 
21983 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
21984 22 78 78 78 22 20 69 73 20 72 65 74 75 72 6e 65  "xxx" is returne
21985 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
21986 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
21987 54 77 6f 50 61 72 74 4e 61 6d 65 28 0a 20 20 50  TwoPartName(.  P
21988 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
21989 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
2198a 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
2198b 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
2198c 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20  oken *pName1,   
2198d 20 20 20 2f 2a 20 54 68 65 20 22 78 78 78 22 20     /* The "xxx" 
2198e 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78  in the name "xxx
2198f 2e 79 79 79 22 20 6f 72 20 22 78 78 78 22 20 2a  .yyy" or "xxx" *
21990 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
21991 32 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22  2,      /* The "
21992 79 79 79 22 20 69 6e 20 74 68 65 20 6e 61 6d 65  yyy" in the name
21993 20 22 78 78 78 2e 79 79 79 22 20 2a 2f 0a 20 20   "xxx.yyy" */.  
21994 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75 61 6c 20  Token **pUnqual 
21995 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
21996 20 75 6e 71 75 61 6c 69 66 69 65 64 20 6f 62 6a   unqualified obj
21997 65 63 74 20 6e 61 6d 65 20 68 65 72 65 20 2a 2f  ect name here */
21998 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
21999 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2199a 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 6f    /* Database ho
2199b 6c 64 69 6e 67 20 74 68 65 20 6f 62 6a 65 63 74  lding the object
2199c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2199d 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2199e 0a 20 20 69 66 28 20 70 4e 61 6d 65 32 20 26 26  .  if( pName2 &&
2199f 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a   pName2->n>0 ){.
219a0 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74      if( db->init
219a1 2e 62 75 73 79 20 29 20 7b 0a 20 20 20 20 20 20  .busy ) {.      
219a2 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
219a3 70 50 61 72 73 65 2c 20 22 63 6f 72 72 75 70 74  pParse, "corrupt
219a4 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20   database");.   
219a5 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b     pParse->nErr+
219a6 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  +;.      return 
219a7 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  -1;.    }.    *p
219a8 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b  Unqual = pName2;
219a9 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
219aa 65 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61  e3FindDb(db, pNa
219ab 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44  me1);.    if( iD
219ac 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  b<0 ){.      sql
219ad 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
219ae 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61  rse, "unknown da
219af 74 61 62 61 73 65 20 25 54 22 2c 20 70 4e 61 6d  tabase %T", pNam
219b0 65 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  e1);.      pPars
219b1 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20  e->nErr++;.     
219b2 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20   return -1;.    
219b3 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  }.  }else{.    a
219b4 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
219b5 69 44 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e  iDb==0 || db->in
219b6 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 69  it.busy );.    i
219b7 44 62 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 69 44  Db = db->init.iD
219b8 62 3b 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20  b;.    *pUnqual 
219b9 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 0a 20 20  = pName1;.  }.  
219ba 72 65 74 75 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f  return iDb;.}../
219bb 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
219bc 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 65  e is used to che
219bd 63 6b 20 69 66 20 74 68 65 20 55 54 46 2d 38 20  ck if the UTF-8 
219be 73 74 72 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20  string zName is 
219bf 61 20 6c 65 67 61 6c 0a 2a 2a 20 75 6e 71 75 61  a legal.** unqua
219c0 6c 69 66 69 65 64 20 6e 61 6d 65 20 66 6f 72 20  lified name for 
219c1 61 20 6e 65 77 20 73 63 68 65 6d 61 20 6f 62 6a  a new schema obj
219c2 65 63 74 20 28 74 61 62 6c 65 2c 20 69 6e 64 65  ect (table, inde
219c3 78 2c 20 76 69 65 77 20 6f 72 0a 2a 2a 20 74 72  x, view or.** tr
219c4 69 67 67 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65  igger). All name
219c5 73 20 61 72 65 20 6c 65 67 61 6c 20 65 78 63 65  s are legal exce
219c6 70 74 20 74 68 6f 73 65 20 74 68 61 74 20 62 65  pt those that be
219c7 67 69 6e 20 77 69 74 68 20 74 68 65 20 73 74 72  gin with the str
219c8 69 6e 67 0a 2a 2a 20 22 73 71 6c 69 74 65 5f 22  ing.** "sqlite_"
219c9 20 28 69 6e 20 75 70 70 65 72 2c 20 6c 6f 77 65   (in upper, lowe
219ca 72 20 6f 72 20 6d 69 78 65 64 20 63 61 73 65 29  r or mixed case)
219cb 2e 20 54 68 69 73 20 70 6f 72 74 69 6f 6e 20 6f  . This portion o
219cc 66 20 74 68 65 20 6e 61 6d 65 73 70 61 63 65 0a  f the namespace.
219cd 2a 2a 20 69 73 20 72 65 73 65 72 76 65 64 20 66  ** is reserved f
219ce 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 2e  or internal use.
219cf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
219d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68  TE int sqlite3Ch
219d1 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61  eckObjectName(Pa
219d2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
219d3 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b  st char *zName){
219d4 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
219d5 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26  db->init.busy &&
219d6 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
219d7 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 26 26  =0 .          &&
219d8 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c   (pParse->db->fl
219d9 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57 72 69  ags & SQLITE_Wri
219da 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20  teSchema)==0.   
219db 20 20 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c         && 0==sql
219dc 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61  ite3StrNICmp(zNa
219dd 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37  me, "sqlite_", 7
219de 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
219df 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
219e0 20 22 6f 62 6a 65 63 74 20 6e 61 6d 65 20 72 65   "object name re
219e1 73 65 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72  served for inter
219e2 6e 61 6c 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e  nal use: %s", zN
219e3 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
219e4 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
219e5 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
219e6 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
219e7 42 65 67 69 6e 20 63 6f 6e 73 74 72 75 63 74 69  Begin constructi
219e8 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65 20 72  ng a new table r
219e9 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e  epresentation in
219ea 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69   memory.  This i
219eb 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f  s.** the first o
219ec 66 20 73 65 76 65 72 61 6c 20 61 63 74 69 6f 6e  f several action
219ed 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 67   routines that g
219ee 65 74 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73  et called in res
219ef 70 6f 6e 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52  ponse.** to a CR
219f0 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
219f1 6d 65 6e 74 2e 20 20 49 6e 20 70 61 72 74 69 63  ment.  In partic
219f2 75 6c 61 72 2c 20 74 68 69 73 20 72 6f 75 74 69  ular, this routi
219f3 6e 65 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20  ne is called.** 
219f4 61 66 74 65 72 20 73 65 65 69 6e 67 20 74 6f 6b  after seeing tok
219f5 65 6e 73 20 22 43 52 45 41 54 45 22 20 61 6e 64  ens "CREATE" and
219f6 20 22 54 41 42 4c 45 22 20 61 6e 64 20 74 68 65   "TABLE" and the
219f7 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65   table name. The
219f8 20 69 73 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20   isTemp.** flag 
219f9 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74  is true if the t
219fa 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73  able should be s
219fb 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 75 78  tored in the aux
219fc 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 0a  iliary database.
219fd 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20  ** file instead 
219fe 6f 66 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  of in the main d
219ff 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
21a00 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20  his is normally 
21a01 74 68 65 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e  the case.** when
21a02 20 74 68 65 20 22 54 45 4d 50 22 20 6f 72 20 22   the "TEMP" or "
21a03 54 45 4d 50 4f 52 41 52 59 22 20 6b 65 79 77 6f  TEMPORARY" keywo
21a04 72 64 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74  rd occurs in bet
21a05 77 65 65 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61  ween.** CREATE a
21a06 6e 64 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20  nd TABLE..**.** 
21a07 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65  The new table re
21a08 63 6f 72 64 20 69 73 20 69 6e 69 74 69 61 6c 69  cord is initiali
21a09 7a 65 64 20 61 6e 64 20 70 75 74 20 69 6e 20 70  zed and put in p
21a0a 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
21a0b 2e 0a 2a 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20  ..** As more of 
21a0c 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
21a0d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61   statement is pa
21a0e 72 73 65 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c  rsed, additional
21a0f 20 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69   action.** routi
21a10 6e 65 73 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  nes will be call
21a11 65 64 20 74 6f 20 61 64 64 20 6d 6f 72 65 20 69  ed to add more i
21a12 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68  nformation to th
21a13 69 73 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74  is record..** At
21a14 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
21a15 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
21a16 74 65 6d 65 6e 74 2c 20 74 68 65 20 73 71 6c 69  tement, the sqli
21a17 74 65 33 45 6e 64 54 61 62 6c 65 28 29 20 72 6f  te3EndTable() ro
21a18 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c  utine.** is call
21a19 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74  ed to complete t
21a1a 68 65 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  he construction 
21a1b 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
21a1c 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49   record..*/.SQLI
21a1d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
21a1e 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
21a1f 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
21a20 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20  se,   /* Parser 
21a21 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b  context */.  Tok
21a22 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a  en *pName1,   /*
21a23 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 74   First part of t
21a24 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
21a25 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a  able or view */.
21a26 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c    Token *pName2,
21a27 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72     /* Second par
21a28 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66  t of the name of
21a29 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69   the table or vi
21a2a 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65  ew */.  int isTe
21a2b 6d 70 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65  mp,      /* True
21a2c 20 69 66 20 74 68 69 73 20 69 73 20 61 20 54 45   if this is a TE
21a2d 4d 50 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  MP table */.  in
21a2e 74 20 69 73 56 69 65 77 2c 20 20 20 20 20 20 2f  t isView,      /
21a2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
21a30 73 20 61 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e  s a VIEW */.  in
21a31 74 20 69 73 56 69 72 74 75 61 6c 2c 20 20 20 2f  t isVirtual,   /
21a32 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
21a33 73 20 61 20 56 49 52 54 55 41 4c 20 74 61 62 6c  s a VIRTUAL tabl
21a34 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72  e */.  int noErr
21a35 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
21a36 74 68 69 6e 67 20 69 66 20 74 61 62 6c 65 20 61  thing if table a
21a37 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f  lready exists */
21a38 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  .){.  Table *pTa
21a39 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  ble;.  char *zNa
21a3a 6d 65 20 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e  me = 0; /* The n
21a3b 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74  ame of the new t
21a3c 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  able */.  sqlite
21a3d 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
21a3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  db;.  Vdbe *v;. 
21a3f 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
21a40 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75    /* Database nu
21a41 6d 62 65 72 20 74 6f 20 63 72 65 61 74 65 20 74  mber to create t
21a42 68 65 20 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20  he table in */. 
21a43 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20   Token *pName;  
21a44 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64    /* Unqualified
21a45 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
21a46 6c 65 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a  le to create */.
21a47 0a 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20  .  /* The table 
21a48 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 74 6f 20  or view name to 
21a49 63 72 65 61 74 65 20 69 73 20 70 61 73 73 65 64  create is passed
21a4a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
21a4b 20 76 69 61 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a   via tokens.  **
21a4c 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d   pName1 and pNam
21a4d 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65  e2. If the table
21a4e 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20   name was fully 
21a4f 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65  qualified, for e
21a50 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a  xample:.  **.  *
21a51 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78  * CREATE TABLE x
21a52 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 20 20  xx.yyy (...);.  
21a53 2a 2a 20 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e  ** .  ** Then pN
21a54 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22  ame1 is set to "
21a55 78 78 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20  xxx" and pName2 
21a56 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74  "yyy". On the ot
21a57 68 65 72 20 68 61 6e 64 20 69 66 0a 20 20 2a 2a  her hand if.  **
21a58 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
21a59 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61  is not fully qua
21a5a 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 20 20  lified, i.e.:.  
21a5b 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54  **.  ** CREATE T
21a5c 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 20  ABLE yyy(...);. 
21a5d 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e   **.  ** Then pN
21a5e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22  ame1 is set to "
21a5f 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20  yyy" and pName2 
21a60 69 73 20 22 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  is ""..  **.  **
21a61 20 54 68 65 20 63 61 6c 6c 20 62 65 6c 6f 77 20   The call below 
21a62 73 65 74 73 20 74 68 65 20 70 4e 61 6d 65 20 70  sets the pName p
21a63 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20  ointer to point 
21a64 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e  at the token (pN
21a65 61 6d 65 31 20 6f 72 0a 20 20 2a 2a 20 70 4e 61  ame1 or.  ** pNa
21a66 6d 65 32 29 20 74 68 61 74 20 73 74 6f 72 65 73  me2) that stores
21a67 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64   the unqualified
21a68 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65   table name. The
21a69 20 76 61 72 69 61 62 6c 65 20 69 44 62 20 69 73   variable iDb is
21a6a 0a 20 20 2a 2a 20 73 65 74 20 74 6f 20 74 68 65  .  ** set to the
21a6b 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   index of the da
21a6c 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20  tabase that the 
21a6d 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 69 73  table or view is
21a6e 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 72 65 61   to be.  ** crea
21a6f 74 65 64 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69  ted in..  */.  i
21a70 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50  Db = sqlite3TwoP
21a71 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  artName(pParse, 
21a72 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
21a73 26 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69  &pName);.  if( i
21a74 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  Db<0 ) return;. 
21a75 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
21a76 42 20 26 26 20 69 73 54 65 6d 70 20 26 26 20 69  B && isTemp && i
21a77 44 62 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Db>1 ){.    /* I
21a78 66 20 63 72 65 61 74 69 6e 67 20 61 20 74 65 6d  f creating a tem
21a79 70 20 74 61 62 6c 65 2c 20 74 68 65 20 6e 61 6d  p table, the nam
21a7a 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61  e may not be qua
21a7b 6c 69 66 69 65 64 20 2a 2f 0a 20 20 20 20 73 71  lified */.    sq
21a7c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
21a7d 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79  arse, "temporary
21a7e 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74   table name must
21a7f 20 62 65 20 75 6e 71 75 61 6c 69 66 69 65 64 22   be unqualified"
21a80 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
21a81 20 7d 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54   }.  if( !OMIT_T
21a82 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20  EMPDB && isTemp 
21a83 29 20 69 44 62 20 3d 20 31 3b 0a 0a 20 20 70 50  ) iDb = 1;..  pP
21a84 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
21a85 20 3d 20 2a 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61   = *pName;.  zNa
21a86 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  me = sqlite3Name
21a87 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e  FromToken(db, pN
21a88 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d  ame);.  if( zNam
21a89 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  e==0 ) return;. 
21a8a 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
21a8b 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65  sqlite3CheckObje
21a8c 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a  ctName(pParse, z
21a8d 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  Name) ){.    got
21a8e 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72  o begin_table_er
21a8f 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64  ror;.  }.  if( d
21a90 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29  b->init.iDb==1 )
21a91 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66   isTemp = 1;.#if
21a92 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
21a93 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
21a94 20 61 73 73 65 72 74 28 20 28 69 73 54 65 6d 70   assert( (isTemp
21a95 20 26 20 31 29 3d 3d 69 73 54 65 6d 70 20 29 3b   & 1)==isTemp );
21a96 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64  .  {.    int cod
21a97 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62  e;.    char *zDb
21a98 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
21a99 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73  zName;.    if( s
21a9a 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
21a9b 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49  pParse, SQLITE_I
21a9c 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41  NSERT, SCHEMA_TA
21a9d 42 4c 45 28 69 73 54 65 6d 70 29 2c 20 30 2c 20  BLE(isTemp), 0, 
21a9e 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  zDb) ){.      go
21a9f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65  to begin_table_e
21aa0 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rror;.    }.    
21aa1 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20  if( isView ){.  
21aa2 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45      if( !OMIT_TE
21aa3 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29  MPDB && isTemp )
21aa4 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
21aa5 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
21aa6 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20  EMP_VIEW;.      
21aa7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
21aa8 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45  ode = SQLITE_CRE
21aa9 41 54 45 5f 56 49 45 57 3b 0a 20 20 20 20 20 20  ATE_VIEW;.      
21aaa 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
21aab 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
21aac 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b  PDB && isTemp ){
21aad 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
21aae 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
21aaf 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20  MP_TABLE;.      
21ab0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
21ab1 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45  ode = SQLITE_CRE
21ab2 41 54 45 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20  ATE_TABLE;.     
21ab3 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
21ab4 20 21 69 73 56 69 72 74 75 61 6c 20 26 26 20 73   !isVirtual && s
21ab5 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
21ab6 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e  pParse, code, zN
21ab7 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  ame, 0, zDb) ){.
21ab8 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e        goto begin
21ab9 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20  _table_error;.  
21aba 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
21abb 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
21abc 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d  he new table nam
21abd 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69  e does not colli
21abe 64 65 20 77 69 74 68 20 61 6e 20 65 78 69 73 74  de with an exist
21abf 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f  ing.  ** index o
21ac0 72 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  r table name in 
21ac1 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
21ac2 65 2e 20 20 49 73 73 75 65 20 61 6e 20 65 72 72  e.  Issue an err
21ac3 6f 72 20 6d 65 73 73 61 67 65 20 69 66 0a 20 20  or message if.  
21ac4 2a 2a 20 69 74 20 64 6f 65 73 2e 20 54 68 65 20  ** it does. The 
21ac5 65 78 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20  exception is if 
21ac6 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65  the statement be
21ac7 69 6e 67 20 70 61 72 73 65 64 20 77 61 73 20 70  ing parsed was p
21ac8 61 73 73 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e  assed.  ** to an
21ac9 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
21aca 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e  _vtab() call. In
21acb 20 74 68 61 74 20 63 61 73 65 20 6f 6e 6c 79 20   that case only 
21acc 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
21acd 0a 20 20 2a 2a 20 61 6e 64 20 74 79 70 65 73 20  .  ** and types 
21ace 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 73 6f  will be used, so
21acf 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
21ad0 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6e 61  d to test for na
21ad1 6d 65 73 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c  mespace.  ** col
21ad2 6c 69 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  lisions..  */.  
21ad3 69 66 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f  if( !IN_DECLARE_
21ad4 56 54 41 42 20 29 7b 0a 20 20 20 20 69 66 28 20  VTAB ){.    if( 
21ad5 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
21ad6 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
21ad7 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  rse) ){.      go
21ad8 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65  to begin_table_e
21ad9 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rror;.    }.    
21ada 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  pTable = sqlite3
21adb 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e  FindTable(db, zN
21adc 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  ame, db->aDb[iDb
21add 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  ].zName);.    if
21ade 28 20 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  ( pTable ){.    
21adf 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a    if( !noErr ){.
21ae0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
21ae1 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
21ae2 22 74 61 62 6c 65 20 25 54 20 61 6c 72 65 61 64  "table %T alread
21ae3 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65  y exists", pName
21ae4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
21ae5 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
21ae6 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  e_error;.    }. 
21ae7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
21ae8 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d  ndIndex(db, zNam
21ae9 65 2c 20 30 29 21 3d 30 20 26 26 20 28 69 44 62  e, 0)!=0 && (iDb
21aea 3d 3d 30 20 7c 7c 20 21 64 62 2d 3e 69 6e 69 74  ==0 || !db->init
21aeb 2e 62 75 73 79 29 20 29 7b 0a 20 20 20 20 20 20  .busy) ){.      
21aec 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
21aed 70 50 61 72 73 65 2c 20 22 74 68 65 72 65 20 69  pParse, "there i
21aee 73 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64  s already an ind
21aef 65 78 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e  ex named %s", zN
21af0 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  ame);.      goto
21af1 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
21af2 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  or;.    }.  }.. 
21af3 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65   pTable = sqlite
21af4 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
21af5 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29  , sizeof(Table))
21af6 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d  ;.  if( pTable==
21af7 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  0 ){.    db->mal
21af8 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
21af9 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
21afa 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
21afb 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
21afc 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e  ;.    goto begin
21afd 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20  _table_error;.  
21afe 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d  }.  pTable->zNam
21aff 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61  e = zName;.  pTa
21b00 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b  ble->iPKey = -1;
21b01 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65  .  pTable->pSche
21b02 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  ma = db->aDb[iDb
21b03 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61  ].pSchema;.  pTa
21b04 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  ble->nRef = 1;. 
21b05 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 20 3d   pTable->dbMem =
21b06 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
21b07 45 6e 61 62 6c 65 64 20 3f 20 64 62 20 3a 20 30  Enabled ? db : 0
21b08 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
21b09 70 4e 65 77 54 61 62 6c 65 20 29 20 73 71 6c 69  pNewTable ) sqli
21b0a 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
21b0b 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
21b0c 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65  );.  pParse->pNe
21b0d 77 54 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b  wTable = pTable;
21b0e 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
21b0f 73 20 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69  s the magic sqli
21b10 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
21b11 65 20 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e  e used by autoin
21b12 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68  crement,.  ** th
21b13 65 6e 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e  en record a poin
21b14 74 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c  ter to this tabl
21b15 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  e in the main da
21b16 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
21b17 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e  .  ** so that IN
21b18 53 45 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68  SERT can find th
21b19 65 20 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a  e table easily..
21b1a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
21b1b 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
21b1c 52 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50  REMENT.  if( !pP
21b1d 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20  arse->nested && 
21b1e 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73  strcmp(zName, "s
21b1f 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29  qlite_sequence")
21b20 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 61 62 6c  ==0 ){.    pTabl
21b21 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71  e->pSchema->pSeq
21b22 54 61 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20  Tab = pTable;.  
21b23 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42  }.#endif..  /* B
21b24 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20  egin generating 
21b25 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  the code that wi
21b26 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 20 74 61  ll insert the ta
21b27 62 6c 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a  ble record into.
21b28 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f    ** the SQLITE_
21b29 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e  MASTER table.  N
21b2a 6f 74 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61  ote in particula
21b2b 72 20 74 68 61 74 20 77 65 20 6d 75 73 74 20 67  r that we must g
21b2c 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64  o ahead.  ** and
21b2d 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65   allocate the re
21b2e 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20  cord number for 
21b2f 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20  the table entry 
21b30 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79  now.  Before any
21b31 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45  .  ** PRIMARY KE
21b32 59 20 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77  Y or UNIQUE keyw
21b33 6f 72 64 73 20 61 72 65 20 70 61 72 73 65 64 2e  ords are parsed.
21b34 20 20 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73    Those keywords
21b35 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a   will cause.  **
21b36 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63   indices to be c
21b37 72 65 61 74 65 64 20 61 6e 64 20 74 68 65 20 74  reated and the t
21b38 61 62 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74  able record must
21b39 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65   come before the
21b3a 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20   .  ** indices. 
21b3b 20 48 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f   Hence, the reco
21b3c 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  rd number for th
21b3d 65 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20  e table must be 
21b3e 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e  allocated.  ** n
21b3f 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ow..  */.  if( !
21b40 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26  db->init.busy &&
21b41 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   (v = sqlite3Get
21b42 56 64 62 65 28 70 50 61 72 73 65 29 29 21 3d 30  Vdbe(pParse))!=0
21b43 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a   ){.    int j1;.
21b44 20 20 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d      int fileForm
21b45 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 31  at;.    int reg1
21b46 2c 20 72 65 67 32 2c 20 72 65 67 33 3b 0a 20 20  , reg2, reg3;.  
21b47 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
21b48 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
21b49 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23  rse, 0, iDb);..#
21b4a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21b4b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
21b4c 20 20 20 20 69 66 28 20 69 73 56 69 72 74 75 61      if( isVirtua
21b4d 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  l ){.      sqlit
21b4e 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
21b4f 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20  OP_VBegin);.    
21b50 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
21b51 20 49 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72   If the file for
21b52 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67  mat and encoding
21b53 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
21b54 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 73   have not been s
21b55 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20  et, .    ** set 
21b56 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f  them now..    */
21b57 0a 20 20 20 20 72 65 67 31 20 3d 20 70 50 61 72  .    reg1 = pPar
21b58 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b  se->regRowid = +
21b59 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
21b5a 20 20 20 72 65 67 32 20 3d 20 70 50 61 72 73 65     reg2 = pParse
21b5b 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70 50  ->regRoot = ++pP
21b5c 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
21b5d 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 73 65 2d  reg3 = ++pParse-
21b5e 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >nMem;.    sqlit
21b5f 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
21b60 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69  OP_ReadCookie, i
21b61 44 62 2c 20 72 65 67 33 2c 20 31 29 3b 20 20 20  Db, reg3, 1);   
21b62 2f 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 2a  /* file_format *
21b63 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
21b64 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
21b65 62 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c  b);.    j1 = sql
21b66 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
21b67 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b 0a  , OP_If, reg3);.
21b68 20 20 20 20 66 69 6c 65 46 6f 72 6d 61 74 20 3d      fileFormat =
21b69 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
21b6a 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46  LITE_LegacyFileF
21b6b 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20  mt)!=0 ?.       
21b6c 20 20 20 20 20 20 20 20 20 20 20 31 20 3a 20 53             1 : S
21b6d 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46  QLITE_MAX_FILE_F
21b6e 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71 6c 69 74  ORMAT;.    sqlit
21b6f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21b70 4f 50 5f 49 6e 74 65 67 65 72 2c 20 66 69 6c 65  OP_Integer, file
21b71 46 6f 72 6d 61 74 2c 20 72 65 67 33 29 3b 0a 20  Format, reg3);. 
21b72 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21b73 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f  dOp3(v, OP_SetCo
21b74 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c 20 72 65  okie, iDb, 1, re
21b75 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g3);.    sqlite3
21b76 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21b77 5f 49 6e 74 65 67 65 72 2c 20 45 4e 43 28 64 62  _Integer, ENC(db
21b78 29 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71  ), reg3);.    sq
21b79 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21b7a 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c  v, OP_SetCookie,
21b7b 20 69 44 62 2c 20 34 2c 20 72 65 67 33 29 3b 0a   iDb, 4, reg3);.
21b7c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
21b7d 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
21b7e 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 6a 75 73  .    /* This jus
21b7f 74 20 63 72 65 61 74 65 73 20 61 20 70 6c 61 63  t creates a plac
21b80 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f 72 64 20  e-holder record 
21b81 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  in the sqlite_ma
21b82 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 20 20  ster table..    
21b83 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 63 72  ** The record cr
21b84 65 61 74 65 64 20 64 6f 65 73 20 6e 6f 74 20 63  eated does not c
21b85 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69 6e 67 20  ontain anything 
21b86 79 65 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65  yet.  It will be
21b87 20 72 65 70 6c 61 63 65 64 0a 20 20 20 20 2a 2a   replaced.    **
21b88 20 62 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74   by the real ent
21b89 72 79 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72  ry in code gener
21b8a 61 74 65 64 20 61 74 20 73 71 6c 69 74 65 33 45  ated at sqlite3E
21b8b 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a  ndTable()..    *
21b8c 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77  *.    ** The row
21b8d 69 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65  id for the new e
21b8e 6e 74 72 79 20 69 73 20 6c 65 66 74 20 69 6e 20  ntry is left in 
21b8f 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d  register pParse-
21b90 3e 72 65 67 52 6f 77 69 64 2e 0a 20 20 20 20 2a  >regRowid..    *
21b91 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20  * The root page 
21b92 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
21b93 77 20 74 61 62 6c 65 20 69 73 20 6c 65 66 74 20  w table is left 
21b94 69 6e 20 72 65 67 20 70 50 61 72 73 65 2d 3e 72  in reg pParse->r
21b95 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a 2a 20 54  egRoot..    ** T
21b96 68 65 20 72 6f 77 69 64 20 61 6e 64 20 72 6f 6f  he rowid and roo
21b97 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 76 61  t page number va
21b98 6c 75 65 73 20 61 72 65 20 6e 65 65 64 65 64 20  lues are needed 
21b99 62 79 20 74 68 65 20 63 6f 64 65 20 74 68 61 74  by the code that
21b9a 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 45  .    ** sqlite3E
21b9b 6e 64 54 61 62 6c 65 20 77 69 6c 6c 20 67 65 6e  ndTable will gen
21b9c 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f 0a 23 69  erate..    */.#i
21b9d 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
21b9e 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
21b9f 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
21ba0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
21ba1 45 29 0a 20 20 20 20 69 66 28 20 69 73 56 69 65  E).    if( isVie
21ba2 77 20 7c 7c 20 69 73 56 69 72 74 75 61 6c 20 29  w || isVirtual )
21ba3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
21ba4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21ba5 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 32  Integer, 0, reg2
21ba6 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e  );.    }else.#en
21ba7 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  dif.    {.      
21ba8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21ba9 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 54 61  2(v, OP_CreateTa
21baa 62 6c 65 2c 20 69 44 62 2c 20 72 65 67 32 29 3b  ble, iDb, reg2);
21bab 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
21bac 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c  e3OpenMasterTabl
21bad 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
21bae 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21baf 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
21bb0 6f 77 69 64 2c 20 30 2c 20 72 65 67 31 29 3b 0a  owid, 0, reg1);.
21bb1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21bb2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
21bb3 2c 20 30 2c 20 72 65 67 33 29 3b 0a 20 20 20 20  , 0, reg3);.    
21bb4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21bb5 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
21bb6 30 2c 20 72 65 67 33 2c 20 72 65 67 31 29 3b 0a  0, reg3, reg1);.
21bb7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
21bb8 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
21bb9 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73  G_APPEND);.    s
21bba 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
21bbb 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20  (v, OP_Close);. 
21bbc 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20   }..  /* Normal 
21bbd 28 6e 6f 6e 2d 65 72 72 6f 72 29 20 72 65 74 75  (non-error) retu
21bbe 72 6e 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3b  rn. */.  return;
21bbf 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72  ..  /* If an err
21bc0 6f 72 20 6f 63 63 75 72 73 2c 20 77 65 20 6a 75  or occurs, we ju
21bc1 6d 70 20 68 65 72 65 20 2a 2f 0a 62 65 67 69 6e  mp here */.begin
21bc2 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 20  _table_error:.  
21bc3 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
21bc4 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75  , zName);.  retu
21bc5 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rn;.}../*.** Thi
21bc6 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20  s macro is used 
21bc7 74 6f 20 63 6f 6d 70 61 72 65 20 74 77 6f 20 73  to compare two s
21bc8 74 72 69 6e 67 73 20 69 6e 20 61 20 63 61 73 65  trings in a case
21bc9 2d 69 6e 73 65 6e 73 69 74 69 76 65 20 6d 61 6e  -insensitive man
21bca 6e 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 73 6c  ner..** It is sl
21bcb 69 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68  ightly faster th
21bcc 61 6e 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  an calling sqlit
21bcd 65 33 53 74 72 49 43 6d 70 28 29 20 64 69 72 65  e3StrICmp() dire
21bce 63 74 6c 79 2c 20 62 75 74 0a 2a 2a 20 70 72 6f  ctly, but.** pro
21bcf 64 75 63 65 73 20 6c 61 72 67 65 72 20 63 6f 64  duces larger cod
21bd0 65 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47  e..**.** WARNING
21bd1 3a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20  : This macro is 
21bd2 6e 6f 74 20 63 6f 6d 70 61 74 69 62 6c 65 20 77  not compatible w
21bd3 69 74 68 20 74 68 65 20 73 74 72 63 6d 70 28 29  ith the strcmp()
21bd4 20 66 61 6d 69 6c 79 2e 20 49 74 0a 2a 2a 20 72   family. It.** r
21bd5 65 74 75 72 6e 73 20 74 72 75 65 20 69 66 20 74  eturns true if t
21bd6 68 65 20 74 77 6f 20 73 74 72 69 6e 67 73 20 61  he two strings a
21bd7 72 65 20 65 71 75 61 6c 2c 20 6f 74 68 65 72 77  re equal, otherw
21bd8 69 73 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 64  ise false..*/.#d
21bd9 65 66 69 6e 65 20 53 54 52 49 43 4d 50 28 78 2c  efine STRICMP(x,
21bda 20 79 29 20 28 5c 0a 73 71 6c 69 74 65 33 55 70   y) (\.sqlite3Up
21bdb 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73  perToLower[*(uns
21bdc 69 67 6e 65 64 20 63 68 61 72 20 2a 29 28 78 29  igned char *)(x)
21bdd 5d 3d 3d 20 20 20 5c 0a 73 71 6c 69 74 65 33 55  ]==   \.sqlite3U
21bde 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e  pperToLower[*(un
21bdf 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 28 79  signed char *)(y
21be0 29 5d 20 20 20 20 20 5c 0a 26 26 20 73 71 6c 69  )]     \.&& sqli
21be1 74 65 33 53 74 72 49 43 6d 70 28 28 78 29 2b 31  te3StrICmp((x)+1
21be2 2c 28 79 29 2b 31 29 3d 3d 30 20 29 0a 0a 2f 2a  ,(y)+1)==0 )../*
21be3 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 63 6f  .** Add a new co
21be4 6c 75 6d 6e 20 74 6f 20 74 68 65 20 74 61 62 6c  lumn to the tabl
21be5 65 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e  e currently bein
21be6 67 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a  g constructed..*
21be7 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20  *.** The parser 
21be8 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
21be9 6e 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ne once for each
21bea 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74   column declarat
21beb 69 6f 6e 0a 2a 2a 20 69 6e 20 61 20 43 52 45 41  ion.** in a CREA
21bec 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
21bed 6e 74 2e 20 20 73 71 6c 69 74 65 33 53 74 61 72  nt.  sqlite3Star
21bee 74 54 61 62 6c 65 28 29 20 67 65 74 73 20 63 61  tTable() gets ca
21bef 6c 6c 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 6f  lled.** first to
21bf0 20 67 65 74 20 74 68 69 6e 67 73 20 67 6f 69 6e   get things goin
21bf1 67 2e 20 20 54 68 65 6e 20 74 68 69 73 20 72 6f  g.  Then this ro
21bf2 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
21bf3 66 6f 72 20 65 61 63 68 0a 2a 2a 20 63 6f 6c 75  for each.** colu
21bf4 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  mn..*/.SQLITE_PR
21bf5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21bf6 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73  e3AddColumn(Pars
21bf7 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
21bf8 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c   *pName){.  Tabl
21bf9 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  e *p;.  int i;. 
21bfa 20 63 68 61 72 20 2a 7a 3b 0a 20 20 43 6f 6c 75   char *z;.  Colu
21bfb 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69  mn *pCol;.  sqli
21bfc 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
21bfd 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 70 20 3d  ->db;.  if( (p =
21bfe 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
21bff 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  le)==0 ) return;
21c00 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
21c01 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e  COLUMN.  if( p->
21c02 6e 43 6f 6c 2b 31 3e 64 62 2d 3e 61 4c 69 6d 69  nCol+1>db->aLimi
21c03 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  t[SQLITE_LIMIT_C
21c04 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71  OLUMN] ){.    sq
21c05 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
21c06 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
21c07 63 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73 22 2c 20  columns on %s", 
21c08 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72  p->zName);.    r
21c09 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69  eturn;.  }.#endi
21c0a 66 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e  f.  z = sqlite3N
21c0b 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
21c0c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a   pName);.  if( z
21c0d 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
21c0e 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43  for(i=0; i<p->nC
21c0f 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  ol; i++){.    if
21c10 28 20 53 54 52 49 43 4d 50 28 7a 2c 20 70 2d 3e  ( STRICMP(z, p->
21c11 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29  aCol[i].zName) )
21c12 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
21c13 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
21c14 22 64 75 70 6c 69 63 61 74 65 20 63 6f 6c 75 6d  "duplicate colum
21c15 6e 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 29 3b  n name: %s", z);
21c16 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
21c17 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20  Free(db, z);.   
21c18 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
21c19 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e  .  }.  if( (p->n
21c1a 43 6f 6c 20 26 20 30 78 37 29 3d 3d 30 20 29 7b  Col & 0x7)==0 ){
21c1b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65  .    Column *aNe
21c1c 77 3b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71  w;.    aNew = sq
21c1d 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64  lite3DbRealloc(d
21c1e 62 2c 70 2d 3e 61 43 6f 6c 2c 28 70 2d 3e 6e 43  b,p->aCol,(p->nC
21c1f 6f 6c 2b 38 29 2a 73 69 7a 65 6f 66 28 70 2d 3e  ol+8)*sizeof(p->
21c20 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 69  aCol[0]));.    i
21c21 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( aNew==0 ){.  
21c22 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
21c23 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20  e(db, z);.      
21c24 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
21c25 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65 77    p->aCol = aNew
21c26 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 20 3d 20 26  ;.  }.  pCol = &
21c27 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d  p->aCol[p->nCol]
21c28 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c  ;.  memset(pCol,
21c29 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43   0, sizeof(p->aC
21c2a 6f 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d  ol[0]));.  pCol-
21c2b 3e 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20  >zName = z;. .  
21c2c 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
21c2d 6f 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64  o type specified
21c2e 2c 20 63 6f 6c 75 6d 6e 73 20 68 61 76 65 20 74  , columns have t
21c2f 68 65 20 64 65 66 61 75 6c 74 20 61 66 66 69 6e  he default affin
21c30 69 74 79 0a 20 20 2a 2a 20 27 4e 4f 4e 45 27 2e  ity.  ** 'NONE'.
21c31 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74   If there is a t
21c32 79 70 65 20 73 70 65 63 69 66 69 65 64 2c 20 74  ype specified, t
21c33 68 65 6e 20 73 71 6c 69 74 65 33 41 64 64 43 6f  hen sqlite3AddCo
21c34 6c 75 6d 6e 54 79 70 65 28 29 20 77 69 6c 6c 0a  lumnType() will.
21c35 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6e    ** be called n
21c36 65 78 74 20 74 6f 20 73 65 74 20 70 43 6f 6c 2d  ext to set pCol-
21c37 3e 61 66 66 69 6e 69 74 79 20 63 6f 72 72 65 63  >affinity correc
21c38 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c  tly..  */.  pCol
21c39 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c  ->affinity = SQL
21c3a 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
21c3b 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a  p->nCol++;.}../*
21c3c 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
21c3d 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
21c3e 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69  e parser while i
21c3f 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a  n the middle of.
21c40 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45  ** parsing a CRE
21c41 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
21c42 65 6e 74 2e 20 20 41 20 22 4e 4f 54 20 4e 55 4c  ent.  A "NOT NUL
21c43 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61  L" constraint ha
21c44 73 0a 2a 2a 20 62 65 65 6e 20 73 65 65 6e 20 6f  s.** been seen o
21c45 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 69  n a column.  Thi
21c46 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
21c47 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c 61 67 20  he notNull flag 
21c48 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  on.** the column
21c49 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
21c4a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a   construction..*
21c4b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21c4c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
21c4d 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70  NotNull(Parse *p
21c4e 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72  Parse, int onErr
21c4f 6f 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  or){.  Table *p;
21c50 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
21c51 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  (p = pParse->pNe
21c52 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74  wTable)==0 ) ret
21c53 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43  urn;.  i = p->nC
21c54 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69 3e 3d 30  ol-1;.  if( i>=0
21c55 20 29 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f   ) p->aCol[i].no
21c56 74 4e 75 6c 6c 20 3d 20 28 75 38 29 6f 6e 45 72  tNull = (u8)onEr
21c57 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63  ror;.}../*.** Sc
21c58 61 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79  an the column ty
21c59 70 65 20 6e 61 6d 65 20 7a 54 79 70 65 20 28 6c  pe name zType (l
21c5a 65 6e 67 74 68 20 6e 54 79 70 65 29 20 61 6e 64  ength nType) and
21c5b 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61   return the.** a
21c5c 73 73 6f 63 69 61 74 65 64 20 61 66 66 69 6e 69  ssociated affini
21c5d 74 79 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 54  ty type..**.** T
21c5e 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  his routine does
21c5f 20 61 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64   a case-independ
21c60 65 6e 74 20 73 65 61 72 63 68 20 6f 66 20 7a 54  ent search of zT
21c61 79 70 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20  ype for the .** 
21c62 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74 68  substrings in th
21c63 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
21c64 65 2e 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65  e. If one of the
21c65 20 73 75 62 73 74 72 69 6e 67 73 20 69 73 0a 2a   substrings is.*
21c66 2a 20 66 6f 75 6e 64 2c 20 74 68 65 20 63 6f 72  * found, the cor
21c67 72 65 73 70 6f 6e 64 69 6e 67 20 61 66 66 69 6e  responding affin
21c68 69 74 79 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ity is returned.
21c69 20 49 66 20 7a 54 79 70 65 20 63 6f 6e 74 61 69   If zType contai
21c6a 6e 73 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20  ns.** more than 
21c6b 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74  one of the subst
21c6c 72 69 6e 67 73 2c 20 65 6e 74 72 69 65 73 20 74  rings, entries t
21c6d 6f 77 61 72 64 20 74 68 65 20 74 6f 70 20 6f 66  oward the top of
21c6e 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 74   .** the table t
21c6f 61 6b 65 20 70 72 69 6f 72 69 74 79 2e 20 46 6f  ake priority. Fo
21c70 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 7a 54  r example, if zT
21c71 79 70 65 20 69 73 20 27 42 4c 4f 42 49 4e 54 27  ype is 'BLOBINT'
21c72 2c 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46  , .** SQLITE_AFF
21c73 5f 49 4e 54 45 47 45 52 20 69 73 20 72 65 74 75  _INTEGER is retu
21c74 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 75 62 73  rned..**.** Subs
21c75 74 72 69 6e 67 20 20 20 20 20 7c 20 41 66 66 69  tring     | Affi
21c76 6e 69 74 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d  nity.** --------
21c77 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
21c78 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e 54  --------.** 'INT
21c79 27 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49  '         | SQLI
21c7a 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 0a 2a  TE_AFF_INTEGER.*
21c7b 2a 20 27 43 48 41 52 27 20 20 20 20 20 20 20 20  * 'CHAR'        
21c7c 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  | SQLITE_AFF_TEX
21c7d 54 0a 2a 2a 20 27 43 4c 4f 42 27 20 20 20 20 20  T.** 'CLOB'     
21c7e 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
21c7f 54 45 58 54 0a 2a 2a 20 27 54 45 58 54 27 20 20  TEXT.** 'TEXT'  
21c80 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
21c81 46 46 5f 54 45 58 54 0a 2a 2a 20 27 42 4c 4f 42  FF_TEXT.** 'BLOB
21c82 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54  '        | SQLIT
21c83 45 5f 41 46 46 5f 4e 4f 4e 45 0a 2a 2a 20 27 52  E_AFF_NONE.** 'R
21c84 45 41 4c 27 20 20 20 20 20 20 20 20 7c 20 53 51  EAL'        | SQ
21c85 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a  LITE_AFF_REAL.**
21c86 20 27 46 4c 4f 41 27 20 20 20 20 20 20 20 20 7c   'FLOA'        |
21c87 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
21c88 0a 2a 2a 20 27 44 4f 55 42 27 20 20 20 20 20 20  .** 'DOUB'      
21c89 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52    | SQLITE_AFF_R
21c8a 45 41 4c 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e  EAL.**.** If non
21c8b 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69  e of the substri
21c8c 6e 67 73 20 69 6e 20 74 68 65 20 61 62 6f 76 65  ngs in the above
21c8d 20 74 61 62 6c 65 20 61 72 65 20 66 6f 75 6e 64   table are found
21c8e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  ,.** SQLITE_AFF_
21c8f 4e 55 4d 45 52 49 43 20 69 73 20 72 65 74 75 72  NUMERIC is retur
21c90 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
21c91 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69  RIVATE char sqli
21c92 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28  te3AffinityType(
21c93 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 79  const Token *pTy
21c94 70 65 29 7b 0a 20 20 75 33 32 20 68 20 3d 20 30  pe){.  u32 h = 0
21c95 3b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 53  ;.  char aff = S
21c96 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
21c97 43 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  C;.  const unsig
21c98 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 20 3d 20  ned char *zIn = 
21c99 70 54 79 70 65 2d 3e 7a 3b 0a 20 20 63 6f 6e 73  pType->z;.  cons
21c9a 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
21c9b 2a 7a 45 6e 64 20 3d 20 26 70 54 79 70 65 2d 3e  *zEnd = &pType->
21c9c 7a 5b 70 54 79 70 65 2d 3e 6e 5d 3b 0a 0a 20 20  z[pType->n];..  
21c9d 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 45 6e 64  while( zIn!=zEnd
21c9e 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c   ){.    h = (h<<
21c9f 38 29 20 2b 20 73 71 6c 69 74 65 33 55 70 70 65  8) + sqlite3Uppe
21ca0 72 54 6f 4c 6f 77 65 72 5b 2a 7a 49 6e 5d 3b 0a  rToLower[*zIn];.
21ca1 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 69      zIn++;.    i
21ca2 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29  f( h==(('c'<<24)
21ca3 2b 28 27 68 27 3c 3c 31 36 29 2b 28 27 61 27 3c  +('h'<<16)+('a'<
21ca4 3c 38 29 2b 27 72 27 29 20 29 7b 20 20 20 20 20  <8)+'r') ){     
21ca5 20 20 20 20 20 20 20 20 2f 2a 20 43 48 41 52 20          /* CHAR 
21ca6 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  */.      aff = S
21ca7 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 20  QLITE_AFF_TEXT; 
21ca8 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68  .    }else if( h
21ca9 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 6c  ==(('c'<<24)+('l
21caa 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b  '<<16)+('o'<<8)+
21cab 27 62 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a  'b') ){       /*
21cac 20 43 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 61   CLOB */.      a
21cad 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
21cae 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20  TEXT;.    }else 
21caf 69 66 28 20 68 3d 3d 28 28 27 74 27 3c 3c 32 34  if( h==(('t'<<24
21cb0 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 78 27  )+('e'<<16)+('x'
21cb1 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20  <<8)+'t') ){    
21cb2 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f 0a 20 20     /* TEXT */.  
21cb3 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
21cb4 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d  _AFF_TEXT;.    }
21cb5 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 62  else if( h==(('b
21cb6 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29  '<<24)+('l'<<16)
21cb7 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 20  +('o'<<8)+'b')  
21cb8 20 20 20 20 20 20 20 20 2f 2a 20 42 4c 4f 42 20          /* BLOB 
21cb9 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 28 61  */.        && (a
21cba 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
21cbb 55 4d 45 52 49 43 20 7c 7c 20 61 66 66 3d 3d 53  UMERIC || aff==S
21cbc 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 29 20  QLITE_AFF_REAL) 
21cbd 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  ){.      aff = S
21cbe 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
21cbf 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
21cc0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
21cc1 4e 54 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  NT.    }else if(
21cc2 20 68 3d 3d 28 28 27 72 27 3c 3c 32 34 29 2b 28   h==(('r'<<24)+(
21cc3 27 65 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38  'e'<<16)+('a'<<8
21cc4 29 2b 27 6c 27 29 20 20 20 20 20 20 20 20 20 20  )+'l')          
21cc5 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20 20 20 20 20  /* REAL */.     
21cc6 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54     && aff==SQLIT
21cc7 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b  E_AFF_NUMERIC ){
21cc8 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c  .      aff = SQL
21cc9 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20  ITE_AFF_REAL;.  
21cca 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28    }else if( h==(
21ccb 28 27 66 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c  ('f'<<24)+('l'<<
21ccc 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 61 27  16)+('o'<<8)+'a'
21ccd 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 4c  )          /* FL
21cce 4f 41 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26  OA */.        &&
21ccf 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46   aff==SQLITE_AFF
21cd0 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20  _NUMERIC ){.    
21cd1 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
21cd2 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c  FF_REAL;.    }el
21cd3 73 65 20 69 66 28 20 68 3d 3d 28 28 27 64 27 3c  se if( h==(('d'<
21cd4 3c 32 34 29 2b 28 27 6f 27 3c 3c 31 36 29 2b 28  <24)+('o'<<16)+(
21cd5 27 75 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 20  'u'<<8)+'b')    
21cd6 20 20 20 20 20 20 2f 2a 20 44 4f 55 42 20 2a 2f        /* DOUB */
21cd7 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d  .        && aff=
21cd8 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
21cd9 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66  RIC ){.      aff
21cda 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45   = SQLITE_AFF_RE
21cdb 41 4c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  AL;.#endif.    }
21cdc 65 6c 73 65 20 69 66 28 20 28 68 26 30 78 30 30  else if( (h&0x00
21cdd 46 46 46 46 46 46 29 3d 3d 28 28 27 69 27 3c 3c  FFFFFF)==(('i'<<
21cde 31 36 29 2b 28 27 6e 27 3c 3c 38 29 2b 27 74 27  16)+('n'<<8)+'t'
21cdf 29 20 29 7b 20 20 20 20 2f 2a 20 49 4e 54 20 2a  ) ){    /* INT *
21ce0 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  /.      aff = SQ
21ce1 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
21ce2 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
21ce3 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
21ce4 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn aff;.}../*.**
21ce5 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
21ce6 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
21ce7 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74  arser while in t
21ce8 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20  he middle of.** 
21ce9 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45  parsing a CREATE
21cea 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
21ceb 2e 20 20 54 68 65 20 70 46 69 72 73 74 20 74 6f  .  The pFirst to
21cec 6b 65 6e 20 69 73 20 74 68 65 20 66 69 72 73 74  ken is the first
21ced 0a 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  .** token in the
21cee 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 6f 6b   sequence of tok
21cef 65 6e 73 20 74 68 61 74 20 64 65 73 63 72 69 62  ens that describ
21cf0 65 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68  e the type of th
21cf1 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63 75 72 72  e.** column curr
21cf2 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
21cf3 74 72 75 63 74 69 6f 6e 2e 20 20 20 70 4c 61 73  truction.   pLas
21cf4 74 20 69 73 20 74 68 65 20 6c 61 73 74 20 74 6f  t is the last to
21cf5 6b 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65  ken.** in the se
21cf6 71 75 65 6e 63 65 2e 20 20 55 73 65 20 74 68 69  quence.  Use thi
21cf7 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  s information to
21cf8 20 63 6f 6e 73 74 72 75 63 74 20 61 20 73 74 72   construct a str
21cf9 69 6e 67 0a 2a 2a 20 74 68 61 74 20 63 6f 6e 74  ing.** that cont
21cfa 61 69 6e 73 20 74 68 65 20 74 79 70 65 6e 61 6d  ains the typenam
21cfb 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  e of the column 
21cfc 61 6e 64 20 73 74 6f 72 65 20 74 68 61 74 20 73  and store that s
21cfd 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 7a 54 79 70  tring.** in zTyp
21cfe 65 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  e..*/ .SQLITE_PR
21cff 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21d00 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28  e3AddColumnType(
21d01 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
21d02 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 20  oken *pType){.  
21d03 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20  Table *p;.  int 
21d04 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  i;.  Column *pCo
21d05 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  l;.  sqlite3 *db
21d06 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50  ;..  if( (p = pP
21d07 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29  arse->pNewTable)
21d08 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
21d09 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20  i = p->nCol-1;. 
21d0a 20 69 66 28 20 69 3c 30 20 29 20 72 65 74 75 72   if( i<0 ) retur
21d0b 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e  n;.  pCol = &p->
21d0c 61 43 6f 6c 5b 69 5d 3b 0a 20 20 64 62 20 3d 20  aCol[i];.  db = 
21d0d 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 73 71  pParse->db;.  sq
21d0e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
21d0f 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20 20  pCol->zType);.  
21d10 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 71  pCol->zType = sq
21d11 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
21d12 65 6e 28 64 62 2c 20 70 54 79 70 65 29 3b 0a 20  en(db, pType);. 
21d13 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20   pCol->affinity 
21d14 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74  = sqlite3Affinit
21d15 79 54 79 70 65 28 70 54 79 70 65 29 3b 0a 7d 0a  yType(pType);.}.
21d16 0a 2f 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 65  ./*.** The expre
21d17 73 73 69 6f 6e 20 69 73 20 74 68 65 20 64 65 66  ssion is the def
21d18 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
21d19 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
21d1a 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a   added column.**
21d1b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 75   of the table cu
21d1c 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
21d1d 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
21d1e 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20  * Default value 
21d1f 65 78 70 72 65 73 73 69 6f 6e 73 20 6d 75 73 74  expressions must
21d20 20 62 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20 52   be constant.  R
21d21 61 69 73 65 20 61 6e 20 65 78 63 65 70 74 69 6f  aise an exceptio
21d22 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 20  n if this.** is 
21d23 6e 6f 74 20 74 68 65 20 63 61 73 65 2e 0a 2a 2a  not the case..**
21d24 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
21d25 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
21d26 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69  e parser while i
21d27 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a  n the middle of.
21d28 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45  ** parsing a CRE
21d29 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
21d2a 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
21d2b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21d2c 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c  te3AddDefaultVal
21d2d 75 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ue(Parse *pParse
21d2e 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  , Expr *pExpr){.
21d2f 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43 6f    Table *p;.  Co
21d30 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71  lumn *pCol;.  sq
21d31 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
21d32 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 70  se->db;.  if( (p
21d33 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
21d34 61 62 6c 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  able)!=0 ){.    
21d35 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 43 6f 6c  pCol = &(p->aCol
21d36 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0a 20 20  [p->nCol-1]);.  
21d37 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78    if( !sqlite3Ex
21d38 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75  prIsConstantOrFu
21d39 6e 63 74 69 6f 6e 28 70 45 78 70 72 29 20 29 7b  nction(pExpr) ){
21d3a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
21d3b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
21d3c 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66  default value of
21d3d 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20 69 73 20   column [%s] is 
21d3e 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22 2c 0a 20  not constant",. 
21d3f 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a           pCol->z
21d40 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Name);.    }else
21d41 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 70  {.      /* A cop
21d42 79 20 6f 66 20 70 45 78 70 72 20 69 73 20 75 73  y of pExpr is us
21d43 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  ed instead of th
21d44 65 20 6f 72 69 67 69 6e 61 6c 2c 20 61 73 20 70  e original, as p
21d45 45 78 70 72 20 63 6f 6e 74 61 69 6e 73 0a 20 20  Expr contains.  
21d46 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 73 20 74 68      ** tokens th
21d47 61 74 20 70 6f 69 6e 74 20 74 6f 20 76 6f 6c 61  at point to vola
21d48 74 69 6c 65 20 6d 65 6d 6f 72 79 2e 20 54 68 65  tile memory. The
21d49 20 27 73 70 61 6e 27 20 6f 66 20 74 68 65 20 65   'span' of the e
21d4a 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20  xpression.      
21d4b 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 62  ** is required b
21d4c 79 20 70 72 61 67 6d 61 20 74 61 62 6c 65 5f 69  y pragma table_i
21d4d 6e 66 6f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  nfo..      */.  
21d4e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
21d4f 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e  elete(db, pCol->
21d50 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43  pDflt);.      pC
21d51 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 73 71 6c 69  ol->pDflt = sqli
21d52 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
21d53 45 78 70 72 2c 20 45 58 50 52 44 55 50 5f 52 45  Expr, EXPRDUP_RE
21d54 44 55 43 45 7c 45 58 50 52 44 55 50 5f 53 50 41  DUCE|EXPRDUP_SPA
21d55 4e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  N);.    }.  }.  
21d56 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
21d57 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 7d 0a  e(db, pExpr);.}.
21d58 0a 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65  ./*.** Designate
21d59 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
21d5a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20   for the table. 
21d5b 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74   pList is a list
21d5c 20 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66   of names .** of
21d5d 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f   columns that fo
21d5e 72 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  rm the primary k
21d5f 65 79 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73  ey.  If pList is
21d60 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a   NULL, then the.
21d61 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  ** most recently
21d62 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66   added column of
21d63 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68   the table is th
21d64 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a  e primary key..*
21d65 2a 0a 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e  *.** A table can
21d66 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e   have at most on
21d67 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20  e primary key.  
21d68 49 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72  If the table alr
21d69 65 61 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72  eady has.** a pr
21d6a 69 6d 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74  imary key (and t
21d6b 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e  his is the secon
21d6c 64 20 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74  d primary key) t
21d6d 68 65 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a  hen create an.**
21d6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66   error..**.** If
21d6f 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
21d70 20 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20   is on a single 
21d71 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74  column whose dat
21d72 61 74 79 70 65 20 69 73 20 49 4e 54 45 47 45 52  atype is INTEGER
21d73 2c 0a 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c  ,.** then we wil
21d74 6c 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 61  l try to use tha
21d75 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20  t column as the 
21d76 72 6f 77 69 64 2e 20 20 53 65 74 20 74 68 65 20  rowid.  Set the 
21d77 54 61 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66  Table.iPKey.** f
21d78 69 65 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c  ield of the tabl
21d79 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
21d7a 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69  tion to be the i
21d7b 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49  ndex of the.** I
21d7c 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
21d7d 45 59 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c  EY column.  Tabl
21d7e 65 2e 69 50 4b 65 79 20 69 73 20 73 65 74 20 74  e.iPKey is set t
21d7f 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73  o -1 if there is
21d80 0a 2a 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50  .** no INTEGER P
21d81 52 49 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a  RIMARY KEY..**.*
21d82 2a 20 49 66 20 74 68 65 20 6b 65 79 20 69 73 20  * If the key is 
21d83 6e 6f 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50  not an INTEGER P
21d84 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e  RIMARY KEY, then
21d85 20 63 72 65 61 74 65 20 61 20 75 6e 69 71 75 65   create a unique
21d86 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68  .** index for th
21d87 65 20 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78  e key.  No index
21d88 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20   is created for 
21d89 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
21d8a 4b 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  KEYs..*/.SQLITE_
21d8b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21d8c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
21d8d 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  y(.  Parse *pPar
21d8e 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  se,    /* Parsin
21d8f 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
21d90 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
21d91 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c   /* List of fiel
21d92 64 20 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e  d names to be in
21d93 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  dexed */.  int o
21d94 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20  nError,      /* 
21d95 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20  What to do with 
21d96 61 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e  a uniqueness con
21d97 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61  flict */.  int a
21d98 75 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20  utoInc,      /* 
21d99 54 72 75 65 20 69 66 20 74 68 65 20 41 55 54 4f  True if the AUTO
21d9a 49 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72  INCREMENT keywor
21d9b 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  d is present */.
21d9c 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20    int sortOrder 
21d9d 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f      /* SQLITE_SO
21d9e 5f 41 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53  _ASC or SQLITE_S
21d9f 4f 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54  O_DESC */.){.  T
21da0 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61  able *pTab = pPa
21da1 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
21da2 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20    char *zType = 
21da3 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20  0;.  int iCol = 
21da4 2d 31 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61  -1, i;.  if( pTa
21da5 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41  b==0 || IN_DECLA
21da6 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70  RE_VTAB ) goto p
21da7 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b  rimary_key_exit;
21da8 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62  .  if( pTab->tab
21da9 46 6c 61 67 73 20 26 20 54 46 5f 48 61 73 50 72  Flags & TF_HasPr
21daa 69 6d 61 72 79 4b 65 79 20 29 7b 0a 20 20 20 20  imaryKey ){.    
21dab 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
21dac 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22  pParse, .      "
21dad 74 61 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73  table \"%s\" has
21dae 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70   more than one p
21daf 72 69 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61  rimary key", pTa
21db0 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  b->zName);.    g
21db1 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f  oto primary_key_
21db2 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62  exit;.  }.  pTab
21db3 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46  ->tabFlags |= TF
21db4 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a  _HasPrimaryKey;.
21db5 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
21db6 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61  {.    iCol = pTa
21db7 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20  b->nCol - 1;.   
21db8 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
21db9 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b  ].isPrimKey = 1;
21dba 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f  .  }else{.    fo
21dbb 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  r(i=0; i<pList->
21dbc 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
21dbd 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
21dbe 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  Col<pTab->nCol; 
21dbf 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20  iCol++){.       
21dc0 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
21dc1 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  Cmp(pList->a[i].
21dc2 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f  zName, pTab->aCo
21dc3 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d  l[iCol].zName)==
21dc4 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  0 ){.          b
21dc5 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
21dc6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
21dc7 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  ( iCol<pTab->nCo
21dc8 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61  l ){.        pTa
21dc9 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73  b->aCol[iCol].is
21dca 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20  PrimKey = 1;.   
21dcb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
21dcc 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e  f( pList->nExpr>
21dcd 31 20 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20  1 ) iCol = -1;. 
21dce 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30   }.  if( iCol>=0
21dcf 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e   && iCol<pTab->n
21dd0 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70 65  Col ){.    zType
21dd1 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
21dd2 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20  ol].zType;.  }. 
21dd3 20 69 66 28 20 7a 54 79 70 65 20 26 26 20 73 71   if( zType && sq
21dd4 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79  lite3StrICmp(zTy
21dd5 70 65 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d  pe, "INTEGER")==
21dd6 30 0a 20 20 20 20 20 20 20 20 26 26 20 73 6f 72  0.        && sor
21dd7 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53  tOrder==SQLITE_S
21dd8 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20 70 54 61  O_ASC ){.    pTa
21dd9 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b  b->iPKey = iCol;
21dda 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f  .    pTab->keyCo
21ddb 6e 66 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72  nf = (u8)onError
21ddc 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 75  ;.    assert( au
21ddd 74 6f 49 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f  toInc==0 || auto
21dde 49 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54  Inc==1 );.    pT
21ddf 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20  ab->tabFlags |= 
21de0 61 75 74 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69  autoInc*TF_Autoi
21de1 6e 63 72 65 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73  ncrement;.  }els
21de2 65 20 69 66 28 20 61 75 74 6f 49 6e 63 20 29 7b  e if( autoInc ){
21de3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
21de4 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45  OMIT_AUTOINCREME
21de5 4e 54 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  NT.    sqlite3Er
21de6 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
21de7 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73  AUTOINCREMENT is
21de8 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 6f 6e   only allowed on
21de9 20 61 6e 20 22 0a 20 20 20 20 20 20 20 22 49 4e   an ".       "IN
21dea 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
21deb 59 22 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65  Y");.#endif.  }e
21dec 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
21ded 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72  CreateIndex(pPar
21dee 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69  se, 0, 0, 0, pLi
21def 73 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20  st, onError, 0, 
21df0 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c 20 30 29  0, sortOrder, 0)
21df1 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 30 3b  ;.    pList = 0;
21df2 0a 20 20 7d 0a 0a 70 72 69 6d 61 72 79 5f 6b 65  .  }..primary_ke
21df3 79 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65  y_exit:.  sqlite
21df4 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
21df5 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73  pParse->db, pLis
21df6 74 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  t);.  return;.}.
21df7 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
21df8 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
21df9 74 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63  t to the table c
21dfa 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63  urrently under c
21dfb 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  onstruction..*/.
21dfc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
21dfd 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 68  oid sqlite3AddCh
21dfe 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20  eckConstraint(. 
21dff 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
21e00 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
21e01 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20  ntext */.  Expr 
21e02 2a 70 43 68 65 63 6b 45 78 70 72 20 20 2f 2a 20  *pCheckExpr  /* 
21e03 54 68 65 20 63 68 65 63 6b 20 65 78 70 72 65 73  The check expres
21e04 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  sion */.){.  sql
21e05 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
21e06 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53  e->db;.#ifndef S
21e07 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
21e08 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
21e09 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
21e0a 6c 65 3b 0a 20 20 69 66 28 20 70 54 61 62 20 26  le;.  if( pTab &
21e0b 26 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  & !IN_DECLARE_VT
21e0c 41 42 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  AB ){.    /* The
21e0d 20 43 48 45 43 4b 20 65 78 70 72 65 73 73 69 6f   CHECK expressio
21e0e 6e 20 6d 75 73 74 20 62 65 20 64 75 70 6c 69 63  n must be duplic
21e0f 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 6f 6b  ated so that tok
21e10 65 6e 73 20 72 65 66 65 72 0a 20 20 20 20 2a 2a  ens refer.    **
21e11 20 74 6f 20 6d 61 6c 6c 6f 63 65 64 20 73 70 61   to malloced spa
21e12 63 65 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 28  ce and not the (
21e13 65 70 68 65 6d 65 72 61 6c 29 20 74 65 78 74 20  ephemeral) text 
21e14 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41  of the CREATE TA
21e15 42 4c 45 0a 20 20 20 20 2a 2a 20 73 74 61 74 65  BLE.    ** state
21e16 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 70 54 61 62  ment */.    pTab
21e17 2d 3e 70 43 68 65 63 6b 20 3d 20 73 71 6c 69 74  ->pCheck = sqlit
21e18 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 54  e3ExprAnd(db, pT
21e19 61 62 2d 3e 70 43 68 65 63 6b 2c 20 0a 20 20 20  ab->pCheck, .   
21e1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
21e1c 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
21e1d 2c 20 70 43 68 65 63 6b 45 78 70 72 2c 20 30 29  , pCheckExpr, 0)
21e1e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
21e1f 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
21e20 65 28 64 62 2c 20 70 43 68 65 63 6b 45 78 70 72  e(db, pCheckExpr
21e21 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  );.}../*.** Set 
21e22 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75  the collation fu
21e23 6e 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f  nction of the mo
21e24 73 74 20 72 65 63 65 6e 74 6c 79 20 70 61 72 73  st recently pars
21e25 65 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a  ed table column.
21e26 2a 2a 20 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65  ** to the CollSe
21e27 71 20 67 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49  q given..*/.SQLI
21e28 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
21e29 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74  sqlite3AddCollat
21e2a 65 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 61  eType(Parse *pPa
21e2b 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  rse, Token *pTok
21e2c 65 6e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  en){.  Table *p;
21e2d 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72  .  int i;.  char
21e2e 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20   *zColl;        
21e2f 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f 74 65        /* Dequote
21e30 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74  d name of collat
21e31 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ion sequence */.
21e32 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a    sqlite3 *db;..
21e33 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73    if( (p = pPars
21e34 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30  e->pNewTable)==0
21e35 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d   ) return;.  i =
21e36 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64 62   p->nCol-1;.  db
21e37 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
21e38 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   zColl = sqlite3
21e39 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
21e3a 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28  , pToken);.  if(
21e3b 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e   !zColl ) return
21e3c 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ;..  if( sqlite3
21e3d 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50  LocateCollSeq(pP
21e3e 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29  arse, zColl, -1)
21e3f 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   ){.    Index *p
21e40 49 64 78 3b 0a 20 20 20 20 70 2d 3e 61 43 6f 6c  Idx;.    p->aCol
21e41 5b 69 5d 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f 6c  [i].zColl = zCol
21e42 6c 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20  l;.  .    /* If 
21e43 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 64 65  the column is de
21e44 63 6c 61 72 65 64 20 61 73 20 22 3c 6e 61 6d 65  clared as "<name
21e45 3e 20 50 52 49 4d 41 52 59 20 4b 45 59 20 43 4f  > PRIMARY KEY CO
21e46 4c 4c 41 54 45 20 3c 74 79 70 65 3e 22 2c 0a 20  LLATE <type>",. 
21e47 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 20 69 6e     ** then an in
21e48 64 65 78 20 6d 61 79 20 68 61 76 65 20 62 65 65  dex may have bee
21e49 6e 20 63 72 65 61 74 65 64 20 6f 6e 20 74 68 69  n created on thi
21e4a 73 20 63 6f 6c 75 6d 6e 20 62 65 66 6f 72 65 20  s column before 
21e4b 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c 61  the.    ** colla
21e4c 74 69 6f 6e 20 74 79 70 65 20 77 61 73 20 61 64  tion type was ad
21e4d 64 65 64 2e 20 43 6f 72 72 65 63 74 20 74 68 69  ded. Correct thi
21e4e 73 20 69 66 20 69 74 20 69 73 20 74 68 65 20 63  s if it is the c
21e4f 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
21e50 66 6f 72 28 70 49 64 78 3d 70 2d 3e 70 49 6e 64  for(pIdx=p->pInd
21e51 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
21e52 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
21e53 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
21e54 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20  >nColumn==1 );. 
21e55 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61       if( pIdx->a
21e56 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29 7b  iColumn[0]==i ){
21e57 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61  .        pIdx->a
21e58 7a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61 43  zColl[0] = p->aC
21e59 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20  ol[i].zColl;.   
21e5a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
21e5b 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  se{.    sqlite3D
21e5c 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29  bFree(db, zColl)
21e5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
21e5e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
21e5f 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c 61 74 69  urns the collati
21e60 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  on sequence for 
21e61 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20  database native 
21e62 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67  text.** encoding
21e63 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
21e64 68 65 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65 2c  he string zName,
21e65 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a   length nName..*
21e66 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71 75  *.** If the requ
21e67 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  ested collation 
21e68 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 20  sequence is not 
21e69 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 20 6e 6f  available, or no
21e6a 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 69  t available.** i
21e6b 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  n the database n
21e6c 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20  ative encoding, 
21e6d 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
21e6e 63 74 6f 72 79 20 69 73 20 69 6e 76 6f 6b 65 64  ctory is invoked
21e6f 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20 69   to.** request i
21e70 74 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 74  t. If the collat
21e71 69 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f 65 73  ion factory does
21e72 20 6e 6f 74 20 73 75 70 70 6c 79 20 73 75 63 68   not supply such
21e73 20 61 20 73 65 71 75 65 6e 63 65 2c 0a 2a 2a 20   a sequence,.** 
21e74 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63 65  and the sequence
21e75 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e   is available in
21e76 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20 65 6e   another text en
21e77 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 61  coding, then tha
21e78 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  t is.** returned
21e79 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20   instead..**.** 
21e7a 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e 73 20 6f  If no versions o
21e7b 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
21e7c 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65 71 75 65  collations seque
21e7d 6e 63 65 20 61 72 65 20 61 76 61 69 6c 61 62 6c  nce are availabl
21e7e 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65 72  e, or.** another
21e7f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e   error occurs, N
21e80 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
21e81 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  and an error mes
21e82 73 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e 74  sage written int
21e83 6f 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2a 0a  o.** pParse..**.
21e84 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
21e85 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 6f  is a wrapper aro
21e86 75 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64 43  und sqlite3FindC
21e87 6f 6c 6c 53 65 71 28 29 2e 20 20 54 68 69 73 20  ollSeq().  This 
21e88 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f 6b  routine.** invok
21e89 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  es the collation
21e8a 20 66 61 63 74 6f 72 79 20 69 66 20 74 68 65 20   factory if the 
21e8b 6e 61 6d 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  named collation 
21e8c 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a  cannot be found.
21e8d 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73  ** and generates
21e8e 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
21e8f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
21e90 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
21e91 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
21e92 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  eq(Parse *pParse
21e93 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
21e94 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 7b  ame, int nName){
21e95 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
21e96 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 75   pParse->db;.  u
21e97 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  8 enc = ENC(db);
21e98 0a 20 20 75 38 20 69 6e 69 74 62 75 73 79 20 3d  .  u8 initbusy =
21e99 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3b 0a   db->init.busy;.
21e9a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
21e9b 3b 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  ;..  pColl = sql
21e9c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
21e9d 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20  db, enc, zName, 
21e9e 6e 4e 61 6d 65 2c 20 69 6e 69 74 62 75 73 79 29  nName, initbusy)
21e9f 3b 0a 20 20 69 66 28 20 21 69 6e 69 74 62 75 73  ;.  if( !initbus
21ea0 79 20 26 26 20 28 21 70 43 6f 6c 6c 20 7c 7c 20  y && (!pColl || 
21ea1 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29 20 29 7b  !pColl->xCmp) ){
21ea2 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  .    pColl = sql
21ea3 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 64  ite3GetCollSeq(d
21ea4 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c  b, pColl, zName,
21ea5 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28   nName);.    if(
21ea6 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
21ea7 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29 7b 0a   if( nName<0 ){.
21ea8 20 20 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20          nName = 
21ea9 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
21eaa 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a  zName);.      }.
21eab 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
21eac 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
21ead 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e  o such collation
21eae 20 73 65 71 75 65 6e 63 65 3a 20 25 2e 2a 73 22   sequence: %.*s"
21eaf 2c 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b  , nName, zName);
21eb0 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30  .      pColl = 0
21eb1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
21eb2 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a  eturn pColl;.}..
21eb3 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
21eb4 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 69  code that will i
21eb5 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63 68  ncrement the sch
21eb6 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a  ema cookie..**.*
21eb7 2a 20 54 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  * The schema coo
21eb8 6b 69 65 20 69 73 20 75 73 65 64 20 74 6f 20 64  kie is used to d
21eb9 65 74 65 72 6d 69 6e 65 20 77 68 65 6e 20 74 68  etermine when th
21eba 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65  e schema for the
21ebb 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 68 61  .** database cha
21ebc 6e 67 65 73 2e 20 20 41 66 74 65 72 20 65 61 63  nges.  After eac
21ebd 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2c  h schema change,
21ebe 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75   the cookie valu
21ebf 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 57  e.** changes.  W
21ec0 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 66 69  hen a process fi
21ec1 72 73 74 20 72 65 61 64 73 20 74 68 65 20 73 63  rst reads the sc
21ec2 68 65 6d 61 20 69 74 20 72 65 63 6f 72 64 73 20  hema it records 
21ec3 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20 20  the.** cookie.  
21ec4 54 68 65 72 65 61 66 74 65 72 2c 20 77 68 65 6e  Thereafter, when
21ec5 65 76 65 72 20 69 74 20 67 6f 65 73 20 74 6f 20  ever it goes to 
21ec6 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62  access the datab
21ec7 61 73 65 2c 0a 2a 2a 20 69 74 20 63 68 65 63 6b  ase,.** it check
21ec8 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20  s the cookie to 
21ec9 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 63  make sure the sc
21eca 68 65 6d 61 20 68 61 73 20 6e 6f 74 20 63 68 61  hema has not cha
21ecb 6e 67 65 64 0a 2a 2a 20 73 69 6e 63 65 20 69 74  nged.** since it
21ecc 20 77 61 73 20 6c 61 73 74 20 72 65 61 64 2e 0a   was last read..
21ecd 2a 2a 0a 2a 2a 20 54 68 69 73 20 70 6c 61 6e 20  **.** This plan 
21ece 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c  is not completel
21ecf 79 20 62 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e 20  y bullet-proof. 
21ed0 20 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   It is possible 
21ed1 66 6f 72 0a 2a 2a 20 74 68 65 20 73 63 68 65 6d  for.** the schem
21ed2 61 20 74 6f 20 63 68 61 6e 67 65 20 6d 75 6c 74  a to change mult
21ed3 69 70 6c 65 20 74 69 6d 65 73 20 61 6e 64 20 66  iple times and f
21ed4 6f 72 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f  or the cookie to
21ed5 20 62 65 0a 2a 2a 20 73 65 74 20 62 61 63 6b 20   be.** set back 
21ed6 74 6f 20 70 72 69 6f 72 20 76 61 6c 75 65 2e 20  to prior value. 
21ed7 20 42 75 74 20 73 63 68 65 6d 61 20 63 68 61 6e   But schema chan
21ed8 67 65 73 20 61 72 65 20 69 6e 66 72 65 71 75 65  ges are infreque
21ed9 6e 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 72  nt.** and the pr
21eda 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 68 69 74  obability of hit
21edb 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 6f  ting the same co
21edc 6f 6b 69 65 20 76 61 6c 75 65 20 69 73 20 6f 6e  okie value is on
21edd 6c 79 0a 2a 2a 20 31 20 63 68 61 6e 63 65 20 69  ly.** 1 chance i
21ede 6e 20 32 5e 33 32 2e 20 20 53 6f 20 77 65 27 72  n 2^32.  So we'r
21edf 65 20 73 61 66 65 20 65 6e 6f 75 67 68 2e 0a 2a  e safe enough..*
21ee0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21ee1 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61   void sqlite3Cha
21ee2 6e 67 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 20  ngeCookie(Parse 
21ee3 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62  *pParse, int iDb
21ee4 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71  ){.  int r1 = sq
21ee5 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
21ee6 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
21ee7 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
21ee8 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d  >db;.  Vdbe *v =
21ee9 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
21eea 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21eeb 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
21eec 72 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  r, db->aDb[iDb].
21eed 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f  pSchema->schema_
21eee 63 6f 6f 6b 69 65 2b 31 2c 20 72 31 29 3b 0a 20  cookie+1, r1);. 
21eef 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21ef0 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b  p3(v, OP_SetCook
21ef1 69 65 2c 20 69 44 62 2c 20 30 2c 20 72 31 29 3b  ie, iDb, 0, r1);
21ef2 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
21ef3 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
21ef4 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d   r1);.}../*.** M
21ef5 65 61 73 75 72 65 20 74 68 65 20 6e 75 6d 62 65  easure the numbe
21ef6 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
21ef7 6e 65 65 64 65 64 20 74 6f 20 6f 75 74 70 75 74  needed to output
21ef8 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 69 64   the given.** id
21ef9 65 6e 74 69 66 69 65 72 2e 20 20 54 68 65 20 6e  entifier.  The n
21efa 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 69  umber returned i
21efb 6e 63 6c 75 64 65 73 20 61 6e 79 20 71 75 6f 74  ncludes any quot
21efc 65 73 20 75 73 65 64 0a 2a 2a 20 62 75 74 20 64  es used.** but d
21efd 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20  oes not include 
21efe 74 68 65 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61  the null termina
21eff 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  tor..**.** The e
21f00 73 74 69 6d 61 74 65 20 69 73 20 63 6f 6e 73 65  stimate is conse
21f01 72 76 61 74 69 76 65 2e 20 20 49 74 20 6d 69 67  rvative.  It mig
21f02 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61  ht be larger tha
21f03 74 20 77 68 61 74 20 69 73 0a 2a 2a 20 72 65 61  t what is.** rea
21f04 6c 6c 79 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73  lly needed..*/.s
21f05 74 61 74 69 63 20 69 6e 74 20 69 64 65 6e 74 4c  tatic int identL
21f06 65 6e 67 74 68 28 63 6f 6e 73 74 20 63 68 61 72  ength(const char
21f07 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20   *z){.  int n;. 
21f08 20 66 6f 72 28 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b   for(n=0; *z; n+
21f09 2b 2c 20 7a 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, z++){.    if(
21f0a 20 2a 7a 3d 3d 27 22 27 20 29 7b 20 6e 2b 2b 3b   *z=='"' ){ n++;
21f0b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
21f0c 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n + 2;.}../*.** 
21f0d 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
21f0e 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e   a wrapper aroun
21f0f 64 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65  d sqlite3GetToke
21f10 6e 28 29 20 75 73 65 64 20 62 79 20 0a 2a 2a 20  n() used by .** 
21f11 69 73 56 61 6c 69 64 44 69 6d 65 6e 73 69 6f 6e  isValidDimension
21f12 28 29 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  (). This functio
21f13 6e 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 73  n differs from s
21f14 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 29  qlite3GetToken()
21f15 20 69 6e 0a 2a 2a 20 74 68 61 74 3a 0a 2a 2a 0a   in.** that:.**.
21f16 2a 2a 20 20 20 2a 20 57 68 69 74 65 73 70 61 63  **   * Whitespac
21f17 65 20 69 73 20 69 67 6e 6f 72 65 64 2c 20 61 6e  e is ignored, an
21f18 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6f 75 74  d.**   * The out
21f19 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a 70 65  put variable *pe
21f1a 54 6f 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20  Token is set to 
21f1b 30 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20  0 if the end of 
21f1c 74 68 65 0a 2a 2a 20 20 20 20 20 6e 75 6c 2d 74  the.**     nul-t
21f1d 65 72 6d 69 6e 61 74 65 64 20 69 6e 70 75 74 20  erminated input 
21f1e 73 74 72 69 6e 67 20 69 73 20 72 65 61 63 68 65  string is reache
21f1f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
21f20 20 67 65 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65   getTokenNoSpace
21f21 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
21f22 7a 2c 20 69 6e 74 20 2a 70 65 54 6f 6b 65 6e 29  z, int *peToken)
21f23 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20  {.  int n = 0;. 
21f24 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
21f25 73 73 70 61 63 65 28 7a 5b 6e 5d 29 20 29 20 6e  sspace(z[n]) ) n
21f26 2b 2b 3b 0a 20 20 69 66 28 20 21 7a 5b 6e 5d 20  ++;.  if( !z[n] 
21f27 29 7b 0a 20 20 20 20 2a 70 65 54 6f 6b 65 6e 20  ){.    *peToken 
21f28 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
21f29 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
21f2a 6e 20 2b 20 73 71 6c 69 74 65 33 47 65 74 54 6f  n + sqlite3GetTo
21f2b 6b 65 6e 28 26 7a 5b 6e 5d 2c 20 70 65 54 6f 6b  ken(&z[n], peTok
21f2c 65 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61  en);.}../*.** Pa
21f2d 72 61 6d 65 74 65 72 20 7a 20 70 6f 69 6e 74 73  rameter z points
21f2e 20 74 6f 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e   to a nul-termin
21f2f 61 74 65 64 20 73 74 72 69 6e 67 2e 20 52 65 74  ated string. Ret
21f30 75 72 6e 20 74 72 75 65 20 69 66 2c 20 77 68 65  urn true if, whe
21f31 6e 0a 2a 2a 20 77 68 69 74 65 73 70 61 63 65 20  n.** whitespace 
21f32 69 73 20 69 67 6e 6f 72 65 64 2c 20 74 68 65 20  is ignored, the 
21f33 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73  contents of this
21f34 20 73 74 72 69 6e 67 20 6d 61 74 63 68 65 73 20   string matches 
21f35 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 66  one of .** the f
21f36 6f 6c 6c 6f 77 69 6e 67 20 70 61 74 74 65 72 6e  ollowing pattern
21f37 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 22 0a  s:.**.**     "".
21f38 2a 2a 20 20 20 20 20 22 28 6e 75 6d 62 65 72 29  **     "(number)
21f39 22 0a 2a 2a 20 20 20 20 20 22 28 6e 75 6d 62 65  ".**     "(numbe
21f3a 72 2c 6e 75 6d 62 65 72 29 22 0a 2a 2f 0a 73 74  r,number)".*/.st
21f3b 61 74 69 63 20 69 6e 74 20 69 73 56 61 6c 69 64  atic int isValid
21f3c 44 69 6d 65 6e 73 69 6f 6e 28 75 6e 73 69 67 6e  Dimension(unsign
21f3d 65 64 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  ed char *z){.  i
21f3e 6e 74 20 65 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74  nt eToken;.  int
21f3f 20 6e 20 3d 20 30 3b 0a 20 20 6e 20 2b 3d 20 67   n = 0;.  n += g
21f40 65 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65 28 26  etTokenNoSpace(&
21f41 7a 5b 6e 5d 2c 20 26 65 54 6f 6b 65 6e 29 3b 0a  z[n], &eToken);.
21f42 20 20 69 66 28 20 65 54 6f 6b 65 6e 20 29 7b 0a    if( eToken ){.
21f43 20 20 20 20 69 66 28 20 65 54 6f 6b 65 6e 21 3d      if( eToken!=
21f44 54 4b 5f 4c 50 20 29 20 72 65 74 75 72 6e 20 30  TK_LP ) return 0
21f45 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f  ;.    n += getTo
21f46 6b 65 6e 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d  kenNoSpace(&z[n]
21f47 2c 20 26 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  , &eToken);.    
21f48 69 66 28 20 65 54 6f 6b 65 6e 3d 3d 54 4b 5f 50  if( eToken==TK_P
21f49 4c 55 53 20 7c 7c 20 65 54 6f 6b 65 6e 3d 3d 54  LUS || eToken==T
21f4a 4b 5f 4d 49 4e 55 53 20 29 7b 0a 20 20 20 20 20  K_MINUS ){.     
21f4b 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e 4e 6f   n += getTokenNo
21f4c 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54  Space(&z[n], &eT
21f4d 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20  oken);.    }.   
21f4e 20 69 66 28 20 65 54 6f 6b 65 6e 21 3d 54 4b 5f   if( eToken!=TK_
21f4f 49 4e 54 45 47 45 52 20 26 26 20 65 54 6f 6b 65  INTEGER && eToke
21f50 6e 21 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 72 65  n!=TK_FLOAT ) re
21f51 74 75 72 6e 20 30 3b 0a 20 20 20 20 6e 20 2b 3d  turn 0;.    n +=
21f52 20 67 65 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65   getTokenNoSpace
21f53 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f 6b 65 6e 29  (&z[n], &eToken)
21f54 3b 0a 20 20 20 20 69 66 28 20 65 54 6f 6b 65 6e  ;.    if( eToken
21f55 3d 3d 54 4b 5f 43 4f 4d 4d 41 20 29 7b 0a 20 20  ==TK_COMMA ){.  
21f56 20 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65      n += getToke
21f57 6e 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20  nNoSpace(&z[n], 
21f58 26 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20  &eToken);.      
21f59 69 66 28 20 65 54 6f 6b 65 6e 3d 3d 54 4b 5f 50  if( eToken==TK_P
21f5a 4c 55 53 20 7c 7c 20 65 54 6f 6b 65 6e 3d 3d 54  LUS || eToken==T
21f5b 4b 5f 4d 49 4e 55 53 20 29 7b 0a 20 20 20 20 20  K_MINUS ){.     
21f5c 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e     n += getToken
21f5d 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26  NoSpace(&z[n], &
21f5e 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d  eToken);.      }
21f5f 0a 20 20 20 20 20 20 69 66 28 20 65 54 6f 6b 65  .      if( eToke
21f60 6e 21 3d 54 4b 5f 49 4e 54 45 47 45 52 20 26 26  n!=TK_INTEGER &&
21f61 20 65 54 6f 6b 65 6e 21 3d 54 4b 5f 46 4c 4f 41   eToken!=TK_FLOA
21f62 54 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  T ) return 0;.  
21f63 20 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65      n += getToke
21f64 6e 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20  nNoSpace(&z[n], 
21f65 26 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a  &eToken);.    }.
21f66 20 20 20 20 69 66 28 20 65 54 6f 6b 65 6e 21 3d      if( eToken!=
21f67 54 4b 5f 52 50 20 29 20 72 65 74 75 72 6e 20 30  TK_RP ) return 0
21f68 3b 0a 20 20 20 20 67 65 74 54 6f 6b 65 6e 4e 6f  ;.    getTokenNo
21f69 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54  Space(&z[n], &eT
21f6a 6f 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28  oken);.  }.  if(
21f6b 20 65 54 6f 6b 65 6e 20 29 20 72 65 74 75 72 6e   eToken ) return
21f6c 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a   0;.  return 1;.
21f6d 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72  }../*.** The fir
21f6e 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  st parameter is 
21f6f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
21f70 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 20 54  output buffer. T
21f71 68 65 20 73 65 63 6f 6e 64 20 0a 2a 2a 20 70 61  he second .** pa
21f72 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69  rameter is a poi
21f73 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 67  nter to an integ
21f74 65 72 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  er that contains
21f75 20 74 68 65 20 6f 66 66 73 65 74 20 61 74 0a 2a   the offset at.*
21f76 2a 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65  * which to write
21f77 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74   into the output
21f78 20 62 75 66 66 65 72 2e 20 54 68 69 73 20 66 75   buffer. This fu
21f79 6e 63 74 69 6f 6e 20 63 6f 70 69 65 73 20 74 68  nction copies th
21f7a 65 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  e.** nul-termina
21f7b 74 65 64 20 73 74 72 69 6e 67 20 70 6f 69 6e 74  ted string point
21f7c 65 64 20 74 6f 20 62 79 20 74 68 65 20 74 68 69  ed to by the thi
21f7d 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 7a 53  rd parameter, zS
21f7e 69 67 6e 65 64 49 64 65 6e 74 2c 0a 2a 2a 20 74  ignedIdent,.** t
21f7f 6f 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  o the specified 
21f80 6f 66 66 73 65 74 20 69 6e 20 74 68 65 20 62 75  offset in the bu
21f81 66 66 65 72 20 61 6e 64 20 75 70 64 61 74 65 73  ffer and updates
21f82 20 2a 70 49 64 78 20 74 6f 20 72 65 66 65 72 0a   *pIdx to refer.
21f83 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  ** to the first 
21f84 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 6c  byte after the l
21f85 61 73 74 20 62 79 74 65 20 77 72 69 74 74 65 6e  ast byte written
21f86 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
21f87 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65  g..** .** If the
21f88 20 73 74 72 69 6e 67 20 7a 53 69 67 6e 65 64 49   string zSignedI
21f89 64 65 6e 74 20 63 6f 6e 73 69 73 74 73 20 65 6e  dent consists en
21f8a 74 69 72 65 6c 79 20 6f 66 20 61 6c 70 68 61 2d  tirely of alpha-
21f8b 6e 75 6d 65 72 69 63 0a 2a 2a 20 63 68 61 72 61  numeric.** chara
21f8c 63 74 65 72 73 2c 20 64 6f 65 73 20 6e 6f 74 20  cters, does not 
21f8d 62 65 67 69 6e 20 77 69 74 68 20 61 20 64 69 67  begin with a dig
21f8e 69 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 6e  it and is not an
21f8f 20 53 51 4c 20 6b 65 79 77 6f 72 64 2c 0a 2a 2a   SQL keyword,.**
21f90 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69   then it is copi
21f91 65 64 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74  ed to the output
21f92 20 62 75 66 66 65 72 20 65 78 61 63 74 6c 79 20   buffer exactly 
21f93 61 73 20 69 74 20 69 73 2e 20 4f 74 68 65 72 77  as it is. Otherw
21f94 69 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 71 75  ise,.** it is qu
21f95 6f 74 65 64 20 75 73 69 6e 67 20 64 6f 75 62 6c  oted using doubl
21f96 65 2d 71 75 6f 74 65 73 2e 0a 2a 2f 0a 73 74 61  e-quotes..*/.sta
21f97 74 69 63 20 76 6f 69 64 20 69 64 65 6e 74 50 75  tic void identPu
21f98 74 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a  t(char *z, int *
21f99 70 49 64 78 2c 20 63 68 61 72 20 2a 7a 53 69 67  pIdx, char *zSig
21f9a 6e 65 64 49 64 65 6e 74 2c 20 69 6e 74 20 69 73  nedIdent, int is
21f9b 54 79 70 65 6e 61 6d 65 29 7b 0a 20 20 75 6e 73  Typename){.  uns
21f9c 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 64 65  igned char *zIde
21f9d 6e 74 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  nt = (unsigned c
21f9e 68 61 72 2a 29 7a 53 69 67 6e 65 64 49 64 65 6e  har*)zSignedIden
21f9f 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e  t;.  int i, j, n
21fa0 65 65 64 51 75 6f 74 65 3b 0a 20 20 69 20 3d 20  eedQuote;.  i = 
21fa1 2a 70 49 64 78 3b 0a 0a 20 20 66 6f 72 28 6a 3d  *pIdx;..  for(j=
21fa2 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b  0; zIdent[j]; j+
21fa3 2b 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c  +){.    if( !sql
21fa4 69 74 65 33 49 73 61 6c 6e 75 6d 28 7a 49 64 65  ite3Isalnum(zIde
21fa5 6e 74 5b 6a 5d 29 20 26 26 20 7a 49 64 65 6e 74  nt[j]) && zIdent
21fa6 5b 6a 5d 21 3d 27 5f 27 20 29 20 62 72 65 61 6b  [j]!='_' ) break
21fa7 3b 0a 20 20 7d 0a 20 20 6e 65 65 64 51 75 6f 74  ;.  }.  needQuot
21fa8 65 20 3d 20 73 71 6c 69 74 65 33 49 73 64 69 67  e = sqlite3Isdig
21fa9 69 74 28 7a 49 64 65 6e 74 5b 30 5d 29 20 7c 7c  it(zIdent[0]) ||
21faa 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43   sqlite3KeywordC
21fab 6f 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29 21 3d  ode(zIdent, j)!=
21fac 54 4b 5f 49 44 3b 0a 20 20 69 66 28 20 21 6e 65  TK_ID;.  if( !ne
21fad 65 64 51 75 6f 74 65 20 29 7b 0a 20 20 20 20 69  edQuote ){.    i
21fae 66 28 20 69 73 54 79 70 65 6e 61 6d 65 20 29 7b  f( isTypename ){
21faf 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69  .      /* If thi
21fb0 73 20 69 73 20 61 20 74 79 70 65 2d 6e 61 6d 65  s is a type-name
21fb1 2c 20 61 6c 6c 6f 77 20 61 20 6c 69 74 74 6c 65  , allow a little
21fb2 20 6d 6f 72 65 20 66 6c 65 78 69 62 69 6c 69 74   more flexibilit
21fb3 79 2e 20 49 6e 20 53 51 4c 69 74 65 2c 0a 20 20  y. In SQLite,.  
21fb4 20 20 20 20 2a 2a 20 61 20 74 79 70 65 2d 6e 61      ** a type-na
21fb5 6d 65 20 69 73 20 73 70 65 63 69 66 69 65 64 20  me is specified 
21fb6 61 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  as:.      **.   
21fb7 20 20 20 2a 2a 20 20 20 69 64 73 20 5b 69 64 73     **   ids [ids
21fb8 5d 20 5b 28 6e 75 6d 62 65 72 20 5b 2c 20 6e 75  ] [(number [, nu
21fb9 6d 62 65 72 5d 29 5d 0a 20 20 20 20 20 20 2a 2a  mber])].      **
21fba 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 72 65 20  .      ** where 
21fbb 22 69 64 73 22 20 69 73 20 65 69 74 68 65 72 20  "ids" is either 
21fbc 61 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20  a quoted string 
21fbd 6f 72 20 61 20 73 69 6d 70 6c 65 20 69 64 65 6e  or a simple iden
21fbe 74 69 66 69 65 72 20 28 69 6e 20 74 68 65 0a 20  tifier (in the. 
21fbf 20 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 6e 6f       ** above no
21fc0 74 61 74 69 6f 6e 2c 20 5b 5d 20 6d 65 61 6e 73  tation, [] means
21fc1 20 6f 70 74 69 6f 6e 61 6c 29 2e 20 49 74 20 69   optional). It i
21fc2 73 20 61 20 62 69 74 20 74 72 69 63 6b 79 20 74  s a bit tricky t
21fc3 6f 20 63 68 65 63 6b 0a 20 20 20 20 20 20 2a 2a  o check.      **
21fc4 20 66 6f 72 20 61 6c 6c 20 63 61 73 65 73 2c 20   for all cases, 
21fc5 62 75 74 20 69 74 20 69 73 20 67 6f 6f 64 20 74  but it is good t
21fc6 6f 20 61 76 6f 69 64 20 75 6e 6e 65 63 65 73 73  o avoid unnecess
21fc7 61 72 69 6c 79 20 71 75 6f 74 69 6e 67 20 63 6f  arily quoting co
21fc8 6d 6d 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 74 79  mmon.      ** ty
21fc9 70 65 6e 61 6d 65 73 20 6c 69 6b 65 20 56 41 52  penames like VAR
21fca 43 48 41 52 28 31 30 29 2e 0a 20 20 20 20 20 20  CHAR(10)..      
21fcb 2a 2f 0a 20 20 20 20 20 20 6e 65 65 64 51 75 6f  */.      needQuo
21fcc 74 65 20 3d 20 21 69 73 56 61 6c 69 64 44 69 6d  te = !isValidDim
21fcd 65 6e 73 69 6f 6e 28 26 7a 49 64 65 6e 74 5b 6a  ension(&zIdent[j
21fce 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ]);.    }else{. 
21fcf 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 3d       needQuote =
21fd0 20 7a 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20   zIdent[j];.    
21fd1 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 65 65  }.  }..  if( nee
21fd2 64 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20  dQuote ) z[i++] 
21fd3 3d 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30  = '"';.  for(j=0
21fd4 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b  ; zIdent[j]; j++
21fd5 29 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20  ){.    z[i++] = 
21fd6 7a 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69  zIdent[j];.    i
21fd7 66 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22  f( zIdent[j]=='"
21fd8 27 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27  ' ) z[i++] = '"'
21fd9 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64  ;.  }.  if( need
21fda 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d  Quote ) z[i++] =
21fdb 20 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30   '"';.  z[i] = 0
21fdc 3b 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d  ;.  *pIdx = i;.}
21fdd 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
21fde 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   a CREATE TABLE 
21fdf 73 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70  statement approp
21fe0 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 69  riate for the gi
21fe1 76 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d  ven.** table.  M
21fe2 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68  emory to hold th
21fe3 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 74  e text of the st
21fe4 61 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69  atement is obtai
21fe5 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69  ned.** from sqli
21fe6 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d  teMalloc() and m
21fe7 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20  ust be freed by 
21fe8 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
21fe9 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
21fea 63 68 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c  char *createTabl
21feb 65 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64  eStmt(sqlite3 *d
21fec 62 2c 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20  b, Table *p){.  
21fed 69 6e 74 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63  int i, k, n;.  c
21fee 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68  har *zStmt;.  ch
21fef 61 72 20 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32  ar *zSep, *zSep2
21ff0 2c 20 2a 7a 45 6e 64 2c 20 2a 7a 3b 0a 20 20 43  , *zEnd, *z;.  C
21ff1 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 6e  olumn *pCol;.  n
21ff2 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 6f 6c   = 0;.  for(pCol
21ff3 20 3d 20 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b   = p->aCol, i=0;
21ff4 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c   i<p->nCol; i++,
21ff5 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 6e 20   pCol++){.    n 
21ff6 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 70  += identLength(p
21ff7 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Col->zName);.   
21ff8 20 7a 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65   z = pCol->zType
21ff9 3b 0a 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20  ;.    if( z ){. 
21ffa 20 20 20 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c       n += identL
21ffb 65 6e 67 74 68 28 7a 29 3b 0a 20 20 20 20 7d 0a  ength(z);.    }.
21ffc 20 20 7d 0a 20 20 6e 20 2b 3d 20 69 64 65 6e 74    }.  n += ident
21ffd 4c 65 6e 67 74 68 28 70 2d 3e 7a 4e 61 6d 65 29  Length(p->zName)
21ffe 3b 0a 20 20 69 66 28 20 6e 3c 35 30 20 29 7b 0a  ;.  if( n<50 ){.
21fff 20 20 20 20 7a 53 65 70 20 3d 20 22 22 3b 0a 20      zSep = "";. 
22000 20 20 20 7a 53 65 70 32 20 3d 20 22 2c 22 3b 0a     zSep2 = ",";.
22001 20 20 20 20 7a 45 6e 64 20 3d 20 22 29 22 3b 0a      zEnd = ")";.
22002 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 53 65    }else{.    zSe
22003 70 20 3d 20 22 5c 6e 20 20 22 3b 0a 20 20 20 20  p = "\n  ";.    
22004 7a 53 65 70 32 20 3d 20 22 2c 5c 6e 20 20 22 3b  zSep2 = ",\n  ";
22005 0a 20 20 20 20 7a 45 6e 64 20 3d 20 22 5c 6e 29  .    zEnd = "\n)
22006 22 3b 0a 20 20 7d 0a 20 20 6e 20 2b 3d 20 33 35  ";.  }.  n += 35
22007 20 2b 20 36 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20   + 6*p->nCol;.  
22008 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d  zStmt = sqlite3M
22009 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 69 66  alloc( n );.  if
2200a 28 20 7a 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20  ( zStmt==0 ){.  
2200b 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2200c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  ed = 1;.    retu
2200d 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  rn 0;.  }.  sqli
2200e 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2c 20  te3_snprintf(n, 
2200f 7a 53 74 6d 74 2c 20 22 43 52 45 41 54 45 20 54  zStmt, "CREATE T
22010 41 42 4c 45 20 22 29 3b 0a 20 20 6b 20 3d 20 73  ABLE ");.  k = s
22011 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
22012 53 74 6d 74 29 3b 0a 20 20 69 64 65 6e 74 50 75  Stmt);.  identPu
22013 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e  t(zStmt, &k, p->
22014 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7a 53 74  zName, 0);.  zSt
22015 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20  mt[k++] = '(';. 
22016 20 66 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f   for(pCol=p->aCo
22017 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  l, i=0; i<p->nCo
22018 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; i++, pCol++){
22019 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
2201a 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d  rintf(n-k, &zStm
2201b 74 5b 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20 20 20  t[k], zSep);.   
2201c 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72   k += sqlite3Str
2201d 6c 65 6e 33 30 28 26 7a 53 74 6d 74 5b 6b 5d 29  len30(&zStmt[k])
2201e 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 7a 53 65  ;.    zSep = zSe
2201f 70 32 3b 0a 20 20 20 20 69 64 65 6e 74 50 75 74  p2;.    identPut
22020 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 43 6f 6c  (zStmt, &k, pCol
22021 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
22022 20 69 66 28 20 28 7a 20 3d 20 70 43 6f 6c 2d 3e   if( (z = pCol->
22023 7a 54 79 70 65 29 21 3d 30 20 29 7b 0a 20 20 20  zType)!=0 ){.   
22024 20 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20     zStmt[k++] = 
22025 27 20 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ' ';.      asser
22026 74 28 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33  t( (int)(sqlite3
22027 53 74 72 6c 65 6e 33 30 28 7a 29 2b 6b 2b 31 29  Strlen30(z)+k+1)
22028 3c 3d 6e 20 29 3b 0a 20 20 20 20 20 20 69 64 65  <=n );.      ide
22029 6e 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c  ntPut(zStmt, &k,
2202a 20 7a 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20   z, 1);.    }.  
2202b 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  }.  sqlite3_snpr
2202c 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74  intf(n-k, &zStmt
2202d 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29  [k], "%s", zEnd)
2202e 3b 0a 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74  ;.  return zStmt
2202f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
22030 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
22031 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20  d to report the 
22032 66 69 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74  final ")" that t
22033 65 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43  erminates.** a C
22034 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
22035 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ement..**.** The
22036 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   table structure
22037 20 74 68 61 74 20 6f 74 68 65 72 20 61 63 74 69   that other acti
22038 6f 6e 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65  on routines have
22039 20 62 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a   been building.*
2203a 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  * is added to th
2203b 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20  e internal hash 
2203c 74 61 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67  tables, assuming
2203d 20 6e 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a   no errors have.
2203e 2a 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a  ** occurred..**.
2203f 2a 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20  ** An entry for 
22040 74 68 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64  the table is mad
22041 65 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20  e in the master 
22042 74 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75  table on disk, u
22043 6e 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73  nless.** this is
22044 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62   a temporary tab
22045 6c 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62  le or db->init.b
22046 75 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62  usy==1.  When db
22047 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a  ->init.busy==1.*
22048 2a 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72  * it means we ar
22049 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 73 71  e reading the sq
2204a 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
2204b 65 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73  e because we jus
2204c 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74  t.** connected t
2204d 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f  o the database o
2204e 72 20 62 65 63 61 75 73 65 20 74 68 65 20 73 71  r because the sq
2204f 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
22050 65 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c  e has.** recentl
22051 79 20 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68  y changed, so th
22052 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73  e entry for this
22053 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65   table already e
22054 78 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20  xists in.** the 
22055 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
22056 62 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20  ble.  We do not 
22057 77 61 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69  want to create i
22058 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49  t again..**.** I
22059 66 20 74 68 65 20 70 53 65 6c 65 63 74 20 61 72  f the pSelect ar
2205a 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55  gument is not NU
2205b 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  LL, it means tha
2205c 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  t this routine.*
2205d 2a 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20  * was called to 
2205e 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 67  create a table g
2205f 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20  enerated from a 
22060 0a 2a 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c  .** "CREATE TABL
22061 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20  E ... AS SELECT 
22062 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20  ..." statement. 
22063 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   The column name
22064 73 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20  s of.** the new 
22065 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68  table will match
22066 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
22067 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a  of the SELECT..*
22068 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22069 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64   void sqlite3End
2206a 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
2206b 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2206c 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2206d 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43  t */.  Token *pC
2206e 6f 6e 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ons,           /
2206f 2a 20 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20  * The ',' token 
22070 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 63  after the last c
22071 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20  olumn defn. */. 
22072 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20   Token *pEnd,   
22073 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
22074 66 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20  final ')' token 
22075 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41  in the CREATE TA
22076 42 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  BLE */.  Select 
22077 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 20  *pSelect        
22078 20 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20   /* Select from 
22079 61 20 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53  a "CREATE ... AS
2207a 20 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20   SELECT" */.){. 
2207b 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c   Table *p;.  sql
2207c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2207d 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62  e->db;.  int iDb
2207e 3b 0a 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d  ;..  if( (pEnd==
2207f 30 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29  0 && pSelect==0)
22080 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
22081 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
22082 69 6c 65 64 20 29 20 7b 0a 20 20 20 20 72 65 74  iled ) {.    ret
22083 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70  urn;.  }.  p = p
22084 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
22085 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
22086 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74  eturn;..  assert
22087 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
22088 20 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a   || !pSelect );.
22089 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
2208a 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
2208b 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a  , p->pSchema);..
2208c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2208d 4d 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52  MIT_CHECK.  /* R
2208e 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20  esolve names in 
2208f 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  all CHECK constr
22090 61 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73  aint expressions
22091 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
22092 70 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72  pCheck ){.    Sr
22093 63 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20 20  cList sSrc;     
22094 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22095 20 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f   Fake SrcList fo
22096 72 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  r pParse->pNewTa
22097 62 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43  ble */.    NameC
22098 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20  ontext sNC;     
22099 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
2209a 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70  me context for p
2209b 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2209c 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28   */..    memset(
2209d 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
2209e 73 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65  sNC));.    memse
2209f 74 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65  t(&sSrc, 0, size
220a0 6f 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73  of(sSrc));.    s
220a1 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20  Src.nSrc = 1;.  
220a2 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d    sSrc.a[0].zNam
220a3 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  e = p->zName;.  
220a4 20 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62    sSrc.a[0].pTab
220a5 20 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61   = p;.    sSrc.a
220a6 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31  [0].iCursor = -1
220a7 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65  ;.    sNC.pParse
220a8 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73   = pParse;.    s
220a9 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73  NC.pSrcList = &s
220aa 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43  Src;.    sNC.isC
220ab 68 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66  heck = 1;.    if
220ac 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
220ad 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
220ae 70 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20  p->pCheck) ){.  
220af 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
220b0 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  }.  }.#endif /* 
220b1 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
220b2 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a  OMIT_CHECK) */..
220b3 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e    /* If the db->
220b4 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 69  init.busy is 1 i
220b5 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72  t means we are r
220b6 65 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f  eading the SQL o
220b7 66 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c  ff the.  ** "sql
220b8 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22  ite_master" or "
220b9 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
220ba 65 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65  er" table on the
220bb 20 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64   disk..  ** So d
220bc 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74  o not write to t
220bd 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20  he disk again.  
220be 45 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74  Extract the root
220bf 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a   page number.  *
220c0 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  * for the table 
220c1 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69  from the db->ini
220c2 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e  t.newTnum field.
220c3 20 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62    (The page numb
220c4 65 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68  er.  ** should h
220c5 61 76 65 20 62 65 65 6e 20 70 75 74 20 74 68 65  ave been put the
220c6 72 65 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  re by the sqlite
220c7 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29  OpenCb routine.)
220c8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
220c9 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
220ca 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69   p->tnum = db->i
220cb 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d  nit.newTnum;.  }
220cc 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e  ..  /* If not in
220cd 69 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e  itializing, then
220ce 20 63 72 65 61 74 65 20 61 20 72 65 63 6f 72 64   create a record
220cf 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62   for the new tab
220d0 6c 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53  le.  ** in the S
220d1 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
220d2 6c 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  le of the databa
220d3 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  se..  **.  ** If
220d4 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f   this is a TEMPO
220d5 52 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74  RARY table, writ
220d6 65 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f  e the entry into
220d7 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20   the auxiliary. 
220d8 20 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64   ** file instead
220d9 20 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69   of into the mai
220da 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
220db 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d  .  */.  if( !db-
220dc 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
220dd 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62    int n;.    Vdb
220de 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a  e *v;.    char *
220df 7a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69  zType;    /* "vi
220e0 65 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a  ew" or "table" *
220e1 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70  /.    char *zTyp
220e2 65 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20  e2;   /* "VIEW" 
220e3 6f 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20  or "TABLE" */.  
220e4 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20    char *zStmt;  
220e5 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65    /* Text of the
220e6 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72   CREATE TABLE or
220e7 20 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61   CREATE VIEW sta
220e8 74 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76  tement */..    v
220e9 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
220ea 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69  e(pParse);.    i
220eb 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
220ec 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
220ed 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
220ee 6c 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f  lose, 0);..    /
220ef 2a 20 0a 20 20 20 20 2a 2a 20 49 6e 69 74 69 61  * .    ** Initia
220f0 6c 69 7a 65 20 7a 54 79 70 65 20 66 6f 72 20 74  lize zType for t
220f1 68 65 20 6e 65 77 20 76 69 65 77 20 6f 72 20 74  he new view or t
220f2 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  able..    */.   
220f3 20 69 66 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d   if( p->pSelect=
220f4 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41  =0 ){.      /* A
220f5 20 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20 2a   regular table *
220f6 2f 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20  /.      zType = 
220f7 22 74 61 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a  "table";.      z
220f8 54 79 70 65 32 20 3d 20 22 54 41 42 4c 45 22 3b  Type2 = "TABLE";
220f9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
220fa 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 7d 65  OMIT_VIEW.    }e
220fb 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  lse{.      /* A 
220fc 76 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54  view */.      zT
220fd 79 70 65 20 3d 20 22 76 69 65 77 22 3b 0a 20 20  ype = "view";.  
220fe 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22 56 49      zType2 = "VI
220ff 45 57 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  EW";.#endif.    
22100 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  }..    /* If thi
22101 73 20 69 73 20 61 20 43 52 45 41 54 45 20 54 41  s is a CREATE TA
22102 42 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43 54  BLE xx AS SELECT
22103 20 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74 68   ..., execute th
22104 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20  e SELECT.    ** 
22105 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70  statement to pop
22106 75 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74 61  ulate the new ta
22107 62 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61  ble. The root-pa
22108 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ge number for th
22109 65 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74 61 62  e.    ** new tab
2210a 6c 65 20 69 73 20 69 6e 20 72 65 67 69 73 74 65  le is in registe
2210b 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f  r pParse->regRoo
2210c 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  t..    **.    **
2210d 20 4f 6e 63 65 20 74 68 65 20 53 45 4c 45 43 54   Once the SELECT
2210e 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64 20   has been coded 
2210f 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  by sqlite3Select
22110 28 29 2c 20 69 74 20 69 73 20 69 6e 20 61 0a 20  (), it is in a. 
22111 20 20 20 2a 2a 20 73 75 69 74 61 62 6c 65 20 73     ** suitable s
22112 74 61 74 65 20 74 6f 20 71 75 65 72 79 20 66 6f  tate to query fo
22113 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  r the column nam
22114 65 73 20 61 6e 64 20 74 79 70 65 73 20 74 6f 20  es and types to 
22115 62 65 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 62  be used.    ** b
22116 79 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e  y the new table.
22117 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41  .    **.    ** A
22118 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 77 72   shared-cache wr
22119 69 74 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20  ite-lock is not 
2211a 72 65 71 75 69 72 65 64 20 74 6f 20 77 72 69 74  required to writ
2211b 65 20 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62  e to the new tab
2211c 6c 65 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61 20  le,.    ** as a 
2211d 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74  schema-lock must
2211e 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
2211f 65 6e 20 6f 62 74 61 69 6e 65 64 20 74 6f 20 63  en obtained to c
22120 72 65 61 74 65 20 69 74 2e 20 53 69 6e 63 65 0a  reate it. Since.
22121 20 20 20 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d      ** a schema-
22122 6c 6f 63 6b 20 65 78 63 6c 75 64 65 73 20 61 6c  lock excludes al
22123 6c 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  l other database
22124 20 75 73 65 72 73 2c 20 74 68 65 20 77 72 69 74   users, the writ
22125 65 2d 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20  e-lock would.   
22126 20 2a 2a 20 62 65 20 72 65 64 75 6e 64 61 6e 74   ** be redundant
22127 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
22128 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
22129 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73    SelectDest des
2212a 74 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a  t;.      Table *
2212b 70 53 65 6c 54 61 62 3b 0a 0a 20 20 20 20 20 20  pSelTab;..      
2212c 61 73 73 65 72 74 28 70 50 61 72 73 65 2d 3e 6e  assert(pParse->n
2212d 54 61 62 3d 3d 31 29 3b 0a 20 20 20 20 20 20 73  Tab==1);.      s
2212e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2212f 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  (v, OP_OpenWrite
22130 2c 20 31 2c 20 70 50 61 72 73 65 2d 3e 72 65 67  , 1, pParse->reg
22131 52 6f 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20 20  Root, iDb);.    
22132 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
22133 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 20  ngeP5(v, 1);.   
22134 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20     pParse->nTab 
22135 3d 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 2;.      sqlit
22136 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
22137 28 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c  (&dest, SRT_Tabl
22138 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  e, 1);.      sql
22139 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
2213a 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73  e, pSelect, &des
2213b 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
2213c 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2213d 50 5f 43 6c 6f 73 65 2c 20 31 29 3b 0a 20 20 20  P_Close, 1);.   
2213e 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e     if( pParse->n
2213f 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Err==0 ){.      
22140 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69    pSelTab = sqli
22141 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65  te3ResultSetOfSe
22142 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65  lect(pParse, pSe
22143 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 69  lect);.        i
22144 66 28 20 70 53 65 6c 54 61 62 3d 3d 30 20 29 20  f( pSelTab==0 ) 
22145 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20  return;.        
22146 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 3d  assert( p->aCol=
22147 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  =0 );.        p-
22148 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d  >nCol = pSelTab-
22149 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70  >nCol;.        p
2214a 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62  ->aCol = pSelTab
2214b 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  ->aCol;.        
2214c 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20  pSelTab->nCol = 
2214d 30 3b 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54  0;.        pSelT
2214e 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20  ab->aCol = 0;.  
2214f 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c        sqlite3Del
22150 65 74 65 54 61 62 6c 65 28 70 53 65 6c 54 61 62  eteTable(pSelTab
22151 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
22152 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65  ..    /* Compute
22153 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65   the complete te
22154 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
22155 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
22156 20 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b    if( pSelect ){
22157 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63  .      zStmt = c
22158 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74 28 64  reateTableStmt(d
22159 62 2c 20 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65  b, p);.    }else
2215a 7b 0a 20 20 20 20 20 20 6e 20 3d 20 28 69 6e 74  {.      n = (int
2215b 29 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72  )(pEnd->z - pPar
2215c 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a  se->sNameToken.z
2215d 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53 74  ) + 1;.      zSt
2215e 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  mt = sqlite3MPri
2215f 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20  ntf(db, .       
22160 20 20 20 22 43 52 45 41 54 45 20 25 73 20 25 2e     "CREATE %s %.
22161 2a 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20  *s", zType2, n, 
22162 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
22163 65 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20  en.z.      );.  
22164 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c    }..    /* A sl
22165 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ot for the recor
22166 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  d has already be
22167 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  en allocated in 
22168 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49  the .    ** SQLI
22169 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e  TE_MASTER table.
2216a 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74    We just need t
2216b 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c  o update that sl
2216c 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20  ot with all.    
2216d 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  ** the informati
2216e 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74  on we've collect
2216f 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ed..    */.    s
22170 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
22171 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
22172 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 22 0a  "UPDATE %Q.%s ".
22173 20 20 20 20 20 20 20 20 20 22 53 45 54 20 74 79           "SET ty
22174 70 65 3d 27 25 73 27 2c 20 6e 61 6d 65 3d 25 51  pe='%s', name=%Q
22175 2c 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72  , tbl_name=%Q, r
22176 6f 6f 74 70 61 67 65 3d 23 25 64 2c 20 73 71 6c  ootpage=#%d, sql
22177 3d 25 51 20 22 0a 20 20 20 20 20 20 20 22 57 48  =%Q ".       "WH
22178 45 52 45 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a  ERE rowid=#%d",.
22179 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
2217a 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
2217b 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20  _TABLE(iDb),.   
2217c 20 20 20 7a 54 79 70 65 2c 0a 20 20 20 20 20 20     zType,.      
2217d 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  p->zName,.      
2217e 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  p->zName,.      
2217f 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c  pParse->regRoot,
22180 0a 20 20 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20  .      zStmt,.  
22181 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52      pParse->regR
22182 6f 77 69 64 0a 20 20 20 20 29 3b 0a 20 20 20 20  owid.    );.    
22183 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22184 2c 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 73 71  , zStmt);.    sq
22185 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
22186 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
22187 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22188 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45  OMIT_AUTOINCREME
22189 4e 54 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  NT.    /* Check 
2218a 74 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65 65  to see if we nee
2218b 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 73  d to create an s
2218c 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74  qlite_sequence t
2218d 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a 2a 20  able for.    ** 
2218e 6b 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66  keeping track of
2218f 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 6b   autoincrement k
22190 65 79 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  eys..    */.    
22191 69 66 28 20 70 2d 3e 74 61 62 46 6c 61 67 73 20  if( p->tabFlags 
22192 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65  & TF_Autoincreme
22193 6e 74 20 29 7b 0a 20 20 20 20 20 20 44 62 20 2a  nt ){.      Db *
22194 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
22195 44 62 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70  Db];.      if( p
22196 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65  Db->pSchema->pSe
22197 71 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20  qTab==0 ){.     
22198 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
22199 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
2219a 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20          "CREATE 
2219b 54 41 42 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f  TABLE %Q.sqlite_
2219c 73 65 71 75 65 6e 63 65 28 6e 61 6d 65 2c 73 65  sequence(name,se
2219d 71 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  q)",.          p
2219e 44 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20  Db->zName.      
2219f 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20    );.      }.   
221a0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   }.#endif..    /
221a1 2a 20 52 65 70 61 72 73 65 20 65 76 65 72 79 74  * Reparse everyt
221a2 68 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20 6f  hing to update o
221a3 75 72 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61  ur internal data
221a4 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 20   structures */. 
221a5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
221a6 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65  dOp4(v, OP_Parse
221a7 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20  Schema, iDb, 0, 
221a8 30 2c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  0,.        sqlit
221a9 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74  e3MPrintf(db, "t
221aa 62 6c 5f 6e 61 6d 65 3d 27 25 71 27 22 2c 70 2d  bl_name='%q'",p-
221ab 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41  >zName), P4_DYNA
221ac 4d 49 43 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a  MIC);.  }...  /*
221ad 20 41 64 64 20 74 68 65 20 74 61 62 6c 65 20 74   Add the table t
221ae 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  o the in-memory 
221af 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
221b0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  f the database..
221b1 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69    */.  if( db->i
221b2 6e 69 74 2e 62 75 73 79 20 26 26 20 70 50 61 72  nit.busy && pPar
221b3 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20  se->nErr==0 ){. 
221b4 20 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a     Table *pOld;.
221b5 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68      Schema *pSch
221b6 65 6d 61 20 3d 20 70 2d 3e 70 53 63 68 65 6d 61  ema = p->pSchema
221b7 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c  ;.    pOld = sql
221b8 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26  ite3HashInsert(&
221b9 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
221ba 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20  , p->zName,.    
221bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221bc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
221bd 53 74 72 6c 65 6e 33 30 28 70 2d 3e 7a 4e 61 6d  Strlen30(p->zNam
221be 65 29 2c 70 29 3b 0a 20 20 20 20 69 66 28 20 70  e),p);.    if( p
221bf 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73  Old ){.      ass
221c0 65 72 74 28 20 70 3d 3d 70 4f 6c 64 20 29 3b 20  ert( p==pOld ); 
221c1 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20   /* Malloc must 
221c2 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69  have failed insi
221c3 64 65 20 48 61 73 68 49 6e 73 65 72 74 28 29 20  de HashInsert() 
221c4 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  */.      db->mal
221c5 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
221c6 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
221c7 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70   }.    pParse->p
221c8 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20  NewTable = 0;.  
221c9 20 20 64 62 2d 3e 6e 54 61 62 6c 65 2b 2b 3b 0a    db->nTable++;.
221ca 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
221cb 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
221cc 61 6e 67 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20  anges;..#ifndef 
221cd 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45  SQLITE_OMIT_ALTE
221ce 52 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 21  RTABLE.    if( !
221cf 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  p->pSelect ){.  
221d0 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
221d1 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63  zName = (const c
221d2 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e 73 4e  har *)pParse->sN
221d3 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20  ameToken.z;.    
221d4 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 20    int nName;.   
221d5 20 20 20 61 73 73 65 72 74 28 20 21 70 53 65 6c     assert( !pSel
221d6 65 63 74 20 26 26 20 70 43 6f 6e 73 20 26 26 20  ect && pCons && 
221d7 70 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 69 66  pEnd );.      if
221d8 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 7b  ( pCons->z==0 ){
221d9 0a 20 20 20 20 20 20 20 20 70 43 6f 6e 73 20 3d  .        pCons =
221da 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20   pEnd;.      }. 
221db 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20 28 69 6e       nName = (in
221dc 74 29 28 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  t)((const char *
221dd 29 70 43 6f 6e 73 2d 3e 7a 20 2d 20 7a 4e 61 6d  )pCons->z - zNam
221de 65 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64  e);.      p->add
221df 43 6f 6c 4f 66 66 73 65 74 20 3d 20 31 33 20 2b  ColOffset = 13 +
221e0 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 72   sqlite3Utf8Char
221e1 4c 65 6e 28 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  Len(zName, nName
221e2 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
221e3 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53    }.}..#ifndef S
221e4 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
221e5 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  /*.** The parser
221e6 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
221e7 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ine in order to 
221e8 63 72 65 61 74 65 20 61 20 6e 65 77 20 56 49 45  create a new VIE
221e9 57 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  W.*/.SQLITE_PRIV
221ea 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
221eb 43 72 65 61 74 65 56 69 65 77 28 0a 20 20 50 61  CreateView(.  Pa
221ec 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
221ed 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
221ee 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b  context */.  Tok
221ef 65 6e 20 2a 70 42 65 67 69 6e 2c 20 20 20 20 20  en *pBegin,     
221f0 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20 74 6f  /* The CREATE to
221f1 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20  ken that begins 
221f2 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  the statement */
221f3 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31  .  Token *pName1
221f4 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b  ,     /* The tok
221f5 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  en that holds th
221f6 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 69  e name of the vi
221f7 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  ew */.  Token *p
221f8 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20 54 68  Name2,     /* Th
221f9 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c  e token that hol
221fa 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ds the name of t
221fb 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 53 65 6c  he view */.  Sel
221fc 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
221fd 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 74  /* A SELECT stat
221fe 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c 20  ement that will 
221ff 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 76  become the new v
22200 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54  iew */.  int isT
22201 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  emp,        /* T
22202 52 55 45 20 66 6f 72 20 61 20 54 45 4d 50 4f 52  RUE for a TEMPOR
22203 41 52 59 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e  ARY view */.  in
22204 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20 20  t noErr         
22205 20 2f 2a 20 53 75 70 70 72 65 73 73 20 65 72 72   /* Suppress err
22206 6f 72 20 6d 65 73 73 61 67 65 73 20 69 66 20 56  or messages if V
22207 49 45 57 20 61 6c 72 65 61 64 79 20 65 78 69 73  IEW already exis
22208 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65  ts */.){.  Table
22209 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20   *p;.  int n;.  
2220a 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2220b 68 61 72 20 2a 7a 3b 0a 20 20 54 6f 6b 65 6e 20  har *z;.  Token 
2220c 73 45 6e 64 3b 0a 20 20 44 62 46 69 78 65 72 20  sEnd;.  DbFixer 
2220d 73 46 69 78 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70  sFix;.  Token *p
2220e 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b  Name;.  int iDb;
2220f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
22210 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
22211 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  if( pParse->nVar
22212 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  >0 ){.    sqlite
22213 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
22214 2c 20 22 70 61 72 61 6d 65 74 65 72 73 20 61 72  , "parameters ar
22215 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e  e not allowed in
22216 20 76 69 65 77 73 22 29 3b 0a 20 20 20 20 73 71   views");.    sq
22217 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
22218 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  e(db, pSelect);.
22219 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2221a 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61    sqlite3StartTa
2221b 62 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ble(pParse, pNam
2221c 65 31 2c 20 70 4e 61 6d 65 32 2c 20 69 73 54 65  e1, pName2, isTe
2221d 6d 70 2c 20 31 2c 20 30 2c 20 6e 6f 45 72 72 29  mp, 1, 0, noErr)
2221e 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e  ;.  p = pParse->
2221f 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28  pNewTable;.  if(
22220 20 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d   p==0 || pParse-
22221 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c  >nErr ){.    sql
22222 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
22223 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20  (db, pSelect);. 
22224 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
22225 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
22226 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ame(pParse, pNam
22227 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61  e1, pName2, &pNa
22228 6d 65 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  me);.  iDb = sql
22229 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2222a 78 28 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61  x(db, p->pSchema
2222b 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
2222c 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70  FixInit(&sFix, p
2222d 50 61 72 73 65 2c 20 69 44 62 2c 20 22 76 69 65  Parse, iDb, "vie
2222e 77 22 2c 20 70 4e 61 6d 65 29 0a 20 20 20 20 26  w", pName).    &
2222f 26 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65  & sqlite3FixSele
22230 63 74 28 26 73 46 69 78 2c 20 70 53 65 6c 65 63  ct(&sFix, pSelec
22231 74 29 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69  t).  ){.    sqli
22232 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
22233 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  db, pSelect);.  
22234 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
22235 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20   /* Make a copy 
22236 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 53 45  of the entire SE
22237 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
22238 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  hat defines the 
22239 76 69 65 77 2e 0a 20 20 2a 2a 20 54 68 69 73 20  view..  ** This 
2223a 77 69 6c 6c 20 66 6f 72 63 65 20 61 6c 6c 20 74  will force all t
2223b 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20  he Expr.token.z 
2223c 76 61 6c 75 65 73 20 74 6f 20 62 65 20 64 79 6e  values to be dyn
2223d 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 61 6c  amically.  ** al
2223e 6c 6f 63 61 74 65 64 20 72 61 74 68 65 72 20 74  located rather t
2223f 68 61 6e 20 70 6f 69 6e 74 20 74 6f 20 74 68 65  han point to the
22240 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 2d 20   input string - 
22241 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74  which means that
22242 0a 20 20 2a 2a 20 74 68 65 79 20 77 69 6c 6c 20  .  ** they will 
22243 70 65 72 73 69 73 74 20 61 66 74 65 72 20 74 68  persist after th
22244 65 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65  e current sqlite
22245 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 72 65  3_exec() call re
22246 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 2d  turns..  */.  p-
22247 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  >pSelect = sqlit
22248 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
22249 70 53 65 6c 65 63 74 2c 20 45 58 50 52 44 55 50  pSelect, EXPRDUP
2224a 5f 52 45 44 55 43 45 29 3b 0a 20 20 73 71 6c 69  _REDUCE);.  sqli
2224b 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2224c 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  db, pSelect);.  
2224d 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2224e 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  iled ){.    retu
2224f 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64  rn;.  }.  if( !d
22250 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
22251 20 20 20 20 73 71 6c 69 74 65 33 56 69 65 77 47      sqlite3ViewG
22252 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  etColumnNames(pP
22253 61 72 73 65 2c 20 70 29 3b 0a 20 20 7d 0a 0a 20  arse, p);.  }.. 
22254 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 65   /* Locate the e
22255 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  nd of the CREATE
22256 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e 74 2e   VIEW statement.
22257 20 20 4d 61 6b 65 20 73 45 6e 64 20 70 6f 69 6e    Make sEnd poin
22258 74 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 65 6e  t to.  ** the en
22259 64 2e 0a 20 20 2a 2f 0a 20 20 73 45 6e 64 20 3d  d..  */.  sEnd =
2225a 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
2225b 6b 65 6e 3b 0a 20 20 69 66 28 20 73 45 6e 64 2e  ken;.  if( sEnd.
2225c 7a 5b 30 5d 21 3d 30 20 26 26 20 73 45 6e 64 2e  z[0]!=0 && sEnd.
2225d 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b 0a 20 20 20  z[0]!=';' ){.   
2225e 20 73 45 6e 64 2e 7a 20 2b 3d 20 73 45 6e 64 2e   sEnd.z += sEnd.
2225f 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e 64 2e 6e 20  n;.  }.  sEnd.n 
22260 3d 20 30 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29  = 0;.  n = (int)
22261 28 73 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e  (sEnd.z - pBegin
22262 2d 3e 7a 29 3b 0a 20 20 7a 20 3d 20 28 63 6f 6e  ->z);.  z = (con
22263 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
22264 2a 29 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77  *)pBegin->z;.  w
22265 68 69 6c 65 28 20 6e 3e 30 20 26 26 20 28 7a 5b  hile( n>0 && (z[
22266 6e 2d 31 5d 3d 3d 27 3b 27 20 7c 7c 20 73 71 6c  n-1]==';' || sql
22267 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b 6e 2d  ite3Isspace(z[n-
22268 31 5d 29 29 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20  1])) ){ n--; }. 
22269 20 73 45 6e 64 2e 7a 20 3d 20 26 7a 5b 6e 2d 31   sEnd.z = &z[n-1
2226a 5d 3b 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 31 3b  ];.  sEnd.n = 1;
2226b 0a 0a 20 20 2f 2a 20 55 73 65 20 73 71 6c 69 74  ..  /* Use sqlit
2226c 65 33 45 6e 64 54 61 62 6c 65 28 29 20 74 6f 20  e3EndTable() to 
2226d 61 64 64 20 74 68 65 20 76 69 65 77 20 74 6f 20  add the view to 
2226e 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45  the SQLITE_MASTE
2226f 52 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c  R table */.  sql
22270 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61  ite3EndTable(pPa
22271 72 73 65 2c 20 30 2c 20 26 73 45 6e 64 2c 20 30  rse, 0, &sEnd, 0
22272 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23  );.  return;.}.#
22273 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
22274 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 23 69  OMIT_VIEW */..#i
22275 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
22276 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
22277 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
22278 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
22279 45 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62  E)./*.** The Tab
2227a 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 54 61  le structure pTa
2227b 62 6c 65 20 69 73 20 72 65 61 6c 6c 79 20 61 20  ble is really a 
2227c 56 49 45 57 2e 20 20 46 69 6c 6c 20 69 6e 20 74  VIEW.  Fill in t
2227d 68 65 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20 74  he names of.** t
2227e 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  he columns of th
2227f 65 20 76 69 65 77 20 69 6e 20 74 68 65 20 70 54  e view in the pT
22280 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 2e 20  able structure. 
22281 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
22282 65 72 0a 2a 2a 20 6f 66 20 65 72 72 6f 72 73 2e  er.** of errors.
22283 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 69 73    If an error is
22284 20 73 65 65 6e 20 6c 65 61 76 65 20 61 6e 20 65   seen leave an e
22285 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20  rror message in 
22286 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e  pParse->zErrMsg.
22287 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22288 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69  TE int sqlite3Vi
22289 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
2228a 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2228b 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a  Table *pTable){.
2228c 20 20 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62    Table *pSelTab
2228d 3b 20 20 20 2f 2a 20 41 20 66 61 6b 65 20 74 61  ;   /* A fake ta
2228e 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77  ble from which w
2228f 65 20 67 65 74 20 74 68 65 20 72 65 73 75 6c 74  e get the result
22290 20 73 65 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74   set */.  Select
22291 20 2a 70 53 65 6c 3b 20 20 20 20 20 2f 2a 20 43   *pSel;     /* C
22292 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43  opy of the SELEC
22293 54 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  T that implement
22294 73 20 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20  s the view */.  
22295 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 20 20 20  int nErr = 0;   
22296 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
22297 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65  rrors encountere
22298 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20  d */.  int n;   
22299 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
2229a 6f 72 61 72 69 6c 79 20 68 6f 6c 64 73 20 74 68  orarily holds th
2229b 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 75 72 73  e number of curs
2229c 6f 72 73 20 61 73 73 69 67 6e 65 64 20 2a 2f 0a  ors assigned */.
2229d 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2229e 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a 20  pParse->db;  /* 
2229f 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
222a0 69 6f 6e 20 66 6f 72 20 6d 61 6c 6c 6f 63 20 65  ion for malloc e
222a1 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 28  rrors */.  int (
222a2 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e  *xAuth)(void*,in
222a3 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  t,const char*,co
222a4 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
222a5 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
222a6 2a 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  *);..  assert( p
222a7 54 61 62 6c 65 20 29 3b 0a 0a 23 69 66 6e 64 65  Table );..#ifnde
222a8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
222a9 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
222aa 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c   sqlite3VtabCall
222ab 43 6f 6e 6e 65 63 74 28 70 50 61 72 73 65 2c 20  Connect(pParse, 
222ac 70 54 61 62 6c 65 29 20 29 7b 0a 20 20 20 20 72  pTable) ){.    r
222ad 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
222ae 4f 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73  OR;.  }.  if( Is
222af 56 69 72 74 75 61 6c 28 70 54 61 62 6c 65 29 20  Virtual(pTable) 
222b0 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64  ) return 0;.#end
222b1 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
222b2 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 2f  TE_OMIT_VIEW.  /
222b3 2a 20 41 20 70 6f 73 69 74 69 76 65 20 6e 43 6f  * A positive nCo
222b4 6c 20 6d 65 61 6e 73 20 74 68 65 20 63 6f 6c 75  l means the colu
222b5 6d 6e 73 20 6e 61 6d 65 73 20 66 6f 72 20 74 68  mns names for th
222b6 69 73 20 76 69 65 77 20 61 72 65 0a 20 20 2a 2a  is view are.  **
222b7 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 6e 2e 0a   already known..
222b8 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c    */.  if( pTabl
222b9 65 2d 3e 6e 43 6f 6c 3e 30 20 29 20 72 65 74 75  e->nCol>0 ) retu
222ba 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 41 20 6e 65  rn 0;..  /* A ne
222bb 67 61 74 69 76 65 20 6e 43 6f 6c 20 69 73 20 61  gative nCol is a
222bc 20 73 70 65 63 69 61 6c 20 6d 61 72 6b 65 72 20   special marker 
222bd 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 77 65 20  meaning that we 
222be 61 72 65 20 63 75 72 72 65 6e 74 6c 79 0a 20 20  are currently.  
222bf 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 63 6f 6d  ** trying to com
222c0 70 75 74 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  pute the column 
222c1 6e 61 6d 65 73 2e 20 20 49 66 20 77 65 20 65 6e  names.  If we en
222c2 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
222c3 20 77 69 74 68 0a 20 20 2a 2a 20 61 20 6e 65 67   with.  ** a neg
222c4 61 74 69 76 65 20 6e 43 6f 6c 2c 20 69 74 20 6d  ative nCol, it m
222c5 65 61 6e 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65  eans two or more
222c6 20 76 69 65 77 73 20 66 6f 72 6d 20 61 20 6c 6f   views form a lo
222c7 6f 70 2c 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20  op, like this:. 
222c8 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 43 52 45   **.  **     CRE
222c9 41 54 45 20 56 49 45 57 20 6f 6e 65 20 41 53 20  ATE VIEW one AS 
222ca 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 77  SELECT * FROM tw
222cb 6f 3b 0a 20 20 2a 2a 20 20 20 20 20 43 52 45 41  o;.  **     CREA
222cc 54 45 20 56 49 45 57 20 74 77 6f 20 41 53 20 53  TE VIEW two AS S
222cd 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6f 6e 65  ELECT * FROM one
222ce 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 63 74 75  ;.  **.  ** Actu
222cf 61 6c 6c 79 2c 20 74 68 69 73 20 65 72 72 6f 72  ally, this error
222d0 20 69 73 20 63 61 75 67 68 74 20 70 72 65 76 69   is caught previ
222d1 6f 75 73 6c 79 20 61 6e 64 20 73 6f 20 74 68 65  ously and so the
222d2 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74 0a   following test.
222d3 20 20 2a 2a 20 73 68 6f 75 6c 64 20 61 6c 77 61    ** should alwa
222d4 79 73 20 66 61 69 6c 2e 20 20 42 75 74 20 77 65  ys fail.  But we
222d5 20 77 69 6c 6c 20 6c 65 61 76 65 20 69 74 20 69   will leave it i
222d6 6e 20 70 6c 61 63 65 20 6a 75 73 74 20 74 6f 20  n place just to 
222d7 62 65 20 73 61 66 65 2e 0a 20 20 2a 2f 0a 20 20  be safe..  */.  
222d8 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  if( pTable->nCol
222d9 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  <0 ){.    sqlite
222da 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
222db 2c 20 22 76 69 65 77 20 25 73 20 69 73 20 63 69  , "view %s is ci
222dc 72 63 75 6c 61 72 6c 79 20 64 65 66 69 6e 65 64  rcularly defined
222dd 22 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65  ", pTable->zName
222de 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
222df 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
222e0 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 3d 30 20 29  Table->nCol>=0 )
222e1 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 67 65  ;..  /* If we ge
222e2 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d  t this far, it m
222e3 65 61 6e 73 20 77 65 20 6e 65 65 64 20 74 6f 20  eans we need to 
222e4 63 6f 6d 70 75 74 65 20 74 68 65 20 74 61 62 6c  compute the tabl
222e5 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2a 20 4e 6f  e names..  ** No
222e6 74 65 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c  te that the call
222e7 20 74 6f 20 73 71 6c 69 74 65 33 52 65 73 75 6c   to sqlite3Resul
222e8 74 53 65 74 4f 66 53 65 6c 65 63 74 28 29 20 77  tSetOfSelect() w
222e9 69 6c 6c 20 65 78 70 61 6e 64 20 61 6e 79 0a 20  ill expand any. 
222ea 20 2a 2a 20 22 2a 22 20 65 6c 65 6d 65 6e 74 73   ** "*" elements
222eb 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20   in the results 
222ec 73 65 74 20 6f 66 20 74 68 65 20 76 69 65 77 20  set of the view 
222ed 61 6e 64 20 77 69 6c 6c 20 61 73 73 69 67 6e 20  and will assign 
222ee 63 75 72 73 6f 72 73 0a 20 20 2a 2a 20 74 6f 20  cursors.  ** to 
222ef 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  the elements of 
222f0 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
222f1 20 20 42 75 74 20 77 65 20 64 6f 20 6e 6f 74 20    But we do not 
222f2 77 61 6e 74 20 74 68 65 73 65 20 63 68 61 6e 67  want these chang
222f3 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 70 65  es.  ** to be pe
222f4 72 6d 61 6e 65 6e 74 2e 20 20 53 6f 20 74 68 65  rmanent.  So the
222f5 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 69 73 20   computation is 
222f6 64 6f 6e 65 20 6f 6e 20 61 20 63 6f 70 79 20 6f  done on a copy o
222f7 66 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 2a  f the SELECT.  *
222f8 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  * statement that
222f9 20 64 65 66 69 6e 65 73 20 74 68 65 20 76 69 65   defines the vie
222fa 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  w..  */.  assert
222fb 28 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63  ( pTable->pSelec
222fc 74 20 29 3b 0a 20 20 70 53 65 6c 20 3d 20 73 71  t );.  pSel = sq
222fd 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
222fe 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65  b, pTable->pSele
222ff 63 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53  ct, 0);.  if( pS
22300 65 6c 20 29 7b 0a 20 20 20 20 75 38 20 65 6e 61  el ){.    u8 ena
22301 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 64  bleLookaside = d
22302 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
22303 61 62 6c 65 64 3b 0a 20 20 20 20 6e 20 3d 20 70  abled;.    n = p
22304 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20  Parse->nTab;.   
22305 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
22306 73 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61  ssignCursors(pPa
22307 72 73 65 2c 20 70 53 65 6c 2d 3e 70 53 72 63 29  rse, pSel->pSrc)
22308 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43  ;.    pTable->nC
22309 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 64 62 2d  ol = -1;.    db-
2230a 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
2230b 6c 65 64 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66  led = 0;.#ifndef
2230c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2230d 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 78  HORIZATION.    x
2230e 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74 68  Auth = db->xAuth
2230f 3b 0a 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20  ;.    db->xAuth 
22310 3d 20 30 3b 0a 20 20 20 20 70 53 65 6c 54 61 62  = 0;.    pSelTab
22311 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74   = sqlite3Result
22312 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72  SetOfSelect(pPar
22313 73 65 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 64  se, pSel);.    d
22314 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68  b->xAuth = xAuth
22315 3b 0a 23 65 6c 73 65 0a 20 20 20 20 70 53 65 6c  ;.#else.    pSel
22316 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73  Tab = sqlite3Res
22317 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70  ultSetOfSelect(p
22318 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a 23 65  Parse, pSel);.#e
22319 6e 64 69 66 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f  ndif.    db->loo
2231a 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
2231b 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64  = enableLookasid
2231c 65 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  e;.    pParse->n
2231d 54 61 62 20 3d 20 6e 3b 0a 20 20 20 20 69 66 28  Tab = n;.    if(
2231e 20 70 53 65 6c 54 61 62 20 29 7b 0a 20 20 20 20   pSelTab ){.    
2231f 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
22320 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20  ->aCol==0 );.   
22321 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20     pTable->nCol 
22322 3d 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b  = pSelTab->nCol;
22323 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 61  .      pTable->a
22324 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 61  Col = pSelTab->a
22325 43 6f 6c 3b 0a 20 20 20 20 20 20 70 53 65 6c 54  Col;.      pSelT
22326 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20  ab->nCol = 0;.  
22327 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f      pSelTab->aCo
22328 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  l = 0;.      sql
22329 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
2232a 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20  pSelTab);.      
2232b 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d  pTable->pSchema-
2232c 3e 66 6c 61 67 73 20 7c 3d 20 44 42 5f 55 6e 72  >flags |= DB_Unr
2232d 65 73 65 74 56 69 65 77 73 3b 0a 20 20 20 20 7d  esetViews;.    }
2232e 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 62  else{.      pTab
2232f 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20  le->nCol = 0;.  
22330 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20      nErr++;.    
22331 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  }.    sqlite3Sel
22332 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53  ectDelete(db, pS
22333 65 6c 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  el);.  } else {.
22334 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a      nErr++;.  }.
22335 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
22336 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 20 20  _OMIT_VIEW */.  
22337 72 65 74 75 72 6e 20 6e 45 72 72 3b 20 20 0a 7d  return nErr;  .}
22338 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
22339 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2233a 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65  VIEW) || !define
2233b 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
2233c 52 54 55 41 4c 54 41 42 4c 45 29 20 2a 2f 0a 0a  RTUALTABLE) */..
2233d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2233e 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a 2a 2a 20 43  MIT_VIEW./*.** C
2233f 6c 65 61 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20  lear the column 
22340 6e 61 6d 65 73 20 66 72 6f 6d 20 65 76 65 72 79  names from every
22341 20 56 49 45 57 20 69 6e 20 64 61 74 61 62 61 73   VIEW in databas
22342 65 20 69 64 78 2e 0a 2a 2f 0a 73 74 61 74 69 63  e idx..*/.static
22343 20 76 6f 69 64 20 73 71 6c 69 74 65 56 69 65 77   void sqliteView
22344 52 65 73 65 74 41 6c 6c 28 73 71 6c 69 74 65 33  ResetAll(sqlite3
22345 20 2a 64 62 2c 20 69 6e 74 20 69 64 78 29 7b 0a   *db, int idx){.
22346 20 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20    HashElem *i;. 
22347 20 69 66 28 20 21 44 62 48 61 73 50 72 6f 70 65   if( !DbHasPrope
22348 72 74 79 28 64 62 2c 20 69 64 78 2c 20 44 42 5f  rty(db, idx, DB_
22349 55 6e 72 65 73 65 74 56 69 65 77 73 29 20 29 20  UnresetViews) ) 
2234a 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
2234b 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
2234c 26 64 62 2d 3e 61 44 62 5b 69 64 78 5d 2e 70 53  &db->aDb[idx].pS
2234d 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b  chema->tblHash);
2234e 20 69 3b 69 3d 73 71 6c 69 74 65 48 61 73 68 4e   i;i=sqliteHashN
2234f 65 78 74 28 69 29 29 7b 0a 20 20 20 20 54 61 62  ext(i)){.    Tab
22350 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74  le *pTab = sqlit
22351 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20  eHashData(i);.  
22352 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c    if( pTab->pSel
22353 65 63 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ect ){.      sql
22354 69 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61  iteResetColumnNa
22355 6d 65 73 28 70 54 61 62 29 3b 0a 20 20 20 20 7d  mes(pTab);.    }
22356 0a 20 20 7d 0a 20 20 44 62 43 6c 65 61 72 50 72  .  }.  DbClearPr
22357 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78 2c 20  operty(db, idx, 
22358 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 29  DB_UnresetViews)
22359 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  ;.}.#else.# defi
2235a 6e 65 20 73 71 6c 69 74 65 56 69 65 77 52 65 73  ne sqliteViewRes
2235b 65 74 41 6c 6c 28 41 2c 42 29 0a 23 65 6e 64 69  etAll(A,B).#endi
2235c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2235d 5f 56 49 45 57 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  _VIEW */../*.** 
2235e 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
2235f 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 56   called by the V
22360 44 42 45 20 74 6f 20 61 64 6a 75 73 74 20 74 68  DBE to adjust th
22361 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  e internal schem
22362 61 0a 2a 2a 20 75 73 65 64 20 62 79 20 53 51 4c  a.** used by SQL
22363 69 74 65 20 77 68 65 6e 20 74 68 65 20 62 74 72  ite when the btr
22364 65 65 20 6c 61 79 65 72 20 6d 6f 76 65 73 20 61  ee layer moves a
22365 20 74 61 62 6c 65 20 72 6f 6f 74 20 70 61 67 65   table root page
22366 2e 20 54 68 65 0a 2a 2a 20 72 6f 6f 74 2d 70 61  . The.** root-pa
22367 67 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72  ge of a table or
22368 20 69 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61   index in databa
22369 73 65 20 69 44 62 20 68 61 73 20 63 68 61 6e 67  se iDb has chang
2236a 65 64 20 66 72 6f 6d 20 69 46 72 6f 6d 0a 2a 2a  ed from iFrom.**
2236b 20 74 6f 20 69 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54   to iTo..**.** T
2236c 69 63 6b 65 74 20 23 31 37 32 38 3a 20 20 54 68  icket #1728:  Th
2236d 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65 20 6d  e symbol table m
2236e 69 67 68 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61  ight still conta
2236f 69 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  in information.*
22370 2a 20 6f 6e 20 74 61 62 6c 65 73 20 61 6e 64 2f  * on tables and/
22371 6f 72 20 69 6e 64 69 63 65 73 20 74 68 61 74 20  or indices that 
22372 61 72 65 20 74 68 65 20 70 72 6f 63 65 73 73 20  are the process 
22373 6f 66 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  of being deleted
22374 2e 0a 2a 2a 20 49 66 20 79 6f 75 20 61 72 65 20  ..** If you are 
22375 75 6e 6c 75 63 6b 79 2c 20 6f 6e 65 20 6f 66 20  unlucky, one of 
22376 74 68 6f 73 65 20 64 65 6c 65 74 65 64 20 69 6e  those deleted in
22377 64 69 63 65 73 20 6f 72 20 74 61 62 6c 65 73 20  dices or tables 
22378 6d 69 67 68 74 0a 2a 2a 20 68 61 76 65 20 74 68  might.** have th
22379 65 20 73 61 6d 65 20 72 6f 6f 74 70 61 67 65 20  e same rootpage 
2237a 6e 75 6d 62 65 72 20 61 73 20 74 68 65 20 72 65  number as the re
2237b 61 6c 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  al table or inde
2237c 78 20 74 68 61 74 20 69 73 0a 2a 2a 20 62 65 69  x that is.** bei
2237d 6e 67 20 6d 6f 76 65 64 2e 20 20 53 6f 20 77 65  ng moved.  So we
2237e 20 63 61 6e 6e 6f 74 20 73 74 6f 70 20 73 65 61   cannot stop sea
2237f 72 63 68 69 6e 67 20 61 66 74 65 72 20 74 68 65  rching after the
22380 20 66 69 72 73 74 20 6d 61 74 63 68 20 0a 2a 2a   first match .**
22381 20 62 65 63 61 75 73 65 20 74 68 65 20 66 69 72   because the fir
22382 73 74 20 6d 61 74 63 68 20 6d 69 67 68 74 20 62  st match might b
22383 65 20 66 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  e for one of the
22384 20 64 65 6c 65 74 65 64 20 69 6e 64 69 63 65 73   deleted indices
22385 0a 2a 2a 20 6f 72 20 74 61 62 6c 65 73 20 61 6e  .** or tables an
22386 64 20 6e 6f 74 20 74 68 65 20 74 61 62 6c 65 2f  d not the table/
22387 69 6e 64 65 78 20 74 68 61 74 20 69 73 20 61 63  index that is ac
22388 74 75 61 6c 6c 79 20 62 65 69 6e 67 20 6d 6f 76  tually being mov
22389 65 64 2e 0a 2a 2a 20 57 65 20 6d 75 73 74 20 63  ed..** We must c
2238a 6f 6e 74 69 6e 75 65 20 6c 6f 6f 70 69 6e 67 20  ontinue looping 
2238b 75 6e 74 69 6c 20 61 6c 6c 20 74 61 62 6c 65 73  until all tables
2238c 20 61 6e 64 20 69 6e 64 69 63 65 73 20 77 69 74   and indices wit
2238d 68 0a 2a 2a 20 72 6f 6f 74 70 61 67 65 3d 3d 69  h.** rootpage==i
2238e 46 72 6f 6d 20 68 61 76 65 20 62 65 65 6e 20 63  From have been c
2238f 6f 6e 76 65 72 74 65 64 20 74 6f 20 68 61 76 65  onverted to have
22390 20 61 20 72 6f 6f 74 70 61 67 65 20 6f 66 20 69   a rootpage of i
22391 54 6f 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74  To.** in order t
22392 6f 20 62 65 20 63 65 72 74 61 69 6e 20 74 68 61  o be certain tha
22393 74 20 77 65 20 67 6f 74 20 74 68 65 20 72 69 67  t we got the rig
22394 68 74 20 6f 6e 65 2e 0a 2a 2f 0a 23 69 66 6e 64  ht one..*/.#ifnd
22395 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
22396 55 54 4f 56 41 43 55 55 4d 0a 53 51 4c 49 54 45  UTOVACUUM.SQLITE
22397 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
22398 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76  lite3RootPageMov
22399 65 64 28 44 62 20 2a 70 44 62 2c 20 69 6e 74 20  ed(Db *pDb, int 
2239a 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b  iFrom, int iTo){
2239b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 45 6c  .  HashElem *pEl
2239c 65 6d 3b 0a 20 20 48 61 73 68 20 2a 70 48 61 73  em;.  Hash *pHas
2239d 68 3b 0a 0a 20 20 70 48 61 73 68 20 3d 20 26 70  h;..  pHash = &p
2239e 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c  Db->pSchema->tbl
2239f 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 45 6c 65  Hash;.  for(pEle
223a0 6d 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  m=sqliteHashFirs
223a1 74 28 70 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b  t(pHash); pElem;
223a2 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73   pElem=sqliteHas
223a3 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20  hNext(pElem)){. 
223a4 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
223a5 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28   sqliteHashData(
223a6 70 45 6c 65 6d 29 3b 0a 20 20 20 20 69 66 28 20  pElem);.    if( 
223a7 70 54 61 62 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f  pTab->tnum==iFro
223a8 6d 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d  m ){.      pTab-
223a9 3e 74 6e 75 6d 20 3d 20 69 54 6f 3b 0a 20 20 20  >tnum = iTo;.   
223aa 20 7d 0a 20 20 7d 0a 20 20 70 48 61 73 68 20 3d   }.  }.  pHash =
223ab 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e   &pDb->pSchema->
223ac 69 64 78 48 61 73 68 3b 0a 20 20 66 6f 72 28 70  idxHash;.  for(p
223ad 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 46  Elem=sqliteHashF
223ae 69 72 73 74 28 70 48 61 73 68 29 3b 20 70 45 6c  irst(pHash); pEl
223af 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65  em; pElem=sqlite
223b0 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 29  HashNext(pElem))
223b1 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  {.    Index *pId
223b2 78 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61  x = sqliteHashDa
223b3 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20 69  ta(pElem);.    i
223b4 66 28 20 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d 69  f( pIdx->tnum==i
223b5 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 70 49  From ){.      pI
223b6 64 78 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f 3b 0a  dx->tnum = iTo;.
223b7 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64      }.  }.}.#end
223b8 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  if../*.** Write 
223b9 63 6f 64 65 20 74 6f 20 65 72 61 73 65 20 74 68  code to erase th
223ba 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f  e table with roo
223bb 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 66 72  t-page iTable fr
223bc 6f 6d 20 64 61 74 61 62 61 73 65 20 69 44 62 2e  om database iDb.
223bd 0a 2a 2a 20 41 6c 73 6f 20 77 72 69 74 65 20 63  .** Also write c
223be 6f 64 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  ode to modify th
223bf 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
223c0 74 61 62 6c 65 20 61 6e 64 20 69 6e 74 65 72 6e  table and intern
223c1 61 6c 20 73 63 68 65 6d 61 0a 2a 2a 20 69 66 20  al schema.** if 
223c2 61 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 61  a root-page of a
223c3 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 69 73 20  nother table is 
223c4 6d 6f 76 65 64 20 62 79 20 74 68 65 20 62 74 72  moved by the btr
223c5 65 65 2d 6c 61 79 65 72 20 77 68 69 6c 73 74 0a  ee-layer whilst.
223c6 2a 2a 20 65 72 61 73 69 6e 67 20 69 54 61 62 6c  ** erasing iTabl
223c7 65 20 28 74 68 69 73 20 63 61 6e 20 68 61 70 70  e (this can happ
223c8 65 6e 20 77 69 74 68 20 61 6e 20 61 75 74 6f 2d  en with an auto-
223c9 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 29  vacuum database)
223ca 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69  ..*/ .static voi
223cb 64 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67  d destroyRootPag
223cc 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
223cd 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74   int iTable, int
223ce 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76   iDb){.  Vdbe *v
223cf 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
223d0 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74  e(pParse);.  int
223d1 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
223d2 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
223d3 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
223d4 64 4f 70 33 28 76 2c 20 4f 50 5f 44 65 73 74 72  dOp3(v, OP_Destr
223d5 6f 79 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c 20  oy, iTable, r1, 
223d6 69 44 62 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  iDb);.#ifndef SQ
223d7 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
223d8 43 55 55 4d 0a 20 20 2f 2a 20 4f 50 5f 44 65 73  CUUM.  /* OP_Des
223d9 74 72 6f 79 20 73 74 6f 72 65 73 20 61 6e 20 69  troy stores an i
223da 6e 20 69 6e 74 65 67 65 72 20 72 31 2e 20 49 66  n integer r1. If
223db 20 74 68 69 73 20 69 6e 74 65 67 65 72 0a 20 20   this integer.  
223dc 2a 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  ** is non-zero, 
223dd 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 72  then it is the r
223de 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
223df 6f 66 20 61 20 74 61 62 6c 65 20 6d 6f 76 65 64  of a table moved
223e0 20 74 6f 0a 20 20 2a 2a 20 6c 6f 63 61 74 69 6f   to.  ** locatio
223e1 6e 20 69 54 61 62 6c 65 2e 20 54 68 65 20 66 6f  n iTable. The fo
223e2 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6d 6f 64  llowing code mod
223e3 69 66 69 65 73 20 74 68 65 20 73 71 6c 69 74 65  ifies the sqlite
223e4 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74 6f  _master table to
223e5 0a 20 20 2a 2a 20 72 65 66 6c 65 63 74 20 74 68  .  ** reflect th
223e6 69 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  is..  **.  ** Th
223e7 65 20 22 23 4e 4e 4e 22 20 69 6e 20 74 68 65 20  e "#NNN" in the 
223e8 53 51 4c 20 69 73 20 61 20 73 70 65 63 69 61 6c  SQL is a special
223e9 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 6d   constant that m
223ea 65 61 6e 73 20 77 68 61 74 65 76 65 72 20 76 61  eans whatever va
223eb 6c 75 65 0a 20 20 2a 2a 20 69 73 20 69 6e 20 72  lue.  ** is in r
223ec 65 67 69 73 74 65 72 20 4e 4e 4e 2e 20 20 53 65  egister NNN.  Se
223ed 65 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  e sqlite3Registe
223ee 72 45 78 70 72 28 29 2e 0a 20 20 2a 2f 0a 20 20  rExpr()..  */.  
223ef 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
223f0 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  se(pParse, .    
223f1 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 53   "UPDATE %Q.%s S
223f2 45 54 20 72 6f 6f 74 70 61 67 65 3d 25 64 20 57  ET rootpage=%d W
223f3 48 45 52 45 20 23 25 64 20 41 4e 44 20 72 6f 6f  HERE #%d AND roo
223f4 74 70 61 67 65 3d 23 25 64 22 2c 0a 20 20 20 20  tpage=#%d",.    
223f5 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62   pParse->db->aDb
223f6 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48  [iDb].zName, SCH
223f7 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20  EMA_TABLE(iDb), 
223f8 69 54 61 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b  iTable, r1, r1);
223f9 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65  .#endif.  sqlite
223fa 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
223fb 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a  pParse, r1);.}..
223fc 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 56 44 42 45  /*.** Write VDBE
223fd 20 63 6f 64 65 20 74 6f 20 65 72 61 73 65 20 74   code to erase t
223fe 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20 61 6c  able pTab and al
223ff 6c 20 61 73 73 6f 63 69 61 74 65 64 20 69 6e 64  l associated ind
22400 69 63 65 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a  ices on disk..**
22401 20 43 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20   Code to update 
22402 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
22403 72 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 74  r tables and int
22404 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 64 65 66  ernal schema def
22405 69 6e 69 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63  initions.** in c
22406 61 73 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20  ase a root-page 
22407 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f  belonging to ano
22408 74 68 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f  ther table is mo
22409 76 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65  ved by the btree
2240a 20 6c 61 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73   layer.** is als
2240b 6f 20 61 64 64 65 64 20 28 74 68 69 73 20 63 61  o added (this ca
2240c 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e  n happen with an
2240d 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
2240e 61 62 61 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69  abase)..*/.stati
2240f 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 54 61  c void destroyTa
22410 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ble(Parse *pPars
22411 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b  e, Table *pTab){
22412 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
22413 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
22414 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
22415 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65  int iDb = sqlite
22416 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
22417 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
22418 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 64 65 73  >pSchema);.  des
22419 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61  troyRootPage(pPa
2241a 72 73 65 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  rse, pTab->tnum,
2241b 20 69 44 62 29 3b 0a 20 20 66 6f 72 28 70 49 64   iDb);.  for(pId
2241c 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2241d 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
2241e 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 64 65 73  >pNext){.    des
2241f 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61  troyRootPage(pPa
22420 72 73 65 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c  rse, pIdx->tnum,
22421 20 69 44 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65   iDb);.  }.#else
22422 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74  .  /* If the dat
22423 61 62 61 73 65 20 6d 61 79 20 62 65 20 61 75 74  abase may be aut
22424 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65  o-vacuum capable
22425 20 28 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54   (if SQLITE_OMIT
22426 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a  _AUTOVACUUM.  **
22427 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29   is not defined)
22428 2c 20 74 68 65 6e 20 69 74 20 69 73 20 69 6d 70  , then it is imp
22429 6f 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f  ortant to call O
2242a 50 5f 44 65 73 74 72 6f 79 20 6f 6e 20 74 68 65  P_Destroy on the
2242b 0a 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20  .  ** table and 
2242c 69 6e 64 65 78 20 72 6f 6f 74 2d 70 61 67 65 73  index root-pages
2242d 20 69 6e 20 6f 72 64 65 72 2c 20 73 74 61 72 74   in order, start
2242e 69 6e 67 20 77 69 74 68 20 74 68 65 20 6e 75 6d  ing with the num
2242f 65 72 69 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c  erically .  ** l
22430 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65  argest root-page
22431 20 6e 75 6d 62 65 72 2e 20 54 68 69 73 20 67 75   number. This gu
22432 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 6e 6f  arantees that no
22433 6e 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70  ne of the root-p
22434 61 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20  ages.  ** to be 
22435 64 65 73 74 72 6f 79 65 64 20 69 73 20 72 65 6c  destroyed is rel
22436 6f 63 61 74 65 64 20 62 79 20 61 6e 20 65 61 72  ocated by an ear
22437 6c 69 65 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e  lier OP_Destroy.
22438 20 69 2e 65 2e 20 69 66 20 74 68 65 0a 20 20 2a   i.e. if the.  *
22439 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65  * following were
2243a 20 63 6f 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a   coded:.  **.  *
2243b 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30  * OP_Destroy 4 0
2243c 0a 20 20 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f  .  ** ....  ** O
2243d 50 5f 44 65 73 74 72 6f 79 20 35 20 30 0a 20 20  P_Destroy 5 0.  
2243e 2a 2a 0a 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74  **.  ** and root
2243f 20 70 61 67 65 20 35 20 68 61 70 70 65 6e 65 64   page 5 happened
22440 20 74 6f 20 62 65 20 74 68 65 20 6c 61 72 67 65   to be the large
22441 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d  st root-page num
22442 62 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  ber in the.  ** 
22443 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 72  database, then r
22444 6f 6f 74 20 70 61 67 65 20 35 20 77 6f 75 6c 64  oot page 5 would
22445 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 70 61 67   be moved to pag
22446 65 20 34 20 62 79 20 74 68 65 20 0a 20 20 2a 2a  e 4 by the .  **
22447 20 22 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30   "OP_Destroy 4 0
22448 22 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 73 75  " opcode. The su
22449 62 73 65 71 75 65 6e 74 20 22 4f 50 5f 44 65 73  bsequent "OP_Des
2244a 74 72 6f 79 20 35 20 30 22 20 77 6f 75 6c 64 20  troy 5 0" would 
2244b 68 69 74 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d  hit.  ** a free-
2244c 6c 69 73 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a  list page..  */.
2244d 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 54 61    int iTab = pTa
2244e 62 2d 3e 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69  b->tnum;.  int i
2244f 44 65 73 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a  Destroyed = 0;..
22450 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20    while( 1 ){.  
22451 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
22452 20 20 20 69 6e 74 20 69 4c 61 72 67 65 73 74 20     int iLargest 
22453 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 69 44  = 0;..    if( iD
22454 65 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69  estroyed==0 || i
22455 54 61 62 3c 69 44 65 73 74 72 6f 79 65 64 20 29  Tab<iDestroyed )
22456 7b 0a 20 20 20 20 20 20 69 4c 61 72 67 65 73 74  {.      iLargest
22457 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20   = iTab;.    }. 
22458 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62     for(pIdx=pTab
22459 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
2245a 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2245b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 49 64  ){.      int iId
2245c 78 20 3d 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a  x = pIdx->tnum;.
2245d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
2245e 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  dx->pSchema==pTa
2245f 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20  b->pSchema );.  
22460 20 20 20 20 69 66 28 20 28 69 44 65 73 74 72 6f      if( (iDestro
22461 79 65 64 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c  yed==0 || (iIdx<
22462 69 44 65 73 74 72 6f 79 65 64 29 29 20 26 26 20  iDestroyed)) && 
22463 69 49 64 78 3e 69 4c 61 72 67 65 73 74 20 29 7b  iIdx>iLargest ){
22464 0a 20 20 20 20 20 20 20 20 69 4c 61 72 67 65 73  .        iLarges
22465 74 20 3d 20 69 49 64 78 3b 0a 20 20 20 20 20 20  t = iIdx;.      
22466 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
22467 69 4c 61 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20  iLargest==0 ){. 
22468 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
22469 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e   }else{.      in
2246a 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
2246b 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
2246c 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
2246d 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 64  Schema);.      d
2246e 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70  estroyRootPage(p
2246f 50 61 72 73 65 2c 20 69 4c 61 72 67 65 73 74 2c  Parse, iLargest,
22470 20 69 44 62 29 3b 0a 20 20 20 20 20 20 69 44 65   iDb);.      iDe
22471 73 74 72 6f 79 65 64 20 3d 20 69 4c 61 72 67 65  stroyed = iLarge
22472 73 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  st;.    }.  }.#e
22473 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ndif.}../*.** Th
22474 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
22475 6c 6c 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77  lled to do the w
22476 6f 72 6b 20 6f 66 20 61 20 44 52 4f 50 20 54 41  ork of a DROP TA
22477 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  BLE statement..*
22478 2a 20 70 4e 61 6d 65 20 69 73 20 74 68 65 20 6e  * pName is the n
22479 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
2247a 20 74 6f 20 62 65 20 64 72 6f 70 70 65 64 2e 0a   to be dropped..
2247b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2247c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72  E void sqlite3Dr
2247d 6f 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70  opTable(Parse *p
2247e 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a  Parse, SrcList *
2247f 70 4e 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 65  pName, int isVie
22480 77 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20  w, int noErr){. 
22481 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
22482 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74  Vdbe *v;.  sqlit
22483 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
22484 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  >db;.  int iDb;.
22485 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
22486 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
22487 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
22488 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
22489 62 6c 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ble;.  }.  asser
2248a 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d  t( pName->nSrc==
2248b 31 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  1 );.  pTab = sq
2248c 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
2248d 28 70 50 61 72 73 65 2c 20 69 73 56 69 65 77 2c  (pParse, isView,
2248e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2248f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
22490 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c  ame->a[0].zName,
22491 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61   pName->a[0].zDa
22492 74 61 62 61 73 65 29 3b 0a 0a 20 20 69 66 28 20  tabase);..  if( 
22493 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 69  pTab==0 ){.    i
22494 66 28 20 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20  f( noErr ){.    
22495 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c    sqlite3ErrorCl
22496 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20  ear(pParse);.   
22497 20 7d 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74   }.    goto exit
22498 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d  _drop_table;.  }
22499 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
2249a 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
2249b 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2249c 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
2249d 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
2249e 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54  b );..  /* If pT
2249f 61 62 20 69 73 20 61 20 76 69 72 74 75 61 6c 20  ab is a virtual 
224a0 74 61 62 6c 65 2c 20 63 61 6c 6c 20 56 69 65 77  table, call View
224a1 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29  GetColumnNames()
224a2 20 74 6f 20 65 6e 73 75 72 65 0a 20 20 2a 2a 20   to ensure.  ** 
224a3 69 74 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  it is initialize
224a4 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 73  d..  */.  if( Is
224a5 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 26 26  Virtual(pTab) &&
224a6 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
224a7 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
224a8 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20  e, pTab) ){.    
224a9 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74  goto exit_drop_t
224aa 61 62 6c 65 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  able;.  }.#ifnde
224ab 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
224ac 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a  THORIZATION.  {.
224ad 20 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20      int code;.  
224ae 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
224af 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c  ab = SCHEMA_TABL
224b0 45 28 69 44 62 29 3b 0a 20 20 20 20 63 6f 6e 73  E(iDb);.    cons
224b1 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62  t char *zDb = db
224b2 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
224b3 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
224b4 20 2a 7a 41 72 67 32 20 3d 20 30 3b 0a 20 20 20   *zArg2 = 0;.   
224b5 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
224b6 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
224b7 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61  LITE_DELETE, zTa
224b8 62 2c 20 30 2c 20 7a 44 62 29 29 7b 0a 20 20 20  b, 0, zDb)){.   
224b9 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
224ba 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20  p_table;.    }. 
224bb 20 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b     if( isView ){
224bc 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54  .      if( !OMIT
224bd 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d  _TEMPDB && iDb==
224be 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  1 ){.        cod
224bf 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
224c0 54 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20 20  TEMP_VIEW;.     
224c1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
224c2 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
224c3 4f 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d  OP_VIEW;.      }
224c4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
224c5 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
224c6 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  E.    }else if( 
224c7 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
224c8 29 7b 0a 20 20 20 20 20 20 63 6f 64 65 20 3d 20  ){.      code = 
224c9 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42  SQLITE_DROP_VTAB
224ca 4c 45 3b 0a 20 20 20 20 20 20 7a 41 72 67 32 20  LE;.      zArg2 
224cb 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e  = pTab->pMod->zN
224cc 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ame;.#endif.    
224cd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
224ce 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
224cf 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20   iDb==1 ){.     
224d0 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
224d1 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45  _DROP_TEMP_TABLE
224d2 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
224d3 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51         code = SQ
224d4 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b  LITE_DROP_TABLE;
224d5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
224d6 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
224d7 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
224d8 63 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  code, pTab->zNam
224d9 65 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29 20 29  e, zArg2, zDb) )
224da 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
224db 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
224dc 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
224dd 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
224de 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
224df 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  TE, pTab->zName,
224e0 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20   0, zDb) ){.    
224e1 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
224e2 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20  _table;.    }.  
224e3 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 73  }.#endif.  if( s
224e4 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70  qlite3StrNICmp(p
224e5 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c  Tab->zName, "sql
224e6 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a  ite_", 7)==0 ){.
224e7 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
224e8 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
224e9 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
224ea 20 64 72 6f 70 70 65 64 22 2c 20 70 54 61 62 2d   dropped", pTab-
224eb 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
224ec 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
224ed 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  e;.  }..#ifndef 
224ee 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
224ef 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 44 52 4f  .  /* Ensure DRO
224f0 50 20 54 41 42 4c 45 20 69 73 20 6e 6f 74 20 75  P TABLE is not u
224f1 73 65 64 20 6f 6e 20 61 20 76 69 65 77 2c 20 61  sed on a view, a
224f2 6e 64 20 44 52 4f 50 20 56 49 45 57 20 69 73 20  nd DROP VIEW is 
224f3 6e 6f 74 20 75 73 65 64 0a 20 20 2a 2a 20 6f 6e  not used.  ** on
224f4 20 61 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20   a table..  */. 
224f5 20 69 66 28 20 69 73 56 69 65 77 20 26 26 20 70   if( isView && p
224f6 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  Tab->pSelect==0 
224f7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
224f8 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
224f9 75 73 65 20 44 52 4f 50 20 54 41 42 4c 45 20 74  use DROP TABLE t
224fa 6f 20 64 65 6c 65 74 65 20 74 61 62 6c 65 20 25  o delete table %
224fb 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  s", pTab->zName)
224fc 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
224fd 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  drop_table;.  }.
224fe 20 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26    if( !isView &&
224ff 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
22500 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
22501 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75  orMsg(pParse, "u
22502 73 65 20 44 52 4f 50 20 56 49 45 57 20 74 6f 20  se DROP VIEW to 
22503 64 65 6c 65 74 65 20 76 69 65 77 20 25 73 22 2c  delete view %s",
22504 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
22505 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
22506 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e  p_table;.  }.#en
22507 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  dif..  /* Genera
22508 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76  te code to remov
22509 65 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d  e the table from
2250a 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c   the master tabl
2250b 65 0a 20 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e 0a  e.  ** on disk..
2250c 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
2250d 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2250e 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
2250f 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
22510 67 65 72 3b 0a 20 20 20 20 44 62 20 2a 70 44 62  ger;.    Db *pDb
22511 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
22512 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67  ;.    sqlite3Beg
22513 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
22514 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29  (pParse, 1, iDb)
22515 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
22516 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
22517 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56 69  BLE.    if( IsVi
22518 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
22519 20 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20       if( v ){.  
2251a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2251b 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42  eAddOp0(v, OP_VB
2251c 65 67 69 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  egin);.      }. 
2251d 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
2251e 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 74 72 69   /* Drop all tri
2251f 67 67 65 72 73 20 61 73 73 6f 63 69 61 74 65 64  ggers associated
22520 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20   with the table 
22521 62 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 43  being dropped. C
22522 6f 64 65 0a 20 20 20 20 2a 2a 20 69 73 20 67 65  ode.    ** is ge
22523 6e 65 72 61 74 65 64 20 74 6f 20 72 65 6d 6f 76  nerated to remov
22524 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 73  e entries from s
22525 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64  qlite_master and
22526 2f 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  /or.    ** sqlit
22527 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 69 66  e_temp_master if
22528 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a   required..    *
22529 2f 0a 20 20 20 20 70 54 72 69 67 67 65 72 20 3d  /.    pTrigger =
2252a 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c   sqlite3TriggerL
2252b 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
2252c 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 54  );.    while( pT
2252d 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20  rigger ){.      
2252e 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65 72  assert( pTrigger
2252f 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d  ->pSchema==pTab-
22530 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a 20 20 20  >pSchema || .   
22531 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72 2d         pTrigger-
22532 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44  >pSchema==db->aD
22533 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a  b[1].pSchema );.
22534 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 72 6f        sqlite3Dro
22535 70 54 72 69 67 67 65 72 50 74 72 28 70 50 61 72  pTriggerPtr(pPar
22536 73 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20  se, pTrigger);. 
22537 20 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20       pTrigger = 
22538 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b  pTrigger->pNext;
22539 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  .    }..#ifndef 
2253a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
2253b 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a  INCREMENT.    /*
2253c 20 52 65 6d 6f 76 65 20 61 6e 79 20 65 6e 74 72   Remove any entr
2253d 69 65 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ies of the sqlit
2253e 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
2253f 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
22540 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 61 62 6c  .    ** the tabl
22541 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 2e  e being dropped.
22542 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65   This is done be
22543 66 6f 72 65 20 74 68 65 20 74 61 62 6c 65 20 69  fore the table i
22544 73 20 64 72 6f 70 70 65 64 0a 20 20 20 20 2a 2a  s dropped.    **
22545 20 61 74 20 74 68 65 20 62 74 72 65 65 20 6c 65   at the btree le
22546 76 65 6c 2c 20 69 6e 20 63 61 73 65 20 74 68 65  vel, in case the
22547 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
22548 20 74 61 62 6c 65 20 6e 65 65 64 73 20 74 6f 0a   table needs to.
22549 20 20 20 20 2a 2a 20 6d 6f 76 65 20 61 73 20 61      ** move as a
2254a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 64   result of the d
2254b 72 6f 70 20 28 63 61 6e 20 68 61 70 70 65 6e 20  rop (can happen 
2254c 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d  in auto-vacuum m
2254d 6f 64 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ode)..    */.   
2254e 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c   if( pTab->tabFl
2254f 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63  ags & TF_Autoinc
22550 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  rement ){.      
22551 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
22552 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
22553 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
22554 25 73 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  %s.sqlite_sequen
22555 63 65 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51  ce WHERE name=%Q
22556 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e  ",.        pDb->
22557 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61  zName, pTab->zNa
22558 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  me.      );.    
22559 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
2255a 20 44 72 6f 70 20 61 6c 6c 20 53 51 4c 49 54 45   Drop all SQLITE
2255b 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 61 6e  _MASTER table an
2255c 64 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20  d index entries 
2255d 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68  that refer to th
2255e 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20  e.    ** table. 
2255f 54 68 65 20 70 72 6f 67 72 61 6d 20 6e 61 6d 65  The program name
22560 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 74   loops through t
22561 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20  he master table 
22562 61 6e 64 20 64 65 6c 65 74 65 73 0a 20 20 20 20  and deletes.    
22563 2a 2a 20 65 76 65 72 79 20 72 6f 77 20 74 68 61  ** every row tha
22564 74 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 61  t refers to a ta
22565 62 6c 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ble of the same 
22566 6e 61 6d 65 20 61 73 20 74 68 65 20 6f 6e 65 20  name as the one 
22567 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 64 72 6f  being.    ** dro
22568 70 70 65 64 2e 20 54 72 69 67 67 65 72 73 20 61  pped. Triggers a
22569 72 65 20 68 61 6e 64 6c 65 64 20 73 65 70 65 72  re handled seper
2256a 61 74 65 6c 79 20 62 65 63 61 75 73 65 20 61 20  ately because a 
2256b 74 72 69 67 67 65 72 20 63 61 6e 20 62 65 0a 20  trigger can be. 
2256c 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e     ** created in
2256d 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61   the temp databa
2256e 73 65 20 74 68 61 74 20 72 65 66 65 72 73 20 74  se that refers t
2256f 6f 20 61 20 74 61 62 6c 65 20 69 6e 20 61 6e 6f  o a table in ano
22570 74 68 65 72 0a 20 20 20 20 2a 2a 20 64 61 74 61  ther.    ** data
22571 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  base..    */.   
22572 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
22573 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20  rse(pParse, .   
22574 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f       "DELETE FRO
22575 4d 20 25 51 2e 25 73 20 57 48 45 52 45 20 74 62  M %Q.%s WHERE tb
22576 6c 5f 6e 61 6d 65 3d 25 51 20 61 6e 64 20 74 79  l_name=%Q and ty
22577 70 65 21 3d 27 74 72 69 67 67 65 72 27 22 2c 0a  pe!='trigger'",.
22578 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61          pDb->zNa
22579 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  me, SCHEMA_TABLE
2257a 28 69 44 62 29 2c 20 70 54 61 62 2d 3e 7a 4e 61  (iDb), pTab->zNa
2257b 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f  me);..    /* Dro
2257c 70 20 61 6e 79 20 73 74 61 74 69 73 74 69 63 73  p any statistics
2257d 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65   from the sqlite
2257e 5f 73 74 61 74 31 20 74 61 62 6c 65 2c 20 69 66  _stat1 table, if
2257f 20 69 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20   it exists */.  
22580 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
22581 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69  dTable(db, "sqli
22582 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e 61  te_stat1", db->a
22583 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29  Db[iDb].zName) )
22584 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e  {.      sqlite3N
22585 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
22586 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45  e,.        "DELE
22587 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74  TE FROM %Q.sqlit
22588 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 74 62  e_stat1 WHERE tb
22589 6c 3d 25 51 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d  l=%Q", pDb->zNam
2258a 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20  e, pTab->zName. 
2258b 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20       );.    }.. 
2258c 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20 26     if( !isView &
2258d 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61  & !IsVirtual(pTa
2258e 62 29 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74  b) ){.      dest
2258f 72 6f 79 54 61 62 6c 65 28 70 50 61 72 73 65 2c  royTable(pParse,
22590 20 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 0a 20   pTab);.    }.. 
22591 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65     /* Remove the
22592 20 74 61 62 6c 65 20 65 6e 74 72 79 20 66 72 6f   table entry fro
22593 6d 20 53 51 4c 69 74 65 27 73 20 69 6e 74 65 72  m SQLite's inter
22594 6e 61 6c 20 73 63 68 65 6d 61 20 61 6e 64 20 6d  nal schema and m
22595 6f 64 69 66 79 0a 20 20 20 20 2a 2a 20 74 68 65  odify.    ** the
22596 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a   schema cookie..
22597 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49      */.    if( I
22598 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
22599 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2259a 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2259b 56 44 65 73 74 72 6f 79 2c 20 69 44 62 2c 20 30  VDestroy, iDb, 0
2259c 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
2259d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
2259e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2259f 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c  4(v, OP_DropTabl
225a0 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 54  e, iDb, 0, 0, pT
225a1 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ab->zName, 0);. 
225a2 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
225a3 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
225a4 44 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  Db);.  }.  sqlit
225a5 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 64 62  eViewResetAll(db
225a6 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f 64 72  , iDb);..exit_dr
225a7 6f 70 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69  op_table:.  sqli
225a8 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
225a9 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a  (db, pName);.}..
225aa 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
225ab 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
225ac 63 72 65 61 74 65 20 61 20 6e 65 77 20 66 6f 72  create a new for
225ad 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 68 65 20  eign key on the 
225ae 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72 65 6e 74  table.** current
225af 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ly under constru
225b0 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f 6c  ction.  pFromCol
225b1 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63   determines whic
225b2 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20  h columns.** in 
225b3 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c  the current tabl
225b4 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  e point to the f
225b5 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20 49 66 20  oreign key.  If 
225b6 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65 6e  pFromCol==0 then
225b7 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74 68 65 20  .** connect the 
225b8 6b 65 79 20 74 6f 20 74 68 65 20 6c 61 73 74 20  key to the last 
225b9 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74 65 64 2e  column inserted.
225ba 20 20 70 54 6f 20 69 73 20 74 68 65 20 6e 61 6d    pTo is the nam
225bb 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  e of.** the tabl
225bc 65 20 72 65 66 65 72 72 65 64 20 74 6f 2e 20 20  e referred to.  
225bd 70 54 6f 43 6f 6c 20 69 73 20 61 20 6c 69 73 74  pToCol is a list
225be 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68   of tables in th
225bf 65 20 6f 74 68 65 72 0a 2a 2a 20 70 54 6f 20 74  e other.** pTo t
225c0 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 66 6f  able that the fo
225c1 72 65 69 67 6e 20 6b 65 79 20 70 6f 69 6e 74 73  reign key points
225c2 20 74 6f 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74   to.  flags cont
225c3 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66 6f  ains all.** info
225c4 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
225c5 65 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c  e conflict resol
225c6 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73  ution algorithms
225c7 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 69 6e   specified.** in
225c8 20 74 68 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20   the ON DELETE, 
225c9 4f 4e 20 55 50 44 41 54 45 20 61 6e 64 20 4f 4e  ON UPDATE and ON
225ca 20 49 4e 53 45 52 54 20 63 6c 61 75 73 65 73 2e   INSERT clauses.
225cb 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b 65 79 20 73  .**.** An FKey s
225cc 74 72 75 63 74 75 72 65 20 69 73 20 63 72 65 61  tructure is crea
225cd 74 65 64 20 61 6e 64 20 61 64 64 65 64 20 74 6f  ted and added to
225ce 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65   the table curre
225cf 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65 72 20 63 6f  ntly.** under co
225d0 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68  nstruction in th
225d1 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  e pParse->pNewTa
225d2 62 6c 65 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a  ble field..**.**
225d3 20 54 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   The foreign key
225d4 20 69 73 20 73 65 74 20 66 6f 72 20 49 4d 4d 45   is set for IMME
225d5 44 49 41 54 45 20 70 72 6f 63 65 73 73 69 6e 67  DIATE processing
225d6 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20  .  A subsequent 
225d7 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  call.** to sqlit
225d8 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65  e3DeferForeignKe
225d9 79 28 29 20 6d 69 67 68 74 20 63 68 61 6e 67 65  y() might change
225da 20 74 68 69 73 20 74 6f 20 44 45 46 45 52 52 45   this to DEFERRE
225db 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  D..*/.SQLITE_PRI
225dc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
225dd 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65  3CreateForeignKe
225de 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  y(.  Parse *pPar
225df 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se,       /* Par
225e0 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
225e1 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46 72 6f    ExprList *pFro
225e2 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d 6e  mCol,  /* Column
225e3 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20  s in this table 
225e4 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 74  that point to ot
225e5 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54  her table */.  T
225e6 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20 20  oken *pTo,      
225e7 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
225e8 68 65 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a  he other table *
225e9 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 54  /.  ExprList *pT
225ea 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c 75  oCol,    /* Colu
225eb 6d 6e 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72  mns in the other
225ec 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
225ed 66 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20  flags           
225ee 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73   /* Conflict res
225ef 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68  olution algorith
225f0 6d 73 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ms. */.){.  sqli
225f1 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
225f2 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51  ->db;.#ifndef SQ
225f3 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
225f4 4e 5f 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70 46  N_KEY.  FKey *pF
225f5 4b 65 79 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65  Key = 0;.  Table
225f6 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e   *p = pParse->pN
225f7 65 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 6e  ewTable;.  int n
225f8 42 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Byte;.  int i;. 
225f9 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 68 61   int nCol;.  cha
225fa 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28  r *z;..  assert(
225fb 20 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 66 28   pTo!=0 );.  if(
225fc 20 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d   p==0 || pParse-
225fd 3e 6e 45 72 72 20 7c 7c 20 49 4e 5f 44 45 43 4c  >nErr || IN_DECL
225fe 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20  ARE_VTAB ) goto 
225ff 66 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 70 46  fk_end;.  if( pF
22600 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20  romCol==0 ){.   
22601 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e   int iCol = p->n
22602 43 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 69  Col-1;.    if( i
22603 43 6f 6c 3c 30 20 29 20 67 6f 74 6f 20 66 6b 5f  Col<0 ) goto fk_
22604 65 6e 64 3b 0a 20 20 20 20 69 66 28 20 70 54 6f  end;.    if( pTo
22605 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e  Col && pToCol->n
22606 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 20  Expr!=1 ){.     
22607 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
22608 28 70 50 61 72 73 65 2c 20 22 66 6f 72 65 69 67  (pParse, "foreig
22609 6e 20 6b 65 79 20 6f 6e 20 25 73 22 0a 20 20 20  n key on %s".   
2260a 20 20 20 20 20 20 22 20 73 68 6f 75 6c 64 20 72        " should r
2260b 65 66 65 72 65 6e 63 65 20 6f 6e 6c 79 20 6f 6e  eference only on
2260c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c  e column of tabl
2260d 65 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20 20  e %T",.         
2260e 70 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  p->aCol[iCol].zN
2260f 61 6d 65 2c 20 70 54 6f 29 3b 0a 20 20 20 20 20  ame, pTo);.     
22610 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20   goto fk_end;.  
22611 20 20 7d 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 31    }.    nCol = 1
22612 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54  ;.  }else if( pT
22613 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e  oCol && pToCol->
22614 6e 45 78 70 72 21 3d 70 46 72 6f 6d 43 6f 6c 2d  nExpr!=pFromCol-
22615 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 71  >nExpr ){.    sq
22616 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
22617 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 6e  arse,.        "n
22618 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
22619 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20   in foreign key 
2261a 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74  does not match t
2261b 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 22 0a 20  he number of ". 
2261c 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 73 20         "columns 
2261d 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  in the reference
2261e 64 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 67  d table");.    g
2261f 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d 65  oto fk_end;.  }e
22620 6c 73 65 7b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20  lse{.    nCol = 
22621 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 3b  pFromCol->nExpr;
22622 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 73  .  }.  nByte = s
22623 69 7a 65 6f 66 28 2a 70 46 4b 65 79 29 20 2b 20  izeof(*pFKey) + 
22624 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 28  (nCol-1)*sizeof(
22625 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20  pFKey->aCol[0]) 
22626 2b 20 70 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20  + pTo->n + 1;.  
22627 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20  if( pToCol ){.  
22628 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f    for(i=0; i<pTo
22629 43 6f 6c 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  Col->nExpr; i++)
2262a 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d  {.      nByte +=
2262b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2262c 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e  (pToCol->a[i].zN
2262d 61 6d 65 29 20 2b 20 31 3b 0a 20 20 20 20 7d 0a  ame) + 1;.    }.
2262e 20 20 7d 0a 20 20 70 46 4b 65 79 20 3d 20 73 71    }.  pFKey = sq
2262f 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
22630 6f 28 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a 20  o(db, nByte );. 
22631 20 69 66 28 20 70 46 4b 65 79 3d 3d 30 20 29 7b   if( pFKey==0 ){
22632 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  .    goto fk_end
22633 3b 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 70  ;.  }.  pFKey->p
22634 46 72 6f 6d 20 3d 20 70 3b 0a 20 20 70 46 4b 65  From = p;.  pFKe
22635 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 3d 20 70  y->pNextFrom = p
22636 2d 3e 70 46 4b 65 79 3b 0a 20 20 7a 20 3d 20 28  ->pFKey;.  z = (
22637 63 68 61 72 2a 29 26 70 46 4b 65 79 2d 3e 61 43  char*)&pFKey->aC
22638 6f 6c 5b 6e 43 6f 6c 5d 3b 0a 20 20 70 46 4b 65  ol[nCol];.  pFKe
22639 79 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a 20 20 6d 65  y->zTo = z;.  me
2263a 6d 63 70 79 28 7a 2c 20 70 54 6f 2d 3e 7a 2c 20  mcpy(z, pTo->z, 
2263b 70 54 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b 70 54 6f  pTo->n);.  z[pTo
2263c 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69  ->n] = 0;.  sqli
2263d 74 65 33 44 65 71 75 6f 74 65 28 7a 29 3b 0a 20  te3Dequote(z);. 
2263e 20 7a 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a   z += pTo->n+1;.
2263f 20 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 20 3d 20    pFKey->nCol = 
22640 6e 43 6f 6c 3b 0a 20 20 69 66 28 20 70 46 72 6f  nCol;.  if( pFro
22641 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 70  mCol==0 ){.    p
22642 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46  FKey->aCol[0].iF
22643 72 6f 6d 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b  rom = p->nCol-1;
22644 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f  .  }else{.    fo
22645 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
22646 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a  ++){.      int j
22647 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
22648 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29   j<p->nCol; j++)
22649 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
2264a 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 2d 3e  lite3StrICmp(p->
2264b 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70  aCol[j].zName, p
2264c 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e  FromCol->a[i].zN
2264d 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ame)==0 ){.     
2264e 20 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c       pFKey->aCol
2264f 5b 69 5d 2e 69 46 72 6f 6d 20 3d 20 6a 3b 0a 20  [i].iFrom = j;. 
22650 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
22651 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22652 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 70  }.      if( j>=p
22653 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
22654 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
22655 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  g(pParse, .     
22656 20 20 20 20 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f       "unknown co
22657 6c 75 6d 6e 20 5c 22 25 73 5c 22 20 69 6e 20 66  lumn \"%s\" in f
22658 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e  oreign key defin
22659 69 74 69 6f 6e 22 2c 20 0a 20 20 20 20 20 20 20  ition", .       
2265a 20 20 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69     pFromCol->a[i
2265b 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ].zName);.      
2265c 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20    goto fk_end;. 
2265d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2265e 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b  .  if( pToCol ){
2265f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
22660 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
22661 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65    int n = sqlite
22662 33 53 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c  3Strlen30(pToCol
22663 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  ->a[i].zName);. 
22664 20 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c       pFKey->aCol
22665 5b 69 5d 2e 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20  [i].zCol = z;.  
22666 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54      memcpy(z, pT
22667 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  oCol->a[i].zName
22668 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d  , n);.      z[n]
22669 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a 20 2b 3d   = 0;.      z +=
2266a 20 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   n+1;.    }.  }.
2266b 20 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72    pFKey->isDefer
2266c 72 65 64 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79  red = 0;.  pFKey
2266d 2d 3e 64 65 6c 65 74 65 43 6f 6e 66 20 3d 20 28  ->deleteConf = (
2266e 75 38 29 28 66 6c 61 67 73 20 26 20 30 78 66 66  u8)(flags & 0xff
2266f 29 3b 0a 20 20 70 46 4b 65 79 2d 3e 75 70 64 61  );.  pFKey->upda
22670 74 65 43 6f 6e 66 20 3d 20 28 75 38 29 28 28 66  teConf = (u8)((f
22671 6c 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78  lags >> 8 ) & 0x
22672 66 66 29 3b 0a 20 20 70 46 4b 65 79 2d 3e 69 6e  ff);.  pFKey->in
22673 73 65 72 74 43 6f 6e 66 20 3d 20 28 75 38 29 28  sertConf = (u8)(
22674 28 66 6c 61 67 73 20 3e 3e 20 31 36 20 29 20 26  (flags >> 16 ) &
22675 20 30 78 66 66 29 3b 0a 0a 20 20 2f 2a 20 4c 69   0xff);..  /* Li
22676 6e 6b 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  nk the foreign k
22677 65 79 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ey to the table 
22678 61 73 20 74 68 65 20 6c 61 73 74 20 73 74 65 70  as the last step
22679 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 46 4b 65  ..  */.  p->pFKe
2267a 79 20 3d 20 70 46 4b 65 79 3b 0a 20 20 70 46 4b  y = pFKey;.  pFK
2267b 65 79 20 3d 20 30 3b 0a 0a 66 6b 5f 65 6e 64 3a  ey = 0;..fk_end:
2267c 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2267d 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 23 65 6e  (db, pFKey);.#en
2267e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
2267f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
22680 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20 73 71  IGN_KEY) */.  sq
22681 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
22682 65 74 65 28 64 62 2c 20 70 46 72 6f 6d 43 6f 6c  ete(db, pFromCol
22683 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
22684 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
22685 54 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ToCol);.}../*.**
22686 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
22687 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20   called when an 
22688 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49  INITIALLY IMMEDI
22689 41 54 45 20 6f 72 20 49 4e 49 54 49 41 4c 4c 59  ATE or INITIALLY
2268a 20 44 45 46 45 52 52 45 44 0a 2a 2a 20 63 6c 61   DEFERRED.** cla
2268b 75 73 65 20 69 73 20 73 65 65 6e 20 61 73 20 70  use is seen as p
2268c 61 72 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e  art of a foreign
2268d 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2e   key definition.
2268e 20 20 54 68 65 20 69 73 44 65 66 65 72 72 65 64    The isDeferred
2268f 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73  .** parameter is
22690 20 31 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59   1 for INITIALLY
22691 20 44 45 46 45 52 52 45 44 20 61 6e 64 20 30 20   DEFERRED and 0 
22692 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d  for INITIALLY IM
22693 4d 45 44 49 41 54 45 2e 0a 2a 2a 20 54 68 65 20  MEDIATE..** The 
22694 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20  behavior of the 
22695 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72  most recently cr
22696 65 61 74 65 64 20 66 6f 72 65 69 67 6e 20 6b 65  eated foreign ke
22697 79 20 69 73 20 61 64 6a 75 73 74 65 64 0a 2a 2a  y is adjusted.**
22698 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2f   accordingly..*/
22699 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2269a 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65  void sqlite3Defe
2269b 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73  rForeignKey(Pars
2269c 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
2269d 73 44 65 66 65 72 72 65 64 29 7b 0a 23 69 66 6e  sDeferred){.#ifn
2269e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2269f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 54 61  FOREIGN_KEY.  Ta
226a0 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 46 4b 65  ble *pTab;.  FKe
226a1 79 20 2a 70 46 4b 65 79 3b 0a 20 20 69 66 28 20  y *pFKey;.  if( 
226a2 28 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e  (pTab = pParse->
226a3 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 7c 7c  pNewTable)==0 ||
226a4 20 28 70 46 4b 65 79 20 3d 20 70 54 61 62 2d 3e   (pFKey = pTab->
226a5 70 46 4b 65 79 29 3d 3d 30 20 29 20 72 65 74 75  pFKey)==0 ) retu
226a6 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73  rn;.  assert( is
226a7 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c 20 69  Deferred==0 || i
226a8 73 44 65 66 65 72 72 65 64 3d 3d 31 20 29 3b 0a  sDeferred==1 );.
226a9 20 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72    pFKey->isDefer
226aa 72 65 64 20 3d 20 28 75 38 29 69 73 44 65 66 65  red = (u8)isDefe
226ab 72 72 65 64 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  rred;.#endif.}..
226ac 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
226ad 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 72  ode that will er
226ae 61 73 65 20 61 6e 64 20 72 65 66 69 6c 6c 20 69  ase and refill i
226af 6e 64 65 78 20 2a 70 49 64 78 2e 20 20 54 68 69  ndex *pIdx.  Thi
226b0 73 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20  s is.** used to 
226b1 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77  initialize a new
226b2 6c 79 20 63 72 65 61 74 65 64 20 69 6e 64 65 78  ly created index
226b3 20 6f 72 20 74 6f 20 72 65 63 6f 6d 70 75 74 65   or to recompute
226b4 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20   the.** content 
226b5 6f 66 20 61 6e 20 69 6e 64 65 78 20 69 6e 20 72  of an index in r
226b6 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 52 45 49  esponse to a REI
226b7 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  NDEX command..**
226b8 0a 2a 2a 20 69 66 20 6d 65 6d 52 6f 6f 74 50 61  .** if memRootPa
226b9 67 65 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69  ge is not negati
226ba 76 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ve, it means tha
226bb 74 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 6e  t the index is n
226bc 65 77 6c 79 0a 2a 2a 20 63 72 65 61 74 65 64 2e  ewly.** created.
226bd 20 20 54 68 65 20 72 65 67 69 73 74 65 72 20 73    The register s
226be 70 65 63 69 66 69 65 64 20 62 79 20 6d 65 6d 52  pecified by memR
226bf 6f 6f 74 50 61 67 65 20 63 6f 6e 74 61 69 6e 73  ootPage contains
226c0 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67   the.** root pag
226c1 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
226c2 69 6e 64 65 78 2e 20 20 49 66 20 6d 65 6d 52 6f  index.  If memRo
226c3 6f 74 50 61 67 65 20 69 73 20 6e 65 67 61 74 69  otPage is negati
226c4 76 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  ve, then.** the 
226c5 69 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78  index already ex
226c6 69 73 74 73 20 61 6e 64 20 6d 75 73 74 20 62 65  ists and must be
226c7 20 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20   cleared before 
226c8 62 65 69 6e 67 20 72 65 66 69 6c 6c 65 64 20 61  being refilled a
226c9 6e 64 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70  nd.** the root p
226ca 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
226cb 65 20 69 6e 64 65 78 20 69 73 20 74 61 6b 65 6e  e index is taken
226cc 20 66 72 6f 6d 20 70 49 6e 64 65 78 2d 3e 74 6e   from pIndex->tn
226cd 75 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  um..*/.static vo
226ce 69 64 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c  id sqlite3Refill
226cf 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61  Index(Parse *pPa
226d0 72 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64  rse, Index *pInd
226d1 65 78 2c 20 69 6e 74 20 6d 65 6d 52 6f 6f 74 50  ex, int memRootP
226d2 61 67 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  age){.  Table *p
226d3 54 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54  Tab = pIndex->pT
226d4 61 62 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 61  able;  /* The ta
226d5 62 6c 65 20 74 68 61 74 20 69 73 20 69 6e 64 65  ble that is inde
226d6 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  xed */.  int iTa
226d7 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  b = pParse->nTab
226d8 2b 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 65  ++;     /* Btree
226d9 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72   cursor used for
226da 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69   pTab */.  int i
226db 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Idx = pParse->nT
226dc 61 62 2b 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72  ab++;     /* Btr
226dd 65 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 66  ee cursor used f
226de 6f 72 20 70 49 6e 64 65 78 20 2a 2f 0a 20 20 69  or pIndex */.  i
226df 6e 74 20 61 64 64 72 31 3b 20 20 20 20 20 20 20  nt addr1;       
226e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
226e1 20 41 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20   Address of top 
226e2 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  of loop */.  int
226e3 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20   tnum;          
226e4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
226e5 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 6e 64 65  oot page of inde
226e6 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  x */.  Vdbe *v; 
226e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226e8 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
226e9 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73  e code into this
226ea 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
226eb 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
226ec 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
226ed 20 20 20 20 20 2f 2a 20 4b 65 79 49 6e 66 6f 20       /* KeyInfo 
226ee 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  for index */.  i
226ef 6e 74 20 72 65 67 49 64 78 4b 65 79 3b 20 20 20  nt regIdxKey;   
226f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
226f1 20 52 65 67 69 73 74 65 72 73 20 63 6f 6e 74 61   Registers conta
226f2 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20  ining the index 
226f3 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  key */.  int reg
226f4 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 20  Record;         
226f5 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
226f6 74 65 72 20 68 6f 6c 64 69 6e 67 20 61 73 73 65  ter holding asse
226f7 6d 62 6c 69 65 64 20 69 6e 64 65 78 20 72 65 63  mblied index rec
226f8 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ord */.  sqlite3
226f9 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
226fa 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64  b;      /* The d
226fb 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
226fc 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 20  on */.  int iDb 
226fd 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
226fe 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65  oIndex(db, pInde
226ff 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69  x->pSchema);..#i
22700 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22701 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
22702 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
22703 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
22704 51 4c 49 54 45 5f 52 45 49 4e 44 45 58 2c 20 70  QLITE_REINDEX, p
22705 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 2c  Index->zName, 0,
22706 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
22707 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 7b 0a 20  Db].zName ) ){. 
22708 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
22709 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 71 75  endif..  /* Requ
2270a 69 72 65 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ire a write-lock
2270b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f   on the table to
2270c 20 70 65 72 66 6f 72 6d 20 74 68 69 73 20 6f 70   perform this op
2270d 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  eration */.  sql
2270e 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50  ite3TableLock(pP
2270f 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d  arse, iDb, pTab-
22710 3e 74 6e 75 6d 2c 20 31 2c 20 70 54 61 62 2d 3e  >tnum, 1, pTab->
22711 7a 4e 61 6d 65 29 3b 0a 0a 20 20 76 20 3d 20 73  zName);..  v = s
22712 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
22713 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
22714 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
22715 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d 30  ( memRootPage>=0
22716 20 29 7b 0a 20 20 20 20 74 6e 75 6d 20 3d 20 6d   ){.    tnum = m
22717 65 6d 52 6f 6f 74 50 61 67 65 3b 0a 20 20 7d 65  emRootPage;.  }e
22718 6c 73 65 7b 0a 20 20 20 20 74 6e 75 6d 20 3d 20  lse{.    tnum = 
22719 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 3b 0a 20 20  pIndex->tnum;.  
2271a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2271b 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c  Op2(v, OP_Clear,
2271c 20 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d   tnum, iDb);.  }
2271d 0a 20 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  .  pKey = sqlite
2271e 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
2271f 61 72 73 65 2c 20 70 49 6e 64 65 78 29 3b 0a 20  arse, pIndex);. 
22720 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22721 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p4(v, OP_OpenWri
22722 74 65 2c 20 69 49 64 78 2c 20 74 6e 75 6d 2c 20  te, iIdx, tnum, 
22723 69 44 62 2c 20 0a 20 20 20 20 20 20 20 20 20 20  iDb, .          
22724 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 20            (char 
22725 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e  *)pKey, P4_KEYIN
22726 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 69  FO_HANDOFF);.  i
22727 66 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d  f( memRootPage>=
22728 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
22729 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2272a 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  1);.  }.  sqlite
2272b 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
2272c 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54  e, iTab, iDb, pT
2272d 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  ab, OP_OpenRead)
2272e 3b 0a 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69  ;.  addr1 = sqli
2272f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22730 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62   OP_Rewind, iTab
22731 2c 20 30 29 3b 0a 20 20 72 65 67 52 65 63 6f 72  , 0);.  regRecor
22732 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
22733 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
22734 20 72 65 67 49 64 78 4b 65 79 20 3d 20 73 71 6c   regIdxKey = sql
22735 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65  ite3GenerateInde
22736 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49 6e  xKey(pParse, pIn
22737 64 65 78 2c 20 69 54 61 62 2c 20 72 65 67 52 65  dex, iTab, regRe
22738 63 6f 72 64 2c 20 31 29 3b 0a 20 20 69 66 28 20  cord, 1);.  if( 
22739 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21  pIndex->onError!
2273a 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 20  =OE_None ){.    
2273b 63 6f 6e 73 74 20 69 6e 74 20 72 65 67 52 6f 77  const int regRow
2273c 69 64 20 3d 20 72 65 67 49 64 78 4b 65 79 20 2b  id = regIdxKey +
2273d 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e   pIndex->nColumn
2273e 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  ;.    const int 
2273f 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j2 = sqlite3Vdbe
22740 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2b  CurrentAddr(v) +
22741 20 32 3b 0a 20 20 20 20 76 6f 69 64 20 2a 20 63   2;.    void * c
22742 6f 6e 73 74 20 70 52 65 67 4b 65 79 20 3d 20 53  onst pRegKey = S
22743 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
22744 28 72 65 67 49 64 78 4b 65 79 29 3b 0a 0a 20 20  (regIdxKey);..  
22745 20 20 2f 2a 20 54 68 65 20 72 65 67 69 73 74 65    /* The registe
22746 72 73 20 61 63 63 65 73 73 65 64 20 62 79 20 74  rs accessed by t
22747 68 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f  he OP_IsUnique o
22748 70 63 6f 64 65 20 77 65 72 65 20 61 6c 6c 6f 63  pcode were alloc
22749 61 74 65 64 0a 20 20 20 20 2a 2a 20 75 73 69 6e  ated.    ** usin
2274a 67 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  g sqlite3GetTemp
2274b 52 61 6e 67 65 28 29 20 69 6e 73 69 64 65 20 6f  Range() inside o
2274c 66 20 74 68 65 20 73 71 6c 69 74 65 33 47 65 6e  f the sqlite3Gen
2274d 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 29 0a  erateIndexKey().
2274e 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76      ** call abov
2274f 65 2e 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74  e. Just before t
22750 68 61 74 20 66 75 6e 63 74 69 6f 6e 20 77 61 73  hat function was
22751 20 66 72 65 65 64 20 74 68 65 79 20 77 65 72 65   freed they were
22752 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a   released.    **
22753 20 28 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65   (made available
22754 20 74 6f 20 74 68 65 20 63 6f 6d 70 69 6c 65 72   to the compiler
22755 20 66 6f 72 20 72 65 75 73 65 29 20 75 73 69 6e   for reuse) usin
22756 67 20 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  g .    ** sqlite
22757 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
22758 65 28 29 2e 20 53 6f 20 69 6e 20 73 6f 6d 65 20  e(). So in some 
22759 77 61 79 73 20 68 61 76 69 6e 67 20 74 68 65 20  ways having the 
2275a 4f 50 5f 49 73 55 6e 69 71 75 65 0a 20 20 20 20  OP_IsUnique.    
2275b 2a 2a 20 6f 70 63 6f 64 65 20 75 73 65 20 74 68  ** opcode use th
2275c 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20  e values stored 
2275d 77 69 74 68 69 6e 20 73 65 65 6d 73 20 64 61 6e  within seems dan
2275e 67 65 72 6f 75 73 2e 20 48 6f 77 65 76 65 72 2c  gerous. However,
2275f 20 73 69 6e 63 65 0a 20 20 20 20 2a 2a 20 77 65   since.    ** we
22760 20 63 61 6e 20 62 65 20 73 75 72 65 20 74 68 61   can be sure tha
22761 74 20 6e 6f 20 6f 74 68 65 72 20 74 65 6d 70 20  t no other temp 
22762 72 65 67 69 73 74 65 72 73 20 68 61 76 65 20 62  registers have b
22763 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20  een allocated.  
22764 20 20 2a 2a 20 73 69 6e 63 65 20 73 71 6c 69 74    ** since sqlit
22765 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
22766 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 2c  ge() was called,
22767 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 64   it is safe to d
22768 6f 20 73 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  o so..    */.   
22769 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2276a 70 34 28 76 2c 20 4f 50 5f 49 73 55 6e 69 71 75  p4(v, OP_IsUniqu
2276b 65 2c 20 69 49 64 78 2c 20 6a 32 2c 20 72 65 67  e, iIdx, j2, reg
2276c 52 6f 77 69 64 2c 20 70 52 65 67 4b 65 79 2c 20  Rowid, pRegKey, 
2276d 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73  P4_INT32);.    s
2276e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2276f 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c  (v, OP_Halt, SQL
22770 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20  ITE_CONSTRAINT, 
22771 4f 45 5f 41 62 6f 72 74 2c 20 30 2c 0a 20 20 20  OE_Abort, 0,.   
22772 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22773 20 22 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e   "indexed column
22774 73 20 61 72 65 20 6e 6f 74 20 75 6e 69 71 75 65  s are not unique
22775 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
22776 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
22777 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
22778 49 6e 73 65 72 74 2c 20 69 49 64 78 2c 20 72 65  Insert, iIdx, re
22779 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69  gRecord);.  sqli
2277a 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
2277b 76 2c 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45  v, OPFLAG_USESEE
2277c 4b 52 45 53 55 4c 54 29 3b 0a 20 20 73 71 6c 69  KRESULT);.  sqli
2277d 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2277e 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 63  g(pParse, regRec
2277f 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ord);.  sqlite3V
22780 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22781 4e 65 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72  Next, iTab, addr
22782 31 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  1+1);.  sqlite3V
22783 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
22784 64 64 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  ddr1);.  sqlite3
22785 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
22786 5f 43 6c 6f 73 65 2c 20 69 54 61 62 29 3b 0a 20  _Close, iTab);. 
22787 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22788 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
22789 69 49 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  iIdx);.}../*.** 
2278a 43 72 65 61 74 65 20 61 20 6e 65 77 20 69 6e 64  Create a new ind
2278b 65 78 20 66 6f 72 20 61 6e 20 53 51 4c 20 74 61  ex for an SQL ta
2278c 62 6c 65 2e 20 20 70 4e 61 6d 65 31 2e 70 4e 61  ble.  pName1.pNa
2278d 6d 65 32 20 69 73 20 74 68 65 20 6e 61 6d 65 20  me2 is the name 
2278e 6f 66 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a  of the index .**
2278f 20 61 6e 64 20 70 54 62 6c 4c 69 73 74 20 69 73   and pTblList is
22790 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
22791 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74   table that is t
22792 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 42  o be indexed.  B
22793 6f 74 68 20 77 69 6c 6c 20 0a 2a 2a 20 62 65 20  oth will .** be 
22794 4e 55 4c 4c 20 66 6f 72 20 61 20 70 72 69 6d 61  NULL for a prima
22795 72 79 20 6b 65 79 20 6f 72 20 61 6e 20 69 6e 64  ry key or an ind
22796 65 78 20 74 68 61 74 20 69 73 20 63 72 65 61 74  ex that is creat
22797 65 64 20 74 6f 20 73 61 74 69 73 66 79 20 61 0a  ed to satisfy a.
22798 2a 2a 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  ** UNIQUE constr
22799 61 69 6e 74 2e 20 20 49 66 20 70 54 61 62 6c 65  aint.  If pTable
2279a 20 61 6e 64 20 70 49 6e 64 65 78 20 61 72 65 20   and pIndex are 
2279b 4e 55 4c 4c 2c 20 75 73 65 20 70 50 61 72 73 65  NULL, use pParse
2279c 2d 3e 70 4e 65 77 54 61 62 6c 65 0a 2a 2a 20 61  ->pNewTable.** a
2279d 73 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  s the table to b
2279e 65 20 69 6e 64 65 78 65 64 2e 20 20 70 50 61 72  e indexed.  pPar
2279f 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 73  se->pNewTable is
227a0 20 61 20 74 61 62 6c 65 20 74 68 61 74 20 69 73   a table that is
227a1 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 62 65  .** currently be
227a2 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20  ing constructed 
227a3 62 79 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  by a CREATE TABL
227a4 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  E statement..**.
227a5 2a 2a 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69  ** pList is a li
227a6 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f  st of columns to
227a7 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 70 4c   be indexed.  pL
227a8 69 73 74 20 77 69 6c 6c 20 62 65 20 4e 55 4c 4c  ist will be NULL
227a9 20 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 61   if this.** is a
227aa 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20   primary key or 
227ab 75 6e 69 71 75 65 2d 63 6f 6e 73 74 72 61 69 6e  unique-constrain
227ac 74 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  t on the most re
227ad 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65  cent column adde
227ae 64 0a 2a 2a 20 74 6f 20 74 68 65 20 74 61 62 6c  d.** to the tabl
227af 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65  e currently unde
227b0 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20  r construction. 
227b1 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
227b2 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
227b3 43 72 65 61 74 65 49 6e 64 65 78 28 0a 20 20 50  CreateIndex(.  P
227b4 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
227b5 20 20 2f 2a 20 41 6c 6c 20 69 6e 66 6f 72 6d 61    /* All informa
227b6 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 69 73 20  tion about this 
227b7 70 61 72 73 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e  parse */.  Token
227b8 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a   *pName1,     /*
227b9 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 69   First part of i
227ba 6e 64 65 78 20 6e 61 6d 65 2e 20 4d 61 79 20 62  ndex name. May b
227bb 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65  e NULL */.  Toke
227bc 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f  n *pName2,     /
227bd 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66  * Second part of
227be 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d 61 79   index name. May
227bf 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 53 72   be NULL */.  Sr
227c0 63 4c 69 73 74 20 2a 70 54 62 6c 4e 61 6d 65 2c  cList *pTblName,
227c1 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 69 6e 64   /* Table to ind
227c2 65 78 2e 20 55 73 65 20 70 50 61 72 73 65 2d 3e  ex. Use pParse->
227c3 70 4e 65 77 54 61 62 6c 65 20 69 66 20 30 20 2a  pNewTable if 0 *
227c4 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  /.  ExprList *pL
227c5 69 73 74 2c 20 20 20 2f 2a 20 41 20 6c 69 73 74  ist,   /* A list
227c6 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62   of columns to b
227c7 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69  e indexed */.  i
227c8 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20  nt onError,     
227c9 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 4f    /* OE_Abort, O
227ca 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 70  E_Ignore, OE_Rep
227cb 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65  lace, or OE_None
227cc 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 53 74   */.  Token *pSt
227cd 61 72 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  art,     /* The 
227ce 43 52 45 41 54 45 20 74 6f 6b 65 6e 20 74 68 61  CREATE token tha
227cf 74 20 62 65 67 69 6e 73 20 74 68 69 73 20 73 74  t begins this st
227d0 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b  atement */.  Tok
227d1 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20  en *pEnd,       
227d2 2f 2a 20 54 68 65 20 22 29 22 20 74 68 61 74 20  /* The ")" that 
227d3 63 6c 6f 73 65 73 20 74 68 65 20 43 52 45 41 54  closes the CREAT
227d4 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e  E INDEX statemen
227d5 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f  t */.  int sortO
227d6 72 64 65 72 2c 20 20 20 20 20 2f 2a 20 53 6f 72  rder,     /* Sor
227d7 74 20 6f 72 64 65 72 20 6f 66 20 70 72 69 6d 61  t order of prima
227d8 72 79 20 6b 65 79 20 77 68 65 6e 20 70 4c 69 73  ry key when pLis
227d9 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74  t==NULL */.  int
227da 20 69 66 4e 6f 74 45 78 69 73 74 20 20 20 20 20   ifNotExist     
227db 2f 2a 20 4f 6d 69 74 20 65 72 72 6f 72 20 69 66  /* Omit error if
227dc 20 69 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65   index already e
227dd 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61  xists */.){.  Ta
227de 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20 20  ble *pTab = 0;  
227df 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62     /* Table to b
227e0 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 49  e indexed */.  I
227e1 6e 64 65 78 20 2a 70 49 6e 64 65 78 20 3d 20 30  ndex *pIndex = 0
227e2 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78  ;   /* The index
227e3 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 2a   to be created *
227e4 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  /.  char *zName 
227e5 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65  = 0;     /* Name
227e6 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 2a 2f   of the index */
227e7 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20  .  int nName;   
227e8 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
227e9 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
227ea 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 69 6e  in zName */.  in
227eb 74 20 69 2c 20 6a 3b 0a 20 20 54 6f 6b 65 6e 20  t i, j;.  Token 
227ec 6e 75 6c 6c 49 64 3b 20 20 20 20 20 20 20 20 2f  nullId;        /
227ed 2a 20 46 61 6b 65 20 74 6f 6b 65 6e 20 66 6f 72  * Fake token for
227ee 20 61 6e 20 65 6d 70 74 79 20 49 44 20 6c 69 73   an empty ID lis
227ef 74 20 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73  t */.  DbFixer s
227f0 46 69 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 46  Fix;        /* F
227f1 6f 72 20 61 73 73 69 67 6e 69 6e 67 20 64 61 74  or assigning dat
227f2 61 62 61 73 65 20 6e 61 6d 65 73 20 74 6f 20 70  abase names to p
227f3 54 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73  Table */.  int s
227f4 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 20 20 20  ortOrderMask;   
227f5 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f 72 20 44 45  /* 1 to honor DE
227f6 53 43 20 69 6e 20 69 6e 64 65 78 2e 20 20 30 20  SC in index.  0 
227f7 74 6f 20 69 67 6e 6f 72 65 2e 20 2a 2f 0a 20 20  to ignore. */.  
227f8 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
227f9 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a  arse->db;.  Db *
227fa 70 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  pDb;            
227fb 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63   /* The specific
227fc 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   table containin
227fd 67 20 74 68 65 20 69 6e 64 65 78 65 64 20 64 61  g the indexed da
227fe 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  tabase */.  int 
227ff 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
22800 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
22801 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 69   database that i
22802 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20  s being written 
22803 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
22804 65 20 3d 20 30 3b 20 20 20 20 2f 2a 20 55 6e 71  e = 0;    /* Unq
22805 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f 66  ualified name of
22806 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 63 72   the index to cr
22807 65 61 74 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  eate */.  struct
22808 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
22809 70 4c 69 73 74 49 74 65 6d 3b 20 2f 2a 20 46 6f  pListItem; /* Fo
2280a 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70  r looping over p
2280b 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  List */.  int nC
2280c 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72 61  ol;.  int nExtra
2280d 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 45   = 0;.  char *zE
2280e 78 74 72 61 3b 0a 0a 20 20 69 66 28 20 70 50 61  xtra;..  if( pPa
2280f 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
22810 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
22811 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42   IN_DECLARE_VTAB
22812 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
22813 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
22814 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46    }..  /*.  ** F
22815 69 6e 64 20 74 68 65 20 74 61 62 6c 65 20 74 68  ind the table th
22816 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65  at is to be inde
22817 78 65 64 2e 20 20 52 65 74 75 72 6e 20 65 61 72  xed.  Return ear
22818 6c 79 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e  ly if not found.
22819 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 62 6c  .  */.  if( pTbl
2281a 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a 20 20 20 20  Name!=0 ){..    
2281b 2f 2a 20 55 73 65 20 74 68 65 20 74 77 6f 2d 70  /* Use the two-p
2281c 61 72 74 20 69 6e 64 65 78 20 6e 61 6d 65 20 74  art index name t
2281d 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
2281e 64 61 74 61 62 61 73 65 20 0a 20 20 20 20 2a 2a  database .    **
2281f 20 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20 74   to search for t
22820 68 65 20 74 61 62 6c 65 2e 20 27 46 69 78 27 20  he table. 'Fix' 
22821 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 74  the table name t
22822 6f 20 74 68 69 73 20 64 62 0a 20 20 20 20 2a 2a  o this db.    **
22823 20 62 65 66 6f 72 65 20 6c 6f 6f 6b 69 6e 67 20   before looking 
22824 75 70 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  up the table..  
22825 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
22826 20 70 4e 61 6d 65 31 20 26 26 20 70 4e 61 6d 65   pName1 && pName
22827 32 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20 73  2 );.    iDb = s
22828 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
22829 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
2282a 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65  , pName2, &pName
2282b 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30  );.    if( iDb<0
2282c 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
2282d 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 23 69 66 6e  ate_index;..#ifn
2282e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2282f 54 45 4d 50 44 42 0a 20 20 20 20 2f 2a 20 49 66  TEMPDB.    /* If
22830 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 20   the index name 
22831 77 61 73 20 75 6e 71 75 61 6c 69 66 69 65 64 2c  was unqualified,
22832 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 74 68   check if the th
22833 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69  e table.    ** i
22834 73 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2e 20  s a temp table. 
22835 49 66 20 73 6f 2c 20 73 65 74 20 74 68 65 20 64  If so, set the d
22836 61 74 61 62 61 73 65 20 74 6f 20 31 2e 20 44 6f  atabase to 1. Do
22837 20 6e 6f 74 20 64 6f 20 74 68 69 73 0a 20 20 20   not do this.   
22838 20 2a 2a 20 69 66 20 69 6e 69 74 69 61 6c 69 73   ** if initialis
22839 69 6e 67 20 61 20 64 61 74 61 62 61 73 65 20 73  ing a database s
2283a 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20  chema..    */.  
2283b 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
2283c 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 70 54  busy ){.      pT
2283d 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  ab = sqlite3SrcL
2283e 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  istLookup(pParse
2283f 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 20  , pTblName);.   
22840 20 20 20 69 66 28 20 70 4e 61 6d 65 32 20 26 26     if( pName2 &&
22841 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26   pName2->n==0 &&
22842 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70   pTab && pTab->p
22843 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b  Schema==db->aDb[
22844 31 5d 2e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  1].pSchema ){.  
22845 20 20 20 20 20 20 69 44 62 20 3d 20 31 3b 0a 20        iDb = 1;. 
22846 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
22847 64 69 66 0a 0a 20 20 20 20 69 66 28 20 73 71 6c  dif..    if( sql
22848 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 69  ite3FixInit(&sFi
22849 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20  x, pParse, iDb, 
2284a 22 69 6e 64 65 78 22 2c 20 70 4e 61 6d 65 29 20  "index", pName) 
2284b 26 26 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  &&.        sqlit
2284c 65 33 46 69 78 53 72 63 4c 69 73 74 28 26 73 46  e3FixSrcList(&sF
2284d 69 78 2c 20 70 54 62 6c 4e 61 6d 65 29 0a 20 20  ix, pTblName).  
2284e 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 42 65    ){.      /* Be
2284f 63 61 75 73 65 20 74 68 65 20 70 61 72 73 65 72  cause the parser
22850 20 63 6f 6e 73 74 72 75 63 74 73 20 70 54 62 6c   constructs pTbl
22851 4e 61 6d 65 20 66 72 6f 6d 20 61 20 73 69 6e 67  Name from a sing
22852 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2c 0a 20  le identifier,. 
22853 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 46       ** sqlite3F
22854 69 78 53 72 63 4c 69 73 74 20 63 61 6e 20 6e 65  ixSrcList can ne
22855 76 65 72 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 20  ver fail. */.   
22856 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20 20     assert(0);.  
22857 20 20 7d 0a 20 20 20 20 70 54 61 62 20 3d 20 73    }.    pTab = s
22858 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
22859 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 54 62  e(pParse, 0, pTb
2285a 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  lName->a[0].zNam
2285b 65 2c 20 0a 20 20 20 20 20 20 20 20 70 54 62 6c  e, .        pTbl
2285c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61  Name->a[0].zData
2285d 62 61 73 65 29 3b 0a 20 20 20 20 69 66 28 20 21  base);.    if( !
2285e 70 54 61 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  pTab || db->mall
2285f 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20  ocFailed ) goto 
22860 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
22861 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  x;.    assert( d
22862 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
22863 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65  ema==pTab->pSche
22864 6d 61 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ma );.  }else{. 
22865 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65     assert( pName
22866 3d 3d 30 20 29 3b 0a 20 20 20 20 70 54 61 62 20  ==0 );.    pTab 
22867 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
22868 62 6c 65 3b 0a 20 20 20 20 69 66 28 20 21 70 54  ble;.    if( !pT
22869 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63  ab ) goto exit_c
2286a 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
2286b 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
2286c 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
2286d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
2286e 20 20 7d 0a 20 20 70 44 62 20 3d 20 26 64 62 2d    }.  pDb = &db-
2286f 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 69 66  >aDb[iDb];..  if
22870 28 20 70 54 61 62 3d 3d 30 20 7c 7c 20 70 50 61  ( pTab==0 || pPa
22871 72 73 65 2d 3e 6e 45 72 72 20 29 20 67 6f 74 6f  rse->nErr ) goto
22872 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
22873 65 78 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  ex;.  if( sqlite
22874 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e  3StrNICmp(pTab->
22875 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22  zName, "sqlite_"
22876 2c 20 37 29 3d 3d 30 20 0a 20 20 20 20 20 20 20  , 7)==0 .       
22877 26 26 20 6d 65 6d 63 6d 70 28 26 70 54 61 62 2d  && memcmp(&pTab-
22878 3e 7a 4e 61 6d 65 5b 37 5d 2c 22 61 6c 74 65 72  >zName[7],"alter
22879 74 61 62 5f 22 2c 39 29 21 3d 30 20 29 7b 0a 20  tab_",9)!=0 ){. 
2287a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2287b 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c  sg(pParse, "tabl
2287c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  e %s may not be 
2287d 69 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d 3e  indexed", pTab->
2287e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
2287f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
22880 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20  ex;.  }.#ifndef 
22881 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
22882 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65  .  if( pTab->pSe
22883 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  lect ){.    sqli
22884 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
22885 73 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20 6e  se, "views may n
22886 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b  ot be indexed");
22887 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63  .    goto exit_c
22888 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d  reate_index;.  }
22889 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
2288a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2288b 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49  UALTABLE.  if( I
2288c 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2288d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2288e 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76  orMsg(pParse, "v
2288f 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 61  irtual tables ma
22890 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64  y not be indexed
22891 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  ");.    goto exi
22892 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
22893 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
22894 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e  .  ** Find the n
22895 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ame of the index
22896 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  .  Make sure the
22897 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  re is not alread
22898 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20 69  y another.  ** i
22899 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77 69  ndex or table wi
2289a 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  th the same name
2289b 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78  .  .  **.  ** Ex
2289c 63 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 20  ception:  If we 
2289d 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20  are reading the 
2289e 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e 65  names of permane
2289f 6e 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d 20  nt indices from 
228a0 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f  the.  ** sqlite_
228a1 6d 61 73 74 65 72 20 74 61 62 6c 65 20 28 62 65  master table (be
228a2 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 72  cause some other
228a3 20 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65 64   process changed
228a4 20 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e 64   the schema) and
228a5 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65  .  ** one of the
228a6 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f 6c   index names col
228a7 6c 69 64 65 73 20 77 69 74 68 20 74 68 65 20 6e  lides with the n
228a8 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61  ame of a tempora
228a9 72 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a  ry table or.  **
228aa 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65 20   index, then we 
228ab 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f  will continue to
228ac 20 70 72 6f 63 65 73 73 20 74 68 69 73 20 69 6e   process this in
228ad 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  dex..  **.  ** I
228ae 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65  f pName==0 it me
228af 61 6e 73 20 74 68 61 74 20 77 65 20 61 72 65 0a  ans that we are.
228b0 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69 74    ** dealing wit
228b1 68 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20  h a primary key 
228b2 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  or UNIQUE constr
228b3 61 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20 74  aint.  We have t
228b4 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20 2a  o invent our.  *
228b5 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a 2f  * own name..  */
228b6 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a  .  if( pName ){.
228b7 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69      zName = sqli
228b8 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
228b9 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20  (db, pName);.   
228ba 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
228bb 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
228bc 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
228bd 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
228be 65 78 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d  ex;.    if( zNam
228bf 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74  e==0 ) goto exit
228c0 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
228c1 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
228c2 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62  !=sqlite3CheckOb
228c3 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  jectName(pParse,
228c4 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20   zName) ){.     
228c5 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
228c6 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
228c7 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74     if( !db->init
228c8 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 69  .busy ){.      i
228c9 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
228ca 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
228cb 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 65  pParse) ) goto e
228cc 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
228cd 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
228ce 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
228cf 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29 7b   zName, 0)!=0 ){
228d0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
228d1 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
228d2 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61   "there is alrea
228d3 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 64  dy a table named
228d4 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
228d5 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
228d6 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
228d7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
228d8 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49  if( sqlite3FindI
228d9 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  ndex(db, zName, 
228da 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 29  pDb->zName)!=0 )
228db 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 66 4e  {.      if( !ifN
228dc 6f 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20 20  otExist ){.     
228dd 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
228de 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64 65  sg(pParse, "inde
228df 78 20 25 73 20 61 6c 72 65 61 64 79 20 65 78 69  x %s already exi
228e0 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  sts", zName);.  
228e1 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f      }.      goto
228e2 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
228e3 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ex;.    }.  }els
228e4 65 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20  e{.    int n;.  
228e5 20 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b 0a    Index *pLoop;.
228e6 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 54      for(pLoop=pT
228e7 61 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31 3b  ab->pIndex, n=1;
228e8 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c   pLoop; pLoop=pL
228e9 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b 29  oop->pNext, n++)
228ea 7b 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73  {}.    zName = s
228eb 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
228ec 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e  , "sqlite_autoin
228ed 64 65 78 5f 25 73 5f 25 64 22 2c 20 70 54 61 62  dex_%s_%d", pTab
228ee 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20  ->zName, n);.   
228ef 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b   if( zName==0 ){
228f0 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
228f1 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
228f2 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43     }.  }..  /* C
228f3 68 65 63 6b 20 66 6f 72 20 61 75 74 68 6f 72 69  heck for authori
228f4 7a 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65  zation to create
228f5 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a   an index..  */.
228f6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
228f7 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
228f8 4e 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20  N.  {.    const 
228f9 63 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d  char *zDb = pDb-
228fa 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20  >zName;.    if( 
228fb 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
228fc 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
228fd 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54  INSERT, SCHEMA_T
228fe 41 42 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a 44  ABLE(iDb), 0, zD
228ff 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
22900 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
22901 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20  ex;.    }.    i 
22902 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  = SQLITE_CREATE_
22903 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 21  INDEX;.    if( !
22904 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
22905 44 62 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c 49  Db==1 ) i = SQLI
22906 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49  TE_CREATE_TEMP_I
22907 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71  NDEX;.    if( sq
22908 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
22909 50 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 2c  Parse, i, zName,
2290a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44   pTab->zName, zD
2290b 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
2290c 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
2290d 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  ex;.    }.  }.#e
2290e 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 4c  ndif..  /* If pL
2290f 69 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73  ist==0, it means
22910 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
22911 73 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65  s called to make
22912 20 61 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20   a primary.  ** 
22913 6b 65 79 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  key out of the l
22914 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64  ast column added
22915 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 75 6e   to the table un
22916 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
22917 2e 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 74 65  ..  ** So create
22918 20 61 20 66 61 6b 65 20 6c 69 73 74 20 74 6f 20   a fake list to 
22919 73 69 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a 20  simulate this.. 
2291a 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   */.  if( pList=
2291b 3d 30 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 64  =0 ){.    nullId
2291c 2e 7a 20 3d 20 28 75 38 2a 29 70 54 61 62 2d 3e  .z = (u8*)pTab->
2291d 61 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d  aCol[pTab->nCol-
2291e 31 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75  1].zName;.    nu
2291f 6c 6c 49 64 2e 6e 20 3d 20 73 71 6c 69 74 65 33  llId.n = sqlite3
22920 53 74 72 6c 65 6e 33 30 28 28 63 68 61 72 2a 29  Strlen30((char*)
22921 6e 75 6c 6c 49 64 2e 7a 29 3b 0a 20 20 20 20 6e  nullId.z);.    n
22922 75 6c 6c 49 64 2e 71 75 6f 74 65 64 20 3d 20 30  ullId.quoted = 0
22923 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71  ;.    pList = sq
22924 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
22925 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 30  end(pParse, 0, 0
22926 2c 20 26 6e 75 6c 6c 49 64 29 3b 0a 20 20 20 20  , &nullId);.    
22927 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 67  if( pList==0 ) g
22928 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
22929 69 6e 64 65 78 3b 0a 20 20 20 20 70 4c 69 73 74  index;.    pList
2292a 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72  ->a[0].sortOrder
2292b 20 3d 20 28 75 38 29 73 6f 72 74 4f 72 64 65 72   = (u8)sortOrder
2292c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75  ;.  }..  /* Figu
2292d 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
2292e 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61  bytes of space a
2292f 72 65 20 72 65 71 75 69 72 65 64 20 74 6f 20 73  re required to s
22930 74 6f 72 65 20 65 78 70 6c 69 63 69 74 6c 79 0a  tore explicitly.
22931 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 63    ** specified c
22932 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
22933 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20  e names..  */.  
22934 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
22935 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
22936 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a     Expr *pExpr;.
22937 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
22938 6c 6c 3b 0a 20 20 20 20 69 66 28 20 28 70 45 78  ll;.    if( (pEx
22939 70 72 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  pr = pList->a[i]
2293a 2e 70 45 78 70 72 29 21 3d 30 20 26 26 20 28 70  .pExpr)!=0 && (p
2293b 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43  Coll = pExpr->pC
2293c 6f 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  oll)!=0 ){.     
2293d 20 6e 45 78 74 72 61 20 2b 3d 20 28 31 20 2b 20   nExtra += (1 + 
2293e 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2293f 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 29 3b 0a  pColl->zName));.
22940 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
22941 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 74  .  ** Allocate t
22942 68 65 20 69 6e 64 65 78 20 73 74 72 75 63 74 75  he index structu
22943 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e 4e 61 6d  re. .  */.  nNam
22944 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  e = sqlite3Strle
22945 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 6e 43  n30(zName);.  nC
22946 6f 6c 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70  ol = pList->nExp
22947 72 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71  r;.  pIndex = sq
22948 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
22949 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 73 69 7a  o(db, .      siz
2294a 65 6f 66 28 49 6e 64 65 78 29 20 2b 20 20 20 20  eof(Index) +    
2294b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
2294c 65 78 20 73 74 72 75 63 74 75 72 65 20 20 2a 2f  ex structure  */
2294d 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 69 6e  .      sizeof(in
2294e 74 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20  t)*nCol +       
2294f 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 43      /* Index.aiC
22950 6f 6c 75 6d 6e 20 20 20 2a 2f 0a 20 20 20 20 20  olumn   */.     
22951 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 43   sizeof(int)*(nC
22952 6f 6c 2b 31 29 20 2b 20 20 20 20 20 20 20 2f 2a  ol+1) +       /*
22953 20 49 6e 64 65 78 2e 61 69 52 6f 77 45 73 74 20   Index.aiRowEst 
22954 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f    */.      sizeo
22955 66 28 63 68 61 72 20 2a 29 2a 6e 43 6f 6c 20 2b  f(char *)*nCol +
22956 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
22957 2e 61 7a 43 6f 6c 6c 20 20 20 20 20 2a 2f 0a 20  .azColl     */. 
22958 20 20 20 20 20 73 69 7a 65 6f 66 28 75 38 29 2a       sizeof(u8)*
22959 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 20 20  nCol +          
2295a 20 20 2f 2a 20 49 6e 64 65 78 2e 61 53 6f 72 74    /* Index.aSort
2295b 4f 72 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 6e  Order */.      n
2295c 4e 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20  Name + 1 +      
2295d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2295e 6e 64 65 78 2e 7a 4e 61 6d 65 20 20 20 20 20 20  ndex.zName      
2295f 2a 2f 0a 20 20 20 20 20 20 6e 45 78 74 72 61 20  */.      nExtra 
22960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22961 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69        /* Collati
22962 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
22963 73 20 2a 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20  s */.  );.  if( 
22964 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
22965 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
22966 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
22967 20 20 7d 0a 20 20 70 49 6e 64 65 78 2d 3e 61 7a    }.  pIndex->az
22968 43 6f 6c 6c 20 3d 20 28 63 68 61 72 2a 2a 29 28  Coll = (char**)(
22969 26 70 49 6e 64 65 78 5b 31 5d 29 3b 0a 20 20 70  &pIndex[1]);.  p
2296a 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 20  Index->aiColumn 
2296b 3d 20 28 69 6e 74 20 2a 29 28 26 70 49 6e 64 65  = (int *)(&pInde
2296c 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29  x->azColl[nCol])
2296d 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 69 52 6f  ;.  pIndex->aiRo
2296e 77 45 73 74 20 3d 20 28 75 6e 73 69 67 6e 65 64  wEst = (unsigned
2296f 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 69 43   *)(&pIndex->aiC
22970 6f 6c 75 6d 6e 5b 6e 43 6f 6c 5d 29 3b 0a 20 20  olumn[nCol]);.  
22971 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64  pIndex->aSortOrd
22972 65 72 20 3d 20 28 75 38 20 2a 29 28 26 70 49 6e  er = (u8 *)(&pIn
22973 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b 6e 43  dex->aiRowEst[nC
22974 6f 6c 2b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78  ol+1]);.  pIndex
22975 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 20  ->zName = (char 
22976 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 53 6f 72  *)(&pIndex->aSor
22977 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d 29 3b 0a 20  tOrder[nCol]);. 
22978 20 7a 45 78 74 72 61 20 3d 20 28 63 68 61 72 20   zExtra = (char 
22979 2a 29 28 26 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d  *)(&pIndex->zNam
2297a 65 5b 6e 4e 61 6d 65 2b 31 5d 29 3b 0a 20 20 6d  e[nName+1]);.  m
2297b 65 6d 63 70 79 28 70 49 6e 64 65 78 2d 3e 7a 4e  emcpy(pIndex->zN
2297c 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ame, zName, nNam
2297d 65 2b 31 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  e+1);.  pIndex->
2297e 70 54 61 62 6c 65 20 3d 20 70 54 61 62 3b 0a 20  pTable = pTab;. 
2297f 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e   pIndex->nColumn
22980 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   = pList->nExpr;
22981 0a 20 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72  .  pIndex->onErr
22982 6f 72 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72  or = (u8)onError
22983 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 75 74 6f  ;.  pIndex->auto
22984 49 6e 64 65 78 20 3d 20 28 75 38 29 28 70 4e 61  Index = (u8)(pNa
22985 6d 65 3d 3d 30 29 3b 0a 20 20 70 49 6e 64 65 78  me==0);.  pIndex
22986 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e  ->pSchema = db->
22987 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
22988 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  ;..  /* Check to
22989 20 73 65 65 20 69 66 20 77 65 20 73 68 6f 75 6c   see if we shoul
2298a 64 20 68 6f 6e 6f 72 20 44 45 53 43 20 72 65 71  d honor DESC req
2298b 75 65 73 74 73 20 6f 6e 20 69 6e 64 65 78 20 63  uests on index c
2298c 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66  olumns.  */.  if
2298d 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
2298e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 29  file_format>=4 )
2298f 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d  {.    sortOrderM
22990 61 73 6b 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 48  ask = -1;   /* H
22991 6f 6e 6f 72 20 44 45 53 43 20 2a 2f 0a 20 20 7d  onor DESC */.  }
22992 65 6c 73 65 7b 0a 20 20 20 20 73 6f 72 74 4f 72  else{.    sortOr
22993 64 65 72 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20  derMask = 0;    
22994 2f 2a 20 49 67 6e 6f 72 65 20 44 45 53 43 20 2a  /* Ignore DESC *
22995 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 63 61 6e  /.  }..  /* Scan
22996 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68   the names of th
22997 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  e columns of the
22998 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   table to be ind
22999 65 78 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f  exed and.  ** lo
2299a 61 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e  ad the column in
2299b 64 69 63 65 73 20 69 6e 74 6f 20 74 68 65 20 49  dices into the I
2299c 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 2e 20  ndex structure. 
2299d 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72   Report an error
2299e 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20 63 6f 6c  .  ** if any col
2299f 75 6d 6e 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64  umn is not found
229a0 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
229a1 2c 20 70 4c 69 73 74 49 74 65 6d 3d 70 4c 69 73  , pListItem=pLis
229a2 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
229a3 45 78 70 72 3b 20 69 2b 2b 2c 20 70 4c 69 73 74  Expr; i++, pList
229a4 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e  Item++){.    con
229a5 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 4e 61 6d  st char *zColNam
229a6 65 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 7a  e = pListItem->z
229a7 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e  Name;.    Column
229a8 20 2a 70 54 61 62 43 6f 6c 3b 0a 20 20 20 20 69   *pTabCol;.    i
229a9 6e 74 20 72 65 71 75 65 73 74 65 64 53 6f 72 74  nt requestedSort
229aa 4f 72 64 65 72 3b 0a 20 20 20 20 63 68 61 72 20  Order;.    char 
229ab 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  *zColl;         
229ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
229ad 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
229ae 6e 61 6d 65 20 2a 2f 0a 0a 20 20 20 20 66 6f 72  name */..    for
229af 28 6a 3d 30 2c 20 70 54 61 62 43 6f 6c 3d 70 54  (j=0, pTabCol=pT
229b0 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62  ab->aCol; j<pTab
229b1 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 54 61  ->nCol; j++, pTa
229b2 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 69  bCol++){.      i
229b3 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
229b4 70 28 7a 43 6f 6c 4e 61 6d 65 2c 20 70 54 61 62  p(zColName, pTab
229b5 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29  Col->zName)==0 )
229b6 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
229b7 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e    if( j>=pTab->n
229b8 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Col ){.      sql
229b9 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
229ba 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 68  rse, "table %s h
229bb 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d  as no column nam
229bc 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20  ed %s",.        
229bd 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  pTab->zName, zCo
229be 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f  lName);.      go
229bf 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
229c0 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
229c1 2f 2a 20 54 4f 44 4f 3a 20 20 41 64 64 20 61 20  /* TODO:  Add a 
229c2 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72  test to make sur
229c3 65 20 74 68 61 74 20 74 68 65 20 73 61 6d 65 20  e that the same 
229c4 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 6e 61  column is not na
229c5 6d 65 64 0a 20 20 20 20 2a 2a 20 6d 6f 72 65 20  med.    ** more 
229c6 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 69 6e  than once within
229c7 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 2e   the same index.
229c8 20 20 4f 6e 6c 79 20 74 68 65 20 66 69 72 73 74    Only the first
229c9 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 20 20   instance of.   
229ca 20 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77   ** the column w
229cb 69 6c 6c 20 65 76 65 72 20 62 65 20 75 73 65 64  ill ever be used
229cc 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65   by the optimize
229cd 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 75 73  r.  Note that us
229ce 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 73  ing the.    ** s
229cf 61 6d 65 20 63 6f 6c 75 6d 6e 20 6d 6f 72 65 20  ame column more 
229d0 74 68 61 6e 20 6f 6e 63 65 20 63 61 6e 6e 6f 74  than once cannot
229d1 20 62 65 20 61 6e 20 65 72 72 6f 72 20 62 65 63   be an error bec
229d2 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20  ause that would 
229d3 0a 20 20 20 20 2a 2a 20 62 72 65 61 6b 20 62 61  .    ** break ba
229d4 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
229d5 69 6c 69 74 79 20 2d 20 69 74 20 6e 65 65 64 73  ility - it needs
229d6 20 74 6f 20 62 65 20 61 20 77 61 72 6e 69 6e 67   to be a warning
229d7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e  ..    */.    pIn
229d8 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  dex->aiColumn[i]
229d9 20 3d 20 6a 3b 0a 20 20 20 20 69 66 28 20 70 4c   = j;.    if( pL
229da 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 20 26  istItem->pExpr &
229db 26 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78  & pListItem->pEx
229dc 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  pr->pColl ){.   
229dd 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
229de 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f  Item->pExpr->pCo
229df 6c 6c 20 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c  ll );.      zCol
229e0 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 20 20  l = zExtra;.    
229e1 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
229e2 74 66 28 6e 45 78 74 72 61 2c 20 7a 45 78 74 72  tf(nExtra, zExtr
229e3 61 2c 20 22 25 73 22 2c 20 70 4c 69 73 74 49 74  a, "%s", pListIt
229e4 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c  em->pExpr->pColl
229e5 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
229e6 7a 45 78 74 72 61 20 2b 3d 20 28 73 71 6c 69 74  zExtra += (sqlit
229e7 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 6f 6c 6c  e3Strlen30(zColl
229e8 29 20 2b 20 31 29 3b 0a 20 20 20 20 7d 65 6c 73  ) + 1);.    }els
229e9 65 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d  e{.      zColl =
229ea 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a   pTab->aCol[j].z
229eb 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 66 28 20  Coll;.      if( 
229ec 21 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  !zColl ){.      
229ed 20 20 7a 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44    zColl = db->pD
229ee 66 6c 74 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a  fltColl->zName;.
229ef 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
229f0 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
229f1 62 75 73 79 20 26 26 20 21 73 71 6c 69 74 65 33  busy && !sqlite3
229f2 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50  LocateCollSeq(pP
229f3 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29  arse, zColl, -1)
229f4 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   ){.      goto e
229f5 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
229f6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64  ;.    }.    pInd
229f7 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 20 3d 20  ex->azColl[i] = 
229f8 7a 43 6f 6c 6c 3b 0a 20 20 20 20 72 65 71 75 65  zColl;.    reque
229f9 73 74 65 64 53 6f 72 74 4f 72 64 65 72 20 3d 20  stedSortOrder = 
229fa 70 4c 69 73 74 49 74 65 6d 2d 3e 73 6f 72 74 4f  pListItem->sortO
229fb 72 64 65 72 20 26 20 73 6f 72 74 4f 72 64 65 72  rder & sortOrder
229fc 4d 61 73 6b 3b 0a 20 20 20 20 70 49 6e 64 65 78  Mask;.    pIndex
229fd 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20  ->aSortOrder[i] 
229fe 3d 20 28 75 38 29 72 65 71 75 65 73 74 65 64 53  = (u8)requestedS
229ff 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 20 20  ortOrder;.  }.  
22a00 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f  sqlite3DefaultRo
22a01 77 45 73 74 28 70 49 6e 64 65 78 29 3b 0a 0a 20  wEst(pIndex);.. 
22a02 20 69 66 28 20 70 54 61 62 3d 3d 70 50 61 72 73   if( pTab==pPars
22a03 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 29 7b 0a  e->pNewTable ){.
22a04 20 20 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74      /* This rout
22a05 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c  ine has been cal
22a06 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e  led to create an
22a07 20 61 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78   automatic index
22a08 20 61 73 20 61 0a 20 20 20 20 2a 2a 20 72 65 73   as a.    ** res
22a09 75 6c 74 20 6f 66 20 61 20 50 52 49 4d 41 52 59  ult of a PRIMARY
22a0a 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63   KEY or UNIQUE c
22a0b 6c 61 75 73 65 20 6f 6e 20 61 20 63 6f 6c 75 6d  lause on a colum
22a0c 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 6f 72  n definition, or
22a0d 0a 20 20 20 20 2a 2a 20 61 20 50 52 49 4d 41 52  .    ** a PRIMAR
22a0e 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20  Y KEY or UNIQUE 
22a0f 63 6c 61 75 73 65 20 66 6f 6c 6c 6f 77 69 6e 67  clause following
22a10 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69   the column defi
22a11 6e 69 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2a 20  nitions..    ** 
22a12 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a 0a 20 20 20  i.e. one of:.   
22a13 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 52 45 41 54   **.    ** CREAT
22a14 45 20 54 41 42 4c 45 20 74 28 78 20 50 52 49 4d  E TABLE t(x PRIM
22a15 41 52 59 20 4b 45 59 2c 20 79 29 3b 0a 20 20 20  ARY KEY, y);.   
22a16 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45   ** CREATE TABLE
22a17 20 74 28 78 2c 20 79 2c 20 55 4e 49 51 55 45 28   t(x, y, UNIQUE(
22a18 78 2c 20 79 29 29 3b 0a 20 20 20 20 2a 2a 0a 20  x, y));.    **. 
22a19 20 20 20 2a 2a 20 45 69 74 68 65 72 20 77 61 79     ** Either way
22a1a 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  , check to see i
22a1b 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65  f the table alre
22a1c 61 64 79 20 68 61 73 20 73 75 63 68 20 61 6e 20  ady has such an 
22a1d 69 6e 64 65 78 2e 20 49 66 0a 20 20 20 20 2a 2a  index. If.    **
22a1e 20 73 6f 2c 20 64 6f 6e 27 74 20 62 6f 74 68 65   so, don't bothe
22a1f 72 20 63 72 65 61 74 69 6e 67 20 74 68 69 73 20  r creating this 
22a20 6f 6e 65 2e 20 54 68 69 73 20 6f 6e 6c 79 20 61  one. This only a
22a21 70 70 6c 69 65 73 20 74 6f 0a 20 20 20 20 2a 2a  pplies to.    **
22a22 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
22a23 72 65 61 74 65 64 20 69 6e 64 69 63 65 73 2e 20  reated indices. 
22a24 55 73 65 72 73 20 63 61 6e 20 64 6f 20 61 73 20  Users can do as 
22a25 74 68 65 79 20 77 69 73 68 20 77 69 74 68 0a 20  they wish with. 
22a26 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 20 69     ** explicit i
22a27 6e 64 69 63 65 73 2e 0a 20 20 20 20 2a 2f 0a 20  ndices..    */. 
22a28 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
22a29 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
22a2a 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
22a2b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
22a2c 74 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b  t){.      int k;
22a2d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
22a2e 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45  Idx->onError!=OE
22a2f 5f 4e 6f 6e 65 20 29 3b 0a 20 20 20 20 20 20 61  _None );.      a
22a30 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61 75 74  ssert( pIdx->aut
22a31 6f 49 6e 64 65 78 20 29 3b 0a 20 20 20 20 20 20  oIndex );.      
22a32 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e  assert( pIndex->
22a33 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
22a34 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70   );..      if( p
22a35 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 49  Idx->nColumn!=pI
22a36 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 20  ndex->nColumn ) 
22a37 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
22a38 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 49 64 78 2d  for(k=0; k<pIdx-
22a39 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 29 7b 0a  >nColumn; k++){.
22a3a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
22a3b 61 72 20 2a 7a 31 20 3d 20 70 49 64 78 2d 3e 61  ar *z1 = pIdx->a
22a3c 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20  zColl[k];.      
22a3d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32    const char *z2
22a3e 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c   = pIndex->azCol
22a3f 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  l[k];.        if
22a40 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ( pIdx->aiColumn
22a41 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 69 43  [k]!=pIndex->aiC
22a42 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72 65 61 6b  olumn[k] ) break
22a43 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  ;.        if( pI
22a44 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6b  dx->aSortOrder[k
22a45 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74  ]!=pIndex->aSort
22a46 4f 72 64 65 72 5b 6b 5d 20 29 20 62 72 65 61 6b  Order[k] ) break
22a47 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 31  ;.        if( z1
22a48 21 3d 7a 32 20 26 26 20 73 71 6c 69 74 65 33 53  !=z2 && sqlite3S
22a49 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29 20 29  trICmp(z1, z2) )
22a4a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
22a4b 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 70 49 64        if( k==pId
22a4c 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  x->nColumn ){.  
22a4d 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
22a4e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e 64 65 78 2d  onError!=pIndex-
22a4f 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20  >onError ){.    
22a50 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f        /* This co
22a51 6e 73 74 72 61 69 6e 74 20 63 72 65 61 74 65 73  nstraint creates
22a52 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 20   the same index 
22a53 61 73 20 61 20 70 72 65 76 69 6f 75 73 0a 20 20  as a previous.  
22a54 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74          ** const
22a55 72 61 69 6e 74 20 73 70 65 63 69 66 69 65 64 20  raint specified 
22a56 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74 68 65  somewhere in the
22a57 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
22a58 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 20  atement..       
22a59 20 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 74 68     ** However th
22a5a 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c  e ON CONFLICT cl
22a5b 61 75 73 65 73 20 61 72 65 20 64 69 66 66 65 72  auses are differ
22a5c 65 6e 74 2e 20 49 66 20 62 6f 74 68 20 74 68 69  ent. If both thi
22a5d 73 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  s .          ** 
22a5e 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74  constraint and t
22a5f 68 65 20 70 72 65 76 69 6f 75 73 20 65 71 75 69  he previous equi
22a60 76 61 6c 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e  valent constrain
22a61 74 20 68 61 76 65 20 65 78 70 6c 69 63 69 74 0a  t have explicit.
22a62 20 20 20 20 20 20 20 20 20 20 2a 2a 20 4f 4e 20            ** ON 
22a63 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73  CONFLICT clauses
22a64 20 74 68 69 73 20 69 73 20 61 6e 20 65 72 72 6f   this is an erro
22a65 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 75 73  r. Otherwise, us
22a66 65 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  e the.          
22a67 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70  ** explicitly sp
22a68 65 63 69 66 69 65 64 20 62 65 68 61 76 69 6f 75  ecified behaviou
22a69 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 2e  r for the index.
22a6a 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
22a6b 20 20 20 20 20 20 20 20 69 66 28 20 21 28 70 49          if( !(pI
22a6c 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  dx->onError==OE_
22a6d 44 65 66 61 75 6c 74 20 7c 7c 20 70 49 6e 64 65  Default || pInde
22a6e 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  x->onError==OE_D
22a6f 65 66 61 75 6c 74 29 20 29 7b 0a 20 20 20 20 20  efault) ){.     
22a70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
22a71 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
22a72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a73 22 63 6f 6e 66 6c 69 63 74 69 6e 67 20 4f 4e 20  "conflicting ON 
22a74 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73  CONFLICT clauses
22a75 20 73 70 65 63 69 66 69 65 64 22 2c 20 30 29 3b   specified", 0);
22a76 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
22a77 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
22a78 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66  >onError==OE_Def
22a79 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20  ault ){.        
22a7a 20 20 20 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f      pIdx->onErro
22a7b 72 20 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72  r = pIndex->onEr
22a7c 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ror;.          }
22a7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
22a7e 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
22a7f 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20  ate_index;.     
22a80 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
22a81 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65 77 20  /* Link the new 
22a82 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20  Index structure 
22a83 74 6f 20 69 74 73 20 74 61 62 6c 65 20 61 6e 64  to its table and
22a84 20 74 6f 20 74 68 65 20 6f 74 68 65 72 0a 20 20   to the other.  
22a85 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  ** in-memory dat
22a86 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 73  abase structures
22a87 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  . .  */.  if( db
22a88 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
22a89 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20     Index *p;.   
22a8a 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68   p = sqlite3Hash
22a8b 49 6e 73 65 72 74 28 26 70 49 6e 64 65 78 2d 3e  Insert(&pIndex->
22a8c 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
22a8d 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
22a8e 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 6e               pIn
22a8f 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69  dex->zName, sqli
22a90 74 65 33 53 74 72 6c 65 6e 33 30 28 70 49 6e 64  te3Strlen30(pInd
22a91 65 78 2d 3e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20  ex->zName),.    
22a92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a93 20 20 20 20 20 20 70 49 6e 64 65 78 29 3b 0a 20        pIndex);. 
22a94 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
22a95 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 49 6e    assert( p==pIn
22a96 64 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f  dex );  /* Mallo
22a97 63 20 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c  c must have fail
22a98 65 64 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e  ed */.      db->
22a99 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
22a9a 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  ;.      goto exi
22a9b 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
22a9c 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 66 6c      }.    db->fl
22a9d 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e  ags |= SQLITE_In
22a9e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 20  ternChanges;.   
22a9f 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21 3d 30   if( pTblName!=0
22aa0 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78   ){.      pIndex
22aa1 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e 69  ->tnum = db->ini
22aa2 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 20 20 7d  t.newTnum;.    }
22aa3 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
22aa4 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  e db->init.busy 
22aa5 69 73 20 30 20 74 68 65 6e 20 63 72 65 61 74 65  is 0 then create
22aa6 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 64 69   the index on di
22aa7 73 6b 2e 20 20 54 68 69 73 0a 20 20 2a 2a 20 69  sk.  This.  ** i
22aa8 6e 76 6f 6c 76 65 73 20 77 72 69 74 69 6e 67 20  nvolves writing 
22aa9 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 74  the index into t
22aaa 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20  he master table 
22aab 61 6e 64 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74  and filling in t
22aac 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 77 69  he.  ** index wi
22aad 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  th the current t
22aae 61 62 6c 65 20 63 6f 6e 74 65 6e 74 73 2e 0a 20  able contents.. 
22aaf 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 62 2d   **.  ** The db-
22ab0 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30 20  >init.busy is 0 
22ab1 77 68 65 6e 20 74 68 65 20 75 73 65 72 20 66 69  when the user fi
22ab2 72 73 74 20 65 6e 74 65 72 73 20 61 20 43 52 45  rst enters a CRE
22ab3 41 54 45 20 49 4e 44 45 58 20 0a 20 20 2a 2a 20  ATE INDEX .  ** 
22ab4 63 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e 69 6e  command.  db->in
22ab5 69 74 2e 62 75 73 79 20 69 73 20 31 20 77 68 65  it.busy is 1 whe
22ab6 6e 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20  n a database is 
22ab7 6f 70 65 6e 65 64 20 61 6e 64 20 0a 20 20 2a 2a  opened and .  **
22ab8 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74   CREATE INDEX st
22ab9 61 74 65 6d 65 6e 74 73 20 61 72 65 20 72 65 61  atements are rea
22aba 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 73  d out of the mas
22abb 74 65 72 20 74 61 62 6c 65 2e 20 20 49 6e 0a 20  ter table.  In. 
22abc 20 2a 2a 20 74 68 65 20 6c 61 74 74 65 72 20 63   ** the latter c
22abd 61 73 65 20 74 68 65 20 69 6e 64 65 78 20 61 6c  ase the index al
22abe 72 65 61 64 79 20 65 78 69 73 74 73 20 6f 6e 20  ready exists on 
22abf 64 69 73 6b 2c 20 77 68 69 63 68 20 69 73 20 77  disk, which is w
22ac0 68 79 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27 74  hy.  ** we don't
22ac1 20 77 61 6e 74 20 74 6f 20 72 65 63 72 65 61 74   want to recreat
22ac2 65 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  e it..  **.  ** 
22ac3 49 66 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 69  If pTblName==0 i
22ac4 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 6e 64  t means this ind
22ac5 65 78 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  ex is generated 
22ac6 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79  as a primary key
22ac7 0a 20 20 2a 2a 20 6f 72 20 55 4e 49 51 55 45 20  .  ** or UNIQUE 
22ac8 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 61 20  constraint of a 
22ac9 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
22aca 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65 20 74  tement.  Since t
22acb 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20 68 61  he table.  ** ha
22acc 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72 65 61  s just been crea
22acd 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69 6e 73  ted, it contains
22ace 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74 68 65   no data and the
22acf 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c 69 7a   index initializ
22ad0 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65 70 20  ation.  ** step 
22ad1 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64 2e 0a  can be skipped..
22ad2 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20    */.  else if( 
22ad3 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30  db->init.busy==0
22ad4 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b   ){.    Vdbe *v;
22ad5 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74  .    char *zStmt
22ad6 3b 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d  ;.    int iMem =
22ad7 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
22ad8 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65  ..    v = sqlite
22ad9 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
22ada 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29  ;.    if( v==0 )
22adb 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
22adc 65 5f 69 6e 64 65 78 3b 0a 0a 0a 20 20 20 20 2f  e_index;...    /
22add 2a 20 43 72 65 61 74 65 20 74 68 65 20 72 6f 6f  * Create the roo
22ade 74 70 61 67 65 20 66 6f 72 20 74 68 65 20 69 6e  tpage for the in
22adf 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  dex.    */.    s
22ae0 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
22ae1 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
22ae2 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 1, iDb);.    s
22ae3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22ae4 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 49 6e 64  (v, OP_CreateInd
22ae5 65 78 2c 20 69 44 62 2c 20 69 4d 65 6d 29 3b 0a  ex, iDb, iMem);.
22ae6 0a 20 20 20 20 2f 2a 20 47 61 74 68 65 72 20 74  .    /* Gather t
22ae7 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74  he complete text
22ae8 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 49   of the CREATE I
22ae9 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 69  NDEX statement i
22aea 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 7a  nto.    ** the z
22aeb 53 74 6d 74 20 76 61 72 69 61 62 6c 65 0a 20 20  Stmt variable.  
22aec 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 74    */.    if( pSt
22aed 61 72 74 20 26 26 20 70 45 6e 64 20 29 7b 0a 20  art && pEnd ){. 
22aee 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d 65 64 20       /* A named 
22aef 69 6e 64 65 78 20 77 69 74 68 20 61 6e 20 65 78  index with an ex
22af0 70 6c 69 63 69 74 20 43 52 45 41 54 45 20 49 4e  plicit CREATE IN
22af1 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  DEX statement */
22af2 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 73  .      zStmt = s
22af3 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
22af4 2c 20 22 43 52 45 41 54 45 25 73 20 49 4e 44 45  , "CREATE%s INDE
22af5 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20 20 20 20  X %.*s",.       
22af6 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e   onError==OE_Non
22af7 65 20 3f 20 22 22 20 3a 20 22 20 55 4e 49 51 55  e ? "" : " UNIQU
22af8 45 22 2c 0a 20 20 20 20 20 20 20 20 70 45 6e 64  E",.        pEnd
22af9 2d 3e 7a 20 2d 20 70 4e 61 6d 65 2d 3e 7a 20 2b  ->z - pName->z +
22afa 20 31 2c 0a 20 20 20 20 20 20 20 20 70 4e 61 6d   1,.        pNam
22afb 65 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65  e->z);.    }else
22afc 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 61 75  {.      /* An au
22afd 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 63 72  tomatic index cr
22afe 65 61 74 65 64 20 62 79 20 61 20 50 52 49 4d 41  eated by a PRIMA
22aff 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45  RY KEY or UNIQUE
22b00 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20   constraint */. 
22b01 20 20 20 20 20 2f 2a 20 7a 53 74 6d 74 20 3d 20       /* zStmt = 
22b02 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22  sqlite3MPrintf("
22b03 22 29 3b 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74  "); */.      zSt
22b04 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  mt = 0;.    }.. 
22b05 20 20 20 2f 2a 20 41 64 64 20 61 6e 20 65 6e 74     /* Add an ent
22b06 72 79 20 69 6e 20 73 71 6c 69 74 65 5f 6d 61 73  ry in sqlite_mas
22b07 74 65 72 20 66 6f 72 20 74 68 69 73 20 69 6e 64  ter for this ind
22b08 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  ex.    */.    sq
22b09 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
22b0a 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
22b0b 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25    "INSERT INTO %
22b0c 51 2e 25 73 20 56 41 4c 55 45 53 28 27 69 6e 64  Q.%s VALUES('ind
22b0d 65 78 27 2c 25 51 2c 25 51 2c 23 25 64 2c 25 51  ex',%Q,%Q,#%d,%Q
22b0e 29 3b 22 2c 0a 20 20 20 20 20 20 20 20 64 62 2d  );",.        db-
22b0f 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
22b10 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
22b11 62 29 2c 0a 20 20 20 20 20 20 20 20 70 49 6e 64  b),.        pInd
22b12 65 78 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  ex->zName,.     
22b13 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a     pTab->zName,.
22b14 20 20 20 20 20 20 20 20 69 4d 65 6d 2c 0a 20 20          iMem,.  
22b15 20 20 20 20 20 20 7a 53 74 6d 74 0a 20 20 20 20        zStmt.    
22b16 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
22b17 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b  Free(db, zStmt);
22b18 0a 0a 20 20 20 20 2f 2a 20 46 69 6c 6c 20 74 68  ..    /* Fill th
22b19 65 20 69 6e 64 65 78 20 77 69 74 68 20 64 61 74  e index with dat
22b1a 61 20 61 6e 64 20 72 65 70 61 72 73 65 20 74 68  a and reparse th
22b1b 65 20 73 63 68 65 6d 61 2e 20 43 6f 64 65 20 61  e schema. Code a
22b1c 6e 20 4f 50 5f 45 78 70 69 72 65 0a 20 20 20 20  n OP_Expire.    
22b1d 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65  ** to invalidate
22b1e 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70 69 6c 65   all pre-compile
22b1f 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20  d statements..  
22b20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 62    */.    if( pTb
22b21 6c 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 73  lName ){.      s
22b22 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65  qlite3RefillInde
22b23 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78  x(pParse, pIndex
22b24 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73  , iMem);.      s
22b25 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
22b26 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
22b27 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22b28 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
22b29 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c  arseSchema, iDb,
22b2a 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20   0, 0,.         
22b2b 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
22b2c 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20  b, "name='%q'", 
22b2d 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c 20  pIndex->zName), 
22b2e 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  P4_DYNAMIC);.   
22b2f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22b30 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70 69 72  dOp1(v, OP_Expir
22b31 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  e, 0);.    }.  }
22b32 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61 64 64 69  ..  /* When addi
22b33 6e 67 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 74  ng an index to t
22b34 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 64 69 63  he list of indic
22b35 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 2c 20  es for a table, 
22b36 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 61  make.  ** sure a
22b37 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61 62 65 6c  ll indices label
22b38 65 64 20 4f 45 5f 52 65 70 6c 61 63 65 20 63 6f  ed OE_Replace co
22b39 6d 65 20 61 66 74 65 72 20 61 6c 6c 20 74 68 6f  me after all tho
22b3a 73 65 20 6c 61 62 65 6c 65 64 0a 20 20 2a 2a 20  se labeled.  ** 
22b3b 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54 68 69 73  OE_Ignore.  This
22b3c 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f   is necessary fo
22b3d 72 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70  r the correct op
22b3e 65 72 61 74 69 6f 6e 20 6f 66 20 55 50 44 41 54  eration of UPDAT
22b3f 45 0a 20 20 2a 2a 20 61 6e 64 20 49 4e 53 45 52  E.  ** and INSER
22b40 54 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  T..  */.  if( db
22b41 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 70  ->init.busy || p
22b42 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  TblName==0 ){.  
22b43 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f    if( onError!=O
22b44 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54 61  E_Replace || pTa
22b45 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a 20 20 20  b->pIndex==0.   
22b46 20 20 20 20 20 20 7c 7c 20 70 54 61 62 2d 3e 70        || pTab->p
22b47 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d  Index->onError==
22b48 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0a 20 20 20  OE_Replace){.   
22b49 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74     pIndex->pNext
22b4a 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b   = pTab->pIndex;
22b4b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 49 6e  .      pTab->pIn
22b4c 64 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20  dex = pIndex;.  
22b4d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49    }else{.      I
22b4e 6e 64 65 78 20 2a 70 4f 74 68 65 72 20 3d 20 70  ndex *pOther = p
22b4f 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20  Tab->pIndex;.   
22b50 20 20 20 77 68 69 6c 65 28 20 70 4f 74 68 65 72     while( pOther
22b51 2d 3e 70 4e 65 78 74 20 26 26 20 70 4f 74 68 65  ->pNext && pOthe
22b52 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72 72 6f  r->pNext->onErro
22b53 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b  r!=OE_Replace ){
22b54 0a 20 20 20 20 20 20 20 20 70 4f 74 68 65 72 20  .        pOther 
22b55 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b  = pOther->pNext;
22b56 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
22b57 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70  Index->pNext = p
22b58 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20  Other->pNext;.  
22b59 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78      pOther->pNex
22b5a 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20  t = pIndex;.    
22b5b 7d 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20 30  }.    pIndex = 0
22b5c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61  ;.  }..  /* Clea
22b5d 6e 20 75 70 20 62 65 66 6f 72 65 20 65 78 69 74  n up before exit
22b5e 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63 72 65 61  ing */.exit_crea
22b5f 74 65 5f 69 6e 64 65 78 3a 0a 20 20 69 66 28 20  te_index:.  if( 
22b60 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71  pIndex ){.    sq
22b61 6c 69 74 65 33 5f 66 72 65 65 28 70 49 6e 64 65  lite3_free(pInde
22b62 78 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 20  x->zColAff);.   
22b63 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
22b64 62 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a  b, pIndex);.  }.
22b65 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
22b66 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73  tDelete(db, pLis
22b67 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  t);.  sqlite3Src
22b68 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
22b69 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  TblName);.  sqli
22b6a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
22b6b 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a  ame);.  return;.
22b6c 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
22b6d 65 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20 73  e code to make s
22b6e 75 72 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72  ure the file for
22b6f 6d 61 74 20 6e 75 6d 62 65 72 20 69 73 20 61 74  mat number is at
22b70 20 6c 65 61 73 74 20 6d 69 6e 46 6f 72 6d 61 74   least minFormat
22b71 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74  ..** The generat
22b72 65 64 20 63 6f 64 65 20 77 69 6c 6c 20 69 6e 63  ed code will inc
22b73 72 65 61 73 65 20 74 68 65 20 66 69 6c 65 20 66  rease the file f
22b74 6f 72 6d 61 74 20 6e 75 6d 62 65 72 20 69 66 20  ormat number if 
22b75 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51  necessary..*/.SQ
22b76 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
22b77 64 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d  d sqlite3Minimum
22b78 46 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65  FileFormat(Parse
22b79 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
22b7a 62 2c 20 69 6e 74 20 6d 69 6e 46 6f 72 6d 61 74  b, int minFormat
22b7b 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  ){.  Vdbe *v;.  
22b7c 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
22b7d 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
22b7e 28 20 76 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ( v ){.    int r
22b7f 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
22b80 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
22b81 20 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69     int r2 = sqli
22b82 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
22b83 61 72 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 6a  arse);.    int j
22b84 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  1;.    sqlite3Vd
22b85 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52  beAddOp3(v, OP_R
22b86 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  eadCookie, iDb, 
22b87 72 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  r1, 1);.    sqli
22b88 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
22b89 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71  (v, iDb);.    sq
22b8a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22b8b 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6d  v, OP_Integer, m
22b8c 69 6e 46 6f 72 6d 61 74 2c 20 72 32 29 3b 0a 20  inFormat, r2);. 
22b8d 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56     j1 = sqlite3V
22b8e 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
22b8f 47 65 2c 20 72 32 2c 20 30 2c 20 72 31 29 3b 0a  Ge, r2, 0, r1);.
22b90 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22b91 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43  ddOp3(v, OP_SetC
22b92 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c 20 72  ookie, iDb, 1, r
22b93 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  2);.    sqlite3V
22b94 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
22b95 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  1);.    sqlite3R
22b96 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
22b97 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 73  arse, r1);.    s
22b98 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
22b99 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29  pReg(pParse, r2)
22b9a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  ;.  }.}../*.** F
22b9b 69 6c 6c 20 74 68 65 20 49 6e 64 65 78 2e 61 69  ill the Index.ai
22b9c 52 6f 77 45 73 74 5b 5d 20 61 72 72 61 79 20 77  RowEst[] array w
22b9d 69 74 68 20 64 65 66 61 75 6c 74 20 69 6e 66 6f  ith default info
22b9e 72 6d 61 74 69 6f 6e 20 2d 20 69 6e 66 6f 72 6d  rmation - inform
22b9f 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 75  ation.** to be u
22ba0 73 65 64 20 77 68 65 6e 20 77 65 20 68 61 76 65  sed when we have
22ba1 20 6e 6f 74 20 72 75 6e 20 74 68 65 20 41 4e 41   not run the ANA
22ba2 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  LYZE command..**
22ba3 0a 2a 2a 20 61 69 52 6f 77 45 73 74 5b 30 5d 20  .** aiRowEst[0] 
22ba4 69 73 20 73 75 70 70 6f 73 65 20 74 6f 20 63 6f  is suppose to co
22ba5 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72  ntain the number
22ba6 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20   of elements in 
22ba7 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 53 69  the index..** Si
22ba8 6e 63 65 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e  nce we do not kn
22ba9 6f 77 2c 20 67 75 65 73 73 20 31 20 6d 69 6c 6c  ow, guess 1 mill
22baa 69 6f 6e 2e 20 20 61 69 52 6f 77 45 73 74 5b 31  ion.  aiRowEst[1
22bab 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65  ] is an estimate
22bac 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65   of the.** numbe
22bad 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  r of rows in the
22bae 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63   table that matc
22baf 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 72  h any particular
22bb0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a   value of the.**
22bb1 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66   first column of
22bb2 20 74 68 65 20 69 6e 64 65 78 2e 20 20 61 69 52   the index.  aiR
22bb3 6f 77 45 73 74 5b 32 5d 20 69 73 20 61 6e 20 65  owEst[2] is an e
22bb4 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6e  stimate of the n
22bb5 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 72 6f 77 73  umber.** of rows
22bb6 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20   that match any 
22bb7 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6d 62 69  particular combi
22bb8 6e 69 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 66  niation of the f
22bb9 69 72 73 74 20 32 20 63 6f 6c 75 6d 6e 73 0a 2a  irst 2 columns.*
22bba 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20  * of the index. 
22bbb 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20   And so forth.  
22bbc 49 74 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62  It must always b
22bbd 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 0a  e the case that.
22bbe 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61  *.**           a
22bbf 69 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52 6f  iRowEst[N]<=aiRo
22bc0 77 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20 20  wEst[N-1].**    
22bc1 20 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b         aiRowEst[
22bc2 4e 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61 72  N]>=1.**.** Apar
22bc3 74 20 66 72 6f 6d 20 74 68 61 74 2c 20 77 65 20  t from that, we 
22bc4 68 61 76 65 20 6c 69 74 74 6c 65 20 74 6f 20 67  have little to g
22bc5 6f 20 6f 6e 20 62 65 73 69 64 65 73 20 69 6e 74  o on besides int
22bc6 75 69 74 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a 20  uition as to.** 
22bc7 68 6f 77 20 61 69 52 6f 77 45 73 74 5b 5d 20 73  how aiRowEst[] s
22bc8 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c  hould be initial
22bc9 69 7a 65 64 2e 20 20 54 68 65 20 6e 75 6d 62 65  ized.  The numbe
22bca 72 73 20 67 65 6e 65 72 61 74 65 64 20 68 65 72  rs generated her
22bcb 65 0a 2a 2a 20 61 72 65 20 62 61 73 65 64 20 6f  e.** are based o
22bcc 6e 20 74 79 70 69 63 61 6c 20 76 61 6c 75 65 73  n typical values
22bcd 20 66 6f 75 6e 64 20 69 6e 20 61 63 74 75 61 6c   found in actual
22bce 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 53 51 4c   indices..*/.SQL
22bcf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
22bd0 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52   sqlite3DefaultR
22bd1 6f 77 45 73 74 28 49 6e 64 65 78 20 2a 70 49 64  owEst(Index *pId
22bd2 78 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a  x){.  unsigned *
22bd3 61 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77 45  a = pIdx->aiRowE
22bd4 73 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61  st;.  int i;.  a
22bd5 73 73 65 72 74 28 20 61 21 3d 30 20 29 3b 0a 20  ssert( a!=0 );. 
22bd6 20 61 5b 30 5d 20 3d 20 31 30 30 30 30 30 30 3b   a[0] = 1000000;
22bd7 0a 20 20 66 6f 72 28 69 3d 70 49 64 78 2d 3e 6e  .  for(i=pIdx->n
22bd8 43 6f 6c 75 6d 6e 3b 20 69 3e 3d 35 3b 20 69 2d  Column; i>=5; i-
22bd9 2d 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20 35  -){.    a[i] = 5
22bda 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 69  ;.  }.  while( i
22bdb 3e 3d 31 20 29 7b 0a 20 20 20 20 61 5b 69 5d 20  >=1 ){.    a[i] 
22bdc 3d 20 31 31 20 2d 20 69 3b 0a 20 20 20 20 69 2d  = 11 - i;.    i-
22bdd 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64  -;.  }.  if( pId
22bde 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
22bdf 6f 6e 65 20 29 7b 0a 20 20 20 20 61 5b 70 49 64  one ){.    a[pId
22be0 78 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31 3b  x->nColumn] = 1;
22be1 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
22be2 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
22be3 64 72 6f 70 20 61 6e 20 65 78 69 73 74 69 6e 67  drop an existing
22be4 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 20 54   named index.  T
22be5 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69  his routine.** i
22be6 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 44 52  mplements the DR
22be7 4f 50 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65  OP INDEX stateme
22be8 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
22be9 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
22bea 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73  e3DropIndex(Pars
22beb 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69  e *pParse, SrcLi
22bec 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69  st *pName, int i
22bed 66 45 78 69 73 74 73 29 7b 0a 20 20 49 6e 64 65  fExists){.  Inde
22bee 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64 62  x *pIndex;.  Vdb
22bef 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20  e *v;.  sqlite3 
22bf0 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
22bf1 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20  ;.  int iDb;..  
22bf2 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
22bf3 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
22bf4 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
22bf5 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
22bf6 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
22bf7 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29  pName->nSrc==1 )
22bf8 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
22bf9 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63  K!=sqlite3ReadSc
22bfa 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a  hema(pParse) ){.
22bfb 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
22bfc 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20  op_index;.  }.  
22bfd 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
22bfe 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 70 4e  FindIndex(db, pN
22bff 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c  ame->a[0].zName,
22c00 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61   pName->a[0].zDa
22c01 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  tabase);.  if( p
22c02 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  Index==0 ){.    
22c03 69 66 28 20 21 69 66 45 78 69 73 74 73 20 29 7b  if( !ifExists ){
22c04 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
22c05 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
22c06 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20 25  no such index: %
22c07 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20  S", pName, 0);. 
22c08 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d     }.    pParse-
22c09 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31  >checkSchema = 1
22c0a 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
22c0b 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  drop_index;.  }.
22c0c 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 61 75    if( pIndex->au
22c0d 74 6f 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73  toIndex ){.    s
22c0e 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
22c0f 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20 61 73  Parse, "index as
22c10 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 55 4e  sociated with UN
22c11 49 51 55 45 20 22 0a 20 20 20 20 20 20 22 6f 72  IQUE ".      "or
22c12 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e   PRIMARY KEY con
22c13 73 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74 20 62  straint cannot b
22c14 65 20 64 72 6f 70 70 65 64 22 2c 20 30 29 3b 0a  e dropped", 0);.
22c15 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
22c16 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20  op_index;.  }.  
22c17 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
22c18 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
22c19 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b  Index->pSchema);
22c1a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22c1b 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
22c1c 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63  ON.  {.    int c
22c1d 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
22c1e 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54 61 62  P_INDEX;.    Tab
22c1f 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64 65  le *pTab = pInde
22c20 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 63  x->pTable;.    c
22c21 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
22c22 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
22c23 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ame;.    const c
22c24 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45  har *zTab = SCHE
22c25 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20  MA_TABLE(iDb);. 
22c26 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
22c27 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
22c28 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a  SQLITE_DELETE, z
22c29 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  Tab, 0, zDb) ){.
22c2a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
22c2b 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20  drop_index;.    
22c2c 7d 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f  }.    if( !OMIT_
22c2d 54 45 4d 50 44 42 20 26 26 20 69 44 62 20 29 20  TEMPDB && iDb ) 
22c2e 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
22c2f 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20  OP_TEMP_INDEX;. 
22c30 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
22c31 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
22c32 63 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e  code, pIndex->zN
22c33 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
22c34 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20  , zDb) ){.      
22c35 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69  goto exit_drop_i
22c36 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ndex;.    }.  }.
22c37 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e  #endif..  /* Gen
22c38 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65  erate code to re
22c39 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 61  move the index a
22c3a 6e 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74  nd from the mast
22c3b 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 20  er table */.  v 
22c3c 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
22c3d 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
22c3e 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
22c3f 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
22c40 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69  ion(pParse, 1, i
22c41 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
22c42 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
22c43 73 65 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45  se,.       "DELE
22c44 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48  TE FROM %Q.%s WH
22c45 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20  ERE name=%Q",.  
22c46 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
22c47 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ].zName, SCHEMA_
22c48 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20  TABLE(iDb),.    
22c49 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65     pIndex->zName
22c4a 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20  .    );.    if( 
22c4b 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
22c4c 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61  (db, "sqlite_sta
22c4d 74 31 22 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  t1", db->aDb[iDb
22c4e 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  ].zName) ){.    
22c4f 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
22c50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
22c51 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f       "DELETE FRO
22c52 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74  M %Q.sqlite_stat
22c53 31 20 57 48 45 52 45 20 69 64 78 3d 25 51 22 2c  1 WHERE idx=%Q",
22c54 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62  .        db->aDb
22c55 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 70 49 6e  [iDb].zName, pIn
22c56 64 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20  dex->zName.     
22c57 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71   );.    }.    sq
22c58 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
22c59 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
22c5a 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50      destroyRootP
22c5b 61 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 64  age(pParse, pInd
22c5c 65 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a  ex->tnum, iDb);.
22c5d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22c5e 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70  ddOp4(v, OP_Drop
22c5f 49 6e 64 65 78 2c 20 69 44 62 2c 20 30 2c 20 30  Index, iDb, 0, 0
22c60 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c  , pIndex->zName,
22c61 20 30 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64   0);.  }..exit_d
22c62 72 6f 70 5f 69 6e 64 65 78 3a 0a 20 20 73 71 6c  rop_index:.  sql
22c63 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
22c64 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a  e(db, pName);.}.
22c65 0a 2f 2a 0a 2a 2a 20 70 41 72 72 61 79 20 69 73  ./*.** pArray is
22c66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
22c67 20 61 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74   array of object
22c68 73 2e 20 20 45 61 63 68 20 6f 62 6a 65 63 74 20  s.  Each object 
22c69 69 6e 20 74 68 65 0a 2a 2a 20 61 72 72 61 79 20  in the.** array 
22c6a 69 73 20 73 7a 45 6e 74 72 79 20 62 79 74 65 73  is szEntry bytes
22c6b 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 69 73 20   in size.  This 
22c6c 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
22c6d 73 20 61 20 6e 65 77 0a 2a 2a 20 6f 62 6a 65 63  s a new.** objec
22c6e 74 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20  t on the end of 
22c6f 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a  the array..**.**
22c70 20 2a 70 6e 45 6e 74 72 79 20 69 73 20 74 68 65   *pnEntry is the
22c71 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
22c72 65 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73  es already in us
22c73 65 2e 20 20 2a 70 6e 41 6c 6c 6f 63 20 69 73 0a  e.  *pnAlloc is.
22c74 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c  ** the previousl
22c75 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65  y allocated size
22c76 20 6f 66 20 74 68 65 20 61 72 72 61 79 2e 20 20   of the array.  
22c77 69 6e 69 74 53 69 7a 65 20 69 73 20 74 68 65 0a  initSize is the.
22c78 2a 2a 20 73 75 67 67 65 73 74 65 64 20 69 6e 69  ** suggested ini
22c79 74 69 61 6c 20 61 72 72 61 79 20 73 69 7a 65 20  tial array size 
22c7a 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  allocation..**.*
22c7b 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74  * The index of t
22c7c 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20  he new entry is 
22c7d 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 49 64  returned in *pId
22c7e 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  x..**.** This ro
22c7f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20  utine returns a 
22c80 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61  pointer to the a
22c81 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e  rray of objects.
22c82 20 20 54 68 69 73 0a 2a 2a 20 6d 69 67 68 74 20    This.** might 
22c83 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  be the same as t
22c84 68 65 20 70 41 72 72 61 79 20 70 61 72 61 6d 65  he pArray parame
22c85 74 65 72 20 6f 72 20 69 74 20 6d 69 67 68 74 20  ter or it might 
22c86 62 65 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a  be a different.*
22c87 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68 65  * pointer if the
22c88 20 61 72 72 61 79 20 77 61 73 20 72 65 73 69 7a   array was resiz
22c89 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
22c8a 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
22c8b 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65  te3ArrayAllocate
22c8c 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
22c8d 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74        /* Connect
22c8e 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66  ion to notify of
22c8f 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
22c90 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 72   */.  void *pArr
22c91 61 79 2c 20 20 20 20 20 2f 2a 20 41 72 72 61 79  ay,     /* Array
22c92 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 4d 69   of objects.  Mi
22c93 67 68 74 20 62 65 20 72 65 61 6c 6c 6f 63 61 74  ght be reallocat
22c94 65 64 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 6e  ed */.  int szEn
22c95 74 72 79 2c 20 20 20 20 20 20 2f 2a 20 53 69 7a  try,      /* Siz
22c96 65 20 6f 66 20 65 61 63 68 20 6f 62 6a 65 63 74  e of each object
22c97 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 2a 2f   in the array */
22c98 0a 20 20 69 6e 74 20 69 6e 69 74 53 69 7a 65 2c  .  int initSize,
22c99 20 20 20 20 20 2f 2a 20 53 75 67 67 65 73 74 65       /* Suggeste
22c9a 64 20 69 6e 69 74 69 61 6c 20 61 6c 6c 6f 63 61  d initial alloca
22c9b 74 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74  tion, in element
22c9c 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 6e  s */.  int *pnEn
22c9d 74 72 79 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62  try,     /* Numb
22c9e 65 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 63 75  er of objects cu
22c9f 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a  rrently in use *
22ca0 2f 0a 20 20 69 6e 74 20 2a 70 6e 41 6c 6c 6f 63  /.  int *pnAlloc
22ca1 2c 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74  ,     /* Current
22ca2 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c   size of the all
22ca3 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d  ocation, in elem
22ca4 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ents */.  int *p
22ca5 49 64 78 20 20 20 20 20 20 20 20 20 2f 2a 20 57  Idx         /* W
22ca6 72 69 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f  rite the index o
22ca7 66 20 61 20 6e 65 77 20 73 6c 6f 74 20 68 65 72  f a new slot her
22ca8 65 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a  e */.){.  char *
22ca9 7a 3b 0a 20 20 69 66 28 20 2a 70 6e 45 6e 74 72  z;.  if( *pnEntr
22caa 79 20 3e 3d 20 2a 70 6e 41 6c 6c 6f 63 20 29 7b  y >= *pnAlloc ){
22cab 0a 20 20 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b  .    void *pNew;
22cac 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65  .    int newSize
22cad 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20  ;.    newSize = 
22cae 28 2a 70 6e 41 6c 6c 6f 63 29 2a 32 20 2b 20 69  (*pnAlloc)*2 + i
22caf 6e 69 74 53 69 7a 65 3b 0a 20 20 20 20 70 4e 65  nitSize;.    pNe
22cb0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  w = sqlite3DbRea
22cb1 6c 6c 6f 63 28 64 62 2c 20 70 41 72 72 61 79 2c  lloc(db, pArray,
22cb2 20 6e 65 77 53 69 7a 65 2a 73 7a 45 6e 74 72 79   newSize*szEntry
22cb3 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d  );.    if( pNew=
22cb4 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 49 64  =0 ){.      *pId
22cb5 78 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 65  x = -1;.      re
22cb6 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 20 20 20  turn pArray;.   
22cb7 20 7d 0a 20 20 20 20 2a 70 6e 41 6c 6c 6f 63 20   }.    *pnAlloc 
22cb8 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
22cb9 63 53 69 7a 65 28 64 62 2c 20 70 4e 65 77 29 2f  cSize(db, pNew)/
22cba 73 7a 45 6e 74 72 79 3b 0a 20 20 20 20 70 41 72  szEntry;.    pAr
22cbb 72 61 79 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  ray = pNew;.  }.
22cbc 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 41 72    z = (char*)pAr
22cbd 72 61 79 3b 0a 20 20 6d 65 6d 73 65 74 28 26 7a  ray;.  memset(&z
22cbe 5b 2a 70 6e 45 6e 74 72 79 20 2a 20 73 7a 45 6e  [*pnEntry * szEn
22cbf 74 72 79 5d 2c 20 30 2c 20 73 7a 45 6e 74 72 79  try], 0, szEntry
22cc0 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 2a 70 6e  );.  *pIdx = *pn
22cc1 45 6e 74 72 79 3b 0a 20 20 2b 2b 2a 70 6e 45 6e  Entry;.  ++*pnEn
22cc2 74 72 79 3b 0a 20 20 72 65 74 75 72 6e 20 70 41  try;.  return pA
22cc3 72 72 61 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rray;.}../*.** A
22cc4 70 70 65 6e 64 20 61 20 6e 65 77 20 65 6c 65 6d  ppend a new elem
22cc5 65 6e 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e  ent to the given
22cc6 20 49 64 4c 69 73 74 2e 20 20 43 72 65 61 74 65   IdList.  Create
22cc7 20 61 20 6e 65 77 20 49 64 4c 69 73 74 20 69 66   a new IdList if
22cc8 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 0a 2a 2a 0a  .** need be..**.
22cc9 2a 2a 20 41 20 6e 65 77 20 49 64 4c 69 73 74 20  ** A new IdList 
22cca 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20  is returned, or 
22ccb 4e 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29  NULL if malloc()
22ccc 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54   fails..*/.SQLIT
22ccd 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74  E_PRIVATE IdList
22cce 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41   *sqlite3IdListA
22ccf 70 70 65 6e 64 28 73 71 6c 69 74 65 33 20 2a 64  ppend(sqlite3 *d
22cd0 62 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74  b, IdList *pList
22cd1 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29  , Token *pToken)
22cd2 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
22cd3 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20   pList==0 ){.   
22cd4 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
22cd5 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
22cd6 20 73 69 7a 65 6f 66 28 49 64 4c 69 73 74 29 20   sizeof(IdList) 
22cd7 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74  );.    if( pList
22cd8 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
22cd9 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f      pList->nAllo
22cda 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69  c = 0;.  }.  pLi
22cdb 73 74 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 41  st->a = sqlite3A
22cdc 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20  rrayAllocate(.  
22cdd 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 70 4c      db,.      pL
22cde 69 73 74 2d 3e 61 2c 0a 20 20 20 20 20 20 73 69  ist->a,.      si
22cdf 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d  zeof(pList->a[0]
22ce0 29 2c 0a 20 20 20 20 20 20 35 2c 0a 20 20 20 20  ),.      5,.    
22ce1 20 20 26 70 4c 69 73 74 2d 3e 6e 49 64 2c 0a 20    &pList->nId,. 
22ce2 20 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e 41 6c       &pList->nAl
22ce3 6c 6f 63 2c 0a 20 20 20 20 20 20 26 69 0a 20 20  loc,.      &i.  
22ce4 29 3b 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a  );.  if( i<0 ){.
22ce5 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73      sqlite3IdLis
22ce6 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73  tDelete(db, pLis
22ce7 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  t);.    return 0
22ce8 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61  ;.  }.  pList->a
22ce9 5b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  [i].zName = sqli
22cea 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
22ceb 28 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20  (db, pToken);.  
22cec 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a  return pList;.}.
22ced 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
22cee 20 49 64 4c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49   IdList..*/.SQLI
22cef 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
22cf0 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
22cf1 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ete(sqlite3 *db,
22cf2 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b   IdList *pList){
22cf3 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
22cf4 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
22cf5 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
22cf6 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29  pList->nId; i++)
22cf7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
22cf8 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61  ree(db, pList->a
22cf9 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a  [i].zName);.  }.
22cfa 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
22cfb 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20  db, pList->a);. 
22cfc 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
22cfd 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a  b, pList);.}../*
22cfe 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69  .** Return the i
22cff 6e 64 65 78 20 69 6e 20 70 4c 69 73 74 20 6f 66  ndex in pList of
22d00 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 20   the identifier 
22d01 6e 61 6d 65 64 20 7a 49 64 2e 20 20 52 65 74 75  named zId.  Retu
22d02 72 6e 20 2d 31 0a 2a 2a 20 69 66 20 6e 6f 74 20  rn -1.** if not 
22d03 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  found..*/.SQLITE
22d04 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
22d05 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28  ite3IdListIndex(
22d06 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 63  IdList *pList, c
22d07 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
22d08 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
22d09 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74  ( pList==0 ) ret
22d0a 75 72 6e 20 2d 31 3b 0a 20 20 66 6f 72 28 69 3d  urn -1;.  for(i=
22d0b 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b  0; i<pList->nId;
22d0c 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73   i++){.    if( s
22d0d 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 4c  qlite3StrICmp(pL
22d0e 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c  ist->a[i].zName,
22d0f 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 72 65 74   zName)==0 ) ret
22d10 75 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74  urn i;.  }.  ret
22d11 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn -1;.}../*.**
22d12 20 45 78 70 61 6e 64 20 74 68 65 20 73 70 61 63   Expand the spac
22d13 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  e allocated for 
22d14 74 68 65 20 67 69 76 65 6e 20 53 72 63 4c 69 73  the given SrcLis
22d15 74 20 6f 62 6a 65 63 74 20 62 79 0a 2a 2a 20 63  t object by.** c
22d16 72 65 61 74 69 6e 67 20 6e 45 78 74 72 61 20 6e  reating nExtra n
22d17 65 77 20 73 6c 6f 74 73 20 62 65 67 69 6e 6e 69  ew slots beginni
22d18 6e 67 20 61 74 20 69 53 74 61 72 74 2e 20 20 69  ng at iStart.  i
22d19 53 74 61 72 74 20 69 73 20 7a 65 72 6f 20 62 61  Start is zero ba
22d1a 73 65 64 2e 0a 2a 2a 20 4e 65 77 20 73 6c 6f 74  sed..** New slot
22d1b 73 20 61 72 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a  s are zeroed..**
22d1c 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  .** For example,
22d1d 20 73 75 70 70 6f 73 65 20 61 20 53 72 63 4c 69   suppose a SrcLi
22d1e 73 74 20 69 6e 69 74 69 61 6c 6c 79 20 63 6f 6e  st initially con
22d1f 74 61 69 6e 73 20 74 77 6f 20 65 6e 74 72 69 65  tains two entrie
22d20 73 3a 20 41 2c 42 2e 0a 2a 2a 20 54 6f 20 61 70  s: A,B..** To ap
22d21 70 65 6e 64 20 33 20 6e 65 77 20 65 6e 74 72 69  pend 3 new entri
22d22 65 73 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 2c  es onto the end,
22d23 20 64 6f 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20   do this:.**.** 
22d24 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
22d25 74 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 53 72  tEnlarge(db, pSr
22d26 63 6c 69 73 74 2c 20 33 2c 20 32 29 3b 0a 2a 2a  clist, 3, 2);.**
22d27 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 63 61  .** After the ca
22d28 6c 6c 20 61 62 6f 76 65 20 69 74 20 77 6f 75 6c  ll above it woul
22d29 64 20 63 6f 6e 74 61 69 6e 3a 20 20 41 2c 20 42  d contain:  A, B
22d2a 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2e  , nil, nil, nil.
22d2b 0a 2a 2a 20 49 66 20 74 68 65 20 69 53 74 61 72  .** If the iStar
22d2c 74 20 61 72 67 75 6d 65 6e 74 20 68 61 64 20 62  t argument had b
22d2d 65 65 6e 20 31 20 69 6e 73 74 65 61 64 20 6f 66  een 1 instead of
22d2e 20 32 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73   2, then the res
22d2f 75 6c 74 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76  ult.** would hav
22d30 65 20 62 65 65 6e 3a 20 20 41 2c 20 6e 69 6c 2c  e been:  A, nil,
22d31 20 6e 69 6c 2c 20 6e 69 6c 2c 20 42 2e 20 20 54   nil, nil, B.  T
22d32 6f 20 70 72 65 70 65 6e 64 20 74 68 65 20 6e 65  o prepend the ne
22d33 77 20 73 6c 6f 74 73 2c 0a 2a 2a 20 74 68 65 20  w slots,.** the 
22d34 69 53 74 61 72 74 20 76 61 6c 75 65 20 77 6f 75  iStart value wou
22d35 6c 64 20 62 65 20 30 2e 20 20 54 68 65 20 72 65  ld be 0.  The re
22d36 73 75 6c 74 20 74 68 65 6e 20 77 6f 75 6c 64 0a  sult then would.
22d37 2a 2a 20 62 65 3a 20 6e 69 6c 2c 20 6e 69 6c 2c  ** be: nil, nil,
22d38 20 6e 69 6c 2c 20 41 2c 20 42 2e 0a 2a 2a 0a 2a   nil, A, B..**.*
22d39 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  * If a memory al
22d3a 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 74  location fails t
22d3b 68 65 20 53 72 63 4c 69 73 74 20 69 73 20 75 6e  he SrcList is un
22d3c 63 68 61 6e 67 65 64 2e 20 20 54 68 65 0a 2a 2a  changed.  The.**
22d3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
22d3e 64 20 66 6c 61 67 20 77 69 6c 6c 20 62 65 20 73  d flag will be s
22d3f 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2f 0a 53  et to true..*/.S
22d40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72  QLITE_PRIVATE Sr
22d41 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72  cList *sqlite3Sr
22d42 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 0a 20 20  cListEnlarge(.  
22d43 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
22d44 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
22d45 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74  onnection to not
22d46 69 66 79 20 6f 66 20 4f 4f 4d 20 65 72 72 6f 72  ify of OOM error
22d47 73 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  s */.  SrcList *
22d48 70 53 72 63 2c 20 20 20 20 20 2f 2a 20 54 68 65  pSrc,     /* The
22d49 20 53 72 63 4c 69 73 74 20 74 6f 20 62 65 20 65   SrcList to be e
22d4a 6e 6c 61 72 67 65 64 20 2a 2f 0a 20 20 69 6e 74  nlarged */.  int
22d4b 20 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20   nExtra,        
22d4c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 77  /* Number of new
22d4d 20 73 6c 6f 74 73 20 74 6f 20 61 64 64 20 74 6f   slots to add to
22d4e 20 70 53 72 63 2d 3e 61 5b 5d 20 2a 2f 0a 20 20   pSrc->a[] */.  
22d4f 69 6e 74 20 69 53 74 61 72 74 20 20 20 20 20 20  int iStart      
22d50 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70     /* Index in p
22d51 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 66 69 72 73  Src->a[] of firs
22d52 74 20 6e 65 77 20 73 6c 6f 74 20 2a 2f 0a 29 7b  t new slot */.){
22d53 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f 2a 20  .  int i;..  /* 
22d54 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20  Sanity checking 
22d55 6f 6e 20 63 61 6c 6c 69 6e 67 20 70 61 72 61 6d  on calling param
22d56 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 72  eters */.  asser
22d57 74 28 20 69 53 74 61 72 74 3e 3d 30 20 29 3b 0a  t( iStart>=0 );.
22d58 20 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61    assert( nExtra
22d59 3e 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 53 72  >=1 );.  if( pSr
22d5a 63 3d 3d 30 20 7c 7c 20 69 53 74 61 72 74 3e 70  c==0 || iStart>p
22d5b 53 72 63 2d 3e 6e 53 72 63 20 29 7b 0a 20 20 20  Src->nSrc ){.   
22d5c 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
22d5d 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
22d5e 20 72 65 74 75 72 6e 20 70 53 72 63 3b 0a 20 20   return pSrc;.  
22d5f 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
22d60 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 70 61 63   additional spac
22d61 65 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20  e if needed */. 
22d62 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 2b   if( pSrc->nSrc+
22d63 6e 45 78 74 72 61 3e 70 53 72 63 2d 3e 6e 41 6c  nExtra>pSrc->nAl
22d64 6c 6f 63 20 29 7b 0a 20 20 20 20 53 72 63 4c 69  loc ){.    SrcLi
22d65 73 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e  st *pNew;.    in
22d66 74 20 6e 41 6c 6c 6f 63 20 3d 20 70 53 72 63 2d  t nAlloc = pSrc-
22d67 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3b 0a 20 20  >nSrc+nExtra;.  
22d68 20 20 69 6e 74 20 6e 47 6f 74 3b 0a 20 20 20 20    int nGot;.    
22d69 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
22d6a 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 53 72 63  Realloc(db, pSrc
22d6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
22d6c 20 73 69 7a 65 6f 66 28 2a 70 53 72 63 29 20 2b   sizeof(*pSrc) +
22d6d 20 28 6e 41 6c 6c 6f 63 2d 31 29 2a 73 69 7a 65   (nAlloc-1)*size
22d6e 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 20 29  of(pSrc->a[0]) )
22d6f 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d  ;.    if( pNew==
22d70 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
22d71 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
22d72 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74  led );.      ret
22d73 75 72 6e 20 70 53 72 63 3b 0a 20 20 20 20 7d 0a  urn pSrc;.    }.
22d74 20 20 20 20 70 53 72 63 20 3d 20 70 4e 65 77 3b      pSrc = pNew;
22d75 0a 20 20 20 20 6e 47 6f 74 20 3d 20 28 73 71 6c  .    nGot = (sql
22d76 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
22d77 28 64 62 2c 20 70 4e 65 77 29 20 2d 20 73 69 7a  (db, pNew) - siz
22d78 65 6f 66 28 2a 70 53 72 63 29 29 2f 73 69 7a 65  eof(*pSrc))/size
22d79 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2b 31  of(pSrc->a[0])+1
22d7a 3b 0a 20 20 20 20 70 53 72 63 2d 3e 6e 41 6c 6c  ;.    pSrc->nAll
22d7b 6f 63 20 3d 20 28 75 31 36 29 6e 47 6f 74 3b 0a  oc = (u16)nGot;.
22d7c 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 65    }..  /* Move e
22d7d 78 69 73 74 69 6e 67 20 73 6c 6f 74 73 20 74 68  xisting slots th
22d7e 61 74 20 63 6f 6d 65 20 61 66 74 65 72 20 74 68  at come after th
22d7f 65 20 6e 65 77 6c 79 20 69 6e 73 65 72 74 65 64  e newly inserted
22d80 20 73 6c 6f 74 73 0a 20 20 2a 2a 20 6f 75 74 20   slots.  ** out 
22d81 6f 66 20 74 68 65 20 77 61 79 20 2a 2f 0a 20 20  of the way */.  
22d82 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63  for(i=pSrc->nSrc
22d83 2d 31 3b 20 69 3e 3d 69 53 74 61 72 74 3b 20 69  -1; i>=iStart; i
22d84 2d 2d 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61  --){.    pSrc->a
22d85 5b 69 2b 6e 45 78 74 72 61 5d 20 3d 20 70 53 72  [i+nExtra] = pSr
22d86 63 2d 3e 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 70  c->a[i];.  }.  p
22d87 53 72 63 2d 3e 6e 53 72 63 20 2b 3d 20 28 69 31  Src->nSrc += (i1
22d88 36 29 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a 20  6)nExtra;..  /* 
22d89 5a 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20 61  Zero the newly a
22d8a 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 2a  llocated slots *
22d8b 2f 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72 63  /.  memset(&pSrc
22d8c 2d 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c 20  ->a[iStart], 0, 
22d8d 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30  sizeof(pSrc->a[0
22d8e 5d 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66 6f  ])*nExtra);.  fo
22d8f 72 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69 53  r(i=iStart; i<iS
22d90 74 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b 2b  tart+nExtra; i++
22d91 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69  ){.    pSrc->a[i
22d92 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a  ].iCursor = -1;.
22d93 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e    }..  /* Return
22d94 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
22d95 65 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c 69  e enlarged SrcLi
22d96 73 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70  st */.  return p
22d97 53 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41  Src;.}.../*.** A
22d98 70 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62 6c  ppend a new tabl
22d99 65 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67 69  e name to the gi
22d9a 76 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43 72  ven SrcList.  Cr
22d9b 65 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c 69  eate a new SrcLi
22d9c 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65  st if.** need be
22d9d 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69  .  A new entry i
22d9e 73 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65  s created in the
22d9f 20 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69 66   SrcList even if
22da0 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 2e   pToken is NULL.
22da1 0a 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73 74  .**.** A SrcList
22da2 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72   is returned, or
22da3 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69   NULL if there i
22da4 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20  s an OOM error. 
22da5 20 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a   The returned.**
22da6 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 62   SrcList might b
22da7 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  e the same as th
22da8 65 20 53 72 63 4c 69 73 74 20 74 68 61 74 20 77  e SrcList that w
22da9 61 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20 6d  as input or it m
22daa 69 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65 77  ight be.** a new
22dab 20 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f 4d   one.  If an OOM
22dac 20 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63 75   error does occu
22dad 72 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69  rs, then the pri
22dae 6f 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69 73  or value of pLis
22daf 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 70  t.** that is inp
22db0 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ut to this routi
22db1 6e 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  ne is automatica
22db2 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a  lly freed..**.**
22db3 20 49 66 20 70 44 61 74 61 62 61 73 65 20 69 73   If pDatabase is
22db4 20 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65   not null, it me
22db5 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 62  ans that the tab
22db6 6c 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e  le has an option
22db7 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6e  al.** database n
22db8 61 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69 6b  ame prefix.  Lik
22db9 65 20 74 68 69 73 3a 20 20 22 64 61 74 61 62 61  e this:  "databa
22dba 73 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65 20  se.table".  The 
22dbb 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f 69  pDatabase.** poi
22dbc 6e 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65  nts to the table
22dbd 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70 54   name and the pT
22dbe 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 74  able points to t
22dbf 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
22dc0 2e 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73 74  ..** The SrcList
22dc1 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c 64  .a[].zName field
22dc2 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
22dc3 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77  the table name w
22dc4 68 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63 6f  hich might.** co
22dc5 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20 28  me from pTable (
22dc6 69 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20  if pDatabase is 
22dc7 4e 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70 44  NULL) or from pD
22dc8 61 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53 72  atabase.  .** Sr
22dc9 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 62  cList.a[].zDatab
22dca 61 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69  ase is filled wi
22dcb 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
22dcc 6e 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65  name from pTable
22dcd 2c 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55 4c  ,.** or with NUL
22dce 4c 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73 65  L if no database
22dcf 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 2a   is specified..*
22dd0 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f  *.** In other wo
22dd1 72 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69 6b  rds, if call lik
22dd2 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
22dd3 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63        sqlite3Src
22dd4 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 42  ListAppend(D,A,B
22dd5 2c 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20  ,0);.**.** Then 
22dd6 42 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d  B is a table nam
22dd7 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  e and the databa
22dd8 73 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70 65  se name is unspe
22dd9 63 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c 6c  cified.  If call
22dda 65 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a  ed.** like this:
22ddb 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 73  .**.**         s
22ddc 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
22ddd 65 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a  end(D,A,B,C);.**
22dde 0a 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74 68  .** Then C is th
22ddf 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64  e table name and
22de0 20 42 20 69 73 20 74 68 65 20 64 61 74 61 62 61   B is the databa
22de1 73 65 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49  se name..*/.SQLI
22de2 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69  TE_PRIVATE SrcLi
22de3 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69  st *sqlite3SrcLi
22de4 73 74 41 70 70 65 6e 64 28 0a 20 20 73 71 6c 69  stAppend(.  sqli
22de5 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
22de6 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f  /* Connection to
22de7 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f   notify of mallo
22de8 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20  c failures */.  
22de9 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  SrcList *pList, 
22dea 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 6f      /* Append to
22deb 20 74 68 69 73 20 53 72 63 4c 69 73 74 2e 20 4e   this SrcList. N
22dec 55 4c 4c 20 63 72 65 61 74 65 73 20 61 20 6e 65  ULL creates a ne
22ded 77 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 54  w SrcList */.  T
22dee 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20  oken *pTable,   
22def 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 61     /* Table to a
22df0 70 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ppend */.  Token
22df1 20 2a 70 44 61 74 61 62 61 73 65 20 20 20 20 2f   *pDatabase    /
22df2 2a 20 44 61 74 61 62 61 73 65 20 6f 66 20 74 68  * Database of th
22df3 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20  e table */.){.  
22df4 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
22df5 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66  tem *pItem;.  if
22df6 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  ( pList==0 ){.  
22df7 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
22df8 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
22df9 2c 20 73 69 7a 65 6f 66 28 53 72 63 4c 69 73 74  , sizeof(SrcList
22dfa 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69  ) );.    if( pLi
22dfb 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  st==0 ) return 0
22dfc 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c  ;.    pList->nAl
22dfd 6c 6f 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70  loc = 1;.  }.  p
22dfe 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 53 72  List = sqlite3Sr
22dff 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c  cListEnlarge(db,
22e00 20 70 4c 69 73 74 2c 20 31 2c 20 70 4c 69 73 74   pList, 1, pList
22e01 2d 3e 6e 53 72 63 29 3b 0a 20 20 69 66 28 20 64  ->nSrc);.  if( d
22e02 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22e03 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72  ){.    sqlite3Sr
22e04 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
22e05 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 65 74 75  pList);.    retu
22e06 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49 74 65  rn 0;.  }.  pIte
22e07 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c  m = &pList->a[pL
22e08 69 73 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20  ist->nSrc-1];.  
22e09 69 66 28 20 70 44 61 74 61 62 61 73 65 20 26 26  if( pDatabase &&
22e0a 20 70 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d 30   pDatabase->z==0
22e0b 20 29 7b 0a 20 20 20 20 70 44 61 74 61 62 61 73   ){.    pDatabas
22e0c 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  e = 0;.  }.  if(
22e0d 20 70 44 61 74 61 62 61 73 65 20 26 26 20 70 54   pDatabase && pT
22e0e 61 62 6c 65 20 29 7b 0a 20 20 20 20 54 6f 6b 65  able ){.    Toke
22e0f 6e 20 2a 70 54 65 6d 70 20 3d 20 70 44 61 74 61  n *pTemp = pData
22e10 62 61 73 65 3b 0a 20 20 20 20 70 44 61 74 61 62  base;.    pDatab
22e11 61 73 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20  ase = pTable;.  
22e12 20 20 70 54 61 62 6c 65 20 3d 20 70 54 65 6d 70    pTable = pTemp
22e13 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 7a  ;.  }.  pItem->z
22e14 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
22e15 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
22e16 70 54 61 62 6c 65 29 3b 0a 20 20 70 49 74 65 6d  pTable);.  pItem
22e17 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71  ->zDatabase = sq
22e18 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
22e19 65 6e 28 64 62 2c 20 70 44 61 74 61 62 61 73 65  en(db, pDatabase
22e1a 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73  );.  return pLis
22e1b 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69  t;.}../*.** Assi
22e1c 67 6e 20 56 64 62 65 43 75 72 73 6f 72 20 69 6e  gn VdbeCursor in
22e1d 64 65 78 20 6e 75 6d 62 65 72 73 20 74 6f 20 61  dex numbers to a
22e1e 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 20 53  ll tables in a S
22e1f 72 63 4c 69 73 74 0a 2a 2f 0a 53 51 4c 49 54 45  rcList.*/.SQLITE
22e20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
22e21 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69  lite3SrcListAssi
22e22 67 6e 43 75 72 73 6f 72 73 28 50 61 72 73 65 20  gnCursors(Parse 
22e23 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74  *pParse, SrcList
22e24 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20   *pList){.  int 
22e25 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  i;.  struct SrcL
22e26 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
22e27 0a 20 20 61 73 73 65 72 74 28 70 4c 69 73 74 20  .  assert(pList 
22e28 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  || pParse->db->m
22e29 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
22e2a 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20   if( pList ){.  
22e2b 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d    for(i=0, pItem
22e2c 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69  =pList->a; i<pLi
22e2d 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
22e2e 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69  Item++){.      i
22e2f 66 28 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f  f( pItem->iCurso
22e30 72 3e 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  r>=0 ) break;.  
22e31 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 75 72 73      pItem->iCurs
22e32 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  or = pParse->nTa
22e33 62 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70  b++;.      if( p
22e34 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b  Item->pSelect ){
22e35 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22e36 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72  SrcListAssignCur
22e37 73 6f 72 73 28 70 50 61 72 73 65 2c 20 70 49 74  sors(pParse, pIt
22e38 65 6d 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 53 72  em->pSelect->pSr
22e39 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
22e3a 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  }.  }.}../*.** D
22e3b 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20  elete an entire 
22e3c 53 72 63 4c 69 73 74 20 69 6e 63 6c 75 64 69 6e  SrcList includin
22e3d 67 20 61 6c 6c 20 69 74 73 20 73 75 62 73 74 72  g all its substr
22e3e 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
22e3f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
22e40 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
22e41 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ete(sqlite3 *db,
22e42 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29   SrcList *pList)
22e43 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
22e44 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
22e45 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70   *pItem;.  if( p
22e46 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
22e47 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c  ;.  for(pItem=pL
22e48 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70  ist->a, i=0; i<p
22e49 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
22e4a 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73   pItem++){.    s
22e4b 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
22e4c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
22e4d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  e);.    sqlite3D
22e4e 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d  bFree(db, pItem-
22e4f 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c  >zName);.    sql
22e50 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
22e51 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20  Item->zAlias);. 
22e52 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
22e53 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 49 6e 64  (db, pItem->zInd
22e54 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ex);.    sqlite3
22e55 44 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65  DeleteTable(pIte
22e56 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20 73 71  m->pTab);.    sq
22e57 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
22e58 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65  e(db, pItem->pSe
22e59 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  lect);.    sqlit
22e5a 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
22e5b 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 20   pItem->pOn);.  
22e5c 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44    sqlite3IdListD
22e5d 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d  elete(db, pItem-
22e5e 3e 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20 20  >pUsing);.  }.  
22e5f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22e60 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a  , pList);.}../*.
22e61 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
22e62 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
22e63 20 70 61 72 73 65 72 20 74 6f 20 61 64 64 20 61   parser to add a
22e64 20 6e 65 77 20 74 65 72 6d 20 74 6f 20 74 68 65   new term to the
22e65 0a 2a 2a 20 65 6e 64 20 6f 66 20 61 20 67 72 6f  .** end of a gro
22e66 77 69 6e 67 20 46 52 4f 4d 20 63 6c 61 75 73 65  wing FROM clause
22e67 2e 20 20 54 68 65 20 22 70 22 20 70 61 72 61 6d  .  The "p" param
22e68 65 74 65 72 20 69 73 20 74 68 65 20 70 61 72 74  eter is the part
22e69 20 6f 66 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20   of.** the FROM 
22e6a 63 6c 61 75 73 65 20 74 68 61 74 20 68 61 73 20  clause that has 
22e6b 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6e  already been con
22e6c 73 74 72 75 63 74 65 64 2e 20 20 22 70 22 20 69  structed.  "p" i
22e6d 73 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 74 68 69  s NULL.** if thi
22e6e 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74  s is the first t
22e6f 65 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  erm of the FROM 
22e70 63 6c 61 75 73 65 2e 20 20 70 54 61 62 6c 65 20  clause.  pTable 
22e71 61 6e 64 20 70 44 61 74 61 62 61 73 65 0a 2a 2a  and pDatabase.**
22e72 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f 66   are the name of
22e73 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 64   the table and d
22e74 61 74 61 62 61 73 65 20 6e 61 6d 65 64 20 69 6e  atabase named in
22e75 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
22e76 20 74 65 72 6d 2e 0a 2a 2a 20 70 44 61 74 61 62   term..** pDatab
22e77 61 73 65 20 69 73 20 4e 55 4c 4c 20 69 66 20 74  ase is NULL if t
22e78 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
22e79 20 71 75 61 6c 69 66 69 65 72 20 69 73 20 6d 69   qualifier is mi
22e7a 73 73 69 6e 67 20 2d 20 74 68 65 0a 2a 2a 20 75  ssing - the.** u
22e7b 73 75 61 6c 20 63 61 73 65 2e 20 20 49 66 20 74  sual case.  If t
22e7c 68 65 20 74 65 72 6d 20 68 61 73 20 61 20 61 6c  he term has a al
22e7d 69 61 73 2c 20 74 68 65 6e 20 70 41 6c 69 61 73  ias, then pAlias
22e7e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a   points to the.*
22e7f 2a 20 61 6c 69 61 73 20 74 6f 6b 65 6e 2e 20 20  * alias token.  
22e80 49 66 20 74 68 65 20 74 65 72 6d 20 69 73 20 61  If the term is a
22e81 20 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e 20   subquery, then 
22e82 70 53 75 62 71 75 65 72 79 20 69 73 20 74 68 65  pSubquery is the
22e83 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65  .** SELECT state
22e84 6d 65 6e 74 20 74 68 61 74 20 74 68 65 20 73 75  ment that the su
22e85 62 71 75 65 72 79 20 65 6e 63 6f 64 65 73 2e 20  bquery encodes. 
22e86 20 54 68 65 20 70 54 61 62 6c 65 20 61 6e 64 0a   The pTable and.
22e87 2a 2a 20 70 44 61 74 61 62 61 73 65 20 70 61 72  ** pDatabase par
22e88 61 6d 65 74 65 72 73 20 61 72 65 20 4e 55 4c 4c  ameters are NULL
22e89 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 2e   for subqueries.
22e8a 20 20 54 68 65 20 70 4f 6e 20 61 6e 64 20 70 55    The pOn and pU
22e8b 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74 65  sing.** paramete
22e8c 72 73 20 61 72 65 20 74 68 65 20 63 6f 6e 74 65  rs are the conte
22e8d 6e 74 20 6f 66 20 74 68 65 20 4f 4e 20 61 6e 64  nt of the ON and
22e8e 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 2e 0a   USING clauses..
22e8f 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6e  **.** Return a n
22e90 65 77 20 53 72 63 4c 69 73 74 20 77 68 69 63 68  ew SrcList which
22e91 20 65 6e 63 6f 64 65 73 20 69 73 20 74 68 65 20   encodes is the 
22e92 46 52 4f 4d 20 77 69 74 68 20 74 68 65 20 6e 65  FROM with the ne
22e93 77 0a 2a 2a 20 74 65 72 6d 20 61 64 64 65 64 2e  w.** term added.
22e94 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22e95 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69  TE SrcList *sqli
22e96 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
22e97 46 72 6f 6d 54 65 72 6d 28 0a 20 20 50 61 72 73  FromTerm(.  Pars
22e98 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
22e99 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
22e9a 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
22e9b 69 73 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ist *p,         
22e9c 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20      /* The left 
22e9d 70 61 72 74 20 6f 66 20 74 68 65 20 46 52 4f 4d  part of the FROM
22e9e 20 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79 20   clause already 
22e9f 73 65 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  seen */.  Token 
22ea0 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20  *pTable,        
22ea1 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
22ea2 20 74 61 62 6c 65 20 74 6f 20 61 64 64 20 74 6f   table to add to
22ea3 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
22ea4 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61   */.  Token *pDa
22ea5 74 61 62 61 73 65 2c 20 20 20 20 20 20 20 2f 2a  tabase,       /*
22ea6 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
22ea7 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
22ea8 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b   pTable */.  Tok
22ea9 65 6e 20 2a 70 41 6c 69 61 73 2c 20 20 20 20 20  en *pAlias,     
22eaa 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68       /* The righ
22eab 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74  t-hand side of t
22eac 68 65 20 41 53 20 73 75 62 65 78 70 72 65 73 73  he AS subexpress
22ead 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ion */.  Select 
22eae 2a 70 53 75 62 71 75 65 72 79 2c 20 20 20 20 20  *pSubquery,     
22eaf 20 2f 2a 20 41 20 73 75 62 71 75 65 72 79 20 75   /* A subquery u
22eb0 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20  sed in place of 
22eb1 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  a table name */.
22eb2 20 20 45 78 70 72 20 2a 70 4f 6e 2c 20 20 20 20    Expr *pOn,    
22eb3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22eb4 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20   ON clause of a 
22eb5 6a 6f 69 6e 20 2a 2f 0a 20 20 49 64 4c 69 73 74  join */.  IdList
22eb6 20 2a 70 55 73 69 6e 67 20 20 20 20 20 20 20 20   *pUsing        
22eb7 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63    /* The USING c
22eb8 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20  lause of a join 
22eb9 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 53  */.){.  struct S
22eba 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
22ebb 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  em;.  sqlite3 *d
22ebc 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
22ebd 20 20 70 20 3d 20 73 71 6c 69 74 65 33 53 72 63    p = sqlite3Src
22ebe 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20 70  ListAppend(db, p
22ebf 2c 20 70 54 61 62 6c 65 2c 20 70 44 61 74 61 62  , pTable, pDatab
22ec0 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30  ase);.  if( p==0
22ec1 20 7c 7c 20 70 2d 3e 6e 53 72 63 3d 3d 30 20 29   || p->nSrc==0 )
22ec2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
22ec3 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4f 6e 29  rDelete(db, pOn)
22ec4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c  ;.    sqlite3IdL
22ec5 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 55  istDelete(db, pU
22ec6 73 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69 74  sing);.    sqlit
22ec7 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
22ec8 62 2c 20 70 53 75 62 71 75 65 72 79 29 3b 0a 20  b, pSubquery);. 
22ec9 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d     return p;.  }
22eca 0a 20 20 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61  .  pItem = &p->a
22ecb 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 69  [p->nSrc-1];.  i
22ecc 66 28 20 70 41 6c 69 61 73 20 26 26 20 70 41 6c  f( pAlias && pAl
22ecd 69 61 73 2d 3e 6e 20 29 7b 0a 20 20 20 20 70 49  ias->n ){.    pI
22ece 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71  tem->zAlias = sq
22ecf 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
22ed0 65 6e 28 64 62 2c 20 70 41 6c 69 61 73 29 3b 0a  en(db, pAlias);.
22ed1 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 70 53 65    }.  pItem->pSe
22ed2 6c 65 63 74 20 3d 20 70 53 75 62 71 75 65 72 79  lect = pSubquery
22ed3 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 4f 6e 20 3d  ;.  pItem->pOn =
22ed4 20 70 4f 6e 3b 0a 20 20 70 49 74 65 6d 2d 3e 70   pOn;.  pItem->p
22ed5 55 73 69 6e 67 20 3d 20 70 55 73 69 6e 67 3b 0a  Using = pUsing;.
22ed6 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
22ed7 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e 44 45  *.** Add an INDE
22ed8 58 45 44 20 42 59 20 6f 72 20 4e 4f 54 20 49 4e  XED BY or NOT IN
22ed9 44 45 58 45 44 20 63 6c 61 75 73 65 20 74 6f 20  DEXED clause to 
22eda 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
22edb 79 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c 65 6d  y added .** elem
22edc 65 6e 74 20 6f 66 20 74 68 65 20 73 6f 75 72 63  ent of the sourc
22edd 65 2d 6c 69 73 74 20 70 61 73 73 65 64 20 61 73  e-list passed as
22ede 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
22edf 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
22ee0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
22ee1 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78  ite3SrcListIndex
22ee2 65 64 42 79 28 50 61 72 73 65 20 2a 70 50 61 72  edBy(Parse *pPar
22ee3 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20  se, SrcList *p, 
22ee4 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65 64 42  Token *pIndexedB
22ee5 79 29 7b 0a 20 20 69 66 28 20 70 49 6e 64 65 78  y){.  if( pIndex
22ee6 65 64 42 79 20 26 26 20 70 20 26 26 20 70 2d 3e  edBy && p && p->
22ee7 6e 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 73 74  nSrc>0 ){.    st
22ee8 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
22ee9 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61  m *pItem = &p->a
22eea 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 20  [p->nSrc-1];.   
22eeb 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e   assert( pItem->
22eec 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 26 26  notIndexed==0 &&
22eed 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 3d 3d   pItem->zIndex==
22eee 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e  0 );.    if( pIn
22eef 64 65 78 65 64 42 79 2d 3e 6e 3d 3d 31 20 26 26  dexedBy->n==1 &&
22ef0 20 21 70 49 6e 64 65 78 65 64 42 79 2d 3e 7a 20   !pIndexedBy->z 
22ef1 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 22 4e  ){.      /* A "N
22ef2 4f 54 20 49 4e 44 45 58 45 44 22 20 63 6c 61 75  OT INDEXED" clau
22ef3 73 65 20 77 61 73 20 73 75 70 70 6c 69 65 64 2e  se was supplied.
22ef4 20 53 65 65 20 70 61 72 73 65 2e 79 20 0a 20 20   See parse.y .  
22ef5 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 75 63 74      ** construct
22ef6 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 22 20 66   "indexed_opt" f
22ef7 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 20  or details. */. 
22ef8 20 20 20 20 20 70 49 74 65 6d 2d 3e 6e 6f 74 49       pItem->notI
22ef9 6e 64 65 78 65 64 20 3d 20 31 3b 0a 20 20 20 20  ndexed = 1;.    
22efa 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 74  }else{.      pIt
22efb 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c  em->zIndex = sql
22efc 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
22efd 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49  n(pParse->db, pI
22efe 6e 64 65 78 65 64 42 79 29 3b 0a 20 20 20 20 7d  ndexedBy);.    }
22eff 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68  .  }.}../*.** Wh
22f00 65 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20 61  en building up a
22f01 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e 20   FROM clause in 
22f02 74 68 65 20 70 61 72 73 65 72 2c 20 74 68 65 20  the parser, the 
22f03 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a  join operator.**
22f04 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61 74   is initially at
22f05 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65  tached to the le
22f06 66 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75 74  ft operand.  But
22f07 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
22f08 74 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20 74  tor.** expects t
22f09 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72  he join operator
22f0a 20 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72 69   to be on the ri
22f0b 67 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54 68  ght operand.  Th
22f0c 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53 68  is routine.** Sh
22f0d 69 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f 70  ifts all join op
22f0e 65 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65 66  erators from lef
22f0f 74 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20 61  t to right for a
22f10 6e 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a 2a  n entire FROM.**
22f11 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 45   clause..**.** E
22f12 78 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65 20  xample: Suppose 
22f13 74 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b 65  the join is like
22f14 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
22f15 20 20 20 20 20 20 20 41 20 6e 61 74 75 72 61 6c         A natural
22f16 20 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a 2a   cross join B.**
22f17 0a 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72  .** The operator
22f18 20 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72 6f   is "natural cro
22f19 73 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 41  ss join".  The A
22f1a 20 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 20   and B operands 
22f1b 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e  are stored.** in
22f1c 20 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e   p->a[0] and p->
22f1d 61 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76 65  a[1], respective
22f1e 6c 79 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  ly.  The parser 
22f1f 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 73  initially stores
22f20 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f 72   the.** operator
22f21 20 77 69 74 68 20 41 2e 20 20 54 68 69 73 20 72   with A.  This r
22f22 6f 75 74 69 6e 65 20 73 68 69 66 74 73 20 74 68  outine shifts th
22f23 61 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 72  at operator over
22f24 20 74 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49 54 45   to B..*/.SQLITE
22f25 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
22f26 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66  lite3SrcListShif
22f27 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73  tJoinType(SrcLis
22f28 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 26  t *p){.  if( p &
22f29 26 20 70 2d 3e 61 20 29 7b 0a 20 20 20 20 69 6e  & p->a ){.    in
22f2a 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 70  t i;.    for(i=p
22f2b 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20 69  ->nSrc-1; i>0; i
22f2c 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 5b  --){.      p->a[
22f2d 69 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 2d  i].jointype = p-
22f2e 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65  >a[i-1].jointype
22f2f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61  ;.    }.    p->a
22f30 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 30  [0].jointype = 0
22f31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42  ;.  }.}../*.** B
22f32 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  egin a transacti
22f33 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  on.*/.SQLITE_PRI
22f34 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
22f35 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f  3BeginTransactio
22f36 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
22f37 20 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73 71   int type){.  sq
22f38 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62  lite3 *db;.  Vdb
22f39 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a  e *v;.  int i;..
22f3a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20    if( pParse==0 
22f3b 7c 7c 20 28 64 62 3d 70 50 61 72 73 65 2d 3e 64  || (db=pParse->d
22f3c 62 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44 62  b)==0 || db->aDb
22f3d 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74  [0].pBt==0 ) ret
22f3e 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73  urn;.  if( pPars
22f3f 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
22f40 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
22f41 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69  turn;.  if( sqli
22f42 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
22f43 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  rse, SQLITE_TRAN
22f44 53 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e 22  SACTION, "BEGIN"
22f45 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75 72 6e  , 0, 0) ) return
22f46 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  ;..  v = sqlite3
22f47 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
22f48 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75  .  if( !v ) retu
22f49 72 6e 3b 0a 20 20 69 66 28 20 74 79 70 65 21 3d  rn;.  if( type!=
22f4a 54 4b 5f 44 45 46 45 52 52 45 44 20 29 7b 0a 20  TK_DEFERRED ){. 
22f4b 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
22f4c 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
22f4d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22f4e 64 4f 70 32 28 76 2c 20 4f 50 5f 54 72 61 6e 73  dOp2(v, OP_Trans
22f4f 61 63 74 69 6f 6e 2c 20 69 2c 20 28 74 79 70 65  action, i, (type
22f50 3d 3d 54 4b 5f 45 58 43 4c 55 53 49 56 45 29 2b  ==TK_EXCLUSIVE)+
22f51 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
22f52 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
22f53 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , i);.    }.  }.
22f54 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22f55 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f  Op2(v, OP_AutoCo
22f56 6d 6d 69 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a  mmit, 0, 0);.}..
22f57 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20 74  /*.** Commit a t
22f58 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51  ransaction.*/.SQ
22f59 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
22f5a 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54  d sqlite3CommitT
22f5b 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65  ransaction(Parse
22f5c 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c   *pParse){.  sql
22f5d 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65  ite3 *db;.  Vdbe
22f5e 20 2a 76 3b 0a 0a 20 20 69 66 28 20 70 50 61 72   *v;..  if( pPar
22f5f 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61  se==0 || (db=pPa
22f60 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64  rse->db)==0 || d
22f61 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30  b->aDb[0].pBt==0
22f62 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
22f63 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
22f64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
22f65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  d ) return;.  if
22f66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
22f67 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
22f68 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22  E_TRANSACTION, "
22f69 43 4f 4d 4d 49 54 22 2c 20 30 2c 20 30 29 20 29  COMMIT", 0, 0) )
22f6a 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20   return;..  v = 
22f6b 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
22f6c 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
22f6d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
22f6e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
22f6f 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 30 29  utoCommit, 1, 0)
22f70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
22f71 6f 6c 6c 62 61 63 6b 20 61 20 74 72 61 6e 73 61  ollback a transa
22f72 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ction.*/.SQLITE_
22f73 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
22f74 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e  ite3RollbackTran
22f75 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70  saction(Parse *p
22f76 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
22f77 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76  3 *db;.  Vdbe *v
22f78 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d  ;..  if( pParse=
22f79 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61 72 73 65  =0 || (db=pParse
22f7a 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e  ->db)==0 || db->
22f7b 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20  aDb[0].pBt==0 ) 
22f7c 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50  return;.  if( pP
22f7d 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
22f7e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
22f7f 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73   return;.  if( s
22f80 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
22f81 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54  pParse, SQLITE_T
22f82 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52 4f 4c  RANSACTION, "ROL
22f83 4c 42 41 43 4b 22 2c 20 30 2c 20 30 29 20 29 20  LBACK", 0, 0) ) 
22f84 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73  return;..  v = s
22f85 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
22f86 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29  arse);.  if( v )
22f87 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
22f88 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75  eAddOp2(v, OP_Au
22f89 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 31 29 3b  toCommit, 1, 1);
22f8a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
22f8b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
22f8c 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
22f8d 73 65 72 20 77 68 65 6e 20 69 74 20 70 61 72 73  ser when it pars
22f8e 65 73 20 61 20 63 6f 6d 6d 61 6e 64 20 74 6f 20  es a command to 
22f8f 63 72 65 61 74 65 2c 0a 2a 2a 20 72 65 6c 65 61  create,.** relea
22f90 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 61  se or rollback a
22f91 6e 20 53 51 4c 20 73 61 76 65 70 6f 69 6e 74 2e  n SQL savepoint.
22f92 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
22f93 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
22f94 53 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65 20  Savepoint(Parse 
22f95 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 70 2c  *pParse, int op,
22f96 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a   Token *pName){.
22f97 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
22f98 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
22f99 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c  oken(pParse->db,
22f9a 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a   pName);.  if( z
22f9b 4e 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65  Name ){.    Vdbe
22f9c 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
22f9d 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 23 69  Vdbe(pParse);.#i
22f9e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22f9f 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
22fa0 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
22fa1 20 63 68 61 72 20 2a 61 7a 5b 5d 20 3d 20 7b 20   char *az[] = { 
22fa2 22 42 45 47 49 4e 22 2c 20 22 52 45 4c 45 41 53  "BEGIN", "RELEAS
22fa3 45 22 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 20 7d  E", "ROLLBACK" }
22fa4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 53  ;.    assert( !S
22fa5 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20 26  AVEPOINT_BEGIN &
22fa6 26 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  & SAVEPOINT_RELE
22fa7 41 53 45 3d 3d 31 20 26 26 20 53 41 56 45 50 4f  ASE==1 && SAVEPO
22fa8 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3d 3d 32 20  INT_ROLLBACK==2 
22fa9 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66  );.#endif.    if
22faa 28 20 21 76 20 7c 7c 20 73 71 6c 69 74 65 33 41  ( !v || sqlite3A
22fab 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
22fac 20 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e   SQLITE_SAVEPOIN
22fad 54 2c 20 61 7a 5b 6f 70 5d 2c 20 7a 4e 61 6d 65  T, az[op], zName
22fae 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71  , 0) ){.      sq
22faf 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72  lite3DbFree(pPar
22fb0 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  se->db, zName);.
22fb1 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
22fb2 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
22fb3 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
22fb4 53 61 76 65 70 6f 69 6e 74 2c 20 6f 70 2c 20 30  Savepoint, op, 0
22fb5 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 50 34 5f 44  , 0, zName, P4_D
22fb6 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a  YNAMIC);.  }.}..
22fb7 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
22fb8 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73  the TEMP databas
22fb9 65 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 61 76  e is open and av
22fba 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 2e  ailable for use.
22fbb 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20    Return.** the 
22fbc 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
22fbd 2e 20 20 4c 65 61 76 65 20 61 6e 79 20 65 72 72  .  Leave any err
22fbe 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e 20 74  or messages in t
22fbf 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74  he pParse struct
22fc0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ure..*/.SQLITE_P
22fc1 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
22fc2 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61  e3OpenTempDataba
22fc3 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  se(Parse *pParse
22fc4 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
22fc5 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
22fc6 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e   if( db->aDb[1].
22fc7 70 42 74 3d 3d 30 20 26 26 20 21 70 50 61 72 73  pBt==0 && !pPars
22fc8 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20  e->explain ){.  
22fc9 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73 74    int rc;.    st
22fca 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 66  atic const int f
22fcb 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20 20 20  lags = .        
22fcc 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45    SQLITE_OPEN_RE
22fcd 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20  ADWRITE |.      
22fce 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
22fcf 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20  CREATE |.       
22fd0 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45     SQLITE_OPEN_E
22fd1 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20  XCLUSIVE |.     
22fd2 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
22fd3 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c  _DELETEONCLOSE |
22fd4 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
22fd5 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a  E_OPEN_TEMP_DB;.
22fd6 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
22fd7 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62  3BtreeFactory(db
22fd8 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44  , 0, 0, SQLITE_D
22fd9 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
22fda 45 2c 20 66 6c 61 67 73 2c 0a 20 20 20 20 20 20  E, flags,.      
22fdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22fdc 20 20 20 20 20 20 20 20 20 20 20 26 64 62 2d 3e             &db->
22fdd 61 44 62 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20  aDb[1].pBt);.   
22fde 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
22fdf 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
22fe0 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
22fe1 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f  se, "unable to o
22fe2 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20  pen a temporary 
22fe3 64 61 74 61 62 61 73 65 20 22 0a 20 20 20 20 20  database ".     
22fe4 20 20 20 22 66 69 6c 65 20 66 6f 72 20 73 74 6f     "file for sto
22fe5 72 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74  ring temporary t
22fe6 61 62 6c 65 73 22 29 3b 0a 20 20 20 20 20 20 70  ables");.      p
22fe7 50 61 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a  Parse->rc = rc;.
22fe8 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
22fe9 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
22fea 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  ( (db->flags & S
22feb 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 29 3d 3d  QLITE_InTrans)==
22fec 30 20 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d  0 || db->autoCom
22fed 6d 69 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  mit );.    asser
22fee 74 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  t( db->aDb[1].pS
22fef 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c  chema );.    sql
22ff0 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
22ff1 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74 72 65  Mode(sqlite3Btre
22ff2 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 31  ePager(db->aDb[1
22ff3 5d 2e 70 42 74 29 2c 0a 20 20 20 20 20 20 20 20  ].pBt),.        
22ff4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22ff5 20 20 20 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72      db->dfltJour
22ff6 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20  nalMode);.  }.  
22ff7 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
22ff8 2a 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45  ** Generate VDBE
22ff9 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
22ffa 76 65 72 69 66 79 20 74 68 65 20 73 63 68 65 6d  verify the schem
22ffb 61 20 63 6f 6f 6b 69 65 20 61 6e 64 20 73 74 61  a cookie and sta
22ffc 72 74 0a 2a 2a 20 61 20 72 65 61 64 2d 74 72 61  rt.** a read-tra
22ffd 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 61 6c 6c  nsaction for all
22ffe 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20   named database 
22fff 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  files..**.** It 
23000 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61  is important tha
23001 74 20 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f  t all schema coo
23002 6b 69 65 73 20 62 65 20 76 65 72 69 66 69 65 64  kies be verified
23003 20 61 6e 64 20 61 6c 6c 0a 2a 2a 20 72 65 61 64   and all.** read
23004 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 62 65   transactions be
23005 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20   started before 
23006 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 68 61  anything else ha
23007 70 70 65 6e 73 20 69 6e 0a 2a 2a 20 74 68 65 20  ppens in.** the 
23008 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 20 20 42  VDBE program.  B
23009 75 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ut this routine 
2300a 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 66  can be called af
2300b 74 65 72 20 6d 75 63 68 20 6f 74 68 65 72 0a 2a  ter much other.*
2300c 2a 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 20  * code has been 
2300d 67 65 6e 65 72 61 74 65 64 2e 20 20 53 6f 20 68  generated.  So h
2300e 65 72 65 20 69 73 20 77 68 61 74 20 77 65 20 64  ere is what we d
2300f 6f 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  o:.**.** The fir
23010 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75  st time this rou
23011 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
23012 77 65 20 63 6f 64 65 20 61 6e 20 4f 50 5f 47 6f  we code an OP_Go
23013 74 6f 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c 20  to that.** will 
23014 6a 75 6d 70 20 74 6f 20 61 20 73 75 62 72 6f 75  jump to a subrou
23015 74 69 6e 65 20 61 74 20 74 68 65 20 65 6e 64 20  tine at the end 
23016 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20  of the program. 
23017 20 54 68 65 6e 20 77 65 0a 2a 2a 20 72 65 63 6f   Then we.** reco
23018 72 64 20 65 76 65 72 79 20 64 61 74 61 62 61 73  rd every databas
23019 65 20 74 68 61 74 20 6e 65 65 64 73 20 69 74 73  e that needs its
2301a 20 73 63 68 65 6d 61 20 76 65 72 69 66 69 65 64   schema verified
2301b 20 69 6e 20 74 68 65 0a 2a 2a 20 70 50 61 72 73   in the.** pPars
2301c 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 66 69  e->cookieMask fi
2301d 65 6c 64 2e 20 20 4c 61 74 65 72 2c 20 61 66 74  eld.  Later, aft
2301e 65 72 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 64  er all other cod
2301f 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 67 65  e has been.** ge
23020 6e 65 72 61 74 65 64 2c 20 74 68 65 20 73 75 62  nerated, the sub
23021 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 6f 65  routine that doe
23022 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 65 72  s the cookie ver
23023 69 66 69 63 61 74 69 6f 6e 73 20 61 6e 64 0a 2a  ifications and.*
23024 2a 20 73 74 61 72 74 73 20 74 68 65 20 74 72 61  * starts the tra
23025 6e 73 61 63 74 69 6f 6e 73 20 77 69 6c 6c 20 62  nsactions will b
23026 65 20 63 6f 64 65 64 20 61 6e 64 20 74 68 65 20  e coded and the 
23027 4f 50 5f 47 6f 74 6f 20 50 32 20 76 61 6c 75 65  OP_Goto P2 value
23028 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 6d 61 64 65  .** will be made
23029 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61   to point to tha
2302a 74 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 54  t subroutine.  T
2302b 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66  he generation of
2302c 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 20 76   the.** cookie v
2302d 65 72 69 66 69 63 61 74 69 6f 6e 20 73 75 62 72  erification subr
2302e 6f 75 74 69 6e 65 20 63 6f 64 65 20 68 61 70 70  outine code happ
2302f 65 6e 73 20 69 6e 20 73 71 6c 69 74 65 33 46 69  ens in sqlite3Fi
23030 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2a  nishCoding()..**
23031 0a 2a 2a 20 49 66 20 69 44 62 3c 30 20 74 68 65  .** If iDb<0 the
23032 6e 20 63 6f 64 65 20 74 68 65 20 4f 50 5f 47 6f  n code the OP_Go
23033 74 6f 20 6f 6e 6c 79 20 2d 20 64 6f 6e 27 74 20  to only - don't 
23034 73 65 74 20 66 6c 61 67 20 74 6f 20 76 65 72 69  set flag to veri
23035 66 79 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61  fy the.** schema
23036 20 6f 6e 20 61 6e 79 20 64 61 74 61 62 61 73 65   on any database
23037 73 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20  s.  This can be 
23038 75 73 65 64 20 74 6f 20 70 6f 73 69 74 69 6f 6e  used to position
23039 20 74 68 65 20 4f 50 5f 47 6f 74 6f 0a 2a 2a 20   the OP_Goto.** 
2303a 65 61 72 6c 79 20 69 6e 20 74 68 65 20 63 6f 64  early in the cod
2303b 65 2c 20 62 65 66 6f 72 65 20 77 65 20 6b 6e 6f  e, before we kno
2303c 77 20 69 66 20 61 6e 79 20 64 61 74 61 62 61 73  w if any databas
2303d 65 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 62 65  e tables will be
2303e 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45   used..*/.SQLITE
2303f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23040 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
23041 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61  chema(Parse *pPa
23042 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20  rse, int iDb){. 
23043 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
23044 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 6d  Vdbe *v;.  int m
23045 61 73 6b 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69  ask;..  v = sqli
23046 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
23047 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  e);.  if( v==0 )
23048 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 54 68 69   return;  /* Thi
23049 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69  s only happens i
2304a 66 20 74 68 65 72 65 20 77 61 73 20 61 20 70 72  f there was a pr
2304b 69 6f 72 20 65 72 72 6f 72 20 2a 2f 0a 20 20 64  ior error */.  d
2304c 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2304d 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6f    if( pParse->co
2304e 6f 6b 69 65 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20  okieGoto==0 ){. 
2304f 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69     pParse->cooki
23050 65 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56  eGoto = sqlite3V
23051 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
23052 47 6f 74 6f 2c 20 30 2c 20 30 29 2b 31 3b 0a 20  Goto, 0, 0)+1;. 
23053 20 7d 0a 20 20 69 66 28 20 69 44 62 3e 3d 30 20   }.  if( iDb>=0 
23054 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ){.    assert( i
23055 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
23056 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
23057 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 7c 7c  b[iDb].pBt!=0 ||
23058 20 69 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 61   iDb==1 );.    a
23059 73 73 65 72 74 28 20 69 44 62 3c 53 51 4c 49 54  ssert( iDb<SQLIT
2305a 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32  E_MAX_ATTACHED+2
2305b 20 29 3b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 31   );.    mask = 1
2305c 3c 3c 69 44 62 3b 0a 20 20 20 20 69 66 28 20 28  <<iDb;.    if( (
2305d 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61  pParse->cookieMa
2305e 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b  sk & mask)==0 ){
2305f 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63  .      pParse->c
23060 6f 6f 6b 69 65 4d 61 73 6b 20 7c 3d 20 6d 61 73  ookieMask |= mas
23061 6b 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  k;.      pParse-
23062 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62  >cookieValue[iDb
23063 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  ] = db->aDb[iDb]
23064 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  .pSchema->schema
23065 5f 63 6f 6f 6b 69 65 3b 0a 20 20 20 20 20 20 69  _cookie;.      i
23066 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
23067 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20  && iDb==1 ){.   
23068 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e       sqlite3Open
23069 54 65 6d 70 44 61 74 61 62 61 73 65 28 70 50 61  TempDatabase(pPa
2306a 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rse);.      }.  
2306b 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2306c 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   Generate VDBE c
2306d 6f 64 65 20 74 68 61 74 20 70 72 65 70 61 72 65  ode that prepare
2306e 73 20 66 6f 72 20 64 6f 69 6e 67 20 61 6e 20 6f  s for doing an o
2306f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a  peration that.**
23070 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68   might change th
23071 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  e database..**.*
23072 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
23073 74 61 72 74 73 20 61 20 6e 65 77 20 74 72 61 6e  tarts a new tran
23074 73 61 63 74 69 6f 6e 20 69 66 20 77 65 20 61 72  saction if we ar
23075 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 77 69  e not already wi
23076 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61  thin.** a transa
23077 63 74 69 6f 6e 2e 20 20 49 66 20 77 65 20 61 72  ction.  If we ar
23078 65 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  e already within
23079 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20   a transaction, 
2307a 74 68 65 6e 20 61 20 63 68 65 63 6b 70 6f 69 6e  then a checkpoin
2307b 74 0a 2a 2a 20 69 73 20 73 65 74 20 69 66 20 74  t.** is set if t
2307c 68 65 20 73 65 74 53 74 61 74 65 6d 65 6e 74 20  he setStatement 
2307d 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75  parameter is tru
2307e 65 2e 20 20 41 20 63 68 65 63 6b 70 6f 69 6e 74  e.  A checkpoint
2307f 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65   should.** be se
23080 74 20 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73  t for operations
23081 20 74 68 61 74 20 6d 69 67 68 74 20 66 61 69 6c   that might fail
23082 20 28 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74   (due to a const
23083 72 61 69 6e 74 29 20 70 61 72 74 20 6f 66 0a 2a  raint) part of.*
23084 2a 20 74 68 65 20 77 61 79 20 74 68 72 6f 75 67  * the way throug
23085 68 20 61 6e 64 20 77 68 69 63 68 20 77 69 6c 6c  h and which will
23086 20 6e 65 65 64 20 74 6f 20 75 6e 64 6f 20 73 6f   need to undo so
23087 6d 65 20 77 72 69 74 65 73 20 77 69 74 68 6f 75  me writes withou
23088 74 20 68 61 76 69 6e 67 20 74 6f 0a 2a 2a 20 72  t having to.** r
23089 6f 6c 6c 62 61 63 6b 20 74 68 65 20 77 68 6f 6c  ollback the whol
2308a 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  e transaction.  
2308b 46 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 77  For operations w
2308c 68 65 72 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61  here all constra
2308d 69 6e 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 63  ints.** can be c
2308e 68 65 63 6b 65 64 20 62 65 66 6f 72 65 20 61 6e  hecked before an
2308f 79 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61  y changes are ma
23090 64 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  de to the databa
23091 73 65 2c 20 69 74 20 69 73 20 6e 65 76 65 72 0a  se, it is never.
23092 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20  ** necessary to 
23093 75 6e 64 6f 20 61 20 77 72 69 74 65 20 61 6e 64  undo a write and
23094 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 20   the checkpoint 
23095 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 65  should not be se
23096 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
23097 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23098 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
23099 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72  tion(Parse *pPar
2309a 73 65 2c 20 69 6e 74 20 73 65 74 53 74 61 74 65  se, int setState
2309b 6d 65 6e 74 2c 20 69 6e 74 20 69 44 62 29 7b 0a  ment, int iDb){.
2309c 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
2309d 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2309e 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  e);.  if( v==0 )
2309f 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
230a0 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65  e3CodeVerifySche
230a1 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ma(pParse, iDb);
230a2 0a 20 20 70 50 61 72 73 65 2d 3e 77 72 69 74 65  .  pParse->write
230a3 4d 61 73 6b 20 7c 3d 20 31 3c 3c 69 44 62 3b 0a  Mask |= 1<<iDb;.
230a4 20 20 69 66 28 20 73 65 74 53 74 61 74 65 6d 65    if( setStateme
230a5 6e 74 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65  nt && pParse->ne
230a6 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73  sted==0 ){.    s
230a7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
230a8 28 76 2c 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74  (v, OP_Statement
230a9 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  , iDb);.  }.}../
230aa 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  *.** Check to se
230ab 65 20 69 66 20 70 49 6e 64 65 78 20 75 73 65 73  e if pIndex uses
230ac 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
230ad 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20  equence pColl.  
230ae 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69  Return.** true i
230af 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20 66 61  f it does and fa
230b0 6c 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e  lse if it does n
230b1 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ot..*/.#ifndef S
230b2 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44  QLITE_OMIT_REIND
230b3 45 58 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  EX.static int co
230b4 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e  llationMatch(con
230b5 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20  st char *zColl, 
230b6 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a  Index *pIndex){.
230b7 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
230b8 3d 30 3b 20 69 3c 70 49 6e 64 65 78 2d 3e 6e 43  =0; i<pIndex->nC
230b9 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
230ba 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
230bb 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b   pIndex->azColl[
230bc 69 5d 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 7a  i];.    if( z==z
230bd 43 6f 6c 6c 20 7c 7c 20 28 7a 20 26 26 20 7a 43  Coll || (z && zC
230be 6f 6c 6c 20 26 26 20 30 3d 3d 73 71 6c 69 74 65  oll && 0==sqlite
230bf 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c  3StrICmp(z, zCol
230c0 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  l)) ){.      ret
230c1 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
230c2 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
230c3 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63  endif../*.** Rec
230c4 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63  ompute all indic
230c5 65 73 20 6f 66 20 70 54 61 62 20 74 68 61 74 20  es of pTab that 
230c6 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  use the collatin
230c7 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c  g sequence pColl
230c8 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30  ..** If pColl==0
230c9 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 20   then recompute 
230ca 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70  all indices of p
230cb 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  Tab..*/.#ifndef 
230cc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e  SQLITE_OMIT_REIN
230cd 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69 64 20  DEX.static void 
230ce 72 65 69 6e 64 65 78 54 61 62 6c 65 28 50 61 72  reindexTable(Par
230cf 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
230d0 65 20 2a 70 54 61 62 2c 20 63 68 61 72 20 63 6f  e *pTab, char co
230d1 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49  nst *zColl){.  I
230d2 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20  ndex *pIndex;   
230d3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
230d4 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65   index associate
230d5 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a  d with pTab */..
230d6 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54 61    for(pIndex=pTa
230d7 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  b->pIndex; pInde
230d8 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65 78  x; pIndex=pIndex
230d9 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
230da 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f  ( zColl==0 || co
230db 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f  llationMatch(zCo
230dc 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b 0a 20  ll, pIndex) ){. 
230dd 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73       int iDb = s
230de 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
230df 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
230e0 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
230e1 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67        sqlite3Beg
230e2 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
230e3 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29  (pParse, 0, iDb)
230e4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
230e5 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73  efillIndex(pPars
230e6 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a  e, pIndex, -1);.
230e7 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64      }.  }.}.#end
230e8 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70  if../*.** Recomp
230e9 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  ute all indices 
230ea 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e  of all tables in
230eb 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 77   all databases w
230ec 68 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69  here the.** indi
230ed 63 65 73 20 75 73 65 20 74 68 65 20 63 6f 6c 6c  ces use the coll
230ee 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70  ating sequence p
230ef 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d  Coll.  If pColl=
230f0 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74  =0 then recomput
230f1 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73  e.** all indices
230f2 20 65 76 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a   everywhere..*/.
230f3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
230f4 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74  MIT_REINDEX.stat
230f5 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78 44  ic void reindexD
230f6 61 74 61 62 61 73 65 73 28 50 61 72 73 65 20 2a  atabases(Parse *
230f7 70 50 61 72 73 65 2c 20 63 68 61 72 20 63 6f 6e  pParse, char con
230f8 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62  st *zColl){.  Db
230f9 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 20   *pDb;          
230fa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
230fb 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 2a  ingle database *
230fc 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
230fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
230fe 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
230ff 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a  index number */.
23100 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23101 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a  pParse->db;   /*
23102 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
23103 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61  nnection */.  Ha
23104 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20  shElem *k;      
23105 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
23106 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74 61   looping over ta
23107 62 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20  bles in pDb */. 
23108 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
23109 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2310a 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64  A table in the d
2310b 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20 66 6f  atabase */..  fo
2310c 72 28 69 44 62 3d 30 2c 20 70 44 62 3d 64 62 2d  r(iDb=0, pDb=db-
2310d 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d 3e 6e 44  >aDb; iDb<db->nD
2310e 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62 2b 2b 29  b; iDb++, pDb++)
2310f 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 44  {.    assert( pD
23110 62 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28  b!=0 );.    for(
23111 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  k=sqliteHashFirs
23112 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  t(&pDb->pSchema-
23113 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b 3b 20 6b  >tblHash);  k; k
23114 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
23115 6b 29 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20  k)){.      pTab 
23116 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65  = (Table*)sqlite
23117 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20  HashData(k);.   
23118 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28     reindexTable(
23119 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 43  pParse, pTab, zC
2311a 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  oll);.    }.  }.
2311b 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
2311c 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
2311d 72 20 74 68 65 20 52 45 49 4e 44 45 58 20 63 6f  r the REINDEX co
2311e 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  mmand..**.**    
2311f 20 20 20 20 52 45 49 4e 44 45 58 20 20 20 20 20      REINDEX     
23120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23121 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20         -- 1.**  
23122 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3c        REINDEX  <
23123 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20 20 20  collation>      
23124 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a           -- 2.**
23125 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20          REINDEX 
23126 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74   ?<database>.?<t
23127 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a  ablename>  -- 3.
23128 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45  **        REINDE
23129 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f  X  ?<database>.?
2312a 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20 2d 2d 20  <indexname>  -- 
2312b 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63  4.**.** Form 1 c
2312c 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69 63 65  auses all indice
2312d 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63 68 65  s in all attache
2312e 64 20 64 61 74 61 62 61 73 65 73 20 74 6f 20 62  d databases to b
2312f 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f  e rebuilt..** Fo
23130 72 6d 20 32 20 72 65 62 75 69 6c 64 73 20 61 6c  rm 2 rebuilds al
23131 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c  l indices in all
23132 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20   databases that 
23133 75 73 65 20 74 68 65 20 6e 61 6d 65 64 0a 2a 2a  use the named.**
23134 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
23135 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33 20 61 6e  ion.  Forms 3 an
23136 64 20 34 20 72 65 62 75 69 6c 64 20 74 68 65 20  d 4 rebuild the 
23137 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f 72 20 61  named index or a
23138 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 61 73  ll.** indices as
23139 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2313a 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a  e named table..*
2313b 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2313c 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 53 51  _OMIT_REINDEX.SQ
2313d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2313e 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78  d sqlite3Reindex
2313f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
23140 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54  Token *pName1, T
23141 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20  oken *pName2){. 
23142 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
23143 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23144 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
23145 63 65 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78  ce to be reindex
23146 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  ed, or NULL */. 
23147 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
23148 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23149 4e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20  Name of a table 
2314a 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f  or index */.  co
2314b 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
2314c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
2314d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
2314e 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  e */.  Table *pT
2314f 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ab;             
23150 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e     /* A table in
23151 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
23152 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
23153 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
23154 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73 6f 63  * An index assoc
23155 69 61 74 65 64 20 77 69 74 68 20 70 54 61 62 20  iated with pTab 
23156 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
23157 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23158 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
23159 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f   index number */
2315a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2315b 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f   pParse->db;   /
2315c 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
2315d 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54  onnection */.  T
2315e 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20  oken *pObjName; 
2315f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
23160 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
23161 6f 72 20 69 6e 64 65 78 20 74 6f 20 62 65 20 72  or index to be r
23162 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f  eindexed */..  /
23163 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61 62  * Read the datab
23164 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61  ase schema. If a
23165 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
23166 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
23167 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20  essage.  ** and 
23168 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20 61  code in pParse a
23169 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  nd return NULL. 
2316a 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  */.  if( SQLITE_
2316b 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53  OK!=sqlite3ReadS
2316c 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
2316d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
2316e 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d  ..  if( pName1==
2316f 30 20 7c 7c 20 70 4e 61 6d 65 31 2d 3e 7a 3d 3d  0 || pName1->z==
23170 30 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65 78  0 ){.    reindex
23171 44 61 74 61 62 61 73 65 73 28 70 50 61 72 73 65  Databases(pParse
23172 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , 0);.    return
23173 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e  ;.  }else if( pN
23174 61 6d 65 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65  ame2==0 || pName
23175 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 63  2->z==0 ){.    c
23176 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20  har *zColl;.    
23177 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31 2d 3e  assert( pName1->
23178 7a 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d  z );.    zColl =
23179 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
2317a 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62  Token(pParse->db
2317b 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69  , pName1);.    i
2317c 66 28 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75  f( !zColl ) retu
2317d 72 6e 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20  rn;.    pColl = 
2317e 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
2317f 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20  eq(db, ENC(db), 
23180 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30 29 3b 0a 20  zColl, -1, 0);. 
23181 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
23182 20 20 20 20 20 20 69 66 28 20 7a 43 6f 6c 6c 20        if( zColl 
23183 29 7b 0a 20 20 20 20 20 20 20 20 72 65 69 6e 64  ){.        reind
23184 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61 72  exDatabases(pPar
23185 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20  se, zColl);.    
23186 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
23187 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  e(db, zColl);.  
23188 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
23189 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  rn;.    }.    sq
2318a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2318b 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 44  zColl);.  }.  iD
2318c 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
2318d 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
2318e 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26  Name1, pName2, &
2318f 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  pObjName);.  if(
23190 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b   iDb<0 ) return;
23191 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61  .  z = sqlite3Na
23192 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
23193 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  pObjName);.  if(
23194 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   z==0 ) return;.
23195 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b    zDb = db->aDb[
23196 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70 54  iDb].zName;.  pT
23197 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
23198 54 61 62 6c 65 28 64 62 2c 20 7a 2c 20 7a 44 62  Table(db, z, zDb
23199 29 3b 0a 20 20 69 66 28 20 70 54 61 62 20 29 7b  );.  if( pTab ){
2319a 0a 20 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c  .    reindexTabl
2319b 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
2319c 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  0);.    sqlite3D
2319d 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20  bFree(db, z);.  
2319e 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
2319f 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
231a0 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 2c  FindIndex(db, z,
231a1 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33   zDb);.  sqlite3
231a2 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
231a3 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20   if( pIndex ){. 
231a4 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
231a5 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
231a6 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20  arse, 0, iDb);. 
231a7 20 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c     sqlite3Refill
231a8 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49  Index(pParse, pI
231a9 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 72  ndex, -1);.    r
231aa 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c  eturn;.  }.  sql
231ab 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
231ac 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20  rse, "unable to 
231ad 69 64 65 6e 74 69 66 79 20 74 68 65 20 6f 62 6a  identify the obj
231ae 65 63 74 20 74 6f 20 62 65 20 72 65 69 6e 64 65  ect to be reinde
231af 78 65 64 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  xed");.}.#endif.
231b0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
231b1 64 79 6e 61 6d 69 63 6c 79 20 61 6c 6c 6f 63 61  dynamicly alloca
231b2 74 65 64 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  ted KeyInfo stru
231b3 63 74 75 72 65 20 74 68 61 74 20 63 61 6e 20 62  cture that can b
231b4 65 20 75 73 65 64 0a 2a 2a 20 77 69 74 68 20 4f  e used.** with O
231b5 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f 50  P_OpenRead or OP
231b6 5f 4f 70 65 6e 57 72 69 74 65 20 74 6f 20 61 63  _OpenWrite to ac
231b7 63 65 73 73 20 64 61 74 61 62 61 73 65 20 69 6e  cess database in
231b8 64 65 78 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20  dex pIdx..**.** 
231b9 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61  If successful, a
231ba 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
231bb 6e 65 77 20 73 74 72 75 63 74 75 72 65 20 69 73  new structure is
231bc 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68   returned. In th
231bd 69 73 20 63 61 73 65 0a 2a 2a 20 74 68 65 20 63  is case.** the c
231be 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73  aller is respons
231bf 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67  ible for calling
231c0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
231c1 62 2c 20 29 20 6f 6e 20 74 68 65 20 72 65 74 75  b, ) on the retu
231c2 72 6e 65 64 20 0a 2a 2a 20 70 6f 69 6e 74 65 72  rned .** pointer
231c3 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
231c4 63 75 72 73 20 28 6f 75 74 20 6f 66 20 6d 65 6d  curs (out of mem
231c5 6f 72 79 20 6f 72 20 6d 69 73 73 69 6e 67 20 63  ory or missing c
231c6 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 73 65 71  ollation .** seq
231c7 75 65 6e 63 65 29 2c 20 4e 55 4c 4c 20 69 73 20  uence), NULL is 
231c8 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
231c9 20 73 74 61 74 65 20 6f 66 20 70 50 61 72 73 65   state of pParse
231ca 20 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 6c   updated to refl
231cb 65 63 74 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  ect.** the error
231cc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
231cd 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c  ATE KeyInfo *sql
231ce 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
231cf 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
231d0 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20  Index *pIdx){.  
231d1 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f  int i;.  int nCo
231d2 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  l = pIdx->nColum
231d3 6e 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 20  n;.  int nBytes 
231d4 3d 20 73 69 7a 65 6f 66 28 4b 65 79 49 6e 66 6f  = sizeof(KeyInfo
231d5 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a  ) + (nCol-1)*siz
231d6 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20  eof(CollSeq*) + 
231d7 6e 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20  nCol;.  sqlite3 
231d8 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
231d9 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  ;.  KeyInfo *pKe
231da 79 20 3d 20 28 4b 65 79 49 6e 66 6f 20 2a 29 73  y = (KeyInfo *)s
231db 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
231dc 72 6f 28 64 62 2c 20 6e 42 79 74 65 73 29 3b 0a  ro(db, nBytes);.
231dd 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20  .  if( pKey ){. 
231de 20 20 20 70 4b 65 79 2d 3e 64 62 20 3d 20 70 50     pKey->db = pP
231df 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 4b  arse->db;.    pK
231e0 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d  ey->aSortOrder =
231e1 20 28 75 38 20 2a 29 26 28 70 4b 65 79 2d 3e 61   (u8 *)&(pKey->a
231e2 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 20  Coll[nCol]);.   
231e3 20 61 73 73 65 72 74 28 20 26 70 4b 65 79 2d 3e   assert( &pKey->
231e4 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d  aSortOrder[nCol]
231e5 3d 3d 26 28 28 28 75 38 20 2a 29 70 4b 65 79 29  ==&(((u8 *)pKey)
231e6 5b 6e 42 79 74 65 73 5d 29 20 29 3b 0a 20 20 20  [nBytes]) );.   
231e7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
231e8 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68  ; i++){.      ch
231e9 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70 49 64 78  ar *zColl = pIdx
231ea 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20  ->azColl[i];.   
231eb 20 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c 6c     assert( zColl
231ec 20 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e   );.      pKey->
231ed 61 43 6f 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69 74  aColl[i] = sqlit
231ee 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
231ef 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d  pParse, zColl, -
231f0 31 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e  1);.      pKey->
231f1 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
231f2 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pIdx->aSortOrder
231f3 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  [i];.    }.    p
231f4 4b 65 79 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75  Key->nField = (u
231f5 31 36 29 6e 43 6f 6c 3b 0a 20 20 7d 0a 0a 20 20  16)nCol;.  }..  
231f6 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
231f7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
231f8 62 46 72 65 65 28 64 62 2c 20 70 4b 65 79 29 3b  bFree(db, pKey);
231f9 0a 20 20 20 20 70 4b 65 79 20 3d 20 30 3b 0a 20  .    pKey = 0;. 
231fa 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4b 65 79   }.  return pKey
231fb 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
231fc 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 75 69 6c  **** End of buil
231fd 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  d.c ************
231fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
231ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23200 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
23201 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
23202 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a  callback.c *****
23203 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23204 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23205 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20  ***/./*.** 2005 
23206 4d 61 79 20 32 33 20 0a 2a 2a 0a 2a 2a 20 54 68  May 23 .**.** Th
23207 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
23208 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
23209 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2320a 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2320b 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2320c 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2320d 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2320e 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2320f 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
23210 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
23211 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
23212 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
23213 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
23214 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
23215 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
23216 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
23217 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
23218 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23219 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2321a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2321b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2321c 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
2321d 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2321e 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  functions used t
2321f 6f 20 61 63 63 65 73 73 20 74 68 65 20 69 6e 74  o access the int
23220 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 6c 65  ernal hash table
23221 73 0a 2a 2a 20 6f 66 20 75 73 65 72 20 64 65 66  s.** of user def
23222 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 61  ined functions a
23223 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  nd collation seq
23224 75 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49  uences..**.** $I
23225 64 3a 20 63 61 6c 6c 62 61 63 6b 2e 63 2c 76 20  d: callback.c,v 
23226 31 2e 33 39 20 32 30 30 39 2f 30 35 2f 30 33 20  1.39 2009/05/03 
23227 32 30 3a 32 33 3a 35 33 20 64 72 68 20 45 78 70  20:23:53 drh Exp
23228 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e   $.*/.../*.** In
23229 76 6f 6b 65 20 74 68 65 20 27 63 6f 6c 6c 61 74  voke the 'collat
2322a 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c 6c  ion needed' call
2322b 62 61 63 6b 20 74 6f 20 72 65 71 75 65 73 74 20  back to request 
2322c 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
2322d 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64  ence.** in the d
2322e 61 74 61 62 61 73 65 20 74 65 78 74 20 65 6e 63  atabase text enc
2322f 6f 64 69 6e 67 20 6f 66 20 6e 61 6d 65 20 7a 4e  oding of name zN
23230 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d  ame, length nNam
23231 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6c  e..** If the col
23232 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a  lation sequence.
23233 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
23234 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64 28 73 71  allCollNeeded(sq
23235 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
23236 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
23237 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 61 73 73 65  t nName){.  asse
23238 72 74 28 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65  rt( !db->xCollNe
23239 65 64 65 64 20 7c 7c 20 21 64 62 2d 3e 78 43 6f  eded || !db->xCo
2323a 6c 6c 4e 65 65 64 65 64 31 36 20 29 3b 0a 20 20  llNeeded16 );.  
2323b 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29 20 6e 4e  if( nName<0 ) nN
2323c 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
2323d 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20  len30(zName);.  
2323e 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  if( db->xCollNee
2323f 64 65 64 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ded ){.    char 
23240 2a 7a 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c  *zExternal = sql
23241 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62  ite3DbStrNDup(db
23242 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
23243 0a 20 20 20 20 69 66 28 20 21 7a 45 78 74 65 72  .    if( !zExter
23244 6e 61 6c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  nal ) return;.  
23245 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65    db->xCollNeede
23246 64 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65  d(db->pCollNeede
23247 64 41 72 67 2c 20 64 62 2c 20 28 69 6e 74 29 45  dArg, db, (int)E
23248 4e 43 28 64 62 29 2c 20 7a 45 78 74 65 72 6e 61  NC(db), zExterna
23249 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  l);.    sqlite3D
2324a 62 46 72 65 65 28 64 62 2c 20 7a 45 78 74 65 72  bFree(db, zExter
2324b 6e 61 6c 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  nal);.  }.#ifnde
2324c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
2324d 46 31 36 0a 20 20 69 66 28 20 64 62 2d 3e 78 43  F16.  if( db->xC
2324e 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29 7b 0a 20  ollNeeded16 ){. 
2324f 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a     char const *z
23250 45 78 74 65 72 6e 61 6c 3b 0a 20 20 20 20 73 71  External;.    sq
23251 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 54 6d  lite3_value *pTm
23252 70 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  p = sqlite3Value
23253 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 73 71 6c  New(db);.    sql
23254 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
23255 70 54 6d 70 2c 20 6e 4e 61 6d 65 2c 20 7a 4e 61  pTmp, nName, zNa
23256 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  me, SQLITE_UTF8,
23257 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
23258 0a 20 20 20 20 7a 45 78 74 65 72 6e 61 6c 20 3d  .    zExternal =
23259 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78   sqlite3ValueTex
2325a 74 28 70 54 6d 70 2c 20 53 51 4c 49 54 45 5f 55  t(pTmp, SQLITE_U
2325b 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 20 20 20  TF16NATIVE);.   
2325c 20 69 66 28 20 7a 45 78 74 65 72 6e 61 6c 20 29   if( zExternal )
2325d 7b 0a 20 20 20 20 20 20 64 62 2d 3e 78 43 6f 6c  {.      db->xCol
2325e 6c 4e 65 65 64 65 64 31 36 28 64 62 2d 3e 70 43  lNeeded16(db->pC
2325f 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 64 62  ollNeededArg, db
23260 2c 20 28 69 6e 74 29 45 4e 43 28 64 62 29 2c 20  , (int)ENC(db), 
23261 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20  zExternal);.    
23262 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  }.    sqlite3Val
23263 75 65 46 72 65 65 28 70 54 6d 70 29 3b 0a 20 20  ueFree(pTmp);.  
23264 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
23265 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
23266 73 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20  s called if the 
23267 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72  collation factor
23268 79 20 66 61 69 6c 73 20 74 6f 20 64 65 6c 69 76  y fails to deliv
23269 65 72 20 61 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f  er a.** collatio
2326a 6e 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74 68  n function in th
2326b 65 20 62 65 73 74 20 65 6e 63 6f 64 69 6e 67 20  e best encoding 
2326c 62 75 74 20 74 68 65 72 65 20 6d 61 79 20 62 65  but there may be
2326d 20 6f 74 68 65 72 20 76 65 72 73 69 6f 6e 73 0a   other versions.
2326e 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6c 6c 61  ** of this colla
2326f 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 28 66  tion function (f
23270 6f 72 20 6f 74 68 65 72 20 74 65 78 74 20 65 6e  or other text en
23271 63 6f 64 69 6e 67 73 29 20 61 76 61 69 6c 61 62  codings) availab
23272 6c 65 2e 20 55 73 65 20 6f 6e 65 0a 2a 2a 20 6f  le. Use one.** o
23273 66 20 74 68 65 73 65 20 69 6e 73 74 65 61 64 20  f these instead 
23274 69 66 20 74 68 65 79 20 65 78 69 73 74 2e 20 41  if they exist. A
23275 76 6f 69 64 20 61 20 55 54 46 2d 38 20 3c 2d 3e  void a UTF-8 <->
23276 20 55 54 46 2d 31 36 20 63 6f 6e 76 65 72 73 69   UTF-16 conversi
23277 6f 6e 20 69 66 0a 2a 2a 20 70 6f 73 73 69 62 6c  on if.** possibl
23278 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
23279 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 73 71   synthCollSeq(sq
2327a 6c 69 74 65 33 20 2a 64 62 2c 20 43 6f 6c 6c 53  lite3 *db, CollS
2327b 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 43 6f  eq *pColl){.  Co
2327c 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 32 3b 0a 20  llSeq *pColl2;. 
2327d 20 63 68 61 72 20 2a 7a 20 3d 20 70 43 6f 6c 6c   char *z = pColl
2327e 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e  ->zName;.  int n
2327f 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
23280 33 30 28 7a 29 3b 0a 20 20 69 6e 74 20 69 3b 0a  30(z);.  int i;.
23281 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
23282 38 20 61 45 6e 63 5b 5d 20 3d 20 7b 20 53 51 4c  8 aEnc[] = { SQL
23283 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c  ITE_UTF16BE, SQL
23284 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 53 51 4c  ITE_UTF16LE, SQL
23285 49 54 45 5f 55 54 46 38 20 7d 3b 0a 20 20 66 6f  ITE_UTF8 };.  fo
23286 72 28 69 3d 30 3b 20 69 3c 33 3b 20 69 2b 2b 29  r(i=0; i<3; i++)
23287 7b 0a 20 20 20 20 70 43 6f 6c 6c 32 20 3d 20 73  {.    pColl2 = s
23288 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
23289 71 28 64 62 2c 20 61 45 6e 63 5b 69 5d 2c 20 7a  q(db, aEnc[i], z
2328a 2c 20 6e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , n, 0);.    if(
2328b 20 70 43 6f 6c 6c 32 2d 3e 78 43 6d 70 21 3d 30   pColl2->xCmp!=0
2328c 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
2328d 28 70 43 6f 6c 6c 2c 20 70 43 6f 6c 6c 32 2c 20  (pColl, pColl2, 
2328e 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 29 29  sizeof(CollSeq))
2328f 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 2d 3e 78  ;.      pColl->x
23290 44 65 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Del = 0;        
23291 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 70 79 20   /* Do not copy 
23292 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 2a  the destructor *
23293 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  /.      return S
23294 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
23295 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
23296 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a  ITE_ERROR;.}../*
23297 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
23298 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  n is responsible
23299 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68   for invoking th
2329a 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74  e collation fact
2329b 6f 72 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20  ory callback.** 
2329c 6f 72 20 73 75 62 73 74 69 74 75 74 69 6e 67 20  or substituting 
2329d 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
2329e 65 6e 63 65 20 6f 66 20 61 20 64 69 66 66 65 72  ence of a differ
2329f 65 6e 74 20 65 6e 63 6f 64 69 6e 67 20 77 68 65  ent encoding whe
232a0 6e 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74  n the.** request
232a1 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
232a2 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61  uence is not ava
232a3 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61  ilable in the da
232a4 74 61 62 61 73 65 20 6e 61 74 69 76 65 0a 2a 2a  tabase native.**
232a5 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20 0a 2a   encoding..** .*
232a6 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e  * If it is not N
232a7 55 4c 4c 2c 20 74 68 65 6e 20 70 43 6f 6c 6c 20  ULL, then pColl 
232a8 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 74 68  must point to th
232a9 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76  e database nativ
232aa 65 20 65 6e 63 6f 64 69 6e 67 20 0a 2a 2a 20 63  e encoding .** c
232ab 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
232ac 65 20 77 69 74 68 20 6e 61 6d 65 20 7a 4e 61 6d  e with name zNam
232ad 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e  e, length nName.
232ae 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
232af 6e 20 76 61 6c 75 65 20 69 73 20 65 69 74 68 65  n value is eithe
232b0 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  r the collation 
232b1 73 65 71 75 65 6e 63 65 20 74 6f 20 62 65 20 75  sequence to be u
232b2 73 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 0a  sed in database.
232b3 2a 2a 20 64 62 20 66 6f 72 20 63 6f 6c 6c 61 74  ** db for collat
232b4 69 6f 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a 4e  ion type name zN
232b5 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d  ame, length nNam
232b6 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20 69 66 20 6e  e, or NULL, if n
232b7 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73  o collation.** s
232b8 65 71 75 65 6e 63 65 20 63 61 6e 20 62 65 20 66  equence can be f
232b9 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ound..*/.SQLITE_
232ba 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
232bb 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53  *sqlite3GetCollS
232bc 65 71 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64  eq(.  sqlite3* d
232bd 62 2c 20 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  b, .  CollSeq *p
232be 43 6f 6c 6c 2c 20 0a 20 20 63 6f 6e 73 74 20 63  Coll, .  const c
232bf 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69  har *zName, .  i
232c0 6e 74 20 6e 4e 61 6d 65 0a 29 7b 0a 20 20 43 6f  nt nName.){.  Co
232c1 6c 6c 53 65 71 20 2a 70 3b 0a 0a 20 20 70 20 3d  llSeq *p;..  p =
232c2 20 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 21 70   pColl;.  if( !p
232c3 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69   ){.    p = sqli
232c4 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
232c5 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 4e 61 6d  b, ENC(db), zNam
232c6 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  e, nName, 0);.  
232c7 7d 0a 20 20 69 66 28 20 21 70 20 7c 7c 20 21 70  }.  if( !p || !p
232c8 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 2f 2a  ->xCmp ){.    /*
232c9 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65   No collation se
232ca 71 75 65 6e 63 65 20 6f 66 20 74 68 69 73 20 74  quence of this t
232cb 79 70 65 20 66 6f 72 20 74 68 69 73 20 65 6e 63  ype for this enc
232cc 6f 64 69 6e 67 20 69 73 20 72 65 67 69 73 74 65  oding is registe
232cd 72 65 64 2e 0a 20 20 20 20 2a 2a 20 43 61 6c 6c  red..    ** Call
232ce 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
232cf 61 63 74 6f 72 79 20 74 6f 20 73 65 65 20 69 66  actory to see if
232d0 20 69 74 20 63 61 6e 20 73 75 70 70 6c 79 20 75   it can supply u
232d1 73 20 77 69 74 68 20 6f 6e 65 2e 0a 20 20 20 20  s with one..    
232d2 2a 2f 0a 20 20 20 20 63 61 6c 6c 43 6f 6c 6c 4e  */.    callCollN
232d3 65 65 64 65 64 28 64 62 2c 20 7a 4e 61 6d 65 2c  eeded(db, zName,
232d4 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 70 20 3d   nName);.    p =
232d5 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
232d6 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c  Seq(db, ENC(db),
232d7 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30   zName, nName, 0
232d8 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 26  );.  }.  if( p &
232d9 26 20 21 70 2d 3e 78 43 6d 70 20 26 26 20 73 79  & !p->xCmp && sy
232da 6e 74 68 43 6f 6c 6c 53 65 71 28 64 62 2c 20 70  nthCollSeq(db, p
232db 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 30 3b 0a  ) ){.    p = 0;.
232dc 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70    }.  assert( !p
232dd 20 7c 7c 20 70 2d 3e 78 43 6d 70 20 29 3b 0a 20   || p->xCmp );. 
232de 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
232df 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
232e0 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20   is called on a 
232e1 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
232e2 63 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 20  ce before it is 
232e3 75 73 65 64 20 74 6f 0a 2a 2a 20 63 68 65 63 6b  used to.** check
232e4 20 74 68 61 74 20 69 74 20 69 73 20 64 65 66 69   that it is defi
232e5 6e 65 64 2e 20 41 6e 20 75 6e 64 65 66 69 6e 65  ned. An undefine
232e6 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  d collation sequ
232e7 65 6e 63 65 20 65 78 69 73 74 73 20 77 68 65 6e  ence exists when
232e8 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 69  .** a database i
232e9 73 20 6c 6f 61 64 65 64 20 74 68 61 74 20 63 6f  s loaded that co
232ea 6e 74 61 69 6e 73 20 72 65 66 65 72 65 6e 63 65  ntains reference
232eb 73 20 74 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  s to collation s
232ec 65 71 75 65 6e 63 65 73 0a 2a 2a 20 74 68 61 74  equences.** that
232ed 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 64   have not been d
232ee 65 66 69 6e 65 64 20 62 79 20 73 71 6c 69 74 65  efined by sqlite
232ef 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
232f0 6f 6e 28 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20  on() etc..**.** 
232f1 49 66 20 72 65 71 75 69 72 65 64 2c 20 74 68 69  If required, thi
232f2 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20  s routine calls 
232f3 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e  the 'collation n
232f4 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63 6b 20  eeded' callback 
232f5 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20 61 20  to.** request a 
232f6 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68  definition of th
232f7 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
232f8 65 6e 63 65 2e 20 49 66 20 74 68 69 73 20 64 6f  ence. If this do
232f9 65 73 6e 27 74 20 77 6f 72 6b 2c 20 0a 2a 2a 20  esn't work, .** 
232fa 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 63 6f  an equivalent co
232fb 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
232fc 20 74 68 61 74 20 75 73 65 73 20 61 20 74 65 78   that uses a tex
232fd 74 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65  t encoding diffe
232fe 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  rent.** from the
232ff 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69   main database i
23300 73 20 73 75 62 73 74 69 74 75 74 65 64 2c 20 69  s substituted, i
23301 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c 61 62  f one is availab
23302 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
23303 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
23304 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61  3CheckCollSeq(Pa
23305 72 73 65 20 2a 70 50 61 72 73 65 2c 20 43 6f 6c  rse *pParse, Col
23306 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20  lSeq *pColl){.  
23307 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
23308 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
23309 6d 65 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  me = pColl->zNam
2330a 65 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  e;.    CollSeq *
2330b 70 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43 6f  p = sqlite3GetCo
2330c 6c 6c 53 65 71 28 70 50 61 72 73 65 2d 3e 64 62  llSeq(pParse->db
2330d 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c 20  , pColl, zName, 
2330e 2d 31 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20  -1);.    if( !p 
2330f 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  ){.      if( pPa
23310 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a  rse->nErr==0 ){.
23311 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
23312 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
23313 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74 69  "no such collati
23314 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25 73 22  on sequence: %s"
23315 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
23316 7d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  }.      pParse->
23317 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65  nErr++;.      re
23318 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
23319 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  R;.    }.    ass
2331a 65 72 74 28 20 70 3d 3d 70 43 6f 6c 6c 20 29 3b  ert( p==pColl );
2331b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
2331c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a  LITE_OK;.}..../*
2331d 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 6e 64 20 72  .** Locate and r
2331e 65 74 75 72 6e 20 61 6e 20 65 6e 74 72 79 20 66  eturn an entry f
2331f 72 6f 6d 20 74 68 65 20 64 62 2e 61 43 6f 6c 6c  rom the db.aColl
23320 53 65 71 20 68 61 73 68 20 74 61 62 6c 65 2e 20  Seq hash table. 
23321 49 66 20 74 68 65 20 65 6e 74 72 79 0a 2a 2a 20  If the entry.** 
23322 73 70 65 63 69 66 69 65 64 20 62 79 20 7a 4e 61  specified by zNa
23323 6d 65 20 61 6e 64 20 6e 4e 61 6d 65 20 69 73 20  me and nName is 
23324 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 70 61  not found and pa
23325 72 61 6d 65 74 65 72 20 27 63 72 65 61 74 65 27  rameter 'create'
23326 20 69 73 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65   is.** true, the
23327 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 65  n create a new e
23328 6e 74 72 79 2e 20 4f 74 68 65 72 77 69 73 65 20  ntry. Otherwise 
23329 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a  return NULL..**.
2332a 2a 2a 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20  ** Each pointer 
2332b 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71  stored in the sq
2332c 6c 69 74 65 33 2e 61 43 6f 6c 6c 53 65 71 20 68  lite3.aCollSeq h
2332d 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  ash table contai
2332e 6e 73 20 61 6e 0a 2a 2a 20 61 72 72 61 79 20 6f  ns an.** array o
2332f 66 20 74 68 72 65 65 20 43 6f 6c 6c 53 65 71 20  f three CollSeq 
23330 73 74 72 75 63 74 75 72 65 73 2e 20 54 68 65 20  structures. The 
23331 66 69 72 73 74 20 69 73 20 74 68 65 20 63 6f 6c  first is the col
23332 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a  lation sequence.
23333 2a 2a 20 70 72 65 66 66 65 72 72 65 64 20 66 6f  ** prefferred fo
23334 72 20 55 54 46 2d 38 2c 20 74 68 65 20 73 65 63  r UTF-8, the sec
23335 6f 6e 64 20 55 54 46 2d 31 36 6c 65 2c 20 61 6e  ond UTF-16le, an
23336 64 20 74 68 65 20 74 68 69 72 64 20 55 54 46 2d  d the third UTF-
23337 31 36 62 65 2e 0a 2a 2a 0a 2a 2a 20 53 74 6f 72  16be..**.** Stor
23338 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61  ed immediately a
23339 66 74 65 72 20 74 68 65 20 74 68 72 65 65 20 63  fter the three c
2333a 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2333b 65 73 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a  es is a copy of.
2333c 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ** the collation
2333d 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 2e 20   sequence name. 
2333e 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69  A pointer to thi
2333f 73 20 73 74 72 69 6e 67 20 69 73 20 73 74 6f 72  s string is stor
23340 65 64 20 69 6e 0a 2a 2a 20 65 61 63 68 20 63 6f  ed in.** each co
23341 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
23342 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
23343 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 66  tatic CollSeq *f
23344 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28  indCollSeqEntry(
23345 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
23346 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
23347 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  ame,.  int nName
23348 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a 29  ,.  int create.)
23349 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
2334a 6c 6c 3b 0a 20 20 69 66 28 20 6e 4e 61 6d 65 3c  ll;.  if( nName<
2334b 30 20 29 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  0 ) nName = sqli
2334c 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
2334d 65 29 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71  e);.  pColl = sq
2334e 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 64  lite3HashFind(&d
2334f 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 7a 4e 61  b->aCollSeq, zNa
23350 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 0a 20 20 69  me, nName);..  i
23351 66 28 20 30 3d 3d 70 43 6f 6c 6c 20 26 26 20 63  f( 0==pColl && c
23352 72 65 61 74 65 20 29 7b 0a 20 20 20 20 70 43 6f  reate ){.    pCo
23353 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ll = sqlite3DbMa
23354 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 33 2a 73  llocZero(db, 3*s
23355 69 7a 65 6f 66 28 2a 70 43 6f 6c 6c 29 20 2b 20  izeof(*pColl) + 
23356 6e 4e 61 6d 65 20 2b 20 31 20 29 3b 0a 20 20 20  nName + 1 );.   
23357 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
23358 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 65      CollSeq *pDe
23359 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43 6f  l = 0;.      pCo
2335a 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63  ll[0].zName = (c
2335b 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a  har*)&pColl[3];.
2335c 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 65        pColl[0].e
2335d 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
2335e 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31 5d  ;.      pColl[1]
2335f 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29  .zName = (char*)
23360 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20  &pColl[3];.     
23361 20 70 43 6f 6c 6c 5b 31 5d 2e 65 6e 63 20 3d 20   pColl[1].enc = 
23362 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b 0a  SQLITE_UTF16LE;.
23363 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e 7a        pColl[2].z
23364 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70  Name = (char*)&p
23365 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70  Coll[3];.      p
23366 43 6f 6c 6c 5b 32 5d 2e 65 6e 63 20 3d 20 53 51  Coll[2].enc = SQ
23367 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20  LITE_UTF16BE;.  
23368 20 20 20 20 6d 65 6d 63 70 79 28 70 43 6f 6c 6c      memcpy(pColl
23369 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  [0].zName, zName
2336a 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , nName);.      
2336b 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 5b 6e  pColl[0].zName[n
2336c 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 20  Name] = 0;.     
2336d 20 70 44 65 6c 20 3d 20 73 71 6c 69 74 65 33 48   pDel = sqlite3H
2336e 61 73 68 49 6e 73 65 72 74 28 26 64 62 2d 3e 61  ashInsert(&db->a
2336f 43 6f 6c 6c 53 65 71 2c 20 70 43 6f 6c 6c 5b 30  CollSeq, pColl[0
23370 5d 2e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20  ].zName, nName, 
23371 70 43 6f 6c 6c 29 3b 0a 0a 20 20 20 20 20 20 2f  pColl);..      /
23372 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  * If a malloc() 
23373 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64  failure occurred
23374 20 69 6e 20 73 71 6c 69 74 65 33 48 61 73 68 49   in sqlite3HashI
23375 6e 73 65 72 74 28 29 2c 20 69 74 20 77 69 6c 6c  nsert(), it will
23376 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 74 75 72   .      ** retur
23377 6e 20 74 68 65 20 70 43 6f 6c 6c 20 70 6f 69 6e  n the pColl poin
23378 74 65 72 20 74 6f 20 62 65 20 64 65 6c 65 74 65  ter to be delete
23379 64 20 28 62 65 63 61 75 73 65 20 69 74 20 77 61  d (because it wa
2337a 73 6e 27 74 20 61 64 64 65 64 0a 20 20 20 20 20  sn't added.     
2337b 20 2a 2a 20 74 6f 20 74 68 65 20 68 61 73 68 20   ** to the hash 
2337c 74 61 62 6c 65 29 2e 0a 20 20 20 20 20 20 2a 2f  table)..      */
2337d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2337e 44 65 6c 3d 3d 30 20 7c 7c 20 70 44 65 6c 3d 3d  Del==0 || pDel==
2337f 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 69  pColl );.      i
23380 66 28 20 70 44 65 6c 21 3d 30 20 29 7b 0a 20 20  f( pDel!=0 ){.  
23381 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
23382 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
23383 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
23384 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20  e(db, pDel);.   
23385 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a       pColl = 0;.
23386 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
23387 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c  }.  return pColl
23388 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d  ;.}../*.** Param
23389 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f 69 6e 74  eter zName point
2338a 73 20 74 6f 20 61 20 55 54 46 2d 38 20 65 6e 63  s to a UTF-8 enc
2338b 6f 64 65 64 20 73 74 72 69 6e 67 20 6e 4e 61 6d  oded string nNam
2338c 65 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a  e bytes long..**
2338d 20 52 65 74 75 72 6e 20 74 68 65 20 43 6f 6c 6c   Return the Coll
2338e 53 65 71 2a 20 70 6f 69 6e 74 65 72 20 66 6f 72  Seq* pointer for
2338f 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
23390 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 7a 4e  equence named zN
23391 61 6d 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65  ame.** for the e
23392 6e 63 6f 64 69 6e 67 20 27 65 6e 63 27 20 66 72  ncoding 'enc' fr
23393 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
23394 27 64 62 27 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  'db'..**.** If t
23395 68 65 20 65 6e 74 72 79 20 73 70 65 63 69 66 69  he entry specifi
23396 65 64 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20  ed is not found 
23397 61 6e 64 20 27 63 72 65 61 74 65 27 20 69 73 20  and 'create' is 
23398 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65 61 74  true, then creat
23399 65 20 61 0a 2a 2a 20 6e 65 77 20 65 6e 74 72 79  e a.** new entry
2339a 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74  .  Otherwise ret
2339b 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20  urn NULL..**.** 
2339c 41 20 73 65 70 61 72 61 74 65 20 66 75 6e 63 74  A separate funct
2339d 69 6f 6e 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  ion sqlite3Locat
2339e 65 43 6f 6c 6c 53 65 71 28 29 20 69 73 20 61 20  eCollSeq() is a 
2339f 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a  wrapper around.*
233a0 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  * this routine. 
233a1 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f   sqlite3LocateCo
233a2 6c 6c 53 65 71 28 29 20 69 6e 76 6f 6b 65 73 20  llSeq() invokes 
233a3 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
233a4 63 74 6f 72 79 0a 2a 2a 20 69 66 20 6e 65 63 65  ctory.** if nece
233a5 73 73 61 72 79 20 61 6e 64 20 67 65 6e 65 72 61  ssary and genera
233a6 74 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  tes an error mes
233a7 73 61 67 65 20 69 66 20 74 68 65 20 63 6f 6c 6c  sage if the coll
233a8 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 0a 2a  ating sequence.*
233a9 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  * cannot be foun
233aa 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
233ab 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
233ac 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
233ad 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
233ae 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20 63 6f 6e  .  u8 enc,.  con
233af 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a  st char *zName,.
233b0 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 0a 20 20 69    int nName,.  i
233b1 6e 74 20 63 72 65 61 74 65 0a 29 7b 0a 20 20 43  nt create.){.  C
233b2 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
233b3 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20   if( zName ){.  
233b4 20 20 70 43 6f 6c 6c 20 3d 20 66 69 6e 64 43 6f    pColl = findCo
233b5 6c 6c 53 65 71 45 6e 74 72 79 28 64 62 2c 20 7a  llSeqEntry(db, z
233b6 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 63 72 65  Name, nName, cre
233b7 61 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ate);.  }else{. 
233b8 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70     pColl = db->p
233b9 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20  DfltColl;.  }.  
233ba 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f 55  assert( SQLITE_U
233bb 54 46 38 3d 3d 31 20 26 26 20 53 51 4c 49 54 45  TF8==1 && SQLITE
233bc 5f 55 54 46 31 36 4c 45 3d 3d 32 20 26 26 20 53  _UTF16LE==2 && S
233bd 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3d 3d 33  QLITE_UTF16BE==3
233be 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 6e   );.  assert( en
233bf 63 3e 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26  c>=SQLITE_UTF8 &
233c0 26 20 65 6e 63 3c 3d 53 51 4c 49 54 45 5f 55 54  & enc<=SQLITE_UT
233c1 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20 70  F16BE );.  if( p
233c2 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b 3d 20  Coll ) pColl += 
233c3 65 6e 63 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20  enc-1;.  return 
233c4 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20 44 75 72  pColl;.}../* Dur
233c5 69 6e 67 20 74 68 65 20 73 65 61 72 63 68 20 66  ing the search f
233c6 6f 72 20 74 68 65 20 62 65 73 74 20 66 75 6e 63  or the best func
233c7 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c  tion definition,
233c8 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 0a   this procedure.
233c9 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ** is called to 
233ca 74 65 73 74 20 68 6f 77 20 77 65 6c 6c 20 74 68  test how well th
233cb 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65  e function passe
233cc 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
233cd 72 67 75 6d 65 6e 74 0a 2a 2a 20 6d 61 74 63 68  rgument.** match
233ce 65 73 20 74 68 65 20 72 65 71 75 65 73 74 20 66  es the request f
233cf 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69  or a function wi
233d0 74 68 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74  th nArg argument
233d1 73 20 69 6e 20 61 20 73 79 73 74 65 6d 0a 2a 2a  s in a system.**
233d2 20 74 68 61 74 20 75 73 65 73 20 65 6e 63 6f 64   that uses encod
233d3 69 6e 67 20 65 6e 63 2e 20 54 68 65 20 76 61 6c  ing enc. The val
233d4 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69  ue returned indi
233d5 63 61 74 65 73 20 68 6f 77 20 77 65 6c 6c 20 74  cates how well t
233d6 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 73  he.** request is
233d7 20 6d 61 74 63 68 65 64 2e 20 41 20 68 69 67 68   matched. A high
233d8 65 72 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74  er value indicat
233d9 65 73 20 61 20 62 65 74 74 65 72 20 6d 61 74 63  es a better matc
233da 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  h..**.** The ret
233db 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 61  urned value is a
233dc 6c 77 61 79 73 20 62 65 74 77 65 65 6e 20 30 20  lways between 0 
233dd 61 6e 64 20 36 2c 20 61 73 20 66 6f 6c 6c 6f 77  and 6, as follow
233de 73 3a 0a 2a 2a 0a 2a 2a 20 30 3a 20 4e 6f 74 20  s:.**.** 0: Not 
233df 61 20 6d 61 74 63 68 2c 20 6f 72 20 69 66 20 6e  a match, or if n
233e0 41 72 67 3c 30 20 61 6e 64 20 74 68 65 20 66 75  Arg<0 and the fu
233e1 6e 63 74 69 6f 6e 20 69 73 20 68 61 73 20 6e 6f  nction is has no
233e2 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
233e3 0a 2a 2a 20 31 3a 20 41 20 76 61 72 69 61 62 6c  .** 1: A variabl
233e4 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63  e arguments func
233e5 74 69 6f 6e 20 74 68 61 74 20 70 72 65 66 65 72  tion that prefer
233e6 73 20 55 54 46 2d 38 20 77 68 65 6e 20 61 20 55  s UTF-8 when a U
233e7 54 46 2d 31 36 0a 2a 2a 20 20 20 20 65 6e 63 6f  TF-16.**    enco
233e8 64 69 6e 67 20 69 73 20 72 65 71 75 65 73 74 65  ding is requeste
233e9 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61  d, or vice versa
233ea 2e 0a 2a 2a 20 32 3a 20 41 20 76 61 72 69 61 62  ..** 2: A variab
233eb 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e  le arguments fun
233ec 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65 73 20  ction that uses 
233ed 55 54 46 2d 31 36 42 45 20 77 68 65 6e 20 55 54  UTF-16BE when UT
233ee 46 2d 31 36 4c 45 20 69 73 0a 2a 2a 20 20 20 20  F-16LE is.**    
233ef 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69  requested, or vi
233f0 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 33 3a 20  ce versa..** 3: 
233f1 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d  A variable argum
233f2 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 75 73  ents function us
233f3 69 6e 67 20 74 68 65 20 73 61 6d 65 20 74 65 78  ing the same tex
233f4 74 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20 34  t encoding..** 4
233f5 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  : A function wit
233f6 68 20 74 68 65 20 65 78 61 63 74 20 6e 75 6d 62  h the exact numb
233f7 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
233f8 72 65 71 75 65 73 74 65 64 20 74 68 61 74 0a 2a  requested that.*
233f9 2a 20 20 20 20 70 72 65 66 65 72 73 20 55 54 46  *    prefers UTF
233fa 2d 38 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36  -8 when a UTF-16
233fb 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 71   encoding is req
233fc 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20  uested, or vice 
233fd 76 65 72 73 61 2e 0a 2a 2a 20 35 3a 20 41 20 66  versa..** 5: A f
233fe 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
233ff 20 65 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66   exact number of
23400 20 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65   arguments reque
23401 73 74 65 64 20 74 68 61 74 0a 2a 2a 20 20 20 20  sted that.**    
23402 70 72 65 66 65 72 73 20 55 54 46 2d 31 36 4c 45  prefers UTF-16LE
23403 20 77 68 65 6e 20 55 54 46 2d 31 36 42 45 20 69   when UTF-16BE i
23404 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20  s requested, or 
23405 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 36  vice versa..** 6
23406 3a 20 41 6e 20 65 78 61 63 74 20 6d 61 74 63 68  : An exact match
23407 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ..**.*/.static i
23408 6e 74 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28  nt matchQuality(
23409 46 75 6e 63 44 65 66 20 2a 70 2c 20 69 6e 74 20  FuncDef *p, int 
2340a 6e 41 72 67 2c 20 75 38 20 65 6e 63 29 7b 0a 20  nArg, u8 enc){. 
2340b 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a   int match = 0;.
2340c 20 20 69 66 28 20 70 2d 3e 6e 41 72 67 3d 3d 2d    if( p->nArg==-
2340d 31 20 7c 7c 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41  1 || p->nArg==nA
2340e 72 67 20 0a 20 20 20 7c 7c 20 28 6e 41 72 67 3d  rg .   || (nArg=
2340f 3d 2d 31 20 26 26 20 28 70 2d 3e 78 46 75 6e 63  =-1 && (p->xFunc
23410 21 3d 30 20 7c 7c 20 70 2d 3e 78 53 74 65 70 21  !=0 || p->xStep!
23411 3d 30 29 29 0a 20 20 29 7b 0a 20 20 20 20 6d 61  =0)).  ){.    ma
23412 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 69 66 28  tch = 1;.    if(
23413 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c   p->nArg==nArg |
23414 7c 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20  | nArg==-1 ){.  
23415 20 20 20 20 6d 61 74 63 68 20 3d 20 34 3b 0a 20      match = 4;. 
23416 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63     }.    if( enc
23417 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20 29 7b  ==p->iPrefEnc ){
23418 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d 20  .      match += 
23419 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  2;.    }.    els
2341a 65 20 69 66 28 20 28 65 6e 63 3d 3d 53 51 4c 49  e if( (enc==SQLI
2341b 54 45 5f 55 54 46 31 36 4c 45 20 26 26 20 70 2d  TE_UTF16LE && p-
2341c 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49 54  >iPrefEnc==SQLIT
2341d 45 5f 55 54 46 31 36 42 45 29 20 7c 7c 0a 20 20  E_UTF16BE) ||.  
2341e 20 20 20 20 20 20 20 20 20 20 20 28 65 6e 63 3d             (enc=
2341f 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
23420 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d  && p->iPrefEnc==
23421 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 20  SQLITE_UTF16LE) 
23422 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b  ){.      match +
23423 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 1;.    }.  }. 
23424 20 72 65 74 75 72 6e 20 6d 61 74 63 68 3b 0a 7d   return match;.}
23425 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 61  ../*.** Search a
23426 20 46 75 6e 63 44 65 66 48 61 73 68 20 66 6f 72   FuncDefHash for
23427 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
23428 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e   the given name.
23429 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f    Return.** a po
2342a 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 61 74  inter to the mat
2342b 63 68 69 6e 67 20 46 75 6e 63 44 65 66 20 69 66  ching FuncDef if
2342c 20 66 6f 75 6e 64 2c 20 6f 72 20 30 20 69 66 20   found, or 0 if 
2342d 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63  there is no matc
2342e 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46 75 6e  h..*/.static Fun
2342f 63 44 65 66 20 2a 66 75 6e 63 74 69 6f 6e 53 65  cDef *functionSe
23430 61 72 63 68 28 0a 20 20 46 75 6e 63 44 65 66 48  arch(.  FuncDefH
23431 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f 2a 20  ash *pHash,  /* 
23432 48 61 73 68 20 74 61 62 6c 65 20 74 6f 20 73 65  Hash table to se
23433 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20 68 2c  arch */.  int h,
23434 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23435 2a 20 48 61 73 68 20 6f 66 20 74 68 65 20 6e 61  * Hash of the na
23436 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  me */.  const ch
23437 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 2f 2a 20  ar *zFunc,   /* 
23438 4e 61 6d 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e  Name of function
23439 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 20   */.  int nFunc 
2343a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2343b 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
2343c 20 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20 20 46   zFunc */.){.  F
2343d 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20 66 6f 72  uncDef *p;.  for
2343e 28 70 3d 70 48 61 73 68 2d 3e 61 5b 68 5d 3b 20  (p=pHash->a[h]; 
2343f 70 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29 7b 0a  p; p=p->pHash){.
23440 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
23441 74 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61 6d 65  trNICmp(p->zName
23442 2c 20 7a 46 75 6e 63 2c 20 6e 46 75 6e 63 29 3d  , zFunc, nFunc)=
23443 3d 30 20 26 26 20 70 2d 3e 7a 4e 61 6d 65 5b 6e  =0 && p->zName[n
23444 46 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  Func]==0 ){.    
23445 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20    return p;.    
23446 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
23447 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72  ;.}../*.** Inser
23448 74 20 61 20 6e 65 77 20 46 75 6e 63 44 65 66 20  t a new FuncDef 
23449 69 6e 74 6f 20 61 20 46 75 6e 63 44 65 66 48 61  into a FuncDefHa
2344a 73 68 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a  sh hash table..*
2344b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2344c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 75 6e   void sqlite3Fun
2344d 63 44 65 66 49 6e 73 65 72 74 28 0a 20 20 46 75  cDefInsert(.  Fu
2344e 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
2344f 2c 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 74  ,  /* The hash t
23450 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
23451 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 46  to insert */.  F
23452 75 6e 63 44 65 66 20 2a 70 44 65 66 20 20 20 20  uncDef *pDef    
23453 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74      /* The funct
23454 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 74  ion definition t
23455 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 29 7b 0a 20  o insert */.){. 
23456 20 46 75 6e 63 44 65 66 20 2a 70 4f 74 68 65 72   FuncDef *pOther
23457 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20  ;.  int nName = 
23458 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
23459 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pDef->zName);.  
2345a 75 38 20 63 31 20 3d 20 28 75 38 29 70 44 65 66  u8 c1 = (u8)pDef
2345b 2d 3e 7a 4e 61 6d 65 5b 30 5d 3b 0a 20 20 69 6e  ->zName[0];.  in
2345c 74 20 68 20 3d 20 28 73 71 6c 69 74 65 33 55 70  t h = (sqlite3Up
2345d 70 65 72 54 6f 4c 6f 77 65 72 5b 63 31 5d 20 2b  perToLower[c1] +
2345e 20 6e 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53   nName) % ArrayS
2345f 69 7a 65 28 70 48 61 73 68 2d 3e 61 29 3b 0a 20  ize(pHash->a);. 
23460 20 70 4f 74 68 65 72 20 3d 20 66 75 6e 63 74 69   pOther = functi
23461 6f 6e 53 65 61 72 63 68 28 70 48 61 73 68 2c 20  onSearch(pHash, 
23462 68 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20  h, pDef->zName, 
23463 6e 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4f  nName);.  if( pO
23464 74 68 65 72 20 29 7b 0a 20 20 20 20 70 44 65 66  ther ){.    pDef
23465 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65 72  ->pNext = pOther
23466 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70 4f 74  ->pNext;.    pOt
23467 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70 44 65  her->pNext = pDe
23468 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  f;.  }else{.    
23469 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  pDef->pNext = 0;
2346a 0a 20 20 20 20 70 44 65 66 2d 3e 70 48 61 73 68  .    pDef->pHash
2346b 20 3d 20 70 48 61 73 68 2d 3e 61 5b 68 5d 3b 0a   = pHash->a[h];.
2346c 20 20 20 20 70 48 61 73 68 2d 3e 61 5b 68 5d 20      pHash->a[h] 
2346d 3d 20 70 44 65 66 3b 0a 20 20 7d 0a 7d 0a 20 20  = pDef;.  }.}.  
2346e 0a 20 20 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74  .  ../*.** Locat
2346f 65 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f  e a user functio
23470 6e 20 67 69 76 65 6e 20 61 20 6e 61 6d 65 2c 20  n given a name, 
23471 61 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  a number of argu
23472 6d 65 6e 74 73 20 61 6e 64 20 61 20 66 6c 61 67  ments and a flag
23473 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 77  .** indicating w
23474 68 65 74 68 65 72 20 74 68 65 20 66 75 6e 63 74  hether the funct
23475 69 6f 6e 20 70 72 65 66 65 72 73 20 55 54 46 2d  ion prefers UTF-
23476 31 36 20 6f 76 65 72 20 55 54 46 2d 38 2e 20 20  16 over UTF-8.  
23477 52 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e  Return a.** poin
23478 74 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44  ter to the FuncD
23479 65 66 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ef structure tha
2347a 74 20 64 65 66 69 6e 65 73 20 74 68 61 74 20 66  t defines that f
2347b 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 72 65 74 75  unction, or retu
2347c 72 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 66 20 74 68  rn.** NULL if th
2347d 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20  e function does 
2347e 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a  not exist..**.**
2347f 20 49 66 20 74 68 65 20 63 72 65 61 74 65 46 6c   If the createFl
23480 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  ag argument is t
23481 72 75 65 2c 20 74 68 65 6e 20 61 20 6e 65 77 20  rue, then a new 
23482 28 62 6c 61 6e 6b 29 20 46 75 6e 63 44 65 66 0a  (blank) FuncDef.
23483 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ** structure is 
23484 63 72 65 61 74 65 64 20 61 6e 64 20 6c 69 6b 65  created and like
23485 64 20 69 6e 74 6f 20 74 68 65 20 22 64 62 22 20  d into the "db" 
23486 73 74 72 75 63 74 75 72 65 20 69 66 20 61 0a 2a  structure if a.*
23487 2a 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 66 75  * no matching fu
23488 6e 63 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c  nction previousl
23489 79 20 65 78 69 73 74 65 64 2e 20 20 57 68 65 6e  y existed.  When
2348a 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 74   createFlag is t
2348b 72 75 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  rue.** and the n
2348c 41 72 67 20 70 61 72 61 6d 65 74 65 72 20 69 73  Arg parameter is
2348d 20 2d 31 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 61   -1, then only a
2348e 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 61   function that a
2348f 63 63 65 70 74 73 0a 2a 2a 20 61 6e 79 20 6e 75  ccepts.** any nu
23490 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
23491 73 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e  s will be return
23492 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72 65  ed..**.** If cre
23493 61 74 65 46 6c 61 67 20 69 73 20 66 61 6c 73 65  ateFlag is false
23494 20 61 6e 64 20 6e 41 72 67 20 69 73 20 2d 31 2c   and nArg is -1,
23495 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   then the first 
23496 76 61 6c 69 64 0a 2a 2a 20 66 75 6e 63 74 69 6f  valid.** functio
23497 6e 20 66 6f 75 6e 64 20 69 73 20 72 65 74 75 72  n found is retur
23498 6e 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e  ned.  A function
23499 20 69 73 20 76 61 6c 69 64 20 69 66 20 65 69 74   is valid if eit
2349a 68 65 72 20 78 46 75 6e 63 0a 2a 2a 20 6f 72 20  her xFunc.** or 
2349b 78 53 74 65 70 20 69 73 20 6e 6f 6e 2d 7a 65 72  xStep is non-zer
2349c 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72 65 61  o..**.** If crea
2349d 74 65 46 6c 61 67 20 69 73 20 66 61 6c 73 65 2c  teFlag is false,
2349e 20 74 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e   then a function
2349f 20 77 69 74 68 20 74 68 65 20 72 65 71 75 69 72   with the requir
234a0 65 64 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 6e  ed name and.** n
234a1 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
234a2 74 73 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e  ts may be return
234a3 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20 65  ed even if the e
234a4 54 65 78 74 52 65 70 20 66 6c 61 67 20 64 6f 65  TextRep flag doe
234a5 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 20 74  s not.** match t
234a6 68 61 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a  hat requested..*
234a7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
234a8 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65   FuncDef *sqlite
234a9 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 0a 20  3FindFunction(. 
234aa 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
234ab 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64      /* An open d
234ac 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e  atabase */.  con
234ad 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
234ae 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
234af 75 6e 63 74 69 6f 6e 2e 20 20 4e 6f 74 20 6e 75  unction.  Not nu
234b0 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f  ll-terminated */
234b1 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 20 20 20  .  int nName,   
234b2 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
234b3 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
234b4 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69   the name */.  i
234b5 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20  nt nArg,        
234b6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
234b7 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65  rguments.  -1 me
234b8 61 6e 73 20 61 6e 79 20 6e 75 6d 62 65 72 20 2a  ans any number *
234b9 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20  /.  u8 enc,     
234ba 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72         /* Prefer
234bb 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
234bc 67 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74  g */.  int creat
234bd 65 46 6c 61 67 20 20 20 20 20 2f 2a 20 43 72 65  eFlag     /* Cre
234be 61 74 65 20 6e 65 77 20 65 6e 74 72 79 20 69 66  ate new entry if
234bf 20 74 72 75 65 20 61 6e 64 20 64 6f 65 73 20 6e   true and does n
234c0 6f 74 20 6f 74 68 65 72 77 69 73 65 20 65 78 69  ot otherwise exi
234c1 73 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63 44  st */.){.  FuncD
234c2 65 66 20 2a 70 3b 20 20 20 20 20 20 20 20 20 2f  ef *p;         /
234c3 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
234c4 62 6c 65 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66  ble */.  FuncDef
234c5 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 2f 2a 20   *pBest = 0; /* 
234c6 42 65 73 74 20 6d 61 74 63 68 20 66 6f 75 6e 64  Best match found
234c7 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74   so far */.  int
234c8 20 62 65 73 74 53 63 6f 72 65 20 3d 20 30 3b 20   bestScore = 0; 
234c9 20 2f 2a 20 53 63 6f 72 65 20 6f 66 20 62 65 73   /* Score of bes
234ca 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 69 6e 74  t match */.  int
234cb 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   h;             
234cc 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 2a   /* Hash value *
234cd 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 65 6e  /...  assert( en
234ce 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c  c==SQLITE_UTF8 |
234cf 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  | enc==SQLITE_UT
234d0 46 31 36 4c 45 20 7c 7c 20 65 6e 63 3d 3d 53 51  F16LE || enc==SQ
234d1 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a  LITE_UTF16BE );.
234d2 20 20 69 66 28 20 6e 41 72 67 3c 2d 31 20 29 20    if( nArg<-1 ) 
234d3 6e 41 72 67 20 3d 20 2d 31 3b 0a 20 20 68 20 3d  nArg = -1;.  h =
234d4 20 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f   (sqlite3UpperTo
234d5 4c 6f 77 65 72 5b 28 75 38 29 7a 4e 61 6d 65 5b  Lower[(u8)zName[
234d6 30 5d 5d 20 2b 20 6e 4e 61 6d 65 29 20 25 20 41  0]] + nName) % A
234d7 72 72 61 79 53 69 7a 65 28 64 62 2d 3e 61 46 75  rraySize(db->aFu
234d8 6e 63 2e 61 29 3b 0a 0a 20 20 2f 2a 20 46 69 72  nc.a);..  /* Fir
234d9 73 74 20 73 65 61 72 63 68 20 66 6f 72 20 61 20  st search for a 
234da 6d 61 74 63 68 20 61 6d 6f 6e 67 73 74 20 74 68  match amongst th
234db 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
234dc 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 2e  fined functions.
234dd 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 66 75 6e 63  .  */.  p = func
234de 74 69 6f 6e 53 65 61 72 63 68 28 26 64 62 2d 3e  tionSearch(&db->
234df 61 46 75 6e 63 2c 20 68 2c 20 7a 4e 61 6d 65 2c  aFunc, h, zName,
234e0 20 6e 4e 61 6d 65 29 3b 0a 20 20 77 68 69 6c 65   nName);.  while
234e1 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 73  ( p ){.    int s
234e2 63 6f 72 65 20 3d 20 6d 61 74 63 68 51 75 61 6c  core = matchQual
234e3 69 74 79 28 70 2c 20 6e 41 72 67 2c 20 65 6e 63  ity(p, nArg, enc
234e4 29 3b 0a 20 20 20 20 69 66 28 20 73 63 6f 72 65  );.    if( score
234e5 3e 62 65 73 74 53 63 6f 72 65 20 29 7b 0a 20 20  >bestScore ){.  
234e6 20 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a 20      pBest = p;. 
234e7 20 20 20 20 20 62 65 73 74 53 63 6f 72 65 20 3d       bestScore =
234e8 20 73 63 6f 72 65 3b 0a 20 20 20 20 7d 0a 20 20   score;.    }.  
234e9 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a    p = p->pNext;.
234ea 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20    }..  /* If no 
234eb 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20  match is found, 
234ec 73 65 61 72 63 68 20 74 68 65 20 62 75 69 6c 74  search the built
234ed 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20  -in functions.. 
234ee 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 2c   **.  ** Except,
234ef 20 69 66 20 63 72 65 61 74 65 46 6c 61 67 20 69   if createFlag i
234f0 73 20 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61  s true, that mea
234f1 6e 73 20 74 68 61 74 20 77 65 20 61 72 65 20 74  ns that we are t
234f2 72 79 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 69 6e  rying to.  ** in
234f3 73 74 61 6c 6c 20 61 20 6e 65 77 20 66 75 6e 63  stall a new func
234f4 74 69 6f 6e 2e 20 20 57 68 61 74 65 76 65 72 20  tion.  Whatever 
234f5 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
234f6 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 77 69  e is returned wi
234f7 6c 6c 0a 20 20 2a 2a 20 68 61 76 65 20 66 69 65  ll.  ** have fie
234f8 6c 64 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20  lds overwritten 
234f9 77 69 74 68 20 6e 65 77 20 69 6e 66 6f 72 6d 61  with new informa
234fa 74 69 6f 6e 20 61 70 70 72 6f 70 72 69 61 74 65  tion appropriate
234fb 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6e 65   for the.  ** ne
234fc 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20 42 75 74  w function.  But
234fd 20 74 68 65 20 46 75 6e 63 44 65 66 73 20 66 6f   the FuncDefs fo
234fe 72 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74  r built-in funct
234ff 69 6f 6e 73 20 61 72 65 20 72 65 61 64 2d 6f 6e  ions are read-on
23500 6c 79 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 6d  ly..  ** So we m
23501 75 73 74 20 6e 6f 74 20 73 65 61 72 63 68 20 66  ust not search f
23502 6f 72 20 62 75 69 6c 74 2d 69 6e 73 20 77 68 65  or built-ins whe
23503 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77  n creating a new
23504 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 20   function..  */ 
23505 0a 20 20 69 66 28 20 21 63 72 65 61 74 65 46 6c  .  if( !createFl
23506 61 67 20 26 26 20 21 70 42 65 73 74 20 29 7b 0a  ag && !pBest ){.
23507 20 20 20 20 46 75 6e 63 44 65 66 48 61 73 68 20      FuncDefHash 
23508 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c  *pHash = &GLOBAL
23509 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71  (FuncDefHash, sq
2350a 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74  lite3GlobalFunct
2350b 69 6f 6e 73 29 3b 0a 20 20 20 20 70 20 3d 20 66  ions);.    p = f
2350c 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28 70 48  unctionSearch(pH
2350d 61 73 68 2c 20 68 2c 20 7a 4e 61 6d 65 2c 20 6e  ash, h, zName, n
2350e 4e 61 6d 65 29 3b 0a 20 20 20 20 77 68 69 6c 65  Name);.    while
2350f 28 20 70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ( p ){.      int
23510 20 73 63 6f 72 65 20 3d 20 6d 61 74 63 68 51 75   score = matchQu
23511 61 6c 69 74 79 28 70 2c 20 6e 41 72 67 2c 20 65  ality(p, nArg, e
23512 6e 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73  nc);.      if( s
23513 63 6f 72 65 3e 62 65 73 74 53 63 6f 72 65 20 29  core>bestScore )
23514 7b 0a 20 20 20 20 20 20 20 20 70 42 65 73 74 20  {.        pBest 
23515 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 65 73  = p;.        bes
23516 74 53 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b 0a  tScore = score;.
23517 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 20        }.      p 
23518 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  = p->pNext;.    
23519 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
2351a 68 65 20 63 72 65 61 74 65 46 6c 61 67 20 70 61  he createFlag pa
2351b 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 20  rameter is true 
2351c 61 6e 64 20 74 68 65 20 73 65 61 72 63 68 20 64  and the search d
2351d 69 64 20 6e 6f 74 20 72 65 76 65 61 6c 20 61 6e  id not reveal an
2351e 0a 20 20 2a 2a 20 65 78 61 63 74 20 6d 61 74 63  .  ** exact matc
2351f 68 20 66 6f 72 20 74 68 65 20 6e 61 6d 65 2c 20  h for the name, 
23520 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
23521 6e 74 73 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67  nts and encoding
23522 2c 20 74 68 65 6e 20 61 64 64 20 61 0a 20 20 2a  , then add a.  *
23523 2a 20 6e 65 77 20 65 6e 74 72 79 20 74 6f 20 74  * new entry to t
23524 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e  he hash table an
23525 64 20 72 65 74 75 72 6e 20 69 74 2e 0a 20 20 2a  d return it..  *
23526 2f 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c  /.  if( createFl
23527 61 67 20 26 26 20 28 62 65 73 74 53 63 6f 72 65  ag && (bestScore
23528 3c 36 20 7c 7c 20 70 42 65 73 74 2d 3e 6e 41 72  <6 || pBest->nAr
23529 67 21 3d 6e 41 72 67 29 20 26 26 20 0a 20 20 20  g!=nArg) && .   
2352a 20 20 20 28 70 42 65 73 74 20 3d 20 73 71 6c 69     (pBest = sqli
2352b 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2352c 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 42 65 73  db, sizeof(*pBes
2352d 74 29 2b 6e 4e 61 6d 65 2b 31 29 29 21 3d 30 20  t)+nName+1))!=0 
2352e 29 7b 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a 4e  ){.    pBest->zN
2352f 61 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70  ame = (char *)&p
23530 42 65 73 74 5b 31 5d 3b 0a 20 20 20 20 70 42 65  Best[1];.    pBe
23531 73 74 2d 3e 6e 41 72 67 20 3d 20 28 75 31 36 29  st->nArg = (u16)
23532 6e 41 72 67 3b 0a 20 20 20 20 70 42 65 73 74 2d  nArg;.    pBest-
23533 3e 69 50 72 65 66 45 6e 63 20 3d 20 65 6e 63 3b  >iPrefEnc = enc;
23534 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 65 73  .    memcpy(pBes
23535 74 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c  t->zName, zName,
23536 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 70 42 65   nName);.    pBe
23537 73 74 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d  st->zName[nName]
23538 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
23539 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 26  3FuncDefInsert(&
2353a 64 62 2d 3e 61 46 75 6e 63 2c 20 70 42 65 73 74  db->aFunc, pBest
2353b 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 42  );.  }..  if( pB
2353c 65 73 74 20 26 26 20 28 70 42 65 73 74 2d 3e 78  est && (pBest->x
2353d 53 74 65 70 20 7c 7c 20 70 42 65 73 74 2d 3e 78  Step || pBest->x
2353e 46 75 6e 63 20 7c 7c 20 63 72 65 61 74 65 46 6c  Func || createFl
2353f 61 67 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ag) ){.    retur
23540 6e 20 70 42 65 73 74 3b 0a 20 20 7d 0a 20 20 72  n pBest;.  }.  r
23541 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
23542 2a 20 46 72 65 65 20 61 6c 6c 20 72 65 73 6f 75  * Free all resou
23543 72 63 65 73 20 68 65 6c 64 20 62 79 20 74 68 65  rces held by the
23544 20 73 63 68 65 6d 61 20 73 74 72 75 63 74 75 72   schema structur
23545 65 2e 20 54 68 65 20 76 6f 69 64 2a 20 61 72 67  e. The void* arg
23546 75 6d 65 6e 74 20 70 6f 69 6e 74 73 0a 2a 2a 20  ument points.** 
23547 61 74 20 61 20 53 63 68 65 6d 61 20 73 74 72 75  at a Schema stru
23548 63 74 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ct. This functio
23549 6e 20 64 6f 65 73 20 6e 6f 74 20 63 61 6c 6c 20  n does not call 
2354a 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2354b 2c 20 29 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 70  , ) on the .** p
2354c 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 2c 20 69  ointer itself, i
2354d 74 20 6a 75 73 74 20 63 6c 65 61 6e 73 20 75 70  t just cleans up
2354e 20 73 75 62 73 69 64 75 61 72 79 20 72 65 73 6f   subsiduary reso
2354f 75 72 63 65 73 20 28 69 2e 65 2e 20 74 68 65 20  urces (i.e. the 
23550 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74  contents.** of t
23551 68 65 20 73 63 68 65 6d 61 20 68 61 73 68 20 74  he schema hash t
23552 61 62 6c 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  ables)..**.** Th
23553 65 20 53 63 68 65 6d 61 2e 63 61 63 68 65 5f 73  e Schema.cache_s
23554 69 7a 65 20 76 61 72 69 61 62 6c 65 20 69 73 20  ize variable is 
23555 6e 6f 74 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a  not cleared..*/.
23556 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23557 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65 6d  oid sqlite3Schem
23558 61 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a  aFree(void *p){.
23559 20 20 48 61 73 68 20 74 65 6d 70 31 3b 0a 20 20    Hash temp1;.  
2355a 48 61 73 68 20 74 65 6d 70 32 3b 0a 20 20 48 61  Hash temp2;.  Ha
2355b 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20  shElem *pElem;. 
2355c 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
2355d 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 3b 0a   = (Schema *)p;.
2355e 0a 20 20 74 65 6d 70 31 20 3d 20 70 53 63 68 65  .  temp1 = pSche
2355f 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 74  ma->tblHash;.  t
23560 65 6d 70 32 20 3d 20 70 53 63 68 65 6d 61 2d 3e  emp2 = pSchema->
23561 74 72 69 67 48 61 73 68 3b 0a 20 20 73 71 6c 69  trigHash;.  sqli
23562 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53 63  te3HashInit(&pSc
23563 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 3b  hema->trigHash);
23564 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  .  sqlite3HashCl
23565 65 61 72 28 26 70 53 63 68 65 6d 61 2d 3e 69 64  ear(&pSchema->id
23566 78 48 61 73 68 29 3b 0a 20 20 66 6f 72 28 70 45  xHash);.  for(pE
23567 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 46 69  lem=sqliteHashFi
23568 72 73 74 28 26 74 65 6d 70 32 29 3b 20 70 45 6c  rst(&temp2); pEl
23569 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65  em; pElem=sqlite
2356a 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 29  HashNext(pElem))
2356b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  {.    sqlite3Del
2356c 65 74 65 54 72 69 67 67 65 72 28 30 2c 20 28 54  eteTrigger(0, (T
2356d 72 69 67 67 65 72 2a 29 73 71 6c 69 74 65 48 61  rigger*)sqliteHa
2356e 73 68 44 61 74 61 28 70 45 6c 65 6d 29 29 3b 0a  shData(pElem));.
2356f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73    }.  sqlite3Has
23570 68 43 6c 65 61 72 28 26 74 65 6d 70 32 29 3b 0a  hClear(&temp2);.
23571 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
23572 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  t(&pSchema->tblH
23573 61 73 68 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65  ash);.  for(pEle
23574 6d 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  m=sqliteHashFirs
23575 74 28 26 74 65 6d 70 31 29 3b 20 70 45 6c 65 6d  t(&temp1); pElem
23576 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61  ; pElem=sqliteHa
23577 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a  shNext(pElem)){.
23578 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
23579 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  = sqliteHashData
2357a 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20 61 73 73  (pElem);.    ass
2357b 65 72 74 28 20 70 54 61 62 2d 3e 64 62 4d 65 6d  ert( pTab->dbMem
2357c 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==0 );.    sqlit
2357d 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54  e3DeleteTable(pT
2357e 61 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ab);.  }.  sqlit
2357f 65 33 48 61 73 68 43 6c 65 61 72 28 26 74 65 6d  e3HashClear(&tem
23580 70 31 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e  p1);.  pSchema->
23581 70 53 65 71 54 61 62 20 3d 20 30 3b 0a 20 20 70  pSeqTab = 0;.  p
23582 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26 3d  Schema->flags &=
23583 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65   ~DB_SchemaLoade
23584 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  d;.}../*.** Find
23585 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20   and return the 
23586 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65  schema associate
23587 64 20 77 69 74 68 20 61 20 42 54 72 65 65 2e 20  d with a BTree. 
23588 20 43 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77   Create.** a new
23589 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61 72   one if necessar
2358a 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
2358b 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c  VATE Schema *sql
2358c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 73 71  ite3SchemaGet(sq
2358d 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 72 65 65  lite3 *db, Btree
2358e 20 2a 70 42 74 29 7b 0a 20 20 53 63 68 65 6d 61   *pBt){.  Schema
2358f 20 2a 20 70 3b 0a 20 20 69 66 28 20 70 42 74 20   * p;.  if( pBt 
23590 29 7b 0a 20 20 20 20 70 20 3d 20 28 53 63 68 65  ){.    p = (Sche
23591 6d 61 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65  ma *)sqlite3Btre
23592 65 53 63 68 65 6d 61 28 70 42 74 2c 20 73 69 7a  eSchema(pBt, siz
23593 65 6f 66 28 53 63 68 65 6d 61 29 2c 20 73 71 6c  eof(Schema), sql
23594 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 29 3b  ite3SchemaFree);
23595 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20  .  }else{.    p 
23596 3d 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69  = (Schema *)sqli
23597 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69  te3MallocZero(si
23598 7a 65 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a 20  zeof(Schema));. 
23599 20 7d 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20   }.  if( !p ){. 
2359a 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2359b 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  led = 1;.  }else
2359c 20 69 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c 65   if ( 0==p->file
2359d 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 73  _format ){.    s
2359e 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
2359f 70 2d 3e 74 62 6c 48 61 73 68 29 3b 0a 20 20 20  p->tblHash);.   
235a0 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74   sqlite3HashInit
235a1 28 26 70 2d 3e 69 64 78 48 61 73 68 29 3b 0a 20  (&p->idxHash);. 
235a2 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e     sqlite3HashIn
235a3 69 74 28 26 70 2d 3e 74 72 69 67 48 61 73 68 29  it(&p->trigHash)
235a4 3b 0a 20 20 20 20 70 2d 3e 65 6e 63 20 3d 20 53  ;.    p->enc = S
235a5 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 7d 0a  QLITE_UTF8;.  }.
235a6 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
235a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
235a8 6e 64 20 6f 66 20 63 61 6c 6c 62 61 63 6b 2e 63  nd of callback.c
235a9 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
235aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
235ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
235ad 65 67 69 6e 20 66 69 6c 65 20 64 65 6c 65 74 65  egin file delete
235ae 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
235af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
235b1 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
235b2 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
235b3 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
235b4 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
235b5 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
235b6 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
235b7 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
235b8 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
235b9 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
235ba 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
235bb 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
235bc 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
235bd 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
235be 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
235bf 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
235c0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
235c1 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
235c2 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
235c3 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
235c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235c8 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
235c9 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
235ca 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  de routines that
235cb 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74   are called by t
235cc 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 69 6e 20  he parser.** in 
235cd 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74  order to generat
235ce 65 20 63 6f 64 65 20 66 6f 72 20 44 45 4c 45 54  e code for DELET
235cf 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74  E FROM statement
235d0 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 65  s..**.** $Id: de
235d1 6c 65 74 65 2e 63 2c 76 20 31 2e 32 30 31 20 32  lete.c,v 1.201 2
235d2 30 30 39 2f 30 35 2f 30 31 20 32 31 3a 31 33 3a  009/05/01 21:13:
235d3 33 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  37 drh Exp $.*/.
235d4 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 20 75 70 20 65  ./*.** Look up e
235d5 76 65 72 79 20 74 61 62 6c 65 20 74 68 61 74 20  very table that 
235d6 69 73 20 6e 61 6d 65 64 20 69 6e 20 70 53 72 63  is named in pSrc
235d7 2e 20 20 49 66 20 61 6e 79 20 74 61 62 6c 65 20  .  If any table 
235d8 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 0a 2a 2a  is not found,.**
235d9 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   add an error me
235da 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 2d  ssage to pParse-
235db 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 72 65 74  >zErrMsg and ret
235dc 75 72 6e 20 4e 55 4c 4c 2e 20 20 49 66 20 61 6c  urn NULL.  If al
235dd 6c 20 74 61 62 6c 65 73 0a 2a 2a 20 61 72 65 20  l tables.** are 
235de 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 61 20  found, return a 
235df 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c  pointer to the l
235e0 61 73 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ast table..*/.SQ
235e1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62  LITE_PRIVATE Tab
235e2 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69  le *sqlite3SrcLi
235e3 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a  stLookup(Parse *
235e4 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20  pParse, SrcList 
235e5 2a 70 53 72 63 29 7b 0a 20 20 73 74 72 75 63 74  *pSrc){.  struct
235e6 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
235e7 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e 61 3b 0a  Item = pSrc->a;.
235e8 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
235e9 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 20 26   assert( pItem &
235ea 26 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31 20  & pSrc->nSrc==1 
235eb 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  );.  pTab = sqli
235ec 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70  te3LocateTable(p
235ed 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65 6d 2d  Parse, 0, pItem-
235ee 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a  >zName, pItem->z
235ef 44 61 74 61 62 61 73 65 29 3b 0a 20 20 73 71 6c  Database);.  sql
235f0 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
235f1 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20  pItem->pTab);.  
235f2 70 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 54  pItem->pTab = pT
235f3 61 62 3b 0a 20 20 69 66 28 20 70 54 61 62 20 29  ab;.  if( pTab )
235f4 7b 0a 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66  {.    pTab->nRef
235f5 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  ++;.  }.  if( sq
235f6 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f  lite3IndexedByLo
235f7 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 49 74  okup(pParse, pIt
235f8 65 6d 29 20 29 7b 0a 20 20 20 20 70 54 61 62 20  em) ){.    pTab 
235f9 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
235fa 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pTab;.}../*.**
235fb 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73   Check to make s
235fc 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 74 61  ure the given ta
235fd 62 6c 65 20 69 73 20 77 72 69 74 61 62 6c 65 2e  ble is writable.
235fe 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 0a 2a    If it is not.*
235ff 2a 20 77 72 69 74 61 62 6c 65 2c 20 67 65 6e 65  * writable, gene
23600 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rate an error me
23601 73 73 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e  ssage and return
23602 20 31 2e 20 20 49 66 20 69 74 20 69 73 0a 2a 2a   1.  If it is.**
23603 20 77 72 69 74 61 62 6c 65 20 72 65 74 75 72 6e   writable return
23604 20 30 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   0;.*/.SQLITE_PR
23605 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
23606 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73  3IsReadOnly(Pars
23607 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
23608 20 2a 70 54 61 62 2c 20 69 6e 74 20 76 69 65 77   *pTab, int view
23609 4f 6b 29 7b 0a 20 20 69 66 28 20 28 28 70 54 61  Ok){.  if( ((pTa
2360a 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
2360b 5f 52 65 61 64 6f 6e 6c 79 29 21 3d 30 0a 20 20  _Readonly)!=0.  
2360c 20 20 20 20 20 20 26 26 20 28 70 50 61 72 73 65        && (pParse
2360d 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
2360e 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61  LITE_WriteSchema
2360f 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20  )==0.        && 
23610 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
23611 30 29 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0) .#ifndef SQLI
23612 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
23613 41 42 4c 45 0a 20 20 20 20 20 20 7c 7c 20 28 70  ABLE.      || (p
23614 54 61 62 2d 3e 70 4d 6f 64 20 26 26 20 70 54 61  Tab->pMod && pTa
23615 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65  b->pMod->pModule
23616 2d 3e 78 55 70 64 61 74 65 3d 3d 30 29 0a 23 65  ->xUpdate==0).#e
23617 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20 73 71  ndif.  ){.    sq
23618 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
23619 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20  arse, "table %s 
2361a 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66  may not be modif
2361b 69 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  ied", pTab->zNam
2361c 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
2361d 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
2361e 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20  LITE_OMIT_VIEW. 
2361f 20 69 66 28 20 21 76 69 65 77 4f 6b 20 26 26 20   if( !viewOk && 
23620 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pTab->pSelect ){
23621 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
23622 72 4d 73 67 28 70 50 61 72 73 65 2c 22 63 61 6e  rMsg(pParse,"can
23623 6e 6f 74 20 6d 6f 64 69 66 79 20 25 73 20 62 65  not modify %s be
23624 63 61 75 73 65 20 69 74 20 69 73 20 61 20 76 69  cause it is a vi
23625 65 77 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  ew",pTab->zName)
23626 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
23627 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
23628 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 23 69 66 20 21  urn 0;.}...#if !
23629 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2362a 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64 65  MIT_VIEW) && !de
2362b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2362c 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a 0a 2a 2a  T_TRIGGER)./*.**
2362d 20 45 76 61 6c 75 61 74 65 20 61 20 76 69 65 77   Evaluate a view
2362e 20 61 6e 64 20 73 74 6f 72 65 20 69 74 73 20 72   and store its r
2362f 65 73 75 6c 74 20 69 6e 20 61 6e 20 65 70 68 65  esult in an ephe
23630 6d 65 72 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  meral table.  Th
23631 65 0a 2a 2a 20 70 57 68 65 72 65 20 61 72 67 75  e.** pWhere argu
23632 6d 65 6e 74 20 69 73 20 61 6e 20 6f 70 74 69 6f  ment is an optio
23633 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65  nal WHERE clause
23634 20 74 68 61 74 20 72 65 73 74 72 69 63 74 73 20   that restricts 
23635 74 68 65 0a 2a 2a 20 73 65 74 20 6f 66 20 72 6f  the.** set of ro
23636 77 73 20 69 6e 20 74 68 65 20 76 69 65 77 20 74  ws in the view t
23637 68 61 74 20 61 72 65 20 74 6f 20 62 65 20 61 64  hat are to be ad
23638 64 65 64 20 74 6f 20 74 68 65 20 65 70 68 65 6d  ded to the ephem
23639 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  eral table..*/.S
2363a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2363b 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69  id sqlite3Materi
2363c 61 6c 69 7a 65 56 69 65 77 28 0a 20 20 50 61 72  alizeView(.  Par
2363d 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2363e 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2363f 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
23640 2a 70 56 69 65 77 2c 20 20 20 20 20 20 20 20 2f  *pView,        /
23641 2a 20 56 69 65 77 20 64 65 66 69 6e 69 74 69 6f  * View definitio
23642 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  n */.  Expr *pWh
23643 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f  ere,        /* O
23644 70 74 69 6f 6e 61 6c 20 57 48 45 52 45 20 63 6c  ptional WHERE cl
23645 61 75 73 65 20 74 6f 20 62 65 20 61 64 64 65 64  ause to be added
23646 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20 20   */.  int iCur  
23647 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
23648 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
23649 65 70 68 65 6d 65 72 69 61 6c 20 74 61 62 6c 65  ephemerial table
2364a 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 44   */.){.  SelectD
2364b 65 73 74 20 64 65 73 74 3b 0a 20 20 53 65 6c 65  est dest;.  Sele
2364c 63 74 20 2a 70 44 75 70 3b 0a 20 20 73 71 6c 69  ct *pDup;.  sqli
2364d 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2364e 2d 3e 64 62 3b 0a 0a 20 20 70 44 75 70 20 3d 20  ->db;..  pDup = 
2364f 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
23650 28 64 62 2c 20 70 56 69 65 77 2d 3e 70 53 65 6c  (db, pView->pSel
23651 65 63 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ect, 0);.  if( p
23652 57 68 65 72 65 20 29 7b 0a 20 20 20 20 53 72 63  Where ){.    Src
23653 4c 69 73 74 20 2a 70 46 72 6f 6d 3b 0a 20 20 20  List *pFrom;.   
23654 20 54 6f 6b 65 6e 20 76 69 65 77 4e 61 6d 65 3b   Token viewName;
23655 0a 20 20 20 20 0a 20 20 20 20 70 57 68 65 72 65  .    .    pWhere
23656 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
23657 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20 30 29  p(db, pWhere, 0)
23658 3b 0a 20 20 20 20 76 69 65 77 4e 61 6d 65 2e 7a  ;.    viewName.z
23659 20 3d 20 28 75 38 2a 29 70 56 69 65 77 2d 3e 7a   = (u8*)pView->z
2365a 4e 61 6d 65 3b 0a 20 20 20 20 76 69 65 77 4e 61  Name;.    viewNa
2365b 6d 65 2e 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64  me.n = (unsigned
2365c 20 69 6e 74 29 73 71 6c 69 74 65 33 53 74 72 6c   int)sqlite3Strl
2365d 65 6e 33 30 28 28 63 6f 6e 73 74 20 63 68 61 72  en30((const char
2365e 2a 29 76 69 65 77 4e 61 6d 65 2e 7a 29 3b 0a 20  *)viewName.z);. 
2365f 20 20 20 76 69 65 77 4e 61 6d 65 2e 71 75 6f 74     viewName.quot
23660 65 64 20 3d 20 30 3b 0a 20 20 20 20 70 46 72 6f  ed = 0;.    pFro
23661 6d 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  m = sqlite3SrcLi
23662 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d  stAppendFromTerm
23663 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30  (pParse, 0, 0, 0
23664 2c 20 26 76 69 65 77 4e 61 6d 65 2c 20 70 44 75  , &viewName, pDu
23665 70 2c 20 30 2c 30 29 3b 0a 20 20 20 20 70 44 75  p, 0,0);.    pDu
23666 70 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  p = sqlite3Selec
23667 74 4e 65 77 28 70 50 61 72 73 65 2c 20 30 2c 20  tNew(pParse, 0, 
23668 70 46 72 6f 6d 2c 20 70 57 68 65 72 65 2c 20 30  pFrom, pWhere, 0
23669 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29  , 0, 0, 0, 0, 0)
2366a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53  ;.  }.  sqlite3S
2366b 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64  electDestInit(&d
2366c 65 73 74 2c 20 53 52 54 5f 45 70 68 65 6d 54 61  est, SRT_EphemTa
2366d 62 2c 20 69 43 75 72 29 3b 0a 20 20 73 71 6c 69  b, iCur);.  sqli
2366e 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
2366f 2c 20 70 44 75 70 2c 20 26 64 65 73 74 29 3b 0a  , pDup, &dest);.
23670 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
23671 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b  elete(db, pDup);
23672 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  .}.#endif /* !de
23673 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
23674 54 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69  T_VIEW) && !defi
23675 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
23676 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 23 69 66  TRIGGER) */..#if
23677 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
23678 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45  ENABLE_UPDATE_DE
23679 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21  LETE_LIMIT) && !
2367a 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2367b 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a  MIT_SUBQUERY)./*
2367c 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20  .** Generate an 
2367d 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
2367e 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
2367f 20 57 48 45 52 45 2c 20 4f 52 44 45 52 20 42 59   WHERE, ORDER BY
23680 2c 0a 2a 2a 20 61 6e 64 20 4c 49 4d 49 54 2f 4f  ,.** and LIMIT/O
23681 46 46 53 45 54 20 70 6f 72 74 69 6f 6e 20 6f 66  FFSET portion of
23682 20 44 45 4c 45 54 45 20 61 6e 64 20 55 50 44 41   DELETE and UPDA
23683 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  TE statements..*
23684 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20  *.**     DELETE 
23685 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20  FROM table_wxyz 
23686 57 48 45 52 45 20 61 3c 35 20 4f 52 44 45 52 20  WHERE a<5 ORDER 
23687 42 59 20 61 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a  BY a LIMIT 1;.**
23688 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23689 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f              \___
2368a 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2368b 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20  _______/.**     
2368c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2368d 20 20 20 20 20 20 20 20 20 20 70 4c 69 6d 69 74            pLimit
2368e 57 68 65 72 65 20 28 70 49 6e 43 6c 61 75 73 65  Where (pInClause
2368f 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ).*/.SQLITE_PRIV
23690 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
23691 33 4c 69 6d 69 74 57 68 65 72 65 28 0a 20 20 50  3LimitWhere(.  P
23692 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
23693 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
23694 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
23695 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
23696 70 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20  pSrc,           
23697 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20      /* the FROM 
23698 63 6c 61 75 73 65 20 2d 2d 20 77 68 69 63 68 20  clause -- which 
23699 74 61 62 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a  tables to scan *
2369a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
2369b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2369c 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
2369d 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75  ause.  May be nu
2369e 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ll */.  ExprList
2369f 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20   *pOrderBy,     
236a0 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45       /* The ORDE
236a1 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61  R BY clause.  Ma
236a2 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45  y be null */.  E
236a3 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20  xpr *pLimit,    
236a4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
236a5 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e  he LIMIT clause.
236a6 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f    May be null */
236a7 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74  .  Expr *pOffset
236a8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
236a9 2f 2a 20 54 68 65 20 4f 46 46 53 45 54 20 63 6c  /* The OFFSET cl
236aa 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75  ause.  May be nu
236ab 6c 6c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53  ll */.  char *zS
236ac 74 6d 74 54 79 70 65 20 20 20 20 20 20 20 20 20  tmtType         
236ad 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 44       /* Either D
236ae 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 2e  ELETE or UPDATE.
236af 20 20 46 6f 72 20 65 72 72 6f 72 20 6d 65 73 73    For error mess
236b0 61 67 65 73 2e 20 2a 2f 0a 29 7b 0a 20 20 45 78  ages. */.){.  Ex
236b1 70 72 20 2a 70 57 68 65 72 65 52 6f 77 69 64 20  pr *pWhereRowid 
236b2 3d 20 4e 55 4c 4c 3b 20 20 20 20 2f 2a 20 57 48  = NULL;    /* WH
236b3 45 52 45 20 72 6f 77 69 64 20 2e 2e 20 2a 2f 0a  ERE rowid .. */.
236b4 20 20 45 78 70 72 20 2a 70 49 6e 43 6c 61 75 73    Expr *pInClaus
236b5 65 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f  e = NULL;      /
236b6 2a 20 57 48 45 52 45 20 72 6f 77 69 64 20 49 4e  * WHERE rowid IN
236b7 20 28 20 73 65 6c 65 63 74 20 29 20 2a 2f 0a 20   ( select ) */. 
236b8 20 45 78 70 72 20 2a 70 53 65 6c 65 63 74 52 6f   Expr *pSelectRo
236b9 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20 2f 2a  wid = NULL;   /*
236ba 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20 2e 2e   SELECT rowid ..
236bb 2e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  . */.  ExprList 
236bc 2a 70 45 4c 69 73 74 20 3d 20 4e 55 4c 4c 3b 20  *pEList = NULL; 
236bd 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
236be 6e 20 6c 69 73 74 20 63 6f 6e 74 61 6e 69 6e 67  n list contaning
236bf 20 6f 6e 6c 79 20 70 53 65 6c 65 63 74 52 6f 77   only pSelectRow
236c0 69 64 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  id */.  SrcList 
236c1 2a 70 53 65 6c 65 63 74 53 72 63 20 3d 20 4e 55  *pSelectSrc = NU
236c2 4c 4c 3b 20 20 2f 2a 20 53 45 4c 45 43 54 20 72  LL;  /* SELECT r
236c3 6f 77 69 64 20 46 52 4f 4d 20 78 20 2e 2e 2e 20  owid FROM x ... 
236c4 28 64 75 70 20 6f 66 20 70 53 72 63 29 20 2a 2f  (dup of pSrc) */
236c5 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
236c6 63 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20  ct = NULL;      
236c7 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 53 45 4c 45  /* Complete SELE
236c8 43 54 20 74 72 65 65 20 2a 2f 0a 0a 20 20 2f 2a  CT tree */..  /*
236c9 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 72   Check that ther
236ca 65 20 69 73 6e 27 74 20 61 6e 20 4f 52 44 45 52  e isn't an ORDER
236cb 20 42 59 20 77 69 74 68 6f 75 74 20 61 20 4c 49   BY without a LI
236cc 4d 49 54 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f  MIT clause..  */
236cd 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20  .  if( pOrderBy 
236ce 26 26 20 28 70 4c 69 6d 69 74 20 3d 3d 20 30 29  && (pLimit == 0)
236cf 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33   ) {.    sqlite3
236d0 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
236d1 20 22 4f 52 44 45 52 20 42 59 20 77 69 74 68 6f   "ORDER BY witho
236d2 75 74 20 4c 49 4d 49 54 20 6f 6e 20 25 73 22 2c  ut LIMIT on %s",
236d3 20 7a 53 74 6d 74 54 79 70 65 29 3b 0a 20 20 20   zStmtType);.   
236d4 20 70 50 61 72 73 65 2d 3e 70 61 72 73 65 45 72   pParse->parseEr
236d5 72 6f 72 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74  ror = 1;.    got
236d6 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  o limit_where_cl
236d7 65 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20  eanup_2;.  }..  
236d8 2f 2a 20 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20  /* We only need 
236d9 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 73 65  to generate a se
236da 6c 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e 20  lect expression 
236db 69 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73  if there.  ** is
236dc 20 61 20 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20   a limit/offset 
236dd 74 65 72 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e  term to enforce.
236de 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 6d  .  */.  if( pLim
236df 69 74 20 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20  it == 0 ) {.    
236e0 2f 2a 20 69 66 20 70 4c 69 6d 69 74 20 69 73 20  /* if pLimit is 
236e1 6e 75 6c 6c 2c 20 70 4f 66 66 73 65 74 20 77 69  null, pOffset wi
236e2 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 6e 75 6c  ll always be nul
236e3 6c 20 61 73 20 77 65 6c 6c 2e 20 2a 2f 0a 20 20  l as well. */.  
236e4 20 20 61 73 73 65 72 74 28 20 70 4f 66 66 73 65    assert( pOffse
236e5 74 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 72 65  t == 0 );.    re
236e6 74 75 72 6e 20 70 57 68 65 72 65 3b 0a 20 20 7d  turn pWhere;.  }
236e7 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
236e8 61 20 73 65 6c 65 63 74 20 65 78 70 72 65 73 73  a select express
236e9 69 6f 6e 20 74 72 65 65 20 74 6f 20 65 6e 66 6f  ion tree to enfo
236ea 72 63 65 20 74 68 65 20 6c 69 6d 69 74 2f 6f 66  rce the limit/of
236eb 66 73 65 74 20 0a 20 20 2a 2a 20 74 65 72 6d 20  fset .  ** term 
236ec 66 6f 72 20 74 68 65 20 44 45 4c 45 54 45 20 6f  for the DELETE o
236ed 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  r UPDATE stateme
236ee 6e 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  nt.  For example
236ef 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54 45 20  :.  **   DELETE 
236f0 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45  FROM table_a WHE
236f1 52 45 20 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20  RE col1=1 ORDER 
236f2 42 59 20 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20  BY col2 LIMIT 1 
236f3 4f 46 46 53 45 54 20 31 0a 20 20 2a 2a 20 62 65  OFFSET 1.  ** be
236f4 63 6f 6d 65 73 3a 0a 20 20 2a 2a 20 20 20 44 45  comes:.  **   DE
236f5 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f  LETE FROM table_
236f6 61 20 57 48 45 52 45 20 72 6f 77 69 64 20 49 4e  a WHERE rowid IN
236f7 20 28 20 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c   ( .  **     SEL
236f8 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74  ECT rowid FROM t
236f9 61 62 6c 65 5f 61 20 57 48 45 52 45 20 63 6f 6c  able_a WHERE col
236fa 31 3d 31 20 4f 52 44 45 52 20 42 59 20 63 6f 6c  1=1 ORDER BY col
236fb 32 20 4c 49 4d 49 54 20 31 20 4f 46 46 53 45 54  2 LIMIT 1 OFFSET
236fc 20 31 0a 20 20 2a 2a 20 20 20 29 3b 0a 20 20 2a   1.  **   );.  *
236fd 2f 0a 0a 20 20 70 53 65 6c 65 63 74 52 6f 77 69  /..  pSelectRowi
236fe 64 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28  d = sqlite3Expr(
236ff 70 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 52  pParse->db, TK_R
23700 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  OW, 0, 0, 0);.  
23701 69 66 28 20 70 53 65 6c 65 63 74 52 6f 77 69 64  if( pSelectRowid
23702 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d   == 0 ) goto lim
23703 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
23704 5f 32 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 73  _2;.  pEList = s
23705 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
23706 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20  pend(pParse, 0, 
23707 70 53 65 6c 65 63 74 52 6f 77 69 64 2c 20 30 29  pSelectRowid, 0)
23708 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 3d  ;.  if( pEList =
23709 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74  = 0 ) goto limit
2370a 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32  _where_cleanup_2
2370b 3b 0a 0a 20 20 2f 2a 20 64 75 70 6c 69 63 61 74  ;..  /* duplicat
2370c 65 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  e the FROM claus
2370d 65 20 61 73 20 69 74 20 69 73 20 6e 65 65 64 65  e as it is neede
2370e 64 20 62 79 20 62 6f 74 68 20 74 68 65 20 44 45  d by both the DE
2370f 4c 45 54 45 2f 55 50 44 41 54 45 20 74 72 65 65  LETE/UPDATE tree
23710 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 53 45  .  ** and the SE
23711 4c 45 43 54 20 73 75 62 74 72 65 65 2e 20 2a 2f  LECT subtree. */
23712 0a 20 20 70 53 65 6c 65 63 74 53 72 63 20 3d 20  .  pSelectSrc = 
23713 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
23714 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  p(pParse->db, pS
23715 72 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53  rc, 0);.  if( pS
23716 65 6c 65 63 74 53 72 63 20 3d 3d 20 30 20 29 20  electSrc == 0 ) 
23717 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
23718 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
23719 73 65 2d 3e 64 62 2c 20 70 45 4c 69 73 74 29 3b  se->db, pEList);
2371a 0a 20 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f  .    goto limit_
2371b 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b  where_cleanup_2;
2371c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65 72  .  }..  /* gener
2371d 61 74 65 20 74 68 65 20 53 45 4c 45 43 54 20 65  ate the SELECT e
2371e 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
2371f 2a 2f 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73  */.  pSelect = s
23720 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
23721 70 50 61 72 73 65 2c 70 45 4c 69 73 74 2c 70 53  pParse,pEList,pS
23722 65 6c 65 63 74 53 72 63 2c 70 57 68 65 72 65 2c  electSrc,pWhere,
23723 30 2c 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0,0,.           
23724 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23725 20 20 70 4f 72 64 65 72 42 79 2c 30 2c 70 4c 69    pOrderBy,0,pLi
23726 6d 69 74 2c 70 4f 66 66 73 65 74 29 3b 0a 20 20  mit,pOffset);.  
23727 69 66 28 20 70 53 65 6c 65 63 74 20 3d 3d 20 30  if( pSelect == 0
23728 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20   ) return 0;..  
23729 2f 2a 20 6e 6f 77 20 67 65 6e 65 72 61 74 65 20  /* now generate 
2372a 74 68 65 20 6e 65 77 20 57 48 45 52 45 20 72 6f  the new WHERE ro
2372b 77 69 64 20 49 4e 20 63 6c 61 75 73 65 20 66 6f  wid IN clause fo
2372c 72 20 74 68 65 20 44 45 4c 45 54 45 2f 55 44 50  r the DELETE/UDP
2372d 41 54 45 20 2a 2f 0a 20 20 70 57 68 65 72 65 52  ATE */.  pWhereR
2372e 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 45 78  owid = sqlite3Ex
2372f 70 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 54  pr(pParse->db, T
23730 4b 5f 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b  K_ROW, 0, 0, 0);
23731 0a 20 20 69 66 28 20 70 57 68 65 72 65 52 6f 77  .  if( pWhereRow
23732 69 64 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c  id == 0 ) goto l
23733 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
23734 75 70 5f 31 3b 0a 20 20 70 49 6e 43 6c 61 75 73  up_1;.  pInClaus
23735 65 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  e = sqlite3PExpr
23736 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20  (pParse, TK_IN, 
23737 70 57 68 65 72 65 52 6f 77 69 64 2c 20 30 2c 20  pWhereRowid, 0, 
23738 30 29 3b 0a 20 20 69 66 28 20 70 49 6e 43 6c 61  0);.  if( pInCla
23739 75 73 65 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20  use == 0 ) goto 
2373a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61  limit_where_clea
2373b 6e 75 70 5f 31 3b 0a 0a 20 20 70 49 6e 43 6c 61  nup_1;..  pInCla
2373c 75 73 65 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d  use->x.pSelect =
2373d 20 70 53 65 6c 65 63 74 3b 0a 20 20 70 49 6e 43   pSelect;.  pInC
2373e 6c 61 75 73 65 2d 3e 66 6c 61 67 73 20 7c 3d 20  lause->flags |= 
2373f 45 50 5f 78 49 73 53 65 6c 65 63 74 3b 0a 20 20  EP_xIsSelect;.  
23740 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
23741 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 49 6e  ight(pParse, pIn
23742 43 6c 61 75 73 65 29 3b 0a 20 20 72 65 74 75 72  Clause);.  retur
23743 6e 20 70 49 6e 43 6c 61 75 73 65 3b 0a 0a 20 20  n pInClause;..  
23744 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e  /* something wen
23745 74 20 77 72 6f 6e 67 2e 20 63 6c 65 61 6e 20 75  t wrong. clean u
23746 70 20 61 6e 79 74 68 69 6e 67 20 61 6c 6c 6f 63  p anything alloc
23747 61 74 65 64 2e 20 2a 2f 0a 6c 69 6d 69 74 5f 77  ated. */.limit_w
23748 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31 3a 0a  here_cleanup_1:.
23749 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2374a 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2374b 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 72 65  , pSelect);.  re
2374c 74 75 72 6e 20 30 3b 0a 0a 6c 69 6d 69 74 5f 77  turn 0;..limit_w
2374d 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3a 0a  here_cleanup_2:.
2374e 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2374f 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
23750 70 57 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74  pWhere);.  sqlit
23751 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
23752 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72  (pParse->db, pOr
23753 64 65 72 42 79 29 3b 0a 20 20 73 71 6c 69 74 65  derBy);.  sqlite
23754 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72  3ExprDelete(pPar
23755 73 65 2d 3e 64 62 2c 20 70 4c 69 6d 69 74 29 3b  se->db, pLimit);
23756 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
23757 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
23758 20 70 4f 66 66 73 65 74 29 3b 0a 20 20 72 65 74   pOffset);.  ret
23759 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 0;.}.#endif 
2375a 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  /* defined(SQLIT
2375b 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f  E_ENABLE_UPDATE_
2375c 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26  DELETE_LIMIT) &&
2375d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2375e 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20  _OMIT_SUBQUERY) 
2375f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  */../*.** Genera
23760 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 44 45  te code for a DE
23761 4c 45 54 45 20 46 52 4f 4d 20 73 74 61 74 65 6d  LETE FROM statem
23762 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44  ent..**.**     D
23763 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
23764 5f 77 78 79 7a 20 57 48 45 52 45 20 61 3c 35 20  _wxyz WHERE a<5 
23765 41 4e 44 20 62 20 4e 4f 54 20 4e 55 4c 4c 3b 0a  AND b NOT NULL;.
23766 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
23767 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20     \________/   
23768 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f      \___________
23769 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20  _____/.**       
2376a 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62 4c             pTabL
2376b 69 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ist             
2376c 20 70 57 68 65 72 65 0a 2a 2f 0a 53 51 4c 49 54   pWhere.*/.SQLIT
2376d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2376e 71 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d  qlite3DeleteFrom
2376f 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
23770 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
23771 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
23772 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
23773 54 61 62 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20  TabList,     /* 
23774 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77  The table from w
23775 68 69 63 68 20 77 65 20 73 68 6f 75 6c 64 20 64  hich we should d
23776 65 6c 65 74 65 20 74 68 69 6e 67 73 20 2a 2f 0a  elete things */.
23777 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20    Expr *pWhere  
23778 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
23779 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d  WHERE clause.  M
2377a 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 29 7b  ay be null */.){
2377b 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20  .  Vdbe *v;     
2377c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2377d 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73   virtual databas
2377e 65 20 65 6e 67 69 6e 65 20 2a 2f 0a 20 20 54 61  e engine */.  Ta
2377f 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
23780 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
23781 65 20 66 72 6f 6d 20 77 68 69 63 68 20 72 65 63  e from which rec
23782 6f 72 64 73 20 77 69 6c 6c 20 62 65 20 64 65 6c  ords will be del
23783 65 74 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  eted */.  const 
23784 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20  char *zDb;      
23785 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61   /* Name of data
23786 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61  base holding pTa
23787 62 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 2c 20  b */.  int end, 
23788 61 64 64 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a  addr = 0;     /*
23789 20 41 20 63 6f 75 70 6c 65 20 61 64 64 72 65 73   A couple addres
2378a 73 65 73 20 6f 66 20 67 65 6e 65 72 61 74 65 64  ses of generated
2378b 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69   code */.  int i
2378c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2378d 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2378e 72 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f  r */.  WhereInfo
2378f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a   *pWInfo;     /*
23790 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
23791 75 74 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  ut the WHERE cla
23792 75 73 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  use */.  Index *
23793 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  pIdx;           
23794 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
23795 76 65 72 20 69 6e 64 69 63 65 73 20 6f 66 20 74  ver indices of t
23796 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
23797 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20 20  t iCur;         
23798 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72       /* VDBE Cur
23799 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70  sor number for p
2379a 54 61 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  Tab */.  sqlite3
2379b 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
2379c 2f 2a 20 4d 61 69 6e 20 64 61 74 61 62 61 73 65  /* Main database
2379d 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
2379e 41 75 74 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e  AuthContext sCon
2379f 74 65 78 74 3b 20 20 2f 2a 20 41 75 74 68 6f 72  text;  /* Author
237a0 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20  ization context 
237a1 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64 78 20  */.  int oldIdx 
237a2 3d 20 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 43  = -1;       /* C
237a3 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 4f 4c  ursor for the OL
237a4 44 20 74 61 62 6c 65 20 6f 66 20 41 46 54 45 52  D table of AFTER
237a5 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 4e   triggers */.  N
237a6 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20  ameContext sNC; 
237a7 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f        /* Name co
237a8 6e 74 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65  ntext to resolve
237a9 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
237aa 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
237ab 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
237ac 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a  atabase number *
237ad 2f 0a 20 20 69 6e 74 20 6d 65 6d 43 6e 74 20 3d  /.  int memCnt =
237ae 20 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65   -1;       /* Me
237af 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66  mory cell used f
237b0 6f 72 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 69  or change counti
237b1 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 72 63 61 75  ng */.  int rcau
237b2 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  th;            /
237b3 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64  * Value returned
237b4 20 62 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   by authorizatio
237b5 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 23  n callback */..#
237b6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
237b7 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74  IT_TRIGGER.  int
237b8 20 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20   isView;        
237b9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
237ba 65 20 69 66 20 61 74 74 65 6d 70 74 69 6e 67 20  e if attempting 
237bb 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 61  to delete from a
237bc 20 76 69 65 77 20 2a 2f 0a 20 20 54 72 69 67 67   view */.  Trigg
237bd 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20  er *pTrigger;   
237be 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
237bf 6f 66 20 74 61 62 6c 65 20 74 72 69 67 67 65 72  of table trigger
237c0 73 2c 20 69 66 20 72 65 71 75 69 72 65 64 20 2a  s, if required *
237c1 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69  /.#endif.  int i
237c2 42 65 67 69 6e 41 66 74 65 72 54 72 69 67 67 65  BeginAfterTrigge
237c3 72 20 3d 20 30 3b 20 20 2f 2a 20 41 64 64 72 65  r = 0;  /* Addre
237c4 73 73 20 6f 66 20 61 66 74 65 72 20 74 72 69 67  ss of after trig
237c5 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  ger program */. 
237c6 20 69 6e 74 20 69 45 6e 64 41 66 74 65 72 54 72   int iEndAfterTr
237c7 69 67 67 65 72 20 3d 20 30 3b 20 20 20 20 2f 2a  igger = 0;    /*
237c8 20 45 78 69 74 20 6f 66 20 61 66 74 65 72 20 74   Exit of after t
237c9 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a  rigger program *
237ca 2f 0a 20 20 69 6e 74 20 69 42 65 67 69 6e 42 65  /.  int iBeginBe
237cb 66 6f 72 65 54 72 69 67 67 65 72 20 3d 20 30 3b  foreTrigger = 0;
237cc 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 62   /* Address of b
237cd 65 66 6f 72 65 20 74 72 69 67 67 65 72 20 70 72  efore trigger pr
237ce 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69  ogram */.  int i
237cf 45 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72  EndBeforeTrigger
237d0 20 3d 20 30 3b 20 20 20 2f 2a 20 45 78 69 74 20   = 0;   /* Exit 
237d1 6f 66 20 62 65 66 6f 72 65 20 74 72 69 67 67 65  of before trigge
237d2 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 75  r program */.  u
237d3 33 32 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20  32 old_col_mask 
237d4 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  = 0;        /* M
237d5 61 73 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c  ask of OLD.* col
237d6 75 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 0a  umns in use */..
237d7 20 20 73 43 6f 6e 74 65 78 74 2e 70 50 61 72 73    sContext.pPars
237d8 65 20 3d 20 30 3b 0a 20 20 64 62 20 3d 20 70 50  e = 0;.  db = pP
237d9 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
237da 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
237db 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
237dc 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
237dd 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
237de 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
237df 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pTabList->nSrc==
237e0 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74  1 );..  /* Locat
237e1 65 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63  e the table whic
237e2 68 20 77 65 20 77 61 6e 74 20 74 6f 20 64 65 6c  h we want to del
237e3 65 74 65 2e 20 20 54 68 69 73 20 74 61 62 6c 65  ete.  This table
237e4 20 68 61 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20   has to be.  ** 
237e5 70 75 74 20 69 6e 20 61 6e 20 53 72 63 4c 69 73  put in an SrcLis
237e6 74 20 73 74 72 75 63 74 75 72 65 20 62 65 63 61  t structure beca
237e7 75 73 65 20 73 6f 6d 65 20 6f 66 20 74 68 65 20  use some of the 
237e8 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 0a 20  subroutines we. 
237e9 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c   ** will be call
237ea 69 6e 67 20 61 72 65 20 64 65 73 69 67 6e 65 64  ing are designed
237eb 20 74 6f 20 77 6f 72 6b 20 77 69 74 68 20 6d 75   to work with mu
237ec 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 6e  ltiple tables an
237ed 64 20 65 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e  d expect.  ** an
237ee 20 53 72 63 4c 69 73 74 2a 20 70 61 72 61 6d 65   SrcList* parame
237ef 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 6a  ter instead of j
237f0 75 73 74 20 61 20 54 61 62 6c 65 2a 20 70 61 72  ust a Table* par
237f1 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70  ameter..  */.  p
237f2 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
237f3 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
237f4 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  e, pTabList);.  
237f5 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 20 67  if( pTab==0 )  g
237f6 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
237f7 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46  cleanup;..  /* F
237f8 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
237f9 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72  have any trigger
237fa 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62  s and if the tab
237fb 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65  le being.  ** de
237fc 6c 65 74 65 64 20 66 72 6f 6d 20 69 73 20 61 20  leted from is a 
237fd 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  view.  */.#ifnde
237fe 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
237ff 49 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72  IGGER.  pTrigger
23800 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
23801 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
23802 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c  pTab, TK_DELETE,
23803 20 30 2c 20 30 29 3b 0a 20 20 69 73 56 69 65 77   0, 0);.  isView
23804 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74   = pTab->pSelect
23805 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  !=0;.#else.# def
23806 69 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a 23  ine pTrigger 0.#
23807 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
23808 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
23809 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
2380a 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23  # undef isView.#
2380b 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
2380c 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73  .#endif..  if( s
2380d 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79  qlite3IsReadOnly
2380e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 28  (pParse, pTab, (
2380f 70 54 72 69 67 67 65 72 3f 31 3a 30 29 29 20 29  pTrigger?1:0)) )
23810 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74  {.    goto delet
23811 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
23812 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69    }.  iDb = sqli
23813 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
23814 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
23815 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ma);.  assert( i
23816 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
23817 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
23818 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 61 75  b].zName;.  rcau
23819 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68  th = sqlite3Auth
2381a 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
2381b 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 70 54 61  LITE_DELETE, pTa
2381c 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62  b->zName, 0, zDb
2381d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 61  );.  assert( rca
2381e 75 74 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  uth==SQLITE_OK |
2381f 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45  | rcauth==SQLITE
23820 5f 44 45 4e 59 20 7c 7c 20 72 63 61 75 74 68 3d  _DENY || rcauth=
23821 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29  =SQLITE_IGNORE )
23822 3b 0a 20 20 69 66 28 20 72 63 61 75 74 68 3d 3d  ;.  if( rcauth==
23823 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20  SQLITE_DENY ){. 
23824 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66     goto delete_f
23825 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  rom_cleanup;.  }
23826 0a 20 20 61 73 73 65 72 74 28 21 69 73 56 69 65  .  assert(!isVie
23827 77 20 7c 7c 20 70 54 72 69 67 67 65 72 29 3b 0a  w || pTrigger);.
23828 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73  .  /* If pTab is
23829 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20   really a view, 
2382a 6d 61 6b 65 20 73 75 72 65 20 69 74 20 68 61 73  make sure it has
2382b 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
2382c 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  d..  */.  if( sq
2382d 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
2382e 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
2382f 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74  pTab) ){.    got
23830 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c  o delete_from_cl
23831 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
23832 20 41 6c 6c 6f 63 61 74 65 20 61 20 63 75 72 73   Allocate a curs
23833 6f 72 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  or used to store
23834 20 74 68 65 20 6f 6c 64 2e 2a 20 64 61 74 61 20   the old.* data 
23835 66 6f 72 20 61 20 74 72 69 67 67 65 72 2e 0a 20  for a trigger.. 
23836 20 2a 2f 0a 20 20 69 66 28 20 70 54 72 69 67 67   */.  if( pTrigg
23837 65 72 20 29 7b 20 0a 20 20 20 20 6f 6c 64 49 64  er ){ .    oldId
23838 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  x = pParse->nTab
23839 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73  ++;.  }..  /* As
2383a 73 69 67 6e 20 20 63 75 72 73 6f 72 20 6e 75 6d  sign  cursor num
2383b 62 65 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ber to the table
2383c 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 69 6e 64   and all its ind
2383d 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  ices..  */.  ass
2383e 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e  ert( pTabList->n
2383f 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69 43 75 72  Src==1 );.  iCur
23840 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30   = pTabList->a[0
23841 5d 2e 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72  ].iCursor = pPar
23842 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f  se->nTab++;.  fo
23843 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
23844 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
23845 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
23846 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b    pParse->nTab++
23847 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 61 72  ;.  }..  /* Star
23848 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e 74 65  t the view conte
23849 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  xt.  */.  if( is
2384a 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69  View ){.    sqli
2384b 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
2384c 73 68 28 70 50 61 72 73 65 2c 20 26 73 43 6f 6e  sh(pParse, &sCon
2384d 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  text, pTab->zNam
2384e 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65  e);.  }..  /* Be
2384f 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63  gin generating c
23850 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  ode..  */.  v = 
23851 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
23852 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d  Parse);.  if( v=
23853 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64  =0 ){.    goto d
23854 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
23855 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  up;.  }.  if( pP
23856 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20  arse->nested==0 
23857 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75  ) sqlite3VdbeCou
23858 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20 20  ntChanges(v);.  
23859 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
2385a 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
2385b 65 2c 20 28 70 54 72 69 67 67 65 72 3f 31 3a 30  e, (pTrigger?1:0
2385c 29 2c 20 69 44 62 29 3b 0a 0a 20 20 69 66 28 20  ), iDb);..  if( 
2385d 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
2385e 69 6e 74 20 6f 72 63 6f 6e 66 20 3d 20 28 28 70  int orconf = ((p
2385f 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
23860 29 3f 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74  )?pParse->trigSt
23861 61 63 6b 2d 3e 6f 72 63 6f 6e 66 3a 4f 45 5f 44  ack->orconf:OE_D
23862 65 66 61 75 6c 74 29 3b 0a 20 20 20 20 69 6e 74  efault);.    int
23863 20 69 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33   iGoto = sqlite3
23864 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
23865 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 61 64 64 72  _Goto);.    addr
23866 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
23867 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20  keLabel(v);..   
23868 20 69 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69   iBeginBeforeTri
23869 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64  gger = sqlite3Vd
2386a 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2386b 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
2386c 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
2386d 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  r(pParse, pTrigg
2386e 65 72 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30  er, TK_DELETE, 0
2386f 2c 20 0a 20 20 20 20 20 20 20 20 54 52 49 47 47  , .        TRIGG
23870 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c  ER_BEFORE, pTab,
23871 20 2d 31 2c 20 6f 6c 64 49 64 78 2c 20 6f 72 63   -1, oldIdx, orc
23872 6f 6e 66 2c 20 61 64 64 72 2c 20 26 6f 6c 64 5f  onf, addr, &old_
23873 63 6f 6c 5f 6d 61 73 6b 2c 20 30 29 3b 0a 20 20  col_mask, 0);.  
23874 20 20 69 45 6e 64 42 65 66 6f 72 65 54 72 69 67    iEndBeforeTrig
23875 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ger = sqlite3Vdb
23876 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
23877 74 6f 29 3b 0a 0a 20 20 20 20 69 42 65 67 69 6e  to);..    iBegin
23878 41 66 74 65 72 54 72 69 67 67 65 72 20 3d 20 73  AfterTrigger = s
23879 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2387a 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 28 76  tAddr(v);.    (v
2387b 6f 69 64 29 73 71 6c 69 74 65 33 43 6f 64 65 52  oid)sqlite3CodeR
2387c 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
2387d 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 44  , pTrigger, TK_D
2387e 45 4c 45 54 45 2c 20 30 2c 20 0a 20 20 20 20 20  ELETE, 0, .     
2387f 20 20 20 54 52 49 47 47 45 52 5f 41 46 54 45 52     TRIGGER_AFTER
23880 2c 20 70 54 61 62 2c 20 2d 31 2c 20 6f 6c 64 49  , pTab, -1, oldI
23881 64 78 2c 20 6f 72 63 6f 6e 66 2c 20 61 64 64 72  dx, orconf, addr
23882 2c 20 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 2c  , &old_col_mask,
23883 20 30 29 3b 0a 20 20 20 20 69 45 6e 64 41 66 74   0);.    iEndAft
23884 65 72 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69  erTrigger = sqli
23885 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
23886 20 4f 50 5f 47 6f 74 6f 29 3b 0a 0a 20 20 20 20   OP_Goto);..    
23887 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
23888 65 72 65 28 76 2c 20 69 47 6f 74 6f 29 3b 0a 20  ere(v, iGoto);. 
23889 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61   }..  /* If we a
2388a 72 65 20 74 72 79 69 6e 67 20 74 6f 20 64 65 6c  re trying to del
2388b 65 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77 2c  ete from a view,
2388c 20 72 65 61 6c 69 7a 65 20 74 68 61 74 20 76 69   realize that vi
2388d 65 77 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 20 65  ew into.  ** a e
2388e 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a  phemeral table..
2388f 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65    */.#if !define
23890 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
23891 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  EW) && !defined(
23892 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
23893 47 45 52 29 0a 20 20 69 66 28 20 69 73 56 69 65  GER).  if( isVie
23894 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  w ){.    sqlite3
23895 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28  MaterializeView(
23896 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 57  pParse, pTab, pW
23897 68 65 72 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d  here, iCur);.  }
23898 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65  .#endif..  /* Re
23899 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e  solve the column
2389a 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57 48   names in the WH
2389b 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f  ERE clause..  */
2389c 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20  .  memset(&sNC, 
2389d 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b  0, sizeof(sNC));
2389e 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20  .  sNC.pParse = 
2389f 70 50 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53  pParse;.  sNC.pS
238a0 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73  rcList = pTabLis
238a1 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  t;.  if( sqlite3
238a2 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
238a3 28 26 73 4e 43 2c 20 70 57 68 65 72 65 29 20 29  (&sNC, pWhere) )
238a4 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74  {.    goto delet
238a5 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
238a6 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61    }..  /* Initia
238a7 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 65 72  lize the counter
238a8 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
238a9 66 20 72 6f 77 73 20 64 65 6c 65 74 65 64 2c 20  f rows deleted, 
238aa 69 66 0a 20 20 2a 2a 20 77 65 20 61 72 65 20 63  if.  ** we are c
238ab 6f 75 6e 74 69 6e 67 20 72 6f 77 73 2e 0a 20 20  ounting rows..  
238ac 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  */.  if( db->fla
238ad 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e  gs & SQLITE_Coun
238ae 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 6d 65 6d  tRows ){.    mem
238af 43 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Cnt = ++pParse->
238b0 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
238b1 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
238b2 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d 65  P_Integer, 0, me
238b3 6d 43 6e 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e  mCnt);.  }..#ifn
238b4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
238b5 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d 49 5a  TRUNCATE_OPTIMIZ
238b6 41 54 49 4f 4e 0a 20 20 2f 2a 20 53 70 65 63 69  ATION.  /* Speci
238b7 61 6c 20 63 61 73 65 3a 20 41 20 44 45 4c 45 54  al case: A DELET
238b8 45 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 52  E without a WHER
238b9 45 20 63 6c 61 75 73 65 20 64 65 6c 65 74 65 73  E clause deletes
238ba 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a   everything..  *
238bb 2a 20 49 74 20 69 73 20 65 61 73 69 65 72 20 6a  * It is easier j
238bc 75 73 74 20 74 6f 20 65 72 61 73 65 20 74 68 65  ust to erase the
238bd 20 77 68 6f 6c 65 20 74 61 62 6c 65 2e 20 20 4e   whole table.  N
238be 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 68  ote, however, th
238bf 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65 61  at.  ** this mea
238c0 6e 73 20 74 68 61 74 20 74 68 65 20 72 6f 77 20  ns that the row 
238c1 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 77 69 6c  change count wil
238c2 6c 20 62 65 20 69 6e 63 6f 72 72 65 63 74 2e 0a  l be incorrect..
238c3 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 61 75 74    */.  if( rcaut
238c4 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  h==SQLITE_OK && 
238c5 70 57 68 65 72 65 3d 3d 30 20 26 26 20 21 70 54  pWhere==0 && !pT
238c6 72 69 67 67 65 72 20 26 26 20 21 49 73 56 69 72  rigger && !IsVir
238c7 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
238c8 20 20 61 73 73 65 72 74 28 20 21 69 73 56 69 65    assert( !isVie
238c9 77 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  w );.    sqlite3
238ca 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
238cb 5f 43 6c 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e  _Clear, pTab->tn
238cc 75 6d 2c 20 69 44 62 2c 20 6d 65 6d 43 6e 74 2c  um, iDb, memCnt,
238cd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
238ce 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61         pTab->zNa
238cf 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  me, P4_STATIC);.
238d0 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
238d1 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
238d2 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
238d3 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t){.      assert
238d4 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d  ( pIdx->pSchema=
238d5 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
238d6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
238d7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
238d8 43 6c 65 61 72 2c 20 70 49 64 78 2d 3e 74 6e 75  Clear, pIdx->tnu
238d9 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 0a 20  m, iDb);.    }. 
238da 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a   }else.#endif /*
238db 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55   SQLITE_OMIT_TRU
238dc 4e 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49  NCATE_OPTIMIZATI
238dd 4f 4e 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 75  ON */.  /* The u
238de 73 75 61 6c 20 63 61 73 65 3a 20 54 68 65 72 65  sual case: There
238df 20 69 73 20 61 20 57 48 45 52 45 20 63 6c 61 75   is a WHERE clau
238e0 73 65 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f  se so we have to
238e1 20 73 63 61 6e 20 74 68 72 6f 75 67 68 0a 20 20   scan through.  
238e2 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  ** the table and
238e3 20 70 69 63 6b 20 77 68 69 63 68 20 72 65 63 6f   pick which reco
238e4 72 64 73 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20  rds to delete.. 
238e5 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20   */.  {.    int 
238e6 69 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73  iRowid = ++pPars
238e7 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 55  e->nMem;    /* U
238e8 73 65 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20  sed for storing 
238e9 72 6f 77 69 64 20 76 61 6c 75 65 73 2e 20 2a 2f  rowid values. */
238ea 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 53 65 74  .    int iRowSet
238eb 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
238ec 6d 3b 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72  m;   /* Register
238ed 20 66 6f 72 20 72 6f 77 73 65 74 20 6f 66 20 72   for rowset of r
238ee 6f 77 73 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  ows to delete */
238ef 0a 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69  .    int regRowi
238f0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
238f1 20 20 20 20 20 2f 2a 20 41 63 74 75 61 6c 20 72       /* Actual r
238f2 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69  egister containi
238f3 6e 67 20 72 6f 77 69 64 73 20 2a 2f 0a 0a 20 20  ng rowids */..  
238f4 20 20 2f 2a 20 43 6f 6c 6c 65 63 74 20 72 6f 77    /* Collect row
238f5 69 64 73 20 6f 66 20 65 76 65 72 79 20 72 6f 77  ids of every row
238f6 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a   to be deleted..
238f7 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
238f8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
238f9 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 6f 77  OP_Null, 0, iRow
238fa 53 65 74 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f  Set);.    pWInfo
238fb 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42   = sqlite3WhereB
238fc 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61  egin(pParse, pTa
238fd 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 30 2c  bList, pWhere,0,
238fe 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53  WHERE_DUPLICATES
238ff 5f 4f 4b 29 3b 0a 20 20 20 20 69 66 28 20 70 57  _OK);.    if( pW
23900 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 64  Info==0 ) goto d
23901 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
23902 75 70 3b 0a 20 20 20 20 72 65 67 52 6f 77 69 64  up;.    regRowid
23903 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
23904 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72  deGetColumn(pPar
23905 73 65 2c 20 70 54 61 62 2c 20 2d 31 2c 20 69 43  se, pTab, -1, iC
23906 75 72 2c 20 69 52 6f 77 69 64 2c 20 30 29 3b 0a  ur, iRowid, 0);.
23907 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23908 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53  ddOp2(v, OP_RowS
23909 65 74 41 64 64 2c 20 69 52 6f 77 53 65 74 2c 20  etAdd, iRowSet, 
2390a 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 69  regRowid);.    i
2390b 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  f( db->flags & S
2390c 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
2390d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2390e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2390f 5f 41 64 64 49 6d 6d 2c 20 6d 65 6d 43 6e 74 2c  _AddImm, memCnt,
23910 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73   1);.    }.    s
23911 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70  qlite3WhereEnd(p
23912 57 49 6e 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20  WInfo);..    /* 
23913 4f 70 65 6e 20 74 68 65 20 70 73 65 75 64 6f 2d  Open the pseudo-
23914 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 73 74  table used to st
23915 6f 72 65 20 4f 4c 44 20 69 66 20 74 68 65 72 65  ore OLD if there
23916 20 61 72 65 20 74 72 69 67 67 65 72 73 2e 0a 20   are triggers.. 
23917 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54     */.    if( pT
23918 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20  rigger ){.      
23919 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2391a 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75  3(v, OP_OpenPseu
2391b 64 6f 2c 20 6f 6c 64 49 64 78 2c 20 30 2c 20 70  do, oldIdx, 0, p
2391c 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20  Tab->nCol);.    
2391d 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  }..    /* Delete
2391e 20 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f 73   every item whos
2391f 65 20 6b 65 79 20 77 61 73 20 77 72 69 74 74 65  e key was writte
23920 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64 75  n to the list du
23921 72 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20  ring the.    ** 
23922 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20 20  database scan.  
23923 57 65 20 68 61 76 65 20 74 6f 20 64 65 6c 65 74  We have to delet
23924 65 20 69 74 65 6d 73 20 61 66 74 65 72 20 74 68  e items after th
23925 65 20 73 63 61 6e 20 69 73 20 63 6f 6d 70 6c 65  e scan is comple
23926 74 65 0a 20 20 20 20 2a 2a 20 62 65 63 61 75 73  te.    ** becaus
23927 65 20 64 65 6c 65 74 69 6e 67 20 61 6e 20 69 74  e deleting an it
23928 65 6d 20 63 61 6e 20 63 68 61 6e 67 65 20 74 68  em can change th
23929 65 20 73 63 61 6e 20 6f 72 64 65 72 2e 0a 20 20  e scan order..  
2392a 20 20 2a 2f 0a 20 20 20 20 65 6e 64 20 3d 20 73    */.    end = s
2392b 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
2392c 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 69 66 28  bel(v);..    if(
2392d 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20   !isView ){.    
2392e 20 20 2f 2a 20 4f 70 65 6e 20 63 75 72 73 6f 72    /* Open cursor
2392f 73 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  s for the table 
23930 77 65 20 61 72 65 20 64 65 6c 65 74 69 6e 67 20  we are deleting 
23931 66 72 6f 6d 20 61 6e 64 20 0a 20 20 20 20 20 20  from and .      
23932 2a 2a 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63  ** all its indic
23933 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  es..      */.   
23934 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61     sqlite3OpenTa
23935 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 50  bleAndIndices(pP
23936 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72  arse, pTab, iCur
23937 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b  , OP_OpenWrite);
23938 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
23939 68 69 73 20 69 73 20 74 68 65 20 62 65 67 69 6e  his is the begin
2393a 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 65 6c 65  ning of the dele
2393b 74 65 20 6c 6f 6f 70 2e 20 49 66 20 61 20 74 72  te loop. If a tr
2393c 69 67 67 65 72 20 65 6e 63 6f 75 6e 74 65 72 73  igger encounters
2393d 0a 20 20 20 20 2a 2a 20 61 6e 20 49 47 4e 4f 52  .    ** an IGNOR
2393e 45 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 69 74  E constraint, it
2393f 20 6a 75 6d 70 73 20 62 61 63 6b 20 74 6f 20 68   jumps back to h
23940 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ere..    */.    
23941 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a  if( pTrigger ){.
23942 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23943 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
23944 20 61 64 64 72 29 3b 0a 20 20 20 20 7d 0a 20 20   addr);.    }.  
23945 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
23946 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
23947 5f 52 6f 77 53 65 74 52 65 61 64 2c 20 69 52 6f  _RowSetRead, iRo
23948 77 53 65 74 2c 20 65 6e 64 2c 20 69 52 6f 77 69  wSet, end, iRowi
23949 64 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 54 72  d);..    if( pTr
2394a 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20 69  igger ){.      i
2394b 6e 74 20 69 44 61 74 61 20 3d 20 2b 2b 70 50 61  nt iData = ++pPa
2394c 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 2f 2a 20  rse->nMem;   /* 
2394d 46 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f 77 20  For storing row 
2394e 64 61 74 61 20 6f 66 20 4f 4c 44 20 74 61 62 6c  data of OLD tabl
2394f 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49  e */..      /* I
23950 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20  f the record is 
23951 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 65 73 65 6e  no longer presen
23952 74 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  t in the table, 
23953 6a 75 6d 70 20 74 6f 20 74 68 65 0a 20 20 20 20  jump to the.    
23954 20 20 2a 2a 20 6e 65 78 74 20 69 74 65 72 61 74    ** next iterat
23955 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  ion of the loop 
23956 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f 6e 74  through the cont
23957 65 6e 74 73 20 6f 66 20 74 68 65 20 66 69 66 6f  ents of the fifo
23958 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
23959 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2395a 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
2395b 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20  ts, iCur, addr, 
2395c 69 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 20 20  iRowid);..      
2395d 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  /* Populate the 
2395e 4f 4c 44 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62  OLD.* pseudo-tab
2395f 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  le */.      if( 
23960 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 29 7b 0a  old_col_mask ){.
23961 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
23962 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
23963 52 6f 77 44 61 74 61 2c 20 69 43 75 72 2c 20 69  RowData, iCur, i
23964 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Data);.      }el
23965 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
23966 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23967 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 44 61   OP_Null, 0, iDa
23968 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ta);.      }.   
23969 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2396a 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
2396b 74 2c 20 6f 6c 64 49 64 78 2c 20 69 44 61 74 61  t, oldIdx, iData
2396c 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20  , iRowid);..    
2396d 20 20 2f 2a 20 4a 75 6d 70 20 62 61 63 6b 20 61    /* Jump back a
2396e 6e 64 20 72 75 6e 20 74 68 65 20 42 45 46 4f 52  nd run the BEFOR
2396f 45 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20  E triggers */.  
23970 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23971 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
23972 2c 20 30 2c 20 69 42 65 67 69 6e 42 65 66 6f 72  , 0, iBeginBefor
23973 65 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20  eTrigger);.     
23974 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
23975 48 65 72 65 28 76 2c 20 69 45 6e 64 42 65 66 6f  Here(v, iEndBefo
23976 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20  reTrigger);.    
23977 7d 0a 0a 20 20 20 20 69 66 28 20 21 69 73 56 69  }..    if( !isVi
23978 65 77 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 44  ew ){.      /* D
23979 65 6c 65 74 65 20 74 68 65 20 72 6f 77 20 2a 2f  elete the row */
2397a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2397b 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2397c 45 0a 20 20 20 20 20 20 69 66 28 20 49 73 56 69  E.      if( IsVi
2397d 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
2397e 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
2397f 72 20 2a 70 56 74 61 62 20 3d 20 28 63 6f 6e 73  r *pVtab = (cons
23980 74 20 63 68 61 72 20 2a 29 70 54 61 62 2d 3e 70  t char *)pTab->p
23981 56 74 61 62 3b 0a 20 20 20 20 20 20 20 20 73 71  Vtab;.        sq
23982 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69  lite3VtabMakeWri
23983 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  table(pParse, pT
23984 61 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ab);.        sql
23985 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
23986 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20 30 2c  , OP_VUpdate, 0,
23987 20 31 2c 20 69 52 6f 77 69 64 2c 20 70 56 74 61   1, iRowid, pVta
23988 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20  b, P4_VTAB);.   
23989 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
2398a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
2398b 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
2398c 6f 77 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c  owDelete(pParse,
2398d 20 70 54 61 62 2c 20 69 43 75 72 2c 20 69 52 6f   pTab, iCur, iRo
2398e 77 69 64 2c 20 70 50 61 72 73 65 2d 3e 6e 65 73  wid, pParse->nes
2398f 74 65 64 3d 3d 30 29 3b 0a 20 20 20 20 20 20 7d  ted==0);.      }
23990 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
23991 66 20 74 68 65 72 65 20 61 72 65 20 72 6f 77 20  f there are row 
23992 74 72 69 67 67 65 72 73 2c 20 63 6c 6f 73 65 20  triggers, close 
23993 61 6c 6c 20 63 75 72 73 6f 72 73 20 74 68 65 6e  all cursors then
23994 20 69 6e 76 6f 6b 65 0a 20 20 20 20 2a 2a 20 74   invoke.    ** t
23995 68 65 20 41 46 54 45 52 20 74 72 69 67 67 65 72  he AFTER trigger
23996 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  s.    */.    if(
23997 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
23998 20 20 20 2f 2a 20 4a 75 6d 70 20 62 61 63 6b 20     /* Jump back 
23999 61 6e 64 20 72 75 6e 20 74 68 65 20 41 46 54 45  and run the AFTE
2399a 52 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20  R triggers */.  
2399b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2399c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
2399d 2c 20 30 2c 20 69 42 65 67 69 6e 41 66 74 65 72  , 0, iBeginAfter
2399e 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20  Trigger);.      
2399f 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
239a0 65 72 65 28 76 2c 20 69 45 6e 64 41 66 74 65 72  ere(v, iEndAfter
239a1 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 7d 0a  Trigger);.    }.
239a2 0a 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74  .    /* End of t
239a3 68 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70 20 2a  he delete loop *
239a4 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
239a5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
239a6 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20  to, 0, addr);.  
239a7 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
239a8 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64  olveLabel(v, end
239a9 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65  );..    /* Close
239aa 20 74 68 65 20 63 75 72 73 6f 72 73 20 61 66 74   the cursors aft
239ab 65 72 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74  er the loop if t
239ac 68 65 72 65 20 61 72 65 20 6e 6f 20 72 6f 77 20  here are no row 
239ad 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20  triggers */.    
239ae 69 66 28 20 21 69 73 56 69 65 77 20 20 26 26 20  if( !isView  && 
239af 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
239b0 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   ){.      for(i=
239b1 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  1, pIdx=pTab->pI
239b2 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c  ndex; pIdx; i++,
239b3 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
239b4 74 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t){.        sqli
239b5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
239b6 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 20   OP_Close, iCur 
239b7 2b 20 69 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 29  + i, pIdx->tnum)
239b8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
239b9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
239ba 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  1(v, OP_Close, i
239bb 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Cur);.    }.  }.
239bc 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72  .  /*.  ** Retur
239bd 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
239be 72 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 64  rows that were d
239bf 65 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73 20  eleted. If this 
239c0 72 6f 75 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a  routine is .  **
239c1 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65   generating code
239c2 20 62 65 63 61 75 73 65 20 6f 66 20 61 20 63 61   because of a ca
239c3 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73  ll to sqlite3Nes
239c4 74 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e  tedParse(), do n
239c5 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74  ot.  ** invoke t
239c6 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
239c7 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tion..  */.  if(
239c8 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c   db->flags & SQL
239c9 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26  ITE_CountRows &&
239ca 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
239cb 3d 30 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74  =0 && !pParse->t
239cc 72 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20  rigStack ){.    
239cd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
239ce 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
239cf 77 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20  w, memCnt, 1);. 
239d0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
239d1 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
239d2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
239d3 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
239d4 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72  COLNAME_NAME, "r
239d5 6f 77 73 20 64 65 6c 65 74 65 64 22 2c 20 53 51  ows deleted", SQ
239d6 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
239d7 7d 0a 0a 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63  }..delete_from_c
239d8 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65  leanup:.  sqlite
239d9 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28  3AuthContextPop(
239da 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71  &sContext);.  sq
239db 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
239dc 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73 74 29  te(db, pTabList)
239dd 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
239de 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72 65  elete(db, pWhere
239df 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );.  return;.}..
239e0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
239e1 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56 44 42  ne generates VDB
239e2 45 20 63 6f 64 65 20 74 68 61 74 20 63 61 75 73  E code that caus
239e3 65 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  es a single row 
239e4 6f 66 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 74  of a.** single t
239e5 61 62 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 74  able to be delet
239e6 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44  ed..**.** The VD
239e7 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61 20  BE must be in a 
239e8 70 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 65  particular state
239e9 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
239ea 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ne is called..**
239eb 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20 72   These are the r
239ec 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a  equirements:.**.
239ed 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64 2f  **   1.  A read/
239ee 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69  write cursor poi
239ef 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74  nting to pTab, t
239f0 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
239f1 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20  ing the row.**  
239f2 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65 74       to be delet
239f3 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ed, must be open
239f4 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75 6d  ed as cursor num
239f5 62 65 72 20 22 62 61 73 65 22 2e 0a 2a 2a 0a 2a  ber "base"..**.*
239f6 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69  *   2.  Read/wri
239f7 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61  te cursors for a
239f8 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54  ll indices of pT
239f9 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  ab must be open 
239fa 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73  as.**       curs
239fb 6f 72 20 6e 75 6d 62 65 72 20 62 61 73 65 2b 69  or number base+i
239fc 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e   for the i-th in
239fd 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20  dex..**.**   3. 
239fe 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   The record numb
239ff 65 72 20 6f 66 20 74 68 65 20 72 6f 77 20 74 6f  er of the row to
23a00 20 62 65 20 64 65 6c 65 74 65 64 20 6d 75 73 74   be deleted must
23a01 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a   be stored in.**
23a02 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 63 65         memory ce
23a03 6c 6c 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a  ll iRowid..**.**
23a04 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 6f   This routine po
23a05 70 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  ps the top of th
23a06 65 20 73 74 61 63 6b 20 74 6f 20 72 65 6d 6f 76  e stack to remov
23a07 65 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  e the record num
23a08 62 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20  ber.** and then 
23a09 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
23a0a 6f 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74 68  o remove both th
23a0b 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 61  e table record a
23a0c 6e 64 20 61 6c 6c 20 69 6e 64 65 78 0a 2a 2a 20  nd all index.** 
23a0d 65 6e 74 72 69 65 73 20 74 68 61 74 20 70 6f 69  entries that poi
23a0e 6e 74 20 74 6f 20 74 68 61 74 20 72 65 63 6f 72  nt to that recor
23a0f 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
23a10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23a11 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
23a12 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  te(.  Parse *pPa
23a13 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
23a14 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
23a15 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
23a16 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e      /* Table con
23a17 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20  taining the row 
23a18 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f  to be deleted */
23a19 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20  .  int iCur,    
23a1a 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
23a1b 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74  number for the t
23a1c 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52  able */.  int iR
23a1d 6f 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a 20  owid,        /* 
23a1e 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74  Memory cell that
23a1f 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f   contains the ro
23a20 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  wid to delete */
23a21 0a 20 20 69 6e 74 20 63 6f 75 6e 74 20 20 20 20  .  int count    
23a22 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65        /* Increme
23a23 6e 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  nt the row chang
23a24 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 29 7b 0a  e counter */.){.
23a25 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 56 64    int addr;.  Vd
23a26 62 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20 70 50  be *v;..  v = pP
23a27 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61  arse->pVdbe;.  a
23a28 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
23a29 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
23a2a 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 30  tExists, iCur, 0
23a2b 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c  , iRowid);.  sql
23a2c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49  ite3GenerateRowI
23a2d 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72 73  ndexDelete(pPars
23a2e 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 30  e, pTab, iCur, 0
23a2f 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
23a30 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c  AddOp2(v, OP_Del
23a31 65 74 65 2c 20 69 43 75 72 2c 20 28 63 6f 75 6e  ete, iCur, (coun
23a32 74 3f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  t?OPFLAG_NCHANGE
23a33 3a 30 29 29 3b 0a 20 20 69 66 28 20 63 6f 75 6e  :0));.  if( coun
23a34 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
23a35 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
23a36 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  -1, pTab->zName,
23a37 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d   P4_STATIC);.  }
23a38 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
23a39 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
23a3a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
23a3b 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
23a3c 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20   VDBE code that 
23a3d 63 61 75 73 65 73 20 74 68 65 20 64 65 6c 65 74  causes the delet
23a3e 69 6f 6e 20 6f 66 20 61 6c 6c 0a 2a 2a 20 69 6e  ion of all.** in
23a3f 64 65 78 20 65 6e 74 72 69 65 73 20 61 73 73 6f  dex entries asso
23a40 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69  ciated with a si
23a41 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73 69  ngle row of a si
23a42 6e 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  ngle table..**.*
23a43 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73 74 20  * The VDBE must 
23a44 62 65 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c  be in a particul
23a45 61 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68  ar state when th
23a46 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
23a47 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61  lled..** These a
23a48 72 65 20 74 68 65 20 72 65 71 75 69 72 65 6d 65  re the requireme
23a49 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20  nts:.**.**   1. 
23a4a 20 41 20 72 65 61 64 2f 77 72 69 74 65 20 63 75   A read/write cu
23a4b 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f  rsor pointing to
23a4c 20 70 54 61 62 2c 20 74 68 65 20 74 61 62 6c 65   pTab, the table
23a4d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
23a4e 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20  row.**       to 
23a4f 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75 73 74  be deleted, must
23a50 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 63 75   be opened as cu
23a51 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 69 43 75  rsor number "iCu
23a52 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20  r"..**.**   2.  
23a53 52 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  Read/write curso
23a54 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63  rs for all indic
23a55 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74 20  es of pTab must 
23a56 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20 20 20  be open as.**   
23a57 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d 62 65      cursor numbe
23a58 72 20 69 43 75 72 2b 69 20 66 6f 72 20 74 68 65  r iCur+i for the
23a59 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a   i-th index..**.
23a5a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 22 69 43  **   3.  The "iC
23a5b 75 72 22 20 63 75 72 73 6f 72 20 6d 75 73 74 20  ur" cursor must 
23a5c 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  be pointing to t
23a5d 68 65 20 72 6f 77 20 74 68 61 74 20 69 73 20 74  he row that is t
23a5e 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 64 65  o be.**       de
23a5f 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  leted..*/.SQLITE
23a60 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23a61 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
23a62 49 6e 64 65 78 44 65 6c 65 74 65 28 0a 20 20 50  IndexDelete(.  P
23a63 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
23a64 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64    /* Parsing and
23a65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67   code generating
23a66 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
23a67 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
23a68 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69   /* Table contai
23a69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74 6f 20  ning the row to 
23a6a 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  be deleted */.  
23a6b 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20  int iCur,       
23a6c 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
23a6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ber for the tabl
23a6e 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67  e */.  int *aReg
23a6f 49 64 78 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c  Idx       /* Onl
23a70 79 20 64 65 6c 65 74 65 20 69 66 20 61 52 65 67  y delete if aReg
23a71 49 64 78 21 3d 30 20 26 26 20 61 52 65 67 49 64  Idx!=0 && aRegId
23a72 78 5b 69 5d 3e 30 20 2a 2f 0a 29 7b 0a 20 20 69  x[i]>0 */.){.  i
23a73 6e 74 20 69 3b 0a 20 20 49 6e 64 65 78 20 2a 70  nt i;.  Index *p
23a74 49 64 78 3b 0a 20 20 69 6e 74 20 72 31 3b 0a 0a  Idx;.  int r1;..
23a75 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d    for(i=1, pIdx=
23a76 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
23a77 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49  dx; i++, pIdx=pI
23a78 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
23a79 69 66 28 20 61 52 65 67 49 64 78 21 3d 30 20 26  if( aRegIdx!=0 &
23a7a 26 20 61 52 65 67 49 64 78 5b 69 2d 31 5d 3d 3d  & aRegIdx[i-1]==
23a7b 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
23a7c 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
23a7d 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70  nerateIndexKey(p
23a7e 50 61 72 73 65 2c 20 70 49 64 78 2c 20 69 43 75  Parse, pIdx, iCu
23a7f 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  r, 0, 0);.    sq
23a80 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
23a81 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f  pParse->pVdbe, O
23a82 50 5f 49 64 78 44 65 6c 65 74 65 2c 20 69 43 75  P_IdxDelete, iCu
23a83 72 2b 69 2c 20 72 31 2c 70 49 64 78 2d 3e 6e 43  r+i, r1,pIdx->nC
23a84 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 7d 0a 7d 0a  olumn+1);.  }.}.
23a85 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
23a86 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 61  code that will a
23a87 73 73 65 6d 62 6c 65 20 61 6e 20 69 6e 64 65 78  ssemble an index
23a88 20 6b 65 79 20 61 6e 64 20 70 75 74 20 69 74 20   key and put it 
23a89 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 72  in register.** r
23a8a 65 67 4f 75 74 2e 20 20 54 68 65 20 6b 65 79 20  egOut.  The key 
23a8b 77 69 74 68 20 62 65 20 66 6f 72 20 69 6e 64 65  with be for inde
23a8c 78 20 70 49 64 78 20 77 68 69 63 68 20 69 73 20  x pIdx which is 
23a8d 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54 61 62  an index on pTab
23a8e 2e 0a 2a 2a 20 69 43 75 72 20 69 73 20 74 68 65  ..** iCur is the
23a8f 20 69 6e 64 65 78 20 6f 66 20 61 20 63 75 72 73   index of a curs
23a90 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 70  or open on the p
23a91 54 61 62 20 74 61 62 6c 65 20 61 6e 64 20 70 6f  Tab table and po
23a92 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 74 68 65  inting to.** the
23a93 20 65 6e 74 72 79 20 74 68 61 74 20 6e 65 65 64   entry that need
23a94 73 20 69 6e 64 65 78 69 6e 67 2e 0a 2a 2a 0a 2a  s indexing..**.*
23a95 2a 20 52 65 74 75 72 6e 20 61 20 72 65 67 69 73  * Return a regis
23a96 74 65 72 20 6e 75 6d 62 65 72 20 77 68 69 63 68  ter number which
23a97 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e   is the first in
23a98 20 61 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 72   a block of.** r
23a99 65 67 69 73 74 65 72 73 20 74 68 61 74 20 68 6f  egisters that ho
23a9a 6c 64 73 20 74 68 65 20 65 6c 65 6d 65 6e 74 73  lds the elements
23a9b 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b 65   of the index ke
23a9c 79 2e 20 20 54 68 65 0a 2a 2a 20 62 6c 6f 63 6b  y.  The.** block
23a9d 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 68 61   of registers ha
23a9e 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64  s already been d
23a9f 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68  eallocated by th
23aa0 65 20 74 69 6d 65 0a 2a 2a 20 74 68 69 73 20 72  e time.** this r
23aa1 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a  outine returns..
23aa2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23aa3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e  E int sqlite3Gen
23aa4 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 0a 20  erateIndexKey(. 
23aa5 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
23aa6 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
23aa7 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 49 6e 64 65  ontext */.  Inde
23aa8 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 2f  x *pIdx,       /
23aa9 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20  * The index for 
23aaa 77 68 69 63 68 20 74 6f 20 67 65 6e 65 72 61 74  which to generat
23aab 65 20 61 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74  e a key */.  int
23aac 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20   iCur,          
23aad 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
23aae 20 66 6f 72 20 74 68 65 20 70 49 64 78 2d 3e 70   for the pIdx->p
23aaf 54 61 62 6c 65 20 74 61 62 6c 65 20 2a 2f 0a 20  Table table */. 
23ab0 20 69 6e 74 20 72 65 67 4f 75 74 2c 20 20 20 20   int regOut,    
23ab1 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
23ab2 20 6e 65 77 20 69 6e 64 65 78 20 6b 65 79 20 74   new index key t
23ab3 6f 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20  o this register 
23ab4 2a 2f 0a 20 20 69 6e 74 20 64 6f 4d 61 6b 65 52  */.  int doMakeR
23ab5 65 63 20 20 20 20 20 20 2f 2a 20 52 75 6e 20 74  ec      /* Run t
23ab6 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  he OP_MakeRecord
23ab7 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 66 20   instruction if 
23ab8 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  true */.){.  Vdb
23ab9 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
23aba 56 64 62 65 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20  Vdbe;.  int j;. 
23abb 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
23abc 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 69  Idx->pTable;.  i
23abd 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20 69 6e  nt regBase;.  in
23abe 74 20 6e 43 6f 6c 3b 0a 0a 20 20 6e 43 6f 6c 20  t nCol;..  nCol 
23abf 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  = pIdx->nColumn;
23ac0 0a 20 20 72 65 67 42 61 73 65 20 3d 20 73 71 6c  .  regBase = sql
23ac1 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
23ac2 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 2b 31 29  (pParse, nCol+1)
23ac3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
23ac4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
23ac5 64 2c 20 69 43 75 72 2c 20 72 65 67 42 61 73 65  d, iCur, regBase
23ac6 2b 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28 6a 3d  +nCol);.  for(j=
23ac7 30 3b 20 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  0; j<nCol; j++){
23ac8 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70  .    int idx = p
23ac9 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
23aca 3b 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d 70  ;.    if( idx==p
23acb 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
23acc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23acd 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
23ace 79 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f 6c 2c  y, regBase+nCol,
23acf 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20   regBase+j);.   
23ad0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
23ad1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
23ad2 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43  v, OP_Column, iC
23ad3 75 72 2c 20 69 64 78 2c 20 72 65 67 42 61 73 65  ur, idx, regBase
23ad4 2b 6a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  +j);.      sqlit
23ad5 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28  e3ColumnDefault(
23ad6 76 2c 20 70 54 61 62 2c 20 69 64 78 29 3b 0a 20  v, pTab, idx);. 
23ad7 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 64     }.  }.  if( d
23ad8 6f 4d 61 6b 65 52 65 63 20 29 7b 0a 20 20 20 20  oMakeRec ){.    
23ad9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23ada 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
23adb 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  rd, regBase, nCo
23adc 6c 2b 31 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20  l+1, regOut);.  
23add 20 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66    sqlite3IndexAf
23ade 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64  finityStr(v, pId
23adf 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  x);.    sqlite3E
23ae0 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
23ae1 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
23ae2 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b  egBase, nCol+1);
23ae3 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65  .  }.  sqlite3Re
23ae4 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
23ae5 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20  Parse, regBase, 
23ae6 6e 43 6f 6c 2b 31 29 3b 0a 20 20 72 65 74 75 72  nCol+1);.  retur
23ae7 6e 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a  n regBase;.}../*
23ae8 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56 69   Make sure "isVi
23ae9 65 77 22 20 67 65 74 73 20 75 6e 64 65 66 69 6e  ew" gets undefin
23aea 65 64 20 69 6e 20 63 61 73 65 20 74 68 69 73 20  ed in case this 
23aeb 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70 61 72  file becomes par
23aec 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c  t of.** the amal
23aed 67 61 6d 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68  gamation - so th
23aee 61 74 20 73 75 62 73 65 71 75 65 6e 74 20 66 69  at subsequent fi
23aef 6c 65 73 20 64 6f 20 6e 6f 74 20 73 65 65 20 69  les do not see i
23af0 73 56 69 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61  sView as a.** ma
23af1 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69  cro. */.#undef i
23af2 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  sView../********
23af3 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 65  ****** End of de
23af4 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lete.c *********
23af5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23af6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23af7 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
23af8 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
23af9 65 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e func.c *******
23afa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23afb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23afc 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
23afd 32 20 46 65 62 72 75 61 72 79 20 32 33 0a 2a 2a  2 February 23.**
23afe 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
23aff 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
23b00 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
23b01 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
23b02 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
23b03 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
23b04 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
23b05 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
23b06 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
23b07 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
23b08 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
23b09 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
23b0a 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
23b0b 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
23b0c 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
23b0d 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
23b0e 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
23b0f 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
23b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
23b14 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
23b15 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f  ns the C functio
23b16 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ns that implemen
23b17 74 20 76 61 72 69 6f 75 73 20 53 51 4c 0a 2a 2a  t various SQL.**
23b18 20 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 53 51   functions of SQ
23b19 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  Lite.  .**.** Th
23b1a 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20  ere is only one 
23b1b 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20  exported symbol 
23b1c 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74  in this file - t
23b1d 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73  he function.** s
23b1e 71 6c 69 74 65 52 65 67 69 73 74 65 72 42 75 69  qliteRegisterBui
23b1f 6c 64 69 6e 46 75 6e 63 74 69 6f 6e 73 28 29 20  ldinFunctions() 
23b20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74  found at the bot
23b21 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e  tom of the file.
23b22 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f  .** All other co
23b23 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70  de has file scop
23b24 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 75  e..**.** $Id: fu
23b25 6e 63 2e 63 2c 76 20 31 2e 32 33 34 20 32 30 30  nc.c,v 1.234 200
23b26 39 2f 30 34 2f 32 30 20 31 32 3a 30 37 3a 33 37  9/04/20 12:07:37
23b27 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
23b28 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
23b29 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
23b2a 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  on associated wi
23b2b 74 68 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  th a function..*
23b2c 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71  /.static CollSeq
23b2d 20 2a 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63   *sqlite3GetFunc
23b2e 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 5f  CollSeq(sqlite3_
23b2f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
23b30 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e 74  ){.  return cont
23b31 65 78 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  ext->pColl;.}../
23b32 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
23b33 69 6f 6e 20 6f 66 20 74 68 65 20 6e 6f 6e 2d 61  ion of the non-a
23b34 67 67 72 65 67 61 74 65 20 6d 69 6e 28 29 20 61  ggregate min() a
23b35 6e 64 20 6d 61 78 28 29 20 66 75 6e 63 74 69 6f  nd max() functio
23b36 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ns.*/.static voi
23b37 64 20 6d 69 6e 6d 61 78 46 75 6e 63 28 0a 20 20  d minmaxFunc(.  
23b38 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
23b39 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
23b3a 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
23b3b 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
23b3c 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d    int i;.  int m
23b3d 61 73 6b 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72  ask;    /* 0 for
23b3e 20 6d 69 6e 28 29 20 6f 72 20 30 78 66 66 66 66   min() or 0xffff
23b3f 66 66 66 66 20 66 6f 72 20 6d 61 78 28 29 20 2a  ffff for max() *
23b40 2f 0a 20 20 69 6e 74 20 69 42 65 73 74 3b 0a 20  /.  int iBest;. 
23b41 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
23b42 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
23b43 3e 31 20 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 73  >1 );.  mask = s
23b44 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
23b45 28 63 6f 6e 74 65 78 74 29 3d 3d 30 20 3f 20 30  (context)==0 ? 0
23b46 20 3a 20 2d 31 3b 0a 20 20 70 43 6f 6c 6c 20 3d   : -1;.  pColl =
23b47 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43   sqlite3GetFuncC
23b48 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b  ollSeq(context);
23b49 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 6c  .  assert( pColl
23b4a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 61   );.  assert( ma
23b4b 73 6b 3d 3d 2d 31 20 7c 7c 20 6d 61 73 6b 3d 3d  sk==-1 || mask==
23b4c 30 20 29 3b 0a 20 20 69 42 65 73 74 20 3d 20 30  0 );.  iBest = 0
23b4d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ;.  if( sqlite3_
23b4e 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
23b4f 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
23b50 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72   ) return;.  for
23b51 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b  (i=1; i<argc; i+
23b52 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  +){.    if( sqli
23b53 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
23b54 72 67 76 5b 69 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[i])==SQLITE_
23b55 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20  NULL ) return;. 
23b56 20 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 4d     if( (sqlite3M
23b57 65 6d 43 6f 6d 70 61 72 65 28 61 72 67 76 5b 69  emCompare(argv[i
23b58 42 65 73 74 5d 2c 20 61 72 67 76 5b 69 5d 2c 20  Best], argv[i], 
23b59 70 43 6f 6c 6c 29 5e 6d 61 73 6b 29 3e 3d 30 20  pColl)^mask)>=0 
23b5a 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ){.      testcas
23b5b 65 28 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20  e( mask==0 );.  
23b5c 20 20 20 20 69 42 65 73 74 20 3d 20 69 3b 0a 20      iBest = i;. 
23b5d 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
23b5e 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
23b5f 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 42  context, argv[iB
23b60 65 73 74 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  est]);.}../*.** 
23b61 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20  Return the type 
23b62 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e  of the argument.
23b63 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
23b64 74 79 70 65 6f 66 46 75 6e 63 28 0a 20 20 73 71  typeofFunc(.  sq
23b65 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
23b66 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
23b67 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
23b68 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
23b69 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
23b6a 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50   = 0;.  UNUSED_P
23b6b 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
23b6c 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c  );.  switch( sql
23b6d 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
23b6e 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20  argv[0]) ){.    
23b6f 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  case SQLITE_INTE
23b70 47 45 52 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65  GER: z = "intege
23b71 72 22 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 63  r"; break;.    c
23b72 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a  ase SQLITE_TEXT:
23b73 20 20 20 20 7a 20 3d 20 22 74 65 78 74 22 3b 20      z = "text"; 
23b74 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
23b75 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
23b76 20 20 20 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20     z = "real";  
23b77 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
23b78 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20  e SQLITE_BLOB:  
23b79 20 20 7a 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20    z = "blob";   
23b7a 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61   break;.    defa
23b7b 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  ult:            
23b7c 20 7a 20 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20   z = "null";    
23b7d 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c  break;.  }.  sql
23b7e 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
23b7f 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c  (context, z, -1,
23b80 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
23b81 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  .}.../*.** Imple
23b82 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
23b83 20 6c 65 6e 67 74 68 28 29 20 66 75 6e 63 74 69   length() functi
23b84 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  on.*/.static voi
23b85 64 20 6c 65 6e 67 74 68 46 75 6e 63 28 0a 20 20  d lengthFunc(.  
23b86 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
23b87 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
23b88 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
23b89 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
23b8a 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 61 73    int len;..  as
23b8b 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
23b8c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
23b8d 54 45 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69  TER(argc);.  swi
23b8e 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c  tch( sqlite3_val
23b8f 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
23b90 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
23b91 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61  ITE_BLOB:.    ca
23b92 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  se SQLITE_INTEGE
23b93 52 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  R:.    case SQLI
23b94 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20  TE_FLOAT: {.    
23b95 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23b96 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  _int(context, sq
23b97 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
23b98 73 28 61 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20  s(argv[0]));.   
23b99 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
23b9a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
23b9b 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f  TEXT: {.      co
23b9c 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
23b9d 72 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76  r *z = sqlite3_v
23b9e 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
23b9f 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d  ]);.      if( z=
23ba0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
23ba1 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20     len = 0;.    
23ba2 20 20 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20    while( *z ){. 
23ba3 20 20 20 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20         len++;.  
23ba4 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
23ba5 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20  P_UTF8(z);.     
23ba6 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
23ba7 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
23ba8 65 78 74 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20  ext, len);.     
23ba9 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
23baa 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
23bab 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
23bac 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b  t_null(context);
23bad 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
23bae 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
23baf 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
23bb0 6f 66 20 74 68 65 20 61 62 73 28 29 20 66 75 6e  of the abs() fun
23bb1 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20  ction.*/.static 
23bb2 76 6f 69 64 20 61 62 73 46 75 6e 63 28 73 71 6c  void absFunc(sql
23bb3 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
23bb4 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
23bb5 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
23bb6 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74  *argv){.  assert
23bb7 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55  ( argc==1 );.  U
23bb8 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
23bb9 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28  argc);.  switch(
23bba 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
23bbb 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a  ype(argv[0]) ){.
23bbc 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
23bbd 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20  INTEGER: {.     
23bbe 20 69 36 34 20 69 56 61 6c 20 3d 20 73 71 6c 69   i64 iVal = sqli
23bbf 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
23bc0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
23bc1 69 66 28 20 69 56 61 6c 3c 30 20 29 7b 0a 20 20  if( iVal<0 ){.  
23bc2 20 20 20 20 20 20 69 66 28 20 28 69 56 61 6c 3c        if( (iVal<
23bc3 3c 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  <1)==0 ){.      
23bc4 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
23bc5 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
23bc6 2c 20 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66  , "integer overf
23bc7 6c 6f 77 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20  low", -1);.     
23bc8 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
23bc9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
23bca 56 61 6c 20 3d 20 2d 69 56 61 6c 3b 0a 20 20 20  Val = -iVal;.   
23bcb 20 20 20 7d 20 0a 20 20 20 20 20 20 73 71 6c 69     } .      sqli
23bcc 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
23bcd 28 63 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b  (context, iVal);
23bce 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
23bcf 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
23bd0 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20  ITE_NULL: {.    
23bd1 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23bd2 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a  _null(context);.
23bd3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23bd4 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
23bd5 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72  {.      double r
23bd6 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Val = sqlite3_va
23bd7 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
23bd8 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  0]);.      if( r
23bd9 56 61 6c 3c 30 20 29 20 72 56 61 6c 20 3d 20 2d  Val<0 ) rVal = -
23bda 72 56 61 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69  rVal;.      sqli
23bdb 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
23bdc 65 28 63 6f 6e 74 65 78 74 2c 20 72 56 61 6c 29  e(context, rVal)
23bdd 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
23bde 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
23bdf 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
23be0 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 28 29   of the substr()
23be1 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
23be2 20 73 75 62 73 74 72 28 78 2c 70 31 2c 70 32 29   substr(x,p1,p2)
23be3 20 20 72 65 74 75 72 6e 73 20 70 32 20 63 68 61    returns p2 cha
23be4 72 61 63 74 65 72 73 20 6f 66 20 78 5b 5d 20 62  racters of x[] b
23be5 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 70 31  eginning with p1
23be6 2e 0a 2a 2a 20 70 31 20 69 73 20 31 2d 69 6e 64  ..** p1 is 1-ind
23be7 65 78 65 64 2e 20 20 53 6f 20 73 75 62 73 74 72  exed.  So substr
23be8 28 78 2c 31 2c 31 29 20 72 65 74 75 72 6e 73 20  (x,1,1) returns 
23be9 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63  the first charac
23bea 74 65 72 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66  ter.** of x.  If
23beb 20 78 20 69 73 20 74 65 78 74 2c 20 74 68 65 6e   x is text, then
23bec 20 77 65 20 61 63 74 75 61 6c 6c 79 20 63 6f 75   we actually cou
23bed 6e 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  nt UTF-8 charact
23bee 65 72 73 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20  ers..** If x is 
23bef 61 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20  a blob, then we 
23bf0 63 6f 75 6e 74 20 62 79 74 65 73 2e 0a 2a 2a 0a  count bytes..**.
23bf1 2a 2a 20 49 66 20 70 31 20 69 73 20 6e 65 67 61  ** If p1 is nega
23bf2 74 69 76 65 2c 20 74 68 65 6e 20 77 65 20 62 65  tive, then we be
23bf3 67 69 6e 20 61 62 73 28 70 31 29 20 66 72 6f 6d  gin abs(p1) from
23bf4 20 74 68 65 20 65 6e 64 20 6f 66 20 78 5b 5d 2e   the end of x[].
23bf5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
23bf6 73 75 62 73 74 72 46 75 6e 63 28 0a 20 20 73 71  substrFunc(.  sq
23bf7 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
23bf8 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
23bf9 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
23bfa 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
23bfb 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
23bfc 68 61 72 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20  har *z;.  const 
23bfd 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
23bfe 32 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20  2;.  int len;.  
23bff 69 6e 74 20 70 30 74 79 70 65 3b 0a 20 20 69 36  int p0type;.  i6
23c00 34 20 70 31 2c 20 70 32 3b 0a 20 20 69 6e 74 20  4 p1, p2;.  int 
23c01 6e 65 67 50 32 20 3d 20 30 3b 0a 0a 20 20 61 73  negP2 = 0;..  as
23c02 73 65 72 74 28 20 61 72 67 63 3d 3d 33 20 7c 7c  sert( argc==3 ||
23c03 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66   argc==2 );.  if
23c04 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
23c05 74 79 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53  type(argv[1])==S
23c06 51 4c 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 7c 7c  QLITE_NULL.   ||
23c07 20 28 61 72 67 63 3d 3d 33 20 26 26 20 73 71 6c   (argc==3 && sql
23c08 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
23c09 61 72 67 76 5b 32 5d 29 3d 3d 53 51 4c 49 54 45  argv[2])==SQLITE
23c0a 5f 4e 55 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20  _NULL).  ){.    
23c0b 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 30  return;.  }.  p0
23c0c 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  type = sqlite3_v
23c0d 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
23c0e 5d 29 3b 0a 20 20 69 66 28 20 70 30 74 79 70 65  ]);.  if( p0type
23c0f 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b  ==SQLITE_BLOB ){
23c10 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74  .    len = sqlit
23c11 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
23c12 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 7a 20 3d  rgv[0]);.    z =
23c13 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
23c14 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  lob(argv[0]);.  
23c15 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
23c16 75 72 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28  urn;.    assert(
23c17 20 6c 65 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61   len==sqlite3_va
23c18 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
23c19 5d 29 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ]) );.  }else{. 
23c1a 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76     z = sqlite3_v
23c1b 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
23c1c 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30  ]);.    if( z==0
23c1d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6c   ) return;.    l
23c1e 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  en = 0;.    for(
23c1f 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 6c 65 6e 2b 2b  z2=z; *z2; len++
23c20 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ){.      SQLITE_
23c21 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20  SKIP_UTF8(z2);. 
23c22 20 20 20 7d 0a 20 20 7d 0a 20 20 70 31 20 3d 20     }.  }.  p1 = 
23c23 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
23c24 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66  t(argv[1]);.  if
23c25 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20  ( argc==3 ){.   
23c26 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f 76 61   p2 = sqlite3_va
23c27 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29  lue_int(argv[2])
23c28 3b 0a 20 20 20 20 69 66 28 20 70 32 3c 30 20 29  ;.    if( p2<0 )
23c29 7b 0a 20 20 20 20 20 20 70 32 20 3d 20 2d 70 32  {.      p2 = -p2
23c2a 3b 0a 20 20 20 20 20 20 6e 65 67 50 32 20 3d 20  ;.      negP2 = 
23c2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
23c2c 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69 74  {.    p2 = sqlit
23c2d 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
23c2e 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61  ndle(context)->a
23c2f 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
23c30 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a  IT_LENGTH];.  }.
23c31 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20    if( p1<0 ){.  
23c32 20 20 70 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20    p1 += len;.   
23c33 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20   if( p1<0 ){.   
23c34 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20     p2 += p1;.   
23c35 20 20 20 69 66 28 20 70 32 3c 30 20 29 20 70 32     if( p2<0 ) p2
23c36 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 31 20 3d   = 0;.      p1 =
23c37 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   0;.    }.  }els
23c38 65 20 69 66 28 20 70 31 3e 30 20 29 7b 0a 20 20  e if( p1>0 ){.  
23c39 20 20 70 31 2d 2d 3b 0a 20 20 7d 65 6c 73 65 20    p1--;.  }else 
23c3a 69 66 28 20 70 32 3e 30 20 29 7b 0a 20 20 20 20  if( p2>0 ){.    
23c3b 70 32 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  p2--;.  }.  if( 
23c3c 6e 65 67 50 32 20 29 7b 0a 20 20 20 20 70 31 20  negP2 ){.    p1 
23c3d 2d 3d 20 70 32 3b 0a 20 20 20 20 69 66 28 20 70  -= p2;.    if( p
23c3e 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32 20  1<0 ){.      p2 
23c3f 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 70 31 20  += p1;.      p1 
23c40 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
23c41 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26   assert( p1>=0 &
23c42 26 20 70 32 3e 3d 30 20 29 3b 0a 20 20 69 66 28  & p2>=0 );.  if(
23c43 20 70 31 2b 70 32 3e 6c 65 6e 20 29 7b 0a 20 20   p1+p2>len ){.  
23c44 20 20 70 32 20 3d 20 6c 65 6e 2d 70 31 3b 0a 20    p2 = len-p1;. 
23c45 20 20 20 69 66 28 20 70 32 3c 30 20 29 20 70 32     if( p2<0 ) p2
23c46 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
23c47 70 30 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 42  p0type!=SQLITE_B
23c48 4c 4f 42 20 29 7b 0a 20 20 20 20 77 68 69 6c 65  LOB ){.    while
23c49 28 20 2a 7a 20 26 26 20 70 31 20 29 7b 0a 20 20  ( *z && p1 ){.  
23c4a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
23c4b 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 70  UTF8(z);.      p
23c4c 31 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  1--;.    }.    f
23c4d 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26 20  or(z2=z; *z2 && 
23c4e 70 32 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 20 20  p2; p2--){.     
23c4f 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
23c50 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20  8(z2);.    }.   
23c51 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
23c52 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63  text(context, (c
23c53 68 61 72 2a 29 7a 2c 20 28 69 6e 74 29 28 7a 32  har*)z, (int)(z2
23c54 2d 7a 29 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  -z), SQLITE_TRAN
23c55 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b  SIENT);.  }else{
23c56 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
23c57 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74  ult_blob(context
23c58 2c 20 28 63 68 61 72 2a 29 26 7a 5b 70 31 5d 2c  , (char*)&z[p1],
23c59 20 28 69 6e 74 29 70 32 2c 20 53 51 4c 49 54 45   (int)p2, SQLITE
23c5a 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
23c5b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
23c5c 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
23c5d 72 6f 75 6e 64 28 29 20 66 75 6e 63 74 69 6f 6e  round() function
23c5e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
23c5f 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
23c60 5f 50 4f 49 4e 54 0a 73 74 61 74 69 63 20 76 6f  _POINT.static vo
23c61 69 64 20 72 6f 75 6e 64 46 75 6e 63 28 73 71 6c  id roundFunc(sql
23c62 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
23c63 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
23c64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
23c65 2a 61 72 67 76 29 7b 0a 20 20 69 6e 74 20 6e 20  *argv){.  int n 
23c66 3d 20 30 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b  = 0;.  double r;
23c67 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 35 30 30  .  char zBuf[500
23c68 5d 3b 20 20 2f 2a 20 6c 61 72 67 65 72 20 74 68  ];  /* larger th
23c69 61 6e 20 74 68 65 20 25 66 20 72 65 70 72 65 73  an the %f repres
23c6a 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
23c6b 6c 61 72 67 65 73 74 20 64 6f 75 62 6c 65 20 2a  largest double *
23c6c 2f 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  /.  assert( argc
23c6d 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29  ==1 || argc==2 )
23c6e 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20  ;.  if( argc==2 
23c6f 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  ){.    if( SQLIT
23c70 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f  E_NULL==sqlite3_
23c71 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
23c72 31 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  1]) ) return;.  
23c73 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    n = sqlite3_va
23c74 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29  lue_int(argv[1])
23c75 3b 0a 20 20 20 20 69 66 28 20 6e 3e 33 30 20 29  ;.    if( n>30 )
23c76 20 6e 20 3d 20 33 30 3b 0a 20 20 20 20 69 66 28   n = 30;.    if(
23c77 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20   n<0 ) n = 0;.  
23c78 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  }.  if( sqlite3_
23c79 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
23c7a 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
23c7b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 72 20 3d   ) return;.  r =
23c7c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
23c7d 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a  ouble(argv[0]);.
23c7e 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
23c7f 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c  tf(sizeof(zBuf),
23c80 7a 42 75 66 2c 22 25 2e 2a 66 22 2c 6e 2c 72 29  zBuf,"%.*f",n,r)
23c81 3b 0a 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28  ;.  sqlite3AtoF(
23c82 7a 42 75 66 2c 20 26 72 29 3b 0a 20 20 73 71 6c  zBuf, &r);.  sql
23c83 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
23c84 6c 65 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a  le(context, r);.
23c85 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
23c86 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62  Allocate nByte b
23c87 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 75 73  ytes of space us
23c88 69 6e 67 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ing sqlite3_mall
23c89 6f 63 28 29 2e 20 49 66 20 74 68 65 0a 2a 2a 20  oc(). If the.** 
23c8a 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73  allocation fails
23c8b 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72  , call sqlite3_r
23c8c 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
23c8d 6d 28 29 20 74 6f 20 6e 6f 74 69 66 79 0a 2a 2a  m() to notify.**
23c8e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
23c8f 6e 64 6c 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63  ndle that malloc
23c90 28 29 20 68 61 73 20 66 61 69 6c 65 64 20 61 6e  () has failed an
23c91 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a  d return NULL..*
23c92 2a 20 49 66 20 6e 42 79 74 65 20 69 73 20 6c 61  * If nByte is la
23c93 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61  rger than the ma
23c94 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6f 72 20  ximum string or 
23c95 62 6c 6f 62 20 6c 65 6e 67 74 68 2c 20 74 68 65  blob length, the
23c96 6e 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20 53 51  n.** raise an SQ
23c97 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 78 63 65  LITE_TOOBIG exce
23c98 70 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e  ption and return
23c99 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63   NULL..*/.static
23c9a 20 76 6f 69 64 20 2a 63 6f 6e 74 65 78 74 4d 61   void *contextMa
23c9b 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lloc(sqlite3_con
23c9c 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
23c9d 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 63 68 61  64 nByte){.  cha
23c9e 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20  r *z;.  sqlite3 
23c9f 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
23ca0 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
23ca1 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65  context);.  asse
23ca2 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20  rt( nByte>0 );. 
23ca3 20 74 65 73 74 63 61 73 65 28 20 6e 42 79 74 65   testcase( nByte
23ca4 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ==db->aLimit[SQL
23ca5 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
23ca6 5d 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  ] );.  testcase(
23ca7 20 6e 42 79 74 65 3d 3d 64 62 2d 3e 61 4c 69 6d   nByte==db->aLim
23ca8 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
23ca9 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 69  LENGTH]+1 );.  i
23caa 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69  f( nByte>db->aLi
23cab 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
23cac 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
23cad 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
23cae 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74  rror_toobig(cont
23caf 65 78 74 29 3b 0a 20 20 20 20 7a 20 3d 20 30 3b  ext);.    z = 0;
23cb0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20  .  }else{.    z 
23cb1 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
23cb2 28 69 6e 74 29 6e 42 79 74 65 29 3b 0a 20 20 20  (int)nByte);.   
23cb3 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20 20 20   if( !z ){.     
23cb4 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
23cb5 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74  error_nomem(cont
23cb6 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ext);.    }.  }.
23cb7 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f    return z;.}../
23cb8 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
23cb9 69 6f 6e 20 6f 66 20 74 68 65 20 75 70 70 65 72  ion of the upper
23cba 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 29 20 53  () and lower() S
23cbb 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f  QL functions..*/
23cbc 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 70  .static void upp
23cbd 65 72 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63  erFunc(sqlite3_c
23cbe 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
23cbf 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
23cc0 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
23cc1 7b 0a 20 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20  {.  char *z1;.  
23cc2 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a  const char *z2;.
23cc3 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 55 4e    int i, n;.  UN
23cc4 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
23cc5 72 67 63 29 3b 0a 20 20 7a 32 20 3d 20 28 63 68  rgc);.  z2 = (ch
23cc6 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
23cc7 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
23cc8 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
23cc9 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
23cca 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79  0]);.  /* Verify
23ccb 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74   that the call t
23ccc 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73 20  o _bytes() does 
23ccd 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 74  not invalidate t
23cce 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e 74  he _text() point
23ccf 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  er */.  assert( 
23cd0 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69 74  z2==(char*)sqlit
23cd1 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
23cd2 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  gv[0]) );.  if( 
23cd3 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63  z2 ){.    z1 = c
23cd4 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
23cd5 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2b 31  text, ((i64)n)+1
23cd6 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b  );.    if( z1 ){
23cd7 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31  .      memcpy(z1
23cd8 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20  , z2, n+1);.    
23cd9 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d    for(i=0; z1[i]
23cda 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
23cdb 7a 31 5b 69 5d 20 3d 20 28 63 68 61 72 29 73 71  z1[i] = (char)sq
23cdc 6c 69 74 65 33 54 6f 75 70 70 65 72 28 7a 31 5b  lite3Toupper(z1[
23cdd 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i]);.      }.   
23cde 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
23cdf 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
23ce0 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f  z1, -1, sqlite3_
23ce1 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  free);.    }.  }
23ce2 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  .}.static void l
23ce3 6f 77 65 72 46 75 6e 63 28 73 71 6c 69 74 65 33  owerFunc(sqlite3
23ce4 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23ce5 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
23ce6 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
23ce7 76 29 7b 0a 20 20 75 38 20 2a 7a 31 3b 0a 20 20  v){.  u8 *z1;.  
23ce8 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a  const char *z2;.
23ce9 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 55 4e    int i, n;.  UN
23cea 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
23ceb 72 67 63 29 3b 0a 20 20 7a 32 20 3d 20 28 63 68  rgc);.  z2 = (ch
23cec 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
23ced 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
23cee 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
23cef 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
23cf0 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79  0]);.  /* Verify
23cf1 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74   that the call t
23cf2 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73 20  o _bytes() does 
23cf3 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 74  not invalidate t
23cf4 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e 74  he _text() point
23cf5 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  er */.  assert( 
23cf6 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69 74  z2==(char*)sqlit
23cf7 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
23cf8 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  gv[0]) );.  if( 
23cf9 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63  z2 ){.    z1 = c
23cfa 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
23cfb 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2b 31  text, ((i64)n)+1
23cfc 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b  );.    if( z1 ){
23cfd 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31  .      memcpy(z1
23cfe 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20  , z2, n+1);.    
23cff 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d    for(i=0; z1[i]
23d00 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
23d01 7a 31 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 54  z1[i] = sqlite3T
23d02 6f 6c 6f 77 65 72 28 7a 31 5b 69 5d 29 3b 0a 20  olower(z1[i]);. 
23d03 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
23d04 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
23d05 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 20  (context, (char 
23d06 2a 29 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65  *)z1, -1, sqlite
23d07 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20  3_free);.    }. 
23d08 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
23d09 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
23d0a 65 20 49 46 4e 55 4c 4c 28 29 2c 20 4e 56 4c 28  e IFNULL(), NVL(
23d0b 29 2c 20 61 6e 64 20 43 4f 41 4c 45 53 43 45 28  ), and COALESCE(
23d0c 29 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 0a 2a  ) functions.  .*
23d0d 2a 20 41 6c 6c 20 74 68 72 65 65 20 64 6f 20 74  * All three do t
23d0e 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e 20 20  he same thing.  
23d0f 54 68 65 79 20 72 65 74 75 72 6e 20 74 68 65 20  They return the 
23d10 66 69 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a  first non-NULL.*
23d11 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73  * argument..*/.s
23d12 74 61 74 69 63 20 76 6f 69 64 20 69 66 6e 75 6c  tatic void ifnul
23d13 6c 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  lFunc(.  sqlite3
23d14 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23d15 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
23d16 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
23d17 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69  *argv.){.  int i
23d18 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61  ;.  for(i=0; i<a
23d19 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  rgc; i++){.    i
23d1a 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d  f( SQLITE_NULL!=
23d1b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
23d1c 70 65 28 61 72 67 76 5b 69 5d 29 20 29 7b 0a 20  pe(argv[i]) ){. 
23d1d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
23d1e 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78  ult_value(contex
23d1f 74 2c 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20  t, argv[i]);.   
23d20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
23d21 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
23d22 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72  lementation of r
23d23 61 6e 64 6f 6d 28 29 2e 20 20 52 65 74 75 72 6e  andom().  Return
23d24 20 61 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65   a random intege
23d25 72 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76  r.  .*/.static v
23d26 6f 69 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28 0a  oid randomFunc(.
23d27 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
23d28 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
23d29 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
23d2a 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
23d2b 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74  Used2.){.  sqlit
23d2c 65 5f 69 6e 74 36 34 20 72 3b 0a 20 20 55 4e 55  e_int64 r;.  UNU
23d2d 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
23d2e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
23d2f 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e  );.  sqlite3_ran
23d30 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72  domness(sizeof(r
23d31 29 2c 20 26 72 29 3b 0a 20 20 69 66 28 20 72 3c  ), &r);.  if( r<
23d32 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 6e  0 ){.    /* We n
23d33 65 65 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61  eed to prevent a
23d34 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 6f   random number o
23d35 66 20 30 78 38 30 30 30 30 30 30 30 30 30 30 30  f 0x800000000000
23d36 30 30 30 30 20 0a 20 20 20 20 2a 2a 20 28 6f 72  0000 .    ** (or
23d37 20 2d 39 32 32 33 33 37 32 30 33 36 38 35 34 37   -92233720368547
23d38 37 35 38 30 38 29 20 73 69 6e 63 65 20 77 68 65  75808) since whe
23d39 6e 20 79 6f 75 20 64 6f 20 61 62 73 28 29 20 6f  n you do abs() o
23d3a 66 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 6e 75  f that.    ** nu
23d3b 6d 62 65 72 20 6f 66 20 79 6f 75 20 67 65 74 20  mber of you get 
23d3c 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 62  the same value b
23d3d 61 63 6b 20 61 67 61 69 6e 2e 20 20 54 6f 20 64  ack again.  To d
23d3e 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 6e  o this.    ** in
23d3f 20 61 20 77 61 79 20 74 68 61 74 20 69 73 20 74   a way that is t
23d40 65 73 74 61 62 6c 65 2c 20 6d 61 73 6b 20 74 68  estable, mask th
23d41 65 20 73 69 67 6e 20 62 69 74 20 6f 66 66 20 6f  e sign bit off o
23d42 66 20 6e 65 67 61 74 69 76 65 0a 20 20 20 20 2a  f negative.    *
23d43 2a 20 76 61 6c 75 65 73 2c 20 72 65 73 75 6c 74  * values, result
23d44 69 6e 67 20 69 6e 20 61 20 70 6f 73 69 74 69 76  ing in a positiv
23d45 65 20 76 61 6c 75 65 2e 20 20 54 68 65 6e 20 74  e value.  Then t
23d46 61 6b 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20  ake the .    ** 
23d47 32 73 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66  2s complement of
23d48 20 74 68 61 74 20 70 6f 73 69 74 69 76 65 20 76   that positive v
23d49 61 6c 75 65 2e 20 20 54 68 65 20 65 6e 64 20 72  alue.  The end r
23d4a 65 73 75 6c 74 20 63 61 6e 0a 20 20 20 20 2a 2a  esult can.    **
23d4b 20 74 68 65 72 65 66 6f 72 65 20 62 65 20 6e 6f   therefore be no
23d4c 20 6c 65 73 73 20 74 68 61 6e 20 2d 39 32 32 33   less than -9223
23d4d 33 37 32 30 33 36 38 35 34 37 37 35 38 30 37 2e  372036854775807.
23d4e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 20 3d 20  .    */.    r = 
23d4f 2d 28 72 20 5e 20 28 28 28 73 71 6c 69 74 65 33  -(r ^ (((sqlite3
23d50 5f 69 6e 74 36 34 29 31 29 3c 3c 36 33 29 29 3b  _int64)1)<<63));
23d51 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72  .  }.  sqlite3_r
23d52 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74  esult_int64(cont
23d53 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ext, r);.}../*.*
23d54 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
23d55 20 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 4e   of randomblob(N
23d56 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 6e  ).  Return a ran
23d57 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68 61 74  dom blob.** that
23d58 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67   is N bytes long
23d59 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
23d5a 20 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20 20 73   randomBlob(.  s
23d5b 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
23d5c 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
23d5d 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
23d5e 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
23d5f 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e   int n;.  unsign
23d60 65 64 20 63 68 61 72 20 2a 70 3b 0a 20 20 61 73  ed char *p;.  as
23d61 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
23d62 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
23d63 54 45 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d  TER(argc);.  n =
23d64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
23d65 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  nt(argv[0]);.  i
23d66 66 28 20 6e 3c 31 20 29 7b 0a 20 20 20 20 6e 20  f( n<1 ){.    n 
23d67 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 63  = 1;.  }.  p = c
23d68 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
23d69 74 65 78 74 2c 20 6e 29 3b 0a 20 20 69 66 28 20  text, n);.  if( 
23d6a 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  p ){.    sqlite3
23d6b 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c 20 70  _randomness(n, p
23d6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
23d6d 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65  esult_blob(conte
23d6e 78 74 2c 20 28 63 68 61 72 2a 29 70 2c 20 6e 2c  xt, (char*)p, n,
23d6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
23d70 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
23d71 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
23d72 68 65 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  he last_insert_r
23d73 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e 63 74  owid() SQL funct
23d74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ion.  The return
23d75 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 74 68 65  .** value is the
23d76 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c   same as the sql
23d77 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
23d78 5f 72 6f 77 69 64 28 29 20 41 50 49 20 66 75 6e  _rowid() API fun
23d79 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
23d7a 20 76 6f 69 64 20 6c 61 73 74 5f 69 6e 73 65 72   void last_inser
23d7b 74 5f 72 6f 77 69 64 28 0a 20 20 73 71 6c 69 74  t_rowid(.  sqlit
23d7c 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
23d7d 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55  ext, .  int NotU
23d7e 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  sed, .  sqlite3_
23d7f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32  value **NotUsed2
23d80 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
23d81 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
23d82 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
23d83 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44  ntext);.  UNUSED
23d84 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
23d85 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
23d86 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23d87 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20  _int64(context, 
23d88 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
23d89 65 72 74 5f 72 6f 77 69 64 28 64 62 29 29 3b 0a  ert_rowid(db));.
23d8a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
23d8b 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63  ntation of the c
23d8c 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e  hanges() SQL fun
23d8d 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75  ction.  The retu
23d8e 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a  rn value is the.
23d8f 2a 2a 20 73 61 6d 65 20 61 73 20 74 68 65 20 73  ** same as the s
23d90 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29  qlite3_changes()
23d91 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   API function..*
23d92 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68  /.static void ch
23d93 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65 33  anges(.  sqlite3
23d94 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23d95 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
23d96 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
23d97 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a  e **NotUsed2.){.
23d98 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23d99 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
23d9a 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
23d9b 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  t);.  UNUSED_PAR
23d9c 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
23d9d 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71   NotUsed2);.  sq
23d9e 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
23d9f 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65  (context, sqlite
23da0 33 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a  3_changes(db));.
23da1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
23da2 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 74  ntation of the t
23da3 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 53  otal_changes() S
23da4 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
23da5 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
23da6 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73  s.** the same as
23da7 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 74   the sqlite3_tot
23da8 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 41 50 49  al_changes() API
23da9 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
23daa 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 5f  atic void total_
23dab 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74  changes(.  sqlit
23dac 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
23dad 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
23dae 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
23daf 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29  lue **NotUsed2.)
23db0 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
23db1 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
23db2 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
23db3 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ext);.  UNUSED_P
23db4 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
23db5 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
23db6 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
23db7 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69  nt(context, sqli
23db8 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65  te3_total_change
23db9 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  s(db));.}../*.**
23dba 20 41 20 73 74 72 75 63 74 75 72 65 20 64 65 66   A structure def
23dbb 69 6e 69 6e 67 20 68 6f 77 20 74 6f 20 64 6f 20  ining how to do 
23dbc 47 4c 4f 42 2d 73 74 79 6c 65 20 63 6f 6d 70 61  GLOB-style compa
23dbd 72 69 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63  risons..*/.struc
23dbe 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 7b 0a  t compareInfo {.
23dbf 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 3b 0a 20    u8 matchAll;. 
23dc0 20 75 38 20 6d 61 74 63 68 4f 6e 65 3b 0a 20 20   u8 matchOne;.  
23dc1 75 38 20 6d 61 74 63 68 53 65 74 3b 0a 20 20 75  u8 matchSet;.  u
23dc2 38 20 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a  8 noCase;.};../*
23dc3 0a 2a 2a 20 46 6f 72 20 4c 49 4b 45 20 61 6e 64  .** For LIKE and
23dc4 20 47 4c 4f 42 20 6d 61 74 63 68 69 6e 67 20 6f   GLOB matching o
23dc5 6e 20 45 42 43 44 49 43 20 6d 61 63 68 69 6e 65  n EBCDIC machine
23dc6 73 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 65  s, assume that e
23dc7 76 65 72 79 0a 2a 2a 20 63 68 61 72 61 63 74 65  very.** characte
23dc8 72 20 69 73 20 65 78 61 63 74 6c 79 20 6f 6e 65  r is exactly one
23dc9 20 62 79 74 65 20 69 6e 20 73 69 7a 65 2e 20 20   byte in size.  
23dca 41 6c 73 6f 2c 20 61 6c 6c 20 63 68 61 72 61 63  Also, all charac
23dcb 74 65 72 73 20 61 72 65 0a 2a 2a 20 61 62 6c 65  ters are.** able
23dcc 20 74 6f 20 70 61 72 74 69 63 69 70 61 74 65 20   to participate 
23dcd 69 6e 20 75 70 70 65 72 2d 63 61 73 65 2d 74 6f  in upper-case-to
23dce 2d 6c 6f 77 65 72 2d 63 61 73 65 20 6d 61 70 70  -lower-case mapp
23dcf 69 6e 67 73 20 69 6e 20 45 42 43 44 49 43 0a 2a  ings in EBCDIC.*
23dd0 2a 20 77 68 65 72 65 61 73 20 6f 6e 6c 79 20 63  * whereas only c
23dd1 68 61 72 61 63 74 65 72 73 20 6c 65 73 73 20 74  haracters less t
23dd2 68 61 6e 20 30 78 38 30 20 64 6f 20 69 6e 20 41  han 0x80 do in A
23dd3 53 43 49 49 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  SCII..*/.#if def
23dd4 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 42 43 44  ined(SQLITE_EBCD
23dd5 49 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  IC).# define sql
23dd6 69 74 65 33 55 74 66 38 52 65 61 64 28 41 2c 43  ite3Utf8Read(A,C
23dd7 29 20 20 20 20 28 2a 28 41 2b 2b 29 29 0a 23 20  )    (*(A++)).# 
23dd8 64 65 66 69 6e 65 20 47 6c 6f 67 55 70 70 65 72  define GlogUpper
23dd9 54 6f 4c 6f 77 65 72 28 41 29 20 20 20 20 20 41  ToLower(A)     A
23dda 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54   = sqlite3UpperT
23ddb 6f 4c 6f 77 65 72 5b 41 5d 0a 23 65 6c 73 65 0a  oLower[A].#else.
23ddc 23 20 64 65 66 69 6e 65 20 47 6c 6f 67 55 70 70  # define GlogUpp
23ddd 65 72 54 6f 4c 6f 77 65 72 28 41 29 20 20 20 20  erToLower(A)    
23dde 20 69 66 28 20 41 3c 30 78 38 30 20 29 7b 20 41   if( A<0x80 ){ A
23ddf 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54   = sqlite3UpperT
23de0 6f 4c 6f 77 65 72 5b 41 5d 3b 20 7d 0a 23 65 6e  oLower[A]; }.#en
23de1 64 69 66 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73  dif..static cons
23de2 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65  t struct compare
23de3 49 6e 66 6f 20 67 6c 6f 62 49 6e 66 6f 20 3d 20  Info globInfo = 
23de4 7b 20 27 2a 27 2c 20 27 3f 27 2c 20 27 5b 27 2c  { '*', '?', '[',
23de5 20 30 20 7d 3b 0a 2f 2a 20 54 68 65 20 63 6f 72   0 };./* The cor
23de6 72 65 63 74 20 53 51 4c 2d 39 32 20 62 65 68 61  rect SQL-92 beha
23de7 76 69 6f 72 20 69 73 20 66 6f 72 20 74 68 65 20  vior is for the 
23de8 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 6f  LIKE operator to
23de9 20 69 67 6e 6f 72 65 0a 2a 2a 20 63 61 73 65 2e   ignore.** case.
23dea 20 20 54 68 75 73 20 20 27 61 27 20 4c 49 4b 45    Thus  'a' LIKE
23deb 20 27 41 27 20 77 6f 75 6c 64 20 62 65 20 74 72   'A' would be tr
23dec 75 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f  ue. */.static co
23ded 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61  nst struct compa
23dee 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 4e  reInfo likeInfoN
23def 6f 72 6d 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27  orm = { '%', '_'
23df0 2c 20 20 20 30 2c 20 31 20 7d 3b 0a 2f 2a 20 49  ,   0, 1 };./* I
23df1 66 20 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 45  f SQLITE_CASE_SE
23df2 4e 53 49 54 49 56 45 5f 4c 49 4b 45 20 69 73 20  NSITIVE_LIKE is 
23df3 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68  defined, then th
23df4 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 0a  e LIKE operator.
23df5 2a 2a 20 69 73 20 63 61 73 65 20 73 65 6e 73 69  ** is case sensi
23df6 74 69 76 65 20 63 61 75 73 69 6e 67 20 27 61 27  tive causing 'a'
23df7 20 4c 49 4b 45 20 27 41 27 20 74 6f 20 62 65 20   LIKE 'A' to be 
23df8 66 61 6c 73 65 20 2a 2f 0a 73 74 61 74 69 63 20  false */.static 
23df9 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d  const struct com
23dfa 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66  pareInfo likeInf
23dfb 6f 41 6c 74 20 3d 20 7b 20 27 25 27 2c 20 27 5f  oAlt = { '%', '_
23dfc 27 2c 20 20 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a  ',   0, 0 };../*
23dfd 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20  .** Compare two 
23dfe 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f  UTF-8 strings fo
23dff 72 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 65  r equality where
23e00 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e   the first strin
23e01 67 20 63 61 6e 0a 2a 2a 20 70 6f 74 65 6e 74 69  g can.** potenti
23e02 61 6c 6c 79 20 62 65 20 61 20 22 67 6c 6f 62 22  ally be a "glob"
23e03 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 52 65   expression.  Re
23e04 74 75 72 6e 20 74 72 75 65 20 28 31 29 20 69 66  turn true (1) if
23e05 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 74 68 65   they.** are the
23e06 20 73 61 6d 65 20 61 6e 64 20 66 61 6c 73 65 20   same and false 
23e07 28 30 29 20 69 66 20 74 68 65 79 20 61 72 65 20  (0) if they are 
23e08 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a  different..**.**
23e09 20 47 6c 6f 62 62 69 6e 67 20 72 75 6c 65 73 3a   Globbing rules:
23e0a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 2a 27 20  .**.**      '*' 
23e0b 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 61 6e        Matches an
23e0c 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 7a 65  y sequence of ze
23e0d 72 6f 20 6f 72 20 6d 6f 72 65 20 63 68 61 72 61  ro or more chara
23e0e 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  cters..**.**    
23e0f 20 20 27 3f 27 20 20 20 20 20 20 20 4d 61 74 63    '?'       Matc
23e10 68 65 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20  hes exactly one 
23e11 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 0a 2a 2a  character..**.**
23e12 20 20 20 20 20 5b 2e 2e 2e 5d 20 20 20 20 20 20       [...]      
23e13 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61 72  Matches one char
23e14 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 65  acter from the e
23e15 6e 63 6c 6f 73 65 64 20 6c 69 73 74 20 6f 66 0a  nclosed list of.
23e16 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
23e17 20 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a    characters..**
23e18 0a 2a 2a 20 20 20 20 20 5b 5e 2e 2e 2e 5d 20 20  .**     [^...]  
23e19 20 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63     Matches one c
23e1a 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69 6e 20  haracter not in 
23e1b 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73  the enclosed lis
23e1c 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68  t..**.** With th
23e1d 65 20 5b 2e 2e 2e 5d 20 61 6e 64 20 5b 5e 2e 2e  e [...] and [^..
23e1e 2e 5d 20 6d 61 74 63 68 69 6e 67 2c 20 61 20 27  .] matching, a '
23e1f 5d 27 20 63 68 61 72 61 63 74 65 72 20 63 61 6e  ]' character can
23e20 20 62 65 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20   be included.** 
23e21 69 6e 20 74 68 65 20 6c 69 73 74 20 62 79 20 6d  in the list by m
23e22 61 6b 69 6e 67 20 69 74 20 74 68 65 20 66 69 72  aking it the fir
23e23 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74  st character aft
23e24 65 72 20 27 5b 27 20 6f 72 20 27 5e 27 2e 20 20  er '[' or '^'.  
23e25 41 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 63 68  A.** range of ch
23e26 61 72 61 63 74 65 72 73 20 63 61 6e 20 62 65 20  aracters can be 
23e27 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 20  specified using 
23e28 27 2d 27 2e 20 20 45 78 61 6d 70 6c 65 3a 0a 2a  '-'.  Example:.*
23e29 2a 20 22 5b 61 2d 7a 5d 22 20 6d 61 74 63 68 65  * "[a-z]" matche
23e2a 73 20 61 6e 79 20 73 69 6e 67 6c 65 20 6c 6f 77  s any single low
23e2b 65 72 2d 63 61 73 65 20 6c 65 74 74 65 72 2e 20  er-case letter. 
23e2c 20 54 6f 20 6d 61 74 63 68 20 61 20 27 2d 27 2c   To match a '-',
23e2d 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 74 68 65 20   make.** it the 
23e2e 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20 69  last character i
23e2f 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a  n the list..**.*
23e30 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
23e31 73 20 75 73 75 61 6c 6c 79 20 71 75 69 63 6b 2c  s usually quick,
23e32 20 62 75 74 20 63 61 6e 20 62 65 20 4e 2a 2a 32   but can be N**2
23e33 20 69 6e 20 74 68 65 20 77 6f 72 73 74 20 63 61   in the worst ca
23e34 73 65 2e 0a 2a 2a 0a 2a 2a 20 48 69 6e 74 73 3a  se..**.** Hints:
23e35 20 74 6f 20 6d 61 74 63 68 20 27 2a 27 20 6f 72   to match '*' or
23e36 20 27 3f 27 2c 20 70 75 74 20 74 68 65 6d 20 69   '?', put them i
23e37 6e 20 22 5b 5d 22 2e 20 20 4c 69 6b 65 20 74 68  n "[]".  Like th
23e38 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  is:.**.**       
23e39 20 20 61 62 63 5b 2a 5d 78 79 7a 20 20 20 20 20    abc[*]xyz     
23e3a 20 20 20 4d 61 74 63 68 65 73 20 22 61 62 63 2a     Matches "abc*
23e3b 78 79 7a 22 20 6f 6e 6c 79 0a 2a 2f 0a 73 74 61  xyz" only.*/.sta
23e3c 74 69 63 20 69 6e 74 20 70 61 74 74 65 72 6e 43  tic int patternC
23e3d 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 20  ompare(.  const 
23e3e 75 38 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 20  u8 *zPattern,   
23e3f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
23e40 65 20 67 6c 6f 62 20 70 61 74 74 65 72 6e 20 2a  e glob pattern *
23e41 2f 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53  /.  const u8 *zS
23e42 74 72 69 6e 67 2c 20 20 20 20 20 20 20 20 20 20  tring,          
23e43 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69       /* The stri
23e44 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67  ng to compare ag
23e45 61 69 6e 73 74 20 74 68 65 20 67 6c 6f 62 20 2a  ainst the glob *
23e46 2f 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74  /.  const struct
23e47 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49   compareInfo *pI
23e48 6e 66 6f 2c 20 2f 2a 20 49 6e 66 6f 72 6d 61 74  nfo, /* Informat
23e49 69 6f 6e 20 61 62 6f 75 74 20 68 6f 77 20 74 6f  ion about how to
23e4a 20 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 65 20   do the compare 
23e4b 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 65  */.  const int e
23e4c 73 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  sc              
23e4d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63        /* The esc
23e4e 61 70 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f  ape character */
23e4f 0a 29 7b 0a 20 20 69 6e 74 20 63 2c 20 63 32 3b  .){.  int c, c2;
23e50 0a 20 20 69 6e 74 20 69 6e 76 65 72 74 3b 0a 20  .  int invert;. 
23e51 20 69 6e 74 20 73 65 65 6e 3b 0a 20 20 75 38 20   int seen;.  u8 
23e52 6d 61 74 63 68 4f 6e 65 20 3d 20 70 49 6e 66 6f  matchOne = pInfo
23e53 2d 3e 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38  ->matchOne;.  u8
23e54 20 6d 61 74 63 68 41 6c 6c 20 3d 20 70 49 6e 66   matchAll = pInf
23e55 6f 2d 3e 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75  o->matchAll;.  u
23e56 38 20 6d 61 74 63 68 53 65 74 20 3d 20 70 49 6e  8 matchSet = pIn
23e57 66 6f 2d 3e 6d 61 74 63 68 53 65 74 3b 0a 20 20  fo->matchSet;.  
23e58 75 38 20 6e 6f 43 61 73 65 20 3d 20 70 49 6e 66  u8 noCase = pInf
23e59 6f 2d 3e 6e 6f 43 61 73 65 3b 20 0a 20 20 69 6e  o->noCase; .  in
23e5a 74 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30  t prevEscape = 0
23e5b 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
23e5c 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68   the previous ch
23e5d 61 72 61 63 74 65 72 20 77 61 73 20 27 65 73 63  aracter was 'esc
23e5e 61 70 65 27 20 2a 2f 0a 0a 20 20 77 68 69 6c 65  ape' */..  while
23e5f 28 20 28 63 20 3d 20 73 71 6c 69 74 65 33 55 74  ( (c = sqlite3Ut
23e60 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c  f8Read(zPattern,
23e61 26 7a 50 61 74 74 65 72 6e 29 29 21 3d 30 20 29  &zPattern))!=0 )
23e62 7b 0a 20 20 20 20 69 66 28 20 21 70 72 65 76 45  {.    if( !prevE
23e63 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61 74 63  scape && c==matc
23e64 68 41 6c 6c 20 29 7b 0a 20 20 20 20 20 20 77 68  hAll ){.      wh
23e65 69 6c 65 28 20 28 63 3d 73 71 6c 69 74 65 33 55  ile( (c=sqlite3U
23e66 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e  tf8Read(zPattern
23e67 2c 26 7a 50 61 74 74 65 72 6e 29 29 20 3d 3d 20  ,&zPattern)) == 
23e68 6d 61 74 63 68 41 6c 6c 0a 20 20 20 20 20 20 20  matchAll.       
23e69 20 20 20 20 20 20 20 20 7c 7c 20 63 20 3d 3d 20          || c == 
23e6a 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20  matchOne ){.    
23e6b 20 20 20 20 69 66 28 20 63 3d 3d 6d 61 74 63 68      if( c==match
23e6c 4f 6e 65 20 26 26 20 73 71 6c 69 74 65 33 55 74  One && sqlite3Ut
23e6d 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20  f8Read(zString, 
23e6e 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a  &zString)==0 ){.
23e6f 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
23e70 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
23e71 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
23e72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  c==0 ){.        
23e73 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
23e74 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 65 73 63  }else if( c==esc
23e75 20 29 7b 0a 20 20 20 20 20 20 20 20 63 20 3d 20   ){.        c = 
23e76 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
23e77 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74  zPattern, &zPatt
23e78 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ern);.        if
23e79 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c==0 ){.      
23e7a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
23e7b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
23e7c 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68  lse if( c==match
23e7d 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61  Set ){.        a
23e7e 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b  ssert( esc==0 );
23e7f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
23e80 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49   is GLOB, not LI
23e81 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  KE */.        as
23e82 73 65 72 74 28 20 6d 61 74 63 68 53 65 74 3c 30  sert( matchSet<0
23e83 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 69  x80 );  /* '[' i
23e84 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  s a single-byte 
23e85 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20  character */.   
23e86 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 74       while( *zSt
23e87 72 69 6e 67 20 26 26 20 70 61 74 74 65 72 6e 43  ring && patternC
23e88 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e  ompare(&zPattern
23e89 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49 6e  [-1],zString,pIn
23e8a 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20 20  fo,esc)==0 ){.  
23e8b 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53          SQLITE_S
23e8c 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69 6e 67  KIP_UTF8(zString
23e8d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
23e8e 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 53 74       return *zSt
23e8f 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20 7d  ring!=0;.      }
23e90 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63  .      while( (c
23e91 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
23e92 65 61 64 28 7a 53 74 72 69 6e 67 2c 26 7a 53 74  ead(zString,&zSt
23e93 72 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20 20 20  ring))!=0 ){.   
23e94 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20       if( noCase 
23e95 29 7b 0a 20 20 20 20 20 20 20 20 20 20 47 6c 6f  ){.          Glo
23e96 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 32  gUpperToLower(c2
23e97 29 3b 0a 20 20 20 20 20 20 20 20 20 20 47 6c 6f  );.          Glo
23e98 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 29  gUpperToLower(c)
23e99 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  ;.          whil
23e9a 65 28 20 63 32 20 21 3d 20 30 20 26 26 20 63 32  e( c2 != 0 && c2
23e9b 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20   != c ){.       
23e9c 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65       c2 = sqlite
23e9d 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e  3Utf8Read(zStrin
23e9e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20  g, &zString);.  
23e9f 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70            GlogUp
23ea0 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a  perToLower(c2);.
23ea1 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
23ea2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23ea3 20 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 21       while( c2 !
23ea4 3d 20 30 20 26 26 20 63 32 20 21 3d 20 63 20 29  = 0 && c2 != c )
23ea5 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 32  {.            c2
23ea6 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
23ea7 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74  ad(zString, &zSt
23ea8 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20  ring);.         
23ea9 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
23eaa 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20 29       if( c2==0 )
23eab 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
23eac 20 20 20 69 66 28 20 70 61 74 74 65 72 6e 43 6f     if( patternCo
23ead 6d 70 61 72 65 28 7a 50 61 74 74 65 72 6e 2c 7a  mpare(zPattern,z
23eae 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 63  String,pInfo,esc
23eaf 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
23eb0 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
23eb1 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20  rn 0;.    }else 
23eb2 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20  if( !prevEscape 
23eb3 26 26 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 29  && c==matchOne )
23eb4 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
23eb5 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72  te3Utf8Read(zStr
23eb6 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d  ing, &zString)==
23eb7 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  0 ){.        ret
23eb8 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
23eb9 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d     }else if( c==
23eba 6d 61 74 63 68 53 65 74 20 29 7b 0a 20 20 20 20  matchSet ){.    
23ebb 20 20 69 6e 74 20 70 72 69 6f 72 5f 63 20 3d 20    int prior_c = 
23ebc 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  0;.      assert(
23ebd 20 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 2f 2a   esc==0 );    /*
23ebe 20 54 68 69 73 20 6f 6e 6c 79 20 6f 63 63 75 72   This only occur
23ebf 73 20 66 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74 20  s for GLOB, not 
23ec0 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 73 65  LIKE */.      se
23ec1 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e  en = 0;.      in
23ec2 76 65 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  vert = 0;.      
23ec3 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  c = sqlite3Utf8R
23ec4 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53  ead(zString, &zS
23ec5 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66  tring);.      if
23ec6 28 20 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( c==0 ) return 
23ec7 30 3b 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71  0;.      c2 = sq
23ec8 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50  lite3Utf8Read(zP
23ec9 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72  attern, &zPatter
23eca 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 32  n);.      if( c2
23ecb 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20 20 20  =='^' ){.       
23ecc 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20 20   invert = 1;.   
23ecd 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65       c2 = sqlite
23ece 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65  3Utf8Read(zPatte
23ecf 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a  rn, &zPattern);.
23ed0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
23ed1 28 20 63 32 3d 3d 27 5d 27 20 29 7b 0a 20 20 20  ( c2==']' ){.   
23ed2 20 20 20 20 20 69 66 28 20 63 3d 3d 27 5d 27 20       if( c==']' 
23ed3 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20  ) seen = 1;.    
23ed4 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33      c2 = sqlite3
23ed5 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
23ed6 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  n, &zPattern);. 
23ed7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69       }.      whi
23ed8 6c 65 28 20 63 32 20 26 26 20 63 32 21 3d 27 5d  le( c2 && c2!=']
23ed9 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ' ){.        if(
23eda 20 63 32 3d 3d 27 2d 27 20 26 26 20 7a 50 61 74   c2=='-' && zPat
23edb 74 65 72 6e 5b 30 5d 21 3d 27 5d 27 20 26 26 20  tern[0]!=']' && 
23edc 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 30 20 26  zPattern[0]!=0 &
23edd 26 20 70 72 69 6f 72 5f 63 3e 30 20 29 7b 0a 20  & prior_c>0 ){. 
23ede 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71           c2 = sq
23edf 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50  lite3Utf8Read(zP
23ee0 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72  attern, &zPatter
23ee1 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  n);.          if
23ee2 28 20 63 3e 3d 70 72 69 6f 72 5f 63 20 26 26 20  ( c>=prior_c && 
23ee3 63 3c 3d 63 32 20 29 20 73 65 65 6e 20 3d 20 31  c<=c2 ) seen = 1
23ee4 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 69 6f  ;.          prio
23ee5 72 5f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  r_c = 0;.       
23ee6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
23ee7 20 20 69 66 28 20 63 3d 3d 63 32 20 29 7b 0a 20    if( c==c2 ){. 
23ee8 20 20 20 20 20 20 20 20 20 20 20 73 65 65 6e 20             seen 
23ee9 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 1;.          }
23eea 0a 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72  .          prior
23eeb 5f 63 20 3d 20 63 32 3b 0a 20 20 20 20 20 20 20  _c = c2;.       
23eec 20 7d 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20   }.        c2 = 
23eed 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
23eee 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74  zPattern, &zPatt
23eef 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ern);.      }.  
23ef0 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c      if( c2==0 ||
23ef1 20 28 73 65 65 6e 20 5e 20 69 6e 76 65 72 74 29   (seen ^ invert)
23ef2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
23ef3 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
23ef4 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65  .    }else if( e
23ef5 73 63 3d 3d 63 20 26 26 20 21 70 72 65 76 45 73  sc==c && !prevEs
23ef6 63 61 70 65 20 29 7b 0a 20 20 20 20 20 20 70 72  cape ){.      pr
23ef7 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 20 20  evEscape = 1;.  
23ef8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
23ef9 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
23efa 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53  ead(zString, &zS
23efb 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66  tring);.      if
23efc 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20  ( noCase ){.    
23efd 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c      GlogUpperToL
23efe 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20 20  ower(c);.       
23eff 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
23f00 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r(c2);.      }. 
23f01 20 20 20 20 20 69 66 28 20 63 21 3d 63 32 20 29       if( c!=c2 )
23f02 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
23f03 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
23f04 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30    prevEscape = 0
23f05 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
23f06 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d 30  turn *zString==0
23f07 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74  ;.}../*.** Count
23f08 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
23f09 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 4c 49  imes that the LI
23f0a 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f 72 20  KE operator (or 
23f0b 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a 2a 2a  GLOB which is.**
23f0c 20 6a 75 73 74 20 61 20 76 61 72 69 61 74 69 6f   just a variatio
23f0d 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 73 20  n of LIKE) gets 
23f0e 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73  called.  This is
23f0f 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
23f10 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69  g.** only..*/.#i
23f11 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
23f12 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
23f13 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f 75  sqlite3_like_cou
23f14 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
23f15 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
23f16 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b  ation of the lik
23f17 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  e() SQL function
23f18 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  .  This function
23f19 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74   implements.** t
23f1a 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45  he build-in LIKE
23f1b 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20   operator.  The 
23f1c 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
23f1d 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  o the function i
23f1e 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e  s the.** pattern
23f1f 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
23f20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
23f21 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65  string.  So, the
23f22 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a   SQL statements:
23f23 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c  .**.**       A L
23f24 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69  IKE B.**.** is i
23f25 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69  mplemented as li
23f26 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20 54  ke(B,A)..**.** T
23f27 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74 69 6f  his same functio
23f28 6e 20 28 77 69 74 68 20 61 20 64 69 66 66 65 72  n (with a differ
23f29 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  ent compareInfo 
23f2a 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d 70 75  structure) compu
23f2b 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f 42 20  tes.** the GLOB 
23f2c 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61  operator..*/.sta
23f2d 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46 75 6e  tic void likeFun
23f2e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
23f2f 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
23f30 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73    int argc, .  s
23f31 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
23f32 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75  rgv.){.  const u
23f33 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41  nsigned char *zA
23f34 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65 73 63  , *zB;.  int esc
23f35 61 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  ape = 0;.  int n
23f36 50 61 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Pat;.  sqlite3 *
23f37 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
23f38 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
23f39 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 7a 42 20 3d  ontext);..  zB =
23f3a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
23f3b 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
23f3c 7a 41 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  zA = sqlite3_val
23f3d 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
23f3e 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68  ;..  /* Limit th
23f3f 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
23f40 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74  LIKE or GLOB pat
23f41 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70 72  tern to avoid pr
23f42 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 64  oblems.  ** of d
23f43 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61 6e  eep recursion an
23f44 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20 69  d N*N behavior i
23f45 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65  n patternCompare
23f46 28 29 2e 0a 20 20 2a 2f 0a 20 20 6e 50 61 74 20  ()..  */.  nPat 
23f47 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
23f48 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
23f49 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 74    testcase( nPat
23f4a 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ==db->aLimit[SQL
23f4b 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50  ITE_LIMIT_LIKE_P
23f4c 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20 29  ATTERN_LENGTH] )
23f4d 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50  ;.  testcase( nP
23f4e 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  at==db->aLimit[S
23f4f 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45  QLITE_LIMIT_LIKE
23f50 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d  _PATTERN_LENGTH]
23f51 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 50 61 74  +1 );.  if( nPat
23f52 20 3e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51   > db->aLimit[SQ
23f53 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f  LITE_LIMIT_LIKE_
23f54 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20  PATTERN_LENGTH] 
23f55 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
23f56 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
23f57 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47 4c  ext, "LIKE or GL
23f58 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20 63  OB pattern too c
23f59 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20 20  omplex", -1);.  
23f5a 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
23f5b 61 73 73 65 72 74 28 20 7a 42 3d 3d 73 71 6c 69  assert( zB==sqli
23f5c 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
23f5d 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 45  rgv[0]) );  /* E
23f5e 6e 63 6f 64 69 6e 67 20 64 69 64 20 6e 6f 74 20  ncoding did not 
23f5f 63 68 61 6e 67 65 20 2a 2f 0a 0a 20 20 69 66 28  change */..  if(
23f60 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20   argc==3 ){.    
23f61 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 68  /* The escape ch
23f62 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 6d  aracter string m
23f63 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 61  ust consist of a
23f64 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68   single UTF-8 ch
23f65 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20  aracter..    ** 
23f66 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
23f67 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20  n an error..    
23f68 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  */.    const uns
23f69 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 73 63  igned char *zEsc
23f6a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
23f6b 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a  _text(argv[2]);.
23f6c 20 20 20 20 69 66 28 20 7a 45 73 63 3d 3d 30 20      if( zEsc==0 
23f6d 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66  ) return;.    if
23f6e 28 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61  ( sqlite3Utf8Cha
23f6f 72 4c 65 6e 28 28 63 68 61 72 2a 29 7a 45 73 63  rLen((char*)zEsc
23f70 2c 20 2d 31 29 21 3d 31 20 29 7b 0a 20 20 20 20  , -1)!=1 ){.    
23f71 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23f72 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
23f73 0a 20 20 20 20 20 20 20 20 20 20 22 45 53 43 41  .          "ESCA
23f74 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75  PE expression mu
23f75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63  st be a single c
23f76 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b 0a  haracter", -1);.
23f77 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
23f78 20 20 7d 0a 20 20 20 20 65 73 63 61 70 65 20 3d    }.    escape =
23f79 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
23f7a 28 7a 45 73 63 2c 20 26 7a 45 73 63 29 3b 0a 20  (zEsc, &zEsc);. 
23f7b 20 7d 0a 20 20 69 66 28 20 7a 41 20 26 26 20 7a   }.  if( zA && z
23f7c 42 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  B ){.    struct 
23f7d 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e  compareInfo *pIn
23f7e 66 6f 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65  fo = sqlite3_use
23f7f 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b  r_data(context);
23f80 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
23f81 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  EST.    sqlite3_
23f82 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  like_count++;.#e
23f83 6e 64 69 66 0a 20 20 20 20 0a 20 20 20 20 73 71  ndif.    .    sq
23f84 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
23f85 28 63 6f 6e 74 65 78 74 2c 20 70 61 74 74 65 72  (context, patter
23f86 6e 43 6f 6d 70 61 72 65 28 7a 42 2c 20 7a 41 2c  nCompare(zB, zA,
23f87 20 70 49 6e 66 6f 2c 20 65 73 63 61 70 65 29 29   pInfo, escape))
23f88 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
23f89 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
23f8a 20 74 68 65 20 4e 55 4c 4c 49 46 28 78 2c 79 29   the NULLIF(x,y)
23f8b 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
23f8c 72 65 73 75 6c 74 20 69 73 20 74 68 65 20 66 69  result is the fi
23f8d 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  rst.** argument 
23f8e 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73  if the arguments
23f8f 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 20   are different. 
23f90 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   The result is N
23f91 55 4c 4c 20 69 66 20 74 68 65 0a 2a 2a 20 61 72  ULL if the.** ar
23f92 67 75 6d 65 6e 74 73 20 61 72 65 20 65 71 75 61  guments are equa
23f93 6c 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 2e  l to each other.
23f94 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
23f95 6e 75 6c 6c 69 66 46 75 6e 63 28 0a 20 20 73 71  nullifFunc(.  sq
23f96 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
23f97 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
23f98 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
23f99 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
23f9a 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
23f9b 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75  l = sqlite3GetFu
23f9c 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78  ncCollSeq(contex
23f9d 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  t);.  UNUSED_PAR
23f9e 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
23f9f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d 65  .  if( sqlite3Me
23fa0 6d 43 6f 6d 70 61 72 65 28 61 72 67 76 5b 30 5d  mCompare(argv[0]
23fa1 2c 20 61 72 67 76 5b 31 5d 2c 20 70 43 6f 6c 6c  , argv[1], pColl
23fa2 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )!=0 ){.    sqli
23fa3 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
23fa4 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30  (context, argv[0
23fa5 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ]);.  }.}../*.**
23fa6 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
23fa7 6f 66 20 74 68 65 20 56 45 52 53 49 4f 4e 28 2a  of the VERSION(*
23fa8 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  ) function.  The
23fa9 20 72 65 73 75 6c 74 20 69 73 20 74 68 65 20 76   result is the v
23faa 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65  ersion.** of the
23fab 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20   SQLite library 
23fac 74 68 61 74 20 69 73 20 72 75 6e 6e 69 6e 67 2e  that is running.
23fad 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
23fae 76 65 72 73 69 6f 6e 46 75 6e 63 28 0a 20 20 73  versionFunc(.  s
23faf 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
23fb0 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
23fb1 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
23fb2 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65  3_value **NotUse
23fb3 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  d2.){.  UNUSED_P
23fb4 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
23fb5 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
23fb6 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
23fb7 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c  ext(context, sql
23fb8 69 74 65 33 5f 76 65 72 73 69 6f 6e 2c 20 2d 31  ite3_version, -1
23fb9 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
23fba 3b 0a 7d 0a 0a 2f 2a 20 41 72 72 61 79 20 66 6f  ;.}../* Array fo
23fbb 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f  r converting fro
23fbc 6d 20 68 61 6c 66 2d 62 79 74 65 73 20 28 6e 79  m half-bytes (ny
23fbd 62 62 6c 65 73 29 20 69 6e 74 6f 20 41 53 43 49  bbles) into ASCI
23fbe 49 20 68 65 78 0a 2a 2a 20 64 69 67 69 74 73 2e  I hex.** digits.
23fbf 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74   */.static const
23fc0 20 63 68 61 72 20 68 65 78 64 69 67 69 74 73 5b   char hexdigits[
23fc1 5d 20 3d 20 7b 0a 20 20 27 30 27 2c 20 27 31 27  ] = {.  '0', '1'
23fc2 2c 20 27 32 27 2c 20 27 33 27 2c 20 27 34 27 2c  , '2', '3', '4',
23fc3 20 27 35 27 2c 20 27 36 27 2c 20 27 37 27 2c 0a   '5', '6', '7',.
23fc4 20 20 27 38 27 2c 20 27 39 27 2c 20 27 41 27 2c    '8', '9', 'A',
23fc5 20 27 42 27 2c 20 27 43 27 2c 20 27 44 27 2c 20   'B', 'C', 'D', 
23fc6 27 45 27 2c 20 27 46 27 20 0a 7d 3b 0a 0a 2f 2a  'E', 'F' .};../*
23fc7 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
23fc8 20 2d 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61   - This is not a
23fc9 6e 20 6f 66 66 69 63 69 61 6c 20 66 75 6e 63 74  n official funct
23fca 69 6f 6e 2e 20 20 54 68 65 20 69 6e 74 65 72 66  ion.  The interf
23fcb 61 63 65 20 6d 61 79 0a 2a 2a 20 63 68 61 6e 67  ace may.** chang
23fcc 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f  e.  This functio
23fcd 6e 20 6d 61 79 20 64 69 73 61 70 70 65 61 72 2e  n may disappear.
23fce 20 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63    Do not write c
23fcf 6f 64 65 20 74 68 61 74 20 64 65 70 65 6e 64 73  ode that depends
23fd0 0a 2a 2a 20 6f 6e 20 74 68 69 73 20 66 75 6e 63  .** on this func
23fd1 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c  tion..**.** Impl
23fd2 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
23fd3 65 20 51 55 4f 54 45 28 29 20 66 75 6e 63 74 69  e QUOTE() functi
23fd4 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69  on.  This functi
23fd5 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c  on takes a singl
23fd6 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20  e.** argument.  
23fd7 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  If the argument 
23fd8 69 73 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 20  is numeric, the 
23fd9 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
23fda 74 68 65 20 73 61 6d 65 20 61 73 0a 2a 2a 20 74  the same as.** t
23fdb 68 65 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66  he argument.  If
23fdc 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
23fdd 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 74 75 72   NULL, the retur
23fde 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73  n value is the s
23fdf 74 72 69 6e 67 0a 2a 2a 20 22 4e 55 4c 4c 22 2e  tring.** "NULL".
23fe0 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65    Otherwise, the
23fe1 20 61 72 67 75 6d 65 6e 74 20 69 73 20 65 6e 63   argument is enc
23fe2 6c 6f 73 65 64 20 69 6e 20 73 69 6e 67 6c 65 20  losed in single 
23fe3 71 75 6f 74 65 73 20 77 69 74 68 0a 2a 2a 20 73  quotes with.** s
23fe4 69 6e 67 6c 65 2d 71 75 6f 74 65 20 65 73 63 61  ingle-quote esca
23fe5 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  pes..*/.static v
23fe6 6f 69 64 20 71 75 6f 74 65 46 75 6e 63 28 73 71  oid quoteFunc(sq
23fe7 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
23fe8 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
23fe9 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
23fea 2a 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65 72  **argv){.  asser
23feb 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20  t( argc==1 );.  
23fec 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
23fed 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68  (argc);.  switch
23fee 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
23fef 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b  type(argv[0]) ){
23ff0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
23ff1 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61  _INTEGER:.    ca
23ff2 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
23ff3 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
23ff4 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f  _result_value(co
23ff5 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b  ntext, argv[0]);
23ff6 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
23ff7 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
23ff8 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20  ITE_BLOB: {.    
23ff9 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20    char *zText = 
23ffa 30 3b 0a 20 20 20 20 20 20 63 68 61 72 20 63 6f  0;.      char co
23ffb 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c  nst *zBlob = sql
23ffc 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
23ffd 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
23ffe 69 6e 74 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69  int nBlob = sqli
23fff 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
24000 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
24001 61 73 73 65 72 74 28 20 7a 42 6c 6f 62 3d 3d 73  assert( zBlob==s
24002 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
24003 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 2f 2a  b(argv[0]) ); /*
24004 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61   No encoding cha
24005 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 65  nge */.      zTe
24006 78 74 20 3d 20 28 63 68 61 72 20 2a 29 63 6f 6e  xt = (char *)con
24007 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65  textMalloc(conte
24008 78 74 2c 20 28 32 2a 28 69 36 34 29 6e 42 6c 6f  xt, (2*(i64)nBlo
24009 62 29 2b 34 29 3b 20 0a 20 20 20 20 20 20 69 66  b)+4); .      if
2400a 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20  ( zText ){.     
2400b 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
2400c 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 6c    for(i=0; i<nBl
2400d 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ob; i++){.      
2400e 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b      zText[(i*2)+
2400f 32 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28  2] = hexdigits[(
24010 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 30  zBlob[i]>>4)&0x0
24011 46 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54  F];.          zT
24012 65 78 74 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 68  ext[(i*2)+3] = h
24013 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b  exdigits[(zBlob[
24014 69 5d 29 26 30 78 30 46 5d 3b 0a 20 20 20 20 20  i])&0x0F];.     
24015 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 54 65     }.        zTe
24016 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20  xt[(nBlob*2)+2] 
24017 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20  = '\'';.        
24018 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b  zText[(nBlob*2)+
24019 33 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  3] = '\0';.     
2401a 20 20 20 7a 54 65 78 74 5b 30 5d 20 3d 20 27 58     zText[0] = 'X
2401b 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74  ';.        zText
2401c 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20  [1] = '\'';.    
2401d 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
2401e 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
2401f 20 7a 54 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49   zText, -1, SQLI
24020 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
24021 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
24022 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20 20  ree(zText);.    
24023 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
24024 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
24025 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20  SQLITE_TEXT: {. 
24026 20 20 20 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20       int i,j;.  
24027 20 20 20 20 75 36 34 20 6e 3b 0a 20 20 20 20 20      u64 n;.     
24028 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
24029 63 68 61 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c  char *zArg = sql
2402a 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2402b 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
2402c 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20  char *z;..      
2402d 69 66 28 20 7a 41 72 67 3d 3d 30 20 29 20 72 65  if( zArg==0 ) re
2402e 74 75 72 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28  turn;.      for(
2402f 69 3d 30 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69  i=0, n=0; zArg[i
24030 5d 3b 20 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72  ]; i++){ if( zAr
24031 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b  g[i]=='\'' ) n++
24032 3b 20 7d 0a 20 20 20 20 20 20 7a 20 3d 20 63 6f  ; }.      z = co
24033 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
24034 65 78 74 2c 20 28 28 69 36 34 29 69 29 2b 28 28  ext, ((i64)i)+((
24035 69 36 34 29 6e 29 2b 33 29 3b 0a 20 20 20 20 20  i64)n)+3);.     
24036 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20   if( z ){.      
24037 20 20 7a 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a 20    z[0] = '\'';. 
24038 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20         for(i=0, 
24039 6a 3d 31 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b  j=1; zArg[i]; i+
2403a 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b  +){.          z[
2403b 6a 2b 2b 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a  j++] = zArg[i];.
2403c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 41            if( zA
2403d 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20  rg[i]=='\'' ){. 
2403e 20 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b             z[j++
2403f 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20  ] = '\'';.      
24040 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
24041 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
24042 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 7a   '\'';.        z
24043 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  [j] = 0;.       
24044 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
24045 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c  text(context, z,
24046 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65   j, sqlite3_free
24047 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
24048 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
24049 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
2404a 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
2404b 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
2404c 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  gv[0])==SQLITE_N
2404d 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ULL );.      sql
2404e 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2404f 28 63 6f 6e 74 65 78 74 2c 20 22 4e 55 4c 4c 22  (context, "NULL"
24050 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  , 4, SQLITE_STAT
24051 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  IC);.      break
24052 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
24053 2a 0a 2a 2a 20 54 68 65 20 68 65 78 28 29 20 66  *.** The hex() f
24054 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74 65 72 70  unction.  Interp
24055 72 65 74 20 74 68 65 20 61 72 67 75 6d 65 6e 74  ret the argument
24056 20 61 73 20 61 20 62 6c 6f 62 2e 20 20 52 65 74   as a blob.  Ret
24057 75 72 6e 0a 2a 2a 20 61 20 68 65 78 61 64 65 63  urn.** a hexadec
24058 69 6d 61 6c 20 72 65 6e 64 65 72 69 6e 67 20 61  imal rendering a
24059 73 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69  s text..*/.stati
2405a 63 20 76 6f 69 64 20 68 65 78 46 75 6e 63 28 0a  c void hexFunc(.
2405b 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
2405c 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
2405d 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
2405e 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
2405f 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  {.  int i, n;.  
24060 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
24061 68 61 72 20 2a 70 42 6c 6f 62 3b 0a 20 20 63 68  har *pBlob;.  ch
24062 61 72 20 2a 7a 48 65 78 2c 20 2a 7a 3b 0a 20 20  ar *zHex, *z;.  
24063 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
24064 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
24065 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 70  METER(argc);.  p
24066 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 76  Blob = sqlite3_v
24067 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
24068 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65  ]);.  n = sqlite
24069 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
2406a 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74  gv[0]);.  assert
2406b 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 33  ( pBlob==sqlite3
2406c 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76  _value_blob(argv
2406d 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65  [0]) );  /* No e
2406e 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a  ncoding change *
2406f 2f 0a 20 20 7a 20 3d 20 7a 48 65 78 20 3d 20 63  /.  z = zHex = c
24070 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
24071 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2a 32  text, ((i64)n)*2
24072 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 7a 48 65   + 1);.  if( zHe
24073 78 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  x ){.    for(i=0
24074 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 6f  ; i<n; i++, pBlo
24075 62 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  b++){.      unsi
24076 67 6e 65 64 20 63 68 61 72 20 63 20 3d 20 2a 70  gned char c = *p
24077 42 6c 6f 62 3b 0a 20 20 20 20 20 20 2a 28 7a 2b  Blob;.      *(z+
24078 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28  +) = hexdigits[(
24079 63 3e 3e 34 29 26 30 78 66 5d 3b 0a 20 20 20 20  c>>4)&0xf];.    
2407a 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69    *(z++) = hexdi
2407b 67 69 74 73 5b 63 26 30 78 66 5d 3b 0a 20 20 20  gits[c&0xf];.   
2407c 20 7d 0a 20 20 20 20 2a 7a 20 3d 20 30 3b 0a 20   }.    *z = 0;. 
2407d 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2407e 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
2407f 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71 6c 69 74  zHex, n*2, sqlit
24080 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a  e3_free);.  }.}.
24081 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 62  ./*.** The zerob
24082 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69 6f 6e 20  lob(N) function 
24083 72 65 74 75 72 6e 73 20 61 20 7a 65 72 6f 2d 66  returns a zero-f
24084 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66 20 73 69  illed blob of si
24085 7a 65 20 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73  ze N bytes..*/.s
24086 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f 62  tatic void zerob
24087 6c 6f 62 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  lobFunc(.  sqlit
24088 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
24089 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
2408a 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2408b 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 36 34   **argv.){.  i64
2408c 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   n;.  sqlite3 *d
2408d 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
2408e 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
2408f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74  ntext);.  assert
24090 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55  ( argc==1 );.  U
24091 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
24092 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73 71 6c  argc);.  n = sql
24093 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
24094 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 73  (argv[0]);.  tes
24095 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 4c  tcase( n==db->aL
24096 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
24097 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74  T_LENGTH] );.  t
24098 65 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e  estcase( n==db->
24099 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2409a 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b  MIT_LENGTH]+1 );
2409b 0a 20 20 69 66 28 20 6e 3e 64 62 2d 3e 61 4c 69  .  if( n>db->aLi
2409c 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
2409d 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
2409e 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2409f 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74  rror_toobig(cont
240a0 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ext);.  }else{. 
240a1 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
240a2 74 5f 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74 65  t_zeroblob(conte
240a3 78 74 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 7d  xt, (int)n);.  }
240a4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65  .}../*.** The re
240a5 70 6c 61 63 65 28 29 20 66 75 6e 63 74 69 6f 6e  place() function
240a6 2e 20 20 54 68 72 65 65 20 61 72 67 75 6d 65 6e  .  Three argumen
240a7 74 73 20 61 72 65 20 61 6c 6c 20 73 74 72 69 6e  ts are all strin
240a8 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 6d  gs: call.** them
240a9 20 41 2c 20 42 2c 20 61 6e 64 20 43 2e 20 54 68   A, B, and C. Th
240aa 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 73 6f  e result is also
240ab 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20   a string which 
240ac 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66 72  is derived.** fr
240ad 6f 6d 20 41 20 62 79 20 72 65 70 6c 61 63 69 6e  om A by replacin
240ae 67 20 65 76 65 72 79 20 6f 63 63 75 72 61 6e 63  g every occuranc
240af 65 20 6f 66 20 42 20 77 69 74 68 20 43 2e 20 20  e of B with C.  
240b0 54 68 65 20 6d 61 74 63 68 0a 2a 2a 20 6d 75 73  The match.** mus
240b1 74 20 62 65 20 65 78 61 63 74 2e 20 20 43 6f 6c  t be exact.  Col
240b2 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
240b3 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e 0a 2a   are not used..*
240b4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
240b5 70 6c 61 63 65 46 75 6e 63 28 0a 20 20 73 71 6c  placeFunc(.  sql
240b6 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
240b7 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
240b8 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
240b9 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
240ba 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
240bb 61 72 20 2a 7a 53 74 72 3b 20 20 20 20 20 20 20  ar *zStr;       
240bc 20 2f 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74   /* The input st
240bd 72 69 6e 67 20 41 20 2a 2f 0a 20 20 63 6f 6e 73  ring A */.  cons
240be 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
240bf 2a 7a 50 61 74 74 65 72 6e 3b 20 20 20 20 2f 2a  *zPattern;    /*
240c0 20 54 68 65 20 70 61 74 74 65 72 6e 20 73 74 72   The pattern str
240c1 69 6e 67 20 42 20 2a 2f 0a 20 20 63 6f 6e 73 74  ing B */.  const
240c2 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
240c3 7a 52 65 70 3b 20 20 20 20 20 20 20 20 2f 2a 20  zRep;        /* 
240c4 54 68 65 20 72 65 70 6c 61 63 65 6d 65 6e 74 20  The replacement 
240c5 73 74 72 69 6e 67 20 43 20 2a 2f 0a 20 20 75 6e  string C */.  un
240c6 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75  signed char *zOu
240c7 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
240c8 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 2a 2f  /* The output */
240c9 0a 20 20 69 6e 74 20 6e 53 74 72 3b 20 20 20 20  .  int nStr;    
240ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
240cb 69 7a 65 20 6f 66 20 7a 53 74 72 20 2a 2f 0a 20  ize of zStr */. 
240cc 20 69 6e 74 20 6e 50 61 74 74 65 72 6e 3b 20 20   int nPattern;  
240cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
240ce 65 20 6f 66 20 7a 50 61 74 74 65 72 6e 20 2a 2f  e of zPattern */
240cf 0a 20 20 69 6e 74 20 6e 52 65 70 3b 20 20 20 20  .  int nRep;    
240d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
240d1 69 7a 65 20 6f 66 20 7a 52 65 70 20 2a 2f 0a 20  ize of zRep */. 
240d2 20 69 36 34 20 6e 4f 75 74 3b 20 20 20 20 20 20   i64 nOut;      
240d3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
240d4 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 7a 4f 75  imum size of zOu
240d5 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 6f 70 4c  t */.  int loopL
240d6 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  imit;           
240d7 2f 2a 20 4c 61 73 74 20 7a 53 74 72 5b 5d 20 74  /* Last zStr[] t
240d8 68 61 74 20 6d 69 67 68 74 20 6d 61 74 63 68 20  hat might match 
240d9 7a 50 61 74 74 65 72 6e 5b 5d 20 2a 2f 0a 20 20  zPattern[] */.  
240da 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
240db 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
240dc 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 0a 20 20   counters */..  
240dd 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 33 20  assert( argc==3 
240de 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
240df 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a  METER(argc);.  z
240e0 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Str = sqlite3_va
240e1 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
240e2 29 3b 0a 20 20 69 66 28 20 7a 53 74 72 3d 3d 30  );.  if( zStr==0
240e3 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 53 74   ) return;.  nSt
240e4 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  r = sqlite3_valu
240e5 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
240e6 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 74 72  ;.  assert( zStr
240e7 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ==sqlite3_value_
240e8 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b  text(argv[0]) );
240e9 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67    /* No encoding
240ea 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 50 61   change */.  zPa
240eb 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ttern = sqlite3_
240ec 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
240ed 31 5d 29 3b 0a 20 20 69 66 28 20 7a 50 61 74 74  1]);.  if( zPatt
240ee 65 72 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  ern==0 ){.    as
240ef 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76 61  sert( sqlite3_va
240f0 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d  lue_type(argv[1]
240f1 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0a 20  )==SQLITE_NULL. 
240f2 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71             || sq
240f3 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
240f4 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
240f5 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
240f6 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
240f7 7d 0a 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e  }.  if( zPattern
240f8 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  [0]==0 ){.    as
240f9 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76 61  sert( sqlite3_va
240fa 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d  lue_type(argv[1]
240fb 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )!=SQLITE_NULL )
240fc 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
240fd 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65  sult_value(conte
240fe 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20  xt, argv[0]);.  
240ff 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
24100 6e 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74  nPattern = sqlit
24101 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
24102 72 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65 72  rgv[1]);.  asser
24103 74 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71 6c  t( zPattern==sql
24104 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
24105 61 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a 20  argv[1]) );  /* 
24106 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e  No encoding chan
24107 67 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20 73  ge */.  zRep = s
24108 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
24109 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69 66  t(argv[2]);.  if
2410a 28 20 7a 52 65 70 3d 3d 30 20 29 20 72 65 74 75  ( zRep==0 ) retu
2410b 72 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71 6c  rn;.  nRep = sql
2410c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2410d 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73 73  (argv[2]);.  ass
2410e 65 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69 74  ert( zRep==sqlit
2410f 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
24110 67 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75 74  gv[2]) );.  nOut
24111 20 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20 61   = nStr + 1;.  a
24112 73 73 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c 49  ssert( nOut<SQLI
24113 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b  TE_MAX_LENGTH );
24114 0a 20 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65 78  .  zOut = contex
24115 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
24116 20 28 69 36 34 29 6e 4f 75 74 29 3b 0a 20 20 69   (i64)nOut);.  i
24117 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20  f( zOut==0 ){.  
24118 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
24119 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74 72  loopLimit = nStr
2411a 20 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a 20   - nPattern;  . 
2411b 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d 6c   for(i=j=0; i<=l
2411c 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a  oopLimit; i++){.
2411d 20 20 20 20 69 66 28 20 7a 53 74 72 5b 69 5d 21      if( zStr[i]!
2411e 3d 7a 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c 20  =zPattern[0] || 
2411f 6d 65 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d 2c  memcmp(&zStr[i],
24120 20 7a 50 61 74 74 65 72 6e 2c 20 6e 50 61 74 74   zPattern, nPatt
24121 65 72 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a 4f  ern) ){.      zO
24122 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b 69  ut[j++] = zStr[i
24123 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ];.    }else{.  
24124 20 20 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20 20      u8 *zOld;.  
24125 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
24126 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
24127 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
24128 65 78 74 29 3b 0a 20 20 20 20 20 20 6e 4f 75 74  ext);.      nOut
24129 20 2b 3d 20 6e 52 65 70 20 2d 20 6e 50 61 74 74   += nRep - nPatt
2412a 65 72 6e 3b 0a 20 20 20 20 20 20 74 65 73 74 63  ern;.      testc
2412b 61 73 65 28 20 6e 4f 75 74 2d 31 3d 3d 64 62 2d  ase( nOut-1==db-
2412c 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
2412d 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a  IMIT_LENGTH] );.
2412e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2412f 6e 4f 75 74 2d 32 3d 3d 64 62 2d 3e 61 4c 69 6d  nOut-2==db->aLim
24130 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
24131 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20 20  LENGTH] );.     
24132 20 69 66 28 20 6e 4f 75 74 2d 31 3e 64 62 2d 3e   if( nOut-1>db->
24133 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
24134 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
24135 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
24136 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
24137 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ig(context);.   
24138 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
24139 65 65 28 64 62 2c 20 7a 4f 75 74 29 3b 0a 20 20  ee(db, zOut);.  
2413a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2413b 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4f 6c 64      }.      zOld
2413c 20 3d 20 7a 4f 75 74 3b 0a 20 20 20 20 20 20 7a   = zOut;.      z
2413d 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  Out = sqlite3_re
2413e 61 6c 6c 6f 63 28 7a 4f 75 74 2c 20 28 69 6e 74  alloc(zOut, (int
2413f 29 6e 4f 75 74 29 3b 0a 20 20 20 20 20 20 69 66  )nOut);.      if
24140 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20  ( zOut==0 ){.   
24141 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
24142 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
24143 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
24144 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
24145 64 62 2c 20 7a 4f 6c 64 29 3b 0a 20 20 20 20 20  db, zOld);.     
24146 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
24147 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28   }.      memcpy(
24148 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c 20  &zOut[j], zRep, 
24149 6e 52 65 70 29 3b 0a 20 20 20 20 20 20 6a 20 2b  nRep);.      j +
2414a 3d 20 6e 52 65 70 3b 0a 20 20 20 20 20 20 69 20  = nRep;.      i 
2414b 2b 3d 20 6e 50 61 74 74 65 72 6e 2d 31 3b 0a 20  += nPattern-1;. 
2414c 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
2414d 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31 3d 3d 6e  t( j+nStr-i+1==n
2414e 4f 75 74 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28  Out );.  memcpy(
2414f 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53 74 72 5b  &zOut[j], &zStr[
24150 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a 20 20 6a  i], nStr-i);.  j
24151 20 2b 3d 20 6e 53 74 72 20 2d 20 69 3b 0a 20 20   += nStr - i;.  
24152 61 73 73 65 72 74 28 20 6a 3c 3d 6e 4f 75 74 20  assert( j<=nOut 
24153 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 30  );.  zOut[j] = 0
24154 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
24155 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
24156 20 28 63 68 61 72 2a 29 7a 4f 75 74 2c 20 6a 2c   (char*)zOut, j,
24157 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
24158 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
24159 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 54  ntation of the T
2415a 52 49 4d 28 29 2c 20 4c 54 52 49 4d 28 29 2c 20  RIM(), LTRIM(), 
2415b 61 6e 64 20 52 54 52 49 4d 28 29 20 66 75 6e 63  and RTRIM() func
2415c 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 75 73  tions..** The us
2415d 65 72 64 61 74 61 20 69 73 20 30 78 31 20 66 6f  erdata is 0x1 fo
2415e 72 20 6c 65 66 74 20 74 72 69 6d 2c 20 30 78 32  r left trim, 0x2
2415f 20 66 6f 72 20 72 69 67 68 74 20 74 72 69 6d 2c   for right trim,
24160 20 30 78 33 20 66 6f 72 20 62 6f 74 68 2e 0a 2a   0x3 for both..*
24161 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72  /.static void tr
24162 69 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  imFunc(.  sqlite
24163 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
24164 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
24165 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
24166 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73  **argv.){.  cons
24167 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
24168 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  *zIn;         /*
24169 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f   Input string */
2416a 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
2416b 64 20 63 68 61 72 20 2a 7a 43 68 61 72 53 65 74  d char *zCharSet
2416c 3b 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 63  ;    /* Set of c
2416d 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 72 69  haracters to tri
2416e 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b 20  m */.  int nIn; 
2416f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24170 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
24171 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 69  er of bytes in i
24172 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  nput */.  int fl
24173 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
24174 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
24175 3a 20 74 72 69 6d 6c 65 66 74 20 20 32 3a 20 74  : trimleft  2: t
24176 72 69 6d 72 69 67 68 74 20 20 33 3a 20 74 72 69  rimright  3: tri
24177 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  m */.  int i;   
24178 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24179 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2417a 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e   counter */.  un
2417b 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4c 65  signed char *aLe
2417c 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  n = 0;          
2417d 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 65 61 63  /* Length of eac
2417e 68 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a  h character in z
2417f 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 75 6e 73  CharSet */.  uns
24180 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 61 7a 43  igned char **azC
24181 68 61 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  har = 0;       /
24182 2a 20 49 6e 64 69 76 69 64 75 61 6c 20 63 68 61  * Individual cha
24183 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61 72  racters in zChar
24184 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  Set */.  int nCh
24185 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ar;             
24186 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
24187 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
24188 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a  rs in zCharSet *
24189 2f 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  /..  if( sqlite3
2418a 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
2418b 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  [0])==SQLITE_NUL
2418c 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  L ){.    return;
2418d 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d 20 73 71 6c  .  }.  zIn = sql
2418e 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2418f 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20  argv[0]);.  if( 
24190 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  zIn==0 ) return;
24191 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69 74 65 33  .  nIn = sqlite3
24192 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
24193 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28  v[0]);.  assert(
24194 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61   zIn==sqlite3_va
24195 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
24196 29 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d  ) );.  if( argc=
24197 3d 31 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63  =1 ){.    static
24198 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
24199 63 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d 20 3d 20  char lenOne[] = 
2419a 7b 20 31 20 7d 3b 0a 20 20 20 20 73 74 61 74 69  { 1 };.    stati
2419b 63 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  c unsigned char 
2419c 2a 20 63 6f 6e 73 74 20 61 7a 4f 6e 65 5b 5d 20  * const azOne[] 
2419d 3d 20 7b 20 28 75 38 2a 29 22 20 22 20 7d 3b 0a  = { (u8*)" " };.
2419e 20 20 20 20 6e 43 68 61 72 20 3d 20 31 3b 0a 20      nChar = 1;. 
2419f 20 20 20 61 4c 65 6e 20 3d 20 28 75 38 2a 29 6c     aLen = (u8*)l
241a0 65 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a 43 68 61  enOne;.    azCha
241a1 72 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  r = (unsigned ch
241a2 61 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a 20 20 20  ar **)azOne;.   
241a3 20 7a 43 68 61 72 53 65 74 20 3d 20 30 3b 0a 20   zCharSet = 0;. 
241a4 20 7d 65 6c 73 65 20 69 66 28 20 28 7a 43 68 61   }else if( (zCha
241a5 72 53 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 76  rSet = sqlite3_v
241a6 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
241a7 5d 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ]))==0 ){.    re
241a8 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  turn;.  }else{. 
241a9 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65     const unsigne
241aa 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 66  d char *z;.    f
241ab 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e  or(z=zCharSet, n
241ac 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61  Char=0; *z; nCha
241ad 72 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c 49  r++){.      SQLI
241ae 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b  TE_SKIP_UTF8(z);
241af 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e  .    }.    if( n
241b0 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 20 20  Char>0 ){.      
241b1 61 7a 43 68 61 72 20 3d 20 63 6f 6e 74 65 78 74  azChar = context
241b2 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20  Malloc(context, 
241b3 28 28 69 36 34 29 6e 43 68 61 72 29 2a 28 73 69  ((i64)nChar)*(si
241b4 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31 29 29 3b  zeof(char*)+1));
241b5 0a 20 20 20 20 20 20 69 66 28 20 61 7a 43 68 61  .      if( azCha
241b6 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r==0 ){.        
241b7 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
241b8 20 20 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 6e        aLen = (un
241b9 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 61 7a  signed char*)&az
241ba 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20 20  Char[nChar];.   
241bb 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65     for(z=zCharSe
241bc 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20  t, nChar=0; *z; 
241bd 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20  nChar++){.      
241be 20 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 20    azChar[nChar] 
241bf 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
241c0 20 2a 29 7a 3b 0a 20 20 20 20 20 20 20 20 53 51   *)z;.        SQ
241c1 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
241c2 29 3b 0a 20 20 20 20 20 20 20 20 61 4c 65 6e 5b  );.        aLen[
241c3 6e 43 68 61 72 5d 20 3d 20 28 75 38 29 28 7a 20  nChar] = (u8)(z 
241c4 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 29  - azChar[nChar])
241c5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
241c6 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 72 3e    }.  if( nChar>
241c7 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d  0 ){.    flags =
241c8 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
241c9 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  NT(sqlite3_user_
241ca 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29 3b 0a  data(context));.
241cb 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
241cc 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65  1 ){.      while
241cd 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20  ( nIn>0 ){.     
241ce 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a     int len = 0;.
241cf 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
241d0 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a   i<nChar; i++){.
241d1 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20            len = 
241d2 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20  aLen[i];.       
241d3 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20     if( len<=nIn 
241d4 26 26 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20 61  && memcmp(zIn, a
241d5 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d 3d  zChar[i], len)==
241d6 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
241d7 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
241d8 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61   i>=nChar ) brea
241d9 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 20 2b  k;.        zIn +
241da 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6e  = len;.        n
241db 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  In -= len;.     
241dc 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
241dd 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a 20 20   flags & 2 ){.  
241de 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30      while( nIn>0
241df 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
241e0 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  len = 0;.       
241e1 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61   for(i=0; i<nCha
241e2 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  r; i++){.       
241e3 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d     len = aLen[i]
241e4 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
241e5 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63  len<=nIn && memc
241e6 6d 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d  mp(&zIn[nIn-len]
241e7 2c 61 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d  ,azChar[i],len)=
241e8 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
241e9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
241ea 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65  ( i>=nChar ) bre
241eb 61 6b 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20  ak;.        nIn 
241ec 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a  -= len;.      }.
241ed 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 43      }.    if( zC
241ee 68 61 72 53 65 74 20 29 7b 0a 20 20 20 20 20 20  harSet ){.      
241ef 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43  sqlite3_free(azC
241f0 68 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  har);.    }.  }.
241f1 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
241f2 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28  _text(context, (
241f3 63 68 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20  char*)zIn, nIn, 
241f4 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
241f5 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51  );.}...#ifdef SQ
241f6 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a  LITE_SOUNDEX./*.
241f7 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 73  ** Compute the s
241f8 6f 75 6e 64 65 78 20 65 6e 63 6f 64 69 6e 67 20  oundex encoding 
241f9 6f 66 20 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73 74  of a word..*/.st
241fa 61 74 69 63 20 76 6f 69 64 20 73 6f 75 6e 64 65  atic void sounde
241fb 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  xFunc(.  sqlite3
241fc 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
241fd 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
241fe 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
241ff 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68 61 72 20  *argv.){.  char 
24200 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f  zResult[8];.  co
24201 6e 73 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69  nst u8 *zIn;.  i
24202 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69  nt i, j;.  stati
24203 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
24204 20 63 68 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20   char iCode[] = 
24205 7b 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  {.    0, 0, 0, 0
24206 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
24207 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
24208 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20  0, 0,.    0, 0, 
24209 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2420a 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2420b 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c   0, 0, 0,.    0,
2420c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2420d 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2420e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20  , 0, 0, 0, 0,.  
2420f 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c    0, 0, 0, 0, 0,
24210 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
24211 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
24212 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32  ,.    0, 0, 1, 2
24213 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c  , 3, 0, 1, 2, 0,
24214 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20   0, 2, 2, 4, 5, 
24215 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20  5, 0,.    1, 2, 
24216 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30  6, 2, 3, 0, 1, 0
24217 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c  , 2, 0, 2, 0, 0,
24218 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c   0, 0, 0,.    0,
24219 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20   0, 1, 2, 3, 0, 
2421a 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32  1, 2, 0, 0, 2, 2
2421b 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20  , 4, 5, 5, 0,.  
2421c 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c    1, 2, 6, 2, 3,
2421d 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20   0, 1, 0, 2, 0, 
2421e 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  2, 0, 0, 0, 0, 0
2421f 2c 0a 20 20 7d 3b 0a 20 20 61 73 73 65 72 74 28  ,.  };.  assert(
24220 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49   argc==1 );.  zI
24221 6e 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33  n = (u8*)sqlite3
24222 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
24223 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d  [0]);.  if( zIn=
24224 3d 30 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29  =0 ) zIn = (u8*)
24225 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a  "";.  for(i=0; z
24226 49 6e 5b 69 5d 20 26 26 20 21 73 71 6c 69 74 65  In[i] && !sqlite
24227 33 49 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29  3Isalpha(zIn[i])
24228 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 7a  ; i++){}.  if( z
24229 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 38 20  In[i] ){.    u8 
2422a 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 64 65  prevcode = iCode
2422b 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20  [zIn[i]&0x7f];. 
2422c 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20     zResult[0] = 
2422d 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 7a  sqlite3Toupper(z
2422e 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28  In[i]);.    for(
2422f 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b  j=1; j<4 && zIn[
24230 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; i++){.      
24231 69 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65  int code = iCode
24232 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20  [zIn[i]&0x7f];. 
24233 20 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20       if( code>0 
24234 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  ){.        if( c
24235 6f 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b  ode!=prevcode ){
24236 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63  .          prevc
24237 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20  ode = code;.    
24238 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b        zResult[j+
24239 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b  +] = code + '0';
2423a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2423b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2423c 70 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20  prevcode = 0;.  
2423d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2423e 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20  while( j<4 ){.  
2423f 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d      zResult[j++]
24240 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20   = '0';.    }.  
24241 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30    zResult[j] = 0
24242 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
24243 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
24244 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53  t, zResult, 4, S
24245 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
24246 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
24247 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
24248 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30  xt(context, "?00
24249 30 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54  0", 4, SQLITE_ST
2424a 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  ATIC);.  }.}.#en
2424b 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
2424c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
2424d 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20  TENSION./*.** A 
2424e 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f  function that lo
2424f 61 64 73 20 61 20 73 68 61 72 65 64 2d 6c 69 62  ads a shared-lib
24250 72 61 72 79 20 65 78 74 65 6e 73 69 6f 6e 20 74  rary extension t
24251 68 65 6e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  hen returns NULL
24252 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
24253 20 6c 6f 61 64 45 78 74 28 73 71 6c 69 74 65 33   loadExt(sqlite3
24254 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
24255 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
24256 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
24257 76 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  v){.  const char
24258 20 2a 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74   *zFile = (const
24259 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
2425a 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
2425b 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  0]);.  const cha
2425c 72 20 2a 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69  r *zProc;.  sqli
2425d 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
2425e 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
2425f 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
24260 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20  char *zErrMsg = 
24261 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  0;..  if( argc==
24262 32 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d  2 ){.    zProc =
24263 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
24264 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
24265 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65  t(argv[1]);.  }e
24266 6c 73 65 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d  lse{.    zProc =
24267 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46   0;.  }.  if( zF
24268 69 6c 65 20 26 26 20 73 71 6c 69 74 65 33 5f 6c  ile && sqlite3_l
24269 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62  oad_extension(db
2426a 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20  , zFile, zProc, 
2426b 26 7a 45 72 72 4d 73 67 29 20 29 7b 0a 20 20 20  &zErrMsg) ){.   
2426c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2426d 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
2426e 45 72 72 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20  ErrMsg, -1);.   
2426f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
24270 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65  rrMsg);.  }.}.#e
24271 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  ndif.../*.** An 
24272 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
24273 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
24274 75 72 65 20 68 6f 6c 64 73 20 74 68 65 20 63 6f  ure holds the co
24275 6e 74 65 78 74 20 6f 66 20 61 0a 2a 2a 20 73 75  ntext of a.** su
24276 6d 28 29 20 6f 72 20 61 76 67 28 29 20 61 67 67  m() or avg() agg
24277 72 65 67 61 74 65 20 63 6f 6d 70 75 74 61 74 69  regate computati
24278 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  on..*/.typedef s
24279 74 72 75 63 74 20 53 75 6d 43 74 78 20 53 75 6d  truct SumCtx Sum
2427a 43 74 78 3b 0a 73 74 72 75 63 74 20 53 75 6d 43  Ctx;.struct SumC
2427b 74 78 20 7b 0a 20 20 64 6f 75 62 6c 65 20 72 53  tx {.  double rS
2427c 75 6d 3b 20 20 20 20 20 20 2f 2a 20 46 6c 6f 61  um;      /* Floa
2427d 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 6d 20 2a  ting point sum *
2427e 2f 0a 20 20 69 36 34 20 69 53 75 6d 3b 20 20 20  /.  i64 iSum;   
2427f 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72        /* Integer
24280 20 73 75 6d 20 2a 2f 20 20 20 0a 20 20 69 36 34   sum */   .  i64
24281 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f   cnt;          /
24282 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
24283 65 6e 74 73 20 73 75 6d 6d 65 64 20 2a 2f 0a 20  ents summed */. 
24284 20 75 38 20 6f 76 65 72 66 6c 6f 77 3b 20 20 20   u8 overflow;   
24285 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e     /* True if in
24286 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 73  teger overflow s
24287 65 65 6e 20 2a 2f 0a 20 20 75 38 20 61 70 70 72  een */.  u8 appr
24288 6f 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ox;        /* Tr
24289 75 65 20 69 66 20 6e 6f 6e 2d 69 6e 74 65 67 65  ue if non-intege
2428a 72 20 76 61 6c 75 65 20 77 61 73 20 69 6e 70 75  r value was inpu
2428b 74 20 74 6f 20 74 68 65 20 73 75 6d 20 2a 2f 0a  t to the sum */.
2428c 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  };../*.** Routin
2428d 65 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75  es used to compu
2428e 74 65 20 74 68 65 20 73 75 6d 2c 20 61 76 65 72  te the sum, aver
2428f 61 67 65 2c 20 61 6e 64 20 74 6f 74 61 6c 2e 0a  age, and total..
24290 2a 2a 0a 2a 2a 20 54 68 65 20 53 55 4d 28 29 20  **.** The SUM() 
24291 66 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73  function follows
24292 20 74 68 65 20 28 62 72 6f 6b 65 6e 29 20 53 51   the (broken) SQ
24293 4c 20 73 74 61 6e 64 61 72 64 20 77 68 69 63 68  L standard which
24294 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69   means.** that i
24295 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69  t returns NULL i
24296 66 20 69 74 20 73 75 6d 73 20 6f 76 65 72 20 6e  f it sums over n
24297 6f 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c  o inputs.  TOTAL
24298 20 72 65 74 75 72 6e 73 0a 2a 2a 20 30 2e 30 20   returns.** 0.0 
24299 69 6e 20 74 68 61 74 20 63 61 73 65 2e 20 20 49  in that case.  I
2429a 6e 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41  n addition, TOTA
2429b 4c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  L always returns
2429c 20 61 20 66 6c 6f 61 74 20 77 68 65 72 65 0a 2a   a float where.*
2429d 2a 20 53 55 4d 20 6d 69 67 68 74 20 72 65 74 75  * SUM might retu
2429e 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66  rn an integer if
2429f 20 69 74 20 6e 65 76 65 72 20 65 6e 63 6f 75 6e   it never encoun
242a0 74 65 72 73 20 61 20 66 6c 6f 61 74 69 6e 67 20  ters a floating 
242a1 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20  point.** value. 
242a2 20 54 4f 54 41 4c 20 6e 65 76 65 72 20 66 61 69   TOTAL never fai
242a3 6c 73 2c 20 62 75 74 20 53 55 4d 20 6d 69 67 68  ls, but SUM migh
242a4 74 20 74 68 72 6f 75 67 68 20 61 6e 20 65 78 63  t through an exc
242a5 65 70 74 69 6f 6e 20 69 66 0a 2a 2a 20 69 74 20  eption if.** it 
242a6 6f 76 65 72 66 6c 6f 77 73 20 61 6e 20 69 6e 74  overflows an int
242a7 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eger..*/.static 
242a8 76 6f 69 64 20 73 75 6d 53 74 65 70 28 73 71 6c  void sumStep(sql
242a9 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
242aa 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
242ab 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
242ac 2a 61 72 67 76 29 7b 0a 20 20 53 75 6d 43 74 78  *argv){.  SumCtx
242ad 20 2a 70 3b 0a 20 20 69 6e 74 20 74 79 70 65 3b   *p;.  int type;
242ae 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
242af 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =1 );.  UNUSED_P
242b0 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a  ARAMETER(argc);.
242b1 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67    p = sqlite3_ag
242b2 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
242b3 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28  context, sizeof(
242b4 2a 70 29 29 3b 0a 20 20 74 79 70 65 20 3d 20 73  *p));.  type = s
242b5 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d  qlite3_value_num
242b6 65 72 69 63 5f 74 79 70 65 28 61 72 67 76 5b 30  eric_type(argv[0
242b7 5d 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 74  ]);.  if( p && t
242b8 79 70 65 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  ype!=SQLITE_NULL
242b9 20 29 7b 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b   ){.    p->cnt++
242ba 3b 0a 20 20 20 20 69 66 28 20 74 79 70 65 3d 3d  ;.    if( type==
242bb 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29  SQLITE_INTEGER )
242bc 7b 0a 20 20 20 20 20 20 69 36 34 20 76 20 3d 20  {.      i64 v = 
242bd 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
242be 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  t64(argv[0]);.  
242bf 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76      p->rSum += v
242c0 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 2d 3e  ;.      if( (p->
242c1 61 70 70 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c  approx|p->overfl
242c2 6f 77 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ow)==0 ){.      
242c3 20 20 69 36 34 20 69 4e 65 77 53 75 6d 20 3d 20    i64 iNewSum = 
242c4 70 2d 3e 69 53 75 6d 20 2b 20 76 3b 0a 20 20 20  p->iSum + v;.   
242c5 20 20 20 20 20 69 6e 74 20 73 31 20 3d 20 28 69       int s1 = (i
242c6 6e 74 29 28 70 2d 3e 69 53 75 6d 20 3e 3e 20 28  nt)(p->iSum >> (
242c7 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29  sizeof(i64)*8-1)
242c8 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73  );.        int s
242c9 32 20 3d 20 28 69 6e 74 29 28 76 20 20 20 20 20  2 = (int)(v     
242ca 20 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34    >> (sizeof(i64
242cb 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20 20 20 20  )*8-1));.       
242cc 20 69 6e 74 20 73 33 20 3d 20 28 69 6e 74 29 28   int s3 = (int)(
242cd 69 4e 65 77 53 75 6d 20 3e 3e 20 28 73 69 7a 65  iNewSum >> (size
242ce 6f 66 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20  of(i64)*8-1));. 
242cf 20 20 20 20 20 20 20 70 2d 3e 6f 76 65 72 66 6c         p->overfl
242d0 6f 77 20 3d 20 28 28 73 31 26 73 32 26 7e 73 33  ow = ((s1&s2&~s3
242d1 29 20 7c 20 28 7e 73 31 26 7e 73 32 26 73 33 29  ) | (~s1&~s2&s3)
242d2 29 3f 31 3a 30 3b 0a 20 20 20 20 20 20 20 20 70  )?1:0;.        p
242d3 2d 3e 69 53 75 6d 20 3d 20 69 4e 65 77 53 75 6d  ->iSum = iNewSum
242d4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
242d5 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 72 53  lse{.      p->rS
242d6 75 6d 20 2b 3d 20 73 71 6c 69 74 65 33 5f 76 61  um += sqlite3_va
242d7 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
242d8 30 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70  0]);.      p->ap
242d9 70 72 6f 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  prox = 1;.    }.
242da 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
242db 64 20 73 75 6d 46 69 6e 61 6c 69 7a 65 28 73 71  d sumFinalize(sq
242dc 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
242dd 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74  ontext){.  SumCt
242de 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
242df 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
242e0 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
242e1 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  );.  if( p && p-
242e2 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 69 66  >cnt>0 ){.    if
242e3 28 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 29 7b  ( p->overflow ){
242e4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
242e5 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
242e6 65 78 74 2c 22 69 6e 74 65 67 65 72 20 6f 76 65  ext,"integer ove
242e7 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a 20 20 20 20  rflow",-1);.    
242e8 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 61 70 70  }else if( p->app
242e9 72 6f 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  rox ){.      sql
242ea 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
242eb 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 72  le(context, p->r
242ec 53 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Sum);.    }else{
242ed 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
242ee 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74  esult_int64(cont
242ef 65 78 74 2c 20 70 2d 3e 69 53 75 6d 29 3b 0a 20  ext, p->iSum);. 
242f0 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69     }.  }.}.stati
242f1 63 20 76 6f 69 64 20 61 76 67 46 69 6e 61 6c 69  c void avgFinali
242f2 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ze(sqlite3_conte
242f3 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20  xt *context){.  
242f4 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d  SumCtx *p;.  p =
242f5 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
242f6 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
242f7 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20  xt, 0);.  if( p 
242f8 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20  && p->cnt>0 ){. 
242f9 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
242fa 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74  t_double(context
242fb 2c 20 70 2d 3e 72 53 75 6d 2f 28 64 6f 75 62 6c  , p->rSum/(doubl
242fc 65 29 70 2d 3e 63 6e 74 29 3b 0a 20 20 7d 0a 7d  e)p->cnt);.  }.}
242fd 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f 74  .static void tot
242fe 61 6c 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  alFinalize(sqlit
242ff 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
24300 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a  ext){.  SumCtx *
24301 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  p;.  p = sqlite3
24302 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
24303 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  xt(context, 0);.
24304 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49    /* (double)0 I
24305 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45  n case of SQLITE
24306 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
24307 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 73 71 6c  OINT... */.  sql
24308 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
24309 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20  le(context, p ? 
2430a 70 2d 3e 72 53 75 6d 20 3a 20 28 64 6f 75 62 6c  p->rSum : (doubl
2430b 65 29 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  e)0);.}../*.** T
2430c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
2430d 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61  ucture keeps tra
2430e 63 6b 20 6f 66 20 73 74 61 74 65 20 69 6e 66 6f  ck of state info
2430f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 0a  rmation for the.
24310 2a 2a 20 63 6f 75 6e 74 28 29 20 61 67 67 72 65  ** count() aggre
24311 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  gate function..*
24312 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
24313 20 43 6f 75 6e 74 43 74 78 20 43 6f 75 6e 74 43   CountCtx CountC
24314 74 78 3b 0a 73 74 72 75 63 74 20 43 6f 75 6e 74  tx;.struct Count
24315 43 74 78 20 7b 0a 20 20 69 36 34 20 6e 3b 0a 7d  Ctx {.  i64 n;.}
24316 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  ;../*.** Routine
24317 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  s to implement t
24318 68 65 20 63 6f 75 6e 74 28 29 20 61 67 67 72 65  he count() aggre
24319 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  gate function..*
2431a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
2431b 75 6e 74 53 74 65 70 28 73 71 6c 69 74 65 33 5f  untStep(sqlite3_
2431c 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2431d 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
2431e 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
2431f 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a 70  ){.  CountCtx *p
24320 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  p = sqlite3_
24321 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
24322 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f  t(context, sizeo
24323 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 28 61  f(*p));.  if( (a
24324 72 67 63 3d 3d 30 20 7c 7c 20 53 51 4c 49 54 45  rgc==0 || SQLITE
24325 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76  _NULL!=sqlite3_v
24326 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
24327 5d 29 29 20 26 26 20 70 20 29 7b 0a 20 20 20 20  ])) && p ){.    
24328 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66  p->n++;.  }..#if
24329 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2432a 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 2f 2a  _DEPRECATED.  /*
2432b 20 54 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67   The sqlite3_agg
2432c 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 29 20 66  regate_count() f
2432d 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65  unction is depre
2432e 63 61 74 65 64 2e 20 20 42 75 74 20 6a 75 73 74  cated.  But just
2432f 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75   to make.  ** su
24330 72 65 20 69 74 20 73 74 69 6c 6c 20 6f 70 65 72  re it still oper
24331 61 74 65 73 20 63 6f 72 72 65 63 74 6c 79 2c 20  ates correctly, 
24332 76 65 72 69 66 79 20 74 68 61 74 20 69 74 73 20  verify that its 
24333 63 6f 75 6e 74 20 61 67 72 65 65 73 20 77 69 74  count agrees wit
24334 68 20 6f 75 72 20 0a 20 20 2a 2a 20 69 6e 74 65  h our .  ** inte
24335 72 6e 61 6c 20 63 6f 75 6e 74 20 77 68 65 6e 20  rnal count when 
24336 75 73 69 6e 67 20 63 6f 75 6e 74 28 2a 29 20 61  using count(*) a
24337 6e 64 20 77 68 65 6e 20 74 68 65 20 74 6f 74 61  nd when the tota
24338 6c 20 63 6f 75 6e 74 20 63 61 6e 20 62 65 0a 20  l count can be. 
24339 20 2a 2a 20 65 78 70 72 65 73 73 65 64 20 61 73   ** expressed as
2433a 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65   a 32-bit intege
2433b 72 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  r. */.  assert( 
2433c 61 72 67 63 3d 3d 31 20 7c 7c 20 70 3d 3d 30 20  argc==1 || p==0 
2433d 7c 7c 20 70 2d 3e 6e 3e 30 78 37 66 66 66 66 66  || p->n>0x7fffff
2433e 66 66 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20  ff.          || 
2433f 70 2d 3e 6e 3d 3d 73 71 6c 69 74 65 33 5f 61 67  p->n==sqlite3_ag
24340 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 63 6f  gregate_count(co
24341 6e 74 65 78 74 29 20 29 3b 0a 23 65 6e 64 69 66  ntext) );.#endif
24342 0a 7d 20 20 20 0a 73 74 61 74 69 63 20 76 6f 69  .}   .static voi
24343 64 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 28  d countFinalize(
24344 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
24345 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 43 6f 75  *context){.  Cou
24346 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20  ntCtx *p;.  p = 
24347 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
24348 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
24349 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  t, 0);.  sqlite3
2434a 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f  _result_int64(co
2434b 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d 3e 6e 20  ntext, p ? p->n 
2434c 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  : 0);.}../*.** R
2434d 6f 75 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65  outines to imple
2434e 6d 65 6e 74 20 6d 69 6e 28 29 20 61 6e 64 20 6d  ment min() and m
2434f 61 78 28 29 20 61 67 67 72 65 67 61 74 65 20 66  ax() aggregate f
24350 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61  unctions..*/.sta
24351 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 53  tic void minmaxS
24352 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  tep(.  sqlite3_c
24353 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
24354 20 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c   .  int NotUsed,
24355 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   .  sqlite3_valu
24356 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 4d 65  e **argv.){.  Me
24357 6d 20 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d 20  m *pArg  = (Mem 
24358 2a 29 61 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d  *)argv[0];.  Mem
24359 20 2a 70 42 65 73 74 3b 0a 20 20 55 4e 55 53 45   *pBest;.  UNUSE
2435a 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
2435b 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c  sed);..  if( sql
2435c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2435d 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45  argv[0])==SQLITE
2435e 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a  _NULL ) return;.
2435f 20 20 70 42 65 73 74 20 3d 20 28 4d 65 6d 20 2a    pBest = (Mem *
24360 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  )sqlite3_aggrega
24361 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
24362 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 42 65 73  xt, sizeof(*pBes
24363 74 29 29 3b 0a 20 20 69 66 28 20 21 70 42 65 73  t));.  if( !pBes
24364 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69  t ) return;..  i
24365 66 28 20 70 42 65 73 74 2d 3e 66 6c 61 67 73 20  f( pBest->flags 
24366 29 7b 0a 20 20 20 20 69 6e 74 20 6d 61 78 3b 0a  ){.    int max;.
24367 20 20 20 20 69 6e 74 20 63 6d 70 3b 0a 20 20 20      int cmp;.   
24368 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
24369 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63  = sqlite3GetFunc
2436a 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29  CollSeq(context)
2436b 3b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74  ;.    /* This st
2436c 65 70 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  ep function is u
2436d 73 65 64 20 66 6f 72 20 62 6f 74 68 20 74 68 65  sed for both the
2436e 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29   min() and max()
2436f 20 61 67 67 72 65 67 61 74 65 73 2c 0a 20 20 20   aggregates,.   
24370 20 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 64 69 66   ** the only dif
24371 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
24372 74 68 65 20 74 77 6f 20 62 65 69 6e 67 20 74 68  the two being th
24373 61 74 20 74 68 65 20 73 65 6e 73 65 20 6f 66 20  at the sense of 
24374 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 61  the.    ** compa
24375 72 69 73 6f 6e 20 69 73 20 69 6e 76 65 72 74 65  rison is inverte
24376 64 2e 20 46 6f 72 20 74 68 65 20 6d 61 78 28 29  d. For the max()
24377 20 61 67 67 72 65 67 61 74 65 2c 20 74 68 65 0a   aggregate, the.
24378 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75      ** sqlite3_u
24379 73 65 72 5f 64 61 74 61 28 29 20 66 75 6e 63 74  ser_data() funct
2437a 69 6f 6e 20 72 65 74 75 72 6e 73 20 28 76 6f 69  ion returns (voi
2437b 64 20 2a 29 2d 31 2e 20 46 6f 72 20 6d 69 6e 28  d *)-1. For min(
2437c 29 20 69 74 0a 20 20 20 20 2a 2a 20 72 65 74 75  ) it.    ** retu
2437d 72 6e 73 20 28 76 6f 69 64 20 2a 29 64 62 2c 20  rns (void *)db, 
2437e 77 68 65 72 65 20 64 62 20 69 73 20 74 68 65 20  where db is the 
2437f 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73  sqlite3* databas
24380 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20 20 20 2a  e pointer..    *
24381 2a 20 54 68 65 72 65 66 6f 72 65 20 74 68 65 20  * Therefore the 
24382 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20 73  next statement s
24383 65 74 73 20 76 61 72 69 61 62 6c 65 20 27 6d 61  ets variable 'ma
24384 78 27 20 74 6f 20 31 20 66 6f 72 20 74 68 65 20  x' to 1 for the 
24385 6d 61 78 28 29 0a 20 20 20 20 2a 2a 20 61 67 67  max().    ** agg
24386 72 65 67 61 74 65 2c 20 6f 72 20 30 20 66 6f 72  regate, or 0 for
24387 20 6d 69 6e 28 29 2e 0a 20 20 20 20 2a 2f 0a 20   min()..    */. 
24388 20 20 20 6d 61 78 20 3d 20 73 71 6c 69 74 65 33     max = sqlite3
24389 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
2438a 78 74 29 21 3d 30 3b 0a 20 20 20 20 63 6d 70 20  xt)!=0;.    cmp 
2438b 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70  = sqlite3MemComp
2438c 61 72 65 28 70 42 65 73 74 2c 20 70 41 72 67 2c  are(pBest, pArg,
2438d 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28   pColl);.    if(
2438e 20 28 6d 61 78 20 26 26 20 63 6d 70 3c 30 29 20   (max && cmp<0) 
2438f 7c 7c 20 28 21 6d 61 78 20 26 26 20 63 6d 70 3e  || (!max && cmp>
24390 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  0) ){.      sqli
24391 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70  te3VdbeMemCopy(p
24392 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 20  Best, pArg);.   
24393 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
24394 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f  sqlite3VdbeMemCo
24395 70 79 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b  py(pBest, pArg);
24396 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
24397 69 64 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a  id minMaxFinaliz
24398 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
24399 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 73  t *context){.  s
2439a 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52  qlite3_value *pR
2439b 65 73 3b 0a 20 20 70 52 65 73 20 3d 20 28 73 71  es;.  pRes = (sq
2439c 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 73 71  lite3_value *)sq
2439d 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
2439e 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  context(context,
2439f 20 30 29 3b 0a 20 20 69 66 28 20 70 52 65 73 20   0);.  if( pRes 
243a0 29 7b 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59  ){.    if( ALWAY
243a1 53 28 70 52 65 73 2d 3e 66 6c 61 67 73 29 20 29  S(pRes->flags) )
243a2 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
243a3 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e  result_value(con
243a4 74 65 78 74 2c 20 70 52 65 73 29 3b 0a 20 20 20  text, pRes);.   
243a5 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
243a6 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 52 65  beMemRelease(pRe
243a7 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  s);.  }.}../*.**
243a8 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28 45 58   group_concat(EX
243a9 50 52 2c 20 3f 53 45 50 41 52 41 54 4f 52 3f 29  PR, ?SEPARATOR?)
243aa 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
243ab 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 28  groupConcatStep(
243ac 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
243ad 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
243ae 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
243af 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
243b0 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
243b1 2a 7a 56 61 6c 3b 0a 20 20 53 74 72 41 63 63 75  *zVal;.  StrAccu
243b2 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 63 6f 6e  m *pAccum;.  con
243b3 73 74 20 63 68 61 72 20 2a 7a 53 65 70 3b 0a 20  st char *zSep;. 
243b4 20 69 6e 74 20 6e 56 61 6c 2c 20 6e 53 65 70 3b   int nVal, nSep;
243b5 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
243b6 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b  =1 || argc==2 );
243b7 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  .  if( sqlite3_v
243b8 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
243b9 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])==SQLITE_NULL 
243ba 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 41 63 63  ) return;.  pAcc
243bb 75 6d 20 3d 20 28 53 74 72 41 63 63 75 6d 2a 29  um = (StrAccum*)
243bc 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
243bd 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
243be 74 2c 20 73 69 7a 65 6f 66 28 2a 70 41 63 63 75  t, sizeof(*pAccu
243bf 6d 29 29 3b 0a 0a 20 20 69 66 28 20 70 41 63 63  m));..  if( pAcc
243c0 75 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  um ){.    sqlite
243c1 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
243c2 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
243c3 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  e(context);.    
243c4 69 6e 74 20 6e 3b 0a 20 20 20 20 70 41 63 63 75  int n;.    pAccu
243c5 6d 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31  m->useMalloc = 1
243c6 3b 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 78  ;.    pAccum->mx
243c7 41 6c 6c 6f 63 20 3d 20 64 62 2d 3e 61 4c 69 6d  Alloc = db->aLim
243c8 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
243c9 4c 45 4e 47 54 48 5d 3b 0a 23 69 66 64 65 66 20  LENGTH];.#ifdef 
243ca 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
243cb 45 43 41 54 45 44 0a 20 20 20 20 6e 20 3d 20 63  ECATED.    n = c
243cc 6f 6e 74 65 78 74 2d 3e 70 4d 65 6d 2d 3e 6e 3b  ontext->pMem->n;
243cd 0a 23 65 6c 73 65 0a 20 20 20 20 6e 20 3d 20 73  .#else.    n = s
243ce 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
243cf 5f 63 6f 75 6e 74 28 63 6f 6e 74 65 78 74 29 3b  _count(context);
243d0 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
243d1 6e 3e 31 20 29 7b 0a 20 20 20 20 20 20 69 66 28  n>1 ){.      if(
243d2 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20   argc==2 ){.    
243d3 20 20 20 20 7a 53 65 70 20 3d 20 28 63 68 61 72      zSep = (char
243d4 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
243d5 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
243d6 20 20 20 20 20 20 20 6e 53 65 70 20 3d 20 73 71         nSep = sq
243d7 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
243d8 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  s(argv[1]);.    
243d9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
243da 20 7a 53 65 70 20 3d 20 22 2c 22 3b 0a 20 20 20   zSep = ",";.   
243db 20 20 20 20 20 6e 53 65 70 20 3d 20 31 3b 0a 20       nSep = 1;. 
243dc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
243dd 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
243de 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 65 70 2c  nd(pAccum, zSep,
243df 20 6e 53 65 70 29 3b 0a 20 20 20 20 7d 0a 20 20   nSep);.    }.  
243e0 20 20 7a 56 61 6c 20 3d 20 28 63 68 61 72 2a 29    zVal = (char*)
243e1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
243e2 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
243e3 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f   nVal = sqlite3_
243e4 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
243e5 5b 30 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  [0]);.    sqlite
243e6 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
243e7 70 41 63 63 75 6d 2c 20 7a 56 61 6c 2c 20 6e 56  pAccum, zVal, nV
243e8 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  al);.  }.}.stati
243e9 63 20 76 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63  c void groupConc
243ea 61 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  atFinalize(sqlit
243eb 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
243ec 65 78 74 29 7b 0a 20 20 53 74 72 41 63 63 75 6d  ext){.  StrAccum
243ed 20 2a 70 41 63 63 75 6d 3b 0a 20 20 70 41 63 63   *pAccum;.  pAcc
243ee 75 6d 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67  um = sqlite3_agg
243ef 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
243f0 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66  ontext, 0);.  if
243f1 28 20 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 20  ( pAccum ){.    
243f2 69 66 28 20 70 41 63 63 75 6d 2d 3e 74 6f 6f 42  if( pAccum->tooB
243f3 69 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ig ){.      sqli
243f4 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
243f5 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29  _toobig(context)
243f6 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
243f7 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61  pAccum->mallocFa
243f8 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71  iled ){.      sq
243f9 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
243fa 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74  or_nomem(context
243fb 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 20 20 20  );.    }else{   
243fc 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f   .      sqlite3_
243fd 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
243fe 65 78 74 2c 20 73 71 6c 69 74 65 33 53 74 72 41  ext, sqlite3StrA
243ff 63 63 75 6d 46 69 6e 69 73 68 28 70 41 63 63 75  ccumFinish(pAccu
24400 6d 29 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20  m), -1, .       
24401 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24402 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29     sqlite3_free)
24403 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
24404 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
24405 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 61 6c  on registered al
24406 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 43  l of the above C
24407 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 51   functions as SQ
24408 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20  L.** functions. 
24409 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20   This should be 
2440a 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
2440b 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
2440c 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c  th.** external l
2440d 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54  inkage..*/.SQLIT
2440e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2440f 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75  qlite3RegisterBu
24410 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73  iltinFunctions(s
24411 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 23 69 66  qlite3 *db){.#if
24412 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24413 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 73 71  _ALTERTABLE.  sq
24414 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69  lite3AlterFuncti
24415 6f 6e 73 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a  ons(db);.#endif.
24416 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
24417 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69  cFailed ){.    i
24418 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  nt rc = sqlite3_
24419 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f  overload_functio
2441a 6e 28 64 62 2c 20 22 4d 41 54 43 48 22 2c 20 32  n(db, "MATCH", 2
2441b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  );.    assert( r
2441c 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
2441d 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  || rc==SQLITE_OK
2441e 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   );.    if( rc==
2441f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
24420 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
24421 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
24422 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c  }.  }.#ifdef SQL
24423 49 54 45 5f 53 53 45 0a 20 20 28 76 6f 69 64 29  ITE_SSE.  (void)
24424 73 71 6c 69 74 65 33 53 73 65 46 75 6e 63 74 69  sqlite3SseFuncti
24425 6f 6e 73 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a  ons(db);.#endif.
24426 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
24427 20 4c 49 4b 45 4f 50 54 20 66 6c 61 67 20 6f 6e   LIKEOPT flag on
24428 20 74 68 65 20 32 2d 61 72 67 75 6d 65 6e 74 20   the 2-argument 
24429 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
2442a 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 0a 2a 2f  e given name..*/
2442b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
2442c 4c 69 6b 65 4f 70 74 46 6c 61 67 28 73 71 6c 69  LikeOptFlag(sqli
2442d 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
2442e 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 75 38 20 66  har *zName, u8 f
2442f 6c 61 67 56 61 6c 29 7b 0a 20 20 46 75 6e 63 44  lagVal){.  FuncD
24430 65 66 20 2a 70 44 65 66 3b 0a 20 20 70 44 65 66  ef *pDef;.  pDef
24431 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
24432 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65  nction(db, zName
24433 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
24434 30 28 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20  0(zName),.      
24435 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24436 20 20 20 20 20 20 20 32 2c 20 53 51 4c 49 54 45         2, SQLITE
24437 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28  _UTF8, 0);.  if(
24438 20 41 4c 57 41 59 53 28 70 44 65 66 29 20 29 7b   ALWAYS(pDef) ){
24439 0a 20 20 20 20 70 44 65 66 2d 3e 66 6c 61 67 73  .    pDef->flags
2443a 20 3d 20 66 6c 61 67 56 61 6c 3b 0a 20 20 7d 0a   = flagVal;.  }.
2443b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
2443c 72 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 4c  r the built-in L
2443d 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 66 75 6e  IKE and GLOB fun
2443e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 63 61 73  ctions.  The cas
2443f 65 53 65 6e 73 69 74 69 76 65 0a 2a 2a 20 70 61  eSensitive.** pa
24440 72 61 6d 65 74 65 72 20 64 65 74 65 72 6d 69 6e  rameter determin
24441 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  es whether or no
24442 74 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61  t the LIKE opera
24443 74 6f 72 20 69 73 20 63 61 73 65 0a 2a 2a 20 73  tor is case.** s
24444 65 6e 73 69 74 69 76 65 2e 20 20 47 4c 4f 42 20  ensitive.  GLOB 
24445 69 73 20 61 6c 77 61 79 73 20 63 61 73 65 20 73  is always case s
24446 65 6e 73 69 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c  ensitive..*/.SQL
24447 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
24448 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
24449 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71  LikeFunctions(sq
2444a 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 63  lite3 *db, int c
2444b 61 73 65 53 65 6e 73 69 74 69 76 65 29 7b 0a 20  aseSensitive){. 
2444c 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49   struct compareI
2444d 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 69 66  nfo *pInfo;.  if
2444e 28 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 20  ( caseSensitive 
2444f 29 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28  ){.    pInfo = (
24450 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e  struct compareIn
24451 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74  fo*)&likeInfoAlt
24452 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
24453 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63  Info = (struct c
24454 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b  ompareInfo*)&lik
24455 65 49 6e 66 6f 4e 6f 72 6d 3b 0a 20 20 7d 0a 20  eInfoNorm;.  }. 
24456 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
24457 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 32  nc(db, "like", 2
24458 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 70 49  , SQLITE_ANY, pI
24459 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30  nfo, likeFunc, 0
2445a 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 43  , 0);.  sqlite3C
2445b 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 6c  reateFunc(db, "l
2445c 69 6b 65 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f  ike", 3, SQLITE_
2445d 41 4e 59 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65  ANY, pInfo, like
2445e 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73  Func, 0, 0);.  s
2445f 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
24460 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20  (db, "glob", 2, 
24461 53 51 4c 49 54 45 5f 41 4e 59 2c 20 0a 20 20 20  SQLITE_ANY, .   
24462 20 20 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61     (struct compa
24463 72 65 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e 66  reInfo*)&globInf
24464 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 30  o, likeFunc, 0,0
24465 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46  );.  setLikeOptF
24466 6c 61 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20  lag(db, "glob", 
24467 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
24468 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43   | SQLITE_FUNC_C
24469 41 53 45 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f  ASE);.  setLikeO
2446a 70 74 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b 65  ptFlag(db, "like
2446b 22 2c 20 0a 20 20 20 20 20 20 63 61 73 65 53 65  ", .      caseSe
2446c 6e 73 69 74 69 76 65 20 3f 20 28 53 51 4c 49 54  nsitive ? (SQLIT
2446d 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51  E_FUNC_LIKE | SQ
2446e 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 20  LITE_FUNC_CASE) 
2446f 3a 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49  : SQLITE_FUNC_LI
24470 4b 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45  KE);.}../*.** pE
24471 78 70 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  xpr points to an
24472 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63   expression whic
24473 68 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66  h implements a f
24474 75 6e 63 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20  unction.  If.** 
24475 69 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  it is appropriat
24476 65 20 74 6f 20 61 70 70 6c 79 20 74 68 65 20 4c  e to apply the L
24477 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  IKE optimization
24478 20 74 6f 20 74 68 61 74 20 66 75 6e 63 74 69 6f   to that functio
24479 6e 0a 2a 2a 20 74 68 65 6e 20 73 65 74 20 61 57  n.** then set aW
2447a 63 5b 30 5d 20 74 68 72 6f 75 67 68 20 61 57 63  c[0] through aWc
2447b 5b 32 5d 20 74 6f 20 74 68 65 20 77 69 6c 64 63  [2] to the wildc
2447c 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20 61  ard characters a
2447d 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 54 52 55  nd.** return TRU
2447e 45 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74  E.  If the funct
2447f 69 6f 6e 20 69 73 20 6e 6f 74 20 61 20 4c 49 4b  ion is not a LIK
24480 45 2d 73 74 79 6c 65 20 66 75 6e 63 74 69 6f 6e  E-style function
24481 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20   then.** return 
24482 46 41 4c 53 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45  FALSE..*/.SQLITE
24483 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
24484 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69  ite3IsLikeFuncti
24485 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  on(sqlite3 *db, 
24486 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
24487 20 2a 70 49 73 4e 6f 63 61 73 65 2c 20 63 68 61   *pIsNocase, cha
24488 72 20 2a 61 57 63 29 7b 0a 20 20 46 75 6e 63 44  r *aWc){.  FuncD
24489 65 66 20 2a 70 44 65 66 3b 0a 20 20 69 66 28 20  ef *pDef;.  if( 
2448a 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55  pExpr->op!=TK_FU
2448b 4e 43 54 49 4f 4e 20 0a 20 20 20 7c 7c 20 21 70  NCTION .   || !p
2448c 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 0a 20  Expr->x.pList . 
2448d 20 20 7c 7c 20 70 45 78 70 72 2d 3e 78 2e 70 4c    || pExpr->x.pL
2448e 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 0a 20 20  ist->nExpr!=2.  
2448f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
24490 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21  .  }.  assert( !
24491 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
24492 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
24493 65 63 74 29 20 29 3b 0a 20 20 70 44 65 66 20 3d  ect) );.  pDef =
24494 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
24495 74 69 6f 6e 28 64 62 2c 20 28 63 68 61 72 2a 29  tion(db, (char*)
24496 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20  pExpr->token.z, 
24497 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20  pExpr->token.n, 
24498 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  2,.             
24499 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2449a 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b  SQLITE_UTF8, 0);
2449b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 44 65  .  if( NEVER(pDe
2449c 66 3d 3d 30 29 20 7c 7c 20 28 70 44 65 66 2d 3e  f==0) || (pDef->
2449d 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
2449e 55 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a  UNC_LIKE)==0 ){.
2449f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
244a0 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 65 6d 63  }..  /* The memc
244a1 70 79 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61  py() statement a
244a2 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
244a3 77 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74  wildcard charact
244a4 65 72 73 20 61 72 65 0a 20 20 2a 2a 20 74 68 65  ers are.  ** the
244a5 20 66 69 72 73 74 20 74 68 72 65 65 20 73 74 61   first three sta
244a6 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 63  tements in the c
244a7 6f 6d 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63  ompareInfo struc
244a8 74 75 72 65 2e 20 20 54 68 65 0a 20 20 2a 2a 20  ture.  The.  ** 
244a9 61 73 73 65 72 74 73 28 29 20 74 68 61 74 20 66  asserts() that f
244aa 6f 6c 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61  ollow verify tha
244ab 74 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a  t assumption.  *
244ac 2f 0a 20 20 6d 65 6d 63 70 79 28 61 57 63 2c 20  /.  memcpy(aWc, 
244ad 70 44 65 66 2d 3e 70 55 73 65 72 44 61 74 61 2c  pDef->pUserData,
244ae 20 33 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28   3);.  assert( (
244af 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41  char*)&likeInfoA
244b0 6c 74 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69  lt == (char*)&li
244b1 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 41  keInfoAlt.matchA
244b2 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ll );.  assert( 
244b3 26 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e  &((char*)&likeIn
244b4 66 6f 41 6c 74 29 5b 31 5d 20 3d 3d 20 28 63 68  foAlt)[1] == (ch
244b5 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74  ar*)&likeInfoAlt
244b6 2e 6d 61 74 63 68 4f 6e 65 20 29 3b 0a 20 20 61  .matchOne );.  a
244b7 73 73 65 72 74 28 20 26 28 28 63 68 61 72 2a 29  ssert( &((char*)
244b8 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d  &likeInfoAlt)[2]
244b9 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65   == (char*)&like
244ba 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74  InfoAlt.matchSet
244bb 20 29 3b 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65   );.  *pIsNocase
244bc 20 3d 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20   = (pDef->flags 
244bd 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41  & SQLITE_FUNC_CA
244be 53 45 29 3d 3d 30 3b 0a 20 20 72 65 74 75 72 6e  SE)==0;.  return
244bf 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c   1;.}../*.** All
244c0 20 61 6c 6c 20 6f 66 20 74 68 65 20 46 75 6e 63   all of the Func
244c1 44 65 66 20 73 74 72 75 63 74 75 72 65 73 20 69  Def structures i
244c2 6e 20 74 68 65 20 61 42 75 69 6c 74 69 6e 46 75  n the aBuiltinFu
244c3 6e 63 5b 5d 20 61 72 72 61 79 20 61 62 6f 76 65  nc[] array above
244c4 0a 2a 2a 20 74 6f 20 74 68 65 20 67 6c 6f 62 61  .** to the globa
244c5 6c 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 68 20  l function hash 
244c6 74 61 62 6c 65 2e 20 20 54 68 69 73 20 6f 63 63  table.  This occ
244c7 75 72 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d  urs at start-tim
244c8 65 20 28 61 73 0a 2a 2a 20 61 20 63 6f 6e 73 65  e (as.** a conse
244c9 71 75 65 6e 63 65 20 6f 66 20 63 61 6c 6c 69 6e  quence of callin
244ca 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  g sqlite3_initia
244cb 6c 69 7a 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41  lize())..**.** A
244cc 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e  fter this routin
244cd 65 20 72 75 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45  e runs.*/.SQLITE
244ce 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
244cf 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f  lite3RegisterGlo
244d0 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69  balFunctions(voi
244d1 64 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  d){.  /*.  ** Th
244d2 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61  e following arra
244d3 79 20 68 6f 6c 64 73 20 46 75 6e 63 44 65 66 20  y holds FuncDef 
244d4 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 61  structures for a
244d5 6c 6c 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  ll of the functi
244d6 6f 6e 73 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64  ons.  ** defined
244d7 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 20   in this file.. 
244d8 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61 72 72   **.  ** The arr
244d9 61 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e  ay cannot be con
244da 73 74 61 6e 74 20 73 69 6e 63 65 20 63 68 61 6e  stant since chan
244db 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20  ges are made to 
244dc 74 68 65 0a 20 20 2a 2a 20 46 75 6e 63 44 65 66  the.  ** FuncDef
244dd 2e 70 48 61 73 68 20 65 6c 65 6d 65 6e 74 73 20  .pHash elements 
244de 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20  at start-time.  
244df 54 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  The elements of 
244e0 74 68 69 73 20 61 72 72 61 79 0a 20 20 2a 2a 20  this array.  ** 
244e1 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66  are read-only af
244e2 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ter initializati
244e3 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a  on is complete..
244e4 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 53 51    */.  static SQ
244e5 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66  LITE_WSD FuncDef
244e6 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20   aBuiltinFunc[] 
244e7 3d 20 7b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  = {.    FUNCTION
244e8 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20  (ltrim,         
244e9 20 20 20 20 20 31 2c 20 31 2c 20 30 2c 20 74 72       1, 1, 0, tr
244ea 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29  imFunc         )
244eb 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c  ,.    FUNCTION(l
244ec 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20  trim,           
244ed 20 20 20 32 2c 20 31 2c 20 30 2c 20 74 72 69 6d     2, 1, 0, trim
244ee 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a  Func         ),.
244ef 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72      FUNCTION(rtr
244f0 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  im,             
244f1 20 31 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75   1, 2, 0, trimFu
244f2 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20  nc         ),.  
244f3 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d    FUNCTION(rtrim
244f4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32  ,              2
244f5 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63  , 2, 0, trimFunc
244f6 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
244f7 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20  FUNCTION(trim,  
244f8 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
244f9 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20  3, 0, trimFunc  
244fa 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
244fb 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20  NCTION(trim,    
244fc 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 33 2c             2, 3,
244fd 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20   0, trimFunc    
244fe 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
244ff 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20  TION(min,       
24500 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 31          -1, 0, 1
24501 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20 20 20  , minmaxFunc    
24502 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
24503 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20  ON(min,         
24504 20 20 20 20 20 20 20 30 2c 20 30 2c 20 31 2c 20         0, 0, 1, 
24505 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
24506 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54   ),.    AGGREGAT
24507 45 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20  E(min,          
24508 20 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 6d 69       1, 0, 1, mi
24509 6e 6d 61 78 53 74 65 70 2c 20 20 20 20 20 20 6d  nmaxStep,      m
2450a 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c  inMaxFinalize ),
2450b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61  .    FUNCTION(ma
2450c 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
2450d 20 2d 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61   -1, 1, 1, minma
2450e 78 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  xFunc       ),. 
2450f 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c     FUNCTION(max,
24510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24511 30 2c 20 31 2c 20 31 2c 20 30 20 20 20 20 20 20  0, 1, 1, 0      
24512 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20            ),.   
24513 20 41 47 47 52 45 47 41 54 45 28 6d 61 78 2c 20   AGGREGATE(max, 
24514 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
24515 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65   1, 1, minmaxSte
24516 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69  p,      minMaxFi
24517 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46 55  nalize ),.    FU
24518 4e 43 54 49 4f 4e 28 74 79 70 65 6f 66 2c 20 20  NCTION(typeof,  
24519 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
2451a 20 30 2c 20 74 79 70 65 6f 66 46 75 6e 63 20 20   0, typeofFunc  
2451b 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
2451c 54 49 4f 4e 28 6c 65 6e 67 74 68 2c 20 20 20 20  TION(length,    
2451d 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
2451e 2c 20 6c 65 6e 67 74 68 46 75 6e 63 20 20 20 20  , lengthFunc    
2451f 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
24520 4f 4e 28 73 75 62 73 74 72 2c 20 20 20 20 20 20  ON(substr,      
24521 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20         2, 0, 0, 
24522 73 75 62 73 74 72 46 75 6e 63 20 20 20 20 20 20  substrFunc      
24523 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
24524 28 73 75 62 73 74 72 2c 20 20 20 20 20 20 20 20  (substr,        
24525 20 20 20 20 20 33 2c 20 30 2c 20 30 2c 20 73 75       3, 0, 0, su
24526 62 73 74 72 46 75 6e 63 20 20 20 20 20 20 20 29  bstrFunc       )
24527 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 61  ,.    FUNCTION(a
24528 62 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  bs,             
24529 20 20 20 31 2c 20 30 2c 20 30 2c 20 61 62 73 46     1, 0, 0, absF
2452a 75 6e 63 20 20 20 20 20 20 20 20 20 20 29 2c 0a  unc          ),.
2452b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2452c 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
2452d 4e 54 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  NT.    FUNCTION(
2452e 72 6f 75 6e 64 2c 20 20 20 20 20 20 20 20 20 20  round,          
2452f 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 72 6f 75      1, 0, 0, rou
24530 6e 64 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c  ndFunc        ),
24531 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f  .    FUNCTION(ro
24532 75 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  und,            
24533 20 20 32 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64    2, 0, 0, round
24534 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 23  Func        ),.#
24535 65 6e 64 69 66 0a 20 20 20 20 46 55 4e 43 54 49  endif.    FUNCTI
24536 4f 4e 28 75 70 70 65 72 2c 20 20 20 20 20 20 20  ON(upper,       
24537 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
24538 75 70 70 65 72 46 75 6e 63 20 20 20 20 20 20 20  upperFunc       
24539 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
2453a 28 6c 6f 77 65 72 2c 20 20 20 20 20 20 20 20 20  (lower,         
2453b 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 6f       1, 0, 0, lo
2453c 77 65 72 46 75 6e 63 20 20 20 20 20 20 20 20 29  werFunc        )
2453d 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63  ,.    FUNCTION(c
2453e 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20  oalesce,        
2453f 20 20 20 31 2c 20 30 2c 20 30 2c 20 30 20 20 20     1, 0, 0, 0   
24540 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a               ),.
24541 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61      FUNCTION(coa
24542 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 20  lesce,          
24543 2d 31 2c 20 30 2c 20 30 2c 20 69 66 6e 75 6c 6c  -1, 0, 0, ifnull
24544 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20  Func       ),.  
24545 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65    FUNCTION(coale
24546 73 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 30  sce,           0
24547 2c 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20  , 0, 0, 0       
24548 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
24549 46 55 4e 43 54 49 4f 4e 28 68 65 78 2c 20 20 20  FUNCTION(hex,   
2454a 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
2454b 30 2c 20 30 2c 20 68 65 78 46 75 6e 63 20 20 20  0, 0, hexFunc   
2454c 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
2454d 4e 43 54 49 4f 4e 28 69 66 6e 75 6c 6c 2c 20 20  NCTION(ifnull,  
2454e 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c             2, 0,
2454f 20 31 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 20 20   1, ifnullFunc  
24550 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
24551 54 49 4f 4e 28 72 61 6e 64 6f 6d 2c 20 20 20 20  TION(random,    
24552 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30           0, 0, 0
24553 2c 20 72 61 6e 64 6f 6d 46 75 6e 63 20 20 20 20  , randomFunc    
24554 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
24555 4f 4e 28 72 61 6e 64 6f 6d 62 6c 6f 62 2c 20 20  ON(randomblob,  
24556 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
24557 72 61 6e 64 6f 6d 42 6c 6f 62 20 20 20 20 20 20  randomBlob      
24558 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
24559 28 6e 75 6c 6c 69 66 2c 20 20 20 20 20 20 20 20  (nullif,        
2455a 20 20 20 20 20 32 2c 20 30 2c 20 31 2c 20 6e 75       2, 0, 1, nu
2455b 6c 6c 69 66 46 75 6e 63 20 20 20 20 20 20 20 29  llifFunc       )
2455c 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73  ,.    FUNCTION(s
2455d 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e 2c 20 20  qlite_version,  
2455e 20 20 20 30 2c 20 30 2c 20 30 2c 20 76 65 72 73     0, 0, 0, vers
2455f 69 6f 6e 46 75 6e 63 20 20 20 20 20 20 29 2c 0a  ionFunc      ),.
24560 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 71 75 6f      FUNCTION(quo
24561 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
24562 20 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74 65 46   1, 0, 0, quoteF
24563 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20  unc        ),.  
24564 20 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73 74 5f    FUNCTION(last_
24565 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20 20 30  insert_rowid,  0
24566 2c 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69 6e 73  , 0, 0, last_ins
24567 65 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20 20 20  ert_rowid),.    
24568 46 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67 65 73  FUNCTION(changes
24569 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20  ,            0, 
2456a 30 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 20 20  0, 0, changes   
2456b 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
2456c 4e 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63 68 61  NCTION(total_cha
2456d 6e 67 65 73 2c 20 20 20 20 20 20 30 2c 20 30 2c  nges,      0, 0,
2456e 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65   0, total_change
2456f 73 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43  s    ),.    FUNC
24570 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20 20 20  TION(replace,   
24571 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30           3, 0, 0
24572 2c 20 72 65 70 6c 61 63 65 46 75 6e 63 20 20 20  , replaceFunc   
24573 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
24574 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20 20 20  ON(zeroblob,    
24575 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
24576 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20 20  zeroblobFunc    
24577 20 29 2c 0a 20 20 23 69 66 64 65 66 20 53 51 4c   ),.  #ifdef SQL
24578 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 20 20  ITE_SOUNDEX.    
24579 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64 65 78  FUNCTION(soundex
2457a 2c 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20  ,            1, 
2457b 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46 75 6e  0, 0, soundexFun
2457c 63 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64  c      ),.  #end
2457d 69 66 0a 20 20 23 69 66 6e 64 65 66 20 53 51 4c  if.  #ifndef SQL
2457e 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
2457f 54 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55 4e 43  TENSION.    FUNC
24580 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73  TION(load_extens
24581 69 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c 20 30  ion,     1, 0, 0
24582 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20  , loadExt       
24583 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
24584 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  ON(load_extensio
24585 6e 2c 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20  n,     2, 0, 0, 
24586 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 20 20  loadExt         
24587 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 20   ),.  #endif.   
24588 20 41 47 47 52 45 47 41 54 45 28 73 75 6d 2c 20   AGGREGATE(sum, 
24589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
2458a 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20   0, 0, sumStep, 
2458b 20 20 20 20 20 20 20 20 73 75 6d 46 69 6e 61 6c          sumFinal
2458c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47  ize    ),.    AG
2458d 47 52 45 47 41 54 45 28 74 6f 74 61 6c 2c 20 20  GREGATE(total,  
2458e 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
2458f 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20   0, sumStep,    
24590 20 20 20 20 20 74 6f 74 61 6c 46 69 6e 61 6c 69       totalFinali
24591 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47  ze    ),.    AGG
24592 52 45 47 41 54 45 28 61 76 67 2c 20 20 20 20 20  REGATE(avg,     
24593 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
24594 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20  0, sumStep,     
24595 20 20 20 20 61 76 67 46 69 6e 61 6c 69 7a 65 20      avgFinalize 
24596 20 20 20 29 2c 0a 20 2f 2a 20 41 47 47 52 45 47     ),. /* AGGREG
24597 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20  ATE(count,      
24598 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20         0, 0, 0, 
24599 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20  countStep,      
2459a 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20   countFinalize  
2459b 29 2c 20 2a 2f 0a 20 20 20 20 7b 30 2c 53 51 4c  ), */.    {0,SQL
2459c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f  ITE_UTF8,SQLITE_
2459d 46 55 4e 43 5f 43 4f 55 4e 54 2c 30 2c 30 2c 30  FUNC_COUNT,0,0,0
2459e 2c 63 6f 75 6e 74 53 74 65 70 2c 63 6f 75 6e 74  ,countStep,count
2459f 46 69 6e 61 6c 69 7a 65 2c 22 63 6f 75 6e 74 22  Finalize,"count"
245a0 2c 30 7d 2c 0a 20 20 20 20 41 47 47 52 45 47 41  ,0},.    AGGREGA
245a1 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 20  TE(count,       
245a2 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 63        1, 0, 0, c
245a3 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 20  ountStep,       
245a4 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29  countFinalize  )
245a5 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28  ,.    AGGREGATE(
245a6 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20 20  group_concat,   
245a7 20 20 20 31 2c 20 30 2c 20 30 2c 20 67 72 6f 75     1, 0, 0, grou
245a8 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72 6f  pConcatStep, gro
245a9 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65  upConcatFinalize
245aa 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45  ),.    AGGREGATE
245ab 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20  (group_concat,  
245ac 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 67 72 6f      2, 0, 0, gro
245ad 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72  upConcatStep, gr
245ae 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a  oupConcatFinaliz
245af 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b 45 46  e),.  .    LIKEF
245b0 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26 67 6c  UNC(glob, 2, &gl
245b1 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45 5f 46  obInfo, SQLITE_F
245b2 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f  UNC_LIKE|SQLITE_
245b3 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 23 69  FUNC_CASE),.  #i
245b4 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41 53 45  fdef SQLITE_CASE
245b5 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 0a  _SENSITIVE_LIKE.
245b6 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b      LIKEFUNC(lik
245b7 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 6f 41  e, 2, &likeInfoA
245b8 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  lt, SQLITE_FUNC_
245b9 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43  LIKE|SQLITE_FUNC
245ba 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c 49 4b 45  _CASE),.    LIKE
245bb 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 26 6c  FUNC(like, 3, &l
245bc 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c 49  ikeInfoAlt, SQLI
245bd 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c  TE_FUNC_LIKE|SQL
245be 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a  ITE_FUNC_CASE),.
245bf 20 20 23 65 6c 73 65 0a 20 20 20 20 4c 49 4b 45    #else.    LIKE
245c0 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 6c  FUNC(like, 2, &l
245c1 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c  ikeInfoNorm, SQL
245c2 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a  ITE_FUNC_LIKE),.
245c3 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b      LIKEFUNC(lik
245c4 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e  e, 3, &likeInfoN
245c5 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43  orm, SQLITE_FUNC
245c6 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64 69 66  _LIKE),.  #endif
245c7 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 3b 0a  .  };..  int i;.
245c8 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70    FuncDefHash *p
245c9 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46  Hash = &GLOBAL(F
245ca 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69  uncDefHash, sqli
245cb 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f  te3GlobalFunctio
245cc 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a  ns);.  FuncDef *
245cd 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66  aFunc = (FuncDef
245ce 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  *)&GLOBAL(FuncDe
245cf 66 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 29  f, aBuiltinFunc)
245d0 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
245d1 41 72 72 61 79 53 69 7a 65 28 61 42 75 69 6c 74  ArraySize(aBuilt
245d2 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b 0a 20  inFunc); i++){. 
245d3 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65     sqlite3FuncDe
245d4 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26  fInsert(pHash, &
245d5 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aFunc[i]);.  }. 
245d6 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
245d7 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e  DateTimeFunction
245d8 73 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  s();.}../*******
245d9 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
245da 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  unc.c **********
245db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245dd 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
245de 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
245df 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a  le insert.c ****
245e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245e2 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
245e3 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
245e4 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
245e5 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
245e6 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
245e7 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
245e8 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
245e9 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
245ea 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
245eb 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
245ec 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
245ed 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
245ee 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
245ef 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
245f0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
245f1 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
245f2 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
245f3 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
245f4 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
245f5 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
245f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
245fa 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
245fb 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
245fc 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
245fd 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
245fe 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20  er.** to handle 
245ff 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
24600 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s in SQLite..**.
24601 2a 2a 20 24 49 64 3a 20 69 6e 73 65 72 74 2e 63  ** $Id: insert.c
24602 2c 76 20 31 2e 32 36 37 20 32 30 30 39 2f 30 35  ,v 1.267 2009/05
24603 2f 30 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e  /04 11:42:30 dan
24604 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
24605 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  /../*.** Generat
24606 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
24607 20 6f 70 65 6e 20 61 20 74 61 62 6c 65 20 66 6f   open a table fo
24608 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2f 0a 53 51  r reading..*/.SQ
24609 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2460a 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62  d sqlite3OpenTab
2460b 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 2c 20  le(.  Parse *p, 
2460c 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
2460d 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73  e code into this
2460e 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 69   VDBE */.  int i
2460f 43 75 72 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  Cur,       /* Th
24610 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  e cursor number 
24611 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
24612 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20    int iDb,      
24613 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
24614 65 20 69 6e 64 65 78 20 69 6e 20 73 71 6c 69 74  e index in sqlit
24615 65 33 2e 61 44 62 5b 5d 20 2a 2f 0a 20 20 54 61  e3.aDb[] */.  Ta
24616 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 2f 2a  ble *pTab,    /*
24617 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   The table to be
24618 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74   opened */.  int
24619 20 6f 70 63 6f 64 65 20 20 20 20 20 20 2f 2a 20   opcode      /* 
2461a 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f  OP_OpenRead or O
2461b 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0a 29  P_OpenWrite */.)
2461c 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  {.  Vdbe *v;.  i
2461d 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2461e 62 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 76  b) ) return;.  v
2461f 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
24620 65 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e(p);.  assert( 
24621 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57  opcode==OP_OpenW
24622 72 69 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d  rite || opcode==
24623 4f 50 5f 4f 70 65 6e 52 65 61 64 20 29 3b 0a 20  OP_OpenRead );. 
24624 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
24625 6b 28 70 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e  k(p, iDb, pTab->
24626 74 6e 75 6d 2c 20 28 6f 70 63 6f 64 65 3d 3d 4f  tnum, (opcode==O
24627 50 5f 4f 70 65 6e 57 72 69 74 65 29 3f 31 3a 30  P_OpenWrite)?1:0
24628 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
24629 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2462a 4f 70 33 28 76 2c 20 6f 70 63 6f 64 65 2c 20 69  Op3(v, opcode, i
2462b 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  Cur, pTab->tnum,
2462c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33   iDb);.  sqlite3
2462d 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2462e 2d 31 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  -1, SQLITE_INT_T
2462f 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c  O_PTR(pTab->nCol
24630 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  ), P4_INT32);.  
24631 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
24632 22 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  "%s", pTab->zNam
24633 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  e));.}../*.** Se
24634 74 20 50 34 20 6f 66 20 74 68 65 20 6d 6f 73 74  t P4 of the most
24635 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74   recently insert
24636 65 64 20 6f 70 63 6f 64 65 20 74 6f 20 61 20 63  ed opcode to a c
24637 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a  olumn affinity.*
24638 2a 20 73 74 72 69 6e 67 20 66 6f 72 20 69 6e 64  * string for ind
24639 65 78 20 70 49 64 78 2e 20 41 20 63 6f 6c 75 6d  ex pIdx. A colum
2463a 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  n affinity strin
2463b 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72 61 63  g has one charac
2463c 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20  ter.** for each 
2463d 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 74 61  column in the ta
2463e 62 6c 65 2c 20 61 63 63 6f 72 64 69 6e 67 20 74  ble, according t
2463f 6f 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f  o the affinity o
24640 66 20 74 68 65 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a  f the column:.**
24641 0a 2a 2a 20 20 43 68 61 72 61 63 74 65 72 20 20  .**  Character  
24642 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e      Column affin
24643 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ity.**  --------
24644 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24645 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20  ------.**  'a'  
24646 20 20 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a            TEXT.*
24647 2a 20 20 27 62 27 20 20 20 20 20 20 20 20 20 20  *  'b'          
24648 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20    NONE.**  'c'  
24649 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49            NUMERI
2464a 43 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20  C.**  'd'       
2464b 20 20 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20       INTEGER.** 
2464c 20 27 65 27 20 20 20 20 20 20 20 20 20 20 20 20   'e'            
2464d 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78  REAL.**.** An ex
2464e 74 72 61 20 27 62 27 20 69 73 20 61 70 70 65 6e  tra 'b' is appen
2464f 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ded to the end o
24650 66 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20  f the string to 
24651 63 6f 76 65 72 20 74 68 65 0a 2a 2a 20 72 6f 77  cover the.** row
24652 69 64 20 74 68 61 74 20 61 70 70 65 61 72 73 20  id that appears 
24653 61 73 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75  as the last colu
24654 6d 6e 20 69 6e 20 65 76 65 72 79 20 69 6e 64 65  mn in every inde
24655 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  x..*/.SQLITE_PRI
24656 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
24657 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74  3IndexAffinitySt
24658 72 28 56 64 62 65 20 2a 76 2c 20 49 6e 64 65 78  r(Vdbe *v, Index
24659 20 2a 70 49 64 78 29 7b 0a 20 20 69 66 28 20 21   *pIdx){.  if( !
2465a 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b  pIdx->zColAff ){
2465b 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73  .    /* The firs
2465c 74 20 74 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20  t time a column 
2465d 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
2465e 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72  for a particular
2465f 20 69 6e 64 65 78 20 69 73 0a 20 20 20 20 2a 2a   index is.    **
24660 20 72 65 71 75 69 72 65 64 2c 20 69 74 20 69 73   required, it is
24661 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70   allocated and p
24662 6f 70 75 6c 61 74 65 64 20 68 65 72 65 2e 20 49  opulated here. I
24663 74 20 69 73 20 74 68 65 6e 20 73 74 6f 72 65 64  t is then stored
24664 20 61 73 0a 20 20 20 20 2a 2a 20 61 20 6d 65 6d   as.    ** a mem
24665 62 65 72 20 6f 66 20 74 68 65 20 49 6e 64 65 78  ber of the Index
24666 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 73   structure for s
24667 75 62 73 65 71 75 65 6e 74 20 75 73 65 2e 0a 20  ubsequent use.. 
24668 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
24669 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
2466a 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 65   string will eve
2466b 6e 74 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 74  ntually be delet
2466c 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 73 71 6c  ed by.    ** sql
2466d 69 74 65 44 65 6c 65 74 65 49 6e 64 65 78 28 29  iteDeleteIndex()
2466e 20 77 68 65 6e 20 74 68 65 20 49 6e 64 65 78 20   when the Index 
2466f 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
24670 20 69 73 20 63 6c 65 61 6e 65 64 0a 20 20 20 20   is cleaned.    
24671 2a 2a 20 75 70 2e 0a 20 20 20 20 2a 2f 0a 20 20  ** up..    */.  
24672 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 54 61 62    int n;.    Tab
24673 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 64 78 2d  le *pTab = pIdx-
24674 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c  >pTable;.    sql
24675 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
24676 65 33 56 64 62 65 44 62 28 76 29 3b 0a 20 20 20  e3VdbeDb(v);.   
24677 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d   pIdx->zColAff =
24678 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
24679 4d 61 6c 6c 6f 63 28 70 49 64 78 2d 3e 6e 43 6f  Malloc(pIdx->nCo
2467a 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 69 66 28  lumn+2);.    if(
2467b 20 21 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20   !pIdx->zColAff 
2467c 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
2467d 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2467e 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2467f 20 7d 0a 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20   }.    for(n=0; 
24680 6e 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  n<pIdx->nColumn;
24681 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 70 49 64   n++){.      pId
24682 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20  x->zColAff[n] = 
24683 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d  pTab->aCol[pIdx-
24684 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66  >aiColumn[n]].af
24685 66 69 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20  finity;.    }.  
24686 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b    pIdx->zColAff[
24687 6e 2b 2b 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46  n++] = SQLITE_AF
24688 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 70 49 64 78  F_NONE;.    pIdx
24689 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 30  ->zColAff[n] = 0
2468a 3b 0a 20 20 7d 0a 20 0a 20 20 73 71 6c 69 74 65  ;.  }. .  sqlite
2468b 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
2468c 20 2d 31 2c 20 70 49 64 78 2d 3e 7a 43 6f 6c 41   -1, pIdx->zColA
2468d 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ff, 0);.}../*.**
2468e 20 53 65 74 20 50 34 20 6f 66 20 74 68 65 20 6d   Set P4 of the m
2468f 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73  ost recently ins
24690 65 72 74 65 64 20 6f 70 63 6f 64 65 20 74 6f 20  erted opcode to 
24691 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  a column affinit
24692 79 0a 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72 20  y.** string for 
24693 74 61 62 6c 65 20 70 54 61 62 2e 20 41 20 63 6f  table pTab. A co
24694 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
24695 72 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61  ring has one cha
24696 72 61 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61  racter.** for ea
24697 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 65  ch column indexe
24698 64 20 62 79 20 74 68 65 20 69 6e 64 65 78 2c 20  d by the index, 
24699 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
2469a 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
2469b 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a  .** column:.**.*
2469c 2a 20 20 43 68 61 72 61 63 74 65 72 20 20 20 20  *  Character    
2469d 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74    Column affinit
2469e 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  y.**  ----------
2469f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
246a0 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20  ----.**  'a'    
246a1 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20          TEXT.** 
246a2 20 27 62 27 20 20 20 20 20 20 20 20 20 20 20 20   'b'            
246a3 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20  NONE.**  'c'    
246a4 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a          NUMERIC.
246a5 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20 20 20  **  'd'         
246a6 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27     INTEGER.**  '
246a7 65 27 20 20 20 20 20 20 20 20 20 20 20 20 52 45  e'            RE
246a8 41 4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  AL.*/.SQLITE_PRI
246a9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
246aa 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74  3TableAffinitySt
246ab 72 28 56 64 62 65 20 2a 76 2c 20 54 61 62 6c 65  r(Vdbe *v, Table
246ac 20 2a 70 54 61 62 29 7b 0a 20 20 2f 2a 20 54 68   *pTab){.  /* Th
246ad 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 63  e first time a c
246ae 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73  olumn affinity s
246af 74 72 69 6e 67 20 66 6f 72 20 61 20 70 61 72 74  tring for a part
246b0 69 63 75 6c 61 72 20 74 61 62 6c 65 0a 20 20 2a  icular table.  *
246b1 2a 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 69  * is required, i
246b2 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  t is allocated a
246b3 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72  nd populated her
246b4 65 2e 20 49 74 20 69 73 20 74 68 65 6e 20 0a 20  e. It is then . 
246b5 20 2a 2a 20 73 74 6f 72 65 64 20 61 73 20 61 20   ** stored as a 
246b6 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 54 61  member of the Ta
246b7 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f  ble structure fo
246b8 72 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65  r subsequent use
246b9 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
246ba 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
246bb 73 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e  string will even
246bc 74 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65  tually be delete
246bd 64 20 62 79 0a 20 20 2a 2a 20 73 71 6c 69 74 65  d by.  ** sqlite
246be 33 44 65 6c 65 74 65 54 61 62 6c 65 28 29 20 77  3DeleteTable() w
246bf 68 65 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74  hen the Table st
246c0 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69  ructure itself i
246c1 73 20 63 6c 65 61 6e 65 64 20 75 70 2e 0a 20 20  s cleaned up..  
246c2 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e  */.  if( !pTab->
246c3 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 63  zColAff ){.    c
246c4 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20 20  har *zColAff;.  
246c5 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c    int i;.    sql
246c6 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
246c7 65 33 56 64 62 65 44 62 28 76 29 3b 0a 0a 20 20  e3VdbeDb(v);..  
246c8 20 20 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61    zColAff = (cha
246c9 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  r *)sqlite3Mallo
246ca 63 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b  c(pTab->nCol+1);
246cb 0a 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 41 66  .    if( !zColAf
246cc 66 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  f ){.      db->m
246cd 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
246ce 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
246cf 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d     }..    for(i=
246d0 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
246d1 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 43 6f   i++){.      zCo
246d2 6c 41 66 66 5b 69 5d 20 3d 20 70 54 61 62 2d 3e  lAff[i] = pTab->
246d3 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79  aCol[i].affinity
246d4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c  ;.    }.    zCol
246d5 41 66 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d 20  Aff[pTab->nCol] 
246d6 3d 20 27 5c 30 27 3b 0a 0a 20 20 20 20 70 54 61  = '\0';..    pTa
246d7 62 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a 43 6f  b->zColAff = zCo
246d8 6c 41 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  lAff;.  }..  sql
246d9 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
246da 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 43  (v, -1, pTab->zC
246db 6f 6c 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a  olAff, 0);.}../*
246dc 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a  .** Return non-z
246dd 65 72 6f 20 69 66 20 74 68 65 20 74 61 62 6c 65  ero if the table
246de 20 70 54 61 62 20 69 6e 20 64 61 74 61 62 61 73   pTab in databas
246df 65 20 69 44 62 20 6f 72 20 61 6e 79 20 6f 66 20  e iDb or any of 
246e0 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 68  its indices.** h
246e1 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20  ave been opened 
246e2 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20  at any point in 
246e3 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d  the VDBE program
246e4 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 6c 6f   beginning at lo
246e5 63 61 74 69 6f 6e 0a 2a 2a 20 69 53 74 61 72 74  cation.** iStart
246e6 41 64 64 72 20 74 68 72 6f 75 67 68 74 20 74 68  Addr throught th
246e7 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f  e end of the pro
246e8 67 72 61 6d 2e 20 20 54 68 69 73 20 69 73 20 75  gram.  This is u
246e9 73 65 64 20 74 6f 20 73 65 65 20 69 66 20 0a 2a  sed to see if .*
246ea 2a 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 66  * a statement of
246eb 20 74 68 65 20 66 6f 72 6d 20 20 22 49 4e 53 45   the form  "INSE
246ec 52 54 20 49 4e 54 4f 20 3c 69 44 62 2c 20 70 54  RT INTO <iDb, pT
246ed 61 62 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20  ab> SELECT ..." 
246ee 63 61 6e 20 0a 2a 2a 20 72 75 6e 20 77 69 74 68  can .** run with
246ef 6f 75 74 20 75 73 69 6e 67 20 74 65 6d 70 6f 72  out using tempor
246f0 61 72 79 20 74 61 62 6c 65 20 66 6f 72 20 74 68  ary table for th
246f1 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  e results of the
246f2 20 53 45 4c 45 43 54 2e 20 0a 2a 2f 0a 73 74 61   SELECT. .*/.sta
246f3 74 69 63 20 69 6e 74 20 72 65 61 64 73 54 61 62  tic int readsTab
246f4 6c 65 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 20  le(Vdbe *v, int 
246f5 69 53 74 61 72 74 41 64 64 72 2c 20 69 6e 74 20  iStartAddr, int 
246f6 69 44 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  iDb, Table *pTab
246f7 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
246f8 74 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33  t iEnd = sqlite3
246f9 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
246fa 76 29 3b 0a 20 20 66 6f 72 28 69 3d 69 53 74 61  v);.  for(i=iSta
246fb 72 74 41 64 64 72 3b 20 69 3c 69 45 6e 64 3b 20  rtAddr; i<iEnd; 
246fc 69 2b 2b 29 7b 0a 20 20 20 20 56 64 62 65 4f 70  i++){.    VdbeOp
246fd 20 2a 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56   *pOp = sqlite3V
246fe 64 62 65 47 65 74 4f 70 28 76 2c 20 69 29 3b 0a  dbeGetOp(v, i);.
246ff 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 21      assert( pOp!
24700 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f  =0 );.    if( pO
24701 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  p->opcode==OP_Op
24702 65 6e 52 65 61 64 20 26 26 20 70 4f 70 2d 3e 70  enRead && pOp->p
24703 33 3d 3d 69 44 62 20 29 7b 0a 20 20 20 20 20 20  3==iDb ){.      
24704 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20  Index *pIndex;. 
24705 20 20 20 20 20 69 6e 74 20 74 6e 75 6d 20 3d 20       int tnum = 
24706 70 4f 70 2d 3e 70 32 3b 0a 20 20 20 20 20 20 69  pOp->p2;.      i
24707 66 28 20 74 6e 75 6d 3d 3d 70 54 61 62 2d 3e 74  f( tnum==pTab->t
24708 6e 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72  num ){.        r
24709 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
2470a 0a 20 20 20 20 20 20 66 6f 72 28 70 49 6e 64 65  .      for(pInde
2470b 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2470c 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70  pIndex; pIndex=p
2470d 49 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  Index->pNext){. 
2470e 20 20 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d         if( tnum=
2470f 3d 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 29 7b  =pIndex->tnum ){
24710 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
24711 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 1;.        }. 
24712 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66       }.    }.#if
24713 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24714 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
24715 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
24716 65 3d 3d 4f 50 5f 56 4f 70 65 6e 20 26 26 20 70  e==OP_VOpen && p
24717 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3d 3d 70 54  Op->p4.pVtab==pT
24718 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20  ab->pVtab ){.   
24719 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
2471a 70 34 2e 70 56 74 61 62 21 3d 30 20 29 3b 0a 20  p4.pVtab!=0 );. 
2471b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
2471c 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41  ->p4type==P4_VTA
2471d 42 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  B );.      retur
2471e 6e 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  n 1;.    }.#endi
2471f 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  f.  }.  return 0
24720 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
24721 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
24722 52 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 57 72 69  REMENT./*.** Wri
24723 74 65 20 6f 75 74 20 63 6f 64 65 20 74 6f 20 69  te out code to i
24724 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75  nitialize the au
24725 74 6f 69 6e 63 72 65 6d 65 6e 74 20 6c 6f 67 69  toincrement logi
24726 63 2e 20 20 54 68 69 73 20 63 6f 64 65 0a 2a 2a  c.  This code.**
24727 20 6c 6f 6f 6b 73 20 75 70 20 74 68 65 20 63 75   looks up the cu
24728 72 72 65 6e 74 20 61 75 74 6f 69 6e 63 72 65 6d  rrent autoincrem
24729 65 6e 74 20 76 61 6c 75 65 20 69 6e 20 74 68 65  ent value in the
2472a 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
2472b 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74  .** table and st
2472c 6f 72 65 73 20 74 68 61 74 20 76 61 6c 75 65 20  ores that value 
2472d 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e 20 20  in a register.  
2472e 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62  Code generated b
2472f 79 0a 2a 2a 20 61 75 74 6f 49 6e 63 53 74 65 70  y.** autoIncStep
24730 28 29 20 77 69 6c 6c 20 6b 65 65 70 20 74 68 61  () will keep tha
24731 74 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69  t register holdi
24732 6e 67 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a  ng the largest.*
24733 2a 20 72 6f 77 69 64 20 76 61 6c 75 65 2e 20 20  * rowid value.  
24734 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62  Code generated b
24735 79 20 61 75 74 6f 49 6e 63 45 6e 64 28 29 20 77  y autoIncEnd() w
24736 69 6c 6c 20 77 72 69 74 65 20 74 68 65 20 6e 65  ill write the ne
24737 77 0a 2a 2a 20 6c 61 72 67 65 73 74 20 76 61 6c  w.** largest val
24738 75 65 20 6f 66 20 74 68 65 20 63 6f 75 6e 74 65  ue of the counte
24739 72 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  r back into the 
2473a 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
2473b 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  table..**.** Thi
2473c 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
2473d 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
2473e 68 65 20 6d 65 6d 5b 5d 20 63 65 6c 6c 20 74 68  he mem[] cell th
2473f 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74  at contains.** t
24740 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  he maximum rowid
24741 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   counter..**.** 
24742 54 68 72 65 65 20 63 6f 6e 73 65 63 75 74 69 76  Three consecutiv
24743 65 20 72 65 67 69 73 74 65 72 73 20 61 72 65 20  e registers are 
24744 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69  allocated by thi
24745 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 0a  s routine.  The.
24746 2a 2a 20 66 69 72 73 74 20 74 77 6f 20 68 6f 6c  ** first two hol
24747 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  d the name of th
24748 65 20 74 61 72 67 65 74 20 74 61 62 6c 65 20 61  e target table a
24749 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72  nd the maximum r
2474a 6f 77 69 64 20 0a 2a 2a 20 69 6e 73 65 72 74 65  owid .** inserte
2474b 64 20 69 6e 74 6f 20 74 68 65 20 74 61 72 67 65  d into the targe
2474c 74 20 74 61 62 6c 65 2c 20 72 65 73 70 65 63 74  t table, respect
2474d 69 76 65 6c 79 2e 0a 2a 2a 20 54 68 65 20 74 68  ively..** The th
2474e 69 72 64 20 68 6f 6c 64 73 20 74 68 65 20 72 6f  ird holds the ro
2474f 77 69 64 20 69 6e 20 73 71 6c 69 74 65 5f 73 65  wid in sqlite_se
24750 71 75 65 6e 63 65 20 77 68 65 72 65 20 77 65 20  quence where we 
24751 77 69 6c 6c 0a 2a 2a 20 77 72 69 74 65 20 62 61  will.** write ba
24752 63 6b 20 74 68 65 20 72 65 76 69 73 65 64 20 6d  ck the revised m
24753 61 78 69 6d 75 6d 20 72 6f 77 69 64 2e 20 20 54  aximum rowid.  T
24754 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
24755 72 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78  rns the.** index
24756 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 6f   of the second o
24757 66 20 74 68 65 73 65 20 74 68 72 65 65 20 72 65  f these three re
24758 67 69 73 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74  gisters..*/.stat
24759 69 63 20 69 6e 74 20 61 75 74 6f 49 6e 63 42 65  ic int autoIncBe
2475a 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  gin(.  Parse *pP
2475b 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61  arse,      /* Pa
2475c 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2475d 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20  .  int iDb,     
2475e 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2475f 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
24760 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a  holding pTab */.
24761 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 20 20    Table *pTab   
24762 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
24763 6c 65 20 77 65 20 61 72 65 20 77 72 69 74 69 6e  le we are writin
24764 67 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  g to */.){.  int
24765 20 6d 65 6d 49 64 20 3d 20 30 3b 20 20 20 20 20   memId = 0;     
24766 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
24767 64 69 6e 67 20 6d 61 78 69 6d 75 6d 20 72 6f 77  ding maximum row
24768 69 64 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  id */.  if( pTab
24769 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
2476a 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b  Autoincrement ){
2476b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70  .    Vdbe *v = p
2476c 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2476d 20 20 44 62 20 2a 70 44 62 20 3d 20 26 70 50 61    Db *pDb = &pPa
2476e 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62  rse->db->aDb[iDb
2476f 5d 3b 0a 20 20 20 20 69 6e 74 20 69 43 75 72 20  ];.    int iCur 
24770 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
24771 3b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20  ;.    int addr; 
24772 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24773 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
24774 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  top of the loop 
24775 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 76  */.    assert( v
24776 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   );.    pParse->
24777 6e 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20  nMem++;         
24778 2f 2a 20 48 6f 6c 64 73 20 6e 61 6d 65 20 6f 66  /* Holds name of
24779 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65   table */.    me
2477a 6d 49 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mId = ++pParse->
2477b 6e 4d 65 6d 3b 0a 20 20 20 20 70 50 61 72 73 65  nMem;.    pParse
2477c 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 73 71  ->nMem++;.    sq
2477d 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
2477e 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62  Parse, iCur, iDb
2477f 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  , pDb->pSchema->
24780 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e  pSeqTab, OP_Open
24781 52 65 61 64 29 3b 0a 20 20 20 20 61 64 64 72 20  Read);.    addr 
24782 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
24783 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
24784 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24785 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
24786 2c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c  , 0, memId-1, 0,
24787 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pTab->zName, 0)
24788 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
24789 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
2478a 77 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64 72  wind, iCur, addr
2478b 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +9);.    sqlite3
2478c 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2478d 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 30  _Column, iCur, 0
2478e 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71  , memId);.    sq
2478f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
24790 76 2c 20 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d  v, OP_Ne, memId-
24791 31 2c 20 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64  1, addr+7, memId
24792 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
24793 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51  beChangeP5(v, SQ
24794 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
24795 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
24796 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
24797 77 69 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49 64  wid, iCur, memId
24798 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +1);.    sqlite3
24799 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2479a 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 31  _Column, iCur, 1
2479b 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71  , memId);.    sq
2479c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2479d 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
2479e 64 64 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69  ddr+9);.    sqli
2479f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
247a0 20 4f 50 5f 4e 65 78 74 2c 20 69 43 75 72 2c 20   OP_Next, iCur, 
247a1 61 64 64 72 2b 32 29 3b 0a 20 20 20 20 73 71 6c  addr+2);.    sql
247a2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
247a3 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
247a4 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c   memId);.    sql
247a5 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
247a6 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72  , OP_Close, iCur
247a7 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
247a8 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a  rn memId;.}../*.
247a9 2a 2a 20 55 70 64 61 74 65 20 74 68 65 20 6d 61  ** Update the ma
247aa 78 69 6d 75 6d 20 72 6f 77 69 64 20 66 6f 72 20  ximum rowid for 
247ab 61 6e 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  an autoincrement
247ac 20 63 61 6c 63 75 6c 61 74 69 6f 6e 2e 0a 2a 2a   calculation..**
247ad 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
247ae 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65   should be calle
247af 64 20 77 68 65 6e 20 74 68 65 20 74 6f 70 20 6f  d when the top o
247b0 66 20 74 68 65 20 73 74 61 63 6b 20 68 6f 6c 64  f the stack hold
247b1 73 20 61 0a 2a 2a 20 6e 65 77 20 72 6f 77 69 64  s a.** new rowid
247b2 20 74 68 61 74 20 69 73 20 61 62 6f 75 74 20 74   that is about t
247b3 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20  o be inserted.  
247b4 49 66 20 74 68 61 74 20 6e 65 77 20 72 6f 77 69  If that new rowi
247b5 64 20 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74  d is.** larger t
247b6 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  han the maximum 
247b7 72 6f 77 69 64 20 69 6e 20 74 68 65 20 6d 65 6d  rowid in the mem
247b8 49 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2c 20  Id memory cell, 
247b9 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f  then the.** memo
247ba 72 79 20 63 65 6c 6c 20 69 73 20 75 70 64 61 74  ry cell is updat
247bb 65 64 2e 20 20 54 68 65 20 73 74 61 63 6b 20 69  ed.  The stack i
247bc 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  s unchanged..*/.
247bd 73 74 61 74 69 63 20 76 6f 69 64 20 61 75 74 6f  static void auto
247be 49 6e 63 53 74 65 70 28 50 61 72 73 65 20 2a 70  IncStep(Parse *p
247bf 50 61 72 73 65 2c 20 69 6e 74 20 6d 65 6d 49 64  Parse, int memId
247c0 2c 20 69 6e 74 20 72 65 67 52 6f 77 69 64 29 7b  , int regRowid){
247c1 0a 20 20 69 66 28 20 6d 65 6d 49 64 3e 30 20 29  .  if( memId>0 )
247c2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
247c3 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e  eAddOp2(pParse->
247c4 70 56 64 62 65 2c 20 4f 50 5f 4d 65 6d 4d 61 78  pVdbe, OP_MemMax
247c5 2c 20 6d 65 6d 49 64 2c 20 72 65 67 52 6f 77 69  , memId, regRowi
247c6 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  d);.  }.}../*.**
247c7 20 41 66 74 65 72 20 64 6f 69 6e 67 20 6f 6e 65   After doing one
247c8 20 6f 72 20 6d 6f 72 65 20 69 6e 73 65 72 74 73   or more inserts
247c9 2c 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f  , the maximum ro
247ca 77 69 64 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a  wid is stored.**
247cb 20 69 6e 20 72 65 67 5b 6d 65 6d 49 64 5d 2e 20   in reg[memId]. 
247cc 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
247cd 6f 20 77 72 69 74 65 20 74 68 69 73 20 76 61 6c  o write this val
247ce 75 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65  ue back into the
247cf 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 73  .** the sqlite_s
247d0 65 71 75 65 6e 63 65 20 74 61 62 6c 65 2e 0a 2a  equence table..*
247d1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 75  /.static void au
247d2 74 6f 49 6e 63 45 6e 64 28 0a 20 20 50 61 72 73  toIncEnd(.  Pars
247d3 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
247d4 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
247d5 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
247d6 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Db,           /*
247d7 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   Index of the da
247d8 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70  tabase holding p
247d9 54 61 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  Tab */.  Table *
247da 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54  pTab,       /* T
247db 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65  able we are inse
247dc 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20  rting into */.  
247dd 69 6e 74 20 6d 65 6d 49 64 20 20 20 20 20 20 20  int memId       
247de 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c     /* Memory cel
247df 6c 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6d 61  l holding the ma
247e0 78 69 6d 75 6d 20 72 6f 77 69 64 20 2a 2f 0a 29  ximum rowid */.)
247e1 7b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61  {.  if( pTab->ta
247e2 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f  bFlags & TF_Auto
247e3 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20  increment ){.   
247e4 20 69 6e 74 20 69 43 75 72 20 3d 20 70 50 61 72   int iCur = pPar
247e5 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
247e6 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
247e7 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20 44 62 20  ->pVdbe;.    Db 
247e8 2a 70 44 62 20 3d 20 26 70 50 61 72 73 65 2d 3e  *pDb = &pParse->
247e9 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20  db->aDb[iDb];.  
247ea 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 69 6e    int j1;.    in
247eb 74 20 69 52 65 63 20 3d 20 2b 2b 70 50 61 72 73  t iRec = ++pPars
247ec 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 4d  e->nMem;    /* M
247ed 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20  emory cell used 
247ee 66 6f 72 20 72 65 63 6f 72 64 20 2a 2f 0a 0a 20  for record */.. 
247ef 20 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a     assert( v );.
247f0 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
247f1 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75  able(pParse, iCu
247f2 72 2c 20 69 44 62 2c 20 70 44 62 2d 3e 70 53 63  r, iDb, pDb->pSc
247f3 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 2c 20 4f  hema->pSeqTab, O
247f4 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20  P_OpenWrite);.  
247f5 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
247f6 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e  beAddOp1(v, OP_N
247f7 6f 74 4e 75 6c 6c 2c 20 6d 65 6d 49 64 2b 31 29  otNull, memId+1)
247f8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
247f9 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
247fa 77 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 6d 65  wRowid, iCur, me
247fb 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  mId+1);.    sqli
247fc 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
247fd 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69  v, j1);.    sqli
247fe 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
247ff 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
24800 6d 65 6d 49 64 2d 31 2c 20 32 2c 20 69 52 65 63  memId-1, 2, iRec
24801 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
24802 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
24803 6e 73 65 72 74 2c 20 69 43 75 72 2c 20 69 52 65  nsert, iCur, iRe
24804 63 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20  c, memId+1);.   
24805 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
24806 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41  geP5(v, OPFLAG_A
24807 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69  PPEND);.    sqli
24808 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
24809 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29   OP_Close, iCur)
2480a 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 2f 2a  ;.  }.}.#else./*
2480b 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d  .** If SQLITE_OM
2480c 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
2480d 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
2480e 6e 20 74 68 65 20 74 68 72 65 65 20 72 6f 75 74  n the three rout
2480f 69 6e 65 73 0a 2a 2a 20 61 62 6f 76 65 20 61 72  ines.** above ar
24810 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a  e all no-ops.*/.
24811 23 20 64 65 66 69 6e 65 20 61 75 74 6f 49 6e 63  # define autoInc
24812 42 65 67 69 6e 28 41 2c 42 2c 43 29 20 28 30 29  Begin(A,B,C) (0)
24813 0a 23 20 64 65 66 69 6e 65 20 61 75 74 6f 49 6e  .# define autoIn
24814 63 53 74 65 70 28 41 2c 42 2c 43 29 0a 23 20 64  cStep(A,B,C).# d
24815 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 45 6e 64  efine autoIncEnd
24816 28 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66  (A,B,C,D).#endif
24817 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
24818 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f  AUTOINCREMENT */
24819 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  .../* Forward de
2481a 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61  claration */.sta
2481b 74 69 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69  tic int xferOpti
2481c 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73  mization(.  Pars
2481d 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2481e 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
2481f 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
24820 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f  pDest,         /
24821 2a 20 54 68 65 20 74 61 62 6c 65 20 77 65 20 61  * The table we a
24822 72 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74  re inserting int
24823 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  o */.  Select *p
24824 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20  Select,      /* 
24825 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
24826 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  nt to use as the
24827 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a   data source */.
24828 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20    int onError,  
24829 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74          /* How t
2482a 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61  o handle constra
2482b 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  int errors */.  
2482c 69 6e 74 20 69 44 62 44 65 73 74 20 20 20 20 20  int iDbDest     
2482d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2482e 61 62 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a  abase of pDest *
2482f 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  /.);../*.** This
24830 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
24831 20 74 6f 20 68 61 6e 64 6c 65 20 53 51 4c 20 6f   to handle SQL o
24832 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
24833 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  forms:.**.**    
24834 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c  insert into TABL
24835 45 20 28 49 44 4c 49 53 54 29 20 76 61 6c 75 65  E (IDLIST) value
24836 73 28 45 58 50 52 4c 49 53 54 29 0a 2a 2a 20 20  s(EXPRLIST).**  
24837 20 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41    insert into TA
24838 42 4c 45 20 28 49 44 4c 49 53 54 29 20 73 65 6c  BLE (IDLIST) sel
24839 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 44  ect.**.** The ID
2483a 4c 49 53 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  LIST following t
2483b 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73  he table name is
2483c 20 61 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c   always optional
2483d 2e 20 20 49 66 20 6f 6d 69 74 74 65 64 2c 0a 2a  .  If omitted,.*
2483e 2a 20 74 68 65 6e 20 61 20 6c 69 73 74 20 6f 66  * then a list of
2483f 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72   all columns for
24840 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 75   the table is su
24841 62 73 74 69 74 75 74 65 64 2e 20 20 54 68 65 20  bstituted.  The 
24842 49 44 4c 49 53 54 0a 2a 2a 20 61 70 70 65 61 72  IDLIST.** appear
24843 73 20 69 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e  s in the pColumn
24844 20 70 61 72 61 6d 65 74 65 72 2e 20 20 70 43 6f   parameter.  pCo
24845 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20  lumn is NULL if 
24846 49 44 4c 49 53 54 20 69 73 20 6f 6d 69 74 74 65  IDLIST is omitte
24847 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4c 69  d..**.** The pLi
24848 73 74 20 70 61 72 61 6d 65 74 65 72 20 68 6f 6c  st parameter hol
24849 64 73 20 45 58 50 52 4c 49 53 54 20 69 6e 20 74  ds EXPRLIST in t
2484a 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20 6f 66  he first form of
2484b 20 74 68 65 20 49 4e 53 45 52 54 0a 2a 2a 20 73   the INSERT.** s
2484c 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 2c 20  tatement above, 
2484d 61 6e 64 20 70 53 65 6c 65 63 74 20 69 73 20 4e  and pSelect is N
2484e 55 4c 4c 2e 20 20 46 6f 72 20 74 68 65 20 73 65  ULL.  For the se
2484f 63 6f 6e 64 20 66 6f 72 6d 2c 20 70 4c 69 73 74  cond form, pList
24850 20 69 73 0a 2a 2a 20 4e 55 4c 4c 20 61 6e 64 20   is.** NULL and 
24851 70 53 65 6c 65 63 74 20 69 73 20 61 20 70 6f 69  pSelect is a poi
24852 6e 74 65 72 20 74 6f 20 74 68 65 20 73 65 6c 65  nter to the sele
24853 63 74 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65  ct statement use
24854 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a  d to generate.**
24855 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 69 6e   data for the in
24856 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sert..**.** The 
24857 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 66  code generated f
24858 6f 6c 6c 6f 77 73 20 6f 6e 65 20 6f 66 20 66 6f  ollows one of fo
24859 75 72 20 74 65 6d 70 6c 61 74 65 73 2e 20 20 46  ur templates.  F
2485a 6f 72 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 73  or a simple.** s
2485b 65 6c 65 63 74 20 77 69 74 68 20 64 61 74 61 20  elect with data 
2485c 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41  coming from a VA
2485d 4c 55 45 53 20 63 6c 61 75 73 65 2c 20 74 68 65  LUES clause, the
2485e 20 63 6f 64 65 20 65 78 65 63 75 74 65 73 0a 2a   code executes.*
2485f 2a 20 6f 6e 63 65 20 73 74 72 61 69 67 68 74 20  * once straight 
24860 64 6f 77 6e 20 74 68 72 6f 75 67 68 2e 20 20 50  down through.  P
24861 73 65 75 64 6f 2d 63 6f 64 65 20 66 6f 6c 6c 6f  seudo-code follo
24862 77 73 20 28 77 65 20 63 61 6c 6c 20 74 68 69 73  ws (we call this
24863 0a 2a 2a 20 74 68 65 20 22 31 73 74 20 74 65 6d  .** the "1st tem
24864 70 6c 61 74 65 22 29 3a 0a 2a 2a 0a 2a 2a 20 20  plate"):.**.**  
24865 20 20 20 20 20 20 20 6f 70 65 6e 20 77 72 69 74         open writ
24866 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62  e cursor to <tab
24867 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69  le> and its indi
24868 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 70  ces.**         p
24869 75 74 73 20 56 41 4c 55 45 53 20 63 6c 61 75 73  uts VALUES claus
2486a 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e  e expressions on
2486b 74 6f 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20  to the stack.** 
2486c 20 20 20 20 20 20 20 20 77 72 69 74 65 20 74 68          write th
2486d 65 20 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f  e resulting reco
2486e 72 64 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a  rd into <table>.
2486f 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
24870 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72  up.**.** The thr
24871 65 65 20 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d  ee remaining tem
24872 70 6c 61 74 65 73 20 61 73 73 75 6d 65 20 74 68  plates assume th
24873 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f  e statement is o
24874 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
24875 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c     INSERT INTO <
24876 74 61 62 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e  table> SELECT ..
24877 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ..**.** If the S
24878 45 4c 45 43 54 20 63 6c 61 75 73 65 20 69 73 20  ELECT clause is 
24879 6f 66 20 74 68 65 20 72 65 73 74 72 69 63 74 65  of the restricte
2487a 64 20 66 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a  d form "SELECT *
2487b 20 46 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20   FROM <table2>" 
2487c 2d 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f  -.** in other wo
2487d 72 64 73 20 69 66 20 74 68 65 20 53 45 4c 45 43  rds if the SELEC
2487e 54 20 70 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75  T pulls all colu
2487f 6d 6e 73 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c  mns from a singl
24880 65 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74  e table.** and t
24881 68 65 72 65 20 69 73 20 6e 6f 20 57 48 45 52 45  here is no WHERE
24882 20 6f 72 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f   or LIMIT or GRO
24883 55 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42  UP BY or ORDER B
24884 59 20 63 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a  Y clauses, and.*
24885 2a 20 69 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e  * if <table2> an
24886 64 20 3c 74 61 62 6c 65 31 3e 20 61 72 65 20 64  d <table1> are d
24887 69 73 74 69 6e 63 74 20 74 61 62 6c 65 73 20 62  istinct tables b
24888 75 74 20 68 61 76 65 20 69 64 65 6e 74 69 63 61  ut have identica
24889 6c 0a 2a 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e  l.** schemas, in
2488a 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 20  cluding all the 
2488b 73 61 6d 65 20 69 6e 64 69 63 65 73 2c 20 74 68  same indices, th
2488c 65 6e 20 61 20 73 70 65 63 69 61 6c 20 6f 70 74  en a special opt
2488d 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20  imization.** is 
2488e 69 6e 76 6f 6b 65 64 20 74 68 61 74 20 63 6f 70  invoked that cop
2488f 69 65 73 20 72 61 77 20 72 65 63 6f 72 64 73 20  ies raw records 
24890 66 72 6f 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76  from <table2> ov
24891 65 72 20 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a  er to <table1>..
24892 2a 2a 20 53 65 65 20 74 68 65 20 78 66 65 72 4f  ** See the xferO
24893 70 74 69 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75  ptimization() fu
24894 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69  nction for the i
24895 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
24896 20 74 68 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74   this.** templat
24897 65 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  e.  This is the 
24898 32 6e 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a  2nd template..**
24899 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e  .**         open
2489a 20 61 20 77 72 69 74 65 20 63 75 72 73 6f 72 20   a write cursor 
2489b 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20  to <table>.**   
2489c 20 20 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20        open read 
2489d 63 75 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65  cursor on <table
2489e 32 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72  2>.**         tr
2489f 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72  ansfer all recor
248a0 64 73 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f  ds in <table2> o
248a1 76 65 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a  ver to <table>.*
248a2 2a 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20  *         close 
248a3 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20  cursors.**      
248a4 20 20 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78     foreach index
248a5 20 6f 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20   on <table>.**  
248a6 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20           open a 
248a7 77 72 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20  write cursor on 
248a8 74 68 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65  the <table> inde
248a9 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f  x.**           o
248aa 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f  pen a read curso
248ab 72 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70  r on the corresp
248ac 6f 6e 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20  onding <table2> 
248ad 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20  index.**        
248ae 20 20 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20     transfer all 
248af 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65  records from the
248b0 20 72 65 61 64 20 74 6f 20 74 68 65 20 77 72 69   read to the wri
248b1 74 65 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20  te cursors.**   
248b2 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75          close cu
248b3 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
248b4 20 65 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a   end foreach.**.
248b5 2a 2a 20 54 68 65 20 33 72 64 20 74 65 6d 70 6c  ** The 3rd templ
248b6 61 74 65 20 69 73 20 66 6f 72 20 77 68 65 6e 20  ate is for when 
248b7 74 68 65 20 73 65 63 6f 6e 64 20 74 65 6d 70 6c  the second templ
248b8 61 74 65 20 64 6f 65 73 20 6e 6f 74 20 61 70 70  ate does not app
248b9 6c 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 53 45  ly.** and the SE
248ba 4c 45 43 54 20 63 6c 61 75 73 65 20 64 6f 65 73  LECT clause does
248bb 20 6e 6f 74 20 72 65 61 64 20 66 72 6f 6d 20 3c   not read from <
248bc 74 61 62 6c 65 3e 20 61 74 20 61 6e 79 20 74 69  table> at any ti
248bd 6d 65 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72  me..** The gener
248be 61 74 65 64 20 63 6f 64 65 20 66 6f 6c 6c 6f 77  ated code follow
248bf 73 20 74 68 69 73 20 74 65 6d 70 6c 61 74 65 3a  s this template:
248c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45  .**.**         E
248c1 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20  OF <- 0.**      
248c2 20 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20     X <- A.**    
248c3 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20       goto B.**  
248c4 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72      A: setup for
248c5 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20   the SELECT.**  
248c6 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72         loop over
248c7 20 74 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65   the rows in the
248c8 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
248c9 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 73       load values
248ca 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 20   into registers 
248cb 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20  R..R+n.**       
248cc 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20      yield X.**  
248cd 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a         end loop.
248ce 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
248cf 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c  up after the SEL
248d0 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45  ECT.**         E
248d1 4f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20  OF <- 1.**      
248d2 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20     yield X.**   
248d3 20 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20        goto A.** 
248d4 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 77 72 69       B: open wri
248d5 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61  te cursor to <ta
248d6 62 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64  ble> and its ind
248d7 69 63 65 73 0a 2a 2a 20 20 20 20 20 20 43 3a 20  ices.**      C: 
248d8 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20  yield X.**      
248d9 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44     if EOF goto D
248da 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65  .**         inse
248db 72 74 20 74 68 65 20 73 65 6c 65 63 74 20 72 65  rt the select re
248dc 73 75 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65  sult into <table
248dd 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a  > from R..R+n.**
248de 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a           goto C.
248df 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e  **      D: clean
248e0 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68  up.**.** The 4th
248e1 20 74 65 6d 70 6c 61 74 65 20 69 73 20 75 73 65   template is use
248e2 64 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20  d if the insert 
248e3 73 74 61 74 65 6d 65 6e 74 20 74 61 6b 65 73 20  statement takes 
248e4 69 74 73 0a 2a 2a 20 76 61 6c 75 65 73 20 66 72  its.** values fr
248e5 6f 6d 20 61 20 53 45 4c 45 43 54 20 62 75 74 20  om a SELECT but 
248e6 74 68 65 20 64 61 74 61 20 69 73 20 62 65 69 6e  the data is bein
248e7 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  g inserted into 
248e8 61 20 74 61 62 6c 65 0a 2a 2a 20 74 68 61 74 20  a table.** that 
248e9 69 73 20 61 6c 73 6f 20 72 65 61 64 20 61 73 20  is also read as 
248ea 70 61 72 74 20 6f 66 20 74 68 65 20 53 45 4c 45  part of the SELE
248eb 43 54 2e 20 20 49 6e 20 74 68 65 20 74 68 69 72  CT.  In the thir
248ec 64 20 66 6f 72 6d 2c 0a 2a 2a 20 77 65 20 68 61  d form,.** we ha
248ed 76 65 20 74 6f 20 75 73 65 20 61 20 69 6e 74 65  ve to use a inte
248ee 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20 74  rmediate table t
248ef 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  o store the resu
248f0 6c 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20 73 65  lts of.** the se
248f1 6c 65 63 74 2e 20 20 54 68 65 20 74 65 6d 70 6c  lect.  The templ
248f2 61 74 65 20 69 73 20 6c 69 6b 65 20 74 68 69 73  ate is like this
248f3 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
248f4 45 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20  EOF <- 0.**     
248f5 20 20 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20      X <- A.**   
248f6 20 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20        goto B.** 
248f7 20 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f       A: setup fo
248f8 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20  r the SELECT.** 
248f9 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65          loop ove
248fa 72 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20  r the tables in 
248fb 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20  the SELECT.**   
248fc 20 20 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c          load val
248fd 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  ue into register
248fe 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20   R..R+n.**      
248ff 20 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20       yield X.** 
24900 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70          end loop
24901 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61  .**         clea
24902 6e 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45  nup after the SE
24903 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
24904 45 4f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20  EOF <- 1.**     
24905 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20      yield X.**  
24906 20 20 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f         halt-erro
24907 72 0a 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65  r.**      B: ope
24908 6e 20 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20  n temp table.** 
24909 20 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a       L: yield X.
2490a 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f  **         if EO
2490b 46 20 67 6f 74 6f 20 4d 0a 2a 2a 20 20 20 20 20  F goto M.**     
2490c 20 20 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66      insert row f
2490d 72 6f 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20  rom R..R+n into 
2490e 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20  temp table.**   
2490f 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a 2a 2a 20        goto L.** 
24910 20 20 20 20 20 4d 3a 20 6f 70 65 6e 20 77 72 69       M: open wri
24911 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61  te cursor to <ta
24912 62 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64  ble> and its ind
24913 69 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ices.**         
24914 72 65 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c  rewind temp tabl
24915 65 0a 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f  e.**      C: loo
24916 70 20 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69  p over rows of i
24917 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
24918 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  e.**           t
24919 72 61 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66  ransfer values f
2491a 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65  orm intermediate
2491b 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62   table into <tab
2491c 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65  le>.**         e
2491d 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20  nd loop.**      
2491e 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2f 0a 53 51  D: cleanup.*/.SQ
2491f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
24920 64 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28  d sqlite3Insert(
24921 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
24922 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
24923 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
24924 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
24925 74 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  t,    /* Name of
24926 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
24927 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
24928 6e 67 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ng */.  ExprList
24929 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a   *pList,      /*
2492a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20   List of values 
2492b 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a  to be inserted *
2492c 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
2492d 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53  ect,      /* A S
2492e 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2492f 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64 61  to use as the da
24930 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 49  ta source */.  I
24931 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20  dList *pColumn, 
24932 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
24933 61 6d 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69  ames correspondi
24934 6e 67 20 74 6f 20 49 44 4c 49 53 54 2e 20 2a 2f  ng to IDLIST. */
24935 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20  .  int onError  
24936 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
24937 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72  to handle constr
24938 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29  aint errors */.)
24939 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
2493a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2493b 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73   main database s
2493c 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 54 61  tructure */.  Ta
2493d 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
2493e 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
2493f 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e   to insert into.
24940 20 20 61 6b 61 20 54 41 42 4c 45 20 2a 2f 0a 20    aka TABLE */. 
24941 20 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20   char *zTab;    
24942 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
24943 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f  f the table into
24944 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69 6e   which we are in
24945 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e  serting */.  con
24946 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20  st char *zDb;   
24947 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
24948 65 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69  e database holdi
24949 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  ng this table */
2494a 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78  .  int i, j, idx
2494b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70  ;        /* Loop
2494c 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56   counters */.  V
2494d 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
2494e 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2494f 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20   code into this 
24950 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
24951 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
24952 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  ;          /* Fo
24953 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69  r looping over i
24954 6e 64 69 63 65 73 20 6f 66 20 74 68 65 20 74 61  ndices of the ta
24955 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
24956 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  lumn;          /
24957 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
24958 6d 6e 73 20 69 6e 20 74 68 65 20 64 61 74 61 20  mns in the data 
24959 2a 2f 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e  */.  int nHidden
2495a 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75   = 0;      /* Nu
2495b 6d 62 65 72 20 6f 66 20 68 69 64 64 65 6e 20 63  mber of hidden c
2495c 6f 6c 75 6d 6e 73 20 69 66 20 54 41 42 4c 45 20  olumns if TABLE 
2495d 69 73 20 76 69 72 74 75 61 6c 20 2a 2f 0a 20 20  is virtual */.  
2495e 69 6e 74 20 62 61 73 65 43 75 72 20 3d 20 30 3b  int baseCur = 0;
2495f 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75        /* VDBE Cu
24960 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
24961 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6b 65  pTab */.  int ke
24962 79 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20  yColumn = -1;   
24963 2f 2a 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69  /* Column that i
24964 73 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52  s the INTEGER PR
24965 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69  IMARY KEY */.  i
24966 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20  nt endOfLoop;   
24967 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f       /* Label fo
24968 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  r the end of the
24969 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20   insertion loop 
2496a 2a 2f 0a 20 20 69 6e 74 20 75 73 65 54 65 6d 70  */.  int useTemp
2496b 54 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 53 74  Table = 0; /* St
2496c 6f 72 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c  ore SELECT resul
2496d 74 73 20 69 6e 20 69 6e 74 65 72 6d 65 64 69 61  ts in intermedia
2496e 74 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  te table */.  in
2496f 74 20 73 72 63 54 61 62 20 3d 20 30 3b 20 20 20  t srcTab = 0;   
24970 20 20 20 20 2f 2a 20 44 61 74 61 20 63 6f 6d 65      /* Data come
24971 73 20 66 72 6f 6d 20 74 68 69 73 20 74 65 6d 70  s from this temp
24972 6f 72 61 72 79 20 63 75 72 73 6f 72 20 69 66 20  orary cursor if 
24973 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  >=0 */.  int add
24974 72 49 6e 73 54 6f 70 20 3d 20 30 3b 20 20 20 2f  rInsTop = 0;   /
24975 2a 20 4a 75 6d 70 20 74 6f 20 6c 61 62 65 6c 20  * Jump to label 
24976 22 44 22 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  "D" */.  int add
24977 72 43 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20 2f  rCont = 0;     /
24978 2a 20 54 6f 70 20 6f 66 20 69 6e 73 65 72 74 20  * Top of insert 
24979 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 22 43 22 20  loop. Label "C" 
2497a 69 6e 20 74 65 6d 70 6c 61 74 65 73 20 33 20 61  in templates 3 a
2497b 6e 64 20 34 20 2a 2f 0a 20 20 69 6e 74 20 61 64  nd 4 */.  int ad
2497c 64 72 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20  drSelect = 0;   
2497d 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 63 6f  /* Address of co
2497e 72 6f 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70  routine that imp
2497f 6c 65 6d 65 6e 74 73 20 74 68 65 20 53 45 4c 45  lements the SELE
24980 43 54 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  CT */.  SelectDe
24981 73 74 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a  st dest;      /*
24982 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72   Destination for
24983 20 53 45 4c 45 43 54 20 6f 6e 20 72 68 73 20 6f   SELECT on rhs o
24984 66 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e  f INSERT */.  in
24985 74 20 6e 65 77 49 64 78 20 3d 20 2d 31 3b 20 20  t newIdx = -1;  
24986 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 66 6f      /* Cursor fo
24987 72 20 74 68 65 20 4e 45 57 20 70 73 65 75 64 6f  r the NEW pseudo
24988 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  -table */.  int 
24989 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
2498a 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61    /* Index of da
2498b 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 54  tabase holding T
2498c 41 42 4c 45 20 2a 2f 0a 20 20 44 62 20 2a 70 44  ABLE */.  Db *pD
2498d 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
2498e 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2498f 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65  containing table
24990 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20   being inserted 
24991 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 61 70  into */.  int ap
24992 70 65 6e 64 46 6c 61 67 20 3d 20 30 3b 20 20 20  pendFlag = 0;   
24993 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 69  /* True if the i
24994 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 20  nsert is likely 
24995 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20  to be an append 
24996 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65  */..  /* Registe
24997 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  r allocations */
24998 0a 20 20 69 6e 74 20 72 65 67 46 72 6f 6d 53 65  .  int regFromSe
24999 6c 65 63 74 20 3d 20 30 3b 2f 2a 20 42 61 73 65  lect = 0;/* Base
2499a 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 64 61   register for da
2499b 74 61 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 53  ta coming from S
2499c 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 72  ELECT */.  int r
2499d 65 67 41 75 74 6f 69 6e 63 20 3d 20 30 3b 20 20  egAutoinc = 0;  
2499e 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
2499f 64 69 6e 67 20 74 68 65 20 41 55 54 4f 49 4e 43  ding the AUTOINC
249a0 52 45 4d 45 4e 54 20 63 6f 75 6e 74 65 72 20 2a  REMENT counter *
249a1 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 43 6f  /.  int regRowCo
249a2 75 6e 74 20 3d 20 30 3b 20 20 2f 2a 20 4d 65 6d  unt = 0;  /* Mem
249a3 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f  ory cell used fo
249a4 72 20 74 68 65 20 72 6f 77 20 63 6f 75 6e 74 65  r the row counte
249a5 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 6e  r */.  int regIn
249a6 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s;           /* 
249a7 42 6c 6f 63 6b 20 6f 66 20 72 65 67 73 20 68 6f  Block of regs ho
249a8 6c 64 69 6e 67 20 72 6f 77 69 64 2b 64 61 74 61  lding rowid+data
249a9 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20   being inserted 
249aa 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69  */.  int regRowi
249ab 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65  d;         /* re
249ac 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20  gisters holding 
249ad 69 6e 73 65 72 74 20 72 6f 77 69 64 20 2a 2f 0a  insert rowid */.
249ae 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20    int regData;  
249af 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67 69 73          /* regis
249b0 74 65 72 20 68 6f 6c 64 69 6e 67 20 66 69 72 73  ter holding firs
249b1 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 69 6e 73 65  t column to inse
249b2 72 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52  rt */.  int regR
249b3 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a  ecord;        /*
249b4 20 48 6f 6c 64 73 20 74 68 65 20 61 73 73 65 6d   Holds the assem
249b5 62 6c 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64  blied row record
249b6 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f 66   */.  int regEof
249b7 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 52   = 0;       /* R
249b8 65 67 69 73 74 65 72 20 72 65 63 6f 72 64 69 6e  egister recordin
249b9 67 20 65 6e 64 20 6f 66 20 53 45 4c 45 43 54 20  g end of SELECT 
249ba 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  data */.  int *a
249bb 52 65 67 49 64 78 20 3d 20 30 3b 20 20 20 20 20  RegIdx = 0;     
249bc 2f 2a 20 4f 6e 65 20 72 65 67 69 73 74 65 72 20  /* One register 
249bd 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63  allocated to eac
249be 68 20 69 6e 64 65 78 20 2a 2f 0a 0a 0a 23 69 66  h index */...#if
249bf 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
249c0 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69  _TRIGGER.  int i
249c1 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20 20  sView;          
249c2 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
249c3 66 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  f attempting to 
249c4 69 6e 73 65 72 74 20 69 6e 74 6f 20 61 20 76 69  insert into a vi
249c5 65 77 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20  ew */.  Trigger 
249c6 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20 20  *pTrigger;      
249c7 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74      /* List of t
249c8 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 2c  riggers on pTab,
249c9 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a   if required */.
249ca 20 20 69 6e 74 20 74 6d 61 73 6b 3b 20 20 20 20    int tmask;    
249cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
249cc 20 4d 61 73 6b 20 6f 66 20 74 72 69 67 67 65 72   Mask of trigger
249cd 20 74 69 6d 65 73 20 2a 2f 0a 23 65 6e 64 69 66   times */.#endif
249ce 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ..  db = pParse-
249cf 3e 64 62 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64  >db;.  memset(&d
249d0 65 73 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64  est, 0, sizeof(d
249d1 65 73 74 29 29 3b 0a 20 20 69 66 28 20 70 50 61  est));.  if( pPa
249d2 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
249d3 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
249d4 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
249d5 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
249d6 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74   /* Locate the t
249d7 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
249d8 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72  we will be inser
249d9 74 69 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d 61  ting new informa
249da 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  tion..  */.  ass
249db 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e  ert( pTabList->n
249dc 53 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61 62  Src==1 );.  zTab
249dd 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30   = pTabList->a[0
249de 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 4e  ].zName;.  if( N
249df 45 56 45 52 28 7a 54 61 62 3d 3d 30 29 20 29 20  EVER(zTab==0) ) 
249e0 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
249e1 6e 75 70 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  nup;.  pTab = sq
249e2 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b  lite3SrcListLook
249e3 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  up(pParse, pTabL
249e4 69 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62  ist);.  if( pTab
249e5 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
249e6 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
249e7 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69    }.  iDb = sqli
249e8 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
249e9 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
249ea 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ma);.  assert( i
249eb 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
249ec 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
249ed 44 62 5d 3b 0a 20 20 7a 44 62 20 3d 20 70 44 62  Db];.  zDb = pDb
249ee 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73  ->zName;.  if( s
249ef 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
249f0 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49  pParse, SQLITE_I
249f1 4e 53 45 52 54 2c 20 70 54 61 62 2d 3e 7a 4e 61  NSERT, pTab->zNa
249f2 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20  me, 0, zDb) ){. 
249f3 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
249f4 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
249f5 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 20  * Figure out if 
249f6 77 65 20 68 61 76 65 20 61 6e 79 20 74 72 69 67  we have any trig
249f7 67 65 72 73 20 61 6e 64 20 69 66 20 74 68 65 20  gers and if the 
249f8 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a  table being.  **
249f9 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69   inserted into i
249fa 73 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69  s a view.  */.#i
249fb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
249fc 54 5f 54 52 49 47 47 45 52 0a 20 20 70 54 72 69  T_TRIGGER.  pTri
249fd 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72  gger = sqlite3Tr
249fe 69 67 67 65 72 73 45 78 69 73 74 28 70 50 61 72  iggersExist(pPar
249ff 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 49 4e 53  se, pTab, TK_INS
24a00 45 52 54 2c 20 30 2c 20 26 74 6d 61 73 6b 29 3b  ERT, 0, &tmask);
24a01 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54 61 62  .  isView = pTab
24a02 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65  ->pSelect!=0;.#e
24a03 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72  lse.# define pTr
24a04 69 67 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65  igger 0.# define
24a05 20 74 6d 61 73 6b 20 30 0a 23 20 64 65 66 69 6e   tmask 0.# defin
24a06 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
24a07 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
24a08 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65  OMIT_VIEW.# unde
24a09 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e  f isView.# defin
24a0a 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
24a0b 66 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 72  f.  assert( (pTr
24a0c 69 67 67 65 72 20 26 26 20 74 6d 61 73 6b 29 20  igger && tmask) 
24a0d 7c 7c 20 28 70 54 72 69 67 67 65 72 3d 3d 30 20  || (pTrigger==0 
24a0e 26 26 20 74 6d 61 73 6b 3d 3d 30 29 20 29 3b 0a  && tmask==0) );.
24a0f 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 61  .  /* Ensure tha
24a10 74 3a 0a 20 20 2a 20 20 28 61 29 20 74 68 65 20  t:.  *  (a) the 
24a11 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 72 65 61  table is not rea
24a12 64 2d 6f 6e 6c 79 2c 20 0a 20 20 2a 20 20 28 62  d-only, .  *  (b
24a13 29 20 74 68 61 74 20 69 66 20 69 74 20 69 73 20  ) that if it is 
24a14 61 20 76 69 65 77 20 74 68 65 6e 20 4f 4e 20 49  a view then ON I
24a15 4e 53 45 52 54 20 74 72 69 67 67 65 72 73 20 65  NSERT triggers e
24a16 78 69 73 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20  xist.  */.  if( 
24a17 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c  sqlite3IsReadOnl
24a18 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  y(pParse, pTab, 
24a19 74 6d 61 73 6b 29 20 29 7b 0a 20 20 20 20 67 6f  tmask) ){.    go
24a1a 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
24a1b 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  p;.  }.  assert(
24a1c 20 70 54 61 62 21 3d 30 20 29 3b 0a 0a 20 20 2f   pTab!=0 );..  /
24a1d 2a 20 49 66 20 70 54 61 62 20 69 73 20 72 65 61  * If pTab is rea
24a1e 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d 61 6b 65  lly a view, make
24a1f 20 73 75 72 65 20 69 74 20 68 61 73 20 62 65 65   sure it has bee
24a20 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20  n initialized.. 
24a21 20 2a 2a 20 56 69 65 77 47 65 74 43 6f 6c 75 6d   ** ViewGetColum
24a22 6e 4e 61 6d 65 73 28 29 20 69 73 20 61 20 6e 6f  nNames() is a no
24a23 2d 6f 70 20 69 66 20 70 54 61 62 20 69 73 20 6e  -op if pTab is n
24a24 6f 74 20 61 20 76 69 65 77 20 28 6f 72 20 76 69  ot a view (or vi
24a25 72 74 75 61 6c 20 0a 20 20 2a 2a 20 6d 6f 64 75  rtual .  ** modu
24a26 6c 65 20 74 61 62 6c 65 29 2e 0a 20 20 2a 2f 0a  le table)..  */.
24a27 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65    if( sqlite3Vie
24a28 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
24a29 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b  pParse, pTab) ){
24a2a 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
24a2b 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
24a2c 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 56   /* Allocate a V
24a2d 44 42 45 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73  DBE.  */.  v = s
24a2e 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
24a2f 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
24a30 30 20 29 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f  0 ) goto insert_
24a31 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 70  cleanup;.  if( p
24a32 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
24a33 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f   ) sqlite3VdbeCo
24a34 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20  untChanges(v);. 
24a35 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
24a36 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
24a37 73 65 2c 20 70 53 65 6c 65 63 74 20 7c 7c 20 70  se, pSelect || p
24a38 54 72 69 67 67 65 72 2c 20 69 44 62 29 3b 0a 0a  Trigger, iDb);..
24a39 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 61 72    /* if there ar
24a3a 65 20 72 6f 77 20 74 72 69 67 67 65 72 73 2c 20  e row triggers, 
24a3b 61 6c 6c 6f 63 61 74 65 20 61 20 74 65 6d 70 20  allocate a temp 
24a3c 74 61 62 6c 65 20 66 6f 72 20 6e 65 77 2e 2a 20  table for new.* 
24a3d 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 20  references. */. 
24a3e 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b   if( pTrigger ){
24a3f 0a 20 20 20 20 6e 65 77 49 64 78 20 3d 20 70 50  .    newIdx = pP
24a40 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
24a41 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
24a42 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 0a  E_OMIT_XFER_OPT.
24a43 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74    /* If the stat
24a44 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20  ement is of the 
24a45 66 6f 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  form.  **.  **  
24a46 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
24a47 20 3c 74 61 62 6c 65 31 3e 20 53 45 4c 45 43 54   <table1> SELECT
24a48 20 2a 20 46 52 4f 4d 20 3c 74 61 62 6c 65 32 3e   * FROM <table2>
24a49 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e  ;.  **.  ** Then
24a4a 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a   special optimiz
24a4b 61 74 69 6f 6e 73 20 63 61 6e 20 62 65 20 61 70  ations can be ap
24a4c 70 6c 69 65 64 20 74 68 61 74 20 6d 61 6b 65 20  plied that make 
24a4d 74 68 65 20 74 72 61 6e 73 66 65 72 0a 20 20 2a  the transfer.  *
24a4e 2a 20 76 65 72 79 20 66 61 73 74 20 61 6e 64 20  * very fast and 
24a4f 77 68 69 63 68 20 72 65 64 75 63 65 20 66 72 61  which reduce fra
24a50 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 69 6e  gmentation of in
24a51 64 69 63 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  dices..  **.  **
24a52 20 54 68 69 73 20 69 73 20 74 68 65 20 32 6e 64   This is the 2nd
24a53 20 74 65 6d 70 6c 61 74 65 2e 0a 20 20 2a 2f 0a   template..  */.
24a54 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30    if( pColumn==0
24a55 20 26 26 20 78 66 65 72 4f 70 74 69 6d 69 7a 61   && xferOptimiza
24a56 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61  tion(pParse, pTa
24a57 62 2c 20 70 53 65 6c 65 63 74 2c 20 6f 6e 45 72  b, pSelect, onEr
24a58 72 6f 72 2c 20 69 44 62 29 20 29 7b 0a 20 20 20  ror, iDb) ){.   
24a59 20 61 73 73 65 72 74 28 20 21 70 54 72 69 67 67   assert( !pTrigg
24a5a 65 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  er );.    assert
24a5b 28 20 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20  ( pList==0 );.  
24a5c 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
24a5d 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e 64 69  eanup;.  }.#endi
24a5e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
24a5f 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a 20 20  _XFER_OPT */..  
24a60 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
24a61 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74   AUTOINCREMENT t
24a62 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75 70 20 74 68  able, look up th
24a63 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65  e sequence numbe
24a64 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71  r in the.  ** sq
24a65 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
24a66 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 20 69 74  ble and store it
24a67 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20   in memory cell 
24a68 72 65 67 41 75 74 6f 69 6e 63 2e 0a 20 20 2a 2f  regAutoinc..  */
24a69 0a 20 20 72 65 67 41 75 74 6f 69 6e 63 20 3d 20  .  regAutoinc = 
24a6a 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 70 50 61  autoIncBegin(pPa
24a6b 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 29 3b  rse, iDb, pTab);
24a6c 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75  ..  /* Figure ou
24a6d 74 20 68 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d  t how many colum
24a6e 6e 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 73  ns of data are s
24a6f 75 70 70 6c 69 65 64 2e 20 20 49 66 20 74 68 65  upplied.  If the
24a70 20 64 61 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f   data.  ** is co
24a71 6d 69 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45  ming from a SELE
24a72 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  CT statement, th
24a73 65 6e 20 67 65 6e 65 72 61 74 65 20 61 20 63 6f  en generate a co
24a74 2d 72 6f 75 74 69 6e 65 20 74 68 61 74 0a 20 20  -routine that.  
24a75 2a 2a 20 70 72 6f 64 75 63 65 73 20 61 20 73 69  ** produces a si
24a76 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20  ngle row of the 
24a77 53 45 4c 45 43 54 20 6f 6e 20 65 61 63 68 20 69  SELECT on each i
24a78 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 0a  nvocation.  The.
24a79 20 20 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 20    ** co-routine 
24a7a 69 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65  is the common he
24a7b 61 64 65 72 20 74 6f 20 74 68 65 20 33 72 64 20  ader to the 3rd 
24a7c 61 6e 64 20 34 74 68 20 74 65 6d 70 6c 61 74 65  and 4th template
24a7d 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  s..  */.  if( pS
24a7e 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20  elect ){.    /* 
24a7f 44 61 74 61 20 69 73 20 63 6f 6d 69 6e 67 20 66  Data is coming f
24a80 72 6f 6d 20 61 20 53 45 4c 45 43 54 2e 20 20 47  rom a SELECT.  G
24a81 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
24a82 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61 74 20 53  implement that S
24a83 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 61 73 20  ELECT.    ** as 
24a84 61 20 63 6f 2d 72 6f 75 74 69 6e 65 2e 20 20 54  a co-routine.  T
24a85 68 65 20 63 6f 64 65 20 69 73 20 63 6f 6d 6d 6f  he code is commo
24a86 6e 20 74 6f 20 62 6f 74 68 20 74 68 65 20 33 72  n to both the 3r
24a87 64 20 61 6e 64 20 34 74 68 0a 20 20 20 20 2a 2a  d and 4th.    **
24a88 20 74 65 6d 70 6c 61 74 65 73 3a 0a 20 20 20 20   templates:.    
24a89 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
24a8a 20 20 45 4f 46 20 3c 2d 20 30 0a 20 20 20 20 2a    EOF <- 0.    *
24a8b 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d 20 41  *         X <- A
24a8c 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
24a8d 67 6f 74 6f 20 42 0a 20 20 20 20 2a 2a 20 20 20  goto B.    **   
24a8e 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20     A: setup for 
24a8f 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a  the SELECT.    *
24a90 2a 20 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f  *         loop o
24a91 76 65 72 20 74 68 65 20 74 61 62 6c 65 73 20 69  ver the tables i
24a92 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20  n the SELECT.   
24a93 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6c 6f   **           lo
24a94 61 64 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65  ad value into re
24a95 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e 0a 20 20  gister R..R+n.  
24a96 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79    **           y
24a97 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20 20  ield X.    **   
24a98 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20        end loop. 
24a99 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6c     **         cl
24a9a 65 61 6e 75 70 20 61 66 74 65 72 20 74 68 65 20  eanup after the 
24a9b 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20 20  SELECT.    **   
24a9c 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31 0a 20        EOF <- 1. 
24a9d 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 79 69     **         yi
24a9e 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20  eld X.    **    
24a9f 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a       halt-error.
24aa0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e      **.    ** On
24aa1 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e   each invocation
24aa2 20 6f 66 20 74 68 65 20 63 6f 2d 72 6f 75 74 69   of the co-routi
24aa3 6e 65 2c 20 69 74 20 70 75 74 73 20 61 20 73 69  ne, it puts a si
24aa4 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 0a  ngle row of the.
24aa5 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 72 65      ** SELECT re
24aa6 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74  sult into regist
24aa7 65 72 73 20 64 65 73 74 2e 69 4d 65 6d 2e 2e 2e  ers dest.iMem...
24aa8 64 65 73 74 2e 69 4d 65 6d 2b 64 65 73 74 2e 6e  dest.iMem+dest.n
24aa9 4d 65 6d 2d 31 2e 0a 20 20 20 20 2a 2a 20 28 54  Mem-1..    ** (T
24aaa 68 65 73 65 20 6f 75 74 70 75 74 20 72 65 67 69  hese output regi
24aab 73 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 63 61  sters are alloca
24aac 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 53 65  ted by sqlite3Se
24aad 6c 65 63 74 28 29 2e 29 20 20 57 68 65 6e 0a 20  lect().)  When. 
24aae 20 20 20 2a 2a 20 74 68 65 20 53 45 4c 45 43 54     ** the SELECT
24aaf 20 63 6f 6d 70 6c 65 74 65 73 2c 20 69 74 20 73   completes, it s
24ab0 65 74 73 20 74 68 65 20 45 4f 46 20 66 6c 61 67  ets the EOF flag
24ab1 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 45 6f   stored in regEo
24ab2 66 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  f..    */.    in
24ab3 74 20 72 63 2c 20 6a 31 3b 0a 0a 20 20 20 20 72  t rc, j1;..    r
24ab4 65 67 45 6f 66 20 3d 20 2b 2b 70 50 61 72 73 65  egEof = ++pParse
24ab5 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  ->nMem;.    sqli
24ab6 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24ab7 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
24ab8 72 65 67 45 6f 66 29 3b 20 20 20 20 20 20 2f 2a  regEof);      /*
24ab9 20 45 4f 46 20 3c 2d 20 30 20 2a 2f 0a 20 20 20   EOF <- 0 */.   
24aba 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
24abb 20 22 53 45 4c 45 43 54 20 65 6f 66 20 66 6c 61   "SELECT eof fla
24abc 67 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  g"));.    sqlite
24abd 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
24abe 26 64 65 73 74 2c 20 53 52 54 5f 43 6f 72 6f 75  &dest, SRT_Corou
24abf 74 69 6e 65 2c 20 2b 2b 70 50 61 72 73 65 2d 3e  tine, ++pParse->
24ac0 6e 4d 65 6d 29 3b 0a 20 20 20 20 61 64 64 72 53  nMem);.    addrS
24ac1 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 56  elect = sqlite3V
24ac2 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
24ac3 29 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  )+2;.    sqlite3
24ac4 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24ac5 5f 49 6e 74 65 67 65 72 2c 20 61 64 64 72 53 65  _Integer, addrSe
24ac6 6c 65 63 74 2d 31 2c 20 64 65 73 74 2e 69 50 61  lect-1, dest.iPa
24ac7 72 6d 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71  rm);.    j1 = sq
24ac8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24ac9 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30  v, OP_Goto, 0, 0
24aca 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
24acb 6e 74 28 28 76 2c 20 22 4a 75 6d 70 20 6f 76 65  nt((v, "Jump ove
24acc 72 20 53 45 4c 45 43 54 20 63 6f 72 6f 75 74 69  r SELECT corouti
24acd 6e 65 22 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 52  ne"));..    /* R
24ace 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65  esolve the expre
24acf 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45  ssions in the SE
24ad0 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61  LECT statement a
24ad1 6e 64 20 65 78 65 63 75 74 65 20 69 74 2e 20 2a  nd execute it. *
24ad2 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
24ad3 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
24ad4 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29   pSelect, &dest)
24ad5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
24ad6 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 7c 7c  arse->nErr==0 ||
24ad7 20 72 63 20 29 3b 0a 20 20 20 20 69 66 28 20 72   rc );.    if( r
24ad8 63 20 7c 7c 20 4e 45 56 45 52 28 70 50 61 72 73  c || NEVER(pPars
24ad9 65 2d 3e 6e 45 72 72 29 20 7c 7c 20 64 62 2d 3e  e->nErr) || db->
24ada 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
24adb 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72        goto inser
24adc 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  t_cleanup;.    }
24add 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
24ade 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
24adf 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 29  eger, 1, regEof)
24ae0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 45 4f 46  ;         /* EOF
24ae1 20 3c 2d 20 31 20 2a 2f 0a 20 20 20 20 73 71 6c   <- 1 */.    sql
24ae2 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
24ae3 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 74  , OP_Yield, dest
24ae4 2e 69 50 61 72 6d 29 3b 20 20 20 2f 2a 20 79 69  .iParm);   /* yi
24ae5 65 6c 64 20 58 20 2a 2f 0a 20 20 20 20 73 71 6c  eld X */.    sql
24ae6 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24ae7 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54  , OP_Halt, SQLIT
24ae8 45 5f 49 4e 54 45 52 4e 41 4c 2c 20 4f 45 5f 41  E_INTERNAL, OE_A
24ae9 62 6f 72 74 29 3b 0a 20 20 20 20 56 64 62 65 43  bort);.    VdbeC
24aea 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e 64 20  omment((v, "End 
24aeb 6f 66 20 53 45 4c 45 43 54 20 63 6f 72 6f 75 74  of SELECT corout
24aec 69 6e 65 22 29 29 3b 0a 20 20 20 20 73 71 6c 69  ine"));.    sqli
24aed 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
24aee 76 2c 20 6a 31 29 3b 20 20 20 20 20 20 20 20 20  v, j1);         
24aef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24af0 20 2f 2a 20 6c 61 62 65 6c 20 42 3a 20 2a 2f 0a   /* label B: */.
24af1 0a 20 20 20 20 72 65 67 46 72 6f 6d 53 65 6c 65  .    regFromSele
24af2 63 74 20 3d 20 64 65 73 74 2e 69 4d 65 6d 3b 0a  ct = dest.iMem;.
24af3 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
24af4 65 63 74 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20  ect->pEList );. 
24af5 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 53 65     nColumn = pSe
24af6 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  lect->pEList->nE
24af7 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  xpr;.    assert(
24af8 20 64 65 73 74 2e 6e 4d 65 6d 3d 3d 6e 43 6f 6c   dest.nMem==nCol
24af9 75 6d 6e 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53  umn );..    /* S
24afa 65 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  et useTempTable 
24afb 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 20 72  to TRUE if the r
24afc 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45 4c  esult of the SEL
24afd 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  ECT statement.  
24afe 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 77    ** should be w
24aff 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 74 65  ritten into a te
24b00 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28 74  mporary table (t
24b01 65 6d 70 6c 61 74 65 20 34 29 2e 20 20 53 65 74  emplate 4).  Set
24b02 20 74 6f 0a 20 20 20 20 2a 2a 20 46 41 4c 53 45   to.    ** FALSE
24b03 20 69 66 20 65 61 63 68 2a 20 72 6f 77 20 6f 66   if each* row of
24b04 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 6e 20   the SELECT can 
24b05 62 65 20 77 72 69 74 74 65 6e 20 64 69 72 65 63  be written direc
24b06 74 6c 79 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20  tly into.    ** 
24b07 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
24b08 74 61 62 6c 65 20 28 74 65 6d 70 6c 61 74 65 20  table (template 
24b09 33 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  3)..    **.    *
24b0a 2a 20 41 20 74 65 6d 70 20 74 61 62 6c 65 20 6d  * A temp table m
24b0b 75 73 74 20 62 65 20 75 73 65 64 20 69 66 20 74  ust be used if t
24b0c 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75  he table being u
24b0d 70 64 61 74 65 64 20 69 73 20 61 6c 73 6f 20 6f  pdated is also o
24b0e 6e 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  ne.    ** of the
24b0f 20 74 61 62 6c 65 73 20 62 65 69 6e 67 20 72 65   tables being re
24b10 61 64 20 62 79 20 74 68 65 20 53 45 4c 45 43 54  ad by the SELECT
24b11 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73   statement.  Als
24b12 6f 20 75 73 65 20 61 20 0a 20 20 20 20 2a 2a 20  o use a .    ** 
24b13 74 65 6d 70 20 74 61 62 6c 65 20 69 6e 20 74 68  temp table in th
24b14 65 20 63 61 73 65 20 6f 66 20 72 6f 77 20 74 72  e case of row tr
24b15 69 67 67 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20  iggers..    */. 
24b16 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20     if( pTrigger 
24b17 7c 7c 20 72 65 61 64 73 54 61 62 6c 65 28 76 2c  || readsTable(v,
24b18 20 61 64 64 72 53 65 6c 65 63 74 2c 20 69 44 62   addrSelect, iDb
24b19 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  , pTab) ){.     
24b1a 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d 20   useTempTable = 
24b1b 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  1;.    }..    if
24b1c 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29  ( useTempTable )
24b1d 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b  {.      /* Invok
24b1e 65 20 74 68 65 20 63 6f 72 6f 75 74 69 6e 65 20  e the coroutine 
24b1f 74 6f 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72  to extract infor
24b20 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  mation from the 
24b21 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a 2a 20  SELECT.      ** 
24b22 61 6e 64 20 61 64 64 20 69 74 20 74 6f 20 61 20  and add it to a 
24b23 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20  transient table 
24b24 73 72 63 54 61 62 2e 20 20 54 68 65 20 63 6f 64  srcTab.  The cod
24b25 65 20 67 65 6e 65 72 61 74 65 64 0a 20 20 20 20  e generated.    
24b26 20 20 2a 2a 20 68 65 72 65 20 69 73 20 66 72 6f    ** here is fro
24b27 6d 20 74 68 65 20 34 74 68 20 74 65 6d 70 6c 61  m the 4th templa
24b28 74 65 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  te:.      **.   
24b29 20 20 20 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70     **      B: op
24b2a 65 6e 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20  en temp table.  
24b2b 20 20 20 20 2a 2a 20 20 20 20 20 20 4c 3a 20 79      **      L: y
24b2c 69 65 6c 64 20 58 0a 20 20 20 20 20 20 2a 2a 20  ield X.      ** 
24b2d 20 20 20 20 20 20 20 20 69 66 20 45 4f 46 20 67          if EOF g
24b2e 6f 74 6f 20 4d 0a 20 20 20 20 20 20 2a 2a 20 20  oto M.      **  
24b2f 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 72 6f         insert ro
24b30 77 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 20 69 6e  w from R..R+n in
24b31 74 6f 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20  to temp table.  
24b32 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67      **         g
24b33 6f 74 6f 20 4c 0a 20 20 20 20 20 20 2a 2a 20 20  oto L.      **  
24b34 20 20 20 20 4d 3a 20 2e 2e 2e 0a 20 20 20 20 20      M: ....     
24b35 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65   */.      int re
24b36 67 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 2f  gRec;          /
24b37 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f  * Register to ho
24b38 6c 64 20 70 61 63 6b 65 64 20 72 65 63 6f 72 64  ld packed record
24b39 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65   */.      int re
24b3a 67 54 65 6d 70 52 6f 77 69 64 3b 20 20 20 20 2f  gTempRowid;    /
24b3b 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f  * Register to ho
24b3c 6c 64 20 74 65 6d 70 20 74 61 62 6c 65 20 52 4f  ld temp table RO
24b3d 57 49 44 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  WID */.      int
24b3e 20 61 64 64 72 54 6f 70 3b 20 20 20 20 20 20 20   addrTop;       
24b3f 20 20 2f 2a 20 4c 61 62 65 6c 20 22 4c 22 20 2a    /* Label "L" *
24b40 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  /.      int addr
24b41 49 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  If;          /* 
24b42 41 64 64 72 65 73 73 20 6f 66 20 6a 75 6d 70 20  Address of jump 
24b43 74 6f 20 4d 20 2a 2f 0a 0a 20 20 20 20 20 20 73  to M */..      s
24b44 72 63 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e  rcTab = pParse->
24b45 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 72 65  nTab++;.      re
24b46 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65  gRec = sqlite3Ge
24b47 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
24b48 3b 0a 20 20 20 20 20 20 72 65 67 54 65 6d 70 52  ;.      regTempR
24b49 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65  owid = sqlite3Ge
24b4a 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
24b4b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
24b4c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24b4d 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 73  OpenEphemeral, s
24b4e 72 63 54 61 62 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  rcTab, nColumn);
24b4f 0a 20 20 20 20 20 20 61 64 64 72 54 6f 70 20 3d  .      addrTop =
24b50 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24b51 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
24b52 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20  dest.iParm);.   
24b53 20 20 20 61 64 64 72 49 66 20 3d 20 73 71 6c 69     addrIf = sqli
24b54 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
24b55 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 29 3b   OP_If, regEof);
24b56 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
24b57 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
24b58 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 46 72  akeRecord, regFr
24b59 6f 6d 53 65 6c 65 63 74 2c 20 6e 43 6f 6c 75 6d  omSelect, nColum
24b5a 6e 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 20 20  n, regRec);.    
24b5b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24b5c 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77  Op2(v, OP_NewRow
24b5d 69 64 2c 20 73 72 63 54 61 62 2c 20 72 65 67 54  id, srcTab, regT
24b5e 65 6d 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  empRowid);.     
24b5f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24b60 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
24b61 20 73 72 63 54 61 62 2c 20 72 65 67 52 65 63 2c   srcTab, regRec,
24b62 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29 3b 0a   regTempRowid);.
24b63 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24b64 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
24b65 74 6f 2c 20 30 2c 20 61 64 64 72 54 6f 70 29 3b  to, 0, addrTop);
24b66 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
24b67 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
24b68 64 72 49 66 29 3b 0a 20 20 20 20 20 20 73 71 6c  drIf);.      sql
24b69 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
24b6a 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 65  eg(pParse, regRe
24b6b 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  c);.      sqlite
24b6c 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
24b6d 70 50 61 72 73 65 2c 20 72 65 67 54 65 6d 70 52  pParse, regTempR
24b6e 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  owid);.    }.  }
24b6f 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69  else{.    /* Thi
24b70 73 20 69 73 20 74 68 65 20 63 61 73 65 20 69 66  s is the case if
24b71 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68   the data for th
24b72 65 20 49 4e 53 45 52 54 20 69 73 20 63 6f 6d 69  e INSERT is comi
24b73 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55 45 53  ng from a VALUES
24b74 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 0a 20  .    ** clause. 
24b75 20 20 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f     */.    NameCo
24b76 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 6d  ntext sNC;.    m
24b77 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73  emset(&sNC, 0, s
24b78 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20  izeof(sNC));.   
24b79 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50   sNC.pParse = pP
24b7a 61 72 73 65 3b 0a 20 20 20 20 73 72 63 54 61 62  arse;.    srcTab
24b7b 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73 65 72   = -1;.    asser
24b7c 74 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 3d  t( useTempTable=
24b7d 3d 30 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 75 6d  =0 );.    nColum
24b7e 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 73  n = pList ? pLis
24b7f 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a 20 20  t->nExpr : 0;.  
24b80 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
24b81 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
24b82 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73    if( sqlite3Res
24b83 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
24b84 4e 43 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  NC, pList->a[i].
24b85 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
24b86 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
24b87 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20  eanup;.      }. 
24b88 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d     }.  }..  /* M
24b89 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e 75 6d  ake sure the num
24b8a 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
24b8b 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74  n the source dat
24b8c 61 20 6d 61 74 63 68 65 73 20 74 68 65 20 6e 75  a matches the nu
24b8d 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 63 6f 6c  mber.  ** of col
24b8e 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 73 65 72  umns to be inser
24b8f 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 61 62  ted into the tab
24b90 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49  le..  */.  if( I
24b91 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
24b92 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
24b93 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
24b94 29 7b 0a 20 20 20 20 20 20 6e 48 69 64 64 65 6e  ){.      nHidden
24b95 20 2b 3d 20 28 49 73 48 69 64 64 65 6e 43 6f 6c   += (IsHiddenCol
24b96 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  umn(&pTab->aCol[
24b97 69 5d 29 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20  i]) ? 1 : 0);.  
24b98 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 43    }.  }.  if( pC
24b99 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c  olumn==0 && nCol
24b9a 75 6d 6e 20 26 26 20 6e 43 6f 6c 75 6d 6e 21 3d  umn && nColumn!=
24b9b 28 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48 69 64  (pTab->nCol-nHid
24b9c 64 65 6e 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  den) ){.    sqli
24b9d 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
24b9e 73 65 2c 20 0a 20 20 20 20 20 20 20 22 74 61 62  se, .       "tab
24b9f 6c 65 20 25 53 20 68 61 73 20 25 64 20 63 6f 6c  le %S has %d col
24ba0 75 6d 6e 73 20 62 75 74 20 25 64 20 76 61 6c 75  umns but %d valu
24ba1 65 73 20 77 65 72 65 20 73 75 70 70 6c 69 65 64  es were supplied
24ba2 22 2c 0a 20 20 20 20 20 20 20 70 54 61 62 4c 69  ",.       pTabLi
24ba3 73 74 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f  st, 0, pTab->nCo
24ba4 6c 2d 6e 48 69 64 64 65 6e 2c 20 6e 43 6f 6c 75  l-nHidden, nColu
24ba5 6d 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  mn);.    goto in
24ba6 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
24ba7 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 21  }.  if( pColumn!
24ba8 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 21 3d 70  =0 && nColumn!=p
24ba9 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20  Column->nId ){. 
24baa 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
24bab 73 67 28 70 50 61 72 73 65 2c 20 22 25 64 20 76  sg(pParse, "%d v
24bac 61 6c 75 65 73 20 66 6f 72 20 25 64 20 63 6f 6c  alues for %d col
24bad 75 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d 6e 2c 20  umns", nColumn, 
24bae 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 3b 0a 20  pColumn->nId);. 
24baf 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
24bb0 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
24bb1 2a 20 49 66 20 74 68 65 20 49 4e 53 45 52 54 20  * If the INSERT 
24bb2 73 74 61 74 65 6d 65 6e 74 20 69 6e 63 6c 75 64  statement includ
24bb3 65 64 20 61 6e 20 49 44 4c 49 53 54 20 74 65 72  ed an IDLIST ter
24bb4 6d 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72  m, then make sur
24bb5 65 0a 20 20 2a 2a 20 61 6c 6c 20 65 6c 65 6d 65  e.  ** all eleme
24bb6 6e 74 73 20 6f 66 20 74 68 65 20 49 44 4c 49 53  nts of the IDLIS
24bb7 54 20 72 65 61 6c 6c 79 20 61 72 65 20 63 6f 6c  T really are col
24bb8 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c  umns of the tabl
24bb9 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 65  e and .  ** reme
24bba 6d 62 65 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20  mber the column 
24bbb 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a 20 20  indices..  **.  
24bbc 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  ** If the table 
24bbd 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50  has an INTEGER P
24bbe 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
24bbf 6e 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d  n and that colum
24bc0 6e 0a 20 20 2a 2a 20 69 73 20 6e 61 6d 65 64 20  n.  ** is named 
24bc1 69 6e 20 74 68 65 20 49 44 4c 49 53 54 2c 20 74  in the IDLIST, t
24bc2 68 65 6e 20 72 65 63 6f 72 64 20 69 6e 20 74 68  hen record in th
24bc3 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61 72 69  e keyColumn vari
24bc4 61 62 6c 65 0a 20 20 2a 2a 20 74 68 65 20 69 6e  able.  ** the in
24bc5 64 65 78 20 69 6e 74 6f 20 49 44 4c 49 53 54 20  dex into IDLIST 
24bc6 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  of the primary k
24bc7 65 79 20 63 6f 6c 75 6d 6e 2e 20 20 6b 65 79 43  ey column.  keyC
24bc8 6f 6c 75 6d 6e 20 69 73 0a 20 20 2a 2a 20 74 68  olumn is.  ** th
24bc9 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70  e index of the p
24bca 72 69 6d 61 72 79 20 6b 65 79 20 61 73 20 69 74  rimary key as it
24bcb 20 61 70 70 65 61 72 73 20 69 6e 20 49 44 4c 49   appears in IDLI
24bcc 53 54 2c 20 6e 6f 74 20 61 73 0a 20 20 2a 2a 20  ST, not as.  ** 
24bcd 69 73 20 61 70 70 65 61 72 73 20 69 6e 20 74 68  is appears in th
24bce 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65  e original table
24bcf 2e 20 20 28 54 68 65 20 69 6e 64 65 78 20 6f 66  .  (The index of
24bd0 20 74 68 65 20 70 72 69 6d 61 72 79 0a 20 20 2a   the primary.  *
24bd1 2a 20 6b 65 79 20 69 6e 20 74 68 65 20 6f 72 69  * key in the ori
24bd2 67 69 6e 61 6c 20 74 61 62 6c 65 20 69 73 20 70  ginal table is p
24bd3 54 61 62 2d 3e 69 50 4b 65 79 2e 29 0a 20 20 2a  Tab->iPKey.).  *
24bd4 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 20  /.  if( pColumn 
24bd5 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
24bd6 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20  i<pColumn->nId; 
24bd7 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c  i++){.      pCol
24bd8 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20 3d 20  umn->a[i].idx = 
24bd9 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  -1;.    }.    fo
24bda 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75 6d 6e  r(i=0; i<pColumn
24bdb 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nId; i++){.   
24bdc 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54     for(j=0; j<pT
24bdd 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a  ab->nCol; j++){.
24bde 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
24bdf 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 75  te3StrICmp(pColu
24be0 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  mn->a[i].zName, 
24be1 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e  pTab->aCol[j].zN
24be2 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ame)==0 ){.     
24be3 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b       pColumn->a[
24be4 69 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20 20 20 20  i].idx = j;.    
24be5 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 54 61        if( j==pTa
24be6 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20  b->iPKey ){.    
24be7 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d          keyColum
24be8 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 20  n = i;.         
24be9 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65   }.          bre
24bea 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
24beb 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
24bec 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b  j>=pTab->nCol ){
24bed 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
24bee 69 74 65 33 49 73 52 6f 77 69 64 28 70 43 6f 6c  ite3IsRowid(pCol
24bef 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  umn->a[i].zName)
24bf0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6b 65   ){.          ke
24bf1 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20 20 20  yColumn = i;.   
24bf2 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
24bf3 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
24bf4 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
24bf5 61 62 6c 65 20 25 53 20 68 61 73 20 6e 6f 20 63  able %S has no c
24bf6 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22 2c  olumn named %s",
24bf7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
24bf8 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 43 6f 6c  TabList, 0, pCol
24bf9 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  umn->a[i].zName)
24bfa 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72  ;.          pPar
24bfb 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  se->nErr++;.    
24bfc 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72        goto inser
24bfd 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20  t_cleanup;.     
24bfe 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
24bff 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
24c00 74 68 65 72 65 20 69 73 20 6e 6f 20 49 44 4c 49  there is no IDLI
24c01 53 54 20 74 65 72 6d 20 62 75 74 20 74 68 65 20  ST term but the 
24c02 74 61 62 6c 65 20 68 61 73 20 61 6e 20 69 6e 74  table has an int
24c03 65 67 65 72 20 70 72 69 6d 61 72 79 0a 20 20 2a  eger primary.  *
24c04 2a 20 6b 65 79 2c 20 74 68 65 20 73 65 74 20 74  * key, the set t
24c05 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61 72  he keyColumn var
24c06 69 61 62 6c 65 20 74 6f 20 74 68 65 20 70 72 69  iable to the pri
24c07 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d 6e 20  mary key column 
24c08 69 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 74 68  index.  ** in th
24c09 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65  e original table
24c0a 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 20 2a   definition..  *
24c0b 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d  /.  if( pColumn=
24c0c 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 3e 30 20  =0 && nColumn>0 
24c0d 29 7b 0a 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e  ){.    keyColumn
24c0e 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a   = pTab->iPKey;.
24c0f 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74    }..  /* Open t
24c10 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20 66 6f  he temp table fo
24c11 72 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 74  r FOR EACH ROW t
24c12 72 69 67 67 65 72 73 0a 20 20 2a 2f 0a 20 20 69  riggers.  */.  i
24c13 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  f( pTrigger ){. 
24c14 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24c15 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 50  dOp3(v, OP_OpenP
24c16 73 65 75 64 6f 2c 20 6e 65 77 49 64 78 2c 20 30  seudo, newIdx, 0
24c17 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20  , pTab->nCol);. 
24c18 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20 49 6e 69   }.    .  /* Ini
24c19 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e  tialize the coun
24c1a 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 62 65 20  t of rows to be 
24c1b 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f 0a 20 20  inserted.  */.  
24c1c 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20  if( db->flags & 
24c1d 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
24c1e 20 29 7b 0a 20 20 20 20 72 65 67 52 6f 77 43 6f   ){.    regRowCo
24c1f 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  unt = ++pParse->
24c20 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
24c21 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24c22 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65  P_Integer, 0, re
24c23 67 52 6f 77 43 6f 75 6e 74 29 3b 0a 20 20 7d 0a  gRowCount);.  }.
24c24 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
24c25 20 6e 6f 74 20 61 20 76 69 65 77 2c 20 6f 70 65   not a view, ope
24c26 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  n the table and 
24c27 61 6e 64 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  and all indices 
24c28 2a 2f 0a 20 20 69 66 28 20 21 69 73 56 69 65 77  */.  if( !isView
24c29 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 49 64 78   ){.    int nIdx
24c2a 3b 0a 0a 20 20 20 20 62 61 73 65 43 75 72 20 3d  ;..    baseCur =
24c2b 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20   pParse->nTab;. 
24c2c 20 20 20 6e 49 64 78 20 3d 20 73 71 6c 69 74 65     nIdx = sqlite
24c2d 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64  3OpenTableAndInd
24c2e 69 63 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ices(pParse, pTa
24c2f 62 2c 20 62 61 73 65 43 75 72 2c 20 4f 50 5f 4f  b, baseCur, OP_O
24c30 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 61  penWrite);.    a
24c31 52 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33  RegIdx = sqlite3
24c32 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
24c33 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 49 64  sizeof(int)*(nId
24c34 78 2b 31 29 29 3b 0a 20 20 20 20 69 66 28 20 61  x+1));.    if( a
24c35 52 65 67 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20  RegIdx==0 ){.   
24c36 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
24c37 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
24c38 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 49 64    for(i=0; i<nId
24c39 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61  x; i++){.      a
24c3a 52 65 67 49 64 78 5b 69 5d 20 3d 20 2b 2b 70 50  RegIdx[i] = ++pP
24c3b 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
24c3c 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73  }.  }..  /* This
24c3d 20 69 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74   is the top of t
24c3e 68 65 20 6d 61 69 6e 20 69 6e 73 65 72 74 69 6f  he main insertio
24c3f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66 28 20  n loop */.  if( 
24c40 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a  useTempTable ){.
24c41 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63      /* This bloc
24c42 6b 20 63 6f 64 65 73 20 74 68 65 20 74 6f 70 20  k codes the top 
24c43 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54  of loop only.  T
24c44 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70  he complete loop
24c45 20 69 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 66   is the.    ** f
24c46 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75 64 6f 63  ollowing pseudoc
24c47 6f 64 65 20 28 74 65 6d 70 6c 61 74 65 20 34 29  ode (template 4)
24c48 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
24c49 20 20 20 20 20 20 20 20 72 65 77 69 6e 64 20 74          rewind t
24c4a 65 6d 70 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a  emp table.    **
24c4b 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20 6f 76        C: loop ov
24c4c 65 72 20 72 6f 77 73 20 6f 66 20 69 6e 74 65 72  er rows of inter
24c4d 6d 65 64 69 61 74 65 20 74 61 62 6c 65 0a 20 20  mediate table.  
24c4e 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74    **           t
24c4f 72 61 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66  ransfer values f
24c50 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65  orm intermediate
24c51 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62   table into <tab
24c52 6c 65 3e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  le>.    **      
24c53 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20 20 20 20     end loop.    
24c54 2a 2a 20 20 20 20 20 20 44 3a 20 2e 2e 2e 0a 20  **      D: .... 
24c55 20 20 20 2a 2f 0a 20 20 20 20 61 64 64 72 49 6e     */.    addrIn
24c56 73 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  sTop = sqlite3Vd
24c57 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52  beAddOp1(v, OP_R
24c58 65 77 69 6e 64 2c 20 73 72 63 54 61 62 29 3b 0a  ewind, srcTab);.
24c59 20 20 20 20 61 64 64 72 43 6f 6e 74 20 3d 20 73      addrCont = s
24c5a 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
24c5b 74 41 64 64 72 28 76 29 3b 0a 20 20 7d 65 6c 73  tAddr(v);.  }els
24c5c 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b  e if( pSelect ){
24c5d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f  .    /* This blo
24c5e 63 6b 20 63 6f 64 65 73 20 74 68 65 20 74 6f 70  ck codes the top
24c5f 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e 20 20   of loop only.  
24c60 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c 6f 6f  The complete loo
24c61 70 20 69 73 20 74 68 65 0a 20 20 20 20 2a 2a 20  p is the.    ** 
24c62 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75 64 6f  following pseudo
24c63 63 6f 64 65 20 28 74 65 6d 70 6c 61 74 65 20 33  code (template 3
24c64 29 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ):.    **.    **
24c65 20 20 20 20 20 20 43 3a 20 79 69 65 6c 64 20 58        C: yield X
24c66 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
24c67 69 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a 20 20  if EOF goto D.  
24c68 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73    **         ins
24c69 65 72 74 20 74 68 65 20 73 65 6c 65 63 74 20 72  ert the select r
24c6a 65 73 75 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c  esult into <tabl
24c6b 65 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 20  e> from R..R+n. 
24c6c 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67 6f     **         go
24c6d 74 6f 20 43 0a 20 20 20 20 2a 2a 20 20 20 20 20  to C.    **     
24c6e 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f 0a 20   D: ....    */. 
24c6f 20 20 20 61 64 64 72 43 6f 6e 74 20 3d 20 73 71     addrCont = sq
24c70 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
24c71 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73  v, OP_Yield, des
24c72 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 61 64  t.iParm);.    ad
24c73 64 72 49 6e 73 54 6f 70 20 3d 20 73 71 6c 69 74  drInsTop = sqlit
24c74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
24c75 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 29 3b 0a  OP_If, regEof);.
24c76 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61    }..  /* Alloca
24c77 74 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72  te registers for
24c78 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 6f 77   holding the row
24c79 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f  id of the new ro
24c7a 77 2c 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74  w,.  ** the cont
24c7b 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 72  ent of the new r
24c7c 6f 77 2c 20 61 6e 64 20 74 68 65 20 61 73 73 65  ow, and the asse
24c7d 6d 62 6c 69 65 64 20 72 6f 77 20 72 65 63 6f 72  mblied row recor
24c7e 64 2e 0a 20 20 2a 2f 0a 20 20 72 65 67 52 65 63  d..  */.  regRec
24c7f 6f 72 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  ord = ++pParse->
24c80 6e 4d 65 6d 3b 0a 20 20 72 65 67 52 6f 77 69 64  nMem;.  regRowid
24c81 20 3d 20 72 65 67 49 6e 73 20 3d 20 70 50 61 72   = regIns = pPar
24c82 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 70 50  se->nMem+1;.  pP
24c83 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54  arse->nMem += pT
24c84 61 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20  ab->nCol + 1;.  
24c85 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
24c86 61 62 29 20 29 7b 0a 20 20 20 20 72 65 67 52 6f  ab) ){.    regRo
24c87 77 69 64 2b 2b 3b 0a 20 20 20 20 70 50 61 72 73  wid++;.    pPars
24c88 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 7d 0a 20  e->nMem++;.  }. 
24c89 20 72 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f   regData = regRo
24c8a 77 69 64 2b 31 3b 0a 0a 20 20 2f 2a 20 52 75 6e  wid+1;..  /* Run
24c8b 20 74 68 65 20 42 45 46 4f 52 45 20 61 6e 64 20   the BEFORE and 
24c8c 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
24c8d 65 72 73 2c 20 69 66 20 74 68 65 72 65 20 61 72  ers, if there ar
24c8e 65 20 61 6e 79 0a 20 20 2a 2f 0a 20 20 65 6e 64  e any.  */.  end
24c8f 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33  OfLoop = sqlite3
24c90 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
24c91 3b 0a 20 20 69 66 28 20 74 6d 61 73 6b 20 26 20  ;.  if( tmask & 
24c92 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 29  TRIGGER_BEFORE )
24c93 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 54 72 69  {.    int regTri
24c94 67 52 6f 77 69 64 3b 0a 20 20 20 20 69 6e 74 20  gRowid;.    int 
24c95 72 65 67 43 6f 6c 73 3b 0a 20 20 20 20 69 6e 74  regCols;.    int
24c96 20 72 65 67 52 65 63 3b 0a 0a 20 20 20 20 2f 2a   regRec;..    /*
24c97 20 62 75 69 6c 64 20 74 68 65 20 4e 45 57 2e 2a   build the NEW.*
24c98 20 72 65 66 65 72 65 6e 63 65 20 72 6f 77 2e 20   reference row. 
24c99 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 74 68   Note that if th
24c9a 65 72 65 20 69 73 20 61 6e 20 49 4e 54 45 47 45  ere is an INTEGE
24c9b 52 0a 20 20 20 20 2a 2a 20 50 52 49 4d 41 52 59  R.    ** PRIMARY
24c9c 20 4b 45 59 20 69 6e 74 6f 20 77 68 69 63 68 20   KEY into which 
24c9d 61 20 4e 55 4c 4c 20 69 73 20 62 65 69 6e 67 20  a NULL is being 
24c9e 69 6e 73 65 72 74 65 64 2c 20 74 68 61 74 20 4e  inserted, that N
24c9f 55 4c 4c 20 77 69 6c 6c 20 62 65 0a 20 20 20 20  ULL will be.    
24ca0 2a 2a 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e  ** translated in
24ca1 74 6f 20 61 20 75 6e 69 71 75 65 20 49 44 20 66  to a unique ID f
24ca2 6f 72 20 74 68 65 20 72 6f 77 2e 20 20 42 75 74  or the row.  But
24ca3 20 6f 6e 20 61 20 42 45 46 4f 52 45 20 74 72 69   on a BEFORE tri
24ca4 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20 77 65 20  gger,.    ** we 
24ca5 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 77 68 61 74  do not know what
24ca6 20 74 68 65 20 75 6e 69 71 75 65 20 49 44 20 77   the unique ID w
24ca7 69 6c 6c 20 62 65 20 28 62 65 63 61 75 73 65 20  ill be (because 
24ca8 74 68 65 20 69 6e 73 65 72 74 20 68 61 73 0a 20  the insert has. 
24ca9 20 20 20 2a 2a 20 6e 6f 74 20 68 61 70 70 65 6e     ** not happen
24caa 65 64 20 79 65 74 29 20 73 6f 20 77 65 20 73 75  ed yet) so we su
24cab 62 73 74 69 74 75 74 65 20 61 20 72 6f 77 69 64  bstitute a rowid
24cac 20 6f 66 20 2d 31 0a 20 20 20 20 2a 2f 0a 20 20   of -1.    */.  
24cad 20 20 72 65 67 54 72 69 67 52 6f 77 69 64 20 3d    regTrigRowid =
24cae 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
24caf 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
24cb0 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3c 30 20  if( keyColumn<0 
24cb1 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
24cb2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24cb3 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65  _Integer, -1, re
24cb4 67 54 72 69 67 52 6f 77 69 64 29 3b 0a 20 20 20  gTrigRowid);.   
24cb5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e   }else{.      in
24cb6 74 20 6a 31 3b 0a 20 20 20 20 20 20 69 66 28 20  t j1;.      if( 
24cb7 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a  useTempTable ){.
24cb8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
24cb9 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
24cba 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20  Column, srcTab, 
24cbb 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 54 72  keyColumn, regTr
24cbc 69 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  igRowid);.      
24cbd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
24cbe 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 3d 3d  ssert( pSelect==
24cbf 30 20 29 3b 20 20 2f 2a 20 4f 74 68 65 72 77 69  0 );  /* Otherwi
24cc0 73 65 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  se useTempTable 
24cc1 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 20  is true */.     
24cc2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
24cc3 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74  de(pParse, pList
24cc4 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70  ->a[keyColumn].p
24cc5 45 78 70 72 2c 20 72 65 67 54 72 69 67 52 6f 77  Expr, regTrigRow
24cc6 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  id);.      }.   
24cc7 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56     j1 = sqlite3V
24cc8 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
24cc9 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 54 72 69 67  NotNull, regTrig
24cca 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71  Rowid);.      sq
24ccb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24ccc 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d  v, OP_Integer, -
24ccd 31 2c 20 72 65 67 54 72 69 67 52 6f 77 69 64 29  1, regTrigRowid)
24cce 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
24ccf 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
24cd0 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
24cd1 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
24cd2 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67  P_MustBeInt, reg
24cd3 54 72 69 67 52 6f 77 69 64 29 3b 0a 20 20 20 20  TrigRowid);.    
24cd4 7d 0a 0a 20 20 20 20 2f 2a 20 43 61 6e 6e 6f 74  }..    /* Cannot
24cd5 20 68 61 76 65 20 74 72 69 67 67 65 72 73 20 6f   have triggers o
24cd6 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  n a virtual tabl
24cd7 65 2e 20 49 66 20 69 74 20 77 65 72 65 20 70 6f  e. If it were po
24cd8 73 73 69 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 74  ssible,.    ** t
24cd9 68 69 73 20 62 6c 6f 63 6b 20 77 6f 75 6c 64 20  his block would 
24cda 68 61 76 65 20 74 6f 20 61 63 63 6f 75 6e 74 20  have to account 
24cdb 66 6f 72 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d  for hidden colum
24cdc 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  n..    */.    as
24cdd 73 65 72 74 28 21 49 73 56 69 72 74 75 61 6c 28  sert(!IsVirtual(
24cde 70 54 61 62 29 29 3b 0a 0a 20 20 20 20 2f 2a 20  pTab));..    /* 
24cdf 43 72 65 61 74 65 20 74 68 65 20 6e 65 77 20 63  Create the new c
24ce0 6f 6c 75 6d 6e 20 64 61 74 61 0a 20 20 20 20 2a  olumn data.    *
24ce1 2f 0a 20 20 20 20 72 65 67 43 6f 6c 73 20 3d 20  /.    regCols = 
24ce2 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61  sqlite3GetTempRa
24ce3 6e 67 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  nge(pParse, pTab
24ce4 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 66 6f 72  ->nCol);.    for
24ce5 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
24ce6 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
24ce7 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29  if( pColumn==0 )
24ce8 7b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 69 3b  {.        j = i;
24ce9 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
24cea 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
24ceb 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a  <pColumn->nId; j
24cec 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
24ced 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d  f( pColumn->a[j]
24cee 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b  .idx==i ) break;
24cef 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
24cf0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f   }.      if( pCo
24cf1 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75  lumn && j>=pColu
24cf2 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 20  mn->nId ){.     
24cf3 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
24cf4 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  de(pParse, pTab-
24cf5 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20  >aCol[i].pDflt, 
24cf6 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 20  regCols+i);.    
24cf7 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65 54    }else if( useT
24cf8 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
24cf9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24cfa 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
24cfb 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20 72  mn, srcTab, j, r
24cfc 65 67 43 6f 6c 73 2b 69 29 3b 20 0a 20 20 20 20  egCols+i); .    
24cfd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
24cfe 20 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74   assert( pSelect
24cff 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74 68 65 72 77  ==0 ); /* Otherw
24d00 69 73 65 20 75 73 65 54 65 6d 70 54 61 62 6c 65  ise useTempTable
24d01 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20   is true */.    
24d02 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
24d03 6f 64 65 41 6e 64 43 61 63 68 65 28 70 50 61 72  odeAndCache(pPar
24d04 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e  se, pList->a[j].
24d05 70 45 78 70 72 2c 20 72 65 67 43 6f 6c 73 2b 69  pExpr, regCols+i
24d06 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
24d07 0a 20 20 20 20 72 65 67 52 65 63 20 3d 20 73 71  .    regRec = sq
24d08 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
24d09 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c  pParse);.    sql
24d0a 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
24d0b 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
24d0c 20 72 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e   regCols, pTab->
24d0d 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a 0a  nCol, regRec);..
24d0e 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
24d0f 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 6e 20 61  s an INSERT on a
24d10 20 76 69 65 77 20 77 69 74 68 20 61 6e 20 49 4e   view with an IN
24d11 53 54 45 41 44 20 4f 46 20 49 4e 53 45 52 54 20  STEAD OF INSERT 
24d12 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20  trigger,.    ** 
24d13 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 61  do not attempt a
24d14 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 62  ny conversions b
24d15 65 66 6f 72 65 20 61 73 73 65 6d 62 6c 69 6e 67  efore assembling
24d16 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20   the record..   
24d17 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   ** If this is a
24d18 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 61 74 74   real table, att
24d19 65 6d 70 74 20 63 6f 6e 76 65 72 73 69 6f 6e 73  empt conversions
24d1a 20 61 73 20 72 65 71 75 69 72 65 64 20 62 79 20   as required by 
24d1b 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65  the.    ** table
24d1c 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 69   column affiniti
24d1d 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  es..    */.    i
24d1e 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  f( !isView ){.  
24d1f 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65      sqlite3Table
24d20 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
24d21 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Tab);.    }.    
24d22 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24d23 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
24d24 6e 65 77 49 64 78 2c 20 72 65 67 52 65 63 2c 20  newIdx, regRec, 
24d25 72 65 67 54 72 69 67 52 6f 77 69 64 29 3b 0a 20  regTrigRowid);. 
24d26 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
24d27 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
24d28 20 72 65 67 52 65 63 29 3b 0a 20 20 20 20 73 71   regRec);.    sq
24d29 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
24d2a 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 54  Reg(pParse, regT
24d2b 72 69 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  rigRowid);.    s
24d2c 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
24d2d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
24d2e 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43  egCols, pTab->nC
24d2f 6f 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 72  ol);..    /* Fir
24d30 65 20 42 45 46 4f 52 45 20 6f 72 20 49 4e 53 54  e BEFORE or INST
24d31 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 20  EAD OF triggers 
24d32 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
24d33 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
24d34 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
24d35 72 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c  r, TK_INSERT, 0,
24d36 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c   TRIGGER_BEFORE,
24d37 20 0a 20 20 20 20 20 20 20 20 70 54 61 62 2c 20   .        pTab, 
24d38 6e 65 77 49 64 78 2c 20 2d 31 2c 20 6f 6e 45 72  newIdx, -1, onEr
24d39 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20  ror, endOfLoop, 
24d3a 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 67  0, 0) ){.      g
24d3b 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
24d3c 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  up;.    }.  }.. 
24d3d 20 2f 2a 20 50 75 73 68 20 74 68 65 20 72 65 63   /* Push the rec
24d3e 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  ord number for t
24d3f 68 65 20 6e 65 77 20 65 6e 74 72 79 20 6f 6e 74  he new entry ont
24d40 6f 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68  o the stack.  Th
24d41 65 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 6e 75  e.  ** record nu
24d42 6d 62 65 72 20 69 73 20 61 20 72 61 6e 64 6f 6d  mber is a random
24d43 6c 79 20 67 65 6e 65 72 61 74 65 20 69 6e 74 65  ly generate inte
24d44 67 65 72 20 63 72 65 61 74 65 64 20 62 79 20 4e  ger created by N
24d45 65 77 52 6f 77 69 64 0a 20 20 2a 2a 20 65 78 63  ewRowid.  ** exc
24d46 65 70 74 20 77 68 65 6e 20 74 68 65 20 74 61 62  ept when the tab
24d47 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45  le has an INTEGE
24d48 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f  R PRIMARY KEY co
24d49 6c 75 6d 6e 2c 20 69 6e 20 77 68 69 63 68 0a 20  lumn, in which. 
24d4a 20 2a 2a 20 63 61 73 65 20 74 68 65 20 72 65 63   ** case the rec
24d4b 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 74 68  ord number is th
24d4c 65 20 73 61 6d 65 20 61 73 20 74 68 61 74 20 63  e same as that c
24d4d 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69  olumn. .  */.  i
24d4e 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  f( !isView ){.  
24d4f 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
24d50 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 2f  pTab) ){.      /
24d51 2a 20 54 68 65 20 72 6f 77 20 74 68 61 74 20 74  * The row that t
24d52 68 65 20 56 55 70 64 61 74 65 20 6f 70 63 6f 64  he VUpdate opcod
24d53 65 20 77 69 6c 6c 20 64 65 6c 65 74 65 3a 20 6e  e will delete: n
24d54 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  one */.      sql
24d55 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24d56 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65  , OP_Null, 0, re
24d57 67 49 6e 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20  gIns);.    }.   
24d58 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d   if( keyColumn>=
24d59 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 75  0 ){.      if( u
24d5a 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20  seTempTable ){. 
24d5b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
24d5c 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
24d5d 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b  olumn, srcTab, k
24d5e 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f 77  eyColumn, regRow
24d5f 69 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  id);.      }else
24d60 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a   if( pSelect ){.
24d61 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
24d62 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24d63 53 43 6f 70 79 2c 20 72 65 67 46 72 6f 6d 53 65  SCopy, regFromSe
24d64 6c 65 63 74 2b 6b 65 79 43 6f 6c 75 6d 6e 2c 20  lect+keyColumn, 
24d65 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
24d66 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
24d67 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20  VdbeOp *pOp;.   
24d68 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
24d69 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69  Code(pParse, pLi
24d6a 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d  st->a[keyColumn]
24d6b 2e 70 45 78 70 72 2c 20 72 65 67 52 6f 77 69 64  .pExpr, regRowid
24d6c 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 20 3d  );.        pOp =
24d6d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f   sqlite3VdbeGetO
24d6e 70 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65  p(v, sqlite3Vdbe
24d6f 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2d  CurrentAddr(v) -
24d70 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   1);.        if(
24d71 20 41 4c 57 41 59 53 28 70 4f 70 29 20 26 26 20   ALWAYS(pOp) && 
24d72 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
24d73 4e 75 6c 6c 20 26 26 20 21 49 73 56 69 72 74 75  Null && !IsVirtu
24d74 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
24d75 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c 61 67        appendFlag
24d76 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
24d77 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50  pOp->opcode = OP
24d78 5f 4e 65 77 52 6f 77 69 64 3b 0a 20 20 20 20 20  _NewRowid;.     
24d79 20 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 62       pOp->p1 = b
24d7a 61 73 65 43 75 72 3b 0a 20 20 20 20 20 20 20 20  aseCur;.        
24d7b 20 20 70 4f 70 2d 3e 70 32 20 3d 20 72 65 67 52    pOp->p2 = regR
24d7c 6f 77 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20  owid;.          
24d7d 70 4f 70 2d 3e 70 33 20 3d 20 72 65 67 41 75 74  pOp->p3 = regAut
24d7e 6f 69 6e 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  oinc;.        }.
24d7f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a        }.      /*
24d80 20 49 66 20 74 68 65 20 50 52 49 4d 41 52 59 20   If the PRIMARY 
24d81 4b 45 59 20 65 78 70 72 65 73 73 69 6f 6e 20 69  KEY expression i
24d82 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 75 73 65  s NULL, then use
24d83 20 4f 50 5f 4e 65 77 52 6f 77 69 64 0a 20 20 20   OP_NewRowid.   
24d84 20 20 20 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74     ** to generat
24d85 65 20 61 20 75 6e 69 71 75 65 20 70 72 69 6d 61  e a unique prima
24d86 72 79 20 6b 65 79 20 76 61 6c 75 65 2e 0a 20 20  ry key value..  
24d87 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
24d88 20 21 61 70 70 65 6e 64 46 6c 61 67 20 29 7b 0a   !appendFlag ){.
24d89 20 20 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 0a          int j1;.
24d8a 20 20 20 20 20 20 20 20 69 66 28 20 21 49 73 56          if( !IsV
24d8b 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
24d8c 20 20 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73            j1 = s
24d8d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
24d8e 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20  (v, OP_NotNull, 
24d8f 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
24d90 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24d91 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 77  AddOp3(v, OP_New
24d92 52 6f 77 69 64 2c 20 62 61 73 65 43 75 72 2c 20  Rowid, baseCur, 
24d93 72 65 67 52 6f 77 69 64 2c 20 72 65 67 41 75 74  regRowid, regAut
24d94 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 20  oinc);.         
24d95 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
24d96 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20  Here(v, j1);.   
24d97 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
24d98 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74        j1 = sqlit
24d99 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
24d9a 72 28 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r(v);.          
24d9b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24d9c 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  2(v, OP_IsNull, 
24d9d 72 65 67 52 6f 77 69 64 2c 20 6a 31 2b 32 29 3b  regRowid, j1+2);
24d9e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
24d9f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24da0 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42  dOp1(v, OP_MustB
24da1 65 49 6e 74 2c 20 72 65 67 52 6f 77 69 64 29 3b  eInt, regRowid);
24da2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
24da3 73 65 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  se if( IsVirtual
24da4 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
24da5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24da6 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
24da7 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
24da8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
24da9 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
24daa 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 62  , OP_NewRowid, b
24dab 61 73 65 43 75 72 2c 20 72 65 67 52 6f 77 69 64  aseCur, regRowid
24dac 2c 20 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a 20  , regAutoinc);. 
24dad 20 20 20 20 20 61 70 70 65 6e 64 46 6c 61 67 20       appendFlag 
24dae 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  = 1;.    }.    a
24daf 75 74 6f 49 6e 63 53 74 65 70 28 70 50 61 72 73  utoIncStep(pPars
24db0 65 2c 20 72 65 67 41 75 74 6f 69 6e 63 2c 20 72  e, regAutoinc, r
24db1 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f  egRowid);..    /
24db2 2a 20 50 75 73 68 20 6f 6e 74 6f 20 74 68 65 20  * Push onto the 
24db3 73 74 61 63 6b 2c 20 64 61 74 61 20 66 6f 72 20  stack, data for 
24db4 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74  all columns of t
24db5 68 65 20 6e 65 77 20 65 6e 74 72 79 2c 20 62 65  he new entry, be
24db6 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 77  ginning.    ** w
24db7 69 74 68 20 74 68 65 20 66 69 72 73 74 20 63 6f  ith the first co
24db8 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  lumn..    */.   
24db9 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 0a 20 20   nHidden = 0;.  
24dba 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
24dbb 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  b->nCol; i++){. 
24dbc 20 20 20 20 20 69 6e 74 20 69 52 65 67 53 74 6f       int iRegSto
24dbd 72 65 20 3d 20 72 65 67 52 6f 77 69 64 2b 31 2b  re = regRowid+1+
24dbe 69 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  i;.      if( i==
24dbf 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pTab->iPKey ){. 
24dc0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61         /* The va
24dc1 6c 75 65 20 6f 66 20 74 68 65 20 49 4e 54 45 47  lue of the INTEG
24dc2 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63  ER PRIMARY KEY c
24dc3 6f 6c 75 6d 6e 20 69 73 20 61 6c 77 61 79 73 20  olumn is always 
24dc4 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20  a NULL..        
24dc5 2a 2a 20 57 68 65 6e 65 76 65 72 20 74 68 69 73  ** Whenever this
24dc6 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 61 64 2c   column is read,
24dc7 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
24dc8 65 72 20 77 69 6c 6c 20 62 65 20 73 75 62 73 74  er will be subst
24dc9 69 74 75 74 65 64 0a 20 20 20 20 20 20 20 20 2a  ituted.        *
24dca 2a 20 69 6e 20 69 74 73 20 70 6c 61 63 65 2e 20  * in its place. 
24dcb 20 53 6f 20 77 69 6c 6c 20 66 69 6c 6c 20 74 68   So will fill th
24dcc 69 73 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 61  is column with a
24dcd 20 4e 55 4c 4c 20 74 6f 20 61 76 6f 69 64 0a 20   NULL to avoid. 
24dce 20 20 20 20 20 20 20 2a 2a 20 74 61 6b 69 6e 67         ** taking
24dcf 20 75 70 20 64 61 74 61 20 73 70 61 63 65 20 77   up data space w
24dd0 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ith information 
24dd1 74 68 61 74 20 77 69 6c 6c 20 6e 65 76 65 72 20  that will never 
24dd2 62 65 20 75 73 65 64 2e 20 2a 2f 0a 20 20 20 20  be used. */.    
24dd3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24dd4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
24dd5 2c 20 30 2c 20 69 52 65 67 53 74 6f 72 65 29 3b  , 0, iRegStore);
24dd6 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
24dd7 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
24dd8 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20   if( pColumn==0 
24dd9 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  ){.        if( I
24dda 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70  sHiddenColumn(&p
24ddb 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 29 7b  Tab->aCol[i]) ){
24ddc 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
24ddd 74 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  t( IsVirtual(pTa
24dde 62 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  b) );.          
24ddf 6a 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20  j = -1;.        
24de0 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20 20    nHidden++;.   
24de1 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
24de2 20 20 20 20 20 20 6a 20 3d 20 69 20 2d 20 6e 48        j = i - nH
24de3 69 64 64 65 6e 3b 0a 20 20 20 20 20 20 20 20 7d  idden;.        }
24de4 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
24de5 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
24de6 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a  <pColumn->nId; j
24de7 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
24de8 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d  f( pColumn->a[j]
24de9 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b  .idx==i ) break;
24dea 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
24deb 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3c 30   }.      if( j<0
24dec 20 7c 7c 20 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 7c   || nColumn==0 |
24ded 7c 20 28 70 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e  | (pColumn && j>
24dee 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 20 29  =pColumn->nId) )
24def 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
24df0 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
24df1 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e  , pTab->aCol[i].
24df2 70 44 66 6c 74 2c 20 69 52 65 67 53 74 6f 72 65  pDflt, iRegStore
24df3 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
24df4 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  f( useTempTable 
24df5 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
24df6 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
24df7 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61  OP_Column, srcTa
24df8 62 2c 20 6a 2c 20 69 52 65 67 53 74 6f 72 65 29  b, j, iRegStore)
24df9 3b 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  ; .      }else i
24dfa 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  f( pSelect ){.  
24dfb 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24dfc 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
24dfd 6f 70 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65  opy, regFromSele
24dfe 63 74 2b 6a 2c 20 69 52 65 67 53 74 6f 72 65 29  ct+j, iRegStore)
24dff 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
24e00 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
24e01 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
24e02 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72  List->a[j].pExpr
24e03 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20  , iRegStore);.  
24e04 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
24e05 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
24e06 65 20 74 6f 20 63 68 65 63 6b 20 63 6f 6e 73 74  e to check const
24e07 72 61 69 6e 74 73 20 61 6e 64 20 67 65 6e 65 72  raints and gener
24e08 61 74 65 20 69 6e 64 65 78 20 6b 65 79 73 20 61  ate index keys a
24e09 6e 64 0a 20 20 20 20 2a 2a 20 64 6f 20 74 68 65  nd.    ** do the
24e0a 20 69 6e 73 65 72 74 69 6f 6e 2e 0a 20 20 20 20   insertion..    
24e0b 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
24e0c 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
24e0d 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56 69  BLE.    if( IsVi
24e0e 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
24e0f 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
24e10 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50 61  MakeWritable(pPa
24e11 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  rse, pTab);.    
24e12 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24e13 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 61 74  Op4(v, OP_VUpdat
24e14 65 2c 20 31 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  e, 1, pTab->nCol
24e15 2b 32 2c 20 72 65 67 49 6e 73 2c 0a 20 20 20 20  +2, regIns,.    
24e16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24e17 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54   (const char*)pT
24e18 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f 56 54  ab->pVtab, P4_VT
24e19 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23  AB);.    }else.#
24e1a 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20  endif.    {.    
24e1b 20 20 69 6e 74 20 69 73 52 65 70 6c 61 63 65 3b    int isReplace;
24e1c 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72      /* Set to tr
24e1d 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74  ue if constraint
24e1e 73 20 6d 61 79 20 63 61 75 73 65 20 61 20 72 65  s may cause a re
24e1f 70 6c 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 73  place */.      s
24e20 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f  qlite3GenerateCo
24e21 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 70  nstraintChecks(p
24e22 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73  Parse, pTab, bas
24e23 65 43 75 72 2c 20 72 65 67 49 6e 73 2c 20 61 52  eCur, regIns, aR
24e24 65 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20  egIdx,.         
24e25 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 2c 20 30   keyColumn>=0, 0
24e26 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66  , onError, endOf
24e27 4c 6f 6f 70 2c 20 26 69 73 52 65 70 6c 61 63 65  Loop, &isReplace
24e28 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
24e29 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49  sqlite3CompleteI
24e2a 6e 73 65 72 74 69 6f 6e 28 0a 20 20 20 20 20 20  nsertion(.      
24e2b 20 20 20 20 70 50 61 72 73 65 2c 20 70 54 61 62      pParse, pTab
24e2c 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 49 6e  , baseCur, regIn
24e2d 73 2c 20 61 52 65 67 49 64 78 2c 20 30 2c 0a 20  s, aRegIdx, 0,. 
24e2e 20 20 20 20 20 20 20 20 20 28 74 6d 61 73 6b 26           (tmask&
24e2f 54 52 49 47 47 45 52 5f 41 46 54 45 52 29 20 3f  TRIGGER_AFTER) ?
24e30 20 6e 65 77 49 64 78 20 3a 20 2d 31 2c 20 61 70   newIdx : -1, ap
24e31 70 65 6e 64 46 6c 61 67 2c 20 69 73 52 65 70 6c  pendFlag, isRepl
24e32 61 63 65 3d 3d 30 0a 20 20 20 20 20 20 29 3b 0a  ace==0.      );.
24e33 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
24e34 55 70 64 61 74 65 20 74 68 65 20 63 6f 75 6e 74  Update the count
24e35 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 61 72   of rows that ar
24e36 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f 0a  e inserted.  */.
24e37 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
24e38 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52   & SQLITE_CountR
24e39 6f 77 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  ows)!=0 ){.    s
24e3a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24e3b 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72  (v, OP_AddImm, r
24e3c 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a  egRowCount, 1);.
24e3d 20 20 7d 0a 0a 20 20 69 66 28 20 70 54 72 69 67    }..  if( pTrig
24e3e 67 65 72 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f  ger ){.    /* Co
24e3f 64 65 20 41 46 54 45 52 20 74 72 69 67 67 65 72  de AFTER trigger
24e40 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c  s */.    if( sql
24e41 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
24e42 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67  er(pParse, pTrig
24e43 67 65 72 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20  ger, TK_INSERT, 
24e44 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52  0, TRIGGER_AFTER
24e45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 70 54 61  , .          pTa
24e46 62 2c 20 6e 65 77 49 64 78 2c 20 2d 31 2c 20 6f  b, newIdx, -1, o
24e47 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f  nError, endOfLoo
24e48 70 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20  p, 0, 0) ){.    
24e49 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
24e4a 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d  eanup;.    }.  }
24e4b 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f  ..  /* The botto
24e4c 6d 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 69 6e  m of the main in
24e4d 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 2c 20 69 66  sertion loop, if
24e4e 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65   the data source
24e4f 0a 20 20 2a 2a 20 69 73 20 61 20 53 45 4c 45 43  .  ** is a SELEC
24e50 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a  T statement..  *
24e51 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  /.  sqlite3VdbeR
24e52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65  esolveLabel(v, e
24e53 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 69 66 28  ndOfLoop);.  if(
24e54 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b   useTempTable ){
24e55 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
24e56 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
24e57 74 2c 20 73 72 63 54 61 62 2c 20 61 64 64 72 43  t, srcTab, addrC
24e58 6f 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ont);.    sqlite
24e59 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
24e5a 20 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a 20 20   addrInsTop);.  
24e5b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24e5c 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
24e5d 20 73 72 63 54 61 62 29 3b 0a 20 20 7d 65 6c 73   srcTab);.  }els
24e5e 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b  e if( pSelect ){
24e5f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
24e60 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
24e61 6f 2c 20 30 2c 20 61 64 64 72 43 6f 6e 74 29 3b  o, 0, addrCont);
24e62 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
24e63 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
24e64 49 6e 73 54 6f 70 29 3b 0a 20 20 7d 0a 0a 20 20  InsTop);.  }..  
24e65 69 66 28 20 21 49 73 56 69 72 74 75 61 6c 28 70  if( !IsVirtual(p
24e66 54 61 62 29 20 26 26 20 21 69 73 56 69 65 77 20  Tab) && !isView 
24e67 29 7b 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20  ){.    /* Close 
24e68 61 6c 6c 20 74 61 62 6c 65 73 20 6f 70 65 6e 65  all tables opene
24e69 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  d */.    sqlite3
24e6a 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
24e6b 5f 43 6c 6f 73 65 2c 20 62 61 73 65 43 75 72 29  _Close, baseCur)
24e6c 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 31 2c  ;.    for(idx=1,
24e6d 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
24e6e 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
24e6f 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 64 78 2b  Idx->pNext, idx+
24e70 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
24e71 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
24e72 50 5f 43 6c 6f 73 65 2c 20 69 64 78 2b 62 61 73  P_Close, idx+bas
24e73 65 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  eCur);.    }.  }
24e74 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68  ..  /* Update th
24e75 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
24e76 65 20 74 61 62 6c 65 20 62 79 20 73 74 6f 72 69  e table by stori
24e77 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ng the content o
24e78 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 75 6e 74  f the.  ** count
24e79 65 72 20 76 61 6c 75 65 20 69 6e 20 6d 65 6d 6f  er value in memo
24e7a 72 79 20 72 65 67 41 75 74 6f 69 6e 63 20 62 61  ry regAutoinc ba
24e7b 63 6b 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  ck into the sqli
24e7c 74 65 5f 73 65 71 75 65 6e 63 65 0a 20 20 2a 2a  te_sequence.  **
24e7d 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61   table..  */.  a
24e7e 75 74 6f 49 6e 63 45 6e 64 28 70 50 61 72 73 65  utoIncEnd(pParse
24e7f 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 72 65 67  , iDb, pTab, reg
24e80 41 75 74 6f 69 6e 63 29 3b 0a 0a 20 20 2f 2a 0a  Autoinc);..  /*.
24e81 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20    ** Return the 
24e82 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  number of rows i
24e83 6e 73 65 72 74 65 64 2e 20 49 66 20 74 68 69 73  nserted. If this
24e84 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20 20 2a   routine is .  *
24e85 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64  * generating cod
24e86 65 20 62 65 63 61 75 73 65 20 6f 66 20 61 20 63  e because of a c
24e87 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65  all to sqlite3Ne
24e88 73 74 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20  stedParse(), do 
24e89 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20  not.  ** invoke 
24e8a 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  the callback fun
24e8b 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  ction..  */.  if
24e8c 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
24e8d 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26  LITE_CountRows &
24e8e 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  & pParse->nested
24e8f 3d 3d 30 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ==0 && !pParse->
24e90 74 72 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20  trigStack ){.   
24e91 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24e92 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
24e93 6f 77 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c  ow, regRowCount,
24e94 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
24e95 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
24e96 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
24e97 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
24e98 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
24e99 4d 45 2c 20 22 72 6f 77 73 20 69 6e 73 65 72 74  ME, "rows insert
24e9a 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ed", SQLITE_STAT
24e9b 49 43 29 3b 0a 20 20 7d 0a 0a 69 6e 73 65 72 74  IC);.  }..insert
24e9c 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69  _cleanup:.  sqli
24e9d 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
24e9e 28 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a  (db, pTabList);.
24e9f 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
24ea0 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73  tDelete(db, pLis
24ea1 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  t);.  sqlite3Sel
24ea2 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53  ectDelete(db, pS
24ea3 65 6c 65 63 74 29 3b 0a 20 20 73 71 6c 69 74 65  elect);.  sqlite
24ea4 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
24ea5 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71  , pColumn);.  sq
24ea6 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24ea7 61 52 65 67 49 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a  aRegIdx);.}../*.
24ea8 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
24ea9 20 74 6f 20 64 6f 20 63 6f 6e 73 74 72 61 69 6e   to do constrain
24eaa 74 20 63 68 65 63 6b 73 20 70 72 69 6f 72 20 74  t checks prior t
24eab 6f 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20 61  o an INSERT or a
24eac 6e 20 55 50 44 41 54 45 2e 0a 2a 2a 0a 2a 2a 20  n UPDATE..**.** 
24ead 54 68 65 20 69 6e 70 75 74 20 69 73 20 61 20 72  The input is a r
24eae 61 6e 67 65 20 6f 66 20 63 6f 6e 73 65 63 75 74  ange of consecut
24eaf 69 76 65 20 72 65 67 69 73 74 65 72 73 20 61 73  ive registers as
24eb0 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
24eb1 20 20 20 31 2e 20 20 54 68 65 20 72 6f 77 69 64     1.  The rowid
24eb2 20 6f 66 20 74 68 65 20 72 6f 77 20 74 6f 20 62   of the row to b
24eb3 65 20 75 70 64 61 74 65 64 20 62 65 66 6f 72 65  e updated before
24eb4 20 74 68 65 20 75 70 64 61 74 65 2e 20 20 54 68   the update.  Th
24eb5 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 76 61 6c  is.**        val
24eb6 75 65 20 69 73 20 6f 6d 69 74 74 65 64 20 75 6e  ue is omitted un
24eb7 6c 65 73 73 20 77 65 20 61 72 65 20 64 6f 69 6e  less we are doin
24eb8 67 20 61 6e 20 55 50 44 41 54 45 20 74 68 61 74  g an UPDATE that
24eb9 20 69 6e 76 6f 6c 76 65 73 20 61 0a 2a 2a 20 20   involves a.**  
24eba 20 20 20 20 20 20 63 68 61 6e 67 65 20 74 6f 20        change to 
24ebb 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
24ebc 72 20 6f 72 20 77 72 69 74 69 6e 67 20 74 6f 20  r or writing to 
24ebd 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
24ebe 0a 2a 2a 0a 2a 2a 20 20 20 20 32 2e 20 20 54 68  .**.**    2.  Th
24ebf 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72  e rowid of the r
24ec0 6f 77 20 61 66 74 65 72 20 74 68 65 20 75 70 64  ow after the upd
24ec1 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 33 2e  ate..**.**    3.
24ec2 20 20 54 68 65 20 64 61 74 61 20 69 6e 20 74 68    The data in th
24ec3 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f  e first column o
24ec4 66 20 74 68 65 20 65 6e 74 72 79 20 61 66 74 65  f the entry afte
24ec5 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a 2a 2a  r the update..**
24ec6 0a 2a 2a 20 20 20 20 69 2e 20 20 44 61 74 61 20  .**    i.  Data 
24ec7 66 72 6f 6d 20 6d 69 64 64 6c 65 20 63 6f 6c 75  from middle colu
24ec8 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  mns....**.**    
24ec9 4e 2e 20 20 54 68 65 20 64 61 74 61 20 69 6e 20  N.  The data in 
24eca 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20  the last column 
24ecb 6f 66 20 74 68 65 20 65 6e 74 72 79 20 61 66 74  of the entry aft
24ecc 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a 2a  er the update..*
24ecd 2a 0a 2a 2a 20 54 68 65 20 72 65 67 52 6f 77 69  *.** The regRowi
24ece 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  d parameter is t
24ecf 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
24ed0 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e  register contain
24ed1 69 6e 67 20 28 32 29 2e 0a 2a 2a 0a 2a 2a 20 54  ing (2)..**.** T
24ed2 68 65 20 6f 6c 64 20 72 6f 77 69 64 20 73 68 6f  he old rowid sho
24ed3 77 6e 20 61 73 20 65 6e 74 72 79 20 28 31 29 20  wn as entry (1) 
24ed4 61 62 6f 76 65 20 69 73 20 6f 6d 69 74 74 65 64  above is omitted
24ed5 20 75 6e 6c 65 73 73 20 62 6f 74 68 20 69 73 55   unless both isU
24ed6 70 64 61 74 65 0a 2a 2a 20 61 6e 64 20 72 6f 77  pdate.** and row
24ed7 69 64 43 68 6e 67 20 61 72 65 20 31 2e 20 20 69  idChng are 1.  i
24ed8 73 55 70 64 61 74 65 20 69 73 20 74 72 75 65 20  sUpdate is true 
24ed9 66 6f 72 20 55 50 44 41 54 45 73 20 61 6e 64 20  for UPDATEs and 
24eda 66 61 6c 73 65 20 66 6f 72 0a 2a 2a 20 49 4e 53  false for.** INS
24edb 45 52 54 73 2e 20 20 52 6f 77 69 64 43 68 6e 67  ERTs.  RowidChng
24edc 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
24edd 6e 65 77 20 72 6f 77 69 64 20 69 73 20 65 78 70  new rowid is exp
24ede 6c 69 63 69 74 6c 79 20 73 70 65 63 69 66 69 65  licitly specifie
24edf 64 20 62 79 0a 2a 2a 20 74 68 65 20 75 70 64 61  d by.** the upda
24ee0 74 65 20 6f 72 20 69 6e 73 65 72 74 20 73 74 61  te or insert sta
24ee1 74 65 6d 65 6e 74 2e 20 20 49 66 20 72 6f 77 69  tement.  If rowi
24ee2 64 43 68 6e 67 20 69 73 20 66 61 6c 73 65 2c 20  dChng is false, 
24ee3 69 74 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a  it means that.**
24ee4 20 74 68 65 20 72 6f 77 69 64 20 69 73 20 63 6f   the rowid is co
24ee5 6d 70 75 74 65 64 20 61 75 74 6f 6d 61 74 69 63  mputed automatic
24ee6 61 6c 6c 79 20 69 6e 20 61 6e 20 69 6e 73 65 72  ally in an inser
24ee7 74 20 6f 72 20 74 68 61 74 20 74 68 65 20 72 6f  t or that the ro
24ee8 77 69 64 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  wid value.** is 
24ee9 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 62 79 20  not modified by 
24eea 74 68 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a  the update..**.*
24eeb 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72  * The code gener
24eec 61 74 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  ated by this rou
24eed 74 69 6e 65 20 73 74 6f 72 65 20 6e 65 77 20 69  tine store new i
24eee 6e 64 65 78 20 65 6e 74 72 69 65 73 20 69 6e 74  ndex entries int
24eef 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 69  o.** registers i
24ef0 64 65 6e 74 69 66 69 65 64 20 62 79 20 61 52 65  dentified by aRe
24ef1 67 49 64 78 5b 5d 2e 20 20 4e 6f 20 69 6e 64 65  gIdx[].  No inde
24ef2 78 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74  x entry is creat
24ef3 65 64 20 66 6f 72 0a 2a 2a 20 69 6e 64 69 63 65  ed for.** indice
24ef4 73 20 77 68 65 72 65 20 61 52 65 67 49 64 78 5b  s where aRegIdx[
24ef5 69 5d 3d 3d 30 2e 20 20 54 68 65 20 6f 72 64 65  i]==0.  The orde
24ef6 72 20 6f 66 20 69 6e 64 69 63 65 73 20 69 6e 20  r of indices in 
24ef7 61 52 65 67 49 64 78 5b 5d 20 69 73 0a 2a 2a 20  aRegIdx[] is.** 
24ef8 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
24ef9 6f 72 64 65 72 20 6f 66 20 69 6e 64 69 63 65 73  order of indices
24efa 20 6f 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c   on the linked l
24efb 69 73 74 20 6f 66 20 69 6e 64 69 63 65 73 0a 2a  ist of indices.*
24efc 2a 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  * attached to th
24efd 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  e table..**.** T
24efe 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f  his routine also
24eff 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
24f00 74 6f 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61  to check constra
24f01 69 6e 74 73 2e 20 20 4e 4f 54 20 4e 55 4c 4c 2c  ints.  NOT NULL,
24f02 0a 2a 2a 20 43 48 45 43 4b 2c 20 61 6e 64 20 55  .** CHECK, and U
24f03 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74  NIQUE constraint
24f04 73 20 61 72 65 20 61 6c 6c 20 63 68 65 63 6b 65  s are all checke
24f05 64 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61  d.  If a constra
24f06 69 6e 74 20 66 61 69 6c 73 2c 0a 2a 2a 20 74 68  int fails,.** th
24f07 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  en the appropria
24f08 74 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72  te action is per
24f09 66 6f 72 6d 65 64 2e 20 20 54 68 65 72 65 20 61  formed.  There a
24f0a 72 65 20 66 69 76 65 20 70 6f 73 73 69 62 6c 65  re five possible
24f0b 0a 2a 2a 20 61 63 74 69 6f 6e 73 3a 20 52 4f 4c  .** actions: ROL
24f0c 4c 42 41 43 4b 2c 20 41 42 4f 52 54 2c 20 46 41  LBACK, ABORT, FA
24f0d 49 4c 2c 20 52 45 50 4c 41 43 45 2c 20 61 6e 64  IL, REPLACE, and
24f0e 20 49 47 4e 4f 52 45 2e 0a 2a 2a 0a 2a 2a 20 20   IGNORE..**.**  
24f0f 43 6f 6e 73 74 72 61 69 6e 74 20 74 79 70 65 20  Constraint type 
24f10 20 41 63 74 69 6f 6e 20 20 20 20 20 20 20 57 68   Action       Wh
24f11 61 74 20 48 61 70 70 65 6e 73 0a 2a 2a 20 20 2d  at Happens.**  -
24f12 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
24f13 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 2d 2d 2d  ----------   ---
24f14 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24f15 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24f16 2d 2d 2d 2d 2d 0a 2a 2a 20 20 61 6e 79 20 20 20  -----.**  any   
24f17 20 20 20 20 20 20 20 20 20 20 20 52 4f 4c 4c 42             ROLLB
24f18 41 43 4b 20 20 20 20 20 54 68 65 20 63 75 72 72  ACK     The curr
24f19 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
24f1a 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  is rolled back a
24f1b 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  nd.**           
24f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f1d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 65 78 65       sqlite3_exe
24f1e 63 28 29 20 72 65 74 75 72 6e 73 20 69 6d 6d 65  c() returns imme
24f1f 64 69 61 74 65 6c 79 20 77 69 74 68 20 61 0a 2a  diately with a.*
24f20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
24f21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f22 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20   return code of 
24f23 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
24f24 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20  T..**.**  any   
24f25 20 20 20 20 20 20 20 20 20 20 20 41 42 4f 52 54             ABORT
24f26 20 20 20 20 20 20 20 20 42 61 63 6b 20 6f 75 74          Back out
24f27 20 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 74 68   changes from th
24f28 65 20 63 75 72 72 65 6e 74 20 63 6f 6d 6d 61 6e  e current comman
24f29 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
24f2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f2b 20 20 20 20 6f 6e 6c 79 20 28 64 6f 20 6e 6f 74      only (do not
24f2c 20 64 6f 20 61 20 63 6f 6d 70 6c 65 74 65 20 72   do a complete r
24f2d 6f 6c 6c 62 61 63 6b 29 20 74 68 65 6e 0a 2a 2a  ollback) then.**
24f2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f30 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 65 78  cause sqlite3_ex
24f31 65 63 28 29 20 74 6f 20 72 65 74 75 72 6e 20 69  ec() to return i
24f32 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 20 20  mmediately.**   
24f33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f34 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69 74               wit
24f35 68 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  h SQLITE_CONSTRA
24f36 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20  INT..**.**  any 
24f37 20 20 20 20 20 20 20 20 20 20 20 20 20 46 41 49               FAI
24f38 4c 20 20 20 20 20 20 20 20 20 53 71 6c 69 74 65  L         Sqlite
24f39 5f 65 78 65 63 28 29 20 72 65 74 75 72 6e 73 20  _exec() returns 
24f3a 69 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74 68  immediately with
24f3b 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   a.**           
24f3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f3d 20 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64 65       return code
24f3e 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54   of SQLITE_CONST
24f3f 52 41 49 4e 54 2e 20 20 54 68 65 0a 2a 2a 20 20  RAINT.  The.**  
24f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 72                tr
24f42 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74  ansaction is not
24f43 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64   rolled back and
24f44 20 61 6e 79 0a 2a 2a 20 20 20 20 20 20 20 20 20   any.**         
24f45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f46 20 20 20 20 20 20 20 70 72 69 6f 72 20 63 68 61         prior cha
24f47 6e 67 65 73 20 61 72 65 20 72 65 74 61 69 6e 65  nges are retaine
24f48 64 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20  d..**.**  any   
24f49 20 20 20 20 20 20 20 20 20 20 20 49 47 4e 4f 52             IGNOR
24f4a 45 20 20 20 20 20 20 20 54 68 65 20 72 65 63 6f  E       The reco
24f4b 72 64 20 6e 75 6d 62 65 72 20 61 6e 64 20 64 61  rd number and da
24f4c 74 61 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f  ta is popped fro
24f4d 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  m.**            
24f4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f4f 20 20 20 20 74 68 65 20 73 74 61 63 6b 20 61 6e      the stack an
24f50 64 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6d  d there is an im
24f51 6d 65 64 69 61 74 65 20 6a 75 6d 70 0a 2a 2a 20  mediate jump.** 
24f52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
24f54 6f 20 6c 61 62 65 6c 20 69 67 6e 6f 72 65 44 65  o label ignoreDe
24f55 73 74 2e 0a 2a 2a 0a 2a 2a 20 20 4e 4f 54 20 4e  st..**.**  NOT N
24f56 55 4c 4c 20 20 20 20 20 20 20 20 20 52 45 50 4c  ULL         REPL
24f57 41 43 45 20 20 20 20 20 20 54 68 65 20 4e 55 4c  ACE      The NUL
24f58 4c 20 76 61 6c 75 65 20 69 73 20 72 65 70 6c 61  L value is repla
24f59 63 65 20 62 79 20 74 68 65 20 64 65 66 61 75 6c  ce by the defaul
24f5a 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
24f5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f5c 20 20 20 20 76 61 6c 75 65 20 66 6f 72 20 74 68      value for th
24f5d 61 74 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74  at column.  If t
24f5e 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
24f5f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
24f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f61 20 20 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20     is NULL, the 
24f62 61 63 74 69 6f 6e 20 69 73 20 74 68 65 20 73 61  action is the sa
24f63 6d 65 20 61 73 20 41 42 4f 52 54 2e 0a 2a 2a 0a  me as ABORT..**.
24f64 2a 2a 20 20 55 4e 49 51 55 45 20 20 20 20 20 20  **  UNIQUE      
24f65 20 20 20 20 20 52 45 50 4c 41 43 45 20 20 20 20       REPLACE    
24f66 20 20 54 68 65 20 6f 74 68 65 72 20 72 6f 77 20    The other row 
24f67 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77  that conflicts w
24f68 69 74 68 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20  ith the row.**  
24f69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
24f6b 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 73 20  ing inserted is 
24f6c 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 20  removed..**.**  
24f6d 43 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20  CHECK           
24f6e 20 52 45 50 4c 41 43 45 20 20 20 20 20 20 49 6c   REPLACE      Il
24f6f 6c 65 67 61 6c 2e 20 20 54 68 65 20 72 65 73 75  legal.  The resu
24f70 6c 74 73 20 69 6e 20 61 6e 20 65 78 63 65 70 74  lts in an except
24f71 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 69 63 68  ion..**.** Which
24f72 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20   action to take 
24f73 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
24f74 20 74 68 65 20 6f 76 65 72 72 69 64 65 45 72 72   the overrideErr
24f75 6f 72 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  or parameter..**
24f76 20 4f 72 20 69 66 20 6f 76 65 72 72 69 64 65 45   Or if overrideE
24f77 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
24f78 2c 20 74 68 65 6e 20 74 68 65 20 70 50 61 72 73  , then the pPars
24f79 65 2d 3e 6f 6e 45 72 72 6f 72 20 70 61 72 61 6d  e->onError param
24f7a 65 74 65 72 0a 2a 2a 20 69 73 20 75 73 65 64 2e  eter.** is used.
24f7b 20 20 4f 72 20 69 66 20 70 50 61 72 73 65 2d 3e    Or if pParse->
24f7c 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
24f7d 75 6c 74 20 74 68 65 6e 20 74 68 65 20 6f 6e 45  ult then the onE
24f7e 72 72 6f 72 20 76 61 6c 75 65 0a 2a 2a 20 66 6f  rror value.** fo
24f7f 72 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  r the constraint
24f80 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   is used..**.** 
24f81 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  The calling rout
24f82 69 6e 65 20 6d 75 73 74 20 6f 70 65 6e 20 61 20  ine must open a 
24f83 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
24f84 72 20 66 6f 72 20 70 54 61 62 20 77 69 74 68 0a  r for pTab with.
24f85 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  ** cursor number
24f86 20 22 62 61 73 65 43 75 72 22 2e 20 20 41 6c 6c   "baseCur".  All
24f87 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62   indices of pTab
24f88 20 6d 75 73 74 20 61 6c 73 6f 20 68 61 76 65 20   must also have 
24f89 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 2f 77 72 69  open.** read/wri
24f8a 74 65 20 63 75 72 73 6f 72 73 20 77 69 74 68 20  te cursors with 
24f8b 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 62 61  cursor number ba
24f8c 73 65 43 75 72 2b 69 20 66 6f 72 20 74 68 65 20  seCur+i for the 
24f8d 69 2d 74 68 20 63 75 72 73 6f 72 2e 0a 2a 2a 20  i-th cursor..** 
24f8e 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 72 65  Except, if there
24f8f 20 69 73 20 6e 6f 20 70 6f 73 73 69 62 69 6c 69   is no possibili
24f90 74 79 20 6f 66 20 61 20 52 45 50 4c 41 43 45 20  ty of a REPLACE 
24f91 61 63 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 63  action then.** c
24f92 75 72 73 6f 72 73 20 64 6f 20 6e 6f 74 20 6e 65  ursors do not ne
24f93 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 20 66 6f  ed to be open fo
24f94 72 20 69 6e 64 69 63 65 73 20 77 68 65 72 65 20  r indices where 
24f95 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30 2e 0a 2a  aRegIdx[i]==0..*
24f96 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24f97 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e   void sqlite3Gen
24f98 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
24f99 68 65 63 6b 73 28 0a 20 20 50 61 72 73 65 20 2a  hecks(.  Parse *
24f9a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
24f9b 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
24f9c 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  xt */.  Table *p
24f9d 54 61 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 74  Tab,        /* t
24f9e 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  he table into wh
24f9f 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65 72  ich we are inser
24fa0 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61  ting */.  int ba
24fa1 73 65 43 75 72 2c 20 20 20 20 20 20 20 20 2f 2a  seCur,        /*
24fa2 20 49 6e 64 65 78 20 6f 66 20 61 20 72 65 61 64   Index of a read
24fa3 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f  /write cursor po
24fa4 69 6e 74 69 6e 67 20 61 74 20 70 54 61 62 20 2a  inting at pTab *
24fa5 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  /.  int regRowid
24fa6 2c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  ,       /* Index
24fa7 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66   of the range of
24fa8 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73   input registers
24fa9 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49   */.  int *aRegI
24faa 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 52 65 67  dx,       /* Reg
24fab 69 73 74 65 72 20 75 73 65 64 20 62 79 20 65 61  ister used by ea
24fac 63 68 20 69 6e 64 65 78 2e 20 20 30 20 66 6f 72  ch index.  0 for
24fad 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20   unused indices 
24fae 2a 2f 0a 20 20 69 6e 74 20 72 6f 77 69 64 43 68  */.  int rowidCh
24faf 6e 67 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ng,      /* True
24fb0 20 69 66 20 74 68 65 20 72 6f 77 69 64 20 6d 69   if the rowid mi
24fb1 67 68 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68  ght collide with
24fb2 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 20   existing entry 
24fb3 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70 64 61 74  */.  int isUpdat
24fb4 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  e,       /* True
24fb5 20 66 6f 72 20 55 50 44 41 54 45 2c 20 46 61 6c   for UPDATE, Fal
24fb6 73 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f  se for INSERT */
24fb7 0a 20 20 69 6e 74 20 6f 76 65 72 72 69 64 65 45  .  int overrideE
24fb8 72 72 6f 72 2c 20 20 2f 2a 20 4f 76 65 72 72 69  rror,  /* Overri
24fb9 64 65 20 6f 6e 45 72 72 6f 72 20 74 6f 20 74 68  de onError to th
24fba 69 73 20 69 66 20 6e 6f 74 20 4f 45 5f 44 65 66  is if not OE_Def
24fbb 61 75 6c 74 20 2a 2f 0a 20 20 69 6e 74 20 69 67  ault */.  int ig
24fbc 6e 6f 72 65 44 65 73 74 2c 20 20 20 20 20 2f 2a  noreDest,     /*
24fbd 20 4a 75 6d 70 20 74 6f 20 74 68 69 73 20 6c 61   Jump to this la
24fbe 62 65 6c 20 6f 6e 20 61 6e 20 4f 45 5f 49 67 6e  bel on an OE_Ign
24fbf 6f 72 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 2a  ore resolution *
24fc0 2f 0a 20 20 69 6e 74 20 2a 70 62 4d 61 79 52 65  /.  int *pbMayRe
24fc1 70 6c 61 63 65 20 20 20 2f 2a 20 4f 55 54 3a 20  place   /* OUT: 
24fc2 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 63  Set to true if c
24fc3 6f 6e 73 74 72 61 69 6e 74 20 6d 61 79 20 63 61  onstraint may ca
24fc4 75 73 65 20 61 20 72 65 70 6c 61 63 65 20 2a 2f  use a replace */
24fc5 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
24fc6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c 6f 6f            /* loo
24fc7 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 56  p counter */.  V
24fc8 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
24fc9 20 20 20 2f 2a 20 56 44 42 45 20 75 6e 64 65 72     /* VDBE under
24fca 20 63 6f 6e 73 74 72 75 74 69 6f 6e 20 2a 2f 0a   constrution */.
24fcb 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20    int nCol;     
24fcc 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
24fcd 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
24fce 69 6e 74 20 6f 6e 45 72 72 6f 72 3b 20 20 20 20  int onError;    
24fcf 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20      /* Conflict 
24fd0 72 65 73 6f 6c 75 74 69 6f 6e 20 73 74 72 61 74  resolution strat
24fd1 65 67 79 20 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b  egy */.  int j1;
24fd2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24fd3 41 64 64 72 65 73 73 73 20 6f 66 20 6a 75 6d 70  Addresss of jump
24fd4 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a   instruction */.
24fd5 20 20 69 6e 74 20 6a 32 20 3d 20 30 2c 20 6a 33    int j2 = 0, j3
24fd6 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73  ;     /* Address
24fd7 65 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72  es of jump instr
24fd8 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  uctions */.  int
24fd9 20 72 65 67 44 61 74 61 3b 20 20 20 20 20 20 20   regData;       
24fda 20 2f 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e   /* Register con
24fdb 74 61 69 6e 69 6e 67 20 66 69 72 73 74 20 64 61  taining first da
24fdc 74 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  ta column */.  i
24fdd 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20  nt iCur;        
24fde 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73     /* Table curs
24fdf 6f 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 49  or number */.  I
24fe0 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
24fe1 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
24fe2 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 64  o one of the ind
24fe3 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 73 65  ices */.  int se
24fe4 65 6e 52 65 70 6c 61 63 65 20 3d 20 30 3b 20 2f  enReplace = 0; /
24fe5 2a 20 54 72 75 65 20 69 66 20 52 45 50 4c 41 43  * True if REPLAC
24fe6 45 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 73  E is used to res
24fe7 6f 6c 76 65 20 49 4e 54 20 50 4b 20 63 6f 6e 66  olve INT PK conf
24fe8 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 61  lict */.  int ha
24fe9 73 54 77 6f 52 6f 77 69 64 73 20 3d 20 28 69 73  sTwoRowids = (is
24fea 55 70 64 61 74 65 20 26 26 20 72 6f 77 69 64 43  Update && rowidC
24feb 68 6e 67 29 3b 0a 0a 20 20 76 20 3d 20 73 71 6c  hng);..  v = sql
24fec 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
24fed 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  se);.  assert( v
24fee 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
24fef 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
24ff0 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61  0 );  /* This ta
24ff1 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45  ble is not a VIE
24ff2 57 20 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54  W */.  nCol = pT
24ff3 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44  ab->nCol;.  regD
24ff4 61 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b  ata = regRowid +
24ff5 20 31 3b 0a 0a 0a 20 20 2f 2a 20 54 65 73 74 20   1;...  /* Test 
24ff6 61 6c 6c 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e  all NOT NULL con
24ff7 73 74 72 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20  straints..  */. 
24ff8 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
24ff9 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
24ffa 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  i==pTab->iPKey )
24ffb 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  {.      continue
24ffc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6e 45 72  ;.    }.    onEr
24ffd 72 6f 72 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  ror = pTab->aCol
24ffe 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20  [i].notNull;.   
24fff 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
25000 5f 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65  _None ) continue
25001 3b 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69  ;.    if( overri
25002 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61  deError!=OE_Defa
25003 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45  ult ){.      onE
25004 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45  rror = overrideE
25005 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20  rror;.    }else 
25006 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
25007 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20  Default ){.     
25008 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
25009 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ort;.    }.    i
2500a 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  f( onError==OE_R
2500b 65 70 6c 61 63 65 20 26 26 20 70 54 61 62 2d 3e  eplace && pTab->
2500c 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30  aCol[i].pDflt==0
2500d 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f   ){.      onErro
2500e 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  r = OE_Abort;.  
2500f 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
25010 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c  onError==OE_Roll
25011 62 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d  back || onError=
25012 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45  =OE_Abort || onE
25013 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20  rror==OE_Fail.  
25014 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72        || onError
25015 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f  ==OE_Ignore || o
25016 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
25017 63 65 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68  ce );.    switch
25018 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20  ( onError ){.   
25019 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62     case OE_Rollb
2501a 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ack:.      case 
2501b 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20  OE_Abort:.      
2501c 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a  case OE_Fail: {.
2501d 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d          char *zM
2501e 73 67 3b 0a 20 20 20 20 20 20 20 20 6a 31 20 3d  sg;.        j1 =
2501f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25020 70 33 28 76 2c 20 4f 50 5f 48 61 6c 74 49 66 4e  p3(v, OP_HaltIfN
25021 75 6c 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ull,.           
25022 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25023 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 4f         SQLITE_CO
25024 4e 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f  NSTRAINT, onErro
25025 72 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20  r, regData+i);. 
25026 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
25027 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61  lite3MPrintf(pPa
25028 72 73 65 2d 3e 64 62 2c 20 22 25 73 2e 25 73 20  rse->db, "%s.%s 
25029 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 22  may not be NULL"
2502a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2502b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2502c 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61  pTab->zName, pTa
2502d 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65  b->aCol[i].zName
2502e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2502f 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
25030 2c 20 2d 31 2c 20 7a 4d 73 67 2c 20 50 34 5f 44  , -1, zMsg, P4_D
25031 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20  YNAMIC);.       
25032 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
25033 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67        case OE_Ig
25034 6e 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20 20 20  nore: {.        
25035 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25036 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  2(v, OP_IsNull, 
25037 72 65 67 44 61 74 61 2b 69 2c 20 69 67 6e 6f 72  regData+i, ignor
25038 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20  eDest);.        
25039 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2503a 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
2503b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2503c 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
2503d 61 63 65 20 29 3b 0a 20 20 20 20 20 20 20 20 6a  ace );.        j
2503e 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
2503f 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e  ddOp1(v, OP_NotN
25040 75 6c 6c 2c 20 72 65 67 44 61 74 61 2b 69 29 3b  ull, regData+i);
25041 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
25042 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
25043 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70   pTab->aCol[i].p
25044 44 66 6c 74 2c 20 72 65 67 44 61 74 61 2b 69 29  Dflt, regData+i)
25045 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
25046 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
25047 20 6a 31 29 3b 0a 20 20 20 20 20 20 20 20 62 72   j1);.        br
25048 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
25049 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73   }.  }..  /* Tes
2504a 74 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73  t all CHECK cons
2504b 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a 23 69 66  traints.  */.#if
2504c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2504d 5f 43 48 45 43 4b 0a 20 20 69 66 28 20 70 54 61  _CHECK.  if( pTa
2504e 62 2d 3e 70 43 68 65 63 6b 20 26 26 20 28 70 50  b->pCheck && (pP
2504f 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  arse->db->flags 
25050 26 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43  & SQLITE_IgnoreC
25051 68 65 63 6b 73 29 3d 3d 30 20 29 7b 0a 20 20 20  hecks)==0 ){.   
25052 20 69 6e 74 20 61 6c 6c 4f 6b 20 3d 20 73 71 6c   int allOk = sql
25053 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
25054 6c 28 76 29 3b 0a 20 20 20 20 70 50 61 72 73 65  l(v);.    pParse
25055 2d 3e 63 6b 42 61 73 65 20 3d 20 72 65 67 44 61  ->ckBase = regDa
25056 74 61 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  ta;.    sqlite3E
25057 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
25058 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20  , pTab->pCheck, 
25059 61 6c 6c 4f 6b 2c 20 53 51 4c 49 54 45 5f 4a 55  allOk, SQLITE_JU
2505a 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 6f  MPIFNULL);.    o
2505b 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64  nError = overrid
2505c 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75  eError!=OE_Defau
2505d 6c 74 20 3f 20 6f 76 65 72 72 69 64 65 45 72 72  lt ? overrideErr
2505e 6f 72 20 3a 20 4f 45 5f 41 62 6f 72 74 3b 0a 20  or : OE_Abort;. 
2505f 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d     if( onError==
25060 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20  OE_Ignore ){.   
25061 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25062 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
25063 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b   0, ignoreDest);
25064 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
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 48 61 6c 74 2c 20  Op2(v, OP_Halt, 
25067 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
25068 54 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a 20 20 20  T, onError);.   
25069 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
2506a 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2506b 2c 20 61 6c 6c 4f 6b 29 3b 0a 20 20 7d 0a 23 65  , allOk);.  }.#e
2506c 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
2506d 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45  (SQLITE_OMIT_CHE
2506e 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  CK) */..  /* If 
2506f 77 65 20 68 61 76 65 20 61 6e 20 49 4e 54 45 47  we have an INTEG
25070 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
25071 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 72  make sure the pr
25072 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f  imary key.  ** o
25073 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64  f the new record
25074 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69 6f   does not previo
25075 75 73 6c 79 20 65 78 69 73 74 2e 20 20 45 78 63  usly exist.  Exc
25076 65 70 74 2c 20 69 66 20 74 68 69 73 0a 20 20 2a  ept, if this.  *
25077 2a 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 61  * is an UPDATE a
25078 6e 64 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  nd the primary k
25079 65 79 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 69  ey is not changi
2507a 6e 67 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e 0a  ng, that is OK..
2507b 20 20 2a 2f 0a 20 20 69 66 28 20 72 6f 77 69 64    */.  if( rowid
2507c 43 68 6e 67 20 29 7b 0a 20 20 20 20 6f 6e 45 72  Chng ){.    onEr
2507d 72 6f 72 20 3d 20 70 54 61 62 2d 3e 6b 65 79 43  ror = pTab->keyC
2507e 6f 6e 66 3b 0a 20 20 20 20 69 66 28 20 6f 76 65  onf;.    if( ove
2507f 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44  rrideError!=OE_D
25080 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
25081 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69  onError = overri
25082 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c  deError;.    }el
25083 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d  se if( onError==
25084 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
25085 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45      onError = OE
25086 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
25087 20 20 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72    .    if( onErr
25088 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 7c  or!=OE_Replace |
25089 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 20 29  | pTab->pIndex )
2508a 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 55 70  {.      if( isUp
2508b 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  date ){.        
2508c 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j2 = sqlite3Vdbe
2508d 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 45 71 2c  AddOp3(v, OP_Eq,
2508e 20 72 65 67 52 6f 77 69 64 2c 20 30 2c 20 72 65   regRowid, 0, re
2508f 67 52 6f 77 69 64 2d 31 29 3b 0a 20 20 20 20 20  gRowid-1);.     
25090 20 7d 0a 20 20 20 20 20 20 6a 33 20 3d 20 73 71   }.      j3 = sq
25091 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
25092 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
25093 20 62 61 73 65 43 75 72 2c 20 30 2c 20 72 65 67   baseCur, 0, reg
25094 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 77  Rowid);.      sw
25095 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b  itch( onError ){
25096 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74  .        default
25097 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 6e  : {.          on
25098 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74  Error = OE_Abort
25099 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;.          /* F
2509a 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68  all thru into th
2509b 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20  e next case */. 
2509c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2509d 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63   case OE_Rollbac
2509e 6b 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k:.        case 
2509f 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20  OE_Abort:.      
250a0 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20    case OE_Fail: 
250a1 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
250a2 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
250a3 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45   OP_Halt, SQLITE
250a4 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e 45  _CONSTRAINT, onE
250a5 72 72 6f 72 2c 20 30 2c 0a 20 20 20 20 20 20 20  rror, 0,.       
250a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
250a7 20 20 20 20 22 50 52 49 4d 41 52 59 20 4b 45 59      "PRIMARY KEY
250a8 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 22   must be unique"
250a9 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
250aa 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
250ab 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
250ac 20 63 61 73 65 20 4f 45 5f 52 65 70 6c 61 63 65   case OE_Replace
250ad 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  : {.          sq
250ae 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
250af 49 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72  IndexDelete(pPar
250b0 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75  se, pTab, baseCu
250b1 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  r, 0);.         
250b2 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31   seenReplace = 1
250b3 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
250b4 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
250b5 20 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e       case OE_Ign
250b6 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  ore: {.         
250b7 20 61 73 73 65 72 74 28 20 73 65 65 6e 52 65 70   assert( seenRep
250b8 6c 61 63 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  lace==0 );.     
250b9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
250ba 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
250bb 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74  o, 0, ignoreDest
250bc 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
250bd 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
250be 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
250bf 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
250c0 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20 69 66  v, j3);.      if
250c1 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20  ( isUpdate ){.  
250c2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
250c3 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29  eJumpHere(v, j2)
250c4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
250c5 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61    }..  /* Test a
250c6 6c 6c 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  ll UNIQUE constr
250c7 61 69 6e 74 73 20 62 79 20 63 72 65 61 74 69 6e  aints by creatin
250c8 67 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61  g entries for ea
250c9 63 68 20 55 4e 49 51 55 45 0a 20 20 2a 2a 20 69  ch UNIQUE.  ** i
250ca 6e 64 65 78 20 61 6e 64 20 6d 61 6b 69 6e 67 20  ndex and making 
250cb 73 75 72 65 20 74 68 61 74 20 64 75 70 6c 69 63  sure that duplic
250cc 61 74 65 20 65 6e 74 72 69 65 73 20 64 6f 20 6e  ate entries do n
250cd 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
250ce 2e 0a 20 20 2a 2a 20 41 64 64 20 74 68 65 20 6e  ..  ** Add the n
250cf 65 77 20 72 65 63 6f 72 64 73 20 74 6f 20 74 68  ew records to th
250d0 65 20 69 6e 64 69 63 65 73 20 61 73 20 77 65 20  e indices as we 
250d1 67 6f 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  go..  */.  for(i
250d2 43 75 72 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  Cur=0, pIdx=pTab
250d3 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
250d4 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
250d5 2c 20 69 43 75 72 2b 2b 29 7b 0a 20 20 20 20 69  , iCur++){.    i
250d6 6e 74 20 72 65 67 49 64 78 3b 0a 20 20 20 20 69  nt regIdx;.    i
250d7 6e 74 20 72 65 67 52 3b 0a 0a 20 20 20 20 69 66  nt regR;..    if
250d8 28 20 61 52 65 67 49 64 78 5b 69 43 75 72 5d 3d  ( aRegIdx[iCur]=
250d9 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20  =0 ) continue;  
250da 2f 2a 20 53 6b 69 70 20 75 6e 75 73 65 64 20 69  /* Skip unused i
250db 6e 64 69 63 65 73 20 2a 2f 0a 0a 20 20 20 20 2f  ndices */..    /
250dc 2a 20 43 72 65 61 74 65 20 61 20 6b 65 79 20 66  * Create a key f
250dd 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  or accessing the
250de 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f 0a   index entry */.
250df 20 20 20 20 72 65 67 49 64 78 20 3d 20 73 71 6c      regIdx = sql
250e0 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
250e1 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e 6e  (pParse, pIdx->n
250e2 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 66  Column+1);.    f
250e3 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e  or(i=0; i<pIdx->
250e4 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
250e5 20 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70       int idx = p
250e6 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
250e7 3b 0a 20 20 20 20 20 20 69 66 28 20 69 64 78 3d  ;.      if( idx=
250e8 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a  =pTab->iPKey ){.
250e9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
250ea 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
250eb 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69 64 2c  SCopy, regRowid,
250ec 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 20 20   regIdx+i);.    
250ed 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
250ee 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
250ef 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
250f0 72 65 67 44 61 74 61 2b 69 64 78 2c 20 72 65 67  regData+idx, reg
250f1 49 64 78 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a  Idx+i);.      }.
250f2 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
250f3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
250f4 50 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69  P_SCopy, regRowi
250f5 64 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20  d, regIdx+i);.  
250f6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
250f7 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
250f8 63 6f 72 64 2c 20 72 65 67 49 64 78 2c 20 70 49  cord, regIdx, pI
250f9 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 2c 20 61  dx->nColumn+1, a
250fa 52 65 67 49 64 78 5b 69 43 75 72 5d 29 3b 0a 20  RegIdx[iCur]);. 
250fb 20 20 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41     sqlite3IndexA
250fc 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49  ffinityStr(v, pI
250fd 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dx);.    sqlite3
250fe 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
250ff 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
25100 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43  regIdx, pIdx->nC
25101 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f  olumn+1);..    /
25102 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20  * Find out what 
25103 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 69  action to take i
25104 6e 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20  n case there is 
25105 61 6e 20 69 6e 64 65 78 69 6e 67 20 63 6f 6e 66  an indexing conf
25106 6c 69 63 74 20 2a 2f 0a 20 20 20 20 6f 6e 45 72  lict */.    onEr
25107 72 6f 72 20 3d 20 70 49 64 78 2d 3e 6f 6e 45 72  ror = pIdx->onEr
25108 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45  ror;.    if( onE
25109 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 7b  rror==OE_None ){
2510a 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52   .      sqlite3R
2510b 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
2510c 70 50 61 72 73 65 2c 20 72 65 67 49 64 78 2c 20  pParse, regIdx, 
2510d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29  pIdx->nColumn+1)
2510e 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  ;.      continue
2510f 3b 20 20 2f 2a 20 70 49 64 78 20 69 73 20 6e 6f  ;  /* pIdx is no
25110 74 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  t a UNIQUE index
25111 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
25112 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21  ( overrideError!
25113 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
25114 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f       onError = o
25115 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 20  verrideError;.  
25116 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 72    }else if( onEr
25117 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
25118 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
25119 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
2511a 20 7d 0a 20 20 20 20 69 66 28 20 73 65 65 6e 52   }.    if( seenR
2511b 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eplace ){.      
2511c 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
2511d 49 67 6e 6f 72 65 20 29 20 6f 6e 45 72 72 6f 72  Ignore ) onError
2511e 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0a 20   = OE_Replace;. 
2511f 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 6f 6e       else if( on
25120 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 20 29  Error==OE_Fail )
25121 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
25122 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a  ort;.    }.    .
25123 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  .    /* Check to
25124 20 73 65 65 20 69 66 20 74 68 65 20 6e 65 77 20   see if the new 
25125 69 6e 64 65 78 20 65 6e 74 72 79 20 77 69 6c 6c  index entry will
25126 20 62 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20   be unique */.  
25127 20 20 72 65 67 52 20 3d 20 73 71 6c 69 74 65 33    regR = sqlite3
25128 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
25129 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
2512a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2512b 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69 64 2d  SCopy, regRowid-
2512c 68 61 73 54 77 6f 52 6f 77 69 64 73 2c 20 72 65  hasTwoRowids, re
2512d 67 52 29 3b 0a 20 20 20 20 6a 33 20 3d 20 73 71  gR);.    j3 = sq
2512e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2512f 76 2c 20 4f 50 5f 49 73 55 6e 69 71 75 65 2c 20  v, OP_IsUnique, 
25130 62 61 73 65 43 75 72 2b 69 43 75 72 2b 31 2c 20  baseCur+iCur+1, 
25131 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
25132 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
25133 67 52 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  gR, SQLITE_INT_T
25134 4f 5f 50 54 52 28 72 65 67 49 64 78 29 2c 0a 20  O_PTR(regIdx),. 
25135 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25136 20 20 20 20 20 20 20 20 20 20 50 34 5f 49 4e 54            P4_INT
25137 33 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  32);.    sqlite3
25138 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
25139 28 70 50 61 72 73 65 2c 20 72 65 67 49 64 78 2c  (pParse, regIdx,
2513a 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31   pIdx->nColumn+1
2513b 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72  );..    /* Gener
2513c 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 65 78  ate code that ex
2513d 65 63 75 74 65 73 20 69 66 20 74 68 65 20 6e 65  ecutes if the ne
2513e 77 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73  w index entry is
2513f 20 6e 6f 74 20 75 6e 69 71 75 65 20 2a 2f 0a 20   not unique */. 
25140 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72     assert( onErr
25141 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20  or==OE_Rollback 
25142 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41  || onError==OE_A
25143 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d  bort || onError=
25144 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20 20  =OE_Fail.       
25145 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
25146 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 72 72 6f  Ignore || onErro
25147 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b  r==OE_Replace );
25148 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45  .    switch( onE
25149 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 63 61  rror ){.      ca
2514a 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a  se OE_Rollback:.
2514b 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62        case OE_Ab
2514c 6f 72 74 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ort:.      case 
2514d 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20  OE_Fail: {.     
2514e 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
2514f 20 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73    StrAccum errMs
25150 67 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  g;.        const
25151 20 63 68 61 72 20 2a 7a 53 65 70 3b 0a 20 20 20   char *zSep;.   
25152 20 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 3b       char *zErr;
25153 0a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
25154 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 65  3StrAccumInit(&e
25155 72 72 4d 73 67 2c 20 30 2c 20 30 2c 20 32 30 30  rrMsg, 0, 0, 200
25156 29 3b 0a 20 20 20 20 20 20 20 20 65 72 72 4d 73  );.        errMs
25157 67 2e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64  g.db = pParse->d
25158 62 3b 0a 20 20 20 20 20 20 20 20 7a 53 65 70 20  b;.        zSep 
25159 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e  = pIdx->nColumn>
2515a 31 20 3f 20 22 63 6f 6c 75 6d 6e 73 20 22 20 3a  1 ? "columns " :
2515b 20 22 63 6f 6c 75 6d 6e 20 22 3b 0a 20 20 20 20   "column ";.    
2515c 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
2515d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b  Idx->nColumn; j+
2515e 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68  +){.          ch
2515f 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 61 62 2d  ar *zCol = pTab-
25160 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f  >aCol[pIdx->aiCo
25161 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a  lumn[j]].zName;.
25162 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25163 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
25164 26 65 72 72 4d 73 67 2c 20 7a 53 65 70 2c 20 2d  &errMsg, zSep, -
25165 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 53  1);.          zS
25166 65 70 20 3d 20 22 2c 20 22 3b 0a 20 20 20 20 20  ep = ", ";.     
25167 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
25168 63 63 75 6d 41 70 70 65 6e 64 28 26 65 72 72 4d  ccumAppend(&errM
25169 73 67 2c 20 7a 43 6f 6c 2c 20 2d 31 29 3b 0a 20  sg, zCol, -1);. 
2516a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2516b 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
2516c 41 70 70 65 6e 64 28 26 65 72 72 4d 73 67 2c 0a  Append(&errMsg,.
2516d 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64 78              pIdx
2516e 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 20  ->nColumn>1 ? " 
2516f 61 72 65 20 6e 6f 74 20 75 6e 69 71 75 65 22 20  are not unique" 
25170 3a 20 22 20 69 73 20 6e 6f 74 20 75 6e 69 71 75  : " is not uniqu
25171 65 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20  e", -1);.       
25172 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 53   zErr = sqlite3S
25173 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 65  trAccumFinish(&e
25174 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20  rrMsg);.        
25175 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25176 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  4(v, OP_Halt, SQ
25177 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c  LITE_CONSTRAINT,
25178 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 7a 45 72   onError, 0, zEr
25179 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  r, 0);.        s
2517a 71 6c 69 74 65 33 44 62 46 72 65 65 28 65 72 72  qlite3DbFree(err
2517b 4d 73 67 2e 64 62 2c 20 7a 45 72 72 29 3b 0a 20  Msg.db, zErr);. 
2517c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2517d 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
2517e 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20   OE_Ignore: {.  
2517f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65        assert( se
25180 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b 0a  enReplace==0 );.
25181 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
25182 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25183 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44  Goto, 0, ignoreD
25184 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72  est);.        br
25185 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
25186 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
25187 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e        assert( on
25188 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
25189 65 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  e );.        sql
2518a 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44  ite3GenerateRowD
2518b 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70 54  elete(pParse, pT
2518c 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67  ab, baseCur, reg
2518d 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  R, 0);.        s
2518e 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31 3b 0a  eenReplace = 1;.
2518f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
25190 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
25191 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
25192 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20  Here(v, j3);.   
25193 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
25194 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
25195 65 67 52 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  egR);.  }..  if(
25196 20 70 62 4d 61 79 52 65 70 6c 61 63 65 20 29 7b   pbMayReplace ){
25197 0a 20 20 20 20 2a 70 62 4d 61 79 52 65 70 6c 61  .    *pbMayRepla
25198 63 65 20 3d 20 73 65 65 6e 52 65 70 6c 61 63 65  ce = seenReplace
25199 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2519a 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
2519b 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20 66 69  rates code to fi
2519c 6e 69 73 68 20 74 68 65 20 49 4e 53 45 52 54 20  nish the INSERT 
2519d 6f 72 20 55 50 44 41 54 45 20 6f 70 65 72 61 74  or UPDATE operat
2519e 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 77 61 73 20  ion.** that was 
2519f 73 74 61 72 74 65 64 20 62 79 20 61 20 70 72 69  started by a pri
251a0 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  or call to sqlit
251a1 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
251a2 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a 2a 20 41  aintChecks..** A
251a3 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 61 6e   consecutive ran
251a4 67 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ge of registers 
251a5 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 52  starting at regR
251a6 6f 77 69 64 20 63 6f 6e 74 61 69 6e 73 20 74 68  owid contains th
251a7 65 0a 2a 2a 20 72 6f 77 69 64 20 61 6e 64 20 74  e.** rowid and t
251a8 68 65 20 63 6f 6e 74 65 6e 74 20 74 6f 20 62 65  he content to be
251a9 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a   inserted..**.**
251aa 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74   The arguments t
251ab 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  o this routine s
251ac 68 6f 75 6c 64 20 62 65 20 74 68 65 20 73 61 6d  hould be the sam
251ad 65 20 61 73 20 74 68 65 20 66 69 72 73 74 20 73  e as the first s
251ae 69 78 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20  ix.** arguments 
251af 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  to sqlite3Genera
251b0 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
251b1 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ks..*/.SQLITE_PR
251b2 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
251b3 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74  e3CompleteInsert
251b4 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
251b5 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  arse,      /* Th
251b6 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
251b7 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
251b8 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  b,        /* the
251b9 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
251ba 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
251bb 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65  ng */.  int base
251bc 43 75 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 49  Cur,        /* I
251bd 6e 64 65 78 20 6f 66 20 61 20 72 65 61 64 2f 77  ndex of a read/w
251be 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e  rite cursor poin
251bf 74 69 6e 67 20 61 74 20 70 54 61 62 20 2a 2f 0a  ting at pTab */.
251c0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 2c 20    int regRowid, 
251c1 20 20 20 20 20 20 2f 2a 20 52 61 6e 67 65 20 6f        /* Range o
251c2 66 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69  f content */.  i
251c3 6e 74 20 2a 61 52 65 67 49 64 78 2c 20 20 20 20  nt *aRegIdx,    
251c4 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75     /* Register u
251c5 73 65 64 20 62 79 20 65 61 63 68 20 69 6e 64 65  sed by each inde
251c6 78 2e 20 20 30 20 66 6f 72 20 75 6e 75 73 65 64  x.  0 for unused
251c7 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e   indices */.  in
251c8 74 20 69 73 55 70 64 61 74 65 2c 20 20 20 20 20  t isUpdate,     
251c9 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 55 50    /* True for UP
251ca 44 41 54 45 2c 20 46 61 6c 73 65 20 66 6f 72 20  DATE, False for 
251cb 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20  INSERT */.  int 
251cc 6e 65 77 49 64 78 2c 20 20 20 20 20 20 20 20 20  newIdx,         
251cd 2f 2a 20 49 6e 64 65 78 20 6f 66 20 4e 45 57 20  /* Index of NEW 
251ce 74 61 62 6c 65 20 66 6f 72 20 74 72 69 67 67 65  table for trigge
251cf 72 73 2e 20 20 2d 31 20 69 66 20 6e 6f 6e 65 20  rs.  -1 if none 
251d0 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 42  */.  int appendB
251d1 69 61 73 2c 20 20 20 20 20 2f 2a 20 54 72 75 65  ias,     /* True
251d2 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65   if this is like
251d3 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
251d4 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 53  nd */.  int useS
251d5 65 65 6b 52 65 73 75 6c 74 20 20 20 2f 2a 20 54  eekResult   /* T
251d6 72 75 65 20 74 6f 20 73 65 74 20 74 68 65 20 55  rue to set the U
251d7 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61  SESEEKRESULT fla
251d8 67 20 6f 6e 20 4f 50 5f 5b 49 64 78 5d 49 6e 73  g on OP_[Idx]Ins
251d9 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ert */.){.  int 
251da 69 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  i;.  Vdbe *v;.  
251db 69 6e 74 20 6e 49 64 78 3b 0a 20 20 49 6e 64 65  int nIdx;.  Inde
251dc 78 20 2a 70 49 64 78 3b 0a 20 20 75 38 20 70 69  x *pIdx;.  u8 pi
251dd 6b 5f 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72  k_flags;.  int r
251de 65 67 44 61 74 61 3b 0a 20 20 69 6e 74 20 72 65  egData;.  int re
251df 67 52 65 63 3b 0a 0a 20 20 76 20 3d 20 73 71 6c  gRec;..  v = sql
251e0 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
251e1 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  se);.  assert( v
251e2 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
251e3 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
251e4 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61  0 );  /* This ta
251e5 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45  ble is not a VIE
251e6 57 20 2a 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d  W */.  for(nIdx=
251e7 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
251e8 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
251e9 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49  =pIdx->pNext, nI
251ea 64 78 2b 2b 29 7b 7d 0a 20 20 66 6f 72 28 69 3d  dx++){}.  for(i=
251eb 6e 49 64 78 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  nIdx-1; i>=0; i-
251ec 2d 29 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67  -){.    if( aReg
251ed 49 64 78 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  Idx[i]==0 ) cont
251ee 69 6e 75 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  inue;.    sqlite
251ef 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
251f0 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 62 61 73  P_IdxInsert, bas
251f1 65 43 75 72 2b 69 2b 31 2c 20 61 52 65 67 49 64  eCur+i+1, aRegId
251f2 78 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 75  x[i]);.    if( u
251f3 73 65 53 65 65 6b 52 65 73 75 6c 74 20 29 7b 0a  seSeekResult ){.
251f4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
251f5 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46  eChangeP5(v, OPF
251f6 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
251f7 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  T);.    }.  }.  
251f8 72 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f 77  regData = regRow
251f9 69 64 20 2b 20 31 3b 0a 20 20 72 65 67 52 65 63  id + 1;.  regRec
251fa 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
251fb 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
251fc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
251fd 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
251fe 72 64 2c 20 72 65 67 44 61 74 61 2c 20 70 54 61  rd, regData, pTa
251ff 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29  b->nCol, regRec)
25200 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ;.  sqlite3Table
25201 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
25202 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  Tab);.  sqlite3E
25203 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
25204 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
25205 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43  egData, pTab->nC
25206 6f 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ol);.#ifndef SQL
25207 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
25208 0a 20 20 69 66 28 20 6e 65 77 49 64 78 3e 3d 30  .  if( newIdx>=0
25209 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2520a 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2520b 49 6e 73 65 72 74 2c 20 6e 65 77 49 64 78 2c 20  Insert, newIdx, 
2520c 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64  regRec, regRowid
2520d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
2520e 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
2520f 65 64 20 29 7b 0a 20 20 20 20 70 69 6b 5f 66 6c  ed ){.    pik_fl
25210 61 67 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  ags = 0;.  }else
25211 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73 20  {.    pik_flags 
25212 3d 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  = OPFLAG_NCHANGE
25213 3b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73 20  ;.    pik_flags 
25214 7c 3d 20 28 69 73 55 70 64 61 74 65 3f 4f 50 46  |= (isUpdate?OPF
25215 4c 41 47 5f 49 53 55 50 44 41 54 45 3a 4f 50 46  LAG_ISUPDATE:OPF
25216 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 29 3b 0a  LAG_LASTROWID);.
25217 20 20 7d 0a 20 20 69 66 28 20 61 70 70 65 6e 64    }.  if( append
25218 42 69 61 73 20 29 7b 0a 20 20 20 20 70 69 6b 5f  Bias ){.    pik_
25219 66 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41 47 5f  flags |= OPFLAG_
2521a 41 50 50 45 4e 44 3b 0a 20 20 7d 0a 20 20 69 66  APPEND;.  }.  if
2521b 28 20 75 73 65 53 65 65 6b 52 65 73 75 6c 74 20  ( useSeekResult 
2521c 29 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73  ){.    pik_flags
2521d 20 7c 3d 20 4f 50 46 4c 41 47 5f 55 53 45 53 45   |= OPFLAG_USESE
2521e 45 4b 52 45 53 55 4c 54 3b 0a 20 20 7d 0a 20 20  EKRESULT;.  }.  
2521f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25220 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
25221 62 61 73 65 43 75 72 2c 20 72 65 67 52 65 63 2c  baseCur, regRec,
25222 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 69 66   regRowid);.  if
25223 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  ( !pParse->neste
25224 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
25225 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
25226 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  -1, pTab->zName,
25227 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d   P4_STATIC);.  }
25228 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
25229 61 6e 67 65 50 35 28 76 2c 20 70 69 6b 5f 66 6c  angeP5(v, pik_fl
2522a 61 67 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ags);.}../*.** G
2522b 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
2522c 74 20 77 69 6c 6c 20 6f 70 65 6e 20 63 75 72 73  t will open curs
2522d 6f 72 73 20 66 6f 72 20 61 20 74 61 62 6c 65 20  ors for a table 
2522e 61 6e 64 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 69  and for all.** i
2522f 6e 64 69 63 65 73 20 6f 66 20 74 68 61 74 20 74  ndices of that t
25230 61 62 6c 65 2e 20 20 54 68 65 20 22 62 61 73 65  able.  The "base
25231 43 75 72 22 20 70 61 72 61 6d 65 74 65 72 20 69  Cur" parameter i
25232 73 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  s the cursor num
25233 62 65 72 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20  ber used.** for 
25234 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 64 69  the table.  Indi
25235 63 65 73 20 61 72 65 20 6f 70 65 6e 65 64 20 6f  ces are opened o
25236 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 75 72  n subsequent cur
25237 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  sors..**.** Retu
25238 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
25239 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 20   indices on the 
2523a 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
2523b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2523c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
2523d 49 6e 64 69 63 65 73 28 0a 20 20 50 61 72 73 65  Indices(.  Parse
2523e 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50   *pParse,   /* P
2523f 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
25240 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
25241 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
25242 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
25243 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20  int baseCur,    
25244 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
25245 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68  r assigned to th
25246 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
25247 20 6f 70 20 20 20 20 20 20 20 20 20 20 20 2f 2a   op           /*
25248 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20   OP_OpenRead or 
25249 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0a  OP_OpenWrite */.
2524a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
2524b 74 20 69 44 62 3b 0a 20 20 49 6e 64 65 78 20 2a  t iDb;.  Index *
2524c 70 49 64 78 3b 0a 20 20 56 64 62 65 20 2a 76 3b  pIdx;.  Vdbe *v;
2524d 0a 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  ..  if( IsVirtua
2524e 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e  l(pTab) ) return
2524f 20 30 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69   0;.  iDb = sqli
25250 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
25251 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
25252 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 76  b->pSchema);.  v
25253 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
25254 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73  e(pParse);.  ass
25255 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 73  ert( v!=0 );.  s
25256 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
25257 70 50 61 72 73 65 2c 20 62 61 73 65 43 75 72 2c  pParse, baseCur,
25258 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 29 3b   iDb, pTab, op);
25259 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78  .  for(i=1, pIdx
2525a 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2525b 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2525c 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20 20  pNext, i++){.   
2525d 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d   KeyInfo *pKey =
2525e 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79   sqlite3IndexKey
2525f 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64  info(pParse, pId
25260 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  x);.    assert( 
25261 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  pIdx->pSchema==p
25262 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a  Tab->pSchema );.
25263 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25264 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69 2b 62  ddOp4(v, op, i+b
25265 61 73 65 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e  aseCur, pIdx->tn
25266 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20  um, iDb,.       
25267 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
25268 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
25269 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
2526a 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
2526b 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d  ((v, "%s", pIdx-
2526c 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 7d 0a 20 20  >zName));.  }.  
2526d 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  if( pParse->nTab
2526e 3c 62 61 73 65 43 75 72 2b 69 20 29 7b 0a 20 20  <baseCur+i ){.  
2526f 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d    pParse->nTab =
25270 20 62 61 73 65 43 75 72 2b 69 3b 0a 20 20 7d 0a   baseCur+i;.  }.
25271 20 20 72 65 74 75 72 6e 20 69 2d 31 3b 0a 7d 0a    return i-1;.}.
25272 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
25273 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  TEST./*.** The f
25274 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20  ollowing global 
25275 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72  variable is incr
25276 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72  emented whenever
25277 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 66 65 72   the.** transfer
25278 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
25279 20 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20   used.  This is 
2527a 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
2527b 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c  .** purposes onl
2527c 79 20 2d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  y - to make sure
2527d 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70   the transfer op
2527e 74 69 6d 69 7a 61 74 69 6f 6e 20 72 65 61 6c 6c  timization reall
2527f 79 0a 2a 2a 20 69 73 20 68 61 70 70 65 6e 69 6e  y.** is happenin
25280 67 20 77 68 65 6e 20 69 74 20 69 73 20 73 75 70  g when it is sup
25281 70 6f 73 65 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49  pose to..*/.SQLI
25282 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
25283 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f 75 6e 74  e3_xferopt_count
25284 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ;.#endif /* SQLI
25285 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 23 69 66  TE_TEST */...#if
25286 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
25287 5f 58 46 45 52 5f 4f 50 54 0a 2f 2a 0a 2a 2a 20  _XFER_OPT./*.** 
25288 43 68 65 63 6b 20 74 6f 20 63 6f 6c 6c 61 74 69  Check to collati
25289 6f 6e 20 6e 61 6d 65 73 20 74 6f 20 73 65 65 20  on names to see 
2528a 69 66 20 74 68 65 79 20 61 72 65 20 63 6f 6d 70  if they are comp
2528b 61 74 69 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  atible..*/.stati
2528c 63 20 69 6e 74 20 78 66 65 72 43 6f 6d 70 61 74  c int xferCompat
2528d 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 63 6f  ibleCollation(co
2528e 6e 73 74 20 63 68 61 72 20 2a 7a 31 2c 20 63 6f  nst char *z1, co
2528f 6e 73 74 20 63 68 61 72 20 2a 7a 32 29 7b 0a 20  nst char *z2){. 
25290 20 69 66 28 20 7a 31 3d 3d 30 20 29 7b 0a 20 20   if( z1==0 ){.  
25291 20 20 72 65 74 75 72 6e 20 7a 32 3d 3d 30 3b 0a    return z2==0;.
25292 20 20 7d 0a 20 20 69 66 28 20 7a 32 3d 3d 30 20    }.  if( z2==0 
25293 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
25294 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
25295 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 31 2c  lite3StrICmp(z1,
25296 20 7a 32 29 3d 3d 30 3b 0a 7d 0a 0a 0a 2f 2a 0a   z2)==0;.}.../*.
25297 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  ** Check to see 
25298 69 66 20 69 6e 64 65 78 20 70 53 72 63 20 69 73  if index pSrc is
25299 20 63 6f 6d 70 61 74 69 62 6c 65 20 61 73 20 61   compatible as a
2529a 20 73 6f 75 72 63 65 20 6f 66 20 64 61 74 61 0a   source of data.
2529b 2a 2a 20 66 6f 72 20 69 6e 64 65 78 20 70 44 65  ** for index pDe
2529c 73 74 20 69 6e 20 61 6e 20 69 6e 73 65 72 74 20  st in an insert 
2529d 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a  transfer optimiz
2529e 61 74 69 6f 6e 2e 20 20 54 68 65 20 72 75 6c 65  ation.  The rule
2529f 73 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70 61  s.** for a compa
252a0 74 69 62 6c 65 20 69 6e 64 65 78 3a 0a 2a 2a 0a  tible index:.**.
252a1 2a 2a 20 20 20 20 2a 20 20 20 54 68 65 20 69 6e  **    *   The in
252a2 64 65 78 20 69 73 20 6f 76 65 72 20 74 68 65 20  dex is over the 
252a3 73 61 6d 65 20 73 65 74 20 6f 66 20 63 6f 6c 75  same set of colu
252a4 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  mns.**    *   Th
252a5 65 20 73 61 6d 65 20 44 45 53 43 20 61 6e 64 20  e same DESC and 
252a6 41 53 43 20 6d 61 72 6b 69 6e 67 73 20 6f 63 63  ASC markings occ
252a7 75 72 73 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d  urs on all colum
252a8 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68 65  ns.**    *   The
252a9 20 73 61 6d 65 20 6f 6e 45 72 72 6f 72 20 70 72   same onError pr
252aa 6f 63 65 73 73 69 6e 67 20 28 4f 45 5f 41 62 6f  ocessing (OE_Abo
252ab 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 65  rt, OE_Ignore, e
252ac 74 63 29 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  tc).**    *   Th
252ad 65 20 73 61 6d 65 20 63 6f 6c 6c 61 74 69 6e 67  e same collating
252ae 20 73 65 71 75 65 6e 63 65 20 6f 6e 20 65 61 63   sequence on eac
252af 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74  h column.*/.stat
252b0 69 63 20 69 6e 74 20 78 66 65 72 43 6f 6d 70 61  ic int xferCompa
252b1 74 69 62 6c 65 49 6e 64 65 78 28 49 6e 64 65 78  tibleIndex(Index
252b2 20 2a 70 44 65 73 74 2c 20 49 6e 64 65 78 20 2a   *pDest, Index *
252b3 70 53 72 63 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  pSrc){.  int i;.
252b4 20 20 61 73 73 65 72 74 28 20 70 44 65 73 74 20    assert( pDest 
252b5 26 26 20 70 53 72 63 20 29 3b 0a 20 20 61 73 73  && pSrc );.  ass
252b6 65 72 74 28 20 70 44 65 73 74 2d 3e 70 54 61 62  ert( pDest->pTab
252b7 6c 65 21 3d 70 53 72 63 2d 3e 70 54 61 62 6c 65  le!=pSrc->pTable
252b8 20 29 3b 0a 20 20 69 66 28 20 70 44 65 73 74 2d   );.  if( pDest-
252b9 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 53 72 63 2d 3e  >nColumn!=pSrc->
252ba 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 72  nColumn ){.    r
252bb 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69  eturn 0;   /* Di
252bc 66 66 65 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f  fferent number o
252bd 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 7d  f columns */.  }
252be 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6f 6e  .  if( pDest->on
252bf 45 72 72 6f 72 21 3d 70 53 72 63 2d 3e 6f 6e 45  Error!=pSrc->onE
252c0 72 72 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75  rror ){.    retu
252c1 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65  rn 0;   /* Diffe
252c2 72 65 6e 74 20 63 6f 6e 66 6c 69 63 74 20 72 65  rent conflict re
252c3 73 6f 6c 75 74 69 6f 6e 20 73 74 72 61 74 65 67  solution strateg
252c4 69 65 73 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72  ies */.  }.  for
252c5 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 43  (i=0; i<pSrc->nC
252c6 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
252c7 20 69 66 28 20 70 53 72 63 2d 3e 61 69 43 6f 6c   if( pSrc->aiCol
252c8 75 6d 6e 5b 69 5d 21 3d 70 44 65 73 74 2d 3e 61  umn[i]!=pDest->a
252c9 69 43 6f 6c 75 6d 6e 5b 69 5d 20 29 7b 0a 20 20  iColumn[i] ){.  
252ca 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
252cb 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6c  /* Different col
252cc 75 6d 6e 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a  umns indexed */.
252cd 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
252ce 72 63 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69  rc->aSortOrder[i
252cf 5d 21 3d 70 44 65 73 74 2d 3e 61 53 6f 72 74 4f  ]!=pDest->aSortO
252d0 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20 20  rder[i] ){.     
252d1 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
252d2 44 69 66 66 65 72 65 6e 74 20 73 6f 72 74 20 6f  Different sort o
252d3 72 64 65 72 73 20 2a 2f 0a 20 20 20 20 7d 0a 20  rders */.    }. 
252d4 20 20 20 69 66 28 20 21 78 66 65 72 43 6f 6d 70     if( !xferComp
252d5 61 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28  atibleCollation(
252d6 70 53 72 63 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 2c  pSrc->azColl[i],
252d7 70 44 65 73 74 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d  pDest->azColl[i]
252d8 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
252d9 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72  n 0;   /* Differ
252da 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ent collating se
252db 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 20 20 7d  quences */.    }
252dc 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f  .  }..  /* If no
252dd 20 74 65 73 74 20 61 62 6f 76 65 20 66 61 69 6c   test above fail
252de 73 20 74 68 65 6e 20 74 68 65 20 69 6e 64 69 63  s then the indic
252df 65 73 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 61  es must be compa
252e0 74 69 62 6c 65 20 2a 2f 0a 20 20 72 65 74 75 72  tible */.  retur
252e1 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74  n 1;.}../*.** At
252e2 74 65 6d 70 74 20 74 68 65 20 74 72 61 6e 73 66  tempt the transf
252e3 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  er optimization 
252e4 6f 6e 20 49 4e 53 45 52 54 73 20 6f 66 20 74 68  on INSERTs of th
252e5 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20  e form.**.**    
252e6 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62   INSERT INTO tab
252e7 31 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  1 SELECT * FROM 
252e8 74 61 62 32 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tab2;.**.** This
252e9 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
252ea 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20   only attempted 
252eb 69 66 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20  if.**.**    (1) 
252ec 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20 68   tab1 and tab2 h
252ed 61 76 65 20 69 64 65 6e 74 69 63 61 6c 20 73 63  ave identical sc
252ee 68 65 6d 61 73 20 69 6e 63 6c 75 64 69 6e 67 20  hemas including 
252ef 61 6c 6c 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  all the.**      
252f0 20 20 20 73 61 6d 65 20 69 6e 64 69 63 65 73 20     same indices 
252f1 61 6e 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  and constraints.
252f2 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 74 61  **.**    (2)  ta
252f3 62 31 20 61 6e 64 20 74 61 62 32 20 61 72 65 20  b1 and tab2 are 
252f4 64 69 66 66 65 72 65 6e 74 20 74 61 62 6c 65 73  different tables
252f5 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20 54  .**.**    (3)  T
252f6 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20  here must be no 
252f7 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 31  triggers on tab1
252f8 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29 20 20 54  .**.**    (4)  T
252f9 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
252fa 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
252fb 65 6d 65 6e 74 20 69 73 20 22 2a 22 0a 2a 2a 0a  ement is "*".**.
252fc 2a 2a 20 20 20 20 28 35 29 20 20 54 68 65 20 53  **    (5)  The S
252fd 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
252fe 68 61 73 20 6e 6f 20 57 48 45 52 45 2c 20 48 41  has no WHERE, HA
252ff 56 49 4e 47 2c 20 4f 52 44 45 52 20 42 59 2c 20  VING, ORDER BY, 
25300 47 52 4f 55 50 20 42 59 2c 0a 2a 2a 20 20 20 20  GROUP BY,.**    
25301 20 20 20 20 20 6f 72 20 4c 49 4d 49 54 20 63 6c       or LIMIT cl
25302 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28  ause..**.**    (
25303 36 29 20 20 54 68 65 20 53 45 4c 45 43 54 20 73  6)  The SELECT s
25304 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69  tatement is a si
25305 6d 70 6c 65 20 28 6e 6f 74 20 61 20 63 6f 6d 70  mple (not a comp
25306 6f 75 6e 64 29 20 73 65 6c 65 63 74 20 74 68 61  ound) select tha
25307 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e  t.**         con
25308 74 61 69 6e 73 20 6f 6e 6c 79 20 74 61 62 32 20  tains only tab2 
25309 69 6e 20 69 74 73 20 46 52 4f 4d 20 63 6c 61 75  in its FROM clau
2530a 73 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65  se.**.** This me
2530b 74 68 6f 64 20 66 6f 72 20 69 6d 70 6c 65 6d 65  thod for impleme
2530c 6e 74 69 6e 67 20 74 68 65 20 49 4e 53 45 52 54  nting the INSERT
2530d 20 74 72 61 6e 73 66 65 72 73 20 72 61 77 20 72   transfers raw r
2530e 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74  ecords from.** t
2530f 61 62 32 20 6f 76 65 72 20 74 6f 20 74 61 62 31  ab2 over to tab1
25310 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 61  .  The columns a
25311 72 65 20 6e 6f 74 20 64 65 63 6f 64 65 64 2e 20  re not decoded. 
25312 20 52 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f   Raw records fro
25313 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 69 63 65 73  m.** the indices
25314 20 6f 66 20 74 61 62 32 20 61 72 65 20 74 72 61   of tab2 are tra
25315 6e 73 66 65 72 65 64 20 74 6f 20 74 61 62 31 20  nsfered to tab1 
25316 61 73 20 77 65 6c 6c 2e 20 20 49 6e 20 73 6f 20  as well.  In so 
25317 64 6f 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 72 65  doing,.** the re
25318 73 75 6c 74 69 6e 67 20 74 61 62 31 20 68 61 73  sulting tab1 has
25319 20 6d 75 63 68 20 6c 65 73 73 20 66 72 61 67 6d   much less fragm
2531a 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  entation..**.** 
2531b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
2531c 75 72 6e 73 20 54 52 55 45 20 69 66 20 74 68 65  urns TRUE if the
2531d 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
2531e 20 61 74 74 65 6d 70 74 65 64 2e 20 20 49 66 20   attempted.  If 
2531f 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f  any.** of the co
25320 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 66  nditions above f
25321 61 69 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20  ail so that the 
25322 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 73 68 6f  optimization sho
25323 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 61 74  uld not.** be at
25324 74 65 6d 70 74 65 64 2c 20 74 68 65 6e 20 74 68  tempted, then th
25325 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
25326 6e 73 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61  ns FALSE..*/.sta
25327 74 69 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69  tic int xferOpti
25328 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73  mization(.  Pars
25329 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2532a 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
2532b 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
2532c 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f  pDest,         /
2532d 2a 20 54 68 65 20 74 61 62 6c 65 20 77 65 20 61  * The table we a
2532e 72 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74  re inserting int
2532f 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  o */.  Select *p
25330 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20  Select,      /* 
25331 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
25332 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  nt to use as the
25333 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a   data source */.
25334 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20    int onError,  
25335 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74          /* How t
25336 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61  o handle constra
25337 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  int errors */.  
25338 69 6e 74 20 69 44 62 44 65 73 74 20 20 20 20 20  int iDbDest     
25339 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2533a 61 62 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a  abase of pDest *
2533b 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20  /.){.  ExprList 
2533c 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20  *pEList;        
2533d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
2533e 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
2533f 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 54 61 62   SELECT */.  Tab
25340 6c 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20  le *pSrc;       
25341 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25342 20 54 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68   The table in th
25343 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
25344 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 49 6e 64   SELECT */.  Ind
25345 65 78 20 2a 70 53 72 63 49 64 78 2c 20 2a 70 44  ex *pSrcIdx, *pD
25346 65 73 74 49 64 78 3b 20 20 20 20 20 20 20 2f 2a  estIdx;       /*
25347 20 53 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74   Source and dest
25348 69 6e 61 74 69 6f 6e 20 69 6e 64 69 63 65 73 20  ination indices 
25349 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
2534a 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
2534b 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 6c 65 6d        /* An elem
2534c 65 6e 74 20 6f 66 20 70 53 65 6c 65 63 74 2d 3e  ent of pSelect->
2534d 70 53 72 63 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  pSrc */.  int i;
2534e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2534f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
25350 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
25351 69 6e 74 20 69 44 62 53 72 63 3b 20 20 20 20 20  int iDbSrc;     
25352 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25353 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
25354 20 6f 66 20 70 53 72 63 20 2a 2f 0a 20 20 69 6e   of pSrc */.  in
25355 74 20 69 53 72 63 2c 20 69 44 65 73 74 3b 20 20  t iSrc, iDest;  
25356 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25357 2a 20 43 75 72 73 6f 72 73 20 66 72 6f 6d 20 73  * Cursors from s
25358 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
25359 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61  ation */.  int a
2535a 64 64 72 31 2c 20 61 64 64 72 32 3b 20 20 20 20  ddr1, addr2;    
2535b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2535c 6f 6f 70 20 61 64 64 72 65 73 73 65 73 20 2a 2f  oop addresses */
2535d 0a 20 20 69 6e 74 20 65 6d 70 74 79 44 65 73 74  .  int emptyDest
2535e 54 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Test;           
2535f 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
25360 66 20 74 65 73 74 20 66 6f 72 20 65 6d 70 74 79  f test for empty
25361 20 70 44 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20   pDest */.  int 
25362 65 6d 70 74 79 53 72 63 54 65 73 74 3b 20 20 20  emptySrcTest;   
25363 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25364 41 64 64 72 65 73 73 20 6f 66 20 74 65 73 74 20  Address of test 
25365 66 6f 72 20 65 6d 70 74 79 20 70 53 72 63 20 2a  for empty pSrc *
25366 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
25367 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25368 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45       /* The VDBE
25369 20 77 65 20 61 72 65 20 62 75 69 6c 64 69 6e 67   we are building
2536a 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
2536b 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
2536c 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 69 6e         /* Key in
2536d 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 6e  formation for an
2536e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
2536f 72 65 67 41 75 74 6f 69 6e 63 3b 20 20 20 20 20  regAutoinc;     
25370 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25371 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20  Memory register 
25372 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 20  used by AUTOINC 
25373 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 48 61 73  */.  int destHas
25374 55 6e 69 71 75 65 49 64 78 20 3d 20 30 3b 20 20  UniqueIdx = 0;  
25375 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
25376 20 70 44 65 73 74 20 68 61 73 20 61 20 55 4e 49   pDest has a UNI
25377 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  QUE index */.  i
25378 6e 74 20 72 65 67 44 61 74 61 2c 20 72 65 67 52  nt regData, regR
25379 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  owid;           
2537a 2f 2a 20 52 65 67 69 73 74 65 72 73 20 68 6f 6c  /* Registers hol
2537b 64 69 6e 67 20 64 61 74 61 20 61 6e 64 20 72 6f  ding data and ro
2537c 77 69 64 20 2a 2f 0a 0a 20 20 69 66 28 20 70 53  wid */..  if( pS
2537d 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20  elect==0 ){.    
2537e 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4d  return 0;   /* M
2537f 75 73 74 20 62 65 20 6f 66 20 74 68 65 20 66 6f  ust be of the fo
25380 72 6d 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  rm  INSERT INTO 
25381 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a  ... SELECT ... *
25382 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  /.  }.  if( sqli
25383 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70  te3TriggerList(p
25384 50 61 72 73 65 2c 20 70 44 65 73 74 29 20 29 7b  Parse, pDest) ){
25385 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
25386 20 2f 2a 20 74 61 62 31 20 6d 75 73 74 20 6e 6f   /* tab1 must no
25387 74 20 68 61 76 65 20 74 72 69 67 67 65 72 73 20  t have triggers 
25388 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  */.  }.#ifndef S
25389 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2538a 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 44  ALTABLE.  if( pD
2538b 65 73 74 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  est->tabFlags & 
2538c 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20 20  TF_Virtual ){.  
2538d 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
2538e 20 74 61 62 31 20 6d 75 73 74 20 6e 6f 74 20 62   tab1 must not b
2538f 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  e a virtual tabl
25390 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  e */.  }.#endif.
25391 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f    if( onError==O
25392 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20  E_Default ){.   
25393 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
25394 6f 72 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f  ort;.  }.  if( o
25395 6e 45 72 72 6f 72 21 3d 4f 45 5f 41 62 6f 72 74  nError!=OE_Abort
25396 20 26 26 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f   && onError!=OE_
25397 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Rollback ){.    
25398 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 43  return 0;   /* C
25399 61 6e 6e 6f 74 20 64 6f 20 4f 52 20 52 45 50 4c  annot do OR REPL
2539a 41 43 45 20 6f 72 20 4f 52 20 49 47 4e 4f 52 45  ACE or OR IGNORE
2539b 20 6f 72 20 4f 52 20 46 41 49 4c 20 2a 2f 0a 20   or OR FAIL */. 
2539c 20 7d 0a 20 20 61 73 73 65 72 74 28 70 53 65 6c   }.  assert(pSel
2539d 65 63 74 2d 3e 70 53 72 63 29 3b 20 20 20 2f 2a  ect->pSrc);   /*
2539e 20 61 6c 6c 6f 63 61 74 65 64 20 65 76 65 6e 20   allocated even 
2539f 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 46  if there is no F
253a0 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
253a1 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  if( pSelect->pSr
253a2 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a 20 20  c->nSrc!=1 ){.  
253a3 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
253a4 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6d 75 73   FROM clause mus
253a5 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f  t have exactly o
253a6 6e 65 20 74 65 72 6d 20 2a 2f 0a 20 20 7d 0a 20  ne term */.  }. 
253a7 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 53   if( pSelect->pS
253a8 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74  rc->a[0].pSelect
253a9 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
253aa 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75  ;   /* FROM clau
253ab 73 65 20 63 61 6e 6e 6f 74 20 63 6f 6e 74 61 69  se cannot contai
253ac 6e 20 61 20 73 75 62 71 75 65 72 79 20 2a 2f 0a  n a subquery */.
253ad 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63    }.  if( pSelec
253ae 74 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20  t->pWhere ){.   
253af 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
253b0 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68  SELECT may not h
253b1 61 76 65 20 61 20 57 48 45 52 45 20 63 6c 61 75  ave a WHERE clau
253b2 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  se */.  }.  if( 
253b3 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 42  pSelect->pOrderB
253b4 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  y ){.    return 
253b5 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d  0;   /* SELECT m
253b6 61 79 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 4f  ay not have an O
253b7 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
253b8 2f 0a 20 20 7d 0a 20 20 2f 2a 20 44 6f 20 6e 6f  /.  }.  /* Do no
253b9 74 20 6e 65 65 64 20 74 6f 20 74 65 73 74 20 66  t need to test f
253ba 6f 72 20 61 20 48 41 56 49 4e 47 20 63 6c 61 75  or a HAVING clau
253bb 73 65 2e 20 20 49 66 20 48 41 56 49 4e 47 20 69  se.  If HAVING i
253bc 73 20 70 72 65 73 65 6e 74 20 62 75 74 0a 20 20  s present but.  
253bd 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f  ** there is no O
253be 52 44 45 52 20 42 59 2c 20 77 65 20 77 69 6c 6c  RDER BY, we will
253bf 20 67 65 74 20 61 6e 20 65 72 72 6f 72 2e 20 2a   get an error. *
253c0 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  /.  if( pSelect-
253c1 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20  >pGroupBy ){.   
253c2 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
253c3 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68  SELECT may not h
253c4 61 76 65 20 61 20 47 52 4f 55 50 20 42 59 20 63  ave a GROUP BY c
253c5 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 69  lause */.  }.  i
253c6 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 4c 69 6d  f( pSelect->pLim
253c7 69 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  it ){.    return
253c8 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
253c9 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 4c  may not have a L
253ca 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a 20  IMIT clause */. 
253cb 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 53 65   }.  assert( pSe
253cc 6c 65 63 74 2d 3e 70 4f 66 66 73 65 74 3d 3d 30  lect->pOffset==0
253cd 20 29 3b 20 20 2f 2a 20 4d 75 73 74 20 62 65 20   );  /* Must be 
253ce 73 6f 20 69 66 20 70 4c 69 6d 69 74 3d 3d 30 20  so if pLimit==0 
253cf 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74  */.  if( pSelect
253d0 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20  ->pPrior ){.    
253d1 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53  return 0;   /* S
253d2 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 62 65  ELECT may not be
253d3 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72   a compound quer
253d4 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  y */.  }.  if( p
253d5 53 65 6c 65 63 74 2d 3e 73 65 6c 46 6c 61 67 73  Select->selFlags
253d6 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 20 29   & SF_Distinct )
253d7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
253d8 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20    /* SELECT may 
253d9 6e 6f 74 20 62 65 20 44 49 53 54 49 4e 43 54 20  not be DISTINCT 
253da 2a 2f 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74 20  */.  }.  pEList 
253db 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  = pSelect->pELis
253dc 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c  t;.  assert( pEL
253dd 69 73 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ist!=0 );.  if( 
253de 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
253df 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
253e0 3b 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c  ;   /* The resul
253e1 74 20 73 65 74 20 6d 75 73 74 20 68 61 76 65 20  t set must have 
253e2 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 6f 6c 75  exactly one colu
253e3 6d 6e 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65  mn */.  }.  asse
253e4 72 74 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d  rt( pEList->a[0]
253e5 2e 70 45 78 70 72 20 29 3b 0a 20 20 69 66 28 20  .pExpr );.  if( 
253e6 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
253e7 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20 29  pr->op!=TK_ALL )
253e8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
253e9 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20    /* The result 
253ea 73 65 74 20 6d 75 73 74 20 62 65 20 74 68 65 20  set must be the 
253eb 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f 72  special operator
253ec 20 22 2a 22 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f   "*" */.  }..  /
253ed 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20  * At this point 
253ee 77 65 20 68 61 76 65 20 65 73 74 61 62 6c 69 73  we have establis
253ef 68 65 64 20 74 68 61 74 20 74 68 65 20 73 74 61  hed that the sta
253f0 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65  tement is of the
253f1 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 20 73 79  .  ** correct sy
253f2 6e 74 61 63 74 69 63 20 66 6f 72 6d 20 74 6f 20  ntactic form to 
253f3 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74  participate in t
253f4 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
253f5 2e 20 20 4e 6f 77 0a 20 20 2a 2a 20 77 65 20 68  .  Now.  ** we h
253f6 61 76 65 20 74 6f 20 63 68 65 63 6b 20 74 68 65  ave to check the
253f7 20 73 65 6d 61 6e 74 69 63 73 2e 0a 20 20 2a 2f   semantics..  */
253f8 0a 20 20 70 49 74 65 6d 20 3d 20 70 53 65 6c 65  .  pItem = pSele
253f9 63 74 2d 3e 70 53 72 63 2d 3e 61 3b 0a 20 20 70  ct->pSrc->a;.  p
253fa 53 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  Src = sqlite3Loc
253fb 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
253fc 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65   0, pItem->zName
253fd 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  , pItem->zDataba
253fe 73 65 29 3b 0a 20 20 69 66 28 20 70 53 72 63 3d  se);.  if( pSrc=
253ff 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
25400 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c   0;   /* FROM cl
25401 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f  ause does not co
25402 6e 74 61 69 6e 20 61 20 72 65 61 6c 20 74 61 62  ntain a real tab
25403 6c 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  le */.  }.  if( 
25404 70 53 72 63 3d 3d 70 44 65 73 74 20 29 7b 0a 20  pSrc==pDest ){. 
25405 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
25406 2a 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20  * tab1 and tab2 
25407 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20 73  may not be the s
25408 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d  ame table */.  }
25409 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2540a 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2540b 45 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 74 61  E.  if( pSrc->ta
2540c 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
2540d 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ual ){.    retur
2540e 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20 6d  n 0;   /* tab2 m
2540f 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76 69 72  ust not be a vir
25410 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
25411 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  }.#endif.  if( p
25412 53 72 63 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Src->pSelect ){.
25413 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
25414 2f 2a 20 74 61 62 32 20 6d 61 79 20 6e 6f 74 20  /* tab2 may not 
25415 62 65 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 7d  be a view */.  }
25416 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6e 43  .  if( pDest->nC
25417 6f 6c 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 20 29  ol!=pSrc->nCol )
25418 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
25419 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
2541a 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 62 65 20 74  olumns must be t
2541b 68 65 20 73 61 6d 65 20 69 6e 20 74 61 62 31 20  he same in tab1 
2541c 61 6e 64 20 74 61 62 32 20 2a 2f 0a 20 20 7d 0a  and tab2 */.  }.
2541d 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50 4b    if( pDest->iPK
2541e 65 79 21 3d 70 53 72 63 2d 3e 69 50 4b 65 79 20  ey!=pSrc->iPKey 
2541f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
25420 20 20 20 2f 2a 20 42 6f 74 68 20 74 61 62 6c 65     /* Both table
25421 73 20 6d 75 73 74 20 68 61 76 65 20 74 68 65 20  s must have the 
25422 73 61 6d 65 20 49 4e 54 45 47 45 52 20 50 52 49  same INTEGER PRI
25423 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 0a  MARY KEY */.  }.
25424 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 44 65    for(i=0; i<pDe
25425 73 74 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  st->nCol; i++){.
25426 20 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 61      if( pDest->a
25427 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 21  Col[i].affinity!
25428 3d 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 61  =pSrc->aCol[i].a
25429 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20  ffinity ){.     
2542a 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a   return 0;    /*
2542b 20 41 66 66 69 6e 69 74 79 20 6d 75 73 74 20 62   Affinity must b
2542c 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20 61 6c  e the same on al
2542d 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 20  l columns */.   
2542e 20 7d 0a 20 20 20 20 69 66 28 20 21 78 66 65 72   }.    if( !xfer
2542f 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74  CompatibleCollat
25430 69 6f 6e 28 70 44 65 73 74 2d 3e 61 43 6f 6c 5b  ion(pDest->aCol[
25431 69 5d 2e 7a 43 6f 6c 6c 2c 20 70 53 72 63 2d 3e  i].zColl, pSrc->
25432 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 29 20 29  aCol[i].zColl) )
25433 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
25434 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e  ;    /* Collatin
25435 67 20 73 65 71 75 65 6e 63 65 20 6d 75 73 74 20  g sequence must 
25436 62 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20 61  be the same on a
25437 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  ll columns */.  
25438 20 20 7d 0a 20 20 20 20 69 66 28 20 70 44 65 73    }.    if( pDes
25439 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75  t->aCol[i].notNu
2543a 6c 6c 20 26 26 20 21 70 53 72 63 2d 3e 61 43 6f  ll && !pSrc->aCo
2543b 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a  l[i].notNull ){.
2543c 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
2543d 20 20 20 2f 2a 20 74 61 62 32 20 6d 75 73 74 20     /* tab2 must 
2543e 62 65 20 4e 4f 54 20 4e 55 4c 4c 20 69 66 20 74  be NOT NULL if t
2543f 61 62 31 20 69 73 20 2a 2f 0a 20 20 20 20 7d 0a  ab1 is */.    }.
25440 20 20 7d 0a 20 20 66 6f 72 28 70 44 65 73 74 49    }.  for(pDestI
25441 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e 64 65 78  dx=pDest->pIndex
25442 3b 20 70 44 65 73 74 49 64 78 3b 20 70 44 65 73  ; pDestIdx; pDes
25443 74 49 64 78 3d 70 44 65 73 74 49 64 78 2d 3e 70  tIdx=pDestIdx->p
25444 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
25445 44 65 73 74 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  DestIdx->onError
25446 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20  !=OE_None ){.   
25447 20 20 20 64 65 73 74 48 61 73 55 6e 69 71 75 65     destHasUnique
25448 49 64 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  Idx = 1;.    }. 
25449 20 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d 70     for(pSrcIdx=p
2544a 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 70 53 72  Src->pIndex; pSr
2544b 63 49 64 78 3b 20 70 53 72 63 49 64 78 3d 70 53  cIdx; pSrcIdx=pS
2544c 72 63 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  rcIdx->pNext){. 
2544d 20 20 20 20 20 69 66 28 20 78 66 65 72 43 6f 6d       if( xferCom
2544e 70 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44 65  patibleIndex(pDe
2544f 73 74 49 64 78 2c 20 70 53 72 63 49 64 78 29 20  stIdx, pSrcIdx) 
25450 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
25451 20 20 20 69 66 28 20 70 53 72 63 49 64 78 3d 3d     if( pSrcIdx==
25452 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
25453 6e 20 30 3b 20 20 20 20 2f 2a 20 70 44 65 73 74  n 0;    /* pDest
25454 49 64 78 20 68 61 73 20 6e 6f 20 63 6f 72 72 65  Idx has no corre
25455 73 70 6f 6e 64 69 6e 67 20 69 6e 64 65 78 20 69  sponding index i
25456 6e 20 70 53 72 63 20 2a 2f 0a 20 20 20 20 7d 0a  n pSrc */.    }.
25457 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
25458 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20  TE_OMIT_CHECK.  
25459 69 66 28 20 70 44 65 73 74 2d 3e 70 43 68 65 63  if( pDest->pChec
2545a 6b 20 26 26 20 21 73 71 6c 69 74 65 33 45 78 70  k && !sqlite3Exp
2545b 72 43 6f 6d 70 61 72 65 28 70 53 72 63 2d 3e 70  rCompare(pSrc->p
2545c 43 68 65 63 6b 2c 20 70 44 65 73 74 2d 3e 70 43  Check, pDest->pC
2545d 68 65 63 6b 29 20 29 7b 0a 20 20 20 20 72 65 74  heck) ){.    ret
2545e 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 61 62 6c  urn 0;   /* Tabl
2545f 65 73 20 68 61 76 65 20 64 69 66 66 65 72 65 6e  es have differen
25460 74 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  t CHECK constrai
25461 6e 74 73 2e 20 20 54 69 63 6b 65 74 20 23 32 32  nts.  Ticket #22
25462 35 32 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66  52 */.  }.#endif
25463 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74  ..  /* If we get
25464 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65   this far, it me
25465 61 6e 73 20 65 69 74 68 65 72 3a 0a 20 20 2a 2a  ans either:.  **
25466 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20 57 65 20  .  **    *   We 
25467 63 61 6e 20 61 6c 77 61 79 73 20 64 6f 20 74 68  can always do th
25468 65 20 74 72 61 6e 73 66 65 72 20 69 66 20 74 68  e transfer if th
25469 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73  e table contains
2546a 20 61 6e 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   an.  **        
2546b 61 6e 20 69 6e 74 65 67 65 72 20 70 72 69 6d 61  an integer prima
2546c 72 79 20 6b 65 79 0a 20 20 2a 2a 0a 20 20 2a 2a  ry key.  **.  **
2546d 20 20 20 20 2a 20 20 20 57 65 20 63 61 6e 20 63      *   We can c
2546e 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 64 6f 20  onditionally do 
2546f 74 68 65 20 74 72 61 6e 73 66 65 72 20 69 66 20  the transfer if 
25470 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a  the destination.
25471 20 20 2a 2a 20 20 20 20 20 20 20 20 74 61 62 6c    **        tabl
25472 65 20 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a 2f  e is empty..  */
25473 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
25474 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 78 66  EST.  sqlite3_xf
25475 65 72 6f 70 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  eropt_count++;.#
25476 65 6e 64 69 66 0a 20 20 69 44 62 53 72 63 20 3d  endif.  iDbSrc =
25477 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
25478 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
25479 2c 20 70 53 72 63 2d 3e 70 53 63 68 65 6d 61 29  , pSrc->pSchema)
2547a 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ;.  v = sqlite3G
2547b 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2547c 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
2547d 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
2547e 2c 20 69 44 62 53 72 63 29 3b 0a 20 20 69 53 72  , iDbSrc);.  iSr
2547f 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  c = pParse->nTab
25480 2b 2b 3b 0a 20 20 69 44 65 73 74 20 3d 20 70 50  ++;.  iDest = pP
25481 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
25482 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 61 75 74  regAutoinc = aut
25483 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73 65  oIncBegin(pParse
25484 2c 20 69 44 62 44 65 73 74 2c 20 70 44 65 73 74  , iDbDest, pDest
25485 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e  );.  sqlite3Open
25486 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
25487 65 73 74 2c 20 69 44 62 44 65 73 74 2c 20 70 44  est, iDbDest, pD
25488 65 73 74 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  est, OP_OpenWrit
25489 65 29 3b 0a 20 20 69 66 28 20 28 70 44 65 73 74  e);.  if( (pDest
2548a 2d 3e 69 50 4b 65 79 3c 30 20 26 26 20 70 44 65  ->iPKey<0 && pDe
2548b 73 74 2d 3e 70 49 6e 64 65 78 21 3d 30 29 20 7c  st->pIndex!=0) |
2548c 7c 20 64 65 73 74 48 61 73 55 6e 69 71 75 65 49  | destHasUniqueI
2548d 64 78 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  dx ){.    /* If 
2548e 74 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 68 61  tables do not ha
2548f 76 65 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  ve an INTEGER PR
25490 49 4d 41 52 59 20 4b 45 59 20 61 6e 64 20 74 68  IMARY KEY and th
25491 65 72 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 69  ere.    ** are i
25492 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 6f 70  ndices to be cop
25493 69 65 64 20 61 6e 64 20 74 68 65 20 64 65 73 74  ied and the dest
25494 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 65  ination is not e
25495 6d 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20  mpty,.    ** we 
25496 68 61 76 65 20 74 6f 20 64 69 73 61 6c 6c 6f 77  have to disallow
25497 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70   the transfer op
25498 74 69 6d 69 7a 61 74 69 6f 6e 20 62 65 63 61 75  timization becau
25499 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68  se the.    ** th
2549a 65 20 72 6f 77 69 64 73 20 6d 69 67 68 74 20 63  e rowids might c
2549b 68 61 6e 67 65 20 77 68 69 63 68 20 77 69 6c 6c  hange which will
2549c 20 6d 65 73 73 20 75 70 20 69 6e 64 65 78 69 6e   mess up indexin
2549d 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  g..    **.    **
2549e 20 4f 72 20 69 66 20 74 68 65 20 64 65 73 74 69   Or if the desti
2549f 6e 61 74 69 6f 6e 20 68 61 73 20 61 20 55 4e 49  nation has a UNI
254a0 51 55 45 20 69 6e 64 65 78 20 61 6e 64 20 69 73  QUE index and is
254a1 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20 20 20   not empty,.    
254a2 2a 2a 20 77 65 20 61 6c 73 6f 20 64 69 73 61 6c  ** we also disal
254a3 6c 6f 77 20 74 68 65 20 74 72 61 6e 73 66 65 72  low the transfer
254a4 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 62 65   optimization be
254a5 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 0a  cause we cannot.
254a6 20 20 20 20 2a 2a 20 69 6e 73 75 72 65 20 74 68      ** insure th
254a7 61 74 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69  at all entries i
254a8 6e 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 44  n the union of D
254a9 45 53 54 20 61 6e 64 20 53 52 43 20 77 69 6c 6c  EST and SRC will
254aa 20 62 65 0a 20 20 20 20 2a 2a 20 75 6e 69 71 75   be.    ** uniqu
254ab 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64  e..    */.    ad
254ac 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
254ad 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
254ae 77 69 6e 64 2c 20 69 44 65 73 74 2c 20 30 29 3b  wind, iDest, 0);
254af 0a 20 20 20 20 65 6d 70 74 79 44 65 73 74 54 65  .    emptyDestTe
254b0 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  st = sqlite3Vdbe
254b1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
254b2 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  o, 0, 0);.    sq
254b3 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
254b4 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d  e(v, addr1);.  }
254b5 65 6c 73 65 7b 0a 20 20 20 20 65 6d 70 74 79 44  else{.    emptyD
254b6 65 73 74 54 65 73 74 20 3d 20 30 3b 0a 20 20 7d  estTest = 0;.  }
254b7 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  .  sqlite3OpenTa
254b8 62 6c 65 28 70 50 61 72 73 65 2c 20 69 53 72 63  ble(pParse, iSrc
254b9 2c 20 69 44 62 53 72 63 2c 20 70 53 72 63 2c 20  , iDbSrc, pSrc, 
254ba 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20  OP_OpenRead);.  
254bb 65 6d 70 74 79 53 72 63 54 65 73 74 20 3d 20 73  emptySrcTest = s
254bc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
254bd 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
254be 53 72 63 2c 20 30 29 3b 0a 20 20 72 65 67 44 61  Src, 0);.  regDa
254bf 74 61 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ta = sqlite3GetT
254c0 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
254c1 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c    regRowid = sql
254c2 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
254c3 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 70 44  Parse);.  if( pD
254c4 65 73 74 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b  est->iPKey>=0 ){
254c5 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
254c6 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
254c7 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 53 72 63  , OP_Rowid, iSrc
254c8 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
254c9 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74 65 33   addr2 = sqlite3
254ca 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
254cb 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 44 65 73  _NotExists, iDes
254cc 74 2c 20 30 2c 20 72 65 67 52 6f 77 69 64 29 3b  t, 0, regRowid);
254cd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
254ce 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c  AddOp4(v, OP_Hal
254cf 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  t, SQLITE_CONSTR
254d0 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 30  AINT, onError, 0
254d1 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
254d2 20 20 20 20 20 20 20 20 22 50 52 49 4d 41 52 59          "PRIMARY
254d3 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75 6e 69   KEY must be uni
254d4 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29  que", P4_STATIC)
254d5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
254d6 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
254d7 72 32 29 3b 0a 20 20 20 20 61 75 74 6f 49 6e 63  r2);.    autoInc
254d8 53 74 65 70 28 70 50 61 72 73 65 2c 20 72 65 67  Step(pParse, reg
254d9 41 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77 69  Autoinc, regRowi
254da 64 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  d);.  }else if( 
254db 70 44 65 73 74 2d 3e 70 49 6e 64 65 78 3d 3d 30  pDest->pIndex==0
254dc 20 29 7b 0a 20 20 20 20 61 64 64 72 31 20 3d 20   ){.    addr1 = 
254dd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
254de 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  2(v, OP_NewRowid
254df 2c 20 69 44 65 73 74 2c 20 72 65 67 52 6f 77 69  , iDest, regRowi
254e0 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  d);.  }else{.   
254e1 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33   addr1 = sqlite3
254e2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
254e3 5f 52 6f 77 69 64 2c 20 69 53 72 63 2c 20 72 65  _Rowid, iSrc, re
254e4 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 61 73 73  gRowid);.    ass
254e5 65 72 74 28 20 28 70 44 65 73 74 2d 3e 74 61 62  ert( (pDest->tab
254e6 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69  Flags & TF_Autoi
254e7 6e 63 72 65 6d 65 6e 74 29 3d 3d 30 20 29 3b 0a  ncrement)==0 );.
254e8 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
254e9 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
254ea 77 44 61 74 61 2c 20 69 53 72 63 2c 20 72 65 67  wData, iSrc, reg
254eb 44 61 74 61 29 3b 0a 20 20 73 71 6c 69 74 65 33  Data);.  sqlite3
254ec 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
254ed 5f 49 6e 73 65 72 74 2c 20 69 44 65 73 74 2c 20  _Insert, iDest, 
254ee 72 65 67 44 61 74 61 2c 20 72 65 67 52 6f 77 69  regData, regRowi
254ef 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  d);.  sqlite3Vdb
254f0 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46  eChangeP5(v, OPF
254f1 4c 41 47 5f 4e 43 48 41 4e 47 45 7c 4f 50 46 4c  LAG_NCHANGE|OPFL
254f2 41 47 5f 4c 41 53 54 52 4f 57 49 44 7c 4f 50 46  AG_LASTROWID|OPF
254f3 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 73  LAG_APPEND);.  s
254f4 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
254f5 50 34 28 76 2c 20 2d 31 2c 20 70 44 65 73 74 2d  P4(v, -1, pDest-
254f6 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 73 71  >zName, 0);.  sq
254f7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
254f8 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 53 72 63  v, OP_Next, iSrc
254f9 2c 20 61 64 64 72 31 29 3b 0a 20 20 61 75 74 6f  , addr1);.  auto
254fa 49 6e 63 45 6e 64 28 70 50 61 72 73 65 2c 20 69  IncEnd(pParse, i
254fb 44 62 44 65 73 74 2c 20 70 44 65 73 74 2c 20 72  DbDest, pDest, r
254fc 65 67 41 75 74 6f 69 6e 63 29 3b 0a 20 20 66 6f  egAutoinc);.  fo
254fd 72 28 70 44 65 73 74 49 64 78 3d 70 44 65 73 74  r(pDestIdx=pDest
254fe 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74 49  ->pIndex; pDestI
254ff 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44 65  dx; pDestIdx=pDe
25500 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  stIdx->pNext){. 
25501 20 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d 70     for(pSrcIdx=p
25502 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 41 4c 57  Src->pIndex; ALW
25503 41 59 53 28 70 53 72 63 49 64 78 29 3b 20 70 53  AYS(pSrcIdx); pS
25504 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70  rcIdx=pSrcIdx->p
25505 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
25506 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49   xferCompatibleI
25507 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c 20 70  ndex(pDestIdx, p
25508 53 72 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b  SrcIdx) ) break;
25509 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
2550a 74 28 20 70 53 72 63 49 64 78 20 29 3b 0a 20 20  t( pSrcIdx );.  
2550b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2550c 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
2550d 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 73   iSrc, 0);.    s
2550e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2550f 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 44  (v, OP_Close, iD
25510 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 70 4b 65  est, 0);.    pKe
25511 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
25512 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
25513 70 53 72 63 49 64 78 29 3b 0a 20 20 20 20 73 71  pSrcIdx);.    sq
25514 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
25515 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20  v, OP_OpenRead, 
25516 69 53 72 63 2c 20 70 53 72 63 49 64 78 2d 3e 74  iSrc, pSrcIdx->t
25517 6e 75 6d 2c 20 69 44 62 53 72 63 2c 0a 20 20 20  num, iDbSrc,.   
25518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25519 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20     (char*)pKey, 
2551a 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
2551b 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  FF);.    VdbeCom
2551c 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70  ment((v, "%s", p
2551d 53 72 63 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  SrcIdx->zName));
2551e 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c 69  .    pKey = sqli
2551f 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
25520 70 50 61 72 73 65 2c 20 70 44 65 73 74 49 64 78  pParse, pDestIdx
25521 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
25522 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
25523 70 65 6e 57 72 69 74 65 2c 20 69 44 65 73 74 2c  penWrite, iDest,
25524 20 70 44 65 73 74 49 64 78 2d 3e 74 6e 75 6d 2c   pDestIdx->tnum,
25525 20 69 44 62 44 65 73 74 2c 0a 20 20 20 20 20 20   iDbDest,.      
25526 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25527 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f  (char*)pKey, P4_
25528 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
25529 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
2552a 74 28 28 76 2c 20 22 25 73 22 2c 20 70 44 65 73  t((v, "%s", pDes
2552b 74 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20  tIdx->zName));. 
2552c 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74     addr1 = sqlit
2552d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2552e 4f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72 63 2c  OP_Rewind, iSrc,
2552f 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
25530 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25531 5f 52 6f 77 4b 65 79 2c 20 69 53 72 63 2c 20 72  _RowKey, iSrc, r
25532 65 67 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c  egData);.    sql
25533 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
25534 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
25535 69 44 65 73 74 2c 20 72 65 67 44 61 74 61 2c 20  iDest, regData, 
25536 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
25537 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25538 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64 64 72  Next, iSrc, addr
25539 31 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  1+1);.    sqlite
2553a 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2553b 20 61 64 64 72 31 29 3b 0a 20 20 7d 0a 20 20 73   addr1);.  }.  s
2553c 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
2553d 72 65 28 76 2c 20 65 6d 70 74 79 53 72 63 54 65  re(v, emptySrcTe
2553e 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  st);.  sqlite3Re
2553f 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
25540 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  rse, regRowid);.
25541 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
25542 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
25543 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71 6c 69  regData);.  sqli
25544 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
25545 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63 2c   OP_Close, iSrc,
25546 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   0);.  sqlite3Vd
25547 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
25548 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29 3b  lose, iDest, 0);
25549 0a 20 20 69 66 28 20 65 6d 70 74 79 44 65 73 74  .  if( emptyDest
2554a 54 65 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Test ){.    sqli
2554b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2554c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45   OP_Halt, SQLITE
2554d 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  _OK, 0);.    sql
2554e 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2554f 28 76 2c 20 65 6d 70 74 79 44 65 73 74 54 65 73  (v, emptyDestTes
25550 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
25551 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25552 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29  Close, iDest, 0)
25553 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
25554 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
25555 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  urn 1;.  }.}.#en
25556 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
25557 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a  IT_XFER_OPT */..
25558 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73  /* Make sure "is
25559 56 69 65 77 22 20 67 65 74 73 20 75 6e 64 65 66  View" gets undef
2555a 69 6e 65 64 20 69 6e 20 63 61 73 65 20 74 68 69  ined in case thi
2555b 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70  s file becomes p
2555c 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 6d  art of.** the am
2555d 61 6c 67 61 6d 61 74 69 6f 6e 20 2d 20 73 6f 20  algamation - so 
2555e 74 68 61 74 20 73 75 62 73 65 71 75 65 6e 74 20  that subsequent 
2555f 66 69 6c 65 73 20 64 6f 20 6e 6f 74 20 73 65 65  files do not see
25560 20 69 73 56 69 65 77 20 61 73 20 61 0a 2a 2a 20   isView as a.** 
25561 6d 61 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66  macro. */.#undef
25562 20 69 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a   isView../******
25563 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
25564 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  insert.c *******
25565 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25566 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25567 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
25568 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
25569 69 6c 65 20 6c 65 67 61 63 79 2e 63 20 2a 2a 2a  ile legacy.c ***
2556a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2556b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2556c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
2556d 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
2556e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
2556f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
25570 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
25571 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
25572 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
25573 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
25574 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
25575 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
25576 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
25577 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
25578 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
25579 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
2557a 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
2557b 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
2557c 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
2557d 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
2557e 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
2557f 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
25580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25581 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25582 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25583 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
25584 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72  ** Main file for
25585 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
25586 61 72 79 2e 20 20 54 68 65 20 72 6f 75 74 69 6e  ary.  The routin
25587 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0a  es in this file.
25588 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  ** implement the
25589 20 70 72 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65   programmer inte
2558a 72 66 61 63 65 20 74 6f 20 74 68 65 20 6c 69 62  rface to the lib
2558b 72 61 72 79 2e 20 20 52 6f 75 74 69 6e 65 73 20  rary.  Routines 
2558c 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65  in.** other file
2558d 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e  s are for intern
2558e 61 6c 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  al use by SQLite
2558f 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20   and should not 
25590 62 65 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 62  be.** accessed b
25591 79 20 75 73 65 72 73 20 6f 66 20 74 68 65 20 6c  y users of the l
25592 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49  ibrary..**.** $I
25593 64 3a 20 6c 65 67 61 63 79 2e 63 2c 76 20 31 2e  d: legacy.c,v 1.
25594 33 33 20 32 30 30 39 2f 30 35 2f 30 35 20 32 30  33 2009/05/05 20
25595 3a 30 32 3a 34 38 20 64 72 68 20 45 78 70 20 24  :02:48 drh Exp $
25596 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63  .*/.../*.** Exec
25597 75 74 65 20 53 51 4c 20 63 6f 64 65 2e 20 20 52  ute SQL code.  R
25598 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65  eturn one of the
25599 20 53 51 4c 49 54 45 5f 20 73 75 63 63 65 73 73   SQLITE_ success
2559a 2f 66 61 69 6c 75 72 65 0a 2a 2a 20 63 6f 64 65  /failure.** code
2559b 73 2e 20 20 41 6c 73 6f 20 77 72 69 74 65 20 61  s.  Also write a
2559c 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
2559d 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
2559e 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c  ined from.** mal
2559f 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a  loc() and make *
255a0 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20 74  pzErrMsg point t
255a1 6f 20 74 68 61 74 20 6d 65 73 73 61 67 65 2e 0a  o that message..
255a2 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
255a3 20 69 73 20 61 20 71 75 65 72 79 2c 20 74 68 65   is a query, the
255a4 6e 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  n for each row i
255a5 6e 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75  n the query resu
255a6 6c 74 0a 2a 2a 20 74 68 65 20 78 43 61 6c 6c 62  lt.** the xCallb
255a7 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ack() function i
255a8 73 20 63 61 6c 6c 65 64 2e 20 20 70 41 72 67 20  s called.  pArg 
255a9 62 65 63 6f 6d 65 73 20 74 68 65 20 66 69 72 73  becomes the firs
255aa 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  t.** argument to
255ab 20 78 43 61 6c 6c 62 61 63 6b 28 29 2e 20 20 49   xCallback().  I
255ac 66 20 78 43 61 6c 6c 62 61 63 6b 3d 4e 55 4c 4c  f xCallback=NULL
255ad 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c 62 61 63   then no callbac
255ae 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2c  k.** is invoked,
255af 20 65 76 65 6e 20 66 6f 72 20 71 75 65 72 69 65   even for querie
255b0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
255b1 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65   int sqlite3_exe
255b2 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  c(.  sqlite3 *db
255b3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
255b4 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
255b5 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 53 51   on which the SQ
255b6 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 20 20  L executes */.  
255b7 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
255b8 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
255b9 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65 78 65  he SQL to be exe
255ba 63 75 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  cuted */.  sqlit
255bb 65 33 5f 63 61 6c 6c 62 61 63 6b 20 78 43 61 6c  e3_callback xCal
255bc 6c 62 61 63 6b 2c 20 2f 2a 20 49 6e 76 6f 6b 65  lback, /* Invoke
255bd 20 74 68 69 73 20 63 61 6c 6c 62 61 63 6b 20 72   this callback r
255be 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 76 6f 69 64  outine */.  void
255bf 20 2a 70 41 72 67 2c 20 20 20 20 20 20 20 20 20   *pArg,         
255c0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
255c1 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 61   argument to xCa
255c2 6c 6c 62 61 63 6b 28 29 20 2a 2f 0a 20 20 63 68  llback() */.  ch
255c3 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
255c4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
255c5 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
255c6 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
255c7 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
255c8 4b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  K;         /* Re
255c9 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63  turn code */.  c
255ca 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
255cb 6f 76 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 61  over;      /* Ta
255cc 69 6c 20 6f 66 20 75 6e 70 72 6f 63 65 73 73 65  il of unprocesse
255cd 64 20 53 51 4c 20 2a 2f 0a 20 20 73 71 6c 69 74  d SQL */.  sqlit
255ce 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d  e3_stmt *pStmt =
255cf 20 30 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 75   0;    /* The cu
255d0 72 72 65 6e 74 20 53 51 4c 20 73 74 61 74 65 6d  rrent SQL statem
255d1 65 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ent */.  char **
255d2 61 7a 43 6f 6c 73 20 3d 20 30 3b 20 20 20 20 20  azCols = 0;     
255d3 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 20 6f 66       /* Names of
255d4 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
255d5 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 74 72 79 20  */.  int nRetry 
255d6 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
255d7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
255d8 74 72 79 20 61 74 74 65 6d 70 74 73 20 2a 2f 0a  try attempts */.
255d9 20 20 69 6e 74 20 63 61 6c 6c 62 61 63 6b 49 73    int callbackIs
255da 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Init;         /*
255db 20 54 72 75 65 20 69 66 20 63 61 6c 6c 62 61 63   True if callbac
255dc 6b 20 64 61 74 61 20 69 73 20 69 6e 69 74 69 61  k data is initia
255dd 6c 69 7a 65 64 20 2a 2f 0a 0a 20 20 69 66 28 20  lized */..  if( 
255de 7a 53 71 6c 3d 3d 30 20 29 20 7a 53 71 6c 20 3d  zSql==0 ) zSql =
255df 20 22 22 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f   "";..  sqlite3_
255e0 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
255e1 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
255e2 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
255e3 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 77 68 69 6c  E_OK, 0);.  whil
255e4 65 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  e( (rc==SQLITE_O
255e5 4b 20 7c 7c 20 28 72 63 3d 3d 53 51 4c 49 54 45  K || (rc==SQLITE
255e6 5f 53 43 48 45 4d 41 20 26 26 20 28 2b 2b 6e 52  _SCHEMA && (++nR
255e7 65 74 72 79 29 3c 32 29 29 20 26 26 20 7a 53 71  etry)<2)) && zSq
255e8 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20  l[0] ){.    int 
255e9 6e 43 6f 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a  nCol;.    char *
255ea 2a 61 7a 56 61 6c 73 20 3d 20 30 3b 0a 0a 20 20  *azVals = 0;..  
255eb 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20    pStmt = 0;.   
255ec 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
255ed 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
255ee 2d 31 2c 20 26 70 53 74 6d 74 2c 20 26 7a 4c 65  -1, &pStmt, &zLe
255ef 66 74 6f 76 65 72 29 3b 0a 20 20 20 20 61 73 73  ftover);.    ass
255f0 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
255f1 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29  OK || pStmt==0 )
255f2 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
255f3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
255f4 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
255f5 0a 20 20 20 20 69 66 28 20 21 70 53 74 6d 74 20  .    if( !pStmt 
255f6 29 7b 0a 20 20 20 20 20 20 2f 2a 20 74 68 69 73  ){.      /* this
255f7 20 68 61 70 70 65 6e 73 20 66 6f 72 20 61 20 63   happens for a c
255f8 6f 6d 6d 65 6e 74 20 6f 72 20 77 68 69 74 65 2d  omment or white-
255f9 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 7a  space */.      z
255fa 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b  Sql = zLeftover;
255fb 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
255fc 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 6c 6c  .    }..    call
255fd 62 61 63 6b 49 73 49 6e 69 74 20 3d 20 30 3b 0a  backIsInit = 0;.
255fe 20 20 20 20 6e 43 6f 6c 20 3d 20 73 71 6c 69 74      nCol = sqlit
255ff 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
25600 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 77 68 69  pStmt);..    whi
25601 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 20 20 69  le( 1 ){.      i
25602 6e 74 20 69 3b 0a 20 20 20 20 20 20 72 63 20 3d  nt i;.      rc =
25603 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53   sqlite3_step(pS
25604 74 6d 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  tmt);..      /* 
25605 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  Invoke the callb
25606 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  ack function if 
25607 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20  required */.    
25608 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63 6b 20    if( xCallback 
25609 26 26 20 28 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  && (SQLITE_ROW==
2560a 72 63 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20  rc || .         
2560b 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45 3d 3d 72   (SQLITE_DONE==r
2560c 63 20 26 26 20 21 63 61 6c 6c 62 61 63 6b 49 73  c && !callbackIs
2560d 49 6e 69 74 0a 20 20 20 20 20 20 20 20 20 20 20  Init.           
2560e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2560f 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  && db->flags&SQL
25610 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b  ITE_NullCallback
25611 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  )) ){.        if
25612 28 20 21 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69  ( !callbackIsIni
25613 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  t ){.          a
25614 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 44  zCols = sqlite3D
25615 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
25616 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 63 6f  2*nCol*sizeof(co
25617 6e 73 74 20 63 68 61 72 2a 29 20 2b 20 31 29 3b  nst char*) + 1);
25618 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61  .          if( a
25619 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20 20 20 20  zCols==0 ){.    
2561a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 65          goto exe
2561b 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 20  c_out;.         
2561c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72   }.          for
2561d 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
2561e 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
2561f 61 7a 43 6f 6c 73 5b 69 5d 20 3d 20 28 63 68 61  azCols[i] = (cha
25620 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75  r *)sqlite3_colu
25621 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c 20 69  mn_name(pStmt, i
25622 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  );.            /
25623 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  * sqlite3VdbeSet
25624 43 6f 6c 4e 61 6d 65 28 29 20 69 6e 73 74 61 6c  ColName() instal
25625 6c 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ls column names 
25626 61 73 20 55 54 46 38 0a 20 20 20 20 20 20 20 20  as UTF8.        
25627 20 20 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 73      ** strings s
25628 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61  o there is no wa
25629 79 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f  y for sqlite3_co
2562a 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20 74 6f 20 66  lumn_name() to f
2562b 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ail. */.        
2562c 20 20 20 20 61 73 73 65 72 74 28 20 61 7a 43 6f      assert( azCo
2562d 6c 73 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 20 20  ls[i]!=0 );.    
2562e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2562f 20 20 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74    callbackIsInit
25630 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
25631 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
25632 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
25633 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 20 3d          azVals =
25634 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a   &azCols[nCol];.
25635 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
25636 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
25637 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 56  .            azV
25638 61 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a  als[i] = (char *
25639 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
2563a 74 65 78 74 28 70 53 74 6d 74 2c 20 69 29 3b 0a  text(pStmt, i);.
2563b 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
2563c 21 61 7a 56 61 6c 73 5b 69 5d 20 26 26 20 73 71  !azVals[i] && sq
2563d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
2563e 65 28 70 53 74 6d 74 2c 20 69 29 21 3d 53 51 4c  e(pStmt, i)!=SQL
2563f 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
25640 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61            db->ma
25641 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
25642 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6f                go
25643 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20  to exec_out;.   
25644 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
25645 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
25646 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 43 61  .        if( xCa
25647 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 43 6f  llback(pArg, nCo
25648 6c 2c 20 61 7a 56 61 6c 73 2c 20 61 7a 43 6f 6c  l, azVals, azCol
25649 73 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  s) ){.          
2564a 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52  rc = SQLITE_ABOR
2564b 54 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  T;.          sql
2564c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
2564d 28 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b  ((Vdbe *)pStmt);
2564e 0a 20 20 20 20 20 20 20 20 20 20 70 53 74 6d 74  .          pStmt
2564f 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
25650 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
25651 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2c 20 30   SQLITE_ABORT, 0
25652 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
25653 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20  o exec_out;.    
25654 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20      }.      }.. 
25655 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
25656 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
25657 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
25658 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
25659 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 20 20  e *)pStmt);.    
2565a 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20      pStmt = 0;. 
2565b 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
2565c 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a  QLITE_SCHEMA ){.
2565d 20 20 20 20 20 20 20 20 20 20 6e 52 65 74 72 79            nRetry
2565e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
2565f 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72  zSql = zLeftover
25660 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  ;.          whil
25661 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  e( sqlite3Isspac
25662 65 28 7a 53 71 6c 5b 30 5d 29 20 29 20 7a 53 71  e(zSql[0]) ) zSq
25663 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  l++;.        }. 
25664 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
25665 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
25666 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
25667 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 20 20 20 20  b, azCols);.    
25668 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 20 20 7d 0a  azCols = 0;.  }.
25669 0a 65 78 65 63 5f 6f 75 74 3a 0a 20 20 69 66 28  .exec_out:.  if(
2566a 20 70 53 74 6d 74 20 29 20 73 71 6c 69 74 65 33   pStmt ) sqlite3
2566b 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64  VdbeFinalize((Vd
2566c 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 73  be *)pStmt);.  s
2566d 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2566e 20 61 7a 43 6f 6c 73 29 3b 0a 0a 20 20 72 63 20   azCols);..  rc 
2566f 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
25670 28 64 62 2c 20 72 63 29 3b 0a 20 20 69 66 28 20  (db, rc);.  if( 
25671 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc!=SQLITE_OK &&
25672 20 41 4c 57 41 59 53 28 72 63 3d 3d 73 71 6c 69   ALWAYS(rc==sqli
25673 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29 29  te3_errcode(db))
25674 20 26 26 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a   && pzErrMsg ){.
25675 20 20 20 20 69 6e 74 20 6e 45 72 72 4d 73 67 20      int nErrMsg 
25676 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 53 74 72  = 1 + sqlite3Str
25677 6c 65 6e 33 30 28 73 71 6c 69 74 65 33 5f 65 72  len30(sqlite3_er
25678 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20 20 2a  rmsg(db));.    *
25679 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
2567a 65 33 4d 61 6c 6c 6f 63 28 6e 45 72 72 4d 73 67  e3Malloc(nErrMsg
2567b 29 3b 0a 20 20 20 20 69 66 28 20 2a 70 7a 45 72  );.    if( *pzEr
2567c 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 6d 65  rMsg ){.      me
2567d 6d 63 70 79 28 2a 70 7a 45 72 72 4d 73 67 2c 20  mcpy(*pzErrMsg, 
2567e 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
2567f 62 29 2c 20 6e 45 72 72 4d 73 67 29 3b 0a 20 20  b), nErrMsg);.  
25680 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
25681 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
25682 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  *pzErrMsg = 0;. 
25683 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72   }..  assert( (r
25684 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d  c&db->errMask)==
25685 72 63 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  rc );.  sqlite3_
25686 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
25687 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
25688 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a   rc;.}../*******
25689 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c  ******* End of l
2568a 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  egacy.c ********
2568b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2568c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2568d 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2568e 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2568f 6c 65 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a  le loadext.c ***
25690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25691 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25692 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
25693 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20  06 June 7.**.** 
25694 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
25695 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
25696 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
25697 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
25698 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
25699 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
2569a 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
2569b 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
2569c 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
2569d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
2569e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
2569f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
256a0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
256a1 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
256a2 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
256a3 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
256a4 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
256a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256a9 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
256aa 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
256ab 6f 64 65 20 75 73 65 64 20 74 6f 20 64 79 6e 61  ode used to dyna
256ac 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 20 65 78 74  mically load ext
256ad 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20  ensions into.** 
256ae 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
256af 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c  ry..**.** $Id: l
256b0 6f 61 64 65 78 74 2e 63 2c 76 20 31 2e 35 38 20  oadext.c,v 1.58 
256b1 32 30 30 39 2f 30 31 2f 32 30 20 31 36 3a 35 33  2009/01/20 16:53
256b2 3a 34 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :40 danielk1977 
256b3 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
256b4 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20  f SQLITE_CORE.  
256b5 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
256b6 4f 52 45 20 31 20 20 2f 2a 20 44 69 73 61 62 6c  ORE 1  /* Disabl
256b7 65 20 74 68 65 20 41 50 49 20 72 65 64 65 66 69  e the API redefi
256b8 6e 69 74 69 6f 6e 20 69 6e 20 73 71 6c 69 74 65  nition in sqlite
256b9 33 65 78 74 2e 68 20 2a 2f 0a 23 65 6e 64 69 66  3ext.h */.#endif
256ba 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
256bb 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33   Include sqlite3
256bc 65 78 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ext.h in the mid
256bd 64 6c 65 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63  dle of loadext.c
256be 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f   **************/
256bf 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
256c0 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69   Begin file sqli
256c1 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  te3ext.h *******
256c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
256c4 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65  ./*.** 2006 June
256c5 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   7.**.** The aut
256c6 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
256c7 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
256c8 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
256c9 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
256ca 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
256cb 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
256cc 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
256cd 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
256ce 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
256cf 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
256d0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
256d1 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
256d2 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
256d3 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
256d4 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
256d5 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
256d6 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
256d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256db 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
256dc 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68   file defines th
256dd 65 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  e SQLite interfa
256de 63 65 20 66 6f 72 20 75 73 65 20 62 79 0a 2a 2a  ce for use by.**
256df 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 69 65   shared librarie
256e0 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 62  s that want to b
256e1 65 20 69 6d 70 6f 72 74 65 64 20 61 73 20 65 78  e imported as ex
256e2 74 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a  tensions into.**
256e3 20 61 6e 20 53 51 4c 69 74 65 20 69 6e 73 74 61   an SQLite insta
256e4 6e 63 65 2e 20 20 53 68 61 72 65 64 20 6c 69 62  nce.  Shared lib
256e5 72 61 72 69 65 73 20 74 68 61 74 20 69 6e 74 65  raries that inte
256e6 6e 64 20 74 6f 20 62 65 20 6c 6f 61 64 65 64 0a  nd to be loaded.
256e7 2a 2a 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73  ** as extensions
256e8 20 62 79 20 53 51 4c 69 74 65 20 73 68 6f 75 6c   by SQLite shoul
256e9 64 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20  d #include this 
256ea 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20  file instead of 
256eb 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a  .** sqlite3.h..*
256ec 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73  *.** @(#) $Id: s
256ed 71 6c 69 74 65 33 65 78 74 2e 68 2c 76 20 31 2e  qlite3ext.h,v 1.
256ee 32 35 20 32 30 30 38 2f 31 30 2f 31 32 20 30 30  25 2008/10/12 00
256ef 3a 32 37 3a 35 34 20 73 68 61 6e 65 20 45 78 70  :27:54 shane Exp
256f0 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53   $.*/.#ifndef _S
256f1 51 4c 49 54 45 33 45 58 54 5f 48 5f 0a 23 64 65  QLITE3EXT_H_.#de
256f2 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 45 58 54  fine _SQLITE3EXT
256f3 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20 73 74 72  _H_..typedef str
256f4 75 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  uct sqlite3_api_
256f5 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33  routines sqlite3
256f6 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 3b 0a 0a  _api_routines;..
256f7 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
256f8 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f  ing structure ho
256f9 6c 64 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  lds pointers to 
256fa 61 6c 6c 20 6f 66 20 74 68 65 20 53 51 4c 69 74  all of the SQLit
256fb 65 20 41 50 49 0a 2a 2a 20 72 6f 75 74 69 6e 65  e API.** routine
256fc 73 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47  s..**.** WARNING
256fd 3a 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d  :  In order to m
256fe 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64  aintain backward
256ff 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c  s compatibility,
25700 20 61 64 64 20 6e 65 77 0a 2a 2a 20 69 6e 74 65   add new.** inte
25701 72 66 61 63 65 73 20 74 6f 20 74 68 65 20 65 6e  rfaces to the en
25702 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  d of this struct
25703 75 72 65 20 6f 6e 6c 79 2e 20 20 49 66 20 79 6f  ure only.  If yo
25704 75 20 69 6e 73 65 72 74 20 6e 65 77 0a 2a 2a 20  u insert new.** 
25705 69 6e 74 65 72 66 61 63 65 73 20 69 6e 20 74 68  interfaces in th
25706 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 69 73  e middle of this
25707 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e   structure, then
25708 20 6f 6c 64 65 72 20 64 69 66 66 65 72 65 6e 74   older different
25709 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  .** versions of 
2570a 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20  SQLite will not 
2570b 62 65 20 61 62 6c 65 20 74 6f 20 6c 6f 61 64 20  be able to load 
2570c 65 61 63 68 20 6f 74 68 65 72 73 27 20 73 68 61  each others' sha
2570d 72 65 64 0a 2a 2a 20 6c 69 62 72 61 72 69 65 73  red.** libraries
2570e 21 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  !.*/.struct sqli
2570f 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
25710 20 7b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 61 67   {.  void * (*ag
25711 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 29  gregate_context)
25712 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
25713 2a 2c 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a 20  *,int nBytes);. 
25714 20 69 6e 74 20 20 28 2a 61 67 67 72 65 67 61 74   int  (*aggregat
25715 65 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33  e_count)(sqlite3
25716 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e  _context*);.  in
25717 74 20 20 28 2a 62 69 6e 64 5f 62 6c 6f 62 29 28  t  (*bind_blob)(
25718 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
25719 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
2571a 74 20 6e 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  t n,void(*)(void
2571b 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  *));.  int  (*bi
2571c 6e 64 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  nd_double)(sqlit
2571d 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 64 6f 75  e3_stmt*,int,dou
2571e 62 6c 65 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  ble);.  int  (*b
2571f 69 6e 64 5f 69 6e 74 29 28 73 71 6c 69 74 65 33  ind_int)(sqlite3
25720 5f 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b  _stmt*,int,int);
25721 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69  .  int  (*bind_i
25722 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73 74  nt64)(sqlite3_st
25723 6d 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 5f 69  mt*,int,sqlite_i
25724 6e 74 36 34 29 3b 0a 20 20 69 6e 74 20 20 28 2a  nt64);.  int  (*
25725 62 69 6e 64 5f 6e 75 6c 6c 29 28 73 71 6c 69 74  bind_null)(sqlit
25726 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
25727 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72   int  (*bind_par
25728 61 6d 65 74 65 72 5f 63 6f 75 6e 74 29 28 73 71  ameter_count)(sq
25729 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20  lite3_stmt*);.  
2572a 69 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72 61  int  (*bind_para
2572b 6d 65 74 65 72 5f 69 6e 64 65 78 29 28 73 71 6c  meter_index)(sql
2572c 69 74 65 33 5f 73 74 6d 74 2a 2c 63 6f 6e 73 74  ite3_stmt*,const
2572d 20 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a 20 20   char*zName);.  
2572e 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 62  const char * (*b
2572f 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
25730 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  me)(sqlite3_stmt
25731 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28  *,int);.  int  (
25732 2a 62 69 6e 64 5f 74 65 78 74 29 28 73 71 6c 69  *bind_text)(sqli
25733 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f  te3_stmt*,int,co
25734 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 20 6e 2c  nst char*,int n,
25735 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
25736 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74  .  int  (*bind_t
25737 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ext16)(sqlite3_s
25738 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  tmt*,int,const v
25739 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
2573a 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20  (void*));.  int 
2573b 20 28 2a 62 69 6e 64 5f 76 61 6c 75 65 29 28 73   (*bind_value)(s
2573c 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2573d 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76  ,const sqlite3_v
2573e 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28  alue*);.  int  (
2573f 2a 62 75 73 79 5f 68 61 6e 64 6c 65 72 29 28 73  *busy_handler)(s
25740 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76  qlite3*,int(*)(v
25741 6f 69 64 2a 2c 69 6e 74 29 2c 76 6f 69 64 2a 29  oid*,int),void*)
25742 3b 0a 20 20 69 6e 74 20 20 28 2a 62 75 73 79 5f  ;.  int  (*busy_
25743 74 69 6d 65 6f 75 74 29 28 73 71 6c 69 74 65 33  timeout)(sqlite3
25744 2a 2c 69 6e 74 20 6d 73 29 3b 0a 20 20 69 6e 74  *,int ms);.  int
25745 20 20 28 2a 63 68 61 6e 67 65 73 29 28 73 71 6c    (*changes)(sql
25746 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28  ite3*);.  int  (
25747 2a 63 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 2a  *close)(sqlite3*
25748 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 6c  );.  int  (*coll
25749 61 74 69 6f 6e 5f 6e 65 65 64 65 64 29 28 73 71  ation_needed)(sq
2574a 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69  lite3*,void*,voi
2574b 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  d(*)(void*,sqlit
2574c 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
2574d 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 3b 0a  ,const char*));.
2574e 20 20 69 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69    int  (*collati
2574f 6f 6e 5f 6e 65 65 64 65 64 31 36 29 28 73 71 6c  on_needed16)(sql
25750 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64  ite3*,void*,void
25751 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
25752 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
25753 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20  const void*));. 
25754 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
25755 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 29 28 73 71 6c  column_blob)(sql
25756 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
25757 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  Col);.  int  (*c
25758 6f 6c 75 6d 6e 5f 62 79 74 65 73 29 28 73 71 6c  olumn_bytes)(sql
25759 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
2575a 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  Col);.  int  (*c
2575b 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 29 28 73  olumn_bytes16)(s
2575c 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2575d 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28   iCol);.  int  (
2575e 2a 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 29 28 73  *column_count)(s
2575f 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d  qlite3_stmt*pStm
25760 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t);.  const char
25761 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61 74 61   * (*column_data
25762 62 61 73 65 5f 6e 61 6d 65 29 28 73 71 6c 69 74  base_name)(sqlit
25763 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
25764 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
25765 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
25766 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f  name16)(sqlite3_
25767 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f  stmt*,int);.  co
25768 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  nst char * (*col
25769 75 6d 6e 5f 64 65 63 6c 74 79 70 65 29 28 73 71  umn_decltype)(sq
2576a 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
2576b 69 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  i);.  const void
2576c 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c   * (*column_decl
2576d 74 79 70 65 31 36 29 28 73 71 6c 69 74 65 33 5f  type16)(sqlite3_
2576e 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 64 6f  stmt*,int);.  do
2576f 75 62 6c 65 20 20 28 2a 63 6f 6c 75 6d 6e 5f 64  uble  (*column_d
25770 6f 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73  ouble)(sqlite3_s
25771 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
25772 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
25773 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  int)(sqlite3_stm
25774 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
25775 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28 2a  sqlite_int64  (*
25776 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 29 28 73 71  column_int64)(sq
25777 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
25778 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73 74 20 63  iCol);.  const c
25779 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6e  har * (*column_n
2577a 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
2577b 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
2577c 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
2577d 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33  _name16)(sqlite3
2577e 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
2577f 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f  onst char * (*co
25780 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
25781 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
25782 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  int);.  const vo
25783 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72  id * (*column_or
25784 69 67 69 6e 5f 6e 61 6d 65 31 36 29 28 73 71 6c  igin_name16)(sql
25785 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
25786 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
25787 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  (*column_table_n
25788 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
25789 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
2578a 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
2578b 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 29 28 73  _table_name16)(s
2578c 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2578d 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  );.  const unsig
2578e 6e 65 64 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  ned char * (*col
2578f 75 6d 6e 5f 74 65 78 74 29 28 73 71 6c 69 74 65  umn_text)(sqlite
25790 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
25791 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
25792 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  * (*column_text1
25793 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
25794 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e  ,int iCol);.  in
25795 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 74 79 70 65  t  (*column_type
25796 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
25797 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 73 71 6c  int iCol);.  sql
25798 69 74 65 33 5f 76 61 6c 75 65 2a 20 28 2a 63 6f  ite3_value* (*co
25799 6c 75 6d 6e 5f 76 61 6c 75 65 29 28 73 71 6c 69  lumn_value)(sqli
2579a 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
2579b 6f 6c 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  ol);.  void * (*
2579c 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 29 28 73 71 6c  commit_hook)(sql
2579d 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69  ite3*,int(*)(voi
2579e 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e  d*),void*);.  in
2579f 74 20 20 28 2a 63 6f 6d 70 6c 65 74 65 29 28 63  t  (*complete)(c
257a0 6f 6e 73 74 20 63 68 61 72 2a 73 71 6c 29 3b 0a  onst char*sql);.
257a1 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70 6c 65 74    int  (*complet
257a2 65 31 36 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  e16)(const void*
257a3 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  sql);.  int  (*c
257a4 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 29  reate_collation)
257a5 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
257a6 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c  char*,int,void*,
257a7 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
257a8 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
257a9 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a  ,const void*));.
257aa 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f    int  (*create_
257ab 63 6f 6c 6c 61 74 69 6f 6e 31 36 29 28 73 71 6c  collation16)(sql
257ac 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  ite3*,const void
257ad 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28  *,int,void*,int(
257ae 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  *)(void*,int,con
257af 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
257b0 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e  st void*));.  in
257b1 74 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63  t  (*create_func
257b2 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63  tion)(sqlite3*,c
257b3 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69  onst char*,int,i
257b4 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a  nt,void*,void (*
257b5 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
257b6 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
257b7 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69  te3_value**),voi
257b8 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
257b9 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
257ba 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
257bb 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28  ,void (*xFinal)(
257bc 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
257bd 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65  ));.  int  (*cre
257be 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 29 28  ate_function16)(
257bf 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76  sqlite3*,const v
257c0 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69  oid*,int,int,voi
257c1 64 2a 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  d*,void (*xFunc)
257c2 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
257c3 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
257c4 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53  lue**),void (*xS
257c5 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
257c6 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
257c7 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20  3_value**),void 
257c8 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65  (*xFinal)(sqlite
257c9 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20  3_context*));.  
257ca 69 6e 74 20 28 2a 63 72 65 61 74 65 5f 6d 6f 64  int (*create_mod
257cb 75 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  ule)(sqlite3*,co
257cc 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
257cd 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c  sqlite3_module*,
257ce 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28  void*);.  int  (
257cf 2a 64 61 74 61 5f 63 6f 75 6e 74 29 28 73 71 6c  *data_count)(sql
257d0 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29  ite3_stmt*pStmt)
257d1 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 20 28 2a  ;.  sqlite3 * (*
257d2 64 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74  db_handle)(sqlit
257d3 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74  e3_stmt*);.  int
257d4 20 28 2a 64 65 63 6c 61 72 65 5f 76 74 61 62 29   (*declare_vtab)
257d5 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
257d6 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 20 28  char*);.  int  (
257d7 2a 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  *enable_shared_c
257d8 61 63 68 65 29 28 69 6e 74 29 3b 0a 20 20 69 6e  ache)(int);.  in
257d9 74 20 20 28 2a 65 72 72 63 6f 64 65 29 28 73 71  t  (*errcode)(sq
257da 6c 69 74 65 33 2a 64 62 29 3b 0a 20 20 63 6f 6e  lite3*db);.  con
257db 73 74 20 63 68 61 72 20 2a 20 28 2a 65 72 72 6d  st char * (*errm
257dc 73 67 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  sg)(sqlite3*);. 
257dd 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
257de 65 72 72 6d 73 67 31 36 29 28 73 71 6c 69 74 65  errmsg16)(sqlite
257df 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 78  3*);.  int  (*ex
257e0 65 63 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ec)(sqlite3*,con
257e1 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33  st char*,sqlite3
257e2 5f 63 61 6c 6c 62 61 63 6b 2c 76 6f 69 64 2a 2c  _callback,void*,
257e3 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20  char**);.  int  
257e4 28 2a 65 78 70 69 72 65 64 29 28 73 71 6c 69 74  (*expired)(sqlit
257e5 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74  e3_stmt*);.  int
257e6 20 20 28 2a 66 69 6e 61 6c 69 7a 65 29 28 73 71    (*finalize)(sq
257e7 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74  lite3_stmt*pStmt
257e8 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65  );.  void  (*fre
257e9 65 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  e)(void*);.  voi
257ea 64 20 20 28 2a 66 72 65 65 5f 74 61 62 6c 65 29  d  (*free_table)
257eb 28 63 68 61 72 2a 2a 72 65 73 75 6c 74 29 3b 0a  (char**result);.
257ec 20 20 69 6e 74 20 20 28 2a 67 65 74 5f 61 75 74    int  (*get_aut
257ed 6f 63 6f 6d 6d 69 74 29 28 73 71 6c 69 74 65 33  ocommit)(sqlite3
257ee 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 67  *);.  void * (*g
257ef 65 74 5f 61 75 78 64 61 74 61 29 28 73 71 6c 69  et_auxdata)(sqli
257f0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
257f1 29 3b 0a 20 20 69 6e 74 20 20 28 2a 67 65 74 5f  );.  int  (*get_
257f2 74 61 62 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c  table)(sqlite3*,
257f3 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61 72  const char*,char
257f4 2a 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 63 68  ***,int*,int*,ch
257f5 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ar**);.  int  (*
257f6 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 29 28  global_recover)(
257f7 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 20 28  void);.  void  (
257f8 2a 69 6e 74 65 72 72 75 70 74 78 29 28 73 71 6c  *interruptx)(sql
257f9 69 74 65 33 2a 29 3b 0a 20 20 73 71 6c 69 74 65  ite3*);.  sqlite
257fa 5f 69 6e 74 36 34 20 20 28 2a 6c 61 73 74 5f 69  _int64  (*last_i
257fb 6e 73 65 72 74 5f 72 6f 77 69 64 29 28 73 71 6c  nsert_rowid)(sql
257fc 69 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  ite3*);.  const 
257fd 63 68 61 72 20 2a 20 28 2a 6c 69 62 76 65 72 73  char * (*libvers
257fe 69 6f 6e 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e  ion)(void);.  in
257ff 74 20 20 28 2a 6c 69 62 76 65 72 73 69 6f 6e 5f  t  (*libversion_
25800 6e 75 6d 62 65 72 29 28 76 6f 69 64 29 3b 0a 20  number)(void);. 
25801 20 76 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 29   void *(*malloc)
25802 28 69 6e 74 29 3b 0a 20 20 63 68 61 72 20 2a 20  (int);.  char * 
25803 28 2a 6d 70 72 69 6e 74 66 29 28 63 6f 6e 73 74  (*mprintf)(const
25804 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20 20 69   char*,...);.  i
25805 6e 74 20 20 28 2a 6f 70 65 6e 29 28 63 6f 6e 73  nt  (*open)(cons
25806 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a  t char*,sqlite3*
25807 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65  *);.  int  (*ope
25808 6e 31 36 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  n16)(const void*
25809 2c 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69  ,sqlite3**);.  i
2580a 6e 74 20 20 28 2a 70 72 65 70 61 72 65 29 28 73  nt  (*prepare)(s
2580b 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
2580c 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  ar*,int,sqlite3_
2580d 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61  stmt**,const cha
2580e 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70  r**);.  int  (*p
2580f 72 65 70 61 72 65 31 36 29 28 73 71 6c 69 74 65  repare16)(sqlite
25810 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  3*,const void*,i
25811 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt,sqlite3_stmt*
25812 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a 29 3b  *,const void**);
25813 0a 20 20 76 6f 69 64 20 2a 20 28 2a 70 72 6f 66  .  void * (*prof
25814 69 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  ile)(sqlite3*,vo
25815 69 64 28 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  id(*)(void*,cons
25816 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75  t char*,sqlite_u
25817 69 6e 74 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20  int64),void*);. 
25818 20 76 6f 69 64 20 20 28 2a 70 72 6f 67 72 65 73   void  (*progres
25819 73 5f 68 61 6e 64 6c 65 72 29 28 73 71 6c 69 74  s_handler)(sqlit
2581a 65 33 2a 2c 69 6e 74 2c 69 6e 74 28 2a 29 28 76  e3*,int,int(*)(v
2581b 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  oid*),void*);.  
2581c 76 6f 69 64 20 2a 28 2a 72 65 61 6c 6c 6f 63 29  void *(*realloc)
2581d 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 69  (void*,int);.  i
2581e 6e 74 20 20 28 2a 72 65 73 65 74 29 28 73 71 6c  nt  (*reset)(sql
2581f 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29  ite3_stmt*pStmt)
25820 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
25821 6c 74 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  lt_blob)(sqlite3
25822 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20  _context*,const 
25823 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a  void*,int,void(*
25824 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69  )(void*));.  voi
25825 64 20 20 28 2a 72 65 73 75 6c 74 5f 64 6f 75 62  d  (*result_doub
25826 6c 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  le)(sqlite3_cont
25827 65 78 74 2a 2c 64 6f 75 62 6c 65 29 3b 0a 20 20  ext*,double);.  
25828 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 65  void  (*result_e
25829 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 63 6f  rror)(sqlite3_co
2582a 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61  ntext*,const cha
2582b 72 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  r*,int);.  void 
2582c 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 31   (*result_error1
2582d 36 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  6)(sqlite3_conte
2582e 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  xt*,const void*,
2582f 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  int);.  void  (*
25830 72 65 73 75 6c 74 5f 69 6e 74 29 28 73 71 6c 69  result_int)(sqli
25831 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
25832 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
25833 75 6c 74 5f 69 6e 74 36 34 29 28 73 71 6c 69 74  ult_int64)(sqlit
25834 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69  e3_context*,sqli
25835 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69  te_int64);.  voi
25836 64 20 20 28 2a 72 65 73 75 6c 74 5f 6e 75 6c 6c  d  (*result_null
25837 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
25838 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  t*);.  void  (*r
25839 65 73 75 6c 74 5f 74 65 78 74 29 28 73 71 6c 69  esult_text)(sqli
2583a 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e  te3_context*,con
2583b 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69  st char*,int,voi
2583c 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
2583d 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74  void  (*result_t
2583e 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 63  ext16)(sqlite3_c
2583f 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f  ontext*,const vo
25840 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28  id*,int,void(*)(
25841 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20  void*));.  void 
25842 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 31 36   (*result_text16
25843 62 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  be)(sqlite3_cont
25844 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ext*,const void*
25845 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69  ,int,void(*)(voi
25846 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  d*));.  void  (*
25847 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 29  result_text16le)
25848 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
25849 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
2584a 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
2584b 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
2584c 75 6c 74 5f 76 61 6c 75 65 29 28 73 71 6c 69 74  ult_value)(sqlit
2584d 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69  e3_context*,sqli
2584e 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 76  te3_value*);.  v
2584f 6f 69 64 20 2a 20 28 2a 72 6f 6c 6c 62 61 63 6b  oid * (*rollback
25850 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c  _hook)(sqlite3*,
25851 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 2c 76  void(*)(void*),v
25852 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  oid*);.  int  (*
25853 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 29 28  set_authorizer)(
25854 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28  sqlite3*,int(*)(
25855 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
25856 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
25857 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
25858 6e 73 74 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a  nst char*),void*
25859 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 73 65 74  );.  void  (*set
2585a 5f 61 75 78 64 61 74 61 29 28 73 71 6c 69 74 65  _auxdata)(sqlite
2585b 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 76  3_context*,int,v
2585c 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 29 28 76 6f  oid*,void (*)(vo
2585d 69 64 2a 29 29 3b 0a 20 20 63 68 61 72 20 2a 20  id*));.  char * 
2585e 28 2a 73 6e 70 72 69 6e 74 66 29 28 69 6e 74 2c  (*snprintf)(int,
2585f 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
25860 2a 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28  *,...);.  int  (
25861 2a 73 74 65 70 29 28 73 71 6c 69 74 65 33 5f 73  *step)(sqlite3_s
25862 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  tmt*);.  int  (*
25863 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
25864 61 64 61 74 61 29 28 73 71 6c 69 74 65 33 2a 2c  adata)(sqlite3*,
25865 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
25866 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
25867 61 72 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a  ar*,char const**
25868 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a 2c 69 6e  ,char const**,in
25869 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 20  t*,int*,int*);. 
2586a 20 76 6f 69 64 20 20 28 2a 74 68 72 65 61 64 5f   void  (*thread_
2586b 63 6c 65 61 6e 75 70 29 28 76 6f 69 64 29 3b 0a  cleanup)(void);.
2586c 20 20 69 6e 74 20 20 28 2a 74 6f 74 61 6c 5f 63    int  (*total_c
2586d 68 61 6e 67 65 73 29 28 73 71 6c 69 74 65 33 2a  hanges)(sqlite3*
2586e 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 74 72  );.  void * (*tr
2586f 61 63 65 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  ace)(sqlite3*,vo
25870 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64  id(*xTrace)(void
25871 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 76  *,const char*),v
25872 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  oid*);.  int  (*
25873 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67  transfer_binding
25874 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  s)(sqlite3_stmt*
25875 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  ,sqlite3_stmt*);
25876 0a 20 20 76 6f 69 64 20 2a 20 28 2a 75 70 64 61  .  void * (*upda
25877 74 65 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33  te_hook)(sqlite3
25878 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  *,void(*)(void*,
25879 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 2a  int ,char const*
2587a 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2c 73 71 6c  ,char const*,sql
2587b 69 74 65 5f 69 6e 74 36 34 29 2c 76 6f 69 64 2a  ite_int64),void*
2587c 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 75 73  );.  void * (*us
2587d 65 72 5f 64 61 74 61 29 28 73 71 6c 69 74 65 33  er_data)(sqlite3
2587e 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 63 6f  _context*);.  co
2587f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c  nst void * (*val
25880 75 65 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  ue_blob)(sqlite3
25881 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
25882 20 28 2a 76 61 6c 75 65 5f 62 79 74 65 73 29 28   (*value_bytes)(
25883 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
25884 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
25885 62 79 74 65 73 31 36 29 28 73 71 6c 69 74 65 33  bytes16)(sqlite3
25886 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 64 6f 75 62  _value*);.  doub
25887 6c 65 20 20 28 2a 76 61 6c 75 65 5f 64 6f 75 62  le  (*value_doub
25888 6c 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  le)(sqlite3_valu
25889 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61  e*);.  int  (*va
2588a 6c 75 65 5f 69 6e 74 29 28 73 71 6c 69 74 65 33  lue_int)(sqlite3
2588b 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 73 71 6c 69  _value*);.  sqli
2588c 74 65 5f 69 6e 74 36 34 20 20 28 2a 76 61 6c 75  te_int64  (*valu
2588d 65 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33  e_int64)(sqlite3
2588e 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
2588f 20 28 2a 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63   (*value_numeric
25890 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 76  _type)(sqlite3_v
25891 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  alue*);.  const 
25892 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 20  unsigned char * 
25893 28 2a 76 61 6c 75 65 5f 74 65 78 74 29 28 73 71  (*value_text)(sq
25894 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
25895 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
25896 76 61 6c 75 65 5f 74 65 78 74 31 36 29 28 73 71  value_text16)(sq
25897 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
25898 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
25899 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 29 28  value_text16be)(
2589a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2589b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
2589c 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  (*value_text16le
2589d 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
2589e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75  );.  int  (*valu
2589f 65 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f  e_type)(sqlite3_
258a0 76 61 6c 75 65 2a 29 3b 0a 20 20 63 68 61 72 20  value*);.  char 
258a1 2a 28 2a 76 6d 70 72 69 6e 74 66 29 28 63 6f 6e  *(*vmprintf)(con
258a2 73 74 20 63 68 61 72 2a 2c 76 61 5f 6c 69 73 74  st char*,va_list
258a3 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 3f 3f  );.  /* Added ??
258a4 3f 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 6f 76 65  ? */.  int (*ove
258a5 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 29 28  rload_function)(
258a6 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
258a7 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c  char *zFuncName,
258a8 20 69 6e 74 20 6e 41 72 67 29 3b 0a 20 20 2f 2a   int nArg);.  /*
258a9 20 41 64 64 65 64 20 62 79 20 33 2e 33 2e 31 33   Added by 3.3.13
258aa 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 70 72 65 70   */.  int (*prep
258ab 61 72 65 5f 76 32 29 28 73 71 6c 69 74 65 33 2a  are_v2)(sqlite3*
258ac 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
258ad 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c  ,sqlite3_stmt**,
258ae 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20  const char**);. 
258af 20 69 6e 74 20 28 2a 70 72 65 70 61 72 65 31 36   int (*prepare16
258b0 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  _v2)(sqlite3*,co
258b1 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71  nst void*,int,sq
258b2 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e  lite3_stmt**,con
258b3 73 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20 69 6e  st void**);.  in
258b4 74 20 28 2a 63 6c 65 61 72 5f 62 69 6e 64 69 6e  t (*clear_bindin
258b5 67 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  gs)(sqlite3_stmt
258b6 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62  *);.  /* Added b
258b7 79 20 33 2e 34 2e 31 20 2a 2f 0a 20 20 69 6e 74  y 3.4.1 */.  int
258b8 20 28 2a 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65   (*create_module
258b9 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  _v2)(sqlite3*,co
258ba 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
258bb 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c  sqlite3_module*,
258bc 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78 44 65  void*,void (*xDe
258bd 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 29 3b  stroy)(void *));
258be 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 33  .  /* Added by 3
258bf 2e 35 2e 30 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  .5.0 */.  int (*
258c0 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 29 28 73  bind_zeroblob)(s
258c1 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
258c2 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 62  ,int);.  int (*b
258c3 6c 6f 62 5f 62 79 74 65 73 29 28 73 71 6c 69 74  lob_bytes)(sqlit
258c4 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74  e3_blob*);.  int
258c5 20 28 2a 62 6c 6f 62 5f 63 6c 6f 73 65 29 28 73   (*blob_close)(s
258c6 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20  qlite3_blob*);. 
258c7 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 6f 70 65 6e   int (*blob_open
258c8 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
258c9 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
258ca 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  r*,const char*,s
258cb 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
258cc 2c 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2a 29  ,sqlite3_blob**)
258cd 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 72  ;.  int (*blob_r
258ce 65 61 64 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f  ead)(sqlite3_blo
258cf 62 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  b*,void*,int,int
258d0 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f  );.  int (*blob_
258d1 77 72 69 74 65 29 28 73 71 6c 69 74 65 33 5f 62  write)(sqlite3_b
258d2 6c 6f 62 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  lob*,const void*
258d3 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ,int,int);.  int
258d4 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74   (*create_collat
258d5 69 6f 6e 5f 76 32 29 28 73 71 6c 69 74 65 33 2a  ion_v2)(sqlite3*
258d6 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
258d7 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f  ,void*,int(*)(vo
258d8 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
258d9 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
258da 69 64 2a 29 2c 76 6f 69 64 28 2a 29 28 76 6f 69  id*),void(*)(voi
258db 64 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 66 69  d*));.  int (*fi
258dc 6c 65 5f 63 6f 6e 74 72 6f 6c 29 28 73 71 6c 69  le_control)(sqli
258dd 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
258de 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73  ,int,void*);.  s
258df 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a 6d  qlite3_int64 (*m
258e0 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 29  emory_highwater)
258e1 28 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  (int);.  sqlite3
258e2 5f 69 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f  _int64 (*memory_
258e3 75 73 65 64 29 28 76 6f 69 64 29 3b 0a 20 20 73  used)(void);.  s
258e4 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a  qlite3_mutex *(*
258e5 6d 75 74 65 78 5f 61 6c 6c 6f 63 29 28 69 6e 74  mutex_alloc)(int
258e6 29 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65  );.  void (*mute
258e7 78 5f 65 6e 74 65 72 29 28 73 71 6c 69 74 65 33  x_enter)(sqlite3
258e8 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64  _mutex*);.  void
258e9 20 28 2a 6d 75 74 65 78 5f 66 72 65 65 29 28 73   (*mutex_free)(s
258ea 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
258eb 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f 6c    void (*mutex_l
258ec 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  eave)(sqlite3_mu
258ed 74 65 78 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 6d  tex*);.  int (*m
258ee 75 74 65 78 5f 74 72 79 29 28 73 71 6c 69 74 65  utex_try)(sqlite
258ef 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 69 6e 74  3_mutex*);.  int
258f0 20 28 2a 6f 70 65 6e 5f 76 32 29 28 63 6f 6e 73   (*open_v2)(cons
258f1 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a  t char*,sqlite3*
258f2 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
258f3 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 72 65 6c 65  *);.  int (*rele
258f4 61 73 65 5f 6d 65 6d 6f 72 79 29 28 69 6e 74 29  ase_memory)(int)
258f5 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c  ;.  void (*resul
258f6 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 29 28 73  t_error_nomem)(s
258f7 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
258f8 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c  ;.  void (*resul
258f9 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 29 28  t_error_toobig)(
258fa 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
258fb 29 3b 0a 20 20 69 6e 74 20 28 2a 73 6c 65 65 70  );.  int (*sleep
258fc 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28  )(int);.  void (
258fd 2a 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  *soft_heap_limit
258fe 29 28 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65  )(int);.  sqlite
258ff 33 5f 76 66 73 20 2a 28 2a 76 66 73 5f 66 69 6e  3_vfs *(*vfs_fin
25900 64 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  d)(const char*);
25901 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 72 65 67  .  int (*vfs_reg
25902 69 73 74 65 72 29 28 73 71 6c 69 74 65 33 5f 76  ister)(sqlite3_v
25903 66 73 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  fs*,int);.  int 
25904 28 2a 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  (*vfs_unregister
25905 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b  )(sqlite3_vfs*);
25906 0a 20 20 69 6e 74 20 28 2a 78 74 68 72 65 61 64  .  int (*xthread
25907 73 61 66 65 29 28 76 6f 69 64 29 3b 0a 20 20 76  safe)(void);.  v
25908 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 7a 65 72  oid (*result_zer
25909 6f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 63  oblob)(sqlite3_c
2590a 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20  ontext*,int);.  
2590b 76 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 65 72  void (*result_er
2590c 72 6f 72 5f 63 6f 64 65 29 28 73 71 6c 69 74 65  ror_code)(sqlite
2590d 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b  3_context*,int);
2590e 0a 20 20 69 6e 74 20 28 2a 74 65 73 74 5f 63 6f  .  int (*test_co
2590f 6e 74 72 6f 6c 29 28 69 6e 74 2c 20 2e 2e 2e 29  ntrol)(int, ...)
25910 3b 0a 20 20 76 6f 69 64 20 28 2a 72 61 6e 64 6f  ;.  void (*rando
25911 6d 6e 65 73 73 29 28 69 6e 74 2c 76 6f 69 64 2a  mness)(int,void*
25912 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 28 2a  );.  sqlite3 *(*
25913 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
25914 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
25915 78 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 65 78  xt*);.  int (*ex
25916 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f  tended_result_co
25917 64 65 73 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  des)(sqlite3*,in
25918 74 29 3b 0a 20 20 69 6e 74 20 28 2a 6c 69 6d 69  t);.  int (*limi
25919 74 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  t)(sqlite3*,int,
2591a 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  int);.  sqlite3_
2591b 73 74 6d 74 20 2a 28 2a 6e 65 78 74 5f 73 74 6d  stmt *(*next_stm
2591c 74 29 28 73 71 6c 69 74 65 33 2a 2c 73 71 6c 69  t)(sqlite3*,sqli
2591d 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 63 6f  te3_stmt*);.  co
2591e 6e 73 74 20 63 68 61 72 20 2a 28 2a 73 71 6c 29  nst char *(*sql)
2591f 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
25920 0a 20 20 69 6e 74 20 28 2a 73 74 61 74 75 73 29  .  int (*status)
25921 28 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  (int,int*,int*,i
25922 6e 74 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  nt);.};../*.** T
25923 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
25924 72 6f 73 20 72 65 64 65 66 69 6e 65 20 74 68 65  ros redefine the
25925 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 73 6f   API routines so
25926 20 74 68 61 74 20 74 68 65 79 20 61 72 65 0a 2a   that they are.*
25927 2a 20 72 65 64 69 72 65 63 74 65 64 20 74 68 72  * redirected thr
25928 6f 75 67 68 74 20 74 68 65 20 67 6c 6f 62 61 6c  ought the global
25929 20 73 71 6c 69 74 65 33 5f 61 70 69 20 73 74 72   sqlite3_api str
2592a 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
2592b 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69  is header file i
2592c 73 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 74  s also used by t
2592d 68 65 20 6c 6f 61 64 65 78 74 2e 63 20 73 6f 75  he loadext.c sou
2592e 72 63 65 20 66 69 6c 65 0a 2a 2a 20 28 70 61 72  rce file.** (par
2592f 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 53 51  t of the main SQ
25930 4c 69 74 65 20 6c 69 62 72 61 72 79 20 2d 20 6e  Lite library - n
25931 6f 74 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 29  ot an extension)
25932 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 63   so that.** it c
25933 61 6e 20 67 65 74 20 61 63 63 65 73 73 20 74 6f  an get access to
25934 20 74 68 65 20 73 71 6c 69 74 65 33 5f 61 70 69   the sqlite3_api
25935 5f 72 6f 75 74 69 6e 65 73 20 73 74 72 75 63 74  _routines struct
25936 75 72 65 0a 2a 2a 20 64 65 66 69 6e 69 74 69 6f  ure.** definitio
25937 6e 2e 20 20 42 75 74 20 74 68 65 20 6d 61 69 6e  n.  But the main
25938 20 6c 69 62 72 61 72 79 20 64 6f 65 73 20 6e 6f   library does no
25939 74 20 77 61 6e 74 20 74 6f 20 72 65 64 65 66 69  t want to redefi
2593a 6e 65 0a 2a 2a 20 74 68 65 20 41 50 49 2e 20 20  ne.** the API.  
2593b 53 6f 20 74 68 65 20 72 65 64 65 66 69 6e 69 74  So the redefinit
2593c 69 6f 6e 20 6d 61 63 72 6f 73 20 61 72 65 20 6f  ion macros are o
2593d 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 74 68 65  nly valid if the
2593e 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  .** SQLITE_CORE 
2593f 6d 61 63 72 6f 73 20 69 73 20 75 6e 64 65 66 69  macros is undefi
25940 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ned..*/.#ifndef 
25941 53 51 4c 49 54 45 5f 43 4f 52 45 0a 23 64 65 66  SQLITE_CORE.#def
25942 69 6e 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72  ine sqlite3_aggr
25943 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 20 20 20  egate_context   
25944 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25945 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
25946 74 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  t.#ifndef SQLITE
25947 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
25948 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25949 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74  _aggregate_count
2594a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2594b 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f 63  api->aggregate_c
2594c 6f 75 6e 74 0a 23 65 6e 64 69 66 0a 23 64 65 66  ount.#endif.#def
2594d 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
2594e 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20  _blob           
2594f 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25950 62 69 6e 64 5f 62 6c 6f 62 0a 23 64 65 66 69 6e  bind_blob.#defin
25951 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  e sqlite3_bind_d
25952 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  ouble           
25953 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
25954 6e 64 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e  nd_double.#defin
25955 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  e sqlite3_bind_i
25956 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
25957 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
25958 6e 64 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73  nd_int.#define s
25959 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
2595a 34 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  4             sq
2595b 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
2595c 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71  int64.#define sq
2595d 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 20  lite3_bind_null 
2595e 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2595f 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 6e  ite3_api->bind_n
25960 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ull.#define sqli
25961 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
25962 65 72 5f 63 6f 75 6e 74 20 20 20 73 71 6c 69 74  er_count   sqlit
25963 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72  e3_api->bind_par
25964 61 6d 65 74 65 72 5f 63 6f 75 6e 74 0a 23 64 65  ameter_count.#de
25965 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
25966 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
25967 78 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  x   sqlite3_api-
25968 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  >bind_parameter_
25969 69 6e 64 65 78 0a 23 64 65 66 69 6e 65 20 73 71  index.#define sq
2596a 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
2596b 65 74 65 72 5f 6e 61 6d 65 20 20 20 20 73 71 6c  eter_name    sql
2596c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70  ite3_api->bind_p
2596d 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 0a 23 64  arameter_name.#d
2596e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
2596f 6e 64 5f 74 65 78 74 20 20 20 20 20 20 20 20 20  nd_text         
25970 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25971 2d 3e 62 69 6e 64 5f 74 65 78 74 0a 23 64 65 66  ->bind_text.#def
25972 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
25973 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20  _text16         
25974 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25975 62 69 6e 64 5f 74 65 78 74 31 36 0a 23 64 65 66  bind_text16.#def
25976 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
25977 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20  _value          
25978 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25979 62 69 6e 64 5f 76 61 6c 75 65 0a 23 64 65 66 69  bind_value.#defi
2597a 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  ne sqlite3_busy_
2597b 68 61 6e 64 6c 65 72 20 20 20 20 20 20 20 20 20  handler         
2597c 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
2597d 75 73 79 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66  usy_handler.#def
2597e 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79  ine sqlite3_busy
2597f 5f 74 69 6d 65 6f 75 74 20 20 20 20 20 20 20 20  _timeout        
25980 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25981 62 75 73 79 5f 74 69 6d 65 6f 75 74 0a 23 64 65  busy_timeout.#de
25982 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 68 61  fine sqlite3_cha
25983 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 20 20  nges            
25984 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25985 3e 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65  >changes.#define
25986 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 20 20   sqlite3_close  
25987 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25988 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 6f  sqlite3_api->clo
25989 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
2598a 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
2598b 64 65 64 20 20 20 20 20 20 20 73 71 6c 69 74 65  ded       sqlite
2598c 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e  3_api->collation
2598d 5f 6e 65 65 64 65 64 0a 23 64 65 66 69 6e 65 20  _needed.#define 
2598e 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
2598f 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 73  n_needed16     s
25990 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 6c  qlite3_api->coll
25991 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 0a 23  ation_needed16.#
25992 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25993 6f 6c 75 6d 6e 5f 62 6c 6f 62 20 20 20 20 20 20  olumn_blob      
25994 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25995 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 0a 23  i->column_blob.#
25996 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25997 6f 6c 75 6d 6e 5f 62 79 74 65 73 20 20 20 20 20  olumn_bytes     
25998 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25999 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 0a  i->column_bytes.
2599a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2599b 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 20 20  column_bytes16  
2599c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2599d 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  pi->column_bytes
2599e 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
2599f 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 20  e3_column_count 
259a0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
259a1 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 63 6f  3_api->column_co
259a2 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
259a3 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
259a4 61 73 65 5f 6e 61 6d 65 20 20 20 73 71 6c 69 74  ase_name   sqlit
259a5 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
259a6 61 74 61 62 61 73 65 5f 6e 61 6d 65 0a 23 64 65  atabase_name.#de
259a7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
259a8 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
259a9 65 31 36 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  e16 sqlite3_api-
259aa 3e 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  >column_database
259ab 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20  _name16.#define 
259ac 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
259ad 65 63 6c 74 79 70 65 20 20 20 20 20 20 20 20 73  ecltype        s
259ae 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
259af 6d 6e 5f 64 65 63 6c 74 79 70 65 0a 23 64 65 66  mn_decltype.#def
259b0 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
259b1 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 20 20 20  mn_decltype16   
259b2 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
259b3 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31  column_decltype1
259b4 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
259b5 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20  3_column_double 
259b6 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
259b7 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 6f 75  _api->column_dou
259b8 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ble.#define sqli
259b9 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 20 20  te3_column_int  
259ba 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
259bb 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69  e3_api->column_i
259bc 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nt.#define sqlit
259bd 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 20  e3_column_int64 
259be 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
259bf 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e  3_api->column_in
259c0 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t64.#define sqli
259c1 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20  te3_column_name 
259c2 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
259c3 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e  e3_api->column_n
259c4 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
259c5 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
259c6 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
259c7 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e  e3_api->column_n
259c8 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ame16.#define sq
259c9 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
259ca 67 69 6e 5f 6e 61 6d 65 20 20 20 20 20 73 71 6c  gin_name     sql
259cb 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
259cc 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 0a 23 64 65  _origin_name.#de
259cd 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
259ce 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
259cf 36 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  6   sqlite3_api-
259d0 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  >column_origin_n
259d1 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ame16.#define sq
259d2 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
259d3 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 73 71 6c  le_name      sql
259d4 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
259d5 5f 74 61 62 6c 65 5f 6e 61 6d 65 0a 23 64 65 66  _table_name.#def
259d6 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
259d7 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20  mn_table_name16 
259d8 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
259d9 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
259da 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  e16.#define sqli
259db 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 20  te3_column_text 
259dc 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
259dd 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
259de 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
259df 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
259e0 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
259e1 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
259e2 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ext16.#define sq
259e3 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
259e4 65 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  e            sql
259e5 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
259e6 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71  _type.#define sq
259e7 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
259e8 75 65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  ue           sql
259e9 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
259ea 5f 76 61 6c 75 65 0a 23 64 65 66 69 6e 65 20 73  _value.#define s
259eb 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
259ec 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ok            sq
259ed 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 6d 69  lite3_api->commi
259ee 74 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73  t_hook.#define s
259ef 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20  qlite3_complete 
259f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
259f1 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c  lite3_api->compl
259f2 65 74 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ete.#define sqli
259f3 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 20  te3_complete16  
259f4 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
259f5 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65 74 65  e3_api->complete
259f6 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
259f7 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
259f8 69 6f 6e 20 20 20 20 20 20 20 73 71 6c 69 74 65  ion       sqlite
259f9 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f  3_api->create_co
259fa 6c 6c 61 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20  llation.#define 
259fb 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
259fc 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20 73  ollation16     s
259fd 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
259fe 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 0a 23  te_collation16.#
259ff 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25a00 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 20  reate_function  
25a01 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25a02 69 2d 3e 63 72 65 61 74 65 5f 66 75 6e 63 74 69  i->create_functi
25a03 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  on.#define sqlit
25a04 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
25a05 6f 6e 31 36 20 20 20 20 20 20 73 71 6c 69 74 65  on16      sqlite
25a06 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 66 75  3_api->create_fu
25a07 6e 63 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65  nction16.#define
25a08 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
25a09 6d 6f 64 75 6c 65 20 20 20 20 20 20 20 20 20 20  module          
25a0a 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65  sqlite3_api->cre
25a0b 61 74 65 5f 6d 6f 64 75 6c 65 0a 23 64 65 66 69  ate_module.#defi
25a0c 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
25a0d 65 5f 6d 6f 64 75 6c 65 5f 76 32 20 20 20 20 20  e_module_v2     
25a0e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
25a0f 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 0a  reate_module_v2.
25a10 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25a11 64 61 74 61 5f 63 6f 75 6e 74 20 20 20 20 20 20  data_count      
25a12 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25a13 70 69 2d 3e 64 61 74 61 5f 63 6f 75 6e 74 0a 23  pi->data_count.#
25a14 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64  define sqlite3_d
25a15 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20  b_handle        
25a16 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25a17 69 2d 3e 64 62 5f 68 61 6e 64 6c 65 0a 23 64 65  i->db_handle.#de
25a18 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63  fine sqlite3_dec
25a19 6c 61 72 65 5f 76 74 61 62 20 20 20 20 20 20 20  lare_vtab       
25a1a 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25a1b 3e 64 65 63 6c 61 72 65 5f 76 74 61 62 0a 23 64  >declare_vtab.#d
25a1c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e  efine sqlite3_en
25a1d 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
25a1e 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  e    sqlite3_api
25a1f 2d 3e 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  ->enable_shared_
25a20 63 61 63 68 65 0a 23 64 65 66 69 6e 65 20 73 71  cache.#define sq
25a21 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 20 20 20  lite3_errcode   
25a22 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
25a23 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 63 6f 64  ite3_api->errcod
25a24 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
25a25 33 5f 65 72 72 6d 73 67 20 20 20 20 20 20 20 20  3_errmsg        
25a26 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25a27 5f 61 70 69 2d 3e 65 72 72 6d 73 67 0a 23 64 65  _api->errmsg.#de
25a28 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
25a29 6d 73 67 31 36 20 20 20 20 20 20 20 20 20 20 20  msg16           
25a2a 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25a2b 3e 65 72 72 6d 73 67 31 36 0a 23 64 65 66 69 6e  >errmsg16.#defin
25a2c 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 20  e sqlite3_exec  
25a2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a2e 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78   sqlite3_api->ex
25a2f 65 63 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ec.#ifndef SQLIT
25a30 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
25a31 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  D.#define sqlite
25a32 33 5f 65 78 70 69 72 65 64 20 20 20 20 20 20 20  3_expired       
25a33 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25a34 5f 61 70 69 2d 3e 65 78 70 69 72 65 64 0a 23 65  _api->expired.#e
25a35 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndif.#define sql
25a36 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 20 20  ite3_finalize   
25a37 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25a38 74 65 33 5f 61 70 69 2d 3e 66 69 6e 61 6c 69 7a  te3_api->finaliz
25a39 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
25a3a 33 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20  3_free          
25a3b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25a3c 5f 61 70 69 2d 3e 66 72 65 65 0a 23 64 65 66 69  _api->free.#defi
25a3d 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  ne sqlite3_free_
25a3e 74 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  table           
25a3f 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66    sqlite3_api->f
25a40 72 65 65 5f 74 61 62 6c 65 0a 23 64 65 66 69 6e  ree_table.#defin
25a41 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  e sqlite3_get_au
25a42 74 6f 63 6f 6d 6d 69 74 20 20 20 20 20 20 20 20  tocommit        
25a43 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 65   sqlite3_api->ge
25a44 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 0a 23 64 65  t_autocommit.#de
25a45 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74  fine sqlite3_get
25a46 5f 61 75 78 64 61 74 61 20 20 20 20 20 20 20 20  _auxdata        
25a47 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25a48 3e 67 65 74 5f 61 75 78 64 61 74 61 0a 23 64 65  >get_auxdata.#de
25a49 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74  fine sqlite3_get
25a4a 5f 74 61 62 6c 65 20 20 20 20 20 20 20 20 20 20  _table          
25a4b 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25a4c 3e 67 65 74 5f 74 61 62 6c 65 0a 23 69 66 6e 64  >get_table.#ifnd
25a4d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
25a4e 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e  EPRECATED.#defin
25a4f 65 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c  e sqlite3_global
25a50 5f 72 65 63 6f 76 65 72 20 20 20 20 20 20 20 20  _recover        
25a51 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 6c   sqlite3_api->gl
25a52 6f 62 61 6c 5f 72 65 63 6f 76 65 72 0a 23 65 6e  obal_recover.#en
25a53 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dif.#define sqli
25a54 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 20 20  te3_interrupt   
25a55 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25a56 65 33 5f 61 70 69 2d 3e 69 6e 74 65 72 72 75 70  e3_api->interrup
25a57 74 78 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  tx.#define sqlit
25a58 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
25a59 6f 77 69 64 20 20 20 20 20 20 73 71 6c 69 74 65  owid      sqlite
25a5a 33 5f 61 70 69 2d 3e 6c 61 73 74 5f 69 6e 73 65  3_api->last_inse
25a5b 72 74 5f 72 6f 77 69 64 0a 23 64 65 66 69 6e 65  rt_rowid.#define
25a5c 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
25a5d 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
25a5e 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 62  sqlite3_api->lib
25a5f 76 65 72 73 69 6f 6e 0a 23 64 65 66 69 6e 65 20  version.#define 
25a60 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
25a61 6f 6e 5f 6e 75 6d 62 65 72 20 20 20 20 20 20 73  on_number      s
25a62 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 62 76  qlite3_api->libv
25a63 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 0a 23 64  ersion_number.#d
25a64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 61  efine sqlite3_ma
25a65 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20  lloc            
25a66 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25a67 2d 3e 6d 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65  ->malloc.#define
25a68 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
25a69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a6a 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 70 72  sqlite3_api->mpr
25a6b 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  intf.#define sql
25a6c 69 74 65 33 5f 6f 70 65 6e 20 20 20 20 20 20 20  ite3_open       
25a6d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25a6e 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 0a 23 64  te3_api->open.#d
25a6f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70  efine sqlite3_op
25a70 65 6e 31 36 20 20 20 20 20 20 20 20 20 20 20 20  en16            
25a71 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25a72 2d 3e 6f 70 65 6e 31 36 0a 23 64 65 66 69 6e 65  ->open16.#define
25a73 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
25a74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a75 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
25a76 70 61 72 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  pare.#define sql
25a77 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 20 20  ite3_prepare16  
25a78 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25a79 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65  te3_api->prepare
25a7a 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
25a7b 65 33 5f 70 72 65 70 61 72 65 5f 76 32 20 20 20  e3_prepare_v2   
25a7c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25a7d 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 5f 76  3_api->prepare_v
25a7e 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  2.#define sqlite
25a7f 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20 20  3_prepare16_v2  
25a80 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25a81 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 31 36 5f  _api->prepare16_
25a82 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  v2.#define sqlit
25a83 65 33 5f 70 72 6f 66 69 6c 65 20 20 20 20 20 20  e3_profile      
25a84 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25a85 33 5f 61 70 69 2d 3e 70 72 6f 66 69 6c 65 0a 23  3_api->profile.#
25a86 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
25a87 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 20  rogress_handler 
25a88 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25a89 69 2d 3e 70 72 6f 67 72 65 73 73 5f 68 61 6e 64  i->progress_hand
25a8a 6c 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ler.#define sqli
25a8b 74 65 33 5f 72 65 61 6c 6c 6f 63 20 20 20 20 20  te3_realloc     
25a8c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25a8d 65 33 5f 61 70 69 2d 3e 72 65 61 6c 6c 6f 63 0a  e3_api->realloc.
25a8e 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25a8f 72 65 73 65 74 20 20 20 20 20 20 20 20 20 20 20  reset           
25a90 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25a91 70 69 2d 3e 72 65 73 65 74 0a 23 64 65 66 69 6e  pi->reset.#defin
25a92 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
25a93 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20  _blob           
25a94 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
25a95 73 75 6c 74 5f 62 6c 6f 62 0a 23 64 65 66 69 6e  sult_blob.#defin
25a96 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
25a97 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20  _double         
25a98 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
25a99 73 75 6c 74 5f 64 6f 75 62 6c 65 0a 23 64 65 66  sult_double.#def
25a9a 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
25a9b 6c 74 5f 65 72 72 6f 72 20 20 20 20 20 20 20 20  lt_error        
25a9c 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25a9d 72 65 73 75 6c 74 5f 65 72 72 6f 72 0a 23 64 65  result_error.#de
25a9e 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
25a9f 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20  ult_error16     
25aa0 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25aa1 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 0a  >result_error16.
25aa2 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25aa3 72 65 73 75 6c 74 5f 69 6e 74 20 20 20 20 20 20  result_int      
25aa4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25aa5 70 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74 0a 23  pi->result_int.#
25aa6 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
25aa7 65 73 75 6c 74 5f 69 6e 74 36 34 20 20 20 20 20  esult_int64     
25aa8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25aa9 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74 36 34 0a  i->result_int64.
25aaa 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25aab 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 20 20 20 20  result_null     
25aac 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25aad 70 69 2d 3e 72 65 73 75 6c 74 5f 6e 75 6c 6c 0a  pi->result_null.
25aae 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25aaf 72 65 73 75 6c 74 5f 74 65 78 74 20 20 20 20 20  result_text     
25ab0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25ab1 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 0a  pi->result_text.
25ab2 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25ab3 72 65 73 75 6c 74 5f 74 65 78 74 31 36 20 20 20  result_text16   
25ab4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25ab5 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 31  pi->result_text1
25ab6 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
25ab7 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  3_result_text16b
25ab8 65 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  e        sqlite3
25ab9 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78  _api->result_tex
25aba 74 31 36 62 65 0a 23 64 65 66 69 6e 65 20 73 71  t16be.#define sq
25abb 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
25abc 74 31 36 6c 65 20 20 20 20 20 20 20 20 73 71 6c  t16le        sql
25abd 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
25abe 5f 74 65 78 74 31 36 6c 65 0a 23 64 65 66 69 6e  _text16le.#defin
25abf 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
25ac0 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20  _value          
25ac1 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
25ac2 73 75 6c 74 5f 76 61 6c 75 65 0a 23 64 65 66 69  sult_value.#defi
25ac3 6e 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62  ne sqlite3_rollb
25ac4 61 63 6b 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20  ack_hook        
25ac5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
25ac6 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 0a 23 64 65  ollback_hook.#de
25ac7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 65 74  fine sqlite3_set
25ac8 5f 61 75 74 68 6f 72 69 7a 65 72 20 20 20 20 20  _authorizer     
25ac9 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25aca 3e 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 0a  >set_authorizer.
25acb 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25acc 73 65 74 5f 61 75 78 64 61 74 61 20 20 20 20 20  set_auxdata     
25acd 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25ace 70 69 2d 3e 73 65 74 5f 61 75 78 64 61 74 61 0a  pi->set_auxdata.
25acf 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25ad0 73 6e 70 72 69 6e 74 66 20 20 20 20 20 20 20 20  snprintf        
25ad1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25ad2 70 69 2d 3e 73 6e 70 72 69 6e 74 66 0a 23 64 65  pi->snprintf.#de
25ad3 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 74 65  fine sqlite3_ste
25ad4 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
25ad5 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25ad6 3e 73 74 65 70 0a 23 64 65 66 69 6e 65 20 73 71  >step.#define sq
25ad7 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75  lite3_table_colu
25ad8 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20 73 71 6c  mn_metadata  sql
25ad9 69 74 65 33 5f 61 70 69 2d 3e 74 61 62 6c 65 5f  ite3_api->table_
25ada 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 0a  column_metadata.
25adb 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25adc 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 20 20  thread_cleanup  
25add 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25ade 70 69 2d 3e 74 68 72 65 61 64 5f 63 6c 65 61 6e  pi->thread_clean
25adf 75 70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  up.#define sqlit
25ae0 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
25ae1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25ae2 33 5f 61 70 69 2d 3e 74 6f 74 61 6c 5f 63 68 61  3_api->total_cha
25ae3 6e 67 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c  nges.#define sql
25ae4 69 74 65 33 5f 74 72 61 63 65 20 20 20 20 20 20  ite3_trace      
25ae5 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25ae6 74 65 33 5f 61 70 69 2d 3e 74 72 61 63 65 0a 23  te3_api->trace.#
25ae7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25ae8 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64  IT_DEPRECATED.#d
25ae9 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72  efine sqlite3_tr
25aea 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 20  ansfer_bindings 
25aeb 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25aec 2d 3e 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69  ->transfer_bindi
25aed 6e 67 73 0a 23 65 6e 64 69 66 0a 23 64 65 66 69  ngs.#endif.#defi
25aee 6e 65 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74  ne sqlite3_updat
25aef 65 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20 20 20  e_hook          
25af0 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75    sqlite3_api->u
25af1 70 64 61 74 65 5f 68 6f 6f 6b 0a 23 64 65 66 69  pdate_hook.#defi
25af2 6e 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  ne sqlite3_user_
25af3 64 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20  data            
25af4 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75    sqlite3_api->u
25af5 73 65 72 5f 64 61 74 61 0a 23 64 65 66 69 6e 65  ser_data.#define
25af6 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
25af7 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 20  lob             
25af8 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
25af9 75 65 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20  ue_blob.#define 
25afa 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
25afb 74 65 73 20 20 20 20 20 20 20 20 20 20 20 20 73  tes            s
25afc 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
25afd 65 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20  e_bytes.#define 
25afe 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
25aff 74 65 73 31 36 20 20 20 20 20 20 20 20 20 20 73  tes16          s
25b00 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
25b01 65 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e  e_bytes16.#defin
25b02 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
25b03 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20  double          
25b04 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
25b05 6c 75 65 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69  lue_double.#defi
25b06 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
25b07 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  _int            
25b08 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
25b09 61 6c 75 65 5f 69 6e 74 0a 23 64 65 66 69 6e 65  alue_int.#define
25b0a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
25b0b 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 20 20  nt64            
25b0c 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
25b0d 75 65 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65  ue_int64.#define
25b0e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
25b0f 75 6d 65 72 69 63 5f 74 79 70 65 20 20 20 20 20  umeric_type     
25b10 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
25b11 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 0a  ue_numeric_type.
25b12 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25b13 76 61 6c 75 65 5f 74 65 78 74 20 20 20 20 20 20  value_text      
25b14 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25b15 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 0a 23  pi->value_text.#
25b16 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
25b17 61 6c 75 65 5f 74 65 78 74 31 36 20 20 20 20 20  alue_text16     
25b18 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25b19 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36 0a  i->value_text16.
25b1a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25b1b 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20  value_text16be  
25b1c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25b1d 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36  pi->value_text16
25b1e 62 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  be.#define sqlit
25b1f 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c  e3_value_text16l
25b20 65 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  e         sqlite
25b21 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78  3_api->value_tex
25b22 74 31 36 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  t16le.#define sq
25b23 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
25b24 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
25b25 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
25b26 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  type.#define sql
25b27 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 20 20 20  ite3_vmprintf   
25b28 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25b29 74 65 33 5f 61 70 69 2d 3e 76 6d 70 72 69 6e 74  te3_api->vmprint
25b2a 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  f.#define sqlite
25b2b 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
25b2c 69 6f 6e 20 20 20 20 20 20 73 71 6c 69 74 65 33  ion      sqlite3
25b2d 5f 61 70 69 2d 3e 6f 76 65 72 6c 6f 61 64 5f 66  _api->overload_f
25b2e 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20  unction.#define 
25b2f 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
25b30 76 32 20 20 20 20 20 20 20 20 20 20 20 20 20 73  v2             s
25b31 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70  qlite3_api->prep
25b32 61 72 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  are_v2.#define s
25b33 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
25b34 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 73 71  _v2           sq
25b35 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61  lite3_api->prepa
25b36 72 65 31 36 5f 76 32 0a 23 64 65 66 69 6e 65 20  re16_v2.#define 
25b37 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69  sqlite3_clear_bi
25b38 6e 64 69 6e 67 73 20 20 20 20 20 20 20 20 20 73  ndings         s
25b39 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 65 61  qlite3_api->clea
25b3a 72 5f 62 69 6e 64 69 6e 67 73 0a 23 64 65 66 69  r_bindings.#defi
25b3b 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
25b3c 7a 65 72 6f 62 6c 6f 62 20 20 20 20 20 20 20 20  zeroblob        
25b3d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
25b3e 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 0a 23 64 65  ind_zeroblob.#de
25b3f 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
25b40 62 5f 62 79 74 65 73 20 20 20 20 20 20 20 20 20  b_bytes         
25b41 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25b42 3e 62 6c 6f 62 5f 62 79 74 65 73 0a 23 64 65 66  >blob_bytes.#def
25b43 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
25b44 5f 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20  _close          
25b45 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25b46 62 6c 6f 62 5f 63 6c 6f 73 65 0a 23 64 65 66 69  blob_close.#defi
25b47 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
25b48 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20  open            
25b49 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
25b4a 6c 6f 62 5f 6f 70 65 6e 0a 23 64 65 66 69 6e 65  lob_open.#define
25b4b 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
25b4c 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ad              
25b4d 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f  sqlite3_api->blo
25b4e 62 5f 72 65 61 64 0a 23 64 65 66 69 6e 65 20 73  b_read.#define s
25b4f 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
25b50 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  e             sq
25b51 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f  lite3_api->blob_
25b52 77 72 69 74 65 0a 23 64 65 66 69 6e 65 20 73 71  write.#define sq
25b53 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
25b54 6c 61 74 69 6f 6e 5f 76 32 20 20 20 20 73 71 6c  lation_v2    sql
25b55 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65  ite3_api->create
25b56 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 0a 23 64  _collation_v2.#d
25b57 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69  efine sqlite3_fi
25b58 6c 65 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20 20  le_control      
25b59 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25b5a 2d 3e 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 0a 23  ->file_control.#
25b5b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
25b5c 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 20  emory_highwater 
25b5d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25b5e 69 2d 3e 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  i->memory_highwa
25b5f 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ter.#define sqli
25b60 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 20  te3_memory_used 
25b61 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25b62 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79 5f 75  e3_api->memory_u
25b63 73 65 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  sed.#define sqli
25b64 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 20  te3_mutex_alloc 
25b65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25b66 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 61 6c  e3_api->mutex_al
25b67 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  loc.#define sqli
25b68 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 20  te3_mutex_enter 
25b69 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25b6a 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 65 6e  e3_api->mutex_en
25b6b 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ter.#define sqli
25b6c 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 20 20  te3_mutex_free  
25b6d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25b6e 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 66 72  e3_api->mutex_fr
25b6f 65 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ee.#define sqlit
25b70 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 20 20  e3_mutex_leave  
25b71 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25b72 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 6c 65 61  3_api->mutex_lea
25b73 76 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ve.#define sqlit
25b74 65 33 5f 6d 75 74 65 78 5f 74 72 79 20 20 20 20  e3_mutex_try    
25b75 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25b76 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 74 72 79  3_api->mutex_try
25b77 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25b78 5f 6f 70 65 6e 5f 76 32 20 20 20 20 20 20 20 20  _open_v2        
25b79 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25b7a 61 70 69 2d 3e 6f 70 65 6e 5f 76 32 0a 23 64 65  api->open_v2.#de
25b7b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 6c  fine sqlite3_rel
25b7c 65 61 73 65 5f 6d 65 6d 6f 72 79 20 20 20 20 20  ease_memory     
25b7d 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25b7e 3e 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 0a  >release_memory.
25b7f 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25b80 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
25b81 65 6d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  em     sqlite3_a
25b82 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72  pi->result_error
25b83 5f 6e 6f 6d 65 6d 0a 23 64 65 66 69 6e 65 20 73  _nomem.#define s
25b84 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
25b85 72 6f 72 5f 74 6f 6f 62 69 67 20 20 20 20 73 71  ror_toobig    sq
25b86 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
25b87 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 0a 23  t_error_toobig.#
25b88 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
25b89 6c 65 65 70 20 20 20 20 20 20 20 20 20 20 20 20  leep            
25b8a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25b8b 69 2d 3e 73 6c 65 65 70 0a 23 64 65 66 69 6e 65  i->sleep.#define
25b8c 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65   sqlite3_soft_he
25b8d 61 70 5f 6c 69 6d 69 74 20 20 20 20 20 20 20 20  ap_limit        
25b8e 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6f 66  sqlite3_api->sof
25b8f 74 5f 68 65 61 70 5f 6c 69 6d 69 74 0a 23 64 65  t_heap_limit.#de
25b90 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73  fine sqlite3_vfs
25b91 5f 66 69 6e 64 20 20 20 20 20 20 20 20 20 20 20  _find           
25b92 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25b93 3e 76 66 73 5f 66 69 6e 64 0a 23 64 65 66 69 6e  >vfs_find.#defin
25b94 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65  e sqlite3_vfs_re
25b95 67 69 73 74 65 72 20 20 20 20 20 20 20 20 20 20  gister          
25b96 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 66   sqlite3_api->vf
25b97 73 5f 72 65 67 69 73 74 65 72 0a 23 64 65 66 69  s_register.#defi
25b98 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75  ne sqlite3_vfs_u
25b99 6e 72 65 67 69 73 74 65 72 20 20 20 20 20 20 20  nregister       
25b9a 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
25b9b 66 73 5f 75 6e 72 65 67 69 73 74 65 72 0a 23 64  fs_unregister.#d
25b9c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 68  efine sqlite3_th
25b9d 72 65 61 64 73 61 66 65 20 20 20 20 20 20 20 20  readsafe        
25b9e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25b9f 2d 3e 78 74 68 72 65 61 64 73 61 66 65 0a 23 64  ->xthreadsafe.#d
25ba0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
25ba1 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 20 20 20  sult_zeroblob   
25ba2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25ba3 2d 3e 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  ->result_zeroblo
25ba4 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
25ba5 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
25ba6 6f 64 65 20 20 20 20 20 20 73 71 6c 69 74 65 33  ode      sqlite3
25ba7 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72  _api->result_err
25ba8 6f 72 5f 63 6f 64 65 0a 23 64 65 66 69 6e 65 20  or_code.#define 
25ba9 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
25baa 74 72 6f 6c 20 20 20 20 20 20 20 20 20 20 20 73  trol           s
25bab 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 65 73 74  qlite3_api->test
25bac 5f 63 6f 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65  _control.#define
25bad 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
25bae 65 73 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ess             
25baf 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 61 6e  sqlite3_api->ran
25bb0 64 6f 6d 6e 65 73 73 0a 23 64 65 66 69 6e 65 20  domness.#define 
25bb1 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
25bb2 64 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 73  db_handle      s
25bb3 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6e 74  qlite3_api->cont
25bb4 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 0a 23 64  ext_db_handle.#d
25bb5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 78  efine sqlite3_ex
25bb6 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f  tended_result_co
25bb7 64 65 73 20 20 73 71 6c 69 74 65 33 5f 61 70 69  des  sqlite3_api
25bb8 2d 3e 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c  ->extended_resul
25bb9 74 5f 63 6f 64 65 73 0a 23 64 65 66 69 6e 65 20  t_codes.#define 
25bba 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 20 20 20  sqlite3_limit   
25bbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
25bbc 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 6d 69  qlite3_api->limi
25bbd 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
25bbe 33 5f 6e 65 78 74 5f 73 74 6d 74 20 20 20 20 20  3_next_stmt     
25bbf 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25bc0 5f 61 70 69 2d 3e 6e 65 78 74 5f 73 74 6d 74 0a  _api->next_stmt.
25bc1 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25bc2 73 71 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  sql             
25bc3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25bc4 70 69 2d 3e 73 71 6c 0a 23 64 65 66 69 6e 65 20  pi->sql.#define 
25bc5 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 20 20  sqlite3_status  
25bc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
25bc7 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 74 61 74  qlite3_api->stat
25bc8 75 73 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  us.#endif /* SQL
25bc9 49 54 45 5f 43 4f 52 45 20 2a 2f 0a 0a 23 64 65  ITE_CORE */..#de
25bca 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45  fine SQLITE_EXTE
25bcb 4e 53 49 4f 4e 5f 49 4e 49 54 31 20 20 20 20 20  NSION_INIT1     
25bcc 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
25bcd 69 5f 72 6f 75 74 69 6e 65 73 20 2a 73 71 6c 69  i_routines *sqli
25bce 74 65 33 5f 61 70 69 20 3d 20 30 3b 0a 23 64 65  te3_api = 0;.#de
25bcf 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45  fine SQLITE_EXTE
25bd0 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 76 29 20 20  NSION_INIT2(v)  
25bd1 73 71 6c 69 74 65 33 5f 61 70 69 20 3d 20 76 3b  sqlite3_api = v;
25bd2 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c  ..#endif /* _SQL
25bd3 49 54 45 33 45 58 54 5f 48 5f 20 2a 2f 0a 0a 2f  ITE3EXT_H_ */../
25bd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
25bd5 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 65 78 74  nd of sqlite3ext
25bd6 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
25bd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25bd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
25bd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
25bda 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
25bdb 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6c  we left off in l
25bdc 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  oadext.c *******
25bdd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
25bde 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
25bdf 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
25be0 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 41  ON../*.** Some A
25be1 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  PI routines are 
25be2 6f 6d 69 74 74 65 64 20 77 68 65 6e 20 76 61 72  omitted when var
25be3 69 6f 75 73 20 66 65 61 74 75 72 65 73 20 61 72  ious features ar
25be4 65 0a 2a 2a 20 65 78 63 6c 75 64 65 64 20 66 72  e.** excluded fr
25be5 6f 6d 20 61 20 62 75 69 6c 64 20 6f 66 20 53 51  om a build of SQ
25be6 4c 69 74 65 2e 20 20 53 75 62 73 74 69 74 75 74  Lite.  Substitut
25be7 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  e a NULL pointer
25be8 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 6d 69 73 73  .** for any miss
25be9 69 6e 67 20 41 50 49 73 2e 0a 2a 2f 0a 23 69 66  ing APIs..*/.#if
25bea 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  ndef SQLITE_ENAB
25beb 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
25bec 54 41 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  TA.# define sqli
25bed 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
25bee 61 73 65 5f 6e 61 6d 65 20 20 20 30 0a 23 20 64  ase_name   0.# d
25bef 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
25bf0 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
25bf1 6d 65 31 36 20 30 0a 23 20 64 65 66 69 6e 65 20  me16 0.# define 
25bf2 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
25bf3 61 62 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 30  able_name      0
25bf4 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
25bf5 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
25bf6 61 6d 65 31 36 20 20 20 20 30 0a 23 20 64 65 66  ame16    0.# def
25bf7 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
25bf8 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20 20  mn_origin_name  
25bf9 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
25bfa 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
25bfb 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a 23  gin_name16   0.#
25bfc 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
25bfd 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
25bfe 61 64 61 74 61 20 20 30 0a 23 65 6e 64 69 66 0a  adata  0.#endif.
25bff 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
25c00 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
25c01 4e 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  N.# define sqlit
25c02 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
25c03 72 20 20 20 20 20 20 20 20 20 30 0a 23 65 6e 64  r         0.#end
25c04 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
25c05 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 23 20 64  E_OMIT_UTF16.# d
25c06 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
25c07 6e 64 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  nd_text16       
25c08 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
25c09 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
25c0a 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 30  n_needed16     0
25c0b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
25c0c 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
25c0d 65 31 36 20 20 20 20 20 20 30 0a 23 20 64 65 66  e16      0.# def
25c0e 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
25c0f 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20  mn_name16       
25c10 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
25c11 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
25c12 74 31 36 20 20 20 20 20 20 20 20 20 20 30 0a 23  t16          0.#
25c13 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
25c14 63 6f 6d 70 6c 65 74 65 31 36 20 20 20 20 20 20  complete16      
25c15 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
25c16 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
25c17 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20  _collation16    
25c18 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
25c19 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
25c1a 69 6f 6e 31 36 20 20 20 20 20 20 30 0a 23 20 64  ion16      0.# d
25c1b 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72  efine sqlite3_er
25c1c 72 6d 73 67 31 36 20 20 20 20 20 20 20 20 20 20  rmsg16          
25c1d 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
25c1e 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20 20  sqlite3_open16  
25c1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
25c20 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
25c21 33 5f 70 72 65 70 61 72 65 31 36 20 20 20 20 20  3_prepare16     
25c22 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
25c23 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
25c24 61 72 65 31 36 5f 76 32 20 20 20 20 20 20 20 20  are16_v2        
25c25 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
25c26 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
25c27 6f 72 31 36 20 20 20 20 20 20 20 20 20 30 0a 23  or16         0.#
25c28 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
25c29 72 65 73 75 6c 74 5f 74 65 78 74 31 36 20 20 20  result_text16   
25c2a 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
25c2b 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
25c2c 5f 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20  _text16be       
25c2d 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
25c2e 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
25c2f 36 6c 65 20 20 20 20 20 20 20 20 30 0a 23 20 64  6le        0.# d
25c30 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
25c31 6c 75 65 5f 74 65 78 74 31 36 20 20 20 20 20 20  lue_text16      
25c32 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
25c33 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
25c34 78 74 31 36 62 65 20 20 20 20 20 20 20 20 20 30  xt16be         0
25c35 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
25c36 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
25c37 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
25c38 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
25c39 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
25c3a 31 36 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71  16 0.# define sq
25c3b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
25c3c 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20 30 0a 23  le_name16    0.#
25c3d 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
25c3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
25c3f 6d 65 31 36 20 20 20 30 0a 23 65 6e 64 69 66 0a  me16   0.#endif.
25c40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
25c41 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 23 20 64  MIT_COMPLETE.# d
25c42 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
25c43 6d 70 6c 65 74 65 20 30 0a 23 20 64 65 66 69 6e  mplete 0.# defin
25c44 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  e sqlite3_comple
25c45 74 65 31 36 20 30 0a 23 65 6e 64 69 66 0a 0a 23  te16 0.#endif..#
25c46 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
25c47 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
25c48 41 43 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ACK.# define sql
25c49 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61  ite3_progress_ha
25c4a 6e 64 6c 65 72 20 30 0a 23 65 6e 64 69 66 0a 0a  ndler 0.#endif..
25c4b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
25c4c 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
25c4d 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
25c4e 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 30  _create_module 0
25c4f 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
25c50 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
25c51 76 32 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71  v2 0.# define sq
25c52 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
25c53 61 62 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ab 0.#endif..#if
25c54 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
25c55 53 48 41 52 45 44 5f 43 41 43 48 45 0a 23 20 64  SHARED_CACHE.# d
25c56 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e  efine sqlite3_en
25c57 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
25c58 65 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  e 0.#endif..#ifd
25c59 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
25c5a 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20 73 71  RACE.# define sq
25c5b 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20 20  lite3_profile   
25c5c 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
25c5d 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20 20 20  qlite3_trace    
25c5e 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23       0.#endif..#
25c5f 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
25c60 54 5f 47 45 54 5f 54 41 42 4c 45 0a 23 20 64 65  T_GET_TABLE.# de
25c61 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65  fine sqlite3_fre
25c62 65 5f 74 61 62 6c 65 20 20 20 20 30 0a 23 20 64  e_table    0.# d
25c63 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65  efine sqlite3_ge
25c64 74 5f 74 61 62 6c 65 20 20 20 20 20 30 0a 23 65  t_table     0.#e
25c65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
25c66 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
25c67 42 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  B.#define sqlite
25c68 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20  3_bind_zeroblob 
25c69 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74   0.#define sqlit
25c6a 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20 20  e3_blob_bytes   
25c6b 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69    0.#define sqli
25c6c 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20  te3_blob_close  
25c6d 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c     0.#define sql
25c6e 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20  ite3_blob_open  
25c6f 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71      0.#define sq
25c70 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 20  lite3_blob_read 
25c71 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73       0.#define s
25c72 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
25c73 65 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a  e     0.#endif..
25c74 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
25c75 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f  ing structure co
25c76 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20  ntains pointers 
25c77 74 6f 20 61 6c 6c 20 53 51 4c 69 74 65 20 41 50  to all SQLite AP
25c78 49 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41  I routines..** A
25c79 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
25c7a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61   structure is pa
25c7b 73 73 65 64 20 69 6e 74 6f 20 65 78 74 65 6e 73  ssed into extens
25c7c 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 79 20 61  ions when they a
25c7d 72 65 0a 2a 2a 20 6c 6f 61 64 65 64 20 73 6f 20  re.** loaded so 
25c7e 74 68 61 74 20 74 68 65 20 65 78 74 65 6e 73 69  that the extensi
25c7f 6f 6e 20 63 61 6e 20 6d 61 6b 65 20 63 61 6c 6c  on can make call
25c80 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  s back into the 
25c81 53 51 4c 69 74 65 0a 2a 2a 20 6c 69 62 72 61 72  SQLite.** librar
25c82 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 64  y..**.** When ad
25c83 64 69 6e 67 20 6e 65 77 20 41 50 49 73 2c 20 61  ding new APIs, a
25c84 64 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20 62  dd them to the b
25c85 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 20 73 74  ottom of this st
25c86 72 75 63 74 75 72 65 0a 2a 2a 20 69 6e 20 6f 72  ructure.** in or
25c87 64 65 72 20 74 6f 20 70 72 65 73 65 72 76 65 20  der to preserve 
25c88 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
25c89 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 45  ibility..**.** E
25c8a 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 75  xtensions that u
25c8b 73 65 20 6e 65 77 65 72 20 41 50 49 73 20 73 68  se newer APIs sh
25c8c 6f 75 6c 64 20 66 69 72 73 74 20 63 61 6c 6c 20  ould first call 
25c8d 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c  the.** sqlite3_l
25c8e 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
25c8f 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  () to make sure 
25c90 74 68 61 74 20 74 68 65 20 41 50 49 20 74 68 65  that the API the
25c91 79 0a 2a 2a 20 69 6e 74 65 6e 64 20 74 6f 20 75  y.** intend to u
25c92 73 65 20 69 73 20 73 75 70 70 6f 72 74 65 64 20  se is supported 
25c93 62 79 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20  by the library. 
25c94 20 45 78 74 65 6e 73 69 6f 6e 73 20 73 68 6f 75   Extensions shou
25c95 6c 64 0a 2a 2a 20 61 6c 73 6f 20 63 68 65 63 6b  ld.** also check
25c96 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
25c97 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  at the pointer t
25c98 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  o the function i
25c99 73 0a 2a 2a 20 6e 6f 74 20 4e 55 4c 4c 20 62 65  s.** not NULL be
25c9a 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 69 74 2e  fore calling it.
25c9b 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
25c9c 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
25c9d 74 69 6e 65 73 20 73 71 6c 69 74 65 33 41 70 69  tines sqlite3Api
25c9e 73 20 3d 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  s = {.  sqlite3_
25c9f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
25ca0 74 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  t,.#ifndef SQLIT
25ca1 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
25ca2 44 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67 72  D.  sqlite3_aggr
25ca3 65 67 61 74 65 5f 63 6f 75 6e 74 2c 0a 23 65 6c  egate_count,.#el
25ca4 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20  se.  0,.#endif. 
25ca5 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
25ca6 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  ob,.  sqlite3_bi
25ca7 6e 64 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c  nd_double,.  sql
25ca8 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 2c 0a 20  ite3_bind_int,. 
25ca9 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
25caa 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  t64,.  sqlite3_b
25cab 69 6e 64 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69  ind_null,.  sqli
25cac 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
25cad 65 72 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69  er_count,.  sqli
25cae 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
25caf 65 72 5f 69 6e 64 65 78 2c 0a 20 20 73 71 6c 69  er_index,.  sqli
25cb0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
25cb1 65 72 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  er_name,.  sqlit
25cb2 65 33 5f 62 69 6e 64 5f 74 65 78 74 2c 0a 20 20  e3_bind_text,.  
25cb3 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
25cb4 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  t16,.  sqlite3_b
25cb5 69 6e 64 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c  ind_value,.  sql
25cb6 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
25cb7 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75 73  r,.  sqlite3_bus
25cb8 79 5f 74 69 6d 65 6f 75 74 2c 0a 20 20 73 71 6c  y_timeout,.  sql
25cb9 69 74 65 33 5f 63 68 61 6e 67 65 73 2c 0a 20 20  ite3_changes,.  
25cba 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 2c 0a 20  sqlite3_close,. 
25cbb 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
25cbc 6f 6e 5f 6e 65 65 64 65 64 2c 0a 20 20 73 71 6c  on_needed,.  sql
25cbd 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
25cbe 65 65 64 65 64 31 36 2c 0a 20 20 73 71 6c 69 74  eeded16,.  sqlit
25cbf 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 2c 0a  e3_column_blob,.
25cc0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
25cc1 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69 74 65  _bytes,.  sqlite
25cc2 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
25cc3 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
25cc4 6d 6e 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69  mn_count,.  sqli
25cc5 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
25cc6 61 73 65 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69  ase_name,.  sqli
25cc7 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
25cc8 61 73 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71  ase_name16,.  sq
25cc9 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63  lite3_column_dec
25cca 6c 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33  ltype,.  sqlite3
25ccb 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
25ccc 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
25ccd 6c 75 6d 6e 5f 64 6f 75 62 6c 65 2c 0a 20 20 73  lumn_double,.  s
25cce 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
25ccf 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t,.  sqlite3_col
25cd0 75 6d 6e 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c  umn_int64,.  sql
25cd1 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
25cd2 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
25cd3 6d 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c  mn_name16,.  sql
25cd4 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
25cd5 69 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  in_name,.  sqlit
25cd6 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
25cd7 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74  _name16,.  sqlit
25cd8 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
25cd9 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  name,.  sqlite3_
25cda 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
25cdb 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
25cdc 6f 6c 75 6d 6e 5f 74 65 78 74 2c 0a 20 20 73 71  olumn_text,.  sq
25cdd 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
25cde 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  t16,.  sqlite3_c
25cdf 6f 6c 75 6d 6e 5f 74 79 70 65 2c 0a 20 20 73 71  olumn_type,.  sq
25ce0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
25ce1 75 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ue,.  sqlite3_co
25ce2 6d 6d 69 74 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c  mmit_hook,.  sql
25ce3 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 2c 0a 20  ite3_complete,. 
25ce4 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
25ce5 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
25ce6 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c  reate_collation,
25ce7 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
25ce8 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 2c 0a 20  e_collation16,. 
25ce9 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
25cea 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 73 71 6c 69  function,.  sqli
25ceb 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
25cec 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ion16,.  sqlite3
25ced 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 2c 0a  _create_module,.
25cee 20 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63    sqlite3_data_c
25cef 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ount,.  sqlite3_
25cf0 64 62 5f 68 61 6e 64 6c 65 2c 0a 20 20 73 71 6c  db_handle,.  sql
25cf1 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
25cf2 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 6e 61  b,.  sqlite3_ena
25cf3 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65  ble_shared_cache
25cf4 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72 63  ,.  sqlite3_errc
25cf5 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65  ode,.  sqlite3_e
25cf6 72 72 6d 73 67 2c 0a 20 20 73 71 6c 69 74 65 33  rrmsg,.  sqlite3
25cf7 5f 65 72 72 6d 73 67 31 36 2c 0a 20 20 73 71 6c  _errmsg16,.  sql
25cf8 69 74 65 33 5f 65 78 65 63 2c 0a 23 69 66 6e 64  ite3_exec,.#ifnd
25cf9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
25cfa 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69  EPRECATED.  sqli
25cfb 74 65 33 5f 65 78 70 69 72 65 64 2c 0a 23 65 6c  te3_expired,.#el
25cfc 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20  se.  0,.#endif. 
25cfd 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
25cfe 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  e,.  sqlite3_fre
25cff 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  e,.  sqlite3_fre
25d00 65 5f 74 61 62 6c 65 2c 0a 20 20 73 71 6c 69 74  e_table,.  sqlit
25d01 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69  e3_get_autocommi
25d02 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74  t,.  sqlite3_get
25d03 5f 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c 69  _auxdata,.  sqli
25d04 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 2c 0a 20  te3_get_table,. 
25d05 20 30 2c 20 20 20 20 20 2f 2a 20 57 61 73 20 73   0,     /* Was s
25d06 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65  qlite3_global_re
25d07 63 6f 76 65 72 28 29 2c 20 62 75 74 20 74 68 61  cover(), but tha
25d08 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65  t function is de
25d09 70 72 65 63 61 74 65 64 20 2a 2f 0a 20 20 73 71  precated */.  sq
25d0a 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 2c  lite3_interrupt,
25d0b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  .  sqlite3_last_
25d0c 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 0a 20 20  insert_rowid,.  
25d0d 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
25d0e 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69  on,.  sqlite3_li
25d0f 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 2c  bversion_number,
25d10 0a 20 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  .  sqlite3_mallo
25d11 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 70 72  c,.  sqlite3_mpr
25d12 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33 5f  intf,.  sqlite3_
25d13 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  open,.  sqlite3_
25d14 6f 70 65 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65  open16,.  sqlite
25d15 33 5f 70 72 65 70 61 72 65 2c 0a 20 20 73 71 6c  3_prepare,.  sql
25d16 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 2c 0a  ite3_prepare16,.
25d17 20 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c    sqlite3_profil
25d18 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 6f  e,.  sqlite3_pro
25d19 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 2c 0a 20  gress_handler,. 
25d1a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
25d1b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65  ,.  sqlite3_rese
25d1c 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  t,.  sqlite3_res
25d1d 75 6c 74 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69  ult_blob,.  sqli
25d1e 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
25d1f 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  e,.  sqlite3_res
25d20 75 6c 74 5f 65 72 72 6f 72 2c 0a 20 20 73 71 6c  ult_error,.  sql
25d21 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
25d22 72 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  r16,.  sqlite3_r
25d23 65 73 75 6c 74 5f 69 6e 74 2c 0a 20 20 73 71 6c  esult_int,.  sql
25d24 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
25d25 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  4,.  sqlite3_res
25d26 75 6c 74 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69  ult_null,.  sqli
25d27 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2c  te3_result_text,
25d28 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
25d29 74 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c 69  t_text16,.  sqli
25d2a 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
25d2b 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  6be,.  sqlite3_r
25d2c 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 2c 0a  esult_text16le,.
25d2d 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25d2e 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65  _value,.  sqlite
25d2f 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 2c  3_rollback_hook,
25d30 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  .  sqlite3_set_a
25d31 75 74 68 6f 72 69 7a 65 72 2c 0a 20 20 73 71 6c  uthorizer,.  sql
25d32 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
25d33 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ,.  sqlite3_snpr
25d34 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33 5f  intf,.  sqlite3_
25d35 73 74 65 70 2c 0a 20 20 73 71 6c 69 74 65 33 5f  step,.  sqlite3_
25d36 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
25d37 61 64 61 74 61 2c 0a 23 69 66 6e 64 65 66 20 53  adata,.#ifndef S
25d38 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
25d39 43 41 54 45 44 0a 20 20 73 71 6c 69 74 65 33 5f  CATED.  sqlite3_
25d3a 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 2c 0a  thread_cleanup,.
25d3b 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69  #else.  0,.#endi
25d3c 66 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  f.  sqlite3_tota
25d3d 6c 5f 63 68 61 6e 67 65 73 2c 0a 20 20 73 71 6c  l_changes,.  sql
25d3e 69 74 65 33 5f 74 72 61 63 65 2c 0a 23 69 66 6e  ite3_trace,.#ifn
25d3f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
25d40 44 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c  DEPRECATED.  sql
25d41 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
25d42 6e 64 69 6e 67 73 2c 0a 23 65 6c 73 65 0a 20 20  ndings,.#else.  
25d43 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  0,.#endif.  sqli
25d44 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 2c  te3_update_hook,
25d45 0a 20 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  .  sqlite3_user_
25d46 64 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f  data,.  sqlite3_
25d47 76 61 6c 75 65 5f 62 6c 6f 62 2c 0a 20 20 73 71  value_blob,.  sq
25d48 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
25d49 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  s,.  sqlite3_val
25d4a 75 65 5f 62 79 74 65 73 31 36 2c 0a 20 20 73 71  ue_bytes16,.  sq
25d4b 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
25d4c 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  le,.  sqlite3_va
25d4d 6c 75 65 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74  lue_int,.  sqlit
25d4e 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 2c 0a  e3_value_int64,.
25d4f 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
25d50 6e 75 6d 65 72 69 63 5f 74 79 70 65 2c 0a 20 20  numeric_type,.  
25d51 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
25d52 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  xt,.  sqlite3_va
25d53 6c 75 65 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  lue_text16,.  sq
25d54 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
25d55 31 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  16be,.  sqlite3_
25d56 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 2c 0a  value_text16le,.
25d57 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
25d58 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  type,.  sqlite3_
25d59 76 6d 70 72 69 6e 74 66 2c 0a 20 20 2f 2a 0a 20  vmprintf,.  /*. 
25d5a 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c   ** The original
25d5b 20 41 50 49 20 73 65 74 20 65 6e 64 73 20 68 65   API set ends he
25d5c 72 65 2e 20 20 41 6c 6c 20 65 78 74 65 6e 73 69  re.  All extensi
25d5d 6f 6e 73 20 63 61 6e 20 63 61 6c 6c 20 61 6e 79  ons can call any
25d5e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 41 50 49  .  ** of the API
25d5f 73 20 61 62 6f 76 65 20 70 72 6f 76 69 64 65 64  s above provided
25d60 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65   that the pointe
25d61 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20  r is not NULL.  
25d62 42 75 74 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20  But.  ** before 
25d63 63 61 6c 6c 69 6e 67 20 41 50 49 73 20 74 68 61  calling APIs tha
25d64 74 20 66 6f 6c 6c 6f 77 2c 20 65 78 74 65 6e 73  t follow, extens
25d65 69 6f 6e 20 73 68 6f 75 6c 64 20 63 68 65 63 6b  ion should check
25d66 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65   the.  ** sqlite
25d67 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
25d68 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20 73 75  ber() to make su
25d69 72 65 20 74 68 65 79 20 61 72 65 20 64 65 61 6c  re they are deal
25d6a 69 6e 67 20 77 69 74 68 0a 20 20 2a 2a 20 61 20  ing with.  ** a 
25d6b 6c 69 62 72 61 72 79 20 74 68 61 74 20 69 73 20  library that is 
25d6c 6e 65 77 20 65 6e 6f 75 67 68 20 74 6f 20 73 75  new enough to su
25d6d 70 70 6f 72 74 20 74 68 61 74 20 41 50 49 2e 0a  pport that API..
25d6e 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    **************
25d6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2f  ***********.  */
25d73 0a 20 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  .  sqlite3_overl
25d74 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 2c 0a 0a 20  oad_function,.. 
25d75 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 61   /*.  ** Added a
25d76 66 74 65 72 20 33 2e 33 2e 31 33 0a 20 20 2a 2f  fter 3.3.13.  */
25d77 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  .  sqlite3_prepa
25d78 72 65 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33  re_v2,.  sqlite3
25d79 5f 70 72 65 70 61 72 65 31 36 5f 76 32 2c 0a 20  _prepare16_v2,. 
25d7a 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62   sqlite3_clear_b
25d7b 69 6e 64 69 6e 67 73 2c 0a 0a 20 20 2f 2a 0a 20  indings,..  /*. 
25d7c 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e   ** Added for 3.
25d7d 34 2e 31 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  4.1.  */.  sqlit
25d7e 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
25d7f 5f 76 32 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  _v2,..  /*.  ** 
25d80 41 64 64 65 64 20 66 6f 72 20 33 2e 35 2e 30 0a  Added for 3.5.0.
25d81 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62    */.  sqlite3_b
25d82 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 2c 0a 20 20  ind_zeroblob,.  
25d83 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
25d84 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  es,.  sqlite3_bl
25d85 6f 62 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c 69  ob_close,.  sqli
25d86 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 2c 0a 20  te3_blob_open,. 
25d87 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
25d88 61 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  ad,.  sqlite3_bl
25d89 6f 62 5f 77 72 69 74 65 2c 0a 20 20 73 71 6c 69  ob_write,.  sqli
25d8a 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
25d8b 74 69 6f 6e 5f 76 32 2c 0a 20 20 73 71 6c 69 74  tion_v2,.  sqlit
25d8c 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 2c  e3_file_control,
25d8d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  .  sqlite3_memor
25d8e 79 5f 68 69 67 68 77 61 74 65 72 2c 0a 20 20 73  y_highwater,.  s
25d8f 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
25d90 65 64 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ed,.#ifdef SQLIT
25d91 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 20 20 30  E_MUTEX_OMIT.  0
25d92 2c 20 0a 20 20 30 2c 20 0a 20 20 30 2c 0a 20 20  , .  0, .  0,.  
25d93 30 2c 0a 20 20 30 2c 0a 23 65 6c 73 65 0a 20 20  0,.  0,.#else.  
25d94 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
25d95 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  loc,.  sqlite3_m
25d96 75 74 65 78 5f 65 6e 74 65 72 2c 0a 20 20 73 71  utex_enter,.  sq
25d97 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
25d98 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ,.  sqlite3_mute
25d99 78 5f 6c 65 61 76 65 2c 0a 20 20 73 71 6c 69 74  x_leave,.  sqlit
25d9a 65 33 5f 6d 75 74 65 78 5f 74 72 79 2c 0a 23 65  e3_mutex_try,.#e
25d9b 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6f  ndif.  sqlite3_o
25d9c 70 65 6e 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65  pen_v2,.  sqlite
25d9d 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
25d9e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
25d9f 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 2c 0a  lt_error_nomem,.
25da0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25da1 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 2c 0a 20  _error_toobig,. 
25da2 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 2c 0a   sqlite3_sleep,.
25da3 20 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68    sqlite3_soft_h
25da4 65 61 70 5f 6c 69 6d 69 74 2c 0a 20 20 73 71 6c  eap_limit,.  sql
25da5 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 2c 0a 20  ite3_vfs_find,. 
25da6 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67   sqlite3_vfs_reg
25da7 69 73 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33  ister,.  sqlite3
25da8 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 2c  _vfs_unregister,
25da9 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65  ..  /*.  ** Adde
25daa 64 20 66 6f 72 20 33 2e 35 2e 38 0a 20 20 2a 2f  d for 3.5.8.  */
25dab 0a 20 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  .  sqlite3_threa
25dac 64 73 61 66 65 2c 0a 20 20 73 71 6c 69 74 65 33  dsafe,.  sqlite3
25dad 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
25dae 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
25daf 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 2c 0a 20  lt_error_code,. 
25db0 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
25db1 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65 33  ntrol,.  sqlite3
25db2 5f 72 61 6e 64 6f 6d 6e 65 73 73 2c 0a 20 20 73  _randomness,.  s
25db3 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
25db4 62 5f 68 61 6e 64 6c 65 2c 0a 0a 20 20 2f 2a 0a  b_handle,..  /*.
25db5 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33    ** Added for 3
25db6 2e 36 2e 30 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  .6.0.  */.  sqli
25db7 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73  te3_extended_res
25db8 75 6c 74 5f 63 6f 64 65 73 2c 0a 20 20 73 71 6c  ult_codes,.  sql
25db9 69 74 65 33 5f 6c 69 6d 69 74 2c 0a 20 20 73 71  ite3_limit,.  sq
25dba 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 2c  lite3_next_stmt,
25dbb 0a 20 20 73 71 6c 69 74 65 33 5f 73 71 6c 2c 0a  .  sqlite3_sql,.
25dbc 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73    sqlite3_status
25dbd 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  ,.};../*.** Atte
25dbe 6d 70 74 20 74 6f 20 6c 6f 61 64 20 61 6e 20 53  mpt to load an S
25dbf 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20  QLite extension 
25dc0 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65  library containe
25dc1 64 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a  d in the file.**
25dc2 20 7a 46 69 6c 65 2e 20 20 54 68 65 20 65 6e 74   zFile.  The ent
25dc3 72 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f  ry point is zPro
25dc4 63 2e 20 20 7a 50 72 6f 63 20 6d 61 79 20 62 65  c.  zProc may be
25dc5 20 30 20 69 6e 20 77 68 69 63 68 20 63 61 73 65   0 in which case
25dc6 20 61 0a 2a 2a 20 64 65 66 61 75 6c 74 20 65 6e   a.** default en
25dc7 74 72 79 20 70 6f 69 6e 74 20 6e 61 6d 65 20 28  try point name (
25dc8 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
25dc9 6e 5f 69 6e 69 74 29 20 69 73 20 75 73 65 64 2e  n_init) is used.
25dca 20 20 55 73 65 0a 2a 2a 20 6f 66 20 74 68 65 20    Use.** of the 
25dcb 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 69 73 20  default name is 
25dcc 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a  recommended..**.
25dcd 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
25dce 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61  _OK on success a
25dcf 6e 64 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  nd SQLITE_ERROR 
25dd0 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  if something goe
25dd1 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49  s wrong..**.** I
25dd2 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
25dd3 73 20 61 6e 64 20 70 7a 45 72 72 4d 73 67 20 69  s and pzErrMsg i
25dd4 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 66 69  s not 0, then fi
25dd5 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74  ll *pzErrMsg wit
25dd6 68 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73  h .** error mess
25dd7 61 67 65 20 74 65 78 74 2e 20 20 54 68 65 20 63  age text.  The c
25dd8 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
25dd9 73 68 6f 75 6c 64 20 66 72 65 65 20 74 68 69 73  should free this
25dda 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 79 20 63 61   memory.** by ca
25ddb 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 44 62 46  lling sqlite3DbF
25ddc 72 65 65 28 64 62 2c 20 29 2e 0a 2a 2f 0a 73 74  ree(db, )..*/.st
25ddd 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
25dde 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28 0a 20  LoadExtension(. 
25ddf 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
25de0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74         /* Load t
25de1 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74  he extension int
25de2 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
25de3 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
25de4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
25de5 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
25de6 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
25de7 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65  ary containing e
25de8 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f  xtension */.  co
25de9 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c  nst char *zProc,
25dea 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69      /* Entry poi
25deb 6e 74 2e 20 20 55 73 65 20 22 73 71 6c 69 74 65  nt.  Use "sqlite
25dec 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
25ded 22 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72  " if 0 */.  char
25dee 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20   **pzErrMsg     
25def 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d    /* Put error m
25df0 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e  essage here if n
25df1 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  ot 0 */.){.  sql
25df2 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
25df3 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 76 6f 69   db->pVfs;.  voi
25df4 64 20 2a 68 61 6e 64 6c 65 3b 0a 20 20 69 6e 74  d *handle;.  int
25df5 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74 65   (*xInit)(sqlite
25df6 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20  3*,char**,const 
25df7 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
25df8 69 6e 65 73 2a 29 3b 0a 20 20 63 68 61 72 20 2a  ines*);.  char *
25df9 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a 20 20 76  zErrmsg = 0;.  v
25dfa 6f 69 64 20 2a 2a 61 48 61 6e 64 6c 65 3b 0a 0a  oid **aHandle;..
25dfb 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31 38 36    /* Ticket #186
25dfc 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61 20 63  3.  To avoid a c
25dfd 72 65 61 74 69 6e 67 20 73 65 63 75 72 69 74 79  reating security
25dfe 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 6c   problems for ol
25dff 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61  der.  ** applica
25e00 74 69 6f 6e 73 20 74 68 61 74 20 72 65 6c 69 6e  tions that relin
25e01 6b 20 61 67 61 69 6e 73 74 20 6e 65 77 65 72 20  k against newer 
25e02 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
25e03 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20 61 62 69  te, the.  ** abi
25e04 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c 6f 61 64  lity to run load
25e05 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 74 75  _extension is tu
25e06 72 6e 65 64 20 6f 66 66 20 62 79 20 64 65 66 61  rned off by defa
25e07 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a 20 6d  ult.  One.  ** m
25e08 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  ust call sqlite3
25e09 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74  _enable_load_ext
25e0a 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74 75 72 6e  ension() to turn
25e0b 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 20 20   on extension.  
25e0c 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20 4f 74 68  ** loading.  Oth
25e0d 65 72 77 69 73 65 20 79 6f 75 20 67 65 74 20 74  erwise you get t
25e0e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72  he following err
25e0f 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  or..  */.  if( (
25e10 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
25e11 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
25e12 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
25e13 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
25e14 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
25e15 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 6e  lite3_mprintf("n
25e16 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29 3b  ot authorized");
25e17 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
25e18 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
25e19 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50 72 6f 63    }..  if( zProc
25e1a 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63  ==0 ){.    zProc
25e1b 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65   = "sqlite3_exte
25e1c 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20 20 7d  nsion_init";.  }
25e1d 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20 73 71 6c  ..  handle = sql
25e1e 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 70 56 66  ite3OsDlOpen(pVf
25e1f 73 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 69 66 28  s, zFile);.  if(
25e20 20 68 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20   handle==0 ){.  
25e21 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29    if( pzErrMsg )
25e22 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a 45 72  {.      char zEr
25e23 72 5b 32 35 36 5d 3b 0a 20 20 20 20 20 20 7a 45  r[256];.      zE
25e24 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d  rr[sizeof(zErr)-
25e25 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  1] = '\0';.     
25e26 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
25e27 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31  f(sizeof(zErr)-1
25e28 2c 20 7a 45 72 72 2c 20 0a 20 20 20 20 20 20 20  , zErr, .       
25e29 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70     "unable to op
25e2a 65 6e 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  en shared librar
25e2b 79 20 5b 25 73 5d 22 2c 20 7a 46 69 6c 65 29 3b  y [%s]", zFile);
25e2c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
25e2d 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 73 69  DlError(pVfs, si
25e2e 7a 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20 7a 45  zeof(zErr)-1, zE
25e2f 72 72 29 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72  rr);.      *pzEr
25e30 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62  rMsg = sqlite3Db
25e31 53 74 72 44 75 70 28 30 2c 20 7a 45 72 72 29 3b  StrDup(0, zErr);
25e32 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
25e33 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
25e34 20 20 7d 0a 20 20 78 49 6e 69 74 20 3d 20 28 69    }.  xInit = (i
25e35 6e 74 28 2a 29 28 73 71 6c 69 74 65 33 2a 2c 63  nt(*)(sqlite3*,c
25e36 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69  har**,const sqli
25e37 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
25e38 2a 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  *)).            
25e39 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73         sqlite3Os
25e3a 44 6c 53 79 6d 28 70 56 66 73 2c 20 68 61 6e 64  DlSym(pVfs, hand
25e3b 6c 65 2c 20 7a 50 72 6f 63 29 3b 0a 20 20 69 66  le, zProc);.  if
25e3c 28 20 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20  ( xInit==0 ){.  
25e3d 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29    if( pzErrMsg )
25e3e 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a 45 72  {.      char zEr
25e3f 72 5b 32 35 36 5d 3b 0a 20 20 20 20 20 20 7a 45  r[256];.      zE
25e40 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d  rr[sizeof(zErr)-
25e41 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  1] = '\0';.     
25e42 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
25e43 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31  f(sizeof(zErr)-1
25e44 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20 20  , zErr,.        
25e45 20 20 22 6e 6f 20 65 6e 74 72 79 20 70 6f 69 6e    "no entry poin
25e46 74 20 5b 25 73 5d 20 69 6e 20 73 68 61 72 65 64  t [%s] in shared
25e47 20 6c 69 62 72 61 72 79 20 5b 25 73 5d 22 2c 20   library [%s]", 
25e48 7a 50 72 6f 63 2c 7a 46 69 6c 65 29 3b 0a 20 20  zProc,zFile);.  
25e49 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45      sqlite3OsDlE
25e4a 72 72 6f 72 28 70 56 66 73 2c 20 73 69 7a 65 6f  rror(pVfs, sizeo
25e4b 66 28 7a 45 72 72 29 2d 31 2c 20 7a 45 72 72 29  f(zErr)-1, zErr)
25e4c 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73  ;.      *pzErrMs
25e4d 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  g = sqlite3DbStr
25e4e 44 75 70 28 30 2c 20 7a 45 72 72 29 3b 0a 20 20  Dup(0, zErr);.  
25e4f 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43      sqlite3OsDlC
25e50 6c 6f 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c  lose(pVfs, handl
25e51 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  e);.    }.    re
25e52 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
25e53 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78  R;.  }else if( x
25e54 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73  Init(db, &zErrms
25e55 67 2c 20 26 73 71 6c 69 74 65 33 41 70 69 73 29  g, &sqlite3Apis)
25e56 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72   ){.    if( pzEr
25e57 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 2a 70  rMsg ){.      *p
25e58 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
25e59 33 5f 6d 70 72 69 6e 74 66 28 22 65 72 72 6f 72  3_mprintf("error
25e5a 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69   during initiali
25e5b 7a 61 74 69 6f 6e 3a 20 25 73 22 2c 20 7a 45 72  zation: %s", zEr
25e5c 72 6d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rmsg);.    }.   
25e5d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
25e5e 72 72 6d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69  rrmsg);.    sqli
25e5f 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66  te3OsDlClose(pVf
25e60 73 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20  s, handle);.    
25e61 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
25e62 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  ROR;.  }..  /* A
25e63 70 70 65 6e 64 20 74 68 65 20 6e 65 77 20 73 68  ppend the new sh
25e64 61 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e  ared library han
25e65 64 6c 65 20 74 6f 20 74 68 65 20 64 62 2d 3e 61  dle to the db->a
25e66 45 78 74 65 6e 73 69 6f 6e 20 61 72 72 61 79 2e  Extension array.
25e67 20 2a 2f 0a 20 20 61 48 61 6e 64 6c 65 20 3d 20   */.  aHandle = 
25e68 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
25e69 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 68  ero(db, sizeof(h
25e6a 61 6e 64 6c 65 29 2a 28 64 62 2d 3e 6e 45 78 74  andle)*(db->nExt
25e6b 65 6e 73 69 6f 6e 2b 31 29 29 3b 0a 20 20 69 66  ension+1));.  if
25e6c 28 20 61 48 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a  ( aHandle==0 ){.
25e6d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
25e6e 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69  E_NOMEM;.  }.  i
25e6f 66 28 20 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f  f( db->nExtensio
25e70 6e 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  n>0 ){.    memcp
25e71 79 28 61 48 61 6e 64 6c 65 2c 20 64 62 2d 3e 61  y(aHandle, db->a
25e72 45 78 74 65 6e 73 69 6f 6e 2c 20 73 69 7a 65 6f  Extension, sizeo
25e73 66 28 68 61 6e 64 6c 65 29 2a 64 62 2d 3e 6e 45  f(handle)*db->nE
25e74 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 7d 0a 20  xtension);.  }. 
25e75 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
25e76 62 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f  b, db->aExtensio
25e77 6e 29 3b 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e  n);.  db->aExten
25e78 73 69 6f 6e 20 3d 20 61 48 61 6e 64 6c 65 3b 0a  sion = aHandle;.
25e79 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f  .  db->aExtensio
25e7a 6e 5b 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e  n[db->nExtension
25e7b 2b 2b 5d 20 3d 20 68 61 6e 64 6c 65 3b 0a 20 20  ++] = handle;.  
25e7c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
25e7d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
25e7e 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f  nt sqlite3_load_
25e7f 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c  extension(.  sql
25e80 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
25e81 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65     /* Load the e
25e82 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68  xtension into th
25e83 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  is database conn
25e84 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73  ection */.  cons
25e85 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20  t char *zFile,  
25e86 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
25e87 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
25e88 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e  containing exten
25e89 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  sion */.  const 
25e8a 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20  char *zProc,    
25e8b 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20  /* Entry point. 
25e8c 20 55 73 65 20 22 73 71 6c 69 74 65 33 5f 65 78   Use "sqlite3_ex
25e8d 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66  tension_init" if
25e8e 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70   0 */.  char **p
25e8f 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a  zErrMsg       /*
25e90 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61   Put error messa
25e91 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30  ge here if not 0
25e92 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
25e93 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
25e94 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
25e95 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
25e96 33 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28 64  3LoadExtension(d
25e97 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c  b, zFile, zProc,
25e98 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71   pzErrMsg);.  sq
25e99 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
25e9a 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
25e9b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
25e9c 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f  .** Call this ro
25e9d 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65 20 64  utine when the d
25e9e 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
25e9f 6f 6e 20 69 73 20 63 6c 6f 73 69 6e 67 20 69 6e  on is closing in
25ea0 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 63 6c 65   order.** to cle
25ea1 61 6e 20 75 70 20 6c 6f 61 64 65 64 20 65 78 74  an up loaded ext
25ea2 65 6e 73 69 6f 6e 73 0a 2a 2f 0a 53 51 4c 49 54  ensions.*/.SQLIT
25ea3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
25ea4 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e  qlite3CloseExten
25ea5 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  sions(sqlite3 *d
25ea6 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61  b){.  int i;.  a
25ea7 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
25ea8 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
25ea9 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d  tex) );.  for(i=
25eaa 30 3b 20 69 3c 64 62 2d 3e 6e 45 78 74 65 6e 73  0; i<db->nExtens
25eab 69 6f 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  ion; i++){.    s
25eac 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28  qlite3OsDlClose(
25ead 64 62 2d 3e 70 56 66 73 2c 20 64 62 2d 3e 61 45  db->pVfs, db->aE
25eae 78 74 65 6e 73 69 6f 6e 5b 69 5d 29 3b 0a 20 20  xtension[i]);.  
25eaf 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
25eb0 65 28 64 62 2c 20 64 62 2d 3e 61 45 78 74 65 6e  e(db, db->aExten
25eb1 73 69 6f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  sion);.}../*.** 
25eb2 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c  Enable or disabl
25eb3 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64  e extension load
25eb4 69 6e 67 2e 20 20 45 78 74 65 6e 73 69 6f 6e 20  ing.  Extension 
25eb5 6c 6f 61 64 69 6e 67 20 69 73 20 64 69 73 61 62  loading is disab
25eb6 6c 65 64 20 62 79 0a 2a 2a 20 64 65 66 61 75 6c  led by.** defaul
25eb7 74 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f  t so as not to o
25eb8 70 65 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c  pen security hol
25eb9 65 73 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c  es in older appl
25eba 69 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  ications..*/.SQL
25ebb 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
25ebc 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
25ebd 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65  extension(sqlite
25ebe 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66  3 *db, int onoff
25ebf 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
25ec0 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
25ec1 65 78 29 3b 0a 20 20 69 66 28 20 6f 6e 6f 66 66  ex);.  if( onoff
25ec2 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67   ){.    db->flag
25ec3 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4c 6f 61 64  s |= SQLITE_Load
25ec4 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 65 6c  Extension;.  }el
25ec5 73 65 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67  se{.    db->flag
25ec6 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c 6f 61  s &= ~SQLITE_Loa
25ec7 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 0a  dExtension;.  }.
25ec8 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
25ec9 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
25eca 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
25ecb 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  E_OK;.}..#endif 
25ecc 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  /* SQLITE_OMIT_L
25ecd 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f  OAD_EXTENSION */
25ece 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6f  ../*.** The auto
25ecf 2d 65 78 74 65 6e 73 69 6f 6e 20 63 6f 64 65 20  -extension code 
25ed0 61 64 64 65 64 20 72 65 67 61 72 64 6c 65 73 73  added regardless
25ed1 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e   of whether or n
25ed2 6f 74 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20  ot extension.** 
25ed3 6c 6f 61 64 69 6e 67 20 69 73 20 73 75 70 70 6f  loading is suppo
25ed4 72 74 65 64 2e 20 20 57 65 20 6e 65 65 64 20 61  rted.  We need a
25ed5 20 64 75 6d 6d 79 20 73 71 6c 69 74 65 33 41 70   dummy sqlite3Ap
25ed6 69 73 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74  is pointer for t
25ed7 68 61 74 0a 2a 2a 20 63 6f 64 65 20 69 66 20 72  hat.** code if r
25ed8 65 67 75 6c 61 72 20 65 78 74 65 6e 73 69 6f 6e  egular extension
25ed9 20 6c 6f 61 64 69 6e 67 20 69 73 20 6e 6f 74 20   loading is not 
25eda 61 76 61 69 6c 61 62 6c 65 2e 20 20 54 68 69 73  available.  This
25edb 20 69 73 20 74 68 61 74 0a 2a 2a 20 64 75 6d 6d   is that.** dumm
25edc 79 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23 69  y pointer..*/.#i
25edd 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
25ede 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
25edf 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
25ee0 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
25ee1 73 20 73 71 6c 69 74 65 33 41 70 69 73 20 3d 20  s sqlite3Apis = 
25ee2 7b 20 30 20 7d 3b 0a 23 65 6e 64 69 66 0a 0a 0a  { 0 };.#endif...
25ee3 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
25ee4 69 6e 67 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73  ing object holds
25ee5 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61 75 74   the list of aut
25ee6 6f 6d 61 74 69 63 61 6c 6c 79 20 6c 6f 61 64 65  omatically loade
25ee7 64 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 73 2e  d.** extensions.
25ee8 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 69 73 74  .**.** This list
25ee9 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73   is shared acros
25eea 73 20 74 68 72 65 61 64 73 2e 20 20 54 68 65 20  s threads.  The 
25eeb 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
25eec 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d 75  TIC_MASTER.** mu
25eed 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64  tex must be held
25eee 20 77 68 69 6c 65 20 61 63 63 65 73 73 69 6e 67   while accessing
25eef 20 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2f 0a 74   this list..*/.t
25ef0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
25ef1 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74  lite3AutoExtList
25ef2 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c   sqlite3AutoExtL
25ef3 69 73 74 3b 0a 73 74 61 74 69 63 20 53 51 4c 49  ist;.static SQLI
25ef4 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71  TE_WSD struct sq
25ef5 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74  lite3AutoExtList
25ef6 20 7b 0a 20 20 69 6e 74 20 6e 45 78 74 3b 20 20   {.  int nExt;  
25ef7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
25ef8 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
25ef9 20 69 6e 20 61 45 78 74 5b 5d 20 2a 2f 20 20 20   in aExt[] */   
25efa 20 20 20 20 20 20 20 0a 20 20 76 6f 69 64 20 28         .  void (
25efb 2a 2a 61 45 78 74 29 28 76 6f 69 64 29 3b 20 20  **aExt)(void);  
25efc 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20   /* Pointers to 
25efd 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e  the extension in
25efe 69 74 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  it functions */.
25eff 7d 20 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74  } sqlite3Autoext
25f00 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a   = { 0, 0 };../*
25f01 20 54 68 65 20 22 77 73 64 41 75 74 6f 65 78 74   The "wsdAutoext
25f02 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73  " macro will res
25f03 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 75 74 6f  olve to the auto
25f04 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 73 74 61  extension.** sta
25f05 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77  te vector.  If w
25f06 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64  ritable static d
25f07 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74  ata is unsupport
25f08 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74  ed on the target
25f09 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20  ,.** we have to 
25f0a 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65  locate the state
25f0b 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74   vector at run-t
25f0c 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72  ime.  In the mor
25f0d 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65  e common.** case
25f0e 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20   where writable 
25f0f 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73  static data is s
25f10 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61  upported, wsdSta
25f11 74 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65  t can refer dire
25f12 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22  ctly.** to the "
25f13 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74 22 20  sqlite3Autoext" 
25f14 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63  state vector dec
25f15 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a  lared above..*/.
25f16 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
25f17 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20  IT_WSD.# define 
25f18 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 20 5c  wsdAutoextInit \
25f19 0a 20 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78  .  sqlite3AutoEx
25f1a 74 4c 69 73 74 20 2a 78 20 3d 20 26 47 4c 4f 42  tList *x = &GLOB
25f1b 41 4c 28 73 71 6c 69 74 65 33 41 75 74 6f 45 78  AL(sqlite3AutoEx
25f1c 74 4c 69 73 74 2c 73 71 6c 69 74 65 33 41 75 74  tList,sqlite3Aut
25f1d 6f 65 78 74 29 0a 23 20 64 65 66 69 6e 65 20 77  oext).# define w
25f1e 73 64 41 75 74 6f 65 78 74 20 78 5b 30 5d 0a 23  sdAutoext x[0].#
25f1f 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73  else.# define ws
25f20 64 41 75 74 6f 65 78 74 49 6e 69 74 0a 23 20 64  dAutoextInit.# d
25f21 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74  efine wsdAutoext
25f22 20 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74 0a   sqlite3Autoext.
25f23 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52  #endif.../*.** R
25f24 65 67 69 73 74 65 72 20 61 20 73 74 61 74 69 63  egister a static
25f25 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74 65  ally linked exte
25f26 6e 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 75  nsion that is au
25f27 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 6c  tomatically.** l
25f28 6f 61 64 65 64 20 62 79 20 65 76 65 72 79 20 6e  oaded by every n
25f29 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ew database conn
25f2a 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
25f2b 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
25f2c 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e  3_auto_extension
25f2d 28 76 6f 69 64 20 28 2a 78 49 6e 69 74 29 28 76  (void (*xInit)(v
25f2e 6f 69 64 29 29 7b 0a 20 20 69 6e 74 20 72 63 20  oid)){.  int rc 
25f2f 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66  = SQLITE_OK;.#if
25f30 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
25f31 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d  _AUTOINIT.  rc =
25f32 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
25f33 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20  ize();.  if( rc 
25f34 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
25f35 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  ;.  }else.#endif
25f36 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  .  {.    int i;.
25f37 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
25f38 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65  DSAFE.    sqlite
25f39 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d  3_mutex *mutex =
25f3a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
25f3b 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
25f3c 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
25f3d 23 65 6e 64 69 66 0a 20 20 20 20 77 73 64 41 75  #endif.    wsdAu
25f3e 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 20 20 73  toextInit;.    s
25f3f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
25f40 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 66  er(mutex);.    f
25f41 6f 72 28 69 3d 30 3b 20 69 3c 77 73 64 41 75 74  or(i=0; i<wsdAut
25f42 6f 65 78 74 2e 6e 45 78 74 3b 20 69 2b 2b 29 7b  oext.nExt; i++){
25f43 0a 20 20 20 20 20 20 69 66 28 20 77 73 64 41 75  .      if( wsdAu
25f44 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3d 3d 78  toext.aExt[i]==x
25f45 49 6e 69 74 20 29 20 62 72 65 61 6b 3b 0a 20 20  Init ) break;.  
25f46 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3d 3d 77    }.    if( i==w
25f47 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 29  sdAutoext.nExt )
25f48 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74  {.      int nByt
25f49 65 20 3d 20 28 77 73 64 41 75 74 6f 65 78 74 2e  e = (wsdAutoext.
25f4a 6e 45 78 74 2b 31 29 2a 73 69 7a 65 6f 66 28 77  nExt+1)*sizeof(w
25f4b 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 30  sdAutoext.aExt[0
25f4c 5d 29 3b 0a 20 20 20 20 20 20 76 6f 69 64 20 28  ]);.      void (
25f4d 2a 2a 61 4e 65 77 29 28 76 6f 69 64 29 3b 0a 20  **aNew)(void);. 
25f4e 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69       aNew = sqli
25f4f 74 65 33 5f 72 65 61 6c 6c 6f 63 28 77 73 64 41  te3_realloc(wsdA
25f50 75 74 6f 65 78 74 2e 61 45 78 74 2c 20 6e 42 79  utoext.aExt, nBy
25f51 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61  te);.      if( a
25f52 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  New==0 ){.      
25f53 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
25f54 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  MEM;.      }else
25f55 7b 0a 20 20 20 20 20 20 20 20 77 73 64 41 75 74  {.        wsdAut
25f56 6f 65 78 74 2e 61 45 78 74 20 3d 20 61 4e 65 77  oext.aExt = aNew
25f57 3b 0a 20 20 20 20 20 20 20 20 77 73 64 41 75 74  ;.        wsdAut
25f58 6f 65 78 74 2e 61 45 78 74 5b 77 73 64 41 75 74  oext.aExt[wsdAut
25f59 6f 65 78 74 2e 6e 45 78 74 5d 20 3d 20 78 49 6e  oext.nExt] = xIn
25f5a 69 74 3b 0a 20 20 20 20 20 20 20 20 77 73 64 41  it;.        wsdA
25f5b 75 74 6f 65 78 74 2e 6e 45 78 74 2b 2b 3b 0a 20  utoext.nExt++;. 
25f5c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
25f5d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
25f5e 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 20  eave(mutex);.   
25f5f 20 61 73 73 65 72 74 28 20 28 72 63 26 30 78 66   assert( (rc&0xf
25f60 66 29 3d 3d 72 63 20 29 3b 0a 20 20 20 20 72 65  f)==rc );.    re
25f61 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a  turn rc;.  }.}..
25f62 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20  /*.** Reset the 
25f63 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73  automatic extens
25f64 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 65 63 68  ion loading mech
25f65 61 6e 69 73 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  anism..*/.SQLITE
25f66 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
25f67 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74  3_reset_auto_ext
25f68 65 6e 73 69 6f 6e 28 76 6f 69 64 29 7b 0a 23 69  ension(void){.#i
25f69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
25f6a 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28  T_AUTOINIT.  if(
25f6b 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
25f6c 69 7a 65 28 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b  ize()==SQLITE_OK
25f6d 20 29 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 23 69   ).#endif.  {.#i
25f6e 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
25f6f 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  AFE.    sqlite3_
25f70 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73  mutex *mutex = s
25f71 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
25f72 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
25f73 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65  ATIC_MASTER);.#e
25f74 6e 64 69 66 0a 20 20 20 20 77 73 64 41 75 74 6f  ndif.    wsdAuto
25f75 65 78 74 49 6e 69 74 3b 0a 20 20 20 20 73 71 6c  extInit;.    sql
25f76 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
25f77 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c  (mutex);.    sql
25f78 69 74 65 33 5f 66 72 65 65 28 77 73 64 41 75 74  ite3_free(wsdAut
25f79 6f 65 78 74 2e 61 45 78 74 29 3b 0a 20 20 20 20  oext.aExt);.    
25f7a 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 20  wsdAutoext.aExt 
25f7b 3d 20 30 3b 0a 20 20 20 20 77 73 64 41 75 74 6f  = 0;.    wsdAuto
25f7c 65 78 74 2e 6e 45 78 74 20 3d 20 30 3b 0a 20 20  ext.nExt = 0;.  
25f7d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
25f7e 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20  leave(mutex);.  
25f7f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20  }.}../*.** Load 
25f80 61 6c 6c 20 61 75 74 6f 6d 61 74 69 63 20 65 78  all automatic ex
25f81 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  tensions..*/.SQL
25f82 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
25f83 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45  sqlite3AutoLoadE
25f84 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65  xtensions(sqlite
25f85 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b  3 *db){.  int i;
25f86 0a 20 20 69 6e 74 20 67 6f 20 3d 20 31 3b 0a 20  .  int go = 1;. 
25f87 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
25f88 5f 4f 4b 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e  _OK;.  int (*xIn
25f89 69 74 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61  it)(sqlite3*,cha
25f8a 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65  r**,const sqlite
25f8b 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29  3_api_routines*)
25f8c 3b 0a 0a 20 20 77 73 64 41 75 74 6f 65 78 74 49  ;..  wsdAutoextI
25f8d 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 41 75  nit;.  if( wsdAu
25f8e 74 6f 65 78 74 2e 6e 45 78 74 3d 3d 30 20 29 7b  toext.nExt==0 ){
25f8f 0a 20 20 20 20 2f 2a 20 43 6f 6d 6d 6f 6e 20 63  .    /* Common c
25f90 61 73 65 3a 20 65 61 72 6c 79 20 6f 75 74 20 77  ase: early out w
25f91 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 76  ithout every hav
25f92 69 6e 67 20 74 6f 20 61 63 71 75 69 72 65 20 61  ing to acquire a
25f93 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 72 65   mutex */.    re
25f94 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
25f95 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 67    }.  for(i=0; g
25f96 6f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61  o; i++){.    cha
25f97 72 20 2a 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a  r *zErrmsg = 0;.
25f98 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
25f99 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65  DSAFE.    sqlite
25f9a 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d  3_mutex *mutex =
25f9b 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
25f9c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
25f9d 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
25f9e 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
25f9f 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
25fa0 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 69  utex);.    if( i
25fa1 3e 3d 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78  >=wsdAutoext.nEx
25fa2 74 20 29 7b 0a 20 20 20 20 20 20 78 49 6e 69 74  t ){.      xInit
25fa3 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f 20 3d   = 0;.      go =
25fa4 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
25fa5 20 20 20 20 20 78 49 6e 69 74 20 3d 20 28 69 6e       xInit = (in
25fa6 74 28 2a 29 28 73 71 6c 69 74 65 33 2a 2c 63 68  t(*)(sqlite3*,ch
25fa7 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74  ar**,const sqlit
25fa8 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a  e3_api_routines*
25fa9 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  )).             
25faa 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
25fab 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  [i];.    }.    s
25fac 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
25fad 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 69  ve(mutex);.    i
25fae 66 28 20 78 49 6e 69 74 20 26 26 20 78 49 6e 69  f( xInit && xIni
25faf 74 28 64 62 2c 20 26 7a 45 72 72 6d 73 67 2c 20  t(db, &zErrmsg, 
25fb0 26 73 71 6c 69 74 65 33 41 70 69 73 29 20 29 7b  &sqlite3Apis) ){
25fb1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
25fb2 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45  ror(db, SQLITE_E
25fb3 52 52 4f 52 2c 0a 20 20 20 20 20 20 20 20 20 20  RROR,.          
25fb4 20 20 22 61 75 74 6f 6d 61 74 69 63 20 65 78 74    "automatic ext
25fb5 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 66  ension loading f
25fb6 61 69 6c 65 64 3a 20 25 73 22 2c 20 7a 45 72 72  ailed: %s", zErr
25fb7 6d 73 67 29 3b 0a 20 20 20 20 20 20 67 6f 20 3d  msg);.      go =
25fb8 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   0;.      rc = S
25fb9 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
25fba 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
25fbb 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 7d 0a  zErrmsg);.    }.
25fbc 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
25fbd 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
25fbe 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f 61 64 65  *** End of loade
25fbf 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xt.c ***********
25fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fc2 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
25fc3 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
25fc4 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ragma.c ********
25fc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fc6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fc7 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41  **/./*.** 2003 A
25fc8 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  pril 6.**.** The
25fc9 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
25fca 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
25fcb 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
25fcc 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
25fcd 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
25fce 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
25fcf 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
25fd0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
25fd1 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
25fd2 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
25fd3 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
25fd4 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
25fd5 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
25fd6 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
25fd7 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
25fd8 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
25fd9 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
25fda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fdc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fdd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fde 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
25fdf 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
25fe0 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
25fe1 6e 74 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f  nt the PRAGMA co
25fe2 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  mmand..**.** $Id
25fe3 3a 20 70 72 61 67 6d 61 2e 63 2c 76 20 31 2e 32  : pragma.c,v 1.2
25fe4 30 39 20 32 30 30 39 2f 30 34 2f 30 37 20 32 32  09 2009/04/07 22
25fe5 3a 30 35 3a 34 33 20 64 72 68 20 45 78 70 20 24  :05:43 drh Exp $
25fe6 0a 2a 2f 0a 0a 2f 2a 20 49 67 6e 6f 72 65 20 74  .*/../* Ignore t
25fe7 68 69 73 20 77 68 6f 6c 65 20 66 69 6c 65 20 69  his whole file i
25fe8 66 20 70 72 61 67 6d 61 73 20 61 72 65 20 64 69  f pragmas are di
25fe9 73 61 62 6c 65 64 0a 2a 2f 0a 23 69 66 20 21 64  sabled.*/.#if !d
25fea 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
25feb 49 54 5f 50 52 41 47 4d 41 29 20 26 26 20 21 64  IT_PRAGMA) && !d
25fec 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
25fed 49 54 5f 50 41 52 53 45 52 29 0a 0a 2f 2a 0a 2a  IT_PARSER)../*.*
25fee 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20  * Interpret the 
25fef 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20  given string as 
25ff0 61 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2e 20  a safety level. 
25ff1 20 52 65 74 75 72 6e 20 30 20 66 6f 72 20 4f 46   Return 0 for OF
25ff2 46 2c 0a 2a 2a 20 31 20 66 6f 72 20 4f 4e 20 6f  F,.** 1 for ON o
25ff3 72 20 4e 4f 52 4d 41 4c 20 61 6e 64 20 32 20 66  r NORMAL and 2 f
25ff4 6f 72 20 46 55 4c 4c 2e 20 20 52 65 74 75 72 6e  or FULL.  Return
25ff5 20 31 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20   1 for an empty 
25ff6 6f 72 20 0a 2a 2a 20 75 6e 72 65 63 6f 67 6e 69  or .** unrecogni
25ff7 7a 65 64 20 73 74 72 69 6e 67 20 61 72 67 75 6d  zed string argum
25ff8 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ent..**.** Note 
25ff9 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20  that the values 
25ffa 72 65 74 75 72 6e 65 64 20 61 72 65 20 6f 6e 65  returned are one
25ffb 20 6c 65 73 73 20 74 68 61 74 20 74 68 65 20 76   less that the v
25ffc 61 6c 75 65 73 20 74 68 61 74 0a 2a 2a 20 73 68  alues that.** sh
25ffd 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 69  ould be passed i
25ffe 6e 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65  nto sqlite3Btree
25fff 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 29  SetSafetyLevel()
26000 2e 20 20 54 68 65 20 69 73 20 64 6f 6e 65 0a 2a  .  The is done.*
26001 2a 20 74 6f 20 73 75 70 70 6f 72 74 20 6c 65 67  * to support leg
26002 61 63 79 20 53 51 4c 20 63 6f 64 65 2e 20 20 54  acy SQL code.  T
26003 68 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20  he safety level 
26004 75 73 65 64 20 74 6f 20 62 65 20 62 6f 6f 6c 65  used to be boole
26005 61 6e 0a 2a 2a 20 61 6e 64 20 6f 6c 64 65 72 20  an.** and older 
26006 73 63 72 69 70 74 73 20 6d 61 79 20 68 61 76 65  scripts may have
26007 20 75 73 65 64 20 6e 75 6d 62 65 72 73 20 30 20   used numbers 0 
26008 66 6f 72 20 4f 46 46 20 61 6e 64 20 31 20 66 6f  for OFF and 1 fo
26009 72 20 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  r ON..*/.static 
2600a 75 38 20 67 65 74 53 61 66 65 74 79 4c 65 76 65  u8 getSafetyLeve
2600b 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  l(const char *z)
2600c 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2600d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2600e 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33 34  * 123456789 1234
2600f 35 36 37 38 39 20 2a 2f 0a 20 20 73 74 61 74 69  56789 */.  stati
26010 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 54 65  c const char zTe
26011 78 74 5b 5d 20 3d 20 22 6f 6e 6f 66 66 61 6c 73  xt[] = "onoffals
26012 65 79 65 73 74 72 75 65 66 75 6c 6c 22 3b 0a 20  eyestruefull";. 
26013 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
26014 20 69 4f 66 66 73 65 74 5b 5d 20 3d 20 7b 30 2c   iOffset[] = {0,
26015 20 31 2c 20 32 2c 20 34 2c 20 39 2c 20 31 32 2c   1, 2, 4, 9, 12,
26016 20 31 36 7d 3b 0a 20 20 73 74 61 74 69 63 20 63   16};.  static c
26017 6f 6e 73 74 20 75 38 20 69 4c 65 6e 67 74 68 5b  onst u8 iLength[
26018 5d 20 3d 20 7b 32 2c 20 32 2c 20 33 2c 20 35 2c  ] = {2, 2, 3, 5,
26019 20 33 2c 20 34 2c 20 34 7d 3b 0a 20 20 73 74 61   3, 4, 4};.  sta
2601a 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69 56 61  tic const u8 iVa
2601b 6c 75 65 5b 5d 20 3d 20 20 7b 31 2c 20 30 2c 20  lue[] =  {1, 0, 
2601c 30 2c 20 30 2c 20 31 2c 20 31 2c 20 32 7d 3b 0a  0, 0, 1, 1, 2};.
2601d 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66    int i, n;.  if
2601e 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
2601f 28 2a 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75  (*z) ){.    retu
26020 72 6e 20 28 75 38 29 61 74 6f 69 28 7a 29 3b 0a  rn (u8)atoi(z);.
26021 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65    }.  n = sqlite
26022 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20  3Strlen30(z);.  
26023 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
26024 53 69 7a 65 28 69 4c 65 6e 67 74 68 29 3b 20 69  Size(iLength); i
26025 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 4c 65  ++){.    if( iLe
26026 6e 67 74 68 5b 69 5d 3d 3d 6e 20 26 26 20 73 71  ngth[i]==n && sq
26027 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 26 7a  lite3StrNICmp(&z
26028 54 65 78 74 5b 69 4f 66 66 73 65 74 5b 69 5d 5d  Text[iOffset[i]]
26029 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ,z,n)==0 ){.    
2602a 20 20 72 65 74 75 72 6e 20 69 56 61 6c 75 65 5b    return iValue[
2602b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  i];.    }.  }.  
2602c 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
2602d 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65  ** Interpret the
2602e 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73   given string as
2602f 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65   a boolean value
26030 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 67  ..*/.static u8 g
26031 65 74 42 6f 6f 6c 65 61 6e 28 63 6f 6e 73 74 20  etBoolean(const 
26032 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74 75  char *z){.  retu
26033 72 6e 20 67 65 74 53 61 66 65 74 79 4c 65 76 65  rn getSafetyLeve
26034 6c 28 7a 29 26 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  l(z)&1;.}../*.**
26035 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67   Interpret the g
26036 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61  iven string as a
26037 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 76 61   locking mode va
26038 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lue..*/.static i
26039 6e 74 20 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f 64  nt getLockingMod
2603a 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  e(const char *z)
2603b 7b 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20  {.  if( z ){.   
2603c 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
2603d 74 72 49 43 6d 70 28 7a 2c 20 22 65 78 63 6c 75  trICmp(z, "exclu
2603e 73 69 76 65 22 29 20 29 20 72 65 74 75 72 6e 20  sive") ) return 
2603f 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
26040 45 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20  E_EXCLUSIVE;.   
26041 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
26042 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f 72 6d 61  trICmp(z, "norma
26043 6c 22 29 20 29 20 72 65 74 75 72 6e 20 50 41 47  l") ) return PAG
26044 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
26045 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 72 65 74  ORMAL;.  }.  ret
26046 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  urn PAGER_LOCKIN
26047 47 4d 4f 44 45 5f 51 55 45 52 59 3b 0a 7d 0a 0a  GMODE_QUERY;.}..
26048 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
26049 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f  MIT_AUTOVACUUM./
2604a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
2604b 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20  he given string 
2604c 61 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  as an auto-vacuu
2604d 6d 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a  m mode value..**
2604e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
2604f 67 20 73 74 72 69 6e 67 73 2c 20 22 6e 6f 6e 65  g strings, "none
26050 22 2c 20 22 66 75 6c 6c 22 20 61 6e 64 20 22 69  ", "full" and "i
26051 6e 63 72 65 6d 65 6e 74 61 6c 22 20 61 72 65 20  ncremental" are 
26052 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2c 20  .** acceptable, 
26053 61 73 20 61 72 65 20 74 68 65 69 72 20 6e 75 6d  as are their num
26054 65 72 69 63 20 65 71 75 69 76 61 6c 65 6e 74 73  eric equivalents
26055 3a 20 30 2c 20 31 20 61 6e 64 20 32 20 72 65 73  : 0, 1 and 2 res
26056 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 73 74  pectively..*/.st
26057 61 74 69 63 20 69 6e 74 20 67 65 74 41 75 74 6f  atic int getAuto
26058 56 61 63 75 75 6d 28 63 6f 6e 73 74 20 63 68 61  Vacuum(const cha
26059 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  r *z){.  int i;.
2605a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33    if( 0==sqlite3
2605b 53 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f 6e 65  StrICmp(z, "none
2605c 22 29 20 29 20 72 65 74 75 72 6e 20 42 54 52 45  ") ) return BTRE
2605d 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e  E_AUTOVACUUM_NON
2605e 45 3b 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69  E;.  if( 0==sqli
2605f 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 66  te3StrICmp(z, "f
26060 75 6c 6c 22 29 20 29 20 72 65 74 75 72 6e 20 42  ull") ) return B
26061 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
26062 46 55 4c 4c 3b 0a 20 20 69 66 28 20 30 3d 3d 73  FULL;.  if( 0==s
26063 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
26064 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 29 20   "incremental") 
26065 29 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41  ) return BTREE_A
26066 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 3b 0a  UTOVACUUM_INCR;.
26067 20 20 69 20 3d 20 61 74 6f 69 28 7a 29 3b 0a 20    i = atoi(z);. 
26068 20 72 65 74 75 72 6e 20 28 75 38 29 28 28 69 3e   return (u8)((i>
26069 3d 30 26 26 69 3c 3d 32 29 3f 69 3a 30 29 3b 0a  =0&&i<=2)?i:0);.
2606a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64  }.#endif /* ifnd
2606b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2606c 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a 23 69  UTOVACUUM */..#i
2606d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2606e 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
2606f 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  /*.** Interpret 
26070 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
26071 20 61 73 20 61 20 74 65 6d 70 20 64 62 20 6c 6f   as a temp db lo
26072 63 61 74 69 6f 6e 2e 20 52 65 74 75 72 6e 20 31  cation. Return 1
26073 20 66 6f 72 20 66 69 6c 65 0a 2a 2a 20 62 61 63   for file.** bac
26074 6b 65 64 20 74 65 6d 70 6f 72 61 72 79 20 64 61  ked temporary da
26075 74 61 62 61 73 65 73 2c 20 32 20 66 6f 72 20 74  tabases, 2 for t
26076 68 65 20 52 65 64 2d 42 6c 61 63 6b 20 74 72 65  he Red-Black tre
26077 65 20 69 6e 20 6d 65 6d 6f 72 79 20 64 61 74 61  e in memory data
26078 62 61 73 65 0a 2a 2a 20 61 6e 64 20 30 20 74 6f  base.** and 0 to
26079 20 75 73 65 20 74 68 65 20 63 6f 6d 70 69 6c 65   use the compile
2607a 2d 74 69 6d 65 20 64 65 66 61 75 6c 74 2e 0a 2a  -time default..*
2607b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
2607c 54 65 6d 70 53 74 6f 72 65 28 63 6f 6e 73 74 20  TempStore(const 
2607d 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20  char *z){.  if( 
2607e 7a 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a 5b 30  z[0]>='0' && z[0
2607f 5d 3c 3d 27 32 27 20 29 7b 0a 20 20 20 20 72 65  ]<='2' ){.    re
26080 74 75 72 6e 20 7a 5b 30 5d 20 2d 20 27 30 27 3b  turn z[0] - '0';
26081 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
26082 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22  ite3StrICmp(z, "
26083 66 69 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  file")==0 ){.   
26084 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
26085 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  se if( sqlite3St
26086 72 49 43 6d 70 28 7a 2c 20 22 6d 65 6d 6f 72 79  rICmp(z, "memory
26087 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ")==0 ){.    ret
26088 75 72 6e 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a  urn 2;.  }else{.
26089 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2608a 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  }.}.#endif /* SQ
2608b 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d  LITE_PAGER_PRAGM
2608c 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  AS */..#ifndef S
2608d 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
2608e 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49  _PRAGMAS./*.** I
2608f 6e 76 61 6c 69 64 61 74 65 20 74 65 6d 70 20 73  nvalidate temp s
26090 74 6f 72 61 67 65 2c 20 65 69 74 68 65 72 20 77  torage, either w
26091 68 65 6e 20 74 68 65 20 74 65 6d 70 20 73 74 6f  hen the temp sto
26092 72 61 67 65 20 69 73 20 63 68 61 6e 67 65 64 0a  rage is changed.
26093 2a 2a 20 66 72 6f 6d 20 64 65 66 61 75 6c 74 2c  ** from default,
26094 20 6f 72 20 77 68 65 6e 20 27 66 69 6c 65 27 20   or when 'file' 
26095 61 6e 64 20 74 68 65 20 74 65 6d 70 5f 73 74 6f  and the temp_sto
26096 72 65 5f 64 69 72 65 63 74 6f 72 79 20 68 61 73  re_directory has
26097 20 63 68 61 6e 67 65 64 0a 2a 2f 0a 73 74 61 74   changed.*/.stat
26098 69 63 20 69 6e 74 20 69 6e 76 61 6c 69 64 61 74  ic int invalidat
26099 65 54 65 6d 70 53 74 6f 72 61 67 65 28 50 61 72  eTempStorage(Par
2609a 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73  se *pParse){.  s
2609b 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2609c 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64  rse->db;.  if( d
2609d 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 21 3d 30  b->aDb[1].pBt!=0
2609e 20 29 7b 0a 20 20 20 20 69 66 28 20 21 64 62 2d   ){.    if( !db-
2609f 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 7c 7c 20 73  >autoCommit || s
260a0 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52  qlite3BtreeIsInR
260a1 65 61 64 54 72 61 6e 73 28 64 62 2d 3e 61 44 62  eadTrans(db->aDb
260a2 5b 31 5d 2e 70 42 74 29 20 29 7b 0a 20 20 20 20  [1].pBt) ){.    
260a3 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
260a4 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f  g(pParse, "tempo
260a5 72 61 72 79 20 73 74 6f 72 61 67 65 20 63 61 6e  rary storage can
260a6 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 22  not be changed "
260a7 0a 20 20 20 20 20 20 20 20 22 66 72 6f 6d 20 77  .        "from w
260a8 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74  ithin a transact
260a9 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 72 65 74  ion");.      ret
260aa 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
260ab 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
260ac 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 64 62  te3BtreeClose(db
260ad 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 3b 0a 20  ->aDb[1].pBt);. 
260ae 20 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42     db->aDb[1].pB
260af 74 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  t = 0;.    sqlit
260b0 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
260b1 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
260b2 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
260b3 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  E_OK;.}.#endif /
260b4 2a 20 53 51 4c 49 54 45 5f 50 41 47 45 52 5f 50  * SQLITE_PAGER_P
260b5 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64  RAGMAS */..#ifnd
260b6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
260b7 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a  AGER_PRAGMAS./*.
260b8 2a 2a 20 49 66 20 74 68 65 20 54 45 4d 50 20 64  ** If the TEMP d
260b9 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 2c  atabase is open,
260ba 20 63 6c 6f 73 65 20 69 74 20 61 6e 64 20 6d 61   close it and ma
260bb 72 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20  rk the database 
260bc 73 63 68 65 6d 61 0a 2a 2a 20 61 73 20 6e 65 65  schema.** as nee
260bd 64 69 6e 67 20 72 65 6c 6f 61 64 69 6e 67 2e 20  ding reloading. 
260be 20 54 68 69 73 20 6d 75 73 74 20 62 65 20 64 6f   This must be do
260bf 6e 65 20 77 68 65 6e 20 75 73 69 6e 67 20 74 68  ne when using th
260c0 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  e SQLITE_TEMP_ST
260c1 4f 52 45 0a 2a 2a 20 6f 72 20 44 45 46 41 55 4c  ORE.** or DEFAUL
260c2 54 5f 54 45 4d 50 5f 53 54 4f 52 45 20 70 72 61  T_TEMP_STORE pra
260c3 67 6d 61 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  gmas..*/.static 
260c4 69 6e 74 20 63 68 61 6e 67 65 54 65 6d 70 53 74  int changeTempSt
260c5 6f 72 61 67 65 28 50 61 72 73 65 20 2a 70 50 61  orage(Parse *pPa
260c6 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rse, const char 
260c7 2a 7a 53 74 6f 72 61 67 65 54 79 70 65 29 7b 0a  *zStorageType){.
260c8 20 20 69 6e 74 20 74 73 20 3d 20 67 65 74 54 65    int ts = getTe
260c9 6d 70 53 74 6f 72 65 28 7a 53 74 6f 72 61 67 65  mpStore(zStorage
260ca 54 79 70 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Type);.  sqlite3
260cb 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
260cc 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 74 65 6d  b;.  if( db->tem
260cd 70 5f 73 74 6f 72 65 3d 3d 74 73 20 29 20 72 65  p_store==ts ) re
260ce 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
260cf 20 20 69 66 28 20 69 6e 76 61 6c 69 64 61 74 65    if( invalidate
260d0 54 65 6d 70 53 74 6f 72 61 67 65 28 20 70 50 61  TempStorage( pPa
260d1 72 73 65 20 29 20 21 3d 20 53 51 4c 49 54 45 5f  rse ) != SQLITE_
260d2 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
260d3 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
260d4 20 7d 0a 20 20 64 62 2d 3e 74 65 6d 70 5f 73 74   }.  db->temp_st
260d5 6f 72 65 20 3d 20 28 75 38 29 74 73 3b 0a 20 20  ore = (u8)ts;.  
260d6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
260d7 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
260d8 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d  LITE_PAGER_PRAGM
260d9 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  AS */../*.** Gen
260da 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65  erate code to re
260db 74 75 72 6e 20 61 20 73 69 6e 67 6c 65 20 69 6e  turn a single in
260dc 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a  teger value..*/.
260dd 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 74 75  static void retu
260de 72 6e 53 69 6e 67 6c 65 49 6e 74 28 50 61 72 73  rnSingleInt(Pars
260df 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
260e0 20 63 68 61 72 20 2a 7a 4c 61 62 65 6c 2c 20 69   char *zLabel, i
260e1 36 34 20 76 61 6c 75 65 29 7b 0a 20 20 56 64 62  64 value){.  Vdb
260e2 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
260e3 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
260e4 20 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70 50 61   int mem = ++pPa
260e5 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 69 36 34  rse->nMem;.  i64
260e6 20 2a 70 49 36 34 20 3d 20 73 71 6c 69 74 65 33   *pI64 = sqlite3
260e7 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 50 61 72  DbMallocRaw(pPar
260e8 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 76  se->db, sizeof(v
260e9 61 6c 75 65 29 29 3b 0a 20 20 69 66 28 20 70 49  alue));.  if( pI
260ea 36 34 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79  64 ){.    memcpy
260eb 28 70 49 36 34 2c 20 26 76 61 6c 75 65 2c 20 73  (pI64, &value, s
260ec 69 7a 65 6f 66 28 76 61 6c 75 65 29 29 3b 0a 20  izeof(value));. 
260ed 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
260ee 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74  AddOp4(v, OP_Int
260ef 36 34 2c 20 30 2c 20 6d 65 6d 2c 20 30 2c 20 28  64, 0, mem, 0, (
260f0 63 68 61 72 2a 29 70 49 36 34 2c 20 50 34 5f 49  char*)pI64, P4_I
260f1 4e 54 36 34 29 3b 0a 20 20 73 71 6c 69 74 65 33  NT64);.  sqlite3
260f2 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
260f3 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 1);.  sqlite3V
260f4 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
260f5 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
260f6 2c 20 7a 4c 61 62 65 6c 2c 20 53 51 4c 49 54 45  , zLabel, SQLITE
260f7 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69  _STATIC);.  sqli
260f8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
260f9 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 6d   OP_ResultRow, m
260fa 65 6d 2c 20 31 29 3b 0a 7d 0a 0a 23 69 66 6e 64  em, 1);.}..#ifnd
260fb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
260fc 4c 41 47 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a  LAG_PRAGMAS./*.*
260fd 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
260fe 66 20 7a 52 69 67 68 74 20 61 6e 64 20 7a 4c 65  f zRight and zLe
260ff 66 74 20 72 65 66 65 72 20 74 6f 20 61 20 70 72  ft refer to a pr
26100 61 67 6d 61 20 74 68 61 74 20 71 75 65 72 69 65  agma that querie
26101 73 0a 2a 2a 20 6f 72 20 63 68 61 6e 67 65 73 20  s.** or changes 
26102 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c 61 67 73  one of the flags
26103 20 69 6e 20 64 62 2d 3e 66 6c 61 67 73 2e 20 20   in db->flags.  
26104 52 65 74 75 72 6e 20 31 20 69 66 20 73 6f 20 61  Return 1 if so a
26105 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2a 20  nd 0 if not..** 
26106 41 6c 73 6f 2c 20 69 6d 70 6c 65 6d 65 6e 74 20  Also, implement 
26107 74 68 65 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 73  the pragma..*/.s
26108 74 61 74 69 63 20 69 6e 74 20 66 6c 61 67 50 72  tatic int flagPr
26109 61 67 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72  agma(Parse *pPar
2610a 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
2610b 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61  zLeft, const cha
2610c 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 73 74  r *zRight){.  st
2610d 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
2610e 74 20 73 50 72 61 67 6d 61 54 79 70 65 20 7b 0a  t sPragmaType {.
2610f 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
26110 7a 4e 61 6d 65 3b 20 20 2f 2a 20 4e 61 6d 65 20  zName;  /* Name 
26111 6f 66 20 74 68 65 20 70 72 61 67 6d 61 20 2a 2f  of the pragma */
26112 0a 20 20 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20  .    int mask;  
26113 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
26114 20 66 6f 72 20 74 68 65 20 64 62 2d 3e 66 6c 61   for the db->fla
26115 67 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20  gs value */.  } 
26116 61 50 72 61 67 6d 61 5b 5d 20 3d 20 7b 0a 20 20  aPragma[] = {.  
26117 20 20 7b 20 22 66 75 6c 6c 5f 63 6f 6c 75 6d 6e    { "full_column
26118 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20 20 20 20  _names",        
26119 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61  SQLITE_FullColNa
2611a 6d 65 73 20 20 7d 2c 0a 20 20 20 20 7b 20 22 73  mes  },.    { "s
2611b 68 6f 72 74 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  hort_column_name
2611c 73 22 2c 20 20 20 20 20 20 20 53 51 4c 49 54 45  s",       SQLITE
2611d 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 20 7d  _ShortColNames }
2611e 2c 0a 20 20 20 20 7b 20 22 63 6f 75 6e 74 5f 63  ,.    { "count_c
2611f 68 61 6e 67 65 73 22 2c 20 20 20 20 20 20 20 20  hanges",        
26120 20 20 20 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74      SQLITE_Count
26121 52 6f 77 73 20 20 20 20 20 7d 2c 0a 20 20 20 20  Rows     },.    
26122 7b 20 22 65 6d 70 74 79 5f 72 65 73 75 6c 74 5f  { "empty_result_
26123 63 61 6c 6c 62 61 63 6b 73 22 2c 20 20 20 53 51  callbacks",   SQ
26124 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63  LITE_NullCallbac
26125 6b 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 65 67  k  },.    { "leg
26126 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 22  acy_file_format"
26127 2c 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4c  ,       SQLITE_L
26128 65 67 61 63 79 46 69 6c 65 46 6d 74 20 7d 2c 0a  egacyFileFmt },.
26129 20 20 20 20 7b 20 22 66 75 6c 6c 66 73 79 6e 63      { "fullfsync
2612a 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
2612b 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79    SQLITE_FullFSy
2612c 6e 63 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20  nc     },.    { 
2612d 22 72 65 76 65 72 73 65 5f 75 6e 6f 72 64 65 72  "reverse_unorder
2612e 65 64 5f 73 65 6c 65 63 74 73 22 2c 20 53 51 4c  ed_selects", SQL
2612f 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72  ITE_ReverseOrder
26130 20 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c 49    },.#ifdef SQLI
26131 54 45 5f 44 45 42 55 47 0a 20 20 20 20 7b 20 22  TE_DEBUG.    { "
26132 73 71 6c 5f 74 72 61 63 65 22 2c 20 20 20 20 20  sql_trace",     
26133 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
26134 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 20  E_SqlTrace      
26135 7d 2c 0a 20 20 20 20 7b 20 22 76 64 62 65 5f 6c  },.    { "vdbe_l
26136 69 73 74 69 6e 67 22 2c 20 20 20 20 20 20 20 20  isting",        
26137 20 20 20 20 20 53 51 4c 49 54 45 5f 56 64 62 65       SQLITE_Vdbe
26138 4c 69 73 74 69 6e 67 20 20 20 7d 2c 0a 20 20 20  Listing   },.   
26139 20 7b 20 22 76 64 62 65 5f 74 72 61 63 65 22 2c   { "vdbe_trace",
2613a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
2613b 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 20  QLITE_VdbeTrace 
2613c 20 20 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 69      },.#endif.#i
2613d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2613e 54 5f 43 48 45 43 4b 0a 20 20 20 20 7b 20 22 69  T_CHECK.    { "i
2613f 67 6e 6f 72 65 5f 63 68 65 63 6b 5f 63 6f 6e 73  gnore_check_cons
26140 74 72 61 69 6e 74 73 22 2c 20 53 51 4c 49 54 45  traints", SQLITE
26141 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20 7d  _IgnoreChecks  }
26142 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20  ,.#endif.    /* 
26143 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73  The following is
26144 20 56 45 52 59 20 65 78 70 65 72 69 6d 65 6e 74   VERY experiment
26145 61 6c 20 2a 2f 0a 20 20 20 20 7b 20 22 77 72 69  al */.    { "wri
26146 74 61 62 6c 65 5f 73 63 68 65 6d 61 22 2c 20 20  table_schema",  
26147 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 57          SQLITE_W
26148 72 69 74 65 53 63 68 65 6d 61 7c 53 51 4c 49 54  riteSchema|SQLIT
26149 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 7d  E_RecoveryMode }
2614a 2c 0a 20 20 20 20 7b 20 22 6f 6d 69 74 5f 72 65  ,.    { "omit_re
2614b 61 64 6c 6f 63 6b 22 2c 20 20 20 20 20 20 20 20  adlock",        
2614c 20 20 20 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61      SQLITE_NoRea
2614d 64 6c 6f 63 6b 20 20 20 20 7d 2c 0a 0a 20 20 20  dlock    },..   
2614e 20 2f 2a 20 54 4f 44 4f 3a 20 4d 61 79 62 65 20   /* TODO: Maybe 
2614f 69 74 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20  it shouldn't be 
26150 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 68 61 6e  possible to chan
26151 67 65 20 74 68 65 20 52 65 61 64 55 6e 63 6f 6d  ge the ReadUncom
26152 6d 69 74 74 65 64 0a 20 20 20 20 2a 2a 20 66 6c  mitted.    ** fl
26153 61 67 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ag if there are 
26154 61 6e 79 20 61 63 74 69 76 65 20 73 74 61 74 65  any active state
26155 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 20 20 7b 20  ments. */.    { 
26156 22 72 65 61 64 5f 75 6e 63 6f 6d 6d 69 74 74 65  "read_uncommitte
26157 64 22 2c 20 20 20 20 20 20 20 20 20 53 51 4c 49  d",         SQLI
26158 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
26159 65 64 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74  ed },.  };.  int
2615a 20 69 3b 0a 20 20 63 6f 6e 73 74 20 73 74 72 75   i;.  const stru
2615b 63 74 20 73 50 72 61 67 6d 61 54 79 70 65 20 2a  ct sPragmaType *
2615c 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d  p;.  for(i=0, p=
2615d 61 50 72 61 67 6d 61 3b 20 69 3c 41 72 72 61 79  aPragma; i<Array
2615e 53 69 7a 65 28 61 50 72 61 67 6d 61 29 3b 20 69  Size(aPragma); i
2615f 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
26160 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
26161 28 7a 4c 65 66 74 2c 20 70 2d 3e 7a 4e 61 6d 65  (zLeft, p->zName
26162 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )==0 ){.      sq
26163 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
26164 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 56 64  se->db;.      Vd
26165 62 65 20 2a 76 3b 0a 20 20 20 20 20 20 76 20 3d  be *v;.      v =
26166 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
26167 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 61  pParse);.      a
26168 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 20 20  ssert( v!=0 );  
26169 2f 2a 20 41 6c 72 65 61 64 79 20 61 6c 6c 6f 63  /* Already alloc
2616a 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 50  ated by sqlite3P
2616b 72 61 67 6d 61 28 29 20 2a 2f 0a 20 20 20 20 20  ragma() */.     
2616c 20 69 66 28 20 41 4c 57 41 59 53 28 76 29 20 29   if( ALWAYS(v) )
2616d 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 52  {.        if( zR
2616e 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ight==0 ){.     
2616f 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c       returnSingl
26170 65 49 6e 74 28 70 50 61 72 73 65 2c 20 70 2d 3e  eInt(pParse, p->
26171 7a 4e 61 6d 65 2c 20 28 64 62 2d 3e 66 6c 61 67  zName, (db->flag
26172 73 20 26 20 70 2d 3e 6d 61 73 6b 29 21 3d 30 20  s & p->mask)!=0 
26173 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
26174 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
26175 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68  getBoolean(zRigh
26176 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  t) ){.          
26177 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 70    db->flags |= p
26178 2d 3e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20  ->mask;.        
26179 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2617a 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26       db->flags &
2617b 3d 20 7e 70 2d 3e 6d 61 73 6b 3b 0a 20 20 20 20  = ~p->mask;.    
2617c 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
2617d 20 20 20 2f 2a 20 4d 61 6e 79 20 6f 66 20 74 68     /* Many of th
2617e 65 20 66 6c 61 67 2d 70 72 61 67 6d 61 73 20 6d  e flag-pragmas m
2617f 6f 64 69 66 79 20 74 68 65 20 63 6f 64 65 20 67  odify the code g
26180 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20  enerated by the 
26181 53 51 4c 20 0a 20 20 20 20 20 20 20 20 20 20 2a  SQL .          *
26182 2a 20 63 6f 6d 70 69 6c 65 72 20 28 65 67 2e 20  * compiler (eg. 
26183 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 29 2e 20  count_changes). 
26184 53 6f 20 61 64 64 20 61 6e 20 6f 70 63 6f 64 65  So add an opcode
26185 20 74 6f 20 65 78 70 69 72 65 20 61 6c 6c 0a 20   to expire all. 
26186 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70           ** comp
26187 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  iled SQL stateme
26188 6e 74 73 20 61 66 74 65 72 20 6d 6f 64 69 66 79  nts after modify
26189 69 6e 67 20 61 20 70 72 61 67 6d 61 20 76 61 6c  ing a pragma val
2618a 75 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  ue..          */
2618b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2618c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2618d 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30 29  OP_Expire, 0, 0)
2618e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2618f 20 20 7d 0a 0a 20 20 20 20 20 20 72 65 74 75 72    }..      retur
26190 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
26191 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
26192 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
26193 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 20  IT_FLAG_PRAGMAS 
26194 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
26195 20 61 20 68 75 6d 61 6e 2d 72 65 61 64 61 62 6c   a human-readabl
26196 65 20 6e 61 6d 65 20 66 6f 72 20 61 20 63 6f 6e  e name for a con
26197 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69  straint resoluti
26198 6f 6e 20 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  on action..*/.st
26199 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2619a 2a 61 63 74 69 6f 6e 4e 61 6d 65 28 75 38 20 61  *actionName(u8 a
2619b 63 74 69 6f 6e 29 7b 0a 20 20 63 6f 6e 73 74 20  ction){.  const 
2619c 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 73  char *zName;.  s
2619d 77 69 74 63 68 28 20 61 63 74 69 6f 6e 20 29 7b  witch( action ){
2619e 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 53 65 74  .    case OE_Set
2619f 4e 75 6c 6c 3a 20 20 7a 4e 61 6d 65 20 3d 20 22  Null:  zName = "
261a0 53 45 54 20 4e 55 4c 4c 22 3b 20 20 20 20 20 20  SET NULL";      
261a1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
261a2 20 63 61 73 65 20 4f 45 5f 53 65 74 44 66 6c 74   case OE_SetDflt
261a3 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20  :  zName = "SET 
261a4 44 45 46 41 55 4c 54 22 3b 20 20 20 20 20 20 20  DEFAULT";       
261a5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
261a6 65 20 4f 45 5f 43 61 73 63 61 64 65 3a 20 20 7a  e OE_Cascade:  z
261a7 4e 61 6d 65 20 3d 20 22 43 41 53 43 41 44 45 22  Name = "CASCADE"
261a8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;             br
261a9 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
261aa 3a 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65  :          zName
261ab 20 3d 20 22 52 45 53 54 52 49 43 54 22 3b 20 20   = "RESTRICT";  
261ac 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
261ad 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61         assert( a
261ae 63 74 69 6f 6e 3d 3d 4f 45 5f 52 65 73 74 72 69  ction==OE_Restri
261af 63 74 20 29 3b 20 62 72 65 61 6b 3b 0a 20 20 7d  ct ); break;.  }
261b0 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b  .  return zName;
261b1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73  .}../*.** Proces
261b2 73 20 61 20 70 72 61 67 6d 61 20 73 74 61 74 65  s a pragma state
261b3 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 50 72  ment.  .**.** Pr
261b4 61 67 6d 61 73 20 61 72 65 20 6f 66 20 74 68 69  agmas are of thi
261b5 73 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  s form:.**.**   
261b6 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
261b7 61 73 65 2e 5d 69 64 20 5b 3d 20 76 61 6c 75 65  ase.]id [= value
261b8 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 65 6e  ].**.** The iden
261b9 74 69 66 69 65 72 20 6d 69 67 68 74 20 61 6c 73  tifier might als
261ba 6f 20 62 65 20 61 20 73 74 72 69 6e 67 2e 20 20  o be a string.  
261bb 54 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73  The value is a s
261bc 74 72 69 6e 67 2c 20 61 6e 64 0a 2a 2a 20 69 64  tring, and.** id
261bd 65 6e 74 69 66 69 65 72 2c 20 6f 72 20 61 20 6e  entifier, or a n
261be 75 6d 62 65 72 2e 20 20 49 66 20 6d 69 6e 75 73  umber.  If minus
261bf 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68  Flag is true, th
261c0 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 0a  en the value is.
261c1 2a 2a 20 61 20 6e 75 6d 62 65 72 20 74 68 61 74  ** a number that
261c2 20 77 61 73 20 70 72 65 63 65 64 65 64 20 62 79   was preceded by
261c3 20 61 20 6d 69 6e 75 73 20 73 69 67 6e 2e 0a 2a   a minus sign..*
261c4 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 65 66 74  *.** If the left
261c5 20 73 69 64 65 20 69 73 20 22 64 61 74 61 62 61   side is "databa
261c6 73 65 2e 69 64 22 20 74 68 65 6e 20 70 49 64 31  se.id" then pId1
261c7 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
261c8 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 70 49 64   name.** and pId
261c9 32 20 69 73 20 74 68 65 20 69 64 2e 20 20 49 66  2 is the id.  If
261ca 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 20 69   the left side i
261cb 73 20 6a 75 73 74 20 22 69 64 22 20 74 68 65 6e  s just "id" then
261cc 20 70 49 64 31 20 69 73 20 74 68 65 0a 2a 2a 20   pId1 is the.** 
261cd 69 64 20 61 6e 64 20 70 49 64 32 20 69 73 20 61  id and pId2 is a
261ce 6e 79 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2e  ny empty string.
261cf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
261d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
261d1 72 61 67 6d 61 28 0a 20 20 50 61 72 73 65 20 2a  ragma(.  Parse *
261d2 70 50 61 72 73 65 2c 20 0a 20 20 54 6f 6b 65 6e  pParse, .  Token
261d3 20 2a 70 49 64 31 2c 20 20 20 20 20 20 20 20 2f   *pId1,        /
261d4 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20  * First part of 
261d5 5b 64 61 74 61 62 61 73 65 2e 5d 69 64 20 66 69  [database.]id fi
261d6 65 6c 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  eld */.  Token *
261d7 70 49 64 32 2c 20 20 20 20 20 20 20 20 2f 2a 20  pId2,        /* 
261d8 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 5b  Second part of [
261d9 64 61 74 61 62 61 73 65 2e 5d 69 64 20 66 69 65  database.]id fie
261da 6c 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  ld, or NULL */. 
261db 20 54 6f 6b 65 6e 20 2a 70 56 61 6c 75 65 2c 20   Token *pValue, 
261dc 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 66 6f       /* Token fo
261dd 72 20 3c 76 61 6c 75 65 3e 2c 20 6f 72 20 4e 55  r <value>, or NU
261de 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 75  LL */.  int minu
261df 73 46 6c 61 67 20 20 20 20 20 20 20 2f 2a 20 54  sFlag       /* T
261e0 72 75 65 20 69 66 20 61 20 27 2d 27 20 73 69 67  rue if a '-' sig
261e1 6e 20 70 72 65 63 65 64 65 64 20 3c 76 61 6c 75  n preceded <valu
261e2 65 3e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20  e> */.){.  char 
261e3 2a 7a 4c 65 66 74 20 3d 20 30 3b 20 20 20 20 20  *zLeft = 0;     
261e4 20 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d 69 6e 61    /* Nul-termina
261e5 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ted UTF-8 string
261e6 20 3c 69 64 3e 20 2a 2f 0a 20 20 63 68 61 72 20   <id> */.  char 
261e7 2a 7a 52 69 67 68 74 20 3d 20 30 3b 20 20 20 20  *zRight = 0;    
261e8 20 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d 69 6e 61    /* Nul-termina
261e9 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ted UTF-8 string
261ea 20 3c 76 61 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c   <value>, or NUL
261eb 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  L */.  const cha
261ec 72 20 2a 7a 44 62 20 3d 20 30 3b 20 20 20 2f 2a  r *zDb = 0;   /*
261ed 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   The database na
261ee 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  me */.  Token *p
261ef 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Id;            /
261f0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 3c 69 64  * Pointer to <id
261f1 3e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  > token */.  int
261f2 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
261f3 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
261f4 69 6e 64 65 78 20 66 6f 72 20 3c 64 61 74 61 62  index for <datab
261f5 61 73 65 3e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ase> */.  sqlite
261f6 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
261f7 64 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20  db;.  Db *pDb;. 
261f8 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
261f9 65 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c 69 74  e->pVdbe = sqlit
261fa 65 33 56 64 62 65 43 72 65 61 74 65 28 64 62 29  e3VdbeCreate(db)
261fb 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72  ;.  if( v==0 ) r
261fc 65 74 75 72 6e 3b 0a 20 20 70 50 61 72 73 65 2d  eturn;.  pParse-
261fd 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 0a 20 20 2f 2a  >nMem = 2;..  /*
261fe 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 5b   Interpret the [
261ff 64 61 74 61 62 61 73 65 2e 5d 20 70 61 72 74 20  database.] part 
26200 6f 66 20 74 68 65 20 70 72 61 67 6d 61 20 73 74  of the pragma st
26201 61 74 65 6d 65 6e 74 2e 20 69 44 62 20 69 73 20  atement. iDb is 
26202 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f  the.  ** index o
26203 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
26204 68 69 73 20 70 72 61 67 6d 61 20 69 73 20 62 65  his pragma is be
26205 69 6e 67 20 61 70 70 6c 69 65 64 20 74 6f 20 69  ing applied to i
26206 6e 20 64 62 2e 61 44 62 5b 5d 2e 20 2a 2f 0a 20  n db.aDb[]. */. 
26207 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77   iDb = sqlite3Tw
26208 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65  oPartName(pParse
26209 2c 20 70 49 64 31 2c 20 70 49 64 32 2c 20 26 70  , pId1, pId2, &p
2620a 49 64 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30  Id);.  if( iDb<0
2620b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 44 62   ) return;.  pDb
2620c 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
2620d 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74  ;..  /* If the t
2620e 65 6d 70 20 64 61 74 61 62 61 73 65 20 68 61 73  emp database has
2620f 20 62 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79   been explicitly
26210 20 6e 61 6d 65 64 20 61 73 20 70 61 72 74 20 6f   named as part o
26211 66 20 74 68 65 20 0a 20 20 2a 2a 20 70 72 61 67  f the .  ** prag
26212 6d 61 2c 20 6d 61 6b 65 20 73 75 72 65 20 69 74  ma, make sure it
26213 20 69 73 20 6f 70 65 6e 2e 20 0a 20 20 2a 2f 0a   is open. .  */.
26214 20 20 69 66 28 20 69 44 62 3d 3d 31 20 26 26 20    if( iDb==1 && 
26215 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44  sqlite3OpenTempD
26216 61 74 61 62 61 73 65 28 70 50 61 72 73 65 29 20  atabase(pParse) 
26217 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
26218 20 7d 0a 0a 20 20 7a 4c 65 66 74 20 3d 20 73 71   }..  zLeft = sq
26219 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
2621a 65 6e 28 64 62 2c 20 70 49 64 29 3b 0a 20 20 69  en(db, pId);.  i
2621b 66 28 20 21 7a 4c 65 66 74 20 29 20 72 65 74 75  f( !zLeft ) retu
2621c 72 6e 3b 0a 20 20 69 66 28 20 6d 69 6e 75 73 46  rn;.  if( minusF
2621d 6c 61 67 20 29 7b 0a 20 20 20 20 7a 52 69 67 68  lag ){.    zRigh
2621e 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
2621f 74 66 28 64 62 2c 20 22 2d 25 54 22 2c 20 70 56  tf(db, "-%T", pV
26220 61 6c 75 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  alue);.  }else{.
26221 20 20 20 20 7a 52 69 67 68 74 20 3d 20 73 71 6c      zRight = sql
26222 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
26223 6e 28 64 62 2c 20 70 56 61 6c 75 65 29 3b 0a 20  n(db, pValue);. 
26224 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 49   }..  assert( pI
26225 64 32 20 29 3b 0a 20 20 7a 44 62 20 3d 20 70 49  d2 );.  zDb = pI
26226 64 32 2d 3e 6e 3e 30 20 3f 20 70 44 62 2d 3e 7a  d2->n>0 ? pDb->z
26227 4e 61 6d 65 20 3a 20 30 3b 0a 20 20 69 66 28 20  Name : 0;.  if( 
26228 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
26229 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2622a 50 52 41 47 4d 41 2c 20 7a 4c 65 66 74 2c 20 7a  PRAGMA, zLeft, z
2622b 52 69 67 68 74 2c 20 7a 44 62 29 20 29 7b 0a 20  Right, zDb) ){. 
2622c 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f     goto pragma_o
2622d 75 74 3b 0a 20 20 7d 0a 20 0a 23 69 66 6e 64 65  ut;.  }. .#ifnde
2622e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  f SQLITE_OMIT_PA
2622f 47 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a  GER_PRAGMAS.  /*
26230 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
26231 61 74 61 62 61 73 65 2e 5d 64 65 66 61 75 6c 74  atabase.]default
26232 5f 63 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a 2a  _cache_size.  **
26233 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
26234 73 65 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63 68  se.]default_cach
26235 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20  e_size=N.  **.  
26236 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72  ** The first for
26237 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75  m reports the cu
26238 72 72 65 6e 74 20 70 65 72 73 69 73 74 65 6e 74  rrent persistent
26239 20 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65   setting for the
2623a 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65  .  ** page cache
2623b 20 73 69 7a 65 2e 20 20 54 68 65 20 76 61 6c 75   size.  The valu
2623c 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68  e returned is th
2623d 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
2623e 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65 73 20 69   of.  ** pages i
2623f 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
26240 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f  .  The second fo
26241 72 6d 20 73 65 74 73 20 62 6f 74 68 20 74 68 65  rm sets both the
26242 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 70 61   current.  ** pa
26243 67 65 20 63 61 63 68 65 20 73 69 7a 65 20 76 61  ge cache size va
26244 6c 75 65 20 61 6e 64 20 74 68 65 20 70 65 72 73  lue and the pers
26245 69 73 74 65 6e 74 20 70 61 67 65 20 63 61 63 68  istent page cach
26246 65 20 73 69 7a 65 20 76 61 6c 75 65 0a 20 20 2a  e size value.  *
26247 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  * stored in the 
26248 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20  database file.. 
26249 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 65 66   **.  ** The def
2624a 61 75 6c 74 20 63 61 63 68 65 20 73 69 7a 65 20  ault cache size 
2624b 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 74  is stored in met
2624c 61 2d 76 61 6c 75 65 20 32 20 6f 66 20 70 61 67  a-value 2 of pag
2624d 65 20 31 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  e 1 of the.  ** 
2624e 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
2624f 54 68 65 20 63 61 63 68 65 20 73 69 7a 65 20 69  The cache size i
26250 73 20 61 63 74 75 61 6c 6c 79 20 74 68 65 20 61  s actually the a
26251 62 73 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f 66  bsolute value of
26252 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65 6d 6f 72  .  ** this memor
26253 79 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65  y location.  The
26254 20 73 69 67 6e 20 6f 66 20 6d 65 74 61 2d 76 61   sign of meta-va
26255 6c 75 65 20 32 20 64 65 74 65 72 6d 69 6e 65 73  lue 2 determines
26256 20 74 68 65 0a 20 20 2a 2a 20 73 79 6e 63 68 72   the.  ** synchr
26257 6f 6e 6f 75 73 20 73 65 74 74 69 6e 67 2e 20 20  onous setting.  
26258 41 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65  A negative value
26259 20 6d 65 61 6e 73 20 73 79 6e 63 68 72 6f 6e 6f   means synchrono
2625a 75 73 20 69 73 20 6f 66 66 0a 20 20 2a 2a 20 61  us is off.  ** a
2625b 6e 64 20 61 20 70 6f 73 69 74 69 76 65 20 76 61  nd a positive va
2625c 6c 75 65 20 6d 65 61 6e 73 20 73 79 6e 63 68 72  lue means synchr
2625d 6f 6e 6f 75 73 20 69 73 20 6f 6e 2e 0a 20 20 2a  onous is on..  *
2625e 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
2625f 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 64 65  trICmp(zLeft,"de
26260 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65  fault_cache_size
26261 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61  ")==0 ){.    sta
26262 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70  tic const VdbeOp
26263 4c 69 73 74 20 67 65 74 43 61 63 68 65 53 69 7a  List getCacheSiz
26264 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20  e[] = {.      { 
26265 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20  OP_ReadCookie,  
26266 30 2c 20 31 2c 20 20 20 20 20 20 20 20 32 7d 2c  0, 1,        2},
26267 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20    /* 0 */.      
26268 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20 20  { OP_IfPos,     
26269 20 20 31 2c 20 36 2c 20 20 20 20 20 20 20 20 30    1, 6,        0
2626a 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e  },.      { OP_In
2626b 74 65 67 65 72 2c 20 20 20 20 20 30 2c 20 32 2c  teger,     0, 2,
2626c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20          0},.    
2626d 20 20 7b 20 4f 50 5f 53 75 62 74 72 61 63 74 2c    { OP_Subtract,
2626e 20 20 20 20 31 2c 20 32 2c 20 20 20 20 20 20 20      1, 2,       
2626f 20 31 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f   1},.      { OP_
26270 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20  IfPos,       1, 
26271 36 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20  6,        0},.  
26272 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72      { OP_Integer
26273 2c 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20  ,     0, 1,     
26274 20 20 20 30 7d 2c 20 20 2f 2a 20 35 20 2a 2f 0a     0},  /* 5 */.
26275 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c        { OP_Resul
26276 74 52 6f 77 2c 20 20 20 31 2c 20 31 2c 20 20 20  tRow,   1, 1,   
26277 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a       0},.    };.
26278 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
26279 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
2627a 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
2627b 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
2627c 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  t;.    sqlite3Vd
2627d 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
2627e 44 62 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 52  Db);.    if( !zR
2627f 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71  ight ){.      sq
26280 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
26281 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20  ols(v, 1);.     
26282 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
26283 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
26284 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 61 63 68  NAME_NAME, "cach
26285 65 5f 73 69 7a 65 22 2c 20 53 51 4c 49 54 45 5f  e_size", SQLITE_
26286 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 70  STATIC);.      p
26287 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 32  Parse->nMem += 2
26288 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
26289 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
2628a 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
2628b 28 67 65 74 43 61 63 68 65 53 69 7a 65 29 2c 20  (getCacheSize), 
2628c 67 65 74 43 61 63 68 65 53 69 7a 65 29 3b 0a 20  getCacheSize);. 
2628d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2628e 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
2628f 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
26290 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
26291 31 28 76 2c 20 61 64 64 72 2b 35 2c 20 53 51 4c  1(v, addr+5, SQL
26292 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48  ITE_DEFAULT_CACH
26293 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 7d 65 6c  E_SIZE);.    }el
26294 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69  se{.      int si
26295 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74  ze = atoi(zRight
26296 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a  );.      if( siz
26297 65 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69  e<0 ) size = -si
26298 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ze;.      sqlite
26299 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2629a 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20  tion(pParse, 0, 
2629b 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
2629c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2629d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 73 69 7a   OP_Integer, siz
2629e 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  e, 1);.      sql
2629f 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
262a0 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c  , OP_ReadCookie,
262a1 20 69 44 62 2c 20 32 2c 20 32 29 3b 0a 20 20 20   iDb, 2, 2);.   
262a2 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
262a3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
262a4 50 5f 49 66 50 6f 73 2c 20 32 2c 20 30 29 3b 0a  P_IfPos, 2, 0);.
262a5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
262a6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
262a7 74 65 67 65 72 2c 20 2d 73 69 7a 65 2c 20 31 29  teger, -size, 1)
262a8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
262a9 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
262aa 64 64 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ddr);.      sqli
262ab 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
262ac 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69   OP_SetCookie, i
262ad 44 62 2c 20 32 2c 20 31 29 3b 0a 20 20 20 20 20  Db, 2, 1);.     
262ae 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63   pDb->pSchema->c
262af 61 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65  ache_size = size
262b0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
262b1 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65  treeSetCacheSize
262b2 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e  (pDb->pBt, pDb->
262b3 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
262b4 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  ize);.    }.  }e
262b5 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  lse..  /*.  **  
262b6 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
262b7 2e 5d 70 61 67 65 5f 73 69 7a 65 0a 20 20 2a 2a  .]page_size.  **
262b8 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
262b9 73 65 2e 5d 70 61 67 65 5f 73 69 7a 65 3d 4e 0a  se.]page_size=N.
262ba 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69    **.  ** The fi
262bb 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73  rst form reports
262bc 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74   the current set
262bd 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a  ting for the.  *
262be 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  * database page 
262bf 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20  size in bytes.  
262c0 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20  The second form 
262c1 73 65 74 73 20 74 68 65 0a 20 20 2a 2a 20 64 61  sets the.  ** da
262c2 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65  tabase page size
262c3 20 76 61 6c 75 65 2e 20 20 54 68 65 20 76 61 6c   value.  The val
262c4 75 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 73  ue can only be s
262c5 65 74 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 64  et if.  ** the d
262c6 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74 20  atabase has not 
262c7 79 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64  yet been created
262c8 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
262c9 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
262ca 74 2c 22 70 61 67 65 5f 73 69 7a 65 22 29 3d 3d  t,"page_size")==
262cb 30 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  0 ){.    Btree *
262cc 70 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a  pBt = pDb->pBt;.
262cd 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 21      assert( pBt!
262ce 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  =0 );.    if( !z
262cf 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69  Right ){.      i
262d0 6e 74 20 73 69 7a 65 20 3d 20 41 4c 57 41 59 53  nt size = ALWAYS
262d1 28 70 42 74 29 20 3f 20 73 71 6c 69 74 65 33 42  (pBt) ? sqlite3B
262d2 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28  treeGetPageSize(
262d3 70 42 74 29 20 3a 20 30 3b 0a 20 20 20 20 20 20  pBt) : 0;.      
262d4 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
262d5 70 50 61 72 73 65 2c 20 22 70 61 67 65 5f 73 69  pParse, "page_si
262d6 7a 65 22 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20  ze", size);.    
262d7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
262d8 4d 61 6c 6c 6f 63 20 6d 61 79 20 66 61 69 6c 20  Malloc may fail 
262d9 77 68 65 6e 20 73 65 74 74 69 6e 67 20 74 68 65  when setting the
262da 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 73 20 74   page-size, as t
262db 68 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72  here is an inter
262dc 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 62 75 66  nal.      ** buf
262dd 66 65 72 20 74 68 61 74 20 74 68 65 20 70 61 67  fer that the pag
262de 65 72 20 6d 6f 64 75 6c 65 20 72 65 73 69 7a 65  er module resize
262df 73 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  s using sqlite3_
262e0 72 65 61 6c 6c 6f 63 28 29 2e 0a 20 20 20 20 20  realloc()..     
262e1 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65   */.      db->ne
262e2 78 74 50 61 67 65 73 69 7a 65 20 3d 20 61 74 6f  xtPagesize = ato
262e3 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  i(zRight);.     
262e4 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45   if( SQLITE_NOME
262e5 4d 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 53  M==sqlite3BtreeS
262e6 65 74 50 61 67 65 53 69 7a 65 28 70 42 74 2c 20  etPageSize(pBt, 
262e7 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65  db->nextPagesize
262e8 2c 20 2d 31 2c 20 30 29 20 29 7b 0a 20 20 20 20  , -1, 0) ){.    
262e9 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
262ea 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
262eb 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
262ec 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47  .  /*.  **  PRAG
262ed 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6d 61  MA [database.]ma
262ee 78 5f 70 61 67 65 5f 63 6f 75 6e 74 0a 20 20 2a  x_page_count.  *
262ef 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
262f0 61 73 65 2e 5d 6d 61 78 5f 70 61 67 65 5f 63 6f  ase.]max_page_co
262f1 75 6e 74 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  unt=N.  **.  ** 
262f2 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72  The first form r
262f3 65 70 6f 72 74 73 20 74 68 65 20 63 75 72 72 65  eports the curre
262f4 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72 20 74  nt setting for t
262f5 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20  he.  ** maximum 
262f6 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
262f7 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
262f8 66 69 6c 65 2e 20 20 54 68 65 20 0a 20 20 2a 2a  file.  The .  **
262f9 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 61 74 74   second form att
262fa 65 6d 70 74 73 20 74 6f 20 63 68 61 6e 67 65 20  empts to change 
262fb 74 68 69 73 20 73 65 74 74 69 6e 67 2e 20 20 42  this setting.  B
262fc 6f 74 68 0a 20 20 2a 2a 20 66 6f 72 6d 73 20 72  oth.  ** forms r
262fd 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e  eturn the curren
262fe 74 20 73 65 74 74 69 6e 67 2e 0a 20 20 2a 2f 0a  t setting..  */.
262ff 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
26300 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6d 61 78 5f  ICmp(zLeft,"max_
26301 70 61 67 65 5f 63 6f 75 6e 74 22 29 3d 3d 30 20  page_count")==0 
26302 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42  ){.    Btree *pB
26303 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20  t = pDb->pBt;.  
26304 20 20 69 6e 74 20 6e 65 77 4d 61 78 20 3d 20 30    int newMax = 0
26305 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
26306 74 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  t!=0 );.    if( 
26307 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
26308 6e 65 77 4d 61 78 20 3d 20 61 74 6f 69 28 7a 52  newMax = atoi(zR
26309 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ight);.    }.   
2630a 20 69 66 28 20 41 4c 57 41 59 53 28 70 42 74 29   if( ALWAYS(pBt)
2630b 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d 61 78   ){.      newMax
2630c 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
2630d 61 78 50 61 67 65 43 6f 75 6e 74 28 70 42 74 2c  axPageCount(pBt,
2630e 20 6e 65 77 4d 61 78 29 3b 0a 20 20 20 20 7d 0a   newMax);.    }.
2630f 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65      returnSingle
26310 49 6e 74 28 70 50 61 72 73 65 2c 20 22 6d 61 78  Int(pParse, "max
26311 5f 70 61 67 65 5f 63 6f 75 6e 74 22 2c 20 6e 65  _page_count", ne
26312 77 4d 61 78 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a  wMax);.  }else..
26313 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
26314 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67  A [database.]pag
26315 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 0a 20 20 2a  e_count.  **.  *
26316 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
26317 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
26318 74 68 65 20 73 70 65 63 69 66 69 65 64 20 64 61  the specified da
26319 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  tabase..  */.  i
2631a 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2631b 70 28 7a 4c 65 66 74 2c 22 70 61 67 65 5f 63 6f  p(zLeft,"page_co
2631c 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  unt")==0 ){.    
2631d 69 6e 74 20 69 52 65 67 3b 0a 20 20 20 20 69 66  int iReg;.    if
2631e 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
2631f 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
26320 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
26321 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65     sqlite3CodeVe
26322 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73  rifySchema(pPars
26323 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 69 52 65  e, iDb);.    iRe
26324 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  g = ++pParse->nM
26325 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  em;.    sqlite3V
26326 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26327 50 61 67 65 63 6f 75 6e 74 2c 20 69 44 62 2c 20  Pagecount, iDb, 
26328 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74  iReg);.    sqlit
26329 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2632a 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 69 52  OP_ResultRow, iR
2632b 65 67 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  eg, 1);.    sqli
2632c 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
2632d 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  s(v, 1);.    sql
2632e 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2632f 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
26330 5f 4e 41 4d 45 2c 20 22 70 61 67 65 5f 63 6f 75  _NAME, "page_cou
26331 6e 74 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  nt", SQLITE_STAT
26332 49 43 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20  IC);.  }else..  
26333 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  /*.  **  PRAGMA 
26334 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 69  [database.]locki
26335 6e 67 5f 6d 6f 64 65 0a 20 20 2a 2a 20 20 50 52  ng_mode.  **  PR
26336 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
26337 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 20 3d 20 28  locking_mode = (
26338 6e 6f 72 6d 61 6c 7c 65 78 63 6c 75 73 69 76 65  normal|exclusive
26339 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ).  */.  if( sql
2633a 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
2633b 74 2c 22 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22  t,"locking_mode"
2633c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73  )==0 ){.    cons
2633d 74 20 63 68 61 72 20 2a 7a 52 65 74 20 3d 20 22  t char *zRet = "
2633e 6e 6f 72 6d 61 6c 22 3b 0a 20 20 20 20 69 6e 74  normal";.    int
2633f 20 65 4d 6f 64 65 20 3d 20 67 65 74 4c 6f 63 6b   eMode = getLock
26340 69 6e 67 4d 6f 64 65 28 7a 52 69 67 68 74 29 3b  ingMode(zRight);
26341 0a 0a 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e  ..    if( pId2->
26342 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65 3d 3d 50  n==0 && eMode==P
26343 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
26344 5f 51 55 45 52 59 20 29 7b 0a 20 20 20 20 20 20  _QUERY ){.      
26345 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52 41 47 4d  /* Simple "PRAGM
26346 41 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3b 22  A locking_mode;"
26347 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73   statement. This
26348 20 69 73 20 61 20 71 75 65 72 79 20 66 6f 72 0a   is a query for.
26349 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72        ** the cur
2634a 72 65 6e 74 20 64 65 66 61 75 6c 74 20 6c 6f 63  rent default loc
2634b 6b 69 6e 67 20 6d 6f 64 65 20 28 77 68 69 63 68  king mode (which
2634c 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e   may be differen
2634d 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  t to.      ** th
2634e 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 6f  e locking-mode o
2634f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
26350 61 73 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ase)..      */. 
26351 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 64 62 2d       eMode = db-
26352 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b 0a 20  >dfltLockMode;. 
26353 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
26354 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20  Pager *pPager;. 
26355 20 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e       if( pId2->n
26356 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
26357 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73  * This indicates
26358 20 74 68 61 74 20 6e 6f 20 64 61 74 61 62 61 73   that no databas
26359 65 20 6e 61 6d 65 20 77 61 73 20 73 70 65 63 69  e name was speci
2635a 66 69 65 64 20 61 73 20 70 61 72 74 0a 20 20 20  fied as part.   
2635b 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 50       ** of the P
2635c 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 20 49  RAGMA command. I
2635d 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
2635e 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 6d 75 73  locking-mode mus
2635f 74 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  t be.        ** 
26360 73 65 74 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63  set on all attac
26361 68 65 64 20 64 61 74 61 62 61 73 65 73 2c 20 61  hed databases, a
26362 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 6d 61  s well as the ma
26363 69 6e 20 64 62 20 66 69 6c 65 2e 0a 20 20 20 20  in db file..    
26364 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
26365 2a 20 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c 69  * Also, the sqli
26366 74 65 33 2e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65  te3.dfltLockMode
26367 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
26368 20 73 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20   so that.       
26369 20 2a 2a 20 61 6e 79 20 73 75 62 73 65 71 75 65   ** any subseque
2636a 6e 74 6c 79 20 61 74 74 61 63 68 65 64 20 64 61  ntly attached da
2636b 74 61 62 61 73 65 73 20 61 6c 73 6f 20 75 73 65  tabases also use
2636c 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 20   the specified. 
2636d 20 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 69 6e         ** lockin
2636e 67 20 6d 6f 64 65 2e 0a 20 20 20 20 20 20 20 20  g mode..        
2636f 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  */.        int i
26370 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  i;.        asser
26371 74 28 70 44 62 3d 3d 26 64 62 2d 3e 61 44 62 5b  t(pDb==&db->aDb[
26372 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  0]);.        for
26373 28 69 69 3d 32 3b 20 69 69 3c 64 62 2d 3e 6e 44  (ii=2; ii<db->nD
26374 62 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20  b; ii++){.      
26375 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c      pPager = sql
26376 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64  ite3BtreePager(d
26377 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74 29 3b  b->aDb[ii].pBt);
26378 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
26379 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
2637a 64 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65  de(pPager, eMode
2637b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2637c 20 20 20 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63       db->dfltLoc
2637d 6b 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64  kMode = (u8)eMod
2637e 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
2637f 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65   pPager = sqlite
26380 33 42 74 72 65 65 50 61 67 65 72 28 70 44 62 2d  3BtreePager(pDb-
26381 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f  >pBt);.      eMo
26382 64 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  de = sqlite3Page
26383 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61  rLockingMode(pPa
26384 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20  ger, eMode);.   
26385 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 65   }..    assert(e
26386 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
26387 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 7c 7c  INGMODE_NORMAL||
26388 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  eMode==PAGER_LOC
26389 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
2638a 56 45 29 3b 0a 20 20 20 20 69 66 28 20 65 4d 6f  VE);.    if( eMo
2638b 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  de==PAGER_LOCKIN
2638c 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20  GMODE_EXCLUSIVE 
2638d 29 7b 0a 20 20 20 20 20 20 7a 52 65 74 20 3d 20  ){.      zRet = 
2638e 22 65 78 63 6c 75 73 69 76 65 22 3b 0a 20 20 20  "exclusive";.   
2638f 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
26390 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
26391 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
26392 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
26393 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
26394 2c 20 22 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22  , "locking_mode"
26395 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
26396 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
26397 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
26398 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20  ring8, 0, 1, 0, 
26399 7a 52 65 74 2c 20 30 29 3b 0a 20 20 20 20 73 71  zRet, 0);.    sq
2639a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2639b 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
2639c 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 0a   1, 1);.  }else.
2639d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47  .  /*.  **  PRAG
2639e 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f  MA [database.]jo
2639f 75 72 6e 61 6c 5f 6d 6f 64 65 0a 20 20 2a 2a 20  urnal_mode.  ** 
263a0 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
263a1 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20  e.]journal_mode 
263a2 3d 20 28 64 65 6c 65 74 65 7c 70 65 72 73 69 73  = (delete|persis
263a3 74 7c 6f 66 66 7c 74 72 75 6e 63 61 74 65 7c 6d  t|off|truncate|m
263a4 65 6d 6f 72 79 29 0a 20 20 2a 2f 0a 20 20 69 66  emory).  */.  if
263a5 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
263a6 28 7a 4c 65 66 74 2c 22 6a 6f 75 72 6e 61 6c 5f  (zLeft,"journal_
263a7 6d 6f 64 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  mode")==0 ){.   
263a8 20 69 6e 74 20 65 4d 6f 64 65 3b 0a 20 20 20 20   int eMode;.    
263a9 73 74 61 74 69 63 20 63 68 61 72 20 2a 20 63 6f  static char * co
263aa 6e 73 74 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b 5d  nst azModeName[]
263ab 20 3d 20 7b 0a 20 20 20 20 20 20 22 64 65 6c 65   = {.      "dele
263ac 74 65 22 2c 20 22 70 65 72 73 69 73 74 22 2c 20  te", "persist", 
263ad 22 6f 66 66 22 2c 20 22 74 72 75 6e 63 61 74 65  "off", "truncate
263ae 22 2c 20 22 6d 65 6d 6f 72 79 22 0a 20 20 20 20  ", "memory".    
263af 7d 3b 0a 0a 20 20 20 20 69 66 28 20 7a 52 69 67  };..    if( zRig
263b0 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65  ht==0 ){.      e
263b1 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55  Mode = PAGER_JOU
263b2 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 3b 0a  RNALMODE_QUERY;.
263b3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
263b4 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33   int n = sqlite3
263b5 53 74 72 6c 65 6e 33 30 28 7a 52 69 67 68 74 29  Strlen30(zRight)
263b6 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  ;.      eMode = 
263b7 73 69 7a 65 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d  sizeof(azModeNam
263b8 65 29 2f 73 69 7a 65 6f 66 28 61 7a 4d 6f 64 65  e)/sizeof(azMode
263b9 4e 61 6d 65 5b 30 5d 29 20 2d 20 31 3b 0a 20 20  Name[0]) - 1;.  
263ba 20 20 20 20 77 68 69 6c 65 28 20 65 4d 6f 64 65      while( eMode
263bb 3e 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74  >=0 && sqlite3St
263bc 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 61  rNICmp(zRight, a
263bd 7a 4d 6f 64 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d  zModeName[eMode]
263be 2c 20 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  , n)!=0 ){.     
263bf 20 20 20 65 4d 6f 64 65 2d 2d 3b 0a 20 20 20 20     eMode--;.    
263c0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
263c1 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 26 26 20  ( pId2->n==0 && 
263c2 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
263c3 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 29  RNALMODE_QUERY )
263c4 7b 0a 20 20 20 20 20 20 2f 2a 20 53 69 6d 70 6c  {.      /* Simpl
263c5 65 20 22 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61  e "PRAGMA journa
263c6 6c 5f 6d 6f 64 65 3b 22 20 73 74 61 74 65 6d 65  l_mode;" stateme
263c7 6e 74 2e 20 54 68 69 73 20 69 73 20 61 20 71 75  nt. This is a qu
263c8 65 72 79 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a  ery for.      **
263c9 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 65 66   the current def
263ca 61 75 6c 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64  ault journal mod
263cb 65 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20  e (which may be 
263cc 64 69 66 66 65 72 65 6e 74 20 74 6f 0a 20 20 20  different to.   
263cd 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61     ** the journa
263ce 6c 2d 6d 6f 64 65 20 6f 66 20 74 68 65 20 6d 61  l-mode of the ma
263cf 69 6e 20 64 61 74 61 62 61 73 65 29 2e 0a 20 20  in database)..  
263d0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 65 4d 6f      */.      eMo
263d1 64 65 20 3d 20 64 62 2d 3e 64 66 6c 74 4a 6f 75  de = db->dfltJou
263d2 72 6e 61 6c 4d 6f 64 65 3b 0a 20 20 20 20 7d 65  rnalMode;.    }e
263d3 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72  lse{.      Pager
263d4 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20   *pPager;.      
263d5 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 29  if( pId2->n==0 )
263d6 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
263d7 73 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  s indicates that
263d8 20 6e 6f 20 64 61 74 61 62 61 73 65 20 6e 61 6d   no database nam
263d9 65 20 77 61 73 20 73 70 65 63 69 66 69 65 64 20  e was specified 
263da 61 73 20 70 61 72 74 0a 20 20 20 20 20 20 20 20  as part.        
263db 2a 2a 20 6f 66 20 74 68 65 20 50 52 41 47 4d 41  ** of the PRAGMA
263dc 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68 69   command. In thi
263dd 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 6e  s case the journ
263de 61 6c 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65 0a  al-mode must be.
263df 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6f          ** set o
263e0 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64  n all attached d
263e1 61 74 61 62 61 73 65 73 2c 20 61 73 20 77 65 6c  atabases, as wel
263e2 6c 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64 62  l as the main db
263e3 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a   file..        *
263e4 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 6c 73  *.        ** Als
263e5 6f 2c 20 74 68 65 20 73 71 6c 69 74 65 33 2e 64  o, the sqlite3.d
263e6 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 20 76  fltJournalMode v
263e7 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 73  ariable is set s
263e8 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a  o that.        *
263e9 2a 20 61 6e 79 20 73 75 62 73 65 71 75 65 6e 74  * any subsequent
263ea 6c 79 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ly attached data
263eb 62 61 73 65 73 20 61 6c 73 6f 20 75 73 65 20 74  bases also use t
263ec 68 65 20 73 70 65 63 69 66 69 65 64 0a 20 20 20  he specified.   
263ed 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20       ** journal 
263ee 6d 6f 64 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  mode..        */
263ef 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b  .        int ii;
263f0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
263f1 70 44 62 3d 3d 26 64 62 2d 3e 61 44 62 5b 30 5d  pDb==&db->aDb[0]
263f2 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  );.        for(i
263f3 69 3d 31 3b 20 69 69 3c 64 62 2d 3e 6e 44 62 3b  i=1; ii<db->nDb;
263f4 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   ii++){.        
263f5 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 69    if( db->aDb[ii
263f6 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ].pBt ){.       
263f7 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71       pPager = sq
263f8 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
263f9 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74 29  db->aDb[ii].pBt)
263fa 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
263fb 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
263fc 6c 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65 4d  lMode(pPager, eM
263fd 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ode);.          
263fe 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
263ff 20 20 20 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72      db->dfltJour
26400 6e 61 6c 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d  nalMode = (u8)eM
26401 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ode;.      }.   
26402 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69     pPager = sqli
26403 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 44  te3BtreePager(pD
26404 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 65  b->pBt);.      e
26405 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 50 61  Mode = sqlite3Pa
26406 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70  gerJournalMode(p
26407 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20  Pager, eMode);. 
26408 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
26409 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
2640a 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45  URNALMODE_DELETE
2640b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c  .              |
2640c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
2640d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43  OURNALMODE_TRUNC
2640e 41 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20  ATE.            
2640f 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45    || eMode==PAGE
26410 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  R_JOURNALMODE_PE
26411 52 53 49 53 54 0a 20 20 20 20 20 20 20 20 20 20  RSIST.          
26412 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41      || eMode==PA
26413 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
26414 4f 46 46 0a 20 20 20 20 20 20 20 20 20 20 20 20  OFF.            
26415 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45    || eMode==PAGE
26416 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
26417 4d 4f 52 59 20 29 3b 0a 20 20 20 20 73 71 6c 69  MORY );.    sqli
26418 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
26419 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  s(v, 1);.    sql
2641a 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2641b 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
2641c 5f 4e 41 4d 45 2c 20 22 6a 6f 75 72 6e 61 6c 5f  _NAME, "journal_
2641d 6d 6f 64 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  mode", SQLITE_ST
2641e 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74  ATIC);.    sqlit
2641f 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
26420 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31  OP_String8, 0, 1
26421 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20  , 0, .          
26422 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b 65 4d 6f 64   azModeName[eMod
26423 65 5d 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  e], P4_STATIC);.
26424 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26425 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
26426 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20  ltRow, 1, 1);.  
26427 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }else..  /*.  **
26428 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
26429 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65  se.]journal_size
2642a 5f 6c 69 6d 69 74 0a 20 20 2a 2a 20 20 50 52 41  _limit.  **  PRA
2642b 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a  GMA [database.]j
2642c 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69  ournal_size_limi
2642d 74 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47 65  t=N.  **.  ** Ge
2642e 74 20 6f 72 20 73 65 74 20 74 68 65 20 73 69 7a  t or set the siz
2642f 65 20 6c 69 6d 69 74 20 6f 6e 20 72 6f 6c 6c 62  e limit on rollb
26430 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ack journal file
26431 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  s..  */.  if( sq
26432 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
26433 66 74 2c 22 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65  ft,"journal_size
26434 5f 6c 69 6d 69 74 22 29 3d 3d 30 20 29 7b 0a 20  _limit")==0 ){. 
26435 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
26436 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
26437 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a  ager(pDb->pBt);.
26438 20 20 20 20 69 36 34 20 69 4c 69 6d 69 74 20 3d      i64 iLimit =
26439 20 2d 32 3b 0a 20 20 20 20 69 66 28 20 7a 52 69   -2;.    if( zRi
2643a 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ght ){.      sql
2643b 69 74 65 33 41 74 6f 69 36 34 28 7a 52 69 67 68  ite3Atoi64(zRigh
2643c 74 2c 20 26 69 4c 69 6d 69 74 29 3b 0a 20 20 20  t, &iLimit);.   
2643d 20 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 2d 31     if( iLimit<-1
2643e 20 29 20 69 4c 69 6d 69 74 20 3d 20 2d 31 3b 0a   ) iLimit = -1;.
2643f 20 20 20 20 7d 0a 20 20 20 20 69 4c 69 6d 69 74      }.    iLimit
26440 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a   = sqlite3PagerJ
26441 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28  ournalSizeLimit(
26442 70 50 61 67 65 72 2c 20 69 4c 69 6d 69 74 29 3b  pPager, iLimit);
26443 0a 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c  .    returnSingl
26444 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 6a 6f  eInt(pParse, "jo
26445 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74  urnal_size_limit
26446 22 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 65  ", iLimit);.  }e
26447 6c 73 65 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  lse..#endif /* S
26448 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
26449 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 20 20 2f  _PRAGMAS */..  /
2644a 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
2644b 64 61 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76  database.]auto_v
2644c 61 63 75 75 6d 0a 20 20 2a 2a 20 20 50 52 41 47  acuum.  **  PRAG
2644d 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 61 75  MA [database.]au
2644e 74 6f 5f 76 61 63 75 75 6d 3d 4e 0a 20 20 2a 2a  to_vacuum=N.  **
2644f 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20 73 65 74  .  ** Get or set
26450 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
26451 65 20 64 61 74 61 62 61 73 65 20 27 61 75 74 6f  e database 'auto
26452 2d 76 61 63 75 75 6d 27 20 70 61 72 61 6d 65 74  -vacuum' paramet
26453 65 72 2e 0a 20 20 2a 2a 20 54 68 65 20 76 61 6c  er..  ** The val
26454 75 65 20 69 73 20 6f 6e 65 20 6f 66 3a 20 20 30  ue is one of:  0
26455 20 4e 4f 4e 45 20 31 20 46 55 4c 4c 20 32 20 49   NONE 1 FULL 2 I
26456 4e 43 52 45 4d 45 4e 54 41 4c 0a 20 20 2a 2f 0a  NCREMENTAL.  */.
26457 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
26458 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
26459 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
2645a 43 6d 70 28 7a 4c 65 66 74 2c 22 61 75 74 6f 5f  Cmp(zLeft,"auto_
2645b 76 61 63 75 75 6d 22 29 3d 3d 30 20 29 7b 0a 20  vacuum")==0 ){. 
2645c 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
2645d 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73  pDb->pBt;.    as
2645e 73 65 72 74 28 20 70 42 74 21 3d 30 20 29 3b 0a  sert( pBt!=0 );.
2645f 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
26460 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
26461 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
26462 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
26463 7d 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68  }.    if( !zRigh
26464 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61  t ){.      int a
26465 75 74 6f 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20  uto_vacuum;.    
26466 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 42 74    if( ALWAYS(pBt
26467 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 61 75  ) ){.         au
26468 74 6f 5f 76 61 63 75 75 6d 20 3d 20 73 71 6c 69  to_vacuum = sqli
26469 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56  te3BtreeGetAutoV
2646a 61 63 75 75 6d 28 70 42 74 29 3b 0a 20 20 20 20  acuum(pBt);.    
2646b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2646c 20 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20    auto_vacuum = 
2646d 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41  SQLITE_DEFAULT_A
2646e 55 54 4f 56 41 43 55 55 4d 3b 0a 20 20 20 20 20  UTOVACUUM;.     
2646f 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53   }.      returnS
26470 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
26471 20 22 61 75 74 6f 5f 76 61 63 75 75 6d 22 2c 20   "auto_vacuum", 
26472 61 75 74 6f 5f 76 61 63 75 75 6d 29 3b 0a 20 20  auto_vacuum);.  
26473 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
26474 6e 74 20 65 41 75 74 6f 20 3d 20 67 65 74 41 75  nt eAuto = getAu
26475 74 6f 56 61 63 75 75 6d 28 7a 52 69 67 68 74 29  toVacuum(zRight)
26476 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
26477 65 41 75 74 6f 3e 3d 30 20 26 26 20 65 41 75 74  eAuto>=0 && eAut
26478 6f 3c 3d 32 20 29 3b 0a 20 20 20 20 20 20 64 62  o<=2 );.      db
26479 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 20 3d 20  ->nextAutovac = 
2647a 28 75 38 29 65 41 75 74 6f 3b 0a 20 20 20 20 20  (u8)eAuto;.     
2647b 20 69 66 28 20 41 4c 57 41 59 53 28 65 41 75 74   if( ALWAYS(eAut
2647c 6f 3e 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20  o>=0) ){.       
2647d 20 2f 2a 20 43 61 6c 6c 20 53 65 74 41 75 74 6f   /* Call SetAuto
2647e 56 61 63 75 75 6d 28 29 20 74 6f 20 73 65 74 20  Vacuum() to set 
2647f 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 69  initialize the i
26480 6e 74 65 72 6e 61 6c 20 61 75 74 6f 20 61 6e 64  nternal auto and
26481 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 63 72  .        ** incr
26482 2d 76 61 63 75 75 6d 20 66 6c 61 67 73 2e 20 54  -vacuum flags. T
26483 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64 20  his is required 
26484 69 6e 20 63 61 73 65 20 74 68 69 73 20 63 6f 6e  in case this con
26485 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20 20 20 20  nection.        
26486 2a 2a 20 63 72 65 61 74 65 73 20 74 68 65 20 64  ** creates the d
26487 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 74  atabase file. It
26488 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68   is important th
26489 61 74 20 69 74 20 69 73 20 63 72 65 61 74 65 64  at it is created
2648a 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73 20 61  .        ** as a
2648b 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61  n auto-vacuum ca
2648c 70 61 62 6c 65 20 64 62 2e 0a 20 20 20 20 20 20  pable db..      
2648d 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
2648e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
2648f 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeSetAutoVacuum(
26490 70 42 74 2c 20 65 41 75 74 6f 29 3b 0a 20 20 20  pBt, eAuto);.   
26491 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
26492 49 54 45 5f 4f 4b 20 26 26 20 28 65 41 75 74 6f  ITE_OK && (eAuto
26493 3d 3d 31 20 7c 7c 20 65 41 75 74 6f 3d 3d 32 29  ==1 || eAuto==2)
26494 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
26495 20 57 68 65 6e 20 73 65 74 74 69 6e 67 20 74 68   When setting th
26496 65 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 6d 6f  e auto_vacuum mo
26497 64 65 20 74 6f 20 65 69 74 68 65 72 20 22 66 75  de to either "fu
26498 6c 6c 22 20 6f 72 20 0a 20 20 20 20 20 20 20 20  ll" or .        
26499 20 20 2a 2a 20 22 69 6e 63 72 65 6d 65 6e 74 61    ** "incrementa
2649a 6c 22 2c 20 77 72 69 74 65 20 74 68 65 20 76 61  l", write the va
2649b 6c 75 65 20 6f 66 20 6d 65 74 61 5b 36 5d 20 69  lue of meta[6] i
2649c 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20  n the database. 
2649d 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65           ** file
2649e 2e 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67  . Before writing
2649f 20 74 6f 20 6d 65 74 61 5b 36 5d 2c 20 63 68 65   to meta[6], che
264a0 63 6b 20 74 68 61 74 20 6d 65 74 61 5b 33 5d 20  ck that meta[3] 
264a1 69 6e 64 69 63 61 74 65 73 0a 20 20 20 20 20 20  indicates.      
264a2 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 69 73      ** that this
264a3 20 72 65 61 6c 6c 79 20 69 73 20 61 6e 20 61 75   really is an au
264a4 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c  to-vacuum capabl
264a5 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  e database..    
264a6 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
264a7 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
264a8 56 64 62 65 4f 70 4c 69 73 74 20 73 65 74 4d 65  VdbeOpList setMe
264a9 74 61 36 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  ta6[] = {.      
264aa 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73        { OP_Trans
264ab 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20 20  action,    0,   
264ac 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 20              1,  
264ad 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20        0},    /* 
264ae 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  0 */.           
264af 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65   { OP_ReadCookie
264b0 2c 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  ,     0,        
264b1 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 20         1,       
264b2 20 33 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a   3},    /* 1 */.
264b3 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
264b4 5f 49 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  _If,            
264b5 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
264b6 20 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 20    0,        0}, 
264b7 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20     /* 2 */.     
264b8 20 20 20 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74         { OP_Halt
264b9 2c 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  ,           SQLI
264ba 54 45 5f 4f 4b 2c 20 20 20 20 20 20 20 4f 45 5f  TE_OK,       OE_
264bb 41 62 6f 72 74 2c 20 30 7d 2c 20 20 20 20 2f 2a  Abort, 0},    /*
264bc 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   3 */.          
264bd 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20    { OP_Integer, 
264be 20 20 20 20 20 20 20 30 2c 20 20 20 20 20 20 20         0,       
264bf 20 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20          1,      
264c0 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f    0},    /* 4 */
264c1 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
264c2 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20  P_SetCookie,    
264c3 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
264c4 20 20 20 36 2c 20 20 20 20 20 20 20 20 31 7d 2c     6,        1},
264c5 20 20 20 20 2f 2a 20 35 20 2a 2f 0a 20 20 20 20      /* 5 */.    
264c6 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20        };.       
264c7 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20     int iAddr;.  
264c8 20 20 20 20 20 20 20 20 69 41 64 64 72 20 3d 20          iAddr = 
264c9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
264ca 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a  List(v, ArraySiz
264cb 65 28 73 65 74 4d 65 74 61 36 29 2c 20 73 65 74  e(setMeta6), set
264cc 4d 65 74 61 36 29 3b 0a 20 20 20 20 20 20 20 20  Meta6);.        
264cd 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
264ce 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2c 20  ngeP1(v, iAddr, 
264cf 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iDb);.          
264d0 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
264d1 65 50 31 28 76 2c 20 69 41 64 64 72 2b 31 2c 20  eP1(v, iAddr+1, 
264d2 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iDb);.          
264d3 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
264d4 65 50 32 28 76 2c 20 69 41 64 64 72 2b 32 2c 20  eP2(v, iAddr+2, 
264d5 69 41 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20  iAddr+4);.      
264d6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
264d7 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72  hangeP1(v, iAddr
264d8 2b 34 2c 20 65 41 75 74 6f 2d 31 29 3b 0a 20 20  +4, eAuto-1);.  
264d9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
264da 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69  dbeChangeP1(v, i
264db 41 64 64 72 2b 35 2c 20 69 44 62 29 3b 0a 20 20  Addr+5, iDb);.  
264dc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
264dd 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
264de 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  iDb);.        }.
264df 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
264e0 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20  }else.#endif..  
264e1 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  /*.  **  PRAGMA 
264e2 5b 64 61 74 61 62 61 73 65 2e 5d 69 6e 63 72 65  [database.]incre
264e3 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 28 4e 29  mental_vacuum(N)
264e4 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 6f 20 4e 20  .  **.  ** Do N 
264e5 73 74 65 70 73 20 6f 66 20 69 6e 63 72 65 6d 65  steps of increme
264e6 6e 74 61 6c 20 76 61 63 75 75 6d 69 6e 67 20 6f  ntal vacuuming o
264e7 6e 20 61 20 64 61 74 61 62 61 73 65 2e 0a 20 20  n a database..  
264e8 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
264e9 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
264ea 4d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  M.  if( sqlite3S
264eb 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 69 6e  trICmp(zLeft,"in
264ec 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d  cremental_vacuum
264ed 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ")==0 ){.    int
264ee 20 69 4c 69 6d 69 74 2c 20 61 64 64 72 3b 0a 20   iLimit, addr;. 
264ef 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
264f0 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
264f1 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
264f2 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d  ragma_out;.    }
264f3 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d  .    if( zRight=
264f4 3d 30 20 7c 7c 20 21 73 71 6c 69 74 65 33 47 65  =0 || !sqlite3Ge
264f5 74 49 6e 74 33 32 28 7a 52 69 67 68 74 2c 20 26  tInt32(zRight, &
264f6 69 4c 69 6d 69 74 29 20 7c 7c 20 69 4c 69 6d 69  iLimit) || iLimi
264f7 74 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 4c  t<=0 ){.      iL
264f8 69 6d 69 74 20 3d 20 30 78 37 66 66 66 66 66 66  imit = 0x7ffffff
264f9 66 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  f;.    }.    sql
264fa 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
264fb 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
264fc 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  0, iDb);.    sql
264fd 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
264fe 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 4c  , OP_Integer, iL
264ff 69 6d 69 74 2c 20 31 29 3b 0a 20 20 20 20 61 64  imit, 1);.    ad
26500 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
26501 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 6e 63  AddOp1(v, OP_Inc
26502 72 56 61 63 75 75 6d 2c 20 69 44 62 29 3b 0a 20  rVacuum, iDb);. 
26503 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26504 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp1(v, OP_Resul
26505 74 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20 73 71  tRow, 1);.    sq
26506 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26507 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 31 2c  v, OP_AddImm, 1,
26508 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   -1);.    sqlite
26509 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2650a 50 5f 49 66 50 6f 73 2c 20 31 2c 20 61 64 64 72  P_IfPos, 1, addr
2650b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2650c 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2650d 64 72 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  dr);.  }else.#en
2650e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
2650f 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
26510 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a  RAGMAS.  /*.  **
26511 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
26512 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 0a 20  se.]cache_size. 
26513 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
26514 61 62 61 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a  abase.]cache_siz
26515 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  e=N.  **.  ** Th
26516 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70  e first form rep
26517 6f 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74  orts the current
26518 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 66   local setting f
26519 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65  or the.  ** page
2651a 20 63 61 63 68 65 20 73 69 7a 65 2e 20 20 54 68   cache size.  Th
2651b 65 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20  e local setting 
2651c 63 61 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74  can be different
2651d 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 70   from.  ** the p
2651e 65 72 73 69 73 74 65 6e 74 20 63 61 63 68 65 20  ersistent cache 
2651f 73 69 7a 65 20 76 61 6c 75 65 20 74 68 61 74 20  size value that 
26520 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  is stored in the
26521 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66   database.  ** f
26522 69 6c 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65  ile itself.  The
26523 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
26524 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  is the maximum n
26525 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61  umber of.  ** pa
26526 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20  ges in the page 
26527 63 61 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f  cache.  The seco
26528 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65  nd form sets the
26529 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20 70 61 67 65   local.  ** page
2652a 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75   cache size valu
2652b 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  e.  It does not 
2652c 63 68 61 6e 67 65 20 74 68 65 20 70 65 72 73 69  change the persi
2652d 73 74 65 6e 74 0a 20 20 2a 2a 20 63 61 63 68 65  stent.  ** cache
2652e 20 73 69 7a 65 20 73 74 6f 72 65 64 20 6f 6e 20   size stored on 
2652f 74 68 65 20 64 69 73 6b 20 73 6f 20 74 68 65 20  the disk so the 
26530 63 61 63 68 65 20 73 69 7a 65 20 77 69 6c 6c 20  cache size will 
26531 72 65 76 65 72 74 0a 20 20 2a 2a 20 74 6f 20 69  revert.  ** to i
26532 74 73 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  ts default value
26533 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   when the databa
26534 73 65 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64  se is closed and
26535 20 72 65 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 20   reopened..  ** 
26536 4e 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f  N should be a po
26537 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2e 0a  sitive integer..
26538 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
26539 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
2653a 22 63 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30  "cache_size")==0
2653b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
2653c 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
2653d 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
2653e 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28  gma_out;.    if(
2653f 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
26540 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
26541 74 28 70 50 61 72 73 65 2c 20 22 63 61 63 68 65  t(pParse, "cache
26542 5f 73 69 7a 65 22 2c 20 70 44 62 2d 3e 70 53 63  _size", pDb->pSc
26543 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
26544 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
26545 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61      int size = a
26546 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  toi(zRight);.   
26547 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20     if( size<0 ) 
26548 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20  size = -size;.  
26549 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
2654a 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73  ->cache_size = s
2654b 69 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ize;.      sqlit
2654c 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
2654d 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44  ize(pDb->pBt, pD
2654e 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
2654f 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20  e_size);.    }. 
26550 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a   }else..  /*.  *
26551 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f  *   PRAGMA temp_
26552 73 74 6f 72 65 0a 20 20 2a 2a 20 20 20 50 52 41  store.  **   PRA
26553 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 20 3d  GMA temp_store =
26554 20 22 64 65 66 61 75 6c 74 22 7c 22 6d 65 6d 6f   "default"|"memo
26555 72 79 22 7c 22 66 69 6c 65 22 0a 20 20 2a 2a 0a  ry"|"file".  **.
26556 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73    ** Return or s
26557 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c  et the local val
26558 75 65 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73  ue of the temp_s
26559 74 6f 72 65 20 66 6c 61 67 2e 20 20 43 68 61 6e  tore flag.  Chan
2655a 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f  ging.  ** the lo
2655b 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73 20 6e  cal value does n
2655c 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20  ot make changes 
2655d 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65  to the disk file
2655e 20 61 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74   and the default
2655f 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c  .  ** value will
26560 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 68 65   be restored the
26561 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 64   next time the d
26562 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
26563 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74  d..  **.  ** Not
26564 65 20 74 68 61 74 20 69 74 20 69 73 20 70 6f 73  e that it is pos
26565 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 6c 69  sible for the li
26566 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 2d 74 69  brary compile-ti
26567 6d 65 20 6f 70 74 69 6f 6e 73 20 74 6f 0a 20 20  me options to.  
26568 2a 2a 20 6f 76 65 72 72 69 64 65 20 74 68 69 73  ** override this
26569 20 73 65 74 74 69 6e 67 0a 20 20 2a 2f 0a 20 20   setting.  */.  
2656a 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2656b 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f  mp(zLeft, "temp_
2656c 73 74 6f 72 65 22 29 3d 3d 30 20 29 7b 0a 20 20  store")==0 ){.  
2656d 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
2656e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e  .      returnSin
2656f 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22  gleInt(pParse, "
26570 74 65 6d 70 5f 73 74 6f 72 65 22 2c 20 64 62 2d  temp_store", db-
26571 3e 74 65 6d 70 5f 73 74 6f 72 65 29 3b 0a 20 20  >temp_store);.  
26572 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
26573 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65  hangeTempStorage
26574 28 70 50 61 72 73 65 2c 20 7a 52 69 67 68 74 29  (pParse, zRight)
26575 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
26576 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41  .  /*.  **   PRA
26577 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64  GMA temp_store_d
26578 69 72 65 63 74 6f 72 79 0a 20 20 2a 2a 20 20 20  irectory.  **   
26579 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72  PRAGMA temp_stor
2657a 65 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 22 22  e_directory = ""
2657b 7c 22 64 69 72 65 63 74 6f 72 79 5f 6e 61 6d 65  |"directory_name
2657c 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75  ".  **.  ** Retu
2657d 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f  rn or set the lo
2657e 63 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65  cal value of the
2657f 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65   temp_store_dire
26580 63 74 6f 72 79 20 66 6c 61 67 2e 20 20 43 68 61  ctory flag.  Cha
26581 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 76  nging.  ** the v
26582 61 6c 75 65 20 73 65 74 73 20 61 20 73 70 65 63  alue sets a spec
26583 69 66 69 63 20 64 69 72 65 63 74 6f 72 79 20 74  ific directory t
26584 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 65  o be used for te
26585 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 20  mporary files.. 
26586 20 2a 2a 20 53 65 74 74 69 6e 67 20 74 6f 20 61   ** Setting to a
26587 20 6e 75 6c 6c 20 73 74 72 69 6e 67 20 72 65 76   null string rev
26588 65 72 74 73 20 74 6f 20 74 68 65 20 64 65 66 61  erts to the defa
26589 75 6c 74 20 74 65 6d 70 6f 72 61 72 79 20 64 69  ult temporary di
2658a 72 65 63 74 6f 72 79 20 73 65 61 72 63 68 2e 0a  rectory search..
2658b 20 20 2a 2a 20 49 66 20 74 65 6d 70 6f 72 61 72    ** If temporar
2658c 79 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 63  y directory is c
2658d 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 69 6e 76  hanged, then inv
2658e 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61  alidateTempStora
2658f 67 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20 20  ge..  **.  */.  
26590 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
26591 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f  mp(zLeft, "temp_
26592 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22  store_directory"
26593 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
26594 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
26595 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d   if( sqlite3_tem
26596 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20  p_directory ){. 
26597 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26598 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
26599 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
2659a 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2659b 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
2659c 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20  NAME, .         
2659d 20 20 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64     "temp_store_d
2659e 69 72 65 63 74 6f 72 79 22 2c 20 53 51 4c 49 54  irectory", SQLIT
2659f 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
265a0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
265a1 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
265a2 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 73 71 6c  g8, 0, 1, 0, sql
265a3 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
265a4 6f 72 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ory, 0);.       
265a5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
265a6 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
265a7 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20  ow, 1, 1);.     
265a8 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69   }.    }else{.#i
265a9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
265aa 54 5f 57 53 44 0a 20 20 20 20 20 20 69 66 28 20  T_WSD.      if( 
265ab 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20 20  zRight[0] ){.   
265ac 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
265ad 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20       int res;.  
265ae 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
265af 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70  e3OsAccess(db->p
265b0 56 66 73 2c 20 7a 52 69 67 68 74 2c 20 53 51 4c  Vfs, zRight, SQL
265b1 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57  ITE_ACCESS_READW
265b2 52 49 54 45 2c 20 26 72 65 73 29 3b 0a 20 20 20  RITE, &res);.   
265b3 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
265b4 49 54 45 5f 4f 4b 20 7c 7c 20 72 65 73 3d 3d 30  ITE_OK || res==0
265b5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
265b6 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
265b7 61 72 73 65 2c 20 22 6e 6f 74 20 61 20 77 72 69  arse, "not a wri
265b8 74 61 62 6c 65 20 64 69 72 65 63 74 6f 72 79 22  table directory"
265b9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
265ba 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
265bb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
265bc 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45        if( SQLITE
265bd 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a 20  _TEMP_STORE==0. 
265be 20 20 20 20 20 20 7c 7c 20 28 53 51 4c 49 54 45        || (SQLITE
265bf 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 20 26  _TEMP_STORE==1 &
265c0 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65  & db->temp_store
265c1 3c 3d 31 29 0a 20 20 20 20 20 20 20 7c 7c 20 28  <=1).       || (
265c2 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
265c3 45 3d 3d 32 20 26 26 20 64 62 2d 3e 74 65 6d 70  E==2 && db->temp
265c4 5f 73 74 6f 72 65 3d 3d 31 29 0a 20 20 20 20 20  _store==1).     
265c5 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61   ){.        inva
265c6 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67  lidateTempStorag
265c7 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  e(pParse);.     
265c8 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
265c9 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 74 65  _free(sqlite3_te
265ca 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20  mp_directory);. 
265cb 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b       if( zRight[
265cc 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  0] ){.        sq
265cd 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
265ce 74 6f 72 79 20 3d 20 73 71 6c 69 74 65 33 44 62  tory = sqlite3Db
265cf 53 74 72 44 75 70 28 30 2c 20 7a 52 69 67 68 74  StrDup(0, zRight
265d0 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
265d1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
265d2 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d  temp_directory =
265d3 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64   0;.      }.#end
265d4 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
265d5 54 5f 57 53 44 20 2a 2f 0a 20 20 20 20 7d 0a 20  T_WSD */.    }. 
265d6 20 7d 65 6c 73 65 0a 0a 23 69 66 20 21 64 65 66   }else..#if !def
265d7 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
265d8 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
265d9 29 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28  ).#  if defined(
265da 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20  __APPLE__).#    
265db 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e  define SQLITE_EN
265dc 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
265dd 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20  LE 1.#  else.#  
265de 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
265df 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
265e0 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a  TYLE 0.#  endif.
265e1 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
265e2 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
265e3 5f 53 54 59 4c 45 0a 20 20 2f 2a 0a 20 20 20 2a  _STYLE.  /*.   *
265e4 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  *   PRAGMA [data
265e5 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79  base.]lock_proxy
265e6 5f 66 69 6c 65 0a 20 20 20 2a 2a 20 20 20 50 52  _file.   **   PR
265e7 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
265e8 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 20  lock_proxy_file 
265e9 3d 20 22 3a 61 75 74 6f 3a 22 7c 22 6c 6f 63 6b  = ":auto:"|"lock
265ea 5f 66 69 6c 65 5f 70 61 74 68 22 0a 20 20 20 2a  _file_path".   *
265eb 2a 0a 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f  *.   ** Return o
265ec 72 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  r set the value 
265ed 6f 66 20 74 68 65 20 6c 6f 63 6b 5f 70 72 6f 78  of the lock_prox
265ee 79 5f 66 69 6c 65 20 66 6c 61 67 2e 20 20 43 68  y_file flag.  Ch
265ef 61 6e 67 69 6e 67 0a 20 20 20 2a 2a 20 74 68 65  anging.   ** the
265f0 20 76 61 6c 75 65 20 73 65 74 73 20 61 20 73 70   value sets a sp
265f1 65 63 69 66 69 63 20 66 69 6c 65 20 74 6f 20 62  ecific file to b
265f2 65 20 75 73 65 64 20 66 6f 72 20 64 61 74 61 62  e used for datab
265f3 61 73 65 20 61 63 63 65 73 73 20 6c 6f 63 6b 73  ase access locks
265f4 2e 0a 20 20 20 2a 2a 0a 20 20 20 2a 2f 0a 20 20  ..   **.   */.  
265f5 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
265f6 6d 70 28 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f  mp(zLeft, "lock_
265f7 70 72 6f 78 79 5f 66 69 6c 65 22 29 3d 3d 30 20  proxy_file")==0 
265f8 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67  ){.    if( !zRig
265f9 68 74 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65  ht ){.      Page
265fa 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69  r *pPager = sqli
265fb 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 44  te3BtreePager(pD
265fc 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 63  b->pBt);.      c
265fd 68 61 72 20 2a 70 72 6f 78 79 5f 66 69 6c 65 5f  har *proxy_file_
265fe 70 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  path = NULL;.   
265ff 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20     sqlite3_file 
26600 2a 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33  *pFile = sqlite3
26601 50 61 67 65 72 46 69 6c 65 28 70 50 61 67 65 72  PagerFile(pPager
26602 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26603 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 46  OsFileControl(pF
26604 69 6c 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f  ile, SQLITE_GET_
26605 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 0a  LOCKPROXYFILE, .
26606 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26607 20 20 20 20 20 20 20 20 20 20 20 26 70 72 6f 78             &prox
26608 79 5f 66 69 6c 65 5f 70 61 74 68 29 3b 0a 20 20  y_file_path);.  
26609 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 70      .      if( p
2660a 72 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68 20 29  roxy_file_path )
2660b 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2660c 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
2660d 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  v, 1);.        s
2660e 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2660f 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
26610 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20  ME_NAME, .      
26611 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26612 20 20 20 20 20 20 20 20 22 6c 6f 63 6b 5f 70 72          "lock_pr
26613 6f 78 79 5f 66 69 6c 65 22 2c 20 53 51 4c 49 54  oxy_file", SQLIT
26614 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
26615 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26616 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
26617 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 70 72 6f  g8, 0, 1, 0, pro
26618 78 79 5f 66 69 6c 65 5f 70 61 74 68 2c 20 30 29  xy_file_path, 0)
26619 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2661a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2661b 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
2661c 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1);.      }.    
2661d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67  }else{.      Pag
2661e 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c  er *pPager = sql
2661f 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
26620 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20  Db->pBt);.      
26621 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46  sqlite3_file *pF
26622 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ile = sqlite3Pag
26623 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a  erFile(pPager);.
26624 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
26625 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b       if( zRight[
26626 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  0] ){.        re
26627 73 3d 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  s=sqlite3OsFileC
26628 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51  ontrol(pFile, SQ
26629 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_SET_LOCKPRO
2662a 58 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20  XYFILE, .       
2662b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2662c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 52                zR
2662d 69 67 68 74 29 3b 0a 20 20 20 20 20 20 7d 20 65  ight);.      } e
2662e 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 72 65  lse {.        re
2662f 73 3d 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  s=sqlite3OsFileC
26630 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51  ontrol(pFile, SQ
26631 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_SET_LOCKPRO
26632 58 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20  XYFILE, .       
26633 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26634 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55                NU
26635 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  LL);.      }.   
26636 20 20 20 69 66 28 20 72 65 73 21 3d 53 51 4c 49     if( res!=SQLI
26637 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
26638 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
26639 28 70 50 61 72 73 65 2c 20 22 66 61 69 6c 65 64  (pParse, "failed
2663a 20 74 6f 20 73 65 74 20 6c 6f 63 6b 20 70 72 6f   to set lock pro
2663b 78 79 20 66 69 6c 65 22 29 3b 0a 20 20 20 20 20  xy file");.     
2663c 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f     goto pragma_o
2663d 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
2663e 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
2663f 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
26640 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
26641 2a 2f 20 20 20 20 20 20 0a 20 20 20 20 0a 20 20  */      .    .  
26642 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  /*.  **   PRAGMA
26643 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e 63   [database.]sync
26644 68 72 6f 6e 6f 75 73 0a 20 20 2a 2a 20 20 20 50  hronous.  **   P
26645 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
26646 5d 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46  ]synchronous=OFF
26647 7c 4f 4e 7c 4e 4f 52 4d 41 4c 7c 46 55 4c 4c 0a  |ON|NORMAL|FULL.
26648 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    **.  ** Return
26649 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61   or set the loca
2664a 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73  l value of the s
2664b 79 6e 63 68 72 6f 6e 6f 75 73 20 66 6c 61 67 2e  ynchronous flag.
2664c 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a 20    Changing.  ** 
2664d 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20  the local value 
2664e 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63 68  does not make ch
2664f 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 69 73  anges to the dis
26650 6b 20 66 69 6c 65 20 61 6e 64 20 74 68 65 0a 20  k file and the. 
26651 20 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75   ** default valu
26652 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72  e will be restor
26653 65 64 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65  ed the next time
26654 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
26655 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 2e 0a 20 20  .  ** opened..  
26656 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
26657 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 73  StrICmp(zLeft,"s
26658 79 6e 63 68 72 6f 6e 6f 75 73 22 29 3d 3d 30 20  ynchronous")==0 
26659 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
2665a 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
2665b 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
2665c 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28 20  ma_out;.    if( 
2665d 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
2665e 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74   returnSingleInt
2665f 28 70 50 61 72 73 65 2c 20 22 73 79 6e 63 68 72  (pParse, "synchr
26660 6f 6e 6f 75 73 22 2c 20 70 44 62 2d 3e 73 61 66  onous", pDb->saf
26661 65 74 79 5f 6c 65 76 65 6c 2d 31 29 3b 0a 20 20  ety_level-1);.  
26662 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
26663 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  f( !db->autoComm
26664 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  it ){.        sq
26665 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
26666 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20  arse, .         
26667 20 20 20 22 53 61 66 65 74 79 20 6c 65 76 65 6c     "Safety level
26668 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e   may not be chan
26669 67 65 64 20 69 6e 73 69 64 65 20 61 20 74 72 61  ged inside a tra
2666a 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20  nsaction");.    
2666b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2666c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76   pDb->safety_lev
2666d 65 6c 20 3d 20 67 65 74 53 61 66 65 74 79 4c 65  el = getSafetyLe
2666e 76 65 6c 28 7a 52 69 67 68 74 29 2b 31 3b 0a 20  vel(zRight)+1;. 
2666f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
26670 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
26671 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
26672 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66  _PRAGMAS */..#if
26673 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
26674 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20 20  _FLAG_PRAGMAS.  
26675 69 66 28 20 66 6c 61 67 50 72 61 67 6d 61 28 70  if( flagPragma(p
26676 50 61 72 73 65 2c 20 7a 4c 65 66 74 2c 20 7a 52  Parse, zLeft, zR
26677 69 67 68 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20  ight) ){.    /* 
26678 54 68 65 20 66 6c 61 67 50 72 61 67 6d 61 28 29  The flagPragma()
26679 20 73 75 62 72 6f 75 74 69 6e 65 20 61 6c 73 6f   subroutine also
2667a 20 67 65 6e 65 72 61 74 65 73 20 61 6e 79 20 6e   generates any n
2667b 65 63 65 73 73 61 72 79 20 63 6f 64 65 0a 20 20  ecessary code.  
2667c 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f    ** there is no
2667d 74 68 69 6e 67 20 6d 6f 72 65 20 74 6f 20 64 6f  thing more to do
2667e 20 68 65 72 65 20 2a 2f 0a 20 20 7d 65 6c 73 65   here */.  }else
2667f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
26680 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47  E_OMIT_FLAG_PRAG
26681 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  MAS */..#ifndef 
26682 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45  SQLITE_OMIT_SCHE
26683 4d 41 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a  MA_PRAGMAS.  /*.
26684 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 61    **   PRAGMA ta
26685 62 6c 65 5f 69 6e 66 6f 28 3c 74 61 62 6c 65 3e  ble_info(<table>
26686 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75  ).  **.  ** Retu
26687 72 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  rn a single row 
26688 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
26689 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62  of the named tab
2668a 6c 65 2e 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20  le. The columns 
2668b 6f 66 0a 20 20 2a 2a 20 74 68 65 20 72 65 74 75  of.  ** the retu
2668c 72 6e 65 64 20 64 61 74 61 20 73 65 74 20 61 72  rned data set ar
2668d 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 69 64  e:.  **.  ** cid
2668e 3a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  :        Column 
2668f 69 64 20 28 6e 75 6d 62 65 72 65 64 20 66 72 6f  id (numbered fro
26690 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2c  m left to right,
26691 20 73 74 61 72 74 69 6e 67 20 61 74 20 30 29 0a   starting at 0).
26692 20 20 2a 2a 20 6e 61 6d 65 3a 20 20 20 20 20 20    ** name:      
26693 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 20 20 2a   Column name.  *
26694 2a 20 74 79 70 65 3a 20 20 20 20 20 20 20 43 6f  * type:       Co
26695 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e  lumn declaration
26696 20 74 79 70 65 2e 0a 20 20 2a 2a 20 6e 6f 74 6e   type..  ** notn
26697 75 6c 6c 3a 20 20 20 20 54 72 75 65 20 69 66 20  ull:    True if 
26698 27 4e 4f 54 20 4e 55 4c 4c 27 20 69 73 20 70 61  'NOT NULL' is pa
26699 72 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 64 65 63  rt of column dec
2669a 6c 61 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 66  laration.  ** df
2669b 6c 74 5f 76 61 6c 75 65 3a 20 54 68 65 20 64 65  lt_value: The de
2669c 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
2669d 74 68 65 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 61  the column, if a
2669e 6e 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ny..  */.  if( s
2669f 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
266a0 65 66 74 2c 20 22 74 61 62 6c 65 5f 69 6e 66 6f  eft, "table_info
266a1 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20  ")==0 && zRight 
266a2 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
266a3 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ab;.    if( sqli
266a4 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
266a5 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
266a6 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61  gma_out;.    pTa
266a7 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
266a8 61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c  able(db, zRight,
266a9 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
266aa 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Tab ){.      int
266ab 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 48   i;.      int nH
266ac 69 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  idden = 0;.     
266ad 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
266ae 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
266af 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 36 29  SetNumCols(v, 6)
266b0 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
266b1 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20 20 20 20  nMem = 6;.      
266b2 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
266b3 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
266b4 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c  AME_NAME, "cid",
266b5 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
266b6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
266b7 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
266b8 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
266b9 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f   "name", SQLITE_
266ba 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
266bb 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
266bc 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41  Name(v, 2, COLNA
266bd 4d 45 5f 4e 41 4d 45 2c 20 22 74 79 70 65 22 2c  ME_NAME, "type",
266be 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
266bf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
266c0 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
266c1 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  3, COLNAME_NAME,
266c2 20 22 6e 6f 74 6e 75 6c 6c 22 2c 20 53 51 4c 49   "notnull", SQLI
266c3 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
266c4 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
266c5 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f  ColName(v, 4, CO
266c6 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 66 6c  LNAME_NAME, "dfl
266c7 74 5f 76 61 6c 75 65 22 2c 20 53 51 4c 49 54 45  t_value", SQLITE
266c8 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
266c9 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
266ca 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e  lName(v, 5, COLN
266cb 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c 20  AME_NAME, "pk", 
266cc 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
266cd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 69 65        sqlite3Vie
266ce 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
266cf 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20  pParse, pTab);. 
266d0 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 43       for(i=0, pC
266d1 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 69  ol=pTab->aCol; i
266d2 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
266d3 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
266d4 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e 43     if( IsHiddenC
266d5 6f 6c 75 6d 6e 28 70 43 6f 6c 29 20 29 7b 0a 20  olumn(pCol) ){. 
266d6 20 20 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e           nHidden
266d7 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f  ++;.          co
266d8 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
266d9 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
266da 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
266db 50 5f 49 6e 74 65 67 65 72 2c 20 69 2d 6e 48 69  P_Integer, i-nHi
266dc 64 64 65 6e 2c 20 31 29 3b 0a 20 20 20 20 20 20  dden, 1);.      
266dd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
266de 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
266df 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c  8, 0, 2, 0, pCol
266e0 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
266e1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
266e2 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
266e3 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20  ing8, 0, 3, 0,. 
266e4 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
266e5 7a 54 79 70 65 20 3f 20 70 43 6f 6c 2d 3e 7a 54  zType ? pCol->zT
266e6 79 70 65 20 3a 20 22 22 2c 20 30 29 3b 0a 20 20  ype : "", 0);.  
266e7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
266e8 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
266e9 74 65 67 65 72 2c 20 28 70 43 6f 6c 2d 3e 6e 6f  teger, (pCol->no
266ea 74 4e 75 6c 6c 20 3f 20 31 20 3a 20 30 29 2c 20  tNull ? 1 : 0), 
266eb 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  4);.        if( 
266ec 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 29 7b 0a 20  pCol->pDflt ){. 
266ed 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 54           const T
266ee 6f 6b 65 6e 20 2a 70 20 3d 20 26 70 43 6f 6c 2d  oken *p = &pCol-
266ef 3e 70 44 66 6c 74 2d 3e 73 70 61 6e 3b 0a 20 20  >pDflt->span;.  
266f0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
266f1 70 2d 3e 7a 20 29 3b 0a 20 20 20 20 20 20 20 20  p->z );.        
266f2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
266f3 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
266f4 38 2c 20 30 2c 20 35 2c 20 30 2c 20 28 63 68 61  8, 0, 5, 0, (cha
266f5 72 2a 29 70 2d 3e 7a 2c 20 70 2d 3e 6e 29 3b 0a  r*)p->z, p->n);.
266f6 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
266f7 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
266f8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
266f9 5f 4e 75 6c 6c 2c 20 30 2c 20 35 29 3b 0a 20 20  _Null, 0, 5);.  
266fa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
266fb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
266fc 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
266fd 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79   pCol->isPrimKey
266fe 2c 20 36 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 6);.        sq
266ff 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26700 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
26701 20 31 2c 20 36 29 3b 0a 20 20 20 20 20 20 7d 0a   1, 6);.      }.
26702 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20      }.  }else.. 
26703 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
26704 43 6d 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65  Cmp(zLeft, "inde
26705 78 5f 69 6e 66 6f 22 29 3d 3d 30 20 26 26 20 7a  x_info")==0 && z
26706 52 69 67 68 74 20 29 7b 0a 20 20 20 20 49 6e 64  Right ){.    Ind
26707 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 54 61  ex *pIdx;.    Ta
26708 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69  ble *pTab;.    i
26709 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
2670a 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
2670b 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
2670c 20 20 20 20 70 49 64 78 20 3d 20 73 71 6c 69 74      pIdx = sqlit
2670d 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20  e3FindIndex(db, 
2670e 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20  zRight, zDb);.  
2670f 20 20 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20    if( pIdx ){.  
26710 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
26711 20 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54   pTab = pIdx->pT
26712 61 62 6c 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  able;.      sqli
26713 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
26714 73 28 76 2c 20 33 29 3b 0a 20 20 20 20 20 20 70  s(v, 3);.      p
26715 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b  Parse->nMem = 3;
26716 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26717 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
26718 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
26719 20 22 73 65 71 6e 6f 22 2c 20 53 51 4c 49 54 45   "seqno", SQLITE
2671a 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
2671b 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2671c 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
2671d 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c  AME_NAME, "cid",
2671e 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
2671f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26720 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
26721 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  2, COLNAME_NAME,
26722 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f   "name", SQLITE_
26723 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 66  STATIC);.      f
26724 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e  or(i=0; i<pIdx->
26725 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
26726 20 20 20 20 20 20 20 69 6e 74 20 63 6e 75 6d 20         int cnum 
26727 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
26728 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  [i];.        sql
26729 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2672a 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c  , OP_Integer, i,
2672b 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
2672c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2672d 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 63 6e  , OP_Integer, cn
2672e 75 6d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20  um, 2);.        
2672f 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 6e 43  assert( pTab->nC
26730 6f 6c 3e 63 6e 75 6d 20 29 3b 0a 20 20 20 20 20  ol>cnum );.     
26731 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26732 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
26733 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 54 61  g8, 0, 3, 0, pTa
26734 62 2d 3e 61 43 6f 6c 5b 63 6e 75 6d 5d 2e 7a 4e  b->aCol[cnum].zN
26735 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ame, 0);.       
26736 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26737 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
26738 6f 77 2c 20 31 2c 20 33 29 3b 0a 20 20 20 20 20  ow, 1, 3);.     
26739 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
2673a 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
2673b 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 69  trICmp(zLeft, "i
2673c 6e 64 65 78 5f 6c 69 73 74 22 29 3d 3d 30 20 26  ndex_list")==0 &
2673d 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  & zRight ){.    
2673e 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20  Index *pIdx;.   
2673f 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
26740 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
26741 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
26742 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
26743 74 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71  t;.    pTab = sq
26744 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
26745 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b  b, zRight, zDb);
26746 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b  .    if( pTab ){
26747 0a 20 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74  .      v = sqlit
26748 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
26749 29 3b 0a 20 20 20 20 20 20 70 49 64 78 20 3d 20  );.      pIdx = 
2674a 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20  pTab->pIndex;.  
2674b 20 20 20 20 69 66 28 20 70 49 64 78 20 29 7b 0a      if( pIdx ){.
2674c 20 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20          int i = 
2674d 30 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0; .        sqli
2674e 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
2674f 73 28 76 2c 20 33 29 3b 0a 20 20 20 20 20 20 20  s(v, 3);.       
26750 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
26751 33 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  3;.        sqlit
26752 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
26753 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
26754 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49  AME, "seq", SQLI
26755 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
26756 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
26757 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20  etColName(v, 1, 
26758 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e  COLNAME_NAME, "n
26759 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ame", SQLITE_STA
2675a 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
2675b 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2675c 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d  ame(v, 2, COLNAM
2675d 45 5f 4e 41 4d 45 2c 20 22 75 6e 69 71 75 65 22  E_NAME, "unique"
2675e 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2675f 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
26760 70 49 64 78 29 7b 0a 20 20 20 20 20 20 20 20 20  pIdx){.         
26761 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26762 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
26763 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  , i, 1);.       
26764 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26765 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
26766 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70 49 64  g8, 0, 2, 0, pId
26767 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  x->zName, 0);.  
26768 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
26769 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2676a 49 6e 74 65 67 65 72 2c 20 70 49 64 78 2d 3e 6f  Integer, pIdx->o
2676b 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 2c  nError!=OE_None,
2676c 20 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   3);.          s
2676d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2676e 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
2676f 2c 20 31 2c 20 33 29 3b 0a 20 20 20 20 20 20 20  , 1, 3);.       
26770 20 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20 20     ++i;.        
26771 20 20 70 49 64 78 20 3d 20 70 49 64 78 2d 3e 70    pIdx = pIdx->p
26772 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Next;.        }.
26773 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
26774 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c  }else..  if( sql
26775 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
26776 74 2c 20 22 64 61 74 61 62 61 73 65 5f 6c 69 73  t, "database_lis
26777 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e  t")==0 ){.    in
26778 74 20 69 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  t i;.    if( sql
26779 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
2677a 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
2677b 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73 71  agma_out;.    sq
2677c 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
2677d 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20 20 70  ols(v, 3);.    p
2677e 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b  Parse->nMem = 3;
2677f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26780 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
26781 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
26782 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  seq", SQLITE_STA
26783 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
26784 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
26785 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 1, COLNAME_NA
26786 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49  ME, "name", SQLI
26787 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
26788 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
26789 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e  lName(v, 2, COLN
2678a 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 69 6c 65 22  AME_NAME, "file"
2678b 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2678c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
2678d 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
2678e 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44        if( db->aD
2678f 62 5b 69 5d 2e 70 42 74 3d 3d 30 20 29 20 63 6f  b[i].pBt==0 ) co
26790 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 61 73  ntinue;.      as
26791 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 5d  sert( db->aDb[i]
26792 2e 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20  .zName!=0 );.   
26793 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26794 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
26795 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20 20  er, i, 1);.     
26796 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26797 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
26798 2c 20 30 2c 20 32 2c 20 30 2c 20 64 62 2d 3e 61  , 0, 2, 0, db->a
26799 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b  Db[i].zName, 0);
2679a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2679b 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
2679c 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c  tring8, 0, 3, 0,
2679d 0a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  .           sqli
2679e 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e  te3BtreeGetFilen
2679f 61 6d 65 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  ame(db->aDb[i].p
267a0 42 74 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 73  Bt), 0);.      s
267a1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
267a2 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
267a3 2c 20 31 2c 20 33 29 3b 0a 20 20 20 20 7d 0a 20  , 1, 3);.    }. 
267a4 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71   }else..  if( sq
267a5 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
267a6 66 74 2c 20 22 63 6f 6c 6c 61 74 69 6f 6e 5f 6c  ft, "collation_l
267a7 69 73 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ist")==0 ){.    
267a8 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 20 20 48  int i = 0;.    H
267a9 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 20  ashElem *p;.    
267aa 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
267ab 6d 43 6f 6c 73 28 76 2c 20 32 29 3b 0a 20 20 20  mCols(v, 2);.   
267ac 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
267ad 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  2;.    sqlite3Vd
267ae 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
267af 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
267b0 20 22 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53   "seq", SQLITE_S
267b1 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
267b2 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
267b3 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 1, COLNAME_
267b4 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53 51  NAME, "name", SQ
267b5 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
267b6 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61    for(p=sqliteHa
267b7 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f  shFirst(&db->aCo
267b8 6c 6c 53 65 71 29 3b 20 70 3b 20 70 3d 73 71 6c  llSeq); p; p=sql
267b9 69 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b  iteHashNext(p)){
267ba 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
267bb 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65 71  pColl = (CollSeq
267bc 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74   *)sqliteHashDat
267bd 61 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  a(p);.      sqli
267be 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
267bf 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2b 2b   OP_Integer, i++
267c0 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
267c1 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
267c2 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
267c3 32 2c 20 30 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61  2, 0, pColl->zNa
267c4 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  me, 0);.      sq
267c5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
267c6 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
267c7 20 31 2c 20 32 29 3b 0a 20 20 20 20 7d 0a 20 20   1, 2);.    }.  
267c8 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
267c9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45  SQLITE_OMIT_SCHE
267ca 4d 41 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23  MA_PRAGMAS */..#
267cb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
267cc 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20  IT_FOREIGN_KEY. 
267cd 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
267ce 43 6d 70 28 7a 4c 65 66 74 2c 20 22 66 6f 72 65  Cmp(zLeft, "fore
267cf 69 67 6e 5f 6b 65 79 5f 6c 69 73 74 22 29 3d 3d  ign_key_list")==
267d0 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20  0 && zRight ){. 
267d1 20 20 20 46 4b 65 79 20 2a 70 46 4b 3b 0a 20 20     FKey *pFK;.  
267d2 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
267d3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
267d4 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
267d5 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
267d6 75 74 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73  ut;.    pTab = s
267d7 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
267d8 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29  db, zRight, zDb)
267d9 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 29  ;.    if( pTab )
267da 7b 0a 20 20 20 20 20 20 76 20 3d 20 73 71 6c 69  {.      v = sqli
267db 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
267dc 65 29 3b 0a 20 20 20 20 20 20 70 46 4b 20 3d 20  e);.      pFK = 
267dd 70 54 61 62 2d 3e 70 46 4b 65 79 3b 0a 20 20 20  pTab->pFKey;.   
267de 20 20 20 69 66 28 20 70 46 4b 20 29 7b 0a 20 20     if( pFK ){.  
267df 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b        int i = 0;
267e0 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65   .        sqlite
267e1 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
267e2 76 2c 20 38 29 3b 0a 20 20 20 20 20 20 20 20 70  v, 8);.        p
267e3 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 38 3b  Parse->nMem = 8;
267e4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
267e5 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
267e6 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
267e7 45 2c 20 22 69 64 22 2c 20 53 51 4c 49 54 45 5f  E, "id", SQLITE_
267e8 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
267e9 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
267ea 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c  olName(v, 1, COL
267eb 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22  NAME_NAME, "seq"
267ec 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
267ed 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
267ee 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
267ef 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 2, COLNAME_NA
267f0 4d 45 2c 20 22 74 61 62 6c 65 22 2c 20 53 51 4c  ME, "table", SQL
267f1 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
267f2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
267f3 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c  SetColName(v, 3,
267f4 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
267f5 66 72 6f 6d 22 2c 20 53 51 4c 49 54 45 5f 53 54  from", SQLITE_ST
267f6 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
267f7 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
267f8 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f 4c 4e 41  Name(v, 4, COLNA
267f9 4d 45 5f 4e 41 4d 45 2c 20 22 74 6f 22 2c 20 53  ME_NAME, "to", S
267fa 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
267fb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
267fc 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
267fd 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  5, COLNAME_NAME,
267fe 20 22 6f 6e 5f 75 70 64 61 74 65 22 2c 20 53 51   "on_update", SQ
267ff 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
26800 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26801 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 36  eSetColName(v, 6
26802 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
26803 22 6f 6e 5f 64 65 6c 65 74 65 22 2c 20 53 51 4c  "on_delete", SQL
26804 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
26805 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26806 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 37 2c  SetColName(v, 7,
26807 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
26808 6d 61 74 63 68 22 2c 20 53 51 4c 49 54 45 5f 53  match", SQLITE_S
26809 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
2680a 77 68 69 6c 65 28 70 46 4b 29 7b 0a 20 20 20 20  while(pFK){.    
2680b 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
2680c 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
2680d 6a 3c 70 46 4b 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  j<pFK->nCol; j++
2680e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
2680f 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 46 4b 2d  har *zCol = pFK-
26810 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 3b 0a 20  >aCol[j].zCol;. 
26811 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20             char 
26812 2a 7a 4f 6e 55 70 64 61 74 65 20 3d 20 28 63 68  *zOnUpdate = (ch
26813 61 72 20 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28  ar *)actionName(
26814 70 46 4b 2d 3e 75 70 64 61 74 65 43 6f 6e 66 29  pFK->updateConf)
26815 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  ;.            ch
26816 61 72 20 2a 7a 4f 6e 44 65 6c 65 74 65 20 3d 20  ar *zOnDelete = 
26817 28 63 68 61 72 20 2a 29 61 63 74 69 6f 6e 4e 61  (char *)actionNa
26818 6d 65 28 70 46 4b 2d 3e 64 65 6c 65 74 65 43 6f  me(pFK->deleteCo
26819 6e 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nf);.           
2681a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2681b 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2681c 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  , i, 1);.       
2681d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2681e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2681f 65 67 65 72 2c 20 6a 2c 20 32 29 3b 0a 20 20 20  eger, j, 2);.   
26820 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
26821 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
26822 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20  _String8, 0, 3, 
26823 30 2c 20 70 46 4b 2d 3e 7a 54 6f 2c 20 30 29 3b  0, pFK->zTo, 0);
26824 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
26825 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
26826 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
26827 20 34 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20   4, 0,.         
26828 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26829 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b       pTab->aCol[
2682a 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72  pFK->aCol[j].iFr
2682b 6f 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  om].zName, 0);. 
2682c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2682d 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2682e 7a 43 6f 6c 20 3f 20 4f 50 5f 53 74 72 69 6e 67  zCol ? OP_String
2682f 38 20 3a 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  8 : OP_Null, 0, 
26830 35 2c 20 30 2c 20 7a 43 6f 6c 2c 20 30 29 3b 0a  5, 0, zCol, 0);.
26831 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
26832 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
26833 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
26834 36 2c 20 30 2c 20 7a 4f 6e 55 70 64 61 74 65 2c  6, 0, zOnUpdate,
26835 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
26836 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26837 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
26838 2c 20 30 2c 20 37 2c 20 30 2c 20 7a 4f 6e 44 65  , 0, 7, 0, zOnDe
26839 6c 65 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  lete, 0);.      
2683a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2683b 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
2683c 72 69 6e 67 38 2c 20 30 2c 20 38 2c 20 30 2c 20  ring8, 0, 8, 0, 
2683d 22 4e 4f 4e 45 22 2c 20 30 29 3b 0a 20 20 20 20  "NONE", 0);.    
2683e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2683f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26840 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 38 29  ResultRow, 1, 8)
26841 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
26842 20 20 20 20 20 20 20 20 2b 2b 69 3b 0a 20 20 20          ++i;.   
26843 20 20 20 20 20 20 20 70 46 4b 20 3d 20 70 46 4b         pFK = pFK
26844 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a 20 20 20  ->pNextFrom;.   
26845 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
26846 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e     }.  }else.#en
26847 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
26848 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
26849 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 0a 23 69 66  IGN_KEY) */..#if
2684a 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66  ndef NDEBUG.  if
2684b 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
2684c 28 7a 4c 65 66 74 2c 20 22 70 61 72 73 65 72 5f  (zLeft, "parser_
2684d 74 72 61 63 65 22 29 3d 3d 30 20 29 7b 0a 20 20  trace")==0 ){.  
2684e 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a    if( zRight ){.
2684f 20 20 20 20 20 20 69 66 28 20 67 65 74 42 6f 6f        if( getBoo
26850 6c 65 61 6e 28 7a 52 69 67 68 74 29 20 29 7b 0a  lean(zRight) ){.
26851 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
26852 61 72 73 65 72 54 72 61 63 65 28 73 74 64 65 72  arserTrace(stder
26853 72 2c 20 22 70 61 72 73 65 72 3a 20 22 29 3b 0a  r, "parser: ");.
26854 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
26855 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73       sqlite3Pars
26856 65 72 54 72 61 63 65 28 30 2c 20 30 29 3b 0a 20  erTrace(0, 0);. 
26857 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
26858 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  else.#endif..  /
26859 2a 20 52 65 69 6e 73 74 61 6c 6c 20 74 68 65 20  * Reinstall the 
2685a 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 66 75  LIKE and GLOB fu
2685b 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 76 61  nctions.  The va
2685c 72 69 61 6e 74 20 6f 66 20 4c 49 4b 45 0a 20 20  riant of LIKE.  
2685d 2a 2a 20 75 73 65 64 20 77 69 6c 6c 20 62 65 20  ** used will be 
2685e 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 20 6f  case sensitive o
2685f 72 20 6e 6f 74 20 64 65 70 65 6e 64 69 6e 67 20  r not depending 
26860 6f 6e 20 74 68 65 20 52 48 53 2e 0a 20 20 2a 2f  on the RHS..  */
26861 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
26862 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 63 61  rICmp(zLeft, "ca
26863 73 65 5f 73 65 6e 73 69 74 69 76 65 5f 6c 69 6b  se_sensitive_lik
26864 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  e")==0 ){.    if
26865 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ( zRight ){.    
26866 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65    sqlite3Registe
26867 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 64  rLikeFunctions(d
26868 62 2c 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52  b, getBoolean(zR
26869 69 67 68 74 29 29 3b 0a 20 20 20 20 7d 0a 20 20  ight));.    }.  
2686a 7d 65 6c 73 65 0a 0a 23 69 66 6e 64 65 66 20 53  }else..#ifndef S
2686b 51 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f  QLITE_INTEGRITY_
2686c 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 0a  CHECK_ERROR_MAX.
2686d 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
2686e 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f  INTEGRITY_CHECK_
2686f 45 52 52 4f 52 5f 4d 41 58 20 31 30 30 0a 23 65  ERROR_MAX 100.#e
26870 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
26871 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
26872 49 54 59 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 50  ITY_CHECK.  /* P
26873 72 61 67 6d 61 20 22 71 75 69 63 6b 5f 63 68 65  ragma "quick_che
26874 63 6b 22 20 69 73 20 61 6e 20 65 78 70 65 72 69  ck" is an experi
26875 6d 65 6e 74 61 6c 20 72 65 64 75 63 65 64 20 76  mental reduced v
26876 65 72 73 69 6f 6e 20 6f 66 20 0a 20 20 2a 2a 20  ersion of .  ** 
26877 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20  integrity_check 
26878 64 65 73 69 67 6e 65 64 20 74 6f 20 64 65 74 65  designed to dete
26879 63 74 20 6d 6f 73 74 20 64 61 74 61 62 61 73 65  ct most database
2687a 20 63 6f 72 72 75 70 74 69 6f 6e 0a 20 20 2a 2a   corruption.  **
2687b 20 77 69 74 68 6f 75 74 20 6d 6f 73 74 20 6f 66   without most of
2687c 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66   the overhead of
2687d 20 61 20 66 75 6c 6c 20 69 6e 74 65 67 72 69 74   a full integrit
2687e 79 2d 63 68 65 63 6b 2e 0a 20 20 2a 2f 0a 20 20  y-check..  */.  
2687f 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
26880 6d 70 28 7a 4c 65 66 74 2c 20 22 69 6e 74 65 67  mp(zLeft, "integ
26881 72 69 74 79 5f 63 68 65 63 6b 22 29 3d 3d 30 0a  rity_check")==0.
26882 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72     || sqlite3Str
26883 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 71 75 69  ICmp(zLeft, "qui
26884 63 6b 5f 63 68 65 63 6b 22 29 3d 3d 30 20 0a 20  ck_check")==0 . 
26885 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a   ){.    int i, j
26886 2c 20 61 64 64 72 2c 20 6d 78 45 72 72 3b 0a 0a  , addr, mxErr;..
26887 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 61 74      /* Code that
26888 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20   appears at the 
26889 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 74 65 67  end of the integ
2688a 72 69 74 79 20 63 68 65 63 6b 2e 20 20 49 66 20  rity check.  If 
2688b 6e 6f 20 65 72 72 6f 72 0a 20 20 20 20 2a 2a 20  no error.    ** 
2688c 6d 65 73 73 61 67 65 73 20 68 61 76 65 20 62 65  messages have be
2688d 65 6e 20 67 65 6e 65 72 61 74 65 64 2c 20 6f 75  en generated, ou
2688e 74 70 75 74 20 4f 4b 2e 20 20 4f 74 68 65 72 77  tput OK.  Otherw
2688f 69 73 65 20 6f 75 74 70 75 74 20 74 68 65 0a 20  ise output the. 
26890 20 20 20 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73     ** error mess
26891 61 67 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  age.    */.    s
26892 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
26893 4f 70 4c 69 73 74 20 65 6e 64 43 6f 64 65 5b 5d  OpList endCode[]
26894 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f   = {.      { OP_
26895 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 31 2c 20  AddImm,      1, 
26896 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20  0,        0},   
26897 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 7b   /* 0 */.      {
26898 20 4f 50 5f 49 66 4e 65 67 2c 20 20 20 20 20 20   OP_IfNeg,      
26899 20 31 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d   1, 0,        0}
2689a 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20  ,    /* 1 */.   
2689b 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
2689c 20 20 20 20 20 30 2c 20 33 2c 20 20 20 20 20 20       0, 3,      
2689d 20 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f    0},    /* 2 */
2689e 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75  .      { OP_Resu
2689f 6c 74 52 6f 77 2c 20 20 20 33 2c 20 31 2c 20 20  ltRow,   3, 1,  
268a0 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b        0},.    };
268a1 0a 0a 20 20 20 20 69 6e 74 20 69 73 51 75 69 63  ..    int isQuic
268a2 6b 20 3d 20 28 7a 4c 65 66 74 5b 30 5d 3d 3d 27  k = (zLeft[0]=='
268a3 71 27 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69  q');..    /* Ini
268a4 74 69 61 6c 69 7a 65 20 74 68 65 20 56 44 42 45  tialize the VDBE
268a5 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20   program */.    
268a6 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
268a7 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20  chema(pParse) ) 
268a8 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
268a9 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
268aa 6d 20 3d 20 36 3b 0a 20 20 20 20 73 71 6c 69 74  m = 6;.    sqlit
268ab 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
268ac 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
268ad 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
268ae 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
268af 4e 41 4d 45 2c 20 22 69 6e 74 65 67 72 69 74 79  NAME, "integrity
268b0 5f 63 68 65 63 6b 22 2c 20 53 51 4c 49 54 45 5f  _check", SQLITE_
268b1 53 54 41 54 49 43 29 3b 0a 0a 20 20 20 20 2f 2a  STATIC);..    /*
268b2 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d   Set the maximum
268b3 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 2a 2f 0a   error count */.
268b4 20 20 20 20 6d 78 45 72 72 20 3d 20 53 51 4c 49      mxErr = SQLI
268b5 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  TE_INTEGRITY_CHE
268b6 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a 20 20  CK_ERROR_MAX;.  
268b7 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a    if( zRight ){.
268b8 20 20 20 20 20 20 6d 78 45 72 72 20 3d 20 61 74        mxErr = at
268b9 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20  oi(zRight);.    
268ba 20 20 69 66 28 20 6d 78 45 72 72 3c 3d 30 20 29    if( mxErr<=0 )
268bb 7b 0a 20 20 20 20 20 20 20 20 6d 78 45 72 72 20  {.        mxErr 
268bc 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49  = SQLITE_INTEGRI
268bd 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d  TY_CHECK_ERROR_M
268be 41 58 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  AX;.      }.    
268bf 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
268c0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
268c1 74 65 67 65 72 2c 20 6d 78 45 72 72 2c 20 31 29  teger, mxErr, 1)
268c2 3b 20 20 2f 2a 20 72 65 67 5b 31 5d 20 68 6f 6c  ;  /* reg[1] hol
268c3 64 73 20 65 72 72 6f 72 73 20 6c 65 66 74 20 2a  ds errors left *
268c4 2f 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 61 6e 20  /..    /* Do an 
268c5 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 20  integrity check 
268c6 6f 6e 20 65 61 63 68 20 64 61 74 61 62 61 73 65  on each database
268c7 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 66 6f 72   file */.    for
268c8 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
268c9 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 48 61 73   i++){.      Has
268ca 68 45 6c 65 6d 20 2a 78 3b 0a 20 20 20 20 20 20  hElem *x;.      
268cb 48 61 73 68 20 2a 70 54 62 6c 73 3b 0a 20 20 20  Hash *pTbls;.   
268cc 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a     int cnt = 0;.
268cd 0a 20 20 20 20 20 20 69 66 28 20 4f 4d 49 54 5f  .      if( OMIT_
268ce 54 45 4d 50 44 42 20 26 26 20 69 3d 3d 31 20 29  TEMPDB && i==1 )
268cf 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20   continue;..    
268d0 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
268d1 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
268d2 2c 20 69 29 3b 0a 20 20 20 20 20 20 61 64 64 72  , i);.      addr
268d3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
268d4 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73  dOp1(v, OP_IfPos
268d5 2c 20 31 29 3b 20 2f 2a 20 48 61 6c 74 20 69 66  , 1); /* Halt if
268d6 20 6f 75 74 20 6f 66 20 65 72 72 6f 72 73 20 2a   out of errors *
268d7 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
268d8 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
268d9 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Halt, 0, 0);.   
268da 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
268db 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
268dc 0a 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20 61 6e  ..      /* Do an
268dd 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b   integrity check
268de 20 6f 66 20 74 68 65 20 42 2d 54 72 65 65 0a 20   of the B-Tree. 
268df 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
268e0 20 42 65 67 69 6e 20 62 79 20 66 69 6c 6c 69 6e   Begin by fillin
268e1 67 20 72 65 67 69 73 74 65 72 73 20 32 2c 20 33  g registers 2, 3
268e2 2c 20 2e 2e 2e 20 77 69 74 68 20 74 68 65 20 72  , ... with the r
268e3 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65 72  oot pages number
268e4 73 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 61  s.      ** for a
268e5 6c 6c 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e  ll tables and in
268e6 64 69 63 65 73 20 69 6e 20 74 68 65 20 64 61 74  dices in the dat
268e7 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  abase..      */.
268e8 20 20 20 20 20 20 70 54 62 6c 73 20 3d 20 26 64        pTbls = &d
268e9 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65 6d  b->aDb[i].pSchem
268ea 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 20 20  a->tblHash;.    
268eb 20 20 66 6f 72 28 78 3d 73 71 6c 69 74 65 48 61    for(x=sqliteHa
268ec 73 68 46 69 72 73 74 28 70 54 62 6c 73 29 3b 20  shFirst(pTbls); 
268ed 78 3b 20 78 3d 73 71 6c 69 74 65 48 61 73 68 4e  x; x=sqliteHashN
268ee 65 78 74 28 78 29 29 7b 0a 20 20 20 20 20 20 20  ext(x)){.       
268ef 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73   Table *pTab = s
268f0 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 78 29  qliteHashData(x)
268f1 3b 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78 20  ;.        Index 
268f2 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 20 20 73  *pIdx;.        s
268f3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
268f4 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
268f5 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e  pTab->tnum, 2+cn
268f6 74 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 2b  t);.        cnt+
268f7 2b 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70  +;.        for(p
268f8 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
268f9 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
268fa 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
268fb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
268fc 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
268fd 65 67 65 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d  eger, pIdx->tnum
268fe 2c 20 32 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20  , 2+cnt);.      
268ff 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20      cnt++;.     
26900 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
26901 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65      /* Make sure
26902 20 73 75 66 66 69 63 69 65 6e 74 20 6e 75 6d 62   sufficient numb
26903 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  er of registers 
26904 68 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61  have been alloca
26905 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ted */.      if(
26906 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3c 20   pParse->nMem < 
26907 63 6e 74 2b 34 20 29 7b 0a 20 20 20 20 20 20 20  cnt+4 ){.       
26908 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
26909 63 6e 74 2b 34 3b 0a 20 20 20 20 20 20 7d 0a 0a  cnt+4;.      }..
2690a 20 20 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20        /* Do the 
2690b 62 2d 74 72 65 65 20 69 6e 74 65 67 72 69 74 79  b-tree integrity
2690c 20 63 68 65 63 6b 73 20 2a 2f 0a 20 20 20 20 20   checks */.     
2690d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2690e 70 33 28 76 2c 20 4f 50 5f 49 6e 74 65 67 72 69  p3(v, OP_Integri
2690f 74 79 43 6b 2c 20 32 2c 20 63 6e 74 2c 20 31 29  tyCk, 2, cnt, 1)
26910 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26911 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28  dbeChangeP5(v, (
26912 75 38 29 69 29 3b 0a 20 20 20 20 20 20 61 64 64  u8)i);.      add
26913 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
26914 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp1(v, OP_IsNu
26915 6c 6c 2c 20 32 29 3b 0a 20 20 20 20 20 20 73 71  ll, 2);.      sq
26916 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
26917 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
26918 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  , 3, 0,.        
26919 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2691a 64 62 2c 20 22 2a 2a 2a 20 69 6e 20 64 61 74 61  db, "*** in data
2691b 62 61 73 65 20 25 73 20 2a 2a 2a 5c 6e 22 2c 20  base %s ***\n", 
2691c 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
2691d 29 2c 0a 20 20 20 20 20 20 20 20 20 50 34 5f 44  ),.         P4_D
2691e 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 73  YNAMIC);.      s
2691f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
26920 28 76 2c 20 4f 50 5f 4d 6f 76 65 2c 20 32 2c 20  (v, OP_Move, 2, 
26921 34 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  4, 1);.      sql
26922 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
26923 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 34 2c 20  , OP_Concat, 4, 
26924 33 2c 20 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  3, 2);.      sql
26925 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26926 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
26927 32 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  2, 1);.      sql
26928 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
26929 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20 20  (v, addr);..    
2692a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61    /* Make sure a
2692b 6c 6c 20 74 68 65 20 69 6e 64 69 63 65 73 20 61  ll the indices a
2692c 72 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 63  re constructed c
2692d 6f 72 72 65 63 74 6c 79 2e 0a 20 20 20 20 20 20  orrectly..      
2692e 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 78 3d 73  */.      for(x=s
2692f 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 70  qliteHashFirst(p
26930 54 62 6c 73 29 3b 20 78 20 26 26 20 21 69 73 51  Tbls); x && !isQ
26931 75 69 63 6b 3b 20 78 3d 73 71 6c 69 74 65 48 61  uick; x=sqliteHa
26932 73 68 4e 65 78 74 28 78 29 29 7b 0a 20 20 20 20  shNext(x)){.    
26933 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
26934 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  = sqliteHashData
26935 28 78 29 3b 0a 20 20 20 20 20 20 20 20 49 6e 64  (x);.        Ind
26936 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20  ex *pIdx;.      
26937 20 20 69 6e 74 20 6c 6f 6f 70 54 6f 70 3b 0a 0a    int loopTop;..
26938 20 20 20 20 20 20 20 20 69 66 28 20 70 54 61 62          if( pTab
26939 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 20 63 6f  ->pIndex==0 ) co
2693a 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
2693b 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
2693c 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
2693d 66 50 6f 73 2c 20 31 29 3b 20 20 2f 2a 20 53 74  fPos, 1);  /* St
2693e 6f 70 20 69 66 20 6f 75 74 20 6f 66 20 65 72 72  op if out of err
2693f 6f 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 73  ors */.        s
26940 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26941 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20  (v, OP_Halt, 0, 
26942 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
26943 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
26944 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20  v, addr);.      
26945 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
26946 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 50 61  leAndIndices(pPa
26947 72 73 65 2c 20 70 54 61 62 2c 20 31 2c 20 4f 50  rse, pTab, 1, OP
26948 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20  _OpenRead);.    
26949 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2694a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2694b 67 65 72 2c 20 30 2c 20 32 29 3b 20 20 2f 2a 20  ger, 0, 2);  /* 
2694c 72 65 67 28 32 29 20 77 69 6c 6c 20 63 6f 75 6e  reg(2) will coun
2694d 74 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20 20  t entries */.   
2694e 20 20 20 20 20 6c 6f 6f 70 54 6f 70 20 3d 20 73       loopTop = s
2694f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26950 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 31  (v, OP_Rewind, 1
26951 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
26952 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26953 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 32 2c  v, OP_AddImm, 2,
26954 20 31 29 3b 20 20 20 2f 2a 20 69 6e 63 72 65 6d   1);   /* increm
26955 65 6e 74 20 65 6e 74 72 79 20 63 6f 75 6e 74 20  ent entry count 
26956 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  */.        for(j
26957 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =0, pIdx=pTab->p
26958 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
26959 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a  x=pIdx->pNext, j
2695a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
2695b 6e 74 20 6a 6d 70 32 3b 0a 20 20 20 20 20 20 20  nt jmp2;.       
2695c 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
2695d 56 64 62 65 4f 70 4c 69 73 74 20 69 64 78 45 72  VdbeOpList idxEr
2695e 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20  r[] = {.        
2695f 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c      { OP_AddImm,
26960 20 20 20 20 20 20 31 2c 20 2d 31 2c 20 20 30 7d        1, -1,  0}
26961 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
26962 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20  OP_String8,     
26963 30 2c 20 20 33 2c 20 20 30 7d 2c 20 20 20 20 2f  0,  3,  0},    /
26964 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 1 */.         
26965 20 20 20 7b 20 4f 50 5f 52 6f 77 69 64 2c 20 20     { OP_Rowid,  
26966 20 20 20 20 20 31 2c 20 20 34 2c 20 20 30 7d 2c       1,  4,  0},
26967 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
26968 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30  P_String8,     0
26969 2c 20 20 35 2c 20 20 30 7d 2c 20 20 20 20 2f 2a  ,  5,  0},    /*
2696a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   3 */.          
2696b 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20    { OP_String8, 
2696c 20 20 20 20 30 2c 20 20 36 2c 20 20 30 7d 2c 20      0,  6,  0}, 
2696d 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 20     /* 4 */.     
2696e 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63         { OP_Conc
2696f 61 74 2c 20 20 20 20 20 20 34 2c 20 20 33 2c 20  at,      4,  3, 
26970 20 33 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   3},.           
26971 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20   { OP_Concat,   
26972 20 20 20 35 2c 20 20 33 2c 20 20 33 7d 2c 0a 20     5,  3,  3},. 
26973 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
26974 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 36 2c 20  Concat,      6, 
26975 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20   3,  3},.       
26976 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74       { OP_Result
26977 52 6f 77 2c 20 20 20 33 2c 20 20 31 2c 20 20 30  Row,   3,  1,  0
26978 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  },.            {
26979 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20 20 20   OP_IfPos,      
2697a 20 31 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20 20   1,  0,  0},    
2697b 2f 2a 20 39 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 9 */.        
2697c 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74 2c 20 20      { OP_Halt,  
2697d 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d        0,  0,  0}
2697e 2c 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20  ,.          };. 
2697f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
26980 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79  GenerateIndexKey
26981 28 70 50 61 72 73 65 2c 20 70 49 64 78 2c 20 31  (pParse, pIdx, 1
26982 2c 20 33 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  , 3, 1);.       
26983 20 20 20 6a 6d 70 32 20 3d 20 73 71 6c 69 74 65     jmp2 = sqlite
26984 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
26985 50 5f 46 6f 75 6e 64 2c 20 6a 2b 32 2c 20 30 2c  P_Found, j+2, 0,
26986 20 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61   3);.          a
26987 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
26988 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
26989 72 61 79 53 69 7a 65 28 69 64 78 45 72 72 29 2c  raySize(idxErr),
2698a 20 69 64 78 45 72 72 29 3b 0a 20 20 20 20 20 20   idxErr);.      
2698b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2698c 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b  hangeP4(v, addr+
2698d 31 2c 20 22 72 6f 77 69 64 20 22 2c 20 50 34 5f  1, "rowid ", P4_
2698e 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
2698f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
26990 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 33  angeP4(v, addr+3
26991 2c 20 22 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d  , " missing from
26992 20 69 6e 64 65 78 20 22 2c 20 50 34 5f 53 54 41   index ", P4_STA
26993 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20  TIC);.          
26994 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
26995 65 50 34 28 76 2c 20 61 64 64 72 2b 34 2c 20 70  eP4(v, addr+4, p
26996 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53  Idx->zName, P4_S
26997 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
26998 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
26999 70 48 65 72 65 28 76 2c 20 61 64 64 72 2b 39 29  pHere(v, addr+9)
2699a 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2699b 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2699c 76 2c 20 6a 6d 70 32 29 3b 0a 20 20 20 20 20 20  v, jmp2);.      
2699d 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
2699e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2699f 20 4f 50 5f 4e 65 78 74 2c 20 31 2c 20 6c 6f 6f   OP_Next, 1, loo
269a0 70 54 6f 70 2b 31 29 3b 0a 20 20 20 20 20 20 20  pTop+1);.       
269a1 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
269a2 48 65 72 65 28 76 2c 20 6c 6f 6f 70 54 6f 70 29  Here(v, loopTop)
269a3 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
269a4 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
269a5 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
269a6 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b  =pIdx->pNext, j+
269a7 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74  +){.          st
269a8 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
269a9 70 4c 69 73 74 20 63 6e 74 49 64 78 5b 5d 20 3d  pList cntIdx[] =
269aa 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
269ab 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20  { OP_Integer,   
269ac 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 0a 20     0,  3,  0},. 
269ad 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
269ae 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 20 20 30  _Rewind,       0
269af 2c 20 20 30 2c 20 20 30 7d 2c 20 20 2f 2a 20 31  ,  0,  0},  /* 1
269b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
269b1 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20   { OP_AddImm,   
269b2 20 20 20 20 33 2c 20 20 31 2c 20 20 30 7d 2c 0a      3,  1,  0},.
269b3 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
269b4 50 5f 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20  P_Next,         
269b5 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 2f 2a 20  0,  0,  0},  /* 
269b6 33 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  3 */.           
269b7 20 20 7b 20 4f 50 5f 45 71 2c 20 20 20 20 20 20    { OP_Eq,      
269b8 20 20 20 20 20 32 2c 20 20 30 2c 20 20 33 7d 2c       2,  0,  3},
269b9 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 20 20    /* 4 */.      
269ba 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49         { OP_AddI
269bb 6d 6d 2c 20 20 20 20 20 20 20 31 2c 20 2d 31 2c  mm,       1, -1,
269bc 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    0},.          
269bd 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
269be 20 20 20 20 20 20 30 2c 20 20 32 2c 20 20 30 7d        0,  2,  0}
269bf 2c 20 20 2f 2a 20 36 20 2a 2f 0a 20 20 20 20 20  ,  /* 6 */.     
269c0 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72          { OP_Str
269c1 69 6e 67 38 2c 20 20 20 20 20 20 30 2c 20 20 33  ing8,      0,  3
269c2 2c 20 20 30 7d 2c 20 20 2f 2a 20 37 20 2a 2f 0a  ,  0},  /* 7 */.
269c3 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
269c4 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 20  P_Concat,       
269c5 33 2c 20 20 32 2c 20 20 32 7d 2c 0a 20 20 20 20  3,  2,  2},.    
269c6 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65           { OP_Re
269c7 73 75 6c 74 52 6f 77 2c 20 20 20 20 32 2c 20 20  sultRow,    2,  
269c8 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20  1,  0},.        
269c9 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 61    };.          a
269ca 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
269cb 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
269cc 50 6f 73 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  Pos, 1);.       
269cd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
269ce 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  dOp2(v, OP_Halt,
269cf 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
269d0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
269d1 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
269d2 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d            addr =
269d3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
269d4 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
269d5 7a 65 28 63 6e 74 49 64 78 29 2c 20 63 6e 74 49  ze(cntIdx), cntI
269d6 64 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  dx);.          s
269d7 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
269d8 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 6a 2b  P1(v, addr+1, j+
269d9 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  2);.          sq
269da 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
269db 32 28 76 2c 20 61 64 64 72 2b 31 2c 20 61 64 64  2(v, addr+1, add
269dc 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r+4);.          
269dd 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
269de 65 50 31 28 76 2c 20 61 64 64 72 2b 33 2c 20 6a  eP1(v, addr+3, j
269df 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +2);.          s
269e0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
269e1 50 32 28 76 2c 20 61 64 64 72 2b 33 2c 20 61 64  P2(v, addr+3, ad
269e2 64 72 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20  dr+2);.         
269e3 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
269e4 48 65 72 65 28 76 2c 20 61 64 64 72 2b 34 29 3b  Here(v, addr+4);
269e5 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
269e6 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
269e7 2c 20 61 64 64 72 2b 36 2c 20 0a 20 20 20 20 20  , addr+6, .     
269e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
269e9 22 77 72 6f 6e 67 20 23 20 6f 66 20 65 6e 74 72  "wrong # of entr
269ea 69 65 73 20 69 6e 20 69 6e 64 65 78 20 22 2c 20  ies in index ", 
269eb 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
269ec 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
269ed 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
269ee 72 2b 37 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  r+7, pIdx->zName
269ef 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
269f0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20        }.      } 
269f1 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72 20  .    }.    addr 
269f2 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
269f3 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
269f4 69 7a 65 28 65 6e 64 43 6f 64 65 29 2c 20 65 6e  ize(endCode), en
269f5 64 43 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69  dCode);.    sqli
269f6 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
269f7 76 2c 20 61 64 64 72 2c 20 2d 6d 78 45 72 72 29  v, addr, -mxErr)
269f8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
269f9 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
269fa 72 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  r+1);.    sqlite
269fb 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
269fc 20 61 64 64 72 2b 32 2c 20 22 6f 6b 22 2c 20 50   addr+2, "ok", P
269fd 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 65 6c  4_STATIC);.  }el
269fe 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
269ff 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
26a00 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66  TY_CHECK */..#if
26a01 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
26a02 5f 55 54 46 31 36 0a 20 20 2f 2a 0a 20 20 2a 2a  _UTF16.  /*.  **
26a03 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f 64 69     PRAGMA encodi
26a04 6e 67 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  ng.  **   PRAGMA
26a05 20 65 6e 63 6f 64 69 6e 67 20 3d 20 22 75 74 66   encoding = "utf
26a06 2d 38 22 7c 22 75 74 66 2d 31 36 22 7c 22 75 74  -8"|"utf-16"|"ut
26a07 66 2d 31 36 6c 65 22 7c 22 75 74 66 2d 31 36 62  f-16le"|"utf-16b
26a08 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20  e".  **.  ** In 
26a09 69 74 73 20 66 69 72 73 74 20 66 6f 72 6d 2c 20  its first form, 
26a0a 74 68 69 73 20 70 72 61 67 6d 61 20 72 65 74 75  this pragma retu
26a0b 72 6e 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  rns the encoding
26a0c 20 6f 66 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a   of the main.  *
26a0d 2a 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74  * database. If t
26a0e 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6e  he database is n
26a0f 6f 74 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20  ot initialized, 
26a10 69 74 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  it is initialize
26a11 64 20 6e 6f 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  d now..  **.  **
26a12 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d   The second form
26a13 20 6f 66 20 74 68 69 73 20 70 72 61 67 6d 61 20   of this pragma 
26a14 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68  is a no-op if th
26a15 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
26a16 66 69 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6e 6f  file.  ** has no
26a17 74 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69  t already been i
26a18 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49 6e 20 74  nitialized. In t
26a19 68 69 73 20 63 61 73 65 20 69 74 20 73 65 74 73  his case it sets
26a1a 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20 20 2a   the default.  *
26a1b 2a 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 74 20  * encoding that 
26a1c 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72  will be used for
26a1d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
26a1e 73 65 20 66 69 6c 65 20 69 66 20 61 20 6e 65 77  se file if a new
26a1f 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20 63 72   file.  ** is cr
26a20 65 61 74 65 64 2e 20 49 66 20 61 6e 20 65 78 69  eated. If an exi
26a21 73 74 69 6e 67 20 6d 61 69 6e 20 64 61 74 61 62  sting main datab
26a22 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  ase file is open
26a23 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a  ed, then the.  *
26a24 2a 20 64 65 66 61 75 6c 74 20 74 65 78 74 20 65  * default text e
26a25 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20  ncoding for the 
26a26 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73  existing databas
26a27 65 20 69 73 20 75 73 65 64 2e 0a 20 20 2a 2a 20  e is used..  ** 
26a28 0a 20 20 2a 2a 20 49 6e 20 61 6c 6c 20 63 61 73  .  ** In all cas
26a29 65 73 20 6e 65 77 20 64 61 74 61 62 61 73 65 73  es new databases
26a2a 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74   created using t
26a2b 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e  he ATTACH comman
26a2c 64 20 61 72 65 0a 20 20 2a 2a 20 63 72 65 61 74  d are.  ** creat
26a2d 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 73 61  ed to use the sa
26a2e 6d 65 20 64 65 66 61 75 6c 74 20 74 65 78 74 20  me default text 
26a2f 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20  encoding as the 
26a30 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 49  main database. I
26a31 66 0a 20 20 2a 2a 20 74 68 65 20 6d 61 69 6e 20  f.  ** the main 
26a32 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74  database has not
26a33 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
26a34 64 20 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64  d and/or created
26a35 20 77 68 65 6e 20 41 54 54 41 43 48 0a 20 20 2a   when ATTACH.  *
26a36 2a 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 74  * is executed, t
26a37 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66 6f  his is done befo
26a38 72 65 20 74 68 65 20 41 54 54 41 43 48 20 6f 70  re the ATTACH op
26a39 65 72 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20  eration..  **.  
26a3a 2a 2a 20 49 6e 20 74 68 65 20 73 65 63 6f 6e 64  ** In the second
26a3b 20 66 6f 72 6d 20 74 68 69 73 20 70 72 61 67 6d   form this pragm
26a3c 61 20 73 65 74 73 20 74 68 65 20 74 65 78 74 20  a sets the text 
26a3d 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 75  encoding to be u
26a3e 73 65 64 20 69 6e 0a 20 20 2a 2a 20 6e 65 77 20  sed in.  ** new 
26a3f 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 63  database files c
26a40 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 69  reated using thi
26a41 73 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  s database handl
26a42 65 2e 20 49 74 20 69 73 20 6f 6e 6c 79 0a 20 20  e. It is only.  
26a43 2a 2a 20 75 73 65 66 75 6c 20 69 66 20 69 6e 76  ** useful if inv
26a44 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  oked immediately
26a45 20 61 66 74 65 72 20 74 68 65 20 6d 61 69 6e 20   after the main 
26a46 64 61 74 61 62 61 73 65 20 69 0a 20 20 2a 2f 0a  database i.  */.
26a47 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
26a48 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 65 6e 63  ICmp(zLeft, "enc
26a49 6f 64 69 6e 67 22 29 3d 3d 30 20 29 7b 0a 20 20  oding")==0 ){.  
26a4a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
26a4b 74 72 75 63 74 20 45 6e 63 4e 61 6d 65 20 7b 0a  truct EncName {.
26a4c 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
26a4d 65 3b 0a 20 20 20 20 20 20 75 38 20 65 6e 63 3b  e;.      u8 enc;
26a4e 0a 20 20 20 20 7d 20 65 6e 63 6e 61 6d 65 73 5b  .    } encnames[
26a4f 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 22 55  ] = {.      { "U
26a50 54 46 38 22 2c 20 20 20 20 20 53 51 4c 49 54 45  TF8",     SQLITE
26a51 5f 55 54 46 38 20 20 20 20 20 20 20 20 7d 2c 0a  _UTF8        },.
26a52 20 20 20 20 20 20 7b 20 22 55 54 46 2d 38 22 2c        { "UTF-8",
26a53 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 20      SQLITE_UTF8 
26a54 20 20 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d 75         },  /* Mu
26a55 73 74 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b 31  st be element [1
26a56 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55 54  ] */.      { "UT
26a57 46 2d 31 36 6c 65 22 2c 20 53 51 4c 49 54 45 5f  F-16le", SQLITE_
26a58 55 54 46 31 36 4c 45 20 20 20 20 20 7d 2c 20 20  UTF16LE     },  
26a59 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c 65 6d 65  /* Must be eleme
26a5a 6e 74 20 5b 32 5d 20 2a 2f 0a 20 20 20 20 20 20  nt [2] */.      
26a5b 7b 20 22 55 54 46 2d 31 36 62 65 22 2c 20 53 51  { "UTF-16be", SQ
26a5c 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20  LITE_UTF16BE    
26a5d 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65 20   },  /* Must be 
26a5e 65 6c 65 6d 65 6e 74 20 5b 33 5d 20 2a 2f 0a 20  element [3] */. 
26a5f 20 20 20 20 20 7b 20 22 55 54 46 31 36 6c 65 22       { "UTF16le"
26a60 2c 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  ,  SQLITE_UTF16L
26a61 45 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b  E     },.      {
26a62 20 22 55 54 46 31 36 62 65 22 2c 20 20 53 51 4c   "UTF16be",  SQL
26a63 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20  ITE_UTF16BE     
26a64 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d  },.      { "UTF-
26a65 31 36 22 2c 20 20 20 30 20 20 20 20 20 20 20 20  16",   0        
26a66 20 20 20 20 20 20 20 20 20 20 7d 2c 20 2f 2a 20            }, /* 
26a67 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
26a68 56 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55  VE */.      { "U
26a69 54 46 31 36 22 2c 20 20 20 20 30 20 20 20 20 20  TF16",    0     
26a6a 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 20               }, 
26a6b 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  /* SQLITE_UTF16N
26a6c 41 54 49 56 45 20 2a 2f 0a 20 20 20 20 20 20 7b  ATIVE */.      {
26a6d 20 30 2c 20 30 20 7d 0a 20 20 20 20 7d 3b 0a 20   0, 0 }.    };. 
26a6e 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20     const struct 
26a6f 45 6e 63 4e 61 6d 65 20 2a 70 45 6e 63 3b 0a 20  EncName *pEnc;. 
26a70 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
26a71 7b 20 20 20 20 2f 2a 20 22 50 52 41 47 4d 41 20  {    /* "PRAGMA 
26a72 65 6e 63 6f 64 69 6e 67 22 20 2a 2f 0a 20 20 20  encoding" */.   
26a73 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
26a74 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
26a75 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
26a76 75 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ut;.      sqlite
26a77 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
26a78 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  v, 1);.      sql
26a79 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
26a7a 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
26a7b 5f 4e 41 4d 45 2c 20 22 65 6e 63 6f 64 69 6e 67  _NAME, "encoding
26a7c 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
26a7d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26a7e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26a7f 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 29 3b  _String8, 0, 1);
26a80 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
26a81 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45 5f 55  ncnames[SQLITE_U
26a82 54 46 38 5d 2e 65 6e 63 3d 3d 53 51 4c 49 54 45  TF8].enc==SQLITE
26a83 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 20 20 61  _UTF8 );.      a
26a84 73 73 65 72 74 28 20 65 6e 63 6e 61 6d 65 73 5b  ssert( encnames[
26a85 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2e  SQLITE_UTF16LE].
26a86 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc==SQLITE_UTF1
26a87 36 4c 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73  6LE );.      ass
26a88 65 72 74 28 20 65 6e 63 6e 61 6d 65 73 5b 53 51  ert( encnames[SQ
26a89 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2e 65 6e  LITE_UTF16BE].en
26a8a 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
26a8b 45 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  E );.      sqlit
26a8c 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
26a8d 2c 20 2d 31 2c 20 65 6e 63 6e 61 6d 65 73 5b 45  , -1, encnames[E
26a8e 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 5d 2e  NC(pParse->db)].
26a8f 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43  zName, P4_STATIC
26a90 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26a91 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26a92 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31  _ResultRow, 1, 1
26a93 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 20 20 20  );.    }else{   
26a94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a95 20 20 20 20 20 2f 2a 20 22 50 52 41 47 4d 41 20       /* "PRAGMA 
26a96 65 6e 63 6f 64 69 6e 67 20 3d 20 58 58 58 22 20  encoding = XXX" 
26a97 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79  */.      /* Only
26a98 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   change the valu
26a99 65 20 6f 66 20 73 71 6c 69 74 65 2e 65 6e 63 20  e of sqlite.enc 
26a9a 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  if the database 
26a9b 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 0a 20 20  handle is not.  
26a9c 20 20 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a      ** initializ
26a9d 65 64 2e 20 49 66 20 74 68 65 20 6d 61 69 6e 20  ed. If the main 
26a9e 64 61 74 61 62 61 73 65 20 65 78 69 73 74 73 2c  database exists,
26a9f 20 74 68 65 20 6e 65 77 20 73 71 6c 69 74 65 2e   the new sqlite.
26aa0 65 6e 63 20 76 61 6c 75 65 0a 20 20 20 20 20 20  enc value.      
26aa1 2a 2a 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77  ** will be overw
26aa2 72 69 74 74 65 6e 20 77 68 65 6e 20 74 68 65 20  ritten when the 
26aa3 73 63 68 65 6d 61 20 69 73 20 6e 65 78 74 20 6c  schema is next l
26aa4 6f 61 64 65 64 2e 20 49 66 20 69 74 20 64 6f 65  oaded. If it doe
26aa5 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 61  s not.      ** a
26aa6 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c 20 69  lready exists, i
26aa7 74 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65  t will be create
26aa8 64 20 74 6f 20 75 73 65 20 74 68 65 20 6e 65 77  d to use the new
26aa9 20 65 6e 63 6f 64 69 6e 67 20 76 61 6c 75 65 2e   encoding value.
26aaa 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
26aab 69 66 28 20 0a 20 20 20 20 20 20 20 20 21 28 44  if( .        !(D
26aac 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c  bHasProperty(db,
26aad 20 30 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61   0, DB_SchemaLoa
26aae 64 65 64 29 29 20 7c 7c 20 0a 20 20 20 20 20 20  ded)) || .      
26aaf 20 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 28    DbHasProperty(
26ab0 64 62 2c 20 30 2c 20 44 42 5f 45 6d 70 74 79 29  db, 0, DB_Empty)
26ab1 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20   .      ){.     
26ab2 20 20 20 66 6f 72 28 70 45 6e 63 3d 26 65 6e 63     for(pEnc=&enc
26ab3 6e 61 6d 65 73 5b 30 5d 3b 20 70 45 6e 63 2d 3e  names[0]; pEnc->
26ab4 7a 4e 61 6d 65 3b 20 70 45 6e 63 2b 2b 29 7b 0a  zName; pEnc++){.
26ab5 20 20 20 20 20 20 20 20 20 20 69 66 28 20 30 3d            if( 0=
26ab6 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
26ab7 7a 52 69 67 68 74 2c 20 70 45 6e 63 2d 3e 7a 4e  zRight, pEnc->zN
26ab8 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ame) ){.        
26ab9 20 20 20 20 45 4e 43 28 70 50 61 72 73 65 2d 3e      ENC(pParse->
26aba 64 62 29 20 3d 20 70 45 6e 63 2d 3e 65 6e 63 20  db) = pEnc->enc 
26abb 3f 20 70 45 6e 63 2d 3e 65 6e 63 20 3a 20 53 51  ? pEnc->enc : SQ
26abc 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
26abd 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
26abe 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
26abf 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
26ac0 20 20 20 69 66 28 20 21 70 45 6e 63 2d 3e 7a 4e     if( !pEnc->zN
26ac1 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ame ){.         
26ac2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
26ac3 28 70 50 61 72 73 65 2c 20 22 75 6e 73 75 70 70  (pParse, "unsupp
26ac4 6f 72 74 65 64 20 65 6e 63 6f 64 69 6e 67 3a 20  orted encoding: 
26ac5 25 73 22 2c 20 7a 52 69 67 68 74 29 3b 0a 20 20  %s", zRight);.  
26ac6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
26ac7 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
26ac8 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
26ac9 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 23 69  MIT_UTF16 */..#i
26aca 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
26acb 54 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e  T_SCHEMA_VERSION
26acc 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20  _PRAGMAS.  /*.  
26acd 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74  **   PRAGMA [dat
26ace 61 62 61 73 65 2e 5d 73 63 68 65 6d 61 5f 76 65  abase.]schema_ve
26acf 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 50 52 41  rsion.  **   PRA
26ad0 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73  GMA [database.]s
26ad1 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 20 3d 20  chema_version = 
26ad2 3c 69 6e 74 65 67 65 72 3e 0a 20 20 2a 2a 0a 20  <integer>.  **. 
26ad3 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
26ad4 74 61 62 61 73 65 2e 5d 75 73 65 72 5f 76 65 72  tabase.]user_ver
26ad5 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 50 52 41 47  sion.  **   PRAG
26ad6 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 75 73  MA [database.]us
26ad7 65 72 5f 76 65 72 73 69 6f 6e 20 3d 20 3c 69 6e  er_version = <in
26ad8 74 65 67 65 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a  teger>.  **.  **
26ad9 20 54 68 65 20 70 72 61 67 6d 61 27 73 20 73 63   The pragma's sc
26ada 68 65 6d 61 5f 76 65 72 73 69 6f 6e 20 61 6e 64  hema_version and
26adb 20 75 73 65 72 5f 76 65 72 73 69 6f 6e 20 61 72   user_version ar
26adc 65 20 75 73 65 64 20 74 6f 20 73 65 74 20 6f 72  e used to set or
26add 20 67 65 74 0a 20 20 2a 2a 20 74 68 65 20 76 61   get.  ** the va
26ade 6c 75 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d  lue of the schem
26adf 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20 75 73  a-version and us
26ae0 65 72 2d 76 65 72 73 69 6f 6e 2c 20 72 65 73 70  er-version, resp
26ae1 65 63 74 69 76 65 6c 79 2e 20 42 6f 74 68 0a 20  ectively. Both. 
26ae2 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 2d 76   ** the schema-v
26ae3 65 72 73 69 6f 6e 20 61 6e 64 20 74 68 65 20 75  ersion and the u
26ae4 73 65 72 2d 76 65 72 73 69 6f 6e 20 61 72 65 20  ser-version are 
26ae5 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  32-bit signed in
26ae6 74 65 67 65 72 73 0a 20 20 2a 2a 20 73 74 6f 72  tegers.  ** stor
26ae7 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ed in the databa
26ae8 73 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 0a  se header..  **.
26ae9 20 20 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 2d    ** The schema-
26aea 63 6f 6f 6b 69 65 20 69 73 20 75 73 75 61 6c 6c  cookie is usuall
26aeb 79 20 6f 6e 6c 79 20 6d 61 6e 69 70 75 6c 61 74  y only manipulat
26aec 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79  ed internally by
26aed 20 53 51 4c 69 74 65 2e 20 49 74 0a 20 20 2a 2a   SQLite. It.  **
26aee 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
26aef 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e 65 76  by SQLite whenev
26af0 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  er the database 
26af1 73 63 68 65 6d 61 20 69 73 20 6d 6f 64 69 66 69  schema is modifi
26af2 65 64 20 28 62 79 0a 20 20 2a 2a 20 63 72 65 61  ed (by.  ** crea
26af3 74 69 6e 67 20 6f 72 20 64 72 6f 70 70 69 6e 67  ting or dropping
26af4 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65   a table or inde
26af5 78 29 2e 20 54 68 65 20 73 63 68 65 6d 61 20 76  x). The schema v
26af6 65 72 73 69 6f 6e 20 69 73 20 75 73 65 64 20 62  ersion is used b
26af7 79 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 65 61  y.  ** SQLite ea
26af8 63 68 20 74 69 6d 65 20 61 20 71 75 65 72 79 20  ch time a query 
26af9 69 73 20 65 78 65 63 75 74 65 64 20 74 6f 20 65  is executed to e
26afa 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 69  nsure that the i
26afb 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 0a 20 20  nternal cache.  
26afc 2a 2a 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61  ** of the schema
26afd 20 75 73 65 64 20 77 68 65 6e 20 63 6f 6d 70 69   used when compi
26afe 6c 69 6e 67 20 74 68 65 20 53 51 4c 20 71 75 65  ling the SQL que
26aff 72 79 20 6d 61 74 63 68 65 73 20 74 68 65 20 73  ry matches the s
26b00 63 68 65 6d 61 20 6f 66 0a 20 20 2a 2a 20 74 68  chema of.  ** th
26b01 65 20 64 61 74 61 62 61 73 65 20 61 67 61 69 6e  e database again
26b02 73 74 20 77 68 69 63 68 20 74 68 65 20 63 6f 6d  st which the com
26b03 70 69 6c 65 64 20 71 75 65 72 79 20 69 73 20 61  piled query is a
26b04 63 74 75 61 6c 6c 79 20 65 78 65 63 75 74 65 64  ctually executed
26b05 2e 0a 20 20 2a 2a 20 53 75 62 76 65 72 74 69 6e  ..  ** Subvertin
26b06 67 20 74 68 69 73 20 6d 65 63 68 61 6e 69 73 6d  g this mechanism
26b07 20 62 79 20 75 73 69 6e 67 20 22 50 52 41 47 4d   by using "PRAGM
26b08 41 20 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e  A schema_version
26b09 22 20 74 6f 20 6d 6f 64 69 66 79 0a 20 20 2a 2a  " to modify.  **
26b0a 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73   the schema-vers
26b0b 69 6f 6e 20 69 73 20 70 6f 74 65 6e 74 69 61 6c  ion is potential
26b0c 6c 79 20 64 61 6e 67 65 72 6f 75 73 20 61 6e 64  ly dangerous and
26b0d 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 70 72 6f   may lead to pro
26b0e 67 72 61 6d 0a 20 20 2a 2a 20 63 72 61 73 68 65  gram.  ** crashe
26b0f 73 20 6f 72 20 64 61 74 61 62 61 73 65 20 63 6f  s or database co
26b10 72 72 75 70 74 69 6f 6e 2e 20 55 73 65 20 77 69  rruption. Use wi
26b11 74 68 20 63 61 75 74 69 6f 6e 21 0a 20 20 2a 2a  th caution!.  **
26b12 0a 20 20 2a 2a 20 54 68 65 20 75 73 65 72 2d 76  .  ** The user-v
26b13 65 72 73 69 6f 6e 20 69 73 20 6e 6f 74 20 75 73  ersion is not us
26b14 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79  ed internally by
26b15 20 53 51 4c 69 74 65 2e 20 49 74 20 6d 61 79 20   SQLite. It may 
26b16 62 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20  be used by.  ** 
26b17 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 66 6f 72  applications for
26b18 20 61 6e 79 20 70 75 72 70 6f 73 65 2e 0a 20 20   any purpose..  
26b19 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
26b1a 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
26b1b 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 22 29  schema_version")
26b1c 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74  ==0 .   || sqlit
26b1d 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
26b1e 20 22 75 73 65 72 5f 76 65 72 73 69 6f 6e 22 29   "user_version")
26b1f 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74  ==0 .   || sqlit
26b20 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
26b21 20 22 66 72 65 65 6c 69 73 74 5f 63 6f 75 6e 74   "freelist_count
26b22 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20 20 20 20  ")==0 .  ){.    
26b23 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 20 20 20 2f  int iCookie;   /
26b24 2a 20 43 6f 6f 6b 69 65 20 69 6e 64 65 78 2e 20  * Cookie index. 
26b25 30 20 66 6f 72 20 73 63 68 65 6d 61 2d 63 6f 6f  0 for schema-coo
26b26 6b 69 65 2c 20 36 20 66 6f 72 20 75 73 65 72 2d  kie, 6 for user-
26b27 63 6f 6f 6b 69 65 2e 20 2a 2f 0a 20 20 20 20 73  cookie. */.    s
26b28 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
26b29 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20  ree(v, iDb);.   
26b2a 20 73 77 69 74 63 68 28 20 7a 4c 65 66 74 5b 30   switch( zLeft[0
26b2b 5d 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  ] ){.      case 
26b2c 27 73 27 3a 20 63 61 73 65 20 27 53 27 3a 0a 20  's': case 'S':. 
26b2d 20 20 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d         iCookie =
26b2e 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   0;.        brea
26b2f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 27 66  k;.      case 'f
26b30 27 3a 20 63 61 73 65 20 27 46 27 3a 0a 20 20 20  ': case 'F':.   
26b31 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 31       iCookie = 1
26b32 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20  ;.        iDb = 
26b33 28 2d 31 2a 28 69 44 62 2b 31 29 29 3b 0a 20 20  (-1*(iDb+1));.  
26b34 20 20 20 20 20 20 61 73 73 65 72 74 28 69 44 62        assert(iDb
26b35 3c 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 62 72  <=0);.        br
26b36 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75  eak;.      defau
26b37 6c 74 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f  lt:.        iCoo
26b38 6b 69 65 20 3d 20 35 3b 0a 20 20 20 20 20 20 20  kie = 5;.       
26b39 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
26b3a 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 26 26     if( zRight &&
26b3b 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 20   iDb>=0 ){.     
26b3c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73 70   /* Write the sp
26b3d 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65 20 76  ecified cookie v
26b3e 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20 73 74  alue */.      st
26b3f 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
26b40 70 4c 69 73 74 20 73 65 74 43 6f 6f 6b 69 65 5b  pList setCookie[
26b41 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b 20  ] = {.        { 
26b42 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20  OP_Transaction, 
26b43 20 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20 20     0,  1,  0},  
26b44 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20    /* 0 */.      
26b45 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20    { OP_Integer, 
26b46 20 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 30         0,  1,  0
26b47 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20  },    /* 1 */.  
26b48 20 20 20 20 20 20 7b 20 4f 50 5f 53 65 74 43 6f        { OP_SetCo
26b49 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20 30  okie,      0,  0
26b4a 2c 20 20 31 7d 2c 20 20 20 20 2f 2a 20 32 20 2a  ,  1},    /* 2 *
26b4b 2f 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20  /.      };.     
26b4c 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69   int addr = sqli
26b4d 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
26b4e 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73 65  (v, ArraySize(se
26b4f 74 43 6f 6f 6b 69 65 29 2c 20 73 65 74 43 6f 6f  tCookie), setCoo
26b50 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  kie);.      sqli
26b51 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
26b52 76 2c 20 61 64 64 72 2c 20 69 44 62 29 3b 0a 20  v, addr, iDb);. 
26b53 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26b54 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
26b55 2b 31 2c 20 61 74 6f 69 28 7a 52 69 67 68 74 29  +1, atoi(zRight)
26b56 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26b57 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
26b58 61 64 64 72 2b 32 2c 20 69 44 62 29 3b 0a 20 20  addr+2, iDb);.  
26b59 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
26b5a 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b  hangeP2(v, addr+
26b5b 32 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20 20  2, iCookie);.   
26b5c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
26b5d 20 52 65 61 64 20 74 68 65 20 73 70 65 63 69 66   Read the specif
26b5e 69 65 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65  ied cookie value
26b5f 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63   */.      static
26b60 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73   const VdbeOpLis
26b61 74 20 72 65 61 64 43 6f 6f 6b 69 65 5b 5d 20 3d  t readCookie[] =
26b62 20 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f   {.        { OP_
26b63 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 20 20 20  ReadCookie,     
26b64 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20 20   0,  1,  0},    
26b65 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 0 */.        
26b66 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  { OP_ResultRow, 
26b67 20 20 20 20 20 20 31 2c 20 20 31 2c 20 20 30 7d        1,  1,  0}
26b68 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20  .      };.      
26b69 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74  int addr = sqlit
26b6a 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
26b6b 76 2c 20 41 72 72 61 79 53 69 7a 65 28 72 65 61  v, ArraySize(rea
26b6c 64 43 6f 6f 6b 69 65 29 2c 20 72 65 61 64 43 6f  dCookie), readCo
26b6d 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  okie);.      sql
26b6e 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
26b6f 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29 3b 0a  (v, addr, iDb);.
26b70 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26b71 65 43 68 61 6e 67 65 50 33 28 76 2c 20 61 64 64  eChangeP3(v, add
26b72 72 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20 20  r, iCookie);.   
26b73 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
26b74 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
26b75 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26b76 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
26b77 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
26b78 7a 4c 65 66 74 2c 20 53 51 4c 49 54 45 5f 54 52  zLeft, SQLITE_TR
26b79 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d 0a  ANSIENT);.    }.
26b7a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
26b7b 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43  * SQLITE_OMIT_SC
26b7c 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41  HEMA_VERSION_PRA
26b7d 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 20 64 65 66  GMAS */..#if def
26b7e 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
26b7f 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  G) || defined(SQ
26b80 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 2f 2a 0a  LITE_TEST).  /*.
26b81 20 20 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20    ** Report the 
26b82 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
26b83 20 66 69 6c 65 20 6c 6f 67 73 20 66 6f 72 20 61   file logs for a
26b84 6c 6c 20 64 61 74 61 62 61 73 65 73 0a 20 20 2a  ll databases.  *
26b85 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
26b86 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6c  trICmp(zLeft, "l
26b87 6f 63 6b 5f 73 74 61 74 75 73 22 29 3d 3d 30 20  ock_status")==0 
26b88 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ){.    static co
26b89 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20  nst char *const 
26b8a 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 5d 20 3d 20 7b  azLockName[] = {
26b8b 0a 20 20 20 20 20 20 22 75 6e 6c 6f 63 6b 65 64  .      "unlocked
26b8c 22 2c 20 22 73 68 61 72 65 64 22 2c 20 22 72 65  ", "shared", "re
26b8d 73 65 72 76 65 64 22 2c 20 22 70 65 6e 64 69 6e  served", "pendin
26b8e 67 22 2c 20 22 65 78 63 6c 75 73 69 76 65 22 0a  g", "exclusive".
26b8f 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 69      };.    int i
26b90 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
26b91 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 32  eSetNumCols(v, 2
26b92 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  );.    pParse->n
26b93 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c  Mem = 2;.    sql
26b94 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
26b95 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
26b96 5f 4e 41 4d 45 2c 20 22 64 61 74 61 62 61 73 65  _NAME, "database
26b97 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
26b98 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
26b99 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
26b9a 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
26b9b 20 22 73 74 61 74 75 73 22 2c 20 53 51 4c 49 54   "status", SQLIT
26b9c 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66  E_STATIC);.    f
26b9d 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
26b9e 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42  b; i++){.      B
26b9f 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 20  tree *pBt;.     
26ba0 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a   Pager *pPager;.
26ba1 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
26ba2 20 2a 7a 53 74 61 74 65 20 3d 20 22 75 6e 6b 6e   *zState = "unkn
26ba3 6f 77 6e 22 3b 0a 20 20 20 20 20 20 69 6e 74 20  own";.      int 
26ba4 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d  j;.      if( db-
26ba5 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d 30  >aDb[i].zName==0
26ba6 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
26ba7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26ba8 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
26ba9 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 64 62 2d  g8, 0, 1, 0, db-
26baa 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 50  >aDb[i].zName, P
26bab 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
26bac 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69   pBt = db->aDb[i
26bad 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28  ].pBt;.      if(
26bae 20 70 42 74 3d 3d 30 20 7c 7c 20 28 70 50 61 67   pBt==0 || (pPag
26baf 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
26bb0 65 50 61 67 65 72 28 70 42 74 29 29 3d 3d 30 20  ePager(pBt))==0 
26bb1 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 74 61 74  ){.        zStat
26bb2 65 20 3d 20 22 63 6c 6f 73 65 64 22 3b 0a 20 20  e = "closed";.  
26bb3 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71      }else if( sq
26bb4 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
26bb5 6f 6c 28 64 62 2c 20 69 20 3f 20 64 62 2d 3e 61  ol(db, i ? db->a
26bb6 44 62 5b 69 5d 2e 7a 4e 61 6d 65 20 3a 20 30 2c  Db[i].zName : 0,
26bb7 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
26bb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bb9 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46 43         SQLITE_FC
26bba 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 2c 20 26  NTL_LOCKSTATE, &
26bbb 6a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  j)==SQLITE_OK ){
26bbc 0a 20 20 20 20 20 20 20 20 20 7a 53 74 61 74 65  .         zState
26bbd 20 3d 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 6a 5d   = azLockName[j]
26bbe 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
26bbf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26bc0 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
26bc1 20 30 2c 20 32 2c 20 30 2c 20 7a 53 74 61 74 65   0, 2, 0, zState
26bc2 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
26bc3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26bc4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
26bc5 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20 20  ltRow, 1, 2);.  
26bc6 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 0a 23 65 6e    }..  }else.#en
26bc7 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
26bc8 54 45 5f 53 53 45 0a 20 20 2f 2a 0a 20 20 2a 2a  TE_SSE.  /*.  **
26bc9 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
26bca 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
26bcb 65 6d 65 6e 74 73 20 74 61 62 6c 65 20 65 78 69  ements table exi
26bcc 73 74 73 2e 20 20 43 72 65 61 74 65 20 69 74 0a  sts.  Create it.
26bcd 20 20 2a 2a 20 69 66 20 69 74 20 64 6f 65 73 20    ** if it does 
26bce 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  not..  */.  if( 
26bcf 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
26bd0 4c 65 66 74 2c 20 22 63 72 65 61 74 65 5f 73 71  Left, "create_sq
26bd1 6c 69 74 65 5f 73 74 61 74 65 6d 65 6e 74 5f 74  lite_statement_t
26bd2 61 62 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  able")==0 ){.   
26bd3 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69   extern int sqli
26bd4 74 65 33 43 72 65 61 74 65 53 74 61 74 65 6d 65  te3CreateStateme
26bd5 6e 74 73 54 61 62 6c 65 28 50 61 72 73 65 2a 29  ntsTable(Parse*)
26bd6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65  ;.    sqlite3Cre
26bd7 61 74 65 53 74 61 74 65 6d 65 6e 74 73 54 61 62  ateStatementsTab
26bd8 6c 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 65  le(pParse);.  }e
26bd9 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  lse.#endif..#if 
26bda 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
26bdb 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
26bdc 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6b 65  rICmp(zLeft, "ke
26bdd 79 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74  y")==0 && zRight
26bde 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
26bdf 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  key(db, zRight, 
26be0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
26be1 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73  zRight));.  }els
26be2 65 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  e.  if( sqlite3S
26be3 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 72  trICmp(zLeft, "r
26be4 65 6b 65 79 22 29 3d 3d 30 20 26 26 20 7a 52 69  ekey")==0 && zRi
26be5 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ght ){.    sqlit
26be6 65 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 52 69  e3_rekey(db, zRi
26be7 67 68 74 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ght, sqlite3Strl
26be8 65 6e 33 30 28 7a 52 69 67 68 74 29 29 3b 0a 20  en30(zRight));. 
26be9 20 7d 65 6c 73 65 0a 20 20 69 66 28 20 7a 52 69   }else.  if( zRi
26bea 67 68 74 20 26 26 20 28 73 71 6c 69 74 65 33 53  ght && (sqlite3S
26beb 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 68  trICmp(zLeft, "h
26bec 65 78 6b 65 79 22 29 3d 3d 30 20 7c 7c 0a 20 20  exkey")==0 ||.  
26bed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
26bee 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
26bef 65 66 74 2c 20 22 68 65 78 72 65 6b 65 79 22 29  eft, "hexrekey")
26bf0 3d 3d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  ==0) ){.    int 
26bf1 69 2c 20 68 31 2c 20 68 32 3b 0a 20 20 20 20 63  i, h1, h2;.    c
26bf2 68 61 72 20 7a 4b 65 79 5b 34 30 5d 3b 0a 20 20  har zKey[40];.  
26bf3 20 20 66 6f 72 28 69 3d 30 3b 20 28 68 31 20 3d    for(i=0; (h1 =
26bf4 20 7a 52 69 67 68 74 5b 69 5d 29 21 3d 30 20 26   zRight[i])!=0 &
26bf5 26 20 28 68 32 20 3d 20 7a 52 69 67 68 74 5b 69  & (h2 = zRight[i
26bf6 2b 31 5d 29 21 3d 30 3b 20 69 2b 3d 32 29 7b 0a  +1])!=0; i+=2){.
26bf7 20 20 20 20 20 20 68 31 20 2b 3d 20 39 2a 28 31        h1 += 9*(1
26bf8 26 28 68 31 3e 3e 36 29 29 3b 0a 20 20 20 20 20  &(h1>>6));.     
26bf9 20 68 32 20 2b 3d 20 39 2a 28 31 26 28 68 32 3e   h2 += 9*(1&(h2>
26bfa 3e 36 29 29 3b 0a 20 20 20 20 20 20 7a 4b 65 79  >6));.      zKey
26bfb 5b 69 2f 32 5d 20 3d 20 28 68 32 20 26 20 30 78  [i/2] = (h2 & 0x
26bfc 30 66 29 20 7c 20 28 28 68 31 20 26 20 30 78 66  0f) | ((h1 & 0xf
26bfd 29 3c 3c 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20  )<<4);.    }.   
26bfe 20 69 66 28 20 28 7a 4c 65 66 74 5b 33 5d 20 26   if( (zLeft[3] &
26bff 20 30 78 66 29 3d 3d 30 78 62 20 29 7b 0a 20 20   0xf)==0xb ){.  
26c00 20 20 20 20 73 71 6c 69 74 65 33 5f 6b 65 79 28      sqlite3_key(
26c01 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b 0a  db, zKey, i/2);.
26c02 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
26c03 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 64   sqlite3_rekey(d
26c04 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b 0a 20  b, zKey, i/2);. 
26c05 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e     }.  }else.#en
26c06 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 48  dif.#if SQLITE_H
26c07 41 53 5f 43 4f 44 45 43 20 7c 7c 20 64 65 66 69  AS_CODEC || defi
26c08 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
26c09 45 5f 43 45 52 4f 44 29 0a 20 20 69 66 28 20 73  E_CEROD).  if( s
26c0a 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
26c0b 65 66 74 2c 20 22 61 63 74 69 76 61 74 65 5f 65  eft, "activate_e
26c0c 78 74 65 6e 73 69 6f 6e 73 22 29 3d 3d 30 20 29  xtensions")==0 )
26c0d 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53  {.#if SQLITE_HAS
26c0e 5f 43 4f 44 45 43 0a 20 20 20 20 69 66 28 20 73  _CODEC.    if( s
26c0f 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
26c10 52 69 67 68 74 2c 20 22 73 65 65 2d 22 2c 20 34  Right, "see-", 4
26c11 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 78  )==0 ){.      ex
26c12 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65  tern void sqlite
26c13 33 5f 61 63 74 69 76 61 74 65 5f 73 65 65 28 63  3_activate_see(c
26c14 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20  onst char*);.   
26c15 20 20 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76     sqlite3_activ
26c16 61 74 65 5f 73 65 65 28 26 7a 52 69 67 68 74 5b  ate_see(&zRight[
26c17 34 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  4]);.    }.#endi
26c18 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
26c19 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a 20 20 20  ENABLE_CEROD.   
26c1a 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
26c1b 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 22 63 65  ICmp(zRight, "ce
26c1c 72 6f 64 2d 22 2c 20 36 29 3d 3d 30 20 29 7b 0a  rod-", 6)==0 ){.
26c1d 20 20 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69        extern voi
26c1e 64 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61  d sqlite3_activa
26c1f 74 65 5f 63 65 72 6f 64 28 63 6f 6e 73 74 20 63  te_cerod(const c
26c20 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c  har*);.      sql
26c21 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65  ite3_activate_ce
26c22 72 6f 64 28 26 7a 52 69 67 68 74 5b 36 5d 29 3b  rod(&zRight[6]);
26c23 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
26c24 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 0a  }else.#endif.. .
26c25 20 20 7b 2f 2a 20 45 6d 70 74 79 20 45 4c 53 45    {/* Empty ELSE
26c26 20 63 6c 61 75 73 65 20 2a 2f 7d 0a 0a 20 20 2f   clause */}..  /
26c27 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70  * Code an OP_Exp
26c28 69 72 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ire at the end o
26c29 66 20 65 61 63 68 20 50 52 41 47 4d 41 20 70 72  f each PRAGMA pr
26c2a 6f 67 72 61 6d 20 74 6f 20 63 61 75 73 65 0a 20  ogram to cause. 
26c2b 20 2a 2a 20 74 68 65 20 56 44 42 45 20 69 6d 70   ** the VDBE imp
26c2c 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 70 72  lementing the pr
26c2d 61 67 6d 61 20 74 6f 20 65 78 70 69 72 65 2e 20  agma to expire. 
26c2e 4d 6f 73 74 20 28 61 6c 6c 3f 29 20 70 72 61 67  Most (all?) prag
26c2f 6d 61 73 0a 20 20 2a 2a 20 61 72 65 20 6f 6e 6c  mas.  ** are onl
26c30 79 20 76 61 6c 69 64 20 66 6f 72 20 61 20 73 69  y valid for a si
26c31 6e 67 6c 65 20 65 78 65 63 75 74 69 6f 6e 2e 0a  ngle execution..
26c32 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
26c33 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45  beAddOp2(v, OP_E
26c34 78 70 69 72 65 2c 20 31 2c 20 30 29 3b 0a 0a 20  xpire, 1, 0);.. 
26c35 20 2f 2a 0a 20 20 2a 2a 20 52 65 73 65 74 20 74   /*.  ** Reset t
26c36 68 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2c  he safety level,
26c37 20 69 6e 20 63 61 73 65 20 74 68 65 20 66 75 6c   in case the ful
26c38 6c 66 73 79 6e 63 20 66 6c 61 67 20 6f 72 20 73  lfsync flag or s
26c39 79 6e 63 68 72 6f 6e 6f 75 73 0a 20 20 2a 2a 20  ynchronous.  ** 
26c3a 73 65 74 74 69 6e 67 20 63 68 61 6e 67 65 64 2e  setting changed.
26c3b 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
26c3c 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
26c3d 50 52 41 47 4d 41 53 0a 20 20 69 66 28 20 64 62  PRAGMAS.  if( db
26c3e 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
26c3f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
26c40 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70  SetSafetyLevel(p
26c41 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 73 61  Db->pBt, pDb->sa
26c42 66 65 74 79 5f 6c 65 76 65 6c 2c 0a 20 20 20 20  fety_level,.    
26c43 20 20 20 20 20 20 20 20 20 20 20 28 64 62 2d 3e             (db->
26c44 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 75 6c  flags&SQLITE_Ful
26c45 6c 46 53 79 6e 63 29 21 3d 30 29 3b 0a 20 20 7d  lFSync)!=0);.  }
26c46 0a 23 65 6e 64 69 66 0a 70 72 61 67 6d 61 5f 6f  .#endif.pragma_o
26c47 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46  ut:.  sqlite3DbF
26c48 72 65 65 28 64 62 2c 20 7a 4c 65 66 74 29 3b 0a  ree(db, zLeft);.
26c49 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
26c4a 64 62 2c 20 7a 52 69 67 68 74 29 3b 0a 7d 0a 0a  db, zRight);.}..
26c4b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
26c4c 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 20 7c 7c 20  _OMIT_PRAGMA || 
26c4d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53  SQLITE_OMIT_PARS
26c4e 45 52 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ER */../********
26c4f 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72  ****** End of pr
26c50 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  agma.c *********
26c51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c53 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
26c54 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
26c55 65 20 70 72 65 70 61 72 65 2e 63 20 2a 2a 2a 2a  e prepare.c ****
26c56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c58 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
26c59 35 20 4d 61 79 20 32 35 0a 2a 2a 0a 2a 2a 20 54  5 May 25.**.** T
26c5a 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
26c5b 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
26c5c 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
26c5d 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
26c5e 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
26c5f 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
26c60 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
26c61 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
26c62 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
26c63 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
26c64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
26c65 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
26c66 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
26c67 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
26c68 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
26c69 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
26c6a 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
26c6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c6f 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
26c70 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
26c71 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
26c72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
26c73 70 72 65 70 61 72 65 28 29 0a 2a 2a 20 69 6e 74  prepare().** int
26c74 65 72 66 61 63 65 2c 20 61 6e 64 20 72 6f 75 74  erface, and rout
26c75 69 6e 65 73 20 74 68 61 74 20 63 6f 6e 74 72 69  ines that contri
26c76 62 75 74 65 20 74 6f 20 6c 6f 61 64 69 6e 67 20  bute to loading 
26c77 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
26c78 65 6d 61 0a 2a 2a 20 66 72 6f 6d 20 64 69 73 6b  ema.** from disk
26c79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 65  ..**.** $Id: pre
26c7a 70 61 72 65 2e 63 2c 76 20 31 2e 31 31 37 20 32  pare.c,v 1.117 2
26c7b 30 30 39 2f 30 34 2f 32 30 20 31 37 3a 34 33 3a  009/04/20 17:43:
26c7c 30 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  03 drh Exp $.*/.
26c7d 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20  ./*.** Fill the 
26c7e 49 6e 69 74 44 61 74 61 20 73 74 72 75 63 74 75  InitData structu
26c7f 72 65 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  re with an error
26c80 20 6d 65 73 73 61 67 65 20 74 68 61 74 20 69 6e   message that in
26c81 64 69 63 61 74 65 73 0a 2a 2a 20 74 68 61 74 20  dicates.** that 
26c82 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
26c83 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 73 74 61 74  corrupt..*/.stat
26c84 69 63 20 76 6f 69 64 20 63 6f 72 72 75 70 74 53  ic void corruptS
26c85 63 68 65 6d 61 28 0a 20 20 49 6e 69 74 44 61 74  chema(.  InitDat
26c86 61 20 2a 70 44 61 74 61 2c 20 20 20 20 20 2f 2a  a *pData,     /*
26c87 20 49 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   Initialization 
26c88 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e  context */.  con
26c89 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a 2c 20 20  st char *zObj,  
26c8a 20 20 2f 2a 20 4f 62 6a 65 63 74 20 62 65 69 6e    /* Object bein
26c8b 67 20 70 61 72 73 65 64 20 61 74 20 74 68 65 20  g parsed at the 
26c8c 70 6f 69 6e 74 20 6f 66 20 65 72 72 6f 72 20 2a  point of error *
26c8d 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
26c8e 7a 45 78 74 72 61 20 20 20 2f 2a 20 45 72 72 6f  zExtra   /* Erro
26c8f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  r information */
26c90 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
26c91 62 20 3d 20 70 44 61 74 61 2d 3e 64 62 3b 0a 20  b = pData->db;. 
26c92 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
26c93 46 61 69 6c 65 64 20 26 26 20 28 64 62 2d 3e 66  Failed && (db->f
26c94 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65  lags & SQLITE_Re
26c95 63 6f 76 65 72 79 4d 6f 64 65 29 3d 3d 30 20 29  coveryMode)==0 )
26c96 7b 0a 20 20 20 20 69 66 28 20 7a 4f 62 6a 3d 3d  {.    if( zObj==
26c97 30 20 29 20 7a 4f 62 6a 20 3d 20 22 3f 22 3b 0a  0 ) zObj = "?";.
26c98 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
26c99 72 69 6e 67 28 70 44 61 74 61 2d 3e 70 7a 45 72  ring(pData->pzEr
26c9a 72 4d 73 67 2c 20 70 44 61 74 61 2d 3e 64 62 2c  rMsg, pData->db,
26c9b 0a 20 20 20 20 20 20 20 22 6d 61 6c 66 6f 72 6d  .       "malform
26c9c 65 64 20 64 61 74 61 62 61 73 65 20 73 63 68 65  ed database sche
26c9d 6d 61 20 28 25 73 29 22 2c 20 7a 4f 62 6a 29 3b  ma (%s)", zObj);
26c9e 0a 20 20 20 20 69 66 28 20 7a 45 78 74 72 61 20  .    if( zExtra 
26c9f 26 26 20 7a 45 78 74 72 61 5b 30 5d 20 29 7b 0a  && zExtra[0] ){.
26ca0 20 20 20 20 20 20 2a 70 44 61 74 61 2d 3e 70 7a        *pData->pz
26ca1 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
26ca2 4d 41 70 70 65 6e 64 66 28 70 44 61 74 61 2d 3e  MAppendf(pData->
26ca3 64 62 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72  db, *pData->pzEr
26ca4 72 4d 73 67 2c 20 22 25 73 20 2d 20 25 73 22 2c  rMsg, "%s - %s",
26ca5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
26ca6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26ca7 20 20 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72     *pData->pzErr
26ca8 4d 73 67 2c 20 7a 45 78 74 72 61 29 3b 0a 20 20  Msg, zExtra);.  
26ca9 20 20 7d 0a 20 20 7d 0a 20 20 70 44 61 74 61 2d    }.  }.  pData-
26caa 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52  >rc = SQLITE_COR
26cab 52 55 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  RUPT;.}../*.** T
26cac 68 69 73 20 69 73 20 74 68 65 20 63 61 6c 6c 62  his is the callb
26cad 61 63 6b 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  ack routine for 
26cae 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 69 6e  the code that in
26caf 69 74 69 61 6c 69 7a 65 73 20 74 68 65 0a 2a 2a  itializes the.**
26cb0 20 64 61 74 61 62 61 73 65 2e 20 20 53 65 65 20   database.  See 
26cb1 73 71 6c 69 74 65 33 49 6e 69 74 28 29 20 62 65  sqlite3Init() be
26cb2 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  low for addition
26cb3 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
26cb4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
26cb5 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20 66  is also called f
26cb6 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61 72 73 65  rom the OP_Parse
26cb7 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 20 6f 66  Schema opcode of
26cb8 20 74 68 65 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a   the VDBE..**.**
26cb9 20 45 61 63 68 20 63 61 6c 6c 62 61 63 6b 20 63   Each callback c
26cba 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 6f 6c 6c  ontains the foll
26cbb 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  owing informatio
26cbc 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67  n:.**.**     arg
26cbd 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74  v[0] = name of t
26cbe 68 69 6e 67 20 62 65 69 6e 67 20 63 72 65 61 74  hing being creat
26cbf 65 64 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 31  ed.**     argv[1
26cc0 5d 20 3d 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  ] = root page nu
26cc1 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20 6f  mber for table o
26cc2 72 20 69 6e 64 65 78 2e 20 30 20 66 6f 72 20 74  r index. 0 for t
26cc3 72 69 67 67 65 72 20 6f 72 20 76 69 65 77 2e 0a  rigger or view..
26cc4 2a 2a 20 20 20 20 20 61 72 67 76 5b 32 5d 20 3d  **     argv[2] =
26cc5 20 53 51 4c 20 74 65 78 74 20 66 6f 72 20 74 68   SQL text for th
26cc6 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d 65  e CREATE stateme
26cc7 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  nt..**.*/.SQLITE
26cc8 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
26cc9 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b  ite3InitCallback
26cca 28 76 6f 69 64 20 2a 70 49 6e 69 74 2c 20 69 6e  (void *pInit, in
26ccb 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61  t argc, char **a
26ccc 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55  rgv, char **NotU
26ccd 73 65 64 29 7b 0a 20 20 49 6e 69 74 44 61 74 61  sed){.  InitData
26cce 20 2a 70 44 61 74 61 20 3d 20 28 49 6e 69 74 44   *pData = (InitD
26ccf 61 74 61 2a 29 70 49 6e 69 74 3b 0a 20 20 73 71  ata*)pInit;.  sq
26cd0 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 44 61 74  lite3 *db = pDat
26cd1 61 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62  a->db;.  int iDb
26cd2 20 3d 20 70 44 61 74 61 2d 3e 69 44 62 3b 0a 0a   = pData->iDb;..
26cd3 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
26cd4 33 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  3 );.  UNUSED_PA
26cd5 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
26cd6 2c 20 61 72 67 63 29 3b 0a 20 20 61 73 73 65 72  , argc);.  asser
26cd7 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
26cd8 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
26cd9 20 29 3b 0a 20 20 44 62 43 6c 65 61 72 50 72 6f   );.  DbClearPro
26cda 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44  perty(db, iDb, D
26cdb 42 5f 45 6d 70 74 79 29 3b 0a 20 20 69 66 28 20  B_Empty);.  if( 
26cdc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
26cdd 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53   ){.    corruptS
26cde 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67  chema(pData, arg
26cdf 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 20 20 72 65  v[0], 0);.    re
26ce0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
26ce1 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  M;.  }..  assert
26ce2 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c  ( iDb>=0 && iDb<
26ce3 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69 66 28  db->nDb );.  if(
26ce4 20 61 72 67 76 3d 3d 30 20 29 20 72 65 74 75 72   argv==0 ) retur
26ce5 6e 20 30 3b 20 20 20 2f 2a 20 4d 69 67 68 74 20  n 0;   /* Might 
26ce6 68 61 70 70 65 6e 20 69 66 20 45 4d 50 54 59 5f  happen if EMPTY_
26ce7 52 45 53 55 4c 54 5f 43 41 4c 4c 42 41 43 4b 53  RESULT_CALLBACKS
26ce8 20 61 72 65 20 6f 6e 20 2a 2f 0a 20 20 69 66 28   are on */.  if(
26ce9 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20   argv[1]==0 ){. 
26cea 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61     corruptSchema
26ceb 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c  (pData, argv[0],
26cec 20 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   0);.  }else if(
26ced 20 61 72 67 76 5b 32 5d 20 26 26 20 61 72 67 76   argv[2] && argv
26cee 5b 32 5d 5b 30 5d 20 29 7b 0a 20 20 20 20 2f 2a  [2][0] ){.    /*
26cef 20 43 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72   Call the parser
26cf0 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20 43 52   to process a CR
26cf1 45 41 54 45 20 54 41 42 4c 45 2c 20 49 4e 44 45  EATE TABLE, INDE
26cf2 58 20 6f 72 20 56 49 45 57 2e 0a 20 20 20 20 2a  X or VIEW..    *
26cf3 2a 20 42 75 74 20 62 65 63 61 75 73 65 20 64 62  * But because db
26cf4 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 73  ->init.busy is s
26cf5 65 74 20 74 6f 20 31 2c 20 6e 6f 20 56 44 42 45  et to 1, no VDBE
26cf6 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   code is generat
26cf7 65 64 0a 20 20 20 20 2a 2a 20 6f 72 20 65 78 65  ed.    ** or exe
26cf8 63 75 74 65 64 2e 20 20 41 6c 6c 20 74 68 65 20  cuted.  All the 
26cf9 70 61 72 73 65 72 20 64 6f 65 73 20 69 73 20 62  parser does is b
26cfa 75 69 6c 64 20 74 68 65 20 69 6e 74 65 72 6e 61  uild the interna
26cfb 6c 20 64 61 74 61 0a 20 20 20 20 2a 2a 20 73 74  l data.    ** st
26cfc 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64 65  ructures that de
26cfd 73 63 72 69 62 65 20 74 68 65 20 74 61 62 6c 65  scribe the table
26cfe 2c 20 69 6e 64 65 78 2c 20 6f 72 20 76 69 65 77  , index, or view
26cff 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61  ..    */.    cha
26d00 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 69 6e 74  r *zErr;.    int
26d01 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 74 28   rc;.    assert(
26d02 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29   db->init.busy )
26d03 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69  ;.    db->init.i
26d04 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 64 62  Db = iDb;.    db
26d05 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 20 3d  ->init.newTnum =
26d06 20 61 74 6f 69 28 61 72 67 76 5b 31 5d 29 3b 0a   atoi(argv[1]);.
26d07 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
26d08 5f 65 78 65 63 28 64 62 2c 20 61 72 67 76 5b 32  _exec(db, argv[2
26d09 5d 2c 20 30 2c 20 30 2c 20 26 7a 45 72 72 29 3b  ], 0, 0, &zErr);
26d0a 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69 44  .    db->init.iD
26d0b 62 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72  b = 0;.    asser
26d0c 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
26d0d 20 7c 7c 20 7a 45 72 72 3d 3d 30 20 29 3b 0a 20   || zErr==0 );. 
26d0e 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
26d0f 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 70 44  !=rc ){.      pD
26d10 61 74 61 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20  ata->rc = rc;.  
26d11 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
26d12 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
26d13 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
26d14 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
26d15 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
26d16 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 26  LITE_INTERRUPT &
26d17 26 20 28 72 63 26 30 78 66 66 29 21 3d 53 51 4c  & (rc&0xff)!=SQL
26d18 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20  ITE_LOCKED ){.  
26d19 20 20 20 20 20 20 63 6f 72 72 75 70 74 53 63 68        corruptSch
26d1a 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b  ema(pData, argv[
26d1b 30 5d 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  0], zErr);.     
26d1c 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
26d1d 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29  DbFree(db, zErr)
26d1e 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
26d1f 69 66 28 20 61 72 67 76 5b 30 5d 3d 3d 30 20 29  if( argv[0]==0 )
26d20 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53 63 68  {.    corruptSch
26d21 65 6d 61 28 70 44 61 74 61 2c 20 30 2c 20 30 29  ema(pData, 0, 0)
26d22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
26d23 2a 20 49 66 20 74 68 65 20 53 51 4c 20 63 6f 6c  * If the SQL col
26d24 75 6d 6e 20 69 73 20 62 6c 61 6e 6b 20 69 74 20  umn is blank it 
26d25 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 61 6e  means this is an
26d26 20 69 6e 64 65 78 20 74 68 61 74 0a 20 20 20 20   index that.    
26d27 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 20 74  ** was created t
26d28 6f 20 62 65 20 74 68 65 20 50 52 49 4d 41 52 59  o be the PRIMARY
26d29 20 4b 45 59 20 6f 72 20 74 6f 20 66 75 6c 66 69   KEY or to fulfi
26d2a 6c 6c 20 61 20 55 4e 49 51 55 45 0a 20 20 20 20  ll a UNIQUE.    
26d2b 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f  ** constraint fo
26d2c 72 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  r a CREATE TABLE
26d2d 2e 20 20 54 68 65 20 69 6e 64 65 78 20 73 68 6f  .  The index sho
26d2e 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79  uld have already
26d2f 0a 20 20 20 20 2a 2a 20 62 65 65 6e 20 63 72 65  .    ** been cre
26d30 61 74 65 64 20 77 68 65 6e 20 77 65 20 70 72 6f  ated when we pro
26d31 63 65 73 73 65 64 20 74 68 65 20 43 52 45 41 54  cessed the CREAT
26d32 45 20 54 41 42 4c 45 2e 20 20 41 6c 6c 20 77 65  E TABLE.  All we
26d33 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20   have.    ** to 
26d34 64 6f 20 68 65 72 65 20 69 73 20 72 65 63 6f 72  do here is recor
26d35 64 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  d the root page 
26d36 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20  number for that 
26d37 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20  index..    */.  
26d38 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
26d39 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20 73 71  .    pIndex = sq
26d3a 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
26d3b 62 2c 20 61 72 67 76 5b 30 5d 2c 20 64 62 2d 3e  b, argv[0], db->
26d3c 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
26d3d 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 3d  .    if( pIndex=
26d3e 3d 30 20 7c 7c 20 70 49 6e 64 65 78 2d 3e 74 6e  =0 || pIndex->tn
26d3f 75 6d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  um!=0 ){.      /
26d40 2a 20 54 68 69 73 20 63 61 6e 20 6f 63 63 75 72  * This can occur
26d41 20 69 66 20 74 68 65 72 65 20 65 78 69 73 74 73   if there exists
26d42 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 61 20 54   an index on a T
26d43 45 4d 50 20 74 61 62 6c 65 20 77 68 69 63 68 0a  EMP table which.
26d44 20 20 20 20 20 20 2a 2a 20 68 61 73 20 74 68 65        ** has the
26d45 20 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 61 6e   same name as an
26d46 6f 74 68 65 72 20 69 6e 64 65 78 20 6f 6e 20 61  other index on a
26d47 20 70 65 72 6d 61 6e 65 6e 74 20 69 6e 64 65 78   permanent index
26d48 2e 20 20 53 69 6e 63 65 0a 20 20 20 20 20 20 2a  .  Since.      *
26d49 2a 20 74 68 65 20 70 65 72 6d 61 6e 65 6e 74 20  * the permanent 
26d4a 74 61 62 6c 65 20 69 73 20 68 69 64 64 65 6e 20  table is hidden 
26d4b 62 79 20 74 68 65 20 54 45 4d 50 20 74 61 62 6c  by the TEMP tabl
26d4c 65 2c 20 77 65 20 63 61 6e 20 61 6c 73 6f 0a 20  e, we can also. 
26d4d 20 20 20 20 20 2a 2a 20 73 61 66 65 6c 79 20 69       ** safely i
26d4e 67 6e 6f 72 65 20 74 68 65 20 69 6e 64 65 78 20  gnore the index 
26d4f 6f 6e 20 74 68 65 20 70 65 72 6d 61 6e 65 6e 74  on the permanent
26d50 20 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f   table..      */
26d51 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20 4e 6f 74  .      /* Do Not
26d52 68 69 6e 67 20 2a 2f 3b 0a 20 20 20 20 7d 65 6c  hing */;.    }el
26d53 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78  se{.      pIndex
26d54 2d 3e 74 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72  ->tnum = atoi(ar
26d55 67 76 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  gv[1]);.    }.  
26d56 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
26d57 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
26d58 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61 62  o read the datab
26d59 61 73 65 20 73 63 68 65 6d 61 20 61 6e 64 20 69  ase schema and i
26d5a 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72 6e  nitialize intern
26d5b 61 6c 0a 2a 2a 20 64 61 74 61 20 73 74 72 75 63  al.** data struc
26d5c 74 75 72 65 73 20 66 6f 72 20 61 20 73 69 6e 67  tures for a sing
26d5d 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
26d5e 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  .  The index of 
26d5f 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
26d60 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79  file is given by
26d61 20 69 44 62 2e 20 20 69 44 62 3d 3d 30 20 69 73   iDb.  iDb==0 is
26d62 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61   used for the ma
26d63 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  in.** database. 
26d64 20 69 44 62 3d 3d 31 20 73 68 6f 75 6c 64 20 6e   iDb==1 should n
26d65 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 20 69  ever be used.  i
26d66 44 62 3e 3d 32 20 69 73 20 75 73 65 64 20 66 6f  Db>=2 is used fo
26d67 72 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64  r.** auxiliary d
26d68 61 74 61 62 61 73 65 73 2e 20 20 52 65 74 75 72  atabases.  Retur
26d69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c  n one of the SQL
26d6a 49 54 45 5f 20 65 72 72 6f 72 20 63 6f 64 65 73  ITE_ error codes
26d6b 20 74 6f 0a 2a 2a 20 69 6e 64 69 63 61 74 65 20   to.** indicate 
26d6c 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75  success or failu
26d6d 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  re..*/.static in
26d6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65  t sqlite3InitOne
26d6f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
26d70 74 20 69 44 62 2c 20 63 68 61 72 20 2a 2a 70 7a  t iDb, char **pz
26d71 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 72  ErrMsg){.  int r
26d72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74  c;.  int i;.  Bt
26d73 43 75 72 73 6f 72 20 2a 63 75 72 4d 61 69 6e 3b  Cursor *curMain;
26d74 0a 20 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 54  .  int size;.  T
26d75 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 44 62  able *pTab;.  Db
26d76 20 2a 70 44 62 3b 0a 20 20 63 68 61 72 20 63 6f   *pDb;.  char co
26d77 6e 73 74 20 2a 61 7a 41 72 67 5b 34 5d 3b 0a 20  nst *azArg[4];. 
26d78 20 69 6e 74 20 6d 65 74 61 5b 31 30 5d 3b 0a 20   int meta[10];. 
26d79 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61   InitData initDa
26d7a 74 61 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74  ta;.  char const
26d7b 20 2a 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 3b   *zMasterSchema;
26d7c 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
26d7d 4d 61 73 74 65 72 4e 61 6d 65 20 3d 20 53 43 48  MasterName = SCH
26d7e 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a  EMA_TABLE(iDb);.
26d7f 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d  .  /*.  ** The m
26d80 61 73 74 65 72 20 64 61 74 61 62 61 73 65 20 74  aster database t
26d81 61 62 6c 65 20 68 61 73 20 61 20 73 74 72 75 63  able has a struc
26d82 74 75 72 65 20 6c 69 6b 65 20 74 68 69 73 0a 20  ture like this. 
26d83 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
26d84 73 74 20 63 68 61 72 20 6d 61 73 74 65 72 5f 73  st char master_s
26d85 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20 20 20 20  chema[] = .     
26d86 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 71  "CREATE TABLE sq
26d87 6c 69 74 65 5f 6d 61 73 74 65 72 28 5c 6e 22 0a  lite_master(\n".
26d88 20 20 20 20 20 22 20 20 74 79 70 65 20 74 65 78       "  type tex
26d89 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 6e 61  t,\n".     "  na
26d8a 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20  me text,\n".    
26d8b 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74 65 78   "  tbl_name tex
26d8c 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 72 6f  t,\n".     "  ro
26d8d 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72 2c 5c  otpage integer,\
26d8e 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c 20 74  n".     "  sql t
26d8f 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29 22 0a  ext\n".     ")".
26d90 20 20 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    ;.#ifndef SQLI
26d91 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20  TE_OMIT_TEMPDB. 
26d92 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
26d93 61 72 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73  ar temp_master_s
26d94 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20 20 20 20  chema[] = .     
26d95 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42  "CREATE TEMP TAB
26d96 4c 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  LE sqlite_temp_m
26d97 61 73 74 65 72 28 5c 6e 22 0a 20 20 20 20 20 22  aster(\n".     "
26d98 20 20 74 79 70 65 20 74 65 78 74 2c 5c 6e 22 0a    type text,\n".
26d99 20 20 20 20 20 22 20 20 6e 61 6d 65 20 74 65 78       "  name tex
26d9a 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 62  t,\n".     "  tb
26d9b 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a  l_name text,\n".
26d9c 20 20 20 20 20 22 20 20 72 6f 6f 74 70 61 67 65       "  rootpage
26d9d 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20 20   integer,\n".   
26d9e 20 20 22 20 20 73 71 6c 20 74 65 78 74 5c 6e 22    "  sql text\n"
26d9f 0a 20 20 20 20 20 22 29 22 0a 20 20 3b 0a 23 65  .     ")".  ;.#e
26da0 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 74 65  lse.  #define te
26da1 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d 61  mp_master_schema
26da2 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73   0.#endif..  ass
26da3 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
26da4 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
26da5 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b  assert( db->aDb[
26da6 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a  iDb].pSchema );.
26da7 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
26da8 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
26da9 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
26daa 65 72 74 28 20 69 44 62 3d 3d 31 20 7c 7c 20 73  ert( iDb==1 || s
26dab 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
26dac 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 69 44  Mutex(db->aDb[iD
26dad 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20 20 2f 2a  b].pBt) );..  /*
26dae 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20 61   zMasterSchema a
26daf 6e 64 20 7a 49 6e 69 74 53 63 72 69 70 74 20 61  nd zInitScript a
26db0 72 65 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  re set to point 
26db1 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 73 63  at the master sc
26db2 68 65 6d 61 0a 20 20 2a 2a 20 61 6e 64 20 69 6e  hema.  ** and in
26db3 69 74 69 61 6c 69 73 61 74 69 6f 6e 20 73 63 72  itialisation scr
26db4 69 70 74 20 61 70 70 72 6f 70 72 69 61 74 65 20  ipt appropriate 
26db5 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
26db6 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69 6e 69 74   being.  ** init
26db7 69 61 6c 69 73 65 64 2e 20 7a 4d 61 73 74 65 72  ialised. zMaster
26db8 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  Name is the name
26db9 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 74   of the master t
26dba 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
26dbb 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
26dbc 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 7a   iDb==1 ){.    z
26dbd 4d 61 73 74 65 72 53 63 68 65 6d 61 20 3d 20 74  MasterSchema = t
26dbe 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d  emp_master_schem
26dbf 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  a;.  }else{.    
26dc0 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20 3d 20  zMasterSchema = 
26dc1 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 3b 0a 20  master_schema;. 
26dc2 20 7d 0a 20 20 7a 4d 61 73 74 65 72 4e 61 6d 65   }.  zMasterName
26dc3 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28   = SCHEMA_TABLE(
26dc4 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73  iDb);..  /* Cons
26dc5 74 72 75 63 74 20 74 68 65 20 73 63 68 65 6d 61  truct the schema
26dc6 20 74 61 62 6c 65 73 2e 20 20 2a 2f 0a 20 20 61   tables.  */.  a
26dc7 7a 41 72 67 5b 30 5d 20 3d 20 7a 4d 61 73 74 65  zArg[0] = zMaste
26dc8 72 4e 61 6d 65 3b 0a 20 20 61 7a 41 72 67 5b 31  rName;.  azArg[1
26dc9 5d 20 3d 20 22 31 22 3b 0a 20 20 61 7a 41 72 67  ] = "1";.  azArg
26dca 5b 32 5d 20 3d 20 7a 4d 61 73 74 65 72 53 63 68  [2] = zMasterSch
26dcb 65 6d 61 3b 0a 20 20 61 7a 41 72 67 5b 33 5d 20  ema;.  azArg[3] 
26dcc 3d 20 30 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e  = 0;.  initData.
26dcd 64 62 20 3d 20 64 62 3b 0a 20 20 69 6e 69 74 44  db = db;.  initD
26dce 61 74 61 2e 69 44 62 20 3d 20 69 44 62 3b 0a 20  ata.iDb = iDb;. 
26dcf 20 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53   initData.rc = S
26dd0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 69 74  QLITE_OK;.  init
26dd1 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d 20  Data.pzErrMsg = 
26dd2 70 7a 45 72 72 4d 73 67 3b 0a 20 20 28 76 6f 69  pzErrMsg;.  (voi
26dd3 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
26dd4 66 66 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  ff(db);.  sqlite
26dd5 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 26 69  3InitCallback(&i
26dd6 6e 69 74 44 61 74 61 2c 20 33 2c 20 28 63 68 61  nitData, 3, (cha
26dd7 72 20 2a 2a 29 61 7a 41 72 67 2c 20 30 29 3b 0a  r **)azArg, 0);.
26dd8 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
26dd9 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 69  afetyOn(db);.  i
26dda 66 28 20 69 6e 69 74 44 61 74 61 2e 72 63 20 29  f( initData.rc )
26ddb 7b 0a 20 20 20 20 72 63 20 3d 20 69 6e 69 74 44  {.    rc = initD
26ddc 61 74 61 2e 72 63 3b 0a 20 20 20 20 67 6f 74 6f  ata.rc;.    goto
26ddd 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a   error_out;.  }.
26dde 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
26ddf 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4d  FindTable(db, zM
26de0 61 73 74 65 72 4e 61 6d 65 2c 20 64 62 2d 3e 61  asterName, db->a
26de1 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a  Db[iDb].zName);.
26de2 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
26de3 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
26de4 20 7c 3d 20 54 46 5f 52 65 61 64 6f 6e 6c 79 3b   |= TF_Readonly;
26de5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74  .  }..  /* Creat
26de6 65 20 61 20 63 75 72 73 6f 72 20 74 6f 20 68 6f  e a cursor to ho
26de7 6c 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ld the database 
26de8 6f 70 65 6e 0a 20 20 2a 2f 0a 20 20 70 44 62 20  open.  */.  pDb 
26de9 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
26dea 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d  .  if( pDb->pBt=
26deb 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 4f  =0 ){.    if( !O
26dec 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44  MIT_TEMPDB && iD
26ded 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 44 62  b==1 ){.      Db
26dee 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20  SetProperty(db, 
26def 31 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  1, DB_SchemaLoad
26df0 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ed);.    }.    r
26df1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
26df2 0a 20 20 7d 0a 20 20 63 75 72 4d 61 69 6e 20 3d  .  }.  curMain =
26df3 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65   sqlite3MallocZe
26df4 72 6f 28 73 71 6c 69 74 65 33 42 74 72 65 65 43  ro(sqlite3BtreeC
26df5 75 72 73 6f 72 53 69 7a 65 28 29 29 3b 0a 20 20  ursorSize());.  
26df6 69 66 28 20 21 63 75 72 4d 61 69 6e 20 29 7b 0a  if( !curMain ){.
26df7 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
26df8 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20  NOMEM;.    goto 
26df9 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 20  error_out;.  }. 
26dfa 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
26dfb 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  er(pDb->pBt);.  
26dfc 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
26dfd 65 43 75 72 73 6f 72 28 70 44 62 2d 3e 70 42 74  eCursor(pDb->pBt
26dfe 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 30  , MASTER_ROOT, 0
26dff 2c 20 30 2c 20 63 75 72 4d 61 69 6e 29 3b 0a 20  , 0, curMain);. 
26e00 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
26e01 45 4d 50 54 59 20 29 20 72 63 20 3d 20 53 51 4c  EMPTY ) rc = SQL
26e02 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 72 63  ITE_OK;.  if( rc
26e03 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
26e04 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
26e05 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62  ing(pzErrMsg, db
26e06 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45  , "%s", sqlite3E
26e07 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 20 20  rrStr(rc));.    
26e08 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72  goto initone_err
26e09 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  or_out;.  }..  /
26e0a 2a 20 47 65 74 20 74 68 65 20 64 61 74 61 62 61  * Get the databa
26e0b 73 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74  se meta informat
26e0c 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d  ion..  **.  ** M
26e0d 65 74 61 20 76 61 6c 75 65 73 20 61 72 65 20 61  eta values are a
26e0e 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 20  s follows:.  ** 
26e0f 20 20 20 6d 65 74 61 5b 30 5d 20 20 20 53 63 68     meta[0]   Sch
26e10 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 43 68 61  ema cookie.  Cha
26e11 6e 67 65 73 20 77 69 74 68 20 65 61 63 68 20 73  nges with each s
26e12 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a 20 20  chema change..  
26e13 2a 2a 20 20 20 20 6d 65 74 61 5b 31 5d 20 20 20  **    meta[1]   
26e14 46 69 6c 65 20 66 6f 72 6d 61 74 20 6f 66 20 73  File format of s
26e15 63 68 65 6d 61 20 6c 61 79 65 72 2e 0a 20 20 2a  chema layer..  *
26e16 2a 20 20 20 20 6d 65 74 61 5b 32 5d 20 20 20 53  *    meta[2]   S
26e17 69 7a 65 20 6f 66 20 74 68 65 20 70 61 67 65 20  ize of the page 
26e18 63 61 63 68 65 2e 0a 20 20 2a 2a 20 20 20 20 6d  cache..  **    m
26e19 65 74 61 5b 33 5d 20 20 20 55 73 65 20 66 72 65  eta[3]   Use fre
26e1a 65 6c 69 73 74 20 69 66 20 30 2e 20 20 41 75 74  elist if 0.  Aut
26e1b 6f 76 61 63 75 75 6d 20 69 66 20 67 72 65 61 74  ovacuum if great
26e1c 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 0a 20 20  er than zero..  
26e1d 2a 2a 20 20 20 20 6d 65 74 61 5b 34 5d 20 20 20  **    meta[4]   
26e1e 44 62 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  Db text encoding
26e1f 2e 20 31 3a 55 54 46 2d 38 20 32 3a 55 54 46 2d  . 1:UTF-8 2:UTF-
26e20 31 36 4c 45 20 33 3a 55 54 46 2d 31 36 42 45 0a  16LE 3:UTF-16BE.
26e21 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 35 5d 20    **    meta[5] 
26e22 20 20 54 68 65 20 75 73 65 72 20 63 6f 6f 6b 69    The user cooki
26e23 65 2e 20 55 73 65 64 20 62 79 20 74 68 65 20 61  e. Used by the a
26e24 70 70 6c 69 63 61 74 69 6f 6e 2e 0a 20 20 2a 2a  pplication..  **
26e25 20 20 20 20 6d 65 74 61 5b 36 5d 20 20 20 49 6e      meta[6]   In
26e26 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d  cremental-vacuum
26e27 20 66 6c 61 67 2e 0a 20 20 2a 2a 20 20 20 20 6d   flag..  **    m
26e28 65 74 61 5b 37 5d 0a 20 20 2a 2a 20 20 20 20 6d  eta[7].  **    m
26e29 65 74 61 5b 38 5d 0a 20 20 2a 2a 20 20 20 20 6d  eta[8].  **    m
26e2a 65 74 61 5b 39 5d 0a 20 20 2a 2a 0a 20 20 2a 2a  eta[9].  **.  **
26e2b 20 4e 6f 74 65 3a 20 54 68 65 20 23 64 65 66 69   Note: The #defi
26e2c 6e 65 64 20 53 51 4c 49 54 45 5f 55 54 46 2a 20  ned SQLITE_UTF* 
26e2d 73 79 6d 62 6f 6c 73 20 69 6e 20 73 71 6c 69 74  symbols in sqlit
26e2e 65 49 6e 74 2e 68 20 63 6f 72 72 65 73 70 6f 6e  eInt.h correspon
26e2f 64 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 70 6f  d to.  ** the po
26e30 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 6f 66  ssible values of
26e31 20 6d 65 74 61 5b 34 5d 2e 0a 20 20 2a 2f 0a 20   meta[4]..  */. 
26e32 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61   for(i=0; i<Arra
26e33 79 53 69 7a 65 28 6d 65 74 61 29 3b 20 69 2b 2b  ySize(meta); i++
26e34 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
26e35 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28  te3BtreeGetMeta(
26e36 70 44 62 2d 3e 70 42 74 2c 20 69 2b 31 2c 20 28  pDb->pBt, i+1, (
26e37 75 33 32 20 2a 29 26 6d 65 74 61 5b 69 5d 29 3b  u32 *)&meta[i]);
26e38 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
26e39 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
26e3a 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
26e3b 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
26e3c 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
26e3d 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65      goto initone
26e3e 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20  _error_out;.    
26e3f 7d 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63  }.  }.  pDb->pSc
26e40 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
26e41 6b 69 65 20 3d 20 6d 65 74 61 5b 30 5d 3b 0a 0a  kie = meta[0];..
26e42 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20    /* If opening 
26e43 61 20 6e 6f 6e 2d 65 6d 70 74 79 20 64 61 74 61  a non-empty data
26e44 62 61 73 65 2c 20 63 68 65 63 6b 20 74 68 65 20  base, check the 
26e45 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20 46  text encoding. F
26e46 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 69 6e  or the.  ** main
26e47 20 64 61 74 61 62 61 73 65 2c 20 73 65 74 20 73   database, set s
26e48 71 6c 69 74 65 33 2e 65 6e 63 20 74 6f 20 74 68  qlite3.enc to th
26e49 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  e encoding of th
26e4a 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
26e4b 0a 20 20 2a 2a 20 46 6f 72 20 61 6e 20 61 74 74  .  ** For an att
26e4c 61 63 68 65 64 20 64 62 2c 20 69 74 20 69 73 20  ached db, it is 
26e4d 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20  an error if the 
26e4e 65 6e 63 6f 64 69 6e 67 20 69 73 20 6e 6f 74 20  encoding is not 
26e4f 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 73  the same.  ** as
26e50 20 73 71 6c 69 74 65 33 2e 65 6e 63 2e 0a 20 20   sqlite3.enc..  
26e51 2a 2f 0a 20 20 69 66 28 20 6d 65 74 61 5b 34 5d  */.  if( meta[4]
26e52 20 29 7b 20 20 2f 2a 20 74 65 78 74 20 65 6e 63   ){  /* text enc
26e53 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 69 66 28  oding */.    if(
26e54 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20   iDb==0 ){.     
26e55 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 74   /* If opening t
26e56 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
26e57 2c 20 73 65 74 20 45 4e 43 28 64 62 29 2e 20 2a  , set ENC(db). *
26e58 2f 0a 20 20 20 20 20 20 45 4e 43 28 64 62 29 20  /.      ENC(db) 
26e59 3d 20 28 75 38 29 6d 65 74 61 5b 34 5d 3b 0a 20  = (u8)meta[4];. 
26e5a 20 20 20 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f       db->pDfltCo
26e5b 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ll = sqlite3Find
26e5c 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49  CollSeq(db, SQLI
26e5d 54 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52 59  TE_UTF8, "BINARY
26e5e 22 2c 20 36 2c 20 30 29 3b 0a 20 20 20 20 7d 65  ", 6, 0);.    }e
26e5f 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  lse{.      /* If
26e60 20 6f 70 65 6e 69 6e 67 20 61 6e 20 61 74 74 61   opening an atta
26e61 63 68 65 64 20 64 61 74 61 62 61 73 65 2c 20 74  ched database, t
26e62 68 65 20 65 6e 63 6f 64 69 6e 67 20 6d 75 63 68  he encoding much
26e63 20 6d 61 74 63 68 20 45 4e 43 28 64 62 29 20 2a   match ENC(db) *
26e64 2f 0a 20 20 20 20 20 20 69 66 28 20 6d 65 74 61  /.      if( meta
26e65 5b 34 5d 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a  [4]!=ENC(db) ){.
26e66 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
26e67 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73  etString(pzErrMs
26e68 67 2c 20 64 62 2c 20 22 61 74 74 61 63 68 65 64  g, db, "attached
26e69 20 64 61 74 61 62 61 73 65 73 20 6d 75 73 74 20   databases must 
26e6a 75 73 65 20 74 68 65 20 73 61 6d 65 22 0a 20 20  use the same".  
26e6b 20 20 20 20 20 20 20 20 20 20 22 20 74 65 78 74            " text
26e6c 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61 69   encoding as mai
26e6d 6e 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20  n database");.  
26e6e 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
26e6f 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20  E_ERROR;.       
26e70 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72   goto initone_er
26e71 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d  ror_out;.      }
26e72 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
26e73 20 20 20 20 44 62 53 65 74 50 72 6f 70 65 72 74      DbSetPropert
26e74 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d  y(db, iDb, DB_Em
26e75 70 74 79 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d  pty);.  }.  pDb-
26e76 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 20 3d 20  >pSchema->enc = 
26e77 45 4e 43 28 64 62 29 3b 0a 0a 20 20 69 66 28 20  ENC(db);..  if( 
26e78 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
26e79 63 68 65 5f 73 69 7a 65 3d 3d 30 20 29 7b 0a 20  che_size==0 ){. 
26e7a 20 20 20 73 69 7a 65 20 3d 20 6d 65 74 61 5b 32     size = meta[2
26e7b 5d 3b 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3d  ];.    if( size=
26e7c 3d 30 20 29 7b 20 73 69 7a 65 20 3d 20 53 51 4c  =0 ){ size = SQL
26e7d 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48  ITE_DEFAULT_CACH
26e7e 45 5f 53 49 5a 45 3b 20 7d 0a 20 20 20 20 69 66  E_SIZE; }.    if
26e7f 28 20 73 69 7a 65 3c 30 20 29 20 73 69 7a 65 20  ( size<0 ) size 
26e80 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20 70 44 62  = -size;.    pDb
26e81 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65  ->pSchema->cache
26e82 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20  _size = size;.  
26e83 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
26e84 74 43 61 63 68 65 53 69 7a 65 28 70 44 62 2d 3e  tCacheSize(pDb->
26e85 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d  pBt, pDb->pSchem
26e86 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a  a->cache_size);.
26e87 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 66    }..  /*.  ** f
26e88 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 31 20 20 20  ile_format==1   
26e89 20 56 65 72 73 69 6f 6e 20 33 2e 30 2e 30 2e 0a   Version 3.0.0..
26e8a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74    ** file_format
26e8b 3d 3d 32 20 20 20 20 56 65 72 73 69 6f 6e 20 33  ==2    Version 3
26e8c 2e 31 2e 33 2e 20 20 2f 2f 20 41 4c 54 45 52 20  .1.3.  // ALTER 
26e8d 54 41 42 4c 45 20 41 44 44 20 43 4f 4c 55 4d 4e  TABLE ADD COLUMN
26e8e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61  .  ** file_forma
26e8f 74 3d 3d 33 20 20 20 20 56 65 72 73 69 6f 6e 20  t==3    Version 
26e90 33 2e 31 2e 34 2e 20 20 2f 2f 20 64 69 74 74 6f  3.1.4.  // ditto
26e91 20 62 75 74 20 77 69 74 68 20 6e 6f 6e 2d 4e 55   but with non-NU
26e92 4c 4c 20 64 65 66 61 75 6c 74 73 0a 20 20 2a 2a  LL defaults.  **
26e93 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 34 20   file_format==4 
26e94 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 33 2e 30     Version 3.3.0
26e95 2e 20 20 2f 2f 20 44 45 53 43 20 69 6e 64 69 63  .  // DESC indic
26e96 65 73 2e 20 20 42 6f 6f 6c 65 61 6e 20 63 6f 6e  es.  Boolean con
26e97 73 74 61 6e 74 73 0a 20 20 2a 2f 0a 20 20 70 44  stants.  */.  pD
26e98 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  b->pSchema->file
26e99 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29 6d 65  _format = (u8)me
26e9a 74 61 5b 31 5d 3b 0a 20 20 69 66 28 20 70 44 62  ta[1];.  if( pDb
26e9b 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
26e9c 66 6f 72 6d 61 74 3d 3d 30 20 29 7b 0a 20 20 20  format==0 ){.   
26e9d 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
26e9e 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 31 3b 0a  ile_format = 1;.
26e9f 20 20 7d 0a 20 20 69 66 28 20 70 44 62 2d 3e 70    }.  if( pDb->p
26ea0 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72  Schema->file_for
26ea1 6d 61 74 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46  mat>SQLITE_MAX_F
26ea2 49 4c 45 5f 46 4f 52 4d 41 54 20 29 7b 0a 20 20  ILE_FORMAT ){.  
26ea3 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
26ea4 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c  ng(pzErrMsg, db,
26ea5 20 22 75 6e 73 75 70 70 6f 72 74 65 64 20 66 69   "unsupported fi
26ea6 6c 65 20 66 6f 72 6d 61 74 22 29 3b 0a 20 20 20  le format");.   
26ea7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
26ea8 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 69  OR;.    goto ini
26ea9 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a  tone_error_out;.
26eaa 20 20 7d 0a 0a 20 20 2f 2a 20 54 69 63 6b 65 74    }..  /* Ticket
26eab 20 23 32 38 30 34 3a 20 20 57 68 65 6e 20 77 65   #2804:  When we
26eac 20 6f 70 65 6e 20 61 20 64 61 74 61 62 61 73 65   open a database
26ead 20 69 6e 20 74 68 65 20 6e 65 77 65 72 20 66 69   in the newer fi
26eae 6c 65 20 66 6f 72 6d 61 74 2c 0a 20 20 2a 2a 20  le format,.  ** 
26eaf 63 6c 65 61 72 20 74 68 65 20 6c 65 67 61 63 79  clear the legacy
26eb0 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 20 70 72 61  _file_format pra
26eb1 67 6d 61 20 66 6c 61 67 20 73 6f 20 74 68 61 74  gma flag so that
26eb2 20 61 20 56 41 43 55 55 4d 20 77 69 6c 6c 0a 20   a VACUUM will. 
26eb3 20 2a 2a 20 6e 6f 74 20 64 6f 77 6e 67 72 61 64   ** not downgrad
26eb4 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  e the database a
26eb5 6e 64 20 74 68 75 73 20 69 6e 76 61 6c 69 64 61  nd thus invalida
26eb6 74 65 20 61 6e 79 20 64 65 73 63 65 6e 64 69 6e  te any descendin
26eb7 67 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 74  g.  ** indices t
26eb8 68 61 74 20 74 68 65 20 75 73 65 72 20 6d 69 67  hat the user mig
26eb9 68 74 20 68 61 76 65 20 63 72 65 61 74 65 64 2e  ht have created.
26eba 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 44 62 3d  .  */.  if( iDb=
26ebb 3d 30 20 26 26 20 6d 65 74 61 5b 31 5d 3e 3d 34  =0 && meta[1]>=4
26ebc 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67   ){.    db->flag
26ebd 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c 65 67  s &= ~SQLITE_Leg
26ebe 61 63 79 46 69 6c 65 46 6d 74 3b 0a 20 20 7d 0a  acyFileFmt;.  }.
26ebf 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73  .  /* Read the s
26ec0 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f  chema informatio
26ec1 6e 20 6f 75 74 20 6f 66 20 74 68 65 20 73 63 68  n out of the sch
26ec2 65 6d 61 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a  ema tables.  */.
26ec3 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e    assert( db->in
26ec4 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 69 66 28  it.busy );.  if(
26ec5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54   rc==SQLITE_EMPT
26ec6 59 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 20  Y ){.    /* For 
26ec7 61 6e 20 65 6d 70 74 79 20 64 61 74 61 62 61 73  an empty databas
26ec8 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68  e, there is noth
26ec9 69 6e 67 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  ing to read */. 
26eca 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
26ecb 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
26ecc 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20  char *zSql;.    
26ecd 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50  zSql = sqlite3MP
26ece 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20  rintf(db, .     
26ecf 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c     "SELECT name,
26ed0 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46   rootpage, sql F
26ed1 52 4f 4d 20 27 25 71 27 2e 25 73 22 2c 0a 20 20  ROM '%q'.%s",.  
26ed2 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
26ed3 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65  b].zName, zMaste
26ed4 72 4e 61 6d 65 29 3b 0a 20 20 20 20 28 76 6f 69  rName);.    (voi
26ed5 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
26ed6 66 66 28 64 62 29 3b 0a 23 69 66 6e 64 65 66 20  ff(db);.#ifndef 
26ed7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
26ed8 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a  ORIZATION.    {.
26ed9 20 20 20 20 20 20 69 6e 74 20 28 2a 78 41 75 74        int (*xAut
26eda 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  h)(void*,int,con
26edb 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
26edc 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
26edd 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
26ede 20 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d       xAuth = db-
26edf 3e 78 41 75 74 68 3b 0a 20 20 20 20 20 20 64 62  >xAuth;.      db
26ee0 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a 23 65 6e  ->xAuth = 0;.#en
26ee1 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 73  dif.      rc = s
26ee2 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
26ee3 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69  zSql, sqlite3Ini
26ee4 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74  tCallback, &init
26ee5 44 61 74 61 2c 20 30 29 3b 0a 23 69 66 6e 64 65  Data, 0);.#ifnde
26ee6 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
26ee7 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
26ee8 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41    db->xAuth = xA
26ee9 75 74 68 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  uth;.    }.#endi
26eea 66 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  f.    if( rc==SQ
26eeb 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69  LITE_OK ) rc = i
26eec 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20  nitData.rc;.    
26eed 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
26eee 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73  etyOn(db);.    s
26eef 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
26ef0 20 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65 66 20   zSql);.#ifndef 
26ef1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
26ef2 59 5a 45 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  YZE.    if( rc==
26ef3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
26ef4 20 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73     sqlite3Analys
26ef5 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29 3b  isLoad(db, iDb);
26ef6 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
26ef7 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  }.  if( db->mall
26ef8 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
26ef9 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
26efa 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  M;.    sqlite3Re
26efb 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
26efc 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  a(db, 0);.  }.  
26efd 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
26efe 4b 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26  K || (db->flags&
26eff 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
26f00 6f 64 65 29 29 7b 0a 20 20 20 20 2f 2a 20 42 6c  ode)){.    /* Bl
26f01 61 63 6b 20 6d 61 67 69 63 3a 20 49 66 20 74 68  ack magic: If th
26f02 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72  e SQLITE_Recover
26f03 79 4d 6f 64 65 20 66 6c 61 67 20 69 73 20 73 65  yMode flag is se
26f04 74 2c 20 74 68 65 6e 20 63 6f 6e 73 69 64 65 72  t, then consider
26f05 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63 68 65  .    ** the sche
26f06 6d 61 20 6c 6f 61 64 65 64 2c 20 65 76 65 6e 20  ma loaded, even 
26f07 69 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 72  if errors occurr
26f08 65 64 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75  ed. In this situ
26f09 61 74 69 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a  ation the .    *
26f0a 2a 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65  * current sqlite
26f0b 33 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72  3_prepare() oper
26f0c 61 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 2c  ation will fail,
26f0d 20 62 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69   but the followi
26f0e 6e 67 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 77 69  ng one.    ** wi
26f0f 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f  ll attempt to co
26f10 6d 70 69 6c 65 20 74 68 65 20 73 75 70 70 6c 69  mpile the suppli
26f11 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 67 61  ed statement aga
26f12 69 6e 73 74 20 77 68 61 74 65 76 65 72 20 73 75  inst whatever su
26f13 62 73 65 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74  bset.    ** of t
26f14 68 65 20 73 63 68 65 6d 61 20 77 61 73 20 6c 6f  he schema was lo
26f15 61 64 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  aded before the 
26f16 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2e 20  error occurred. 
26f17 54 68 65 20 70 72 69 6d 61 72 79 0a 20 20 20 20  The primary.    
26f18 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68  ** purpose of th
26f19 69 73 20 69 73 20 74 6f 20 61 6c 6c 6f 77 20 61  is is to allow a
26f1a 63 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c  ccess to the sql
26f1b 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
26f1c 0a 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65  .    ** even whe
26f1d 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 68  n its contents h
26f1e 61 76 65 20 62 65 65 6e 20 63 6f 72 72 75 70 74  ave been corrupt
26f1f 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 44  ed..    */.    D
26f20 62 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c  bSetProperty(db,
26f21 20 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c   iDb, DB_SchemaL
26f22 6f 61 64 65 64 29 3b 0a 20 20 20 20 72 63 20 3d  oaded);.    rc =
26f23 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
26f24 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20  .  /* Jump here 
26f25 66 6f 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61  for an error tha
26f26 74 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 73  t occurs after s
26f27 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6c 6c 6f  uccessfully allo
26f28 63 61 74 69 6e 67 0a 20 20 2a 2a 20 63 75 72 4d  cating.  ** curM
26f29 61 69 6e 20 61 6e 64 20 63 61 6c 6c 69 6e 67 20  ain and calling 
26f2a 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
26f2b 72 28 29 2e 20 46 6f 72 20 61 6e 20 65 72 72 6f  r(). For an erro
26f2c 72 20 74 68 61 74 20 6f 63 63 75 72 73 0a 20 20  r that occurs.  
26f2d 2a 2a 20 62 65 66 6f 72 65 20 74 68 61 74 20 70  ** before that p
26f2e 6f 69 6e 74 2c 20 6a 75 6d 70 20 74 6f 20 65 72  oint, jump to er
26f2f 72 6f 72 5f 6f 75 74 2e 0a 20 20 2a 2f 0a 69 6e  ror_out..  */.in
26f30 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3a  itone_error_out:
26f31 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  .  sqlite3BtreeC
26f32 6c 6f 73 65 43 75 72 73 6f 72 28 63 75 72 4d 61  loseCursor(curMa
26f33 69 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  in);.  sqlite3_f
26f34 72 65 65 28 63 75 72 4d 61 69 6e 29 3b 0a 20 20  ree(curMain);.  
26f35 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
26f36 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 0a 65 72  e(pDb->pBt);..er
26f37 72 6f 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72  ror_out:.  if( r
26f38 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
26f39 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  || rc==SQLITE_IO
26f3a 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ERR_NOMEM ){.   
26f3b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
26f3c 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  d = 1;.  }.  ret
26f3d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
26f3e 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6c 6c 20   Initialize all 
26f3f 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 2d  database files -
26f40 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
26f41 73 65 20 66 69 6c 65 2c 20 74 68 65 20 66 69 6c  se file, the fil
26f42 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f  e.** used to sto
26f43 72 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  re temporary tab
26f44 6c 65 73 2c 20 61 6e 64 20 61 6e 79 20 61 64 64  les, and any add
26f45 69 74 69 6f 6e 61 6c 20 64 61 74 61 62 61 73 65  itional database
26f46 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65 61 74 65   files.** create
26f47 64 20 75 73 69 6e 67 20 41 54 54 41 43 48 20 73  d using ATTACH s
26f48 74 61 74 65 6d 65 6e 74 73 2e 20 20 52 65 74 75  tatements.  Retu
26f49 72 6e 20 61 20 73 75 63 63 65 73 73 20 63 6f 64  rn a success cod
26f4a 65 2e 20 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72  e.  If an.** err
26f4b 6f 72 20 6f 63 63 75 72 73 2c 20 77 72 69 74 65  or occurs, write
26f4c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
26f4d 65 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67  e into *pzErrMsg
26f4e 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20  ..**.** After a 
26f4f 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 69 74  database is init
26f50 69 61 6c 69 7a 65 64 2c 20 74 68 65 20 44 42 5f  ialized, the DB_
26f51 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 62 69 74  SchemaLoaded bit
26f52 20 69 73 20 73 65 74 0a 2a 2a 20 62 69 74 20 69   is set.** bit i
26f53 73 20 73 65 74 20 69 6e 20 74 68 65 20 66 6c 61  s set in the fla
26f54 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  gs field of the 
26f55 44 62 20 73 74 72 75 63 74 75 72 65 2e 20 49 66  Db structure. If
26f56 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
26f57 20 66 69 6c 65 20 77 61 73 20 6f 66 20 7a 65 72   file was of zer
26f58 6f 2d 6c 65 6e 67 74 68 2c 20 74 68 65 6e 20 74  o-length, then t
26f59 68 65 20 44 42 5f 45 6d 70 74 79 20 66 6c 61 67  he DB_Empty flag
26f5a 20 69 73 20 61 6c 73 6f 20 73 65 74 2e 0a 2a 2f   is also set..*/
26f5b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
26f5c 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28  int sqlite3Init(
26f5d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61  sqlite3 *db, cha
26f5e 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20  r **pzErrMsg){. 
26f5f 20 69 6e 74 20 69 2c 20 72 63 3b 0a 20 20 69 6e   int i, rc;.  in
26f60 74 20 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61  t commit_interna
26f61 6c 20 3d 20 21 28 64 62 2d 3e 66 6c 61 67 73 26  l = !(db->flags&
26f62 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
26f63 6e 67 65 73 29 3b 0a 20 20 0a 20 20 61 73 73 65  nges);.  .  asse
26f64 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
26f65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
26f66 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 69  ) );.  if( db->i
26f67 6e 69 74 2e 62 75 73 79 20 29 20 72 65 74 75 72  nit.busy ) retur
26f68 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72  n SQLITE_OK;.  r
26f69 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
26f6a 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d   db->init.busy =
26f6b 20 31 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72   1;.  for(i=0; r
26f6c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
26f6d 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
26f6e 0a 20 20 20 20 69 66 28 20 44 62 48 61 73 50 72  .    if( DbHasPr
26f6f 6f 70 65 72 74 79 28 64 62 2c 20 69 2c 20 44 42  operty(db, i, DB
26f70 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 7c  _SchemaLoaded) |
26f71 7c 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75  | i==1 ) continu
26f72 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e;.    rc = sqli
26f73 74 65 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 69  te3InitOne(db, i
26f74 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  , pzErrMsg);.   
26f75 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
26f76 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
26f77 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
26f78 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  i);.    }.  }.. 
26f79 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65   /* Once all the
26f7a 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 73   other databases
26f7b 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69   have been initi
26f7c 61 6c 69 73 65 64 2c 20 6c 6f 61 64 20 74 68 65  alised, load the
26f7d 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 66 6f 72   schema.  ** for
26f7e 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
26f7f 73 65 2e 20 54 68 69 73 20 69 73 20 6c 6f 61 64  se. This is load
26f80 65 64 20 6c 61 73 74 2c 20 61 73 20 74 68 65 20  ed last, as the 
26f81 54 45 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20  TEMP database.  
26f82 2a 2a 20 73 63 68 65 6d 61 20 6d 61 79 20 63 6f  ** schema may co
26f83 6e 74 61 69 6e 20 72 65 66 65 72 65 6e 63 65 73  ntain references
26f84 20 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 6f   to objects in o
26f85 74 68 65 72 20 64 61 74 61 62 61 73 65 73 2e 0a  ther databases..
26f86 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
26f87 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a  ITE_OMIT_TEMPDB.
26f88 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
26f89 5f 4f 4b 20 26 26 20 64 62 2d 3e 6e 44 62 3e 31  _OK && db->nDb>1
26f8a 20 26 26 20 21 44 62 48 61 73 50 72 6f 70 65 72   && !DbHasProper
26f8b 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53 63 68  ty(db, 1, DB_Sch
26f8c 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20  emaLoaded) ){.  
26f8d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e    rc = sqlite3In
26f8e 69 74 4f 6e 65 28 64 62 2c 20 31 2c 20 70 7a 45  itOne(db, 1, pzE
26f8f 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 28 20  rrMsg);.    if( 
26f90 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rc ){.      sqli
26f91 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
26f92 53 63 68 65 6d 61 28 64 62 2c 20 31 29 3b 0a 20  Schema(db, 1);. 
26f93 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
26f94 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  .  db->init.busy
26f95 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d   = 0;.  if( rc==
26f96 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63 6f 6d  SQLITE_OK && com
26f97 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20 29 7b 0a  mit_internal ){.
26f98 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69      sqlite3Commi
26f99 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73  tInternalChanges
26f9a 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  (db);.  }..  ret
26f9b 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f 2a 0a 2a  urn rc; .}../*.*
26f9c 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
26f9d 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65  s a no-op if the
26f9e 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
26f9f 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 69 74   is already init
26fa0 69 61 6c 69 73 65 64 2e 0a 2a 2a 20 4f 74 68 65  ialised..** Othe
26fa1 72 77 69 73 65 2c 20 74 68 65 20 73 63 68 65 6d  rwise, the schem
26fa2 61 20 69 73 20 6c 6f 61 64 65 64 2e 20 41 6e 20  a is loaded. An 
26fa3 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
26fa4 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
26fa5 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
26fa6 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
26fa7 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
26fa8 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
26fa9 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 20  E_OK;.  sqlite3 
26faa 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
26fab 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
26fac 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
26fad 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  b->mutex) );.  i
26fae 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
26faf 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  y ){.    rc = sq
26fb0 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20 26 70  lite3Init(db, &p
26fb1 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b  Parse->zErrMsg);
26fb2 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53  .  }.  if( rc!=S
26fb3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
26fb4 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b  pParse->rc = rc;
26fb5 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  .    pParse->nEr
26fb6 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  r++;.  }.  retur
26fb7 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
26fb8 43 68 65 63 6b 20 73 63 68 65 6d 61 20 63 6f 6f  Check schema coo
26fb9 6b 69 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 61  kies in all data
26fba 62 61 73 65 73 2e 20 20 49 66 20 61 6e 79 20 63  bases.  If any c
26fbb 6f 6f 6b 69 65 20 69 73 20 6f 75 74 0a 2a 2a 20  ookie is out.** 
26fbc 6f 66 20 64 61 74 65 2c 20 72 65 74 75 72 6e 20  of date, return 
26fbd 30 2e 20 20 49 66 20 61 6c 6c 20 73 63 68 65 6d  0.  If all schem
26fbe 61 20 63 6f 6f 6b 69 65 73 20 61 72 65 20 63 75  a cookies are cu
26fbf 72 72 65 6e 74 2c 20 72 65 74 75 72 6e 20 31 2e  rrent, return 1.
26fc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
26fc1 63 68 65 6d 61 49 73 56 61 6c 69 64 28 73 71 6c  chemaIsValid(sql
26fc2 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
26fc3 20 69 44 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a   iDb;.  int rc;.
26fc4 20 20 42 74 43 75 72 73 6f 72 20 2a 63 75 72 54    BtCursor *curT
26fc5 65 6d 70 3b 0a 20 20 69 6e 74 20 63 6f 6f 6b 69  emp;.  int cooki
26fc6 65 3b 0a 20 20 69 6e 74 20 61 6c 6c 4f 6b 20 3d  e;.  int allOk =
26fc7 20 31 3b 0a 0a 20 20 63 75 72 54 65 6d 70 20 3d   1;..  curTemp =
26fc8 20 28 42 74 43 75 72 73 6f 72 20 2a 29 73 71 6c   (BtCursor *)sql
26fc9 69 74 65 33 4d 61 6c 6c 6f 63 28 73 71 6c 69 74  ite3Malloc(sqlit
26fca 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a  e3BtreeCursorSiz
26fcb 65 28 29 29 3b 0a 20 20 69 66 28 20 63 75 72 54  e());.  if( curT
26fcc 65 6d 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72  emp ){.    asser
26fcd 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
26fce 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
26fcf 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 44 62 3d   );.    for(iDb=
26fd0 30 3b 20 61 6c 6c 4f 6b 20 26 26 20 69 44 62 3c  0; allOk && iDb<
26fd1 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b  db->nDb; iDb++){
26fd2 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
26fd3 74 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 64  t;.      pBt = d
26fd4 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b  b->aDb[iDb].pBt;
26fd5 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 3d 3d  .      if( pBt==
26fd6 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
26fd7 20 20 20 20 6d 65 6d 73 65 74 28 63 75 72 54 65      memset(curTe
26fd8 6d 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 42 74  mp, 0, sqlite3Bt
26fd9 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 29  reeCursorSize())
26fda 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
26fdb 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28  ite3BtreeCursor(
26fdc 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  pBt, MASTER_ROOT
26fdd 2c 20 30 2c 20 30 2c 20 63 75 72 54 65 6d 70 29  , 0, 0, curTemp)
26fde 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
26fdf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
26fe0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
26fe1 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 42  3BtreeGetMeta(pB
26fe2 74 2c 20 31 2c 20 28 75 33 32 20 2a 29 26 63 6f  t, 1, (u32 *)&co
26fe3 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 20 20 69  okie);.        i
26fe4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
26fe5 20 26 26 20 63 6f 6f 6b 69 65 21 3d 64 62 2d 3e   && cookie!=db->
26fe6 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
26fe7 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20  ->schema_cookie 
26fe8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 6c 6c  ){.          all
26fe9 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  Ok = 0;.        
26fea 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
26feb 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f  3BtreeCloseCurso
26fec 72 28 63 75 72 54 65 6d 70 29 3b 0a 20 20 20 20  r(curTemp);.    
26fed 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63    }.      if( rc
26fee 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c  ==SQLITE_NOMEM |
26fef 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  | rc==SQLITE_IOE
26ff0 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  RR_NOMEM ){.    
26ff1 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
26ff2 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
26ff3 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
26ff4 74 65 33 5f 66 72 65 65 28 63 75 72 54 65 6d 70  te3_free(curTemp
26ff5 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
26ff6 61 6c 6c 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 64  allOk = 0;.    d
26ff7 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
26ff8 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  = 1;.  }..  retu
26ff9 72 6e 20 61 6c 6c 4f 6b 3b 0a 7d 0a 0a 2f 2a 0a  rn allOk;.}../*.
26ffa 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 63 68  ** Convert a sch
26ffb 65 6d 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f  ema pointer into
26ffc 20 74 68 65 20 69 44 62 20 69 6e 64 65 78 20 74   the iDb index t
26ffd 68 61 74 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a  hat indicates.**
26ffe 20 77 68 69 63 68 20 64 61 74 61 62 61 73 65 20   which database 
26fff 66 69 6c 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b  file in db->aDb[
27000 5d 20 74 68 65 20 73 63 68 65 6d 61 20 72 65 66  ] the schema ref
27001 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66  ers to..**.** If
27002 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
27003 73 65 20 69 73 20 61 74 74 61 63 68 65 64 20 6d  se is attached m
27004 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74  ore than once, t
27005 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 74 74 61  he first.** atta
27006 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 73  ched database is
27007 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
27008 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
27009 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2700a 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64  Index(sqlite3 *d
2700b 62 2c 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65  b, Schema *pSche
2700c 6d 61 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 2d  ma){.  int i = -
2700d 31 30 30 30 30 30 30 3b 0a 0a 20 20 2f 2a 20 49  1000000;..  /* I
2700e 66 20 70 53 63 68 65 6d 61 20 69 73 20 4e 55 4c  f pSchema is NUL
2700f 4c 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 2d  L, then return -
27010 31 30 30 30 30 30 30 2e 20 54 68 69 73 20 68 61  1000000. This ha
27011 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 65 20  ppens when code 
27012 69 6e 20 0a 20 20 2a 2a 20 65 78 70 72 2e 63 20  in .  ** expr.c 
27013 69 73 20 74 72 79 69 6e 67 20 74 6f 20 72 65 73  is trying to res
27014 6f 6c 76 65 20 61 20 72 65 66 65 72 65 6e 63 65  olve a reference
27015 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20   to a transient 
27016 74 61 62 6c 65 20 28 69 2e 65 2e 20 6f 6e 65 0a  table (i.e. one.
27017 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20    ** created by 
27018 61 20 73 75 62 2d 73 65 6c 65 63 74 29 2e 20 49  a sub-select). I
27019 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
2701a 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
2701b 74 68 69 73 20 0a 20 20 2a 2a 20 66 75 6e 63 74  this .  ** funct
2701c 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  ion should never
2701d 20 62 65 20 75 73 65 64 2e 0a 20 20 2a 2a 0a 20   be used..  **. 
2701e 20 2a 2a 20 57 65 20 72 65 74 75 72 6e 20 2d 31   ** We return -1
2701f 30 30 30 30 30 30 20 69 6e 73 74 65 61 64 20 6f  000000 instead o
27020 66 20 74 68 65 20 6d 6f 72 65 20 75 73 75 61 6c  f the more usual
27021 20 2d 31 20 73 69 6d 70 6c 79 20 62 65 63 61 75   -1 simply becau
27022 73 65 20 75 73 69 6e 67 0a 20 20 2a 2a 20 2d 31  se using.  ** -1
27023 30 30 30 30 30 30 20 61 73 20 74 68 65 20 69 6e  000000 as the in
27024 63 6f 72 72 65 63 74 20 69 6e 64 65 78 20 69 6e  correct index in
27025 74 6f 20 64 62 2d 3e 61 44 62 5b 5d 20 69 73 20  to db->aDb[] is 
27026 6d 75 63 68 20 0a 20 20 2a 2a 20 6d 6f 72 65 20  much .  ** more 
27027 6c 69 6b 65 6c 79 20 74 6f 20 63 61 75 73 65 20  likely to cause 
27028 61 20 73 65 67 66 61 75 6c 74 20 74 68 61 6e 20  a segfault than 
27029 2d 31 20 28 6f 66 20 63 6f 75 72 73 65 20 74 68  -1 (of course th
2702a 65 72 65 20 61 72 65 20 61 73 73 65 72 74 28 29  ere are assert()
2702b 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73  .  ** statements
2702c 20 74 6f 6f 2c 20 62 75 74 20 69 74 20 6e 65 76   too, but it nev
2702d 65 72 20 68 75 72 74 73 20 74 6f 20 70 6c 61 79  er hurts to play
2702e 20 74 68 65 20 6f 64 64 73 29 2e 0a 20 20 2a 2f   the odds)..  */
2702f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
27030 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
27031 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
27032 28 20 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ( pSchema ){.   
27033 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57 41 59 53   for(i=0; ALWAYS
27034 28 69 3c 64 62 2d 3e 6e 44 62 29 3b 20 69 2b 2b  (i<db->nDb); i++
27035 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d  ){.      if( db-
27036 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65 6d 61 3d  >aDb[i].pSchema=
27037 3d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20  =pSchema ){.    
27038 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
27039 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73   }.    }.    ass
2703a 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 64  ert( i>=0 && i<d
2703b 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7d 0a 20 20  b->nDb );.  }.  
2703c 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a  return i;.}../*.
2703d 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65 20 55  ** Compile the U
2703e 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c  TF-8 encoded SQL
2703f 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71 6c 20   statement zSql 
27040 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65 6e 74  into a statement
27041 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74   handle..*/.stat
27042 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 50 72  ic int sqlite3Pr
27043 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33  epare(.  sqlite3
27044 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
27045 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
27046 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73  andle. */.  cons
27047 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
27048 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
27049 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
2704a 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
2704b 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
2704c 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
2704d 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
2704e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71   */.  int saveSq
2704f 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20  lFlag,          
27050 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70 79 20  /* True to copy 
27051 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68  SQL text into th
27052 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e sqlite3_stmt *
27053 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
27054 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
27055 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
27056 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
27057 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
27058 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
27059 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
2705a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
2705b 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 61  tring */.){.  Pa
2705c 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20 63 68  rse sParse;.  ch
2705d 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ar *zErrMsg = 0;
2705e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2705f 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b 0a  TE_OK;.  int i;.
27060 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
27061 66 65 74 79 4f 6e 28 64 62 29 20 29 20 72 65 74  fetyOn(db) ) ret
27062 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
27063 45 3b 0a 20 20 61 73 73 65 72 74 28 20 70 70 53  E;.  assert( ppS
27064 74 6d 74 20 26 26 20 2a 70 70 53 74 6d 74 3d 3d  tmt && *ppStmt==
27065 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  0 );.  assert( !
27066 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
27067 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
27068 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
27069 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  (db->mutex) );..
2706a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 76 65    /* Check to ve
2706b 72 69 66 79 20 74 68 61 74 20 69 74 20 69 73 20  rify that it is 
2706c 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 74 20  possible to get 
2706d 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 61  a read lock on a
2706e 6c 6c 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  ll.  ** database
2706f 20 73 63 68 65 6d 61 73 2e 20 20 54 68 65 20 69   schemas.  The i
27070 6e 61 62 69 6c 69 74 79 20 74 6f 20 67 65 74 20  nability to get 
27071 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 6e 64 69  a read lock indi
27072 63 61 74 65 73 20 74 68 61 74 0a 20 20 2a 2a 20  cates that.  ** 
27073 73 6f 6d 65 20 6f 74 68 65 72 20 64 61 74 61 62  some other datab
27074 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
27075 73 20 68 6f 6c 64 69 6e 67 20 61 20 77 72 69 74  s holding a writ
27076 65 2d 6c 6f 63 6b 2c 20 77 68 69 63 68 20 69 6e  e-lock, which in
27077 0a 20 20 2a 2a 20 74 75 72 6e 20 6d 65 61 6e 73  .  ** turn means
27078 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20   that the other 
27079 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 6d  connection has m
2707a 61 64 65 20 75 6e 63 6f 6d 6d 69 74 74 65 64 20  ade uncommitted 
2707b 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20  changes.  ** to 
2707c 74 68 65 20 73 63 68 65 6d 61 2e 0a 20 20 2a 2a  the schema..  **
2707d 0a 20 20 2a 2a 20 57 65 72 65 20 77 65 20 74 6f  .  ** Were we to
2707e 20 70 72 6f 63 65 65 64 20 61 6e 64 20 70 72 65   proceed and pre
2707f 70 61 72 65 20 74 68 65 20 73 74 61 74 65 6d 65  pare the stateme
27080 6e 74 20 61 67 61 69 6e 73 74 20 74 68 65 20 75  nt against the u
27081 6e 63 6f 6d 6d 69 74 74 65 64 0a 20 20 2a 2a 20  ncommitted.  ** 
27082 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61  schema changes a
27083 6e 64 20 69 66 20 74 68 6f 73 65 20 73 63 68 65  nd if those sche
27084 6d 61 20 63 68 61 6e 67 65 73 20 61 72 65 20 73  ma changes are s
27085 75 62 73 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c  ubsequently roll
27086 65 64 0a 20 20 2a 2a 20 62 61 63 6b 20 61 6e 64  ed.  ** back and
27087 20 64 69 66 66 65 72 65 6e 74 20 63 68 61 6e 67   different chang
27088 65 73 20 61 72 65 20 6d 61 64 65 20 69 6e 20 74  es are made in t
27089 68 65 69 72 20 70 6c 61 63 65 2c 20 74 68 65 6e  heir place, then
2708a 20 77 68 65 6e 20 74 68 69 73 0a 20 20 2a 2a 20   when this.  ** 
2708b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2708c 6e 74 20 67 6f 65 73 20 74 6f 20 72 75 6e 20 74  nt goes to run t
2708d 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
2708e 20 77 6f 75 6c 64 20 66 61 69 6c 20 74 6f 20 64   would fail to d
2708f 65 74 65 63 74 0a 20 20 2a 2a 20 74 68 65 20 73  etect.  ** the s
27090 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20 20 44  chema change.  D
27091 69 73 61 73 74 65 72 20 77 6f 75 6c 64 20 66 6f  isaster would fo
27092 6c 6c 6f 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  llow..  **.  ** 
27093 54 68 69 73 20 74 68 72 65 61 64 20 69 73 20 63  This thread is c
27094 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67  urrently holding
27095 20 6d 75 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20   mutexes on all 
27096 42 74 72 65 65 73 20 28 62 65 63 61 75 73 65 0a  Btrees (because.
27097 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69    ** of the sqli
27098 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
27099 28 29 20 69 6e 20 73 71 6c 69 74 65 33 4c 6f 63  () in sqlite3Loc
2709a 6b 41 6e 64 50 72 65 70 61 72 65 28 29 29 20 73  kAndPrepare()) s
2709b 6f 20 69 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74  o it.  ** is not
2709c 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 6e   possible for an
2709d 6f 74 68 65 72 20 74 68 72 65 61 64 20 74 6f 20  other thread to 
2709e 73 74 61 72 74 20 61 20 6e 65 77 20 73 63 68 65  start a new sche
2709f 6d 61 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 77  ma change.  ** w
270a0 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74 69 6e  hile this routin
270a1 65 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 48  e is running.  H
270a2 65 6e 63 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20  ence, we do not 
270a3 6e 65 65 64 20 74 6f 20 68 6f 6c 64 20 0a 20 20  need to hold .  
270a4 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20  ** locks on the 
270a5 73 63 68 65 6d 61 2c 20 77 65 20 6a 75 73 74 20  schema, we just 
270a6 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72  need to make sur
270a7 65 20 6e 6f 62 6f 64 79 20 65 6c 73 65 20 69 73  e nobody else is
270a8 20 0a 20 20 2a 2a 20 68 6f 6c 64 69 6e 67 20 74   .  ** holding t
270a9 68 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e  hem..  **.  ** N
270aa 6f 74 65 20 74 68 61 74 20 73 65 74 74 69 6e 67  ote that setting
270ab 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45   READ_UNCOMMITTE
270ac 44 20 6f 76 65 72 72 69 64 65 73 20 6d 6f 73 74  D overrides most
270ad 20 6c 6f 63 6b 20 64 65 74 65 63 74 69 6f 6e 2c   lock detection,
270ae 0a 20 20 2a 2a 20 62 75 74 20 69 74 20 64 6f 65  .  ** but it doe
270af 73 20 2a 6e 6f 74 2a 20 6f 76 65 72 72 69 64 65  s *not* override
270b0 20 73 63 68 65 6d 61 20 6c 6f 63 6b 20 64 65 74   schema lock det
270b1 65 63 74 69 6f 6e 2c 20 73 6f 20 74 68 69 73 20  ection, so this 
270b2 61 6c 6c 20 73 74 69 6c 6c 0a 20 20 2a 2a 20 77  all still.  ** w
270b3 6f 72 6b 73 20 65 76 65 6e 20 69 66 20 52 45 41  orks even if REA
270b4 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 69 73  D_UNCOMMITTED is
270b5 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72   set..  */.  for
270b6 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
270b7 20 69 2b 2b 29 20 7b 0a 20 20 20 20 42 74 72 65   i++) {.    Btre
270b8 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  e *pBt = db->aDb
270b9 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
270ba 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 61 73   pBt ){.      as
270bb 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
270bc 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74  eeHoldsMutex(pBt
270bd 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) );.      rc = 
270be 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65  sqlite3BtreeSche
270bf 6d 61 4c 6f 63 6b 65 64 28 70 42 74 29 3b 0a 20  maLocked(pBt);. 
270c0 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
270c1 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
270c2 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  r *zDb = db->aDb
270c3 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [i].zName;.     
270c4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
270c5 64 62 2c 20 72 63 2c 20 22 64 61 74 61 62 61 73  db, rc, "databas
270c6 65 20 73 63 68 65 6d 61 20 69 73 20 6c 6f 63 6b  e schema is lock
270c7 65 64 3a 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20  ed: %s", zDb);. 
270c8 20 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c         (void)sql
270c9 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
270ca 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
270cb 61 73 65 28 20 64 62 2d 3e 66 6c 61 67 73 20 26  ase( db->flags &
270cc 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f   SQLITE_ReadUnco
270cd 6d 6d 69 74 74 65 64 20 29 3b 0a 20 20 20 20 20  mmitted );.     
270ce 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
270cf 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
270d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
270d1 20 20 7d 0a 20 20 0a 20 20 6d 65 6d 73 65 74 28    }.  .  memset(
270d2 26 73 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65  &sParse, 0, size
270d3 6f 66 28 73 50 61 72 73 65 29 29 3b 0a 20 20 73  of(sParse));.  s
270d4 50 61 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 20  Parse.db = db;. 
270d5 20 69 66 28 20 6e 42 79 74 65 73 3e 3d 30 20 26   if( nBytes>=0 &
270d6 26 20 28 6e 42 79 74 65 73 3d 3d 30 20 7c 7c 20  & (nBytes==0 || 
270d7 7a 53 71 6c 5b 6e 42 79 74 65 73 2d 31 5d 21 3d  zSql[nBytes-1]!=
270d8 30 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  0) ){.    char *
270d9 7a 53 71 6c 43 6f 70 79 3b 0a 20 20 20 20 69 6e  zSqlCopy;.    in
270da 74 20 6d 78 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c  t mxLen = db->aL
270db 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
270dc 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 20  T_SQL_LENGTH];. 
270dd 20 20 20 69 66 28 20 6e 42 79 74 65 73 3e 6d 78     if( nBytes>mx
270de 4c 65 6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Len ){.      sql
270df 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
270e0 4c 49 54 45 5f 54 4f 4f 42 49 47 2c 20 22 73 74  LITE_TOOBIG, "st
270e1 61 74 65 6d 65 6e 74 20 74 6f 6f 20 6c 6f 6e 67  atement too long
270e2 22 29 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29  ");.      (void)
270e3 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
270e4 28 64 62 29 3b 0a 20 20 20 20 20 20 72 65 74 75  (db);.      retu
270e5 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  rn sqlite3ApiExi
270e6 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f  t(db, SQLITE_TOO
270e7 42 49 47 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BIG);.    }.    
270e8 7a 53 71 6c 43 6f 70 79 20 3d 20 73 71 6c 69 74  zSqlCopy = sqlit
270e9 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
270ea 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20  zSql, nBytes);. 
270eb 20 20 20 69 66 28 20 7a 53 71 6c 43 6f 70 79 20     if( zSqlCopy 
270ec 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
270ed 52 75 6e 50 61 72 73 65 72 28 26 73 50 61 72 73  RunParser(&sPars
270ee 65 2c 20 7a 53 71 6c 43 6f 70 79 2c 20 26 7a 45  e, zSqlCopy, &zE
270ef 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 73 71  rrMsg);.      sq
270f0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
270f1 7a 53 71 6c 43 6f 70 79 29 3b 0a 20 20 20 20 20  zSqlCopy);.     
270f2 20 73 50 61 72 73 65 2e 7a 54 61 69 6c 20 3d 20   sParse.zTail = 
270f3 26 7a 53 71 6c 5b 73 50 61 72 73 65 2e 7a 54 61  &zSql[sParse.zTa
270f4 69 6c 2d 7a 53 71 6c 43 6f 70 79 5d 3b 0a 20 20  il-zSqlCopy];.  
270f5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
270f6 50 61 72 73 65 2e 7a 54 61 69 6c 20 3d 20 26 7a  Parse.zTail = &z
270f7 53 71 6c 5b 6e 42 79 74 65 73 5d 3b 0a 20 20 20  Sql[nBytes];.   
270f8 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
270f9 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
270fa 28 26 73 50 61 72 73 65 2c 20 7a 53 71 6c 2c 20  (&sParse, zSql, 
270fb 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 0a  &zErrMsg);.  }..
270fc 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
270fd 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 50  Failed ){.    sP
270fe 61 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54 45  arse.rc = SQLITE
270ff 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66  _NOMEM;.  }.  if
27100 28 20 73 50 61 72 73 65 2e 72 63 3d 3d 53 51 4c  ( sParse.rc==SQL
27101 49 54 45 5f 44 4f 4e 45 20 29 20 73 50 61 72 73  ITE_DONE ) sPars
27102 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  e.rc = SQLITE_OK
27103 3b 0a 20 20 69 66 28 20 73 50 61 72 73 65 2e 63  ;.  if( sParse.c
27104 68 65 63 6b 53 63 68 65 6d 61 20 26 26 20 21 73  heckSchema && !s
27105 63 68 65 6d 61 49 73 56 61 6c 69 64 28 64 62 29  chemaIsValid(db)
27106 20 29 7b 0a 20 20 20 20 73 50 61 72 73 65 2e 72   ){.    sParse.r
27107 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  c = SQLITE_SCHEM
27108 41 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 50 61  A;.  }.  if( sPa
27109 72 73 65 2e 72 63 3d 3d 53 51 4c 49 54 45 5f 53  rse.rc==SQLITE_S
2710a 43 48 45 4d 41 20 29 7b 0a 20 20 20 20 73 71 6c  CHEMA ){.    sql
2710b 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
2710c 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
2710d 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
2710e 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2710f 20 20 73 50 61 72 73 65 2e 72 63 20 3d 20 53 51    sParse.rc = SQ
27110 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
27111 20 20 69 66 28 20 70 7a 54 61 69 6c 20 29 7b 0a    if( pzTail ){.
27112 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 73 50      *pzTail = sP
27113 61 72 73 65 2e 7a 54 61 69 6c 3b 0a 20 20 7d 0a  arse.zTail;.  }.
27114 20 20 72 63 20 3d 20 73 50 61 72 73 65 2e 72 63    rc = sParse.rc
27115 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
27116 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
27117 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
27118 4f 4b 20 26 26 20 73 50 61 72 73 65 2e 70 56 64  OK && sParse.pVd
27119 62 65 20 26 26 20 73 50 61 72 73 65 2e 65 78 70  be && sParse.exp
2711a 6c 61 69 6e 20 29 7b 0a 20 20 20 20 69 66 28 20  lain ){.    if( 
2711b 73 50 61 72 73 65 2e 65 78 70 6c 61 69 6e 3d 3d  sParse.explain==
2711c 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  2 ){.      sqlit
2711d 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
2711e 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 33  (sParse.pVdbe, 3
2711f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27120 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73  VdbeSetColName(s
27121 50 61 72 73 65 2e 70 56 64 62 65 2c 20 30 2c 20  Parse.pVdbe, 0, 
27122 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f  COLNAME_NAME, "o
27123 72 64 65 72 22 2c 20 53 51 4c 49 54 45 5f 53 54  rder", SQLITE_ST
27124 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
27125 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
27126 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  me(sParse.pVdbe,
27127 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
27128 2c 20 22 66 72 6f 6d 22 2c 20 53 51 4c 49 54 45  , "from", SQLITE
27129 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
2712a 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2712b 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64  lName(sParse.pVd
2712c 62 65 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  be, 2, COLNAME_N
2712d 41 4d 45 2c 20 22 64 65 74 61 69 6c 22 2c 20 53  AME, "detail", S
2712e 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2712f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27130 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
27131 6d 43 6f 6c 73 28 73 50 61 72 73 65 2e 70 56 64  mCols(sParse.pVd
27132 62 65 2c 20 38 29 3b 0a 20 20 20 20 20 20 73 71  be, 8);.      sq
27133 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
27134 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65  ame(sParse.pVdbe
27135 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
27136 45 2c 20 22 61 64 64 72 22 2c 20 53 51 4c 49 54  E, "addr", SQLIT
27137 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
27138 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
27139 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56  olName(sParse.pV
2713a 64 62 65 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  dbe, 1, COLNAME_
2713b 4e 41 4d 45 2c 20 22 6f 70 63 6f 64 65 22 2c 20  NAME, "opcode", 
2713c 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
2713d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2713e 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
2713f 73 65 2e 70 56 64 62 65 2c 20 32 2c 20 43 4f 4c  se.pVdbe, 2, COL
27140 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 31 22 2c  NAME_NAME, "p1",
27141 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
27142 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
27143 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61  beSetColName(sPa
27144 72 73 65 2e 70 56 64 62 65 2c 20 33 2c 20 43 4f  rse.pVdbe, 3, CO
27145 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 32 22  LNAME_NAME, "p2"
27146 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
27147 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27148 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50  dbeSetColName(sP
27149 61 72 73 65 2e 70 56 64 62 65 2c 20 34 2c 20 43  arse.pVdbe, 4, C
2714a 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 33  OLNAME_NAME, "p3
2714b 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
2714c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2714d 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73  VdbeSetColName(s
2714e 50 61 72 73 65 2e 70 56 64 62 65 2c 20 35 2c 20  Parse.pVdbe, 5, 
2714f 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70  COLNAME_NAME, "p
27150 34 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  4", SQLITE_STATI
27151 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
27152 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
27153 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 36 2c  sParse.pVdbe, 6,
27154 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
27155 70 35 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  p5", SQLITE_STAT
27156 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
27157 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
27158 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 37  (sParse.pVdbe, 7
27159 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2715a 22 63 6f 6d 6d 65 6e 74 22 2c 20 53 51 4c 49 54  "comment", SQLIT
2715b 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d  E_STATIC);.    }
2715c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
2715d 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
2715e 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72  Off(db) ){.    r
2715f 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
27160 45 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  E;.  }..  assert
27161 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d  ( db->init.busy=
27162 3d 30 20 7c 7c 20 73 61 76 65 53 71 6c 46 6c 61  =0 || saveSqlFla
27163 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 64 62  g==0 );.  if( db
27164 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29  ->init.busy==0 )
27165 7b 0a 20 20 20 20 56 64 62 65 20 2a 70 56 64 62  {.    Vdbe *pVdb
27166 65 20 3d 20 73 50 61 72 73 65 2e 70 56 64 62 65  e = sParse.pVdbe
27167 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27168 65 53 65 74 53 71 6c 28 70 56 64 62 65 2c 20 7a  eSetSql(pVdbe, z
27169 53 71 6c 2c 20 28 69 6e 74 29 28 73 50 61 72 73  Sql, (int)(sPars
2716a 65 2e 7a 54 61 69 6c 2d 7a 53 71 6c 29 2c 20 73  e.zTail-zSql), s
2716b 61 76 65 53 71 6c 46 6c 61 67 29 3b 0a 20 20 7d  aveSqlFlag);.  }
2716c 0a 20 20 69 66 28 20 73 50 61 72 73 65 2e 70 56  .  if( sParse.pV
2716d 64 62 65 20 26 26 20 28 72 63 21 3d 53 51 4c 49  dbe && (rc!=SQLI
2716e 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c  TE_OK || db->mal
2716f 6c 6f 63 46 61 69 6c 65 64 29 20 29 7b 0a 20 20  locFailed) ){.  
27170 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e    sqlite3VdbeFin
27171 61 6c 69 7a 65 28 73 50 61 72 73 65 2e 70 56 64  alize(sParse.pVd
27172 62 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  be);.    assert(
27173 21 28 2a 70 70 53 74 6d 74 29 29 3b 0a 20 20 7d  !(*ppStmt));.  }
27174 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 53 74 6d  else{.    *ppStm
27175 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d  t = (sqlite3_stm
27176 74 2a 29 73 50 61 72 73 65 2e 70 56 64 62 65 3b  t*)sParse.pVdbe;
27177 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 45 72 72  .  }..  if( zErr
27178 4d 73 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Msg ){.    sqlit
27179 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
2717a 22 25 73 22 2c 20 7a 45 72 72 4d 73 67 29 3b 0a  "%s", zErrMsg);.
2717b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2717c 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a  e(db, zErrMsg);.
2717d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
2717e 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
2717f 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  , 0);.  }..  rc 
27180 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
27181 28 64 62 2c 20 72 63 29 3b 0a 20 20 61 73 73 65  (db, rc);.  asse
27182 72 74 28 20 28 72 63 26 64 62 2d 3e 65 72 72 4d  rt( (rc&db->errM
27183 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 72 65  ask)==rc );.  re
27184 74 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69  turn rc;.}.stati
27185 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4c 6f 63  c int sqlite3Loc
27186 6b 41 6e 64 50 72 65 70 61 72 65 28 0a 20 20 73  kAndPrepare(.  s
27187 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
27188 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
27189 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a  base handle. */.
2718a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
2718b 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55  ql,         /* U
2718c 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c  TF-8 encoded SQL
2718d 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
2718e 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
2718f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
27190 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
27191 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20  bytes. */.  int 
27192 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20  saveSqlFlag,    
27193 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
27194 20 63 6f 70 79 20 53 51 4c 20 74 65 78 74 20 69   copy SQL text i
27195 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f  nto the sqlite3_
27196 73 74 6d 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  stmt */.  sqlite
27197 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c  3_stmt **ppStmt,
27198 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f      /* OUT: A po
27199 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72 65  inter to the pre
2719a 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
2719b 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2719c 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f  **pzTail       /
2719d 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61  * OUT: End of pa
2719e 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29  rsed string */.)
2719f 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
271a0 73 65 72 74 28 20 70 70 53 74 6d 74 21 3d 30 20  sert( ppStmt!=0 
271a1 29 3b 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 30  );.  *ppStmt = 0
271a2 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
271a3 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62  SafetyCheckOk(db
271a4 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
271a5 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
271a6 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
271a7 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
271a8 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ex);.  sqlite3Bt
271a9 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
271aa 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
271ab 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c  repare(db, zSql,
271ac 20 6e 42 79 74 65 73 2c 20 73 61 76 65 53 71 6c   nBytes, saveSql
271ad 46 6c 61 67 2c 20 70 70 53 74 6d 74 2c 20 70 7a  Flag, ppStmt, pz
271ae 54 61 69 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33  Tail);.  sqlite3
271af 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
271b0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
271b1 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
271b2 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
271b3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 72 75 6e  ;.}../*.** Rerun
271b4 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e   the compilation
271b5 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   of a statement 
271b6 61 66 74 65 72 20 61 20 73 63 68 65 6d 61 20 63  after a schema c
271b7 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  hange..**.** If 
271b8 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
271b9 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65   successfully re
271ba 63 6f 6d 70 69 6c 65 64 2c 20 72 65 74 75 72 6e  compiled, return
271bb 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65   SQLITE_OK. Othe
271bc 72 77 69 73 65 2c 0a 2a 2a 20 69 66 20 74 68 65  rwise,.** if the
271bd 20 73 74 61 74 65 6d 65 6e 74 20 63 61 6e 6e 6f   statement canno
271be 74 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20  t be recompiled 
271bf 62 65 63 61 75 73 65 20 61 6e 6f 74 68 65 72 20  because another 
271c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 0a 2a  connection has.*
271c1 2a 20 6c 6f 63 6b 65 64 20 74 68 65 20 73 71 6c  * locked the sql
271c2 69 74 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c  ite3_master tabl
271c3 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  e, return SQLITE
271c4 5f 4c 4f 43 4b 45 44 2e 20 49 66 20 61 6e 79 20  _LOCKED. If any 
271c5 6f 74 68 65 72 20 65 72 72 6f 72 0a 2a 2a 20 6f  other error.** o
271c6 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 53 51  ccurs, return SQ
271c7 4c 49 54 45 5f 53 43 48 45 4d 41 2e 0a 2a 2f 0a  LITE_SCHEMA..*/.
271c8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
271c9 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70  nt sqlite3Reprep
271ca 61 72 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  are(Vdbe *p){.  
271cb 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
271cc 33 5f 73 74 6d 74 20 2a 70 4e 65 77 3b 0a 20 20  3_stmt *pNew;.  
271cd 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
271ce 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
271cf 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
271d0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73  te3_mutex_held(s
271d1 71 6c 69 74 65 33 56 64 62 65 44 62 28 70 29 2d  qlite3VdbeDb(p)-
271d2 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 7a 53 71  >mutex) );.  zSq
271d3 6c 20 3d 20 73 71 6c 69 74 65 33 5f 73 71 6c 28  l = sqlite3_sql(
271d4 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
271d5 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53  p);.  assert( zS
271d6 71 6c 21 3d 30 20 29 3b 20 20 2f 2a 20 52 65 70  ql!=0 );  /* Rep
271d7 72 65 70 61 72 65 20 6f 6e 6c 79 20 63 61 6c 6c  repare only call
271d8 65 64 20 66 6f 72 20 70 72 65 70 61 72 65 5f 76  ed for prepare_v
271d9 32 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a  2() statements *
271da 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33  /.  db = sqlite3
271db 56 64 62 65 44 62 28 70 29 3b 0a 20 20 61 73 73  VdbeDb(p);.  ass
271dc 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
271dd 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
271de 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  x) );.  rc = sql
271df 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61  ite3LockAndPrepa
271e0 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  re(db, zSql, -1,
271e1 20 30 2c 20 26 70 4e 65 77 2c 20 30 29 3b 0a 20   0, &pNew, 0);. 
271e2 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69   if( rc ){.    i
271e3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
271e4 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  MEM ){.      db-
271e5 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
271e6 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  1;.    }.    ass
271e7 65 72 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b 0a  ert( pNew==0 );.
271e8 20 20 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d      return (rc==
271e9 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 20 3f  SQLITE_LOCKED) ?
271ea 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 3a   SQLITE_LOCKED :
271eb 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a   SQLITE_SCHEMA;.
271ec 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
271ed 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a  ert( pNew!=0 );.
271ee 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
271ef 65 53 77 61 70 28 28 56 64 62 65 2a 29 70 4e 65  eSwap((Vdbe*)pNe
271f0 77 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  w, p);.  sqlite3
271f1 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73  TransferBindings
271f2 28 70 4e 65 77 2c 20 28 73 71 6c 69 74 65 33 5f  (pNew, (sqlite3_
271f3 73 74 6d 74 2a 29 70 29 3b 0a 20 20 73 71 6c 69  stmt*)p);.  sqli
271f4 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70  te3VdbeResetStep
271f5 52 65 73 75 6c 74 28 28 56 64 62 65 2a 29 70 4e  Result((Vdbe*)pN
271f6 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ew);.  sqlite3Vd
271f7 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62 65  beFinalize((Vdbe
271f8 2a 29 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72  *)pNew);.  retur
271f9 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
271fa 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73 69  ./*.** Two versi
271fb 6f 6e 73 20 6f 66 20 74 68 65 20 6f 66 66 69 63  ons of the offic
271fc 69 61 6c 20 41 50 49 2e 20 20 4c 65 67 61 63 79  ial API.  Legacy
271fd 20 61 6e 64 20 6e 65 77 20 75 73 65 2e 20 20 49   and new use.  I
271fe 6e 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20  n the legacy.** 
271ff 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69  version, the ori
27200 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69  ginal SQL text i
27201 73 20 6e 6f 74 20 73 61 76 65 64 20 69 6e 20 74  s not saved in t
27202 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
27203 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f 20  ement.** and so 
27204 69 66 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e  if a schema chan
27205 67 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54  ge occurs, SQLIT
27206 45 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74 75  E_SCHEMA is retu
27207 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74  rned by.** sqlit
27208 65 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20 74  e3_step().  In t
27209 68 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20  he new version, 
2720a 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
2720b 20 74 65 78 74 20 69 73 20 72 65 74 61 69 6e 65   text is retaine
2720c 64 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61  d.** and the sta
2720d 74 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61  tement is automa
2720e 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c  tically recompil
2720f 65 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61 20  ed if an schema 
27210 63 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72 73  change.** occurs
27211 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
27212 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  int sqlite3_prep
27213 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  are(.  sqlite3 *
27214 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
27215 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
27216 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  dle. */.  const 
27217 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
27218 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63      /* UTF-8 enc
27219 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  oded SQL stateme
2721a 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  nt. */.  int nBy
2721b 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
2721c 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
2721d 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
2721e 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
2721f 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
27220 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
27221 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
27222 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
27223 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
27224 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
27225 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
27226 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
27227 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  t rc;.  rc = sql
27228 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61  ite3LockAndPrepa
27229 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65  re(db,zSql,nByte
2722a 73 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69  s,0,ppStmt,pzTai
2722b 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  l);.  assert( rc
2722c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
2722d 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53  pStmt==0 || *ppS
2722e 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45  tmt==0 );  /* VE
2722f 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a  RIFY: F13021 */.
27230 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
27231 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
27232 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
27233 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
27234 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27235 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
27236 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  . */.  const cha
27237 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
27238 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
27239 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
2723a 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
2723b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2723c 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
2723d 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
2723e 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
2723f 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55  ppStmt,    /* OU
27240 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  T: A pointer to 
27241 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
27242 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73  tement */.  cons
27243 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20  t char **pzTail 
27244 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e        /* OUT: En
27245 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69  d of parsed stri
27246 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ng */.){.  int r
27247 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  c;.  rc = sqlite
27248 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28  3LockAndPrepare(
27249 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 31  db,zSql,nBytes,1
2724a 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b  ,ppStmt,pzTail);
2724b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
2724c 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74  QLITE_OK || ppSt
2724d 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74  mt==0 || *ppStmt
2724e 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46  ==0 );  /* VERIF
2724f 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72  Y: F13021 */.  r
27250 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69  eturn rc;.}...#i
27251 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
27252 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f  T_UTF16./*.** Co
27253 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 31 36  mpile the UTF-16
27254 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
27255 74 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f  tement zSql into
27256 20 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e   a statement han
27257 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  dle..*/.static i
27258 6e 74 20 73 71 6c 69 74 65 33 50 72 65 70 61 72  nt sqlite3Prepar
27259 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  e16(.  sqlite3 *
2725a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2725b 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
2725c 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74  dle. */ .  const
2725d 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20   void *zSql,    
2725e 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e       /* UTF-8 en
2725f 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
27260 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ent. */.  int nB
27261 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  ytes,           
27262 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
27263 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
27264 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71 6c  */.  int saveSql
27265 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 2f  Flag,          /
27266 2a 20 54 72 75 65 20 74 6f 20 73 61 76 65 20 53  * True to save S
27267 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68 65  QL text into the
27268 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2f   sqlite3_stmt */
27269 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2726a 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20  **ppStmt,    /* 
2726b 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  OUT: A pointer t
2726c 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  o the prepared s
2726d 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f  tatement */.  co
2726e 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
2726f 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  l       /* OUT: 
27270 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74  End of parsed st
27271 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20  ring */.){.  /* 
27272 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 75  This function cu
27273 72 72 65 6e 74 6c 79 20 77 6f 72 6b 73 20 62 79  rrently works by
27274 20 66 69 72 73 74 20 74 72 61 6e 73 66 6f 72 6d   first transform
27275 69 6e 67 20 74 68 65 20 55 54 46 2d 31 36 0a 20  ing the UTF-16. 
27276 20 2a 2a 20 65 6e 63 6f 64 65 64 20 73 74 72 69   ** encoded stri
27277 6e 67 20 74 6f 20 55 54 46 2d 38 2c 20 74 68 65  ng to UTF-8, the
27278 6e 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74  n invoking sqlit
27279 65 33 5f 70 72 65 70 61 72 65 28 29 2e 20 54 68  e3_prepare(). Th
2727a 65 0a 20 20 2a 2a 20 74 72 69 63 6b 79 20 62 69  e.  ** tricky bi
2727b 74 20 69 73 20 66 69 67 75 72 69 6e 67 20 6f 75  t is figuring ou
2727c 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  t the pointer to
2727d 20 72 65 74 75 72 6e 20 69 6e 20 2a 70 7a 54 61   return in *pzTa
2727e 69 6c 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20  il..  */.  char 
2727f 2a 7a 53 71 6c 38 3b 0a 20 20 63 6f 6e 73 74 20  *zSql8;.  const 
27280 63 68 61 72 20 2a 7a 54 61 69 6c 38 20 3d 20 30  char *zTail8 = 0
27281 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
27282 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72  ITE_OK;..  asser
27283 74 28 20 70 70 53 74 6d 74 20 29 3b 0a 20 20 2a  t( ppStmt );.  *
27284 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66  ppStmt = 0;.  if
27285 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
27286 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20  CheckOk(db) ){. 
27287 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
27288 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73  _MISUSE;.  }.  s
27289 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2728a 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
2728b 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65 33   zSql8 = sqlite3
2728c 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 53 71  Utf16to8(db, zSq
2728d 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 66  l, nBytes);.  if
2728e 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20 20 20 72  ( zSql8 ){.    r
2728f 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41  c = sqlite3LockA
27290 6e 64 50 72 65 70 61 72 65 28 64 62 2c 20 7a 53  ndPrepare(db, zS
27291 71 6c 38 2c 20 2d 31 2c 20 73 61 76 65 53 71 6c  ql8, -1, saveSql
27292 46 6c 61 67 2c 20 70 70 53 74 6d 74 2c 20 26 7a  Flag, ppStmt, &z
27293 54 61 69 6c 38 29 3b 0a 20 20 7d 0a 0a 20 20 69  Tail8);.  }..  i
27294 66 28 20 7a 54 61 69 6c 38 20 26 26 20 70 7a 54  f( zTail8 && pzT
27295 61 69 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  ail ){.    /* If
27296 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
27297 20 72 65 74 75 72 6e 73 20 61 20 74 61 69 6c 20   returns a tail 
27298 70 6f 69 6e 74 65 72 2c 20 77 65 20 63 61 6c 63  pointer, we calc
27299 75 6c 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a  ulate the.    **
2729a 20 65 71 75 69 76 61 6c 65 6e 74 20 70 6f 69 6e   equivalent poin
2729b 74 65 72 20 69 6e 74 6f 20 74 68 65 20 55 54 46  ter into the UTF
2729c 2d 31 36 20 73 74 72 69 6e 67 20 62 79 20 63 6f  -16 string by co
2729d 75 6e 74 69 6e 67 20 74 68 65 20 75 6e 69 63 6f  unting the unico
2729e 64 65 0a 20 20 20 20 2a 2a 20 63 68 61 72 61 63  de.    ** charac
2729f 74 65 72 73 20 62 65 74 77 65 65 6e 20 7a 53 71  ters between zSq
272a0 6c 38 20 61 6e 64 20 7a 54 61 69 6c 38 2c 20 61  l8 and zTail8, a
272a1 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e 69 6e  nd then returnin
272a2 67 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 20 20  g a pointer.    
272a3 2a 2a 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62  ** the same numb
272a4 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
272a5 20 69 6e 74 6f 20 74 68 65 20 55 54 46 2d 31 36   into the UTF-16
272a6 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a   string..    */.
272a7 20 20 20 20 69 6e 74 20 63 68 61 72 73 5f 70 61      int chars_pa
272a8 72 73 65 64 20 3d 20 73 71 6c 69 74 65 33 55 74  rsed = sqlite3Ut
272a9 66 38 43 68 61 72 4c 65 6e 28 7a 53 71 6c 38 2c  f8CharLen(zSql8,
272aa 20 28 69 6e 74 29 28 7a 54 61 69 6c 38 2d 7a 53   (int)(zTail8-zS
272ab 71 6c 38 29 29 3b 0a 20 20 20 20 2a 70 7a 54 61  ql8));.    *pzTa
272ac 69 6c 20 3d 20 28 75 38 20 2a 29 7a 53 71 6c 20  il = (u8 *)zSql 
272ad 2b 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79  + sqlite3Utf16By
272ae 74 65 4c 65 6e 28 7a 53 71 6c 2c 20 63 68 61 72  teLen(zSql, char
272af 73 5f 70 61 72 73 65 64 29 3b 0a 20 20 7d 0a 20  s_parsed);.  }. 
272b0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
272b1 62 2c 20 7a 53 71 6c 38 29 3b 20 0a 20 20 72 63  b, zSql8); .  rc
272b2 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
272b3 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
272b4 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
272b5 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
272b6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
272b7 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e 73 20  ** Two versions 
272b8 6f 66 20 74 68 65 20 6f 66 66 69 63 69 61 6c 20  of the official 
272b9 41 50 49 2e 20 20 4c 65 67 61 63 79 20 61 6e 64  API.  Legacy and
272ba 20 6e 65 77 20 75 73 65 2e 20 20 49 6e 20 74 68   new use.  In th
272bb 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73  e legacy.** vers
272bc 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61  ion, the origina
272bd 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 6e 6f  l SQL text is no
272be 74 20 73 61 76 65 64 20 69 6e 20 74 68 65 20 70  t saved in the p
272bf 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
272c0 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20 61  t.** and so if a
272c1 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 6f   schema change o
272c2 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 53 43  ccurs, SQLITE_SC
272c3 48 45 4d 41 20 69 73 20 72 65 74 75 72 6e 65 64  HEMA is returned
272c4 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73   by.** sqlite3_s
272c5 74 65 70 28 29 2e 20 20 49 6e 20 74 68 65 20 6e  tep().  In the n
272c6 65 77 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20  ew version, the 
272c7 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78  original SQL tex
272c8 74 20 69 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a  t is retained.**
272c9 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65   and the stateme
272ca 6e 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  nt is automatica
272cb 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 69  lly recompiled i
272cc 66 20 61 6e 20 73 63 68 65 6d 61 20 63 68 61 6e  f an schema chan
272cd 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f  ge.** occurs..*/
272ce 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
272cf 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
272d0 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  6(.  sqlite3 *db
272d1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
272d2 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
272d3 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76  e. */ .  const v
272d4 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  oid *zSql,      
272d5 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f     /* UTF-8 enco
272d6 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ded SQL statemen
272d7 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  t. */.  int nByt
272d8 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
272d9 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a    /* Length of z
272da 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
272db 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
272dc 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20  **ppStmt,    /* 
272dd 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  OUT: A pointer t
272de 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  o the prepared s
272df 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f  tatement */.  co
272e0 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
272e1 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  l       /* OUT: 
272e2 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74  End of parsed st
272e3 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ring */.){.  int
272e4 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69   rc;.  rc = sqli
272e5 74 65 33 50 72 65 70 61 72 65 31 36 28 64 62 2c  te3Prepare16(db,
272e6 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70  zSql,nBytes,0,pp
272e7 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20  Stmt,pzTail);.  
272e8 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
272e9 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d  TE_OK || ppStmt=
272ea 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30  =0 || *ppStmt==0
272eb 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20   );  /* VERIFY: 
272ec 46 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75  F13021 */.  retu
272ed 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
272ee 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
272ef 70 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20  prepare16_v2(.  
272f0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
272f1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
272f2 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
272f3 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
272f4 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
272f5 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
272f6 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
272f7 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
272f8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
272f9 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
272fa 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71  n bytes. */.  sq
272fb 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
272fc 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
272fd 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
272fe 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
272ff 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ent */.  const v
27300 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
27301 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
27302 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
27303 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
27304 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72    rc = sqlite3Pr
27305 65 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c  epare16(db,zSql,
27306 6e 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74 2c  nBytes,1,ppStmt,
27307 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72  pzTail);.  asser
27308 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
27309 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c   || ppStmt==0 ||
2730a 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20   *ppStmt==0 );  
2730b 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32  /* VERIFY: F1302
2730c 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63  1 */.  return rc
2730d 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
2730e 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2730f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
27310 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 65 70  **** End of prep
27311 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  are.c **********
27312 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27313 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27314 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
27315 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
27316 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  select.c *******
27317 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27318 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27319 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
2731a 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
2731b 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2731c 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2731d 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2731e 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2731f 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
27320 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
27321 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
27322 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
27323 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
27324 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
27325 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
27326 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
27327 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
27328 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
27329 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2732a 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2732b 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2732c 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2732d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2732e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2732f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
27331 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
27332 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65  s C code routine
27333 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65  s that are calle
27334 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a  d by the parser.
27335 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 53 45 4c  ** to handle SEL
27336 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ECT statements i
27337 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  n SQLite..**.** 
27338 24 49 64 3a 20 73 65 6c 65 63 74 2e 63 2c 76 20  $Id: select.c,v 
27339 31 2e 35 31 32 20 32 30 30 39 2f 30 35 2f 30 33  1.512 2009/05/03
2733a 20 32 30 3a 32 33 3a 35 34 20 64 72 68 20 45 78   20:23:54 drh Ex
2733b 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 44  p $.*/.../*.** D
2733c 65 6c 65 74 65 20 61 6c 6c 20 74 68 65 20 63 6f  elete all the co
2733d 6e 74 65 6e 74 20 6f 66 20 61 20 53 65 6c 65 63  ntent of a Selec
2733e 74 20 73 74 72 75 63 74 75 72 65 20 62 75 74 20  t structure but 
2733f 64 6f 20 6e 6f 74 20 64 65 61 6c 6c 6f 63 61 74  do not deallocat
27340 65 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 20  e.** the select 
27341 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
27342 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
27343 20 63 6c 65 61 72 53 65 6c 65 63 74 28 73 71 6c   clearSelect(sql
27344 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74  ite3 *db, Select
27345 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 45   *p){.  sqlite3E
27346 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
27347 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  , p->pEList);.  
27348 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
27349 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 53 72 63  lete(db, p->pSrc
2734a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2734b 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 57  Delete(db, p->pW
2734c 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  here);.  sqlite3
2734d 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
2734e 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 3b  b, p->pGroupBy);
2734f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
27350 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 48 61 76  lete(db, p->pHav
27351 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ing);.  sqlite3E
27352 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
27353 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a  , p->pOrderBy);.
27354 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
27355 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72  elete(db, p->pPr
27356 69 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ior);.  sqlite3E
27357 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
27358 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c 69  >pLimit);.  sqli
27359 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2735a 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 7d  , p->pOffset);.}
2735b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
2735c 7a 65 20 61 20 53 65 6c 65 63 74 44 65 73 74 20  ze a SelectDest 
2735d 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
2735e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2735f 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  d sqlite3SelectD
27360 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74 44 65  estInit(SelectDe
27361 73 74 20 2a 70 44 65 73 74 2c 20 69 6e 74 20 65  st *pDest, int e
27362 44 65 73 74 2c 20 69 6e 74 20 69 50 61 72 6d 29  Dest, int iParm)
27363 7b 0a 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74  {.  pDest->eDest
27364 20 3d 20 28 75 38 29 65 44 65 73 74 3b 0a 20 20   = (u8)eDest;.  
27365 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 3d 20 69  pDest->iParm = i
27366 50 61 72 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 61  Parm;.  pDest->a
27367 66 66 69 6e 69 74 79 20 3d 20 30 3b 0a 20 20 70  ffinity = 0;.  p
27368 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 30 3b 0a  Dest->iMem = 0;.
27369 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20    pDest->nMem = 
2736a 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  0;.}.../*.** All
2736b 6f 63 61 74 65 20 61 20 6e 65 77 20 53 65 6c 65  ocate a new Sele
2736c 63 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  ct structure and
2736d 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
2736e 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 73 74 72  r to that.** str
2736f 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
27370 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74  E_PRIVATE Select
27371 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e   *sqlite3SelectN
27372 65 77 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ew(.  Parse *pPa
27373 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
27374 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
27375 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
27376 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 77 68 69  List,     /* whi
27377 63 68 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 69 6e  ch columns to in
27378 63 6c 75 64 65 20 69 6e 20 74 68 65 20 72 65 73  clude in the res
27379 75 6c 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ult */.  SrcList
2737a 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 2f   *pSrc,        /
2737b 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
2737c 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62 6c 65  e -- which table
2737d 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45  s to scan */.  E
2737e 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20  xpr *pWhere,    
2737f 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52       /* the WHER
27380 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  E clause */.  Ex
27381 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79  prList *pGroupBy
27382 2c 20 20 20 2f 2a 20 74 68 65 20 47 52 4f 55 50  ,   /* the GROUP
27383 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
27384 45 78 70 72 20 2a 70 48 61 76 69 6e 67 2c 20 20  Expr *pHaving,  
27385 20 20 20 20 20 20 2f 2a 20 74 68 65 20 48 41 56        /* the HAV
27386 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ING clause */.  
27387 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
27388 42 79 2c 20 20 20 2f 2a 20 74 68 65 20 4f 52 44  By,   /* the ORD
27389 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2738a 20 20 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74    int isDistinct
2738b 2c 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20  ,       /* true 
2738c 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20  if the DISTINCT 
2738d 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
2738e 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c  nt */.  Expr *pL
2738f 69 6d 69 74 2c 20 20 20 20 20 20 20 20 20 2f 2a  imit,         /*
27390 20 4c 49 4d 49 54 20 76 61 6c 75 65 2e 20 20 4e   LIMIT value.  N
27391 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
27392 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f  ed */.  Expr *pO
27393 66 66 73 65 74 20 20 20 20 20 20 20 20 20 2f 2a  ffset         /*
27394 20 4f 46 46 53 45 54 20 76 61 6c 75 65 2e 20 20   OFFSET value.  
27395 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 20 6f 66  NULL means no of
27396 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c  fset */.){.  Sel
27397 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 53 65 6c  ect *pNew;.  Sel
27398 65 63 74 20 73 74 61 6e 64 69 6e 3b 0a 20 20 73  ect standin;.  s
27399 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2739a 72 73 65 2d 3e 64 62 3b 0a 20 20 70 4e 65 77 20  rse->db;.  pNew 
2739b 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2739c 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2739d 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 61 73 73  (*pNew) );.  ass
2739e 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
2739f 61 69 6c 65 64 20 7c 7c 20 21 70 4f 66 66 73 65  ailed || !pOffse
273a0 74 20 7c 7c 20 70 4c 69 6d 69 74 20 29 3b 20 2f  t || pLimit ); /
273a1 2a 20 4f 46 46 53 45 54 20 69 6d 70 6c 69 65 73  * OFFSET implies
273a2 20 4c 49 4d 49 54 20 2a 2f 0a 20 20 69 66 28 20   LIMIT */.  if( 
273a3 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70  pNew==0 ){.    p
273a4 4e 65 77 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0a  New = &standin;.
273a5 20 20 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c      memset(pNew,
273a6 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77   0, sizeof(*pNew
273a7 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45  ));.  }.  if( pE
273a8 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  List==0 ){.    p
273a9 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  EList = sqlite3E
273aa 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
273ab 61 72 73 65 2c 20 30 2c 20 73 71 6c 69 74 65 33  arse, 0, sqlite3
273ac 45 78 70 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c 30  Expr(db,TK_ALL,0
273ad 2c 30 2c 30 29 2c 20 30 29 3b 0a 20 20 7d 0a 20  ,0,0), 0);.  }. 
273ae 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20   pNew->pEList = 
273af 70 45 4c 69 73 74 3b 0a 20 20 70 4e 65 77 2d 3e  pEList;.  pNew->
273b0 70 53 72 63 20 3d 20 70 53 72 63 3b 0a 20 20 70  pSrc = pSrc;.  p
273b1 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d 20 70 57  New->pWhere = pW
273b2 68 65 72 65 3b 0a 20 20 70 4e 65 77 2d 3e 70 47  here;.  pNew->pG
273b3 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42  roupBy = pGroupB
273b4 79 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69  y;.  pNew->pHavi
273b5 6e 67 20 3d 20 70 48 61 76 69 6e 67 3b 0a 20 20  ng = pHaving;.  
273b6 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d  pNew->pOrderBy =
273b7 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 4e 65   pOrderBy;.  pNe
273b8 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20 69 73  w->selFlags = is
273b9 44 69 73 74 69 6e 63 74 20 3f 20 53 46 5f 44 69  Distinct ? SF_Di
273ba 73 74 69 6e 63 74 20 3a 20 30 3b 0a 20 20 70 4e  stinct : 0;.  pN
273bb 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45  ew->op = TK_SELE
273bc 43 54 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d  CT;.  pNew->pLim
273bd 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 70  it = pLimit;.  p
273be 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70  New->pOffset = p
273bf 4f 66 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e  Offset;.  pNew->
273c0 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20  addrOpenEphm[0] 
273c1 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64  = -1;.  pNew->ad
273c2 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20  drOpenEphm[1] = 
273c3 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72  -1;.  pNew->addr
273c4 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31  OpenEphm[2] = -1
273c5 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
273c6 6f 63 46 61 69 6c 65 64 20 29 20 7b 0a 20 20 20  ocFailed ) {.   
273c7 20 63 6c 65 61 72 53 65 6c 65 63 74 28 64 62 2c   clearSelect(db,
273c8 20 70 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20   pNew);.    if( 
273c9 70 4e 65 77 21 3d 26 73 74 61 6e 64 69 6e 20 29  pNew!=&standin )
273ca 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
273cb 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 70 4e  b, pNew);.    pN
273cc 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ew = 0;.  }.  re
273cd 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
273ce 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 67  .** Delete the g
273cf 69 76 65 6e 20 53 65 6c 65 63 74 20 73 74 72 75  iven Select stru
273d0 63 74 75 72 65 20 61 6e 64 20 61 6c 6c 20 6f 66  cture and all of
273d1 20 69 74 73 20 73 75 62 73 74 72 75 63 74 75 72   its substructur
273d2 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
273d3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
273d4 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 73  e3SelectDelete(s
273d5 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65  qlite3 *db, Sele
273d6 63 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  ct *p){.  if( p 
273d7 29 7b 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65  ){.    clearSele
273d8 63 74 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73  ct(db, p);.    s
273d9 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
273da 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   p);.  }.}../*.*
273db 2a 20 47 69 76 65 6e 20 31 20 74 6f 20 33 20 69  * Given 1 to 3 i
273dc 64 65 6e 74 69 66 69 65 72 73 20 70 72 65 63 65  dentifiers prece
273dd 65 64 69 6e 67 20 74 68 65 20 4a 4f 49 4e 20 6b  eding the JOIN k
273de 65 79 77 6f 72 64 2c 20 64 65 74 65 72 6d 69 6e  eyword, determin
273df 65 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 6f 66  e the.** type of
273e0 20 6a 6f 69 6e 2e 20 20 52 65 74 75 72 6e 20 61   join.  Return a
273e1 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61  n integer consta
273e2 6e 74 20 74 68 61 74 20 65 78 70 72 65 73 73 65  nt that expresse
273e3 73 20 74 68 61 74 20 74 79 70 65 0a 2a 2a 20 69  s that type.** i
273e4 6e 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66  n terms of the f
273e5 6f 6c 6c 6f 77 69 6e 67 20 62 69 74 20 76 61 6c  ollowing bit val
273e6 75 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4a  ues:.**.**     J
273e7 54 5f 49 4e 4e 45 52 0a 2a 2a 20 20 20 20 20 4a  T_INNER.**     J
273e8 54 5f 43 52 4f 53 53 0a 2a 2a 20 20 20 20 20 4a  T_CROSS.**     J
273e9 54 5f 4f 55 54 45 52 0a 2a 2a 20 20 20 20 20 4a  T_OUTER.**     J
273ea 54 5f 4e 41 54 55 52 41 4c 0a 2a 2a 20 20 20 20  T_NATURAL.**    
273eb 20 4a 54 5f 4c 45 46 54 0a 2a 2a 20 20 20 20 20   JT_LEFT.**     
273ec 4a 54 5f 52 49 47 48 54 0a 2a 2a 0a 2a 2a 20 41  JT_RIGHT.**.** A
273ed 20 66 75 6c 6c 20 6f 75 74 65 72 20 6a 6f 69 6e   full outer join
273ee 20 69 73 20 74 68 65 20 63 6f 6d 62 69 6e 61 74   is the combinat
273ef 69 6f 6e 20 6f 66 20 4a 54 5f 4c 45 46 54 20 61  ion of JT_LEFT a
273f0 6e 64 20 4a 54 5f 52 49 47 48 54 2e 0a 2a 2a 0a  nd JT_RIGHT..**.
273f1 2a 2a 20 49 66 20 61 6e 20 69 6c 6c 65 67 61 6c  ** If an illegal
273f2 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20   or unsupported 
273f3 6a 6f 69 6e 20 74 79 70 65 20 69 73 20 73 65 65  join type is see
273f4 6e 2c 20 74 68 65 6e 20 73 74 69 6c 6c 20 72 65  n, then still re
273f5 74 75 72 6e 0a 2a 2a 20 61 20 6a 6f 69 6e 20 74  turn.** a join t
273f6 79 70 65 2c 20 62 75 74 20 70 75 74 20 61 6e 20  ype, but put an 
273f7 65 72 72 6f 72 20 69 6e 20 74 68 65 20 70 50 61  error in the pPa
273f8 72 73 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  rse structure..*
273f9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
273fa 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e   int sqlite3Join
273fb 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72  Type(Parse *pPar
273fc 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 41 2c 20 54  se, Token *pA, T
273fd 6f 6b 65 6e 20 2a 70 42 2c 20 54 6f 6b 65 6e 20  oken *pB, Token 
273fe 2a 70 43 29 7b 0a 20 20 69 6e 74 20 6a 6f 69 6e  *pC){.  int join
273ff 74 79 70 65 20 3d 20 30 3b 0a 20 20 54 6f 6b 65  type = 0;.  Toke
27400 6e 20 2a 61 70 41 6c 6c 5b 33 5d 3b 0a 20 20 54  n *apAll[3];.  T
27401 6f 6b 65 6e 20 2a 70 3b 0a 20 20 73 74 61 74 69  oken *p;.  stati
27402 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 7b  c const struct {
27403 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
27404 7a 4b 65 79 77 6f 72 64 5b 38 5d 3b 0a 20 20 20  zKeyword[8];.   
27405 20 75 38 20 6e 43 68 61 72 3b 0a 20 20 20 20 75   u8 nChar;.    u
27406 38 20 63 6f 64 65 3b 0a 20 20 7d 20 6b 65 79 77  8 code;.  } keyw
27407 6f 72 64 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b  ords[] = {.    {
27408 20 22 6e 61 74 75 72 61 6c 22 2c 20 37 2c 20 4a   "natural", 7, J
27409 54 5f 4e 41 54 55 52 41 4c 20 7d 2c 0a 20 20 20  T_NATURAL },.   
2740a 20 7b 20 22 6c 65 66 74 22 2c 20 20 20 20 34 2c   { "left",    4,
2740b 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 4f 55 54 45   JT_LEFT|JT_OUTE
2740c 52 20 7d 2c 0a 20 20 20 20 7b 20 22 72 69 67 68  R },.    { "righ
2740d 74 22 2c 20 20 20 35 2c 20 4a 54 5f 52 49 47 48  t",   5, JT_RIGH
2740e 54 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20  T|JT_OUTER },.  
2740f 20 20 7b 20 22 66 75 6c 6c 22 2c 20 20 20 20 34    { "full",    4
27410 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 52 49 47  , JT_LEFT|JT_RIG
27411 48 54 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20  HT|JT_OUTER },. 
27412 20 20 20 7b 20 22 6f 75 74 65 72 22 2c 20 20 20     { "outer",   
27413 35 2c 20 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20  5, JT_OUTER },. 
27414 20 20 20 7b 20 22 69 6e 6e 65 72 22 2c 20 20 20     { "inner",   
27415 35 2c 20 4a 54 5f 49 4e 4e 45 52 20 7d 2c 0a 20  5, JT_INNER },. 
27416 20 20 20 7b 20 22 63 72 6f 73 73 22 2c 20 20 20     { "cross",   
27417 35 2c 20 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 43  5, JT_INNER|JT_C
27418 52 4f 53 53 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69  ROSS },.  };.  i
27419 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 70 41 6c 6c  nt i, j;.  apAll
2741a 5b 30 5d 20 3d 20 70 41 3b 0a 20 20 61 70 41 6c  [0] = pA;.  apAl
2741b 6c 5b 31 5d 20 3d 20 70 42 3b 0a 20 20 61 70 41  l[1] = pB;.  apA
2741c 6c 6c 5b 32 5d 20 3d 20 70 43 3b 0a 20 20 66 6f  ll[2] = pC;.  fo
2741d 72 28 69 3d 30 3b 20 69 3c 33 20 26 26 20 61 70  r(i=0; i<3 && ap
2741e 41 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20  All[i]; i++){.  
2741f 20 20 70 20 3d 20 61 70 41 6c 6c 5b 69 5d 3b 0a    p = apAll[i];.
27420 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41      for(j=0; j<A
27421 72 72 61 79 53 69 7a 65 28 6b 65 79 77 6f 72 64  rraySize(keyword
27422 73 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  s); j++){.      
27423 69 66 28 20 70 2d 3e 6e 3d 3d 6b 65 79 77 6f 72  if( p->n==keywor
27424 64 73 5b 6a 5d 2e 6e 43 68 61 72 20 0a 20 20 20  ds[j].nChar .   
27425 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
27426 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a  3StrNICmp((char*
27427 29 70 2d 3e 7a 2c 20 6b 65 79 77 6f 72 64 73 5b  )p->z, keywords[
27428 6a 5d 2e 7a 4b 65 79 77 6f 72 64 2c 20 70 2d 3e  j].zKeyword, p->
27429 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  n)==0 ){.       
2742a 20 6a 6f 69 6e 74 79 70 65 20 7c 3d 20 6b 65 79   jointype |= key
2742b 77 6f 72 64 73 5b 6a 5d 2e 63 6f 64 65 3b 0a 20  words[j].code;. 
2742c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2742d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2742e 69 66 28 20 6a 3e 3d 41 72 72 61 79 53 69 7a 65  if( j>=ArraySize
2742f 28 6b 65 79 77 6f 72 64 73 29 20 29 7b 0a 20 20  (keywords) ){.  
27430 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d 20      jointype |= 
27431 4a 54 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  JT_ERROR;.      
27432 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
27433 0a 20 20 69 66 28 0a 20 20 20 20 20 28 6a 6f 69  .  if(.     (joi
27434 6e 74 79 70 65 20 26 20 28 4a 54 5f 49 4e 4e 45  ntype & (JT_INNE
27435 52 7c 4a 54 5f 4f 55 54 45 52 29 29 3d 3d 28 4a  R|JT_OUTER))==(J
27436 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52  T_INNER|JT_OUTER
27437 29 20 7c 7c 0a 20 20 20 20 20 28 6a 6f 69 6e 74  ) ||.     (joint
27438 79 70 65 20 26 20 4a 54 5f 45 52 52 4f 52 29 21  ype & JT_ERROR)!
27439 3d 30 0a 20 20 29 7b 0a 20 20 20 20 63 6f 6e 73  =0.  ){.    cons
2743a 74 20 63 68 61 72 20 2a 7a 53 70 20 3d 20 22 20  t char *zSp = " 
2743b 22 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ";.    assert( p
2743c 42 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  B!=0 );.    if( 
2743d 70 43 3d 3d 30 20 29 7b 20 7a 53 70 2b 2b 3b 20  pC==0 ){ zSp++; 
2743e 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  }.    sqlite3Err
2743f 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75  orMsg(pParse, "u
27440 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70  nknown or unsupp
27441 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 3a  orted join type:
27442 20 22 0a 20 20 20 20 20 20 20 22 25 54 20 25 54   ".       "%T %T
27443 25 73 25 54 22 2c 20 70 41 2c 20 70 42 2c 20 7a  %s%T", pA, pB, z
27444 53 70 2c 20 70 43 29 3b 0a 20 20 20 20 6a 6f 69  Sp, pC);.    joi
27445 6e 74 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52  ntype = JT_INNER
27446 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6a 6f  ;.  }else if( jo
27447 69 6e 74 79 70 65 20 26 20 4a 54 5f 52 49 47 48  intype & JT_RIGH
27448 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  T ){.    sqlite3
27449 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2744a 20 0a 20 20 20 20 20 20 22 52 49 47 48 54 20 61   .      "RIGHT a
2744b 6e 64 20 46 55 4c 4c 20 4f 55 54 45 52 20 4a 4f  nd FULL OUTER JO
2744c 49 4e 73 20 61 72 65 20 6e 6f 74 20 63 75 72 72  INs are not curr
2744d 65 6e 74 6c 79 20 73 75 70 70 6f 72 74 65 64 22  ently supported"
2744e 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65 20  );.    jointype 
2744f 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 0a  = JT_INNER;.  }.
27450 20 20 72 65 74 75 72 6e 20 6a 6f 69 6e 74 79 70    return jointyp
27451 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  e;.}../*.** Retu
27452 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
27453 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61  a column in a ta
27454 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 2d 31 20  ble.  Return -1 
27455 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a  if the column.**
27456 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65   is not containe
27457 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  d in the table..
27458 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
27459 6c 75 6d 6e 49 6e 64 65 78 28 54 61 62 6c 65 20  lumnIndex(Table 
2745a 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61  *pTab, const cha
2745b 72 20 2a 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74 20  r *zCol){.  int 
2745c 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
2745d 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
2745e 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
2745f 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61  3StrICmp(pTab->a
27460 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 43  Col[i].zName, zC
27461 6f 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ol)==0 ) return 
27462 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  i;.  }.  return 
27463 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  -1;.}../*.** Set
27464 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
27465 74 6f 6b 65 6e 20 74 6f 20 61 20 27 5c 30 30 30  token to a '\000
27466 27 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  '-terminated str
27467 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ing..*/.static v
27468 6f 69 64 20 73 65 74 54 6f 6b 65 6e 28 54 6f 6b  oid setToken(Tok
27469 65 6e 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  en *p, const cha
2746a 72 20 2a 7a 29 7b 0a 20 20 70 2d 3e 7a 20 3d 20  r *z){.  p->z = 
2746b 28 75 38 2a 29 7a 3b 0a 20 20 70 2d 3e 6e 20 3d  (u8*)z;.  p->n =
2746c 20 7a 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c   z ? sqlite3Strl
2746d 65 6e 33 30 28 7a 29 20 3a 20 30 3b 0a 20 20 70  en30(z) : 0;.  p
2746e 2d 3e 64 79 6e 20 3d 20 30 3b 0a 20 20 70 2d 3e  ->dyn = 0;.  p->
2746f 71 75 6f 74 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f  quoted = 0;.}../
27470 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e 20 65  *.** Create an e
27471 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66  xpression node f
27472 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  or an identifier
27473 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 20 6f   with the name o
27474 66 20 7a 4e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54  f zName.*/.SQLIT
27475 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
27476 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45  sqlite3CreateIdE
27477 78 70 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  xpr(Parse *pPars
27478 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
27479 4e 61 6d 65 29 7b 0a 20 20 54 6f 6b 65 6e 20 64  Name){.  Token d
2747a 75 6d 6d 79 3b 0a 20 20 73 65 74 54 6f 6b 65 6e  ummy;.  setToken
2747b 28 26 64 75 6d 6d 79 2c 20 7a 4e 61 6d 65 29 3b  (&dummy, zName);
2747c 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
2747d 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2747e 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 64 75 6d  K_ID, 0, 0, &dum
2747f 6d 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  my);.}../*.** Ad
27480 64 20 61 20 74 65 72 6d 20 74 6f 20 74 68 65 20  d a term to the 
27481 57 48 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e  WHERE expression
27482 20 69 6e 20 2a 70 70 45 78 70 72 20 74 68 61 74   in *ppExpr that
27483 20 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a   requires the.**
27484 20 7a 43 6f 6c 20 63 6f 6c 75 6d 6e 20 74 6f 20   zCol column to 
27485 62 65 20 65 71 75 61 6c 20 69 6e 20 74 68 65 20  be equal in the 
27486 74 77 6f 20 74 61 62 6c 65 73 20 70 54 61 62 31  two tables pTab1
27487 20 61 6e 64 20 70 54 61 62 32 2e 0a 2a 2f 0a 73   and pTab2..*/.s
27488 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 57 68  tatic void addWh
27489 65 72 65 54 65 72 6d 28 0a 20 20 50 61 72 73 65  ereTerm(.  Parse
2748a 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2748b 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2748c 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73  ontext */.  cons
2748d 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20  t char *zCol,   
2748e 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2748f 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  the column */.  
27490 63 6f 6e 73 74 20 54 61 62 6c 65 20 2a 70 54 61  const Table *pTa
27491 62 31 2c 20 20 20 20 20 20 2f 2a 20 46 69 72 73  b1,      /* Firs
27492 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e  t table */.  con
27493 73 74 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 31  st char *zAlias1
27494 2c 20 20 20 20 20 2f 2a 20 41 6c 69 61 73 20 66  ,     /* Alias f
27495 6f 72 20 66 69 72 73 74 20 74 61 62 6c 65 2e 20  or first table. 
27496 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
27497 20 20 63 6f 6e 73 74 20 54 61 62 6c 65 20 2a 70    const Table *p
27498 54 61 62 32 2c 20 20 20 20 20 20 2f 2a 20 53 65  Tab2,      /* Se
27499 63 6f 6e 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20  cond table */.  
2749a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 6c 69  const char *zAli
2749b 61 73 32 2c 20 20 20 20 20 2f 2a 20 41 6c 69 61  as2,     /* Alia
2749c 73 20 66 6f 72 20 73 65 63 6f 6e 64 20 74 61 62  s for second tab
2749d 6c 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  le.  May be NULL
2749e 20 2a 2f 0a 20 20 69 6e 74 20 69 52 69 67 68 74   */.  int iRight
2749f 4a 6f 69 6e 54 61 62 6c 65 2c 20 20 20 20 20 2f  JoinTable,     /
274a0 2a 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f  * VDBE cursor fo
274a1 72 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  r the right tabl
274a2 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 70 70  e */.  Expr **pp
274a3 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
274a4 2f 2a 20 41 64 64 20 74 68 65 20 65 71 75 61 6c  /* Add the equal
274a5 69 74 79 20 74 65 72 6d 20 74 6f 20 74 68 69 73  ity term to this
274a6 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
274a7 20 69 6e 74 20 69 73 4f 75 74 65 72 4a 6f 69 6e   int isOuterJoin
274a8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
274a9 65 20 69 66 20 64 65 61 6c 69 6e 67 20 77 69 74  e if dealing wit
274aa 68 20 61 6e 20 4f 55 54 45 52 20 6a 6f 69 6e 20  h an OUTER join 
274ab 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 45  */.){.  Expr *pE
274ac 31 61 2c 20 2a 70 45 31 62 2c 20 2a 70 45 31 63  1a, *pE1b, *pE1c
274ad 3b 0a 20 20 45 78 70 72 20 2a 70 45 32 61 2c 20  ;.  Expr *pE2a, 
274ae 2a 70 45 32 62 2c 20 2a 70 45 32 63 3b 0a 20 20  *pE2b, *pE2c;.  
274af 45 78 70 72 20 2a 70 45 3b 0a 0a 20 20 70 45 31  Expr *pE;..  pE1
274b0 61 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  a = sqlite3Creat
274b1 65 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20  eIdExpr(pParse, 
274b2 7a 43 6f 6c 29 3b 0a 20 20 70 45 32 61 20 3d 20  zCol);.  pE2a = 
274b3 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45  sqlite3CreateIdE
274b4 78 70 72 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c  xpr(pParse, zCol
274b5 29 3b 0a 20 20 69 66 28 20 7a 41 6c 69 61 73 31  );.  if( zAlias1
274b6 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 41 6c 69 61  ==0 ){.    zAlia
274b7 73 31 20 3d 20 70 54 61 62 31 2d 3e 7a 4e 61 6d  s1 = pTab1->zNam
274b8 65 3b 0a 20 20 7d 0a 20 20 70 45 31 62 20 3d 20  e;.  }.  pE1b = 
274b9 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45  sqlite3CreateIdE
274ba 78 70 72 28 70 50 61 72 73 65 2c 20 7a 41 6c 69  xpr(pParse, zAli
274bb 61 73 31 29 3b 0a 20 20 69 66 28 20 7a 41 6c 69  as1);.  if( zAli
274bc 61 73 32 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 41  as2==0 ){.    zA
274bd 6c 69 61 73 32 20 3d 20 70 54 61 62 32 2d 3e 7a  lias2 = pTab2->z
274be 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 70 45 32 62  Name;.  }.  pE2b
274bf 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
274c0 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20 7a  IdExpr(pParse, z
274c1 41 6c 69 61 73 32 29 3b 0a 20 20 70 45 31 63 20  Alias2);.  pE1c 
274c2 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
274c3 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70  Parse, TK_DOT, p
274c4 45 31 62 2c 20 70 45 31 61 2c 20 30 29 3b 0a 20  E1b, pE1a, 0);. 
274c5 20 70 45 32 63 20 3d 20 73 71 6c 69 74 65 33 50   pE2c = sqlite3P
274c6 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
274c7 44 4f 54 2c 20 70 45 32 62 2c 20 70 45 32 61 2c  DOT, pE2b, pE2a,
274c8 20 30 29 3b 0a 20 20 70 45 20 3d 20 73 71 6c 69   0);.  pE = sqli
274c9 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
274ca 20 54 4b 5f 45 51 2c 20 70 45 31 63 2c 20 70 45   TK_EQ, pE1c, pE
274cb 32 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 45  2c, 0);.  if( pE
274cc 20 26 26 20 69 73 4f 75 74 65 72 4a 6f 69 6e 20   && isOuterJoin 
274cd 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72  ){.    ExprSetPr
274ce 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72  operty(pE, EP_Fr
274cf 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 70 45 2d  omJoin);.    pE-
274d0 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  >iRightJoinTable
274d1 20 3d 20 69 52 69 67 68 74 4a 6f 69 6e 54 61 62   = iRightJoinTab
274d2 6c 65 3b 0a 20 20 7d 0a 20 20 2a 70 70 45 78 70  le;.  }.  *ppExp
274d3 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  r = sqlite3ExprA
274d4 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 2a 70  nd(pParse->db,*p
274d5 70 45 78 70 72 2c 20 70 45 29 3b 0a 7d 0a 0a 2f  pExpr, pE);.}../
274d6 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 50 5f  *.** Set the EP_
274d7 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74  FromJoin propert
274d8 79 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f  y on all terms o
274d9 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72  f the given expr
274da 65 73 73 69 6f 6e 2e 0a 2a 2a 20 41 6e 64 20 73  ession..** And s
274db 65 74 20 74 68 65 20 45 78 70 72 2e 69 52 69 67  et the Expr.iRig
274dc 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74 6f 20 69  htJoinTable to i
274dd 54 61 62 6c 65 20 66 6f 72 20 65 76 65 72 79 20  Table for every 
274de 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 65  term in the.** e
274df 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
274e0 20 54 68 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e   The EP_FromJoin
274e1 20 70 72 6f 70 65 72 74 79 20 69 73 20 75 73 65   property is use
274e2 64 20 6f 6e 20 74 65 72 6d 73 20 6f 66 20 61 6e  d on terms of an
274e3 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 74   expression to t
274e4 65 6c 6c 0a 2a 2a 20 74 68 65 20 4c 45 46 54 20  ell.** the LEFT 
274e5 4f 55 54 45 52 20 4a 4f 49 4e 20 70 72 6f 63 65  OUTER JOIN proce
274e6 73 73 69 6e 67 20 6c 6f 67 69 63 20 74 68 61 74  ssing logic that
274e7 20 74 68 69 73 20 74 65 72 6d 20 69 73 20 70 61   this term is pa
274e8 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6a 6f 69  rt of the.** joi
274e9 6e 20 72 65 73 74 72 69 63 74 69 6f 6e 20 73 70  n restriction sp
274ea 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 4f  ecified in the O
274eb 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  N or USING claus
274ec 65 20 61 6e 64 20 6e 6f 74 20 61 20 70 61 72 74  e and not a part
274ed 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 6f 72 65 20  .** of the more 
274ee 67 65 6e 65 72 61 6c 20 57 48 45 52 45 20 63 6c  general WHERE cl
274ef 61 75 73 65 2e 20 20 54 68 65 73 65 20 74 65 72  ause.  These ter
274f0 6d 73 20 61 72 65 20 6d 6f 76 65 64 20 6f 76 65  ms are moved ove
274f1 72 20 74 6f 20 74 68 65 0a 2a 2a 20 57 48 45 52  r to the.** WHER
274f2 45 20 63 6c 61 75 73 65 20 64 75 72 69 6e 67 20  E clause during 
274f3 6a 6f 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20  join processing 
274f4 62 75 74 20 77 65 20 6e 65 65 64 20 74 6f 20 72  but we need to r
274f5 65 6d 65 6d 62 65 72 20 74 68 61 74 20 74 68 65  emember that the
274f6 79 0a 2a 2a 20 6f 72 69 67 69 6e 61 74 65 64 20  y.** originated 
274f7 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  in the ON or USI
274f8 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  NG clause..**.**
274f9 20 54 68 65 20 45 78 70 72 2e 69 52 69 67 68 74   The Expr.iRight
274fa 4a 6f 69 6e 54 61 62 6c 65 20 74 65 6c 6c 73 20  JoinTable tells 
274fb 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
274fc 20 70 72 6f 63 65 73 73 69 6e 67 20 74 68 61 74   processing that
274fd 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69   the.** expressi
274fe 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 61  on depends on ta
274ff 62 6c 65 20 69 52 69 67 68 74 4a 6f 69 6e 54 61  ble iRightJoinTa
27500 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 61 74  ble even if that
27501 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 0a 2a 2a   table is not.**
27502 20 65 78 70 6c 69 63 69 74 6c 79 20 6d 65 6e 74   explicitly ment
27503 69 6f 6e 65 64 20 69 6e 20 74 68 65 20 65 78 70  ioned in the exp
27504 72 65 73 73 69 6f 6e 2e 20 20 54 68 61 74 20 69  ression.  That i
27505 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6e 65  nformation is ne
27506 65 64 65 64 0a 2a 2a 20 66 6f 72 20 63 61 73 65  eded.** for case
27507 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
27508 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  **    SELECT * F
27509 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e  ROM t1 LEFT JOIN
2750a 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 62   t2 ON t1.a=t2.b
2750b 20 41 4e 44 20 74 31 2e 78 3d 35 0a 2a 2a 0a 2a   AND t1.x=5.**.*
2750c 2a 20 54 68 65 20 77 68 65 72 65 20 63 6c 61 75  * The where clau
2750d 73 65 20 6e 65 65 64 73 20 74 6f 20 64 65 66 65  se needs to defe
2750e 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f  r the handling o
2750f 66 20 74 68 65 20 74 31 2e 78 3d 35 0a 2a 2a 20  f the t1.x=5.** 
27510 74 65 72 6d 20 75 6e 74 69 6c 20 61 66 74 65 72  term until after
27511 20 74 68 65 20 74 32 20 6c 6f 6f 70 20 6f 66 20   the t2 loop of 
27512 74 68 65 20 6a 6f 69 6e 2e 20 20 49 6e 20 74 68  the join.  In th
27513 61 74 20 77 61 79 2c 20 61 0a 2a 2a 20 4e 55 4c  at way, a.** NUL
27514 4c 20 74 32 20 72 6f 77 20 77 69 6c 6c 20 62 65  L t2 row will be
27515 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e 65 76   inserted whenev
27516 65 72 20 74 31 2e 78 21 3d 35 2e 20 20 49 66 20  er t1.x!=5.  If 
27517 77 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65 66  we do not.** def
27518 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20  er the handling 
27519 6f 66 20 74 31 2e 78 3d 35 2c 20 69 74 20 77 69  of t1.x=5, it wi
2751a 6c 6c 20 62 65 20 70 72 6f 63 65 73 73 65 64 20  ll be processed 
2751b 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 61  immediately.** a
2751c 66 74 65 72 20 74 68 65 20 74 31 20 6c 6f 6f 70  fter the t1 loop
2751d 20 61 6e 64 20 72 6f 77 73 20 77 69 74 68 20 74   and rows with t
2751e 31 2e 78 21 3d 35 20 77 69 6c 6c 20 6e 65 76 65  1.x!=5 will neve
2751f 72 20 61 70 70 65 61 72 20 69 6e 0a 2a 2a 20 74  r appear in.** t
27520 68 65 20 6f 75 74 70 75 74 2c 20 77 68 69 63 68  he output, which
27521 20 69 73 20 69 6e 63 6f 72 72 65 63 74 2e 0a 2a   is incorrect..*
27522 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65  /.static void se
27523 74 4a 6f 69 6e 45 78 70 72 28 45 78 70 72 20 2a  tJoinExpr(Expr *
27524 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 29 7b 0a  p, int iTable){.
27525 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20    while( p ){.  
27526 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
27527 79 28 70 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e  y(p, EP_FromJoin
27528 29 3b 0a 20 20 20 20 70 2d 3e 69 52 69 67 68 74  );.    p->iRight
27529 4a 6f 69 6e 54 61 62 6c 65 20 3d 20 69 54 61 62  JoinTable = iTab
2752a 6c 65 3b 0a 20 20 20 20 73 65 74 4a 6f 69 6e 45  le;.    setJoinE
2752b 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 69 54  xpr(p->pLeft, iT
2752c 61 62 6c 65 29 3b 0a 20 20 20 20 70 20 3d 20 70  able);.    p = p
2752d 2d 3e 70 52 69 67 68 74 3b 0a 20 20 7d 20 0a 7d  ->pRight;.  } .}
2752e 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2752f 74 69 6e 65 20 70 72 6f 63 65 73 73 65 73 20 74  tine processes t
27530 68 65 20 6a 6f 69 6e 20 69 6e 66 6f 72 6d 61 74  he join informat
27531 69 6f 6e 20 66 6f 72 20 61 20 53 45 4c 45 43 54  ion for a SELECT
27532 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 4f   statement..** O
27533 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75  N and USING clau
27534 73 65 73 20 61 72 65 20 63 6f 6e 76 65 72 74 65  ses are converte
27535 64 20 69 6e 74 6f 20 65 78 74 72 61 20 74 65 72  d into extra ter
27536 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  ms of the WHERE 
27537 63 6c 61 75 73 65 2e 0a 2a 2a 20 4e 41 54 55 52  clause..** NATUR
27538 41 4c 20 6a 6f 69 6e 73 20 61 6c 73 6f 20 63 72  AL joins also cr
27539 65 61 74 65 20 65 78 74 72 61 20 57 48 45 52 45  eate extra WHERE
2753a 20 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a   clause terms..*
2753b 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 6f  *.** The terms o
2753c 66 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  f a FROM clause 
2753d 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  are contained in
2753e 20 74 68 65 20 53 65 6c 65 63 74 2e 70 53 72 63   the Select.pSrc
2753f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54   structure..** T
27540 68 65 20 6c 65 66 74 20 6d 6f 73 74 20 74 61 62  he left most tab
27541 6c 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20  le is the first 
27542 65 6e 74 72 79 20 69 6e 20 53 65 6c 65 63 74 2e  entry in Select.
27543 70 53 72 63 2e 20 20 54 68 65 20 72 69 67 68 74  pSrc.  The right
27544 2d 6d 6f 73 74 0a 2a 2a 20 74 61 62 6c 65 20 69  -most.** table i
27545 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  s the last entry
27546 2e 20 20 54 68 65 20 6a 6f 69 6e 20 6f 70 65 72  .  The join oper
27547 61 74 6f 72 20 69 73 20 68 65 6c 64 20 69 6e 20  ator is held in 
27548 74 68 65 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20  the entry to.** 
27549 74 68 65 20 6c 65 66 74 2e 20 20 54 68 75 73 20  the left.  Thus 
2754a 65 6e 74 72 79 20 30 20 63 6f 6e 74 61 69 6e 73  entry 0 contains
2754b 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74   the join operat
2754c 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 69 6e 20  or for the join 
2754d 62 65 74 77 65 65 6e 0a 2a 2a 20 65 6e 74 72 69  between.** entri
2754e 65 73 20 30 20 61 6e 64 20 31 2e 20 20 41 6e 79  es 0 and 1.  Any
2754f 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
27550 75 73 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  uses associated 
27551 77 69 74 68 20 74 68 65 20 6a 6f 69 6e 20 61 72  with the join ar
27552 65 0a 2a 2a 20 61 6c 73 6f 20 61 74 74 61 63 68  e.** also attach
27553 65 64 20 74 6f 20 74 68 65 20 6c 65 66 74 20 65  ed to the left e
27554 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ntry..**.** This
27555 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
27556 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
27557 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65  rrors encountere
27558 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
27559 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f   sqliteProcessJo
2755a 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  in(Parse *pParse
2755b 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
2755c 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20  SrcList *pSrc;  
2755d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2755e 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20 69 6e  /* All tables in
2755f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
27560 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20   */.  int i, j; 
27561 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27562 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
27563 75 6e 74 65 72 73 20 2a 2f 0a 20 20 73 74 72 75  unters */.  stru
27564 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
27565 2a 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c  *pLeft;     /* L
27566 65 66 74 20 74 61 62 6c 65 20 62 65 69 6e 67 20  eft table being 
27567 6a 6f 69 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75  joined */.  stru
27568 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
27569 2a 70 52 69 67 68 74 3b 20 20 20 20 2f 2a 20 52  *pRight;    /* R
2756a 69 67 68 74 20 74 61 62 6c 65 20 62 65 69 6e 67  ight table being
2756b 20 6a 6f 69 6e 65 64 20 2a 2f 0a 0a 20 20 70 53   joined */..  pS
2756c 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20  rc = p->pSrc;.  
2756d 70 4c 65 66 74 20 3d 20 26 70 53 72 63 2d 3e 61  pLeft = &pSrc->a
2756e 5b 30 5d 3b 0a 20 20 70 52 69 67 68 74 20 3d 20  [0];.  pRight = 
2756f 26 70 4c 65 66 74 5b 31 5d 3b 0a 20 20 66 6f 72  &pLeft[1];.  for
27570 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 53  (i=0; i<pSrc->nS
27571 72 63 2d 31 3b 20 69 2b 2b 2c 20 70 52 69 67 68  rc-1; i++, pRigh
27572 74 2b 2b 2c 20 70 4c 65 66 74 2b 2b 29 7b 0a 20  t++, pLeft++){. 
27573 20 20 20 54 61 62 6c 65 20 2a 70 4c 65 66 74 54     Table *pLeftT
27574 61 62 20 3d 20 70 4c 65 66 74 2d 3e 70 54 61 62  ab = pLeft->pTab
27575 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 52 69  ;.    Table *pRi
27576 67 68 74 54 61 62 20 3d 20 70 52 69 67 68 74 2d  ghtTab = pRight-
27577 3e 70 54 61 62 3b 0a 20 20 20 20 69 6e 74 20 69  >pTab;.    int i
27578 73 4f 75 74 65 72 3b 0a 0a 20 20 20 20 69 66 28  sOuter;..    if(
27579 20 4e 45 56 45 52 28 70 4c 65 66 74 54 61 62 3d   NEVER(pLeftTab=
2757a 3d 30 20 7c 7c 20 70 52 69 67 68 74 54 61 62 3d  =0 || pRightTab=
2757b 3d 30 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  =0) ) continue;.
2757c 20 20 20 20 69 73 4f 75 74 65 72 20 3d 20 28 70      isOuter = (p
2757d 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20  Right->jointype 
2757e 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 3b 0a  & JT_OUTER)!=0;.
2757f 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65  .    /* When the
27580 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64   NATURAL keyword
27581 20 69 73 20 70 72 65 73 65 6e 74 2c 20 61 64 64   is present, add
27582 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
27583 72 6d 73 20 66 6f 72 0a 20 20 20 20 2a 2a 20 65  rms for.    ** e
27584 76 65 72 79 20 63 6f 6c 75 6d 6e 20 74 68 61 74  very column that
27585 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20   the two tables 
27586 68 61 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a  have in common..
27587 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
27588 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20  Right->jointype 
27589 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a  & JT_NATURAL ){.
2758a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74        if( pRight
2758b 2d 3e 70 4f 6e 20 7c 7c 20 70 52 69 67 68 74 2d  ->pOn || pRight-
2758c 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20  >pUsing ){.     
2758d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2758e 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 4e 41  sg(pParse, "a NA
2758f 54 55 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e  TURAL join may n
27590 6f 74 20 68 61 76 65 20 22 0a 20 20 20 20 20 20  ot have ".      
27591 20 20 20 20 20 22 61 6e 20 4f 4e 20 6f 72 20 55       "an ON or U
27592 53 49 4e 47 20 63 6c 61 75 73 65 22 2c 20 30 29  SING clause", 0)
27593 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
27594 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
27595 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 65    for(j=0; j<pLe
27596 66 74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  ftTab->nCol; j++
27597 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
27598 2a 7a 4e 61 6d 65 20 3d 20 70 4c 65 66 74 54 61  *zName = pLeftTa
27599 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65  b->aCol[j].zName
2759a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f  ;.        if( co
2759b 6c 75 6d 6e 49 6e 64 65 78 28 70 52 69 67 68 74  lumnIndex(pRight
2759c 54 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29  Tab, zName)>=0 )
2759d 7b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 57  {.          addW
2759e 68 65 72 65 54 65 72 6d 28 70 50 61 72 73 65 2c  hereTerm(pParse,
2759f 20 7a 4e 61 6d 65 2c 20 70 4c 65 66 74 54 61 62   zName, pLeftTab
275a0 2c 20 70 4c 65 66 74 2d 3e 7a 41 6c 69 61 73 2c  , pLeft->zAlias,
275a1 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
275a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275a3 70 52 69 67 68 74 54 61 62 2c 20 70 52 69 67 68  pRightTab, pRigh
275a4 74 2d 3e 7a 41 6c 69 61 73 2c 0a 20 20 20 20 20  t->zAlias,.     
275a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275a6 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74 2d           pRight-
275a7 3e 69 43 75 72 73 6f 72 2c 20 26 70 2d 3e 70 57  >iCursor, &p->pW
275a8 68 65 72 65 2c 20 69 73 4f 75 74 65 72 29 3b 0a  here, isOuter);.
275a9 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20            .     
275aa 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
275ab 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61 6c   }..    /* Disal
275ac 6c 6f 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20  low both ON and 
275ad 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 69 6e  USING clauses in
275ae 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0a 20   the same join. 
275af 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52     */.    if( pR
275b0 69 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52 69  ight->pOn && pRi
275b1 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20  ght->pUsing ){. 
275b2 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
275b3 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
275b4 6e 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20 4f  nnot have both O
275b5 4e 20 61 6e 64 20 55 53 49 4e 47 20 22 0a 20 20  N and USING ".  
275b6 20 20 20 20 20 20 22 63 6c 61 75 73 65 73 20 69        "clauses i
275b7 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 22  n the same join"
275b8 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
275b9 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  1;.    }..    /*
275ba 20 41 64 64 20 74 68 65 20 4f 4e 20 63 6c 61 75   Add the ON clau
275bb 73 65 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  se to the end of
275bc 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
275bd 65 2c 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a  e, connected by.
275be 20 20 20 20 2a 2a 20 61 6e 20 41 4e 44 20 6f 70      ** an AND op
275bf 65 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20  erator..    */. 
275c0 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70     if( pRight->p
275c1 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  On ){.      if( 
275c2 69 73 4f 75 74 65 72 20 29 20 73 65 74 4a 6f 69  isOuter ) setJoi
275c3 6e 45 78 70 72 28 70 52 69 67 68 74 2d 3e 70 4f  nExpr(pRight->pO
275c4 6e 2c 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73  n, pRight->iCurs
275c5 6f 72 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 57  or);.      p->pW
275c6 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78  here = sqlite3Ex
275c7 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  prAnd(pParse->db
275c8 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 70 52 69  , p->pWhere, pRi
275c9 67 68 74 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 20  ght->pOn);.     
275ca 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 3d 20 30   pRight->pOn = 0
275cb 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
275cc 43 72 65 61 74 65 20 65 78 74 72 61 20 74 65 72  Create extra ter
275cd 6d 73 20 6f 6e 20 74 68 65 20 57 48 45 52 45 20  ms on the WHERE 
275ce 63 6c 61 75 73 65 20 66 6f 72 20 65 61 63 68 20  clause for each 
275cf 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 0a 20 20 20  column named.   
275d0 20 2a 2a 20 69 6e 20 74 68 65 20 55 53 49 4e 47   ** in the USING
275d1 20 63 6c 61 75 73 65 2e 20 20 45 78 61 6d 70 6c   clause.  Exampl
275d2 65 3a 20 49 66 20 74 68 65 20 74 77 6f 20 74 61  e: If the two ta
275d3 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65  bles to be joine
275d4 64 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 41 20  d are .    ** A 
275d5 61 6e 64 20 42 20 61 6e 64 20 74 68 65 20 55 53  and B and the US
275d6 49 4e 47 20 63 6c 61 75 73 65 20 6e 61 6d 65 73  ING clause names
275d7 20 58 2c 20 59 2c 20 61 6e 64 20 5a 2c 20 74 68   X, Y, and Z, th
275d8 65 6e 20 61 64 64 20 74 68 69 73 0a 20 20 20 20  en add this.    
275d9 2a 2a 20 74 6f 20 74 68 65 20 57 48 45 52 45 20  ** to the WHERE 
275da 63 6c 61 75 73 65 3a 20 20 20 20 41 2e 58 3d 42  clause:    A.X=B
275db 2e 58 20 41 4e 44 20 41 2e 59 3d 42 2e 59 20 41  .X AND A.Y=B.Y A
275dc 4e 44 20 41 2e 5a 3d 42 2e 5a 0a 20 20 20 20 2a  ND A.Z=B.Z.    *
275dd 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f  * Report an erro
275de 72 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20  r if any column 
275df 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68 65  mentioned in the
275e0 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 69 73   USING clause is
275e1 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6e 74  .    ** not cont
275e2 61 69 6e 65 64 20 69 6e 20 62 6f 74 68 20 74 61  ained in both ta
275e3 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65  bles to be joine
275e4 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
275e5 28 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67  ( pRight->pUsing
275e6 20 29 7b 0a 20 20 20 20 20 20 49 64 4c 69 73 74   ){.      IdList
275e7 20 2a 70 4c 69 73 74 20 3d 20 70 52 69 67 68 74   *pList = pRight
275e8 2d 3e 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20  ->pUsing;.      
275e9 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 69 73 74  for(j=0; j<pList
275ea 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ->nId; j++){.   
275eb 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65       char *zName
275ec 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a   = pList->a[j].z
275ed 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66  Name;.        if
275ee 28 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c  ( columnIndex(pL
275ef 65 66 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3c 30  eftTab, zName)<0
275f0 20 7c 7c 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28   || columnIndex(
275f1 70 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65  pRightTab, zName
275f2 29 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  )<0 ){.         
275f3 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
275f4 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
275f5 20 6a 6f 69 6e 20 75 73 69 6e 67 20 63 6f 6c 75   join using colu
275f6 6d 6e 20 25 73 20 2d 20 63 6f 6c 75 6d 6e 20 22  mn %s - column "
275f7 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 6e 6f  .            "no
275f8 74 20 70 72 65 73 65 6e 74 20 69 6e 20 62 6f 74  t present in bot
275f9 68 20 74 61 62 6c 65 73 22 2c 20 7a 4e 61 6d 65  h tables", zName
275fa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
275fb 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  urn 1;.        }
275fc 0a 20 20 20 20 20 20 20 20 61 64 64 57 68 65 72  .        addWher
275fd 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20 7a 4e  eTerm(pParse, zN
275fe 61 6d 65 2c 20 70 4c 65 66 74 54 61 62 2c 20 70  ame, pLeftTab, p
275ff 4c 65 66 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a 20  Left->zAlias, . 
27600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27601 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68             pRigh
27602 74 54 61 62 2c 20 70 52 69 67 68 74 2d 3e 7a 41  tTab, pRight->zA
27603 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lias,.          
27604 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27605 20 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f    pRight->iCurso
27606 72 2c 20 26 70 2d 3e 70 57 68 65 72 65 2c 20 69  r, &p->pWhere, i
27607 73 4f 75 74 65 72 29 3b 0a 20 20 20 20 20 20 7d  sOuter);.      }
27608 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
27609 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
2760a 49 6e 73 65 72 74 20 63 6f 64 65 20 69 6e 74 6f  Insert code into
2760b 20 22 76 22 20 74 68 61 74 20 77 69 6c 6c 20 70   "v" that will p
2760c 75 73 68 20 74 68 65 20 72 65 63 6f 72 64 20 6f  ush the record o
2760d 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  n the top of the
2760e 0a 2a 2a 20 73 74 61 63 6b 20 69 6e 74 6f 20 74  .** stack into t
2760f 68 65 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73 74  he sorter..*/.st
27610 61 74 69 63 20 76 6f 69 64 20 70 75 73 68 4f 6e  atic void pushOn
27611 74 6f 53 6f 72 74 65 72 28 0a 20 20 50 61 72 73  toSorter(.  Pars
27612 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
27613 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
27614 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
27615 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
27616 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
27617 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c   clause */.  Sel
27618 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
27619 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65      /* The whole
2761a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2761b 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61  t */.  int regDa
2761c 74 61 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ta            /*
2761d 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
2761e 67 20 64 61 74 61 20 74 6f 20 62 65 20 73 6f 72  g data to be sor
2761f 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  ted */.){.  Vdbe
27620 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
27621 64 62 65 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72  dbe;.  int nExpr
27622 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   = pOrderBy->nEx
27623 70 72 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 73  pr;.  int regBas
27624 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  e = sqlite3GetTe
27625 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
27626 6e 45 78 70 72 2b 32 29 3b 0a 20 20 69 6e 74 20  nExpr+2);.  int 
27627 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69  regRecord = sqli
27628 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
27629 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
2762a 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
2762b 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
2762c 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
2762d 74 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  t(pParse, pOrder
2762e 42 79 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b  By, regBase, 0);
2762f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27630 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65  dOp2(v, OP_Seque
27631 6e 63 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69  nce, pOrderBy->i
27632 45 43 75 72 73 6f 72 2c 20 72 65 67 42 61 73 65  ECursor, regBase
27633 2b 6e 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74  +nExpr);.  sqlit
27634 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70  e3ExprCodeMove(p
27635 50 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c 20  Parse, regData, 
27636 72 65 67 42 61 73 65 2b 6e 45 78 70 72 2b 31 2c  regBase+nExpr+1,
27637 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   1);.  sqlite3Vd
27638 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
27639 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61  akeRecord, regBa
2763a 73 65 2c 20 6e 45 78 70 72 20 2b 20 32 2c 20 72  se, nExpr + 2, r
2763b 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c  egRecord);.  sql
2763c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2763d 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
2763e 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73  pOrderBy->iECurs
2763f 6f 72 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a  or, regRecord);.
27640 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
27641 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
27642 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71  regRecord);.  sq
27643 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
27644 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
27645 67 42 61 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b  gBase, nExpr+2);
27646 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
27647 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 69 6e  iLimit ){.    in
27648 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 0a  t addr1, addr2;.
27649 20 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 0a      int iLimit;.
2764a 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d      if( pSelect-
2764b 3e 69 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  >iOffset ){.    
2764c 20 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65    iLimit = pSele
2764d 63 74 2d 3e 69 4f 66 66 73 65 74 2b 31 3b 0a 20  ct->iOffset+1;. 
2764e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2764f 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74  iLimit = pSelect
27650 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 7d 0a  ->iLimit;.    }.
27651 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
27652 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
27653 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d   OP_IfZero, iLim
27654 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  it);.    sqlite3
27655 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27656 5f 41 64 64 49 6d 6d 2c 20 69 4c 69 6d 69 74 2c  _AddImm, iLimit,
27657 20 2d 31 29 3b 0a 20 20 20 20 61 64 64 72 32 20   -1);.    addr2 
27658 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
27659 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b  Op0(v, OP_Goto);
2765a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2765b 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2765c 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2765d 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2765e 4c 61 73 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e  Last, pOrderBy->
2765f 69 45 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73  iECursor);.    s
27660 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
27661 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70  (v, OP_Delete, p
27662 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
27663 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  r);.    sqlite3V
27664 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
27665 64 64 72 32 29 3b 0a 20 20 20 20 70 53 65 6c 65  ddr2);.    pSele
27666 63 74 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a  ct->iLimit = 0;.
27667 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64    }.}../*.** Add
27668 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
27669 6e 74 20 74 68 65 20 4f 46 46 53 45 54 0a 2a 2f  nt the OFFSET.*/
2766a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
2766b 65 4f 66 66 73 65 74 28 0a 20 20 56 64 62 65 20  eOffset(.  Vdbe 
2766c 2a 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  *v,          /* 
2766d 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
2766e 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20  to this VM */.  
2766f 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
27670 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
27671 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
27672 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  coded */.  int i
27673 43 6f 6e 74 69 6e 75 65 20 20 20 20 20 2f 2a 20  Continue     /* 
27674 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69  Jump here to ski
27675 70 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 65  p the current re
27676 63 6f 72 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  cord */.){.  if(
27677 20 70 2d 3e 69 4f 66 66 73 65 74 20 26 26 20 69   p->iOffset && i
27678 43 6f 6e 74 69 6e 75 65 21 3d 30 20 29 7b 0a 20  Continue!=0 ){. 
27679 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
2767a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2767b 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
2767c 20 70 2d 3e 69 4f 66 66 73 65 74 2c 20 2d 31 29   p->iOffset, -1)
2767d 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ;.    addr = sql
2767e 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2767f 2c 20 4f 50 5f 49 66 4e 65 67 2c 20 70 2d 3e 69  , OP_IfNeg, p->i
27680 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c  Offset);.    sql
27681 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27682 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43  , OP_Goto, 0, iC
27683 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 56 64  ontinue);.    Vd
27684 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73  beComment((v, "s
27685 6b 69 70 20 4f 46 46 53 45 54 20 72 65 63 6f 72  kip OFFSET recor
27686 64 73 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  ds"));.    sqlit
27687 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
27688 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a  , addr);.  }.}..
27689 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64 65 20 74  /*.** Add code t
2768a 68 61 74 20 77 69 6c 6c 20 63 68 65 63 6b 20 74  hat will check t
2768b 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
2768c 4e 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  N registers star
2768d 74 69 6e 67 20 61 74 20 69 4d 65 6d 0a 2a 2a 20  ting at iMem.** 
2768e 66 6f 72 6d 20 61 20 64 69 73 74 69 6e 63 74 20  form a distinct 
2768f 65 6e 74 72 79 2e 20 20 69 54 61 62 20 69 73 20  entry.  iTab is 
27690 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  a sorting index 
27691 74 68 61 74 20 68 6f 6c 64 73 20 70 72 65 76 69  that holds previ
27692 6f 75 73 6c 79 0a 2a 2a 20 73 65 65 6e 20 63 6f  ously.** seen co
27693 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 74 68  mbinations of th
27694 65 20 4e 20 76 61 6c 75 65 73 2e 20 20 41 20 6e  e N values.  A n
27695 65 77 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65  ew entry is made
27696 20 69 6e 20 69 54 61 62 0a 2a 2a 20 69 66 20 74   in iTab.** if t
27697 68 65 20 63 75 72 72 65 6e 74 20 4e 20 76 61 6c  he current N val
27698 75 65 73 20 61 72 65 20 6e 65 77 2e 0a 2a 2a 0a  ues are new..**.
27699 2a 2a 20 41 20 6a 75 6d 70 20 74 6f 20 61 64 64  ** A jump to add
2769a 72 52 65 70 65 61 74 20 69 73 20 6d 61 64 65 20  rRepeat is made 
2769b 61 6e 64 20 74 68 65 20 4e 2b 31 20 76 61 6c 75  and the N+1 valu
2769c 65 73 20 61 72 65 20 70 6f 70 70 65 64 20 66 72  es are popped fr
2769d 6f 6d 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20  om the.** stack 
2769e 69 66 20 74 68 65 20 74 6f 70 20 4e 20 65 6c 65  if the top N ele
2769f 6d 65 6e 74 73 20 61 72 65 20 6e 6f 74 20 64 69  ments are not di
276a0 73 74 69 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74 69  stinct..*/.stati
276a1 63 20 76 6f 69 64 20 63 6f 64 65 44 69 73 74 69  c void codeDisti
276a2 6e 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  nct(.  Parse *pP
276a3 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72  arse,     /* Par
276a4 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65  sing and code ge
276a5 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74  nerating context
276a6 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20   */.  int iTab, 
276a7 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 6f           /* A so
276a8 72 74 69 6e 67 20 69 6e 64 65 78 20 75 73 65 64  rting index used
276a9 20 74 6f 20 74 65 73 74 20 66 6f 72 20 64 69 73   to test for dis
276aa 74 69 6e 63 74 6e 65 73 73 20 2a 2f 0a 20 20 69  tinctness */.  i
276ab 6e 74 20 61 64 64 72 52 65 70 65 61 74 2c 20 20  nt addrRepeat,  
276ac 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72    /* Jump to her
276ad 65 20 69 66 20 6e 6f 74 20 64 69 73 74 69 6e 63  e if not distinc
276ae 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20  t */.  int N,   
276af 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
276b0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
276b1 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20  */.  int iMem   
276b2 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
276b3 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20   element */.){. 
276b4 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20   Vdbe *v;.  int 
276b5 72 31 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73  r1;..  v = pPars
276b6 65 2d 3e 70 56 64 62 65 3b 0a 20 20 72 31 20 3d  e->pVdbe;.  r1 =
276b7 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
276b8 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
276b9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
276ba 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
276bb 2c 20 69 4d 65 6d 2c 20 4e 2c 20 72 31 29 3b 0a  , iMem, N, r1);.
276bc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
276bd 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Op3(v, OP_Found,
276be 20 69 54 61 62 2c 20 61 64 64 72 52 65 70 65 61   iTab, addrRepea
276bf 74 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65  t, r1);.  sqlite
276c0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
276c1 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 54 61  P_IdxInsert, iTa
276c2 62 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65  b, r1);.  sqlite
276c3 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
276c4 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a  pParse, r1);.}..
276c5 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
276c6 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
276c7 77 68 65 6e 20 61 20 53 45 4c 45 43 54 20 69 73  when a SELECT is
276c8 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 73   used within a s
276c9 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  ubexpression.** 
276ca 28 65 78 61 6d 70 6c 65 3a 20 20 22 61 20 49 4e  (example:  "a IN
276cb 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20   (SELECT * FROM 
276cc 74 61 62 6c 65 29 22 29 20 62 75 74 20 69 74 20  table)") but it 
276cd 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 31 20  has more than 1 
276ce 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e  result.** column
276cf 2e 20 20 57 65 20 64 6f 20 74 68 69 73 20 69 6e  .  We do this in
276d0 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 62 65   a subroutine be
276d1 63 61 75 73 65 20 74 68 65 20 65 72 72 6f 72 20  cause the error 
276d2 6f 63 63 75 72 73 20 69 6e 20 6d 75 6c 74 69 70  occurs in multip
276d3 6c 65 0a 2a 2a 20 70 6c 61 63 65 73 2e 0a 2a 2f  le.** places..*/
276d4 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63  .static int chec
276d5 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53  kForMultiColumnS
276d6 65 6c 65 63 74 45 72 72 6f 72 28 0a 20 20 50 61  electError(.  Pa
276d7 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
276d8 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
276d9 65 78 74 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ext. */.  Select
276da 44 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20 2f  Dest *pDest,   /
276db 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 6f 66  * Destination of
276dc 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20   SELECT results 
276dd 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 70 72 20 20  */.  int nExpr  
276de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
276df 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f  ber of result co
276e0 6c 75 6d 6e 73 20 72 65 74 75 72 6e 65 64 20 62  lumns returned b
276e1 79 20 53 45 4c 45 43 54 20 2a 2f 0a 29 7b 0a 20  y SELECT */.){. 
276e2 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65   int eDest = pDe
276e3 73 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69 66 28  st->eDest;.  if(
276e4 20 6e 45 78 70 72 3e 31 20 26 26 20 28 65 44 65   nExpr>1 && (eDe
276e5 73 74 3d 3d 53 52 54 5f 4d 65 6d 20 7c 7c 20 65  st==SRT_Mem || e
276e6 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 29 20 29  Dest==SRT_Set) )
276e7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
276e8 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f  orMsg(pParse, "o
276e9 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 65 73  nly a single res
276ea 75 6c 74 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20  ult allowed for 
276eb 22 0a 20 20 20 20 20 20 20 22 61 20 53 45 4c 45  ".       "a SELE
276ec 43 54 20 74 68 61 74 20 69 73 20 70 61 72 74 20  CT that is part 
276ed 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
276ee 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  ");.    return 1
276ef 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
276f0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  eturn 0;.  }.}..
276f1 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
276f2 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  ne generates the
276f3 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 69 6e   code for the in
276f4 73 69 64 65 20 6f 66 20 74 68 65 20 69 6e 6e 65  side of the inne
276f5 72 20 6c 6f 6f 70 0a 2a 2a 20 6f 66 20 61 20 53  r loop.** of a S
276f6 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ELECT..**.** If 
276f7 73 72 63 54 61 62 20 61 6e 64 20 6e 43 6f 6c 75  srcTab and nColu
276f8 6d 6e 20 61 72 65 20 62 6f 74 68 20 7a 65 72 6f  mn are both zero
276f9 2c 20 74 68 65 6e 20 74 68 65 20 70 45 4c 69 73  , then the pELis
276fa 74 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a  t expressions.**
276fb 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 69   are evaluated i
276fc 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 74  n order to get t
276fd 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73  he data for this
276fe 20 72 6f 77 2e 20 20 49 66 20 6e 43 6f 6c 75 6d   row.  If nColum
276ff 6e 3e 30 0a 2a 2a 20 74 68 65 6e 20 64 61 74 61  n>0.** then data
27700 20 69 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20   is pulled from 
27701 73 72 63 54 61 62 20 61 6e 64 20 70 45 4c 69 73  srcTab and pELis
27702 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74  t is used only t
27703 6f 20 67 65 74 20 74 68 65 0a 2a 2a 20 64 61 74  o get the.** dat
27704 61 74 79 70 65 73 20 66 6f 72 20 65 61 63 68 20  atypes for each 
27705 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69  column..*/.stati
27706 63 20 76 6f 69 64 20 73 65 6c 65 63 74 49 6e 6e  c void selectInn
27707 65 72 4c 6f 6f 70 28 0a 20 20 50 61 72 73 65 20  erLoop(.  Parse 
27708 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
27709 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
2770a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
2770b 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ect *p,         
2770c 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70       /* The comp
2770d 6c 65 74 65 20 73 65 6c 65 63 74 20 73 74 61 74  lete select stat
2770e 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65  ement being code
2770f 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  d */.  ExprList 
27710 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f  *pEList,       /
27711 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73  * List of values
27712 20 62 65 69 6e 67 20 65 78 74 72 61 63 74 65 64   being extracted
27713 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63 54 61 62   */.  int srcTab
27714 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
27715 20 50 75 6c 6c 20 64 61 74 61 20 66 72 6f 6d 20   Pull data from 
27716 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
27717 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20  int nColumn,    
27718 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
27719 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
2771a 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65  the source table
2771b 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2771c 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a  pOrderBy,     /*
2771d 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 6f   If not NULL, so
2771e 72 74 20 72 65 73 75 6c 74 73 20 75 73 69 6e 67  rt results using
2771f 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 69   this key */.  i
27720 6e 74 20 64 69 73 74 69 6e 63 74 2c 20 20 20 20  nt distinct,    
27721 20 20 20 20 20 20 20 2f 2a 20 49 66 20 3e 3d 30         /* If >=0
27722 2c 20 6d 61 6b 65 20 73 75 72 65 20 72 65 73 75  , make sure resu
27723 6c 74 73 20 61 72 65 20 64 69 73 74 69 6e 63 74  lts are distinct
27724 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
27725 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f 2a   *pDest,      /*
27726 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20   How to dispose 
27727 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a  of the results *
27728 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75  /.  int iContinu
27729 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a  e,          /* J
2772a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74  ump here to cont
2772b 69 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20 72  inue with next r
2772c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65  ow */.  int iBre
2772d 61 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ak              
2772e 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
2772f 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
27730 20 69 6e 6e 65 72 20 6c 6f 6f 70 20 2a 2f 0a 29   inner loop */.)
27731 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
27732 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
27733 6e 74 20 69 3b 0a 20 20 69 6e 74 20 68 61 73 44  nt i;.  int hasD
27734 69 73 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20  istinct;        
27735 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 44  /* True if the D
27736 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
27737 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
27738 69 6e 74 20 72 65 67 52 65 73 75 6c 74 3b 20 20  int regResult;  
27739 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
2773a 74 61 72 74 20 6f 66 20 6d 65 6d 6f 72 79 20 68  tart of memory h
2773b 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65  olding result se
2773c 74 20 2a 2f 0a 20 20 69 6e 74 20 65 44 65 73 74  t */.  int eDest
2773d 20 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b   = pDest->eDest;
2773e 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73     /* How to dis
2773f 70 6f 73 65 20 6f 66 20 72 65 73 75 6c 74 73 20  pose of results 
27740 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 3d  */.  int iParm =
27741 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 3b 20 20   pDest->iParm;  
27742 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65   /* First argume
27743 6e 74 20 74 6f 20 64 69 73 70 6f 73 61 6c 20 6d  nt to disposal m
27744 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ethod */.  int n
27745 52 65 73 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20  ResultCol;      
27746 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
27747 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
27748 6e 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ns */..  assert(
27749 20 76 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45   v );.  if( NEVE
2774a 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  R(v==0) ) return
2774b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69  ;.  assert( pELi
2774c 73 74 21 3d 30 20 29 3b 0a 20 20 68 61 73 44 69  st!=0 );.  hasDi
2774d 73 74 69 6e 63 74 20 3d 20 64 69 73 74 69 6e 63  stinct = distinc
2774e 74 3e 3d 30 3b 0a 20 20 69 66 28 20 70 4f 72 64  t>=0;.  if( pOrd
2774f 65 72 42 79 3d 3d 30 20 26 26 20 21 68 61 73 44  erBy==0 && !hasD
27750 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 63  istinct ){.    c
27751 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20  odeOffset(v, p, 
27752 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 7d 0a  iContinue);.  }.
27753 0a 20 20 2f 2a 20 50 75 6c 6c 20 74 68 65 20 72  .  /* Pull the r
27754 65 71 75 65 73 74 65 64 20 63 6f 6c 75 6d 6e 73  equested columns
27755 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 43 6f  ..  */.  if( nCo
27756 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 6e 52  lumn>0 ){.    nR
27757 65 73 75 6c 74 43 6f 6c 20 3d 20 6e 43 6f 6c 75  esultCol = nColu
27758 6d 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  mn;.  }else{.   
27759 20 6e 52 65 73 75 6c 74 43 6f 6c 20 3d 20 70 45   nResultCol = pE
2775a 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d  List->nExpr;.  }
2775b 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 4d  .  if( pDest->iM
2775c 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 65  em==0 ){.    pDe
2775d 73 74 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73  st->iMem = pPars
2775e 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70  e->nMem+1;.    p
2775f 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 6e 52 65  Dest->nMem = nRe
27760 73 75 6c 74 43 6f 6c 3b 0a 20 20 20 20 70 50 61  sultCol;.    pPa
27761 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65  rse->nMem += nRe
27762 73 75 6c 74 43 6f 6c 3b 0a 20 20 7d 65 6c 73 65  sultCol;.  }else
27763 7b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  { .    assert( p
27764 44 65 73 74 2d 3e 6e 4d 65 6d 3d 3d 6e 52 65 73  Dest->nMem==nRes
27765 75 6c 74 43 6f 6c 20 29 3b 0a 20 20 7d 0a 20 20  ultCol );.  }.  
27766 72 65 67 52 65 73 75 6c 74 20 3d 20 70 44 65 73  regResult = pDes
27767 74 2d 3e 69 4d 65 6d 3b 0a 20 20 69 66 28 20 6e  t->iMem;.  if( n
27768 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20  Column>0 ){.    
27769 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75  for(i=0; i<nColu
2776a 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
2776b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2776c 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2776d 73 72 63 54 61 62 2c 20 69 2c 20 72 65 67 52 65  srcTab, i, regRe
2776e 73 75 6c 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20  sult+i);.    }. 
2776f 20 7d 65 6c 73 65 20 69 66 28 20 65 44 65 73 74   }else if( eDest
27770 21 3d 53 52 54 5f 45 78 69 73 74 73 20 29 7b 0a  !=SRT_Exists ){.
27771 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65      /* If the de
27772 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20  stination is an 
27773 45 58 49 53 54 53 28 2e 2e 2e 29 20 65 78 70 72  EXISTS(...) expr
27774 65 73 73 69 6f 6e 2c 20 74 68 65 20 61 63 74 75  ession, the actu
27775 61 6c 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73  al.    ** values
27776 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
27777 20 53 45 4c 45 43 54 20 61 72 65 20 6e 6f 74 20   SELECT are not 
27778 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f  required..    */
27779 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2777a 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
2777b 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  e);.    sqlite3E
2777c 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
2777d 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20  pParse, pEList, 
2777e 72 65 67 52 65 73 75 6c 74 2c 20 65 44 65 73 74  regResult, eDest
2777f 3d 3d 53 52 54 5f 4f 75 74 70 75 74 29 3b 0a 20  ==SRT_Output);. 
27780 20 7d 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 6e   }.  nColumn = n
27781 52 65 73 75 6c 74 43 6f 6c 3b 0a 0a 20 20 2f 2a  ResultCol;..  /*
27782 20 49 66 20 74 68 65 20 44 49 53 54 49 4e 43 54   If the DISTINCT
27783 20 6b 65 79 77 6f 72 64 20 77 61 73 20 70 72 65   keyword was pre
27784 73 65 6e 74 20 6f 6e 20 74 68 65 20 53 45 4c 45  sent on the SELE
27785 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a  CT statement.  *
27786 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 77 20 68  * and this row h
27787 61 73 20 62 65 65 6e 20 73 65 65 6e 20 62 65 66  as been seen bef
27788 6f 72 65 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74  ore, then do not
27789 20 6d 61 6b 65 20 74 68 69 73 20 72 6f 77 0a 20   make this row. 
2778a 20 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20   ** part of the 
2778b 72 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 69  result..  */.  i
2778c 66 28 20 68 61 73 44 69 73 74 69 6e 63 74 20 29  f( hasDistinct )
2778d 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  {.    assert( pE
2778e 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 61  List!=0 );.    a
2778f 73 73 65 72 74 28 20 70 45 4c 69 73 74 2d 3e 6e  ssert( pEList->n
27790 45 78 70 72 3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b  Expr==nColumn );
27791 0a 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63  .    codeDistinc
27792 74 28 70 50 61 72 73 65 2c 20 64 69 73 74 69 6e  t(pParse, distin
27793 63 74 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6e  ct, iContinue, n
27794 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 65 73 75 6c  Column, regResul
27795 74 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 72 64  t);.    if( pOrd
27796 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  erBy==0 ){.     
27797 20 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70   codeOffset(v, p
27798 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20  , iContinue);.  
27799 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 63    }.  }..  if( c
2779a 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75  heckForMultiColu
2779b 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72 28 70 50  mnSelectError(pP
2779c 61 72 73 65 2c 20 70 44 65 73 74 2c 20 70 45 4c  arse, pDest, pEL
2779d 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20  ist->nExpr) ){. 
2779e 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
2779f 20 20 73 77 69 74 63 68 28 20 65 44 65 73 74 20    switch( eDest 
277a0 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69  ){.    /* In thi
277a1 73 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 65 61  s mode, write ea
277a2 63 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 20  ch query result 
277a3 74 6f 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68  to the key of th
277a4 65 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20  e temporary.    
277a5 2a 2a 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a  ** table iParm..
277a6 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
277a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
277a8 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20 20 20 63  UND_SELECT.    c
277a9 61 73 65 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b  ase SRT_Union: {
277aa 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a 20  .      int r1;. 
277ab 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
277ac 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
277ad 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  se);.      sqlit
277ae 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
277af 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
277b0 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d  egResult, nColum
277b1 6e 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  n, r1);.      sq
277b2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
277b3 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
277b4 20 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20   iParm, r1);.   
277b5 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
277b6 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
277b7 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61   r1);.      brea
277b8 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
277b9 20 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65 63   Construct a rec
277ba 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 71 75 65  ord from the que
277bb 72 79 20 72 65 73 75 6c 74 2c 20 62 75 74 20 69  ry result, but i
277bc 6e 73 74 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a  nstead of.    **
277bd 20 73 61 76 69 6e 67 20 74 68 61 74 20 72 65 63   saving that rec
277be 6f 72 64 2c 20 75 73 65 20 69 74 20 61 73 20 61  ord, use it as a
277bf 20 6b 65 79 20 74 6f 20 64 65 6c 65 74 65 20 65   key to delete e
277c0 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 0a 20 20 20  lements from.   
277c1 20 2a 2a 20 74 68 65 20 74 65 6d 70 6f 72 61 72   ** the temporar
277c2 79 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20  y table iParm.. 
277c3 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
277c4 52 54 5f 45 78 63 65 70 74 3a 20 7b 0a 20 20 20  RT_Except: {.   
277c5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
277c6 64 4f 70 33 28 76 2c 20 4f 50 5f 49 64 78 44 65  dOp3(v, OP_IdxDe
277c7 6c 65 74 65 2c 20 69 50 61 72 6d 2c 20 72 65 67  lete, iParm, reg
277c8 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29  Result, nColumn)
277c9 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
277ca 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
277cb 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65   /* Store the re
277cc 73 75 6c 74 20 61 73 20 64 61 74 61 20 75 73 69  sult as data usi
277cd 6e 67 20 61 20 75 6e 69 71 75 65 20 6b 65 79 2e  ng a unique key.
277ce 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
277cf 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20   SRT_Table:.    
277d0 63 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54 61  case SRT_EphemTa
277d1 62 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  b: {.      int r
277d2 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
277d3 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
277d4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
277d5 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
277d6 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75  eRecord, regResu
277d7 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29  lt, nColumn, r1)
277d8 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64  ;.      if( pOrd
277d9 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20  erBy ){.        
277da 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70  pushOntoSorter(p
277db 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c  Parse, pOrderBy,
277dc 20 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 7d   p, r1);.      }
277dd 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
277de 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65  t r2 = sqlite3Ge
277df 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
277e0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
277e1 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
277e2 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 50 61 72  P_NewRowid, iPar
277e3 6d 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20  m, r2);.        
277e4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
277e5 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
277e6 69 50 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b 0a  iParm, r1, r2);.
277e7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
277e8 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
277e9 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
277ea 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
277eb 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
277ec 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20  rse, r2);.      
277ed 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  }.      sqlite3R
277ee 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
277ef 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
277f0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23   break;.    }..#
277f1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
277f2 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20  IT_SUBQUERY.    
277f3 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 72 65  /* If we are cre
277f4 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f 72 20  ating a set for 
277f5 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53 45 4c  an "expr IN (SEL
277f6 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72  ECT ...)" constr
277f7 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e  uct,.    ** then
277f8 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65   there should be
277f9 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f   a single item o
277fa 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 57 72  n the stack.  Wr
277fb 69 74 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20  ite this.    ** 
277fc 69 74 65 6d 20 69 6e 74 6f 20 74 68 65 20 73 65  item into the se
277fd 74 20 74 61 62 6c 65 20 77 69 74 68 20 62 6f 67  t table with bog
277fe 75 73 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a  us data..    */.
277ff 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 65 74      case SRT_Set
27800 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
27801 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
27802 20 20 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74        p->affinit
27803 79 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61  y = sqlite3Compa
27804 72 65 41 66 66 69 6e 69 74 79 28 70 45 4c 69 73  reAffinity(pELis
27805 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 70  t->a[0].pExpr, p
27806 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 29 3b  Dest->affinity);
27807 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
27808 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  rBy ){.        /
27809 2a 20 41 74 20 66 69 72 73 74 20 67 6c 61 6e 63  * At first glanc
2780a 65 20 79 6f 75 20 77 6f 75 6c 64 20 74 68 69 6e  e you would thin
2780b 6b 20 77 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d  k we could optim
2780c 69 7a 65 20 6f 75 74 20 74 68 65 0a 20 20 20 20  ize out the.    
2780d 20 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20      ** ORDER BY 
2780e 69 6e 20 74 68 69 73 20 63 61 73 65 20 73 69 6e  in this case sin
2780f 63 65 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20  ce the order of 
27810 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 73  entries in the s
27811 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 6f  et.        ** do
27812 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 2e 20 20  es not matter.  
27813 42 75 74 20 74 68 65 72 65 20 6d 69 67 68 74 20  But there might 
27814 62 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73  be a LIMIT claus
27815 65 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 20 20  e, in which.    
27816 20 20 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20      ** case the 
27817 6f 72 64 65 72 20 64 6f 65 73 20 6d 61 74 74 65  order does matte
27818 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 73  r */.        pus
27819 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72  hOntoSorter(pPar
2781a 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c  se, pOrderBy, p,
2781b 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20   regResult);.   
2781c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2781d 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
2781e 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2781f 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
27820 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
27821 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
27822 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 31 2c 20  , regResult, 1, 
27823 72 31 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79  r1, &p->affinity
27824 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 1);.        sq
27825 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
27826 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
27827 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  rse, regResult, 
27828 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
27829 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2782a 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69   OP_IdxInsert, i
2782b 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20  Parm, r1);.     
2782c 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2782d 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2782e 20 72 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20   r1);.      }.  
2782f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
27830 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79 20  ..    /* If any 
27831 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68 65  row exist in the
27832 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65 63   result set, rec
27833 6f 72 64 20 74 68 61 74 20 66 61 63 74 20 61 6e  ord that fact an
27834 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a  d abort..    */.
27835 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 69      case SRT_Exi
27836 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  sts: {.      sql
27837 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27838 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
27839 20 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 2f   iParm);.      /
2783a 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75  * The LIMIT clau
2783b 73 65 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74  se will terminat
2783c 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75  e the loop for u
2783d 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b  s */.      break
2783e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2783f 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61  If this is a sca
27840 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20  lar select that 
27841 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78  is part of an ex
27842 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20  pression, then. 
27843 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20     ** store the 
27844 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61  results in the a
27845 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72  ppropriate memor
27846 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b  y cell and break
27847 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74   out.    ** of t
27848 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20  he scan loop..  
27849 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
2784a 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61  T_Mem: {.      a
2784b 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d  ssert( nColumn==
2784c 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  1 );.      if( p
2784d 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2784e 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65     pushOntoSorte
2784f 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  r(pParse, pOrder
27850 42 79 2c 20 70 2c 20 72 65 67 52 65 73 75 6c 74  By, p, regResult
27851 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
27852 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
27853 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72  xprCodeMove(pPar
27854 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 69  se, regResult, i
27855 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20  Parm, 1);.      
27856 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
27857 6c 61 75 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20  lause will jump 
27858 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  out of the loop 
27859 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20  for us */.      
2785a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
2785b 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23     }.#endif /* #
2785c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2785d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a  IT_SUBQUERY */..
2785e 20 20 20 20 2f 2a 20 53 65 6e 64 20 74 68 65 20      /* Send the 
2785f 64 61 74 61 20 74 6f 20 74 68 65 20 63 61 6c 6c  data to the call
27860 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6f 72  back function or
27861 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65   to a subroutine
27862 2e 20 20 49 6e 20 74 68 65 0a 20 20 20 20 2a 2a  .  In the.    **
27863 20 63 61 73 65 20 6f 66 20 61 20 73 75 62 72 6f   case of a subro
27864 75 74 69 6e 65 2c 20 74 68 65 20 73 75 62 72 6f  utine, the subro
27865 75 74 69 6e 65 20 69 74 73 65 6c 66 20 69 73 20  utine itself is 
27866 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a  responsible for.
27867 20 20 20 20 2a 2a 20 70 6f 70 70 69 6e 67 20 74      ** popping t
27868 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  he data from the
27869 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20   stack..    */. 
2786a 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f     case SRT_Coro
2786b 75 74 69 6e 65 3a 0a 20 20 20 20 63 61 73 65 20  utine:.    case 
2786c 53 52 54 5f 4f 75 74 70 75 74 3a 20 7b 0a 20 20  SRT_Output: {.  
2786d 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
2786e 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
2786f 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
27870 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
27871 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
27872 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
27873 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52  MakeRecord, regR
27874 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20  esult, nColumn, 
27875 72 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 73  r1);.        pus
27876 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72  hOntoSorter(pPar
27877 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c  se, pOrderBy, p,
27878 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71   r1);.        sq
27879 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2787a 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2787b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
2787c 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
2787d 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 20 20 20  utine ){.       
2787e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2787f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
27880 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20  pDest->iParm);. 
27881 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27882 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27883 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
27884 6c 74 52 6f 77 2c 20 72 65 67 52 65 73 75 6c 74  ltRow, regResult
27885 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
27886 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
27887 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
27888 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 52 65  ge(pParse, regRe
27889 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a  sult, nColumn);.
2788a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
2788b 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20  eak;.    }..#if 
2788c 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2788d 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20  OMIT_TRIGGER).  
2788e 20 20 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65    /* Discard the
2788f 20 72 65 73 75 6c 74 73 2e 20 20 54 68 69 73 20   results.  This 
27890 69 73 20 75 73 65 64 20 66 6f 72 20 53 45 4c 45  is used for SELE
27891 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  CT statements in
27892 73 69 64 65 0a 20 20 20 20 2a 2a 20 74 68 65 20  side.    ** the 
27893 62 6f 64 79 20 6f 66 20 61 20 54 52 49 47 47 45  body of a TRIGGE
27894 52 2e 20 20 54 68 65 20 70 75 72 70 6f 73 65 20  R.  The purpose 
27895 6f 66 20 73 75 63 68 20 73 65 6c 65 63 74 73 20  of such selects 
27896 69 73 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 2a  is to call.    *
27897 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66  * user-defined f
27898 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61  unctions that ha
27899 76 65 20 73 69 64 65 20 65 66 66 65 63 74 73 2e  ve side effects.
2789a 20 20 57 65 20 64 6f 20 6e 6f 74 20 63 61 72 65    We do not care
2789b 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20 74 68  .    ** about th
2789c 65 20 61 63 74 75 61 6c 20 72 65 73 75 6c 74 73  e actual results
2789d 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e 0a   of the select..
2789e 20 20 20 20 2a 2f 0a 20 20 20 20 64 65 66 61 75      */.    defau
2789f 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  lt: {.      asse
278a0 72 74 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 44  rt( eDest==SRT_D
278a1 69 73 63 61 72 64 20 29 3b 0a 20 20 20 20 20 20  iscard );.      
278a2 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
278a3 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75  dif.  }..  /* Ju
278a4 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  mp to the end of
278a5 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65   the loop if the
278a6 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63 68 65   LIMIT is reache
278a7 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  d..  */.  if( p-
278a8 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 61  >iLimit ){.    a
278a9 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 3d  ssert( pOrderBy=
278aa 3d 30 20 29 3b 20 20 2f 2a 20 49 66 20 74 68 65  =0 );  /* If the
278ab 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  re is an ORDER B
278ac 59 2c 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a 20  Y, the call to. 
278ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
278ae 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 75             ** pu
278af 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 29 20 77  shOntoSorter() w
278b0 6f 75 6c 64 20 68 61 76 65 20 63 6c 65 61 72 65  ould have cleare
278b1 64 20 70 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20  d p->iLimit */. 
278b2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
278b3 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
278b4 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 2d 31  m, p->iLimit, -1
278b5 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
278b6 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
278b7 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74  fZero, p->iLimit
278b8 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 7d 0a 7d  , iBreak);.  }.}
278b9 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e  ../*.** Given an
278ba 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
278bb 2c 20 67 65 6e 65 72 61 74 65 20 61 20 4b 65 79  , generate a Key
278bc 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  Info structure t
278bd 68 61 74 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74  hat records.** t
278be 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
278bf 75 65 6e 63 65 20 66 6f 72 20 65 61 63 68 20 65  uence for each e
278c0 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 61  xpression in tha
278c1 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  t expression lis
278c2 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
278c3 45 78 70 72 4c 69 73 74 20 69 73 20 61 6e 20 4f  ExprList is an O
278c4 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
278c5 20 42 59 20 63 6c 61 75 73 65 20 74 68 65 6e 20   BY clause then 
278c6 74 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a  the resulting.**
278c7 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
278c8 72 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  re is appropriat
278c9 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69  e for initializi
278ca 6e 67 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64  ng a virtual ind
278cb 65 78 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65  ex to.** impleme
278cc 6e 74 20 74 68 61 74 20 63 6c 61 75 73 65 2e 20  nt that clause. 
278cd 20 49 66 20 74 68 65 20 45 78 70 72 4c 69 73 74   If the ExprList
278ce 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 73   is the result s
278cf 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 0a 2a  et of a SELECT.*
278d0 2a 20 74 68 65 6e 20 74 68 65 20 4b 65 79 49 6e  * then the KeyIn
278d1 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
278d2 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
278d3 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 20 76  initializing a v
278d4 69 72 74 75 61 6c 0a 2a 2a 20 69 6e 64 65 78 20  irtual.** index 
278d5 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 44  to implement a D
278d6 49 53 54 49 4e 43 54 20 74 65 73 74 2e 0a 2a 2a  ISTINCT test..**
278d7 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
278d8 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  d the KeyInfo st
278d9 72 75 63 74 75 72 65 20 69 73 20 6f 62 74 61 69  ructure is obtai
278da 6e 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 20  n from malloc.  
278db 54 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66  The calling.** f
278dc 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f  unction is respo
278dd 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 65 69 6e  nsible for seein
278de 67 20 74 68 61 74 20 74 68 69 73 20 73 74 72 75  g that this stru
278df 63 74 75 72 65 20 69 73 20 65 76 65 6e 74 75 61  cture is eventua
278e0 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 2e 20 20 41  lly.** freed.  A
278e1 64 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  dd the KeyInfo s
278e2 74 72 75 63 74 75 72 65 20 74 6f 20 74 68 65 20  tructure to the 
278e3 50 34 20 66 69 65 6c 64 20 6f 66 20 61 6e 20 6f  P4 field of an o
278e4 70 63 6f 64 65 20 75 73 69 6e 67 0a 2a 2a 20 50  pcode using.** P
278e5 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
278e6 46 20 69 73 20 74 68 65 20 75 73 75 61 6c 20 77  F is the usual w
278e7 61 79 20 6f 66 20 64 65 61 6c 69 6e 67 20 77 69  ay of dealing wi
278e8 74 68 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74  th this..*/.stat
278e9 69 63 20 4b 65 79 49 6e 66 6f 20 2a 6b 65 79 49  ic KeyInfo *keyI
278ea 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  nfoFromExprList(
278eb 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
278ec 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  xprList *pList){
278ed 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
278ee 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
278ef 6e 74 20 6e 45 78 70 72 3b 0a 20 20 4b 65 79 49  nt nExpr;.  KeyI
278f0 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 73 74  nfo *pInfo;.  st
278f1 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
278f2 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74  em *pItem;.  int
278f3 20 69 3b 0a 0a 20 20 6e 45 78 70 72 20 3d 20 70   i;..  nExpr = p
278f4 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70  List->nExpr;.  p
278f5 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62  Info = sqlite3Db
278f6 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
278f7 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 20 2b 20  izeof(*pInfo) + 
278f8 6e 45 78 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f  nExpr*(sizeof(Co
278f9 6c 6c 53 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20  llSeq*)+1) );.  
278fa 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20  if( pInfo ){.   
278fb 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64   pInfo->aSortOrd
278fc 65 72 20 3d 20 28 75 38 2a 29 26 70 49 6e 66 6f  er = (u8*)&pInfo
278fd 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a  ->aColl[nExpr];.
278fe 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 46 69 65 6c      pInfo->nFiel
278ff 64 20 3d 20 28 75 31 36 29 6e 45 78 70 72 3b 0a  d = (u16)nExpr;.
27900 20 20 20 20 70 49 6e 66 6f 2d 3e 65 6e 63 20 3d      pInfo->enc =
27901 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70 49   ENC(db);.    pI
27902 6e 66 6f 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20  nfo->db = db;.  
27903 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d    for(i=0, pItem
27904 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 6e 45 78  =pList->a; i<nEx
27905 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
27906 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  ){.      CollSeq
27907 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70   *pColl;.      p
27908 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
27909 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
2790a 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b  , pItem->pExpr);
2790b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c  .      if( !pCol
2790c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f  l ){.        pCo
2790d 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
2790e 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
2790f 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69    pInfo->aColl[i
27910 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  ] = pColl;.     
27911 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64   pInfo->aSortOrd
27912 65 72 5b 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 73  er[i] = pItem->s
27913 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a  ortOrder;.    }.
27914 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 49 6e    }.  return pIn
27915 66 6f 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66  fo;.}.../*.** If
27916 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 20   the inner loop 
27917 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 75 73  was generated us
27918 69 6e 67 20 61 20 6e 6f 6e 2d 6e 75 6c 6c 20 70  ing a non-null p
27919 4f 72 64 65 72 42 79 20 61 72 67 75 6d 65 6e 74  OrderBy argument
2791a 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65  ,.** then the re
2791b 73 75 6c 74 73 20 77 65 72 65 20 70 6c 61 63 65  sults were place
2791c 64 20 69 6e 20 61 20 73 6f 72 74 65 72 2e 20 20  d in a sorter.  
2791d 41 66 74 65 72 20 74 68 65 20 6c 6f 6f 70 20 69  After the loop i
2791e 73 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20  s terminated.** 
2791f 77 65 20 6e 65 65 64 20 74 6f 20 72 75 6e 20 74  we need to run t
27920 68 65 20 73 6f 72 74 65 72 20 61 6e 64 20 6f 75  he sorter and ou
27921 74 70 75 74 20 74 68 65 20 72 65 73 75 6c 74 73  tput the results
27922 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
27923 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  .** routine gene
27924 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 6e  rates the code n
27925 65 65 64 65 64 20 74 6f 20 64 6f 20 74 68 61 74  eeded to do that
27926 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
27927 20 67 65 6e 65 72 61 74 65 53 6f 72 74 54 61 69   generateSortTai
27928 6c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  l(.  Parse *pPar
27929 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  se,    /* Parsin
2792a 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
2792b 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
2792c 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
2792d 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 56 64  tatement */.  Vd
2792e 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20 20  be *v,          
2792f 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
27930 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45 20   into this VDBE 
27931 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e  */.  int nColumn
27932 2c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ,      /* Number
27933 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64   of columns of d
27934 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  ata */.  SelectD
27935 65 73 74 20 2a 70 44 65 73 74 20 2f 2a 20 57 72  est *pDest /* Wr
27936 69 74 65 20 74 68 65 20 73 6f 72 74 65 64 20 72  ite the sorted r
27937 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29  esults here */.)
27938 7b 0a 20 20 69 6e 74 20 61 64 64 72 42 72 65 61  {.  int addrBrea
27939 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  k = sqlite3VdbeM
2793a 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 20 20  akeLabel(v);    
2793b 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
2793c 20 65 78 69 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20   exit loop */.  
2793d 69 6e 74 20 61 64 64 72 43 6f 6e 74 69 6e 75 65  int addrContinue
2793e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2793f 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 2f 2a 20  keLabel(v);  /* 
27940 4a 75 6d 70 20 68 65 72 65 20 66 6f 72 20 6e 65  Jump here for ne
27941 78 74 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e  xt cycle */.  in
27942 74 20 61 64 64 72 3b 0a 20 20 69 6e 74 20 69 54  t addr;.  int iT
27943 61 62 3b 0a 20 20 69 6e 74 20 70 73 65 75 64 6f  ab;.  int pseudo
27944 54 61 62 20 3d 20 30 3b 0a 20 20 45 78 70 72 4c  Tab = 0;.  ExprL
27945 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20 3d 20  ist *pOrderBy = 
27946 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 0a 20 20  p->pOrderBy;..  
27947 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65 73  int eDest = pDes
27948 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69 6e 74 20  t->eDest;.  int 
27949 69 50 61 72 6d 20 3d 20 70 44 65 73 74 2d 3e 69  iParm = pDest->i
2794a 50 61 72 6d 3b 0a 0a 20 20 69 6e 74 20 72 65 67  Parm;..  int reg
2794b 52 6f 77 3b 0a 20 20 69 6e 74 20 72 65 67 52 6f  Row;.  int regRo
2794c 77 69 64 3b 0a 0a 20 20 69 54 61 62 20 3d 20 70  wid;..  iTab = p
2794d 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
2794e 72 3b 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d  r;.  if( eDest==
2794f 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44  SRT_Output || eD
27950 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69  est==SRT_Corouti
27951 6e 65 20 29 7b 0a 20 20 20 20 70 73 65 75 64 6f  ne ){.    pseudo
27952 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
27953 61 62 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65  ab++;.    sqlite
27954 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
27955 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 70 73  P_OpenPseudo, ps
27956 65 75 64 6f 54 61 62 2c 20 65 44 65 73 74 3d 3d  eudoTab, eDest==
27957 53 52 54 5f 4f 75 74 70 75 74 2c 20 6e 43 6f 6c  SRT_Output, nCol
27958 75 6d 6e 29 3b 0a 20 20 7d 0a 20 20 61 64 64 72  umn);.  }.  addr
27959 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56 64   = 1 + sqlite3Vd
2795a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
2795b 6f 72 74 2c 20 69 54 61 62 2c 20 61 64 64 72 42  ort, iTab, addrB
2795c 72 65 61 6b 29 3b 0a 20 20 63 6f 64 65 4f 66 66  reak);.  codeOff
2795d 73 65 74 28 76 2c 20 70 2c 20 61 64 64 72 43 6f  set(v, p, addrCo
2795e 6e 74 69 6e 75 65 29 3b 0a 20 20 72 65 67 52 6f  ntinue);.  regRo
2795f 77 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  w = sqlite3GetTe
27960 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
27961 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69   regRowid = sqli
27962 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
27963 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
27964 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
27965 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 70  _Column, iTab, p
27966 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 2b  OrderBy->nExpr +
27967 20 31 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 73   1, regRow);.  s
27968 77 69 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a  witch( eDest ){.
27969 20 20 20 20 63 61 73 65 20 53 52 54 5f 54 61 62      case SRT_Tab
2796a 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54  le:.    case SRT
2796b 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20  _EphemTab: {.   
2796c 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65     testcase( eDe
2796d 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b  st==SRT_Table );
2796e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2796f 20 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65   eDest==SRT_Ephe
27970 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73 71  mTab );.      sq
27971 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27972 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
27973 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 69 64 29  iParm, regRowid)
27974 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27975 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
27976 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72  Insert, iParm, r
27977 65 67 52 6f 77 2c 20 72 65 67 52 6f 77 69 64 29  egRow, regRowid)
27978 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27979 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
2797a 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
2797b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2797c 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2797d 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
2797e 20 20 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a     case SRT_Set:
2797f 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
27980 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20   nColumn==1 );. 
27981 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27982 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp4(v, OP_Mak
27983 65 52 65 63 6f 72 64 2c 20 72 65 67 52 6f 77 2c  eRecord, regRow,
27984 20 31 2c 20 72 65 67 52 6f 77 69 64 2c 20 26 70   1, regRowid, &p
27985 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a  ->affinity, 1);.
27986 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
27987 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
27988 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
27989 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  Row, 1);.      s
2798a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2798b 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
2798c 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 69  , iParm, regRowi
2798d 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  d);.      break;
2798e 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2798f 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20  SRT_Mem: {.     
27990 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e   assert( nColumn
27991 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
27992 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
27993 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 2c  (pParse, regRow,
27994 20 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20   iParm, 1);.    
27995 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
27996 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d 69  lause will termi
27997 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f  nate the loop fo
27998 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72  r us */.      br
27999 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2799a 66 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4f  f.    case SRT_O
2799b 75 74 70 75 74 3a 0a 20 20 20 20 63 61 73 65 20  utput:.    case 
2799c 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 3a 20 7b  SRT_Coroutine: {
2799d 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
2799e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44      testcase( eD
2799f 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
279a0 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
279a1 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f  e( eDest==SRT_Co
279a2 72 6f 75 74 69 6e 65 20 29 3b 0a 20 20 20 20 20  routine );.     
279a3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
279a4 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
279a5 2c 20 31 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  , 1, regRowid);.
279a6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
279a7 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
279a8 73 65 72 74 2c 20 70 73 65 75 64 6f 54 61 62 2c  sert, pseudoTab,
279a9 20 72 65 67 52 6f 77 2c 20 72 65 67 52 6f 77 69   regRow, regRowi
279aa 64 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  d);.      for(i=
279ab 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  0; i<nColumn; i+
279ac 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  +){.        asse
279ad 72 74 28 20 72 65 67 52 6f 77 21 3d 70 44 65 73  rt( regRow!=pDes
279ae 74 2d 3e 69 4d 65 6d 2b 69 20 29 3b 0a 20 20 20  t->iMem+i );.   
279af 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
279b0 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
279b1 75 6d 6e 2c 20 70 73 65 75 64 6f 54 61 62 2c 20  umn, pseudoTab, 
279b2 69 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2b 69  i, pDest->iMem+i
279b3 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
279b4 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f   if( eDest==SRT_
279b5 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20  Output ){.      
279b6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
279b7 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
279b8 52 6f 77 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d  Row, pDest->iMem
279b9 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
279ba 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
279bb 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
279bc 67 65 28 70 50 61 72 73 65 2c 20 70 44 65 73 74  ge(pParse, pDest
279bd 2d 3e 69 4d 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29  ->iMem, nColumn)
279be 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
279bf 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
279c0 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
279c1 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69 50 61  ield, pDest->iPa
279c2 72 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rm);.      }.   
279c3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
279c4 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
279c5 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69       /* Do nothi
279c6 6e 67 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61  ng */.      brea
279c7 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  k;.    }.  }.  s
279c8 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
279c9 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
279ca 52 6f 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  Row);.  sqlite3R
279cb 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
279cc 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b  arse, regRowid);
279cd 0a 0a 20 20 2f 2a 20 4c 49 4d 49 54 20 68 61 73  ..  /* LIMIT has
279ce 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65   been implemente
279cf 64 20 62 79 20 74 68 65 20 70 75 73 68 4f 6e 74  d by the pushOnt
279d0 6f 53 6f 72 74 65 72 28 29 20 72 6f 75 74 69 6e  oSorter() routin
279d1 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  e..  */.  assert
279d2 28 20 70 2d 3e 69 4c 69 6d 69 74 3d 3d 30 20 29  ( p->iLimit==0 )
279d3 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74  ;..  /* The bott
279d4 6f 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20  om of the loop. 
279d5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
279d6 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
279d7 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0a   addrContinue);.
279d8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
279d9 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
279da 69 54 61 62 2c 20 61 64 64 72 29 3b 0a 20 20 73  iTab, addr);.  s
279db 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
279dc 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 42 72  eLabel(v, addrBr
279dd 65 61 6b 29 3b 0a 20 20 69 66 28 20 65 44 65 73  eak);.  if( eDes
279de 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c  t==SRT_Output ||
279df 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
279e0 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c  utine ){.    sql
279e1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
279e2 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75  , OP_Close, pseu
279e3 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 7d  doTab, 0);.  }.}
279e4 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
279e5 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
279e6 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
279e7 74 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f 6e  the 'declaration
279e8 20 74 79 70 65 27 20 6f 66 20 74 68 65 0a 2a 2a   type' of the.**
279e9 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70   expression pExp
279ea 72 2e 20 54 68 65 20 73 74 72 69 6e 67 20 6d 61  r. The string ma
279eb 79 20 62 65 20 74 72 65 61 74 65 64 20 61 73 20  y be treated as 
279ec 73 74 61 74 69 63 20 62 79 20 74 68 65 20 63 61  static by the ca
279ed 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ller..**.** The 
279ee 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
279ef 20 69 73 20 74 68 65 20 65 78 61 63 74 20 64 61   is the exact da
279f0 74 61 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f  tatype definitio
279f1 6e 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d  n extracted from
279f2 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   the.** original
279f3 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
279f4 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20 65  atement if the e
279f5 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63  xpression is a c
279f6 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64 65  olumn. The.** de
279f7 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 66  claration type f
279f8 6f 72 20 61 20 52 4f 57 49 44 20 66 69 65 6c 64  or a ROWID field
279f9 20 69 73 20 49 4e 54 45 47 45 52 2e 20 45 78 61   is INTEGER. Exa
279fa 63 74 6c 79 20 77 68 65 6e 20 61 6e 20 65 78 70  ctly when an exp
279fb 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f  ression.** is co
279fc 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
279fd 6e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 78  n can be complex
279fe 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e 63 65   in the presence
279ff 20 6f 66 20 73 75 62 71 75 65 72 69 65 73 2e 20   of subqueries. 
27a00 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65  The.** result-se
27a01 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  t expression in 
27a02 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  all of the follo
27a03 77 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74  wing SELECT stat
27a04 65 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20 63 6f  ements is .** co
27a05 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
27a06 6e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  n by this functi
27a07 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  on..**.**   SELE
27a08 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b  CT col FROM tbl;
27a09 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45  .**   SELECT (SE
27a0a 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62  LECT col FROM tb
27a0b 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28  l;.**   SELECT (
27a0c 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20  SELECT col FROM 
27a0d 74 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43  tbl);.**   SELEC
27a0e 54 20 61 62 63 20 46 52 4f 4d 20 28 53 45 4c 45  T abc FROM (SELE
27a0f 43 54 20 63 6f 6c 20 41 53 20 61 62 63 20 46 52  CT col AS abc FR
27a10 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20  OM tbl);.** .** 
27a11 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  The declaration 
27a12 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65 78 70  type for any exp
27a13 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 74 68  ression other th
27a14 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 4e  an a column is N
27a15 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ULL..*/.static c
27a16 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d  onst char *colum
27a17 6e 54 79 70 65 28 0a 20 20 4e 61 6d 65 43 6f 6e  nType(.  NameCon
27a18 74 65 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45 78  text *pNC, .  Ex
27a19 70 72 20 2a 70 45 78 70 72 2c 0a 20 20 63 6f 6e  pr *pExpr,.  con
27a1a 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67  st char **pzOrig
27a1b 69 6e 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  inDb,.  const ch
27a1c 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 54 61 62  ar **pzOriginTab
27a1d 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
27a1e 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a  *pzOriginCol.){.
27a1f 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54    char const *zT
27a20 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  ype = 0;.  char 
27a21 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 44 62  const *zOriginDb
27a22 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e   = 0;.  char con
27a23 73 74 20 2a 7a 4f 72 69 67 69 6e 54 61 62 20 3d  st *zOriginTab =
27a24 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   0;.  char const
27a25 20 2a 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 30   *zOriginCol = 0
27a26 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69 66 28  ;.  int j;.  if(
27a27 20 70 45 78 70 72 3d 3d 30 20 7c 7c 20 70 4e 43   pExpr==0 || pNC
27a28 2d 3e 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20  ->pSrcList==0 ) 
27a29 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 73 77 69  return 0;..  swi
27a2a 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
27a2b 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  {.    case TK_AG
27a2c 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61  G_COLUMN:.    ca
27a2d 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a  se TK_COLUMN: {.
27a2e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
27a2f 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c  ression is a col
27a30 75 6d 6e 2e 20 4c 6f 63 61 74 65 20 74 68 65 20  umn. Locate the 
27a31 74 61 62 6c 65 20 74 68 65 20 63 6f 6c 75 6d 6e  table the column
27a32 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20   is being.      
27a33 2a 2a 20 65 78 74 72 61 63 74 65 64 20 66 72 6f  ** extracted fro
27a34 6d 20 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 78 74  m in NameContext
27a35 2e 70 53 72 63 4c 69 73 74 2e 20 54 68 69 73 20  .pSrcList. This 
27a36 74 61 62 6c 65 20 6d 61 79 20 62 65 20 72 65 61  table may be rea
27a37 6c 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62  l.      ** datab
27a38 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 20 73  ase table or a s
27a39 75 62 71 75 65 72 79 2e 0a 20 20 20 20 20 20 2a  ubquery..      *
27a3a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  /.      Table *p
27a3b 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Tab = 0;        
27a3c 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 73 74 72      /* Table str
27a3d 75 63 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69 73  ucture column is
27a3e 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
27a3f 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20  */.      Select 
27a40 2a 70 53 20 3d 20 30 3b 20 20 20 20 20 20 20 20  *pS = 0;        
27a41 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74       /* Select t
27a42 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74  he column is ext
27a43 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20  racted from */. 
27a44 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
27a45 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20  pExpr->iColumn; 
27a46 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
27a47 75 6d 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20  umn in pTab */. 
27a48 20 20 20 20 20 77 68 69 6c 65 28 20 70 4e 43 20       while( pNC 
27a49 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20  && !pTab ){.    
27a4a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61      SrcList *pTa
27a4b 62 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  bList = pNC->pSr
27a4c 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 66  cList;.        f
27a4d 6f 72 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69 73  or(j=0;j<pTabLis
27a4e 74 2d 3e 6e 53 72 63 20 26 26 20 70 54 61 62 4c  t->nSrc && pTabL
27a4f 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f  ist->a[j].iCurso
27a50 72 21 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  r!=pExpr->iTable
27a51 3b 6a 2b 2b 29 3b 0a 20 20 20 20 20 20 20 20 69  ;j++);.        i
27a52 66 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  f( j<pTabList->n
27a53 53 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Src ){.         
27a54 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74   pTab = pTabList
27a55 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20  ->a[j].pTab;.   
27a56 20 20 20 20 20 20 20 70 53 20 3d 20 70 54 61 62         pS = pTab
27a57 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c 65  List->a[j].pSele
27a58 63 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ct;.        }els
27a59 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 43  e{.          pNC
27a5a 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20   = pNC->pNext;. 
27a5b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
27a5c 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ..      if( pTab
27a5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
27a5e 2a 20 46 49 58 20 4d 45 3a 0a 20 20 20 20 20 20  * FIX ME:.      
27a5f 20 20 2a 2a 20 54 68 69 73 20 63 61 6e 20 6f 63    ** This can oc
27a60 63 75 72 73 20 69 66 20 79 6f 75 20 68 61 76 65  curs if you have
27a61 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65 20   something like 
27a62 22 53 45 4c 45 43 54 20 6e 65 77 2e 78 3b 22 20  "SELECT new.x;" 
27a63 69 6e 73 69 64 65 0a 20 20 20 20 20 20 20 20 2a  inside.        *
27a64 2a 20 61 20 74 72 69 67 67 65 72 2e 20 20 49 6e  * a trigger.  In
27a65 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66   other words, if
27a66 20 79 6f 75 20 72 65 66 65 72 65 6e 63 65 20 74   you reference t
27a67 68 65 20 73 70 65 63 69 61 6c 20 22 6e 65 77 22  he special "new"
27a68 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c  .        ** tabl
27a69 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  e in the result 
27a6a 73 65 74 20 6f 66 20 61 20 73 65 6c 65 63 74 2e  set of a select.
27a6b 20 20 57 65 20 64 6f 20 6e 6f 74 20 68 61 76 65    We do not have
27a6c 20 61 20 67 6f 6f 64 20 77 61 79 0a 20 20 20 20   a good way.    
27a6d 20 20 20 20 2a 2a 20 74 6f 20 66 69 6e 64 20 74      ** to find t
27a6e 68 65 20 61 63 74 75 61 6c 20 74 61 62 6c 65 20  he actual table 
27a6f 74 79 70 65 2c 20 73 6f 20 63 61 6c 6c 20 69 74  type, so call it
27a70 20 22 54 45 58 54 22 2e 20 20 54 68 69 73 20 69   "TEXT".  This i
27a71 73 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 20  s really.       
27a72 20 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 66   ** something of
27a73 20 61 20 62 75 67 2c 20 62 75 74 20 49 20 64 6f   a bug, but I do
27a74 20 6e 6f 74 20 6b 6e 6f 77 20 68 6f 77 20 74 6f   not know how to
27a75 20 66 69 78 20 69 74 2e 0a 20 20 20 20 20 20 20   fix it..       
27a76 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54   **.        ** T
27a77 68 69 73 20 63 6f 64 65 20 64 6f 65 73 20 6e 6f  his code does no
27a78 74 20 70 72 6f 64 75 63 65 20 74 68 65 20 63 6f  t produce the co
27a79 72 72 65 63 74 20 61 6e 73 77 65 72 20 2d 20 69  rrect answer - i
27a7a 74 20 6a 75 73 74 20 70 72 65 76 65 6e 74 73 0a  t just prevents.
27a7b 20 20 20 20 20 20 20 20 2a 2a 20 61 20 73 65 67          ** a seg
27a7c 66 61 75 6c 74 2e 20 20 53 65 65 20 74 69 63 6b  fault.  See tick
27a7d 65 74 20 23 31 32 32 39 2e 0a 20 20 20 20 20 20  et #1229..      
27a7e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 54 79    */.        zTy
27a7f 70 65 20 3d 20 22 54 45 58 54 22 3b 0a 20 20 20  pe = "TEXT";.   
27a80 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27a81 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72    }..      asser
27a82 74 28 20 70 54 61 62 20 29 3b 0a 20 20 20 20 20  t( pTab );.     
27a83 20 69 66 28 20 70 53 20 29 7b 0a 20 20 20 20 20   if( pS ){.     
27a84 20 20 20 2f 2a 20 54 68 65 20 22 74 61 62 6c 65     /* The "table
27a85 22 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20  " is actually a 
27a86 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 20  sub-select or a 
27a87 76 69 65 77 20 69 6e 20 74 68 65 20 46 52 4f 4d  view in the FROM
27a88 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20   clause.        
27a89 2a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ** of the SELECT
27a8a 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 74 75   statement. Retu
27a8b 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74 69  rn the declarati
27a8c 6f 6e 20 74 79 70 65 20 61 6e 64 20 6f 72 69 67  on type and orig
27a8d 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61  in.        ** da
27a8e 74 61 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c  ta for the resul
27a8f 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  t-set column of 
27a90 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74 2e 0a  the sub-select..
27a91 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
27a92 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 69 43     if( ALWAYS(iC
27a93 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 53  ol>=0 && iCol<pS
27a94 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  ->pEList->nExpr)
27a95 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
27a96 20 49 66 20 69 43 6f 6c 20 69 73 20 6c 65 73 73   If iCol is less
27a97 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e   than zero, then
27a98 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
27a99 72 65 71 75 65 73 74 73 20 74 68 65 0a 20 20 20  requests the.   
27a9a 20 20 20 20 20 20 20 2a 2a 20 72 6f 77 69 64 20         ** rowid 
27a9b 6f 66 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63  of the sub-selec
27a9c 74 20 6f 72 20 76 69 65 77 2e 20 54 68 69 73 20  t or view. This 
27a9d 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6c 65  expression is le
27a9e 67 61 6c 20 28 73 65 65 20 0a 20 20 20 20 20 20  gal (see .      
27a9f 20 20 20 20 2a 2a 20 74 65 73 74 20 63 61 73 65      ** test case
27aa0 20 6d 69 73 63 32 2e 32 2e 32 29 20 2d 20 69 74   misc2.2.2) - it
27aa1 20 61 6c 77 61 79 73 20 65 76 61 6c 75 61 74 65   always evaluate
27aa2 73 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20  s to NULL..     
27aa3 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
27aa4 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
27aa5 43 3b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70  C;.          Exp
27aa6 72 20 2a 70 20 3d 20 70 53 2d 3e 70 45 4c 69 73  r *p = pS->pELis
27aa7 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72  t->a[iCol].pExpr
27aa8 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e  ;.          sNC.
27aa9 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70  pSrcList = pS->p
27aaa 53 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73  Src;.          s
27aab 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  NC.pNext = 0;.  
27aac 20 20 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72          sNC.pPar
27aad 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
27aae 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70  ;.          zTyp
27aaf 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
27ab0 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69 6e  sNC, p, &zOrigin
27ab1 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62 2c  Db, &zOriginTab,
27ab2 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20 0a   &zOriginCol); .
27ab3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
27ab4 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53  }else if( ALWAYS
27ab5 28 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 20  (pTab->pSchema) 
27ab6 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20  ){.        /* A 
27ab7 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  real table */.  
27ab8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
27ab9 53 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  S );.        if(
27aba 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d   iCol<0 ) iCol =
27abb 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20   pTab->iPKey;.  
27abc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 43        assert( iC
27abd 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e  ol==-1 || (iCol>
27abe 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d  =0 && iCol<pTab-
27abf 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20  >nCol) );.      
27ac0 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a    if( iCol<0 ){.
27ac1 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 20            zType 
27ac2 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20  = "INTEGER";.   
27ac3 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f         zOriginCo
27ac4 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20  l = "rowid";.   
27ac5 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27ac6 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54        zType = pT
27ac7 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
27ac8 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Type;.          
27ac9 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54 61  zOriginCol = pTa
27aca 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
27acb 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ame;.        }. 
27acc 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 54 61         zOriginTa
27acd 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  b = pTab->zName;
27ace 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 43  .        if( pNC
27acf 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20  ->pParse ){.    
27ad0 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20        int iDb = 
27ad1 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
27ad2 6e 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73 65  ndex(pNC->pParse
27ad3 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
27ad4 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ema);.          
27ad5 7a 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43 2d  zOriginDb = pNC-
27ad6 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  >pParse->db->aDb
27ad7 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  [iDb].zName;.   
27ad8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
27ad9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27ada 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
27adb 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
27adc 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
27add 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  T: {.      /* Th
27ade 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
27adf 61 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 52 65  a sub-select. Re
27ae0 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61  turn the declara
27ae1 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20 20  tion type and.  
27ae2 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69 6e      ** origin in
27ae3 66 6f 20 66 6f 72 20 74 68 65 20 73 69 6e 67 6c  fo for the singl
27ae4 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  e column in the 
27ae5 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
27ae6 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a  e SELECT.      *
27ae7 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  * statement..   
27ae8 20 20 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d 65     */.      Name
27ae9 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20  Context sNC;.   
27aea 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d 20     Select *pS = 
27aeb 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
27aec 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 20  ;.      Expr *p 
27aed 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  = pS->pEList->a[
27aee 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  0].pExpr;.      
27aef 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50  assert( ExprHasP
27af0 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
27af1 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
27af2 20 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69        sNC.pSrcLi
27af3 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20  st = pS->pSrc;. 
27af4 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d       sNC.pNext =
27af5 20 70 4e 43 3b 0a 20 20 20 20 20 20 73 4e 43 2e   pNC;.      sNC.
27af6 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
27af7 61 72 73 65 3b 0a 20 20 20 20 20 20 7a 54 79 70  arse;.      zTyp
27af8 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
27af9 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69 6e  sNC, p, &zOrigin
27afa 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62 2c  Db, &zOriginTab,
27afb 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20 0a   &zOriginCol); .
27afc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27afd 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20   }.#endif.  }.  
27afe 0a 20 20 69 66 28 20 70 7a 4f 72 69 67 69 6e 44  .  if( pzOriginD
27aff 62 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  b ){.    assert(
27b00 20 70 7a 4f 72 69 67 69 6e 54 61 62 20 26 26 20   pzOriginTab && 
27b01 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 29 3b 0a 20  pzOriginCol );. 
27b02 20 20 20 2a 70 7a 4f 72 69 67 69 6e 44 62 20 3d     *pzOriginDb =
27b03 20 7a 4f 72 69 67 69 6e 44 62 3b 0a 20 20 20 20   zOriginDb;.    
27b04 2a 70 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 7a  *pzOriginTab = z
27b05 4f 72 69 67 69 6e 54 61 62 3b 0a 20 20 20 20 2a  OriginTab;.    *
27b06 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 7a 4f  pzOriginCol = zO
27b07 72 69 67 69 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20  riginCol;.  }.  
27b08 72 65 74 75 72 6e 20 7a 54 79 70 65 3b 0a 7d 0a  return zType;.}.
27b09 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
27b0a 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 74  code that will t
27b0b 65 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68 65  ell the VDBE the
27b0c 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
27b0d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  es of columns.**
27b0e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
27b0f 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  et..*/.static vo
27b10 69 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d  id generateColum
27b11 6e 54 79 70 65 73 28 0a 20 20 50 61 72 73 65 20  nTypes(.  Parse 
27b12 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
27b13 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
27b14 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
27b15 61 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74  abList,  /* List
27b16 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20   of tables */.  
27b17 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
27b18 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
27b19 6e 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20  ns defining the 
27b1a 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29 7b  result set */.){
27b1b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
27b1c 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a 20 20  OMIT_DECLTYPE.  
27b1d 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
27b1e 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
27b1f 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
27b20 73 4e 43 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c  sNC;.  sNC.pSrcL
27b21 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a  ist = pTabList;.
27b22 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
27b23 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30  Parse;.  for(i=0
27b24 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; i<pEList->nExp
27b25 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  r; i++){.    Exp
27b26 72 20 2a 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61  r *p = pEList->a
27b27 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 63  [i].pExpr;.    c
27b28 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65  onst char *zType
27b29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
27b2a 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
27b2b 54 41 44 41 54 41 0a 20 20 20 20 63 6f 6e 73 74  TADATA.    const
27b2c 20 63 68 61 72 20 2a 7a 4f 72 69 67 44 62 20 3d   char *zOrigDb =
27b2d 20 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   0;.    const ch
27b2e 61 72 20 2a 7a 4f 72 69 67 54 61 62 20 3d 20 30  ar *zOrigTab = 0
27b2f 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
27b30 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d 20 30 3b 0a   *zOrigCol = 0;.
27b31 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75      zType = colu
27b32 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
27b33 26 7a 4f 72 69 67 44 62 2c 20 26 7a 4f 72 69 67  &zOrigDb, &zOrig
27b34 54 61 62 2c 20 26 7a 4f 72 69 67 43 6f 6c 29 3b  Tab, &zOrigCol);
27b35 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 76 64 62  ..    /* The vdb
27b36 65 20 6d 75 73 74 20 6d 61 6b 65 20 69 74 73 20  e must make its 
27b37 6f 77 6e 20 63 6f 70 79 20 6f 66 20 74 68 65 20  own copy of the 
27b38 63 6f 6c 75 6d 6e 2d 74 79 70 65 20 61 6e 64 20  column-type and 
27b39 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 63 6f  other .    ** co
27b3a 6c 75 6d 6e 20 73 70 65 63 69 66 69 63 20 73 74  lumn specific st
27b3b 72 69 6e 67 73 2c 20 69 6e 20 63 61 73 65 20 74  rings, in case t
27b3c 68 65 20 73 63 68 65 6d 61 20 69 73 20 72 65 73  he schema is res
27b3d 65 74 20 62 65 66 6f 72 65 20 74 68 69 73 0a 20  et before this. 
27b3e 20 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61     ** virtual ma
27b3f 63 68 69 6e 65 20 69 73 20 64 65 6c 65 74 65 64  chine is deleted
27b40 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
27b41 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
27b42 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
27b43 5f 44 41 54 41 42 41 53 45 2c 20 7a 4f 72 69 67  _DATABASE, zOrig
27b44 44 62 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  Db, SQLITE_TRANS
27b45 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74  IENT);.    sqlit
27b46 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
27b47 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 54  (v, i, COLNAME_T
27b48 41 42 4c 45 2c 20 7a 4f 72 69 67 54 61 62 2c 20  ABLE, zOrigTab, 
27b49 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
27b4a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
27b4b 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
27b4c 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d  i, COLNAME_COLUM
27b4d 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c 20 53 51 4c  N, zOrigCol, SQL
27b4e 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
27b4f 23 65 6c 73 65 0a 20 20 20 20 7a 54 79 70 65 20  #else.    zType 
27b50 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e  = columnType(&sN
27b51 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  C, p, 0, 0, 0);.
27b52 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
27b53 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
27b54 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44  (v, i, COLNAME_D
27b55 45 43 4c 54 59 50 45 2c 20 7a 54 79 70 65 2c 20  ECLTYPE, zType, 
27b56 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
27b57 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  );.  }.#endif /*
27b58 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
27b59 4c 54 59 50 45 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a  LTYPE */.}../*.*
27b5a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
27b5b 74 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74  that will tell t
27b5c 68 65 20 56 44 42 45 20 74 68 65 20 6e 61 6d 65  he VDBE the name
27b5d 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  s of columns.** 
27b5e 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
27b5f 74 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61  t.  This informa
27b60 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
27b61 70 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61  provide the.** a
27b62 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65 73 20 69 6e  zCol[] values in
27b63 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a   the callback..*
27b64 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  /.static void ge
27b65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65  nerateColumnName
27b66 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  s(.  Parse *pPar
27b67 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73  se,      /* Pars
27b68 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
27b69 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
27b6a 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  t,  /* List of t
27b6b 61 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c  ables */.  ExprL
27b6c 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f  ist *pEList    /
27b6d 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65  * Expressions de
27b6e 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c  fining the resul
27b6f 74 20 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 56 64  t set */.){.  Vd
27b70 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
27b71 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 2c 20  pVdbe;.  int i, 
27b72 6a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  j;.  sqlite3 *db
27b73 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
27b74 20 69 6e 74 20 66 75 6c 6c 4e 61 6d 65 73 2c 20   int fullNames, 
27b75 73 68 6f 72 74 4e 61 6d 65 73 3b 0a 0a 23 69 66  shortNames;..#if
27b76 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27b77 5f 45 58 50 4c 41 49 4e 0a 20 20 2f 2a 20 49 66  _EXPLAIN.  /* If
27b78 20 74 68 69 73 20 69 73 20 61 6e 20 45 58 50 4c   this is an EXPL
27b79 41 49 4e 2c 20 73 6b 69 70 20 74 68 69 73 20 73  AIN, skip this s
27b7a 74 65 70 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  tep */.  if( pPa
27b7b 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a  rse->explain ){.
27b7c 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
27b7d 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74  #endif..  assert
27b7e 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ( v!=0 );.  if( 
27b7f 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73  pParse->colNames
27b80 53 65 74 20 7c 7c 20 4e 45 56 45 52 28 76 3d 3d  Set || NEVER(v==
27b81 30 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  0) || db->malloc
27b82 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b  Failed ) return;
27b83 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61  .  pParse->colNa
27b84 6d 65 73 53 65 74 20 3d 20 31 3b 0a 20 20 66 75  mesSet = 1;.  fu
27b85 6c 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66  llNames = (db->f
27b86 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75  lags & SQLITE_Fu
27b87 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a  llColNames)!=0;.
27b88 20 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d 20 28    shortNames = (
27b89 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
27b8a 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
27b8b 29 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 56  )!=0;.  sqlite3V
27b8c 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
27b8d 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b   pEList->nExpr);
27b8e 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45  .  for(i=0; i<pE
27b8f 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
27b90 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 3b 0a  ){.    Expr *p;.
27b91 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e      p = pEList->
27b92 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
27b93 69 66 28 20 70 3d 3d 30 20 29 20 63 6f 6e 74 69  if( p==0 ) conti
27b94 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 45 4c  nue;.    if( pEL
27b95 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20  ist->a[i].zName 
27b96 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
27b97 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61  Name = pEList->a
27b98 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [i].zName;.     
27b99 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
27b9a 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
27b9b 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65  NAME_NAME, zName
27b9c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
27b9d 4e 54 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  NT);.    }else i
27b9e 66 28 20 28 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  f( (p->op==TK_CO
27b9f 4c 55 4d 4e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 54  LUMN || p->op==T
27ba0 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 29 20 26 26  K_AGG_COLUMN) &&
27ba1 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20 20 20   pTabList ){.   
27ba2 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
27ba3 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c        char *zCol
27ba4 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  ;.      int iCol
27ba5 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20   = p->iColumn;. 
27ba6 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 41 4c       for(j=0; AL
27ba7 57 41 59 53 28 6a 3c 70 54 61 62 4c 69 73 74 2d  WAYS(j<pTabList-
27ba8 3e 6e 53 72 63 29 3b 20 6a 2b 2b 29 7b 0a 20 20  >nSrc); j++){.  
27ba9 20 20 20 20 20 20 69 66 28 20 70 54 61 62 4c 69        if( pTabLi
27baa 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72  st->a[j].iCursor
27bab 3d 3d 70 2d 3e 69 54 61 62 6c 65 20 29 20 62 72  ==p->iTable ) br
27bac 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
27bad 20 20 20 61 73 73 65 72 74 28 20 6a 3c 70 54 61     assert( j<pTa
27bae 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20  bList->nSrc );. 
27baf 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62       pTab = pTab
27bb0 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b  List->a[j].pTab;
27bb1 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  .      if( iCol<
27bb2 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d  0 ) iCol = pTab-
27bb3 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 61 73  >iPKey;.      as
27bb4 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c  sert( iCol==-1 |
27bb5 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43  | (iCol>=0 && iC
27bb6 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29  ol<pTab->nCol) )
27bb7 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
27bb8 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43  <0 ){.        zC
27bb9 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20  ol = "rowid";.  
27bba 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27bbb 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e     zCol = pTab->
27bbc 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
27bbd 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
27bbe 69 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73 20  if( !shortNames 
27bbf 26 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 29 7b  && !fullNames ){
27bc0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27bc1 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
27bc2 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , i, COLNAME_NAM
27bc3 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  E, .            
27bc4 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
27bc5 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73  (db, (char*)p->s
27bc6 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e  pan.z, p->span.n
27bc7 29 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  ), SQLITE_DYNAMI
27bc8 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  C);.      }else 
27bc9 69 66 28 20 66 75 6c 6c 4e 61 6d 65 73 20 29 7b  if( fullNames ){
27bca 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
27bcb 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Name = 0;.      
27bcc 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
27bcd 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
27bce 2e 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  .%s", pTab->zNam
27bcf 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20  e, zCol);.      
27bd0 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
27bd1 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
27bd2 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d  LNAME_NAME, zNam
27bd3 65 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  e, SQLITE_DYNAMI
27bd4 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  C);.      }else{
27bd5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27bd6 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
27bd7 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , i, COLNAME_NAM
27bd8 45 2c 20 7a 43 6f 6c 2c 20 53 51 4c 49 54 45 5f  E, zCol, SQLITE_
27bd9 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
27bda 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
27bdb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27bdc 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
27bdd 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a   COLNAME_NAME, .
27bde 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27bdf 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
27be0 63 68 61 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a 2c  char*)p->span.z,
27be1 20 70 2d 3e 73 70 61 6e 2e 6e 29 2c 20 53 51 4c   p->span.n), SQL
27be2 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ITE_DYNAMIC);.  
27be3 20 20 7d 0a 20 20 7d 0a 20 20 67 65 6e 65 72 61    }.  }.  genera
27be4 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 70 50  teColumnTypes(pP
27be5 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
27be6 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a 23 69 66 6e  pEList);.}..#ifn
27be7 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27be8 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
27be9 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68  /*.** Name of th
27bea 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65  e connection ope
27beb 72 61 74 6f 72 2c 20 75 73 65 64 20 66 6f 72 20  rator, used for 
27bec 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e 0a  error messages..
27bed 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
27bee 63 68 61 72 20 2a 73 65 6c 65 63 74 4f 70 4e 61  char *selectOpNa
27bef 6d 65 28 69 6e 74 20 69 64 29 7b 0a 20 20 63 68  me(int id){.  ch
27bf0 61 72 20 2a 7a 3b 0a 20 20 73 77 69 74 63 68 28  ar *z;.  switch(
27bf1 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20   id ){.    case 
27bf2 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 20 20 7a 20  TK_ALL:       z 
27bf3 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b 20 20  = "UNION ALL";  
27bf4 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
27bf5 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20 7a   TK_INTERSECT: z
27bf6 20 3d 20 22 49 4e 54 45 52 53 45 43 54 22 3b 20   = "INTERSECT"; 
27bf7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
27bf8 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20 20 20  e TK_EXCEPT:    
27bf9 7a 20 3d 20 22 45 58 43 45 50 54 22 3b 20 20 20  z = "EXCEPT";   
27bfa 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
27bfb 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20  fault:          
27bfc 20 7a 20 3d 20 22 55 4e 49 4f 4e 22 3b 20 20 20   z = "UNION";   
27bfd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
27bfe 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e   return z;.}.#en
27bff 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
27c00 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
27c01 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 69 76  CT */../*.** Giv
27c02 65 6e 20 61 20 61 6e 20 65 78 70 72 65 73 73 69  en a an expressi
27c03 6f 6e 20 6c 69 73 74 20 28 77 68 69 63 68 20 69  on list (which i
27c04 73 20 72 65 61 6c 6c 79 20 74 68 65 20 6c 69 73  s really the lis
27c05 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
27c06 0a 2a 2a 20 74 68 61 74 20 66 6f 72 6d 20 74 68  .** that form th
27c07 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
27c08 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
27c09 6e 74 29 20 63 6f 6d 70 75 74 65 20 61 70 70 72  nt) compute appr
27c0a 6f 70 72 69 61 74 65 0a 2a 2a 20 63 6f 6c 75 6d  opriate.** colum
27c0b 6e 20 6e 61 6d 65 73 20 66 6f 72 20 61 20 74 61  n names for a ta
27c0c 62 6c 65 20 74 68 61 74 20 77 6f 75 6c 64 20 68  ble that would h
27c0d 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73 69  old the expressi
27c0e 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41  on list..**.** A
27c0f 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ll column names 
27c10 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 2e 0a  will be unique..
27c11 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 63  **.** Only the c
27c12 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 72 65 20  olumn names are 
27c13 63 6f 6d 70 75 74 65 64 2e 20 20 43 6f 6c 75 6d  computed.  Colum
27c14 6e 2e 7a 54 79 70 65 2c 20 43 6f 6c 75 6d 6e 2e  n.zType, Column.
27c15 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61 6e 64 20 6f 74  zColl,.** and ot
27c16 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20 43 6f  her fields of Co
27c17 6c 75 6d 6e 20 61 72 65 20 7a 65 72 6f 65 64 2e  lumn are zeroed.
27c18 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
27c19 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
27c1a 73 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79  ss.  If a memory
27c1b 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
27c1c 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 73 74 6f  r occurs,.** sto
27c1d 72 65 20 4e 55 4c 4c 20 69 6e 20 2a 70 61 43 6f  re NULL in *paCo
27c1e 6c 20 61 6e 64 20 30 20 69 6e 20 2a 70 6e 43 6f  l and 0 in *pnCo
27c1f 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c  l and return SQL
27c20 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74  ITE_NOMEM..*/.st
27c21 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 43  atic int selectC
27c22 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69  olumnsFromExprLi
27c23 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  st(.  Parse *pPa
27c24 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
27c25 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
27c26 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
27c27 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  pEList,       /*
27c28 20 45 78 70 72 20 6c 69 73 74 20 66 72 6f 6d 20   Expr list from 
27c29 77 68 69 63 68 20 74 6f 20 64 65 72 69 76 65 20  which to derive 
27c2a 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a  column names */.
27c2b 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 2c 20 20 20    int *pnCol,   
27c2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
27c2d 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  te the number of
27c2e 20 63 6f 6c 75 6d 6e 73 20 68 65 72 65 20 2a 2f   columns here */
27c2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a 70 61 43 6f  .  Column **paCo
27c30 6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  l          /* Wr
27c31 69 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c 75  ite the new colu
27c32 6d 6e 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  mn list here */.
27c33 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
27c34 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
27c35 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
27c36 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  nection */.  int
27c37 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20   i, j;          
27c38 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
27c39 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
27c3a 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20  nt cnt;         
27c3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
27c3c 64 65 78 20 61 64 64 65 64 20 74 6f 20 6d 61 6b  dex added to mak
27c3d 65 20 74 68 65 20 6e 61 6d 65 20 75 6e 69 71 75  e the name uniqu
27c3e 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61  e */.  Column *a
27c3f 43 6f 6c 2c 20 2a 70 43 6f 6c 3b 20 20 20 20 20  Col, *pCol;     
27c40 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
27c41 67 20 6f 76 65 72 20 72 65 73 75 6c 74 20 63 6f  g over result co
27c42 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  lumns */.  int n
27c43 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
27c44 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
27c45 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
27c46 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
27c47 0a 20 20 45 78 70 72 20 2a 70 3b 20 20 20 20 20  .  Expr *p;     
27c48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27c49 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 66 6f 72  * Expression for
27c4a 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c 74   a single result
27c4b 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61   column */.  cha
27c4c 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
27c4d 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
27c4e 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74  mn name */.  int
27c4f 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20   nName;         
27c50 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
27c51 20 6f 66 20 6e 61 6d 65 20 69 6e 20 7a 4e 61 6d   of name in zNam
27c52 65 5b 5d 20 2a 2f 0a 0a 20 20 2a 70 6e 43 6f 6c  e[] */..  *pnCol
27c53 20 3d 20 6e 43 6f 6c 20 3d 20 70 45 4c 69 73 74   = nCol = pEList
27c54 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 43 6f 6c 20  ->nExpr;.  aCol 
27c55 3d 20 2a 70 61 43 6f 6c 20 3d 20 73 71 6c 69 74  = *paCol = sqlit
27c56 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
27c57 62 2c 20 73 69 7a 65 6f 66 28 61 43 6f 6c 5b 30  b, sizeof(aCol[0
27c58 5d 29 2a 6e 43 6f 6c 29 3b 0a 20 20 69 66 28 20  ])*nCol);.  if( 
27c59 61 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  aCol==0 ) return
27c5a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
27c5b 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 61   for(i=0, pCol=a
27c5c 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  Col; i<nCol; i++
27c5d 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 2f  , pCol++){.    /
27c5e 2a 20 47 65 74 20 61 6e 20 61 70 70 72 6f 70 72  * Get an appropr
27c5f 69 61 74 65 20 6e 61 6d 65 20 66 6f 72 20 74 68  iate name for th
27c60 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a  e column.    */.
27c61 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e      p = pEList->
27c62 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
27c63 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68  assert( p->pRigh
27c64 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 52 69 67 68  t==0 || p->pRigh
27c65 74 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 7c 7c  t->token.z==0 ||
27c66 20 70 2d 3e 70 52 69 67 68 74 2d 3e 74 6f 6b 65   p->pRight->toke
27c67 6e 2e 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20  n.z[0]!=0 );.   
27c68 20 69 66 28 20 28 7a 4e 61 6d 65 20 3d 20 70 45   if( (zName = pE
27c69 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
27c6a 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  )!=0 ){.      /*
27c6b 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63   If the column c
27c6c 6f 6e 74 61 69 6e 73 20 61 6e 20 22 41 53 20 3c  ontains an "AS <
27c6d 6e 61 6d 65 3e 22 20 70 68 72 61 73 65 2c 20 75  name>" phrase, u
27c6e 73 65 20 3c 6e 61 6d 65 3e 20 61 73 20 74 68 65  se <name> as the
27c6f 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a   name */.      z
27c70 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Name = sqlite3Db
27c71 53 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65  StrDup(db, zName
27c72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
27c73 20 20 20 20 45 78 70 72 20 2a 70 43 6f 6c 45 78      Expr *pColEx
27c74 70 72 20 3d 20 70 3b 20 20 2f 2a 20 54 68 65 20  pr = p;  /* The 
27c75 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
27c76 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  is the result co
27c77 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  lumn name */.   
27c78 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20     Table *pTab; 
27c79 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
27c7a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
27c7b 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   this expression
27c7c 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
27c7d 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54   pColExpr->op==T
27c7e 4b 5f 44 4f 54 20 29 20 70 43 6f 6c 45 78 70 72  K_DOT ) pColExpr
27c7f 20 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 70 52 69   = pColExpr->pRi
27c80 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ght;.      if( p
27c81 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ColExpr->op==TK_
27c82 43 4f 4c 55 4d 4e 20 26 26 20 28 70 54 61 62 20  COLUMN && (pTab 
27c83 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62  = pColExpr->pTab
27c84 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
27c85 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d 6e 73 20 75  /* For columns u
27c86 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  se the column na
27c87 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20  me name */.     
27c88 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 43     int iCol = pC
27c89 6f 6c 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b  olExpr->iColumn;
27c8a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
27c8b 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61  l<0 ) iCol = pTa
27c8c 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20  b->iPKey;.      
27c8d 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
27c8e 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
27c8f 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
27c90 20 20 20 20 69 43 6f 6c 3e 3d 30 20 3f 20 70 54      iCol>=0 ? pT
27c91 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
27c92 4e 61 6d 65 20 3a 20 22 72 6f 77 69 64 22 29 3b  Name : "rowid");
27c93 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
27c94 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65        /* Use the
27c95 20 6f 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f   original text o
27c96 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 65 78 70  f the column exp
27c97 72 65 73 73 69 6f 6e 20 61 73 20 69 74 73 20 6e  ression as its n
27c98 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 54  ame */.        T
27c99 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 28  oken *pToken = (
27c9a 70 43 6f 6c 45 78 70 72 2d 3e 73 70 61 6e 2e 7a  pColExpr->span.z
27c9b 3f 26 70 43 6f 6c 45 78 70 72 2d 3e 73 70 61 6e  ?&pColExpr->span
27c9c 3a 26 70 43 6f 6c 45 78 70 72 2d 3e 74 6f 6b 65  :&pColExpr->toke
27c9d 6e 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d  n);.        zNam
27c9e 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
27c9f 74 66 28 64 62 2c 20 22 25 54 22 2c 20 70 54 6f  tf(db, "%T", pTo
27ca0 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ken);.      }.  
27ca1 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e    }.    if( db->
27ca2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
27ca3 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
27ca4 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  ree(db, zName);.
27ca5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27ca6 20 7d 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20   }..    /* Make 
27ca7 73 75 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  sure the column 
27ca8 6e 61 6d 65 20 69 73 20 75 6e 69 71 75 65 2e 20  name is unique. 
27ca9 20 49 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20   If the name is 
27caa 6e 6f 74 20 75 6e 69 71 75 65 2c 0a 20 20 20 20  not unique,.    
27cab 2a 2a 20 61 70 70 65 6e 64 20 61 20 69 6e 74 65  ** append a inte
27cac 67 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20  ger to the name 
27cad 73 6f 20 74 68 61 74 20 69 74 20 62 65 63 6f 6d  so that it becom
27cae 65 73 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a  es unique..    *
27caf 2f 0a 20 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71  /.    nName = sq
27cb0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
27cb1 61 6d 65 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  ame);.    for(j=
27cb2 63 6e 74 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29  cnt=0; j<i; j++)
27cb3 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
27cb4 74 65 33 53 74 72 49 43 6d 70 28 61 43 6f 6c 5b  te3StrICmp(aCol[
27cb5 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29  j].zName, zName)
27cb6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ==0 ){.        c
27cb7 68 61 72 20 2a 7a 4e 65 77 4e 61 6d 65 3b 0a 20  har *zNewName;. 
27cb8 20 20 20 20 20 20 20 7a 4e 61 6d 65 5b 6e 4e 61         zName[nNa
27cb9 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  me] = 0;.       
27cba 20 7a 4e 65 77 4e 61 6d 65 20 3d 20 73 71 6c 69   zNewName = sqli
27cbb 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
27cbc 25 73 3a 25 64 22 2c 20 7a 4e 61 6d 65 2c 20 2b  %s:%d", zName, +
27cbd 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73  +cnt);.        s
27cbe 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
27cbf 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
27cc0 20 7a 4e 61 6d 65 20 3d 20 7a 4e 65 77 4e 61 6d   zName = zNewNam
27cc1 65 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 2d  e;.        j = -
27cc2 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  1;.        if( z
27cc3 4e 61 6d 65 3d 3d 30 20 29 20 62 72 65 61 6b 3b  Name==0 ) break;
27cc4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
27cc5 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d     pCol->zName =
27cc6 20 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 69 66   zName;.  }.  if
27cc7 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
27cc8 65 64 20 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d  ed ){.    for(j=
27cc9 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20  0; j<i; j++){.  
27cca 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
27ccb 65 28 64 62 2c 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e  e(db, aCol[j].zN
27ccc 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ame);.    }.    
27ccd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
27cce 2c 20 61 43 6f 6c 29 3b 0a 20 20 20 20 2a 70 61  , aCol);.    *pa
27ccf 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 2a 70 6e  Col = 0;.    *pn
27cd0 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  Col = 0;.    ret
27cd1 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
27cd2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
27cd3 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
27cd4 2a 2a 20 41 64 64 20 74 79 70 65 20 61 6e 64 20  ** Add type and 
27cd5 63 6f 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d  collation inform
27cd6 61 74 69 6f 6e 20 74 6f 20 61 20 63 6f 6c 75 6d  ation to a colum
27cd7 6e 20 6c 69 73 74 20 62 61 73 65 64 20 6f 6e 0a  n list based on.
27cd8 2a 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  ** a SELECT stat
27cd9 65 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  ement..** .** Th
27cda 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 70 72  e column list pr
27cdb 65 73 75 6d 61 62 6c 79 20 63 61 6d 65 20 66 72  esumably came fr
27cdc 6f 6d 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 4e  om selectColumnN
27cdd 61 6d 65 73 46 72 6f 6d 45 78 70 72 4c 69 73 74  amesFromExprList
27cde 28 29 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d  ()..** The colum
27cdf 6e 20 6c 69 73 74 20 68 61 73 20 6f 6e 6c 79 20  n list has only 
27ce0 6e 61 6d 65 73 2c 20 6e 6f 74 20 74 79 70 65 73  names, not types
27ce1 20 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e 20   or collations. 
27ce2 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   This.** routine
27ce3 20 67 6f 65 73 20 74 68 72 6f 75 67 68 20 61 6e   goes through an
27ce4 64 20 61 64 64 73 20 74 68 65 20 74 79 70 65 73  d adds the types
27ce5 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e   and collations.
27ce6 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
27ce7 69 6e 65 20 72 65 71 75 69 72 65 73 20 74 68 61  ine requires tha
27ce8 74 20 61 6c 6c 20 69 64 65 6e 74 69 66 69 65 72  t all identifier
27ce9 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a  s in the SELECT.
27cea 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 62 65 20  ** statement be 
27ceb 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61  resolved..*/.sta
27cec 74 69 63 20 76 6f 69 64 20 73 65 6c 65 63 74 41  tic void selectA
27ced 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43  ddColumnTypeAndC
27cee 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 50 61 72 73  ollation(.  Pars
27cef 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
27cf0 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
27cf1 74 65 78 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  texts */.  int n
27cf2 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Col,            
27cf3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
27cf4 6c 75 6d 6e 73 20 2a 2f 0a 20 20 43 6f 6c 75 6d  lumns */.  Colum
27cf5 6e 20 2a 61 43 6f 6c 2c 20 20 20 20 20 20 20 20  n *aCol,        
27cf6 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75   /* List of colu
27cf7 6d 6e 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  mns */.  Select 
27cf8 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f  *pSelect       /
27cf9 2a 20 53 45 4c 45 43 54 20 75 73 65 64 20 74 6f  * SELECT used to
27cfa 20 64 65 74 65 72 6d 69 6e 65 20 74 79 70 65 73   determine types
27cfb 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20   and collations 
27cfc 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
27cfd 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
27cfe 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
27cff 73 4e 43 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70  sNC;.  Column *p
27d00 43 6f 6c 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  Col;.  CollSeq *
27d01 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a  pColl;.  int i;.
27d02 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 73 74 72    Expr *p;.  str
27d03 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
27d04 6d 20 2a 61 3b 0a 0a 20 20 61 73 73 65 72 74 28  m *a;..  assert(
27d05 20 70 53 65 6c 65 63 74 21 3d 30 20 29 3b 0a 20   pSelect!=0 );. 
27d06 20 61 73 73 65 72 74 28 20 28 70 53 65 6c 65 63   assert( (pSelec
27d07 74 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  t->selFlags & SF
27d08 5f 52 65 73 6f 6c 76 65 64 29 21 3d 30 20 29 3b  _Resolved)!=0 );
27d09 0a 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 3d  .  assert( nCol=
27d0a 3d 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74  =pSelect->pEList
27d0b 2d 3e 6e 45 78 70 72 20 7c 7c 20 64 62 2d 3e 6d  ->nExpr || db->m
27d0c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
27d0d 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
27d0e 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
27d0f 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
27d10 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
27d11 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
27d12 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a   pSelect->pSrc;.
27d13 20 20 61 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70    a = pSelect->p
27d14 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 66 6f 72 28  EList->a;.  for(
27d15 69 3d 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20  i=0, pCol=aCol; 
27d16 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  i<nCol; i++, pCo
27d17 6c 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 61 5b  l++){.    p = a[
27d18 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 43  i].pExpr;.    pC
27d19 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69  ol->zType = sqli
27d1a 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
27d1b 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c  columnType(&sNC,
27d1c 20 70 2c 20 30 2c 20 30 2c 20 30 29 29 3b 0a 20   p, 0, 0, 0));. 
27d1d 20 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74     pCol->affinit
27d1e 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  y = sqlite3ExprA
27d1f 66 66 69 6e 69 74 79 28 70 29 3b 0a 20 20 20 20  ffinity(p);.    
27d20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
27d21 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
27d22 65 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70  e, p);.    if( p
27d23 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43  Coll ){.      pC
27d24 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69  ol->zColl = sqli
27d25 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
27d26 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pColl->zName);. 
27d27 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
27d28 2a 20 47 69 76 65 6e 20 61 20 53 45 4c 45 43 54  * Given a SELECT
27d29 20 73 74 61 74 65 6d 65 6e 74 2c 20 67 65 6e 65   statement, gene
27d2a 72 61 74 65 20 61 20 54 61 62 6c 65 20 73 74 72  rate a Table str
27d2b 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63  ucture that desc
27d2c 72 69 62 65 73 0a 2a 2a 20 74 68 65 20 72 65 73  ribes.** the res
27d2d 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 20  ult set of that 
27d2e 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54  SELECT..*/.SQLIT
27d2f 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20  E_PRIVATE Table 
27d30 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65  *sqlite3ResultSe
27d31 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 20  tOfSelect(Parse 
27d32 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20  *pParse, Select 
27d33 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 54 61 62  *pSelect){.  Tab
27d34 6c 65 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69  le *pTab;.  sqli
27d35 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
27d36 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 73 61 76 65  ->db;.  int save
27d37 64 46 6c 61 67 73 3b 0a 0a 20 20 73 61 76 65 64  dFlags;..  saved
27d38 46 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67  Flags = db->flag
27d39 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  s;.  db->flags &
27d3a 3d 20 7e 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f  = ~SQLITE_FullCo
27d3b 6c 4e 61 6d 65 73 3b 0a 20 20 64 62 2d 3e 66 6c  lNames;.  db->fl
27d3c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68  ags |= SQLITE_Sh
27d3d 6f 72 74 43 6f 6c 4e 61 6d 65 73 3b 0a 20 20 73  ortColNames;.  s
27d3e 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70  qlite3SelectPrep
27d3f 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
27d40 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 50 61 72  , 0);.  if( pPar
27d41 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72  se->nErr ) retur
27d42 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53  n 0;.  while( pS
27d43 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 20  elect->pPrior ) 
27d44 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
27d45 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 64 62 2d  t->pPrior;.  db-
27d46 3e 66 6c 61 67 73 20 3d 20 73 61 76 65 64 46 6c  >flags = savedFl
27d47 61 67 73 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  ags;.  pTab = sq
27d48 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
27d49 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62  o(db, sizeof(Tab
27d4a 6c 65 29 20 29 3b 0a 20 20 69 66 28 20 70 54 61  le) );.  if( pTa
27d4b 62 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  b==0 ){.    retu
27d4c 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62  rn 0;.  }.  pTab
27d4d 2d 3e 64 62 4d 65 6d 20 3d 20 64 62 2d 3e 6c 6f  ->dbMem = db->lo
27d4e 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
27d4f 20 3f 20 64 62 20 3a 20 30 3b 0a 20 20 70 54 61   ? db : 0;.  pTa
27d50 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70  b->nRef = 1;.  p
27d51 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a  Tab->zName = 0;.
27d52 20 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46    selectColumnsF
27d53 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72  romExprList(pPar
27d54 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  se, pSelect->pEL
27d55 69 73 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f 6c  ist, &pTab->nCol
27d56 2c 20 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a  , &pTab->aCol);.
27d57 20 20 73 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d    selectAddColum
27d58 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f  nTypeAndCollatio
27d59 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e  n(pParse, pTab->
27d5a 6e 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  nCol, pTab->aCol
27d5b 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 70 54  , pSelect);.  pT
27d5c 61 62 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a  ab->iPKey = -1;.
27d5d 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
27d5e 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71  Failed ){.    sq
27d5f 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
27d60 28 70 54 61 62 29 3b 0a 20 20 20 20 72 65 74 75  (pTab);.    retu
27d61 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 0;.  }.  retu
27d62 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pTab;.}../*.*
27d63 2a 20 47 65 74 20 61 20 56 44 42 45 20 66 6f 72  * Get a VDBE for
27d64 20 74 68 65 20 67 69 76 65 6e 20 70 61 72 73 65   the given parse
27d65 72 20 63 6f 6e 74 65 78 74 2e 20 20 43 72 65 61  r context.  Crea
27d66 74 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20  te a new one if 
27d67 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 49 66  necessary..** If
27d68 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
27d69 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 61 6e  , return NULL an
27d6a 64 20 6c 65 61 76 65 20 61 20 6d 65 73 73 61 67  d leave a messag
27d6b 65 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a  e in pParse..*/.
27d6c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
27d6d 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56  dbe *sqlite3GetV
27d6e 64 62 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  dbe(Parse *pPars
27d6f 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  e){.  Vdbe *v = 
27d70 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
27d71 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20   if( v==0 ){.   
27d72 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64   v = pParse->pVd
27d73 62 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  be = sqlite3Vdbe
27d74 43 72 65 61 74 65 28 70 50 61 72 73 65 2d 3e 64  Create(pParse->d
27d75 62 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  b);.#ifndef SQLI
27d76 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20  TE_OMIT_TRACE.  
27d77 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20    if( v ){.     
27d78 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27d79 70 30 28 76 2c 20 4f 50 5f 54 72 61 63 65 29 3b  p0(v, OP_Trace);
27d7a 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
27d7b 7d 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a  }.  return v;.}.
27d7c 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ../*.** Compute 
27d7d 74 68 65 20 69 4c 69 6d 69 74 20 61 6e 64 20 69  the iLimit and i
27d7e 4f 66 66 73 65 74 20 66 69 65 6c 64 73 20 6f 66  Offset fields of
27d7f 20 74 68 65 20 53 45 4c 45 43 54 20 62 61 73 65   the SELECT base
27d80 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 70 4c 69 6d  d on the.** pLim
27d81 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74 20 65  it and pOffset e
27d82 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 70 4c 69  xpressions.  pLi
27d83 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74 20  mit and pOffset 
27d84 68 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73  hold the express
27d85 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 70 70  ions.** that app
27d86 65 61 72 20 69 6e 20 74 68 65 20 6f 72 69 67 69  ear in the origi
27d87 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  nal SQL statemen
27d88 74 20 61 66 74 65 72 20 74 68 65 20 4c 49 4d 49  t after the LIMI
27d89 54 20 61 6e 64 20 4f 46 46 53 45 54 0a 2a 2a 20  T and OFFSET.** 
27d8a 6b 65 79 77 6f 72 64 73 2e 20 20 4f 72 20 4e 55  keywords.  Or NU
27d8b 4c 4c 20 69 66 20 74 68 6f 73 65 20 6b 65 79 77  LL if those keyw
27d8c 6f 72 64 73 20 61 72 65 20 6f 6d 69 74 74 65 64  ords are omitted
27d8d 2e 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66  . iLimit and iOf
27d8e 66 73 65 74 20 0a 2a 2a 20 61 72 65 20 74 68 65  fset .** are the
27d8f 20 69 6e 74 65 67 65 72 20 6d 65 6d 6f 72 79 20   integer memory 
27d90 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73  register numbers
27d91 20 66 6f 72 20 63 6f 75 6e 74 65 72 73 20 75 73   for counters us
27d92 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 0a 2a  ed to compute .*
27d93 2a 20 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20  * the limit and 
27d94 6f 66 66 73 65 74 2e 20 20 49 66 20 74 68 65 72  offset.  If ther
27d95 65 20 69 73 20 6e 6f 20 6c 69 6d 69 74 20 61 6e  e is no limit an
27d96 64 2f 6f 72 20 6f 66 66 73 65 74 2c 20 74 68 65  d/or offset, the
27d97 6e 20 0a 2a 2a 20 69 4c 69 6d 69 74 20 61 6e 64  n .** iLimit and
27d98 20 69 4f 66 66 73 65 74 20 61 72 65 20 6e 65 67   iOffset are neg
27d99 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ative..**.** Thi
27d9a 73 20 72 6f 75 74 69 6e 65 20 63 68 61 6e 67 65  s routine change
27d9b 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  s the values of 
27d9c 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73  iLimit and iOffs
27d9d 65 74 20 6f 6e 6c 79 20 69 66 0a 2a 2a 20 61 20  et only if.** a 
27d9e 6c 69 6d 69 74 20 6f 72 20 6f 66 66 73 65 74 20  limit or offset 
27d9f 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 70 4c  is defined by pL
27da0 69 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74  imit and pOffset
27da1 2e 20 20 69 4c 69 6d 69 74 20 61 6e 64 0a 2a 2a  .  iLimit and.**
27da2 20 69 4f 66 66 73 65 74 20 73 68 6f 75 6c 64 20   iOffset should 
27da3 68 61 76 65 20 62 65 65 6e 20 70 72 65 73 65 74  have been preset
27da4 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20   to appropriate 
27da5 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73 0a 2a  default values.*
27da6 2a 20 28 75 73 75 61 6c 6c 79 20 62 75 74 20 6e  * (usually but n
27da7 6f 74 20 61 6c 77 61 79 73 20 2d 31 29 20 70 72  ot always -1) pr
27da8 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74  ior to calling t
27da9 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20  his routine..** 
27daa 4f 6e 6c 79 20 69 66 20 70 4c 69 6d 69 74 21 3d  Only if pLimit!=
27dab 30 20 6f 72 20 70 4f 66 66 73 65 74 21 3d 30 20  0 or pOffset!=0 
27dac 64 6f 20 74 68 65 20 6c 69 6d 69 74 20 72 65 67  do the limit reg
27dad 69 73 74 65 72 73 20 67 65 74 0a 2a 2a 20 72 65  isters get.** re
27dae 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 55 4e  defined.  The UN
27daf 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72  ION ALL operator
27db0 20 75 73 65 73 20 74 68 69 73 20 70 72 6f 70 65   uses this prope
27db1 72 74 79 20 74 6f 20 66 6f 72 63 65 0a 2a 2a 20  rty to force.** 
27db2 74 68 65 20 72 65 75 73 65 20 6f 66 20 74 68 65  the reuse of the
27db3 20 73 61 6d 65 20 6c 69 6d 69 74 20 61 6e 64 20   same limit and 
27db4 6f 66 66 73 65 74 20 72 65 67 69 73 74 65 72 73  offset registers
27db5 20 61 63 72 6f 73 73 20 6d 75 6c 74 69 70 6c 65   across multiple
27db6 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65  .** SELECT state
27db7 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ments..*/.static
27db8 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4c 69 6d   void computeLim
27db9 69 74 52 65 67 69 73 74 65 72 73 28 50 61 72 73  itRegisters(Pars
27dba 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63  e *pParse, Selec
27dbb 74 20 2a 70 2c 20 69 6e 74 20 69 42 72 65 61 6b  t *p, int iBreak
27dbc 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30  ){.  Vdbe *v = 0
27dbd 3b 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 20 3d  ;.  int iLimit =
27dbe 20 30 3b 0a 20 20 69 6e 74 20 69 4f 66 66 73 65   0;.  int iOffse
27dbf 74 3b 0a 20 20 69 6e 74 20 61 64 64 72 31 3b 0a  t;.  int addr1;.
27dc0 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20    if( p->iLimit 
27dc1 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20  ) return;..  /* 
27dc2 0a 20 20 2a 2a 20 22 4c 49 4d 49 54 20 2d 31 22  .  ** "LIMIT -1"
27dc3 20 61 6c 77 61 79 73 20 73 68 6f 77 73 20 61 6c   always shows al
27dc4 6c 20 72 6f 77 73 2e 20 20 54 68 65 72 65 20 69  l rows.  There i
27dc5 73 20 73 6f 6d 65 0a 20 20 2a 2a 20 63 6f 6e 74  s some.  ** cont
27dc6 72 61 76 65 72 73 79 20 61 62 6f 75 74 20 77 68  raversy about wh
27dc7 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 62  at the correct b
27dc8 65 68 61 76 69 6f 72 20 73 68 6f 75 6c 64 20 62  ehavior should b
27dc9 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 75 72 72  e..  ** The curr
27dca 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ent implementati
27dcb 6f 6e 20 69 6e 74 65 72 70 72 65 74 73 20 22 4c  on interprets "L
27dcc 49 4d 49 54 20 30 22 20 74 6f 20 6d 65 61 6e 0a  IMIT 0" to mean.
27dcd 20 20 2a 2a 20 6e 6f 20 72 6f 77 73 2e 0a 20 20    ** no rows..  
27dce 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  */.  sqlite3Expr
27dcf 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
27dd0 65 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69  e);.  if( p->pLi
27dd1 6d 69 74 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4c  mit ){.    p->iL
27dd2 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74 20 3d 20  imit = iLimit = 
27dd3 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
27dd4 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
27dd5 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
27dd6 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72      if( v==0 ) r
27dd7 65 74 75 72 6e 3b 0a 20 20 20 20 73 71 6c 69 74  eturn;.    sqlit
27dd8 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
27dd9 65 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 69 4c  e, p->pLimit, iL
27dda 69 6d 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  imit);.    sqlit
27ddb 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
27ddc 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4c  OP_MustBeInt, iL
27ddd 69 6d 69 74 29 3b 0a 20 20 20 20 56 64 62 65 43  imit);.    VdbeC
27dde 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49  omment((v, "LIMI
27ddf 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20  T counter"));.  
27de0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27de1 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f  Op2(v, OP_IfZero
27de2 2c 20 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b  , iLimit, iBreak
27de3 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
27de4 70 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 70  pOffset ){.    p
27de5 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 66  ->iOffset = iOff
27de6 73 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  set = ++pParse->
27de7 6e 4d 65 6d 3b 0a 20 20 20 20 69 66 28 20 70 2d  nMem;.    if( p-
27de8 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20  >pLimit ){.     
27de9 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b   pParse->nMem++;
27dea 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61     /* Allocate a
27deb 6e 20 65 78 74 72 61 20 72 65 67 69 73 74 65 72  n extra register
27dec 20 66 6f 72 20 6c 69 6d 69 74 2b 6f 66 66 73 65   for limit+offse
27ded 74 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 76  t */.    }.    v
27dee 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
27def 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69  e(pParse);.    i
27df0 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
27df1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
27df2 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d  rCode(pParse, p-
27df3 3e 70 4f 66 66 73 65 74 2c 20 69 4f 66 66 73 65  >pOffset, iOffse
27df4 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
27df5 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
27df6 4d 75 73 74 42 65 49 6e 74 2c 20 69 4f 66 66 73  MustBeInt, iOffs
27df7 65 74 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  et);.    VdbeCom
27df8 6d 65 6e 74 28 28 76 2c 20 22 4f 46 46 53 45 54  ment((v, "OFFSET
27df9 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20 20   counter"));.   
27dfa 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33   addr1 = sqlite3
27dfb 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
27dfc 5f 49 66 50 6f 73 2c 20 69 4f 66 66 73 65 74 29  _IfPos, iOffset)
27dfd 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27dfe 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
27dff 74 65 67 65 72 2c 20 30 2c 20 69 4f 66 66 73 65  teger, 0, iOffse
27e00 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
27e01 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
27e02 64 64 72 31 29 3b 0a 20 20 20 20 69 66 28 20 70  ddr1);.    if( p
27e03 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  ->pLimit ){.    
27e04 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27e05 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c 20 69  Op3(v, OP_Add, i
27e06 4c 69 6d 69 74 2c 20 69 4f 66 66 73 65 74 2c 20  Limit, iOffset, 
27e07 69 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20 20 20  iOffset+1);.    
27e08 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
27e09 2c 20 22 4c 49 4d 49 54 2b 4f 46 46 53 45 54 22  , "LIMIT+OFFSET"
27e0a 29 29 3b 0a 20 20 20 20 20 20 61 64 64 72 31 20  ));.      addr1 
27e0b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
27e0c 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c  Op1(v, OP_IfPos,
27e0d 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20   iLimit);.      
27e0e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27e0f 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
27e10 20 2d 31 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b   -1, iOffset+1);
27e11 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
27e12 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
27e13 64 72 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  dr1);.    }.  }.
27e14 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
27e15 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
27e16 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 52 65 74  SELECT./*.** Ret
27e17 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69  urn the appropri
27e18 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ate collating se
27e19 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 69  quence for the i
27e1a 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  Col-th column of
27e1b 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73  .** the result s
27e1c 65 74 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f  et for the compo
27e1d 75 6e 64 2d 73 65 6c 65 63 74 20 73 74 61 74 65  und-select state
27e1e 6d 65 6e 74 20 22 70 22 2e 20 20 52 65 74 75 72  ment "p".  Retur
27e1f 6e 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20 74 68 65  n NULL if.** the
27e20 20 63 6f 6c 75 6d 6e 20 68 61 73 20 6e 6f 20 64   column has no d
27e21 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67  efault collating
27e22 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a   sequence..**.**
27e23 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   The collating s
27e24 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20  equence for the 
27e25 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20  compound select 
27e26 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68  is taken from th
27e27 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74  e.** left-most t
27e28 65 72 6d 20 6f 66 20 74 68 65 20 73 65 6c 65 63  erm of the selec
27e29 74 20 74 68 61 74 20 68 61 73 20 61 20 63 6f 6c  t that has a col
27e2a 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e  lating sequence.
27e2b 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53  .*/.static CollS
27e2c 65 71 20 2a 6d 75 6c 74 69 53 65 6c 65 63 74 43  eq *multiSelectC
27e2d 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50  ollSeq(Parse *pP
27e2e 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c  arse, Select *p,
27e2f 20 69 6e 74 20 69 43 6f 6c 29 7b 0a 20 20 43 6f   int iCol){.  Co
27e30 6c 6c 53 65 71 20 2a 70 52 65 74 3b 0a 20 20 69  llSeq *pRet;.  i
27e31 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a  f( p->pPrior ){.
27e32 20 20 20 20 70 52 65 74 20 3d 20 6d 75 6c 74 69      pRet = multi
27e33 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50  SelectCollSeq(pP
27e34 61 72 73 65 2c 20 70 2d 3e 70 50 72 69 6f 72 2c  arse, p->pPrior,
27e35 20 69 43 6f 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b   iCol);.  }else{
27e36 0a 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a 20  .    pRet = 0;. 
27e37 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f   }.  assert( iCo
27e38 6c 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 52  l>=0 );.  if( pR
27e39 65 74 3d 3d 30 20 26 26 20 69 43 6f 6c 3c 70 2d  et==0 && iCol<p-
27e3a 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  >pEList->nExpr )
27e3b 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 73 71 6c  {.    pRet = sql
27e3c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
27e3d 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69 73  pParse, p->pELis
27e3e 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72  t->a[iCol].pExpr
27e3f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
27e40 70 52 65 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  pRet;.}.#endif /
27e41 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  * SQLITE_OMIT_CO
27e42 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f  MPOUND_SELECT */
27e43 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66  ../* Forward ref
27e44 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63  erence */.static
27e45 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c 65 63 74   int multiSelect
27e46 4f 72 64 65 72 42 79 28 0a 20 20 50 61 72 73 65  OrderBy(.  Parse
27e47 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
27e48 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
27e49 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
27e4a 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  *p,            /
27e4b 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  * The right-most
27e4c 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f 20 62   of SELECTs to b
27e4d 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c  e coded */.  Sel
27e4e 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20  ectDest *pDest  
27e4f 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f     /* What to do
27e50 20 77 69 74 68 20 71 75 65 72 79 20 72 65 73 75   with query resu
27e51 6c 74 73 20 2a 2f 0a 29 3b 0a 0a 0a 23 69 66 6e  lts */.);...#ifn
27e52 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27e53 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
27e54 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
27e55 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
27e56 70 72 6f 63 65 73 73 20 61 20 63 6f 6d 70 6f 75  process a compou
27e57 6e 64 20 71 75 65 72 79 20 66 6f 72 6d 20 66 72  nd query form fr
27e58 6f 6d 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72  om.** two or mor
27e59 65 20 73 65 70 61 72 61 74 65 20 71 75 65 72 69  e separate queri
27e5a 65 73 20 75 73 69 6e 67 20 55 4e 49 4f 4e 2c 20  es using UNION, 
27e5b 55 4e 49 4f 4e 20 41 4c 4c 2c 20 45 58 43 45 50  UNION ALL, EXCEP
27e5c 54 2c 20 6f 72 0a 2a 2a 20 49 4e 54 45 52 53 45  T, or.** INTERSE
27e5d 43 54 0a 2a 2a 0a 2a 2a 20 22 70 22 20 70 6f 69  CT.**.** "p" poi
27e5e 6e 74 73 20 74 6f 20 74 68 65 20 72 69 67 68 74  nts to the right
27e5f 2d 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 77 6f  -most of the two
27e60 20 71 75 65 72 69 65 73 2e 20 20 74 68 65 20 71   queries.  the q
27e61 75 65 72 79 20 6f 6e 20 74 68 65 0a 2a 2a 20 6c  uery on the.** l
27e62 65 66 74 20 69 73 20 70 2d 3e 70 50 72 69 6f 72  eft is p->pPrior
27e63 2e 20 20 54 68 65 20 6c 65 66 74 20 71 75 65 72  .  The left quer
27e64 79 20 63 6f 75 6c 64 20 61 6c 73 6f 20 62 65 20  y could also be 
27e65 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79  a compound query
27e66 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63 61 73  .** in which cas
27e67 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  e this routine w
27e68 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 72 65  ill be called re
27e69 63 75 72 73 69 76 65 6c 79 2e 20 0a 2a 2a 0a 2a  cursively. .**.*
27e6a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f 66  * The results of
27e6b 20 74 68 65 20 74 6f 74 61 6c 20 71 75 65 72 79   the total query
27e6c 20 61 72 65 20 74 6f 20 62 65 20 77 72 69 74 74   are to be writt
27e6d 65 6e 20 69 6e 74 6f 20 61 20 64 65 73 74 69 6e  en into a destin
27e6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 79 70 65  ation.** of type
27e6f 20 65 44 65 73 74 20 77 69 74 68 20 70 61 72 61   eDest with para
27e70 6d 65 74 65 72 20 69 50 61 72 6d 2e 0a 2a 2a 0a  meter iParm..**.
27e71 2a 2a 20 45 78 61 6d 70 6c 65 20 31 3a 20 20 43  ** Example 1:  C
27e72 6f 6e 73 69 64 65 72 20 61 20 74 68 72 65 65 2d  onsider a three-
27e73 77 61 79 20 63 6f 6d 70 6f 75 6e 64 20 53 51 4c  way compound SQL
27e74 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
27e75 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 20 46  *     SELECT a F
27e76 52 4f 4d 20 74 31 20 55 4e 49 4f 4e 20 53 45 4c  ROM t1 UNION SEL
27e77 45 43 54 20 62 20 46 52 4f 4d 20 74 32 20 55 4e  ECT b FROM t2 UN
27e78 49 4f 4e 20 53 45 4c 45 43 54 20 63 20 46 52 4f  ION SELECT c FRO
27e79 4d 20 74 33 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  M t3.**.** This 
27e7a 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72  statement is par
27e7b 73 65 64 20 75 70 20 61 73 20 66 6f 6c 6c 6f 77  sed up as follow
27e7c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c  s:.**.**     SEL
27e7d 45 43 54 20 63 20 46 52 4f 4d 20 74 33 0a 2a 2a  ECT c FROM t3.**
27e7e 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20        |.**      
27e7f 60 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43 54 20  `----->  SELECT 
27e80 62 20 46 52 4f 4d 20 74 32 0a 2a 2a 20 20 20 20  b FROM t2.**    
27e81 20 20 20 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a              |.**
27e82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27e83 60 2d 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43 54  `------>  SELECT
27e84 20 61 20 46 52 4f 4d 20 74 31 0a 2a 2a 0a 2a 2a   a FROM t1.**.**
27e85 20 54 68 65 20 61 72 72 6f 77 73 20 69 6e 20 74   The arrows in t
27e86 68 65 20 64 69 61 67 72 61 6d 20 61 62 6f 76 65  he diagram above
27e87 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 53   represent the S
27e88 65 6c 65 63 74 2e 70 50 72 69 6f 72 20 70 6f 69  elect.pPrior poi
27e89 6e 74 65 72 2e 0a 2a 2a 20 53 6f 20 69 66 20 74  nter..** So if t
27e8a 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
27e8b 61 6c 6c 65 64 20 77 69 74 68 20 70 20 65 71 75  alled with p equ
27e8c 61 6c 20 74 6f 20 74 68 65 20 74 33 20 71 75 65  al to the t3 que
27e8d 72 79 2c 20 74 68 65 6e 0a 2a 2a 20 70 50 72 69  ry, then.** pPri
27e8e 6f 72 20 77 69 6c 6c 20 62 65 20 74 68 65 20 74  or will be the t
27e8f 32 20 71 75 65 72 79 2e 20 20 70 2d 3e 6f 70 20  2 query.  p->op 
27e90 77 69 6c 6c 20 62 65 20 54 4b 5f 55 4e 49 4f 4e  will be TK_UNION
27e91 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a   in this case..*
27e92 2a 0a 2a 2a 20 4e 6f 74 69 63 65 20 74 68 61 74  *.** Notice that
27e93 20 62 65 63 61 75 73 65 20 6f 66 20 74 68 65 20   because of the 
27e94 77 61 79 20 53 51 4c 69 74 65 20 70 61 72 73 65  way SQLite parse
27e95 73 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  s compound SELEC
27e96 54 73 2c 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76  Ts, the.** indiv
27e97 69 64 75 61 6c 20 73 65 6c 65 63 74 73 20 61 6c  idual selects al
27e98 77 61 79 73 20 67 72 6f 75 70 20 66 72 6f 6d 20  ways group from 
27e99 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2e 0a 2a  left to right..*
27e9a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c  /.static int mul
27e9b 74 69 53 65 6c 65 63 74 28 0a 20 20 50 61 72 73  tiSelect(.  Pars
27e9c 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
27e9d 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
27e9e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
27e9f 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
27ea0 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73  /* The right-mos
27ea1 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f 20  t of SELECTs to 
27ea2 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65  be coded */.  Se
27ea3 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20  lectDest *pDest 
27ea4 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64      /* What to d
27ea5 6f 20 77 69 74 68 20 71 75 65 72 79 20 72 65 73  o with query res
27ea6 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ults */.){.  int
27ea7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
27ea8 20 20 20 2f 2a 20 53 75 63 63 65 73 73 20 63 6f     /* Success co
27ea9 64 65 20 66 72 6f 6d 20 61 20 73 75 62 72 6f 75  de from a subrou
27eaa 74 69 6e 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74  tine */.  Select
27eab 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20   *pPrior;       
27eac 2f 2a 20 41 6e 6f 74 68 65 72 20 53 45 4c 45 43  /* Another SELEC
27ead 54 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  T immediately to
27eae 20 6f 75 72 20 6c 65 66 74 20 2a 2f 0a 20 20 56   our left */.  V
27eaf 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
27eb0 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
27eb1 20 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 44   code to this VD
27eb2 42 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  BE */.  SelectDe
27eb3 73 74 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a  st dest;      /*
27eb4 20 41 6c 74 65 72 6e 61 74 69 76 65 20 64 61 74   Alternative dat
27eb5 61 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f  a destination */
27eb6 0a 20 20 53 65 6c 65 63 74 20 2a 70 44 65 6c 65  .  Select *pDele
27eb7 74 65 20 3d 20 30 3b 20 20 2f 2a 20 43 68 61 69  te = 0;  /* Chai
27eb8 6e 20 6f 66 20 73 69 6d 70 6c 65 20 73 65 6c 65  n of simple sele
27eb9 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  cts to delete */
27eba 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
27ebb 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
27ebc 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
27ebd 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  */..  /* Make su
27ebe 72 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f  re there is no O
27ebf 52 44 45 52 20 42 59 20 6f 72 20 4c 49 4d 49 54  RDER BY or LIMIT
27ec0 20 63 6c 61 75 73 65 20 6f 6e 20 70 72 69 6f 72   clause on prior
27ec1 20 53 45 4c 45 43 54 73 2e 20 20 4f 6e 6c 79 0a   SELECTs.  Only.
27ec2 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 28 72    ** the last (r
27ec3 69 67 68 74 2d 6d 6f 73 74 29 20 53 45 4c 45 43  ight-most) SELEC
27ec4 54 20 69 6e 20 74 68 65 20 73 65 72 69 65 73 20  T in the series 
27ec5 6d 61 79 20 68 61 76 65 20 61 6e 20 4f 52 44 45  may have an ORDE
27ec6 52 20 42 59 20 6f 72 20 4c 49 4d 49 54 2e 0a 20  R BY or LIMIT.. 
27ec7 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 20   */.  assert( p 
27ec8 26 26 20 70 2d 3e 70 50 72 69 6f 72 20 29 3b 20  && p->pPrior ); 
27ec9 20 2f 2a 20 43 61 6c 6c 69 6e 67 20 66 75 6e 63   /* Calling func
27eca 74 69 6f 6e 20 67 75 61 72 61 6e 74 65 65 73 20  tion guarantees 
27ecb 74 68 69 73 20 6d 75 63 68 20 2a 2f 0a 20 20 64  this much */.  d
27ecc 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
27ecd 20 20 70 50 72 69 6f 72 20 3d 20 70 2d 3e 70 50    pPrior = p->pP
27ece 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20  rior;.  assert( 
27ecf 70 50 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f  pPrior->pRightmo
27ed0 73 74 21 3d 70 50 72 69 6f 72 20 29 3b 0a 20 20  st!=pPrior );.  
27ed1 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e  assert( pPrior->
27ed2 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 2d 3e 70  pRightmost==p->p
27ed3 52 69 67 68 74 6d 6f 73 74 20 29 3b 0a 20 20 64  Rightmost );.  d
27ed4 65 73 74 20 3d 20 2a 70 44 65 73 74 3b 0a 20 20  est = *pDest;.  
27ed5 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64  if( pPrior->pOrd
27ed6 65 72 42 79 20 29 7b 0a 20 20 20 20 73 71 6c 69  erBy ){.    sqli
27ed7 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
27ed8 73 65 2c 22 4f 52 44 45 52 20 42 59 20 63 6c 61  se,"ORDER BY cla
27ed9 75 73 65 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20  use should come 
27eda 61 66 74 65 72 20 25 73 20 6e 6f 74 20 62 65 66  after %s not bef
27edb 6f 72 65 22 2c 0a 20 20 20 20 20 20 73 65 6c 65  ore",.      sele
27edc 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29  ctOpName(p->op))
27edd 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20  ;.    rc = 1;.  
27ede 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
27edf 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 69  ect_end;.  }.  i
27ee0 66 28 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69  f( pPrior->pLimi
27ee1 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
27ee2 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
27ee3 22 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 73 68  "LIMIT clause sh
27ee4 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 65 72 20  ould come after 
27ee5 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 22 2c 0a  %s not before",.
27ee6 20 20 20 20 20 20 73 65 6c 65 63 74 4f 70 4e 61        selectOpNa
27ee7 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20 20  me(p->op));.    
27ee8 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f  rc = 1;.    goto
27ee9 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e   multi_select_en
27eea 64 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71  d;.  }..  v = sq
27eeb 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
27eec 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  rse);.  assert( 
27eed 76 21 3d 30 20 29 3b 20 20 2f 2a 20 54 68 65 20  v!=0 );  /* The 
27eee 56 44 42 45 20 61 6c 72 65 61 64 79 20 63 72 65  VDBE already cre
27eef 61 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 20  ated by calling 
27ef0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f  function */..  /
27ef1 2a 20 43 72 65 61 74 65 20 74 68 65 20 64 65 73  * Create the des
27ef2 74 69 6e 61 74 69 6f 6e 20 74 65 6d 70 6f 72 61  tination tempora
27ef3 72 79 20 74 61 62 6c 65 20 69 66 20 6e 65 63 65  ry table if nece
27ef4 73 73 61 72 79 0a 20 20 2a 2f 0a 20 20 69 66 28  ssary.  */.  if(
27ef5 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 53 52 54   dest.eDest==SRT
27ef6 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a 20 20 20  _EphemTab ){.   
27ef7 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69   assert( p->pELi
27ef8 73 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  st );.    sqlite
27ef9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27efa 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
27efb 20 64 65 73 74 2e 69 50 61 72 6d 2c 20 70 2d 3e   dest.iParm, p->
27efc 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a  pEList->nExpr);.
27efd 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d      dest.eDest =
27efe 20 53 52 54 5f 54 61 62 6c 65 3b 0a 20 20 7d 0a   SRT_Table;.  }.
27eff 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
27f00 61 6c 6c 20 53 45 4c 45 43 54 73 20 69 6e 20 74  all SELECTs in t
27f01 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 76  he statement hav
27f02 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65  e the same numbe
27f03 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 0a 20 20  r of elements.  
27f04 2a 2a 20 69 6e 20 74 68 65 69 72 20 72 65 73 75  ** in their resu
27f05 6c 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a 20 20  lt sets..  */.  
27f06 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73  assert( p->pELis
27f07 74 20 26 26 20 70 50 72 69 6f 72 2d 3e 70 45 4c  t && pPrior->pEL
27f08 69 73 74 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  ist );.  if( p->
27f09 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70  pEList->nExpr!=p
27f0a 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 2d 3e 6e  Prior->pEList->n
27f0b 45 78 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  Expr ){.    sqli
27f0c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
27f0d 73 65 2c 20 22 53 45 4c 45 43 54 73 20 74 6f 20  se, "SELECTs to 
27f0e 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69 67  the left and rig
27f0f 68 74 20 6f 66 20 25 73 22 0a 20 20 20 20 20 20  ht of %s".      
27f10 22 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 68  " do not have th
27f11 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66  e same number of
27f12 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 22   result columns"
27f13 2c 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70  , selectOpName(p
27f14 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d  ->op));.    rc =
27f15 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c   1;.    goto mul
27f16 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
27f17 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 6f 75 6e   }..  /* Compoun
27f18 64 20 53 45 4c 45 43 54 73 20 74 68 61 74 20 68  d SELECTs that h
27f19 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ave an ORDER BY 
27f1a 63 6c 61 75 73 65 20 61 72 65 20 68 61 6e 64 6c  clause are handl
27f1b 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 20  ed separately.. 
27f1c 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72   */.  if( p->pOr
27f1d 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65 74  derBy ){.    ret
27f1e 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f  urn multiSelectO
27f1f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c 20 70  rderBy(pParse, p
27f20 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a 20  , pDest);.  }.. 
27f21 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
27f22 65 20 66 6f 72 20 74 68 65 20 6c 65 66 74 20 61  e for the left a
27f23 6e 64 20 72 69 67 68 74 20 53 45 4c 45 43 54 20  nd right SELECT 
27f24 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f  statements..  */
27f25 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70  .  switch( p->op
27f26 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
27f27 41 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ALL: {.      int
27f28 20 61 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20   addr = 0;.     
27f29 20 61 73 73 65 72 74 28 20 21 70 50 72 69 6f 72   assert( !pPrior
27f2a 2d 3e 70 4c 69 6d 69 74 20 29 3b 0a 20 20 20 20  ->pLimit );.    
27f2b 20 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74    pPrior->pLimit
27f2c 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20   = p->pLimit;.  
27f2d 20 20 20 20 70 50 72 69 6f 72 2d 3e 70 4f 66 66      pPrior->pOff
27f2e 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74  set = p->pOffset
27f2f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
27f30 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
27f31 65 2c 20 70 50 72 69 6f 72 2c 20 26 64 65 73 74  e, pPrior, &dest
27f32 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  );.      p->pLim
27f33 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  it = 0;.      p-
27f34 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  >pOffset = 0;.  
27f35 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
27f36 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69        goto multi
27f37 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20  _select_end;.   
27f38 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70 50     }.      p->pP
27f39 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  rior = 0;.      
27f3a 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 70 50 72 69  p->iLimit = pPri
27f3b 6f 72 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20  or->iLimit;.    
27f3c 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 70    p->iOffset = p
27f3d 50 72 69 6f 72 2d 3e 69 4f 66 66 73 65 74 3b 0a  Prior->iOffset;.
27f3e 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 4c 69        if( p->iLi
27f3f 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61  mit ){.        a
27f40 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
27f41 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
27f42 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 29  Zero, p->iLimit)
27f43 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f  ;.        VdbeCo
27f44 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d 70 20  mment((v, "Jump 
27f45 61 68 65 61 64 20 69 66 20 4c 49 4d 49 54 20 72  ahead if LIMIT r
27f46 65 61 63 68 65 64 22 29 29 3b 0a 20 20 20 20 20  eached"));.     
27f47 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   }.      rc = sq
27f48 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
27f49 73 65 2c 20 70 2c 20 26 64 65 73 74 29 3b 0a 20  se, p, &dest);. 
27f4a 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70       pDelete = p
27f4b 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
27f4c 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  p->pPrior = pPri
27f4d 6f 72 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  or;.      if( rc
27f4e 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
27f4f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e   multi_select_en
27f50 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  d;.      }.     
27f51 20 69 66 28 20 61 64 64 72 20 29 7b 0a 20 20 20   if( addr ){.   
27f52 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27f53 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
27f54 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
27f55 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
27f56 20 20 63 61 73 65 20 54 4b 5f 45 58 43 45 50 54    case TK_EXCEPT
27f57 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4e  :.    case TK_UN
27f58 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ION: {.      int
27f59 20 75 6e 69 6f 6e 54 61 62 3b 20 20 20 20 2f 2a   unionTab;    /*
27f5a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   Cursor number o
27f5b 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  f the temporary 
27f5c 74 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 72 65  table holding re
27f5d 73 75 6c 74 20 2a 2f 0a 20 20 20 20 20 20 75 38  sult */.      u8
27f5e 20 6f 70 20 3d 20 30 3b 20 20 20 20 20 20 20 2f   op = 0;       /
27f5f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 52 54  * One of the SRT
27f60 5f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f 20  _ operations to 
27f61 61 70 70 6c 79 20 74 6f 20 73 65 6c 66 20 2a 2f  apply to self */
27f62 0a 20 20 20 20 20 20 69 6e 74 20 70 72 69 6f 72  .      int prior
27f63 4f 70 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 53  Op;     /* The S
27f64 52 54 5f 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f  RT_ operation to
27f65 20 61 70 70 6c 79 20 74 6f 20 70 72 69 6f 72 20   apply to prior 
27f66 73 65 6c 65 63 74 73 20 2a 2f 0a 20 20 20 20 20  selects */.     
27f67 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a   Expr *pLimit, *
27f68 70 4f 66 66 73 65 74 3b 20 2f 2a 20 53 61 76 65  pOffset; /* Save
27f69 64 20 76 61 6c 75 65 73 20 6f 66 20 70 2d 3e 6e  d values of p->n
27f6a 4c 69 6d 69 74 20 61 6e 64 20 70 2d 3e 6e 4f 66  Limit and p->nOf
27f6b 66 73 65 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e  fset */.      in
27f6c 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 53 65  t addr;.      Se
27f6d 6c 65 63 74 44 65 73 74 20 75 6e 69 6f 6e 64 65  lectDest unionde
27f6e 73 74 3b 0a 0a 20 20 20 20 20 20 70 72 69 6f 72  st;..      prior
27f6f 4f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a  Op = SRT_Union;.
27f70 20 20 20 20 20 20 69 66 28 20 64 65 73 74 2e 65        if( dest.e
27f71 44 65 73 74 3d 3d 70 72 69 6f 72 4f 70 20 26 26  Dest==priorOp &&
27f72 20 41 4c 57 41 59 53 28 21 70 2d 3e 70 4c 69 6d   ALWAYS(!p->pLim
27f73 69 74 20 26 26 21 70 2d 3e 70 4f 66 66 73 65 74  it &&!p->pOffset
27f74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
27f75 57 65 20 63 61 6e 20 72 65 75 73 65 20 61 20 74  We can reuse a t
27f76 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 67  emporary table g
27f77 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 53 45  enerated by a SE
27f78 4c 45 43 54 20 74 6f 20 6f 75 72 0a 20 20 20 20  LECT to our.    
27f79 20 20 20 20 2a 2a 20 72 69 67 68 74 2e 0a 20 20      ** right..  
27f7a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
27f7b 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67   assert( p->pRig
27f7c 68 74 6d 6f 73 74 21 3d 70 20 29 3b 20 20 2f 2a  htmost!=p );  /*
27f7d 20 43 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e   Can only happen
27f7e 20 66 6f 72 20 6c 65 66 74 77 61 72 64 20 65 6c   for leftward el
27f7f 65 6d 65 6e 74 73 0a 20 20 20 20 20 20 20 20 20  ements.         
27f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f81 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f              ** o
27f82 66 20 61 20 33 2d 77 61 79 20 6f 72 20 6d 6f 72  f a 3-way or mor
27f83 65 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20  e compound */.  
27f84 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
27f85 3e 70 4c 69 6d 69 74 3d 3d 30 20 29 3b 20 20 20  >pLimit==0 );   
27f86 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65     /* Not allowe
27f87 64 20 6f 6e 20 6c 65 66 74 77 61 72 64 20 65 6c  d on leftward el
27f88 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20  ements */.      
27f89 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 66    assert( p->pOf
27f8a 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 20 20 2f  fset==0 );     /
27f8b 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e  * Not allowed on
27f8c 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e   leftward elemen
27f8d 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e  ts */.        un
27f8e 69 6f 6e 54 61 62 20 3d 20 64 65 73 74 2e 69 50  ionTab = dest.iP
27f8f 61 72 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  arm;.      }else
27f90 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20  {.        /* We 
27f91 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 72 65  will need to cre
27f92 61 74 65 20 6f 75 72 20 6f 77 6e 20 74 65 6d 70  ate our own temp
27f93 6f 72 61 72 79 20 74 61 62 6c 65 20 74 6f 20 68  orary table to h
27f94 6f 6c 64 20 74 68 65 0a 20 20 20 20 20 20 20 20  old the.        
27f95 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ** intermediate 
27f96 72 65 73 75 6c 74 73 2e 0a 20 20 20 20 20 20 20  results..       
27f97 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 69 6f   */.        unio
27f98 6e 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e  nTab = pParse->n
27f99 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61  Tab++;.        a
27f9a 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64 65 72  ssert( p->pOrder
27f9b 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  By==0 );.       
27f9c 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
27f9d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27f9e 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 75  OpenEphemeral, u
27f9f 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20 20  nionTab, 0);.   
27fa0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
27fa1 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20  addrOpenEphm[0] 
27fa2 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 20  == -1 );.       
27fa3 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
27fa4 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20 20  [0] = addr;.    
27fa5 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73      p->pRightmos
27fa6 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  t->selFlags |= S
27fa7 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b  F_UsesEphemeral;
27fa8 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
27fa9 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20   p->pEList );.  
27faa 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
27fab 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54 20  Code the SELECT 
27fac 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 6f 75  statements to ou
27fad 72 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2f 0a  r left.      */.
27fae 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
27faf 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20  Prior->pOrderBy 
27fb0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27fb1 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
27fb2 75 6e 69 6f 6e 64 65 73 74 2c 20 70 72 69 6f 72  uniondest, prior
27fb3 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 29 3b 0a 20  Op, unionTab);. 
27fb4 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
27fb5 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
27fb6 70 50 72 69 6f 72 2c 20 26 75 6e 69 6f 6e 64 65  pPrior, &unionde
27fb7 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  st);.      if( r
27fb8 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  c ){.        got
27fb9 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
27fba 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  nd;.      }..   
27fbb 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 63     /* Code the c
27fbc 75 72 72 65 6e 74 20 53 45 4c 45 43 54 20 73 74  urrent SELECT st
27fbd 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2f  atement.      */
27fbe 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f 70  .      if( p->op
27fbf 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29 7b 0a 20  ==TK_EXCEPT ){. 
27fc0 20 20 20 20 20 20 20 6f 70 20 3d 20 53 52 54 5f         op = SRT_
27fc1 45 78 63 65 70 74 3b 0a 20 20 20 20 20 20 7d 65  Except;.      }e
27fc2 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
27fc3 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55  ert( p->op==TK_U
27fc4 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 20 20  NION );.        
27fc5 6f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a  op = SRT_Union;.
27fc6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
27fc7 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20  >pPrior = 0;.   
27fc8 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70     pLimit = p->p
27fc9 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
27fca 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
27fcb 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70    pOffset = p->p
27fcc 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d  Offset;.      p-
27fcd 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  >pOffset = 0;.  
27fce 20 20 20 20 75 6e 69 6f 6e 64 65 73 74 2e 65 44      uniondest.eD
27fcf 65 73 74 20 3d 20 6f 70 3b 0a 20 20 20 20 20 20  est = op;.      
27fd0 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  rc = sqlite3Sele
27fd1 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26 75  ct(pParse, p, &u
27fd2 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20 20  niondest);.     
27fd3 20 2f 2a 20 51 75 65 72 79 20 66 6c 61 74 74 65   /* Query flatte
27fd4 6e 69 6e 67 20 69 6e 20 73 71 6c 69 74 65 33 53  ning in sqlite3S
27fd5 65 6c 65 63 74 28 29 20 6d 69 67 68 74 20 72 65  elect() might re
27fd6 66 69 6c 6c 20 70 2d 3e 70 4f 72 64 65 72 42 79  fill p->pOrderBy
27fd7 2e 0a 20 20 20 20 20 20 2a 2a 20 42 65 20 73 75  ..      ** Be su
27fd8 72 65 20 74 6f 20 64 65 6c 65 74 65 20 70 2d 3e  re to delete p->
27fd9 70 4f 72 64 65 72 42 79 2c 20 74 68 65 72 65 66  pOrderBy, theref
27fda 6f 72 65 2c 20 74 6f 20 61 76 6f 69 64 20 61 20  ore, to avoid a 
27fdb 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e 20 2a 2f 0a  memory leak. */.
27fdc 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
27fdd 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
27fde 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20  p->pOrderBy);.  
27fdf 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70 2d      pDelete = p-
27fe0 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70  >pPrior;.      p
27fe1 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f  ->pPrior = pPrio
27fe2 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 72 64  r;.      p->pOrd
27fe3 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20  erBy = 0;.      
27fe4 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
27fe5 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29  e(db, p->pLimit)
27fe6 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ;.      p->pLimi
27fe7 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20  t = pLimit;.    
27fe8 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70    p->pOffset = p
27fe9 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d  Offset;.      p-
27fea 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20  >iLimit = 0;.   
27feb 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20     p->iOffset = 
27fec 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  0;.      if( rc 
27fed 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
27fee 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
27fef 3b 0a 20 20 20 20 20 20 7d 0a 0a 0a 20 20 20 20  ;.      }...    
27ff0 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65    /* Convert the
27ff1 20 64 61 74 61 20 69 6e 20 74 68 65 20 74 65 6d   data in the tem
27ff2 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69 6e 74  porary table int
27ff3 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72 6d 0a  o whatever form.
27ff4 20 20 20 20 20 20 2a 2a 20 69 74 20 69 73 20 74        ** it is t
27ff5 68 61 74 20 77 65 20 63 75 72 72 65 6e 74 6c 79  hat we currently
27ff6 20 6e 65 65 64 2e 0a 20 20 20 20 20 20 2a 2f 20   need..      */ 
27ff7 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20       .      if( 
27ff8 64 65 73 74 2e 65 44 65 73 74 21 3d 70 72 69 6f  dest.eDest!=prio
27ff9 72 4f 70 20 7c 7c 20 75 6e 69 6f 6e 54 61 62 21  rOp || unionTab!
27ffa 3d 64 65 73 74 2e 69 50 61 72 6d 20 29 7b 0a 20  =dest.iParm ){. 
27ffb 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6e 74         int iCont
27ffc 2c 20 69 42 72 65 61 6b 2c 20 69 53 74 61 72 74  , iBreak, iStart
27ffd 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
27ffe 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20  ( p->pEList );. 
27fff 20 20 20 20 20 20 20 69 66 28 20 64 65 73 74 2e         if( dest.
28000 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
28001 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 53  t ){.          S
28002 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20  elect *pFirst = 
28003 70 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69  p;.          whi
28004 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69  le( pFirst->pPri
28005 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70 46  or ) pFirst = pF
28006 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20  irst->pPrior;.  
28007 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74 65          generate
28008 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
28009 73 65 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70  se, 0, pFirst->p
2800a 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20  EList);.        
2800b 7d 0a 20 20 20 20 20 20 20 20 69 42 72 65 61 6b  }.        iBreak
2800c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2800d 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
2800e 20 20 20 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69      iCont = sqli
2800f 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
28010 28 76 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d  (v);.        com
28011 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65  puteLimitRegiste
28012 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69 42  rs(pParse, p, iB
28013 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73  reak);.        s
28014 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28015 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 75  (v, OP_Rewind, u
28016 6e 69 6f 6e 54 61 62 2c 20 69 42 72 65 61 6b 29  nionTab, iBreak)
28017 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61 72 74  ;.        iStart
28018 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
28019 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
2801a 20 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65        selectInne
2801b 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c  rLoop(pParse, p,
2801c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 75 6e 69 6f   p->pEList, unio
2801d 6e 54 61 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d  nTab, p->pEList-
2801e 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20  >nExpr,.        
2801f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28020 30 2c 20 2d 31 2c 20 26 64 65 73 74 2c 20 69 43  0, -1, &dest, iC
28021 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20  ont, iBreak);.  
28022 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28023 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
28024 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20 20   iCont);.       
28025 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28026 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 75  p2(v, OP_Next, u
28027 6e 69 6f 6e 54 61 62 2c 20 69 53 74 61 72 74 29  nionTab, iStart)
28028 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
28029 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2802a 6c 28 76 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20  l(v, iBreak);.  
2802b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2802c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
2802d 6f 73 65 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30  ose, unionTab, 0
2802e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2802f 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
28030 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 52 53    case TK_INTERS
28031 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ECT: {.      int
28032 20 74 61 62 31 2c 20 74 61 62 32 3b 0a 20 20 20   tab1, tab2;.   
28033 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42     int iCont, iB
28034 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20  reak, iStart;.  
28035 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74      Expr *pLimit
28036 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20 20 20 20  , *pOffset;.    
28037 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20    int addr;.    
28038 20 20 53 65 6c 65 63 74 44 65 73 74 20 69 6e 74    SelectDest int
28039 65 72 73 65 63 74 64 65 73 74 3b 0a 20 20 20 20  ersectdest;.    
2803a 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 20 20 20    int r1;..     
2803b 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20 69 73   /* INTERSECT is
2803c 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
2803d 74 68 65 20 6f 74 68 65 72 73 20 73 69 6e 63 65  the others since
2803e 20 69 74 20 72 65 71 75 69 72 65 73 0a 20 20 20   it requires.   
2803f 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d 70 6f 72     ** two tempor
28040 61 72 79 20 74 61 62 6c 65 73 2e 20 20 48 65 6e  ary tables.  Hen
28041 63 65 20 69 74 20 68 61 73 20 69 74 73 20 6f 77  ce it has its ow
28042 6e 20 63 61 73 65 2e 20 20 42 65 67 69 6e 0a 20  n case.  Begin. 
28043 20 20 20 20 20 2a 2a 20 62 79 20 61 6c 6c 6f 63       ** by alloc
28044 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65 73  ating the tables
28045 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 2e 0a 20   we will need.. 
28046 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 74 61       */.      ta
28047 62 31 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  b1 = pParse->nTa
28048 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61 62 32 20  b++;.      tab2 
28049 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2804a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2804b 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  p->pOrderBy==0 )
2804c 3b 0a 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  ;..      addr = 
2804d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2804e 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  2(v, OP_OpenEphe
2804f 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20 30 29 3b  meral, tab1, 0);
28050 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
28051 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30  ->addrOpenEphm[0
28052 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20  ] == -1 );.     
28053 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
28054 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20 20  [0] = addr;.    
28055 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d    p->pRightmost-
28056 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f  >selFlags |= SF_
28057 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20  UsesEphemeral;. 
28058 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
28059 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20 20 20  pEList );..     
2805a 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45 4c   /* Code the SEL
2805b 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65 66 74  ECTs to our left
2805c 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79 20   into temporary 
2805d 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a 20 20  table "tab1"..  
2805e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
2805f 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
28060 69 74 28 26 69 6e 74 65 72 73 65 63 74 64 65 73  it(&intersectdes
28061 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74 61  t, SRT_Union, ta
28062 62 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  b1);.      rc = 
28063 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
28064 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 69  arse, pPrior, &i
28065 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b 0a 20  ntersectdest);. 
28066 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
28067 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74         goto mult
28068 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
28069 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
2806a 43 6f 64 65 20 74 68 65 20 63 75 72 72 65 6e 74  Code the current
2806b 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74 65 6d   SELECT into tem
2806c 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22 74 61  porary table "ta
2806d 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  b2".      */.   
2806e 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
2806f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28070 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
28071 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20 20 20   tab2, 0);.     
28072 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72   assert( p->addr
28073 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20 2d  OpenEphm[1] == -
28074 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64  1 );.      p->ad
28075 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20  drOpenEphm[1] = 
28076 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70  addr;.      p->p
28077 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20  Prior = 0;.     
28078 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69   pLimit = p->pLi
28079 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c  mit;.      p->pL
2807a 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  imit = 0;.      
2807b 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66  pOffset = p->pOf
2807c 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70  fset;.      p->p
2807d 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20  Offset = 0;.    
2807e 20 20 69 6e 74 65 72 73 65 63 74 64 65 73 74 2e    intersectdest.
2807f 69 50 61 72 6d 20 3d 20 74 61 62 32 3b 0a 20 20  iParm = tab2;.  
28080 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
28081 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
28082 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65 73 74  , &intersectdest
28083 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65  );.      pDelete
28084 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
28085 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20      p->pPrior = 
28086 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 73 71  pPrior;.      sq
28087 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
28088 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a  db, p->pLimit);.
28089 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20        p->pLimit 
2808a 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20  = pLimit;.      
2808b 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66  p->pOffset = pOf
2808c 66 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20  fset;.      if( 
2808d 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  rc ){.        go
2808e 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
2808f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  end;.      }..  
28090 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
28091 63 6f 64 65 20 74 6f 20 74 61 6b 65 20 74 68 65  code to take the
28092 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6f 66   intersection of
28093 20 74 68 65 20 74 77 6f 20 74 65 6d 70 6f 72 61   the two tempora
28094 72 79 0a 20 20 20 20 20 20 2a 2a 20 74 61 62 6c  ry.      ** tabl
28095 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  es..      */.   
28096 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45     assert( p->pE
28097 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20 69 66  List );.      if
28098 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 53 52  ( dest.eDest==SR
28099 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20  T_Output ){.    
2809a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 46 69 72      Select *pFir
2809b 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  st = p;.        
2809c 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d 3e 70  while( pFirst->p
2809d 50 72 69 6f 72 20 29 20 70 46 69 72 73 74 20 3d  Prior ) pFirst =
2809e 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b   pFirst->pPrior;
2809f 0a 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74  .        generat
280a0 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  eColumnNames(pPa
280a1 72 73 65 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e  rse, 0, pFirst->
280a2 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 7d  pEList);.      }
280a3 0a 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d 20  .      iBreak = 
280a4 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
280a5 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69  abel(v);.      i
280a6 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Cont = sqlite3Vd
280a7 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
280a8 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c 69 6d        computeLim
280a9 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72  itRegisters(pPar
280aa 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29 3b 0a  se, p, iBreak);.
280ab 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
280ac 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
280ad 77 69 6e 64 2c 20 74 61 62 31 2c 20 69 42 72 65  wind, tab1, iBre
280ae 61 6b 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  ak);.      r1 = 
280af 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
280b0 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
280b1 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74 65   iStart = sqlite
280b2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
280b3 50 5f 52 6f 77 4b 65 79 2c 20 74 61 62 31 2c 20  P_RowKey, tab1, 
280b4 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r1);.      sqlit
280b5 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
280b6 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 62  OP_NotFound, tab
280b7 32 2c 20 69 43 6f 6e 74 2c 20 72 31 29 3b 0a 20  2, iCont, r1);. 
280b8 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
280b9 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
280ba 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 65  e, r1);.      se
280bb 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50  lectInnerLoop(pP
280bc 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69  arse, p, p->pELi
280bd 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e 70 45 4c  st, tab1, p->pEL
280be 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20  ist->nExpr,.    
280bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
280c0 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74 2c 20    0, -1, &dest, 
280c1 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0a  iCont, iBreak);.
280c2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
280c3 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
280c4 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20 73   iCont);.      s
280c5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
280c6 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74 61 62  (v, OP_Next, tab
280c7 31 2c 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20  1, iStart);.    
280c8 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
280c9 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42 72  olveLabel(v, iBr
280ca 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  eak);.      sqli
280cb 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
280cc 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 32 2c   OP_Close, tab2,
280cd 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
280ce 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
280cf 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 31 2c 20  OP_Close, tab1, 
280d0 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  0);.      break;
280d1 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
280d2 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c 61 74 69   Compute collati
280d3 6e 67 20 73 65 71 75 65 6e 63 65 73 20 75 73 65  ng sequences use
280d4 64 20 62 79 20 0a 20 20 2a 2a 20 74 65 6d 70 6f  d by .  ** tempo
280d5 72 61 72 79 20 74 61 62 6c 65 73 20 6e 65 65 64  rary tables need
280d6 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
280d7 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  the compound sel
280d8 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74 61 63 68  ect..  ** Attach
280d9 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72   the KeyInfo str
280da 75 63 74 75 72 65 20 74 6f 20 61 6c 6c 20 74 65  ucture to all te
280db 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a  mporary tables..
280dc 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 73    **.  ** This s
280dd 65 63 74 69 6f 6e 20 69 73 20 72 75 6e 20 62 79  ection is run by
280de 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
280df 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
280e0 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 45 4c 45   only..  ** SELE
280e1 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  CT statements to
280e2 20 74 68 65 20 6c 65 66 74 20 61 6c 77 61 79 73   the left always
280e3 20 73 6b 69 70 20 74 68 69 73 20 70 61 72 74 2e   skip this part.
280e4 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74    The right-most
280e5 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 6d 69 67  .  ** SELECT mig
280e6 68 74 20 61 6c 73 6f 20 73 6b 69 70 20 74 68 69  ht also skip thi
280e7 73 20 70 61 72 74 20 69 66 20 69 74 20 68 61 73  s part if it has
280e8 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 63 6c 61   no ORDER BY cla
280e9 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20 6e 6f 20  use and.  ** no 
280ea 74 65 6d 70 20 74 61 62 6c 65 73 20 61 72 65 20  temp tables are 
280eb 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2f 0a 20  required..  */. 
280ec 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73   if( p->selFlags
280ed 20 26 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65   & SF_UsesEpheme
280ee 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ral ){.    int i
280ef 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
280f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
280f1 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
280f2 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
280f3 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  o;            /*
280f4 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
280f5 6e 63 65 20 66 6f 72 20 74 68 65 20 72 65 73 75  nce for the resu
280f6 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 53 65  lt set */.    Se
280f7 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20 20 20 20  lect *pLoop;    
280f8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
280f9 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75  or looping throu
280fa 67 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  gh SELECT statem
280fb 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c  ents */.    Coll
280fc 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b 20 20 20  Seq **apColl;   
280fd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
280fe 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68   looping through
280ff 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c   pKeyInfo->aColl
28100 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 43  [] */.    int nC
28101 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
28102 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
28103 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
28104 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20  result set */.. 
28105 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52     assert( p->pR
28106 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29 3b 0a 20  ightmost==p );. 
28107 20 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e 70 45 4c     nCol = p->pEL
28108 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  ist->nExpr;.    
28109 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74  pKeyInfo = sqlit
2810a 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2810b 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
2810c 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
2810d 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e 43 6f 6c  (*pKeyInfo)+nCol
2810e 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  *(sizeof(CollSeq
2810f 2a 29 20 2b 20 31 29 29 3b 0a 20 20 20 20 69 66  *) + 1));.    if
28110 28 20 21 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20  ( !pKeyInfo ){. 
28111 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
28112 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f  _NOMEM;.      go
28113 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
28114 65 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  end;.    }..    
28115 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20  pKeyInfo->enc = 
28116 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70 4b 65  ENC(db);.    pKe
28117 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20  yInfo->nField = 
28118 28 75 31 36 29 6e 43 6f 6c 3b 0a 0a 20 20 20 20  (u16)nCol;..    
28119 66 6f 72 28 69 3d 30 2c 20 61 70 43 6f 6c 6c 3d  for(i=0, apColl=
2811a 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b  pKeyInfo->aColl;
2811b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 61 70   i<nCol; i++, ap
2811c 43 6f 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 2a  Coll++){.      *
2811d 61 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65  apColl = multiSe
2811e 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61 72  lectCollSeq(pPar
2811f 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20 20 20 20  se, p, i);.     
28120 20 69 66 28 20 30 3d 3d 2a 61 70 43 6f 6c 6c 20   if( 0==*apColl 
28121 29 7b 0a 20 20 20 20 20 20 20 20 2a 61 70 43 6f  ){.        *apCo
28122 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
28123 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
28124 7d 0a 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70  }..    for(pLoop
28125 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70  =p; pLoop; pLoop
28126 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 29 7b  =pLoop->pPrior){
28127 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
28128 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<2; i++){.     
28129 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20 70 4c     int addr = pL
2812a 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  oop->addrOpenEph
2812b 6d 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  m[i];.        if
2812c 28 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 20 20  ( addr<0 ){.    
2812d 20 20 20 20 20 20 2f 2a 20 49 66 20 5b 30 5d 20        /* If [0] 
2812e 69 73 20 75 6e 75 73 65 64 20 74 68 65 6e 20 5b  is unused then [
2812f 31 5d 20 69 73 20 61 6c 73 6f 20 75 6e 75 73 65  1] is also unuse
28130 64 2e 20 20 53 6f 20 77 65 20 63 61 6e 0a 20 20  d.  So we can.  
28131 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79          ** alway
28132 73 20 73 61 66 65 6c 79 20 61 62 6f 72 74 20 61  s safely abort a
28133 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 66 69  s soon as the fi
28134 72 73 74 20 75 6e 75 73 65 64 20 73 6c 6f 74 20  rst unused slot 
28135 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 20 20  is found */.    
28136 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c        assert( pL
28137 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  oop->addrOpenEph
28138 6d 5b 31 5d 3c 30 20 29 3b 0a 20 20 20 20 20 20  m[1]<0 );.      
28139 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2813a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
2813b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
2813c 28 76 2c 20 61 64 64 72 2c 20 6e 43 6f 6c 29 3b  (v, addr, nCol);
2813d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2813e 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2813f 61 64 64 72 2c 20 28 63 68 61 72 2a 29 70 4b 65  addr, (char*)pKe
28140 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
28141 4f 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f  O);.        pLoo
28142 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
28143 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d  i] = -1;.      }
28144 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
28145 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4b 65  e3DbFree(db, pKe
28146 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a 6d 75 6c  yInfo);.  }..mul
28147 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 20  ti_select_end:. 
28148 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 64   pDest->iMem = d
28149 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 70 44 65 73  est.iMem;.  pDes
2814a 74 2d 3e 6e 4d 65 6d 20 3d 20 64 65 73 74 2e 6e  t->nMem = dest.n
2814b 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  Mem;.  sqlite3Se
2814c 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2814d 44 65 6c 65 74 65 29 3b 0a 20 20 72 65 74 75 72  Delete);.  retur
2814e 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  n rc;.}.#endif /
2814f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  * SQLITE_OMIT_CO
28150 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f  MPOUND_SELECT */
28151 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20  ../*.** Code an 
28152 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e  output subroutin
28153 65 20 66 6f 72 20 61 20 63 6f 72 6f 75 74 69 6e  e for a coroutin
28154 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
28155 20 6f 66 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20   of a.** SELECT 
28156 73 74 61 74 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  statment..**.** 
28157 54 68 65 20 64 61 74 61 20 74 6f 20 62 65 20 6f  The data to be o
28158 75 74 70 75 74 20 69 73 20 63 6f 6e 74 61 69 6e  utput is contain
28159 65 64 20 69 6e 20 70 49 6e 2d 3e 69 4d 65 6d 2e  ed in pIn->iMem.
2815a 20 20 54 68 65 72 65 20 61 72 65 0a 2a 2a 20 70    There are.** p
2815b 49 6e 2d 3e 6e 4d 65 6d 20 63 6f 6c 75 6d 6e 73  In->nMem columns
2815c 20 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 20 20   to be output.  
2815d 70 44 65 73 74 20 69 73 20 77 68 65 72 65 20 74  pDest is where t
2815e 68 65 20 6f 75 74 70 75 74 20 73 68 6f 75 6c 64  he output should
2815f 0a 2a 2a 20 62 65 20 73 65 6e 74 2e 0a 2a 2a 0a  .** be sent..**.
28160 2a 2a 20 72 65 67 52 65 74 75 72 6e 20 69 73 20  ** regReturn is 
28161 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  the number of th
28162 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69  e register holdi
28163 6e 67 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  ng the subroutin
28164 65 0a 2a 2a 20 72 65 74 75 72 6e 20 61 64 64 72  e.** return addr
28165 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65  ess..**.** If re
28166 67 50 72 65 76 3e 30 20 74 68 65 6e 20 69 74 20  gPrev>0 then it 
28167 69 73 20 61 20 74 68 65 20 66 69 72 73 74 20 72  is a the first r
28168 65 67 69 73 74 65 72 20 69 6e 20 61 20 76 65 63  egister in a vec
28169 74 6f 72 20 74 68 61 74 0a 2a 2a 20 72 65 63 6f  tor that.** reco
2816a 72 64 73 20 74 68 65 20 70 72 65 76 69 6f 75 73  rds the previous
2816b 20 6f 75 74 70 75 74 2e 20 20 6d 65 6d 5b 72 65   output.  mem[re
2816c 67 50 72 65 76 5d 20 69 73 20 61 20 66 6c 61 67  gPrev] is a flag
2816d 20 74 68 61 74 20 69 73 20 66 61 6c 73 65 0a 2a   that is false.*
2816e 2a 20 69 66 20 74 68 65 72 65 20 68 61 73 20 62  * if there has b
2816f 65 65 6e 20 6e 6f 20 70 72 65 76 69 6f 75 73 20  een no previous 
28170 6f 75 74 70 75 74 2e 20 20 49 66 20 72 65 67 50  output.  If regP
28171 72 65 76 3e 30 20 74 68 65 6e 20 63 6f 64 65 20  rev>0 then code 
28172 69 73 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20  is.** generated 
28173 74 6f 20 73 75 70 70 72 65 73 73 20 64 75 70 6c  to suppress dupl
28174 69 63 61 74 65 73 2e 20 20 70 4b 65 79 49 6e 66  icates.  pKeyInf
28175 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20 63 6f  o is used for co
28176 6d 70 61 72 69 6e 67 0a 2a 2a 20 6b 65 79 73 2e  mparing.** keys.
28177 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4c 49  .**.** If the LI
28178 4d 49 54 20 66 6f 75 6e 64 20 69 6e 20 70 2d 3e  MIT found in p->
28179 69 4c 69 6d 69 74 20 69 73 20 72 65 61 63 68 65  iLimit is reache
2817a 64 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74  d, jump immediat
2817b 65 6c 79 20 74 6f 0a 2a 2a 20 69 42 72 65 61 6b  ely to.** iBreak
2817c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2817d 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74 53 75  generateOutputSu
2817e 62 72 6f 75 74 69 6e 65 28 0a 20 20 50 61 72 73  broutine(.  Pars
2817f 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
28180 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
28181 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
28182 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
28183 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
28184 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  T statement */. 
28185 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 49 6e   SelectDest *pIn
28186 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 72 6f  ,        /* Coro
28187 75 74 69 6e 65 20 73 75 70 70 6c 79 69 6e 67 20  utine supplying 
28188 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74  data */.  Select
28189 44 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20 20  Dest *pDest,    
2818a 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 73 65    /* Where to se
2818b 6e 64 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  nd the data */. 
2818c 20 69 6e 74 20 72 65 67 52 65 74 75 72 6e 2c 20   int regReturn, 
2818d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2818e 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 72  return address r
2818f 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74  egister */.  int
28190 20 72 65 67 50 72 65 76 2c 20 20 20 20 20 20 20   regPrev,       
28191 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73       /* Previous
28192 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65 72   result register
28193 2e 20 20 4e 6f 20 75 6e 69 71 75 65 6e 65 73 73  .  No uniqueness
28194 20 69 66 20 30 20 2a 2f 0a 20 20 4b 65 79 49 6e   if 0 */.  KeyIn
28195 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20  fo *pKeyInfo,   
28196 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 72     /* For compar
28197 69 6e 67 20 77 69 74 68 20 70 72 65 76 69 6f 75  ing with previou
28198 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74  s entry */.  int
28199 20 70 34 74 79 70 65 2c 20 20 20 20 20 20 20 20   p4type,        
2819a 20 20 20 20 20 2f 2a 20 54 68 65 20 70 34 20 74       /* The p4 t
2819b 79 70 65 20 66 6f 72 20 70 4b 65 79 49 6e 66 6f  ype for pKeyInfo
2819c 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b   */.  int iBreak
2819d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2819e 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 77 65   Jump here if we
2819f 20 68 69 74 20 74 68 65 20 4c 49 4d 49 54 20 2a   hit the LIMIT *
281a0 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
281a1 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
281a2 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b    int iContinue;
281a3 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 0a 20 20  .  int addr;..  
281a4 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
281a5 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
281a6 3b 0a 20 20 69 43 6f 6e 74 69 6e 75 65 20 3d 20  ;.  iContinue = 
281a7 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
281a8 61 62 65 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 53  abel(v);..  /* S
281a9 75 70 70 72 65 73 73 20 64 75 70 6c 69 63 61 74  uppress duplicat
281aa 65 73 20 66 6f 72 20 55 4e 49 4f 4e 2c 20 45 58  es for UNION, EX
281ab 43 45 50 54 2c 20 61 6e 64 20 49 4e 54 45 52 53  CEPT, and INTERS
281ac 45 43 54 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ECT .  */.  if( 
281ad 72 65 67 50 72 65 76 20 29 7b 0a 20 20 20 20 69  regPrev ){.    i
281ae 6e 74 20 6a 31 2c 20 6a 32 3b 0a 20 20 20 20 6a  nt j1, j2;.    j
281af 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
281b0 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 4e 6f  ddOp1(v, OP_IfNo
281b1 74 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20 20  t, regPrev);.   
281b2 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j2 = sqlite3Vdb
281b3 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f  eAddOp4(v, OP_Co
281b4 6d 70 61 72 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d  mpare, pIn->iMem
281b5 2c 20 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e  , regPrev+1, pIn
281b6 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20  ->nMem,.        
281b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
281b8 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
281b9 79 49 6e 66 6f 2c 20 70 34 74 79 70 65 29 3b 0a  yInfo, p4type);.
281ba 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
281bb 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70  ddOp3(v, OP_Jump
281bc 2c 20 6a 32 2b 32 2c 20 69 43 6f 6e 74 69 6e 75  , j2+2, iContinu
281bd 65 2c 20 6a 32 2b 32 29 3b 0a 20 20 20 20 73 71  e, j2+2);.    sq
281be 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
281bf 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71  e(v, j1);.    sq
281c0 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70  lite3ExprCodeCop
281c1 79 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69  y(pParse, pIn->i
281c2 4d 65 6d 2c 20 72 65 67 50 72 65 76 2b 31 2c 20  Mem, regPrev+1, 
281c3 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  pIn->nMem);.    
281c4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
281c5 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
281c6 20 31 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20   1, regPrev);.  
281c7 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  }.  if( pParse->
281c8 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
281c9 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20   ) return 0;..  
281ca 2f 2a 20 53 75 70 70 72 65 73 73 20 74 68 65 20  /* Suppress the 
281cb 74 68 65 20 66 69 72 73 74 20 4f 46 46 53 45 54  the first OFFSET
281cc 20 65 6e 74 72 69 65 73 20 69 66 20 74 68 65 72   entries if ther
281cd 65 20 69 73 20 61 6e 20 4f 46 46 53 45 54 20 63  e is an OFFSET c
281ce 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20 63 6f 64  lause.  */.  cod
281cf 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69 43  eOffset(v, p, iC
281d0 6f 6e 74 69 6e 75 65 29 3b 0a 0a 20 20 73 77 69  ontinue);..  swi
281d1 74 63 68 28 20 70 44 65 73 74 2d 3e 65 44 65 73  tch( pDest->eDes
281d2 74 20 29 7b 0a 20 20 20 20 2f 2a 20 53 74 6f 72  t ){.    /* Stor
281d3 65 20 74 68 65 20 72 65 73 75 6c 74 20 61 73 20  e the result as 
281d4 64 61 74 61 20 75 73 69 6e 67 20 61 20 75 6e 69  data using a uni
281d5 71 75 65 20 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a  que key..    */.
281d6 20 20 20 20 63 61 73 65 20 53 52 54 5f 54 61 62      case SRT_Tab
281d7 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54  le:.    case SRT
281d8 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20  _EphemTab: {.   
281d9 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69     int r1 = sqli
281da 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
281db 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 6e 74  arse);.      int
281dc 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r2 = sqlite3Get
281dd 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
281de 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
281df 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
281e0 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d 3e  akeRecord, pIn->
281e1 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 2c  iMem, pIn->nMem,
281e2 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
281e3 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
281e4 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 70 44   OP_NewRowid, pD
281e5 65 73 74 2d 3e 69 50 61 72 6d 2c 20 72 32 29 3b  est->iParm, r2);
281e6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
281e7 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
281e8 6e 73 65 72 74 2c 20 70 44 65 73 74 2d 3e 69 50  nsert, pDest->iP
281e9 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b 0a 20 20  arm, r1, r2);.  
281ea 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
281eb 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
281ec 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20  G_APPEND);.     
281ed 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
281ee 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
281ef 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
281f0 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
281f1 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
281f2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
281f3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
281f4 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20  OMIT_SUBQUERY.  
281f5 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63    /* If we are c
281f6 72 65 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f  reating a set fo
281f7 72 20 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53  r an "expr IN (S
281f8 45 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73  ELECT ...)" cons
281f9 74 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68  truct,.    ** th
281fa 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20  en there should 
281fb 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d  be a single item
281fc 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20   on the stack.  
281fd 57 72 69 74 65 20 74 68 69 73 0a 20 20 20 20 2a  Write this.    *
281fe 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68 65 20  * item into the 
281ff 73 65 74 20 74 61 62 6c 65 20 77 69 74 68 20 62  set table with b
28200 6f 67 75 73 20 64 61 74 61 2e 0a 20 20 20 20 2a  ogus data..    *
28201 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53  /.    case SRT_S
28202 65 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  et: {.      int 
28203 72 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  r1;.      assert
28204 28 20 70 49 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29  ( pIn->nMem==1 )
28205 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 69 6e  ;.      p->affin
28206 69 74 79 20 3d 20 0a 20 20 20 20 20 20 20 20 20  ity = .         
28207 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
28208 66 69 6e 69 74 79 28 70 2d 3e 70 45 4c 69 73 74  finity(p->pEList
28209 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 70 44  ->a[0].pExpr, pD
2820a 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 29 3b 0a  est->affinity);.
2820b 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
2820c 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2820d 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rse);.      sqli
2820e 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2820f 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
28210 70 49 6e 2d 3e 69 4d 65 6d 2c 20 31 2c 20 72 31  pIn->iMem, 1, r1
28211 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20  , &p->affinity, 
28212 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
28213 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
28214 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
28215 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a   pIn->iMem, 1);.
28216 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28217 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64  eAddOp2(v, OP_Id
28218 78 49 6e 73 65 72 74 2c 20 70 44 65 73 74 2d 3e  xInsert, pDest->
28219 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20  iParm, r1);.    
2821a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2821b 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2821c 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  r1);.      break
2821d 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 20 20  ;.    }..#if 0  
2821e 2f 2a 20 4e 65 76 65 72 20 6f 63 63 75 72 73 20  /* Never occurs 
2821f 6f 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 71  on an ORDER BY q
28220 75 65 72 79 20 2a 2f 0a 20 20 20 20 2f 2a 20 49  uery */.    /* I
28221 66 20 61 6e 79 20 72 6f 77 20 65 78 69 73 74 20  f any row exist 
28222 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
28223 74 2c 20 72 65 63 6f 72 64 20 74 68 61 74 20 66  t, record that f
28224 61 63 74 20 61 6e 64 20 61 62 6f 72 74 2e 0a 20  act and abort.. 
28225 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
28226 52 54 5f 45 78 69 73 74 73 3a 20 7b 0a 20 20 20  RT_Exists: {.   
28227 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28228 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
28229 65 72 2c 20 31 2c 20 70 44 65 73 74 2d 3e 69 50  er, 1, pDest->iP
2822a 61 72 6d 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  arm);.      /* T
2822b 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
2822c 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74  will terminate t
2822d 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a  he loop for us *
2822e 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
2822f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
28230 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
28231 20 73 63 61 6c 61 72 20 73 65 6c 65 63 74 20 74   scalar select t
28232 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61  hat is part of a
28233 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  n expression, th
28234 65 6e 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 20  en.    ** store 
28235 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  the results in t
28236 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d  he appropriate m
28237 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 62  emory cell and b
28238 72 65 61 6b 20 6f 75 74 0a 20 20 20 20 2a 2a 20  reak out.    ** 
28239 6f 66 20 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70  of the scan loop
2823a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
2823b 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20  e SRT_Mem: {.   
2823c 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 2d 3e     assert( pIn->
2823d 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20 20 20 20  nMem==1 );.     
2823e 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2823f 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49 6e  Move(pParse, pIn
28240 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 69  ->iMem, pDest->i
28241 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20  Parm, 1);.      
28242 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61  /* The LIMIT cla
28243 75 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75  use will jump ou
28244 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f  t of the loop fo
28245 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72  r us */.      br
28246 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
28247 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
28248 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
28249 59 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 54 68 65  Y */..    /* The
2824a 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f   results are sto
2824b 72 65 64 20 69 6e 20 61 20 73 65 71 75 65 6e 63  red in a sequenc
2824c 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 0a 20  e of registers. 
2824d 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 61     ** starting a
2824e 74 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2e 20 20  t pDest->iMem.  
2824f 54 68 65 6e 20 74 68 65 20 63 6f 2d 72 6f 75 74  Then the co-rout
28250 69 6e 65 20 79 69 65 6c 64 73 2e 0a 20 20 20 20  ine yields..    
28251 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
28252 43 6f 72 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20  Coroutine: {.   
28253 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 4d     if( pDest->iM
28254 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  em==0 ){.       
28255 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 73   pDest->iMem = s
28256 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
28257 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e  ge(pParse, pIn->
28258 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 70  nMem);.        p
28259 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 70 49 6e  Dest->nMem = pIn
2825a 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a  ->nMem;.      }.
2825b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2825c 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65  rCodeMove(pParse
2825d 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 44 65  , pIn->iMem, pDe
2825e 73 74 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d  st->iMem, pDest-
2825f 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71  >nMem);.      sq
28260 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
28261 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65  v, OP_Yield, pDe
28262 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20  st->iParm);.    
28263 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
28264 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 61      /* Results a
28265 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73  re stored in a s
28266 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69 73  equence of regis
28267 74 65 72 73 2e 20 20 54 68 65 6e 20 74 68 65 0a  ters.  Then the.
28268 20 20 20 20 2a 2a 20 4f 50 5f 52 65 73 75 6c 74      ** OP_Result
28269 52 6f 77 20 6f 70 63 6f 64 65 20 69 73 20 75 73  Row opcode is us
2826a 65 64 20 74 6f 20 63 61 75 73 65 20 73 71 6c 69  ed to cause sqli
2826b 74 65 33 5f 73 74 65 70 28 29 20 74 6f 20 72 65  te3_step() to re
2826c 74 75 72 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  turn.    ** the 
2826d 6e 65 78 74 20 72 6f 77 20 6f 66 20 72 65 73 75  next row of resu
2826e 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  lt..    */.    c
2826f 61 73 65 20 53 52 54 5f 4f 75 74 70 75 74 3a 20  ase SRT_Output: 
28270 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
28271 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28272 52 65 73 75 6c 74 52 6f 77 2c 20 70 49 6e 2d 3e  ResultRow, pIn->
28273 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29  iMem, pIn->nMem)
28274 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
28275 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
28276 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70  Change(pParse, p
28277 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e  In->iMem, pIn->n
28278 4d 65 6d 29 3b 0a 20 20 20 20 20 20 62 72 65 61  Mem);.      brea
28279 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 21 64  k;.    }..#if !d
2827a 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2827b 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20  IT_TRIGGER).    
2827c 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 72  /* Discard the r
2827d 65 73 75 6c 74 73 2e 20 20 54 68 69 73 20 69 73  esults.  This is
2827e 20 75 73 65 64 20 66 6f 72 20 53 45 4c 45 43 54   used for SELECT
2827f 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69   statements insi
28280 64 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 6f  de.    ** the bo
28281 64 79 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e  dy of a TRIGGER.
28282 20 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66    The purpose of
28283 20 73 75 63 68 20 73 65 6c 65 63 74 73 20 69 73   such selects is
28284 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20   to call.    ** 
28285 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e  user-defined fun
28286 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65  ctions that have
28287 20 73 69 64 65 20 65 66 66 65 63 74 73 2e 20 20   side effects.  
28288 57 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20  We do not care. 
28289 20 20 20 2a 2a 20 61 62 6f 75 74 20 74 68 65 20     ** about the 
2828a 61 63 74 75 61 6c 20 72 65 73 75 6c 74 73 20 6f  actual results o
2828b 66 20 74 68 65 20 73 65 6c 65 63 74 2e 0a 20 20  f the select..  
2828c 20 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74    */.    default
2828d 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  : {.      break;
2828e 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
2828f 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  }..  /* Jump to 
28290 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c  the end of the l
28291 6f 6f 70 20 69 66 20 74 68 65 20 4c 49 4d 49 54  oop if the LIMIT
28292 20 69 73 20 72 65 61 63 68 65 64 2e 0a 20 20 2a   is reached..  *
28293 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69  /.  if( p->iLimi
28294 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
28295 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28296 5f 41 64 64 49 6d 6d 2c 20 70 2d 3e 69 4c 69 6d  _AddImm, p->iLim
28297 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c  it, -1);.    sql
28298 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28299 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e  , OP_IfZero, p->
2829a 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 29 3b  iLimit, iBreak);
2829b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
2829c 61 74 65 20 74 68 65 20 73 75 62 72 6f 75 74 69  ate the subrouti
2829d 6e 65 20 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20  ne return.  */. 
2829e 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2829f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e  lveLabel(v, iCon
282a0 74 69 6e 75 65 29 3b 0a 20 20 73 71 6c 69 74 65  tinue);.  sqlite
282a1 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
282a2 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 52 65 74  P_Return, regRet
282a3 75 72 6e 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  urn);..  return 
282a4 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  addr;.}../*.** A
282a5 6c 74 65 72 6e 61 74 69 76 65 20 63 6f 6d 70 6f  lternative compo
282a6 75 6e 64 20 73 65 6c 65 63 74 20 63 6f 64 65 20  und select code 
282a7 67 65 6e 65 72 61 74 6f 72 20 66 6f 72 20 63 61  generator for ca
282a8 73 65 73 20 77 68 65 6e 20 74 68 65 72 65 0a 2a  ses when there.*
282a9 2a 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  * is an ORDER BY
282aa 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 57   clause..**.** W
282ab 65 20 61 73 73 75 6d 65 20 61 20 71 75 65 72 79  e assume a query
282ac 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
282ad 67 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  g form:.**.**   
282ae 20 20 20 3c 73 65 6c 65 63 74 41 3e 20 20 3c 6f     <selectA>  <o
282af 70 65 72 61 74 6f 72 3e 20 20 3c 73 65 6c 65 63  perator>  <selec
282b0 74 42 3e 20 20 4f 52 44 45 52 20 42 59 20 3c 6f  tB>  ORDER BY <o
282b1 72 64 65 72 62 79 6c 69 73 74 3e 0a 2a 2a 0a 2a  rderbylist>.**.*
282b2 2a 20 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73 20  * <operator> is 
282b3 6f 6e 65 20 6f 66 20 55 4e 49 4f 4e 20 41 4c 4c  one of UNION ALL
282b4 2c 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c  , UNION, EXCEPT,
282b5 20 6f 72 20 49 4e 54 45 52 53 45 43 54 2e 20 20   or INTERSECT.  
282b6 54 68 65 20 69 64 65 61 0a 2a 2a 20 69 73 20 74  The idea.** is t
282b7 6f 20 63 6f 64 65 20 62 6f 74 68 20 3c 73 65 6c  o code both <sel
282b8 65 63 74 41 3e 20 61 6e 64 20 3c 73 65 6c 65 63  ectA> and <selec
282b9 74 42 3e 20 77 69 74 68 20 74 68 65 20 4f 52 44  tB> with the ORD
282ba 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 73 0a  ER BY clause as.
282bb 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 73 2e 20  ** co-routines. 
282bc 20 54 68 65 6e 20 72 75 6e 20 74 68 65 20 63 6f   Then run the co
282bd 2d 72 6f 75 74 69 6e 65 73 20 69 6e 20 70 61 72  -routines in par
282be 61 6c 6c 65 6c 20 61 6e 64 20 6d 65 72 67 65 20  allel and merge 
282bf 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69  the results.** i
282c0 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e 20  nto the output. 
282c1 20 49 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20   In addition to 
282c2 74 68 65 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e  the two coroutin
282c3 65 73 20 28 63 61 6c 6c 65 64 20 73 65 6c 65 63  es (called selec
282c4 74 41 20 61 6e 64 0a 2a 2a 20 73 65 6c 65 63 74  tA and.** select
282c5 42 29 20 74 68 65 72 65 20 61 72 65 20 37 20 73  B) there are 7 s
282c6 75 62 72 6f 75 74 69 6e 65 73 3a 0a 2a 2a 0a 2a  ubroutines:.**.*
282c7 2a 20 20 20 20 6f 75 74 41 3a 20 20 20 20 4d 6f  *    outA:    Mo
282c8 76 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f 66  ve the output of
282c9 20 74 68 65 20 73 65 6c 65 63 74 41 20 63 6f 72   the selectA cor
282ca 6f 75 74 69 6e 65 20 69 6e 74 6f 20 74 68 65 20  outine into the 
282cb 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20  output.**       
282cc 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f 6d        of the com
282cd 70 6f 75 6e 64 20 71 75 65 72 79 2e 0a 2a 2a 0a  pound query..**.
282ce 2a 2a 20 20 20 20 6f 75 74 42 3a 20 20 20 20 4d  **    outB:    M
282cf 6f 76 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f  ove the output o
282d0 66 20 74 68 65 20 73 65 6c 65 63 74 42 20 63 6f  f the selectB co
282d1 72 6f 75 74 69 6e 65 20 69 6e 74 6f 20 74 68 65  routine into the
282d2 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20   output.**      
282d3 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f         of the co
282d4 6d 70 6f 75 6e 64 20 71 75 65 72 79 2e 20 20 28  mpound query.  (
282d5 4f 6e 6c 79 20 67 65 6e 65 72 61 74 65 64 20 66  Only generated f
282d6 6f 72 20 55 4e 49 4f 4e 20 61 6e 64 0a 2a 2a 20  or UNION and.** 
282d7 20 20 20 20 20 20 20 20 20 20 20 20 55 4e 49 4f              UNIO
282d8 4e 20 41 4c 4c 2e 20 20 45 58 43 45 50 54 20 61  N ALL.  EXCEPT a
282d9 6e 64 20 49 4e 53 45 52 54 53 45 43 54 20 6e 65  nd INSERTSECT ne
282da 76 65 72 20 6f 75 74 70 75 74 20 61 20 72 6f 77  ver output a row
282db 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
282dc 20 20 20 20 20 61 70 70 65 61 72 73 20 6f 6e 6c       appears onl
282dd 79 20 69 6e 20 42 2e 29 0a 2a 2a 0a 2a 2a 20 20  y in B.).**.**  
282de 20 20 41 6c 74 42 3a 20 20 20 20 43 61 6c 6c 65    AltB:    Calle
282df 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  d when there is 
282e0 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63  data from both c
282e1 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3c  oroutines and A<
282e2 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 65 71 42  B..**.**    AeqB
282e3 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e  :    Called when
282e4 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20 66   there is data f
282e5 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69  rom both corouti
282e6 6e 65 73 20 61 6e 64 20 41 3d 3d 42 2e 0a 2a 2a  nes and A==B..**
282e7 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20 20 20 20  .**    AgtB:    
282e8 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72  Called when ther
282e9 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62  e is data from b
282ea 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61  oth coroutines a
282eb 6e 64 20 41 3e 42 2e 0a 2a 2a 0a 2a 2a 20 20 20  nd A>B..**.**   
282ec 20 45 6f 66 41 3a 20 20 20 20 43 61 6c 6c 65 64   EofA:    Called
282ed 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 65 78   when data is ex
282ee 68 61 75 73 74 65 64 20 66 72 6f 6d 20 73 65 6c  hausted from sel
282ef 65 63 74 41 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45  ectA..**.**    E
282f0 6f 66 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77  ofB:    Called w
282f1 68 65 6e 20 64 61 74 61 20 69 73 20 65 78 68 61  hen data is exha
282f2 75 73 74 65 64 20 66 72 6f 6d 20 73 65 6c 65 63  usted from selec
282f3 74 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d  tB..**.** The im
282f4 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
282f5 74 68 65 20 6c 61 74 74 65 72 20 66 69 76 65 20  the latter five 
282f6 73 75 62 72 6f 75 74 69 6e 65 73 20 64 65 70 65  subroutines depe
282f7 6e 64 20 6f 6e 20 77 68 69 63 68 20 0a 2a 2a 20  nd on which .** 
282f8 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73 20 75 73  <operator> is us
282f9 65 64 3a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 20 20 20  ed:.**.**.**    
282fa 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41           UNION A
282fb 4c 4c 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e  LL         UNION
282fc 20 20 20 20 20 20 20 20 20 20 20 20 45 58 43 45              EXCE
282fd 50 54 20 20 20 20 20 20 20 20 20 20 49 4e 54 45  PT          INTE
282fe 52 53 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  RSECT.**        
282ff 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20    ------------- 
28300 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
28301 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --  ------------
28302 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --  ------------
28303 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 41 6c 74 42 3a  -----.**   AltB:
28304 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20     outA, nextA  
28305 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20      outA, nextA 
28306 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74        outA, next
28307 41 20 20 20 20 20 20 20 20 20 6e 65 78 74 41 0a  A         nextA.
28308 2a 2a 0a 2a 2a 20 20 20 41 65 71 42 3a 20 20 20  **.**   AeqB:   
28309 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
2830a 20 20 20 20 6e 65 78 74 41 20 20 20 20 20 20 20      nextA       
2830b 20 20 20 20 20 20 6e 65 78 74 41 20 20 20 20 20        nextA     
2830c 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 0a      outA, nextA.
2830d 2a 2a 0a 2a 2a 20 20 20 41 67 74 42 3a 20 20 20  **.**   AgtB:   
2830e 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20  outB, nextB     
2830f 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20   outB, nextB    
28310 20 20 20 20 20 20 6e 65 78 74 42 20 20 20 20 20        nextB     
28311 20 20 20 20 20 20 20 6e 65 78 74 42 0a 2a 2a 0a         nextB.**.
28312 2a 2a 20 20 20 45 6f 66 41 3a 20 20 20 6f 75 74  **   EofA:   out
28313 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 6f 75  B, nextB      ou
28314 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 20  tB, nextB       
28315 20 20 20 68 61 6c 74 20 20 20 20 20 20 20 20 20     halt         
28316 20 20 20 20 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 20      halt.**.**  
28317 20 45 6f 66 42 3a 20 20 20 6f 75 74 41 2c 20 6e   EofB:   outA, n
28318 65 78 74 41 20 20 20 20 20 20 6f 75 74 41 2c 20  extA      outA, 
28319 6e 65 78 74 41 20 20 20 20 20 20 20 6f 75 74 41  nextA       outA
2831a 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20  , nextA         
2831b 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  halt.**.** In th
2831c 65 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 61 6e  e AltB, AeqB, an
2831d 64 20 41 67 74 42 20 73 75 62 72 6f 75 74 69 6e  d AgtB subroutin
2831e 65 73 2c 20 61 6e 20 45 4f 46 20 6f 6e 20 41 20  es, an EOF on A 
2831f 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 41 0a  following nextA.
28320 2a 2a 20 63 61 75 73 65 73 20 61 6e 20 69 6d 6d  ** causes an imm
28321 65 64 69 61 74 65 20 6a 75 6d 70 20 74 6f 20 45  ediate jump to E
28322 6f 66 41 20 61 6e 64 20 61 6e 20 45 4f 46 20 6f  ofA and an EOF o
28323 6e 20 42 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65  n B following ne
28324 78 74 42 20 63 61 75 73 65 73 0a 2a 2a 20 61 6e  xtB causes.** an
28325 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70 20   immediate jump 
28326 74 6f 20 45 6f 66 42 2e 20 20 57 69 74 68 69 6e  to EofB.  Within
28327 20 45 6f 66 41 20 61 6e 64 20 45 6f 66 42 2c 20   EofA and EofB, 
28328 61 6e 64 20 45 4f 46 20 6f 6e 20 65 6e 74 72 79  and EOF on entry
28329 20 6f 72 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67   or.** following
2832a 20 6e 65 78 74 58 20 63 61 75 73 65 73 20 61 20   nextX causes a 
2832b 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20  jump to the end 
2832c 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 70 72  of the select pr
2832d 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  ocessing..**.** 
2832e 44 75 70 6c 69 63 61 74 65 20 72 65 6d 6f 76 61  Duplicate remova
2832f 6c 20 69 6e 20 74 68 65 20 55 4e 49 4f 4e 2c 20  l in the UNION, 
28330 45 58 43 45 50 54 2c 20 61 6e 64 20 49 4e 54 45  EXCEPT, and INTE
28331 52 53 45 43 54 20 63 61 73 65 73 20 69 73 20 68  RSECT cases is h
28332 61 6e 64 6c 65 64 0a 2a 2a 20 77 69 74 68 69 6e  andled.** within
28333 20 74 68 65 20 6f 75 74 70 75 74 20 73 75 62 72   the output subr
28334 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 72 65 67  outine.  The reg
28335 50 72 65 76 20 72 65 67 69 73 74 65 72 20 73 65  Prev register se
28336 74 20 68 6f 6c 64 73 20 74 68 65 20 70 72 65 76  t holds the prev
28337 69 6f 75 73 6c 79 0a 2a 2a 20 6f 75 74 70 75 74  iously.** output
28338 20 76 61 6c 75 65 2e 20 20 41 20 63 6f 6d 70 61   value.  A compa
28339 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 20 61 67  rison is made ag
2833a 61 69 6e 73 74 20 74 68 69 73 20 76 61 6c 75 65  ainst this value
2833b 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a   and the output.
2833c 2a 2a 20 69 73 20 73 6b 69 70 70 65 64 20 69 66  ** is skipped if
2833d 20 74 68 65 20 6e 65 78 74 20 72 65 73 75 6c 74   the next result
2833e 73 20 77 6f 75 6c 64 20 62 65 20 74 68 65 20 73  s would be the s
2833f 61 6d 65 20 61 73 20 74 68 65 20 70 72 65 76 69  ame as the previ
28340 6f 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ous..**.** The i
28341 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 70 6c  mplementation pl
28342 61 6e 20 69 73 20 74 6f 20 69 6d 70 6c 65 6d 65  an is to impleme
28343 6e 74 20 74 68 65 20 74 77 6f 20 63 6f 72 6f 75  nt the two corou
28344 74 69 6e 65 73 20 61 6e 64 20 73 65 76 65 6e 0a  tines and seven.
28345 2a 2a 20 73 75 62 72 6f 75 74 69 6e 65 73 20 66  ** subroutines f
28346 69 72 73 74 2c 20 74 68 65 6e 20 70 75 74 20 74  irst, then put t
28347 68 65 20 63 6f 6e 74 72 6f 6c 20 6c 6f 67 69 63  he control logic
28348 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 2e 20   at the bottom. 
28349 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   Like this:.**.*
2834a 2a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  *          goto 
2834b 49 6e 69 74 0a 2a 2a 20 20 20 20 20 63 6f 41 3a  Init.**     coA:
2834c 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c   coroutine for l
2834d 65 66 74 20 71 75 65 72 79 20 28 41 29 0a 2a 2a  eft query (A).**
2834e 20 20 20 20 20 63 6f 42 3a 20 63 6f 72 6f 75 74       coB: corout
2834f 69 6e 65 20 66 6f 72 20 72 69 67 68 74 20 71 75  ine for right qu
28350 65 72 79 20 28 42 29 0a 2a 2a 20 20 20 20 6f 75  ery (B).**    ou
28351 74 41 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20 72  tA: output one r
28352 6f 77 20 6f 66 20 41 0a 2a 2a 20 20 20 20 6f 75  ow of A.**    ou
28353 74 42 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20 72  tB: output one r
28354 6f 77 20 6f 66 20 42 20 28 55 4e 49 4f 4e 20 61  ow of B (UNION a
28355 6e 64 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 6e 6c  nd UNION ALL onl
28356 79 29 0a 2a 2a 20 20 20 20 45 6f 66 41 3a 20 2e  y).**    EofA: .
28357 2e 2e 0a 2a 2a 20 20 20 20 45 6f 66 42 3a 20 2e  ...**    EofB: .
28358 2e 2e 0a 2a 2a 20 20 20 20 41 6c 74 42 3a 20 2e  ...**    AltB: .
28359 2e 2e 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20 2e  ...**    AeqB: .
2835a 2e 2e 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20 2e  ...**    AgtB: .
2835b 2e 2e 0a 2a 2a 20 20 20 20 49 6e 69 74 3a 20 69  ...**    Init: i
2835c 6e 69 74 69 61 6c 69 7a 65 20 63 6f 72 6f 75 74  nitialize corout
2835d 69 6e 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2a  ine registers.**
2835e 20 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20            yield 
2835f 63 6f 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  coA.**          
28360 69 66 20 65 6f 66 28 41 29 20 67 6f 74 6f 20 45  if eof(A) goto E
28361 6f 66 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ofA.**          
28362 79 69 65 6c 64 20 63 6f 42 0a 2a 2a 20 20 20 20  yield coB.**    
28363 20 20 20 20 20 20 69 66 20 65 6f 66 28 42 29 20        if eof(B) 
28364 67 6f 74 6f 20 45 6f 66 42 0a 2a 2a 20 20 20 20  goto EofB.**    
28365 43 6d 70 72 3a 20 43 6f 6d 70 61 72 65 20 41 2c  Cmpr: Compare A,
28366 20 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4a   B.**          J
28367 75 6d 70 20 41 6c 74 42 2c 20 41 65 71 42 2c 20  ump AltB, AeqB, 
28368 41 67 74 42 0a 2a 2a 20 20 20 20 20 45 6e 64 3a  AgtB.**     End:
28369 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61   ....**.** We ca
2836a 6c 6c 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 41  ll AltB, AeqB, A
2836b 67 74 42 2c 20 45 6f 66 41 2c 20 61 6e 64 20 45  gtB, EofA, and E
2836c 6f 66 42 20 22 73 75 62 72 6f 75 74 69 6e 65 73  ofB "subroutines
2836d 22 20 62 75 74 20 74 68 65 79 20 61 72 65 20 6e  " but they are n
2836e 6f 74 0a 2a 2a 20 61 63 74 75 61 6c 6c 79 20 63  ot.** actually c
2836f 61 6c 6c 65 64 20 75 73 69 6e 67 20 47 6f 73 75  alled using Gosu
28370 62 20 61 6e 64 20 74 68 65 79 20 64 6f 20 6e 6f  b and they do no
28371 74 20 52 65 74 75 72 6e 2e 20 20 45 6f 66 41 20  t Return.  EofA 
28372 61 6e 64 20 45 6f 66 42 20 6c 6f 6f 70 0a 2a 2a  and EofB loop.**
28373 20 75 6e 74 69 6c 20 61 6c 6c 20 64 61 74 61 20   until all data 
28374 69 73 20 65 78 68 61 75 73 74 65 64 20 74 68 65  is exhausted the
28375 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 22 65  n jump to the "e
28376 6e 64 22 20 6c 61 62 65 2e 20 20 41 6c 74 42 2c  nd" labe.  AltB,
28377 20 41 65 71 42 2c 0a 2a 2a 20 61 6e 64 20 41 67   AeqB,.** and Ag
28378 74 42 20 6a 75 6d 70 20 74 6f 20 65 69 74 68 65  tB jump to eithe
28379 72 20 4c 32 20 6f 72 20 74 6f 20 6f 6e 65 20 6f  r L2 or to one o
2837a 66 20 45 6f 66 41 20 6f 72 20 45 6f 66 42 2e 0a  f EofA or EofB..
2837b 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2837c 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
2837d 53 45 4c 45 43 54 0a 73 74 61 74 69 63 20 69 6e  SELECT.static in
2837e 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64  t multiSelectOrd
2837f 65 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70  erBy(.  Parse *p
28380 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
28381 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
28382 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
28383 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
28384 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66  he right-most of
28385 20 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63   SELECTs to be c
28386 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74  oded */.  Select
28387 44 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20  Dest *pDest     
28388 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69  /* What to do wi
28389 74 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  th query results
2838a 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
2838b 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  j;             /
2838c 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
2838d 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72  */.  Select *pPr
2838e 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  ior;       /* An
2838f 6f 74 68 65 72 20 53 45 4c 45 43 54 20 69 6d 6d  other SELECT imm
28390 65 64 69 61 74 65 6c 79 20 74 6f 20 6f 75 72 20  ediately to our 
28391 6c 65 66 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a  left */.  Vdbe *
28392 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
28393 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
28394 20 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f   to this VDBE */
28395 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
28396 73 74 41 3b 20 20 20 20 20 2f 2a 20 44 65 73 74  stA;     /* Dest
28397 69 6e 61 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f  ination for coro
28398 75 74 69 6e 65 20 41 20 2a 2f 0a 20 20 53 65 6c  utine A */.  Sel
28399 65 63 74 44 65 73 74 20 64 65 73 74 42 3b 20 20  ectDest destB;  
2839a 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
2839b 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e 65 20  n for coroutine 
2839c 42 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 64  B */.  int regAd
2839d 64 72 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  drA;         /* 
2839e 41 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72  Address register
2839f 20 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 63 6f   for select-A co
283a0 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
283a1 20 72 65 67 45 6f 66 41 3b 20 20 20 20 20 20 20   regEofA;       
283a2 20 20 20 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e     /* Flag to in
283a3 64 69 63 61 74 65 20 77 68 65 6e 20 73 65 6c 65  dicate when sele
283a4 63 74 2d 41 20 69 73 20 63 6f 6d 70 6c 65 74 65  ct-A is complete
283a5 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 64 64   */.  int regAdd
283a6 72 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  rB;         /* A
283a7 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20  ddress register 
283a8 66 6f 72 20 73 65 6c 65 63 74 2d 42 20 63 6f 72  for select-B cor
283a9 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
283aa 72 65 67 45 6f 66 42 3b 20 20 20 20 20 20 20 20  regEofB;        
283ab 20 20 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e 64    /* Flag to ind
283ac 69 63 61 74 65 20 77 68 65 6e 20 73 65 6c 65 63  icate when selec
283ad 74 2d 42 20 69 73 20 63 6f 6d 70 6c 65 74 65 20  t-B is complete 
283ae 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c  */.  int addrSel
283af 65 63 74 41 3b 20 20 20 20 20 20 2f 2a 20 41 64  ectA;      /* Ad
283b0 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c  dress of the sel
283b1 65 63 74 2d 41 20 63 6f 72 6f 75 74 69 6e 65 20  ect-A coroutine 
283b2 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c  */.  int addrSel
283b3 65 63 74 42 3b 20 20 20 20 20 20 2f 2a 20 41 64  ectB;      /* Ad
283b4 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c  dress of the sel
283b5 65 63 74 2d 42 20 63 6f 72 6f 75 74 69 6e 65 20  ect-B coroutine 
283b6 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 75 74 41  */.  int regOutA
283b7 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  ;          /* Ad
283b8 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66  dress register f
283b9 6f 72 20 74 68 65 20 6f 75 74 70 75 74 2d 41 20  or the output-A 
283ba 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  subroutine */.  
283bb 69 6e 74 20 72 65 67 4f 75 74 42 3b 20 20 20 20  int regOutB;    
283bc 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
283bd 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 68   register for th
283be 65 20 6f 75 74 70 75 74 2d 42 20 73 75 62 72 6f  e output-B subro
283bf 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61  utine */.  int a
283c0 64 64 72 4f 75 74 41 3b 20 20 20 20 20 20 20 20  ddrOutA;        
283c1 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
283c2 68 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72  he output-A subr
283c3 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
283c4 61 64 64 72 4f 75 74 42 20 3d 20 30 3b 20 20 20  addrOutB = 0;   
283c5 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
283c6 74 68 65 20 6f 75 74 70 75 74 2d 42 20 73 75 62  the output-B sub
283c7 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
283c8 20 61 64 64 72 45 6f 66 41 3b 20 20 20 20 20 20   addrEofA;      
283c9 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
283ca 20 74 68 65 20 73 65 6c 65 63 74 2d 41 2d 65 78   the select-A-ex
283cb 68 61 75 73 74 65 64 20 73 75 62 72 6f 75 74 69  hausted subrouti
283cc 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
283cd 45 6f 66 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  EofB;         /*
283ce 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
283cf 73 65 6c 65 63 74 2d 42 2d 65 78 68 61 75 73 74  select-B-exhaust
283d0 65 64 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f  ed subroutine */
283d1 0a 20 20 69 6e 74 20 61 64 64 72 41 6c 74 42 3b  .  int addrAltB;
283d2 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
283d3 65 73 73 20 6f 66 20 74 68 65 20 41 3c 42 20 73  ess of the A<B s
283d4 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
283d5 6e 74 20 61 64 64 72 41 65 71 42 3b 20 20 20 20  nt addrAeqB;    
283d6 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
283d7 6f 66 20 74 68 65 20 41 3d 3d 42 20 73 75 62 72  of the A==B subr
283d8 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
283d9 61 64 64 72 41 67 74 42 3b 20 20 20 20 20 20 20  addrAgtB;       
283da 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
283db 74 68 65 20 41 3e 42 20 73 75 62 72 6f 75 74 69  the A>B subrouti
283dc 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c  ne */.  int regL
283dd 69 6d 69 74 41 3b 20 20 20 20 20 20 20 20 2f 2a  imitA;        /*
283de 20 4c 69 6d 69 74 20 72 65 67 69 73 74 65 72 20   Limit register 
283df 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a  for select-A */.
283e0 20 20 69 6e 74 20 72 65 67 4c 69 6d 69 74 42 3b    int regLimitB;
283e1 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6d 69 74          /* Limit
283e2 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73 65   register for se
283e3 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69 6e 74 20  lect-A */.  int 
283e4 72 65 67 50 72 65 76 3b 20 20 20 20 20 20 20 20  regPrev;        
283e5 20 20 2f 2a 20 41 20 72 61 6e 67 65 20 6f 66 20    /* A range of 
283e6 72 65 67 69 73 74 65 72 73 20 74 6f 20 68 6f 6c  registers to hol
283e7 64 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70 75  d previous outpu
283e8 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64  t */.  int saved
283e9 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a 20  Limit;       /* 
283ea 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 70  Saved value of p
283eb 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 69 6e  ->iLimit */.  in
283ec 74 20 73 61 76 65 64 4f 66 66 73 65 74 3b 20 20  t savedOffset;  
283ed 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c      /* Saved val
283ee 75 65 20 6f 66 20 70 2d 3e 69 4f 66 66 73 65 74  ue of p->iOffset
283ef 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 43   */.  int labelC
283f0 6d 70 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  mpr;        /* L
283f1 61 62 65 6c 20 66 6f 72 20 74 68 65 20 73 74 61  abel for the sta
283f2 72 74 20 6f 66 20 74 68 65 20 6d 65 72 67 65 20  rt of the merge 
283f3 61 6c 67 6f 72 69 74 68 6d 20 2a 2f 0a 20 20 69  algorithm */.  i
283f4 6e 74 20 6c 61 62 65 6c 45 6e 64 3b 20 20 20 20  nt labelEnd;    
283f5 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f       /* Label fo
283f6 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  r the end of the
283f7 20 6f 76 65 72 61 6c 6c 20 53 45 4c 45 43 54 20   overall SELECT 
283f8 73 74 6d 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 31  stmt */.  int j1
283f9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
283fa 2f 2a 20 4a 75 6d 70 20 69 6e 73 74 72 75 63 74  /* Jump instruct
283fb 69 6f 6e 73 20 74 68 61 74 20 67 65 74 20 72 65  ions that get re
283fc 74 61 72 67 65 74 74 65 64 20 2a 2f 0a 20 20 69  targetted */.  i
283fd 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  nt op;          
283fe 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
283ff 4b 5f 41 4c 4c 2c 20 54 4b 5f 55 4e 49 4f 4e 2c  K_ALL, TK_UNION,
28400 20 54 4b 5f 45 58 43 45 50 54 2c 20 54 4b 5f 49   TK_EXCEPT, TK_I
28401 4e 54 45 52 53 45 43 54 20 2a 2f 0a 20 20 4b 65  NTERSECT */.  Ke
28402 79 49 6e 66 6f 20 2a 70 4b 65 79 44 75 70 20 3d  yInfo *pKeyDup =
28403 20 30 3b 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f   0; /* Compariso
28404 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  n information fo
28405 72 20 64 75 70 6c 69 63 61 74 65 20 72 65 6d 6f  r duplicate remo
28406 76 61 6c 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f  val */.  KeyInfo
28407 20 2a 70 4b 65 79 4d 65 72 67 65 3b 20 20 20 2f   *pKeyMerge;   /
28408 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66  * Comparison inf
28409 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 6d 65 72  ormation for mer
2840a 67 69 6e 67 20 72 6f 77 73 20 2a 2f 0a 20 20 73  ging rows */.  s
2840b 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
2840c 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2840d 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
2840e 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2840f 72 42 79 3b 20 20 20 2f 2a 20 54 68 65 20 4f 52  rBy;   /* The OR
28410 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
28411 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b  .  int nOrderBy;
28412 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
28413 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74  er of terms in t
28414 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
28415 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65  se */.  int *aPe
28416 72 6d 75 74 65 3b 20 20 20 20 20 20 20 20 2f 2a  rmute;        /*
28417 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 4f 52   Mapping from OR
28418 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 6f 20  DER BY terms to 
28419 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d  result set colum
2841a 6e 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ns */..  assert(
2841b 20 70 2d 3e 70 4f 72 64 65 72 42 79 21 3d 30 20   p->pOrderBy!=0 
2841c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65  );.  assert( pKe
2841d 79 44 75 70 3d 3d 30 20 29 3b 20 2f 2a 20 22 4d  yDup==0 ); /* "M
2841e 61 6e 61 67 65 64 22 20 63 6f 64 65 20 6e 65 65  anaged" code nee
2841f 64 73 20 74 68 69 73 2e 20 20 54 69 63 6b 65 74  ds this.  Ticket
28420 20 23 33 33 38 32 2e 20 2a 2f 0a 20 20 64 62 20   #3382. */.  db 
28421 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
28422 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
28423 65 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  e;.  if( v==0 ) 
28424 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
28425 4d 45 4d 3b 0a 20 20 6c 61 62 65 6c 45 6e 64 20  MEM;.  labelEnd 
28426 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
28427 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 6c 61 62  eLabel(v);.  lab
28428 65 6c 43 6d 70 72 20 3d 20 73 71 6c 69 74 65 33  elCmpr = sqlite3
28429 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
2842a 3b 0a 0a 0a 20 20 2f 2a 20 50 61 74 63 68 20 75  ;...  /* Patch u
2842b 70 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  p the ORDER BY c
2842c 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20 6f 70 20  lause.  */.  op 
2842d 3d 20 70 2d 3e 6f 70 3b 20 20 0a 20 20 70 50 72  = p->op;  .  pPr
2842e 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b  ior = p->pPrior;
2842f 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f  .  assert( pPrio
28430 72 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  r->pOrderBy==0 )
28431 3b 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70  ;.  pOrderBy = p
28432 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 61 73  ->pOrderBy;.  as
28433 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 20 29  sert( pOrderBy )
28434 3b 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 70  ;.  nOrderBy = p
28435 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a  OrderBy->nExpr;.
28436 0a 20 20 2f 2a 20 46 6f 72 20 6f 70 65 72 61 74  .  /* For operat
28437 6f 72 73 20 6f 74 68 65 72 20 74 68 61 6e 20 55  ors other than U
28438 4e 49 4f 4e 20 41 4c 4c 20 77 65 20 68 61 76 65  NION ALL we have
28439 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
2843a 61 74 0a 20 20 2a 2a 20 74 68 65 20 4f 52 44 45  at.  ** the ORDE
2843b 52 20 42 59 20 63 6c 61 75 73 65 20 63 6f 76 65  R BY clause cove
2843c 72 73 20 65 76 65 72 79 20 74 65 72 6d 20 6f 66  rs every term of
2843d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
2843e 20 20 41 64 64 0a 20 20 2a 2a 20 74 65 72 6d 73    Add.  ** terms
2843f 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59   to the ORDER BY
28440 20 63 6c 61 75 73 65 20 61 73 20 6e 65 63 65 73   clause as neces
28441 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  sary..  */.  if(
28442 20 6f 70 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20   op!=TK_ALL ){. 
28443 20 20 20 66 6f 72 28 69 3d 31 3b 20 64 62 2d 3e     for(i=1; db->
28444 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20  mallocFailed==0 
28445 26 26 20 69 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d  && i<=p->pEList-
28446 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
28447 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
28448 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
28449 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20  .      for(j=0, 
2844a 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e  pItem=pOrderBy->
2844b 61 3b 20 6a 3c 6e 4f 72 64 65 72 42 79 3b 20 6a  a; j<nOrderBy; j
2844c 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
2844d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
2844e 74 65 6d 2d 3e 69 43 6f 6c 3e 30 20 29 3b 0a 20  tem->iCol>0 );. 
2844f 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
28450 2d 3e 69 43 6f 6c 3d 3d 69 20 29 20 62 72 65 61  ->iCol==i ) brea
28451 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
28452 20 69 66 28 20 6a 3d 3d 6e 4f 72 64 65 72 42 79   if( j==nOrderBy
28453 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72   ){.        Expr
28454 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
28455 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
28456 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 2c 20  _INTEGER, 0, 0, 
28457 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
28458 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  pNew==0 ) return
28459 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2845a 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61         pNew->fla
2845b 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75  gs |= EP_IntValu
2845c 65 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d  e;.        pNew-
2845d 3e 69 54 61 62 6c 65 20 3d 20 69 3b 0a 20 20 20  >iTable = i;.   
2845e 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20       pOrderBy = 
2845f 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
28460 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4f  ppend(pParse, pO
28461 72 64 65 72 42 79 2c 20 70 4e 65 77 2c 20 30 29  rderBy, pNew, 0)
28462 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 64 65 72  ;.        pOrder
28463 42 79 2d 3e 61 5b 6e 4f 72 64 65 72 42 79 2b 2b  By->a[nOrderBy++
28464 5d 2e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 3b  ].iCol = (u16)i;
28465 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
28466 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65   }..  /* Compute
28467 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
28468 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 6e 64 20  permutation and 
28469 6b 65 79 69 6e 66 6f 20 74 68 61 74 20 69 73 20  keyinfo that is 
2846a 75 73 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 74  used with.  ** t
2846b 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75  he permutation u
2846c 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
2846d 20 69 66 20 74 68 65 20 6e 65 78 74 0a 20 20 2a   if the next.  *
2846e 2a 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 73  * row of results
2846f 20 63 6f 6d 65 73 20 66 72 6f 6d 20 73 65 6c 65   comes from sele
28470 63 74 41 20 6f 72 20 73 65 6c 65 63 74 42 2e 20  ctA or selectB. 
28471 20 41 6c 73 6f 20 61 64 64 20 65 78 70 6c 69 63   Also add explic
28472 69 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f  it.  ** collatio
28473 6e 73 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20  ns to the ORDER 
28474 42 59 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20  BY clause terms 
28475 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68 65  so that when the
28476 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 2a 2a   subqueries.  **
28477 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 61 6e   to the right an
28478 64 20 74 68 65 20 6c 65 66 74 20 61 72 65 20 65  d the left are e
28479 76 61 6c 75 61 74 65 64 2c 20 74 68 65 79 20 75  valuated, they u
2847a 73 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a 20  se the correct. 
2847b 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 20   ** collation.. 
2847c 20 2a 2f 0a 20 20 61 50 65 72 6d 75 74 65 20 3d   */.  aPermute =
2847d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2847e 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69  Raw(db, sizeof(i
2847f 6e 74 29 2a 6e 4f 72 64 65 72 42 79 29 3b 0a 20  nt)*nOrderBy);. 
28480 20 69 66 28 20 61 50 65 72 6d 75 74 65 20 29 7b   if( aPermute ){
28481 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72  .    struct Expr
28482 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
28483 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70  ;.    for(i=0, p
28484 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61  Item=pOrderBy->a
28485 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b  ; i<nOrderBy; i+
28486 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
28487 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d     assert( pItem
28488 2d 3e 69 43 6f 6c 3e 30 20 20 26 26 20 70 49 74  ->iCol>0  && pIt
28489 65 6d 2d 3e 69 43 6f 6c 3c 3d 70 2d 3e 70 45 4c  em->iCol<=p->pEL
2848a 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20  ist->nExpr );.  
2848b 20 20 20 20 61 50 65 72 6d 75 74 65 5b 69 5d 20      aPermute[i] 
2848c 3d 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 2d 20  = pItem->iCol - 
2848d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4b 65  1;.    }.    pKe
2848e 79 4d 65 72 67 65 20 3d 0a 20 20 20 20 20 20 73  yMerge =.      s
2848f 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
28490 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4b  w(db, sizeof(*pK
28491 65 79 4d 65 72 67 65 29 2b 6e 4f 72 64 65 72 42  eyMerge)+nOrderB
28492 79 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65  y*(sizeof(CollSe
28493 71 2a 29 2b 31 29 29 3b 0a 20 20 20 20 69 66 28  q*)+1));.    if(
28494 20 70 4b 65 79 4d 65 72 67 65 20 29 7b 0a 20 20   pKeyMerge ){.  
28495 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61      pKeyMerge->a
28496 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a  SortOrder = (u8*
28497 29 26 70 4b 65 79 4d 65 72 67 65 2d 3e 61 43 6f  )&pKeyMerge->aCo
28498 6c 6c 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20  ll[nOrderBy];.  
28499 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 6e      pKeyMerge->n
2849a 46 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 4f 72  Field = (u16)nOr
2849b 64 65 72 42 79 3b 0a 20 20 20 20 20 20 70 4b 65  derBy;.      pKe
2849c 79 4d 65 72 67 65 2d 3e 65 6e 63 20 3d 20 45 4e  yMerge->enc = EN
2849d 43 28 64 62 29 3b 0a 20 20 20 20 20 20 66 6f 72  C(db);.      for
2849e 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79  (i=0; i<nOrderBy
2849f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
284a0 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
284a1 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 54          Expr *pT
284a2 65 72 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  erm = pOrderBy->
284a3 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
284a4 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 66      if( pTerm->f
284a5 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
284a6 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  late ){.        
284a7 20 20 70 43 6f 6c 6c 20 3d 20 70 54 65 72 6d 2d    pColl = pTerm-
284a8 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20  >pColl;.        
284a9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
284aa 20 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65   pColl = multiSe
284ab 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61 72  lectCollSeq(pPar
284ac 73 65 2c 20 70 2c 20 61 50 65 72 6d 75 74 65 5b  se, p, aPermute[
284ad 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  i]);.          p
284ae 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Term->flags |= E
284af 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
284b0 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 70          pTerm->p
284b1 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20  Coll = pColl;.  
284b2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
284b3 70 4b 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c  pKeyMerge->aColl
284b4 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20  [i] = pColl;.   
284b5 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e       pKeyMerge->
284b6 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
284b7 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73  pOrderBy->a[i].s
284b8 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20  ortOrder;.      
284b9 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  }.    }.  }else{
284ba 0a 20 20 20 20 70 4b 65 79 4d 65 72 67 65 20 3d  .    pKeyMerge =
284bb 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65   0;.  }..  /* Re
284bc 61 74 74 61 63 68 20 74 68 65 20 4f 52 44 45 52  attach the ORDER
284bd 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 74 68   BY clause to th
284be 65 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20  e query..  */.  
284bf 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f  p->pOrderBy = pO
284c0 72 64 65 72 42 79 3b 0a 20 20 70 50 72 69 6f 72  rderBy;.  pPrior
284c1 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c  ->pOrderBy = sql
284c2 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
284c3 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64  pParse->db, pOrd
284c4 65 72 42 79 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20  erBy, 0);..  /* 
284c5 41 6c 6c 6f 63 61 74 65 20 61 20 72 61 6e 67 65  Allocate a range
284c6 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65   of temporary re
284c7 67 69 73 74 65 72 73 20 61 6e 64 20 74 68 65 20  gisters and the 
284c8 4b 65 79 49 6e 66 6f 20 6e 65 65 64 65 64 0a 20  KeyInfo needed. 
284c9 20 2a 2a 20 66 6f 72 20 74 68 65 20 6c 6f 67 69   ** for the logi
284ca 63 20 74 68 61 74 20 72 65 6d 6f 76 65 73 20 64  c that removes d
284cb 75 70 6c 69 63 61 74 65 20 72 65 73 75 6c 74 20  uplicate result 
284cc 72 6f 77 73 20 77 68 65 6e 20 74 68 65 0a 20 20  rows when the.  
284cd 2a 2a 20 6f 70 65 72 61 74 6f 72 20 69 73 20 55  ** operator is U
284ce 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72  NION, EXCEPT, or
284cf 20 49 4e 54 45 52 53 45 43 54 20 28 62 75 74 20   INTERSECT (but 
284d0 6e 6f 74 20 55 4e 49 4f 4e 20 41 4c 4c 29 2e 0a  not UNION ALL)..
284d1 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54    */.  if( op==T
284d2 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 67  K_ALL ){.    reg
284d3 50 72 65 76 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  Prev = 0;.  }els
284d4 65 7b 0a 20 20 20 20 69 6e 74 20 6e 45 78 70 72  e{.    int nExpr
284d5 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45   = p->pEList->nE
284d6 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  xpr;.    assert(
284d7 20 6e 4f 72 64 65 72 42 79 3e 3d 6e 45 78 70 72   nOrderBy>=nExpr
284d8 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
284d9 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 67 50  iled );.    regP
284da 72 65 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74  rev = sqlite3Get
284db 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
284dc 2c 20 6e 45 78 70 72 2b 31 29 3b 0a 20 20 20 20  , nExpr+1);.    
284dd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
284de 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
284df 20 30 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20   0, regPrev);.  
284e0 20 20 70 4b 65 79 44 75 70 20 3d 20 73 71 6c 69    pKeyDup = sqli
284e1 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
284e2 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  db,.            
284e3 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4b        sizeof(*pK
284e4 65 79 44 75 70 29 20 2b 20 6e 45 78 70 72 2a 28  eyDup) + nExpr*(
284e5 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29  sizeof(CollSeq*)
284e6 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70  +1) );.    if( p
284e7 4b 65 79 44 75 70 20 29 7b 0a 20 20 20 20 20 20  KeyDup ){.      
284e8 70 4b 65 79 44 75 70 2d 3e 61 53 6f 72 74 4f 72  pKeyDup->aSortOr
284e9 64 65 72 20 3d 20 28 75 38 2a 29 26 70 4b 65 79  der = (u8*)&pKey
284ea 44 75 70 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72  Dup->aColl[nExpr
284eb 5d 3b 0a 20 20 20 20 20 20 70 4b 65 79 44 75 70  ];.      pKeyDup
284ec 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  ->nField = (u16)
284ed 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4b 65  nExpr;.      pKe
284ee 79 44 75 70 2d 3e 65 6e 63 20 3d 20 45 4e 43 28  yDup->enc = ENC(
284ef 64 62 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  db);.      for(i
284f0 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b  =0; i<nExpr; i++
284f1 29 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 44  ){.        pKeyD
284f2 75 70 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 6d  up->aColl[i] = m
284f3 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65  ultiSelectCollSe
284f4 71 28 70 50 61 72 73 65 2c 20 70 2c 20 69 29 3b  q(pParse, p, i);
284f5 0a 20 20 20 20 20 20 20 20 70 4b 65 79 44 75 70  .        pKeyDup
284f6 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20  ->aSortOrder[i] 
284f7 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
284f8 20 7d 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 53 65   }.  }. .  /* Se
284f9 70 61 72 61 74 65 20 74 68 65 20 6c 65 66 74 20  parate the left 
284fa 61 6e 64 20 74 68 65 20 72 69 67 68 74 20 71 75  and the right qu
284fb 65 72 79 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f  ery from one ano
284fc 74 68 65 72 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70  ther.  */.  p->p
284fd 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 70 50 72  Prior = 0;.  pPr
284fe 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20  ior->pRightmost 
284ff 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  = 0;.  sqlite3Re
28500 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
28501 79 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e  y(pParse, p, p->
28502 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52  pOrderBy, "ORDER
28503 22 29 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72  ");.  if( pPrior
28504 2d 3e 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20  ->pPrior==0 ){. 
28505 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76     sqlite3Resolv
28506 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50  eOrderGroupBy(pP
28507 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 70 50  arse, pPrior, pP
28508 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 2c 20  rior->pOrderBy, 
28509 22 4f 52 44 45 52 22 29 3b 0a 20 20 7d 0a 0a 20  "ORDER");.  }.. 
2850a 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
2850b 6c 69 6d 69 74 20 72 65 67 69 73 74 65 72 73 20  limit registers 
2850c 2a 2f 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69  */.  computeLimi
2850d 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72 73  tRegisters(pPars
2850e 65 2c 20 70 2c 20 6c 61 62 65 6c 45 6e 64 29 3b  e, p, labelEnd);
2850f 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74  .  if( p->iLimit
28510 20 26 26 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29   && op==TK_ALL )
28511 7b 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 41 20  {.    regLimitA 
28512 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
28513 3b 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 42 20  ;.    regLimitB 
28514 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
28515 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28516 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
28517 70 79 2c 20 70 2d 3e 69 4f 66 66 73 65 74 20 3f  py, p->iOffset ?
28518 20 70 2d 3e 69 4f 66 66 73 65 74 2b 31 20 3a 20   p->iOffset+1 : 
28519 70 2d 3e 69 4c 69 6d 69 74 2c 0a 20 20 20 20 20  p->iLimit,.     
2851a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2851b 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67               reg
2851c 4c 69 6d 69 74 41 29 3b 0a 20 20 20 20 73 71 6c  LimitA);.    sql
2851d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2851e 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4c 69  , OP_Copy, regLi
2851f 6d 69 74 41 2c 20 72 65 67 4c 69 6d 69 74 42 29  mitA, regLimitB)
28520 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
28521 65 67 4c 69 6d 69 74 41 20 3d 20 72 65 67 4c 69  egLimitA = regLi
28522 6d 69 74 42 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  mitB = 0;.  }.  
28523 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
28524 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29  e(db, p->pLimit)
28525 3b 0a 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20  ;.  p->pLimit = 
28526 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  0;.  sqlite3Expr
28527 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f  Delete(db, p->pO
28528 66 66 73 65 74 29 3b 0a 20 20 70 2d 3e 70 4f 66  ffset);.  p->pOf
28529 66 73 65 74 20 3d 20 30 3b 0a 0a 20 20 72 65 67  fset = 0;..  reg
2852a 41 64 64 72 41 20 3d 20 2b 2b 70 50 61 72 73 65  AddrA = ++pParse
2852b 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 45 6f 66  ->nMem;.  regEof
2852c 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  A = ++pParse->nM
2852d 65 6d 3b 0a 20 20 72 65 67 41 64 64 72 42 20 3d  em;.  regAddrB =
2852e 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2852f 0a 20 20 72 65 67 45 6f 66 42 20 3d 20 2b 2b 70  .  regEofB = ++p
28530 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72  Parse->nMem;.  r
28531 65 67 4f 75 74 41 20 3d 20 2b 2b 70 50 61 72 73  egOutA = ++pPars
28532 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75  e->nMem;.  regOu
28533 74 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  tB = ++pParse->n
28534 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  Mem;.  sqlite3Se
28535 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
28536 73 74 41 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69  stA, SRT_Corouti
28537 6e 65 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20  ne, regAddrA);. 
28538 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
28539 73 74 49 6e 69 74 28 26 64 65 73 74 42 2c 20 53  stInit(&destB, S
2853a 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65  RT_Coroutine, re
2853b 67 41 64 64 72 42 29 3b 0a 0a 20 20 2f 2a 20 4a  gAddrB);..  /* J
2853c 75 6d 70 20 70 61 73 74 20 74 68 65 20 76 61 72  ump past the var
2853d 69 6f 75 73 20 73 75 62 72 6f 75 74 69 6e 65 73  ious subroutines
2853e 20 61 6e 64 20 63 6f 72 6f 75 74 69 6e 65 73 20   and coroutines 
2853f 74 6f 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a 2a  to the main.  **
28540 20 6d 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f   merge loop.  */
28541 0a 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56  .  j1 = sqlite3V
28542 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
28543 47 6f 74 6f 29 3b 0a 20 20 61 64 64 72 53 65 6c  Goto);.  addrSel
28544 65 63 74 41 20 3d 20 73 71 6c 69 74 65 33 56 64  ectA = sqlite3Vd
28545 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
28546 3b 0a 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  ;...  /* Generat
28547 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f  e a coroutine to
28548 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 53 45   evaluate the SE
28549 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
2854a 6f 20 74 68 65 0a 20 20 2a 2a 20 6c 65 66 74 20  o the.  ** left 
2854b 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  of the compound 
2854c 6f 70 65 72 61 74 6f 72 20 2d 20 74 68 65 20 22  operator - the "
2854d 41 22 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a  A" select..  */.
2854e 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
2854f 74 28 28 76 2c 20 22 42 65 67 69 6e 20 63 6f 72  t((v, "Begin cor
28550 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20  outine for left 
28551 53 45 4c 45 43 54 22 29 29 3b 0a 20 20 70 50 72  SELECT"));.  pPr
28552 69 6f 72 2d 3e 69 4c 69 6d 69 74 20 3d 20 72 65  ior->iLimit = re
28553 67 4c 69 6d 69 74 41 3b 0a 20 20 73 71 6c 69 74  gLimitA;.  sqlit
28554 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
28555 20 70 50 72 69 6f 72 2c 20 26 64 65 73 74 41 29   pPrior, &destA)
28556 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
28557 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
28558 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 41 29  ger, 1, regEofA)
28559 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2855a 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2855b 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20  d, regAddrA);.  
2855c 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
2855d 28 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69  (v, "End corouti
2855e 6e 65 20 66 6f 72 20 6c 65 66 74 20 53 45 4c 45  ne for left SELE
2855f 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e  CT"));..  /* Gen
28560 65 72 61 74 65 20 61 20 63 6f 72 6f 75 74 69 6e  erate a coroutin
28561 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68  e to evaluate th
28562 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
28563 6e 74 20 6f 6e 20 0a 20 20 2a 2a 20 74 68 65 20  nt on .  ** the 
28564 72 69 67 68 74 20 2d 20 74 68 65 20 22 42 22 20  right - the "B" 
28565 73 65 6c 65 63 74 0a 20 20 2a 2f 0a 20 20 61 64  select.  */.  ad
28566 64 72 53 65 6c 65 63 74 42 20 3d 20 73 71 6c 69  drSelectB = sqli
28567 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
28568 64 72 28 76 29 3b 0a 20 20 56 64 62 65 4e 6f 6f  dr(v);.  VdbeNoo
28569 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65  pComment((v, "Be
2856a 67 69 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f  gin coroutine fo
2856b 72 20 72 69 67 68 74 20 53 45 4c 45 43 54 22 29  r right SELECT")
2856c 29 3b 0a 20 20 73 61 76 65 64 4c 69 6d 69 74 20  );.  savedLimit 
2856d 3d 20 70 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 73  = p->iLimit;.  s
2856e 61 76 65 64 4f 66 66 73 65 74 20 3d 20 70 2d 3e  avedOffset = p->
2856f 69 4f 66 66 73 65 74 3b 0a 20 20 70 2d 3e 69 4c  iOffset;.  p->iL
28570 69 6d 69 74 20 3d 20 72 65 67 4c 69 6d 69 74 42  imit = regLimitB
28571 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d  ;.  p->iOffset =
28572 20 30 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 53   0;  .  sqlite3S
28573 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c  elect(pParse, p,
28574 20 26 64 65 73 74 42 29 3b 0a 20 20 70 2d 3e 69   &destB);.  p->i
28575 4c 69 6d 69 74 20 3d 20 73 61 76 65 64 4c 69 6d  Limit = savedLim
28576 69 74 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74  it;.  p->iOffset
28577 20 3d 20 73 61 76 65 64 4f 66 66 73 65 74 3b 0a   = savedOffset;.
28578 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28579 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2857a 72 2c 20 31 2c 20 72 65 67 45 6f 66 42 29 3b 0a  r, 1, regEofB);.
2857b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2857c 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2857d 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20 56 64   regAddrB);.  Vd
2857e 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2857f 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65  , "End coroutine
28580 20 66 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43   for right SELEC
28581 54 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65  T"));..  /* Gene
28582 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e  rate a subroutin
28583 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20 74  e that outputs t
28584 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f  he current row o
28585 66 20 74 68 65 20 41 0a 20 20 2a 2a 20 73 65 6c  f the A.  ** sel
28586 65 63 74 20 61 73 20 74 68 65 20 6e 65 78 74 20  ect as the next 
28587 6f 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68  output row of th
28588 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  e compound selec
28589 74 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f  t..  */.  VdbeNo
2858a 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f  opComment((v, "O
2858b 75 74 70 75 74 20 72 6f 75 74 69 6e 65 20 66 6f  utput routine fo
2858c 72 20 41 22 29 29 3b 0a 20 20 61 64 64 72 4f 75  r A"));.  addrOu
2858d 74 41 20 3d 20 67 65 6e 65 72 61 74 65 4f 75 74  tA = generateOut
2858e 70 75 74 53 75 62 72 6f 75 74 69 6e 65 28 70 50  putSubroutine(pP
2858f 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
28590 20 20 20 20 20 20 20 70 2c 20 26 64 65 73 74 41         p, &destA
28591 2c 20 70 44 65 73 74 2c 20 72 65 67 4f 75 74 41  , pDest, regOutA
28592 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
28593 20 20 20 72 65 67 50 72 65 76 2c 20 70 4b 65 79     regPrev, pKey
28594 44 75 70 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Dup, P4_KEYINFO_
28595 48 41 4e 44 4f 46 46 2c 20 6c 61 62 65 6c 45 6e  HANDOFF, labelEn
28596 64 29 3b 0a 20 20 0a 20 20 2f 2a 20 47 65 6e 65  d);.  .  /* Gene
28597 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e  rate a subroutin
28598 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20 74  e that outputs t
28599 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f  he current row o
2859a 66 20 74 68 65 20 42 0a 20 20 2a 2a 20 73 65 6c  f the B.  ** sel
2859b 65 63 74 20 61 73 20 74 68 65 20 6e 65 78 74 20  ect as the next 
2859c 6f 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68  output row of th
2859d 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  e compound selec
2859e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70  t..  */.  if( op
2859f 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d 3d  ==TK_ALL || op==
285a0 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20 20  TK_UNION ){.    
285a1 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
285a2 28 76 2c 20 22 4f 75 74 70 75 74 20 72 6f 75 74  (v, "Output rout
285a3 69 6e 65 20 66 6f 72 20 42 22 29 29 3b 0a 20 20  ine for B"));.  
285a4 20 20 61 64 64 72 4f 75 74 42 20 3d 20 67 65 6e    addrOutB = gen
285a5 65 72 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f  erateOutputSubro
285a6 75 74 69 6e 65 28 70 50 61 72 73 65 2c 0a 20 20  utine(pParse,.  
285a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
285a8 2c 20 26 64 65 73 74 42 2c 20 70 44 65 73 74 2c  , &destB, pDest,
285a9 20 72 65 67 4f 75 74 42 2c 0a 20 20 20 20 20 20   regOutB,.      
285aa 20 20 20 20 20 20 20 20 20 20 20 72 65 67 50 72             regPr
285ab 65 76 2c 20 70 4b 65 79 44 75 70 2c 20 50 34 5f  ev, pKeyDup, P4_
285ac 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 2c 20  KEYINFO_STATIC, 
285ad 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 0a 0a  labelEnd);.  }..
285ae 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
285af 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75  subroutine to ru
285b0 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c  n when the resul
285b1 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 41  ts from select A
285b2 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73  .  ** are exhaus
285b3 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74  ted and only dat
285b4 61 20 69 6e 20 73 65 6c 65 63 74 20 42 20 72 65  a in select B re
285b5 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 56 64  mains..  */.  Vd
285b6 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
285b7 2c 20 22 65 6f 66 2d 41 20 73 75 62 72 6f 75 74  , "eof-A subrout
285b8 69 6e 65 22 29 29 3b 0a 20 20 69 66 28 20 6f 70  ine"));.  if( op
285b9 3d 3d 54 4b 5f 45 58 43 45 50 54 20 7c 7c 20 6f  ==TK_EXCEPT || o
285ba 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20  p==TK_INTERSECT 
285bb 29 7b 0a 20 20 20 20 61 64 64 72 45 6f 66 41 20  ){.    addrEofA 
285bc 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
285bd 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
285be 30 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20  0, labelEnd);.  
285bf 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 61 64 64  }else{  .    add
285c0 72 45 6f 66 41 20 3d 20 73 71 6c 69 74 65 33 56  rEofA = sqlite3V
285c1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
285c2 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 6c 61 62  If, regEofB, lab
285c3 65 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69  elEnd);.    sqli
285c4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
285c5 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75   OP_Gosub, regOu
285c6 74 42 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a 20  tB, addrOutB);. 
285c7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
285c8 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
285c9 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20 20  , regAddrB);.   
285ca 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
285cb 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
285cc 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 7d  , addrEofA);.  }
285cd 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
285ce 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20  a subroutine to 
285cf 72 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73  run when the res
285d0 75 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74  ults from select
285d1 20 42 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61   B.  ** are exha
285d2 75 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64  usted and only d
285d3 61 74 61 20 69 6e 20 73 65 6c 65 63 74 20 41 20  ata in select A 
285d4 72 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20  remains..  */.  
285d5 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52  if( op==TK_INTER
285d6 53 45 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72  SECT ){.    addr
285d7 45 6f 66 42 20 3d 20 61 64 64 72 45 6f 66 41 3b  EofB = addrEofA;
285d8 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20  .  }else{  .    
285d9 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
285da 28 76 2c 20 22 65 6f 66 2d 42 20 73 75 62 72 6f  (v, "eof-B subro
285db 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 61 64  utine"));.    ad
285dc 64 72 45 6f 66 42 20 3d 20 73 71 6c 69 74 65 33  drEofB = sqlite3
285dd 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
285de 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 6c 61  _If, regEofA, la
285df 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c  belEnd);.    sql
285e0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
285e1 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f  , OP_Gosub, regO
285e2 75 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a  utA, addrOutA);.
285e3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
285e4 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
285e5 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20  d, regAddrA);.  
285e6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
285e7 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
285e8 30 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a 20 20  0, addrEofB);.  
285e9 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  }..  /* Generate
285ea 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20   code to handle 
285eb 74 68 65 20 63 61 73 65 20 6f 66 20 41 3c 42 0a  the case of A<B.
285ec 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43    */.  VdbeNoopC
285ed 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 6c 74  omment((v, "A-lt
285ee 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29  -B subroutine"))
285ef 3b 0a 20 20 61 64 64 72 41 6c 74 42 20 3d 20 73  ;.  addrAltB = s
285f0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
285f1 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
285f2 67 4f 75 74 41 2c 20 61 64 64 72 4f 75 74 41 29  gOutA, addrOutA)
285f3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
285f4 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
285f5 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20  d, regAddrA);.  
285f6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
285f7 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45  2(v, OP_If, regE
285f8 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a  ofA, addrEofA);.
285f9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
285fa 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
285fb 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a  0, labelCmpr);..
285fc 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
285fd 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65  de to handle the
285fe 20 63 61 73 65 20 6f 66 20 41 3d 3d 42 0a 20 20   case of A==B.  
285ff 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  */.  if( op==TK_
28600 41 4c 4c 20 29 7b 0a 20 20 20 20 61 64 64 72 41  ALL ){.    addrA
28601 65 71 42 20 3d 20 61 64 64 72 41 6c 74 42 3b 0a  eqB = addrAltB;.
28602 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d    }else if( op==
28603 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b 0a  TK_INTERSECT ){.
28604 20 20 20 20 61 64 64 72 41 65 71 42 20 3d 20 61      addrAeqB = a
28605 64 64 72 41 6c 74 42 3b 0a 20 20 20 20 61 64 64  ddrAltB;.    add
28606 72 41 6c 74 42 2b 2b 3b 0a 20 20 7d 65 6c 73 65  rAltB++;.  }else
28607 7b 0a 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f  {.    VdbeNoopCo
28608 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 65 71 2d  mment((v, "A-eq-
28609 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b  B subroutine"));
2860a 0a 20 20 20 20 61 64 64 72 41 65 71 42 20 3d 0a  .    addrAeqB =.
2860b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2860c 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2860d 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20  d, regAddrA);.  
2860e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2860f 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65  Op2(v, OP_If, re
28610 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29  gEofA, addrEofA)
28611 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28612 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
28613 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72  to, 0, labelCmpr
28614 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e  );.  }..  /* Gen
28615 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61  erate code to ha
28616 6e 64 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66  ndle the case of
28617 20 41 3e 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65   A>B.  */.  Vdbe
28618 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
28619 22 41 2d 67 74 2d 42 20 73 75 62 72 6f 75 74 69  "A-gt-B subrouti
2861a 6e 65 22 29 29 3b 0a 20 20 61 64 64 72 41 67 74  ne"));.  addrAgt
2861b 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  B = sqlite3VdbeC
2861c 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
2861d 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20   if( op==TK_ALL 
2861e 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20  || op==TK_UNION 
2861f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
28620 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
28621 6f 73 75 62 2c 20 72 65 67 4f 75 74 42 2c 20 61  osub, regOutB, a
28622 64 64 72 4f 75 74 42 29 3b 0a 20 20 7d 0a 20 20  ddrOutB);.  }.  
28623 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28624 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
28625 65 67 41 64 64 72 42 29 3b 0a 20 20 73 71 6c 69  egAddrB);.  sqli
28626 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
28627 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c   OP_If, regEofB,
28628 20 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 73 71   addrEofB);.  sq
28629 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2862a 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c  v, OP_Goto, 0, l
2862b 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a  abelCmpr);..  /*
2862c 20 54 68 69 73 20 63 6f 64 65 20 72 75 6e 73 20   This code runs 
2862d 6f 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69  once to initiali
2862e 7a 65 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20  ze everything.. 
2862f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
28630 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29  eJumpHere(v, j1)
28631 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
28632 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
28633 67 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 41 29  ger, 0, regEofA)
28634 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
28635 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
28636 67 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 42 29  ger, 0, regEofB)
28637 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
28638 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75  ddOp2(v, OP_Gosu
28639 62 2c 20 72 65 67 41 64 64 72 41 2c 20 61 64 64  b, regAddrA, add
2863a 72 53 65 6c 65 63 74 41 29 3b 0a 20 20 73 71 6c  rSelectA);.  sql
2863b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2863c 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 41  , OP_Gosub, regA
2863d 64 64 72 42 2c 20 61 64 64 72 53 65 6c 65 63 74  ddrB, addrSelect
2863e 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  B);.  sqlite3Vdb
2863f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
28640 2c 20 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45  , regEofA, addrE
28641 6f 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ofA);.  sqlite3V
28642 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28643 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 61 64 64  If, regEofB, add
28644 72 45 6f 66 42 29 3b 0a 0a 20 20 2f 2a 20 49 6d  rEofB);..  /* Im
28645 70 6c 65 6d 65 6e 74 20 74 68 65 20 6d 61 69 6e  plement the main
28646 20 6d 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f   merge loop.  */
28647 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
28648 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c 61  solveLabel(v, la
28649 62 65 6c 43 6d 70 72 29 3b 0a 20 20 73 71 6c 69  belCmpr);.  sqli
2864a 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2864b 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c   OP_Permutation,
2864c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 2a   0, 0, 0, (char*
2864d 29 61 50 65 72 6d 75 74 65 2c 20 50 34 5f 49 4e  )aPermute, P4_IN
2864e 54 41 52 52 41 59 29 3b 0a 20 20 73 71 6c 69 74  TARRAY);.  sqlit
2864f 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
28650 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 64 65 73 74  OP_Compare, dest
28651 41 2e 69 4d 65 6d 2c 20 64 65 73 74 42 2e 69 4d  A.iMem, destB.iM
28652 65 6d 2c 20 6e 4f 72 64 65 72 42 79 2c 0a 20 20  em, nOrderBy,.  
28653 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28654 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
28655 65 79 4d 65 72 67 65 2c 20 50 34 5f 4b 45 59 49  eyMerge, P4_KEYI
28656 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
28657 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28658 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 61 64  3(v, OP_Jump, ad
28659 64 72 41 6c 74 42 2c 20 61 64 64 72 41 65 71 42  drAltB, addrAeqB
2865a 2c 20 61 64 64 72 41 67 74 42 29 3b 0a 0a 20 20  , addrAgtB);..  
2865b 2f 2a 20 52 65 6c 65 61 73 65 20 74 65 6d 70 6f  /* Release tempo
2865c 72 61 72 79 20 72 65 67 69 73 74 65 72 73 0a 20  rary registers. 
2865d 20 2a 2f 0a 20 20 69 66 28 20 72 65 67 50 72 65   */.  if( regPre
2865e 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
2865f 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
28660 28 70 50 61 72 73 65 2c 20 72 65 67 50 72 65 76  (pParse, regPrev
28661 2c 20 6e 4f 72 64 65 72 42 79 2b 31 29 3b 0a 20  , nOrderBy+1);. 
28662 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f   }..  /* Jump to
28663 20 74 68 65 20 74 68 69 73 20 70 6f 69 6e 74 20   the this point 
28664 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 65 72 6d  in order to term
28665 69 6e 61 74 65 20 74 68 65 20 71 75 65 72 79 2e  inate the query.
28666 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
28667 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
28668 76 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a 20  v, labelEnd);.. 
28669 20 2f 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62   /* Set the numb
2866a 65 72 20 6f 66 20 6f 75 74 70 75 74 20 63 6f 6c  er of output col
2866b 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20  umns.  */.  if( 
2866c 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
2866d 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20  T_Output ){.    
2866e 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d  Select *pFirst =
2866f 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 77 68 69   pPrior;.    whi
28670 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69  le( pFirst->pPri
28671 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70 46  or ) pFirst = pF
28672 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20  irst->pPrior;.  
28673 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e    generateColumn
28674 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c  Names(pParse, 0,
28675 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29   pFirst->pEList)
28676 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 73  ;.  }..  /* Reas
28677 73 65 6d 62 6c 79 20 74 68 65 20 63 6f 6d 70 6f  sembly the compo
28678 75 6e 64 20 71 75 65 72 79 20 73 6f 20 74 68 61  und query so tha
28679 74 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65  t it will be fre
2867a 65 64 20 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a  ed correctly.  *
2867b 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  * by the calling
2867c 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69   function */.  i
2867d 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a  f( p->pPrior ){.
2867e 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2867f 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  tDelete(db, p->p
28680 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 70 2d  Prior);.  }.  p-
28681 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72  >pPrior = pPrior
28682 3b 0a 0a 20 20 2f 2a 2a 2a 20 54 42 44 3a 20 20  ;..  /*** TBD:  
28683 49 6e 73 65 72 74 20 73 75 62 72 6f 75 74 69 6e  Insert subroutin
28684 65 20 63 61 6c 6c 73 20 74 6f 20 63 6c 6f 73 65  e calls to close
28685 20 63 75 72 73 6f 72 73 20 6f 6e 20 69 6e 63 6f   cursors on inco
28686 6d 70 6c 65 74 65 0a 20 20 2a 2a 2a 2a 20 73 75  mplete.  **** su
28687 62 71 75 65 72 69 65 73 20 2a 2a 2a 2a 2f 0a 20  bqueries ****/. 
28688 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
28689 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  K;.}.#endif..#if
2868a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2868b 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20  _OMIT_SUBQUERY) 
2868c 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
2868d 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a  TE_OMIT_VIEW)./*
2868e 20 46 6f 72 77 61 72 64 20 44 65 63 6c 61 72 61   Forward Declara
2868f 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20  tions */.static 
28690 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 4c 69  void substExprLi
28691 73 74 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70  st(sqlite3*, Exp
28692 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 45 78 70  rList*, int, Exp
28693 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69 63 20  rList*);.static 
28694 76 6f 69 64 20 73 75 62 73 74 53 65 6c 65 63 74  void substSelect
28695 28 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c 65 63  (sqlite3*, Selec
28696 74 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69  t *, int, ExprLi
28697 73 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63  st *);../*.** Sc
28698 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 65  an through the e
28699 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e  xpression pExpr.
2869a 20 20 52 65 70 6c 61 63 65 20 65 76 65 72 79 20    Replace every 
2869b 72 65 66 65 72 65 6e 63 65 20 74 6f 0a 2a 2a 20  reference to.** 
2869c 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c  a column in tabl
2869d 65 20 6e 75 6d 62 65 72 20 69 54 61 62 6c 65 20  e number iTable 
2869e 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74  with a copy of t
2869f 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a  he iColumn-th.**
286a0 20 65 6e 74 72 79 20 69 6e 20 70 45 4c 69 73 74   entry in pEList
286a1 2e 20 20 28 42 75 74 20 6c 65 61 76 65 20 72 65  .  (But leave re
286a2 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
286a3 52 4f 57 49 44 20 63 6f 6c 75 6d 6e 20 0a 2a 2a  ROWID column .**
286a4 20 75 6e 63 68 61 6e 67 65 64 2e 29 0a 2a 2a 0a   unchanged.).**.
286a5 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
286a6 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 66  is part of the f
286a7 6c 61 74 74 65 6e 69 6e 67 20 70 72 6f 63 65 64  lattening proced
286a8 75 72 65 2e 20 20 41 20 73 75 62 71 75 65 72 79  ure.  A subquery
286a9 0a 2a 2a 20 77 68 6f 73 65 20 72 65 73 75 6c 74  .** whose result
286aa 20 73 65 74 20 69 73 20 64 65 66 69 6e 65 64 20   set is defined 
286ab 62 79 20 70 45 4c 69 73 74 20 61 70 70 65 61 72  by pEList appear
286ac 73 20 61 73 20 65 6e 74 72 79 20 69 6e 20 74 68  s as entry in th
286ad 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65  e.** FROM clause
286ae 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 75 63   of a SELECT suc
286af 68 20 74 68 61 74 20 74 68 65 20 56 44 42 45 20  h that the VDBE 
286b0 63 75 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20  cursor assigned 
286b1 74 6f 20 74 68 61 74 0a 2a 2a 20 46 4f 52 4d 20  to that.** FORM 
286b2 63 6c 61 75 73 65 20 65 6e 74 72 79 20 69 73 20  clause entry is 
286b3 69 54 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f  iTable.  This ro
286b4 75 74 69 6e 65 20 6d 61 6b 65 20 74 68 65 20 6e  utine make the n
286b5 65 63 65 73 73 61 72 79 20 0a 2a 2a 20 63 68 61  ecessary .** cha
286b6 6e 67 65 73 20 74 6f 20 70 45 78 70 72 20 73 6f  nges to pExpr so
286b7 20 74 68 61 74 20 69 74 20 72 65 66 65 72 73 20   that it refers 
286b8 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20  directly to the 
286b9 73 6f 75 72 63 65 20 74 61 62 6c 65 0a 2a 2a 20  source table.** 
286ba 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  of the subquery 
286bb 72 61 74 68 65 72 20 74 68 65 20 72 65 73 75 6c  rather the resul
286bc 74 20 73 65 74 20 6f 66 20 74 68 65 20 73 75 62  t set of the sub
286bd 71 75 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  query..*/.static
286be 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 28   void substExpr(
286bf 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
286c0 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f 72 74         /* Report
286c1 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 74   malloc errors t
286c2 6f 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f  o this connectio
286c3 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  n */.  Expr *pEx
286c4 70 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78  pr,        /* Ex
286c5 70 72 20 69 6e 20 77 68 69 63 68 20 73 75 62 73  pr in which subs
286c6 74 69 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 20  titution occurs 
286c7 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c  */.  int iTable,
286c8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
286c9 65 20 74 6f 20 62 65 20 73 75 62 73 74 69 74 75  e to be substitu
286ca 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ted */.  ExprLis
286cb 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20  t *pEList    /* 
286cc 53 75 62 73 74 69 74 75 74 65 20 65 78 70 72 65  Substitute expre
286cd 73 73 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 69  ssions */.){.  i
286ce 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65  f( pExpr==0 ) re
286cf 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 70  turn;.  if( pExp
286d0 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op==TK_COLUMN
286d1 20 26 26 20 70 45 78 70 72 2d 3e 69 54 61 62 6c   && pExpr->iTabl
286d2 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20 20  e==iTable ){.   
286d3 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c   if( pExpr->iCol
286d4 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 70  umn<0 ){.      p
286d5 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55  Expr->op = TK_NU
286d6 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  LL;.    }else{. 
286d7 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 3b       Expr *pNew;
286d8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
286d9 45 4c 69 73 74 21 3d 30 20 26 26 20 70 45 78 70  EList!=0 && pExp
286da 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 45 4c 69 73  r->iColumn<pELis
286db 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20 20  t->nExpr );.    
286dc 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
286dd 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78  >pLeft==0 && pEx
286de 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b  pr->pRight==0 );
286df 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 45  .      pNew = pE
286e0 4c 69 73 74 2d 3e 61 5b 70 45 78 70 72 2d 3e 69  List->a[pExpr->i
286e1 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 3b 0a 20  Column].pExpr;. 
286e2 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65       assert( pNe
286e3 77 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45  w!=0 );.      pE
286e4 78 70 72 2d 3e 6f 70 20 3d 20 70 4e 65 77 2d 3e  xpr->op = pNew->
286e5 6f 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  op;.      assert
286e6 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d  ( pExpr->pLeft==
286e7 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72  0 );.      pExpr
286e8 2d 3e 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65  ->pLeft = sqlite
286e9 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4e 65  3ExprDup(db, pNe
286ea 77 2d 3e 70 4c 65 66 74 2c 20 30 29 3b 0a 20 20  w->pLeft, 0);.  
286eb 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
286ec 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a  r->pRight==0 );.
286ed 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69        pExpr->pRi
286ee 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ght = sqlite3Exp
286ef 72 44 75 70 28 64 62 2c 20 70 4e 65 77 2d 3e 70  rDup(db, pNew->p
286f0 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20  Right, 0);.     
286f1 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
286f2 20 70 4e 65 77 2d 3e 69 54 61 62 6c 65 3b 0a 20   pNew->iTable;. 
286f3 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62       pExpr->pTab
286f4 20 3d 20 70 4e 65 77 2d 3e 70 54 61 62 3b 0a 20   = pNew->pTab;. 
286f5 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
286f6 75 6d 6e 20 3d 20 70 4e 65 77 2d 3e 69 43 6f 6c  umn = pNew->iCol
286f7 75 6d 6e 3b 0a 20 20 20 20 20 20 70 45 78 70 72  umn;.      pExpr
286f8 2d 3e 69 41 67 67 20 3d 20 70 4e 65 77 2d 3e 69  ->iAgg = pNew->i
286f9 41 67 67 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Agg;.      sqlit
286fa 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20  e3TokenCopy(db, 
286fb 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2c 20 26  &pExpr->token, &
286fc 70 4e 65 77 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20  pNew->token);.  
286fd 20 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e      sqlite3Token
286fe 43 6f 70 79 28 64 62 2c 20 26 70 45 78 70 72 2d  Copy(db, &pExpr-
286ff 3e 73 70 61 6e 2c 20 26 70 4e 65 77 2d 3e 73 70  >span, &pNew->sp
28700 61 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  an);.      asser
28701 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  t( pExpr->x.pLis
28702 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 78  t==0 && pExpr->x
28703 2e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20  .pSelect==0 );. 
28704 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73       if( ExprHas
28705 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45  Property(pNew, E
28706 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a  P_xIsSelect) ){.
28707 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 78          pExpr->x
28708 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  .pSelect = sqlit
28709 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
2870a 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 2c  pNew->x.pSelect,
2870b 20 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   0);.      }else
2870c 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  {.        pExpr-
2870d 3e 78 2e 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  >x.pList = sqlit
2870e 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
2870f 2c 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 2c  , pNew->x.pList,
28710 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
28711 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20     pExpr->flags 
28712 3d 20 70 4e 65 77 2d 3e 66 6c 61 67 73 3b 0a 20  = pNew->flags;. 
28713 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67       pExpr->pAgg
28714 49 6e 66 6f 20 3d 20 70 4e 65 77 2d 3e 70 41 67  Info = pNew->pAg
28715 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 70 4e 65  gInfo;.      pNe
28716 77 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 30 3b  w->pAggInfo = 0;
28717 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
28718 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64 62      substExpr(db
28719 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
2871a 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2871b 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64  .    substExpr(d
2871c 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  b, pExpr->pRight
2871d 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
2871e 29 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72 48  );.    if( ExprH
2871f 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
28720 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
28721 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74 53 65  ){.      substSe
28722 6c 65 63 74 28 64 62 2c 20 70 45 78 70 72 2d 3e  lect(db, pExpr->
28723 78 2e 70 53 65 6c 65 63 74 2c 20 69 54 61 62 6c  x.pSelect, iTabl
28724 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  e, pEList);.    
28725 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 75 62  }else{.      sub
28726 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70  stExprList(db, p
28727 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 20 69  Expr->x.pList, i
28728 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
28729 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74      }.  }.}.stat
2872a 69 63 20 76 6f 69 64 20 73 75 62 73 74 45 78 70  ic void substExp
2872b 72 4c 69 73 74 28 0a 20 20 73 71 6c 69 74 65 33  rList(.  sqlite3
2872c 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a   *db,         /*
2872d 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65   Report malloc e
2872e 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20  rrors here */.  
2872f 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
28730 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20       /* List to 
28731 73 63 61 6e 20 61 6e 64 20 69 6e 20 77 68 69 63  scan and in whic
28732 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69  h to make substi
28733 74 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69  tutes */.  int i
28734 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20  Table,          
28735 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73  /* Table to be s
28736 75 62 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20  ubstituted */.  
28737 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
28738 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75       /* Substitu
28739 74 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a  te values */.){.
2873a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70    int i;.  if( p
2873b 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
2873c 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2873d 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
2873e 29 7b 0a 20 20 20 20 73 75 62 73 74 45 78 70 72  ){.    substExpr
2873f 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  (db, pList->a[i]
28740 2e 70 45 78 70 72 2c 20 69 54 61 62 6c 65 2c 20  .pExpr, iTable, 
28741 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73  pEList);.  }.}.s
28742 74 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74  tatic void subst
28743 53 65 6c 65 63 74 28 0a 20 20 73 71 6c 69 74 65  Select(.  sqlite
28744 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f  3 *db,         /
28745 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20  * Report malloc 
28746 65 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20  errors here */. 
28747 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20   Select *p,     
28748 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20        /* SELECT 
28749 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 77 68 69  statement in whi
2874a 63 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74  ch to make subst
2874b 69 74 75 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  itutions */.  in
2874c 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20  t iTable,       
2874d 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62     /* Table to b
2874e 65 20 72 65 70 6c 61 63 65 64 20 2a 2f 0a 20 20  e replaced */.  
2874f 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
28750 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75       /* Substitu
28751 74 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a  te values */.){.
28752 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
28753 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
28754 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
28755 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 70   int i;.  if( !p
28756 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 75 62   ) return;.  sub
28757 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70  stExprList(db, p
28758 2d 3e 70 45 4c 69 73 74 2c 20 69 54 61 62 6c 65  ->pEList, iTable
28759 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62  , pEList);.  sub
2875a 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70  stExprList(db, p
2875b 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69 54 61 62  ->pGroupBy, iTab
2875c 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73  le, pEList);.  s
2875d 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c  ubstExprList(db,
2875e 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 54   p->pOrderBy, iT
2875f 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
28760 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70   substExpr(db, p
28761 2d 3e 70 48 61 76 69 6e 67 2c 20 69 54 61 62 6c  ->pHaving, iTabl
28762 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75  e, pEList);.  su
28763 62 73 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70  bstExpr(db, p->p
28764 57 68 65 72 65 2c 20 69 54 61 62 6c 65 2c 20 70  Where, iTable, p
28765 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 74 53  EList);.  substS
28766 65 6c 65 63 74 28 64 62 2c 20 70 2d 3e 70 50 72  elect(db, p->pPr
28767 69 6f 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ior, iTable, pEL
28768 69 73 74 29 3b 0a 20 20 70 53 72 63 20 3d 20 70  ist);.  pSrc = p
28769 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74  ->pSrc;.  assert
2876a 28 20 70 53 72 63 20 29 3b 20 20 2f 2a 20 45 76  ( pSrc );  /* Ev
2876b 65 6e 20 66 6f 72 20 28 53 45 4c 45 43 54 20 31  en for (SELECT 1
2876c 29 20 77 65 20 68 61 76 65 3a 20 70 53 72 63 21  ) we have: pSrc!
2876d 3d 30 20 62 75 74 20 70 53 72 63 2d 3e 6e 53 72  =0 but pSrc->nSr
2876e 63 3d 3d 30 20 2a 2f 0a 20 20 69 66 28 20 41 4c  c==0 */.  if( AL
2876f 57 41 59 53 28 70 53 72 63 29 20 29 7b 0a 20 20  WAYS(pSrc) ){.  
28770 20 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53    for(i=pSrc->nS
28771 72 63 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e  rc, pItem=pSrc->
28772 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74  a; i>0; i--, pIt
28773 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 73 75 62  em++){.      sub
28774 73 74 53 65 6c 65 63 74 28 64 62 2c 20 70 49 74  stSelect(db, pIt
28775 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 69 54 61  em->pSelect, iTa
28776 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
28777 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66    }.  }.}.#endif
28778 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
28779 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2877a 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  Y) || !defined(S
2877b 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
2877c 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65   */..#if !define
2877d 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
2877e 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66 69  BQUERY) || !defi
2877f 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
28780 56 49 45 57 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73  VIEW)./*.** This
28781 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74   routine attempt
28782 73 20 74 6f 20 66 6c 61 74 74 65 6e 20 73 75 62  s to flatten sub
28783 71 75 65 72 69 65 73 20 69 6e 20 6f 72 64 65 72  queries in order
28784 20 74 6f 20 73 70 65 65 64 0a 2a 2a 20 65 78 65   to speed.** exe
28785 63 75 74 69 6f 6e 2e 20 20 49 74 20 72 65 74 75  cution.  It retu
28786 72 6e 73 20 31 20 69 66 20 69 74 20 6d 61 6b 65  rns 1 if it make
28787 73 20 63 68 61 6e 67 65 73 20 61 6e 64 20 30 20  s changes and 0 
28788 69 66 20 6e 6f 20 66 6c 61 74 74 65 6e 69 6e 67  if no flattening
28789 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a  .** occurs..**.*
2878a 2a 20 54 6f 20 75 6e 64 65 72 73 74 61 6e 64 20  * To understand 
2878b 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 66  the concept of f
2878c 6c 61 74 74 65 6e 69 6e 67 2c 20 63 6f 6e 73 69  lattening, consi
2878d 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  der the followin
2878e 67 0a 2a 2a 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a  g.** query:.**.*
2878f 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 20 46  *     SELECT a F
28790 52 4f 4d 20 28 53 45 4c 45 43 54 20 78 2b 79 20  ROM (SELECT x+y 
28791 41 53 20 61 20 46 52 4f 4d 20 74 31 20 57 48 45  AS a FROM t1 WHE
28792 52 45 20 7a 3c 31 30 30 29 20 57 48 45 52 45 20  RE z<100) WHERE 
28793 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65  a>5.**.** The de
28794 66 61 75 6c 74 20 77 61 79 20 6f 66 20 69 6d 70  fault way of imp
28795 6c 65 6d 65 6e 74 69 6e 67 20 74 68 69 73 20 71  lementing this q
28796 75 65 72 79 20 69 73 20 74 6f 20 65 78 65 63 75  uery is to execu
28797 74 65 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65  te the.** subque
28798 72 79 20 66 69 72 73 74 20 61 6e 64 20 73 74 6f  ry first and sto
28799 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 69  re the results i
2879a 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  n a temporary ta
2879b 62 6c 65 2c 20 74 68 65 6e 0a 2a 2a 20 72 75 6e  ble, then.** run
2879c 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2879d 20 6f 6e 20 74 68 61 74 20 74 65 6d 70 6f 72 61   on that tempora
2879e 72 79 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  ry table.  This 
2879f 72 65 71 75 69 72 65 73 20 74 77 6f 0a 2a 2a 20  requires two.** 
287a0 70 61 73 73 65 73 20 6f 76 65 72 20 74 68 65 20  passes over the 
287a1 64 61 74 61 2e 20 20 46 75 72 74 68 65 72 6d 6f  data.  Furthermo
287a2 72 65 2c 20 62 65 63 61 75 73 65 20 74 68 65 20  re, because the 
287a3 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a  temporary table.
287a4 2a 2a 20 68 61 73 20 6e 6f 20 69 6e 64 69 63 65  ** has no indice
287a5 73 2c 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  s, the WHERE cla
287a6 75 73 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72  use on the outer
287a7 20 71 75 65 72 79 20 63 61 6e 6e 6f 74 20 62 65   query cannot be
287a8 0a 2a 2a 20 6f 70 74 69 6d 69 7a 65 64 2e 0a 2a  .** optimized..*
287a9 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
287aa 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65  e attempts to re
287ab 77 72 69 74 65 20 71 75 65 72 69 65 73 20 73 75  write queries su
287ac 63 68 20 61 73 20 74 68 65 20 61 62 6f 76 65 20  ch as the above 
287ad 69 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65  into.** a single
287ae 20 66 6c 61 74 20 73 65 6c 65 63 74 2c 20 6c 69   flat select, li
287af 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
287b0 20 20 20 53 45 4c 45 43 54 20 78 2b 79 20 41 53     SELECT x+y AS
287b1 20 61 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   a FROM t1 WHERE
287b2 20 7a 3c 31 30 30 20 41 4e 44 20 61 3e 35 0a 2a   z<100 AND a>5.*
287b3 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65  *.** The code ge
287b4 6e 65 72 61 74 65 64 20 66 6f 72 20 74 68 69 73  nerated for this
287b5 20 73 69 6d 70 69 66 69 63 61 74 69 6f 6e 20 67   simpification g
287b6 69 76 65 73 20 74 68 65 20 73 61 6d 65 20 72 65  ives the same re
287b7 73 75 6c 74 0a 2a 2a 20 62 75 74 20 6f 6e 6c 79  sult.** but only
287b8 20 68 61 73 20 74 6f 20 73 63 61 6e 20 74 68 65   has to scan the
287b9 20 64 61 74 61 20 6f 6e 63 65 2e 20 20 41 6e 64   data once.  And
287ba 20 62 65 63 61 75 73 65 20 69 6e 64 69 63 65 73   because indices
287bb 20 6d 69 67 68 74 20 0a 2a 2a 20 65 78 69 73 74   might .** exist
287bc 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 31   on the table t1
287bd 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20 73 63 61  , a complete sca
287be 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 6d 69  n of the data mi
287bf 67 68 74 20 62 65 0a 2a 2a 20 61 76 6f 69 64 65  ght be.** avoide
287c0 64 2e 0a 2a 2a 0a 2a 2a 20 46 6c 61 74 74 65 6e  d..**.** Flatten
287c1 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 74 74 65  ing is only atte
287c2 6d 70 74 65 64 20 69 66 20 61 6c 6c 20 6f 66 20  mpted if all of 
287c3 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72  the following ar
287c4 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  e true:.**.**   
287c5 28 31 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (1)  The subquer
287c6 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20  y and the outer 
287c7 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74  query do not bot
287c8 68 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73  h use aggregates
287c9 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20 20 54  ..**.**   (2)  T
287ca 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 6e  he subquery is n
287cb 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  ot an aggregate 
287cc 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
287cd 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e  ry is not a join
287ce 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 20 54  ..**.**   (3)  T
287cf 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 6e  he subquery is n
287d0 6f 74 20 74 68 65 20 72 69 67 68 74 20 6f 70 65  ot the right ope
287d1 72 61 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f  rand of a left o
287d2 75 74 65 72 20 6a 6f 69 6e 0a 2a 2a 20 20 20 20  uter join.**    
287d3 20 20 20 20 28 4f 72 69 67 69 6e 61 6c 6c 79 20      (Originally 
287d4 74 69 63 6b 65 74 20 23 33 30 36 2e 20 20 53 74  ticket #306.  St
287d5 72 65 6e 67 68 74 65 6e 65 64 20 62 79 20 74 69  renghtened by ti
287d6 63 6b 65 74 20 23 33 33 30 30 29 0a 2a 2a 0a 2a  cket #3300).**.*
287d7 2a 20 20 20 28 34 29 20 20 54 68 65 20 73 75 62  *   (4)  The sub
287d8 71 75 65 72 79 20 69 73 20 6e 6f 74 20 44 49 53  query is not DIS
287d9 54 49 4e 43 54 20 6f 72 20 74 68 65 20 6f 75 74  TINCT or the out
287da 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20  er query is not 
287db 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  a join..**.**   
287dc 28 35 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (5)  The subquer
287dd 79 20 69 73 20 6e 6f 74 20 44 49 53 54 49 4e 43  y is not DISTINC
287de 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71  T or the outer q
287df 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
287e0 65 0a 2a 2a 20 20 20 20 20 20 20 20 61 67 67 72  e.**        aggr
287e1 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  egates..**.**   
287e2 28 36 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (6)  The subquer
287e3 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 61  y does not use a
287e4 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 68 65  ggregates or the
287e5 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
287e6 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 44 49  not.**        DI
287e7 53 54 49 4e 43 54 2e 0a 2a 2a 0a 2a 2a 20 20 20  STINCT..**.**   
287e8 28 37 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (7)  The subquer
287e9 79 20 68 61 73 20 61 20 46 52 4f 4d 20 63 6c 61  y has a FROM cla
287ea 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 38 29  use..**.**   (8)
287eb 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64    The subquery d
287ec 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49  oes not use LIMI
287ed 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71  T or the outer q
287ee 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f  uery is not a jo
287ef 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 39 29 20  in..**.**   (9) 
287f0 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
287f1 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54  es not use LIMIT
287f2 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
287f3 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65  ery does not use
287f4 0a 2a 2a 20 20 20 20 20 20 20 20 61 67 67 72 65  .**        aggre
287f5 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31  gates..**.**  (1
287f6 30 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  0)  The subquery
287f7 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67   does not use ag
287f8 67 72 65 67 61 74 65 73 20 6f 72 20 74 68 65 20  gregates or the 
287f9 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73  outer query does
287fa 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 75   not.**        u
287fb 73 65 20 4c 49 4d 49 54 2e 0a 2a 2a 0a 2a 2a 20  se LIMIT..**.** 
287fc 20 28 31 31 29 20 20 54 68 65 20 73 75 62 71 75   (11)  The subqu
287fd 65 72 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65  ery and the oute
287fe 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62  r query do not b
287ff 6f 74 68 20 68 61 76 65 20 4f 52 44 45 52 20 42  oth have ORDER B
28800 59 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a  Y clauses..**.**
28801 20 20 28 31 32 29 20 20 4e 6f 74 20 69 6d 70 6c    (12)  Not impl
28802 65 6d 65 6e 74 65 64 2e 20 20 53 75 62 73 75 6d  emented.  Subsum
28803 65 64 20 69 6e 74 6f 20 72 65 73 74 72 69 63 74  ed into restrict
28804 69 6f 6e 20 28 33 29 2e 20 20 57 61 73 20 70 72  ion (3).  Was pr
28805 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 20  eviously.**     
28806 20 20 20 61 20 73 65 70 61 72 61 74 65 20 72 65     a separate re
28807 73 74 72 69 63 74 69 6f 6e 20 64 65 72 69 76 69  striction derivi
28808 6e 67 20 66 72 6f 6d 20 74 69 63 6b 65 74 20 23  ng from ticket #
28809 33 35 30 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 33 29  350..**.**  (13)
2880a 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 61    The subquery a
2880b 6e 64 20 6f 75 74 65 72 20 71 75 65 72 79 20 64  nd outer query d
2880c 6f 20 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 4c  o not both use L
2880d 49 4d 49 54 0a 2a 2a 0a 2a 2a 20 20 28 31 34 29  IMIT.**.**  (14)
2880e 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64    The subquery d
2880f 6f 65 73 20 6e 6f 74 20 75 73 65 20 4f 46 46 53  oes not use OFFS
28810 45 54 0a 2a 2a 0a 2a 2a 20 20 28 31 35 29 20 20  ET.**.**  (15)  
28811 54 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  The outer query 
28812 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61  is not part of a
28813 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
28814 20 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   or the.**      
28815 20 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20    subquery does 
28816 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20 61 6e  not have both an
28817 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 61 20   ORDER BY and a 
28818 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a 2a  LIMIT clause..**
28819 20 20 20 20 20 20 20 20 28 53 65 65 20 74 69 63          (See tic
2881a 6b 65 74 20 23 32 33 33 39 29 0a 2a 2a 0a 2a 2a  ket #2339).**.**
2881b 20 20 28 31 36 29 20 20 54 68 65 20 6f 75 74 65    (16)  The oute
2881c 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61  r query is not a
2881d 6e 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 74  n aggregate or t
2881e 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
2881f 0a 2a 2a 20 20 20 20 20 20 20 20 6e 6f 74 20 63  .**        not c
28820 6f 6e 74 61 69 6e 20 4f 52 44 45 52 20 42 59 2e  ontain ORDER BY.
28821 20 20 28 54 69 63 6b 65 74 20 23 32 39 34 32 29    (Ticket #2942)
28822 20 20 54 68 69 73 20 75 73 65 64 20 74 6f 20 6e    This used to n
28823 6f 74 20 6d 61 74 74 65 72 0a 2a 2a 20 20 20 20  ot matter.**    
28824 20 20 20 20 75 6e 74 69 6c 20 77 65 20 69 6e 74      until we int
28825 72 6f 64 75 63 65 64 20 74 68 65 20 67 72 6f 75  roduced the grou
28826 70 5f 63 6f 6e 63 61 74 28 29 20 66 75 6e 63 74  p_concat() funct
28827 69 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 28 31  ion.  .**.**  (1
28828 37 29 20 20 54 68 65 20 73 75 62 2d 71 75 65 72  7)  The sub-quer
28829 79 20 69 73 20 6e 6f 74 20 61 20 63 6f 6d 70 6f  y is not a compo
2882a 75 6e 64 20 73 65 6c 65 63 74 2c 20 6f 72 20 69  und select, or i
2882b 74 20 69 73 20 61 20 55 4e 49 4f 4e 20 41 4c 4c  t is a UNION ALL
2882c 20 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f 6d 70   .**        comp
2882d 6f 75 6e 64 20 63 6c 61 75 73 65 20 6d 61 64 65  ound clause made
2882e 20 75 70 20 65 6e 74 69 72 65 6c 79 20 6f 66 20   up entirely of 
2882f 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75  non-aggregate qu
28830 65 72 69 65 73 2c 20 61 6e 64 20 0a 2a 2a 20 20  eries, and .**  
28831 20 20 20 20 20 20 74 68 65 20 70 61 72 65 6e 74        the parent
28832 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20   query:.**.**   
28833 20 20 20 20 20 20 20 2a 20 69 73 20 6e 6f 74 20         * is not 
28834 69 74 73 65 6c 66 20 70 61 72 74 20 6f 66 20 61  itself part of a
28835 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
28836 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20  ,.**          * 
28837 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67  is not an aggreg
28838 61 74 65 20 6f 72 20 44 49 53 54 49 4e 43 54 20  ate or DISTINCT 
28839 71 75 65 72 79 2c 20 61 6e 64 0a 2a 2a 20 20 20  query, and.**   
2883a 20 20 20 20 20 20 20 2a 20 68 61 73 20 6e 6f 20         * has no 
2883b 6f 74 68 65 72 20 74 61 62 6c 65 73 20 6f 72 20  other tables or 
2883c 73 75 62 2d 73 65 6c 65 63 74 73 20 69 6e 20 74  sub-selects in t
2883d 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a  he FROM clause..
2883e 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 54 68 65  **.**        The
2883f 20 70 61 72 65 6e 74 20 61 6e 64 20 73 75 62 2d   parent and sub-
28840 71 75 65 72 79 20 6d 61 79 20 63 6f 6e 74 61 69  query may contai
28841 6e 20 57 48 45 52 45 20 63 6c 61 75 73 65 73 2e  n WHERE clauses.
28842 20 53 75 62 6a 65 63 74 20 74 6f 0a 2a 2a 20 20   Subject to.**  
28843 20 20 20 20 20 20 72 75 6c 65 73 20 28 31 31 29        rules (11)
28844 2c 20 28 31 33 29 20 61 6e 64 20 28 31 34 29 2c  , (13) and (14),
28845 20 74 68 65 79 20 6d 61 79 20 61 6c 73 6f 20 63   they may also c
28846 6f 6e 74 61 69 6e 20 4f 52 44 45 52 20 42 59 2c  ontain ORDER BY,
28847 0a 2a 2a 20 20 20 20 20 20 20 20 4c 49 4d 49 54  .**        LIMIT
28848 20 61 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75   and OFFSET clau
28849 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 38 29  ses..**.**  (18)
2884a 20 20 49 66 20 74 68 65 20 73 75 62 2d 71 75 65    If the sub-que
2884b 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64  ry is a compound
2884c 20 73 65 6c 65 63 74 2c 20 74 68 65 6e 20 61 6c   select, then al
2884d 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 0a 2a  l terms of the.*
2884e 2a 20 20 20 20 20 20 20 20 4f 52 44 45 52 20 62  *        ORDER b
2884f 79 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  y clause of the 
28850 70 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20 73  parent must be s
28851 69 6d 70 6c 65 20 72 65 66 65 72 65 6e 63 65 73  imple references
28852 20 74 6f 20 0a 2a 2a 20 20 20 20 20 20 20 20 63   to .**        c
28853 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 75  olumns of the su
28854 62 2d 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20  b-query..**.**  
28855 28 31 39 29 20 20 54 68 65 20 73 75 62 71 75 65  (19)  The subque
28856 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20  ry does not use 
28857 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f 75 74  LIMIT or the out
28858 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  er query does no
28859 74 0a 2a 2a 20 20 20 20 20 20 20 20 68 61 76 65  t.**        have
2885a 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e   a WHERE clause.
2885b 0a 2a 2a 0a 2a 2a 20 20 28 32 30 29 20 20 49 66  .**.**  (20)  If
2885c 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69   the sub-query i
2885d 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  s a compound sel
2885e 65 63 74 2c 20 74 68 65 6e 20 69 74 20 6d 75 73  ect, then it mus
2885f 74 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20  t not use.**    
28860 20 20 20 20 61 6e 20 4f 52 44 45 52 20 42 59 20      an ORDER BY 
28861 63 6c 61 75 73 65 2e 20 20 54 69 63 6b 65 74 20  clause.  Ticket 
28862 23 33 37 37 33 2e 20 20 57 65 20 63 6f 75 6c 64  #3773.  We could
28863 20 72 65 6c 61 78 20 74 68 69 73 20 63 6f 6e 73   relax this cons
28864 74 72 61 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20  traint.**       
28865 20 73 6f 6d 65 77 68 61 74 20 62 79 20 73 61 79   somewhat by say
28866 69 6e 67 20 74 68 61 74 20 74 68 65 20 74 65 72  ing that the ter
28867 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  ms of the ORDER 
28868 42 59 20 63 6c 61 75 73 65 20 6d 75 73 74 0a 2a  BY clause must.*
28869 2a 20 20 20 20 20 20 20 20 61 70 70 65 61 72 20  *        appear 
2886a 61 73 20 75 6e 6d 6f 64 69 66 69 65 64 20 72 65  as unmodified re
2886b 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  sult columns in 
2886c 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e  the outer query.
2886d 20 20 42 75 74 0a 2a 2a 20 20 20 20 20 20 20 20    But.**        
2886e 68 61 76 65 20 6f 74 68 65 72 20 6f 70 74 69 6d  have other optim
2886f 69 7a 61 74 69 6f 6e 73 20 69 6e 20 6d 69 6e 64  izations in mind
28870 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68   to deal with th
28871 61 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49  at case..**.** I
28872 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  n this routine, 
28873 74 68 65 20 22 70 22 20 70 61 72 61 6d 65 74 65  the "p" paramete
28874 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  r is a pointer t
28875 6f 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  o the outer quer
28876 79 2e 0a 2a 2a 20 54 68 65 20 73 75 62 71 75 65  y..** The subque
28877 72 79 20 69 73 20 70 2d 3e 70 53 72 63 2d 3e 61  ry is p->pSrc->a
28878 5b 69 46 72 6f 6d 5d 2e 20 20 69 73 41 67 67 20  [iFrom].  isAgg 
28879 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 6f  is true if the o
2887a 75 74 65 72 20 71 75 65 72 79 0a 2a 2a 20 75 73  uter query.** us
2887b 65 73 20 61 67 67 72 65 67 61 74 65 73 20 61 6e  es aggregates an
2887c 64 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20  d subqueryIsAgg 
2887d 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 73  is true if the s
2887e 75 62 71 75 65 72 79 20 75 73 65 73 20 61 67 67  ubquery uses agg
2887f 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  regates..**.** I
28880 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20  f flattening is 
28881 6e 6f 74 20 61 74 74 65 6d 70 74 65 64 2c 20 74  not attempted, t
28882 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
28883 20 6e 6f 2d 6f 70 20 61 6e 64 20 72 65 74 75 72   no-op and retur
28884 6e 73 20 30 2e 0a 2a 2a 20 49 66 20 66 6c 61 74  ns 0..** If flat
28885 74 65 6e 69 6e 67 20 69 73 20 61 74 74 65 6d 70  tening is attemp
28886 74 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ted this routine
28887 20 72 65 74 75 72 6e 73 20 31 2e 0a 2a 2a 0a 2a   returns 1..**.*
28888 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 65 78 70  * All of the exp
28889 72 65 73 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73  ression analysis
2888a 20 6d 75 73 74 20 6f 63 63 75 72 20 6f 6e 20 62   must occur on b
2888b 6f 74 68 20 74 68 65 20 6f 75 74 65 72 20 71 75  oth the outer qu
2888c 65 72 79 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73  ery and.** the s
2888d 75 62 71 75 65 72 79 20 62 65 66 6f 72 65 20 74  ubquery before t
2888e 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73  his routine runs
2888f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
28890 66 6c 61 74 74 65 6e 53 75 62 71 75 65 72 79 28  flattenSubquery(
28891 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
28892 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69  ,       /* Parsi
28893 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
28894 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
28895 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 65       /* The pare
28896 6e 74 20 6f 72 20 6f 75 74 65 72 20 53 45 4c 45  nt or outer SELE
28897 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  CT statement */.
28898 20 20 69 6e 74 20 69 46 72 6f 6d 2c 20 20 20 20    int iFrom,    
28899 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2889a 69 6e 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 5d 20  in p->pSrc->a[] 
2889b 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 73 75 62  of the inner sub
2889c 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69  query */.  int i
2889d 73 41 67 67 2c 20 20 20 20 20 20 20 20 20 20 20  sAgg,           
2889e 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 65 72  /* True if outer
2889f 20 53 45 4c 45 43 54 20 75 73 65 73 20 61 67 67   SELECT uses agg
288a0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
288a1 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 71 75 65   */.  int subque
288a2 72 79 49 73 41 67 67 20 20 20 20 2f 2a 20 54 72  ryIsAgg    /* Tr
288a3 75 65 20 69 66 20 74 68 65 20 73 75 62 71 75 65  ue if the subque
288a4 72 79 20 75 73 65 73 20 61 67 67 72 65 67 61 74  ry uses aggregat
288a5 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 29  e functions */.)
288a6 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
288a7 7a 53 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78  zSavedAuthContex
288a8 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74  t = pParse->zAut
288a9 68 43 6f 6e 74 65 78 74 3b 0a 20 20 53 65 6c 65  hContext;.  Sele
288aa 63 74 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 53  ct *pParent;.  S
288ab 65 6c 65 63 74 20 2a 70 53 75 62 3b 20 20 20 20  elect *pSub;    
288ac 20 20 20 2f 2a 20 54 68 65 20 69 6e 6e 65 72 20     /* The inner 
288ad 71 75 65 72 79 20 6f 72 20 22 73 75 62 71 75 65  query or "subque
288ae 72 79 22 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ry" */.  Select 
288af 2a 70 53 75 62 31 3b 20 20 20 20 20 20 2f 2a 20  *pSub1;      /* 
288b0 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  Pointer to the r
288b1 69 67 68 74 6d 6f 73 74 20 73 65 6c 65 63 74 20  ightmost select 
288b2 69 6e 20 73 75 62 2d 71 75 65 72 79 20 2a 2f 0a  in sub-query */.
288b3 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
288b4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f        /* The FRO
288b5 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  M clause of the 
288b6 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 20  outer query */. 
288b7 20 53 72 63 4c 69 73 74 20 2a 70 53 75 62 53 72   SrcList *pSubSr
288b8 63 3b 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d  c;   /* The FROM
288b9 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73   clause of the s
288ba 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 45 78 70  ubquery */.  Exp
288bb 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20  rList *pList;   
288bc 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73   /* The result s
288bd 65 74 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20  et of the outer 
288be 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69  query */.  int i
288bf 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20 2f  Parent;        /
288c0 2a 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  * VDBE cursor nu
288c1 6d 62 65 72 20 6f 66 20 74 68 65 20 70 53 75 62  mber of the pSub
288c2 20 72 65 73 75 6c 74 20 73 65 74 20 74 65 6d 70   result set temp
288c3 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
288c4 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
288c5 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
288c6 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
288c7 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
288c8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
288c9 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73  RE clause */.  s
288ca 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
288cb 65 6d 20 2a 70 53 75 62 69 74 65 6d 3b 20 20 20  em *pSubitem;   
288cc 2f 2a 20 54 68 65 20 73 75 62 71 75 65 72 79 20  /* The subquery 
288cd 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
288ce 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
288cf 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
288d0 65 20 69 66 20 66 6c 61 74 74 65 6e 69 6e 67 20  e if flattening 
288d1 69 73 20 70 65 72 6d 69 74 74 65 64 2e 20 20 52  is permitted.  R
288d2 65 74 75 72 6e 20 30 20 69 66 20 6e 6f 74 2e 0a  eturn 0 if not..
288d3 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
288d4 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
288d5 20 70 2d 3e 70 50 72 69 6f 72 3d 3d 30 20 29 3b   p->pPrior==0 );
288d6 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 66    /* Unable to f
288d7 6c 61 74 74 65 6e 20 63 6f 6d 70 6f 75 6e 64 20  latten compound 
288d8 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 70 53 72  queries */.  pSr
288d9 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61  c = p->pSrc;.  a
288da 73 73 65 72 74 28 20 70 53 72 63 20 26 26 20 69  ssert( pSrc && i
288db 46 72 6f 6d 3e 3d 30 20 26 26 20 69 46 72 6f 6d  From>=0 && iFrom
288dc 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20  <pSrc->nSrc );. 
288dd 20 70 53 75 62 69 74 65 6d 20 3d 20 26 70 53 72   pSubitem = &pSr
288de 63 2d 3e 61 5b 69 46 72 6f 6d 5d 3b 0a 20 20 69  c->a[iFrom];.  i
288df 50 61 72 65 6e 74 20 3d 20 70 53 75 62 69 74 65  Parent = pSubite
288e0 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 70 53  m->iCursor;.  pS
288e1 75 62 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70  ub = pSubitem->p
288e2 53 65 6c 65 63 74 3b 0a 20 20 61 73 73 65 72 74  Select;.  assert
288e3 28 20 70 53 75 62 21 3d 30 20 29 3b 0a 20 20 69  ( pSub!=0 );.  i
288e4 66 28 20 69 73 41 67 67 20 26 26 20 73 75 62 71  f( isAgg && subq
288e5 75 65 72 79 49 73 41 67 67 20 29 20 72 65 74 75  ueryIsAgg ) retu
288e6 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
288e7 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
288e8 74 69 6f 6e 20 28 31 29 20 20 2a 2f 0a 20 20 69  tion (1)  */.  i
288e9 66 28 20 73 75 62 71 75 65 72 79 49 73 41 67 67  f( subqueryIsAgg
288ea 20 26 26 20 70 53 72 63 2d 3e 6e 53 72 63 3e 31   && pSrc->nSrc>1
288eb 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
288ec 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
288ed 74 69 6f 6e 20 28 32 29 20 20 2a 2f 0a 20 20 70  tion (2)  */.  p
288ee 53 75 62 53 72 63 20 3d 20 70 53 75 62 2d 3e 70  SubSrc = pSub->p
288ef 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Src;.  assert( p
288f0 53 75 62 53 72 63 20 29 3b 0a 20 20 2f 2a 20 50  SubSrc );.  /* P
288f1 72 69 6f 72 20 74 6f 20 76 65 72 73 69 6f 6e 20  rior to version 
288f2 33 2e 31 2e 32 2c 20 77 68 65 6e 20 4c 49 4d 49  3.1.2, when LIMI
288f3 54 20 61 6e 64 20 4f 46 46 53 45 54 20 68 61 64  T and OFFSET had
288f4 20 74 6f 20 62 65 20 73 69 6d 70 6c 65 20 63 6f   to be simple co
288f5 6e 73 74 61 6e 74 73 2c 0a 20 20 2a 2a 20 6e 6f  nstants,.  ** no
288f6 74 20 61 72 62 69 74 72 61 72 79 20 65 78 70 72  t arbitrary expr
288f7 65 73 73 73 69 6f 6e 73 2c 20 77 65 20 61 6c 6c  esssions, we all
288f8 6f 77 65 64 20 73 6f 6d 65 20 63 6f 6d 62 69 6e  owed some combin
288f9 69 6e 67 20 6f 66 20 4c 49 4d 49 54 20 61 6e 64  ing of LIMIT and
288fa 20 4f 46 46 53 45 54 0a 20 20 2a 2a 20 62 65 63   OFFSET.  ** bec
288fb 61 75 73 65 20 74 68 65 79 20 63 6f 75 6c 64 20  ause they could 
288fc 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74 20 63  be computed at c
288fd 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20 20 42 75  ompile-time.  Bu
288fe 74 20 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64  t when LIMIT and
288ff 20 4f 46 46 53 45 54 0a 20 20 2a 2a 20 62 65 63   OFFSET.  ** bec
28900 61 6d 65 20 61 72 62 69 74 72 61 72 79 20 65 78  ame arbitrary ex
28901 70 72 65 73 73 69 6f 6e 73 2c 20 77 65 20 77 65  pressions, we we
28902 72 65 20 66 6f 72 63 65 64 20 74 6f 20 61 64 64  re forced to add
28903 20 72 65 73 74 72 69 63 74 69 6f 6e 73 20 28 31   restrictions (1
28904 33 29 0a 20 20 2a 2a 20 61 6e 64 20 28 31 34 29  3).  ** and (14)
28905 2e 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d  . */.  if( pSub-
28906 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70 4c  >pLimit && p->pL
28907 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20 30 3b  imit ) return 0;
28908 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28909 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 33   Restriction (13
2890a 29 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d  ) */.  if( pSub-
2890b 3e 70 4f 66 66 73 65 74 20 29 20 72 65 74 75 72  >pOffset ) retur
2890c 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
2890d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2890e 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 34   Restriction (14
2890f 29 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 52  ) */.  if( p->pR
28910 69 67 68 74 6d 6f 73 74 20 26 26 20 70 53 75 62  ightmost && pSub
28911 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70 53 75 62  ->pLimit && pSub
28912 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
28913 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20    return 0;     
28914 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28915 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28916 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
28917 63 74 69 6f 6e 20 28 31 35 29 20 2a 2f 0a 20 20  ction (15) */.  
28918 7d 0a 20 20 69 66 28 20 70 53 75 62 53 72 63 2d  }.  if( pSubSrc-
28919 3e 6e 53 72 63 3d 3d 30 20 29 20 72 65 74 75 72  >nSrc==0 ) retur
2891a 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
2891b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2891c 73 74 72 69 63 74 69 6f 6e 20 28 37 29 20 20 2a  striction (7)  *
2891d 2f 0a 20 20 69 66 28 20 28 28 70 53 75 62 2d 3e  /.  if( ((pSub->
2891e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
2891f 73 74 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53  stinct)!=0 || pS
28920 75 62 2d 3e 70 4c 69 6d 69 74 29 20 0a 20 20 20  ub->pLimit) .   
28921 20 20 20 20 20 20 26 26 20 28 70 53 72 63 2d 3e        && (pSrc->
28922 6e 53 72 63 3e 31 20 7c 7c 20 69 73 41 67 67 29  nSrc>1 || isAgg)
28923 20 29 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   ){          /* 
28924 52 65 73 74 72 69 63 74 69 6f 6e 73 20 28 34 29  Restrictions (4)
28925 28 35 29 28 38 29 28 39 29 20 2a 2f 0a 20 20 20  (5)(8)(9) */.   
28926 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20    return 0;     
28927 20 20 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d    .  }.  if( (p-
28928 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44  >selFlags & SF_D
28929 69 73 74 69 6e 63 74 29 21 3d 30 20 26 26 20 73  istinct)!=0 && s
2892a 75 62 71 75 65 72 79 49 73 41 67 67 20 29 7b 0a  ubqueryIsAgg ){.
2892b 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
2892c 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
2892d 63 74 69 6f 6e 20 28 36 29 20 20 2a 2f 0a 20 20  ction (6)  */.  
2892e 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65  }.  if( p->pOrde
2892f 72 42 79 20 26 26 20 70 53 75 62 2d 3e 70 4f 72  rBy && pSub->pOr
28930 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 72 65  derBy ){.     re
28931 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
28932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28933 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28934 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
28935 20 28 31 31 29 20 2a 2f 0a 20 20 7d 0a 20 20 69   (11) */.  }.  i
28936 66 28 20 69 73 41 67 67 20 26 26 20 70 53 75 62  f( isAgg && pSub
28937 2d 3e 70 4f 72 64 65 72 42 79 20 29 20 72 65 74  ->pOrderBy ) ret
28938 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
28939 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
2893a 74 69 6f 6e 20 28 31 36 29 20 2a 2f 0a 20 20 69  tion (16) */.  i
2893b 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20  f( pSub->pLimit 
2893c 26 26 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72  && p->pWhere ) r
2893d 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
2893e 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
2893f 74 69 6f 6e 20 28 31 39 29 20 2a 2f 0a 0a 20 20  tion (19) */..  
28940 2f 2a 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d  /* OBSOLETE COMM
28941 45 4e 54 20 31 3a 0a 20 20 2a 2a 20 52 65 73 74  ENT 1:.  ** Rest
28942 72 69 63 74 69 6f 6e 20 33 3a 20 20 49 66 20 74  riction 3:  If t
28943 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 61  he subquery is a
28944 20 6a 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65   join, make sure
28945 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
28946 20 0a 20 20 2a 2a 20 6e 6f 74 20 75 73 65 64 20   .  ** not used 
28947 61 73 20 74 68 65 20 72 69 67 68 74 20 6f 70 65  as the right ope
28948 72 61 6e 64 20 6f 66 20 61 6e 20 6f 75 74 65 72  rand of an outer
28949 20 6a 6f 69 6e 2e 20 20 45 78 61 6d 70 6c 65 73   join.  Examples
2894a 20 6f 66 20 77 68 79 20 74 68 69 73 0a 20 20 2a   of why this.  *
2894b 2a 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  * is not allowed
2894c 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
2894d 20 20 20 20 74 31 20 4c 45 46 54 20 4f 55 54 45      t1 LEFT OUTE
2894e 52 20 4a 4f 49 4e 20 28 74 32 20 4a 4f 49 4e 20  R JOIN (t2 JOIN 
2894f 74 33 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  t3).  **.  ** If
28950 20 77 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20   we flatten the 
28951 61 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20  above, we would 
28952 67 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  get.  **.  **   
28953 20 20 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f        (t1 LEFT O
28954 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20 4a 4f  UTER JOIN t2) JO
28955 49 4e 20 74 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20  IN t3.  **.  ** 
28956 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 74 20  which is not at 
28957 61 6c 6c 20 74 68 65 20 73 61 6d 65 20 74 68 69  all the same thi
28958 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 42  ng..  **.  ** OB
28959 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 20 32  SOLETE COMMENT 2
2895a 3a 0a 20 20 2a 2a 20 52 65 73 74 72 69 63 74 69  :.  ** Restricti
2895b 6f 6e 20 31 32 3a 20 20 49 66 20 74 68 65 20 73  on 12:  If the s
2895c 75 62 71 75 65 72 79 20 69 73 20 74 68 65 20 72  ubquery is the r
2895d 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20  ight operand of 
2895e 61 20 6c 65 66 74 20 6f 75 74 65 72 0a 20 20 2a  a left outer.  *
2895f 2a 20 6a 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72  * join, make sur
28960 65 20 74 68 65 20 73 75 62 71 75 65 72 79 20 68  e the subquery h
28961 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75  as no WHERE clau
28962 73 65 2e 0a 20 20 2a 2a 20 41 6e 20 65 78 61 6d  se..  ** An exam
28963 70 6c 65 73 20 6f 66 20 77 68 79 20 74 68 69 73  ples of why this
28964 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a   is not allowed:
28965 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
28966 20 20 20 74 31 20 4c 45 46 54 20 4f 55 54 45 52     t1 LEFT OUTER
28967 20 4a 4f 49 4e 20 28 53 45 4c 45 43 54 20 2a 20   JOIN (SELECT * 
28968 46 52 4f 4d 20 74 32 20 57 48 45 52 45 20 74 32  FROM t2 WHERE t2
28969 2e 78 3e 30 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20  .x>0).  **.  ** 
2896a 49 66 20 77 65 20 66 6c 61 74 74 65 6e 20 74 68  If we flatten th
2896b 65 20 61 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c  e above, we woul
2896c 64 20 67 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20  d get.  **.  ** 
2896d 20 20 20 20 20 20 20 20 28 74 31 20 4c 45 46 54          (t1 LEFT
2896e 20 4f 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20   OUTER JOIN t2) 
2896f 57 48 45 52 45 20 74 32 2e 78 3e 30 0a 20 20 2a  WHERE t2.x>0.  *
28970 2a 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20 74  *.  ** But the t
28971 32 2e 78 3e 30 20 74 65 73 74 20 77 69 6c 6c 20  2.x>0 test will 
28972 61 6c 77 61 79 73 20 66 61 69 6c 20 6f 6e 20 61  always fail on a
28973 20 4e 55 4c 4c 20 72 6f 77 20 6f 66 20 74 32 2c   NULL row of t2,
28974 20 77 68 69 63 68 0a 20 20 2a 2a 20 65 66 66 65   which.  ** effe
28975 63 74 69 76 65 6c 79 20 63 6f 6e 76 65 72 74 73  ctively converts
28976 20 74 68 65 20 4f 55 54 45 52 20 4a 4f 49 4e 20   the OUTER JOIN 
28977 69 6e 74 6f 20 61 6e 20 49 4e 4e 45 52 20 4a 4f  into an INNER JO
28978 49 4e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 48  IN..  **.  ** TH
28979 49 53 20 4f 56 45 52 52 49 44 45 53 20 4f 42 53  IS OVERRIDES OBS
2897a 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 53 20 31  OLETE COMMENTS 1
2897b 20 41 4e 44 20 32 20 41 42 4f 56 45 3a 0a 20 20   AND 2 ABOVE:.  
2897c 2a 2a 20 54 69 63 6b 65 74 20 23 33 33 30 30 20  ** Ticket #3300 
2897d 73 68 6f 77 73 20 74 68 61 74 20 66 6c 61 74 74  shows that flatt
2897e 65 6e 69 6e 67 20 74 68 65 20 72 69 67 68 74 20  ening the right 
2897f 74 65 72 6d 20 6f 66 20 61 20 4c 45 46 54 20 4a  term of a LEFT J
28980 4f 49 4e 0a 20 20 2a 2a 20 69 73 20 66 72 61 75  OIN.  ** is frau
28981 67 68 74 20 77 69 74 68 20 64 61 6e 67 65 72 2e  ght with danger.
28982 20 20 42 65 73 74 20 74 6f 20 61 76 6f 69 64 20    Best to avoid 
28983 74 68 65 20 77 68 6f 6c 65 20 74 68 69 6e 67 2e  the whole thing.
28984 20 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 73 75    If the.  ** su
28985 62 71 75 65 72 79 20 69 73 20 74 68 65 20 72 69  bquery is the ri
28986 67 68 74 20 74 65 72 6d 20 6f 66 20 61 20 4c 45  ght term of a LE
28987 46 54 20 4a 4f 49 4e 2c 20 74 68 65 6e 20 64 6f  FT JOIN, then do
28988 20 6e 6f 74 20 66 6c 61 74 74 65 6e 2e 0a 20 20   not flatten..  
28989 2a 2f 0a 20 20 69 66 28 20 28 70 53 75 62 69 74  */.  if( (pSubit
2898a 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a  em->jointype & J
2898b 54 5f 4f 55 54 45 52 29 21 3d 30 20 29 7b 0a 20  T_OUTER)!=0 ){. 
2898c 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2898d 0a 0a 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69  ..  /* Restricti
2898e 6f 6e 20 31 37 3a 20 49 66 20 74 68 65 20 73 75  on 17: If the su
2898f 62 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f 6d  b-query is a com
28990 70 6f 75 6e 64 20 53 45 4c 45 43 54 2c 20 74 68  pound SELECT, th
28991 65 6e 20 69 74 20 6d 75 73 74 0a 20 20 2a 2a 20  en it must.  ** 
28992 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 55 4e 49  use only the UNI
28993 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 2e  ON ALL operator.
28994 20 41 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65   And none of the
28995 20 73 69 6d 70 6c 65 20 73 65 6c 65 63 74 20 71   simple select q
28996 75 65 72 69 65 73 0a 20 20 2a 2a 20 74 68 61 74  ueries.  ** that
28997 20 6d 61 6b 65 20 75 70 20 74 68 65 20 63 6f 6d   make up the com
28998 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 72 65  pound SELECT are
28999 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 61   allowed to be a
2899a 67 67 72 65 67 61 74 65 20 6f 72 20 64 69 73 74  ggregate or dist
2899b 69 6e 63 74 0a 20 20 2a 2a 20 71 75 65 72 69 65  inct.  ** querie
2899c 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  s..  */.  if( pS
2899d 75 62 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20  ub->pPrior ){.  
2899e 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f 72 64    if( pSub->pOrd
2899f 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 72 65  erBy ){.      re
289a0 74 75 72 6e 20 30 3b 20 20 2f 2a 20 52 65 73 74  turn 0;  /* Rest
289a1 72 69 63 74 69 6f 6e 20 32 30 20 2a 2f 0a 20 20  riction 20 */.  
289a2 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 41 67    }.    if( isAg
289a3 67 20 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67  g || (p->selFlag
289a4 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29  s & SF_Distinct)
289a5 21 3d 30 20 7c 7c 20 70 53 72 63 2d 3e 6e 53 72  !=0 || pSrc->nSr
289a6 63 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65  c!=1 ){.      re
289a7 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
289a8 20 20 66 6f 72 28 70 53 75 62 31 3d 70 53 75 62    for(pSub1=pSub
289a9 3b 20 70 53 75 62 31 3b 20 70 53 75 62 31 3d 70  ; pSub1; pSub1=p
289aa 53 75 62 31 2d 3e 70 50 72 69 6f 72 29 7b 0a 20  Sub1->pPrior){. 
289ab 20 20 20 20 20 69 66 28 20 28 70 53 75 62 31 2d       if( (pSub1-
289ac 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f  >selFlags & (SF_
289ad 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72  Distinct|SF_Aggr
289ae 65 67 61 74 65 29 29 21 3d 30 0a 20 20 20 20 20  egate))!=0.     
289af 20 20 7c 7c 20 28 70 53 75 62 31 2d 3e 70 50 72    || (pSub1->pPr
289b0 69 6f 72 20 26 26 20 70 53 75 62 31 2d 3e 6f 70  ior && pSub1->op
289b1 21 3d 54 4b 5f 41 4c 4c 29 20 0a 20 20 20 20 20  !=TK_ALL) .     
289b2 20 20 7c 7c 20 21 70 53 75 62 31 2d 3e 70 53 72    || !pSub1->pSr
289b3 63 20 7c 7c 20 70 53 75 62 31 2d 3e 70 53 72 63  c || pSub1->pSrc
289b4 2d 3e 6e 53 72 63 21 3d 31 0a 20 20 20 20 20 20  ->nSrc!=1.      
289b5 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
289b6 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
289b7 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 74 72   }..    /* Restr
289b8 69 63 74 69 6f 6e 20 31 38 2e 20 2a 2f 0a 20 20  iction 18. */.  
289b9 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42    if( p->pOrderB
289ba 79 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  y ){.      int i
289bb 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d  i;.      for(ii=
289bc 30 3b 20 69 69 3c 70 2d 3e 70 4f 72 64 65 72 42  0; ii<p->pOrderB
289bd 79 2d 3e 6e 45 78 70 72 3b 20 69 69 2b 2b 29 7b  y->nExpr; ii++){
289be 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e  .        if( p->
289bf 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 69 5d 2e  pOrderBy->a[ii].
289c0 69 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  iCol==0 ) return
289c1 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
289c2 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 2a 2a 2a 2a 20  }.  }..  /***** 
289c3 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73  If we reach this
289c4 20 70 6f 69 6e 74 2c 20 66 6c 61 74 74 65 6e 69   point, flatteni
289c5 6e 67 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e  ng is permitted.
289c6 20 2a 2a 2a 2a 2a 2f 0a 0a 20 20 2f 2a 20 41 75   *****/..  /* Au
289c7 74 68 6f 72 69 7a 65 20 74 68 65 20 73 75 62 71  thorize the subq
289c8 75 65 72 79 20 2a 2f 0a 20 20 70 50 61 72 73 65  uery */.  pParse
289c9 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d  ->zAuthContext =
289ca 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65   pSubitem->zName
289cb 3b 0a 20 20 73 71 6c 69 74 65 33 41 75 74 68 43  ;.  sqlite3AuthC
289cc 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
289cd 49 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30  ITE_SELECT, 0, 0
289ce 2c 20 30 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  , 0);.  pParse->
289cf 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a  zAuthContext = z
289d0 53 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74  SavedAuthContext
289d1 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  ;..  /* If the s
289d2 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f  ub-query is a co
289d3 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74  mpound SELECT st
289d4 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 28 62  atement, then (b
289d5 79 20 72 65 73 74 72 69 63 74 69 6f 6e 73 0a 20  y restrictions. 
289d6 20 2a 2a 20 31 37 20 61 6e 64 20 31 38 20 61 62   ** 17 and 18 ab
289d7 6f 76 65 29 20 69 74 20 6d 75 73 74 20 62 65 20  ove) it must be 
289d8 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 61 6e 64 20  a UNION ALL and 
289d9 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72 79  the parent query
289da 20 6d 75 73 74 20 0a 20 20 2a 2a 20 62 65 20 6f   must .  ** be o
289db 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a  f the form:.  **
289dc 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  .  **     SELECT
289dd 20 3c 65 78 70 72 2d 6c 69 73 74 3e 20 46 52 4f   <expr-list> FRO
289de 4d 20 28 3c 73 75 62 2d 71 75 65 72 79 3e 29 20  M (<sub-query>) 
289df 3c 77 68 65 72 65 2d 63 6c 61 75 73 65 3e 20 0a  <where-clause> .
289e0 20 20 2a 2a 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77    **.  ** follow
289e1 65 64 20 62 79 20 61 6e 79 20 4f 52 44 45 52 20  ed by any ORDER 
289e2 42 59 2c 20 4c 49 4d 49 54 20 61 6e 64 2f 6f 72  BY, LIMIT and/or
289e3 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e   OFFSET clauses.
289e4 20 54 68 69 73 20 62 6c 6f 63 6b 0a 20 20 2a 2a   This block.  **
289e5 20 63 72 65 61 74 65 73 20 4e 2d 31 20 63 6f 70   creates N-1 cop
289e6 69 65 73 20 6f 66 20 74 68 65 20 70 61 72 65 6e  ies of the paren
289e7 74 20 71 75 65 72 79 20 77 69 74 68 6f 75 74 20  t query without 
289e8 61 6e 79 20 4f 52 44 45 52 20 42 59 2c 20 4c 49  any ORDER BY, LI
289e9 4d 49 54 20 6f 72 20 0a 20 20 2a 2a 20 4f 46 46  MIT or .  ** OFF
289ea 53 45 54 20 63 6c 61 75 73 65 73 20 61 6e 64 20  SET clauses and 
289eb 6a 6f 69 6e 73 20 74 68 65 6d 20 74 6f 20 74 68  joins them to th
289ec 65 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65  e left-hand-side
289ed 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
289ee 0a 20 20 2a 2a 20 75 73 69 6e 67 20 55 4e 49 4f  .  ** using UNIO
289ef 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 73 2e  N ALL operators.
289f0 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 4e 20   In this case N 
289f1 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
289f2 20 73 69 6d 70 6c 65 0a 20 20 2a 2a 20 73 65 6c   simple.  ** sel
289f3 65 63 74 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ect statements i
289f4 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  n the compound s
289f5 75 62 2d 71 75 65 72 79 2e 0a 20 20 2a 2a 0a 20  ub-query..  **. 
289f6 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 2a   ** Example:.  *
289f7 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43  *.  **     SELEC
289f8 54 20 61 2b 31 20 46 52 4f 4d 20 28 0a 20 20 2a  T a+1 FROM (.  *
289f9 2a 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20  *        SELECT 
289fa 78 20 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a 20  x FROM tab.  ** 
289fb 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c         UNION ALL
289fc 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45 4c  .  **        SEL
289fd 45 43 54 20 79 20 46 52 4f 4d 20 74 61 62 0a 20  ECT y FROM tab. 
289fe 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49 4f 4e   **        UNION
289ff 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 20 20   ALL.  **       
28a00 20 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29   SELECT abs(z*2)
28a01 20 46 52 4f 4d 20 74 61 62 32 0a 20 20 2a 2a 20   FROM tab2.  ** 
28a02 20 20 20 20 29 20 57 48 45 52 45 20 61 21 3d 35      ) WHERE a!=5
28a03 20 4f 52 44 45 52 20 42 59 20 31 0a 20 20 2a 2a   ORDER BY 1.  **
28a04 0a 20 20 2a 2a 20 54 72 61 6e 73 66 6f 72 6d 65  .  ** Transforme
28a05 64 20 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a  d into:.  **.  *
28a06 2a 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b 31  *     SELECT x+1
28a07 20 46 52 4f 4d 20 74 61 62 20 57 48 45 52 45 20   FROM tab WHERE 
28a08 78 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20  x+1!=5.  **     
28a09 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20  UNION ALL.  **  
28a0a 20 20 20 53 45 4c 45 43 54 20 79 2b 31 20 46 52     SELECT y+1 FR
28a0b 4f 4d 20 74 61 62 20 57 48 45 52 45 20 79 2b 31  OM tab WHERE y+1
28a0c 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e 49  !=5.  **     UNI
28a0d 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20  ON ALL.  **     
28a0e 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29 2b  SELECT abs(z*2)+
28a0f 31 20 46 52 4f 4d 20 74 61 62 32 20 57 48 45 52  1 FROM tab2 WHER
28a10 45 20 61 62 73 28 7a 2a 32 29 2b 31 21 3d 35 0a  E abs(z*2)+1!=5.
28a11 20 20 2a 2a 20 20 20 20 20 4f 52 44 45 52 20 42    **     ORDER B
28a12 59 20 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65  Y 1.  **.  ** We
28a13 20 63 61 6c 6c 20 74 68 69 73 20 74 68 65 20 22   call this the "
28a14 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72  compound-subquer
28a15 79 20 66 6c 61 74 74 65 6e 69 6e 67 22 2e 0a 20  y flattening".. 
28a16 20 2a 2f 0a 20 20 66 6f 72 28 70 53 75 62 3d 70   */.  for(pSub=p
28a17 53 75 62 2d 3e 70 50 72 69 6f 72 3b 20 70 53 75  Sub->pPrior; pSu
28a18 62 3b 20 70 53 75 62 3d 70 53 75 62 2d 3e 70 50  b; pSub=pSub->pP
28a19 72 69 6f 72 29 7b 0a 20 20 20 20 53 65 6c 65 63  rior){.    Selec
28a1a 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 45 78 70  t *pNew;.    Exp
28a1b 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20  rList *pOrderBy 
28a1c 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20  = p->pOrderBy;. 
28a1d 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 20     Expr *pLimit 
28a1e 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20  = p->pLimit;.   
28a1f 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 20   Select *pPrior 
28a20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  = p->pPrior;.   
28a21 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30   p->pOrderBy = 0
28a22 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20  ;.    p->pSrc = 
28a23 30 3b 0a 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  0;.    p->pPrior
28a24 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 69   = 0;.    p->pLi
28a25 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 70 4e 65  mit = 0;.    pNe
28a26 77 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  w = sqlite3Selec
28a27 74 44 75 70 28 64 62 2c 20 70 2c 20 30 29 3b 0a  tDup(db, p, 0);.
28a28 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
28a29 70 4c 69 6d 69 74 3b 0a 20 20 20 20 70 2d 3e 70  pLimit;.    p->p
28a2a 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72  OrderBy = pOrder
28a2b 42 79 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20  By;.    p->pSrc 
28a2c 3d 20 70 53 72 63 3b 0a 20 20 20 20 70 2d 3e 6f  = pSrc;.    p->o
28a2d 70 20 3d 20 54 4b 5f 41 4c 4c 3b 0a 20 20 20 20  p = TK_ALL;.    
28a2e 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  p->pRightmost = 
28a2f 30 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d  0;.    if( pNew=
28a30 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77  =0 ){.      pNew
28a31 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 7d   = pPrior;.    }
28a32 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77  else{.      pNew
28a33 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f  ->pPrior = pPrio
28a34 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  r;.      pNew->p
28a35 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20  Rightmost = 0;. 
28a36 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50 72 69     }.    p->pPri
28a37 6f 72 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 69  or = pNew;.    i
28a38 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
28a39 6c 65 64 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  led ) return 1;.
28a3a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20    }..  /* Begin 
28a3b 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20 69  flattening the i
28a3c 46 72 6f 6d 2d 74 68 20 65 6e 74 72 79 20 6f 66  From-th entry of
28a3d 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
28a3e 20 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 75   .  ** in the ou
28a3f 74 65 72 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a  ter query..  */.
28a40 20 20 70 53 75 62 20 3d 20 70 53 75 62 31 20 3d    pSub = pSub1 =
28a41 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65   pSubitem->pSele
28a42 63 74 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65  ct;..  /* Delete
28a43 20 74 68 65 20 74 72 61 6e 73 69 65 6e 74 20 74   the transient t
28a44 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 61  able structure a
28a45 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
28a46 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72 79  he.  ** subquery
28a47 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44  .  */.  sqlite3D
28a48 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 74  bFree(db, pSubit
28a49 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  em->zDatabase);.
28a4a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
28a4b 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e  db, pSubitem->zN
28a4c 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ame);.  sqlite3D
28a4d 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 74  bFree(db, pSubit
28a4e 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 70  em->zAlias);.  p
28a4f 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62 61  Subitem->zDataba
28a50 73 65 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74  se = 0;.  pSubit
28a51 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  em->zName = 0;. 
28a52 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61   pSubitem->zAlia
28a53 73 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74 65  s = 0;.  pSubite
28a54 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 30 3b 0a  m->pSelect = 0;.
28a55 0a 20 20 2f 2a 20 44 65 66 65 72 20 64 65 6c 65  .  /* Defer dele
28a56 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65 20 6f  ting the Table o
28a57 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64  bject associated
28a58 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73   with the.  ** s
28a59 75 62 71 75 65 72 79 20 75 6e 74 69 6c 20 63 6f  ubquery until co
28a5a 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 69 73  de generation is
28a5b 0a 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 2c 20  .  ** complete, 
28a5c 73 69 6e 63 65 20 74 68 65 72 65 20 6d 61 79 20  since there may 
28a5d 73 74 69 6c 6c 20 65 78 69 73 74 20 45 78 70 72  still exist Expr
28a5e 2e 70 54 61 62 20 65 6e 74 72 69 65 73 20 74 68  .pTab entries th
28a5f 61 74 0a 20 20 2a 2a 20 72 65 66 65 72 20 74 6f  at.  ** refer to
28a60 20 74 68 65 20 73 75 62 71 75 65 72 79 20 65 76   the subquery ev
28a61 65 6e 20 61 66 74 65 72 20 66 6c 61 74 74 65 6e  en after flatten
28a62 69 6e 67 2e 20 20 54 69 63 6b 65 74 20 23 33 33  ing.  Ticket #33
28a63 34 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  46..  */.  if( p
28a64 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 21 3d 30  Subitem->pTab!=0
28a65 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
28a66 54 61 62 54 6f 44 65 6c 20 3d 20 70 53 75 62 69  TabToDel = pSubi
28a67 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69  tem->pTab;.    i
28a68 66 28 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52  f( pTabToDel->nR
28a69 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70  ef==1 ){.      p
28a6a 54 61 62 54 6f 44 65 6c 2d 3e 70 4e 65 78 74 5a  TabToDel->pNextZ
28a6b 6f 6d 62 69 65 20 3d 20 70 50 61 72 73 65 2d 3e  ombie = pParse->
28a6c 70 5a 6f 6d 62 69 65 54 61 62 3b 0a 20 20 20 20  pZombieTab;.    
28a6d 20 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69    pParse->pZombi
28a6e 65 54 61 62 20 3d 20 70 54 61 62 54 6f 44 65 6c  eTab = pTabToDel
28a6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
28a70 20 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52     pTabToDel->nR
28a71 65 66 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ef--;.    }.    
28a72 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 20 3d  pSubitem->pTab =
28a73 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68   0;.  }..  /* Th
28a74 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f 70  e following loop
28a75 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65   runs once for e
28a76 61 63 68 20 74 65 72 6d 20 69 6e 20 61 20 63 6f  ach term in a co
28a77 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 0a  mpound-subquery.
28a78 20 20 2a 2a 20 66 6c 61 74 74 65 6e 69 6e 67 20    ** flattening 
28a79 28 61 73 20 64 65 73 63 72 69 62 65 64 20 61 62  (as described ab
28a7a 6f 76 65 29 2e 20 20 49 66 20 77 65 20 61 72 65  ove).  If we are
28a7b 20 64 6f 69 6e 67 20 61 20 64 69 66 66 65 72 65   doing a differe
28a7c 6e 74 20 6b 69 6e 64 0a 20 20 2a 2a 20 6f 66 20  nt kind.  ** of 
28a7d 66 6c 61 74 74 65 6e 69 6e 67 20 2d 20 61 20 66  flattening - a f
28a7e 6c 61 74 74 65 6e 69 6e 67 20 6f 74 68 65 72 20  lattening other 
28a7f 74 68 61 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 2d  than a compound-
28a80 73 75 62 71 75 65 72 79 20 66 6c 61 74 74 65 6e  subquery flatten
28a81 69 6e 67 20 2d 0a 20 20 2a 2a 20 74 68 65 6e 20  ing -.  ** then 
28a82 74 68 69 73 20 6c 6f 6f 70 20 6f 6e 6c 79 20 72  this loop only r
28a83 75 6e 73 20 6f 6e 63 65 2e 0a 20 20 2a 2a 0a 20  uns once..  **. 
28a84 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 6d 6f   ** This loop mo
28a85 76 65 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 46  ves all of the F
28a86 52 4f 4d 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  ROM elements of 
28a87 74 68 65 20 73 75 62 71 75 65 72 79 20 69 6e 74  the subquery int
28a88 6f 20 74 68 65 0a 20 20 2a 2a 20 74 68 65 20 46  o the.  ** the F
28a89 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ROM clause of th
28a8a 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 20 20  e outer query.  
28a8b 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 74 68 69  Before doing thi
28a8c 73 2c 20 72 65 6d 65 6d 62 65 72 0a 20 20 2a 2a  s, remember.  **
28a8d 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62   the cursor numb
28a8e 65 72 20 66 6f 72 20 74 68 65 20 6f 72 69 67 69  er for the origi
28a8f 6e 61 6c 20 6f 75 74 65 72 20 71 75 65 72 79 20  nal outer query 
28a90 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 20 69 6e 0a  FROM element in.
28a91 20 20 2a 2a 20 69 50 61 72 65 6e 74 2e 20 20 54    ** iParent.  T
28a92 68 65 20 69 50 61 72 65 6e 74 20 63 75 72 73 6f  he iParent curso
28a93 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  r will never be 
28a94 75 73 65 64 2e 20 20 53 75 62 73 65 71 75 65 6e  used.  Subsequen
28a95 74 20 63 6f 64 65 0a 20 20 2a 2a 20 77 69 6c 6c  t code.  ** will
28a96 20 73 63 61 6e 20 65 78 70 72 65 73 73 69 6f 6e   scan expression
28a97 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 69 50  s looking for iP
28a98 61 72 65 6e 74 20 72 65 66 65 72 65 6e 63 65 73  arent references
28a99 20 61 6e 64 20 72 65 70 6c 61 63 65 0a 20 20 2a   and replace.  *
28a9a 2a 20 74 68 6f 73 65 20 72 65 66 65 72 65 6e 63  * those referenc
28a9b 65 73 20 77 69 74 68 20 65 78 70 72 65 73 73 69  es with expressi
28a9c 6f 6e 73 20 74 68 61 74 20 72 65 73 6f 6c 76 65  ons that resolve
28a9d 20 74 6f 20 74 68 65 20 73 75 62 71 75 65 72 79   to the subquery
28a9e 20 46 52 4f 4d 0a 20 20 2a 2a 20 65 6c 65 6d 65   FROM.  ** eleme
28a9f 6e 74 73 20 77 65 20 61 72 65 20 6e 6f 77 20 63  nts we are now c
28aa0 6f 70 79 69 6e 67 20 69 6e 2e 0a 20 20 2a 2f 0a  opying in..  */.
28aa1 20 20 66 6f 72 28 70 50 61 72 65 6e 74 3d 70 3b    for(pParent=p;
28aa2 20 70 50 61 72 65 6e 74 3b 20 70 50 61 72 65 6e   pParent; pParen
28aa3 74 3d 70 50 61 72 65 6e 74 2d 3e 70 50 72 69 6f  t=pParent->pPrio
28aa4 72 2c 20 70 53 75 62 3d 70 53 75 62 2d 3e 70 50  r, pSub=pSub->pP
28aa5 72 69 6f 72 29 7b 0a 20 20 20 20 69 6e 74 20 6e  rior){.    int n
28aa6 53 75 62 53 72 63 3b 0a 20 20 20 20 75 38 20 6a  SubSrc;.    u8 j
28aa7 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a 20 20 20  ointype = 0;.   
28aa8 20 70 53 75 62 53 72 63 20 3d 20 70 53 75 62 2d   pSubSrc = pSub-
28aa9 3e 70 53 72 63 3b 20 20 20 20 20 2f 2a 20 46 52  >pSrc;     /* FR
28aaa 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 73 75 62  OM clause of sub
28aab 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 6e 53 75  query */.    nSu
28aac 62 53 72 63 20 3d 20 70 53 75 62 53 72 63 2d 3e  bSrc = pSubSrc->
28aad 6e 53 72 63 3b 20 20 2f 2a 20 4e 75 6d 62 65 72  nSrc;  /* Number
28aae 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 73 75 62   of terms in sub
28aaf 71 75 65 72 79 20 46 52 4f 4d 20 63 6c 61 75 73  query FROM claus
28ab0 65 20 2a 2f 0a 20 20 20 20 70 53 72 63 20 3d 20  e */.    pSrc = 
28ab1 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 3b 20 20  pParent->pSrc;  
28ab2 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73     /* FROM claus
28ab3 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71  e of the outer q
28ab4 75 65 72 79 20 2a 2f 0a 0a 20 20 20 20 69 66 28  uery */..    if(
28ab5 20 70 53 72 63 20 29 7b 0a 20 20 20 20 20 20 61   pSrc ){.      a
28ab6 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 3d 3d  ssert( pParent==
28ab7 70 20 29 3b 20 20 2f 2a 20 46 69 72 73 74 20 74  p );  /* First t
28ab8 69 6d 65 20 74 68 72 6f 75 67 68 20 74 68 65 20  ime through the 
28ab9 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 6a 6f  loop */.      jo
28aba 69 6e 74 79 70 65 20 3d 20 70 53 75 62 69 74 65  intype = pSubite
28abb 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20  m->jointype;.   
28abc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
28abd 73 65 72 74 28 20 70 50 61 72 65 6e 74 21 3d 70  sert( pParent!=p
28abe 20 29 3b 20 20 2f 2a 20 32 6e 64 20 61 6e 64 20   );  /* 2nd and 
28abf 73 75 62 73 65 71 75 65 6e 74 20 74 69 6d 65 73  subsequent times
28ac0 20 74 68 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f   through the loo
28ac1 70 20 2a 2f 0a 20 20 20 20 20 20 70 53 72 63 20  p */.      pSrc 
28ac2 3d 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 20  = pParent->pSrc 
28ac3 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
28ac4 41 70 70 65 6e 64 28 64 62 2c 20 30 2c 20 30 2c  Append(db, 0, 0,
28ac5 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   0);.      if( p
28ac6 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Src==0 ){.      
28ac7 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61    assert( db->ma
28ac8 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
28ac9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
28aca 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
28acb 2f 2a 20 54 68 65 20 73 75 62 71 75 65 72 79 20  /* The subquery 
28acc 75 73 65 73 20 61 20 73 69 6e 67 6c 65 20 73 6c  uses a single sl
28acd 6f 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  ot of the FROM c
28ace 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74  lause of the out
28acf 65 72 0a 20 20 20 20 2a 2a 20 71 75 65 72 79 2e  er.    ** query.
28ad0 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72    If the subquer
28ad1 79 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20  y has more than 
28ad2 6f 6e 65 20 65 6c 65 6d 65 6e 74 20 69 6e 20 69  one element in i
28ad3 74 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c 0a  ts FROM clause,.
28ad4 20 20 20 20 2a 2a 20 74 68 65 6e 20 65 78 70 61      ** then expa
28ad5 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  nd the outer que
28ad6 72 79 20 74 6f 20 6d 61 6b 65 20 73 70 61 63 65  ry to make space
28ad7 20 66 6f 72 20 69 74 20 74 6f 20 68 6f 6c 64 20   for it to hold 
28ad8 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 0a 20 20 20  all elements.   
28ad9 20 2a 2a 20 6f 66 20 74 68 65 20 73 75 62 71 75   ** of the subqu
28ada 65 72 79 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ery..    **.    
28adb 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 20 20  ** Example:.    
28adc 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 53 45 4c  **.    **    SEL
28add 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 62 41 2c  ECT * FROM tabA,
28ade 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20   (SELECT * FROM 
28adf 73 75 62 31 2c 20 73 75 62 32 29 2c 20 74 61 62  sub1, sub2), tab
28ae0 42 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  B;.    **.    **
28ae1 20 54 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   The outer query
28ae2 20 68 61 73 20 33 20 73 6c 6f 74 73 20 69 6e 20   has 3 slots in 
28ae3 69 74 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  its FROM clause.
28ae4 20 20 4f 6e 65 20 73 6c 6f 74 20 6f 66 20 74 68    One slot of th
28ae5 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71  e.    ** outer q
28ae6 75 65 72 79 20 28 74 68 65 20 6d 69 64 64 6c 65  uery (the middle
28ae7 20 73 6c 6f 74 29 20 69 73 20 75 73 65 64 20 62   slot) is used b
28ae8 79 20 74 68 65 20 73 75 62 71 75 65 72 79 2e 20  y the subquery. 
28ae9 20 54 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a   The next.    **
28aea 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 77   block of code w
28aeb 69 6c 6c 20 65 78 70 61 6e 64 20 74 68 65 20 6f  ill expand the o
28aec 75 74 20 71 75 65 72 79 20 74 6f 20 34 20 73 6c  ut query to 4 sl
28aed 6f 74 73 2e 20 20 54 68 65 20 6d 69 64 64 6c 65  ots.  The middle
28aee 0a 20 20 20 20 2a 2a 20 73 6c 6f 74 20 69 73 20  .    ** slot is 
28aef 65 78 70 61 6e 64 65 64 20 74 6f 20 74 77 6f 20  expanded to two 
28af0 73 6c 6f 74 73 20 69 6e 20 6f 72 64 65 72 20 74  slots in order t
28af1 6f 20 6d 61 6b 65 20 73 70 61 63 65 20 66 6f 72  o make space for
28af2 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 77 6f 20   the.    ** two 
28af3 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
28af4 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
28af5 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20 20  he subquery..   
28af6 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 53 75 62   */.    if( nSub
28af7 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20 70  Src>1 ){.      p
28af8 50 61 72 65 6e 74 2d 3e 70 53 72 63 20 3d 20 70  Parent->pSrc = p
28af9 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Src = sqlite3Src
28afa 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c 20  ListEnlarge(db, 
28afb 70 53 72 63 2c 20 6e 53 75 62 53 72 63 2d 31 2c  pSrc, nSubSrc-1,
28afc 69 46 72 6f 6d 2b 31 29 3b 0a 20 20 20 20 20 20  iFrom+1);.      
28afd 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
28afe 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  iled ){.        
28aff 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
28b00 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 72 61     }..    /* Tra
28b01 6e 73 66 65 72 20 74 68 65 20 46 52 4f 4d 20 63  nsfer the FROM c
28b02 6c 61 75 73 65 20 74 65 72 6d 73 20 66 72 6f 6d  lause terms from
28b03 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 6e   the subquery in
28b04 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 75  to the.    ** ou
28b05 74 65 72 20 71 75 65 72 79 2e 0a 20 20 20 20 2a  ter query..    *
28b06 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
28b07 3c 6e 53 75 62 53 72 63 3b 20 69 2b 2b 29 7b 0a  <nSubSrc; i++){.
28b08 20 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b        pSrc->a[i+
28b09 69 46 72 6f 6d 5d 20 3d 20 70 53 75 62 53 72 63  iFrom] = pSubSrc
28b0a 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 6d 65  ->a[i];.      me
28b0b 6d 73 65 74 28 26 70 53 75 62 53 72 63 2d 3e 61  mset(&pSubSrc->a
28b0c 5b 69 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70  [i], 0, sizeof(p
28b0d 53 75 62 53 72 63 2d 3e 61 5b 69 5d 29 29 3b 0a  SubSrc->a[i]));.
28b0e 20 20 20 20 7d 0a 20 20 20 20 70 53 72 63 2d 3e      }.    pSrc->
28b0f 61 5b 69 46 72 6f 6d 5d 2e 6a 6f 69 6e 74 79 70  a[iFrom].jointyp
28b10 65 20 3d 20 6a 6f 69 6e 74 79 70 65 3b 0a 20 20  e = jointype;.  
28b11 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 62 65 67 69  .    /* Now begi
28b12 6e 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 73  n substituting s
28b13 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 20 73  ubquery result s
28b14 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 66  et expressions f
28b15 6f 72 20 0a 20 20 20 20 2a 2a 20 72 65 66 65 72  or .    ** refer
28b16 65 6e 63 65 73 20 74 6f 20 74 68 65 20 69 50 61  ences to the iPa
28b17 72 65 6e 74 20 69 6e 20 74 68 65 20 6f 75 74 65  rent in the oute
28b18 72 20 71 75 65 72 79 2e 0a 20 20 20 20 2a 2a 20  r query..    ** 
28b19 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a  .    ** Example:
28b1a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
28b1b 20 53 45 4c 45 43 54 20 61 2b 35 2c 20 62 2a 31   SELECT a+5, b*1
28b1c 30 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78  0 FROM (SELECT x
28b1d 2a 33 20 41 53 20 61 2c 20 79 2b 31 30 20 41 53  *3 AS a, y+10 AS
28b1e 20 62 20 46 52 4f 4d 20 74 31 29 20 57 48 45 52   b FROM t1) WHER
28b1f 45 20 61 3e 62 3b 0a 20 20 20 20 2a 2a 20 20 20  E a>b;.    **   
28b20 5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \               
28b21 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f        \_________
28b22 5f 5f 5f 5f 20 73 75 62 71 75 65 72 79 20 5f 5f  ____ subquery __
28b23 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20  ________/       
28b24 20 20 20 2f 0a 20 20 20 20 2a 2a 20 20 20 20 5c     /.    **    \
28b25 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
28b26 5f 5f 5f 5f 5f 20 6f 75 74 65 72 20 71 75 65 72  _____ outer quer
28b27 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  y ______________
28b28 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
28b29 2f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  /.    **.    ** 
28b2a 57 65 20 6c 6f 6f 6b 20 61 74 20 65 76 65 72 79  We look at every
28b2b 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
28b2c 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61  he outer query a
28b2d 6e 64 20 65 76 65 72 79 20 70 6c 61 63 65 20 77  nd every place w
28b2e 65 20 73 65 65 0a 20 20 20 20 2a 2a 20 22 61 22  e see.    ** "a"
28b2f 20 77 65 20 73 75 62 73 74 69 74 75 74 65 20 22   we substitute "
28b30 78 2a 33 22 20 61 6e 64 20 65 76 65 72 79 20 70  x*3" and every p
28b31 6c 61 63 65 20 77 65 20 73 65 65 20 22 62 22 20  lace we see "b" 
28b32 77 65 20 73 75 62 73 74 69 74 75 74 65 20 22 79  we substitute "y
28b33 2b 31 30 22 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  +10"..    */.   
28b34 20 70 4c 69 73 74 20 3d 20 70 50 61 72 65 6e 74   pList = pParent
28b35 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 66 6f  ->pEList;.    fo
28b36 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  r(i=0; i<pList->
28b37 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
28b38 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a     Expr *pExpr;.
28b39 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d        if( pList-
28b3a 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 26  >a[i].zName==0 &
28b3b 26 20 28 70 45 78 70 72 20 3d 20 70 4c 69 73 74  & (pExpr = pList
28b3c 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 2d 3e 73  ->a[i].pExpr)->s
28b3d 70 61 6e 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20  pan.z!=0 ){.    
28b3e 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e      pList->a[i].
28b3f 7a 4e 61 6d 65 20 3d 20 0a 20 20 20 20 20 20 20  zName = .       
28b40 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
28b41 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68  bStrNDup(db, (ch
28b42 61 72 2a 29 70 45 78 70 72 2d 3e 73 70 61 6e 2e  ar*)pExpr->span.
28b43 7a 2c 20 70 45 78 70 72 2d 3e 73 70 61 6e 2e 6e  z, pExpr->span.n
28b44 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
28b45 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 4c 69  .    substExprLi
28b46 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e  st(db, pParent->
28b47 70 45 4c 69 73 74 2c 20 69 50 61 72 65 6e 74 2c  pEList, iParent,
28b48 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
28b49 20 20 20 20 69 66 28 20 69 73 41 67 67 20 29 7b      if( isAgg ){
28b4a 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70 72  .      substExpr
28b4b 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74  List(db, pParent
28b4c 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69 50 61 72  ->pGroupBy, iPar
28b4d 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73  ent, pSub->pELis
28b4e 74 29 3b 0a 20 20 20 20 20 20 73 75 62 73 74 45  t);.      substE
28b4f 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  xpr(db, pParent-
28b50 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72 65 6e  >pHaving, iParen
28b51 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29  t, pSub->pEList)
28b52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
28b53 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29  pSub->pOrderBy )
28b54 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
28b55 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42  pParent->pOrderB
28b56 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50  y==0 );.      pP
28b57 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20  arent->pOrderBy 
28b58 3d 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79  = pSub->pOrderBy
28b59 3b 0a 20 20 20 20 20 20 70 53 75 62 2d 3e 70 4f  ;.      pSub->pO
28b5a 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20  rderBy = 0;.    
28b5b 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 65 6e  }else if( pParen
28b5c 74 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  t->pOrderBy ){. 
28b5d 20 20 20 20 20 73 75 62 73 74 45 78 70 72 4c 69       substExprLi
28b5e 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e  st(db, pParent->
28b5f 70 4f 72 64 65 72 42 79 2c 20 69 50 61 72 65 6e  pOrderBy, iParen
28b60 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29  t, pSub->pEList)
28b61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
28b62 70 53 75 62 2d 3e 70 57 68 65 72 65 20 29 7b 0a  pSub->pWhere ){.
28b63 20 20 20 20 20 20 70 57 68 65 72 65 20 3d 20 73        pWhere = s
28b64 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
28b65 2c 20 70 53 75 62 2d 3e 70 57 68 65 72 65 2c 20  , pSub->pWhere, 
28b66 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  0);.    }else{. 
28b67 20 20 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b       pWhere = 0;
28b68 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
28b69 75 62 71 75 65 72 79 49 73 41 67 67 20 29 7b 0a  ubqueryIsAgg ){.
28b6a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
28b6b 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 3d 3d  arent->pHaving==
28b6c 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65  0 );.      pPare
28b6d 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70 50  nt->pHaving = pP
28b6e 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 3b 0a 20  arent->pWhere;. 
28b6f 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 57       pParent->pW
28b70 68 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20  here = pWhere;. 
28b71 20 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64       substExpr(d
28b72 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76  b, pParent->pHav
28b73 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70 53  ing, iParent, pS
28b74 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20  ub->pEList);.   
28b75 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76     pParent->pHav
28b76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ing = sqlite3Exp
28b77 72 41 6e 64 28 64 62 2c 20 70 50 61 72 65 6e 74  rAnd(db, pParent
28b78 2d 3e 70 48 61 76 69 6e 67 2c 20 0a 20 20 20 20  ->pHaving, .    
28b79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28b7b 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
28b7c 20 70 53 75 62 2d 3e 70 48 61 76 69 6e 67 2c 20   pSub->pHaving, 
28b7d 30 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0));.      asser
28b7e 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f  t( pParent->pGro
28b7f 75 70 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20  upBy==0 );.     
28b80 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70   pParent->pGroup
28b81 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  By = sqlite3Expr
28b82 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53 75 62  ListDup(db, pSub
28b83 2d 3e 70 47 72 6f 75 70 42 79 2c 20 30 29 3b 0a  ->pGroupBy, 0);.
28b84 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
28b85 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70   substExpr(db, p
28b86 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 2c 20  Parent->pWhere, 
28b87 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70  iParent, pSub->p
28b88 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70 50  EList);.      pP
28b89 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d 20  arent->pWhere = 
28b8a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64  sqlite3ExprAnd(d
28b8b 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65  b, pParent->pWhe
28b8c 72 65 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 20  re, pWhere);.   
28b8d 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68 65   }.  .    /* The
28b8e 20 66 6c 61 74 74 65 6e 65 64 20 71 75 65 72 79   flattened query
28b8f 20 69 73 20 64 69 73 74 69 6e 63 74 20 69 66 20   is distinct if 
28b90 65 69 74 68 65 72 20 74 68 65 20 69 6e 6e 65 72  either the inner
28b91 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f   or the.    ** o
28b92 75 74 65 72 20 71 75 65 72 79 20 69 73 20 64 69  uter query is di
28b93 73 74 69 6e 63 74 2e 20 0a 20 20 20 20 2a 2f 0a  stinct. .    */.
28b94 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 73 65 6c      pParent->sel
28b95 46 6c 61 67 73 20 7c 3d 20 70 53 75 62 2d 3e 73  Flags |= pSub->s
28b96 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73  elFlags & SF_Dis
28b97 74 69 6e 63 74 3b 0a 20 20 0a 20 20 20 20 2f 2a  tinct;.  .    /*
28b98 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 2e  .    ** SELECT .
28b99 2e 2e 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20  .. FROM (SELECT 
28b9a 2e 2e 2e 20 4c 49 4d 49 54 20 61 20 4f 46 46 53  ... LIMIT a OFFS
28b9b 45 54 20 62 29 20 4c 49 4d 49 54 20 78 20 4f 46  ET b) LIMIT x OF
28b9c 46 53 45 54 20 79 3b 0a 20 20 20 20 2a 2a 0a 20  FSET y;.    **. 
28b9d 20 20 20 2a 2a 20 4f 6e 65 20 69 73 20 74 65 6d     ** One is tem
28b9e 70 74 65 64 20 74 6f 20 74 72 79 20 74 6f 20 61  pted to try to a
28b9f 64 64 20 61 20 61 6e 64 20 62 20 74 6f 20 63 6f  dd a and b to co
28ba0 6d 62 69 6e 65 20 74 68 65 20 6c 69 6d 69 74 73  mbine the limits
28ba1 2e 20 20 42 75 74 20 74 68 69 73 0a 20 20 20 20  .  But this.    
28ba2 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b  ** does not work
28ba3 20 69 66 20 65 69 74 68 65 72 20 6c 69 6d 69 74   if either limit
28ba4 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 20 20   is negative..  
28ba5 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 75    */.    if( pSu
28ba6 62 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20  b->pLimit ){.   
28ba7 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 4c 69 6d     pParent->pLim
28ba8 69 74 20 3d 20 70 53 75 62 2d 3e 70 4c 69 6d 69  it = pSub->pLimi
28ba9 74 3b 0a 20 20 20 20 20 20 70 53 75 62 2d 3e 70  t;.      pSub->p
28baa 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 7d  Limit = 0;.    }
28bab 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 61  .  }..  /* Finia
28bac 6c 6c 79 2c 20 64 65 6c 65 74 65 20 77 68 61 74  lly, delete what
28bad 20 69 73 20 6c 65 66 74 20 6f 66 20 74 68 65 20   is left of the 
28bae 73 75 62 71 75 65 72 79 20 61 6e 64 20 72 65 74  subquery and ret
28baf 75 72 6e 0a 20 20 2a 2a 20 73 75 63 63 65 73 73  urn.  ** success
28bb0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
28bb1 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
28bb2 20 70 53 75 62 31 29 3b 0a 0a 20 20 72 65 74 75   pSub1);..  retu
28bb3 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn 1;.}.#endif /
28bb4 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
28bb5 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
28bb6 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
28bb7 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 2a  ITE_OMIT_VIEW) *
28bb8 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65  /../*.** Analyze
28bb9 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
28bba 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73 20  ement passed as 
28bbb 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  an argument to s
28bbc 65 65 20 69 66 20 69 74 0a 2a 2a 20 69 73 20 61  ee if it.** is a
28bbd 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29 20   min() or max() 
28bbe 71 75 65 72 79 2e 20 52 65 74 75 72 6e 20 57 48  query. Return WH
28bbf 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20  ERE_ORDERBY_MIN 
28bc0 6f 72 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  or WHERE_ORDERBY
28bc1 5f 4d 41 58 20 69 66 20 0a 2a 2a 20 69 74 20 69  _MAX if .** it i
28bc2 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73  s, or 0 otherwis
28bc3 65 2e 20 41 74 20 70 72 65 73 65 6e 74 2c 20 61  e. At present, a
28bc4 20 71 75 65 72 79 20 69 73 20 63 6f 6e 73 69 64   query is consid
28bc5 65 72 65 64 20 74 6f 20 62 65 0a 2a 2a 20 61 20  ered to be.** a 
28bc6 6d 69 6e 28 29 2f 6d 61 78 28 29 20 71 75 65 72  min()/max() quer
28bc7 79 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e  y if:.**.**   1.
28bc8 20 54 68 65 72 65 20 69 73 20 61 20 73 69 6e 67   There is a sing
28bc9 6c 65 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65  le object in the
28bca 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a   FROM clause..**
28bcb 0a 2a 2a 20 20 20 32 2e 20 54 68 65 72 65 20 69  .**   2. There i
28bcc 73 20 61 20 73 69 6e 67 6c 65 20 65 78 70 72 65  s a single expre
28bcd 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72 65 73  ssion in the res
28bce 75 6c 74 20 73 65 74 2c 20 61 6e 64 20 69 74 20  ult set, and it 
28bcf 69 73 0a 2a 2a 20 20 20 20 20 20 65 69 74 68 65  is.**      eithe
28bd0 72 20 6d 69 6e 28 78 29 20 6f 72 20 6d 61 78 28  r min(x) or max(
28bd1 78 29 2c 20 77 68 65 72 65 20 78 20 69 73 20 61  x), where x is a
28bd2 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63   column referenc
28bd3 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20  e..*/.static u8 
28bd4 6d 69 6e 4d 61 78 51 75 65 72 79 28 53 65 6c 65  minMaxQuery(Sele
28bd5 63 74 20 2a 70 29 7b 0a 20 20 45 78 70 72 20 2a  ct *p){.  Expr *
28bd6 70 45 78 70 72 3b 0a 20 20 45 78 70 72 4c 69 73  pExpr;.  ExprLis
28bd7 74 20 2a 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70  t *pEList = p->p
28bd8 45 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70 45  EList;..  if( pE
28bd9 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29  List->nExpr!=1 )
28bda 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52   return WHERE_OR
28bdb 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 20 20  DERBY_NORMAL;.  
28bdc 70 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e  pExpr = pEList->
28bdd 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66  a[0].pExpr;.  if
28bde 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
28bdf 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
28be0 65 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e 20  elect) ) return 
28be1 30 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 45  0;.  pEList = pE
28be2 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20  xpr->x.pList;.  
28be3 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
28be4 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 7c  K_AGG_FUNCTION |
28be5 7c 20 70 45 4c 69 73 74 3d 3d 30 20 7c 7c 20 70  | pEList==0 || p
28be6 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20  EList->nExpr!=1 
28be7 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
28be8 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  ( pEList->a[0].p
28be9 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47  Expr->op!=TK_AGG
28bea 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e  _COLUMN ) return
28beb 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e   WHERE_ORDERBY_N
28bec 4f 52 4d 41 4c 3b 0a 20 20 69 66 28 20 70 45 78  ORMAL;.  if( pEx
28bed 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 33 20 29  pr->token.n!=3 )
28bee 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52   return WHERE_OR
28bef 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 20 20  DERBY_NORMAL;.  
28bf0 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
28bf1 43 6d 70 28 28 63 68 61 72 2a 29 70 45 78 70 72  Cmp((char*)pExpr
28bf2 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d 69 6e 22 2c  ->token.z,"min",
28bf3 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  3)==0 ){.    ret
28bf4 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42  urn WHERE_ORDERB
28bf5 59 5f 4d 49 4e 3b 0a 20 20 7d 65 6c 73 65 20 69  Y_MIN;.  }else i
28bf6 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  f( sqlite3StrNIC
28bf7 6d 70 28 28 63 68 61 72 2a 29 70 45 78 70 72 2d  mp((char*)pExpr-
28bf8 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d 61 78 22 2c 33  >token.z,"max",3
28bf9 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
28bfa 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  rn WHERE_ORDERBY
28bfb 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 72 65 74 75  _MAX;.  }.  retu
28bfc 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  rn WHERE_ORDERBY
28bfd 5f 4e 4f 52 4d 41 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a  _NORMAL;.}../*.*
28bfe 2a 20 54 68 65 20 73 65 6c 65 63 74 20 73 74 61  * The select sta
28bff 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73  tement passed as
28c00 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
28c01 65 6e 74 20 69 73 20 61 6e 20 61 67 67 72 65 67  ent is an aggreg
28c02 61 74 65 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68  ate query..** Th
28c03 65 20 73 65 63 6f 6e 64 20 61 72 67 6d 65 6e 74  e second argment
28c04 20 69 73 20 74 68 65 20 61 73 73 6f 63 69 61 74   is the associat
28c05 65 64 20 61 67 67 72 65 67 61 74 65 2d 69 6e 66  ed aggregate-inf
28c06 6f 20 6f 62 6a 65 63 74 2e 20 54 68 69 73 20 0a  o object. This .
28c07 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 65 73 74  ** function test
28c08 73 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20  s if the SELECT 
28c09 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  is of the form:.
28c0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 63  **.**   SELECT c
28c0b 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74 62  ount(*) FROM <tb
28c0c 6c 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 74  l>.**.** where t
28c0d 61 62 6c 65 20 69 73 20 61 20 64 61 74 61 62 61  able is a databa
28c0e 73 65 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20  se table, not a 
28c0f 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 76 69  sub-select or vi
28c10 65 77 2e 20 49 66 20 74 68 65 20 71 75 65 72 79  ew. If the query
28c11 0a 2a 2a 20 64 6f 65 73 20 6d 61 74 63 68 20 74  .** does match t
28c12 68 69 73 20 70 61 74 74 65 72 6e 2c 20 74 68 65  his pattern, the
28c13 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
28c14 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20  he Table object 
28c15 72 65 70 72 65 73 65 6e 74 69 6e 67 0a 2a 2a 20  representing.** 
28c16 3c 74 62 6c 3e 20 69 73 20 72 65 74 75 72 6e 65  <tbl> is returne
28c17 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 20  d. Otherwise, 0 
28c18 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
28c19 73 74 61 74 69 63 20 54 61 62 6c 65 20 2a 69 73  static Table *is
28c1a 53 69 6d 70 6c 65 43 6f 75 6e 74 28 53 65 6c 65  SimpleCount(Sele
28c1b 63 74 20 2a 70 2c 20 41 67 67 49 6e 66 6f 20 2a  ct *p, AggInfo *
28c1c 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 54 61 62  pAggInfo){.  Tab
28c1d 6c 65 20 2a 70 54 61 62 3b 0a 20 20 45 78 70 72  le *pTab;.  Expr
28c1e 20 2a 70 45 78 70 72 3b 0a 0a 20 20 61 73 73 65   *pExpr;..  asse
28c1f 72 74 28 20 21 70 2d 3e 70 47 72 6f 75 70 42 79  rt( !p->pGroupBy
28c20 20 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 70 57   );..  if( p->pW
28c21 68 65 72 65 20 7c 7c 20 70 2d 3e 70 45 4c 69 73  here || p->pELis
28c22 74 2d 3e 6e 45 78 70 72 21 3d 31 20 0a 20 20 20  t->nExpr!=1 .   
28c23 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63  || p->pSrc->nSrc
28c24 21 3d 31 20 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e  !=1 || p->pSrc->
28c25 61 5b 30 5d 2e 70 53 65 6c 65 63 74 0a 20 20 29  a[0].pSelect.  )
28c26 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
28c27 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 70 2d 3e    }.  pTab = p->
28c28 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b  pSrc->a[0].pTab;
28c29 0a 20 20 70 45 78 70 72 20 3d 20 70 2d 3e 70 45  .  pExpr = p->pE
28c2a 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
28c2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
28c2c 20 26 26 20 21 70 54 61 62 2d 3e 70 53 65 6c 65   && !pTab->pSele
28c2d 63 74 20 26 26 20 70 45 78 70 72 20 29 3b 0a 0a  ct && pExpr );..
28c2e 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
28c2f 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 30  pTab) ) return 0
28c30 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ;.  if( pExpr->o
28c31 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  p!=TK_AGG_FUNCTI
28c32 4f 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  ON ) return 0;. 
28c33 20 69 66 28 20 28 70 41 67 67 49 6e 66 6f 2d 3e   if( (pAggInfo->
28c34 61 46 75 6e 63 5b 30 5d 2e 70 46 75 6e 63 2d 3e  aFunc[0].pFunc->
28c35 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 55 4e  flags&SQLITE_FUN
28c36 43 5f 43 4f 55 4e 54 29 3d 3d 30 20 29 20 72 65  C_COUNT)==0 ) re
28c37 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45  turn 0;.  if( pE
28c38 78 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f 44 69  xpr->flags&EP_Di
28c39 73 74 69 6e 63 74 20 29 20 72 65 74 75 72 6e 20  stinct ) return 
28c3a 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 61  0;..  return pTa
28c3b 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  b;.}../*.** If t
28c3c 68 65 20 73 6f 75 72 63 65 2d 6c 69 73 74 20 69  he source-list i
28c3d 74 65 6d 20 70 61 73 73 65 64 20 61 73 20 61 6e  tem passed as an
28c3e 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 61 75   argument was au
28c3f 67 6d 65 6e 74 65 64 20 77 69 74 68 20 61 6e 0a  gmented with an.
28c40 2a 2a 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  ** INDEXED BY cl
28c41 61 75 73 65 2c 20 74 68 65 6e 20 74 72 79 20 74  ause, then try t
28c42 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 70 65  o locate the spe
28c43 63 69 66 69 65 64 20 69 6e 64 65 78 2e 20 49 66  cified index. If
28c44 20 74 68 65 72 65 0a 2a 2a 20 77 61 73 20 73 75   there.** was su
28c45 63 68 20 61 20 63 6c 61 75 73 65 20 61 6e 64 20  ch a clause and 
28c46 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20  the named index 
28c47 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c  cannot be found,
28c48 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49   return .** SQLI
28c49 54 45 5f 45 52 52 4f 52 20 61 6e 64 20 6c 65 61  TE_ERROR and lea
28c4a 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 70  ve an error in p
28c4b 50 61 72 73 65 2e 20 4f 74 68 65 72 77 69 73 65  Parse. Otherwise
28c4c 2c 20 70 6f 70 75 6c 61 74 65 20 0a 2a 2a 20 70  , populate .** p
28c4d 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20 61 6e 64  From->pIndex and
28c4e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
28c4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  K..*/.SQLITE_PRI
28c50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
28c51 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28  IndexedByLookup(
28c52 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 73  Parse *pParse, s
28c53 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
28c54 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66  em *pFrom){.  if
28c55 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 26 26  ( pFrom->pTab &&
28c56 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78 20 29   pFrom->zIndex )
28c57 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  {.    Table *pTa
28c58 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b  b = pFrom->pTab;
28c59 0a 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64 65  .    char *zInde
28c5a 78 20 3d 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65  x = pFrom->zInde
28c5b 78 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  x;.    Index *pI
28c5c 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78  dx;.    for(pIdx
28c5d 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 0a  =pTab->pIndex; .
28c5e 20 20 20 20 20 20 20 20 70 49 64 78 20 26 26 20          pIdx && 
28c5f 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
28c60 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 49 6e 64  Idx->zName, zInd
28c61 65 78 29 3b 20 0a 20 20 20 20 20 20 20 20 70 49  ex); .        pI
28c62 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 0a 20  dx=pIdx->pNext. 
28c63 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70     );.    if( !p
28c64 49 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Idx ){.      sql
28c65 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
28c66 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e  rse, "no such in
28c67 64 65 78 3a 20 25 73 22 2c 20 7a 49 6e 64 65 78  dex: %s", zIndex
28c68 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 74 75  , 0);.      retu
28c69 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
28c6a 0a 20 20 20 20 7d 0a 20 20 20 20 70 46 72 6f 6d  .    }.    pFrom
28c6b 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 64 78 3b  ->pIndex = pIdx;
28c6c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
28c6d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
28c6e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
28c6f 73 20 61 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62  s a Walker callb
28c70 61 63 6b 20 66 6f 72 20 22 65 78 70 61 6e 64 69  ack for "expandi
28c71 6e 67 22 20 61 20 53 45 4c 45 43 54 20 73 74 61  ng" a SELECT sta
28c72 74 65 6d 65 6e 74 2e 0a 2a 2a 20 22 45 78 70 61  tement..** "Expa
28c73 6e 64 69 6e 67 22 20 6d 65 61 6e 73 20 74 6f 20  nding" means to 
28c74 64 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  do the following
28c75 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20  :.**.**    (1)  
28c76 4d 61 6b 65 20 73 75 72 65 20 56 44 42 45 20 63  Make sure VDBE c
28c77 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61  ursor numbers ha
28c78 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64  ve been assigned
28c79 20 74 6f 20 65 76 65 72 79 0a 2a 2a 20 20 20 20   to every.**    
28c7a 20 20 20 20 20 65 6c 65 6d 65 6e 74 20 6f 66 20       element of 
28c7b 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
28c7c 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 46  .**.**    (2)  F
28c7d 69 6c 6c 20 69 6e 20 74 68 65 20 70 54 61 62 4c  ill in the pTabL
28c7e 69 73 74 2d 3e 61 5b 5d 2e 70 54 61 62 20 66 69  ist->a[].pTab fi
28c7f 65 6c 64 73 20 69 6e 20 74 68 65 20 53 72 63 4c  elds in the SrcL
28c80 69 73 74 20 74 68 61 74 20 0a 2a 2a 20 20 20 20  ist that .**    
28c81 20 20 20 20 20 64 65 66 69 6e 65 73 20 46 52 4f       defines FRO
28c82 4d 20 63 6c 61 75 73 65 2e 20 20 57 68 65 6e 20  M clause.  When 
28c83 76 69 65 77 73 20 61 70 70 65 61 72 20 69 6e 20  views appear in 
28c84 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c  the FROM clause,
28c85 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 69 6c 6c  .**         fill
28c86 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70   pTabList->a[].p
28c87 53 65 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f  Select with a co
28c88 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  py of the SELECT
28c89 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20   statement.**   
28c8a 20 20 20 20 20 20 74 68 61 74 20 69 6d 70 6c 65        that imple
28c8b 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 2e 20  ments the view. 
28c8c 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20   A copy is made 
28c8d 6f 66 20 74 68 65 20 76 69 65 77 27 73 20 53 45  of the view's SE
28c8e 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
28c8f 73 74 61 74 65 6d 65 6e 74 20 73 6f 20 74 68 61  statement so tha
28c90 74 20 77 65 20 63 61 6e 20 66 72 65 65 6c 79 20  t we can freely 
28c91 6d 6f 64 69 66 79 20 6f 72 20 64 65 6c 65 74 65  modify or delete
28c92 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 0a   that statement.
28c93 2a 2a 20 20 20 20 20 20 20 20 20 77 69 74 68 6f  **         witho
28c94 75 74 20 77 6f 72 72 79 69 6e 67 20 61 62 6f 75  ut worrying abou
28c95 74 20 6d 65 73 73 69 6e 67 20 75 70 20 74 68 65  t messing up the
28c96 20 70 72 65 73 69 73 74 65 6e 74 20 72 65 70 72   presistent repr
28c97 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20  esentation.**   
28c98 20 20 20 20 20 20 6f 66 20 74 68 65 20 76 69 65        of the vie
28c99 77 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20  w..**.**    (3) 
28c9a 20 41 64 64 20 74 65 72 6d 73 20 74 6f 20 74 68   Add terms to th
28c9b 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  e WHERE clause t
28c9c 6f 20 61 63 63 6f 6d 6f 64 61 74 65 20 74 68 65  o accomodate the
28c9d 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64   NATURAL keyword
28c9e 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 6e 20 6a  .**         on j
28c9f 6f 69 6e 73 20 61 6e 64 20 74 68 65 20 4f 4e 20  oins and the ON 
28ca0 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
28ca1 20 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a 2a 2a   of joins..**.**
28ca2 20 20 20 20 28 34 29 20 20 53 63 61 6e 20 74 68      (4)  Scan th
28ca3 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  e list of column
28ca4 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
28ca5 73 65 74 20 28 70 45 4c 69 73 74 29 20 6c 6f 6f  set (pEList) loo
28ca6 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  king.**         
28ca7 66 6f 72 20 69 6e 73 74 61 6e 63 65 73 20 6f 66  for instances of
28ca8 20 74 68 65 20 22 2a 22 20 6f 70 65 72 61 74 6f   the "*" operato
28ca9 72 20 6f 72 20 74 68 65 20 54 41 42 4c 45 2e 2a  r or the TABLE.*
28caa 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 20 20   operator..**   
28cab 20 20 20 20 20 20 49 66 20 66 6f 75 6e 64 2c 20        If found, 
28cac 65 78 70 61 6e 64 20 65 61 63 68 20 22 2a 22 20  expand each "*" 
28cad 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c 75  to be every colu
28cae 6d 6e 20 69 6e 20 65 76 65 72 79 20 74 61 62 6c  mn in every tabl
28caf 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 6e 64  e.**         and
28cb0 20 54 41 42 4c 45 2e 2a 20 74 6f 20 62 65 20 65   TABLE.* to be e
28cb1 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 54  very column in T
28cb2 41 42 4c 45 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74  ABLE..**.*/.stat
28cb3 69 63 20 69 6e 74 20 73 65 6c 65 63 74 45 78 70  ic int selectExp
28cb4 61 6e 64 65 72 28 57 61 6c 6b 65 72 20 2a 70 57  ander(Walker *pW
28cb5 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70  alker, Select *p
28cb6 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  ){.  Parse *pPar
28cb7 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50  se = pWalker->pP
28cb8 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  arse;.  int i, j
28cb9 2c 20 6b 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a  , k;.  SrcList *
28cba 70 54 61 62 4c 69 73 74 3b 0a 20 20 45 78 70 72  pTabList;.  Expr
28cbb 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20  List *pEList;.  
28cbc 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
28cbd 74 65 6d 20 2a 70 46 72 6f 6d 3b 0a 20 20 73 71  tem *pFrom;.  sq
28cbe 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
28cbf 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64  se->db;..  if( d
28cc0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
28cc1 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
28cc2 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 20 20  RC_Abort;.  }.  
28cc3 69 66 28 20 70 2d 3e 70 53 72 63 3d 3d 30 20 7c  if( p->pSrc==0 |
28cc4 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  | (p->selFlags &
28cc5 20 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30   SF_Expanded)!=0
28cc6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
28cc7 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20  RC_Prune;.  }.  
28cc8 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  p->selFlags |= S
28cc9 46 5f 45 78 70 61 6e 64 65 64 3b 0a 20 20 70 54  F_Expanded;.  pT
28cca 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63  abList = p->pSrc
28ccb 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e  ;.  pEList = p->
28ccc 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 4d 61  pEList;..  /* Ma
28ccd 6b 65 20 73 75 72 65 20 63 75 72 73 6f 72 20 6e  ke sure cursor n
28cce 75 6d 62 65 72 73 20 68 61 76 65 20 62 65 65 6e  umbers have been
28ccf 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6c 6c   assigned to all
28cd0 20 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 2a 2a   entries in.  **
28cd1 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
28cd2 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
28cd3 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20  tatement..  */. 
28cd4 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
28cd5 73 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61  ssignCursors(pPa
28cd6 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a  rse, pTabList);.
28cd7 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 65 76  .  /* Look up ev
28cd8 65 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 64 20  ery table named 
28cd9 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
28cda 73 65 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  se of the select
28cdb 2e 20 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 6e  .  If.  ** an en
28cdc 74 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  try of the FROM 
28cdd 63 6c 61 75 73 65 20 69 73 20 61 20 73 75 62 71  clause is a subq
28cde 75 65 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20  uery instead of 
28cdf 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2c  a table or view,
28ce0 0a 20 20 2a 2a 20 74 68 65 6e 20 63 72 65 61 74  .  ** then creat
28ce1 65 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61  e a transient ta
28ce2 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 6f  ble structure to
28ce3 20 64 65 73 63 72 69 62 65 20 74 68 65 20 73 75   describe the su
28ce4 62 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 66  bquery..  */.  f
28ce5 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54  or(i=0, pFrom=pT
28ce6 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61  abList->a; i<pTa
28ce7 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
28ce8 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20  , pFrom++){.    
28ce9 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20  Table *pTab;.   
28cea 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62   if( pFrom->pTab
28ceb 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  !=0 ){.      /* 
28cec 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 68  This statement h
28ced 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
28cee 70 72 65 70 61 72 65 64 2e 20 20 54 68 65 72 65  prepared.  There
28cef 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20   is no need.    
28cf0 20 20 2a 2a 20 74 6f 20 67 6f 20 66 75 72 74 68    ** to go furth
28cf1 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  er. */.      ass
28cf2 65 72 74 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20  ert( i==0 );.   
28cf3 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
28cf4 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  une;.    }.    i
28cf5 66 28 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3d  f( pFrom->zName=
28cf6 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  =0 ){.#ifndef SQ
28cf7 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
28cf8 52 59 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20  RY.      Select 
28cf9 2a 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70  *pSel = pFrom->p
28cfa 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 2f 2a  Select;.      /*
28cfb 20 41 20 73 75 62 2d 71 75 65 72 79 20 69 6e 20   A sub-query in 
28cfc 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
28cfd 6f 66 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a 20  of a SELECT */. 
28cfe 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65       assert( pSe
28cff 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73  l!=0 );.      as
28d00 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54 61  sert( pFrom->pTa
28d01 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  b==0 );.      sq
28d02 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
28d03 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c 29 3b 0a  pWalker, pSel);.
28d04 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61        pFrom->pTa
28d05 62 20 3d 20 70 54 61 62 20 3d 20 73 71 6c 69 74  b = pTab = sqlit
28d06 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
28d07 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29  b, sizeof(Table)
28d08 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  );.      if( pTa
28d09 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52  b==0 ) return WR
28d0a 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70  C_Abort;.      p
28d0b 54 61 62 2d 3e 64 62 4d 65 6d 20 3d 20 64 62 2d  Tab->dbMem = db-
28d0c 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
28d0d 6c 65 64 20 3f 20 64 62 20 3a 20 30 3b 0a 20 20  led ? db : 0;.  
28d0e 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d      pTab->nRef =
28d0f 20 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e   1;.      pTab->
28d10 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  zName = sqlite3M
28d11 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69  Printf(db, "sqli
28d12 74 65 5f 73 75 62 71 75 65 72 79 5f 25 70 5f 22  te_subquery_%p_"
28d13 2c 20 28 76 6f 69 64 2a 29 70 54 61 62 29 3b 0a  , (void*)pTab);.
28d14 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 65        while( pSe
28d15 6c 2d 3e 70 50 72 69 6f 72 20 29 7b 20 70 53 65  l->pPrior ){ pSe
28d16 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72  l = pSel->pPrior
28d17 3b 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63 74  ; }.      select
28d18 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c  ColumnsFromExprL
28d19 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ist(pParse, pSel
28d1a 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d  ->pEList, &pTab-
28d1b 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43  >nCol, &pTab->aC
28d1c 6f 6c 29 3b 0a 20 20 20 20 20 20 70 54 61 62 2d  ol);.      pTab-
28d1d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 20  >iPKey = -1;.   
28d1e 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67     pTab->tabFlag
28d1f 73 20 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72 61  s |= TF_Ephemera
28d20 6c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65  l;.#endif.    }e
28d21 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e  lse{.      /* An
28d22 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20   ordinary table 
28d23 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 69 6e 20  or view name in 
28d24 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
28d25 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
28d26 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20   pFrom->pTab==0 
28d27 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e  );.      pFrom->
28d28 70 54 61 62 20 3d 20 70 54 61 62 20 3d 20 0a 20  pTab = pTab = . 
28d29 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c 6f         sqlite3Lo
28d2a 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
28d2b 2c 30 2c 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 2c  ,0,pFrom->zName,
28d2c 70 46 72 6f 6d 2d 3e 7a 44 61 74 61 62 61 73 65  pFrom->zDatabase
28d2d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  );.      if( pTa
28d2e 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52  b==0 ) return WR
28d2f 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70  C_Abort;.      p
28d30 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 69 66  Tab->nRef++;.#if
28d31 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
28d32 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21  _OMIT_VIEW) || !
28d33 64 65 66 69 6e 65 64 20 28 53 51 4c 49 54 45 5f  defined (SQLITE_
28d34 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
28d35 45 29 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  E).      if( pTa
28d36 62 2d 3e 70 53 65 6c 65 63 74 20 7c 7c 20 49 73  b->pSelect || Is
28d37 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
28d38 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 72  .        /* We r
28d39 65 61 63 68 20 68 65 72 65 20 69 66 20 74 68 65  each here if the
28d3a 20 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73 20   named table is 
28d3b 61 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77 20  a really a view 
28d3c 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  */.        if( s
28d3d 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
28d3e 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
28d3f 20 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20   pTab) ) return 
28d40 57 52 43 5f 41 62 6f 72 74 3b 0a 0a 20 20 20 20  WRC_Abort;..    
28d41 20 20 20 20 2f 2a 20 49 66 20 70 46 72 6f 6d 2d      /* If pFrom-
28d42 3e 70 53 65 6c 65 63 74 21 3d 30 20 69 74 20 6d  >pSelect!=0 it m
28d43 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c  eans we are deal
28d44 69 6e 67 20 77 69 74 68 20 61 0a 20 20 20 20 20  ing with a.     
28d45 20 20 20 2a 2a 20 76 69 65 77 20 77 69 74 68 69     ** view withi
28d46 6e 20 61 20 76 69 65 77 2e 20 20 54 68 65 20 53  n a view.  The S
28d47 45 4c 45 43 54 20 73 74 72 75 63 74 75 72 65 20  ELECT structure 
28d48 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
28d49 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 69  .        ** copi
28d4a 65 64 20 62 79 20 74 68 65 20 6f 75 74 65 72 20  ed by the outer 
28d4b 76 69 65 77 20 73 6f 20 77 65 20 63 61 6e 20 73  view so we can s
28d4c 6b 69 70 20 74 68 65 20 63 6f 70 79 20 73 74 65  kip the copy ste
28d4d 70 20 68 65 72 65 0a 20 20 20 20 20 20 20 20 2a  p here.        *
28d4e 2a 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 76  * in the inner v
28d4f 69 65 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  iew..        */.
28d50 20 20 20 20 20 20 20 20 69 66 28 20 70 46 72 6f          if( pFro
28d51 6d 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b  m->pSelect==0 ){
28d52 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d  .          pFrom
28d53 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  ->pSelect = sqli
28d54 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
28d55 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 2c 20   pTab->pSelect, 
28d56 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  0);.          sq
28d57 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
28d58 70 57 61 6c 6b 65 72 2c 20 70 46 72 6f 6d 2d 3e  pWalker, pFrom->
28d59 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  pSelect);.      
28d5a 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64    }.      }.#end
28d5b 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  if.    }..    /*
28d5c 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65   Locate the inde
28d5d 78 20 6e 61 6d 65 64 20 62 79 20 74 68 65 20 49  x named by the I
28d5e 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65  NDEXED BY clause
28d5f 2c 20 69 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20  , if any. */.   
28d60 20 69 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65   if( sqlite3Inde
28d61 78 65 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72  xedByLookup(pPar
28d62 73 65 2c 20 70 46 72 6f 6d 29 20 29 7b 0a 20 20  se, pFrom) ){.  
28d63 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
28d64 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  bort;.    }.  }.
28d65 0a 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 4e 41  .  /* Process NA
28d66 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 73 2c 20  TURAL keywords, 
28d67 61 6e 64 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  and ON and USING
28d68 20 63 6c 61 75 73 65 73 20 6f 66 20 6a 6f 69 6e   clauses of join
28d69 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  s..  */.  if( db
28d6a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
28d6b 7c 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a  | sqliteProcessJ
28d6c 6f 69 6e 28 70 50 61 72 73 65 2c 20 70 29 20 29  oin(pParse, p) )
28d6d 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  {.    return WRC
28d6e 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 2f  _Abort;.  }..  /
28d6f 2a 20 46 6f 72 20 65 76 65 72 79 20 22 2a 22 20  * For every "*" 
28d70 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 74  that occurs in t
28d71 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2c 20  he column list, 
28d72 69 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73  insert the names
28d73 20 6f 66 0a 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c   of.  ** all col
28d74 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c  umns in all tabl
28d75 65 73 2e 20 20 41 6e 64 20 66 6f 72 20 65 76 65  es.  And for eve
28d76 72 79 20 54 41 42 4c 45 2e 2a 20 69 6e 73 65 72  ry TABLE.* inser
28d77 74 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a  t the names.  **
28d78 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20   of all columns 
28d79 69 6e 20 54 41 42 4c 45 2e 20 20 54 68 65 20 70  in TABLE.  The p
28d7a 61 72 73 65 72 20 69 6e 73 65 72 74 65 64 20 61  arser inserted a
28d7b 20 73 70 65 63 69 61 6c 20 65 78 70 72 65 73 73   special express
28d7c 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 20 74 68  ion.  ** with th
28d7d 65 20 54 4b 5f 41 4c 4c 20 6f 70 65 72 61 74 6f  e TK_ALL operato
28d7e 72 20 66 6f 72 20 65 61 63 68 20 22 2a 22 20 74  r for each "*" t
28d7f 68 61 74 20 69 74 20 66 6f 75 6e 64 20 69 6e 20  hat it found in 
28d80 74 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e  the column list.
28d81 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  ** The follow
28d82 69 6e 67 20 63 6f 64 65 20 6a 75 73 74 20 68 61  ing code just ha
28d83 73 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  s to locate the 
28d84 54 4b 5f 41 4c 4c 20 65 78 70 72 65 73 73 69 6f  TK_ALL expressio
28d85 6e 73 20 61 6e 64 20 65 78 70 61 6e 64 0a 20 20  ns and expand.  
28d86 2a 2a 20 65 61 63 68 20 6f 6e 65 20 74 6f 20 74  ** each one to t
28d87 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63  he list of all c
28d88 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61  olumns in all ta
28d89 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  bles..  **.  ** 
28d8a 54 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 20 6a  The first loop j
28d8b 75 73 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65  ust checks to se
28d8c 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  e if there are a
28d8d 6e 79 20 22 2a 22 20 6f 70 65 72 61 74 6f 72 73  ny "*" operators
28d8e 0a 20 20 2a 2a 20 74 68 61 74 20 6e 65 65 64 20  .  ** that need 
28d8f 65 78 70 61 6e 64 69 6e 67 2e 0a 20 20 2a 2f 0a  expanding..  */.
28d90 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c    for(k=0; k<pEL
28d91 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29  ist->nExpr; k++)
28d92 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d  {.    Expr *pE =
28d93 20 70 45 4c 69 73 74 2d 3e 61 5b 6b 5d 2e 70 45   pEList->a[k].pE
28d94 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45 2d  xpr;.    if( pE-
28d95 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72  >op==TK_ALL ) br
28d96 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 70 45 2d  eak;.    if( pE-
28d97 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20 70  >op==TK_DOT && p
28d98 45 2d 3e 70 52 69 67 68 74 20 26 26 20 70 45 2d  E->pRight && pE-
28d99 3e 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f  >pRight->op==TK_
28d9a 41 4c 4c 0a 20 20 20 20 20 20 20 20 20 26 26 20  ALL.         && 
28d9b 70 45 2d 3e 70 4c 65 66 74 20 26 26 20 70 45 2d  pE->pLeft && pE-
28d9c 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49  >pLeft->op==TK_I
28d9d 44 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20  D ) break;.  }. 
28d9e 20 69 66 28 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e   if( k<pEList->n
28d9f 45 78 70 72 20 29 7b 0a 20 20 20 20 2f 2a 0a 20  Expr ){.    /*. 
28da0 20 20 20 2a 2a 20 49 66 20 77 65 20 67 65 74 20     ** If we get 
28da1 68 65 72 65 20 69 74 20 6d 65 61 6e 73 20 74 68  here it means th
28da2 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6e  e result set con
28da3 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
28da4 65 20 22 2a 22 0a 20 20 20 20 2a 2a 20 6f 70 65  e "*".    ** ope
28da5 72 61 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64  rators that need
28da6 20 74 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e   to be expanded.
28da7 20 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65    Loop through e
28da8 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  ach expression. 
28da9 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 73     ** in the res
28daa 75 6c 74 20 73 65 74 20 61 6e 64 20 65 78 70 61  ult set and expa
28dab 6e 64 20 74 68 65 6d 20 6f 6e 65 20 62 79 20 6f  nd them one by o
28dac 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ne..    */.    s
28dad 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
28dae 74 65 6d 20 2a 61 20 3d 20 70 45 4c 69 73 74 2d  tem *a = pEList-
28daf 3e 61 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74  >a;.    ExprList
28db0 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20   *pNew = 0;.    
28db1 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 50 61 72  int flags = pPar
28db2 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 3b 0a 20  se->db->flags;. 
28db3 20 20 20 69 6e 74 20 6c 6f 6e 67 4e 61 6d 65 73     int longNames
28db4 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
28db5 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29  TE_FullColNames)
28db6 21 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20  !=0.            
28db7 20 20 20 20 20 20 20 20 20 20 26 26 20 28 66 6c            && (fl
28db8 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 68 6f  ags & SQLITE_Sho
28db9 72 74 43 6f 6c 4e 61 6d 65 73 29 3d 3d 30 3b 0a  rtColNames)==0;.
28dba 0a 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c  .    for(k=0; k<
28dbb 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b  pEList->nExpr; k
28dbc 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  ++){.      Expr 
28dbd 2a 70 45 20 3d 20 61 5b 6b 5d 2e 70 45 78 70 72  *pE = a[k].pExpr
28dbe 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e  ;.      if( pE->
28dbf 6f 70 21 3d 54 4b 5f 41 4c 4c 20 26 26 0a 20 20  op!=TK_ALL &&.  
28dc0 20 20 20 20 20 20 20 20 20 28 70 45 2d 3e 6f 70           (pE->op
28dc1 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e  !=TK_DOT || pE->
28dc2 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 45 2d  pRight==0 || pE-
28dc3 3e 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f  >pRight->op!=TK_
28dc4 41 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ALL) ){.        
28dc5 2f 2a 20 54 68 69 73 20 70 61 72 74 69 63 75 6c  /* This particul
28dc6 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 64 6f  ar expression do
28dc7 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
28dc8 65 20 65 78 70 61 6e 64 65 64 2e 0a 20 20 20 20  e expanded..    
28dc9 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70      */.        p
28dca 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
28dcb 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
28dcc 73 65 2c 20 70 4e 65 77 2c 20 61 5b 6b 5d 2e 70  se, pNew, a[k].p
28dcd 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  Expr, 0);.      
28dce 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20    if( pNew ){.  
28dcf 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 61 5b          pNew->a[
28dd0 70 4e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a  pNew->nExpr-1].z
28dd1 4e 61 6d 65 20 3d 20 61 5b 6b 5d 2e 7a 4e 61 6d  Name = a[k].zNam
28dd2 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
28dd3 20 20 20 20 20 61 5b 6b 5d 2e 70 45 78 70 72 20       a[k].pExpr 
28dd4 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 61 5b 6b  = 0;.        a[k
28dd5 5d 2e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20  ].zName = 0;.   
28dd6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
28dd7 20 20 2f 2a 20 54 68 69 73 20 65 78 70 72 65 73    /* This expres
28dd8 73 69 6f 6e 20 69 73 20 61 20 22 2a 22 20 6f 72  sion is a "*" or
28dd9 20 61 20 22 54 41 42 4c 45 2e 2a 22 20 61 6e 64   a "TABLE.*" and
28dda 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20   needs to be.   
28ddb 20 20 20 20 20 2a 2a 20 65 78 70 61 6e 64 65 64       ** expanded
28ddc 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74  . */.        int
28ddd 20 74 61 62 6c 65 53 65 65 6e 20 3d 20 30 3b 20   tableSeen = 0; 
28dde 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31       /* Set to 1
28ddf 20 77 68 65 6e 20 54 41 42 4c 45 20 6d 61 74 63   when TABLE matc
28de0 68 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 63  hes */.        c
28de1 68 61 72 20 2a 7a 54 4e 61 6d 65 3b 20 20 20 20  har *zTName;    
28de2 20 20 20 20 20 20 20 20 2f 2a 20 74 65 78 74 20          /* text 
28de3 6f 66 20 6e 61 6d 65 20 6f 66 20 54 41 42 4c 45  of name of TABLE
28de4 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
28de5 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26  pE->op==TK_DOT &
28de6 26 20 70 45 2d 3e 70 4c 65 66 74 20 29 7b 0a 20  & pE->pLeft ){. 
28de7 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65 20           zTName 
28de8 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
28de9 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 70 45 2d 3e  mToken(db, &pE->
28dea 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 29 3b 0a 20  pLeft->token);. 
28deb 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
28dec 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65 20 3d          zTName =
28ded 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
28dee 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70        for(i=0, p
28def 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61  From=pTabList->a
28df0 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
28df1 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b  rc; i++, pFrom++
28df2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 54 61 62  ){.          Tab
28df3 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d  le *pTab = pFrom
28df4 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20  ->pTab;.        
28df5 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65    char *zTabName
28df6 20 3d 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61 73   = pFrom->zAlias
28df7 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
28df8 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 7c 7c 20 7a  zTabName==0 || z
28df9 54 61 62 4e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b  TabName[0]==0 ){
28dfa 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 54   .            zT
28dfb 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a  abName = pTab->z
28dfc 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Name;.          
28dfd 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  }.          if( 
28dfe 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
28dff 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
28e00 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65 20 26      if( zTName &
28e01 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
28e02 28 7a 54 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d  (zTName, zTabNam
28e03 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)!=0 ){.       
28e04 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
28e05 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
28e06 20 20 20 20 20 74 61 62 6c 65 53 65 65 6e 20 3d       tableSeen =
28e07 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   1;.          fo
28e08 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e  r(j=0; j<pTab->n
28e09 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
28e0a 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78         Expr *pEx
28e0b 70 72 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20  pr, *pRight;.   
28e0c 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
28e0d 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f  Name = pTab->aCo
28e0e 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 20  l[j].zName;..   
28e0f 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61           /* If a
28e10 20 63 6f 6c 75 6d 6e 20 69 73 20 6d 61 72 6b 65   column is marke
28e11 64 20 61 73 20 27 68 69 64 64 65 6e 27 20 28 63  d as 'hidden' (c
28e12 75 72 72 65 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f  urrently only po
28e13 73 73 69 62 6c 65 0a 20 20 20 20 20 20 20 20 20  ssible.         
28e14 20 20 20 2a 2a 20 66 6f 72 20 76 69 72 74 75 61     ** for virtua
28e15 6c 20 74 61 62 6c 65 73 29 2c 20 64 6f 20 6e 6f  l tables), do no
28e16 74 20 69 6e 63 6c 75 64 65 20 69 74 20 69 6e 20  t include it in 
28e17 74 68 65 20 65 78 70 61 6e 64 65 64 0a 20 20 20  the expanded.   
28e18 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 73 75           ** resu
28e19 6c 74 2d 73 65 74 20 6c 69 73 74 2e 0a 20 20 20  lt-set list..   
28e1a 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
28e1b 20 20 20 20 20 20 20 20 69 66 28 20 49 73 48 69          if( IsHi
28e1c 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62  ddenColumn(&pTab
28e1d 2d 3e 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20  ->aCol[j]) ){.  
28e1e 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
28e1f 72 74 28 49 73 56 69 72 74 75 61 6c 28 70 54 61  rt(IsVirtual(pTa
28e20 62 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  b));.           
28e21 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
28e22 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20           }..    
28e23 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 30 20          if( i>0 
28e24 26 26 20 7a 54 4e 61 6d 65 3d 3d 30 20 29 7b 0a  && zTName==0 ){.
28e25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
28e26 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
28e27 6d 20 2a 70 4c 65 66 74 20 3d 20 26 70 54 61 62  m *pLeft = &pTab
28e28 4c 69 73 74 2d 3e 61 5b 69 2d 31 5d 3b 0a 20 20  List->a[i-1];.  
28e29 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
28e2a 28 70 4c 65 66 74 5b 31 5d 2e 6a 6f 69 6e 74 79  (pLeft[1].jointy
28e2b 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 29  pe & JT_NATURAL)
28e2c 21 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20 20  !=0 &&.         
28e2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
28e2e 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66 74  olumnIndex(pLeft
28e2f 2d 3e 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d  ->pTab, zName)>=
28e30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
28e31 20 20 20 20 20 2f 2a 20 49 6e 20 61 20 4e 41 54       /* In a NAT
28e32 55 52 41 4c 20 6a 6f 69 6e 2c 20 6f 6d 69 74 20  URAL join, omit 
28e33 74 68 65 20 6a 6f 69 6e 20 63 6f 6c 75 6d 6e 73  the join columns
28e34 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 20   from the .     
28e35 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 61             ** ta
28e36 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68 74  ble on the right
28e37 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
28e38 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
28e39 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
28e3a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
28e3b 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64  sqlite3IdListInd
28e3c 65 78 28 70 4c 65 66 74 5b 31 5d 2e 70 55 73 69  ex(pLeft[1].pUsi
28e3d 6e 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b  ng, zName)>=0 ){
28e3e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28e3f 20 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e 20 77 69   /* In a join wi
28e40 74 68 20 61 20 55 53 49 4e 47 20 63 6c 61 75 73  th a USING claus
28e41 65 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d 6e 73 20  e, omit columns 
28e42 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20  in the.         
28e43 20 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20         ** using 
28e44 63 6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65 20  clause from the 
28e45 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67  table on the rig
28e46 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ht. */.         
28e47 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
28e48 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
28e49 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
28e4a 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68             pRigh
28e4b 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  t = sqlite3PExpr
28e4c 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
28e4d 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  0, 0, 0);.      
28e4e 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74        if( pRight
28e4f 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
28e50 20 20 20 20 20 20 20 20 20 73 65 74 54 6f 6b 65           setToke
28e51 6e 28 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e  n(&pRight->token
28e52 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
28e53 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61        if( longNa
28e54 6d 65 73 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d  mes || pTabList-
28e55 3e 6e 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20  >nSrc>1 ){.     
28e56 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70           Expr *p
28e57 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 50 45  Left = sqlite3PE
28e58 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
28e59 44 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  D, 0, 0, 0);.   
28e5a 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
28e5b 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
28e5c 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
28e5d 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30  pLeft, pRight, 0
28e5e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
28e5f 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
28e60 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
28e61 20 20 20 20 20 73 65 74 54 6f 6b 65 6e 28 26 70       setToken(&p
28e62 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2c 20 7a 54 61  Left->token, zTa
28e63 62 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  bName);.        
28e64 20 20 20 20 20 20 73 65 74 54 6f 6b 65 6e 28 26        setToken(&
28e65 70 45 78 70 72 2d 3e 73 70 61 6e 2c 20 0a 20 20  pExpr->span, .  
28e66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28e67 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
28e68 62 2c 20 22 25 73 2e 25 73 22 2c 20 7a 54 61 62  b, "%s.%s", zTab
28e69 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 29 3b 0a 20  Name, zName));. 
28e6a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
28e6b 70 72 2d 3e 73 70 61 6e 2e 64 79 6e 20 3d 20 31  pr->span.dyn = 1
28e6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
28e6d 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d  pExpr->token.z =
28e6e 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   0;.            
28e6f 20 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e    pExpr->token.n
28e70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
28e71 20 20 20 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e      pExpr->token
28e72 2e 64 79 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  .dyn = 0;.      
28e73 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
28e74 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
28e75 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 20   = pRight;.     
28e76 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
28e77 73 70 61 6e 20 3d 20 70 45 78 70 72 2d 3e 74 6f  span = pExpr->to
28e78 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ken;.           
28e79 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e 2e 64     pExpr->span.d
28e7a 79 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  yn = 0;.        
28e7b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
28e7c 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20    if( longNames 
28e7d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
28e7e 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45   pNew = sqlite3E
28e7f 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
28e80 61 72 73 65 2c 20 70 4e 65 77 2c 20 70 45 78 70  arse, pNew, pExp
28e81 72 2c 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29  r, &pExpr->span)
28e82 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65  ;.            }e
28e83 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
28e84 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
28e85 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
28e86 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 70 45  pParse, pNew, pE
28e87 78 70 72 2c 20 26 70 52 69 67 68 74 2d 3e 74 6f  xpr, &pRight->to
28e88 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ken);.          
28e89 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
28e8a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
28e8b 20 20 69 66 28 20 21 74 61 62 6c 65 53 65 65 6e    if( !tableSeen
28e8c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
28e8d 28 20 7a 54 4e 61 6d 65 20 29 7b 0a 20 20 20 20  ( zTName ){.    
28e8e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
28e8f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
28e90 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a 20  "no such table: 
28e91 25 73 22 2c 20 7a 54 4e 61 6d 65 29 3b 0a 20 20  %s", zTName);.  
28e92 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
28e93 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28e94 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
28e95 65 2c 20 22 6e 6f 20 74 61 62 6c 65 73 20 73 70  e, "no tables sp
28e96 65 63 69 66 69 65 64 22 29 3b 0a 20 20 20 20 20  ecified");.     
28e97 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
28e98 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28e99 44 62 46 72 65 65 28 64 62 2c 20 7a 54 4e 61 6d  DbFree(db, zTNam
28e9a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
28e9b 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  }.    sqlite3Exp
28e9c 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
28e9d 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e  pEList);.    p->
28e9e 70 45 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20  pEList = pNew;. 
28e9f 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41   }.#if SQLITE_MA
28ea0 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70  X_COLUMN.  if( p
28ea1 2d 3e 70 45 4c 69 73 74 20 26 26 20 70 2d 3e 70  ->pEList && p->p
28ea2 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 64 62 2d  EList->nExpr>db-
28ea3 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
28ea4 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a  IMIT_COLUMN] ){.
28ea5 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
28ea6 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
28ea7 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e   many columns in
28ea8 20 72 65 73 75 6c 74 20 73 65 74 22 29 3b 0a 20   result set");. 
28ea9 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
28eaa 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
28eab 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20  .}../*.** No-op 
28eac 72 6f 75 74 69 6e 65 20 66 6f 72 20 74 68 65 20  routine for the 
28ead 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b 65  parse-tree walke
28eae 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  r..**.** When th
28eaf 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
28eb0 65 20 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 61  e Walker.xExprCa
28eb1 6c 6c 62 61 63 6b 20 74 68 65 6e 20 65 78 70 72  llback then expr
28eb2 65 73 73 69 6f 6e 20 74 72 65 65 73 0a 2a 2a 20  ession trees.** 
28eb3 61 72 65 20 77 61 6c 6b 65 64 20 77 69 74 68 6f  are walked witho
28eb4 75 74 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 62  ut any actions b
28eb5 65 69 6e 67 20 74 61 6b 65 6e 20 61 74 20 65 61  eing taken at ea
28eb6 63 68 20 6e 6f 64 65 2e 20 20 50 72 65 73 75 6d  ch node.  Presum
28eb7 61 62 6c 79 2c 0a 2a 2a 20 77 68 65 6e 20 74 68  ably,.** when th
28eb8 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
28eb9 65 64 20 66 6f 72 20 57 61 6c 6b 65 72 2e 78 45  ed for Walker.xE
28eba 78 70 72 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e  xprCallback then
28ebb 20 0a 2a 2a 20 57 61 6c 6b 65 72 2e 78 53 65 6c   .** Walker.xSel
28ebc 65 63 74 43 61 6c 6c 62 61 63 6b 20 69 73 20 73  ectCallback is s
28ebd 65 74 20 74 6f 20 64 6f 20 73 6f 6d 65 74 68 69  et to do somethi
28ebe 6e 67 20 75 73 65 66 75 6c 20 66 6f 72 20 65 76  ng useful for ev
28ebf 65 72 79 20 0a 2a 2a 20 73 75 62 71 75 65 72 79  ery .** subquery
28ec0 20 69 6e 20 74 68 65 20 70 61 72 73 65 72 20 74   in the parser t
28ec1 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ree..*/.static i
28ec2 6e 74 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 28  nt exprWalkNoop(
28ec3 57 61 6c 6b 65 72 20 2a 4e 6f 74 55 73 65 64 2c  Walker *NotUsed,
28ec4 20 45 78 70 72 20 2a 4e 6f 74 55 73 65 64 32 29   Expr *NotUsed2)
28ec5 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
28ec6 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
28ec7 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75  otUsed2);.  retu
28ec8 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
28ec9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
28eca 6f 75 74 69 6e 65 20 22 65 78 70 61 6e 64 73 22  outine "expands"
28ecb 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
28ecc 65 6e 74 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69  ent and all of i
28ecd 74 73 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a  ts subqueries..*
28ece 2a 20 46 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  * For additional
28ecf 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20   information on 
28ed0 77 68 61 74 20 69 74 20 6d 65 61 6e 73 20 74 6f  what it means to
28ed1 20 22 65 78 70 61 6e 64 22 20 61 20 53 45 4c 45   "expand" a SELE
28ed2 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2c  CT.** statement,
28ed3 20 73 65 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74   see the comment
28ed4 20 6f 6e 20 74 68 65 20 73 65 6c 65 63 74 45 78   on the selectEx
28ed5 70 61 6e 64 20 77 6f 72 6b 65 72 20 63 61 6c 6c  pand worker call
28ed6 62 61 63 6b 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a  back above..**.*
28ed7 2a 20 45 78 70 61 6e 64 69 6e 67 20 61 20 53 45  * Expanding a SE
28ed8 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
28ed9 73 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70  s the first step
28eda 20 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61   in processing a
28edb 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65  .** SELECT state
28edc 6d 65 6e 74 2e 20 20 54 68 65 20 53 45 4c 45 43  ment.  The SELEC
28edd 54 20 73 74 61 74 65 6d 65 6e 74 20 6d 75 73 74  T statement must
28ede 20 62 65 20 65 78 70 61 6e 64 65 64 20 62 65 66   be expanded bef
28edf 6f 72 65 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f  ore.** name reso
28ee0 6c 75 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72  lution is perfor
28ee1 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  med..**.** If an
28ee2 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ything goes wron
28ee3 67 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  g, an error mess
28ee4 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  age is written i
28ee5 6e 74 6f 20 70 50 61 72 73 65 2e 0a 2a 2a 20 54  nto pParse..** T
28ee6 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
28ee7 69 6f 6e 20 63 61 6e 20 64 65 74 65 63 74 20 74  ion can detect t
28ee8 68 65 20 70 72 6f 62 6c 65 6d 20 62 79 20 6c 6f  he problem by lo
28ee9 6f 6b 69 6e 67 20 61 74 20 70 50 61 72 73 65 2d  oking at pParse-
28eea 3e 6e 45 72 72 0a 2a 2a 20 61 6e 64 2f 6f 72 20  >nErr.** and/or 
28eeb 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
28eec 6f 63 46 61 69 6c 65 64 2e 0a 2a 2f 0a 73 74 61  ocFailed..*/.sta
28eed 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
28eee 53 65 6c 65 63 74 45 78 70 61 6e 64 28 50 61 72  SelectExpand(Par
28eef 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65  se *pParse, Sele
28ef0 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20  ct *pSelect){.  
28ef1 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 53  Walker w;.  w.xS
28ef2 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20  electCallback = 
28ef3 73 65 6c 65 63 74 45 78 70 61 6e 64 65 72 3b 0a  selectExpander;.
28ef4 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63    w.xExprCallbac
28ef5 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70  k = exprWalkNoop
28ef6 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70  ;.  w.pParse = p
28ef7 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33  Parse;.  sqlite3
28ef8 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70  WalkSelect(&w, p
28ef9 53 65 6c 65 63 74 29 3b 0a 7d 0a 0a 0a 23 69 66  Select);.}...#if
28efa 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
28efb 5f 53 55 42 51 55 45 52 59 0a 2f 2a 0a 2a 2a 20  _SUBQUERY./*.** 
28efc 54 68 69 73 20 69 73 20 61 20 57 61 6c 6b 65 72  This is a Walker
28efd 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
28efe 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68   callback for th
28eff 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 54  e sqlite3SelectT
28f00 79 70 65 49 6e 66 6f 28 29 0a 2a 2a 20 69 6e 74  ypeInfo().** int
28f01 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f  erface..**.** Fo
28f02 72 20 65 61 63 68 20 46 52 4f 4d 2d 63 6c 61 75  r each FROM-clau
28f03 73 65 20 73 75 62 71 75 65 72 79 2c 20 61 64 64  se subquery, add
28f04 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 20 61 6e   Column.zType an
28f05 64 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 0a 2a  d Column.zColl.*
28f06 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  * information to
28f07 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
28f08 74 75 72 65 20 74 68 61 74 20 72 65 70 72 65 73  ture that repres
28f09 65 6e 74 73 20 74 68 65 20 72 65 73 75 6c 74 20  ents the result 
28f0a 73 65 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 73  set.** of that s
28f0b 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 54  ubquery..**.** T
28f0c 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
28f0d 72 65 20 74 68 61 74 20 72 65 70 72 65 73 65 6e  re that represen
28f0e 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ts the result se
28f0f 74 20 77 61 73 20 63 6f 6e 73 74 72 75 63 74 65  t was constructe
28f10 64 0a 2a 2a 20 62 79 20 73 65 6c 65 63 74 45 78  d.** by selectEx
28f11 70 61 6e 64 65 72 28 29 20 62 75 74 20 74 68 65  pander() but the
28f12 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74   type and collat
28f13 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ion information 
28f14 77 61 73 20 6f 6d 69 74 74 65 64 0a 2a 2a 20 61  was omitted.** a
28f15 74 20 74 68 61 74 20 70 6f 69 6e 74 20 62 65 63  t that point bec
28f16 61 75 73 65 20 69 64 65 6e 74 69 66 69 65 72 73  ause identifiers
28f17 20 68 61 64 20 6e 6f 74 20 79 65 74 20 62 65 65   had not yet bee
28f18 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20 54 68 69  n resolved.  Thi
28f19 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
28f1a 63 61 6c 6c 65 64 20 61 66 74 65 72 20 69 64 65  called after ide
28f1b 6e 74 69 66 69 65 72 20 72 65 73 6f 6c 75 74 69  ntifier resoluti
28f1c 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
28f1d 74 20 73 65 6c 65 63 74 41 64 64 53 75 62 71 75  t selectAddSubqu
28f1e 65 72 79 54 79 70 65 49 6e 66 6f 28 57 61 6c 6b  eryTypeInfo(Walk
28f1f 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c  er *pWalker, Sel
28f20 65 63 74 20 2a 70 29 7b 0a 20 20 50 61 72 73 65  ect *p){.  Parse
28f21 20 2a 70 50 61 72 73 65 3b 0a 20 20 69 6e 74 20   *pParse;.  int 
28f22 69 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  i;.  SrcList *pT
28f23 61 62 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74  abList;.  struct
28f24 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
28f25 46 72 6f 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28  From;..  assert(
28f26 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53   p->selFlags & S
28f27 46 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20  F_Resolved );.  
28f28 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73  if( (p->selFlags
28f29 20 26 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66   & SF_HasTypeInf
28f2a 6f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  o)==0 ){.    p->
28f2b 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 48  selFlags |= SF_H
28f2c 61 73 54 79 70 65 49 6e 66 6f 3b 0a 20 20 20 20  asTypeInfo;.    
28f2d 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72  pParse = pWalker
28f2e 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 70 54  ->pParse;.    pT
28f2f 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63  abList = p->pSrc
28f30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70  ;.    for(i=0, p
28f31 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61  From=pTabList->a
28f32 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
28f33 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b  rc; i++, pFrom++
28f34 29 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a  ){.      Table *
28f35 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54  pTab = pFrom->pT
28f36 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  ab;.      if( pT
28f37 61 62 20 26 26 20 28 70 54 61 62 2d 3e 74 61 62  ab && (pTab->tab
28f38 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d  Flags & TF_Ephem
28f39 65 72 61 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20  eral)!=0 ){.    
28f3a 20 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65      /* A sub-que
28f3b 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ry in the FROM c
28f3c 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
28f3d 54 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c  T */.        Sel
28f3e 65 63 74 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f  ect *pSel = pFro
28f3f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20  m->pSelect;.    
28f40 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
28f41 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c   );.        whil
28f42 65 28 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20  e( pSel->pPrior 
28f43 29 20 70 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70  ) pSel = pSel->p
28f44 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20 20 73  Prior;.        s
28f45 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79  electAddColumnTy
28f46 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70  peAndCollation(p
28f47 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f  Parse, pTab->nCo
28f48 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70  l, pTab->aCol, p
28f49 53 65 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Sel);.      }.  
28f4a 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
28f4b 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
28f4c 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
28f4d 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64  This routine add
28f4e 73 20 64 61 74 61 74 79 70 65 20 61 6e 64 20 63  s datatype and c
28f4f 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
28f50 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  e information to
28f51 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65 20 73 74  .** the Table st
28f52 72 75 63 74 75 72 65 73 20 6f 66 20 61 6c 6c 20  ructures of all 
28f53 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71  FROM-clause subq
28f54 75 65 72 69 65 73 20 69 6e 20 61 0a 2a 2a 20 53  ueries in a.** S
28f55 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
28f56 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 69 73 20  .**.** Use this 
28f57 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 6e 61  routine after na
28f58 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a  me resolution..*
28f59 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
28f5a 6c 69 74 65 33 53 65 6c 65 63 74 41 64 64 54 79  lite3SelectAddTy
28f5b 70 65 49 6e 66 6f 28 50 61 72 73 65 20 2a 70 50  peInfo(Parse *pP
28f5c 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53  arse, Select *pS
28f5d 65 6c 65 63 74 29 7b 0a 23 69 66 6e 64 65 66 20  elect){.#ifndef 
28f5e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
28f5f 55 45 52 59 0a 20 20 57 61 6c 6b 65 72 20 77 3b  UERY.  Walker w;
28f60 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
28f61 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 41 64 64  back = selectAdd
28f62 53 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f  SubqueryTypeInfo
28f63 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
28f64 61 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f  ack = exprWalkNo
28f65 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d  op;.  w.pParse =
28f66 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74   pParse;.  sqlit
28f67 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c  e3WalkSelect(&w,
28f68 20 70 53 65 6c 65 63 74 29 3b 0a 23 65 6e 64 69   pSelect);.#endi
28f69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  f.}.../*.** This
28f6a 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 6f 66   routine sets of
28f6b 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
28f6c 65 6e 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69  ent for processi
28f6d 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c  ng.  The.** foll
28f6e 6f 77 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c  owing is accompl
28f6f 69 73 68 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ished:.**.**    
28f70 20 2a 20 20 56 44 42 45 20 43 75 72 73 6f 72 20   *  VDBE Cursor 
28f71 6e 75 6d 62 65 72 73 20 61 72 65 20 61 73 73 69  numbers are assi
28f72 67 6e 65 64 20 74 6f 20 61 6c 6c 20 46 52 4f 4d  gned to all FROM
28f73 2d 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a  -clause terms..*
28f74 2a 20 20 20 20 20 2a 20 20 45 70 68 65 6d 65 72  *     *  Ephemer
28f75 61 6c 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 73  al Table objects
28f76 20 61 72 65 20 63 72 65 61 74 65 64 20 66 6f 72   are created for
28f77 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65   all FROM-clause
28f78 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20   subqueries..** 
28f79 20 20 20 20 2a 20 20 4f 4e 20 61 6e 64 20 55 53      *  ON and US
28f7a 49 4e 47 20 63 6c 61 75 73 65 73 20 61 72 65 20  ING clauses are 
28f7b 73 68 69 66 74 65 64 20 69 6e 74 6f 20 57 48 45  shifted into WHE
28f7c 52 45 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  RE statements.**
28f7d 20 20 20 20 20 2a 20 20 57 69 6c 64 63 61 72 64       *  Wildcard
28f7e 73 20 22 2a 22 20 61 6e 64 20 22 54 41 42 4c 45  s "*" and "TABLE
28f7f 2e 2a 22 20 69 6e 20 72 65 73 75 6c 74 20 73 65  .*" in result se
28f80 74 73 20 61 72 65 20 65 78 70 61 6e 64 65 64 2e  ts are expanded.
28f81 0a 2a 2a 20 20 20 20 20 2a 20 20 49 64 65 6e 74  .**     *  Ident
28f82 69 66 69 65 72 73 20 69 6e 20 65 78 70 72 65 73  ifiers in expres
28f83 73 69 6f 6e 20 61 72 65 20 6d 61 74 63 68 65 64  sion are matched
28f84 20 74 6f 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a   to tables..**.*
28f85 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
28f86 63 74 73 20 72 65 63 75 72 73 69 76 65 6c 79 20  cts recursively 
28f87 6f 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65  on all subquerie
28f88 73 20 77 69 74 68 69 6e 20 74 68 65 20 53 45 4c  s within the SEL
28f89 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ECT..*/.SQLITE_P
28f8a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
28f8b 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 0a 20  te3SelectPrep(. 
28f8c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
28f8d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
28f8e 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
28f8f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
28f90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
28f91 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
28f92 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a  t being coded. *
28f93 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
28f94 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61  *pOuterNC  /* Na
28f95 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 63  me context for c
28f96 6f 6e 74 61 69 6e 65 72 20 2a 2f 0a 29 7b 0a 20  ontainer */.){. 
28f97 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
28f98 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
28f99 6e 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  n;.  db = pParse
28f9a 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d 3e 73  ->db;.  if( p->s
28f9b 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 48 61 73  elFlags & SF_Has
28f9c 54 79 70 65 49 6e 66 6f 20 29 20 72 65 74 75 72  TypeInfo ) retur
28f9d 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  n;.  if( pParse-
28f9e 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
28f9f 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
28fa0 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  rn;.  sqlite3Sel
28fa1 65 63 74 45 78 70 61 6e 64 28 70 50 61 72 73 65  ectExpand(pParse
28fa2 2c 20 70 29 3b 0a 20 20 69 66 28 20 70 50 61 72  , p);.  if( pPar
28fa3 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
28fa4 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
28fa5 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
28fa6 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d  ResolveSelectNam
28fa7 65 73 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4f  es(pParse, p, pO
28fa8 75 74 65 72 4e 43 29 3b 0a 20 20 69 66 28 20 70  uterNC);.  if( p
28fa9 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
28faa 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
28fab 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
28fac 74 65 33 53 65 6c 65 63 74 41 64 64 54 79 70 65  te3SelectAddType
28fad 49 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 29 3b  Info(pParse, p);
28fae 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20  .}../*.** Reset 
28faf 74 68 65 20 61 67 67 72 65 67 61 74 65 20 61 63  the aggregate ac
28fb0 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  cumulator..**.**
28fb1 20 54 68 65 20 61 67 67 72 65 67 61 74 65 20 61   The aggregate a
28fb2 63 63 75 6d 75 6c 61 74 6f 72 20 69 73 20 61 20  ccumulator is a 
28fb3 73 65 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65  set of memory ce
28fb4 6c 6c 73 20 74 68 61 74 20 68 6f 6c 64 0a 2a 2a  lls that hold.**
28fb5 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72 65   intermediate re
28fb6 73 75 6c 74 73 20 77 68 69 6c 65 20 63 61 6c 63  sults while calc
28fb7 75 6c 61 74 69 6e 67 20 61 6e 20 61 67 67 72 65  ulating an aggre
28fb8 67 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a 20 72  gate.  This.** r
28fb9 6f 75 74 69 6e 65 20 73 69 6d 70 6c 79 20 73 74  outine simply st
28fba 6f 72 65 73 20 4e 55 4c 4c 73 20 69 6e 20 61 6c  ores NULLs in al
28fbb 6c 20 6f 66 20 74 68 6f 73 65 20 6d 65 6d 6f 72  l of those memor
28fbc 79 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74  y cells..*/.stat
28fbd 69 63 20 76 6f 69 64 20 72 65 73 65 74 41 63 63  ic void resetAcc
28fbe 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65 20 2a  umulator(Parse *
28fbf 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20  pParse, AggInfo 
28fc0 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64  *pAggInfo){.  Vd
28fc1 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
28fc2 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a  pVdbe;.  int i;.
28fc3 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f    struct AggInfo
28fc4 5f 66 75 6e 63 20 2a 70 46 75 6e 63 3b 0a 20 20  _func *pFunc;.  
28fc5 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 46  if( pAggInfo->nF
28fc6 75 6e 63 2b 70 41 67 67 49 6e 66 6f 2d 3e 6e 43  unc+pAggInfo->nC
28fc7 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  olumn==0 ){.    
28fc8 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f  return;.  }.  fo
28fc9 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66  r(i=0; i<pAggInf
28fca 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  o->nColumn; i++)
28fcb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
28fcc 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
28fcd 6c 6c 2c 20 30 2c 20 70 41 67 67 49 6e 66 6f 2d  ll, 0, pAggInfo-
28fce 3e 61 43 6f 6c 5b 69 5d 2e 69 4d 65 6d 29 3b 0a  >aCol[i].iMem);.
28fcf 20 20 7d 0a 20 20 66 6f 72 28 70 46 75 6e 63 3d    }.  for(pFunc=
28fd0 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c  pAggInfo->aFunc,
28fd1 20 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f   i=0; i<pAggInfo
28fd2 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46  ->nFunc; i++, pF
28fd3 75 6e 63 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  unc++){.    sqli
28fd4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
28fd5 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 70 46 75   OP_Null, 0, pFu
28fd6 6e 63 2d 3e 69 4d 65 6d 29 3b 0a 20 20 20 20 69  nc->iMem);.    i
28fd7 66 28 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69  f( pFunc->iDisti
28fd8 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  nct>=0 ){.      
28fd9 45 78 70 72 20 2a 70 45 20 3d 20 70 46 75 6e 63  Expr *pE = pFunc
28fda 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 61  ->pExpr;.      a
28fdb 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
28fdc 72 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 78  roperty(pE, EP_x
28fdd 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
28fde 20 20 20 69 66 28 20 70 45 2d 3e 78 2e 70 4c 69     if( pE->x.pLi
28fdf 73 74 3d 3d 30 20 7c 7c 20 70 45 2d 3e 78 2e 70  st==0 || pE->x.p
28fe0 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29  List->nExpr!=1 )
28fe1 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
28fe2 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
28fe3 2c 20 22 44 49 53 54 49 4e 43 54 20 61 67 67 72  , "DISTINCT aggr
28fe4 65 67 61 74 65 73 20 6d 75 73 74 20 68 61 76 65  egates must have
28fe5 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 22 0a 20   exactly one ". 
28fe6 20 20 20 20 20 20 20 20 20 20 22 61 72 67 75 6d            "argum
28fe7 65 6e 74 22 29 3b 0a 20 20 20 20 20 20 20 20 70  ent");.        p
28fe8 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 20  Func->iDistinct 
28fe9 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73  = -1;.      }els
28fea 65 7b 0a 20 20 20 20 20 20 20 20 4b 65 79 49 6e  e{.        KeyIn
28feb 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 6b  fo *pKeyInfo = k
28fec 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69  eyInfoFromExprLi
28fed 73 74 28 70 50 61 72 73 65 2c 20 70 45 2d 3e 78  st(pParse, pE->x
28fee 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  .pList);.       
28fef 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28ff0 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p4(v, OP_OpenEph
28ff1 65 6d 65 72 61 6c 2c 20 70 46 75 6e 63 2d 3e 69  emeral, pFunc->i
28ff2 44 69 73 74 69 6e 63 74 2c 20 30 2c 20 30 2c 0a  Distinct, 0, 0,.
28ff3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28ff4 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
28ff5 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45  )pKeyInfo, P4_KE
28ff6 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
28ff7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
28ff8 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  }.}../*.** Invok
28ff9 65 20 74 68 65 20 4f 50 5f 41 67 67 46 69 6e 61  e the OP_AggFina
28ffa 6c 69 7a 65 20 6f 70 63 6f 64 65 20 66 6f 72 20  lize opcode for 
28ffb 65 76 65 72 79 20 61 67 67 72 65 67 61 74 65 20  every aggregate 
28ffc 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74  function.** in t
28ffd 68 65 20 41 67 67 49 6e 66 6f 20 73 74 72 75 63  he AggInfo struc
28ffe 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ture..*/.static 
28fff 76 6f 69 64 20 66 69 6e 61 6c 69 7a 65 41 67 67  void finalizeAgg
29000 46 75 6e 63 74 69 6f 6e 73 28 50 61 72 73 65 20  Functions(Parse 
29001 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f  *pParse, AggInfo
29002 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56   *pAggInfo){.  V
29003 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
29004 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b  >pVdbe;.  int i;
29005 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
29006 6f 5f 66 75 6e 63 20 2a 70 46 3b 0a 20 20 66 6f  o_func *pF;.  fo
29007 72 28 69 3d 30 2c 20 70 46 3d 70 41 67 67 49 6e  r(i=0, pF=pAggIn
29008 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c 70 41 67  fo->aFunc; i<pAg
29009 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b  gInfo->nFunc; i+
2900a 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20 20 20 45 78  +, pF++){.    Ex
2900b 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  prList *pList = 
2900c 70 46 2d 3e 70 45 78 70 72 2d 3e 78 2e 70 4c 69  pF->pExpr->x.pLi
2900d 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  st;.    assert( 
2900e 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2900f 28 70 46 2d 3e 70 45 78 70 72 2c 20 45 50 5f 78  (pF->pExpr, EP_x
29010 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
29011 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29012 70 34 28 76 2c 20 4f 50 5f 41 67 67 46 69 6e 61  p4(v, OP_AggFina
29013 6c 2c 20 70 46 2d 3e 69 4d 65 6d 2c 20 70 4c 69  l, pF->iMem, pLi
29014 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70  st ? pList->nExp
29015 72 20 3a 20 30 2c 20 30 2c 0a 20 20 20 20 20 20  r : 0, 0,.      
29016 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29017 28 76 6f 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63  (void*)pF->pFunc
29018 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20  , P4_FUNCDEF);. 
29019 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61   }.}../*.** Upda
2901a 74 65 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74  te the accumulat
2901b 6f 72 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  or memory cells 
2901c 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65  for an aggregate
2901d 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65   based on.** the
2901e 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20   current cursor 
2901f 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  position..*/.sta
29020 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 65 41  tic void updateA
29021 63 63 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65  ccumulator(Parse
29022 20 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66   *pParse, AggInf
29023 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20  o *pAggInfo){.  
29024 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
29025 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
29026 3b 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e  ;.  struct AggIn
29027 66 6f 5f 66 75 6e 63 20 2a 70 46 3b 0a 20 20 73  fo_func *pF;.  s
29028 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f  truct AggInfo_co
29029 6c 20 2a 70 43 3b 0a 0a 20 20 70 41 67 67 49 6e  l *pC;..  pAggIn
2902a 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d  fo->directMode =
2902b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70   1;.  sqlite3Exp
2902c 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
2902d 73 65 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  se);.  for(i=0, 
2902e 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75  pF=pAggInfo->aFu
2902f 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e  nc; i<pAggInfo->
29030 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b  nFunc; i++, pF++
29031 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b  ){.    int nArg;
29032 0a 20 20 20 20 69 6e 74 20 61 64 64 72 4e 65 78  .    int addrNex
29033 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72  t = 0;.    int r
29034 65 67 41 67 67 3b 0a 20 20 20 20 45 78 70 72 4c  egAgg;.    ExprL
29035 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d  ist *pList = pF-
29036 3e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b  >pExpr->x.pList;
29037 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78  .    assert( !Ex
29038 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 46  prHasProperty(pF
29039 2d 3e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  ->pExpr, EP_xIsS
2903a 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 69 66  elect) );.    if
2903b 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20  ( pList ){.     
2903c 20 6e 41 72 67 20 3d 20 70 4c 69 73 74 2d 3e 6e   nArg = pList->n
2903d 45 78 70 72 3b 0a 20 20 20 20 20 20 72 65 67 41  Expr;.      regA
2903e 67 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  gg = sqlite3GetT
2903f 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
29040 20 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 73 71   nArg);.      sq
29041 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70  lite3ExprCodeExp
29042 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4c  rList(pParse, pL
29043 69 73 74 2c 20 72 65 67 41 67 67 2c 20 30 29 3b  ist, regAgg, 0);
29044 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
29045 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20    nArg = 0;.    
29046 20 20 72 65 67 41 67 67 20 3d 20 30 3b 0a 20 20    regAgg = 0;.  
29047 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e    }.    if( pF->
29048 69 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a  iDistinct>=0 ){.
29049 20 20 20 20 20 20 61 64 64 72 4e 65 78 74 20 3d        addrNext =
2904a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2904b 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
2904c 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 31 20  assert( nArg==1 
2904d 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 44 69 73  );.      codeDis
2904e 74 69 6e 63 74 28 70 50 61 72 73 65 2c 20 70 46  tinct(pParse, pF
2904f 2d 3e 69 44 69 73 74 69 6e 63 74 2c 20 61 64 64  ->iDistinct, add
29050 72 4e 65 78 74 2c 20 31 2c 20 72 65 67 41 67 67  rNext, 1, regAgg
29051 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
29052 20 70 46 2d 3e 70 46 75 6e 63 2d 3e 66 6c 61 67   pF->pFunc->flag
29053 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  s & SQLITE_FUNC_
29054 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20  NEEDCOLL ){.    
29055 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
29056 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 72 75   = 0;.      stru
29057 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
29058 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 69   *pItem;.      i
29059 6e 74 20 6a 3b 0a 20 20 20 20 20 20 61 73 73 65  nt j;.      asse
2905a 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 20  rt( pList!=0 ); 
2905b 20 2f 2a 20 70 4c 69 73 74 21 3d 30 20 69 66 20   /* pList!=0 if 
2905c 70 46 2d 3e 70 46 75 6e 63 20 68 61 73 20 4e 45  pF->pFunc has NE
2905d 45 44 43 4f 4c 4c 20 2a 2f 0a 20 20 20 20 20 20  EDCOLL */.      
2905e 66 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d 70  for(j=0, pItem=p
2905f 4c 69 73 74 2d 3e 61 3b 20 21 70 43 6f 6c 6c 20  List->a; !pColl 
29060 26 26 20 6a 3c 6e 41 72 67 3b 20 6a 2b 2b 2c 20  && j<nArg; j++, 
29061 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
29062 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
29063 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
29064 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70  rse, pItem->pExp
29065 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
29066 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a    if( !pColl ){.
29067 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
29068 70 50 61 72 73 65 2d 3e 64 62 2d 3e 70 44 66 6c  pParse->db->pDfl
29069 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  tColl;.      }. 
2906a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2906b 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp4(v, OP_Col
2906c 6c 53 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28  lSeq, 0, 0, 0, (
2906d 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34  char *)pColl, P4
2906e 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 7d  _COLLSEQ);.    }
2906f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29070 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41 67 67  AddOp4(v, OP_Agg
29071 53 74 65 70 2c 20 30 2c 20 72 65 67 41 67 67 2c  Step, 0, regAgg,
29072 20 70 46 2d 3e 69 4d 65 6d 2c 0a 20 20 20 20 20   pF->iMem,.     
29073 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29074 20 28 76 6f 69 64 2a 29 70 46 2d 3e 70 46 75 6e   (void*)pF->pFun
29075 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a  c, P4_FUNCDEF);.
29076 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29077 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 6e  hangeP5(v, (u8)n
29078 41 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Arg);.    sqlite
29079 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
2907a 65 28 70 50 61 72 73 65 2c 20 72 65 67 41 67 67  e(pParse, regAgg
2907b 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 73 71 6c  , nArg);.    sql
2907c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
2907d 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
2907e 73 65 2c 20 72 65 67 41 67 67 2c 20 6e 41 72 67  se, regAgg, nArg
2907f 29 3b 0a 20 20 20 20 69 66 28 20 61 64 64 72 4e  );.    if( addrN
29080 65 78 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ext ){.      sql
29081 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
29082 61 62 65 6c 28 76 2c 20 61 64 64 72 4e 65 78 74  abel(v, addrNext
29083 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29084 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
29085 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Parse);.    }.  
29086 7d 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 3d  }.  for(i=0, pC=
29087 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20  pAggInfo->aCol; 
29088 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 41 63 63  i<pAggInfo->nAcc
29089 75 6d 75 6c 61 74 6f 72 3b 20 69 2b 2b 2c 20 70  umulator; i++, p
2908a 43 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  C++){.    sqlite
2908b 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
2908c 2c 20 70 43 2d 3e 70 45 78 70 72 2c 20 70 43 2d  , pC->pExpr, pC-
2908d 3e 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 70 41  >iMem);.  }.  pA
2908e 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f  ggInfo->directMo
2908f 64 65 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  de = 0;.  sqlite
29090 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
29091 70 50 61 72 73 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pParse);.}../*.*
29092 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
29093 66 6f 72 20 74 68 65 20 53 45 4c 45 43 54 20 73  for the SELECT s
29094 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 69  tatement given i
29095 6e 20 74 68 65 20 70 20 61 72 67 75 6d 65 6e 74  n the p argument
29096 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  .  .**.** The re
29097 73 75 6c 74 73 20 61 72 65 20 64 69 73 74 72 69  sults are distri
29098 62 75 74 65 64 20 69 6e 20 76 61 72 69 6f 75 73  buted in various
29099 20 77 61 79 73 20 64 65 70 65 6e 64 69 6e 67 20   ways depending 
2909a 6f 6e 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e  on the.** conten
2909b 74 73 20 6f 66 20 74 68 65 20 53 65 6c 65 63 74  ts of the Select
2909c 44 65 73 74 20 73 74 72 75 63 74 75 72 65 20 70  Dest structure p
2909d 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 72 67  ointed to by arg
2909e 75 6d 65 6e 74 20 70 44 65 73 74 0a 2a 2a 20 61  ument pDest.** a
2909f 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
290a0 20 20 20 20 20 70 44 65 73 74 2d 3e 65 44 65 73       pDest->eDes
290a1 74 20 20 20 20 52 65 73 75 6c 74 0a 2a 2a 20 20  t    Result.**  
290a2 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20     ------------ 
290a3 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
290a4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
290a5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
290a6 2a 20 20 20 20 20 53 52 54 5f 4f 75 74 70 75 74  *     SRT_Output
290a7 20 20 20 20 20 20 47 65 6e 65 72 61 74 65 20 61        Generate a
290a8 20 72 6f 77 20 6f 66 20 6f 75 74 70 75 74 20 28   row of output (
290a9 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 52 65 73  using the OP_Res
290aa 75 6c 74 52 6f 77 0a 2a 2a 20 20 20 20 20 20 20  ultRow.**       
290ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
290ac 63 6f 64 65 29 20 66 6f 72 20 65 61 63 68 20 72  code) for each r
290ad 6f 77 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ow in the result
290ae 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20   set..**.**     
290af 53 52 54 5f 4d 65 6d 20 20 20 20 20 20 20 20 20  SRT_Mem         
290b0 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 74 68  Only valid if th
290b1 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 73 69  e result is a si
290b2 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20  ngle column..** 
290b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290b4 20 20 20 20 53 74 6f 72 65 20 74 68 65 20 66 69      Store the fi
290b5 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  rst column of th
290b6 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 72  e first result r
290b7 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ow.**           
290b8 20 20 20 20 20 20 20 20 20 20 69 6e 20 72 65 67            in reg
290b9 69 73 74 65 72 20 70 44 65 73 74 2d 3e 69 50 61  ister pDest->iPa
290ba 72 6d 20 74 68 65 6e 20 61 62 61 6e 64 6f 6e 20  rm then abandon 
290bb 74 68 65 20 72 65 73 74 0a 2a 2a 20 20 20 20 20  the rest.**     
290bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290bd 6f 66 20 74 68 65 20 71 75 65 72 79 2e 20 20 54  of the query.  T
290be 68 69 73 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  his destination 
290bf 69 6d 70 6c 69 65 73 20 22 4c 49 4d 49 54 20 31  implies "LIMIT 1
290c0 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  "..**.**     SRT
290c1 5f 53 65 74 20 20 20 20 20 20 20 20 20 54 68 65  _Set         The
290c2 20 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20   result must be 
290c3 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e  a single column.
290c4 20 20 53 74 6f 72 65 20 65 61 63 68 0a 2a 2a 20    Store each.** 
290c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290c6 20 20 20 20 72 6f 77 20 6f 66 20 72 65 73 75 6c      row of resul
290c7 74 20 61 73 20 74 68 65 20 6b 65 79 20 69 6e 20  t as the key in 
290c8 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61  table pDest->iPa
290c9 72 6d 2e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  rm. .**         
290ca 20 20 20 20 20 20 20 20 20 20 20 20 41 70 70 6c              Appl
290cb 79 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 70  y the affinity p
290cc 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 20 62  Dest->affinity b
290cd 65 66 6f 72 65 20 73 74 6f 72 69 6e 67 0a 2a 2a  efore storing.**
290ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290cf 20 20 20 20 20 72 65 73 75 6c 74 73 2e 20 20 55       results.  U
290d0 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
290d1 20 22 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e   "IN (SELECT ...
290d2 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  )"..**.**     SR
290d3 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 53 74  T_Union       St
290d4 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 61  ore results as a
290d5 20 6b 65 79 20 69 6e 20 61 20 74 65 6d 70 6f 72   key in a tempor
290d6 61 72 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d  ary table pDest-
290d7 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20  >iParm..**.**   
290d8 20 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 20    SRT_Except    
290d9 20 20 52 65 6d 6f 76 65 20 72 65 73 75 6c 74 73    Remove results
290da 20 66 72 6f 6d 20 74 68 65 20 74 65 6d 70 6f 72   from the tempor
290db 61 72 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d  ary table pDest-
290dc 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20  >iParm..**.**   
290dd 20 20 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20    SRT_Table     
290de 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20    Store results 
290df 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  in temporary tab
290e0 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e  le pDest->iParm.
290e1 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
290e2 20 20 20 20 20 20 20 20 54 68 69 73 20 69 73 20          This is 
290e3 6c 69 6b 65 20 53 52 54 5f 45 70 68 65 6d 54 61  like SRT_EphemTa
290e4 62 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68  b except that th
290e5 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20  e table.**      
290e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
290e7 73 20 61 73 73 75 6d 65 64 20 74 6f 20 61 6c 72  s assumed to alr
290e8 65 61 64 79 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a  eady be open..**
290e9 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45 70 68 65  .**     SRT_Ephe
290ea 6d 54 61 62 20 20 20 20 43 72 65 61 74 65 20 61  mTab    Create a
290eb 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  n temporary tabl
290ec 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 61  e pDest->iParm a
290ed 6e 64 20 73 74 6f 72 65 0a 2a 2a 20 20 20 20 20  nd store.**     
290ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290ef 74 68 65 20 72 65 73 75 6c 74 20 74 68 65 72 65  the result there
290f0 2e 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20  . The cursor is 
290f1 6c 65 66 74 20 6f 70 65 6e 20 61 66 74 65 72 0a  left open after.
290f2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
290f3 20 20 20 20 20 20 20 72 65 74 75 72 6e 69 6e 67         returning
290f4 2e 20 20 54 68 69 73 20 69 73 20 6c 69 6b 65 20  .  This is like 
290f5 53 52 54 5f 54 61 62 6c 65 20 65 78 63 65 70 74  SRT_Table except
290f6 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
290f7 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 69               thi
290f8 73 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 75 73  s destination us
290f9 65 73 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  es OP_OpenEpheme
290fa 72 61 6c 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a  ral to create.**
290fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290fc 20 20 20 20 20 74 68 65 20 74 61 62 6c 65 20 66       the table f
290fd 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  irst..**.**     
290fe 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20  SRT_Coroutine   
290ff 47 65 6e 65 72 61 74 65 20 61 20 63 6f 2d 72 6f  Generate a co-ro
29100 75 74 69 6e 65 20 74 68 61 74 20 72 65 74 75 72  utine that retur
29101 6e 73 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 0a  ns a new row of.
29102 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
29103 20 20 20 20 20 20 20 72 65 73 75 6c 74 73 20 65         results e
29104 61 63 68 20 74 69 6d 65 20 69 74 20 69 73 20 69  ach time it is i
29105 6e 76 6f 6b 65 64 2e 20 20 54 68 65 20 65 6e 74  nvoked.  The ent
29106 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20  ry point.**     
29107 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29108 6f 66 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e  of the co-routin
29109 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  e is stored in r
2910a 65 67 69 73 74 65 72 20 70 44 65 73 74 2d 3e 69  egister pDest->i
2910b 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  Parm..**.**     
2910c 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20 20  SRT_Exists      
2910d 53 74 6f 72 65 20 61 20 31 20 69 6e 20 6d 65 6d  Store a 1 in mem
2910e 6f 72 79 20 63 65 6c 6c 20 70 44 65 73 74 2d 3e  ory cell pDest->
2910f 69 50 61 72 6d 20 69 66 20 74 68 65 20 72 65 73  iParm if the res
29110 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ult.**          
29111 20 20 20 20 20 20 20 20 20 20 20 73 65 74 20 69             set i
29112 73 20 6e 6f 74 20 65 6d 70 74 79 2e 0a 2a 2a 0a  s not empty..**.
29113 2a 2a 20 20 20 20 20 53 52 54 5f 44 69 73 63 61  **     SRT_Disca
29114 72 64 20 20 20 20 20 54 68 72 6f 77 20 74 68 65  rd     Throw the
29115 20 72 65 73 75 6c 74 73 20 61 77 61 79 2e 20 20   results away.  
29116 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
29117 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
29118 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
29119 61 74 65 6d 65 6e 74 73 20 77 69 74 68 69 6e 20  atements within 
2911a 74 72 69 67 67 65 72 73 20 77 68 6f 73 65 20 6f  triggers whose o
2911b 6e 6c 79 20 70 75 72 70 6f 73 65 20 69 73 0a 2a  nly purpose is.*
2911c 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2911d 20 20 20 20 20 20 74 68 65 20 73 69 64 65 2d 65        the side-e
2911e 66 66 65 63 74 73 20 6f 66 20 66 75 6e 63 74 69  ffects of functi
2911f 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ons..**.** This 
29120 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
29121 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  the number of er
29122 72 6f 72 73 2e 20 20 49 66 20 61 6e 79 20 65 72  rors.  If any er
29123 72 6f 72 73 20 61 72 65 0a 2a 2a 20 65 6e 63 6f  rors are.** enco
29124 75 6e 74 65 72 65 64 2c 20 74 68 65 6e 20 61 6e  untered, then an
29125 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72   appropriate err
29126 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c 65  or message is le
29127 66 74 20 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2d  ft in.** pParse-
29128 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20  >zErrMsg..**.** 
29129 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
2912a 73 20 4e 4f 54 20 66 72 65 65 20 74 68 65 20 53  s NOT free the S
2912b 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20  elect structure 
2912c 70 61 73 73 65 64 20 69 6e 2e 20 20 54 68 65 0a  passed in.  The.
2912d 2a 2a 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  ** calling funct
2912e 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 64 6f 20  ion needs to do 
2912f 74 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  that..*/.SQLITE_
29130 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
29131 74 65 33 53 65 6c 65 63 74 28 0a 20 20 50 61 72  te3Select(.  Par
29132 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
29133 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
29134 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
29135 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
29136 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
29137 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65  ECT statement be
29138 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20  ing coded. */.  
29139 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
2913a 74 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74  t      /* What t
2913b 6f 20 64 6f 20 77 69 74 68 20 74 68 65 20 71 75  o do with the qu
2913c 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29  ery results */.)
2913d 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  {.  int i, j;   
2913e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2913f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
29140 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e   WhereInfo *pWIn
29141 66 6f 3b 20 20 20 20 20 2f 2a 20 52 65 74 75 72  fo;     /* Retur
29142 6e 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 57 68  n from sqlite3Wh
29143 65 72 65 42 65 67 69 6e 28 29 20 2a 2f 0a 20 20  ereBegin() */.  
29144 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
29145 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
29146 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e  rtual machine un
29147 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
29148 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41 67 67 3b   */.  int isAgg;
29149 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2914a 54 72 75 65 20 66 6f 72 20 73 65 6c 65 63 74 20  True for select 
2914b 6c 69 73 74 73 20 6c 69 6b 65 20 22 63 6f 75 6e  lists like "coun
2914c 74 28 2a 29 22 20 2a 2f 0a 20 20 45 78 70 72 4c  t(*)" */.  ExprL
2914d 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20  ist *pEList;    
2914e 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c    /* List of col
2914f 75 6d 6e 73 20 74 6f 20 65 78 74 72 61 63 74 2e  umns to extract.
29150 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
29151 54 61 62 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20  TabList;     /* 
29152 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 74  List of tables t
29153 6f 20 73 65 6c 65 63 74 20 66 72 6f 6d 20 2a 2f  o select from */
29154 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b  .  Expr *pWhere;
29155 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
29156 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
29157 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
29158 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
29159 72 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f  rBy;    /* The O
2915a 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20  RDER BY clause. 
2915b 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
2915c 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f    ExprList *pGro
2915d 75 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20  upBy;    /* The 
2915e 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e  GROUP BY clause.
2915f 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f    May be NULL */
29160 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67  .  Expr *pHaving
29161 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
29162 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 2e 20   HAVING clause. 
29163 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
29164 20 20 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74    int isDistinct
29165 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
29166 20 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54   if the DISTINCT
29167 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
29168 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73  ent */.  int dis
29169 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 20 20  tinct;          
2916a 2f 2a 20 54 61 62 6c 65 20 74 6f 20 75 73 65 20  /* Table to use 
2916b 66 6f 72 20 74 68 65 20 64 69 73 74 69 6e 63 74  for the distinct
2916c 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63   set */.  int rc
2916d 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20   = 1;           
2916e 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74   /* Value to ret
2916f 75 72 6e 20 66 72 6f 6d 20 74 68 69 73 20 66 75  urn from this fu
29170 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nction */.  int 
29171 61 64 64 72 53 6f 72 74 49 6e 64 65 78 3b 20 20  addrSortIndex;  
29172 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
29173 20 61 6e 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d   an OP_OpenEphem
29174 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  eral instruction
29175 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 73 41   */.  AggInfo sA
29176 67 67 49 6e 66 6f 3b 20 20 20 20 20 20 2f 2a 20  ggInfo;      /* 
29177 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64  Information used
29178 20 62 79 20 61 67 67 72 65 67 61 74 65 20 71 75   by aggregate qu
29179 65 72 69 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69  eries */.  int i
2917a 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  End;            
2917b 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
2917c 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 71  the end of the q
2917d 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65  uery */.  sqlite
2917e 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
2917f 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
29180 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
29181 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
29182 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c  b;.  if( p==0 ||
29183 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
29184 64 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72  d || pParse->nEr
29185 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
29186 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  1;.  }.  if( sql
29187 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
29188 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 53 45 4c  arse, SQLITE_SEL
29189 45 43 54 2c 20 30 2c 20 30 2c 20 30 29 20 29 20  ECT, 0, 0, 0) ) 
2918a 72 65 74 75 72 6e 20 31 3b 0a 20 20 6d 65 6d 73  return 1;.  mems
2918b 65 74 28 26 73 41 67 67 49 6e 66 6f 2c 20 30 2c  et(&sAggInfo, 0,
2918c 20 73 69 7a 65 6f 66 28 73 41 67 67 49 6e 66 6f   sizeof(sAggInfo
2918d 29 29 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20  ));..  pOrderBy 
2918e 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20  = p->pOrderBy;. 
2918f 20 69 66 28 20 49 67 6e 6f 72 61 62 6c 65 4f 72   if( IgnorableOr
29190 64 65 72 62 79 28 70 44 65 73 74 29 20 29 7b 0a  derby(pDest) ){.
29191 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20      p->pOrderBy 
29192 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 20  = 0;..    /* In 
29193 74 68 65 73 65 20 63 61 73 65 73 20 74 68 65 20  these cases the 
29194 44 49 53 54 49 4e 43 54 20 6f 70 65 72 61 74 6f  DISTINCT operato
29195 72 20 6d 61 6b 65 73 20 6e 6f 20 64 69 66 66 65  r makes no diffe
29196 72 65 6e 63 65 20 74 6f 20 74 68 65 0a 20 20 20  rence to the.   
29197 20 2a 2a 20 72 65 73 75 6c 74 73 2c 20 73 6f 20   ** results, so 
29198 72 65 6d 6f 76 65 20 69 74 20 69 66 20 69 74 20  remove it if it 
29199 77 65 72 65 20 73 70 65 63 69 66 69 65 64 2e 0a  were specified..
2919a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
2919b 74 28 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d  t(pDest->eDest==
2919c 53 52 54 5f 45 78 69 73 74 73 20 7c 7c 20 70 44  SRT_Exists || pD
2919d 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2919e 55 6e 69 6f 6e 20 7c 7c 20 0a 20 20 20 20 20 20  Union || .      
2919f 20 20 20 20 20 70 44 65 73 74 2d 3e 65 44 65 73       pDest->eDes
291a0 74 3d 3d 53 52 54 5f 45 78 63 65 70 74 20 7c 7c  t==SRT_Except ||
291a1 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
291a2 52 54 5f 44 69 73 63 61 72 64 29 3b 0a 20 20 20  RT_Discard);.   
291a3 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20   p->selFlags &= 
291a4 7e 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20  ~SF_Distinct;.  
291a5 7d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  }.  sqlite3Selec
291a6 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c  tPrep(pParse, p,
291a7 20 30 29 3b 0a 20 20 70 54 61 62 4c 69 73 74 20   0);.  pTabList 
291a8 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70 45 4c  = p->pSrc;.  pEL
291a9 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b  ist = p->pEList;
291aa 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
291ab 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
291ac 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
291ad 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
291ae 20 20 7d 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42    }.  p->pOrderB
291af 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20  y = pOrderBy;.  
291b0 69 73 41 67 67 20 3d 20 28 70 2d 3e 73 65 6c 46  isAgg = (p->selF
291b1 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 67  lags & SF_Aggreg
291b2 61 74 65 29 21 3d 30 3b 0a 20 20 69 66 28 20 70  ate)!=0;.  if( p
291b3 45 4c 69 73 74 3d 3d 30 20 29 20 67 6f 74 6f 20  EList==0 ) goto 
291b4 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f  select_end;..  /
291b5 2a 20 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 65  * .  ** Do not e
291b6 76 65 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 67  ven attempt to g
291b7 65 6e 65 72 61 74 65 20 61 6e 79 20 63 6f 64 65  enerate any code
291b8 20 69 66 20 77 65 20 68 61 76 65 20 61 6c 72 65   if we have alre
291b9 61 64 79 20 73 65 65 6e 0a 20 20 2a 2a 20 65 72  ady seen.  ** er
291ba 72 6f 72 73 20 62 65 66 6f 72 65 20 74 68 69 73  rors before this
291bb 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73 2e   routine starts.
291bc 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72  .  */.  if( pPar
291bd 73 65 2d 3e 6e 45 72 72 3e 30 20 29 20 67 6f 74  se->nErr>0 ) got
291be 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20  o select_end;.. 
291bf 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 69 73 20   /* ORDER BY is 
291c0 69 67 6e 6f 72 65 64 20 66 6f 72 20 73 6f 6d 65  ignored for some
291c1 20 64 65 73 74 69 6e 61 74 69 6f 6e 73 2e 0a 20   destinations.. 
291c2 20 2a 2f 0a 20 20 69 66 28 20 49 67 6e 6f 72 61   */.  if( Ignora
291c3 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73 74  bleOrderby(pDest
291c4 29 20 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42  ) ){.    pOrderB
291c5 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  y = 0;.  }..  /*
291c6 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e   Begin generatin
291c7 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76  g code..  */.  v
291c8 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
291c9 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
291ca 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c   v==0 ) goto sel
291cb 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 47  ect_end;..  /* G
291cc 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
291cd 20 61 6c 6c 20 73 75 62 2d 71 75 65 72 69 65 73   all sub-queries
291ce 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
291cf 75 73 65 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65  use.  */.#if !de
291d0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
291d1 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21  T_SUBQUERY) || !
291d2 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
291d3 4d 49 54 5f 56 49 45 57 29 0a 20 20 66 6f 72 28  MIT_VIEW).  for(
291d4 69 3d 30 3b 20 21 70 2d 3e 70 50 72 69 6f 72 20  i=0; !p->pPrior 
291d5 26 26 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  && i<pTabList->n
291d6 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  Src; i++){.    s
291d7 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
291d8 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54 61  em *pItem = &pTa
291d9 62 4c 69 73 74 2d 3e 61 5b 69 5d 3b 0a 20 20 20  bList->a[i];.   
291da 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
291db 3b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53  ;.    Select *pS
291dc 75 62 20 3d 20 70 49 74 65 6d 2d 3e 70 53 65 6c  ub = pItem->pSel
291dd 65 63 74 3b 0a 20 20 20 20 69 6e 74 20 69 73 41  ect;.    int isA
291de 67 67 53 75 62 3b 0a 0a 20 20 20 20 69 66 28 20  ggSub;..    if( 
291df 70 53 75 62 3d 3d 30 20 7c 7c 20 70 49 74 65 6d  pSub==0 || pItem
291e0 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 29 20  ->isPopulated ) 
291e1 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 2f  continue;..    /
291e2 2a 20 49 6e 63 72 65 6d 65 6e 74 20 50 61 72 73  * Increment Pars
291e3 65 2e 6e 48 65 69 67 68 74 20 62 79 20 74 68 65  e.nHeight by the
291e4 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 20 6c   height of the l
291e5 61 72 67 65 73 74 20 65 78 70 72 65 73 73 69 6f  argest expressio
291e6 6e 0a 20 20 20 20 2a 2a 20 74 72 65 65 20 72 65  n.    ** tree re
291e7 66 65 72 65 64 20 74 6f 20 62 79 20 74 68 69 73  fered to by this
291e8 2c 20 74 68 65 20 70 61 72 65 6e 74 20 73 65 6c  , the parent sel
291e9 65 63 74 2e 20 54 68 65 20 63 68 69 6c 64 20 73  ect. The child s
291ea 65 6c 65 63 74 0a 20 20 20 20 2a 2a 20 6d 61 79  elect.    ** may
291eb 20 63 6f 6e 74 61 69 6e 20 65 78 70 72 65 73 73   contain express
291ec 69 6f 6e 20 74 72 65 65 73 20 6f 66 20 61 74 20  ion trees of at 
291ed 6d 6f 73 74 0a 20 20 20 20 2a 2a 20 28 53 51 4c  most.    ** (SQL
291ee 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
291ef 54 48 2d 50 61 72 73 65 2e 6e 48 65 69 67 68 74  TH-Parse.nHeight
291f0 29 20 68 65 69 67 68 74 2e 20 54 68 69 73 20 69  ) height. This i
291f1 73 20 61 20 62 69 74 0a 20 20 20 20 2a 2a 20 6d  s a bit.    ** m
291f2 6f 72 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65  ore conservative
291f3 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2c   than necessary,
291f4 20 62 75 74 20 6d 75 63 68 20 65 61 73 69 65 72   but much easier
291f5 20 74 68 61 6e 20 65 6e 66 6f 72 63 69 6e 67 0a   than enforcing.
291f6 20 20 20 20 2a 2a 20 61 6e 20 65 78 61 63 74 20      ** an exact 
291f7 6c 69 6d 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  limit..    */.  
291f8 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68    pParse->nHeigh
291f9 74 20 2b 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  t += sqlite3Sele
291fa 63 74 45 78 70 72 48 65 69 67 68 74 28 70 29 3b  ctExprHeight(p);
291fb 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74  ..    /* Check t
291fc 6f 20 73 65 65 20 69 66 20 74 68 65 20 73 75 62  o see if the sub
291fd 71 75 65 72 79 20 63 61 6e 20 62 65 20 61 62 73  query can be abs
291fe 6f 72 62 65 64 20 69 6e 74 6f 20 74 68 65 20 70  orbed into the p
291ff 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 73  arent. */.    is
29200 41 67 67 53 75 62 20 3d 20 28 70 53 75 62 2d 3e  AggSub = (pSub->
29201 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67  selFlags & SF_Ag
29202 67 72 65 67 61 74 65 29 21 3d 30 3b 0a 20 20 20  gregate)!=0;.   
29203 20 69 66 28 20 66 6c 61 74 74 65 6e 53 75 62 71   if( flattenSubq
29204 75 65 72 79 28 70 50 61 72 73 65 2c 20 70 2c 20  uery(pParse, p, 
29205 69 2c 20 69 73 41 67 67 2c 20 69 73 41 67 67 53  i, isAgg, isAggS
29206 75 62 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ub) ){.      if(
29207 20 69 73 41 67 67 53 75 62 20 29 7b 0a 20 20 20   isAggSub ){.   
29208 20 20 20 20 20 69 73 41 67 67 20 3d 20 31 3b 0a       isAgg = 1;.
29209 20 20 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 6c          p->selFl
2920a 61 67 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 67  ags |= SF_Aggreg
2920b 61 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ate;.      }.   
2920c 20 20 20 69 20 3d 20 2d 31 3b 0a 20 20 20 20 7d     i = -1;.    }
2920d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
2920e 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
2920f 74 28 26 64 65 73 74 2c 20 53 52 54 5f 45 70 68  t(&dest, SRT_Eph
29210 65 6d 54 61 62 2c 20 70 49 74 65 6d 2d 3e 69 43  emTab, pItem->iC
29211 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 61 73  ursor);.      as
29212 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 73 50  sert( pItem->isP
29213 6f 70 75 6c 61 74 65 64 3d 3d 30 20 29 3b 0a 20  opulated==0 );. 
29214 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
29215 63 74 28 70 50 61 72 73 65 2c 20 70 53 75 62 2c  ct(pParse, pSub,
29216 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70   &dest);.      p
29217 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65  Item->isPopulate
29218 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  d = 1;.    }.   
29219 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
2921a 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
2921b 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 67  ailed ){.      g
2921c 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
2921d 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65      }.    pParse
2921e 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 73 71 6c  ->nHeight -= sql
2921f 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65  ite3SelectExprHe
29220 69 67 68 74 28 70 29 3b 0a 20 20 20 20 70 54 61  ight(p);.    pTa
29221 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b  bList = p->pSrc;
29222 0a 20 20 20 20 69 66 28 20 21 49 67 6e 6f 72 61  .    if( !Ignora
29223 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73 74  bleOrderby(pDest
29224 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 72 64 65  ) ){.      pOrde
29225 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42  rBy = p->pOrderB
29226 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  y;.    }.  }.  p
29227 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
29228 74 3b 0a 23 65 6e 64 69 66 0a 20 20 70 57 68 65  t;.#endif.  pWhe
29229 72 65 20 3d 20 70 2d 3e 70 57 68 65 72 65 3b 0a  re = p->pWhere;.
2922a 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e    pGroupBy = p->
2922b 70 47 72 6f 75 70 42 79 3b 0a 20 20 70 48 61 76  pGroupBy;.  pHav
2922c 69 6e 67 20 3d 20 70 2d 3e 70 48 61 76 69 6e 67  ing = p->pHaving
2922d 3b 0a 20 20 69 73 44 69 73 74 69 6e 63 74 20 3d  ;.  isDistinct =
2922e 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
2922f 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30 3b  SF_Distinct)!=0;
29230 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
29231 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
29232 45 4c 45 43 54 0a 20 20 2f 2a 20 49 66 20 74 68  ELECT.  /* If th
29233 65 72 65 20 69 73 20 61 72 65 20 61 20 73 65 71  ere is are a seq
29234 75 65 6e 63 65 20 6f 66 20 71 75 65 72 69 65 73  uence of queries
29235 2c 20 64 6f 20 74 68 65 20 65 61 72 6c 69 65 72  , do the earlier
29236 20 6f 6e 65 73 20 66 69 72 73 74 2e 0a 20 20 2a   ones first..  *
29237 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f  /.  if( p->pPrio
29238 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  r ){.    if( p->
29239 70 52 69 67 68 74 6d 6f 73 74 3d 3d 30 20 29 7b  pRightmost==0 ){
2923a 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
2923b 4c 6f 6f 70 2c 20 2a 70 52 69 67 68 74 20 3d 20  Loop, *pRight = 
2923c 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74  0;.      int cnt
2923d 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20   = 0;.      int 
2923e 6d 78 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20  mxSelect;.      
2923f 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20 70 4c 6f  for(pLoop=p; pLo
29240 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d  op; pLoop=pLoop-
29241 3e 70 50 72 69 6f 72 2c 20 63 6e 74 2b 2b 29 7b  >pPrior, cnt++){
29242 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e  .        pLoop->
29243 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 70 3b 0a  pRightmost = p;.
29244 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 70          pLoop->p
29245 4e 65 78 74 20 3d 20 70 52 69 67 68 74 3b 0a 20  Next = pRight;. 
29246 20 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20         pRight = 
29247 70 4c 6f 6f 70 3b 0a 20 20 20 20 20 20 7d 0a 20  pLoop;.      }. 
29248 20 20 20 20 20 6d 78 53 65 6c 65 63 74 20 3d 20       mxSelect = 
29249 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
2924a 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  E_LIMIT_COMPOUND
2924b 5f 53 45 4c 45 43 54 5d 3b 0a 20 20 20 20 20 20  _SELECT];.      
2924c 69 66 28 20 6d 78 53 65 6c 65 63 74 20 26 26 20  if( mxSelect && 
2924d 63 6e 74 3e 6d 78 53 65 6c 65 63 74 20 29 7b 0a  cnt>mxSelect ){.
2924e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2924f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
29250 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20  "too many terms 
29251 69 6e 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45  in compound SELE
29252 43 54 22 29 3b 0a 20 20 20 20 20 20 20 20 72 65  CT");.        re
29253 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
29254 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
29255 20 6d 75 6c 74 69 53 65 6c 65 63 74 28 70 50 61   multiSelect(pPa
29256 72 73 65 2c 20 70 2c 20 70 44 65 73 74 29 3b 0a  rse, p, pDest);.
29257 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
29258 20 49 66 20 77 72 69 74 69 6e 67 20 74 6f 20 6d   If writing to m
29259 65 6d 6f 72 79 20 6f 72 20 67 65 6e 65 72 61 74  emory or generat
2925a 69 6e 67 20 61 20 73 65 74 0a 20 20 2a 2a 20 6f  ing a set.  ** o
2925b 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c  nly a single col
2925c 75 6d 6e 20 6d 61 79 20 62 65 20 6f 75 74 70 75  umn may be outpu
2925d 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  t..  */.#ifndef 
2925e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2925f 55 45 52 59 0a 20 20 69 66 28 20 63 68 65 63 6b  UERY.  if( check
29260 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65  ForMultiColumnSe
29261 6c 65 63 74 45 72 72 6f 72 28 70 50 61 72 73 65  lectError(pParse
29262 2c 20 70 44 65 73 74 2c 20 70 45 4c 69 73 74 2d  , pDest, pEList-
29263 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 67  >nExpr) ){.    g
29264 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
29265 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
29266 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 72 65   If possible, re
29267 77 72 69 74 65 20 74 68 65 20 71 75 65 72 79 20  write the query 
29268 74 6f 20 75 73 65 20 47 52 4f 55 50 20 42 59 20  to use GROUP BY 
29269 69 6e 73 74 65 61 64 20 6f 66 20 44 49 53 54 49  instead of DISTI
2926a 4e 43 54 2e 0a 20 20 2a 2a 20 47 52 4f 55 50 20  NCT..  ** GROUP 
2926b 42 59 20 6d 69 67 68 74 20 75 73 65 20 61 6e 20  BY might use an 
2926c 69 6e 64 65 78 2c 20 44 49 53 54 49 4e 43 54 20  index, DISTINCT 
2926d 6e 65 76 65 72 20 64 6f 65 73 2e 0a 20 20 2a 2f  never does..  */
2926e 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c  .  if( (p->selFl
2926f 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e  ags & (SF_Distin
29270 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29  ct|SF_Aggregate)
29271 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74 20 26  )==SF_Distinct &
29272 26 20 21 70 2d 3e 70 47 72 6f 75 70 42 79 20 29  & !p->pGroupBy )
29273 7b 0a 20 20 20 20 70 2d 3e 70 47 72 6f 75 70 42  {.    p->pGroupB
29274 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  y = sqlite3ExprL
29275 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45  istDup(db, p->pE
29276 4c 69 73 74 2c 20 30 29 3b 0a 20 20 20 20 70 47  List, 0);.    pG
29277 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f  roupBy = p->pGro
29278 75 70 42 79 3b 0a 20 20 20 20 70 2d 3e 73 65 6c  upBy;.    p->sel
29279 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44 69 73  Flags &= ~SF_Dis
2927a 74 69 6e 63 74 3b 0a 20 20 20 20 69 73 44 69 73  tinct;.    isDis
2927b 74 69 6e 63 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a  tinct = 0;.  }..
2927c 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
2927d 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
2927e 75 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 73  use, then this s
2927f 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65  orting.  ** inde
29280 78 20 6d 69 67 68 74 20 65 6e 64 20 75 70 20 62  x might end up b
29281 65 69 6e 67 20 75 6e 75 73 65 64 20 69 66 20 74  eing unused if t
29282 68 65 20 64 61 74 61 20 63 61 6e 20 62 65 20 0a  he data can be .
29283 20 20 2a 2a 20 65 78 74 72 61 63 74 65 64 20 69    ** extracted i
29284 6e 20 70 72 65 2d 73 6f 72 74 65 64 20 6f 72 64  n pre-sorted ord
29285 65 72 2e 20 20 49 66 20 74 68 61 74 20 69 73 20  er.  If that is 
29286 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74  the case, then t
29287 68 65 0a 20 20 2a 2a 20 4f 50 5f 4f 70 65 6e 45  he.  ** OP_OpenE
29288 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63  phemeral instruc
29289 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 68 61  tion will be cha
2928a 6e 67 65 64 20 74 6f 20 61 6e 20 4f 50 5f 4e 6f  nged to an OP_No
2928b 6f 70 20 6f 6e 63 65 0a 20 20 2a 2a 20 77 65 20  op once.  ** we 
2928c 66 69 67 75 72 65 20 6f 75 74 20 74 68 61 74 20  figure out that 
2928d 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  the sorting inde
2928e 78 20 69 73 20 6e 6f 74 20 6e 65 65 64 65 64 2e  x is not needed.
2928f 20 20 54 68 65 20 61 64 64 72 53 6f 72 74 49 6e    The addrSortIn
29290 64 65 78 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c  dex.  ** variabl
29291 65 20 69 73 20 75 73 65 64 20 74 6f 20 66 61 63  e is used to fac
29292 69 6c 69 74 61 74 65 20 74 68 61 74 20 63 68 61  ilitate that cha
29293 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  nge..  */.  if( 
29294 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
29295 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
29296 6f 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20  o;.    pKeyInfo 
29297 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70  = keyInfoFromExp
29298 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4f  rList(pParse, pO
29299 72 64 65 72 42 79 29 3b 0a 20 20 20 20 70 4f 72  rderBy);.    pOr
2929a 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 20  derBy->iECursor 
2929b 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2929c 3b 0a 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65  ;.    p->addrOpe
2929d 6e 45 70 68 6d 5b 32 5d 20 3d 20 61 64 64 72 53  nEphm[2] = addrS
2929e 6f 72 74 49 6e 64 65 78 20 3d 0a 20 20 20 20 20  ortIndex =.     
2929f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
292a0 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p4(v, OP_OpenEph
292a1 65 6d 65 72 61 6c 2c 0a 20 20 20 20 20 20 20 20  emeral,.        
292a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292a3 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43     pOrderBy->iEC
292a4 75 72 73 6f 72 2c 20 70 4f 72 64 65 72 42 79 2d  ursor, pOrderBy-
292a5 3e 6e 45 78 70 72 2b 32 2c 20 30 2c 0a 20 20 20  >nExpr+2, 0,.   
292a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292a7 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
292a8 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49  KeyInfo, P4_KEYI
292a9 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
292aa 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 53  }else{.    addrS
292ab 6f 72 74 49 6e 64 65 78 20 3d 20 2d 31 3b 0a 20  ortIndex = -1;. 
292ac 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
292ad 6f 75 74 70 75 74 20 69 73 20 64 65 73 74 69 6e  output is destin
292ae 65 64 20 66 6f 72 20 61 20 74 65 6d 70 6f 72 61  ed for a tempora
292af 72 79 20 74 61 62 6c 65 2c 20 6f 70 65 6e 20 74  ry table, open t
292b0 68 61 74 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  hat table..  */.
292b1 20 20 69 66 28 20 70 44 65 73 74 2d 3e 65 44 65    if( pDest->eDe
292b2 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62  st==SRT_EphemTab
292b3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
292b4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
292b5 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70  OpenEphemeral, p
292b6 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 70 45 4c  Dest->iParm, pEL
292b7 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 7d  ist->nExpr);.  }
292b8 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6c  ..  /* Set the l
292b9 69 6d 69 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 69  imiter..  */.  i
292ba 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62  End = sqlite3Vdb
292bb 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
292bc 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
292bd 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70  isters(pParse, p
292be 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20 4f  , iEnd);..  /* O
292bf 70 65 6e 20 61 20 76 69 72 74 75 61 6c 20 69 6e  pen a virtual in
292c0 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20 74  dex to use for t
292c1 68 65 20 64 69 73 74 69 6e 63 74 20 73 65 74 2e  he distinct set.
292c2 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 44 69  .  */.  if( isDi
292c3 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 4b 65  stinct ){.    Ke
292c4 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
292c5 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 41  .    assert( isA
292c6 67 67 20 7c 7c 20 70 47 72 6f 75 70 42 79 20 29  gg || pGroupBy )
292c7 3b 0a 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d  ;.    distinct =
292c8 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
292c9 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20  .    pKeyInfo = 
292ca 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c  keyInfoFromExprL
292cb 69 73 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 70  ist(pParse, p->p
292cc 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  EList);.    sqli
292cd 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
292ce 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
292cf 6c 2c 20 64 69 73 74 69 6e 63 74 2c 20 30 2c 20  l, distinct, 0, 
292d0 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
292d1 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
292d2 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b  *)pKeyInfo, P4_K
292d3 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
292d4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 69  .  }else{.    di
292d5 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 7d  stinct = -1;.  }
292d6 0a 0a 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65  ..  /* Aggregate
292d7 20 61 6e 64 20 6e 6f 6e 2d 61 67 67 72 65 67 61   and non-aggrega
292d8 74 65 20 71 75 65 72 69 65 73 20 61 72 65 20 68  te queries are h
292d9 61 6e 64 6c 65 64 20 64 69 66 66 65 72 65 6e 74  andled different
292da 6c 79 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 41  ly */.  if( !isA
292db 67 67 20 26 26 20 70 47 72 6f 75 70 42 79 3d 3d  gg && pGroupBy==
292dc 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  0 ){.    /* This
292dd 20 63 61 73 65 20 69 73 20 66 6f 72 20 6e 6f 6e   case is for non
292de 2d 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69  -aggregate queri
292df 65 73 0a 20 20 20 20 2a 2a 20 42 65 67 69 6e 20  es.    ** Begin 
292e0 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61  the database sca
292e1 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 57 49  n.    */.    pWI
292e2 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65  nfo = sqlite3Whe
292e3 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20  reBegin(pParse, 
292e4 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65  pTabList, pWhere
292e5 2c 20 26 70 4f 72 64 65 72 42 79 2c 20 30 29 3b  , &pOrderBy, 0);
292e6 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d  .    if( pWInfo=
292e7 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74  =0 ) goto select
292e8 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  _end;..    /* If
292e9 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74   sorting index t
292ea 68 61 74 20 77 61 73 20 63 72 65 61 74 65 64 20  hat was created 
292eb 62 79 20 61 20 70 72 69 6f 72 20 4f 50 5f 4f 70  by a prior OP_Op
292ec 65 6e 45 70 68 65 6d 65 72 61 6c 20 0a 20 20 20  enEphemeral .   
292ed 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   ** instruction 
292ee 65 6e 64 65 64 20 75 70 20 6e 6f 74 20 62 65 69  ended up not bei
292ef 6e 67 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20  ng needed, then 
292f0 63 68 61 6e 67 65 20 74 68 65 20 4f 50 5f 4f 70  change the OP_Op
292f1 65 6e 45 70 68 65 6d 65 72 61 6c 0a 20 20 20 20  enEphemeral.    
292f2 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f  ** into an OP_No
292f3 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  op..    */.    i
292f4 66 28 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78  f( addrSortIndex
292f5 3e 3d 30 20 26 26 20 70 4f 72 64 65 72 42 79 3d  >=0 && pOrderBy=
292f6 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
292f7 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e  te3VdbeChangeToN
292f8 6f 6f 70 28 76 2c 20 61 64 64 72 53 6f 72 74 49  oop(v, addrSortI
292f9 6e 64 65 78 2c 20 31 29 3b 0a 20 20 20 20 20 20  ndex, 1);.      
292fa 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
292fb 32 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a  2] = -1;.    }..
292fc 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 73      /* Use the s
292fd 74 61 6e 64 61 72 64 20 69 6e 6e 65 72 20 6c 6f  tandard inner lo
292fe 6f 70 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  op.    */.    as
292ff 73 65 72 74 28 21 69 73 44 69 73 74 69 6e 63 74  sert(!isDistinct
29300 29 3b 0a 20 20 20 20 73 65 6c 65 63 74 49 6e 6e  );.    selectInn
29301 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70  erLoop(pParse, p
29302 2c 20 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20  , pEList, 0, 0, 
29303 70 4f 72 64 65 72 42 79 2c 20 2d 31 2c 20 70 44  pOrderBy, -1, pD
29304 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  est,.           
29305 20 20 20 20 20 20 20 20 20 70 57 49 6e 66 6f 2d           pWInfo-
29306 3e 69 43 6f 6e 74 69 6e 75 65 2c 20 70 57 49 6e  >iContinue, pWIn
29307 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 0a 20 20  fo->iBreak);..  
29308 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74    /* End the dat
29309 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e  abase scan loop.
2930a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
2930b 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
2930c 66 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  fo);.  }else{.  
2930d 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
2930e 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   processing for 
2930f 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65  aggregate querie
29310 73 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e  s */.    NameCon
29311 74 65 78 74 20 73 4e 43 3b 20 20 20 20 2f 2a 20  text sNC;    /* 
29312 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  Name context for
29313 20 70 72 6f 63 65 73 73 69 6e 67 20 61 67 67 72   processing aggr
29314 65 67 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  egate informatio
29315 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 4d  n */.    int iAM
29316 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  em;          /* 
29317 46 69 72 73 74 20 4d 65 6d 20 61 64 64 72 65 73  First Mem addres
29318 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 63 75  s for storing cu
29319 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20 2a  rrent GROUP BY *
2931a 2f 0a 20 20 20 20 69 6e 74 20 69 42 4d 65 6d 3b  /.    int iBMem;
2931b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
2931c 73 74 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66  st Mem address f
2931d 6f 72 20 70 72 65 76 69 6f 75 73 20 47 52 4f 55  or previous GROU
2931e 50 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74 20  P BY */.    int 
2931f 69 55 73 65 46 6c 61 67 3b 20 20 20 20 20 20 20  iUseFlag;       
29320 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73 20 68  /* Mem address h
29321 6f 6c 64 69 6e 67 20 66 6c 61 67 20 69 6e 64 69  olding flag indi
29322 63 61 74 69 6e 67 20 74 68 61 74 20 61 74 20 6c  cating that at l
29323 65 61 73 74 0a 20 20 20 20 20 20 20 20 20 20 20  east.           
29324 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
29325 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 69  one row of the i
29326 6e 70 75 74 20 74 6f 20 74 68 65 20 61 67 67 72  nput to the aggr
29327 65 67 61 74 6f 72 20 68 61 73 20 62 65 65 6e 0a  egator has been.
29328 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29329 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65          ** proce
2932a 73 73 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ssed */.    int 
2932b 69 41 62 6f 72 74 46 6c 61 67 3b 20 20 20 20 20  iAbortFlag;     
2932c 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73 20 77  /* Mem address w
2932d 68 69 63 68 20 63 61 75 73 65 73 20 71 75 65 72  hich causes quer
2932e 79 20 61 62 6f 72 74 20 69 66 20 70 6f 73 69 74  y abort if posit
2932f 69 76 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 67  ive */.    int g
29330 72 6f 75 70 42 79 53 6f 72 74 3b 20 20 20 20 2f  roupBySort;    /
29331 2a 20 52 6f 77 73 20 63 6f 6d 65 20 66 72 6f 6d  * Rows come from
29332 20 73 6f 75 72 63 65 20 69 6e 20 47 52 4f 55 50   source in GROUP
29333 20 42 59 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20   BY order */.   
29334 20 69 6e 74 20 61 64 64 72 45 6e 64 3b 20 20 20   int addrEnd;   
29335 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70       /* End of p
29336 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68  rocessing for th
29337 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 0a 20 20  is SELECT */..  
29338 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20    /* Remove any 
29339 61 6e 64 20 61 6c 6c 20 61 6c 69 61 73 65 73 20  and all aliases 
2933a 62 65 74 77 65 65 6e 20 74 68 65 20 72 65 73 75  between the resu
2933b 6c 74 20 73 65 74 20 61 6e 64 20 74 68 65 0a 20  lt set and the. 
2933c 20 20 20 2a 2a 20 47 52 4f 55 50 20 42 59 20 63     ** GROUP BY c
2933d 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  lause..    */.  
2933e 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29    if( pGroupBy )
2933f 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 20 20  {.      int k;  
29340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29341 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
29342 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 73  unter */.      s
29343 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
29344 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 2f 2a 20  tem *pItem;  /* 
29345 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
29346 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 61   expression in a
29347 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 20 20   list */..      
29348 66 6f 72 28 6b 3d 70 2d 3e 70 45 4c 69 73 74 2d  for(k=p->pEList-
29349 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 2d  >nExpr, pItem=p-
2934a 3e 70 45 4c 69 73 74 2d 3e 61 3b 20 6b 3e 30 3b  >pEList->a; k>0;
2934b 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   k--, pItem++){.
2934c 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69          pItem->i
2934d 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 20  Alias = 0;.     
2934e 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70   }.      for(k=p
2934f 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2c 20  GroupBy->nExpr, 
29350 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e  pItem=pGroupBy->
29351 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70 49 74  a; k>0; k--, pIt
29352 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70  em++){.        p
29353 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30  Item->iAlias = 0
29354 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
29355 0a 20 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65  . .    /* Create
29356 20 61 20 6c 61 62 65 6c 20 74 6f 20 6a 75 6d 70   a label to jump
29357 20 74 6f 20 77 68 65 6e 20 77 65 20 77 61 6e 74   to when we want
29358 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20 71 75   to abort the qu
29359 65 72 79 20 2a 2f 0a 20 20 20 20 61 64 64 72 45  ery */.    addrE
2935a 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nd = sqlite3Vdbe
2935b 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20  MakeLabel(v);.. 
2935c 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 54 4b     /* Convert TK
2935d 5f 43 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20 69 6e  _COLUMN nodes in
2935e 74 6f 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  to TK_AGG_COLUMN
2935f 20 61 6e 64 20 6d 61 6b 65 20 65 6e 74 72 69 65   and make entrie
29360 73 20 69 6e 0a 20 20 20 20 2a 2a 20 73 41 67 67  s in.    ** sAgg
29361 49 6e 66 6f 20 66 6f 72 20 61 6c 6c 20 54 4b 5f  Info for all TK_
29362 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 6e 6f 64  AGG_FUNCTION nod
29363 65 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e  es in expression
29364 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  s of the.    ** 
29365 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
29366 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d  ..    */.    mem
29367 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
29368 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73  eof(sNC));.    s
29369 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  NC.pParse = pPar
2936a 73 65 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63  se;.    sNC.pSrc
2936b 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
2936c 0a 20 20 20 20 73 4e 43 2e 70 41 67 67 49 6e 66  .    sNC.pAggInf
2936d 6f 20 3d 20 26 73 41 67 67 49 6e 66 6f 3b 0a 20  o = &sAggInfo;. 
2936e 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e 53 6f 72     sAggInfo.nSor
2936f 74 69 6e 67 43 6f 6c 75 6d 6e 20 3d 20 70 47 72  tingColumn = pGr
29370 6f 75 70 42 79 20 3f 20 70 47 72 6f 75 70 42 79  oupBy ? pGroupBy
29371 2d 3e 6e 45 78 70 72 2b 31 20 3a 20 30 3b 0a 20  ->nExpr+1 : 0;. 
29372 20 20 20 73 41 67 67 49 6e 66 6f 2e 70 47 72 6f     sAggInfo.pGro
29373 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79 3b  upBy = pGroupBy;
29374 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
29375 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26  AnalyzeAggList(&
29376 73 4e 43 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  sNC, pEList);.  
29377 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61    sqlite3ExprAna
29378 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43  lyzeAggList(&sNC
29379 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20  , pOrderBy);.   
2937a 20 69 66 28 20 70 48 61 76 69 6e 67 20 29 7b 0a   if( pHaving ){.
2937b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2937c 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
2937d 65 73 28 26 73 4e 43 2c 20 70 48 61 76 69 6e 67  es(&sNC, pHaving
2937e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 41 67  );.    }.    sAg
2937f 67 49 6e 66 6f 2e 6e 41 63 63 75 6d 75 6c 61 74  gInfo.nAccumulat
29380 6f 72 20 3d 20 73 41 67 67 49 6e 66 6f 2e 6e 43  or = sAggInfo.nC
29381 6f 6c 75 6d 6e 3b 0a 20 20 20 20 66 6f 72 28 69  olumn;.    for(i
29382 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e  =0; i<sAggInfo.n
29383 46 75 6e 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Func; i++){.    
29384 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
29385 61 73 50 72 6f 70 65 72 74 79 28 73 41 67 67 49  asProperty(sAggI
29386 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70 45 78  nfo.aFunc[i].pEx
29387 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
29388 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) );.      sqlit
29389 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
2938a 4c 69 73 74 28 26 73 4e 43 2c 20 73 41 67 67 49  List(&sNC, sAggI
2938b 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70 45 78  nfo.aFunc[i].pEx
2938c 70 72 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20  pr->x.pList);.  
2938d 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e    }.    if( db->
2938e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
2938f 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
29390 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 69  .    /* Processi
29391 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 65  ng for aggregate
29392 73 20 77 69 74 68 20 47 52 4f 55 50 20 42 59 20  s with GROUP BY 
29393 69 73 20 76 65 72 79 20 64 69 66 66 65 72 65 6e  is very differen
29394 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 6d 75 63  t and.    ** muc
29395 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 74  h more complex t
29396 68 61 6e 20 61 67 67 72 65 67 61 74 65 73 20 77  han aggregates w
29397 69 74 68 6f 75 74 20 61 20 47 52 4f 55 50 20 42  ithout a GROUP B
29398 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  Y..    */.    if
29399 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20  ( pGroupBy ){.  
2939a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
2939b 79 49 6e 66 6f 3b 20 20 2f 2a 20 4b 65 79 69 6e  yInfo;  /* Keyin
2939c 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  g information fo
2939d 72 20 74 68 65 20 67 72 6f 75 70 20 62 79 20 63  r the group by c
2939e 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 69  lause */.      i
2939f 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20  nt j1;          
293a0 20 20 20 2f 2a 20 41 2d 76 73 2d 42 20 63 6f 6d     /* A-vs-B com
293a1 70 61 72 69 73 69 6f 6e 20 6a 75 6d 70 20 2a 2f  parision jump */
293a2 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 4f  .      int addrO
293a3 75 74 70 75 74 52 6f 77 3b 20 20 2f 2a 20 53 74  utputRow;  /* St
293a4 61 72 74 20 6f 66 20 73 75 62 72 6f 75 74 69 6e  art of subroutin
293a5 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20 61  e that outputs a
293a6 20 72 65 73 75 6c 74 20 72 6f 77 20 2a 2f 0a 20   result row */. 
293a7 20 20 20 20 20 69 6e 74 20 72 65 67 4f 75 74 70       int regOutp
293a8 75 74 52 6f 77 3b 20 20 20 2f 2a 20 52 65 74 75  utRow;   /* Retu
293a9 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73  rn address regis
293aa 74 65 72 20 66 6f 72 20 6f 75 74 70 75 74 20 73  ter for output s
293ab 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 20  ubroutine */.   
293ac 20 20 20 69 6e 74 20 61 64 64 72 53 65 74 41 62     int addrSetAb
293ad 6f 72 74 3b 20 20 20 2f 2a 20 53 65 74 20 74 68  ort;   /* Set th
293ae 65 20 61 62 6f 72 74 20 66 6c 61 67 20 61 6e 64  e abort flag and
293af 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20 20   return */.     
293b0 20 69 6e 74 20 61 64 64 72 54 6f 70 4f 66 4c 6f   int addrTopOfLo
293b1 6f 70 3b 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74  op;  /* Top of t
293b2 68 65 20 69 6e 70 75 74 20 6c 6f 6f 70 20 2a 2f  he input loop */
293b3 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 53  .      int addrS
293b4 6f 72 74 69 6e 67 49 64 78 3b 20 2f 2a 20 54 68  ortingIdx; /* Th
293b5 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  e OP_OpenEphemer
293b6 61 6c 20 66 6f 72 20 74 68 65 20 73 6f 72 74 69  al for the sorti
293b7 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20  ng index */.    
293b8 20 20 69 6e 74 20 61 64 64 72 52 65 73 65 74 3b    int addrReset;
293b9 20 20 20 20 20 20 2f 2a 20 53 75 62 72 6f 75 74        /* Subrout
293ba 69 6e 65 20 66 6f 72 20 72 65 73 65 74 74 69 6e  ine for resettin
293bb 67 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f  g the accumulato
293bc 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72  r */.      int r
293bd 65 67 52 65 73 65 74 3b 20 20 20 20 20 20 20 2f  egReset;       /
293be 2a 20 52 65 74 75 72 6e 20 61 64 64 72 65 73 73  * Return address
293bf 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 65   register for re
293c0 73 65 74 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  set subroutine *
293c1 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  /..      /* If t
293c2 68 65 72 65 20 69 73 20 61 20 47 52 4f 55 50 20  here is a GROUP 
293c3 42 59 20 63 6c 61 75 73 65 20 77 65 20 6d 69 67  BY clause we mig
293c4 68 74 20 6e 65 65 64 20 61 20 73 6f 72 74 69 6e  ht need a sortin
293c5 67 20 69 6e 64 65 78 20 74 6f 0a 20 20 20 20 20  g index to.     
293c6 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 69 74   ** implement it
293c7 2e 20 20 41 6c 6c 6f 63 61 74 65 20 74 68 61 74  .  Allocate that
293c8 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 6e   sorting index n
293c9 6f 77 2e 20 20 49 66 20 69 74 20 74 75 72 6e 73  ow.  If it turns
293ca 20 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20 74 68   out.      ** th
293cb 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65  at we do not nee
293cc 64 20 69 74 20 61 66 74 65 72 20 61 6c 6c 2c 20  d it after all, 
293cd 74 68 65 20 4f 70 65 6e 45 70 68 65 6d 65 72 61  the OpenEphemera
293ce 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 20 20  l instruction.  
293cf 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63      ** will be c
293d0 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20  onverted into a 
293d1 4e 6f 6f 70 2e 20 20 0a 20 20 20 20 20 20 2a 2f  Noop.  .      */
293d2 0a 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e  .      sAggInfo.
293d3 73 6f 72 74 69 6e 67 49 64 78 20 3d 20 70 50 61  sortingIdx = pPa
293d4 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
293d5 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65     pKeyInfo = ke
293d6 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73  yInfoFromExprLis
293d7 74 28 70 50 61 72 73 65 2c 20 70 47 72 6f 75 70  t(pParse, pGroup
293d8 42 79 29 3b 0a 20 20 20 20 20 20 61 64 64 72 53  By);.      addrS
293d9 6f 72 74 69 6e 67 49 64 78 20 3d 20 73 71 6c 69  ortingIdx = sqli
293da 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
293db 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
293dc 6c 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73 41  l, .          sA
293dd 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64  ggInfo.sortingId
293de 78 2c 20 73 41 67 67 49 6e 66 6f 2e 6e 53 6f 72  x, sAggInfo.nSor
293df 74 69 6e 67 43 6f 6c 75 6d 6e 2c 20 0a 20 20 20  tingColumn, .   
293e0 20 20 20 20 20 20 20 30 2c 20 28 63 68 61 72 2a         0, (char*
293e1 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45  )pKeyInfo, P4_KE
293e2 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
293e3 0a 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61  .      /* Initia
293e4 6c 69 7a 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61  lize memory loca
293e5 74 69 6f 6e 73 20 75 73 65 64 20 62 79 20 47 52  tions used by GR
293e6 4f 55 50 20 42 59 20 61 67 67 72 65 67 61 74 65  OUP BY aggregate
293e7 20 70 72 6f 63 65 73 73 69 6e 67 0a 20 20 20 20   processing.    
293e8 20 20 2a 2f 0a 20 20 20 20 20 20 69 55 73 65 46    */.      iUseF
293e9 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  lag = ++pParse->
293ea 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 41 62 6f  nMem;.      iAbo
293eb 72 74 46 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73  rtFlag = ++pPars
293ec 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 72  e->nMem;.      r
293ed 65 67 4f 75 74 70 75 74 52 6f 77 20 3d 20 2b 2b  egOutputRow = ++
293ee 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
293ef 20 20 20 20 61 64 64 72 4f 75 74 70 75 74 52 6f      addrOutputRo
293f0 77 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  w = sqlite3VdbeM
293f1 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
293f2 20 20 20 72 65 67 52 65 73 65 74 20 3d 20 2b 2b     regReset = ++
293f3 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
293f4 20 20 20 20 61 64 64 72 52 65 73 65 74 20 3d 20      addrReset = 
293f5 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
293f6 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69  abel(v);.      i
293f7 41 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e  AMem = pParse->n
293f8 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20 70  Mem + 1;.      p
293f9 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70  Parse->nMem += p
293fa 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a  GroupBy->nExpr;.
293fb 20 20 20 20 20 20 69 42 4d 65 6d 20 3d 20 70 50        iBMem = pP
293fc 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a  arse->nMem + 1;.
293fd 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
293fe 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d 3e  em += pGroupBy->
293ff 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c  nExpr;.      sql
29400 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29401 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
29402 20 69 41 62 6f 72 74 46 6c 61 67 29 3b 0a 20 20   iAbortFlag);.  
29403 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
29404 28 76 2c 20 22 63 6c 65 61 72 20 61 62 6f 72 74  (v, "clear abort
29405 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20 20   flag"));.      
29406 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29407 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
29408 20 30 2c 20 69 55 73 65 46 6c 61 67 29 3b 0a 20   0, iUseFlag);. 
29409 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2940a 28 28 76 2c 20 22 69 6e 64 69 63 61 74 65 20 61  ((v, "indicate a
2940b 63 63 75 6d 75 6c 61 74 6f 72 20 65 6d 70 74 79  ccumulator empty
2940c 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 42  "));..      /* B
2940d 65 67 69 6e 20 61 20 6c 6f 6f 70 20 74 68 61 74  egin a loop that
2940e 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20 61 6c   will extract al
2940f 6c 20 73 6f 75 72 63 65 20 72 6f 77 73 20 69 6e  l source rows in
29410 20 47 52 4f 55 50 20 42 59 20 6f 72 64 65 72 2e   GROUP BY order.
29411 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 6d  .      ** This m
29412 69 67 68 74 20 69 6e 76 6f 6c 76 65 20 74 77 6f  ight involve two
29413 20 73 65 70 61 72 61 74 65 20 6c 6f 6f 70 73 20   separate loops 
29414 77 69 74 68 20 61 6e 20 4f 50 5f 53 6f 72 74 20  with an OP_Sort 
29415 69 6e 20 62 65 74 77 65 65 6e 2c 20 6f 72 0a 20  in between, or. 
29416 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67 68 74       ** it might
29417 20 62 65 20 61 20 73 69 6e 67 6c 65 20 6c 6f 6f   be a single loo
29418 70 20 74 68 61 74 20 75 73 65 73 20 61 6e 20 69  p that uses an i
29419 6e 64 65 78 20 74 6f 20 65 78 74 72 61 63 74 20  ndex to extract 
2941a 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 20 20 20 20  information.    
2941b 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 69 67 68    ** in the righ
2941c 74 20 6f 72 64 65 72 20 74 6f 20 62 65 67 69 6e  t order to begin
2941d 20 77 69 74 68 2e 0a 20 20 20 20 20 20 2a 2f 0a   with..      */.
2941e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2941f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
29420 73 75 62 2c 20 72 65 67 52 65 73 65 74 2c 20 61  sub, regReset, a
29421 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20 20 20  ddrReset);.     
29422 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
29423 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72  3WhereBegin(pPar
29424 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57  se, pTabList, pW
29425 68 65 72 65 2c 20 26 70 47 72 6f 75 70 42 79 2c  here, &pGroupBy,
29426 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   0);.      if( p
29427 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20  WInfo==0 ) goto 
29428 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
29429 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 3d 3d    if( pGroupBy==
2942a 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
2942b 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 69 73  The optimizer is
2942c 20 61 62 6c 65 20 74 6f 20 64 65 6c 69 76 65 72   able to deliver
2942d 20 72 6f 77 73 20 69 6e 20 67 72 6f 75 70 20 62   rows in group b
2942e 79 20 6f 72 64 65 72 20 73 6f 0a 20 20 20 20 20  y order so.     
2942f 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e 6f 74 20     ** we do not 
29430 68 61 76 65 20 74 6f 20 73 6f 72 74 2e 20 20 54  have to sort.  T
29431 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  he OP_OpenEpheme
29432 72 61 6c 20 74 61 62 6c 65 20 77 69 6c 6c 20 62  ral table will b
29433 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6e  e.        ** can
29434 63 65 6c 6c 65 64 20 6c 61 74 65 72 20 62 65 63  celled later bec
29435 61 75 73 65 20 77 65 20 73 74 69 6c 6c 20 6e 65  ause we still ne
29436 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 70 4b  ed to use the pK
29437 65 79 49 6e 66 6f 0a 20 20 20 20 20 20 20 20 2a  eyInfo.        *
29438 2f 0a 20 20 20 20 20 20 20 20 70 47 72 6f 75 70  /.        pGroup
29439 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79  By = p->pGroupBy
2943a 3b 0a 20 20 20 20 20 20 20 20 67 72 6f 75 70 42  ;.        groupB
2943b 79 53 6f 72 74 20 3d 20 30 3b 0a 20 20 20 20 20  ySort = 0;.     
2943c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2943d 2f 2a 20 52 6f 77 73 20 61 72 65 20 63 6f 6d 69  /* Rows are comi
2943e 6e 67 20 6f 75 74 20 69 6e 20 75 6e 64 65 74 65  ng out in undete
2943f 72 6d 69 6e 65 64 20 6f 72 64 65 72 2e 20 20 57  rmined order.  W
29440 65 20 68 61 76 65 20 74 6f 20 70 75 73 68 0a 20  e have to push. 
29441 20 20 20 20 20 20 20 2a 2a 20 65 61 63 68 20 72         ** each r
29442 6f 77 20 69 6e 74 6f 20 61 20 73 6f 72 74 69 6e  ow into a sortin
29443 67 20 69 6e 64 65 78 2c 20 74 65 72 6d 69 6e 61  g index, termina
29444 74 65 20 74 68 65 20 66 69 72 73 74 20 6c 6f 6f  te the first loo
29445 70 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  p,.        ** th
29446 65 6e 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65  en loop over the
29447 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 69   sorting index i
29448 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 74  n order to get t
29449 68 65 20 6f 75 74 70 75 74 0a 20 20 20 20 20 20  he output.      
2944a 20 20 2a 2a 20 69 6e 20 73 6f 72 74 65 64 20 6f    ** in sorted o
2944b 72 64 65 72 0a 20 20 20 20 20 20 20 20 2a 2f 0a  rder.        */.
2944c 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 67 42          int regB
2944d 61 73 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  ase;.        int
2944e 20 72 65 67 52 65 63 6f 72 64 3b 0a 20 20 20 20   regRecord;.    
2944f 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20      int nCol;.  
29450 20 20 20 20 20 20 69 6e 74 20 6e 47 72 6f 75 70        int nGroup
29451 42 79 3b 0a 0a 20 20 20 20 20 20 20 20 67 72 6f  By;..        gro
29452 75 70 42 79 53 6f 72 74 20 3d 20 31 3b 0a 20 20  upBySort = 1;.  
29453 20 20 20 20 20 20 6e 47 72 6f 75 70 42 79 20 3d        nGroupBy =
29454 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72   pGroupBy->nExpr
29455 3b 0a 20 20 20 20 20 20 20 20 6e 43 6f 6c 20 3d  ;.        nCol =
29456 20 6e 47 72 6f 75 70 42 79 20 2b 20 31 3b 0a 20   nGroupBy + 1;. 
29457 20 20 20 20 20 20 20 6a 20 3d 20 6e 47 72 6f 75         j = nGrou
29458 70 42 79 2b 31 3b 0a 20 20 20 20 20 20 20 20 66  pBy+1;.        f
29459 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e  or(i=0; i<sAggIn
2945a 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  fo.nColumn; i++)
2945b 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
2945c 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d  sAggInfo.aCol[i]
2945d 2e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d  .iSorterColumn>=
2945e 6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  j ){.           
2945f 20 6e 43 6f 6c 2b 2b 3b 0a 20 20 20 20 20 20 20   nCol++;.       
29460 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20       j++;.      
29461 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
29462 20 20 20 20 20 20 20 20 72 65 67 42 61 73 65 20          regBase 
29463 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
29464 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 43  Range(pParse, nC
29465 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ol);.        sql
29466 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65  ite3ExprCacheCle
29467 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  ar(pParse);.    
29468 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
29469 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72  odeExprList(pPar
2946a 73 65 2c 20 70 47 72 6f 75 70 42 79 2c 20 72 65  se, pGroupBy, re
2946b 67 42 61 73 65 2c 20 30 29 3b 0a 20 20 20 20 20  gBase, 0);.     
2946c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2946d 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65  dOp2(v, OP_Seque
2946e 6e 63 65 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f  nce, sAggInfo.so
2946f 72 74 69 6e 67 49 64 78 2c 72 65 67 42 61 73 65  rtingIdx,regBase
29470 2b 6e 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20  +nGroupBy);.    
29471 20 20 20 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79      j = nGroupBy
29472 2b 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  +1;.        for(
29473 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e  i=0; i<sAggInfo.
29474 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
29475 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20           struct 
29476 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f  AggInfo_col *pCo
29477 6c 20 3d 20 26 73 41 67 67 49 6e 66 6f 2e 61 43  l = &sAggInfo.aC
29478 6f 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20  ol[i];.         
29479 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f 72 74   if( pCol->iSort
2947a 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20  erColumn>=j ){. 
2947b 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 72             int r
2947c 31 20 3d 20 6a 20 2b 20 72 65 67 42 61 73 65 3b  1 = j + regBase;
2947d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
2947e 20 72 32 3b 0a 0a 20 20 20 20 20 20 20 20 20 20   r2;..          
2947f 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78    r2 = sqlite3Ex
29480 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28  prCodeGetColumn(
29481 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
29482 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29483 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54          pCol->pT
29484 61 62 2c 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d  ab, pCol->iColum
29485 6e 2c 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 2c  n, pCol->iTable,
29486 20 72 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20   r1, 0);.       
29487 20 20 20 20 20 69 66 28 20 72 31 21 3d 72 32 20       if( r1!=r2 
29488 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
29489 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2948a 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
2948b 72 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20  r2, r1);.       
2948c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2948d 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 20     j++;.        
2948e 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
2948f 20 20 20 20 20 20 72 65 67 52 65 63 6f 72 64 20        regRecord 
29490 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
29491 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
29492 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29493 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
29494 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73 65  eRecord, regBase
29495 2c 20 6e 43 6f 6c 2c 20 72 65 67 52 65 63 6f 72  , nCol, regRecor
29496 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  d);.        sqli
29497 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29498 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 73   OP_IdxInsert, s
29499 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49  AggInfo.sortingI
2949a 64 78 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a  dx, regRecord);.
2949b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
2949c 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2949d 61 72 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29  arse, regRecord)
2949e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2949f 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
294a0 65 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  e(pParse, regBas
294a1 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20  e, nCol);.      
294a2 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e    sqlite3WhereEn
294a3 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20  d(pWInfo);.     
294a4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
294a5 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 2c  dOp2(v, OP_Sort,
294a6 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
294a7 67 49 64 78 2c 20 61 64 64 72 45 6e 64 29 3b 0a  gIdx, addrEnd);.
294a8 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d          VdbeComm
294a9 65 6e 74 28 28 76 2c 20 22 47 52 4f 55 50 20 42  ent((v, "GROUP B
294aa 59 20 73 6f 72 74 22 29 29 3b 0a 20 20 20 20 20  Y sort"));.     
294ab 20 20 20 73 41 67 67 49 6e 66 6f 2e 75 73 65 53     sAggInfo.useS
294ac 6f 72 74 69 6e 67 49 64 78 20 3d 20 31 3b 0a 20  ortingIdx = 1;. 
294ad 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
294ae 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61  prCacheClear(pPa
294af 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  rse);.      }.. 
294b0 20 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65       /* Evaluate
294b1 20 74 68 65 20 63 75 72 72 65 6e 74 20 47 52 4f   the current GRO
294b2 55 50 20 42 59 20 74 65 72 6d 73 20 61 6e 64 20  UP BY terms and 
294b3 73 74 6f 72 65 20 69 6e 20 62 30 2c 20 62 31 2c  store in b0, b1,
294b4 20 62 32 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20   b2....      ** 
294b5 28 62 30 20 69 73 20 6d 65 6d 6f 72 79 20 6c 6f  (b0 is memory lo
294b6 63 61 74 69 6f 6e 20 69 42 4d 65 6d 2b 30 2c 20  cation iBMem+0, 
294b7 62 31 20 69 73 20 69 42 4d 65 6d 2b 31 2c 20 61  b1 is iBMem+1, a
294b8 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 20 20 20  nd so forth).   
294b9 20 20 20 2a 2a 20 54 68 65 6e 20 63 6f 6d 70 61     ** Then compa
294ba 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 47  re the current G
294bb 52 4f 55 50 20 42 59 20 74 65 72 6d 73 20 61 67  ROUP BY terms ag
294bc 61 69 6e 73 74 20 74 68 65 20 47 52 4f 55 50 20  ainst the GROUP 
294bd 42 59 20 74 65 72 6d 73 0a 20 20 20 20 20 20 2a  BY terms.      *
294be 2a 20 66 72 6f 6d 20 74 68 65 20 70 72 65 76 69  * from the previ
294bf 6f 75 73 20 72 6f 77 20 63 75 72 72 65 6e 74 6c  ous row currentl
294c0 79 20 73 74 6f 72 65 64 20 69 6e 20 61 30 2c 20  y stored in a0, 
294c1 61 31 2c 20 61 32 2e 2e 2e 0a 20 20 20 20 20 20  a1, a2....      
294c2 2a 2f 0a 20 20 20 20 20 20 61 64 64 72 54 6f 70  */.      addrTop
294c3 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33  OfLoop = sqlite3
294c4 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
294c5 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
294c6 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
294c7 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 66  pParse);.      f
294c8 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f 75 70  or(j=0; j<pGroup
294c9 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b  By->nExpr; j++){
294ca 0a 20 20 20 20 20 20 20 20 69 66 28 20 67 72 6f  .        if( gro
294cb 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20 20 20  upBySort ){.    
294cc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
294cd 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
294ce 6c 75 6d 6e 2c 20 73 41 67 67 49 6e 66 6f 2e 73  lumn, sAggInfo.s
294cf 6f 72 74 69 6e 67 49 64 78 2c 20 6a 2c 20 69 42  ortingIdx, j, iB
294d0 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20 20 20 20 20  Mem+j);.        
294d1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
294d2 20 73 41 67 67 49 6e 66 6f 2e 64 69 72 65 63 74   sAggInfo.direct
294d3 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  Mode = 1;.      
294d4 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
294d5 6f 64 65 28 70 50 61 72 73 65 2c 20 70 47 72 6f  ode(pParse, pGro
294d6 75 70 42 79 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72  upBy->a[j].pExpr
294d7 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20  , iBMem+j);.    
294d8 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
294d9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
294da 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70  ddOp4(v, OP_Comp
294db 61 72 65 2c 20 69 41 4d 65 6d 2c 20 69 42 4d 65  are, iAMem, iBMe
294dc 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78  m, pGroupBy->nEx
294dd 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr,.            
294de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
294df 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50  har*)pKeyInfo, P
294e0 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20  4_KEYINFO);.    
294e1 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
294e2 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
294e3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
294e4 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
294e5 4a 75 6d 70 2c 20 6a 31 2b 31 2c 20 30 2c 20 6a  Jump, j1+1, 0, j
294e6 31 2b 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  1+1);..      /* 
294e7 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
294e8 61 74 20 72 75 6e 73 20 77 68 65 6e 65 76 65 72  at runs whenever
294e9 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 68   the GROUP BY ch
294ea 61 6e 67 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20  anges..      ** 
294eb 43 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 47  Changes in the G
294ec 52 4f 55 50 20 42 59 20 61 72 65 20 64 65 74 65  ROUP BY are dete
294ed 63 74 65 64 20 62 79 20 74 68 65 20 70 72 65 76  cted by the prev
294ee 69 6f 75 73 20 63 6f 64 65 0a 20 20 20 20 20 20  ious code.      
294ef 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20 74 68  ** block.  If th
294f0 65 72 65 20 77 65 72 65 20 6e 6f 20 63 68 61 6e  ere were no chan
294f1 67 65 73 2c 20 74 68 69 73 20 62 6c 6f 63 6b 20  ges, this block 
294f2 69 73 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 20  is skipped..    
294f3 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68    **.      ** Th
294f4 69 73 20 63 6f 64 65 20 63 6f 70 69 65 73 20 63  is code copies c
294f5 75 72 72 65 6e 74 20 67 72 6f 75 70 20 62 79 20  urrent group by 
294f6 74 65 72 6d 73 20 69 6e 20 62 30 2c 62 31 2c 62  terms in b0,b1,b
294f7 32 2c 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 6f  2,....      ** o
294f8 76 65 72 20 74 6f 20 61 30 2c 61 31 2c 61 32 2e  ver to a0,a1,a2.
294f9 20 20 49 74 20 74 68 65 6e 20 63 61 6c 6c 73 20    It then calls 
294fa 74 68 65 20 6f 75 74 70 75 74 20 73 75 62 72 6f  the output subro
294fb 75 74 69 6e 65 0a 20 20 20 20 20 20 2a 2a 20 61  utine.      ** a
294fc 6e 64 20 72 65 73 65 74 73 20 74 68 65 20 61 67  nd resets the ag
294fd 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c 61  gregate accumula
294fe 74 6f 72 20 72 65 67 69 73 74 65 72 73 20 69 6e  tor registers in
294ff 20 70 72 65 70 61 72 61 74 69 6f 6e 0a 20 20 20   preparation.   
29500 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 6e 65     ** for the ne
29501 78 74 20 47 52 4f 55 50 20 42 59 20 62 61 74 63  xt GROUP BY batc
29502 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  h..      */.    
29503 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
29504 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 69 42  eMove(pParse, iB
29505 4d 65 6d 2c 20 69 41 4d 65 6d 2c 20 70 47 72 6f  Mem, iAMem, pGro
29506 75 70 42 79 2d 3e 6e 45 78 70 72 29 3b 0a 20 20  upBy->nExpr);.  
29507 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29508 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75  ddOp2(v, OP_Gosu
29509 62 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 2c  b, regOutputRow,
2950a 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 29 3b   addrOutputRow);
2950b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2950c 6e 74 28 28 76 2c 20 22 6f 75 74 70 75 74 20 6f  nt((v, "output o
2950d 6e 65 20 72 6f 77 22 29 29 3b 0a 20 20 20 20 20  ne row"));.     
2950e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2950f 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p2(v, OP_IfPos, 
29510 69 41 62 6f 72 74 46 6c 61 67 2c 20 61 64 64 72  iAbortFlag, addr
29511 45 6e 64 29 3b 0a 20 20 20 20 20 20 56 64 62 65  End);.      Vdbe
29512 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 68 65  Comment((v, "che
29513 63 6b 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29  ck abort flag"))
29514 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29515 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29516 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65 74 2c  Gosub, regReset,
29517 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20   addrReset);.   
29518 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
29519 76 2c 20 22 72 65 73 65 74 20 61 63 63 75 6d 75  v, "reset accumu
2951a 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20 20  lator"));..     
2951b 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61   /* Update the a
2951c 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c  ggregate accumul
2951d 61 74 6f 72 73 20 62 61 73 65 64 20 6f 6e 20 74  ators based on t
2951e 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 20 20  he content of.  
2951f 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65      ** the curre
29520 6e 74 20 72 6f 77 0a 20 20 20 20 20 20 2a 2f 0a  nt row.      */.
29521 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29522 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29  eJumpHere(v, j1)
29523 3b 0a 20 20 20 20 20 20 75 70 64 61 74 65 41 63  ;.      updateAc
29524 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65  cumulator(pParse
29525 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
29526 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29527 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
29528 67 65 72 2c 20 31 2c 20 69 55 73 65 46 6c 61 67  ger, 1, iUseFlag
29529 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2952a 6d 65 6e 74 28 28 76 2c 20 22 69 6e 64 69 63 61  ment((v, "indica
2952b 74 65 20 64 61 74 61 20 69 6e 20 61 63 63 75 6d  te data in accum
2952c 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20  ulator"));..    
2952d 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20    /* End of the 
2952e 6c 6f 6f 70 0a 20 20 20 20 20 20 2a 2f 0a 20 20  loop.      */.  
2952f 20 20 20 20 69 66 28 20 67 72 6f 75 70 42 79 53      if( groupByS
29530 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ort ){.        s
29531 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29532 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 41 67  (v, OP_Next, sAg
29533 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78  gInfo.sortingIdx
29534 2c 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 29  , addrTopOfLoop)
29535 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
29536 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
29537 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
29538 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29539 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
2953a 76 2c 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64  v, addrSortingId
2953b 78 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  x, 1);.      }..
2953c 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
2953d 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 20 6f 66  the final row of
2953e 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2f   result.      */
2953f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29540 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
29541 6f 73 75 62 2c 20 72 65 67 4f 75 74 70 75 74 52  osub, regOutputR
29542 6f 77 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f  ow, addrOutputRo
29543 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  w);.      VdbeCo
29544 6d 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74 70 75  mment((v, "outpu
29545 74 20 66 69 6e 61 6c 20 72 6f 77 22 29 29 3b 0a  t final row"));.
29546 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 6f  .      /* Jump o
29547 76 65 72 20 74 68 65 20 73 75 62 72 6f 75 74 69  ver the subrouti
29548 6e 65 73 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  nes.      */.   
29549 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2954a 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2954b 20 30 2c 20 61 64 64 72 45 6e 64 29 3b 0a 0a 20   0, addrEnd);.. 
2954c 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2954d 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
2954e 61 74 20 6f 75 74 70 75 74 73 20 61 20 73 69 6e  at outputs a sin
2954f 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72  gle row of the r
29550 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2a 20 73  esult.      ** s
29551 65 74 2e 20 20 54 68 69 73 20 73 75 62 72 6f 75  et.  This subrou
29552 74 69 6e 65 20 66 69 72 73 74 20 6c 6f 6f 6b 73  tine first looks
29553 20 61 74 20 74 68 65 20 69 55 73 65 46 6c 61 67   at the iUseFlag
29554 2e 20 20 49 66 20 69 55 73 65 46 6c 61 67 0a 20  .  If iUseFlag. 
29555 20 20 20 20 20 2a 2a 20 69 73 20 6c 65 73 73 20       ** is less 
29556 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
29557 20 7a 65 72 6f 2c 20 74 68 65 20 73 75 62 72 6f   zero, the subro
29558 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
29559 2e 20 20 49 66 0a 20 20 20 20 20 20 2a 2a 20 74  .  If.      ** t
2955a 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 63 61  he processing ca
2955b 6c 6c 73 20 66 6f 72 20 74 68 65 20 71 75 65 72  lls for the quer
2955c 79 20 74 6f 20 61 62 6f 72 74 2c 20 74 68 69 73  y to abort, this
2955d 20 73 75 62 72 6f 75 74 69 6e 65 0a 20 20 20 20   subroutine.    
2955e 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 73 20    ** increments 
2955f 74 68 65 20 69 41 62 6f 72 74 46 6c 61 67 20 6d  the iAbortFlag m
29560 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 62  emory location b
29561 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20  efore returning 
29562 69 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65  in.      ** orde
29563 72 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 65 20  r to signal the 
29564 63 61 6c 6c 65 72 20 74 6f 20 61 62 6f 72 74 2e  caller to abort.
29565 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
29566 61 64 64 72 53 65 74 41 62 6f 72 74 20 3d 20 73  addrSetAbort = s
29567 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
29568 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
29569 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2956a 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2956b 20 31 2c 20 69 41 62 6f 72 74 46 6c 61 67 29 3b   1, iAbortFlag);
2956c 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2956d 6e 74 28 28 76 2c 20 22 73 65 74 20 61 62 6f 72  nt((v, "set abor
2956e 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20  t flag"));.     
2956f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29570 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c  p1(v, OP_Return,
29571 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a   regOutputRow);.
29572 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29573 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
29574 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 29 3b   addrOutputRow);
29575 0a 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75  .      addrOutpu
29576 74 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64  tRow = sqlite3Vd
29577 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
29578 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29579 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2957a 49 66 50 6f 73 2c 20 69 55 73 65 46 6c 61 67 2c  IfPos, iUseFlag,
2957b 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 32   addrOutputRow+2
2957c 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2957d 6d 65 6e 74 28 28 76 2c 20 22 47 72 6f 75 70 62  ment((v, "Groupb
2957e 79 20 72 65 73 75 6c 74 20 67 65 6e 65 72 61 74  y result generat
2957f 6f 72 20 65 6e 74 72 79 20 70 6f 69 6e 74 22 29  or entry point")
29580 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29581 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
29582 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70  _Return, regOutp
29583 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 66 69  utRow);.      fi
29584 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f  nalizeAggFunctio
29585 6e 73 28 70 50 61 72 73 65 2c 20 26 73 41 67 67  ns(pParse, &sAgg
29586 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 69 66 28  Info);.      if(
29587 20 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20   pHaving ){.    
29588 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
29589 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
2958a 48 61 76 69 6e 67 2c 20 61 64 64 72 4f 75 74 70  Having, addrOutp
2958b 75 74 52 6f 77 2b 31 2c 20 53 51 4c 49 54 45 5f  utRow+1, SQLITE_
2958c 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
2958d 20 20 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63     }.      selec
2958e 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73  tInnerLoop(pPars
2958f 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c  e, p, p->pEList,
29590 20 30 2c 20 30 2c 20 70 4f 72 64 65 72 42 79 2c   0, 0, pOrderBy,
29591 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
29592 20 20 20 20 20 20 20 64 69 73 74 69 6e 63 74 2c         distinct,
29593 20 70 44 65 73 74 2c 0a 20 20 20 20 20 20 20 20   pDest,.        
29594 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 64                ad
29595 64 72 4f 75 74 70 75 74 52 6f 77 2b 31 2c 20 61  drOutputRow+1, a
29596 64 64 72 53 65 74 41 62 6f 72 74 29 3b 0a 20 20  ddrSetAbort);.  
29597 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29598 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75  ddOp1(v, OP_Retu
29599 72 6e 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77  rn, regOutputRow
2959a 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2959b 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64 20 67 72  ment((v, "end gr
2959c 6f 75 70 62 79 20 72 65 73 75 6c 74 20 67 65 6e  oupby result gen
2959d 65 72 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20  erator"));..    
2959e 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
2959f 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
295a0 77 69 6c 6c 20 72 65 73 65 74 20 74 68 65 20 67  will reset the g
295a1 72 6f 75 70 2d 62 79 20 61 63 63 75 6d 75 6c 61  roup-by accumula
295a2 74 6f 72 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  tor.      */.   
295a3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
295a4 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64  solveLabel(v, ad
295a5 64 72 52 65 73 65 74 29 3b 0a 20 20 20 20 20 20  drReset);.      
295a6 72 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72  resetAccumulator
295a7 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
295a8 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  fo);.      sqlit
295a9 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
295aa 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 52 65  OP_Return, regRe
295ab 73 65 74 29 3b 0a 20 20 20 20 20 0a 20 20 20 20  set);.     .    
295ac 7d 20 2f 2a 20 65 6e 64 69 66 20 70 47 72 6f 75  } /* endif pGrou
295ad 70 42 79 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20  pBy */.    else 
295ae 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74  {.      ExprList
295af 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 23 69 66 6e   *pDel = 0;.#ifn
295b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
295b1 42 54 52 45 45 43 4f 55 4e 54 0a 20 20 20 20 20  BTREECOUNT.     
295b2 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
295b3 20 20 20 20 69 66 28 20 28 70 54 61 62 20 3d 20      if( (pTab = 
295b4 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 70 2c  isSimpleCount(p,
295b5 20 26 73 41 67 67 49 6e 66 6f 29 29 21 3d 30 20   &sAggInfo))!=0 
295b6 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
295b7 20 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 29   isSimpleCount()
295b8 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
295b9 65 72 20 74 6f 20 61 20 54 61 62 6c 65 20 73 74  er to a Table st
295ba 72 75 63 74 75 72 65 2c 20 74 68 65 6e 0a 20 20  ructure, then.  
295bb 20 20 20 20 20 20 2a 2a 20 74 68 65 20 53 51 4c        ** the SQL
295bc 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66   statement is of
295bd 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 20 20 20   the form:.     
295be 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
295bf 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28     SELECT count(
295c0 2a 29 20 46 52 4f 4d 20 3c 74 62 6c 3e 0a 20 20  *) FROM <tbl>.  
295c1 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
295c2 20 2a 2a 20 77 68 65 72 65 20 74 68 65 20 54 61   ** where the Ta
295c3 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 72 65  ble structure re
295c4 74 75 72 6e 65 64 20 72 65 70 72 65 73 65 6e 74  turned represent
295c5 73 20 74 61 62 6c 65 20 3c 74 62 6c 3e 2e 0a 20  s table <tbl>.. 
295c6 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
295c7 20 20 2a 2a 20 54 68 69 73 20 73 74 61 74 65 6d    ** This statem
295c8 65 6e 74 20 69 73 20 73 6f 20 63 6f 6d 6d 6f 6e  ent is so common
295c9 20 74 68 61 74 20 69 74 20 69 73 20 6f 70 74 69   that it is opti
295ca 6d 69 7a 65 64 20 73 70 65 63 69 61 6c 6c 79 2e  mized specially.
295cb 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   The.        ** 
295cc 4f 50 5f 43 6f 75 6e 74 20 69 6e 73 74 72 75 63  OP_Count instruc
295cd 74 69 6f 6e 20 69 73 20 65 78 65 63 75 74 65 64  tion is executed
295ce 20 65 69 74 68 65 72 20 6f 6e 20 74 68 65 20 69   either on the i
295cf 6e 74 6b 65 79 20 74 61 62 6c 65 20 74 68 61 74  ntkey table that
295d0 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74  .        ** cont
295d1 61 69 6e 73 20 74 68 65 20 64 61 74 61 20 66 6f  ains the data fo
295d2 72 20 74 61 62 6c 65 20 3c 74 62 6c 3e 20 6f 72  r table <tbl> or
295d3 20 6f 6e 20 6f 6e 65 20 6f 66 20 69 74 73 20 69   on one of its i
295d4 6e 64 65 78 65 73 2e 20 49 74 0a 20 20 20 20 20  ndexes. It.     
295d5 20 20 20 2a 2a 20 69 73 20 62 65 74 74 65 72 20     ** is better 
295d6 74 6f 20 65 78 65 63 75 74 65 20 74 68 65 20 6f  to execute the o
295d7 70 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2c 20 61  p on an index, a
295d8 73 20 69 6e 64 65 78 65 73 20 61 72 65 20 61 6c  s indexes are al
295d9 6d 6f 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  most.        ** 
295da 61 6c 77 61 79 73 20 73 70 72 65 61 64 20 61 63  always spread ac
295db 72 6f 73 73 20 6c 65 73 73 20 70 61 67 65 73 20  ross less pages 
295dc 74 68 61 6e 20 74 68 65 69 72 20 63 6f 72 72 65  than their corre
295dd 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 73 2e  sponding tables.
295de 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
295df 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44      const int iD
295e0 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
295e1 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
295e2 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
295e3 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e  ma);.        con
295e4 73 74 20 69 6e 74 20 69 43 73 72 20 3d 20 70 50  st int iCsr = pP
295e5 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20  arse->nTab++;   
295e6 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 73    /* Cursor to s
295e7 63 61 6e 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20  can b-tree */.  
295e8 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
295e9 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
295ea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
295eb 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
295ec 2a 2f 0a 20 20 20 20 20 20 20 20 4b 65 79 49 6e  */.        KeyIn
295ed 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 30  fo *pKeyInfo = 0
295ee 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
295ef 2f 2a 20 4b 65 79 69 6e 66 6f 20 66 6f 72 20 73  /* Keyinfo for s
295f0 63 61 6e 6e 65 64 20 69 6e 64 65 78 20 2a 2f 0a  canned index */.
295f1 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70          Index *p
295f2 42 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20  Best = 0;       
295f3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
295f4 42 65 73 74 20 69 6e 64 65 78 20 66 6f 75 6e 64  Best index found
295f5 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 20 20 20   so far */.     
295f6 20 20 20 69 6e 74 20 69 52 6f 6f 74 20 3d 20 70     int iRoot = p
295f7 54 61 62 2d 3e 74 6e 75 6d 3b 20 20 20 20 20 20  Tab->tnum;      
295f8 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20          /* Root 
295f9 70 61 67 65 20 6f 66 20 73 63 61 6e 6e 65 64 20  page of scanned 
295fa 62 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 20  b-tree */..     
295fb 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65     sqlite3CodeVe
295fc 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73  rifySchema(pPars
295fd 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20  e, iDb);.       
295fe 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
295ff 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70  k(pParse, iDb, p
29600 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54  Tab->tnum, 0, pT
29601 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20  ab->zName);..   
29602 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66       /* Search f
29603 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74 68 61  or the index tha
29604 74 20 68 61 73 20 74 68 65 20 6c 65 61 73 74 20  t has the least 
29605 61 6d 6f 75 6e 74 20 6f 66 20 63 6f 6c 75 6d 6e  amount of column
29606 73 2e 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a  s. If.        **
29607 20 74 68 65 72 65 20 69 73 20 73 75 63 68 20 61   there is such a
29608 6e 20 69 6e 64 65 78 2c 20 61 6e 64 20 69 74 20  n index, and it 
29609 68 61 73 20 6c 65 73 73 20 63 6f 6c 75 6d 6e 73  has less columns
2960a 20 74 68 61 6e 20 74 68 65 20 74 61 62 6c 65 0a   than the table.
2960b 20 20 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 2c          ** does,
2960c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 61 73 73   then we can ass
2960d 75 6d 65 20 74 68 61 74 20 69 74 20 63 6f 6e 73  ume that it cons
2960e 75 6d 65 73 20 6c 65 73 73 20 73 70 61 63 65 20  umes less space 
2960f 6f 6e 20 64 69 73 6b 20 61 6e 64 0a 20 20 20 20  on disk and.    
29610 20 20 20 20 2a 2a 20 77 69 6c 6c 20 74 68 65 72      ** will ther
29611 65 66 6f 72 65 20 62 65 20 63 68 65 61 70 65 72  efore be cheaper
29612 20 74 6f 20 73 63 61 6e 20 74 6f 20 64 65 74 65   to scan to dete
29613 72 6d 69 6e 65 20 74 68 65 20 71 75 65 72 79 20  rmine the query 
29614 72 65 73 75 6c 74 2e 0a 20 20 20 20 20 20 20 20  result..        
29615 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ** In this case 
29616 73 65 74 20 69 52 6f 6f 74 20 74 6f 20 74 68 65  set iRoot to the
29617 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
29618 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62  r of the index b
29619 2d 74 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a  -tree.        **
2961a 20 61 6e 64 20 70 4b 65 79 49 6e 66 6f 20 74 6f   and pKeyInfo to
2961b 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72   the KeyInfo str
2961c 75 63 74 75 72 65 20 72 65 71 75 69 72 65 64 20  ucture required 
2961d 74 6f 20 6e 61 76 69 67 61 74 65 20 74 68 65 0a  to navigate the.
2961e 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 78          ** index
2961f 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
29620 20 20 20 20 20 2a 2a 20 49 6e 20 70 72 61 63 74       ** In pract
29621 69 63 65 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  ice the KeyInfo 
29622 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 6e  structure will n
29623 6f 74 20 62 65 20 75 73 65 64 2e 20 49 74 20 69  ot be used. It i
29624 73 20 6f 6e 6c 79 20 0a 20 20 20 20 20 20 20 20  s only .        
29625 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 6b 65 65  ** passed to kee
29626 70 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 68 61  p OP_OpenRead ha
29627 70 70 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ppy..        */.
29628 20 20 20 20 20 20 20 20 66 6f 72 28 70 49 64 78          for(pIdx
29629 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2962a 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2962b 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
2962c 20 20 69 66 28 20 21 70 42 65 73 74 20 7c 7c 20    if( !pBest || 
2962d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3c 70 42  pIdx->nColumn<pB
2962e 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  est->nColumn ){.
2962f 20 20 20 20 20 20 20 20 20 20 20 20 70 42 65 73              pBes
29630 74 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 20 20  t = pIdx;.      
29631 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
29632 20 20 20 20 20 20 20 20 69 66 28 20 70 42 65 73          if( pBes
29633 74 20 26 26 20 70 42 65 73 74 2d 3e 6e 43 6f 6c  t && pBest->nCol
29634 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  umn<pTab->nCol )
29635 7b 0a 20 20 20 20 20 20 20 20 20 20 69 52 6f 6f  {.          iRoo
29636 74 20 3d 20 70 42 65 73 74 2d 3e 74 6e 75 6d 3b  t = pBest->tnum;
29637 0a 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 49  .          pKeyI
29638 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  nfo = sqlite3Ind
29639 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
2963a 2c 20 70 42 65 73 74 29 3b 0a 20 20 20 20 20 20  , pBest);.      
2963b 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20    }..        /* 
2963c 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79  Open a read-only
2963d 20 63 75 72 73 6f 72 2c 20 65 78 65 63 75 74 65   cursor, execute
2963e 20 74 68 65 20 4f 50 5f 43 6f 75 6e 74 2c 20 63   the OP_Count, c
2963f 6c 6f 73 65 20 74 68 65 20 63 75 72 73 6f 72 2e  lose the cursor.
29640 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
29641 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
29642 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 43   OP_OpenRead, iC
29643 73 72 2c 20 69 52 6f 6f 74 2c 20 69 44 62 29 3b  sr, iRoot, iDb);
29644 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4b 65  .        if( pKe
29645 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20  yInfo ){.       
29646 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
29647 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63  angeP4(v, -1, (c
29648 68 61 72 20 2a 29 70 4b 65 79 49 6e 66 6f 2c 20  har *)pKeyInfo, 
29649 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
2964a 46 46 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  FF);.        }. 
2964b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2964c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2964d 6f 75 6e 74 2c 20 69 43 73 72 2c 20 73 41 67 67  ount, iCsr, sAgg
2964e 49 6e 66 6f 2e 61 46 75 6e 63 5b 30 5d 2e 69 4d  Info.aFunc[0].iM
2964f 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  em);.        sql
29650 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
29651 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 73 72  , OP_Close, iCsr
29652 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23  );.      }else.#
29653 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
29654 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 20  OMIT_BTREECOUNT 
29655 2a 2f 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  */.      {.     
29656 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74     /* Check if t
29657 68 65 20 71 75 65 72 79 20 69 73 20 6f 66 20 6f  he query is of o
29658 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
29659 69 6e 67 20 66 6f 72 6d 73 3a 0a 20 20 20 20 20  ing forms:.     
2965a 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
2965b 20 20 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29     SELECT min(x)
2965c 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20   FROM ....      
2965d 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d 61    **   SELECT ma
2965e 78 28 78 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20  x(x) FROM ....  
2965f 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
29660 20 2a 2a 20 49 66 20 69 74 20 69 73 2c 20 74 68   ** If it is, th
29661 65 6e 20 61 73 6b 20 74 68 65 20 63 6f 64 65 20  en ask the code 
29662 69 6e 20 77 68 65 72 65 2e 63 20 74 6f 20 61 74  in where.c to at
29663 74 65 6d 70 74 20 74 6f 20 73 6f 72 74 20 72 65  tempt to sort re
29664 73 75 6c 74 73 0a 20 20 20 20 20 20 20 20 2a 2a  sults.        **
29665 20 61 73 20 69 66 20 74 68 65 72 65 20 77 61 73   as if there was
29666 20 61 6e 20 22 4f 52 44 45 52 20 4f 4e 20 78 22   an "ORDER ON x"
29667 20 6f 72 20 22 4f 52 44 45 52 20 4f 4e 20 78 20   or "ORDER ON x 
29668 44 45 53 43 22 20 63 6c 61 75 73 65 2e 20 0a 20  DESC" clause. . 
29669 20 20 20 20 20 20 20 2a 2a 20 49 66 20 77 68 65         ** If whe
2966a 72 65 2e 63 20 69 73 20 61 62 6c 65 20 74 6f 20  re.c is able to 
2966b 70 72 6f 64 75 63 65 20 72 65 73 75 6c 74 73 20  produce results 
2966c 73 6f 72 74 65 64 20 69 6e 20 74 68 69 73 20 6f  sorted in this o
2966d 72 64 65 72 2c 20 74 68 65 6e 0a 20 20 20 20 20  rder, then.     
2966e 20 20 20 2a 2a 20 61 64 64 20 76 64 62 65 20 63     ** add vdbe c
2966f 6f 64 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  ode to break out
29670 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 69   of the processi
29671 6e 67 20 6c 6f 6f 70 20 61 66 74 65 72 20 74 68  ng loop after th
29672 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69  e .        ** fi
29673 72 73 74 20 69 74 65 72 61 74 69 6f 6e 20 28 73  rst iteration (s
29674 69 6e 63 65 20 74 68 65 20 66 69 72 73 74 20 69  ince the first i
29675 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  teration of the 
29676 6c 6f 6f 70 20 69 73 20 0a 20 20 20 20 20 20 20  loop is .       
29677 20 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20 74   ** guaranteed t
29678 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 68 65  o operate on the
29679 20 72 6f 77 20 77 69 74 68 20 74 68 65 20 6d 69   row with the mi
2967a 6e 69 6d 75 6d 20 6f 72 20 6d 61 78 69 6d 75 6d  nimum or maximum
2967b 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c   .        ** val
2967c 75 65 20 6f 66 20 78 2c 20 74 68 65 20 6f 6e 6c  ue of x, the onl
2967d 79 20 72 6f 77 20 72 65 71 75 69 72 65 64 29 2e  y row required).
2967e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
2967f 20 20 20 20 2a 2a 20 41 20 73 70 65 63 69 61 6c      ** A special
29680 20 66 6c 61 67 20 6d 75 73 74 20 62 65 20 70 61   flag must be pa
29681 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 57  ssed to sqlite3W
29682 68 65 72 65 42 65 67 69 6e 28 29 20 74 6f 20 73  hereBegin() to s
29683 6c 69 67 68 74 6c 79 0a 20 20 20 20 20 20 20 20  lightly.        
29684 2a 2a 20 6d 6f 64 69 66 79 20 62 65 68 61 76 69  ** modify behavi
29685 6f 75 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  our as follows:.
29686 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
29687 20 20 20 2a 2a 20 20 20 2b 20 49 66 20 74 68 65     **   + If the
29688 20 71 75 65 72 79 20 69 73 20 61 20 22 53 45 4c   query is a "SEL
29689 45 43 54 20 6d 69 6e 28 78 29 22 2c 20 74 68 65  ECT min(x)", the
2968a 6e 20 74 68 65 20 6c 6f 6f 70 20 63 6f 64 65 64  n the loop coded
2968b 20 62 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   by.        **  
2968c 20 20 20 77 68 65 72 65 2e 63 20 73 68 6f 75 6c     where.c shoul
2968d 64 20 6e 6f 74 20 69 74 65 72 61 74 65 20 6f 76  d not iterate ov
2968e 65 72 20 61 6e 79 20 76 61 6c 75 65 73 20 77 69  er any values wi
2968f 74 68 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 0a  th a NULL value.
29690 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 66          **     f
29691 6f 72 20 78 2e 0a 20 20 20 20 20 20 20 20 2a 2a  or x..        **
29692 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2b 20  .        **   + 
29693 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 63 6f  The optimizer co
29694 64 65 20 69 6e 20 77 68 65 72 65 2e 63 20 28 74  de in where.c (t
29695 68 65 20 74 68 69 6e 67 20 74 68 61 74 20 64 65  he thing that de
29696 63 69 64 65 73 20 77 68 69 63 68 0a 20 20 20 20  cides which.    
29697 20 20 20 20 2a 2a 20 20 20 20 20 69 6e 64 65 78      **     index
29698 20 6f 72 20 69 6e 64 69 63 65 73 20 74 6f 20 75   or indices to u
29699 73 65 29 20 73 68 6f 75 6c 64 20 70 6c 61 63 65  se) should place
2969a 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 72 69   a different pri
2969b 6f 72 69 74 79 20 6f 6e 20 0a 20 20 20 20 20 20  ority on .      
2969c 20 20 2a 2a 20 20 20 20 20 73 61 74 69 73 66 79    **     satisfy
2969d 69 6e 67 20 74 68 65 20 27 4f 52 44 45 52 20 42  ing the 'ORDER B
2969e 59 27 20 63 6c 61 75 73 65 20 74 68 61 6e 20 69  Y' clause than i
2969f 74 20 64 6f 65 73 20 69 6e 20 6f 74 68 65 72 20  t does in other 
296a0 63 61 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a  cases..        *
296a1 2a 20 20 20 20 20 52 65 66 65 72 20 74 6f 20 63  *     Refer to c
296a2 6f 64 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73  ode and comments
296a3 20 69 6e 20 77 68 65 72 65 2e 63 20 66 6f 72 20   in where.c for 
296a4 64 65 74 61 69 6c 73 2e 0a 20 20 20 20 20 20 20  details..       
296a5 20 2a 2f 0a 20 20 20 20 20 20 20 20 45 78 70 72   */.        Expr
296a6 4c 69 73 74 20 2a 70 4d 69 6e 4d 61 78 20 3d 20  List *pMinMax = 
296a7 30 3b 0a 20 20 20 20 20 20 20 20 75 38 20 66 6c  0;.        u8 fl
296a8 61 67 20 3d 20 6d 69 6e 4d 61 78 51 75 65 72 79  ag = minMaxQuery
296a9 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  (p);.        if(
296aa 20 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20   flag ){.       
296ab 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
296ac 48 61 73 50 72 6f 70 65 72 74 79 28 70 2d 3e 70  HasProperty(p->p
296ad 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
296ae 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
296af 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d   );.          pM
296b0 69 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 45  inMax = sqlite3E
296b1 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
296b2 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  ->pEList->a[0].p
296b3 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 30 29  Expr->x.pList,0)
296b4 3b 0a 20 20 20 20 20 20 20 20 20 20 70 44 65 6c  ;.          pDel
296b5 20 3d 20 70 4d 69 6e 4d 61 78 3b 0a 20 20 20 20   = pMinMax;.    
296b6 20 20 20 20 20 20 69 66 28 20 70 4d 69 6e 4d 61        if( pMinMa
296b7 78 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  x && !db->malloc
296b8 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
296b9 20 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61        pMinMax->a
296ba 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20  [0].sortOrder = 
296bb 66 6c 61 67 21 3d 57 48 45 52 45 5f 4f 52 44 45  flag!=WHERE_ORDE
296bc 52 42 59 5f 4d 49 4e 20 3f 31 3a 30 3b 0a 20 20  RBY_MIN ?1:0;.  
296bd 20 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d 61            pMinMa
296be 78 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f  x->a[0].pExpr->o
296bf 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20  p = TK_COLUMN;. 
296c0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
296c1 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20     }.  .        
296c2 2f 2a 20 54 68 69 73 20 63 61 73 65 20 72 75 6e  /* This case run
296c3 73 20 69 66 20 74 68 65 20 61 67 67 72 65 67 61  s if the aggrega
296c4 74 65 20 68 61 73 20 6e 6f 20 47 52 4f 55 50 20  te has no GROUP 
296c5 42 59 20 63 6c 61 75 73 65 2e 20 20 54 68 65 0a  BY clause.  The.
296c6 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65          ** proce
296c7 73 73 69 6e 67 20 69 73 20 6d 75 63 68 20 73 69  ssing is much si
296c8 6d 70 6c 65 72 20 73 69 6e 63 65 20 74 68 65 72  mpler since ther
296c9 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67  e is only a sing
296ca 6c 65 20 72 6f 77 0a 20 20 20 20 20 20 20 20 2a  le row.        *
296cb 2a 20 6f 66 20 6f 75 74 70 75 74 2e 0a 20 20 20  * of output..   
296cc 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
296cd 72 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72  resetAccumulator
296ce 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
296cf 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 57 49  fo);.        pWI
296d0 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65  nfo = sqlite3Whe
296d1 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20  reBegin(pParse, 
296d2 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65  pTabList, pWhere
296d3 2c 20 26 70 4d 69 6e 4d 61 78 2c 20 66 6c 61 67  , &pMinMax, flag
296d4 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
296d5 57 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20  WInfo==0 ){.    
296d6 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
296d7 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
296d8 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 20  pDel);.         
296d9 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
296da 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
296db 20 20 20 20 75 70 64 61 74 65 41 63 63 75 6d 75      updateAccumu
296dc 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73  lator(pParse, &s
296dd 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  AggInfo);.      
296de 20 20 69 66 28 20 21 70 4d 69 6e 4d 61 78 20 26    if( !pMinMax &
296df 26 20 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20  & flag ){.      
296e0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
296e1 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
296e2 2c 20 30 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72  , 0, pWInfo->iBr
296e3 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20  eak);.          
296e4 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
296e5 22 25 73 28 29 20 62 79 20 69 6e 64 65 78 22 2c  "%s() by index",
296e6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
296e7 20 28 66 6c 61 67 3d 3d 57 48 45 52 45 5f 4f 52   (flag==WHERE_OR
296e8 44 45 52 42 59 5f 4d 49 4e 3f 22 6d 69 6e 22 3a  DERBY_MIN?"min":
296e9 22 6d 61 78 22 29 29 29 3b 0a 20 20 20 20 20 20  "max")));.      
296ea 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
296eb 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
296ec 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 66 69 6e  fo);.        fin
296ed 61 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e  alizeAggFunction
296ee 73 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49  s(pParse, &sAggI
296ef 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  nfo);.      }.. 
296f0 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20       pOrderBy = 
296f1 30 3b 0a 20 20 20 20 20 20 69 66 28 20 70 48 61  0;.      if( pHa
296f2 76 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20  ving ){.        
296f3 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c  sqlite3ExprIfFal
296f4 73 65 28 70 50 61 72 73 65 2c 20 70 48 61 76 69  se(pParse, pHavi
296f5 6e 67 2c 20 61 64 64 72 45 6e 64 2c 20 53 51 4c  ng, addrEnd, SQL
296f6 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
296f7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
296f8 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70  electInnerLoop(p
296f9 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c  Parse, p, p->pEL
296fa 69 73 74 2c 20 30 2c 20 30 2c 20 30 2c 20 2d 31  ist, 0, 0, 0, -1
296fb 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
296fc 20 20 20 20 20 20 20 20 20 70 44 65 73 74 2c 20           pDest, 
296fd 61 64 64 72 45 6e 64 2c 20 61 64 64 72 45 6e 64  addrEnd, addrEnd
296fe 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
296ff 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
29700 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 7d 0a  b, pDel);.    }.
29701 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
29702 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61  esolveLabel(v, a
29703 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 0a 20 20  ddrEnd);.    .  
29704 7d 20 2f 2a 20 65 6e 64 69 66 20 61 67 67 72 65  } /* endif aggre
29705 67 61 74 65 20 71 75 65 72 79 20 2a 2f 0a 0a 20  gate query */.. 
29706 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
29707 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
29708 73 65 2c 20 74 68 65 6e 20 77 65 20 6e 65 65 64  se, then we need
29709 20 74 6f 20 73 6f 72 74 20 74 68 65 20 72 65 73   to sort the res
2970a 75 6c 74 73 0a 20 20 2a 2a 20 61 6e 64 20 73 65  ults.  ** and se
2970b 6e 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20 63  nd them to the c
2970c 61 6c 6c 62 61 63 6b 20 6f 6e 65 20 62 79 20 6f  allback one by o
2970d 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ne..  */.  if( p
2970e 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 67  OrderBy ){.    g
2970f 65 6e 65 72 61 74 65 53 6f 72 74 54 61 69 6c 28  enerateSortTail(
29710 70 50 61 72 73 65 2c 20 70 2c 20 76 2c 20 70 45  pParse, p, v, pE
29711 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 44 65  List->nExpr, pDe
29712 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a  st);.  }..  /* J
29713 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69 70  ump here to skip
29714 20 74 68 69 73 20 71 75 65 72 79 0a 20 20 2a 2f   this query.  */
29715 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
29716 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 45  solveLabel(v, iE
29717 6e 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 53  nd);..  /* The S
29718 45 4c 45 43 54 20 77 61 73 20 73 75 63 63 65 73  ELECT was succes
29719 73 66 75 6c 6c 79 20 63 6f 64 65 64 2e 20 20 20  sfully coded.   
2971a 53 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63  Set the return c
2971b 6f 64 65 20 74 6f 20 30 0a 20 20 2a 2a 20 74 6f  ode to 0.  ** to
2971c 20 69 6e 64 69 63 61 74 65 20 6e 6f 20 65 72 72   indicate no err
2971d 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ors..  */.  rc =
2971e 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 74 72 6f   0;..  /* Contro
2971f 6c 20 6a 75 6d 70 73 20 74 6f 20 68 65 72 65 20  l jumps to here 
29720 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65  if an error is e
29721 6e 63 6f 75 6e 74 65 72 65 64 20 61 62 6f 76 65  ncountered above
29722 2c 20 6f 72 20 75 70 6f 6e 0a 20 20 2a 2a 20 73  , or upon.  ** s
29723 75 63 63 65 73 73 66 75 6c 20 63 6f 64 69 6e 67  uccessful coding
29724 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a   of the SELECT..
29725 20 20 2a 2f 0a 73 65 6c 65 63 74 5f 65 6e 64 3a    */.select_end:
29726 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66 79 20  ..  /* Identify 
29727 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 66 20  column names if 
29728 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53  results of the S
29729 45 4c 45 43 54 20 61 72 65 20 74 6f 20 62 65 20  ELECT are to be 
2972a 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 69  output..  */.  i
2972b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2972c 20 26 26 20 70 44 65 73 74 2d 3e 65 44 65 73 74   && pDest->eDest
2972d 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a  ==SRT_Output ){.
2972e 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75      generateColu
2972f 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
29730 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69 73 74  pTabList, pEList
29731 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  );.  }..  sqlite
29732 33 44 62 46 72 65 65 28 64 62 2c 20 73 41 67 67  3DbFree(db, sAgg
29733 49 6e 66 6f 2e 61 43 6f 6c 29 3b 0a 20 20 73 71  Info.aCol);.  sq
29734 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
29735 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 29 3b  sAggInfo.aFunc);
29736 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
29737 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
29738 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a  ITE_DEBUG)./*.**
29739 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2973a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2973b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2973c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2973d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2973e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
2973f 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ode is used for 
29740 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
29741 67 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68 65  gging only.  The
29742 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 66 6f   code.** that fo
29743 6c 6c 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61  llows does not a
29744 70 70 65 61 72 20 69 6e 20 6e 6f 72 6d 61 6c 20  ppear in normal 
29745 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  builds..**.** Th
29746 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
29747 20 75 73 65 64 20 74 6f 20 70 72 69 6e 74 20 6f   used to print o
29748 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ut the content o
29749 66 20 61 6c 6c 20 6f 72 20 70 61 72 74 20 6f 66  f all or part of
2974a 20 61 20 0a 2a 2a 20 70 61 72 73 65 20 73 74 72   a .** parse str
2974b 75 63 74 75 72 65 73 20 73 75 63 68 20 61 73 20  uctures such as 
2974c 53 65 6c 65 63 74 20 6f 72 20 45 78 70 72 2e 20  Select or Expr. 
2974d 20 53 75 63 68 20 70 72 69 6e 74 6f 75 74 73 20   Such printouts 
2974e 61 72 65 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f  are useful.** fo
2974f 72 20 68 65 6c 70 69 6e 67 20 74 6f 20 75 6e 64  r helping to und
29750 65 72 73 74 61 6e 64 20 77 68 61 74 20 69 73 20  erstand what is 
29751 68 61 70 70 65 6e 69 6e 67 20 69 6e 73 69 64 65  happening inside
29752 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
29753 74 6f 72 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68  tor.** during th
29754 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 63  e execution of c
29755 6f 6d 70 6c 65 78 20 53 45 4c 45 43 54 20 73 74  omplex SELECT st
29756 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
29757 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20 61 72  These routine ar
29758 65 20 6e 6f 74 20 63 61 6c 6c 65 64 20 61 6e 79  e not called any
29759 77 68 65 72 65 20 66 72 6f 6d 20 77 69 74 68 69  where from withi
2975a 6e 20 74 68 65 20 6e 6f 72 6d 61 6c 0a 2a 2a 20  n the normal.** 
2975b 63 6f 64 65 20 62 61 73 65 2e 20 20 54 68 65 6e  code base.  Then
2975c 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f   are intended to
2975d 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
2975e 77 69 74 68 69 6e 20 74 68 65 20 64 65 62 75 67  within the debug
2975f 67 65 72 0a 2a 2a 20 6f 72 20 66 72 6f 6d 20 74  ger.** or from t
29760 65 6d 70 6f 72 61 72 79 20 22 70 72 69 6e 74 66  emporary "printf
29761 22 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73  " statements ins
29762 65 72 74 65 64 20 66 6f 72 20 64 65 62 75 67 67  erted for debugg
29763 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
29764 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
29765 74 65 33 50 72 69 6e 74 45 78 70 72 28 45 78 70  te3PrintExpr(Exp
29766 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  r *p){.  if( p->
29767 74 6f 6b 65 6e 2e 7a 20 26 26 20 70 2d 3e 74 6f  token.z && p->to
29768 6b 65 6e 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 73  ken.n>0 ){.    s
29769 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2976a 66 28 22 28 25 2e 2a 73 22 2c 20 70 2d 3e 74 6f  f("(%.*s", p->to
2976b 6b 65 6e 2e 6e 2c 20 70 2d 3e 74 6f 6b 65 6e 2e  ken.n, p->token.
2976c 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  z);.  }else{.   
2976d 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2976e 6e 74 66 28 22 28 25 64 22 2c 20 70 2d 3e 6f 70  ntf("(%d", p->op
2976f 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
29770 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 71 6c  pLeft ){.    sql
29771 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
29772 22 20 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  " ");.    sqlite
29773 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 4c  3PrintExpr(p->pL
29774 65 66 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  eft);.  }.  if( 
29775 70 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20  p->pRight ){.   
29776 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
29777 6e 74 66 28 22 20 22 29 3b 0a 20 20 20 20 73 71  ntf(" ");.    sq
29778 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70  lite3PrintExpr(p
29779 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20  ->pRight);.  }. 
2977a 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2977b 6e 74 66 28 22 29 22 29 3b 0a 7d 0a 53 51 4c 49  ntf(")");.}.SQLI
2977c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2977d 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
2977e 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70  List(ExprList *p
2977f 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  List){.  int i;.
29780 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
29781 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
29782 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e  .    sqlite3Prin
29783 74 45 78 70 72 28 70 4c 69 73 74 2d 3e 61 5b 69  tExpr(pList->a[i
29784 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66  ].pExpr);.    if
29785 28 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ( i<pList->nExpr
29786 2d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  -1 ){.      sqli
29787 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
29788 2c 20 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , ");.    }.  }.
29789 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
2978a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 69   void sqlite3Pri
2978b 6e 74 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20  ntSelect(Select 
2978c 2a 70 2c 20 69 6e 74 20 69 6e 64 65 6e 74 29 7b  *p, int indent){
2978d 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  .  sqlite3DebugP
2978e 72 69 6e 74 66 28 22 25 2a 73 53 45 4c 45 43 54  rintf("%*sSELECT
2978f 28 25 70 29 20 22 2c 20 69 6e 64 65 6e 74 2c 20  (%p) ", indent, 
29790 22 22 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65  "", p);.  sqlite
29791 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70  3PrintExprList(p
29792 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c  ->pEList);.  sql
29793 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
29794 22 5c 6e 22 29 3b 0a 20 20 69 66 28 20 70 2d 3e  "\n");.  if( p->
29795 70 53 72 63 20 29 7b 0a 20 20 20 20 63 68 61 72  pSrc ){.    char
29796 20 2a 7a 50 72 65 66 69 78 3b 0a 20 20 20 20 69   *zPrefix;.    i
29797 6e 74 20 69 3b 0a 20 20 20 20 7a 50 72 65 66 69  nt i;.    zPrefi
29798 78 20 3d 20 22 46 52 4f 4d 22 3b 0a 20 20 20 20  x = "FROM";.    
29799 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53  for(i=0; i<p->pS
2979a 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  rc->nSrc; i++){.
2979b 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63        struct Src
2979c 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
2979d 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69   = &p->pSrc->a[i
2979e 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ];.      sqlite3
2979f 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73  DebugPrintf("%*s
297a0 20 22 2c 20 69 6e 64 65 6e 74 2b 36 2c 20 7a 50   ", indent+6, zP
297a1 72 65 66 69 78 29 3b 0a 20 20 20 20 20 20 7a 50  refix);.      zP
297a2 72 65 66 69 78 20 3d 20 22 22 3b 0a 20 20 20 20  refix = "";.    
297a3 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65    if( pItem->pSe
297a4 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  lect ){.        
297a5 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
297a6 74 66 28 22 28 5c 6e 22 29 3b 0a 20 20 20 20 20  tf("(\n");.     
297a7 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 53     sqlite3PrintS
297a8 65 6c 65 63 74 28 70 49 74 65 6d 2d 3e 70 53 65  elect(pItem->pSe
297a9 6c 65 63 74 2c 20 69 6e 64 65 6e 74 2b 31 30 29  lect, indent+10)
297aa 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
297ab 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a  3DebugPrintf("%*
297ac 73 29 22 2c 20 69 6e 64 65 6e 74 2b 38 2c 20 22  s)", indent+8, "
297ad 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  ");.      }else 
297ae 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  if( pItem->zName
297af 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
297b0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
297b1 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  %s", pItem->zNam
297b2 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
297b3 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 54 61    if( pItem->pTa
297b4 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  b ){.        sql
297b5 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
297b6 22 28 74 61 62 6c 65 3a 20 25 73 29 22 2c 20 70  "(table: %s)", p
297b7 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  Item->pTab->zNam
297b8 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
297b9 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c    if( pItem->zAl
297ba 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ias ){.        s
297bb 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
297bc 66 28 22 20 41 53 20 25 73 22 2c 20 70 49 74 65  f(" AS %s", pIte
297bd 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20  m->zAlias);.    
297be 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3c    }.      if( i<
297bf 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 2d 31 20  p->pSrc->nSrc-1 
297c0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
297c1 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 2c  e3DebugPrintf(",
297c2 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ");.      }.    
297c3 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
297c4 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 20 20  intf("\n");.    
297c5 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  }.  }.  if( p->p
297c6 57 68 65 72 65 20 29 7b 0a 20 20 20 20 73 71 6c  Where ){.    sql
297c7 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
297c8 22 25 2a 73 20 57 48 45 52 45 20 22 2c 20 69 6e  "%*s WHERE ", in
297c9 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73  dent, "");.    s
297ca 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28  qlite3PrintExpr(
297cb 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20  p->pWhere);.    
297cc 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
297cd 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20  tf("\n");.  }.  
297ce 69 66 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 20  if( p->pGroupBy 
297cf 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
297d0 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 47  bugPrintf("%*s G
297d1 52 4f 55 50 20 42 59 20 22 2c 20 69 6e 64 65 6e  ROUP BY ", inden
297d2 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69  t, "");.    sqli
297d3 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74  te3PrintExprList
297d4 28 70 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20  (p->pGroupBy);. 
297d5 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
297d6 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d  rintf("\n");.  }
297d7 0a 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e  .  if( p->pHavin
297d8 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  g ){.    sqlite3
297d9 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73  DebugPrintf("%*s
297da 20 48 41 56 49 4e 47 20 22 2c 20 69 6e 64 65 6e   HAVING ", inden
297db 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69  t, "");.    sqli
297dc 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e  te3PrintExpr(p->
297dd 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 73 71  pHaving);.    sq
297de 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
297df 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66  ("\n");.  }.  if
297e0 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b  ( p->pOrderBy ){
297e1 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
297e2 67 50 72 69 6e 74 66 28 22 25 2a 73 20 4f 52 44  gPrintf("%*s ORD
297e3 45 52 20 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c  ER BY ", indent,
297e4 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   "");.    sqlite
297e5 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70  3PrintExprList(p
297e6 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20  ->pOrderBy);.   
297e7 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
297e8 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 7d  ntf("\n");.  }.}
297e9 0a 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 73  ./* End of the s
297ea 74 72 75 63 74 75 72 65 20 64 65 62 75 67 20 70  tructure debug p
297eb 72 69 6e 74 69 6e 67 20 63 6f 64 65 0a 2a 2a 2a  rinting code.***
297ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64  **********/.#end
297f1 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51  if /* defined(SQ
297f2 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65  LITE_TEST) || de
297f3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
297f4 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  UG) */../*******
297f5 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73  ******* End of s
297f6 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  elect.c ********
297f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297f9 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
297fa 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
297fb 6c 65 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a  le table.c *****
297fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297fe 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
297ff 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
29800 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
29801 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
29802 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
29803 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
29804 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
29805 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
29806 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
29807 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
29808 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
29809 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2980a 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2980b 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2980c 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2980d 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2980e 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2980f 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
29810 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
29811 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
29812 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29813 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29814 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29815 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
29816 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
29817 61 69 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33  ains the sqlite3
29818 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 61 6e 64  _get_table() and
29819 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
2981a 62 6c 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61  ble().** interfa
2981b 63 65 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68  ce routines.  Th
2981c 65 73 65 20 61 72 65 20 6a 75 73 74 20 77 72 61  ese are just wra
2981d 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68 65  ppers around the
2981e 20 6d 61 69 6e 0a 2a 2a 20 69 6e 74 65 72 66 61   main.** interfa
2981f 63 65 20 72 6f 75 74 69 6e 65 20 6f 66 20 73 71  ce routine of sq
29820 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a  lite3_exec()..**
29821 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
29822 65 73 20 61 72 65 20 69 6e 20 61 20 73 65 70 61  es are in a sepa
29823 72 61 74 65 20 66 69 6c 65 73 20 73 6f 20 74 68  rate files so th
29824 61 74 20 74 68 65 79 20 77 69 6c 6c 20 6e 6f 74  at they will not
29825 20 62 65 20 6c 69 6e 6b 65 64 0a 2a 2a 20 69 66   be linked.** if
29826 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 75 73   they are not us
29827 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74  ed..**.** $Id: t
29828 61 62 6c 65 2e 63 2c 76 20 31 2e 34 30 20 32 30  able.c,v 1.40 20
29829 30 39 2f 30 34 2f 31 30 20 31 34 3a 32 38 3a 30  09/04/10 14:28:0
2982a 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  0 drh Exp $.*/..
2982b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2982c 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0a 0a 2f  MIT_GET_TABLE../
2982d 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74  *.** This struct
2982e 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 70  ure is used to p
2982f 61 73 73 20 64 61 74 61 20 66 72 6f 6d 20 73 71  ass data from sq
29830 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
29831 29 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20  ) through.** to 
29832 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  the callback fun
29833 63 74 69 6f 6e 20 69 73 20 75 73 65 73 20 74 6f  ction is uses to
29834 20 62 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c   build the resul
29835 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
29836 72 75 63 74 20 54 61 62 52 65 73 75 6c 74 20 7b  ruct TabResult {
29837 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65 73 75  .  char **azResu
29838 6c 74 3b 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c  lt;   /* Accumul
29839 61 74 65 64 20 6f 75 74 70 75 74 20 2a 2f 0a 20  ated output */. 
2983a 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20   char *zErrMsg; 
2983b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73      /* Error mes
2983c 73 61 67 65 20 74 65 78 74 2c 20 69 66 20 61 6e  sage text, if an
2983d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 2a 2f   error occurs */
2983e 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20  .  int nAlloc;  
2983f 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 61        /* Slots a
29840 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 7a 52  llocated for azR
29841 65 73 75 6c 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74  esult[] */.  int
29842 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20   nRow;          
29843 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77  /* Number of row
29844 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
29845 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e  */.  int nColumn
29846 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
29847 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
29848 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20  the result */.  
29849 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20  int nData;      
2984a 20 20 20 2f 2a 20 53 6c 6f 74 73 20 75 73 65 64     /* Slots used
2984b 20 69 6e 20 61 7a 52 65 73 75 6c 74 5b 5d 2e 20   in azResult[]. 
2984c 20 28 6e 52 6f 77 2b 31 29 2a 6e 43 6f 6c 75 6d   (nRow+1)*nColum
2984d 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  n */.  int rc;  
2984e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
2984f 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 71  urn code from sq
29850 6c 69 74 65 33 5f 65 78 65 63 28 29 20 2a 2f 0a  lite3_exec() */.
29851 7d 20 54 61 62 52 65 73 75 6c 74 3b 0a 0a 2f 2a  } TabResult;../*
29852 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
29853 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   is called once 
29854 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e 20  for each row in 
29855 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
29856 2e 20 20 49 74 73 20 6a 6f 62 0a 2a 2a 20 69 73  .  Its job.** is
29857 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20   to fill in the 
29858 54 61 62 52 65 73 75 6c 74 20 73 74 72 75 63 74  TabResult struct
29859 75 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c  ure appropriatel
2985a 79 2c 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 65  y, allocating ne
2985b 77 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 73 20 6e  w.** memory as n
2985c 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61  ecessary..*/.sta
2985d 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  tic int sqlite3_
2985e 67 65 74 5f 74 61 62 6c 65 5f 63 62 28 76 6f 69  get_table_cb(voi
2985f 64 20 2a 70 41 72 67 2c 20 69 6e 74 20 6e 43 6f  d *pArg, int nCo
29860 6c 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20  l, char **argv, 
29861 63 68 61 72 20 2a 2a 63 6f 6c 76 29 7b 0a 20 20  char **colv){.  
29862 54 61 62 52 65 73 75 6c 74 20 2a 70 20 3d 20 28  TabResult *p = (
29863 54 61 62 52 65 73 75 6c 74 2a 29 70 41 72 67 3b  TabResult*)pArg;
29864 20 20 2f 2a 20 52 65 73 75 6c 74 20 61 63 63 75    /* Result accu
29865 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  mulator */.  int
29866 20 6e 65 65 64 3b 20 20 20 20 20 20 20 20 20 20   need;          
29867 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
29868 2a 20 53 6c 6f 74 73 20 6e 65 65 64 65 64 20 69  * Slots needed i
29869 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 5d 20  n p->azResult[] 
2986a 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
2986b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2986c 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2986d 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72  ounter */.  char
2986e 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
2986f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29870 20 41 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e   A single column
29871 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 20   of result */.. 
29872 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
29873 65 72 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70  ere is enough sp
29874 61 63 65 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75  ace in p->azResu
29875 6c 74 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79  lt to hold every
29876 74 68 69 6e 67 0a 20 20 2a 2a 20 77 65 20 6e 65  thing.  ** we ne
29877 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 66  ed to remember f
29878 72 6f 6d 20 74 68 69 73 20 69 6e 76 6f 63 61 74  rom this invocat
29879 69 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 62  ion of the callb
2987a 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ack..  */.  if( 
2987b 70 2d 3e 6e 52 6f 77 3d 3d 30 20 26 26 20 61 72  p->nRow==0 && ar
2987c 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 65  gv!=0 ){.    nee
2987d 64 20 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20 7d 65  d = nCol*2;.  }e
2987e 6c 73 65 7b 0a 20 20 20 20 6e 65 65 64 20 3d 20  lse{.    need = 
2987f 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  nCol;.  }.  if( 
29880 70 2d 3e 6e 44 61 74 61 20 2b 20 6e 65 65 64 20  p->nData + need 
29881 3e 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  > p->nAlloc ){. 
29882 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b     char **azNew;
29883 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d  .    p->nAlloc =
29884 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 6e   p->nAlloc*2 + n
29885 65 65 64 3b 0a 20 20 20 20 61 7a 4e 65 77 20 3d  eed;.    azNew =
29886 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
29887 28 20 70 2d 3e 61 7a 52 65 73 75 6c 74 2c 20 73  ( p->azResult, s
29888 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 70 2d 3e  izeof(char*)*p->
29889 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 69 66  nAlloc );.    if
2988a 28 20 61 7a 4e 65 77 3d 3d 30 20 29 20 67 6f 74  ( azNew==0 ) got
2988b 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b  o malloc_failed;
2988c 0a 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74  .    p->azResult
2988d 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a 0a 20   = azNew;.  }.. 
2988e 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74   /* If this is t
2988f 68 65 20 66 69 72 73 74 20 72 6f 77 2c 20 74 68  he first row, th
29890 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65  en generate an e
29891 78 74 72 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e  xtra row contain
29892 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d  ing.  ** the nam
29893 65 73 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e  es of all column
29894 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  s..  */.  if( p-
29895 3e 6e 52 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRow==0 ){.    
29896 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f  p->nColumn = nCo
29897 6c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  l;.    for(i=0; 
29898 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
29899 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
2989a 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 63 6f  mprintf("%s", co
2989b 6c 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66  lv[i]);.      if
2989c 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6d 61  ( z==0 ) goto ma
2989d 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 20  lloc_failed;.   
2989e 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70     p->azResult[p
2989f 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a  ->nData++] = z;.
298a0 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
298a1 28 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 6e 43  ( p->nColumn!=nC
298a2 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ol ){.    sqlite
298a3 33 5f 66 72 65 65 28 70 2d 3e 7a 45 72 72 4d 73  3_free(p->zErrMs
298a4 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d  g);.    p->zErrM
298a5 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  sg = sqlite3_mpr
298a6 69 6e 74 66 28 0a 20 20 20 20 20 20 20 22 73 71  intf(.       "sq
298a7 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
298a8 29 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 77  ) called with tw
298a9 6f 20 6f 72 20 6d 6f 72 65 20 69 6e 63 6f 6d 70  o or more incomp
298aa 61 74 69 62 6c 65 20 71 75 65 72 69 65 73 22 0a  atible queries".
298ab 20 20 20 20 29 3b 0a 20 20 20 20 70 2d 3e 72 63      );.    p->rc
298ac 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
298ad 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
298ae 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76   }..  /* Copy ov
298af 65 72 20 74 68 65 20 72 6f 77 20 64 61 74 61 0a  er the row data.
298b0 20 20 2a 2f 0a 20 20 69 66 28 20 61 72 67 76 21    */.  if( argv!
298b1 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  =0 ){.    for(i=
298b2 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
298b3 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 76 5b  .      if( argv[
298b4 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  i]==0 ){.       
298b5 20 7a 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65   z = 0;.      }e
298b6 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  lse{.        int
298b7 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
298b8 65 6e 33 30 28 61 72 67 76 5b 69 5d 29 2b 31 3b  en30(argv[i])+1;
298b9 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 73 71 6c  .        z = sql
298ba 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 20 29  ite3_malloc( n )
298bb 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 3d  ;.        if( z=
298bc 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63  =0 ) goto malloc
298bd 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20  _failed;.       
298be 20 6d 65 6d 63 70 79 28 7a 2c 20 61 72 67 76 5b   memcpy(z, argv[
298bf 69 5d 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  i], n);.      }.
298c0 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c        p->azResul
298c1 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20  t[p->nData++] = 
298c2 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  z;.    }.    p->
298c3 6e 52 6f 77 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65  nRow++;.  }.  re
298c4 74 75 72 6e 20 30 3b 0a 0a 6d 61 6c 6c 6f 63 5f  turn 0;..malloc_
298c5 66 61 69 6c 65 64 3a 0a 20 20 70 2d 3e 72 63 20  failed:.  p->rc 
298c6 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
298c7 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
298c8 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 64  *.** Query the d
298c9 61 74 61 62 61 73 65 2e 20 20 42 75 74 20 69 6e  atabase.  But in
298ca 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e  stead of invokin
298cb 67 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72  g a callback for
298cc 20 65 61 63 68 20 72 6f 77 2c 0a 2a 2a 20 6d 61   each row,.** ma
298cd 6c 6c 6f 63 28 29 20 66 6f 72 20 73 70 61 63 65  lloc() for space
298ce 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73   to hold the res
298cf 75 6c 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ult and return t
298d0 68 65 20 65 6e 74 69 72 65 20 72 65 73 75 6c 74  he entire result
298d1 73 0a 2a 2a 20 61 74 20 74 68 65 20 63 6f 6e 63  s.** at the conc
298d2 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 61  lusion of the ca
298d3 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ll..**.** The re
298d4 73 75 6c 74 20 74 68 61 74 20 69 73 20 77 72 69  sult that is wri
298d5 74 74 65 6e 20 74 6f 20 2a 2a 2a 70 61 7a 52 65  tten to ***pazRe
298d6 73 75 6c 74 20 69 73 20 68 65 6c 64 20 69 6e 20  sult is held in 
298d7 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a  memory obtained.
298d8 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ** from malloc()
298d9 2e 20 20 42 75 74 20 74 68 65 20 63 61 6c 6c 65  .  But the calle
298da 72 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 74 68  r cannot free th
298db 69 73 20 6d 65 6d 6f 72 79 20 64 69 72 65 63 74  is memory direct
298dc 6c 79 2e 20 20 0a 2a 2a 20 49 6e 73 74 65 61 64  ly.  .** Instead
298dd 2c 20 74 68 65 20 65 6e 74 69 72 65 20 74 61 62  , the entire tab
298de 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  le should be pas
298df 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 66  sed to sqlite3_f
298e0 72 65 65 5f 74 61 62 6c 65 28 29 20 77 68 65 6e  ree_table() when
298e1 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  .** the calling 
298e2 70 72 6f 63 65 64 75 72 65 20 69 73 20 66 69 6e  procedure is fin
298e3 69 73 68 65 64 20 75 73 69 6e 67 20 69 74 2e 0a  ished using it..
298e4 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
298e5 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  t sqlite3_get_ta
298e6 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ble(.  sqlite3 *
298e7 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
298e8 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
298e9 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  se on which the 
298ea 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a  SQL executes */.
298eb 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
298ec 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ql,           /*
298ed 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65   The SQL to be e
298ee 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 63 68 61  xecuted */.  cha
298ef 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20  r ***pazResult, 
298f0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
298f1 65 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62  e the result tab
298f2 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  le here */.  int
298f3 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 20   *pnRow,        
298f4 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
298f5 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
298f6 72 6f 77 73 20 69 6e 20 74 68 65 20 72 65 73 75  rows in the resu
298f7 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  lt here */.  int
298f8 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20   *pnColumn,     
298f9 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
298fa 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
298fb 63 6f 6c 75 6d 6e 73 20 6f 66 20 72 65 73 75 6c  columns of resul
298fc 74 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72  t here */.  char
298fd 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20   **pzErrMsg     
298fe 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
298ff 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
29900 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
29901 20 72 63 3b 0a 20 20 54 61 62 52 65 73 75 6c 74   rc;.  TabResult
29902 20 72 65 73 3b 0a 0a 20 20 2a 70 61 7a 52 65 73   res;..  *pazRes
29903 75 6c 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  ult = 0;.  if( p
29904 6e 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c  nColumn ) *pnCol
29905 75 6d 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  umn = 0;.  if( p
29906 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20  nRow ) *pnRow = 
29907 30 3b 0a 20 20 69 66 28 20 70 7a 45 72 72 4d 73  0;.  if( pzErrMs
29908 67 20 29 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20  g ) *pzErrMsg = 
29909 30 3b 0a 20 20 72 65 73 2e 7a 45 72 72 4d 73 67  0;.  res.zErrMsg
2990a 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 52 6f 77   = 0;.  res.nRow
2990b 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 43 6f 6c   = 0;.  res.nCol
2990c 75 6d 6e 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e  umn = 0;.  res.n
2990d 44 61 74 61 20 3d 20 31 3b 0a 20 20 72 65 73 2e  Data = 1;.  res.
2990e 6e 41 6c 6c 6f 63 20 3d 20 32 30 3b 0a 20 20 72  nAlloc = 20;.  r
2990f 65 73 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  es.rc = SQLITE_O
29910 4b 3b 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c  K;.  res.azResul
29911 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  t = sqlite3_mall
29912 6f 63 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29  oc(sizeof(char*)
29913 2a 72 65 73 2e 6e 41 6c 6c 6f 63 20 29 3b 0a 20  *res.nAlloc );. 
29914 20 69 66 28 20 72 65 73 2e 61 7a 52 65 73 75 6c   if( res.azResul
29915 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 64 62 2d  t==0 ){.     db-
29916 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54  >errCode = SQLIT
29917 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 72 65  E_NOMEM;.     re
29918 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
29919 4d 3b 0a 20 20 7d 0a 20 20 72 65 73 2e 61 7a 52  M;.  }.  res.azR
2991a 65 73 75 6c 74 5b 30 5d 20 3d 20 30 3b 0a 20 20  esult[0] = 0;.  
2991b 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
2991c 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69  c(db, zSql, sqli
2991d 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62  te3_get_table_cb
2991e 2c 20 26 72 65 73 2c 20 70 7a 45 72 72 4d 73 67  , &res, pzErrMsg
2991f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  );.  assert( siz
29920 65 6f 66 28 72 65 73 2e 61 7a 52 65 73 75 6c 74  eof(res.azResult
29921 5b 30 5d 29 3e 3d 20 73 69 7a 65 6f 66 28 72 65  [0])>= sizeof(re
29922 73 2e 6e 44 61 74 61 29 20 29 3b 0a 20 20 72 65  s.nData) );.  re
29923 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20  s.azResult[0] = 
29924 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54  SQLITE_INT_TO_PT
29925 52 28 72 65 73 2e 6e 44 61 74 61 29 3b 0a 20 20  R(res.nData);.  
29926 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53  if( (rc&0xff)==S
29927 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20  QLITE_ABORT ){. 
29928 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f     sqlite3_free_
29929 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73  table(&res.azRes
2992a 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28  ult[1]);.    if(
2992b 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 29 7b 0a   res.zErrMsg ){.
2992c 20 20 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d        if( pzErrM
2992d 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  sg ){.        sq
2992e 6c 69 74 65 33 5f 66 72 65 65 28 2a 70 7a 45 72  lite3_free(*pzEr
2992f 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a  rMsg);.        *
29930 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
29931 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
29932 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  res.zErrMsg);.  
29933 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
29934 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72  te3_free(res.zEr
29935 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rMsg);.    }.   
29936 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72   db->errCode = r
29937 65 73 2e 72 63 3b 20 20 2f 2a 20 41 73 73 75 6d  es.rc;  /* Assum
29938 65 20 33 32 2d 62 69 74 20 61 73 73 69 67 6e 6d  e 32-bit assignm
29939 65 6e 74 20 69 73 20 61 74 6f 6d 69 63 20 2a 2f  ent is atomic */
2993a 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65 73 2e  .    return res.
2993b 72 63 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  rc;.  }.  sqlite
2993c 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d  3_free(res.zErrM
2993d 73 67 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  sg);.  if( rc!=S
2993e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2993f 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
29940 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74  le(&res.azResult
29941 5b 31 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  [1]);.    return
29942 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72   rc;.  }.  if( r
29943 65 73 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e 6e 44  es.nAlloc>res.nD
29944 61 74 61 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ata ){.    char 
29945 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20 61 7a 4e  **azNew;.    azN
29946 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
29947 6c 6c 6f 63 28 20 72 65 73 2e 61 7a 52 65 73 75  lloc( res.azResu
29948 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a  lt, sizeof(char*
29949 29 2a 72 65 73 2e 6e 44 61 74 61 20 29 3b 0a 20  )*res.nData );. 
2994a 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20     if( azNew==0 
2994b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2994c 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73  _free_table(&res
2994d 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20  .azResult[1]);. 
2994e 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65       db->errCode
2994f 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
29950 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
29951 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
29952 7d 0a 20 20 20 20 72 65 73 2e 61 7a 52 65 73 75  }.    res.azResu
29953 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a  lt = azNew;.  }.
29954 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20 26    *pazResult = &
29955 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 3b  res.azResult[1];
29956 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20  .  if( pnColumn 
29957 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20 72 65  ) *pnColumn = re
29958 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28  s.nColumn;.  if(
29959 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20   pnRow ) *pnRow 
2995a 3d 20 72 65 73 2e 6e 52 6f 77 3b 0a 20 20 72 65  = res.nRow;.  re
2995b 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2995c 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66  * This routine f
2995d 72 65 65 73 20 74 68 65 20 73 70 61 63 65 20 74  rees the space t
2995e 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  he sqlite3_get_t
2995f 61 62 6c 65 28 29 20 6d 61 6c 6c 6f 63 65 64 2e  able() malloced.
29960 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
29961 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65  oid sqlite3_free
29962 5f 74 61 62 6c 65 28 0a 20 20 63 68 61 72 20 2a  _table(.  char *
29963 2a 61 7a 52 65 73 75 6c 74 20 20 20 20 20 20 20  *azResult       
29964 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72       /* Result r
29965 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 66 72 6f  eturned from fro
29966 6d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  m sqlite3_get_ta
29967 62 6c 65 28 29 20 2a 2f 0a 29 7b 0a 20 20 69 66  ble() */.){.  if
29968 28 20 61 7a 52 65 73 75 6c 74 20 29 7b 0a 20 20  ( azResult ){.  
29969 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20    int i, n;.    
2996a 61 7a 52 65 73 75 6c 74 2d 2d 3b 0a 20 20 20 20  azResult--;.    
2996b 61 73 73 65 72 74 28 20 61 7a 52 65 73 75 6c 74  assert( azResult
2996c 21 3d 30 20 29 3b 0a 20 20 20 20 6e 20 3d 20 53  !=0 );.    n = S
2996d 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
2996e 28 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3b 0a 20  (azResult[0]);. 
2996f 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 3b     for(i=1; i<n;
29970 20 69 2b 2b 29 7b 20 69 66 28 20 61 7a 52 65 73   i++){ if( azRes
29971 75 6c 74 5b 69 5d 20 29 20 73 71 6c 69 74 65 33  ult[i] ) sqlite3
29972 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74 5b 69  _free(azResult[i
29973 5d 29 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65  ]); }.    sqlite
29974 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74 29  3_free(azResult)
29975 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20  ;.  }.}..#endif 
29976 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47  /* SQLITE_OMIT_G
29977 45 54 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a  ET_TABLE */../**
29978 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
29979 20 6f 66 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a   of table.c ****
2997a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2997b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2997c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2997d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2997e 69 6e 20 66 69 6c 65 20 74 72 69 67 67 65 72 2e  in file trigger.
2997f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
29980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29981 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
29982 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
29983 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
29984 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
29985 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
29986 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
29987 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
29988 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
29989 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2998a 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2998b 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2998c 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2998d 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2998e 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2998f 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
29990 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
29991 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
29992 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
29993 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
29994 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29995 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29996 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29997 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
29998 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74 72 69  *.**.** $Id: tri
29999 67 67 65 72 2e 63 2c 76 20 31 2e 31 33 38 20 32  gger.c,v 1.138 2
2999a 30 30 39 2f 30 35 2f 30 36 20 31 38 3a 34 32 3a  009/05/06 18:42:
2999b 32 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  21 drh Exp $.*/.
2999c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2999d 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a 0a  OMIT_TRIGGER./*.
2999e 2a 2a 20 44 65 6c 65 74 65 20 61 20 6c 69 6e 6b  ** Delete a link
2999f 65 64 20 6c 69 73 74 20 6f 66 20 54 72 69 67 67  ed list of Trigg
299a0 65 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65  erStep structure
299a1 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
299a2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
299a3 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74  3DeleteTriggerSt
299a4 65 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ep(sqlite3 *db, 
299a5 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72  TriggerStep *pTr
299a6 69 67 67 65 72 53 74 65 70 29 7b 0a 20 20 77 68  iggerStep){.  wh
299a7 69 6c 65 28 20 70 54 72 69 67 67 65 72 53 74 65  ile( pTriggerSte
299a8 70 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72  p ){.    Trigger
299a9 53 74 65 70 20 2a 20 70 54 6d 70 20 3d 20 70 54  Step * pTmp = pT
299aa 72 69 67 67 65 72 53 74 65 70 3b 0a 20 20 20 20  riggerStep;.    
299ab 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 70  pTriggerStep = p
299ac 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 4e 65  TriggerStep->pNe
299ad 78 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 54 6d  xt;..    if( pTm
299ae 70 2d 3e 74 61 72 67 65 74 2e 64 79 6e 20 29 20  p->target.dyn ) 
299af 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
299b0 2c 20 28 63 68 61 72 2a 29 70 54 6d 70 2d 3e 74  , (char*)pTmp->t
299b1 61 72 67 65 74 2e 7a 29 3b 0a 20 20 20 20 73 71  arget.z);.    sq
299b2 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
299b3 64 62 2c 20 70 54 6d 70 2d 3e 70 57 68 65 72 65  db, pTmp->pWhere
299b4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
299b5 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
299b6 20 70 54 6d 70 2d 3e 70 45 78 70 72 4c 69 73 74   pTmp->pExprList
299b7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  );.    sqlite3Se
299b8 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
299b9 54 6d 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  Tmp->pSelect);. 
299ba 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
299bb 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d  Delete(db, pTmp-
299bc 3e 70 49 64 4c 69 73 74 29 3b 0a 0a 20 20 20 20  >pIdList);..    
299bd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
299be 2c 20 70 54 6d 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  , pTmp);.  }.}..
299bf 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 61 62 6c  /*.** Given tabl
299c0 65 20 70 54 61 62 2c 20 72 65 74 75 72 6e 20 61  e pTab, return a
299c1 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 74 68 65   list of all the
299c2 20 74 72 69 67 67 65 72 73 20 61 74 74 61 63 68   triggers attach
299c3 65 64 20 74 6f 20 0a 2a 2a 20 74 68 65 20 74 61  ed to .** the ta
299c4 62 6c 65 2e 20 54 68 65 20 6c 69 73 74 20 69 73  ble. The list is
299c5 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 54 72   connected by Tr
299c6 69 67 67 65 72 2e 70 4e 65 78 74 20 70 6f 69 6e  igger.pNext poin
299c7 74 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ters..*/.SQLITE_
299c8 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72 20  PRIVATE Trigger 
299c9 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c  *sqlite3TriggerL
299ca 69 73 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  ist(Parse *pPars
299cb 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b  e, Table *pTab){
299cc 0a 20 20 53 63 68 65 6d 61 20 2a 20 63 6f 6e 73  .  Schema * cons
299cd 74 20 70 54 6d 70 53 63 68 65 6d 61 20 3d 20 70  t pTmpSchema = p
299ce 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31  Parse->db->aDb[1
299cf 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 54 72 69  ].pSchema;.  Tri
299d0 67 67 65 72 20 2a 70 4c 69 73 74 20 3d 20 30 3b  gger *pList = 0;
299d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
299d2 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69    /* List of tri
299d3 67 67 65 72 73 20 74 6f 20 72 65 74 75 72 6e 20  ggers to return 
299d4 2a 2f 0a 0a 20 20 69 66 28 20 70 54 6d 70 53 63  */..  if( pTmpSc
299d5 68 65 6d 61 21 3d 70 54 61 62 2d 3e 70 53 63 68  hema!=pTab->pSch
299d6 65 6d 61 20 29 7b 0a 20 20 20 20 48 61 73 68 45  ema ){.    HashE
299d7 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28  lem *p;.    for(
299d8 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  p=sqliteHashFirs
299d9 74 28 26 70 54 6d 70 53 63 68 65 6d 61 2d 3e 74  t(&pTmpSchema->t
299da 72 69 67 48 61 73 68 29 3b 20 70 3b 20 70 3d 73  rigHash); p; p=s
299db 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 29  qliteHashNext(p)
299dc 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72  ){.      Trigger
299dd 20 2a 70 54 72 69 67 20 3d 20 28 54 72 69 67 67   *pTrig = (Trigg
299de 65 72 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44  er *)sqliteHashD
299df 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66  ata(p);.      if
299e0 28 20 70 54 72 69 67 2d 3e 70 54 61 62 53 63 68  ( pTrig->pTabSch
299e1 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65  ema==pTab->pSche
299e2 6d 61 0a 20 20 20 20 20 20 20 26 26 20 30 3d 3d  ma.       && 0==
299e3 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
299e4 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 70 54 61  Trig->table, pTa
299e5 62 2d 3e 7a 4e 61 6d 65 29 20 0a 20 20 20 20 20  b->zName) .     
299e6 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 72 69   ){.        pTri
299e7 67 2d 3e 70 4e 65 78 74 20 3d 20 28 70 4c 69 73  g->pNext = (pLis
299e8 74 20 3f 20 70 4c 69 73 74 20 3a 20 70 54 61 62  t ? pList : pTab
299e9 2d 3e 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20  ->pTrigger);.   
299ea 20 20 20 20 20 70 4c 69 73 74 20 3d 20 70 54 72       pList = pTr
299eb 69 67 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ig;.      }.    
299ec 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
299ed 28 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 20 3a  (pList ? pList :
299ee 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 29   pTab->pTrigger)
299ef 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
299f0 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
299f1 20 70 61 72 73 65 72 20 77 68 65 6e 20 69 74 20   parser when it 
299f2 73 65 65 73 20 61 20 43 52 45 41 54 45 20 54 52  sees a CREATE TR
299f3 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 0a  IGGER statement.
299f4 2a 2a 20 75 70 20 74 6f 20 74 68 65 20 70 6f 69  ** up to the poi
299f5 6e 74 20 6f 66 20 74 68 65 20 42 45 47 49 4e 20  nt of the BEGIN 
299f6 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67  before the trigg
299f7 65 72 20 61 63 74 69 6f 6e 73 2e 20 20 41 20 54  er actions.  A T
299f8 72 69 67 67 65 72 0a 2a 2a 20 73 74 72 75 63 74  rigger.** struct
299f9 75 72 65 20 69 73 20 67 65 6e 65 72 61 74 65 64  ure is generated
299fa 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 69 6e   based on the in
299fb 66 6f 72 6d 61 74 69 6f 6e 20 61 76 61 69 6c 61  formation availa
299fc 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 64 0a 2a  ble and stored.*
299fd 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65  * in pParse->pNe
299fe 77 54 72 69 67 67 65 72 2e 20 20 41 66 74 65 72  wTrigger.  After
299ff 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74   the trigger act
29a00 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70  ions have been p
29a01 61 72 73 65 64 2c 20 74 68 65 0a 2a 2a 20 73 71  arsed, the.** sq
29a02 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67  lite3FinishTrigg
29a03 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  er() function is
29a04 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c   called to compl
29a05 65 74 65 20 74 68 65 20 74 72 69 67 67 65 72 0a  ete the trigger.
29a06 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  ** construction 
29a07 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49  process..*/.SQLI
29a08 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
29a09 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67  sqlite3BeginTrig
29a0a 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ger(.  Parse *pP
29a0b 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  arse,      /* Th
29a0c 65 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20  e parse context 
29a0d 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 52  of the CREATE TR
29a0e 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 20  IGGER statement 
29a0f 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
29a10 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e1,      /* The 
29a11 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67  name of the trig
29a12 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ger */.  Token *
29a13 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20  pName2,      /* 
29a14 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
29a15 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 69 6e 74  trigger */.  int
29a16 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20   tr_tm,         
29a17 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 42 45   /* One of TK_BE
29a18 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45 52 2c 20  FORE, TK_AFTER, 
29a19 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20  TK_INSTEAD */.  
29a1a 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
29a1b 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
29a1c 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50 44 41  _INSERT, TK_UPDA
29a1d 54 45 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f  TE, TK_DELETE */
29a1e 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75  .  IdList *pColu
29a1f 6d 6e 73 2c 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e  mns,   /* column
29a20 20 6c 69 73 74 20 69 66 20 74 68 69 73 20 69 73   list if this is
29a21 20 61 6e 20 55 50 44 41 54 45 20 4f 46 20 74 72   an UPDATE OF tr
29a22 69 67 67 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69  igger */.  SrcLi
29a23 73 74 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 2f  st *pTableName,/
29a24 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
29a25 65 20 74 61 62 6c 65 2f 76 69 65 77 20 74 68 65  e table/view the
29a26 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73   trigger applies
29a27 20 74 6f 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70   to */.  Expr *p
29a28 57 68 65 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20  When,        /* 
29a29 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20  WHEN clause */. 
29a2a 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20   int isTemp,    
29a2b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
29a2c 74 68 65 20 54 45 4d 50 4f 52 41 52 59 20 6b 65  the TEMPORARY ke
29a2d 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74  yword is present
29a2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20   */.  int noErr 
29a2f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 70            /* Sup
29a30 70 72 65 73 73 20 65 72 72 6f 72 73 20 69 66 20  press errors if 
29a31 74 68 65 20 74 72 69 67 67 65 72 20 61 6c 72 65  the trigger alre
29a32 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b  ady exists */.){
29a33 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
29a34 67 67 65 72 20 3d 20 30 3b 0a 20 20 54 61 62 6c  gger = 0;.  Tabl
29a35 65 20 2a 70 54 61 62 3b 0a 20 20 63 68 61 72 20  e *pTab;.  char 
29a36 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20  *zName = 0;     
29a37 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
29a38 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 73  e trigger */.  s
29a39 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
29a3a 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
29a3b 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
29a3c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
29a3d 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  se to store the 
29a3e 74 72 69 67 67 65 72 20 69 6e 20 2a 2f 0a 20 20  trigger in */.  
29a3f 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20  Token *pName;   
29a40 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75          /* The u
29a41 6e 71 75 61 6c 69 66 69 65 64 20 64 62 20 6e 61  nqualified db na
29a42 6d 65 20 2a 2f 0a 20 20 44 62 46 69 78 65 72 20  me */.  DbFixer 
29a43 73 46 69 78 3b 0a 20 20 69 6e 74 20 69 54 61 62  sFix;.  int iTab
29a44 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  Db;..  assert( p
29a45 4e 61 6d 65 31 21 3d 30 20 29 3b 20 20 20 2f 2a  Name1!=0 );   /*
29a46 20 70 4e 61 6d 65 31 2d 3e 7a 20 6d 69 67 68 74   pName1->z might
29a47 20 62 65 20 4e 55 4c 4c 2c 20 62 75 74 20 6e 6f   be NULL, but no
29a48 74 20 70 4e 61 6d 65 31 20 69 74 73 65 6c 66 20  t pName1 itself 
29a49 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61  */.  assert( pNa
29a4a 6d 65 32 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  me2!=0 );.  asse
29a4b 72 74 28 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52  rt( op==TK_INSER
29a4c 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 44 41  T || op==TK_UPDA
29a4d 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c  TE || op==TK_DEL
29a4e 45 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ETE );.  assert(
29a4f 20 6f 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66   op>0 && op<0xff
29a50 20 29 3b 0a 20 20 69 66 28 20 69 73 54 65 6d 70   );.  if( isTemp
29a51 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 54 45   ){.    /* If TE
29a52 4d 50 20 77 61 73 20 73 70 65 63 69 66 69 65 64  MP was specified
29a53 2c 20 74 68 65 6e 20 74 68 65 20 74 72 69 67 67  , then the trigg
29a54 65 72 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20  er name may not 
29a55 62 65 20 71 75 61 6c 69 66 69 65 64 2e 20 2a 2f  be qualified. */
29a56 0a 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 2d  .    if( pName2-
29a57 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71  >n>0 ){.      sq
29a58 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
29a59 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79  arse, "temporary
29a5a 20 74 72 69 67 67 65 72 20 6d 61 79 20 6e 6f 74   trigger may not
29a5b 20 68 61 76 65 20 71 75 61 6c 69 66 69 65 64 20   have qualified 
29a5c 6e 61 6d 65 22 29 3b 0a 20 20 20 20 20 20 67 6f  name");.      go
29a5d 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
29a5e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 44  up;.    }.    iD
29a5f 62 20 3d 20 31 3b 0a 20 20 20 20 70 4e 61 6d 65  b = 1;.    pName
29a60 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 65 6c   = pName1;.  }el
29a61 73 65 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72  se{.    /* Figur
29a62 65 20 6f 75 74 20 74 68 65 20 64 62 20 74 68 61  e out the db tha
29a63 74 20 74 68 65 20 74 68 65 20 74 72 69 67 67 65  t the the trigge
29a64 72 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65  r will be create
29a65 64 20 69 6e 20 2a 2f 0a 20 20 20 20 69 44 62 20  d in */.    iDb 
29a66 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
29a67 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
29a68 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e  me1, pName2, &pN
29a69 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44  ame);.    if( iD
29a6a 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  b<0 ){.      got
29a6b 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
29a6c 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  p;.    }.  }..  
29a6d 2f 2a 20 49 66 20 74 68 65 20 74 72 69 67 67 65  /* If the trigge
29a6e 72 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61  r name was unqua
29a6f 6c 69 66 69 65 64 2c 20 61 6e 64 20 74 68 65 20  lified, and the 
29a70 74 61 62 6c 65 20 69 73 20 61 20 74 65 6d 70 20  table is a temp 
29a71 74 61 62 6c 65 2c 0a 20 20 2a 2a 20 74 68 65 6e  table,.  ** then
29a72 20 73 65 74 20 69 44 62 20 74 6f 20 31 20 74 6f   set iDb to 1 to
29a73 20 63 72 65 61 74 65 20 74 68 65 20 74 72 69 67   create the trig
29a74 67 65 72 20 69 6e 20 74 68 65 20 74 65 6d 70 6f  ger in the tempo
29a75 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20  rary database.. 
29a76 20 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 53 72   ** If sqlite3Sr
29a77 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 29 20 72 65  cListLookup() re
29a78 74 75 72 6e 73 20 30 2c 20 69 6e 64 69 63 61 74  turns 0, indicat
29a79 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 64 6f  ing the table do
29a7a 65 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 78 69 73  es not.  ** exis
29a7b 74 2c 20 74 68 65 20 65 72 72 6f 72 20 69 73 20  t, the error is 
29a7c 63 61 75 67 68 74 20 62 79 20 74 68 65 20 62 6c  caught by the bl
29a7d 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a  ock below..  */.
29a7e 20 20 69 66 28 20 21 70 54 61 62 6c 65 4e 61 6d    if( !pTableNam
29a7f 65 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  e || db->mallocF
29a80 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
29a81 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
29a82 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20  p;.  }.  pTab = 
29a83 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
29a84 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61  okup(pParse, pTa
29a85 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  bleName);.  if( 
29a86 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26 20  pName2->n==0 && 
29a87 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53  pTab && pTab->pS
29a88 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31  chema==db->aDb[1
29a89 5d 2e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ].pSchema ){.   
29a8a 20 69 44 62 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20   iDb = 1;.  }.. 
29a8b 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 74   /* Ensure the t
29a8c 61 62 6c 65 20 6e 61 6d 65 20 6d 61 74 63 68 65  able name matche
29a8d 73 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  s database name 
29a8e 61 6e 64 20 74 68 61 74 20 74 68 65 20 74 61 62  and that the tab
29a8f 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69  le exists */.  i
29a90 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
29a91 6c 65 64 20 29 20 67 6f 74 6f 20 74 72 69 67 67  led ) goto trigg
29a92 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 61 73  er_cleanup;.  as
29a93 73 65 72 74 28 20 70 54 61 62 6c 65 4e 61 6d 65  sert( pTableName
29a94 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69  ->nSrc==1 );.  i
29a95 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69  f( sqlite3FixIni
29a96 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c  t(&sFix, pParse,
29a97 20 69 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c   iDb, "trigger",
29a98 20 70 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20   pName) && .    
29a99 20 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c    sqlite3FixSrcL
29a9a 69 73 74 28 26 73 46 69 78 2c 20 70 54 61 62 6c  ist(&sFix, pTabl
29a9b 65 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f  eName) ){.    go
29a9c 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
29a9d 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d  up;.  }.  pTab =
29a9e 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c   sqlite3SrcListL
29a9f 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54  ookup(pParse, pT
29aa0 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28  ableName);.  if(
29aa1 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 2f 2a   !pTab ){.    /*
29aa2 20 54 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20   The table does 
29aa3 6e 6f 74 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20  not exist. */.  
29aa4 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
29aa5 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66  leanup;.  }.  if
29aa6 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
29aa7 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
29aa8 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
29aa9 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20   "cannot create 
29aaa 74 72 69 67 67 65 72 73 20 6f 6e 20 76 69 72 74  triggers on virt
29aab 75 61 6c 20 74 61 62 6c 65 73 22 29 3b 0a 20 20  ual tables");.  
29aac 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
29aad 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
29aae 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
29aaf 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69 73   trigger name is
29ab0 20 6e 6f 74 20 72 65 73 65 72 76 65 64 20 61 6e   not reserved an
29ab1 64 20 74 68 61 74 20 6e 6f 20 74 72 69 67 67 65  d that no trigge
29ab2 72 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 70  r of the.  ** sp
29ab3 65 63 69 66 69 65 64 20 6e 61 6d 65 20 65 78 69  ecified name exi
29ab4 73 74 73 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d  sts */.  zName =
29ab5 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
29ab6 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29  Token(db, pName)
29ab7 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 7c  ;.  if( !zName |
29ab8 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  | SQLITE_OK!=sql
29ab9 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e  ite3CheckObjectN
29aba 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d  ame(pParse, zNam
29abb 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  e) ){.    goto t
29abc 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
29abd 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
29abe 33 48 61 73 68 46 69 6e 64 28 26 28 64 62 2d 3e  3HashFind(&(db->
29abf 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
29ac0 2d 3e 74 72 69 67 48 61 73 68 29 2c 0a 20 20 20  ->trigHash),.   
29ac1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29ac2 20 20 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65     zName, sqlite
29ac3 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
29ac4 29 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e 6f  ) ){.    if( !no
29ac5 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Err ){.      sql
29ac6 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
29ac7 72 73 65 2c 20 22 74 72 69 67 67 65 72 20 25 54  rse, "trigger %T
29ac8 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22   already exists"
29ac9 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  , pName);.    }.
29aca 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
29acb 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
29acc 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 72 65 61 74   /* Do not creat
29acd 65 20 61 20 74 72 69 67 67 65 72 20 6f 6e 20 61  e a trigger on a
29ace 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f   system table */
29acf 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
29ad0 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61  rNICmp(pTab->zNa
29ad1 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37  me, "sqlite_", 7
29ad2 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
29ad3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
29ad4 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61  se, "cannot crea
29ad5 74 65 20 74 72 69 67 67 65 72 20 6f 6e 20 73 79  te trigger on sy
29ad6 73 74 65 6d 20 74 61 62 6c 65 22 29 3b 0a 20 20  stem table");.  
29ad7 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
29ad8 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  ;.    goto trigg
29ad9 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  er_cleanup;.  }.
29ada 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44 20 6f 66  .  /* INSTEAD of
29adb 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6f 6e   triggers are on
29adc 6c 79 20 66 6f 72 20 76 69 65 77 73 20 61 6e 64  ly for views and
29add 20 76 69 65 77 73 20 6f 6e 6c 79 20 73 75 70 70   views only supp
29ade 6f 72 74 20 49 4e 53 54 45 41 44 0a 20 20 2a 2a  ort INSTEAD.  **
29adf 20 6f 66 20 74 72 69 67 67 65 72 73 2e 0a 20 20   of triggers..  
29ae0 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  */.  if( pTab->p
29ae1 53 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 21  Select && tr_tm!
29ae2 3d 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20  =TK_INSTEAD ){. 
29ae3 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
29ae4 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
29ae5 6f 74 20 63 72 65 61 74 65 20 25 73 20 74 72 69  ot create %s tri
29ae6 67 67 65 72 20 6f 6e 20 76 69 65 77 3a 20 25 53  gger on view: %S
29ae7 22 2c 20 0a 20 20 20 20 20 20 20 20 28 74 72 5f  ", .        (tr_
29ae8 74 6d 20 3d 3d 20 54 4b 5f 42 45 46 4f 52 45 29  tm == TK_BEFORE)
29ae9 3f 22 42 45 46 4f 52 45 22 3a 22 41 46 54 45 52  ?"BEFORE":"AFTER
29aea 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30  ", pTableName, 0
29aeb 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  );.    goto trig
29aec 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
29aed 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 70 53  .  if( !pTab->pS
29aee 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 3d 3d  elect && tr_tm==
29aef 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20 20  TK_INSTEAD ){.  
29af0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
29af1 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
29af2 74 20 63 72 65 61 74 65 20 49 4e 53 54 45 41 44  t create INSTEAD
29af3 20 4f 46 22 0a 20 20 20 20 20 20 20 20 22 20 74   OF".        " t
29af4 72 69 67 67 65 72 20 6f 6e 20 74 61 62 6c 65 3a  rigger on table:
29af5 20 25 53 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65   %S", pTableName
29af6 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74  , 0);.    goto t
29af7 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
29af8 20 20 7d 0a 20 20 69 54 61 62 44 62 20 3d 20 73    }.  iTabDb = s
29af9 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
29afa 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
29afb 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66  chema);..#ifndef
29afc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
29afd 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20  HORIZATION.  {. 
29afe 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51     int code = SQ
29aff 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47  LITE_CREATE_TRIG
29b00 47 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  GER;.    const c
29b01 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
29b02 44 62 5b 69 54 61 62 44 62 5d 2e 7a 4e 61 6d 65  Db[iTabDb].zName
29b03 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
29b04 20 2a 7a 44 62 54 72 69 67 20 3d 20 69 73 54 65   *zDbTrig = isTe
29b05 6d 70 20 3f 20 64 62 2d 3e 61 44 62 5b 31 5d 2e  mp ? db->aDb[1].
29b06 7a 4e 61 6d 65 20 3a 20 7a 44 62 3b 0a 20 20 20  zName : zDb;.   
29b07 20 69 66 28 20 69 54 61 62 44 62 3d 3d 31 20 7c   if( iTabDb==1 |
29b08 7c 20 69 73 54 65 6d 70 20 29 20 63 6f 64 65 20  | isTemp ) code 
29b09 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  = SQLITE_CREATE_
29b0a 54 45 4d 50 5f 54 52 49 47 47 45 52 3b 0a 20 20  TEMP_TRIGGER;.  
29b0b 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
29b0c 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63  hCheck(pParse, c
29b0d 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62  ode, zName, pTab
29b0e 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 54 72 69 67  ->zName, zDbTrig
29b0f 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
29b10 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
29b11 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
29b12 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
29b13 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49  pParse, SQLITE_I
29b14 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41  NSERT, SCHEMA_TA
29b15 42 4c 45 28 69 54 61 62 44 62 29 2c 30 2c 7a 44  BLE(iTabDb),0,zD
29b16 62 29 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  b)){.      goto 
29b17 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
29b18 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
29b19 66 0a 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44 20  f..  /* INSTEAD 
29b1a 4f 46 20 74 72 69 67 67 65 72 73 20 63 61 6e 20  OF triggers can 
29b1b 6f 6e 6c 79 20 61 70 70 65 61 72 20 6f 6e 20 76  only appear on v
29b1c 69 65 77 73 20 61 6e 64 20 42 45 46 4f 52 45 20  iews and BEFORE 
29b1d 74 72 69 67 67 65 72 73 0a 20 20 2a 2a 20 63 61  triggers.  ** ca
29b1e 6e 6e 6f 74 20 61 70 70 65 61 72 20 6f 6e 20 76  nnot appear on v
29b1f 69 65 77 73 2e 20 20 53 6f 20 77 65 20 6d 69 67  iews.  So we mig
29b20 68 74 20 61 73 20 77 65 6c 6c 20 74 72 61 6e 73  ht as well trans
29b21 6c 61 74 65 20 65 76 65 72 79 0a 20 20 2a 2a 20  late every.  ** 
29b22 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
29b23 65 72 20 69 6e 74 6f 20 61 20 42 45 46 4f 52 45  er into a BEFORE
29b24 20 74 72 69 67 67 65 72 2e 20 20 49 74 20 73 69   trigger.  It si
29b25 6d 70 6c 69 66 69 65 73 20 63 6f 64 65 0a 20 20  mplifies code.  
29b26 2a 2a 20 65 6c 73 65 77 68 65 72 65 2e 0a 20 20  ** elsewhere..  
29b27 2a 2f 0a 20 20 69 66 20 28 74 72 5f 74 6d 20 3d  */.  if (tr_tm =
29b28 3d 20 54 4b 5f 49 4e 53 54 45 41 44 29 7b 0a 20  = TK_INSTEAD){. 
29b29 20 20 20 74 72 5f 74 6d 20 3d 20 54 4b 5f 42 45     tr_tm = TK_BE
29b2a 46 4f 52 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  FORE;.  }..  /* 
29b2b 42 75 69 6c 64 20 74 68 65 20 54 72 69 67 67 65  Build the Trigge
29b2c 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 70 54  r object */.  pT
29b2d 72 69 67 67 65 72 20 3d 20 28 54 72 69 67 67 65  rigger = (Trigge
29b2e 72 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  r*)sqlite3DbMall
29b2f 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
29b30 66 28 54 72 69 67 67 65 72 29 29 3b 0a 20 20 69  f(Trigger));.  i
29b31 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20 29  f( pTrigger==0 )
29b32 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
29b33 65 61 6e 75 70 3b 0a 20 20 70 54 72 69 67 67 65  eanup;.  pTrigge
29b34 72 2d 3e 6e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b  r->name = zName;
29b35 0a 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20  .  zName = 0;.  
29b36 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 20  pTrigger->table 
29b37 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
29b38 70 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65  p(db, pTableName
29b39 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  ->a[0].zName);. 
29b3a 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65   pTrigger->pSche
29b3b 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  ma = db->aDb[iDb
29b3c 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 72  ].pSchema;.  pTr
29b3d 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d  igger->pTabSchem
29b3e 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  a = pTab->pSchem
29b3f 61 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 6f  a;.  pTrigger->o
29b40 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70 54  p = (u8)op;.  pT
29b41 72 69 67 67 65 72 2d 3e 74 72 5f 74 6d 20 3d 20  rigger->tr_tm = 
29b42 74 72 5f 74 6d 3d 3d 54 4b 5f 42 45 46 4f 52 45  tr_tm==TK_BEFORE
29b43 20 3f 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52   ? TRIGGER_BEFOR
29b44 45 20 3a 20 54 52 49 47 47 45 52 5f 41 46 54 45  E : TRIGGER_AFTE
29b45 52 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70  R;.  pTrigger->p
29b46 57 68 65 6e 20 3d 20 73 71 6c 69 74 65 33 45 78  When = sqlite3Ex
29b47 70 72 44 75 70 28 64 62 2c 20 70 57 68 65 6e 2c  prDup(db, pWhen,
29b48 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
29b49 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 43  ;.  pTrigger->pC
29b4a 6f 6c 75 6d 6e 73 20 3d 20 73 71 6c 69 74 65 33  olumns = sqlite3
29b4b 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 43  IdListDup(db, pC
29b4c 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74  olumns);.  sqlit
29b4d 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20  e3TokenCopy(db, 
29b4e 26 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 54  &pTrigger->nameT
29b4f 6f 6b 65 6e 2c 70 4e 61 6d 65 29 3b 0a 20 20 61  oken,pName);.  a
29b50 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
29b51 4e 65 77 54 72 69 67 67 65 72 3d 3d 30 20 29 3b  NewTrigger==0 );
29b52 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  .  pParse->pNewT
29b53 72 69 67 67 65 72 20 3d 20 70 54 72 69 67 67 65  rigger = pTrigge
29b54 72 3b 0a 0a 74 72 69 67 67 65 72 5f 63 6c 65 61  r;..trigger_clea
29b55 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 44 62  nup:.  sqlite3Db
29b56 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b  Free(db, zName);
29b57 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
29b58 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62  tDelete(db, pTab
29b59 6c 65 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  leName);.  sqlit
29b5a 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
29b5b 62 2c 20 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20  b, pColumns);.  
29b5c 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
29b5d 65 28 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20 20  e(db, pWhen);.  
29b5e 69 66 28 20 21 70 50 61 72 73 65 2d 3e 70 4e 65  if( !pParse->pNe
29b5f 77 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  wTrigger ){.    
29b60 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
29b61 67 67 65 72 28 64 62 2c 20 70 54 72 69 67 67 65  gger(db, pTrigge
29b62 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r);.  }else{.   
29b63 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
29b64 3e 70 4e 65 77 54 72 69 67 67 65 72 3d 3d 70 54  >pNewTrigger==pT
29b65 72 69 67 67 65 72 20 29 3b 0a 20 20 7d 0a 7d 0a  rigger );.  }.}.
29b66 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
29b67 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66  ine is called af
29b68 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 74  ter all of the t
29b69 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73 20 68  rigger actions h
29b6a 61 76 65 20 62 65 65 6e 20 70 61 72 73 65 64 0a  ave been parsed.
29b6b 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63  ** in order to c
29b6c 6f 6d 70 6c 65 74 65 20 74 68 65 20 70 72 6f 63  omplete the proc
29b6d 65 73 73 20 6f 66 20 62 75 69 6c 64 69 6e 67 20  ess of building 
29b6e 74 68 65 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a  the trigger..*/.
29b6f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
29b70 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73  oid sqlite3Finis
29b71 68 54 72 69 67 67 65 72 28 0a 20 20 50 61 72 73  hTrigger(.  Pars
29b72 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
29b73 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
29b74 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67  ntext */.  Trigg
29b75 65 72 53 74 65 70 20 2a 70 53 74 65 70 4c 69 73  erStep *pStepLis
29b76 74 2c 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65  t, /* The trigge
29b77 72 65 64 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  red program */. 
29b78 20 54 6f 6b 65 6e 20 2a 70 41 6c 6c 20 20 20 20   Token *pAll    
29b79 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65           /* Toke
29b7a 6e 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  n that describes
29b7b 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 43 52   the complete CR
29b7c 45 41 54 45 20 54 52 49 47 47 45 52 20 2a 2f 0a  EATE TRIGGER */.
29b7d 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  ){.  Trigger *pT
29b7e 72 69 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  rig = pParse->pN
29b7f 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 2f 2a  ewTrigger;    /*
29b80 20 54 72 69 67 67 65 72 20 62 65 69 6e 67 20 66   Trigger being f
29b81 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 63 68 61  inished */.  cha
29b82 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
29b83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b84 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
29b85 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 73 71   trigger */.  sq
29b86 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
29b87 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20  se->db;         
29b88 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
29b89 74 61 62 61 73 65 20 2a 2f 0a 20 20 44 62 46 69  tabase */.  DbFi
29b8a 78 65 72 20 73 46 69 78 3b 0a 20 20 69 6e 74 20  xer sFix;.  int 
29b8b 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
29b8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b8d 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
29b8e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
29b8f 74 72 69 67 67 65 72 20 2a 2f 0a 0a 20 20 70 54  trigger */..  pT
29b90 72 69 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  rig = pParse->pN
29b91 65 77 54 72 69 67 67 65 72 3b 0a 20 20 70 50 61  ewTrigger;.  pPa
29b92 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
29b93 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 72   = 0;.  if( pPar
29b94 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 21 70 54 72  se->nErr || !pTr
29b95 69 67 20 29 20 67 6f 74 6f 20 74 72 69 67 67 65  ig ) goto trigge
29b96 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b  rfinish_cleanup;
29b97 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 54 72 69 67  .  zName = pTrig
29b98 2d 3e 6e 61 6d 65 3b 0a 20 20 69 44 62 20 3d 20  ->name;.  iDb = 
29b99 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
29b9a 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
29b9b 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29   pTrig->pSchema)
29b9c 3b 0a 20 20 70 54 72 69 67 2d 3e 73 74 65 70 5f  ;.  pTrig->step_
29b9d 6c 69 73 74 20 3d 20 70 53 74 65 70 4c 69 73 74  list = pStepList
29b9e 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65 70  ;.  while( pStep
29b9f 4c 69 73 74 20 29 7b 0a 20 20 20 20 70 53 74 65  List ){.    pSte
29ba0 70 4c 69 73 74 2d 3e 70 54 72 69 67 20 3d 20 70  pList->pTrig = p
29ba1 54 72 69 67 3b 0a 20 20 20 20 70 53 74 65 70 4c  Trig;.    pStepL
29ba2 69 73 74 20 3d 20 70 53 74 65 70 4c 69 73 74 2d  ist = pStepList-
29ba3 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66  >pNext;.  }.  if
29ba4 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  ( sqlite3FixInit
29ba5 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20  (&sFix, pParse, 
29ba6 69 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c 20  iDb, "trigger", 
29ba7 26 70 54 72 69 67 2d 3e 6e 61 6d 65 54 6f 6b 65  &pTrig->nameToke
29ba8 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 26  n) .          &&
29ba9 20 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67   sqlite3FixTrigg
29baa 65 72 53 74 65 70 28 26 73 46 69 78 2c 20 70 54  erStep(&sFix, pT
29bab 72 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74 29 20  rig->step_list) 
29bac 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  ){.    goto trig
29bad 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75  gerfinish_cleanu
29bae 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20  p;.  }..  /* if 
29baf 77 65 20 61 72 65 20 6e 6f 74 20 69 6e 69 74 69  we are not initi
29bb0 61 6c 69 7a 69 6e 67 2c 20 61 6e 64 20 74 68 69  alizing, and thi
29bb1 73 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74  s trigger is not
29bb2 20 6f 6e 20 61 20 54 45 4d 50 20 74 61 62 6c 65   on a TEMP table
29bb3 2c 20 0a 20 20 2a 2a 20 62 75 69 6c 64 20 74 68  , .  ** build th
29bb4 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
29bb5 65 6e 74 72 79 0a 20 20 2a 2f 0a 20 20 69 66 28  entry.  */.  if(
29bb6 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
29bb7 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a  ){.    Vdbe *v;.
29bb8 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20      char *z;..  
29bb9 20 20 2f 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74    /* Make an ent
29bba 72 79 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ry in the sqlite
29bbb 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a 2f  _master table */
29bbc 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33  .    v = sqlite3
29bbd 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
29bbe 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20  .    if( v==0 ) 
29bbf 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69  goto triggerfini
29bc0 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  sh_cleanup;.    
29bc1 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
29bc2 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
29bc3 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20  e, 0, iDb);.    
29bc4 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  z = sqlite3DbStr
29bc5 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29  NDup(db, (char*)
29bc6 70 41 6c 6c 2d 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e  pAll->z, pAll->n
29bc7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  );.    sqlite3Ne
29bc8 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
29bc9 2c 0a 20 20 20 20 20 20 20 22 49 4e 53 45 52 54  ,.       "INSERT
29bca 20 49 4e 54 4f 20 25 51 2e 25 73 20 56 41 4c 55   INTO %Q.%s VALU
29bcb 45 53 28 27 74 72 69 67 67 65 72 27 2c 25 51 2c  ES('trigger',%Q,
29bcc 25 51 2c 30 2c 27 43 52 45 41 54 45 20 54 52 49  %Q,0,'CREATE TRI
29bcd 47 47 45 52 20 25 71 27 29 22 2c 0a 20 20 20 20  GGER %q')",.    
29bce 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e     db->aDb[iDb].
29bcf 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41  zName, SCHEMA_TA
29bd0 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c  BLE(iDb), zName,
29bd1 0a 20 20 20 20 20 20 20 70 54 72 69 67 2d 3e 74  .       pTrig->t
29bd2 61 62 6c 65 2c 20 7a 29 3b 0a 20 20 20 20 73 71  able, z);.    sq
29bd3 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
29bd4 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  z);.    sqlite3C
29bd5 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72  hangeCookie(pPar
29bd6 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71  se, iDb);.    sq
29bd7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
29bd8 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  v, OP_ParseSchem
29bd9 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 73 71  a, iDb, 0, 0, sq
29bda 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 0a 20 20  lite3MPrintf(.  
29bdb 20 20 20 20 20 20 64 62 2c 20 22 74 79 70 65 3d        db, "type=
29bdc 27 74 72 69 67 67 65 72 27 20 41 4e 44 20 6e 61  'trigger' AND na
29bdd 6d 65 3d 27 25 71 27 22 2c 20 7a 4e 61 6d 65 29  me='%q'", zName)
29bde 2c 20 50 34 5f 44 59 4e 41 4d 49 43 0a 20 20 20  , P4_DYNAMIC.   
29bdf 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64   );.  }..  if( d
29be0 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
29be1 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69      Trigger *pLi
29be2 6e 6b 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20  nk = pTrig;.    
29be3 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 64  Hash *pHash = &d
29be4 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
29be5 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 3b 0a 20  ema->trigHash;. 
29be6 20 20 20 70 54 72 69 67 20 3d 20 73 71 6c 69 74     pTrig = sqlit
29be7 65 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61  e3HashInsert(pHa
29be8 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74  sh, zName, sqlit
29be9 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
29bea 29 2c 20 70 54 72 69 67 29 3b 0a 20 20 20 20 69  ), pTrig);.    i
29beb 66 28 20 70 54 72 69 67 20 29 7b 0a 20 20 20 20  f( pTrig ){.    
29bec 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
29bed 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  ed = 1;.    }els
29bee 65 20 69 66 28 20 70 4c 69 6e 6b 2d 3e 70 53 63  e if( pLink->pSc
29bef 68 65 6d 61 3d 3d 70 4c 69 6e 6b 2d 3e 70 54 61  hema==pLink->pTa
29bf0 62 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20  bSchema ){.     
29bf1 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
29bf2 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69      int n = sqli
29bf3 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4c 69 6e  te3Strlen30(pLin
29bf4 6b 2d 3e 74 61 62 6c 65 29 3b 0a 20 20 20 20 20  k->table);.     
29bf5 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 48   pTab = sqlite3H
29bf6 61 73 68 46 69 6e 64 28 26 70 4c 69 6e 6b 2d 3e  ashFind(&pLink->
29bf7 70 54 61 62 53 63 68 65 6d 61 2d 3e 74 62 6c 48  pTabSchema->tblH
29bf8 61 73 68 2c 20 70 4c 69 6e 6b 2d 3e 74 61 62 6c  ash, pLink->tabl
29bf9 65 2c 20 6e 29 3b 0a 20 20 20 20 20 20 61 73 73  e, n);.      ass
29bfa 65 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a  ert( pTab!=0 );.
29bfb 20 20 20 20 20 20 70 4c 69 6e 6b 2d 3e 70 4e 65        pLink->pNe
29bfc 78 74 20 3d 20 70 54 61 62 2d 3e 70 54 72 69 67  xt = pTab->pTrig
29bfd 67 65 72 3b 0a 20 20 20 20 20 20 70 54 61 62 2d  ger;.      pTab-
29bfe 3e 70 54 72 69 67 67 65 72 20 3d 20 70 4c 69 6e  >pTrigger = pLin
29bff 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 74 72  k;.    }.  }..tr
29c00 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61  iggerfinish_clea
29c01 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 44 65  nup:.  sqlite3De
29c02 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20  leteTrigger(db, 
29c03 70 54 72 69 67 29 3b 0a 20 20 61 73 73 65 72 74  pTrig);.  assert
29c04 28 20 21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  ( !pParse->pNewT
29c05 72 69 67 67 65 72 20 29 3b 0a 20 20 73 71 6c 69  rigger );.  sqli
29c06 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
29c07 53 74 65 70 28 64 62 2c 20 70 53 74 65 70 4c 69  Step(db, pStepLi
29c08 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  st);.}../*.** Ma
29c09 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 61 6c 6c  ke a copy of all
29c0a 20 63 6f 6d 70 6f 6e 65 6e 74 73 20 6f 66 20 74   components of t
29c0b 68 65 20 67 69 76 65 6e 20 74 72 69 67 67 65 72  he given trigger
29c0c 20 73 74 65 70 2e 20 20 54 68 69 73 20 68 61 73   step.  This has
29c0d 0a 2a 2a 20 74 68 65 20 65 66 66 65 63 74 20 6f  .** the effect o
29c0e 66 20 63 6f 70 79 69 6e 67 20 61 6c 6c 20 45 78  f copying all Ex
29c0f 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65  pr.token.z value
29c10 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  s into memory ob
29c11 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73  tained.** from s
29c12 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
29c13 20 20 41 73 20 69 6e 69 74 69 61 6c 6c 79 20 63    As initially c
29c14 72 65 61 74 65 64 2c 20 74 68 65 20 45 78 70 72  reated, the Expr
29c15 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65 73 0a  .token.z values.
29c16 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20  ** all point to 
29c17 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
29c18 20 74 68 61 74 20 77 61 73 20 66 65 64 20 74 6f   that was fed to
29c19 20 74 68 65 20 70 61 72 73 65 72 2e 20 20 42 75   the parser.  Bu
29c1a 74 20 74 68 61 74 0a 2a 2a 20 73 74 72 69 6e 67  t that.** string
29c1b 20 69 73 20 65 70 68 65 6d 65 72 61 6c 20 2d 20   is ephemeral - 
29c1c 69 74 20 77 69 6c 6c 20 67 6f 20 61 77 61 79 20  it will go away 
29c1d 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 73  as soon as the s
29c1e 71 6c 69 74 65 33 5f 65 78 65 63 28 29 0a 2a 2a  qlite3_exec().**
29c1f 20 63 61 6c 6c 20 74 68 61 74 20 73 74 61 72 74   call that start
29c20 65 64 20 74 68 65 20 70 61 72 73 65 72 20 65 78  ed the parser ex
29c21 69 74 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69  its.  This routi
29c22 6e 65 20 6d 61 6b 65 73 20 61 20 70 65 72 73 69  ne makes a persi
29c23 73 74 65 6e 74 0a 2a 2a 20 63 6f 70 79 20 6f 66  stent.** copy of
29c24 20 61 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f   all the Expr.to
29c25 6b 65 6e 2e 7a 20 73 74 72 69 6e 67 73 20 73 6f  ken.z strings so
29c26 20 74 68 61 74 20 74 68 65 20 54 72 69 67 67 65   that the Trigge
29c27 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65 0a  rStep structure.
29c28 2a 2a 20 77 69 6c 6c 20 62 65 20 76 61 6c 69 64  ** will be valid
29c29 20 65 76 65 6e 20 61 66 74 65 72 20 74 68 65 20   even after the 
29c2a 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63  sqlite3_exec() c
29c2b 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a  all returns..*/.
29c2c 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
29c2d 74 65 50 65 72 73 69 73 74 54 72 69 67 67 65 72  tePersistTrigger
29c2e 53 74 65 70 28 73 71 6c 69 74 65 33 20 2a 64 62  Step(sqlite3 *db
29c2f 2c 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70  , TriggerStep *p
29c30 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 61 72 67  ){.  if( p->targ
29c31 65 74 2e 7a 20 29 7b 0a 20 20 20 20 70 2d 3e 74  et.z ){.    p->t
29c32 61 72 67 65 74 2e 7a 20 3d 20 28 75 38 2a 29 73  arget.z = (u8*)s
29c33 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
29c34 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 74 61  db, (char*)p->ta
29c35 72 67 65 74 2e 7a 2c 20 70 2d 3e 74 61 72 67 65  rget.z, p->targe
29c36 74 2e 6e 29 3b 0a 20 20 20 20 70 2d 3e 74 61 72  t.n);.    p->tar
29c37 67 65 74 2e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d  get.dyn = 1;.  }
29c38 0a 20 20 69 66 28 20 70 2d 3e 70 53 65 6c 65 63  .  if( p->pSelec
29c39 74 20 29 7b 0a 20 20 20 20 53 65 6c 65 63 74 20  t ){.    Select 
29c3a 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 53  *pNew = sqlite3S
29c3b 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e  electDup(db, p->
29c3c 70 53 65 6c 65 63 74 2c 20 31 29 3b 0a 20 20 20  pSelect, 1);.   
29c3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
29c3e 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 53 65 6c  lete(db, p->pSel
29c3f 65 63 74 29 3b 0a 20 20 20 20 70 2d 3e 70 53 65  ect);.    p->pSe
29c40 6c 65 63 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d  lect = pNew;.  }
29c41 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65  .  if( p->pWhere
29c42 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e   ){.    Expr *pN
29c43 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
29c44 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72  Dup(db, p->pWher
29c45 65 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43  e, EXPRDUP_REDUC
29c46 45 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  E);.    sqlite3E
29c47 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
29c48 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 70 2d  >pWhere);.    p-
29c49 3e 70 57 68 65 72 65 20 3d 20 70 4e 65 77 3b 0a  >pWhere = pNew;.
29c4a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 45 78    }.  if( p->pEx
29c4b 70 72 4c 69 73 74 20 29 7b 0a 20 20 20 20 45 78  prList ){.    Ex
29c4c 70 72 4c 69 73 74 20 2a 70 4e 65 77 20 3d 20 73  prList *pNew = s
29c4d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
29c4e 70 28 64 62 2c 20 70 2d 3e 70 45 78 70 72 4c 69  p(db, p->pExprLi
29c4f 73 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  st, 1);.    sqli
29c50 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
29c51 65 28 64 62 2c 20 70 2d 3e 70 45 78 70 72 4c 69  e(db, p->pExprLi
29c52 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70 45 78 70  st);.    p->pExp
29c53 72 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20  rList = pNew;.  
29c54 7d 0a 20 20 69 66 28 20 70 2d 3e 70 49 64 4c 69  }.  if( p->pIdLi
29c55 73 74 20 29 7b 0a 20 20 20 20 49 64 4c 69 73 74  st ){.    IdList
29c56 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
29c57 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  IdListDup(db, p-
29c58 3e 70 49 64 4c 69 73 74 29 3b 0a 20 20 20 20 73  >pIdList);.    s
29c59 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
29c5a 74 65 28 64 62 2c 20 70 2d 3e 70 49 64 4c 69 73  te(db, p->pIdLis
29c5b 74 29 3b 0a 20 20 20 20 70 2d 3e 70 49 64 4c 69  t);.    p->pIdLi
29c5c 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d  st = pNew;.  }.}
29c5d 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 53  ../*.** Turn a S
29c5e 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
29c5f 28 74 68 61 74 20 74 68 65 20 70 53 65 6c 65 63  (that the pSelec
29c60 74 20 70 61 72 61 6d 65 74 65 72 20 70 6f 69 6e  t parameter poin
29c61 74 73 20 74 6f 29 20 69 6e 74 6f 0a 2a 2a 20 61  ts to) into.** a
29c62 20 74 72 69 67 67 65 72 20 73 74 65 70 2e 20 20   trigger step.  
29c63 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
29c64 20 74 6f 20 61 20 54 72 69 67 67 65 72 53 74 65   to a TriggerSte
29c65 70 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  p structure..**.
29c66 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
29c67 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
29c68 20 77 68 65 6e 20 69 74 20 66 69 6e 64 73 20 61   when it finds a
29c69 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
29c6a 74 20 69 6e 0a 2a 2a 20 62 6f 64 79 20 6f 66 20  t in.** body of 
29c6b 61 20 54 52 49 47 47 45 52 2e 20 20 0a 2a 2f 0a  a TRIGGER.  .*/.
29c6c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
29c6d 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69  riggerStep *sqli
29c6e 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74  te3TriggerSelect
29c6f 53 74 65 70 28 73 71 6c 69 74 65 33 20 2a 64 62  Step(sqlite3 *db
29c70 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63  , Select *pSelec
29c71 74 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65  t){.  TriggerSte
29c72 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 20  p *pTriggerStep 
29c73 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
29c74 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
29c75 28 54 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a  (TriggerStep));.
29c76 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74    if( pTriggerSt
29c77 65 70 3d 3d 30 20 29 20 7b 0a 20 20 20 20 73 71  ep==0 ) {.    sq
29c78 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
29c79 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  e(db, pSelect);.
29c7a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
29c7b 7d 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  }..  pTriggerSte
29c7c 70 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43  p->op = TK_SELEC
29c7d 54 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  T;.  pTriggerSte
29c7e 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 65  p->pSelect = pSe
29c7f 6c 65 63 74 3b 0a 20 20 70 54 72 69 67 67 65 72  lect;.  pTrigger
29c80 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f  Step->orconf = O
29c81 45 5f 44 65 66 61 75 6c 74 3b 0a 20 20 73 71 6c  E_Default;.  sql
29c82 69 74 65 50 65 72 73 69 73 74 54 72 69 67 67 65  itePersistTrigge
29c83 72 53 74 65 70 28 64 62 2c 20 70 54 72 69 67 67  rStep(db, pTrigg
29c84 65 72 53 74 65 70 29 3b 0a 0a 20 20 72 65 74 75  erStep);..  retu
29c85 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b  rn pTriggerStep;
29c86 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 69 6c 64 20  .}../*.** Build 
29c87 61 20 74 72 69 67 67 65 72 20 73 74 65 70 20 6f  a trigger step o
29c88 75 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20  ut of an INSERT 
29c89 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75  statement.  Retu
29c8a 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  rn a pointer.** 
29c8b 74 6f 20 74 68 65 20 6e 65 77 20 74 72 69 67 67  to the new trigg
29c8c 65 72 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54  er step..**.** T
29c8d 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20  he parser calls 
29c8e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
29c8f 6e 20 69 74 20 73 65 65 73 20 61 6e 20 49 4e 53  n it sees an INS
29c90 45 52 54 20 69 6e 73 69 64 65 20 74 68 65 0a 2a  ERT inside the.*
29c91 2a 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67  * body of a trig
29c92 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ger..*/.SQLITE_P
29c93 52 49 56 41 54 45 20 54 72 69 67 67 65 72 53 74  RIVATE TriggerSt
29c94 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  ep *sqlite3Trigg
29c95 65 72 49 6e 73 65 72 74 53 74 65 70 28 0a 20 20  erInsertStep(.  
29c96 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
29c97 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
29c98 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
29c99 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c  /.  Token *pTabl
29c9a 65 4e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20  eName,  /* Name 
29c9b 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74  of the table int
29c9c 6f 20 77 68 69 63 68 20 77 65 20 69 6e 73 65 72  o which we inser
29c9d 74 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70  t */.  IdList *p
29c9e 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 4c 69  Column,    /* Li
29c9f 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  st of columns in
29ca0 20 70 54 61 62 6c 65 4e 61 6d 65 20 74 6f 20 69   pTableName to i
29ca1 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20  nsert into */.  
29ca2 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
29ca3 2c 20 20 20 2f 2a 20 54 68 65 20 56 41 4c 55 45  ,   /* The VALUE
29ca4 20 63 6c 61 75 73 65 3a 20 61 20 6c 69 73 74 20   clause: a list 
29ca5 6f 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20  of values to be 
29ca6 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53 65  inserted */.  Se
29ca7 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
29ca8 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74    /* A SELECT st
29ca9 61 74 65 6d 65 6e 74 20 74 68 61 74 20 73 75 70  atement that sup
29caa 70 6c 69 65 73 20 76 61 6c 75 65 73 20 2a 2f 0a  plies values */.
29cab 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20    int orconf    
29cac 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e        /* The con
29cad 66 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 20  flict algorithm 
29cae 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 52 65  (OE_Abort, OE_Re
29caf 70 6c 61 63 65 2c 20 65 74 63 2e 29 20 2a 2f 0a  place, etc.) */.
29cb0 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  ){.  TriggerStep
29cb1 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a   *pTriggerStep;.
29cb2 0a 20 20 61 73 73 65 72 74 28 70 45 4c 69 73 74  .  assert(pEList
29cb3 20 3d 3d 20 30 20 7c 7c 20 70 53 65 6c 65 63 74   == 0 || pSelect
29cb4 20 3d 3d 20 30 29 3b 0a 20 20 61 73 73 65 72 74   == 0);.  assert
29cb5 28 70 45 4c 69 73 74 20 21 3d 20 30 20 7c 7c 20  (pEList != 0 || 
29cb6 70 53 65 6c 65 63 74 20 21 3d 20 30 20 7c 7c 20  pSelect != 0 || 
29cb7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
29cb8 29 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74  );..  pTriggerSt
29cb9 65 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ep = sqlite3DbMa
29cba 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
29cbb 65 6f 66 28 54 72 69 67 67 65 72 53 74 65 70 29  eof(TriggerStep)
29cbc 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  );.  if( pTrigge
29cbd 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70 54 72  rStep ){.    pTr
29cbe 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d 20  iggerStep->op = 
29cbf 54 4b 5f 49 4e 53 45 52 54 3b 0a 20 20 20 20 70  TK_INSERT;.    p
29cc0 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65  TriggerStep->pSe
29cc1 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a  lect = pSelect;.
29cc2 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70      pTriggerStep
29cc3 2d 3e 74 61 72 67 65 74 20 20 3d 20 2a 70 54 61  ->target  = *pTa
29cc4 62 6c 65 4e 61 6d 65 3b 0a 20 20 20 20 70 54 72  bleName;.    pTr
29cc5 69 67 67 65 72 53 74 65 70 2d 3e 70 49 64 4c 69  iggerStep->pIdLi
29cc6 73 74 20 3d 20 70 43 6f 6c 75 6d 6e 3b 0a 20 20  st = pColumn;.  
29cc7 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
29cc8 70 45 78 70 72 4c 69 73 74 20 3d 20 70 45 4c 69  pExprList = pELi
29cc9 73 74 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  st;.    pTrigger
29cca 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f  Step->orconf = o
29ccb 72 63 6f 6e 66 3b 0a 20 20 20 20 73 71 6c 69 74  rconf;.    sqlit
29ccc 65 50 65 72 73 69 73 74 54 72 69 67 67 65 72 53  ePersistTriggerS
29ccd 74 65 70 28 64 62 2c 20 70 54 72 69 67 67 65 72  tep(db, pTrigger
29cce 53 74 65 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Step);.  }else{.
29ccf 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73      sqlite3IdLis
29cd0 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c  tDelete(db, pCol
29cd1 75 6d 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  umn);.    sqlite
29cd2 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
29cd3 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20  db, pEList);.   
29cd4 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
29cd5 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
29cd6 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
29cd7 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d   pTriggerStep;.}
29cd8 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63  ../*.** Construc
29cd9 74 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70  t a trigger step
29cda 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
29cdb 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65   an UPDATE state
29cdc 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a  ment and return.
29cdd 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
29cde 74 68 61 74 20 74 72 69 67 67 65 72 20 73 74 65  that trigger ste
29cdf 70 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63  p.  The parser c
29ce0 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
29ce1 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65  e when it.** see
29ce2 73 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74  s an UPDATE stat
29ce3 65 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65  ement inside the
29ce4 20 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54   body of a CREAT
29ce5 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51  E TRIGGER..*/.SQ
29ce6 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69  LITE_PRIVATE Tri
29ce7 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
29ce8 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74  3TriggerUpdateSt
29ce9 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ep(.  sqlite3 *d
29cea 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
29ceb 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
29cec 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ction */.  Token
29ced 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20   *pTableName,   
29cee 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
29cef 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61 74  able to be updat
29cf0 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ed */.  ExprList
29cf1 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 2f 2a 20   *pEList,    /* 
29cf2 54 68 65 20 53 45 54 20 63 6c 61 75 73 65 3a 20  The SET clause: 
29cf3 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 61  list of column a
29cf4 6e 64 20 6e 65 77 20 76 61 6c 75 65 73 20 2a 2f  nd new values */
29cf5 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c  .  Expr *pWhere,
29cf6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
29cf7 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
29cf8 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20   int orconf     
29cf9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e        /* The con
29cfa 66 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 2e  flict algorithm.
29cfb 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49   (OE_Abort, OE_I
29cfc 67 6e 6f 72 65 2c 20 65 74 63 29 20 2a 2f 0a 29  gnore, etc) */.)
29cfd 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  {.  TriggerStep 
29cfe 2a 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20  *pTriggerStep = 
29cff 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
29d00 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
29d01 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20  riggerStep));.  
29d02 69 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70  if( pTriggerStep
29d03 3d 3d 30 20 29 7b 0a 20 20 20 20 20 73 71 6c 69  ==0 ){.     sqli
29d04 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
29d05 65 28 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20  e(db, pEList);. 
29d06 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
29d07 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72 65  elete(db, pWhere
29d08 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30  );.     return 0
29d09 3b 0a 20 20 7d 0a 0a 20 20 70 54 72 69 67 67 65  ;.  }..  pTrigge
29d0a 72 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 55  rStep->op = TK_U
29d0b 50 44 41 54 45 3b 0a 20 20 70 54 72 69 67 67 65  PDATE;.  pTrigge
29d0c 72 53 74 65 70 2d 3e 74 61 72 67 65 74 20 20 3d  rStep->target  =
29d0d 20 2a 70 54 61 62 6c 65 4e 61 6d 65 3b 0a 20 20   *pTableName;.  
29d0e 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45  pTriggerStep->pE
29d0f 78 70 72 4c 69 73 74 20 3d 20 70 45 4c 69 73 74  xprList = pEList
29d10 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ;.  pTriggerStep
29d11 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65 72  ->pWhere = pWher
29d12 65 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  e;.  pTriggerSte
29d13 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f  p->orconf = orco
29d14 6e 66 3b 0a 20 20 73 71 6c 69 74 65 50 65 72 73  nf;.  sqlitePers
29d15 69 73 74 54 72 69 67 67 65 72 53 74 65 70 28 64  istTriggerStep(d
29d16 62 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29  b, pTriggerStep)
29d17 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69  ;..  return pTri
29d18 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a  ggerStep;.}../*.
29d19 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 74  ** Construct a t
29d1a 72 69 67 67 65 72 20 73 74 65 70 20 74 68 61 74  rigger step that
29d1b 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 44 45   implements a DE
29d1c 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 61  LETE statement a
29d1d 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  nd return.** a p
29d1e 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74  ointer to that t
29d1f 72 69 67 67 65 72 20 73 74 65 70 2e 20 20 54 68  rigger step.  Th
29d20 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74  e parser calls t
29d21 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
29d22 20 69 74 0a 2a 2a 20 73 65 65 73 20 61 20 44 45   it.** sees a DE
29d23 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69  LETE statement i
29d24 6e 73 69 64 65 20 74 68 65 20 62 6f 64 79 20 6f  nside the body o
29d25 66 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47  f a CREATE TRIGG
29d26 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ER..*/.SQLITE_PR
29d27 49 56 41 54 45 20 54 72 69 67 67 65 72 53 74 65  IVATE TriggerSte
29d28 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65  p *sqlite3Trigge
29d29 72 44 65 6c 65 74 65 53 74 65 70 28 0a 20 20 73  rDeleteStep(.  s
29d2a 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
29d2b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
29d2c 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
29d2d 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65  .  Token *pTable
29d2e 4e 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  Name,      /* Th
29d2f 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69  e table from whi
29d30 63 68 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65  ch rows are dele
29d31 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ted */.  Expr *p
29d32 57 68 65 72 65 20 20 20 20 20 20 20 20 20 20 20  Where           
29d33 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
29d34 61 75 73 65 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  ause */.){.  Tri
29d35 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
29d36 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74 65 33  erStep = sqlite3
29d37 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
29d38 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 53   sizeof(TriggerS
29d39 74 65 70 29 29 3b 0a 20 20 69 66 28 20 70 54 72  tep));.  if( pTr
29d3a 69 67 67 65 72 53 74 65 70 3d 3d 30 20 29 7b 0a  iggerStep==0 ){.
29d3b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
29d3c 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72 65  elete(db, pWhere
29d3d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
29d3e 0a 20 20 7d 0a 0a 20 20 70 54 72 69 67 67 65 72  .  }..  pTrigger
29d3f 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 44 45  Step->op = TK_DE
29d40 4c 45 54 45 3b 0a 20 20 70 54 72 69 67 67 65 72  LETE;.  pTrigger
29d41 53 74 65 70 2d 3e 74 61 72 67 65 74 20 20 3d 20  Step->target  = 
29d42 2a 70 54 61 62 6c 65 4e 61 6d 65 3b 0a 20 20 70  *pTableName;.  p
29d43 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68  TriggerStep->pWh
29d44 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20  ere = pWhere;.  
29d45 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
29d46 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66 61 75 6c  conf = OE_Defaul
29d47 74 3b 0a 20 20 73 71 6c 69 74 65 50 65 72 73 69  t;.  sqlitePersi
29d48 73 74 54 72 69 67 67 65 72 53 74 65 70 28 64 62  stTriggerStep(db
29d49 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29 3b  , pTriggerStep);
29d4a 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67  ..  return pTrig
29d4b 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 20 0a  gerStep;.}../* .
29d4c 2a 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 64  ** Recursively d
29d4d 65 6c 65 74 65 20 61 20 54 72 69 67 67 65 72 20  elete a Trigger 
29d4e 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 53 51 4c  structure.*/.SQL
29d4f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
29d50 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
29d51 69 67 67 65 72 28 73 71 6c 69 74 65 33 20 2a 64  igger(sqlite3 *d
29d52 62 2c 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  b, Trigger *pTri
29d53 67 67 65 72 29 7b 0a 20 20 69 66 28 20 70 54 72  gger){.  if( pTr
29d54 69 67 67 65 72 3d 3d 30 20 29 20 72 65 74 75 72  igger==0 ) retur
29d55 6e 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65  n;.  sqlite3Dele
29d56 74 65 54 72 69 67 67 65 72 53 74 65 70 28 64 62  teTriggerStep(db
29d57 2c 20 70 54 72 69 67 67 65 72 2d 3e 73 74 65 70  , pTrigger->step
29d58 5f 6c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  _list);.  sqlite
29d59 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69  3DbFree(db, pTri
29d5a 67 67 65 72 2d 3e 6e 61 6d 65 29 3b 0a 20 20 73  gger->name);.  s
29d5b 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
29d5c 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65   pTrigger->table
29d5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
29d5e 44 65 6c 65 74 65 28 64 62 2c 20 70 54 72 69 67  Delete(db, pTrig
29d5f 67 65 72 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 73  ger->pWhen);.  s
29d60 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
29d61 74 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d  te(db, pTrigger-
29d62 3e 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 69 66  >pColumns);.  if
29d63 28 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65  ( pTrigger->name
29d64 54 6f 6b 65 6e 2e 64 79 6e 20 29 20 73 71 6c 69  Token.dyn ) sqli
29d65 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63  te3DbFree(db, (c
29d66 68 61 72 2a 29 70 54 72 69 67 67 65 72 2d 3e 6e  har*)pTrigger->n
29d67 61 6d 65 54 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 73  ameToken.z);.  s
29d68 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
29d69 20 70 54 72 69 67 67 65 72 29 3b 0a 7d 0a 0a 2f   pTrigger);.}../
29d6a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
29d6b 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
29d6c 64 72 6f 70 20 61 20 74 72 69 67 67 65 72 20 66  drop a trigger f
29d6d 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
29d6e 20 73 63 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20   schema. .**.** 
29d6f 54 68 69 73 20 6d 61 79 20 62 65 20 63 61 6c 6c  This may be call
29d70 65 64 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d  ed directly from
29d71 20 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 20   the parser and 
29d72 74 68 65 72 65 66 6f 72 65 20 69 64 65 6e 74 69  therefore identi
29d73 66 69 65 73 0a 2a 2a 20 74 68 65 20 74 72 69 67  fies.** the trig
29d74 67 65 72 20 62 79 20 6e 61 6d 65 2e 20 20 54 68  ger by name.  Th
29d75 65 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69  e sqlite3DropTri
29d76 67 67 65 72 50 74 72 28 29 20 72 6f 75 74 69 6e  ggerPtr() routin
29d77 65 20 64 6f 65 73 20 74 68 65 0a 2a 2a 20 73 61  e does the.** sa
29d78 6d 65 20 6a 6f 62 20 61 73 20 74 68 69 73 20 72  me job as this r
29d79 6f 75 74 69 6e 65 20 65 78 63 65 70 74 20 69 74  outine except it
29d7a 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72   takes a pointer
29d7b 20 74 6f 20 74 68 65 20 74 72 69 67 67 65 72 0a   to the trigger.
29d7c 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  ** instead of th
29d7d 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 2e 0a  e trigger name..
29d7e 2a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  **/.SQLITE_PRIVA
29d7f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
29d80 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 65  ropTrigger(Parse
29d81 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73   *pParse, SrcLis
29d82 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 6e 6f  t *pName, int no
29d83 45 72 72 29 7b 0a 20 20 54 72 69 67 67 65 72 20  Err){.  Trigger 
29d84 2a 70 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20  *pTrigger = 0;. 
29d85 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20   int i;.  const 
29d86 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 63 6f 6e  char *zDb;.  con
29d87 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a  st char *zName;.
29d88 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 73    int nName;.  s
29d89 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
29d8a 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20  rse->db;..  if( 
29d8b 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
29d8c 20 29 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69   ) goto drop_tri
29d8d 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
29d8e 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
29d8f 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
29d90 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
29d91 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65  goto drop_trigge
29d92 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  r_cleanup;.  }..
29d93 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 2d    assert( pName-
29d94 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 44  >nSrc==1 );.  zD
29d95 62 20 3d 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e  b = pName->a[0].
29d96 7a 44 61 74 61 62 61 73 65 3b 0a 20 20 7a 4e 61  zDatabase;.  zNa
29d97 6d 65 20 3d 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d  me = pName->a[0]
29d98 2e 7a 4e 61 6d 65 3b 0a 20 20 6e 4e 61 6d 65 20  .zName;.  nName 
29d99 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
29d9a 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28  0(zName);.  for(
29d9b 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69  i=OMIT_TEMPDB; i
29d9c 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
29d9d 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32      int j = (i<2
29d9e 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a  ) ? i^1 : i;  /*
29d9f 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 66   Search TEMP bef
29da0 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20  ore MAIN */.    
29da1 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c 69 74  if( zDb && sqlit
29da2 65 33 53 74 72 49 43 6d 70 28 64 62 2d 3e 61 44  e3StrICmp(db->aD
29da3 62 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29  b[j].zName, zDb)
29da4 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
29da5 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69   pTrigger = sqli
29da6 74 65 33 48 61 73 68 46 69 6e 64 28 26 28 64 62  te3HashFind(&(db
29da7 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61  ->aDb[j].pSchema
29da8 2d 3e 74 72 69 67 48 61 73 68 29 2c 20 7a 4e 61  ->trigHash), zNa
29da9 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
29daa 69 66 28 20 70 54 72 69 67 67 65 72 20 29 20 62  if( pTrigger ) b
29dab 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  reak;.  }.  if( 
29dac 21 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20  !pTrigger ){.   
29dad 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20   if( !noErr ){. 
29dae 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
29daf 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
29db0 20 73 75 63 68 20 74 72 69 67 67 65 72 3a 20 25   such trigger: %
29db1 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20  S", pName, 0);. 
29db2 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 64 72     }.    goto dr
29db3 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  op_trigger_clean
29db4 75 70 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  up;.  }.  sqlite
29db5 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28  3DropTriggerPtr(
29db6 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
29db7 29 3b 0a 0a 64 72 6f 70 5f 74 72 69 67 67 65 72  );..drop_trigger
29db8 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69  _cleanup:.  sqli
29db9 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
29dba 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a  (db, pName);.}..
29dbb 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
29dbc 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 54 61  ointer to the Ta
29dbd 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f  ble structure fo
29dbe 72 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  r the table that
29dbf 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73   a trigger.** is
29dc0 20 73 65 74 20 6f 6e 2e 0a 2a 2f 0a 73 74 61 74   set on..*/.stat
29dc1 69 63 20 54 61 62 6c 65 20 2a 74 61 62 6c 65 4f  ic Table *tableO
29dc2 66 54 72 69 67 67 65 72 28 54 72 69 67 67 65 72  fTrigger(Trigger
29dc3 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 69   *pTrigger){.  i
29dc4 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
29dc5 72 6c 65 6e 33 30 28 70 54 72 69 67 67 65 72 2d  rlen30(pTrigger-
29dc6 3e 74 61 62 6c 65 29 3b 0a 20 20 72 65 74 75 72  >table);.  retur
29dc7 6e 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  n sqlite3HashFin
29dc8 64 28 26 70 54 72 69 67 67 65 72 2d 3e 70 54 61  d(&pTrigger->pTa
29dc9 62 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  bSchema->tblHash
29dca 2c 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c  , pTrigger->tabl
29dcb 65 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  e, n);.}.../*.**
29dcc 20 44 72 6f 70 20 61 20 74 72 69 67 67 65 72 20   Drop a trigger 
29dcd 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20  given a pointer 
29dce 74 6f 20 74 68 61 74 20 74 72 69 67 67 65 72 2e  to that trigger.
29dcf 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
29dd0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
29dd1 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 50  DropTriggerPtr(P
29dd2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 72  arse *pParse, Tr
29dd3 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 29  igger *pTrigger)
29dd4 7b 0a 20 20 54 61 62 6c 65 20 20 20 2a 70 54 61  {.  Table   *pTa
29dd5 62 6c 65 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  ble;.  Vdbe *v;.
29dd6 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
29dd7 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
29dd8 74 20 69 44 62 3b 0a 0a 20 20 69 44 62 20 3d 20  t iDb;..  iDb = 
29dd9 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
29dda 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
29ddb 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65   pTrigger->pSche
29ddc 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ma);.  assert( i
29ddd 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d  Db>=0 && iDb<db-
29dde 3e 6e 44 62 20 29 3b 0a 20 20 70 54 61 62 6c 65  >nDb );.  pTable
29ddf 20 3d 20 74 61 62 6c 65 4f 66 54 72 69 67 67 65   = tableOfTrigge
29de0 72 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20 61  r(pTrigger);.  a
29de1 73 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b  ssert( pTable );
29de2 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c  .  assert( pTabl
29de3 65 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 72 69  e->pSchema==pTri
29de4 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 7c 7c  gger->pSchema ||
29de5 20 69 44 62 3d 3d 31 20 29 3b 0a 23 69 66 6e 64   iDb==1 );.#ifnd
29de6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
29de7 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b  UTHORIZATION.  {
29de8 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20  .    int code = 
29de9 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47  SQLITE_DROP_TRIG
29dea 47 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  GER;.    const c
29deb 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
29dec 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
29ded 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
29dee 54 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42  Tab = SCHEMA_TAB
29def 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 69 66 28  LE(iDb);.    if(
29df0 20 69 44 62 3d 3d 31 20 29 20 63 6f 64 65 20 3d   iDb==1 ) code =
29df1 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
29df2 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 69  P_TRIGGER;.    i
29df3 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
29df4 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65  eck(pParse, code
29df5 2c 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65  , pTrigger->name
29df6 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c  , pTable->zName,
29df7 20 7a 44 62 29 20 7c 7c 0a 20 20 20 20 20 20 73   zDb) ||.      s
29df8 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
29df9 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44  pParse, SQLITE_D
29dfa 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20  ELETE, zTab, 0, 
29dfb 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 72 65  zDb) ){.      re
29dfc 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
29dfd 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e  #endif..  /* Gen
29dfe 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 65  erate code to de
29dff 73 74 72 6f 79 20 74 68 65 20 64 61 74 61 62 61  stroy the databa
29e00 73 65 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65  se record of the
29e01 20 74 72 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20   trigger..  */. 
29e02 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 21   assert( pTable!
29e03 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 76 20 3d  =0 );.  if( (v =
29e04 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
29e05 70 50 61 72 73 65 29 29 21 3d 30 20 29 7b 0a 20  pParse))!=0 ){. 
29e06 20 20 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20     int base;.   
29e07 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64   static const Vd
29e08 62 65 4f 70 4c 69 73 74 20 64 72 6f 70 54 72 69  beOpList dropTri
29e09 67 67 65 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  gger[] = {.     
29e0a 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20   { OP_Rewind,   
29e0b 20 20 30 2c 20 41 44 44 52 28 39 29 2c 20 20 30    0, ADDR(9),  0
29e0c 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  },.      { OP_St
29e0d 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 31 2c 20  ring8,    0, 1, 
29e0e 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 31 20         0}, /* 1 
29e0f 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f  */.      { OP_Co
29e10 6c 75 6d 6e 2c 20 20 20 20 20 30 2c 20 31 2c 20  lumn,     0, 1, 
29e11 20 20 20 20 20 20 20 32 7d 2c 0a 20 20 20 20 20         2},.     
29e12 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20   { OP_Ne,       
29e13 20 20 32 2c 20 41 44 44 52 28 38 29 2c 20 20 31    2, ADDR(8),  1
29e14 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  },.      { OP_St
29e15 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 31 2c 20  ring8,    0, 1, 
29e16 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 34 3a         0}, /* 4:
29e17 20 22 74 72 69 67 67 65 72 22 20 2a 2f 0a 20 20   "trigger" */.  
29e18 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c      { OP_Column,
29e19 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20       0, 0,      
29e1a 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    2},.      { OP
29e1b 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c 20  _Ne,         2, 
29e1c 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20  ADDR(8),  1},.  
29e1d 20 20 20 20 7b 20 4f 50 5f 44 65 6c 65 74 65 2c      { OP_Delete,
29e1e 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20       0, 0,      
29e1f 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    0},.      { OP
29e20 5f 4e 65 78 74 2c 20 20 20 20 20 20 20 30 2c 20  _Next,       0, 
29e21 41 44 44 52 28 31 29 2c 20 20 30 7d 2c 20 2f 2a  ADDR(1),  0}, /*
29e22 20 38 20 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20   8 */.    };..  
29e23 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
29e24 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
29e25 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20  rse, 0, iDb);.  
29e26 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73    sqlite3OpenMas
29e27 74 65 72 54 61 62 6c 65 28 70 50 61 72 73 65 2c  terTable(pParse,
29e28 20 69 44 62 29 3b 0a 20 20 20 20 62 61 73 65 20   iDb);.    base 
29e29 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
29e2a 4f 70 4c 69 73 74 28 76 2c 20 20 41 72 72 61 79  OpList(v,  Array
29e2b 53 69 7a 65 28 64 72 6f 70 54 72 69 67 67 65 72  Size(dropTrigger
29e2c 29 2c 20 64 72 6f 70 54 72 69 67 67 65 72 29 3b  ), dropTrigger);
29e2d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29e2e 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61 73 65  ChangeP4(v, base
29e2f 2b 31 2c 20 70 54 72 69 67 67 65 72 2d 3e 6e 61  +1, pTrigger->na
29e30 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  me, 0);.    sqli
29e31 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
29e32 76 2c 20 62 61 73 65 2b 34 2c 20 22 74 72 69 67  v, base+4, "trig
29e33 67 65 72 22 2c 20 50 34 5f 53 54 41 54 49 43 29  ger", P4_STATIC)
29e34 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
29e35 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
29e36 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
29e37 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29e38 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 29   OP_Close, 0, 0)
29e39 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
29e3a 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72  eAddOp4(v, OP_Dr
29e3b 6f 70 54 72 69 67 67 65 72 2c 20 69 44 62 2c 20  opTrigger, iDb, 
29e3c 30 2c 20 30 2c 20 70 54 72 69 67 67 65 72 2d 3e  0, 0, pTrigger->
29e3d 6e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 69 66  name, 0);.    if
29e3e 28 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3c 33  ( pParse->nMem<3
29e3f 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65   ){.      pParse
29e40 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20  ->nMem = 3;.    
29e41 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  }.  }.}../*.** R
29e42 65 6d 6f 76 65 20 61 20 74 72 69 67 67 65 72 20  emove a trigger 
29e43 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61  from the hash ta
29e44 62 6c 65 73 20 6f 66 20 74 68 65 20 73 71 6c 69  bles of the sqli
29e45 74 65 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  te* pointer..*/.
29e46 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
29e47 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  oid sqlite3Unlin
29e48 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65  kAndDeleteTrigge
29e49 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  r(sqlite3 *db, i
29e4a 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68  nt iDb, const ch
29e4b 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 48 61  ar *zName){.  Ha
29e4c 73 68 20 2a 70 48 61 73 68 20 3d 20 26 28 64 62  sh *pHash = &(db
29e4d 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
29e4e 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 3b 0a 20  ma->trigHash);. 
29e4f 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
29e50 65 72 3b 0a 20 20 70 54 72 69 67 67 65 72 20 3d  er;.  pTrigger =
29e51 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
29e52 72 74 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  rt(pHash, zName,
29e53 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
29e54 28 7a 4e 61 6d 65 29 2c 20 30 29 3b 0a 20 20 69  (zName), 0);.  i
29e55 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  f( pTrigger ){. 
29e56 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72 2d     if( pTrigger-
29e57 3e 70 53 63 68 65 6d 61 3d 3d 70 54 72 69 67 67  >pSchema==pTrigg
29e58 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 29  er->pTabSchema )
29e59 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  {.      Table *p
29e5a 54 61 62 20 3d 20 74 61 62 6c 65 4f 66 54 72 69  Tab = tableOfTri
29e5b 67 67 65 72 28 70 54 72 69 67 67 65 72 29 3b 0a  gger(pTrigger);.
29e5c 20 20 20 20 20 20 54 72 69 67 67 65 72 20 2a 2a        Trigger **
29e5d 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 70  pp;.      for(pp
29e5e 3d 26 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  =&pTab->pTrigger
29e5f 3b 20 2a 70 70 21 3d 70 54 72 69 67 67 65 72 3b  ; *pp!=pTrigger;
29e60 20 70 70 3d 26 28 28 2a 70 70 29 2d 3e 70 4e 65   pp=&((*pp)->pNe
29e61 78 74 29 29 3b 0a 20 20 20 20 20 20 2a 70 70 20  xt));.      *pp 
29e62 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a  = (*pp)->pNext;.
29e63 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
29e64 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64  3DeleteTrigger(d
29e65 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20  b, pTrigger);.  
29e66 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
29e67 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
29e68 67 65 73 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ges;.  }.}../*.*
29e69 2a 20 70 45 4c 69 73 74 20 69 73 20 74 68 65 20  * pEList is the 
29e6a 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e  SET clause of an
29e6b 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
29e6c 74 2e 20 20 45 61 63 68 20 65 6e 74 72 79 0a 2a  t.  Each entry.*
29e6d 2a 20 69 6e 20 70 45 4c 69 73 74 20 69 73 20 6f  * in pEList is o
29e6e 66 20 74 68 65 20 66 6f 72 6d 61 74 20 3c 69 64  f the format <id
29e6f 3e 3d 3c 65 78 70 72 3e 2e 20 20 49 66 20 61 6e  >=<expr>.  If an
29e70 79 20 6f 66 20 74 68 65 20 65 6e 74 72 69 65 73  y of the entries
29e71 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20 68 61  .** in pEList ha
29e72 76 65 20 61 6e 20 3c 69 64 3e 20 77 68 69 63 68  ve an <id> which
29e73 20 6d 61 74 63 68 65 73 20 61 6e 20 69 64 65 6e   matches an iden
29e74 74 69 66 69 65 72 20 69 6e 20 70 49 64 4c 69 73  tifier in pIdLis
29e75 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72  t,.** then retur
29e76 6e 20 54 52 55 45 2e 20 20 49 66 20 70 49 64 4c  n TRUE.  If pIdL
29e77 69 73 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20  ist==NULL, then 
29e78 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  it is considered
29e79 20 61 0a 2a 2a 20 77 69 6c 64 63 61 72 64 20 74   a.** wildcard t
29e7a 68 61 74 20 6d 61 74 63 68 65 73 20 61 6e 79 74  hat matches anyt
29e7b 68 69 6e 67 2e 20 20 4c 69 6b 65 77 69 73 65 20  hing.  Likewise 
29e7c 69 66 20 70 45 4c 69 73 74 3d 3d 4e 55 4c 4c 20  if pEList==NULL 
29e7d 74 68 65 6e 0a 2a 2a 20 69 74 20 6d 61 74 63 68  then.** it match
29e7e 65 73 20 61 6e 79 74 68 69 6e 67 20 73 6f 20 61  es anything so a
29e7f 6c 77 61 79 73 20 72 65 74 75 72 6e 20 74 72 75  lways return tru
29e80 65 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65  e.  Return false
29e81 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68 65 72   only.** if ther
29e82 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a  e is no match..*
29e83 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65  /.static int che
29e84 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70 28  ckColumnOverLap(
29e85 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 2c  IdList *pIdList,
29e86 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
29e87 74 29 7b 0a 20 20 69 6e 74 20 65 3b 0a 20 20 69  t){.  int e;.  i
29e88 66 28 20 21 70 49 64 4c 69 73 74 20 7c 7c 20 21  f( !pIdList || !
29e89 70 45 4c 69 73 74 20 29 20 72 65 74 75 72 6e 20  pEList ) return 
29e8a 31 3b 0a 20 20 66 6f 72 28 65 3d 30 3b 20 65 3c  1;.  for(e=0; e<
29e8b 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 65  pEList->nExpr; e
29e8c 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
29e8d 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28  ite3IdListIndex(
29e8e 70 49 64 4c 69 73 74 2c 20 70 45 4c 69 73 74 2d  pIdList, pEList-
29e8f 3e 61 5b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30 20  >a[e].zName)>=0 
29e90 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a  ) return 1;.  }.
29e91 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a    return 0; .}..
29e92 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c  /*.** Return a l
29e93 69 73 74 20 6f 66 20 61 6c 6c 20 74 72 69 67 67  ist of all trigg
29e94 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54 61  ers on table pTa
29e95 62 20 69 66 20 74 68 65 72 65 20 65 78 69 73 74  b if there exist
29e96 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 6f 6e  s at least.** on
29e97 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20 6d  e trigger that m
29e98 75 73 74 20 62 65 20 66 69 72 65 64 20 77 68 65  ust be fired whe
29e99 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 6f  n an operation o
29e9a 66 20 74 79 70 65 20 27 6f 70 27 20 69 73 20 0a  f type 'op' is .
29e9b 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20  ** performed on 
29e9c 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 2c 20  the table, and, 
29e9d 69 66 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f  if that operatio
29e9e 6e 20 69 73 20 61 6e 20 55 50 44 41 54 45 2c 20  n is an UPDATE, 
29e9f 69 66 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 6f  if at.** least o
29ea0 6e 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ne of the column
29ea1 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20 69 73  s in pChanges is
29ea2 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e   being modified.
29ea3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
29ea4 54 45 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69  TE Trigger *sqli
29ea5 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74  te3TriggersExist
29ea6 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
29ea7 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e,          /* P
29ea8 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  arse context */.
29ea9 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
29eaa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
29eab 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6e 74 61   table the conta
29eac 69 6e 73 20 74 68 65 20 74 72 69 67 67 65 72 73  ins the triggers
29ead 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
29eae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29eaf 20 6f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54   one of TK_DELET
29eb0 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b  E, TK_INSERT, TK
29eb1 5f 55 50 44 41 54 45 20 2a 2f 0a 20 20 45 78 70  _UPDATE */.  Exp
29eb2 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
29eb3 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20       /* Columns 
29eb4 74 68 61 74 20 63 68 61 6e 67 65 20 69 6e 20 61  that change in a
29eb5 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  n UPDATE stateme
29eb6 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4d 61  nt */.  int *pMa
29eb7 73 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  sk              
29eb8 2f 2a 20 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20  /* OUT: Mask of 
29eb9 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 7c 54  TRIGGER_BEFORE|T
29eba 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a  RIGGER_AFTER */.
29ebb 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20  ){.  int mask = 
29ebc 30 3b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4c  0;.  Trigger *pL
29ebd 69 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72 69  ist = sqlite3Tri
29ebe 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ggerList(pParse,
29ebf 20 70 54 61 62 29 3b 0a 20 20 54 72 69 67 67 65   pTab);.  Trigge
29ec0 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  r *p;.  assert( 
29ec1 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 49 73 56 69  pList==0 || IsVi
29ec2 72 74 75 61 6c 28 70 54 61 62 29 3d 3d 30 20 29  rtual(pTab)==0 )
29ec3 3b 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73 74 3b  ;.  for(p=pList;
29ec4 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
29ec5 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  .    if( p->op==
29ec6 6f 70 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d  op && checkColum
29ec7 6e 4f 76 65 72 4c 61 70 28 70 2d 3e 70 43 6f 6c  nOverLap(p->pCol
29ec8 75 6d 6e 73 2c 20 70 43 68 61 6e 67 65 73 29 20  umns, pChanges) 
29ec9 29 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d  ){.      mask |=
29eca 20 70 2d 3e 74 72 5f 74 6d 3b 0a 20 20 20 20 7d   p->tr_tm;.    }
29ecb 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d 61 73 6b  .  }.  if( pMask
29ecc 20 29 7b 0a 20 20 20 20 2a 70 4d 61 73 6b 20 3d   ){.    *pMask =
29ecd 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 72 65 74   mask;.  }.  ret
29ece 75 72 6e 20 28 6d 61 73 6b 20 3f 20 70 4c 69 73  urn (mask ? pLis
29ecf 74 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  t : 0);.}../*.**
29ed0 20 43 6f 6e 76 65 72 74 20 74 68 65 20 70 53 74   Convert the pSt
29ed1 65 70 2d 3e 74 61 72 67 65 74 20 74 6f 6b 65 6e  ep->target token
29ed2 20 69 6e 74 6f 20 61 20 53 72 63 4c 69 73 74 20   into a SrcList 
29ed3 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
29ed4 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 61 74 20  nter.** to that 
29ed5 53 72 63 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54  SrcList..**.** T
29ed6 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64 73  his routine adds
29ed7 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74 61   a specific data
29ed8 62 61 73 65 20 6e 61 6d 65 2c 20 69 66 20 6e 65  base name, if ne
29ed9 65 64 65 64 2c 20 74 6f 20 74 68 65 20 74 61 72  eded, to the tar
29eda 67 65 74 20 77 68 65 6e 0a 2a 2a 20 66 6f 72 6d  get when.** form
29edb 69 6e 67 20 74 68 65 20 53 72 63 4c 69 73 74 2e  ing the SrcList.
29edc 20 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20    This prevents 
29edd 61 20 74 72 69 67 67 65 72 20 69 6e 20 6f 6e 65  a trigger in one
29ede 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d 0a 2a   database from.*
29edf 2a 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20 61  * referring to a
29ee0 20 74 61 72 67 65 74 20 69 6e 20 61 6e 6f 74 68   target in anoth
29ee1 65 72 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e  er database.  An
29ee2 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 77 68   exception is wh
29ee3 65 6e 20 74 68 65 0a 2a 2a 20 74 72 69 67 67 65  en the.** trigge
29ee4 72 20 69 73 20 69 6e 20 54 45 4d 50 20 69 6e 20  r is in TEMP in 
29ee5 77 68 69 63 68 20 63 61 73 65 20 69 74 20 63 61  which case it ca
29ee6 6e 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6f  n refer to any o
29ee7 74 68 65 72 20 64 61 74 61 62 61 73 65 20 69 74  ther database it
29ee8 0a 2a 2a 20 77 61 6e 74 73 2e 0a 2a 2f 0a 73 74  .** wants..*/.st
29ee9 61 74 69 63 20 53 72 63 4c 69 73 74 20 2a 74 61  atic SrcList *ta
29eea 72 67 65 74 53 72 63 4c 69 73 74 28 0a 20 20 50  rgetSrcList(.  P
29eeb 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
29eec 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
29eed 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
29eee 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
29eef 65 70 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67  ep   /* The trig
29ef0 67 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ger containing t
29ef1 68 65 20 74 61 72 67 65 74 20 74 6f 6b 65 6e 20  he target token 
29ef2 2a 2f 0a 29 7b 0a 20 20 54 6f 6b 65 6e 20 73 44  */.){.  Token sD
29ef3 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
29ef4 44 75 6d 6d 79 20 64 61 74 61 62 61 73 65 20 6e  Dummy database n
29ef5 61 6d 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69  ame token */.  i
29ef6 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
29ef7 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
29ef8 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
29ef9 75 73 65 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  use */.  SrcList
29efa 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20 2f 2a   *pSrc;       /*
29efb 20 53 72 63 4c 69 73 74 20 74 6f 20 62 65 20 72   SrcList to be r
29efc 65 74 75 72 6e 65 64 20 2a 2f 0a 0a 20 20 69 44  eturned */..  iD
29efd 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
29efe 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
29eff 3e 64 62 2c 20 70 53 74 65 70 2d 3e 70 54 72 69  >db, pStep->pTri
29f00 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 69  g->pSchema);.  i
29f01 66 28 20 69 44 62 3d 3d 30 20 7c 7c 20 69 44 62  f( iDb==0 || iDb
29f02 3e 3d 32 20 29 7b 0a 20 20 20 20 61 73 73 65 72  >=2 ){.    asser
29f03 74 28 20 69 44 62 3c 70 50 61 72 73 65 2d 3e 64  t( iDb<pParse->d
29f04 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 73 44  b->nDb );.    sD
29f05 62 2e 7a 20 3d 20 28 75 38 2a 29 70 50 61 72 73  b.z = (u8*)pPars
29f06 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e->db->aDb[iDb].
29f07 7a 4e 61 6d 65 3b 0a 20 20 20 20 73 44 62 2e 6e  zName;.    sDb.n
29f08 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
29f09 33 30 28 28 63 68 61 72 2a 29 73 44 62 2e 7a 29  30((char*)sDb.z)
29f0a 3b 0a 20 20 20 20 73 44 62 2e 71 75 6f 74 65 64  ;.    sDb.quoted
29f0b 20 3d 20 30 3b 0a 20 20 20 20 70 53 72 63 20 3d   = 0;.    pSrc =
29f0c 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
29f0d 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
29f0e 2c 20 30 2c 20 26 73 44 62 2c 20 26 70 53 74 65  , 0, &sDb, &pSte
29f0f 70 2d 3e 74 61 72 67 65 74 29 3b 0a 20 20 7d 20  p->target);.  } 
29f10 65 6c 73 65 20 7b 0a 20 20 20 20 70 53 72 63 20  else {.    pSrc 
29f11 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
29f12 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64  Append(pParse->d
29f13 62 2c 20 30 2c 20 26 70 53 74 65 70 2d 3e 74 61  b, 0, &pStep->ta
29f14 72 67 65 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  rget, 0);.  }.  
29f15 72 65 74 75 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a  return pSrc;.}..
29f16 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56  /*.** Generate V
29f17 44 42 45 20 63 6f 64 65 20 66 6f 72 20 7a 65 72  DBE code for zer
29f18 6f 20 6f 72 20 6d 6f 72 65 20 73 74 61 74 65 6d  o or more statem
29f19 65 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65 20  ents inside the 
29f1a 62 6f 64 79 20 6f 66 20 61 0a 2a 2a 20 74 72 69  body of a.** tri
29f1b 67 67 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74 69  gger.  .*/.stati
29f1c 63 20 69 6e 74 20 63 6f 64 65 54 72 69 67 67 65  c int codeTrigge
29f1d 72 50 72 6f 67 72 61 6d 28 0a 20 20 50 61 72 73  rProgram(.  Pars
29f1e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
29f1f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
29f20 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
29f21 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53   TriggerStep *pS
29f22 74 65 70 4c 69 73 74 2c 20 20 20 2f 2a 20 4c 69  tepList,   /* Li
29f23 73 74 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73  st of statements
29f24 20 69 6e 73 69 64 65 20 74 68 65 20 74 72 69 67   inside the trig
29f25 67 65 72 20 62 6f 64 79 20 2a 2f 0a 20 20 69 6e  ger body */.  in
29f26 74 20 6f 72 63 6f 6e 66 69 6e 20 20 20 20 20 20  t orconfin      
29f27 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c          /* Confl
29f28 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 2e 20 28  ict algorithm. (
29f29 4f 45 5f 41 62 6f 72 74 2c 20 65 74 63 29 20 2a  OE_Abort, etc) *
29f2a 2f 20 20 0a 29 7b 0a 20 20 54 72 69 67 67 65 72  /  .){.  Trigger
29f2b 53 74 65 70 20 2a 20 70 54 72 69 67 67 65 72 53  Step * pTriggerS
29f2c 74 65 70 20 3d 20 70 53 74 65 70 4c 69 73 74 3b  tep = pStepList;
29f2d 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 0a 20  .  int orconf;. 
29f2e 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
29f2f 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c 69  e->pVdbe;.  sqli
29f30 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
29f31 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  ->db;..  assert(
29f32 20 70 54 72 69 67 67 65 72 53 74 65 70 21 3d 30   pTriggerStep!=0
29f33 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21   );.  assert( v!
29f34 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  =0 );.  sqlite3V
29f35 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29f36 43 6f 6e 74 65 78 74 50 75 73 68 2c 20 30 2c 20  ContextPush, 0, 
29f37 30 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e  0);.  VdbeCommen
29f38 74 28 28 76 2c 20 22 62 65 67 69 6e 20 74 72 69  t((v, "begin tri
29f39 67 67 65 72 20 25 73 22 2c 20 70 53 74 65 70 4c  gger %s", pStepL
29f3a 69 73 74 2d 3e 70 54 72 69 67 2d 3e 6e 61 6d 65  ist->pTrig->name
29f3b 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 54 72  ));.  while( pTr
29f3c 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20  iggerStep ){.   
29f3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
29f3e 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a  eClear(pParse);.
29f3f 20 20 20 20 6f 72 63 6f 6e 66 20 3d 20 28 6f 72      orconf = (or
29f40 63 6f 6e 66 69 6e 20 3d 3d 20 4f 45 5f 44 65 66  confin == OE_Def
29f41 61 75 6c 74 29 3f 70 54 72 69 67 67 65 72 53 74  ault)?pTriggerSt
29f42 65 70 2d 3e 6f 72 63 6f 6e 66 3a 6f 72 63 6f 6e  ep->orconf:orcon
29f43 66 69 6e 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  fin;.    pParse-
29f44 3e 74 72 69 67 53 74 61 63 6b 2d 3e 6f 72 63 6f  >trigStack->orco
29f45 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 20  nf = orconf;.   
29f46 20 73 77 69 74 63 68 28 20 70 54 72 69 67 67 65   switch( pTrigge
29f47 72 53 74 65 70 2d 3e 6f 70 20 29 7b 0a 20 20 20  rStep->op ){.   
29f48 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
29f49 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 65 6c  T: {.        Sel
29f4a 65 63 74 20 2a 73 73 20 3d 20 73 71 6c 69 74 65  ect *ss = sqlite
29f4b 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
29f4c 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65  TriggerStep->pSe
29f4d 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  lect, 0);.      
29f4e 20 20 69 66 28 20 73 73 20 29 7b 0a 20 20 20 20    if( ss ){.    
29f4f 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74        SelectDest
29f50 20 64 65 73 74 3b 0a 0a 20 20 20 20 20 20 20 20   dest;..        
29f51 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
29f52 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53  estInit(&dest, S
29f53 52 54 5f 44 69 73 63 61 72 64 2c 20 30 29 3b 0a  RT_Discard, 0);.
29f54 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29f55 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
29f56 73 73 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20  ss, &dest);.    
29f57 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
29f58 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 73 73  ectDelete(db, ss
29f59 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
29f5a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29f5b 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54    }.      case T
29f5c 4b 5f 55 50 44 41 54 45 3a 20 7b 0a 20 20 20 20  K_UPDATE: {.    
29f5d 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72      SrcList *pSr
29f5e 63 3b 0a 20 20 20 20 20 20 20 20 70 53 72 63 20  c;.        pSrc 
29f5f 3d 20 74 61 72 67 65 74 53 72 63 4c 69 73 74 28  = targetSrcList(
29f60 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
29f61 53 74 65 70 29 3b 0a 20 20 20 20 20 20 20 20 73  Step);.        s
29f62 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29f63 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e  (v, OP_ResetCoun
29f64 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 0, 0);.      
29f65 20 20 73 71 6c 69 74 65 33 55 70 64 61 74 65 28    sqlite3Update(
29f66 70 50 61 72 73 65 2c 20 70 53 72 63 2c 0a 20 20  pParse, pSrc,.  
29f67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
29f68 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
29f69 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74 65  (db, pTriggerSte
29f6a 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c 20 30 29  p->pExprList, 0)
29f6b 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
29f6c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
29f6d 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74  p(db, pTriggerSt
29f6e 65 70 2d 3e 70 57 68 65 72 65 2c 20 30 29 2c 20  ep->pWhere, 0), 
29f6f 6f 72 63 6f 6e 66 29 3b 0a 20 20 20 20 20 20 20  orconf);.       
29f70 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29f71 70 32 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f  p2(v, OP_ResetCo
29f72 75 6e 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  unt, 1, 0);.    
29f73 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
29f74 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b   }.      case TK
29f75 5f 49 4e 53 45 52 54 3a 20 7b 0a 20 20 20 20 20  _INSERT: {.     
29f76 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
29f77 3b 0a 20 20 20 20 20 20 20 20 70 53 72 63 20 3d  ;.        pSrc =
29f78 20 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70   targetSrcList(p
29f79 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 53  Parse, pTriggerS
29f7a 74 65 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71  tep);.        sq
29f7b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29f7c 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74  v, OP_ResetCount
29f7d 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
29f7e 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70   sqlite3Insert(p
29f7f 50 61 72 73 65 2c 20 70 53 72 63 2c 0a 20 20 20  Parse, pSrc,.   
29f80 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
29f81 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 54  prListDup(db, pT
29f82 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70  riggerStep->pExp
29f83 72 4c 69 73 74 2c 20 30 29 2c 20 0a 20 20 20 20  rList, 0), .    
29f84 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
29f85 65 63 74 44 75 70 28 64 62 2c 20 70 54 72 69 67  ectDup(db, pTrig
29f86 67 65 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74  gerStep->pSelect
29f87 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20  , 0), .         
29f88 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75   sqlite3IdListDu
29f89 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74  p(db, pTriggerSt
29f8a 65 70 2d 3e 70 49 64 4c 69 73 74 29 2c 20 6f 72  ep->pIdList), or
29f8b 63 6f 6e 66 29 3b 0a 20 20 20 20 20 20 20 20 73  conf);.        s
29f8c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29f8d 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e  (v, OP_ResetCoun
29f8e 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 1, 0);.      
29f8f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
29f90 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 44  .      case TK_D
29f91 45 4c 45 54 45 3a 20 7b 0a 20 20 20 20 20 20 20  ELETE: {.       
29f92 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a   SrcList *pSrc;.
29f93 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29f94 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29f95 52 65 73 65 74 43 6f 75 6e 74 2c 20 30 2c 20 30  ResetCount, 0, 0
29f96 29 3b 0a 20 20 20 20 20 20 20 20 70 53 72 63 20  );.        pSrc 
29f97 3d 20 74 61 72 67 65 74 53 72 63 4c 69 73 74 28  = targetSrcList(
29f98 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
29f99 53 74 65 70 29 3b 0a 20 20 20 20 20 20 20 20 73  Step);.        s
29f9a 71 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d  qlite3DeleteFrom
29f9b 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c 20 0a  (pParse, pSrc, .
29f9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29f9d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29f9e 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 54 72  3ExprDup(db, pTr
29f9f 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72  iggerStep->pWher
29fa0 65 2c 20 30 29 29 3b 0a 20 20 20 20 20 20 20 20  e, 0));.        
29fa1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29fa2 32 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75  2(v, OP_ResetCou
29fa3 6e 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20  nt, 1, 0);.     
29fa4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
29fa5 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  }.      default:
29fa6 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
29fa7 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 20 20 70  0);.    } .    p
29fa8 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 70 54  TriggerStep = pT
29fa9 72 69 67 67 65 72 53 74 65 70 2d 3e 70 4e 65 78  riggerStep->pNex
29faa 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  t;.  }.  sqlite3
29fab 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29fac 5f 43 6f 6e 74 65 78 74 50 6f 70 2c 20 30 2c 20  _ContextPop, 0, 
29fad 30 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e  0);.  VdbeCommen
29fae 74 28 28 76 2c 20 22 65 6e 64 20 74 72 69 67 67  t((v, "end trigg
29faf 65 72 20 25 73 22 2c 20 70 53 74 65 70 4c 69 73  er %s", pStepLis
29fb0 74 2d 3e 70 54 72 69 67 2d 3e 6e 61 6d 65 29 29  t->pTrig->name))
29fb1 3b 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  ;..  return 0;.}
29fb2 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
29fb3 63 61 6c 6c 65 64 20 74 6f 20 63 6f 64 65 20 46  called to code F
29fb4 4f 52 20 45 41 43 48 20 52 4f 57 20 74 72 69 67  OR EACH ROW trig
29fb5 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  gers..**.** When
29fb6 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 74   the code that t
29fb7 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 67 65 6e  his function gen
29fb8 65 72 61 74 65 73 20 69 73 20 65 78 65 63 75 74  erates is execut
29fb9 65 64 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ed, the followin
29fba 67 20 0a 2a 2a 20 6d 75 73 74 20 62 65 20 74 72  g .** must be tr
29fbb 75 65 3a 0a 2a 2a 0a 2a 2a 20 31 2e 20 4e 6f 20  ue:.**.** 1. No 
29fbc 63 75 72 73 6f 72 73 20 6d 61 79 20 62 65 20 6f  cursors may be o
29fbd 70 65 6e 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  pen in the main 
29fbe 64 61 74 61 62 61 73 65 2e 20 20 28 42 75 74 20  database.  (But 
29fbf 6e 65 77 49 64 78 20 61 6e 64 20 6f 6c 64 49 64  newIdx and oldId
29fc0 78 0a 2a 2a 20 20 20 20 63 61 6e 20 62 65 20 69  x.**    can be i
29fc1 6e 64 69 63 65 73 20 6f 66 20 63 75 72 73 6f 72  ndices of cursor
29fc2 73 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 74  s in temporary t
29fc3 61 62 6c 65 73 2e 20 20 53 65 65 20 62 65 6c 6f  ables.  See belo
29fc4 77 2e 29 0a 2a 2a 0a 2a 2a 20 32 2e 20 49 66 20  w.).**.** 2. If 
29fc5 74 68 65 20 74 72 69 67 67 65 72 73 20 62 65 69  the triggers bei
29fc6 6e 67 20 63 6f 64 65 64 20 61 72 65 20 4f 4e 20  ng coded are ON 
29fc7 49 4e 53 45 52 54 20 6f 72 20 4f 4e 20 55 50 44  INSERT or ON UPD
29fc8 41 54 45 20 74 72 69 67 67 65 72 73 2c 20 74 68  ATE triggers, th
29fc9 65 6e 0a 2a 2a 20 20 20 20 61 20 74 65 6d 70 6f  en.**    a tempo
29fca 72 61 72 79 20 76 64 62 65 20 63 75 72 73 6f 72  rary vdbe cursor
29fcb 20 28 69 6e 64 65 78 20 6e 65 77 49 64 78 29 20   (index newIdx) 
29fcc 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61 6e 64  must be open and
29fcd 20 70 6f 69 6e 74 69 6e 67 20 61 74 0a 2a 2a 20   pointing at.** 
29fce 20 20 20 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e     a row contain
29fcf 69 6e 67 20 76 61 6c 75 65 73 20 74 6f 20 62 65  ing values to be
29fd0 20 73 75 62 73 74 69 74 75 74 65 64 20 66 6f 72   substituted for
29fd1 20 6e 65 77 2e 2a 20 65 78 70 72 65 73 73 69 6f   new.* expressio
29fd2 6e 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  ns in the.**    
29fd3 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 28  trigger program(
29fd4 73 29 2e 0a 2a 2a 0a 2a 2a 20 33 2e 20 49 66 20  s)..**.** 3. If 
29fd5 74 68 65 20 74 72 69 67 67 65 72 73 20 62 65 69  the triggers bei
29fd6 6e 67 20 63 6f 64 65 64 20 61 72 65 20 4f 4e 20  ng coded are ON 
29fd7 44 45 4c 45 54 45 20 6f 72 20 4f 4e 20 55 50 44  DELETE or ON UPD
29fd8 41 54 45 20 74 72 69 67 67 65 72 73 2c 20 74 68  ATE triggers, th
29fd9 65 6e 0a 2a 2a 20 20 20 20 61 20 74 65 6d 70 6f  en.**    a tempo
29fda 72 61 72 79 20 76 64 62 65 20 63 75 72 73 6f 72  rary vdbe cursor
29fdb 20 28 69 6e 64 65 78 20 6f 6c 64 49 64 78 29 20   (index oldIdx) 
29fdc 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61 6e 64  must be open and
29fdd 20 70 6f 69 6e 74 69 6e 67 20 61 74 0a 2a 2a 20   pointing at.** 
29fde 20 20 20 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e     a row contain
29fdf 69 6e 67 20 76 61 6c 75 65 73 20 74 6f 20 62 65  ing values to be
29fe0 20 73 75 62 73 74 69 74 75 74 65 64 20 66 6f 72   substituted for
29fe1 20 6f 6c 64 2e 2a 20 65 78 70 72 65 73 73 69 6f   old.* expressio
29fe2 6e 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  ns in the.**    
29fe3 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 28  trigger program(
29fe4 73 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  s)..**.** If the
29fe5 79 20 61 72 65 20 6e 6f 74 20 4e 55 4c 4c 2c 20  y are not NULL, 
29fe6 74 68 65 20 70 69 4f 6c 64 43 6f 6c 4d 61 73 6b  the piOldColMask
29fe7 20 61 6e 64 20 70 69 4e 65 77 43 6f 6c 4d 61 73   and piNewColMas
29fe8 6b 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  k output variabl
29fe9 65 73 0a 2a 2a 20 61 72 65 20 73 65 74 20 74 6f  es.** are set to
29fea 20 76 61 6c 75 65 73 20 74 68 61 74 20 64 65 73   values that des
29feb 63 72 69 62 65 20 74 68 65 20 63 6f 6c 75 6d 6e  cribe the column
29fec 73 20 75 73 65 64 20 62 79 20 74 68 65 20 74 72  s used by the tr
29fed 69 67 67 65 72 20 70 72 6f 67 72 61 6d 0a 2a 2a  igger program.**
29fee 20 69 6e 20 74 68 65 20 4f 4c 44 2e 2a 20 61 6e   in the OLD.* an
29fef 64 20 4e 45 57 2e 2a 20 74 61 62 6c 65 73 20 72  d NEW.* tables r
29ff0 65 73 70 65 63 74 69 76 65 6c 79 2e 20 49 66 20  espectively. If 
29ff1 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20 74 68 65 20  column N of the 
29ff2 0a 2a 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  .** pseudo-table
29ff3 20 69 73 20 72 65 61 64 20 61 74 20 6c 65 61 73   is read at leas
29ff4 74 20 6f 6e 63 65 2c 20 74 68 65 20 63 6f 72 72  t once, the corr
29ff5 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 6f 66  esponding bit of
29ff6 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 6d   the output.** m
29ff7 61 73 6b 20 69 73 20 73 65 74 2e 20 49 66 20 61  ask is set. If a
29ff8 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 61 6e 20   column with an 
29ff9 69 6e 64 65 78 20 67 72 65 61 74 65 72 20 74 68  index greater th
29ffa 61 6e 20 33 32 20 69 73 20 72 65 61 64 2c 20 74  an 32 is read, t
29ffb 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 6d 61 73  he.** output mas
29ffc 6b 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  k is set to the 
29ffd 73 70 65 63 69 61 6c 20 76 61 6c 75 65 20 30 78  special value 0x
29ffe 66 66 66 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2f 0a  ffffffff..**.*/.
29fff 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2a000 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  nt sqlite3CodeRo
2a001 77 54 72 69 67 67 65 72 28 0a 20 20 50 61 72 73  wTrigger(.  Pars
2a002 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2a003 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2a004 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  t */.  Trigger *
2a005 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a 20 4c  pTrigger,   /* L
2a006 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73 20  ist of triggers 
2a007 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20 2a 2f  on table pTab */
2a008 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
2a009 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
2a00a 66 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f  f TK_UPDATE, TK_
2a00b 49 4e 53 45 52 54 2c 20 54 4b 5f 44 45 4c 45 54  INSERT, TK_DELET
2a00c 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  E */.  ExprList 
2a00d 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 43  *pChanges,  /* C
2a00e 68 61 6e 67 65 73 20 6c 69 73 74 20 66 6f 72 20  hanges list for 
2a00f 61 6e 79 20 55 50 44 41 54 45 20 4f 46 20 74 72  any UPDATE OF tr
2a010 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  iggers */.  int 
2a011 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20 20  tr_tm,          
2a012 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47 47   /* One of TRIGG
2a013 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49 47 47  ER_BEFORE, TRIGG
2a014 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20 54 61  ER_AFTER */.  Ta
2a015 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
2a016 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
2a017 74 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72 73  to code triggers
2a018 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e   from */.  int n
2a019 65 77 49 64 78 2c 20 20 20 20 20 20 20 20 20 20  ewIdx,          
2a01a 2f 2a 20 54 68 65 20 69 6e 64 69 63 65 20 6f 66  /* The indice of
2a01b 20 74 68 65 20 22 6e 65 77 22 20 72 6f 77 20 74   the "new" row t
2a01c 6f 20 61 63 63 65 73 73 20 2a 2f 0a 20 20 69 6e  o access */.  in
2a01d 74 20 6f 6c 64 49 64 78 2c 20 20 20 20 20 20 20  t oldIdx,       
2a01e 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 69 63 65     /* The indice
2a01f 20 6f 66 20 74 68 65 20 22 6f 6c 64 22 20 72 6f   of the "old" ro
2a020 77 20 74 6f 20 61 63 63 65 73 73 20 2a 2f 0a 20  w to access */. 
2a021 20 69 6e 74 20 6f 72 63 6f 6e 66 2c 20 20 20 20   int orconf,    
2a022 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46        /* ON CONF
2a023 4c 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20  LICT policy */. 
2a024 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 2c   int ignoreJump,
2a025 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 72 75 63        /* Instruc
2a026 74 69 6f 6e 20 74 6f 20 6a 75 6d 70 20 74 6f 20  tion to jump to 
2a027 66 6f 72 20 52 41 49 53 45 28 49 47 4e 4f 52 45  for RAISE(IGNORE
2a028 29 20 2a 2f 0a 20 20 75 33 32 20 2a 70 69 4f 6c  ) */.  u32 *piOl
2a029 64 43 6f 6c 4d 61 73 6b 2c 20 20 20 2f 2a 20 4f  dColMask,   /* O
2a02a 55 54 3a 20 4d 61 73 6b 20 6f 66 20 63 6f 6c 75  UT: Mask of colu
2a02b 6d 6e 73 20 75 73 65 64 20 66 72 6f 6d 20 74 68  mns used from th
2a02c 65 20 4f 4c 44 2e 2a 20 74 61 62 6c 65 20 2a 2f  e OLD.* table */
2a02d 0a 20 20 75 33 32 20 2a 70 69 4e 65 77 43 6f 6c  .  u32 *piNewCol
2a02e 4d 61 73 6b 20 20 20 20 2f 2a 20 4f 55 54 3a 20  Mask    /* OUT: 
2a02f 4d 61 73 6b 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  Mask of columns 
2a030 75 73 65 64 20 66 72 6f 6d 20 74 68 65 20 4e 45  used from the NE
2a031 57 2e 2a 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a  W.* table */.){.
2a032 20 20 54 72 69 67 67 65 72 20 2a 70 3b 0a 20 20    Trigger *p;.  
2a033 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2a034 61 72 73 65 2d 3e 64 62 3b 0a 20 20 54 72 69 67  arse->db;.  Trig
2a035 67 65 72 53 74 61 63 6b 20 74 72 69 67 53 74 61  gerStack trigSta
2a036 63 6b 45 6e 74 72 79 3b 0a 0a 20 20 74 72 69 67  ckEntry;..  trig
2a037 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c 64 43 6f  StackEntry.oldCo
2a038 6c 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 74 72 69  lMask = 0;.  tri
2a039 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e 65 77 43  gStackEntry.newC
2a03a 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a 0a 20 20 61  olMask = 0;..  a
2a03b 73 73 65 72 74 28 6f 70 20 3d 3d 20 54 4b 5f 55  ssert(op == TK_U
2a03c 50 44 41 54 45 20 7c 7c 20 6f 70 20 3d 3d 20 54  PDATE || op == T
2a03d 4b 5f 49 4e 53 45 52 54 20 7c 7c 20 6f 70 20 3d  K_INSERT || op =
2a03e 3d 20 54 4b 5f 44 45 4c 45 54 45 29 3b 0a 20 20  = TK_DELETE);.  
2a03f 61 73 73 65 72 74 28 74 72 5f 74 6d 20 3d 3d 20  assert(tr_tm == 
2a040 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 7c  TRIGGER_BEFORE |
2a041 7c 20 74 72 5f 74 6d 20 3d 3d 20 54 52 49 47 47  | tr_tm == TRIGG
2a042 45 52 5f 41 46 54 45 52 20 29 3b 0a 0a 20 20 61  ER_AFTER );..  a
2a043 73 73 65 72 74 28 6e 65 77 49 64 78 20 21 3d 20  ssert(newIdx != 
2a044 2d 31 20 7c 7c 20 6f 6c 64 49 64 78 20 21 3d 20  -1 || oldIdx != 
2a045 2d 31 29 3b 0a 0a 20 20 66 6f 72 28 70 3d 70 54  -1);..  for(p=pT
2a046 72 69 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d 3e  rigger; p; p=p->
2a047 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20  pNext){.    int 
2a048 66 69 72 65 5f 74 68 69 73 20 3d 20 30 3b 0a 0a  fire_this = 0;..
2a049 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
2a04a 20 77 68 65 74 68 65 72 20 77 65 20 73 68 6f 75   whether we shou
2a04b 6c 64 20 63 6f 64 65 20 74 68 69 73 20 74 72 69  ld code this tri
2a04c 67 67 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20  gger */.    if( 
2a04d 0a 20 20 20 20 20 20 70 2d 3e 6f 70 3d 3d 6f 70  .      p->op==op
2a04e 20 26 26 20 0a 20 20 20 20 20 20 70 2d 3e 74 72   && .      p->tr
2a04f 5f 74 6d 3d 3d 74 72 5f 74 6d 20 26 26 20 0a 20  _tm==tr_tm && . 
2a050 20 20 20 20 20 28 70 2d 3e 70 53 63 68 65 6d 61       (p->pSchema
2a051 3d 3d 70 2d 3e 70 54 61 62 53 63 68 65 6d 61 20  ==p->pTabSchema 
2a052 7c 7c 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d 64  || p->pSchema==d
2a053 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
2a054 61 29 20 26 26 0a 20 20 20 20 20 20 28 6f 70 21  a) &&.      (op!
2a055 3d 54 4b 5f 55 50 44 41 54 45 7c 7c 21 70 2d 3e  =TK_UPDATE||!p->
2a056 70 43 6f 6c 75 6d 6e 73 7c 7c 63 68 65 63 6b 43  pColumns||checkC
2a057 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70 28 70 2d 3e  olumnOverLap(p->
2a058 70 43 6f 6c 75 6d 6e 73 2c 70 43 68 61 6e 67 65  pColumns,pChange
2a059 73 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  s)).    ){.     
2a05a 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70   TriggerStack *p
2a05b 53 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  S;      /* Point
2a05c 65 72 20 74 6f 20 74 72 69 67 67 65 72 2d 73 74  er to trigger-st
2a05d 61 63 6b 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20  ack entry */.   
2a05e 20 20 20 66 6f 72 28 70 53 3d 70 50 61 72 73 65     for(pS=pParse
2a05f 2d 3e 74 72 69 67 53 74 61 63 6b 3b 20 70 53 20  ->trigStack; pS 
2a060 26 26 20 70 21 3d 70 53 2d 3e 70 54 72 69 67 67  && p!=pS->pTrigg
2a061 65 72 3b 20 70 53 3d 70 53 2d 3e 70 4e 65 78 74  er; pS=pS->pNext
2a062 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70  ){}.      if( !p
2a063 53 20 29 7b 0a 20 20 20 20 20 20 20 20 66 69 72  S ){.        fir
2a064 65 5f 74 68 69 73 20 3d 20 31 3b 0a 20 20 20 20  e_this = 1;.    
2a065 20 20 7d 0a 23 69 66 20 30 20 20 20 20 2f 2a 20    }.#if 0    /* 
2a066 47 69 76 65 20 6e 6f 20 77 61 72 6e 69 6e 67 20  Give no warning 
2a067 66 6f 72 20 72 65 63 75 72 73 69 76 65 20 74 72  for recursive tr
2a068 69 67 67 65 72 73 2e 20 20 4a 75 73 74 20 64 6f  iggers.  Just do
2a069 20 6e 6f 74 20 64 6f 20 74 68 65 6d 20 2a 2f 0a   not do them */.
2a06a 20 20 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20        else{.    
2a06b 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2a06c 4d 73 67 28 70 50 61 72 73 65 2c 20 22 72 65 63  Msg(pParse, "rec
2a06d 75 72 73 69 76 65 20 74 72 69 67 67 65 72 73 20  ursive triggers 
2a06e 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20 28 25  not supported (%
2a06f 73 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  s)",.           
2a070 20 70 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20   p->name);.     
2a071 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2a072 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a  _ERROR;.      }.
2a073 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 0a 20  #endif.    }. . 
2a074 20 20 20 69 66 28 20 66 69 72 65 5f 74 68 69 73     if( fire_this
2a075 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 6e   ){.      int en
2a076 64 54 72 69 67 67 65 72 3b 0a 20 20 20 20 20 20  dTrigger;.      
2a077 45 78 70 72 20 2a 20 77 68 65 6e 45 78 70 72 3b  Expr * whenExpr;
2a078 0a 20 20 20 20 20 20 41 75 74 68 43 6f 6e 74 65  .      AuthConte
2a079 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20 20  xt sContext;.   
2a07a 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73     NameContext s
2a07b 4e 43 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  NC;..#ifndef SQL
2a07c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20  ITE_OMIT_TRACE. 
2a07d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2a07e 41 64 64 4f 70 34 28 70 50 61 72 73 65 2d 3e 70  AddOp4(pParse->p
2a07f 56 64 62 65 2c 20 4f 50 5f 54 72 61 63 65 2c 20  Vdbe, OP_Trace, 
2a080 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20  0, 0, 0,.       
2a081 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a082 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2a083 64 62 2c 20 22 2d 2d 20 54 52 49 47 47 45 52 20  db, "-- TRIGGER 
2a084 25 73 22 2c 20 70 2d 3e 6e 61 6d 65 29 2c 0a 20  %s", p->name),. 
2a085 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a086 20 20 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49         P4_DYNAMI
2a087 43 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  C);.#endif.     
2a088 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
2a089 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
2a08a 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20       sNC.pParse 
2a08b 3d 20 70 50 61 72 73 65 3b 0a 0a 20 20 20 20 20  = pParse;..     
2a08c 20 2f 2a 20 50 75 73 68 20 61 6e 20 65 6e 74 72   /* Push an entr
2a08d 79 20 6f 6e 20 74 6f 20 74 68 65 20 74 72 69 67  y on to the trig
2a08e 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20  ger stack */.   
2a08f 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72     trigStackEntr
2a090 79 2e 70 54 72 69 67 67 65 72 20 3d 20 70 3b 0a  y.pTrigger = p;.
2a091 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b 45        trigStackE
2a092 6e 74 72 79 2e 6e 65 77 49 64 78 20 3d 20 6e 65  ntry.newIdx = ne
2a093 77 49 64 78 3b 0a 20 20 20 20 20 20 74 72 69 67  wIdx;.      trig
2a094 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c 64 49 64  StackEntry.oldId
2a095 78 20 3d 20 6f 6c 64 49 64 78 3b 0a 20 20 20 20  x = oldIdx;.    
2a096 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79    trigStackEntry
2a097 2e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20  .pTab = pTab;.  
2a098 20 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74      trigStackEnt
2a099 72 79 2e 70 4e 65 78 74 20 3d 20 70 50 61 72 73  ry.pNext = pPars
2a09a 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a 20 20  e->trigStack;.  
2a09b 20 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74      trigStackEnt
2a09c 72 79 2e 69 67 6e 6f 72 65 4a 75 6d 70 20 3d 20  ry.ignoreJump = 
2a09d 69 67 6e 6f 72 65 4a 75 6d 70 3b 0a 20 20 20 20  ignoreJump;.    
2a09e 20 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74    pParse->trigSt
2a09f 61 63 6b 20 3d 20 26 74 72 69 67 53 74 61 63 6b  ack = &trigStack
2a0a0 45 6e 74 72 79 3b 0a 20 20 20 20 20 20 73 71 6c  Entry;.      sql
2a0a1 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
2a0a2 75 73 68 28 70 50 61 72 73 65 2c 20 26 73 43 6f  ush(pParse, &sCo
2a0a3 6e 74 65 78 74 2c 20 70 2d 3e 6e 61 6d 65 29 3b  ntext, p->name);
2a0a4 0a 0a 20 20 20 20 20 20 2f 2a 20 63 6f 64 65 20  ..      /* code 
2a0a5 74 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20  the WHEN clause 
2a0a6 2a 2f 0a 20 20 20 20 20 20 65 6e 64 54 72 69 67  */.      endTrig
2a0a7 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ger = sqlite3Vdb
2a0a8 65 4d 61 6b 65 4c 61 62 65 6c 28 70 50 61 72 73  eMakeLabel(pPars
2a0a9 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 20  e->pVdbe);.     
2a0aa 20 77 68 65 6e 45 78 70 72 20 3d 20 73 71 6c 69   whenExpr = sqli
2a0ab 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
2a0ac 2d 3e 70 57 68 65 6e 2c 20 30 29 3b 0a 20 20 20  ->pWhen, 0);.   
2a0ad 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
2a0ae 63 46 61 69 6c 65 64 20 7c 7c 20 73 71 6c 69 74  cFailed || sqlit
2a0af 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
2a0b0 65 73 28 26 73 4e 43 2c 20 77 68 65 6e 45 78 70  es(&sNC, whenExp
2a0b1 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50  r) ){.        pP
2a0b2 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20  arse->trigStack 
2a0b3 3d 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79  = trigStackEntry
2a0b4 2e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  .pNext;.        
2a0b5 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2a0b6 65 28 64 62 2c 20 77 68 65 6e 45 78 70 72 29 3b  e(db, whenExpr);
2a0b7 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2a0b8 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
2a0b9 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
2a0ba 6c 73 65 28 70 50 61 72 73 65 2c 20 77 68 65 6e  lse(pParse, when
2a0bb 45 78 70 72 2c 20 65 6e 64 54 72 69 67 67 65 72  Expr, endTrigger
2a0bc 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
2a0bd 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ULL);.      sqli
2a0be 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2a0bf 2c 20 77 68 65 6e 45 78 70 72 29 3b 0a 0a 20 20  , whenExpr);..  
2a0c0 20 20 20 20 63 6f 64 65 54 72 69 67 67 65 72 50      codeTriggerP
2a0c1 72 6f 67 72 61 6d 28 70 50 61 72 73 65 2c 20 70  rogram(pParse, p
2a0c2 2d 3e 73 74 65 70 5f 6c 69 73 74 2c 20 6f 72 63  ->step_list, orc
2a0c3 6f 6e 66 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a  onf); ..      /*
2a0c4 20 50 6f 70 20 74 68 65 20 65 6e 74 72 79 20 6f   Pop the entry o
2a0c5 66 66 20 74 68 65 20 74 72 69 67 67 65 72 20 73  ff the trigger s
2a0c6 74 61 63 6b 20 2a 2f 0a 20 20 20 20 20 20 70 50  tack */.      pP
2a0c7 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20  arse->trigStack 
2a0c8 3d 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79  = trigStackEntry
2a0c9 2e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71  .pNext;.      sq
2a0ca 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
2a0cb 50 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a  Pop(&sContext);.
2a0cc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2a0cd 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 70  beResolveLabel(p
2a0ce 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 65 6e  Parse->pVdbe, en
2a0cf 64 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 7d  dTrigger);.    }
2a0d0 0a 20 20 7d 0a 20 20 69 66 28 20 70 69 4f 6c 64  .  }.  if( piOld
2a0d1 43 6f 6c 4d 61 73 6b 20 29 20 2a 70 69 4f 6c 64  ColMask ) *piOld
2a0d2 43 6f 6c 4d 61 73 6b 20 7c 3d 20 74 72 69 67 53  ColMask |= trigS
2a0d3 74 61 63 6b 45 6e 74 72 79 2e 6f 6c 64 43 6f 6c  tackEntry.oldCol
2a0d4 4d 61 73 6b 3b 0a 20 20 69 66 28 20 70 69 4e 65  Mask;.  if( piNe
2a0d5 77 43 6f 6c 4d 61 73 6b 20 29 20 2a 70 69 4e 65  wColMask ) *piNe
2a0d6 77 43 6f 6c 4d 61 73 6b 20 7c 3d 20 74 72 69 67  wColMask |= trig
2a0d7 53 74 61 63 6b 45 6e 74 72 79 2e 6e 65 77 43 6f  StackEntry.newCo
2a0d8 6c 4d 61 73 6b 3b 0a 20 20 72 65 74 75 72 6e 20  lMask;.  return 
2a0d9 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  0;.}.#endif /* !
2a0da 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2a0db 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a  MIT_TRIGGER) */.
2a0dc 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2a0dd 20 45 6e 64 20 6f 66 20 74 72 69 67 67 65 72 2e   End of trigger.
2a0de 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2a0df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2a0e1 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2a0e2 20 42 65 67 69 6e 20 66 69 6c 65 20 75 70 64 61   Begin file upda
2a0e3 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te.c ***********
2a0e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2a0e6 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
2a0e7 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
2a0e8 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2a0e9 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
2a0ea 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
2a0eb 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
2a0ec 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
2a0ed 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
2a0ee 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
2a0ef 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
2a0f0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
2a0f1 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
2a0f2 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
2a0f3 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
2a0f4 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
2a0f5 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
2a0f6 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
2a0f7 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2a0f8 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2a0f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0fd 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
2a0fe 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20  file contains C 
2a0ff 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68  code routines th
2a100 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79  at are called by
2a101 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74   the parser.** t
2a102 6f 20 68 61 6e 64 6c 65 20 55 50 44 41 54 45 20  o handle UPDATE 
2a103 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
2a104 2a 20 24 49 64 3a 20 75 70 64 61 74 65 2e 63 2c  * $Id: update.c,
2a105 76 20 31 2e 32 30 30 20 32 30 30 39 2f 30 35 2f  v 1.200 2009/05/
2a106 30 35 20 31 35 3a 34 36 3a 31 30 20 64 72 68 20  05 15:46:10 drh 
2a107 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
2a108 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2a109 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 46 6f  RTUALTABLE./* Fo
2a10a 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
2a10b 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  n */.static void
2a10c 20 75 70 64 61 74 65 56 69 72 74 75 61 6c 54 61   updateVirtualTa
2a10d 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
2a10e 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  arse,       /* T
2a10f 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
2a110 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
2a111 2a 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a 20  *pSrc,       /* 
2a112 54 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  The virtual tabl
2a113 65 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65 64  e to be modified
2a114 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2a115 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
2a116 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2a117 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2a118 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 54 68 65  Changes,  /* The
2a119 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 61 6e   columns to chan
2a11a 67 65 20 69 6e 20 74 68 65 20 55 50 44 41 54 45  ge in the UPDATE
2a11b 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2a11c 45 78 70 72 20 2a 70 52 6f 77 69 64 45 78 70 72  Expr *pRowidExpr
2a11d 2c 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69  ,    /* Expressi
2a11e 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 6d  on used to recom
2a11f 70 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 2a  pute the rowid *
2a120 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 2c 20  /.  int *aXRef, 
2a121 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70           /* Mapp
2a122 69 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73  ing from columns
2a123 20 6f 66 20 70 54 61 62 20 74 6f 20 65 6e 74 72   of pTab to entr
2a124 69 65 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20  ies in pChanges 
2a125 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
2a126 65 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45  e         /* WHE
2a127 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  RE clause of the
2a128 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
2a129 74 20 2a 2f 0a 29 3b 0a 23 65 6e 64 69 66 20 2f  t */.);.#endif /
2a12a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
2a12b 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f  RTUALTABLE */../
2a12c 2a 0a 2a 2a 20 54 68 65 20 6d 6f 73 74 20 72 65  *.** The most re
2a12d 63 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e 73  cently coded ins
2a12e 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61 6e 20  truction was an 
2a12f 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74  OP_Column to ret
2a130 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 69 2d 74  rieve the.** i-t
2a131 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c  h column of tabl
2a132 65 20 70 54 61 62 2e 20 54 68 69 73 20 72 6f 75  e pTab. This rou
2a133 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 50 34  tine sets the P4
2a134 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68   parameter of th
2a135 65 20 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20  e .** OP_Column 
2a136 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  to the default v
2a137 61 6c 75 65 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a  alue, if any..**
2a138 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
2a139 76 61 6c 75 65 20 6f 66 20 61 20 63 6f 6c 75 6d  value of a colum
2a13a 6e 20 69 73 20 73 70 65 63 69 66 69 65 64 20 62  n is specified b
2a13b 79 20 61 20 44 45 46 41 55 4c 54 20 63 6c 61 75  y a DEFAULT clau
2a13c 73 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 63 6f  se in the .** co
2a13d 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e  lumn definition.
2a13e 20 54 68 69 73 20 77 61 73 20 65 69 74 68 65 72   This was either
2a13f 20 73 75 70 70 6c 69 65 64 20 62 79 20 74 68 65   supplied by the
2a140 20 75 73 65 72 20 77 68 65 6e 20 74 68 65 20 74   user when the t
2a141 61 62 6c 65 0a 2a 2a 20 77 61 73 20 63 72 65 61  able.** was crea
2a142 74 65 64 2c 20 6f 72 20 61 64 64 65 64 20 6c 61  ted, or added la
2a143 74 65 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ter to the table
2a144 20 64 65 66 69 6e 69 74 69 6f 6e 20 62 79 20 61   definition by a
2a145 6e 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2a  n ALTER TABLE.**
2a146 20 63 6f 6d 6d 61 6e 64 2e 20 49 66 20 74 68 65   command. If the
2a147 20 6c 61 74 74 65 72 2c 20 74 68 65 6e 20 74 68   latter, then th
2a148 65 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 69 6e  e row-records in
2a149 20 74 68 65 20 74 61 62 6c 65 20 62 74 72 65 65   the table btree
2a14a 20 6f 6e 20 64 69 73 6b 0a 2a 2a 20 6d 61 79 20   on disk.** may 
2a14b 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 76 61  not contain a va
2a14c 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  lue for the colu
2a14d 6d 6e 20 61 6e 64 20 74 68 65 20 64 65 66 61 75  mn and the defau
2a14e 6c 74 20 76 61 6c 75 65 2c 20 74 61 6b 65 6e 0a  lt value, taken.
2a14f 2a 2a 20 66 72 6f 6d 20 74 68 65 20 50 34 20 70  ** from the P4 p
2a150 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20  arameter of the 
2a151 4f 50 5f 43 6f 6c 75 6d 6e 20 69 6e 73 74 72 75  OP_Column instru
2a152 63 74 69 6f 6e 2c 20 69 73 20 72 65 74 75 72 6e  ction, is return
2a153 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 49  ed instead..** I
2a154 66 20 74 68 65 20 66 6f 72 6d 65 72 2c 20 74 68  f the former, th
2a155 65 6e 20 61 6c 6c 20 72 6f 77 2d 72 65 63 6f 72  en all row-recor
2a156 64 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 65  ds are guarantee
2a157 64 20 74 6f 20 69 6e 63 6c 75 64 65 20 61 20 76  d to include a v
2a158 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20  alue.** for the 
2a159 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20 50  column and the P
2a15a 34 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 72  4 value is not r
2a15b 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 43  equired..**.** C
2a15c 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
2a15d 73 20 63 72 65 61 74 65 64 20 62 79 20 61 6e 20  s created by an 
2a15e 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d  ALTER TABLE comm
2a15f 61 6e 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76  and may only hav
2a160 65 20 0a 2a 2a 20 6c 69 74 65 72 61 6c 20 64 65  e .** literal de
2a161 66 61 75 6c 74 20 76 61 6c 75 65 73 20 73 70 65  fault values spe
2a162 63 69 66 69 65 64 3a 20 61 20 6e 75 6d 62 65 72  cified: a number
2a163 2c 20 6e 75 6c 6c 20 6f 72 20 61 20 73 74 72 69  , null or a stri
2a164 6e 67 2e 20 28 49 66 20 61 20 6d 6f 72 65 0a 2a  ng. (If a more.*
2a165 2a 20 63 6f 6d 70 6c 69 63 61 74 65 64 20 64 65  * complicated de
2a166 66 61 75 6c 74 20 65 78 70 72 65 73 73 69 6f 6e  fault expression
2a167 20 76 61 6c 75 65 20 77 61 73 20 70 72 6f 76 69   value was provi
2a168 64 65 64 2c 20 69 74 20 69 73 20 65 76 61 6c 75  ded, it is evalu
2a169 61 74 65 64 20 0a 2a 2a 20 77 68 65 6e 20 74 68  ated .** when th
2a16a 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 69 73  e ALTER TABLE is
2a16b 20 65 78 65 63 75 74 65 64 20 61 6e 64 20 6f 6e   executed and on
2a16c 65 20 6f 66 20 74 68 65 20 6c 69 74 65 72 61 6c  e of the literal
2a16d 20 76 61 6c 75 65 73 20 77 72 69 74 74 65 6e 0a   values written.
2a16e 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  ** into the sqli
2a16f 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  te_master table.
2a170 29 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 66 6f 72  ).**.** Therefor
2a171 65 2c 20 74 68 65 20 50 34 20 70 61 72 61 6d 65  e, the P4 parame
2a172 74 65 72 20 69 73 20 6f 6e 6c 79 20 72 65 71 75  ter is only requ
2a173 69 72 65 64 20 69 66 20 74 68 65 20 64 65 66 61  ired if the defa
2a174 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 0a 2a 2a  ult value for.**
2a175 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 61   the column is a
2a176 20 6c 69 74 65 72 61 6c 20 6e 75 6d 62 65 72 2c   literal number,
2a177 20 73 74 72 69 6e 67 20 6f 72 20 6e 75 6c 6c 2e   string or null.
2a178 20 54 68 65 20 73 71 6c 69 74 65 33 56 61 6c 75   The sqlite3Valu
2a179 65 46 72 6f 6d 45 78 70 72 28 29 0a 2a 2a 20 66  eFromExpr().** f
2a17a 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 70 61 62  unction is capab
2a17b 6c 65 20 6f 66 20 74 72 61 6e 73 66 6f 72 6d 69  le of transformi
2a17c 6e 67 20 74 68 65 73 65 20 74 79 70 65 73 20 6f  ng these types o
2a17d 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  f expressions in
2a17e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61  to.** sqlite3_va
2a17f 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a  lue objects..*/.
2a180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2a181 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d  oid sqlite3Colum
2a182 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20 2a 76  nDefault(Vdbe *v
2a183 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 69  , Table *pTab, i
2a184 6e 74 20 69 29 7b 0a 20 20 61 73 73 65 72 74 28  nt i){.  assert(
2a185 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 69 66   pTab!=0 );.  if
2a186 28 20 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  ( !pTab->pSelect
2a187 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2a188 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 3b 0a 20  value *pValue;. 
2a189 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28     u8 enc = ENC(
2a18a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
2a18b 29 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70  );.    Column *p
2a18c 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f  Col = &pTab->aCo
2a18d 6c 5b 69 5d 3b 0a 20 20 20 20 56 64 62 65 43 6f  l[i];.    VdbeCo
2a18e 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 73  mment((v, "%s.%s
2a18f 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  ", pTab->zName, 
2a190 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20  pCol->zName));. 
2a191 20 20 20 61 73 73 65 72 74 28 20 69 3c 70 54 61     assert( i<pTa
2a192 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 73  b->nCol );.    s
2a193 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45  qlite3ValueFromE
2a194 78 70 72 28 73 71 6c 69 74 65 33 56 64 62 65 44  xpr(sqlite3VdbeD
2a195 62 28 76 29 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c  b(v), pCol->pDfl
2a196 74 2c 20 65 6e 63 2c 20 0a 20 20 20 20 20 20 20  t, enc, .       
2a197 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a198 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79    pCol->affinity
2a199 2c 20 26 70 56 61 6c 75 65 29 3b 0a 20 20 20 20  , &pValue);.    
2a19a 69 66 28 20 70 56 61 6c 75 65 20 29 7b 0a 20 20  if( pValue ){.  
2a19b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2a19c 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28  hangeP4(v, -1, (
2a19d 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 56 61  const char *)pVa
2a19e 6c 75 65 2c 20 50 34 5f 4d 45 4d 29 3b 0a 20 20  lue, P4_MEM);.  
2a19f 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2a1a0 20 50 72 6f 63 65 73 73 20 61 6e 20 55 50 44 41   Process an UPDA
2a1a1 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  TE statement..**
2a1a2 0a 2a 2a 20 20 20 55 50 44 41 54 45 20 4f 52 20  .**   UPDATE OR 
2a1a3 49 47 4e 4f 52 45 20 74 61 62 6c 65 5f 77 78 79  IGNORE table_wxy
2a1a4 7a 20 53 45 54 20 61 3d 62 2c 20 63 3d 64 20 57  z SET a=b, c=d W
2a1a5 48 45 52 45 20 65 3c 35 20 41 4e 44 20 66 20 4e  HERE e<5 AND f N
2a1a6 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20  OT NULL;.**     
2a1a7 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c       \_______/ \
2a1a8 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f  ________/     \_
2a1a9 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f  _____/       \__
2a1aa 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a  ______________/.
2a1ab 2a 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e 45  *            onE
2a1ac 72 72 6f 72 20 20 20 70 54 61 62 4c 69 73 74 20  rror   pTabList 
2a1ad 20 20 20 20 20 70 43 68 61 6e 67 65 73 20 20 20       pChanges   
2a1ae 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65            pWhere
2a1af 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2a1b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
2a1b1 70 64 61 74 65 28 0a 20 20 50 61 72 73 65 20 2a  pdate(.  Parse *
2a1b2 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2a1b3 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
2a1b4 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
2a1b5 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20  st *pTabList,   
2a1b6 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69    /* The table i
2a1b7 6e 20 77 68 69 63 68 20 77 65 20 73 68 6f 75 6c  n which we shoul
2a1b8 64 20 63 68 61 6e 67 65 20 74 68 69 6e 67 73 20  d change things 
2a1b9 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2a1ba 43 68 61 6e 67 65 73 2c 20 20 20 20 2f 2a 20 54  Changes,    /* T
2a1bb 68 69 6e 67 73 20 74 6f 20 62 65 20 63 68 61 6e  hings to be chan
2a1bc 67 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ged */.  Expr *p
2a1bd 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 20  Where,          
2a1be 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2a1bf 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c  use.  May be nul
2a1c0 6c 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72  l */.  int onErr
2a1c1 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  or            /*
2a1c2 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63   How to handle c
2a1c3 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73  onstraint errors
2a1c4 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
2a1c5 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
2a1c6 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
2a1c7 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2a1c8 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
2a1c9 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  The table to be 
2a1ca 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74  updated */.  int
2a1cb 20 61 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20   addr = 0;      
2a1cc 20 20 20 20 2f 2a 20 56 44 42 45 20 69 6e 73 74      /* VDBE inst
2a1cd 72 75 63 74 69 6f 6e 20 61 64 64 72 65 73 73 20  ruction address 
2a1ce 6f 66 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  of the start of 
2a1cf 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 57 68  the loop */.  Wh
2a1d0 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b  ereInfo *pWInfo;
2a1d1 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
2a1d2 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48  ion about the WH
2a1d3 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2a1d4 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
2a1d5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
2a1d6 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
2a1d7 6e 67 69 6e 65 20 2a 2f 0a 20 20 49 6e 64 65 78  ngine */.  Index
2a1d8 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
2a1d9 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
2a1da 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20 2a 2f   over indices */
2a1db 0a 20 20 69 6e 74 20 6e 49 64 78 3b 20 20 20 20  .  int nIdx;    
2a1dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2a1dd 62 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20 74  ber of indices t
2a1de 68 61 74 20 6e 65 65 64 20 75 70 64 61 74 69 6e  hat need updatin
2a1df 67 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  g */.  int iCur;
2a1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a1e1 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d   VDBE Cursor num
2a1e2 62 65 72 20 6f 66 20 70 54 61 62 20 2a 2f 0a 20  ber of pTab */. 
2a1e3 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
2a1e4 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
2a1e5 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72  atabase structur
2a1e6 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67  e */.  int *aReg
2a1e7 49 64 78 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  Idx = 0;      /*
2a1e8 20 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61 73   One register as
2a1e9 73 69 67 6e 65 64 20 74 6f 20 65 61 63 68 20 69  signed to each i
2a1ea 6e 64 65 78 20 74 6f 20 62 65 20 75 70 64 61 74  ndex to be updat
2a1eb 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52  ed */.  int *aXR
2a1ec 65 66 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  ef = 0;        /
2a1ed 2a 20 61 58 52 65 66 5b 69 5d 20 69 73 20 74 68  * aXRef[i] is th
2a1ee 65 20 69 6e 64 65 78 20 69 6e 20 70 43 68 61 6e  e index in pChan
2a1ef 67 65 73 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 0a  ges->a[] of the.
2a1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a1f1 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 65           ** an e
2a1f2 78 70 72 65 73 73 69 6f 6e 20 66 6f 72 20 74 68  xpression for th
2a1f3 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  e i-th column of
2a1f4 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20   the table..    
2a1f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a1f6 20 20 20 20 20 2a 2a 20 61 58 52 65 66 5b 69 5d       ** aXRef[i]
2a1f7 3d 3d 2d 31 20 69 66 20 74 68 65 20 69 2d 74 68  ==-1 if the i-th
2a1f8 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 63   column is not c
2a1f9 68 61 6e 67 65 64 2e 20 2a 2f 0a 20 20 69 6e 74  hanged. */.  int
2a1fa 20 63 68 6e 67 52 6f 77 69 64 3b 20 20 20 20 20   chngRowid;     
2a1fb 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
2a1fc 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
2a1fd 20 69 73 20 62 65 69 6e 67 20 63 68 61 6e 67 65   is being change
2a1fe 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f  d */.  Expr *pRo
2a1ff 77 69 64 45 78 70 72 20 3d 20 30 3b 20 20 2f 2a  widExpr = 0;  /*
2a200 20 45 78 70 72 65 73 73 69 6f 6e 20 64 65 66 69   Expression defi
2a201 6e 69 6e 67 20 74 68 65 20 6e 65 77 20 72 65 63  ning the new rec
2a202 6f 72 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ord number */.  
2a203 69 6e 74 20 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b  int openAll = 0;
2a204 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
2a205 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6e 65  f all indices ne
2a206 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20  ed to be opened 
2a207 2a 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74  */.  AuthContext
2a208 20 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20 54   sContext;  /* T
2a209 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  he authorization
2a20a 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 4e 61   context */.  Na
2a20b 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
2a20c 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65       /* The name
2a20d 2d 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 6f  -context to reso
2a20e 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  lve expressions 
2a20f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  in */.  int iDb;
2a210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a211 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 74 61  * Database conta
2a212 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20  ining the table 
2a213 62 65 69 6e 67 20 75 70 64 61 74 65 64 20 2a 2f  being updated */
2a214 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20  .  int j1;      
2a215 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
2a216 72 65 73 73 65 73 20 6f 66 20 6a 75 6d 70 20 69  resses of jump i
2a217 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20  nstructions */. 
2a218 20 69 6e 74 20 6f 6b 4f 6e 65 50 61 73 73 3b 20   int okOnePass; 
2a219 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2a21a 66 6f 72 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67  for one-pass alg
2a21b 6f 72 69 74 68 6d 20 77 69 74 68 6f 75 74 20 74  orithm without t
2a21c 68 65 20 46 49 46 4f 20 2a 2f 0a 0a 23 69 66 6e  he FIFO */..#ifn
2a21d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2a21e 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73  TRIGGER.  int is
2a21f 56 69 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  View;           
2a220 20 20 20 20 20 20 20 2f 2a 20 54 72 79 69 6e 67         /* Trying
2a221 20 74 6f 20 75 70 64 61 74 65 20 61 20 76 69 65   to update a vie
2a222 77 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  w */.  Trigger *
2a223 70 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 20  pTrigger;       
2a224 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74      /* List of t
2a225 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 2c  riggers on pTab,
2a226 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a   if required */.
2a227 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 42 65  #endif.  int iBe
2a228 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 72 20  ginAfterTrigger 
2a229 3d 20 30 3b 20 20 2f 2a 20 41 64 64 72 65 73 73  = 0;  /* Address
2a22a 20 6f 66 20 61 66 74 65 72 20 74 72 69 67 67 65   of after trigge
2a22b 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69  r program */.  i
2a22c 6e 74 20 69 45 6e 64 41 66 74 65 72 54 72 69 67  nt iEndAfterTrig
2a22d 67 65 72 20 3d 20 30 3b 20 20 20 20 2f 2a 20 45  ger = 0;    /* E
2a22e 78 69 74 20 6f 66 20 61 66 74 65 72 20 74 72 69  xit of after tri
2a22f 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  gger program */.
2a230 20 20 69 6e 74 20 69 42 65 67 69 6e 42 65 66 6f    int iBeginBefo
2a231 72 65 54 72 69 67 67 65 72 20 3d 20 30 3b 20 2f  reTrigger = 0; /
2a232 2a 20 41 64 64 72 65 73 73 20 6f 66 20 62 65 66  * Address of bef
2a233 6f 72 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  ore trigger prog
2a234 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e  ram */.  int iEn
2a235 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 20 3d  dBeforeTrigger =
2a236 20 30 3b 20 20 20 2f 2a 20 45 78 69 74 20 6f 66   0;   /* Exit of
2a237 20 62 65 66 6f 72 65 20 74 72 69 67 67 65 72 20   before trigger 
2a238 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 75 33 32  program */.  u32
2a239 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 3d 20   old_col_mask = 
2a23a 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73  0;        /* Mas
2a23b 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d  k of OLD.* colum
2a23c 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 75  ns in use */.  u
2a23d 33 32 20 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 20  32 new_col_mask 
2a23e 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  = 0;        /* M
2a23f 61 73 6b 20 6f 66 20 4e 45 57 2e 2a 20 63 6f 6c  ask of NEW.* col
2a240 75 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 0a  umns in use */..
2a241 20 20 69 6e 74 20 6e 65 77 49 64 78 20 20 20 20    int newIdx    
2a242 20 20 3d 20 2d 31 3b 20 20 2f 2a 20 69 6e 64 65    = -1;  /* inde
2a243 78 20 6f 66 20 74 72 69 67 67 65 72 20 22 6e 65  x of trigger "ne
2a244 77 22 20 74 65 6d 70 20 74 61 62 6c 65 20 20 20  w" temp table   
2a245 20 20 20 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64      */.  int old
2a246 49 64 78 20 20 20 20 20 20 3d 20 2d 31 3b 20 20  Idx      = -1;  
2a247 2f 2a 20 69 6e 64 65 78 20 6f 66 20 74 72 69 67  /* index of trig
2a248 67 65 72 20 22 6f 6c 64 22 20 74 65 6d 70 20 74  ger "old" temp t
2a249 61 62 6c 65 20 20 20 20 20 20 20 2a 2f 0a 0a 20  able       */.. 
2a24a 20 2f 2a 20 52 65 67 69 73 74 65 72 20 41 6c 6c   /* Register All
2a24b 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  ocations */.  in
2a24c 74 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20  t regRowCount = 
2a24d 30 3b 20 20 20 2f 2a 20 41 20 63 6f 75 6e 74 20  0;   /* A count 
2a24e 6f 66 20 72 6f 77 73 20 63 68 61 6e 67 65 64 20  of rows changed 
2a24f 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64 52  */.  int regOldR
2a250 6f 77 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 54  owid;       /* T
2a251 68 65 20 6f 6c 64 20 72 6f 77 69 64 20 2a 2f 0a  he old rowid */.
2a252 20 20 69 6e 74 20 72 65 67 4e 65 77 52 6f 77 69    int regNewRowi
2a253 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  d;       /* The 
2a254 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  new rowid */.  i
2a255 6e 74 20 72 65 67 44 61 74 61 3b 20 20 20 20 20  nt regData;     
2a256 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 64 61 74        /* New dat
2a257 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 2a 2f  a for the row */
2a258 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74  .  int regRowSet
2a259 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 52 6f 77   = 0;     /* Row
2a25a 73 65 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 62  set of rows to b
2a25b 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 0a 20 20  e updated */..  
2a25c 73 43 6f 6e 74 65 78 74 2e 70 50 61 72 73 65 20  sContext.pParse 
2a25d 3d 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 72  = 0;.  db = pPar
2a25e 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50  se->db;.  if( pP
2a25f 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
2a260 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2a261 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74  {.    goto updat
2a262 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  e_cleanup;.  }. 
2a263 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73   assert( pTabLis
2a264 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20  t->nSrc==1 );.. 
2a265 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74   /* Locate the t
2a266 61 62 6c 65 20 77 68 69 63 68 20 77 65 20 77 61  able which we wa
2a267 6e 74 20 74 6f 20 75 70 64 61 74 65 2e 20 0a 20  nt to update. . 
2a268 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   */.  pTab = sql
2a269 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
2a26a 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  p(pParse, pTabLi
2a26b 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  st);.  if( pTab=
2a26c 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65  =0 ) goto update
2a26d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 44 62 20  _cleanup;.  iDb 
2a26e 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2a26f 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
2a270 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
2a271 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20  );..  /* Figure 
2a272 6f 75 74 20 69 66 20 77 65 20 68 61 76 65 20 61  out if we have a
2a273 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20  ny triggers and 
2a274 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  if the table bei
2a275 6e 67 0a 20 20 2a 2a 20 75 70 64 61 74 65 64 20  ng.  ** updated 
2a276 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23  is a view.  */.#
2a277 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2a278 49 54 5f 54 52 49 47 47 45 52 0a 20 20 70 54 72  IT_TRIGGER.  pTr
2a279 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54  igger = sqlite3T
2a27a 72 69 67 67 65 72 73 45 78 69 73 74 28 70 50 61  riggersExist(pPa
2a27b 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 55 50  rse, pTab, TK_UP
2a27c 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20  DATE, pChanges, 
2a27d 30 29 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70  0);.  isView = p
2a27e 54 61 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b  Tab->pSelect!=0;
2a27f 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
2a280 70 54 72 69 67 67 65 72 20 30 0a 23 20 64 65 66  pTrigger 0.# def
2a281 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e  ine isView 0.#en
2a282 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
2a283 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e  E_OMIT_VIEW.# un
2a284 64 65 66 20 69 73 56 69 65 77 0a 23 20 64 65 66  def isView.# def
2a285 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e  ine isView 0.#en
2a286 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  dif..  if( sqlit
2a287 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61  e3IsReadOnly(pPa
2a288 72 73 65 2c 20 70 54 61 62 2c 20 28 70 54 72 69  rse, pTab, (pTri
2a289 67 67 65 72 3f 31 3a 30 29 29 20 29 7b 0a 20 20  gger?1:0)) ){.  
2a28a 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
2a28b 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28  eanup;.  }.  if(
2a28c 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
2a28d 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
2a28e 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20  e, pTab) ){.    
2a28f 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
2a290 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 58 52 65 66  nup;.  }.  aXRef
2a291 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2a292 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
2a293 28 69 6e 74 29 20 2a 20 70 54 61 62 2d 3e 6e 43  (int) * pTab->nC
2a294 6f 6c 20 29 3b 0a 20 20 69 66 28 20 61 58 52 65  ol );.  if( aXRe
2a295 66 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61  f==0 ) goto upda
2a296 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 66 6f  te_cleanup;.  fo
2a297 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
2a298 43 6f 6c 3b 20 69 2b 2b 29 20 61 58 52 65 66 5b  Col; i++) aXRef[
2a299 69 5d 20 3d 20 2d 31 3b 0a 0a 20 20 2f 2a 20 49  i] = -1;..  /* I
2a29a 66 20 74 68 65 72 65 20 61 72 65 20 46 4f 52 20  f there are FOR 
2a29b 45 41 43 48 20 52 4f 57 20 74 72 69 67 67 65 72  EACH ROW trigger
2a29c 73 2c 20 61 6c 6c 6f 63 61 74 65 20 63 75 72 73  s, allocate curs
2a29d 6f 72 73 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ors for the.  **
2a29e 20 73 70 65 63 69 61 6c 20 4f 4c 44 20 61 6e 64   special OLD and
2a29f 20 4e 45 57 20 74 61 62 6c 65 73 0a 20 20 2a 2f   NEW tables.  */
2a2a0 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20  .  if( pTrigger 
2a2a1 29 7b 0a 20 20 20 20 6e 65 77 49 64 78 20 3d 20  ){.    newIdx = 
2a2a2 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
2a2a3 20 20 20 20 6f 6c 64 49 64 78 20 3d 20 70 50 61      oldIdx = pPa
2a2a4 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d  rse->nTab++;.  }
2a2a5 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
2a2a6 61 20 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68  a cursors for th
2a2a7 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
2a2a8 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 61 6c  table and for al
2a2a9 6c 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 20  l indices..  ** 
2a2aa 54 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72  The index cursor
2a2ab 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 75  s might not be u
2a2ac 73 65 64 2c 20 62 75 74 20 69 66 20 74 68 65 79  sed, but if they
2a2ad 20 61 72 65 20 75 73 65 64 20 74 68 65 79 0a 20   are used they. 
2a2ae 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6f 63 63 75   ** need to occu
2a2af 72 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68  r right after th
2a2b0 65 20 64 61 74 61 62 61 73 65 20 63 75 72 73 6f  e database curso
2a2b1 72 2e 20 20 53 6f 20 67 6f 20 61 68 65 61 64 20  r.  So go ahead 
2a2b2 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  and.  ** allocat
2a2b3 65 20 65 6e 6f 75 67 68 20 73 70 61 63 65 2c 20  e enough space, 
2a2b4 6a 75 73 74 20 69 6e 20 63 61 73 65 2e 0a 20 20  just in case..  
2a2b5 2a 2f 0a 20 20 70 54 61 62 4c 69 73 74 2d 3e 61  */.  pTabList->a
2a2b6 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 69 43  [0].iCursor = iC
2a2b7 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
2a2b8 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d  b++;.  for(pIdx=
2a2b9 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2a2ba 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2a2bb 4e 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72 73  Next){.    pPars
2a2bc 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a  e->nTab++;.  }..
2a2bd 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
2a2be 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74  the name-context
2a2bf 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e   */.  memset(&sN
2a2c0 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
2a2c1 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65  ));.  sNC.pParse
2a2c2 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 4e 43   = pParse;.  sNC
2a2c3 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62  .pSrcList = pTab
2a2c4 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f  List;..  /* Reso
2a2c5 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  lve the column n
2a2c6 61 6d 65 73 20 69 6e 20 61 6c 6c 20 74 68 65 20  ames in all the 
2a2c7 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  expressions of t
2a2c8 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 55  he.  ** of the U
2a2c9 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
2a2ca 20 20 41 6c 73 6f 20 66 69 6e 64 20 74 68 65 20    Also find the 
2a2cb 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20 2a  column index.  *
2a2cc 2a 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  * for each colum
2a2cd 6e 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  n to be updated 
2a2ce 69 6e 20 74 68 65 20 70 43 68 61 6e 67 65 73 20  in the pChanges 
2a2cf 61 72 72 61 79 2e 20 20 46 6f 72 20 65 61 63 68  array.  For each
2a2d0 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20  .  ** column to 
2a2d1 62 65 20 75 70 64 61 74 65 64 2c 20 6d 61 6b 65  be updated, make
2a2d2 20 73 75 72 65 20 77 65 20 68 61 76 65 20 61 75   sure we have au
2a2d3 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 63  thorization to c
2a2d4 68 61 6e 67 65 0a 20 20 2a 2a 20 74 68 61 74 20  hange.  ** that 
2a2d5 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f 0a 20 20 63  column..  */.  c
2a2d6 68 6e 67 52 6f 77 69 64 20 3d 20 30 3b 0a 20 20  hngRowid = 0;.  
2a2d7 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 61 6e  for(i=0; i<pChan
2a2d8 67 65 73 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ges->nExpr; i++)
2a2d9 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
2a2da 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2a2db 73 28 26 73 4e 43 2c 20 70 43 68 61 6e 67 65 73  s(&sNC, pChanges
2a2dc 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b  ->a[i].pExpr) ){
2a2dd 0a 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61  .      goto upda
2a2de 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  te_cleanup;.    
2a2df 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  }.    for(j=0; j
2a2e0 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  <pTab->nCol; j++
2a2e1 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
2a2e2 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62  ite3StrICmp(pTab
2a2e3 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c  ->aCol[j].zName,
2a2e4 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e   pChanges->a[i].
2a2e5 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
2a2e6 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 54 61 62       if( j==pTab
2a2e7 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
2a2e8 20 20 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d       chngRowid =
2a2e9 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 52   1;.          pR
2a2ea 6f 77 69 64 45 78 70 72 20 3d 20 70 43 68 61 6e  owidExpr = pChan
2a2eb 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  ges->a[i].pExpr;
2a2ec 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2a2ed 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20 69 3b     aXRef[j] = i;
2a2ee 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2a2ef 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2a2f0 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e    if( j>=pTab->n
2a2f1 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Col ){.      if(
2a2f2 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
2a2f3 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a  pChanges->a[i].z
2a2f4 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Name) ){.       
2a2f5 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a   chngRowid = 1;.
2a2f6 20 20 20 20 20 20 20 20 70 52 6f 77 69 64 45 78          pRowidEx
2a2f7 70 72 20 3d 20 70 43 68 61 6e 67 65 73 2d 3e 61  pr = pChanges->a
2a2f8 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
2a2f9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2a2fa 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2a2fb 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
2a2fc 20 63 6f 6c 75 6d 6e 3a 20 25 73 22 2c 20 70 43   column: %s", pC
2a2fd 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61  hanges->a[i].zNa
2a2fe 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  me);.        got
2a2ff 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
2a300 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2a301 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2a302 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
2a303 4e 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e  N.    {.      in
2a304 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d  t rc;.      rc =
2a305 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2a306 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
2a307 5f 55 50 44 41 54 45 2c 20 70 54 61 62 2d 3e 7a  _UPDATE, pTab->z
2a308 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
2a309 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a30a 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a   pTab->aCol[j].z
2a30b 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44  Name, db->aDb[iD
2a30c 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b].zName);.     
2a30d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2a30e 44 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20  DENY ){.        
2a30f 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
2a310 6e 75 70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  nup;.      }else
2a311 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2a312 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 20 20  IGNORE ){.      
2a313 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20 2d 31 3b    aXRef[j] = -1;
2a314 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
2a315 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20  endif.  }..  /* 
2a316 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  Allocate memory 
2a317 66 6f 72 20 74 68 65 20 61 72 72 61 79 20 61 52  for the array aR
2a318 65 67 49 64 78 5b 5d 2e 20 20 54 68 65 72 65 20  egIdx[].  There 
2a319 69 73 20 6f 6e 65 20 65 6e 74 72 79 20 69 6e 20  is one entry in 
2a31a 74 68 65 0a 20 20 2a 2a 20 61 72 72 61 79 20 66  the.  ** array f
2a31b 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 61 73  or each index as
2a31c 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 61  sociated with ta
2a31d 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74 65  ble being update
2a31e 64 2e 20 20 46 69 6c 6c 20 69 6e 0a 20 20 2a 2a  d.  Fill in.  **
2a31f 20 74 68 65 20 76 61 6c 75 65 20 77 69 74 68 20   the value with 
2a320 61 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  a register numbe
2a321 72 20 66 6f 72 20 69 6e 64 69 63 65 73 20 74 68  r for indices th
2a322 61 74 20 61 72 65 20 74 6f 20 62 65 20 75 73 65  at are to be use
2a323 64 0a 20 20 2a 2a 20 61 6e 64 20 77 69 74 68 20  d.  ** and with 
2a324 7a 65 72 6f 20 66 6f 72 20 75 6e 75 73 65 64 20  zero for unused 
2a325 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20  indices..  */.  
2a326 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78  for(nIdx=0, pIdx
2a327 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2a328 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2a329 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d  pNext, nIdx++){}
2a32a 0a 20 20 69 66 28 20 6e 49 64 78 3e 30 20 29 7b  .  if( nIdx>0 ){
2a32b 0a 20 20 20 20 61 52 65 67 49 64 78 20 3d 20 73  .    aRegIdx = s
2a32c 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
2a32d 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 6e 64  w(db, sizeof(Ind
2a32e 65 78 2a 29 20 2a 20 6e 49 64 78 20 29 3b 0a 20  ex*) * nIdx );. 
2a32f 20 20 20 69 66 28 20 61 52 65 67 49 64 78 3d 3d     if( aRegIdx==
2a330 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
2a331 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 66  cleanup;.  }.  f
2a332 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61  or(j=0, pIdx=pTa
2a333 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
2a334 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
2a335 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  t, j++){.    int
2a336 20 72 65 67 3b 0a 20 20 20 20 69 66 28 20 63 68   reg;.    if( ch
2a337 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  ngRowid ){.     
2a338 20 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d   reg = ++pParse-
2a339 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65  >nMem;.    }else
2a33a 7b 0a 20 20 20 20 20 20 72 65 67 20 3d 20 30 3b  {.      reg = 0;
2a33b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
2a33c 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  i<pIdx->nColumn;
2a33d 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   i++){.        i
2a33e 66 28 20 61 58 52 65 66 5b 70 49 64 78 2d 3e 61  f( aXRef[pIdx->a
2a33f 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29  iColumn[i]]>=0 )
2a340 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 20  {.          reg 
2a341 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2a342 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
2a343 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
2a344 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
2a345 52 65 67 49 64 78 5b 6a 5d 20 3d 20 72 65 67 3b  RegIdx[j] = reg;
2a346 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
2a347 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 72  ate a block of r
2a348 65 67 69 73 74 65 72 20 75 73 65 64 20 74 6f 20  egister used to 
2a349 73 74 6f 72 65 20 74 68 65 20 63 68 61 6e 67 65  store the change
2a34a 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20 73 65 6e   record.  ** sen
2a34b 74 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65  t to sqlite3Gene
2a34c 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68  rateConstraintCh
2a34d 65 63 6b 73 28 29 2e 20 20 54 68 65 72 65 20 61  ecks().  There a
2a34e 72 65 20 65 69 74 68 65 72 0a 20 20 2a 2a 20 6f  re either.  ** o
2a34f 6e 65 20 6f 72 20 74 77 6f 20 72 65 67 69 73 74  ne or two regist
2a350 65 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20  ers for holding 
2a351 74 68 65 20 72 6f 77 69 64 2e 20 20 4f 6e 65 20  the rowid.  One 
2a352 72 6f 77 69 64 20 72 65 67 69 73 74 65 72 0a 20  rowid register. 
2a353 20 2a 2a 20 69 73 20 75 73 65 64 20 69 66 20 63   ** is used if c
2a354 68 6e 67 52 6f 77 69 64 20 69 73 20 66 61 6c 73  hngRowid is fals
2a355 65 20 61 6e 64 20 74 77 6f 20 61 72 65 20 75 73  e and two are us
2a356 65 64 20 69 66 20 63 68 6e 67 52 6f 77 69 64 20  ed if chngRowid 
2a357 69 73 0a 20 20 2a 2a 20 74 72 75 65 2e 20 20 46  is.  ** true.  F
2a358 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 73 65 20 61  ollowing these a
2a359 72 65 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 72 65  re pTab->nCol re
2a35a 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63  gister holding c
2a35b 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 64 61 74 61 2e  olumn.  ** data.
2a35c 0a 20 20 2a 2f 0a 20 20 72 65 67 4f 6c 64 52 6f  .  */.  regOldRo
2a35d 77 69 64 20 3d 20 72 65 67 4e 65 77 52 6f 77 69  wid = regNewRowi
2a35e 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  d = pParse->nMem
2a35f 20 2b 20 31 3b 0a 20 20 70 50 61 72 73 65 2d 3e   + 1;.  pParse->
2a360 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43  nMem += pTab->nC
2a361 6f 6c 20 2b 20 31 3b 0a 20 20 69 66 28 20 63 68  ol + 1;.  if( ch
2a362 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 72  ngRowid ){.    r
2a363 65 67 4e 65 77 52 6f 77 69 64 2b 2b 3b 0a 20 20  egNewRowid++;.  
2a364 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b    pParse->nMem++
2a365 3b 0a 20 20 7d 0a 20 20 72 65 67 44 61 74 61 20  ;.  }.  regData 
2a366 3d 20 72 65 67 4e 65 77 52 6f 77 69 64 2b 31 3b  = regNewRowid+1;
2a367 0a 20 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67  . ..  /* Begin g
2a368 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a  enerating code..
2a369 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
2a36a 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2a36b 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
2a36c 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
2a36d 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73  nup;.  if( pPars
2a36e 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73  e->nested==0 ) s
2a36f 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
2a370 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c  hanges(v);.  sql
2a371 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
2a372 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
2a373 31 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65  1, iDb);..#ifnde
2a374 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2a375 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20  RTUALTABLE.  /* 
2a376 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  Virtual tables m
2a377 75 73 74 20 62 65 20 68 61 6e 64 6c 65 64 20 73  ust be handled s
2a378 65 70 61 72 61 74 65 6c 79 20 2a 2f 0a 20 20 69  eparately */.  i
2a379 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2a37a 62 29 20 29 7b 0a 20 20 20 20 75 70 64 61 74 65  b) ){.    update
2a37b 56 69 72 74 75 61 6c 54 61 62 6c 65 28 70 50 61  VirtualTable(pPa
2a37c 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
2a37d 54 61 62 2c 20 70 43 68 61 6e 67 65 73 2c 20 70  Tab, pChanges, p
2a37e 52 6f 77 69 64 45 78 70 72 2c 20 61 58 52 65 66  RowidExpr, aXRef
2a37f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2a380 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65 29           pWhere)
2a381 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 30  ;.    pWhere = 0
2a382 3b 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d  ;.    pTabList =
2a383 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64   0;.    goto upd
2a384 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ate_cleanup;.  }
2a385 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 53 74  .#endif..  /* St
2a386 61 72 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e  art the view con
2a387 74 65 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20  text.  */.  if( 
2a388 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71  isView ){.    sq
2a389 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
2a38a 50 75 73 68 28 70 50 61 72 73 65 2c 20 26 73 43  Push(pParse, &sC
2a38b 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e  ontext, pTab->zN
2a38c 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ame);.  }..  /* 
2a38d 47 65 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64  Generate the cod
2a38e 65 20 66 6f 72 20 74 72 69 67 67 65 72 73 2e 0a  e for triggers..
2a38f 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 72 69 67    */.  if( pTrig
2a390 67 65 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ger ){.    int i
2a391 47 6f 74 6f 3b 0a 0a 20 20 20 20 2f 2a 20 43 72  Goto;..    /* Cr
2a392 65 61 74 65 20 70 73 65 75 64 6f 2d 74 61 62 6c  eate pseudo-tabl
2a393 65 73 20 66 6f 72 20 4e 45 57 20 61 6e 64 20 4f  es for NEW and O
2a394 4c 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  LD.    */.    sq
2a395 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2a396 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f  v, OP_OpenPseudo
2a397 2c 20 6f 6c 64 49 64 78 2c 20 30 2c 20 70 54 61  , oldIdx, 0, pTa
2a398 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 71  b->nCol);.    sq
2a399 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2a39a 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f  v, OP_OpenPseudo
2a39b 2c 20 6e 65 77 49 64 78 2c 20 30 2c 20 70 54 61  , newIdx, 0, pTa
2a39c 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a 20 20 20 20 69  b->nCol);..    i
2a39d 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56 64  Goto = sqlite3Vd
2a39e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2a39f 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  oto, 0, 0);.    
2a3a0 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
2a3a1 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2a3a2 20 20 20 20 69 42 65 67 69 6e 42 65 66 6f 72 65      iBeginBefore
2a3a3 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65  Trigger = sqlite
2a3a4 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2a3a5 28 76 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  (v);.    if( sql
2a3a6 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
2a3a7 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67  er(pParse, pTrig
2a3a8 67 65 72 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20  ger, TK_UPDATE, 
2a3a9 70 43 68 61 6e 67 65 73 2c 20 0a 20 20 20 20 20  pChanges, .     
2a3aa 20 20 20 20 20 54 52 49 47 47 45 52 5f 42 45 46       TRIGGER_BEF
2a3ab 4f 52 45 2c 20 70 54 61 62 2c 20 6e 65 77 49 64  ORE, pTab, newId
2a3ac 78 2c 20 6f 6c 64 49 64 78 2c 20 6f 6e 45 72 72  x, oldIdx, onErr
2a3ad 6f 72 2c 20 61 64 64 72 2c 20 0a 20 20 20 20 20  or, addr, .     
2a3ae 20 20 20 20 20 26 6f 6c 64 5f 63 6f 6c 5f 6d 61       &old_col_ma
2a3af 73 6b 2c 20 26 6e 65 77 5f 63 6f 6c 5f 6d 61 73  sk, &new_col_mas
2a3b0 6b 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  k) ){.      goto
2a3b1 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2a3b2 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 42  .    }.    iEndB
2a3b3 65 66 6f 72 65 54 72 69 67 67 65 72 20 3d 20 73  eforeTrigger = s
2a3b4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2a3b5 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
2a3b6 30 29 3b 0a 20 20 20 20 69 42 65 67 69 6e 41 66  0);.    iBeginAf
2a3b7 74 65 72 54 72 69 67 67 65 72 20 3d 20 73 71 6c  terTrigger = sql
2a3b8 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
2a3b9 64 64 72 28 76 29 3b 0a 20 20 20 20 69 66 28 20  ddr(v);.    if( 
2a3ba 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
2a3bb 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 54  igger(pParse, pT
2a3bc 72 69 67 67 65 72 2c 20 54 4b 5f 55 50 44 41 54  rigger, TK_UPDAT
2a3bd 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 0a 20 20  E, pChanges, .  
2a3be 20 20 20 20 20 20 20 20 54 52 49 47 47 45 52 5f          TRIGGER_
2a3bf 41 46 54 45 52 2c 20 70 54 61 62 2c 20 6e 65 77  AFTER, pTab, new
2a3c0 49 64 78 2c 20 6f 6c 64 49 64 78 2c 20 6f 6e 45  Idx, oldIdx, onE
2a3c1 72 72 6f 72 2c 20 61 64 64 72 2c 20 0a 20 20 20  rror, addr, .   
2a3c2 20 20 20 20 20 20 20 26 6f 6c 64 5f 63 6f 6c 5f         &old_col_
2a3c3 6d 61 73 6b 2c 20 26 6e 65 77 5f 63 6f 6c 5f 6d  mask, &new_col_m
2a3c4 61 73 6b 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  ask) ){.      go
2a3c5 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
2a3c6 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e  p;.    }.    iEn
2a3c7 64 41 66 74 65 72 54 72 69 67 67 65 72 20 3d 20  dAfterTrigger = 
2a3c8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a3c9 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2a3ca 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
2a3cb 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2a3cc 69 47 6f 74 6f 29 3b 0a 20 20 7d 0a 0a 20 20 2f  iGoto);.  }..  /
2a3cd 2a 20 49 66 20 77 65 20 61 72 65 20 74 72 79 69  * If we are tryi
2a3ce 6e 67 20 74 6f 20 75 70 64 61 74 65 20 61 20 76  ng to update a v
2a3cf 69 65 77 2c 20 72 65 61 6c 69 7a 65 20 74 68 61  iew, realize tha
2a3d0 74 20 76 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a  t view into.  **
2a3d1 20 61 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62   a ephemeral tab
2a3d2 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65  le..  */.#if !de
2a3d3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2a3d4 54 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69  T_VIEW) && !defi
2a3d5 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2a3d6 54 52 49 47 47 45 52 29 0a 20 20 69 66 28 20 69  TRIGGER).  if( i
2a3d7 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c  sView ){.    sql
2a3d8 69 74 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56  ite3MaterializeV
2a3d9 69 65 77 28 70 50 61 72 73 65 2c 20 70 54 61 62  iew(pParse, pTab
2a3da 2c 20 70 57 68 65 72 65 2c 20 69 43 75 72 29 3b  , pWhere, iCur);
2a3db 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
2a3dc 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f  * Resolve the co
2a3dd 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c  lumn names in al
2a3de 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  l the expression
2a3df 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 57 48  s in the.  ** WH
2a3e0 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f  ERE clause..  */
2a3e1 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65  .  if( sqlite3Re
2a3e2 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
2a3e3 73 4e 43 2c 20 70 57 68 65 72 65 29 20 29 7b 0a  sNC, pWhere) ){.
2a3e4 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
2a3e5 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
2a3e6 2f 2a 20 42 65 67 69 6e 20 74 68 65 20 64 61 74  /* Begin the dat
2a3e7 61 62 61 73 65 20 73 63 61 6e 0a 20 20 2a 2f 0a  abase scan.  */.
2a3e8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a3e9 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
2a3ea 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b  0, regOldRowid);
2a3eb 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69  .  pWInfo = sqli
2a3ec 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50  te3WhereBegin(pP
2a3ed 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
2a3ee 70 57 68 65 72 65 2c 30 2c 20 57 48 45 52 45 5f  pWhere,0, WHERE_
2a3ef 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 29  ONEPASS_DESIRED)
2a3f0 3b 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d  ;.  if( pWInfo==
2a3f1 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
2a3f2 63 6c 65 61 6e 75 70 3b 0a 20 20 6f 6b 4f 6e 65  cleanup;.  okOne
2a3f3 50 61 73 73 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f  Pass = pWInfo->o
2a3f4 6b 4f 6e 65 50 61 73 73 3b 0a 0a 20 20 2f 2a 20  kOnePass;..  /* 
2a3f5 52 65 6d 65 6d 62 65 72 20 74 68 65 20 72 6f 77  Remember the row
2a3f6 69 64 20 6f 66 20 65 76 65 72 79 20 69 74 65 6d  id of every item
2a3f7 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a   to be updated..
2a3f8 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
2a3f9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2a3fa 6f 77 69 64 2c 20 69 43 75 72 2c 20 72 65 67 4f  owid, iCur, regO
2a3fb 6c 64 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20  ldRowid);.  if( 
2a3fc 21 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20  !okOnePass ){.  
2a3fd 20 20 72 65 67 52 6f 77 53 65 74 20 3d 20 2b 2b    regRowSet = ++
2a3fe 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2a3ff 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a400 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74  Op2(v, OP_RowSet
2a401 41 64 64 2c 20 72 65 67 52 6f 77 53 65 74 2c 20  Add, regRowSet, 
2a402 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20  regOldRowid);.  
2a403 7d 0a 0a 20 20 2f 2a 20 45 6e 64 20 74 68 65 20  }..  /* End the 
2a404 64 61 74 61 62 61 73 65 20 73 63 61 6e 20 6c 6f  database scan lo
2a405 6f 70 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  op..  */.  sqlit
2a406 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66  e3WhereEnd(pWInf
2a407 6f 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61  o);..  /* Initia
2a408 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f  lize the count o
2a409 66 20 75 70 64 61 74 65 64 20 72 6f 77 73 0a 20  f updated rows. 
2a40a 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c   */.  if( db->fl
2a40b 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75  ags & SQLITE_Cou
2a40c 6e 74 52 6f 77 73 20 26 26 20 21 70 50 61 72 73  ntRows && !pPars
2a40d 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a  e->trigStack ){.
2a40e 20 20 20 20 72 65 67 52 6f 77 43 6f 75 6e 74 20      regRowCount 
2a40f 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2a410 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2a411 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2a412 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52 6f 77  teger, 0, regRow
2a413 43 6f 75 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 69  Count);.  }..  i
2a414 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  f( !isView ){.  
2a415 20 20 2f 2a 20 0a 20 20 20 20 2a 2a 20 4f 70 65    /* .    ** Ope
2a416 6e 20 65 76 65 72 79 20 69 6e 64 65 78 20 74 68  n every index th
2a417 61 74 20 6e 65 65 64 73 20 75 70 64 61 74 69 6e  at needs updatin
2a418 67 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69 66  g.  Note that if
2a419 20 61 6e 79 0a 20 20 20 20 2a 2a 20 69 6e 64 65   any.    ** inde
2a41a 78 20 63 6f 75 6c 64 20 70 6f 74 65 6e 74 69 61  x could potentia
2a41b 6c 6c 79 20 69 6e 76 6f 6b 65 20 61 20 52 45 50  lly invoke a REP
2a41c 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74 20 72 65  LACE conflict re
2a41d 73 6f 6c 75 74 69 6f 6e 20 0a 20 20 20 20 2a 2a  solution .    **
2a41e 20 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 77 65   action, then we
2a41f 20 6e 65 65 64 20 74 6f 20 6f 70 65 6e 20 61 6c   need to open al
2a420 6c 20 69 6e 64 69 63 65 73 20 62 65 63 61 75 73  l indices becaus
2a421 65 20 77 65 20 6d 69 67 68 74 20 6e 65 65 64 0a  e we might need.
2a422 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 6c      ** to be del
2a423 65 74 69 6e 67 20 73 6f 6d 65 20 72 65 63 6f 72  eting some recor
2a424 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ds..    */.    i
2a425 66 28 20 21 6f 6b 4f 6e 65 50 61 73 73 20 29 20  f( !okOnePass ) 
2a426 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
2a427 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69  (pParse, iCur, i
2a428 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65  Db, pTab, OP_Ope
2a429 6e 57 72 69 74 65 29 3b 20 0a 20 20 20 20 69 66  nWrite); .    if
2a42a 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  ( onError==OE_Re
2a42b 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20 6f  place ){.      o
2a42c 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20 20  penAll = 1;.    
2a42d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6f 70 65  }else{.      ope
2a42e 6e 41 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  nAll = 0;.      
2a42f 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
2a430 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
2a431 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
2a432 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
2a433 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  ->onError==OE_Re
2a434 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20  place ){.       
2a435 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a     openAll = 1;.
2a436 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
2a437 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2a438 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72   }.    }.    for
2a439 28 69 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d  (i=0, pIdx=pTab-
2a43a 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
2a43b 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c  Idx=pIdx->pNext,
2a43c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
2a43d 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 52 65 67   openAll || aReg
2a43e 49 64 78 5b 69 5d 3e 30 20 29 7b 0a 20 20 20 20  Idx[i]>0 ){.    
2a43f 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
2a440 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
2a441 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
2a442 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 73  pIdx);.        s
2a443 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2a444 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  (v, OP_OpenWrite
2a445 2c 20 69 43 75 72 2b 69 2b 31 2c 20 70 49 64 78  , iCur+i+1, pIdx
2a446 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20  ->tnum, iDb,.   
2a447 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a448 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c      (char*)pKey,
2a449 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
2a44a 4f 46 46 29 3b 0a 20 20 20 20 20 20 20 20 61 73  OFF);.        as
2a44b 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 54  sert( pParse->nT
2a44c 61 62 3e 69 43 75 72 2b 69 2b 31 20 29 3b 0a 20  ab>iCur+i+1 );. 
2a44d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2a44e 0a 20 20 0a 20 20 2f 2a 20 4a 75 6d 70 20 62 61  .  .  /* Jump ba
2a44f 63 6b 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74  ck to this point
2a450 20 69 66 20 61 20 74 72 69 67 67 65 72 20 65 6e   if a trigger en
2a451 63 6f 75 6e 74 65 72 73 20 61 6e 20 49 47 4e 4f  counters an IGNO
2a452 52 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 2a  RE constraint. *
2a453 2f 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  /.  if( pTrigger
2a454 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2a455 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2a456 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 0a 20  v, addr);.  }.. 
2a457 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 75   /* Top of the u
2a458 70 64 61 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  pdate loop */.  
2a459 69 66 28 20 6f 6b 4f 6e 65 50 61 73 73 20 29 7b  if( okOnePass ){
2a45a 0a 20 20 20 20 69 6e 74 20 61 31 20 3d 20 73 71  .    int a1 = sq
2a45b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2a45c 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72  v, OP_NotNull, r
2a45d 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20  egOldRowid);.   
2a45e 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
2a45f 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
2a460 47 6f 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74  Goto);.    sqlit
2a461 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2a462 2c 20 61 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  , a1);.  }else{.
2a463 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2a464 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2a465 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20 72  OP_RowSetRead, r
2a466 65 67 52 6f 77 53 65 74 2c 20 30 2c 20 72 65 67  egRowSet, 0, reg
2a467 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a  OldRowid);.  }..
2a468 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29    if( pTrigger )
2a469 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77  {.    int regRow
2a46a 69 64 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 52  id;.    int regR
2a46b 6f 77 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 43  ow;.    int regC
2a46c 6f 6c 73 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b  ols;..    /* Mak
2a46d 65 20 63 75 72 73 6f 72 20 69 43 75 72 20 70 6f  e cursor iCur po
2a46e 69 6e 74 20 74 6f 20 74 68 65 20 72 65 63 6f 72  int to the recor
2a46f 64 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20  d that is being 
2a470 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  updated..    */.
2a471 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a472 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
2a473 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64  xists, iCur, add
2a474 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b  r, regOldRowid);
2a475 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74  ..    /* Generat
2a476 65 20 74 68 65 20 4f 4c 44 20 74 61 62 6c 65 0a  e the OLD table.
2a477 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 52 6f      */.    regRo
2a478 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74  wid = sqlite3Get
2a479 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2a47a 0a 20 20 20 20 72 65 67 52 6f 77 20 3d 20 73 71  .    regRow = sq
2a47b 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2a47c 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c  pParse);.    sql
2a47d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2a47e 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72  , OP_Rowid, iCur
2a47f 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
2a480 20 69 66 28 20 21 6f 6c 64 5f 63 6f 6c 5f 6d 61   if( !old_col_ma
2a481 73 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  sk ){.      sqli
2a482 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a483 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
2a484 52 6f 77 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Row);.    }else{
2a485 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2a486 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2a487 6f 77 44 61 74 61 2c 20 69 43 75 72 2c 20 72 65  owData, iCur, re
2a488 67 52 6f 77 29 3b 0a 20 20 20 20 7d 0a 20 20 20  gRow);.    }.   
2a489 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a48a 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
2a48b 20 6f 6c 64 49 64 78 2c 20 72 65 67 52 6f 77 2c   oldIdx, regRow,
2a48c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20 20   regRowid);..   
2a48d 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65   /* Generate the
2a48e 20 4e 45 57 20 74 61 62 6c 65 0a 20 20 20 20 2a   NEW table.    *
2a48f 2f 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f  /.    if( chngRo
2a490 77 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  wid ){.      sql
2a491 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43  ite3ExprCodeAndC
2a492 61 63 68 65 28 70 50 61 72 73 65 2c 20 70 52 6f  ache(pParse, pRo
2a493 77 69 64 45 78 70 72 2c 20 72 65 67 52 6f 77 69  widExpr, regRowi
2a494 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  d);.      sqlite
2a495 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2a496 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67  P_MustBeInt, reg
2a497 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73  Rowid);.    }els
2a498 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
2a499 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2a49a 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 72 65  _Rowid, iCur, re
2a49b 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20  gRowid);.    }. 
2a49c 20 20 20 72 65 67 43 6f 6c 73 20 3d 20 73 71 6c     regCols = sql
2a49d 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
2a49e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e  (pParse, pTab->n
2a49f 43 6f 6c 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  Col);.    for(i=
2a4a0 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
2a4a1 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
2a4a2 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20   i==pTab->iPKey 
2a4a3 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2a4a4 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2a4a5 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 43  OP_Null, 0, regC
2a4a6 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 20 20  ols+i);.        
2a4a7 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2a4a8 7d 0a 20 20 20 20 20 20 6a 20 3d 20 61 58 52 65  }.      j = aXRe
2a4a9 66 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  f[i];.      if( 
2a4aa 28 69 3c 33 32 20 26 26 20 28 6e 65 77 5f 63 6f  (i<32 && (new_co
2a4ab 6c 5f 6d 61 73 6b 26 28 28 75 33 32 29 31 3c 3c  l_mask&((u32)1<<
2a4ac 69 29 29 21 3d 30 29 20 7c 7c 20 6e 65 77 5f 63  i))!=0) || new_c
2a4ad 6f 6c 5f 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66  ol_mask==0xfffff
2a4ae 66 66 66 20 29 7b 0a 20 20 20 20 20 20 20 20 69  fff ){.        i
2a4af 66 28 20 6a 3c 30 20 29 7b 0a 20 20 20 20 20 20  f( j<0 ){.      
2a4b0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a4b1 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
2a4b2 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67  mn, iCur, i, reg
2a4b3 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 20  Cols+i);.       
2a4b4 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e     sqlite3Column
2a4b5 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c  Default(v, pTab,
2a4b6 20 69 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c   i);.        }el
2a4b7 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  se{.          sq
2a4b8 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64  lite3ExprCodeAnd
2a4b9 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 70 43  Cache(pParse, pC
2a4ba 68 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78  hanges->a[j].pEx
2a4bb 70 72 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a  pr, regCols+i);.
2a4bc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2a4bd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
2a4be 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2a4bf 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2a4c0 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 20  regCols+i);.    
2a4c1 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
2a4c2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2a4c3 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
2a4c4 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d  , regCols, pTab-
2a4c5 3e 6e 43 6f 6c 2c 20 72 65 67 52 6f 77 29 3b 0a  >nCol, regRow);.
2a4c6 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20      if( !isView 
2a4c7 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2a4c8 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72  TableAffinityStr
2a4c9 28 76 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  (v, pTab);.     
2a4ca 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2a4cb 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
2a4cc 70 50 61 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c  pParse, regCols,
2a4cd 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
2a4ce 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52    }.    sqlite3R
2a4cf 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
2a4d0 70 50 61 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c  pParse, regCols,
2a4d1 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
2a4d2 20 20 2f 2a 20 69 66 28 20 70 50 61 72 73 65 2d    /* if( pParse-
2a4d3 3e 6e 45 72 72 20 29 20 67 6f 74 6f 20 75 70 64  >nErr ) goto upd
2a4d4 61 74 65 5f 63 6c 65 61 6e 75 70 3b 20 2a 2f 0a  ate_cleanup; */.
2a4d5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a4d6 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
2a4d7 72 74 2c 20 6e 65 77 49 64 78 2c 20 72 65 67 52  rt, newIdx, regR
2a4d8 6f 77 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  ow, regRowid);. 
2a4d9 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2a4da 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2a4db 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
2a4dc 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2a4dd 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
2a4de 67 52 6f 77 29 3b 0a 0a 20 20 20 20 73 71 6c 69  gRow);..    sqli
2a4df 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a4e0 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 65   OP_Goto, 0, iBe
2a4e1 67 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72  ginBeforeTrigger
2a4e2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2a4e3 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 45  beJumpHere(v, iE
2a4e4 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 29  ndBeforeTrigger)
2a4e5 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 69 73  ;.  }..  if( !is
2a4e6 56 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a 20 4c  View ){.    /* L
2a4e7 6f 6f 70 20 6f 76 65 72 20 65 76 65 72 79 20 72  oop over every r
2a4e8 65 63 6f 72 64 20 74 68 61 74 20 6e 65 65 64 73  ecord that needs
2a4e9 20 75 70 64 61 74 69 6e 67 2e 20 20 57 65 20 68   updating.  We h
2a4ea 61 76 65 20 74 6f 20 6c 6f 61 64 0a 20 20 20 20  ave to load.    
2a4eb 2a 2a 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20  ** the old data 
2a4ec 66 6f 72 20 65 61 63 68 20 72 65 63 6f 72 64 20  for each record 
2a4ed 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 62 65  to be updated be
2a4ee 63 61 75 73 65 20 73 6f 6d 65 20 63 6f 6c 75 6d  cause some colum
2a4ef 6e 73 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20  ns.    ** might 
2a4f0 6e 6f 74 20 63 68 61 6e 67 65 20 61 6e 64 20 77  not change and w
2a4f1 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63  e will need to c
2a4f2 6f 70 79 20 74 68 65 20 6f 6c 64 20 76 61 6c 75  opy the old valu
2a4f3 65 2e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20  e..    ** Also, 
2a4f4 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20  the old data is 
2a4f5 6e 65 65 64 65 64 20 74 6f 20 64 65 6c 65 74 65  needed to delete
2a4f6 20 74 68 65 20 6f 6c 64 20 69 6e 64 65 78 20 65   the old index e
2a4f7 6e 74 72 69 65 73 2e 0a 20 20 20 20 2a 2a 20 53  ntries..    ** S
2a4f8 6f 20 6d 61 6b 65 20 74 68 65 20 63 75 72 73 6f  o make the curso
2a4f9 72 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6f  r point at the o
2a4fa 6c 64 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a  ld record..    *
2a4fb 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
2a4fc 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
2a4fd 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61  tExists, iCur, a
2a4fe 64 64 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  ddr, regOldRowid
2a4ff 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
2a500 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
2a501 77 69 6c 6c 20 63 68 61 6e 67 65 2c 20 70 75 73  will change, pus
2a502 68 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  h the record num
2a503 62 65 72 20 61 73 20 69 74 0a 20 20 20 20 2a 2a  ber as it.    **
2a504 20 77 69 6c 6c 20 62 65 20 61 66 74 65 72 20 74   will be after t
2a505 68 65 20 75 70 64 61 74 65 2e 20 28 54 68 65 20  he update. (The 
2a506 6f 6c 64 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  old record numbe
2a507 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 20  r is currently. 
2a508 20 20 20 2a 2a 20 6f 6e 20 74 6f 70 20 6f 66 20     ** on top of 
2a509 74 68 65 20 73 74 61 63 6b 2e 29 0a 20 20 20 20  the stack.).    
2a50a 2a 2f 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52  */.    if( chngR
2a50b 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71  owid ){.      sq
2a50c 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2a50d 61 72 73 65 2c 20 70 52 6f 77 69 64 45 78 70 72  arse, pRowidExpr
2a50e 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a  , regNewRowid);.
2a50f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a510 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75  eAddOp1(v, OP_Mu
2a511 73 74 42 65 49 6e 74 2c 20 72 65 67 4e 65 77 52  stBeInt, regNewR
2a512 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  owid);.    }..  
2a513 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 6e 65 77    /* Compute new
2a514 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 72   data for this r
2a515 65 63 6f 72 64 2e 20 20 0a 20 20 20 20 2a 2f 0a  ecord.  .    */.
2a516 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2a517 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
2a518 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 70 54  .      if( i==pT
2a519 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
2a51a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2a51b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2a51c 6c 2c 20 30 2c 20 72 65 67 44 61 74 61 2b 69 29  l, 0, regData+i)
2a51d 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e  ;.        contin
2a51e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ue;.      }.    
2a51f 20 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b 0a    j = aXRef[i];.
2a520 20 20 20 20 20 20 69 66 28 20 6a 3c 30 20 29 7b        if( j<0 ){
2a521 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2a522 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2a523 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69  _Column, iCur, i
2a524 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20  , regData+i);.  
2a525 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c        sqlite3Col
2a526 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54  umnDefault(v, pT
2a527 61 62 2c 20 69 29 3b 0a 20 20 20 20 20 20 7d 65  ab, i);.      }e
2a528 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
2a529 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
2a52a 72 73 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61  rse, pChanges->a
2a52b 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 44 61  [j].pExpr, regDa
2a52c 74 61 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ta+i);.      }. 
2a52d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20     }..    /* Do 
2a52e 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b  constraint check
2a52f 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  s.    */.    sql
2a530 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73  ite3GenerateCons
2a531 74 72 61 69 6e 74 43 68 65 63 6b 73 28 70 50 61  traintChecks(pPa
2a532 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c  rse, pTab, iCur,
2a533 20 72 65 67 4e 65 77 52 6f 77 69 64 2c 0a 20 20   regNewRowid,.  
2a534 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a535 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a536 20 20 61 52 65 67 49 64 78 2c 20 63 68 6e 67 52    aRegIdx, chngR
2a537 6f 77 69 64 2c 20 31 2c 0a 20 20 20 20 20 20 20  owid, 1,.       
2a538 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a539 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e 45               onE
2a53a 72 72 6f 72 2c 20 61 64 64 72 2c 20 30 29 3b 0a  rror, addr, 0);.
2a53b 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74  .    /* Delete t
2a53c 68 65 20 6f 6c 64 20 69 6e 64 69 63 65 73 20 66  he old indices f
2a53d 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
2a53e 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  ecord..    */.  
2a53f 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
2a540 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
2a541 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20  otExists, iCur, 
2a542 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b  0, regOldRowid);
2a543 0a 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65  .    sqlite3Gene
2a544 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
2a545 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  te(pParse, pTab,
2a546 20 69 43 75 72 2c 20 61 52 65 67 49 64 78 29 3b   iCur, aRegIdx);
2a547 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63 68 61 6e  ..    /* If chan
2a548 67 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20  ging the record 
2a549 6e 75 6d 62 65 72 2c 20 64 65 6c 65 74 65 20 74  number, delete t
2a54a 68 65 20 6f 6c 64 20 72 65 63 6f 72 64 2e 0a 20  he old record.. 
2a54b 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 68     */.    if( ch
2a54c 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  ngRowid ){.     
2a54d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a54e 70 32 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c  p2(v, OP_Delete,
2a54f 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 7d   iCur, 0);.    }
2a550 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a551 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
2a552 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20  ..    /* Create 
2a553 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e  the new index en
2a554 74 72 69 65 73 20 61 6e 64 20 74 68 65 20 6e 65  tries and the ne
2a555 77 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f  w record..    */
2a556 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70  .    sqlite3Comp
2a557 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 70 50  leteInsertion(pP
2a558 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72  arse, pTab, iCur
2a559 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 2c 20 0a  , regNewRowid, .
2a55a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a55b 20 20 20 20 20 20 20 20 20 20 20 20 20 61 52 65               aRe
2a55c 67 49 64 78 2c 20 31 2c 20 2d 31 2c 20 30 2c 20  gIdx, 1, -1, 0, 
2a55d 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e  0);.  }..  /* In
2a55e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f 77 20  crement the row 
2a55f 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2f 0a 20 20  counter .  */.  
2a560 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20  if( db->flags & 
2a561 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
2a562 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69   && !pParse->tri
2a563 67 53 74 61 63 6b 29 7b 0a 20 20 20 20 73 71 6c  gStack){.    sql
2a564 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2a565 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67  , OP_AddImm, reg
2a566 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20  RowCount, 1);.  
2a567 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  }..  /* If there
2a568 20 61 72 65 20 74 72 69 67 67 65 72 73 2c 20 63   are triggers, c
2a569 6c 6f 73 65 20 61 6c 6c 20 74 68 65 20 63 75 72  lose all the cur
2a56a 73 6f 72 73 20 61 66 74 65 72 20 65 61 63 68 20  sors after each 
2a56b 69 74 65 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 74  iteration.  ** t
2a56c 68 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 2e  hrough the loop.
2a56d 20 20 54 68 65 20 66 69 72 65 20 74 68 65 20 61    The fire the a
2a56e 66 74 65 72 20 74 72 69 67 67 65 72 73 2e 0a 20  fter triggers.. 
2a56f 20 2a 2f 0a 20 20 69 66 28 20 70 54 72 69 67 67   */.  if( pTrigg
2a570 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  er ){.    sqlite
2a571 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a572 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 65 67 69  P_Goto, 0, iBegi
2a573 6e 41 66 74 65 72 54 72 69 67 67 65 72 29 3b 0a  nAfterTrigger);.
2a574 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
2a575 75 6d 70 48 65 72 65 28 76 2c 20 69 45 6e 64 41  umpHere(v, iEndA
2a576 66 74 65 72 54 72 69 67 67 65 72 29 3b 0a 20 20  fterTrigger);.  
2a577 7d 0a 0a 20 20 2f 2a 20 52 65 70 65 61 74 20 74  }..  /* Repeat t
2a578 68 65 20 61 62 6f 76 65 20 77 69 74 68 20 74 68  he above with th
2a579 65 20 6e 65 78 74 20 72 65 63 6f 72 64 20 74 6f  e next record to
2a57a 20 62 65 20 75 70 64 61 74 65 64 2c 20 75 6e 74   be updated, unt
2a57b 69 6c 0a 20 20 2a 2a 20 61 6c 6c 20 72 65 63 6f  il.  ** all reco
2a57c 72 64 20 73 65 6c 65 63 74 65 64 20 62 79 20 74  rd selected by t
2a57d 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2a57e 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 65  have been update
2a57f 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  d..  */.  sqlite
2a580 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a581 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29  P_Goto, 0, addr)
2a582 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  ;.  sqlite3VdbeJ
2a583 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
2a584 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c  ;..  /* Close al
2a585 6c 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 66 6f  l tables */.  fo
2a586 72 28 69 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  r(i=0, pIdx=pTab
2a587 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
2a588 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2a589 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , i++){.    if( 
2a58a 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49  openAll || aRegI
2a58b 64 78 5b 69 5d 3e 30 20 29 7b 0a 20 20 20 20 20  dx[i]>0 ){.     
2a58c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a58d 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
2a58e 69 43 75 72 2b 69 2b 31 2c 20 30 29 3b 0a 20 20  iCur+i+1, 0);.  
2a58f 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
2a590 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a591 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30  P_Close, iCur, 0
2a592 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  );.  if( pTrigge
2a593 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
2a594 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2a595 5f 43 6c 6f 73 65 2c 20 6e 65 77 49 64 78 2c 20  _Close, newIdx, 
2a596 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
2a597 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a598 43 6c 6f 73 65 2c 20 6f 6c 64 49 64 78 2c 20 30  Close, oldIdx, 0
2a599 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  );.  }..  /*.  *
2a59a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
2a59b 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74  ber of rows that
2a59c 20 77 65 72 65 20 63 68 61 6e 67 65 64 2e 20 49   were changed. I
2a59d 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  f this routine i
2a59e 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 69  s .  ** generati
2a59f 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73 65 20  ng code because 
2a5a0 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  of a call to sql
2a5a1 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
2a5a2 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69  ), do not.  ** i
2a5a3 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61  nvoke the callba
2a5a4 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a  ck function..  *
2a5a5 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67  /.  if( db->flag
2a5a6 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74  s & SQLITE_Count
2a5a7 52 6f 77 73 20 26 26 20 21 70 50 61 72 73 65 2d  Rows && !pParse-
2a5a8 3e 74 72 69 67 53 74 61 63 6b 20 26 26 20 70 50  >trigStack && pP
2a5a9 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20  arse->nested==0 
2a5aa 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2a5ab 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2a5ac 65 73 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f 77  esultRow, regRow
2a5ad 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73  Count, 1);.    s
2a5ae 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
2a5af 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
2a5b0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2a5b1 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
2a5b2 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20  AME_NAME, "rows 
2a5b3 75 70 64 61 74 65 64 22 2c 20 53 51 4c 49 54 45  updated", SQLITE
2a5b4 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 75  _STATIC);.  }..u
2a5b5 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3a 0a 20  pdate_cleanup:. 
2a5b6 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
2a5b7 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78 74  extPop(&sContext
2a5b8 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
2a5b9 65 65 28 64 62 2c 20 61 52 65 67 49 64 78 29 3b  ee(db, aRegIdx);
2a5ba 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2a5bb 28 64 62 2c 20 61 58 52 65 66 29 3b 0a 20 20 73  (db, aXRef);.  s
2a5bc 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
2a5bd 65 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73 74  ete(db, pTabList
2a5be 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2a5bf 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2a5c0 43 68 61 6e 67 65 73 29 3b 0a 20 20 73 71 6c 69  Changes);.  sqli
2a5c1 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2a5c2 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74  , pWhere);.  ret
2a5c3 75 72 6e 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  urn;.}..#ifndef 
2a5c4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2a5c5 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 47  UALTABLE./*.** G
2a5c6 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
2a5c7 20 61 6e 20 55 50 44 41 54 45 20 6f 66 20 61 20   an UPDATE of a 
2a5c8 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
2a5c9 2a 0a 2a 2a 20 54 68 65 20 73 74 72 61 74 65 67  *.** The strateg
2a5ca 79 20 69 73 20 74 68 61 74 20 77 65 20 63 72 65  y is that we cre
2a5cb 61 74 65 20 61 6e 20 65 70 68 65 6d 65 72 69 61  ate an ephemeria
2a5cc 6c 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e  l table that con
2a5cd 74 61 69 6e 73 0a 2a 2a 20 66 6f 72 20 65 61 63  tains.** for eac
2a5ce 68 20 72 6f 77 20 74 6f 20 62 65 20 63 68 61 6e  h row to be chan
2a5cf 67 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 41 29  ged:.**.**   (A)
2a5d0 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 72    The original r
2a5d1 6f 77 69 64 20 6f 66 20 74 68 61 74 20 72 6f 77  owid of that row
2a5d2 2e 0a 2a 2a 20 20 20 28 42 29 20 20 54 68 65 20  ..**   (B)  The 
2a5d3 72 65 76 69 73 65 64 20 72 6f 77 69 64 20 66 6f  revised rowid fo
2a5d4 72 20 74 68 65 20 72 6f 77 2e 20 28 6e 6f 74 65  r the row. (note
2a5d5 31 29 0a 2a 2a 20 20 20 28 43 29 20 20 54 68 65  1).**   (C)  The
2a5d6 20 63 6f 6e 74 65 6e 74 20 6f 66 20 65 76 65 72   content of ever
2a5d7 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  y column in the 
2a5d8 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20  row..**.** Then 
2a5d9 77 65 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 69  we loop over thi
2a5da 73 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  s ephemeral tabl
2a5db 65 20 61 6e 64 20 66 6f 72 20 65 61 63 68 20 72  e and for each r
2a5dc 6f 77 20 69 6e 0a 2a 2a 20 74 68 65 20 65 70 68  ow in.** the eph
2a5dd 65 72 6d 65 72 61 6c 20 74 61 62 6c 65 20 63 61  ermeral table ca
2a5de 6c 6c 20 56 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a  ll VUpdate..**.*
2a5df 2a 20 57 68 65 6e 20 66 69 6e 69 73 68 65 64 2c  * When finished,
2a5e0 20 64 72 6f 70 20 74 68 65 20 65 70 68 65 6d 65   drop the epheme
2a5e1 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ral table..**.**
2a5e2 20 28 6e 6f 74 65 31 29 20 41 63 74 75 61 6c 6c   (note1) Actuall
2a5e3 79 2c 20 69 66 20 77 65 20 6b 6e 6f 77 20 69 6e  y, if we know in
2a5e4 20 61 64 76 61 6e 63 65 20 74 68 61 74 20 28 41   advance that (A
2a5e5 29 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20  ) is always the 
2a5e6 73 61 6d 65 0a 2a 2a 20 61 73 20 28 42 29 20 77  same.** as (B) w
2a5e7 65 20 6f 6e 6c 79 20 73 74 6f 72 65 20 28 41 29  e only store (A)
2a5e8 2c 20 74 68 65 6e 20 64 75 70 6c 69 63 61 74 65  , then duplicate
2a5e9 20 28 41 29 20 77 68 65 6e 20 70 75 6c 6c 69 6e   (A) when pullin
2a5ea 67 0a 2a 2a 20 69 74 20 6f 75 74 20 6f 66 20 74  g.** it out of t
2a5eb 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
2a5ec 6c 65 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e  le before callin
2a5ed 67 20 56 55 70 64 61 74 65 2e 0a 2a 2f 0a 73 74  g VUpdate..*/.st
2a5ee 61 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 65  atic void update
2a5ef 56 69 72 74 75 61 6c 54 61 62 6c 65 28 0a 20 20  VirtualTable(.  
2a5f0 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2a5f1 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2a5f2 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2a5f3 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
2a5f4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
2a5f5 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20 62 65  tual table to be
2a5f6 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20 20 54   modified */.  T
2a5f7 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
2a5f8 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75      /* The virtu
2a5f9 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78  al table */.  Ex
2a5fa 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73  prList *pChanges
2a5fb 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e  ,  /* The column
2a5fc 73 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 74  s to change in t
2a5fd 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  he UPDATE statem
2a5fe 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ent */.  Expr *p
2a5ff 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a  Rowid,        /*
2a600 20 45 78 70 72 65 73 73 69 6f 6e 20 75 73 65 64   Expression used
2a601 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68   to recompute th
2a602 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  e rowid */.  int
2a603 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20 20 20   *aXRef,        
2a604 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f    /* Mapping fro
2a605 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 54 61  m columns of pTa
2a606 62 20 74 6f 20 65 6e 74 72 69 65 73 20 69 6e 20  b to entries in 
2a607 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20 45 78  pChanges */.  Ex
2a608 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20  pr *pWhere      
2a609 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75     /* WHERE clau
2a60a 73 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45  se of the UPDATE
2a60b 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b   statement */.){
2a60c 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
2a60d 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20  rse->pVdbe;  /* 
2a60e 56 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  Virtual machine 
2a60f 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
2a610 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  on */.  ExprList
2a611 20 2a 70 45 4c 69 73 74 20 3d 20 30 3b 20 20 20   *pEList = 0;   
2a612 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20    /* The result 
2a613 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43  set of the SELEC
2a614 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  T statement */. 
2a615 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
2a616 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 68   = 0;      /* Th
2a617 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2a618 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  nt */.  Expr *pE
2a619 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
2a61a 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 65    /* Temporary e
2a61b 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69  xpression */.  i
2a61c 6e 74 20 65 70 68 65 6d 54 61 62 3b 20 20 20 20  nt ephemTab;    
2a61d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2a61e 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65  e holding the re
2a61f 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45 4c 45  sult of the SELE
2a620 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  CT */.  int i;  
2a621 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a622 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2a623 72 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b  r */.  int addr;
2a624 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a625 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2a626 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20  op of loop */.  
2a627 69 6e 74 20 69 52 65 67 3b 20 20 20 20 20 20 20  int iReg;       
2a628 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
2a629 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 73  st register in s
2a62a 65 74 20 70 61 73 73 65 64 20 74 6f 20 4f 50 5f  et passed to OP_
2a62b 56 55 70 64 61 74 65 20 2a 2f 0a 20 20 73 71 6c  VUpdate */.  sql
2a62c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2a62d 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74 61 62 61  e->db; /* Databa
2a62e 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2a62f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
2a630 56 74 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68  Vtab = (const ch
2a631 61 72 2a 29 70 54 61 62 2d 3e 70 56 74 61 62 3b  ar*)pTab->pVtab;
2a632 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
2a633 73 74 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72  st;..  /* Constr
2a634 75 63 74 20 74 68 65 20 53 45 4c 45 43 54 20 73  uct the SELECT s
2a635 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69  tatement that wi
2a636 6c 6c 20 66 69 6e 64 20 74 68 65 20 6e 65 77 20  ll find the new 
2a637 76 61 6c 75 65 73 20 66 6f 72 0a 20 20 2a 2a 20  values for.  ** 
2a638 61 6c 6c 20 75 70 64 61 74 65 64 20 72 6f 77 73  all updated rows
2a639 2e 20 0a 20 20 2a 2f 0a 20 20 70 45 4c 69 73 74  . .  */.  pEList
2a63a 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2a63b 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2a63c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
2a63d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a63e 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 72 65        sqlite3Cre
2a63f 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73 65  ateIdExpr(pParse
2a640 2c 20 22 5f 72 6f 77 69 64 5f 22 29 2c 20 30 29  , "_rowid_"), 0)
2a641 3b 0a 20 20 69 66 28 20 70 52 6f 77 69 64 20 29  ;.  if( pRowid )
2a642 7b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 73  {.    pEList = s
2a643 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2a644 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 45 4c  pend(pParse, pEL
2a645 69 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ist,.           
2a646 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a647 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2a648 78 70 72 44 75 70 28 64 62 2c 20 70 52 6f 77 69  xprDup(db, pRowi
2a649 64 2c 20 30 29 2c 20 30 29 3b 0a 20 20 7d 0a 20  d, 0), 0);.  }. 
2a64a 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 69   assert( pTab->i
2a64b 50 4b 65 79 3c 30 20 29 3b 0a 20 20 66 6f 72 28  PKey<0 );.  for(
2a64c 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
2a64d 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  l; i++){.    if(
2a64e 20 61 58 52 65 66 5b 69 5d 3e 3d 30 20 29 7b 0a   aXRef[i]>=0 ){.
2a64f 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 73 71        pExpr = sq
2a650 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2a651 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 61 58 52   pChanges->a[aXR
2a652 65 66 5b 69 5d 5d 2e 70 45 78 70 72 2c 20 30 29  ef[i]].pExpr, 0)
2a653 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2a654 20 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74     pExpr = sqlit
2a655 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28 70  e3CreateIdExpr(p
2a656 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f  Parse, pTab->aCo
2a657 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  l[i].zName);.   
2a658 20 7d 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20   }.    pEList = 
2a659 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2a65a 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 45  ppend(pParse, pE
2a65b 4c 69 73 74 2c 20 70 45 78 70 72 2c 20 30 29 3b  List, pExpr, 0);
2a65c 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 20 3d  .  }.  pSelect =
2a65d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
2a65e 77 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  w(pParse, pEList
2a65f 2c 20 70 53 72 63 2c 20 70 57 68 65 72 65 2c 20  , pSrc, pWhere, 
2a660 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2a661 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 72 65 61 74  );.  .  /* Creat
2a662 65 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20  e the ephemeral 
2a663 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68  table into which
2a664 20 74 68 65 20 75 70 64 61 74 65 20 72 65 73 75   the update resu
2a665 6c 74 73 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65  lts will.  ** be
2a666 20 73 74 6f 72 65 64 2e 0a 20 20 2a 2f 0a 20 20   stored..  */.  
2a667 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20 65  assert( v );.  e
2a668 70 68 65 6d 54 61 62 20 3d 20 70 50 61 72 73 65  phemTab = pParse
2a669 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 73 71 6c 69  ->nTab++;.  sqli
2a66a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a66b 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
2a66c 6c 2c 20 65 70 68 65 6d 54 61 62 2c 20 70 54 61  l, ephemTab, pTa
2a66d 62 2d 3e 6e 43 6f 6c 2b 31 2b 28 70 52 6f 77 69  b->nCol+1+(pRowi
2a66e 64 21 3d 30 29 29 3b 0a 0a 20 20 2f 2a 20 66 69  d!=0));..  /* fi
2a66f 6c 6c 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c  ll the ephemeral
2a670 20 74 61 62 6c 65 20 0a 20 20 2a 2f 0a 20 20 73   table .  */.  s
2a671 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
2a672 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f  Init(&dest, SRT_
2a673 54 61 62 6c 65 2c 20 65 70 68 65 6d 54 61 62 29  Table, ephemTab)
2a674 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2a675 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  t(pParse, pSelec
2a676 74 2c 20 26 64 65 73 74 29 3b 0a 0a 20 20 2f 2a  t, &dest);..  /*
2a677 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2a678 6f 20 73 63 61 6e 20 74 68 65 20 65 70 68 65 6d  o scan the ephem
2a679 65 72 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 63  eral table and c
2a67a 61 6c 6c 20 56 55 70 64 61 74 65 2e 20 2a 2f 0a  all VUpdate. */.
2a67b 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73    iReg = ++pPars
2a67c 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 70 50 61 72 73  e->nMem;.  pPars
2a67d 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d  e->nMem += pTab-
2a67e 3e 6e 43 6f 6c 2b 31 3b 0a 20 20 73 71 6c 69 74  >nCol+1;.  sqlit
2a67f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2a680 4f 50 5f 52 65 77 69 6e 64 2c 20 65 70 68 65 6d  OP_Rewind, ephem
2a681 54 61 62 2c 20 30 29 3b 0a 20 20 61 64 64 72 20  Tab, 0);.  addr 
2a682 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2a683 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 73  rentAddr(v);.  s
2a684 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a685 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20  (v, OP_Column,  
2a686 65 70 68 65 6d 54 61 62 2c 20 30 2c 20 69 52 65  ephemTab, 0, iRe
2a687 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  g);.  sqlite3Vdb
2a688 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2a689 6c 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20  lumn, ephemTab, 
2a68a 28 70 52 6f 77 69 64 3f 31 3a 30 29 2c 20 69 52  (pRowid?1:0), iR
2a68b 65 67 2b 31 29 3b 0a 20 20 66 6f 72 28 69 3d 30  eg+1);.  for(i=0
2a68c 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
2a68d 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
2a68e 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2a68f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68 65 6d 54  P_Column, ephemT
2a690 61 62 2c 20 69 2b 31 2b 28 70 52 6f 77 69 64 21  ab, i+1+(pRowid!
2a691 3d 30 29 2c 20 69 52 65 67 2b 32 2b 69 29 3b 0a  =0), iReg+2+i);.
2a692 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 74 61    }.  sqlite3Vta
2a693 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50  bMakeWritable(pP
2a694 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 73  arse, pTab);.  s
2a695 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2a696 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20  (v, OP_VUpdate, 
2a697 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c  0, pTab->nCol+2,
2a698 20 69 52 65 67 2c 20 70 56 74 61 62 2c 20 50 34   iReg, pVtab, P4
2a699 5f 56 54 41 42 29 3b 0a 20 20 73 71 6c 69 74 65  _VTAB);.  sqlite
2a69a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a69b 50 5f 4e 65 78 74 2c 20 65 70 68 65 6d 54 61 62  P_Next, ephemTab
2a69c 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74  , addr);.  sqlit
2a69d 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2a69e 2c 20 61 64 64 72 2d 31 29 3b 0a 20 20 73 71 6c  , addr-1);.  sql
2a69f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2a6a0 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 65 70 68 65  , OP_Close, ephe
2a6a1 6d 54 61 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20  mTab, 0);..  /* 
2a6a2 43 6c 65 61 6e 75 70 20 2a 2f 0a 20 20 73 71 6c  Cleanup */.  sql
2a6a3 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2a6a4 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 20 20  (db, pSelect);  
2a6a5 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
2a6a6 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2a6a7 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 20 4d 61 6b  TABLE */../* Mak
2a6a8 65 20 73 75 72 65 20 22 69 73 56 69 65 77 22 20  e sure "isView" 
2a6a9 67 65 74 73 20 75 6e 64 65 66 69 6e 65 64 20 69  gets undefined i
2a6aa 6e 20 63 61 73 65 20 74 68 69 73 20 66 69 6c 65  n case this file
2a6ab 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f 66   becomes part of
2a6ac 0a 2a 2a 20 74 68 65 20 61 6d 61 6c 67 61 6d 61  .** the amalgama
2a6ad 74 69 6f 6e 20 2d 20 73 6f 20 74 68 61 74 20 73  tion - so that s
2a6ae 75 62 73 65 71 75 65 6e 74 20 66 69 6c 65 73 20  ubsequent files 
2a6af 64 6f 20 6e 6f 74 20 73 65 65 20 69 73 56 69 65  do not see isVie
2a6b0 77 20 61 73 20 61 0a 2a 2a 20 6d 61 63 72 6f 2e  w as a.** macro.
2a6b1 20 2a 2f 0a 23 75 6e 64 65 66 20 69 73 56 69 65   */.#undef isVie
2a6b2 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  w../************
2a6b3 2a 2a 20 45 6e 64 20 6f 66 20 75 70 64 61 74 65  ** End of update
2a6b4 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2a6b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6b7 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2a6b8 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 61  ** Begin file va
2a6b9 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  cuum.c *********
2a6ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6bc 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41 70  */./*.** 2003 Ap
2a6bd 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ril 6.**.** The 
2a6be 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2a6bf 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2a6c0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2a6c1 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2a6c2 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2a6c3 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2a6c4 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2a6c5 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2a6c6 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2a6c7 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2a6c8 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2a6c9 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2a6ca 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2a6cb 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2a6cc 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2a6cd 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2a6ce 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
2a6cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6d3 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
2a6d4 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
2a6d5 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
2a6d6 74 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d  t the VACUUM com
2a6d7 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74  mand..**.** Most
2a6d8 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20   of the code in 
2a6d9 74 68 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65  this file may be
2a6da 20 6f 6d 69 74 74 65 64 20 62 79 20 64 65 66 69   omitted by defi
2a6db 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49  ning the.** SQLI
2a6dc 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 6d  TE_OMIT_VACUUM m
2a6dd 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  acro..**.** $Id:
2a6de 20 76 61 63 75 75 6d 2e 63 2c 76 20 31 2e 38 38   vacuum.c,v 1.88
2a6df 20 32 30 30 39 2f 30 35 2f 30 35 20 31 37 3a 33   2009/05/05 17:3
2a6e0 37 3a 32 33 20 64 72 68 20 45 78 70 20 24 0a 2a  7:23 drh Exp $.*
2a6e1 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
2a6e2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55  SQLITE_OMIT_VACU
2a6e3 55 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  UM) && !defined(
2a6e4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41  SQLITE_OMIT_ATTA
2a6e5 43 48 29 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  CH)./*.** Execut
2a6e6 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61  e zSql on databa
2a6e7 73 65 20 64 62 2e 20 52 65 74 75 72 6e 20 61 6e  se db. Return an
2a6e8 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
2a6e9 73 74 61 74 69 63 20 69 6e 74 20 65 78 65 63 53  static int execS
2a6ea 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ql(sqlite3 *db, 
2a6eb 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
2a6ec 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
2a6ed 74 20 2a 70 53 74 6d 74 3b 0a 20 20 56 56 41 5f  t *pStmt;.  VVA_
2a6ee 4f 4e 4c 59 28 20 69 6e 74 20 72 63 3b 20 29 0a  ONLY( int rc; ).
2a6ef 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20    if( !zSql ){. 
2a6f0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2a6f1 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66  _NOMEM;.  }.  if
2a6f2 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
2a6f3 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c  ite3_prepare(db,
2a6f4 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d   zSql, -1, &pStm
2a6f5 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74  t, 0) ){.    ret
2a6f6 75 72 6e 20 73 71 6c 69 74 65 33 5f 65 72 72 63  urn sqlite3_errc
2a6f7 6f 64 65 28 64 62 29 3b 0a 20 20 7d 0a 20 20 56  ode(db);.  }.  V
2a6f8 56 41 5f 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20  VA_ONLY( rc = ) 
2a6f9 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
2a6fa 6d 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  mt);.  assert( r
2a6fb 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 3b  c!=SQLITE_ROW );
2a6fc 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
2a6fd 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
2a6fe 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63  );.}../*.** Exec
2a6ff 75 74 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61  ute zSql on data
2a700 62 61 73 65 20 64 62 2e 20 54 68 65 20 73 74 61  base db. The sta
2a701 74 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 65  tement returns e
2a702 78 61 63 74 6c 79 0a 2a 2a 20 6f 6e 65 20 63 6f  xactly.** one co
2a703 6c 75 6d 6e 2e 20 45 78 65 63 75 74 65 20 74 68  lumn. Execute th
2a704 69 73 20 61 73 20 53 51 4c 20 6f 6e 20 74 68 65  is as SQL on the
2a705 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e 0a   same database..
2a706 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78  */.static int ex
2a707 65 63 45 78 65 63 53 71 6c 28 73 71 6c 69 74 65  ecExecSql(sqlite
2a708 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
2a709 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69  r *zSql){.  sqli
2a70a 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
2a70b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63  .  int rc;..  rc
2a70c 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61   = sqlite3_prepa
2a70d 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  re(db, zSql, -1,
2a70e 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69   &pStmt, 0);.  i
2a70f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a710 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2a711 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52   while( SQLITE_R
2a712 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70  OW==sqlite3_step
2a713 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 72  (pStmt) ){.    r
2a714 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20  c = execSql(db, 
2a715 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63  (char*)sqlite3_c
2a716 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74  olumn_text(pStmt
2a717 2c 20 30 29 29 3b 0a 20 20 20 20 69 66 28 20 72  , 0));.    if( r
2a718 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
2a719 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69        sqlite3_fi
2a71a 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20  nalize(pStmt);. 
2a71b 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
2a71c 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
2a71d 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
2a71e 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
2a71f 2f 2a 0a 2a 2a 20 54 68 65 20 6e 6f 6e 2d 73 74  /*.** The non-st
2a720 61 6e 64 61 72 64 20 56 41 43 55 55 4d 20 63 6f  andard VACUUM co
2a721 6d 6d 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f  mmand is used to
2a722 20 63 6c 65 61 6e 20 75 70 20 74 68 65 20 64 61   clean up the da
2a723 74 61 62 61 73 65 2c 0a 2a 2a 20 63 6f 6c 6c 61  tabase,.** colla
2a724 70 73 65 20 66 72 65 65 20 73 70 61 63 65 2c 20  pse free space, 
2a725 65 74 63 2e 20 20 49 74 20 69 73 20 6d 6f 64 65  etc.  It is mode
2a726 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 56  lled after the V
2a727 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 0a 2a 2a  ACUUM command.**
2a728 20 69 6e 20 50 6f 73 74 67 72 65 53 51 4c 2e 0a   in PostgreSQL..
2a729 2a 2a 0a 2a 2a 20 49 6e 20 76 65 72 73 69 6f 6e  **.** In version
2a72a 20 31 2e 30 2e 78 20 6f 66 20 53 51 4c 69 74 65   1.0.x of SQLite
2a72b 2c 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d  , the VACUUM com
2a72c 6d 61 6e 64 20 77 6f 75 6c 64 20 63 61 6c 6c 0a  mand would call.
2a72d 2a 2a 20 67 64 62 6d 5f 72 65 6f 72 67 61 6e 69  ** gdbm_reorgani
2a72e 7a 65 28 29 20 6f 6e 20 61 6c 6c 20 74 68 65 20  ze() on all the 
2a72f 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 73 2e  database tables.
2a730 20 20 42 75 74 20 62 65 67 69 6e 6e 69 6e 67 0a    But beginning.
2a731 2a 2a 20 77 69 74 68 20 32 2e 30 2e 30 2c 20 53  ** with 2.0.0, S
2a732 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20  QLite no longer 
2a733 75 73 65 73 20 47 44 42 4d 20 73 6f 20 74 68 69  uses GDBM so thi
2a734 73 20 63 6f 6d 6d 61 6e 64 20 68 61 73 0a 2a 2a  s command has.**
2a735 20 62 65 63 6f 6d 65 20 61 20 6e 6f 2d 6f 70 2e   become a no-op.
2a736 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2a737 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
2a738 61 63 75 75 6d 28 50 61 72 73 65 20 2a 70 50 61  acuum(Parse *pPa
2a739 72 73 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  rse){.  Vdbe *v 
2a73a 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2a73b 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
2a73c 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
2a73d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2a73e 5f 56 61 63 75 75 6d 2c 20 30 2c 20 30 29 3b 0a  _Vacuum, 0, 0);.
2a73f 20 20 7d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a    }.  return;.}.
2a740 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2a741 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ine implements t
2a742 68 65 20 4f 50 5f 56 61 63 75 75 6d 20 6f 70 63  he OP_Vacuum opc
2a743 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e  ode of the VDBE.
2a744 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2a745 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75  TE int sqlite3Ru
2a746 6e 56 61 63 75 75 6d 28 63 68 61 72 20 2a 2a 70  nVacuum(char **p
2a747 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33  zErrMsg, sqlite3
2a748 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20   *db){.  int rc 
2a749 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
2a74a 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
2a74b 66 72 6f 6d 20 73 65 72 76 69 63 65 20 72 6f 75  from service rou
2a74c 74 69 6e 65 73 20 2a 2f 0a 20 20 42 74 72 65 65  tines */.  Btree
2a74d 20 2a 70 4d 61 69 6e 3b 20 20 20 20 20 20 20 20   *pMain;        
2a74e 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2a74f 73 65 20 62 65 69 6e 67 20 76 61 63 75 75 6d 65  se being vacuume
2a750 64 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 4d  d */.  Pager *pM
2a751 61 69 6e 50 61 67 65 72 3b 20 20 20 20 20 20 2f  ainPager;      /
2a752 2a 20 50 61 67 65 72 20 66 6f 72 20 64 61 74 61  * Pager for data
2a753 62 61 73 65 20 62 65 69 6e 67 20 76 61 63 75 75  base being vacuu
2a754 6d 65 64 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  med */.  Btree *
2a755 70 54 65 6d 70 3b 20 20 20 20 20 20 20 20 20 20  pTemp;          
2a756 20 2f 2a 20 54 68 65 20 74 65 6d 70 6f 72 61 72   /* The temporar
2a757 79 20 64 61 74 61 62 61 73 65 20 77 65 20 76 61  y database we va
2a758 63 75 75 6d 20 69 6e 74 6f 20 2a 2f 0a 20 20 63  cuum into */.  c
2a759 68 61 72 20 2a 7a 53 71 6c 20 3d 20 30 3b 20 20  har *zSql = 0;  
2a75a 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74         /* SQL st
2a75b 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e  atements */.  in
2a75c 74 20 73 61 76 65 64 5f 66 6c 61 67 73 3b 20 20  t saved_flags;  
2a75d 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76        /* Saved v
2a75e 61 6c 75 65 20 6f 66 20 74 68 65 20 64 62 2d 3e  alue of the db->
2a75f 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 73  flags */.  int s
2a760 61 76 65 64 5f 6e 43 68 61 6e 67 65 3b 20 20 20  aved_nChange;   
2a761 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75     /* Saved valu
2a762 65 20 6f 66 20 64 62 2d 3e 6e 43 68 61 6e 67 65  e of db->nChange
2a763 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f   */.  int saved_
2a764 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 20 2f 2a  nTotalChange; /*
2a765 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20   Saved value of 
2a766 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65  db->nTotalChange
2a767 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 20 3d 20   */.  Db *pDb = 
2a768 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  0;            /*
2a769 20 44 61 74 61 62 61 73 65 20 74 6f 20 64 65 74   Database to det
2a76a 61 63 68 20 61 74 20 65 6e 64 20 6f 66 20 76 61  ach at end of va
2a76b 63 75 75 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 73  cuum */.  int is
2a76c 4d 65 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20  MemDb;          
2a76d 20 20 2f 2a 20 54 72 75 65 20 69 73 20 76 61 63    /* True is vac
2a76e 75 75 6d 69 6e 67 20 61 20 3a 6d 65 6d 6f 72 79  uuming a :memory
2a76f 3a 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  : database */.  
2a770 69 6e 74 20 6e 52 65 73 3b 0a 0a 20 20 69 66 28  int nRes;..  if(
2a771 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
2a772 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
2a773 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73  etString(pzErrMs
2a774 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 56  g, db, "cannot V
2a775 41 43 55 55 4d 20 66 72 6f 6d 20 77 69 74 68 69  ACUUM from withi
2a776 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22  n a transaction"
2a777 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
2a778 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
2a779 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 63  .  /* Save the c
2a77a 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
2a77b 74 68 65 20 77 72 69 74 65 2d 73 63 68 65 6d 61  the write-schema
2a77c 20 66 6c 61 67 20 62 65 66 6f 72 65 20 73 65 74   flag before set
2a77d 74 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 20 73 61  ting it. */.  sa
2a77e 76 65 64 5f 66 6c 61 67 73 20 3d 20 64 62 2d 3e  ved_flags = db->
2a77f 66 6c 61 67 73 3b 0a 20 20 73 61 76 65 64 5f 6e  flags;.  saved_n
2a780 43 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 43 68  Change = db->nCh
2a781 61 6e 67 65 3b 0a 20 20 73 61 76 65 64 5f 6e 54  ange;.  saved_nT
2a782 6f 74 61 6c 43 68 61 6e 67 65 20 3d 20 64 62 2d  otalChange = db-
2a783 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 20  >nTotalChange;. 
2a784 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51   db->flags |= SQ
2a785 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61  LITE_WriteSchema
2a786 20 7c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65   | SQLITE_Ignore
2a787 43 68 65 63 6b 73 3b 0a 0a 20 20 70 4d 61 69 6e  Checks;..  pMain
2a788 20 3d 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42   = db->aDb[0].pB
2a789 74 3b 0a 20 20 70 4d 61 69 6e 50 61 67 65 72 20  t;.  pMainPager 
2a78a 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
2a78b 67 65 72 28 70 4d 61 69 6e 29 3b 0a 20 20 69 73  ger(pMain);.  is
2a78c 4d 65 6d 44 62 20 3d 20 73 71 6c 69 74 65 33 50  MemDb = sqlite3P
2a78d 61 67 65 72 46 69 6c 65 28 70 4d 61 69 6e 50 61  agerFile(pMainPa
2a78e 67 65 72 29 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d  ger)->pMethods==
2a78f 30 3b 0a 0a 20 20 2f 2a 20 41 74 74 61 63 68 20  0;..  /* Attach 
2a790 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
2a791 74 61 62 61 73 65 20 61 73 20 27 76 61 63 75 75  tabase as 'vacuu
2a792 6d 5f 64 62 27 2e 20 54 68 65 20 73 79 6e 63 68  m_db'. The synch
2a793 72 6f 6e 6f 75 73 20 70 72 61 67 6d 61 0a 20 20  ronous pragma.  
2a794 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 74 6f  ** can be set to
2a795 20 27 6f 66 66 27 20 66 6f 72 20 74 68 69 73 20   'off' for this 
2a796 66 69 6c 65 2c 20 61 73 20 69 74 20 69 73 20 6e  file, as it is n
2a797 6f 74 20 72 65 63 6f 76 65 72 65 64 20 69 66 20  ot recovered if 
2a798 61 20 63 72 61 73 68 0a 20 20 2a 2a 20 6f 63 63  a crash.  ** occ
2a799 75 72 73 20 61 6e 79 77 61 79 2e 20 54 68 65 20  urs anyway. The 
2a79a 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65  integrity of the
2a79b 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 61 69   database is mai
2a79c 6e 74 61 69 6e 65 64 20 62 79 20 61 0a 20 20 2a  ntained by a.  *
2a79d 2a 20 28 70 6f 73 73 69 62 6c 79 20 73 79 6e 63  * (possibly sync
2a79e 68 72 6f 6e 6f 75 73 29 20 74 72 61 6e 73 61 63  hronous) transac
2a79f 74 69 6f 6e 20 6f 70 65 6e 65 64 20 6f 6e 20 74  tion opened on t
2a7a0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2a7a1 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 73 71 6c   before.  ** sql
2a7a2 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c  ite3BtreeCopyFil
2a7a3 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20  e() is called.. 
2a7a4 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 6f 70 74 69   **.  ** An opti
2a7a5 6d 69 73 61 74 69 6f 6e 20 77 6f 75 6c 64 20 62  misation would b
2a7a6 65 20 74 6f 20 75 73 65 20 61 20 6e 6f 6e 2d 6a  e to use a non-j
2a7a7 6f 75 72 6e 61 6c 65 64 20 70 61 67 65 72 2e 0a  ournaled pager..
2a7a8 20 20 2a 2a 20 28 4c 61 74 65 72 3a 29 20 49 20    ** (Later:) I 
2a7a9 74 72 69 65 64 20 73 65 74 74 69 6e 67 20 22 50  tried setting "P
2a7aa 52 41 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e  RAGMA vacuum_db.
2a7ab 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46  journal_mode=OFF
2a7ac 22 20 62 75 74 0a 20 20 2a 2a 20 74 68 61 74 20  " but.  ** that 
2a7ad 61 63 74 75 61 6c 6c 79 20 6d 61 64 65 20 74 68  actually made th
2a7ae 65 20 56 41 43 55 55 4d 20 72 75 6e 20 73 6c 6f  e VACUUM run slo
2a7af 77 65 72 2e 20 20 56 65 72 79 20 6c 69 74 74 6c  wer.  Very littl
2a7b0 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 0a 20 20  e journalling.  
2a7b1 2a 2a 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75  ** actually occu
2a7b2 72 73 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20  rs when doing a 
2a7b3 76 61 63 75 75 6d 20 73 69 6e 63 65 20 74 68 65  vacuum since the
2a7b4 20 76 61 63 75 75 6d 5f 64 62 20 69 73 20 69 6e   vacuum_db is in
2a7b5 69 74 69 61 6c 6c 79 0a 20 20 2a 2a 20 65 6d 70  itially.  ** emp
2a7b6 74 79 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6a 6f  ty.  Only the jo
2a7b7 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20  urnal header is 
2a7b8 77 72 69 74 74 65 6e 2e 20 20 41 70 70 61 72 65  written.  Appare
2a7b9 6e 74 6c 79 20 69 74 20 74 61 6b 65 73 20 6d 6f  ntly it takes mo
2a7ba 72 65 0a 20 20 2a 2a 20 74 69 6d 65 20 74 6f 20  re.  ** time to 
2a7bb 70 61 72 73 65 20 61 6e 64 20 72 75 6e 20 74 68  parse and run th
2a7bc 65 20 50 52 41 47 4d 41 20 74 6f 20 74 75 72 6e  e PRAGMA to turn
2a7bd 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f 66 66   journalling off
2a7be 20 74 68 61 6e 20 69 74 20 64 6f 65 73 0a 20 20   than it does.  
2a7bf 2a 2a 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  ** to write the 
2a7c0 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
2a7c1 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 7a 53 71 6c  ile..  */.  zSql
2a7c2 20 3d 20 22 41 54 54 41 43 48 20 27 27 20 41 53   = "ATTACH '' AS
2a7c3 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0a 20 20   vacuum_db;";.  
2a7c4 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
2a7c5 20 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63   zSql);.  if( rc
2a7c6 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2a7c7 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2a7c8 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  ;.  pDb = &db->a
2a7c9 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20  Db[db->nDb-1];. 
2a7ca 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28   assert( strcmp(
2a7cb 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d  db->aDb[db->nDb-
2a7cc 31 5d 2e 7a 4e 61 6d 65 2c 22 76 61 63 75 75 6d  1].zName,"vacuum
2a7cd 5f 64 62 22 29 3d 3d 30 20 29 3b 0a 20 20 70 54  _db")==0 );.  pT
2a7ce 65 6d 70 20 3d 20 64 62 2d 3e 61 44 62 5b 64 62  emp = db->aDb[db
2a7cf 2d 3e 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a 0a 20  ->nDb-1].pBt;.. 
2a7d0 20 6e 52 65 73 20 3d 20 73 71 6c 69 74 65 33 42   nRes = sqlite3B
2a7d1 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 70  treeGetReserve(p
2a7d2 4d 61 69 6e 29 3b 0a 0a 20 20 2f 2a 20 41 20 56  Main);..  /* A V
2a7d3 41 43 55 55 4d 20 63 61 6e 6e 6f 74 20 63 68 61  ACUUM cannot cha
2a7d4 6e 67 65 20 74 68 65 20 70 61 67 65 73 69 7a 65  nge the pagesize
2a7d5 20 6f 66 20 61 6e 20 65 6e 63 72 79 70 74 65 64   of an encrypted
2a7d6 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 23 69   database. */.#i
2a7d7 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
2a7d8 43 4f 44 45 43 0a 20 20 69 66 28 20 64 62 2d 3e  CODEC.  if( db->
2a7d9 6e 65 78 74 50 61 67 65 73 69 7a 65 20 29 7b 0a  nextPagesize ){.
2a7da 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20      extern void 
2a7db 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b  sqlite3CodecGetK
2a7dc 65 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ey(sqlite3*, int
2a7dd 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b  , void**, int*);
2a7de 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20  .    int nKey;. 
2a7df 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20     char *zKey;. 
2a7e0 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47     sqlite3CodecG
2a7e1 65 74 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f  etKey(db, 0, (vo
2a7e2 69 64 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65  id**)&zKey, &nKe
2a7e3 79 29 3b 0a 20 20 20 20 69 66 28 20 6e 4b 65 79  y);.    if( nKey
2a7e4 20 29 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73   ) db->nextPages
2a7e5 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e  ize = 0;.  }.#en
2a7e6 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  dif..  if( sqlit
2a7e7 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69  e3BtreeSetPageSi
2a7e8 7a 65 28 70 54 65 6d 70 2c 20 73 71 6c 69 74 65  ze(pTemp, sqlite
2a7e9 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
2a7ea 65 28 70 4d 61 69 6e 29 2c 20 6e 52 65 73 2c 20  e(pMain), nRes, 
2a7eb 30 29 0a 20 20 20 7c 7c 20 28 21 69 73 4d 65 6d  0).   || (!isMem
2a7ec 44 62 20 26 26 20 73 71 6c 69 74 65 33 42 74 72  Db && sqlite3Btr
2a7ed 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 54  eeSetPageSize(pT
2a7ee 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67  emp, db->nextPag
2a7ef 65 73 69 7a 65 2c 20 6e 52 65 73 2c 20 30 29 29  esize, nRes, 0))
2a7f0 0a 20 20 20 7c 7c 20 4e 45 56 45 52 28 64 62 2d  .   || NEVER(db-
2a7f1 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 0a 20  >mallocFailed). 
2a7f2 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
2a7f3 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
2a7f4 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2a7f5 6d 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 65 78  m;.  }.  rc = ex
2a7f6 65 63 53 71 6c 28 64 62 2c 20 22 50 52 41 47 4d  ecSql(db, "PRAGM
2a7f7 41 20 76 61 63 75 75 6d 5f 64 62 2e 73 79 6e 63  A vacuum_db.sync
2a7f8 68 72 6f 6e 6f 75 73 3d 4f 46 46 22 29 3b 0a 20  hronous=OFF");. 
2a7f9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2a7fa 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  OK ){.    goto e
2a7fb 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
2a7fc 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2a7fd 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
2a7fe 4d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  M.  sqlite3Btree
2a7ff 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 54  SetAutoVacuum(pT
2a800 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 41 75 74  emp, db->nextAut
2a801 6f 76 61 63 3e 3d 30 20 3f 20 64 62 2d 3e 6e 65  ovac>=0 ? db->ne
2a802 78 74 41 75 74 6f 76 61 63 20 3a 0a 20 20 20 20  xtAutovac :.    
2a803 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a804 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a805 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
2a806 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d  reeGetAutoVacuum
2a807 28 70 4d 61 69 6e 29 29 3b 0a 23 65 6e 64 69 66  (pMain));.#endif
2a808 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74  ..  /* Begin a t
2a809 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
2a80a 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
2a80b 20 22 42 45 47 49 4e 20 45 58 43 4c 55 53 49 56   "BEGIN EXCLUSIV
2a80c 45 3b 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  E;");.  if( rc!=
2a80d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2a80e 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2a80f 0a 20 20 2f 2a 20 51 75 65 72 79 20 74 68 65 20  .  /* Query the 
2a810 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 6d 61  schema of the ma
2a811 69 6e 20 64 61 74 61 62 61 73 65 2e 20 43 72 65  in database. Cre
2a812 61 74 65 20 61 20 6d 69 72 72 6f 72 20 73 63 68  ate a mirror sch
2a813 65 6d 61 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ema.  ** in the 
2a814 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
2a815 73 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  se..  */.  rc = 
2a816 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20  execExecSql(db, 
2a817 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27  .      "SELECT '
2a818 43 52 45 41 54 45 20 54 41 42 4c 45 20 76 61 63  CREATE TABLE vac
2a819 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73  uum_db.' || subs
2a81a 74 72 28 73 71 6c 2c 31 34 29 20 22 0a 20 20 20  tr(sql,14) ".   
2a81b 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69 74     "  FROM sqlit
2a81c 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 74  e_master WHERE t
2a81d 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e 44 20  ype='table' AND 
2a81e 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f 73 65  name!='sqlite_se
2a81f 71 75 65 6e 63 65 27 22 0a 20 20 20 20 20 20 22  quence'".      "
2a820 20 20 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3e     AND rootpage>
2a821 30 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  0".  );.  if( rc
2a822 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2a823 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2a824 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  ;.  rc = execExe
2a825 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
2a826 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45 20  "SELECT 'CREATE 
2a827 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62 2e  INDEX vacuum_db.
2a828 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  ' || substr(sql,
2a829 31 34 29 22 0a 20 20 20 20 20 20 22 20 20 46 52  14)".      "  FR
2a82a 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
2a82b 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b 45 20   WHERE sql LIKE 
2a82c 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 25 27  'CREATE INDEX %'
2a82d 20 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53   ");.  if( rc!=S
2a82e 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2a82f 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2a830 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71   rc = execExecSq
2a831 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45  l(db, .      "SE
2a832 4c 45 43 54 20 27 43 52 45 41 54 45 20 55 4e 49  LECT 'CREATE UNI
2a833 51 55 45 20 49 4e 44 45 58 20 76 61 63 75 75 6d  QUE INDEX vacuum
2a834 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28  _db.' || substr(
2a835 73 71 6c 2c 32 31 29 20 22 0a 20 20 20 20 20 20  sql,21) ".      
2a836 22 20 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d  "  FROM sqlite_m
2a837 61 73 74 65 72 20 57 48 45 52 45 20 73 71 6c 20  aster WHERE sql 
2a838 4c 49 4b 45 20 27 43 52 45 41 54 45 20 55 4e 49  LIKE 'CREATE UNI
2a839 51 55 45 20 49 4e 44 45 58 20 25 27 22 29 3b 0a  QUE INDEX %'");.
2a83a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2a83b 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2a83c 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20  f_vacuum;..  /* 
2a83d 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65  Loop through the
2a83e 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6d   tables in the m
2a83f 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 46 6f  ain database. Fo
2a840 72 20 65 61 63 68 2c 20 64 6f 0a 20 20 2a 2a 20  r each, do.  ** 
2a841 61 6e 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20  an "INSERT INTO 
2a842 76 61 63 75 75 6d 5f 64 62 2e 78 78 78 20 53 45  vacuum_db.xxx SE
2a843 4c 45 43 54 20 2a 20 46 52 4f 4d 20 78 78 78 3b  LECT * FROM xxx;
2a844 22 20 74 6f 20 63 6f 70 79 0a 20 20 2a 2a 20 74  " to copy.  ** t
2a845 68 65 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 74  he contents to t
2a846 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  he temporary dat
2a847 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  abase..  */.  rc
2a848 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64   = execExecSql(d
2a849 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43  b, .      "SELEC
2a84a 54 20 27 49 4e 53 45 52 54 20 49 4e 54 4f 20 76  T 'INSERT INTO v
2a84b 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75  acuum_db.' || qu
2a84c 6f 74 65 28 6e 61 6d 65 29 20 22 0a 20 20 20 20  ote(name) ".    
2a84d 20 20 22 7c 7c 20 27 20 53 45 4c 45 43 54 20 2a    "|| ' SELECT *
2a84e 20 46 52 4f 4d 20 27 20 7c 7c 20 71 75 6f 74 65   FROM ' || quote
2a84f 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27 22 0a 20  (name) || ';'". 
2a850 20 20 20 20 20 22 46 52 4f 4d 20 73 71 6c 69 74       "FROM sqlit
2a851 65 5f 6d 61 73 74 65 72 20 22 0a 20 20 20 20 20  e_master ".     
2a852 20 22 57 48 45 52 45 20 74 79 70 65 20 3d 20 27   "WHERE type = '
2a853 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65 21  table' AND name!
2a854 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  ='sqlite_sequenc
2a855 65 27 20 22 0a 20 20 20 20 20 20 22 20 20 41 4e  e' ".      "  AN
2a856 44 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a 0a 20  D rootpage>0".. 
2a857 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
2a858 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2a859 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20  nd_of_vacuum;.. 
2a85a 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20 74 68   /* Copy over th
2a85b 65 20 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e sequence table
2a85c 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65  .  */.  rc = exe
2a85d 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20  cExecSql(db, .  
2a85e 20 20 20 20 22 53 45 4c 45 43 54 20 27 44 45 4c      "SELECT 'DEL
2a85f 45 54 45 20 46 52 4f 4d 20 76 61 63 75 75 6d 5f  ETE FROM vacuum_
2a860 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61  db.' || quote(na
2a861 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0a 20 20 20  me) || ';' ".   
2a862 20 20 20 22 46 52 4f 4d 20 76 61 63 75 75 6d 5f     "FROM vacuum_
2a863 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  db.sqlite_master
2a864 20 57 48 45 52 45 20 6e 61 6d 65 3d 27 73 71 6c   WHERE name='sql
2a865 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22 0a  ite_sequence' ".
2a866 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53    );.  if( rc!=S
2a867 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2a868 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2a869 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71   rc = execExecSq
2a86a 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45  l(db, .      "SE
2a86b 4c 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e 54  LECT 'INSERT INT
2a86c 4f 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c  O vacuum_db.' ||
2a86d 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0a 20   quote(name) ". 
2a86e 20 20 20 20 20 22 7c 7c 20 27 20 53 45 4c 45 43       "|| ' SELEC
2a86f 54 20 2a 20 46 52 4f 4d 20 27 20 7c 7c 20 71 75  T * FROM ' || qu
2a870 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27  ote(name) || ';'
2a871 20 22 0a 20 20 20 20 20 20 22 46 52 4f 4d 20 76   ".      "FROM v
2a872 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f  acuum_db.sqlite_
2a873 6d 61 73 74 65 72 20 57 48 45 52 45 20 6e 61 6d  master WHERE nam
2a874 65 3d 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65  e=='sqlite_seque
2a875 6e 63 65 27 3b 22 0a 20 20 29 3b 0a 20 20 69 66  nce';".  );.  if
2a876 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2a877 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
2a878 63 75 75 6d 3b 0a 0a 0a 20 20 2f 2a 20 43 6f 70  cuum;...  /* Cop
2a879 79 20 74 68 65 20 74 72 69 67 67 65 72 73 2c 20  y the triggers, 
2a87a 76 69 65 77 73 2c 20 61 6e 64 20 76 69 72 74 75  views, and virtu
2a87b 61 6c 20 74 61 62 6c 65 73 20 66 72 6f 6d 20 74  al tables from t
2a87c 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2a87d 0a 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68  .  ** over to th
2a87e 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  e temporary data
2a87f 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74  base.  None of t
2a880 68 65 73 65 20 6f 62 6a 65 63 74 73 20 68 61 73  hese objects has
2a881 20 61 6e 79 0a 20 20 2a 2a 20 61 73 73 6f 63 69   any.  ** associ
2a882 61 74 65 64 20 73 74 6f 72 61 67 65 2c 20 73 6f  ated storage, so
2a883 20 61 6c 6c 20 77 65 20 68 61 76 65 20 74 6f 20   all we have to 
2a884 64 6f 20 69 73 20 63 6f 70 79 20 74 68 65 69 72  do is copy their
2a885 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 66 72   entries.  ** fr
2a886 6f 6d 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41  om the SQLITE_MA
2a887 53 54 45 52 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  STER table..  */
2a888 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28  .  rc = execSql(
2a889 64 62 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 52  db,.      "INSER
2a88a 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62  T INTO vacuum_db
2a88b 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 22  .sqlite_master "
2a88c 0a 20 20 20 20 20 20 22 20 20 53 45 4c 45 43 54  .      "  SELECT
2a88d 20 74 79 70 65 2c 20 6e 61 6d 65 2c 20 74 62 6c   type, name, tbl
2a88e 5f 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c  _name, rootpage,
2a88f 20 73 71 6c 22 0a 20 20 20 20 20 20 22 20 20 20   sql".      "   
2a890 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73   FROM sqlite_mas
2a891 74 65 72 22 0a 20 20 20 20 20 20 22 20 20 20 57  ter".      "   W
2a892 48 45 52 45 20 74 79 70 65 3d 27 76 69 65 77 27  HERE type='view'
2a893 20 4f 52 20 74 79 70 65 3d 27 74 72 69 67 67 65   OR type='trigge
2a894 72 27 22 0a 20 20 20 20 20 20 22 20 20 20 20 20  r'".      "     
2a895 20 4f 52 20 28 74 79 70 65 3d 27 74 61 62 6c 65   OR (type='table
2a896 27 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3d 30  ' AND rootpage=0
2a897 29 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  )".  );.  if( rc
2a898 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
2a899 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 41 74 20  acuum;..  /* At 
2a89a 74 68 69 73 20 70 6f 69 6e 74 2c 20 75 6e 6c 65  this point, unle
2a89b 73 73 20 74 68 65 20 6d 61 69 6e 20 64 62 20 77  ss the main db w
2a89c 61 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d  as completely em
2a89d 70 74 79 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  pty, there is no
2a89e 77 20 61 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  w a.  ** transac
2a89f 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20 74 68 65  tion open on the
2a8a0 20 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65   vacuum database
2a8a1 2c 20 62 75 74 20 6e 6f 74 20 6f 6e 20 74 68 65  , but not on the
2a8a2 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 0a   main database..
2a8a3 20 20 2a 2a 20 4f 70 65 6e 20 61 20 62 74 72 65    ** Open a btre
2a8a4 65 20 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 74  e level transact
2a8a5 69 6f 6e 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  ion on the main 
2a8a6 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 61  database. This a
2a8a7 6c 6c 6f 77 73 20 61 0a 20 20 2a 2a 20 63 61 6c  llows a.  ** cal
2a8a8 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
2a8a9 65 43 6f 70 79 46 69 6c 65 28 29 2e 20 54 68 65  eCopyFile(). The
2a8aa 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 62   main database b
2a8ab 74 72 65 65 20 6c 65 76 65 6c 0a 20 20 2a 2a 20  tree level.  ** 
2a8ac 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74  transaction is t
2a8ad 68 65 6e 20 63 6f 6d 6d 69 74 74 65 64 2c 20 73  hen committed, s
2a8ae 6f 20 74 68 65 20 53 51 4c 20 6c 65 76 65 6c 20  o the SQL level 
2a8af 6e 65 76 65 72 20 6b 6e 6f 77 73 20 69 74 20 77  never knows it w
2a8b0 61 73 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20 66  as.  ** opened f
2a8b1 6f 72 20 77 72 69 74 69 6e 67 2e 20 54 68 69 73  or writing. This
2a8b2 20 77 61 79 2c 20 74 68 65 20 53 51 4c 20 74 72   way, the SQL tr
2a8b3 61 6e 73 61 63 74 69 6f 6e 20 75 73 65 64 20 74  ansaction used t
2a8b4 6f 20 63 72 65 61 74 65 20 74 68 65 0a 20 20 2a  o create the.  *
2a8b5 2a 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  * temporary data
2a8b6 62 61 73 65 20 6e 65 76 65 72 20 6e 65 65 64 73  base never needs
2a8b7 20 74 6f 20 62 65 20 63 6f 6d 6d 69 74 74 65 64   to be committed
2a8b8 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75  ..  */.  {.    u
2a8b9 33 32 20 6d 65 74 61 3b 0a 20 20 20 20 69 6e 74  32 meta;.    int
2a8ba 20 69 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73   i;..    /* This
2a8bb 20 61 72 72 61 79 20 64 65 74 65 72 6d 69 6e 65   array determine
2a8bc 73 20 77 68 69 63 68 20 6d 65 74 61 20 6d 65 74  s which meta met
2a8bd 61 20 76 61 6c 75 65 73 20 61 72 65 20 70 72 65  a values are pre
2a8be 73 65 72 76 65 64 20 69 6e 20 74 68 65 0a 20 20  served in the.  
2a8bf 20 20 2a 2a 20 76 61 63 75 75 6d 2e 20 20 45 76    ** vacuum.  Ev
2a8c0 65 6e 20 65 6e 74 72 69 65 73 20 61 72 65 20 74  en entries are t
2a8c1 68 65 20 6d 65 74 61 20 76 61 6c 75 65 20 6e 75  he meta value nu
2a8c2 6d 62 65 72 20 61 6e 64 20 6f 64 64 20 65 6e 74  mber and odd ent
2a8c3 72 69 65 73 0a 20 20 20 20 2a 2a 20 61 72 65 20  ries.    ** are 
2a8c4 61 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20  an increment to 
2a8c5 61 70 70 6c 79 20 74 6f 20 74 68 65 20 6d 65 74  apply to the met
2a8c6 61 20 76 61 6c 75 65 20 61 66 74 65 72 20 74 68  a value after th
2a8c7 65 20 76 61 63 75 75 6d 2e 0a 20 20 20 20 2a 2a  e vacuum..    **
2a8c8 20 54 68 65 20 69 6e 63 72 65 6d 65 6e 74 20 69   The increment i
2a8c9 73 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 61  s used to increa
2a8ca 73 65 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  se the schema co
2a8cb 6f 6b 69 65 20 73 6f 20 74 68 61 74 20 6f 74 68  okie so that oth
2a8cc 65 72 0a 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63  er.    ** connec
2a8cd 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d  tions to the sam
2a8ce 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20  e database will 
2a8cf 6b 6e 6f 77 20 74 6f 20 72 65 72 65 61 64 20 74  know to reread t
2a8d0 68 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a  he schema..    *
2a8d1 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  /.    static con
2a8d2 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2a8d3 20 61 43 6f 70 79 5b 5d 20 3d 20 7b 0a 20 20 20   aCopy[] = {.   
2a8d4 20 20 20 20 31 2c 20 31 2c 20 20 20 20 2f 2a 20      1, 1,    /* 
2a8d5 41 64 64 20 6f 6e 65 20 74 6f 20 74 68 65 20 6f  Add one to the o
2a8d6 6c 64 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  ld schema cookie
2a8d7 20 2a 2f 0a 20 20 20 20 20 20 20 33 2c 20 30 2c   */.       3, 0,
2a8d8 20 20 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20      /* Preserve 
2a8d9 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  the default page
2a8da 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20   cache size */. 
2a8db 20 20 20 20 20 20 35 2c 20 30 2c 20 20 20 20 2f        5, 0,    /
2a8dc 2a 20 50 72 65 73 65 72 76 65 20 74 68 65 20 64  * Preserve the d
2a8dd 65 66 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f  efault text enco
2a8de 64 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 36  ding */.       6
2a8df 2c 20 30 2c 20 20 20 20 2f 2a 20 50 72 65 73 65  , 0,    /* Prese
2a8e0 72 76 65 20 74 68 65 20 75 73 65 72 20 76 65 72  rve the user ver
2a8e1 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 7d 3b 0a 0a  sion */.    };..
2a8e2 20 20 20 20 61 73 73 65 72 74 28 20 31 3d 3d 73      assert( 1==s
2a8e3 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
2a8e4 72 61 6e 73 28 70 54 65 6d 70 29 20 29 3b 0a 20  rans(pTemp) );. 
2a8e5 20 20 20 61 73 73 65 72 74 28 20 31 3d 3d 73 71     assert( 1==sq
2a8e6 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
2a8e7 61 6e 73 28 70 4d 61 69 6e 29 20 29 3b 0a 0a 20  ans(pMain) );.. 
2a8e8 20 20 20 2f 2a 20 43 6f 70 79 20 42 74 72 65 65     /* Copy Btree
2a8e9 20 6d 65 74 61 20 76 61 6c 75 65 73 20 2a 2f 0a   meta values */.
2a8ea 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41      for(i=0; i<A
2a8eb 72 72 61 79 53 69 7a 65 28 61 43 6f 70 79 29 3b  rraySize(aCopy);
2a8ec 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 2f 2a   i+=2){.      /*
2a8ed 20 47 65 74 4d 65 74 61 28 29 20 61 6e 64 20 55   GetMeta() and U
2a8ee 70 64 61 74 65 4d 65 74 61 28 29 20 63 61 6e 6e  pdateMeta() cann
2a8ef 6f 74 20 66 61 69 6c 20 69 6e 20 74 68 69 73 20  ot fail in this 
2a8f0 63 6f 6e 74 65 78 74 20 62 65 63 61 75 73 65 0a  context because.
2a8f1 20 20 20 20 20 20 2a 2a 20 77 65 20 61 6c 72 65        ** we alre
2a8f2 61 64 79 20 68 61 76 65 20 70 61 67 65 20 31 20  ady have page 1 
2a8f3 6c 6f 61 64 65 64 20 69 6e 74 6f 20 63 61 63 68  loaded into cach
2a8f4 65 20 61 6e 64 20 6d 61 72 6b 65 64 20 64 69 72  e and marked dir
2a8f5 74 79 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  ty. */.      rc 
2a8f6 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
2a8f7 74 4d 65 74 61 28 70 4d 61 69 6e 2c 20 61 43 6f  tMeta(pMain, aCo
2a8f8 70 79 5b 69 5d 2c 20 26 6d 65 74 61 29 3b 0a 20  py[i], &meta);. 
2a8f9 20 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72       if( NEVER(r
2a8fa 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20  c!=SQLITE_OK) ) 
2a8fb 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
2a8fc 75 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  um;.      rc = s
2a8fd 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
2a8fe 65 4d 65 74 61 28 70 54 65 6d 70 2c 20 61 43 6f  eMeta(pTemp, aCo
2a8ff 70 79 5b 69 5d 2c 20 6d 65 74 61 2b 61 43 6f 70  py[i], meta+aCop
2a900 79 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69  y[i+1]);.      i
2a901 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c  f( NEVER(rc!=SQL
2a902 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 65  ITE_OK) ) goto e
2a903 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
2a904 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71    }..    rc = sq
2a905 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69  lite3BtreeCopyFi
2a906 6c 65 28 70 4d 61 69 6e 2c 20 70 54 65 6d 70 29  le(pMain, pTemp)
2a907 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2a908 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2a909 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
2a90a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
2a90b 72 65 65 43 6f 6d 6d 69 74 28 70 54 65 6d 70 29  reeCommit(pTemp)
2a90c 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2a90d 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2a90e 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 23 69  nd_of_vacuum;.#i
2a90f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2a910 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
2a911 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
2a912 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e  AutoVacuum(pMain
2a913 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  , sqlite3BtreeGe
2a914 74 41 75 74 6f 56 61 63 75 75 6d 28 70 54 65 6d  tAutoVacuum(pTem
2a915 70 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  p));.#endif.  }.
2a916 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
2a917 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63  QLITE_OK );.  rc
2a918 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53   = sqlite3BtreeS
2a919 65 74 50 61 67 65 53 69 7a 65 28 70 4d 61 69 6e  etPageSize(pMain
2a91a 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  , sqlite3BtreeGe
2a91b 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70 29  tPageSize(pTemp)
2a91c 2c 20 6e 52 65 73 2c 31 29 3b 0a 0a 65 6e 64 5f  , nRes,1);..end_
2a91d 6f 66 5f 76 61 63 75 75 6d 3a 0a 20 20 2f 2a 20  of_vacuum:.  /* 
2a91e 52 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67  Restore the orig
2a91f 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 64 62  inal value of db
2a920 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 64 62 2d  ->flags */.  db-
2a921 3e 66 6c 61 67 73 20 3d 20 73 61 76 65 64 5f 66  >flags = saved_f
2a922 6c 61 67 73 3b 0a 20 20 64 62 2d 3e 6e 43 68 61  lags;.  db->nCha
2a923 6e 67 65 20 3d 20 73 61 76 65 64 5f 6e 43 68 61  nge = saved_nCha
2a924 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61  nge;.  db->nTota
2a925 6c 43 68 61 6e 67 65 20 3d 20 73 61 76 65 64 5f  lChange = saved_
2a926 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 0a 20  nTotalChange;.. 
2a927 20 2f 2a 20 43 75 72 72 65 6e 74 6c 79 20 74 68   /* Currently th
2a928 65 72 65 20 69 73 20 61 6e 20 53 51 4c 20 6c 65  ere is an SQL le
2a929 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  vel transaction 
2a92a 6f 70 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75  open on the vacu
2a92b 75 6d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  um.  ** database
2a92c 2e 20 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68  . No locks are h
2a92d 65 6c 64 20 6f 6e 20 61 6e 79 20 6f 74 68 65 72  eld on any other
2a92e 20 66 69 6c 65 73 20 28 73 69 6e 63 65 20 74 68   files (since th
2a92f 65 20 6d 61 69 6e 20 66 69 6c 65 0a 20 20 2a 2a  e main file.  **
2a930 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 20 61   was committed a
2a931 74 20 74 68 65 20 62 74 72 65 65 20 6c 65 76 65  t the btree leve
2a932 6c 29 2e 20 53 6f 20 69 74 20 73 61 66 65 20 74  l). So it safe t
2a933 6f 20 65 6e 64 20 74 68 65 20 74 72 61 6e 73 61  o end the transa
2a934 63 74 69 6f 6e 0a 20 20 2a 2a 20 62 79 20 6d 61  ction.  ** by ma
2a935 6e 75 61 6c 6c 79 20 73 65 74 74 69 6e 67 20 74  nually setting t
2a936 68 65 20 61 75 74 6f 43 6f 6d 6d 69 74 20 66 6c  he autoCommit fl
2a937 61 67 20 74 6f 20 74 72 75 65 20 61 6e 64 20 64  ag to true and d
2a938 65 74 61 63 68 69 6e 67 20 74 68 65 0a 20 20 2a  etaching the.  *
2a939 2a 20 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  * vacuum databas
2a93a 65 2e 20 54 68 65 20 76 61 63 75 75 6d 5f 64 62  e. The vacuum_db
2a93b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
2a93c 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68   deleted when th
2a93d 65 20 70 61 67 65 72 0a 20 20 2a 2a 20 69 73 20  e pager.  ** is 
2a93e 63 6c 6f 73 65 64 20 62 79 20 74 68 65 20 44 45  closed by the DE
2a93f 54 41 43 48 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d  TACH..  */.  db-
2a940 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
2a941 0a 0a 20 20 69 66 28 20 70 44 62 20 29 7b 0a 20  ..  if( pDb ){. 
2a942 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
2a943 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a  lose(pDb->pBt);.
2a944 20 20 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30      pDb->pBt = 0
2a945 3b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65  ;.    pDb->pSche
2a946 6d 61 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73  ma = 0;.  }..  s
2a947 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
2a948 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
2a949 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  ;..  return rc;.
2a94a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c  }.#endif  /* SQL
2a94b 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20  ITE_OMIT_VACUUM 
2a94c 26 26 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  && SQLITE_OMIT_A
2a94d 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  TTACH */../*****
2a94e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2a94f 20 76 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a   vacuum.c ******
2a950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a951 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a952 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2a953 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2a954 66 69 6c 65 20 76 74 61 62 2e 63 20 2a 2a 2a 2a  file vtab.c ****
2a955 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a956 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a957 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2a958 32 30 30 36 20 4a 75 6e 65 20 31 30 0a 2a 2a 0a  2006 June 10.**.
2a959 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2a95a 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2a95b 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2a95c 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2a95d 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2a95e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2a95f 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2a960 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2a961 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2a962 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2a963 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2a964 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2a965 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2a966 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2a967 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2a968 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2a969 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2a96a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2a96b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a96c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a96d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a96e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2a96f 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
2a970 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 68  s code used to h
2a971 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 76 69  elp implement vi
2a972 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2a  rtual tables..**
2a973 0a 2a 2a 20 24 49 64 3a 20 76 74 61 62 2e 63 2c  .** $Id: vtab.c,
2a974 76 20 31 2e 38 36 20 32 30 30 39 2f 30 34 2f 32  v 1.86 2009/04/2
2a975 38 20 31 33 3a 30 31 3a 30 39 20 64 72 68 20 45  8 13:01:09 drh E
2a976 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
2a977 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2a978 55 41 4c 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20  UALTABLE../*.** 
2a979 54 68 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74  The actual funct
2a97a 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 74 68  ion that does th
2a97b 65 20 77 6f 72 6b 20 6f 66 20 63 72 65 61 74 69  e work of creati
2a97c 6e 67 20 61 20 6e 65 77 20 6d 6f 64 75 6c 65 2e  ng a new module.
2a97d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2a97e 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  n implements the
2a97f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2a980 6d 6f 64 75 6c 65 28 29 20 61 6e 64 0a 2a 2a 20  module() and.** 
2a981 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
2a982 6f 64 75 6c 65 5f 76 32 28 29 20 69 6e 74 65 72  odule_v2() inter
2a983 66 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  faces..*/.static
2a984 20 69 6e 74 20 63 72 65 61 74 65 4d 6f 64 75 6c   int createModul
2a985 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
2a986 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2a987 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2a988 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65   in which module
2a989 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 2a   is registered *
2a98a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2a98b 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20  zName,          
2a98c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69      /* Name assi
2a98d 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64  gned to this mod
2a98e 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
2a98f 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
2a990 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20  Module,  /* The 
2a991 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68  definition of th
2a992 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f  e module */.  vo
2a993 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20  id *pAux,       
2a994 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a995 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   Context pointer
2a996 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
2a997 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20  nnect */.  void 
2a998 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64  (*xDestroy)(void
2a999 20 2a 29 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f   *)        /* Mo
2a99a 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20  dule destructor 
2a99b 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 20 7b 0a  function */.) {.
2a99c 20 20 69 6e 74 20 72 63 2c 20 6e 4e 61 6d 65 3b    int rc, nName;
2a99d 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b  .  Module *pMod;
2a99e 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
2a99f 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
2a9a0 78 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71  x);.  nName = sq
2a9a1 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
2a9a2 61 6d 65 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28  ame);.  pMod = (
2a9a3 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33  Module *)sqlite3
2a9a4 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
2a9a5 73 69 7a 65 6f 66 28 4d 6f 64 75 6c 65 29 20 2b  sizeof(Module) +
2a9a6 20 6e 4e 61 6d 65 20 2b 20 31 29 3b 0a 20 20 69   nName + 1);.  i
2a9a7 66 28 20 70 4d 6f 64 20 29 7b 0a 20 20 20 20 4d  f( pMod ){.    M
2a9a8 6f 64 75 6c 65 20 2a 70 44 65 6c 3b 0a 20 20 20  odule *pDel;.   
2a9a9 20 63 68 61 72 20 2a 7a 43 6f 70 79 20 3d 20 28   char *zCopy = (
2a9aa 63 68 61 72 20 2a 29 28 26 70 4d 6f 64 5b 31 5d  char *)(&pMod[1]
2a9ab 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 43  );.    memcpy(zC
2a9ac 6f 70 79 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  opy, zName, nNam
2a9ad 65 2b 31 29 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e  e+1);.    pMod->
2a9ae 7a 4e 61 6d 65 20 3d 20 7a 43 6f 70 79 3b 0a 20  zName = zCopy;. 
2a9af 20 20 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65     pMod->pModule
2a9b0 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20   = pModule;.    
2a9b1 70 4d 6f 64 2d 3e 70 41 75 78 20 3d 20 70 41 75  pMod->pAux = pAu
2a9b2 78 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 78 44 65  x;.    pMod->xDe
2a9b3 73 74 72 6f 79 20 3d 20 78 44 65 73 74 72 6f 79  stroy = xDestroy
2a9b4 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20 28 4d 6f  ;.    pDel = (Mo
2a9b5 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 48 61  dule *)sqlite3Ha
2a9b6 73 68 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 4d  shInsert(&db->aM
2a9b7 6f 64 75 6c 65 2c 20 7a 43 6f 70 79 2c 20 6e 4e  odule, zCopy, nN
2a9b8 61 6d 65 2c 20 28 76 6f 69 64 2a 29 70 4d 6f 64  ame, (void*)pMod
2a9b9 29 3b 0a 20 20 20 20 69 66 28 20 70 44 65 6c 20  );.    if( pDel 
2a9ba 26 26 20 70 44 65 6c 2d 3e 78 44 65 73 74 72 6f  && pDel->xDestro
2a9bb 79 20 29 7b 0a 20 20 20 20 20 20 70 44 65 6c 2d  y ){.      pDel-
2a9bc 3e 78 44 65 73 74 72 6f 79 28 70 44 65 6c 2d 3e  >xDestroy(pDel->
2a9bd 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pAux);.    }.   
2a9be 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2a9bf 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 69 66  b, pDel);.    if
2a9c0 28 20 70 44 65 6c 3d 3d 70 4d 6f 64 20 29 7b 0a  ( pDel==pMod ){.
2a9c1 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
2a9c2 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
2a9c3 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  }.    sqlite3Res
2a9c4 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2a9c5 28 64 62 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  (db, 0);.  }else
2a9c6 20 69 66 28 20 78 44 65 73 74 72 6f 79 20 29 7b   if( xDestroy ){
2a9c7 0a 20 20 20 20 78 44 65 73 74 72 6f 79 28 70 41  .    xDestroy(pA
2a9c8 75 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ux);.  }.  rc = 
2a9c9 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
2a9ca 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20  b, SQLITE_OK);. 
2a9cb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2a9cc 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2a9cd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2a9ce 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c  ../*.** External
2a9cf 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75 73   API function us
2a9d0 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  ed to create a n
2a9d1 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  ew virtual-table
2a9d2 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   module..*/.SQLI
2a9d3 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2a9d4 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
2a9d5 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2a9d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a9d7 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2a9d8 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20  in which module 
2a9d9 69 73 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f  is registered */
2a9da 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2a9db 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20  Name,           
2a9dc 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67     /* Name assig
2a9dd 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75  ned to this modu
2a9de 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71  le */.  const sq
2a9df 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
2a9e0 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64  odule,  /* The d
2a9e1 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65  efinition of the
2a9e2 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69   module */.  voi
2a9e3 64 20 2a 70 41 75 78 20 20 20 20 20 20 20 20 20  d *pAux         
2a9e4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a9e5 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20  Context pointer 
2a9e6 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
2a9e7 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 72 65 74  nect */.){.  ret
2a9e8 75 72 6e 20 63 72 65 61 74 65 4d 6f 64 75 6c 65  urn createModule
2a9e9 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64  (db, zName, pMod
2a9ea 75 6c 65 2c 20 70 41 75 78 2c 20 30 29 3b 0a 7d  ule, pAux, 0);.}
2a9eb 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c  ../*.** External
2a9ec 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75 73   API function us
2a9ed 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  ed to create a n
2a9ee 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  ew virtual-table
2a9ef 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   module..*/.SQLI
2a9f0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2a9f1 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
2a9f2 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  _v2(.  sqlite3 *
2a9f3 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2a9f4 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2a9f5 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75  se in which modu
2a9f6 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64  le is registered
2a9f7 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2a9f8 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
2a9f9 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73        /* Name as
2a9fa 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d  signed to this m
2a9fb 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  odule */.  const
2a9fc 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
2a9fd 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68  *pModule,  /* Th
2a9fe 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  e definition of 
2a9ff 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
2aa00 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20  void *pAux,     
2aa01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa02 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74  /* Context point
2aa03 65 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78  er for xCreate/x
2aa04 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69  Connect */.  voi
2aa05 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  d (*xDestroy)(vo
2aa06 69 64 20 2a 29 20 20 20 20 20 20 20 20 2f 2a 20  id *)        /* 
2aa07 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f  Module destructo
2aa08 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  r function */.){
2aa09 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 65  .  return create
2aa0a 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d 65  Module(db, zName
2aa0b 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78 2c  , pModule, pAux,
2aa0c 20 78 44 65 73 74 72 6f 79 29 3b 0a 7d 0a 0a 2f   xDestroy);.}../
2aa0d 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 76 69  *.** Lock the vi
2aa0e 72 74 75 61 6c 20 74 61 62 6c 65 20 73 6f 20 74  rtual table so t
2aa0f 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65  hat it cannot be
2aa10 20 64 69 73 63 6f 6e 6e 65 63 74 65 64 2e 0a 2a   disconnected..*
2aa11 2a 20 4c 6f 63 6b 73 20 6e 65 73 74 2e 20 20 45  * Locks nest.  E
2aa12 76 65 72 79 20 6c 6f 63 6b 20 73 68 6f 75 6c 64  very lock should
2aa13 20 68 61 76 65 20 61 20 63 6f 72 72 65 73 70 6f   have a correspo
2aa14 6e 64 69 6e 67 20 75 6e 6c 6f 63 6b 2e 0a 2a 2a  nding unlock..**
2aa15 20 49 66 20 61 6e 20 75 6e 6c 6f 63 6b 20 69 73   If an unlock is
2aa16 20 6f 6d 69 74 74 65 64 2c 20 72 65 73 6f 75 72   omitted, resour
2aa17 63 65 73 20 6c 65 61 6b 73 20 77 69 6c 6c 20 6f  ces leaks will o
2aa18 63 63 75 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66  ccur.  .**.** If
2aa19 20 61 20 64 69 73 63 6f 6e 6e 65 63 74 20 69 73   a disconnect is
2aa1a 20 61 74 74 65 6d 70 74 65 64 20 77 68 69 6c 65   attempted while
2aa1b 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2aa1c 20 69 73 20 6c 6f 63 6b 65 64 2c 0a 2a 2a 20 74   is locked,.** t
2aa1d 68 65 20 64 69 73 63 6f 6e 6e 65 63 74 20 69 73  he disconnect is
2aa1e 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20   deferred until 
2aa1f 61 6c 6c 20 6c 6f 63 6b 73 20 68 61 76 65 20 62  all locks have b
2aa20 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a  een removed..*/.
2aa21 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2aa22 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4c  oid sqlite3VtabL
2aa23 6f 63 6b 28 73 71 6c 69 74 65 33 5f 76 74 61 62  ock(sqlite3_vtab
2aa24 20 2a 70 56 74 61 62 29 7b 0a 20 20 70 56 74 61   *pVtab){.  pVta
2aa25 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a  b->nRef++;.}../*
2aa26 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 61 20 76 69 72  .** Unlock a vir
2aa27 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 57 68 65  tual table.  Whe
2aa28 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20  n the last lock 
2aa29 69 73 20 72 65 6d 6f 76 65 64 2c 0a 2a 2a 20 64  is removed,.** d
2aa2a 69 73 63 6f 6e 6e 65 63 74 20 74 68 65 20 76 69  isconnect the vi
2aa2b 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a  rtual table..*/.
2aa2c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2aa2d 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55  oid sqlite3VtabU
2aa2e 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 20 2a 64  nlock(sqlite3 *d
2aa2f 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  b, sqlite3_vtab 
2aa30 2a 70 56 74 61 62 29 7b 0a 20 20 61 73 73 65 72  *pVtab){.  asser
2aa31 74 28 20 70 56 74 61 62 2d 3e 6e 52 65 66 3e 30  t( pVtab->nRef>0
2aa32 20 29 3b 0a 20 20 70 56 74 61 62 2d 3e 6e 52 65   );.  pVtab->nRe
2aa33 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 64 62  f--;.  assert(db
2aa34 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
2aa35 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f  ite3SafetyCheckO
2aa36 6b 28 64 62 29 20 29 3b 0a 20 20 69 66 28 20 70  k(db) );.  if( p
2aa37 56 74 61 62 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  Vtab->nRef==0 ){
2aa38 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 67  .    if( db->mag
2aa39 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic==SQLITE_MAGIC
2aa3a 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 28  _BUSY ){.      (
2aa3b 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
2aa3c 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
2aa3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d   pVtab->pModule-
2aa3e 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28 70 56 74  >xDisconnect(pVt
2aa3f 61 62 29 3b 0a 20 20 20 20 20 20 28 76 6f 69 64  ab);.      (void
2aa40 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
2aa41 28 64 62 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65  (db);.    } else
2aa42 20 7b 0a 20 20 20 20 20 20 70 56 74 61 62 2d 3e   {.      pVtab->
2aa43 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73 63 6f 6e  pModule->xDiscon
2aa44 6e 65 63 74 28 70 56 74 61 62 29 3b 0a 20 20 20  nect(pVtab);.   
2aa45 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2aa46 43 6c 65 61 72 20 61 6e 79 20 61 6e 64 20 61 6c  Clear any and al
2aa47 6c 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  l virtual-table 
2aa48 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
2aa49 20 74 68 65 20 54 61 62 6c 65 20 72 65 63 6f 72   the Table recor
2aa4a 64 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  d..** This routi
2aa4b 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 66 6f  ne is called, fo
2aa4c 72 20 65 78 61 6d 70 6c 65 2c 20 6a 75 73 74 20  r example, just 
2aa4d 62 65 66 6f 72 65 20 64 65 6c 65 74 69 6e 67 20  before deleting 
2aa4e 74 68 65 20 54 61 62 6c 65 0a 2a 2a 20 72 65 63  the Table.** rec
2aa4f 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ord..*/.SQLITE_P
2aa50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2aa51 74 65 33 56 74 61 62 43 6c 65 61 72 28 54 61 62  te3VtabClear(Tab
2aa52 6c 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  le *p){.  sqlite
2aa53 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20  3_vtab *pVtab = 
2aa54 70 2d 3e 70 56 74 61 62 3b 0a 20 20 53 63 68 65  p->pVtab;.  Sche
2aa55 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d  ma *pSchema = p-
2aa56 3e 70 53 63 68 65 6d 61 3b 0a 20 20 73 71 6c 69  >pSchema;.  sqli
2aa57 74 65 33 20 2a 64 62 20 3d 20 70 53 63 68 65 6d  te3 *db = pSchem
2aa58 61 20 3f 20 70 53 63 68 65 6d 61 2d 3e 64 62 20  a ? pSchema->db 
2aa59 3a 20 30 3b 0a 20 20 69 66 28 20 70 56 74 61 62  : 0;.  if( pVtab
2aa5a 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2aa5b 70 2d 3e 70 4d 6f 64 20 26 26 20 70 2d 3e 70 4d  p->pMod && p->pM
2aa5c 6f 64 2d 3e 70 4d 6f 64 75 6c 65 20 29 3b 0a 20  od->pModule );. 
2aa5d 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e     sqlite3VtabUn
2aa5e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b  lock(db, pVtab);
2aa5f 0a 20 20 20 20 70 2d 3e 70 56 74 61 62 20 3d 20  .    p->pVtab = 
2aa60 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  0;.  }.  if( p->
2aa61 61 7a 4d 6f 64 75 6c 65 41 72 67 20 29 7b 0a 20  azModuleArg ){. 
2aa62 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
2aa63 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d 6f 64  r(i=0; i<p->nMod
2aa64 75 6c 65 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20  uleArg; i++){.  
2aa65 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2aa66 65 28 64 62 2c 20 70 2d 3e 61 7a 4d 6f 64 75 6c  e(db, p->azModul
2aa67 65 41 72 67 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  eArg[i]);.    }.
2aa68 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2aa69 65 28 64 62 2c 20 70 2d 3e 61 7a 4d 6f 64 75 6c  e(db, p->azModul
2aa6a 65 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  eArg);.  }.}../*
2aa6b 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 6d 6f  .** Add a new mo
2aa6c 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 6f  dule argument to
2aa6d 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c   pTable->azModul
2aa6e 65 41 72 67 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73  eArg[]..** The s
2aa6f 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 63 6f 70  tring is not cop
2aa70 69 65 64 20 2d 20 74 68 65 20 70 6f 69 6e 74 65  ied - the pointe
2aa71 72 20 69 73 20 73 74 6f 72 65 64 2e 20 20 54 68  r is stored.  Th
2aa72 65 0a 2a 2a 20 73 74 72 69 6e 67 20 77 69 6c 6c  e.** string will
2aa73 20 62 65 20 66 72 65 65 64 20 61 75 74 6f 6d 61   be freed automa
2aa74 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65  tically when the
2aa75 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 64 65 6c   table is.** del
2aa76 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eted..*/.static 
2aa77 76 6f 69 64 20 61 64 64 4d 6f 64 75 6c 65 41 72  void addModuleAr
2aa78 67 75 6d 65 6e 74 28 73 71 6c 69 74 65 33 20 2a  gument(sqlite3 *
2aa79 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c  db, Table *pTabl
2aa7a 65 2c 20 63 68 61 72 20 2a 7a 41 72 67 29 7b 0a  e, char *zArg){.
2aa7b 20 20 69 6e 74 20 69 20 3d 20 70 54 61 62 6c 65    int i = pTable
2aa7c 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 2b 2b 3b 0a  ->nModuleArg++;.
2aa7d 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73    int nBytes = s
2aa7e 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 28 31  izeof(char *)*(1
2aa7f 2b 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65  +pTable->nModule
2aa80 41 72 67 29 3b 0a 20 20 63 68 61 72 20 2a 2a 61  Arg);.  char **a
2aa81 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 61 7a  zModuleArg;.  az
2aa82 4d 6f 64 75 6c 65 41 72 67 20 3d 20 73 71 6c 69  ModuleArg = sqli
2aa83 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c  te3DbRealloc(db,
2aa84 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c   pTable->azModul
2aa85 65 41 72 67 2c 20 6e 42 79 74 65 73 29 3b 0a 20  eArg, nBytes);. 
2aa86 20 69 66 28 20 61 7a 4d 6f 64 75 6c 65 41 72 67   if( azModuleArg
2aa87 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a  ==0 ){.    int j
2aa88 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  ;.    for(j=0; j
2aa89 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  <i; j++){.      
2aa8a 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2aa8b 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75  , pTable->azModu
2aa8c 6c 65 41 72 67 5b 6a 5d 29 3b 0a 20 20 20 20 7d  leArg[j]);.    }
2aa8d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2aa8e 65 65 28 64 62 2c 20 7a 41 72 67 29 3b 0a 20 20  ee(db, zArg);.  
2aa8f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2aa90 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  db, pTable->azMo
2aa91 64 75 6c 65 41 72 67 29 3b 0a 20 20 20 20 70 54  duleArg);.    pT
2aa92 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67  able->nModuleArg
2aa93 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
2aa94 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69     azModuleArg[i
2aa95 5d 20 3d 20 7a 41 72 67 3b 0a 20 20 20 20 61 7a  ] = zArg;.    az
2aa96 4d 6f 64 75 6c 65 41 72 67 5b 69 2b 31 5d 20 3d  ModuleArg[i+1] =
2aa97 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65   0;.  }.  pTable
2aa98 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 3d 20  ->azModuleArg = 
2aa99 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 7d 0a 0a  azModuleArg;.}..
2aa9a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  /*.** The parser
2aa9b 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2aa9c 69 6e 65 20 77 68 65 6e 20 69 74 20 66 69 72 73  ine when it firs
2aa9d 74 20 73 65 65 73 20 61 20 43 52 45 41 54 45 20  t sees a CREATE 
2aa9e 56 49 52 54 55 41 4c 20 54 41 42 4c 45 0a 2a 2a  VIRTUAL TABLE.**
2aa9f 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
2aaa0 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 68 61 73   module name has
2aaa1 20 62 65 65 6e 20 70 61 72 73 65 64 2c 20 62 75   been parsed, bu
2aaa2 74 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 6c  t the optional l
2aaa3 69 73 74 0a 2a 2a 20 6f 66 20 70 61 72 61 6d 65  ist.** of parame
2aaa4 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ters that follow
2aaa5 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65   the module name
2aaa6 20 61 72 65 20 73 74 69 6c 6c 20 70 65 6e 64 69   are still pendi
2aaa7 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
2aaa8 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2aaa9 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 65  e3VtabBeginParse
2aaaa 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2aaab 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
2aaac 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2aaad 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c    Token *pName1,
2aaae 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2aaaf 6f 66 20 6e 65 77 20 74 61 62 6c 65 2c 20 6f 72  of new table, or
2aab0 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a   database name *
2aab1 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
2aab2 32 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  2,        /* Nam
2aab3 65 20 6f 66 20 6e 65 77 20 74 61 62 6c 65 20 6f  e of new table o
2aab4 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65  r NULL */.  Toke
2aab5 6e 20 2a 70 4d 6f 64 75 6c 65 4e 61 6d 65 20 20  n *pModuleName  
2aab6 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2aab7 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 65 20   module for the 
2aab8 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
2aab9 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
2aaba 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2aabb 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20  he database the 
2aabc 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 63  table is being c
2aabd 72 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 54  reated in */.  T
2aabe 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20  able *pTable;   
2aabf 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
2aac0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
2aac1 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
2aac2 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2aac3 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2aac4 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65  */..  if( pParse
2aac5 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
2aac6 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 65  LITE_SharedCache
2aac7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2aac8 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2aac9 22 43 61 6e 6e 6f 74 20 75 73 65 20 76 69 72 74  "Cannot use virt
2aaca 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 73 68  ual tables in sh
2aacb 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 22  ared-cache mode"
2aacc 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
2aacd 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 53 74 61   }..  sqlite3Sta
2aace 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  rtTable(pParse, 
2aacf 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
2aad0 30 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 70  0, 0, 1, 0);.  p
2aad1 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e  Table = pParse->
2aad2 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28  pNewTable;.  if(
2aad3 20 70 54 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 50   pTable==0 || pP
2aad4 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74  arse->nErr ) ret
2aad5 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 30  urn;.  assert( 0
2aad6 3d 3d 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  ==pTable->pIndex
2aad7 20 29 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72   );..  db = pPar
2aad8 73 65 2d 3e 64 62 3b 0a 20 20 69 44 62 20 3d 20  se->db;.  iDb = 
2aad9 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2aada 6e 64 65 78 28 64 62 2c 20 70 54 61 62 6c 65 2d  ndex(db, pTable-
2aadb 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73  >pSchema);.  ass
2aadc 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a  ert( iDb>=0 );..
2aadd 20 20 70 54 61 62 6c 65 2d 3e 74 61 62 46 6c 61    pTable->tabFla
2aade 67 73 20 7c 3d 20 54 46 5f 56 69 72 74 75 61 6c  gs |= TF_Virtual
2aadf 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64  ;.  pTable->nMod
2aae0 75 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20 61 64  uleArg = 0;.  ad
2aae1 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28  dModuleArgument(
2aae2 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69  db, pTable, sqli
2aae3 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
2aae4 28 64 62 2c 20 70 4d 6f 64 75 6c 65 4e 61 6d 65  (db, pModuleName
2aae5 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41  ));.  addModuleA
2aae6 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62  rgument(db, pTab
2aae7 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72  le, sqlite3DbStr
2aae8 44 75 70 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b  Dup(db, db->aDb[
2aae9 69 44 62 5d 2e 7a 4e 61 6d 65 29 29 3b 0a 20 20  iDb].zName));.  
2aaea 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e  addModuleArgumen
2aaeb 74 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71  t(db, pTable, sq
2aaec 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2aaed 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29  , pTable->zName)
2aaee 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 4e 61  );.  pParse->sNa
2aaef 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e 74  meToken.n = (int
2aaf0 29 28 26 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e  )(&pModuleName->
2aaf1 7a 5b 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e 6e  z[pModuleName->n
2aaf2 5d 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a 29 3b 0a  ] - pName1->z);.
2aaf3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2aaf4 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
2aaf5 4f 4e 0a 20 20 2f 2a 20 43 72 65 61 74 69 6e 67  ON.  /* Creating
2aaf6 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2aaf7 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 61 75 74   invokes the aut
2aaf8 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
2aaf9 61 63 6b 20 74 77 69 63 65 2e 0a 20 20 2a 2a 20  ack twice..  ** 
2aafa 54 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61  The first invoca
2aafb 74 69 6f 6e 2c 20 74 6f 20 6f 62 74 61 69 6e 20  tion, to obtain 
2aafc 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 49 4e  permission to IN
2aafd 53 45 52 54 20 61 20 72 6f 77 20 69 6e 74 6f 20  SERT a row into 
2aafe 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f  the.  ** sqlite_
2aaff 6d 61 73 74 65 72 20 74 61 62 6c 65 2c 20 68 61  master table, ha
2ab00 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d  s already been m
2ab01 61 64 65 20 62 79 20 73 71 6c 69 74 65 33 53 74  ade by sqlite3St
2ab02 61 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 2a 2a  artTable()..  **
2ab03 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c   The second call
2ab04 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d  , to obtain perm
2ab05 69 73 73 69 6f 6e 20 74 6f 20 63 72 65 61 74 65  ission to create
2ab06 20 74 68 65 20 74 61 62 6c 65 2c 20 69 73 20 6d   the table, is m
2ab07 61 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20  ade now..  */.  
2ab08 69 66 28 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  if( pTable->azMo
2ab09 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 73  duleArg ){.    s
2ab0a 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2ab0b 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 43  pParse, SQLITE_C
2ab0c 52 45 41 54 45 5f 56 54 41 42 4c 45 2c 20 70 54  REATE_VTABLE, pT
2ab0d 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20  able->zName, .  
2ab0e 20 20 20 20 20 20 20 20 20 20 70 54 61 62 6c 65            pTable
2ab0f 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
2ab10 2c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  , pParse->db->aD
2ab11 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
2ab12 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
2ab13 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2ab14 74 61 6b 65 73 20 74 68 65 20 6d 6f 64 75 6c 65  takes the module
2ab15 20 61 72 67 75 6d 65 6e 74 20 74 68 61 74 20 68   argument that h
2ab16 61 73 20 62 65 65 6e 20 61 63 63 75 6d 75 6c 61  as been accumula
2ab17 74 69 6e 67 0a 2a 2a 20 69 6e 20 70 50 61 72 73  ting.** in pPars
2ab18 65 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64 20 61 70  e->zArg[] and ap
2ab19 70 65 6e 64 73 20 69 74 20 74 6f 20 74 68 65 20  pends it to the 
2ab1a 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74  list of argument
2ab1b 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 69 72 74  s on the.** virt
2ab1c 75 61 6c 20 74 61 62 6c 65 20 63 75 72 72 65 6e  ual table curren
2ab1d 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
2ab1e 75 63 74 69 6f 6e 20 69 6e 20 70 50 61 72 73 65  uction in pParse
2ab1f 2d 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ->pTable..*/.sta
2ab20 74 69 63 20 76 6f 69 64 20 61 64 64 41 72 67 75  tic void addArgu
2ab21 6d 65 6e 74 54 6f 56 74 61 62 28 50 61 72 73 65  mentToVtab(Parse
2ab22 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 66 28   *pParse){.  if(
2ab23 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20   pParse->sArg.z 
2ab24 26 26 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  && pParse->pNewT
2ab25 61 62 6c 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73  able ){.    cons
2ab26 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e  t char *z = (con
2ab27 73 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d  st char*)pParse-
2ab28 3e 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69 6e 74  >sArg.z;.    int
2ab29 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41 72   n = pParse->sAr
2ab2a 67 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g.n;.    sqlite3
2ab2b 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2ab2c 62 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75 6c 65  b;.    addModule
2ab2d 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 50 61  Argument(db, pPa
2ab2e 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20  rse->pNewTable, 
2ab2f 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
2ab30 28 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20 20 7d  (db, z, n));.  }
2ab31 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  .}../*.** The pa
2ab32 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
2ab33 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 74 68  routine after th
2ab34 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  e CREATE VIRTUAL
2ab35 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
2ab36 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 6d  .** has been com
2ab37 70 6c 65 74 65 6c 79 20 70 61 72 73 65 64 2e 0a  pletely parsed..
2ab38 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2ab39 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2ab3a 61 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61  abFinishParse(Pa
2ab3b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
2ab3c 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54 61 62  en *pEnd){.  Tab
2ab3d 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
2ab3e 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65   /* The table be
2ab3f 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20  ing constructed 
2ab40 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2ab41 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
2ab42 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2ab43 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ion */.  char *z
2ab44 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 2f 2a 20  Module;      /* 
2ab45 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  The module name 
2ab46 6f 66 20 74 68 65 20 74 61 62 6c 65 3a 20 55 53  of the table: US
2ab47 49 4e 47 20 6d 6f 64 75 6c 65 6e 61 6d 65 20 2a  ING modulename *
2ab48 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  /.  Module *pMod
2ab49 20 3d 20 30 3b 0a 0a 20 20 61 64 64 41 72 67 75   = 0;..  addArgu
2ab4a 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73  mentToVtab(pPars
2ab4b 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41  e);.  pParse->sA
2ab4c 72 67 2e 7a 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  rg.z = 0;..  /* 
2ab4d 4c 6f 6f 6b 75 70 20 74 68 65 20 6d 6f 64 75 6c  Lookup the modul
2ab4e 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 70 54 61  e name. */.  pTa
2ab4f 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  b = pParse->pNew
2ab50 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61  Table;.  if( pTa
2ab51 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  b==0 ) return;. 
2ab52 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
2ab53 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 4d  ;.  if( pTab->nM
2ab54 6f 64 75 6c 65 41 72 67 3c 31 20 29 20 72 65 74  oduleArg<1 ) ret
2ab55 75 72 6e 3b 0a 20 20 7a 4d 6f 64 75 6c 65 20 3d  urn;.  zModule =
2ab56 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41   pTab->azModuleA
2ab57 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20  rg[0];.  pMod = 
2ab58 28 4d 6f 64 75 6c 65 2a 29 73 71 6c 69 74 65 33  (Module*)sqlite3
2ab59 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d  HashFind(&db->aM
2ab5a 6f 64 75 6c 65 2c 20 7a 4d 6f 64 75 6c 65 2c 0a  odule, zModule,.
2ab5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ab5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ab5d 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33    sqlite3Strlen3
2ab5e 30 28 7a 4d 6f 64 75 6c 65 29 29 3b 0a 20 20 70  0(zModule));.  p
2ab5f 54 61 62 2d 3e 70 4d 6f 64 20 3d 20 70 4d 6f 64  Tab->pMod = pMod
2ab60 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 74 68 65  ;.  .  /* If the
2ab61 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
2ab62 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
2ab63 69 73 20 62 65 69 6e 67 20 65 6e 74 65 72 65 64  is being entered
2ab64 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 66 69   for the.  ** fi
2ab65 72 73 74 20 74 69 6d 65 20 28 69 6e 20 6f 74 68  rst time (in oth
2ab66 65 72 20 77 6f 72 64 73 20 69 66 20 74 68 65 20  er words if the 
2ab67 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73  virtual table is
2ab68 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 0a   actually being.
2ab69 20 20 2a 2a 20 63 72 65 61 74 65 64 20 6e 6f 77    ** created now
2ab6a 20 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74   instead of just
2ab6b 20 62 65 69 6e 67 20 72 65 61 64 20 6f 75 74 20   being read out 
2ab6c 6f 66 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  of sqlite_master
2ab6d 29 20 74 68 65 6e 0a 20 20 2a 2a 20 64 6f 20 61  ) then.  ** do a
2ab6e 64 64 69 74 69 6f 6e 61 6c 20 69 6e 69 74 69 61  dditional initia
2ab6f 6c 69 7a 61 74 69 6f 6e 20 77 6f 72 6b 20 61 6e  lization work an
2ab70 64 20 73 74 6f 72 65 20 74 68 65 20 73 74 61 74  d store the stat
2ab71 65 6d 65 6e 74 20 74 65 78 74 0a 20 20 2a 2a 20  ement text.  ** 
2ab72 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  in the sqlite_ma
2ab73 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  ster table..  */
2ab74 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74  .  if( !db->init
2ab75 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 63 68 61  .busy ){.    cha
2ab76 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 63 68  r *zStmt;.    ch
2ab77 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20 20 20  ar *zWhere;.    
2ab78 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 56 64 62  int iDb;.    Vdb
2ab79 65 20 2a 76 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f  e *v;..    /* Co
2ab7a 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65  mpute the comple
2ab7b 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43  te text of the C
2ab7c 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
2ab7d 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  BLE statement */
2ab7e 0a 20 20 20 20 69 66 28 20 70 45 6e 64 20 29 7b  .    if( pEnd ){
2ab7f 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 73  .      pParse->s
2ab80 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69  NameToken.n = (i
2ab81 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50  nt)(pEnd->z - pP
2ab82 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
2ab83 2e 7a 29 20 2b 20 70 45 6e 64 2d 3e 6e 3b 0a 20  .z) + pEnd->n;. 
2ab84 20 20 20 7d 0a 20 20 20 20 7a 53 74 6d 74 20 3d     }.    zStmt =
2ab85 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2ab86 64 62 2c 20 22 43 52 45 41 54 45 20 56 49 52 54  db, "CREATE VIRT
2ab87 55 41 4c 20 54 41 42 4c 45 20 25 54 22 2c 20 26  UAL TABLE %T", &
2ab88 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
2ab89 65 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 73  en);..    /* A s
2ab8a 6c 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f  lot for the reco
2ab8b 72 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  rd has already b
2ab8c 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  een allocated in
2ab8d 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c   the .    ** SQL
2ab8e 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65  ITE_MASTER table
2ab8f 2e 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20  .  We just need 
2ab90 74 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73  to update that s
2ab91 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20  lot with all.   
2ab92 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74   ** the informat
2ab93 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63  ion we've collec
2ab94 74 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20  ted.  .    **.  
2ab95 20 20 2a 2a 20 54 68 65 20 56 4d 20 72 65 67 69    ** The VM regi
2ab96 73 74 65 72 20 6e 75 6d 62 65 72 20 70 50 61 72  ster number pPar
2ab97 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 68 6f 6c  se->regRowid hol
2ab98 64 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  ds the rowid of 
2ab99 61 6e 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20  an.    ** entry 
2ab9a 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  in the sqlite_ma
2ab9b 73 74 65 72 20 74 61 62 6c 65 20 74 68 74 20 77  ster table tht w
2ab9c 61 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74  as created for t
2ab9d 68 69 73 20 76 74 61 62 0a 20 20 20 20 2a 2a 20  his vtab.    ** 
2ab9e 62 79 20 73 71 6c 69 74 65 33 53 74 61 72 74 54  by sqlite3StartT
2ab9f 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2f 0a 20  able()..    */. 
2aba0 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
2aba1 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
2aba2 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2aba3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  ;.    sqlite3Nes
2aba4 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
2aba5 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25  .      "UPDATE %
2aba6 51 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20  Q.%s ".         
2aba7 22 53 45 54 20 74 79 70 65 3d 27 74 61 62 6c 65  "SET type='table
2aba8 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f  ', name=%Q, tbl_
2aba9 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67  name=%Q, rootpag
2abaa 65 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20  e=0, sql=%Q ".  
2abab 20 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69       "WHERE rowi
2abac 64 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62  d=#%d",.      db
2abad 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2abae 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
2abaf 44 62 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d  Db),.      pTab-
2abb0 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 54  >zName,.      pT
2abb1 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  ab->zName,.     
2abb2 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50   zStmt,.      pP
2abb3 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20  arse->regRowid. 
2abb4 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65     );.    sqlite
2abb5 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d  3DbFree(db, zStm
2abb6 74 29 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  t);.    v = sqli
2abb7 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2abb8 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  e);.    sqlite3C
2abb9 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72  hangeCookie(pPar
2abba 73 65 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 73  se, iDb);..    s
2abbb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2abbc 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30  (v, OP_Expire, 0
2abbd 2c 20 30 29 3b 0a 20 20 20 20 7a 57 68 65 72 65  , 0);.    zWhere
2abbe 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2abbf 66 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27  f(db, "name='%q'
2abc0 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
2abc1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2abc2 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72  AddOp4(v, OP_Par
2abc3 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 31  seSchema, iDb, 1
2abc4 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f  , 0, zWhere, P4_
2abc5 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 73 71  DYNAMIC);.    sq
2abc6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2abc7 76 2c 20 4f 50 5f 56 43 72 65 61 74 65 2c 20 69  v, OP_VCreate, i
2abc8 44 62 2c 20 30 2c 20 30 2c 20 0a 20 20 20 20 20  Db, 0, 0, .     
2abc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2abca 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
2abcb 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2abcc 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20 2b 20  (pTab->zName) + 
2abcd 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  1);.  }..  /* If
2abce 20 77 65 20 61 72 65 20 72 65 72 65 61 64 69 6e   we are rereadin
2abcf 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  g the sqlite_mas
2abd0 74 65 72 20 74 61 62 6c 65 20 63 72 65 61 74 65  ter table create
2abd1 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20   the in-memory. 
2abd2 20 2a 2a 20 72 65 63 6f 72 64 20 6f 66 20 74 68   ** record of th
2abd3 65 20 74 61 62 6c 65 2e 20 49 66 20 74 68 65 20  e table. If the 
2abd4 6d 6f 64 75 6c 65 20 68 61 73 20 61 6c 72 65 61  module has alrea
2abd5 64 79 20 62 65 65 6e 20 72 65 67 69 73 74 65 72  dy been register
2abd6 65 64 2c 0a 20 20 2a 2a 20 61 6c 73 6f 20 63 61  ed,.  ** also ca
2abd7 6c 6c 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 20  ll the xConnect 
2abd8 6d 65 74 68 6f 64 20 68 65 72 65 2e 0a 20 20 2a  method here..  *
2abd9 2f 0a 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 54  /.  else {.    T
2abda 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20  able *pOld;.    
2abdb 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20  Schema *pSchema 
2abdc 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b  = pTab->pSchema;
2abdd 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2abde 2a 7a 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a  *zName = pTab->z
2abdf 4e 61 6d 65 3b 0a 20 20 20 20 69 6e 74 20 6e 4e  Name;.    int nN
2abe0 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
2abe1 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20  len30(zName);.  
2abe2 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33    pOld = sqlite3
2abe3 48 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68  HashInsert(&pSch
2abe4 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e  ema->tblHash, zN
2abe5 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 54 61 62  ame, nName, pTab
2abe6 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 20  );.    if( pOld 
2abe7 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
2abe8 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2abe9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61       assert( pTa
2abea 62 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d  b==pOld );  /* M
2abeb 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20  alloc must have 
2abec 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 48 61  failed inside Ha
2abed 73 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20 20  shInsert() */.  
2abee 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2abef 7d 0a 20 20 20 20 70 53 63 68 65 6d 61 2d 3e 64  }.    pSchema->d
2abf0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2abf1 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77      pParse->pNew
2abf2 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d  Table = 0;.  }.}
2abf3 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73  ../*.** The pars
2abf4 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
2abf5 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65  utine when it se
2abf6 65 73 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b  es the first tok
2abf7 65 6e 0a 2a 2a 20 6f 66 20 61 6e 20 61 72 67 75  en.** of an argu
2abf8 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64 75  ment to the modu
2abf9 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20 43 52 45  le name in a CRE
2abfa 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
2abfb 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a  E statement..*/.
2abfc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2abfd 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 41  oid sqlite3VtabA
2abfe 72 67 49 6e 69 74 28 50 61 72 73 65 20 2a 70 50  rgInit(Parse *pP
2abff 61 72 73 65 29 7b 0a 20 20 61 64 64 41 72 67 75  arse){.  addArgu
2ac00 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73  mentToVtab(pPars
2ac01 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41  e);.  pParse->sA
2ac02 72 67 2e 7a 20 3d 20 30 3b 0a 20 20 70 50 61 72  rg.z = 0;.  pPar
2ac03 73 65 2d 3e 73 41 72 67 2e 6e 20 3d 20 30 3b 0a  se->sArg.n = 0;.
2ac04 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72  }../*.** The par
2ac05 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
2ac06 6f 75 74 69 6e 65 20 66 6f 72 20 65 61 63 68 20  outine for each 
2ac07 74 6f 6b 65 6e 20 61 66 74 65 72 20 74 68 65 20  token after the 
2ac08 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69  first token.** i
2ac09 6e 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f  n an argument to
2ac0a 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65   the module name
2ac0b 20 69 6e 20 61 20 43 52 45 41 54 45 20 56 49 52   in a CREATE VIR
2ac0c 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65  TUAL TABLE state
2ac0d 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
2ac0e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2ac0f 69 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e  ite3VtabArgExten
2ac10 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  d(Parse *pParse,
2ac11 20 54 6f 6b 65 6e 20 2a 70 29 7b 0a 20 20 54 6f   Token *p){.  To
2ac12 6b 65 6e 20 2a 70 41 72 67 20 3d 20 26 70 50 61  ken *pArg = &pPa
2ac13 72 73 65 2d 3e 73 41 72 67 3b 0a 20 20 69 66 28  rse->sArg;.  if(
2ac14 20 70 41 72 67 2d 3e 7a 3d 3d 30 20 29 7b 0a 20   pArg->z==0 ){. 
2ac15 20 20 20 70 41 72 67 2d 3e 7a 20 3d 20 70 2d 3e     pArg->z = p->
2ac16 7a 3b 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20 3d  z;.    pArg->n =
2ac17 20 70 2d 3e 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a   p->n;.  }else{.
2ac18 20 20 20 20 61 73 73 65 72 74 28 70 41 72 67 2d      assert(pArg-
2ac19 3e 7a 20 3c 20 70 2d 3e 7a 29 3b 0a 20 20 20 20  >z < p->z);.    
2ac1a 70 41 72 67 2d 3e 6e 20 3d 20 28 69 6e 74 29 28  pArg->n = (int)(
2ac1b 26 70 2d 3e 7a 5b 70 2d 3e 6e 5d 20 2d 20 70 41  &p->z[p->n] - pA
2ac1c 72 67 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  rg->z);.  }.}../
2ac1d 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 76 69  *.** Invoke a vi
2ac1e 72 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6e 73  rtual table cons
2ac1f 74 72 75 63 74 6f 72 20 28 65 69 74 68 65 72 20  tructor (either 
2ac20 78 43 72 65 61 74 65 20 6f 72 20 78 43 6f 6e 6e  xCreate or xConn
2ac21 65 63 74 29 2e 20 54 68 65 0a 2a 2a 20 70 6f 69  ect). The.** poi
2ac22 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63  nter to the func
2ac23 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 69  tion to invoke i
2ac24 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
2ac25 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
2ac26 0a 2a 2a 20 74 6f 20 74 68 69 73 20 70 72 6f 63  .** to this proc
2ac27 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  edure..*/.static
2ac28 20 69 6e 74 20 76 74 61 62 43 61 6c 6c 43 6f 6e   int vtabCallCon
2ac29 73 74 72 75 63 74 6f 72 28 0a 20 20 73 71 6c 69  structor(.  sqli
2ac2a 74 65 33 20 2a 64 62 2c 20 0a 20 20 54 61 62 6c  te3 *db, .  Tabl
2ac2b 65 20 2a 70 54 61 62 2c 0a 20 20 4d 6f 64 75 6c  e *pTab,.  Modul
2ac2c 65 20 2a 70 4d 6f 64 2c 0a 20 20 69 6e 74 20 28  e *pMod,.  int (
2ac2d 2a 78 43 6f 6e 73 74 72 75 63 74 29 28 73 71 6c  *xConstruct)(sql
2ac2e 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c  ite3*,void*,int,
2ac2f 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e 73 74  const char*const
2ac30 2a 2c 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 2a  *,sqlite3_vtab**
2ac31 2c 63 68 61 72 2a 2a 29 2c 0a 20 20 63 68 61 72  ,char**),.  char
2ac32 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 69 6e   **pzErr.){.  in
2ac33 74 20 72 63 3b 0a 20 20 69 6e 74 20 72 63 32 3b  t rc;.  int rc2;
2ac34 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
2ac35 2a 70 56 74 61 62 20 3d 20 30 3b 0a 20 20 63 6f  *pVtab = 0;.  co
2ac36 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
2ac37 61 7a 41 72 67 20 3d 20 28 63 6f 6e 73 74 20 63  azArg = (const c
2ac38 68 61 72 20 2a 63 6f 6e 73 74 2a 29 70 54 61 62  har *const*)pTab
2ac39 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20  ->azModuleArg;. 
2ac3a 20 69 6e 74 20 6e 41 72 67 20 3d 20 70 54 61 62   int nArg = pTab
2ac3b 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20  ->nModuleArg;.  
2ac3c 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a  char *zErr = 0;.
2ac3d 20 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 4e    char *zModuleN
2ac3e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
2ac3f 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 20 70  intf(db, "%s", p
2ac40 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20  Tab->zName);..  
2ac41 69 66 28 20 21 7a 4d 6f 64 75 6c 65 4e 61 6d 65  if( !zModuleName
2ac42 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2ac43 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
2ac44 0a 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ..  assert( !db-
2ac45 3e 70 56 54 61 62 20 29 3b 0a 20 20 61 73 73 65  >pVTab );.  asse
2ac46 72 74 28 20 78 43 6f 6e 73 74 72 75 63 74 20 29  rt( xConstruct )
2ac47 3b 0a 0a 20 20 64 62 2d 3e 70 56 54 61 62 20 3d  ;..  db->pVTab =
2ac48 20 70 54 61 62 3b 0a 20 20 72 63 20 3d 20 73 71   pTab;.  rc = sq
2ac49 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
2ac4a 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  b);.  assert( rc
2ac4b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
2ac4c 20 72 63 20 3d 20 78 43 6f 6e 73 74 72 75 63 74   rc = xConstruct
2ac4d 28 64 62 2c 20 70 4d 6f 64 2d 3e 70 41 75 78 2c  (db, pMod->pAux,
2ac4e 20 6e 41 72 67 2c 20 61 7a 41 72 67 2c 20 26 70   nArg, azArg, &p
2ac4f 56 74 61 62 2c 20 26 7a 45 72 72 29 3b 0a 20 20  Vtab, &zErr);.  
2ac50 72 63 32 20 3d 20 73 71 6c 69 74 65 33 53 61 66  rc2 = sqlite3Saf
2ac51 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 69 66 28  etyOn(db);.  if(
2ac52 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
2ac53 26 20 70 56 74 61 62 20 29 7b 0a 20 20 20 20 70  & pVtab ){.    p
2ac54 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20  Vtab->pModule = 
2ac55 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  pMod->pModule;. 
2ac56 20 20 20 70 56 74 61 62 2d 3e 6e 52 65 66 20 3d     pVtab->nRef =
2ac57 20 31 3b 0a 20 20 20 20 70 54 61 62 2d 3e 70 56   1;.    pTab->pV
2ac58 74 61 62 20 3d 20 70 56 74 61 62 3b 0a 20 20 7d  tab = pVtab;.  }
2ac59 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
2ac5a 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 69 66 28  K!=rc ){.    if(
2ac5b 20 7a 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20   zErr==0 ){.    
2ac5c 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
2ac5d 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 76  e3MPrintf(db, "v
2ac5e 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f  table constructo
2ac5f 72 20 66 61 69 6c 65 64 3a 20 25 73 22 2c 20 7a  r failed: %s", z
2ac60 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 20 20 20  ModuleName);.   
2ac61 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 2a   }else {.      *
2ac62 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d  pzErr = sqlite3M
2ac63 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c  Printf(db, "%s",
2ac64 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71   zErr);.      sq
2ac65 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2ac66 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  zErr);.    }.  }
2ac67 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 70 56 54  else if( db->pVT
2ac68 61 62 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  ab ){.    const 
2ac69 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20  char *zFormat = 
2ac6a 22 76 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63  "vtable construc
2ac6b 74 6f 72 20 64 69 64 20 6e 6f 74 20 64 65 63 6c  tor did not decl
2ac6c 61 72 65 20 73 63 68 65 6d 61 3a 20 25 73 22 3b  are schema: %s";
2ac6d 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71  .    *pzErr = sq
2ac6e 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2ac6f 20 7a 46 6f 72 6d 61 74 2c 20 70 54 61 62 2d 3e   zFormat, pTab->
2ac70 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d  zName);.    rc =
2ac71 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2ac72 20 7d 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   } .  if( rc==SQ
2ac73 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
2ac74 63 20 3d 20 72 63 32 3b 0a 20 20 7d 0a 20 20 64  c = rc2;.  }.  d
2ac75 62 2d 3e 70 56 54 61 62 20 3d 20 30 3b 0a 20 20  b->pVTab = 0;.  
2ac76 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2ac77 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a  , zModuleName);.
2ac78 0a 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74 68  .  /* If everyth
2ac79 69 6e 67 20 77 65 6e 74 20 61 63 63 6f 72 64 69  ing went accordi
2ac7a 6e 67 20 74 6f 20 70 6c 61 6e 2c 20 6c 6f 6f 70  ng to plan, loop
2ac7b 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f 6c   through the col
2ac7c 75 6d 6e 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65  umns.  ** of the
2ac7d 20 74 61 62 6c 65 20 74 6f 20 73 65 65 20 69 66   table to see if
2ac7e 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 63 6f 6e   any of them con
2ac7f 74 61 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20 22  tain the token "
2ac80 68 69 64 64 65 6e 22 2e 0a 20 20 2a 2a 20 49 66  hidden"..  ** If
2ac81 20 73 6f 2c 20 73 65 74 20 74 68 65 20 43 6f 6c   so, set the Col
2ac82 75 6d 6e 2e 69 73 48 69 64 64 65 6e 20 66 6c 61  umn.isHidden fla
2ac83 67 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65  g and remove the
2ac84 20 74 6f 6b 65 6e 20 66 72 6f 6d 0a 20 20 2a 2a   token from.  **
2ac85 20 74 68 65 20 74 79 70 65 20 73 74 72 69 6e 67   the type string
2ac86 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  ..  */.  if( rc=
2ac87 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2ac88 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20    int iCol;.    
2ac89 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c  for(iCol=0; iCol
2ac8a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f  <pTab->nCol; iCo
2ac8b 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72  l++){.      char
2ac8c 20 2a 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e   *zType = pTab->
2ac8d 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65  aCol[iCol].zType
2ac8e 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 79 70  ;.      int nTyp
2ac8f 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 20 3d  e;.      int i =
2ac90 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a   0;.      if( !z
2ac91 54 79 70 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  Type ) continue;
2ac92 0a 20 20 20 20 20 20 6e 54 79 70 65 20 3d 20 73  .      nType = s
2ac93 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2ac94 54 79 70 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Type);.      if(
2ac95 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
2ac96 28 22 68 69 64 64 65 6e 22 2c 20 7a 54 79 70 65  ("hidden", zType
2ac97 2c 20 36 29 20 7c 7c 20 28 7a 54 79 70 65 5b 36  , 6) || (zType[6
2ac98 5d 20 26 26 20 7a 54 79 70 65 5b 36 5d 21 3d 27  ] && zType[6]!='
2ac99 20 27 29 20 29 7b 0a 20 20 20 20 20 20 20 20 66   ') ){.        f
2ac9a 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 79 70 65 3b  or(i=0; i<nType;
2ac9b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
2ac9c 20 69 66 28 20 28 30 3d 3d 73 71 6c 69 74 65 33   if( (0==sqlite3
2ac9d 53 74 72 4e 49 43 6d 70 28 22 20 68 69 64 64 65  StrNICmp(" hidde
2ac9e 6e 22 2c 20 26 7a 54 79 70 65 5b 69 5d 2c 20 37  n", &zType[i], 7
2ac9f 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26  )).           &&
2aca0 20 28 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d 27 5c   (zType[i+7]=='\
2aca1 30 27 20 7c 7c 20 7a 54 79 70 65 5b 69 2b 37 5d  0' || zType[i+7]
2aca2 3d 3d 27 20 27 29 0a 20 20 20 20 20 20 20 20 20  ==' ').         
2aca3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2aca4 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  i++;.           
2aca5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2aca6 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
2aca7 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2aca8 69 3c 6e 54 79 70 65 20 29 7b 0a 20 20 20 20 20  i<nType ){.     
2aca9 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
2acaa 20 20 69 6e 74 20 6e 44 65 6c 20 3d 20 36 20 2b    int nDel = 6 +
2acab 20 28 7a 54 79 70 65 5b 69 2b 36 5d 20 3f 20 31   (zType[i+6] ? 1
2acac 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 66   : 0);.        f
2acad 6f 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44 65 6c 29  or(j=i; (j+nDel)
2acae 3c 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29 7b 0a 20  <=nType; j++){. 
2acaf 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 5b 6a           zType[j
2acb0 5d 20 3d 20 7a 54 79 70 65 5b 6a 2b 6e 44 65 6c  ] = zType[j+nDel
2acb1 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ];.        }.   
2acb2 20 20 20 20 20 69 66 28 20 7a 54 79 70 65 5b 69       if( zType[i
2acb3 5d 3d 3d 27 5c 30 27 20 26 26 20 69 3e 30 20 29  ]=='\0' && i>0 )
2acb4 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
2acb5 72 74 28 7a 54 79 70 65 5b 69 2d 31 5d 3d 3d 27  rt(zType[i-1]=='
2acb6 20 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7a   ');.          z
2acb7 54 79 70 65 5b 69 2d 31 5d 20 3d 20 27 5c 30 27  Type[i-1] = '\0'
2acb8 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2acb9 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69      pTab->aCol[i
2acba 43 6f 6c 5d 2e 69 73 48 69 64 64 65 6e 20 3d 20  Col].isHidden = 
2acbb 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
2acbc 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
2acbd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
2acbe 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f  function is invo
2acbf 6b 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  ked by the parse
2acc0 72 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 43  r to call the xC
2acc1 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 0a  onnect() method.
2acc2 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  ** of the virtua
2acc3 6c 20 74 61 62 6c 65 20 70 54 61 62 2e 20 49 66  l table pTab. If
2acc4 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
2acc5 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  , an error code 
2acc6 69 73 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20  is returned .** 
2acc7 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6c 65 66  and an error lef
2acc8 74 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2a 0a  t in pParse..**.
2acc9 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20  ** This call is 
2acca 61 20 6e 6f 2d 6f 70 20 69 66 20 74 61 62 6c 65  a no-op if table
2accb 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20 76   pTab is not a v
2accc 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f  irtual table..*/
2accd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2acce 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43  int sqlite3VtabC
2accf 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65  allConnect(Parse
2acd0 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20   *pParse, Table 
2acd1 2a 70 54 61 62 29 7b 0a 20 20 4d 6f 64 75 6c 65  *pTab){.  Module
2acd2 20 2a 70 4d 6f 64 3b 0a 20 20 69 6e 74 20 72 63   *pMod;.  int rc
2acd3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
2acd4 20 69 66 28 20 21 70 54 61 62 20 7c 7c 20 28 70   if( !pTab || (p
2acd5 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
2acd6 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 20 7c  TF_Virtual)==0 |
2acd7 7c 20 70 54 61 62 2d 3e 70 56 74 61 62 20 29 7b  | pTab->pVtab ){
2acd8 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2acd9 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 4d  TE_OK;.  }..  pM
2acda 6f 64 20 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 3b  od = pTab->pMod;
2acdb 0a 20 20 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a  .  if( !pMod ){.
2acdc 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2acdd 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62 2d 3e  zModule = pTab->
2acde 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a  azModuleArg[0];.
2acdf 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2ace0 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
2ace1 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22  such module: %s"
2ace2 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20  , zModule);.    
2ace3 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
2ace4 52 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  R;.  } else {.  
2ace5 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30    char *zErr = 0
2ace6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  ;.    sqlite3 *d
2ace7 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2ace8 20 20 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c      rc = vtabCal
2ace9 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c  lConstructor(db,
2acea 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f   pTab, pMod, pMo
2aceb 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6e  d->pModule->xCon
2acec 6e 65 63 74 2c 20 26 7a 45 72 72 29 3b 0a 20 20  nect, &zErr);.  
2aced 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2acee 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
2acef 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2acf0 72 73 65 2c 20 22 25 73 22 2c 20 7a 45 72 72 29  rse, "%s", zErr)
2acf1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2acf2 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45  te3DbFree(db, zE
2acf3 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  rr);.  }..  retu
2acf4 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
2acf5 41 64 64 20 74 68 65 20 76 69 72 74 75 61 6c 20  Add the virtual 
2acf6 74 61 62 6c 65 20 70 56 74 61 62 20 74 6f 20 74  table pVtab to t
2acf7 68 65 20 61 72 72 61 79 20 73 71 6c 69 74 65 33  he array sqlite3
2acf8 2e 61 56 54 72 61 6e 73 5b 5d 2e 0a 2a 2f 0a 73  .aVTrans[]..*/.s
2acf9 74 61 74 69 63 20 69 6e 74 20 61 64 64 54 6f 56  tatic int addToV
2acfa 54 72 61 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  Trans(sqlite3 *d
2acfb 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  b, sqlite3_vtab 
2acfc 2a 70 56 74 61 62 29 7b 0a 20 20 63 6f 6e 73 74  *pVtab){.  const
2acfd 20 69 6e 74 20 41 52 52 41 59 5f 49 4e 43 52 20   int ARRAY_INCR 
2acfe 3d 20 35 3b 0a 0a 20 20 2f 2a 20 47 72 6f 77 20  = 5;..  /* Grow 
2acff 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72  the sqlite3.aVTr
2ad00 61 6e 73 20 61 72 72 61 79 20 69 66 20 72 65 71  ans array if req
2ad01 75 69 72 65 64 20 2a 2f 0a 20 20 69 66 28 20 28  uired */.  if( (
2ad02 64 62 2d 3e 6e 56 54 72 61 6e 73 25 41 52 52 41  db->nVTrans%ARRA
2ad03 59 5f 49 4e 43 52 29 3d 3d 30 20 29 7b 0a 20 20  Y_INCR)==0 ){.  
2ad04 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
2ad05 2a 61 56 54 72 61 6e 73 3b 0a 20 20 20 20 69 6e  *aVTrans;.    in
2ad06 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f  t nBytes = sizeo
2ad07 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  f(sqlite3_vtab *
2ad08 29 20 2a 20 28 64 62 2d 3e 6e 56 54 72 61 6e 73  ) * (db->nVTrans
2ad09 20 2b 20 41 52 52 41 59 5f 49 4e 43 52 29 3b 0a   + ARRAY_INCR);.
2ad0a 20 20 20 20 61 56 54 72 61 6e 73 20 3d 20 73 71      aVTrans = sq
2ad0b 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64  lite3DbRealloc(d
2ad0c 62 2c 20 28 76 6f 69 64 20 2a 29 64 62 2d 3e 61  b, (void *)db->a
2ad0d 56 54 72 61 6e 73 2c 20 6e 42 79 74 65 73 29 3b  VTrans, nBytes);
2ad0e 0a 20 20 20 20 69 66 28 20 21 61 56 54 72 61 6e  .    if( !aVTran
2ad0f 73 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  s ){.      retur
2ad10 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
2ad11 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74      }.    memset
2ad12 28 26 61 56 54 72 61 6e 73 5b 64 62 2d 3e 6e 56  (&aVTrans[db->nV
2ad13 54 72 61 6e 73 5d 2c 20 30 2c 20 73 69 7a 65 6f  Trans], 0, sizeo
2ad14 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  f(sqlite3_vtab *
2ad15 29 2a 41 52 52 41 59 5f 49 4e 43 52 29 3b 0a 20  )*ARRAY_INCR);. 
2ad16 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d     db->aVTrans =
2ad17 20 61 56 54 72 61 6e 73 3b 0a 20 20 7d 0a 0a 20   aVTrans;.  }.. 
2ad18 20 2f 2a 20 41 64 64 20 70 56 74 61 62 20 74 6f   /* Add pVtab to
2ad19 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71 6c 69   the end of sqli
2ad1a 74 65 33 2e 61 56 54 72 61 6e 73 20 2a 2f 0a 20  te3.aVTrans */. 
2ad1b 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 64 62 2d   db->aVTrans[db-
2ad1c 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d 20 70 56  >nVTrans++] = pV
2ad1d 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 74  tab;.  sqlite3Vt
2ad1e 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20  abLock(pVtab);. 
2ad1f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2ad20 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  K;.}../*.** This
2ad21 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76   function is inv
2ad22 6f 6b 65 64 20 62 79 20 74 68 65 20 76 64 62 65  oked by the vdbe
2ad23 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 43 72   to call the xCr
2ad24 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f  eate method.** o
2ad25 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2ad26 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62 20 69  ble named zTab i
2ad27 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20  n database iDb. 
2ad28 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
2ad29 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 7a 45 72  or occurs, *pzEr
2ad2a 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  r is set to poin
2ad2b 74 20 61 6e 20 61 6e 20 45 6e 67 6c 69 73 68 20  t an an English 
2ad2c 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 64 65 73 63  language.** desc
2ad2d 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 65  ription of the e
2ad2e 72 72 6f 72 20 61 6e 64 20 61 6e 20 53 51 4c 49  rror and an SQLI
2ad2f 54 45 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64  TE_XXX error cod
2ad30 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
2ad31 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  * In this case t
2ad32 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63  he caller must c
2ad33 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65  all sqlite3DbFre
2ad34 65 28 64 62 2c 20 29 20 6f 6e 20 2a 70 7a 45 72  e(db, ) on *pzEr
2ad35 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
2ad36 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2ad37 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 73  VtabCallCreate(s
2ad38 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2ad39 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  iDb, const char 
2ad3a 2a 7a 54 61 62 2c 20 63 68 61 72 20 2a 2a 70 7a  *zTab, char **pz
2ad3b 45 72 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Err){.  int rc =
2ad3c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54 61   SQLITE_OK;.  Ta
2ad3d 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 4d 6f 64  ble *pTab;.  Mod
2ad3e 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 63 6f 6e  ule *pMod;.  con
2ad3f 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c 65  st char *zModule
2ad40 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  ;..  pTab = sqli
2ad41 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
2ad42 20 7a 54 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69   zTab, db->aDb[i
2ad43 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 61 73  Db].zName);.  as
2ad44 73 65 72 74 28 70 54 61 62 20 26 26 20 28 70 54  sert(pTab && (pT
2ad45 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
2ad46 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 20 26 26  F_Virtual)!=0 &&
2ad47 20 21 70 54 61 62 2d 3e 70 56 74 61 62 29 3b 0a   !pTab->pVtab);.
2ad48 20 20 70 4d 6f 64 20 3d 20 70 54 61 62 2d 3e 70    pMod = pTab->p
2ad49 4d 6f 64 3b 0a 20 20 7a 4d 6f 64 75 6c 65 20 3d  Mod;.  zModule =
2ad4a 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41   pTab->azModuleA
2ad4b 72 67 5b 30 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20  rg[0];..  /* If 
2ad4c 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 62  the module has b
2ad4d 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 20 61  een registered a
2ad4e 6e 64 20 69 6e 63 6c 75 64 65 73 20 61 20 43 72  nd includes a Cr
2ad4f 65 61 74 65 20 6d 65 74 68 6f 64 2c 20 0a 20 20  eate method, .  
2ad50 2a 2a 20 69 6e 76 6f 6b 65 20 69 74 20 6e 6f 77  ** invoke it now
2ad51 2e 20 49 66 20 74 68 65 20 6d 6f 64 75 6c 65 20  . If the module 
2ad52 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 72 65 67  has not been reg
2ad53 69 73 74 65 72 65 64 2c 20 72 65 74 75 72 6e 20  istered, return 
2ad54 61 6e 20 0a 20 20 2a 2a 20 65 72 72 6f 72 2e 20  an .  ** error. 
2ad55 4f 74 68 65 72 77 69 73 65 2c 20 64 6f 20 6e 6f  Otherwise, do no
2ad56 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66  thing..  */.  if
2ad57 28 20 21 70 4d 6f 64 20 29 7b 0a 20 20 20 20 2a  ( !pMod ){.    *
2ad58 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d  pzErr = sqlite3M
2ad59 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73  Printf(db, "no s
2ad5a 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22 2c  uch module: %s",
2ad5b 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 72   zModule);.    r
2ad5c 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
2ad5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
2ad5e 63 20 3d 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73  c = vtabCallCons
2ad5f 74 72 75 63 74 6f 72 28 64 62 2c 20 70 54 61 62  tructor(db, pTab
2ad60 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d 3e 70 4d  , pMod, pMod->pM
2ad61 6f 64 75 6c 65 2d 3e 78 43 72 65 61 74 65 2c 20  odule->xCreate, 
2ad62 70 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 69  pzErr);.  }..  i
2ad63 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2ad64 20 26 26 20 70 54 61 62 2d 3e 70 56 74 61 62 20   && pTab->pVtab 
2ad65 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 64  ){.      rc = ad
2ad66 64 54 6f 56 54 72 61 6e 73 28 64 62 2c 20 70 54  dToVTrans(db, pT
2ad67 61 62 2d 3e 70 56 74 61 62 29 3b 0a 20 20 7d 0a  ab->pVtab);.  }.
2ad68 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2ad69 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
2ad6a 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
2ad6b 73 65 74 20 74 68 65 20 73 63 68 65 6d 61 20 6f  set the schema o
2ad6c 66 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  f a virtual tabl
2ad6d 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a  e.  It is only.*
2ad6e 2a 20 76 61 6c 69 64 20 74 6f 20 63 61 6c 6c 20  * valid to call 
2ad6f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 72  this function fr
2ad70 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 78 43  om within the xC
2ad71 72 65 61 74 65 28 29 20 6f 72 20 78 43 6f 6e 6e  reate() or xConn
2ad72 65 63 74 28 29 20 6f 66 20 61 0a 2a 2a 20 76 69  ect() of a.** vi
2ad73 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
2ad74 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
2ad75 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65  I int sqlite3_de
2ad76 63 6c 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74  clare_vtab(sqlit
2ad77 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
2ad78 61 72 20 2a 7a 43 72 65 61 74 65 54 61 62 6c 65  ar *zCreateTable
2ad79 29 7b 0a 20 20 50 61 72 73 65 20 73 50 61 72 73  ){.  Parse sPars
2ad7a 65 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e;..  int rc = S
2ad7b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c  QLITE_OK;.  Tabl
2ad7c 65 20 2a 70 54 61 62 3b 0a 20 20 63 68 61 72 20  e *pTab;.  char 
2ad7d 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20 20 73 71  *zErr = 0;..  sq
2ad7e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
2ad7f 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
2ad80 70 54 61 62 20 3d 20 64 62 2d 3e 70 56 54 61 62  pTab = db->pVTab
2ad81 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 7b  ;.  if( !pTab ){
2ad82 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2ad83 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53  r(db, SQLITE_MIS
2ad84 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  USE, 0);.    sql
2ad85 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2ad86 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  (db->mutex);.   
2ad87 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
2ad88 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 61 73 73  ISUSE;.  }.  ass
2ad89 65 72 74 28 28 70 54 61 62 2d 3e 74 61 62 46 6c  ert((pTab->tabFl
2ad8a 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c  ags & TF_Virtual
2ad8b 29 21 3d 30 20 26 26 20 70 54 61 62 2d 3e 6e 43  )!=0 && pTab->nC
2ad8c 6f 6c 3d 3d 30 20 26 26 20 70 54 61 62 2d 3e 61  ol==0 && pTab->a
2ad8d 43 6f 6c 3d 3d 30 29 3b 0a 0a 20 20 6d 65 6d 73  Col==0);..  mems
2ad8e 65 74 28 26 73 50 61 72 73 65 2c 20 30 2c 20 73  et(&sParse, 0, s
2ad8f 69 7a 65 6f 66 28 50 61 72 73 65 29 29 3b 0a 20  izeof(Parse));. 
2ad90 20 73 50 61 72 73 65 2e 64 65 63 6c 61 72 65 56   sParse.declareV
2ad91 74 61 62 20 3d 20 31 3b 0a 20 20 73 50 61 72 73  tab = 1;.  sPars
2ad92 65 2e 64 62 20 3d 20 64 62 3b 0a 0a 20 20 69 66  e.db = db;..  if
2ad93 28 20 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ( .      SQLITE_
2ad94 4f 4b 20 3d 3d 20 73 71 6c 69 74 65 33 52 75 6e  OK == sqlite3Run
2ad95 50 61 72 73 65 72 28 26 73 50 61 72 73 65 2c 20  Parser(&sParse, 
2ad96 7a 43 72 65 61 74 65 54 61 62 6c 65 2c 20 26 7a  zCreateTable, &z
2ad97 45 72 72 29 20 26 26 20 0a 20 20 20 20 20 20 73  Err) && .      s
2ad98 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 20  Parse.pNewTable 
2ad99 26 26 20 0a 20 20 20 20 20 20 21 73 50 61 72 73  && .      !sPars
2ad9a 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 70 53 65  e.pNewTable->pSe
2ad9b 6c 65 63 74 20 26 26 20 0a 20 20 20 20 20 20 28  lect && .      (
2ad9c 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65  sParse.pNewTable
2ad9d 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
2ad9e 56 69 72 74 75 61 6c 29 3d 3d 30 0a 20 20 29 7b  Virtual)==0.  ){
2ad9f 0a 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 20  .    pTab->aCol 
2ada0 3d 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62  = sParse.pNewTab
2ada1 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 70 54  le->aCol;.    pT
2ada2 61 62 2d 3e 6e 43 6f 6c 20 3d 20 73 50 61 72 73  ab->nCol = sPars
2ada3 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43 6f  e.pNewTable->nCo
2ada4 6c 3b 0a 20 20 20 20 73 50 61 72 73 65 2e 70 4e  l;.    sParse.pN
2ada5 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20  ewTable->nCol = 
2ada6 30 3b 0a 20 20 20 20 73 50 61 72 73 65 2e 70 4e  0;.    sParse.pN
2ada7 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20  ewTable->aCol = 
2ada8 30 3b 0a 20 20 20 20 64 62 2d 3e 70 56 54 61 62  0;.    db->pVTab
2ada9 20 3d 20 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b   = 0;.  } else {
2adaa 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2adab 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 52 52  r(db, SQLITE_ERR
2adac 4f 52 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 73  OR, zErr);.    s
2adad 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2adae 20 7a 45 72 72 29 3b 0a 20 20 20 20 72 63 20 3d   zErr);.    rc =
2adaf 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2adb0 20 7d 0a 20 20 73 50 61 72 73 65 2e 64 65 63 6c   }.  sParse.decl
2adb1 61 72 65 56 74 61 62 20 3d 20 30 3b 0a 0a 20 20  areVtab = 0;..  
2adb2 69 66 28 20 73 50 61 72 73 65 2e 70 56 64 62 65  if( sParse.pVdbe
2adb3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2adb4 64 62 65 46 69 6e 61 6c 69 7a 65 28 73 50 61 72  dbeFinalize(sPar
2adb5 73 65 2e 70 56 64 62 65 29 3b 0a 20 20 7d 0a 20  se.pVdbe);.  }. 
2adb6 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
2adb7 62 6c 65 28 73 50 61 72 73 65 2e 70 4e 65 77 54  ble(sParse.pNewT
2adb8 61 62 6c 65 29 3b 0a 20 20 73 50 61 72 73 65 2e  able);.  sParse.
2adb9 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 0a  pNewTable = 0;..
2adba 20 20 61 73 73 65 72 74 28 20 28 72 63 26 30 78    assert( (rc&0x
2adbb 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 72 63 20  ff)==rc );.  rc 
2adbc 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
2adbd 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
2adbe 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
2adbf 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
2adc0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2adc1 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2adc2 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68  is invoked by th
2adc3 65 20 76 64 62 65 20 74 6f 20 63 61 6c 6c 20 74  e vdbe to call t
2adc4 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68  he xDestroy meth
2adc5 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72  od.** of the vir
2adc6 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 64  tual table named
2adc7 20 7a 54 61 62 20 69 6e 20 64 61 74 61 62 61 73   zTab in databas
2adc8 65 20 69 44 62 2e 20 54 68 69 73 20 6f 63 63 75  e iDb. This occu
2adc9 72 73 0a 2a 2a 20 77 68 65 6e 20 61 20 44 52 4f  rs.** when a DRO
2adca 50 20 54 41 42 4c 45 20 69 73 20 6d 65 6e 74 69  P TABLE is menti
2adcb 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  oned..**.** This
2adcc 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70   call is a no-op
2adcd 20 69 66 20 7a 54 61 62 20 69 73 20 6e 6f 74 20   if zTab is not 
2adce 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
2adcf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2add0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74  TE int sqlite3Vt
2add1 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 73 71  abCallDestroy(sq
2add2 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
2add3 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  Db, const char *
2add4 7a 54 61 62 29 0a 7b 0a 20 20 69 6e 74 20 72 63  zTab).{.  int rc
2add5 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
2add6 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a 20 20  Table *pTab;..  
2add7 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
2add8 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
2add9 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
2adda 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28  Name);.  assert(
2addb 70 54 61 62 29 3b 0a 20 20 69 66 28 20 70 54 61  pTab);.  if( pTa
2addc 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20 20  b->pVtab ){.    
2addd 69 6e 74 20 28 2a 78 44 65 73 74 72 6f 79 29 28  int (*xDestroy)(
2adde 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
2addf 54 61 62 29 20 3d 20 70 54 61 62 2d 3e 70 4d 6f  Tab) = pTab->pMo
2ade0 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73  d->pModule->xDes
2ade1 74 72 6f 79 3b 0a 20 20 20 20 72 63 20 3d 20 73  troy;.    rc = s
2ade2 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
2ade3 64 62 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  db);.    assert(
2ade4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2ade5 3b 0a 20 20 20 20 69 66 28 20 78 44 65 73 74 72  ;.    if( xDestr
2ade6 6f 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  oy ){.      rc =
2ade7 20 78 44 65 73 74 72 6f 79 28 70 54 61 62 2d 3e   xDestroy(pTab->
2ade8 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20  pVtab);.    }.  
2ade9 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2adea 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20  afetyOn(db);.   
2adeb 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2adec 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
2aded 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  i;.      for(i=0
2adee 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 3b  ; i<db->nVTrans;
2adef 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   i++){.        i
2adf0 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69  f( db->aVTrans[i
2adf1 5d 3d 3d 70 54 61 62 2d 3e 70 56 74 61 62 20 29  ]==pTab->pVtab )
2adf2 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e  {.          db->
2adf3 61 56 54 72 61 6e 73 5b 69 5d 20 3d 20 64 62 2d  aVTrans[i] = db-
2adf4 3e 61 56 54 72 61 6e 73 5b 2d 2d 64 62 2d 3e 6e  >aVTrans[--db->n
2adf5 56 54 72 61 6e 73 5d 3b 0a 20 20 20 20 20 20 20  VTrans];.       
2adf6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2adf7 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2adf8 20 20 70 54 61 62 2d 3e 70 56 74 61 62 20 3d 20    pTab->pVtab = 
2adf9 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  0;.    }.  }..  
2adfa 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2adfb 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2adfc 6e 20 69 6e 76 6f 6b 65 73 20 65 69 74 68 65 72  n invokes either
2adfd 20 74 68 65 20 78 52 6f 6c 6c 62 61 63 6b 20 6f   the xRollback o
2adfe 72 20 78 43 6f 6d 6d 69 74 20 6d 65 74 68 6f 64  r xCommit method
2adff 0a 2a 2a 20 6f 66 20 65 61 63 68 20 6f 66 20 74  .** of each of t
2ae00 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2ae01 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  s in the sqlite3
2ae02 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20  .aVTrans array. 
2ae03 54 68 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 61  The method.** ca
2ae04 6c 6c 65 64 20 69 73 20 69 64 65 6e 74 69 66 69  lled is identifi
2ae05 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  ed by the second
2ae06 20 61 72 67 75 6d 65 6e 74 2c 20 22 6f 66 66 73   argument, "offs
2ae07 65 74 22 2c 20 77 68 69 63 68 20 69 73 0a 2a 2a  et", which is.**
2ae08 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74   the offset of t
2ae09 68 65 20 6d 65 74 68 6f 64 20 74 6f 20 63 61 6c  he method to cal
2ae0a 6c 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  l in the sqlite3
2ae0b 5f 6d 6f 64 75 6c 65 20 73 74 72 75 63 74 75 72  _module structur
2ae0c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72  e..**.** The arr
2ae0d 61 79 20 69 73 20 63 6c 65 61 72 65 64 20 61 66  ay is cleared af
2ae0e 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65  ter invoking the
2ae0f 20 63 61 6c 6c 62 61 63 6b 73 2e 20 0a 2a 2f 0a   callbacks. .*/.
2ae10 73 74 61 74 69 63 20 76 6f 69 64 20 63 61 6c 6c  static void call
2ae11 46 69 6e 61 6c 69 73 65 72 28 73 71 6c 69 74 65  Finaliser(sqlite
2ae12 33 20 2a 64 62 2c 20 69 6e 74 20 6f 66 66 73 65  3 *db, int offse
2ae13 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  t){.  int i;.  i
2ae14 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 29  f( db->aVTrans )
2ae15 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
2ae16 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 20 26 26 20  <db->nVTrans && 
2ae17 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3b 20  db->aVTrans[i]; 
2ae18 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
2ae19 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20  te3_vtab *pVtab 
2ae1a 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  = db->aVTrans[i]
2ae1b 3b 0a 20 20 20 20 20 20 69 6e 74 20 28 2a 78 29  ;.      int (*x)
2ae1c 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29  (sqlite3_vtab *)
2ae1d 3b 0a 20 20 20 20 20 20 78 20 3d 20 2a 28 69 6e  ;.      x = *(in
2ae1e 74 20 28 2a 2a 29 28 73 71 6c 69 74 65 33 5f 76  t (**)(sqlite3_v
2ae1f 74 61 62 20 2a 29 29 28 28 63 68 61 72 20 2a 29  tab *))((char *)
2ae20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 20 2b  pVtab->pModule +
2ae21 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20   offset);.      
2ae22 69 66 28 20 78 20 29 20 78 28 70 56 74 61 62 29  if( x ) x(pVtab)
2ae23 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2ae24 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56  tabUnlock(db, pV
2ae25 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  tab);.    }.    
2ae26 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2ae27 2c 20 64 62 2d 3e 61 56 54 72 61 6e 73 29 3b 0a  , db->aVTrans);.
2ae28 20 20 20 20 64 62 2d 3e 6e 56 54 72 61 6e 73 20      db->nVTrans 
2ae29 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 61 56 54  = 0;.    db->aVT
2ae2a 72 61 6e 73 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  rans = 0;.  }.}.
2ae2b 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
2ae2c 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64 20 6f  e xSync method o
2ae2d 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74 61  f all virtual ta
2ae2e 62 6c 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69  bles in the sqli
2ae2f 74 65 33 2e 61 56 54 72 61 6e 73 0a 2a 2a 20 61  te3.aVTrans.** a
2ae30 72 72 61 79 2e 20 52 65 74 75 72 6e 20 74 68 65  rray. Return the
2ae31 20 65 72 72 6f 72 20 63 6f 64 65 20 66 6f 72 20   error code for 
2ae32 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20  the first error 
2ae33 74 68 61 74 20 6f 63 63 75 72 73 2c 20 6f 72 0a  that occurs, or.
2ae34 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  ** SQLITE_OK if 
2ae35 61 6c 6c 20 78 53 79 6e 63 20 6f 70 65 72 61 74  all xSync operat
2ae36 69 6f 6e 73 20 61 72 65 20 73 75 63 63 65 73 73  ions are success
2ae37 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 20 2a  ful..**.** Set *
2ae38 70 7a 45 72 72 6d 73 67 20 74 6f 20 70 6f 69 6e  pzErrmsg to poin
2ae39 74 20 74 6f 20 61 20 62 75 66 66 65 72 20 74 68  t to a buffer th
2ae3a 61 74 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c  at should be rel
2ae3b 65 61 73 65 64 20 75 73 69 6e 67 20 0a 2a 2a 20  eased using .** 
2ae3c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20  sqlite3DbFree() 
2ae3d 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e 20 65 72  containing an er
2ae3e 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 69 66 20  ror message, if 
2ae3f 6f 6e 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65  one is available
2ae40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2ae41 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2ae42 74 61 62 53 79 6e 63 28 73 71 6c 69 74 65 33 20  tabSync(sqlite3 
2ae43 2a 64 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72  *db, char **pzEr
2ae44 72 6d 73 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rmsg){.  int i;.
2ae45 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
2ae46 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 63 73 61  E_OK;.  int rcsa
2ae47 66 65 74 79 3b 0a 20 20 73 71 6c 69 74 65 33 5f  fety;.  sqlite3_
2ae48 76 74 61 62 20 2a 2a 61 56 54 72 61 6e 73 20 3d  vtab **aVTrans =
2ae49 20 64 62 2d 3e 61 56 54 72 61 6e 73 3b 0a 0a 20   db->aVTrans;.. 
2ae4a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66   rc = sqlite3Saf
2ae4b 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 64 62  etyOff(db);.  db
2ae4c 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20  ->aVTrans = 0;. 
2ae4d 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51   for(i=0; rc==SQ
2ae4e 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d  LITE_OK && i<db-
2ae4f 3e 6e 56 54 72 61 6e 73 20 26 26 20 61 56 54 72  >nVTrans && aVTr
2ae50 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20  ans[i]; i++){.  
2ae51 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
2ae52 70 56 74 61 62 20 3d 20 61 56 54 72 61 6e 73 5b  pVtab = aVTrans[
2ae53 69 5d 3b 0a 20 20 20 20 69 6e 74 20 28 2a 78 29  i];.    int (*x)
2ae54 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29  (sqlite3_vtab *)
2ae55 3b 0a 20 20 20 20 78 20 3d 20 70 56 74 61 62 2d  ;.    x = pVtab-
2ae56 3e 70 4d 6f 64 75 6c 65 2d 3e 78 53 79 6e 63 3b  >pModule->xSync;
2ae57 0a 20 20 20 20 69 66 28 20 78 20 29 7b 0a 20 20  .    if( x ){.  
2ae58 20 20 20 20 72 63 20 3d 20 78 28 70 56 74 61 62      rc = x(pVtab
2ae59 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2ae5a 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a 45 72  DbFree(db, *pzEr
2ae5b 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 2a 70 7a  rmsg);.      *pz
2ae5c 45 72 72 6d 73 67 20 3d 20 70 56 74 61 62 2d 3e  Errmsg = pVtab->
2ae5d 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 20 20 70  zErrMsg;.      p
2ae5e 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
2ae5f 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64  0;.    }.  }.  d
2ae60 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61 56 54  b->aVTrans = aVT
2ae61 72 61 6e 73 3b 0a 20 20 72 63 73 61 66 65 74 79  rans;.  rcsafety
2ae62 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79   = sqlite3Safety
2ae63 4f 6e 28 64 62 29 3b 0a 0a 20 20 69 66 28 20 72  On(db);..  if( r
2ae64 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
2ae65 20 20 20 20 72 63 20 3d 20 72 63 73 61 66 65 74      rc = rcsafet
2ae66 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  y;.  }.  return 
2ae67 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76  rc;.}../*.** Inv
2ae68 6f 6b 65 20 74 68 65 20 78 52 6f 6c 6c 62 61 63  oke the xRollbac
2ae69 6b 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20  k method of all 
2ae6a 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69  virtual tables i
2ae6b 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65  n the .** sqlite
2ae6c 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e  3.aVTrans array.
2ae6d 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20   Then clear the 
2ae6e 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f  array itself..*/
2ae6f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2ae70 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 52  int sqlite3VtabR
2ae71 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 20  ollback(sqlite3 
2ae72 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61  *db){.  callFina
2ae73 6c 69 73 65 72 28 64 62 2c 20 6f 66 66 73 65 74  liser(db, offset
2ae74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  of(sqlite3_modul
2ae75 65 2c 78 52 6f 6c 6c 62 61 63 6b 29 29 3b 0a 20  e,xRollback));. 
2ae76 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2ae77 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  K;.}../*.** Invo
2ae78 6b 65 20 74 68 65 20 78 43 6f 6d 6d 69 74 20 6d  ke the xCommit m
2ae79 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72  ethod of all vir
2ae7a 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74  tual tables in t
2ae7b 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 61  he .** sqlite3.a
2ae7c 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20 54 68  VTrans array. Th
2ae7d 65 6e 20 63 6c 65 61 72 20 74 68 65 20 61 72 72  en clear the arr
2ae7e 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51  ay itself..*/.SQ
2ae7f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2ae80 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d   sqlite3VtabComm
2ae81 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  it(sqlite3 *db){
2ae82 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72  .  callFinaliser
2ae83 28 64 62 2c 20 6f 66 66 73 65 74 6f 66 28 73 71  (db, offsetof(sq
2ae84 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78 43 6f  lite3_module,xCo
2ae85 6d 6d 69 74 29 29 3b 0a 20 20 72 65 74 75 72 6e  mmit));.  return
2ae86 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
2ae87 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 69 72 74  *.** If the virt
2ae88 75 61 6c 20 74 61 62 6c 65 20 70 56 74 61 62 20  ual table pVtab 
2ae89 73 75 70 70 6f 72 74 73 20 74 68 65 20 74 72 61  supports the tra
2ae8a 6e 73 61 63 74 69 6f 6e 20 69 6e 74 65 72 66 61  nsaction interfa
2ae8b 63 65 0a 2a 2a 20 28 78 42 65 67 69 6e 2f 78 52  ce.** (xBegin/xR
2ae8c 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d 6d 69 74 20  ollback/xCommit 
2ae8d 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 78  and optionally x
2ae8e 53 79 6e 63 29 20 61 6e 64 20 61 20 74 72 61 6e  Sync) and a tran
2ae8f 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f  saction is.** no
2ae90 74 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e  t currently open
2ae91 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 42 65  , invoke the xBe
2ae92 67 69 6e 20 6d 65 74 68 6f 64 20 6e 6f 77 2e 0a  gin method now..
2ae93 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 78 42 65  **.** If the xBe
2ae94 67 69 6e 20 63 61 6c 6c 20 69 73 20 73 75 63 63  gin call is succ
2ae95 65 73 73 66 75 6c 2c 20 70 6c 61 63 65 20 74 68  essful, place th
2ae96 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 70  e sqlite3_vtab p
2ae97 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65  ointer.** in the
2ae98 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73   sqlite3.aVTrans
2ae99 20 61 72 72 61 79 2e 0a 2a 2f 0a 53 51 4c 49 54   array..*/.SQLIT
2ae9a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2ae9b 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 73  lite3VtabBegin(s
2ae9c 71 6c 69 74 65 33 20 2a 64 62 2c 20 73 71 6c 69  qlite3 *db, sqli
2ae9d 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
2ae9e 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
2ae9f 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20  ITE_OK;.  const 
2aea0 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
2aea1 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 53  pModule;..  /* S
2aea2 70 65 63 69 61 6c 20 63 61 73 65 3a 20 49 66 20  pecial case: If 
2aea3 64 62 2d 3e 61 56 54 72 61 6e 73 20 69 73 20 4e  db->aVTrans is N
2aea4 55 4c 4c 20 61 6e 64 20 64 62 2d 3e 6e 56 54 72  ULL and db->nVTr
2aea5 61 6e 73 20 69 73 20 67 72 65 61 74 65 72 0a 20  ans is greater. 
2aea6 20 2a 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74   ** than zero, t
2aea7 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
2aea8 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65  n is being calle
2aea9 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 0a  d from within a.
2aeaa 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64    ** virtual mod
2aeab 75 6c 65 20 78 53 79 6e 63 28 29 20 63 61 6c 6c  ule xSync() call
2aeac 62 61 63 6b 2e 20 49 74 20 69 73 20 69 6c 6c 65  back. It is ille
2aead 67 61 6c 20 74 6f 20 77 72 69 74 65 20 74 6f 20  gal to write to 
2aeae 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f  .  ** virtual mo
2aeaf 64 75 6c 65 20 74 61 62 6c 65 73 20 69 6e 20 74  dule tables in t
2aeb0 68 69 73 20 63 61 73 65 2c 20 73 6f 20 72 65 74  his case, so ret
2aeb1 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
2aeb2 45 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  E..  */.  if( sq
2aeb3 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28  lite3VtabInSync(
2aeb4 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  db) ){.    retur
2aeb5 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b  n SQLITE_LOCKED;
2aeb6 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 56 74 61  .  }.  if( !pVta
2aeb7 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  b ){.    return 
2aeb8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 0a  SQLITE_OK;.  } .
2aeb9 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61    pModule = pVta
2aeba 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 69  b->pModule;..  i
2aebb 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67  f( pModule->xBeg
2aebc 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  in ){.    int i;
2aebd 0a 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 56 74  ...    /* If pVt
2aebe 61 62 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  ab is already in
2aebf 20 74 68 65 20 61 56 54 72 61 6e 73 20 61 72 72   the aVTrans arr
2aec0 61 79 2c 20 72 65 74 75 72 6e 20 65 61 72 6c 79  ay, return early
2aec1 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
2aec2 20 28 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 29   (i<db->nVTrans)
2aec3 20 26 26 20 30 21 3d 64 62 2d 3e 61 56 54 72 61   && 0!=db->aVTra
2aec4 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  ns[i]; i++){.   
2aec5 20 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61     if( db->aVTra
2aec6 6e 73 5b 69 5d 3d 3d 70 56 74 61 62 20 29 7b 0a  ns[i]==pVtab ){.
2aec7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
2aec8 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
2aec9 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
2aeca 49 6e 76 6f 6b 65 20 74 68 65 20 78 42 65 67 69  Invoke the xBegi
2aecb 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 20 20  n method */.    
2aecc 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 42  rc = pModule->xB
2aecd 65 67 69 6e 28 70 56 74 61 62 29 3b 0a 20 20 20  egin(pVtab);.   
2aece 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2aecf 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
2aed0 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64 62 2c   addToVTrans(db,
2aed1 20 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20   pVtab);.    }. 
2aed2 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
2aed3 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72  }../*.** The fir
2aed4 73 74 20 70 61 72 61 6d 65 74 65 72 20 28 70 44  st parameter (pD
2aed5 65 66 29 20 69 73 20 61 20 66 75 6e 63 74 69 6f  ef) is a functio
2aed6 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
2aed7 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64  .  The.** second
2aed8 20 70 61 72 61 6d 65 74 65 72 20 28 70 45 78 70   parameter (pExp
2aed9 72 29 20 69 73 20 74 68 65 20 66 69 72 73 74 20  r) is the first 
2aeda 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73  argument to this
2aedb 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 66   function..** If
2aedc 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75   pExpr is a colu
2aedd 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20  mn in a virtual 
2aede 74 61 62 6c 65 2c 20 74 68 65 6e 20 6c 65 74 20  table, then let 
2aedf 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74  the virtual.** t
2aee0 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  able implementat
2aee1 69 6f 6e 20 68 61 76 65 20 61 6e 20 6f 70 70 6f  ion have an oppo
2aee2 72 74 75 6e 69 74 79 20 74 6f 20 6f 76 65 72 6c  rtunity to overl
2aee3 6f 61 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  oad the function
2aee4 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
2aee5 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20  tine is used to 
2aee6 61 6c 6c 6f 77 20 76 69 72 74 75 61 6c 20 74 61  allow virtual ta
2aee7 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
2aee8 6f 6e 73 20 74 6f 0a 2a 2a 20 6f 76 65 72 6c 6f  ons to.** overlo
2aee9 61 64 20 4d 41 54 43 48 2c 20 4c 49 4b 45 2c 20  ad MATCH, LIKE, 
2aeea 47 4c 4f 42 2c 20 61 6e 64 20 52 45 47 45 58 50  GLOB, and REGEXP
2aeeb 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a   operators..**.*
2aeec 2a 20 52 65 74 75 72 6e 20 65 69 74 68 65 72 20  * Return either 
2aeed 74 68 65 20 70 44 65 66 20 61 72 67 75 6d 65 6e  the pDef argumen
2aeee 74 20 28 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f  t (indicating no
2aeef 20 63 68 61 6e 67 65 29 20 6f 72 20 61 20 0a 2a   change) or a .*
2aef0 2a 20 6e 65 77 20 46 75 6e 63 44 65 66 20 73 74  * new FuncDef st
2aef1 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20  ructure that is 
2aef2 6d 61 72 6b 65 64 20 61 73 20 65 70 68 65 6d 65  marked as epheme
2aef3 72 61 6c 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  ral using the.**
2aef4 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48   SQLITE_FUNC_EPH
2aef5 45 4d 20 66 6c 61 67 2e 0a 2a 2f 0a 53 51 4c 49  EM flag..*/.SQLI
2aef6 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44  TE_PRIVATE FuncD
2aef7 65 66 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f  ef *sqlite3VtabO
2aef8 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28  verloadFunction(
2aef9 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2aefa 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2aefb 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 72 65  onnection for re
2aefc 70 6f 72 74 69 6e 67 20 6d 61 6c 6c 6f 63 20 70  porting malloc p
2aefd 72 6f 62 6c 65 6d 73 20 2a 2f 0a 20 20 46 75 6e  roblems */.  Fun
2aefe 63 44 65 66 20 2a 70 44 65 66 2c 20 20 2f 2a 20  cDef *pDef,  /* 
2aeff 46 75 6e 63 74 69 6f 6e 20 74 6f 20 70 6f 73 73  Function to poss
2af00 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 2a 2f  ibly overload */
2af01 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20  .  int nArg,    
2af02 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2af03 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
2af04 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 45   function */.  E
2af05 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 2f  xpr *pExpr     /
2af06 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * First argument
2af07 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
2af08 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
2af09 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pTab;.  sqlite3_
2af0a 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73  vtab *pVtab;.  s
2af0b 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
2af0c 4d 6f 64 3b 0a 20 20 76 6f 69 64 20 28 2a 78 46  Mod;.  void (*xF
2af0d 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
2af0e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
2af0f 33 5f 76 61 6c 75 65 2a 2a 29 20 3d 20 30 3b 0a  3_value**) = 0;.
2af10 20 20 76 6f 69 64 20 2a 70 41 72 67 20 3d 20 30    void *pArg = 0
2af11 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65  ;.  FuncDef *pNe
2af12 77 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b  w;.  int rc = 0;
2af13 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 77 65 72 4e  .  char *zLowerN
2af14 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  ame;.  unsigned 
2af15 63 68 61 72 20 2a 7a 3b 0a 0a 0a 20 20 2f 2a 20  char *z;...  /* 
2af16 43 68 65 63 6b 20 74 6f 20 73 65 65 20 74 68 65  Check to see the
2af17 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73   left operand is
2af18 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76   a column in a v
2af19 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
2af1a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29    if( pExpr==0 )
2af1b 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20   return pDef;.  
2af1c 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
2af1d 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72  K_COLUMN ) retur
2af1e 6e 20 70 44 65 66 3b 0a 20 20 70 54 61 62 20 3d  n pDef;.  pTab =
2af1f 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20   pExpr->pTab;.  
2af20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65  if( pTab==0 ) re
2af21 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 69 66 28  turn pDef;.  if(
2af22 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
2af23 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d   & TF_Virtual)==
2af24 30 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b  0 ) return pDef;
2af25 0a 20 20 70 56 74 61 62 20 3d 20 70 54 61 62 2d  .  pVtab = pTab-
2af26 3e 70 56 74 61 62 3b 0a 20 20 61 73 73 65 72 74  >pVtab;.  assert
2af27 28 20 70 56 74 61 62 21 3d 30 20 29 3b 0a 20 20  ( pVtab!=0 );.  
2af28 61 73 73 65 72 74 28 20 70 56 74 61 62 2d 3e 70  assert( pVtab->p
2af29 4d 6f 64 75 6c 65 21 3d 30 20 29 3b 0a 20 20 70  Module!=0 );.  p
2af2a 4d 6f 64 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d  Mod = (sqlite3_m
2af2b 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70  odule *)pVtab->p
2af2c 4d 6f 64 75 6c 65 3b 0a 20 20 69 66 28 20 70 4d  Module;.  if( pM
2af2d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74 69 6f  od->xFindFunctio
2af2e 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70 44  n==0 ) return pD
2af2f 65 66 3b 0a 20 0a 20 20 2f 2a 20 43 61 6c 6c 20  ef;. .  /* Call 
2af30 74 68 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f  the xFindFunctio
2af31 6e 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20  n method on the 
2af32 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d  virtual table im
2af33 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 20 20 2a  plementation.  *
2af34 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  * to see if the 
2af35 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
2af36 61 6e 74 73 20 74 6f 20 6f 76 65 72 6c 6f 61 64  ants to overload
2af37 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a   this function .
2af38 20 20 2a 2f 0a 20 20 7a 4c 6f 77 65 72 4e 61 6d    */.  zLowerNam
2af39 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
2af3a 44 75 70 28 64 62 2c 20 70 44 65 66 2d 3e 7a 4e  Dup(db, pDef->zN
2af3b 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4c 6f 77  ame);.  if( zLow
2af3c 65 72 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f  erName ){.    fo
2af3d 72 28 7a 3d 28 75 6e 73 69 67 6e 65 64 20 63 68  r(z=(unsigned ch
2af3e 61 72 2a 29 7a 4c 6f 77 65 72 4e 61 6d 65 3b 20  ar*)zLowerName; 
2af3f 2a 7a 3b 20 7a 2b 2b 29 7b 0a 20 20 20 20 20 20  *z; z++){.      
2af40 2a 7a 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65  *z = sqlite3Uppe
2af41 72 54 6f 4c 6f 77 65 72 5b 2a 7a 5d 3b 0a 20 20  rToLower[*z];.  
2af42 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f    }.    rc = pMo
2af43 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e  d->xFindFunction
2af44 28 70 56 74 61 62 2c 20 6e 41 72 67 2c 20 7a 4c  (pVtab, nArg, zL
2af45 6f 77 65 72 4e 61 6d 65 2c 20 26 78 46 75 6e 63  owerName, &xFunc
2af46 2c 20 26 70 41 72 67 29 3b 0a 20 20 20 20 73 71  , &pArg);.    sq
2af47 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2af48 7a 4c 6f 77 65 72 4e 61 6d 65 29 3b 0a 20 20 20  zLowerName);.   
2af49 20 69 66 28 20 70 56 74 61 62 2d 3e 7a 45 72 72   if( pVtab->zErr
2af4a 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Msg ){.      sql
2af4b 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
2af4c 2c 20 22 25 73 22 2c 20 70 56 74 61 62 2d 3e 7a  , "%s", pVtab->z
2af4d 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 73  ErrMsg);.      s
2af4e 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2af4f 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29   pVtab->zErrMsg)
2af50 3b 0a 20 20 20 20 20 20 70 56 74 61 62 2d 3e 7a  ;.      pVtab->z
2af51 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20  ErrMsg = 0;.    
2af52 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  }.  }.  if( rc==
2af53 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
2af54 70 44 65 66 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  pDef;.  }..  /* 
2af55 43 72 65 61 74 65 20 61 20 6e 65 77 20 65 70 68  Create a new eph
2af56 65 6d 65 72 61 6c 20 66 75 6e 63 74 69 6f 6e 20  emeral function 
2af57 64 65 66 69 6e 69 74 69 6f 6e 20 66 6f 72 20 74  definition for t
2af58 68 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a 20 20  he overloaded.  
2af59 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  ** function */. 
2af5a 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
2af5b 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2af5c 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 0a 20 20  sizeof(*pNew).  
2af5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af5e 20 20 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c             + sql
2af5f 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44 65  ite3Strlen30(pDe
2af60 66 2d 3e 7a 4e 61 6d 65 29 20 29 3b 0a 20 20 69  f->zName) );.  i
2af61 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( pNew==0 ){.  
2af62 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20    return pDef;. 
2af63 20 7d 0a 20 20 2a 70 4e 65 77 20 3d 20 2a 70 44   }.  *pNew = *pD
2af64 65 66 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d  ef;.  pNew->zNam
2af65 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 4e 65  e = (char *)&pNe
2af66 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 28 70  w[1];.  memcpy(p
2af67 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66  New->zName, pDef
2af68 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33  ->zName, sqlite3
2af69 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a  Strlen30(pDef->z
2af6a 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 70 4e 65 77  Name)+1);.  pNew
2af6b 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b  ->xFunc = xFunc;
2af6c 0a 20 20 70 4e 65 77 2d 3e 70 55 73 65 72 44 61  .  pNew->pUserDa
2af6d 74 61 20 3d 20 70 41 72 67 3b 0a 20 20 70 4e 65  ta = pArg;.  pNe
2af6e 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  w->flags |= SQLI
2af6f 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 3b 0a 20  TE_FUNC_EPHEM;. 
2af70 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
2af71 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
2af72 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70   virtual table p
2af73 54 61 62 20 69 73 20 63 6f 6e 74 61 69 6e 65 64  Tab is contained
2af74 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e   in the pParse->
2af75 61 70 56 69 72 74 75 61 6c 4c 6f 63 6b 5b 5d 0a  apVirtualLock[].
2af76 2a 2a 20 61 72 72 61 79 20 73 6f 20 74 68 61 74  ** array so that
2af77 20 61 6e 20 4f 50 5f 56 42 65 67 69 6e 20 77 69   an OP_VBegin wi
2af78 6c 6c 20 67 65 74 20 67 65 6e 65 72 61 74 65 64  ll get generated
2af79 20 66 6f 72 20 69 74 2e 20 20 41 64 64 20 70 54   for it.  Add pT
2af7a 61 62 20 74 6f 20 74 68 65 0a 2a 2a 20 61 72 72  ab to the.** arr
2af7b 61 79 20 69 66 20 69 74 20 69 73 20 6d 69 73 73  ay if it is miss
2af7c 69 6e 67 2e 20 20 49 66 20 70 54 61 62 20 69 73  ing.  If pTab is
2af7d 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
2af7e 61 72 72 61 79 2c 20 74 68 69 73 20 72 6f 75 74  array, this rout
2af7f 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f  ine.** is a no-o
2af80 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  p..*/.SQLITE_PRI
2af81 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2af82 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c  3VtabMakeWritabl
2af83 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2af84 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
2af85 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 61 73 73   int i, n;.  ass
2af86 65 72 74 28 20 49 73 56 69 72 74 75 61 6c 28 70  ert( IsVirtual(p
2af87 54 61 62 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d  Tab) );.  for(i=
2af88 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56 74  0; i<pParse->nVt
2af89 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20  abLock; i++){.  
2af8a 20 20 69 66 28 20 70 54 61 62 3d 3d 70 50 61 72    if( pTab==pPar
2af8b 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 69  se->apVtabLock[i
2af8c 5d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a  ] ) return;.  }.
2af8d 20 20 6e 20 3d 20 28 70 50 61 72 73 65 2d 3e 6e    n = (pParse->n
2af8e 56 74 61 62 4c 6f 63 6b 2b 31 29 2a 73 69 7a 65  VtabLock+1)*size
2af8f 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56 74 61  of(pParse->apVta
2af90 62 4c 6f 63 6b 5b 30 5d 29 3b 0a 20 20 70 50 61  bLock[0]);.  pPa
2af91 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 20  rse->apVtabLock 
2af92 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
2af93 63 28 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62  c(pParse->apVtab
2af94 4c 6f 63 6b 2c 20 6e 29 3b 0a 20 20 69 66 28 20  Lock, n);.  if( 
2af95 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f  pParse->apVtabLo
2af96 63 6b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  ck ){.    pParse
2af97 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 70 50 61  ->apVtabLock[pPa
2af98 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 2b  rse->nVtabLock++
2af99 5d 20 3d 20 70 54 61 62 3b 0a 20 20 7d 65 6c 73  ] = pTab;.  }els
2af9a 65 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64  e{.    pParse->d
2af9b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2af9c 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64  = 1;.  }.}..#end
2af9d 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2af9e 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
2af9f 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2afa0 2a 2a 20 45 6e 64 20 6f 66 20 76 74 61 62 2e 63  ** End of vtab.c
2afa1 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2afa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afa4 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2afa5 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 77 68  ** Begin file wh
2afa6 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ere.c **********
2afa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afa9 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
2afaa 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
2afab 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2afac 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2afad 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2afae 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2afaf 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2afb0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2afb1 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2afb2 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2afb3 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2afb4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2afb5 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2afb6 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2afb7 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2afb8 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2afb9 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2afba 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2afbb 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2afbc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afbf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
2afc1 73 20 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e  s module contain
2afc2 73 20 43 20 63 6f 64 65 20 74 68 61 74 20 67 65  s C code that ge
2afc3 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64  nerates VDBE cod
2afc4 65 20 75 73 65 64 20 74 6f 20 70 72 6f 63 65 73  e used to proces
2afc5 73 0a 2a 2a 20 74 68 65 20 57 48 45 52 45 20 63  s.** the WHERE c
2afc6 6c 61 75 73 65 20 6f 66 20 53 51 4c 20 73 74 61  lause of SQL sta
2afc7 74 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 20 6d  tements.  This m
2afc8 6f 64 75 6c 65 20 69 73 20 72 65 73 70 6f 6e 73  odule is respons
2afc9 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 67 65 6e 65  ible for.** gene
2afca 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20  rating the code 
2afcb 74 68 61 74 20 6c 6f 6f 70 73 20 74 68 72 6f 75  that loops throu
2afcc 67 68 20 61 20 74 61 62 6c 65 20 6c 6f 6f 6b 69  gh a table looki
2afcd 6e 67 20 66 6f 72 20 61 70 70 6c 69 63 61 62 6c  ng for applicabl
2afce 65 0a 2a 2a 20 72 6f 77 73 2e 20 20 49 6e 64 69  e.** rows.  Indi
2afcf 63 65 73 20 61 72 65 20 73 65 6c 65 63 74 65 64  ces are selected
2afd0 20 61 6e 64 20 75 73 65 64 20 74 6f 20 73 70 65   and used to spe
2afd1 65 64 20 74 68 65 20 73 65 61 72 63 68 20 77 68  ed the search wh
2afd2 65 6e 20 64 6f 69 6e 67 0a 2a 2a 20 73 6f 20 69  en doing.** so i
2afd3 73 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 42  s applicable.  B
2afd4 65 63 61 75 73 65 20 74 68 69 73 20 6d 6f 64 75  ecause this modu
2afd5 6c 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  le is responsibl
2afd6 65 20 66 6f 72 20 73 65 6c 65 63 74 69 6e 67 0a  e for selecting.
2afd7 2a 2a 20 69 6e 64 69 63 65 73 2c 20 79 6f 75 20  ** indices, you 
2afd8 6d 69 67 68 74 20 61 6c 73 6f 20 74 68 69 6e 6b  might also think
2afd9 20 6f 66 20 74 68 69 73 20 6d 6f 64 75 6c 65 20   of this module 
2afda 61 73 20 74 68 65 20 22 71 75 65 72 79 20 6f 70  as the "query op
2afdb 74 69 6d 69 7a 65 72 22 2e 0a 2a 2a 0a 2a 2a 20  timizer"..**.** 
2afdc 24 49 64 3a 20 77 68 65 72 65 2e 63 2c 76 20 31  $Id: where.c,v 1
2afdd 2e 33 39 36 20 32 30 30 39 2f 30 35 2f 30 36 20  .396 2009/05/06 
2afde 31 39 3a 30 33 3a 31 34 20 64 72 68 20 45 78 70  19:03:14 drh Exp
2afdf 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 72 61   $.*/../*.** Tra
2afe0 63 65 20 6f 75 74 70 75 74 20 6d 61 63 72 6f 73  ce output macros
2afe1 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
2afe2 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20  SQLITE_TEST) || 
2afe3 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
2afe4 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49  EBUG).SQLITE_PRI
2afe5 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2afe6 57 68 65 72 65 54 72 61 63 65 20 3d 20 30 3b 0a  WhereTrace = 0;.
2afe7 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e  #endif.#if defin
2afe8 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
2afe9 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
2afea 45 5f 44 45 42 55 47 29 0a 23 20 64 65 66 69 6e  E_DEBUG).# defin
2afeb 65 20 57 48 45 52 45 54 52 41 43 45 28 58 29 20  e WHERETRACE(X) 
2afec 20 69 66 28 73 71 6c 69 74 65 33 57 68 65 72 65   if(sqlite3Where
2afed 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65  Trace) sqlite3De
2afee 62 75 67 50 72 69 6e 74 66 20 58 0a 23 65 6c 73  bugPrintf X.#els
2afef 65 0a 23 20 64 65 66 69 6e 65 20 57 48 45 52 45  e.# define WHERE
2aff0 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a  TRACE(X).#endif.
2aff1 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
2aff2 72 65 6e 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66  rence.*/.typedef
2aff3 20 73 74 72 75 63 74 20 57 68 65 72 65 43 6c 61   struct WhereCla
2aff4 75 73 65 20 57 68 65 72 65 43 6c 61 75 73 65 3b  use WhereClause;
2aff5 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2aff6 57 68 65 72 65 4d 61 73 6b 53 65 74 20 57 68 65  WhereMaskSet Whe
2aff7 72 65 4d 61 73 6b 53 65 74 3b 0a 74 79 70 65 64  reMaskSet;.typed
2aff8 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4f  ef struct WhereO
2aff9 72 49 6e 66 6f 20 57 68 65 72 65 4f 72 49 6e 66  rInfo WhereOrInf
2affa 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  o;.typedef struc
2affb 74 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 57  t WhereAndInfo W
2affc 68 65 72 65 41 6e 64 49 6e 66 6f 3b 0a 74 79 70  hereAndInfo;.typ
2affd 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
2affe 65 43 6f 73 74 20 57 68 65 72 65 43 6f 73 74 3b  eCost WhereCost;
2afff 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 71 75 65 72  ../*.** The quer
2b000 79 20 67 65 6e 65 72 61 74 6f 72 20 75 73 65 73  y generator uses
2b001 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 73   an array of ins
2b002 74 61 6e 63 65 73 20 6f 66 20 74 68 69 73 20 73  tances of this s
2b003 74 72 75 63 74 75 72 65 20 74 6f 0a 2a 2a 20 68  tructure to.** h
2b004 65 6c 70 20 69 74 20 61 6e 61 6c 79 7a 65 20 74  elp it analyze t
2b005 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  he subexpression
2b006 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  s of the WHERE c
2b007 6c 61 75 73 65 2e 20 20 45 61 63 68 20 57 48 45  lause.  Each WHE
2b008 52 45 0a 2a 2a 20 63 6c 61 75 73 65 20 73 75 62  RE.** clause sub
2b009 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65  expression is se
2b00a 70 61 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65  parated from the
2b00b 20 6f 74 68 65 72 73 20 62 79 20 41 4e 44 20 6f   others by AND o
2b00c 70 65 72 61 74 6f 72 73 2c 0a 2a 2a 20 75 73 75  perators,.** usu
2b00d 61 6c 6c 79 2c 20 6f 72 20 73 6f 6d 65 74 69 6d  ally, or sometim
2b00e 65 73 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  es subexpression
2b00f 73 20 73 65 70 61 72 61 74 65 64 20 62 79 20 4f  s separated by O
2b010 52 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 57 68 65  R..**.** All Whe
2b011 72 65 54 65 72 6d 73 20 61 72 65 20 63 6f 6c 6c  reTerms are coll
2b012 65 63 74 65 64 20 69 6e 74 6f 20 61 20 73 69 6e  ected into a sin
2b013 67 6c 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  gle WhereClause 
2b014 73 74 72 75 63 74 75 72 65 2e 20 20 0a 2a 2a 20  structure.  .** 
2b015 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 64  The following id
2b016 65 6e 74 69 74 79 20 68 6f 6c 64 73 3a 0a 2a 2a  entity holds:.**
2b017 0a 2a 2a 20 20 20 20 20 20 20 20 57 68 65 72 65  .**        Where
2b018 54 65 72 6d 2e 70 57 43 2d 3e 61 5b 57 68 65 72  Term.pWC->a[Wher
2b019 65 54 65 72 6d 2e 69 64 78 5d 20 3d 3d 20 57 68  eTerm.idx] == Wh
2b01a 65 72 65 54 65 72 6d 0a 2a 2a 0a 2a 2a 20 57 68  ereTerm.**.** Wh
2b01b 65 6e 20 61 20 74 65 72 6d 20 69 73 20 6f 66 20  en a term is of 
2b01c 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  the form:.**.** 
2b01d 20 20 20 20 20 20 20 20 20 20 20 20 20 58 20 3c               X <
2b01e 6f 70 3e 20 3c 65 78 70 72 3e 0a 2a 2a 0a 2a 2a  op> <expr>.**.**
2b01f 20 77 68 65 72 65 20 58 20 69 73 20 61 20 63 6f   where X is a co
2b020 6c 75 6d 6e 20 6e 61 6d 65 20 61 6e 64 20 3c 6f  lumn name and <o
2b021 70 3e 20 69 73 20 6f 6e 65 20 6f 66 20 63 65 72  p> is one of cer
2b022 74 61 69 6e 20 6f 70 65 72 61 74 6f 72 73 2c 0a  tain operators,.
2b023 2a 2a 20 74 68 65 6e 20 57 68 65 72 65 54 65 72  ** then WhereTer
2b024 6d 2e 6c 65 66 74 43 75 72 73 6f 72 20 61 6e 64  m.leftCursor and
2b025 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 6c 65 66   WhereTerm.u.lef
2b026 74 43 6f 6c 75 6d 6e 20 72 65 63 6f 72 64 20 74  tColumn record t
2b027 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d  he.** cursor num
2b028 62 65 72 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e  ber and column n
2b029 75 6d 62 65 72 20 66 6f 72 20 58 2e 20 20 57 68  umber for X.  Wh
2b02a 65 72 65 54 65 72 6d 2e 65 4f 70 65 72 61 74 6f  ereTerm.eOperato
2b02b 72 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65  r records.** the
2b02c 20 3c 6f 70 3e 20 75 73 69 6e 67 20 61 20 62 69   <op> using a bi
2b02d 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 64  tmask encoding d
2b02e 65 66 69 6e 65 64 20 62 79 20 57 4f 5f 78 78 78  efined by WO_xxx
2b02f 20 62 65 6c 6f 77 2e 20 20 54 68 65 0a 2a 2a 20   below.  The.** 
2b030 75 73 65 20 6f 66 20 61 20 62 69 74 6d 61 73 6b  use of a bitmask
2b031 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68   encoding for th
2b032 65 20 6f 70 65 72 61 74 6f 72 20 61 6c 6c 6f 77  e operator allow
2b033 73 20 75 73 20 74 6f 20 73 65 61 72 63 68 0a 2a  s us to search.*
2b034 2a 20 71 75 69 63 6b 6c 79 20 66 6f 72 20 74 65  * quickly for te
2b035 72 6d 73 20 74 68 61 74 20 6d 61 74 63 68 20 61  rms that match a
2b036 6e 79 20 6f 66 20 73 65 76 65 72 61 6c 20 64 69  ny of several di
2b037 66 66 65 72 65 6e 74 20 6f 70 65 72 61 74 6f 72  fferent operator
2b038 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 57 68 65 72 65  s..**.** A Where
2b039 54 65 72 6d 20 6d 69 67 68 74 20 61 6c 73 6f 20  Term might also 
2b03a 62 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73  be two or more s
2b03b 75 62 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65  ubterms connecte
2b03c 64 20 62 79 20 4f 52 3a 0a 2a 2a 0a 2a 2a 20 20  d by OR:.**.**  
2b03d 20 20 20 20 20 20 20 28 74 31 2e 58 20 3c 6f 70         (t1.X <op
2b03e 3e 20 3c 65 78 70 72 3e 29 20 4f 52 20 28 74 31  > <expr>) OR (t1
2b03f 2e 59 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 29 20  .Y <op> <expr>) 
2b040 4f 52 20 2e 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 6e  OR .....**.** In
2b041 20 74 68 69 73 20 73 65 63 6f 6e 64 20 63 61 73   this second cas
2b042 65 2c 20 77 74 46 6c 61 67 20 61 73 20 74 68 65  e, wtFlag as the
2b043 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 73 65 74   TERM_ORINFO set
2b044 20 61 6e 64 20 65 4f 70 65 72 61 74 6f 72 3d 3d   and eOperator==
2b045 57 4f 5f 4f 52 0a 2a 2a 20 61 6e 64 20 74 68 65  WO_OR.** and the
2b046 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72   WhereTerm.u.pOr
2b047 49 6e 66 6f 20 66 69 65 6c 64 20 70 6f 69 6e 74  Info field point
2b048 73 20 74 6f 20 61 75 78 69 6c 69 61 72 79 20 69  s to auxiliary i
2b049 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 0a  nformation that.
2b04a 2a 2a 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20  ** is collected 
2b04b 61 62 6f 75 74 20 74 68 65 0a 2a 2a 0a 2a 2a 20  about the.**.** 
2b04c 49 66 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65  If a term in the
2b04d 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 64 6f   WHERE clause do
2b04e 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 65 69 74  es not match eit
2b04f 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20 70  her of the two p
2b050 72 65 76 69 6f 75 73 0a 2a 2a 20 63 61 74 65 67  revious.** categ
2b051 6f 72 69 65 73 2c 20 74 68 65 6e 20 65 4f 70 65  ories, then eOpe
2b052 72 61 74 6f 72 3d 3d 30 2e 20 20 54 68 65 20 57  rator==0.  The W
2b053 68 65 72 65 54 65 72 6d 2e 70 45 78 70 72 20 66  hereTerm.pExpr f
2b054 69 65 6c 64 20 69 73 20 73 74 69 6c 6c 20 73 65  ield is still se
2b055 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 72 69 67  t.** to the orig
2b056 69 6e 61 6c 20 73 75 62 65 78 70 72 65 73 73 69  inal subexpressi
2b057 6f 6e 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 77  on content and w
2b058 74 46 6c 61 67 73 20 69 73 20 73 65 74 20 75 70  tFlags is set up
2b059 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 0a 2a   appropriately.*
2b05a 2a 20 62 75 74 20 6e 6f 20 6f 74 68 65 72 20 66  * but no other f
2b05b 69 65 6c 64 73 20 69 6e 20 74 68 65 20 57 68 65  ields in the Whe
2b05c 72 65 54 65 72 6d 20 6f 62 6a 65 63 74 20 61 72  reTerm object ar
2b05d 65 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2a  e meaningful..**
2b05e 0a 2a 2a 20 57 68 65 6e 20 65 4f 70 65 72 61 74  .** When eOperat
2b05f 6f 72 21 3d 30 2c 20 70 72 65 72 65 71 52 69 67  or!=0, prereqRig
2b060 68 74 20 61 6e 64 20 70 72 65 72 65 71 41 6c 6c  ht and prereqAll
2b061 20 72 65 63 6f 72 64 20 73 65 74 73 20 6f 66 20   record sets of 
2b062 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 2c 0a  cursor numbers,.
2b063 2a 2a 20 62 75 74 20 74 68 65 79 20 64 6f 20 73  ** but they do s
2b064 6f 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 41  o indirectly.  A
2b065 20 73 69 6e 67 6c 65 20 57 68 65 72 65 4d 61 73   single WhereMas
2b066 6b 53 65 74 20 73 74 72 75 63 74 75 72 65 20 74  kSet structure t
2b067 72 61 6e 73 6c 61 74 65 73 0a 2a 2a 20 63 75 72  ranslates.** cur
2b068 73 6f 72 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20  sor number into 
2b069 62 69 74 73 20 61 6e 64 20 74 68 65 20 74 72 61  bits and the tra
2b06a 6e 73 6c 61 74 65 64 20 62 69 74 20 69 73 20 73  nslated bit is s
2b06b 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70 72 65  tored in the pre
2b06c 72 65 71 0a 2a 2a 20 66 69 65 6c 64 73 2e 20 20  req.** fields.  
2b06d 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20  The translation 
2b06e 69 73 20 75 73 65 64 20 69 6e 20 6f 72 64 65 72  is used in order
2b06f 20 74 6f 20 6d 61 78 69 6d 69 7a 65 20 74 68 65   to maximize the
2b070 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 69   number of.** bi
2b071 74 73 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74  ts that will fit
2b072 20 69 6e 20 61 20 42 69 74 6d 61 73 6b 2e 20 20   in a Bitmask.  
2b073 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
2b074 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 62 65  numbers might be
2b075 0a 2a 2a 20 73 70 72 65 61 64 20 6f 75 74 20 6f  .** spread out o
2b076 76 65 72 20 74 68 65 20 6e 6f 6e 2d 6e 65 67 61  ver the non-nega
2b077 74 69 76 65 20 69 6e 74 65 67 65 72 73 2e 20 20  tive integers.  
2b078 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  For example, the
2b079 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65   cursor.** numbe
2b07a 72 73 20 6d 69 67 68 74 20 62 65 20 33 2c 20 38  rs might be 3, 8
2b07b 2c 20 39 2c 20 31 30 2c 20 32 30 2c 20 32 33 2c  , 9, 10, 20, 23,
2b07c 20 34 31 2c 20 61 6e 64 20 34 35 2e 20 20 54 68   41, and 45.  Th
2b07d 65 20 57 68 65 72 65 4d 61 73 6b 53 65 74 0a 2a  e WhereMaskSet.*
2b07e 2a 20 74 72 61 6e 73 6c 61 74 65 73 20 74 68 65  * translates the
2b07f 73 65 20 73 70 61 72 73 65 20 63 75 72 73 6f 72  se sparse cursor
2b080 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f   numbers into co
2b081 6e 73 65 63 75 74 69 76 65 20 69 6e 74 65 67 65  nsecutive intege
2b082 72 73 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20  rs.** beginning 
2b083 77 69 74 68 20 30 20 69 6e 20 6f 72 64 65 72 20  with 0 in order 
2b084 74 6f 20 6d 61 6b 65 20 74 68 65 20 62 65 73 74  to make the best
2b085 20 70 6f 73 73 69 62 6c 65 20 75 73 65 20 6f 66   possible use of
2b086 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a   the available.*
2b087 2a 20 62 69 74 73 20 69 6e 20 74 68 65 20 42 69  * bits in the Bi
2b088 74 6d 61 73 6b 2e 20 20 53 6f 2c 20 69 6e 20 74  tmask.  So, in t
2b089 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65  he example above
2b08a 2c 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  , the cursor num
2b08b 62 65 72 73 0a 2a 2a 20 77 6f 75 6c 64 20 62 65  bers.** would be
2b08c 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 69 6e 74   mapped into int
2b08d 65 67 65 72 73 20 30 20 74 68 72 6f 75 67 68 20  egers 0 through 
2b08e 37 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d  7..**.** The num
2b08f 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
2b090 61 20 6a 6f 69 6e 20 69 73 20 6c 69 6d 69 74 65  a join is limite
2b091 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20  d by the number 
2b092 6f 66 20 62 69 74 73 0a 2a 2a 20 69 6e 20 70 72  of bits.** in pr
2b093 65 72 65 71 52 69 67 68 74 20 61 6e 64 20 70 72  ereqRight and pr
2b094 65 72 65 71 41 6c 6c 2e 20 20 54 68 65 20 64 65  ereqAll.  The de
2b095 66 61 75 6c 74 20 69 73 20 36 34 20 62 69 74 73  fault is 64 bits
2b096 2c 20 68 65 6e 63 65 20 53 51 4c 69 74 65 0a 2a  , hence SQLite.*
2b097 2a 20 69 73 20 6f 6e 6c 79 20 61 62 6c 65 20 74  * is only able t
2b098 6f 20 70 72 6f 63 65 73 73 20 6a 6f 69 6e 73 20  o process joins 
2b099 77 69 74 68 20 36 34 20 6f 72 20 66 65 77 65 72  with 64 or fewer
2b09a 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65   tables..*/.type
2b09b 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65  def struct Where
2b09c 54 65 72 6d 20 57 68 65 72 65 54 65 72 6d 3b 0a  Term WhereTerm;.
2b09d 73 74 72 75 63 74 20 57 68 65 72 65 54 65 72 6d  struct WhereTerm
2b09e 20 7b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72   {.  Expr *pExpr
2b09f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2b0a0 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73  Pointer to the s
2b0a1 75 62 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  ubexpression tha
2b0a2 74 20 69 73 20 74 68 69 73 20 74 65 72 6d 20 2a  t is this term *
2b0a3 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e 74 3b  /.  int iParent;
2b0a4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2b0a5 69 73 61 62 6c 65 20 70 57 43 2d 3e 61 5b 69 50  isable pWC->a[iP
2b0a6 61 72 65 6e 74 5d 20 77 68 65 6e 20 74 68 69 73  arent] when this
2b0a7 20 74 65 72 6d 20 64 69 73 61 62 6c 65 64 20 2a   term disabled *
2b0a8 2f 0a 20 20 69 6e 74 20 6c 65 66 74 43 75 72 73  /.  int leftCurs
2b0a9 6f 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  or;         /* C
2b0aa 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
2b0ab 58 20 69 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78  X in "X <op> <ex
2b0ac 70 72 3e 22 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20  pr>" */.  union 
2b0ad 7b 0a 20 20 20 20 69 6e 74 20 6c 65 66 74 43 6f  {.    int leftCo
2b0ae 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  lumn;         /*
2b0af 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f   Column number o
2b0b0 66 20 58 20 69 6e 20 22 58 20 3c 6f 70 3e 20 3c  f X in "X <op> <
2b0b1 65 78 70 72 3e 22 20 2a 2f 0a 20 20 20 20 57 68  expr>" */.    Wh
2b0b2 65 72 65 4f 72 49 6e 66 6f 20 2a 70 4f 72 49 6e  ereOrInfo *pOrIn
2b0b3 66 6f 3b 20 20 20 2f 2a 20 45 78 74 72 61 20 69  fo;   /* Extra i
2b0b4 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20 65 4f  nformation if eO
2b0b5 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 2a  perator==WO_OR *
2b0b6 2f 0a 20 20 20 20 57 68 65 72 65 41 6e 64 49 6e  /.    WhereAndIn
2b0b7 66 6f 20 2a 70 41 6e 64 49 6e 66 6f 3b 20 2f 2a  fo *pAndInfo; /*
2b0b8 20 45 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69   Extra informati
2b0b9 6f 6e 20 69 66 20 65 4f 70 65 72 61 74 6f 72 3d  on if eOperator=
2b0ba 3d 57 4f 5f 41 4e 44 20 2a 2f 0a 20 20 7d 20 75  =WO_AND */.  } u
2b0bb 3b 0a 20 20 75 31 36 20 65 4f 70 65 72 61 74 6f  ;.  u16 eOperato
2b0bc 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  r;          /* A
2b0bd 20 57 4f 5f 78 78 20 76 61 6c 75 65 20 64 65 73   WO_xx value des
2b0be 63 72 69 62 69 6e 67 20 3c 6f 70 3e 20 2a 2f 0a  cribing <op> */.
2b0bf 20 20 75 38 20 77 74 46 6c 61 67 73 3b 20 20 20    u8 wtFlags;   
2b0c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 45 52            /* TER
2b0c1 4d 5f 78 78 78 20 62 69 74 20 66 6c 61 67 73 2e  M_xxx bit flags.
2b0c2 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20    See below */. 
2b0c3 20 75 38 20 6e 43 68 69 6c 64 3b 20 20 20 20 20   u8 nChild;     
2b0c4 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2b0c5 65 72 20 6f 66 20 63 68 69 6c 64 72 65 6e 20 74  er of children t
2b0c6 68 61 74 20 6d 75 73 74 20 64 69 73 61 62 6c 65  hat must disable
2b0c7 20 75 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c   us */.  WhereCl
2b0c8 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20  ause *pWC;      
2b0c9 20 2f 2a 20 54 68 65 20 63 6c 61 75 73 65 20 74   /* The clause t
2b0ca 68 69 73 20 74 65 72 6d 20 69 73 20 70 61 72 74  his term is part
2b0cb 20 6f 66 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b   of */.  Bitmask
2b0cc 20 70 72 65 72 65 71 52 69 67 68 74 3b 20 20 20   prereqRight;   
2b0cd 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74   /* Bitmask of t
2b0ce 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 70 45  ables used by pE
2b0cf 78 70 72 2d 3e 70 52 69 67 68 74 20 2a 2f 0a 20  xpr->pRight */. 
2b0d0 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41   Bitmask prereqA
2b0d1 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d  ll;      /* Bitm
2b0d2 61 73 6b 20 6f 66 20 74 61 62 6c 65 73 20 72 65  ask of tables re
2b0d3 66 65 72 65 6e 63 65 64 20 62 79 20 70 45 78 70  ferenced by pExp
2b0d4 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  r */.};../*.** A
2b0d5 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66  llowed values of
2b0d6 20 57 68 65 72 65 54 65 72 6d 2e 77 74 46 6c 61   WhereTerm.wtFla
2b0d7 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  gs.*/.#define TE
2b0d8 52 4d 5f 44 59 4e 41 4d 49 43 20 20 20 20 30 78  RM_DYNAMIC    0x
2b0d9 30 31 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20  01   /* Need to 
2b0da 63 61 6c 6c 20 73 71 6c 69 74 65 33 45 78 70 72  call sqlite3Expr
2b0db 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72  Delete(db, pExpr
2b0dc 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52  ) */.#define TER
2b0dd 4d 5f 56 49 52 54 55 41 4c 20 20 20 20 30 78 30  M_VIRTUAL    0x0
2b0de 32 20 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20  2   /* Added by 
2b0df 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20  the optimizer.  
2b0e0 44 6f 20 6e 6f 74 20 63 6f 64 65 20 2a 2f 0a 23  Do not code */.#
2b0e1 64 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f 44 45  define TERM_CODE
2b0e2 44 20 20 20 20 20 20 30 78 30 34 20 20 20 2f 2a  D      0x04   /*
2b0e3 20 54 68 69 73 20 74 65 72 6d 20 69 73 20 61 6c   This term is al
2b0e4 72 65 61 64 79 20 63 6f 64 65 64 20 2a 2f 0a 23  ready coded */.#
2b0e5 64 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f 50 49  define TERM_COPI
2b0e6 45 44 20 20 20 20 20 30 78 30 38 20 20 20 2f 2a  ED     0x08   /*
2b0e7 20 48 61 73 20 61 20 63 68 69 6c 64 20 2a 2f 0a   Has a child */.
2b0e8 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 4f 52 49  #define TERM_ORI
2b0e9 4e 46 4f 20 20 20 20 20 30 78 31 30 20 20 20 2f  NFO     0x10   /
2b0ea 2a 20 4e 65 65 64 20 74 6f 20 66 72 65 65 20 74  * Need to free t
2b0eb 68 65 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70  he WhereTerm.u.p
2b0ec 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f  OrInfo object */
2b0ed 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 41 4e  .#define TERM_AN
2b0ee 44 49 4e 46 4f 20 20 20 20 30 78 32 30 20 20 20  DINFO    0x20   
2b0ef 2f 2a 20 4e 65 65 64 20 74 6f 20 66 72 65 65 20  /* Need to free 
2b0f0 74 68 65 20 57 68 65 72 65 54 65 72 6d 2e 75 2e  the WhereTerm.u.
2b0f1 70 41 6e 64 49 6e 66 6f 20 6f 62 6a 20 2a 2f 0a  pAndInfo obj */.
2b0f2 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 4f 52 5f  #define TERM_OR_
2b0f3 4f 4b 20 20 20 20 20 20 30 78 34 30 20 20 20 2f  OK      0x40   /
2b0f4 2a 20 55 73 65 64 20 64 75 72 69 6e 67 20 4f 52  * Used during OR
2b0f5 2d 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69  -clause processi
2b0f6 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  ng */../*.** An 
2b0f7 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
2b0f8 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
2b0f9 75 72 65 20 68 6f 6c 64 73 20 61 6c 6c 20 69 6e  ure holds all in
2b0fa 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
2b0fb 61 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73  a.** WHERE claus
2b0fc 65 2e 20 20 4d 6f 73 74 6c 79 20 74 68 69 73 20  e.  Mostly this 
2b0fd 69 73 20 61 20 63 6f 6e 74 61 69 6e 65 72 20 66  is a container f
2b0fe 6f 72 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 57  or one or more W
2b0ff 68 65 72 65 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74  hereTerms..*/.st
2b100 72 75 63 74 20 57 68 65 72 65 43 6c 61 75 73 65  ruct WhereClause
2b101 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72   {.  Parse *pPar
2b102 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  se;           /*
2b103 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
2b104 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61  ext */.  WhereMa
2b105 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b  skSet *pMaskSet;
2b106 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20    /* Mapping of 
2b107 74 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d  table cursor num
2b108 62 65 72 73 20 74 6f 20 62 69 74 6d 61 73 6b 73  bers to bitmasks
2b109 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20   */.  u8 op;    
2b10a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b10b 2a 20 53 70 6c 69 74 20 6f 70 65 72 61 74 6f 72  * Split operator
2b10c 2e 20 20 54 4b 5f 41 4e 44 20 6f 72 20 54 4b 5f  .  TK_AND or TK_
2b10d 4f 52 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  OR */.  int nTer
2b10e 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
2b10f 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65   /* Number of te
2b110 72 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6c  rms */.  int nSl
2b111 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ot;             
2b112 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
2b113 6e 74 72 69 65 73 20 69 6e 20 61 5b 5d 20 2a 2f  ntries in a[] */
2b114 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b  .  WhereTerm *a;
2b115 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
2b116 61 63 68 20 61 5b 5d 20 64 65 73 63 72 69 62 65  ach a[] describe
2b117 73 20 61 20 74 65 72 6d 20 6f 66 20 74 68 65 20  s a term of the 
2b118 57 48 45 52 45 20 63 6c 75 61 73 65 20 2a 2f 0a  WHERE cluase */.
2b119 20 20 57 68 65 72 65 54 65 72 6d 20 61 53 74 61    WhereTerm aSta
2b11a 74 69 63 5b 34 5d 3b 20 20 20 20 2f 2a 20 49 6e  tic[4];    /* In
2b11b 69 74 69 61 6c 20 73 74 61 74 69 63 20 73 70 61  itial static spa
2b11c 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 7d 3b  ce for a[] */.};
2b11d 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54  ../*.** A WhereT
2b11e 65 72 6d 20 77 69 74 68 20 65 4f 70 65 72 61 74  erm with eOperat
2b11f 6f 72 3d 3d 57 4f 5f 4f 52 20 68 61 73 20 69 74  or==WO_OR has it
2b120 73 20 75 2e 70 4f 72 49 6e 66 6f 20 70 6f 69 6e  s u.pOrInfo poin
2b121 74 65 72 20 73 65 74 20 74 6f 0a 2a 2a 20 61 20  ter set to.** a 
2b122 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
2b123 63 61 74 65 64 20 69 6e 73 74 61 6e 63 65 20 6f  cated instance o
2b124 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2b125 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74  structure..*/.st
2b126 72 75 63 74 20 57 68 65 72 65 4f 72 49 6e 66 6f  ruct WhereOrInfo
2b127 20 7b 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65   {.  WhereClause
2b128 20 77 63 3b 20 20 20 20 20 20 20 20 20 20 2f 2a   wc;          /*
2b129 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 69   Decomposition i
2b12a 6e 74 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a  nto subterms */.
2b12b 20 20 42 69 74 6d 61 73 6b 20 69 6e 64 65 78 61    Bitmask indexa
2b12c 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 42 69  ble;       /* Bi
2b12d 74 6d 61 73 6b 20 6f 66 20 61 6c 6c 20 69 6e 64  tmask of all ind
2b12e 65 78 61 62 6c 65 20 74 61 62 6c 65 73 20 69 6e  exable tables in
2b12f 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a 7d   the clause */.}
2b130 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65  ;../*.** A Where
2b131 54 65 72 6d 20 77 69 74 68 20 65 4f 70 65 72 61  Term with eOpera
2b132 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 68 61 73 20  tor==WO_AND has 
2b133 69 74 73 20 75 2e 70 41 6e 64 49 6e 66 6f 20 70  its u.pAndInfo p
2b134 6f 69 6e 74 65 72 20 73 65 74 20 74 6f 0a 2a 2a  ointer set to.**
2b135 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61   a dynamically a
2b136 6c 6c 6f 63 61 74 65 64 20 69 6e 73 74 61 6e 63  llocated instanc
2b137 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
2b138 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ng structure..*/
2b139 0a 73 74 72 75 63 74 20 57 68 65 72 65 41 6e 64  .struct WhereAnd
2b13a 49 6e 66 6f 20 7b 0a 20 20 57 68 65 72 65 43 6c  Info {.  WhereCl
2b13b 61 75 73 65 20 77 63 3b 20 20 20 20 20 20 20 20  ause wc;        
2b13c 20 20 2f 2a 20 54 68 65 20 73 75 62 65 78 70 72    /* The subexpr
2b13d 65 73 73 69 6f 6e 20 62 72 6f 6b 65 6e 20 6f 75  ession broken ou
2b13e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t */.};../*.** A
2b13f 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
2b140 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
2b141 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63  cture keeps trac
2b142 6b 20 6f 66 20 61 20 6d 61 70 70 69 6e 67 0a 2a  k of a mapping.*
2b143 2a 20 62 65 74 77 65 65 6e 20 56 44 42 45 20 63  * between VDBE c
2b144 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 6e  ursor numbers an
2b145 64 20 62 69 74 73 20 6f 66 20 74 68 65 20 62 69  d bits of the bi
2b146 74 6d 61 73 6b 73 20 69 6e 20 57 68 65 72 65 54  tmasks in WhereT
2b147 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56  erm..**.** The V
2b148 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
2b149 72 73 20 61 72 65 20 73 6d 61 6c 6c 20 69 6e 74  rs are small int
2b14a 65 67 65 72 73 20 63 6f 6e 74 61 69 6e 65 64 20  egers contained 
2b14b 69 6e 20 0a 2a 2a 20 53 72 63 4c 69 73 74 5f 69  in .** SrcList_i
2b14c 74 65 6d 2e 69 43 75 72 73 6f 72 20 61 6e 64 20  tem.iCursor and 
2b14d 45 78 70 72 2e 69 54 61 62 6c 65 20 66 69 65 6c  Expr.iTable fiel
2b14e 64 73 2e 20 20 46 6f 72 20 61 6e 79 20 67 69 76  ds.  For any giv
2b14f 65 6e 20 57 48 45 52 45 20 0a 2a 2a 20 63 6c 61  en WHERE .** cla
2b150 75 73 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20  use, the cursor 
2b151 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 6e 6f  numbers might no
2b152 74 20 62 65 67 69 6e 20 77 69 74 68 20 30 20 61  t begin with 0 a
2b153 6e 64 20 74 68 65 79 20 6d 69 67 68 74 0a 2a 2a  nd they might.**
2b154 20 63 6f 6e 74 61 69 6e 20 67 61 70 73 20 69 6e   contain gaps in
2b155 20 74 68 65 20 6e 75 6d 62 65 72 69 6e 67 20 73   the numbering s
2b156 65 71 75 65 6e 63 65 2e 20 20 42 75 74 20 77 65  equence.  But we
2b157 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 6d 61   want to make ma
2b158 78 69 6d 75 6d 0a 2a 2a 20 75 73 65 20 6f 66 20  ximum.** use of 
2b159 74 68 65 20 62 69 74 73 20 69 6e 20 6f 75 72 20  the bits in our 
2b15a 62 69 74 6d 61 73 6b 73 2e 20 20 54 68 69 73 20  bitmasks.  This 
2b15b 73 74 72 75 63 74 75 72 65 20 70 72 6f 76 69 64  structure provid
2b15c 65 73 20 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20  es a mapping.** 
2b15d 66 72 6f 6d 20 74 68 65 20 73 70 61 72 73 65 20  from the sparse 
2b15e 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69  cursor numbers i
2b15f 6e 74 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20  nto consecutive 
2b160 69 6e 74 65 67 65 72 73 20 62 65 67 69 6e 6e 69  integers beginni
2b161 6e 67 0a 2a 2a 20 77 69 74 68 20 30 2e 0a 2a 2a  ng.** with 0..**
2b162 0a 2a 2a 20 49 66 20 57 68 65 72 65 4d 61 73 6b  .** If WhereMask
2b163 53 65 74 2e 69 78 5b 41 5d 3d 3d 42 20 69 74 20  Set.ix[A]==B it 
2b164 6d 65 61 6e 73 20 74 68 61 74 20 54 68 65 20 41  means that The A
2b165 2d 74 68 20 62 69 74 20 6f 66 20 61 20 42 69 74  -th bit of a Bit
2b166 6d 61 73 6b 0a 2a 2a 20 63 6f 72 72 65 73 70 6f  mask.** correspo
2b167 6e 64 73 20 56 44 42 45 20 63 75 72 73 6f 72 20  nds VDBE cursor 
2b168 6e 75 6d 62 65 72 20 42 2e 20 20 54 68 65 20 41  number B.  The A
2b169 2d 74 68 20 62 69 74 20 6f 66 20 61 20 62 69 74  -th bit of a bit
2b16a 6d 61 73 6b 20 69 73 20 31 3c 3c 41 2e 0a 2a 2a  mask is 1<<A..**
2b16b 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  .** For example,
2b16c 20 69 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   if the WHERE cl
2b16d 61 75 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20  ause expression 
2b16e 75 73 65 64 20 74 68 65 73 65 20 56 44 42 45 0a  used these VDBE.
2b16f 2a 2a 20 63 75 72 73 6f 72 73 3a 20 20 34 2c 20  ** cursors:  4, 
2b170 35 2c 20 38 2c 20 32 39 2c 20 35 37 2c 20 37 33  5, 8, 29, 57, 73
2b171 2e 20 20 54 68 65 6e 20 74 68 65 20 20 57 68 65  .  Then the  Whe
2b172 72 65 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74  reMaskSet struct
2b173 75 72 65 0a 2a 2a 20 77 6f 75 6c 64 20 6d 61 70  ure.** would map
2b174 20 74 68 6f 73 65 20 63 75 72 73 6f 72 20 6e 75   those cursor nu
2b175 6d 62 65 72 73 20 69 6e 74 6f 20 62 69 74 73 20  mbers into bits 
2b176 30 20 74 68 72 6f 75 67 68 20 35 2e 0a 2a 2a 0a  0 through 5..**.
2b177 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
2b178 20 6d 61 70 70 69 6e 67 20 69 73 20 6e 6f 74 20   mapping is not 
2b179 6e 65 63 65 73 73 61 72 69 6c 79 20 6f 72 64 65  necessarily orde
2b17a 72 65 64 2e 20 20 49 6e 20 74 68 65 20 65 78 61  red.  In the exa
2b17b 6d 70 6c 65 0a 2a 2a 20 61 62 6f 76 65 2c 20 74  mple.** above, t
2b17c 68 65 20 6d 61 70 70 69 6e 67 20 6d 69 67 68 74  he mapping might
2b17d 20 67 6f 20 6c 69 6b 65 20 74 68 69 73 3a 20 20   go like this:  
2b17e 34 2d 3e 33 2c 20 35 2d 3e 31 2c 20 38 2d 3e 32  4->3, 5->1, 8->2
2b17f 2c 20 32 39 2d 3e 30 2c 0a 2a 2a 20 35 37 2d 3e  , 29->0,.** 57->
2b180 35 2c 20 37 33 2d 3e 34 2e 20 20 4f 72 20 6f 6e  5, 73->4.  Or on
2b181 65 20 6f 66 20 37 31 39 20 6f 74 68 65 72 20 63  e of 719 other c
2b182 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6d 69 67 68  ombinations migh
2b183 74 20 62 65 20 75 73 65 64 2e 20 49 74 0a 2a 2a  t be used. It.**
2b184 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79   does not really
2b185 20 6d 61 74 74 65 72 2e 20 20 57 68 61 74 20 69   matter.  What i
2b186 73 20 69 6d 70 6f 72 74 61 6e 74 20 69 73 20 74  s important is t
2b187 68 61 74 20 73 70 61 72 73 65 20 63 75 72 73 6f  hat sparse curso
2b188 72 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61 6c 6c  r.** numbers all
2b189 20 67 65 74 20 6d 61 70 70 65 64 20 69 6e 74 6f   get mapped into
2b18a 20 62 69 74 20 6e 75 6d 62 65 72 73 20 74 68 61   bit numbers tha
2b18b 74 20 62 65 67 69 6e 20 77 69 74 68 20 30 20 61  t begin with 0 a
2b18c 6e 64 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 6f  nd contain.** no
2b18d 20 67 61 70 73 2e 0a 2a 2f 0a 73 74 72 75 63 74   gaps..*/.struct
2b18e 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 7b 0a   WhereMaskSet {.
2b18f 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
2b190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b191 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 73 73  /* Number of ass
2b192 69 67 6e 65 64 20 63 75 72 73 6f 72 20 76 61 6c  igned cursor val
2b193 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 78 5b  ues */.  int ix[
2b194 42 4d 53 5d 3b 20 20 20 20 20 20 20 20 20 20 20  BMS];           
2b195 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
2b196 20 61 73 73 69 67 6e 65 64 20 74 6f 20 65 61 63   assigned to eac
2b197 68 20 62 69 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  h bit */.};../*.
2b198 2a 2a 20 41 20 57 68 65 72 65 43 6f 73 74 20 6f  ** A WhereCost o
2b199 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20 61 20  bject records a 
2b19a 6c 6f 6f 6b 75 70 20 73 74 72 61 74 65 67 79 20  lookup strategy 
2b19b 61 6e 64 20 74 68 65 20 65 73 74 69 6d 61 74 65  and the estimate
2b19c 64 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 70 75 72  d.** cost of pur
2b19d 73 75 69 6e 67 20 74 68 61 74 20 73 74 72 61 74  suing that strat
2b19e 65 67 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57  egy..*/.struct W
2b19f 68 65 72 65 43 6f 73 74 20 7b 0a 20 20 57 68 65  hereCost {.  Whe
2b1a0 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20  rePlan plan;    
2b1a1 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20 73 74  /* The lookup st
2b1a2 72 61 74 65 67 79 20 2a 2f 0a 20 20 64 6f 75 62  rategy */.  doub
2b1a3 6c 65 20 72 43 6f 73 74 3b 20 20 20 20 20 20 2f  le rCost;      /
2b1a4 2a 20 4f 76 65 72 61 6c 6c 20 63 6f 73 74 20 6f  * Overall cost o
2b1a5 66 20 70 75 72 73 75 69 6e 67 20 74 68 69 73 20  f pursuing this 
2b1a6 73 65 61 72 63 68 20 73 74 72 61 74 65 67 79 20  search strategy 
2b1a7 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77  */.  double nRow
2b1a8 3b 20 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d  ;       /* Estim
2b1a9 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 6f  ated number of o
2b1aa 75 74 70 75 74 20 72 6f 77 73 20 2a 2f 0a 7d 3b  utput rows */.};
2b1ab 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 6d 61 73 6b 73  ../*.** Bitmasks
2b1ac 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f   for the operato
2b1ad 72 73 20 74 68 61 74 20 69 6e 64 69 63 65 73 20  rs that indices 
2b1ae 61 72 65 20 61 62 6c 65 20 74 6f 20 65 78 70 6c  are able to expl
2b1af 6f 69 74 2e 20 20 41 6e 0a 2a 2a 20 4f 52 2d 65  oit.  An.** OR-e
2b1b0 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  d combination of
2b1b1 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 63 61   these values ca
2b1b2 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 73  n be used when s
2b1b3 65 61 72 63 68 69 6e 67 20 66 6f 72 0a 2a 2a 20  earching for.** 
2b1b4 74 65 72 6d 73 20 69 6e 20 74 68 65 20 77 68 65  terms in the whe
2b1b5 72 65 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 23 64  re clause..*/.#d
2b1b6 65 66 69 6e 65 20 57 4f 5f 49 4e 20 20 20 20 20  efine WO_IN     
2b1b7 30 78 30 30 31 0a 23 64 65 66 69 6e 65 20 57 4f  0x001.#define WO
2b1b8 5f 45 51 20 20 20 20 20 30 78 30 30 32 0a 23 64  _EQ     0x002.#d
2b1b9 65 66 69 6e 65 20 57 4f 5f 4c 54 20 20 20 20 20  efine WO_LT     
2b1ba 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 54 2d 54  (WO_EQ<<(TK_LT-T
2b1bb 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57  K_EQ)).#define W
2b1bc 4f 5f 4c 45 20 20 20 20 20 28 57 4f 5f 45 51 3c  O_LE     (WO_EQ<
2b1bd 3c 28 54 4b 5f 4c 45 2d 54 4b 5f 45 51 29 29 0a  <(TK_LE-TK_EQ)).
2b1be 23 64 65 66 69 6e 65 20 57 4f 5f 47 54 20 20 20  #define WO_GT   
2b1bf 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 54    (WO_EQ<<(TK_GT
2b1c0 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65  -TK_EQ)).#define
2b1c1 20 57 4f 5f 47 45 20 20 20 20 20 28 57 4f 5f 45   WO_GE     (WO_E
2b1c2 51 3c 3c 28 54 4b 5f 47 45 2d 54 4b 5f 45 51 29  Q<<(TK_GE-TK_EQ)
2b1c3 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4d 41 54  ).#define WO_MAT
2b1c4 43 48 20 20 30 78 30 34 30 0a 23 64 65 66 69 6e  CH  0x040.#defin
2b1c5 65 20 57 4f 5f 49 53 4e 55 4c 4c 20 30 78 30 38  e WO_ISNULL 0x08
2b1c6 30 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4f 52 20  0.#define WO_OR 
2b1c7 20 20 20 20 30 78 31 30 30 20 20 20 20 20 20 20      0x100       
2b1c8 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 4f  /* Two or more O
2b1c9 52 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d  R-connected term
2b1ca 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f  s */.#define WO_
2b1cb 41 4e 44 20 20 20 20 30 78 32 30 30 20 20 20 20  AND    0x200    
2b1cc 20 20 20 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72     /* Two or mor
2b1cd 65 20 41 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20  e AND-connected 
2b1ce 74 65 72 6d 73 20 2a 2f 0a 0a 23 64 65 66 69 6e  terms */..#defin
2b1cf 65 20 57 4f 5f 41 4c 4c 20 20 20 20 30 78 66 66  e WO_ALL    0xff
2b1d0 66 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20  f       /* Mask 
2b1d1 6f 66 20 61 6c 6c 20 70 6f 73 73 69 62 6c 65 20  of all possible 
2b1d2 57 4f 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 23  WO_* values */.#
2b1d3 64 65 66 69 6e 65 20 57 4f 5f 53 49 4e 47 4c 45  define WO_SINGLE
2b1d4 20 30 78 30 66 66 20 20 20 20 20 20 20 2f 2a 20   0x0ff       /* 
2b1d5 4d 61 73 6b 20 6f 66 20 61 6c 6c 20 6e 6f 6e 2d  Mask of all non-
2b1d6 63 6f 6d 70 6f 75 6e 64 20 57 4f 5f 2a 20 76 61  compound WO_* va
2b1d7 6c 75 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56  lues */../*.** V
2b1d8 61 6c 75 65 20 66 6f 72 20 77 73 46 6c 61 67 73  alue for wsFlags
2b1d9 20 72 65 74 75 72 6e 65 64 20 62 79 20 62 65 73   returned by bes
2b1da 74 49 6e 64 65 78 28 29 20 61 6e 64 20 73 74 6f  tIndex() and sto
2b1db 72 65 64 20 69 6e 0a 2a 2a 20 57 68 65 72 65 4c  red in.** WhereL
2b1dc 65 76 65 6c 2e 77 73 46 6c 61 67 73 2e 20 20 54  evel.wsFlags.  T
2b1dd 68 65 73 65 20 66 6c 61 67 73 20 64 65 74 65 72  hese flags deter
2b1de 6d 69 6e 65 20 77 68 69 63 68 20 73 65 61 72 63  mine which searc
2b1df 68 0a 2a 2a 20 73 74 72 61 74 65 67 69 65 73 20  h.** strategies 
2b1e0 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e  are appropriate.
2b1e1 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 61 73 74  .**.** The least
2b1e2 20 73 69 67 6e 69 66 69 63 61 6e 74 20 31 32 20   significant 12 
2b1e3 62 69 74 73 20 69 73 20 72 65 73 65 72 76 65 64  bits is reserved
2b1e4 20 61 73 20 61 20 6d 61 73 6b 20 66 6f 72 20 57   as a mask for W
2b1e5 4f 5f 20 76 61 6c 75 65 73 20 61 62 6f 76 65 2e  O_ values above.
2b1e6 0a 2a 2a 20 54 68 65 20 57 68 65 72 65 4c 65 76  .** The WhereLev
2b1e7 65 6c 2e 77 73 46 6c 61 67 73 20 66 69 65 6c 64  el.wsFlags field
2b1e8 20 69 73 20 75 73 75 61 6c 6c 79 20 73 65 74 20   is usually set 
2b1e9 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 7c 57  to WO_IN|WO_EQ|W
2b1ea 4f 5f 49 53 4e 55 4c 4c 2e 0a 2a 2a 20 42 75 74  O_ISNULL..** But
2b1eb 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   if the table is
2b1ec 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65   the right table
2b1ed 20 6f 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e 2c   of a left join,
2b1ee 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c   WhereLevel.wsFl
2b1ef 61 67 73 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  ags.** is set to
2b1f0 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 2e 20 20 54   WO_IN|WO_EQ.  T
2b1f1 68 65 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73  he WhereLevel.ws
2b1f2 46 6c 61 67 73 20 66 69 65 6c 64 20 63 61 6e 20  Flags field can 
2b1f3 74 68 65 6e 20 62 65 20 75 73 65 64 20 61 73 0a  then be used as.
2b1f4 2a 2a 20 74 68 65 20 22 6f 70 22 20 70 61 72 61  ** the "op" para
2b1f5 6d 65 74 65 72 20 74 6f 20 66 69 6e 64 54 65 72  meter to findTer
2b1f6 6d 20 77 68 65 6e 20 77 65 20 61 72 65 20 72 65  m when we are re
2b1f7 73 6f 6c 76 69 6e 67 20 65 71 75 61 6c 69 74 79  solving equality
2b1f8 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a   constraints..**
2b1f9 20 49 53 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69   ISNULL constrai
2b1fa 6e 74 73 20 77 69 6c 6c 20 74 68 65 6e 20 6e 6f  nts will then no
2b1fb 74 20 62 65 20 75 73 65 64 20 6f 6e 20 74 68 65  t be used on the
2b1fc 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
2b1fd 61 20 6c 65 66 74 0a 2a 2a 20 6a 6f 69 6e 2e 20  a left.** join. 
2b1fe 20 54 69 63 6b 65 74 73 20 23 32 31 37 37 20 61   Tickets #2177 a
2b1ff 6e 64 20 23 32 31 38 39 2e 0a 2a 2f 0a 23 64 65  nd #2189..*/.#de
2b200 66 69 6e 65 20 57 48 45 52 45 5f 52 4f 57 49 44  fine WHERE_ROWID
2b201 5f 45 51 20 20 20 20 20 30 78 30 30 30 30 31 30  _EQ     0x000010
2b202 30 30 20 20 2f 2a 20 72 6f 77 69 64 3d 45 58 50  00  /* rowid=EXP
2b203 52 20 6f 72 20 72 6f 77 69 64 20 49 4e 20 28 2e  R or rowid IN (.
2b204 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  ..) */.#define W
2b205 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45  HERE_ROWID_RANGE
2b206 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a    0x00002000  /*
2b207 20 72 6f 77 69 64 3c 45 58 50 52 20 61 6e 64 2f   rowid<EXPR and/
2b208 6f 72 20 72 6f 77 69 64 3e 45 58 50 52 20 2a 2f  or rowid>EXPR */
2b209 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43  .#define WHERE_C
2b20a 4f 4c 55 4d 4e 5f 45 51 20 20 20 20 30 78 30 30  OLUMN_EQ    0x00
2b20b 30 31 30 30 30 30 20 20 2f 2a 20 78 3d 45 58 50  010000  /* x=EXP
2b20c 52 20 6f 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20  R or x IN (...) 
2b20d 6f 72 20 78 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a  or x IS NULL */.
2b20e 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f  #define WHERE_CO
2b20f 4c 55 4d 4e 5f 52 41 4e 47 45 20 30 78 30 30 30  LUMN_RANGE 0x000
2b210 32 30 30 30 30 20 20 2f 2a 20 78 3c 45 58 50 52  20000  /* x<EXPR
2b211 20 61 6e 64 2f 6f 72 20 78 3e 45 58 50 52 20 2a   and/or x>EXPR *
2b212 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2b213 43 4f 4c 55 4d 4e 5f 49 4e 20 20 20 20 30 78 30  COLUMN_IN    0x0
2b214 30 30 34 30 30 30 30 20 20 2f 2a 20 78 20 49 4e  0040000  /* x IN
2b215 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e   (...) */.#defin
2b216 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e  e WHERE_COLUMN_N
2b217 55 4c 4c 20 20 30 78 30 30 30 38 30 30 30 30 20  ULL  0x00080000 
2b218 20 2f 2a 20 78 20 49 53 20 4e 55 4c 4c 20 2a 2f   /* x IS NULL */
2b219 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 49  .#define WHERE_I
2b21a 4e 44 45 58 45 44 20 20 20 20 20 20 30 78 30 30  NDEXED      0x00
2b21b 30 66 30 30 30 30 20 20 2f 2a 20 41 6e 79 74 68  0f0000  /* Anyth
2b21c 69 6e 67 20 74 68 61 74 20 75 73 65 73 20 61 6e  ing that uses an
2b21d 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e   index */.#defin
2b21e 65 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20  e WHERE_IN_ABLE 
2b21f 20 20 20 20 20 30 78 30 30 30 66 31 30 30 30 20       0x000f1000 
2b220 20 2f 2a 20 41 62 6c 65 20 74 6f 20 73 75 70 70   /* Able to supp
2b221 6f 72 74 20 61 6e 20 49 4e 20 6f 70 65 72 61 74  ort an IN operat
2b222 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  or */.#define WH
2b223 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 20 20  ERE_TOP_LIMIT   
2b224 20 30 78 30 30 31 30 30 30 30 30 20 20 2f 2a 20   0x00100000  /* 
2b225 78 3c 45 58 50 52 20 6f 72 20 78 3c 3d 45 58 50  x<EXPR or x<=EXP
2b226 52 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  R constraint */.
2b227 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 42 54  #define WHERE_BT
2b228 4d 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 30 32  M_LIMIT    0x002
2b229 30 30 30 30 30 20 20 2f 2a 20 78 3e 45 58 50 52  00000  /* x>EXPR
2b22a 20 6f 72 20 78 3e 3d 45 58 50 52 20 63 6f 6e 73   or x>=EXPR cons
2b22b 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  traint */.#defin
2b22c 65 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  e WHERE_IDX_ONLY
2b22d 20 20 20 20 20 30 78 30 30 38 30 30 30 30 30 20       0x00800000 
2b22e 20 2f 2a 20 55 73 65 20 69 6e 64 65 78 20 6f 6e   /* Use index on
2b22f 6c 79 20 2d 20 6f 6d 69 74 20 74 61 62 6c 65 20  ly - omit table 
2b230 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2b231 5f 4f 52 44 45 52 42 59 20 20 20 20 20 20 30 78  _ORDERBY      0x
2b232 30 31 30 30 30 30 30 30 20 20 2f 2a 20 4f 75 74  01000000  /* Out
2b233 70 75 74 20 77 69 6c 6c 20 61 70 70 65 61 72 20  put will appear 
2b234 69 6e 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72  in correct order
2b235 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
2b236 45 5f 52 45 56 45 52 53 45 20 20 20 20 20 20 30  E_REVERSE      0
2b237 78 30 32 30 30 30 30 30 30 20 20 2f 2a 20 53 63  x02000000  /* Sc
2b238 61 6e 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72  an in reverse or
2b239 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  der */.#define W
2b23a 48 45 52 45 5f 55 4e 49 51 55 45 20 20 20 20 20  HERE_UNIQUE     
2b23b 20 20 30 78 30 34 30 30 30 30 30 30 20 20 2f 2a    0x04000000  /*
2b23c 20 53 65 6c 65 63 74 73 20 6e 6f 20 6d 6f 72 65   Selects no more
2b23d 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 2a 2f   than one row */
2b23e 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 56  .#define WHERE_V
2b23f 49 52 54 55 41 4c 54 41 42 4c 45 20 30 78 30 38  IRTUALTABLE 0x08
2b240 30 30 30 30 30 30 20 20 2f 2a 20 55 73 65 20 76  000000  /* Use v
2b241 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 70 72 6f  irtual-table pro
2b242 63 65 73 73 69 6e 67 20 2a 2f 0a 23 64 65 66 69  cessing */.#defi
2b243 6e 65 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f  ne WHERE_MULTI_O
2b244 52 20 20 20 20 20 30 78 31 30 30 30 30 30 30 30  R     0x10000000
2b245 20 20 2f 2a 20 4f 52 20 75 73 69 6e 67 20 6d 75    /* OR using mu
2b246 6c 74 69 70 6c 65 20 69 6e 64 69 63 65 73 20 2a  ltiple indices *
2b247 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  /../*.** Initial
2b248 69 7a 65 20 61 20 70 72 65 61 6c 6c 6f 63 61 74  ize a preallocat
2b249 65 64 20 57 68 65 72 65 43 6c 61 75 73 65 20 73  ed WhereClause s
2b24a 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61  tructure..*/.sta
2b24b 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c  tic void whereCl
2b24c 61 75 73 65 49 6e 69 74 28 0a 20 20 57 68 65 72  auseInit(.  Wher
2b24d 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
2b24e 20 20 20 20 20 2f 2a 20 54 68 65 20 57 68 65 72       /* The Wher
2b24f 65 43 6c 61 75 73 65 20 74 6f 20 62 65 20 69 6e  eClause to be in
2b250 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 50  itialized */.  P
2b251 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2b252 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2b253 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2b254 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74  /.  WhereMaskSet
2b255 20 2a 70 4d 61 73 6b 53 65 74 20 20 20 2f 2a 20   *pMaskSet   /* 
2b256 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62  Mapping from tab
2b257 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  le cursor number
2b258 73 20 74 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f  s to bitmasks */
2b259 0a 29 7b 0a 20 20 70 57 43 2d 3e 70 50 61 72 73  .){.  pWC->pPars
2b25a 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70 57  e = pParse;.  pW
2b25b 43 2d 3e 70 4d 61 73 6b 53 65 74 20 3d 20 70 4d  C->pMaskSet = pM
2b25c 61 73 6b 53 65 74 3b 0a 20 20 70 57 43 2d 3e 6e  askSet;.  pWC->n
2b25d 54 65 72 6d 20 3d 20 30 3b 0a 20 20 70 57 43 2d  Term = 0;.  pWC-
2b25e 3e 6e 53 6c 6f 74 20 3d 20 41 72 72 61 79 53 69  >nSlot = ArraySi
2b25f 7a 65 28 70 57 43 2d 3e 61 53 74 61 74 69 63 29  ze(pWC->aStatic)
2b260 3b 0a 20 20 70 57 43 2d 3e 61 20 3d 20 70 57 43  ;.  pWC->a = pWC
2b261 2d 3e 61 53 74 61 74 69 63 3b 0a 7d 0a 0a 2f 2a  ->aStatic;.}../*
2b262 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
2b263 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ce */.static voi
2b264 64 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65  d whereClauseCle
2b265 61 72 28 57 68 65 72 65 43 6c 61 75 73 65 2a 29  ar(WhereClause*)
2b266 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63  ;../*.** Dealloc
2b267 61 74 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61  ate all memory a
2b268 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
2b269 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 6f 62 6a   WhereOrInfo obj
2b26a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ect..*/.static v
2b26b 6f 69 64 20 77 68 65 72 65 4f 72 49 6e 66 6f 44  oid whereOrInfoD
2b26c 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64  elete(sqlite3 *d
2b26d 62 2c 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a  b, WhereOrInfo *
2b26e 70 29 7b 0a 20 20 77 68 65 72 65 43 6c 61 75 73  p){.  whereClaus
2b26f 65 43 6c 65 61 72 28 26 70 2d 3e 77 63 29 3b 0a  eClear(&p->wc);.
2b270 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2b271 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  db, p);.}../*.**
2b272 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20   Deallocate all 
2b273 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
2b274 64 20 77 69 74 68 20 61 20 57 68 65 72 65 41 6e  d with a WhereAn
2b275 64 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a 2f  dInfo object..*/
2b276 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65  .static void whe
2b277 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65 74 65 28  reAndInfoDelete(
2b278 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65  sqlite3 *db, Whe
2b279 72 65 41 6e 64 49 6e 66 6f 20 2a 70 29 7b 0a 20  reAndInfo *p){. 
2b27a 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61   whereClauseClea
2b27b 72 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c  r(&p->wc);.  sql
2b27c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2b27d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c  );.}../*.** Deal
2b27e 6c 6f 63 61 74 65 20 61 20 57 68 65 72 65 43 6c  locate a WhereCl
2b27f 61 75 73 65 20 73 74 72 75 63 74 75 72 65 2e 20  ause structure. 
2b280 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   The WhereClause
2b281 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 74   structure.** it
2b282 73 65 6c 66 20 69 73 20 6e 6f 74 20 66 72 65 65  self is not free
2b283 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  d.  This routine
2b284 20 69 73 20 74 68 65 20 69 6e 76 65 72 73 65 20   is the inverse 
2b285 6f 66 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e  of whereClauseIn
2b286 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  it()..*/.static 
2b287 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73 65  void whereClause
2b288 43 6c 65 61 72 28 57 68 65 72 65 43 6c 61 75 73  Clear(WhereClaus
2b289 65 20 2a 70 57 43 29 7b 0a 20 20 69 6e 74 20 69  e *pWC){.  int i
2b28a 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61  ;.  WhereTerm *a
2b28b 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2b28c 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 2d 3e 64  = pWC->pParse->d
2b28d 62 3b 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e  b;.  for(i=pWC->
2b28e 6e 54 65 72 6d 2d 31 2c 20 61 3d 70 57 43 2d 3e  nTerm-1, a=pWC->
2b28f 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 61 2b  a; i>=0; i--, a+
2b290 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 2d 3e 77  +){.    if( a->w
2b291 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59  tFlags & TERM_DY
2b292 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73  NAMIC ){.      s
2b293 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2b294 28 64 62 2c 20 61 2d 3e 70 45 78 70 72 29 3b 0a  (db, a->pExpr);.
2b295 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 61 2d      }.    if( a-
2b296 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2b297 4f 52 49 4e 46 4f 20 29 7b 0a 20 20 20 20 20 20  ORINFO ){.      
2b298 77 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74  whereOrInfoDelet
2b299 65 28 64 62 2c 20 61 2d 3e 75 2e 70 4f 72 49 6e  e(db, a->u.pOrIn
2b29a 66 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  fo);.    }else i
2b29b 66 28 20 61 2d 3e 77 74 46 6c 61 67 73 20 26 20  f( a->wtFlags & 
2b29c 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 20 29 7b 0a  TERM_ANDINFO ){.
2b29d 20 20 20 20 20 20 77 68 65 72 65 41 6e 64 49 6e        whereAndIn
2b29e 66 6f 44 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e  foDelete(db, a->
2b29f 75 2e 70 41 6e 64 49 6e 66 6f 29 3b 0a 20 20 20  u.pAndInfo);.   
2b2a0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 57 43   }.  }.  if( pWC
2b2a1 2d 3e 61 21 3d 70 57 43 2d 3e 61 53 74 61 74 69  ->a!=pWC->aStati
2b2a2 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  c ){.    sqlite3
2b2a3 44 62 46 72 65 65 28 64 62 2c 20 70 57 43 2d 3e  DbFree(db, pWC->
2b2a4 61 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  a);.  }.}../*.**
2b2a5 20 41 64 64 20 61 20 73 69 6e 67 6c 65 20 6e 65   Add a single ne
2b2a6 77 20 57 68 65 72 65 54 65 72 6d 20 65 6e 74 72  w WhereTerm entr
2b2a7 79 20 74 6f 20 74 68 65 20 57 68 65 72 65 43 6c  y to the WhereCl
2b2a8 61 75 73 65 20 6f 62 6a 65 63 74 20 70 57 43 2e  ause object pWC.
2b2a9 0a 2a 2a 20 54 68 65 20 6e 65 77 20 57 68 65 72  .** The new Wher
2b2aa 65 54 65 72 6d 20 6f 62 6a 65 63 74 20 69 73 20  eTerm object is 
2b2ab 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d  constructed from
2b2ac 20 45 78 70 72 20 70 20 61 6e 64 20 77 69 74 68   Expr p and with
2b2ad 20 77 74 46 6c 61 67 73 2e 0a 2a 2a 20 54 68 65   wtFlags..** The
2b2ae 20 69 6e 64 65 78 20 69 6e 20 70 57 43 2d 3e 61   index in pWC->a
2b2af 5b 5d 20 6f 66 20 74 68 65 20 6e 65 77 20 57 68  [] of the new Wh
2b2b0 65 72 65 54 65 72 6d 20 69 73 20 72 65 74 75 72  ereTerm is retur
2b2b1 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a  ned on success..
2b2b2 2a 2a 20 30 20 69 73 20 72 65 74 75 72 6e 65 64  ** 0 is returned
2b2b3 20 69 66 20 74 68 65 20 6e 65 77 20 57 68 65 72   if the new Wher
2b2b4 65 54 65 72 6d 20 63 6f 75 6c 64 20 6e 6f 74 20  eTerm could not 
2b2b5 62 65 20 61 64 64 65 64 20 64 75 65 20 74 6f 20  be added due to 
2b2b6 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  a memory.** allo
2b2b7 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 20 20 54  cation error.  T
2b2b8 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
2b2b9 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 77 69 6c  tion failure wil
2b2ba 6c 20 62 65 20 72 65 63 6f 72 64 65 64 20 69 6e  l be recorded in
2b2bb 0a 2a 2a 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c  .** the db->mall
2b2bc 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 73 6f  ocFailed flag so
2b2bd 20 74 68 61 74 20 68 69 67 68 65 72 2d 6c 65 76   that higher-lev
2b2be 65 6c 20 66 75 6e 63 74 69 6f 6e 73 20 63 61 6e  el functions can
2b2bf 20 64 65 74 65 63 74 20 69 74 2e 0a 2a 2a 0a 2a   detect it..**.*
2b2c0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
2b2c1 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65  ill increase the
2b2c2 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 57 43   size of the pWC
2b2c3 2d 3e 61 5b 5d 20 61 72 72 61 79 20 61 73 20 6e  ->a[] array as n
2b2c4 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20  ecessary..**.** 
2b2c5 49 66 20 74 68 65 20 77 74 46 6c 61 67 73 20 61  If the wtFlags a
2b2c6 72 67 75 6d 65 6e 74 20 69 6e 63 6c 75 64 65 73  rgument includes
2b2c7 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 2c 20 74   TERM_DYNAMIC, t
2b2c8 68 65 6e 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  hen responsibili
2b2c9 74 79 0a 2a 2a 20 66 6f 72 20 66 72 65 65 69 6e  ty.** for freein
2b2ca 67 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  g the expression
2b2cb 20 70 20 69 73 20 61 73 73 75 6d 65 64 20 62 79   p is assumed by
2b2cc 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   the WhereClause
2b2cd 20 6f 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a 20   object pWC..** 
2b2ce 54 68 69 73 20 69 73 20 74 72 75 65 20 65 76 65  This is true eve
2b2cf 6e 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e  n if this routin
2b2d0 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63  e fails to alloc
2b2d1 61 74 65 20 61 20 6e 65 77 20 57 68 65 72 65 54  ate a new WhereT
2b2d2 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49  erm..**.** WARNI
2b2d3 4e 47 3a 20 20 54 68 69 73 20 72 6f 75 74 69 6e  NG:  This routin
2b2d4 65 20 6d 69 67 68 74 20 72 65 61 6c 6c 6f 63 61  e might realloca
2b2d5 74 65 20 74 68 65 20 73 70 61 63 65 20 75 73 65  te the space use
2b2d6 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 57 68  d to store.** Wh
2b2d7 65 72 65 54 65 72 6d 73 2e 20 20 41 6c 6c 20 70  ereTerms.  All p
2b2d8 6f 69 6e 74 65 72 73 20 74 6f 20 57 68 65 72 65  ointers to Where
2b2d9 54 65 72 6d 73 20 73 68 6f 75 6c 64 20 62 65 20  Terms should be 
2b2da 69 6e 76 61 6c 69 64 61 74 65 64 20 61 66 74 65  invalidated afte
2b2db 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69  r.** calling thi
2b2dc 73 20 72 6f 75 74 69 6e 65 2e 20 20 53 75 63 68  s routine.  Such
2b2dd 20 70 6f 69 6e 74 65 72 73 20 6d 61 79 20 62 65   pointers may be
2b2de 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20 62   reinitialized b
2b2df 79 20 72 65 66 65 72 65 6e 63 69 6e 67 0a 2a 2a  y referencing.**
2b2e0 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20 61 72   the pWC->a[] ar
2b2e1 72 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ray..*/.static i
2b2e2 6e 74 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e  nt whereClauseIn
2b2e3 73 65 72 74 28 57 68 65 72 65 43 6c 61 75 73 65  sert(WhereClause
2b2e4 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 2c 20   *pWC, Expr *p, 
2b2e5 75 38 20 77 74 46 6c 61 67 73 29 7b 0a 20 20 57  u8 wtFlags){.  W
2b2e6 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
2b2e7 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 69 66  .  int idx;.  if
2b2e8 28 20 70 57 43 2d 3e 6e 54 65 72 6d 3e 3d 70 57  ( pWC->nTerm>=pW
2b2e9 43 2d 3e 6e 53 6c 6f 74 20 29 7b 0a 20 20 20 20  C->nSlot ){.    
2b2ea 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 6c 64 20  WhereTerm *pOld 
2b2eb 3d 20 70 57 43 2d 3e 61 3b 0a 20 20 20 20 73 71  = pWC->a;.    sq
2b2ec 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 57 43 2d  lite3 *db = pWC-
2b2ed 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20  >pParse->db;.   
2b2ee 20 70 57 43 2d 3e 61 20 3d 20 73 71 6c 69 74 65   pWC->a = sqlite
2b2ef 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2b2f0 20 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30   sizeof(pWC->a[0
2b2f1 5d 29 2a 70 57 43 2d 3e 6e 53 6c 6f 74 2a 32 20  ])*pWC->nSlot*2 
2b2f2 29 3b 0a 20 20 20 20 69 66 28 20 70 57 43 2d 3e  );.    if( pWC->
2b2f3 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  a==0 ){.      if
2b2f4 28 20 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ( wtFlags & TERM
2b2f5 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20  _DYNAMIC ){.    
2b2f6 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2b2f7 65 6c 65 74 65 28 64 62 2c 20 70 29 3b 0a 20 20  elete(db, p);.  
2b2f8 20 20 20 20 7d 0a 20 20 20 20 20 20 70 57 43 2d      }.      pWC-
2b2f9 3e 61 20 3d 20 70 4f 6c 64 3b 0a 20 20 20 20 20  >a = pOld;.     
2b2fa 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
2b2fb 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57 43 2d  .    memcpy(pWC-
2b2fc 3e 61 2c 20 70 4f 6c 64 2c 20 73 69 7a 65 6f 66  >a, pOld, sizeof
2b2fd 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d  (pWC->a[0])*pWC-
2b2fe 3e 6e 54 65 72 6d 29 3b 0a 20 20 20 20 69 66 28  >nTerm);.    if(
2b2ff 20 70 4f 6c 64 21 3d 70 57 43 2d 3e 61 53 74 61   pOld!=pWC->aSta
2b300 74 69 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tic ){.      sql
2b301 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2b302 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Old);.    }.    
2b303 70 57 43 2d 3e 6e 53 6c 6f 74 20 3d 20 73 71 6c  pWC->nSlot = sql
2b304 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
2b305 28 64 62 2c 20 70 57 43 2d 3e 61 29 2f 73 69 7a  (db, pWC->a)/siz
2b306 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 3b 0a  eof(pWC->a[0]);.
2b307 20 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26 70    }.  pTerm = &p
2b308 57 43 2d 3e 61 5b 69 64 78 20 3d 20 70 57 43 2d  WC->a[idx = pWC-
2b309 3e 6e 54 65 72 6d 2b 2b 5d 3b 0a 20 20 70 54 65  >nTerm++];.  pTe
2b30a 72 6d 2d 3e 70 45 78 70 72 20 3d 20 70 3b 0a 20  rm->pExpr = p;. 
2b30b 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2b30c 3d 20 77 74 46 6c 61 67 73 3b 0a 20 20 70 54 65  = wtFlags;.  pTe
2b30d 72 6d 2d 3e 70 57 43 20 3d 20 70 57 43 3b 0a 20  rm->pWC = pWC;. 
2b30e 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20   pTerm->iParent 
2b30f 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 69  = -1;.  return i
2b310 64 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  dx;.}../*.** Thi
2b311 73 20 72 6f 75 74 69 6e 65 20 69 64 65 6e 74 69  s routine identi
2b312 66 69 65 73 20 73 75 62 65 78 70 72 65 73 73 69  fies subexpressi
2b313 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 45  ons in the WHERE
2b314 20 63 6c 61 75 73 65 20 77 68 65 72 65 0a 2a 2a   clause where.**
2b315 20 65 61 63 68 20 73 75 62 65 78 70 72 65 73 73   each subexpress
2b316 69 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65 64  ion is separated
2b317 20 62 79 20 74 68 65 20 41 4e 44 20 6f 70 65 72   by the AND oper
2b318 61 74 6f 72 20 6f 72 20 73 6f 6d 65 20 6f 74 68  ator or some oth
2b319 65 72 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 73  er.** operator s
2b31a 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
2b31b 6f 70 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54  op parameter.  T
2b31c 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73  he WhereClause s
2b31d 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 66  tructure.** is f
2b31e 69 6c 6c 65 64 20 77 69 74 68 20 70 6f 69 6e 74  illed with point
2b31f 65 72 73 20 74 6f 20 73 75 62 65 78 70 72 65 73  ers to subexpres
2b320 73 69 6f 6e 73 2e 20 20 46 6f 72 20 65 78 61 6d  sions.  For exam
2b321 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 57 48  ple:.**.**    WH
2b322 45 52 45 20 20 61 3d 3d 27 68 65 6c 6c 6f 27 20  ERE  a=='hello' 
2b323 41 4e 44 20 63 6f 61 6c 65 73 63 65 28 62 2c 31  AND coalesce(b,1
2b324 31 29 3c 31 30 20 41 4e 44 20 28 63 2b 31 32 21  1)<10 AND (c+12!
2b325 3d 64 20 4f 52 20 63 3d 3d 32 32 29 0a 2a 2a 20  =d OR c==22).** 
2b326 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f            \_____
2b327 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  ___/     \______
2b328 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c  _________/     \
2b329 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2b32a 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  /.**            
2b32b 73 6c 6f 74 5b 30 5d 20 20 20 20 20 20 20 20 20  slot[0]         
2b32c 20 20 20 73 6c 6f 74 5b 31 5d 20 20 20 20 20 20     slot[1]      
2b32d 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 32 5d           slot[2]
2b32e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69  .**.** The origi
2b32f 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65  nal WHERE clause
2b330 20 69 6e 20 70 45 78 70 72 20 69 73 20 75 6e 61   in pExpr is una
2b331 6c 74 65 72 65 64 2e 20 20 41 6c 6c 20 74 68 69  ltered.  All thi
2b332 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 6f 65  s routine.** doe
2b333 73 20 69 73 20 6d 61 6b 65 20 73 6c 6f 74 5b 5d  s is make slot[]
2b334 20 65 6e 74 72 69 65 73 20 70 6f 69 6e 74 20 74   entries point t
2b335 6f 20 73 75 62 73 74 72 75 63 74 75 72 65 20 77  o substructure w
2b336 69 74 68 69 6e 20 70 45 78 70 72 2e 0a 2a 2a 0a  ithin pExpr..**.
2b337 2a 2a 20 49 6e 20 74 68 65 20 70 72 65 76 69 6f  ** In the previo
2b338 75 73 20 73 65 6e 74 65 6e 63 65 20 61 6e 64 20  us sentence and 
2b339 69 6e 20 74 68 65 20 64 69 61 67 72 61 6d 2c 20  in the diagram, 
2b33a 22 73 6c 6f 74 5b 5d 22 20 72 65 66 65 72 73 20  "slot[]" refers 
2b33b 74 6f 0a 2a 2a 20 74 68 65 20 57 68 65 72 65 43  to.** the WhereC
2b33c 6c 61 75 73 65 2e 61 5b 5d 20 61 72 72 61 79 2e  lause.a[] array.
2b33d 20 20 54 68 65 20 73 6c 6f 74 5b 5d 20 61 72 72    The slot[] arr
2b33e 61 79 20 67 72 6f 77 73 20 61 73 20 6e 65 65 64  ay grows as need
2b33f 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 2a 2a  ed to contain.**
2b340 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68   all terms of th
2b341 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a  e WHERE clause..
2b342 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
2b343 68 65 72 65 53 70 6c 69 74 28 57 68 65 72 65 43  hereSplit(WhereC
2b344 6c 61 75 73 65 20 2a 70 57 43 2c 20 45 78 70 72  lause *pWC, Expr
2b345 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6f 70 29   *pExpr, int op)
2b346 7b 0a 20 20 70 57 43 2d 3e 6f 70 20 3d 20 28 75  {.  pWC->op = (u
2b347 38 29 6f 70 3b 0a 20 20 69 66 28 20 70 45 78 70  8)op;.  if( pExp
2b348 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  r==0 ) return;. 
2b349 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
2b34a 6f 70 20 29 7b 0a 20 20 20 20 77 68 65 72 65 43  op ){.    whereC
2b34b 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
2b34c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d 65   pExpr, 0);.  }e
2b34d 6c 73 65 7b 0a 20 20 20 20 77 68 65 72 65 53 70  lse{.    whereSp
2b34e 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72 2d 3e  lit(pWC, pExpr->
2b34f 70 4c 65 66 74 2c 20 6f 70 29 3b 0a 20 20 20 20  pLeft, op);.    
2b350 77 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20  whereSplit(pWC, 
2b351 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f  pExpr->pRight, o
2b352 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
2b353 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 20 65   Initialize an e
2b354 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73  xpression mask s
2b355 65 74 20 28 61 20 57 68 65 72 65 4d 61 73 6b 53  et (a WhereMaskS
2b356 65 74 20 6f 62 6a 65 63 74 29 0a 2a 2f 0a 23 64  et object).*/.#d
2b357 65 66 69 6e 65 20 69 6e 69 74 4d 61 73 6b 53 65  efine initMaskSe
2b358 74 28 50 29 20 20 6d 65 6d 73 65 74 28 50 2c 20  t(P)  memset(P, 
2b359 30 2c 20 73 69 7a 65 6f 66 28 2a 50 29 29 0a 0a  0, sizeof(*P))..
2b35a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
2b35b 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74 68 65   bitmask for the
2b35c 20 67 69 76 65 6e 20 63 75 72 73 6f 72 20 6e 75   given cursor nu
2b35d 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 20 30 20  mber.  Return 0 
2b35e 69 66 0a 2a 2a 20 69 43 75 72 73 6f 72 20 69 73  if.** iCursor is
2b35f 20 6e 6f 74 20 69 6e 20 74 68 65 20 73 65 74 2e   not in the set.
2b360 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61  .*/.static Bitma
2b361 73 6b 20 67 65 74 4d 61 73 6b 28 57 68 65 72 65  sk getMask(Where
2b362 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2b363 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b  t, int iCursor){
2b364 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
2b365 72 74 28 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 3c  rt( pMaskSet->n<
2b366 3d 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29  =sizeof(Bitmask)
2b367 2a 38 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  *8 );.  for(i=0;
2b368 20 69 3c 70 4d 61 73 6b 53 65 74 2d 3e 6e 3b 20   i<pMaskSet->n; 
2b369 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4d  i++){.    if( pM
2b36a 61 73 6b 53 65 74 2d 3e 69 78 5b 69 5d 3d 3d 69  askSet->ix[i]==i
2b36b 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20  Cursor ){.      
2b36c 72 65 74 75 72 6e 20 28 28 42 69 74 6d 61 73 6b  return ((Bitmask
2b36d 29 31 29 3c 3c 69 3b 0a 20 20 20 20 7d 0a 20 20  )1)<<i;.    }.  
2b36e 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2b36f 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
2b370 6e 65 77 20 6d 61 73 6b 20 66 6f 72 20 63 75 72  new mask for cur
2b371 73 6f 72 20 69 43 75 72 73 6f 72 2e 0a 2a 2a 0a  sor iCursor..**.
2b372 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20  ** There is one 
2b373 63 75 72 73 6f 72 20 70 65 72 20 74 61 62 6c 65  cursor per table
2b374 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
2b375 75 73 65 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  use.  The number
2b376 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 69 6e   of.** tables in
2b377 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2b378 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20 61   is limited by a
2b379 20 74 65 73 74 20 65 61 72 6c 79 20 69 6e 20 74   test early in t
2b37a 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68 65  he.** sqlite3Whe
2b37b 72 65 42 65 67 69 6e 28 29 20 72 6f 75 74 69 6e  reBegin() routin
2b37c 65 2e 20 20 53 6f 20 77 65 20 6b 6e 6f 77 20 74  e.  So we know t
2b37d 68 61 74 20 74 68 65 20 70 4d 61 73 6b 53 65 74  hat the pMaskSet
2b37e 2d 3e 69 78 5b 5d 0a 2a 2a 20 61 72 72 61 79 20  ->ix[].** array 
2b37f 77 69 6c 6c 20 6e 65 76 65 72 20 6f 76 65 72 66  will never overf
2b380 6c 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  low..*/.static v
2b381 6f 69 64 20 63 72 65 61 74 65 4d 61 73 6b 28 57  oid createMask(W
2b382 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
2b383 73 6b 53 65 74 2c 20 69 6e 74 20 69 43 75 72 73  skSet, int iCurs
2b384 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  or){.  assert( p
2b385 4d 61 73 6b 53 65 74 2d 3e 6e 20 3c 20 41 72 72  MaskSet->n < Arr
2b386 61 79 53 69 7a 65 28 70 4d 61 73 6b 53 65 74 2d  aySize(pMaskSet-
2b387 3e 69 78 29 20 29 3b 0a 20 20 70 4d 61 73 6b 53  >ix) );.  pMaskS
2b388 65 74 2d 3e 69 78 5b 70 4d 61 73 6b 53 65 74 2d  et->ix[pMaskSet-
2b389 3e 6e 2b 2b 5d 20 3d 20 69 43 75 72 73 6f 72 3b  >n++] = iCursor;
2b38a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2b38b 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 28 72 65  outine walks (re
2b38c 63 75 72 73 69 76 65 6c 79 29 20 61 6e 20 65 78  cursively) an ex
2b38d 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e  pression tree an
2b38e 64 20 67 65 6e 65 72 61 74 65 73 0a 2a 2a 20 61  d generates.** a
2b38f 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74   bitmask indicat
2b390 69 6e 67 20 77 68 69 63 68 20 74 61 62 6c 65 73  ing which tables
2b391 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68 61   are used in tha
2b392 74 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  t expression.** 
2b393 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f  tree..**.** In o
2b394 72 64 65 72 20 66 6f 72 20 74 68 69 73 20 72 6f  rder for this ro
2b395 75 74 69 6e 65 20 74 6f 20 77 6f 72 6b 2c 20 74  utine to work, t
2b396 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
2b397 69 6f 6e 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a  ion must have.**
2b398 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 76 6f   previously invo
2b399 6b 65 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  ked sqlite3Resol
2b39a 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 6f 6e  veExprNames() on
2b39b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e   the expression.
2b39c 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 68 65 61    See.** the hea
2b39d 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74  der comment on t
2b39e 68 61 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  hat routine for 
2b39f 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
2b3a0 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 73  mation..** The s
2b3a1 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
2b3a2 72 4e 61 6d 65 73 28 29 20 72 6f 75 74 69 6e 65  rNames() routine
2b3a3 73 20 6c 6f 6f 6b 73 20 66 6f 72 20 63 6f 6c 75  s looks for colu
2b3a4 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 0a 2a 2a 20  mn names and.** 
2b3a5 73 65 74 73 20 74 68 65 69 72 20 6f 70 63 6f 64  sets their opcod
2b3a6 65 73 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20  es to TK_COLUMN 
2b3a7 61 6e 64 20 74 68 65 69 72 20 45 78 70 72 2e 69  and their Expr.i
2b3a8 54 61 62 6c 65 20 66 69 65 6c 64 73 20 74 6f 0a  Table fields to.
2b3a9 2a 2a 20 74 68 65 20 56 44 42 45 20 63 75 72 73  ** the VDBE curs
2b3aa 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  or number of the
2b3ab 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f   table.  This ro
2b3ac 75 74 69 6e 65 20 6a 75 73 74 20 68 61 73 20 74  utine just has t
2b3ad 6f 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65 20 74  o.** translate t
2b3ae 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
2b3af 73 20 69 6e 74 6f 20 62 69 74 6d 61 73 6b 20 76  s into bitmask v
2b3b0 61 6c 75 65 73 20 61 6e 64 20 4f 52 20 61 6c 6c  alues and OR all
2b3b1 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 73 6b 73  .** the bitmasks
2b3b2 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 73 74   together..*/.st
2b3b3 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70  atic Bitmask exp
2b3b4 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
2b3b5 57 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20 45  WhereMaskSet*, E
2b3b6 78 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69  xprList*);.stati
2b3b7 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53 65  c Bitmask exprSe
2b3b8 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 57  lectTableUsage(W
2b3b9 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20 53 65  hereMaskSet*, Se
2b3ba 6c 65 63 74 2a 29 3b 0a 73 74 61 74 69 63 20 42  lect*);.static B
2b3bb 69 74 6d 61 73 6b 20 65 78 70 72 54 61 62 6c 65  itmask exprTable
2b3bc 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53  Usage(WhereMaskS
2b3bd 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 45 78  et *pMaskSet, Ex
2b3be 70 72 20 2a 70 29 7b 0a 20 20 42 69 74 6d 61 73  pr *p){.  Bitmas
2b3bf 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66  k mask = 0;.  if
2b3c0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
2b3c1 30 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  0;.  if( p->op==
2b3c2 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  TK_COLUMN ){.   
2b3c3 20 6d 61 73 6b 20 3d 20 67 65 74 4d 61 73 6b 28   mask = getMask(
2b3c4 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 69 54 61  pMaskSet, p->iTa
2b3c5 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ble);.    return
2b3c6 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 6d 61 73   mask;.  }.  mas
2b3c7 6b 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  k = exprTableUsa
2b3c8 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e  ge(pMaskSet, p->
2b3c9 70 52 69 67 68 74 29 3b 0a 20 20 6d 61 73 6b 20  pRight);.  mask 
2b3ca 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67  |= exprTableUsag
2b3cb 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70  e(pMaskSet, p->p
2b3cc 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 45 78 70  Left);.  if( Exp
2b3cd 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
2b3ce 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
2b3cf 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
2b3d0 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67  rSelectTableUsag
2b3d1 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 78  e(pMaskSet, p->x
2b3d2 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 7d 65 6c  .pSelect);.  }el
2b3d3 73 65 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20  se{.    mask |= 
2b3d4 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61  exprListTableUsa
2b3d5 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e  ge(pMaskSet, p->
2b3d6 78 2e 70 4c 69 73 74 29 3b 0a 20 20 7d 0a 20 20  x.pList);.  }.  
2b3d7 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73  return mask;.}.s
2b3d8 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78  tatic Bitmask ex
2b3d9 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65  prListTableUsage
2b3da 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70  (WhereMaskSet *p
2b3db 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 4c 69 73  MaskSet, ExprLis
2b3dc 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74  t *pList){.  int
2b3dd 20 69 3b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61   i;.  Bitmask ma
2b3de 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4c  sk = 0;.  if( pL
2b3df 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ist ){.    for(i
2b3e0 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
2b3e1 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
2b3e2 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c  mask |= exprTabl
2b3e3 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
2b3e4 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
2b3e5 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  pr);.    }.  }. 
2b3e6 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a   return mask;.}.
2b3e7 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65  static Bitmask e
2b3e8 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73  xprSelectTableUs
2b3e9 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74  age(WhereMaskSet
2b3ea 20 2a 70 4d 61 73 6b 53 65 74 2c 20 53 65 6c 65   *pMaskSet, Sele
2b3eb 63 74 20 2a 70 53 29 7b 0a 20 20 42 69 74 6d 61  ct *pS){.  Bitma
2b3ec 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 77  sk mask = 0;.  w
2b3ed 68 69 6c 65 28 20 70 53 20 29 7b 0a 20 20 20 20  hile( pS ){.    
2b3ee 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74  mask |= exprList
2b3ef 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2b3f0 53 65 74 2c 20 70 53 2d 3e 70 45 4c 69 73 74 29  Set, pS->pEList)
2b3f1 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78  ;.    mask |= ex
2b3f2 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65  prListTableUsage
2b3f3 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70  (pMaskSet, pS->p
2b3f4 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 6d 61  GroupBy);.    ma
2b3f5 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61  sk |= exprListTa
2b3f6 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2b3f7 74 2c 20 70 53 2d 3e 70 4f 72 64 65 72 42 79 29  t, pS->pOrderBy)
2b3f8 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78  ;.    mask |= ex
2b3f9 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
2b3fa 73 6b 53 65 74 2c 20 70 53 2d 3e 70 57 68 65 72  skSet, pS->pWher
2b3fb 65 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20  e);.    mask |= 
2b3fc 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2b3fd 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 48 61  MaskSet, pS->pHa
2b3fe 76 69 6e 67 29 3b 0a 20 20 20 20 70 53 20 3d 20  ving);.    pS = 
2b3ff 70 53 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a  pS->pPrior;.  }.
2b400 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d    return mask;.}
2b401 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
2b402 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e  RUE if the given
2b403 20 6f 70 65 72 61 74 6f 72 20 69 73 20 6f 6e 65   operator is one
2b404 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 6f 72   of the operator
2b405 73 20 74 68 61 74 20 69 73 0a 2a 2a 20 61 6c 6c  s that is.** all
2b406 6f 77 65 64 20 66 6f 72 20 61 6e 20 69 6e 64 65  owed for an inde
2b407 78 61 62 6c 65 20 57 48 45 52 45 20 63 6c 61 75  xable WHERE clau
2b408 73 65 20 74 65 72 6d 2e 20 20 54 68 65 20 61 6c  se term.  The al
2b409 6c 6f 77 65 64 20 6f 70 65 72 61 74 6f 72 73 20  lowed operators 
2b40a 61 72 65 0a 2a 2a 20 22 3d 22 2c 20 22 3c 22 2c  are.** "=", "<",
2b40b 20 22 3e 22 2c 20 22 3c 3d 22 2c 20 22 3e 3d 22   ">", "<=", ">="
2b40c 2c 20 61 6e 64 20 22 49 4e 22 2e 0a 2a 2f 0a 73  , and "IN"..*/.s
2b40d 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 77 65  tatic int allowe
2b40e 64 4f 70 28 69 6e 74 20 6f 70 29 7b 0a 20 20 61  dOp(int op){.  a
2b40f 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f  ssert( TK_GT>TK_
2b410 45 51 20 26 26 20 54 4b 5f 47 54 3c 54 4b 5f 47  EQ && TK_GT<TK_G
2b411 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54  E );.  assert( T
2b412 4b 5f 4c 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b  K_LT>TK_EQ && TK
2b413 5f 4c 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61  _LT<TK_GE );.  a
2b414 73 73 65 72 74 28 20 54 4b 5f 4c 45 3e 54 4b 5f  ssert( TK_LE>TK_
2b415 45 51 20 26 26 20 54 4b 5f 4c 45 3c 54 4b 5f 47  EQ && TK_LE<TK_G
2b416 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54  E );.  assert( T
2b417 4b 5f 47 45 3d 3d 54 4b 5f 45 51 2b 34 20 29 3b  K_GE==TK_EQ+4 );
2b418 0a 20 20 72 65 74 75 72 6e 20 6f 70 3d 3d 54 4b  .  return op==TK
2b419 5f 49 4e 20 7c 7c 20 28 6f 70 3e 3d 54 4b 5f 45  _IN || (op>=TK_E
2b41a 51 20 26 26 20 6f 70 3c 3d 54 4b 5f 47 45 29 20  Q && op<=TK_GE) 
2b41b 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c  || op==TK_ISNULL
2b41c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20  ;.}../*.** Swap 
2b41d 74 77 6f 20 6f 62 6a 65 63 74 73 20 6f 66 20 74  two objects of t
2b41e 79 70 65 20 54 59 50 45 2e 0a 2a 2f 0a 23 64 65  ype TYPE..*/.#de
2b41f 66 69 6e 65 20 53 57 41 50 28 54 59 50 45 2c 41  fine SWAP(TYPE,A
2b420 2c 42 29 20 7b 54 59 50 45 20 74 3d 41 3b 20 41  ,B) {TYPE t=A; A
2b421 3d 42 3b 20 42 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a  =B; B=t;}../*.**
2b422 20 43 6f 6d 6d 75 74 65 20 61 20 63 6f 6d 70 61   Commute a compa
2b423 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20  rison operator. 
2b424 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20   Expressions of 
2b425 74 68 65 20 66 6f 72 6d 20 22 58 20 6f 70 20 59  the form "X op Y
2b426 22 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74  ".** are convert
2b427 65 64 20 69 6e 74 6f 20 22 59 20 6f 70 20 58 22  ed into "Y op X"
2b428 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 6f 6c  ..**.** If a col
2b429 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2b42a 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  is associated wi
2b42b 74 68 20 65 69 74 68 65 72 20 74 68 65 20 6c 65  th either the le
2b42c 66 74 20 6f 72 20 72 69 67 68 74 0a 2a 2a 20 73  ft or right.** s
2b42d 69 64 65 20 6f 66 20 74 68 65 20 63 6f 6d 70 61  ide of the compa
2b42e 72 69 73 6f 6e 2c 20 69 74 20 72 65 6d 61 69 6e  rison, it remain
2b42f 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2b430 68 20 74 68 65 20 73 61 6d 65 20 73 69 64 65 20  h the same side 
2b431 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 63 6f 6d  after.** the com
2b432 6d 75 74 61 74 69 6f 6e 2e 20 53 6f 20 22 59 20  mutation. So "Y 
2b433 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f  collate NOCASE o
2b434 70 20 58 22 20 62 65 63 6f 6d 65 73 20 0a 2a 2a  p X" becomes .**
2b435 20 22 58 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41   "X collate NOCA
2b436 53 45 20 6f 70 20 59 22 2e 20 54 68 69 73 20 69  SE op Y". This i
2b437 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 63 6f  s because any co
2b438 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2b439 20 6f 6e 0a 2a 2a 20 74 68 65 20 6c 65 66 74 20   on.** the left 
2b43a 68 61 6e 64 20 73 69 64 65 20 6f 66 20 61 20 63  hand side of a c
2b43b 6f 6d 70 61 72 69 73 6f 6e 20 6f 76 65 72 72 69  omparison overri
2b43c 64 65 73 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f  des any collatio
2b43d 6e 20 73 65 71 75 65 6e 63 65 20 0a 2a 2a 20 61  n sequence .** a
2b43e 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 72  ttached to the r
2b43f 69 67 68 74 2e 20 46 6f 72 20 74 68 65 20 73 61  ight. For the sa
2b440 6d 65 20 72 65 61 73 6f 6e 20 74 68 65 20 45 50  me reason the EP
2b441 5f 45 78 70 43 6f 6c 6c 61 74 65 20 66 6c 61 67  _ExpCollate flag
2b442 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6d 6d 75  .** is not commu
2b443 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ted..*/.static v
2b444 6f 69 64 20 65 78 70 72 43 6f 6d 6d 75 74 65 28  oid exprCommute(
2b445 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
2b446 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 75  xpr *pExpr){.  u
2b447 31 36 20 65 78 70 52 69 67 68 74 20 3d 20 28 70  16 expRight = (p
2b448 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c  Expr->pRight->fl
2b449 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
2b44a 61 74 65 29 3b 0a 20 20 75 31 36 20 65 78 70 4c  ate);.  u16 expL
2b44b 65 66 74 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c  eft = (pExpr->pL
2b44c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  eft->flags & EP_
2b44d 45 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 61  ExpCollate);.  a
2b44e 73 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70  ssert( allowedOp
2b44f 28 70 45 78 70 72 2d 3e 6f 70 29 20 26 26 20 70  (pExpr->op) && p
2b450 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 4e 20  Expr->op!=TK_IN 
2b451 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67  );.  pExpr->pRig
2b452 68 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ht->pColl = sqli
2b453 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
2b454 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
2b455 69 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e  ight);.  pExpr->
2b456 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73  pLeft->pColl = s
2b457 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
2b458 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  q(pParse, pExpr-
2b459 3e 70 4c 65 66 74 29 3b 0a 20 20 53 57 41 50 28  >pLeft);.  SWAP(
2b45a 43 6f 6c 6c 53 65 71 2a 2c 70 45 78 70 72 2d 3e  CollSeq*,pExpr->
2b45b 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 2c 70 45  pRight->pColl,pE
2b45c 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c  xpr->pLeft->pCol
2b45d 6c 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69  l);.  pExpr->pRi
2b45e 67 68 74 2d 3e 66 6c 61 67 73 20 3d 20 28 70 45  ght->flags = (pE
2b45f 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61  xpr->pRight->fla
2b460 67 73 20 26 20 7e 45 50 5f 45 78 70 43 6f 6c 6c  gs & ~EP_ExpColl
2b461 61 74 65 29 20 7c 20 65 78 70 4c 65 66 74 3b 0a  ate) | expLeft;.
2b462 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e    pExpr->pLeft->
2b463 66 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e  flags = (pExpr->
2b464 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 7e  pLeft->flags & ~
2b465 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c  EP_ExpCollate) |
2b466 20 65 78 70 52 69 67 68 74 3b 0a 20 20 53 57 41   expRight;.  SWA
2b467 50 28 45 78 70 72 2a 2c 70 45 78 70 72 2d 3e 70  P(Expr*,pExpr->p
2b468 52 69 67 68 74 2c 70 45 78 70 72 2d 3e 70 4c 65  Right,pExpr->pLe
2b469 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72  ft);.  if( pExpr
2b46a 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 29 7b 0a 20  ->op>=TK_GT ){. 
2b46b 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54     assert( TK_LT
2b46c 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b 0a 20 20 20  ==TK_GT+2 );.   
2b46d 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d   assert( TK_GE==
2b46e 54 4b 5f 4c 45 2b 32 20 29 3b 0a 20 20 20 20 61  TK_LE+2 );.    a
2b46f 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f  ssert( TK_GT>TK_
2b470 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  EQ );.    assert
2b471 28 20 54 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29 3b  ( TK_GT<TK_LE );
2b472 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
2b473 70 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 26 26  pr->op>=TK_GT &&
2b474 20 70 45 78 70 72 2d 3e 6f 70 3c 3d 54 4b 5f 47   pExpr->op<=TK_G
2b475 45 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e  E );.    pExpr->
2b476 6f 70 20 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70  op = ((pExpr->op
2b477 2d 54 4b 5f 47 54 29 5e 32 29 2b 54 4b 5f 47 54  -TK_GT)^2)+TK_GT
2b478 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2b479 72 61 6e 73 6c 61 74 65 20 66 72 6f 6d 20 54 4b  ranslate from TK
2b47a 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  _xx operator to 
2b47b 57 4f 5f 78 78 20 62 69 74 6d 61 73 6b 2e 0a 2a  WO_xx bitmask..*
2b47c 2f 0a 73 74 61 74 69 63 20 75 31 36 20 6f 70 65  /.static u16 ope
2b47d 72 61 74 6f 72 4d 61 73 6b 28 69 6e 74 20 6f 70  ratorMask(int op
2b47e 29 7b 0a 20 20 75 31 36 20 63 3b 0a 20 20 61 73  ){.  u16 c;.  as
2b47f 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28  sert( allowedOp(
2b480 6f 70 29 20 29 3b 0a 20 20 69 66 28 20 6f 70 3d  op) );.  if( op=
2b481 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 63 20  =TK_IN ){.    c 
2b482 3d 20 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65  = WO_IN;.  }else
2b483 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55   if( op==TK_ISNU
2b484 4c 4c 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f  LL ){.    c = WO
2b485 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65  _ISNULL;.  }else
2b486 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 57  {.    assert( (W
2b487 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29  O_EQ<<(op-TK_EQ)
2b488 29 20 3c 20 30 78 37 66 66 66 20 29 3b 0a 20 20  ) < 0x7fff );.  
2b489 20 20 63 20 3d 20 28 75 31 36 29 28 57 4f 5f 45    c = (u16)(WO_E
2b48a 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 3b 0a  Q<<(op-TK_EQ));.
2b48b 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 70    }.  assert( op
2b48c 21 3d 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 63  !=TK_ISNULL || c
2b48d 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20  ==WO_ISNULL );. 
2b48e 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2b48f 49 4e 20 7c 7c 20 63 3d 3d 57 4f 5f 49 4e 20 29  IN || c==WO_IN )
2b490 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
2b491 54 4b 5f 45 51 20 7c 7c 20 63 3d 3d 57 4f 5f 45  TK_EQ || c==WO_E
2b492 51 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  Q );.  assert( o
2b493 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20 63 3d 3d 57  p!=TK_LT || c==W
2b494 4f 5f 4c 54 20 29 3b 0a 20 20 61 73 73 65 72 74  O_LT );.  assert
2b495 28 20 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 63  ( op!=TK_LE || c
2b496 3d 3d 57 4f 5f 4c 45 20 29 3b 0a 20 20 61 73 73  ==WO_LE );.  ass
2b497 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 54 20 7c  ert( op!=TK_GT |
2b498 7c 20 63 3d 3d 57 4f 5f 47 54 20 29 3b 0a 20 20  | c==WO_GT );.  
2b499 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47  assert( op!=TK_G
2b49a 45 20 7c 7c 20 63 3d 3d 57 4f 5f 47 45 20 29 3b  E || c==WO_GE );
2b49b 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a  .  return c;.}..
2b49c 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f 72  /*.** Search for
2b49d 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57   a term in the W
2b49e 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74  HERE clause that
2b49f 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20   is of the form 
2b4a0 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 0a  "X <op> <expr>".
2b4a1 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20 61 20  ** where X is a 
2b4a2 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
2b4a3 20 69 43 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c   iColumn of tabl
2b4a4 65 20 69 43 75 72 20 61 6e 64 20 3c 6f 70 3e 20  e iCur and <op> 
2b4a5 69 73 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65  is one of.** the
2b4a6 20 57 4f 5f 78 78 20 6f 70 65 72 61 74 6f 72 20   WO_xx operator 
2b4a7 63 6f 64 65 73 20 73 70 65 63 69 66 69 65 64 20  codes specified 
2b4a8 62 79 20 74 68 65 20 6f 70 20 70 61 72 61 6d 65  by the op parame
2b4a9 74 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ter..** Return a
2b4aa 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2b4ab 74 65 72 6d 2e 20 20 52 65 74 75 72 6e 20 30 20  term.  Return 0 
2b4ac 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f  if not found..*/
2b4ad 0a 73 74 61 74 69 63 20 57 68 65 72 65 54 65 72  .static WhereTer
2b4ae 6d 20 2a 66 69 6e 64 54 65 72 6d 28 0a 20 20 57  m *findTerm(.  W
2b4af 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
2b4b0 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2b4b1 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 73  E clause to be s
2b4b2 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74  earched */.  int
2b4b3 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20   iCur,          
2b4b4 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
2b4b5 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20  ber of LHS */.  
2b4b6 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20  int iColumn,    
2b4b7 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20        /* Column 
2b4b8 6e 75 6d 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f  number of LHS */
2b4b9 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65  .  Bitmask notRe
2b4ba 61 64 79 2c 20 20 20 20 20 2f 2a 20 52 48 53 20  ady,     /* RHS 
2b4bb 6d 75 73 74 20 6e 6f 74 20 6f 76 65 72 6c 61 70  must not overlap
2b4bc 20 77 69 74 68 20 74 68 69 73 20 6d 61 73 6b 20   with this mask 
2b4bd 2a 2f 0a 20 20 75 33 32 20 6f 70 2c 20 20 20 20  */.  u32 op,    
2b4be 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
2b4bf 73 6b 20 6f 66 20 57 4f 5f 78 78 20 76 61 6c 75  sk of WO_xx valu
2b4c0 65 73 20 64 65 73 63 72 69 62 69 6e 67 20 6f 70  es describing op
2b4c1 65 72 61 74 6f 72 20 2a 2f 0a 20 20 49 6e 64 65  erator */.  Inde
2b4c2 78 20 2a 70 49 64 78 20 20 20 20 20 20 20 20 20  x *pIdx         
2b4c3 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 63 6f 6d    /* Must be com
2b4c4 70 61 74 69 62 6c 65 20 77 69 74 68 20 74 68 69  patible with thi
2b4c5 73 20 69 6e 64 65 78 2c 20 69 66 20 6e 6f 74 20  s index, if not 
2b4c6 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 57 68 65  NULL */.){.  Whe
2b4c7 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20  reTerm *pTerm;. 
2b4c8 20 69 6e 74 20 6b 3b 0a 20 20 61 73 73 65 72 74   int k;.  assert
2b4c9 28 20 69 43 75 72 3e 3d 30 20 29 3b 0a 20 20 6f  ( iCur>=0 );.  o
2b4ca 70 20 26 3d 20 57 4f 5f 41 4c 4c 3b 0a 20 20 66  p &= WO_ALL;.  f
2b4cb 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c  or(pTerm=pWC->a,
2b4cc 20 6b 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6b   k=pWC->nTerm; k
2b4cd 3b 20 6b 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b  ; k--, pTerm++){
2b4ce 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e  .    if( pTerm->
2b4cf 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72  leftCursor==iCur
2b4d0 0a 20 20 20 20 20 20 20 26 26 20 28 70 54 65 72  .       && (pTer
2b4d1 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 26  m->prereqRight &
2b4d2 20 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a 20 20   notReady)==0.  
2b4d3 20 20 20 20 20 26 26 20 70 54 65 72 6d 2d 3e 75       && pTerm->u
2b4d4 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f  .leftColumn==iCo
2b4d5 6c 75 6d 6e 0a 20 20 20 20 20 20 20 26 26 20 28  lumn.       && (
2b4d6 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2b4d7 20 26 20 6f 70 29 21 3d 30 0a 20 20 20 20 29 7b   & op)!=0.    ){
2b4d8 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78 20  .      if( pIdx 
2b4d9 26 26 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  && pTerm->eOpera
2b4da 74 6f 72 21 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29  tor!=WO_ISNULL )
2b4db 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a  {.        Expr *
2b4dc 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pX = pTerm->pExp
2b4dd 72 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53  r;.        CollS
2b4de 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  eq *pColl;.     
2b4df 20 20 20 63 68 61 72 20 69 64 78 61 66 66 3b 0a     char idxaff;.
2b4e0 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20          int j;. 
2b4e1 20 20 20 20 20 20 20 50 61 72 73 65 20 2a 70 50         Parse *pP
2b4e2 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72  arse = pWC->pPar
2b4e3 73 65 3b 0a 0a 20 20 20 20 20 20 20 20 69 64 78  se;..        idx
2b4e4 61 66 66 20 3d 20 70 49 64 78 2d 3e 70 54 61 62  aff = pIdx->pTab
2b4e5 6c 65 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e  le->aCol[iColumn
2b4e6 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20  ].affinity;.    
2b4e7 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
2b4e8 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 28  IndexAffinityOk(
2b4e9 70 58 2c 20 69 64 78 61 66 66 29 20 29 20 63 6f  pX, idxaff) ) co
2b4ea 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 20  ntinue;..       
2b4eb 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74   /* Figure out t
2b4ec 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
2b4ed 75 65 6e 63 65 20 72 65 71 75 69 72 65 64 20 66  uence required f
2b4ee 72 6f 6d 20 61 6e 20 69 6e 64 65 78 20 66 6f 72  rom an index for
2b4ef 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 74  .        ** it t
2b4f0 6f 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72 20  o be useful for 
2b4f1 6f 70 74 69 6d 69 73 69 6e 67 20 65 78 70 72 65  optimising expre
2b4f2 73 73 69 6f 6e 20 70 58 2e 20 53 74 6f 72 65 20  ssion pX. Store 
2b4f3 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  this.        ** 
2b4f4 76 61 6c 75 65 20 69 6e 20 76 61 72 69 61 62 6c  value in variabl
2b4f5 65 20 70 43 6f 6c 6c 2e 0a 20 20 20 20 20 20 20  e pColl..       
2b4f6 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
2b4f7 72 74 28 70 58 2d 3e 70 4c 65 66 74 29 3b 0a 20  rt(pX->pLeft);. 
2b4f8 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73         pColl = s
2b4f9 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70  qlite3BinaryComp
2b4fa 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  areCollSeq(pPars
2b4fb 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 58  e, pX->pLeft, pX
2b4fc 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20  ->pRight);.     
2b4fd 20 20 20 61 73 73 65 72 74 28 70 43 6f 6c 6c 20     assert(pColl 
2b4fe 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 29  || pParse->nErr)
2b4ff 3b 0a 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  ;..        for(j
2b500 3d 30 3b 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  =0; pIdx->aiColu
2b501 6d 6e 5b 6a 5d 21 3d 69 43 6f 6c 75 6d 6e 3b 20  mn[j]!=iColumn; 
2b502 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
2b503 69 66 28 20 4e 45 56 45 52 28 6a 3e 3d 70 49 64  if( NEVER(j>=pId
2b504 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29 20 72 65  x->nColumn) ) re
2b505 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20  turn 0;.        
2b506 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43  }.        if( pC
2b507 6f 6c 6c 20 26 26 20 73 71 6c 69 74 65 33 53 74  oll && sqlite3St
2b508 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61  rICmp(pColl->zNa
2b509 6d 65 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  me, pIdx->azColl
2b50a 5b 6a 5d 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  [j]) ) continue;
2b50b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
2b50c 65 74 75 72 6e 20 70 54 65 72 6d 3b 0a 20 20 20  eturn pTerm;.   
2b50d 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2b50e 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  0;.}../* Forward
2b50f 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74   reference */.st
2b510 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e  atic void exprAn
2b511 61 6c 79 7a 65 28 53 72 63 4c 69 73 74 2a 2c 20  alyze(SrcList*, 
2b512 57 68 65 72 65 43 6c 61 75 73 65 2a 2c 20 69 6e  WhereClause*, in
2b513 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  t);../*.** Call 
2b514 65 78 70 72 41 6e 61 6c 79 7a 65 20 6f 6e 20 61  exprAnalyze on a
2b515 6c 6c 20 74 65 72 6d 73 20 69 6e 20 61 20 57 48  ll terms in a WH
2b516 45 52 45 20 63 6c 61 75 73 65 2e 20 20 0a 2a 2a  ERE clause.  .**
2b517 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  .**.*/.static vo
2b518 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c  id exprAnalyzeAl
2b519 6c 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  l(.  SrcList *pT
2b51a 61 62 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  abList,       /*
2b51b 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2b51c 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
2b51d 65 20 2a 70 57 43 20 20 20 20 20 20 20 20 20 2f  e *pWC         /
2b51e 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  * the WHERE clau
2b51f 73 65 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  se to be analyze
2b520 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  d */.){.  int i;
2b521 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54  .  for(i=pWC->nT
2b522 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  erm-1; i>=0; i--
2b523 29 7b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79  ){.    exprAnaly
2b524 7a 65 28 70 54 61 62 4c 69 73 74 2c 20 70 57 43  ze(pTabList, pWC
2b525 2c 20 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66  , i);.  }.}..#if
2b526 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2b527 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49  _LIKE_OPTIMIZATI
2b528 4f 4e 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  ON./*.** Check t
2b529 6f 20 73 65 65 20 69 66 20 74 68 65 20 67 69 76  o see if the giv
2b52a 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  en expression is
2b52b 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20   a LIKE or GLOB 
2b52c 6f 70 65 72 61 74 6f 72 20 74 68 61 74 0a 2a 2a  operator that.**
2b52d 20 63 61 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65   can be optimize
2b52e 64 20 75 73 69 6e 67 20 69 6e 65 71 75 61 6c 69  d using inequali
2b52f 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  ty constraints. 
2b530 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
2b531 69 74 20 69 73 0a 2a 2a 20 73 6f 20 61 6e 64 20  it is.** so and 
2b532 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a  false if not..**
2b533 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72  .** In order for
2b534 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
2b535 20 62 65 20 6f 70 74 69 6d 69 7a 69 62 6c 65 2c   be optimizible,
2b536 20 74 68 65 20 52 48 53 20 6d 75 73 74 20 62 65   the RHS must be
2b537 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 69 74   a string.** lit
2b538 65 72 61 6c 20 74 68 61 74 20 64 6f 65 73 20 6e  eral that does n
2b539 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
2b53a 77 69 6c 64 63 61 72 64 2e 20 20 0a 2a 2f 0a 73  wildcard.  .*/.s
2b53b 74 61 74 69 63 20 69 6e 74 20 69 73 4c 69 6b 65  tatic int isLike
2b53c 4f 72 47 6c 6f 62 28 0a 20 20 50 61 72 73 65 20  OrGlob(.  Parse 
2b53d 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50  *pParse,    /* P
2b53e 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
2b53f 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
2b540 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  xt */.  Expr *pE
2b541 78 70 72 2c 20 20 20 20 20 20 2f 2a 20 54 65 73  xpr,      /* Tes
2b542 74 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  t this expressio
2b543 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 50 61  n */.  int *pnPa
2b544 74 74 65 72 6e 2c 20 20 20 2f 2a 20 4e 75 6d 62  ttern,   /* Numb
2b545 65 72 20 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61  er of non-wildca
2b546 72 64 20 70 72 65 66 69 78 20 63 68 61 72 61 63  rd prefix charac
2b547 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ters */.  int *p
2b548 69 73 43 6f 6d 70 6c 65 74 65 2c 20 2f 2a 20 54  isComplete, /* T
2b549 72 75 65 20 69 66 20 74 68 65 20 6f 6e 6c 79 20  rue if the only 
2b54a 77 69 6c 64 63 61 72 64 20 69 73 20 25 20 69 6e  wildcard is % in
2b54b 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63   the last charac
2b54c 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ter */.  int *pn
2b54d 6f 43 61 73 65 20 20 20 20 20 20 2f 2a 20 54 72  oCase      /* Tr
2b54e 75 65 20 69 66 20 75 70 70 65 72 63 61 73 65 20  ue if uppercase 
2b54f 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  is equivalent to
2b550 20 6c 6f 77 65 72 63 61 73 65 20 2a 2f 0a 29 7b   lowercase */.){
2b551 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2b552 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2b553 20 53 74 72 69 6e 67 20 6f 6e 20 52 48 53 20 6f   String on RHS o
2b554 66 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  f LIKE operator 
2b555 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68  */.  Expr *pRigh
2b556 74 2c 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20  t, *pLeft;      
2b557 2f 2a 20 52 69 67 68 74 20 61 6e 64 20 6c 65 66  /* Right and lef
2b558 74 20 73 69 7a 65 20 6f 66 20 4c 49 4b 45 20 6f  t size of LIKE o
2b559 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70  perator */.  Exp
2b55a 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20  rList *pList;   
2b55b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
2b55c 6f 66 20 6f 70 65 72 61 6e 64 73 20 74 6f 20 74  of operands to t
2b55d 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72  he LIKE operator
2b55e 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 20 20 20 20   */.  int c;    
2b55f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b560 20 2f 2a 20 4f 6e 65 20 63 68 61 72 61 63 74 65   /* One characte
2b561 72 20 69 6e 20 7a 5b 5d 20 2a 2f 0a 20 20 69 6e  r in z[] */.  in
2b562 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  t n;            
2b563 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
2b564 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a 5b 5d  th of string z[]
2b565 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20   */.  int cnt;  
2b566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b567 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f   /* Number of no
2b568 6e 2d 77 69 6c 64 63 61 72 64 20 70 72 65 66 69  n-wildcard prefi
2b569 78 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a  x characters */.
2b56a 20 20 63 68 61 72 20 77 63 5b 33 5d 3b 20 20 20    char wc[3];   
2b56b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b56c 57 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74  Wildcard charact
2b56d 65 72 73 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71  ers */.  CollSeq
2b56e 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20   *pColl;        
2b56f 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67      /* Collating
2b570 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 4c 48   sequence for LH
2b571 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  S */.  sqlite3 *
2b572 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2b573 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2b574 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69  nnection */..  i
2b575 66 28 20 21 73 71 6c 69 74 65 33 49 73 4c 69 6b  f( !sqlite3IsLik
2b576 65 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45  eFunction(db, pE
2b577 78 70 72 2c 20 70 6e 6f 43 61 73 65 2c 20 77 63  xpr, pnoCase, wc
2b578 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2b579 30 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51  0;.  }.#ifdef SQ
2b57a 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 69 66  LITE_EBCDIC.  if
2b57b 28 20 2a 70 6e 6f 43 61 73 65 20 29 20 72 65 74  ( *pnoCase ) ret
2b57c 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20  urn 0;.#endif.  
2b57d 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78  pList = pExpr->x
2b57e 2e 70 4c 69 73 74 3b 0a 20 20 70 52 69 67 68 74  .pList;.  pRight
2b57f 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70   = pList->a[0].p
2b580 45 78 70 72 3b 0a 20 20 69 66 28 20 70 52 69 67  Expr;.  if( pRig
2b581 68 74 2d 3e 6f 70 21 3d 54 4b 5f 53 54 52 49 4e  ht->op!=TK_STRIN
2b582 47 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  G ){.    return 
2b583 30 3b 0a 20 20 7d 0a 20 20 70 4c 65 66 74 20 3d  0;.  }.  pLeft =
2b584 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78   pList->a[1].pEx
2b585 70 72 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d  pr;.  if( pLeft-
2b586 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29  >op!=TK_COLUMN )
2b587 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2b588 20 20 7d 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71    }.  pColl = sq
2b589 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
2b58a 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 29 3b  (pParse, pLeft);
2b58b 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 6c  .  assert( pColl
2b58c 21 3d 30 20 7c 7c 20 70 4c 65 66 74 2d 3e 69 43  !=0 || pLeft->iC
2b58d 6f 6c 75 6d 6e 3d 3d 2d 31 20 29 3b 0a 20 20 69  olumn==-1 );.  i
2b58e 66 28 20 70 43 6f 6c 6c 3d 3d 30 20 29 7b 0a 20  f( pColl==0 ){. 
2b58f 20 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69     /* No collati
2b590 6f 6e 20 69 73 20 64 65 66 69 6e 65 64 20 66 6f  on is defined fo
2b591 72 20 74 68 65 20 52 4f 57 49 44 2e 20 20 55 73  r the ROWID.  Us
2b592 65 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 2a  e the default. *
2b593 2f 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62  /.    pColl = db
2b594 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d  ->pDfltColl;.  }
2b595 0a 20 20 69 66 28 20 28 70 43 6f 6c 6c 2d 3e 74  .  if( (pColl->t
2b596 79 70 65 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c  ype!=SQLITE_COLL
2b597 5f 42 49 4e 41 52 59 20 7c 7c 20 2a 70 6e 6f 43  _BINARY || *pnoC
2b598 61 73 65 29 20 26 26 0a 20 20 20 20 20 20 28 70  ase) &&.      (p
2b599 43 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49  Coll->type!=SQLI
2b59a 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 7c  TE_COLL_NOCASE |
2b59b 7c 20 21 2a 70 6e 6f 43 61 73 65 29 20 29 7b 0a  | !*pnoCase) ){.
2b59c 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2b59d 7d 0a 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20 63  }.  z = (const c
2b59e 68 61 72 2a 29 70 52 69 67 68 74 2d 3e 74 6f 6b  har*)pRight->tok
2b59f 65 6e 2e 7a 3b 0a 20 20 63 6e 74 20 3d 20 30 3b  en.z;.  cnt = 0;
2b5a0 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20  .  if( z ){.    
2b5a1 6e 20 3d 20 70 52 69 67 68 74 2d 3e 74 6f 6b 65  n = pRight->toke
2b5a2 6e 2e 6e 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  n.n;.    while( 
2b5a3 63 6e 74 3c 6e 20 26 26 20 28 63 3d 7a 5b 63 6e  cnt<n && (c=z[cn
2b5a4 74 5d 29 21 3d 30 20 26 26 20 63 21 3d 77 63 5b  t])!=0 && c!=wc[
2b5a5 30 5d 20 26 26 20 63 21 3d 77 63 5b 31 5d 20 26  0] && c!=wc[1] &
2b5a6 26 20 63 21 3d 77 63 5b 32 5d 20 29 7b 0a 20 20  & c!=wc[2] ){.  
2b5a7 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d      cnt++;.    }
2b5a8 0a 20 20 7d 0a 20 20 69 66 28 20 63 6e 74 3d 3d  .  }.  if( cnt==
2b5a9 30 20 7c 7c 20 32 35 35 3d 3d 28 75 38 29 7a 5b  0 || 255==(u8)z[
2b5aa 63 6e 74 2d 31 5d 20 29 7b 0a 20 20 20 20 72 65  cnt-1] ){.    re
2b5ab 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a 70  turn 0;.  }.  *p
2b5ac 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 7a 5b 63  isComplete = z[c
2b5ad 6e 74 5d 3d 3d 77 63 5b 30 5d 20 26 26 20 7a 5b  nt]==wc[0] && z[
2b5ae 63 6e 74 2b 31 5d 3d 3d 30 3b 0a 20 20 2a 70 6e  cnt+1]==0;.  *pn
2b5af 50 61 74 74 65 72 6e 20 3d 20 63 6e 74 3b 0a 20  Pattern = cnt;. 
2b5b0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e   return 1;.}.#en
2b5b1 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2b5b2 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41  IT_LIKE_OPTIMIZA
2b5b3 54 49 4f 4e 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65  TION */...#ifnde
2b5b4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2b5b5 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a  RTUALTABLE./*.**
2b5b6 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
2b5b7 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65   the given expre
2b5b8 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20  ssion is of the 
2b5b9 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  form.**.**      
2b5ba 20 20 20 63 6f 6c 75 6d 6e 20 4d 41 54 43 48 20     column MATCH 
2b5bb 65 78 70 72 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74  expr.**.** If it
2b5bc 20 69 73 20 74 68 65 6e 20 72 65 74 75 72 6e 20   is then return 
2b5bd 54 52 55 45 2e 20 20 49 66 20 6e 6f 74 2c 20 72  TRUE.  If not, r
2b5be 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a  eturn FALSE..*/.
2b5bf 73 74 61 74 69 63 20 69 6e 74 20 69 73 4d 61 74  static int isMat
2b5c0 63 68 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20 45 78  chOfColumn(.  Ex
2b5c1 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 2f  pr *pExpr      /
2b5c2 2a 20 54 65 73 74 20 74 68 69 73 20 65 78 70 72  * Test this expr
2b5c3 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 45  ession */.){.  E
2b5c4 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a  xprList *pList;.
2b5c5 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  .  if( pExpr->op
2b5c6 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 7b  !=TK_FUNCTION ){
2b5c7 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2b5c8 20 7d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e   }.  if( pExpr->
2b5c9 74 6f 6b 65 6e 2e 6e 21 3d 35 20 7c 7c 0a 20 20  token.n!=5 ||.  
2b5ca 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 4e       sqlite3StrN
2b5cb 49 43 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 72  ICmp((const char
2b5cc 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  *)pExpr->token.z
2b5cd 2c 22 6d 61 74 63 68 22 2c 35 29 21 3d 30 20 29  ,"match",5)!=0 )
2b5ce 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2b5cf 20 20 7d 0a 20 20 70 4c 69 73 74 20 3d 20 70 45    }.  pList = pE
2b5d0 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20  xpr->x.pList;.  
2b5d1 69 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  if( pList->nExpr
2b5d2 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=2 ){.    retur
2b5d3 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n 0;.  }.  if( p
2b5d4 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72  List->a[1].pExpr
2b5d5 2d 3e 6f 70 20 21 3d 20 54 4b 5f 43 4f 4c 55 4d  ->op != TK_COLUM
2b5d6 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  N ){.    return 
2b5d7 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
2b5d8 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  1;.}.#endif /* S
2b5d9 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2b5da 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a  ALTABLE */../*.*
2b5db 2a 20 49 66 20 74 68 65 20 70 42 61 73 65 20 65  * If the pBase e
2b5dc 78 70 72 65 73 73 69 6f 6e 20 6f 72 69 67 69 6e  xpression origin
2b5dd 61 74 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f  ated in the ON o
2b5de 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f  r USING clause o
2b5df 66 0a 2a 2a 20 61 20 6a 6f 69 6e 2c 20 74 68 65  f.** a join, the
2b5e0 6e 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 61  n transfer the a
2b5e1 70 70 72 6f 70 72 69 61 74 65 20 6d 61 72 6b 69  ppropriate marki
2b5e2 6e 67 73 20 6f 76 65 72 20 74 6f 20 64 65 72 69  ngs over to deri
2b5e3 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ved..*/.static v
2b5e4 6f 69 64 20 74 72 61 6e 73 66 65 72 4a 6f 69 6e  oid transferJoin
2b5e5 4d 61 72 6b 69 6e 67 73 28 45 78 70 72 20 2a 70  Markings(Expr *p
2b5e6 44 65 72 69 76 65 64 2c 20 45 78 70 72 20 2a 70  Derived, Expr *p
2b5e7 42 61 73 65 29 7b 0a 20 20 70 44 65 72 69 76 65  Base){.  pDerive
2b5e8 64 2d 3e 66 6c 61 67 73 20 7c 3d 20 70 42 61 73  d->flags |= pBas
2b5e9 65 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 46 72  e->flags & EP_Fr
2b5ea 6f 6d 4a 6f 69 6e 3b 0a 20 20 70 44 65 72 69 76  omJoin;.  pDeriv
2b5eb 65 64 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61  ed->iRightJoinTa
2b5ec 62 6c 65 20 3d 20 70 42 61 73 65 2d 3e 69 52 69  ble = pBase->iRi
2b5ed 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a 7d 0a  ghtJoinTable;.}.
2b5ee 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2b5ef 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54  LITE_OMIT_OR_OPT
2b5f0 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20 21 64  IMIZATION) && !d
2b5f1 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2b5f2 49 54 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a  IT_SUBQUERY)./*.
2b5f3 2a 2a 20 41 6e 61 6c 79 7a 65 20 61 20 74 65 72  ** Analyze a ter
2b5f4 6d 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20  m that consists 
2b5f5 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 4f  of two or more O
2b5f6 52 2d 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 73  R-connected.** s
2b5f7 75 62 74 65 72 6d 73 2e 20 20 53 6f 20 69 6e 3a  ubterms.  So in:
2b5f8 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2e 2e 2e 20 57  .**.**     ... W
2b5f9 48 45 52 45 20 20 28 61 3d 35 29 20 41 4e 44 20  HERE  (a=5) AND 
2b5fa 28 62 3d 37 20 4f 52 20 63 3d 39 20 4f 52 20 64  (b=7 OR c=9 OR d
2b5fb 3d 31 33 29 20 41 4e 44 20 28 64 3d 31 33 29 0a  =13) AND (d=13).
2b5fc 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2b5fd 20 20 20 20 20 20 20 20 20 20 20 20 5e 5e 5e 5e              ^^^^
2b5fe 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e  ^^^^^^^^^^^^^^^^
2b5ff 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2b600 69 6e 65 20 61 6e 61 6c 79 7a 65 73 20 74 65 72  ine analyzes ter
2b601 6d 73 20 73 75 63 68 20 61 73 20 74 68 65 20 6d  ms such as the m
2b602 69 64 64 6c 65 20 74 65 72 6d 20 69 6e 20 74 68  iddle term in th
2b603 65 20 61 62 6f 76 65 20 65 78 61 6d 70 6c 65 2e  e above example.
2b604 0a 2a 2a 20 41 20 57 68 65 72 65 4f 72 54 65 72  .** A WhereOrTer
2b605 6d 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6d 70  m object is comp
2b606 75 74 65 64 20 61 6e 64 20 61 74 74 61 63 68 65  uted and attache
2b607 64 20 74 6f 20 74 68 65 20 74 65 72 6d 20 75 6e  d to the term un
2b608 64 65 72 0a 2a 2a 20 61 6e 61 6c 79 73 69 73 2c  der.** analysis,
2b609 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74   regardless of t
2b60a 68 65 20 6f 75 74 63 6f 6d 65 20 6f 66 20 74 68  he outcome of th
2b60b 65 20 61 6e 61 6c 79 73 69 73 2e 20 20 48 65 6e  e analysis.  Hen
2b60c 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 57 68  ce:.**.**     Wh
2b60d 65 72 65 54 65 72 6d 2e 77 74 46 6c 61 67 73 20  ereTerm.wtFlags 
2b60e 20 20 7c 3d 20 20 54 45 52 4d 5f 4f 52 49 4e 46    |=  TERM_ORINF
2b60f 4f 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65  O.**     WhereTe
2b610 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 20 3d 20  rm.u.pOrInfo  = 
2b611 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61   a dynamically a
2b612 6c 6c 6f 63 61 74 65 64 20 57 68 65 72 65 4f 72  llocated WhereOr
2b613 54 65 72 6d 20 6f 62 6a 65 63 74 0a 2a 2a 0a 2a  Term object.**.*
2b614 2a 20 54 68 65 20 74 65 72 6d 20 62 65 69 6e 67  * The term being
2b615 20 61 6e 61 6c 79 7a 65 64 20 6d 75 73 74 20 68   analyzed must h
2b616 61 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20  ave two or more 
2b617 6f 66 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 20  of OR-connected 
2b618 73 75 62 74 65 72 6d 73 2e 0a 2a 2a 20 41 20 73  subterms..** A s
2b619 69 6e 67 6c 65 20 73 75 62 74 65 72 6d 20 6d 69  ingle subterm mi
2b61a 67 68 74 20 62 65 20 61 20 73 65 74 20 6f 66 20  ght be a set of 
2b61b 41 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75  AND-connected su
2b61c 62 2d 73 75 62 74 65 72 6d 73 2e 0a 2a 2a 20 45  b-subterms..** E
2b61d 78 61 6d 70 6c 65 73 20 6f 66 20 74 65 72 6d 73  xamples of terms
2b61e 20 75 6e 64 65 72 20 61 6e 61 6c 79 73 69 73 3a   under analysis:
2b61f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 41 29 20 20  .**.**     (A)  
2b620 20 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f 52 20     t1.x=t2.y OR 
2b621 74 31 2e 78 3d 74 32 2e 7a 20 4f 52 20 74 31 2e  t1.x=t2.z OR t1.
2b622 79 3d 31 35 20 4f 52 20 74 31 2e 7a 3d 74 33 2e  y=15 OR t1.z=t3.
2b623 61 2b 35 0a 2a 2a 20 20 20 20 20 28 42 29 20 20  a+5.**     (B)  
2b624 20 20 20 78 3d 65 78 70 72 31 20 4f 52 20 65 78     x=expr1 OR ex
2b625 70 72 32 3d 78 20 4f 52 20 78 3d 65 78 70 72 33  pr2=x OR x=expr3
2b626 0a 2a 2a 20 20 20 20 20 28 43 29 20 20 20 20 20  .**     (C)     
2b627 74 31 2e 78 3d 74 32 2e 79 20 4f 52 20 28 74 31  t1.x=t2.y OR (t1
2b628 2e 78 3d 74 32 2e 7a 20 41 4e 44 20 74 31 2e 79  .x=t2.z AND t1.y
2b629 3d 31 35 29 0a 2a 2a 20 20 20 20 20 28 44 29 20  =15).**     (D) 
2b62a 20 20 20 20 78 3d 65 78 70 72 31 20 4f 52 20 28      x=expr1 OR (
2b62b 79 3e 31 31 20 41 4e 44 20 79 3c 32 32 20 41 4e  y>11 AND y<22 AN
2b62c 44 20 7a 20 4c 49 4b 45 20 27 2a 68 65 6c 6c 6f  D z LIKE '*hello
2b62d 2a 27 29 0a 2a 2a 20 20 20 20 20 28 45 29 20 20  *').**     (E)  
2b62e 20 20 20 28 70 2e 61 3d 31 20 41 4e 44 20 71 2e     (p.a=1 AND q.
2b62f 62 3d 32 20 41 4e 44 20 72 2e 63 3d 33 29 20 4f  b=2 AND r.c=3) O
2b630 52 20 28 70 2e 78 3d 34 20 41 4e 44 20 71 2e 79  R (p.x=4 AND q.y
2b631 3d 35 20 41 4e 44 20 72 2e 7a 3d 36 29 0a 2a 2a  =5 AND r.z=6).**
2b632 0a 2a 2a 20 43 41 53 45 20 31 3a 0a 2a 2a 0a 2a  .** CASE 1:.**.*
2b633 2a 20 49 66 20 61 6c 6c 20 73 75 62 74 65 72 6d  * If all subterm
2b634 73 20 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72  s are of the for
2b635 6d 20 54 2e 43 3d 65 78 70 72 20 66 6f 72 20 73  m T.C=expr for s
2b636 6f 6d 65 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  ome single colum
2b637 6e 20 6f 66 20 43 0a 2a 2a 20 61 20 73 69 6e 67  n of C.** a sing
2b638 6c 65 20 74 61 62 6c 65 20 54 20 28 61 73 20 73  le table T (as s
2b639 68 6f 77 6e 20 69 6e 20 65 78 61 6d 70 6c 65 20  hown in example 
2b63a 42 20 61 62 6f 76 65 29 20 74 68 65 6e 20 63 72  B above) then cr
2b63b 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75  eate a new virtu
2b63c 61 6c 0a 2a 2a 20 74 65 72 6d 20 74 68 61 74 20  al.** term that 
2b63d 69 73 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74  is an equivalent
2b63e 20 49 4e 20 65 78 70 72 65 73 73 69 6f 6e 2e 20   IN expression. 
2b63f 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
2b640 20 69 66 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20   if the term.** 
2b641 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 69  being analyzed i
2b642 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20  s:.**.**      x 
2b643 3d 20 65 78 70 72 31 20 20 4f 52 20 20 65 78 70  = expr1  OR  exp
2b644 72 32 20 3d 20 78 20 20 4f 52 20 20 78 20 3d 20  r2 = x  OR  x = 
2b645 65 78 70 72 33 0a 2a 2a 0a 2a 2a 20 74 68 65 6e  expr3.**.** then
2b646 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69   create a new vi
2b647 72 74 75 61 6c 20 74 65 72 6d 20 6c 69 6b 65 20  rtual term like 
2b648 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
2b649 20 78 20 49 4e 20 28 65 78 70 72 31 2c 65 78 70   x IN (expr1,exp
2b64a 72 32 2c 65 78 70 72 33 29 0a 2a 2a 0a 2a 2a 20  r2,expr3).**.** 
2b64b 43 41 53 45 20 32 3a 0a 2a 2a 0a 2a 2a 20 49 66  CASE 2:.**.** If
2b64c 20 61 6c 6c 20 73 75 62 74 65 72 6d 73 20 61 72   all subterms ar
2b64d 65 20 69 6e 64 65 78 61 62 6c 65 20 62 79 20 61  e indexable by a
2b64e 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 54 2c   single table T,
2b64f 20 74 68 65 6e 20 73 65 74 0a 2a 2a 0a 2a 2a 20   then set.**.** 
2b650 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 65 4f      WhereTerm.eO
2b651 70 65 72 61 74 6f 72 20 20 20 20 20 20 20 20 20  perator         
2b652 20 20 20 20 20 3d 20 20 57 4f 5f 4f 52 0a 2a 2a       =  WO_OR.**
2b653 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 75       WhereTerm.u
2b654 2e 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61  .pOrInfo->indexa
2b655 62 6c 65 20 20 7c 3d 20 20 74 68 65 20 63 75 72  ble  |=  the cur
2b656 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
2b657 61 62 6c 65 20 54 0a 2a 2a 0a 2a 2a 20 41 20 73  able T.**.** A s
2b658 75 62 74 65 72 6d 20 69 73 20 22 69 6e 64 65 78  ubterm is "index
2b659 61 62 6c 65 22 20 69 66 20 69 74 20 69 73 20 6f  able" if it is o
2b65a 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 20 22 54  f the form.** "T
2b65b 2e 43 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20  .C <op> <expr>" 
2b65c 77 68 65 72 65 20 43 20 69 73 20 61 6e 79 20 63  where C is any c
2b65d 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 54  olumn of table T
2b65e 20 61 6e 64 20 0a 2a 2a 20 3c 6f 70 3e 20 69 73   and .** <op> is
2b65f 20 6f 6e 65 20 6f 66 20 22 3d 22 2c 20 22 3c 22   one of "=", "<"
2b660 2c 20 22 3c 3d 22 2c 20 22 3e 22 2c 20 22 3e 3d  , "<=", ">", ">=
2b661 22 2c 20 22 49 53 20 4e 55 4c 4c 22 2c 20 6f 72  ", "IS NULL", or
2b662 20 22 49 4e 22 2e 0a 2a 2a 20 41 20 73 75 62 74   "IN"..** A subt
2b663 65 72 6d 20 69 73 20 61 6c 73 6f 20 69 6e 64 65  erm is also inde
2b664 78 61 62 6c 65 20 69 66 20 69 74 20 69 73 20 61  xable if it is a
2b665 6e 20 41 4e 44 20 6f 66 20 74 77 6f 20 6f 72 20  n AND of two or 
2b666 6d 6f 72 65 0a 2a 2a 20 73 75 62 73 75 62 74 65  more.** subsubte
2b667 72 6d 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  rms at least one
2b668 20 6f 66 20 77 68 69 63 68 20 69 73 20 69 6e 64   of which is ind
2b669 65 78 61 62 6c 65 2e 20 20 49 6e 64 65 78 61 62  exable.  Indexab
2b66a 6c 65 20 41 4e 44 20 0a 2a 2a 20 73 75 62 74 65  le AND .** subte
2b66b 72 6d 73 20 68 61 76 65 20 74 68 65 69 72 20 65  rms have their e
2b66c 4f 70 65 72 61 74 6f 72 20 73 65 74 20 74 6f 20  Operator set to 
2b66d 57 4f 5f 41 4e 44 20 61 6e 64 20 74 68 65 79 20  WO_AND and they 
2b66e 68 61 76 65 0a 2a 2a 20 75 2e 70 41 6e 64 49 6e  have.** u.pAndIn
2b66f 66 6f 20 73 65 74 20 74 6f 20 61 20 64 79 6e 61  fo set to a dyna
2b670 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
2b671 64 20 57 68 65 72 65 41 6e 64 54 65 72 6d 20 6f  d WhereAndTerm o
2b672 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 46 72 6f  bject..**.** Fro
2b673 6d 20 61 6e 6f 74 68 65 72 20 70 6f 69 6e 74 20  m another point 
2b674 6f 66 20 76 69 65 77 2c 20 22 69 6e 64 65 78 61  of view, "indexa
2b675 62 6c 65 22 20 6d 65 61 6e 73 20 74 68 61 74 20  ble" means that 
2b676 74 68 65 20 73 75 62 74 65 72 6d 20 63 6f 75 6c  the subterm coul
2b677 64 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c 6c 79  d.** potentially
2b678 20 62 65 20 75 73 65 64 20 77 69 74 68 20 61 6e   be used with an
2b679 20 69 6e 64 65 78 20 69 66 20 61 6e 20 61 70 70   index if an app
2b67a 72 6f 70 72 69 61 74 65 20 69 6e 64 65 78 20 65  ropriate index e
2b67b 78 69 73 74 73 2e 0a 2a 2a 20 54 68 69 73 20 61  xists..** This a
2b67c 6e 61 6c 79 73 69 73 20 64 6f 65 73 20 6e 6f 74  nalysis does not
2b67d 20 63 6f 6e 73 69 64 65 72 20 77 68 65 74 68 65   consider whethe
2b67e 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64  r or not the ind
2b67f 65 78 20 65 78 69 73 74 73 3b 20 74 68 61 74 0a  ex exists; that.
2b680 2a 2a 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20  ** is something 
2b681 74 68 65 20 62 65 73 74 49 6e 64 65 78 28 29 20  the bestIndex() 
2b682 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 65 74  routine will det
2b683 65 72 6d 69 6e 65 2e 20 20 54 68 69 73 20 61 6e  ermine.  This an
2b684 61 6c 79 73 69 73 0a 2a 2a 20 6f 6e 6c 79 20 6c  alysis.** only l
2b685 6f 6f 6b 73 20 61 74 20 77 68 65 74 68 65 72 20  ooks at whether 
2b686 73 75 62 74 65 72 6d 73 20 61 70 70 72 6f 70 72  subterms appropr
2b687 69 61 74 65 20 66 6f 72 20 69 6e 64 65 78 69 6e  iate for indexin
2b688 67 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41  g exist..**.** A
2b689 6c 6c 20 65 78 61 6d 70 6c 65 73 20 41 20 74 68  ll examples A th
2b68a 72 6f 75 67 68 20 45 20 61 62 6f 76 65 20 61 6c  rough E above al
2b68b 6c 20 73 61 74 69 73 66 79 20 63 61 73 65 20 32  l satisfy case 2
2b68c 2e 20 20 42 75 74 20 69 66 20 61 20 74 65 72 6d  .  But if a term
2b68d 0a 2a 2a 20 61 6c 73 6f 20 73 74 61 74 69 73 66  .** also statisf
2b68e 69 65 73 20 63 61 73 65 20 31 20 28 73 75 63 68  ies case 1 (such
2b68f 20 61 73 20 42 29 20 77 65 20 6b 6e 6f 77 20 74   as B) we know t
2b690 68 61 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 65  hat the optimize
2b691 72 20 77 69 6c 6c 0a 2a 2a 20 61 6c 77 61 79 73  r will.** always
2b692 20 70 72 65 66 65 72 20 63 61 73 65 20 31 2c 20   prefer case 1, 
2b693 73 6f 20 69 6e 20 74 68 61 74 20 63 61 73 65 20  so in that case 
2b694 77 65 20 70 72 65 74 65 6e 64 20 74 68 61 74 20  we pretend that 
2b695 63 61 73 65 20 32 20 69 73 20 6e 6f 74 0a 2a 2a  case 2 is not.**
2b696 20 73 61 74 69 73 66 69 65 64 2e 0a 2a 2a 0a 2a   satisfied..**.*
2b697 2a 20 49 74 20 6d 69 67 68 74 20 62 65 20 74 68  * It might be th
2b698 65 20 63 61 73 65 20 74 68 61 74 20 6d 75 6c 74  e case that mult
2b699 69 70 6c 65 20 74 61 62 6c 65 73 20 61 72 65 20  iple tables are 
2b69a 69 6e 64 65 78 61 62 6c 65 2e 20 20 46 6f 72 20  indexable.  For 
2b69b 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 28 45 29 20  example,.** (E) 
2b69c 61 62 6f 76 65 20 69 73 20 69 6e 64 65 78 61 62  above is indexab
2b69d 6c 65 20 6f 6e 20 74 61 62 6c 65 73 20 50 2c 20  le on tables P, 
2b69e 51 2c 20 61 6e 64 20 52 2e 0a 2a 2a 0a 2a 2a 20  Q, and R..**.** 
2b69f 54 65 72 6d 73 20 74 68 61 74 20 73 61 74 69 73  Terms that satis
2b6a0 66 79 20 63 61 73 65 20 32 20 61 72 65 20 63 61  fy case 2 are ca
2b6a1 6e 64 69 64 61 74 65 73 20 66 6f 72 20 6c 6f 6f  ndidates for loo
2b6a2 6b 75 70 20 62 79 20 75 73 69 6e 67 0a 2a 2a 20  kup by using.** 
2b6a3 73 65 70 61 72 61 74 65 20 69 6e 64 69 63 65 73  separate indices
2b6a4 20 74 6f 20 66 69 6e 64 20 72 6f 77 69 64 73 20   to find rowids 
2b6a5 66 6f 72 20 65 61 63 68 20 73 75 62 74 65 72 6d  for each subterm
2b6a6 20 61 6e 64 20 63 6f 6d 70 6f 73 69 6e 67 0a 2a   and composing.*
2b6a7 2a 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 61  * the union of a
2b6a8 6c 6c 20 72 6f 77 69 64 73 20 75 73 69 6e 67 20  ll rowids using 
2b6a9 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e  a RowSet object.
2b6aa 20 20 54 68 69 73 20 69 73 20 73 69 6d 69 6c 61    This is simila
2b6ab 72 0a 2a 2a 20 74 6f 20 22 62 69 74 6d 61 70 20  r.** to "bitmap 
2b6ac 69 6e 64 69 63 65 73 22 20 69 6e 20 6f 74 68 65  indices" in othe
2b6ad 72 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  r database engin
2b6ae 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 54 48 45 52 57  es..**.** OTHERW
2b6af 49 53 45 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 65  ISE:.**.** If ne
2b6b0 69 74 68 65 72 20 63 61 73 65 20 31 20 6e 6f 72  ither case 1 nor
2b6b1 20 63 61 73 65 20 32 20 61 70 70 6c 79 2c 20 74   case 2 apply, t
2b6b2 68 65 6e 20 6c 65 61 76 65 20 74 68 65 20 65 4f  hen leave the eO
2b6b3 70 65 72 61 74 6f 72 20 73 65 74 20 74 6f 0a 2a  perator set to.*
2b6b4 2a 20 7a 65 72 6f 2e 20 20 54 68 69 73 20 74 65  * zero.  This te
2b6b5 72 6d 20 69 73 20 6e 6f 74 20 75 73 65 66 75 6c  rm is not useful
2b6b6 20 66 6f 72 20 73 65 61 72 63 68 2e 0a 2a 2f 0a   for search..*/.
2b6b7 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72  static void expr
2b6b8 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28 0a 20  AnalyzeOrTerm(. 
2b6b9 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
2b6ba 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68             /* th
2b6bb 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  e FROM clause */
2b6bc 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2b6bd 70 57 43 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  pWC,         /* 
2b6be 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 57 48 45  the complete WHE
2b6bf 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  RE clause */.  i
2b6c0 6e 74 20 69 64 78 54 65 72 6d 20 20 20 20 20 20  nt idxTerm      
2b6c1 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2b6c2 78 20 6f 66 20 74 68 65 20 4f 52 2d 74 65 72 6d  x of the OR-term
2b6c3 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20   to be analyzed 
2b6c4 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70  */.){.  Parse *p
2b6c5 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61  Parse = pWC->pPa
2b6c6 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  rse;            
2b6c7 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
2b6c8 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  t */.  sqlite3 *
2b6c9 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2b6ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b6cb 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
2b6cc 63 74 69 6f 6e 20 2a 2f 0a 20 20 57 68 65 72 65  ction */.  Where
2b6cd 54 65 72 6d 20 2a 70 54 65 72 6d 20 3d 20 26 70  Term *pTerm = &p
2b6ce 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 20  WC->a[idxTerm]; 
2b6cf 20 20 20 2f 2a 20 54 68 65 20 74 65 72 6d 20 74     /* The term t
2b6d0 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  o be analyzed */
2b6d1 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d  .  Expr *pExpr =
2b6d2 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 20 20   pTerm->pExpr;  
2b6d3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2b6d4 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20  e expression of 
2b6d5 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20 20 57 68  the term */.  Wh
2b6d6 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2b6d7 6b 53 65 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73  kSet = pWC->pMas
2b6d8 6b 53 65 74 3b 20 2f 2a 20 54 61 62 6c 65 20 75  kSet; /* Table u
2b6d9 73 65 20 6d 61 73 6b 73 20 2a 2f 0a 20 20 69 6e  se masks */.  in
2b6da 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
2b6db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b6dc 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2b6dd 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68 65 72  unters */.  Wher
2b6de 65 43 6c 61 75 73 65 20 2a 70 4f 72 57 63 3b 20  eClause *pOrWc; 
2b6df 20 20 20 20 20 20 2f 2a 20 42 72 65 61 6b 75 70        /* Breakup
2b6e0 20 6f 66 20 70 54 65 72 6d 20 69 6e 74 6f 20 73   of pTerm into s
2b6e1 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 57 68 65  ubterms */.  Whe
2b6e2 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b  reTerm *pOrTerm;
2b6e3 20 20 20 20 20 20 20 2f 2a 20 41 20 53 75 62 2d         /* A Sub-
2b6e4 74 65 72 6d 20 77 69 74 68 69 6e 20 74 68 65 20  term within the 
2b6e5 70 4f 72 57 63 20 2a 2f 0a 20 20 57 68 65 72 65  pOrWc */.  Where
2b6e6 4f 72 49 6e 66 6f 20 2a 70 4f 72 49 6e 66 6f 3b  OrInfo *pOrInfo;
2b6e7 20 20 20 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e       /* Addition
2b6e8 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  al information a
2b6e9 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70  ssociated with p
2b6ea 54 65 72 6d 20 2a 2f 0a 20 20 42 69 74 6d 61 73  Term */.  Bitmas
2b6eb 6b 20 63 68 6e 67 54 6f 49 4e 3b 20 20 20 20 20  k chngToIN;     
2b6ec 20 20 20 20 2f 2a 20 54 61 62 6c 65 73 20 74 68      /* Tables th
2b6ed 61 74 20 6d 69 67 68 74 20 73 61 74 69 73 66 79  at might satisfy
2b6ee 20 63 61 73 65 20 31 20 2a 2f 0a 20 20 42 69 74   case 1 */.  Bit
2b6ef 6d 61 73 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20  mask indexable; 
2b6f0 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 73         /* Tables
2b6f1 20 74 68 61 74 20 61 72 65 20 69 6e 64 65 78 61   that are indexa
2b6f2 62 6c 65 2c 20 73 61 74 69 73 66 79 69 6e 67 20  ble, satisfying 
2b6f3 63 61 73 65 20 32 20 2a 2f 0a 0a 20 20 2f 2a 0a  case 2 */..  /*.
2b6f4 20 20 2a 2a 20 42 72 65 61 6b 20 74 68 65 20 4f    ** Break the O
2b6f5 52 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 69 74  R clause into it
2b6f6 73 20 73 65 70 61 72 61 74 65 20 73 75 62 74 65  s separate subte
2b6f7 72 6d 73 2e 20 20 54 68 65 20 73 75 62 74 65 72  rms.  The subter
2b6f8 6d 73 20 61 72 65 0a 20 20 2a 2a 20 73 74 6f 72  ms are.  ** stor
2b6f9 65 64 20 69 6e 20 61 20 57 68 65 72 65 43 6c 61  ed in a WhereCla
2b6fa 75 73 65 20 73 74 72 75 63 74 75 72 65 20 63 6f  use structure co
2b6fb 6e 74 61 69 6e 69 6e 67 20 77 69 74 68 69 6e 20  ntaining within 
2b6fc 74 68 65 20 57 68 65 72 65 4f 72 49 6e 66 6f 0a  the WhereOrInfo.
2b6fd 20 20 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74    ** object that
2b6fe 20 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20   is attached to 
2b6ff 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 4f 52 20  the original OR 
2b700 63 6c 61 75 73 65 20 74 65 72 6d 2e 0a 20 20 2a  clause term..  *
2b701 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 65  /.  assert( (pTe
2b702 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54  rm->wtFlags & (T
2b703 45 52 4d 5f 44 59 4e 41 4d 49 43 7c 54 45 52 4d  ERM_DYNAMIC|TERM
2b704 5f 4f 52 49 4e 46 4f 7c 54 45 52 4d 5f 41 4e 44  _ORINFO|TERM_AND
2b705 49 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20 20 61  INFO))==0 );.  a
2b706 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70  ssert( pExpr->op
2b707 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 70 54 65  ==TK_OR );.  pTe
2b708 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 20 3d 20  rm->u.pOrInfo = 
2b709 70 4f 72 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65  pOrInfo = sqlite
2b70a 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2b70b 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 72 49 6e 66  , sizeof(*pOrInf
2b70c 6f 29 29 3b 0a 20 20 69 66 28 20 70 4f 72 49 6e  o));.  if( pOrIn
2b70d 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  fo==0 ) return;.
2b70e 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73    pTerm->wtFlags
2b70f 20 7c 3d 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 3b   |= TERM_ORINFO;
2b710 0a 20 20 70 4f 72 57 63 20 3d 20 26 70 4f 72 49  .  pOrWc = &pOrI
2b711 6e 66 6f 2d 3e 77 63 3b 0a 20 20 77 68 65 72 65  nfo->wc;.  where
2b712 43 6c 61 75 73 65 49 6e 69 74 28 70 4f 72 57 63  ClauseInit(pOrWc
2b713 2c 20 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70  , pWC->pParse, p
2b714 4d 61 73 6b 53 65 74 29 3b 0a 20 20 77 68 65 72  MaskSet);.  wher
2b715 65 53 70 6c 69 74 28 70 4f 72 57 63 2c 20 70 45  eSplit(pOrWc, pE
2b716 78 70 72 2c 20 54 4b 5f 4f 52 29 3b 0a 20 20 65  xpr, TK_OR);.  e
2b717 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53  xprAnalyzeAll(pS
2b718 72 63 2c 20 70 4f 72 57 63 29 3b 0a 20 20 69 66  rc, pOrWc);.  if
2b719 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2b71a 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  ed ) return;.  a
2b71b 73 73 65 72 74 28 20 70 4f 72 57 63 2d 3e 6e 54  ssert( pOrWc->nT
2b71c 65 72 6d 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 0a  erm>=2 );..  /*.
2b71d 20 20 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65    ** Compute the
2b71e 20 73 65 74 20 6f 66 20 74 61 62 6c 65 73 20 74   set of tables t
2b71f 68 61 74 20 6d 69 67 68 74 20 73 61 74 69 73 66  hat might satisf
2b720 79 20 63 61 73 65 73 20 31 20 6f 72 20 32 2e 0a  y cases 1 or 2..
2b721 20 20 2a 2f 0a 20 20 69 6e 64 65 78 61 62 6c 65    */.  indexable
2b722 20 3d 20 63 68 6e 67 54 6f 49 4e 20 3d 20 7e 28   = chngToIN = ~(
2b723 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 66 6f 72  Bitmask)0;.  for
2b724 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d  (i=pOrWc->nTerm-
2b725 31 2c 20 70 4f 72 54 65 72 6d 3d 70 4f 72 57 63  1, pOrTerm=pOrWc
2b726 2d 3e 61 3b 20 69 3e 3d 30 20 26 26 20 69 6e 64  ->a; i>=0 && ind
2b727 65 78 61 62 6c 65 3b 20 69 2d 2d 2c 20 70 4f 72  exable; i--, pOr
2b728 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Term++){.    if(
2b729 20 28 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72   (pOrTerm->eOper
2b72a 61 74 6f 72 20 26 20 57 4f 5f 53 49 4e 47 4c 45  ator & WO_SINGLE
2b72b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 57 68  )==0 ){.      Wh
2b72c 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70 41 6e 64  ereAndInfo *pAnd
2b72d 49 6e 66 6f 3b 0a 20 20 20 20 20 20 61 73 73 65  Info;.      asse
2b72e 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70  rt( pOrTerm->eOp
2b72f 65 72 61 74 6f 72 3d 3d 30 20 29 3b 0a 20 20 20  erator==0 );.   
2b730 20 20 20 61 73 73 65 72 74 28 20 28 70 4f 72 54     assert( (pOrT
2b731 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28  erm->wtFlags & (
2b732 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 7c 54 45 52  TERM_ANDINFO|TER
2b733 4d 5f 4f 52 49 4e 46 4f 29 29 3d 3d 30 20 29 3b  M_ORINFO))==0 );
2b734 0a 20 20 20 20 20 20 63 68 6e 67 54 6f 49 4e 20  .      chngToIN 
2b735 3d 20 30 3b 0a 20 20 20 20 20 20 70 41 6e 64 49  = 0;.      pAndI
2b736 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  nfo = sqlite3DbM
2b737 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
2b738 65 6f 66 28 2a 70 41 6e 64 49 6e 66 6f 29 29 3b  eof(*pAndInfo));
2b739 0a 20 20 20 20 20 20 69 66 28 20 70 41 6e 64 49  .      if( pAndI
2b73a 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 57  nfo ){.        W
2b73b 68 65 72 65 43 6c 61 75 73 65 20 2a 70 41 6e 64  hereClause *pAnd
2b73c 57 43 3b 0a 20 20 20 20 20 20 20 20 57 68 65 72  WC;.        Wher
2b73d 65 54 65 72 6d 20 2a 70 41 6e 64 54 65 72 6d 3b  eTerm *pAndTerm;
2b73e 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  .        int j;.
2b73f 20 20 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20          Bitmask 
2b740 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  b = 0;.        p
2b741 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e  OrTerm->u.pAndIn
2b742 66 6f 20 3d 20 70 41 6e 64 49 6e 66 6f 3b 0a 20  fo = pAndInfo;. 
2b743 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e         pOrTerm->
2b744 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
2b745 41 4e 44 49 4e 46 4f 3b 0a 20 20 20 20 20 20 20  ANDINFO;.       
2b746 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
2b747 74 6f 72 20 3d 20 57 4f 5f 41 4e 44 3b 0a 20 20  tor = WO_AND;.  
2b748 20 20 20 20 20 20 70 41 6e 64 57 43 20 3d 20 26        pAndWC = &
2b749 70 41 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20  pAndInfo->wc;.  
2b74a 20 20 20 20 20 20 77 68 65 72 65 43 6c 61 75 73        whereClaus
2b74b 65 49 6e 69 74 28 70 41 6e 64 57 43 2c 20 70 57  eInit(pAndWC, pW
2b74c 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61 73 6b  C->pParse, pMask
2b74d 53 65 74 29 3b 0a 20 20 20 20 20 20 20 20 77 68  Set);.        wh
2b74e 65 72 65 53 70 6c 69 74 28 70 41 6e 64 57 43 2c  ereSplit(pAndWC,
2b74f 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2c   pOrTerm->pExpr,
2b750 20 54 4b 5f 41 4e 44 29 3b 0a 20 20 20 20 20 20   TK_AND);.      
2b751 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c    exprAnalyzeAll
2b752 28 70 53 72 63 2c 20 70 41 6e 64 57 43 29 3b 0a  (pSrc, pAndWC);.
2b753 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
2b754 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2b755 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ed );.        if
2b756 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
2b757 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  led ){.         
2b758 20 66 6f 72 28 6a 3d 30 2c 20 70 41 6e 64 54 65   for(j=0, pAndTe
2b759 72 6d 3d 70 41 6e 64 57 43 2d 3e 61 3b 20 6a 3c  rm=pAndWC->a; j<
2b75a 70 41 6e 64 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a  pAndWC->nTerm; j
2b75b 2b 2b 2c 20 70 41 6e 64 54 65 72 6d 2b 2b 29 7b  ++, pAndTerm++){
2b75c 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
2b75d 65 72 74 28 20 70 41 6e 64 54 65 72 6d 2d 3e 70  ert( pAndTerm->p
2b75e 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 20 20  Expr );.        
2b75f 20 20 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f      if( allowedO
2b760 70 28 70 41 6e 64 54 65 72 6d 2d 3e 70 45 78 70  p(pAndTerm->pExp
2b761 72 2d 3e 6f 70 29 20 29 7b 0a 20 20 20 20 20 20  r->op) ){.      
2b762 20 20 20 20 20 20 20 20 62 20 7c 3d 20 67 65 74          b |= get
2b763 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70  Mask(pMaskSet, p
2b764 41 6e 64 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  AndTerm->leftCur
2b765 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  sor);.          
2b766 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
2b767 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2b768 20 20 69 6e 64 65 78 61 62 6c 65 20 26 3d 20 62    indexable &= b
2b769 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2b76a 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d  lse if( pOrTerm-
2b76b 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2b76c 43 4f 50 49 45 44 20 29 7b 0a 20 20 20 20 20 20  COPIED ){.      
2b76d 2f 2a 20 53 6b 69 70 20 74 68 69 73 20 74 65 72  /* Skip this ter
2b76e 6d 20 66 6f 72 20 6e 6f 77 2e 20 20 57 65 20 72  m for now.  We r
2b76f 65 76 69 73 69 74 20 69 74 20 77 68 65 6e 20 77  evisit it when w
2b770 65 20 70 72 6f 63 65 73 73 20 74 68 65 0a 20 20  e process the.  
2b771 20 20 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e      ** correspon
2b772 64 69 6e 67 20 54 45 52 4d 5f 56 49 52 54 55 41  ding TERM_VIRTUA
2b773 4c 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 7d 65  L term */.    }e
2b774 6c 73 65 7b 0a 20 20 20 20 20 20 42 69 74 6d 61  lse{.      Bitma
2b775 73 6b 20 62 3b 0a 20 20 20 20 20 20 62 20 3d 20  sk b;.      b = 
2b776 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
2b777 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  , pOrTerm->leftC
2b778 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66  ursor);.      if
2b779 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61  ( pOrTerm->wtFla
2b77a 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41  gs & TERM_VIRTUA
2b77b 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65  L ){.        Whe
2b77c 72 65 54 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d  reTerm *pOther =
2b77d 20 26 70 4f 72 57 63 2d 3e 61 5b 70 4f 72 54 65   &pOrWc->a[pOrTe
2b77e 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20  rm->iParent];.  
2b77f 20 20 20 20 20 20 62 20 7c 3d 20 67 65 74 4d 61        b |= getMa
2b780 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f 74  sk(pMaskSet, pOt
2b781 68 65 72 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29  her->leftCursor)
2b782 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2b783 69 6e 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a  indexable &= b;.
2b784 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65 72        if( pOrTer
2b785 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f  m->eOperator!=WO
2b786 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 20 20 63  _EQ ){.        c
2b787 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20  hngToIN = 0;.   
2b788 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2b789 20 20 63 68 6e 67 54 6f 49 4e 20 26 3d 20 62 3b    chngToIN &= b;
2b78a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2b78b 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65   }..  /*.  ** Re
2b78c 63 6f 72 64 20 74 68 65 20 73 65 74 20 6f 66 20  cord the set of 
2b78d 74 61 62 6c 65 73 20 74 68 61 74 20 73 61 74 69  tables that sati
2b78e 73 66 79 20 63 61 73 65 20 32 2e 20 20 54 68 65  sfy case 2.  The
2b78f 20 73 65 74 20 6d 69 67 68 74 20 62 65 0a 20 20   set might be.  
2b790 2a 2a 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20  ** empty..  */. 
2b791 20 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61   pOrInfo->indexa
2b792 62 6c 65 20 3d 20 69 6e 64 65 78 61 62 6c 65 3b  ble = indexable;
2b793 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  .  pTerm->eOpera
2b794 74 6f 72 20 3d 20 69 6e 64 65 78 61 62 6c 65 3d  tor = indexable=
2b795 3d 30 20 3f 20 30 20 3a 20 57 4f 5f 4f 52 3b 0a  =0 ? 0 : WO_OR;.
2b796 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 68 6e 67 54  .  /*.  ** chngT
2b797 6f 49 4e 20 68 6f 6c 64 73 20 61 20 73 65 74 20  oIN holds a set 
2b798 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20 2a  of tables that *
2b799 6d 69 67 68 74 2a 20 73 61 74 69 73 66 79 20 63  might* satisfy c
2b79a 61 73 65 20 31 2e 20 20 42 75 74 0a 20 20 2a 2a  ase 1.  But.  **
2b79b 20 77 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73   we have to do s
2b79c 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63  ome additional c
2b79d 68 65 63 6b 69 6e 67 20 74 6f 20 73 65 65 20 69  hecking to see i
2b79e 66 20 63 61 73 65 20 31 20 72 65 61 6c 6c 79 0a  f case 1 really.
2b79f 20 20 2a 2a 20 69 73 20 73 61 74 69 73 66 69 65    ** is satisfie
2b7a0 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 68  d..  */.  if( ch
2b7a1 6e 67 54 6f 49 4e 20 29 7b 0a 20 20 20 20 69 6e  ngToIN ){.    in
2b7a2 74 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d  t okToChngToIN =
2b7a3 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20   0;     /* True 
2b7a4 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  if the conversio
2b7a5 6e 20 74 6f 20 49 4e 20 69 73 20 76 61 6c 69 64  n to IN is valid
2b7a6 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c   */.    int iCol
2b7a7 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 20 20 20 20  umn = -1;       
2b7a8 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65    /* Column inde
2b7a9 78 20 6f 6e 20 6c 68 73 20 6f 66 20 49 4e 20 6f  x on lhs of IN o
2b7aa 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69  perator */.    i
2b7ab 6e 74 20 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b  nt iCursor = -1;
2b7ac 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2b7ad 65 20 63 75 72 73 6f 72 20 63 6f 6d 6d 6f 6e 20  e cursor common 
2b7ae 74 6f 20 61 6c 6c 20 74 65 72 6d 73 20 2a 2f 0a  to all terms */.
2b7af 20 20 20 20 69 6e 74 20 6a 20 3d 20 30 3b 20 20      int j = 0;  
2b7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b7b1 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2b7b2 0a 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20  ..    /* Search 
2b7b3 66 6f 72 20 61 20 74 61 62 6c 65 20 61 6e 64 20  for a table and 
2b7b4 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65  column that appe
2b7b5 61 72 73 20 6f 6e 20 6f 6e 65 20 73 69 64 65 20  ars on one side 
2b7b6 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74  or the.    ** ot
2b7b7 68 65 72 20 6f 66 20 74 68 65 20 3d 3d 20 6f 70  her of the == op
2b7b8 65 72 61 74 6f 72 20 69 6e 20 65 76 65 72 79 20  erator in every 
2b7b9 73 75 62 74 65 72 6d 2e 20 20 54 68 61 74 20 74  subterm.  That t
2b7ba 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a  able and column.
2b7bb 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72      ** will be r
2b7bc 65 63 6f 72 64 65 64 20 69 6e 20 69 43 75 72 73  ecorded in iCurs
2b7bd 6f 72 20 61 6e 64 20 69 43 6f 6c 75 6d 6e 2e 20  or and iColumn. 
2b7be 20 54 68 65 72 65 20 6d 69 67 68 74 20 6e 6f 74   There might not
2b7bf 20 62 65 20 61 6e 79 0a 20 20 20 20 2a 2a 20 73   be any.    ** s
2b7c0 75 63 68 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  uch table and co
2b7c1 6c 75 6d 6e 2e 20 20 53 65 74 20 6f 6b 54 6f 43  lumn.  Set okToC
2b7c2 68 6e 67 54 6f 49 4e 20 69 66 20 61 6e 20 61 70  hngToIN if an ap
2b7c3 70 72 6f 70 72 69 61 74 65 20 74 61 62 6c 65 0a  propriate table.
2b7c4 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d      ** and colum
2b7c5 6e 20 69 73 20 66 6f 75 6e 64 20 62 75 74 20 6c  n is found but l
2b7c6 65 61 76 65 20 6f 6b 54 6f 43 68 6e 67 54 6f 49  eave okToChngToI
2b7c7 4e 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 20 66  N false if not f
2b7c8 6f 75 6e 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ound..    */.   
2b7c9 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 32 20 26 26   for(j=0; j<2 &&
2b7ca 20 21 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 3b 20   !okToChngToIN; 
2b7cb 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70 4f 72 54  j++){.      pOrT
2b7cc 65 72 6d 20 3d 20 70 4f 72 57 63 2d 3e 61 3b 0a  erm = pOrWc->a;.
2b7cd 20 20 20 20 20 20 66 6f 72 28 69 3d 70 4f 72 57        for(i=pOrW
2b7ce 63 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30  c->nTerm-1; i>=0
2b7cf 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b  ; i--, pOrTerm++
2b7d0 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
2b7d1 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65  t( pOrTerm->eOpe
2b7d2 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a  rator==WO_EQ );.
2b7d3 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d          pOrTerm-
2b7d4 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e 54 45 52  >wtFlags &= ~TER
2b7d5 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  M_OR_OK;.       
2b7d6 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65   if( pOrTerm->le
2b7d7 66 74 43 75 72 73 6f 72 3d 3d 69 43 6f 6c 75 6d  ftCursor==iColum
2b7d8 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  n ) continue;.  
2b7d9 20 20 20 20 20 20 69 66 28 20 28 63 68 6e 67 54        if( (chngT
2b7da 6f 49 4e 20 26 20 67 65 74 4d 61 73 6b 28 70 4d  oIN & getMask(pM
2b7db 61 73 6b 53 65 74 2c 20 70 4f 72 54 65 72 6d 2d  askSet, pOrTerm-
2b7dc 3e 6c 65 66 74 43 75 72 73 6f 72 29 29 3d 3d 30  >leftCursor))==0
2b7dd 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2b7de 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70       iColumn = p
2b7df 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f  OrTerm->u.leftCo
2b7e0 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 69 43  lumn;.        iC
2b7e1 75 72 73 6f 72 20 3d 20 70 4f 72 54 65 72 6d 2d  ursor = pOrTerm-
2b7e2 3e 6c 65 66 74 43 75 72 73 6f 72 3b 0a 20 20 20  >leftCursor;.   
2b7e3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b7e4 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3c    }.      if( i<
2b7e5 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  0 ){.        ass
2b7e6 65 72 74 28 20 6a 3d 3d 31 20 29 3b 0a 20 20 20  ert( j==1 );.   
2b7e7 20 20 20 20 20 61 73 73 65 72 74 28 20 28 63 68       assert( (ch
2b7e8 6e 67 54 6f 49 4e 26 28 63 68 6e 67 54 6f 49 4e  ngToIN&(chngToIN
2b7e9 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  -1))==0 );.     
2b7ea 20 20 20 61 73 73 65 72 74 28 20 63 68 6e 67 54     assert( chngT
2b7eb 6f 49 4e 3d 3d 67 65 74 4d 61 73 6b 28 70 4d 61  oIN==getMask(pMa
2b7ec 73 6b 53 65 74 2c 20 69 43 6f 6c 75 6d 6e 29 20  skSet, iColumn) 
2b7ed 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
2b7ee 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2b7ef 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 31  okToChngToIN = 1
2b7f0 3b 0a 20 20 20 20 20 20 66 6f 72 28 3b 20 69 3e  ;.      for(; i>
2b7f1 3d 30 20 26 26 20 6f 6b 54 6f 43 68 6e 67 54 6f  =0 && okToChngTo
2b7f2 49 4e 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d  IN; i--, pOrTerm
2b7f3 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  ++){.        ass
2b7f4 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f  ert( pOrTerm->eO
2b7f5 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29  perator==WO_EQ )
2b7f6 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f  ;.        if( pO
2b7f7 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2b7f8 72 21 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20 20  r!=iCursor ){.  
2b7f9 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d          pOrTerm-
2b7fa 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e 54 45 52  >wtFlags &= ~TER
2b7fb 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  M_OR_OK;.       
2b7fc 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 54 65   }else if( pOrTe
2b7fd 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e  rm->u.leftColumn
2b7fe 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  !=iColumn ){.   
2b7ff 20 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54         okToChngT
2b800 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  oIN = 0;.       
2b801 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2b802 20 20 69 6e 74 20 61 66 66 4c 65 66 74 2c 20 61    int affLeft, a
2b803 66 66 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  ffRight;.       
2b804 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 69 67     /* If the rig
2b805 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20  ht-hand side is 
2b806 61 6c 73 6f 20 61 20 63 6f 6c 75 6d 6e 2c 20 74  also a column, t
2b807 68 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 69  hen the affiniti
2b808 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  es.          ** 
2b809 6f 66 20 62 6f 74 68 20 72 69 67 68 74 20 61 6e  of both right an
2b80a 64 20 6c 65 66 74 20 73 69 64 65 73 20 6d 75 73  d left sides mus
2b80b 74 20 62 65 20 73 75 63 68 20 74 68 61 74 20 6e  t be such that n
2b80c 6f 20 74 79 70 65 0a 20 20 20 20 20 20 20 20 20  o type.         
2b80d 20 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20   ** conversions 
2b80e 61 72 65 20 72 65 71 75 69 72 65 64 20 6f 6e 20  are required on 
2b80f 74 68 65 20 72 69 67 68 74 2e 20 20 28 54 69 63  the right.  (Tic
2b810 6b 65 74 20 23 32 32 34 39 29 0a 20 20 20 20 20  ket #2249).     
2b811 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2b812 20 20 61 66 66 52 69 67 68 74 20 3d 20 73 71 6c    affRight = sql
2b813 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
2b814 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d  (pOrTerm->pExpr-
2b815 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  >pRight);.      
2b816 20 20 20 20 61 66 66 4c 65 66 74 20 3d 20 73 71      affLeft = sq
2b817 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
2b818 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  y(pOrTerm->pExpr
2b819 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  ->pLeft);.      
2b81a 20 20 20 20 69 66 28 20 61 66 66 52 69 67 68 74      if( affRight
2b81b 21 3d 30 20 26 26 20 61 66 66 52 69 67 68 74 21  !=0 && affRight!
2b81c 3d 61 66 66 4c 65 66 74 20 29 7b 0a 20 20 20 20  =affLeft ){.    
2b81d 20 20 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67          okToChng
2b81e 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20  ToIN = 0;.      
2b81f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2b820 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e         pOrTerm->
2b821 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
2b822 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20  OR_OK;.         
2b823 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2b824 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
2b825 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
2b826 2c 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 69  , okToChngToIN i
2b827 73 20 74 72 75 65 20 69 66 20 6f 72 69 67 69 6e  s true if origin
2b828 61 6c 20 70 54 65 72 6d 20 73 61 74 69 73 66 69  al pTerm satisfi
2b829 65 73 0a 20 20 20 20 2a 2a 20 63 61 73 65 20 31  es.    ** case 1
2b82a 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c  .  In that case,
2b82b 20 63 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77   construct a new
2b82c 20 76 69 72 74 75 61 6c 20 74 65 72 6d 20 74 68   virtual term th
2b82d 61 74 20 69 73 20 0a 20 20 20 20 2a 2a 20 70 54  at is .    ** pT
2b82e 65 72 6d 20 63 6f 6e 76 65 72 74 65 64 20 69 6e  erm converted in
2b82f 74 6f 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f  to an IN operato
2b830 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  r..    */.    if
2b831 28 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 29  ( okToChngToIN )
2b832 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 44  {.      Expr *pD
2b833 75 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  up;            /
2b834 2a 20 41 20 74 72 61 6e 73 69 65 6e 74 20 64 75  * A transient du
2b835 70 6c 69 63 61 74 65 20 65 78 70 72 65 73 73 69  plicate expressi
2b836 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  on */.      Expr
2b837 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 30 3b  List *pList = 0;
2b838 20 20 20 2f 2a 20 54 68 65 20 52 48 53 20 6f 66     /* The RHS of
2b839 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
2b83a 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
2b83b 70 4c 65 66 74 20 3d 20 30 3b 20 20 20 20 20 20  pLeft = 0;      
2b83c 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66 20 74   /* The LHS of t
2b83d 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a  he IN operator *
2b83e 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e  /.      Expr *pN
2b83f 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ew;            /
2b840 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 49  * The complete I
2b841 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 0a 20  N operator */.. 
2b842 20 20 20 20 20 66 6f 72 28 69 3d 70 4f 72 57 63       for(i=pOrWc
2b843 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72 54 65  ->nTerm-1, pOrTe
2b844 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d  rm=pOrWc->a; i>=
2b845 30 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b  0; i--, pOrTerm+
2b846 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
2b847 28 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67  (pOrTerm->wtFlag
2b848 73 20 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29 3d  s & TERM_OR_OK)=
2b849 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
2b84a 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2b84b 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  OrTerm->eOperato
2b84c 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20  r==WO_EQ );.    
2b84d 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54      assert( pOrT
2b84e 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d  erm->leftCursor=
2b84f 3d 69 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20  =iCursor );.    
2b850 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54      assert( pOrT
2b851 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d  erm->u.leftColum
2b852 6e 3d 3d 69 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20  n==iColumn );.  
2b853 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c        pDup = sql
2b854 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2b855 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e  pOrTerm->pExpr->
2b856 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20  pRight, 0);.    
2b857 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69      pList = sqli
2b858 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2b859 64 28 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70  d(pWC->pParse, p
2b85a 4c 69 73 74 2c 20 70 44 75 70 2c 20 30 29 3b 0a  List, pDup, 0);.
2b85b 20 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20          pLeft = 
2b85c 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e  pOrTerm->pExpr->
2b85d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pLeft;.      }. 
2b85e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65       assert( pLe
2b85f 66 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ft!=0 );.      p
2b860 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Dup = sqlite3Exp
2b861 72 44 75 70 28 64 62 2c 20 70 4c 65 66 74 2c 20  rDup(db, pLeft, 
2b862 30 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d  0);.      pNew =
2b863 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
2b864 20 54 4b 5f 49 4e 2c 20 70 44 75 70 2c 20 30 2c   TK_IN, pDup, 0,
2b865 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   0);.      if( p
2b866 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 69  New ){.        i
2b867 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20  nt idxNew;.     
2b868 20 20 20 74 72 61 6e 73 66 65 72 4a 6f 69 6e 4d     transferJoinM
2b869 61 72 6b 69 6e 67 73 28 70 4e 65 77 2c 20 70 45  arkings(pNew, pE
2b86a 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 61 73  xpr);.        as
2b86b 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
2b86c 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 50 5f  operty(pNew, EP_
2b86d 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20  xIsSelect) );.  
2b86e 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c        pNew->x.pL
2b86f 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20  ist = pList;.   
2b870 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68       idxNew = wh
2b871 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
2b872 70 57 43 2c 20 70 4e 65 77 2c 20 54 45 52 4d 5f  pWC, pNew, TERM_
2b873 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e  VIRTUAL|TERM_DYN
2b874 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 74  AMIC);.        t
2b875 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 3d  estcase( idxNew=
2b876 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 65 78  =0 );.        ex
2b877 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20  prAnalyze(pSrc, 
2b878 70 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20  pWC, idxNew);.  
2b879 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70        pTerm = &p
2b87a 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a  WC->a[idxTerm];.
2b87b 20 20 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69          pWC->a[i
2b87c 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20 3d  dxNew].iParent =
2b87d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20   idxTerm;.      
2b87e 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20    pTerm->nChild 
2b87f 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 1;.      }else
2b880 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2b881 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2b882 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20  db, pList);.    
2b883 20 20 7d 0a 20 20 20 20 20 20 70 54 65 72 6d 2d    }.      pTerm-
2b884 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 30 3b 20  >eOperator = 0; 
2b885 20 2f 2a 20 63 61 73 65 20 31 20 74 72 75 6d 70   /* case 1 trump
2b886 73 20 63 61 73 65 20 32 20 2a 2f 0a 20 20 20 20  s case 2 */.    
2b887 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  }.  }.}.#endif /
2b888 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f  * !SQLITE_OMIT_O
2b889 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 26  R_OPTIMIZATION &
2b88a 26 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  & !SQLITE_OMIT_S
2b88b 55 42 51 55 45 52 59 20 2a 2f 0a 0a 0a 2f 2a 0a  UBQUERY */.../*.
2b88c 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20  ** The input to 
2b88d 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2b88e 61 6e 20 57 68 65 72 65 54 65 72 6d 20 73 74 72  an WhereTerm str
2b88f 75 63 74 75 72 65 20 77 69 74 68 20 6f 6e 6c 79  ucture with only
2b890 20 74 68 65 0a 2a 2a 20 22 70 45 78 70 72 22 20   the.** "pExpr" 
2b891 66 69 65 6c 64 20 66 69 6c 6c 65 64 20 69 6e 2e  field filled in.
2b892 20 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69    The job of thi
2b893 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 6f 20  s routine is to 
2b894 61 6e 61 6c 79 7a 65 20 74 68 65 0a 2a 2a 20 73  analyze the.** s
2b895 75 62 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  ubexpression and
2b896 20 70 6f 70 75 6c 61 74 65 20 61 6c 6c 20 74 68   populate all th
2b897 65 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20 6f  e other fields o
2b898 66 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 0a  f the WhereTerm.
2b899 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ** structure..**
2b89a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
2b89b 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20  ssion is of the 
2b89c 66 6f 72 6d 20 22 3c 65 78 70 72 3e 20 3c 6f 70  form "<expr> <op
2b89d 3e 20 58 22 20 69 74 20 67 65 74 73 20 63 6f 6d  > X" it gets com
2b89e 6d 75 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20  muted.** to the 
2b89f 73 74 61 6e 64 61 72 64 20 66 6f 72 6d 20 6f 66  standard form of
2b8a0 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22   "X <op> <expr>"
2b8a1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65  ..**.** If the e
2b8a2 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20  xpression is of 
2b8a3 74 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e  the form "X <op>
2b8a4 20 59 22 20 77 68 65 72 65 20 62 6f 74 68 20 58   Y" where both X
2b8a5 20 61 6e 64 20 59 20 61 72 65 0a 2a 2a 20 63 6f   and Y are.** co
2b8a6 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 74 68 65 20  lumns, then the 
2b8a7 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65 73 73  original express
2b8a8 69 6f 6e 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ion is unchanged
2b8a9 20 61 6e 64 20 61 20 6e 65 77 20 76 69 72 74 75   and a new virtu
2b8aa 61 6c 0a 2a 2a 20 74 65 72 6d 20 6f 66 20 74 68  al.** term of th
2b8ab 65 20 66 6f 72 6d 20 22 59 20 3c 6f 70 3e 20 58  e form "Y <op> X
2b8ac 22 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  " is added to th
2b8ad 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 61  e WHERE clause a
2b8ae 6e 64 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 73  nd.** analyzed s
2b8af 65 70 61 72 61 74 65 6c 79 2e 20 20 54 68 65 20  eparately.  The 
2b8b0 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 20 69 73  original term is
2b8b1 20 6d 61 72 6b 65 64 20 77 69 74 68 20 54 45 52   marked with TER
2b8b2 4d 5f 43 4f 50 49 45 44 0a 2a 2a 20 61 6e 64 20  M_COPIED.** and 
2b8b3 74 68 65 20 6e 65 77 20 74 65 72 6d 20 69 73 20  the new term is 
2b8b4 6d 61 72 6b 65 64 20 77 69 74 68 20 54 45 52 4d  marked with TERM
2b8b5 5f 44 59 4e 41 4d 49 43 20 28 62 65 63 61 75 73  _DYNAMIC (becaus
2b8b6 65 20 69 74 27 73 20 70 45 78 70 72 0a 2a 2a 20  e it's pExpr.** 
2b8b7 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72 65 65  needs to be free
2b8b8 64 20 77 69 74 68 20 74 68 65 20 57 68 65 72 65  d with the Where
2b8b9 43 6c 61 75 73 65 29 20 61 6e 64 20 54 45 52 4d  Clause) and TERM
2b8ba 5f 56 49 52 54 55 41 4c 20 28 62 65 63 61 75 73  _VIRTUAL (becaus
2b8bb 65 20 69 74 0a 2a 2a 20 69 73 20 61 20 63 6f 6d  e it.** is a com
2b8bc 6d 75 74 65 64 20 63 6f 70 79 20 6f 66 20 61 20  muted copy of a 
2b8bd 70 72 69 6f 72 20 74 65 72 6d 2e 29 20 20 54 68  prior term.)  Th
2b8be 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 20  e original term 
2b8bf 68 61 73 20 6e 43 68 69 6c 64 3d 31 0a 2a 2a 20  has nChild=1.** 
2b8c0 61 6e 64 20 74 68 65 20 63 6f 70 79 20 68 61 73  and the copy has
2b8c1 20 69 64 78 50 61 72 65 6e 74 20 73 65 74 20 74   idxParent set t
2b8c2 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  o the index of t
2b8c3 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d  he original term
2b8c4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2b8c5 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 0a 20 20   exprAnalyze(.  
2b8c6 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20  SrcList *pSrc,  
2b8c7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65            /* the
2b8c8 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
2b8c9 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2b8ca 57 43 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 74  WC,         /* t
2b8cb 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2b8cc 2a 2f 0a 20 20 69 6e 74 20 69 64 78 54 65 72 6d  */.  int idxTerm
2b8cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b8ce 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 74  * Index of the t
2b8cf 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  erm to be analyz
2b8d0 65 64 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65  ed */.){.  Where
2b8d1 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20  Term *pTerm;    
2b8d2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2b8d3 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e  he term to be an
2b8d4 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 57 68 65 72  alyzed */.  Wher
2b8d5 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  eMaskSet *pMaskS
2b8d6 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  et;          /* 
2b8d7 53 65 74 20 6f 66 20 74 61 62 6c 65 20 69 6e 64  Set of table ind
2b8d8 65 78 20 6d 61 73 6b 73 20 2a 2f 0a 20 20 45 78  ex masks */.  Ex
2b8d9 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20  pr *pExpr;      
2b8da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b8db 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
2b8dc 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20   to be analyzed 
2b8dd 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65  */.  Bitmask pre
2b8de 72 65 71 4c 65 66 74 3b 20 20 20 20 20 20 20 20  reqLeft;        
2b8df 20 20 20 20 20 20 2f 2a 20 50 72 65 72 65 71 75        /* Prerequ
2b8e0 65 73 69 74 65 73 20 6f 66 20 74 68 65 20 70 45  esites of the pE
2b8e1 78 70 72 2d 3e 70 4c 65 66 74 20 2a 2f 0a 20 20  xpr->pLeft */.  
2b8e2 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41 6c  Bitmask prereqAl
2b8e3 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
2b8e4 20 2f 2a 20 50 72 65 72 65 71 75 65 73 69 74 65   /* Prerequesite
2b8e5 73 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a 20 20  s of pExpr */.  
2b8e6 42 69 74 6d 61 73 6b 20 65 78 74 72 61 52 69 67  Bitmask extraRig
2b8e7 68 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 50  ht = 0;.  int nP
2b8e8 61 74 74 65 72 6e 3b 0a 20 20 69 6e 74 20 69 73  attern;.  int is
2b8e9 43 6f 6d 70 6c 65 74 65 3b 0a 20 20 69 6e 74 20  Complete;.  int 
2b8ea 6e 6f 43 61 73 65 3b 0a 20 20 69 6e 74 20 6f 70  noCase;.  int op
2b8eb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2b8ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
2b8ed 70 2d 6c 65 76 65 6c 20 6f 70 65 72 61 74 6f 72  p-level operator
2b8ee 2e 20 20 70 45 78 70 72 2d 3e 6f 70 20 2a 2f 0a  .  pExpr->op */.
2b8ef 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20    Parse *pParse 
2b8f0 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 20 20  = pWC->pParse;  
2b8f1 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2b8f2 6e 74 65 78 74 20 2a 2f 0a 20 20 73 71 6c 69 74  ntext */.  sqlit
2b8f3 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2b8f4 3e 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 44  >db;        /* D
2b8f5 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2b8f6 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d  on */..  if( db-
2b8f7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2b8f8 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
2b8f9 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d  .  pTerm = &pWC-
2b8fa 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 70  >a[idxTerm];.  p
2b8fb 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e 70  MaskSet = pWC->p
2b8fc 4d 61 73 6b 53 65 74 3b 0a 20 20 70 45 78 70 72  MaskSet;.  pExpr
2b8fd 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b   = pTerm->pExpr;
2b8fe 0a 20 20 70 72 65 72 65 71 4c 65 66 74 20 3d 20  .  prereqLeft = 
2b8ff 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2b900 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e  MaskSet, pExpr->
2b901 70 4c 65 66 74 29 3b 0a 20 20 6f 70 20 3d 20 70  pLeft);.  op = p
2b902 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20  Expr->op;.  if( 
2b903 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20 20  op==TK_IN ){.   
2b904 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
2b905 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20  pRight==0 );.   
2b906 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
2b907 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
2b908 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20  IsSelect) ){.   
2b909 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71     pTerm->prereq
2b90a 52 69 67 68 74 20 3d 20 65 78 70 72 53 65 6c 65  Right = exprSele
2b90b 63 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  ctTableUsage(pMa
2b90c 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 78 2e  skSet, pExpr->x.
2b90d 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 7d 65  pSelect);.    }e
2b90e 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 65 72 6d  lse{.      pTerm
2b90f 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20  ->prereqRight = 
2b910 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61  exprListTableUsa
2b911 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78  ge(pMaskSet, pEx
2b912 70 72 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20  pr->x.pList);.  
2b913 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
2b914 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b  op==TK_ISNULL ){
2b915 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72  .    pTerm->prer
2b916 65 71 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 7d  eqRight = 0;.  }
2b917 65 6c 73 65 7b 0a 20 20 20 20 70 54 65 72 6d 2d  else{.    pTerm-
2b918 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 65  >prereqRight = e
2b919 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
2b91a 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 70  askSet, pExpr->p
2b91b 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 70 72  Right);.  }.  pr
2b91c 65 72 65 71 41 6c 6c 20 3d 20 65 78 70 72 54 61  ereqAll = exprTa
2b91d 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2b91e 74 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28  t, pExpr);.  if(
2b91f 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
2b920 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a  (pExpr, EP_FromJ
2b921 6f 69 6e 29 20 29 7b 0a 20 20 20 20 42 69 74 6d  oin) ){.    Bitm
2b922 61 73 6b 20 78 20 3d 20 67 65 74 4d 61 73 6b 28  ask x = getMask(
2b923 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d  pMaskSet, pExpr-
2b924 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  >iRightJoinTable
2b925 29 3b 0a 20 20 20 20 70 72 65 72 65 71 41 6c 6c  );.    prereqAll
2b926 20 7c 3d 20 78 3b 0a 20 20 20 20 65 78 74 72 61   |= x;.    extra
2b927 52 69 67 68 74 20 3d 20 78 2d 31 3b 20 20 2f 2a  Right = x-1;  /*
2b928 20 4f 4e 20 63 6c 61 75 73 65 20 74 65 72 6d 73   ON clause terms
2b929 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64   may not be used
2b92a 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 0a 20   with an index. 
2b92b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b92c 20 20 20 20 20 20 2a 2a 20 6f 6e 20 6c 65 66 74        ** on left
2b92d 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54   table of a LEFT
2b92e 20 4a 4f 49 4e 2e 20 20 54 69 63 6b 65 74 20 23   JOIN.  Ticket #
2b92f 33 30 31 35 20 2a 2f 0a 20 20 7d 0a 20 20 70 54  3015 */.  }.  pT
2b930 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d  erm->prereqAll =
2b931 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20 70 54   prereqAll;.  pT
2b932 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20  erm->leftCursor 
2b933 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d 3e 69  = -1;.  pTerm->i
2b934 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a 20 20 70  Parent = -1;.  p
2b935 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2b936 3d 20 30 3b 0a 20 20 69 66 28 20 61 6c 6c 6f 77  = 0;.  if( allow
2b937 65 64 4f 70 28 6f 70 29 20 26 26 20 28 70 54 65  edOp(op) && (pTe
2b938 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20  rm->prereqRight 
2b939 26 20 70 72 65 72 65 71 4c 65 66 74 29 3d 3d 30  & prereqLeft)==0
2b93a 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4c   ){.    Expr *pL
2b93b 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  eft = pExpr->pLe
2b93c 66 74 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 52  ft;.    Expr *pR
2b93d 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 52  ight = pExpr->pR
2b93e 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20 70 4c  ight;.    if( pL
2b93f 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  eft->op==TK_COLU
2b940 4d 4e 20 29 7b 0a 20 20 20 20 20 20 70 54 65 72  MN ){.      pTer
2b941 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20  m->leftCursor = 
2b942 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20  pLeft->iTable;. 
2b943 20 20 20 20 20 70 54 65 72 6d 2d 3e 75 2e 6c 65       pTerm->u.le
2b944 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74  ftColumn = pLeft
2b945 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
2b946 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2b947 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b  r = operatorMask
2b948 28 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  (op);.    }.    
2b949 69 66 28 20 70 52 69 67 68 74 20 26 26 20 70 52  if( pRight && pR
2b94a 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  ight->op==TK_COL
2b94b 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 57 68 65  UMN ){.      Whe
2b94c 72 65 54 65 72 6d 20 2a 70 4e 65 77 3b 0a 20 20  reTerm *pNew;.  
2b94d 20 20 20 20 45 78 70 72 20 2a 70 44 75 70 3b 0a      Expr *pDup;.
2b94e 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d        if( pTerm-
2b94f 3e 6c 65 66 74 43 75 72 73 6f 72 3e 3d 30 20 29  >leftCursor>=0 )
2b950 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 64  {.        int id
2b951 78 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70 44  xNew;.        pD
2b952 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  up = sqlite3Expr
2b953 44 75 70 28 64 62 2c 20 70 45 78 70 72 2c 20 30  Dup(db, pExpr, 0
2b954 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64  );.        if( d
2b955 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2b956 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
2b957 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2b958 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20  b, pDup);.      
2b959 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2b95a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 64      }.        id
2b95b 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75  xNew = whereClau
2b95c 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 44  seInsert(pWC, pD
2b95d 75 70 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  up, TERM_VIRTUAL
2b95e 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a  |TERM_DYNAMIC);.
2b95f 20 20 20 20 20 20 20 20 69 66 28 20 69 64 78 4e          if( idxN
2b960 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ew==0 ) return;.
2b961 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26          pNew = &
2b962 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a  pWC->a[idxNew];.
2b963 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 50          pNew->iP
2b964 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b  arent = idxTerm;
2b965 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d  .        pTerm =
2b966 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
2b967 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d  ];.        pTerm
2b968 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20  ->nChild = 1;.  
2b969 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46        pTerm->wtF
2b96a 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 50  lags |= TERM_COP
2b96b 49 45 44 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  IED;.      }else
2b96c 7b 0a 20 20 20 20 20 20 20 20 70 44 75 70 20 3d  {.        pDup =
2b96d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20   pExpr;.        
2b96e 70 4e 65 77 20 3d 20 70 54 65 72 6d 3b 0a 20 20  pNew = pTerm;.  
2b96f 20 20 20 20 7d 0a 20 20 20 20 20 20 65 78 70 72      }.      expr
2b970 43 6f 6d 6d 75 74 65 28 70 50 61 72 73 65 2c 20  Commute(pParse, 
2b971 70 44 75 70 29 3b 0a 20 20 20 20 20 20 70 4c 65  pDup);.      pLe
2b972 66 74 20 3d 20 70 44 75 70 2d 3e 70 4c 65 66 74  ft = pDup->pLeft
2b973 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c 65  ;.      pNew->le
2b974 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74  ftCursor = pLeft
2b975 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->iTable;.      
2b976 70 4e 65 77 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75  pNew->u.leftColu
2b977 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c  mn = pLeft->iCol
2b978 75 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  umn;.      pNew-
2b979 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 70  >prereqRight = p
2b97a 72 65 72 65 71 4c 65 66 74 3b 0a 20 20 20 20 20  rereqLeft;.     
2b97b 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 41 6c 6c   pNew->prereqAll
2b97c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20   = prereqAll;.  
2b97d 20 20 20 20 70 4e 65 77 2d 3e 65 4f 70 65 72 61      pNew->eOpera
2b97e 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61  tor = operatorMa
2b97f 73 6b 28 70 44 75 70 2d 3e 6f 70 29 3b 0a 20 20  sk(pDup->op);.  
2b980 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66    }.  }..#ifndef
2b981 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 45 54   SQLITE_OMIT_BET
2b982 57 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  WEEN_OPTIMIZATIO
2b983 4e 0a 20 20 2f 2a 20 49 66 20 61 20 74 65 72 6d  N.  /* If a term
2b984 20 69 73 20 74 68 65 20 42 45 54 57 45 45 4e 20   is the BETWEEN 
2b985 6f 70 65 72 61 74 6f 72 2c 20 63 72 65 61 74 65  operator, create
2b986 20 74 77 6f 20 6e 65 77 20 76 69 72 74 75 61 6c   two new virtual
2b987 20 74 65 72 6d 73 0a 20 20 2a 2a 20 74 68 61 74   terms.  ** that
2b988 20 64 65 66 69 6e 65 20 74 68 65 20 72 61 6e 67   define the rang
2b989 65 20 74 68 61 74 20 74 68 65 20 42 45 54 57 45  e that the BETWE
2b98a 45 4e 20 69 6d 70 6c 65 6d 65 6e 74 73 2e 20 20  EN implements.  
2b98b 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20 20 2a  For example:.  *
2b98c 2a 0a 20 20 2a 2a 20 20 20 20 20 20 61 20 42 45  *.  **      a BE
2b98d 54 57 45 45 4e 20 62 20 41 4e 44 20 63 0a 20 20  TWEEN b AND c.  
2b98e 2a 2a 0a 20 20 2a 2a 20 69 73 20 63 6f 6e 76 65  **.  ** is conve
2b98f 72 74 65 64 20 69 6e 74 6f 3a 0a 20 20 2a 2a 0a  rted into:.  **.
2b990 20 20 2a 2a 20 20 20 20 20 20 28 61 20 42 45 54    **      (a BET
2b991 57 45 45 4e 20 62 20 41 4e 44 20 63 29 20 41 4e  WEEN b AND c) AN
2b992 44 20 28 61 3e 3d 62 29 20 41 4e 44 20 28 61 3c  D (a>=b) AND (a<
2b993 3d 63 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  =c).  **.  ** Th
2b994 65 20 74 77 6f 20 6e 65 77 20 74 65 72 6d 73 20  e two new terms 
2b995 61 72 65 20 61 64 64 65 64 20 6f 6e 74 6f 20 74  are added onto t
2b996 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 68  he end of the Wh
2b997 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65 63 74  ereClause object
2b998 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 65 77 20 74  ..  ** The new t
2b999 65 72 6d 73 20 61 72 65 20 22 64 79 6e 61 6d 69  erms are "dynami
2b99a 63 22 20 61 6e 64 20 61 72 65 20 63 68 69 6c 64  c" and are child
2b99b 72 65 6e 20 6f 66 20 74 68 65 20 6f 72 69 67 69  ren of the origi
2b99c 6e 61 6c 20 42 45 54 57 45 45 4e 0a 20 20 2a 2a  nal BETWEEN.  **
2b99d 20 74 65 72 6d 2e 20 20 54 68 61 74 20 6d 65 61   term.  That mea
2b99e 6e 73 20 74 68 61 74 20 69 66 20 74 68 65 20 42  ns that if the B
2b99f 45 54 57 45 45 4e 20 74 65 72 6d 20 69 73 20 63  ETWEEN term is c
2b9a0 6f 64 65 64 2c 20 74 68 65 20 63 68 69 6c 64 72  oded, the childr
2b9a1 65 6e 20 61 72 65 0a 20 20 2a 2a 20 73 6b 69 70  en are.  ** skip
2b9a2 70 65 64 2e 20 20 4f 72 2c 20 69 66 20 74 68 65  ped.  Or, if the
2b9a3 20 63 68 69 6c 64 72 65 6e 20 61 72 65 20 73 61   children are sa
2b9a4 74 69 73 66 69 65 64 20 62 79 20 61 6e 20 69 6e  tisfied by an in
2b9a5 64 65 78 2c 20 74 68 65 20 6f 72 69 67 69 6e 61  dex, the origina
2b9a6 6c 0a 20 20 2a 2a 20 42 45 54 57 45 45 4e 20 74  l.  ** BETWEEN t
2b9a7 65 72 6d 20 69 73 20 73 6b 69 70 70 65 64 2e 0a  erm is skipped..
2b9a8 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20    */.  else if( 
2b9a9 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 42 45  pExpr->op==TK_BE
2b9aa 54 57 45 45 4e 20 26 26 20 70 57 43 2d 3e 6f 70  TWEEN && pWC->op
2b9ab 3d 3d 54 4b 5f 41 4e 44 20 29 7b 0a 20 20 20 20  ==TK_AND ){.    
2b9ac 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
2b9ad 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2b9ae 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
2b9af 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
2b9b0 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b 5f 47 45 2c   ops[] = {TK_GE,
2b9b1 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20 20 61 73 73   TK_LE};.    ass
2b9b2 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b  ert( pList!=0 );
2b9b3 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69  .    assert( pLi
2b9b4 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 20 29 3b 0a  st->nExpr==2 );.
2b9b5 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32      for(i=0; i<2
2b9b6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78  ; i++){.      Ex
2b9b7 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20 20  pr *pNewExpr;.  
2b9b8 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a      int idxNew;.
2b9b9 20 20 20 20 20 20 70 4e 65 77 45 78 70 72 20 3d        pNewExpr =
2b9ba 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
2b9bb 20 6f 70 73 5b 69 5d 2c 20 73 71 6c 69 74 65 33   ops[i], sqlite3
2b9bc 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 78 70  ExprDup(db, pExp
2b9bd 72 2d 3e 70 4c 65 66 74 2c 20 30 29 2c 0a 20 20  r->pLeft, 0),.  
2b9be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b9bf 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2b9c0 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4c  e3ExprDup(db, pL
2b9c1 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c  ist->a[i].pExpr,
2b9c2 20 30 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 69   0), 0);.      i
2b9c3 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61  dxNew = whereCla
2b9c4 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
2b9c5 4e 65 77 45 78 70 72 2c 20 54 45 52 4d 5f 56 49  NewExpr, TERM_VI
2b9c6 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d  RTUAL|TERM_DYNAM
2b9c7 49 43 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  IC);.      testc
2b9c8 61 73 65 28 20 69 64 78 4e 65 77 3d 3d 30 20 29  ase( idxNew==0 )
2b9c9 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c  ;.      exprAnal
2b9ca 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69  yze(pSrc, pWC, i
2b9cb 64 78 4e 65 77 29 3b 0a 20 20 20 20 20 20 70 54  dxNew);.      pT
2b9cc 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  erm = &pWC->a[id
2b9cd 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 70 57  xTerm];.      pW
2b9ce 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61  C->a[idxNew].iPa
2b9cf 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a  rent = idxTerm;.
2b9d0 20 20 20 20 7d 0a 20 20 20 20 70 54 65 72 6d 2d      }.    pTerm-
2b9d1 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20 7d  >nChild = 2;.  }
2b9d2 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2b9d3 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f  E_OMIT_BETWEEN_O
2b9d4 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a  PTIMIZATION */..
2b9d5 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2b9d6 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49  ITE_OMIT_OR_OPTI
2b9d7 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20 21 64 65  MIZATION) && !de
2b9d8 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2b9d9 54 5f 53 55 42 51 55 45 52 59 29 0a 20 20 2f 2a  T_SUBQUERY).  /*
2b9da 20 41 6e 61 6c 79 7a 65 20 61 20 74 65 72 6d 20   Analyze a term 
2b9db 74 68 61 74 20 69 73 20 63 6f 6d 70 6f 73 65 64  that is composed
2b9dc 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   of two or more 
2b9dd 73 75 62 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74  subterms connect
2b9de 65 64 20 62 79 0a 20 20 2a 2a 20 61 6e 20 4f 52  ed by.  ** an OR
2b9df 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a   operator..  */.
2b9e0 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72    else if( pExpr
2b9e1 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52 20 29 7b 0a 20  ->op==TK_OR ){. 
2b9e2 20 20 20 61 73 73 65 72 74 28 20 70 57 43 2d 3e     assert( pWC->
2b9e3 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20  op==TK_AND );.  
2b9e4 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 4f 72 54    exprAnalyzeOrT
2b9e5 65 72 6d 28 70 53 72 63 2c 20 70 57 43 2c 20 69  erm(pSrc, pWC, i
2b9e6 64 78 54 65 72 6d 29 3b 0a 20 20 7d 0a 23 65 6e  dxTerm);.  }.#en
2b9e7 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2b9e8 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49  IT_OR_OPTIMIZATI
2b9e9 4f 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  ON */..#ifndef S
2b9ea 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f  QLITE_OMIT_LIKE_
2b9eb 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f  OPTIMIZATION.  /
2b9ec 2a 20 41 64 64 20 63 6f 6e 73 74 72 61 69 6e 74  * Add constraint
2b9ed 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20  s to reduce the 
2b9ee 73 65 61 72 63 68 20 73 70 61 63 65 20 6f 6e 20  search space on 
2b9ef 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 20  a LIKE or GLOB. 
2b9f0 20 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20   ** operator..  
2b9f1 2a 2a 0a 20 20 2a 2a 20 41 20 6c 69 6b 65 20 70  **.  ** A like p
2b9f2 61 74 74 65 72 6e 20 6f 66 20 74 68 65 20 66 6f  attern of the fo
2b9f3 72 6d 20 22 78 20 4c 49 4b 45 20 27 61 62 63 25  rm "x LIKE 'abc%
2b9f4 27 22 20 69 73 20 63 68 61 6e 67 65 64 20 69 6e  '" is changed in
2b9f5 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20  to constraints. 
2b9f6 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
2b9f7 20 20 78 3e 3d 27 61 62 63 27 20 41 4e 44 20 78    x>='abc' AND x
2b9f8 3c 27 61 62 64 27 20 41 4e 44 20 78 20 4c 49 4b  <'abd' AND x LIK
2b9f9 45 20 27 61 62 63 25 27 0a 20 20 2a 2a 0a 20 20  E 'abc%'.  **.  
2b9fa 2a 2a 20 54 68 65 20 6c 61 73 74 20 63 68 61 72  ** The last char
2b9fb 61 63 74 65 72 20 6f 66 20 74 68 65 20 70 72 65  acter of the pre
2b9fc 66 69 78 20 22 61 62 63 22 20 69 73 20 69 6e 63  fix "abc" is inc
2b9fd 72 65 6d 65 6e 74 65 64 20 74 6f 20 66 6f 72 6d  remented to form
2b9fe 20 74 68 65 0a 20 20 2a 2a 20 74 65 72 6d 69 6e   the.  ** termin
2b9ff 61 74 69 6f 6e 20 63 6f 6e 64 69 74 69 6f 6e 20  ation condition 
2ba00 22 61 62 64 22 2e 0a 20 20 2a 2f 0a 20 20 69 66  "abd"..  */.  if
2ba01 28 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 70  ( isLikeOrGlob(p
2ba02 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 6e  Parse, pExpr, &n
2ba03 50 61 74 74 65 72 6e 2c 20 26 69 73 43 6f 6d 70  Pattern, &isComp
2ba04 6c 65 74 65 2c 20 26 6e 6f 43 61 73 65 29 0a 20  lete, &noCase). 
2ba05 20 20 20 20 20 20 20 20 26 26 20 70 57 43 2d 3e          && pWC->
2ba06 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 7b 0a 20 20  op==TK_AND ){.  
2ba07 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 2a    Expr *pLeft, *
2ba08 70 52 69 67 68 74 3b 0a 20 20 20 20 45 78 70 72  pRight;.    Expr
2ba09 20 2a 70 53 74 72 31 2c 20 2a 70 53 74 72 32 3b   *pStr1, *pStr2;
2ba0a 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45  .    Expr *pNewE
2ba0b 78 70 72 31 2c 20 2a 70 4e 65 77 45 78 70 72 32  xpr1, *pNewExpr2
2ba0c 3b 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77  ;.    int idxNew
2ba0d 31 2c 20 69 64 78 4e 65 77 32 3b 0a 0a 20 20 20  1, idxNew2;..   
2ba0e 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e   pLeft = pExpr->
2ba0f 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  x.pList->a[1].pE
2ba10 78 70 72 3b 0a 20 20 20 20 70 52 69 67 68 74 20  xpr;.    pRight 
2ba11 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2ba12 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
2ba13 20 20 70 53 74 72 31 20 3d 20 73 71 6c 69 74 65    pStr1 = sqlite
2ba14 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2ba15 4b 5f 53 54 52 49 4e 47 2c 20 30 2c 20 30 2c 20  K_STRING, 0, 0, 
2ba16 30 29 3b 0a 20 20 20 20 69 66 28 20 70 53 74 72  0);.    if( pStr
2ba17 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  1 ){.      sqlit
2ba18 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20  e3TokenCopy(db, 
2ba19 26 70 53 74 72 31 2d 3e 74 6f 6b 65 6e 2c 20 26  &pStr1->token, &
2ba1a 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 29 3b 0a  pRight->token);.
2ba1b 20 20 20 20 20 20 70 53 74 72 31 2d 3e 74 6f 6b        pStr1->tok
2ba1c 65 6e 2e 6e 20 3d 20 6e 50 61 74 74 65 72 6e 3b  en.n = nPattern;
2ba1d 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 74 72 32  .    }.    pStr2
2ba1e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2ba1f 70 28 64 62 2c 20 70 53 74 72 31 2c 20 30 29 3b  p(db, pStr1, 0);
2ba20 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  .    if( !db->ma
2ba21 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2ba22 20 20 20 20 75 38 20 63 2c 20 2a 70 43 3b 0a 20      u8 c, *pC;. 
2ba23 20 20 20 20 20 2f 2a 20 61 73 73 65 72 74 28 20       /* assert( 
2ba24 70 53 74 72 32 2d 3e 74 6f 6b 65 6e 2e 64 79 6e  pStr2->token.dyn
2ba25 20 29 3b 20 2a 2f 0a 20 20 20 20 20 20 70 43 20   ); */.      pC 
2ba26 3d 20 28 75 38 2a 29 26 70 53 74 72 32 2d 3e 74  = (u8*)&pStr2->t
2ba27 6f 6b 65 6e 2e 7a 5b 6e 50 61 74 74 65 72 6e 2d  oken.z[nPattern-
2ba28 31 5d 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 70  1];.      c = *p
2ba29 43 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43  C;.      if( noC
2ba2a 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ase ){.        i
2ba2b 66 28 20 63 3d 3d 27 40 27 20 29 20 69 73 43 6f  f( c=='@' ) isCo
2ba2c 6d 70 6c 65 74 65 20 3d 20 30 3b 0a 20 20 20 20  mplete = 0;.    
2ba2d 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
2ba2e 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 5d 3b 0a  pperToLower[c];.
2ba2f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70        }.      *p
2ba30 43 20 3d 20 63 20 2b 20 31 3b 0a 20 20 20 20 7d  C = c + 1;.    }
2ba31 0a 20 20 20 20 70 4e 65 77 45 78 70 72 31 20 3d  .    pNewExpr1 =
2ba32 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2ba33 61 72 73 65 2c 20 54 4b 5f 47 45 2c 20 73 71 6c  arse, TK_GE, sql
2ba34 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 70  ite3ExprDup(db,p
2ba35 4c 65 66 74 2c 30 29 2c 70 53 74 72 31 2c 30 29  Left,0),pStr1,0)
2ba36 3b 0a 20 20 20 20 69 64 78 4e 65 77 31 20 3d 20  ;.    idxNew1 = 
2ba37 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
2ba38 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 31  t(pWC, pNewExpr1
2ba39 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54  , TERM_VIRTUAL|T
2ba3a 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ERM_DYNAMIC);.  
2ba3b 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e    testcase( idxN
2ba3c 65 77 31 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78  ew1==0 );.    ex
2ba3d 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20  prAnalyze(pSrc, 
2ba3e 70 57 43 2c 20 69 64 78 4e 65 77 31 29 3b 0a 20  pWC, idxNew1);. 
2ba3f 20 20 20 70 4e 65 77 45 78 70 72 32 20 3d 20 73     pNewExpr2 = s
2ba40 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2ba41 73 65 2c 20 54 4b 5f 4c 54 2c 20 73 71 6c 69 74  se, TK_LT, sqlit
2ba42 65 33 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65  e3ExprDup(db,pLe
2ba43 66 74 2c 30 29 2c 70 53 74 72 32 2c 30 29 3b 0a  ft,0),pStr2,0);.
2ba44 20 20 20 20 69 64 78 4e 65 77 32 20 3d 20 77 68      idxNew2 = wh
2ba45 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
2ba46 70 57 43 2c 20 70 4e 65 77 45 78 70 72 32 2c 20  pWC, pNewExpr2, 
2ba47 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
2ba48 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  M_DYNAMIC);.    
2ba49 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77  testcase( idxNew
2ba4a 32 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78 70 72  2==0 );.    expr
2ba4b 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57  Analyze(pSrc, pW
2ba4c 43 2c 20 69 64 78 4e 65 77 32 29 3b 0a 20 20 20  C, idxNew2);.   
2ba4d 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
2ba4e 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 69  [idxTerm];.    i
2ba4f 66 28 20 69 73 43 6f 6d 70 6c 65 74 65 20 29 7b  f( isComplete ){
2ba50 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64  .      pWC->a[id
2ba51 78 4e 65 77 31 5d 2e 69 50 61 72 65 6e 74 20 3d  xNew1].iParent =
2ba52 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20   idxTerm;.      
2ba53 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 32 5d 2e  pWC->a[idxNew2].
2ba54 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72  iParent = idxTer
2ba55 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  m;.      pTerm->
2ba56 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20 20 20  nChild = 2;.    
2ba57 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  }.  }.#endif /* 
2ba58 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45  SQLITE_OMIT_LIKE
2ba59 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
2ba5a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2ba5b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2ba5c 4c 45 0a 20 20 2f 2a 20 41 64 64 20 61 20 57 4f  LE.  /* Add a WO
2ba5d 5f 4d 41 54 43 48 20 61 75 78 69 6c 69 61 72 79  _MATCH auxiliary
2ba5e 20 74 65 72 6d 20 74 6f 20 74 68 65 20 63 6f 6e   term to the con
2ba5f 73 74 72 61 69 6e 74 20 73 65 74 20 69 66 20 74  straint set if t
2ba60 68 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20  he.  ** current 
2ba61 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66  expression is of
2ba62 20 74 68 65 20 66 6f 72 6d 3a 20 20 63 6f 6c 75   the form:  colu
2ba63 6d 6e 20 4d 41 54 43 48 20 65 78 70 72 2e 0a 20  mn MATCH expr.. 
2ba64 20 2a 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61   ** This informa
2ba65 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20  tion is used by 
2ba66 74 68 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d  the xBestIndex m
2ba67 65 74 68 6f 64 73 20 6f 66 0a 20 20 2a 2a 20 76  ethods of.  ** v
2ba68 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 20 20  irtual tables.  
2ba69 54 68 65 20 6e 61 74 69 76 65 20 71 75 65 72 79  The native query
2ba6a 20 6f 70 74 69 6d 69 7a 65 72 20 64 6f 65 73 20   optimizer does 
2ba6b 6e 6f 74 20 61 74 74 65 6d 70 74 0a 20 20 2a 2a  not attempt.  **
2ba6c 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20   to do anything 
2ba6d 77 69 74 68 20 4d 41 54 43 48 20 66 75 6e 63 74  with MATCH funct
2ba6e 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ions..  */.  if(
2ba6f 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e   isMatchOfColumn
2ba70 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69  (pExpr) ){.    i
2ba71 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 45  nt idxNew;.    E
2ba72 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c  xpr *pRight, *pL
2ba73 65 66 74 3b 0a 20 20 20 20 57 68 65 72 65 54 65  eft;.    WhereTe
2ba74 72 6d 20 2a 70 4e 65 77 54 65 72 6d 3b 0a 20 20  rm *pNewTerm;.  
2ba75 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71    Bitmask prereq
2ba76 43 6f 6c 75 6d 6e 2c 20 70 72 65 72 65 71 45 78  Column, prereqEx
2ba77 70 72 3b 0a 0a 20 20 20 20 70 52 69 67 68 74 20  pr;..    pRight 
2ba78 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2ba79 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
2ba7a 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d    pLeft = pExpr-
2ba7b 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  >x.pList->a[1].p
2ba7c 45 78 70 72 3b 0a 20 20 20 20 70 72 65 72 65 71  Expr;.    prereq
2ba7d 45 78 70 72 20 3d 20 65 78 70 72 54 61 62 6c 65  Expr = exprTable
2ba7e 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2ba7f 70 52 69 67 68 74 29 3b 0a 20 20 20 20 70 72 65  pRight);.    pre
2ba80 72 65 71 43 6f 6c 75 6d 6e 20 3d 20 65 78 70 72  reqColumn = expr
2ba81 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2ba82 53 65 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20  Set, pLeft);.   
2ba83 20 69 66 28 20 28 70 72 65 72 65 71 45 78 70 72   if( (prereqExpr
2ba84 20 26 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 29   & prereqColumn)
2ba85 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70  ==0 ){.      Exp
2ba86 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20 20 20  r *pNewExpr;.   
2ba87 20 20 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71     pNewExpr = sq
2ba88 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b  lite3Expr(db, TK
2ba89 5f 4d 41 54 43 48 2c 20 30 2c 20 73 71 6c 69 74  _MATCH, 0, sqlit
2ba8a 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 52  e3ExprDup(db, pR
2ba8b 69 67 68 74 2c 20 30 29 2c 20 30 29 3b 0a 20 20  ight, 0), 0);.  
2ba8c 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65      idxNew = whe
2ba8d 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70  reClauseInsert(p
2ba8e 57 43 2c 20 70 4e 65 77 45 78 70 72 2c 20 54 45  WC, pNewExpr, TE
2ba8f 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f  RM_VIRTUAL|TERM_
2ba90 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
2ba91 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77  testcase( idxNew
2ba92 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4e 65  ==0 );.      pNe
2ba93 77 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  wTerm = &pWC->a[
2ba94 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20 20 70  idxNew];.      p
2ba95 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 52  NewTerm->prereqR
2ba96 69 67 68 74 20 3d 20 70 72 65 72 65 71 45 78 70  ight = prereqExp
2ba97 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72  r;.      pNewTer
2ba98 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20  m->leftCursor = 
2ba99 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20  pLeft->iTable;. 
2ba9a 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 75       pNewTerm->u
2ba9b 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c  .leftColumn = pL
2ba9c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  eft->iColumn;.  
2ba9d 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 65 4f      pNewTerm->eO
2ba9e 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f 4d 41 54  perator = WO_MAT
2ba9f 43 48 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65  CH;.      pNewTe
2baa0 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 69 64  rm->iParent = id
2baa1 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 54 65  xTerm;.      pTe
2baa2 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78  rm = &pWC->a[idx
2baa3 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 70 54 65  Term];.      pTe
2baa4 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a  rm->nChild = 1;.
2baa5 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46        pTerm->wtF
2baa6 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 50  lags |= TERM_COP
2baa7 49 45 44 3b 0a 20 20 20 20 20 20 70 4e 65 77 54  IED;.      pNewT
2baa8 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d  erm->prereqAll =
2baa9 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c   pTerm->prereqAl
2baaa 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  l;.    }.  }.#en
2baab 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2baac 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
2baad 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 76 65 6e 74  */..  /* Prevent
2baae 20 4f 4e 20 63 6c 61 75 73 65 20 74 65 72 6d 73   ON clause terms
2baaf 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20   of a LEFT JOIN 
2bab0 66 72 6f 6d 20 62 65 69 6e 67 20 75 73 65 64 20  from being used 
2bab1 74 6f 20 64 72 69 76 65 0a 20 20 2a 2a 20 61 6e  to drive.  ** an
2bab2 20 69 6e 64 65 78 20 66 6f 72 20 74 61 62 6c 65   index for table
2bab3 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66  s to the left of
2bab4 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a   the join..  */.
2bab5 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52    pTerm->prereqR
2bab6 69 67 68 74 20 7c 3d 20 65 78 74 72 61 52 69 67  ight |= extraRig
2bab7 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ht;.}../*.** Ret
2bab8 75 72 6e 20 54 52 55 45 20 69 66 20 61 6e 79 20  urn TRUE if any 
2bab9 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
2baba 6e 73 20 69 6e 20 70 4c 69 73 74 2d 3e 61 5b 69  ns in pList->a[i
2babb 46 69 72 73 74 2e 2e 2e 5d 20 63 6f 6e 74 61 69  First...] contai
2babc 6e 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65  n.** a reference
2babd 20 74 6f 20 61 6e 79 20 74 61 62 6c 65 20 6f 74   to any table ot
2babe 68 65 72 20 74 68 61 6e 20 74 68 65 20 69 42 61  her than the iBa
2babf 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  se table..*/.sta
2bac0 74 69 63 20 69 6e 74 20 72 65 66 65 72 65 6e 63  tic int referenc
2bac1 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28 0a 20  esOtherTables(. 
2bac2 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2bac3 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65  ,          /* Se
2bac4 61 72 63 68 20 65 78 70 72 65 73 73 69 6f 6e 73  arch expressions
2bac5 20 69 6e 20 74 68 73 20 6c 69 73 74 20 2a 2f 0a   in ths list */.
2bac6 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a    WhereMaskSet *
2bac7 70 4d 61 73 6b 53 65 74 2c 20 20 20 2f 2a 20 4d  pMaskSet,   /* M
2bac8 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c  apping from tabl
2bac9 65 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f  es to bitmaps */
2baca 0a 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20 20  .  int iFirst,  
2bacb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bacc 42 65 20 73 65 61 72 63 68 69 6e 67 20 77 69 74  Be searching wit
2bacd 68 20 74 68 65 20 69 46 69 72 73 74 2d 74 68 20  h the iFirst-th 
2bace 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
2bacf 69 6e 74 20 69 42 61 73 65 20 20 20 20 20 20 20  int iBase       
2bad0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 67 6e            /* Ign
2bad1 6f 72 65 20 72 65 66 65 72 65 6e 63 65 73 20 74  ore references t
2bad2 6f 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  o this table */.
2bad3 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 61 6c 6c  ){.  Bitmask all
2bad4 6f 77 65 64 20 3d 20 7e 67 65 74 4d 61 73 6b 28  owed = ~getMask(
2bad5 70 4d 61 73 6b 53 65 74 2c 20 69 42 61 73 65 29  pMaskSet, iBase)
2bad6 3b 0a 20 20 77 68 69 6c 65 28 20 69 46 69 72 73  ;.  while( iFirs
2bad7 74 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  t<pList->nExpr )
2bad8 7b 0a 20 20 20 20 69 66 28 20 28 65 78 70 72 54  {.    if( (exprT
2bad9 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2bada 65 74 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 46 69  et, pList->a[iFi
2badb 72 73 74 2b 2b 5d 2e 70 45 78 70 72 29 26 61 6c  rst++].pExpr)&al
2badc 6c 6f 77 65 64 29 21 3d 30 20 29 7b 0a 20 20 20  lowed)!=0 ){.   
2badd 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
2bade 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2badf 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  0;.}.../*.** Thi
2bae0 73 20 72 6f 75 74 69 6e 65 20 64 65 63 69 64 65  s routine decide
2bae1 73 20 69 66 20 70 49 64 78 20 63 61 6e 20 62 65  s if pIdx can be
2bae2 20 75 73 65 64 20 74 6f 20 73 61 74 69 73 66 79   used to satisfy
2bae3 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 2a 2a   the ORDER BY.**
2bae4 20 63 6c 61 75 73 65 2e 20 20 49 66 20 69 74 20   clause.  If it 
2bae5 63 61 6e 2c 20 69 74 20 72 65 74 75 72 6e 73 20  can, it returns 
2bae6 31 2e 20 20 49 66 20 70 49 64 78 20 63 61 6e 6e  1.  If pIdx cann
2bae7 6f 74 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a  ot satisfy the.*
2bae8 2a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  * ORDER BY claus
2bae9 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  e, this routine 
2baea 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 0a 2a 2a  returns 0..**.**
2baeb 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 6e 20   pOrderBy is an 
2baec 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2baed 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 73 74  from a SELECT st
2baee 61 74 65 6d 65 6e 74 2e 20 20 70 54 61 62 20 69  atement.  pTab i
2baef 73 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f  s the.** left-mo
2baf0 73 74 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  st table in the 
2baf1 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
2baf2 68 61 74 20 73 61 6d 65 20 53 45 4c 45 43 54 20  hat same SELECT 
2baf3 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 0a 2a 2a  statement and.**
2baf4 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
2baf5 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   cursor number o
2baf6 66 20 22 62 61 73 65 22 2e 20 20 70 49 64 78 20  f "base".  pIdx 
2baf7 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70  is an index on p
2baf8 54 61 62 2e 0a 2a 2a 0a 2a 2a 20 6e 45 71 43 6f  Tab..**.** nEqCo
2baf9 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  l is the number 
2bafa 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 49  of columns of pI
2bafb 64 78 20 74 68 61 74 20 61 72 65 20 75 73 65 64  dx that are used
2bafc 20 61 73 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20   as equality.** 
2bafd 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 41 6e  constraints.  An
2bafe 79 20 6f 66 20 74 68 65 73 65 20 63 6f 6c 75 6d  y of these colum
2baff 6e 73 20 6d 61 79 20 62 65 20 6d 69 73 73 69 6e  ns may be missin
2bb00 67 20 66 72 6f 6d 20 74 68 65 20 4f 52 44 45 52  g from the ORDER
2bb01 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 20 61 6e   BY.** clause an
2bb02 64 20 74 68 65 20 6d 61 74 63 68 20 63 61 6e 20  d the match can 
2bb03 73 74 69 6c 6c 20 62 65 20 61 20 73 75 63 63 65  still be a succe
2bb04 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 74 65  ss..**.** All te
2bb05 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
2bb06 20 42 59 20 74 68 61 74 20 6d 61 74 63 68 20 61   BY that match a
2bb07 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78  gainst the index
2bb08 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 0a   must be either.
2bb09 2a 2a 20 41 53 43 20 6f 72 20 44 45 53 43 2e 20  ** ASC or DESC. 
2bb0a 20 28 54 65 72 6d 73 20 6f 66 20 74 68 65 20 4f   (Terms of the O
2bb0b 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 70  RDER BY clause p
2bb0c 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61  ast the end of a
2bb0d 20 55 4e 49 51 55 45 0a 2a 2a 20 69 6e 64 65 78   UNIQUE.** index
2bb0e 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   do not need to 
2bb0f 73 61 74 69 73 66 79 20 74 68 69 73 20 63 6f 6e  satisfy this con
2bb10 73 74 72 61 69 6e 74 2e 29 20 20 54 68 65 20 2a  straint.)  The *
2bb11 70 62 52 65 76 20 76 61 6c 75 65 20 69 73 0a 2a  pbRev value is.*
2bb12 2a 20 73 65 74 20 74 6f 20 31 20 69 66 20 74 68  * set to 1 if th
2bb13 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2bb14 65 20 69 73 20 61 6c 6c 20 44 45 53 43 20 61 6e  e is all DESC an
2bb15 64 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 30  d it is set to 0
2bb16 20 69 66 0a 2a 2a 20 74 68 65 20 4f 52 44 45 52   if.** the ORDER
2bb17 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 6c   BY clause is al
2bb18 6c 20 41 53 43 2e 0a 2a 2f 0a 73 74 61 74 69 63  l ASC..*/.static
2bb19 20 69 6e 74 20 69 73 53 6f 72 74 69 6e 67 49 6e   int isSortingIn
2bb1a 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50  dex(.  Parse *pP
2bb1b 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
2bb1c 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
2bb1d 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b  t */.  WhereMask
2bb1e 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 2f  Set *pMaskSet, /
2bb1f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74  * Mapping from t
2bb20 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  able cursor numb
2bb21 65 72 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a  ers to bitmaps *
2bb22 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c  /.  Index *pIdx,
2bb23 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2bb24 68 65 20 69 6e 64 65 78 20 77 65 20 61 72 65 20  he index we are 
2bb25 74 65 73 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  testing */.  int
2bb26 20 62 61 73 65 2c 20 20 20 20 20 20 20 20 20 20   base,          
2bb27 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
2bb28 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61  umber for the ta
2bb29 62 6c 65 20 74 6f 20 62 65 20 73 6f 72 74 65 64  ble to be sorted
2bb2a 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2bb2b 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a  pOrderBy,     /*
2bb2c 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
2bb2d 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  ause */.  int nE
2bb2e 71 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  qCol,           
2bb2f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
2bb30 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20 77 69 74  ndex columns wit
2bb31 68 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73  h == constraints
2bb32 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52 65 76   */.  int *pbRev
2bb33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2bb34 20 53 65 74 20 74 6f 20 31 20 69 66 20 4f 52 44   Set to 1 if ORD
2bb35 45 52 20 42 59 20 69 73 20 44 45 53 43 20 2a 2f  ER BY is DESC */
2bb36 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20  .){.  int i, j; 
2bb37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb38 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2bb39 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  unters */.  int 
2bb3a 73 6f 72 74 4f 72 64 65 72 20 3d 20 30 3b 20 20  sortOrder = 0;  
2bb3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58              /* X
2bb3c 4f 52 20 6f 66 20 69 6e 64 65 78 20 61 6e 64 20  OR of index and 
2bb3d 4f 52 44 45 52 20 42 59 20 73 6f 72 74 20 64 69  ORDER BY sort di
2bb3e 72 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  rection */.  int
2bb3f 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20   nTerm;         
2bb40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bb41 4e 75 6d 62 65 72 20 6f 66 20 4f 52 44 45 52 20  Number of ORDER 
2bb42 42 59 20 74 65 72 6d 73 20 2a 2f 0a 20 20 73 74  BY terms */.  st
2bb43 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
2bb44 65 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 2f 2a  em *pTerm;    /*
2bb45 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f   A term of the O
2bb46 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
2bb47 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2bb48 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
2bb49 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42   assert( pOrderB
2bb4a 79 21 3d 30 20 29 3b 0a 20 20 6e 54 65 72 6d 20  y!=0 );.  nTerm 
2bb4b 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  = pOrderBy->nExp
2bb4c 72 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65  r;.  assert( nTe
2bb4d 72 6d 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 4d 61  rm>0 );..  /* Ma
2bb4e 74 63 68 20 74 65 72 6d 73 20 6f 66 20 74 68 65  tch terms of the
2bb4f 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2bb50 20 61 67 61 69 6e 73 74 20 63 6f 6c 75 6d 6e 73   against columns
2bb51 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64   of.  ** the ind
2bb52 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f  ex..  **.  ** No
2bb53 74 65 20 74 68 61 74 20 69 6e 64 69 63 65 73 20  te that indices 
2bb54 68 61 76 65 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  have pIdx->nColu
2bb55 6d 6e 20 72 65 67 75 6c 61 72 20 63 6f 6c 75 6d  mn regular colum
2bb56 6e 73 20 70 6c 75 73 0a 20 20 2a 2a 20 6f 6e 65  ns plus.  ** one
2bb57 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75   additional colu
2bb58 6d 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  mn containing th
2bb59 65 20 72 6f 77 69 64 2e 20 20 54 68 65 20 72 6f  e rowid.  The ro
2bb5a 77 69 64 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20  wid column.  ** 
2bb5b 6f 66 20 74 68 65 20 69 6e 64 65 78 20 69 73 20  of the index is 
2bb5c 61 6c 73 6f 20 61 6c 6c 6f 77 65 64 20 74 6f 20  also allowed to 
2bb5d 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20 74 68  match against th
2bb5e 65 20 4f 52 44 45 52 20 42 59 0a 20 20 2a 2a 20  e ORDER BY.  ** 
2bb5f 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 66  clause..  */.  f
2bb60 6f 72 28 69 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d  or(i=j=0, pTerm=
2bb61 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c 6e  pOrderBy->a; j<n
2bb62 54 65 72 6d 20 26 26 20 69 3c 3d 70 49 64 78 2d  Term && i<=pIdx-
2bb63 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  >nColumn; i++){.
2bb64 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b      Expr *pExpr;
2bb65 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
2bb66 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20  pression of the 
2bb67 4f 52 44 45 52 20 42 59 20 70 54 65 72 6d 20 2a  ORDER BY pTerm *
2bb68 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70  /.    CollSeq *p
2bb69 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 54 68 65 20  Coll;    /* The 
2bb6a 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2bb6b 63 65 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a 20  ce of pExpr */. 
2bb6c 20 20 20 69 6e 74 20 74 65 72 6d 53 6f 72 74 4f     int termSortO
2bb6d 72 64 65 72 3b 20 2f 2a 20 53 6f 72 74 20 6f 72  rder; /* Sort or
2bb6e 64 65 72 20 66 6f 72 20 74 68 69 73 20 74 65 72  der for this ter
2bb6f 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f  m */.    int iCo
2bb70 6c 75 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 54  lumn;       /* T
2bb71 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f  he i-th column o
2bb72 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 2d 31  f the index.  -1
2bb73 20 66 6f 72 20 72 6f 77 69 64 20 2a 2f 0a 20 20   for rowid */.  
2bb74 20 20 69 6e 74 20 69 53 6f 72 74 4f 72 64 65 72    int iSortOrder
2bb75 3b 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 44 45  ;    /* 1 for DE
2bb76 53 43 2c 20 30 20 66 6f 72 20 41 53 43 20 6f 6e  SC, 0 for ASC on
2bb77 20 74 68 65 20 69 2d 74 68 20 69 6e 64 65 78 20   the i-th index 
2bb78 74 65 72 6d 20 2a 2f 0a 20 20 20 20 63 6f 6e 73  term */.    cons
2bb79 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 2f  t char *zColl; /
2bb7a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f  * Name of the co
2bb7b 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2bb7c 20 66 6f 72 20 69 2d 74 68 20 69 6e 64 65 78 20   for i-th index 
2bb7d 74 65 72 6d 20 2a 2f 0a 0a 20 20 20 20 70 45 78  term */..    pEx
2bb7e 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pr = pTerm->pExp
2bb7f 72 3b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72  r;.    if( pExpr
2bb80 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op!=TK_COLUMN 
2bb81 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  || pExpr->iTable
2bb82 21 3d 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20  !=base ){.      
2bb83 2f 2a 20 43 61 6e 20 6e 6f 74 20 75 73 65 20 61  /* Can not use a
2bb84 6e 20 69 6e 64 65 78 20 73 6f 72 74 20 6f 6e 20  n index sort on 
2bb85 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 69 73  anything that is
2bb86 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 69 6e   not a column in
2bb87 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6c 65   the.      ** le
2bb88 66 74 2d 6d 6f 73 74 20 74 61 62 6c 65 20 6f 66  ft-most table of
2bb89 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2bb8a 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   */.      break;
2bb8b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 6f 6c 6c  .    }.    pColl
2bb8c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2bb8d 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
2bb8e 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20 21 70  xpr);.    if( !p
2bb8f 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43  Coll ){.      pC
2bb90 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43  oll = db->pDfltC
2bb91 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  oll;.    }.    i
2bb92 66 28 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  f( i<pIdx->nColu
2bb93 6d 6e 20 29 7b 0a 20 20 20 20 20 20 69 43 6f 6c  mn ){.      iCol
2bb94 75 6d 6e 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f  umn = pIdx->aiCo
2bb95 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69  lumn[i];.      i
2bb96 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d 70 49 64 78  f( iColumn==pIdx
2bb97 2d 3e 70 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20  ->pTable->iPKey 
2bb98 29 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 75  ){.        iColu
2bb99 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d  mn = -1;.      }
2bb9a 0a 20 20 20 20 20 20 69 53 6f 72 74 4f 72 64 65  .      iSortOrde
2bb9b 72 20 3d 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f  r = pIdx->aSortO
2bb9c 72 64 65 72 5b 69 5d 3b 0a 20 20 20 20 20 20 7a  rder[i];.      z
2bb9d 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43  Coll = pIdx->azC
2bb9e 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73  oll[i];.    }els
2bb9f 65 7b 0a 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e  e{.      iColumn
2bba0 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 69 53 6f   = -1;.      iSo
2bba1 72 74 4f 72 64 65 72 20 3d 20 30 3b 0a 20 20 20  rtOrder = 0;.   
2bba2 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c     zColl = pColl
2bba3 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 7d 0a 20  ->zName;.    }. 
2bba4 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43     if( pExpr->iC
2bba5 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 7c  olumn!=iColumn |
2bba6 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
2bba7 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a  (pColl->zName, z
2bba8 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f  Coll) ){.      /
2bba9 2a 20 54 65 72 6d 20 6a 20 6f 66 20 74 68 65 20  * Term j of the 
2bbaa 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2bbab 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 63  does not match c
2bbac 6f 6c 75 6d 6e 20 69 20 6f 66 20 74 68 65 20 69  olumn i of the i
2bbad 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20 20 69 66  ndex */.      if
2bbae 28 20 69 3c 6e 45 71 43 6f 6c 20 29 7b 0a 20 20  ( i<nEqCol ){.  
2bbaf 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 69        /* If an i
2bbb0 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 74 68 61 74  ndex column that
2bbb1 20 69 73 20 63 6f 6e 73 74 72 61 69 6e 65 64 20   is constrained 
2bbb2 62 79 20 3d 3d 20 66 61 69 6c 73 20 74 6f 20 6d  by == fails to m
2bbb3 61 74 63 68 20 61 6e 0a 20 20 20 20 20 20 20 20  atch an.        
2bbb4 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  ** ORDER BY term
2bbb5 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e 20 20 4a  , that is OK.  J
2bbb6 75 73 74 20 69 67 6e 6f 72 65 20 74 68 61 74 20  ust ignore that 
2bbb7 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e  column of the in
2bbb8 64 65 78 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  dex.        */. 
2bbb9 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
2bbba 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
2bbbb 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d   i==pIdx->nColum
2bbbc 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  n ){.        /* 
2bbbd 49 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 69 20 69  Index column i i
2bbbe 73 20 74 68 65 20 72 6f 77 69 64 2e 20 20 41 6c  s the rowid.  Al
2bbbf 6c 20 6f 74 68 65 72 20 74 65 72 6d 73 20 6d 61  l other terms ma
2bbc0 74 63 68 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tch. */.        
2bbc1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c  break;.      }el
2bbc2 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  se{.        /* I
2bbc3 66 20 61 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d  f an index colum
2bbc4 6e 20 66 61 69 6c 73 20 74 6f 20 6d 61 74 63 68  n fails to match
2bbc5 20 61 6e 64 20 69 73 20 6e 6f 74 20 63 6f 6e 73   and is not cons
2bbc6 74 72 61 69 6e 65 64 20 62 79 20 3d 3d 0a 20 20  trained by ==.  
2bbc7 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68        ** then th
2bbc8 65 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 73  e index cannot s
2bbc9 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52  atisfy the ORDER
2bbca 20 42 59 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a   BY constraint..
2bbcb 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2bbcc 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
2bbcd 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
2bbce 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61 53 6f  ssert( pIdx->aSo
2bbcf 72 74 4f 72 64 65 72 21 3d 30 20 29 3b 0a 20 20  rtOrder!=0 );.  
2bbd0 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d    assert( pTerm-
2bbd1 3e 73 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c  >sortOrder==0 ||
2bbd2 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65   pTerm->sortOrde
2bbd3 72 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65  r==1 );.    asse
2bbd4 72 74 28 20 69 53 6f 72 74 4f 72 64 65 72 3d 3d  rt( iSortOrder==
2bbd5 30 20 7c 7c 20 69 53 6f 72 74 4f 72 64 65 72 3d  0 || iSortOrder=
2bbd6 3d 31 20 29 3b 0a 20 20 20 20 74 65 72 6d 53 6f  =1 );.    termSo
2bbd7 72 74 4f 72 64 65 72 20 3d 20 69 53 6f 72 74 4f  rtOrder = iSortO
2bbd8 72 64 65 72 20 5e 20 70 54 65 72 6d 2d 3e 73 6f  rder ^ pTerm->so
2bbd9 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 69 66 28  rtOrder;.    if(
2bbda 20 69 3e 6e 45 71 43 6f 6c 20 29 7b 0a 20 20 20   i>nEqCol ){.   
2bbdb 20 20 20 69 66 28 20 74 65 72 6d 53 6f 72 74 4f     if( termSortO
2bbdc 72 64 65 72 21 3d 73 6f 72 74 4f 72 64 65 72 20  rder!=sortOrder 
2bbdd 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ){.        /* In
2bbde 64 69 63 65 73 20 63 61 6e 20 6f 6e 6c 79 20 62  dices can only b
2bbdf 65 20 75 73 65 64 20 69 66 20 61 6c 6c 20 4f 52  e used if all OR
2bbe0 44 45 52 20 42 59 20 74 65 72 6d 73 20 70 61 73  DER BY terms pas
2bbe1 74 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  t the.        **
2bbe2 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
2bbe3 61 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 65 69  aints are all ei
2bbe4 74 68 65 72 20 44 45 53 43 20 6f 72 20 41 53 43  ther DESC or ASC
2bbe5 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74  . */.        ret
2bbe6 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
2bbe7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2bbe8 73 6f 72 74 4f 72 64 65 72 20 3d 20 74 65 72 6d  sortOrder = term
2bbe9 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d  SortOrder;.    }
2bbea 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 70 54  .    j++;.    pT
2bbeb 65 72 6d 2b 2b 3b 0a 20 20 20 20 69 66 28 20 69  erm++;.    if( i
2bbec 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 72 65 66  Column<0 && !ref
2bbed 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c  erencesOtherTabl
2bbee 65 73 28 70 4f 72 64 65 72 42 79 2c 20 70 4d 61  es(pOrderBy, pMa
2bbef 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65 29 20  skSet, j, base) 
2bbf0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ){.      /* If t
2bbf1 68 65 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d  he indexed colum
2bbf2 6e 20 69 73 20 74 68 65 20 70 72 69 6d 61 72 79  n is the primary
2bbf3 20 6b 65 79 20 61 6e 64 20 65 76 65 72 79 74 68   key and everyth
2bbf4 69 6e 67 20 6d 61 74 63 68 65 73 0a 20 20 20 20  ing matches.    
2bbf5 20 20 2a 2a 20 73 6f 20 66 61 72 20 61 6e 64 20    ** so far and 
2bbf6 6e 6f 6e 65 20 6f 66 20 74 68 65 20 4f 52 44 45  none of the ORDE
2bbf7 52 20 42 59 20 74 65 72 6d 73 20 74 6f 20 74 68  R BY terms to th
2bbf8 65 20 72 69 67 68 74 20 72 65 66 65 72 65 6e 63  e right referenc
2bbf9 65 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a  e other.      **
2bbfa 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a   tables in the j
2bbfb 6f 69 6e 2c 20 74 68 65 6e 20 77 65 20 61 72 65  oin, then we are
2bbfc 20 61 73 73 75 72 65 64 20 74 68 61 74 20 74 68   assured that th
2bbfd 65 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75  e index can be u
2bbfe 73 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f  sed .      ** to
2bbff 20 73 6f 72 74 20 62 65 63 61 75 73 65 20 74 68   sort because th
2bc00 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73  e primary key is
2bc01 20 75 6e 69 71 75 65 20 61 6e 64 20 73 6f 20 6e   unique and so n
2bc02 6f 6e 65 20 6f 66 20 74 68 65 20 6f 74 68 65 72  one of the other
2bc03 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e  .      ** column
2bc04 73 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20  s will make any 
2bc05 64 69 66 66 65 72 65 6e 63 65 0a 20 20 20 20 20  difference.     
2bc06 20 2a 2f 0a 20 20 20 20 20 20 6a 20 3d 20 6e 54   */.      j = nT
2bc07 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  erm;.    }.  }..
2bc08 20 20 2a 70 62 52 65 76 20 3d 20 73 6f 72 74 4f    *pbRev = sortO
2bc09 72 64 65 72 21 3d 30 3b 0a 20 20 69 66 28 20 6a  rder!=0;.  if( j
2bc0a 3e 3d 6e 54 65 72 6d 20 29 7b 0a 20 20 20 20 2f  >=nTerm ){.    /
2bc0b 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74  * All terms of t
2bc0c 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2bc0d 73 65 20 61 72 65 20 63 6f 76 65 72 65 64 20 62  se are covered b
2bc0e 79 20 74 68 69 73 20 69 6e 64 65 78 20 73 6f 0a  y this index so.
2bc0f 20 20 20 20 2a 2a 20 74 68 69 73 20 69 6e 64 65      ** this inde
2bc10 78 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f  x can be used fo
2bc11 72 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a 20 20  r sorting. */.  
2bc12 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
2bc13 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72    if( pIdx->onEr
2bc14 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20  ror!=OE_None && 
2bc15 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  i==pIdx->nColumn
2bc16 0a 20 20 20 20 20 20 26 26 20 21 72 65 66 65 72  .      && !refer
2bc17 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73  encesOtherTables
2bc18 28 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73 6b  (pOrderBy, pMask
2bc19 53 65 74 2c 20 6a 2c 20 62 61 73 65 29 20 29 7b  Set, j, base) ){
2bc1a 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 74 65 72 6d  .    /* All term
2bc1b 73 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20  s of this index 
2bc1c 6d 61 74 63 68 20 73 6f 6d 65 20 70 72 65 66 69  match some prefi
2bc1d 78 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  x of the ORDER B
2bc1e 59 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2a 20  Y clause.    ** 
2bc1f 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 73  and the index is
2bc20 20 55 4e 49 51 55 45 20 61 6e 64 20 6e 6f 20 74   UNIQUE and no t
2bc21 65 72 6d 73 20 6f 6e 20 74 68 65 20 74 61 69 6c  erms on the tail
2bc22 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2bc23 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 72  .    ** clause r
2bc24 65 66 65 72 65 6e 63 65 20 6f 74 68 65 72 20 74  eference other t
2bc25 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 2e  ables in a join.
2bc26 20 20 49 66 20 74 68 69 73 20 69 73 20 61 6c 6c    If this is all
2bc27 20 74 72 75 65 20 74 68 65 6e 0a 20 20 20 20 2a   true then.    *
2bc28 2a 20 74 68 65 20 6f 72 64 65 72 20 62 79 20 63  * the order by c
2bc29 6c 61 75 73 65 20 69 73 20 73 75 70 65 72 66 6c  lause is superfl
2bc2a 75 6f 75 73 2e 20 2a 2f 0a 20 20 20 20 72 65 74  uous. */.    ret
2bc2b 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 1;.  }.  ret
2bc2c 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
2bc2d 43 68 65 63 6b 20 74 61 62 6c 65 20 74 6f 20 73  Check table to s
2bc2e 65 65 20 69 66 20 74 68 65 20 4f 52 44 45 52 20  ee if the ORDER 
2bc2f 42 59 20 63 6c 61 75 73 65 20 69 6e 20 70 4f 72  BY clause in pOr
2bc30 64 65 72 42 79 20 63 61 6e 20 62 65 20 73 61 74  derBy can be sat
2bc31 69 73 66 69 65 64 0a 2a 2a 20 62 79 20 73 6f 72  isfied.** by sor
2bc32 74 69 6e 67 20 69 6e 20 6f 72 64 65 72 20 6f 66  ting in order of
2bc33 20 52 4f 57 49 44 2e 20 20 52 65 74 75 72 6e 20   ROWID.  Return 
2bc34 74 72 75 65 20 69 66 20 73 6f 20 61 6e 64 20 73  true if so and s
2bc35 65 74 20 2a 70 62 52 65 76 20 74 6f 20 62 65 0a  et *pbRev to be.
2bc36 2a 2a 20 74 72 75 65 20 66 6f 72 20 72 65 76 65  ** true for reve
2bc37 72 73 65 20 52 4f 57 49 44 20 61 6e 64 20 66 61  rse ROWID and fa
2bc38 6c 73 65 20 66 6f 72 20 66 6f 72 77 61 72 64 20  lse for forward 
2bc39 52 4f 57 49 44 20 6f 72 64 65 72 2e 0a 2a 2f 0a  ROWID order..*/.
2bc3a 73 74 61 74 69 63 20 69 6e 74 20 73 6f 72 74 61  static int sorta
2bc3b 62 6c 65 42 79 52 6f 77 69 64 28 0a 20 20 69 6e  bleByRowid(.  in
2bc3c 74 20 62 61 73 65 2c 20 20 20 20 20 20 20 20 20  t base,         
2bc3d 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
2bc3e 6e 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65  number for table
2bc3f 20 74 6f 20 62 65 20 73 6f 72 74 65 64 20 2a 2f   to be sorted */
2bc40 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
2bc41 64 65 72 42 79 2c 20 20 20 20 20 2f 2a 20 54 68  derBy,     /* Th
2bc42 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2bc43 65 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b  e */.  WhereMask
2bc44 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 2f  Set *pMaskSet, /
2bc45 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74  * Mapping from t
2bc46 61 62 6c 65 20 63 75 72 73 6f 72 73 20 74 6f 20  able cursors to 
2bc47 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20 69 6e 74  bitmaps */.  int
2bc48 20 2a 70 62 52 65 76 20 20 20 20 20 20 20 20 20   *pbRev         
2bc49 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31       /* Set to 1
2bc4a 20 69 66 20 4f 52 44 45 52 20 42 59 20 69 73 20   if ORDER BY is 
2bc4b 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  DESC */.){.  Exp
2bc4c 72 20 2a 70 3b 0a 0a 20 20 61 73 73 65 72 74 28  r *p;..  assert(
2bc4d 20 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b 0a   pOrderBy!=0 );.
2bc4e 20 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72    assert( pOrder
2bc4f 42 79 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20  By->nExpr>0 );. 
2bc50 20 70 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61   p = pOrderBy->a
2bc51 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28  [0].pExpr;.  if(
2bc52 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d   p->op==TK_COLUM
2bc53 4e 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d  N && p->iTable==
2bc54 62 61 73 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75  base && p->iColu
2bc55 6d 6e 3d 3d 2d 31 0a 20 20 20 20 26 26 20 21 72  mn==-1.    && !r
2bc56 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61  eferencesOtherTa
2bc57 62 6c 65 73 28 70 4f 72 64 65 72 42 79 2c 20 70  bles(pOrderBy, p
2bc58 4d 61 73 6b 53 65 74 2c 20 31 2c 20 62 61 73 65  MaskSet, 1, base
2bc59 29 20 29 7b 0a 20 20 20 20 2a 70 62 52 65 76 20  ) ){.    *pbRev 
2bc5a 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d  = pOrderBy->a[0]
2bc5b 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  .sortOrder;.    
2bc5c 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
2bc5d 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
2bc5e 2a 2a 20 50 72 65 70 61 72 65 20 61 20 63 72 75  ** Prepare a cru
2bc5f 64 65 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74  de estimate of t
2bc60 68 65 20 6c 6f 67 61 72 69 74 68 6d 20 6f 66 20  he logarithm of 
2bc61 74 68 65 20 69 6e 70 75 74 20 76 61 6c 75 65 2e  the input value.
2bc62 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20  .** The results 
2bc63 6e 65 65 64 20 6e 6f 74 20 62 65 20 65 78 61 63  need not be exac
2bc64 74 2e 20 20 54 68 69 73 20 69 73 20 6f 6e 6c 79  t.  This is only
2bc65 20 75 73 65 64 20 66 6f 72 20 65 73 74 69 6d 61   used for estima
2bc66 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74 6f 74 61  ting.** the tota
2bc67 6c 20 63 6f 73 74 20 6f 66 20 70 65 72 66 6f 72  l cost of perfor
2bc68 6d 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 20  ming operations 
2bc69 77 69 74 68 20 4f 28 6c 6f 67 4e 29 20 6f 72 20  with O(logN) or 
2bc6a 4f 28 4e 6c 6f 67 4e 29 0a 2a 2a 20 63 6f 6d 70  O(NlogN).** comp
2bc6b 6c 65 78 69 74 79 2e 20 20 42 65 63 61 75 73 65  lexity.  Because
2bc6c 20 4e 20 69 73 20 6a 75 73 74 20 61 20 67 75 65   N is just a gue
2bc6d 73 73 2c 20 69 74 20 69 73 20 6e 6f 20 67 72 65  ss, it is no gre
2bc6e 61 74 20 74 72 61 67 65 64 79 20 69 66 0a 2a 2a  at tragedy if.**
2bc6f 20 6c 6f 67 4e 20 69 73 20 61 20 6c 69 74 74 6c   logN is a littl
2bc70 65 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63  e off..*/.static
2bc71 20 64 6f 75 62 6c 65 20 65 73 74 4c 6f 67 28 64   double estLog(d
2bc72 6f 75 62 6c 65 20 4e 29 7b 0a 20 20 64 6f 75 62  ouble N){.  doub
2bc73 6c 65 20 6c 6f 67 4e 20 3d 20 31 3b 0a 20 20 64  le logN = 1;.  d
2bc74 6f 75 62 6c 65 20 78 20 3d 20 31 30 3b 0a 20 20  ouble x = 10;.  
2bc75 77 68 69 6c 65 28 20 4e 3e 78 20 29 7b 0a 20 20  while( N>x ){.  
2bc76 20 20 6c 6f 67 4e 20 2b 3d 20 31 3b 0a 20 20 20    logN += 1;.   
2bc77 20 78 20 2a 3d 20 31 30 3b 0a 20 20 7d 0a 20 20   x *= 10;.  }.  
2bc78 72 65 74 75 72 6e 20 6c 6f 67 4e 3b 0a 7d 0a 0a  return logN;.}..
2bc79 2f 2a 0a 2a 2a 20 54 77 6f 20 72 6f 75 74 69 6e  /*.** Two routin
2bc7a 65 73 20 66 6f 72 20 70 72 69 6e 74 69 6e 67 20  es for printing 
2bc7b 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61  the content of a
2bc7c 6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  n sqlite3_index_
2bc7d 69 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72  info.** structur
2bc7e 65 2e 20 20 55 73 65 64 20 66 6f 72 20 74 65 73  e.  Used for tes
2bc7f 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69  ting and debuggi
2bc80 6e 67 20 6f 6e 6c 79 2e 20 20 49 66 20 6e 65 69  ng only.  If nei
2bc81 74 68 65 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 54  ther.** SQLITE_T
2bc82 45 53 54 20 6f 72 20 53 51 4c 49 54 45 5f 44 45  EST or SQLITE_DE
2bc83 42 55 47 20 61 72 65 20 64 65 66 69 6e 65 64 2c  BUG are defined,
2bc84 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74   then these rout
2bc85 69 6e 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 2d 6f  ines.** are no-o
2bc86 70 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  ps..*/.#if !defi
2bc87 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2bc88 56 49 52 54 55 41 4c 54 41 42 4c 45 29 20 26 26  VIRTUALTABLE) &&
2bc89 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
2bc8a 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 76 6f  DEBUG).static vo
2bc8b 69 64 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50  id TRACE_IDX_INP
2bc8c 55 54 53 28 73 71 6c 69 74 65 33 5f 69 6e 64 65  UTS(sqlite3_inde
2bc8d 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 6e  x_info *p){.  in
2bc8e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  t i;.  if( !sqli
2bc8f 74 65 33 57 68 65 72 65 54 72 61 63 65 20 29 20  te3WhereTrace ) 
2bc90 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
2bc91 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61  0; i<p->nConstra
2bc92 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  int; i++){.    s
2bc93 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2bc94 66 28 22 20 20 63 6f 6e 73 74 72 61 69 6e 74 5b  f("  constraint[
2bc95 25 64 5d 3a 20 63 6f 6c 3d 25 64 20 74 65 72 6d  %d]: col=%d term
2bc96 69 64 3d 25 64 20 6f 70 3d 25 64 20 75 73 61 62  id=%d op=%d usab
2bc97 6c 65 64 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20  led=%d\n",.     
2bc98 20 20 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61    i,.       p->a
2bc99 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69 43  Constraint[i].iC
2bc9a 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 70 2d  olumn,.       p-
2bc9b 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e  >aConstraint[i].
2bc9c 69 54 65 72 6d 4f 66 66 73 65 74 2c 0a 20 20 20  iTermOffset,.   
2bc9d 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69      p->aConstrai
2bc9e 6e 74 5b 69 5d 2e 6f 70 2c 0a 20 20 20 20 20 20  nt[i].op,.      
2bc9f 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b   p->aConstraint[
2bca0 69 5d 2e 75 73 61 62 6c 65 29 3b 0a 20 20 7d 0a  i].usable);.  }.
2bca1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2bca2 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a  nOrderBy; i++){.
2bca3 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2bca4 50 72 69 6e 74 66 28 22 20 20 6f 72 64 65 72 62  Printf("  orderb
2bca5 79 5b 25 64 5d 3a 20 63 6f 6c 3d 25 64 20 64 65  y[%d]: col=%d de
2bca6 73 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  sc=%d\n",.      
2bca7 20 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 4f   i,.       p->aO
2bca8 72 64 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d  rderBy[i].iColum
2bca9 6e 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 4f 72  n,.       p->aOr
2bcaa 64 65 72 42 79 5b 69 5d 2e 64 65 73 63 29 3b 0a  derBy[i].desc);.
2bcab 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
2bcac 64 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50  d TRACE_IDX_OUTP
2bcad 55 54 53 28 73 71 6c 69 74 65 33 5f 69 6e 64 65  UTS(sqlite3_inde
2bcae 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 6e  x_info *p){.  in
2bcaf 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  t i;.  if( !sqli
2bcb0 74 65 33 57 68 65 72 65 54 72 61 63 65 20 29 20  te3WhereTrace ) 
2bcb1 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
2bcb2 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61  0; i<p->nConstra
2bcb3 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  int; i++){.    s
2bcb4 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2bcb5 66 28 22 20 20 75 73 61 67 65 5b 25 64 5d 3a 20  f("  usage[%d]: 
2bcb6 61 72 67 76 49 64 78 3d 25 64 20 6f 6d 69 74 3d  argvIdx=%d omit=
2bcb7 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c  %d\n",.       i,
2bcb8 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73  .       p->aCons
2bcb9 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 61  traintUsage[i].a
2bcba 72 67 76 49 6e 64 65 78 2c 0a 20 20 20 20 20 20  rgvIndex,.      
2bcbb 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55   p->aConstraintU
2bcbc 73 61 67 65 5b 69 5d 2e 6f 6d 69 74 29 3b 0a 20  sage[i].omit);. 
2bcbd 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75   }.  sqlite3Debu
2bcbe 67 50 72 69 6e 74 66 28 22 20 20 69 64 78 4e 75  gPrintf("  idxNu
2bcbf 6d 3d 25 64 5c 6e 22 2c 20 70 2d 3e 69 64 78 4e  m=%d\n", p->idxN
2bcc0 75 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  um);.  sqlite3De
2bcc1 62 75 67 50 72 69 6e 74 66 28 22 20 20 69 64 78  bugPrintf("  idx
2bcc2 53 74 72 3d 25 73 5c 6e 22 2c 20 70 2d 3e 69 64  Str=%s\n", p->id
2bcc3 78 53 74 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  xStr);.  sqlite3
2bcc4 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 6f  DebugPrintf("  o
2bcc5 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3d 25  rderByConsumed=%
2bcc6 64 5c 6e 22 2c 20 70 2d 3e 6f 72 64 65 72 42 79  d\n", p->orderBy
2bcc7 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 73 71 6c  Consumed);.  sql
2bcc8 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2bcc9 22 20 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74  "  estimatedCost
2bcca 3d 25 67 5c 6e 22 2c 20 70 2d 3e 65 73 74 69 6d  =%g\n", p->estim
2bccb 61 74 65 64 43 6f 73 74 29 3b 0a 7d 0a 23 65 6c  atedCost);.}.#el
2bccc 73 65 0a 23 64 65 66 69 6e 65 20 54 52 41 43 45  se.#define TRACE
2bccd 5f 49 44 58 5f 49 4e 50 55 54 53 28 41 29 0a 23  _IDX_INPUTS(A).#
2bcce 64 65 66 69 6e 65 20 54 52 41 43 45 5f 49 44 58  define TRACE_IDX
2bccf 5f 4f 55 54 50 55 54 53 28 41 29 0a 23 65 6e 64  _OUTPUTS(A).#end
2bcd0 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 71 75 69  if../* .** Requi
2bcd1 72 65 64 20 62 65 63 61 75 73 65 20 62 65 73 74  red because best
2bcd2 49 6e 64 65 78 28 29 20 69 73 20 63 61 6c 6c 65  Index() is calle
2bcd3 64 20 62 79 20 62 65 73 74 4f 72 43 6c 61 75 73  d by bestOrClaus
2bcd4 65 49 6e 64 65 78 28 29 20 0a 2a 2f 0a 73 74 61  eIndex() .*/.sta
2bcd5 74 69 63 20 76 6f 69 64 20 62 65 73 74 49 6e 64  tic void bestInd
2bcd6 65 78 28 0a 20 20 20 20 50 61 72 73 65 2a 2c 20  ex(.    Parse*, 
2bcd7 57 68 65 72 65 43 6c 61 75 73 65 2a 2c 20 73 74  WhereClause*, st
2bcd8 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2bcd9 6d 2a 2c 20 42 69 74 6d 61 73 6b 2c 20 45 78 70  m*, Bitmask, Exp
2bcda 72 4c 69 73 74 2a 2c 20 57 68 65 72 65 43 6f 73  rList*, WhereCos
2bcdb 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  t*);../*.** This
2bcdc 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74   routine attempt
2bcdd 73 20 74 6f 20 66 69 6e 64 20 61 6e 20 73 63 61  s to find an sca
2bcde 6e 6e 69 6e 67 20 73 74 72 61 74 65 67 79 20 74  nning strategy t
2bcdf 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20  hat can be used 
2bce0 0a 2a 2a 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20  .** to optimize 
2bce1 61 6e 20 27 4f 52 27 20 65 78 70 72 65 73 73 69  an 'OR' expressi
2bce2 6f 6e 20 74 68 61 74 20 69 73 20 70 61 72 74 20  on that is part 
2bce3 6f 66 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  of a WHERE claus
2bce4 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61  e. .**.** The ta
2bce5 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ble associated w
2bce6 69 74 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  ith FROM clause 
2bce7 74 65 72 6d 20 70 53 72 63 20 6d 61 79 20 62 65  term pSrc may be
2bce8 20 65 69 74 68 65 72 20 61 0a 2a 2a 20 72 65 67   either a.** reg
2bce9 75 6c 61 72 20 42 2d 54 72 65 65 20 74 61 62 6c  ular B-Tree tabl
2bcea 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74  e or a virtual t
2bceb 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
2bcec 76 6f 69 64 20 62 65 73 74 4f 72 43 6c 61 75 73  void bestOrClaus
2bced 65 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  eIndex(.  Parse 
2bcee 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2bcef 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2bcf0 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2bcf1 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2bcf2 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  WC,           /*
2bcf3 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
2bcf4 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  e */.  struct Sr
2bcf5 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63  cList_item *pSrc
2bcf6 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63  ,  /* The FROM c
2bcf7 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65  lause term to se
2bcf8 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73  arch */.  Bitmas
2bcf9 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20  k notReady,     
2bcfa 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
2bcfb 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 61 72   cursors that ar
2bcfc 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20  e not available 
2bcfd 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2bcfe 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20  OrderBy,        
2bcff 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
2bd00 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
2bd01 72 65 43 6f 73 74 20 2a 70 43 6f 73 74 20 20 20  reCost *pCost   
2bd02 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65           /* Lowe
2bd03 73 74 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c  st cost query pl
2bd04 61 6e 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66  an */.){.#ifndef
2bd05 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f   SQLITE_OMIT_OR_
2bd06 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 63  OPTIMIZATION.  c
2bd07 6f 6e 73 74 20 69 6e 74 20 69 43 75 72 20 3d 20  onst int iCur = 
2bd08 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 3b 20 20  pSrc->iCursor;  
2bd09 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 6f   /* The cursor o
2bd0a 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  f the table to b
2bd0b 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20  e accessed */.  
2bd0c 63 6f 6e 73 74 20 42 69 74 6d 61 73 6b 20 6d 61  const Bitmask ma
2bd0d 73 6b 53 72 63 20 3d 20 67 65 74 4d 61 73 6b 28  skSrc = getMask(
2bd0e 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 69  pWC->pMaskSet, i
2bd0f 43 75 72 29 3b 20 20 2f 2a 20 42 69 74 6d 61 73  Cur);  /* Bitmas
2bd10 6b 20 66 6f 72 20 70 53 72 63 20 2a 2f 0a 20 20  k for pSrc */.  
2bd11 57 68 65 72 65 54 65 72 6d 20 2a 20 63 6f 6e 73  WhereTerm * cons
2bd12 74 20 70 57 43 45 6e 64 20 3d 20 26 70 57 43 2d  t pWCEnd = &pWC-
2bd13 3e 61 5b 70 57 43 2d 3e 6e 54 65 72 6d 5d 3b 20  >a[pWC->nTerm]; 
2bd14 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66         /* End of
2bd15 20 70 57 43 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 57   pWC->a[] */.  W
2bd16 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
2bd17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bd18 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74 65 72   /* A single ter
2bd19 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  m of the WHERE c
2bd1a 6c 61 75 73 65 20 2a 2f 0a 0a 20 20 2f 2a 20 53  lause */..  /* S
2bd1b 65 61 72 63 68 20 74 68 65 20 57 48 45 52 45 20  earch the WHERE 
2bd1c 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66 6f 72  clause terms for
2bd1d 20 61 20 75 73 61 62 6c 65 20 57 4f 5f 4f 52 20   a usable WO_OR 
2bd1e 74 65 72 6d 2e 20 2a 2f 0a 20 20 66 6f 72 28 70  term. */.  for(p
2bd1f 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 70 54 65  Term=pWC->a; pTe
2bd20 72 6d 3c 70 57 43 45 6e 64 3b 20 70 54 65 72 6d  rm<pWCEnd; pTerm
2bd21 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65  ++){.    if( pTe
2bd22 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
2bd23 4f 5f 4f 52 20 0a 20 20 20 20 20 26 26 20 28 28  O_OR .     && ((
2bd24 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c  pTerm->prereqAll
2bd25 20 26 20 7e 6d 61 73 6b 53 72 63 29 20 26 20 6e   & ~maskSrc) & n
2bd26 6f 74 52 65 61 64 79 29 3d 3d 30 0a 20 20 20 20  otReady)==0.    
2bd27 20 26 26 20 28 70 54 65 72 6d 2d 3e 75 2e 70 4f   && (pTerm->u.pO
2bd28 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65  rInfo->indexable
2bd29 20 26 20 6d 61 73 6b 53 72 63 29 21 3d 30 20 0a   & maskSrc)!=0 .
2bd2a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 57 68 65      ){.      Whe
2bd2b 72 65 43 6c 61 75 73 65 20 2a 20 63 6f 6e 73 74  reClause * const
2bd2c 20 70 4f 72 57 43 20 3d 20 26 70 54 65 72 6d 2d   pOrWC = &pTerm-
2bd2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a  >u.pOrInfo->wc;.
2bd2e 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
2bd2f 2a 20 63 6f 6e 73 74 20 70 4f 72 57 43 45 6e 64  * const pOrWCEnd
2bd30 20 3d 20 26 70 4f 72 57 43 2d 3e 61 5b 70 4f 72   = &pOrWC->a[pOr
2bd31 57 43 2d 3e 6e 54 65 72 6d 5d 3b 0a 20 20 20 20  WC->nTerm];.    
2bd32 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72    WhereTerm *pOr
2bd33 54 65 72 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20  Term;.      int 
2bd34 66 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 4d 55  flags = WHERE_MU
2bd35 4c 54 49 5f 4f 52 3b 0a 20 20 20 20 20 20 64 6f  LTI_OR;.      do
2bd36 75 62 6c 65 20 72 54 6f 74 61 6c 20 3d 20 30 3b  uble rTotal = 0;
2bd37 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 6e 52  .      double nR
2bd38 6f 77 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 66  ow = 0;..      f
2bd39 6f 72 28 70 4f 72 54 65 72 6d 3d 70 4f 72 57 43  or(pOrTerm=pOrWC
2bd3a 2d 3e 61 3b 20 70 4f 72 54 65 72 6d 3c 70 4f 72  ->a; pOrTerm<pOr
2bd3b 57 43 45 6e 64 3b 20 70 4f 72 54 65 72 6d 2b 2b  WCEnd; pOrTerm++
2bd3c 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  ){.        Where
2bd3d 43 6f 73 74 20 73 54 65 72 6d 43 6f 73 74 3b 0a  Cost sTermCost;.
2bd3e 20 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41          WHERETRA
2bd3f 43 45 28 28 22 2e 2e 2e 20 4d 75 6c 74 69 2d 69  CE(("... Multi-i
2bd40 6e 64 65 78 20 4f 52 20 74 65 73 74 69 6e 67 20  ndex OR testing 
2bd41 66 6f 72 20 74 65 72 6d 20 25 64 20 6f 66 20 25  for term %d of %
2bd42 64 2e 2e 2e 2e 5c 6e 22 2c 20 0a 20 20 20 20 20  d....\n", .     
2bd43 20 20 20 20 20 28 70 4f 72 54 65 72 6d 20 2d 20       (pOrTerm - 
2bd44 70 4f 72 57 43 2d 3e 61 29 2c 20 28 70 54 65 72  pOrWC->a), (pTer
2bd45 6d 20 2d 20 70 57 43 2d 3e 61 29 0a 20 20 20 20  m - pWC->a).    
2bd46 20 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20 20      ));.        
2bd47 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70  if( pOrTerm->eOp
2bd48 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29  erator==WO_AND )
2bd49 7b 0a 20 20 20 20 20 20 20 20 20 20 57 68 65 72  {.          Wher
2bd4a 65 43 6c 61 75 73 65 20 2a 70 41 6e 64 57 43 20  eClause *pAndWC 
2bd4b 3d 20 26 70 4f 72 54 65 72 6d 2d 3e 75 2e 70 41  = &pOrTerm->u.pA
2bd4c 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20  ndInfo->wc;.    
2bd4d 20 20 20 20 20 20 62 65 73 74 49 6e 64 65 78 28        bestIndex(
2bd4e 70 50 61 72 73 65 2c 20 70 41 6e 64 57 43 2c 20  pParse, pAndWC, 
2bd4f 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20  pSrc, notReady, 
2bd50 30 2c 20 26 73 54 65 72 6d 43 6f 73 74 29 3b 0a  0, &sTermCost);.
2bd51 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
2bd52 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  ( pOrTerm->leftC
2bd53 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 7b 0a 20  ursor==iCur ){. 
2bd54 20 20 20 20 20 20 20 20 20 57 68 65 72 65 43 6c           WhereCl
2bd55 61 75 73 65 20 74 65 6d 70 57 43 3b 0a 20 20 20  ause tempWC;.   
2bd56 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 70 50         tempWC.pP
2bd57 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72  arse = pWC->pPar
2bd58 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65  se;.          te
2bd59 6d 70 57 43 2e 70 4d 61 73 6b 53 65 74 20 3d 20  mpWC.pMaskSet = 
2bd5a 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a 20  pWC->pMaskSet;. 
2bd5b 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e           tempWC.
2bd5c 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20  op = TK_AND;.   
2bd5d 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 61 20         tempWC.a 
2bd5e 3d 20 70 4f 72 54 65 72 6d 3b 0a 20 20 20 20 20  = pOrTerm;.     
2bd5f 20 20 20 20 20 74 65 6d 70 57 43 2e 6e 54 65 72       tempWC.nTer
2bd60 6d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  m = 1;.         
2bd61 20 62 65 73 74 49 6e 64 65 78 28 70 50 61 72 73   bestIndex(pPars
2bd62 65 2c 20 26 74 65 6d 70 57 43 2c 20 70 53 72 63  e, &tempWC, pSrc
2bd63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 30 2c 20 26  , notReady, 0, &
2bd64 73 54 65 72 6d 43 6f 73 74 29 3b 0a 20 20 20 20  sTermCost);.    
2bd65 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2bd66 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2bd67 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2bd68 20 72 54 6f 74 61 6c 20 2b 3d 20 73 54 65 72 6d   rTotal += sTerm
2bd69 43 6f 73 74 2e 72 43 6f 73 74 3b 0a 20 20 20 20  Cost.rCost;.    
2bd6a 20 20 20 20 6e 52 6f 77 20 2b 3d 20 73 54 65 72      nRow += sTer
2bd6b 6d 43 6f 73 74 2e 6e 52 6f 77 3b 0a 20 20 20 20  mCost.nRow;.    
2bd6c 20 20 20 20 69 66 28 20 72 54 6f 74 61 6c 3e 3d      if( rTotal>=
2bd6d 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29 20 62  pCost->rCost ) b
2bd6e 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  reak;.      }.. 
2bd6f 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
2bd70 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20   is an ORDER BY 
2bd71 63 6c 61 75 73 65 2c 20 69 6e 63 72 65 61 73 65  clause, increase
2bd72 20 74 68 65 20 73 63 61 6e 20 63 6f 73 74 20 74   the scan cost t
2bd73 6f 20 61 63 63 6f 75 6e 74 20 0a 20 20 20 20 20  o account .     
2bd74 20 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 73 74   ** for the cost
2bd75 20 6f 66 20 74 68 65 20 73 6f 72 74 2e 20 2a 2f   of the sort. */
2bd76 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
2bd77 72 42 79 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  rBy!=0 ){.      
2bd78 20 20 72 54 6f 74 61 6c 20 2b 3d 20 6e 52 6f 77    rTotal += nRow
2bd79 2a 65 73 74 4c 6f 67 28 6e 52 6f 77 29 3b 0a 20  *estLog(nRow);. 
2bd7a 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43         WHERETRAC
2bd7b 45 28 28 22 2e 2e 2e 20 73 6f 72 74 69 6e 67 20  E(("... sorting 
2bd7c 69 6e 63 72 65 61 73 65 73 20 4f 52 20 63 6f 73  increases OR cos
2bd7d 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 72 54  t to %.9g\n", rT
2bd7e 6f 74 61 6c 29 29 3b 0a 20 20 20 20 20 20 7d 0a  otal));.      }.
2bd7f 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
2bd80 20 63 6f 73 74 20 6f 66 20 73 63 61 6e 6e 69 6e   cost of scannin
2bd81 67 20 75 73 69 6e 67 20 74 68 69 73 20 4f 52 20  g using this OR 
2bd82 74 65 72 6d 20 66 6f 72 20 6f 70 74 69 6d 69 7a  term for optimiz
2bd83 61 74 69 6f 6e 20 69 73 0a 20 20 20 20 20 20 2a  ation is.      *
2bd84 2a 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  * less than the 
2bd85 63 75 72 72 65 6e 74 20 63 6f 73 74 20 73 74 6f  current cost sto
2bd86 72 65 64 20 69 6e 20 70 43 6f 73 74 2c 20 72 65  red in pCost, re
2bd87 70 6c 61 63 65 20 74 68 65 20 63 6f 6e 74 65 6e  place the conten
2bd88 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 70  ts.      ** of p
2bd89 43 6f 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 57  Cost. */.      W
2bd8a 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
2bd8b 6d 75 6c 74 69 2d 69 6e 64 65 78 20 4f 52 20 63  multi-index OR c
2bd8c 6f 73 74 3d 25 2e 39 67 20 6e 72 6f 77 3d 25 2e  ost=%.9g nrow=%.
2bd8d 39 67 5c 6e 22 2c 20 72 54 6f 74 61 6c 2c 20 6e  9g\n", rTotal, n
2bd8e 52 6f 77 29 29 3b 0a 20 20 20 20 20 20 69 66 28  Row));.      if(
2bd8f 20 72 54 6f 74 61 6c 3c 70 43 6f 73 74 2d 3e 72   rTotal<pCost->r
2bd90 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Cost ){.        
2bd91 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 72  pCost->rCost = r
2bd92 54 6f 74 61 6c 3b 0a 20 20 20 20 20 20 20 20 70  Total;.        p
2bd93 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f  Cost->nRow = nRo
2bd94 77 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74  w;.        pCost
2bd95 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d  ->plan.wsFlags =
2bd96 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20   flags;.        
2bd97 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 54  pCost->plan.u.pT
2bd98 65 72 6d 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20  erm = pTerm;.   
2bd99 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
2bd9a 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2bd9b 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2bd9c 54 49 4f 4e 20 2a 2f 0a 7d 0a 0a 23 69 66 6e 64  TION */.}..#ifnd
2bd9d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2bd9e 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a  IRTUALTABLE./*.*
2bd9f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 70  * Allocate and p
2bda0 6f 70 75 6c 61 74 65 20 61 6e 20 73 71 6c 69 74  opulate an sqlit
2bda1 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
2bda2 72 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 74  ructure. It is t
2bda3 68 65 20 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62  he .** responsib
2bda4 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c  ility of the cal
2bda5 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c  ler to eventuall
2bda6 79 20 72 65 6c 65 61 73 65 20 74 68 65 20 73 74  y release the st
2bda7 72 75 63 74 75 72 65 0a 2a 2a 20 62 79 20 70 61  ructure.** by pa
2bda8 73 73 69 6e 67 20 74 68 65 20 70 6f 69 6e 74 65  ssing the pointe
2bda9 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  r returned by th
2bdaa 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 73  is function to s
2bdab 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a  qlite3_free()..*
2bdac 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
2bdad 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 61 6c 6c  _index_info *all
2bdae 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28 0a  ocateIndexInfo(.
2bdaf 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2bdb0 20 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20   .  WhereClause 
2bdb1 2a 70 57 43 2c 0a 20 20 73 74 72 75 63 74 20 53  *pWC,.  struct S
2bdb2 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72  rcList_item *pSr
2bdb3 63 2c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  c,.  ExprList *p
2bdb4 4f 72 64 65 72 42 79 0a 29 7b 0a 20 20 69 6e 74  OrderBy.){.  int
2bdb5 20 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 54 65   i, j;.  int nTe
2bdb6 72 6d 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c  rm;.  struct sql
2bdb7 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
2bdb8 72 61 69 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b  raint *pIdxCons;
2bdb9 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
2bdba 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20  3_index_orderby 
2bdbb 2a 70 49 64 78 4f 72 64 65 72 42 79 3b 0a 20 20  *pIdxOrderBy;.  
2bdbc 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2bdbd 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f  ndex_constraint_
2bdbe 75 73 61 67 65 20 2a 70 55 73 61 67 65 3b 0a 20  usage *pUsage;. 
2bdbf 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2bdc0 6d 3b 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42  m;.  int nOrderB
2bdc1 79 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64  y;.  sqlite3_ind
2bdc2 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66  ex_info *pIdxInf
2bdc3 6f 3b 0a 0a 20 20 57 48 45 52 45 54 52 41 43 45  o;..  WHERETRACE
2bdc4 28 28 22 52 65 63 6f 6d 70 75 74 69 6e 67 20 69  (("Recomputing i
2bdc5 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 25 73  ndex info for %s
2bdc6 2e 2e 2e 5c 6e 22 2c 20 70 53 72 63 2d 3e 70 54  ...\n", pSrc->pT
2bdc7 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20  ab->zName));..  
2bdc8 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d  /* Count the num
2bdc9 62 65 72 20 6f 66 20 70 6f 73 73 69 62 6c 65 20  ber of possible 
2bdca 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e  WHERE clause con
2bdcb 73 74 72 61 69 6e 74 73 20 72 65 66 65 72 72 69  straints referri
2bdcc 6e 67 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20  ng.  ** to this 
2bdcd 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
2bdce 0a 20 20 66 6f 72 28 69 3d 6e 54 65 72 6d 3d 30  .  for(i=nTerm=0
2bdcf 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20  , pTerm=pWC->a; 
2bdd0 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b  i<pWC->nTerm; i+
2bdd1 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  +, pTerm++){.   
2bdd2 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74   if( pTerm->left
2bdd3 43 75 72 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e  Cursor != pSrc->
2bdd4 69 43 75 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e  iCursor ) contin
2bdd5 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ue;.    assert( 
2bdd6 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  (pTerm->eOperato
2bdd7 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  r&(pTerm->eOpera
2bdd8 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  tor-1))==0 );.  
2bdd9 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
2bdda 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
2bddb 5f 49 4e 20 29 3b 0a 20 20 20 20 74 65 73 74 63  _IN );.    testc
2bddc 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
2bddd 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c  rator==WO_ISNULL
2bdde 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72   );.    if( pTer
2bddf 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  m->eOperator & (
2bde0 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29  WO_IN|WO_ISNULL)
2bde1 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2bde2 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20 7d 0a 0a 20   nTerm++;.  }.. 
2bde3 20 2f 2a 20 49 66 20 74 68 65 20 4f 52 44 45 52   /* If the ORDER
2bde4 20 42 59 20 63 6c 61 75 73 65 20 63 6f 6e 74 61   BY clause conta
2bde5 69 6e 73 20 6f 6e 6c 79 20 63 6f 6c 75 6d 6e 73  ins only columns
2bde6 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
2bde7 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 74 61  .  ** virtual ta
2bde8 62 6c 65 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74  ble then allocat
2bde9 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  e space for the 
2bdea 61 4f 72 64 65 72 42 79 20 70 61 72 74 20 6f 66  aOrderBy part of
2bdeb 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65  .  ** the sqlite
2bdec 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
2bded 75 63 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 6e  ucture..  */.  n
2bdee 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 69  OrderBy = 0;.  i
2bdef 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
2bdf0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f     for(i=0; i<pO
2bdf1 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69  rderBy->nExpr; i
2bdf2 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  ++){.      Expr 
2bdf3 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42  *pExpr = pOrderB
2bdf4 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  y->a[i].pExpr;. 
2bdf5 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
2bdf6 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op!=TK_COLUMN ||
2bdf7 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 21 3d   pExpr->iTable!=
2bdf8 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20  pSrc->iCursor ) 
2bdf9 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
2bdfa 20 69 66 28 20 69 3d 3d 70 4f 72 64 65 72 42 79   if( i==pOrderBy
2bdfb 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20  ->nExpr ){.     
2bdfc 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64   nOrderBy = pOrd
2bdfd 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  erBy->nExpr;.   
2bdfe 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c   }.  }..  /* All
2bdff 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  ocate the sqlite
2be00 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
2be01 75 63 74 75 72 65 0a 20 20 2a 2f 0a 20 20 70 49  ucture.  */.  pI
2be02 64 78 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  dxInfo = sqlite3
2be03 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61  DbMallocZero(pPa
2be04 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28  rse->db, sizeof(
2be05 2a 70 49 64 78 49 6e 66 6f 29 0a 20 20 20 20 20  *pIdxInfo).     
2be06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be07 20 20 20 20 20 20 2b 20 28 73 69 7a 65 6f 66 28        + (sizeof(
2be08 2a 70 49 64 78 43 6f 6e 73 29 20 2b 20 73 69 7a  *pIdxCons) + siz
2be09 65 6f 66 28 2a 70 55 73 61 67 65 29 29 2a 6e 54  eof(*pUsage))*nT
2be0a 65 72 6d 0a 20 20 20 20 20 20 20 20 20 20 20 20  erm.            
2be0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b                 +
2be0c 20 73 69 7a 65 6f 66 28 2a 70 49 64 78 4f 72 64   sizeof(*pIdxOrd
2be0d 65 72 42 79 29 2a 6e 4f 72 64 65 72 42 79 20 29  erBy)*nOrderBy )
2be0e 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f  ;.  if( pIdxInfo
2be0f 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
2be10 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2be11 65 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  e, "out of memor
2be12 79 22 29 3b 0a 20 20 20 20 2f 2a 20 28 64 6f 75  y");.    /* (dou
2be13 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66  ble)0 In case of
2be14 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
2be15 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a  ATING_POINT... *
2be16 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  /.    return 0;.
2be17 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61    }..  /* Initia
2be18 6c 69 7a 65 20 74 68 65 20 73 74 72 75 63 74 75  lize the structu
2be19 72 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  re.  The sqlite3
2be1a 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2be1b 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 20  cture contains. 
2be1c 20 2a 2a 20 6d 61 6e 79 20 66 69 65 6c 64 73 20   ** many fields 
2be1d 74 68 61 74 20 61 72 65 20 64 65 63 6c 61 72 65  that are declare
2be1e 64 20 22 63 6f 6e 73 74 22 20 74 6f 20 70 72 65  d "const" to pre
2be1f 76 65 6e 74 20 78 42 65 73 74 49 6e 64 65 78 20  vent xBestIndex 
2be20 66 72 6f 6d 0a 20 20 2a 2a 20 63 68 61 6e 67 69  from.  ** changi
2be21 6e 67 20 74 68 65 6d 2e 20 20 57 65 20 68 61 76  ng them.  We hav
2be22 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20 66 75 6e  e to do some fun
2be23 6b 79 20 63 61 73 74 69 6e 67 20 69 6e 20 6f 72  ky casting in or
2be24 64 65 72 20 74 6f 0a 20 20 2a 2a 20 69 6e 69 74  der to.  ** init
2be25 69 61 6c 69 7a 65 20 74 68 6f 73 65 20 66 69 65  ialize those fie
2be26 6c 64 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78  lds..  */.  pIdx
2be27 43 6f 6e 73 20 3d 20 28 73 74 72 75 63 74 20 73  Cons = (struct s
2be28 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2be29 73 74 72 61 69 6e 74 2a 29 26 70 49 64 78 49 6e  straint*)&pIdxIn
2be2a 66 6f 5b 31 5d 3b 0a 20 20 70 49 64 78 4f 72 64  fo[1];.  pIdxOrd
2be2b 65 72 42 79 20 3d 20 28 73 74 72 75 63 74 20 73  erBy = (struct s
2be2c 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64  qlite3_index_ord
2be2d 65 72 62 79 2a 29 26 70 49 64 78 43 6f 6e 73 5b  erby*)&pIdxCons[
2be2e 6e 54 65 72 6d 5d 3b 0a 20 20 70 55 73 61 67 65  nTerm];.  pUsage
2be2f 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74   = (struct sqlit
2be30 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2be31 69 6e 74 5f 75 73 61 67 65 2a 29 26 70 49 64 78  int_usage*)&pIdx
2be32 4f 72 64 65 72 42 79 5b 6e 4f 72 64 65 72 42 79  OrderBy[nOrderBy
2be33 5d 3b 0a 20 20 2a 28 69 6e 74 2a 29 26 70 49 64  ];.  *(int*)&pId
2be34 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
2be35 6e 74 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 2a 28  nt = nTerm;.  *(
2be36 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  int*)&pIdxInfo->
2be37 6e 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72 64 65  nOrderBy = nOrde
2be38 72 42 79 3b 0a 20 20 2a 28 73 74 72 75 63 74 20  rBy;.  *(struct 
2be39 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
2be3a 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78  nstraint**)&pIdx
2be3b 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
2be3c 74 20 3d 20 70 49 64 78 43 6f 6e 73 3b 0a 20 20  t = pIdxCons;.  
2be3d 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  *(struct sqlite3
2be3e 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 2a 2a  _index_orderby**
2be3f 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 4f 72 64  )&pIdxInfo->aOrd
2be40 65 72 42 79 20 3d 20 70 49 64 78 4f 72 64 65 72  erBy = pIdxOrder
2be41 42 79 3b 0a 20 20 2a 28 73 74 72 75 63 74 20 73  By;.  *(struct s
2be42 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2be43 73 74 72 61 69 6e 74 5f 75 73 61 67 65 2a 2a 29  straint_usage**)
2be44 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73  &pIdxInfo->aCons
2be45 74 72 61 69 6e 74 55 73 61 67 65 20 3d 0a 20 20  traintUsage =.  
2be46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be4a 20 70 55 73 61 67 65 3b 0a 0a 20 20 66 6f 72 28   pUsage;..  for(
2be4b 69 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d 70 57 43  i=j=0, pTerm=pWC
2be4c 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 72  ->a; i<pWC->nTer
2be4d 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29  m; i++, pTerm++)
2be4e 7b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d  {.    if( pTerm-
2be4f 3e 6c 65 66 74 43 75 72 73 6f 72 20 21 3d 20 70  >leftCursor != p
2be50 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20 63  Src->iCursor ) c
2be51 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 73  ontinue;.    ass
2be52 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70  ert( (pTerm->eOp
2be53 65 72 61 74 6f 72 26 28 70 54 65 72 6d 2d 3e 65  erator&(pTerm->e
2be54 4f 70 65 72 61 74 6f 72 2d 31 29 29 3d 3d 30 20  Operator-1))==0 
2be55 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2be56 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2be57 72 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20  r==WO_IN );.    
2be58 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
2be59 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49  >eOperator==WO_I
2be5a 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 69 66 28  SNULL );.    if(
2be5b 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2be5c 72 20 26 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53  r & (WO_IN|WO_IS
2be5d 4e 55 4c 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65  NULL) ) continue
2be5e 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a  ;.    pIdxCons[j
2be5f 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 54 65 72  ].iColumn = pTer
2be60 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b  m->u.leftColumn;
2be61 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d  .    pIdxCons[j]
2be62 2e 69 54 65 72 6d 4f 66 66 73 65 74 20 3d 20 69  .iTermOffset = i
2be63 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a  ;.    pIdxCons[j
2be64 5d 2e 6f 70 20 3d 20 28 75 38 29 70 54 65 72 6d  ].op = (u8)pTerm
2be65 2d 3e 65 4f 70 65 72 61 74 6f 72 3b 0a 20 20 20  ->eOperator;.   
2be66 20 2f 2a 20 54 68 65 20 64 69 72 65 63 74 20 61   /* The direct a
2be67 73 73 69 67 6e 6d 65 6e 74 20 69 6e 20 74 68 65  ssignment in the
2be68 20 70 72 65 76 69 6f 75 73 20 6c 69 6e 65 20 69   previous line i
2be69 73 20 70 6f 73 73 69 62 6c 65 20 6f 6e 6c 79 20  s possible only 
2be6a 62 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 74  because.    ** t
2be6b 68 65 20 57 4f 5f 20 61 6e 64 20 53 51 4c 49 54  he WO_ and SQLIT
2be6c 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
2be6d 4e 54 5f 20 63 6f 64 65 73 20 61 72 65 20 69 64  NT_ codes are id
2be6e 65 6e 74 69 63 61 6c 2e 20 20 54 68 65 0a 20 20  entical.  The.  
2be6f 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61    ** following a
2be70 73 73 65 72 74 73 20 76 65 72 69 66 79 20 74 68  sserts verify th
2be71 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 20 20  is fact. */.    
2be72 61 73 73 65 72 74 28 20 57 4f 5f 45 51 3d 3d 53  assert( WO_EQ==S
2be73 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
2be74 54 52 41 49 4e 54 5f 45 51 20 29 3b 0a 20 20 20  TRAINT_EQ );.   
2be75 20 61 73 73 65 72 74 28 20 57 4f 5f 4c 54 3d 3d   assert( WO_LT==
2be76 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
2be77 53 54 52 41 49 4e 54 5f 4c 54 20 29 3b 0a 20 20  STRAINT_LT );.  
2be78 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4c 45 3d    assert( WO_LE=
2be79 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f  =SQLITE_INDEX_CO
2be7a 4e 53 54 52 41 49 4e 54 5f 4c 45 20 29 3b 0a 20  NSTRAINT_LE );. 
2be7b 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 47 54     assert( WO_GT
2be7c 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
2be7d 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 29 3b 0a  ONSTRAINT_GT );.
2be7e 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 47      assert( WO_G
2be7f 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  E==SQLITE_INDEX_
2be80 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 29 3b  CONSTRAINT_GE );
2be81 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f  .    assert( WO_
2be82 4d 41 54 43 48 3d 3d 53 51 4c 49 54 45 5f 49 4e  MATCH==SQLITE_IN
2be83 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d  DEX_CONSTRAINT_M
2be84 41 54 43 48 20 29 3b 0a 20 20 20 20 61 73 73 65  ATCH );.    asse
2be85 72 74 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  rt( pTerm->eOper
2be86 61 74 6f 72 20 26 20 28 57 4f 5f 45 51 7c 57 4f  ator & (WO_EQ|WO
2be87 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c  _LT|WO_LE|WO_GT|
2be88 57 4f 5f 47 45 7c 57 4f 5f 4d 41 54 43 48 29 20  WO_GE|WO_MATCH) 
2be89 29 3b 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a  );.    j++;.  }.
2be8a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72    for(i=0; i<nOr
2be8b 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20  derBy; i++){.   
2be8c 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70   Expr *pExpr = p
2be8d 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45  OrderBy->a[i].pE
2be8e 78 70 72 3b 0a 20 20 20 20 70 49 64 78 4f 72 64  xpr;.    pIdxOrd
2be8f 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 20  erBy[i].iColumn 
2be90 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
2be91 3b 0a 20 20 20 20 70 49 64 78 4f 72 64 65 72 42  ;.    pIdxOrderB
2be92 79 5b 69 5d 2e 64 65 73 63 20 3d 20 70 4f 72 64  y[i].desc = pOrd
2be93 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f 72 74 4f  erBy->a[i].sortO
2be94 72 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  rder;.  }..  ret
2be95 75 72 6e 20 70 49 64 78 49 6e 66 6f 3b 0a 7d 0a  urn pIdxInfo;.}.
2be96 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65  ./*.** The table
2be97 20 6f 62 6a 65 63 74 20 72 65 66 65 72 65 6e 63   object referenc
2be98 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
2be99 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
2be9a 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
2be9b 0a 2a 2a 20 6d 75 73 74 20 72 65 70 72 65 73 65  .** must represe
2be9c 6e 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  nt a virtual tab
2be9d 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  le. This functio
2be9e 6e 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 42  n invokes the xB
2be9f 65 73 74 49 6e 64 65 78 28 29 0a 2a 2a 20 6d 65  estIndex().** me
2bea0 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74  thod of the virt
2bea1 75 61 6c 20 74 61 62 6c 65 20 77 69 74 68 20 74  ual table with t
2bea2 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2bea3 5f 69 6e 66 6f 20 70 6f 69 6e 74 65 72 20 70 61  _info pointer pa
2bea4 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 61  ssed.** as the a
2bea5 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49  rgument..**.** I
2bea6 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
2bea7 73 2c 20 70 50 61 72 73 65 20 69 73 20 70 6f 70  s, pParse is pop
2bea8 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e 20 65  ulated with an e
2bea9 72 72 6f 72 20 6d 65 73 73 61 67 65 20 61 6e 64  rror message and
2beaa 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76   a.** non-zero v
2beab 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  alue is returned
2beac 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 20 69  . Otherwise, 0 i
2bead 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
2beae 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72  he output.** par
2beaf 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  t of the sqlite3
2beb0 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2beb1 63 74 75 72 65 20 69 73 20 6c 65 66 74 20 70 6f  cture is left po
2beb2 70 75 6c 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 57  pulated..**.** W
2beb3 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e  hether or not an
2beb4 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
2beb5 65 64 2c 20 69 74 20 69 73 20 74 68 65 20 72 65  ed, it is the re
2beb6 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
2beb7 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f  the.** caller to
2beb8 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65   eventually free
2beb9 20 70 2d 3e 69 64 78 53 74 72 20 69 66 20 70 2d   p->idxStr if p-
2beba 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
2bebb 72 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 74  r indicates.** t
2bebc 68 61 74 20 74 68 69 73 20 69 73 20 72 65 71 75  hat this is requ
2bebd 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ired..*/.static 
2bebe 69 6e 74 20 76 74 61 62 42 65 73 74 49 6e 64 65  int vtabBestInde
2bebf 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  x(Parse *pParse,
2bec0 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 73 71   Table *pTab, sq
2bec1 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2bec2 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f   *p){.  sqlite3_
2bec3 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 54  vtab *pVtab = pT
2bec4 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 69 6e 74  ab->pVtab;.  int
2bec5 20 69 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20   i;.  int rc;.. 
2bec6 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2bec7 66 65 74 79 4f 66 66 28 70 50 61 72 73 65 2d 3e  fetyOff(pParse->
2bec8 64 62 29 3b 0a 20 20 57 48 45 52 45 54 52 41 43  db);.  WHERETRAC
2bec9 45 28 28 22 78 42 65 73 74 49 6e 64 65 78 20 66  E(("xBestIndex f
2beca 6f 72 20 25 73 5c 6e 22 2c 20 70 54 61 62 2d 3e  or %s\n", pTab->
2becb 7a 4e 61 6d 65 29 29 3b 0a 20 20 54 52 41 43 45  zName));.  TRACE
2becc 5f 49 44 58 5f 49 4e 50 55 54 53 28 70 29 3b 0a  _IDX_INPUTS(p);.
2becd 20 20 72 63 20 3d 20 70 56 74 61 62 2d 3e 70 4d    rc = pVtab->pM
2bece 6f 64 75 6c 65 2d 3e 78 42 65 73 74 49 6e 64 65  odule->xBestInde
2becf 78 28 70 56 74 61 62 2c 20 70 29 3b 0a 20 20 54  x(pVtab, p);.  T
2bed0 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53  RACE_IDX_OUTPUTS
2bed1 28 70 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c  (p);.  (void)sql
2bed2 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 50 61  ite3SafetyOn(pPa
2bed3 72 73 65 2d 3e 64 62 29 3b 0a 0a 20 20 69 66 28  rse->db);..  if(
2bed4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2bed5 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  {.    if( rc==SQ
2bed6 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
2bed7 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e      pParse->db->
2bed8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2bed9 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
2beda 21 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20  !pVtab->zErrMsg 
2bedb 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2bedc 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2bedd 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72   "%s", sqlite3Er
2bede 72 53 74 72 28 72 63 29 29 3b 0a 20 20 20 20 7d  rStr(rc));.    }
2bedf 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
2bee0 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2bee1 73 65 2c 20 22 25 73 22 2c 20 70 56 74 61 62 2d  se, "%s", pVtab-
2bee2 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d  >zErrMsg);.    }
2bee3 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
2bee4 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  Free(pParse->db,
2bee5 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29   pVtab->zErrMsg)
2bee6 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d  ;.  pVtab->zErrM
2bee7 73 67 20 3d 20 30 3b 0a 0a 20 20 66 6f 72 28 69  sg = 0;..  for(i
2bee8 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72  =0; i<p->nConstr
2bee9 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  aint; i++){.    
2beea 69 66 28 20 21 70 2d 3e 61 43 6f 6e 73 74 72 61  if( !p->aConstra
2beeb 69 6e 74 5b 69 5d 2e 75 73 61 62 6c 65 20 26 26  int[i].usable &&
2beec 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55   p->aConstraintU
2beed 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65  sage[i].argvInde
2beee 78 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  x>0 ){.      sql
2beef 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2bef0 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
2bef1 22 74 61 62 6c 65 20 25 73 3a 20 78 42 65 73 74  "table %s: xBest
2bef2 49 6e 64 65 78 20 72 65 74 75 72 6e 65 64 20 61  Index returned a
2bef3 6e 20 69 6e 76 61 6c 69 64 20 70 6c 61 6e 22 2c  n invalid plan",
2bef4 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
2bef5 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
2bef6 72 6e 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3b  rn pParse->nErr;
2bef7 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  .}.../*.** Compu
2bef8 74 65 20 74 68 65 20 62 65 73 74 20 69 6e 64 65  te the best inde
2bef9 78 20 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20  x for a virtual 
2befa 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  table..**.** The
2befb 20 62 65 73 74 20 69 6e 64 65 78 20 69 73 20 63   best index is c
2befc 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 78  omputed by the x
2befd 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64  BestIndex method
2befe 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 0a   of the virtual.
2beff 2a 2a 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e  ** table module.
2bf00 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
2bf01 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 20  s really just a 
2bf02 77 72 61 70 70 65 72 20 74 68 61 74 20 73 65 74  wrapper that set
2bf03 73 20 75 70 0a 2a 2a 20 74 68 65 20 73 71 6c 69  s up.** the sqli
2bf04 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
2bf05 74 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73  tructure that is
2bf06 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e 69   used to communi
2bf07 63 61 74 65 20 77 69 74 68 0a 2a 2a 20 78 42 65  cate with.** xBe
2bf08 73 74 49 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49  stIndex..**.** I
2bf09 6e 20 61 20 6a 6f 69 6e 2c 20 74 68 69 73 20 72  n a join, this r
2bf0a 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 62 65 20  outine might be 
2bf0b 63 61 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65 20  called multiple 
2bf0c 74 69 6d 65 73 20 66 6f 72 20 74 68 65 0a 2a 2a  times for the.**
2bf0d 20 73 61 6d 65 20 76 69 72 74 75 61 6c 20 74 61   same virtual ta
2bf0e 62 6c 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ble.  The sqlite
2bf0f 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
2bf10 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65  ucture is create
2bf11 64 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c  d.** and initial
2bf12 69 7a 65 64 20 6f 6e 20 74 68 65 20 66 69 72 73  ized on the firs
2bf13 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64  t invocation and
2bf14 20 72 65 75 73 65 64 20 6f 6e 20 61 6c 6c 20 73   reused on all s
2bf15 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 76  ubsequent.** inv
2bf16 6f 63 61 74 69 6f 6e 73 2e 20 20 54 68 65 20 73  ocations.  The s
2bf17 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2bf18 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  o structure is a
2bf19 6c 73 6f 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a  lso used when.**
2bf1a 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   code is generat
2bf1b 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ed to access the
2bf1c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
2bf1d 20 54 68 65 20 77 68 65 72 65 49 6e 66 6f 44 65   The whereInfoDe
2bf1e 6c 65 74 65 28 29 20 0a 2a 2a 20 72 6f 75 74 69  lete() .** routi
2bf1f 6e 65 20 74 61 6b 65 73 20 63 61 72 65 20 6f 66  ne takes care of
2bf20 20 66 72 65 65 69 6e 67 20 74 68 65 20 73 71 6c   freeing the sql
2bf21 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2bf22 73 74 72 75 63 74 75 72 65 20 61 66 74 65 72 0a  structure after.
2bf23 2a 2a 20 65 76 65 72 79 62 6f 64 79 20 68 61 73  ** everybody has
2bf24 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69   finished with i
2bf25 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
2bf26 64 20 62 65 73 74 56 69 72 74 75 61 6c 49 6e 64  d bestVirtualInd
2bf27 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ex(.  Parse *pPa
2bf28 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
2bf29 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2bf2a 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2bf2b 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2bf2c 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  WC,             
2bf2d 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
2bf2e 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63  lause */.  struc
2bf2f 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2bf30 70 53 72 63 2c 20 20 20 20 20 20 2f 2a 20 54 68  pSrc,      /* Th
2bf31 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65  e FROM clause te
2bf32 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a  rm to search */.
2bf33 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
2bf34 64 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  dy,             
2bf35 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72    /* Mask of cur
2bf36 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f  sors that are no
2bf37 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20  t available */. 
2bf38 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2bf39 72 42 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  rBy,            
2bf3a 20 2f 2a 20 54 68 65 20 6f 72 64 65 72 20 62 79   /* The order by
2bf3b 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
2bf3c 72 65 43 6f 73 74 20 2a 70 43 6f 73 74 2c 20 20  reCost *pCost,  
2bf3d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bf3e 4c 6f 77 65 73 74 20 63 6f 73 74 20 71 75 65 72  Lowest cost quer
2bf3f 79 20 70 6c 61 6e 20 2a 2f 0a 20 20 73 71 6c 69  y plan */.  sqli
2bf40 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2bf41 2a 70 70 49 64 78 49 6e 66 6f 20 20 2f 2a 20 49  *ppIdxInfo  /* I
2bf42 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ndex information
2bf43 20 70 61 73 73 65 64 20 74 6f 20 78 42 65 73 74   passed to xBest
2bf44 49 6e 64 65 78 20 2a 2f 0a 29 7b 0a 20 20 54 61  Index */.){.  Ta
2bf45 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 53 72 63  ble *pTab = pSrc
2bf46 2d 3e 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65  ->pTab;.  sqlite
2bf47 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49  3_index_info *pI
2bf48 64 78 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74  dxInfo;.  struct
2bf49 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
2bf4a 6f 6e 73 74 72 61 69 6e 74 20 2a 70 49 64 78 43  onstraint *pIdxC
2bf4b 6f 6e 73 3b 0a 20 20 73 74 72 75 63 74 20 73 71  ons;.  struct sq
2bf4c 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
2bf4d 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 70 55  traint_usage *pU
2bf4e 73 61 67 65 3b 0a 20 20 57 68 65 72 65 54 65 72  sage;.  WhereTer
2bf4f 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20  m *pTerm;.  int 
2bf50 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 4f 72 64  i, j;.  int nOrd
2bf51 65 72 42 79 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65  erBy;..  /* Make
2bf52 20 73 75 72 65 20 77 73 46 6c 61 67 73 20 69 73   sure wsFlags is
2bf53 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
2bf54 73 6f 6d 65 20 73 61 6e 65 20 76 61 6c 75 65 2e  some sane value.
2bf55 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74   Otherwise, if t
2bf56 68 65 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20  he .  ** malloc 
2bf57 69 6e 20 61 6c 6c 6f 63 61 74 65 49 6e 64 65 78  in allocateIndex
2bf58 49 6e 66 6f 28 29 20 66 61 69 6c 73 20 61 6e 64  Info() fails and
2bf59 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
2bf5a 65 74 75 72 6e 73 20 6c 65 61 76 69 6e 67 0a 20  eturns leaving. 
2bf5b 20 2a 2a 20 77 73 46 6c 61 67 73 20 69 6e 20 61   ** wsFlags in a
2bf5c 6e 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20  n uninitialized 
2bf5d 73 74 61 74 65 2c 20 74 68 65 20 63 61 6c 6c 65  state, the calle
2bf5e 72 20 6d 61 79 20 62 65 68 61 76 65 20 75 6e 70  r may behave unp
2bf5f 72 65 64 69 63 74 61 62 6c 79 2e 0a 20 20 2a 2f  redictably..  */
2bf60 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 73 74 2c  .  memset(pCost,
2bf61 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f 73   0, sizeof(*pCos
2bf62 74 29 29 3b 0a 20 20 70 43 6f 73 74 2d 3e 70 6c  t));.  pCost->pl
2bf63 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57 48 45  an.wsFlags = WHE
2bf64 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 3b  RE_VIRTUALTABLE;
2bf65 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 71  ..  /* If the sq
2bf66 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2bf67 20 73 74 72 75 63 74 75 72 65 20 68 61 73 20 6e   structure has n
2bf68 6f 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73  ot been previous
2bf69 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65  ly.  ** allocate
2bf6a 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  d and initialize
2bf6b 64 2c 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65  d, then allocate
2bf6c 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20   and initialize 
2bf6d 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 70  it now..  */.  p
2bf6e 49 64 78 49 6e 66 6f 20 3d 20 2a 70 70 49 64 78  IdxInfo = *ppIdx
2bf6f 49 6e 66 6f 3b 0a 20 20 69 66 28 20 70 49 64 78  Info;.  if( pIdx
2bf70 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  Info==0 ){.    *
2bf71 70 70 49 64 78 49 6e 66 6f 20 3d 20 70 49 64 78  ppIdxInfo = pIdx
2bf72 49 6e 66 6f 20 3d 20 61 6c 6c 6f 63 61 74 65 49  Info = allocateI
2bf73 6e 64 65 78 49 6e 66 6f 28 70 50 61 72 73 65 2c  ndexInfo(pParse,
2bf74 20 70 57 43 2c 20 70 53 72 63 2c 20 70 4f 72 64   pWC, pSrc, pOrd
2bf75 65 72 42 79 29 3b 0a 20 20 7d 0a 20 20 69 66 28  erBy);.  }.  if(
2bf76 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a   pIdxInfo==0 ){.
2bf77 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2bf78 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
2bf79 69 6e 74 2c 20 74 68 65 20 73 71 6c 69 74 65 33  int, the sqlite3
2bf7a 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2bf7b 63 74 75 72 65 20 74 68 61 74 20 70 49 64 78 49  cture that pIdxI
2bf7c 6e 66 6f 20 70 6f 69 6e 74 73 0a 20 20 2a 2a 20  nfo points.  ** 
2bf7d 74 6f 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65  to will have bee
2bf7e 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 65  n initialized, e
2bf7f 69 74 68 65 72 20 64 75 72 69 6e 67 20 74 68 65  ither during the
2bf80 20 63 75 72 72 65 6e 74 20 69 6e 76 6f 63 61 74   current invocat
2bf81 69 6f 6e 20 6f 72 0a 20 20 2a 2a 20 64 75 72 69  ion or.  ** duri
2bf82 6e 67 20 73 6f 6d 65 20 70 72 69 6f 72 20 69 6e  ng some prior in
2bf83 76 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f 77 20 77  vocation.  Now w
2bf84 65 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20 63  e just have to c
2bf85 75 73 74 6f 6d 69 7a 65 20 74 68 65 0a 20 20 2a  ustomize the.  *
2bf86 2a 20 64 65 74 61 69 6c 73 20 6f 66 20 70 49 64  * details of pId
2bf87 78 49 6e 66 6f 20 66 6f 72 20 74 68 65 20 63 75  xInfo for the cu
2bf88 72 72 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e  rrent invocation
2bf89 20 61 6e 64 20 70 61 73 73 20 69 74 20 74 6f 0a   and pass it to.
2bf8a 20 20 2a 2a 20 78 42 65 73 74 49 6e 64 65 78 2e    ** xBestIndex.
2bf8b 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20  .  */..  /* The 
2bf8c 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 6d 75 73 74  module name must
2bf8d 20 62 65 20 64 65 66 69 6e 65 64 2e 20 41 6c 73   be defined. Als
2bf8e 6f 2c 20 62 79 20 74 68 69 73 20 70 6f 69 6e 74  o, by this point
2bf8f 20 74 68 65 72 65 20 6d 75 73 74 0a 20 20 2a 2a   there must.  **
2bf90 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   be a pointer to
2bf91 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62   an sqlite3_vtab
2bf92 20 73 74 72 75 63 74 75 72 65 2e 20 4f 74 68 65   structure. Othe
2bf93 72 77 69 73 65 0a 20 20 2a 2a 20 73 71 6c 69 74  rwise.  ** sqlit
2bf94 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
2bf95 61 6d 65 73 28 29 20 77 6f 75 6c 64 20 68 61 76  ames() would hav
2bf96 65 20 70 69 63 6b 65 64 20 75 70 20 74 68 65 20  e picked up the 
2bf97 65 72 72 6f 72 2e 20 0a 20 20 2a 2f 0a 20 20 61  error. .  */.  a
2bf98 73 73 65 72 74 28 20 70 54 61 62 2d 3e 61 7a 4d  ssert( pTab->azM
2bf99 6f 64 75 6c 65 41 72 67 20 26 26 20 70 54 61 62  oduleArg && pTab
2bf9a 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
2bf9b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
2bf9c 61 62 2d 3e 70 56 74 61 62 20 29 3b 0a 0a 20 20  ab->pVtab );..  
2bf9d 2f 2a 20 53 65 74 20 74 68 65 20 61 43 6f 6e 73  /* Set the aCons
2bf9e 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20  traint[].usable 
2bf9f 66 69 65 6c 64 73 20 61 6e 64 20 69 6e 69 74 69  fields and initi
2bfa0 61 6c 69 7a 65 20 61 6c 6c 20 0a 20 20 2a 2a 20  alize all .  ** 
2bfa1 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73  output variables
2bfa2 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20   to zero..  **. 
2bfa3 20 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b   ** aConstraint[
2bfa4 5d 2e 75 73 61 62 6c 65 20 69 73 20 74 72 75 65  ].usable is true
2bfa5 20 66 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73   for constraints
2bfa6 20 77 68 65 72 65 20 74 68 65 20 72 69 67 68 74   where the right
2bfa7 2d 68 61 6e 64 0a 20 20 2a 2a 20 73 69 64 65 20  -hand.  ** side 
2bfa8 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 72 65  contains only re
2bfa9 66 65 72 65 6e 63 65 73 20 74 6f 20 74 61 62 6c  ferences to tabl
2bfaa 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  es to the left o
2bfab 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20  f the current.  
2bfac 2a 2a 20 74 61 62 6c 65 2e 20 20 49 6e 20 6f 74  ** table.  In ot
2bfad 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68  her words, if th
2bfae 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  e constraint is 
2bfaf 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a  of the form:.  *
2bfb0 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  *.  **          
2bfb1 20 63 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 0a 20   column = expr. 
2bfb2 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64 20 77 65 20   **.  ** and we 
2bfb3 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20 61  are evaluating a
2bfb4 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 68 65 20   join, then the 
2bfb5 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 63 6f  constraint on co
2bfb6 6c 75 6d 6e 20 69 73 20 0a 20 20 2a 2a 20 6f 6e  lumn is .  ** on
2bfb7 6c 79 20 76 61 6c 69 64 20 69 66 20 61 6c 6c 20  ly valid if all 
2bfb8 74 61 62 6c 65 73 20 72 65 66 65 72 65 6e 63 65  tables reference
2bfb9 64 20 69 6e 20 65 78 70 72 20 6f 63 63 75 72 20  d in expr occur 
2bfba 74 6f 20 74 68 65 20 6c 65 66 74 0a 20 20 2a 2a  to the left.  **
2bfbb 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f   of the table co
2bfbc 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2e  ntaining column.
2bfbd 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61  .  **.  ** The a
2bfbe 43 6f 6e 73 74 72 61 69 6e 74 73 5b 5d 20 61 72  Constraints[] ar
2bfbf 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 65 6e 74  ray contains ent
2bfc0 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f 6e  ries for all con
2bfc1 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 6f 6e  straints.  ** on
2bfc2 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62   the current tab
2bfc3 6c 65 2e 20 20 54 68 61 74 20 77 61 79 20 77 65  le.  That way we
2bfc4 20 6f 6e 6c 79 20 68 61 76 65 20 74 6f 20 63 6f   only have to co
2bfc5 6d 70 75 74 65 20 69 74 20 6f 6e 63 65 0a 20 20  mpute it once.  
2bfc6 2a 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20 77  ** even though w
2bfc7 65 20 6d 69 67 68 74 20 74 72 79 20 74 6f 20 70  e might try to p
2bfc8 69 63 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64  ick the best ind
2bfc9 65 78 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ex multiple time
2bfca 73 2e 0a 20 20 2a 2a 20 46 6f 72 20 65 61 63 68  s..  ** For each
2bfcb 20 61 74 74 65 6d 70 74 20 61 74 20 70 69 63 6b   attempt at pick
2bfcc 69 6e 67 20 61 6e 20 69 6e 64 65 78 2c 20 74 68  ing an index, th
2bfcd 65 20 6f 72 64 65 72 20 6f 66 20 74 61 62 6c 65  e order of table
2bfce 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 6a 6f  s in the.  ** jo
2bfcf 69 6e 20 6d 69 67 68 74 20 62 65 20 64 69 66 66  in might be diff
2bfd0 65 72 65 6e 74 20 73 6f 20 77 65 20 68 61 76 65  erent so we have
2bfd1 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68   to recompute th
2bfd2 65 20 75 73 61 62 6c 65 20 66 6c 61 67 0a 20 20  e usable flag.  
2bfd3 2a 2a 20 65 61 63 68 20 74 69 6d 65 2e 0a 20 20  ** each time..  
2bfd4 2a 2f 0a 20 20 70 49 64 78 43 6f 6e 73 20 3d 20  */.  pIdxCons = 
2bfd5 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  *(struct sqlite3
2bfd6 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
2bfd7 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61  t**)&pIdxInfo->a
2bfd8 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 70 55  Constraint;.  pU
2bfd9 73 61 67 65 20 3d 20 70 49 64 78 49 6e 66 6f 2d  sage = pIdxInfo-
2bfda 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
2bfdb 65 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  e;.  for(i=0; i<
2bfdc 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74  pIdxInfo->nConst
2bfdd 72 61 69 6e 74 3b 20 69 2b 2b 2c 20 70 49 64 78  raint; i++, pIdx
2bfde 43 6f 6e 73 2b 2b 29 7b 0a 20 20 20 20 6a 20 3d  Cons++){.    j =
2bfdf 20 70 49 64 78 43 6f 6e 73 2d 3e 69 54 65 72 6d   pIdxCons->iTerm
2bfe0 4f 66 66 73 65 74 3b 0a 20 20 20 20 70 54 65 72  Offset;.    pTer
2bfe1 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 6a 5d 3b 0a  m = &pWC->a[j];.
2bfe2 20 20 20 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73      pIdxCons->us
2bfe3 61 62 6c 65 20 3d 20 20 28 70 54 65 72 6d 2d 3e  able =  (pTerm->
2bfe4 70 72 65 72 65 71 52 69 67 68 74 20 26 20 6e 6f  prereqRight & no
2bfe5 74 52 65 61 64 79 29 3d 3d 30 20 3f 31 3a 30 3b  tReady)==0 ?1:0;
2bfe6 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 55  .  }.  memset(pU
2bfe7 73 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  sage, 0, sizeof(
2bfe8 70 55 73 61 67 65 5b 30 5d 29 2a 70 49 64 78 49  pUsage[0])*pIdxI
2bfe9 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  nfo->nConstraint
2bfea 29 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66  );.  if( pIdxInf
2bfeb 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  o->needToFreeIdx
2bfec 53 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Str ){.    sqlit
2bfed 65 33 5f 66 72 65 65 28 70 49 64 78 49 6e 66 6f  e3_free(pIdxInfo
2bfee 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 7d 0a 20  ->idxStr);.  }. 
2bfef 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74   pIdxInfo->idxSt
2bff0 72 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66  r = 0;.  pIdxInf
2bff1 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 30 3b 0a 20  o->idxNum = 0;. 
2bff2 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54   pIdxInfo->needT
2bff3 6f 46 72 65 65 49 64 78 53 74 72 20 3d 20 30 3b  oFreeIdxStr = 0;
2bff4 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6f 72 64  .  pIdxInfo->ord
2bff5 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 3d 20 30  erByConsumed = 0
2bff6 3b 0a 20 20 2f 2a 20 28 28 64 6f 75 62 6c 65 29  ;.  /* ((double)
2bff7 32 29 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51  2) In case of SQ
2bff8 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
2bff9 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20  NG_POINT... */. 
2bffa 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d   pIdxInfo->estim
2bffb 61 74 65 64 43 6f 73 74 20 3d 20 53 51 4c 49 54  atedCost = SQLIT
2bffc 45 5f 42 49 47 5f 44 42 4c 20 2f 20 28 28 64 6f  E_BIG_DBL / ((do
2bffd 75 62 6c 65 29 32 29 3b 0a 20 20 6e 4f 72 64 65  uble)2);.  nOrde
2bffe 72 42 79 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e  rBy = pIdxInfo->
2bfff 6e 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28 20  nOrderBy;.  if( 
2c000 21 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  !pOrderBy ){.   
2c001 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65   pIdxInfo->nOrde
2c002 72 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  rBy = 0;.  }..  
2c003 69 66 28 20 76 74 61 62 42 65 73 74 49 6e 64 65  if( vtabBestInde
2c004 78 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  x(pParse, pTab, 
2c005 70 49 64 78 49 6e 66 6f 29 20 29 7b 0a 20 20 20  pIdxInfo) ){.   
2c006 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
2c007 2f 2a 20 54 68 65 20 63 6f 73 74 20 69 73 20 6e  /* The cost is n
2c008 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65  ot allowed to be
2c009 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 51 4c   larger than SQL
2c00a 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 74 68 65  ITE_BIG_DBL (the
2c00b 0a 20 20 2a 2a 20 69 6e 69 74 61 6c 20 76 61 6c  .  ** inital val
2c00c 75 65 20 6f 66 20 6c 6f 77 65 73 74 43 6f 73 74  ue of lowestCost
2c00d 20 69 6e 20 74 68 69 73 20 6c 6f 6f 70 2e 20 49   in this loop. I
2c00e 66 20 69 74 20 69 73 2c 20 74 68 65 6e 20 74 68  f it is, then th
2c00f 65 0a 20 20 2a 2a 20 28 63 6f 73 74 3c 6c 6f 77  e.  ** (cost<low
2c010 65 73 74 43 6f 73 74 29 20 74 65 73 74 20 62 65  estCost) test be
2c011 6c 6f 77 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  low will never b
2c012 65 20 74 72 75 65 2e 0a 20 20 2a 2a 20 0a 20 20  e true..  ** .  
2c013 2a 2a 20 55 73 65 20 22 28 64 6f 75 62 6c 65 29  ** Use "(double)
2c014 32 22 20 69 6e 73 74 65 61 64 20 6f 66 20 22 32  2" instead of "2
2c015 2e 30 22 20 69 6e 20 63 61 73 65 20 4f 4d 49 54  .0" in case OMIT
2c016 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20  _FLOATING_POINT 
2c017 0a 20 20 2a 2a 20 69 73 20 64 65 66 69 6e 65 64  .  ** is defined
2c018 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 53 51  ..  */.  if( (SQ
2c019 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2f 28 28 64  LITE_BIG_DBL/((d
2c01a 6f 75 62 6c 65 29 32 29 29 3c 70 49 64 78 49 6e  ouble)2))<pIdxIn
2c01b 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73  fo->estimatedCos
2c01c 74 20 29 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e  t ){.    pCost->
2c01d 72 43 6f 73 74 20 3d 20 28 53 51 4c 49 54 45 5f  rCost = (SQLITE_
2c01e 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c 65  BIG_DBL/((double
2c01f 29 32 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  )2));.  }else{. 
2c020 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20     pCost->rCost 
2c021 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69  = pIdxInfo->esti
2c022 6d 61 74 65 64 43 6f 73 74 3b 0a 20 20 7d 0a 20  matedCost;.  }. 
2c023 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70   pCost->plan.u.p
2c024 56 74 61 62 49 64 78 20 3d 20 70 49 64 78 49 6e  VtabIdx = pIdxIn
2c025 66 6f 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e  fo;.  if( pIdxIn
2c026 66 6f 20 26 26 20 70 49 64 78 49 6e 66 6f 2d 3e  fo && pIdxInfo->
2c027 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20  orderByConsumed 
2c028 29 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c  ){.    pCost->pl
2c029 61 6e 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48  an.wsFlags |= WH
2c02a 45 52 45 5f 4f 52 44 45 52 42 59 3b 0a 20 20 7d  ERE_ORDERBY;.  }
2c02b 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e  .  pCost->plan.n
2c02c 45 71 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e  Eq = 0;.  pIdxIn
2c02d 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e  fo->nOrderBy = n
2c02e 4f 72 64 65 72 42 79 3b 0a 0a 20 20 2f 2a 20 54  OrderBy;..  /* T
2c02f 72 79 20 74 6f 20 66 69 6e 64 20 61 20 6d 6f 72  ry to find a mor
2c030 65 20 65 66 66 69 63 69 65 6e 74 20 61 63 63 65  e efficient acce
2c031 73 73 20 70 61 74 74 65 72 6e 20 62 79 20 75 73  ss pattern by us
2c032 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 69 6e 64  ing multiple ind
2c033 65 78 65 73 0a 20 20 2a 2a 20 74 6f 20 6f 70 74  exes.  ** to opt
2c034 69 6d 69 7a 65 20 61 6e 20 4f 52 20 65 78 70 72  imize an OR expr
2c035 65 73 73 69 6f 6e 20 77 69 74 68 69 6e 20 74 68  ession within th
2c036 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
2c037 0a 20 20 2a 2f 0a 20 20 62 65 73 74 4f 72 43 6c  .  */.  bestOrCl
2c038 61 75 73 65 49 6e 64 65 78 28 70 50 61 72 73 65  auseIndex(pParse
2c039 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  , pWC, pSrc, not
2c03a 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79 2c  Ready, pOrderBy,
2c03b 20 70 43 6f 73 74 29 3b 0a 7d 0a 23 65 6e 64 69   pCost);.}.#endi
2c03c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2c03d 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
2c03e 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
2c03f 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20   query plan for 
2c040 61 63 63 65 73 73 69 6e 67 20 61 20 70 61 72 74  accessing a part
2c041 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 20 57  icular table.  W
2c042 72 69 74 65 20 74 68 65 0a 2a 2a 20 62 65 73 74  rite the.** best
2c043 20 71 75 65 72 79 20 70 6c 61 6e 20 61 6e 64 20   query plan and 
2c044 69 74 73 20 63 6f 73 74 20 69 6e 74 6f 20 74 68  its cost into th
2c045 65 20 57 68 65 72 65 43 6f 73 74 20 6f 62 6a 65  e WhereCost obje
2c046 63 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 74  ct supplied as t
2c047 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61 72 61 6d  he.** last param
2c048 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eter..**.** The 
2c049 6c 6f 77 65 73 74 20 63 6f 73 74 20 70 6c 61 6e  lowest cost plan
2c04a 20 77 69 6e 73 2e 20 20 54 68 65 20 63 6f 73 74   wins.  The cost
2c04b 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20   is an estimate 
2c04c 6f 66 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  of the amount of
2c04d 0a 2a 2a 20 43 50 55 20 61 6e 64 20 64 69 73 6b  .** CPU and disk
2c04e 20 49 2f 4f 20 6e 65 65 64 20 74 6f 20 70 72 6f   I/O need to pro
2c04f 63 65 73 73 20 74 68 65 20 72 65 71 75 65 73 74  cess the request
2c050 20 75 73 69 6e 67 20 74 68 65 20 73 65 6c 65 63   using the selec
2c051 74 65 64 20 70 6c 61 6e 2e 0a 2a 2a 20 46 61 63  ted plan..** Fac
2c052 74 6f 72 73 20 74 68 61 74 20 69 6e 66 6c 75 65  tors that influe
2c053 6e 63 65 20 63 6f 73 74 20 69 6e 63 6c 75 64 65  nce cost include
2c054 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 54 68  :.**.**    *  Th
2c055 65 20 65 73 74 69 6d 61 74 65 64 20 6e 75 6d 62  e estimated numb
2c056 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20  er of rows that 
2c057 77 69 6c 6c 20 62 65 20 72 65 74 72 69 65 76 65  will be retrieve
2c058 64 2e 20 20 28 54 68 65 0a 2a 2a 20 20 20 20 20  d.  (The.**     
2c059 20 20 66 65 77 65 72 20 74 68 65 20 62 65 74 74    fewer the bett
2c05a 65 72 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  er.).**.**    * 
2c05b 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
2c05c 73 6f 72 74 69 6e 67 20 6d 75 73 74 20 6f 63 63  sorting must occ
2c05d 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  ur..**.**    *  
2c05e 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  Whether or not t
2c05f 68 65 72 65 20 6d 75 73 74 20 62 65 20 73 65 70  here must be sep
2c060 61 72 61 74 65 20 6c 6f 6f 6b 75 70 73 20 69 6e  arate lookups in
2c061 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 69 6e   the.**       in
2c062 64 65 78 20 61 6e 64 20 69 6e 20 74 68 65 20 6d  dex and in the m
2c063 61 69 6e 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ain table..**.**
2c064 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61 6e   If there was an
2c065 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75   INDEXED BY clau
2c066 73 65 20 28 70 53 72 63 2d 3e 70 49 6e 64 65 78  se (pSrc->pIndex
2c067 29 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  ) attached to th
2c068 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68  e table in.** th
2c069 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c  e SQL statement,
2c06a 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
2c06b 69 6f 6e 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65  ion only conside
2c06c 72 73 20 70 6c 61 6e 73 20 75 73 69 6e 67 20 74  rs plans using t
2c06d 68 65 20 0a 2a 2a 20 6e 61 6d 65 64 20 69 6e 64  he .** named ind
2c06e 65 78 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 70  ex. If no such p
2c06f 6c 61 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68  lan is found, th
2c070 65 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  en the returned 
2c071 63 6f 73 74 20 69 73 0a 2a 2a 20 53 51 4c 49 54  cost is.** SQLIT
2c072 45 5f 42 49 47 5f 44 42 4c 2e 20 49 66 20 61 20  E_BIG_DBL. If a 
2c073 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64 20 74 68  plan is found th
2c074 61 74 20 75 73 65 73 20 74 68 65 20 6e 61 6d 65  at uses the name
2c075 64 20 69 6e 64 65 78 2c 20 0a 2a 2a 20 74 68 65  d index, .** the
2c076 6e 20 74 68 65 20 63 6f 73 74 20 69 73 20 63 61  n the cost is ca
2c077 6c 63 75 6c 61 74 65 64 20 69 6e 20 74 68 65 20  lculated in the 
2c078 75 73 75 61 6c 20 77 61 79 2e 0a 2a 2a 0a 2a 2a  usual way..**.**
2c079 20 49 66 20 61 20 4e 4f 54 20 49 4e 44 45 58 45   If a NOT INDEXE
2c07a 44 20 63 6c 61 75 73 65 20 28 70 53 72 63 2d 3e  D clause (pSrc->
2c07b 6e 6f 74 49 6e 64 65 78 65 64 21 3d 30 29 20 77  notIndexed!=0) w
2c07c 61 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  as attached to t
2c07d 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20 69 6e 20  he table .** in 
2c07e 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
2c07f 6d 65 6e 74 2c 20 74 68 65 6e 20 6e 6f 20 69 6e  ment, then no in
2c080 64 65 78 65 73 20 61 72 65 20 63 6f 6e 73 69 64  dexes are consid
2c081 65 72 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74  ered. However, t
2c082 68 65 20 0a 2a 2a 20 73 65 6c 65 63 74 65 64 20  he .** selected 
2c083 70 6c 61 6e 20 6d 61 79 20 73 74 69 6c 6c 20 74  plan may still t
2c084 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66  ake advantage of
2c085 20 74 68 65 20 74 61 62 6c 65 73 20 62 75 69 6c   the tables buil
2c086 74 2d 69 6e 20 72 6f 77 69 64 0a 2a 2a 20 69 6e  t-in rowid.** in
2c087 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  dex..*/.static v
2c088 6f 69 64 20 62 65 73 74 42 74 72 65 65 49 6e 64  oid bestBtreeInd
2c089 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ex(.  Parse *pPa
2c08a 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
2c08b 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
2c08c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
2c08d 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2c08e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2c08f 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2c090 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2c091 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f  t_item *pSrc,  /
2c092 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * The FROM claus
2c093 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68  e term to search
2c094 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f   */.  Bitmask no
2c095 74 52 65 61 64 79 2c 20 20 20 20 20 20 20 20 20  tReady,         
2c096 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72    /* Mask of cur
2c097 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f  sors that are no
2c098 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20  t available */. 
2c099 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2c09a 72 42 79 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rBy,         /* 
2c09b 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
2c09c 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f  use */.  WhereCo
2c09d 73 74 20 2a 70 43 6f 73 74 20 20 20 20 20 20 20  st *pCost       
2c09e 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63       /* Lowest c
2c09f 6f 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 2a  ost query plan *
2c0a0 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d  /.){.  WhereTerm
2c0a1 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
2c0a2 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74     /* A single t
2c0a3 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45  erm of the WHERE
2c0a4 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
2c0a5 20 69 43 75 72 20 3d 20 70 53 72 63 2d 3e 69 43   iCur = pSrc->iC
2c0a6 75 72 73 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20  ursor;   /* The 
2c0a7 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61  cursor of the ta
2c0a8 62 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73 73  ble to be access
2c0a9 65 64 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70  ed */.  Index *p
2c0aa 50 72 6f 62 65 3b 20 20 20 20 20 20 20 20 20 20  Probe;          
2c0ab 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20      /* An index 
2c0ac 77 65 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e  we are evaluatin
2c0ad 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65 76 3b 20  g */.  int rev; 
2c0ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0af 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 63     /* True to sc
2c0b0 61 6e 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72  an in reverse or
2c0b1 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 77 73 46  der */.  int wsF
2c0b2 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
2c0b3 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73       /* Flags as
2c0b4 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 50  sociated with pP
2c0b5 72 6f 62 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  robe */.  int nE
2c0b6 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
2c0b7 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2c0b8 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73  of == or IN cons
2c0b9 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74  traints */.  int
2c0ba 20 65 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20   eqTermMask;    
2c0bb 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
2c0bc 20 6f 66 20 76 61 6c 69 64 20 65 71 75 61 6c 69   of valid equali
2c0bd 74 79 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f 0a  ty operators */.
2c0be 20 20 64 6f 75 62 6c 65 20 63 6f 73 74 3b 20 20    double cost;  
2c0bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c0c0 20 43 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 70   Cost of using p
2c0c1 50 72 6f 62 65 20 2a 2f 0a 20 20 64 6f 75 62 6c  Probe */.  doubl
2c0c2 65 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20  e nRow;         
2c0c3 20 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61         /* Estima
2c0c4 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  ted number of ro
2c0c5 77 73 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74  ws in result set
2c0c6 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
2c0c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0c8 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2c0c9 72 20 2a 2f 0a 0a 20 20 57 48 45 52 45 54 52 41  r */..  WHERETRA
2c0ca 43 45 28 28 22 62 65 73 74 49 6e 64 65 78 3a 20  CE(("bestIndex: 
2c0cb 74 62 6c 3d 25 73 20 6e 6f 74 52 65 61 64 79 3d  tbl=%s notReady=
2c0cc 25 6c 6c 78 5c 6e 22 2c 20 70 53 72 63 2d 3e 70  %llx\n", pSrc->p
2c0cd 54 61 62 2d 3e 7a 4e 61 6d 65 2c 6e 6f 74 52 65  Tab->zName,notRe
2c0ce 61 64 79 29 29 3b 0a 20 20 70 50 72 6f 62 65 20  ady));.  pProbe 
2c0cf 3d 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e 70 49  = pSrc->pTab->pI
2c0d0 6e 64 65 78 3b 0a 20 20 69 66 28 20 70 53 72 63  ndex;.  if( pSrc
2c0d1 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 29 7b 0a  ->notIndexed ){.
2c0d2 20 20 20 20 70 50 72 6f 62 65 20 3d 20 30 3b 0a      pProbe = 0;.
2c0d3 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
2c0d4 20 74 61 62 6c 65 20 68 61 73 20 6e 6f 20 69 6e   table has no in
2c0d5 64 69 63 65 73 20 61 6e 64 20 74 68 65 72 65 20  dices and there 
2c0d6 61 72 65 20 6e 6f 20 74 65 72 6d 73 20 69 6e 20  are no terms in 
2c0d7 74 68 65 20 77 68 65 72 65 0a 20 20 2a 2a 20 63  the where.  ** c
2c0d8 6c 61 75 73 65 20 74 68 61 74 20 72 65 66 65 72  lause that refer
2c0d9 20 74 6f 20 74 68 65 20 52 4f 57 49 44 2c 20 74   to the ROWID, t
2c0da 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e 65 76 65  hen we will neve
2c0db 72 20 62 65 20 61 62 6c 65 20 74 6f 20 64 6f 0a  r be able to do.
2c0dc 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67 20 6f 74    ** anything ot
2c0dd 68 65 72 20 74 68 61 6e 20 61 20 66 75 6c 6c 20  her than a full 
2c0de 74 61 62 6c 65 20 73 63 61 6e 20 6f 6e 20 74 68  table scan on th
2c0df 69 73 20 74 61 62 6c 65 2e 20 20 57 65 20 6d 69  is table.  We mi
2c0e0 67 68 74 20 61 73 0a 20 20 2a 2a 20 77 65 6c 6c  ght as.  ** well
2c0e1 20 70 75 74 20 69 74 20 66 69 72 73 74 20 69 6e   put it first in
2c0e2 20 74 68 65 20 6a 6f 69 6e 20 6f 72 64 65 72 2e   the join order.
2c0e3 20 20 54 68 61 74 20 77 61 79 2c 20 70 65 72 68    That way, perh
2c0e4 61 70 73 20 69 74 20 63 61 6e 20 62 65 0a 20 20  aps it can be.  
2c0e5 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  ** referenced by
2c0e6 20 6f 74 68 65 72 20 74 61 62 6c 65 73 20 69 6e   other tables in
2c0e7 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a   the join..  */.
2c0e8 20 20 6d 65 6d 73 65 74 28 70 43 6f 73 74 2c 20    memset(pCost, 
2c0e9 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f 73 74  0, sizeof(*pCost
2c0ea 29 29 3b 0a 20 20 69 66 28 20 70 50 72 6f 62 65  ));.  if( pProbe
2c0eb 3d 3d 30 20 26 26 0a 20 20 20 20 20 66 69 6e 64  ==0 &&.     find
2c0ec 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20  Term(pWC, iCur, 
2c0ed 2d 31 2c 20 30 2c 20 57 4f 5f 45 51 7c 57 4f 5f  -1, 0, WO_EQ|WO_
2c0ee 49 4e 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57  IN|WO_LT|WO_LE|W
2c0ef 4f 5f 47 54 7c 57 4f 5f 47 45 2c 30 29 3d 3d 30  O_GT|WO_GE,0)==0
2c0f0 20 26 26 0a 20 20 20 20 20 28 70 4f 72 64 65 72   &&.     (pOrder
2c0f1 42 79 3d 3d 30 20 7c 7c 20 21 73 6f 72 74 61 62  By==0 || !sortab
2c0f2 6c 65 42 79 52 6f 77 69 64 28 69 43 75 72 2c 20  leByRowid(iCur, 
2c0f3 70 4f 72 64 65 72 42 79 2c 20 70 57 43 2d 3e 70  pOrderBy, pWC->p
2c0f4 4d 61 73 6b 53 65 74 2c 20 26 72 65 76 29 29 20  MaskSet, &rev)) 
2c0f5 29 7b 0a 20 20 20 20 20 69 66 28 20 70 50 61 72  ){.     if( pPar
2c0f6 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  se->db->flags & 
2c0f7 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72  SQLITE_ReverseOr
2c0f8 64 65 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  der ){.      /* 
2c0f9 46 6f 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  For application 
2c0fa 74 65 73 74 69 6e 67 2c 20 72 61 6e 64 6f 6d 6c  testing, randoml
2c0fb 79 20 72 65 76 65 72 73 65 20 74 68 65 20 6f 75  y reverse the ou
2c0fc 74 70 75 74 20 6f 72 64 65 72 20 66 6f 72 0a 20  tput order for. 
2c0fd 20 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 73       ** SELECT s
2c0fe 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 6f  tatements that o
2c0ff 6d 69 74 20 74 68 65 20 4f 52 44 45 52 20 42 59  mit the ORDER BY
2c100 20 63 6c 61 75 73 65 2e 20 20 54 68 69 73 20 77   clause.  This w
2c101 69 6c 6c 20 68 65 6c 70 0a 20 20 20 20 20 20 2a  ill help.      *
2c102 2a 20 74 6f 20 66 69 6e 64 20 63 61 73 65 73 20  * to find cases 
2c103 77 68 65 72 65 0a 20 20 20 20 20 20 2a 2f 0a 20  where.      */. 
2c104 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e       pCost->plan
2c105 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52  .wsFlags |= WHER
2c106 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 20 20 7d  E_REVERSE;.    }
2c107 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
2c108 0a 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20  .  pCost->rCost 
2c109 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  = SQLITE_BIG_DBL
2c10a 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f  ;..  /* Check fo
2c10b 72 20 61 20 72 6f 77 69 64 3d 45 58 50 52 20 6f  r a rowid=EXPR o
2c10c 72 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29  r rowid IN (...)
2c10d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 49 66   constraints. If
2c10e 20 74 68 65 72 65 20 77 61 73 0a 20 20 2a 2a 20   there was.  ** 
2c10f 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  an INDEXED BY cl
2c110 61 75 73 65 20 61 74 74 61 63 68 65 64 20 74 6f  ause attached to
2c111 20 74 68 69 73 20 74 61 62 6c 65 2c 20 73 6b 69   this table, ski
2c112 70 20 74 68 69 73 20 73 74 65 70 2e 0a 20 20 2a  p this step..  *
2c113 2f 0a 20 20 69 66 28 20 21 70 53 72 63 2d 3e 70  /.  if( !pSrc->p
2c114 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 70 54 65  Index ){.    pTe
2c115 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  rm = findTerm(pW
2c116 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
2c117 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f  Ready, WO_EQ|WO_
2c118 49 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  IN, 0);.    if( 
2c119 70 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 45  pTerm ){.      E
2c11a 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 20  xpr *pExpr;.    
2c11b 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
2c11c 46 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 52 4f  Flags = WHERE_RO
2c11d 57 49 44 5f 45 51 3b 0a 20 20 20 20 20 20 69 66  WID_EQ;.      if
2c11e 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  ( pTerm->eOperat
2c11f 6f 72 20 26 20 57 4f 5f 45 51 20 29 7b 0a 20 20  or & WO_EQ ){.  
2c120 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 3d 3d        /* Rowid==
2c121 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 62   is always the b
2c122 65 73 74 20 70 69 63 6b 2e 20 20 4c 6f 6f 6b 20  est pick.  Look 
2c123 6e 6f 20 66 75 72 74 68 65 72 2e 20 20 42 65 63  no further.  Bec
2c124 61 75 73 65 20 6f 6e 6c 79 0a 20 20 20 20 20 20  ause only.      
2c125 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 72 6f    ** a single ro
2c126 77 20 69 73 20 67 65 6e 65 72 61 74 65 64 2c 20  w is generated, 
2c127 6f 75 74 70 75 74 20 69 73 20 61 6c 77 61 79 73  output is always
2c128 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
2c129 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 43 6f 73   */.        pCos
2c12a 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  t->plan.wsFlags 
2c12b 3d 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51  = WHERE_ROWID_EQ
2c12c 20 7c 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b   | WHERE_UNIQUE;
2c12d 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  .        pCost->
2c12e 70 6c 61 6e 2e 6e 45 71 20 3d 20 31 3b 0a 20 20  plan.nEq = 1;.  
2c12f 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45        WHERETRACE
2c130 28 28 22 2e 2e 2e 20 62 65 73 74 20 69 73 20 72  (("... best is r
2c131 6f 77 69 64 5c 6e 22 29 29 3b 0a 20 20 20 20 20  owid\n"));.     
2c132 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20     pCost->rCost 
2c133 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 43 6f  = 0;.        pCo
2c134 73 74 2d 3e 6e 52 6f 77 20 3d 20 31 3b 0a 20 20  st->nRow = 1;.  
2c135 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2c136 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 45      }else if( !E
2c137 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 28  xprHasProperty((
2c138 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70  pExpr = pTerm->p
2c139 45 78 70 72 29 2c 20 45 50 5f 78 49 73 53 65 6c  Expr), EP_xIsSel
2c13a 65 63 74 29 20 0a 20 20 20 20 20 20 20 20 20 20  ect) .          
2c13b 20 20 20 26 26 20 70 45 78 70 72 2d 3e 78 2e 70     && pExpr->x.p
2c13c 4c 69 73 74 20 0a 20 20 20 20 20 20 29 7b 0a 20  List .      ){. 
2c13d 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20         /* Rowid 
2c13e 49 4e 20 28 4c 49 53 54 29 3a 20 63 6f 73 74 20  IN (LIST): cost 
2c13f 69 73 20 4e 6c 6f 67 4e 20 77 68 65 72 65 20 4e  is NlogN where N
2c140 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
2c141 66 20 6c 69 73 74 0a 20 20 20 20 20 20 20 20 2a  f list.        *
2c142 2a 20 65 6c 65 6d 65 6e 74 73 2e 20 20 2a 2f 0a  * elements.  */.
2c143 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 72          pCost->r
2c144 43 6f 73 74 20 3d 20 70 43 6f 73 74 2d 3e 6e 52  Cost = pCost->nR
2c145 6f 77 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ow = pExpr->x.pL
2c146 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  ist->nExpr;.    
2c147 20 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74      pCost->rCost
2c148 20 2a 3d 20 65 73 74 4c 6f 67 28 70 43 6f 73 74   *= estLog(pCost
2c149 2d 3e 72 43 6f 73 74 29 3b 0a 20 20 20 20 20 20  ->rCost);.      
2c14a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
2c14b 2a 20 52 6f 77 69 64 20 49 4e 20 28 53 45 4c 45  * Rowid IN (SELE
2c14c 43 54 29 3a 20 63 6f 73 74 20 69 73 20 4e 6c 6f  CT): cost is Nlo
2c14d 67 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  gN where N is th
2c14e 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
2c14f 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 74  .        ** in t
2c150 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
2c151 20 69 6e 6e 65 72 20 73 65 6c 65 63 74 2e 20 20   inner select.  
2c152 57 65 20 68 61 76 65 20 6e 6f 20 77 61 79 20 74  We have no way t
2c153 6f 20 65 73 74 69 6d 61 74 65 0a 20 20 20 20 20  o estimate.     
2c154 20 20 20 2a 2a 20 74 68 61 74 20 76 61 6c 75 65     ** that value
2c155 20 73 6f 20 6d 61 6b 65 20 61 20 77 69 6c 64 20   so make a wild 
2c156 67 75 65 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20  guess. */.      
2c157 20 20 70 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20    pCost->nRow = 
2c158 31 30 30 3b 0a 20 20 20 20 20 20 20 20 70 43 6f  100;.        pCo
2c159 73 74 2d 3e 72 43 6f 73 74 20 3d 20 32 30 30 3b  st->rCost = 200;
2c15a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 57  .      }.      W
2c15b 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
2c15c 72 6f 77 69 64 20 49 4e 20 63 6f 73 74 3a 20 25  rowid IN cost: %
2c15d 2e 39 67 5c 6e 22 2c 20 70 43 6f 73 74 2d 3e 72  .9g\n", pCost->r
2c15e 43 6f 73 74 29 29 3b 0a 20 20 20 20 7d 0a 20 20  Cost));.    }.  
2c15f 0a 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65  .    /* Estimate
2c160 20 74 68 65 20 63 6f 73 74 20 6f 66 20 61 20 74   the cost of a t
2c161 61 62 6c 65 20 73 63 61 6e 2e 20 20 49 66 20 77  able scan.  If w
2c162 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 68 6f  e do not know ho
2c163 77 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 65 6e  w many.    ** en
2c164 74 72 69 65 73 20 61 72 65 20 69 6e 20 74 68 65  tries are in the
2c165 20 74 61 62 6c 65 2c 20 75 73 65 20 31 20 6d 69   table, use 1 mi
2c166 6c 6c 69 6f 6e 20 61 73 20 61 20 67 75 65 73 73  llion as a guess
2c167 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 6f 73  ..    */.    cos
2c168 74 20 3d 20 70 50 72 6f 62 65 20 3f 20 70 50 72  t = pProbe ? pPr
2c169 6f 62 65 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d  obe->aiRowEst[0]
2c16a 20 3a 20 31 30 30 30 30 30 30 3b 0a 20 20 20 20   : 1000000;.    
2c16b 57 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e  WHERETRACE(("...
2c16c 20 74 61 62 6c 65 20 73 63 61 6e 20 62 61 73 65   table scan base
2c16d 20 63 6f 73 74 3a 20 25 2e 39 67 5c 6e 22 2c 20   cost: %.9g\n", 
2c16e 63 6f 73 74 29 29 3b 0a 20 20 20 20 77 73 46 6c  cost));.    wsFl
2c16f 61 67 73 20 3d 20 57 48 45 52 45 5f 52 4f 57 49  ags = WHERE_ROWI
2c170 44 5f 52 41 4e 47 45 3b 0a 20 20 0a 20 20 20 20  D_RANGE;.  .    
2c171 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6e  /* Check for con
2c172 73 74 72 61 69 6e 74 73 20 6f 6e 20 61 20 72 61  straints on a ra
2c173 6e 67 65 20 6f 66 20 72 6f 77 69 64 73 20 69 6e  nge of rowids in
2c174 20 61 20 74 61 62 6c 65 20 73 63 61 6e 2e 0a 20   a table scan.. 
2c175 20 20 20 2a 2f 0a 20 20 20 20 70 54 65 72 6d 20     */.    pTerm 
2c176 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
2c177 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61  iCur, -1, notRea
2c178 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c  dy, WO_LT|WO_LE|
2c179 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30 29 3b  WO_GT|WO_GE, 0);
2c17a 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 20 29  .    if( pTerm )
2c17b 7b 0a 20 20 20 20 20 20 69 66 28 20 66 69 6e 64  {.      if( find
2c17c 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20  Term(pWC, iCur, 
2c17d 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f  -1, notReady, WO
2c17e 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30 29 20 29 7b  _LT|WO_LE, 0) ){
2c17f 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73  .        wsFlags
2c180 20 7c 3d 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49   |= WHERE_TOP_LI
2c181 4d 49 54 3b 0a 20 20 20 20 20 20 20 20 63 6f 73  MIT;.        cos
2c182 74 20 2f 3d 20 33 3b 20 20 2f 2a 20 47 75 65 73  t /= 3;  /* Gues
2c183 73 20 74 68 61 74 20 72 6f 77 69 64 3c 45 58 50  s that rowid<EXP
2c184 52 20 65 6c 69 6d 69 6e 61 74 65 73 20 74 77 6f  R eliminates two
2c185 2d 74 68 69 72 64 73 20 6f 66 20 72 6f 77 73 20  -thirds of rows 
2c186 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  */.      }.     
2c187 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70 57   if( findTerm(pW
2c188 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
2c189 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f  Ready, WO_GT|WO_
2c18a 47 45 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20  GE, 0) ){.      
2c18b 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2c18c 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20  RE_BTM_LIMIT;.  
2c18d 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20 33 3b        cost /= 3;
2c18e 20 20 2f 2a 20 47 75 65 73 73 20 74 68 61 74 20    /* Guess that 
2c18f 72 6f 77 69 64 3e 45 58 50 52 20 65 6c 69 6d 69  rowid>EXPR elimi
2c190 6e 61 74 65 73 20 74 77 6f 2d 74 68 69 72 64 73  nates two-thirds
2c191 20 6f 66 20 72 6f 77 73 20 2a 2f 0a 20 20 20 20   of rows */.    
2c192 20 20 7d 0a 20 20 20 20 20 20 57 48 45 52 45 54    }.      WHERET
2c193 52 41 43 45 28 28 22 2e 2e 2e 20 72 6f 77 69 64  RACE(("... rowid
2c194 20 72 61 6e 67 65 20 72 65 64 75 63 65 73 20 63   range reduces c
2c195 6f 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20  ost to %.9g\n", 
2c196 63 6f 73 74 29 29 3b 0a 20 20 20 20 7d 65 6c 73  cost));.    }els
2c197 65 7b 0a 20 20 20 20 20 20 77 73 46 6c 61 67 73  e{.      wsFlags
2c198 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
2c199 6e 52 6f 77 20 3d 20 63 6f 73 74 3b 0a 20 20 0a  nRow = cost;.  .
2c19a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61      /* If the ta
2c19b 62 6c 65 20 73 63 61 6e 20 64 6f 65 73 20 6e 6f  ble scan does no
2c19c 74 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52  t satisfy the OR
2c19d 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 69  DER BY clause, i
2c19e 6e 63 72 65 61 73 65 0a 20 20 20 20 2a 2a 20 74  ncrease.    ** t
2c19f 68 65 20 63 6f 73 74 20 62 79 20 4e 6c 6f 67 4e  he cost by NlogN
2c1a0 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 65 78   to cover the ex
2c1a1 70 65 6e 73 65 20 6f 66 20 73 6f 72 74 69 6e 67  pense of sorting
2c1a2 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 72  . */.    if( pOr
2c1a3 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69  derBy ){.      i
2c1a4 66 28 20 73 6f 72 74 61 62 6c 65 42 79 52 6f 77  f( sortableByRow
2c1a5 69 64 28 69 43 75 72 2c 20 70 4f 72 64 65 72 42  id(iCur, pOrderB
2c1a6 79 2c 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74  y, pWC->pMaskSet
2c1a7 2c 20 26 72 65 76 29 20 29 7b 0a 20 20 20 20 20  , &rev) ){.     
2c1a8 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48     wsFlags |= WH
2c1a9 45 52 45 5f 4f 52 44 45 52 42 59 7c 57 48 45 52  ERE_ORDERBY|WHER
2c1aa 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 3b 0a 20  E_ROWID_RANGE;. 
2c1ab 20 20 20 20 20 20 20 69 66 28 20 72 65 76 20 29         if( rev )
2c1ac 7b 0a 20 20 20 20 20 20 20 20 20 20 77 73 46 6c  {.          wsFl
2c1ad 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 45 56  ags |= WHERE_REV
2c1ae 45 52 53 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ERSE;.        }.
2c1af 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2c1b0 20 20 20 20 20 63 6f 73 74 20 2b 3d 20 63 6f 73       cost += cos
2c1b1 74 2a 65 73 74 4c 6f 67 28 63 6f 73 74 29 3b 0a  t*estLog(cost);.
2c1b2 20 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41          WHERETRA
2c1b3 43 45 28 28 22 2e 2e 2e 20 73 6f 72 74 69 6e 67  CE(("... sorting
2c1b4 20 69 6e 63 72 65 61 73 65 73 20 63 6f 73 74 20   increases cost 
2c1b5 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74  to %.9g\n", cost
2c1b6 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
2c1b7 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 73 65  }else if( pParse
2c1b8 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
2c1b9 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64 65  LITE_ReverseOrde
2c1ba 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 6f  r ){.      /* Fo
2c1bb 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 65  r application te
2c1bc 73 74 69 6e 67 2c 20 72 61 6e 64 6f 6d 6c 79 20  sting, randomly 
2c1bd 72 65 76 65 72 73 65 20 74 68 65 20 6f 75 74 70  reverse the outp
2c1be 75 74 20 6f 72 64 65 72 20 66 6f 72 0a 20 20 20  ut order for.   
2c1bf 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74 61     ** SELECT sta
2c1c0 74 65 6d 65 6e 74 73 20 74 68 61 74 20 6f 6d 69  tements that omi
2c1c1 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  t the ORDER BY c
2c1c2 6c 61 75 73 65 2e 20 20 54 68 69 73 20 77 69 6c  lause.  This wil
2c1c3 6c 20 68 65 6c 70 0a 20 20 20 20 20 20 2a 2a 20  l help.      ** 
2c1c4 74 6f 20 66 69 6e 64 20 63 61 73 65 73 20 77 68  to find cases wh
2c1c5 65 72 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ere.      */.   
2c1c6 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48     wsFlags |= WH
2c1c7 45 52 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 20  ERE_REVERSE;.   
2c1c8 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 65 6d   }..    /* Remem
2c1c9 62 65 72 20 74 68 69 73 20 63 61 73 65 20 69 66  ber this case if
2c1ca 20 69 74 20 69 73 20 74 68 65 20 62 65 73 74 20   it is the best 
2c1cb 73 6f 20 66 61 72 20 2a 2f 0a 20 20 20 20 69 66  so far */.    if
2c1cc 28 20 63 6f 73 74 3c 70 43 6f 73 74 2d 3e 72 43  ( cost<pCost->rC
2c1cd 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 70 43 6f  ost ){.      pCo
2c1ce 73 74 2d 3e 72 43 6f 73 74 20 3d 20 63 6f 73 74  st->rCost = cost
2c1cf 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 6e  ;.      pCost->n
2c1d0 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20 20 20  Row = nRow;.    
2c1d1 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
2c1d2 46 6c 61 67 73 20 3d 20 77 73 46 6c 61 67 73 3b  Flags = wsFlags;
2c1d3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 65  .    }.  }..  be
2c1d4 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28  stOrClauseIndex(
2c1d5 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72  pParse, pWC, pSr
2c1d6 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72  c, notReady, pOr
2c1d7 64 65 72 42 79 2c 20 70 43 6f 73 74 29 3b 0a 0a  derBy, pCost);..
2c1d8 20 20 2f 2a 20 49 66 20 74 68 65 20 70 53 72 63    /* If the pSrc
2c1d9 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 72 69   table is the ri
2c1da 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 4c  ght table of a L
2c1db 45 46 54 20 4a 4f 49 4e 20 74 68 65 6e 20 77 65  EFT JOIN then we
2c1dc 20 6d 61 79 20 6e 6f 74 0a 20 20 2a 2a 20 75 73   may not.  ** us
2c1dd 65 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 73 61  e an index to sa
2c1de 74 69 73 66 79 20 49 53 20 4e 55 4c 4c 20 63 6f  tisfy IS NULL co
2c1df 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 74 68 61  nstraints on tha
2c1e0 74 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69  t table.  This i
2c1e1 73 0a 20 20 2a 2a 20 62 65 63 61 75 73 65 20 63  s.  ** because c
2c1e2 6f 6c 75 6d 6e 73 20 6d 69 67 68 74 20 65 6e 64  olumns might end
2c1e3 20 75 70 20 62 65 69 6e 67 20 4e 55 4c 4c 20 69   up being NULL i
2c1e4 66 20 74 68 65 20 74 61 62 6c 65 20 64 6f 65 73  f the table does
2c1e5 20 6e 6f 74 20 6d 61 74 63 68 20 2d 0a 20 20 2a   not match -.  *
2c1e6 2a 20 61 20 63 69 72 63 75 6d 73 74 61 6e 63 65  * a circumstance
2c1e7 20 77 68 69 63 68 20 74 68 65 20 69 6e 64 65 78   which the index
2c1e8 20 63 61 6e 6e 6f 74 20 68 65 6c 70 20 75 73 20   cannot help us 
2c1e9 64 69 73 63 6f 76 65 72 2e 20 20 54 69 63 6b 65  discover.  Ticke
2c1ea 74 20 23 32 31 37 37 2e 0a 20 20 2a 2f 0a 20 20  t #2177..  */.  
2c1eb 69 66 28 20 28 70 53 72 63 2d 3e 6a 6f 69 6e 74  if( (pSrc->joint
2c1ec 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d  ype & JT_LEFT)!=
2c1ed 30 20 29 7b 0a 20 20 20 20 65 71 54 65 72 6d 4d  0 ){.    eqTermM
2c1ee 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49  ask = WO_EQ|WO_I
2c1ef 4e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  N;.  }else{.    
2c1f0 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f  eqTermMask = WO_
2c1f1 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55  EQ|WO_IN|WO_ISNU
2c1f2 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f  LL;.  }..  /* Lo
2c1f3 6f 6b 20 61 74 20 65 61 63 68 20 69 6e 64 65 78  ok at each index
2c1f4 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 72  ..  */.  if( pSr
2c1f5 63 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  c->pIndex ){.   
2c1f6 20 70 50 72 6f 62 65 20 3d 20 70 53 72 63 2d 3e   pProbe = pSrc->
2c1f7 70 49 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 66 6f  pIndex;.  }.  fo
2c1f8 72 28 3b 20 70 50 72 6f 62 65 3b 20 70 50 72 6f  r(; pProbe; pPro
2c1f9 62 65 3d 28 70 53 72 63 2d 3e 70 49 6e 64 65 78  be=(pSrc->pIndex
2c1fa 20 3f 20 30 20 3a 20 70 50 72 6f 62 65 2d 3e 70   ? 0 : pProbe->p
2c1fb 4e 65 78 74 29 29 7b 0a 20 20 20 20 64 6f 75 62  Next)){.    doub
2c1fc 6c 65 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20  le inMultiplier 
2c1fd 3d 20 31 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20  = 1;  /* Number 
2c1fe 6f 66 20 65 71 75 61 6c 69 74 79 20 6c 6f 6f 6b  of equality look
2c1ff 2d 75 70 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20  -ups needed */. 
2c200 20 20 20 69 6e 74 20 69 6e 4d 75 6c 74 49 73 45     int inMultIsE
2c201 73 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  st = 0;      /* 
2c202 54 72 75 65 20 69 66 20 69 6e 4d 75 6c 74 69 70  True if inMultip
2c203 6c 69 65 72 20 69 73 20 61 6e 20 65 73 74 69 6d  lier is an estim
2c204 61 74 65 20 2a 2f 0a 0a 20 20 20 20 57 48 45 52  ate */..    WHER
2c205 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 69 6e 64  ETRACE(("... ind
2c206 65 78 20 25 73 3a 5c 6e 22 2c 20 70 50 72 6f 62  ex %s:\n", pProb
2c207 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20 20  e->zName));..   
2c208 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75   /* Count the nu
2c209 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2c20a 69 6e 20 74 68 65 20 69 6e 64 65 78 20 74 68 61  in the index tha
2c20b 74 20 61 72 65 20 73 61 74 69 73 66 69 65 64 0a  t are satisfied.
2c20c 20 20 20 20 2a 2a 20 62 79 20 78 3d 45 58 50 52      ** by x=EXPR
2c20d 20 6f 72 20 78 20 49 53 20 4e 55 4c 4c 20 63 6f   or x IS NULL co
2c20e 6e 73 74 72 61 69 6e 74 73 20 6f 72 20 78 20 49  nstraints or x I
2c20f 4e 20 28 2e 2e 2e 29 20 63 6f 6e 73 74 72 61 69  N (...) constrai
2c210 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 46 6f 72 20  nts..    ** For 
2c211 61 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f  a term of the fo
2c212 72 6d 20 78 3d 45 58 50 52 20 6f 72 20 78 20 49  rm x=EXPR or x I
2c213 53 20 4e 55 4c 4c 20 77 65 20 6f 6e 6c 79 20 68  S NULL we only h
2c214 61 76 65 20 74 6f 20 64 6f 20 0a 20 20 20 20 2a  ave to do .    *
2c215 2a 20 61 20 73 69 6e 67 6c 65 20 62 69 6e 61 72  * a single binar
2c216 79 20 73 65 61 72 63 68 2e 20 20 42 75 74 20 66  y search.  But f
2c217 6f 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20 77 65  or x IN (...) we
2c218 20 68 61 76 65 20 74 6f 20 64 6f 20 61 0a 20 20   have to do a.  
2c219 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62    ** number of b
2c21a 69 6e 61 72 79 20 73 65 61 72 63 68 65 64 0a 20  inary searched. 
2c21b 20 20 20 2a 2a 20 65 71 75 61 6c 20 74 6f 20 74     ** equal to t
2c21c 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
2c21d 72 69 65 73 20 6f 6e 20 74 68 65 20 52 48 53 20  ries on the RHS 
2c21e 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74  of the IN operat
2c21f 6f 72 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 69  or..    ** The i
2c220 6e 4d 75 6c 74 69 70 6c 65 72 20 76 61 72 69 61  nMultipler varia
2c221 62 6c 65 20 77 69 74 68 20 74 72 79 20 74 6f 20  ble with try to 
2c222 65 73 74 69 6d 61 74 65 20 74 68 65 20 6e 75 6d  estimate the num
2c223 62 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 62 69  ber of.    ** bi
2c224 6e 61 72 79 20 73 65 61 72 63 68 65 73 20 6e 65  nary searches ne
2c225 65 64 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eded..    */.   
2c226 20 77 73 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20   wsFlags = 0;.  
2c227 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 72    for(i=0; i<pPr
2c228 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  obe->nColumn; i+
2c229 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20  +){.      int j 
2c22a 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75  = pProbe->aiColu
2c22b 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 70 54 65  mn[i];.      pTe
2c22c 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  rm = findTerm(pW
2c22d 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52  C, iCur, j, notR
2c22e 65 61 64 79 2c 20 65 71 54 65 72 6d 4d 61 73 6b  eady, eqTermMask
2c22f 2c 20 70 50 72 6f 62 65 29 3b 0a 20 20 20 20 20  , pProbe);.     
2c230 20 69 66 28 20 70 54 65 72 6d 3d 3d 30 20 29 20   if( pTerm==0 ) 
2c231 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 77 73 46  break;.      wsF
2c232 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f  lags |= WHERE_CO
2c233 4c 55 4d 4e 5f 45 51 3b 0a 20 20 20 20 20 20 69  LUMN_EQ;.      i
2c234 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  f( pTerm->eOpera
2c235 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 7b 0a 20  tor & WO_IN ){. 
2c236 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78         Expr *pEx
2c237 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pr = pTerm->pExp
2c238 72 3b 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61  r;.        wsFla
2c239 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55  gs |= WHERE_COLU
2c23a 4d 4e 5f 49 4e 3b 0a 20 20 20 20 20 20 20 20 69  MN_IN;.        i
2c23b 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
2c23c 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
2c23d 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20  Select) ){.     
2c23e 20 20 20 20 20 69 6e 4d 75 6c 74 69 70 6c 69 65       inMultiplie
2c23f 72 20 2a 3d 20 32 35 3b 0a 20 20 20 20 20 20 20  r *= 25;.       
2c240 20 20 20 69 6e 4d 75 6c 74 49 73 45 73 74 20 3d     inMultIsEst =
2c241 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   1;.        }els
2c242 65 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70  e if( pExpr->x.p
2c243 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  List ){.        
2c244 20 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20 2a    inMultiplier *
2c245 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2c246 2d 3e 6e 45 78 70 72 20 2b 20 31 3b 0a 20 20 20  ->nExpr + 1;.   
2c247 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
2c248 73 65 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f  se if( pTerm->eO
2c249 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 53 4e  perator & WO_ISN
2c24a 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 77  ULL ){.        w
2c24b 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f  sFlags |= WHERE_
2c24c 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 3b 0a 20 20 20  COLUMN_NULL;.   
2c24d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6e     }.    }.    n
2c24e 52 6f 77 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69  Row = pProbe->ai
2c24f 52 6f 77 45 73 74 5b 69 5d 20 2a 20 69 6e 4d 75  RowEst[i] * inMu
2c250 6c 74 69 70 6c 69 65 72 3b 0a 20 20 20 20 2f 2a  ltiplier;.    /*
2c251 20 49 66 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72   If inMultiplier
2c252 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20   is an estimate 
2c253 61 6e 64 20 74 68 61 74 20 65 73 74 69 6d 61 74  and that estimat
2c254 65 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a  e results in an.
2c255 20 20 20 20 2a 2a 20 6e 52 6f 77 20 69 74 20 74      ** nRow it t
2c256 68 61 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e  hat is more than
2c257 20 68 61 6c 66 20 6e 75 6d 62 65 72 20 6f 66 20   half number of 
2c258 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c  rows in the tabl
2c259 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 72  e,.    ** then r
2c25a 65 64 75 63 65 20 69 6e 4d 75 6c 74 69 70 6c 65  educe inMultiple
2c25b 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 6e 4d  r */.    if( inM
2c25c 75 6c 74 49 73 45 73 74 20 26 26 20 6e 52 6f 77  ultIsEst && nRow
2c25d 2a 32 20 3e 20 70 50 72 6f 62 65 2d 3e 61 69 52  *2 > pProbe->aiR
2c25e 6f 77 45 73 74 5b 30 5d 20 29 7b 0a 20 20 20 20  owEst[0] ){.    
2c25f 20 20 6e 52 6f 77 20 3d 20 70 50 72 6f 62 65 2d    nRow = pProbe-
2c260 3e 61 69 52 6f 77 45 73 74 5b 30 5d 2f 32 3b 0a  >aiRowEst[0]/2;.
2c261 20 20 20 20 20 20 69 6e 4d 75 6c 74 69 70 6c 69        inMultipli
2c262 65 72 20 3d 20 6e 52 6f 77 2f 70 50 72 6f 62 65  er = nRow/pProbe
2c263 2d 3e 61 69 52 6f 77 45 73 74 5b 69 5d 3b 0a 20  ->aiRowEst[i];. 
2c264 20 20 20 7d 0a 20 20 20 20 63 6f 73 74 20 3d 20     }.    cost = 
2c265 6e 52 6f 77 20 2b 20 69 6e 4d 75 6c 74 69 70 6c  nRow + inMultipl
2c266 69 65 72 2a 65 73 74 4c 6f 67 28 70 50 72 6f 62  ier*estLog(pProb
2c267 65 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 29 3b  e->aiRowEst[0]);
2c268 0a 20 20 20 20 6e 45 71 20 3d 20 69 3b 0a 20 20  .    nEq = i;.  
2c269 20 20 69 66 28 20 70 50 72 6f 62 65 2d 3e 6f 6e    if( pProbe->on
2c26a 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 26  Error!=OE_None &
2c26b 26 20 6e 45 71 3d 3d 70 50 72 6f 62 65 2d 3e 6e  & nEq==pProbe->n
2c26c 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20  Column ){.      
2c26d 74 65 73 74 63 61 73 65 28 20 77 73 46 6c 61 67  testcase( wsFlag
2c26e 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  s & WHERE_COLUMN
2c26f 5f 49 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _IN );.      tes
2c270 74 63 61 73 65 28 20 77 73 46 6c 61 67 73 20 26  tcase( wsFlags &
2c271 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55   WHERE_COLUMN_NU
2c272 4c 4c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  LL );.      if( 
2c273 28 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52  (wsFlags & (WHER
2c274 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57 48 45 52  E_COLUMN_IN|WHER
2c275 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 29 29 3d  E_COLUMN_NULL))=
2c276 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 77 73  =0 ){.        ws
2c277 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 55  Flags |= WHERE_U
2c278 4e 49 51 55 45 3b 0a 20 20 20 20 20 20 7d 0a 20  NIQUE;.      }. 
2c279 20 20 20 7d 0a 20 20 20 20 57 48 45 52 45 54 52     }.    WHERETR
2c27a 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20 6e 45 71  ACE(("...... nEq
2c27b 3d 25 64 20 69 6e 4d 75 6c 74 3d 25 2e 39 67 20  =%d inMult=%.9g 
2c27c 6e 52 6f 77 3d 25 2e 39 67 20 63 6f 73 74 3d 25  nRow=%.9g cost=%
2c27d 2e 39 67 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  .9g\n",.        
2c27e 20 20 20 20 20 20 20 20 6e 45 71 2c 20 69 6e 4d          nEq, inM
2c27f 75 6c 74 69 70 6c 69 65 72 2c 20 6e 52 6f 77 2c  ultiplier, nRow,
2c280 20 63 6f 73 74 29 29 3b 0a 0a 20 20 20 20 2f 2a   cost));..    /*
2c281 20 4c 6f 6f 6b 20 66 6f 72 20 72 61 6e 67 65 20   Look for range 
2c282 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 41 73  constraints.  As
2c283 73 75 6d 65 20 74 68 61 74 20 65 61 63 68 20 72  sume that each r
2c284 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a  ange constraint.
2c285 20 20 20 20 2a 2a 20 6d 61 6b 65 73 20 74 68 65      ** makes the
2c286 20 73 65 61 72 63 68 20 73 70 61 63 65 20 31 2f   search space 1/
2c287 33 72 64 20 73 6d 61 6c 6c 65 72 2e 0a 20 20 20  3rd smaller..   
2c288 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 45 71 3c   */.    if( nEq<
2c289 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20  pProbe->nColumn 
2c28a 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d  ){.      int j =
2c28b 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d   pProbe->aiColum
2c28c 6e 5b 6e 45 71 5d 3b 0a 20 20 20 20 20 20 70 54  n[nEq];.      pT
2c28d 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  erm = findTerm(p
2c28e 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74  WC, iCur, j, not
2c28f 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f  Ready, WO_LT|WO_
2c290 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20  LE|WO_GT|WO_GE, 
2c291 70 50 72 6f 62 65 29 3b 0a 20 20 20 20 20 20 69  pProbe);.      i
2c292 66 28 20 70 54 65 72 6d 20 29 7b 0a 20 20 20 20  f( pTerm ){.    
2c293 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57      wsFlags |= W
2c294 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47  HERE_COLUMN_RANG
2c295 45 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  E;.        if( f
2c296 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
2c297 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20  r, j, notReady, 
2c298 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 70 50 72  WO_LT|WO_LE, pPr
2c299 6f 62 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  obe) ){.        
2c29a 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2c29b 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20  RE_TOP_LIMIT;.  
2c29c 20 20 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20          cost /= 
2c29d 33 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 52 6f  3;.          nRo
2c29e 77 20 2f 3d 20 33 3b 0a 20 20 20 20 20 20 20 20  w /= 3;.        
2c29f 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 69  }.        if( fi
2c2a0 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
2c2a1 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57  , j, notReady, W
2c2a2 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 70 50 72 6f  O_GT|WO_GE, pPro
2c2a3 62 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  be) ){.         
2c2a4 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2c2a5 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20 20  E_BTM_LIMIT;.   
2c2a6 20 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20 33         cost /= 3
2c2a7 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 52 6f 77  ;.          nRow
2c2a8 20 2f 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 7d   /= 3;.        }
2c2a9 0a 20 20 20 20 20 20 20 20 57 48 45 52 45 54 52  .        WHERETR
2c2aa 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20 72 61 6e  ACE(("...... ran
2c2ab 67 65 20 72 65 64 75 63 65 73 20 6e 52 6f 77 20  ge reduces nRow 
2c2ac 74 6f 20 25 2e 39 67 20 61 6e 64 20 63 6f 73 74  to %.9g and cost
2c2ad 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 0a 20 20 20   to %.9g\n",.   
2c2ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2af 20 6e 52 6f 77 2c 20 63 6f 73 74 29 29 3b 0a 20   nRow, cost));. 
2c2b0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
2c2b1 20 20 2f 2a 20 41 64 64 20 74 68 65 20 61 64 64    /* Add the add
2c2b2 69 74 69 6f 6e 61 6c 20 63 6f 73 74 20 6f 66 20  itional cost of 
2c2b3 73 6f 72 74 69 6e 67 20 69 66 20 74 68 61 74 20  sorting if that 
2c2b4 69 73 20 61 20 66 61 63 74 6f 72 2e 0a 20 20 20  is a factor..   
2c2b5 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 72 64   */.    if( pOrd
2c2b6 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69 66  erBy ){.      if
2c2b7 28 20 28 77 73 46 6c 61 67 73 20 26 20 28 57 48  ( (wsFlags & (WH
2c2b8 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57 48  ERE_COLUMN_IN|WH
2c2b9 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 29  ERE_COLUMN_NULL)
2c2ba 29 3d 3d 30 0a 20 20 20 20 20 20 20 26 26 20 69  )==0.       && i
2c2bb 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 70 50  sSortingIndex(pP
2c2bc 61 72 73 65 2c 70 57 43 2d 3e 70 4d 61 73 6b 53  arse,pWC->pMaskS
2c2bd 65 74 2c 70 50 72 6f 62 65 2c 69 43 75 72 2c 70  et,pProbe,iCur,p
2c2be 4f 72 64 65 72 42 79 2c 6e 45 71 2c 26 72 65 76  OrderBy,nEq,&rev
2c2bf 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  ).      ){.     
2c2c0 20 20 20 69 66 28 20 77 73 46 6c 61 67 73 3d 3d     if( wsFlags==
2c2c1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77  0 ){.          w
2c2c2 73 46 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 43  sFlags = WHERE_C
2c2c3 4f 4c 55 4d 4e 5f 52 41 4e 47 45 3b 0a 20 20 20  OLUMN_RANGE;.   
2c2c4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77       }.        w
2c2c5 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f  sFlags |= WHERE_
2c2c6 4f 52 44 45 52 42 59 3b 0a 20 20 20 20 20 20 20  ORDERBY;.       
2c2c7 20 69 66 28 20 72 65 76 20 29 7b 0a 20 20 20 20   if( rev ){.    
2c2c8 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2c2c9 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 3b 0a   WHERE_REVERSE;.
2c2ca 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2c2cb 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
2c2cc 6f 73 74 20 2b 3d 20 63 6f 73 74 2a 65 73 74 4c  ost += cost*estL
2c2cd 6f 67 28 63 6f 73 74 29 3b 0a 20 20 20 20 20 20  og(cost);.      
2c2ce 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2e    WHERETRACE((".
2c2cf 2e 2e 2e 2e 2e 20 6f 72 64 65 72 62 79 20 69 6e  ..... orderby in
2c2d0 63 72 65 61 73 65 73 20 63 6f 73 74 20 74 6f 20  creases cost to 
2c2d1 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b  %.9g\n", cost));
2c2d2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
2c2d3 73 65 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64  se if( pParse->d
2c2d4 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2c2d5 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 20 29  E_ReverseOrder )
2c2d6 7b 0a 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61  {.      /* For a
2c2d7 70 70 6c 69 63 61 74 69 6f 6e 20 74 65 73 74 69  pplication testi
2c2d8 6e 67 2c 20 72 61 6e 64 6f 6d 6c 79 20 72 65 76  ng, randomly rev
2c2d9 65 72 73 65 20 74 68 65 20 6f 75 74 70 75 74 20  erse the output 
2c2da 6f 72 64 65 72 20 66 6f 72 0a 20 20 20 20 20 20  order for.      
2c2db 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
2c2dc 65 6e 74 73 20 74 68 61 74 20 6f 6d 69 74 20 74  ents that omit t
2c2dd 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2c2de 73 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 68  se.  This will h
2c2df 65 6c 70 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  elp.      ** to 
2c2e0 66 69 6e 64 20 63 61 73 65 73 20 77 68 65 72 65  find cases where
2c2e1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2c2e2 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45  wsFlags |= WHERE
2c2e3 5f 52 45 56 45 52 53 45 3b 0a 20 20 20 20 7d 0a  _REVERSE;.    }.
2c2e4 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  .    /* Check to
2c2e5 20 73 65 65 20 69 66 20 77 65 20 63 61 6e 20 67   see if we can g
2c2e6 65 74 20 61 77 61 79 20 77 69 74 68 20 75 73 69  et away with usi
2c2e7 6e 67 20 6a 75 73 74 20 74 68 65 20 69 6e 64 65  ng just the inde
2c2e8 78 20 77 69 74 68 6f 75 74 0a 20 20 20 20 2a 2a  x without.    **
2c2e9 20 65 76 65 72 20 72 65 61 64 69 6e 67 20 74 68   ever reading th
2c2ea 65 20 74 61 62 6c 65 2e 20 20 49 66 20 74 68 61  e table.  If tha
2c2eb 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  t is the case, t
2c2ec 68 65 6e 20 68 61 6c 76 65 20 74 68 65 0a 20 20  hen halve the.  
2c2ed 20 20 2a 2a 20 63 6f 73 74 20 6f 66 20 74 68 69    ** cost of thi
2c2ee 73 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a  s index..    */.
2c2ef 20 20 20 20 69 66 28 20 77 73 46 6c 61 67 73 20      if( wsFlags 
2c2f0 26 26 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65 64  && pSrc->colUsed
2c2f1 20 3c 20 28 28 28 42 69 74 6d 61 73 6b 29 31 29   < (((Bitmask)1)
2c2f2 3c 3c 28 42 4d 53 2d 31 29 29 20 29 7b 0a 20 20  <<(BMS-1)) ){.  
2c2f3 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 20 3d 20      Bitmask m = 
2c2f4 70 53 72 63 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20  pSrc->colUsed;. 
2c2f5 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2c2f6 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 72    for(j=0; j<pPr
2c2f7 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b  obe->nColumn; j+
2c2f8 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  +){.        int 
2c2f9 78 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f  x = pProbe->aiCo
2c2fa 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 20 20 20  lumn[j];.       
2c2fb 20 69 66 28 20 78 3c 42 4d 53 2d 31 20 29 7b 0a   if( x<BMS-1 ){.
2c2fc 20 20 20 20 20 20 20 20 20 20 6d 20 26 3d 20 7e            m &= ~
2c2fd 28 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 78  (((Bitmask)1)<<x
2c2fe 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2c2ff 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6d     }.      if( m
2c300 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 77  ==0 ){.        w
2c301 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f  sFlags |= WHERE_
2c302 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20 20 20 20 20  IDX_ONLY;.      
2c303 20 20 63 6f 73 74 20 2f 3d 20 32 3b 0a 20 20 20    cost /= 2;.   
2c304 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
2c305 28 22 2e 2e 2e 2e 2e 2e 20 69 64 78 2d 6f 6e 6c  ("...... idx-onl
2c306 79 20 72 65 64 75 63 65 73 20 63 6f 73 74 20 74  y reduces cost t
2c307 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29  o %.9g\n", cost)
2c308 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2c309 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  ..    /* If this
2c30a 20 69 6e 64 65 78 20 68 61 73 20 61 63 68 69 65   index has achie
2c30b 76 65 64 20 74 68 65 20 6c 6f 77 65 73 74 20 63  ved the lowest c
2c30c 6f 73 74 20 73 6f 20 66 61 72 2c 20 74 68 65 6e  ost so far, then
2c30d 20 75 73 65 20 69 74 2e 0a 20 20 20 20 2a 2f 0a   use it..    */.
2c30e 20 20 20 20 69 66 28 20 77 73 46 6c 61 67 73 21      if( wsFlags!
2c30f 3d 30 20 26 26 20 63 6f 73 74 20 3c 20 70 43 6f  =0 && cost < pCo
2c310 73 74 2d 3e 72 43 6f 73 74 20 29 7b 0a 20 20 20  st->rCost ){.   
2c311 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20     pCost->rCost 
2c312 3d 20 63 6f 73 74 3b 0a 20 20 20 20 20 20 70 43  = cost;.      pC
2c313 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77  ost->nRow = nRow
2c314 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70  ;.      pCost->p
2c315 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 77 73  lan.wsFlags = ws
2c316 46 6c 61 67 73 3b 0a 20 20 20 20 20 20 70 43 6f  Flags;.      pCo
2c317 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20 6e  st->plan.nEq = n
2c318 45 71 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  Eq;.      assert
2c319 28 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73  ( pCost->plan.ws
2c31a 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
2c31b 44 45 58 45 44 20 29 3b 0a 20 20 20 20 20 20 70  DEXED );.      p
2c31c 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64  Cost->plan.u.pId
2c31d 78 20 3d 20 70 50 72 6f 62 65 3b 0a 20 20 20 20  x = pProbe;.    
2c31e 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 70 6f  }.  }..  /* Repo
2c31f 72 74 20 74 68 65 20 62 65 73 74 20 72 65 73 75  rt the best resu
2c320 6c 74 0a 20 20 2a 2f 0a 20 20 70 43 6f 73 74 2d  lt.  */.  pCost-
2c321 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c 3d  >plan.wsFlags |=
2c322 20 65 71 54 65 72 6d 4d 61 73 6b 3b 0a 20 20 57   eqTermMask;.  W
2c323 48 45 52 45 54 52 41 43 45 28 28 22 62 65 73 74  HERETRACE(("best
2c324 20 69 6e 64 65 78 20 69 73 20 25 73 2c 20 63 6f   index is %s, co
2c325 73 74 3d 25 2e 39 67 2c 20 6e 72 6f 77 3d 25 2e  st=%.9g, nrow=%.
2c326 39 67 2c 20 77 73 46 6c 61 67 73 3d 25 78 2c 20  9g, wsFlags=%x, 
2c327 6e 45 71 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20  nEq=%d\n",.     
2c328 20 20 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e     (pCost->plan.
2c329 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2c32a 49 4e 44 45 58 45 44 29 21 3d 30 20 3f 0a 20 20  INDEXED)!=0 ?.  
2c32b 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 73 74             pCost
2c32c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a  ->plan.u.pIdx->z
2c32d 4e 61 6d 65 20 3a 20 22 28 6e 6f 6e 65 29 22 2c  Name : "(none)",
2c32e 20 70 43 6f 73 74 2d 3e 6e 52 6f 77 2c 0a 20 20   pCost->nRow,.  
2c32f 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f        pCost->rCo
2c330 73 74 2c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e  st, pCost->plan.
2c331 77 73 46 6c 61 67 73 2c 20 70 43 6f 73 74 2d 3e  wsFlags, pCost->
2c332 70 6c 61 6e 2e 6e 45 71 29 29 3b 0a 7d 0a 0a 2f  plan.nEq));.}../
2c333 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 71 75  *.** Find the qu
2c334 65 72 79 20 70 6c 61 6e 20 66 6f 72 20 61 63 63  ery plan for acc
2c335 65 73 73 69 6e 67 20 74 61 62 6c 65 20 70 53 72  essing table pSr
2c336 63 2d 3e 70 54 61 62 2e 20 57 72 69 74 65 20 74  c->pTab. Write t
2c337 68 65 0a 2a 2a 20 62 65 73 74 20 71 75 65 72 79  he.** best query
2c338 20 70 6c 61 6e 20 61 6e 64 20 69 74 73 20 63 6f   plan and its co
2c339 73 74 20 69 6e 74 6f 20 74 68 65 20 57 68 65 72  st into the Wher
2c33a 65 43 6f 73 74 20 6f 62 6a 65 63 74 20 73 75 70  eCost object sup
2c33b 70 6c 69 65 64 20 0a 2a 2a 20 61 73 20 74 68 65  plied .** as the
2c33c 20 6c 61 73 74 20 70 61 72 61 6d 65 74 65 72 2e   last parameter.
2c33d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
2c33e 61 79 20 63 61 6c 63 75 6c 61 74 65 20 74 68 65  ay calculate the
2c33f 20 63 6f 73 74 20 6f 66 0a 2a 2a 20 62 6f 74 68   cost of.** both
2c340 20 72 65 61 6c 20 61 6e 64 20 76 69 72 74 75 61   real and virtua
2c341 6c 20 74 61 62 6c 65 20 73 63 61 6e 73 2e 0a 2a  l table scans..*
2c342 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65  /.static void be
2c343 73 74 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65  stIndex(.  Parse
2c344 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2c345 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
2c346 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2c347 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2c348 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 2f  pWC,           /
2c349 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
2c34a 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  se */.  struct S
2c34b 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72  rcList_item *pSr
2c34c 63 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20  c,  /* The FROM 
2c34d 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73  clause term to s
2c34e 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61  earch */.  Bitma
2c34f 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20  sk notReady,    
2c350 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
2c351 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 61  f cursors that a
2c352 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  re not available
2c353 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2c354 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20  pOrderBy,       
2c355 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
2c356 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68  Y clause */.  Wh
2c357 65 72 65 43 6f 73 74 20 2a 70 43 6f 73 74 20 20  ereCost *pCost  
2c358 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77            /* Low
2c359 65 73 74 20 63 6f 73 74 20 71 75 65 72 79 20 70  est cost query p
2c35a 6c 61 6e 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  lan */.){.  if( 
2c35b 49 73 56 69 72 74 75 61 6c 28 70 53 72 63 2d 3e  IsVirtual(pSrc->
2c35c 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c  pTab) ){.    sql
2c35d 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2c35e 2a 70 20 3d 20 30 3b 0a 20 20 20 20 62 65 73 74  *p = 0;.    best
2c35f 56 69 72 74 75 61 6c 49 6e 64 65 78 28 70 50 61  VirtualIndex(pPa
2c360 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20  rse, pWC, pSrc, 
2c361 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72  notReady, pOrder
2c362 42 79 2c 20 70 43 6f 73 74 2c 20 26 70 29 3b 0a  By, pCost, &p);.
2c363 20 20 20 20 69 66 28 20 70 2d 3e 6e 65 65 64 54      if( p->needT
2c364 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b 0a 20  oFreeIdxStr ){. 
2c365 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
2c366 65 28 70 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20  e(p->idxStr);.  
2c367 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
2c368 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62  bFree(pParse->db
2c369 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  , p);.  }else{. 
2c36a 20 20 20 62 65 73 74 42 74 72 65 65 49 6e 64 65     bestBtreeInde
2c36b 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70  x(pParse, pWC, p
2c36c 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70  Src, notReady, p
2c36d 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74 29 3b  OrderBy, pCost);
2c36e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69  .  }.}../*.** Di
2c36f 73 61 62 6c 65 20 61 20 74 65 72 6d 20 69 6e 20  sable a term in 
2c370 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2c371 2e 20 20 45 78 63 65 70 74 2c 20 64 6f 20 6e 6f  .  Except, do no
2c372 74 20 64 69 73 61 62 6c 65 20 74 68 65 20 74 65  t disable the te
2c373 72 6d 0a 2a 2a 20 69 66 20 69 74 20 63 6f 6e 74  rm.** if it cont
2c374 72 6f 6c 73 20 61 20 4c 45 46 54 20 4f 55 54 45  rols a LEFT OUTE
2c375 52 20 4a 4f 49 4e 20 61 6e 64 20 69 74 20 64 69  R JOIN and it di
2c376 64 20 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65 20  d not originate 
2c377 69 6e 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20  in the ON.** or 
2c378 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20  USING clause of 
2c379 74 68 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a  that join..**.**
2c37a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 74 65   Consider the te
2c37b 72 6d 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20  rm t2.z='ok' in 
2c37c 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75  the following qu
2c37d 65 72 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28  eries:.**.**   (
2c37e 31 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  1)  SELECT * FRO
2c37f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74  M t1 LEFT JOIN t
2c380 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57  2 ON t1.a=t2.x W
2c381 48 45 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a  HERE t2.z='ok'.*
2c382 2a 20 20 20 28 32 29 20 20 53 45 4c 45 43 54 20  *   (2)  SELECT 
2c383 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a  * FROM t1 LEFT J
2c384 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74  OIN t2 ON t1.a=t
2c385 32 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b  2.x AND t2.z='ok
2c386 27 0a 2a 2a 20 20 20 28 33 29 20 20 53 45 4c 45  '.**   (3)  SELE
2c387 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32  CT * FROM t1, t2
2c388 20 57 48 45 52 45 20 74 31 2e 61 3d 74 32 2e 78   WHERE t1.a=t2.x
2c389 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a   AND t2.z='ok'.*
2c38a 2a 0a 2a 2a 20 54 68 65 20 74 32 2e 7a 3d 27 6f  *.** The t2.z='o
2c38b 6b 27 20 69 73 20 64 69 73 61 62 6c 65 64 20 69  k' is disabled i
2c38c 6e 20 74 68 65 20 69 6e 20 28 32 29 20 62 65 63  n the in (2) bec
2c38d 61 75 73 65 20 69 74 20 6f 72 69 67 69 6e 61 74  ause it originat
2c38e 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20  es.** in the ON 
2c38f 63 6c 61 75 73 65 2e 20 20 54 68 65 20 74 65 72  clause.  The ter
2c390 6d 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e  m is disabled in
2c391 20 28 33 29 20 62 65 63 61 75 73 65 20 69 74 20   (3) because it 
2c392 69 73 20 6e 6f 74 20 70 61 72 74 0a 2a 2a 20 6f  is not part.** o
2c393 66 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a  f a LEFT OUTER J
2c394 4f 49 4e 2e 20 20 49 6e 20 28 31 29 2c 20 74 68  OIN.  In (1), th
2c395 65 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 69  e term is not di
2c396 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 44 69  sabled..**.** Di
2c397 73 61 62 6c 69 6e 67 20 61 20 74 65 72 6d 20 63  sabling a term c
2c398 61 75 73 65 73 20 74 68 61 74 20 74 65 72 6d 20  auses that term 
2c399 74 6f 20 6e 6f 74 20 62 65 20 74 65 73 74 65 64  to not be tested
2c39a 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f   in the inner lo
2c39b 6f 70 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69  op.** of the joi
2c39c 6e 2e 20 20 44 69 73 61 62 6c 69 6e 67 20 69 73  n.  Disabling is
2c39d 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
2c39e 2e 20 20 57 68 65 6e 20 74 65 72 6d 73 20 61 72  .  When terms ar
2c39f 65 20 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62  e satisfied.** b
2c3a0 79 20 69 6e 64 69 63 65 73 2c 20 77 65 20 64 69  y indices, we di
2c3a1 73 61 62 6c 65 20 74 68 65 6d 20 74 6f 20 70 72  sable them to pr
2c3a2 65 76 65 6e 74 20 72 65 64 75 6e 64 61 6e 74 20  event redundant 
2c3a3 74 65 73 74 73 20 69 6e 20 74 68 65 20 69 6e 6e  tests in the inn
2c3a4 65 72 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20  er.** loop.  We 
2c3a5 77 6f 75 6c 64 20 67 65 74 20 74 68 65 20 63 6f  would get the co
2c3a6 72 72 65 63 74 20 72 65 73 75 6c 74 73 20 69 66  rrect results if
2c3a7 20 6e 6f 74 68 69 6e 67 20 77 65 72 65 20 65 76   nothing were ev
2c3a8 65 72 20 64 69 73 61 62 6c 65 64 2c 0a 2a 2a 20  er disabled,.** 
2c3a9 62 75 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20  but joins might 
2c3aa 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f  run a little slo
2c3ab 77 65 72 2e 20 20 54 68 65 20 74 72 69 63 6b 20  wer.  The trick 
2c3ac 69 73 20 74 6f 20 64 69 73 61 62 6c 65 20 61 73  is to disable as
2c3ad 20 6d 75 63 68 0a 2a 2a 20 61 73 20 77 65 20 63   much.** as we c
2c3ae 61 6e 20 77 69 74 68 6f 75 74 20 64 69 73 61 62  an without disab
2c3af 6c 69 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20  ling too much.  
2c3b0 49 66 20 77 65 20 64 69 73 61 62 6c 65 64 20 69  If we disabled i
2c3b1 6e 20 28 31 29 2c 20 77 65 27 64 20 67 65 74 0a  n (1), we'd get.
2c3b2 2a 2a 20 74 68 65 20 77 72 6f 6e 67 20 61 6e 73  ** the wrong ans
2c3b3 77 65 72 2e 20 20 53 65 65 20 74 69 63 6b 65 74  wer.  See ticket
2c3b4 20 23 38 31 33 2e 0a 2a 2f 0a 73 74 61 74 69 63   #813..*/.static
2c3b5 20 76 6f 69 64 20 64 69 73 61 62 6c 65 54 65 72   void disableTer
2c3b6 6d 28 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c  m(WhereLevel *pL
2c3b7 65 76 65 6c 2c 20 57 68 65 72 65 54 65 72 6d 20  evel, WhereTerm 
2c3b8 2a 70 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70  *pTerm){.  if( p
2c3b9 54 65 72 6d 0a 20 20 20 20 20 20 26 26 20 41 4c  Term.      && AL
2c3ba 57 41 59 53 28 28 70 54 65 72 6d 2d 3e 77 74 46  WAYS((pTerm->wtF
2c3bb 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45  lags & TERM_CODE
2c3bc 44 29 3d 3d 30 29 0a 20 20 20 20 20 20 26 26 20  D)==0).      && 
2c3bd 28 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f  (pLevel->iLeftJo
2c3be 69 6e 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61 73  in==0 || ExprHas
2c3bf 50 72 6f 70 65 72 74 79 28 70 54 65 72 6d 2d 3e  Property(pTerm->
2c3c0 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f  pExpr, EP_FromJo
2c3c1 69 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 54  in)).  ){.    pT
2c3c2 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20  erm->wtFlags |= 
2c3c3 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20  TERM_CODED;.    
2c3c4 69 66 28 20 70 54 65 72 6d 2d 3e 69 50 61 72 65  if( pTerm->iPare
2c3c5 6e 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 57  nt>=0 ){.      W
2c3c6 68 65 72 65 54 65 72 6d 20 2a 70 4f 74 68 65 72  hereTerm *pOther
2c3c7 20 3d 20 26 70 54 65 72 6d 2d 3e 70 57 43 2d 3e   = &pTerm->pWC->
2c3c8 61 5b 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74  a[pTerm->iParent
2c3c9 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 28 2d 2d  ];.      if( (--
2c3ca 70 4f 74 68 65 72 2d 3e 6e 43 68 69 6c 64 29 3d  pOther->nChild)=
2c3cb 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69  =0 ){.        di
2c3cc 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
2c3cd 2c 20 70 4f 74 68 65 72 29 3b 0a 20 20 20 20 20  , pOther);.     
2c3ce 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
2c3cf 2f 2a 0a 2a 2a 20 41 70 70 6c 79 20 74 68 65 20  /*.** Apply the 
2c3d0 61 66 66 69 6e 69 74 69 65 73 20 61 73 73 6f 63  affinities assoc
2c3d1 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 66  iated with the f
2c3d2 69 72 73 74 20 6e 20 63 6f 6c 75 6d 6e 73 20 6f  irst n columns o
2c3d3 66 20 69 6e 64 65 78 0a 2a 2a 20 70 49 64 78 20  f index.** pIdx 
2c3d4 74 6f 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  to the values in
2c3d5 20 74 68 65 20 6e 20 72 65 67 69 73 74 65 72 73   the n registers
2c3d6 20 73 74 61 72 74 69 6e 67 20 61 74 20 62 61 73   starting at bas
2c3d7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
2c3d8 64 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e  d codeApplyAffin
2c3d9 69 74 79 28 50 61 72 73 65 20 2a 70 50 61 72 73  ity(Parse *pPars
2c3da 65 2c 20 69 6e 74 20 62 61 73 65 2c 20 69 6e 74  e, int base, int
2c3db 20 6e 2c 20 49 6e 64 65 78 20 2a 70 49 64 78 29   n, Index *pIdx)
2c3dc 7b 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b 0a 20  {.  if( n>0 ){. 
2c3dd 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61     Vdbe *v = pPa
2c3de 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20  rse->pVdbe;.    
2c3df 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a  assert( v!=0 );.
2c3e0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c3e1 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66 66 69  ddOp2(v, OP_Affi
2c3e2 6e 69 74 79 2c 20 62 61 73 65 2c 20 6e 29 3b 0a  nity, base, n);.
2c3e3 20 20 20 20 73 71 6c 69 74 65 33 49 6e 64 65 78      sqlite3Index
2c3e4 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
2c3e5 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Idx);.    sqlite
2c3e6 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
2c3e7 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
2c3e8 20 62 61 73 65 2c 20 6e 29 3b 0a 20 20 7d 0a 7d   base, n);.  }.}
2c3e9 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  .../*.** Generat
2c3ea 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e  e code for a sin
2c3eb 67 6c 65 20 65 71 75 61 6c 69 74 79 20 74 65 72  gle equality ter
2c3ec 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  m of the WHERE c
2c3ed 6c 61 75 73 65 2e 20 20 41 6e 20 65 71 75 61 6c  lause.  An equal
2c3ee 69 74 79 0a 2a 2a 20 74 65 72 6d 20 63 61 6e 20  ity.** term can 
2c3ef 62 65 20 65 69 74 68 65 72 20 58 3d 65 78 70 72  be either X=expr
2c3f0 20 6f 72 20 58 20 49 4e 20 28 2e 2e 2e 29 2e 20   or X IN (...). 
2c3f1 20 20 70 54 65 72 6d 20 69 73 20 74 68 65 20 74    pTerm is the t
2c3f2 65 72 6d 20 74 6f 20 62 65 20 0a 2a 2a 20 63 6f  erm to be .** co
2c3f3 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ded..**.** The c
2c3f4 75 72 72 65 6e 74 20 76 61 6c 75 65 20 66 6f 72  urrent value for
2c3f5 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
2c3f6 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67 69 73  is left in regis
2c3f7 74 65 72 20 69 52 65 67 2e 0a 2a 2a 0a 2a 2a 20  ter iReg..**.** 
2c3f8 46 6f 72 20 61 20 63 6f 6e 73 74 72 61 69 6e 74  For a constraint
2c3f9 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 3d 65   of the form X=e
2c3fa 78 70 72 2c 20 74 68 65 20 65 78 70 72 65 73 73  xpr, the express
2c3fb 69 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65 64  ion is evaluated
2c3fc 20 61 6e 64 20 69 74 73 0a 2a 2a 20 72 65 73 75   and its.** resu
2c3fd 6c 74 20 69 73 20 6c 65 66 74 20 6f 6e 20 74 68  lt is left on th
2c3fe 65 20 73 74 61 63 6b 2e 20 20 46 6f 72 20 63 6f  e stack.  For co
2c3ff 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65  nstraints of the
2c400 20 66 6f 72 6d 20 58 20 49 4e 20 28 2e 2e 2e 29   form X IN (...)
2c401 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
2c402 20 73 65 74 73 20 75 70 20 61 20 6c 6f 6f 70 20   sets up a loop 
2c403 74 68 61 74 20 77 69 6c 6c 20 69 74 65 72 61 74  that will iterat
2c404 65 20 6f 76 65 72 20 61 6c 6c 20 76 61 6c 75 65  e over all value
2c405 73 20 6f 66 20 58 2e 0a 2a 2f 0a 73 74 61 74 69  s of X..*/.stati
2c406 63 20 69 6e 74 20 63 6f 64 65 45 71 75 61 6c 69  c int codeEquali
2c407 74 79 54 65 72 6d 28 0a 20 20 50 61 72 73 65 20  tyTerm(.  Parse 
2c408 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
2c409 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e   The parsing con
2c40a 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 54  text */.  WhereT
2c40b 65 72 6d 20 2a 70 54 65 72 6d 2c 20 20 20 2f 2a  erm *pTerm,   /*
2c40c 20 54 68 65 20 74 65 72 6d 20 6f 66 20 74 68 65   The term of the
2c40d 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f   WHERE clause to
2c40e 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 57   be coded */.  W
2c40f 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65  hereLevel *pLeve
2c410 6c 2c 20 2f 2a 20 57 68 65 6e 20 6c 65 76 65 6c  l, /* When level
2c411 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
2c412 75 73 65 20 77 65 20 61 72 65 20 77 6f 72 6b 69  use we are worki
2c413 6e 67 20 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ng on */.  int i
2c414 54 61 72 67 65 74 20 20 20 20 20 20 20 20 20 2f  Target         /
2c415 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 65 61  * Attempt to lea
2c416 76 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68  ve results in th
2c417 69 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 29  is register */.)
2c418 7b 0a 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70  {.  Expr *pX = p
2c419 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 56  Term->pExpr;.  V
2c41a 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
2c41b 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 52  >pVdbe;.  int iR
2c41c 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  eg;             
2c41d 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
2c41e 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74 73   holding results
2c41f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69   */..  assert( i
2c420 54 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 69 66  Target>0 );.  if
2c421 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20  ( pX->op==TK_EQ 
2c422 29 7b 0a 20 20 20 20 69 52 65 67 20 3d 20 73 71  ){.    iReg = sq
2c423 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72  lite3ExprCodeTar
2c424 67 65 74 28 70 50 61 72 73 65 2c 20 70 58 2d 3e  get(pParse, pX->
2c425 70 52 69 67 68 74 2c 20 69 54 61 72 67 65 74 29  pRight, iTarget)
2c426 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58  ;.  }else if( pX
2c427 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ->op==TK_ISNULL 
2c428 29 7b 0a 20 20 20 20 69 52 65 67 20 3d 20 69 54  ){.    iReg = iT
2c429 61 72 67 65 74 3b 0a 20 20 20 20 73 71 6c 69 74  arget;.    sqlit
2c42a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c42b 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 65 67  OP_Null, 0, iReg
2c42c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
2c42d 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
2c42e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
2c42f 20 65 54 79 70 65 3b 0a 20 20 20 20 69 6e 74 20   eType;.    int 
2c430 69 54 61 62 3b 0a 20 20 20 20 73 74 72 75 63 74  iTab;.    struct
2c431 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 0a 20   InLoop *pIn;.. 
2c432 20 20 20 61 73 73 65 72 74 28 20 70 58 2d 3e 6f     assert( pX->o
2c433 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20 20  p==TK_IN );.    
2c434 69 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b 0a  iReg = iTarget;.
2c435 20 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69      eType = sqli
2c436 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70  te3FindInIndex(p
2c437 50 61 72 73 65 2c 20 70 58 2c 20 30 29 3b 0a 20  Parse, pX, 0);. 
2c438 20 20 20 69 54 61 62 20 3d 20 70 58 2d 3e 69 54     iTab = pX->iT
2c439 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  able;.    sqlite
2c43a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c43b 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62 2c 20  P_Rewind, iTab, 
2c43c 30 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  0);.    VdbeComm
2c43d 65 6e 74 28 28 76 2c 20 22 25 2e 2a 73 22 2c 20  ent((v, "%.*s", 
2c43e 70 58 2d 3e 73 70 61 6e 2e 6e 2c 20 70 58 2d 3e  pX->span.n, pX->
2c43f 73 70 61 6e 2e 7a 29 29 3b 0a 20 20 20 20 61 73  span.z));.    as
2c440 73 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e 70 6c  sert( pLevel->pl
2c441 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2c442 52 45 5f 49 4e 5f 41 42 4c 45 20 29 3b 0a 20 20  RE_IN_ABLE );.  
2c443 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 75 2e    if( pLevel->u.
2c444 69 6e 2e 6e 49 6e 3d 3d 30 20 29 7b 0a 20 20 20  in.nIn==0 ){.   
2c445 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e     pLevel->addrN
2c446 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  xt = sqlite3Vdbe
2c447 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
2c448 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e    }.    pLevel->
2c449 75 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0a 20 20 20 20  u.in.nIn++;.    
2c44a 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e  pLevel->u.in.aIn
2c44b 4c 6f 6f 70 20 3d 0a 20 20 20 20 20 20 20 73 71  Loop =.       sq
2c44c 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72  lite3DbReallocOr
2c44d 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  Free(pParse->db,
2c44e 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49   pLevel->u.in.aI
2c44f 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20 20 20 20 20  nLoop,.         
2c450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c451 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 65 76       sizeof(pLev
2c452 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70  el->u.in.aInLoop
2c453 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d 3e 75 2e 69  [0])*pLevel->u.i
2c454 6e 2e 6e 49 6e 29 3b 0a 20 20 20 20 70 49 6e 20  n.nIn);.    pIn 
2c455 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61  = pLevel->u.in.a
2c456 49 6e 4c 6f 6f 70 3b 0a 20 20 20 20 69 66 28 20  InLoop;.    if( 
2c457 70 49 6e 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  pIn ){.      pIn
2c458 20 2b 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e   += pLevel->u.in
2c459 2e 6e 49 6e 20 2d 20 31 3b 0a 20 20 20 20 20 20  .nIn - 1;.      
2c45a 70 49 6e 2d 3e 69 43 75 72 20 3d 20 69 54 61 62  pIn->iCur = iTab
2c45b 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70  ;.      if( eTyp
2c45c 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49  e==IN_INDEX_ROWI
2c45d 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49 6e  D ){.        pIn
2c45e 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d 20 73 71  ->addrInTop = sq
2c45f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c460 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61  v, OP_Rowid, iTa
2c461 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20 20  b, iReg);.      
2c462 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
2c463 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d 20  In->addrInTop = 
2c464 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c465 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2c466 69 54 61 62 2c 20 30 2c 20 69 52 65 67 29 3b 0a  iTab, 0, iReg);.
2c467 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2c468 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2c469 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52  v, OP_IsNull, iR
2c46a 65 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  eg);.    }else{.
2c46b 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e        pLevel->u.
2c46c 69 6e 2e 6e 49 6e 20 3d 20 30 3b 0a 20 20 20 20  in.nIn = 0;.    
2c46d 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 64  }.#endif.  }.  d
2c46e 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65  isableTerm(pLeve
2c46f 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 72 65 74  l, pTerm);.  ret
2c470 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a  urn iReg;.}../*.
2c471 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2c472 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c 75   that will evalu
2c473 61 74 65 20 61 6c 6c 20 3d 3d 20 61 6e 64 20 49  ate all == and I
2c474 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f  N constraints fo
2c475 72 20 61 6e 0a 2a 2a 20 69 6e 64 65 78 2e 20 20  r an.** index.  
2c476 54 68 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61  The values for a
2c477 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61  ll constraints a
2c478 72 65 20 6c 65 66 74 20 6f 6e 20 74 68 65 20 73  re left on the s
2c479 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  tack..**.** For 
2c47a 65 78 61 6d 70 6c 65 2c 20 63 6f 6e 73 69 64 65  example, conside
2c47b 72 20 74 61 62 6c 65 20 74 31 28 61 2c 62 2c 63  r table t1(a,b,c
2c47c 2c 64 2c 65 2c 66 29 20 77 69 74 68 20 69 6e 64  ,d,e,f) with ind
2c47d 65 78 20 69 31 28 61 2c 62 2c 63 29 2e 0a 2a 2a  ex i1(a,b,c)..**
2c47e 20 53 75 70 70 6f 73 65 20 74 68 65 20 57 48 45   Suppose the WHE
2c47f 52 45 20 63 6c 61 75 73 65 20 69 73 20 74 68 69  RE clause is thi
2c480 73 3a 20 20 61 3d 3d 35 20 41 4e 44 20 62 20 49  s:  a==5 AND b I
2c481 4e 20 28 31 2c 32 2c 33 29 20 41 4e 44 20 63 3e  N (1,2,3) AND c>
2c482 35 20 41 4e 44 20 63 3c 31 30 0a 2a 2a 20 54 68  5 AND c<10.** Th
2c483 65 20 69 6e 64 65 78 20 68 61 73 20 61 73 20 6d  e index has as m
2c484 61 6e 79 20 61 73 20 74 68 72 65 65 20 65 71 75  any as three equ
2c485 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2c486 73 2c 20 62 75 74 20 69 6e 20 74 68 69 73 0a 2a  s, but in this.*
2c487 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 74  * example, the t
2c488 68 69 72 64 20 22 63 22 20 76 61 6c 75 65 20 69  hird "c" value i
2c489 73 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 2e  s an inequality.
2c48a 20 20 53 6f 20 6f 6e 6c 79 20 74 77 6f 20 0a 2a    So only two .*
2c48b 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72  * constraints ar
2c48c 65 20 63 6f 64 65 64 2e 20 20 54 68 69 73 20 72  e coded.  This r
2c48d 6f 75 74 69 6e 65 20 77 69 6c 6c 20 67 65 6e 65  outine will gene
2c48e 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61  rate code to eva
2c48f 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d 35 20 61 6e  luate.** a==5 an
2c490 64 20 62 20 49 4e 20 28 31 2c 32 2c 33 29 2e 20  d b IN (1,2,3). 
2c491 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   The current val
2c492 75 65 73 20 66 6f 72 20 61 20 61 6e 64 20 62 20  ues for a and b 
2c493 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 0a 2a  will be stored.*
2c494 2a 20 69 6e 20 63 6f 6e 73 65 63 75 74 69 76 65  * in consecutive
2c495 20 72 65 67 69 73 74 65 72 73 20 61 6e 64 20 74   registers and t
2c496 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
2c497 66 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69  first register i
2c498 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
2c499 2a 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65  * In the example
2c49a 20 61 62 6f 76 65 20 6e 45 71 3d 3d 32 2e 20 20   above nEq==2.  
2c49b 42 75 74 20 74 68 69 73 20 73 75 62 72 6f 75 74  But this subrout
2c49c 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20 61 6e  ine works for an
2c49d 79 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 6e 45  y value.** of nE
2c49e 71 20 69 6e 63 6c 75 64 69 6e 67 20 30 2e 20 20  q including 0.  
2c49f 49 66 20 6e 45 71 3d 3d 30 2c 20 74 68 69 73 20  If nEq==0, this 
2c4a0 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 61 72 6c  routine is nearl
2c4a1 79 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 54 68  y a no-op..** Th
2c4a2 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 69 74 20  e only thing it 
2c4a3 64 6f 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65  does is allocate
2c4a4 20 74 68 65 20 70 4c 65 76 65 6c 2d 3e 69 4d 65   the pLevel->iMe
2c4a5 6d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 0a 2a  m memory cell..*
2c4a6 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2c4a7 65 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74  e always allocat
2c4a8 65 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  es at least one 
2c4a9 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20  memory cell and 
2c4aa 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 69  returns.** the i
2c4ab 6e 64 65 78 20 6f 66 20 74 68 61 74 20 6d 65 6d  ndex of that mem
2c4ac 6f 72 79 20 63 65 6c 6c 2e 20 54 68 65 20 63 6f  ory cell. The co
2c4ad 64 65 20 74 68 61 74 0a 2a 2a 20 63 61 6c 6c 73  de that.** calls
2c4ae 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
2c4af 6c 6c 20 75 73 65 20 74 68 61 74 20 6d 65 6d 6f  ll use that memo
2c4b0 72 79 20 63 65 6c 6c 20 74 6f 20 73 74 6f 72 65  ry cell to store
2c4b1 20 74 68 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e   the termination
2c4b2 0a 2a 2a 20 6b 65 79 20 76 61 6c 75 65 20 6f 66  .** key value of
2c4b3 20 74 68 65 20 6c 6f 6f 70 2e 20 20 49 66 20 6f   the loop.  If o
2c4b4 6e 65 20 6f 72 20 6d 6f 72 65 20 49 4e 20 6f 70  ne or more IN op
2c4b5 65 72 61 74 6f 72 73 20 61 70 70 65 61 72 2c 20  erators appear, 
2c4b6 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75  then.** this rou
2c4b7 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61  tine allocates a
2c4b8 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e 45 71  n additional nEq
2c4b9 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f   memory cells fo
2c4ba 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
2c4bb 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2c4bc 20 63 6f 64 65 41 6c 6c 45 71 75 61 6c 69 74 79   codeAllEquality
2c4bd 54 65 72 6d 73 28 0a 20 20 50 61 72 73 65 20 2a  Terms(.  Parse *
2c4be 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
2c4bf 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
2c4c0 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65  t */.  WhereLeve
2c4c1 6c 20 2a 70 4c 65 76 65 6c 2c 20 20 20 2f 2a 20  l *pLevel,   /* 
2c4c2 57 68 69 63 68 20 6e 65 73 74 65 64 20 6c 6f 6f  Which nested loo
2c4c3 70 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 77 65  p of the FROM we
2c4c4 20 61 72 65 20 63 6f 64 69 6e 67 20 2a 2f 0a 20   are coding */. 
2c4c5 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2c4c6 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48  C,     /* The WH
2c4c7 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2c4c8 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
2c4c9 2c 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 70  ,     /* Which p
2c4ca 61 72 74 73 20 6f 66 20 46 52 4f 4d 20 68 61 76  arts of FROM hav
2c4cb 65 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63  e not yet been c
2c4cc 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  oded */.  int nE
2c4cd 78 74 72 61 52 65 67 20 20 20 20 20 20 20 20 20  xtraReg         
2c4ce 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74  /* Number of ext
2c4cf 72 61 20 72 65 67 69 73 74 65 72 73 20 74 6f 20  ra registers to 
2c4d0 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 29 7b 0a 20  allocate */.){. 
2c4d1 20 69 6e 74 20 6e 45 71 20 3d 20 70 4c 65 76 65   int nEq = pLeve
2c4d2 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 3b 20 20 20 2f  l->plan.nEq;   /
2c4d3 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
2c4d4 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61  == or IN constra
2c4d5 69 6e 74 73 20 74 6f 20 63 6f 64 65 20 2a 2f 0a  ints to code */.
2c4d6 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2c4d7 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20 20 20  se->pVdbe;      
2c4d8 2f 2a 20 54 68 65 20 76 6d 20 75 6e 64 65 72 20  /* The vm under 
2c4d9 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a  construction */.
2c4da 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
2c4db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4dc 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 62 65 69  /* The index bei
2c4dd 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 69 73  ng used for this
2c4de 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69   loop */.  int i
2c4df 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 54  Cur = pLevel->iT
2c4e0 61 62 43 75 72 3b 20 20 20 2f 2a 20 54 68 65 20  abCur;   /* The 
2c4e1 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61  cursor of the ta
2c4e2 62 6c 65 20 2a 2f 0a 20 20 57 68 65 72 65 54 65  ble */.  WhereTe
2c4e3 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20  rm *pTerm;      
2c4e4 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67         /* A sing
2c4e5 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 65  le constraint te
2c4e6 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20  rm */.  int j;  
2c4e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4e8 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2c4e9 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72  unter */.  int r
2c4ea 65 67 42 61 73 65 3b 20 20 20 20 20 20 20 20 20  egBase;         
2c4eb 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
2c4ec 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69   register */.  i
2c4ed 6e 74 20 6e 52 65 67 3b 20 20 20 20 20 20 20 20  nt nReg;        
2c4ee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c4ef 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74  Number of regist
2c4f0 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ers to allocate 
2c4f1 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 6f  */..  /* This mo
2c4f2 64 75 6c 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c  dule is only cal
2c4f3 6c 65 64 20 6f 6e 20 71 75 65 72 79 20 70 6c 61  led on query pla
2c4f4 6e 73 20 74 68 61 74 20 75 73 65 20 61 6e 20 69  ns that use an i
2c4f5 6e 64 65 78 2e 20 2a 2f 0a 20 20 61 73 73 65 72  ndex. */.  asser
2c4f6 74 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  t( pLevel->plan.
2c4f7 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2c4f8 49 4e 44 45 58 45 44 20 29 3b 0a 20 20 70 49 64  INDEXED );.  pId
2c4f9 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  x = pLevel->plan
2c4fa 2e 75 2e 70 49 64 78 3b 0a 0a 20 20 2f 2a 20 46  .u.pIdx;..  /* F
2c4fb 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61  igure out how ma
2c4fc 6e 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  ny memory cells 
2c4fd 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 68 65  we will need the
2c4fe 6e 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 6d 2e  n allocate them.
2c4ff 0a 20 20 2a 2f 0a 20 20 72 65 67 42 61 73 65 20  .  */.  regBase 
2c500 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b  = pParse->nMem +
2c501 20 31 3b 0a 20 20 6e 52 65 67 20 3d 20 70 4c 65   1;.  nReg = pLe
2c502 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 20 2b 20  vel->plan.nEq + 
2c503 6e 45 78 74 72 61 52 65 67 3b 0a 20 20 70 50 61  nExtraReg;.  pPa
2c504 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65  rse->nMem += nRe
2c505 67 3b 0a 0a 20 20 2f 2a 20 45 76 61 6c 75 61 74  g;..  /* Evaluat
2c506 65 20 74 68 65 20 65 71 75 61 6c 69 74 79 20 63  e the equality c
2c507 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a  onstraints.  */.
2c508 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e    assert( pIdx->
2c509 6e 43 6f 6c 75 6d 6e 3e 3d 6e 45 71 20 29 3b 0a  nColumn>=nEq );.
2c50a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 45 71    for(j=0; j<nEq
2c50b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  ; j++){.    int 
2c50c 72 31 3b 0a 20 20 20 20 69 6e 74 20 6b 20 3d 20  r1;.    int k = 
2c50d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a  pIdx->aiColumn[j
2c50e 5d 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 66  ];.    pTerm = f
2c50f 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
2c510 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20  r, k, notReady, 
2c511 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2c512 6c 61 67 73 2c 20 70 49 64 78 29 3b 0a 20 20 20  lags, pIdx);.   
2c513 20 69 66 28 20 4e 45 56 45 52 28 70 54 65 72 6d   if( NEVER(pTerm
2c514 3d 3d 30 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  ==0) ) break;.  
2c515 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72 6d    assert( (pTerm
2c516 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
2c517 5f 43 4f 44 45 44 29 3d 3d 30 20 29 3b 0a 20 20  _CODED)==0 );.  
2c518 20 20 72 31 20 3d 20 63 6f 64 65 45 71 75 61 6c    r1 = codeEqual
2c519 69 74 79 54 65 72 6d 28 70 50 61 72 73 65 2c 20  ityTerm(pParse, 
2c51a 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 72  pTerm, pLevel, r
2c51b 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 69  egBase+j);.    i
2c51c 66 28 20 72 31 21 3d 72 65 67 42 61 73 65 2b 6a  f( r1!=regBase+j
2c51d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 52   ){.      if( nR
2c51e 65 67 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  eg==1 ){.       
2c51f 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2c520 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2c521 65 67 42 61 73 65 29 3b 0a 20 20 20 20 20 20 20  egBase);.       
2c522 20 72 65 67 42 61 73 65 20 3d 20 72 31 3b 0a 20   regBase = r1;. 
2c523 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2c524 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c525 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
2c526 79 2c 20 72 31 2c 20 72 65 67 42 61 73 65 2b 6a  y, r1, regBase+j
2c527 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2c528 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
2c529 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2c52a 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20  & WO_ISNULL );. 
2c52b 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
2c52c 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2c52d 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 69 66 28  WO_IN );.    if(
2c52e 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74   (pTerm->eOperat
2c52f 6f 72 20 26 20 28 57 4f 5f 49 53 4e 55 4c 4c 7c  or & (WO_ISNULL|
2c530 57 4f 5f 49 4e 29 29 3d 3d 30 20 29 7b 0a 20 20  WO_IN))==0 ){.  
2c531 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c532 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp2(v, OP_IsNu
2c533 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6a 2c 20 70  ll, regBase+j, p
2c534 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29 3b  Level->addrBrk);
2c535 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2c536 75 72 6e 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a  urn regBase;.}..
2c537 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2c538 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74 61 72  ode for the star
2c539 74 20 6f 66 20 74 68 65 20 69 4c 65 76 65 6c 2d  t of the iLevel-
2c53a 74 68 20 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57  th loop in the W
2c53b 48 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 69  HERE clause.** i
2c53c 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 65  mplementation de
2c53d 73 63 72 69 62 65 64 20 62 79 20 70 57 49 6e 66  scribed by pWInf
2c53e 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74  o..*/.static Bit
2c53f 6d 61 73 6b 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70  mask codeOneLoop
2c540 53 74 61 72 74 28 0a 20 20 57 68 65 72 65 49 6e  Start(.  WhereIn
2c541 66 6f 20 2a 70 57 49 6e 66 6f 2c 20 20 20 2f 2a  fo *pWInfo,   /*
2c542 20 43 6f 6d 70 6c 65 74 65 20 69 6e 66 6f 72 6d   Complete inform
2c543 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
2c544 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2c545 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20    int iLevel,   
2c546 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20         /* Which 
2c547 6c 65 76 65 6c 20 6f 66 20 70 57 49 6e 66 6f 2d  level of pWInfo-
2c548 3e 61 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 63  >a[] should be c
2c549 6f 64 65 64 20 2a 2f 0a 20 20 75 31 36 20 77 63  oded */.  u16 wc
2c54a 74 72 6c 46 6c 61 67 73 2c 20 20 20 20 20 20 2f  trlFlags,      /
2c54b 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57 48 45  * One of the WHE
2c54c 52 45 5f 2a 20 66 6c 61 67 73 20 64 65 66 69 6e  RE_* flags defin
2c54d 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ed in sqliteInt.
2c54e 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  h */.  Bitmask n
2c54f 6f 74 52 65 61 64 79 20 20 20 20 20 2f 2a 20 57  otReady     /* W
2c550 68 69 63 68 20 74 61 62 6c 65 73 20 61 72 65 20  hich tables are 
2c551 63 75 72 72 65 6e 74 6c 79 20 61 76 61 69 6c 61  currently availa
2c552 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ble */.){.  int 
2c553 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20  j, k;           
2c554 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2c555 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  s */.  int iCur;
2c556 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2c557 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66  he VDBE cursor f
2c558 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  or the table */.
2c559 20 20 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 20    int addrNxt;  
2c55a 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
2c55b 74 6f 20 6a 75 6d 70 20 74 6f 20 63 6f 6e 74 69  to jump to conti
2c55c 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78  nue with the nex
2c55d 74 20 49 4e 20 63 61 73 65 20 2a 2f 0a 20 20 69  t IN case */.  i
2c55e 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b 20 20 20  nt omitTable;   
2c55f 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77      /* True if w
2c560 65 20 75 73 65 20 74 68 65 20 69 6e 64 65 78 20  e use the index 
2c561 6f 6e 6c 79 20 2a 2f 0a 20 20 69 6e 74 20 62 52  only */.  int bR
2c562 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ev;            /
2c563 2a 20 54 72 75 65 20 69 66 20 77 65 20 6e 65 65  * True if we nee
2c564 64 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 65 76  d to scan in rev
2c565 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20 20  erse order */.  
2c566 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
2c567 65 6c 3b 20 20 2f 2a 20 54 68 65 20 77 68 65 72  el;  /* The wher
2c568 65 20 6c 65 76 65 6c 20 74 6f 20 62 65 20 63 6f  e level to be co
2c569 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ded */.  WhereCl
2c56a 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 2f 2a  ause *pWC;    /*
2c56b 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f   Decomposition o
2c56c 66 20 74 68 65 20 65 6e 74 69 72 65 20 57 48 45  f the entire WHE
2c56d 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57  RE clause */.  W
2c56e 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
2c56f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c570 2a 20 41 20 57 48 45 52 45 20 63 6c 61 75 73 65  * A WHERE clause
2c571 20 74 65 72 6d 20 2a 2f 0a 20 20 50 61 72 73 65   term */.  Parse
2c572 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20   *pParse;       
2c573 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2c574 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2c575 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20  .  Vdbe *v;     
2c576 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c577 20 20 20 2f 2a 20 54 68 65 20 70 72 65 70 61 72     /* The prepar
2c578 65 64 20 73 74 6d 74 20 75 6e 64 65 72 20 63 6f  ed stmt under co
2c579 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20  nstructions */. 
2c57a 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2c57b 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 3b 20  item *pTabItem; 
2c57c 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   /* FROM clause 
2c57d 74 65 72 6d 20 62 65 69 6e 67 20 63 6f 64 65 64  term being coded
2c57e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 72   */.  int addrBr
2c57f 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
2c580 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
2c581 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20  re to break out 
2c582 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
2c583 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 3b 20 20   int addrCont;  
2c584 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c585 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
2c586 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e   continue with n
2c587 65 78 74 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69  ext cycle */.  i
2c588 6e 74 20 69 52 6f 77 69 64 52 65 67 20 3d 20 30  nt iRowidReg = 0
2c589 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69  ;        /* Rowi
2c58a 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  d is stored in t
2c58b 68 69 73 20 72 65 67 69 73 74 65 72 2c 20 69 66  his register, if
2c58c 20 6e 6f 74 20 7a 65 72 6f 20 2a 2f 0a 20 20 69   not zero */.  i
2c58d 6e 74 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d  nt iReleaseReg =
2c58e 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 65 6d 70   0;      /* Temp
2c58f 20 72 65 67 69 73 74 65 72 20 74 6f 20 66 72 65   register to fre
2c590 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  e before returni
2c591 6e 67 20 2a 2f 0a 0a 20 20 70 50 61 72 73 65 20  ng */..  pParse 
2c592 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65  = pWInfo->pParse
2c593 3b 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e  ;.  v = pParse->
2c594 70 56 64 62 65 3b 0a 20 20 70 57 43 20 3d 20 70  pVdbe;.  pWC = p
2c595 57 49 6e 66 6f 2d 3e 70 57 43 3b 0a 20 20 70 4c  WInfo->pWC;.  pL
2c596 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e  evel = &pWInfo->
2c597 61 5b 69 4c 65 76 65 6c 5d 3b 0a 20 20 70 54 61  a[iLevel];.  pTa
2c598 62 49 74 65 6d 20 3d 20 26 70 57 49 6e 66 6f 2d  bItem = &pWInfo-
2c599 3e 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65  >pTabList->a[pLe
2c59a 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 69  vel->iFrom];.  i
2c59b 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e  Cur = pTabItem->
2c59c 69 43 75 72 73 6f 72 3b 0a 20 20 62 52 65 76 20  iCursor;.  bRev 
2c59d 3d 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  = (pLevel->plan.
2c59e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2c59f 52 45 56 45 52 53 45 29 21 3d 30 3b 0a 20 20 6f  REVERSE)!=0;.  o
2c5a0 6d 69 74 54 61 62 6c 65 20 3d 20 28 70 4c 65 76  mitTable = (pLev
2c5a1 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2c5a2 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c   & WHERE_IDX_ONL
2c5a3 59 29 21 3d 30 20 0a 20 20 20 20 20 20 20 20 20  Y)!=0 .         
2c5a4 20 20 26 26 20 28 77 63 74 72 6c 46 6c 61 67 73    && (wctrlFlags
2c5a5 20 26 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54   & WHERE_FORCE_T
2c5a6 41 42 4c 45 29 3d 3d 30 3b 0a 0a 20 20 2f 2a 20  ABLE)==0;..  /* 
2c5a7 43 72 65 61 74 65 20 6c 61 62 65 6c 73 20 66 6f  Create labels fo
2c5a8 72 20 74 68 65 20 22 62 72 65 61 6b 22 20 61 6e  r the "break" an
2c5a9 64 20 22 63 6f 6e 74 69 6e 75 65 22 20 69 6e 73  d "continue" ins
2c5aa 74 72 75 63 74 69 6f 6e 73 0a 20 20 2a 2a 20 66  tructions.  ** f
2c5ab 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  or the current l
2c5ac 6f 6f 70 2e 20 20 4a 75 6d 70 20 74 6f 20 61 64  oop.  Jump to ad
2c5ad 64 72 42 72 6b 20 74 6f 20 62 72 65 61 6b 20 6f  drBrk to break o
2c5ae 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 0a 20 20  ut of a loop..  
2c5af 2a 2a 20 4a 75 6d 70 20 74 6f 20 63 6f 6e 74 20  ** Jump to cont 
2c5b0 74 6f 20 67 6f 20 69 6d 6d 65 64 69 61 74 65 6c  to go immediatel
2c5b1 79 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74  y to the next it
2c5b2 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20  eration of the. 
2c5b3 20 2a 2a 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 0a 20   ** loop..  **. 
2c5b4 20 2a 2a 20 57 68 65 6e 20 74 68 65 72 65 20 69   ** When there i
2c5b5 73 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72  s an IN operator
2c5b6 2c 20 77 65 20 61 6c 73 6f 20 68 61 76 65 20 61  , we also have a
2c5b7 20 22 61 64 64 72 4e 78 74 22 20 6c 61 62 65 6c   "addrNxt" label
2c5b8 20 74 68 61 74 0a 20 20 2a 2a 20 6d 65 61 6e 73   that.  ** means
2c5b9 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
2c5ba 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20 76 61  h the next IN va
2c5bb 6c 75 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 2e  lue combination.
2c5bc 20 20 57 68 65 6e 0a 20 20 2a 2a 20 74 68 65 72    When.  ** ther
2c5bd 65 20 61 72 65 20 6e 6f 20 49 4e 20 6f 70 65 72  e are no IN oper
2c5be 61 74 6f 72 73 20 69 6e 20 74 68 65 20 63 6f 6e  ators in the con
2c5bf 73 74 72 61 69 6e 74 73 2c 20 74 68 65 20 22 61  straints, the "a
2c5c0 64 64 72 4e 78 74 22 20 6c 61 62 65 6c 0a 20 20  ddrNxt" label.  
2c5c1 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ** is the same a
2c5c2 73 20 22 61 64 64 72 42 72 6b 22 2e 0a 20 20 2a  s "addrBrk"..  *
2c5c3 2f 0a 20 20 61 64 64 72 42 72 6b 20 3d 20 70 4c  /.  addrBrk = pL
2c5c4 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 20 3d 20  evel->addrBrk = 
2c5c5 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 20  pLevel->addrNxt 
2c5c6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2c5c7 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 61 64 64  eLabel(v);.  add
2c5c8 72 43 6f 6e 74 20 3d 20 70 4c 65 76 65 6c 2d 3e  rCont = pLevel->
2c5c9 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74  addrCont = sqlit
2c5ca 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2c5cb 76 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69  v);..  /* If thi
2c5cc 73 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74  s is the right t
2c5cd 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4f  able of a LEFT O
2c5ce 55 54 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c 6f 63  UTER JOIN, alloc
2c5cf 61 74 65 20 61 6e 64 0a 20 20 2a 2a 20 69 6e 69  ate and.  ** ini
2c5d0 74 69 61 6c 69 7a 65 20 61 20 6d 65 6d 6f 72 79  tialize a memory
2c5d1 20 63 65 6c 6c 20 74 68 61 74 20 72 65 63 6f 72   cell that recor
2c5d2 64 73 20 69 66 20 74 68 69 73 20 74 61 62 6c 65  ds if this table
2c5d3 20 6d 61 74 63 68 65 73 20 61 6e 79 0a 20 20 2a   matches any.  *
2c5d4 2a 20 72 6f 77 20 6f 66 20 74 68 65 20 6c 65 66  * row of the lef
2c5d5 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a  t table of the j
2c5d6 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  oin..  */.  if( 
2c5d7 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 3e 30 20  pLevel->iFrom>0 
2c5d8 26 26 20 28 70 54 61 62 49 74 65 6d 5b 30 5d 2e  && (pTabItem[0].
2c5d9 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45  jointype & JT_LE
2c5da 46 54 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 4c  FT)!=0 ){.    pL
2c5db 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20  evel->iLeftJoin 
2c5dc 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2c5dd 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2c5de 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2c5df 74 65 67 65 72 2c 20 30 2c 20 70 4c 65 76 65 6c  teger, 0, pLevel
2c5e0 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20  ->iLeftJoin);.  
2c5e1 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2c5e2 2c 20 22 69 6e 69 74 20 4c 45 46 54 20 4a 4f 49  , "init LEFT JOI
2c5e3 4e 20 6e 6f 2d 6d 61 74 63 68 20 66 6c 61 67 22  N no-match flag"
2c5e4 29 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  ));.  }..#ifndef
2c5e5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2c5e6 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
2c5e7 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
2c5e8 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56  sFlags & WHERE_V
2c5e9 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20  IRTUALTABLE)!=0 
2c5ea 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 30  ){.    /* Case 0
2c5eb 3a 20 20 54 68 65 20 74 61 62 6c 65 20 69 73 20  :  The table is 
2c5ec 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2e  a virtual-table.
2c5ed 20 20 55 73 65 20 74 68 65 20 56 46 69 6c 74 65    Use the VFilte
2c5ee 72 20 61 6e 64 20 56 4e 65 78 74 0a 20 20 20 20  r and VNext.    
2c5ef 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 61  **          to a
2c5f0 63 63 65 73 73 20 74 68 65 20 64 61 74 61 2e 0a  ccess the data..
2c5f1 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69      */.    int i
2c5f2 52 65 67 3b 20 20 20 2f 2a 20 50 33 20 56 61 6c  Reg;   /* P3 Val
2c5f3 75 65 20 66 6f 72 20 4f 50 5f 56 46 69 6c 74 65  ue for OP_VFilte
2c5f4 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  r */.    sqlite3
2c5f5 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74  _index_info *pVt
2c5f6 61 62 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e  abIdx = pLevel->
2c5f7 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64 78 3b  plan.u.pVtabIdx;
2c5f8 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72  .    int nConstr
2c5f9 61 69 6e 74 20 3d 20 70 56 74 61 62 49 64 78 2d  aint = pVtabIdx-
2c5fa 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20  >nConstraint;.  
2c5fb 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
2c5fc 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
2c5fd 74 5f 75 73 61 67 65 20 2a 61 55 73 61 67 65 20  t_usage *aUsage 
2c5fe 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
2c5ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c601 20 20 70 56 74 61 62 49 64 78 2d 3e 61 43 6f 6e    pVtabIdx->aCon
2c602 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20  straintUsage;.  
2c603 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73    const struct s
2c604 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2c605 73 74 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72  straint *aConstr
2c606 61 69 6e 74 20 3d 0a 20 20 20 20 20 20 20 20 20  aint =.         
2c607 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c608 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c609 20 20 20 20 20 20 20 70 56 74 61 62 49 64 78 2d         pVtabIdx-
2c60a 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 0a 20  >aConstraint;.. 
2c60b 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65     iReg = sqlite
2c60c 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
2c60d 61 72 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e  arse, nConstrain
2c60e 74 2b 32 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  t+2);.    for(j=
2c60f 31 3b 20 6a 3c 3d 6e 43 6f 6e 73 74 72 61 69 6e  1; j<=nConstrain
2c610 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66  t; j++){.      f
2c611 6f 72 28 6b 3d 30 3b 20 6b 3c 6e 43 6f 6e 73 74  or(k=0; k<nConst
2c612 72 61 69 6e 74 3b 20 6b 2b 2b 29 7b 0a 20 20 20  raint; k++){.   
2c613 20 20 20 20 20 69 66 28 20 61 55 73 61 67 65 5b       if( aUsage[
2c614 6b 5d 2e 61 72 67 76 49 6e 64 65 78 3d 3d 6a 20  k].argvIndex==j 
2c615 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
2c616 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74 72   iTerm = aConstr
2c617 61 69 6e 74 5b 6b 5d 2e 69 54 65 72 6d 4f 66 66  aint[k].iTermOff
2c618 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 73  set;.          s
2c619 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2c61a 50 61 72 73 65 2c 20 70 57 43 2d 3e 61 5b 69 54  Parse, pWC->a[iT
2c61b 65 72 6d 5d 2e 70 45 78 70 72 2d 3e 70 52 69 67  erm].pExpr->pRig
2c61c 68 74 2c 20 69 52 65 67 2b 6a 2b 31 29 3b 0a 20  ht, iReg+j+1);. 
2c61d 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c61e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2c61f 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 6e  }.      if( k==n
2c620 43 6f 6e 73 74 72 61 69 6e 74 20 29 20 62 72 65  Constraint ) bre
2c621 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ak;.    }.    sq
2c622 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c623 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70  v, OP_Integer, p
2c624 56 74 61 62 49 64 78 2d 3e 69 64 78 4e 75 6d 2c  VtabIdx->idxNum,
2c625 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69   iReg);.    sqli
2c626 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c627 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6a 2d 31   OP_Integer, j-1
2c628 2c 20 69 52 65 67 2b 31 29 3b 0a 20 20 20 20 73  , iReg+1);.    s
2c629 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2c62a 28 76 2c 20 4f 50 5f 56 46 69 6c 74 65 72 2c 20  (v, OP_VFilter, 
2c62b 69 43 75 72 2c 20 61 64 64 72 42 72 6b 2c 20 69  iCur, addrBrk, i
2c62c 52 65 67 2c 20 70 56 74 61 62 49 64 78 2d 3e 69  Reg, pVtabIdx->i
2c62d 64 78 53 74 72 2c 0a 20 20 20 20 20 20 20 20 20  dxStr,.         
2c62e 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56 74               pVt
2c62f 61 62 49 64 78 2d 3e 6e 65 65 64 54 6f 46 72 65  abIdx->needToFre
2c630 65 49 64 78 53 74 72 20 3f 20 50 34 5f 4d 50 52  eIdxStr ? P4_MPR
2c631 49 4e 54 46 20 3a 20 50 34 5f 53 54 41 54 49 43  INTF : P4_STATIC
2c632 29 3b 0a 20 20 20 20 70 56 74 61 62 49 64 78 2d  );.    pVtabIdx-
2c633 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
2c634 72 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a  r = 0;.    for(j
2c635 3d 30 3b 20 6a 3c 6e 43 6f 6e 73 74 72 61 69 6e  =0; j<nConstrain
2c636 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69  t; j++){.      i
2c637 66 28 20 61 55 73 61 67 65 5b 6a 5d 2e 6f 6d 69  f( aUsage[j].omi
2c638 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  t ){.        int
2c639 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74 72   iTerm = aConstr
2c63a 61 69 6e 74 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66  aint[j].iTermOff
2c63b 73 65 74 3b 0a 20 20 20 20 20 20 20 20 64 69 73  set;.        dis
2c63c 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
2c63d 20 26 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d 29   &pWC->a[iTerm])
2c63e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2c63f 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d      pLevel->op =
2c640 20 4f 50 5f 56 4e 65 78 74 3b 0a 20 20 20 20 70   OP_VNext;.    p
2c641 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72  Level->p1 = iCur
2c642 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32  ;.    pLevel->p2
2c643 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
2c644 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
2c645 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2c646 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
2c647 2c 20 69 52 65 67 2c 20 6e 43 6f 6e 73 74 72 61  , iReg, nConstra
2c648 69 6e 74 2b 32 29 3b 0a 20 20 7d 65 6c 73 65 0a  int+2);.  }else.
2c649 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2c64a 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2c64b 4c 45 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4c 65  LE */..  if( pLe
2c64c 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2c64d 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f  s & WHERE_ROWID_
2c64e 45 51 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73  EQ ){.    /* Cas
2c64f 65 20 31 3a 20 20 57 65 20 63 61 6e 20 64 69 72  e 1:  We can dir
2c650 65 63 74 6c 79 20 72 65 66 65 72 65 6e 63 65 20  ectly reference 
2c651 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 75 73 69  a single row usi
2c652 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 20 20 20  ng an.    **    
2c653 20 20 20 20 20 20 65 71 75 61 6c 69 74 79 20 63        equality c
2c654 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73  omparison agains
2c655 74 20 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c  t the ROWID fiel
2c656 64 2e 20 20 4f 72 0a 20 20 20 20 2a 2a 20 20 20  d.  Or.    **   
2c657 20 20 20 20 20 20 20 77 65 20 72 65 66 65 72 65         we refere
2c658 6e 63 65 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77  nce multiple row
2c659 73 20 75 73 69 6e 67 20 61 20 22 72 6f 77 69 64  s using a "rowid
2c65a 20 49 4e 20 28 2e 2e 2e 29 22 0a 20 20 20 20 2a   IN (...)".    *
2c65b 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74  *          const
2c65c 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ruct..    */.   
2c65d 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73   iReleaseReg = s
2c65e 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2c65f 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 70 54  (pParse);.    pT
2c660 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  erm = findTerm(p
2c661 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f  WC, iCur, -1, no
2c662 74 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57 4f  tReady, WO_EQ|WO
2c663 5f 49 4e 2c 20 30 29 3b 0a 20 20 20 20 61 73 73  _IN, 0);.    ass
2c664 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b  ert( pTerm!=0 );
2c665 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65  .    assert( pTe
2c666 72 6d 2d 3e 70 45 78 70 72 21 3d 30 20 29 3b 0a  rm->pExpr!=0 );.
2c667 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72      assert( pTer
2c668 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69  m->leftCursor==i
2c669 43 75 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72  Cur );.    asser
2c66a 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20  t( omitTable==0 
2c66b 29 3b 0a 20 20 20 20 69 52 6f 77 69 64 52 65 67  );.    iRowidReg
2c66c 20 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54   = codeEqualityT
2c66d 65 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72  erm(pParse, pTer
2c66e 6d 2c 20 70 4c 65 76 65 6c 2c 20 69 52 65 6c 65  m, pLevel, iRele
2c66f 61 73 65 52 65 67 29 3b 0a 20 20 20 20 61 64 64  aseReg);.    add
2c670 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61  rNxt = pLevel->a
2c671 64 64 72 4e 78 74 3b 0a 20 20 20 20 73 71 6c 69  ddrNxt;.    sqli
2c672 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c673 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69   OP_MustBeInt, i
2c674 52 6f 77 69 64 52 65 67 2c 20 61 64 64 72 4e 78  RowidReg, addrNx
2c675 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
2c676 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2c677 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c  NotExists, iCur,
2c678 20 61 64 64 72 4e 78 74 2c 20 69 52 6f 77 69 64   addrNxt, iRowid
2c679 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Reg);.    sqlite
2c67a 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28  3ExprCacheStore(
2c67b 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31  pParse, iCur, -1
2c67c 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20  , iRowidReg);.  
2c67d 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2c67e 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20 20 70 4c  , "pk"));.    pL
2c67f 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 6f  evel->op = OP_No
2c680 6f 70 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  op;.  }else if( 
2c681 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2c682 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57  lags & WHERE_ROW
2c683 49 44 5f 52 41 4e 47 45 20 29 7b 0a 20 20 20 20  ID_RANGE ){.    
2c684 2f 2a 20 43 61 73 65 20 32 3a 20 20 57 65 20 68  /* Case 2:  We h
2c685 61 76 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74  ave an inequalit
2c686 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61  y comparison aga
2c687 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44 20 66  inst the ROWID f
2c688 69 65 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ield..    */.   
2c689 20 69 6e 74 20 74 65 73 74 4f 70 20 3d 20 4f 50   int testOp = OP
2c68a 5f 4e 6f 6f 70 3b 0a 20 20 20 20 69 6e 74 20 73  _Noop;.    int s
2c68b 74 61 72 74 3b 0a 20 20 20 20 69 6e 74 20 6d 65  tart;.    int me
2c68c 6d 45 6e 64 56 61 6c 75 65 20 3d 20 30 3b 0a 20  mEndValue = 0;. 
2c68d 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 53     WhereTerm *pS
2c68e 74 61 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a 20 20  tart, *pEnd;..  
2c68f 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74 54 61    assert( omitTa
2c690 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 53  ble==0 );.    pS
2c691 74 61 72 74 20 3d 20 66 69 6e 64 54 65 72 6d 28  tart = findTerm(
2c692 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e  pWC, iCur, -1, n
2c693 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57  otReady, WO_GT|W
2c694 4f 5f 47 45 2c 20 30 29 3b 0a 20 20 20 20 70 45  O_GE, 0);.    pE
2c695 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  nd = findTerm(pW
2c696 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
2c697 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f  Ready, WO_LT|WO_
2c698 4c 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  LE, 0);.    if( 
2c699 62 52 65 76 20 29 7b 0a 20 20 20 20 20 20 70 54  bRev ){.      pT
2c69a 65 72 6d 20 3d 20 70 53 74 61 72 74 3b 0a 20 20  erm = pStart;.  
2c69b 20 20 20 20 70 53 74 61 72 74 20 3d 20 70 45 6e      pStart = pEn
2c69c 64 3b 0a 20 20 20 20 20 20 70 45 6e 64 20 3d 20  d;.      pEnd = 
2c69d 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  pTerm;.    }.   
2c69e 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20   if( pStart ){. 
2c69f 20 20 20 20 20 45 78 70 72 20 2a 70 58 3b 20 20       Expr *pX;  
2c6a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2c6a1 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  e expression tha
2c6a2 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 73 74  t defines the st
2c6a3 61 72 74 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 20  art bound */.   
2c6a4 20 20 20 69 6e 74 20 72 31 2c 20 72 54 65 6d 70     int r1, rTemp
2c6a5 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  ;        /* Regi
2c6a6 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e  sters for holdin
2c6a7 67 20 74 68 65 20 73 74 61 72 74 20 62 6f 75 6e  g the start boun
2c6a8 64 61 72 79 20 2a 2f 0a 0a 20 20 20 20 20 20 2f  dary */..      /
2c6a9 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2c6aa 63 6f 6e 73 74 61 6e 74 20 6d 61 70 73 20 54 4b  constant maps TK
2c6ab 5f 78 78 20 63 6f 64 65 73 20 69 6e 74 6f 20 63  _xx codes into c
2c6ac 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a 20 20  orresponding .  
2c6ad 20 20 20 20 2a 2a 20 73 65 65 6b 20 6f 70 63 6f      ** seek opco
2c6ae 64 65 73 2e 20 20 49 74 20 64 65 70 65 6e 64 73  des.  It depends
2c6af 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   on a particular
2c6b0 20 6f 72 64 65 72 69 6e 67 20 6f 66 20 54 4b 5f   ordering of TK_
2c6b1 78 78 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  xx.      */.    
2c6b2 20 20 63 6f 6e 73 74 20 75 38 20 61 4d 6f 76 65    const u8 aMove
2c6b3 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  Op[] = {.       
2c6b4 20 20 20 20 2f 2a 20 54 4b 5f 47 54 20 2a 2f 20      /* TK_GT */ 
2c6b5 20 4f 50 5f 53 65 65 6b 47 74 2c 0a 20 20 20 20   OP_SeekGt,.    
2c6b6 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 45 20         /* TK_LE 
2c6b7 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c 65 2c 0a 20  */  OP_SeekLe,. 
2c6b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f            /* TK_
2c6b9 4c 54 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c 74  LT */  OP_SeekLt
2c6ba 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,.           /* 
2c6bb 54 4b 5f 47 45 20 2a 2f 20 20 4f 50 5f 53 65 65  TK_GE */  OP_See
2c6bc 6b 47 65 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20  kGe.      };.   
2c6bd 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45     assert( TK_LE
2c6be 3d 3d 54 4b 5f 47 54 2b 31 20 29 3b 20 20 20 20  ==TK_GT+1 );    
2c6bf 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
2c6c0 68 65 20 6f 72 64 65 72 69 6e 67 2e 2e 20 2a 2f  he ordering.. */
2c6c1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
2c6c2 4b 5f 4c 54 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b  K_LT==TK_GT+2 );
2c6c3 20 20 20 20 20 20 2f 2a 20 20 2e 2e 2e 20 6f 66        /*  ... of
2c6c4 20 74 68 65 20 54 4b 5f 78 78 20 76 61 6c 75 65   the TK_xx value
2c6c5 73 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73  s... */.      as
2c6c6 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f  sert( TK_GE==TK_
2c6c7 47 54 2b 33 20 29 3b 20 20 20 20 20 20 2f 2a 20  GT+3 );      /* 
2c6c8 20 2e 2e 2e 20 69 73 20 63 6f 72 72 65 63 63 74   ... is correcct
2c6c9 2e 20 2a 2f 0a 0a 20 20 20 20 20 20 70 58 20 3d  . */..      pX =
2c6ca 20 70 53 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a   pStart->pExpr;.
2c6cb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 58        assert( pX
2c6cc 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
2c6cd 65 72 74 28 20 70 53 74 61 72 74 2d 3e 6c 65 66  ert( pStart->lef
2c6ce 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b  tCursor==iCur );
2c6cf 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
2c6d0 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
2c6d1 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67  pParse, pX->pRig
2c6d2 68 74 2c 20 26 72 54 65 6d 70 29 3b 0a 20 20 20  ht, &rTemp);.   
2c6d3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c6d4 64 4f 70 33 28 76 2c 20 61 4d 6f 76 65 4f 70 5b  dOp3(v, aMoveOp[
2c6d5 70 58 2d 3e 6f 70 2d 54 4b 5f 47 54 5d 2c 20 69  pX->op-TK_GT], i
2c6d6 43 75 72 2c 20 61 64 64 72 42 72 6b 2c 20 72 31  Cur, addrBrk, r1
2c6d7 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2c6d8 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b  ment((v, "pk"));
2c6d9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2c6da 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
2c6db 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31  hange(pParse, r1
2c6dc 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
2c6dd 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2c6de 67 28 70 50 61 72 73 65 2c 20 72 54 65 6d 70 29  g(pParse, rTemp)
2c6df 3b 0a 20 20 20 20 20 20 64 69 73 61 62 6c 65 54  ;.      disableT
2c6e0 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 53 74 61  erm(pLevel, pSta
2c6e1 72 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  rt);.    }else{.
2c6e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c6e3 65 41 64 64 4f 70 32 28 76 2c 20 62 52 65 76 20  eAddOp2(v, bRev 
2c6e4 3f 20 4f 50 5f 4c 61 73 74 20 3a 20 4f 50 5f 52  ? OP_Last : OP_R
2c6e5 65 77 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64  ewind, iCur, add
2c6e6 72 42 72 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rBrk);.    }.   
2c6e7 20 69 66 28 20 70 45 6e 64 20 29 7b 0a 20 20 20   if( pEnd ){.   
2c6e8 20 20 20 45 78 70 72 20 2a 70 58 3b 0a 20 20 20     Expr *pX;.   
2c6e9 20 20 20 70 58 20 3d 20 70 45 6e 64 2d 3e 70 45     pX = pEnd->pE
2c6ea 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xpr;.      asser
2c6eb 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 20 20  t( pX!=0 );.    
2c6ec 20 20 61 73 73 65 72 74 28 20 70 45 6e 64 2d 3e    assert( pEnd->
2c6ed 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72  leftCursor==iCur
2c6ee 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 45 6e 64   );.      memEnd
2c6ef 56 61 6c 75 65 20 3d 20 2b 2b 70 50 61 72 73 65  Value = ++pParse
2c6f0 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71  ->nMem;.      sq
2c6f1 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2c6f2 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74  arse, pX->pRight
2c6f3 2c 20 6d 65 6d 45 6e 64 56 61 6c 75 65 29 3b 0a  , memEndValue);.
2c6f4 20 20 20 20 20 20 69 66 28 20 70 58 2d 3e 6f 70        if( pX->op
2c6f5 3d 3d 54 4b 5f 4c 54 20 7c 7c 20 70 58 2d 3e 6f  ==TK_LT || pX->o
2c6f6 70 3d 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20  p==TK_GT ){.    
2c6f7 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65      testOp = bRe
2c6f8 76 20 3f 20 4f 50 5f 4c 65 20 3a 20 4f 50 5f 47  v ? OP_Le : OP_G
2c6f9 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  e;.      }else{.
2c6fa 20 20 20 20 20 20 20 20 74 65 73 74 4f 70 20 3d          testOp =
2c6fb 20 62 52 65 76 20 3f 20 4f 50 5f 4c 74 20 3a 20   bRev ? OP_Lt : 
2c6fc 4f 50 5f 47 74 3b 0a 20 20 20 20 20 20 7d 0a 20  OP_Gt;.      }. 
2c6fd 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d       disableTerm
2c6fe 28 70 4c 65 76 65 6c 2c 20 70 45 6e 64 29 3b 0a  (pLevel, pEnd);.
2c6ff 20 20 20 20 7d 0a 20 20 20 20 73 74 61 72 74 20      }.    start 
2c700 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2c701 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
2c702 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52   pLevel->op = bR
2c703 65 76 20 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f  ev ? OP_Prev : O
2c704 50 5f 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65 76  P_Next;.    pLev
2c705 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20  el->p1 = iCur;. 
2c706 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20     pLevel->p2 = 
2c707 73 74 61 72 74 3b 0a 20 20 20 20 70 4c 65 76 65  start;.    pLeve
2c708 6c 2d 3e 70 35 20 3d 20 28 70 53 74 61 72 74 3d  l->p5 = (pStart=
2c709 3d 30 20 26 26 20 70 45 6e 64 3d 3d 30 29 20 3f  =0 && pEnd==0) ?
2c70a 31 3a 30 3b 0a 20 20 20 20 69 66 28 20 74 65 73  1:0;.    if( tes
2c70b 74 4f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a  tOp!=OP_Noop ){.
2c70c 20 20 20 20 20 20 69 52 6f 77 69 64 52 65 67 20        iRowidReg 
2c70d 3d 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20  = iReleaseReg = 
2c70e 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2c70f 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
2c710 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c711 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
2c712 69 43 75 72 2c 20 69 52 6f 77 69 64 52 65 67 29  iCur, iRowidReg)
2c713 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2c714 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70 50  xprCacheStore(pP
2c715 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20  arse, iCur, -1, 
2c716 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20  iRowidReg);.    
2c717 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c718 4f 70 33 28 76 2c 20 74 65 73 74 4f 70 2c 20 6d  Op3(v, testOp, m
2c719 65 6d 45 6e 64 56 61 6c 75 65 2c 20 61 64 64 72  emEndValue, addr
2c71a 42 72 6b 2c 20 69 52 6f 77 69 64 52 65 67 29 3b  Brk, iRowidReg);
2c71b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c71c 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51  beChangeP5(v, SQ
2c71d 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
2c71e 20 7c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46   | SQLITE_JUMPIF
2c71f 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  NULL);.    }.  }
2c720 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d  else if( pLevel-
2c721 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2c722 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41  (WHERE_COLUMN_RA
2c723 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  NGE|WHERE_COLUMN
2c724 5f 45 51 29 20 29 7b 0a 20 20 20 20 2f 2a 20 43  _EQ) ){.    /* C
2c725 61 73 65 20 33 3a 20 41 20 73 63 61 6e 20 75 73  ase 3: A scan us
2c726 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20  ing an index..  
2c727 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
2c728 20 20 20 20 54 68 65 20 57 48 45 52 45 20 63 6c      The WHERE cl
2c729 61 75 73 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e  ause may contain
2c72a 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 65 71   zero or more eq
2c72b 75 61 6c 69 74 79 20 0a 20 20 20 20 2a 2a 20 20  uality .    **  
2c72c 20 20 20 20 20 20 20 74 65 72 6d 73 20 28 22 3d         terms ("=
2c72d 3d 22 20 6f 72 20 22 49 4e 22 20 6f 70 65 72 61  =" or "IN" opera
2c72e 74 6f 72 73 29 20 74 68 61 74 20 72 65 66 65 72  tors) that refer
2c72f 20 74 6f 20 74 68 65 20 4e 0a 20 20 20 20 2a 2a   to the N.    **
2c730 20 20 20 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f           left-mo
2c731 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  st columns of th
2c732 65 20 69 6e 64 65 78 2e 20 49 74 20 6d 61 79 20  e index. It may 
2c733 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20  also contain.   
2c734 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 65 71   **         ineq
2c735 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2c736 74 73 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20  ts (>, <, >= or 
2c737 3c 3d 29 20 6f 6e 20 74 68 65 20 69 6e 64 65 78  <=) on the index
2c738 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  ed.    **       
2c739 20 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 6d    column that im
2c73a 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77  mediately follow
2c73b 73 20 74 68 65 20 4e 20 65 71 75 61 6c 69 74 69  s the N equaliti
2c73c 65 73 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 2a 2a  es. Only .    **
2c73d 20 20 20 20 20 20 20 20 20 74 68 65 20 72 69 67           the rig
2c73e 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 63  ht-most column c
2c73f 61 6e 20 62 65 20 61 6e 20 69 6e 65 71 75 61 6c  an be an inequal
2c740 69 74 79 20 2d 20 74 68 65 20 72 65 73 74 20 6d  ity - the rest m
2c741 75 73 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ust.    **      
2c742 20 20 20 75 73 65 20 74 68 65 20 22 3d 3d 22 20     use the "==" 
2c743 61 6e 64 20 22 49 4e 22 20 6f 70 65 72 61 74 6f  and "IN" operato
2c744 72 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  rs. For example,
2c745 20 69 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20   if the .    ** 
2c746 20 20 20 20 20 20 20 20 69 6e 64 65 78 20 69 73          index is
2c747 20 6f 6e 20 28 78 2c 79 2c 7a 29 2c 20 74 68 65   on (x,y,z), the
2c748 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
2c749 63 6c 61 75 73 65 73 20 61 72 65 20 61 6c 6c 20  clauses are all 
2c74a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2c74b 6f 70 74 69 6d 69 7a 65 64 3a 0a 20 20 20 20 2a  optimized:.    *
2c74c 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
2c74d 20 20 20 20 78 3d 35 0a 20 20 20 20 2a 2a 20 20      x=5.    **  
2c74e 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e            x=5 AN
2c74f 44 20 79 3d 31 30 0a 20 20 20 20 2a 2a 20 20 20  D y=10.    **   
2c750 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44           x=5 AND
2c751 20 79 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20 20   y<10.    **    
2c752 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
2c753 79 3e 35 20 41 4e 44 20 79 3c 31 30 0a 20 20 20  y>5 AND y<10.   
2c754 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78   **            x
2c755 3d 35 20 41 4e 44 20 79 3d 35 20 41 4e 44 20 7a  =5 AND y=5 AND z
2c756 3c 3d 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20  <=10.    **.    
2c757 2a 2a 20 20 20 20 20 20 20 20 20 54 68 65 20 7a  **         The z
2c758 3c 31 30 20 74 65 72 6d 20 6f 66 20 74 68 65 20  <10 term of the 
2c759 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6e 6e 6f 74  following cannot
2c75a 20 62 65 20 75 73 65 64 2c 20 6f 6e 6c 79 0a 20   be used, only. 
2c75b 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74 68     **         th
2c75c 65 20 78 3d 35 20 74 65 72 6d 3a 0a 20 20 20 20  e x=5 term:.    
2c75d 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
2c75e 20 20 20 20 20 78 3d 35 20 41 4e 44 20 7a 3c 31       x=5 AND z<1
2c75f 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  0.    **.    ** 
2c760 20 20 20 20 20 20 20 20 4e 20 6d 61 79 20 62 65          N may be
2c761 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65 20 61   zero if there a
2c762 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  re inequality co
2c763 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a  nstraints..    *
2c764 2a 20 20 20 20 20 20 20 20 20 49 66 20 74 68 65  *         If the
2c765 72 65 20 61 72 65 20 6e 6f 20 69 6e 65 71 75 61  re are no inequa
2c766 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2c767 2c 20 74 68 65 6e 20 4e 20 69 73 20 61 74 0a 20  , then N is at. 
2c768 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 65     **         le
2c769 61 73 74 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a  ast one..    **.
2c76a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54      **         T
2c76b 68 69 73 20 63 61 73 65 20 69 73 20 61 6c 73 6f  his case is also
2c76c 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 72 65   used when there
2c76d 20 61 72 65 20 6e 6f 20 57 48 45 52 45 20 63 6c   are no WHERE cl
2c76e 61 75 73 65 0a 20 20 20 20 2a 2a 20 20 20 20 20  ause.    **     
2c76f 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74 73 20      constraints 
2c770 62 75 74 20 61 6e 20 69 6e 64 65 78 20 69 73 20  but an index is 
2c771 73 65 6c 65 63 74 65 64 20 61 6e 79 77 61 79 2c  selected anyway,
2c772 20 69 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a   in order.    **
2c773 20 20 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63           to forc
2c774 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64  e the output ord
2c775 65 72 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f  er to conform to
2c776 20 61 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20 20   an ORDER BY..  
2c777 20 20 2a 2f 20 20 0a 20 20 20 20 69 6e 74 20 61    */  .    int a
2c778 53 74 61 72 74 4f 70 5b 5d 20 3d 20 7b 0a 20 20  StartOp[] = {.  
2c779 20 20 20 20 30 2c 0a 20 20 20 20 20 20 30 2c 0a      0,.      0,.
2c77a 20 20 20 20 20 20 4f 50 5f 52 65 77 69 6e 64 2c        OP_Rewind,
2c77b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 3a             /* 2:
2c77c 20 28 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61   (!start_constra
2c77d 69 6e 74 73 20 26 26 20 73 74 61 72 74 45 71 20  ints && startEq 
2c77e 26 26 20 20 21 62 52 65 76 29 20 2a 2f 0a 20 20  &&  !bRev) */.  
2c77f 20 20 20 20 4f 50 5f 4c 61 73 74 2c 20 20 20 20      OP_Last,    
2c780 20 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 28           /* 3: (
2c781 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e  !start_constrain
2c782 74 73 20 26 26 20 73 74 61 72 74 45 71 20 26 26  ts && startEq &&
2c783 20 20 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20     bRev) */.    
2c784 20 20 4f 50 5f 53 65 65 6b 47 74 2c 20 20 20 20    OP_SeekGt,    
2c785 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 28 73 74         /* 4: (st
2c786 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20  art_constraints 
2c787 20 26 26 20 21 73 74 61 72 74 45 71 20 26 26 20   && !startEq && 
2c788 21 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20  !bRev) */.      
2c789 4f 50 5f 53 65 65 6b 4c 74 2c 20 20 20 20 20 20  OP_SeekLt,      
2c78a 20 20 20 20 20 2f 2a 20 35 3a 20 28 73 74 61 72       /* 5: (star
2c78b 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26  t_constraints  &
2c78c 26 20 21 73 74 61 72 74 45 71 20 26 26 20 20 62  & !startEq &&  b
2c78d 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50  Rev) */.      OP
2c78e 5f 53 65 65 6b 47 65 2c 20 20 20 20 20 20 20 20  _SeekGe,        
2c78f 20 20 20 2f 2a 20 36 3a 20 28 73 74 61 72 74 5f     /* 6: (start_
2c790 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20  constraints  && 
2c791 20 73 74 61 72 74 45 71 20 26 26 20 21 62 52 65   startEq && !bRe
2c792 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53  v) */.      OP_S
2c793 65 65 6b 4c 65 20 20 20 20 20 20 20 20 20 20 20  eekLe           
2c794 20 2f 2a 20 37 3a 20 28 73 74 61 72 74 5f 63 6f   /* 7: (start_co
2c795 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 20 73  nstraints  &&  s
2c796 74 61 72 74 45 71 20 26 26 20 20 62 52 65 76 29  tartEq &&  bRev)
2c797 20 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69   */.    };.    i
2c798 6e 74 20 61 45 6e 64 4f 70 5b 5d 20 3d 20 7b 0a  nt aEndOp[] = {.
2c799 20 20 20 20 20 20 4f 50 5f 4e 6f 6f 70 2c 20 20        OP_Noop,  
2c79a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 3a             /* 0:
2c79b 20 28 21 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e   (!end_constrain
2c79c 74 73 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f  ts) */.      OP_
2c79d 49 64 78 47 45 2c 20 20 20 20 20 20 20 20 20 20  IdxGE,          
2c79e 20 20 2f 2a 20 31 3a 20 28 65 6e 64 5f 63 6f 6e    /* 1: (end_con
2c79f 73 74 72 61 69 6e 74 73 20 26 26 20 21 62 52 65  straints && !bRe
2c7a0 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 49  v) */.      OP_I
2c7a1 64 78 4c 54 20 20 20 20 20 20 20 20 20 20 20 20  dxLT            
2c7a2 20 2f 2a 20 32 3a 20 28 65 6e 64 5f 63 6f 6e 73   /* 2: (end_cons
2c7a3 74 72 61 69 6e 74 73 20 26 26 20 62 52 65 76 29  traints && bRev)
2c7a4 20 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69   */.    };.    i
2c7a5 6e 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d  nt nEq = pLevel-
2c7a6 3e 70 6c 61 6e 2e 6e 45 71 3b 0a 20 20 20 20 69  >plan.nEq;.    i
2c7a7 6e 74 20 69 73 4d 69 6e 51 75 65 72 79 20 3d 20  nt isMinQuery = 
2c7a8 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  0;          /* I
2c7a9 66 20 74 68 69 73 20 69 73 20 61 6e 20 6f 70 74  f this is an opt
2c7aa 69 6d 69 7a 65 64 20 53 45 4c 45 43 54 20 6d 69  imized SELECT mi
2c7ab 6e 28 78 29 2e 2e 20 2a 2f 0a 20 20 20 20 69 6e  n(x).. */.    in
2c7ac 74 20 72 65 67 42 61 73 65 3b 20 20 20 20 20 20  t regBase;      
2c7ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61             /* Ba
2c7ae 73 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64  se register hold
2c7af 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 76  ing constraint v
2c7b0 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74  alues */.    int
2c7b1 20 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   r1;            
2c7b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
2c7b3 70 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  p register */.  
2c7b4 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 52 61    WhereTerm *pRa
2c7b5 6e 67 65 53 74 61 72 74 20 3d 20 30 3b 20 20 2f  ngeStart = 0;  /
2c7b6 2a 20 49 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  * Inequality con
2c7b7 73 74 72 61 69 6e 74 20 61 74 20 72 61 6e 67 65  straint at range
2c7b8 20 73 74 61 72 74 20 2a 2f 0a 20 20 20 20 57 68   start */.    Wh
2c7b9 65 72 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 45  ereTerm *pRangeE
2c7ba 6e 64 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 6e  nd = 0;    /* In
2c7bb 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2c7bc 69 6e 74 20 61 74 20 72 61 6e 67 65 20 65 6e 64  int at range end
2c7bd 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 74 61 72   */.    int star
2c7be 74 45 71 3b 20 20 20 20 20 20 20 20 20 20 20 20  tEq;            
2c7bf 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2c7c0 72 61 6e 67 65 20 73 74 61 72 74 20 75 73 65 73  range start uses
2c7c1 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a 2f   ==, >= or <= */
2c7c2 0a 20 20 20 20 69 6e 74 20 65 6e 64 45 71 3b 20  .    int endEq; 
2c7c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c7c4 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e    /* True if ran
2c7c5 67 65 20 65 6e 64 20 75 73 65 73 20 3d 3d 2c 20  ge end uses ==, 
2c7c6 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20  >= or <= */.    
2c7c7 69 6e 74 20 73 74 61 72 74 5f 63 6f 6e 73 74 72  int start_constr
2c7c8 61 69 6e 74 73 3b 20 20 20 20 20 20 20 2f 2a 20  aints;       /* 
2c7c9 53 74 61 72 74 20 6f 66 20 72 61 6e 67 65 20 69  Start of range i
2c7ca 73 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 2a 2f  s constrained */
2c7cb 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72  .    int nConstr
2c7cc 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  aint;           
2c7cd 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
2c7ce 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20  onstraint terms 
2c7cf 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  */.    Index *pI
2c7d0 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  dx;         /* T
2c7d1 68 65 20 69 6e 64 65 78 20 77 65 20 77 69 6c 6c  he index we will
2c7d2 20 62 65 20 75 73 69 6e 67 20 2a 2f 0a 20 20 20   be using */.   
2c7d3 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20   int iIdxCur;   
2c7d4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
2c7d5 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65  E cursor for the
2c7d6 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e   index */.    in
2c7d7 74 20 6e 45 78 74 72 61 52 65 67 20 3d 20 30 3b  t nExtraReg = 0;
2c7d8 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2c7d9 65 78 74 72 61 20 72 65 67 69 73 74 65 72 73 20  extra registers 
2c7da 6e 65 65 64 65 64 20 2a 2f 0a 20 20 20 20 69 6e  needed */.    in
2c7db 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
2c7dc 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f     /* Instructio
2c7dd 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 0a 20 20 20  n opcode */..   
2c7de 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e   pIdx = pLevel->
2c7df 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20 20  plan.u.pIdx;.   
2c7e0 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65   iIdxCur = pLeve
2c7e1 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20  l->iIdxCur;.    
2c7e2 6b 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  k = pIdx->aiColu
2c7e3 6d 6e 5b 6e 45 71 5d 3b 20 20 20 20 20 2f 2a 20  mn[nEq];     /* 
2c7e4 43 6f 6c 75 6d 6e 20 66 6f 72 20 69 6e 65 71 75  Column for inequ
2c7e5 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2c7e6 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20  s */..    /* If 
2c7e7 74 68 69 73 20 6c 6f 6f 70 20 73 61 74 69 73 66  this loop satisf
2c7e8 69 65 73 20 61 20 73 6f 72 74 20 6f 72 64 65 72  ies a sort order
2c7e9 20 28 70 4f 72 64 65 72 42 79 29 20 72 65 71 75   (pOrderBy) requ
2c7ea 65 73 74 20 74 68 61 74 20 0a 20 20 20 20 2a 2a  est that .    **
2c7eb 20 77 61 73 20 70 61 73 73 65 64 20 74 6f 20 74   was passed to t
2c7ec 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  his function to 
2c7ed 69 6d 70 6c 65 6d 65 6e 74 20 61 20 22 53 45 4c  implement a "SEL
2c7ee 45 43 54 20 6d 69 6e 28 78 29 20 2e 2e 2e 22 20  ECT min(x) ..." 
2c7ef 0a 20 20 20 20 2a 2a 20 71 75 65 72 79 2c 20 74  .    ** query, t
2c7f0 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 77  hen the caller w
2c7f1 69 6c 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74  ill only allow t
2c7f2 68 65 20 6c 6f 6f 70 20 74 6f 20 72 75 6e 20 66  he loop to run f
2c7f3 6f 72 0a 20 20 20 20 2a 2a 20 61 20 73 69 6e 67  or.    ** a sing
2c7f4 6c 65 20 69 74 65 72 61 74 69 6f 6e 2e 20 54 68  le iteration. Th
2c7f5 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  is means that th
2c7f6 65 20 66 69 72 73 74 20 72 6f 77 20 72 65 74 75  e first row retu
2c7f7 72 6e 65 64 0a 20 20 20 20 2a 2a 20 73 68 6f 75  rned.    ** shou
2c7f8 6c 64 20 6e 6f 74 20 68 61 76 65 20 61 20 4e 55  ld not have a NU
2c7f9 4c 4c 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  LL value stored 
2c7fa 69 6e 20 27 78 27 2e 20 49 66 20 63 6f 6c 75 6d  in 'x'. If colum
2c7fb 6e 20 27 78 27 20 69 73 0a 20 20 20 20 2a 2a 20  n 'x' is.    ** 
2c7fc 74 68 65 20 66 69 72 73 74 20 6f 6e 65 20 61 66  the first one af
2c7fd 74 65 72 20 74 68 65 20 6e 45 71 20 65 71 75 61  ter the nEq equa
2c7fe 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2c7ff 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2c 0a 20   in the index,. 
2c800 20 20 20 2a 2a 20 74 68 69 73 20 72 65 71 75 69     ** this requi
2c801 72 65 73 20 73 6f 6d 65 20 73 70 65 63 69 61 6c  res some special
2c802 20 68 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 2a   handling..    *
2c803 2f 0a 20 20 20 20 69 66 28 20 28 77 63 74 72 6c  /.    if( (wctrl
2c804 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45  Flags&WHERE_ORDE
2c805 52 42 59 5f 4d 49 4e 29 21 3d 30 0a 20 20 20 20  RBY_MIN)!=0.    
2c806 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61   && (pLevel->pla
2c807 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f  n.wsFlags&WHERE_
2c808 4f 52 44 45 52 42 59 29 0a 20 20 20 20 20 26 26  ORDERBY).     &&
2c809 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e   (pIdx->nColumn>
2c80a 6e 45 71 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  nEq).    ){.    
2c80b 20 20 2f 2a 20 61 73 73 65 72 74 28 20 70 4f 72    /* assert( pOr
2c80c 64 65 72 42 79 2d 3e 6e 45 78 70 72 3d 3d 31 20  derBy->nExpr==1 
2c80d 29 3b 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 61  ); */.      /* a
2c80e 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d  ssert( pOrderBy-
2c80f 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 69 43 6f  >a[0].pExpr->iCo
2c810 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f  lumn==pIdx->aiCo
2c811 6c 75 6d 6e 5b 6e 45 71 5d 20 29 3b 20 2a 2f 0a  lumn[nEq] ); */.
2c812 20 20 20 20 20 20 69 73 4d 69 6e 51 75 65 72 79        isMinQuery
2c813 20 3d 20 31 3b 0a 20 20 20 20 20 20 6e 45 78 74   = 1;.      nExt
2c814 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d  raReg = 1;.    }
2c815 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 6e  ..    /* Find an
2c816 79 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  y inequality con
2c817 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 66 6f  straint terms fo
2c818 72 20 74 68 65 20 73 74 61 72 74 20 61 6e 64 20  r the start and 
2c819 65 6e 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74  end .    ** of t
2c81a 68 65 20 72 61 6e 67 65 2e 20 0a 20 20 20 20 2a  he range. .    *
2c81b 2f 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  /.    if( pLevel
2c81c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2c81d 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54   WHERE_TOP_LIMIT
2c81e 20 29 7b 0a 20 20 20 20 20 20 70 52 61 6e 67 65   ){.      pRange
2c81f 45 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  End = findTerm(p
2c820 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74  WC, iCur, k, not
2c821 52 65 61 64 79 2c 20 28 57 4f 5f 4c 54 7c 57 4f  Ready, (WO_LT|WO
2c822 5f 4c 45 29 2c 20 70 49 64 78 29 3b 0a 20 20 20  _LE), pIdx);.   
2c823 20 20 20 6e 45 78 74 72 61 52 65 67 20 3d 20 31     nExtraReg = 1
2c824 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2c825 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2c826 6c 61 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d  lags & WHERE_BTM
2c827 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 20 20  _LIMIT ){.      
2c828 70 52 61 6e 67 65 53 74 61 72 74 20 3d 20 66 69  pRangeStart = fi
2c829 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
2c82a 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20 28  , k, notReady, (
2c82b 57 4f 5f 47 54 7c 57 4f 5f 47 45 29 2c 20 70 49  WO_GT|WO_GE), pI
2c82c 64 78 29 3b 0a 20 20 20 20 20 20 6e 45 78 74 72  dx);.      nExtr
2c82d 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  aReg = 1;.    }.
2c82e 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  .    /* Generate
2c82f 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74   code to evaluat
2c830 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74  e all constraint
2c831 20 74 65 72 6d 73 20 75 73 69 6e 67 20 3d 3d 20   terms using == 
2c832 6f 72 20 49 4e 0a 20 20 20 20 2a 2a 20 61 6e 64  or IN.    ** and
2c833 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65   store the value
2c834 73 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73  s of those terms
2c835 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20   in an array of 
2c836 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a  registers.    **
2c837 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67   starting at reg
2c838 42 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  Base..    */.   
2c839 20 72 65 67 42 61 73 65 20 3d 20 63 6f 64 65 41   regBase = codeA
2c83a 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28  llEqualityTerms(
2c83b 70 50 61 72 73 65 2c 20 70 4c 65 76 65 6c 2c 20  pParse, pLevel, 
2c83c 70 57 43 2c 20 6e 6f 74 52 65 61 64 79 2c 20 6e  pWC, notReady, n
2c83d 45 78 74 72 61 52 65 67 29 3b 0a 20 20 20 20 61  ExtraReg);.    a
2c83e 64 64 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c 2d  ddrNxt = pLevel-
2c83f 3e 61 64 64 72 4e 78 74 3b 0a 0a 0a 20 20 20 20  >addrNxt;...    
2c840 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69  /* If we are doi
2c841 6e 67 20 61 20 72 65 76 65 72 73 65 20 6f 72 64  ng a reverse ord
2c842 65 72 20 73 63 61 6e 20 6f 6e 20 61 6e 20 61 73  er scan on an as
2c843 63 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20 6f  cending index, o
2c844 72 0a 20 20 20 20 2a 2a 20 61 20 66 6f 72 77 61  r.    ** a forwa
2c845 72 64 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e  rd order scan on
2c846 20 61 20 64 65 73 63 65 6e 64 69 6e 67 20 69 6e   a descending in
2c847 64 65 78 2c 20 69 6e 74 65 72 63 68 61 6e 67 65  dex, interchange
2c848 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 74 61   the .    ** sta
2c849 72 74 20 61 6e 64 20 65 6e 64 20 74 65 72 6d 73  rt and end terms
2c84a 20 28 70 52 61 6e 67 65 53 74 61 72 74 20 61 6e   (pRangeStart an
2c84b 64 20 70 52 61 6e 67 65 45 6e 64 29 2e 0a 20 20  d pRangeEnd)..  
2c84c 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 52 65    */.    if( bRe
2c84d 76 3d 3d 28 70 49 64 78 2d 3e 61 53 6f 72 74 4f  v==(pIdx->aSortO
2c84e 72 64 65 72 5b 6e 45 71 5d 3d 3d 53 51 4c 49 54  rder[nEq]==SQLIT
2c84f 45 5f 53 4f 5f 41 53 43 29 20 29 7b 0a 20 20 20  E_SO_ASC) ){.   
2c850 20 20 20 53 57 41 50 28 57 68 65 72 65 54 65 72     SWAP(WhereTer
2c851 6d 20 2a 2c 20 70 52 61 6e 67 65 45 6e 64 2c 20  m *, pRangeEnd, 
2c852 70 52 61 6e 67 65 53 74 61 72 74 29 3b 0a 20 20  pRangeStart);.  
2c853 20 20 7d 0a 0a 20 20 20 20 74 65 73 74 63 61 73    }..    testcas
2c854 65 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26  e( pRangeStart &
2c855 26 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65  & pRangeStart->e
2c856 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45  Operator & WO_LE
2c857 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2c858 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26  ( pRangeStart &&
2c859 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f   pRangeStart->eO
2c85a 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 20  perator & WO_GE 
2c85b 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2c85c 20 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52   pRangeEnd && pR
2c85d 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74  angeEnd->eOperat
2c85e 6f 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a 20 20  or & WO_LE );.  
2c85f 20 20 74 65 73 74 63 61 73 65 28 20 70 52 61 6e    testcase( pRan
2c860 67 65 45 6e 64 20 26 26 20 70 52 61 6e 67 65 45  geEnd && pRangeE
2c861 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  nd->eOperator & 
2c862 57 4f 5f 47 45 20 29 3b 0a 20 20 20 20 73 74 61  WO_GE );.    sta
2c863 72 74 45 71 20 3d 20 21 70 52 61 6e 67 65 53 74  rtEq = !pRangeSt
2c864 61 72 74 20 7c 7c 20 70 52 61 6e 67 65 53 74 61  art || pRangeSta
2c865 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rt->eOperator & 
2c866 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a 20  (WO_LE|WO_GE);. 
2c867 20 20 20 65 6e 64 45 71 20 3d 20 20 20 21 70 52     endEq =   !pR
2c868 61 6e 67 65 45 6e 64 20 7c 7c 20 70 52 61 6e 67  angeEnd || pRang
2c869 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20  eEnd->eOperator 
2c86a 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b  & (WO_LE|WO_GE);
2c86b 0a 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74  .    start_const
2c86c 72 61 69 6e 74 73 20 3d 20 70 52 61 6e 67 65 53  raints = pRangeS
2c86d 74 61 72 74 20 7c 7c 20 6e 45 71 3e 30 3b 0a 0a  tart || nEq>0;..
2c86e 20 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20      /* Seek the 
2c86f 69 6e 64 65 78 20 63 75 72 73 6f 72 20 74 6f 20  index cursor to 
2c870 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
2c871 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6e   range. */.    n
2c872 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71  Constraint = nEq
2c873 3b 0a 20 20 20 20 69 66 28 20 70 52 61 6e 67 65  ;.    if( pRange
2c874 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 73  Start ){.      s
2c875 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2c876 50 61 72 73 65 2c 20 70 52 61 6e 67 65 53 74 61  Parse, pRangeSta
2c877 72 74 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  rt->pExpr->pRigh
2c878 74 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b  t, regBase+nEq);
2c879 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c87a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2c87b 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6e  sNull, regBase+n
2c87c 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20 20  Eq, addrNxt);.  
2c87d 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b      nConstraint+
2c87e 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  +;.    }else if(
2c87f 20 69 73 4d 69 6e 51 75 65 72 79 20 29 7b 0a 20   isMinQuery ){. 
2c880 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c881 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2c882 6c 2c 20 30 2c 20 72 65 67 42 61 73 65 2b 6e 45  l, 0, regBase+nE
2c883 71 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74  q);.      nConst
2c884 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 73  raint++;.      s
2c885 74 61 72 74 45 71 20 3d 20 30 3b 0a 20 20 20 20  tartEq = 0;.    
2c886 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69    start_constrai
2c887 6e 74 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  nts = 1;.    }. 
2c888 20 20 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69     codeApplyAffi
2c889 6e 69 74 79 28 70 50 61 72 73 65 2c 20 72 65 67  nity(pParse, reg
2c88a 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e  Base, nConstrain
2c88b 74 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 6f 70  t, pIdx);.    op
2c88c 20 3d 20 61 53 74 61 72 74 4f 70 5b 28 73 74 61   = aStartOp[(sta
2c88d 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3c 3c  rt_constraints<<
2c88e 32 29 20 2b 20 28 73 74 61 72 74 45 71 3c 3c 31  2) + (startEq<<1
2c88f 29 20 2b 20 62 52 65 76 5d 3b 0a 20 20 20 20 61  ) + bRev];.    a
2c890 73 73 65 72 74 28 20 6f 70 21 3d 30 20 29 3b 0a  ssert( op!=0 );.
2c891 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
2c892 3d 3d 4f 50 5f 52 65 77 69 6e 64 20 29 3b 0a 20  ==OP_Rewind );. 
2c893 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2c894 3d 4f 50 5f 4c 61 73 74 20 29 3b 0a 20 20 20 20  =OP_Last );.    
2c895 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
2c896 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 74  _SeekGt );.    t
2c897 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
2c898 53 65 65 6b 47 65 20 29 3b 0a 20 20 20 20 74 65  SeekGe );.    te
2c899 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53  stcase( op==OP_S
2c89a 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 74 65 73  eekLe );.    tes
2c89b 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65  tcase( op==OP_Se
2c89c 65 6b 4c 74 20 29 3b 0a 20 20 20 20 73 71 6c 69  ekLt );.    sqli
2c89d 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2c89e 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 61 64   op, iIdxCur, ad
2c89f 64 72 4e 78 74 2c 20 72 65 67 42 61 73 65 2c 20  drNxt, regBase, 
2c8a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c8a1 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e         SQLITE_IN
2c8a2 54 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73 74 72  T_TO_PTR(nConstr
2c8a3 61 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33 32 29  aint), P4_INT32)
2c8a4 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74  ;..    /* Load t
2c8a5 68 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  he value for the
2c8a6 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73   inequality cons
2c8a7 74 72 61 69 6e 74 20 61 74 20 74 68 65 20 65 6e  traint at the en
2c8a8 64 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  d of the.    ** 
2c8a9 72 61 6e 67 65 20 28 69 66 20 61 6e 79 29 2e 0a  range (if any)..
2c8aa 20 20 20 20 2a 2f 0a 20 20 20 20 6e 43 6f 6e 73      */.    nCons
2c8ab 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20 20  traint = nEq;.  
2c8ac 20 20 69 66 28 20 70 52 61 6e 67 65 45 6e 64 20    if( pRangeEnd 
2c8ad 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2c8ae 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28  ExprCacheRemove(
2c8af 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2b  pParse, regBase+
2c8b0 6e 45 71 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  nEq);.      sqli
2c8b1 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
2c8b2 73 65 2c 20 70 52 61 6e 67 65 45 6e 64 2d 3e 70  se, pRangeEnd->p
2c8b3 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 72 65  Expr->pRight, re
2c8b4 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20  gBase+nEq);.    
2c8b5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c8b6 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c  Op2(v, OP_IsNull
2c8b7 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 2c 20 61  , regBase+nEq, a
2c8b8 64 64 72 4e 78 74 29 3b 0a 20 20 20 20 20 20 63  ddrNxt);.      c
2c8b9 6f 64 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79  odeApplyAffinity
2c8ba 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65  (pParse, regBase
2c8bb 2c 20 6e 45 71 2b 31 2c 20 70 49 64 78 29 3b 0a  , nEq+1, pIdx);.
2c8bc 20 20 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e        nConstrain
2c8bd 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  t++;.    }..    
2c8be 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 6c 6f  /* Top of the lo
2c8bf 6f 70 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 70  op body */.    p
2c8c0 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 71 6c 69  Level->p2 = sqli
2c8c1 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2c8c2 64 72 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  dr(v);..    /* C
2c8c3 68 65 63 6b 20 69 66 20 74 68 65 20 69 6e 64 65  heck if the inde
2c8c4 78 20 63 75 72 73 6f 72 20 69 73 20 70 61 73 74  x cursor is past
2c8c5 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
2c8c6 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6f 70  range. */.    op
2c8c7 20 3d 20 61 45 6e 64 4f 70 5b 28 70 52 61 6e 67   = aEndOp[(pRang
2c8c8 65 45 6e 64 20 7c 7c 20 6e 45 71 29 20 2a 20 28  eEnd || nEq) * (
2c8c9 31 20 2b 20 62 52 65 76 29 5d 3b 0a 20 20 20 20  1 + bRev)];.    
2c8ca 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
2c8cb 5f 4e 6f 6f 70 20 29 3b 0a 20 20 20 20 74 65 73  _Noop );.    tes
2c8cc 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49 64  tcase( op==OP_Id
2c8cd 78 47 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63  xGE );.    testc
2c8ce 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 4c  ase( op==OP_IdxL
2c8cf 54 20 29 3b 0a 20 20 20 20 69 66 28 20 6f 70 21  T );.    if( op!
2c8d0 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20  =OP_Noop ){.    
2c8d1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c8d2 4f 70 34 28 76 2c 20 6f 70 2c 20 69 49 64 78 43  Op4(v, op, iIdxC
2c8d3 75 72 2c 20 61 64 64 72 4e 78 74 2c 20 72 65 67  ur, addrNxt, reg
2c8d4 42 61 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Base,.          
2c8d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
2c8d6 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
2c8d7 6e 43 6f 6e 73 74 72 61 69 6e 74 29 2c 20 50 34  nConstraint), P4
2c8d8 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 20 20 73  _INT32);.      s
2c8d9 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2c8da 50 35 28 76 2c 20 65 6e 64 45 71 21 3d 62 52 65  P5(v, endEq!=bRe
2c8db 76 20 3f 31 3a 30 29 3b 0a 20 20 20 20 7d 0a 0a  v ?1:0);.    }..
2c8dc 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
2c8dd 61 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63  are inequality c
2c8de 6f 6e 73 74 72 61 69 6e 74 73 2c 20 63 68 65 63  onstraints, chec
2c8df 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  k that the value
2c8e0 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 74  .    ** of the t
2c8e1 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74  able column that
2c8e2 20 74 68 65 20 69 6e 65 71 75 61 6c 69 74 79 20   the inequality 
2c8e3 63 6f 6e 74 72 61 69 6e 73 20 69 73 20 6e 6f 74  contrains is not
2c8e4 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 49 66   NULL..    ** If
2c8e5 20 69 74 20 69 73 2c 20 6a 75 6d 70 20 74 6f 20   it is, jump to 
2c8e6 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 69  the next iterati
2c8e7 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a  on of the loop..
2c8e8 20 20 20 20 2a 2f 0a 20 20 20 20 72 31 20 3d 20      */.    r1 = 
2c8e9 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2c8ea 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 74  g(pParse);.    t
2c8eb 65 73 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d  estcase( pLevel-
2c8ec 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2c8ed 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20  WHERE_BTM_LIMIT 
2c8ee 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2c8ef 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2c8f0 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 54 4f  Flags & WHERE_TO
2c8f1 50 5f 4c 49 4d 49 54 20 29 3b 0a 20 20 20 20 69  P_LIMIT );.    i
2c8f2 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  f( pLevel->plan.
2c8f3 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52 45  wsFlags & (WHERE
2c8f4 5f 42 54 4d 5f 4c 49 4d 49 54 7c 57 48 45 52 45  _BTM_LIMIT|WHERE
2c8f5 5f 54 4f 50 5f 4c 49 4d 49 54 29 20 29 7b 0a 20  _TOP_LIMIT) ){. 
2c8f6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c8f7 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
2c8f8 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 6e 45  umn, iIdxCur, nE
2c8f9 71 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  q, r1);.      sq
2c8fa 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c8fb 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 31  v, OP_IsNull, r1
2c8fc 2c 20 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20  , addrCont);.   
2c8fd 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65   }.    sqlite3Re
2c8fe 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2c8ff 72 73 65 2c 20 72 31 29 3b 0a 0a 20 20 20 20 2f  rse, r1);..    /
2c900 2a 20 53 65 65 6b 20 74 68 65 20 74 61 62 6c 65  * Seek the table
2c901 20 63 75 72 73 6f 72 2c 20 69 66 20 72 65 71 75   cursor, if requ
2c902 69 72 65 64 20 2a 2f 0a 20 20 20 20 64 69 73 61  ired */.    disa
2c903 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20  bleTerm(pLevel, 
2c904 70 52 61 6e 67 65 53 74 61 72 74 29 3b 0a 20 20  pRangeStart);.  
2c905 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c    disableTerm(pL
2c906 65 76 65 6c 2c 20 70 52 61 6e 67 65 45 6e 64 29  evel, pRangeEnd)
2c907 3b 0a 20 20 20 20 69 66 28 20 21 6f 6d 69 74 54  ;.    if( !omitT
2c908 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 52  able ){.      iR
2c909 6f 77 69 64 52 65 67 20 3d 20 69 52 65 6c 65 61  owidReg = iRelea
2c90a 73 65 52 65 67 20 3d 20 73 71 6c 69 74 65 33 47  seReg = sqlite3G
2c90b 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
2c90c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c90d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c90e 5f 49 64 78 52 6f 77 69 64 2c 20 69 49 64 78 43  _IdxRowid, iIdxC
2c90f 75 72 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a  ur, iRowidReg);.
2c910 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2c911 72 43 61 63 68 65 53 74 6f 72 65 28 70 50 61 72  rCacheStore(pPar
2c912 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52  se, iCur, -1, iR
2c913 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20 20  owidReg);.      
2c914 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c915 32 28 76 2c 20 4f 50 5f 53 65 65 6b 2c 20 69 43  2(v, OP_Seek, iC
2c916 75 72 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 20  ur, iRowidReg); 
2c917 20 2f 2a 20 44 65 66 65 72 72 65 64 20 73 65 65   /* Deferred see
2c918 6b 20 2a 2f 0a 20 20 20 20 7d 0a 0a 20 20 20 20  k */.    }..    
2c919 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20 69 6e  /* Record the in
2c91a 73 74 72 75 63 74 69 6f 6e 20 75 73 65 64 20 74  struction used t
2c91b 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20  o terminate the 
2c91c 6c 6f 6f 70 2e 20 44 69 73 61 62 6c 65 20 0a 20  loop. Disable . 
2c91d 20 20 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75     ** WHERE clau
2c91e 73 65 20 74 65 72 6d 73 20 6d 61 64 65 20 72 65  se terms made re
2c91f 64 75 6e 64 61 6e 74 20 62 79 20 74 68 65 20 69  dundant by the i
2c920 6e 64 65 78 20 72 61 6e 67 65 20 73 63 61 6e 2e  ndex range scan.
2c921 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c 65 76  .    */.    pLev
2c922 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76 20 3f 20  el->op = bRev ? 
2c923 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f 4e 65 78  OP_Prev : OP_Nex
2c924 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  t;.    pLevel->p
2c925 31 20 3d 20 69 49 64 78 43 75 72 3b 0a 20 20 7d  1 = iIdxCur;.  }
2c926 65 6c 73 65 0a 0a 23 69 66 6e 64 65 66 20 53 51  else..#ifndef SQ
2c927 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54  LITE_OMIT_OR_OPT
2c928 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20  IMIZATION.  if( 
2c929 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2c92a 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4d 55 4c  lags & WHERE_MUL
2c92b 54 49 5f 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20  TI_OR ){.    /* 
2c92c 43 61 73 65 20 34 3a 20 20 54 77 6f 20 6f 72 20  Case 4:  Two or 
2c92d 6d 6f 72 65 20 73 65 70 61 72 61 74 65 6c 79 20  more separately 
2c92e 69 6e 64 65 78 65 64 20 74 65 72 6d 73 20 63 6f  indexed terms co
2c92f 6e 6e 65 63 74 65 64 20 62 79 20 4f 52 0a 20 20  nnected by OR.  
2c930 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 61 6d    **.    ** Exam
2c931 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ple:.    **.    
2c932 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  **   CREATE TABL
2c933 45 20 74 31 28 61 2c 62 2c 63 2c 64 29 3b 0a 20  E t1(a,b,c,d);. 
2c934 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49     **   CREATE I
2c935 4e 44 45 58 20 69 31 20 4f 4e 20 74 31 28 61 29  NDEX i1 ON t1(a)
2c936 3b 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54  ;.    **   CREAT
2c937 45 20 49 4e 44 45 58 20 69 32 20 4f 4e 20 74 31  E INDEX i2 ON t1
2c938 28 62 29 3b 0a 20 20 20 20 2a 2a 20 20 20 43 52  (b);.    **   CR
2c939 45 41 54 45 20 49 4e 44 45 58 20 69 33 20 4f 4e  EATE INDEX i3 ON
2c93a 20 74 31 28 63 29 3b 0a 20 20 20 20 2a 2a 0a 20   t1(c);.    **. 
2c93b 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 2a     **   SELECT *
2c93c 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
2c93d 3d 35 20 4f 52 20 62 3d 37 20 4f 52 20 28 63 3d  =5 OR b=7 OR (c=
2c93e 31 31 20 41 4e 44 20 64 3d 31 33 29 0a 20 20 20  11 AND d=13).   
2c93f 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 74 68   **.    ** In th
2c940 65 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 72 65  e example, there
2c941 20 61 72 65 20 74 68 72 65 65 20 69 6e 64 65 78   are three index
2c942 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74  ed terms connect
2c943 65 64 20 62 79 20 4f 52 2e 0a 20 20 20 20 2a 2a  ed by OR..    **
2c944 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   The top of the 
2c945 6c 6f 6f 70 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  loop looks like 
2c946 74 68 69 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  this:.    **.   
2c947 20 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 75 6c   **          Nul
2c948 6c 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20  l       1       
2c949 20 20 20 20 20 20 20 20 20 23 20 5a 65 72 6f 20           # Zero 
2c94a 74 68 65 20 72 6f 77 73 65 74 20 69 6e 20 72 65  the rowset in re
2c94b 67 20 31 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  g 1.    **.    *
2c94c 2a 20 54 68 65 6e 2c 20 66 6f 72 20 65 61 63 68  * Then, for each
2c94d 20 69 6e 64 65 78 65 64 20 74 65 72 6d 2c 20 74   indexed term, t
2c94e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 2e 20 54 68  he following. Th
2c94f 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 0a 20  e arguments to. 
2c950 20 20 20 2a 2a 20 52 6f 77 53 65 74 54 65 73 74     ** RowSetTest
2c951 20 61 72 65 20 73 75 63 68 20 74 68 61 74 20 74   are such that t
2c952 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
2c953 63 75 72 72 65 6e 74 20 72 6f 77 20 69 73 20 69  current row is i
2c954 6e 73 65 72 74 65 64 0a 20 20 20 20 2a 2a 20 69  nserted.    ** i
2c955 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 20  nto the RowSet. 
2c956 49 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79  If it is already
2c957 20 70 72 65 73 65 6e 74 2c 20 63 6f 6e 74 72 6f   present, contro
2c958 6c 20 73 6b 69 70 73 20 74 68 65 0a 20 20 20 20  l skips the.    
2c959 2a 2a 20 47 6f 73 75 62 20 6f 70 63 6f 64 65 20  ** Gosub opcode 
2c95a 61 6e 64 20 6a 75 6d 70 73 20 73 74 72 61 69 67  and jumps straig
2c95b 68 74 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67  ht to the code g
2c95c 65 6e 65 72 61 74 65 64 20 62 79 20 57 68 65 72  enerated by Wher
2c95d 65 45 6e 64 28 29 2e 0a 20 20 20 20 2a 2a 0a 20  eEnd()..    **. 
2c95e 20 20 20 2a 2a 20 20 20 20 20 20 20 20 73 71 6c     **        sql
2c95f 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 3c  ite3WhereBegin(<
2c960 74 65 72 6d 3e 29 0a 20 20 20 20 2a 2a 20 20 20  term>).    **   
2c961 20 20 20 20 20 20 20 52 6f 77 53 65 74 54 65 73         RowSetTes
2c962 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
2c963 20 20 20 23 20 49 6e 73 65 72 74 20 72 6f 77 69     # Insert rowi
2c964 64 20 69 6e 74 6f 20 72 6f 77 73 65 74 0a 20 20  d into rowset.  
2c965 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 47 6f    **          Go
2c966 73 75 62 20 20 20 20 20 20 32 20 41 0a 20 20 20  sub      2 A.   
2c967 20 2a 2a 20 20 20 20 20 20 20 20 73 71 6c 69 74   **        sqlit
2c968 65 33 57 68 65 72 65 45 6e 64 28 29 0a 20 20 20  e3WhereEnd().   
2c969 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 6c 6c 6f   **.    ** Follo
2c96a 77 69 6e 67 20 74 68 65 20 61 62 6f 76 65 2c 20  wing the above, 
2c96b 63 6f 64 65 20 74 6f 20 74 65 72 6d 69 6e 61 74  code to terminat
2c96c 65 20 74 68 65 20 6c 6f 6f 70 2e 20 4c 61 62 65  e the loop. Labe
2c96d 6c 20 41 2c 20 74 68 65 20 74 61 72 67 65 74 0a  l A, the target.
2c96e 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 47 6f      ** of the Go
2c96f 73 75 62 20 61 62 6f 76 65 2c 20 6a 75 6d 70 73  sub above, jumps
2c970 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74   to the instruct
2c971 69 6f 6e 20 72 69 67 68 74 20 61 66 74 65 72 20  ion right after 
2c972 74 68 65 20 47 6f 74 6f 2e 0a 20 20 20 20 2a 2a  the Goto..    **
2c973 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2c974 20 4e 75 6c 6c 20 20 20 20 20 20 20 31 20 20 20   Null       1   
2c975 20 20 20 20 20 20 20 20 20 20 20 20 20 23 20 5a               # Z
2c976 65 72 6f 20 74 68 65 20 72 6f 77 73 65 74 20 69  ero the rowset i
2c977 6e 20 72 65 67 20 31 0a 20 20 20 20 2a 2a 20 20  n reg 1.    **  
2c978 20 20 20 20 20 20 20 20 47 6f 74 6f 20 20 20 20          Goto    
2c979 20 20 20 42 20 20 20 20 20 20 20 20 20 20 20 20     B            
2c97a 20 20 20 20 23 20 54 68 65 20 6c 6f 6f 70 20 69      # The loop i
2c97b 73 20 66 69 6e 69 73 68 65 64 2e 0a 20 20 20 20  s finished..    
2c97c 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
2c97d 41 3a 20 3c 6c 6f 6f 70 20 62 6f 64 79 3e 20 20  A: <loop body>  
2c97e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23                 #
2c97f 20 52 65 74 75 72 6e 20 64 61 74 61 2c 20 77 68   Return data, wh
2c980 61 74 65 76 65 72 2e 0a 20 20 20 20 2a 2a 0a 20  atever..    **. 
2c981 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 52     **          R
2c982 65 74 75 72 6e 20 20 20 20 20 32 20 20 20 20 20  eturn     2     
2c983 20 20 20 20 20 20 20 20 20 20 20 23 20 4a 75 6d             # Jum
2c984 70 20 62 61 63 6b 20 74 6f 20 74 68 65 20 47 6f  p back to the Go
2c985 73 75 62 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  sub.    **.    *
2c986 2a 20 20 20 20 20 20 20 42 3a 20 3c 61 66 74 65  *       B: <afte
2c987 72 20 74 68 65 20 6c 6f 6f 70 3e 0a 20 20 20 20  r the loop>.    
2c988 2a 2a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 57 68  **.    */.    Wh
2c989 65 72 65 43 6c 61 75 73 65 20 2a 70 4f 72 57 63  ereClause *pOrWc
2c98a 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 2d 63  ;    /* The OR-c
2c98b 6c 61 75 73 65 20 62 72 6f 6b 65 6e 20 6f 75 74  lause broken out
2c98c 20 69 6e 74 6f 20 73 75 62 74 65 72 6d 73 20 2a   into subterms *
2c98d 2f 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20  /.    WhereTerm 
2c98e 2a 70 46 69 6e 61 6c 3b 20 20 20 20 20 2f 2a 20  *pFinal;     /* 
2c98f 46 69 6e 61 6c 20 73 75 62 74 65 72 6d 20 77 69  Final subterm wi
2c990 74 68 69 6e 20 74 68 65 20 4f 52 2d 63 6c 61 75  thin the OR-clau
2c991 73 65 2e 20 2a 2f 0a 20 20 20 20 53 72 63 4c 69  se. */.    SrcLi
2c992 73 74 20 6f 6e 65 54 61 62 3b 20 20 20 20 20 20  st oneTab;      
2c993 20 20 2f 2a 20 53 68 6f 72 74 65 6e 65 64 20 74    /* Shortened t
2c994 61 62 6c 65 20 6c 69 73 74 20 2a 2f 0a 0a 20 20  able list */..  
2c995 20 20 69 6e 74 20 72 65 67 52 65 74 75 72 6e 20    int regReturn 
2c996 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2c997 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  ;           /* R
2c998 65 67 69 73 74 65 72 20 75 73 65 64 20 77 69 74  egister used wit
2c999 68 20 4f 50 5f 47 6f 73 75 62 20 2a 2f 0a 20 20  h OP_Gosub */.  
2c99a 20 20 69 6e 74 20 72 65 67 52 6f 77 73 65 74 3b    int regRowset;
2c99b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c99c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2c99d 65 67 69 73 74 65 72 20 66 6f 72 20 52 6f 77 53  egister for RowS
2c99e 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20  et object */.   
2c99f 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20   int regRowid;  
2c9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9a1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2c9a2 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72  gister holding r
2c9a3 6f 77 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  owid */.    int 
2c9a4 69 4c 6f 6f 70 42 6f 64 79 20 3d 20 73 71 6c 69  iLoopBody = sqli
2c9a5 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2c9a6 28 76 29 3b 20 20 2f 2a 20 53 74 61 72 74 20 6f  (v);  /* Start o
2c9a7 66 20 6c 6f 6f 70 20 62 6f 64 79 20 2a 2f 0a 20  f loop body */. 
2c9a8 20 20 20 69 6e 74 20 69 52 65 74 49 6e 69 74 3b     int iRetInit;
2c9a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9aa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c9ab 41 64 64 72 65 73 73 20 6f 66 20 72 65 67 52 65  Address of regRe
2c9ac 74 75 72 6e 20 69 6e 69 74 20 2a 2f 0a 20 20 20  turn init */.   
2c9ad 20 69 6e 74 20 69 69 3b 0a 20 20 20 0a 20 20 20   int ii;.   .   
2c9ae 20 70 54 65 72 6d 20 3d 20 70 4c 65 76 65 6c 2d   pTerm = pLevel-
2c9af 3e 70 6c 61 6e 2e 75 2e 70 54 65 72 6d 3b 0a 20  >plan.u.pTerm;. 
2c9b0 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d     assert( pTerm
2c9b1 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  !=0 );.    asser
2c9b2 74 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  t( pTerm->eOpera
2c9b3 74 6f 72 3d 3d 57 4f 5f 4f 52 20 29 3b 0a 20 20  tor==WO_OR );.  
2c9b4 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72 6d    assert( (pTerm
2c9b5 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
2c9b6 5f 4f 52 49 4e 46 4f 29 21 3d 30 20 29 3b 0a 20  _ORINFO)!=0 );. 
2c9b7 20 20 20 70 4f 72 57 63 20 3d 20 26 70 54 65 72     pOrWc = &pTer
2c9b8 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 77 63  m->u.pOrInfo->wc
2c9b9 3b 0a 20 20 20 20 70 46 69 6e 61 6c 20 3d 20 26  ;.    pFinal = &
2c9ba 70 4f 72 57 63 2d 3e 61 5b 70 4f 72 57 63 2d 3e  pOrWc->a[pOrWc->
2c9bb 6e 54 65 72 6d 2d 31 5d 3b 0a 0a 20 20 20 20 2f  nTerm-1];..    /
2c9bc 2a 20 53 65 74 20 75 70 20 61 20 53 72 63 4c 69  * Set up a SrcLi
2c9bd 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6a 75  st containing ju
2c9be 73 74 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  st the table bei
2c9bf 6e 67 20 73 63 61 6e 6e 65 64 20 62 79 20 74 68  ng scanned by th
2c9c0 69 73 20 6c 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20  is loop. */.    
2c9c1 6f 6e 65 54 61 62 2e 6e 53 72 63 20 3d 20 31 3b  oneTab.nSrc = 1;
2c9c2 0a 20 20 20 20 6f 6e 65 54 61 62 2e 6e 41 6c 6c  .    oneTab.nAll
2c9c3 6f 63 20 3d 20 31 3b 0a 20 20 20 20 6f 6e 65 54  oc = 1;.    oneT
2c9c4 61 62 2e 61 5b 30 5d 20 3d 20 2a 70 54 61 62 49  ab.a[0] = *pTabI
2c9c5 74 65 6d 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69  tem;..    /* Ini
2c9c6 74 69 61 6c 69 7a 65 20 74 68 65 20 72 6f 77 73  tialize the rows
2c9c7 65 74 20 72 65 67 69 73 74 65 72 20 74 6f 20 63  et register to c
2c9c8 6f 6e 74 61 69 6e 20 4e 55 4c 4c 2e 20 41 6e 20  ontain NULL. An 
2c9c9 53 51 4c 20 4e 55 4c 4c 20 69 73 20 0a 20 20 20  SQL NULL is .   
2c9ca 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 74   ** equivalent t
2c9cb 6f 20 61 6e 20 65 6d 70 74 79 20 72 6f 77 73 65  o an empty rowse
2c9cc 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  t..    **.    **
2c9cd 20 41 6c 73 6f 20 69 6e 69 74 69 61 6c 69 7a 65   Also initialize
2c9ce 20 72 65 67 52 65 74 75 72 6e 20 74 6f 20 63 6f   regReturn to co
2c9cf 6e 74 61 69 6e 20 74 68 65 20 61 64 64 72 65 73  ntain the addres
2c9d0 73 20 6f 66 20 74 68 65 20 69 6e 73 74 72 75 63  s of the instruc
2c9d1 74 69 6f 6e 20 0a 20 20 20 20 2a 2a 20 69 6d 6d  tion .    ** imm
2c9d2 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69  ediately followi
2c9d3 6e 67 20 74 68 65 20 4f 50 5f 52 65 74 75 72 6e  ng the OP_Return
2c9d4 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f   at the bottom o
2c9d5 66 20 74 68 65 20 6c 6f 6f 70 2e 20 54 68 69 73  f the loop. This
2c9d6 0a 20 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69  .    ** is requi
2c9d7 72 65 64 20 69 6e 20 61 20 66 65 77 20 6f 62 73  red in a few obs
2c9d8 63 75 72 65 20 4c 45 46 54 20 4a 4f 49 4e 20 63  cure LEFT JOIN c
2c9d9 61 73 65 73 20 77 68 65 72 65 20 63 6f 6e 74 72  ases where contr
2c9da 6f 6c 20 6a 75 6d 70 73 0a 20 20 20 20 2a 2a 20  ol jumps.    ** 
2c9db 6f 76 65 72 20 74 68 65 20 74 6f 70 20 6f 66 20  over the top of 
2c9dc 74 68 65 20 6c 6f 6f 70 20 69 6e 74 6f 20 74 68  the loop into th
2c9dd 65 20 62 6f 64 79 20 6f 66 20 69 74 2e 20 49 6e  e body of it. In
2c9de 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 0a   this case the .
2c9df 20 20 20 20 2a 2a 20 63 6f 72 72 65 63 74 20 72      ** correct r
2c9e0 65 73 70 6f 6e 73 65 20 66 6f 72 20 74 68 65 20  esponse for the 
2c9e1 65 6e 64 2d 6f 66 2d 6c 6f 6f 70 20 63 6f 64 65  end-of-loop code
2c9e2 20 28 74 68 65 20 4f 50 5f 52 65 74 75 72 6e 29   (the OP_Return)
2c9e3 20 69 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20 66   is to .    ** f
2c9e4 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
2c9e5 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
2c9e6 69 6f 6e 2c 20 6a 75 73 74 20 61 73 20 61 6e 20  ion, just as an 
2c9e7 4f 50 5f 4e 65 78 74 20 64 6f 65 73 20 69 66 0a  OP_Next does if.
2c9e8 20 20 20 20 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e      ** called on
2c9e9 20 61 6e 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65   an uninitialize
2c9ea 64 20 63 75 72 73 6f 72 2e 0a 20 20 20 20 2a 2f  d cursor..    */
2c9eb 0a 20 20 20 20 69 66 28 20 28 77 63 74 72 6c 46  .    if( (wctrlF
2c9ec 6c 61 67 73 20 26 20 57 48 45 52 45 5f 44 55 50  lags & WHERE_DUP
2c9ed 4c 49 43 41 54 45 53 5f 4f 4b 29 3d 3d 30 20 29  LICATES_OK)==0 )
2c9ee 7b 0a 20 20 20 20 20 20 72 65 67 52 6f 77 73 65  {.      regRowse
2c9ef 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  t = ++pParse->nM
2c9f0 65 6d 3b 0a 20 20 20 20 20 20 72 65 67 52 6f 77  em;.      regRow
2c9f1 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  id = ++pParse->n
2c9f2 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Mem;.      sqlit
2c9f3 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c9f4 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52  OP_Null, 0, regR
2c9f5 6f 77 73 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20  owset);.    }.  
2c9f6 20 20 69 52 65 74 49 6e 69 74 20 3d 20 73 71 6c    iRetInit = sql
2c9f7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2c9f8 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
2c9f9 20 72 65 67 52 65 74 75 72 6e 29 3b 0a 0a 20 20   regReturn);..  
2c9fa 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
2c9fb 4f 72 57 63 2d 3e 6e 54 65 72 6d 3b 20 69 69 2b  OrWc->nTerm; ii+
2c9fc 2b 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54  +){.      WhereT
2c9fd 65 72 6d 20 2a 70 4f 72 54 65 72 6d 20 3d 20 26  erm *pOrTerm = &
2c9fe 70 4f 72 57 63 2d 3e 61 5b 69 69 5d 3b 0a 20 20  pOrWc->a[ii];.  
2c9ff 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d      if( pOrTerm-
2ca00 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
2ca01 72 20 7c 7c 20 70 4f 72 54 65 72 6d 2d 3e 65 4f  r || pOrTerm->eO
2ca02 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20  perator==WO_AND 
2ca03 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  ){.        Where
2ca04 49 6e 66 6f 20 2a 70 53 75 62 57 49 6e 66 6f 3b  Info *pSubWInfo;
2ca05 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66            /* Inf
2ca06 6f 20 66 6f 72 20 73 69 6e 67 6c 65 20 4f 52 2d  o for single OR-
2ca07 74 65 72 6d 20 73 63 61 6e 20 2a 2f 0a 0a 20 20  term scan */..  
2ca08 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68        /* Loop th
2ca09 72 6f 75 67 68 20 74 61 62 6c 65 20 65 6e 74 72  rough table entr
2ca0a 69 65 73 20 74 68 61 74 20 6d 61 74 63 68 20 74  ies that match t
2ca0b 65 72 6d 20 70 4f 72 54 65 72 6d 2e 20 2a 2f 0a  erm pOrTerm. */.
2ca0c 20 20 20 20 20 20 20 20 70 53 75 62 57 49 6e 66          pSubWInf
2ca0d 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65  o = sqlite3Where
2ca0e 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 26 6f  Begin(pParse, &o
2ca0f 6e 65 54 61 62 2c 20 70 4f 72 54 65 72 6d 2d 3e  neTab, pOrTerm->
2ca10 70 45 78 70 72 2c 20 30 2c 0a 20 20 20 20 20 20  pExpr, 0,.      
2ca11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca12 20 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45    WHERE_OMIT_OPE
2ca13 4e 20 7c 20 57 48 45 52 45 5f 4f 4d 49 54 5f 43  N | WHERE_OMIT_C
2ca14 4c 4f 53 45 20 7c 20 57 48 45 52 45 5f 46 4f 52  LOSE | WHERE_FOR
2ca15 43 45 5f 54 41 42 4c 45 29 3b 0a 20 20 20 20 20  CE_TABLE);.     
2ca16 20 20 20 69 66 28 20 70 53 75 62 57 49 6e 66 6f     if( pSubWInfo
2ca17 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
2ca18 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20  ( (wctrlFlags & 
2ca19 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53  WHERE_DUPLICATES
2ca1a 5f 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  _OK)==0 ){.     
2ca1b 20 20 20 20 20 20 20 69 6e 74 20 69 53 65 74 20         int iSet 
2ca1c 3d 20 28 28 69 69 3d 3d 70 4f 72 57 63 2d 3e 6e  = ((ii==pOrWc->n
2ca1d 54 65 72 6d 2d 31 29 3f 2d 31 3a 69 69 29 3b 0a  Term-1)?-1:ii);.
2ca1e 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
2ca1f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  r;.            r
2ca20 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2ca21 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72  deGetColumn(pPar
2ca22 73 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e 70 54  se, pTabItem->pT
2ca23 61 62 2c 20 2d 31 2c 20 69 43 75 72 2c 20 0a 20  ab, -1, iCur, . 
2ca24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca26 20 20 20 20 20 20 20 20 72 65 67 52 6f 77 69 64          regRowid
2ca27 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
2ca28 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ca29 4f 70 34 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74  Op4(v, OP_RowSet
2ca2a 54 65 73 74 2c 20 72 65 67 52 6f 77 73 65 74 2c  Test, regRowset,
2ca2b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2ca2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca2d 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2ca2e 6e 74 41 64 64 72 28 76 29 2b 32 2c 0a 20 20 20  ntAddr(v)+2,.   
2ca2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca30 20 20 20 20 20 20 20 20 20 20 20 72 2c 20 53 51             r, SQ
2ca31 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
2ca32 69 53 65 74 29 2c 20 50 34 5f 49 4e 54 33 32 29  iSet), P4_INT32)
2ca33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2ca34 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2ca35 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2ca36 47 6f 73 75 62 2c 20 72 65 67 52 65 74 75 72 6e  Gosub, regReturn
2ca37 2c 20 69 4c 6f 6f 70 42 6f 64 79 29 3b 0a 0a 20  , iLoopBody);.. 
2ca38 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 69           /* Fini
2ca39 73 68 20 74 68 65 20 6c 6f 6f 70 20 74 68 72 6f  sh the loop thro
2ca3a 75 67 68 20 74 61 62 6c 65 20 65 6e 74 72 69 65  ugh table entrie
2ca3b 73 20 74 68 61 74 20 6d 61 74 63 68 20 74 65 72  s that match ter
2ca3c 6d 20 70 4f 72 54 65 72 6d 2e 20 2a 2f 0a 20 20  m pOrTerm. */.  
2ca3d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57          sqlite3W
2ca3e 68 65 72 65 45 6e 64 28 70 53 75 62 57 49 6e 66  hereEnd(pSubWInf
2ca3f 6f 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  o);.        }.  
2ca40 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2ca41 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2ca42 65 50 31 28 76 2c 20 69 52 65 74 49 6e 69 74 2c  eP1(v, iRetInit,
2ca43 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
2ca44 65 6e 74 41 64 64 72 28 76 29 29 3b 0a 20 20 20  entAddr(v));.   
2ca45 20 2f 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41   /* sqlite3VdbeA
2ca46 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
2ca47 2c 20 30 2c 20 72 65 67 52 6f 77 73 65 74 29 3b  , 0, regRowset);
2ca48 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
2ca49 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2ca4a 47 6f 74 6f 2c 20 30 2c 20 70 4c 65 76 65 6c 2d  Goto, 0, pLevel-
2ca4b 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20 73  >addrBrk);.    s
2ca4c 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2ca4d 65 4c 61 62 65 6c 28 76 2c 20 69 4c 6f 6f 70 42  eLabel(v, iLoopB
2ca4e 6f 64 79 29 3b 0a 0a 20 20 20 20 70 4c 65 76 65  ody);..    pLeve
2ca4f 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 52 65 74 75 72  l->op = OP_Retur
2ca50 6e 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  n;.    pLevel->p
2ca51 31 20 3d 20 72 65 67 52 65 74 75 72 6e 3b 0a 20  1 = regReturn;. 
2ca52 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70     disableTerm(p
2ca53 4c 65 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 20  Level, pTerm);. 
2ca54 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a   }else.#endif /*
2ca55 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f   SQLITE_OMIT_OR_
2ca56 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a  OPTIMIZATION */.
2ca57 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65  .  {.    /* Case
2ca58 20 35 3a 20 20 54 68 65 72 65 20 69 73 20 6e 6f   5:  There is no
2ca59 20 75 73 61 62 6c 65 20 69 6e 64 65 78 2e 20 20   usable index.  
2ca5a 57 65 20 6d 75 73 74 20 64 6f 20 61 20 63 6f 6d  We must do a com
2ca5b 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 20 20 20  plete.    **    
2ca5c 20 20 20 20 20 20 73 63 61 6e 20 6f 66 20 74 68        scan of th
2ca5d 65 20 65 6e 74 69 72 65 20 74 61 62 6c 65 2e 0a  e entire table..
2ca5e 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69      */.    stati
2ca5f 63 20 63 6f 6e 73 74 20 75 38 20 61 53 74 65 70  c const u8 aStep
2ca60 5b 5d 20 3d 20 7b 20 4f 50 5f 4e 65 78 74 2c 20  [] = { OP_Next, 
2ca61 4f 50 5f 50 72 65 76 20 7d 3b 0a 20 20 20 20 73  OP_Prev };.    s
2ca62 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61  tatic const u8 a
2ca63 53 74 61 72 74 5b 5d 20 3d 20 7b 20 4f 50 5f 52  Start[] = { OP_R
2ca64 65 77 69 6e 64 2c 20 4f 50 5f 4c 61 73 74 20 7d  ewind, OP_Last }
2ca65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 62 52  ;.    assert( bR
2ca66 65 76 3d 3d 30 20 7c 7c 20 62 52 65 76 3d 3d 31  ev==0 || bRev==1
2ca67 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2ca68 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  omitTable==0 );.
2ca69 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d      pLevel->op =
2ca6a 20 61 53 74 65 70 5b 62 52 65 76 5d 3b 0a 20 20   aStep[bRev];.  
2ca6b 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69    pLevel->p1 = i
2ca6c 43 75 72 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  Cur;.    pLevel-
2ca6d 3e 70 32 20 3d 20 31 20 2b 20 73 71 6c 69 74 65  >p2 = 1 + sqlite
2ca6e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 61  3VdbeAddOp2(v, a
2ca6f 53 74 61 72 74 5b 62 52 65 76 5d 2c 20 69 43 75  Start[bRev], iCu
2ca70 72 2c 20 61 64 64 72 42 72 6b 29 3b 0a 20 20 20  r, addrBrk);.   
2ca71 20 70 4c 65 76 65 6c 2d 3e 70 35 20 3d 20 53 51   pLevel->p5 = SQ
2ca72 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f  LITE_STMTSTATUS_
2ca73 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3b 0a 20  FULLSCAN_STEP;. 
2ca74 20 7d 0a 20 20 6e 6f 74 52 65 61 64 79 20 26 3d   }.  notReady &=
2ca75 20 7e 67 65 74 4d 61 73 6b 28 70 57 43 2d 3e 70   ~getMask(pWC->p
2ca76 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 29 3b 0a  MaskSet, iCur);.
2ca77 0a 20 20 2f 2a 20 49 6e 73 65 72 74 20 63 6f 64  .  /* Insert cod
2ca78 65 20 74 6f 20 74 65 73 74 20 65 76 65 72 79 20  e to test every 
2ca79 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 74 68  subexpression th
2ca7a 61 74 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65  at can be comple
2ca7b 74 65 6c 79 0a 20 20 2a 2a 20 63 6f 6d 70 75 74  tely.  ** comput
2ca7c 65 64 20 75 73 69 6e 67 20 74 68 65 20 63 75 72  ed using the cur
2ca7d 72 65 6e 74 20 73 65 74 20 6f 66 20 74 61 62 6c  rent set of tabl
2ca7e 65 73 2e 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20 30  es..  */.  k = 0
2ca7f 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57  ;.  for(pTerm=pW
2ca80 43 2d 3e 61 2c 20 6a 3d 70 57 43 2d 3e 6e 54 65  C->a, j=pWC->nTe
2ca81 72 6d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70 54  rm; j>0; j--, pT
2ca82 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72  erm++){.    Expr
2ca83 20 2a 70 45 3b 0a 20 20 20 20 74 65 73 74 63 61   *pE;.    testca
2ca84 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61  se( pTerm->wtFla
2ca85 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41  gs & TERM_VIRTUA
2ca86 4c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  L );.    testcas
2ca87 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67  e( pTerm->wtFlag
2ca88 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 20 29  s & TERM_CODED )
2ca89 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d  ;.    if( pTerm-
2ca8a 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d  >wtFlags & (TERM
2ca8b 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f  _VIRTUAL|TERM_CO
2ca8c 44 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  DED) ) continue;
2ca8d 0a 20 20 20 20 69 66 28 20 28 70 54 65 72 6d 2d  .    if( (pTerm-
2ca8e 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f 74  >prereqAll & not
2ca8f 52 65 61 64 79 29 21 3d 30 20 29 20 63 6f 6e 74  Ready)!=0 ) cont
2ca90 69 6e 75 65 3b 0a 20 20 20 20 70 45 20 3d 20 70  inue;.    pE = p
2ca91 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Term->pExpr;.   
2ca92 20 61 73 73 65 72 74 28 20 70 45 21 3d 30 20 29   assert( pE!=0 )
2ca93 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  ;.    if( pLevel
2ca94 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 26 26 20 21  ->iLeftJoin && !
2ca95 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2ca96 70 45 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29  pE, EP_FromJoin)
2ca97 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e   ){.      contin
2ca98 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ue;.    }.    sq
2ca99 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
2ca9a 28 70 50 61 72 73 65 2c 20 70 45 2c 20 61 64 64  (pParse, pE, add
2ca9b 72 43 6f 6e 74 2c 20 53 51 4c 49 54 45 5f 4a 55  rCont, SQLITE_JU
2ca9c 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 6b  MPIFNULL);.    k
2ca9d 20 3d 20 31 3b 0a 20 20 20 20 70 54 65 72 6d 2d   = 1;.    pTerm-
2ca9e 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d  >wtFlags |= TERM
2ca9f 5f 43 4f 44 45 44 3b 0a 20 20 7d 0a 0a 20 20 2f  _CODED;.  }..  /
2caa0 2a 20 46 6f 72 20 61 20 4c 45 46 54 20 4f 55 54  * For a LEFT OUT
2caa1 45 52 20 4a 4f 49 4e 2c 20 67 65 6e 65 72 61 74  ER JOIN, generat
2caa2 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
2caa3 20 72 65 63 6f 72 64 20 74 68 65 20 66 61 63 74   record the fact
2caa4 20 74 68 61 74 0a 20 20 2a 2a 20 61 74 20 6c 65   that.  ** at le
2caa5 61 73 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74  ast one row of t
2caa6 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 68  he right table h
2caa7 61 73 20 6d 61 74 63 68 65 64 20 74 68 65 20 6c  as matched the l
2caa8 65 66 74 20 74 61 62 6c 65 2e 20 20 0a 20 20 2a  eft table.  .  *
2caa9 2f 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  /.  if( pLevel->
2caaa 69 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a 20 20 20  iLeftJoin ){.   
2caab 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46 69 72   pLevel->addrFir
2caac 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  st = sqlite3Vdbe
2caad 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
2caae 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2caaf 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2cab0 67 65 72 2c 20 31 2c 20 70 4c 65 76 65 6c 2d 3e  ger, 1, pLevel->
2cab1 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20  iLeftJoin);.    
2cab2 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2cab3 22 72 65 63 6f 72 64 20 4c 45 46 54 20 4a 4f 49  "record LEFT JOI
2cab4 4e 20 68 69 74 22 29 29 3b 0a 20 20 20 20 73 71  N hit"));.    sq
2cab5 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
2cab6 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20  ear(pParse);.   
2cab7 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e   for(pTerm=pWC->
2cab8 61 2c 20 6a 3d 30 3b 20 6a 3c 70 57 43 2d 3e 6e  a, j=0; j<pWC->n
2cab9 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d  Term; j++, pTerm
2caba 2b 2b 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63  ++){.      testc
2cabb 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c  ase( pTerm->wtFl
2cabc 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55  ags & TERM_VIRTU
2cabd 41 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  AL );.      test
2cabe 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46  case( pTerm->wtF
2cabf 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45  lags & TERM_CODE
2cac0 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  D );.      if( p
2cac1 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
2cac2 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45  (TERM_VIRTUAL|TE
2cac3 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f 6e 74  RM_CODED) ) cont
2cac4 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20  inue;.      if( 
2cac5 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c  (pTerm->prereqAl
2cac6 6c 20 26 20 6e 6f 74 52 65 61 64 79 29 21 3d 30  l & notReady)!=0
2cac7 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2cac8 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d     assert( pTerm
2cac9 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20 20  ->pExpr );.     
2caca 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
2cacb 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65 72  lse(pParse, pTer
2cacc 6d 2d 3e 70 45 78 70 72 2c 20 61 64 64 72 43 6f  m->pExpr, addrCo
2cacd 6e 74 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  nt, SQLITE_JUMPI
2cace 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 70 54  FNULL);.      pT
2cacf 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20  erm->wtFlags |= 
2cad0 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20  TERM_CODED;.    
2cad1 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  }.  }.  sqlite3R
2cad2 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2cad3 61 72 73 65 2c 20 69 52 65 6c 65 61 73 65 52 65  arse, iReleaseRe
2cad4 67 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6e 6f  g);..  return no
2cad5 74 52 65 61 64 79 3b 0a 7d 0a 0a 23 69 66 20 64  tReady;.}..#if d
2cad6 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
2cad7 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  ST)./*.** The fo
2cad8 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65  llowing variable
2cad9 20 68 6f 6c 64 73 20 61 20 74 65 78 74 20 64 65   holds a text de
2cada 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 71 75 65  scription of que
2cadb 72 79 20 70 6c 61 6e 20 67 65 6e 65 72 61 74 65  ry plan generate
2cadc 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 6f 73 74  d.** by the most
2cadd 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20   recent call to 
2cade 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
2cadf 6e 28 29 2e 20 20 45 61 63 68 20 63 61 6c 6c 20  n().  Each call 
2cae0 74 6f 20 57 68 65 72 65 42 65 67 69 6e 0a 2a 2a  to WhereBegin.**
2cae1 20 6f 76 65 72 77 72 69 74 65 73 20 74 68 65 20   overwrites the 
2cae2 70 72 65 76 69 6f 75 73 2e 20 20 54 68 69 73 20  previous.  This 
2cae3 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75  information is u
2cae4 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
2cae5 61 6e 64 0a 2a 2a 20 61 6e 61 6c 79 73 69 73 20  and.** analysis 
2cae6 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  only..*/.SQLITE_
2cae7 41 50 49 20 63 68 61 72 20 73 71 6c 69 74 65 33  API char sqlite3
2cae8 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 42 4d 53 2a  _query_plan[BMS*
2cae9 32 2a 34 30 5d 3b 20 20 2f 2a 20 54 65 78 74 20  2*40];  /* Text 
2caea 6f 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 73  of the join */.s
2caeb 74 61 74 69 63 20 69 6e 74 20 6e 51 50 6c 61 6e  tatic int nQPlan
2caec 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2caed 20 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20     /* Next free 
2caee 73 6c 6f 77 20 69 6e 20 5f 71 75 65 72 79 5f 70  slow in _query_p
2caef 6c 61 6e 5b 5d 20 2a 2f 0a 0a 23 65 6e 64 69 66  lan[] */..#endif
2caf0 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20   /* SQLITE_TEST 
2caf1 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  */.../*.** Free 
2caf2 61 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75  a WhereInfo stru
2caf3 63 74 75 72 65 0a 2a 2f 0a 73 74 61 74 69 63 20  cture.*/.static 
2caf4 76 6f 69 64 20 77 68 65 72 65 49 6e 66 6f 46 72  void whereInfoFr
2caf5 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ee(sqlite3 *db, 
2caf6 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66  WhereInfo *pWInf
2caf7 6f 29 7b 0a 20 20 69 66 28 20 70 57 49 6e 66 6f  o){.  if( pWInfo
2caf8 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
2caf9 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 57     for(i=0; i<pW
2cafa 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 20 69 2b  Info->nLevel; i+
2cafb 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
2cafc 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49  3_index_info *pI
2cafd 6e 66 6f 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b  nfo = pWInfo->a[
2cafe 69 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20 20 20  i].pIdxInfo;.   
2caff 20 20 20 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a     if( pInfo ){.
2cb00 20 20 20 20 20 20 20 20 2f 2a 20 61 73 73 65 72          /* asser
2cb01 74 28 20 70 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f  t( pInfo->needTo
2cb02 46 72 65 65 49 64 78 53 74 72 3d 3d 30 20 7c 7c  FreeIdxStr==0 ||
2cb03 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2cb04 64 20 29 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20  d ); */.        
2cb05 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 65 65 64 54  if( pInfo->needT
2cb06 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b 0a 20  oFreeIdxStr ){. 
2cb07 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2cb08 5f 66 72 65 65 28 70 49 6e 66 6f 2d 3e 69 64 78  _free(pInfo->idx
2cb09 53 74 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Str);.        }.
2cb0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2cb0b 62 46 72 65 65 28 64 62 2c 20 70 49 6e 66 6f 29  bFree(db, pInfo)
2cb0c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2cb0d 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 65 43      whereClauseC
2cb0e 6c 65 61 72 28 70 57 49 6e 66 6f 2d 3e 70 57 43  lear(pWInfo->pWC
2cb0f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
2cb10 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29  Free(db, pWInfo)
2cb11 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  ;.  }.}.../*.** 
2cb12 47 65 6e 65 72 61 74 65 20 74 68 65 20 62 65 67  Generate the beg
2cb13 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f  inning of the lo
2cb14 6f 70 20 75 73 65 64 20 66 6f 72 20 57 48 45 52  op used for WHER
2cb15 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73  E clause process
2cb16 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75  ing..** The retu
2cb17 72 6e 20 76 61 6c 75 65 20 69 73 20 61 20 70 6f  rn value is a po
2cb18 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 70 61 71  inter to an opaq
2cb19 75 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ue structure tha
2cb1a 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 69 6e  t contains.** in
2cb1b 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64  formation needed
2cb1c 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68   to terminate th
2cb1d 65 20 6c 6f 6f 70 2e 20 20 4c 61 74 65 72 2c 20  e loop.  Later, 
2cb1e 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  the calling rout
2cb1f 69 6e 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 69 6e  ine.** should in
2cb20 76 6f 6b 65 20 73 71 6c 69 74 65 33 57 68 65 72  voke sqlite3Wher
2cb21 65 45 6e 64 28 29 20 77 69 74 68 20 74 68 65 20  eEnd() with the 
2cb22 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
2cb23 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  this function.**
2cb24 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6d   in order to com
2cb25 70 6c 65 74 65 20 74 68 65 20 57 48 45 52 45 20  plete the WHERE 
2cb26 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
2cb27 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  g..**.** If an e
2cb28 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 69  rror occurs, thi
2cb29 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
2cb2a 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68  s NULL..**.** Th
2cb2b 65 20 62 61 73 69 63 20 69 64 65 61 20 69 73 20  e basic idea is 
2cb2c 74 6f 20 64 6f 20 61 20 6e 65 73 74 65 64 20 6c  to do a nested l
2cb2d 6f 6f 70 2c 20 6f 6e 65 20 6c 6f 6f 70 20 66 6f  oop, one loop fo
2cb2e 72 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 0a  r each table in.
2cb2f 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  ** the FROM clau
2cb30 73 65 20 6f 66 20 61 20 73 65 6c 65 63 74 2e 20  se of a select. 
2cb31 20 28 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44   (INSERT and UPD
2cb32 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 61  ATE statements a
2cb33 72 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61  re the.** same a
2cb34 73 20 61 20 53 45 4c 45 43 54 20 77 69 74 68 20  s a SELECT with 
2cb35 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 74 61  only a single ta
2cb36 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ble in the FROM 
2cb37 63 6c 61 75 73 65 2e 29 20 20 46 6f 72 0a 2a 2a  clause.)  For.**
2cb38 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
2cb39 20 53 51 4c 20 69 73 20 74 68 69 73 3a 0a 2a 2a   SQL is this:.**
2cb3a 0a 2a 2a 20 20 20 20 20 20 20 53 45 4c 45 43 54  .**       SELECT
2cb3b 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32 2c 20   * FROM t1, t2, 
2cb3c 74 33 20 57 48 45 52 45 20 2e 2e 2e 3b 0a 2a 2a  t3 WHERE ...;.**
2cb3d 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 63 6f 64  .** Then the cod
2cb3e 65 20 67 65 6e 65 72 61 74 65 64 20 69 73 20 63  e generated is c
2cb3f 6f 6e 63 65 70 74 75 61 6c 6c 79 20 6c 69 6b 65  onceptually like
2cb40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
2cb41 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 6f 72 65 61  **.**      forea
2cb42 63 68 20 72 6f 77 31 20 69 6e 20 74 31 20 64 6f  ch row1 in t1 do
2cb43 20 20 20 20 20 20 20 5c 20 20 20 20 43 6f 64 65         \    Code
2cb44 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 20 20   generated.**   
2cb45 20 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77       foreach row
2cb46 32 20 69 6e 20 74 32 20 64 6f 20 20 20 20 20 20  2 in t2 do      
2cb47 7c 2d 2d 20 62 79 20 73 71 6c 69 74 65 33 57 68  |-- by sqlite3Wh
2cb48 65 72 65 42 65 67 69 6e 28 29 0a 2a 2a 20 20 20  ereBegin().**   
2cb49 20 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 72         foreach r
2cb4a 6f 77 33 20 69 6e 20 74 33 20 64 6f 20 20 20 2f  ow3 in t3 do   /
2cb4b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 2e  .**            .
2cb4c 2e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65  ...**          e
2cb4d 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nd              
2cb4e 20 20 20 20 20 20 20 5c 20 20 20 20 43 6f 64 65         \    Code
2cb4f 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 20 20   generated.**   
2cb50 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20 20       end        
2cb51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb52 7c 2d 2d 20 62 79 20 73 71 6c 69 74 65 33 57 68  |-- by sqlite3Wh
2cb53 65 72 65 45 6e 64 28 29 0a 2a 2a 20 20 20 20 20  ereEnd().**     
2cb54 20 65 6e 64 20 20 20 20 20 20 20 20 20 20 20 20   end            
2cb55 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0a 2a               /.*
2cb56 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
2cb57 68 65 20 6c 6f 6f 70 73 20 6d 69 67 68 74 20 6e  he loops might n
2cb58 6f 74 20 62 65 20 6e 65 73 74 65 64 20 69 6e 20  ot be nested in 
2cb59 74 68 65 20 6f 72 64 65 72 20 69 6e 20 77 68 69  the order in whi
2cb5a 63 68 20 74 68 65 79 0a 2a 2a 20 61 70 70 65 61  ch they.** appea
2cb5b 72 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  r in the FROM cl
2cb5c 61 75 73 65 20 69 66 20 61 20 64 69 66 66 65 72  ause if a differ
2cb5d 65 6e 74 20 6f 72 64 65 72 20 69 73 20 62 65 74  ent order is bet
2cb5e 74 65 72 20 61 62 6c 65 20 74 6f 20 6d 61 6b 65  ter able to make
2cb5f 0a 2a 2a 20 75 73 65 20 6f 66 20 69 6e 64 69 63  .** use of indic
2cb60 65 73 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74  es.  Note also t
2cb61 68 61 74 20 77 68 65 6e 20 74 68 65 20 49 4e 20  hat when the IN 
2cb62 6f 70 65 72 61 74 6f 72 20 61 70 70 65 61 72 73  operator appears
2cb63 20 69 6e 0a 2a 2a 20 74 68 65 20 57 48 45 52 45   in.** the WHERE
2cb64 20 63 6c 61 75 73 65 2c 20 69 74 20 6d 69 67 68   clause, it migh
2cb65 74 20 72 65 73 75 6c 74 20 69 6e 20 61 64 64 69  t result in addi
2cb66 74 69 6f 6e 61 6c 20 6e 65 73 74 65 64 20 6c 6f  tional nested lo
2cb67 6f 70 73 20 66 6f 72 0a 2a 2a 20 73 63 61 6e 6e  ops for.** scann
2cb68 69 6e 67 20 74 68 72 6f 75 67 68 20 61 6c 6c 20  ing through all 
2cb69 76 61 6c 75 65 73 20 6f 6e 20 74 68 65 20 72 69  values on the ri
2cb6a 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  ght-hand side of
2cb6b 20 74 68 65 20 49 4e 2e 0a 2a 2a 0a 2a 2a 20 54   the IN..**.** T
2cb6c 68 65 72 65 20 61 72 65 20 42 74 72 65 65 20 63  here are Btree c
2cb6d 75 72 73 6f 72 73 20 61 73 73 6f 63 69 61 74 65  ursors associate
2cb6e 64 20 77 69 74 68 20 65 61 63 68 20 74 61 62 6c  d with each tabl
2cb6f 65 2e 20 20 74 31 20 75 73 65 73 20 63 75 72 73  e.  t1 uses curs
2cb70 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 70 54 61  or.** number pTa
2cb71 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72  bList->a[0].iCur
2cb72 73 6f 72 2e 20 20 74 32 20 75 73 65 73 20 74 68  sor.  t2 uses th
2cb73 65 20 63 75 72 73 6f 72 20 70 54 61 62 4c 69 73  e cursor pTabLis
2cb74 74 2d 3e 61 5b 31 5d 2e 69 43 75 72 73 6f 72 2e  t->a[1].iCursor.
2cb75 0a 2a 2a 20 41 6e 64 20 73 6f 20 66 6f 72 74 68  .** And so forth
2cb76 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
2cb77 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
2cb78 6f 20 6f 70 65 6e 20 74 68 6f 73 65 20 56 44 42  o open those VDB
2cb79 45 20 63 75 72 73 6f 72 73 0a 2a 2a 20 61 6e 64  E cursors.** and
2cb7a 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
2cb7b 28 29 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  () generates the
2cb7c 20 63 6f 64 65 20 74 6f 20 63 6c 6f 73 65 20 74   code to close t
2cb7d 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  hem..**.** The c
2cb7e 6f 64 65 20 74 68 61 74 20 73 71 6c 69 74 65 33  ode that sqlite3
2cb7f 57 68 65 72 65 42 65 67 69 6e 28 29 20 67 65 6e  WhereBegin() gen
2cb80 65 72 61 74 65 73 20 6c 65 61 76 65 73 20 74 68  erates leaves th
2cb81 65 20 63 75 72 73 6f 72 73 20 6e 61 6d 65 64 0a  e cursors named.
2cb82 2a 2a 20 69 6e 20 70 54 61 62 4c 69 73 74 20 70  ** in pTabList p
2cb83 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 69 72  ointing at their
2cb84 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 6e 74   appropriate ent
2cb85 72 69 65 73 2e 20 20 54 68 65 20 5b 2e 2e 2e 5d  ries.  The [...]
2cb86 20 63 6f 64 65 0a 2a 2a 20 63 61 6e 20 75 73 65   code.** can use
2cb87 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61 6e 64 20 4f   OP_Column and O
2cb88 50 5f 52 6f 77 69 64 20 6f 70 63 6f 64 65 73 20  P_Rowid opcodes 
2cb89 6f 6e 20 74 68 65 73 65 20 63 75 72 73 6f 72 73  on these cursors
2cb8a 20 74 6f 20 65 78 74 72 61 63 74 0a 2a 2a 20 64   to extract.** d
2cb8b 61 74 61 20 66 72 6f 6d 20 74 68 65 20 76 61 72  ata from the var
2cb8c 69 6f 75 73 20 74 61 62 6c 65 73 20 6f 66 20 74  ious tables of t
2cb8d 68 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49  he loop..**.** I
2cb8e 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
2cb8f 73 65 20 69 73 20 65 6d 70 74 79 2c 20 74 68 65  se is empty, the
2cb90 20 66 6f 72 65 61 63 68 20 6c 6f 6f 70 73 20 6d   foreach loops m
2cb91 75 73 74 20 65 61 63 68 20 73 63 61 6e 20 74 68  ust each scan th
2cb92 65 69 72 0a 2a 2a 20 65 6e 74 69 72 65 20 74 61  eir.** entire ta
2cb93 62 6c 65 73 2e 20 20 54 68 75 73 20 61 20 74 68  bles.  Thus a th
2cb94 72 65 65 2d 77 61 79 20 6a 6f 69 6e 20 69 73 20  ree-way join is 
2cb95 61 6e 20 4f 28 4e 5e 33 29 20 6f 70 65 72 61 74  an O(N^3) operat
2cb96 69 6f 6e 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20  ion.  But if.** 
2cb97 74 68 65 20 74 61 62 6c 65 73 20 68 61 76 65 20  the tables have 
2cb98 69 6e 64 69 63 65 73 20 61 6e 64 20 74 68 65 72  indices and ther
2cb99 65 20 61 72 65 20 74 65 72 6d 73 20 69 6e 20 74  e are terms in t
2cb9a 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2cb9b 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 20 74 6f  that.** refer to
2cb9c 20 74 68 6f 73 65 20 69 6e 64 69 63 65 73 2c 20   those indices, 
2cb9d 61 20 63 6f 6d 70 6c 65 74 65 20 74 61 62 6c 65  a complete table
2cb9e 20 73 63 61 6e 20 63 61 6e 20 62 65 20 61 76 6f   scan can be avo
2cb9f 69 64 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20  ided and the.** 
2cba0 63 6f 64 65 20 77 69 6c 6c 20 72 75 6e 20 6d 75  code will run mu
2cba1 63 68 20 66 61 73 74 65 72 2e 20 20 4d 6f 73 74  ch faster.  Most
2cba2 20 6f 66 20 74 68 65 20 77 6f 72 6b 20 6f 66 20   of the work of 
2cba3 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2cba4 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 74 6f 20 73  checking.** to s
2cba5 65 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ee if there are 
2cba6 69 6e 64 69 63 65 73 20 74 68 61 74 20 63 61 6e  indices that can
2cba7 20 62 65 20 75 73 65 64 20 74 6f 20 73 70 65 65   be used to spee
2cba8 64 20 75 70 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a  d up the loop..*
2cba9 2a 0a 2a 2a 20 54 65 72 6d 73 20 6f 66 20 74 68  *.** Terms of th
2cbaa 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 61  e WHERE clause a
2cbab 72 65 20 61 6c 73 6f 20 75 73 65 64 20 74 6f 20  re also used to 
2cbac 6c 69 6d 69 74 20 77 68 69 63 68 20 72 6f 77 73  limit which rows
2cbad 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 6d 61 6b   actually.** mak
2cbae 65 20 69 74 20 74 6f 20 74 68 65 20 22 2e 2e 2e  e it to the "...
2cbaf 22 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  " in the middle 
2cbb0 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 41 66  of the loop.  Af
2cbb1 74 65 72 20 65 61 63 68 20 22 66 6f 72 65 61 63  ter each "foreac
2cbb2 68 22 2c 0a 2a 2a 20 74 65 72 6d 73 20 6f 66 20  h",.** terms of 
2cbb3 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2cbb4 20 74 68 61 74 20 75 73 65 20 6f 6e 6c 79 20 74   that use only t
2cbb5 65 72 6d 73 20 69 6e 20 74 68 61 74 20 6c 6f 6f  erms in that loo
2cbb6 70 20 61 6e 64 20 6f 75 74 65 72 0a 2a 2a 20 6c  p and outer.** l
2cbb7 6f 6f 70 73 20 61 72 65 20 65 76 61 6c 75 61 74  oops are evaluat
2cbb8 65 64 20 61 6e 64 20 69 66 20 66 61 6c 73 65 20  ed and if false 
2cbb9 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 61  a jump is made a
2cbba 72 6f 75 6e 64 20 61 6c 6c 20 73 75 62 73 65 71  round all subseq
2cbbb 75 65 6e 74 0a 2a 2a 20 69 6e 6e 65 72 20 6c 6f  uent.** inner lo
2cbbc 6f 70 73 20 28 6f 72 20 61 72 6f 75 6e 64 20 74  ops (or around t
2cbbd 68 65 20 22 2e 2e 2e 22 20 69 66 20 74 68 65 20  he "..." if the 
2cbbe 74 65 73 74 20 6f 63 63 75 72 73 20 77 69 74 68  test occurs with
2cbbf 69 6e 20 74 68 65 20 69 6e 6e 65 72 2d 0a 2a 2a  in the inner-.**
2cbc0 20 6d 6f 73 74 20 6c 6f 6f 70 29 0a 2a 2a 0a 2a   most loop).**.*
2cbc1 2a 20 4f 55 54 45 52 20 4a 4f 49 4e 53 0a 2a 2a  * OUTER JOINS.**
2cbc2 0a 2a 2a 20 41 6e 20 6f 75 74 65 72 20 6a 6f 69  .** An outer joi
2cbc3 6e 20 6f 66 20 74 61 62 6c 65 73 20 74 31 20 61  n of tables t1 a
2cbc4 6e 64 20 74 32 20 69 73 20 63 6f 6e 63 65 70 74  nd t2 is concept
2cbc5 61 6c 6c 79 20 63 6f 64 65 64 20 61 73 20 66 6f  ally coded as fo
2cbc6 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  llows:.**.**    
2cbc7 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e 20  foreach row1 in 
2cbc8 74 31 20 64 6f 0a 2a 2a 20 20 20 20 20 20 66 6c  t1 do.**      fl
2cbc9 61 67 20 3d 20 30 0a 2a 2a 20 20 20 20 20 20 66  ag = 0.**      f
2cbca 6f 72 65 61 63 68 20 72 6f 77 32 20 69 6e 20 74  oreach row2 in t
2cbcb 32 20 64 6f 0a 2a 2a 20 20 20 20 20 20 20 20 73  2 do.**        s
2cbcc 74 61 72 74 3a 0a 2a 2a 20 20 20 20 20 20 20 20  tart:.**        
2cbcd 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20 20 20    ....**        
2cbce 20 20 66 6c 61 67 20 3d 20 31 0a 2a 2a 20 20 20    flag = 1.**   
2cbcf 20 20 20 65 6e 64 0a 2a 2a 20 20 20 20 20 20 69     end.**      i
2cbd0 66 20 66 6c 61 67 3d 3d 30 20 74 68 65 6e 0a 2a  f flag==0 then.*
2cbd1 2a 20 20 20 20 20 20 20 20 6d 6f 76 65 20 74 68  *        move th
2cbd2 65 20 72 6f 77 32 20 63 75 72 73 6f 72 20 74 6f  e row2 cursor to
2cbd3 20 61 20 6e 75 6c 6c 20 72 6f 77 0a 2a 2a 20 20   a null row.**  
2cbd4 20 20 20 20 20 20 67 6f 74 6f 20 73 74 61 72 74        goto start
2cbd5 0a 2a 2a 20 20 20 20 20 20 66 69 0a 2a 2a 20 20  .**      fi.**  
2cbd6 20 20 65 6e 64 0a 2a 2a 0a 2a 2a 20 4f 52 44 45    end.**.** ORDE
2cbd7 52 20 42 59 20 43 4c 41 55 53 45 20 50 52 4f 43  R BY CLAUSE PROC
2cbd8 45 53 53 49 4e 47 0a 2a 2a 0a 2a 2a 20 2a 70 70  ESSING.**.** *pp
2cbd9 4f 72 64 65 72 42 79 20 69 73 20 61 20 70 6f 69  OrderBy is a poi
2cbda 6e 74 65 72 20 74 6f 20 74 68 65 20 4f 52 44 45  nter to the ORDE
2cbdb 52 20 42 59 20 63 6c 61 75 73 65 20 6f 66 20 61  R BY clause of a
2cbdc 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2cbdd 74 2c 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69  t,.** if there i
2cbde 73 20 6f 6e 65 2e 20 20 49 66 20 74 68 65 72 65  s one.  If there
2cbdf 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20   is no ORDER BY 
2cbe0 63 6c 61 75 73 65 20 6f 72 20 69 66 20 74 68 69  clause or if thi
2cbe1 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20  s routine.** is 
2cbe2 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 6e 20 55  called from an U
2cbe3 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
2cbe4 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
2cbe5 70 70 4f 72 64 65 72 42 79 20 69 73 20 4e 55 4c  ppOrderBy is NUL
2cbe6 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69  L..**.** If an i
2cbe7 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64  ndex can be used
2cbe8 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 61 74   so that the nat
2cbe9 75 72 61 6c 20 6f 75 74 70 75 74 20 6f 72 64 65  ural output orde
2cbea 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a  r of the table.*
2cbeb 2a 20 73 63 61 6e 20 69 73 20 63 6f 72 72 65 63  * scan is correc
2cbec 74 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20  t for the ORDER 
2cbed 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20  BY clause, then 
2cbee 74 68 61 74 20 69 6e 64 65 78 20 69 73 20 75 73  that index is us
2cbef 65 64 20 61 6e 64 0a 2a 2a 20 2a 70 70 4f 72 64  ed and.** *ppOrd
2cbf0 65 72 42 79 20 69 73 20 73 65 74 20 74 6f 20 4e  erBy is set to N
2cbf1 55 4c 4c 2e 20 20 54 68 69 73 20 69 73 20 61 6e  ULL.  This is an
2cbf2 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74 68   optimization th
2cbf3 61 74 20 70 72 65 76 65 6e 74 73 20 61 6e 0a 2a  at prevents an.*
2cbf4 2a 20 75 6e 6e 65 63 65 73 73 61 72 79 20 73 6f  * unnecessary so
2cbf5 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  rt of the result
2cbf6 20 73 65 74 20 69 66 20 61 6e 20 69 6e 64 65 78   set if an index
2cbf7 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
2cbf8 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59   the.** ORDER BY
2cbf9 20 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79 20   clause already 
2cbfa 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  exists..**.** If
2cbfb 20 74 68 65 20 77 68 65 72 65 20 63 6c 61 75 73   the where claus
2cbfc 65 20 6c 6f 6f 70 73 20 63 61 6e 6e 6f 74 20 62  e loops cannot b
2cbfd 65 20 61 72 72 61 6e 67 65 64 20 74 6f 20 70 72  e arranged to pr
2cbfe 6f 76 69 64 65 20 74 68 65 20 63 6f 72 72 65 63  ovide the correc
2cbff 74 0a 2a 2a 20 6f 75 74 70 75 74 20 6f 72 64 65  t.** output orde
2cc00 72 2c 20 74 68 65 6e 20 74 68 65 20 2a 70 70 4f  r, then the *ppO
2cc01 72 64 65 72 42 79 20 69 73 20 75 6e 63 68 61 6e  rderBy is unchan
2cc02 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ged..*/.SQLITE_P
2cc03 52 49 56 41 54 45 20 57 68 65 72 65 49 6e 66 6f  RIVATE WhereInfo
2cc04 20 2a 73 71 6c 69 74 65 33 57 68 65 72 65 42 65   *sqlite3WhereBe
2cc05 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  gin(.  Parse *pP
2cc06 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
2cc07 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
2cc08 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
2cc09 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f 2a  *pTabList,    /*
2cc0a 20 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 74   A list of all t
2cc0b 61 62 6c 65 73 20 74 6f 20 62 65 20 73 63 61 6e  ables to be scan
2cc0c 6e 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ned */.  Expr *p
2cc0d 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 2f  Where,         /
2cc0e 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
2cc0f 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  se */.  ExprList
2cc10 20 2a 2a 70 70 4f 72 64 65 72 42 79 2c 20 2f 2a   **ppOrderBy, /*
2cc11 20 41 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   An ORDER BY cla
2cc12 75 73 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  use, or NULL */.
2cc13 20 20 75 31 36 20 77 63 74 72 6c 46 6c 61 67 73    u16 wctrlFlags
2cc14 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
2cc15 66 20 74 68 65 20 57 48 45 52 45 5f 2a 20 66 6c  f the WHERE_* fl
2cc16 61 67 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73  ags defined in s
2cc17 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2f 0a 29 7b  qliteInt.h */.){
2cc18 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
2cc19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2cc1a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2cc1b 0a 20 20 69 6e 74 20 6e 42 79 74 65 57 49 6e 66  .  int nByteWInf
2cc1c 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  o;            /*
2cc1d 20 4e 75 6d 2e 20 62 79 74 65 73 20 61 6c 6c 6f   Num. bytes allo
2cc1e 63 61 74 65 64 20 66 6f 72 20 57 68 65 72 65 49  cated for WhereI
2cc1f 6e 66 6f 20 73 74 72 75 63 74 20 2a 2f 0a 20 20  nfo struct */.  
2cc20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66  WhereInfo *pWInf
2cc21 6f 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69  o;         /* Wi
2cc22 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 72 65  ll become the re
2cc23 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
2cc24 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  is function */. 
2cc25 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
2cc26 65 2d 3e 70 56 64 62 65 3b 20 20 20 2f 2a 20 54  e->pVdbe;   /* T
2cc27 68 65 20 76 69 72 74 75 61 6c 20 64 61 74 61 62  he virtual datab
2cc28 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f 0a 20 20  ase engine */.  
2cc29 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
2cc2a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  ;          /* Cu
2cc2b 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e  rsors that are n
2cc2c 6f 74 20 79 65 74 20 70 6f 73 69 74 69 6f 6e 65  ot yet positione
2cc2d 64 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b  d */.  WhereMask
2cc2e 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20  Set *pMaskSet;  
2cc2f 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
2cc30 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20 2a 2f 0a  ion mask set */.
2cc31 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2cc32 57 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  WC;             
2cc33 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74 69    /* Decompositi
2cc34 6f 6e 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  on of the WHERE 
2cc35 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75  clause */.  stru
2cc36 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2cc37 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20 41  *pTabItem;  /* A
2cc38 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72   single entry fr
2cc39 6f 6d 20 70 54 61 62 4c 69 73 74 20 2a 2f 0a 20  om pTabList */. 
2cc3a 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65   WhereLevel *pLe
2cc3b 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  vel;            
2cc3c 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 6c 65 76   /* A single lev
2cc3d 65 6c 20 69 6e 20 74 68 65 20 70 57 49 6e 66 6f  el in the pWInfo
2cc3e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69   list */.  int i
2cc3f 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20  From;           
2cc40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
2cc41 72 73 74 20 75 6e 75 73 65 64 20 46 52 4f 4d 20  rst unused FROM 
2cc42 63 6c 61 75 73 65 20 65 6c 65 6d 65 6e 74 20 2a  clause element *
2cc43 2f 0a 20 20 69 6e 74 20 61 6e 64 46 6c 61 67 73  /.  int andFlags
2cc44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2cc45 2a 20 41 4e 44 2d 65 64 20 63 6f 6d 62 69 6e 61  * AND-ed combina
2cc46 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 70 57 43 2d  tion of all pWC-
2cc47 3e 61 5b 5d 2e 77 74 46 6c 61 67 73 20 2a 2f 0a  >a[].wtFlags */.
2cc48 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
2cc49 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2cc4a 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2cc4b 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  ion */..  /* The
2cc4c 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65   number of table
2cc4d 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
2cc4e 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65 64 20  ause is limited 
2cc4f 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  by the number of
2cc50 0a 20 20 2a 2a 20 62 69 74 73 20 69 6e 20 61 20  .  ** bits in a 
2cc51 42 69 74 6d 61 73 6b 20 0a 20 20 2a 2f 0a 20 20  Bitmask .  */.  
2cc52 69 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  if( pTabList->nS
2cc53 72 63 3e 42 4d 53 20 29 7b 0a 20 20 20 20 73 71  rc>BMS ){.    sq
2cc54 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2cc55 61 72 73 65 2c 20 22 61 74 20 6d 6f 73 74 20 25  arse, "at most %
2cc56 64 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f  d tables in a jo
2cc57 69 6e 22 2c 20 42 4d 53 29 3b 0a 20 20 20 20 72  in", BMS);.    r
2cc58 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
2cc59 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20  /* Allocate and 
2cc5a 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 57  initialize the W
2cc5b 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75  hereInfo structu
2cc5c 72 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63  re that will bec
2cc5d 6f 6d 65 20 74 68 65 0a 20 20 2a 2a 20 72 65 74  ome the.  ** ret
2cc5e 75 72 6e 20 76 61 6c 75 65 2e 20 41 20 73 69 6e  urn value. A sin
2cc5f 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  gle allocation i
2cc60 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  s used to store 
2cc61 74 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 20 20  the WhereInfo.  
2cc62 2a 2a 20 73 74 72 75 63 74 2c 20 74 68 65 20 63  ** struct, the c
2cc63 6f 6e 74 65 6e 74 73 20 6f 66 20 57 68 65 72 65  ontents of Where
2cc64 49 6e 66 6f 2e 61 5b 5d 2c 20 74 68 65 20 57 68  Info.a[], the Wh
2cc65 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74  ereClause struct
2cc66 75 72 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  ure.  ** and the
2cc67 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 73 74   WhereMaskSet st
2cc68 72 75 63 74 75 72 65 2e 20 53 69 6e 63 65 20 57  ructure. Since W
2cc69 68 65 72 65 43 6c 61 75 73 65 20 63 6f 6e 74 61  hereClause conta
2cc6a 69 6e 73 20 61 6e 20 38 2d 62 79 74 65 0a 20 20  ins an 8-byte.  
2cc6b 2a 2a 20 66 69 65 6c 64 20 28 74 79 70 65 20 42  ** field (type B
2cc6c 69 74 6d 61 73 6b 29 20 69 74 20 6d 75 73 74 20  itmask) it must 
2cc6d 62 65 20 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e  be aligned on an
2cc6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79   8-byte boundary
2cc6f 20 6f 6e 0a 20 20 2a 2a 20 73 6f 6d 65 20 61 72   on.  ** some ar
2cc70 63 68 69 74 65 63 74 75 72 65 73 2e 20 48 65 6e  chitectures. Hen
2cc71 63 65 20 74 68 65 20 52 4f 55 4e 44 38 28 29 20  ce the ROUND8() 
2cc72 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 64 62  below..  */.  db
2cc73 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2cc74 20 6e 42 79 74 65 57 49 6e 66 6f 20 3d 20 52 4f   nByteWInfo = RO
2cc75 55 4e 44 38 28 73 69 7a 65 6f 66 28 57 68 65 72  UND8(sizeof(Wher
2cc76 65 49 6e 66 6f 29 2b 28 70 54 61 62 4c 69 73 74  eInfo)+(pTabList
2cc77 2d 3e 6e 53 72 63 2d 31 29 2a 73 69 7a 65 6f 66  ->nSrc-1)*sizeof
2cc78 28 57 68 65 72 65 4c 65 76 65 6c 29 29 3b 0a 20  (WhereLevel));. 
2cc79 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
2cc7a 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2cc7b 2c 20 0a 20 20 20 20 20 20 6e 42 79 74 65 57 49  , .      nByteWI
2cc7c 6e 66 6f 20 2b 20 0a 20 20 20 20 20 20 73 69 7a  nfo + .      siz
2cc7d 65 6f 66 28 57 68 65 72 65 43 6c 61 75 73 65 29  eof(WhereClause)
2cc7e 20 2b 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28   +.      sizeof(
2cc7f 57 68 65 72 65 4d 61 73 6b 53 65 74 29 0a 20 20  WhereMaskSet).  
2cc80 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
2cc81 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2cc82 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e   goto whereBegin
2cc83 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 57 49  Error;.  }.  pWI
2cc84 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 20 3d 20 70 54  nfo->nLevel = pT
2cc85 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 0a 20 20  abList->nSrc;.  
2cc86 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 20 3d  pWInfo->pParse =
2cc87 20 70 50 61 72 73 65 3b 0a 20 20 70 57 49 6e 66   pParse;.  pWInf
2cc88 6f 2d 3e 70 54 61 62 4c 69 73 74 20 3d 20 70 54  o->pTabList = pT
2cc89 61 62 4c 69 73 74 3b 0a 20 20 70 57 49 6e 66 6f  abList;.  pWInfo
2cc8a 2d 3e 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74  ->iBreak = sqlit
2cc8b 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2cc8c 76 29 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 57  v);.  pWInfo->pW
2cc8d 43 20 3d 20 70 57 43 20 3d 20 28 57 68 65 72 65  C = pWC = (Where
2cc8e 43 6c 61 75 73 65 20 2a 29 26 28 28 75 38 20 2a  Clause *)&((u8 *
2cc8f 29 70 57 49 6e 66 6f 29 5b 6e 42 79 74 65 57 49  )pWInfo)[nByteWI
2cc90 6e 66 6f 5d 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e  nfo];.  pWInfo->
2cc91 77 63 74 72 6c 46 6c 61 67 73 20 3d 20 77 63 74  wctrlFlags = wct
2cc92 72 6c 46 6c 61 67 73 3b 0a 20 20 70 4d 61 73 6b  rlFlags;.  pMask
2cc93 53 65 74 20 3d 20 28 57 68 65 72 65 4d 61 73 6b  Set = (WhereMask
2cc94 53 65 74 2a 29 26 70 57 43 5b 31 5d 3b 0a 0a 20  Set*)&pWC[1];.. 
2cc95 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 57 48   /* Split the WH
2cc96 45 52 45 20 63 6c 61 75 73 65 20 69 6e 74 6f 20  ERE clause into 
2cc97 73 65 70 61 72 61 74 65 20 73 75 62 65 78 70 72  separate subexpr
2cc98 65 73 73 69 6f 6e 73 20 77 68 65 72 65 20 65 61  essions where ea
2cc99 63 68 0a 20 20 2a 2a 20 73 75 62 65 78 70 72 65  ch.  ** subexpre
2cc9a 73 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61 74  ssion is separat
2cc9b 65 64 20 62 79 20 61 6e 20 41 4e 44 20 6f 70 65  ed by an AND ope
2cc9c 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 6e  rator..  */.  in
2cc9d 69 74 4d 61 73 6b 53 65 74 28 70 4d 61 73 6b 53  itMaskSet(pMaskS
2cc9e 65 74 29 3b 0a 20 20 77 68 65 72 65 43 6c 61 75  et);.  whereClau
2cc9f 73 65 49 6e 69 74 28 70 57 43 2c 20 70 50 61 72  seInit(pWC, pPar
2cca0 73 65 2c 20 70 4d 61 73 6b 53 65 74 29 3b 0a 20  se, pMaskSet);. 
2cca1 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2cca2 43 6f 6e 73 74 61 6e 74 73 28 70 50 61 72 73 65  Constants(pParse
2cca3 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 77 68 65  , pWhere);.  whe
2cca4 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70 57 68  reSplit(pWC, pWh
2cca5 65 72 65 2c 20 54 4b 5f 41 4e 44 29 3b 0a 20 20  ere, TK_AND);.  
2cca6 20 20 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20    .  /* Special 
2cca7 63 61 73 65 3a 20 61 20 57 48 45 52 45 20 63 6c  case: a WHERE cl
2cca8 61 75 73 65 20 74 68 61 74 20 69 73 20 63 6f 6e  ause that is con
2cca9 73 74 61 6e 74 2e 20 20 45 76 61 6c 75 61 74 65  stant.  Evaluate
2ccaa 20 74 68 65 0a 20 20 2a 2a 20 65 78 70 72 65 73   the.  ** expres
2ccab 73 69 6f 6e 20 61 6e 64 20 65 69 74 68 65 72 20  sion and either 
2ccac 6a 75 6d 70 20 6f 76 65 72 20 61 6c 6c 20 6f 66  jump over all of
2ccad 20 74 68 65 20 63 6f 64 65 20 6f 72 20 66 61 6c   the code or fal
2ccae 6c 20 74 68 72 75 2e 0a 20 20 2a 2f 0a 20 20 69  l thru..  */.  i
2ccaf 66 28 20 70 57 68 65 72 65 20 26 26 20 28 70 54  f( pWhere && (pT
2ccb0 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 30 20  abList->nSrc==0 
2ccb1 7c 7c 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  || sqlite3ExprIs
2ccb2 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28  ConstantNotJoin(
2ccb3 70 57 68 65 72 65 29 29 20 29 7b 0a 20 20 20 20  pWhere)) ){.    
2ccb4 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c  sqlite3ExprIfFal
2ccb5 73 65 28 70 50 61 72 73 65 2c 20 70 57 68 65 72  se(pParse, pWher
2ccb6 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61  e, pWInfo->iBrea
2ccb7 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  k, SQLITE_JUMPIF
2ccb8 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 57 68 65 72  NULL);.    pWher
2ccb9 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e = 0;.  }..  /*
2ccba 20 41 73 73 69 67 6e 20 61 20 62 69 74 20 66 72   Assign a bit fr
2ccbb 6f 6d 20 74 68 65 20 62 69 74 6d 61 73 6b 20 74  om the bitmask t
2ccbc 6f 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20  o every term in 
2ccbd 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
2ccbe 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20  .  **.  ** When 
2ccbf 61 73 73 69 67 6e 69 6e 67 20 62 69 74 6d 61 73  assigning bitmas
2ccc0 6b 20 76 61 6c 75 65 73 20 74 6f 20 46 52 4f 4d  k values to FROM
2ccc1 20 63 6c 61 75 73 65 20 63 75 72 73 6f 72 73 2c   clause cursors,
2ccc2 20 69 74 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a   it must be.  **
2ccc3 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 69   the case that i
2ccc4 66 20 58 20 69 73 20 74 68 65 20 62 69 74 6d 61  f X is the bitma
2ccc5 73 6b 20 66 6f 72 20 74 68 65 20 4e 2d 74 68 20  sk for the N-th 
2ccc6 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
2ccc7 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 62   then.  ** the b
2ccc8 69 74 6d 61 73 6b 20 66 6f 72 20 61 6c 6c 20 46  itmask for all F
2ccc9 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 73  ROM clause terms
2ccca 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20   to the left of 
2cccb 74 68 65 20 4e 2d 74 68 20 74 65 72 6d 0a 20 20  the N-th term.  
2cccc 2a 2a 20 69 73 20 28 58 2d 31 29 2e 20 20 20 41  ** is (X-1).   A
2cccd 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 66 72 6f  n expression fro
2ccce 6d 20 74 68 65 20 4f 4e 20 63 6c 61 75 73 65 20  m the ON clause 
2cccf 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20 63  of a LEFT JOIN c
2ccd0 61 6e 20 75 73 65 0a 20 20 2a 2a 20 69 74 73 20  an use.  ** its 
2ccd1 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54  Expr.iRightJoinT
2ccd2 61 62 6c 65 20 76 61 6c 75 65 20 74 6f 20 66 69  able value to fi
2ccd3 6e 64 20 74 68 65 20 62 69 74 6d 61 73 6b 20 6f  nd the bitmask o
2ccd4 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  f the right tabl
2ccd5 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6a 6f  e.  ** of the jo
2ccd6 69 6e 2e 20 20 53 75 62 74 72 61 63 74 69 6e 67  in.  Subtracting
2ccd7 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 20 72 69   one from the ri
2ccd8 67 68 74 20 74 61 62 6c 65 20 62 69 74 6d 61 73  ght table bitmas
2ccd9 6b 20 67 69 76 65 73 20 61 0a 20 20 2a 2a 20 62  k gives a.  ** b
2ccda 69 74 6d 61 73 6b 20 66 6f 72 20 61 6c 6c 20 74  itmask for all t
2ccdb 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c 65 66  ables to the lef
2ccdc 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20  t of the join.  
2ccdd 4b 6e 6f 77 69 6e 67 20 74 68 65 20 62 69 74 6d  Knowing the bitm
2ccde 61 73 6b 0a 20 20 2a 2a 20 66 6f 72 20 61 6c 6c  ask.  ** for all
2ccdf 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c   tables to the l
2cce0 65 66 74 20 6f 66 20 61 20 6c 65 66 74 20 6a 6f  eft of a left jo
2cce1 69 6e 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e  in is important.
2cce2 20 20 54 69 63 6b 65 74 20 23 33 30 31 35 2e 0a    Ticket #3015..
2cce3 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
2cce4 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2cce5 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 72 65 61  ; i++){.    crea
2cce6 74 65 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  teMask(pMaskSet,
2cce7 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e   pTabList->a[i].
2cce8 69 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69  iCursor);.  }.#i
2cce9 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 7b  fndef NDEBUG.  {
2ccea 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 74 6f 54  .    Bitmask toT
2cceb 68 65 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20  heLeft = 0;.    
2ccec 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c  for(i=0; i<pTabL
2cced 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  ist->nSrc; i++){
2ccee 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 6d  .      Bitmask m
2ccef 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b   = getMask(pMask
2ccf0 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61  Set, pTabList->a
2ccf1 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20  [i].iCursor);.  
2ccf2 20 20 20 20 61 73 73 65 72 74 28 20 28 6d 2d 31      assert( (m-1
2ccf3 29 3d 3d 74 6f 54 68 65 4c 65 66 74 20 29 3b 0a  )==toTheLeft );.
2ccf4 20 20 20 20 20 20 74 6f 54 68 65 4c 65 66 74 20        toTheLeft 
2ccf5 7c 3d 20 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  |= m;.    }.  }.
2ccf6 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e 61  #endif..  /* Ana
2ccf7 6c 79 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65 20  lyze all of the 
2ccf8 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  subexpressions. 
2ccf9 20 4e 6f 74 65 20 74 68 61 74 20 65 78 70 72 41   Note that exprA
2ccfa 6e 61 6c 79 7a 65 28 29 20 6d 69 67 68 74 0a 20  nalyze() might. 
2ccfb 20 2a 2a 20 61 64 64 20 6e 65 77 20 76 69 72 74   ** add new virt
2ccfc 75 61 6c 20 74 65 72 6d 73 20 6f 6e 74 6f 20 74  ual terms onto t
2ccfd 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48  he end of the WH
2ccfe 45 52 45 20 63 6c 61 75 73 65 2e 20 20 57 65 20  ERE clause.  We 
2ccff 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 77 61 6e 74  do not.  ** want
2cd00 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 73   to analyze thes
2cd01 65 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73 2c  e virtual terms,
2cd02 20 73 6f 20 73 74 61 72 74 20 61 6e 61 6c 79 7a   so start analyz
2cd03 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64 0a 20  ing at the end. 
2cd04 20 2a 2a 20 61 6e 64 20 77 6f 72 6b 20 66 6f 72   ** and work for
2cd05 77 61 72 64 20 73 6f 20 74 68 61 74 20 74 68 65  ward so that the
2cd06 20 61 64 64 65 64 20 76 69 72 74 75 61 6c 20 74   added virtual t
2cd07 65 72 6d 73 20 61 72 65 20 6e 65 76 65 72 20 70  erms are never p
2cd08 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20  rocessed..  */. 
2cd09 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28   exprAnalyzeAll(
2cd0a 70 54 61 62 4c 69 73 74 2c 20 70 57 43 29 3b 0a  pTabList, pWC);.
2cd0b 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2cd0c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
2cd0d 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72 72  to whereBeginErr
2cd0e 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  or;.  }..  /* Ch
2cd0f 6f 73 65 20 74 68 65 20 62 65 73 74 20 69 6e 64  ose the best ind
2cd10 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20 65 61  ex to use for ea
2cd11 63 68 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  ch table in the 
2cd12 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a  FROM clause..  *
2cd13 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70  *.  ** This loop
2cd14 20 66 69 6c 6c 73 20 69 6e 20 74 68 65 20 66 6f   fills in the fo
2cd15 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 3a 0a  llowing fields:.
2cd16 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 70 57 49 6e    **.  **   pWIn
2cd17 66 6f 2d 3e 61 5b 5d 2e 70 49 64 78 20 20 20 20  fo->a[].pIdx    
2cd18 20 20 54 68 65 20 69 6e 64 65 78 20 74 6f 20 75    The index to u
2cd19 73 65 20 66 6f 72 20 74 68 69 73 20 6c 65 76 65  se for this leve
2cd1a 6c 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 20  l of the loop.. 
2cd1b 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b   **   pWInfo->a[
2cd1c 5d 2e 77 73 46 6c 61 67 73 20 20 20 57 48 45 52  ].wsFlags   WHER
2cd1d 45 5f 78 78 78 20 66 6c 61 67 73 20 61 73 73 6f  E_xxx flags asso
2cd1e 63 69 61 74 65 64 20 77 69 74 68 20 70 49 64 78  ciated with pIdx
2cd1f 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
2cd20 61 5b 5d 2e 6e 45 71 20 20 20 20 20 20 20 54 68  a[].nEq       Th
2cd21 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 61  e number of == a
2cd22 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74  nd IN constraint
2cd23 73 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  s.  **   pWInfo-
2cd24 3e 61 5b 5d 2e 69 46 72 6f 6d 20 20 20 20 20 57  >a[].iFrom     W
2cd25 68 69 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65  hich term of the
2cd26 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20   FROM clause is 
2cd27 62 65 69 6e 67 20 63 6f 64 65 64 0a 20 20 2a 2a  being coded.  **
2cd28 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69     pWInfo->a[].i
2cd29 54 61 62 43 75 72 20 20 20 54 68 65 20 56 44 42  TabCur   The VDB
2cd2a 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65  E cursor for the
2cd2b 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a   database table.
2cd2c 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61    **   pWInfo->a
2cd2d 5b 5d 2e 69 49 64 78 43 75 72 20 20 20 54 68 65  [].iIdxCur   The
2cd2e 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72   VDBE cursor for
2cd2f 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20   the index.  ** 
2cd30 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 54    pWInfo->a[].pT
2cd31 65 72 6d 20 20 20 20 20 57 68 65 6e 20 77 73 46  erm     When wsF
2cd32 6c 61 67 73 3d 3d 57 4f 5f 4f 52 2c 20 74 68 65  lags==WO_OR, the
2cd33 20 4f 52 2d 63 6c 61 75 73 65 20 74 65 72 6d 0a   OR-clause term.
2cd34 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c    **.  ** This l
2cd35 6f 6f 70 20 61 6c 73 6f 20 66 69 67 75 72 65 73  oop also figures
2cd36 20 6f 75 74 20 74 68 65 20 6e 65 73 74 69 6e 67   out the nesting
2cd37 20 6f 72 64 65 72 20 6f 66 20 74 61 62 6c 65 73   order of tables
2cd38 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20 2a   in the FROM.  *
2cd39 2a 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20  * clause..  */. 
2cd3a 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e 28 42 69   notReady = ~(Bi
2cd3b 74 6d 61 73 6b 29 30 3b 0a 20 20 70 54 61 62 49  tmask)0;.  pTabI
2cd3c 74 65 6d 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e  tem = pTabList->
2cd3d 61 3b 0a 20 20 70 4c 65 76 65 6c 20 3d 20 70 57  a;.  pLevel = pW
2cd3e 49 6e 66 6f 2d 3e 61 3b 0a 20 20 61 6e 64 46 6c  Info->a;.  andFl
2cd3f 61 67 73 20 3d 20 7e 30 3b 0a 20 20 57 48 45 52  ags = ~0;.  WHER
2cd40 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74  ETRACE(("*** Opt
2cd41 69 6d 69 7a 65 72 20 53 74 61 72 74 20 2a 2a 2a  imizer Start ***
2cd42 5c 6e 22 29 29 3b 0a 20 20 66 6f 72 28 69 3d 69  \n"));.  for(i=i
2cd43 46 72 6f 6d 3d 30 2c 20 70 4c 65 76 65 6c 3d 70  From=0, pLevel=p
2cd44 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62  WInfo->a; i<pTab
2cd45 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
2cd46 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20   pLevel++){.    
2cd47 57 68 65 72 65 43 6f 73 74 20 62 65 73 74 50 6c  WhereCost bestPl
2cd48 61 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  an;         /* M
2cd49 6f 73 74 20 65 66 66 69 63 69 65 6e 74 20 70 6c  ost efficient pl
2cd4a 61 6e 20 73 65 65 6e 20 73 6f 20 66 61 72 20 2a  an seen so far *
2cd4b 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  /.    Index *pId
2cd4c 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
2cd4d 20 20 2f 2a 20 49 6e 64 65 78 20 66 6f 72 20 46    /* Index for F
2cd4e 52 4f 4d 20 74 61 62 6c 65 20 61 74 20 70 54 61  ROM table at pTa
2cd4f 62 49 74 65 6d 20 2a 2f 0a 20 20 20 20 69 6e 74  bItem */.    int
2cd50 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
2cd51 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
2cd52 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 46 52 4f  looping over FRO
2cd53 4d 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20  M tables */.    
2cd54 69 6e 74 20 62 65 73 74 4a 20 3d 20 30 3b 20 20  int bestJ = 0;  
2cd55 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2cd56 68 65 20 76 61 6c 75 65 20 6f 66 20 6a 20 2a 2f  he value of j */
2cd57 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 3b 20  .    Bitmask m; 
2cd58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cd59 20 2f 2a 20 42 69 74 6d 61 73 6b 20 76 61 6c 75   /* Bitmask valu
2cd5a 65 20 66 6f 72 20 6a 20 6f 72 20 62 65 73 74 4a  e for j or bestJ
2cd5b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 6e 63 65   */.    int once
2cd5c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2cd5d 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 65 6e      /* True when
2cd5e 20 66 69 72 73 74 20 74 61 62 6c 65 20 69 73 20   first table is 
2cd5f 73 65 65 6e 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d  seen */..    mem
2cd60 73 65 74 28 26 62 65 73 74 50 6c 61 6e 2c 20 30  set(&bestPlan, 0
2cd61 2c 20 73 69 7a 65 6f 66 28 62 65 73 74 50 6c 61  , sizeof(bestPla
2cd62 6e 29 29 3b 0a 20 20 20 20 62 65 73 74 50 6c 61  n));.    bestPla
2cd63 6e 2e 72 43 6f 73 74 20 3d 20 53 51 4c 49 54 45  n.rCost = SQLITE
2cd64 5f 42 49 47 5f 44 42 4c 3b 0a 20 20 20 20 66 6f  _BIG_DBL;.    fo
2cd65 72 28 6a 3d 69 46 72 6f 6d 2c 20 70 54 61 62 49  r(j=iFrom, pTabI
2cd66 74 65 6d 3d 26 70 54 61 62 4c 69 73 74 2d 3e 61  tem=&pTabList->a
2cd67 5b 6a 5d 3b 20 6a 3c 70 54 61 62 4c 69 73 74 2d  [j]; j<pTabList-
2cd68 3e 6e 53 72 63 3b 20 6a 2b 2b 2c 20 70 54 61 62  >nSrc; j++, pTab
2cd69 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69  Item++){.      i
2cd6a 6e 74 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 3b  nt doNotReorder;
2cd6b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
2cd6c 68 69 73 20 74 61 62 6c 65 20 73 68 6f 75 6c 64  his table should
2cd6d 20 6e 6f 74 20 62 65 20 72 65 6f 72 64 65 72 65   not be reordere
2cd6e 64 20 2a 2f 0a 20 20 20 20 20 20 57 68 65 72 65  d */.      Where
2cd6f 43 6f 73 74 20 73 43 6f 73 74 3b 20 20 20 20 20  Cost sCost;     
2cd70 2f 2a 20 43 6f 73 74 20 69 6e 66 6f 72 6d 61 74  /* Cost informat
2cd71 69 6f 6e 20 66 72 6f 6d 20 62 65 73 74 5b 56 69  ion from best[Vi
2cd72 72 74 75 61 6c 5d 49 6e 64 65 78 28 29 20 2a 2f  rtual]Index() */
2cd73 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
2cd74 2a 70 4f 72 64 65 72 42 79 3b 20 20 2f 2a 20 4f  *pOrderBy;  /* O
2cd75 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66  RDER BY clause f
2cd76 6f 72 20 69 6e 64 65 78 20 74 6f 20 6f 70 74 69  or index to opti
2cd77 6d 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 20 20 64  mize */..      d
2cd78 6f 4e 6f 74 52 65 6f 72 64 65 72 20 3d 20 20 28  oNotReorder =  (
2cd79 70 54 61 62 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79  pTabItem->jointy
2cd7a 70 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54  pe & (JT_LEFT|JT
2cd7b 5f 43 52 4f 53 53 29 29 21 3d 30 3b 0a 20 20 20  _CROSS))!=0;.   
2cd7c 20 20 20 69 66 28 20 6f 6e 63 65 20 26 26 20 64     if( once && d
2cd7d 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29 20 62 72  oNotReorder ) br
2cd7e 65 61 6b 3b 0a 20 20 20 20 20 20 6d 20 3d 20 67  eak;.      m = g
2cd7f 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  etMask(pMaskSet,
2cd80 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
2cd81 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  or);.      if( (
2cd82 6d 20 26 20 6e 6f 74 52 65 61 64 79 29 3d 3d 30  m & notReady)==0
2cd83 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
2cd84 6a 3d 3d 69 46 72 6f 6d 20 29 20 69 46 72 6f 6d  j==iFrom ) iFrom
2cd85 2b 2b 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74  ++;.        cont
2cd86 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
2cd87 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 28      pOrderBy = (
2cd88 28 69 3d 3d 30 20 26 26 20 70 70 4f 72 64 65 72  (i==0 && ppOrder
2cd89 42 79 20 29 3f 2a 70 70 4f 72 64 65 72 42 79 3a  By )?*ppOrderBy:
2cd8a 30 29 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72  0);..      asser
2cd8b 74 28 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61  t( pTabItem->pTa
2cd8c 62 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  b );.#ifndef SQL
2cd8d 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2cd8e 54 41 42 4c 45 0a 20 20 20 20 20 20 69 66 28 20  TABLE.      if( 
2cd8f 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 49 74  IsVirtual(pTabIt
2cd90 65 6d 2d 3e 70 54 61 62 29 20 29 7b 0a 20 20 20  em->pTab) ){.   
2cd91 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64       sqlite3_ind
2cd92 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 20 3d 20 26  ex_info **pp = &
2cd93 70 57 49 6e 66 6f 2d 3e 61 5b 6a 5d 2e 70 49 64  pWInfo->a[j].pId
2cd94 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 62  xInfo;.        b
2cd95 65 73 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28  estVirtualIndex(
2cd96 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 54 61  pParse, pWC, pTa
2cd97 62 49 74 65 6d 2c 20 6e 6f 74 52 65 61 64 79 2c  bItem, notReady,
2cd98 20 70 4f 72 64 65 72 42 79 2c 20 26 73 43 6f 73   pOrderBy, &sCos
2cd99 74 2c 20 70 70 29 3b 0a 20 20 20 20 20 20 7d 65  t, pp);.      }e
2cd9a 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20 20 20  lse .#endif.    
2cd9b 20 20 7b 0a 20 20 20 20 20 20 20 20 62 65 73 74    {.        best
2cd9c 42 74 72 65 65 49 6e 64 65 78 28 70 50 61 72 73  BtreeIndex(pPars
2cd9d 65 2c 20 70 57 43 2c 20 70 54 61 62 49 74 65 6d  e, pWC, pTabItem
2cd9e 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64  , notReady, pOrd
2cd9f 65 72 42 79 2c 20 26 73 43 6f 73 74 29 3b 0a 20  erBy, &sCost);. 
2cda0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2cda1 20 6f 6e 63 65 3d 3d 30 20 7c 7c 20 73 43 6f 73   once==0 || sCos
2cda2 74 2e 72 43 6f 73 74 3c 62 65 73 74 50 6c 61 6e  t.rCost<bestPlan
2cda3 2e 72 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20  .rCost ){.      
2cda4 20 20 6f 6e 63 65 20 3d 20 31 3b 0a 20 20 20 20    once = 1;.    
2cda5 20 20 20 20 62 65 73 74 50 6c 61 6e 20 3d 20 73      bestPlan = s
2cda6 43 6f 73 74 3b 0a 20 20 20 20 20 20 20 20 62 65  Cost;.        be
2cda7 73 74 4a 20 3d 20 6a 3b 0a 20 20 20 20 20 20 7d  stJ = j;.      }
2cda8 0a 20 20 20 20 20 20 69 66 28 20 64 6f 4e 6f 74  .      if( doNot
2cda9 52 65 6f 72 64 65 72 20 29 20 62 72 65 61 6b 3b  Reorder ) break;
2cdaa 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
2cdab 74 28 20 6f 6e 63 65 20 29 3b 0a 20 20 20 20 61  t( once );.    a
2cdac 73 73 65 72 74 28 20 6e 6f 74 52 65 61 64 79 20  ssert( notReady 
2cdad 26 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53  & getMask(pMaskS
2cdae 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  et, pTabList->a[
2cdaf 62 65 73 74 4a 5d 2e 69 43 75 72 73 6f 72 29 20  bestJ].iCursor) 
2cdb0 29 3b 0a 20 20 20 20 57 48 45 52 45 54 52 41 43  );.    WHERETRAC
2cdb1 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65  E(("*** Optimize
2cdb2 72 20 73 65 6c 65 63 74 73 20 74 61 62 6c 65 20  r selects table 
2cdb3 25 64 20 66 6f 72 20 6c 6f 6f 70 20 25 64 5c 6e  %d for loop %d\n
2cdb4 22 2c 20 62 65 73 74 4a 2c 0a 20 20 20 20 20 20  ", bestJ,.      
2cdb5 20 20 20 20 20 70 4c 65 76 65 6c 2d 70 57 49 6e       pLevel-pWIn
2cdb6 66 6f 2d 3e 61 29 29 3b 0a 20 20 20 20 69 66 28  fo->a));.    if(
2cdb7 20 28 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e   (bestPlan.plan.
2cdb8 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2cdb9 4f 52 44 45 52 42 59 29 21 3d 30 20 29 7b 0a 20  ORDERBY)!=0 ){. 
2cdba 20 20 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20       *ppOrderBy 
2cdbb 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  = 0;.    }.    a
2cdbc 6e 64 46 6c 61 67 73 20 26 3d 20 62 65 73 74 50  ndFlags &= bestP
2cdbd 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  lan.plan.wsFlags
2cdbe 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 6c  ;.    pLevel->pl
2cdbf 61 6e 20 3d 20 62 65 73 74 50 6c 61 6e 2e 70 6c  an = bestPlan.pl
2cdc0 61 6e 3b 0a 20 20 20 20 69 66 28 20 62 65 73 74  an;.    if( best
2cdc1 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  Plan.plan.wsFlag
2cdc2 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45  s & WHERE_INDEXE
2cdc3 44 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 76 65  D ){.      pLeve
2cdc4 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20 70 50 61  l->iIdxCur = pPa
2cdc5 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
2cdc6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c   }else{.      pL
2cdc7 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20  evel->iIdxCur = 
2cdc8 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 6f  -1;.    }.    no
2cdc9 74 52 65 61 64 79 20 26 3d 20 7e 67 65 74 4d 61  tReady &= ~getMa
2cdca 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61  sk(pMaskSet, pTa
2cdcb 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e  bList->a[bestJ].
2cdcc 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 70 4c  iCursor);.    pL
2cdcd 65 76 65 6c 2d 3e 69 46 72 6f 6d 20 3d 20 28 75  evel->iFrom = (u
2cdce 38 29 62 65 73 74 4a 3b 0a 0a 20 20 20 20 2f 2a  8)bestJ;..    /*
2cdcf 20 43 68 65 63 6b 20 74 68 61 74 20 69 66 20 74   Check that if t
2cdd0 68 65 20 74 61 62 6c 65 20 73 63 61 6e 6e 65 64  he table scanned
2cdd1 20 62 79 20 74 68 69 73 20 6c 6f 6f 70 20 69 74   by this loop it
2cdd2 65 72 61 74 69 6f 6e 20 68 61 64 20 61 6e 0a 20  eration had an. 
2cdd3 20 20 20 2a 2a 20 49 4e 44 45 58 45 44 20 42 59     ** INDEXED BY
2cdd4 20 63 6c 61 75 73 65 20 61 74 74 61 63 68 65 64   clause attached
2cdd5 20 74 6f 20 69 74 2c 20 74 68 61 74 20 74 68 65   to it, that the
2cdd6 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 69 73 20   named index is 
2cdd7 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 73 65  being.    ** use
2cdd8 64 20 66 6f 72 20 74 68 65 20 73 63 61 6e 2e 20  d for the scan. 
2cdd9 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 71 75 65  If not, then que
2cdda 72 79 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 68  ry compilation h
2cddb 61 73 20 66 61 69 6c 65 64 2e 0a 20 20 20 20 2a  as failed..    *
2cddc 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  * Return an erro
2cddd 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49  r..    */.    pI
2cdde 64 78 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61  dx = pTabList->a
2cddf 5b 62 65 73 74 4a 5d 2e 70 49 6e 64 65 78 3b 0a  [bestJ].pIndex;.
2cde0 20 20 20 20 69 66 28 20 70 49 64 78 20 29 7b 0a      if( pIdx ){.
2cde1 20 20 20 20 20 20 69 66 28 20 28 62 65 73 74 50        if( (bestP
2cde2 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  lan.plan.wsFlags
2cde3 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44   & WHERE_INDEXED
2cde4 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
2cde5 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2cde6 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
2cde7 75 73 65 20 69 6e 64 65 78 3a 20 25 73 22 2c 20  use index: %s", 
2cde8 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pIdx->zName);.  
2cde9 20 20 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65        goto where
2cdea 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 20 20  BeginError;.    
2cdeb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2cdec 20 2f 2a 20 49 66 20 61 6e 20 49 4e 44 45 58 45   /* If an INDEXE
2cded 44 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 75  D BY clause is u
2cdee 73 65 64 2c 20 74 68 65 20 62 65 73 74 49 6e 64  sed, the bestInd
2cdef 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  ex() function is
2cdf0 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 75 61 72  .        ** guar
2cdf1 61 6e 74 65 65 64 20 74 6f 20 66 69 6e 64 20 74  anteed to find t
2cdf2 68 65 20 69 6e 64 65 78 20 73 70 65 63 69 66 69  he index specifi
2cdf3 65 64 20 69 6e 20 74 68 65 20 49 4e 44 45 58 45  ed in the INDEXE
2cdf4 44 20 42 59 20 63 6c 61 75 73 65 0a 20 20 20 20  D BY clause.    
2cdf5 20 20 20 20 2a 2a 20 69 66 20 69 74 20 66 69 6e      ** if it fin
2cdf6 64 20 61 6e 20 69 6e 64 65 78 20 61 74 20 61 6c  d an index at al
2cdf7 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  l. */.        as
2cdf8 73 65 72 74 28 20 62 65 73 74 50 6c 61 6e 2e 70  sert( bestPlan.p
2cdf9 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 70 49 64 78  lan.u.pIdx==pIdx
2cdfa 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
2cdfb 7d 0a 20 20 7d 0a 20 20 57 48 45 52 45 54 52 41  }.  }.  WHERETRA
2cdfc 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a  CE(("*** Optimiz
2cdfd 65 72 20 46 69 6e 69 73 68 65 64 20 2a 2a 2a 5c  er Finished ***\
2cdfe 6e 22 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72  n"));.  if( pPar
2cdff 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
2ce00 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2ce01 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42 65      goto whereBe
2ce02 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20  ginError;.  }.. 
2ce03 20 2f 2a 20 49 66 20 74 68 65 20 74 6f 74 61 6c   /* If the total
2ce04 20 71 75 65 72 79 20 6f 6e 6c 79 20 73 65 6c 65   query only sele
2ce05 63 74 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  cts a single row
2ce06 2c 20 74 68 65 6e 20 74 68 65 20 4f 52 44 45 52  , then the ORDER
2ce07 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65 20   BY.  ** clause 
2ce08 69 73 20 69 72 72 65 6c 65 76 61 6e 74 2e 0a 20  is irrelevant.. 
2ce09 20 2a 2f 0a 20 20 69 66 28 20 28 61 6e 64 46 6c   */.  if( (andFl
2ce0a 61 67 73 20 26 20 57 48 45 52 45 5f 55 4e 49 51  ags & WHERE_UNIQ
2ce0b 55 45 29 21 3d 30 20 26 26 20 70 70 4f 72 64 65  UE)!=0 && ppOrde
2ce0c 72 42 79 20 29 7b 0a 20 20 20 20 2a 70 70 4f 72  rBy ){.    *ppOr
2ce0d 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a  derBy = 0;.  }..
2ce0e 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 6c 6c    /* If the call
2ce0f 65 72 20 69 73 20 61 6e 20 55 50 44 41 54 45 20  er is an UPDATE 
2ce10 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d  or DELETE statem
2ce11 65 6e 74 20 74 68 61 74 20 69 73 20 72 65 71 75  ent that is requ
2ce12 65 73 74 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 75  esting.  ** to u
2ce13 73 65 20 61 20 6f 6e 65 2d 70 61 73 73 20 61 6c  se a one-pass al
2ce14 67 6f 72 69 74 68 6d 2c 20 64 65 74 65 72 6d 69  gorithm, determi
2ce15 6e 65 20 69 66 20 74 68 69 73 20 69 73 20 61 70  ne if this is ap
2ce16 70 72 6f 70 72 69 61 74 65 2e 0a 20 20 2a 2a 20  propriate..  ** 
2ce17 54 68 65 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67  The one-pass alg
2ce18 6f 72 69 74 68 6d 20 6f 6e 6c 79 20 77 6f 72 6b  orithm only work
2ce19 73 20 69 66 20 74 68 65 20 57 48 45 52 45 20 63  s if the WHERE c
2ce1a 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74  lause constraint
2ce1b 73 0a 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65  s.  ** the state
2ce1c 6d 65 6e 74 20 74 6f 20 75 70 64 61 74 65 20 61  ment to update a
2ce1d 20 73 69 6e 67 6c 65 20 72 6f 77 2e 0a 20 20 2a   single row..  *
2ce1e 2f 0a 20 20 61 73 73 65 72 74 28 20 28 77 63 74  /.  assert( (wct
2ce1f 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  rlFlags & WHERE_
2ce20 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 29  ONEPASS_DESIRED)
2ce21 3d 3d 30 20 7c 7c 20 70 57 49 6e 66 6f 2d 3e 6e  ==0 || pWInfo->n
2ce22 4c 65 76 65 6c 3d 3d 31 20 29 3b 0a 20 20 69 66  Level==1 );.  if
2ce23 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20  ( (wctrlFlags & 
2ce24 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45  WHERE_ONEPASS_DE
2ce25 53 49 52 45 44 29 21 3d 30 20 26 26 20 28 61 6e  SIRED)!=0 && (an
2ce26 64 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 55  dFlags & WHERE_U
2ce27 4e 49 51 55 45 29 21 3d 30 20 29 7b 0a 20 20 20  NIQUE)!=0 ){.   
2ce28 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61   pWInfo->okOnePa
2ce29 73 73 20 3d 20 31 3b 0a 20 20 20 20 70 57 49 6e  ss = 1;.    pWIn
2ce2a 66 6f 2d 3e 61 5b 30 5d 2e 70 6c 61 6e 2e 77 73  fo->a[0].plan.ws
2ce2b 46 6c 61 67 73 20 26 3d 20 7e 57 48 45 52 45 5f  Flags &= ~WHERE_
2ce2c 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20  IDX_ONLY;.  }.. 
2ce2d 20 2f 2a 20 4f 70 65 6e 20 61 6c 6c 20 74 61 62   /* Open all tab
2ce2e 6c 65 73 20 69 6e 20 74 68 65 20 70 54 61 62 4c  les in the pTabL
2ce2f 69 73 74 20 61 6e 64 20 61 6e 79 20 69 6e 64 69  ist and any indi
2ce30 63 65 73 20 73 65 6c 65 63 74 65 64 20 66 6f 72  ces selected for
2ce31 0a 20 20 2a 2a 20 73 65 61 72 63 68 69 6e 67 20  .  ** searching 
2ce32 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a 20 20  those tables..  
2ce33 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65  */.  sqlite3Code
2ce34 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61  VerifySchema(pPa
2ce35 72 73 65 2c 20 2d 31 29 3b 20 2f 2a 20 49 6e 73  rse, -1); /* Ins
2ce36 65 72 74 20 74 68 65 20 63 6f 6f 6b 69 65 20 76  ert the cookie v
2ce37 65 72 69 66 69 65 72 20 47 6f 74 6f 20 2a 2f 0a  erifier Goto */.
2ce38 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65    for(i=0, pLeve
2ce39 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70  l=pWInfo->a; i<p
2ce3a 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
2ce3b 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20  ++, pLevel++){. 
2ce3c 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20     Table *pTab; 
2ce3d 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20      /* Table to 
2ce3e 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  open */.    int 
2ce3f 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  iDb;         /* 
2ce40 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73  Index of databas
2ce41 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62  e containing tab
2ce42 6c 65 2f 69 6e 64 65 78 20 2a 2f 0a 0a 23 69 66  le/index */..#if
2ce43 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2ce44 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 69 66 28  _EXPLAIN.    if(
2ce45 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
2ce46 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 63 68 61  ==2 ){.      cha
2ce47 72 20 2a 7a 4d 73 67 3b 0a 20 20 20 20 20 20 73  r *zMsg;.      s
2ce48 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2ce49 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54 61  em *pItem = &pTa
2ce4a 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d  bList->a[pLevel-
2ce4b 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 20 20 7a  >iFrom];.      z
2ce4c 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Msg = sqlite3MPr
2ce4d 69 6e 74 66 28 64 62 2c 20 22 54 41 42 4c 45 20  intf(db, "TABLE 
2ce4e 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  %s", pItem->zNam
2ce4f 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  e);.      if( pI
2ce50 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20  tem->zAlias ){. 
2ce51 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
2ce52 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62  lite3MAppendf(db
2ce53 2c 20 7a 4d 73 67 2c 20 22 25 73 20 41 53 20 25  , zMsg, "%s AS %
2ce54 73 22 2c 20 7a 4d 73 67 2c 20 70 49 74 65 6d 2d  s", zMsg, pItem-
2ce55 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 20 20  >zAlias);.      
2ce56 7d 0a 20 20 20 20 20 20 69 66 28 20 28 70 4c 65  }.      if( (pLe
2ce57 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2ce58 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45  s & WHERE_INDEXE
2ce59 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  D)!=0 ){.       
2ce5a 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
2ce5b 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
2ce5c 2c 20 22 25 73 20 57 49 54 48 20 49 4e 44 45 58  , "%s WITH INDEX
2ce5d 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %s",.          
2ce5e 20 7a 4d 73 67 2c 20 70 4c 65 76 65 6c 2d 3e 70   zMsg, pLevel->p
2ce5f 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d  lan.u.pIdx->zNam
2ce60 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  e);.      }else 
2ce61 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
2ce62 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2ce63 5f 4d 55 4c 54 49 5f 4f 52 20 29 7b 0a 20 20 20  _MULTI_OR ){.   
2ce64 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
2ce65 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20  te3MAppendf(db, 
2ce66 7a 4d 73 67 2c 20 22 25 73 20 56 49 41 20 4d 55  zMsg, "%s VIA MU
2ce67 4c 54 49 2d 49 4e 44 45 58 20 55 4e 49 4f 4e 22  LTI-INDEX UNION"
2ce68 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d  , zMsg);.      }
2ce69 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d  else if( pLevel-
2ce6a 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2ce6b 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c  (WHERE_ROWID_EQ|
2ce6c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47  WHERE_ROWID_RANG
2ce6d 45 29 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d  E) ){.        zM
2ce6e 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70  sg = sqlite3MApp
2ce6f 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22  endf(db, zMsg, "
2ce70 25 73 20 55 53 49 4e 47 20 50 52 49 4d 41 52 59  %s USING PRIMARY
2ce71 20 4b 45 59 22 2c 20 7a 4d 73 67 29 3b 0a 20 20   KEY", zMsg);.  
2ce72 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
2ce73 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2ce74 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 65 6c 73  LTABLE.      els
2ce75 65 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70  e if( (pLevel->p
2ce76 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2ce77 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  ERE_VIRTUALTABLE
2ce78 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
2ce79 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2ce7a 66 6f 20 2a 70 56 74 61 62 49 64 78 20 3d 20 70  fo *pVtabIdx = p
2ce7b 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 56  Level->plan.u.pV
2ce7c 74 61 62 49 64 78 3b 0a 20 20 20 20 20 20 20 20  tabIdx;.        
2ce7d 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41  zMsg = sqlite3MA
2ce7e 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c  ppendf(db, zMsg,
2ce7f 20 22 25 73 20 56 49 52 54 55 41 4c 20 54 41 42   "%s VIRTUAL TAB
2ce80 4c 45 20 49 4e 44 45 58 20 25 64 3a 25 73 22 2c  LE INDEX %d:%s",
2ce81 20 7a 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20   zMsg,.         
2ce82 20 20 20 20 20 20 20 20 20 20 20 70 56 74 61 62             pVtab
2ce83 49 64 78 2d 3e 69 64 78 4e 75 6d 2c 20 70 56 74  Idx->idxNum, pVt
2ce84 61 62 49 64 78 2d 3e 69 64 78 53 74 72 29 3b 0a  abIdx->idxStr);.
2ce85 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
2ce86 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d       if( pLevel-
2ce87 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2ce88 57 48 45 52 45 5f 4f 52 44 45 52 42 59 20 29 7b  WHERE_ORDERBY ){
2ce89 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  .        zMsg = 
2ce8a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
2ce8b 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 4f 52  db, zMsg, "%s OR
2ce8c 44 45 52 20 42 59 22 2c 20 7a 4d 73 67 29 3b 0a  DER BY", zMsg);.
2ce8d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2ce8e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2ce8f 76 2c 20 4f 50 5f 45 78 70 6c 61 69 6e 2c 20 69  v, OP_Explain, i
2ce90 2c 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 2c  , pLevel->iFrom,
2ce91 20 30 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e   0, zMsg, P4_DYN
2ce92 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  AMIC);.    }.#en
2ce93 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2ce94 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20 20  IT_EXPLAIN */.  
2ce95 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70 54    pTabItem = &pT
2ce96 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c  abList->a[pLevel
2ce97 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 70 54  ->iFrom];.    pT
2ce98 61 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70  ab = pTabItem->p
2ce99 54 61 62 3b 0a 20 20 20 20 69 44 62 20 3d 20 73  Tab;.    iDb = s
2ce9a 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2ce9b 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
2ce9c 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
2ce9d 20 20 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74      if( (pTab->t
2ce9e 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68  abFlags & TF_Eph
2ce9f 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70 54  emeral)!=0 || pT
2cea0 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63 6f  ab->pSelect ) co
2cea1 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20  ntinue;.#ifndef 
2cea2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2cea3 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28  UALTABLE.    if(
2cea4 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
2cea5 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56  sFlags & WHERE_V
2cea6 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20  IRTUALTABLE)!=0 
2cea7 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 75  ){.      int iCu
2cea8 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  r = pTabItem->iC
2cea9 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c  ursor;.      sql
2ceaa 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2ceab 2c 20 4f 50 5f 56 4f 70 65 6e 2c 20 69 43 75 72  , OP_VOpen, iCur
2ceac 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  , 0, 0,.        
2cead 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ceae 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 61  (const char*)pTa
2ceaf 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f 56 54 41  b->pVtab, P4_VTA
2ceb0 42 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65  B);.    }else.#e
2ceb1 6e 64 69 66 0a 20 20 20 20 69 66 28 20 28 70 4c  ndif.    if( (pL
2ceb2 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2ceb3 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f  gs & WHERE_IDX_O
2ceb4 4e 4c 59 29 3d 3d 30 0a 20 20 20 20 20 20 20 20  NLY)==0.        
2ceb5 20 26 26 20 28 77 63 74 72 6c 46 6c 61 67 73 20   && (wctrlFlags 
2ceb6 26 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45  & WHERE_OMIT_OPE
2ceb7 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  N)==0 ){.      i
2ceb8 6e 74 20 6f 70 20 3d 20 70 57 49 6e 66 6f 2d 3e  nt op = pWInfo->
2ceb9 6f 6b 4f 6e 65 50 61 73 73 20 3f 20 4f 50 5f 4f  okOnePass ? OP_O
2ceba 70 65 6e 57 72 69 74 65 20 3a 20 4f 50 5f 4f 70  penWrite : OP_Op
2cebb 65 6e 52 65 61 64 3b 0a 20 20 20 20 20 20 73 71  enRead;.      sq
2cebc 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
2cebd 50 61 72 73 65 2c 20 70 54 61 62 49 74 65 6d 2d  Parse, pTabItem-
2cebe 3e 69 43 75 72 73 6f 72 2c 20 69 44 62 2c 20 70  >iCursor, iDb, p
2cebf 54 61 62 2c 20 6f 70 29 3b 0a 20 20 20 20 20 20  Tab, op);.      
2cec0 69 66 28 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f  if( !pWInfo->okO
2cec1 6e 65 50 61 73 73 20 26 26 20 70 54 61 62 2d 3e  nePass && pTab->
2cec2 6e 43 6f 6c 3c 42 4d 53 20 29 7b 0a 20 20 20 20  nCol<BMS ){.    
2cec3 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20 3d 20      Bitmask b = 
2cec4 70 54 61 62 49 74 65 6d 2d 3e 63 6f 6c 55 73 65  pTabItem->colUse
2cec5 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  d;.        int n
2cec6 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f   = 0;.        fo
2cec7 72 28 3b 20 62 3b 20 62 3d 62 3e 3e 31 2c 20 6e  r(; b; b=b>>1, n
2cec8 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 73 71  ++){}.        sq
2cec9 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2ceca 34 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65  4(v, sqlite3Vdbe
2cecb 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d 31  CurrentAddr(v)-1
2cecc 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  , SQLITE_INT_TO_
2cecd 50 54 52 28 6e 29 2c 20 50 34 5f 49 4e 54 33 32  PTR(n), P4_INT32
2cece 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
2cecf 74 28 20 6e 3c 3d 70 54 61 62 2d 3e 6e 43 6f 6c  t( n<=pTab->nCol
2ced0 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
2ced1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
2ced2 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50  ite3TableLock(pP
2ced3 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d  arse, iDb, pTab-
2ced4 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e  >tnum, 0, pTab->
2ced5 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20  zName);.    }.  
2ced6 20 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75    pLevel->iTabCu
2ced7 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  r = pTabItem->iC
2ced8 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 28  ursor;.    if( (
2ced9 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2ceda 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44  lags & WHERE_IND
2cedb 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20  EXED)!=0 ){.    
2cedc 20 20 49 6e 64 65 78 20 2a 70 49 78 20 3d 20 70    Index *pIx = p
2cedd 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49  Level->plan.u.pI
2cede 64 78 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66  dx;.      KeyInf
2cedf 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  o *pKey = sqlite
2cee0 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
2cee1 61 72 73 65 2c 20 70 49 78 29 3b 0a 20 20 20 20  arse, pIx);.    
2cee2 20 20 69 6e 74 20 69 49 64 78 43 75 72 20 3d 20    int iIdxCur = 
2cee3 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b  pLevel->iIdxCur;
2cee4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2cee5 49 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  Ix->pSchema==pTa
2cee6 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20  b->pSchema );.  
2cee7 20 20 20 20 61 73 73 65 72 74 28 20 69 49 64 78      assert( iIdx
2cee8 43 75 72 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20  Cur>=0 );.      
2cee9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ceea 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  4(v, OP_OpenRead
2ceeb 2c 20 69 49 64 78 43 75 72 2c 20 70 49 78 2d 3e  , iIdxCur, pIx->
2ceec 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20  tnum, iDb,.     
2ceed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ceee 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20     (char*)pKey, 
2ceef 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
2cef0 46 46 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  FF);.      VdbeC
2cef1 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
2cef2 20 70 49 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20   pIx->zName));. 
2cef3 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2cef4 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61  CodeVerifySchema
2cef5 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
2cef6 20 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e 69 54 6f   }.  pWInfo->iTo
2cef7 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  p = sqlite3VdbeC
2cef8 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 0a  urrentAddr(v);..
2cef9 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68    /* Generate th
2cefa 65 20 63 6f 64 65 20 74 6f 20 64 6f 20 74 68 65  e code to do the
2cefb 20 73 65 61 72 63 68 2e 20 20 45 61 63 68 20 69   search.  Each i
2cefc 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  teration of the 
2cefd 66 6f 72 0a 20 20 2a 2a 20 6c 6f 6f 70 20 62 65  for.  ** loop be
2cefe 6c 6f 77 20 67 65 6e 65 72 61 74 65 73 20 63 6f  low generates co
2ceff 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  de for a single 
2cf00 6e 65 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74  nested loop of t
2cf01 68 65 20 56 4d 0a 20 20 2a 2a 20 70 72 6f 67 72  he VM.  ** progr
2cf02 61 6d 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 74 52 65  am..  */.  notRe
2cf03 61 64 79 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29  ady = ~(Bitmask)
2cf04 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
2cf05 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
2cf06 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 74 52 65 61  i++){.    notRea
2cf07 64 79 20 3d 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70  dy = codeOneLoop
2cf08 53 74 61 72 74 28 70 57 49 6e 66 6f 2c 20 69 2c  Start(pWInfo, i,
2cf09 20 77 63 74 72 6c 46 6c 61 67 73 2c 20 6e 6f 74   wctrlFlags, not
2cf0a 52 65 61 64 79 29 3b 0a 20 20 20 20 70 57 49 6e  Ready);.    pWIn
2cf0b 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65 20 3d 20  fo->iContinue = 
2cf0c 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 2e 61 64 64  pWInfo->a[i].add
2cf0d 72 43 6f 6e 74 3b 0a 20 20 7d 0a 0a 23 69 66 64  rCont;.  }..#ifd
2cf0e 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 20 20  ef SQLITE_TEST  
2cf0f 2f 2a 20 46 6f 72 20 74 65 73 74 69 6e 67 20 61  /* For testing a
2cf10 6e 64 20 64 65 62 75 67 67 69 6e 67 20 75 73 65  nd debugging use
2cf11 20 6f 6e 6c 79 20 2a 2f 0a 20 20 2f 2a 20 52 65   only */.  /* Re
2cf12 63 6f 72 64 20 69 6e 20 74 68 65 20 71 75 65 72  cord in the quer
2cf13 79 20 70 6c 61 6e 20 69 6e 66 6f 72 6d 61 74 69  y plan informati
2cf14 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 75 72  on about the cur
2cf15 72 65 6e 74 20 74 61 62 6c 65 0a 20 20 2a 2a 20  rent table.  ** 
2cf16 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 75 73  and the index us
2cf17 65 64 20 74 6f 20 61 63 63 65 73 73 20 69 74 20  ed to access it 
2cf18 28 69 66 20 61 6e 79 29 2e 20 20 49 66 20 74 68  (if any).  If th
2cf19 65 20 74 61 62 6c 65 20 69 74 73 65 6c 66 0a 20  e table itself. 
2cf1a 20 2a 2a 20 69 73 20 6e 6f 74 20 75 73 65 64 2c   ** is not used,
2cf1b 20 69 74 73 20 6e 61 6d 65 20 69 73 20 6a 75 73   its name is jus
2cf1c 74 20 27 7b 7d 27 2e 20 20 49 66 20 6e 6f 20 69  t '{}'.  If no i
2cf1d 6e 64 65 78 20 69 73 20 75 73 65 64 0a 20 20 2a  ndex is used.  *
2cf1e 2a 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 6c  * the index is l
2cf1f 69 73 74 65 64 20 61 73 20 22 7b 7d 22 2e 20 20  isted as "{}".  
2cf20 49 66 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  If the primary k
2cf21 65 79 20 69 73 20 75 73 65 64 20 74 68 65 0a 20  ey is used the. 
2cf22 20 2a 2a 20 69 6e 64 65 78 20 6e 61 6d 65 20 69   ** index name i
2cf23 73 20 27 2a 27 2e 0a 20 20 2a 2f 0a 20 20 66 6f  s '*'..  */.  fo
2cf24 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73  r(i=0; i<pTabLis
2cf25 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  t->nSrc; i++){. 
2cf26 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20     char *z;.    
2cf27 69 6e 74 20 6e 3b 0a 20 20 20 20 70 4c 65 76 65  int n;.    pLeve
2cf28 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69  l = &pWInfo->a[i
2cf29 5d 3b 0a 20 20 20 20 70 54 61 62 49 74 65 6d 20  ];.    pTabItem 
2cf2a 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70  = &pTabList->a[p
2cf2b 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20  Level->iFrom];. 
2cf2c 20 20 20 7a 20 3d 20 70 54 61 62 49 74 65 6d 2d     z = pTabItem-
2cf2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 69 66 28  >zAlias;.    if(
2cf2e 20 7a 3d 3d 30 20 29 20 7a 20 3d 20 70 54 61 62   z==0 ) z = pTab
2cf2f 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  Item->pTab->zNam
2cf30 65 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  e;.    n = sqlit
2cf31 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20  e3Strlen30(z);. 
2cf32 20 20 20 69 66 28 20 6e 2b 6e 51 50 6c 61 6e 20     if( n+nQPlan 
2cf33 3c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  < sizeof(sqlite3
2cf34 5f 71 75 65 72 79 5f 70 6c 61 6e 29 2d 31 30 20  _query_plan)-10 
2cf35 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65  ){.      if( pLe
2cf36 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2cf37 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  s & WHERE_IDX_ON
2cf38 4c 59 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  LY ){.        me
2cf39 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75  mcpy(&sqlite3_qu
2cf3a 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
2cf3b 2c 20 22 7b 7d 22 2c 20 32 29 3b 0a 20 20 20 20  , "{}", 2);.    
2cf3c 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b      nQPlan += 2;
2cf3d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2cf3e 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71        memcpy(&sq
2cf3f 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
2cf40 5b 6e 51 50 6c 61 6e 5d 2c 20 7a 2c 20 6e 29 3b  [nQPlan], z, n);
2cf41 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  .        nQPlan 
2cf42 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  += n;.      }.  
2cf43 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75 65 72      sqlite3_quer
2cf44 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d  y_plan[nQPlan++]
2cf45 20 3d 20 27 20 27 3b 0a 20 20 20 20 7d 0a 20 20   = ' ';.    }.  
2cf46 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 76    testcase( pLev
2cf47 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2cf48 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45   & WHERE_ROWID_E
2cf49 51 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  Q );.    testcas
2cf4a 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  e( pLevel->plan.
2cf4b 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2cf4c 52 4f 57 49 44 5f 52 41 4e 47 45 20 29 3b 0a 20  ROWID_RANGE );. 
2cf4d 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70     if( pLevel->p
2cf4e 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 28 57  lan.wsFlags & (W
2cf4f 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48  HERE_ROWID_EQ|WH
2cf50 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29  ERE_ROWID_RANGE)
2cf51 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
2cf52 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  (&sqlite3_query_
2cf53 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 2a  plan[nQPlan], "*
2cf54 20 22 2c 20 32 29 3b 0a 20 20 20 20 20 20 6e 51   ", 2);.      nQ
2cf55 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20 20 20 7d  Plan += 2;.    }
2cf56 65 6c 73 65 20 69 66 28 20 28 70 4c 65 76 65 6c  else if( (pLevel
2cf57 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2cf58 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21   WHERE_INDEXED)!
2cf59 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20  =0 ){.      n = 
2cf5a 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2cf5b 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
2cf5c 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Idx->zName);.   
2cf5d 20 20 20 69 66 28 20 6e 2b 6e 51 50 6c 61 6e 20     if( n+nQPlan 
2cf5e 3c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  < sizeof(sqlite3
2cf5f 5f 71 75 65 72 79 5f 70 6c 61 6e 29 2d 32 20 29  _query_plan)-2 )
2cf60 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
2cf61 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  (&sqlite3_query_
2cf62 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 70 4c  plan[nQPlan], pL
2cf63 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64  evel->plan.u.pId
2cf64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20  x->zName, n);.  
2cf65 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20        nQPlan += 
2cf66 6e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  n;.        sqlit
2cf67 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
2cf68 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20  Plan++] = ' ';. 
2cf69 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2cf6a 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  {.      memcpy(&
2cf6b 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
2cf6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 7b 7d 20  an[nQPlan], "{} 
2cf6d 22 2c 20 33 29 3b 0a 20 20 20 20 20 20 6e 51 50  ", 3);.      nQP
2cf6e 6c 61 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 7d 0a  lan += 3;.    }.
2cf6f 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 6e 51 50    }.  while( nQP
2cf70 6c 61 6e 3e 30 20 26 26 20 73 71 6c 69 74 65 33  lan>0 && sqlite3
2cf71 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
2cf72 61 6e 2d 31 5d 3d 3d 27 20 27 20 29 7b 0a 20 20  an-1]==' ' ){.  
2cf73 20 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f    sqlite3_query_
2cf74 70 6c 61 6e 5b 2d 2d 6e 51 50 6c 61 6e 5d 20 3d  plan[--nQPlan] =
2cf75 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
2cf76 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50  3_query_plan[nQP
2cf77 6c 61 6e 5d 20 3d 20 30 3b 0a 20 20 6e 51 50 6c  lan] = 0;.  nQPl
2cf78 61 6e 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20 2f  an = 0;.#endif /
2cf79 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2f 2f  * SQLITE_TEST //
2cf7a 20 54 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62   Testing and deb
2cf7b 75 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20  ugging use only 
2cf7c 2a 2f 0a 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20  */..  /* Record 
2cf7d 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e  the continuation
2cf7e 20 61 64 64 72 65 73 73 20 69 6e 20 74 68 65 20   address in the 
2cf7f 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74  WhereInfo struct
2cf80 75 72 65 2e 20 20 54 68 65 6e 0a 20 20 2a 2a 20  ure.  Then.  ** 
2cf81 63 6c 65 61 6e 20 75 70 20 61 6e 64 20 72 65 74  clean up and ret
2cf82 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75  urn..  */.  retu
2cf83 72 6e 20 70 57 49 6e 66 6f 3b 0a 0a 20 20 2f 2a  rn pWInfo;..  /*
2cf84 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 6d 61   Jump here if ma
2cf85 6c 6c 6f 63 20 66 61 69 6c 73 20 2a 2f 0a 77 68  lloc fails */.wh
2cf86 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3a 0a 20  ereBeginError:. 
2cf87 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28 64   whereInfoFree(d
2cf88 62 2c 20 70 57 49 6e 66 6f 29 3b 0a 20 20 72 65  b, pWInfo);.  re
2cf89 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
2cf8a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 65 6e   Generate the en
2cf8b 64 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c  d of the WHERE l
2cf8c 6f 6f 70 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e  oop.  See commen
2cf8d 74 73 20 6f 6e 20 0a 2a 2a 20 73 71 6c 69 74 65  ts on .** sqlite
2cf8e 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 66 6f  3WhereBegin() fo
2cf8f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
2cf90 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  ormation..*/.SQL
2cf91 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2cf92 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
2cf93 28 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e  (WhereInfo *pWIn
2cf94 66 6f 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  fo){.  Parse *pP
2cf95 61 72 73 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 70  arse = pWInfo->p
2cf96 50 61 72 73 65 3b 0a 20 20 56 64 62 65 20 2a 76  Parse;.  Vdbe *v
2cf97 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2cf98 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 57 68 65  ;.  int i;.  Whe
2cf99 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b  reLevel *pLevel;
2cf9a 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
2cf9b 4c 69 73 74 20 3d 20 70 57 49 6e 66 6f 2d 3e 70  List = pWInfo->p
2cf9c 54 61 62 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74  TabList;.  sqlit
2cf9d 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2cf9e 3e 64 62 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  >db;..  /* Gener
2cf9f 61 74 65 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61  ate loop termina
2cfa0 74 69 6f 6e 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a  tion code..  */.
2cfa1 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2cfa2 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
2cfa3 0a 20 20 66 6f 72 28 69 3d 70 54 61 62 4c 69 73  .  for(i=pTabLis
2cfa4 74 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 30 3b  t->nSrc-1; i>=0;
2cfa5 20 69 2d 2d 29 7b 0a 20 20 20 20 70 4c 65 76 65   i--){.    pLeve
2cfa6 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69  l = &pWInfo->a[i
2cfa7 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
2cfa8 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2cfa9 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 43 6f  , pLevel->addrCo
2cfaa 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65  nt);.    if( pLe
2cfab 76 65 6c 2d 3e 6f 70 21 3d 4f 50 5f 4e 6f 6f 70  vel->op!=OP_Noop
2cfac 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2cfad 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 70  3VdbeAddOp2(v, p
2cfae 4c 65 76 65 6c 2d 3e 6f 70 2c 20 70 4c 65 76 65  Level->op, pLeve
2cfaf 6c 2d 3e 70 31 2c 20 70 4c 65 76 65 6c 2d 3e 70  l->p1, pLevel->p
2cfb0 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
2cfb1 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2cfb2 20 70 4c 65 76 65 6c 2d 3e 70 35 29 3b 0a 20 20   pLevel->p5);.  
2cfb3 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 76    }.    if( pLev
2cfb4 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2cfb5 20 26 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45   & WHERE_IN_ABLE
2cfb6 20 26 26 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e   && pLevel->u.in
2cfb7 2e 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20  .nIn>0 ){.      
2cfb8 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 2a 70  struct InLoop *p
2cfb9 49 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  In;.      int j;
2cfba 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2cfbb 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2cfbc 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78  , pLevel->addrNx
2cfbd 74 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  t);.      for(j=
2cfbe 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e  pLevel->u.in.nIn
2cfbf 2c 20 70 49 6e 3d 26 70 4c 65 76 65 6c 2d 3e 75  , pIn=&pLevel->u
2cfc0 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 5b 6a 2d 31 5d  .in.aInLoop[j-1]
2cfc1 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70 49 6e 2d  ; j>0; j--, pIn-
2cfc2 2d 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  -){.        sqli
2cfc3 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2cfc4 76 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f  v, pIn->addrInTo
2cfc5 70 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  p+1);.        sq
2cfc6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2cfc7 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 70 49 6e 2d  v, OP_Next, pIn-
2cfc8 3e 69 43 75 72 2c 20 70 49 6e 2d 3e 61 64 64 72  >iCur, pIn->addr
2cfc9 49 6e 54 6f 70 29 3b 0a 20 20 20 20 20 20 20 20  InTop);.        
2cfca 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2cfcb 65 72 65 28 76 2c 20 70 49 6e 2d 3e 61 64 64 72  ere(v, pIn->addr
2cfcc 49 6e 54 6f 70 2d 31 29 3b 0a 20 20 20 20 20 20  InTop-1);.      
2cfcd 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  }.      sqlite3D
2cfce 62 46 72 65 65 28 64 62 2c 20 70 4c 65 76 65 6c  bFree(db, pLevel
2cfcf 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 29 3b  ->u.in.aInLoop);
2cfd0 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2cfd1 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2cfd2 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64  el(v, pLevel->ad
2cfd3 64 72 42 72 6b 29 3b 0a 20 20 20 20 69 66 28 20  drBrk);.    if( 
2cfd4 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
2cfd5 6e 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61  n ){.      int a
2cfd6 64 64 72 3b 0a 20 20 20 20 20 20 61 64 64 72 20  ddr;.      addr 
2cfd7 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2cfd8 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c  Op1(v, OP_IfPos,
2cfd9 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
2cfda 69 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  in);.      sqlit
2cfdb 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2cfdc 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 54 61 62  OP_NullRow, pTab
2cfdd 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73  List->a[i].iCurs
2cfde 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  or);.      if( p
2cfdf 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3e 3d  Level->iIdxCur>=
2cfe0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
2cfe1 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2cfe2 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 4c  , OP_NullRow, pL
2cfe3 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 29 3b 0a  evel->iIdxCur);.
2cfe4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2cfe5 28 20 70 4c 65 76 65 6c 2d 3e 6f 70 3d 3d 4f 50  ( pLevel->op==OP
2cfe6 5f 52 65 74 75 72 6e 20 29 7b 0a 20 20 20 20 20  _Return ){.     
2cfe7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2cfe8 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
2cfe9 2c 20 70 4c 65 76 65 6c 2d 3e 70 31 2c 20 70 4c  , pLevel->p1, pL
2cfea 65 76 65 6c 2d 3e 61 64 64 72 46 69 72 73 74 29  evel->addrFirst)
2cfeb 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2cfec 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2cfed 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2cfee 6f 74 6f 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e  oto, 0, pLevel->
2cfef 61 64 64 72 46 69 72 73 74 29 3b 0a 20 20 20 20  addrFirst);.    
2cff0 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2cff1 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2cff2 20 61 64 64 72 29 3b 0a 20 20 20 20 7d 0a 20 20   addr);.    }.  
2cff3 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 22 62 72 65  }..  /* The "bre
2cff4 61 6b 22 20 70 6f 69 6e 74 20 69 73 20 68 65 72  ak" point is her
2cff5 65 2c 20 6a 75 73 74 20 70 61 73 74 20 74 68 65  e, just past the
2cff6 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 75 74 65   end of the oute
2cff7 72 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 53 65 74  r loop..  ** Set
2cff8 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69   it..  */.  sqli
2cff9 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2cffa 62 65 6c 28 76 2c 20 70 57 49 6e 66 6f 2d 3e 69  bel(v, pWInfo->i
2cffb 42 72 65 61 6b 29 3b 0a 0a 20 20 2f 2a 20 43 6c  Break);..  /* Cl
2cffc 6f 73 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 63  ose all of the c
2cffd 75 72 73 6f 72 73 20 74 68 61 74 20 77 65 72 65  ursors that were
2cffe 20 6f 70 65 6e 65 64 20 62 79 20 73 71 6c 69 74   opened by sqlit
2cfff 65 33 57 68 65 72 65 42 65 67 69 6e 2e 0a 20 20  e3WhereBegin..  
2d000 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c  */.  for(i=0, pL
2d001 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20  evel=pWInfo->a; 
2d002 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2d003 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29  ; i++, pLevel++)
2d004 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63  {.    struct Src
2d005 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49  List_item *pTabI
2d006 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
2d007 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
2d008 5d 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ];.    Table *pT
2d009 61 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70  ab = pTabItem->p
2d00a 54 61 62 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Tab;.    assert(
2d00b 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20   pTab!=0 );.    
2d00c 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c  if( (pTab->tabFl
2d00d 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d 65 72  ags & TF_Ephemer
2d00e 61 6c 29 21 3d 30 20 7c 7c 20 70 54 61 62 2d 3e  al)!=0 || pTab->
2d00f 70 53 65 6c 65 63 74 20 29 20 63 6f 6e 74 69 6e  pSelect ) contin
2d010 75 65 3b 0a 20 20 20 20 69 66 28 20 28 70 57 49  ue;.    if( (pWI
2d011 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20  nfo->wctrlFlags 
2d012 26 20 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f  & WHERE_OMIT_CLO
2d013 53 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  SE)==0 ){.      
2d014 69 66 28 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f  if( !pWInfo->okO
2d015 6e 65 50 61 73 73 20 26 26 20 28 70 4c 65 76 65  nePass && (pLeve
2d016 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2d017 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
2d018 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
2d019 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d01a 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70  1(v, OP_Close, p
2d01b 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  TabItem->iCursor
2d01c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2d01d 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c   if( (pLevel->pl
2d01e 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2d01f 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29  RE_INDEXED)!=0 )
2d020 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2d021 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2d022 50 5f 43 6c 6f 73 65 2c 20 70 4c 65 76 65 6c 2d  P_Close, pLevel-
2d023 3e 69 49 64 78 43 75 72 29 3b 0a 20 20 20 20 20  >iIdxCur);.     
2d024 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
2d025 20 49 66 20 74 68 69 73 20 73 63 61 6e 20 75 73   If this scan us
2d026 65 73 20 61 6e 20 69 6e 64 65 78 2c 20 6d 61 6b  es an index, mak
2d027 65 20 63 6f 64 65 20 73 75 62 73 74 69 74 75 74  e code substitut
2d028 69 6f 6e 73 20 74 6f 20 72 65 61 64 20 64 61 74  ions to read dat
2d029 61 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68  a.    ** from th
2d02a 65 20 69 6e 64 65 78 20 69 6e 20 70 72 65 66 65  e index in prefe
2d02b 72 65 6e 63 65 20 74 6f 20 74 68 65 20 74 61 62  rence to the tab
2d02c 6c 65 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 20 74  le. Sometimes, t
2d02d 68 69 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a  his means.    **
2d02e 20 74 68 65 20 74 61 62 6c 65 20 6e 65 65 64 20   the table need 
2d02f 6e 65 76 65 72 20 62 65 20 72 65 61 64 20 66 72  never be read fr
2d030 6f 6d 2e 20 54 68 69 73 20 69 73 20 61 20 70 65  om. This is a pe
2d031 72 66 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 74 2c  rformance boost,
2d032 0a 20 20 20 20 2a 2a 20 61 73 20 74 68 65 20 76  .    ** as the v
2d033 64 62 65 20 6c 65 76 65 6c 20 77 61 69 74 73 20  dbe level waits 
2d034 75 6e 74 69 6c 20 74 68 65 20 74 61 62 6c 65 20  until the table 
2d035 69 73 20 72 65 61 64 20 62 65 66 6f 72 65 20 61  is read before a
2d036 63 74 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 73  ctually.    ** s
2d037 65 65 6b 69 6e 67 20 74 68 65 20 74 61 62 6c 65  eeking the table
2d038 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 72   cursor to the r
2d039 65 63 6f 72 64 20 63 6f 72 72 65 73 70 6f 6e 64  ecord correspond
2d03a 69 6e 67 20 74 6f 20 74 68 65 20 63 75 72 72 65  ing to the curre
2d03b 6e 74 0a 20 20 20 20 2a 2a 20 70 6f 73 69 74 69  nt.    ** positi
2d03c 6f 6e 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e  on in the index.
2d03d 0a 20 20 20 20 2a 2a 20 0a 20 20 20 20 2a 2a 20  .    ** .    ** 
2d03e 43 61 6c 6c 73 20 74 6f 20 74 68 65 20 63 6f 64  Calls to the cod
2d03f 65 20 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 62  e generator in b
2d040 65 74 77 65 65 6e 20 73 71 6c 69 74 65 33 57 68  etween sqlite3Wh
2d041 65 72 65 42 65 67 69 6e 20 61 6e 64 0a 20 20 20  ereBegin and.   
2d042 20 2a 2a 20 73 71 6c 69 74 65 33 57 68 65 72 65   ** sqlite3Where
2d043 45 6e 64 20 77 69 6c 6c 20 68 61 76 65 20 63 72  End will have cr
2d044 65 61 74 65 64 20 63 6f 64 65 20 74 68 61 74 20  eated code that 
2d045 72 65 66 65 72 65 6e 63 65 73 20 74 68 65 20 74  references the t
2d046 61 62 6c 65 0a 20 20 20 20 2a 2a 20 64 69 72 65  able.    ** dire
2d047 63 74 6c 79 2e 20 20 54 68 69 73 20 6c 6f 6f 70  ctly.  This loop
2d048 20 73 63 61 6e 73 20 61 6c 6c 20 74 68 61 74 20   scans all that 
2d049 63 6f 64 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  code looking for
2d04a 20 6f 70 63 6f 64 65 73 0a 20 20 20 20 2a 2a 20   opcodes.    ** 
2d04b 74 68 61 74 20 72 65 66 65 72 65 6e 63 65 20 74  that reference t
2d04c 68 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6e  he table and con
2d04d 76 65 72 74 73 20 74 68 65 6d 20 69 6e 74 6f 20  verts them into 
2d04e 6f 70 63 6f 64 65 73 20 74 68 61 74 0a 20 20 20  opcodes that.   
2d04f 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 74 68   ** reference th
2d050 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a  e index..    */.
2d051 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d      if( (pLevel-
2d052 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2d053 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d  WHERE_INDEXED)!=
2d054 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b  0 ){.      int k
2d055 2c 20 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20 20  , j, last;.     
2d056 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20   VdbeOp *pOp;.  
2d057 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
2d058 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  = pLevel->plan.u
2d059 2e 70 49 64 78 3b 0a 20 20 20 20 20 20 69 6e 74  .pIdx;.      int
2d05a 20 75 73 65 49 6e 64 65 78 4f 6e 6c 79 20 3d 20   useIndexOnly = 
2d05b 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2d05c 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
2d05d 5f 4f 4e 4c 59 3b 0a 0a 20 20 20 20 20 20 61 73  _ONLY;..      as
2d05e 73 65 72 74 28 20 70 49 64 78 21 3d 30 20 29 3b  sert( pIdx!=0 );
2d05f 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c  .      pOp = sql
2d060 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c  ite3VdbeGetOp(v,
2d061 20 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a   pWInfo->iTop);.
2d062 20 20 20 20 20 20 6c 61 73 74 20 3d 20 73 71 6c        last = sql
2d063 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
2d064 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66 6f  ddr(v);.      fo
2d065 72 28 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f 70  r(k=pWInfo->iTop
2d066 3b 20 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70  ; k<last; k++, p
2d067 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  Op++){.        i
2d068 66 28 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65 76  f( pOp->p1!=pLev
2d069 65 6c 2d 3e 69 54 61 62 43 75 72 20 29 20 63 6f  el->iTabCur ) co
2d06a 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
2d06b 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
2d06c 3d 4f 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  =OP_Column ){.  
2d06d 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
2d06e 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
2d06f 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
2d070 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d      if( pOp->p2=
2d071 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  =pIdx->aiColumn[
2d072 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j] ){.          
2d073 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b      pOp->p2 = j;
2d074 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2d075 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d  Op->p1 = pLevel-
2d076 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20  >iIdxCur;.      
2d077 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2d078 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2d079 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2d07a 20 20 20 61 73 73 65 72 74 28 21 75 73 65 49 6e     assert(!useIn
2d07b 64 65 78 4f 6e 6c 79 20 7c 7c 20 6a 3c 70 49 64  dexOnly || j<pId
2d07c 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  x->nColumn);.   
2d07d 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
2d07e 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52  Op->opcode==OP_R
2d07f 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20  owid ){.        
2d080 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76    pOp->p1 = pLev
2d081 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20  el->iIdxCur;.   
2d082 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f         pOp->opco
2d083 64 65 20 3d 20 4f 50 5f 49 64 78 52 6f 77 69 64  de = OP_IdxRowid
2d084 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
2d085 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
2d086 3d 4f 50 5f 4e 75 6c 6c 52 6f 77 20 26 26 20 75  =OP_NullRow && u
2d087 73 65 49 6e 64 65 78 4f 6e 6c 79 20 29 7b 0a 20  seIndexOnly ){. 
2d088 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70           pOp->op
2d089 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a  code = OP_Noop;.
2d08a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2d08b 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
2d08c 2a 20 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70 0a  * Final cleanup.
2d08d 20 20 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66 6f    */.  whereInfo
2d08e 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29  Free(db, pWInfo)
2d08f 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;.  return;.}../
2d090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
2d091 6e 64 20 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a  nd of where.c **
2d092 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d093 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2d095 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
2d096 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e  egin file parse.
2d097 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2d098 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d099 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2d09a 2a 20 44 72 69 76 65 72 20 74 65 6d 70 6c 61 74  * Driver templat
2d09b 65 20 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20  e for the LEMON 
2d09c 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72  parser generator
2d09d 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  ..** The author 
2d09e 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2d09f 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2d0a0 63 65 20 63 6f 64 65 2e 0a 2a 2f 0a 2f 2a 20 46  ce code..*/./* F
2d0a1 69 72 73 74 20 6f 66 66 2c 20 63 6f 64 65 20 69  irst off, code i
2d0a2 73 20 69 6e 63 6c 75 64 65 64 20 74 68 61 74 20  s included that 
2d0a3 66 6f 6c 6c 6f 77 73 20 74 68 65 20 22 69 6e 63  follows the "inc
2d0a4 6c 75 64 65 22 20 64 65 63 6c 61 72 61 74 69 6f  lude" declaratio
2d0a5 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 69 6e 70 75  n.** in the inpu
2d0a6 74 20 67 72 61 6d 6d 61 72 20 66 69 6c 65 2e 20  t grammar file. 
2d0a7 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  */.../*.** An in
2d0a8 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
2d0a9 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69  tructure holds i
2d0aa 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
2d0ab 20 74 68 65 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c   the.** LIMIT cl
2d0ac 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
2d0ad 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73   statement..*/.s
2d0ae 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 7b  truct LimitVal {
2d0af 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b  .  Expr *pLimit;
2d0b0 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54      /* The LIMIT
2d0b1 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55   expression.  NU
2d0b2 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  LL if there is n
2d0b3 6f 20 6c 69 6d 69 74 20 2a 2f 0a 20 20 45 78 70  o limit */.  Exp
2d0b4 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 2f 2a  r *pOffset;   /*
2d0b5 20 54 68 65 20 4f 46 46 53 45 54 20 65 78 70 72   The OFFSET expr
2d0b6 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66  ession.  NULL if
2d0b7 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 20 2a   there is none *
2d0b8 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
2d0b9 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
2d0ba 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
2d0bb 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 4c  d to store the L
2d0bc 49 4b 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20 4e 4f  IKE,.** GLOB, NO
2d0bd 54 20 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f 54 20  T LIKE, and NOT 
2d0be 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e 0a  GLOB operators..
2d0bf 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6b 65 4f 70  */.struct LikeOp
2d0c0 20 7b 0a 20 20 54 6f 6b 65 6e 20 65 4f 70 65 72   {.  Token eOper
2d0c1 61 74 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b 65 22  ator;  /* "like"
2d0c2 20 6f 72 20 22 67 6c 6f 62 22 20 6f 72 20 22 72   or "glob" or "r
2d0c3 65 67 65 78 70 22 20 2a 2f 0a 20 20 69 6e 74 20  egexp" */.  int 
2d0c4 6e 6f 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  not;         /* 
2d0c5 54 72 75 65 20 69 66 20 74 68 65 20 4e 4f 54 20  True if the NOT 
2d0c6 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
2d0c7 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
2d0c8 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
2d0c9 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
2d0ca 75 63 74 75 72 65 20 64 65 73 63 72 69 62 65 73  ucture describes
2d0cb 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 0a   the event of a.
2d0cc 2a 2a 20 54 52 49 47 47 45 52 2e 20 20 22 61 22  ** TRIGGER.  "a"
2d0cd 20 69 73 20 74 68 65 20 65 76 65 6e 74 20 74 79   is the event ty
2d0ce 70 65 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 50  pe, one of TK_UP
2d0cf 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  DATE, TK_INSERT,
2d0d0 0a 2a 2a 20 54 4b 5f 44 45 4c 45 54 45 2c 20 6f  .** TK_DELETE, o
2d0d1 72 20 54 4b 5f 49 4e 53 54 45 41 44 2e 20 20 49  r TK_INSTEAD.  I
2d0d2 66 20 74 68 65 20 65 76 65 6e 74 20 69 73 20 6f  f the event is o
2d0d3 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
2d0d4 20 20 20 20 20 20 55 50 44 41 54 45 20 4f 4e 20        UPDATE ON 
2d0d5 28 61 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20 54 68  (a,b,c).**.** Th
2d0d6 65 6e 20 74 68 65 20 22 62 22 20 49 64 4c 69 73  en the "b" IdLis
2d0d7 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 69  t records the li
2d0d8 73 74 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f 0a 73  st "a,b,c"..*/.s
2d0d9 74 72 75 63 74 20 54 72 69 67 45 76 65 6e 74 20  truct TrigEvent 
2d0da 7b 20 69 6e 74 20 61 3b 20 49 64 4c 69 73 74 20  { int a; IdList 
2d0db 2a 20 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  * b; };../*.** A
2d0dc 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
2d0dd 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  is structure hol
2d0de 64 73 20 74 68 65 20 41 54 54 41 43 48 20 6b 65  ds the ATTACH ke
2d0df 79 20 61 6e 64 20 74 68 65 20 6b 65 79 20 74 79  y and the key ty
2d0e0 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 74  pe..*/.struct At
2d0e1 74 61 63 68 4b 65 79 20 7b 20 69 6e 74 20 74 79  tachKey { int ty
2d0e2 70 65 3b 20 20 54 6f 6b 65 6e 20 6b 65 79 3b 20  pe;  Token key; 
2d0e3 7d 3b 0a 0a 2f 2a 20 4e 65 78 74 20 69 73 20 61  };../* Next is a
2d0e4 6c 6c 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 2c  ll token values,
2d0e5 20 69 6e 20 61 20 66 6f 72 6d 20 73 75 69 74 61   in a form suita
2d0e6 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 20 6d  ble for use by m
2d0e7 61 6b 65 68 65 61 64 65 72 73 2e 0a 2a 2a 20 54  akeheaders..** T
2d0e8 68 69 73 20 73 65 63 74 69 6f 6e 20 77 69 6c 6c  his section will
2d0e9 20 62 65 20 6e 75 6c 6c 20 75 6e 6c 65 73 73 20   be null unless 
2d0ea 6c 65 6d 6f 6e 20 69 73 20 72 75 6e 20 77 69 74  lemon is run wit
2d0eb 68 20 74 68 65 20 2d 6d 20 73 77 69 74 63 68 2e  h the -m switch.
2d0ec 0a 2a 2f 0a 2f 2a 20 0a 2a 2a 20 54 68 65 73 65  .*/./* .** These
2d0ed 20 63 6f 6e 73 74 61 6e 74 73 20 28 61 6c 6c 20   constants (all 
2d0ee 67 65 6e 65 72 61 74 65 64 20 61 75 74 6f 6d 61  generated automa
2d0ef 74 69 63 61 6c 6c 79 20 62 79 20 74 68 65 20 70  tically by the p
2d0f0 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 29  arser generator)
2d0f1 0a 2a 2a 20 73 70 65 63 69 66 79 20 74 68 65 20  .** specify the 
2d0f2 76 61 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66  various kinds of
2d0f3 20 74 6f 6b 65 6e 73 20 28 74 65 72 6d 69 6e 61   tokens (termina
2d0f4 6c 73 29 20 74 68 61 74 20 74 68 65 20 70 61 72  ls) that the par
2d0f5 73 65 72 0a 2a 2a 20 75 6e 64 65 72 73 74 61 6e  ser.** understan
2d0f6 64 73 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  ds. .**.** Each 
2d0f7 73 79 6d 62 6f 6c 20 68 65 72 65 20 69 73 20 61  symbol here is a
2d0f8 20 74 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c   terminal symbol
2d0f9 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e   in the grammar.
2d0fa 0a 2a 2f 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65  .*/./* Make sure
2d0fb 20 74 68 65 20 49 4e 54 45 52 46 41 43 45 20 6d   the INTERFACE m
2d0fc 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e  acro is defined.
2d0fd 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 54 45  .*/.#ifndef INTE
2d0fe 52 46 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49  RFACE.# define I
2d0ff 4e 54 45 52 46 41 43 45 20 31 0a 23 65 6e 64 69  NTERFACE 1.#endi
2d100 66 0a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 68  f./* The next th
2d101 69 6e 67 20 69 6e 63 6c 75 64 65 64 20 69 73 20  ing included is 
2d102 73 65 72 69 65 73 20 6f 66 20 64 65 66 69 6e 65  series of define
2d103 73 20 77 68 69 63 68 20 63 6f 6e 74 72 6f 6c 0a  s which control.
2d104 2a 2a 20 76 61 72 69 6f 75 73 20 61 73 70 65 63  ** various aspec
2d105 74 73 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61  ts of the genera
2d106 74 65 64 20 70 61 72 73 65 72 2e 0a 2a 2a 20 20  ted parser..**  
2d107 20 20 59 59 43 4f 44 45 54 59 50 45 20 20 20 20    YYCODETYPE    
2d108 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61       is the data
2d109 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 73   type used for s
2d10a 74 6f 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a  toring terminal.
2d10b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2d10c 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e           and non
2d10d 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73  terminal numbers
2d10e 2e 20 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61  .  "unsigned cha
2d10f 72 22 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  r" is.**        
2d110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
2d111 73 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65  sed if there are
2d112 20 66 65 77 65 72 20 74 68 61 6e 20 32 35 30 20   fewer than 250 
2d113 74 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20 20 20 20  terminals.**    
2d114 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d115 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e     and nontermin
2d116 61 6c 73 2e 20 20 22 69 6e 74 22 20 69 73 20 75  als.  "int" is u
2d117 73 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a  sed otherwise..*
2d118 2a 20 20 20 20 59 59 4e 4f 43 4f 44 45 20 20 20  *    YYNOCODE   
2d119 20 20 20 20 20 20 20 20 69 73 20 61 20 6e 75 6d          is a num
2d11a 62 65 72 20 6f 66 20 74 79 70 65 20 59 59 43 4f  ber of type YYCO
2d11b 44 45 54 59 50 45 20 77 68 69 63 68 20 63 6f 72  DETYPE which cor
2d11c 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 20 20 20 20  responds.**     
2d11d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d11e 20 20 74 6f 20 6e 6f 20 6c 65 67 61 6c 20 74 65    to no legal te
2d11f 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65 72  rminal or nonter
2d120 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 2e 20 20 54  minal number.  T
2d121 68 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  his.**          
2d122 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 75 6d               num
2d123 62 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 66  ber is used to f
2d124 69 6c 6c 20 69 6e 20 65 6d 70 74 79 20 73 6c 6f  ill in empty slo
2d125 74 73 20 6f 66 20 74 68 65 20 68 61 73 68 20 0a  ts of the hash .
2d126 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2d127 20 20 20 20 20 20 20 20 20 74 61 62 6c 65 2e 0a           table..
2d128 2a 2a 20 20 20 20 59 59 46 41 4c 4c 42 41 43 4b  **    YYFALLBACK
2d129 20 20 20 20 20 20 20 20 20 49 66 20 64 65 66 69           If defi
2d12a 6e 65 64 2c 20 74 68 69 73 20 69 6e 64 69 63 61  ned, this indica
2d12b 74 65 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20  tes that one or 
2d12c 6d 6f 72 65 20 74 6f 6b 65 6e 73 0a 2a 2a 20 20  more tokens.**  
2d12d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d12e 20 20 20 20 20 68 61 76 65 20 66 61 6c 6c 2d 62       have fall-b
2d12f 61 63 6b 20 76 61 6c 75 65 73 20 77 68 69 63 68  ack values which
2d130 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
2d131 69 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  if the.**       
2d132 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d133 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f  original value o
2d134 66 20 74 68 65 20 74 6f 6b 65 6e 20 77 69 6c 6c  f the token will
2d135 20 6e 6f 74 20 70 61 72 73 65 2e 0a 2a 2a 20 20   not parse..**  
2d136 20 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 20    YYACTIONTYPE  
2d137 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61       is the data
2d138 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 73   type used for s
2d139 74 6f 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a  toring terminal.
2d13a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2d13b 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e           and non
2d13c 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73  terminal numbers
2d13d 2e 20 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61  .  "unsigned cha
2d13e 72 22 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  r" is.**        
2d13f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
2d140 73 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65  sed if there are
2d141 20 66 65 77 65 72 20 74 68 61 6e 20 32 35 30 20   fewer than 250 
2d142 72 75 6c 65 73 20 61 6e 64 0a 2a 2a 20 20 20 20  rules and.**    
2d143 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d144 20 20 20 73 74 61 74 65 73 20 63 6f 6d 62 69 6e     states combin
2d145 65 64 2e 20 20 22 69 6e 74 22 20 69 73 20 75 73  ed.  "int" is us
2d146 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a  ed otherwise..**
2d147 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
2d148 72 54 4f 4b 45 4e 54 59 50 45 20 20 20 20 20 69  rTOKENTYPE     i
2d149 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65 20  s the data type 
2d14a 75 73 65 64 20 66 6f 72 20 6d 69 6e 6f 72 20 74  used for minor t
2d14b 6f 6b 65 6e 73 20 67 69 76 65 6e 20 0a 2a 2a 20  okens given .** 
2d14c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d14d 20 20 20 20 20 20 64 69 72 65 63 74 6c 79 20 74        directly t
2d14e 6f 20 74 68 65 20 70 61 72 73 65 72 20 66 72 6f  o the parser fro
2d14f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e  m the tokenizer.
2d150 0a 2a 2a 20 20 20 20 59 59 4d 49 4e 4f 52 54 59  .**    YYMINORTY
2d151 50 45 20 20 20 20 20 20 20 20 69 73 20 74 68 65  PE        is the
2d152 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20   data type used 
2d153 66 6f 72 20 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f  for all minor to
2d154 6b 65 6e 73 2e 0a 2a 2a 20 20 20 20 20 20 20 20  kens..**        
2d155 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
2d156 68 69 73 20 69 73 20 74 79 70 69 63 61 6c 6c 79  his is typically
2d157 20 61 20 75 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79   a union of many
2d158 20 74 79 70 65 73 2c 20 6f 6e 65 20 6f 66 0a 2a   types, one of.*
2d159 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2d15a 20 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73          which is
2d15b 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f   sqlite3ParserTO
2d15c 4b 45 4e 54 59 50 45 2e 20 20 54 68 65 20 65 6e  KENTYPE.  The en
2d15d 74 72 79 20 69 6e 20 74 68 65 20 75 6e 69 6f 6e  try in the union
2d15e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2d15f 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 62 61            for ba
2d160 73 65 20 74 6f 6b 65 6e 73 20 69 73 20 63 61 6c  se tokens is cal
2d161 6c 65 64 20 22 79 79 30 22 2e 0a 2a 2a 20 20 20  led "yy0"..**   
2d162 20 59 59 53 54 41 43 4b 44 45 50 54 48 20 20 20   YYSTACKDEPTH   
2d163 20 20 20 20 69 73 20 74 68 65 20 6d 61 78 69 6d      is the maxim
2d164 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  um depth of the 
2d165 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 2e 20  parser's stack. 
2d166 20 49 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   If.**          
2d167 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 65 72               zer
2d168 6f 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 64  o the stack is d
2d169 79 6e 61 6d 69 63 61 6c 6c 79 20 73 69 7a 65 64  ynamically sized
2d16a 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29   using realloc()
2d16b 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61  .**    sqlite3Pa
2d16c 72 73 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20  rserARG_SDECL   
2d16d 20 20 41 20 73 74 61 74 69 63 20 76 61 72 69 61    A static varia
2d16e 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  ble declaration 
2d16f 66 6f 72 20 74 68 65 20 25 65 78 74 72 61 5f 61  for the %extra_a
2d170 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71  rgument.**    sq
2d171 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 50  lite3ParserARG_P
2d172 44 45 43 4c 20 20 20 20 20 41 20 70 61 72 61 6d  DECL     A param
2d173 65 74 65 72 20 64 65 63 6c 61 72 61 74 69 6f 6e  eter declaration
2d174 20 66 6f 72 20 74 68 65 20 25 65 78 74 72 61 5f   for the %extra_
2d175 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73  argument.**    s
2d176 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
2d177 53 54 4f 52 45 20 20 20 20 20 43 6f 64 65 20 74  STORE     Code t
2d178 6f 20 73 74 6f 72 65 20 25 65 78 74 72 61 5f 61  o store %extra_a
2d179 72 67 75 6d 65 6e 74 20 69 6e 74 6f 20 79 79 70  rgument into yyp
2d17a 50 61 72 73 65 72 0a 2a 2a 20 20 20 20 73 71 6c  Parser.**    sql
2d17b 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45  ite3ParserARG_FE
2d17c 54 43 48 20 20 20 20 20 43 6f 64 65 20 74 6f 20  TCH     Code to 
2d17d 65 78 74 72 61 63 74 20 25 65 78 74 72 61 5f 61  extract %extra_a
2d17e 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 79 79 70  rgument from yyp
2d17f 50 61 72 73 65 72 0a 2a 2a 20 20 20 20 59 59 4e  Parser.**    YYN
2d180 53 54 41 54 45 20 20 20 20 20 20 20 20 20 20 20  STATE           
2d181 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 6e 75 6d  the combined num
2d182 62 65 72 20 6f 66 20 73 74 61 74 65 73 2e 0a 2a  ber of states..*
2d183 2a 20 20 20 20 59 59 4e 52 55 4c 45 20 20 20 20  *    YYNRULE    
2d184 20 20 20 20 20 20 20 20 74 68 65 20 6e 75 6d 62          the numb
2d185 65 72 20 6f 66 20 72 75 6c 65 73 20 69 6e 20 74  er of rules in t
2d186 68 65 20 67 72 61 6d 6d 61 72 0a 2a 2a 20 20 20  he grammar.**   
2d187 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 20   YYERRORSYMBOL  
2d188 20 20 20 20 69 73 20 74 68 65 20 63 6f 64 65 20      is the code 
2d189 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 65 72  number of the er
2d18a 72 6f 72 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20  ror symbol.  If 
2d18b 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  not.**          
2d18c 20 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66               def
2d18d 69 6e 65 64 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  ined, then do no
2d18e 20 65 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e   error processin
2d18f 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59  g..*/.#define YY
2d190 43 4f 44 45 54 59 50 45 20 75 6e 73 69 67 6e 65  CODETYPE unsigne
2d191 64 20 73 68 6f 72 74 20 69 6e 74 0a 23 64 65 66  d short int.#def
2d192 69 6e 65 20 59 59 4e 4f 43 4f 44 45 20 32 35 32  ine YYNOCODE 252
2d193 0a 23 64 65 66 69 6e 65 20 59 59 41 43 54 49 4f  .#define YYACTIO
2d194 4e 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73  NTYPE unsigned s
2d195 68 6f 72 74 20 69 6e 74 0a 23 64 65 66 69 6e 65  hort int.#define
2d196 20 59 59 57 49 4c 44 43 41 52 44 20 36 35 0a 23   YYWILDCARD 65.#
2d197 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61  define sqlite3Pa
2d198 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 54 6f  rserTOKENTYPE To
2d199 6b 65 6e 0a 74 79 70 65 64 65 66 20 75 6e 69 6f  ken.typedef unio
2d19a 6e 20 7b 0a 20 20 69 6e 74 20 79 79 69 6e 69 74  n {.  int yyinit
2d19b 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  ;.  sqlite3Parse
2d19c 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 30 3b 0a  rTOKENTYPE yy0;.
2d19d 20 20 45 78 70 72 2a 20 79 79 37 32 3b 0a 20 20    Expr* yy72;.  
2d19e 54 72 69 67 67 65 72 53 74 65 70 2a 20 79 79 31  TriggerStep* yy1
2d19f 34 35 3b 0a 20 20 45 78 70 72 4c 69 73 74 2a 20  45;.  ExprList* 
2d1a0 79 79 31 34 38 3b 0a 20 20 53 72 63 4c 69 73 74  yy148;.  SrcList
2d1a1 2a 20 79 79 31 38 35 3b 0a 20 20 69 6e 74 20 79  * yy185;.  int y
2d1a2 79 31 39 34 3b 0a 20 20 53 65 6c 65 63 74 2a 20  y194;.  Select* 
2d1a3 79 79 32 34 33 3b 0a 20 20 49 64 4c 69 73 74 2a  yy243;.  IdList*
2d1a4 20 79 79 32 35 34 3b 0a 20 20 73 74 72 75 63 74   yy254;.  struct
2d1a5 20 54 72 69 67 45 76 65 6e 74 20 79 79 33 33 32   TrigEvent yy332
2d1a6 3b 0a 20 20 73 74 72 75 63 74 20 4c 69 6d 69 74  ;.  struct Limit
2d1a7 56 61 6c 20 79 79 33 35 34 3b 0a 20 20 73 74 72  Val yy354;.  str
2d1a8 75 63 74 20 4c 69 6b 65 4f 70 20 79 79 33 39 32  uct LikeOp yy392
2d1a9 3b 0a 20 20 73 74 72 75 63 74 20 7b 69 6e 74 20  ;.  struct {int 
2d1aa 76 61 6c 75 65 3b 20 69 6e 74 20 6d 61 73 6b 3b  value; int mask;
2d1ab 7d 20 79 79 34 39 37 3b 0a 7d 20 59 59 4d 49 4e  } yy497;.} YYMIN
2d1ac 4f 52 54 59 50 45 3b 0a 23 69 66 6e 64 65 66 20  ORTYPE;.#ifndef 
2d1ad 59 59 53 54 41 43 4b 44 45 50 54 48 0a 23 64 65  YYSTACKDEPTH.#de
2d1ae 66 69 6e 65 20 59 59 53 54 41 43 4b 44 45 50 54  fine YYSTACKDEPT
2d1af 48 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 64 65  H 100.#endif.#de
2d1b0 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73  fine sqlite3Pars
2d1b1 65 72 41 52 47 5f 53 44 45 43 4c 20 50 61 72 73  erARG_SDECL Pars
2d1b2 65 20 2a 70 50 61 72 73 65 3b 0a 23 64 65 66 69  e *pParse;.#defi
2d1b3 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  ne sqlite3Parser
2d1b4 41 52 47 5f 50 44 45 43 4c 20 2c 50 61 72 73 65  ARG_PDECL ,Parse
2d1b5 20 2a 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65   *pParse.#define
2d1b6 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2d1b7 47 5f 46 45 54 43 48 20 50 61 72 73 65 20 2a 70  G_FETCH Parse *p
2d1b8 50 61 72 73 65 20 3d 20 79 79 70 50 61 72 73 65  Parse = yypParse
2d1b9 72 2d 3e 70 50 61 72 73 65 0a 23 64 65 66 69 6e  r->pParse.#defin
2d1ba 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41  e sqlite3ParserA
2d1bb 52 47 5f 53 54 4f 52 45 20 79 79 70 50 61 72 73  RG_STORE yypPars
2d1bc 65 72 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61  er->pParse = pPa
2d1bd 72 73 65 0a 23 64 65 66 69 6e 65 20 59 59 4e 53  rse.#define YYNS
2d1be 54 41 54 45 20 36 31 36 0a 23 64 65 66 69 6e 65  TATE 616.#define
2d1bf 20 59 59 4e 52 55 4c 45 20 33 32 33 0a 23 64 65   YYNRULE 323.#de
2d1c0 66 69 6e 65 20 59 59 46 41 4c 4c 42 41 43 4b 20  fine YYFALLBACK 
2d1c1 31 0a 23 64 65 66 69 6e 65 20 59 59 5f 4e 4f 5f  1.#define YY_NO_
2d1c2 41 43 54 49 4f 4e 20 20 20 20 20 20 28 59 59 4e  ACTION      (YYN
2d1c3 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32 29  STATE+YYNRULE+2)
2d1c4 0a 23 64 65 66 69 6e 65 20 59 59 5f 41 43 43 45  .#define YY_ACCE
2d1c5 50 54 5f 41 43 54 49 4f 4e 20 20 28 59 59 4e 53  PT_ACTION  (YYNS
2d1c6 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31 29 0a  TATE+YYNRULE+1).
2d1c7 23 64 65 66 69 6e 65 20 59 59 5f 45 52 52 4f 52  #define YY_ERROR
2d1c8 5f 41 43 54 49 4f 4e 20 20 20 28 59 59 4e 53 54  _ACTION   (YYNST
2d1c9 41 54 45 2b 59 59 4e 52 55 4c 45 29 0a 0a 2f 2a  ATE+YYNRULE)../*
2d1ca 20 54 68 65 20 79 79 7a 65 72 6f 6d 69 6e 6f 72   The yyzerominor
2d1cb 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 75 73 65   constant is use
2d1cc 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20  d to initialize 
2d1cd 69 6e 73 74 61 6e 63 65 73 20 6f 66 0a 2a 2a 20  instances of.** 
2d1ce 59 59 4d 49 4e 4f 52 54 59 50 45 20 6f 62 6a 65  YYMINORTYPE obje
2d1cf 63 74 73 20 74 6f 20 7a 65 72 6f 2e 20 2a 2f 0a  cts to zero. */.
2d1d0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59 4d  static const YYM
2d1d1 49 4e 4f 52 54 59 50 45 20 79 79 7a 65 72 6f 6d  INORTYPE yyzerom
2d1d2 69 6e 6f 72 20 3d 20 7b 20 30 20 7d 3b 0a 0a 0a  inor = { 0 };...
2d1d3 2f 2a 20 4e 65 78 74 20 61 72 65 20 74 68 65 20  /* Next are the 
2d1d4 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 64  tables used to d
2d1d5 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 61 63  etermine what ac
2d1d6 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 62 61 73  tion to take bas
2d1d7 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72  ed on the.** cur
2d1d8 72 65 6e 74 20 73 74 61 74 65 20 61 6e 64 20 6c  rent state and l
2d1d9 6f 6f 6b 61 68 65 61 64 20 74 6f 6b 65 6e 2e 20  ookahead token. 
2d1da 20 54 68 65 73 65 20 74 61 62 6c 65 73 20 61 72   These tables ar
2d1db 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  e used to implem
2d1dc 65 6e 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  ent.** functions
2d1dd 20 74 68 61 74 20 74 61 6b 65 20 61 20 73 74 61   that take a sta
2d1de 74 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 6c 6f  te number and lo
2d1df 6f 6b 61 68 65 61 64 20 76 61 6c 75 65 20 61 6e  okahead value an
2d1e0 64 20 72 65 74 75 72 6e 20 61 6e 0a 2a 2a 20 61  d return an.** a
2d1e1 63 74 69 6f 6e 20 69 6e 74 65 67 65 72 2e 20 20  ction integer.  
2d1e2 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74  .**.** Suppose t
2d1e3 68 65 20 61 63 74 69 6f 6e 20 69 6e 74 65 67 65  he action intege
2d1e4 72 20 69 73 20 4e 2e 20 20 54 68 65 6e 20 74 68  r is N.  Then th
2d1e5 65 20 61 63 74 69 6f 6e 20 69 73 20 64 65 74 65  e action is dete
2d1e6 72 6d 69 6e 65 64 20 61 73 0a 2a 2a 20 66 6f 6c  rmined as.** fol
2d1e7 6c 6f 77 73 0a 2a 2a 0a 2a 2a 20 20 20 30 20 3c  lows.**.**   0 <
2d1e8 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54 45 20 20  = N < YYNSTATE  
2d1e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d1ea 53 68 69 66 74 20 4e 2e 20 20 54 68 61 74 20 69  Shift N.  That i
2d1eb 73 2c 20 70 75 73 68 20 74 68 65 20 6c 6f 6f 6b  s, push the look
2d1ec 61 68 65 61 64 0a 2a 2a 20 20 20 20 20 20 20 20  ahead.**        
2d1ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d1ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
2d1ef 6b 65 6e 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  ken onto the sta
2d1f0 63 6b 20 61 6e 64 20 67 6f 74 6f 20 73 74 61 74  ck and goto stat
2d1f1 65 20 4e 2e 0a 2a 2a 0a 2a 2a 20 20 20 59 59 4e  e N..**.**   YYN
2d1f2 53 54 41 54 45 20 3c 3d 20 4e 20 3c 20 59 59 4e  STATE <= N < YYN
2d1f3 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 20 20 20  STATE+YYNRULE   
2d1f4 52 65 64 75 63 65 20 62 79 20 72 75 6c 65 20 4e  Reduce by rule N
2d1f5 2d 59 59 4e 53 54 41 54 45 2e 0a 2a 2a 0a 2a 2a  -YYNSTATE..**.**
2d1f6 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45     N == YYNSTATE
2d1f7 2b 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20 20  +YYNRULE        
2d1f8 20 20 20 20 20 20 41 20 73 79 6e 74 61 78 20 65        A syntax e
2d1f9 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
2d1fa 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20  d..**.**   N == 
2d1fb 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45  YYNSTATE+YYNRULE
2d1fc 2b 31 20 20 20 20 20 20 20 20 20 20 20 20 54 68  +1            Th
2d1fd 65 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73  e parser accepts
2d1fe 20 69 74 73 20 69 6e 70 75 74 2e 0a 2a 2a 0a 2a   its input..**.*
2d1ff 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54  *   N == YYNSTAT
2d200 45 2b 59 59 4e 52 55 4c 45 2b 32 20 20 20 20 20  E+YYNRULE+2     
2d201 20 20 20 20 20 20 20 4e 6f 20 73 75 63 68 20 61         No such a
2d202 63 74 69 6f 6e 2e 20 20 44 65 6e 6f 74 65 73 20  ction.  Denotes 
2d203 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 20 20  unused.**       
2d204 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d205 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2d206 6c 6f 74 73 20 69 6e 20 74 68 65 20 79 79 5f 61  lots in the yy_a
2d207 63 74 69 6f 6e 5b 5d 20 74 61 62 6c 65 2e 0a 2a  ction[] table..*
2d208 2a 0a 2a 2a 20 54 68 65 20 61 63 74 69 6f 6e 20  *.** The action 
2d209 74 61 62 6c 65 20 69 73 20 63 6f 6e 73 74 72 75  table is constru
2d20a 63 74 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65  cted as a single
2d20b 20 6c 61 72 67 65 20 74 61 62 6c 65 20 6e 61 6d   large table nam
2d20c 65 64 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 2e 0a  ed yy_action[]..
2d20d 2a 2a 20 47 69 76 65 6e 20 73 74 61 74 65 20 53  ** Given state S
2d20e 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20 58   and lookahead X
2d20f 2c 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20  , the action is 
2d210 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 0a 2a  computed as.**.*
2d211 2a 20 20 20 20 20 20 79 79 5f 61 63 74 69 6f 6e  *      yy_action
2d212 5b 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b  [ yy_shift_ofst[
2d213 53 5d 20 2b 20 58 20 5d 0a 2a 2a 0a 2a 2a 20 49  S] + X ].**.** I
2d214 66 20 74 68 65 20 69 6e 64 65 78 20 76 61 6c 75  f the index valu
2d215 65 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b  e yy_shift_ofst[
2d216 53 5d 2b 58 20 69 73 20 6f 75 74 20 6f 66 20 72  S]+X is out of r
2d217 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 76  ange or if the v
2d218 61 6c 75 65 0a 2a 2a 20 79 79 5f 6c 6f 6f 6b 61  alue.** yy_looka
2d219 68 65 61 64 5b 79 79 5f 73 68 69 66 74 5f 6f 66  head[yy_shift_of
2d21a 73 74 5b 53 5d 2b 58 5d 20 69 73 20 6e 6f 74 20  st[S]+X] is not 
2d21b 65 71 75 61 6c 20 74 6f 20 58 20 6f 72 20 69 66  equal to X or if
2d21c 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53   yy_shift_ofst[S
2d21d 5d 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 74 6f  ].** is equal to
2d21e 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46   YY_SHIFT_USE_DF
2d21f 4c 54 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  LT, it means tha
2d220 74 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20  t the action is 
2d221 6e 6f 74 20 69 6e 20 74 68 65 20 74 61 62 6c 65  not in the table
2d222 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 79 79 5f  .** and that yy_
2d223 64 65 66 61 75 6c 74 5b 53 5d 20 73 68 6f 75 6c  default[S] shoul
2d224 64 20 62 65 20 75 73 65 64 20 69 6e 73 74 65 61  d be used instea
2d225 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  d.  .**.** The f
2d226 6f 72 6d 75 6c 61 20 61 62 6f 76 65 20 69 73 20  ormula above is 
2d227 66 6f 72 20 63 6f 6d 70 75 74 69 6e 67 20 74 68  for computing th
2d228 65 20 61 63 74 69 6f 6e 20 77 68 65 6e 20 74 68  e action when th
2d229 65 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 0a 2a  e lookahead is.*
2d22a 2a 20 61 20 74 65 72 6d 69 6e 61 6c 20 73 79 6d  * a terminal sym
2d22b 62 6f 6c 2e 20 20 49 66 20 74 68 65 20 6c 6f 6f  bol.  If the loo
2d22c 6b 61 68 65 61 64 20 69 73 20 61 20 6e 6f 6e 2d  kahead is a non-
2d22d 74 65 72 6d 69 6e 61 6c 20 28 61 73 20 6f 63 63  terminal (as occ
2d22e 75 72 73 20 61 66 74 65 72 0a 2a 2a 20 61 20 72  urs after.** a r
2d22f 65 64 75 63 65 20 61 63 74 69 6f 6e 29 20 74 68  educe action) th
2d230 65 6e 20 74 68 65 20 79 79 5f 72 65 64 75 63 65  en the yy_reduce
2d231 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20 69 73  _ofst[] array is
2d232 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f   used in place o
2d233 66 0a 2a 2a 20 74 68 65 20 79 79 5f 73 68 69 66  f.** the yy_shif
2d234 74 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20 61  t_ofst[] array a
2d235 6e 64 20 59 59 5f 52 45 44 55 43 45 5f 55 53 45  nd YY_REDUCE_USE
2d236 5f 44 46 4c 54 20 69 73 20 75 73 65 64 20 69 6e  _DFLT is used in
2d237 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 59 59 5f   place of.** YY_
2d238 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2e 0a  SHIFT_USE_DFLT..
2d239 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
2d23a 69 6e 67 20 61 72 65 20 74 68 65 20 74 61 62 6c  ing are the tabl
2d23b 65 73 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  es generated in 
2d23c 74 68 69 73 20 73 65 63 74 69 6f 6e 3a 0a 2a 2a  this section:.**
2d23d 0a 2a 2a 20 20 79 79 5f 61 63 74 69 6f 6e 5b 5d  .**  yy_action[]
2d23e 20 20 20 20 20 20 20 20 41 20 73 69 6e 67 6c 65          A single
2d23f 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   table containin
2d240 67 20 61 6c 6c 20 61 63 74 69 6f 6e 73 2e 0a 2a  g all actions..*
2d241 2a 20 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b  *  yy_lookahead[
2d242 5d 20 20 20 20 20 41 20 74 61 62 6c 65 20 63 6f  ]     A table co
2d243 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6c 6f 6f  ntaining the loo
2d244 6b 61 68 65 61 64 20 66 6f 72 20 65 61 63 68 20  kahead for each 
2d245 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 20 20 20 20  entry in.**     
2d246 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d247 79 79 5f 61 63 74 69 6f 6e 2e 20 20 55 73 65 64  yy_action.  Used
2d248 20 74 6f 20 64 65 74 65 63 74 20 68 61 73 68 20   to detect hash 
2d249 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 2a 2a 20 20  collisions..**  
2d24a 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20  yy_shift_ofst[] 
2d24b 20 20 20 46 6f 72 20 65 61 63 68 20 73 74 61 74     For each stat
2d24c 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e  e, the offset in
2d24d 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f 72  to yy_action for
2d24e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2d24f 20 20 20 20 20 20 20 20 73 68 69 66 74 69 6e 67          shifting
2d250 20 74 65 72 6d 69 6e 61 6c 73 2e 0a 2a 2a 20 20   terminals..**  
2d251 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d  yy_reduce_ofst[]
2d252 20 20 20 46 6f 72 20 65 61 63 68 20 73 74 61 74     For each stat
2d253 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e  e, the offset in
2d254 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f 72  to yy_action for
2d255 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2d256 20 20 20 20 20 20 20 20 73 68 69 66 74 69 6e 67          shifting
2d257 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 73 20 61   non-terminals a
2d258 66 74 65 72 20 61 20 72 65 64 75 63 65 2e 0a 2a  fter a reduce..*
2d259 2a 20 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d 20  *  yy_default[] 
2d25a 20 20 20 20 20 20 44 65 66 61 75 6c 74 20 61 63        Default ac
2d25b 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 73 74  tion for each st
2d25c 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ate..*/.static c
2d25d 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54 59 50  onst YYACTIONTYP
2d25e 45 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 3d 20  E yy_action[] = 
2d25f 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20  {. /*     0 */  
2d260 20 33 30 34 2c 20 20 39 34 30 2c 20 20 31 37 36   304,  940,  176
2d261 2c 20 20 36 31 35 2c 20 20 20 20 32 2c 20 20 31  ,  615,    2,  1
2d262 35 30 2c 20 20 32 31 34 2c 20 20 34 33 39 2c 20  50,  214,  439, 
2d263 20 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20    24,   24,. /* 
2d264 20 20 20 31 30 20 2a 2f 20 20 20 20 32 34 2c 20     10 */    24, 
2d265 20 20 32 34 2c 20 20 34 38 38 2c 20 20 20 32 36    24,  488,   26
2d266 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2d267 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20  26,   27,   27, 
2d268 20 20 32 38 2c 0a 20 2f 2a 20 20 20 20 32 30 20    28,. /*    20 
2d269 2a 2f 20 20 20 20 32 38 2c 20 20 20 32 38 2c 20  */    28,   28, 
2d26a 20 20 32 39 2c 20 20 32 31 36 2c 20 20 34 31 33    29,  216,  413
2d26b 2c 20 20 34 31 34 2c 20 20 32 31 32 2c 20 20 34  ,  414,  212,  4
2d26c 31 33 2c 20 20 34 31 34 2c 20 20 34 34 36 2c 0a  13,  414,  446,.
2d26d 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 34   /*    30 */   4
2d26e 35 32 2c 20 20 20 33 31 2c 20 20 20 32 36 2c 20  52,   31,   26, 
2d26f 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36    26,   26,   26
2d270 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20  ,   27,   27,   
2d271 32 38 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20  28,   28,. /*   
2d272 20 34 30 20 2a 2f 20 20 20 20 32 38 2c 20 20 20   40 */    28,   
2d273 32 39 2c 20 20 32 31 36 2c 20 20 20 33 30 2c 20  29,  216,   30, 
2d274 20 34 38 33 2c 20 20 20 33 32 2c 20 20 31 33 34   483,   32,  134
2d275 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20 33  ,   23,   22,  3
2d276 30 38 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f  08,. /*    50 */
2d277 20 20 20 34 35 36 2c 20 20 34 35 37 2c 20 20 34     456,  457,  4
2d278 35 33 2c 20 20 34 35 33 2c 20 20 20 32 35 2c 20  53,  453,   25, 
2d279 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34    25,   24,   24
2d27a 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f  ,   24,   24,. /
2d27b 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 34 33 36  *    60 */   436
2d27c 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2d27d 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20  26,   26,   27, 
2d27e 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38    27,   28,   28
2d27f 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 20 37  ,   28,. /*    7
2d280 30 20 2a 2f 20 20 20 20 32 39 2c 20 20 32 31 36  0 */    29,  216
2d281 2c 20 20 33 30 34 2c 20 20 32 31 36 2c 20 20 33  ,  304,  216,  3
2d282 31 31 2c 20 20 34 33 39 2c 20 20 35 31 32 2c 20  11,  439,  512, 
2d283 20 34 39 30 2c 20 20 20 34 35 2c 20 20 20 32 36   490,   45,   26
2d284 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20  ,. /*    80 */  
2d285 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36    26,   26,   26
2d286 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20  ,   27,   27,   
2d287 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20  28,   28,   28, 
2d288 20 20 32 39 2c 20 20 32 31 36 2c 0a 20 2f 2a 20    29,  216,. /* 
2d289 20 20 20 39 30 20 2a 2f 20 20 20 34 31 33 2c 20     90 */   413, 
2d28a 20 34 31 34 2c 20 20 34 31 36 2c 20 20 34 31 37   414,  416,  417
2d28b 2c 20 20 31 35 36 2c 20 20 34 31 36 2c 20 20 34  ,  156,  416,  4
2d28c 31 37 2c 20 20 33 36 30 2c 20 20 33 36 33 2c 20  17,  360,  363, 
2d28d 20 33 36 34 2c 0a 20 2f 2a 20 20 20 31 30 30 20   364,. /*   100 
2d28e 2a 2f 20 20 20 33 31 31 2c 20 20 34 34 36 2c 20  */   311,  446, 
2d28f 20 34 35 32 2c 20 20 33 38 35 2c 20 20 35 31 34   452,  385,  514
2d290 2c 20 20 20 32 31 2c 20 20 31 38 36 2c 20 20 34  ,   21,  186,  4
2d291 39 35 2c 20 20 33 36 35 2c 20 20 20 32 37 2c 0a  95,  365,   27,.
2d292 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 20   /*   110 */    
2d293 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20  27,   28,   28, 
2d294 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 36    28,   29,  216
2d295 2c 20 20 34 31 33 2c 20 20 34 31 34 2c 20 20 34  ,  413,  414,  4
2d296 31 35 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20  15,   23,. /*   
2d297 31 32 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 33  120 */    22,  3
2d298 30 38 2c 20 20 34 35 36 2c 20 20 34 35 37 2c 20  08,  456,  457, 
2d299 20 34 35 33 2c 20 20 34 35 33 2c 20 20 20 32 35   453,  453,   25
2d29a 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20  ,   25,   24,   
2d29b 32 34 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f  24,. /*   130 */
2d29c 20 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 35      24,   24,  5
2d29d 35 35 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  55,   26,   26, 
2d29e 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37    26,   26,   27
2d29f 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 0a 20 2f  ,   27,   28,. /
2d2a0 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20 32 38  *   140 */    28
2d2a1 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32  ,   28,   29,  2
2d2a2 31 36 2c 20 20 33 30 34 2c 20 20 32 32 38 2c 20  16,  304,  228, 
2d2a3 20 35 30 34 2c 20 20 31 33 35 2c 20 20 34 36 38   504,  135,  468
2d2a4 2c 20 20 32 31 38 2c 0a 20 2f 2a 20 20 20 31 35  ,  218,. /*   15
2d2a5 30 20 2a 2f 20 20 20 35 34 38 2c 20 20 31 34 35  0 */   548,  145
2d2a6 2c 20 20 31 33 32 2c 20 20 32 35 36 2c 20 20 33  ,  132,  256,  3
2d2a7 35 38 2c 20 20 32 36 31 2c 20 20 33 35 39 2c 20  58,  261,  359, 
2d2a8 20 31 35 33 2c 20 20 34 31 36 2c 20 20 34 31 37   153,  416,  417
2d2a9 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20  ,. /*   160 */  
2d2aa 20 32 34 31 2c 20 20 35 39 38 2c 20 20 33 33 31   241,  598,  331
2d2ab 2c 20 20 20 33 30 2c 20 20 32 36 35 2c 20 20 20  ,   30,  265,   
2d2ac 33 32 2c 20 20 31 33 34 2c 20 20 34 33 39 2c 20  32,  134,  439, 
2d2ad 20 35 39 36 2c 20 20 35 39 37 2c 0a 20 2f 2a 20   596,  597,. /* 
2d2ae 20 20 31 37 30 20 2a 2f 20 20 20 32 33 30 2c 20    170 */   230, 
2d2af 20 32 32 38 2c 20 20 34 39 30 2c 20 20 34 34 36   228,  490,  446
2d2b0 2c 20 20 34 35 32 2c 20 20 20 35 37 2c 20 20 35  ,  452,   57,  5
2d2b1 30 36 2c 20 20 33 32 38 2c 20 20 31 33 32 2c 20  06,  328,  132, 
2d2b2 20 32 35 36 2c 0a 20 2f 2a 20 20 20 31 38 30 20   256,. /*   180 
2d2b3 2a 2f 20 20 20 33 35 38 2c 20 20 32 36 31 2c 20  */   358,  261, 
2d2b4 20 33 35 39 2c 20 20 31 35 33 2c 20 20 34 31 36   359,  153,  416
2d2b5 2c 20 20 34 31 37 2c 20 20 34 33 35 2c 20 20 20  ,  417,  435,   
2d2b6 37 38 2c 20 20 34 30 38 2c 20 20 34 30 35 2c 0a  78,  408,  405,.
2d2b7 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 32   /*   190 */   2
2d2b8 36 35 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20  65,   23,   22, 
2d2b9 20 33 30 38 2c 20 20 34 35 36 2c 20 20 34 35 37   308,  456,  457
2d2ba 2c 20 20 34 35 33 2c 20 20 34 35 33 2c 20 20 20  ,  453,  453,   
2d2bb 32 35 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20  25,   25,. /*   
2d2bc 32 30 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 20  200 */    24,   
2d2bd 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20  24,   24,   24, 
2d2be 20 33 34 32 2c 20 20 20 32 36 2c 20 20 20 32 36   342,   26,   26
2d2bf 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2d2c0 32 37 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f  27,. /*   210 */
2d2c1 20 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20      27,   28,   
2d2c2 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20  28,   28,   29, 
2d2c3 20 32 31 36 2c 20 20 33 30 34 2c 20 20 32 31 34   216,  304,  214
2d2c4 2c 20 20 35 33 34 2c 20 20 35 34 37 2c 0a 20 2f  ,  534,  547,. /
2d2c5 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 33 30 37  *   220 */   307
2d2c6 2c 20 20 31 32 37 2c 20 20 34 38 39 2c 20 20 35  ,  127,  489,  5
2d2c7 39 35 2c 20 20 20 33 30 2c 20 20 33 33 31 2c 20  95,   30,  331, 
2d2c8 20 20 33 32 2c 20 20 31 33 34 2c 20 20 33 34 35    32,  134,  345
2d2c9 2c 20 20 33 38 37 2c 0a 20 2f 2a 20 20 20 32 33  ,  387,. /*   23
2d2ca 30 20 2a 2f 20 20 20 34 32 39 2c 20 20 20 36 33  0 */   429,   63
2d2cb 2c 20 20 33 33 31 2c 20 20 33 35 35 2c 20 20 34  ,  331,  355,  4
2d2cc 31 35 2c 20 20 34 33 39 2c 20 20 35 30 37 2c 20  15,  439,  507, 
2d2cd 20 33 33 31 2c 20 20 34 31 35 2c 20 20 35 33 35   331,  415,  535
2d2ce 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20  ,. /*   240 */  
2d2cf 20 33 32 38 2c 20 20 32 31 35 2c 20 20 31 39 33   328,  215,  193
2d2d0 2c 20 20 35 39 34 2c 20 20 35 39 33 2c 20 20 34  ,  594,  593,  4
2d2d1 34 36 2c 20 20 34 35 32 2c 20 20 33 32 38 2c 20  46,  452,  328, 
2d2d2 20 20 31 38 2c 20 20 34 33 35 2c 0a 20 2f 2a 20    18,  435,. /* 
2d2d3 20 20 32 35 30 20 2a 2f 20 20 20 20 38 35 2c 20    250 */    85, 
2d2d4 20 20 31 36 2c 20 20 33 32 38 2c 20 20 31 38 33    16,  328,  183
2d2d5 2c 20 20 31 39 30 2c 20 20 35 35 36 2c 20 20 34  ,  190,  556,  4
2d2d6 33 35 2c 20 20 20 37 38 2c 20 20 33 30 39 2c 20  35,   78,  309, 
2d2d7 20 34 36 33 2c 0a 20 2f 2a 20 20 20 32 36 30 20   463,. /*   260 
2d2d8 2a 2f 20 20 20 34 36 34 2c 20 20 34 33 35 2c 20  */   464,  435, 
2d2d9 20 20 38 35 2c 20 20 20 32 33 2c 20 20 20 32 32    85,   23,   22
2d2da 2c 20 20 33 30 38 2c 20 20 34 35 36 2c 20 20 34  ,  308,  456,  4
2d2db 35 37 2c 20 20 34 35 33 2c 20 20 34 35 33 2c 0a  57,  453,  453,.
2d2dc 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 20   /*   270 */    
2d2dd 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20  25,   25,   24, 
2d2de 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34    24,   24,   24
2d2df 2c 20 20 34 33 36 2c 20 20 20 32 36 2c 20 20 20  ,  436,   26,   
2d2e0 32 36 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20 20  26,   26,. /*   
2d2e1 32 38 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20  280 */    26,   
2d2e2 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20  27,   27,   28, 
2d2e3 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39    28,   28,   29
2d2e4 2c 20 20 32 31 36 2c 20 20 33 30 34 2c 20 20 33  ,  216,  304,  3
2d2e5 34 37 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f  47,. /*   290 */
2d2e6 20 20 20 32 32 31 2c 20 20 33 31 33 2c 20 20 35     221,  313,  5
2d2e7 39 35 2c 20 20 31 39 31 2c 20 20 33 37 38 2c 20  95,  191,  378, 
2d2e8 20 33 33 31 2c 20 20 34 37 32 2c 20 20 32 33 34   331,  472,  234
2d2e9 2c 20 20 33 34 35 2c 20 20 33 38 31 2c 0a 20 2f  ,  345,  381,. /
2d2ea 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 33 32 34  *   300 */   324
2d2eb 2c 20 20 34 31 30 2c 20 20 32 32 30 2c 20 20 33  ,  410,  220,  3
2d2ec 34 34 2c 20 20 35 39 32 2c 20 20 32 31 37 2c 20  44,  592,  217, 
2d2ed 20 32 31 33 2c 20 20 34 31 35 2c 20 20 31 31 32   213,  415,  112
2d2ee 2c 20 20 33 33 31 2c 0a 20 2f 2a 20 20 20 33 31  ,  331,. /*   31
2d2ef 30 20 2a 2f 20 20 20 33 32 38 2c 20 20 20 20 34  0 */   328,    4
2d2f0 2c 20 20 35 39 34 2c 20 20 33 39 39 2c 20 20 32  ,  594,  399,  2
2d2f1 31 31 2c 20 20 35 35 34 2c 20 20 35 32 39 2c 20  11,  554,  529, 
2d2f2 20 34 34 36 2c 20 20 34 35 32 2c 20 20 34 33 35   446,  452,  435
2d2f3 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20  ,. /*   320 */  
2d2f4 20 20 37 39 2c 20 20 32 31 37 2c 20 20 35 35 33    79,  217,  553
2d2f5 2c 20 20 35 31 35 2c 20 20 33 32 38 2c 20 20 33  ,  515,  328,  3
2d2f6 33 34 2c 20 20 35 31 33 2c 20 20 34 35 39 2c 20  34,  513,  459, 
2d2f7 20 34 35 39 2c 20 20 34 36 39 2c 0a 20 2f 2a 20   459,  469,. /* 
2d2f8 20 20 33 33 30 20 2a 2f 20 20 20 34 34 31 2c 20    330 */   441, 
2d2f9 20 35 37 32 2c 20 20 34 33 32 2c 20 20 34 33 35   572,  432,  435
2d2fa 2c 20 20 20 37 38 2c 20 20 20 32 33 2c 20 20 20  ,   78,   23,   
2d2fb 32 32 2c 20 20 33 30 38 2c 20 20 34 35 36 2c 20  22,  308,  456, 
2d2fc 20 34 35 37 2c 0a 20 2f 2a 20 20 20 33 34 30 20   457,. /*   340 
2d2fd 2a 2f 20 20 20 34 35 33 2c 20 20 34 35 33 2c 20  */   453,  453, 
2d2fe 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34    25,   25,   24
2d2ff 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20  ,   24,   24,   
2d300 32 34 2c 20 20 34 33 36 2c 20 20 20 32 36 2c 0a  24,  436,   26,.
2d301 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 20   /*   350 */    
2d302 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  26,   26,   26, 
2d303 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38    27,   27,   28
2d304 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20  ,   28,   28,   
2d305 32 39 2c 20 20 32 31 36 2c 0a 20 2f 2a 20 20 20  29,  216,. /*   
2d306 33 36 30 20 2a 2f 20 20 20 33 30 34 2c 20 20 34  360 */   304,  4
2d307 34 33 2c 20 20 34 34 33 2c 20 20 34 34 33 2c 20  43,  443,  443, 
2d308 20 31 35 36 2c 20 20 34 36 38 2c 20 20 32 31 38   156,  468,  218
2d309 2c 20 20 33 36 30 2c 20 20 33 36 33 2c 20 20 33  ,  360,  363,  3
2d30a 36 34 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f  64,. /*   370 */
2d30b 20 20 20 33 33 31 2c 20 20 32 34 37 2c 20 20 33     331,  247,  3
2d30c 39 35 2c 20 20 33 39 38 2c 20 20 32 31 37 2c 20  95,  398,  217, 
2d30d 20 33 34 39 2c 20 20 33 33 31 2c 20 20 20 33 30   349,  331,   30
2d30e 2c 20 20 33 36 35 2c 20 20 20 33 32 2c 0a 20 2f  ,  365,   32,. /
2d30f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 31 33 34  *   380 */   134
2d310 2c 20 20 33 38 38 2c 20 20 32 38 32 2c 20 20 32  ,  388,  282,  2
2d311 38 31 2c 20 20 20 33 39 2c 20 20 33 32 38 2c 20  81,   39,  328, 
2d312 20 20 34 31 2c 20 20 34 33 30 2c 20 20 35 34 35    41,  430,  545
2d313 2c 20 20 34 34 36 2c 0a 20 2f 2a 20 20 20 33 39  ,  446,. /*   39
2d314 30 20 2a 2f 20 20 20 34 35 32 2c 20 20 33 32 38  0 */   452,  328
2d315 2c 20 20 32 31 34 2c 20 20 35 33 31 2c 20 20 34  ,  214,  531,  4
2d316 33 35 2c 20 20 20 39 33 2c 20 20 35 34 32 2c 20  35,   93,  542, 
2d317 20 36 30 31 2c 20 20 20 20 31 2c 20 20 34 30 34   601,    1,  404
2d318 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20  ,. /*   400 */  
2d319 20 34 33 35 2c 20 20 20 39 33 2c 20 20 34 31 33   435,   93,  413
2d31a 2c 20 20 34 31 34 2c 20 20 34 39 35 2c 20 20 20  ,  414,  495,   
2d31b 34 30 2c 20 20 35 33 36 2c 20 20 20 32 33 2c 20  40,  536,   23, 
2d31c 20 20 32 32 2c 20 20 33 30 38 2c 0a 20 2f 2a 20    22,  308,. /* 
2d31d 20 20 34 31 30 20 2a 2f 20 20 20 34 35 36 2c 20    410 */   456, 
2d31e 20 34 35 37 2c 20 20 34 35 33 2c 20 20 34 35 33   457,  453,  453
2d31f 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20  ,   25,   25,   
2d320 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20  24,   24,   24, 
2d321 20 20 32 34 2c 0a 20 2f 2a 20 20 20 34 32 30 20    24,. /*   420 
2d322 2a 2f 20 20 20 35 37 33 2c 20 20 20 32 36 2c 20  */   573,   26, 
2d323 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36    26,   26,   26
2d324 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20  ,   27,   27,   
2d325 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 0a  28,   28,   28,.
2d326 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 20   /*   430 */    
2d327 32 39 2c 20 20 32 31 36 2c 20 20 33 30 34 2c 20  29,  216,  304, 
2d328 20 32 37 36 2c 20 20 33 33 31 2c 20 20 31 37 39   276,  331,  179
2d329 2c 20 20 35 30 38 2c 20 20 34 39 30 2c 20 20 32  ,  508,  490,  2
2d32a 31 30 2c 20 20 35 34 37 2c 0a 20 2f 2a 20 20 20  10,  547,. /*   
2d32b 34 34 30 20 2a 2f 20 20 20 33 31 39 2c 20 20 34  440 */   319,  4
2d32c 31 33 2c 20 20 34 31 34 2c 20 20 32 32 32 2c 20  13,  414,  222, 
2d32d 20 31 39 32 2c 20 20 33 38 35 2c 20 20 33 32 30   192,  385,  320
2d32e 2c 20 20 32 34 30 2c 20 20 34 31 35 2c 20 20 33  ,  240,  415,  3
2d32f 32 38 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f  28,. /*   450 */
2d330 20 20 20 35 35 37 2c 20 20 20 36 33 2c 20 20 34     557,   63,  4
2d331 31 33 2c 20 20 34 31 34 2c 20 20 34 31 35 2c 20  13,  414,  415, 
2d332 20 36 31 36 2c 20 20 34 30 38 2c 20 20 34 30 35   616,  408,  405
2d333 2c 20 20 34 33 35 2c 20 20 20 37 31 2c 0a 20 2f  ,  435,   71,. /
2d334 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 34 31 35  *   460 */   415
2d335 2c 20 20 34 34 36 2c 20 20 34 35 32 2c 20 20 36  ,  446,  452,  6
2d336 31 31 2c 20 20 35 37 32 2c 20 20 20 32 38 2c 20  11,  572,   28, 
2d337 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39    28,   28,   29
2d338 2c 20 20 32 31 36 2c 0a 20 2f 2a 20 20 20 34 37  ,  216,. /*   47
2d339 30 20 2a 2f 20 20 20 34 31 36 2c 20 20 34 31 37  0 */   416,  417
2d33a 2c 20 20 34 33 36 2c 20 20 33 33 36 2c 20 20 34  ,  436,  336,  4
2d33b 36 33 2c 20 20 34 36 34 2c 20 20 34 30 31 2c 20  63,  464,  401, 
2d33c 20 20 34 33 2c 20 20 34 33 36 2c 20 20 20 32 33    43,  436,   23
2d33d 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20  ,. /*   480 */  
2d33e 20 20 32 32 2c 20 20 33 30 38 2c 20 20 34 35 36    22,  308,  456
2d33f 2c 20 20 34 35 37 2c 20 20 34 35 33 2c 20 20 34  ,  457,  453,  4
2d340 35 33 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20  53,   25,   25, 
2d341 20 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20    24,   24,. /* 
2d342 20 20 34 39 30 20 2a 2f 20 20 20 20 32 34 2c 20    490 */    24, 
2d343 20 20 32 34 2c 20 20 34 39 35 2c 20 20 20 32 36    24,  495,   26
2d344 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2d345 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20  26,   27,   27, 
2d346 20 20 32 38 2c 0a 20 2f 2a 20 20 20 35 30 30 20    28,. /*   500 
2d347 2a 2f 20 20 20 20 32 38 2c 20 20 20 32 38 2c 20  */    28,   28, 
2d348 20 20 32 39 2c 20 20 32 31 36 2c 20 20 33 30 34    29,  216,  304
2d349 2c 20 20 36 31 32 2c 20 20 32 30 39 2c 20 20 31  ,  612,  209,  1
2d34a 33 35 2c 20 20 35 31 31 2c 20 20 34 31 36 2c 0a  35,  511,  416,.
2d34b 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 34   /*   510 */   4
2d34c 31 37 2c 20 20 34 33 31 2c 20 20 32 33 33 2c 20  17,  431,  233, 
2d34d 20 20 36 34 2c 20 20 33 38 38 2c 20 20 32 38 32    64,  388,  282
2d34e 2c 20 20 32 38 31 2c 20 20 34 33 39 2c 20 20 20  ,  281,  439,   
2d34f 36 36 2c 20 20 35 34 32 2c 0a 20 2f 2a 20 20 20  66,  542,. /*   
2d350 35 32 30 20 2a 2f 20 20 20 34 31 36 2c 20 20 34  520 */   416,  4
2d351 31 37 2c 20 20 34 31 33 2c 20 20 34 31 34 2c 20  17,  413,  414, 
2d352 20 31 35 36 2c 20 20 32 31 34 2c 20 20 34 30 33   156,  214,  403
2d353 2c 20 20 33 36 30 2c 20 20 33 36 33 2c 20 20 33  ,  360,  363,  3
2d354 36 34 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f  64,. /*   530 */
2d355 20 20 20 35 34 37 2c 20 20 32 35 32 2c 20 20 34     547,  252,  4
2d356 39 30 2c 20 20 34 34 36 2c 20 20 34 35 32 2c 20  90,  446,  452, 
2d357 20 34 39 31 2c 20 20 32 31 37 2c 20 20 20 20 38   491,  217,    8
2d358 2c 20 20 33 36 35 2c 20 20 34 39 35 2c 0a 20 2f  ,  365,  495,. /
2d359 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 34 33 36  *   540 */   436
2d35a 2c 20 20 36 30 36 2c 20 20 20 36 33 2c 20 20 35  ,  606,   63,  5
2d35b 33 37 2c 20 20 32 39 39 2c 20 20 34 31 35 2c 20  37,  299,  415, 
2d35c 20 34 39 32 2c 20 20 34 37 30 2c 20 20 35 34 36   492,  470,  546
2d35d 2c 20 20 32 30 30 2c 0a 20 2f 2a 20 20 20 35 35  ,  200,. /*   55
2d35e 30 20 2a 2f 20 20 20 31 39 36 2c 20 20 20 32 33  0 */   196,   23
2d35f 2c 20 20 20 32 32 2c 20 20 33 30 38 2c 20 20 34  ,   22,  308,  4
2d360 35 36 2c 20 20 34 35 37 2c 20 20 34 35 33 2c 20  56,  457,  453, 
2d361 20 34 35 33 2c 20 20 20 32 35 2c 20 20 20 32 35   453,   25,   25
2d362 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20  ,. /*   560 */  
2d363 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34    24,   24,   24
2d364 2c 20 20 20 32 34 2c 20 20 33 38 36 2c 20 20 20  ,   24,  386,   
2d365 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  26,   26,   26, 
2d366 20 20 32 36 2c 20 20 20 32 37 2c 0a 20 2f 2a 20    26,   27,. /* 
2d367 20 20 35 37 30 20 2a 2f 20 20 20 20 32 37 2c 20    570 */    27, 
2d368 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38    28,   28,   28
2d369 2c 20 20 20 32 39 2c 20 20 32 31 36 2c 20 20 33  ,   29,  216,  3
2d36a 30 34 2c 20 20 34 37 37 2c 20 20 32 35 34 2c 20  04,  477,  254, 
2d36b 20 33 35 34 2c 0a 20 2f 2a 20 20 20 35 38 30 20   354,. /*   580 
2d36c 2a 2f 20 20 20 35 32 38 2c 20 20 20 36 30 2c 20  */   528,   60, 
2d36d 20 35 31 37 2c 20 20 35 31 38 2c 20 20 34 33 36   517,  518,  436
2d36e 2c 20 20 34 33 39 2c 20 20 33 38 39 2c 20 20 33  ,  439,  389,  3
2d36f 33 31 2c 20 20 33 35 36 2c 20 20 20 20 37 2c 0a  31,  356,    7,.
2d370 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 34   /*   590 */   4
2d371 31 36 2c 20 20 34 31 37 2c 20 20 33 33 31 2c 20  16,  417,  331, 
2d372 20 34 37 38 2c 20 20 33 32 38 2c 20 20 32 30 38   478,  328,  208
2d373 2c 20 20 31 39 37 2c 20 20 31 33 37 2c 20 20 34  ,  197,  137,  4
2d374 36 30 2c 20 20 34 39 39 2c 0a 20 2f 2a 20 20 20  60,  499,. /*   
2d375 36 30 30 20 2a 2f 20 20 20 34 34 37 2c 20 20 34  600 */   447,  4
2d376 34 38 2c 20 20 33 32 38 2c 20 20 34 33 35 2c 20  48,  328,  435, 
2d377 20 20 20 39 2c 20 20 34 34 36 2c 20 20 34 35 32     9,  446,  452
2d378 2c 20 20 33 32 38 2c 20 20 34 37 39 2c 20 20 34  ,  328,  479,  4
2d379 38 35 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f  85,. /*   610 */
2d37a 20 20 20 35 31 39 2c 20 20 34 33 35 2c 20 20 20     519,  435,   
2d37b 37 32 2c 20 20 35 36 37 2c 20 20 34 31 35 2c 20  72,  567,  415, 
2d37c 20 34 33 34 2c 20 20 34 33 35 2c 20 20 20 36 37   434,  435,   67
2d37d 2c 20 20 34 38 36 2c 20 20 34 33 33 2c 0a 20 2f  ,  486,  433,. /
2d37e 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 35 32 30  *   620 */   520
2d37f 2c 20 20 34 35 30 2c 20 20 34 35 31 2c 20 20 20  ,  450,  451,   
2d380 32 33 2c 20 20 20 32 32 2c 20 20 33 30 38 2c 20  23,   22,  308, 
2d381 20 34 35 36 2c 20 20 34 35 37 2c 20 20 34 35 33   456,  457,  453
2d382 2c 20 20 34 35 33 2c 0a 20 2f 2a 20 20 20 36 33  ,  453,. /*   63
2d383 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 20 32 35  0 */    25,   25
2d384 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20  ,   24,   24,   
2d385 32 34 2c 20 20 20 32 34 2c 20 20 33 33 31 2c 20  24,   24,  331, 
2d386 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36    26,   26,   26
2d387 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20 20  ,. /*   640 */  
2d388 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37    26,   27,   27
2d389 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20  ,   28,   28,   
2d38a 32 38 2c 20 20 20 32 39 2c 20 20 32 31 36 2c 20  28,   29,  216, 
2d38b 20 33 30 34 2c 20 20 33 33 31 2c 0a 20 2f 2a 20   304,  331,. /* 
2d38c 20 20 36 35 30 20 2a 2f 20 20 20 34 34 39 2c 20    650 */   449, 
2d38d 20 33 32 38 2c 20 20 32 36 38 2c 20 20 33 39 30   328,  268,  390
2d38e 2c 20 20 34 36 31 2c 20 20 33 33 31 2c 20 20 20  ,  461,  331,   
2d38f 36 35 2c 20 20 33 33 31 2c 20 20 33 36 38 2c 20  65,  331,  368, 
2d390 20 34 33 34 2c 0a 20 2f 2a 20 20 20 36 36 30 20   434,. /*   660 
2d391 2a 2f 20 20 20 34 33 35 2c 20 20 20 37 36 2c 20  */   435,   76, 
2d392 20 33 31 30 2c 20 20 34 33 33 2c 20 20 33 32 38   310,  433,  328
2d393 2c 20 20 31 35 30 2c 20 20 34 32 37 2c 20 20 34  ,  150,  427,  4
2d394 33 39 2c 20 20 34 37 33 2c 20 20 33 33 31 2c 0a  39,  473,  331,.
2d395 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20 20 20 33   /*   670 */   3
2d396 32 38 2c 20 20 34 39 39 2c 20 20 33 32 38 2c 20  28,  499,  328, 
2d397 20 34 33 35 2c 20 20 20 39 37 2c 20 20 20 32 39   435,   97,   29
2d398 2c 20 20 32 31 36 2c 20 20 34 34 36 2c 20 20 34  ,  216,  446,  4
2d399 35 32 2c 20 20 34 33 35 2c 0a 20 2f 2a 20 20 20  52,  435,. /*   
2d39a 36 38 30 20 2a 2f 20 20 20 20 39 36 2c 20 20 34  680 */    96,  4
2d39b 33 35 2c 20 20 31 30 31 2c 20 20 33 35 33 2c 20  35,  101,  353, 
2d39c 20 33 32 38 2c 20 20 33 37 32 2c 20 20 34 31 35   328,  372,  415
2d39d 2c 20 20 33 33 34 2c 20 20 31 35 34 2c 20 20 34  ,  334,  154,  4
2d39e 35 39 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a 2f  59,. /*   690 */
2d39f 20 20 20 34 35 39 2c 20 20 33 35 32 2c 20 20 35     459,  352,  5
2d3a0 36 39 2c 20 20 34 33 35 2c 20 20 20 39 39 2c 20  69,  435,   99, 
2d3a1 20 20 32 33 2c 20 20 20 32 32 2c 20 20 33 30 38    23,   22,  308
2d3a2 2c 20 20 34 35 36 2c 20 20 34 35 37 2c 0a 20 2f  ,  456,  457,. /
2d3a3 2a 20 20 20 37 30 30 20 2a 2f 20 20 20 34 35 33  *   700 */   453
2d3a4 2c 20 20 34 35 33 2c 20 20 20 32 35 2c 20 20 20  ,  453,   25,   
2d3a5 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20  25,   24,   24, 
2d3a6 20 20 32 34 2c 20 20 20 32 34 2c 20 20 33 33 31    24,   24,  331
2d3a7 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20 20 37 31  ,   26,. /*   71
2d3a8 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 36  0 */    26,   26
2d3a9 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20  ,   26,   27,   
2d3aa 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20  27,   28,   28, 
2d3ab 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 36    28,   29,  216
2d3ac 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20 20  ,. /*   720 */  
2d3ad 20 33 30 34 2c 20 20 33 33 31 2c 20 20 32 34 38   304,  331,  248
2d3ae 2c 20 20 33 32 38 2c 20 20 32 36 34 2c 20 20 20  ,  328,  264,   
2d3af 35 36 2c 20 20 33 33 34 2c 20 20 33 33 31 2c 20  56,  334,  331, 
2d3b0 20 34 35 39 2c 20 20 34 35 39 2c 0a 20 2f 2a 20   459,  459,. /* 
2d3b1 20 20 37 33 30 20 2a 2f 20 20 20 38 36 31 2c 20    730 */   861, 
2d3b2 20 33 33 33 2c 20 20 34 33 35 2c 20 20 31 30 34   333,  435,  104
2d3b3 2c 20 20 33 37 36 2c 20 20 34 33 39 2c 20 20 33  ,  376,  439,  3
2d3b4 32 38 2c 20 20 34 31 35 2c 20 20 33 33 31 2c 20  28,  415,  331, 
2d3b5 20 34 31 35 2c 0a 20 2f 2a 20 20 20 37 34 30 20   415,. /*   740 
2d3b6 2a 2f 20 20 20 35 36 35 2c 20 20 33 33 31 2c 20  */   565,  331, 
2d3b7 20 33 32 38 2c 20 20 33 30 36 2c 20 20 35 36 34   328,  306,  564
2d3b8 2c 20 20 34 33 35 2c 20 20 31 30 35 2c 20 20 31  ,  435,  105,  1
2d3b9 38 35 2c 20 20 32 36 35 2c 20 20 34 34 36 2c 0a  85,  265,  446,.
2d3ba 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20 34   /*   750 */   4
2d3bb 35 32 2c 20 20 34 33 35 2c 20 20 31 32 36 2c 20  52,  435,  126, 
2d3bc 20 33 32 38 2c 20 20 35 37 30 2c 20 20 35 31 38   328,  570,  518
2d3bd 2c 20 20 33 32 38 2c 20 20 33 33 34 2c 20 20 33  ,  328,  334,  3
2d3be 37 37 2c 20 20 34 35 39 2c 0a 20 2f 2a 20 20 20  77,  459,. /*   
2d3bf 37 36 30 20 2a 2f 20 20 20 34 35 39 2c 20 20 33  760 */   459,  3
2d3c0 31 34 2c 20 20 34 33 35 2c 20 20 31 32 38 2c 20  14,  435,  128, 
2d3c1 20 31 39 34 2c 20 20 34 33 35 2c 20 20 20 35 39   194,  435,   59
2d3c2 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20 33  ,   23,   22,  3
2d3c3 30 38 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f  08,. /*   770 */
2d3c4 20 20 20 34 35 36 2c 20 20 34 35 37 2c 20 20 34     456,  457,  4
2d3c5 35 33 2c 20 20 34 35 33 2c 20 20 20 32 35 2c 20  53,  453,   25, 
2d3c6 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34    25,   24,   24
2d3c7 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f  ,   24,   24,. /
2d3c8 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 33 33 31  *   780 */   331
2d3c9 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2d3ca 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20  26,   26,   27, 
2d3cb 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38    27,   28,   28
2d3cc 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 37 39  ,   28,. /*   79
2d3cd 30 20 2a 2f 20 20 20 20 32 39 2c 20 20 32 31 36  0 */    29,  216
2d3ce 2c 20 20 33 30 34 2c 20 20 33 33 31 2c 20 20 31  ,  304,  331,  1
2d3cf 33 36 2c 20 20 33 32 38 2c 20 20 32 34 32 2c 20  36,  328,  242, 
2d3d0 20 34 37 37 2c 20 20 34 33 36 2c 20 20 33 33 31   477,  436,  331
2d3d1 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20 20  ,. /*   800 */  
2d3d2 20 33 35 30 2c 20 20 33 33 31 2c 20 20 36 30 39   350,  331,  609
2d3d3 2c 20 20 33 30 33 2c 20 20 34 33 35 2c 20 20 31  ,  303,  435,  1
2d3d4 30 32 2c 20 20 32 30 31 2c 20 20 31 33 37 2c 20  02,  201,  137, 
2d3d5 20 33 32 38 2c 20 20 34 31 35 2c 0a 20 2f 2a 20   328,  415,. /* 
2d3d6 20 20 38 31 30 20 2a 2f 20 20 20 34 35 34 2c 20    810 */   454, 
2d3d7 20 31 37 38 2c 20 20 33 33 31 2c 20 20 34 37 38   178,  331,  478
2d3d8 2c 20 20 33 32 38 2c 20 20 34 31 35 2c 20 20 33  ,  328,  415,  3
2d3d9 32 38 2c 20 20 34 33 35 2c 20 20 20 37 37 2c 20  28,  435,   77, 
2d3da 20 34 34 30 2c 0a 20 2f 2a 20 20 20 38 32 30 20   440,. /*   820 
2d3db 2a 2f 20 20 20 32 34 39 2c 20 20 34 34 36 2c 20  */   249,  446, 
2d3dc 20 34 35 32 2c 20 20 34 33 35 2c 20 20 31 30 30   452,  435,  100
2d3dd 2c 20 20 34 33 35 2c 20 20 20 36 38 2c 20 20 33  ,  435,   68,  3
2d3de 32 38 2c 20 20 34 37 39 2c 20 20 34 36 35 2c 0a  28,  479,  465,.
2d3df 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20 20 20 33   /*   830 */   3
2d3e0 34 31 2c 20 20 36 31 33 2c 20 20 39 33 31 2c 20  41,  613,  931, 
2d3e1 20 34 38 34 2c 20 20 39 33 31 2c 20 20 34 31 35   484,  931,  415
2d3e2 2c 20 20 34 33 35 2c 20 20 20 39 38 2c 20 20 34  ,  435,   98,  4
2d3e3 36 37 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20  67,   23,. /*   
2d3e4 38 34 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 33  840 */    22,  3
2d3e5 30 38 2c 20 20 34 35 36 2c 20 20 34 35 37 2c 20  08,  456,  457, 
2d3e6 20 34 35 33 2c 20 20 34 35 33 2c 20 20 20 32 35   453,  453,   25
2d3e7 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20  ,   25,   24,   
2d3e8 32 34 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a 2f  24,. /*   850 */
2d3e9 20 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 33      24,   24,  3
2d3ea 33 31 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  31,   26,   26, 
2d3eb 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37    26,   26,   27
2d3ec 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 0a 20 2f  ,   27,   28,. /
2d3ed 2a 20 20 20 38 36 30 20 2a 2f 20 20 20 20 32 38  *   860 */    28
2d3ee 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32  ,   28,   29,  2
2d3ef 31 36 2c 20 20 33 30 34 2c 20 20 33 33 31 2c 20  16,  304,  331, 
2d3f0 20 33 39 37 2c 20 20 33 32 38 2c 20 20 31 36 34   397,  328,  164
2d3f1 2c 20 20 32 36 34 2c 0a 20 2f 2a 20 20 20 38 37  ,  264,. /*   87
2d3f2 30 20 2a 2f 20 20 20 32 30 35 2c 20 20 33 33 31  0 */   205,  331
2d3f3 2c 20 20 32 36 34 2c 20 20 33 33 32 2c 20 20 36  ,  264,  332,  6
2d3f4 31 30 2c 20 20 33 33 39 2c 20 20 34 33 35 2c 20  10,  339,  435, 
2d3f5 20 31 32 39 2c 20 20 34 30 37 2c 20 20 20 20 32   129,  407,    2
2d3f6 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20 20  ,. /*   880 */  
2d3f7 20 33 32 38 2c 20 20 33 32 32 2c 20 20 31 37 35   328,  322,  175
2d3f8 2c 20 20 33 33 31 2c 20 20 34 31 35 2c 20 20 32  ,  331,  415,  2
2d3f9 31 34 2c 20 20 33 32 38 2c 20 20 34 31 35 2c 20  14,  328,  415, 
2d3fa 20 34 31 35 2c 20 20 34 33 35 2c 0a 20 2f 2a 20   415,  435,. /* 
2d3fb 20 20 38 39 30 20 2a 2f 20 20 20 31 33 30 2c 20    890 */   130, 
2d3fc 20 34 36 36 2c 20 20 34 36 36 2c 20 20 34 34 36   466,  466,  446
2d3fd 2c 20 20 34 35 32 2c 20 20 34 33 35 2c 20 20 31  ,  452,  435,  1
2d3fe 33 31 2c 20 20 33 39 36 2c 20 20 33 32 38 2c 20  31,  396,  328, 
2d3ff 20 32 35 37 2c 0a 20 2f 2a 20 20 20 39 30 30 20   257,. /*   900 
2d400 2a 2f 20 20 20 33 33 34 2c 20 20 34 38 37 2c 20  */   334,  487, 
2d401 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34 33 36   459,  459,  436
2d402 2c 20 20 31 35 34 2c 20 20 32 32 39 2c 20 20 34  ,  154,  229,  4
2d403 33 35 2c 20 20 20 36 39 2c 20 20 33 31 35 2c 0a  35,   69,  315,.
2d404 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20 32   /*   910 */   2
2d405 35 38 2c 20 20 20 32 33 2c 20 20 20 33 33 2c 20  58,   23,   33, 
2d406 20 33 30 38 2c 20 20 34 35 36 2c 20 20 34 35 37   308,  456,  457
2d407 2c 20 20 34 35 33 2c 20 20 34 35 33 2c 20 20 20  ,  453,  453,   
2d408 32 35 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20  25,   25,. /*   
2d409 39 32 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 20  920 */    24,   
2d40a 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20  24,   24,   24, 
2d40b 20 33 33 31 2c 20 20 20 32 36 2c 20 20 20 32 36   331,   26,   26
2d40c 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2d40d 32 37 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f  27,. /*   930 */
2d40e 20 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20      27,   28,   
2d40f 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20  28,   28,   29, 
2d410 20 32 31 36 2c 20 20 33 30 34 2c 20 20 33 33 31   216,  304,  331
2d411 2c 20 20 34 39 37 2c 20 20 33 32 38 2c 0a 20 2f  ,  497,  328,. /
2d412 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 31 35 31  *   940 */   151
2d413 2c 20 20 32 36 34 2c 20 20 34 31 32 2c 20 20 33  ,  264,  412,  3
2d414 33 31 2c 20 20 32 36 34 2c 20 20 34 37 30 2c 20  31,  264,  470, 
2d415 20 33 33 37 2c 20 20 32 30 30 2c 20 20 34 33 35   337,  200,  435
2d416 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 39 35  ,   80,. /*   95
2d417 30 20 2a 2f 20 20 20 32 35 30 2c 20 20 31 35 35  0 */   250,  155
2d418 2c 20 20 33 32 38 2c 20 20 35 32 33 2c 20 20 35  ,  328,  523,  5
2d419 32 34 2c 20 20 33 33 31 2c 20 20 34 31 35 2c 20  24,  331,  415, 
2d41a 20 34 31 35 2c 20 20 33 32 38 2c 20 20 34 31 35   415,  328,  415
2d41b 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20 20  ,. /*   960 */  
2d41c 20 33 30 36 2c 20 20 34 33 35 2c 20 20 20 38 31   306,  435,   81
2d41d 2c 20 20 35 33 33 2c 20 20 35 33 32 2c 20 20 34  ,  533,  532,  4
2d41e 34 36 2c 20 20 34 35 32 2c 20 20 34 33 35 2c 20  46,  452,  435, 
2d41f 20 20 37 30 2c 20 20 20 34 37 2c 0a 20 2f 2a 20    70,   47,. /* 
2d420 20 20 39 37 30 20 2a 2f 20 20 20 33 32 38 2c 20    970 */   328, 
2d421 20 36 31 33 2c 20 20 39 33 30 2c 20 20 32 35 39   613,  930,  259
2d422 2c 20 20 39 33 30 2c 20 20 34 31 38 2c 20 20 34  ,  930,  418,  4
2d423 31 39 2c 20 20 34 32 30 2c 20 20 33 31 36 2c 20  19,  420,  316, 
2d424 20 34 33 35 2c 0a 20 2f 2a 20 20 20 39 38 30 20   435,. /*   980 
2d425 2a 2f 20 20 20 20 38 32 2c 20 20 33 31 37 2c 20  */    82,  317, 
2d426 20 32 30 36 2c 20 20 35 33 39 2c 20 20 20 32 32   206,  539,   22
2d427 2c 20 20 33 30 38 2c 20 20 34 35 36 2c 20 20 34  ,  308,  456,  4
2d428 35 37 2c 20 20 34 35 33 2c 20 20 34 35 33 2c 0a  57,  453,  453,.
2d429 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20 20 20 20   /*   990 */    
2d42a 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20  25,   25,   24, 
2d42b 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34    24,   24,   24
2d42c 2c 20 20 33 33 31 2c 20 20 20 32 36 2c 20 20 20  ,  331,   26,   
2d42d 32 36 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20 31  26,   26,. /*  1
2d42e 30 30 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20  000 */    26,   
2d42f 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20  27,   27,   28, 
2d430 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39    28,   28,   29
2d431 2c 20 20 32 31 36 2c 20 20 33 30 34 2c 20 20 33  ,  216,  304,  3
2d432 33 31 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a 2f  31,. /*  1010 */
2d433 20 20 20 32 30 39 2c 20 20 33 32 38 2c 20 20 35     209,  328,  5
2d434 32 39 2c 20 20 35 34 30 2c 20 20 36 31 30 2c 20  29,  540,  610, 
2d435 20 33 33 31 2c 20 20 34 33 36 2c 20 20 35 36 33   331,  436,  563
2d436 2c 20 20 33 37 35 2c 20 20 35 36 33 2c 0a 20 2f  ,  375,  563,. /
2d437 2a 20 20 31 30 32 30 20 2a 2f 20 20 20 34 33 35  *  1020 */   435
2d438 2c 20 20 20 38 33 2c 20 20 33 36 32 2c 20 20 35  ,   83,  362,  5
2d439 33 38 2c 20 20 33 32 38 2c 20 20 31 35 35 2c 20  38,  328,  155, 
2d43a 20 35 34 31 2c 20 20 33 33 31 2c 20 20 34 39 39   541,  331,  499
2d43b 2c 20 20 35 32 36 2c 0a 20 2f 2a 20 20 31 30 33  ,  526,. /*  103
2d43c 30 20 2a 2f 20 20 20 33 32 38 2c 20 20 33 33 31  0 */   328,  331
2d43d 2c 20 20 35 37 35 2c 20 20 34 33 35 2c 20 20 20  ,  575,  435,   
2d43e 38 34 2c 20 20 34 32 34 2c 20 20 35 34 33 2c 20  84,  424,  543, 
2d43f 20 34 34 36 2c 20 20 34 35 32 2c 20 20 34 33 35   446,  452,  435
2d440 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20 20  ,. /*  1040 */  
2d441 20 20 38 36 2c 20 20 32 39 30 2c 20 20 33 32 38    86,  290,  328
2d442 2c 20 20 34 31 35 2c 20 20 34 33 36 2c 20 20 32  ,  415,  436,  2
2d443 36 37 2c 20 20 33 32 38 2c 20 20 31 35 35 2c 20  67,  328,  155, 
2d444 20 33 39 34 2c 20 20 31 34 31 2c 0a 20 2f 2a 20   394,  141,. /* 
2d445 20 31 30 35 30 20 2a 2f 20 20 20 34 31 35 2c 20   1050 */   415, 
2d446 20 34 33 35 2c 20 20 20 38 37 2c 20 20 35 38 38   435,   87,  588
2d447 2c 20 20 34 31 31 2c 20 20 34 33 35 2c 20 20 20  ,  411,  435,   
2d448 38 38 2c 20 20 33 30 38 2c 20 20 34 35 36 2c 20  88,  308,  456, 
2d449 20 34 35 37 2c 0a 20 2f 2a 20 20 31 30 36 30 20   457,. /*  1060 
2d44a 2a 2f 20 20 20 34 35 33 2c 20 20 34 35 33 2c 20  */   453,  453, 
2d44b 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34    25,   25,   24
2d44c 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20  ,   24,   24,   
2d44d 32 34 2c 20 20 33 38 36 2c 20 20 20 32 36 2c 0a  24,  386,   26,.
2d44e 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20 20   /*  1070 */    
2d44f 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  26,   26,   26, 
2d450 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38    27,   27,   28
2d451 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20  ,   28,   28,   
2d452 32 39 2c 20 20 32 31 36 2c 0a 20 2f 2a 20 20 31  29,  216,. /*  1
2d453 30 38 30 20 2a 2f 20 20 20 20 33 35 2c 20 20 33  080 */    35,  3
2d454 33 38 2c 20 20 32 38 36 2c 20 20 20 20 33 2c 20  38,  286,    3, 
2d455 20 33 33 31 2c 20 20 32 37 30 2c 20 20 33 33 31   331,  270,  331
2d456 2c 20 20 33 32 37 2c 20 20 34 31 34 2c 20 20 34  ,  327,  414,  4
2d457 32 31 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f  21,. /*  1090 */
2d458 20 20 20 33 38 32 2c 20 20 33 31 38 2c 20 20 32     382,  318,  2
2d459 37 36 2c 20 20 34 32 32 2c 20 20 33 32 35 2c 20  76,  422,  325, 
2d45a 20 20 33 35 2c 20 20 33 33 38 2c 20 20 33 33 35    35,  338,  335
2d45b 2c 20 20 20 20 33 2c 20 20 33 32 38 2c 0a 20 2f  ,    3,  328,. /
2d45c 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 34 32 33  *  1100 */   423
2d45d 2c 20 20 33 32 38 2c 20 20 33 32 37 2c 20 20 34  ,  328,  327,  4
2d45e 31 34 2c 20 20 31 34 32 2c 20 20 31 34 34 2c 20  14,  142,  144, 
2d45f 20 32 37 36 2c 20 20 34 31 35 2c 20 20 34 33 35   276,  415,  435
2d460 2c 20 20 20 37 33 2c 0a 20 2f 2a 20 20 31 31 31  ,   73,. /*  111
2d461 30 20 2a 2f 20 20 20 34 33 35 2c 20 20 20 37 34  0 */   435,   74
2d462 2c 20 20 33 33 35 2c 20 20 33 33 31 2c 20 20 20  ,  335,  331,   
2d463 20 36 2c 20 20 33 34 30 2c 20 20 34 32 35 2c 20   6,  340,  425, 
2d464 20 33 33 31 2c 20 20 33 32 36 2c 20 20 33 33 31   331,  326,  331
2d465 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20 20  ,. /*  1120 */  
2d466 20 33 36 37 2c 20 20 34 31 35 2c 20 20 31 35 35   367,  415,  155
2d467 2c 20 20 34 33 37 2c 20 20 32 38 39 2c 20 20 34  ,  437,  289,  4
2d468 37 32 2c 20 20 32 38 37 2c 20 20 32 37 34 2c 20  72,  287,  274, 
2d469 20 33 32 38 2c 20 20 32 37 32 2c 0a 20 2f 2a 20   328,  272,. /* 
2d46a 20 31 31 33 30 20 2a 2f 20 20 20 33 34 30 2c 20   1130 */   340, 
2d46b 20 34 31 35 2c 20 20 33 32 38 2c 20 20 20 34 37   415,  328,   47
2d46c 2c 20 20 33 32 38 2c 20 20 32 37 37 2c 20 20 32  ,  328,  277,  2
2d46d 37 36 2c 20 20 34 33 35 2c 20 20 20 38 39 2c 20  76,  435,   89, 
2d46e 20 33 34 38 2c 0a 20 2f 2a 20 20 31 31 34 30 20   348,. /*  1140 
2d46f 2a 2f 20 20 20 34 37 32 2c 20 20 34 33 35 2c 20  */   472,  435, 
2d470 20 20 39 30 2c 20 20 34 33 35 2c 20 20 20 39 31    90,  435,   91
2d471 2c 20 20 20 33 38 2c 20 20 20 33 37 2c 20 20 32  ,   38,   37,  2
2d472 34 33 2c 20 20 33 33 31 2c 20 20 35 38 32 2c 0a  43,  331,  582,.
2d473 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20 20 20 32   /*  1150 */   2
2d474 34 34 2c 20 20 34 31 35 2c 20 20 34 32 36 2c 20  44,  415,  426, 
2d475 20 32 37 36 2c 20 20 20 33 36 2c 20 20 33 32 39   276,   36,  329
2d476 2c 20 20 33 33 30 2c 20 20 20 34 36 2c 20 20 32  ,  330,   46,  2
2d477 34 35 2c 20 20 34 34 31 2c 0a 20 2f 2a 20 20 31  45,  441,. /*  1
2d478 31 36 30 20 2a 2f 20 20 20 20 33 38 2c 20 20 20  160 */    38,   
2d479 33 37 2c 20 20 35 30 35 2c 20 20 33 32 38 2c 20  37,  505,  328, 
2d47a 20 32 30 32 2c 20 20 32 30 33 2c 20 20 32 30 34   202,  203,  204
2d47b 2c 20 20 34 31 35 2c 20 20 34 31 35 2c 20 20 20  ,  415,  415,   
2d47c 33 36 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a 2f  36,. /*  1170 */
2d47d 20 20 20 33 32 39 2c 20 20 33 33 30 2c 20 20 34     329,  330,  4
2d47e 33 35 2c 20 20 20 39 32 2c 20 20 34 34 31 2c 20  35,   92,  441, 
2d47f 20 31 39 38 2c 20 20 35 36 38 2c 20 20 32 31 34   198,  568,  214
2d480 2c 20 20 31 35 35 2c 20 20 35 38 34 2c 0a 20 2f  ,  155,  584,. /
2d481 2a 20 20 31 31 38 30 20 2a 2f 20 20 20 32 33 35  *  1180 */   235
2d482 2c 20 20 32 33 36 2c 20 20 32 33 37 2c 20 20 31  ,  236,  237,  1
2d483 34 33 2c 20 20 32 33 39 2c 20 20 33 34 36 2c 20  43,  239,  346, 
2d484 20 31 33 33 2c 20 20 35 38 31 2c 20 20 34 33 38   133,  581,  438
2d485 2c 20 20 32 34 36 2c 0a 20 2f 2a 20 20 31 31 39  ,  246,. /*  119
2d486 30 20 2a 2f 20 20 20 34 34 33 2c 20 20 34 34 33  0 */   443,  443
2d487 2c 20 20 34 34 33 2c 20 20 34 34 34 2c 20 20 34  ,  443,  444,  4
2d488 34 35 2c 20 20 20 31 30 2c 20 20 35 38 35 2c 20  45,   10,  585, 
2d489 20 32 37 36 2c 20 20 20 32 30 2c 20 20 20 34 32   276,   20,   42
2d48a 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20 20  ,. /*  1200 */  
2d48b 20 31 37 32 2c 20 20 34 31 35 2c 20 20 32 39 34   172,  415,  294
2d48c 2c 20 20 33 33 31 2c 20 20 32 38 38 2c 20 20 34  ,  331,  288,  4
2d48d 34 33 2c 20 20 34 34 33 2c 20 20 34 34 33 2c 20  43,  443,  443, 
2d48e 20 34 34 34 2c 20 20 34 34 35 2c 0a 20 2f 2a 20   444,  445,. /* 
2d48f 20 31 32 31 30 20 2a 2f 20 20 20 20 31 30 2c 20   1210 */    10, 
2d490 20 32 39 35 2c 20 20 34 31 35 2c 20 20 20 33 35   295,  415,   35
2d491 2c 20 20 33 33 38 2c 20 20 32 31 39 2c 20 20 20  ,  338,  219,   
2d492 20 33 2c 20 20 31 34 39 2c 20 20 33 32 38 2c 20   3,  149,  328, 
2d493 20 34 38 32 2c 0a 20 2f 2a 20 20 31 32 32 30 20   482,. /*  1220 
2d494 2a 2f 20 20 20 33 32 37 2c 20 20 34 31 34 2c 20  */   327,  414, 
2d495 20 33 33 31 2c 20 20 31 37 30 2c 20 20 32 37 36   331,  170,  276
2d496 2c 20 20 35 37 32 2c 20 20 20 34 38 2c 20 20 34  ,  572,   48,  4
2d497 33 35 2c 20 20 20 37 35 2c 20 20 31 36 39 2c 0a  35,   75,  169,.
2d498 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20 33   /*  1230 */   3
2d499 33 35 2c 20 20 20 31 39 2c 20 20 31 37 31 2c 20  35,   19,  171, 
2d49a 20 32 35 31 2c 20 20 34 34 32 2c 20 20 34 31 33   251,  442,  413
2d49b 2c 20 20 34 31 34 2c 20 20 33 32 38 2c 20 20 33  ,  414,  328,  3
2d49c 33 31 2c 20 20 34 31 35 2c 0a 20 2f 2a 20 20 31  31,  415,. /*  1
2d49d 32 34 30 20 2a 2f 20 20 20 35 38 36 2c 20 20 33  240 */   586,  3
2d49e 34 33 2c 20 20 32 37 36 2c 20 20 31 37 37 2c 20  43,  276,  177, 
2d49f 20 33 35 31 2c 20 20 34 39 36 2c 20 20 34 33 35   351,  496,  435
2d4a0 2c 20 20 20 31 37 2c 20 20 33 34 30 2c 20 20 34  ,   17,  340,  4
2d4a1 31 35 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f  15,. /*  1250 */
2d4a2 20 20 20 34 38 31 2c 20 20 32 35 33 2c 20 20 32     481,  253,  2
2d4a3 35 35 2c 20 20 33 32 38 2c 20 20 32 37 36 2c 20  55,  328,  276, 
2d4a4 20 35 30 32 2c 20 20 34 31 35 2c 20 20 34 31 35   502,  415,  415
2d4a5 2c 20 20 34 37 32 2c 20 20 33 33 31 2c 0a 20 2f  ,  472,  331,. /
2d4a6 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 35 30 33  *  1260 */   503
2d4a7 2c 20 20 33 35 37 2c 20 20 34 33 35 2c 20 20 20  ,  357,  435,   
2d4a8 39 34 2c 20 20 35 37 36 2c 20 20 34 31 35 2c 20  94,  576,  415, 
2d4a9 20 31 35 31 2c 20 20 32 33 31 2c 20 20 33 31 32   151,  231,  312
2d4aa 2c 20 20 34 31 35 2c 0a 20 2f 2a 20 20 31 32 37  ,  415,. /*  127
2d4ab 30 20 2a 2f 20 20 20 35 37 37 2c 20 20 35 31 36  0 */   577,  516
2d4ac 2c 20 20 20 35 34 2c 20 20 34 37 32 2c 20 20 33  ,   54,  472,  3
2d4ad 32 38 2c 20 20 33 39 33 2c 20 20 32 39 31 2c 20  28,  393,  291, 
2d4ae 20 32 38 31 2c 20 20 20 33 38 2c 20 20 20 33 37   281,   38,   37
2d4af 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20 20  ,. /*  1280 */  
2d4b0 20 34 39 34 2c 20 20 33 30 35 2c 20 20 35 32 31   494,  305,  521
2d4b1 2c 20 20 34 33 35 2c 20 20 20 39 35 2c 20 20 32  ,  435,   95,  2
2d4b2 33 32 2c 20 20 32 31 34 2c 20 20 20 33 36 2c 20  32,  214,   36, 
2d4b3 20 33 32 39 2c 20 20 33 33 30 2c 0a 20 2f 2a 20   329,  330,. /* 
2d4b4 20 31 32 39 30 20 2a 2f 20 20 20 35 32 36 2c 20   1290 */   526, 
2d4b5 20 34 39 38 2c 20 20 34 34 31 2c 20 20 31 38 38   498,  441,  188
2d4b6 2c 20 20 31 38 39 2c 20 20 34 31 35 2c 20 20 35  ,  189,  415,  5
2d4b7 30 30 2c 20 20 32 39 32 2c 20 20 35 32 32 2c 20  00,  292,  522, 
2d4b8 20 32 36 32 2c 0a 20 2f 2a 20 20 31 33 30 30 20   262,. /*  1300 
2d4b9 2a 2f 20 20 20 35 33 30 2c 20 20 32 36 30 2c 20  */   530,  260, 
2d4ba 20 32 36 33 2c 20 20 35 31 33 2c 20 20 35 34 39   263,  513,  549
2d4bb 2c 20 20 32 36 39 2c 20 20 34 31 35 2c 20 20 34  ,  269,  415,  4
2d4bc 34 31 2c 20 20 35 38 39 2c 20 20 34 30 30 2c 0a  41,  589,  400,.
2d4bd 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20 20   /*  1310 */    
2d4be 35 34 2c 20 20 34 31 35 2c 20 20 35 32 35 2c 20  54,  415,  525, 
2d4bf 20 35 32 37 2c 20 20 34 31 35 2c 20 20 34 31 35   527,  415,  415
2d4c0 2c 20 20 32 37 31 2c 20 20 34 31 35 2c 20 20 32  ,  271,  415,  2
2d4c1 37 33 2c 20 20 34 31 35 2c 0a 20 2f 2a 20 20 31  73,  415,. /*  1
2d4c2 33 32 30 20 2a 2f 20 20 20 34 31 35 2c 20 20 32  320 */   415,  2
2d4c3 37 35 2c 20 20 32 38 30 2c 20 20 34 34 33 2c 20  75,  280,  443, 
2d4c4 20 34 34 33 2c 20 20 34 34 33 2c 20 20 34 34 34   443,  443,  444
2d4c5 2c 20 20 34 34 35 2c 20 20 20 31 30 2c 20 20 31  ,  445,   10,  1
2d4c6 30 37 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a 2f  07,. /*  1330 */
2d4c7 20 20 20 33 38 30 2c 20 20 34 31 35 2c 20 20 33     380,  415,  3
2d4c8 38 33 2c 20 20 34 31 35 2c 20 20 33 38 34 2c 20  83,  415,  384, 
2d4c9 20 32 38 33 2c 20 20 34 31 35 2c 20 20 34 31 35   283,  415,  415
2d4ca 2c 20 20 34 34 33 2c 20 20 34 34 33 2c 0a 20 2f  ,  443,  443,. /
2d4cb 2a 20 20 31 33 34 30 20 2a 2f 20 20 20 34 34 33  *  1340 */   443
2d4cc 2c 20 20 32 38 34 2c 20 20 32 38 35 2c 20 20 35  ,  284,  285,  5
2d4cd 38 30 2c 20 20 33 30 30 2c 20 20 34 31 35 2c 20  80,  300,  415, 
2d4ce 20 35 39 31 2c 20 20 34 31 35 2c 20 20 32 39 33   591,  415,  293
2d4cf 2c 20 20 34 31 35 2c 0a 20 2f 2a 20 20 31 33 35  ,  415,. /*  135
2d4d0 30 20 2a 2f 20 20 20 34 31 35 2c 20 20 32 39 36  0 */   415,  296
2d4d1 2c 20 20 32 39 37 2c 20 20 36 30 35 2c 20 20 32  ,  297,  605,  2
2d4d2 32 36 2c 20 20 35 35 30 2c 20 20 34 31 35 2c 20  26,  550,  415, 
2d4d3 20 34 31 35 2c 20 20 34 31 35 2c 20 20 32 32 35   415,  415,  225
2d4d4 2c 0a 20 2f 2a 20 20 31 33 36 30 20 2a 2f 20 20  ,. /*  1360 */  
2d4d5 20 36 30 38 2c 20 20 34 31 35 2c 20 20 33 30 32   608,  415,  302
2d4d6 2c 20 20 34 31 35 2c 20 20 35 35 31 2c 20 20 32  ,  415,  551,  2
2d4d7 32 37 2c 20 20 34 31 35 2c 20 20 34 31 35 2c 20  27,  415,  415, 
2d4d8 20 34 31 35 2c 20 20 33 30 31 2c 0a 20 2f 2a 20   415,  301,. /* 
2d4d9 20 31 33 37 30 20 2a 2f 20 20 20 35 34 34 2c 20   1370 */   544, 
2d4da 20 35 35 32 2c 20 20 33 36 39 2c 20 20 31 35 38   552,  369,  158
2d4db 2c 20 20 33 37 33 2c 20 20 35 35 38 2c 20 20 31  ,  373,  558,  1
2d4dc 35 39 2c 20 20 32 37 38 2c 20 20 33 37 31 2c 20  59,  278,  371, 
2d4dd 20 31 36 30 2c 0a 20 2f 2a 20 20 31 33 38 30 20   160,. /*  1380 
2d4de 2a 2f 20 20 20 20 35 31 2c 20 20 32 30 37 2c 20  */    51,  207, 
2d4df 20 35 36 30 2c 20 20 35 36 31 2c 20 20 31 36 31   560,  561,  161
2d4e0 2c 20 20 31 34 30 2c 20 20 33 37 39 2c 20 20 31  ,  140,  379,  1
2d4e1 31 37 2c 20 20 35 37 31 2c 20 20 31 36 33 2c 0a  17,  571,  163,.
2d4e2 20 2f 2a 20 20 31 33 39 30 20 2a 2f 20 20 20 33   /*  1390 */   3
2d4e3 39 31 2c 20 20 33 39 32 2c 20 20 31 38 31 2c 20  91,  392,  181, 
2d4e4 20 31 38 30 2c 20 20 33 32 31 2c 20 20 36 30 32   180,  321,  602
2d4e5 2c 20 20 35 37 38 2c 20 20 31 31 38 2c 20 20 31  ,  578,  118,  1
2d4e6 31 39 2c 20 20 31 32 30 2c 0a 20 2f 2a 20 20 31  19,  120,. /*  1
2d4e7 34 30 30 20 2a 2f 20 20 20 31 32 31 2c 20 20 31  400 */   121,  1
2d4e8 32 33 2c 20 20 20 35 35 2c 20 20 35 38 37 2c 20  23,   55,  587, 
2d4e9 20 20 35 38 2c 20 20 36 30 33 2c 20 20 36 30 34    58,  603,  604
2d4ea 2c 20 20 36 30 37 2c 20 20 20 36 32 2c 20 20 31  ,  607,   62,  1
2d4eb 37 34 2c 0a 20 2f 2a 20 20 31 34 31 30 20 2a 2f  74,. /*  1410 */
2d4ec 20 20 20 31 30 33 2c 20 20 32 32 34 2c 20 20 31     103,  224,  1
2d4ed 31 31 2c 20 20 34 30 39 2c 20 20 32 33 38 2c 20  11,  409,  238, 
2d4ee 20 34 32 38 2c 20 20 31 39 39 2c 20 20 33 32 33   428,  199,  323
2d4ef 2c 20 20 36 35 37 2c 20 20 36 35 38 2c 0a 20 2f  ,  657,  658,. /
2d4f0 2a 20 20 31 34 32 30 20 2a 2f 20 20 20 36 35 39  *  1420 */   659
2d4f1 2c 20 20 31 34 36 2c 20 20 31 34 37 2c 20 20 34  ,  146,  147,  4
2d4f2 35 35 2c 20 20 34 35 38 2c 20 20 20 33 34 2c 20  55,  458,   34, 
2d4f3 20 34 37 34 2c 20 20 34 36 32 2c 20 20 34 37 31   474,  462,  471
2d4f4 2c 20 20 31 38 32 2c 0a 20 2f 2a 20 20 31 34 33  ,  182,. /*  143
2d4f5 30 20 2a 2f 20 20 20 31 39 35 2c 20 20 31 34 38  0 */   195,  148
2d4f6 2c 20 20 34 37 35 2c 20 20 34 37 36 2c 20 20 34  ,  475,  476,  4
2d4f7 38 30 2c 20 20 20 20 35 2c 20 20 20 31 32 2c 20  80,    5,   12, 
2d4f8 20 34 39 33 2c 20 20 20 34 34 2c 20 20 20 31 31   493,   44,   11
2d4f9 2c 0a 20 2f 2a 20 20 31 34 34 30 20 2a 2f 20 20  ,. /*  1440 */  
2d4fa 20 31 30 36 2c 20 20 31 33 38 2c 20 20 35 30 39   106,  138,  509
2d4fb 2c 20 20 35 31 30 2c 20 20 35 30 31 2c 20 20 32  ,  510,  501,  2
2d4fc 32 33 2c 20 20 20 34 39 2c 20 20 33 36 31 2c 20  23,   49,  361, 
2d4fd 20 31 30 38 2c 20 20 31 30 39 2c 0a 20 2f 2a 20   108,  109,. /* 
2d4fe 20 31 34 35 30 20 2a 2f 20 20 20 31 35 32 2c 20   1450 */   152, 
2d4ff 20 32 36 36 2c 20 20 20 35 30 2c 20 20 31 31 30   266,   50,  110
2d500 2c 20 20 31 35 37 2c 20 20 32 35 38 2c 20 20 33  ,  157,  258,  3
2d501 37 30 2c 20 20 31 38 34 2c 20 20 35 35 39 2c 20  70,  184,  559, 
2d502 20 31 33 39 2c 0a 20 2f 2a 20 20 31 34 36 30 20   139,. /*  1460 
2d503 2a 2f 20 20 20 31 35 31 2c 20 20 31 31 33 2c 20  */   151,  113, 
2d504 20 32 37 39 2c 20 20 31 36 32 2c 20 20 31 31 35   279,  162,  115
2d505 2c 20 20 33 37 34 2c 20 20 20 31 35 2c 20 20 35  ,  374,   15,  5
2d506 37 34 2c 20 20 31 31 36 2c 20 20 31 36 35 2c 0a  74,  116,  165,.
2d507 20 2f 2a 20 20 31 34 37 30 20 2a 2f 20 20 20 20   /*  1470 */    
2d508 35 32 2c 20 20 20 31 33 2c 20 20 33 36 36 2c 20  52,   13,  366, 
2d509 20 35 37 39 2c 20 20 20 35 33 2c 20 20 31 36 37   579,   53,  167
2d50a 2c 20 20 31 36 38 2c 20 20 31 36 36 2c 20 20 35  ,  168,  166,  5
2d50b 38 33 2c 20 20 31 32 34 2c 0a 20 2f 2a 20 20 31  83,  124,. /*  1
2d50c 34 38 30 20 2a 2f 20 20 20 31 31 34 2c 20 20 31  480 */   114,  1
2d50d 32 32 2c 20 20 35 36 32 2c 20 20 35 36 36 2c 20  22,  562,  566, 
2d50e 20 20 31 34 2c 20 20 20 36 31 2c 20 20 35 39 39    14,   61,  599
2d50f 2c 20 20 36 30 30 2c 20 20 31 32 35 2c 20 20 31  ,  600,  125,  1
2d510 37 33 2c 0a 20 2f 2a 20 20 31 34 39 30 20 2a 2f  73,. /*  1490 */
2d511 20 20 20 32 39 38 2c 20 20 35 39 30 2c 20 20 31     298,  590,  1
2d512 38 37 2c 20 20 34 30 36 2c 20 20 39 34 31 2c 20  87,  406,  941, 
2d513 20 36 31 34 2c 20 20 39 34 31 2c 20 20 34 30 32   614,  941,  402
2d514 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73  ,.};.static cons
2d515 74 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 5f  t YYCODETYPE yy_
2d516 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 3d 20 7b 0a  lookahead[] = {.
2d517 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 20   /*     0 */    
2d518 31 39 2c 20 20 31 34 32 2c 20 20 31 34 33 2c 20  19,  142,  143, 
2d519 20 31 34 34 2c 20 20 31 34 35 2c 20 20 20 32 34   144,  145,   24
2d51a 2c 20 20 31 31 36 2c 20 20 20 32 36 2c 20 20 20  ,  116,   26,   
2d51b 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20  75,   76,. /*   
2d51c 20 31 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20   10 */    77,   
2d51d 37 38 2c 20 20 20 32 35 2c 20 20 20 38 30 2c 20  78,   25,   80, 
2d51e 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
2d51f 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
2d520 38 36 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f  86,. /*    20 */
2d521 20 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20      87,   88,   
2d522 38 39 2c 20 20 20 39 30 2c 20 20 20 32 36 2c 20  89,   90,   26, 
2d523 20 20 32 37 2c 20 20 31 36 30 2c 20 20 20 32 36    27,  160,   26
2d524 2c 20 20 20 32 37 2c 20 20 20 34 38 2c 0a 20 2f  ,   27,   48,. /
2d525 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 20 34 39  *    30 */    49
2d526 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20  ,   79,   80,   
2d527 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  81,   82,   83, 
2d528 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
2d529 2c 20 20 20 38 37 2c 0a 20 2f 2a 20 20 20 20 34  ,   87,. /*    4
2d52a 30 20 2a 2f 20 20 20 20 38 38 2c 20 20 20 38 39  0 */    88,   89
2d52b 2c 20 20 20 39 30 2c 20 20 32 32 32 2c 20 20 32  ,   90,  222,  2
2d52c 32 33 2c 20 20 32 32 34 2c 20 20 32 32 35 2c 20  23,  224,  225, 
2d52d 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
2d52e 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20  ,. /*    50 */  
2d52f 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
2d530 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
2d531 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
2d532 20 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20    77,   78,. /* 
2d533 20 20 20 36 30 20 2a 2f 20 20 20 31 39 34 2c 20     60 */   194, 
2d534 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32    80,   81,   82
2d535 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
2d536 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
2d537 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20 37 30 20    88,. /*    70 
2d538 2a 2f 20 20 20 20 38 39 2c 20 20 20 39 30 2c 20  */    89,   90, 
2d539 20 20 31 39 2c 20 20 20 39 30 2c 20 20 20 31 39    19,   90,   19
2d53a 2c 20 20 20 39 34 2c 20 20 31 37 34 2c 20 20 20  ,   94,  174,   
2d53b 32 35 2c 20 20 20 32 35 2c 20 20 20 38 30 2c 0a  25,   25,   80,.
2d53c 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20   /*    80 */    
2d53d 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  81,   82,   83, 
2d53e 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
2d53f 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20  ,   87,   88,   
2d540 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20 20  89,   90,. /*   
2d541 20 39 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20   90 */    26,   
2d542 32 37 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20  27,   94,   95, 
2d543 20 20 39 36 2c 20 20 20 39 34 2c 20 20 20 39 35    96,   94,   95
2d544 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31  ,   99,  100,  1
2d545 30 31 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f  01,. /*   100 */
2d546 20 20 20 20 31 39 2c 20 20 20 34 38 2c 20 20 20      19,   48,   
2d547 34 39 2c 20 20 31 35 30 2c 20 20 31 37 34 2c 20  49,  150,  174, 
2d548 20 20 35 32 2c 20 20 31 31 39 2c 20 20 31 36 36    52,  119,  166
2d549 2c 20 20 31 31 30 2c 20 20 20 38 34 2c 0a 20 2f  ,  110,   84,. /
2d54a 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 20 38 35  *   110 */    85
2d54b 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
2d54c 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
2d54d 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 36 35    26,   27,  165
2d54e 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 31 32  ,   66,. /*   12
2d54f 30 20 2a 2f 20 20 20 20 36 37 2c 20 20 20 36 38  0 */    67,   68
2d550 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
2d551 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
2d552 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
2d553 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20  ,. /*   130 */  
2d554 20 20 37 37 2c 20 20 20 37 38 2c 20 20 31 38 36    77,   78,  186
2d555 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
2d556 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
2d557 20 20 38 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20    85,   86,. /* 
2d558 20 20 31 34 30 20 2a 2f 20 20 20 20 38 37 2c 20    140 */    87, 
2d559 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
2d55a 2c 20 20 20 31 39 2c 20 20 20 39 30 2c 20 20 32  ,   19,   90,  2
2d55b 30 35 2c 20 20 20 39 35 2c 20 20 20 38 34 2c 20  05,   95,   84, 
2d55c 20 20 38 35 2c 0a 20 2f 2a 20 20 20 31 35 30 20    85,. /*   150 
2d55d 2a 2f 20 20 20 31 38 36 2c 20 20 20 39 36 2c 20  */   186,   96, 
2d55e 20 20 39 37 2c 20 20 20 39 38 2c 20 20 20 39 39    97,   98,   99
2d55f 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31  ,  100,  101,  1
2d560 30 32 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 0a  02,   94,   95,.
2d561 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 31   /*   160 */   1
2d562 39 35 2c 20 20 20 39 37 2c 20 20 31 35 30 2c 20  95,   97,  150, 
2d563 20 32 32 32 2c 20 20 31 30 39 2c 20 20 32 32 34   222,  109,  224
2d564 2c 20 20 32 32 35 2c 20 20 20 32 36 2c 20 20 31  ,  225,   26,  1
2d565 30 34 2c 20 20 31 30 35 2c 0a 20 2f 2a 20 20 20  04,  105,. /*   
2d566 31 37 30 20 2a 2f 20 20 20 32 31 37 2c 20 20 20  170 */   217,   
2d567 39 30 2c 20 20 31 32 30 2c 20 20 20 34 38 2c 20  90,  120,   48, 
2d568 20 20 34 39 2c 20 20 20 35 30 2c 20 20 20 38 36    49,   50,   86
2d569 2c 20 20 31 36 35 2c 20 20 20 39 37 2c 20 20 20  ,  165,   97,   
2d56a 39 38 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f  98,. /*   180 */
2d56b 20 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31      99,  100,  1
2d56c 30 31 2c 20 20 31 30 32 2c 20 20 20 39 34 2c 20  01,  102,   94, 
2d56d 20 20 39 35 2c 20 20 31 37 34 2c 20 20 31 37 35    95,  174,  175
2d56e 2c 20 20 20 20 31 2c 20 20 20 20 32 2c 0a 20 2f  ,    1,    2,. /
2d56f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 31 30 39  *   190 */   109
2d570 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20  ,   66,   67,   
2d571 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20  68,   69,   70, 
2d572 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33    71,   72,   73
2d573 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 32 30  ,   74,. /*   20
2d574 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36  0 */    75,   76
2d575 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 31  ,   77,   78,  1
2d576 39 31 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20  91,   80,   81, 
2d577 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2d578 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20  ,. /*   210 */  
2d579 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
2d57a 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20  ,   88,   89,   
2d57b 39 30 2c 20 20 20 31 39 2c 20 20 31 31 36 2c 20  90,   19,  116, 
2d57c 20 20 33 35 2c 20 20 31 35 30 2c 0a 20 2f 2a 20    35,  150,. /* 
2d57d 20 20 32 32 30 20 2a 2f 20 20 20 31 35 35 2c 20    220 */   155, 
2d57e 20 20 32 34 2c 20 20 32 30 38 2c 20 20 31 35 30    24,  208,  150
2d57f 2c 20 20 32 32 32 2c 20 20 31 35 30 2c 20 20 32  ,  222,  150,  2
2d580 32 34 2c 20 20 32 32 35 2c 20 20 32 31 36 2c 20  24,  225,  216, 
2d581 20 31 32 38 2c 0a 20 2f 2a 20 20 20 32 33 30 20   128,. /*   230 
2d582 2a 2f 20 20 20 31 36 31 2c 20 20 31 36 32 2c 20  */   161,  162, 
2d583 20 31 35 30 2c 20 20 32 32 31 2c 20 20 31 36 35   150,  221,  165
2d584 2c 20 20 20 39 34 2c 20 20 20 32 33 2c 20 20 31  ,   94,   23,  1
2d585 35 30 2c 20 20 31 36 35 2c 20 20 20 35 36 2c 0a  50,  165,   56,.
2d586 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 31   /*   240 */   1
2d587 36 35 2c 20 20 31 39 37 2c 20 20 31 36 30 2c 20  65,  197,  160, 
2d588 20 31 37 30 2c 20 20 31 37 31 2c 20 20 20 34 38   170,  171,   48
2d589 2c 20 20 20 34 39 2c 20 20 31 36 35 2c 20 20 32  ,   49,  165,  2
2d58a 30 34 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20  04,  174,. /*   
2d58b 32 35 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 20  250 */   175,   
2d58c 32 32 2c 20 20 31 36 35 2c 20 20 20 32 34 2c 20  22,  165,   24, 
2d58d 20 31 38 35 2c 20 20 31 38 36 2c 20 20 31 37 34   185,  186,  174
2d58e 2c 20 20 31 37 35 2c 20 20 31 36 39 2c 20 20 31  ,  175,  169,  1
2d58f 37 30 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f  70,. /*   260 */
2d590 20 20 20 31 37 31 2c 20 20 31 37 34 2c 20 20 31     171,  174,  1
2d591 37 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  75,   66,   67, 
2d592 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30    68,   69,   70
2d593 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 0a 20 2f  ,   71,   72,. /
2d594 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 20 37 33  *   270 */    73
2d595 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
2d596 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
2d597 20 31 39 34 2c 20 20 20 38 30 2c 20 20 20 38 31   194,   80,   81
2d598 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20 20 32 38  ,   82,. /*   28
2d599 30 20 2a 2f 20 20 20 20 38 33 2c 20 20 20 38 34  0 */    83,   84
2d59a 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
2d59b 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
2d59c 20 20 39 30 2c 20 20 20 31 39 2c 20 20 32 31 34    90,   19,  214
2d59d 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20  ,. /*   290 */  
2d59e 20 32 31 35 2c 20 20 31 30 38 2c 20 20 31 35 30   215,  108,  150
2d59f 2c 20 20 20 32 35 2c 20 20 32 32 39 2c 20 20 31  ,   25,  229,  1
2d5a0 35 30 2c 20 20 20 36 34 2c 20 20 31 34 38 2c 20  50,   64,  148, 
2d5a1 20 32 31 36 2c 20 20 32 33 34 2c 0a 20 2f 2a 20   216,  234,. /* 
2d5a2 20 20 33 30 30 20 2a 2f 20 20 20 31 34 36 2c 20    300 */   146, 
2d5a3 20 31 34 37 2c 20 20 32 31 35 2c 20 20 32 32 31   147,  215,  221
2d5a4 2c 20 20 32 33 31 2c 20 20 32 33 32 2c 20 20 31  ,  231,  232,  1
2d5a5 35 32 2c 20 20 31 36 35 2c 20 20 31 35 34 2c 20  52,  165,  154, 
2d5a6 20 31 35 30 2c 0a 20 2f 2a 20 20 20 33 31 30 20   150,. /*   310 
2d5a7 2a 2f 20 20 20 31 36 35 2c 20 20 31 39 36 2c 20  */   165,  196, 
2d5a8 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31 36 30   170,  171,  160
2d5a9 2c 20 20 31 38 31 2c 20 20 31 38 32 2c 20 20 20  ,  181,  182,   
2d5aa 34 38 2c 20 20 20 34 39 2c 20 20 31 37 34 2c 0a  48,   49,  174,.
2d5ab 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 31   /*   320 */   1
2d5ac 37 35 2c 20 20 32 33 32 2c 20 20 31 38 38 2c 20  75,  232,  188, 
2d5ad 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 31 32   165,  165,  112
2d5ae 2c 20 20 20 39 34 2c 20 20 31 31 34 2c 20 20 31  ,   94,  114,  1
2d5af 31 35 2c 20 20 31 36 36 2c 0a 20 2f 2a 20 20 20  15,  166,. /*   
2d5b0 33 33 30 20 2a 2f 20 20 20 20 39 38 2c 20 20 20  330 */    98,   
2d5b1 35 35 2c 20 20 31 37 34 2c 20 20 31 37 34 2c 20  55,  174,  174, 
2d5b2 20 31 37 35 2c 20 20 20 36 36 2c 20 20 20 36 37   175,   66,   67
2d5b3 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
2d5b4 37 30 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f  70,. /*   340 */
2d5b5 20 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20      71,   72,   
2d5b6 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20  73,   74,   75, 
2d5b7 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
2d5b8 2c 20 20 31 39 34 2c 20 20 20 38 30 2c 0a 20 2f  ,  194,   80,. /
2d5b9 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 20 38 31  *   350 */    81
2d5ba 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
2d5bb 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20  84,   85,   86, 
2d5bc 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
2d5bd 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20 20 33 36  ,   90,. /*   36
2d5be 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 31 32 39  0 */    19,  129
2d5bf 2c 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 20  ,  130,  131,   
2d5c0 39 36 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  96,   84,   85, 
2d5c1 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31    99,  100,  101
2d5c2 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20  ,. /*   370 */  
2d5c3 20 31 35 30 2c 20 20 32 32 36 2c 20 20 32 31 38   150,  226,  218
2d5c4 2c 20 20 32 33 31 2c 20 20 32 33 32 2c 20 20 32  ,  231,  232,  2
2d5c5 31 36 2c 20 20 31 35 30 2c 20 20 32 32 32 2c 20  16,  150,  222, 
2d5c6 20 31 31 30 2c 20 20 32 32 34 2c 0a 20 2f 2a 20   110,  224,. /* 
2d5c7 20 20 33 38 30 20 2a 2f 20 20 20 32 32 35 2c 20    380 */   225, 
2d5c8 20 31 30 35 2c 20 20 31 30 36 2c 20 20 31 30 37   105,  106,  107
2d5c9 2c 20 20 31 33 35 2c 20 20 31 36 35 2c 20 20 31  ,  135,  165,  1
2d5ca 33 37 2c 20 20 31 37 32 2c 20 20 31 37 33 2c 20  37,  172,  173, 
2d5cb 20 20 34 38 2c 0a 20 2f 2a 20 20 20 33 39 30 20    48,. /*   390 
2d5cc 2a 2f 20 20 20 20 34 39 2c 20 20 31 36 35 2c 20  */    49,  165, 
2d5cd 20 31 31 36 2c 20 20 31 38 33 2c 20 20 31 37 34   116,  183,  174
2d5ce 2c 20 20 31 37 35 2c 20 20 31 38 31 2c 20 20 32  ,  175,  181,  2
2d5cf 34 32 2c 20 20 20 32 32 2c 20 20 32 34 35 2c 0a  42,   22,  245,.
2d5d0 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 31   /*   400 */   1
2d5d1 37 34 2c 20 20 31 37 35 2c 20 20 20 32 36 2c 20  74,  175,   26, 
2d5d2 20 20 32 37 2c 20 20 31 36 36 2c 20 20 31 33 36    27,  166,  136
2d5d3 2c 20 20 31 38 33 2c 20 20 20 36 36 2c 20 20 20  ,  183,   66,   
2d5d4 36 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20  67,   68,. /*   
2d5d5 34 31 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20  410 */    69,   
2d5d6 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
2d5d7 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35    73,   74,   75
2d5d8 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
2d5d9 37 38 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f  78,. /*   420 */
2d5da 20 20 20 20 31 31 2c 20 20 20 38 30 2c 20 20 20      11,   80,   
2d5db 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  81,   82,   83, 
2d5dc 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
2d5dd 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f  ,   87,   88,. /
2d5de 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 20 38 39  *   430 */    89
2d5df 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20 31  ,   90,   19,  1
2d5e0 35 30 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20  50,  150,   23, 
2d5e1 20 20 32 33 2c 20 20 20 32 35 2c 20 20 31 36 30    23,   25,  160
2d5e2 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 34 34  ,  150,. /*   44
2d5e3 30 20 2a 2f 20 20 20 32 32 30 2c 20 20 20 32 36  0 */   220,   26
2d5e4 2c 20 20 20 32 37 2c 20 20 32 30 35 2c 20 20 31  ,   27,  205,  1
2d5e5 36 30 2c 20 20 31 35 30 2c 20 20 32 32 30 2c 20  60,  150,  220, 
2d5e6 20 31 35 38 2c 20 20 31 36 35 2c 20 20 31 36 35   158,  165,  165
2d5e7 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20  ,. /*   450 */  
2d5e8 20 31 36 31 2c 20 20 31 36 32 2c 20 20 20 32 36   161,  162,   26
2d5e9 2c 20 20 20 32 37 2c 20 20 31 36 35 2c 20 20 20  ,   27,  165,   
2d5ea 20 30 2c 20 20 20 20 31 2c 20 20 20 20 32 2c 20   0,    1,    2, 
2d5eb 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20   174,  175,. /* 
2d5ec 20 20 34 36 30 20 2a 2f 20 20 20 31 36 35 2c 20    460 */   165, 
2d5ed 20 20 34 38 2c 20 20 20 34 39 2c 20 20 20 32 33    48,   49,   23
2d5ee 2c 20 20 20 35 35 2c 20 20 20 38 36 2c 20 20 20  ,   55,   86,   
2d5ef 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
2d5f0 20 20 39 30 2c 0a 20 2f 2a 20 20 20 34 37 30 20    90,. /*   470 
2d5f1 2a 2f 20 20 20 20 39 34 2c 20 20 20 39 35 2c 20  */    94,   95, 
2d5f2 20 31 39 34 2c 20 20 31 36 39 2c 20 20 31 37 30   194,  169,  170
2d5f3 2c 20 20 31 37 31 2c 20 20 31 39 33 2c 20 20 31  ,  171,  193,  1
2d5f4 33 36 2c 20 20 31 39 34 2c 20 20 20 36 36 2c 0a  36,  194,   66,.
2d5f5 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 20   /*   480 */    
2d5f6 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20  67,   68,   69, 
2d5f7 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
2d5f8 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20  ,   73,   74,   
2d5f9 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20  75,   76,. /*   
2d5fa 34 39 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20  490 */    77,   
2d5fb 37 38 2c 20 20 31 36 36 2c 20 20 20 38 30 2c 20  78,  166,   80, 
2d5fc 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
2d5fd 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
2d5fe 38 36 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f  86,. /*   500 */
2d5ff 20 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20      87,   88,   
2d600 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20  89,   90,   19, 
2d601 20 20 36 35 2c 20 20 31 36 30 2c 20 20 20 39 35    65,  160,   95
2d602 2c 20 20 20 32 33 2c 20 20 20 39 34 2c 0a 20 2f  ,   23,   94,. /
2d603 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 20 39 35  *   510 */    95
2d604 2c 20 20 31 37 33 2c 20 20 32 31 37 2c 20 20 20  ,  173,  217,   
2d605 32 32 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 20  22,  105,  106, 
2d606 20 31 30 37 2c 20 20 20 32 36 2c 20 20 20 32 32   107,   26,   22
2d607 2c 20 20 31 38 31 2c 0a 20 2f 2a 20 20 20 35 32  ,  181,. /*   52
2d608 30 20 2a 2f 20 20 20 20 39 34 2c 20 20 20 39 35  0 */    94,   95
2d609 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20  ,   26,   27,   
2d60a 39 36 2c 20 20 31 31 36 2c 20 20 32 34 33 2c 20  96,  116,  243, 
2d60b 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31    99,  100,  101
2d60c 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20  ,. /*   530 */  
2d60d 20 31 35 30 2c 20 20 32 30 35 2c 20 20 31 32 30   150,  205,  120
2d60e 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 31  ,   48,   49,  1
2d60f 32 30 2c 20 20 32 33 32 2c 20 20 20 32 32 2c 20  20,  232,   22, 
2d610 20 31 31 30 2c 20 20 31 36 36 2c 0a 20 2f 2a 20   110,  166,. /* 
2d611 20 20 35 34 30 20 2a 2f 20 20 20 31 39 34 2c 20    540 */   194, 
2d612 20 31 36 31 2c 20 20 31 36 32 2c 20 20 31 38 33   161,  162,  183
2d613 2c 20 20 31 36 33 2c 20 20 31 36 35 2c 20 20 31  ,  163,  165,  1
2d614 32 30 2c 20 20 31 36 36 2c 20 20 31 36 37 2c 20  20,  166,  167, 
2d615 20 31 36 38 2c 0a 20 2f 2a 20 20 20 35 35 30 20   168,. /*   550 
2d616 2a 2f 20 20 20 31 36 30 2c 20 20 20 36 36 2c 20  */   160,   66, 
2d617 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39    67,   68,   69
2d618 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
2d619 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 0a  72,   73,   74,.
2d61a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 20   /*   560 */    
2d61b 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
2d61c 20 20 37 38 2c 20 20 32 31 38 2c 20 20 20 38 30    78,  218,   80
2d61d 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20  ,   81,   82,   
2d61e 38 33 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20  83,   84,. /*   
2d61f 35 37 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 20  570 */    85,   
2d620 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20  86,   87,   88, 
2d621 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39    89,   90,   19
2d622 2c 20 20 20 31 32 2c 20 20 32 30 35 2c 20 20 31  ,   12,  205,  1
2d623 35 30 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f  50,. /*   580 */
2d624 20 20 20 20 32 33 2c 20 20 32 33 35 2c 20 20 31      23,  235,  1
2d625 39 30 2c 20 20 31 39 31 2c 20 20 31 39 34 2c 20  90,  191,  194, 
2d626 20 20 39 34 2c 20 20 32 34 30 2c 20 20 31 35 30    94,  240,  150
2d627 2c 20 20 20 38 36 2c 20 20 20 37 34 2c 0a 20 2f  ,   86,   74,. /
2d628 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 20 39 34  *   590 */    94
2d629 2c 20 20 20 39 35 2c 20 20 31 35 30 2c 20 20 20  ,   95,  150,   
2d62a 32 38 2c 20 20 31 36 35 2c 20 20 32 33 36 2c 20  28,  165,  236, 
2d62b 20 32 30 36 2c 20 20 32 30 37 2c 20 20 20 32 33   206,  207,   23
2d62c 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 36 30  ,  150,. /*   60
2d62d 30 20 2a 2f 20 20 20 20 34 38 2c 20 20 20 34 39  0 */    48,   49
2d62e 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31  ,  165,  174,  1
2d62f 37 35 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20  75,   48,   49, 
2d630 20 31 36 35 2c 20 20 20 34 33 2c 20 20 20 33 31   165,   43,   31
2d631 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20  ,. /*   610 */  
2d632 20 20 34 35 2c 20 20 31 37 34 2c 20 20 31 37 35    45,  174,  175
2d633 2c 20 20 20 32 31 2c 20 20 31 36 35 2c 20 20 31  ,   21,  165,  1
2d634 31 33 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  13,  174,  175, 
2d635 20 20 34 30 2c 20 20 31 31 37 2c 0a 20 2f 2a 20    40,  117,. /* 
2d636 20 20 36 32 30 20 2a 2f 20 20 20 20 35 35 2c 20    620 */    55, 
2d637 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 36 36    69,   70,   66
2d638 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20  ,   67,   68,   
2d639 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
2d63a 20 20 37 32 2c 0a 20 2f 2a 20 20 20 36 33 30 20    72,. /*   630 
2d63b 2a 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c 20  */    73,   74, 
2d63c 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
2d63d 2c 20 20 20 37 38 2c 20 20 31 35 30 2c 20 20 20  ,   78,  150,   
2d63e 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 0a  80,   81,   82,.
2d63f 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20 20 20 20   /*   640 */    
2d640 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
2d641 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
2d642 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20  ,   89,   90,   
2d643 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20  19,  150,. /*   
2d644 36 35 30 20 2a 2f 20 20 20 20 39 38 2c 20 20 31  650 */    98,  1
2d645 36 35 2c 20 20 20 32 33 2c 20 20 20 36 31 2c 20  65,   23,   61, 
2d646 20 20 32 33 2c 20 20 31 35 30 2c 20 20 20 32 35    23,  150,   25
2d647 2c 20 20 31 35 30 2c 20 20 20 31 39 2c 20 20 31  ,  150,   19,  1
2d648 31 33 2c 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f  13,. /*   660 */
2d649 20 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32     174,  175,  2
2d64a 31 33 2c 20 20 31 31 37 2c 20 20 31 36 35 2c 20  13,  117,  165, 
2d64b 20 20 32 34 2c 20 20 31 35 33 2c 20 20 20 32 36    24,  153,   26
2d64c 2c 20 20 20 32 33 2c 20 20 31 35 30 2c 0a 20 2f  ,   23,  150,. /
2d64d 2a 20 20 20 36 37 30 20 2a 2f 20 20 20 31 36 35  *   670 */   165
2d64e 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31  ,  150,  165,  1
2d64f 37 34 2c 20 20 31 37 35 2c 20 20 20 38 39 2c 20  74,  175,   89, 
2d650 20 20 39 30 2c 20 20 20 34 38 2c 20 20 20 34 39    90,   48,   49
2d651 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20 36 38  ,  174,. /*   68
2d652 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 37 34  0 */   175,  174
2d653 2c 20 20 31 37 35 2c 20 20 20 31 39 2c 20 20 31  ,  175,   19,  1
2d654 36 35 2c 20 20 32 33 37 2c 20 20 31 36 35 2c 20  65,  237,  165, 
2d655 20 31 31 32 2c 20 20 20 34 39 2c 20 20 31 31 34   112,   49,  114
2d656 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20  ,. /*   690 */  
2d657 20 31 31 35 2c 20 20 20 32 37 2c 20 20 31 30 30   115,   27,  100
2d658 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
2d659 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20  66,   67,   68, 
2d65a 20 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20    69,   70,. /* 
2d65b 20 20 37 30 30 20 2a 2f 20 20 20 20 37 31 2c 20    700 */    71, 
2d65c 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34    72,   73,   74
2d65d 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20  ,   75,   76,   
2d65e 37 37 2c 20 20 20 37 38 2c 20 20 31 35 30 2c 20  77,   78,  150, 
2d65f 20 20 38 30 2c 0a 20 2f 2a 20 20 20 37 31 30 20    80,. /*   710 
2d660 2a 2f 20 20 20 20 38 31 2c 20 20 20 38 32 2c 20  */    81,   82, 
2d661 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35    83,   84,   85
2d662 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
2d663 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a  88,   89,   90,.
2d664 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20 20 20 20   /*   720 */    
2d665 31 39 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  19,  150,  150, 
2d666 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 32 34   165,  150,   24
2d667 2c 20 20 31 31 32 2c 20 20 31 35 30 2c 20 20 31  ,  112,  150,  1
2d668 31 34 2c 20 20 31 31 35 2c 0a 20 2f 2a 20 20 20  14,  115,. /*   
2d669 37 33 30 20 2a 2f 20 20 20 31 33 38 2c 20 20 20  730 */   138,   
2d66a 31 39 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  19,  174,  175, 
2d66b 20 32 31 33 2c 20 20 20 39 34 2c 20 20 31 36 35   213,   94,  165
2d66c 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
2d66d 36 35 2c 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f  65,. /*   740 */
2d66e 20 20 20 20 32 39 2c 20 20 31 35 30 2c 20 20 31      29,  150,  1
2d66f 36 35 2c 20 20 31 30 34 2c 20 20 20 33 33 2c 20  65,  104,   33, 
2d670 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 39 36   174,  175,  196
2d671 2c 20 20 31 30 39 2c 20 20 20 34 38 2c 0a 20 2f  ,  109,   48,. /
2d672 2a 20 20 20 37 35 30 20 2a 2f 20 20 20 20 34 39  *   750 */    49
2d673 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
2d674 36 35 2c 20 20 31 39 30 2c 20 20 31 39 31 2c 20  65,  190,  191, 
2d675 20 31 36 35 2c 20 20 31 31 32 2c 20 20 20 34 37   165,  112,   47
2d676 2c 20 20 31 31 34 2c 0a 20 2f 2a 20 20 20 37 36  ,  114,. /*   76
2d677 30 20 2a 2f 20 20 20 31 31 35 2c 20 20 31 38 37  0 */   115,  187
2d678 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
2d679 36 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  60,  174,  175, 
2d67a 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
2d67b 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20  ,. /*   770 */  
2d67c 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
2d67d 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
2d67e 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
2d67f 20 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20    77,   78,. /* 
2d680 20 20 37 38 30 20 2a 2f 20 20 20 31 35 30 2c 20    780 */   150, 
2d681 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32    80,   81,   82
2d682 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
2d683 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
2d684 20 20 38 38 2c 0a 20 2f 2a 20 20 20 37 39 30 20    88,. /*   790 
2d685 2a 2f 20 20 20 20 38 39 2c 20 20 20 39 30 2c 20  */    89,   90, 
2d686 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31 35 30    19,  150,  150
2d687 2c 20 20 31 36 35 2c 20 20 31 39 38 2c 20 20 20  ,  165,  198,   
2d688 31 32 2c 20 20 31 39 34 2c 20 20 31 35 30 2c 0a  12,  194,  150,.
2d689 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20 20 20 31   /*   800 */   1
2d68a 35 30 2c 20 20 31 35 30 2c 20 20 32 34 38 2c 20  50,  150,  248, 
2d68b 20 32 34 39 2c 20 20 31 37 34 2c 20 20 31 37 35   249,  174,  175
2d68c 2c 20 20 32 30 36 2c 20 20 32 30 37 2c 20 20 31  ,  206,  207,  1
2d68d 36 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20  65,  165,. /*   
2d68e 38 31 30 20 2a 2f 20 20 20 20 39 38 2c 20 20 20  810 */    98,   
2d68f 32 33 2c 20 20 31 35 30 2c 20 20 20 32 38 2c 20  23,  150,   28, 
2d690 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35   165,  165,  165
2d691 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
2d692 36 36 2c 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f  66,. /*   820 */
2d693 20 20 20 31 35 30 2c 20 20 20 34 38 2c 20 20 20     150,   48,   
2d694 34 39 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  49,  174,  175, 
2d695 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35   174,  175,  165
2d696 2c 20 20 20 34 33 2c 20 20 32 33 33 2c 0a 20 2f  ,   43,  233,. /
2d697 2a 20 20 20 38 33 30 20 2a 2f 20 20 20 20 34 35  *   830 */    45
2d698 2c 20 20 20 32 32 2c 20 20 20 32 33 2c 20 20 31  ,   22,   23,  1
2d699 37 37 2c 20 20 20 32 35 2c 20 20 31 36 35 2c 20  77,   25,  165, 
2d69a 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 33 33   174,  175,  233
2d69b 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 38 34  ,   66,. /*   84
2d69c 30 20 2a 2f 20 20 20 20 36 37 2c 20 20 20 36 38  0 */    67,   68
2d69d 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
2d69e 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
2d69f 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
2d6a0 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20  ,. /*   850 */  
2d6a1 20 20 37 37 2c 20 20 20 37 38 2c 20 20 31 35 30    77,   78,  150
2d6a2 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
2d6a3 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
2d6a4 20 20 38 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20    85,   86,. /* 
2d6a5 20 20 38 36 30 20 2a 2f 20 20 20 20 38 37 2c 20    860 */    87, 
2d6a6 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
2d6a7 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 20  ,   19,  150,   
2d6a8 39 37 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20  97,  165,   25, 
2d6a9 20 31 35 30 2c 0a 20 2f 2a 20 20 20 38 37 30 20   150,. /*   870 
2d6aa 2a 2f 20 20 20 31 36 30 2c 20 20 31 35 30 2c 20  */   160,  150, 
2d6ab 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 36 35   150,  150,   65
2d6ac 2c 20 20 32 32 38 2c 20 20 31 37 34 2c 20 20 31  ,  228,  174,  1
2d6ad 37 35 2c 20 20 31 34 34 2c 20 20 31 34 35 2c 0a  75,  144,  145,.
2d6ae 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20 20 20 31   /*   880 */   1
2d6af 36 35 2c 20 20 32 34 36 2c 20 20 32 34 37 2c 20  65,  246,  247, 
2d6b0 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 31 36   150,  165,  116
2d6b1 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31  ,  165,  165,  1
2d6b2 36 35 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20  65,  174,. /*   
2d6b3 38 39 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31  890 */   175,  1
2d6b4 32 39 2c 20 20 31 33 30 2c 20 20 20 34 38 2c 20  29,  130,   48, 
2d6b5 20 20 34 39 2c 20 20 31 37 34 2c 20 20 31 37 35    49,  174,  175
2d6b6 2c 20 20 31 32 38 2c 20 20 31 36 35 2c 20 20 20  ,  128,  165,   
2d6b7 39 38 2c 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f  98,. /*   900 */
2d6b8 20 20 20 31 31 32 2c 20 20 31 37 37 2c 20 20 31     112,  177,  1
2d6b9 31 34 2c 20 20 31 31 35 2c 20 20 31 39 34 2c 20  14,  115,  194, 
2d6ba 20 20 34 39 2c 20 20 31 38 37 2c 20 20 31 37 34    49,  187,  174
2d6bb 2c 20 20 31 37 35 2c 20 20 31 38 37 2c 0a 20 2f  ,  175,  187,. /
2d6bc 2a 20 20 20 39 31 30 20 2a 2f 20 20 20 31 30 39  *   910 */   109
2d6bd 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20  ,   66,   67,   
2d6be 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20  68,   69,   70, 
2d6bf 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33    71,   72,   73
2d6c0 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 39 32  ,   74,. /*   92
2d6c1 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36  0 */    75,   76
2d6c2 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 31  ,   77,   78,  1
2d6c3 35 30 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20  50,   80,   81, 
2d6c4 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2d6c5 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20  ,. /*   930 */  
2d6c6 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
2d6c7 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20  ,   88,   89,   
2d6c8 39 30 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 20  90,   19,  150, 
2d6c9 20 20 32 33 2c 20 20 31 36 35 2c 0a 20 2f 2a 20    23,  165,. /* 
2d6ca 20 20 39 34 30 20 2a 2f 20 20 20 20 32 35 2c 20    940 */    25, 
2d6cb 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30   150,  150,  150
2d6cc 2c 20 20 31 35 30 2c 20 20 31 36 36 2c 20 20 31  ,  150,  166,  1
2d6cd 36 37 2c 20 20 31 36 38 2c 20 20 31 37 34 2c 20  67,  168,  174, 
2d6ce 20 31 37 35 2c 0a 20 2f 2a 20 20 20 39 35 30 20   175,. /*   950 
2d6cf 2a 2f 20 20 20 32 30 39 2c 20 20 20 32 35 2c 20  */   209,   25, 
2d6d0 20 31 36 35 2c 20 20 20 20 37 2c 20 20 20 20 38   165,    7,    8
2d6d1 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31  ,  150,  165,  1
2d6d2 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 0a  65,  165,  165,.
2d6d3 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20 20 20 31   /*   960 */   1
2d6d4 30 34 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  04,  174,  175, 
2d6d5 20 20 39 37 2c 20 20 20 39 38 2c 20 20 20 34 38    97,   98,   48
2d6d6 2c 20 20 20 34 39 2c 20 20 31 37 34 2c 20 20 31  ,   49,  174,  1
2d6d7 37 35 2c 20 20 31 32 36 2c 0a 20 2f 2a 20 20 20  75,  126,. /*   
2d6d8 39 37 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 20  970 */   165,   
2d6d9 32 32 2c 20 20 20 32 33 2c 20 20 31 37 37 2c 20  22,   23,  177, 
2d6da 20 20 32 35 2c 20 20 20 20 37 2c 20 20 20 20 38    25,    7,    8
2d6db 2c 20 20 20 20 39 2c 20 20 31 38 37 2c 20 20 31  ,    9,  187,  1
2d6dc 37 34 2c 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f  74,. /*   980 */
2d6dd 20 20 20 31 37 35 2c 20 20 31 38 37 2c 20 20 31     175,  187,  1
2d6de 36 30 2c 20 20 31 37 37 2c 20 20 20 36 37 2c 20  60,  177,   67, 
2d6df 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30    68,   69,   70
2d6e0 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 0a 20 2f  ,   71,   72,. /
2d6e1 2a 20 20 20 39 39 30 20 2a 2f 20 20 20 20 37 33  *   990 */    73
2d6e2 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
2d6e3 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
2d6e4 20 31 35 30 2c 20 20 20 38 30 2c 20 20 20 38 31   150,   80,   81
2d6e5 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20 31 30 30  ,   82,. /*  100
2d6e6 30 20 2a 2f 20 20 20 20 38 33 2c 20 20 20 38 34  0 */    83,   84
2d6e7 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
2d6e8 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
2d6e9 20 20 39 30 2c 20 20 20 31 39 2c 20 20 31 35 30    90,   19,  150
2d6ea 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20  ,. /*  1010 */  
2d6eb 20 31 36 30 2c 20 20 31 36 35 2c 20 20 31 38 32   160,  165,  182
2d6ec 2c 20 20 31 36 36 2c 20 20 20 36 35 2c 20 20 31  ,  166,   65,  1
2d6ed 35 30 2c 20 20 31 39 34 2c 20 20 31 30 35 2c 20  50,  194,  105, 
2d6ee 20 31 30 36 2c 20 20 31 30 37 2c 0a 20 2f 2a 20   106,  107,. /* 
2d6ef 20 31 30 32 30 20 2a 2f 20 20 20 31 37 34 2c 20   1020 */   174, 
2d6f0 20 31 37 35 2c 20 20 31 37 38 2c 20 20 20 32 33   175,  178,   23
2d6f1 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20 20 31  ,  165,   25,  1
2d6f2 37 37 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  77,  150,  150, 
2d6f3 20 31 30 33 2c 0a 20 2f 2a 20 20 31 30 33 30 20   103,. /*  1030 
2d6f4 2a 2f 20 20 20 31 36 35 2c 20 20 31 35 30 2c 20  */   165,  150, 
2d6f5 20 31 39 39 2c 20 20 31 37 34 2c 20 20 31 37 35   199,  174,  175
2d6f6 2c 20 20 31 35 30 2c 20 20 31 36 36 2c 20 20 20  ,  150,  166,   
2d6f7 34 38 2c 20 20 20 34 39 2c 20 20 31 37 34 2c 0a  48,   49,  174,.
2d6f8 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20 20 20 31   /*  1040 */   1
2d6f9 37 35 2c 20 20 32 30 39 2c 20 20 31 36 35 2c 20  75,  209,  165, 
2d6fa 20 31 36 35 2c 20 20 31 39 34 2c 20 20 20 32 33   165,  194,   23
2d6fb 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20 20 32  ,  165,   25,  2
2d6fc 30 39 2c 20 20 20 20 36 2c 0a 20 2f 2a 20 20 31  09,    6,. /*  1
2d6fd 30 35 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 31  050 */   165,  1
2d6fe 37 34 2c 20 20 31 37 35 2c 20 20 31 39 39 2c 20  74,  175,  199, 
2d6ff 20 31 34 39 2c 20 20 31 37 34 2c 20 20 31 37 35   149,  174,  175
2d700 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
2d701 37 30 2c 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f  70,. /*  1060 */
2d702 20 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20      71,   72,   
2d703 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20  73,   74,   75, 
2d704 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
2d705 2c 20 20 32 31 38 2c 20 20 20 38 30 2c 0a 20 2f  ,  218,   80,. /
2d706 2a 20 20 31 30 37 30 20 2a 2f 20 20 20 20 38 31  *  1070 */    81
2d707 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
2d708 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20  84,   85,   86, 
2d709 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
2d70a 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20 31 30 38  ,   90,. /*  108
2d70b 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 32 30  0 */    19,   20
2d70c 2c 20 20 20 31 36 2c 20 20 20 32 32 2c 20 20 31  ,   16,   22,  1
2d70d 35 30 2c 20 20 20 31 36 2c 20 20 31 35 30 2c 20  50,   16,  150, 
2d70e 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 34 39    26,   27,  149
2d70f 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20  ,. /*  1090 */  
2d710 20 32 34 30 2c 20 20 32 31 33 2c 20 20 31 35 30   240,  213,  150
2d711 2c 20 20 31 34 39 2c 20 20 31 34 39 2c 20 20 20  ,  149,  149,   
2d712 31 39 2c 20 20 20 32 30 2c 20 20 20 33 36 2c 20  19,   20,   36, 
2d713 20 20 32 32 2c 20 20 31 36 35 2c 0a 20 2f 2a 20    22,  165,. /* 
2d714 20 31 31 30 30 20 2a 2f 20 20 20 20 31 33 2c 20   1100 */    13, 
2d715 20 31 36 35 2c 20 20 20 32 36 2c 20 20 20 32 37   165,   26,   27
2d716 2c 20 20 31 35 31 2c 20 20 31 35 31 2c 20 20 31  ,  151,  151,  1
2d717 35 30 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20  50,  165,  174, 
2d718 20 31 37 35 2c 0a 20 2f 2a 20 20 31 31 31 30 20   175,. /*  1110 
2d719 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35 2c 20  */   174,  175, 
2d71a 20 20 33 36 2c 20 20 31 35 30 2c 20 20 20 32 35    36,  150,   25
2d71b 2c 20 20 20 35 34 2c 20 20 31 35 30 2c 20 20 31  ,   54,  150,  1
2d71c 35 30 2c 20 20 31 35 39 2c 20 20 31 35 30 2c 0a  50,  159,  150,.
2d71d 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20 20 20 20   /*  1120 */    
2d71e 32 33 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20  23,  165,   25, 
2d71f 20 31 39 34 2c 20 20 20 35 38 2c 20 20 20 36 34   194,   58,   64
2d720 2c 20 20 20 36 30 2c 20 20 20 35 38 2c 20 20 31  ,   60,   58,  1
2d721 36 35 2c 20 20 20 36 30 2c 0a 20 2f 2a 20 20 31  65,   60,. /*  1
2d722 31 33 30 20 2a 2f 20 20 20 20 35 34 2c 20 20 31  130 */    54,  1
2d723 36 35 2c 20 20 31 36 35 2c 20 20 31 32 36 2c 20  65,  165,  126, 
2d724 20 31 36 35 2c 20 20 31 39 33 2c 20 20 31 35 30   165,  193,  150
2d725 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
2d726 32 33 2c 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f  23,. /*  1140 */
2d727 20 20 20 20 36 34 2c 20 20 31 37 34 2c 20 20 31      64,  174,  1
2d728 37 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  75,  174,  175, 
2d729 20 20 38 34 2c 20 20 20 38 35 2c 20 20 31 39 39    84,   85,  199
2d72a 2c 20 20 31 35 30 2c 20 20 31 39 33 2c 0a 20 2f  ,  150,  193,. /
2d72b 2a 20 20 31 31 35 30 20 2a 2f 20 20 20 32 30 30  *  1150 */   200
2d72c 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
2d72d 35 30 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20  50,   93,   94, 
2d72e 20 20 39 35 2c 20 20 31 32 34 2c 20 20 32 30 31    95,  124,  201
2d72f 2c 20 20 20 39 38 2c 0a 20 2f 2a 20 20 31 31 36  ,   98,. /*  116
2d730 30 20 2a 2f 20 20 20 20 38 34 2c 20 20 20 38 35  0 */    84,   85
2d731 2c 20 20 20 38 36 2c 20 20 31 36 35 2c 20 20 31  ,   86,  165,  1
2d732 30 35 2c 20 20 31 30 36 2c 20 20 31 30 37 2c 20  05,  106,  107, 
2d733 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20 39 33   165,  165,   93
2d734 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20  ,. /*  1170 */  
2d735 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 37 34    94,   95,  174
2d736 2c 20 20 31 37 35 2c 20 20 20 39 38 2c 20 20 20  ,  175,   98,   
2d737 20 35 2c 20 20 20 32 33 2c 20 20 31 31 36 2c 20   5,   23,  116, 
2d738 20 20 32 35 2c 20 20 31 39 33 2c 0a 20 2f 2a 20    25,  193,. /* 
2d739 20 31 31 38 30 20 2a 2f 20 20 20 20 31 30 2c 20   1180 */    10, 
2d73a 20 20 31 31 2c 20 20 20 31 32 2c 20 20 20 31 33    11,   12,   13
2d73b 2c 20 20 20 31 34 2c 20 20 31 32 32 2c 20 20 31  ,   14,  122,  1
2d73c 35 30 2c 20 20 20 31 37 2c 20 20 32 30 33 2c 20  50,   17,  203, 
2d73d 20 32 30 32 2c 0a 20 2f 2a 20 20 31 31 39 30 20   202,. /*  1190 
2d73e 2a 2f 20 20 20 31 32 39 2c 20 20 31 33 30 2c 20  */   129,  130, 
2d73f 20 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33   131,  132,  133
2d740 2c 20 20 31 33 34 2c 20 20 31 39 33 2c 20 20 31  ,  134,  193,  1
2d741 35 30 2c 20 20 31 32 35 2c 20 20 31 33 35 2c 0a  50,  125,  135,.
2d742 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20 20 20 20   /*  1200 */    
2d743 33 30 2c 20 20 31 36 35 2c 20 20 20 33 32 2c 20  30,  165,   32, 
2d744 20 31 35 30 2c 20 20 31 33 38 2c 20 20 31 32 39   150,  138,  129
2d745 2c 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31  ,  130,  131,  1
2d746 33 32 2c 20 20 31 33 33 2c 0a 20 2f 2a 20 20 31  32,  133,. /*  1
2d747 32 31 30 20 2a 2f 20 20 20 31 33 34 2c 20 20 20  210 */   134,   
2d748 34 31 2c 20 20 31 36 35 2c 20 20 20 31 39 2c 20  41,  165,   19, 
2d749 20 20 32 30 2c 20 20 32 32 37 2c 20 20 20 32 32    20,  227,   22
2d74a 2c 20 20 31 31 38 2c 20 20 31 36 35 2c 20 20 31  ,  118,  165,  1
2d74b 35 37 2c 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f  57,. /*  1220 */
2d74c 20 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31      26,   27,  1
2d74d 35 30 2c 20 20 20 35 33 2c 20 20 31 35 30 2c 20  50,   53,  150, 
2d74e 20 20 35 35 2c 20 20 31 30 34 2c 20 20 31 37 34    55,  104,  174
2d74f 2c 20 20 31 37 35 2c 20 20 20 35 39 2c 0a 20 2f  ,  175,   59,. /
2d750 2a 20 20 31 32 33 30 20 2a 2f 20 20 20 20 33 36  *  1230 */    36
2d751 2c 20 20 20 32 32 2c 20 20 20 36 32 2c 20 20 32  ,   22,   62,  2
2d752 31 30 2c 20 20 31 35 30 2c 20 20 20 32 36 2c 20  10,  150,   26, 
2d753 20 20 32 37 2c 20 20 31 36 35 2c 20 20 31 35 30    27,  165,  150
2d754 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 32 34  ,  165,. /*  124
2d755 30 20 2a 2f 20 20 20 31 39 33 2c 20 20 31 35 30  0 */   193,  150
2d756 2c 20 20 31 35 30 2c 20 20 31 35 37 2c 20 20 31  ,  150,  157,  1
2d757 32 31 2c 20 20 32 31 31 2c 20 20 31 37 34 2c 20  21,  211,  174, 
2d758 20 31 37 35 2c 20 20 20 35 34 2c 20 20 31 36 35   175,   54,  165
2d759 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20  ,. /*  1250 */  
2d75a 20 31 35 30 2c 20 20 32 31 30 2c 20 20 32 31 30   150,  210,  210
2d75b 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 32  ,  165,  150,  2
2d75c 31 31 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  11,  165,  165, 
2d75d 20 20 36 34 2c 20 20 31 35 30 2c 0a 20 2f 2a 20    64,  150,. /* 
2d75e 20 31 32 36 30 20 2a 2f 20 20 20 32 31 31 2c 20   1260 */   211, 
2d75f 20 31 30 34 2c 20 20 31 37 34 2c 20 20 31 37 35   104,  174,  175
2d760 2c 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20  ,   23,  165,   
2d761 32 35 2c 20 20 31 39 33 2c 20 20 20 34 36 2c 20  25,  193,   46, 
2d762 20 31 36 35 2c 0a 20 2f 2a 20 20 31 32 37 30 20   165,. /*  1270 
2d763 2a 2f 20 20 20 20 32 33 2c 20 20 31 37 36 2c 20  */    23,  176, 
2d764 20 20 32 35 2c 20 20 20 36 34 2c 20 20 31 36 35    25,   64,  165
2d765 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 20 20 31  ,  105,  106,  1
2d766 30 37 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 0a  07,   84,   85,.
2d767 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20 20 20 31   /*  1280 */   1
2d768 35 30 2c 20 20 31 31 31 2c 20 20 31 37 36 2c 20  50,  111,  176, 
2d769 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 39 33   174,  175,  193
2d76a 2c 20 20 31 31 36 2c 20 20 20 39 33 2c 20 20 20  ,  116,   93,   
2d76b 39 34 2c 20 20 20 39 35 2c 0a 20 2f 2a 20 20 31  94,   95,. /*  1
2d76c 32 39 30 20 2a 2f 20 20 20 31 30 33 2c 20 20 31  290 */   103,  1
2d76d 35 30 2c 20 20 20 39 38 2c 20 20 20 38 34 2c 20  50,   98,   84, 
2d76e 20 20 38 35 2c 20 20 31 36 35 2c 20 20 31 35 30    85,  165,  150
2d76f 2c 20 20 31 39 33 2c 20 20 31 38 34 2c 20 20 31  ,  193,  184,  1
2d770 35 30 2c 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f  50,. /*  1300 */
2d771 20 20 20 31 35 30 2c 20 20 31 37 36 2c 20 20 31     150,  176,  1
2d772 35 30 2c 20 20 20 39 34 2c 20 20 31 35 30 2c 20  50,   94,  150, 
2d773 20 31 35 30 2c 20 20 31 36 35 2c 20 20 20 39 38   150,  165,   98
2d774 2c 20 20 20 32 33 2c 20 20 31 33 39 2c 0a 20 2f  ,   23,  139,. /
2d775 2a 20 20 31 33 31 30 20 2a 2f 20 20 20 20 32 35  *  1310 */    25
2d776 2c 20 20 31 36 35 2c 20 20 31 37 38 2c 20 20 31  ,  165,  178,  1
2d777 37 36 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  76,  165,  165, 
2d778 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30   150,  165,  150
2d779 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 33 32  ,  165,. /*  132
2d77a 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 35 30  0 */   165,  150
2d77b 2c 20 20 31 35 30 2c 20 20 31 32 39 2c 20 20 31  ,  150,  129,  1
2d77c 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c 20  30,  131,  132, 
2d77d 20 31 33 33 2c 20 20 31 33 34 2c 20 20 20 32 32   133,  134,   22
2d77e 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20  ,. /*  1330 */  
2d77f 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30   150,  165,  150
2d780 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
2d781 35 30 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  50,  165,  165, 
2d782 20 31 32 39 2c 20 20 31 33 30 2c 0a 20 2f 2a 20   129,  130,. /* 
2d783 20 31 33 34 30 20 2a 2f 20 20 20 31 33 31 2c 20   1340 */   131, 
2d784 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30   150,  150,  150
2d785 2c 20 20 31 37 39 2c 20 20 31 36 35 2c 20 20 31  ,  179,  165,  1
2d786 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20  50,  165,  150, 
2d787 20 31 36 35 2c 0a 20 2f 2a 20 20 31 33 35 30 20   165,. /*  1350 
2d788 2a 2f 20 20 20 31 36 35 2c 20 20 31 35 30 2c 20  */   165,  150, 
2d789 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 39 30   150,  150,   90
2d78a 2c 20 20 31 37 36 2c 20 20 31 36 35 2c 20 20 31  ,  176,  165,  1
2d78b 36 35 2c 20 20 31 36 35 2c 20 20 32 33 30 2c 0a  65,  165,  230,.
2d78c 20 2f 2a 20 20 31 33 36 30 20 2a 2f 20 20 20 20   /*  1360 */    
2d78d 32 33 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20  23,  165,   25, 
2d78e 20 31 36 35 2c 20 20 31 37 36 2c 20 20 32 33 30   165,  176,  230
2d78f 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31  ,  165,  165,  1
2d790 36 35 2c 20 20 31 37 39 2c 0a 20 2f 2a 20 20 31  65,  179,. /*  1
2d791 33 37 30 20 2a 2f 20 20 20 31 38 34 2c 20 20 31  370 */   184,  1
2d792 37 36 2c 20 20 20 31 38 2c 20 20 31 35 36 2c 20  76,   18,  156, 
2d793 20 20 34 34 2c 20 20 31 35 37 2c 20 20 31 35 36    44,  157,  156
2d794 2c 20 20 32 33 38 2c 20 20 31 35 37 2c 20 20 31  ,  238,  157,  1
2d795 35 36 2c 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f  56,. /*  1380 */
2d796 20 20 20 31 33 35 2c 20 20 31 35 37 2c 20 20 31     135,  157,  1
2d797 35 37 2c 20 20 32 33 39 2c 20 20 31 35 36 2c 20  57,  239,  156, 
2d798 20 20 36 36 2c 20 20 31 35 37 2c 20 20 20 32 32    66,  157,   22
2d799 2c 20 20 31 38 39 2c 20 20 31 38 39 2c 0a 20 2f  ,  189,  189,. /
2d79a 2a 20 20 31 33 39 30 20 2a 2f 20 20 20 31 35 37  *  1390 */   157
2d79b 2c 20 20 20 31 38 2c 20 20 32 31 39 2c 20 20 32  ,   18,  219,  2
2d79c 31 39 2c 20 20 31 35 37 2c 20 20 20 33 39 2c 20  19,  157,   39, 
2d79d 20 31 39 39 2c 20 20 31 39 32 2c 20 20 31 39 32   199,  192,  192
2d79e 2c 20 20 31 39 32 2c 0a 20 2f 2a 20 20 31 34 30  ,  192,. /*  140
2d79f 30 20 2a 2f 20 20 20 31 39 32 2c 20 20 31 38 39  0 */   192,  189
2d7a0 2c 20 20 32 34 31 2c 20 20 31 39 39 2c 20 20 32  ,  241,  199,  2
2d7a1 34 31 2c 20 20 31 35 37 2c 20 20 31 35 37 2c 20  41,  157,  157, 
2d7a2 20 20 33 37 2c 20 20 32 34 34 2c 20 20 32 34 37    37,  244,  247
2d7a3 2c 0a 20 2f 2a 20 20 31 34 31 30 20 2a 2f 20 20  ,. /*  1410 */  
2d7a4 20 31 36 34 2c 20 20 31 38 30 2c 20 20 31 38 30   164,  180,  180
2d7a5 2c 20 20 20 20 31 2c 20 20 20 31 35 2c 20 20 20  ,    1,   15,   
2d7a6 32 33 2c 20 20 20 32 32 2c 20 20 32 35 30 2c 20  23,   22,  250, 
2d7a7 20 31 31 38 2c 20 20 31 31 38 2c 0a 20 2f 2a 20   118,  118,. /* 
2d7a8 20 31 34 32 30 20 2a 2f 20 20 20 31 31 38 2c 20   1420 */   118, 
2d7a9 20 31 31 38 2c 20 20 31 31 38 2c 20 20 20 39 38   118,  118,   98
2d7aa 2c 20 20 31 31 33 2c 20 20 20 32 32 2c 20 20 20  ,  113,   22,   
2d7ab 31 31 2c 20 20 20 32 33 2c 20 20 20 32 33 2c 20  11,   23,   23, 
2d7ac 20 20 32 32 2c 0a 20 2f 2a 20 20 31 34 33 30 20    22,. /*  1430 
2d7ad 2a 2f 20 20 20 20 32 32 2c 20 20 20 32 35 2c 20  */    22,   25, 
2d7ae 20 20 32 33 2c 20 20 20 32 33 2c 20 20 20 32 33    23,   23,   23
2d7af 2c 20 20 20 33 34 2c 20 20 20 33 34 2c 20 20 31  ,   34,   34,  1
2d7b0 32 30 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 0a  20,   25,   25,.
2d7b1 20 2f 2a 20 20 31 34 34 30 20 2a 2f 20 20 20 20   /*  1440 */    
2d7b2 32 32 2c 20 20 31 31 38 2c 20 20 20 32 33 2c 20  22,  118,   23, 
2d7b3 20 20 32 33 2c 20 20 20 32 37 2c 20 20 20 35 30    23,   27,   50
2d7b4 2c 20 20 20 32 32 2c 20 20 20 35 30 2c 20 20 20  ,   22,   50,   
2d7b5 32 32 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 31  22,   22,. /*  1
2d7b6 34 35 30 20 2a 2f 20 20 20 20 33 34 2c 20 20 20  450 */    34,   
2d7b7 32 33 2c 20 20 20 32 32 2c 20 20 20 32 32 2c 20  23,   22,   22, 
2d7b8 20 31 30 32 2c 20 20 31 30 39 2c 20 20 20 31 39   102,  109,   19
2d7b9 2c 20 20 20 32 34 2c 20 20 20 32 30 2c 20 20 20  ,   24,   20,   
2d7ba 33 38 2c 0a 20 2f 2a 20 20 31 34 36 30 20 2a 2f  38,. /*  1460 */
2d7bb 20 20 20 20 32 35 2c 20 20 31 30 34 2c 20 20 31      25,  104,  1
2d7bc 33 38 2c 20 20 31 30 34 2c 20 20 20 32 32 2c 20  38,  104,   22, 
2d7bd 20 20 34 32 2c 20 20 20 20 35 2c 20 20 20 20 31    42,    5,    1
2d7be 2c 20 20 31 30 38 2c 20 20 31 32 37 2c 0a 20 2f  ,  108,  127,. /
2d7bf 2a 20 20 31 34 37 30 20 2a 2f 20 20 20 20 37 34  *  1470 */    74
2d7c0 2c 20 20 20 32 32 2c 20 20 20 35 30 2c 20 20 20  ,   22,   50,   
2d7c1 20 31 2c 20 20 20 37 34 2c 20 20 20 31 36 2c 20   1,   74,   16, 
2d7c2 20 31 32 31 2c 20 20 31 31 39 2c 20 20 20 32 30   121,  119,   20
2d7c3 2c 20 20 31 30 38 2c 0a 20 2f 2a 20 20 31 34 38  ,  108,. /*  148
2d7c4 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 31 31 39  0 */    51,  119
2d7c5 2c 20 20 20 35 37 2c 20 20 20 35 31 2c 20 20 20  ,   57,   51,   
2d7c6 32 32 2c 20 20 20 31 36 2c 20 20 20 32 33 2c 20  22,   16,   23, 
2d7c7 20 20 32 33 2c 20 20 31 32 37 2c 20 20 20 31 35    23,  127,   15
2d7c8 2c 0a 20 2f 2a 20 20 31 34 39 30 20 2a 2f 20 20  ,. /*  1490 */  
2d7c9 20 31 34 30 2c 20 20 31 32 38 2c 20 20 20 32 32   140,  128,   22
2d7ca 2c 20 20 20 20 33 2c 20 20 32 35 31 2c 20 20 20  ,    3,  251,   
2d7cb 20 34 2c 20 20 32 35 31 2c 20 20 20 36 33 2c 0a   4,  251,   63,.
2d7cc 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f 53 48  };.#define YY_SH
2d7cd 49 46 54 5f 55 53 45 5f 44 46 4c 54 20 28 2d 31  IFT_USE_DFLT (-1
2d7ce 31 31 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 53  11).#define YY_S
2d7cf 48 49 46 54 5f 4d 41 58 20 34 30 36 0a 73 74 61  HIFT_MAX 406.sta
2d7d0 74 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74 20  tic const short 
2d7d1 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20  yy_shift_ofst[] 
2d7d2 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f  = {. /*     0 */
2d7d3 20 20 20 31 38 37 2c 20 31 30 36 31 2c 20 31 31     187, 1061, 11
2d7d4 37 30 2c 20 31 30 36 31 2c 20 31 31 39 34 2c 20  70, 1061, 1194, 
2d7d5 31 31 39 34 2c 20 20 20 2d 32 2c 20 20 20 36 34  1194,   -2,   64
2d7d6 2c 20 20 20 36 34 2c 20 20 2d 31 39 2c 0a 20 2f  ,   64,  -19,. /
2d7d7 2a 20 20 20 20 31 30 20 2a 2f 20 20 31 31 39 34  *    10 */  1194
2d7d8 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31  , 1194, 1194, 11
2d7d9 39 34 2c 20 31 31 39 34 2c 20 20 32 37 36 2c 20  94, 1194,  276, 
2d7da 20 20 20 31 2c 20 20 31 32 35 2c 20 31 30 37 36     1,  125, 1076
2d7db 2c 20 31 31 39 34 2c 0a 20 2f 2a 20 20 20 20 32  , 1194,. /*    2
2d7dc 30 20 2a 2f 20 20 31 31 39 34 2c 20 31 31 39 34  0 */  1194, 1194
2d7dd 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31  , 1194, 1194, 11
2d7de 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20  94, 1194, 1194, 
2d7df 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34  1194, 1194, 1194
2d7e0 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20  ,. /*    30 */  
2d7e1 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34  1194, 1194, 1194
2d7e2 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31  , 1194, 1194, 11
2d7e3 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20  94, 1194, 1194, 
2d7e4 31 31 39 34 2c 20 31 31 39 34 2c 0a 20 2f 2a 20  1194, 1194,. /* 
2d7e5 20 20 20 34 30 20 2a 2f 20 20 31 31 39 34 2c 20     40 */  1194, 
2d7e6 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34  1194, 1194, 1194
2d7e7 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31  , 1194, 1194, 11
2d7e8 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20  94, 1194, 1194, 
2d7e9 31 31 39 34 2c 0a 20 2f 2a 20 20 20 20 35 30 20  1194,. /*    50 
2d7ea 2a 2f 20 20 31 31 39 34 2c 20 31 31 39 34 2c 20  */  1194, 1194, 
2d7eb 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34  1194, 1194, 1194
2d7ec 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31  , 1194, 1194, 11
2d7ed 39 34 2c 20 31 31 39 34 2c 20 20 2d 34 38 2c 0a  94, 1194,  -48,.
2d7ee 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 34   /*    60 */   4
2d7ef 30 39 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20  09,    1,    1, 
2d7f0 20 31 34 31 2c 20 20 32 38 31 2c 20 20 32 38 31   141,  281,  281
2d7f1 2c 20 2d 31 31 30 2c 20 20 20 35 33 2c 20 20 31  , -110,   53,  1
2d7f2 39 37 2c 20 20 32 36 39 2c 0a 20 2f 2a 20 20 20  97,  269,. /*   
2d7f3 20 37 30 20 2a 2f 20 20 20 33 34 31 2c 20 20 34   70 */   341,  4
2d7f4 31 33 2c 20 20 34 38 35 2c 20 20 35 35 37 2c 20  13,  485,  557, 
2d7f5 20 36 32 39 2c 20 20 37 30 31 2c 20 20 37 37 33   629,  701,  773
2d7f6 2c 20 20 38 34 35 2c 20 20 37 37 33 2c 20 20 37  ,  845,  773,  7
2d7f7 37 33 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f  73,. /*    80 */
2d7f8 20 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20 37     773,  773,  7
2d7f9 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20  73,  773,  773, 
2d7fa 20 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33   773,  773,  773
2d7fb 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 0a 20 2f  ,  773,  773,. /
2d7fc 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 37 37 33  *    90 */   773
2d7fd 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20 37  ,  773,  773,  7
2d7fe 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20  73,  773,  773, 
2d7ff 20 39 31 37 2c 20 20 39 38 39 2c 20 20 39 38 39   917,  989,  989
2d800 2c 20 20 2d 36 37 2c 0a 20 2f 2a 20 20 20 31 30  ,  -67,. /*   10
2d801 30 20 2a 2f 20 20 20 2d 36 37 2c 20 20 20 2d 31  0 */   -67,   -1
2d802 2c 20 20 20 2d 31 2c 20 20 20 35 35 2c 20 20 20  ,   -1,   55,   
2d803 32 35 2c 20 20 33 37 39 2c 20 20 20 20 31 2c 20  25,  379,    1, 
2d804 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31     1,    1,    1
2d805 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20  ,. /*   110 */  
2d806 20 20 20 31 2c 20 20 36 33 39 2c 20 20 35 39 32     1,  639,  592
2d807 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20  ,    1,    1,   
2d808 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20   1,    1,    1, 
2d809 20 20 20 31 2c 20 20 20 20 31 2c 0a 20 2f 2a 20     1,    1,. /* 
2d80a 20 20 31 32 30 20 2a 2f 20 20 20 20 20 31 2c 20    120 */     1, 
2d80b 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31     1,    1,    1
2d80c 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 35  ,    1,    1,  5
2d80d 38 36 2c 20 20 31 34 31 2c 20 20 2d 31 37 2c 20  86,  141,  -17, 
2d80e 2d 31 31 31 2c 0a 20 2f 2a 20 20 20 31 33 30 20  -111,. /*   130 
2d80f 2a 2f 20 20 2d 31 31 31 2c 20 2d 31 31 31 2c 20  */  -111, -111, 
2d810 31 32 30 39 2c 20 20 20 38 31 2c 20 20 33 37 36  1209,   81,  376
2d811 2c 20 20 34 31 35 2c 20 20 34 32 36 2c 20 20 34  ,  415,  426,  4
2d812 39 36 2c 20 20 20 39 30 2c 20 20 35 36 35 2c 0a  96,   90,  565,.
2d813 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 35   /*   140 */   5
2d814 36 35 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20  65,    1,    1, 
2d815 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31     1,    1,    1
2d816 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20  ,    1,    1,   
2d817 20 31 2c 20 20 20 20 31 2c 0a 20 2f 2a 20 20 20   1,    1,. /*   
2d818 31 35 30 20 2a 2f 20 20 20 20 20 31 2c 20 20 20  150 */     1,   
2d819 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20   1,    1,    1, 
2d81a 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31     1,    1,    1
2d81b 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20  ,    1,    1,   
2d81c 20 31 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f   1,. /*   160 */
2d81d 20 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20       1,    1,   
2d81e 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20   1,    1,    1, 
2d81f 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31     1,    1,    1
2d820 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 0a 20 2f  ,    1,    1,. /
2d821 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 20 20 31  *   170 */     1
2d822 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20  ,    1,    1,   
2d823 20 31 2c 20 20 38 30 39 2c 20 20 39 34 39 2c 20   1,  809,  949, 
2d824 20 34 35 35 2c 20 20 36 34 31 2c 20 20 36 34 31   455,  641,  641
2d825 2c 20 20 36 34 31 2c 0a 20 2f 2a 20 20 20 31 38  ,  641,. /*   18
2d826 30 20 2a 2f 20 20 20 37 36 39 2c 20 20 31 30 31  0 */   769,  101
2d827 2c 20 2d 31 31 30 2c 20 2d 31 31 30 2c 20 2d 31  , -110, -110, -1
2d828 31 30 2c 20 2d 31 31 31 2c 20 2d 31 31 31 2c 20  10, -111, -111, 
2d829 2d 31 31 31 2c 20 20 32 33 32 2c 20 20 32 33 32  -111,  232,  232
2d82a 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20  ,. /*   190 */  
2d82b 20 32 36 38 2c 20 20 34 32 38 2c 20 20 32 31 33   268,  428,  213
2d82c 2c 20 20 35 37 35 2c 20 20 36 34 35 2c 20 20 37  ,  575,  645,  7
2d82d 38 35 2c 20 20 37 38 38 2c 20 20 34 31 32 2c 20  85,  788,  412, 
2d82e 20 39 36 38 2c 20 20 35 30 32 2c 0a 20 2f 2a 20   968,  502,. /* 
2d82f 20 20 32 30 30 20 2a 2f 20 20 20 34 39 31 2c 20    200 */   491, 
2d830 20 20 35 32 2c 20 20 31 38 33 2c 20 20 31 38 33    52,  183,  183
2d831 2c 20 20 31 38 33 2c 20 20 36 31 34 2c 20 20 36  ,  183,  614,  6
2d832 31 34 2c 20 20 37 31 31 2c 20 20 39 31 32 2c 20  14,  711,  912, 
2d833 20 36 31 34 2c 0a 20 2f 2a 20 20 20 32 31 30 20   614,. /*   210 
2d834 2a 2f 20 20 20 36 31 34 2c 20 20 36 31 34 2c 20  */   614,  614, 
2d835 20 36 31 34 2c 20 20 32 32 39 2c 20 20 35 34 36   614,  229,  546
2d836 2c 20 20 2d 31 33 2c 20 20 31 34 31 2c 20 20 37  ,  -13,  141,  7
2d837 36 32 2c 20 20 37 36 32 2c 20 20 32 34 39 2c 0a  62,  762,  249,.
2d838 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 35   /*   220 */   5
2d839 37 38 2c 20 20 35 37 38 2c 20 20 36 36 34 2c 20  78,  578,  664, 
2d83a 20 35 37 38 2c 20 20 38 35 36 2c 20 20 35 37 38   578,  856,  578
2d83b 2c 20 20 31 34 31 2c 20 20 35 37 38 2c 20 20 31  ,  141,  578,  1
2d83c 34 31 2c 20 20 39 32 36 2c 0a 20 2f 2a 20 20 20  41,  926,. /*   
2d83d 32 33 30 20 2a 2f 20 20 20 38 34 33 2c 20 20 36  230 */   843,  6
2d83e 36 34 2c 20 20 36 36 34 2c 20 20 38 34 33 2c 20  64,  664,  843, 
2d83f 31 30 34 33 2c 20 31 30 34 33 2c 20 31 30 34 33  1043, 1043, 1043
2d840 2c 20 31 30 34 33 2c 20 31 30 38 37 2c 20 31 30  , 1043, 1087, 10
2d841 38 37 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f  87,. /*   240 */
2d842 20 20 31 30 38 39 2c 20 2d 31 31 30 2c 20 31 30    1089, -110, 10
2d843 30 37 2c 20 31 30 31 36 2c 20 31 30 33 33 2c 20  07, 1016, 1033, 
2d844 31 30 36 33 2c 20 31 30 37 33 2c 20 31 30 36 34  1063, 1073, 1064
2d845 2c 20 31 30 39 39 2c 20 31 30 39 39 2c 0a 20 2f  , 1099, 1099,. /
2d846 2a 20 20 20 32 35 30 20 2a 2f 20 20 31 31 32 32  *   250 */  1122
2d847 2c 20 31 31 32 33 2c 20 31 31 32 32 2c 20 31 31  , 1123, 1122, 11
2d848 32 33 2c 20 31 31 32 32 2c 20 31 31 32 33 2c 20  23, 1122, 1123, 
2d849 31 31 35 37 2c 20 31 31 35 37 2c 20 31 32 32 32  1157, 1157, 1222
2d84a 2c 20 31 31 35 37 2c 0a 20 2f 2a 20 20 20 32 36  , 1157,. /*   26
2d84b 30 20 2a 2f 20 20 31 31 38 37 2c 20 31 31 35 37  0 */  1187, 1157
2d84c 2c 20 31 33 30 37 2c 20 31 32 36 34 2c 20 31 32  , 1307, 1264, 12
2d84d 36 34 2c 20 31 32 32 32 2c 20 31 31 35 37 2c 20  64, 1222, 1157, 
2d84e 31 31 35 37 2c 20 31 31 35 37 2c 20 31 33 30 37  1157, 1157, 1307
2d84f 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20  ,. /*   270 */  
2d850 31 33 35 34 2c 20 31 30 39 39 2c 20 31 33 35 34  1354, 1099, 1354
2d851 2c 20 31 30 39 39 2c 20 31 33 35 34 2c 20 31 30  , 1099, 1354, 10
2d852 39 39 2c 20 31 30 39 39 2c 20 31 33 33 30 2c 20  99, 1099, 1330, 
2d853 31 32 34 35 2c 20 31 33 35 34 2c 0a 20 2f 2a 20  1245, 1354,. /* 
2d854 20 20 32 38 30 20 2a 2f 20 20 31 30 39 39 2c 20    280 */  1099, 
2d855 31 33 31 39 2c 20 31 33 31 39 2c 20 31 33 36 35  1319, 1319, 1365
2d856 2c 20 31 30 30 37 2c 20 31 30 39 39 2c 20 31 33  , 1007, 1099, 13
2d857 37 33 2c 20 31 33 37 33 2c 20 31 33 37 33 2c 20  73, 1373, 1373, 
2d858 31 33 37 33 2c 0a 20 2f 2a 20 20 20 32 39 30 20  1373,. /*   290 
2d859 2a 2f 20 20 31 30 30 37 2c 20 31 33 31 39 2c 20  */  1007, 1319, 
2d85a 31 33 36 35 2c 20 31 30 39 39 2c 20 31 33 35 36  1365, 1099, 1356
2d85b 2c 20 31 33 35 36 2c 20 31 30 39 39 2c 20 31 30  , 1356, 1099, 10
2d85c 39 39 2c 20 31 33 37 30 2c 20 2d 31 31 31 2c 0a  99, 1370, -111,.
2d85d 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 2d 31   /*   300 */  -1
2d85e 31 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31 2c 20  11, -111, -111, 
2d85f 2d 31 31 31 2c 20 20 35 35 32 2c 20 31 30 36 36  -111,  552, 1066
2d860 2c 20 31 30 35 39 2c 20 31 30 36 39 2c 20 20 37  , 1059, 1069,  7
2d861 31 32 2c 20 20 36 33 31 2c 0a 20 2f 2a 20 20 20  12,  631,. /*   
2d862 33 31 30 20 2a 2f 20 20 20 39 31 35 2c 20 20 38  310 */   915,  8
2d863 30 31 2c 20 20 39 34 36 2c 20 20 38 36 36 2c 20  01,  946,  866, 
2d864 31 30 30 30 2c 20 31 30 32 32 2c 20 31 30 39 37  1000, 1022, 1097
2d865 2c 20 31 31 35 33 2c 20 31 32 34 31 2c 20 31 32  , 1153, 1241, 12
2d866 34 37 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f  47,. /*   320 */
2d867 20 20 31 32 38 35 2c 20 20 35 31 35 2c 20 31 33    1285,  515, 13
2d868 33 37 2c 20 20 34 34 30 2c 20 31 34 31 32 2c 20  37,  440, 1412, 
2d869 31 33 39 39 2c 20 31 33 39 32 2c 20 31 33 39 34  1399, 1392, 1394
2d86a 2c 20 31 33 30 30 2c 20 31 33 30 31 2c 0a 20 2f  , 1300, 1301,. /
2d86b 2a 20 20 20 33 33 30 20 2a 2f 20 20 31 33 30 32  *   330 */  1302
2d86c 2c 20 31 33 30 33 2c 20 31 33 30 34 2c 20 31 33  , 1303, 1304, 13
2d86d 32 35 2c 20 31 33 31 31 2c 20 31 34 30 33 2c 20  25, 1311, 1403, 
2d86e 31 34 30 34 2c 20 31 34 30 35 2c 20 31 34 30 37  1404, 1405, 1407
2d86f 2c 20 31 34 31 35 2c 0a 20 2f 2a 20 20 20 33 34  , 1415,. /*   34
2d870 30 20 2a 2f 20 20 31 34 30 38 2c 20 31 34 30 39  0 */  1408, 1409
2d871 2c 20 31 34 30 36 2c 20 31 34 31 30 2c 20 31 34  , 1406, 1410, 14
2d872 31 31 2c 20 31 34 31 33 2c 20 31 34 30 31 2c 20  11, 1413, 1401, 
2d873 31 34 31 34 2c 20 31 34 30 32 2c 20 31 34 31 33  1414, 1402, 1413
2d874 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20  ,. /*   350 */  
2d875 31 33 31 37 2c 20 31 34 31 38 2c 20 31 34 31 36  1317, 1418, 1416
2d876 2c 20 31 34 31 37 2c 20 31 33 32 33 2c 20 31 34  , 1417, 1323, 14
2d877 31 39 2c 20 31 34 32 30 2c 20 31 34 32 31 2c 20  19, 1420, 1421, 
2d878 31 33 39 35 2c 20 31 34 32 34 2c 0a 20 2f 2a 20  1395, 1424,. /* 
2d879 20 20 33 36 30 20 2a 2f 20 20 31 33 39 37 2c 20    360 */  1397, 
2d87a 31 34 32 36 2c 20 31 34 32 38 2c 20 31 34 32 37  1426, 1428, 1427
2d87b 2c 20 31 34 33 30 2c 20 31 34 32 32 2c 20 31 34  , 1430, 1422, 14
2d87c 33 31 2c 20 31 33 35 32 2c 20 31 33 34 36 2c 20  31, 1352, 1346, 
2d87d 31 34 33 37 2c 0a 20 2f 2a 20 20 20 33 37 30 20  1437,. /*   370 
2d87e 2a 2f 20 20 31 34 33 38 2c 20 31 34 33 33 2c 20  */  1438, 1433, 
2d87f 31 33 35 37 2c 20 31 34 32 33 2c 20 31 34 32 35  1357, 1423, 1425
2d880 2c 20 31 34 32 39 2c 20 31 34 33 35 2c 20 31 34  , 1429, 1435, 14
2d881 33 32 2c 20 31 33 32 34 2c 20 31 33 35 39 2c 0a  32, 1324, 1359,.
2d882 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 31 34   /*   380 */  14
2d883 34 32 2c 20 31 34 36 31 2c 20 31 34 36 36 2c 20  42, 1461, 1466, 
2d884 31 33 36 30 2c 20 31 33 39 36 2c 20 31 34 30 30  1360, 1396, 1400
2d885 2c 20 31 33 34 32 2c 20 31 34 34 39 2c 20 31 33  , 1342, 1449, 13
2d886 35 38 2c 20 31 34 37 32 2c 0a 20 2f 2a 20 20 20  58, 1472,. /*   
2d887 33 39 30 20 2a 2f 20 20 31 34 35 39 2c 20 31 33  390 */  1459, 13
2d888 35 35 2c 20 31 34 35 38 2c 20 31 33 36 32 2c 20  55, 1458, 1362, 
2d889 31 33 37 31 2c 20 31 33 36 31 2c 20 31 34 36 32  1371, 1361, 1462
2d88a 2c 20 31 33 36 33 2c 20 31 34 36 33 2c 20 31 34  , 1363, 1463, 14
2d88b 36 34 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f  64,. /*   400 */
2d88c 20 20 31 34 36 39 2c 20 31 34 33 34 2c 20 31 34    1469, 1434, 14
2d88d 37 34 2c 20 31 33 35 30 2c 20 31 34 37 30 2c 20  74, 1350, 1470, 
2d88e 31 34 39 30 2c 20 31 34 39 31 2c 0a 7d 3b 0a 23  1490, 1491,.};.#
2d88f 64 65 66 69 6e 65 20 59 59 5f 52 45 44 55 43 45  define YY_REDUCE
2d890 5f 55 53 45 5f 44 46 4c 54 20 28 2d 31 38 30 29  _USE_DFLT (-180)
2d891 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44 55  .#define YY_REDU
2d892 43 45 5f 4d 41 58 20 33 30 33 0a 73 74 61 74 69  CE_MAX 303.stati
2d893 63 20 63 6f 6e 73 74 20 73 68 6f 72 74 20 79 79  c const short yy
2d894 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20 3d  _reduce_ofst[] =
2d895 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20   {. /*     0 */ 
2d896 20 2d 31 34 31 2c 20 20 20 38 32 2c 20 20 31 35   -141,   82,  15
2d897 34 2c 20 20 32 38 34 2c 20 20 20 31 32 2c 20 20  4,  284,   12,  
2d898 20 37 35 2c 20 20 20 36 39 2c 20 20 20 37 33 2c   75,   69,   73,
2d899 20 20 31 34 32 2c 20 20 2d 35 39 2c 0a 20 2f 2a    142,  -59,. /*
2d89a 20 20 20 20 31 30 20 2a 2f 20 20 20 31 34 35 2c      10 */   145,
2d89b 20 20 20 38 37 2c 20 20 31 35 39 2c 20 20 32 32     87,  159,  22
2d89c 30 2c 20 20 32 32 36 2c 20 20 33 34 36 2c 20 20  0,  226,  346,  
2d89d 32 38 39 2c 20 20 31 35 35 2c 20 20 34 32 39 2c  289,  155,  429,
2d89e 20 20 34 33 37 2c 0a 20 2f 2a 20 20 20 20 32 30    437,. /*    20
2d89f 20 2a 2f 20 20 20 34 34 32 2c 20 20 34 38 36 2c   */   442,  486,
2d8a0 20 20 34 39 39 2c 20 20 35 30 35 2c 20 20 35 30    499,  505,  50
2d8a1 37 2c 20 20 35 31 39 2c 20 20 35 35 38 2c 20 20  7,  519,  558,  
2d8a2 35 37 31 2c 20 20 35 37 37 2c 20 20 35 38 38 2c  571,  577,  588,
2d8a3 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20  . /*    30 */   
2d8a4 35 39 31 2c 20 20 36 33 30 2c 20 20 36 34 33 2c  591,  630,  643,
2d8a5 20 20 36 34 39 2c 20 20 36 35 31 2c 20 20 36 36    649,  651,  66
2d8a6 32 2c 20 20 37 30 32 2c 20 20 37 31 35 2c 20 20  2,  702,  715,  
2d8a7 37 32 31 2c 20 20 37 33 33 2c 0a 20 2f 2a 20 20  721,  733,. /*  
2d8a8 20 20 34 30 20 2a 2f 20 20 20 37 37 34 2c 20 20    40 */   774,  
2d8a9 37 38 37 2c 20 20 37 39 33 2c 20 20 38 30 35 2c  787,  793,  805,
2d8aa 20 20 38 34 36 2c 20 20 38 35 39 2c 20 20 38 36    846,  859,  86
2d8ab 35 2c 20 20 38 37 37 2c 20 20 38 38 31 2c 20 20  5,  877,  881,  
2d8ac 39 33 34 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a  934,. /*    50 *
2d8ad 2f 20 20 20 39 33 36 2c 20 20 39 36 33 2c 20 20  /   936,  963,  
2d8ae 39 36 37 2c 20 20 39 36 39 2c 20 20 39 39 38 2c  967,  969,  998,
2d8af 20 31 30 35 33 2c 20 31 30 37 32 2c 20 31 30 38   1053, 1072, 108
2d8b0 38 2c 20 31 31 30 39 2c 20 2d 31 37 39 2c 0a 20  8, 1109, -179,. 
2d8b1 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 38 35  /*    60 */   85
2d8b2 30 2c 20 20 32 38 33 2c 20 20 33 38 30 2c 20 20  0,  283,  380,  
2d8b3 33 38 31 2c 20 20 20 38 39 2c 20 20 33 30 34 2c  381,   89,  304,
2d8b4 20 20 33 39 30 2c 20 20 20 20 32 2c 20 20 20 20    390,    2,    
2d8b5 32 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20 20  2,    2,. /*    
2d8b6 37 30 20 2a 2f 20 20 20 20 20 32 2c 20 20 20 20  70 */     2,    
2d8b7 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20  2,    2,    2,  
2d8b8 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c    2,    2,    2,
2d8b9 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20      2,    2,    
2d8ba 32 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20  2,. /*    80 */ 
2d8bb 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20      2,    2,    
2d8bc 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20  2,    2,    2,  
2d8bd 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c    2,    2,    2,
2d8be 20 20 20 20 32 2c 20 20 20 20 32 2c 0a 20 2f 2a      2,    2,. /*
2d8bf 20 20 20 20 39 30 20 2a 2f 20 20 20 20 20 32 2c      90 */     2,
2d8c0 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20      2,    2,    
2d8c1 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20  2,    2,    2,  
2d8c2 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c    2,    2,    2,
2d8c3 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20 31 30 30      2,. /*   100
2d8c4 20 2a 2f 20 20 20 20 20 32 2c 20 20 20 20 32 2c   */     2,    2,
2d8c5 20 20 20 20 32 2c 20 20 32 31 35 2c 20 20 20 20      2,  215,    
2d8c6 32 2c 20 20 20 20 32 2c 20 20 34 34 39 2c 20 20  2,    2,  449,  
2d8c7 35 37 34 2c 20 20 37 31 39 2c 20 20 37 32 32 2c  574,  719,  722,
2d8c8 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20  . /*   110 */   
2d8c9 37 39 31 2c 20 20 31 33 34 2c 20 20 20 36 35 2c  791,  134,   65,
2d8ca 20 20 39 34 32 2c 20 20 35 32 31 2c 20 20 37 39    942,  521,  79
2d8cb 34 2c 20 20 2d 34 37 2c 20 20 38 37 38 2c 20 20  4,  -47,  878,  
2d8cc 39 35 36 2c 20 20 39 38 36 2c 0a 20 2f 2a 20 20  956,  986,. /*  
2d8cd 20 31 32 30 20 2a 2f 20 20 31 30 30 33 2c 20 31   120 */  1003, 1
2d8ce 30 34 37 2c 20 31 30 37 34 2c 20 31 30 39 32 2c  047, 1074, 1092,
2d8cf 20 20 32 39 35 2c 20 31 31 30 34 2c 20 20 20 20    295, 1104,    
2d8d0 32 2c 20 20 37 37 39 2c 20 20 20 20 32 2c 20 20  2,  779,    2,  
2d8d1 20 20 32 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a    2,. /*   130 *
2d8d2 2f 20 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20  /     2,    2,  
2d8d3 31 35 38 2c 20 20 33 33 38 2c 20 20 35 37 32 2c  158,  338,  572,
2d8d4 20 20 36 34 34 2c 20 20 36 35 30 2c 20 20 36 37    644,  650,  67
2d8d5 30 2c 20 20 37 32 33 2c 20 20 33 39 32 2c 0a 20  0,  723,  392,. 
2d8d6 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 35 36  /*   140 */   56
2d8d7 34 2c 20 20 37 39 32 2c 20 20 38 38 35 2c 20 20  4,  792,  885,  
2d8d8 39 36 36 2c 20 31 30 30 32 2c 20 31 30 33 36 2c  966, 1002, 1036,
2d8d9 20 20 37 32 33 2c 20 31 30 38 34 2c 20 31 30 39    723, 1084, 109
2d8da 31 2c 20 31 31 30 30 2c 0a 20 2f 2a 20 20 20 31  1, 1100,. /*   1
2d8db 35 30 20 2a 2f 20 20 31 31 33 30 2c 20 31 31 34  50 */  1130, 114
2d8dc 31 2c 20 31 31 34 36 2c 20 31 31 34 39 2c 20 31  1, 1146, 1149, 1
2d8dd 31 35 30 2c 20 31 31 35 32 2c 20 31 31 35 34 2c  150, 1152, 1154,
2d8de 20 31 31 35 35 2c 20 31 31 36 36 2c 20 31 31 36   1155, 1166, 116
2d8df 38 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20  8,. /*   160 */ 
2d8e0 20 31 31 37 31 2c 20 31 31 37 32 2c 20 31 31 38   1171, 1172, 118
2d8e1 30 2c 20 31 31 38 32 2c 20 31 31 38 34 2c 20 31  0, 1182, 1184, 1
2d8e2 31 38 35 2c 20 31 31 39 31 2c 20 31 31 39 32 2c  185, 1191, 1192,
2d8e3 20 31 31 39 33 2c 20 31 31 39 36 2c 0a 20 2f 2a   1193, 1196,. /*
2d8e4 20 20 20 31 37 30 20 2a 2f 20 20 31 31 39 38 2c     170 */  1198,
2d8e5 20 31 32 30 31 2c 20 31 32 30 32 2c 20 31 32 30   1201, 1202, 120
2d8e6 33 2c 20 20 35 35 34 2c 20 20 35 35 34 2c 20 20  3,  554,  554,  
2d8e7 37 33 34 2c 20 20 32 33 38 2c 20 20 33 32 36 2c  734,  238,  326,
2d8e8 20 20 33 37 33 2c 0a 20 2f 2a 20 20 20 31 38 30    373,. /*   180
2d8e9 20 2a 2f 20 20 2d 31 33 34 2c 20 20 32 37 38 2c   */  -134,  278,
2d8ea 20 20 36 30 34 2c 20 20 37 31 30 2c 20 20 38 32    604,  710,  82
2d8eb 32 2c 20 20 20 34 34 2c 20 20 36 30 30 2c 20 20  2,   44,  600,  
2d8ec 36 33 35 2c 20 20 2d 39 38 2c 20 20 2d 37 30 2c  635,  -98,  -70,
2d8ed 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20  . /*   190 */   
2d8ee 2d 35 34 2c 20 20 2d 33 36 2c 20 20 2d 33 35 2c  -54,  -36,  -35,
2d8ef 20 20 2d 33 35 2c 20 20 2d 33 35 2c 20 20 20 31    -35,  -35,   1
2d8f0 33 2c 20 20 2d 33 35 2c 20 20 20 31 34 2c 20 20  3,  -35,   14,  
2d8f1 31 34 39 2c 20 20 31 31 35 2c 0a 20 2f 2a 20 20  149,  115,. /*  
2d8f2 20 32 30 30 20 2a 2f 20 20 20 31 36 33 2c 20 20   200 */   163,  
2d8f3 20 31 34 2c 20 20 32 31 30 2c 20 20 32 32 33 2c   14,  210,  223,
2d8f4 20 20 33 36 30 2c 20 20 2d 33 35 2c 20 20 2d 33    360,  -35,  -3
2d8f5 35 2c 20 20 33 35 39 2c 20 20 34 34 38 2c 20 20  5,  359,  448,  
2d8f6 2d 33 35 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a  -35,. /*   210 *
2d8f7 2f 20 20 20 2d 33 35 2c 20 20 2d 33 35 2c 20 20  /   -35,  -35,  
2d8f8 2d 33 35 2c 20 20 35 31 33 2c 20 20 35 35 31 2c  -35,  513,  551,
2d8f9 20 20 35 39 38 2c 20 20 36 35 33 2c 20 20 35 39    598,  653,  59
2d8fa 36 2c 20 20 36 30 35 2c 20 20 36 34 37 2c 0a 20  6,  605,  647,. 
2d8fb 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 36 35  /*   220 */   65
2d8fc 36 2c 20 20 37 32 34 2c 20 20 37 34 31 2c 20 20  6,  724,  741,  
2d8fd 37 39 36 2c 20 20 38 33 30 2c 20 20 38 30 36 2c  796,  830,  806,
2d8fe 20 20 38 34 37 2c 20 20 38 34 39 2c 20 20 38 37    847,  849,  87
2d8ff 30 2c 20 20 38 34 34 2c 0a 20 2f 2a 20 20 20 32  0,  844,. /*   2
2d900 33 30 20 2a 2f 20 20 20 38 33 33 2c 20 20 38 33  30 */   833,  83
2d901 32 2c 20 20 38 33 39 2c 20 20 38 35 34 2c 20 20  2,  839,  854,  
2d902 39 30 35 2c 20 20 39 34 30 2c 20 20 39 34 34 2c  905,  940,  944,
2d903 20 20 39 34 35 2c 20 20 39 35 33 2c 20 20 39 35    945,  953,  95
2d904 34 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20  4,. /*   240 */ 
2d905 20 20 39 35 39 2c 20 20 39 32 39 2c 20 20 39 34    959,  929,  94
2d906 38 2c 20 20 39 35 30 2c 20 20 39 35 37 2c 20 20  8,  950,  957,  
2d907 39 38 37 2c 20 20 39 38 35 2c 20 20 39 38 38 2c  987,  985,  988,
2d908 20 31 30 36 32 2c 20 31 30 38 36 2c 0a 20 2f 2a   1062, 1086,. /*
2d909 20 20 20 32 35 30 20 2a 2f 20 20 31 30 32 33 2c     250 */  1023,
2d90a 20 31 30 33 34 2c 20 31 30 34 31 2c 20 31 30 34   1034, 1041, 104
2d90b 34 2c 20 31 30 34 32 2c 20 31 30 34 39 2c 20 31  4, 1042, 1049, 1
2d90c 30 39 35 2c 20 31 31 30 36 2c 20 31 31 31 34 2c  095, 1106, 1114,
2d90d 20 31 31 32 35 2c 0a 20 2f 2a 20 20 20 32 36 30   1125,. /*   260
2d90e 20 2a 2f 20 20 31 31 33 34 2c 20 31 31 33 37 2c   */  1134, 1137,
2d90f 20 31 31 36 35 2c 20 31 31 32 39 2c 20 31 31 33   1165, 1129, 113
2d910 35 2c 20 31 31 38 36 2c 20 31 31 37 39 2c 20 31  5, 1186, 1179, 1
2d911 31 38 38 2c 20 31 31 39 35 2c 20 31 31 39 30 2c  188, 1195, 1190,
2d912 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 31  . /*   270 */  1
2d913 32 31 37 2c 20 31 32 31 38 2c 20 31 32 32 30 2c  217, 1218, 1220,
2d914 20 31 32 32 31 2c 20 31 32 32 33 2c 20 31 32 32   1221, 1223, 122
2d915 34 2c 20 31 32 32 35 2c 20 31 31 33 39 2c 20 31  4, 1225, 1139, 1
2d916 31 34 34 2c 20 31 32 32 38 2c 0a 20 2f 2a 20 20  144, 1228,. /*  
2d917 20 32 38 30 20 2a 2f 20 20 31 32 32 39 2c 20 31   280 */  1229, 1
2d918 31 39 39 2c 20 31 32 30 30 2c 20 31 31 37 33 2c  199, 1200, 1173,
2d919 20 31 31 39 37 2c 20 31 32 33 33 2c 20 31 32 30   1197, 1233, 120
2d91a 35 2c 20 31 32 30 36 2c 20 31 32 30 37 2c 20 31  5, 1206, 1207, 1
2d91b 32 30 38 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a  208,. /*   290 *
2d91c 2f 20 20 31 32 30 34 2c 20 31 32 31 32 2c 20 31  /  1204, 1212, 1
2d91d 31 37 34 2c 20 31 32 33 37 2c 20 31 31 36 31 2c  174, 1237, 1161,
2d91e 20 31 31 36 33 2c 20 31 32 34 38 2c 20 31 32 34   1163, 1248, 124
2d91f 39 2c 20 31 31 36 34 2c 20 31 32 34 36 2c 0a 20  9, 1164, 1246,. 
2d920 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 31 32 33  /*   300 */  123
2d921 31 2c 20 31 32 33 32 2c 20 31 31 36 32 2c 20 31  1, 1232, 1162, 1
2d922 31 36 37 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63  167,.};.static c
2d923 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54 59 50  onst YYACTIONTYP
2d924 45 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d 20 3d  E yy_default[] =
2d925 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20   {. /*     0 */ 
2d926 20 20 36 32 31 2c 20 20 38 35 36 2c 20 20 39 33    621,  856,  93
2d927 39 2c 20 20 39 33 39 2c 20 20 38 35 36 2c 20 20  9,  939,  856,  
2d928 39 33 39 2c 20 20 39 33 39 2c 20 20 38 38 35 2c  939,  939,  885,
2d929 20 20 38 38 35 2c 20 20 37 34 34 2c 0a 20 2f 2a    885,  744,. /*
2d92a 20 20 20 20 31 30 20 2a 2f 20 20 20 38 35 34 2c      10 */   854,
2d92b 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d92c 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d92d 39 33 39 2c 20 20 39 31 34 2c 20 20 39 33 39 2c  939,  914,  939,
2d92e 20 20 39 33 39 2c 0a 20 2f 2a 20 20 20 20 32 30    939,. /*    20
2d92f 20 2a 2f 20 20 20 39 33 39 2c 20 20 39 33 39 2c   */   939,  939,
2d930 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d931 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d932 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d933 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20  . /*    30 */   
2d934 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d935 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d936 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d937 39 33 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20 20  939,  939,. /*  
2d938 20 20 34 30 20 2a 2f 20 20 20 39 33 39 2c 20 20    40 */   939,  
2d939 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d93a 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d93b 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d93c 39 33 39 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a  939,. /*    50 *
2d93d 2f 20 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  /   939,  939,  
2d93e 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d93f 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d940 39 2c 20 20 39 33 39 2c 20 20 38 32 38 2c 0a 20  9,  939,  828,. 
2d941 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 39 33  /*    60 */   93
2d942 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d943 36 36 30 2c 20 20 38 38 35 2c 20 20 38 38 35 2c  660,  885,  885,
2d944 20 20 37 34 38 2c 20 20 37 37 39 2c 20 20 39 33    748,  779,  93
2d945 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20 20 20 20  9,  939,. /*    
2d946 37 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 39 33  70 */   939,  93
2d947 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d948 39 33 39 2c 20 20 39 33 39 2c 20 20 37 38 30 2c  939,  939,  780,
2d949 20 20 39 33 39 2c 20 20 38 35 38 2c 20 20 38 35    939,  858,  85
2d94a 33 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20  3,. /*    80 */ 
2d94b 20 20 38 34 39 2c 20 20 38 35 31 2c 20 20 38 35    849,  851,  85
2d94c 30 2c 20 20 38 35 37 2c 20 20 37 38 31 2c 20 20  0,  857,  781,  
2d94d 37 37 30 2c 20 20 37 37 37 2c 20 20 37 38 34 2c  770,  777,  784,
2d94e 20 20 37 35 39 2c 20 20 38 39 38 2c 0a 20 2f 2a    759,  898,. /*
2d94f 20 20 20 20 39 30 20 2a 2f 20 20 20 37 38 36 2c      90 */   786,
2d950 20 20 37 38 37 2c 20 20 37 39 33 2c 20 20 37 39    787,  793,  79
2d951 34 2c 20 20 39 31 35 2c 20 20 39 31 33 2c 20 20  4,  915,  913,  
2d952 38 31 36 2c 20 20 38 31 35 2c 20 20 38 33 34 2c  816,  815,  834,
2d953 20 20 38 31 38 2c 0a 20 2f 2a 20 20 20 31 30 30    818,. /*   100
2d954 20 2a 2f 20 20 20 38 34 30 2c 20 20 38 31 37 2c   */   840,  817,
2d955 20 20 38 32 37 2c 20 20 36 35 32 2c 20 20 38 31    827,  652,  81
2d956 39 2c 20 20 38 32 30 2c 20 20 39 33 39 2c 20 20  9,  820,  939,  
2d957 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d958 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20  . /*   110 */   
2d959 39 33 39 2c 20 20 37 31 33 2c 20 20 36 34 37 2c  939,  713,  647,
2d95a 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d95b 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d95c 39 33 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20 20  939,  939,. /*  
2d95d 20 31 32 30 20 2a 2f 20 20 20 39 33 39 2c 20 20   120 */   939,  
2d95e 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d95f 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 38 32    939,  939,  82
2d960 31 2c 20 20 39 33 39 2c 20 20 38 32 32 2c 20 20  1,  939,  822,  
2d961 38 33 35 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a  835,. /*   130 *
2d962 2f 20 20 20 38 33 36 2c 20 20 38 33 37 2c 20 20  /   836,  837,  
2d963 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d964 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d965 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 0a 20  9,  939,  939,. 
2d966 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 39 33  /*   140 */   93
2d967 39 2c 20 20 36 32 37 2c 20 20 39 33 39 2c 20 20  9,  627,  939,  
2d968 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d969 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d96a 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20 20 20 31  9,  939,. /*   1
2d96b 35 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 39 33  50 */   939,  93
2d96c 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d96d 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d96e 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d96f 39 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20  9,. /*   160 */ 
2d970 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d971 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d972 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d973 20 20 39 33 39 2c 20 20 38 36 39 2c 0a 20 2f 2a    939,  869,. /*
2d974 20 20 20 31 37 30 20 2a 2f 20 20 20 39 33 39 2c     170 */   939,
2d975 20 20 39 31 38 2c 20 20 39 32 30 2c 20 20 39 33    918,  920,  93
2d976 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d977 36 32 31 2c 20 20 37 34 34 2c 20 20 37 34 34 2c  621,  744,  744,
2d978 20 20 37 34 34 2c 0a 20 2f 2a 20 20 20 31 38 30    744,. /*   180
2d979 20 2a 2f 20 20 20 39 33 39 2c 20 20 39 33 39 2c   */   939,  939,
2d97a 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d97b 39 2c 20 20 37 33 38 2c 20 20 37 34 38 2c 20 20  9,  738,  748,  
2d97c 39 33 32 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  932,  939,  939,
2d97d 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20  . /*   190 */   
2d97e 37 30 34 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  704,  939,  939,
2d97f 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d980 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d981 36 32 39 2c 20 20 37 33 36 2c 0a 20 2f 2a 20 20  629,  736,. /*  
2d982 20 32 30 30 20 2a 2f 20 20 20 36 36 32 2c 20 20   200 */   662,  
2d983 37 34 36 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  746,  939,  939,
2d984 20 20 39 33 39 2c 20 20 36 34 39 2c 20 20 37 32    939,  649,  72
2d985 35 2c 20 20 38 39 31 2c 20 20 39 33 39 2c 20 20  5,  891,  939,  
2d986 39 30 35 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a  905,. /*   210 *
2d987 2f 20 20 20 39 30 33 2c 20 20 37 32 37 2c 20 20  /   903,  727,  
2d988 37 38 39 2c 20 20 39 33 39 2c 20 20 37 33 36 2c  789,  939,  736,
2d989 20 20 37 34 35 2c 20 20 39 33 39 2c 20 20 39 33    745,  939,  93
2d98a 39 2c 20 20 39 33 39 2c 20 20 38 35 32 2c 0a 20  9,  939,  852,. 
2d98b 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 37 37  /*   220 */   77
2d98c 33 2c 20 20 37 37 33 2c 20 20 37 36 31 2c 20 20  3,  773,  761,  
2d98d 37 37 33 2c 20 20 36 38 33 2c 20 20 37 37 33 2c  773,  683,  773,
2d98e 20 20 39 33 39 2c 20 20 37 37 33 2c 20 20 39 33    939,  773,  93
2d98f 39 2c 20 20 36 38 36 2c 0a 20 2f 2a 20 20 20 32  9,  686,. /*   2
2d990 33 30 20 2a 2f 20 20 20 37 38 33 2c 20 20 37 36  30 */   783,  76
2d991 31 2c 20 20 37 36 31 2c 20 20 37 38 33 2c 20 20  1,  761,  783,  
2d992 36 32 36 2c 20 20 36 32 36 2c 20 20 36 32 36 2c  626,  626,  626,
2d993 20 20 36 32 36 2c 20 20 36 33 37 2c 20 20 36 33    626,  637,  63
2d994 37 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20  7,. /*   240 */ 
2d995 20 20 37 30 33 2c 20 20 39 33 39 2c 20 20 37 38    703,  939,  78
2d996 33 2c 20 20 37 37 34 2c 20 20 37 37 36 2c 20 20  3,  774,  776,  
2d997 37 36 36 2c 20 20 37 37 38 2c 20 20 39 33 39 2c  766,  778,  939,
2d998 20 20 37 35 32 2c 20 20 37 35 32 2c 0a 20 2f 2a    752,  752,. /*
2d999 20 20 20 32 35 30 20 2a 2f 20 20 20 37 36 30 2c     250 */   760,
2d99a 20 20 37 36 35 2c 20 20 37 36 30 2c 20 20 37 36    765,  760,  76
2d99b 35 2c 20 20 37 36 30 2c 20 20 37 36 35 2c 20 20  5,  760,  765,  
2d99c 37 31 35 2c 20 20 37 31 35 2c 20 20 37 30 30 2c  715,  715,  700,
2d99d 20 20 37 31 35 2c 0a 20 2f 2a 20 20 20 32 36 30    715,. /*   260
2d99e 20 2a 2f 20 20 20 36 38 36 2c 20 20 37 31 35 2c   */   686,  715,
2d99f 20 20 38 36 32 2c 20 20 38 36 36 2c 20 20 38 36    862,  866,  86
2d9a0 36 2c 20 20 37 30 30 2c 20 20 37 31 35 2c 20 20  6,  700,  715,  
2d9a1 37 31 35 2c 20 20 37 31 35 2c 20 20 38 36 32 2c  715,  715,  862,
2d9a2 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20  . /*   270 */   
2d9a3 36 34 34 2c 20 20 37 35 32 2c 20 20 36 34 34 2c  644,  752,  644,
2d9a4 20 20 37 35 32 2c 20 20 36 34 34 2c 20 20 37 35    752,  644,  75
2d9a5 32 2c 20 20 37 35 32 2c 20 20 38 39 35 2c 20 20  2,  752,  895,  
2d9a6 38 39 37 2c 20 20 36 34 34 2c 0a 20 2f 2a 20 20  897,  644,. /*  
2d9a7 20 32 38 30 20 2a 2f 20 20 20 37 35 32 2c 20 20   280 */   752,  
2d9a8 37 31 37 2c 20 20 37 31 37 2c 20 20 37 39 35 2c  717,  717,  795,
2d9a9 20 20 37 38 33 2c 20 20 37 35 32 2c 20 20 37 32    783,  752,  72
2d9aa 34 2c 20 20 37 32 34 2c 20 20 37 32 34 2c 20 20  4,  724,  724,  
2d9ab 37 32 34 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a  724,. /*   290 *
2d9ac 2f 20 20 20 37 38 33 2c 20 20 37 31 37 2c 20 20  /   783,  717,  
2d9ad 37 39 35 2c 20 20 37 35 32 2c 20 20 39 31 37 2c  795,  752,  917,
2d9ae 20 20 39 31 37 2c 20 20 37 35 32 2c 20 20 37 35    917,  752,  75
2d9af 32 2c 20 20 39 32 35 2c 20 20 36 37 30 2c 0a 20  2,  925,  670,. 
2d9b0 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 36 38  /*   300 */   68
2d9b1 38 2c 20 20 36 38 38 2c 20 20 39 33 32 2c 20 20  8,  688,  932,  
2d9b2 39 33 37 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  937,  939,  939,
2d9b3 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d9b4 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20 20 20 33  9,  939,. /*   3
2d9b5 31 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 39 33  10 */   939,  93
2d9b6 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d9b7 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d9b8 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d9b9 39 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20  9,. /*   320 */ 
2d9ba 20 20 39 33 39 2c 20 20 38 37 31 2c 20 20 39 33    939,  871,  93
2d9bb 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d9bc 36 33 35 2c 20 20 39 33 39 2c 20 20 36 35 34 2c  635,  939,  654,
2d9bd 20 20 38 30 32 2c 20 20 38 30 37 2c 0a 20 2f 2a    802,  807,. /*
2d9be 20 20 20 33 33 30 20 2a 2f 20 20 20 38 30 33 2c     330 */   803,
2d9bf 20 20 39 33 39 2c 20 20 38 30 34 2c 20 20 39 33    939,  804,  93
2d9c0 39 2c 20 20 37 33 30 2c 20 20 39 33 39 2c 20 20  9,  730,  939,  
2d9c1 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d9c2 20 20 39 33 39 2c 0a 20 2f 2a 20 20 20 33 34 30    939,. /*   340
2d9c3 20 2a 2f 20 20 20 39 33 39 2c 20 20 39 33 39 2c   */   939,  939,
2d9c4 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d9c5 39 2c 20 20 38 35 35 2c 20 20 39 33 39 2c 20 20  9,  855,  939,  
2d9c6 37 36 37 2c 20 20 39 33 39 2c 20 20 37 37 35 2c  767,  939,  775,
2d9c7 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20  . /*   350 */   
2d9c8 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d9c9 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d9ca 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d9cb 39 33 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20 20  939,  939,. /*  
2d9cc 20 33 36 30 20 2a 2f 20 20 20 39 33 39 2c 20 20   360 */   939,  
2d9cd 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d9ce 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d9cf 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d9d0 39 33 39 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a  939,. /*   370 *
2d9d1 2f 20 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  /   939,  939,  
2d9d2 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d9d3 20 20 38 39 33 2c 20 20 38 39 34 2c 20 20 39 33    893,  894,  93
2d9d4 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 0a 20  9,  939,  939,. 
2d9d5 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 39 33  /*   380 */   93
2d9d6 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d9d7 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d9d8 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d9d9 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20 20 20 33  9,  939,. /*   3
2d9da 39 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 39 33  90 */   939,  93
2d9db 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20  9,  939,  939,  
2d9dc 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c  939,  939,  939,
2d9dd 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33    939,  939,  93
2d9de 39 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20  9,. /*   400 */ 
2d9df 20 20 39 33 39 2c 20 20 39 32 34 2c 20 20 39 33    939,  924,  93
2d9e0 39 2c 20 20 39 33 39 2c 20 20 39 32 37 2c 20 20  9,  939,  927,  
2d9e1 36 32 32 2c 20 20 39 33 39 2c 20 20 36 31 37 2c  622,  939,  617,
2d9e2 20 20 36 31 39 2c 20 20 36 32 30 2c 0a 20 2f 2a    619,  620,. /*
2d9e3 20 20 20 34 31 30 20 2a 2f 20 20 20 36 32 34 2c     410 */   624,
2d9e4 20 20 36 32 35 2c 20 20 36 32 38 2c 20 20 36 35    625,  628,  65
2d9e5 34 2c 20 20 36 35 35 2c 20 20 36 35 37 2c 20 20  4,  655,  657,  
2d9e6 36 35 38 2c 20 20 36 35 39 2c 20 20 36 33 30 2c  658,  659,  630,
2d9e7 20 20 36 33 31 2c 0a 20 2f 2a 20 20 20 34 32 30    631,. /*   420
2d9e8 20 2a 2f 20 20 20 36 33 32 2c 20 20 36 33 33 2c   */   632,  633,
2d9e9 20 20 36 33 34 2c 20 20 36 33 36 2c 20 20 36 34    634,  636,  64
2d9ea 30 2c 20 20 36 33 38 2c 20 20 36 33 39 2c 20 20  0,  638,  639,  
2d9eb 36 34 31 2c 20 20 36 34 38 2c 20 20 36 35 30 2c  641,  648,  650,
2d9ec 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20  . /*   430 */   
2d9ed 36 36 39 2c 20 20 36 37 31 2c 20 20 36 37 33 2c  669,  671,  673,
2d9ee 20 20 37 33 34 2c 20 20 37 33 35 2c 20 20 37 39    734,  735,  79
2d9ef 39 2c 20 20 37 32 38 2c 20 20 37 32 39 2c 20 20  9,  728,  729,  
2d9f0 37 33 33 2c 20 20 36 35 36 2c 0a 20 2f 2a 20 20  733,  656,. /*  
2d9f1 20 34 34 30 20 2a 2f 20 20 20 38 31 30 2c 20 20   440 */   810,  
2d9f2 38 30 31 2c 20 20 38 30 35 2c 20 20 38 30 36 2c  801,  805,  806,
2d9f3 20 20 38 30 38 2c 20 20 38 30 39 2c 20 20 38 32    808,  809,  82
2d9f4 33 2c 20 20 38 32 34 2c 20 20 38 32 36 2c 20 20  3,  824,  826,  
2d9f5 38 33 32 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a  832,. /*   450 *
2d9f6 2f 20 20 20 38 33 39 2c 20 20 38 34 32 2c 20 20  /   839,  842,  
2d9f7 38 32 35 2c 20 20 38 33 30 2c 20 20 38 33 31 2c  825,  830,  831,
2d9f8 20 20 38 33 33 2c 20 20 38 33 38 2c 20 20 38 34    833,  838,  84
2d9f9 31 2c 20 20 37 33 31 2c 20 20 37 33 32 2c 0a 20  1,  731,  732,. 
2d9fa 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 38 34  /*   460 */   84
2d9fb 35 2c 20 20 36 36 33 2c 20 20 36 36 34 2c 20 20  5,  663,  664,  
2d9fc 36 36 37 2c 20 20 36 36 38 2c 20 20 38 38 31 2c  667,  668,  881,
2d9fd 20 20 38 38 33 2c 20 20 38 38 32 2c 20 20 38 38    883,  882,  88
2d9fe 34 2c 20 20 36 36 36 2c 0a 20 2f 2a 20 20 20 34  4,  666,. /*   4
2d9ff 37 30 20 2a 2f 20 20 20 36 36 35 2c 20 20 38 31  70 */   665,  81
2da00 31 2c 20 20 38 31 34 2c 20 20 38 34 37 2c 20 20  1,  814,  847,  
2da01 38 34 38 2c 20 20 39 30 36 2c 20 20 39 30 37 2c  848,  906,  907,
2da02 20 20 39 30 38 2c 20 20 39 30 39 2c 20 20 39 31    908,  909,  91
2da03 30 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20  0,. /*   480 */ 
2da04 20 20 38 34 33 2c 20 20 37 35 33 2c 20 20 38 34    843,  753,  84
2da05 36 2c 20 20 38 32 39 2c 20 20 37 36 38 2c 20 20  6,  829,  768,  
2da06 37 37 31 2c 20 20 37 37 32 2c 20 20 37 36 39 2c  771,  772,  769,
2da07 20 20 37 33 37 2c 20 20 37 34 37 2c 0a 20 2f 2a    737,  747,. /*
2da08 20 20 20 34 39 30 20 2a 2f 20 20 20 37 35 35 2c     490 */   755,
2da09 20 20 37 35 36 2c 20 20 37 35 37 2c 20 20 37 35    756,  757,  75
2da0a 38 2c 20 20 37 34 32 2c 20 20 37 34 33 2c 20 20  8,  742,  743,  
2da0b 37 34 39 2c 20 20 37 36 34 2c 20 20 37 39 37 2c  749,  764,  797,
2da0c 20 20 37 39 38 2c 0a 20 2f 2a 20 20 20 35 30 30    798,. /*   500
2da0d 20 2a 2f 20 20 20 37 36 32 2c 20 20 37 36 33 2c   */   762,  763,
2da0e 20 20 37 35 30 2c 20 20 37 35 31 2c 20 20 37 33    750,  751,  73
2da0f 39 2c 20 20 37 34 30 2c 20 20 37 34 31 2c 20 20  9,  740,  741,  
2da10 38 34 34 2c 20 20 38 30 30 2c 20 20 38 31 32 2c  844,  800,  812,
2da11 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20  . /*   510 */   
2da12 38 31 33 2c 20 20 36 37 34 2c 20 20 36 37 35 2c  813,  674,  675,
2da13 20 20 38 30 37 2c 20 20 36 37 36 2c 20 20 36 37    807,  676,  67
2da14 37 2c 20 20 36 37 38 2c 20 20 37 31 36 2c 20 20  7,  678,  716,  
2da15 37 31 39 2c 20 20 37 32 30 2c 0a 20 2f 2a 20 20  719,  720,. /*  
2da16 20 35 32 30 20 2a 2f 20 20 20 37 32 31 2c 20 20   520 */   721,  
2da17 36 37 39 2c 20 20 36 39 38 2c 20 20 37 30 31 2c  679,  698,  701,
2da18 20 20 37 30 32 2c 20 20 36 38 30 2c 20 20 36 38    702,  680,  68
2da19 37 2c 20 20 36 38 31 2c 20 20 36 38 32 2c 20 20  7,  681,  682,  
2da1a 36 38 39 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a  689,. /*   530 *
2da1b 2f 20 20 20 36 39 30 2c 20 20 36 39 31 2c 20 20  /   690,  691,  
2da1c 36 39 34 2c 20 20 36 39 35 2c 20 20 36 39 36 2c  694,  695,  696,
2da1d 20 20 36 39 37 2c 20 20 36 39 32 2c 20 20 36 39    697,  692,  69
2da1e 33 2c 20 20 38 36 33 2c 20 20 38 36 34 2c 0a 20  3,  863,  864,. 
2da1f 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 38 36  /*   540 */   86
2da20 37 2c 20 20 38 36 35 2c 20 20 36 38 34 2c 20 20  7,  865,  684,  
2da21 36 38 35 2c 20 20 36 39 39 2c 20 20 36 37 32 2c  685,  699,  672,
2da22 20 20 36 36 31 2c 20 20 36 35 33 2c 20 20 37 30    661,  653,  70
2da23 35 2c 20 20 37 30 38 2c 0a 20 2f 2a 20 20 20 35  5,  708,. /*   5
2da24 35 30 20 2a 2f 20 20 20 37 30 39 2c 20 20 37 31  50 */   709,  71
2da25 30 2c 20 20 37 31 31 2c 20 20 37 31 32 2c 20 20  0,  711,  712,  
2da26 37 31 34 2c 20 20 37 30 36 2c 20 20 37 30 37 2c  714,  706,  707,
2da27 20 20 36 35 31 2c 20 20 36 34 32 2c 20 20 36 34    651,  642,  64
2da28 35 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20  5,. /*   560 */ 
2da29 20 20 37 35 34 2c 20 20 38 38 37 2c 20 20 38 39    754,  887,  89
2da2a 36 2c 20 20 38 39 32 2c 20 20 38 38 38 2c 20 20  6,  892,  888,  
2da2b 38 38 39 2c 20 20 38 39 30 2c 20 20 36 34 36 2c  889,  890,  646,
2da2c 20 20 38 35 39 2c 20 20 38 36 30 2c 0a 20 2f 2a    859,  860,. /*
2da2d 20 20 20 35 37 30 20 2a 2f 20 20 20 37 31 38 2c     570 */   718,
2da2e 20 20 37 39 31 2c 20 20 37 39 32 2c 20 20 38 38    791,  792,  88
2da2f 36 2c 20 20 38 39 39 2c 20 20 39 30 31 2c 20 20  6,  899,  901,  
2da30 37 39 36 2c 20 20 39 30 32 2c 20 20 39 30 34 2c  796,  902,  904,
2da31 20 20 39 30 30 2c 0a 20 2f 2a 20 20 20 35 38 30    900,. /*   580
2da32 20 2a 2f 20 20 20 39 32 39 2c 20 20 36 34 33 2c   */   929,  643,
2da33 20 20 37 32 32 2c 20 20 37 32 33 2c 20 20 37 32    722,  723,  72
2da34 36 2c 20 20 38 36 38 2c 20 20 39 31 31 2c 20 20  6,  868,  911,  
2da35 37 38 32 2c 20 20 37 38 35 2c 20 20 37 38 38 2c  782,  785,  788,
2da36 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20  . /*   590 */   
2da37 37 39 30 2c 20 20 38 37 30 2c 20 20 38 37 32 2c  790,  870,  872,
2da38 20 20 38 37 34 2c 20 20 38 37 36 2c 20 20 38 37    874,  876,  87
2da39 37 2c 20 20 38 37 38 2c 20 20 38 37 39 2c 20 20  7,  878,  879,  
2da3a 38 38 30 2c 20 20 38 37 33 2c 0a 20 2f 2a 20 20  880,  873,. /*  
2da3b 20 36 30 30 20 2a 2f 20 20 20 38 37 35 2c 20 20   600 */   875,  
2da3c 39 31 32 2c 20 20 39 31 36 2c 20 20 39 31 39 2c  912,  916,  919,
2da3d 20 20 39 32 31 2c 20 20 39 32 32 2c 20 20 39 32    921,  922,  92
2da3e 33 2c 20 20 39 32 36 2c 20 20 39 32 38 2c 20 20  3,  926,  928,  
2da3f 39 33 33 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a  933,. /*   610 *
2da40 2f 20 20 20 39 33 34 2c 20 20 39 33 35 2c 20 20  /   934,  935,  
2da41 39 33 38 2c 20 20 39 33 36 2c 20 20 36 32 33 2c  938,  936,  623,
2da42 20 20 36 31 38 2c 0a 7d 3b 0a 23 64 65 66 69 6e    618,.};.#defin
2da43 65 20 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 28  e YY_SZ_ACTTAB (
2da44 69 6e 74 29 28 73 69 7a 65 6f 66 28 79 79 5f 61  int)(sizeof(yy_a
2da45 63 74 69 6f 6e 29 2f 73 69 7a 65 6f 66 28 79 79  ction)/sizeof(yy
2da46 5f 61 63 74 69 6f 6e 5b 30 5d 29 29 0a 0a 2f 2a  _action[0]))../*
2da47 20 54 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20   The next table 
2da48 6d 61 70 73 20 74 6f 6b 65 6e 73 20 69 6e 74 6f  maps tokens into
2da49 20 66 61 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 73   fallback tokens
2da4a 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 75 63  .  If a construc
2da4b 74 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 66 6f  t.** like the fo
2da4c 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20  llowing:.** .** 
2da4d 20 20 20 20 20 25 66 61 6c 6c 62 61 63 6b 20 49       %fallback I
2da4e 44 20 58 20 59 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61  D X Y Z..**.** a
2da4f 70 70 65 61 72 73 20 69 6e 20 74 68 65 20 67 72  ppears in the gr
2da50 61 6d 6d 61 72 2c 20 74 68 65 6e 20 49 44 20 62  ammar, then ID b
2da51 65 63 6f 6d 65 73 20 61 20 66 61 6c 6c 62 61 63  ecomes a fallbac
2da52 6b 20 74 6f 6b 65 6e 20 66 6f 72 20 58 2c 20 59  k token for X, Y
2da53 2c 0a 2a 2a 20 61 6e 64 20 5a 2e 20 20 57 68 65  ,.** and Z.  Whe
2da54 6e 65 76 65 72 20 6f 6e 65 20 6f 66 20 74 68 65  never one of the
2da55 20 74 6f 6b 65 6e 73 20 58 2c 20 59 2c 20 6f 72   tokens X, Y, or
2da56 20 5a 20 69 73 20 69 6e 70 75 74 20 74 6f 20 74   Z is input to t
2da57 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 62 75 74  he parser.** but
2da58 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 70 61 72   it does not par
2da59 73 65 2c 20 74 68 65 20 74 79 70 65 20 6f 66 20  se, the type of 
2da5a 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 63 68 61  the token is cha
2da5b 6e 67 65 64 20 74 6f 20 49 44 20 61 6e 64 0a 2a  nged to ID and.*
2da5c 2a 20 74 68 65 20 70 61 72 73 65 20 69 73 20 72  * the parse is r
2da5d 65 74 72 69 65 64 20 62 65 66 6f 72 65 20 61 6e  etried before an
2da5e 20 65 72 72 6f 72 20 69 73 20 74 68 72 6f 77 6e   error is thrown
2da5f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59 46 41  ..*/.#ifdef YYFA
2da60 4c 4c 42 41 43 4b 0a 73 74 61 74 69 63 20 63 6f  LLBACK.static co
2da61 6e 73 74 20 59 59 43 4f 44 45 54 59 50 45 20 79  nst YYCODETYPE y
2da62 79 46 61 6c 6c 62 61 63 6b 5b 5d 20 3d 20 7b 0a  yFallback[] = {.
2da63 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2da64 20 20 20 20 24 20 3d 3e 20 6e 6f 74 68 69 6e 67      $ => nothing
2da65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2da66 20 20 20 20 20 53 45 4d 49 20 3d 3e 20 6e 6f 74       SEMI => not
2da67 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20  hing */.   26,  
2da68 2f 2a 20 20 20 20 45 58 50 4c 41 49 4e 20 3d 3e  /*    EXPLAIN =>
2da69 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2da6a 2a 20 20 20 20 20 20 51 55 45 52 59 20 3d 3e 20  *      QUERY => 
2da6b 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2da6c 20 20 20 20 20 20 20 50 4c 41 4e 20 3d 3e 20 49         PLAN => I
2da6d 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2da6e 20 20 20 20 20 42 45 47 49 4e 20 3d 3e 20 49 44       BEGIN => ID
2da6f 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 54   */.    0,  /* T
2da70 52 41 4e 53 41 43 54 49 4f 4e 20 3d 3e 20 6e 6f  RANSACTION => no
2da71 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20  thing */.   26, 
2da72 20 2f 2a 20 20 20 44 45 46 45 52 52 45 44 20 3d   /*   DEFERRED =
2da73 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2da74 2f 2a 20 20 49 4d 4d 45 44 49 41 54 45 20 3d 3e  /*  IMMEDIATE =>
2da75 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2da76 2a 20 20 45 58 43 4c 55 53 49 56 45 20 3d 3e 20  *  EXCLUSIVE => 
2da77 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ID */.    0,  /*
2da78 20 20 20 20 20 43 4f 4d 4d 49 54 20 3d 3e 20 6e       COMMIT => n
2da79 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c  othing */.   26,
2da7a 20 20 2f 2a 20 20 20 20 20 20 20 20 45 4e 44 20    /*        END 
2da7b 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2da7c 20 2f 2a 20 20 20 52 4f 4c 4c 42 41 43 4b 20 3d   /*   ROLLBACK =
2da7d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2da7e 2f 2a 20 20 53 41 56 45 50 4f 49 4e 54 20 3d 3e  /*  SAVEPOINT =>
2da7f 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2da80 2a 20 20 20 20 52 45 4c 45 41 53 45 20 3d 3e 20  *    RELEASE => 
2da81 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ID */.    0,  /*
2da82 20 20 20 20 20 20 20 20 20 54 4f 20 3d 3e 20 6e           TO => n
2da83 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2da84 20 20 2f 2a 20 20 20 20 20 20 54 41 42 4c 45 20    /*      TABLE 
2da85 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2da86 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 43 52 45    0,  /*     CRE
2da87 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  ATE => nothing *
2da88 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2da89 20 20 20 20 20 49 46 20 3d 3e 20 49 44 20 2a 2f       IF => ID */
2da8a 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
2da8b 20 20 20 4e 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e     NOT => nothin
2da8c 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  g */.    0,  /* 
2da8d 20 20 20 20 45 58 49 53 54 53 20 3d 3e 20 6e 6f      EXISTS => no
2da8e 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20  thing */.   26, 
2da8f 20 2f 2a 20 20 20 20 20 20 20 54 45 4d 50 20 3d   /*       TEMP =
2da90 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
2da91 2f 2a 20 20 20 20 20 20 20 20 20 4c 50 20 3d 3e  /*         LP =>
2da92 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2da93 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 52  0,  /*         R
2da94 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  P => nothing */.
2da95 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2da96 20 20 20 41 53 20 3d 3e 20 6e 6f 74 68 69 6e 67     AS => nothing
2da97 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2da98 20 20 20 20 43 4f 4d 4d 41 20 3d 3e 20 6e 6f 74      COMMA => not
2da99 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2da9a 2f 2a 20 20 20 20 20 20 20 20 20 49 44 20 3d 3e  /*         ID =>
2da9b 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2da9c 30 2c 20 20 2f 2a 20 20 20 20 49 4e 44 45 58 45  0,  /*    INDEXE
2da9d 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  D => nothing */.
2da9e 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
2da9f 41 42 4f 52 54 20 3d 3e 20 49 44 20 2a 2f 0a 20  ABORT => ID */. 
2daa0 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 41    26,  /*      A
2daa1 46 54 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  FTER => ID */.  
2daa2 20 32 36 2c 20 20 2f 2a 20 20 20 20 41 4e 41 4c   26,  /*    ANAL
2daa3 59 5a 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  YZE => ID */.   
2daa4 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41  26,  /*        A
2daa5 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  SC => ID */.   2
2daa6 36 2c 20 20 2f 2a 20 20 20 20 20 41 54 54 41 43  6,  /*     ATTAC
2daa7 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  H => ID */.   26
2daa8 2c 20 20 2f 2a 20 20 20 20 20 42 45 46 4f 52 45  ,  /*     BEFORE
2daa9 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2daaa 20 20 2f 2a 20 20 20 20 20 20 20 20 20 42 59 20    /*         BY 
2daab 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2daac 20 2f 2a 20 20 20 20 43 41 53 43 41 44 45 20 3d   /*    CASCADE =
2daad 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2daae 2f 2a 20 20 20 20 20 20 20 43 41 53 54 20 3d 3e  /*       CAST =>
2daaf 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2dab0 2a 20 20 20 43 4f 4c 55 4d 4e 4b 57 20 3d 3e 20  *   COLUMNKW => 
2dab1 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2dab2 20 20 20 43 4f 4e 46 4c 49 43 54 20 3d 3e 20 49     CONFLICT => I
2dab3 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2dab4 20 20 44 41 54 41 42 41 53 45 20 3d 3e 20 49 44    DATABASE => ID
2dab5 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2dab6 20 20 20 20 20 44 45 53 43 20 3d 3e 20 49 44 20       DESC => ID 
2dab7 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2dab8 20 20 44 45 54 41 43 48 20 3d 3e 20 49 44 20 2a    DETACH => ID *
2dab9 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2daba 20 20 20 45 41 43 48 20 3d 3e 20 49 44 20 2a 2f     EACH => ID */
2dabb 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2dabc 20 20 46 41 49 4c 20 3d 3e 20 49 44 20 2a 2f 0a    FAIL => ID */.
2dabd 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
2dabe 20 20 46 4f 52 20 3d 3e 20 49 44 20 2a 2f 0a 20    FOR => ID */. 
2dabf 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 49 47    26,  /*     IG
2dac0 4e 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  NORE => ID */.  
2dac1 20 32 36 2c 20 20 2f 2a 20 20 49 4e 49 54 49 41   26,  /*  INITIA
2dac2 4c 4c 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  LLY => ID */.   
2dac3 32 36 2c 20 20 2f 2a 20 20 20 20 49 4e 53 54 45  26,  /*    INSTE
2dac4 41 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  AD => ID */.   2
2dac5 36 2c 20 20 2f 2a 20 20 20 20 4c 49 4b 45 5f 4b  6,  /*    LIKE_K
2dac6 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  W => ID */.   26
2dac7 2c 20 20 2f 2a 20 20 20 20 20 20 4d 41 54 43 48  ,  /*      MATCH
2dac8 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2dac9 20 20 2f 2a 20 20 20 20 20 20 20 20 4b 45 59 20    /*        KEY 
2daca 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2dacb 20 2f 2a 20 20 20 20 20 20 20 20 20 4f 46 20 3d   /*         OF =
2dacc 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2dacd 2f 2a 20 20 20 20 20 4f 46 46 53 45 54 20 3d 3e  /*     OFFSET =>
2dace 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2dacf 2a 20 20 20 20 20 50 52 41 47 4d 41 20 3d 3e 20  *     PRAGMA => 
2dad0 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2dad1 20 20 20 20 20 20 52 41 49 53 45 20 3d 3e 20 49        RAISE => I
2dad2 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2dad3 20 20 20 52 45 50 4c 41 43 45 20 3d 3e 20 49 44     REPLACE => ID
2dad4 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2dad5 20 52 45 53 54 52 49 43 54 20 3d 3e 20 49 44 20   RESTRICT => ID 
2dad6 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2dad7 20 20 20 20 20 52 4f 57 20 3d 3e 20 49 44 20 2a       ROW => ID *
2dad8 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2dad9 54 52 49 47 47 45 52 20 3d 3e 20 49 44 20 2a 2f  TRIGGER => ID */
2dada 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2dadb 56 41 43 55 55 4d 20 3d 3e 20 49 44 20 2a 2f 0a  VACUUM => ID */.
2dadc 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
2dadd 20 56 49 45 57 20 3d 3e 20 49 44 20 2a 2f 0a 20   VIEW => ID */. 
2dade 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 56 49 52    26,  /*    VIR
2dadf 54 55 41 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TUAL => ID */.  
2dae0 20 32 36 2c 20 20 2f 2a 20 20 20 20 52 45 49 4e   26,  /*    REIN
2dae1 44 45 58 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  DEX => ID */.   
2dae2 32 36 2c 20 20 2f 2a 20 20 20 20 20 52 45 4e 41  26,  /*     RENA
2dae3 4d 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ME => ID */.   2
2dae4 36 2c 20 20 2f 2a 20 20 20 43 54 49 4d 45 5f 4b  6,  /*   CTIME_K
2dae5 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30  W => ID */.    0
2dae6 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 4e 59  ,  /*        ANY
2dae7 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2dae8 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2dae9 20 20 4f 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    OR => nothing 
2daea 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2daeb 20 20 20 20 20 41 4e 44 20 3d 3e 20 6e 6f 74 68       AND => noth
2daec 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2daed 2a 20 20 20 20 20 20 20 20 20 49 53 20 3d 3e 20  *         IS => 
2daee 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2daef 2c 20 20 2f 2a 20 20 20 20 42 45 54 57 45 45 4e  ,  /*    BETWEEN
2daf0 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2daf1 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2daf2 20 20 49 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    IN => nothing 
2daf3 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2daf4 20 20 49 53 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68    ISNULL => noth
2daf5 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2daf6 2a 20 20 20 20 4e 4f 54 4e 55 4c 4c 20 3d 3e 20  *    NOTNULL => 
2daf7 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2daf8 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4e 45  ,  /*         NE
2daf9 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2dafa 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2dafb 20 20 45 51 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    EQ => nothing 
2dafc 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2dafd 20 20 20 20 20 20 47 54 20 3d 3e 20 6e 6f 74 68        GT => noth
2dafe 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2daff 2a 20 20 20 20 20 20 20 20 20 4c 45 20 3d 3e 20  *         LE => 
2db00 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db01 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c 54  ,  /*         LT
2db02 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db03 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2db04 20 20 47 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    GE => nothing 
2db05 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db06 20 20 45 53 43 41 50 45 20 3d 3e 20 6e 6f 74 68    ESCAPE => noth
2db07 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db08 2a 20 20 20 20 20 42 49 54 41 4e 44 20 3d 3e 20  *     BITAND => 
2db09 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db0a 2c 20 20 2f 2a 20 20 20 20 20 20 42 49 54 4f 52  ,  /*      BITOR
2db0b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db0c 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 4c 53     0,  /*     LS
2db0d 48 49 46 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  HIFT => nothing 
2db0e 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db0f 20 20 52 53 48 49 46 54 20 3d 3e 20 6e 6f 74 68    RSHIFT => noth
2db10 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db11 2a 20 20 20 20 20 20 20 50 4c 55 53 20 3d 3e 20  *       PLUS => 
2db12 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db13 2c 20 20 2f 2a 20 20 20 20 20 20 4d 49 4e 55 53  ,  /*      MINUS
2db14 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db15 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2db16 53 54 41 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  STAR => nothing 
2db17 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db18 20 20 20 53 4c 41 53 48 20 3d 3e 20 6e 6f 74 68     SLASH => noth
2db19 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db1a 2a 20 20 20 20 20 20 20 20 52 45 4d 20 3d 3e 20  *        REM => 
2db1b 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db1c 2c 20 20 2f 2a 20 20 20 20 20 43 4f 4e 43 41 54  ,  /*     CONCAT
2db1d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db1e 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 43 4f 4c     0,  /*    COL
2db1f 4c 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  LATE => nothing 
2db20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db21 20 20 55 4d 49 4e 55 53 20 3d 3e 20 6e 6f 74 68    UMINUS => noth
2db22 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db23 2a 20 20 20 20 20 20 55 50 4c 55 53 20 3d 3e 20  *      UPLUS => 
2db24 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db25 2c 20 20 2f 2a 20 20 20 20 20 42 49 54 4e 4f 54  ,  /*     BITNOT
2db26 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db27 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 53 54     0,  /*     ST
2db28 52 49 4e 47 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  RING => nothing 
2db29 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db2a 20 4a 4f 49 4e 5f 4b 57 20 3d 3e 20 6e 6f 74 68   JOIN_KW => noth
2db2b 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db2c 2a 20 43 4f 4e 53 54 52 41 49 4e 54 20 3d 3e 20  * CONSTRAINT => 
2db2d 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db2e 2c 20 20 2f 2a 20 20 20 20 44 45 46 41 55 4c 54  ,  /*    DEFAULT
2db2f 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db30 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2db31 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NULL => nothing 
2db32 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db33 20 50 52 49 4d 41 52 59 20 3d 3e 20 6e 6f 74 68   PRIMARY => noth
2db34 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db35 2a 20 20 20 20 20 55 4e 49 51 55 45 20 3d 3e 20  *     UNIQUE => 
2db36 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db37 2c 20 20 2f 2a 20 20 20 20 20 20 43 48 45 43 4b  ,  /*      CHECK
2db38 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db39 20 20 20 30 2c 20 20 2f 2a 20 52 45 46 45 52 45     0,  /* REFERE
2db3a 4e 43 45 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NCES => nothing 
2db3b 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db3c 41 55 54 4f 49 4e 43 52 20 3d 3e 20 6e 6f 74 68  AUTOINCR => noth
2db3d 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db3e 2a 20 20 20 20 20 20 20 20 20 4f 4e 20 3d 3e 20  *         ON => 
2db3f 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db40 2c 20 20 2f 2a 20 20 20 20 20 44 45 4c 45 54 45  ,  /*     DELETE
2db41 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db42 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 55 50     0,  /*     UP
2db43 44 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  DATE => nothing 
2db44 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db45 20 20 49 4e 53 45 52 54 20 3d 3e 20 6e 6f 74 68    INSERT => noth
2db46 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db47 2a 20 20 20 20 20 20 20 20 53 45 54 20 3d 3e 20  *        SET => 
2db48 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db49 2c 20 20 2f 2a 20 44 45 46 45 52 52 41 42 4c 45  ,  /* DEFERRABLE
2db4a 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db4b 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 46 4f 52     0,  /*    FOR
2db4c 45 49 47 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  EIGN => nothing 
2db4d 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db4e 20 20 20 20 44 52 4f 50 20 3d 3e 20 6e 6f 74 68      DROP => noth
2db4f 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db50 2a 20 20 20 20 20 20 55 4e 49 4f 4e 20 3d 3e 20  *      UNION => 
2db51 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db52 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 4c 4c  ,  /*        ALL
2db53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db54 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 45 58     0,  /*     EX
2db55 43 45 50 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  CEPT => nothing 
2db56 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 49  */.    0,  /*  I
2db57 4e 54 45 52 53 45 43 54 20 3d 3e 20 6e 6f 74 68  NTERSECT => noth
2db58 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db59 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3d 3e 20  *     SELECT => 
2db5a 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db5b 2c 20 20 2f 2a 20 20 20 44 49 53 54 49 4e 43 54  ,  /*   DISTINCT
2db5c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db5d 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2db5e 20 44 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   DOT => nothing 
2db5f 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db60 20 20 20 20 46 52 4f 4d 20 3d 3e 20 6e 6f 74 68      FROM => noth
2db61 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db62 2a 20 20 20 20 20 20 20 4a 4f 49 4e 20 3d 3e 20  *       JOIN => 
2db63 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db64 2c 20 20 2f 2a 20 20 20 20 20 20 55 53 49 4e 47  ,  /*      USING
2db65 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db66 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 4f     0,  /*      O
2db67 52 44 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  RDER => nothing 
2db68 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db69 20 20 20 47 52 4f 55 50 20 3d 3e 20 6e 6f 74 68     GROUP => noth
2db6a 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db6b 2a 20 20 20 20 20 48 41 56 49 4e 47 20 3d 3e 20  *     HAVING => 
2db6c 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db6d 2c 20 20 2f 2a 20 20 20 20 20 20 4c 49 4d 49 54  ,  /*      LIMIT
2db6e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db6f 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 57     0,  /*      W
2db70 48 45 52 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  HERE => nothing 
2db71 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db72 20 20 20 20 49 4e 54 4f 20 3d 3e 20 6e 6f 74 68      INTO => noth
2db73 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db74 2a 20 20 20 20 20 56 41 4c 55 45 53 20 3d 3e 20  *     VALUES => 
2db75 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db76 2c 20 20 2f 2a 20 20 20 20 49 4e 54 45 47 45 52  ,  /*    INTEGER
2db77 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db78 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 46     0,  /*      F
2db79 4c 4f 41 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  LOAT => nothing 
2db7a 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db7b 20 20 20 20 42 4c 4f 42 20 3d 3e 20 6e 6f 74 68      BLOB => noth
2db7c 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db7d 2a 20 20 20 52 45 47 49 53 54 45 52 20 3d 3e 20  *   REGISTER => 
2db7e 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db7f 2c 20 20 2f 2a 20 20 20 56 41 52 49 41 42 4c 45  ,  /*   VARIABLE
2db80 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db81 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2db82 43 41 53 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  CASE => nothing 
2db83 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db84 20 20 20 20 57 48 45 4e 20 3d 3e 20 6e 6f 74 68      WHEN => noth
2db85 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db86 2a 20 20 20 20 20 20 20 54 48 45 4e 20 3d 3e 20  *       THEN => 
2db87 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2db88 2c 20 20 2f 2a 20 20 20 20 20 20 20 45 4c 53 45  ,  /*       ELSE
2db89 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2db8a 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 49     0,  /*      I
2db8b 4e 44 45 58 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NDEX => nothing 
2db8c 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2db8d 20 20 20 41 4c 54 45 52 20 3d 3e 20 6e 6f 74 68     ALTER => noth
2db8e 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2db8f 2a 20 20 20 20 20 20 20 20 41 44 44 20 3d 3e 20  *        ADD => 
2db90 6e 6f 74 68 69 6e 67 20 2a 2f 0a 7d 3b 0a 23 65  nothing */.};.#e
2db91 6e 64 69 66 20 2f 2a 20 59 59 46 41 4c 4c 42 41  ndif /* YYFALLBA
2db92 43 4b 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 66 6f  CK */../* The fo
2db93 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
2db94 65 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73  e represents a s
2db95 69 6e 67 6c 65 20 65 6c 65 6d 65 6e 74 20 6f 66  ingle element of
2db96 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 72 27 73   the.** parser's
2db97 20 73 74 61 63 6b 2e 20 20 49 6e 66 6f 72 6d 61   stack.  Informa
2db98 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e 63 6c  tion stored incl
2db99 75 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20  udes:.**.**   + 
2db9a 20 54 68 65 20 73 74 61 74 65 20 6e 75 6d 62 65   The state numbe
2db9b 72 20 66 6f 72 20 74 68 65 20 70 61 72 73 65 72  r for the parser
2db9c 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f   at this level o
2db9d 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a  f the stack..**.
2db9e 2a 2a 20 20 20 2b 20 20 54 68 65 20 76 61 6c 75  **   +  The valu
2db9f 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 73  e of the token s
2dba0 74 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c 65  tored at this le
2dba1 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b  vel of the stack
2dba2 2e 0a 2a 2a 20 20 20 20 20 20 28 49 6e 20 6f 74  ..**      (In ot
2dba3 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 22  her words, the "
2dba4 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e 2e 29 0a 2a  major" token.).*
2dba5 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 73 65  *.**   +  The se
2dba6 6d 61 6e 74 69 63 20 76 61 6c 75 65 20 73 74 6f  mantic value sto
2dba7 72 65 64 20 61 74 20 74 68 69 73 20 6c 65 76 65  red at this leve
2dba8 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 20  l of the stack. 
2dba9 20 54 68 69 73 20 69 73 0a 2a 2a 20 20 20 20 20   This is.**     
2dbaa 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   the information
2dbab 20 75 73 65 64 20 62 79 20 74 68 65 20 61 63 74   used by the act
2dbac 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  ion routines in 
2dbad 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2a 20  the grammar..** 
2dbae 20 20 20 20 20 49 74 20 69 73 20 73 6f 6d 65 74       It is somet
2dbaf 69 6d 65 73 20 63 61 6c 6c 65 64 20 74 68 65 20  imes called the 
2dbb0 22 6d 69 6e 6f 72 22 20 74 6f 6b 65 6e 2e 0a 2a  "minor" token..*
2dbb1 2f 0a 73 74 72 75 63 74 20 79 79 53 74 61 63 6b  /.struct yyStack
2dbb2 45 6e 74 72 79 20 7b 0a 20 20 59 59 41 43 54 49  Entry {.  YYACTI
2dbb3 4f 4e 54 59 50 45 20 73 74 61 74 65 6e 6f 3b 20  ONTYPE stateno; 
2dbb4 20 2f 2a 20 54 68 65 20 73 74 61 74 65 2d 6e 75   /* The state-nu
2dbb5 6d 62 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45  mber */.  YYCODE
2dbb6 54 59 50 45 20 6d 61 6a 6f 72 3b 20 20 20 20 20  TYPE major;     
2dbb7 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f   /* The major to
2dbb8 6b 65 6e 20 76 61 6c 75 65 2e 20 20 54 68 69 73  ken value.  This
2dbb9 20 69 73 20 74 68 65 20 63 6f 64 65 0a 20 20 20   is the code.   
2dbba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dbbb 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
2dbbc 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20 61 74  for the token at
2dbbd 20 74 68 69 73 20 73 74 61 63 6b 20 6c 65 76 65   this stack leve
2dbbe 6c 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59  l */.  YYMINORTY
2dbbf 50 45 20 6d 69 6e 6f 72 3b 20 20 20 20 20 2f 2a  PE minor;     /*
2dbc0 20 54 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69   The user-suppli
2dbc1 65 64 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 76  ed minor token v
2dbc2 61 6c 75 65 2e 20 20 54 68 69 73 0a 20 20 20 20  alue.  This.    
2dbc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dbc4 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 76       ** is the v
2dbc5 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65  alue of the toke
2dbc6 6e 20 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66  n  */.};.typedef
2dbc7 20 73 74 72 75 63 74 20 79 79 53 74 61 63 6b 45   struct yyStackE
2dbc8 6e 74 72 79 20 79 79 53 74 61 63 6b 45 6e 74 72  ntry yyStackEntr
2dbc9 79 3b 0a 0a 2f 2a 20 54 68 65 20 73 74 61 74 65  y;../* The state
2dbca 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 20 69   of the parser i
2dbcb 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 6e  s completely con
2dbcc 74 61 69 6e 65 64 20 69 6e 20 61 6e 20 69 6e 73  tained in an ins
2dbcd 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20  tance of.** the 
2dbce 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
2dbcf 75 72 65 20 2a 2f 0a 73 74 72 75 63 74 20 79 79  ure */.struct yy
2dbd0 50 61 72 73 65 72 20 7b 0a 20 20 69 6e 74 20 79  Parser {.  int y
2dbd1 79 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  yidx;           
2dbd2 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2dbd3 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74  x of top element
2dbd4 20 69 6e 20 73 74 61 63 6b 20 2a 2f 0a 23 69 66   in stack */.#if
2dbd5 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54  def YYTRACKMAXST
2dbd6 41 43 4b 44 45 50 54 48 0a 20 20 69 6e 74 20 79  ACKDEPTH.  int y
2dbd7 79 69 64 78 4d 61 78 3b 20 20 20 20 20 20 20 20  yidxMax;        
2dbd8 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
2dbd9 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 79 79 69  mum value of yyi
2dbda 64 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69  dx */.#endif.  i
2dbdb 6e 74 20 79 79 65 72 72 63 6e 74 3b 20 20 20 20  nt yyerrcnt;    
2dbdc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2dbdd 53 68 69 66 74 73 20 6c 65 66 74 20 62 65 66 6f  Shifts left befo
2dbde 72 65 20 6f 75 74 20 6f 66 20 74 68 65 20 65 72  re out of the er
2dbdf 72 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ror */.  sqlite3
2dbe0 50 61 72 73 65 72 41 52 47 5f 53 44 45 43 4c 20  ParserARG_SDECL 
2dbe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2dbe2 2a 20 41 20 70 6c 61 63 65 20 74 6f 20 68 6f 6c  * A place to hol
2dbe3 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e  d %extra_argumen
2dbe4 74 20 2a 2f 0a 23 69 66 20 59 59 53 54 41 43 4b  t */.#if YYSTACK
2dbe5 44 45 50 54 48 3c 3d 30 0a 20 20 69 6e 74 20 79  DEPTH<=0.  int y
2dbe6 79 73 74 6b 73 7a 3b 20 20 20 20 20 20 20 20 20  ystksz;         
2dbe7 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
2dbe8 65 6e 74 20 73 69 64 65 20 6f 66 20 74 68 65 20  ent side of the 
2dbe9 73 74 61 63 6b 20 2a 2f 0a 20 20 79 79 53 74 61  stack */.  yySta
2dbea 63 6b 45 6e 74 72 79 20 2a 79 79 73 74 61 63 6b  ckEntry *yystack
2dbeb 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
2dbec 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20 2a  parser's stack *
2dbed 2f 0a 23 65 6c 73 65 0a 20 20 79 79 53 74 61 63  /.#else.  yyStac
2dbee 6b 45 6e 74 72 79 20 79 79 73 74 61 63 6b 5b 59  kEntry yystack[Y
2dbef 59 53 54 41 43 4b 44 45 50 54 48 5d 3b 20 20 2f  YSTACKDEPTH];  /
2dbf0 2a 20 54 68 65 20 70 61 72 73 65 72 27 73 20 73  * The parser's s
2dbf1 74 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  tack */.#endif.}
2dbf2 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
2dbf3 20 79 79 50 61 72 73 65 72 20 79 79 50 61 72 73   yyParser yyPars
2dbf4 65 72 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  er;..#ifndef NDE
2dbf5 42 55 47 0a 73 74 61 74 69 63 20 46 49 4c 45 20  BUG.static FILE 
2dbf6 2a 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20 30  *yyTraceFILE = 0
2dbf7 3b 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 79  ;.static char *y
2dbf8 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 30  yTracePrompt = 0
2dbf9 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  ;.#endif /* NDEB
2dbfa 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e  UG */..#ifndef N
2dbfb 44 45 42 55 47 0a 2f 2a 20 0a 2a 2a 20 54 75 72  DEBUG./* .** Tur
2dbfc 6e 20 70 61 72 73 65 72 20 74 72 61 63 69 6e 67  n parser tracing
2dbfd 20 6f 6e 20 62 79 20 67 69 76 69 6e 67 20 61 20   on by giving a 
2dbfe 73 74 72 65 61 6d 20 74 6f 20 77 68 69 63 68 20  stream to which 
2dbff 74 6f 20 77 72 69 74 65 20 74 68 65 20 74 72 61  to write the tra
2dc00 63 65 0a 2a 2a 20 61 6e 64 20 61 20 70 72 6f 6d  ce.** and a prom
2dc01 70 74 20 74 6f 20 70 72 65 66 61 63 65 20 65 61  pt to preface ea
2dc02 63 68 20 74 72 61 63 65 20 6d 65 73 73 61 67 65  ch trace message
2dc03 2e 20 20 54 72 61 63 69 6e 67 20 69 73 20 74 75  .  Tracing is tu
2dc04 72 6e 65 64 20 6f 66 66 0a 2a 2a 20 62 79 20 6d  rned off.** by m
2dc05 61 6b 69 6e 67 20 65 69 74 68 65 72 20 61 72 67  aking either arg
2dc06 75 6d 65 6e 74 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a  ument NULL .**.*
2dc07 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c  * Inputs:.** <ul
2dc08 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 46 49 4c 45  >.** <li> A FILE
2dc09 2a 20 74 6f 20 77 68 69 63 68 20 74 72 61 63 65  * to which trace
2dc0a 20 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62   output should b
2dc0b 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 20 20 20  e written..**   
2dc0c 20 20 20 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e     If NULL, then
2dc0d 20 74 72 61 63 69 6e 67 20 69 73 20 74 75 72 6e   tracing is turn
2dc0e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 6c 69 3e 20  ed off..** <li> 
2dc0f 41 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 20  A prefix string 
2dc10 77 72 69 74 74 65 6e 20 61 74 20 74 68 65 20 62  written at the b
2dc11 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72  eginning of ever
2dc12 79 0a 2a 2a 20 20 20 20 20 20 6c 69 6e 65 20 6f  y.**      line o
2dc13 66 20 74 72 61 63 65 20 6f 75 74 70 75 74 2e 20  f trace output. 
2dc14 20 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74   If NULL, then t
2dc15 72 61 63 69 6e 67 20 69 73 0a 2a 2a 20 20 20 20  racing is.**    
2dc16 20 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a    turned off..**
2dc17 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74   </ul>.**.** Out
2dc18 70 75 74 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a  puts:.** None..*
2dc19 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2dc1a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
2dc1b 73 65 72 54 72 61 63 65 28 46 49 4c 45 20 2a 54  serTrace(FILE *T
2dc1c 72 61 63 65 46 49 4c 45 2c 20 63 68 61 72 20 2a  raceFILE, char *
2dc1d 7a 54 72 61 63 65 50 72 6f 6d 70 74 29 7b 0a 20  zTracePrompt){. 
2dc1e 20 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20 54   yyTraceFILE = T
2dc1f 72 61 63 65 46 49 4c 45 3b 0a 20 20 79 79 54 72  raceFILE;.  yyTr
2dc20 61 63 65 50 72 6f 6d 70 74 20 3d 20 7a 54 72 61  acePrompt = zTra
2dc21 63 65 50 72 6f 6d 70 74 3b 0a 20 20 69 66 28 20  cePrompt;.  if( 
2dc22 79 79 54 72 61 63 65 46 49 4c 45 3d 3d 30 20 29  yyTraceFILE==0 )
2dc23 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d   yyTracePrompt =
2dc24 20 30 3b 0a 20 20 65 6c 73 65 20 69 66 28 20 79   0;.  else if( y
2dc25 79 54 72 61 63 65 50 72 6f 6d 70 74 3d 3d 30 20  yTracePrompt==0 
2dc26 29 20 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20  ) yyTraceFILE = 
2dc27 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  0;.}.#endif /* N
2dc28 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65  DEBUG */..#ifnde
2dc29 66 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20  f NDEBUG./* For 
2dc2a 74 72 61 63 69 6e 67 20 73 68 69 66 74 73 2c 20  tracing shifts, 
2dc2b 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c  the names of all
2dc2c 20 74 65 72 6d 69 6e 61 6c 73 20 61 6e 64 20 6e   terminals and n
2dc2d 6f 6e 74 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20 61  onterminals.** a
2dc2e 72 65 20 72 65 71 75 69 72 65 64 2e 20 20 54 68  re required.  Th
2dc2f 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
2dc30 65 20 73 75 70 70 6c 69 65 73 20 74 68 65 73 65  e supplies these
2dc31 20 6e 61 6d 65 73 20 2a 2f 0a 73 74 61 74 69 63   names */.static
2dc32 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
2dc33 73 74 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 5d  st yyTokenName[]
2dc34 20 3d 20 7b 20 0a 20 20 22 24 22 2c 20 20 20 20   = { .  "$",    
2dc35 20 20 20 20 20 20 20 20 20 22 53 45 4d 49 22 2c           "SEMI",
2dc36 20 20 20 20 20 20 20 20 20 20 22 45 58 50 4c 41            "EXPLA
2dc37 49 4e 22 2c 20 20 20 20 20 20 20 22 51 55 45 52  IN",       "QUER
2dc38 59 22 2c 20 20 20 20 20 20 20 0a 20 20 22 50 4c  Y",       .  "PL
2dc39 41 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22 42  AN",          "B
2dc3a 45 47 49 4e 22 2c 20 20 20 20 20 20 20 20 20 22  EGIN",         "
2dc3b 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c 20 20 20  TRANSACTION",   
2dc3c 22 44 45 46 45 52 52 45 44 22 2c 20 20 20 20 0a  "DEFERRED",    .
2dc3d 20 20 22 49 4d 4d 45 44 49 41 54 45 22 2c 20 20    "IMMEDIATE",  
2dc3e 20 20 20 22 45 58 43 4c 55 53 49 56 45 22 2c 20     "EXCLUSIVE", 
2dc3f 20 20 20 20 22 43 4f 4d 4d 49 54 22 2c 20 20 20      "COMMIT",   
2dc40 20 20 20 20 20 22 45 4e 44 22 2c 20 20 20 20 20       "END",     
2dc41 20 20 20 20 0a 20 20 22 52 4f 4c 4c 42 41 43 4b      .  "ROLLBACK
2dc42 22 2c 20 20 20 20 20 20 22 53 41 56 45 50 4f 49  ",      "SAVEPOI
2dc43 4e 54 22 2c 20 20 20 20 20 22 52 45 4c 45 41 53  NT",     "RELEAS
2dc44 45 22 2c 20 20 20 20 20 20 20 22 54 4f 22 2c 20  E",       "TO", 
2dc45 20 20 20 20 20 20 20 20 20 0a 20 20 22 54 41 42           .  "TAB
2dc46 4c 45 22 2c 20 20 20 20 20 20 20 20 20 22 43 52  LE",         "CR
2dc47 45 41 54 45 22 2c 20 20 20 20 20 20 20 20 22 49  EATE",        "I
2dc48 46 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  F",            "
2dc49 4e 4f 54 22 2c 20 20 20 20 20 20 20 20 20 0a 20  NOT",         . 
2dc4a 20 22 45 58 49 53 54 53 22 2c 20 20 20 20 20 20   "EXISTS",      
2dc4b 20 20 22 54 45 4d 50 22 2c 20 20 20 20 20 20 20    "TEMP",       
2dc4c 20 20 20 22 4c 50 22 2c 20 20 20 20 20 20 20 20     "LP",        
2dc4d 20 20 20 20 22 52 50 22 2c 20 20 20 20 20 20 20      "RP",       
2dc4e 20 20 20 0a 20 20 22 41 53 22 2c 20 20 20 20 20     .  "AS",     
2dc4f 20 20 20 20 20 20 20 22 43 4f 4d 4d 41 22 2c 20         "COMMA", 
2dc50 20 20 20 20 20 20 20 20 22 49 44 22 2c 20 20 20          "ID",   
2dc51 20 20 20 20 20 20 20 20 20 22 49 4e 44 45 58 45           "INDEXE
2dc52 44 22 2c 20 20 20 20 20 0a 20 20 22 41 42 4f 52  D",     .  "ABOR
2dc53 54 22 2c 20 20 20 20 20 20 20 20 20 22 41 46 54  T",         "AFT
2dc54 45 52 22 2c 20 20 20 20 20 20 20 20 20 22 41 4e  ER",         "AN
2dc55 41 4c 59 5a 45 22 2c 20 20 20 20 20 20 20 22 41  ALYZE",       "A
2dc56 53 43 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20  SC",         .  
2dc57 22 41 54 54 41 43 48 22 2c 20 20 20 20 20 20 20  "ATTACH",       
2dc58 20 22 42 45 46 4f 52 45 22 2c 20 20 20 20 20 20   "BEFORE",      
2dc59 20 20 22 42 59 22 2c 20 20 20 20 20 20 20 20 20    "BY",         
2dc5a 20 20 20 22 43 41 53 43 41 44 45 22 2c 20 20 20     "CASCADE",   
2dc5b 20 20 0a 20 20 22 43 41 53 54 22 2c 20 20 20 20    .  "CAST",    
2dc5c 20 20 20 20 20 20 22 43 4f 4c 55 4d 4e 4b 57 22        "COLUMNKW"
2dc5d 2c 20 20 20 20 20 20 22 43 4f 4e 46 4c 49 43 54  ,      "CONFLICT
2dc5e 22 2c 20 20 20 20 20 20 22 44 41 54 41 42 41 53  ",      "DATABAS
2dc5f 45 22 2c 20 20 20 20 0a 20 20 22 44 45 53 43 22  E",    .  "DESC"
2dc60 2c 20 20 20 20 20 20 20 20 20 20 22 44 45 54 41  ,          "DETA
2dc61 43 48 22 2c 20 20 20 20 20 20 20 20 22 45 41 43  CH",        "EAC
2dc62 48 22 2c 20 20 20 20 20 20 20 20 20 20 22 46 41  H",          "FA
2dc63 49 4c 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22  IL",        .  "
2dc64 46 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20  FOR",           
2dc65 22 49 47 4e 4f 52 45 22 2c 20 20 20 20 20 20 20  "IGNORE",       
2dc66 20 22 49 4e 49 54 49 41 4c 4c 59 22 2c 20 20 20   "INITIALLY",   
2dc67 20 20 22 49 4e 53 54 45 41 44 22 2c 20 20 20 20    "INSTEAD",    
2dc68 20 0a 20 20 22 4c 49 4b 45 5f 4b 57 22 2c 20 20   .  "LIKE_KW",  
2dc69 20 20 20 20 20 22 4d 41 54 43 48 22 2c 20 20 20       "MATCH",   
2dc6a 20 20 20 20 20 20 22 4b 45 59 22 2c 20 20 20 20        "KEY",    
2dc6b 20 20 20 20 20 20 20 22 4f 46 22 2c 20 20 20 20         "OF",    
2dc6c 20 20 20 20 20 20 0a 20 20 22 4f 46 46 53 45 54        .  "OFFSET
2dc6d 22 2c 20 20 20 20 20 20 20 20 22 50 52 41 47 4d  ",        "PRAGM
2dc6e 41 22 2c 20 20 20 20 20 20 20 20 22 52 41 49 53  A",        "RAIS
2dc6f 45 22 2c 20 20 20 20 20 20 20 20 20 22 52 45 50  E",         "REP
2dc70 4c 41 43 45 22 2c 20 20 20 20 20 0a 20 20 22 52  LACE",     .  "R
2dc71 45 53 54 52 49 43 54 22 2c 20 20 20 20 20 20 22  ESTRICT",      "
2dc72 52 4f 57 22 2c 20 20 20 20 20 20 20 20 20 20 20  ROW",           
2dc73 22 54 52 49 47 47 45 52 22 2c 20 20 20 20 20 20  "TRIGGER",      
2dc74 20 22 56 41 43 55 55 4d 22 2c 20 20 20 20 20 20   "VACUUM",      
2dc75 0a 20 20 22 56 49 45 57 22 2c 20 20 20 20 20 20  .  "VIEW",      
2dc76 20 20 20 20 22 56 49 52 54 55 41 4c 22 2c 20 20      "VIRTUAL",  
2dc77 20 20 20 20 20 22 52 45 49 4e 44 45 58 22 2c 20       "REINDEX", 
2dc78 20 20 20 20 20 20 22 52 45 4e 41 4d 45 22 2c 20        "RENAME", 
2dc79 20 20 20 20 20 0a 20 20 22 43 54 49 4d 45 5f 4b       .  "CTIME_K
2dc7a 57 22 2c 20 20 20 20 20 20 22 41 4e 59 22 2c 20  W",      "ANY", 
2dc7b 20 20 20 20 20 20 20 20 20 20 22 4f 52 22 2c 20            "OR", 
2dc7c 20 20 20 20 20 20 20 20 20 20 20 22 41 4e 44 22             "AND"
2dc7d 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22 49 53  ,         .  "IS
2dc7e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 42  ",            "B
2dc7f 45 54 57 45 45 4e 22 2c 20 20 20 20 20 20 20 22  ETWEEN",       "
2dc80 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  IN",            
2dc81 22 49 53 4e 55 4c 4c 22 2c 20 20 20 20 20 20 0a  "ISNULL",      .
2dc82 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c 20 20 20 20    "NOTNULL",    
2dc83 20 20 20 22 4e 45 22 2c 20 20 20 20 20 20 20 20     "NE",        
2dc84 20 20 20 20 22 45 51 22 2c 20 20 20 20 20 20 20      "EQ",       
2dc85 20 20 20 20 20 22 47 54 22 2c 20 20 20 20 20 20       "GT",      
2dc86 20 20 20 20 0a 20 20 22 4c 45 22 2c 20 20 20 20      .  "LE",    
2dc87 20 20 20 20 20 20 20 20 22 4c 54 22 2c 20 20 20          "LT",   
2dc88 20 20 20 20 20 20 20 20 20 22 47 45 22 2c 20 20           "GE",  
2dc89 20 20 20 20 20 20 20 20 20 20 22 45 53 43 41 50            "ESCAP
2dc8a 45 22 2c 20 20 20 20 20 20 0a 20 20 22 42 49 54  E",      .  "BIT
2dc8b 41 4e 44 22 2c 20 20 20 20 20 20 20 20 22 42 49  AND",        "BI
2dc8c 54 4f 52 22 2c 20 20 20 20 20 20 20 20 20 22 4c  TOR",         "L
2dc8d 53 48 49 46 54 22 2c 20 20 20 20 20 20 20 20 22  SHIFT",        "
2dc8e 52 53 48 49 46 54 22 2c 20 20 20 20 20 20 0a 20  RSHIFT",      . 
2dc8f 20 22 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20   "PLUS",        
2dc90 20 20 22 4d 49 4e 55 53 22 2c 20 20 20 20 20 20    "MINUS",      
2dc91 20 20 20 22 53 54 41 52 22 2c 20 20 20 20 20 20     "STAR",      
2dc92 20 20 20 20 22 53 4c 41 53 48 22 2c 20 20 20 20      "SLASH",    
2dc93 20 20 20 0a 20 20 22 52 45 4d 22 2c 20 20 20 20     .  "REM",    
2dc94 20 20 20 20 20 20 20 22 43 4f 4e 43 41 54 22 2c         "CONCAT",
2dc95 20 20 20 20 20 20 20 20 22 43 4f 4c 4c 41 54 45          "COLLATE
2dc96 22 2c 20 20 20 20 20 20 20 22 55 4d 49 4e 55 53  ",       "UMINUS
2dc97 22 2c 20 20 20 20 20 20 0a 20 20 22 55 50 4c 55  ",      .  "UPLU
2dc98 53 22 2c 20 20 20 20 20 20 20 20 20 22 42 49 54  S",         "BIT
2dc99 4e 4f 54 22 2c 20 20 20 20 20 20 20 20 22 53 54  NOT",        "ST
2dc9a 52 49 4e 47 22 2c 20 20 20 20 20 20 20 20 22 4a  RING",        "J
2dc9b 4f 49 4e 5f 4b 57 22 2c 20 20 20 20 20 0a 20 20  OIN_KW",     .  
2dc9c 22 43 4f 4e 53 54 52 41 49 4e 54 22 2c 20 20 20  "CONSTRAINT",   
2dc9d 20 22 44 45 46 41 55 4c 54 22 2c 20 20 20 20 20   "DEFAULT",     
2dc9e 20 20 22 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20    "NULL",       
2dc9f 20 20 20 22 50 52 49 4d 41 52 59 22 2c 20 20 20     "PRIMARY",   
2dca0 20 20 0a 20 20 22 55 4e 49 51 55 45 22 2c 20 20    .  "UNIQUE",  
2dca1 20 20 20 20 20 20 22 43 48 45 43 4b 22 2c 20 20        "CHECK",  
2dca2 20 20 20 20 20 20 20 22 52 45 46 45 52 45 4e 43         "REFERENC
2dca3 45 53 22 2c 20 20 20 20 22 41 55 54 4f 49 4e 43  ES",    "AUTOINC
2dca4 52 22 2c 20 20 20 20 0a 20 20 22 4f 4e 22 2c 20  R",    .  "ON", 
2dca5 20 20 20 20 20 20 20 20 20 20 20 22 44 45 4c 45             "DELE
2dca6 54 45 22 2c 20 20 20 20 20 20 20 20 22 55 50 44  TE",        "UPD
2dca7 41 54 45 22 2c 20 20 20 20 20 20 20 20 22 49 4e  ATE",        "IN
2dca8 53 45 52 54 22 2c 20 20 20 20 20 20 0a 20 20 22  SERT",      .  "
2dca9 53 45 54 22 2c 20 20 20 20 20 20 20 20 20 20 20  SET",           
2dcaa 22 44 45 46 45 52 52 41 42 4c 45 22 2c 20 20 20  "DEFERRABLE",   
2dcab 20 22 46 4f 52 45 49 47 4e 22 2c 20 20 20 20 20   "FOREIGN",     
2dcac 20 20 22 44 52 4f 50 22 2c 20 20 20 20 20 20 20    "DROP",       
2dcad 20 0a 20 20 22 55 4e 49 4f 4e 22 2c 20 20 20 20   .  "UNION",    
2dcae 20 20 20 20 20 22 41 4c 4c 22 2c 20 20 20 20 20       "ALL",     
2dcaf 20 20 20 20 20 20 22 45 58 43 45 50 54 22 2c 20        "EXCEPT", 
2dcb0 20 20 20 20 20 20 20 22 49 4e 54 45 52 53 45 43         "INTERSEC
2dcb1 54 22 2c 20 20 20 0a 20 20 22 53 45 4c 45 43 54  T",   .  "SELECT
2dcb2 22 2c 20 20 20 20 20 20 20 20 22 44 49 53 54 49  ",        "DISTI
2dcb3 4e 43 54 22 2c 20 20 20 20 20 20 22 44 4f 54 22  NCT",      "DOT"
2dcb4 2c 20 20 20 20 20 20 20 20 20 20 20 22 46 52 4f  ,           "FRO
2dcb5 4d 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 4a  M",        .  "J
2dcb6 4f 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22  OIN",          "
2dcb7 55 53 49 4e 47 22 2c 20 20 20 20 20 20 20 20 20  USING",         
2dcb8 22 4f 52 44 45 52 22 2c 20 20 20 20 20 20 20 20  "ORDER",        
2dcb9 20 22 47 52 4f 55 50 22 2c 20 20 20 20 20 20 20   "GROUP",       
2dcba 0a 20 20 22 48 41 56 49 4e 47 22 2c 20 20 20 20  .  "HAVING",    
2dcbb 20 20 20 20 22 4c 49 4d 49 54 22 2c 20 20 20 20      "LIMIT",    
2dcbc 20 20 20 20 20 22 57 48 45 52 45 22 2c 20 20 20       "WHERE",   
2dcbd 20 20 20 20 20 20 22 49 4e 54 4f 22 2c 20 20 20        "INTO",   
2dcbe 20 20 20 20 20 0a 20 20 22 56 41 4c 55 45 53 22       .  "VALUES"
2dcbf 2c 20 20 20 20 20 20 20 20 22 49 4e 54 45 47 45  ,        "INTEGE
2dcc0 52 22 2c 20 20 20 20 20 20 20 22 46 4c 4f 41 54  R",       "FLOAT
2dcc1 22 2c 20 20 20 20 20 20 20 20 20 22 42 4c 4f 42  ",         "BLOB
2dcc2 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 52 45  ",        .  "RE
2dcc3 47 49 53 54 45 52 22 2c 20 20 20 20 20 20 22 56  GISTER",      "V
2dcc4 41 52 49 41 42 4c 45 22 2c 20 20 20 20 20 20 22  ARIABLE",      "
2dcc5 43 41 53 45 22 2c 20 20 20 20 20 20 20 20 20 20  CASE",          
2dcc6 22 57 48 45 4e 22 2c 20 20 20 20 20 20 20 20 0a  "WHEN",        .
2dcc7 20 20 22 54 48 45 4e 22 2c 20 20 20 20 20 20 20    "THEN",       
2dcc8 20 20 20 22 45 4c 53 45 22 2c 20 20 20 20 20 20     "ELSE",      
2dcc9 20 20 20 20 22 49 4e 44 45 58 22 2c 20 20 20 20      "INDEX",    
2dcca 20 20 20 20 20 22 41 4c 54 45 52 22 2c 20 20 20       "ALTER",   
2dccb 20 20 20 20 0a 20 20 22 41 44 44 22 2c 20 20 20      .  "ADD",   
2dccc 20 20 20 20 20 20 20 20 22 65 72 72 6f 72 22 2c          "error",
2dccd 20 20 20 20 20 20 20 20 20 22 69 6e 70 75 74 22           "input"
2dcce 2c 20 20 20 20 20 20 20 20 20 22 63 6d 64 6c 69  ,         "cmdli
2dccf 73 74 22 2c 20 20 20 20 20 0a 20 20 22 65 63 6d  st",     .  "ecm
2dcd0 64 22 2c 20 20 20 20 20 20 20 20 20 20 22 65 78  d",          "ex
2dcd1 70 6c 61 69 6e 22 2c 20 20 20 20 20 20 20 22 63  plain",       "c
2dcd2 6d 64 78 22 2c 20 20 20 20 20 20 20 20 20 20 22  mdx",          "
2dcd3 63 6d 64 22 2c 20 20 20 20 20 20 20 20 20 0a 20  cmd",         . 
2dcd4 20 22 74 72 61 6e 73 74 79 70 65 22 2c 20 20 20   "transtype",   
2dcd5 20 20 22 74 72 61 6e 73 5f 6f 70 74 22 2c 20 20    "trans_opt",  
2dcd6 20 20 20 22 6e 6d 22 2c 20 20 20 20 20 20 20 20     "nm",        
2dcd7 20 20 20 20 22 73 61 76 65 70 6f 69 6e 74 5f 6f      "savepoint_o
2dcd8 70 74 22 2c 0a 20 20 22 63 72 65 61 74 65 5f 74  pt",.  "create_t
2dcd9 61 62 6c 65 22 2c 20 20 22 63 72 65 61 74 65 5f  able",  "create_
2dcda 74 61 62 6c 65 5f 61 72 67 73 22 2c 20 20 22 63  table_args",  "c
2dcdb 72 65 61 74 65 6b 77 22 2c 20 20 20 20 20 20 22  reatekw",      "
2dcdc 74 65 6d 70 22 2c 20 20 20 20 20 20 20 20 0a 20  temp",        . 
2dcdd 20 22 69 66 6e 6f 74 65 78 69 73 74 73 22 2c 20   "ifnotexists", 
2dcde 20 20 22 64 62 6e 6d 22 2c 20 20 20 20 20 20 20    "dbnm",       
2dcdf 20 20 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 22 2c     "columnlist",
2dce0 20 20 20 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70      "conslist_op
2dce1 74 22 2c 0a 20 20 22 73 65 6c 65 63 74 22 2c 20  t",.  "select", 
2dce2 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 22 2c         "column",
2dce3 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 69          "columni
2dce4 64 22 2c 20 20 20 20 20 20 22 74 79 70 65 22 2c  d",      "type",
2dce5 20 20 20 20 20 20 20 20 0a 20 20 22 63 61 72 67          .  "carg
2dce6 6c 69 73 74 22 2c 20 20 20 20 20 20 22 69 64 22  list",      "id"
2dce7 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 69 64  ,            "id
2dce8 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 74  s",           "t
2dce9 79 70 65 74 6f 6b 65 6e 22 2c 20 20 20 0a 20 20  ypetoken",   .  
2dcea 22 74 79 70 65 6e 61 6d 65 22 2c 20 20 20 20 20  "typename",     
2dceb 20 22 73 69 67 6e 65 64 22 2c 20 20 20 20 20 20   "signed",      
2dcec 20 20 22 70 6c 75 73 5f 6e 75 6d 22 2c 20 20 20    "plus_num",   
2dced 20 20 20 22 6d 69 6e 75 73 5f 6e 75 6d 22 2c 20     "minus_num", 
2dcee 20 20 0a 20 20 22 63 61 72 67 22 2c 20 20 20 20    .  "carg",    
2dcef 20 20 20 20 20 20 22 63 63 6f 6e 73 22 2c 20 20        "ccons",  
2dcf0 20 20 20 20 20 20 20 22 74 65 72 6d 22 2c 20 20         "term",  
2dcf1 20 20 20 20 20 20 20 20 22 65 78 70 72 22 2c 20          "expr", 
2dcf2 20 20 20 20 20 20 20 0a 20 20 22 6f 6e 63 6f 6e         .  "oncon
2dcf3 66 22 2c 20 20 20 20 20 20 20 20 22 73 6f 72 74  f",        "sort
2dcf4 6f 72 64 65 72 22 2c 20 20 20 20 20 22 61 75 74  order",     "aut
2dcf5 6f 69 6e 63 22 2c 20 20 20 20 20 20 20 22 69 64  oinc",       "id
2dcf6 78 6c 69 73 74 5f 6f 70 74 22 2c 20 0a 20 20 22  xlist_opt", .  "
2dcf7 72 65 66 61 72 67 73 22 2c 20 20 20 20 20 20 20  refargs",       
2dcf8 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65  "defer_subclause
2dcf9 22 2c 20 20 22 72 65 66 61 72 67 22 2c 20 20 20  ",  "refarg",   
2dcfa 20 20 20 20 20 22 72 65 66 61 63 74 22 2c 20 20       "refact",  
2dcfb 20 20 20 20 0a 20 20 22 69 6e 69 74 5f 64 65 66      .  "init_def
2dcfc 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c  erred_pred_opt",
2dcfd 20 20 22 63 6f 6e 73 6c 69 73 74 22 2c 20 20 20    "conslist",   
2dcfe 20 20 20 22 74 63 6f 6e 73 22 2c 20 20 20 20 20     "tcons",     
2dcff 20 20 20 20 22 69 64 78 6c 69 73 74 22 2c 20 20      "idxlist",  
2dd00 20 20 20 0a 20 20 22 64 65 66 65 72 5f 73 75 62     .  "defer_sub
2dd01 63 6c 61 75 73 65 5f 6f 70 74 22 2c 20 20 22 6f  clause_opt",  "o
2dd02 72 63 6f 6e 66 22 2c 20 20 20 20 20 20 20 20 22  rconf",        "
2dd03 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 20 20 20  resolvetype",   
2dd04 22 72 61 69 73 65 74 79 70 65 22 2c 20 20 20 0a  "raisetype",   .
2dd05 20 20 22 69 66 65 78 69 73 74 73 22 2c 20 20 20    "ifexists",   
2dd06 20 20 20 22 66 75 6c 6c 6e 61 6d 65 22 2c 20 20     "fullname",  
2dd07 20 20 20 20 22 6f 6e 65 73 65 6c 65 63 74 22 2c      "oneselect",
2dd08 20 20 20 20 20 22 6d 75 6c 74 69 73 65 6c 65 63       "multiselec
2dd09 74 5f 6f 70 22 2c 0a 20 20 22 64 69 73 74 69 6e  t_op",.  "distin
2dd0a 63 74 22 2c 20 20 20 20 20 20 22 73 65 6c 63 6f  ct",      "selco
2dd0b 6c 6c 69 73 74 22 2c 20 20 20 20 22 66 72 6f 6d  llist",    "from
2dd0c 22 2c 20 20 20 20 20 20 20 20 20 20 22 77 68 65  ",          "whe
2dd0d 72 65 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 67  re_opt",   .  "g
2dd0e 72 6f 75 70 62 79 5f 6f 70 74 22 2c 20 20 20 22  roupby_opt",   "
2dd0f 68 61 76 69 6e 67 5f 6f 70 74 22 2c 20 20 20 20  having_opt",    
2dd10 22 6f 72 64 65 72 62 79 5f 6f 70 74 22 2c 20 20  "orderby_opt",  
2dd11 20 22 6c 69 6d 69 74 5f 6f 70 74 22 2c 20 20 20   "limit_opt",   
2dd12 0a 20 20 22 73 63 6c 70 22 2c 20 20 20 20 20 20  .  "sclp",      
2dd13 20 20 20 20 22 61 73 22 2c 20 20 20 20 20 20 20      "as",       
2dd14 20 20 20 20 20 22 73 65 6c 74 61 62 6c 69 73 74       "seltablist
2dd15 22 2c 20 20 20 20 22 73 74 6c 5f 70 72 65 66 69  ",    "stl_prefi
2dd16 78 22 2c 20 20 0a 20 20 22 6a 6f 69 6e 6f 70 22  x",  .  "joinop"
2dd17 2c 20 20 20 20 20 20 20 20 22 69 6e 64 65 78 65  ,        "indexe
2dd18 64 5f 6f 70 74 22 2c 20 20 20 22 6f 6e 5f 6f 70  d_opt",   "on_op
2dd19 74 22 2c 20 20 20 20 20 20 20 20 22 75 73 69 6e  t",        "usin
2dd1a 67 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 6a 6f  g_opt",   .  "jo
2dd1b 69 6e 6f 70 32 22 2c 20 20 20 20 20 20 20 22 69  inop2",       "i
2dd1c 6e 73 63 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22  nscollist",    "
2dd1d 73 6f 72 74 6c 69 73 74 22 2c 20 20 20 20 20 20  sortlist",      
2dd1e 22 73 6f 72 74 69 74 65 6d 22 2c 20 20 20 20 0a  "sortitem",    .
2dd1f 20 20 22 6e 65 78 70 72 6c 69 73 74 22 2c 20 20    "nexprlist",  
2dd20 20 20 20 22 73 65 74 6c 69 73 74 22 2c 20 20 20     "setlist",   
2dd21 20 20 20 20 22 69 6e 73 65 72 74 5f 63 6d 64 22      "insert_cmd"
2dd22 2c 20 20 20 20 22 69 6e 73 63 6f 6c 6c 69 73 74  ,    "inscollist
2dd23 5f 6f 70 74 22 2c 0a 20 20 22 69 74 65 6d 6c 69  _opt",.  "itemli
2dd24 73 74 22 2c 20 20 20 20 20 20 22 65 78 70 72 6c  st",      "exprl
2dd25 69 73 74 22 2c 20 20 20 20 20 20 22 6c 69 6b 65  ist",      "like
2dd26 6f 70 22 2c 20 20 20 20 20 20 20 20 22 65 73 63  op",        "esc
2dd27 61 70 65 22 2c 20 20 20 20 20 20 0a 20 20 22 62  ape",      .  "b
2dd28 65 74 77 65 65 6e 5f 6f 70 22 2c 20 20 20 20 22  etween_op",    "
2dd29 69 6e 5f 6f 70 22 2c 20 20 20 20 20 20 20 20 20  in_op",         
2dd2a 22 63 61 73 65 5f 6f 70 65 72 61 6e 64 22 2c 20  "case_operand", 
2dd2b 20 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74 22   "case_exprlist"
2dd2c 2c 0a 20 20 22 63 61 73 65 5f 65 6c 73 65 22 2c  ,.  "case_else",
2dd2d 20 20 20 20 20 22 75 6e 69 71 75 65 66 6c 61 67       "uniqueflag
2dd2e 22 2c 20 20 20 20 22 63 6f 6c 6c 61 74 65 22 2c  ",    "collate",
2dd2f 20 20 20 20 20 20 20 22 6e 6d 6e 75 6d 22 2c 20         "nmnum", 
2dd30 20 20 20 20 20 20 0a 20 20 22 70 6c 75 73 5f 6f        .  "plus_o
2dd31 70 74 22 2c 20 20 20 20 20 20 22 6e 75 6d 62 65  pt",      "numbe
2dd32 72 22 2c 20 20 20 20 20 20 20 20 22 74 72 69 67  r",        "trig
2dd33 67 65 72 5f 64 65 63 6c 22 2c 20 20 22 74 72 69  ger_decl",  "tri
2dd34 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 22 2c 0a  gger_cmd_list",.
2dd35 20 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 22    "trigger_time"
2dd36 2c 20 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e  ,  "trigger_even
2dd37 74 22 2c 20 20 22 66 6f 72 65 61 63 68 5f 63 6c  t",  "foreach_cl
2dd38 61 75 73 65 22 2c 20 20 22 77 68 65 6e 5f 63 6c  ause",  "when_cl
2dd39 61 75 73 65 22 2c 20 0a 20 20 22 74 72 69 67 67  ause", .  "trigg
2dd3a 65 72 5f 63 6d 64 22 2c 20 20 20 22 64 61 74 61  er_cmd",   "data
2dd3b 62 61 73 65 5f 6b 77 5f 6f 70 74 22 2c 20 20 22  base_kw_opt",  "
2dd3c 6b 65 79 5f 6f 70 74 22 2c 20 20 20 20 20 20 20  key_opt",       
2dd3d 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c  "add_column_full
2dd3e 6e 61 6d 65 22 2c 0a 20 20 22 6b 77 63 6f 6c 75  name",.  "kwcolu
2dd3f 6d 6e 5f 6f 70 74 22 2c 20 20 22 63 72 65 61 74  mn_opt",  "creat
2dd40 65 5f 76 74 61 62 22 2c 20 20 20 22 76 74 61 62  e_vtab",   "vtab
2dd41 61 72 67 6c 69 73 74 22 2c 20 20 20 22 76 74 61  arglist",   "vta
2dd42 62 61 72 67 22 2c 20 20 20 20 20 0a 20 20 22 76  barg",     .  "v
2dd43 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 20 20 22  tabargtoken",  "
2dd44 6c 70 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  lp",            
2dd45 22 61 6e 79 6c 69 73 74 22 2c 20 20 20 20 20 0a  "anylist",     .
2dd46 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45  };.#endif /* NDE
2dd47 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BUG */..#ifndef 
2dd48 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72  NDEBUG./* For tr
2dd49 61 63 69 6e 67 20 72 65 64 75 63 65 20 61 63 74  acing reduce act
2dd4a 69 6f 6e 73 2c 20 74 68 65 20 6e 61 6d 65 73 20  ions, the names 
2dd4b 6f 66 20 61 6c 6c 20 72 75 6c 65 73 20 61 72 65  of all rules are
2dd4c 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74   required..*/.st
2dd4d 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2dd4e 2a 63 6f 6e 73 74 20 79 79 52 75 6c 65 4e 61 6d  *const yyRuleNam
2dd4f 65 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 30 20  e[] = {. /*   0 
2dd50 2a 2f 20 22 69 6e 70 75 74 20 3a 3a 3d 20 63 6d  */ "input ::= cm
2dd51 64 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 20 31 20  dlist",. /*   1 
2dd52 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a 3a 3d 20  */ "cmdlist ::= 
2dd53 63 6d 64 6c 69 73 74 20 65 63 6d 64 22 2c 0a 20  cmdlist ecmd",. 
2dd54 2f 2a 20 20 20 32 20 2a 2f 20 22 63 6d 64 6c 69  /*   2 */ "cmdli
2dd55 73 74 20 3a 3a 3d 20 65 63 6d 64 22 2c 0a 20 2f  st ::= ecmd",. /
2dd56 2a 20 20 20 33 20 2a 2f 20 22 65 63 6d 64 20 3a  *   3 */ "ecmd :
2dd57 3a 3d 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20  := SEMI",. /*   
2dd58 34 20 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d 20 65  4 */ "ecmd ::= e
2dd59 78 70 6c 61 69 6e 20 63 6d 64 78 20 53 45 4d 49  xplain cmdx SEMI
2dd5a 22 2c 0a 20 2f 2a 20 20 20 35 20 2a 2f 20 22 65  ",. /*   5 */ "e
2dd5b 78 70 6c 61 69 6e 20 3a 3a 3d 22 2c 0a 20 2f 2a  xplain ::=",. /*
2dd5c 20 20 20 36 20 2a 2f 20 22 65 78 70 6c 61 69 6e     6 */ "explain
2dd5d 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 22 2c 0a 20   ::= EXPLAIN",. 
2dd5e 2f 2a 20 20 20 37 20 2a 2f 20 22 65 78 70 6c 61  /*   7 */ "expla
2dd5f 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 51  in ::= EXPLAIN Q
2dd60 55 45 52 59 20 50 4c 41 4e 22 2c 0a 20 2f 2a 20  UERY PLAN",. /* 
2dd61 20 20 38 20 2a 2f 20 22 63 6d 64 78 20 3a 3a 3d    8 */ "cmdx ::=
2dd62 20 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 39 20 2a   cmd",. /*   9 *
2dd63 2f 20 22 63 6d 64 20 3a 3a 3d 20 42 45 47 49 4e  / "cmd ::= BEGIN
2dd64 20 74 72 61 6e 73 74 79 70 65 20 74 72 61 6e 73   transtype trans
2dd65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 30 20 2a  _opt",. /*  10 *
2dd66 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d  / "trans_opt ::=
2dd67 22 2c 0a 20 2f 2a 20 20 31 31 20 2a 2f 20 22 74  ",. /*  11 */ "t
2dd68 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41  rans_opt ::= TRA
2dd69 4e 53 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a 20 20  NSACTION",. /*  
2dd6a 31 32 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74  12 */ "trans_opt
2dd6b 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e   ::= TRANSACTION
2dd6c 20 6e 6d 22 2c 0a 20 2f 2a 20 20 31 33 20 2a 2f   nm",. /*  13 */
2dd6d 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 22   "transtype ::="
2dd6e 2c 0a 20 2f 2a 20 20 31 34 20 2a 2f 20 22 74 72  ,. /*  14 */ "tr
2dd6f 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44 45 46 45  anstype ::= DEFE
2dd70 52 52 45 44 22 2c 0a 20 2f 2a 20 20 31 35 20 2a  RRED",. /*  15 *
2dd71 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d  / "transtype ::=
2dd72 20 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a   IMMEDIATE",. /*
2dd73 20 20 31 36 20 2a 2f 20 22 74 72 61 6e 73 74 79    16 */ "transty
2dd74 70 65 20 3a 3a 3d 20 45 58 43 4c 55 53 49 56 45  pe ::= EXCLUSIVE
2dd75 22 2c 0a 20 2f 2a 20 20 31 37 20 2a 2f 20 22 63  ",. /*  17 */ "c
2dd76 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72  md ::= COMMIT tr
2dd77 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31  ans_opt",. /*  1
2dd78 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 45 4e  8 */ "cmd ::= EN
2dd79 44 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f  D trans_opt",. /
2dd7a 2a 20 20 31 39 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  19 */ "cmd ::
2dd7b 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73  = ROLLBACK trans
2dd7c 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 32 30 20 2a  _opt",. /*  20 *
2dd7d 2f 20 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74  / "savepoint_opt
2dd7e 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 22 2c   ::= SAVEPOINT",
2dd7f 0a 20 2f 2a 20 20 32 31 20 2a 2f 20 22 73 61 76  . /*  21 */ "sav
2dd80 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 22 2c  epoint_opt ::=",
2dd81 0a 20 2f 2a 20 20 32 32 20 2a 2f 20 22 63 6d 64  . /*  22 */ "cmd
2dd82 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 6e   ::= SAVEPOINT n
2dd83 6d 22 2c 0a 20 2f 2a 20 20 32 33 20 2a 2f 20 22  m",. /*  23 */ "
2dd84 63 6d 64 20 3a 3a 3d 20 52 45 4c 45 41 53 45 20  cmd ::= RELEASE 
2dd85 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d  savepoint_opt nm
2dd86 22 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f 20 22 63  ",. /*  24 */ "c
2dd87 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20  md ::= ROLLBACK 
2dd88 74 72 61 6e 73 5f 6f 70 74 20 54 4f 20 73 61 76  trans_opt TO sav
2dd89 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c 0a  epoint_opt nm",.
2dd8a 20 2f 2a 20 20 32 35 20 2a 2f 20 22 63 6d 64 20   /*  25 */ "cmd 
2dd8b 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65  ::= create_table
2dd8c 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72   create_table_ar
2dd8d 67 73 22 2c 0a 20 2f 2a 20 20 32 36 20 2a 2f 20  gs",. /*  26 */ 
2dd8e 22 63 72 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a  "create_table ::
2dd8f 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20  = createkw temp 
2dd90 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74  TABLE ifnotexist
2dd91 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20  s nm dbnm",. /* 
2dd92 20 32 37 20 2a 2f 20 22 63 72 65 61 74 65 6b 77   27 */ "createkw
2dd93 20 3a 3a 3d 20 43 52 45 41 54 45 22 2c 0a 20 2f   ::= CREATE",. /
2dd94 2a 20 20 32 38 20 2a 2f 20 22 69 66 6e 6f 74 65  *  28 */ "ifnote
2dd95 78 69 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  xists ::=",. /* 
2dd96 20 32 39 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69   29 */ "ifnotexi
2dd97 73 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45  sts ::= IF NOT E
2dd98 58 49 53 54 53 22 2c 0a 20 2f 2a 20 20 33 30 20  XISTS",. /*  30 
2dd99 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d 20 54 45 4d  */ "temp ::= TEM
2dd9a 50 22 2c 0a 20 2f 2a 20 20 33 31 20 2a 2f 20 22  P",. /*  31 */ "
2dd9b 74 65 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  temp ::=",. /*  
2dd9c 33 32 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61  32 */ "create_ta
2dd9d 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20  ble_args ::= LP 
2dd9e 63 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c  columnlist consl
2dd9f 69 73 74 5f 6f 70 74 20 52 50 22 2c 0a 20 2f 2a  ist_opt RP",. /*
2dda0 20 20 33 33 20 2a 2f 20 22 63 72 65 61 74 65 5f    33 */ "create_
2dda1 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41  table_args ::= A
2dda2 53 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 20  S select",. /*  
2dda3 33 34 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73  34 */ "columnlis
2dda4 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74  t ::= columnlist
2dda5 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22 2c 0a   COMMA column",.
2dda6 20 2f 2a 20 20 33 35 20 2a 2f 20 22 63 6f 6c 75   /*  35 */ "colu
2dda7 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d  mnlist ::= colum
2dda8 6e 22 2c 0a 20 2f 2a 20 20 33 36 20 2a 2f 20 22  n",. /*  36 */ "
2dda9 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d  column ::= colum
2ddaa 6e 69 64 20 74 79 70 65 20 63 61 72 67 6c 69 73  nid type carglis
2ddab 74 22 2c 0a 20 2f 2a 20 20 33 37 20 2a 2f 20 22  t",. /*  37 */ "
2ddac 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 22  columnid ::= nm"
2ddad 2c 0a 20 2f 2a 20 20 33 38 20 2a 2f 20 22 69 64  ,. /*  38 */ "id
2ddae 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20 33   ::= ID",. /*  3
2ddaf 39 20 2a 2f 20 22 69 64 20 3a 3a 3d 20 49 4e 44  9 */ "id ::= IND
2ddb0 45 58 45 44 22 2c 0a 20 2f 2a 20 20 34 30 20 2a  EXED",. /*  40 *
2ddb1 2f 20 22 69 64 73 20 3a 3a 3d 20 49 44 7c 53 54  / "ids ::= ID|ST
2ddb2 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 34 31 20 2a  RING",. /*  41 *
2ddb3 2f 20 22 6e 6d 20 3a 3a 3d 20 69 64 22 2c 0a 20  / "nm ::= id",. 
2ddb4 2f 2a 20 20 34 32 20 2a 2f 20 22 6e 6d 20 3a 3a  /*  42 */ "nm ::
2ddb5 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20  = STRING",. /*  
2ddb6 34 33 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a 4f  43 */ "nm ::= JO
2ddb7 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 20 34 34 20  IN_KW",. /*  44 
2ddb8 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 22 2c 0a 20  */ "type ::=",. 
2ddb9 2f 2a 20 20 34 35 20 2a 2f 20 22 74 79 70 65 20  /*  45 */ "type 
2ddba 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 22 2c 0a  ::= typetoken",.
2ddbb 20 2f 2a 20 20 34 36 20 2a 2f 20 22 74 79 70 65   /*  46 */ "type
2ddbc 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61  token ::= typena
2ddbd 6d 65 22 2c 0a 20 2f 2a 20 20 34 37 20 2a 2f 20  me",. /*  47 */ 
2ddbe 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74  "typetoken ::= t
2ddbf 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65  ypename LP signe
2ddc0 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 38 20 2a  d RP",. /*  48 *
2ddc1 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d  / "typetoken ::=
2ddc2 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67   typename LP sig
2ddc3 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67 6e 65 64  ned COMMA signed
2ddc4 20 52 50 22 2c 0a 20 2f 2a 20 20 34 39 20 2a 2f   RP",. /*  49 */
2ddc5 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 69   "typename ::= i
2ddc6 64 73 22 2c 0a 20 2f 2a 20 20 35 30 20 2a 2f 20  ds",. /*  50 */ 
2ddc7 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79  "typename ::= ty
2ddc8 70 65 6e 61 6d 65 20 69 64 73 22 2c 0a 20 2f 2a  pename ids",. /*
2ddc9 20 20 35 31 20 2a 2f 20 22 73 69 67 6e 65 64 20    51 */ "signed 
2ddca 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20  ::= plus_num",. 
2ddcb 2f 2a 20 20 35 32 20 2a 2f 20 22 73 69 67 6e 65  /*  52 */ "signe
2ddcc 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d 22  d ::= minus_num"
2ddcd 2c 0a 20 2f 2a 20 20 35 33 20 2a 2f 20 22 63 61  ,. /*  53 */ "ca
2ddce 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c  rglist ::= cargl
2ddcf 69 73 74 20 63 61 72 67 22 2c 0a 20 2f 2a 20 20  ist carg",. /*  
2ddd0 35 34 20 2a 2f 20 22 63 61 72 67 6c 69 73 74 20  54 */ "carglist 
2ddd1 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 35 35 20 2a 2f  ::=",. /*  55 */
2ddd2 20 22 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54   "carg ::= CONST
2ddd3 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 22 2c  RAINT nm ccons",
2ddd4 0a 20 2f 2a 20 20 35 36 20 2a 2f 20 22 63 61 72  . /*  56 */ "car
2ddd5 67 20 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a 20 2f  g ::= ccons",. /
2ddd6 2a 20 20 35 37 20 2a 2f 20 22 63 63 6f 6e 73 20  *  57 */ "ccons 
2ddd7 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72 6d  ::= DEFAULT term
2ddd8 22 2c 0a 20 2f 2a 20 20 35 38 20 2a 2f 20 22 63  ",. /*  58 */ "c
2ddd9 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54  cons ::= DEFAULT
2ddda 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f   LP expr RP",. /
2dddb 2a 20 20 35 39 20 2a 2f 20 22 63 63 6f 6e 73 20  *  59 */ "ccons 
2dddc 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55 53  ::= DEFAULT PLUS
2dddd 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 36 30 20   term",. /*  60 
2ddde 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45  */ "ccons ::= DE
2dddf 46 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72 6d  FAULT MINUS term
2dde0 22 2c 0a 20 2f 2a 20 20 36 31 20 2a 2f 20 22 63  ",. /*  61 */ "c
2dde1 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54  cons ::= DEFAULT
2dde2 20 69 64 22 2c 0a 20 2f 2a 20 20 36 32 20 2a 2f   id",. /*  62 */
2dde3 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c 4c   "ccons ::= NULL
2dde4 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36   onconf",. /*  6
2dde5 33 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  3 */ "ccons ::= 
2dde6 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 22  NOT NULL onconf"
2dde7 2c 0a 20 2f 2a 20 20 36 34 20 2a 2f 20 22 63 63  ,. /*  64 */ "cc
2dde8 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20  ons ::= PRIMARY 
2dde9 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f 6e  KEY sortorder on
2ddea 63 6f 6e 66 20 61 75 74 6f 69 6e 63 22 2c 0a 20  conf autoinc",. 
2ddeb 2f 2a 20 20 36 35 20 2a 2f 20 22 63 63 6f 6e 73  /*  65 */ "ccons
2ddec 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63 6f   ::= UNIQUE onco
2dded 6e 66 22 2c 0a 20 2f 2a 20 20 36 36 20 2a 2f 20  nf",. /*  66 */ 
2ddee 22 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b  "ccons ::= CHECK
2ddef 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f   LP expr RP",. /
2ddf0 2a 20 20 36 37 20 2a 2f 20 22 63 63 6f 6e 73 20  *  67 */ "ccons 
2ddf1 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53 20 6e  ::= REFERENCES n
2ddf2 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72 65  m idxlist_opt re
2ddf3 66 61 72 67 73 22 2c 0a 20 2f 2a 20 20 36 38 20  fargs",. /*  68 
2ddf4 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 64 65  */ "ccons ::= de
2ddf5 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a  fer_subclause",.
2ddf6 20 2f 2a 20 20 36 39 20 2a 2f 20 22 63 63 6f 6e   /*  69 */ "ccon
2ddf7 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64  s ::= COLLATE id
2ddf8 73 22 2c 0a 20 2f 2a 20 20 37 30 20 2a 2f 20 22  s",. /*  70 */ "
2ddf9 61 75 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a 20 2f  autoinc ::=",. /
2ddfa 2a 20 20 37 31 20 2a 2f 20 22 61 75 74 6f 69 6e  *  71 */ "autoin
2ddfb 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 22 2c  c ::= AUTOINCR",
2ddfc 0a 20 2f 2a 20 20 37 32 20 2a 2f 20 22 72 65 66  . /*  72 */ "ref
2ddfd 61 72 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  args ::=",. /*  
2ddfe 37 33 20 2a 2f 20 22 72 65 66 61 72 67 73 20 3a  73 */ "refargs :
2ddff 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66 61 72  := refargs refar
2de00 67 22 2c 0a 20 2f 2a 20 20 37 34 20 2a 2f 20 22  g",. /*  74 */ "
2de01 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48  refarg ::= MATCH
2de02 20 6e 6d 22 2c 0a 20 2f 2a 20 20 37 35 20 2a 2f   nm",. /*  75 */
2de03 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20   "refarg ::= ON 
2de04 44 45 4c 45 54 45 20 72 65 66 61 63 74 22 2c 0a  DELETE refact",.
2de05 20 2f 2a 20 20 37 36 20 2a 2f 20 22 72 65 66 61   /*  76 */ "refa
2de06 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45  rg ::= ON UPDATE
2de07 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20 37   refact",. /*  7
2de08 37 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d  7 */ "refarg ::=
2de09 20 4f 4e 20 49 4e 53 45 52 54 20 72 65 66 61 63   ON INSERT refac
2de0a 74 22 2c 0a 20 2f 2a 20 20 37 38 20 2a 2f 20 22  t",. /*  78 */ "
2de0b 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 4e  refact ::= SET N
2de0c 55 4c 4c 22 2c 0a 20 2f 2a 20 20 37 39 20 2a 2f  ULL",. /*  79 */
2de0d 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54   "refact ::= SET
2de0e 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a 20 20   DEFAULT",. /*  
2de0f 38 30 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a  80 */ "refact ::
2de10 3d 20 43 41 53 43 41 44 45 22 2c 0a 20 2f 2a 20  = CASCADE",. /* 
2de11 20 38 31 20 2a 2f 20 22 72 65 66 61 63 74 20 3a   81 */ "refact :
2de12 3a 3d 20 52 45 53 54 52 49 43 54 22 2c 0a 20 2f  := RESTRICT",. /
2de13 2a 20 20 38 32 20 2a 2f 20 22 64 65 66 65 72 5f  *  82 */ "defer_
2de14 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f  subclause ::= NO
2de15 54 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  T DEFERRABLE ini
2de16 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
2de17 6f 70 74 22 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f  opt",. /*  83 */
2de18 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
2de19 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c 45  e ::= DEFERRABLE
2de1a 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
2de1b 72 65 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 38  red_opt",. /*  8
2de1c 34 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65 72  4 */ "init_defer
2de1d 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d  red_pred_opt ::=
2de1e 22 2c 0a 20 2f 2a 20 20 38 35 20 2a 2f 20 22 69  ",. /*  85 */ "i
2de1f 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
2de20 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41  d_opt ::= INITIA
2de21 4c 4c 59 20 44 45 46 45 52 52 45 44 22 2c 0a 20  LLY DEFERRED",. 
2de22 2f 2a 20 20 38 36 20 2a 2f 20 22 69 6e 69 74 5f  /*  86 */ "init_
2de23 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
2de24 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20  t ::= INITIALLY 
2de25 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20  IMMEDIATE",. /* 
2de26 20 38 37 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74   87 */ "conslist
2de27 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  _opt ::=",. /*  
2de28 38 38 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f  88 */ "conslist_
2de29 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f  opt ::= COMMA co
2de2a 6e 73 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 38 39  nslist",. /*  89
2de2b 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a   */ "conslist ::
2de2c 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41  = conslist COMMA
2de2d 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39 30   tcons",. /*  90
2de2e 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a   */ "conslist ::
2de2f 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73  = conslist tcons
2de30 22 2c 0a 20 2f 2a 20 20 39 31 20 2a 2f 20 22 63  ",. /*  91 */ "c
2de31 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e  onslist ::= tcon
2de32 73 22 2c 0a 20 2f 2a 20 20 39 32 20 2a 2f 20 22  s",. /*  92 */ "
2de33 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52  tcons ::= CONSTR
2de34 41 49 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 39  AINT nm",. /*  9
2de35 33 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20  3 */ "tcons ::= 
2de36 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69  PRIMARY KEY LP i
2de37 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52  dxlist autoinc R
2de38 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  P onconf",. /*  
2de39 39 34 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  94 */ "tcons ::=
2de3a 20 55 4e 49 51 55 45 20 4c 50 20 69 64 78 6c 69   UNIQUE LP idxli
2de3b 73 74 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20  st RP onconf",. 
2de3c 2f 2a 20 20 39 35 20 2a 2f 20 22 74 63 6f 6e 73  /*  95 */ "tcons
2de3d 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78   ::= CHECK LP ex
2de3e 70 72 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20  pr RP onconf",. 
2de3f 2f 2a 20 20 39 36 20 2a 2f 20 22 74 63 6f 6e 73  /*  96 */ "tcons
2de40 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59   ::= FOREIGN KEY
2de41 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 52   LP idxlist RP R
2de42 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78  EFERENCES nm idx
2de43 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73  list_opt refargs
2de44 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
2de45 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 39 37 20 2a  _opt",. /*  97 *
2de46 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75  / "defer_subclau
2de47 73 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  se_opt ::=",. /*
2de48 20 20 39 38 20 2a 2f 20 22 64 65 66 65 72 5f 73    98 */ "defer_s
2de49 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d  ubclause_opt ::=
2de4a 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
2de4b 22 2c 0a 20 2f 2a 20 20 39 39 20 2a 2f 20 22 6f  ",. /*  99 */ "o
2de4c 6e 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  nconf ::=",. /* 
2de4d 31 30 30 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a  100 */ "onconf :
2de4e 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72  := ON CONFLICT r
2de4f 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a  esolvetype",. /*
2de50 20 31 30 31 20 2a 2f 20 22 6f 72 63 6f 6e 66 20   101 */ "orconf 
2de51 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 32 20 2a 2f  ::=",. /* 102 */
2de52 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20   "orconf ::= OR 
2de53 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f  resolvetype",. /
2de54 2a 20 31 30 33 20 2a 2f 20 22 72 65 73 6f 6c 76  * 103 */ "resolv
2de55 65 74 79 70 65 20 3a 3a 3d 20 72 61 69 73 65 74  etype ::= raiset
2de56 79 70 65 22 2c 0a 20 2f 2a 20 31 30 34 20 2a 2f  ype",. /* 104 */
2de57 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a   "resolvetype ::
2de58 3d 20 49 47 4e 4f 52 45 22 2c 0a 20 2f 2a 20 31  = IGNORE",. /* 1
2de59 30 35 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79  05 */ "resolvety
2de5a 70 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c  pe ::= REPLACE",
2de5b 0a 20 2f 2a 20 31 30 36 20 2a 2f 20 22 63 6d 64  . /* 106 */ "cmd
2de5c 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c 45 20   ::= DROP TABLE 
2de5d 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
2de5e 65 22 2c 0a 20 2f 2a 20 31 30 37 20 2a 2f 20 22  e",. /* 107 */ "
2de5f 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20  ifexists ::= IF 
2de60 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20 31 30 38  EXISTS",. /* 108
2de61 20 2a 2f 20 22 69 66 65 78 69 73 74 73 20 3a 3a   */ "ifexists ::
2de62 3d 22 2c 0a 20 2f 2a 20 31 30 39 20 2a 2f 20 22  =",. /* 109 */ "
2de63 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77  cmd ::= createkw
2de64 20 74 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74   temp VIEW ifnot
2de65 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41  exists nm dbnm A
2de66 53 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31  S select",. /* 1
2de67 31 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44  10 */ "cmd ::= D
2de68 52 4f 50 20 56 49 45 57 20 69 66 65 78 69 73 74  ROP VIEW ifexist
2de69 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a  s fullname",. /*
2de6a 20 31 31 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   111 */ "cmd ::=
2de6b 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31   select",. /* 11
2de6c 32 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d  2 */ "select ::=
2de6d 20 6f 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a   oneselect",. /*
2de6e 20 31 31 33 20 2a 2f 20 22 73 65 6c 65 63 74 20   113 */ "select 
2de6f 3a 3a 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74 69  ::= select multi
2de70 73 65 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c  select_op onesel
2de71 65 63 74 22 2c 0a 20 2f 2a 20 31 31 34 20 2a 2f  ect",. /* 114 */
2de72 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70   "multiselect_op
2de73 20 3a 3a 3d 20 55 4e 49 4f 4e 22 2c 0a 20 2f 2a   ::= UNION",. /*
2de74 20 31 31 35 20 2a 2f 20 22 6d 75 6c 74 69 73 65   115 */ "multise
2de75 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f  lect_op ::= UNIO
2de76 4e 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 31 36 20  N ALL",. /* 116 
2de77 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f  */ "multiselect_
2de78 6f 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e  op ::= EXCEPT|IN
2de79 54 45 52 53 45 43 54 22 2c 0a 20 2f 2a 20 31 31  TERSECT",. /* 11
2de7a 37 20 2a 2f 20 22 6f 6e 65 73 65 6c 65 63 74 20  7 */ "oneselect 
2de7b 3a 3a 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69  ::= SELECT disti
2de7c 6e 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66  nct selcollist f
2de7d 72 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72  rom where_opt gr
2de7e 6f 75 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67  oupby_opt having
2de7f 5f 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74  _opt orderby_opt
2de80 20 6c 69 6d 69 74 5f 6f 70 74 22 2c 0a 20 2f 2a   limit_opt",. /*
2de81 20 31 31 38 20 2a 2f 20 22 64 69 73 74 69 6e 63   118 */ "distinc
2de82 74 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54 22 2c  t ::= DISTINCT",
2de83 0a 20 2f 2a 20 31 31 39 20 2a 2f 20 22 64 69 73  . /* 119 */ "dis
2de84 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0a  tinct ::= ALL",.
2de85 20 2f 2a 20 31 32 30 20 2a 2f 20 22 64 69 73 74   /* 120 */ "dist
2de86 69 6e 63 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  inct ::=",. /* 1
2de87 32 31 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 20  21 */ "sclp ::= 
2de88 73 65 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41  selcollist COMMA
2de89 22 2c 0a 20 2f 2a 20 31 32 32 20 2a 2f 20 22 73  ",. /* 122 */ "s
2de8a 63 6c 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32  clp ::=",. /* 12
2de8b 33 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74  3 */ "selcollist
2de8c 20 3a 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61   ::= sclp expr a
2de8d 73 22 2c 0a 20 2f 2a 20 31 32 34 20 2a 2f 20 22  s",. /* 124 */ "
2de8e 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73  selcollist ::= s
2de8f 63 6c 70 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31  clp STAR",. /* 1
2de90 32 35 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73  25 */ "selcollis
2de91 74 20 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f  t ::= sclp nm DO
2de92 54 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 32 36  T STAR",. /* 126
2de93 20 2a 2f 20 22 61 73 20 3a 3a 3d 20 41 53 20 6e   */ "as ::= AS n
2de94 6d 22 2c 0a 20 2f 2a 20 31 32 37 20 2a 2f 20 22  m",. /* 127 */ "
2de95 61 73 20 3a 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a  as ::= ids",. /*
2de96 20 31 32 38 20 2a 2f 20 22 61 73 20 3a 3a 3d 22   128 */ "as ::="
2de97 2c 0a 20 2f 2a 20 31 32 39 20 2a 2f 20 22 66 72  ,. /* 129 */ "fr
2de98 6f 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 30  om ::=",. /* 130
2de99 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d 20 46 52   */ "from ::= FR
2de9a 4f 4d 20 73 65 6c 74 61 62 6c 69 73 74 22 2c 0a  OM seltablist",.
2de9b 20 2f 2a 20 31 33 31 20 2a 2f 20 22 73 74 6c 5f   /* 131 */ "stl_
2de9c 70 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61  prefix ::= selta
2de9d 62 6c 69 73 74 20 6a 6f 69 6e 6f 70 22 2c 0a 20  blist joinop",. 
2de9e 2f 2a 20 31 33 32 20 2a 2f 20 22 73 74 6c 5f 70  /* 132 */ "stl_p
2de9f 72 65 66 69 78 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  refix ::=",. /* 
2dea0 31 33 33 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69  133 */ "seltabli
2dea1 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69  st ::= stl_prefi
2dea2 78 20 6e 6d 20 64 62 6e 6d 20 61 73 20 69 6e 64  x nm dbnm as ind
2dea3 65 78 65 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20  exed_opt on_opt 
2dea4 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20  using_opt",. /* 
2dea5 31 33 34 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69  134 */ "seltabli
2dea6 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69  st ::= stl_prefi
2dea7 78 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 61  x LP select RP a
2dea8 73 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f  s on_opt using_o
2dea9 70 74 22 2c 0a 20 2f 2a 20 31 33 35 20 2a 2f 20  pt",. /* 135 */ 
2deaa 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20  "seltablist ::= 
2deab 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65  stl_prefix LP se
2deac 6c 74 61 62 6c 69 73 74 20 52 50 20 61 73 20 6f  ltablist RP as o
2dead 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22  n_opt using_opt"
2deae 2c 0a 20 2f 2a 20 31 33 36 20 2a 2f 20 22 64 62  ,. /* 136 */ "db
2deaf 6e 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 37  nm ::=",. /* 137
2deb0 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 20 44 4f   */ "dbnm ::= DO
2deb1 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 33 38 20 2a  T nm",. /* 138 *
2deb2 2f 20 22 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20  / "fullname ::= 
2deb3 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 31 33  nm dbnm",. /* 13
2deb4 39 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d  9 */ "joinop ::=
2deb5 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 22 2c 0a 20 2f   COMMA|JOIN",. /
2deb6 2a 20 31 34 30 20 2a 2f 20 22 6a 6f 69 6e 6f 70  * 140 */ "joinop
2deb7 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49   ::= JOIN_KW JOI
2deb8 4e 22 2c 0a 20 2f 2a 20 31 34 31 20 2a 2f 20 22  N",. /* 141 */ "
2deb9 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
2deba 4b 57 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a  KW nm JOIN",. /*
2debb 20 31 34 32 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20   142 */ "joinop 
2debc 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e  ::= JOIN_KW nm n
2debd 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 33  m JOIN",. /* 143
2debe 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 20   */ "on_opt ::= 
2debf 4f 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 34  ON expr",. /* 14
2dec0 34 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d  4 */ "on_opt ::=
2dec1 22 2c 0a 20 2f 2a 20 31 34 35 20 2a 2f 20 22 69  ",. /* 145 */ "i
2dec2 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c  ndexed_opt ::=",
2dec3 0a 20 2f 2a 20 31 34 36 20 2a 2f 20 22 69 6e 64  . /* 146 */ "ind
2dec4 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44  exed_opt ::= IND
2dec5 45 58 45 44 20 42 59 20 6e 6d 22 2c 0a 20 2f 2a  EXED BY nm",. /*
2dec6 20 31 34 37 20 2a 2f 20 22 69 6e 64 65 78 65 64   147 */ "indexed
2dec7 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44  _opt ::= NOT IND
2dec8 45 58 45 44 22 2c 0a 20 2f 2a 20 31 34 38 20 2a  EXED",. /* 148 *
2dec9 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d  / "using_opt ::=
2deca 20 55 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c   USING LP inscol
2decb 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 34  list RP",. /* 14
2decc 39 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20  9 */ "using_opt 
2decd 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 30 20 2a 2f  ::=",. /* 150 */
2dece 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a   "orderby_opt ::
2decf 3d 22 2c 0a 20 2f 2a 20 31 35 31 20 2a 2f 20 22  =",. /* 151 */ "
2ded0 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20  orderby_opt ::= 
2ded1 4f 52 44 45 52 20 42 59 20 73 6f 72 74 6c 69 73  ORDER BY sortlis
2ded2 74 22 2c 0a 20 2f 2a 20 31 35 32 20 2a 2f 20 22  t",. /* 152 */ "
2ded3 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72  sortlist ::= sor
2ded4 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72 74  tlist COMMA sort
2ded5 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c  item sortorder",
2ded6 0a 20 2f 2a 20 31 35 33 20 2a 2f 20 22 73 6f 72  . /* 153 */ "sor
2ded7 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74  tlist ::= sortit
2ded8 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20  em sortorder",. 
2ded9 2f 2a 20 31 35 34 20 2a 2f 20 22 73 6f 72 74 69  /* 154 */ "sorti
2deda 74 65 6d 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20  tem ::= expr",. 
2dedb 2f 2a 20 31 35 35 20 2a 2f 20 22 73 6f 72 74 6f  /* 155 */ "sorto
2dedc 72 64 65 72 20 3a 3a 3d 20 41 53 43 22 2c 0a 20  rder ::= ASC",. 
2dedd 2f 2a 20 31 35 36 20 2a 2f 20 22 73 6f 72 74 6f  /* 156 */ "sorto
2dede 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 22 2c 0a  rder ::= DESC",.
2dedf 20 2f 2a 20 31 35 37 20 2a 2f 20 22 73 6f 72 74   /* 157 */ "sort
2dee0 6f 72 64 65 72 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  order ::=",. /* 
2dee1 31 35 38 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f  158 */ "groupby_
2dee2 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35  opt ::=",. /* 15
2dee3 39 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70  9 */ "groupby_op
2dee4 74 20 3a 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e  t ::= GROUP BY n
2dee5 65 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 31  exprlist",. /* 1
2dee6 36 30 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70  60 */ "having_op
2dee7 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 31 20  t ::=",. /* 161 
2dee8 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a  */ "having_opt :
2dee9 3a 3d 20 48 41 56 49 4e 47 20 65 78 70 72 22 2c  := HAVING expr",
2deea 0a 20 2f 2a 20 31 36 32 20 2a 2f 20 22 6c 69 6d  . /* 162 */ "lim
2deeb 69 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  it_opt ::=",. /*
2deec 20 31 36 33 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f   163 */ "limit_o
2deed 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
2deee 72 22 2c 0a 20 2f 2a 20 31 36 34 20 2a 2f 20 22  r",. /* 164 */ "
2deef 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49  limit_opt ::= LI
2def0 4d 49 54 20 65 78 70 72 20 4f 46 46 53 45 54 20  MIT expr OFFSET 
2def1 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 35 20 2a  expr",. /* 165 *
2def2 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d  / "limit_opt ::=
2def3 20 4c 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d   LIMIT expr COMM
2def4 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 36  A expr",. /* 166
2def5 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 4c   */ "cmd ::= DEL
2def6 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d  ETE FROM fullnam
2def7 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 77 68  e indexed_opt wh
2def8 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 36  ere_opt",. /* 16
2def9 37 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20  7 */ "where_opt 
2defa 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 38 20 2a 2f  ::=",. /* 168 */
2defb 20 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20   "where_opt ::= 
2defc 57 48 45 52 45 20 65 78 70 72 22 2c 0a 20 2f 2a  WHERE expr",. /*
2defd 20 31 36 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   169 */ "cmd ::=
2defe 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 66   UPDATE orconf f
2deff 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f  ullname indexed_
2df00 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73 74 20  opt SET setlist 
2df01 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20  where_opt",. /* 
2df02 31 37 30 20 2a 2f 20 22 73 65 74 6c 69 73 74 20  170 */ "setlist 
2df03 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d  ::= setlist COMM
2df04 41 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20  A nm EQ expr",. 
2df05 2f 2a 20 31 37 31 20 2a 2f 20 22 73 65 74 6c 69  /* 171 */ "setli
2df06 73 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70  st ::= nm EQ exp
2df07 72 22 2c 0a 20 2f 2a 20 31 37 32 20 2a 2f 20 22  r",. /* 172 */ "
2df08 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
2df09 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
2df0a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
2df0b 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69  VALUES LP itemli
2df0c 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 37 33 20  st RP",. /* 173 
2df0d 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  */ "cmd ::= inse
2df0e 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
2df0f 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
2df10 6f 70 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a  opt select",. /*
2df11 20 31 37 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   174 */ "cmd ::=
2df12 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
2df13 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c   fullname inscol
2df14 6c 69 73 74 5f 6f 70 74 20 44 45 46 41 55 4c 54  list_opt DEFAULT
2df15 20 56 41 4c 55 45 53 22 2c 0a 20 2f 2a 20 31 37   VALUES",. /* 17
2df16 35 20 2a 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64  5 */ "insert_cmd
2df17 20 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72 63 6f   ::= INSERT orco
2df18 6e 66 22 2c 0a 20 2f 2a 20 31 37 36 20 2a 2f 20  nf",. /* 176 */ 
2df19 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20  "insert_cmd ::= 
2df1a 52 45 50 4c 41 43 45 22 2c 0a 20 2f 2a 20 31 37  REPLACE",. /* 17
2df1b 37 20 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a  7 */ "itemlist :
2df1c 3a 3d 20 69 74 65 6d 6c 69 73 74 20 43 4f 4d 4d  := itemlist COMM
2df1d 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 38  A expr",. /* 178
2df1e 20 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a   */ "itemlist ::
2df1f 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 39  = expr",. /* 179
2df20 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f   */ "inscollist_
2df21 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 38  opt ::=",. /* 18
2df22 30 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74  0 */ "inscollist
2df23 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63  _opt ::= LP insc
2df24 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  ollist RP",. /* 
2df25 31 38 31 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69  181 */ "inscolli
2df26 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73  st ::= inscollis
2df27 74 20 43 4f 4d 4d 41 20 6e 6d 22 2c 0a 20 2f 2a  t COMMA nm",. /*
2df28 20 31 38 32 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c   182 */ "inscoll
2df29 69 73 74 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a  ist ::= nm",. /*
2df2a 20 31 38 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a   183 */ "expr ::
2df2b 3d 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 31 38 34  = term",. /* 184
2df2c 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50   */ "expr ::= LP
2df2d 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 31   expr RP",. /* 1
2df2e 38 35 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20  85 */ "term ::= 
2df2f 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 31 38 36 20 2a  NULL",. /* 186 *
2df30 2f 20 22 65 78 70 72 20 3a 3a 3d 20 69 64 22 2c  / "expr ::= id",
2df31 0a 20 2f 2a 20 31 38 37 20 2a 2f 20 22 65 78 70  . /* 187 */ "exp
2df32 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a  r ::= JOIN_KW",.
2df33 20 2f 2a 20 31 38 38 20 2a 2f 20 22 65 78 70 72   /* 188 */ "expr
2df34 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c   ::= nm DOT nm",
2df35 0a 20 2f 2a 20 31 38 39 20 2a 2f 20 22 65 78 70  . /* 189 */ "exp
2df36 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20  r ::= nm DOT nm 
2df37 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 39 30  DOT nm",. /* 190
2df38 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 49 4e   */ "term ::= IN
2df39 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42  TEGER|FLOAT|BLOB
2df3a 22 2c 0a 20 2f 2a 20 31 39 31 20 2a 2f 20 22 74  ",. /* 191 */ "t
2df3b 65 72 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c  erm ::= STRING",
2df3c 0a 20 2f 2a 20 31 39 32 20 2a 2f 20 22 65 78 70  . /* 192 */ "exp
2df3d 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52 22 2c  r ::= REGISTER",
2df3e 0a 20 2f 2a 20 31 39 33 20 2a 2f 20 22 65 78 70  . /* 193 */ "exp
2df3f 72 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 22 2c  r ::= VARIABLE",
2df40 0a 20 2f 2a 20 31 39 34 20 2a 2f 20 22 65 78 70  . /* 194 */ "exp
2df41 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4c 4c 41  r ::= expr COLLA
2df42 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 31 39 35  TE ids",. /* 195
2df43 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 43 41   */ "expr ::= CA
2df44 53 54 20 4c 50 20 65 78 70 72 20 41 53 20 74 79  ST LP expr AS ty
2df45 70 65 74 6f 6b 65 6e 20 52 50 22 2c 0a 20 2f 2a  petoken RP",. /*
2df46 20 31 39 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a   196 */ "expr ::
2df47 3d 20 49 44 20 4c 50 20 64 69 73 74 69 6e 63 74  = ID LP distinct
2df48 20 65 78 70 72 6c 69 73 74 20 52 50 22 2c 0a 20   exprlist RP",. 
2df49 2f 2a 20 31 39 37 20 2a 2f 20 22 65 78 70 72 20  /* 197 */ "expr 
2df4a 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52 20 52  ::= ID LP STAR R
2df4b 50 22 2c 0a 20 2f 2a 20 31 39 38 20 2a 2f 20 22  P",. /* 198 */ "
2df4c 74 65 72 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b  term ::= CTIME_K
2df4d 57 22 2c 0a 20 2f 2a 20 31 39 39 20 2a 2f 20 22  W",. /* 199 */ "
2df4e 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e  expr ::= expr AN
2df4f 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 30  D expr",. /* 200
2df50 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2df51 70 72 20 4f 52 20 65 78 70 72 22 2c 0a 20 2f 2a  pr OR expr",. /*
2df52 20 32 30 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a   201 */ "expr ::
2df53 3d 20 65 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c  = expr LT|GT|GE|
2df54 4c 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30  LE expr",. /* 20
2df55 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  2 */ "expr ::= e
2df56 78 70 72 20 45 51 7c 4e 45 20 65 78 70 72 22 2c  xpr EQ|NE expr",
2df57 0a 20 2f 2a 20 32 30 33 20 2a 2f 20 22 65 78 70  . /* 203 */ "exp
2df58 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e  r ::= expr BITAN
2df59 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52  D|BITOR|LSHIFT|R
2df5a 53 48 49 46 54 20 65 78 70 72 22 2c 0a 20 2f 2a  SHIFT expr",. /*
2df5b 20 32 30 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   204 */ "expr ::
2df5c 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d 49 4e 55  = expr PLUS|MINU
2df5d 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 35  S expr",. /* 205
2df5e 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2df5f 70 72 20 53 54 41 52 7c 53 4c 41 53 48 7c 52 45  pr STAR|SLASH|RE
2df60 4d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 36  M expr",. /* 206
2df61 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2df62 70 72 20 43 4f 4e 43 41 54 20 65 78 70 72 22 2c  pr CONCAT expr",
2df63 0a 20 2f 2a 20 32 30 37 20 2a 2f 20 22 6c 69 6b  . /* 207 */ "lik
2df64 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b 57 22  eop ::= LIKE_KW"
2df65 2c 0a 20 2f 2a 20 32 30 38 20 2a 2f 20 22 6c 69  ,. /* 208 */ "li
2df66 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b  keop ::= NOT LIK
2df67 45 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30 39 20 2a  E_KW",. /* 209 *
2df68 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41  / "likeop ::= MA
2df69 54 43 48 22 2c 0a 20 2f 2a 20 32 31 30 20 2a 2f  TCH",. /* 210 */
2df6a 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54   "likeop ::= NOT
2df6b 20 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32 31 31   MATCH",. /* 211
2df6c 20 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d 20   */ "escape ::= 
2df6d 45 53 43 41 50 45 20 65 78 70 72 22 2c 0a 20 2f  ESCAPE expr",. /
2df6e 2a 20 32 31 32 20 2a 2f 20 22 65 73 63 61 70 65  * 212 */ "escape
2df6f 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 31 33 20 2a   ::=",. /* 213 *
2df70 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
2df71 20 6c 69 6b 65 6f 70 20 65 78 70 72 20 65 73 63   likeop expr esc
2df72 61 70 65 22 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f  ape",. /* 214 */
2df73 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
2df74 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 22 2c  ISNULL|NOTNULL",
2df75 0a 20 2f 2a 20 32 31 35 20 2a 2f 20 22 65 78 70  . /* 215 */ "exp
2df76 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 55  r ::= expr IS NU
2df77 4c 4c 22 2c 0a 20 2f 2a 20 32 31 36 20 2a 2f 20  LL",. /* 216 */ 
2df78 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4e  "expr ::= expr N
2df79 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31  OT NULL",. /* 21
2df7a 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  7 */ "expr ::= e
2df7b 78 70 72 20 49 53 20 4e 4f 54 20 4e 55 4c 4c 22  xpr IS NOT NULL"
2df7c 2c 0a 20 2f 2a 20 32 31 38 20 2a 2f 20 22 65 78  ,. /* 218 */ "ex
2df7d 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 22  pr ::= NOT expr"
2df7e 2c 0a 20 2f 2a 20 32 31 39 20 2a 2f 20 22 65 78  ,. /* 219 */ "ex
2df7f 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78  pr ::= BITNOT ex
2df80 70 72 22 2c 0a 20 2f 2a 20 32 32 30 20 2a 2f 20  pr",. /* 220 */ 
2df81 22 65 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20  "expr ::= MINUS 
2df82 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 31 20 2a  expr",. /* 221 *
2df83 2f 20 22 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53  / "expr ::= PLUS
2df84 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 32 20   expr",. /* 222 
2df85 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a  */ "between_op :
2df86 3a 3d 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a  := BETWEEN",. /*
2df87 20 32 32 33 20 2a 2f 20 22 62 65 74 77 65 65 6e   223 */ "between
2df88 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57  _op ::= NOT BETW
2df89 45 45 4e 22 2c 0a 20 2f 2a 20 32 32 34 20 2a 2f  EEN",. /* 224 */
2df8a 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
2df8b 62 65 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20  between_op expr 
2df8c 41 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  AND expr",. /* 2
2df8d 32 35 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d  25 */ "in_op ::=
2df8e 20 49 4e 22 2c 0a 20 2f 2a 20 32 32 36 20 2a 2f   IN",. /* 226 */
2df8f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20   "in_op ::= NOT 
2df90 49 4e 22 2c 0a 20 2f 2a 20 32 32 37 20 2a 2f 20  IN",. /* 227 */ 
2df91 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69  "expr ::= expr i
2df92 6e 5f 6f 70 20 4c 50 20 65 78 70 72 6c 69 73 74  n_op LP exprlist
2df93 20 52 50 22 2c 0a 20 2f 2a 20 32 32 38 20 2a 2f   RP",. /* 228 */
2df94 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65   "expr ::= LP se
2df95 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32  lect RP",. /* 22
2df96 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  9 */ "expr ::= e
2df97 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c  xpr in_op LP sel
2df98 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 33 30  ect RP",. /* 230
2df99 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2df9a 70 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d  pr in_op nm dbnm
2df9b 22 2c 0a 20 2f 2a 20 32 33 31 20 2a 2f 20 22 65  ",. /* 231 */ "e
2df9c 78 70 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c  xpr ::= EXISTS L
2df9d 50 20 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f  P select RP",. /
2df9e 2a 20 32 33 32 20 2a 2f 20 22 65 78 70 72 20 3a  * 232 */ "expr :
2df9f 3a 3d 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65  := CASE case_ope
2dfa0 72 61 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69  rand case_exprli
2dfa1 73 74 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44  st case_else END
2dfa2 22 2c 0a 20 2f 2a 20 32 33 33 20 2a 2f 20 22 63  ",. /* 233 */ "c
2dfa3 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
2dfa4 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57   case_exprlist W
2dfa5 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
2dfa6 70 72 22 2c 0a 20 2f 2a 20 32 33 34 20 2a 2f 20  pr",. /* 234 */ 
2dfa7 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a  "case_exprlist :
2dfa8 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48 45  := WHEN expr THE
2dfa9 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 35  N expr",. /* 235
2dfaa 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65 20 3a   */ "case_else :
2dfab 3a 3d 20 45 4c 53 45 20 65 78 70 72 22 2c 0a 20  := ELSE expr",. 
2dfac 2f 2a 20 32 33 36 20 2a 2f 20 22 63 61 73 65 5f  /* 236 */ "case_
2dfad 65 6c 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  else ::=",. /* 2
2dfae 33 37 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72  37 */ "case_oper
2dfaf 61 6e 64 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20  and ::= expr",. 
2dfb0 2f 2a 20 32 33 38 20 2a 2f 20 22 63 61 73 65 5f  /* 238 */ "case_
2dfb1 6f 70 65 72 61 6e 64 20 3a 3a 3d 22 2c 0a 20 2f  operand ::=",. /
2dfb2 2a 20 32 33 39 20 2a 2f 20 22 65 78 70 72 6c 69  * 239 */ "exprli
2dfb3 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74  st ::= nexprlist
2dfb4 22 2c 0a 20 2f 2a 20 32 34 30 20 2a 2f 20 22 65  ",. /* 240 */ "e
2dfb5 78 70 72 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f  xprlist ::=",. /
2dfb6 2a 20 32 34 31 20 2a 2f 20 22 6e 65 78 70 72 6c  * 241 */ "nexprl
2dfb7 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73  ist ::= nexprlis
2dfb8 74 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20  t COMMA expr",. 
2dfb9 2f 2a 20 32 34 32 20 2a 2f 20 22 6e 65 78 70 72  /* 242 */ "nexpr
2dfba 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a  list ::= expr",.
2dfbb 20 2f 2a 20 32 34 33 20 2a 2f 20 22 63 6d 64 20   /* 243 */ "cmd 
2dfbc 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 75 6e 69  ::= createkw uni
2dfbd 71 75 65 66 6c 61 67 20 49 4e 44 45 58 20 69 66  queflag INDEX if
2dfbe 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e  notexists nm dbn
2dfbf 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69  m ON nm LP idxli
2dfc0 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 34 34 20  st RP",. /* 244 
2dfc1 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a  */ "uniqueflag :
2dfc2 3a 3d 20 55 4e 49 51 55 45 22 2c 0a 20 2f 2a 20  := UNIQUE",. /* 
2dfc3 32 34 35 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c  245 */ "uniquefl
2dfc4 61 67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 36  ag ::=",. /* 246
2dfc5 20 2a 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70 74   */ "idxlist_opt
2dfc6 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 37 20 2a   ::=",. /* 247 *
2dfc7 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a  / "idxlist_opt :
2dfc8 3a 3d 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50  := LP idxlist RP
2dfc9 22 2c 0a 20 2f 2a 20 32 34 38 20 2a 2f 20 22 69  ",. /* 248 */ "i
2dfca 64 78 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69  dxlist ::= idxli
2dfcb 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c  st COMMA nm coll
2dfcc 61 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a  ate sortorder",.
2dfcd 20 2f 2a 20 32 34 39 20 2a 2f 20 22 69 64 78 6c   /* 249 */ "idxl
2dfce 69 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61  ist ::= nm colla
2dfcf 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20  te sortorder",. 
2dfd0 2f 2a 20 32 35 30 20 2a 2f 20 22 63 6f 6c 6c 61  /* 250 */ "colla
2dfd1 74 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 35 31  te ::=",. /* 251
2dfd2 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d   */ "collate ::=
2dfd3 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20   COLLATE ids",. 
2dfd4 2f 2a 20 32 35 32 20 2a 2f 20 22 63 6d 64 20 3a  /* 252 */ "cmd :
2dfd5 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58 20 69 66  := DROP INDEX if
2dfd6 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22  exists fullname"
2dfd7 2c 0a 20 2f 2a 20 32 35 33 20 2a 2f 20 22 63 6d  ,. /* 253 */ "cm
2dfd8 64 20 3a 3a 3d 20 56 41 43 55 55 4d 22 2c 0a 20  d ::= VACUUM",. 
2dfd9 2f 2a 20 32 35 34 20 2a 2f 20 22 63 6d 64 20 3a  /* 254 */ "cmd :
2dfda 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 22 2c 0a 20  := VACUUM nm",. 
2dfdb 2f 2a 20 32 35 35 20 2a 2f 20 22 63 6d 64 20 3a  /* 255 */ "cmd :
2dfdc 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
2dfdd 6d 22 2c 0a 20 2f 2a 20 32 35 36 20 2a 2f 20 22  m",. /* 256 */ "
2dfde 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
2dfdf 6d 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 22  m dbnm EQ nmnum"
2dfe0 2c 0a 20 2f 2a 20 32 35 37 20 2a 2f 20 22 63 6d  ,. /* 257 */ "cm
2dfe1 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
2dfe2 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20 52 50  dbnm LP nmnum RP
2dfe3 22 2c 0a 20 2f 2a 20 32 35 38 20 2a 2f 20 22 63  ",. /* 258 */ "c
2dfe4 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
2dfe5 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73 5f 6e   dbnm EQ minus_n
2dfe6 75 6d 22 2c 0a 20 2f 2a 20 32 35 39 20 2a 2f 20  um",. /* 259 */ 
2dfe7 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20  "cmd ::= PRAGMA 
2dfe8 6e 6d 20 64 62 6e 6d 20 4c 50 20 6d 69 6e 75 73  nm dbnm LP minus
2dfe9 5f 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a 20 32 36  _num RP",. /* 26
2dfea 30 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20  0 */ "nmnum ::= 
2dfeb 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 32  plus_num",. /* 2
2dfec 36 31 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d  61 */ "nmnum ::=
2dfed 20 6e 6d 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f   nm",. /* 262 */
2dfee 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e 22 2c   "nmnum ::= ON",
2dfef 0a 20 2f 2a 20 32 36 33 20 2a 2f 20 22 6e 6d 6e  . /* 263 */ "nmn
2dff0 75 6d 20 3a 3a 3d 20 44 45 4c 45 54 45 22 2c 0a  um ::= DELETE",.
2dff1 20 2f 2a 20 32 36 34 20 2a 2f 20 22 6e 6d 6e 75   /* 264 */ "nmnu
2dff2 6d 20 3a 3a 3d 20 44 45 46 41 55 4c 54 22 2c 0a  m ::= DEFAULT",.
2dff3 20 2f 2a 20 32 36 35 20 2a 2f 20 22 70 6c 75 73   /* 265 */ "plus
2dff4 5f 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70  _num ::= plus_op
2dff5 74 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32  t number",. /* 2
2dff6 36 36 20 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d  66 */ "minus_num
2dff7 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65   ::= MINUS numbe
2dff8 72 22 2c 0a 20 2f 2a 20 32 36 37 20 2a 2f 20 22  r",. /* 267 */ "
2dff9 6e 75 6d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47  number ::= INTEG
2dffa 45 52 7c 46 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32  ER|FLOAT",. /* 2
2dffb 36 38 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20  68 */ "plus_opt 
2dffc 3a 3a 3d 20 50 4c 55 53 22 2c 0a 20 2f 2a 20 32  ::= PLUS",. /* 2
2dffd 36 39 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20  69 */ "plus_opt 
2dffe 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f  ::=",. /* 270 */
2dfff 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65   "cmd ::= create
2e000 6b 77 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20  kw trigger_decl 
2e001 42 45 47 49 4e 20 74 72 69 67 67 65 72 5f 63 6d  BEGIN trigger_cm
2e002 64 5f 6c 69 73 74 20 45 4e 44 22 2c 0a 20 2f 2a  d_list END",. /*
2e003 20 32 37 31 20 2a 2f 20 22 74 72 69 67 67 65 72   271 */ "trigger
2e004 5f 64 65 63 6c 20 3a 3a 3d 20 74 65 6d 70 20 54  _decl ::= temp T
2e005 52 49 47 47 45 52 20 69 66 6e 6f 74 65 78 69 73  RIGGER ifnotexis
2e006 74 73 20 6e 6d 20 64 62 6e 6d 20 74 72 69 67 67  ts nm dbnm trigg
2e007 65 72 5f 74 69 6d 65 20 74 72 69 67 67 65 72 5f  er_time trigger_
2e008 65 76 65 6e 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d  event ON fullnam
2e009 65 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65  e foreach_clause
2e00a 20 77 68 65 6e 5f 63 6c 61 75 73 65 22 2c 0a 20   when_clause",. 
2e00b 2f 2a 20 32 37 32 20 2a 2f 20 22 74 72 69 67 67  /* 272 */ "trigg
2e00c 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 42 45 46 4f  er_time ::= BEFO
2e00d 52 45 22 2c 0a 20 2f 2a 20 32 37 33 20 2a 2f 20  RE",. /* 273 */ 
2e00e 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a  "trigger_time ::
2e00f 3d 20 41 46 54 45 52 22 2c 0a 20 2f 2a 20 32 37  = AFTER",. /* 27
2e010 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69  4 */ "trigger_ti
2e011 6d 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f  me ::= INSTEAD O
2e012 46 22 2c 0a 20 2f 2a 20 32 37 35 20 2a 2f 20 22  F",. /* 275 */ "
2e013 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
2e014 22 2c 0a 20 2f 2a 20 32 37 36 20 2a 2f 20 22 74  ",. /* 276 */ "t
2e015 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
2e016 20 44 45 4c 45 54 45 7c 49 4e 53 45 52 54 22 2c   DELETE|INSERT",
2e017 0a 20 2f 2a 20 32 37 37 20 2a 2f 20 22 74 72 69  . /* 277 */ "tri
2e018 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55  gger_event ::= U
2e019 50 44 41 54 45 22 2c 0a 20 2f 2a 20 32 37 38 20  PDATE",. /* 278 
2e01a 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e  */ "trigger_even
2e01b 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f 46 20  t ::= UPDATE OF 
2e01c 69 6e 73 63 6f 6c 6c 69 73 74 22 2c 0a 20 2f 2a  inscollist",. /*
2e01d 20 32 37 39 20 2a 2f 20 22 66 6f 72 65 61 63 68   279 */ "foreach
2e01e 5f 63 6c 61 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f  _clause ::=",. /
2e01f 2a 20 32 38 30 20 2a 2f 20 22 66 6f 72 65 61 63  * 280 */ "foreac
2e020 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52  h_clause ::= FOR
2e021 20 45 41 43 48 20 52 4f 57 22 2c 0a 20 2f 2a 20   EACH ROW",. /* 
2e022 32 38 31 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61  281 */ "when_cla
2e023 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38  use ::=",. /* 28
2e024 32 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73  2 */ "when_claus
2e025 65 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 22  e ::= WHEN expr"
2e026 2c 0a 20 2f 2a 20 32 38 33 20 2a 2f 20 22 74 72  ,. /* 283 */ "tr
2e027 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a  igger_cmd_list :
2e028 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  := trigger_cmd_l
2e029 69 73 74 20 74 72 69 67 67 65 72 5f 63 6d 64 20  ist trigger_cmd 
2e02a 53 45 4d 49 22 2c 0a 20 2f 2a 20 32 38 34 20 2a  SEMI",. /* 284 *
2e02b 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  / "trigger_cmd_l
2e02c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f  ist ::= trigger_
2e02d 63 6d 64 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 32  cmd SEMI",. /* 2
2e02e 38 35 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  85 */ "trigger_c
2e02f 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72  md ::= UPDATE or
2e030 63 6f 6e 66 20 6e 6d 20 53 45 54 20 73 65 74 6c  conf nm SET setl
2e031 69 73 74 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a  ist where_opt",.
2e032 20 2f 2a 20 32 38 36 20 2a 2f 20 22 74 72 69 67   /* 286 */ "trig
2e033 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  ger_cmd ::= inse
2e034 72 74 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69  rt_cmd INTO nm i
2e035 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41  nscollist_opt VA
2e036 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74  LUES LP itemlist
2e037 20 52 50 22 2c 0a 20 2f 2a 20 32 38 37 20 2a 2f   RP",. /* 287 */
2e038 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a   "trigger_cmd ::
2e039 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
2e03a 4f 20 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  O nm inscollist_
2e03b 6f 70 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a  opt select",. /*
2e03c 20 32 38 38 20 2a 2f 20 22 74 72 69 67 67 65 72   288 */ "trigger
2e03d 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20  _cmd ::= DELETE 
2e03e 46 52 4f 4d 20 6e 6d 20 77 68 65 72 65 5f 6f 70  FROM nm where_op
2e03f 74 22 2c 0a 20 2f 2a 20 32 38 39 20 2a 2f 20 22  t",. /* 289 */ "
2e040 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
2e041 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32 39 30  select",. /* 290
2e042 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52 41   */ "expr ::= RA
2e043 49 53 45 20 4c 50 20 49 47 4e 4f 52 45 20 52 50  ISE LP IGNORE RP
2e044 22 2c 0a 20 2f 2a 20 32 39 31 20 2a 2f 20 22 65  ",. /* 291 */ "e
2e045 78 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50  xpr ::= RAISE LP
2e046 20 72 61 69 73 65 74 79 70 65 20 43 4f 4d 4d 41   raisetype COMMA
2e047 20 6e 6d 20 52 50 22 2c 0a 20 2f 2a 20 32 39 32   nm RP",. /* 292
2e048 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a   */ "raisetype :
2e049 3a 3d 20 52 4f 4c 4c 42 41 43 4b 22 2c 0a 20 2f  := ROLLBACK",. /
2e04a 2a 20 32 39 33 20 2a 2f 20 22 72 61 69 73 65 74  * 293 */ "raiset
2e04b 79 70 65 20 3a 3a 3d 20 41 42 4f 52 54 22 2c 0a  ype ::= ABORT",.
2e04c 20 2f 2a 20 32 39 34 20 2a 2f 20 22 72 61 69 73   /* 294 */ "rais
2e04d 65 74 79 70 65 20 3a 3a 3d 20 46 41 49 4c 22 2c  etype ::= FAIL",
2e04e 0a 20 2f 2a 20 32 39 35 20 2a 2f 20 22 63 6d 64  . /* 295 */ "cmd
2e04f 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47 47 45   ::= DROP TRIGGE
2e050 52 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  R ifexists fulln
2e051 61 6d 65 22 2c 0a 20 2f 2a 20 32 39 36 20 2a 2f  ame",. /* 296 */
2e052 20 22 63 6d 64 20 3a 3a 3d 20 41 54 54 41 43 48   "cmd ::= ATTACH
2e053 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74   database_kw_opt
2e054 20 65 78 70 72 20 41 53 20 65 78 70 72 20 6b 65   expr AS expr ke
2e055 79 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 39 37 20  y_opt",. /* 297 
2e056 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 54 41  */ "cmd ::= DETA
2e057 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  CH database_kw_o
2e058 70 74 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 39  pt expr",. /* 29
2e059 38 20 2a 2f 20 22 6b 65 79 5f 6f 70 74 20 3a 3a  8 */ "key_opt ::
2e05a 3d 22 2c 0a 20 2f 2a 20 32 39 39 20 2a 2f 20 22  =",. /* 299 */ "
2e05b 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45 59 20  key_opt ::= KEY 
2e05c 65 78 70 72 22 2c 0a 20 2f 2a 20 33 30 30 20 2a  expr",. /* 300 *
2e05d 2f 20 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  / "database_kw_o
2e05e 70 74 20 3a 3a 3d 20 44 41 54 41 42 41 53 45 22  pt ::= DATABASE"
2e05f 2c 0a 20 2f 2a 20 33 30 31 20 2a 2f 20 22 64 61  ,. /* 301 */ "da
2e060 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a  tabase_kw_opt ::
2e061 3d 22 2c 0a 20 2f 2a 20 33 30 32 20 2a 2f 20 22  =",. /* 302 */ "
2e062 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 22  cmd ::= REINDEX"
2e063 2c 0a 20 2f 2a 20 33 30 33 20 2a 2f 20 22 63 6d  ,. /* 303 */ "cm
2e064 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 6e 6d  d ::= REINDEX nm
2e065 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 33 30 34 20   dbnm",. /* 304 
2e066 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c  */ "cmd ::= ANAL
2e067 59 5a 45 22 2c 0a 20 2f 2a 20 33 30 35 20 2a 2f  YZE",. /* 305 */
2e068 20 22 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a   "cmd ::= ANALYZ
2e069 45 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20  E nm dbnm",. /* 
2e06a 33 30 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  306 */ "cmd ::= 
2e06b 41 4c 54 45 52 20 54 41 42 4c 45 20 66 75 6c 6c  ALTER TABLE full
2e06c 6e 61 6d 65 20 52 45 4e 41 4d 45 20 54 4f 20 6e  name RENAME TO n
2e06d 6d 22 2c 0a 20 2f 2a 20 33 30 37 20 2a 2f 20 22  m",. /* 307 */ "
2e06e 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41  cmd ::= ALTER TA
2e06f 42 4c 45 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66  BLE add_column_f
2e070 75 6c 6c 6e 61 6d 65 20 41 44 44 20 6b 77 63 6f  ullname ADD kwco
2e071 6c 75 6d 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e 22  lumn_opt column"
2e072 2c 0a 20 2f 2a 20 33 30 38 20 2a 2f 20 22 61 64  ,. /* 308 */ "ad
2e073 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d  d_column_fullnam
2e074 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d 65 22 2c  e ::= fullname",
2e075 0a 20 2f 2a 20 33 30 39 20 2a 2f 20 22 6b 77 63  . /* 309 */ "kwc
2e076 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  olumn_opt ::=",.
2e077 20 2f 2a 20 33 31 30 20 2a 2f 20 22 6b 77 63 6f   /* 310 */ "kwco
2e078 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c  lumn_opt ::= COL
2e079 55 4d 4e 4b 57 22 2c 0a 20 2f 2a 20 33 31 31 20  UMNKW",. /* 311 
2e07a 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61  */ "cmd ::= crea
2e07b 74 65 5f 76 74 61 62 22 2c 0a 20 2f 2a 20 33 31  te_vtab",. /* 31
2e07c 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72  2 */ "cmd ::= cr
2e07d 65 61 74 65 5f 76 74 61 62 20 4c 50 20 76 74 61  eate_vtab LP vta
2e07e 62 61 72 67 6c 69 73 74 20 52 50 22 2c 0a 20 2f  barglist RP",. /
2e07f 2a 20 33 31 33 20 2a 2f 20 22 63 72 65 61 74 65  * 313 */ "create
2e080 5f 76 74 61 62 20 3a 3a 3d 20 63 72 65 61 74 65  _vtab ::= create
2e081 6b 77 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  kw VIRTUAL TABLE
2e082 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e   nm dbnm USING n
2e083 6d 22 2c 0a 20 2f 2a 20 33 31 34 20 2a 2f 20 22  m",. /* 314 */ "
2e084 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20  vtabarglist ::= 
2e085 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20 33 31  vtabarg",. /* 31
2e086 35 20 2a 2f 20 22 76 74 61 62 61 72 67 6c 69 73  5 */ "vtabarglis
2e087 74 20 3a 3a 3d 20 76 74 61 62 61 72 67 6c 69 73  t ::= vtabarglis
2e088 74 20 43 4f 4d 4d 41 20 76 74 61 62 61 72 67 22  t COMMA vtabarg"
2e089 2c 0a 20 2f 2a 20 33 31 36 20 2a 2f 20 22 76 74  ,. /* 316 */ "vt
2e08a 61 62 61 72 67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  abarg ::=",. /* 
2e08b 33 31 37 20 2a 2f 20 22 76 74 61 62 61 72 67 20  317 */ "vtabarg 
2e08c 3a 3a 3d 20 76 74 61 62 61 72 67 20 76 74 61 62  ::= vtabarg vtab
2e08d 61 72 67 74 6f 6b 65 6e 22 2c 0a 20 2f 2a 20 33  argtoken",. /* 3
2e08e 31 38 20 2a 2f 20 22 76 74 61 62 61 72 67 74 6f  18 */ "vtabargto
2e08f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 22 2c 0a 20 2f  ken ::= ANY",. /
2e090 2a 20 33 31 39 20 2a 2f 20 22 76 74 61 62 61 72  * 319 */ "vtabar
2e091 67 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61 6e  gtoken ::= lp an
2e092 79 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33  ylist RP",. /* 3
2e093 32 30 20 2a 2f 20 22 6c 70 20 3a 3a 3d 20 4c 50  20 */ "lp ::= LP
2e094 22 2c 0a 20 2f 2a 20 33 32 31 20 2a 2f 20 22 61  ",. /* 321 */ "a
2e095 6e 79 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  nylist ::=",. /*
2e096 20 33 32 32 20 2a 2f 20 22 61 6e 79 6c 69 73 74   322 */ "anylist
2e097 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59   ::= anylist ANY
2e098 22 2c 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  ",.};.#endif /* 
2e099 4e 44 45 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 20  NDEBUG */...#if 
2e09a 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a  YYSTACKDEPTH<=0.
2e09b 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 69 6e 63  /*.** Try to inc
2e09c 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f  rease the size o
2e09d 66 20 74 68 65 20 70 61 72 73 65 72 20 73 74 61  f the parser sta
2e09e 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ck..*/.static vo
2e09f 69 64 20 79 79 47 72 6f 77 53 74 61 63 6b 28 79  id yyGrowStack(y
2e0a0 79 50 61 72 73 65 72 20 2a 70 29 7b 0a 20 20 69  yParser *p){.  i
2e0a1 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 20 20 79 79  nt newSize;.  yy
2e0a2 53 74 61 63 6b 45 6e 74 72 79 20 2a 70 4e 65 77  StackEntry *pNew
2e0a3 3b 0a 0a 20 20 6e 65 77 53 69 7a 65 20 3d 20 70  ;..  newSize = p
2e0a4 2d 3e 79 79 73 74 6b 73 7a 2a 32 20 2b 20 31 30  ->yystksz*2 + 10
2e0a5 30 3b 0a 20 20 70 4e 65 77 20 3d 20 72 65 61 6c  0;.  pNew = real
2e0a6 6c 6f 63 28 70 2d 3e 79 79 73 74 61 63 6b 2c 20  loc(p->yystack, 
2e0a7 6e 65 77 53 69 7a 65 2a 73 69 7a 65 6f 66 28 70  newSize*sizeof(p
2e0a8 4e 65 77 5b 30 5d 29 29 3b 0a 20 20 69 66 28 20  New[0]));.  if( 
2e0a9 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d 3e 79  pNew ){.    p->y
2e0aa 79 73 74 61 63 6b 20 3d 20 70 4e 65 77 3b 0a 20  ystack = pNew;. 
2e0ab 20 20 20 70 2d 3e 79 79 73 74 6b 73 7a 20 3d 20     p->yystksz = 
2e0ac 6e 65 77 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66  newSize;.#ifndef
2e0ad 20 4e 44 45 42 55 47 0a 20 20 20 20 69 66 28 20   NDEBUG.    if( 
2e0ae 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
2e0af 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
2e0b0 72 61 63 65 46 49 4c 45 2c 22 25 73 53 74 61 63  raceFILE,"%sStac
2e0b1 6b 20 67 72 6f 77 73 20 74 6f 20 25 64 20 65 6e  k grows to %d en
2e0b2 74 72 69 65 73 21 5c 6e 22 2c 0a 20 20 20 20 20  tries!\n",.     
2e0b3 20 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65           yyTrace
2e0b4 50 72 6f 6d 70 74 2c 20 70 2d 3e 79 79 73 74 6b  Prompt, p->yystk
2e0b5 73 7a 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  sz);.    }.#endi
2e0b6 66 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  f.  }.}.#endif..
2e0b7 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  /* .** This func
2e0b8 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61  tion allocates a
2e0b9 20 6e 65 77 20 70 61 72 73 65 72 2e 0a 2a 2a 20   new parser..** 
2e0ba 54 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e  The only argumen
2e0bb 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
2e0bc 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 68 69  o a function whi
2e0bd 63 68 20 77 6f 72 6b 73 20 6c 69 6b 65 0a 2a 2a  ch works like.**
2e0be 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 49   malloc..**.** I
2e0bf 6e 70 75 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e  nputs:.** A poin
2e0c0 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ter to the funct
2e0c1 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f  ion used to allo
2e0c2 63 61 74 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a  cate memory..**.
2e0c3 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a 20 41  ** Outputs:.** A
2e0c4 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 61   pointer to a pa
2e0c5 72 73 65 72 2e 20 20 54 68 69 73 20 70 6f 69 6e  rser.  This poin
2e0c6 74 65 72 20 69 73 20 75 73 65 64 20 69 6e 20 73  ter is used in s
2e0c7 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 0a  ubsequent calls.
2e0c8 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 72  ** to sqlite3Par
2e0c9 73 65 72 20 61 6e 64 20 73 71 6c 69 74 65 33 50  ser and sqlite3P
2e0ca 61 72 73 65 72 46 72 65 65 2e 0a 2a 2f 0a 53 51  arserFree..*/.SQ
2e0cb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2e0cc 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65 72  d *sqlite3Parser
2e0cd 41 6c 6c 6f 63 28 76 6f 69 64 20 2a 28 2a 6d 61  Alloc(void *(*ma
2e0ce 6c 6c 6f 63 50 72 6f 63 29 28 73 69 7a 65 5f 74  llocProc)(size_t
2e0cf 29 29 7b 0a 20 20 79 79 50 61 72 73 65 72 20 2a  )){.  yyParser *
2e0d0 70 50 61 72 73 65 72 3b 0a 20 20 70 50 61 72 73  pParser;.  pPars
2e0d1 65 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29  er = (yyParser*)
2e0d2 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 20 28  (*mallocProc)( (
2e0d3 73 69 7a 65 5f 74 29 73 69 7a 65 6f 66 28 79 79  size_t)sizeof(yy
2e0d4 50 61 72 73 65 72 29 20 29 3b 0a 20 20 69 66 28  Parser) );.  if(
2e0d5 20 70 50 61 72 73 65 72 20 29 7b 0a 20 20 20 20   pParser ){.    
2e0d6 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d  pParser->yyidx =
2e0d7 20 2d 31 3b 0a 23 69 66 64 65 66 20 59 59 54 52   -1;.#ifdef YYTR
2e0d8 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48  ACKMAXSTACKDEPTH
2e0d9 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79  .    pParser->yy
2e0da 69 64 78 4d 61 78 20 3d 20 30 3b 0a 23 65 6e 64  idxMax = 0;.#end
2e0db 69 66 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45  if.#if YYSTACKDE
2e0dc 50 54 48 3c 3d 30 0a 20 20 20 20 70 50 61 72 73  PTH<=0.    pPars
2e0dd 65 72 2d 3e 79 79 73 74 61 63 6b 20 3d 20 4e 55  er->yystack = NU
2e0de 4c 4c 3b 0a 20 20 20 20 70 50 61 72 73 65 72 2d  LL;.    pParser-
2e0df 3e 79 79 73 74 6b 73 7a 20 3d 20 30 3b 0a 20 20  >yystksz = 0;.  
2e0e0 20 20 79 79 47 72 6f 77 53 74 61 63 6b 28 70 50    yyGrowStack(pP
2e0e1 61 72 73 65 72 29 3b 0a 23 65 6e 64 69 66 0a 20  arser);.#endif. 
2e0e2 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72   }.  return pPar
2e0e3 73 65 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66  ser;.}../* The f
2e0e4 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f  ollowing functio
2e0e5 6e 20 64 65 6c 65 74 65 73 20 74 68 65 20 76 61  n deletes the va
2e0e6 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  lue associated w
2e0e7 69 74 68 20 61 0a 2a 2a 20 73 79 6d 62 6f 6c 2e  ith a.** symbol.
2e0e8 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 63 61 6e    The symbol can
2e0e9 20 62 65 20 65 69 74 68 65 72 20 61 20 74 65 72   be either a ter
2e0ea 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d  minal or nonterm
2e0eb 69 6e 61 6c 2e 0a 2a 2a 20 22 79 79 6d 61 6a 6f  inal..** "yymajo
2e0ec 72 22 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c  r" is the symbol
2e0ed 20 63 6f 64 65 2c 20 61 6e 64 20 22 79 79 70 6d   code, and "yypm
2e0ee 69 6e 6f 72 22 20 69 73 20 61 20 70 6f 69 6e 74  inor" is a point
2e0ef 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61 6c  er to.** the val
2e0f0 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ue..*/.static vo
2e0f1 69 64 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72  id yy_destructor
2e0f2 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79  (.  yyParser *yy
2e0f3 70 50 61 72 73 65 72 2c 20 20 20 20 2f 2a 20 54  pParser,    /* T
2e0f4 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 59  he parser */.  Y
2e0f5 59 43 4f 44 45 54 59 50 45 20 79 79 6d 61 6a 6f  YCODETYPE yymajo
2e0f6 72 2c 20 20 20 20 20 2f 2a 20 54 79 70 65 20 63  r,     /* Type c
2e0f7 6f 64 65 20 66 6f 72 20 6f 62 6a 65 63 74 20 74  ode for object t
2e0f8 6f 20 64 65 73 74 72 6f 79 20 2a 2f 0a 20 20 59  o destroy */.  Y
2e0f9 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 6d  YMINORTYPE *yypm
2e0fa 69 6e 6f 72 20 20 20 2f 2a 20 54 68 65 20 6f 62  inor   /* The ob
2e0fb 6a 65 63 74 20 74 6f 20 62 65 20 64 65 73 74 72  ject to be destr
2e0fc 6f 79 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  oyed */.){.  sql
2e0fd 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45  ite3ParserARG_FE
2e0fe 54 43 48 3b 0a 20 20 73 77 69 74 63 68 28 20 79  TCH;.  switch( y
2e0ff 79 6d 61 6a 6f 72 20 29 7b 0a 20 20 20 20 2f 2a  ymajor ){.    /*
2e100 20 48 65 72 65 20 69 73 20 69 6e 73 65 72 74 65   Here is inserte
2e101 64 20 74 68 65 20 61 63 74 69 6f 6e 73 20 77 68  d the actions wh
2e102 69 63 68 20 74 61 6b 65 20 70 6c 61 63 65 20 77  ich take place w
2e103 68 65 6e 20 61 0a 20 20 20 20 2a 2a 20 74 65 72  hen a.    ** ter
2e104 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 2d 74 65 72  minal or non-ter
2e105 6d 69 6e 61 6c 20 69 73 20 64 65 73 74 72 6f 79  minal is destroy
2e106 65 64 2e 20 20 54 68 69 73 20 63 61 6e 20 68 61  ed.  This can ha
2e107 70 70 65 6e 0a 20 20 20 20 2a 2a 20 77 68 65 6e  ppen.    ** when
2e108 20 74 68 65 20 73 79 6d 62 6f 6c 20 69 73 20 70   the symbol is p
2e109 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73  opped from the s
2e10a 74 61 63 6b 20 64 75 72 69 6e 67 20 61 0a 20 20  tack during a.  
2e10b 20 20 2a 2a 20 72 65 64 75 63 65 20 6f 72 20 64    ** reduce or d
2e10c 75 72 69 6e 67 20 65 72 72 6f 72 20 70 72 6f 63  uring error proc
2e10d 65 73 73 69 6e 67 20 6f 72 20 77 68 65 6e 20 61  essing or when a
2e10e 20 70 61 72 73 65 72 20 69 73 20 0a 20 20 20 20   parser is .    
2e10f 2a 2a 20 62 65 69 6e 67 20 64 65 73 74 72 6f 79  ** being destroy
2e110 65 64 20 62 65 66 6f 72 65 20 69 74 20 69 73 20  ed before it is 
2e111 66 69 6e 69 73 68 65 64 20 70 61 72 73 69 6e 67  finished parsing
2e112 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2e113 4e 6f 74 65 3a 20 64 75 72 69 6e 67 20 61 20 72  Note: during a r
2e114 65 64 75 63 65 2c 20 74 68 65 20 6f 6e 6c 79 20  educe, the only 
2e115 73 79 6d 62 6f 6c 73 20 64 65 73 74 72 6f 79 65  symbols destroye
2e116 64 20 61 72 65 20 74 68 6f 73 65 0a 20 20 20 20  d are those.    
2e117 2a 2a 20 77 68 69 63 68 20 61 70 70 65 61 72 20  ** which appear 
2e118 6f 6e 20 74 68 65 20 52 48 53 20 6f 66 20 74 68  on the RHS of th
2e119 65 20 72 75 6c 65 2c 20 62 75 74 20 77 68 69 63  e rule, but whic
2e11a 68 20 61 72 65 20 6e 6f 74 20 75 73 65 64 0a 20  h are not used. 
2e11b 20 20 20 2a 2a 20 69 6e 73 69 64 65 20 74 68 65     ** inside the
2e11c 20 43 20 63 6f 64 65 2e 0a 20 20 20 20 2a 2f 0a   C code..    */.
2e11d 20 20 20 20 63 61 73 65 20 31 36 30 3a 20 2f 2a      case 160: /*
2e11e 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20 20 20 63   select */.    c
2e11f 61 73 65 20 31 39 34 3a 20 2f 2a 20 6f 6e 65 73  ase 194: /* ones
2e120 65 6c 65 63 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74  elect */.{.sqlit
2e121 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
2e122 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d  Parse->db, (yypm
2e123 69 6e 6f 72 2d 3e 79 79 32 34 33 29 29 3b 0a 7d  inor->yy243));.}
2e124 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2e125 20 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20 74    case 174: /* t
2e126 65 72 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  erm */.    case 
2e127 31 37 35 3a 20 2f 2a 20 65 78 70 72 20 2a 2f 0a  175: /* expr */.
2e128 20 20 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a      case 199: /*
2e129 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 20 20   where_opt */.  
2e12a 20 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20 68    case 201: /* h
2e12b 61 76 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20 20  aving_opt */.   
2e12c 20 63 61 73 65 20 32 31 30 3a 20 2f 2a 20 6f 6e   case 210: /* on
2e12d 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  _opt */.    case
2e12e 20 32 31 35 3a 20 2f 2a 20 73 6f 72 74 69 74 65   215: /* sortite
2e12f 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32  m */.    case 22
2e130 33 3a 20 2f 2a 20 65 73 63 61 70 65 20 2a 2f 0a  3: /* escape */.
2e131 20 20 20 20 63 61 73 65 20 32 32 36 3a 20 2f 2a      case 226: /*
2e132 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 2a 2f   case_operand */
2e133 0a 20 20 20 20 63 61 73 65 20 32 32 38 3a 20 2f  .    case 228: /
2e134 2a 20 63 61 73 65 5f 65 6c 73 65 20 2a 2f 0a 20  * case_else */. 
2e135 20 20 20 63 61 73 65 20 32 33 39 3a 20 2f 2a 20     case 239: /* 
2e136 77 68 65 6e 5f 63 6c 61 75 73 65 20 2a 2f 0a 20  when_clause */. 
2e137 20 20 20 63 61 73 65 20 32 34 32 3a 20 2f 2a 20     case 242: /* 
2e138 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c  key_opt */.{.sql
2e139 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
2e13a 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d  Parse->db, (yypm
2e13b 69 6e 6f 72 2d 3e 79 79 37 32 29 29 3b 0a 7d 0a  inor->yy72));.}.
2e13c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e13d 20 63 61 73 65 20 31 37 39 3a 20 2f 2a 20 69 64   case 179: /* id
2e13e 78 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a 20 20 20  xlist_opt */.   
2e13f 20 63 61 73 65 20 31 38 37 3a 20 2f 2a 20 69 64   case 187: /* id
2e140 78 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73  xlist */.    cas
2e141 65 20 31 39 37 3a 20 2f 2a 20 73 65 6c 63 6f 6c  e 197: /* selcol
2e142 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
2e143 20 32 30 30 3a 20 2f 2a 20 67 72 6f 75 70 62 79   200: /* groupby
2e144 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  _opt */.    case
2e145 20 32 30 32 3a 20 2f 2a 20 6f 72 64 65 72 62 79   202: /* orderby
2e146 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  _opt */.    case
2e147 20 32 30 34 3a 20 2f 2a 20 73 63 6c 70 20 2a 2f   204: /* sclp */
2e148 0a 20 20 20 20 63 61 73 65 20 32 31 34 3a 20 2f  .    case 214: /
2e149 2a 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20  * sortlist */.  
2e14a 20 20 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 6e    case 216: /* n
2e14b 65 78 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20  exprlist */.    
2e14c 63 61 73 65 20 32 31 37 3a 20 2f 2a 20 73 65 74  case 217: /* set
2e14d 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
2e14e 20 32 32 30 3a 20 2f 2a 20 69 74 65 6d 6c 69 73   220: /* itemlis
2e14f 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32  t */.    case 22
2e150 31 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 2a  1: /* exprlist *
2e151 2f 0a 20 20 20 20 63 61 73 65 20 32 32 37 3a 20  /.    case 227: 
2e152 2f 2a 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74  /* case_exprlist
2e153 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70   */.{.sqlite3Exp
2e154 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
2e155 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f  se->db, (yypmino
2e156 72 2d 3e 79 79 31 34 38 29 29 3b 0a 7d 0a 20 20  r->yy148));.}.  
2e157 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
2e158 61 73 65 20 31 39 33 3a 20 2f 2a 20 66 75 6c 6c  ase 193: /* full
2e159 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 61 73 65  name */.    case
2e15a 20 31 39 38 3a 20 2f 2a 20 66 72 6f 6d 20 2a 2f   198: /* from */
2e15b 0a 20 20 20 20 63 61 73 65 20 32 30 36 3a 20 2f  .    case 206: /
2e15c 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a  * seltablist */.
2e15d 20 20 20 20 63 61 73 65 20 32 30 37 3a 20 2f 2a      case 207: /*
2e15e 20 73 74 6c 5f 70 72 65 66 69 78 20 2a 2f 0a 7b   stl_prefix */.{
2e15f 0a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44  .sqlite3SrcListD
2e160 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2e161 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31  , (yypminor->yy1
2e162 38 35 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  85));.}.      br
2e163 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 31  eak;.    case 21
2e164 31 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20  1: /* using_opt 
2e165 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31 33 3a  */.    case 213:
2e166 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a   /* inscollist *
2e167 2f 0a 20 20 20 20 63 61 73 65 20 32 31 39 3a 20  /.    case 219: 
2e168 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  /* inscollist_op
2e169 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64  t */.{.sqlite3Id
2e16a 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
2e16b 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72  e->db, (yypminor
2e16c 2d 3e 79 79 32 35 34 29 29 3b 0a 7d 0a 20 20 20  ->yy254));.}.   
2e16d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
2e16e 73 65 20 32 33 35 3a 20 2f 2a 20 74 72 69 67 67  se 235: /* trigg
2e16f 65 72 5f 63 6d 64 5f 6c 69 73 74 20 2a 2f 0a 20  er_cmd_list */. 
2e170 20 20 20 63 61 73 65 20 32 34 30 3a 20 2f 2a 20     case 240: /* 
2e171 74 72 69 67 67 65 72 5f 63 6d 64 20 2a 2f 0a 7b  trigger_cmd */.{
2e172 0a 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72  .sqlite3DeleteTr
2e173 69 67 67 65 72 53 74 65 70 28 70 50 61 72 73 65  iggerStep(pParse
2e174 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
2e175 3e 79 79 31 34 35 29 29 3b 0a 7d 0a 20 20 20 20  >yy145));.}.    
2e176 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
2e177 65 20 32 33 37 3a 20 2f 2a 20 74 72 69 67 67 65  e 237: /* trigge
2e178 72 5f 65 76 65 6e 74 20 2a 2f 0a 7b 0a 73 71 6c  r_event */.{.sql
2e179 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
2e17a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79  (pParse->db, (yy
2e17b 70 6d 69 6e 6f 72 2d 3e 79 79 33 33 32 29 2e 62  pminor->yy332).b
2e17c 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
2e17d 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  ;.    default:  
2e17e 62 72 65 61 6b 3b 20 20 20 2f 2a 20 49 66 20 6e  break;   /* If n
2e17f 6f 20 64 65 73 74 72 75 63 74 6f 72 20 61 63 74  o destructor act
2e180 69 6f 6e 20 73 70 65 63 69 66 69 65 64 3a 20 64  ion specified: d
2e181 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 7d  o nothing */.  }
2e182 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 74 68  .}../*.** Pop th
2e183 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
2e184 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   once..**.** If 
2e185 74 68 65 72 65 20 69 73 20 61 20 64 65 73 74 72  there is a destr
2e186 75 63 74 6f 72 20 72 6f 75 74 69 6e 65 20 61 73  uctor routine as
2e187 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2e188 65 20 74 6f 6b 65 6e 20 77 68 69 63 68 0a 2a 2a  e token which.**
2e189 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20   is popped from 
2e18a 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20  the stack, then 
2e18b 63 61 6c 6c 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 52  call it..**.** R
2e18c 65 74 75 72 6e 20 74 68 65 20 6d 61 6a 6f 72 20  eturn the major 
2e18d 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 66 6f 72  token number for
2e18e 20 74 68 65 20 73 79 6d 62 6f 6c 20 70 6f 70 70   the symbol popp
2e18f 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
2e190 74 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f  t yy_pop_parser_
2e191 73 74 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a  stack(yyParser *
2e192 70 50 61 72 73 65 72 29 7b 0a 20 20 59 59 43 4f  pParser){.  YYCO
2e193 44 45 54 59 50 45 20 79 79 6d 61 6a 6f 72 3b 0a  DETYPE yymajor;.
2e194 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a    yyStackEntry *
2e195 79 79 74 6f 73 20 3d 20 26 70 50 61 72 73 65 72  yytos = &pParser
2e196 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65  ->yystack[pParse
2e197 72 2d 3e 79 79 69 64 78 5d 3b 0a 0a 20 20 69 66  r->yyidx];..  if
2e198 28 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  ( pParser->yyidx
2e199 3c 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  <0 ) return 0;.#
2e19a 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
2e19b 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
2e19c 26 26 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64  && pParser->yyid
2e19d 78 3e 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69  x>=0 ){.    fpri
2e19e 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
2e19f 22 25 73 50 6f 70 70 69 6e 67 20 25 73 5c 6e 22  "%sPopping %s\n"
2e1a0 2c 0a 20 20 20 20 20 20 79 79 54 72 61 63 65 50  ,.      yyTraceP
2e1a1 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20 79 79 54  rompt,.      yyT
2e1a2 6f 6b 65 6e 4e 61 6d 65 5b 79 79 74 6f 73 2d 3e  okenName[yytos->
2e1a3 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e  major]);.  }.#en
2e1a4 64 69 66 0a 20 20 79 79 6d 61 6a 6f 72 20 3d 20  dif.  yymajor = 
2e1a5 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 3b 0a 20 20  yytos->major;.  
2e1a6 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 70 50  yy_destructor(pP
2e1a7 61 72 73 65 72 2c 20 79 79 6d 61 6a 6f 72 2c 20  arser, yymajor, 
2e1a8 26 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72 29 3b 0a  &yytos->minor);.
2e1a9 20 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78    pParser->yyidx
2e1aa 2d 2d 3b 0a 20 20 72 65 74 75 72 6e 20 79 79 6d  --;.  return yym
2e1ab 61 6a 6f 72 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  ajor;.}../* .** 
2e1ac 44 65 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 64  Deallocate and d
2e1ad 65 73 74 72 6f 79 20 61 20 70 61 72 73 65 72 2e  estroy a parser.
2e1ae 20 20 44 65 73 74 72 75 63 74 6f 72 73 20 61 72    Destructors ar
2e1af 65 20 61 6c 6c 20 63 61 6c 6c 65 64 20 66 6f 72  e all called for
2e1b0 0a 2a 2a 20 61 6c 6c 20 73 74 61 63 6b 20 65 6c  .** all stack el
2e1b1 65 6d 65 6e 74 73 20 62 65 66 6f 72 65 20 73 68  ements before sh
2e1b2 75 74 74 69 6e 67 20 74 68 65 20 70 61 72 73 65  utting the parse
2e1b3 72 20 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6e  r down..**.** In
2e1b4 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  puts:.** <ul>.**
2e1b5 20 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65 72   <li>  A pointer
2e1b6 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 2e 20   to the parser. 
2e1b7 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20   This should be 
2e1b8 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20  a pointer.**    
2e1b9 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d     obtained from
2e1ba 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c   sqlite3ParserAl
2e1bb 6c 6f 63 2e 0a 2a 2a 20 3c 6c 69 3e 20 20 41 20  loc..** <li>  A 
2e1bc 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
2e1bd 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65  ction used to re
2e1be 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 6f 62 74  claim memory obt
2e1bf 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 66  ained.**       f
2e1c0 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 3c  rom malloc..** <
2e1c1 2f 75 6c 3e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  /ul>.*/.SQLITE_P
2e1c2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2e1c3 74 65 33 50 61 72 73 65 72 46 72 65 65 28 0a 20  te3ParserFree(. 
2e1c4 20 76 6f 69 64 20 2a 70 2c 20 20 20 20 20 20 20   void *p,       
2e1c5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e1c6 54 68 65 20 70 61 72 73 65 72 20 74 6f 20 62 65  The parser to be
2e1c7 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 76 6f   deleted */.  vo
2e1c8 69 64 20 28 2a 66 72 65 65 50 72 6f 63 29 28 76  id (*freeProc)(v
2e1c9 6f 69 64 2a 29 20 20 20 20 20 2f 2a 20 46 75 6e  oid*)     /* Fun
2e1ca 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65  ction used to re
2e1cb 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 2a 2f 0a  claim memory */.
2e1cc 29 7b 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70  ){.  yyParser *p
2e1cd 50 61 72 73 65 72 20 3d 20 28 79 79 50 61 72 73  Parser = (yyPars
2e1ce 65 72 2a 29 70 3b 0a 20 20 69 66 28 20 70 50 61  er*)p;.  if( pPa
2e1cf 72 73 65 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  rser==0 ) return
2e1d0 3b 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73  ;.  while( pPars
2e1d1 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79  er->yyidx>=0 ) y
2e1d2 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61  y_pop_parser_sta
2e1d3 63 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 69 66  ck(pParser);.#if
2e1d4 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30   YYSTACKDEPTH<=0
2e1d5 0a 20 20 66 72 65 65 28 70 50 61 72 73 65 72 2d  .  free(pParser-
2e1d6 3e 79 79 73 74 61 63 6b 29 3b 0a 23 65 6e 64 69  >yystack);.#endi
2e1d7 66 0a 20 20 28 2a 66 72 65 65 50 72 6f 63 29 28  f.  (*freeProc)(
2e1d8 28 76 6f 69 64 2a 29 70 50 61 72 73 65 72 29 3b  (void*)pParser);
2e1d9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2e1da 20 74 68 65 20 70 65 61 6b 20 64 65 70 74 68 20   the peak depth 
2e1db 6f 66 20 74 68 65 20 73 74 61 63 6b 20 66 6f 72  of the stack for
2e1dc 20 61 20 70 61 72 73 65 72 2e 0a 2a 2f 0a 23 69   a parser..*/.#i
2e1dd 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
2e1de 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45  TACKDEPTH.SQLITE
2e1df 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2e1e0 69 74 65 33 50 61 72 73 65 72 53 74 61 63 6b 50  ite3ParserStackP
2e1e1 65 61 6b 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  eak(void *p){.  
2e1e2 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65  yyParser *pParse
2e1e3 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70  r = (yyParser*)p
2e1e4 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73  ;.  return pPars
2e1e5 65 72 2d 3e 79 79 69 64 78 4d 61 78 3b 0a 7d 0a  er->yyidxMax;.}.
2e1e6 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69  #endif../*.** Fi
2e1e7 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  nd the appropria
2e1e8 74 65 20 61 63 74 69 6f 6e 20 66 6f 72 20 61 20  te action for a 
2e1e9 70 61 72 73 65 72 20 67 69 76 65 6e 20 74 68 65  parser given the
2e1ea 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f   terminal.** loo
2e1eb 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c  k-ahead token iL
2e1ec 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  ookAhead..**.** 
2e1ed 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61  If the look-ahea
2e1ee 64 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43  d token is YYNOC
2e1ef 4f 44 45 2c 20 74 68 65 6e 20 63 68 65 63 6b 20  ODE, then check 
2e1f0 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 61 63  to see if the ac
2e1f1 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65 70  tion is.** indep
2e1f2 65 6e 64 65 6e 74 20 6f 66 20 74 68 65 20 6c 6f  endent of the lo
2e1f3 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66 20 69 74  ok-ahead.  If it
2e1f4 20 69 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20   is, return the 
2e1f5 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69 73  action, otherwis
2e1f6 65 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f 4e  e.** return YY_N
2e1f7 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61  O_ACTION..*/.sta
2e1f8 74 69 63 20 69 6e 74 20 79 79 5f 66 69 6e 64 5f  tic int yy_find_
2e1f9 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 0a 20 20  shift_action(.  
2e1fa 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65  yyParser *pParse
2e1fb 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r,        /* The
2e1fc 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 59 59 43   parser */.  YYC
2e1fd 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65  ODETYPE iLookAhe
2e1fe 61 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f  ad     /* The lo
2e1ff 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a  ok-ahead token *
2e200 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
2e201 69 6e 74 20 73 74 61 74 65 6e 6f 20 3d 20 70 50  int stateno = pP
2e202 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 70  arser->yystack[p
2e203 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73  Parser->yyidx].s
2e204 74 61 74 65 6e 6f 3b 0a 20 0a 20 20 69 66 28 20  tateno;. .  if( 
2e205 73 74 61 74 65 6e 6f 3e 59 59 5f 53 48 49 46 54  stateno>YY_SHIFT
2e206 5f 4d 41 58 20 7c 7c 20 28 69 20 3d 20 79 79 5f  _MAX || (i = yy_
2e207 73 68 69 66 74 5f 6f 66 73 74 5b 73 74 61 74 65  shift_ofst[state
2e208 6e 6f 5d 29 3d 3d 59 59 5f 53 48 49 46 54 5f 55  no])==YY_SHIFT_U
2e209 53 45 5f 44 46 4c 54 20 29 7b 0a 20 20 20 20 72  SE_DFLT ){.    r
2e20a 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74  eturn yy_default
2e20b 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 20  [stateno];.  }. 
2e20c 20 61 73 73 65 72 74 28 20 69 4c 6f 6f 6b 41 68   assert( iLookAh
2e20d 65 61 64 21 3d 59 59 4e 4f 43 4f 44 45 20 29 3b  ead!=YYNOCODE );
2e20e 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65  .  i += iLookAhe
2e20f 61 64 3b 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c  ad;.  if( i<0 ||
2e210 20 69 3e 3d 59 59 5f 53 5a 5f 41 43 54 54 41 42   i>=YY_SZ_ACTTAB
2e211 20 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64   || yy_lookahead
2e212 5b 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20  [i]!=iLookAhead 
2e213 29 7b 0a 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b  ){.    if( iLook
2e214 41 68 65 61 64 3e 30 20 29 7b 0a 23 69 66 64 65  Ahead>0 ){.#ifde
2e215 66 20 59 59 46 41 4c 4c 42 41 43 4b 0a 20 20 20  f YYFALLBACK.   
2e216 20 20 20 59 59 43 4f 44 45 54 59 50 45 20 69 46     YYCODETYPE iF
2e217 61 6c 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20  allback;        
2e218 20 20 20 20 2f 2a 20 46 61 6c 6c 62 61 63 6b 20      /* Fallback 
2e219 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69  token */.      i
2e21a 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3c 73 69  f( iLookAhead<si
2e21b 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61 63 6b 29  zeof(yyFallback)
2e21c 2f 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61  /sizeof(yyFallba
2e21d 63 6b 5b 30 5d 29 0a 20 20 20 20 20 20 20 20 20  ck[0]).         
2e21e 20 20 20 20 26 26 20 28 69 46 61 6c 6c 62 61 63      && (iFallbac
2e21f 6b 20 3d 20 79 79 46 61 6c 6c 62 61 63 6b 5b 69  k = yyFallback[i
2e220 4c 6f 6f 6b 41 68 65 61 64 5d 29 21 3d 30 20 29  LookAhead])!=0 )
2e221 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  {.#ifndef NDEBUG
2e222 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 79 54  .        if( yyT
2e223 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
2e224 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79        fprintf(yy
2e225 54 72 61 63 65 46 49 4c 45 2c 20 22 25 73 46 41  TraceFILE, "%sFA
2e226 4c 4c 42 41 43 4b 20 25 73 20 3d 3e 20 25 73 5c  LLBACK %s => %s\
2e227 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
2e228 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20   yyTracePrompt, 
2e229 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f  yyTokenName[iLoo
2e22a 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e  kAhead], yyToken
2e22b 4e 61 6d 65 5b 69 46 61 6c 6c 62 61 63 6b 5d 29  Name[iFallback])
2e22c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64  ;.        }.#end
2e22d 69 66 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  if.        retur
2e22e 6e 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f  n yy_find_shift_
2e22f 61 63 74 69 6f 6e 28 70 50 61 72 73 65 72 2c 20  action(pParser, 
2e230 69 46 61 6c 6c 62 61 63 6b 29 3b 0a 20 20 20 20  iFallback);.    
2e231 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65    }.#endif.#ifde
2e232 66 20 59 59 57 49 4c 44 43 41 52 44 0a 20 20 20  f YYWILDCARD.   
2e233 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74     {.        int
2e234 20 6a 20 3d 20 69 20 2d 20 69 4c 6f 6f 6b 41 68   j = i - iLookAh
2e235 65 61 64 20 2b 20 59 59 57 49 4c 44 43 41 52 44  ead + YYWILDCARD
2e236 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e  ;.        if( j>
2e237 3d 30 20 26 26 20 6a 3c 59 59 5f 53 5a 5f 41 43  =0 && j<YY_SZ_AC
2e238 54 54 41 42 20 26 26 20 79 79 5f 6c 6f 6f 6b 61  TTAB && yy_looka
2e239 68 65 61 64 5b 6a 5d 3d 3d 59 59 57 49 4c 44 43  head[j]==YYWILDC
2e23a 41 52 44 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e  ARD ){.#ifndef N
2e23b 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 20 20  DEBUG.          
2e23c 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
2e23d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66  ){.            f
2e23e 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
2e23f 4c 45 2c 20 22 25 73 57 49 4c 44 43 41 52 44 20  LE, "%sWILDCARD 
2e240 25 73 20 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20  %s => %s\n",.   
2e241 20 20 20 20 20 20 20 20 20 20 20 20 79 79 54 72              yyTr
2e242 61 63 65 50 72 6f 6d 70 74 2c 20 79 79 54 6f 6b  acePrompt, yyTok
2e243 65 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b 41 68 65 61  enName[iLookAhea
2e244 64 5d 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  d], yyTokenName[
2e245 59 59 57 49 4c 44 43 41 52 44 5d 29 3b 0a 20 20  YYWILDCARD]);.  
2e246 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
2e247 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 20 20   /* NDEBUG */.  
2e248 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 79          return y
2e249 79 5f 61 63 74 69 6f 6e 5b 6a 5d 3b 0a 20 20 20  y_action[j];.   
2e24a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23       }.      }.#
2e24b 65 6e 64 69 66 20 2f 2a 20 59 59 57 49 4c 44 43  endif /* YYWILDC
2e24c 41 52 44 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  ARD */.    }.   
2e24d 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75   return yy_defau
2e24e 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d  lt[stateno];.  }
2e24f 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
2e250 20 79 79 5f 61 63 74 69 6f 6e 5b 69 5d 3b 0a 20   yy_action[i];. 
2e251 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64   }.}../*.** Find
2e252 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
2e253 20 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 70 61   action for a pa
2e254 72 73 65 72 20 67 69 76 65 6e 20 74 68 65 20 6e  rser given the n
2e255 6f 6e 2d 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c  on-terminal.** l
2e256 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20  ook-ahead token 
2e257 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a  iLookAhead..**.*
2e258 2a 20 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68  * If the look-ah
2e259 65 61 64 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e  ead token is YYN
2e25a 4f 43 4f 44 45 2c 20 74 68 65 6e 20 63 68 65 63  OCODE, then chec
2e25b 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
2e25c 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64  action is.** ind
2e25d 65 70 65 6e 64 65 6e 74 20 6f 66 20 74 68 65 20  ependent of the 
2e25e 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66 20  look-ahead.  If 
2e25f 69 74 20 69 73 2c 20 72 65 74 75 72 6e 20 74 68  it is, return th
2e260 65 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77  e action, otherw
2e261 69 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59  ise.** return YY
2e262 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73  _NO_ACTION..*/.s
2e263 74 61 74 69 63 20 69 6e 74 20 79 79 5f 66 69 6e  tatic int yy_fin
2e264 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28  d_reduce_action(
2e265 0a 20 20 69 6e 74 20 73 74 61 74 65 6e 6f 2c 20  .  int stateno, 
2e266 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e267 43 75 72 72 65 6e 74 20 73 74 61 74 65 20 6e 75  Current state nu
2e268 6d 62 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45  mber */.  YYCODE
2e269 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65 61 64 20  TYPE iLookAhead 
2e26a 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d      /* The look-
2e26b 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29  ahead token */.)
2e26c 7b 0a 20 20 69 6e 74 20 69 3b 0a 23 69 66 64 65  {.  int i;.#ifde
2e26d 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a  f YYERRORSYMBOL.
2e26e 20 20 69 66 28 20 73 74 61 74 65 6e 6f 3e 59 59    if( stateno>YY
2e26f 5f 52 45 44 55 43 45 5f 4d 41 58 20 29 7b 0a 20  _REDUCE_MAX ){. 
2e270 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66     return yy_def
2e271 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20  ault[stateno];. 
2e272 20 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72   }.#else.  asser
2e273 74 28 20 73 74 61 74 65 6e 6f 3c 3d 59 59 5f 52  t( stateno<=YY_R
2e274 45 44 55 43 45 5f 4d 41 58 20 29 3b 0a 23 65 6e  EDUCE_MAX );.#en
2e275 64 69 66 0a 20 20 69 20 3d 20 79 79 5f 72 65 64  dif.  i = yy_red
2e276 75 63 65 5f 6f 66 73 74 5b 73 74 61 74 65 6e 6f  uce_ofst[stateno
2e277 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 21 3d  ];.  assert( i!=
2e278 59 59 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46  YY_REDUCE_USE_DF
2e279 4c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  LT );.  assert( 
2e27a 69 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f  iLookAhead!=YYNO
2e27b 43 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d 20 69  CODE );.  i += i
2e27c 4c 6f 6f 6b 41 68 65 61 64 3b 0a 23 69 66 64 65  LookAhead;.#ifde
2e27d 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a  f YYERRORSYMBOL.
2e27e 20 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d    if( i<0 || i>=
2e27f 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20  YY_SZ_ACTTAB || 
2e280 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21  yy_lookahead[i]!
2e281 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20  =iLookAhead ){. 
2e282 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66     return yy_def
2e283 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20  ault[stateno];. 
2e284 20 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72   }.#else.  asser
2e285 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 59 59 5f  t( i>=0 && i<YY_
2e286 53 5a 5f 41 43 54 54 41 42 20 29 3b 0a 20 20 61  SZ_ACTTAB );.  a
2e287 73 73 65 72 74 28 20 79 79 5f 6c 6f 6f 6b 61 68  ssert( yy_lookah
2e288 65 61 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b 41 68 65  ead[i]==iLookAhe
2e289 61 64 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72  ad );.#endif.  r
2e28a 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b  eturn yy_action[
2e28b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  i];.}../*.** The
2e28c 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
2e28d 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20  ne is called if 
2e28e 74 68 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c  the stack overfl
2e28f 6f 77 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ows..*/.static v
2e290 6f 69 64 20 79 79 53 74 61 63 6b 4f 76 65 72 66  oid yyStackOverf
2e291 6c 6f 77 28 79 79 50 61 72 73 65 72 20 2a 79 79  low(yyParser *yy
2e292 70 50 61 72 73 65 72 2c 20 59 59 4d 49 4e 4f 52  pParser, YYMINOR
2e293 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72 29 7b  TYPE *yypMinor){
2e294 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  .   sqlite3Parse
2e295 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 20 79  rARG_FETCH;.   y
2e296 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d  ypParser->yyidx-
2e297 2d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  -;.#ifndef NDEBU
2e298 47 0a 20 20 20 69 66 28 20 79 79 54 72 61 63 65  G.   if( yyTrace
2e299 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 66 70 72  FILE ){.     fpr
2e29a 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
2e29b 2c 22 25 73 53 74 61 63 6b 20 4f 76 65 72 66 6c  ,"%sStack Overfl
2e29c 6f 77 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72  ow!\n",yyTracePr
2e29d 6f 6d 70 74 29 3b 0a 20 20 20 7d 0a 23 65 6e 64  ompt);.   }.#end
2e29e 69 66 0a 20 20 20 77 68 69 6c 65 28 20 79 79 70  if.   while( yyp
2e29f 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30  Parser->yyidx>=0
2e2a0 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72   ) yy_pop_parser
2e2a1 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72  _stack(yypParser
2e2a2 29 3b 0a 20 20 20 2f 2a 20 48 65 72 65 20 63 6f  );.   /* Here co
2e2a3 64 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 77  de is inserted w
2e2a4 68 69 63 68 20 77 69 6c 6c 20 65 78 65 63 75 74  hich will execut
2e2a5 65 20 69 66 20 74 68 65 20 70 61 72 73 65 72 0a  e if the parser.
2e2a6 20 20 20 2a 2a 20 73 74 61 63 6b 20 65 76 65 72     ** stack ever
2e2a7 79 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a 0a  y overflows */..
2e2a8 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
2e2a9 45 52 28 79 79 70 4d 69 6e 6f 72 29 3b 20 2f 2a  ER(yypMinor); /*
2e2aa 20 53 69 6c 65 6e 63 65 20 73 6f 6d 65 20 63 6f   Silence some co
2e2ab 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20  mpiler warnings 
2e2ac 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  */.  sqlite3Erro
2e2ad 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70 61  rMsg(pParse, "pa
2e2ae 72 73 65 72 20 73 74 61 63 6b 20 6f 76 65 72 66  rser stack overf
2e2af 6c 6f 77 22 29 3b 0a 20 20 70 50 61 72 73 65 2d  low");.  pParse-
2e2b0 3e 70 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b  >parseError = 1;
2e2b1 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  .   sqlite3Parse
2e2b2 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53  rARG_STORE; /* S
2e2b3 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20  uppress warning 
2e2b4 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78  about unused %ex
2e2b5 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72  tra_argument var
2e2b6 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72   */.}../*.** Per
2e2b7 66 6f 72 6d 20 61 20 73 68 69 66 74 20 61 63 74  form a shift act
2e2b8 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
2e2b9 6f 69 64 20 79 79 5f 73 68 69 66 74 28 0a 20 20  oid yy_shift(.  
2e2ba 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72  yyParser *yypPar
2e2bb 73 65 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ser,          /*
2e2bc 20 54 68 65 20 70 61 72 73 65 72 20 74 6f 20 62   The parser to b
2e2bd 65 20 73 68 69 66 74 65 64 20 2a 2f 0a 20 20 69  e shifted */.  i
2e2be 6e 74 20 79 79 4e 65 77 53 74 61 74 65 2c 20 20  nt yyNewState,  
2e2bf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e2c0 54 68 65 20 6e 65 77 20 73 74 61 74 65 20 74 6f  The new state to
2e2c1 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 69   shift in */.  i
2e2c2 6e 74 20 79 79 4d 61 6a 6f 72 2c 20 20 20 20 20  nt yyMajor,     
2e2c3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e2c4 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20  The major token 
2e2c5 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20  to shift in */. 
2e2c6 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79   YYMINORTYPE *yy
2e2c7 70 4d 69 6e 6f 72 20 20 20 20 20 20 20 20 20 2f  pMinor         /
2e2c8 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
2e2c9 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 74 6f 20   minor token to 
2e2ca 73 68 69 66 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20  shift in */.){. 
2e2cb 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79   yyStackEntry *y
2e2cc 79 74 6f 73 3b 0a 20 20 79 79 70 50 61 72 73 65  ytos;.  yypParse
2e2cd 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a 23 69 66 64  r->yyidx++;.#ifd
2e2ce 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41  ef YYTRACKMAXSTA
2e2cf 43 4b 44 45 50 54 48 0a 20 20 69 66 28 20 79 79  CKDEPTH.  if( yy
2e2d0 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 79  pParser->yyidx>y
2e2d1 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d  ypParser->yyidxM
2e2d2 61 78 20 29 7b 0a 20 20 20 20 79 79 70 50 61 72  ax ){.    yypPar
2e2d3 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20  ser->yyidxMax = 
2e2d4 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
2e2d5 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66  ;.  }.#endif.#if
2e2d6 20 59 59 53 54 41 43 4b 44 45 50 54 48 3e 30 20   YYSTACKDEPTH>0 
2e2d7 0a 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72  .  if( yypParser
2e2d8 2d 3e 79 79 69 64 78 3e 3d 59 59 53 54 41 43 4b  ->yyidx>=YYSTACK
2e2d9 44 45 50 54 48 20 29 7b 0a 20 20 20 20 79 79 53  DEPTH ){.    yyS
2e2da 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70  tackOverflow(yyp
2e2db 50 61 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72  Parser, yypMinor
2e2dc 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
2e2dd 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 79   }.#else.  if( y
2e2de 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
2e2df 3d 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74  =yypParser->yyst
2e2e0 6b 73 7a 20 29 7b 0a 20 20 20 20 79 79 47 72 6f  ksz ){.    yyGro
2e2e1 77 53 74 61 63 6b 28 79 79 70 50 61 72 73 65 72  wStack(yypParser
2e2e2 29 3b 0a 20 20 20 20 69 66 28 20 79 79 70 50 61  );.    if( yypPa
2e2e3 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70  rser->yyidx>=yyp
2e2e4 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20  Parser->yystksz 
2e2e5 29 7b 0a 20 20 20 20 20 20 79 79 53 74 61 63 6b  ){.      yyStack
2e2e6 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73  Overflow(yypPars
2e2e7 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20  er, yypMinor);. 
2e2e8 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2e2e9 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
2e2ea 79 79 74 6f 73 20 3d 20 26 79 79 70 50 61 72 73  yytos = &yypPars
2e2eb 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50  er->yystack[yypP
2e2ec 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 20  arser->yyidx];. 
2e2ed 20 79 79 74 6f 73 2d 3e 73 74 61 74 65 6e 6f 20   yytos->stateno 
2e2ee 3d 20 28 59 59 41 43 54 49 4f 4e 54 59 50 45 29  = (YYACTIONTYPE)
2e2ef 79 79 4e 65 77 53 74 61 74 65 3b 0a 20 20 79 79  yyNewState;.  yy
2e2f0 74 6f 73 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59 59  tos->major = (YY
2e2f1 43 4f 44 45 54 59 50 45 29 79 79 4d 61 6a 6f 72  CODETYPE)yyMajor
2e2f2 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72  ;.  yytos->minor
2e2f3 20 3d 20 2a 79 79 70 4d 69 6e 6f 72 3b 0a 23 69   = *yypMinor;.#i
2e2f4 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
2e2f5 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 26  f( yyTraceFILE &
2e2f6 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  & yypParser->yyi
2e2f7 64 78 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  dx>0 ){.    int 
2e2f8 69 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79  i;.    fprintf(y
2e2f9 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53 68  yTraceFILE,"%sSh
2e2fa 69 66 74 20 25 64 5c 6e 22 2c 79 79 54 72 61 63  ift %d\n",yyTrac
2e2fb 65 50 72 6f 6d 70 74 2c 79 79 4e 65 77 53 74 61  ePrompt,yyNewSta
2e2fc 74 65 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  te);.    fprintf
2e2fd 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
2e2fe 53 74 61 63 6b 3a 22 2c 79 79 54 72 61 63 65 50  Stack:",yyTraceP
2e2ff 72 6f 6d 70 74 29 3b 0a 20 20 20 20 66 6f 72 28  rompt);.    for(
2e300 69 3d 31 3b 20 69 3c 3d 79 79 70 50 61 72 73 65  i=1; i<=yypParse
2e301 72 2d 3e 79 79 69 64 78 3b 20 69 2b 2b 29 0a 20  r->yyidx; i++). 
2e302 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
2e303 72 61 63 65 46 49 4c 45 2c 22 20 25 73 22 2c 79  raceFILE," %s",y
2e304 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 70 50 61  yTokenName[yypPa
2e305 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 69 5d  rser->yystack[i]
2e306 2e 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20 20 66 70  .major]);.    fp
2e307 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2e308 45 2c 22 5c 6e 22 29 3b 0a 20 20 7d 0a 23 65 6e  E,"\n");.  }.#en
2e309 64 69 66 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f  dif.}../* The fo
2e30a 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 63 6f  llowing table co
2e30b 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69  ntains informati
2e30c 6f 6e 20 61 62 6f 75 74 20 65 76 65 72 79 20 72  on about every r
2e30d 75 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 75  ule that.** is u
2e30e 73 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 72  sed during the r
2e30f 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  educe..*/.static
2e310 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a   const struct {.
2e311 20 20 59 59 43 4f 44 45 54 59 50 45 20 6c 68 73    YYCODETYPE lhs
2e312 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 6d  ;         /* Sym
2e313 62 6f 6c 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d  bol on the left-
2e314 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65  hand side of the
2e315 20 72 75 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67   rule */.  unsig
2e316 6e 65 64 20 63 68 61 72 20 6e 72 68 73 3b 20 20  ned char nrhs;  
2e317 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2e318 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
2e319 73 79 6d 62 6f 6c 73 20 69 6e 20 74 68 65 20 72  symbols in the r
2e31a 75 6c 65 20 2a 2f 0a 7d 20 79 79 52 75 6c 65 49  ule */.} yyRuleI
2e31b 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 31 34  nfo[] = {.  { 14
2e31c 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  2, 1 },.  { 143,
2e31d 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 31   2 },.  { 143, 1
2e31e 20 7d 2c 0a 20 20 7b 20 31 34 34 2c 20 31 20 7d   },.  { 144, 1 }
2e31f 2c 0a 20 20 7b 20 31 34 34 2c 20 33 20 7d 2c 0a  ,.  { 144, 3 },.
2e320 20 20 7b 20 31 34 35 2c 20 30 20 7d 2c 0a 20 20    { 145, 0 },.  
2e321 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 145, 1 },.  { 
2e322 31 34 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  145, 3 },.  { 14
2e323 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  6, 1 },.  { 147,
2e324 20 33 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 30   3 },.  { 149, 0
2e325 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 31 20 7d   },.  { 149, 1 }
2e326 2c 0a 20 20 7b 20 31 34 39 2c 20 32 20 7d 2c 0a  ,.  { 149, 2 },.
2e327 20 20 7b 20 31 34 38 2c 20 30 20 7d 2c 0a 20 20    { 148, 0 },.  
2e328 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 148, 1 },.  { 
2e329 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  148, 1 },.  { 14
2e32a 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  8, 1 },.  { 147,
2e32b 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32   2 },.  { 147, 2
2e32c 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d   },.  { 147, 2 }
2e32d 2c 0a 20 20 7b 20 31 35 31 2c 20 31 20 7d 2c 0a  ,.  { 151, 1 },.
2e32e 20 20 7b 20 31 35 31 2c 20 30 20 7d 2c 0a 20 20    { 151, 0 },.  
2e32f 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 147, 2 },.  { 
2e330 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  147, 3 },.  { 14
2e331 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 5 },.  { 147,
2e332 20 32 20 7d 2c 0a 20 20 7b 20 31 35 32 2c 20 36   2 },.  { 152, 6
2e333 20 7d 2c 0a 20 20 7b 20 31 35 34 2c 20 31 20 7d   },.  { 154, 1 }
2e334 2c 0a 20 20 7b 20 31 35 36 2c 20 30 20 7d 2c 0a  ,.  { 156, 0 },.
2e335 20 20 7b 20 31 35 36 2c 20 33 20 7d 2c 0a 20 20    { 156, 3 },.  
2e336 7b 20 31 35 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 155, 1 },.  { 
2e337 31 35 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35  155, 0 },.  { 15
2e338 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 35 33 2c  3, 4 },.  { 153,
2e339 20 32 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 33   2 },.  { 158, 3
2e33a 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 31 20 7d   },.  { 158, 1 }
2e33b 2c 0a 20 20 7b 20 31 36 31 2c 20 33 20 7d 2c 0a  ,.  { 161, 3 },.
2e33c 20 20 7b 20 31 36 32 2c 20 31 20 7d 2c 0a 20 20    { 162, 1 },.  
2e33d 7b 20 31 36 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 165, 1 },.  { 
2e33e 31 36 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  165, 1 },.  { 16
2e33f 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c  6, 1 },.  { 150,
2e340 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31   1 },.  { 150, 1
2e341 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d   },.  { 150, 1 }
2e342 2c 0a 20 20 7b 20 31 36 33 2c 20 30 20 7d 2c 0a  ,.  { 163, 0 },.
2e343 20 20 7b 20 31 36 33 2c 20 31 20 7d 2c 0a 20 20    { 163, 1 },.  
2e344 7b 20 31 36 37 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 167, 1 },.  { 
2e345 31 36 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36  167, 4 },.  { 16
2e346 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 36 38 2c  7, 6 },.  { 168,
2e347 20 31 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 32   1 },.  { 168, 2
2e348 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d   },.  { 169, 1 }
2e349 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a  ,.  { 169, 1 },.
2e34a 20 20 7b 20 31 36 34 2c 20 32 20 7d 2c 0a 20 20    { 164, 2 },.  
2e34b 7b 20 31 36 34 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 164, 0 },.  { 
2e34c 31 37 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  172, 3 },.  { 17
2e34d 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  2, 1 },.  { 173,
2e34e 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34   2 },.  { 173, 4
2e34f 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d   },.  { 173, 3 }
2e350 2c 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a  ,.  { 173, 3 },.
2e351 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20    { 173, 2 },.  
2e352 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 173, 2 },.  { 
2e353 31 37 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  173, 3 },.  { 17
2e354 33 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  3, 5 },.  { 173,
2e355 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34   2 },.  { 173, 4
2e356 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d   },.  { 173, 4 }
2e357 2c 0a 20 20 7b 20 31 37 33 2c 20 31 20 7d 2c 0a  ,.  { 173, 1 },.
2e358 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20    { 173, 2 },.  
2e359 7b 20 31 37 38 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 178, 0 },.  { 
2e35a 31 37 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38  178, 1 },.  { 18
2e35b 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 30 2c  0, 0 },.  { 180,
2e35c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 32   2 },.  { 182, 2
2e35d 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d   },.  { 182, 3 }
2e35e 2c 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a  ,.  { 182, 3 },.
2e35f 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20 20    { 182, 3 },.  
2e360 7b 20 31 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 183, 2 },.  { 
2e361 31 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38  183, 2 },.  { 18
2e362 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 33 2c  3, 1 },.  { 183,
2e363 20 31 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 33   1 },.  { 181, 3
2e364 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 32 20 7d   },.  { 181, 2 }
2e365 2c 0a 20 20 7b 20 31 38 34 2c 20 30 20 7d 2c 0a  ,.  { 184, 0 },.
2e366 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20    { 184, 2 },.  
2e367 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 184, 2 },.  { 
2e368 31 35 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35  159, 0 },.  { 15
2e369 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 35 2c  9, 2 },.  { 185,
2e36a 20 33 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 32   3 },.  { 185, 2
2e36b 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 31 20 7d   },.  { 185, 1 }
2e36c 2c 0a 20 20 7b 20 31 38 36 2c 20 32 20 7d 2c 0a  ,.  { 186, 2 },.
2e36d 20 20 7b 20 31 38 36 2c 20 37 20 7d 2c 0a 20 20    { 186, 7 },.  
2e36e 7b 20 31 38 36 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 186, 5 },.  { 
2e36f 31 38 36 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38  186, 5 },.  { 18
2e370 36 2c 20 31 30 20 7d 2c 0a 20 20 7b 20 31 38 38  6, 10 },.  { 188
2e371 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20  , 0 },.  { 188, 
2e372 31 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 30 20  1 },.  { 176, 0 
2e373 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 33 20 7d 2c  },.  { 176, 3 },
2e374 0a 20 20 7b 20 31 38 39 2c 20 30 20 7d 2c 0a 20  .  { 189, 0 },. 
2e375 20 7b 20 31 38 39 2c 20 32 20 7d 2c 0a 20 20 7b   { 189, 2 },.  {
2e376 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   190, 1 },.  { 1
2e377 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 30  90, 1 },.  { 190
2e378 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
2e379 34 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 32 20  4 },.  { 192, 2 
2e37a 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 30 20 7d 2c  },.  { 192, 0 },
2e37b 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c 0a 20  .  { 147, 8 },. 
2e37c 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b   { 147, 4 },.  {
2e37d 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   147, 1 },.  { 1
2e37e 36 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 30  60, 1 },.  { 160
2e37f 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20  , 3 },.  { 195, 
2e380 31 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 32 20  1 },.  { 195, 2 
2e381 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 31 20 7d 2c  },.  { 195, 1 },
2e382 0a 20 20 7b 20 31 39 34 2c 20 39 20 7d 2c 0a 20  .  { 194, 9 },. 
2e383 20 7b 20 31 39 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 196, 1 },.  {
2e384 20 31 39 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   196, 1 },.  { 1
2e385 39 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 34  96, 0 },.  { 204
2e386 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20  , 2 },.  { 204, 
2e387 30 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 33 20  0 },.  { 197, 3 
2e388 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 32 20 7d 2c  },.  { 197, 2 },
2e389 0a 20 20 7b 20 31 39 37 2c 20 34 20 7d 2c 0a 20  .  { 197, 4 },. 
2e38a 20 7b 20 32 30 35 2c 20 32 20 7d 2c 0a 20 20 7b   { 205, 2 },.  {
2e38b 20 32 30 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   205, 1 },.  { 2
2e38c 30 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38  05, 0 },.  { 198
2e38d 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20  , 0 },.  { 198, 
2e38e 32 20 7d 2c 0a 20 20 7b 20 32 30 37 2c 20 32 20  2 },.  { 207, 2 
2e38f 7d 2c 0a 20 20 7b 20 32 30 37 2c 20 30 20 7d 2c  },.  { 207, 0 },
2e390 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20  .  { 206, 7 },. 
2e391 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b   { 206, 7 },.  {
2e392 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 31   206, 7 },.  { 1
2e393 35 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 37  57, 0 },.  { 157
2e394 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20  , 2 },.  { 193, 
2e395 32 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 31 20  2 },.  { 208, 1 
2e396 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 32 20 7d 2c  },.  { 208, 2 },
2e397 0a 20 20 7b 20 32 30 38 2c 20 33 20 7d 2c 0a 20  .  { 208, 3 },. 
2e398 20 7b 20 32 30 38 2c 20 34 20 7d 2c 0a 20 20 7b   { 208, 4 },.  {
2e399 20 32 31 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   210, 2 },.  { 2
2e39a 31 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 39  10, 0 },.  { 209
2e39b 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20  , 0 },.  { 209, 
2e39c 33 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 32 20  3 },.  { 209, 2 
2e39d 7d 2c 0a 20 20 7b 20 32 31 31 2c 20 34 20 7d 2c  },.  { 211, 4 },
2e39e 0a 20 20 7b 20 32 31 31 2c 20 30 20 7d 2c 0a 20  .  { 211, 0 },. 
2e39f 20 7b 20 32 30 32 2c 20 30 20 7d 2c 0a 20 20 7b   { 202, 0 },.  {
2e3a0 20 32 30 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   202, 3 },.  { 2
2e3a1 31 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31 34  14, 4 },.  { 214
2e3a2 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 35 2c 20  , 2 },.  { 215, 
2e3a3 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 31 20  1 },.  { 177, 1 
2e3a4 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 31 20 7d 2c  },.  { 177, 1 },
2e3a5 0a 20 20 7b 20 31 37 37 2c 20 30 20 7d 2c 0a 20  .  { 177, 0 },. 
2e3a6 20 7b 20 32 30 30 2c 20 30 20 7d 2c 0a 20 20 7b   { 200, 0 },.  {
2e3a7 20 32 30 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   200, 3 },.  { 2
2e3a8 30 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 31  01, 0 },.  { 201
2e3a9 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20  , 2 },.  { 203, 
2e3aa 30 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 32 20  0 },.  { 203, 2 
2e3ab 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c  },.  { 203, 4 },
2e3ac 0a 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20  .  { 203, 4 },. 
2e3ad 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b   { 147, 5 },.  {
2e3ae 20 31 39 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   199, 0 },.  { 1
2e3af 39 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37  99, 2 },.  { 147
2e3b0 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20  , 7 },.  { 217, 
2e3b1 35 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 33 20  5 },.  { 217, 3 
2e3b2 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c  },.  { 147, 8 },
2e3b3 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20  .  { 147, 5 },. 
2e3b4 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b   { 147, 6 },.  {
2e3b5 20 32 31 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   218, 2 },.  { 2
2e3b6 31 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 30  18, 1 },.  { 220
2e3b7 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20  , 3 },.  { 220, 
2e3b8 31 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 30 20  1 },.  { 219, 0 
2e3b9 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 33 20 7d 2c  },.  { 219, 3 },
2e3ba 0a 20 20 7b 20 32 31 33 2c 20 33 20 7d 2c 0a 20  .  { 213, 3 },. 
2e3bb 20 7b 20 32 31 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 213, 1 },.  {
2e3bc 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   175, 1 },.  { 1
2e3bd 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 34  75, 3 },.  { 174
2e3be 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 1 },.  { 175, 
2e3bf 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20  1 },.  { 175, 1 
2e3c0 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
2e3c1 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20  .  { 175, 5 },. 
2e3c2 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 174, 1 },.  {
2e3c3 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   174, 1 },.  { 1
2e3c4 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 1 },.  { 175
2e3c5 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 1 },.  { 175, 
2e3c6 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 36 20  3 },.  { 175, 6 
2e3c7 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c  },.  { 175, 5 },
2e3c8 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20  .  { 175, 4 },. 
2e3c9 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 174, 1 },.  {
2e3ca 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
2e3cb 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
2e3cc 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 3 },.  { 175, 
2e3cd 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  3 },.  { 175, 3 
2e3ce 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
2e3cf 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20  .  { 175, 3 },. 
2e3d0 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
2e3d1 20 32 32 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   222, 1 },.  { 2
2e3d2 32 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 32  22, 2 },.  { 222
2e3d3 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20  , 1 },.  { 222, 
2e3d4 32 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20 32 20  2 },.  { 223, 2 
2e3d5 7d 2c 0a 20 20 7b 20 32 32 33 2c 20 30 20 7d 2c  },.  { 223, 0 },
2e3d6 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20  .  { 175, 4 },. 
2e3d7 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b   { 175, 2 },.  {
2e3d8 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
2e3d9 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
2e3da 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 4 },.  { 175, 
2e3db 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20  2 },.  { 175, 2 
2e3dc 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c  },.  { 175, 2 },
2e3dd 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20  .  { 175, 2 },. 
2e3de 20 7b 20 32 32 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 224, 1 },.  {
2e3df 20 32 32 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   224, 2 },.  { 1
2e3e0 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 35  75, 5 },.  { 225
2e3e1 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20  , 1 },.  { 225, 
2e3e2 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20  2 },.  { 175, 5 
2e3e3 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
2e3e4 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20  .  { 175, 5 },. 
2e3e5 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b   { 175, 4 },.  {
2e3e6 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   175, 4 },.  { 1
2e3e7 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 37  75, 5 },.  { 227
2e3e8 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20  , 5 },.  { 227, 
2e3e9 34 20 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 32 20  4 },.  { 228, 2 
2e3ea 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 30 20 7d 2c  },.  { 228, 0 },
2e3eb 0a 20 20 7b 20 32 32 36 2c 20 31 20 7d 2c 0a 20  .  { 226, 1 },. 
2e3ec 20 7b 20 32 32 36 2c 20 30 20 7d 2c 0a 20 20 7b   { 226, 0 },.  {
2e3ed 20 32 32 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   221, 1 },.  { 2
2e3ee 32 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 36  21, 0 },.  { 216
2e3ef 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20  , 3 },.  { 216, 
2e3f0 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 31  1 },.  { 147, 11
2e3f1 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20 31 20 7d   },.  { 229, 1 }
2e3f2 2c 0a 20 20 7b 20 32 32 39 2c 20 30 20 7d 2c 0a  ,.  { 229, 0 },.
2e3f3 20 20 7b 20 31 37 39 2c 20 30 20 7d 2c 0a 20 20    { 179, 0 },.  
2e3f4 7b 20 31 37 39 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 179, 3 },.  { 
2e3f5 31 38 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38  187, 5 },.  { 18
2e3f6 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 30 2c  7, 3 },.  { 230,
2e3f7 20 30 20 7d 2c 0a 20 20 7b 20 32 33 30 2c 20 32   0 },.  { 230, 2
2e3f8 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d   },.  { 147, 4 }
2e3f9 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a  ,.  { 147, 1 },.
2e3fa 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20    { 147, 2 },.  
2e3fb 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 147, 3 },.  { 
2e3fc 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34  147, 5 },.  { 14
2e3fd 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 6 },.  { 147,
2e3fe 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36   5 },.  { 147, 6
2e3ff 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d   },.  { 231, 1 }
2e400 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a  ,.  { 231, 1 },.
2e401 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20    { 231, 1 },.  
2e402 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 231, 1 },.  { 
2e403 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  231, 1 },.  { 17
2e404 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 31 2c  0, 2 },.  { 171,
2e405 20 32 20 7d 2c 0a 20 20 7b 20 32 33 33 2c 20 31   2 },.  { 233, 1
2e406 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 31 20 7d   },.  { 232, 1 }
2e407 2c 0a 20 20 7b 20 32 33 32 2c 20 30 20 7d 2c 0a  ,.  { 232, 0 },.
2e408 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20    { 147, 5 },.  
2e409 7b 20 32 33 34 2c 20 31 31 20 7d 2c 0a 20 20 7b  { 234, 11 },.  {
2e40a 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   236, 1 },.  { 2
2e40b 33 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 36  36, 1 },.  { 236
2e40c 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20  , 2 },.  { 236, 
2e40d 30 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 31 20  0 },.  { 237, 1 
2e40e 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 31 20 7d 2c  },.  { 237, 1 },
2e40f 0a 20 20 7b 20 32 33 37 2c 20 33 20 7d 2c 0a 20  .  { 237, 3 },. 
2e410 20 7b 20 32 33 38 2c 20 30 20 7d 2c 0a 20 20 7b   { 238, 0 },.  {
2e411 20 32 33 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   238, 3 },.  { 2
2e412 33 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 39  39, 0 },.  { 239
2e413 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20  , 2 },.  { 235, 
2e414 33 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 32 20  3 },.  { 235, 2 
2e415 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 36 20 7d 2c  },.  { 240, 6 },
2e416 0a 20 20 7b 20 32 34 30 2c 20 38 20 7d 2c 0a 20  .  { 240, 8 },. 
2e417 20 7b 20 32 34 30 2c 20 35 20 7d 2c 0a 20 20 7b   { 240, 5 },.  {
2e418 20 32 34 30 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   240, 4 },.  { 2
2e419 34 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35  40, 1 },.  { 175
2e41a 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 4 },.  { 175, 
2e41b 36 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31 20  6 },.  { 191, 1 
2e41c 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31 20 7d 2c  },.  { 191, 1 },
2e41d 0a 20 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20  .  { 191, 1 },. 
2e41e 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b   { 147, 4 },.  {
2e41f 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31   147, 6 },.  { 1
2e420 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 32  47, 3 },.  { 242
2e421 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34 32 2c 20  , 0 },.  { 242, 
2e422 32 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 31 20  2 },.  { 241, 1 
2e423 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 30 20 7d 2c  },.  { 241, 0 },
2e424 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20  .  { 147, 1 },. 
2e425 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b   { 147, 3 },.  {
2e426 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   147, 1 },.  { 1
2e427 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 3 },.  { 147
2e428 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 6 },.  { 147, 
2e429 36 20 7d 2c 0a 20 20 7b 20 32 34 33 2c 20 31 20  6 },.  { 243, 1 
2e42a 7d 2c 0a 20 20 7b 20 32 34 34 2c 20 30 20 7d 2c  },.  { 244, 0 },
2e42b 0a 20 20 7b 20 32 34 34 2c 20 31 20 7d 2c 0a 20  .  { 244, 1 },. 
2e42c 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b   { 147, 1 },.  {
2e42d 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   147, 4 },.  { 2
2e42e 34 35 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 34 36  45, 7 },.  { 246
2e42f 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20  , 1 },.  { 246, 
2e430 33 20 7d 2c 0a 20 20 7b 20 32 34 37 2c 20 30 20  3 },.  { 247, 0 
2e431 7d 2c 0a 20 20 7b 20 32 34 37 2c 20 32 20 7d 2c  },.  { 247, 2 },
2e432 0a 20 20 7b 20 32 34 38 2c 20 31 20 7d 2c 0a 20  .  { 248, 1 },. 
2e433 20 7b 20 32 34 38 2c 20 33 20 7d 2c 0a 20 20 7b   { 248, 3 },.  {
2e434 20 32 34 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   249, 1 },.  { 2
2e435 35 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 35 30  50, 0 },.  { 250
2e436 2c 20 32 20 7d 2c 0a 7d 3b 0a 0a 73 74 61 74 69  , 2 },.};..stati
2e437 63 20 76 6f 69 64 20 79 79 5f 61 63 63 65 70 74  c void yy_accept
2e438 28 79 79 50 61 72 73 65 72 2a 29 3b 20 20 2f 2a  (yyParser*);  /*
2e439 20 46 6f 72 77 61 72 64 20 44 65 63 6c 61 72 61   Forward Declara
2e43a 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50  tion */../*.** P
2e43b 65 72 66 6f 72 6d 20 61 20 72 65 64 75 63 65 20  erform a reduce 
2e43c 61 63 74 69 6f 6e 20 61 6e 64 20 74 68 65 20 73  action and the s
2e43d 68 69 66 74 20 74 68 61 74 20 6d 75 73 74 20 69  hift that must i
2e43e 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 66 6f  mmediately.** fo
2e43f 6c 6c 6f 77 20 74 68 65 20 72 65 64 75 63 65 2e  llow the reduce.
2e440 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2e441 79 79 5f 72 65 64 75 63 65 28 0a 20 20 79 79 50  yy_reduce(.  yyP
2e442 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
2e443 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2e444 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74   parser */.  int
2e445 20 79 79 72 75 6c 65 6e 6f 20 20 20 20 20 20 20   yyruleno       
2e446 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2e447 62 65 72 20 6f 66 20 74 68 65 20 72 75 6c 65 20  ber of the rule 
2e448 62 79 20 77 68 69 63 68 20 74 6f 20 72 65 64 75  by which to redu
2e449 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 79  ce */.){.  int y
2e44a 79 67 6f 74 6f 3b 20 20 20 20 20 20 20 20 20 20  ygoto;          
2e44b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2e44c 65 20 6e 65 78 74 20 73 74 61 74 65 20 2a 2f 0a  e next state */.
2e44d 20 20 69 6e 74 20 79 79 61 63 74 3b 20 20 20 20    int yyact;    
2e44e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e44f 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 61 63    /* The next ac
2e450 74 69 6f 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f  tion */.  YYMINO
2e451 52 54 59 50 45 20 79 79 67 6f 74 6f 6d 69 6e 6f  RTYPE yygotomino
2e452 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r;        /* The
2e453 20 4c 48 53 20 6f 66 20 74 68 65 20 72 75 6c 65   LHS of the rule
2e454 20 72 65 64 75 63 65 64 20 2a 2f 0a 20 20 79 79   reduced */.  yy
2e455 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 6d 73  StackEntry *yyms
2e456 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p;            /*
2e457 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   The top of the 
2e458 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20 2a  parser's stack *
2e459 2f 0a 20 20 69 6e 74 20 79 79 73 69 7a 65 3b 20  /.  int yysize; 
2e45a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e45b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 74 6f      /* Amount to
2e45c 20 70 6f 70 20 74 68 65 20 73 74 61 63 6b 20 2a   pop the stack *
2e45d 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
2e45e 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 79 79  rARG_FETCH;.  yy
2e45f 6d 73 70 20 3d 20 26 79 79 70 50 61 72 73 65 72  msp = &yypParser
2e460 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72  ->yystack[yypPar
2e461 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 23 69 66  ser->yyidx];.#if
2e462 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66  ndef NDEBUG.  if
2e463 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 26 26  ( yyTraceFILE &&
2e464 20 79 79 72 75 6c 65 6e 6f 3e 3d 30 20 0a 20 20   yyruleno>=0 .  
2e465 20 20 20 20 20 20 26 26 20 79 79 72 75 6c 65 6e        && yyrulen
2e466 6f 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 79  o<(int)(sizeof(y
2e467 79 52 75 6c 65 4e 61 6d 65 29 2f 73 69 7a 65 6f  yRuleName)/sizeo
2e468 66 28 79 79 52 75 6c 65 4e 61 6d 65 5b 30 5d 29  f(yyRuleName[0])
2e469 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66  ) ){.    fprintf
2e46a 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22 25  (yyTraceFILE, "%
2e46b 73 52 65 64 75 63 65 20 5b 25 73 5d 2e 5c 6e 22  sReduce [%s].\n"
2e46c 2c 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c  , yyTracePrompt,
2e46d 0a 20 20 20 20 20 20 79 79 52 75 6c 65 4e 61 6d  .      yyRuleNam
2e46e 65 5b 79 79 72 75 6c 65 6e 6f 5d 29 3b 0a 20 20  e[yyruleno]);.  
2e46f 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  }.#endif /* NDEB
2e470 55 47 20 2a 2f 0a 0a 20 20 2f 2a 20 53 69 6c 65  UG */..  /* Sile
2e471 6e 63 65 20 63 6f 6d 70 6c 61 69 6e 74 73 20 66  nce complaints f
2e472 72 6f 6d 20 70 75 72 69 66 79 20 61 62 6f 75 74  rom purify about
2e473 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 62 65 69   yygotominor bei
2e474 6e 67 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  ng uninitialized
2e475 0a 20 20 2a 2a 20 69 6e 20 73 6f 6d 65 20 63 61  .  ** in some ca
2e476 73 65 73 20 77 68 65 6e 20 69 74 20 69 73 20 63  ses when it is c
2e477 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 73  opied into the s
2e478 74 61 63 6b 20 61 66 74 65 72 20 74 68 65 20 66  tack after the f
2e479 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 77  ollowing.  ** sw
2e47a 69 74 63 68 2e 20 20 79 79 67 6f 74 6f 6d 69 6e  itch.  yygotomin
2e47b 6f 72 20 69 73 20 75 6e 69 6e 69 74 69 61 6c 69  or is uninitiali
2e47c 7a 65 64 20 77 68 65 6e 20 61 20 72 75 6c 65 20  zed when a rule 
2e47d 72 65 64 75 63 65 73 20 74 68 61 74 20 64 6f 65  reduces that doe
2e47e 73 0a 20 20 2a 2a 20 6e 6f 74 20 73 65 74 20 74  s.  ** not set t
2e47f 68 65 20 76 61 6c 75 65 20 6f 66 20 69 74 73 20  he value of its 
2e480 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6e  left-hand side n
2e481 6f 6e 74 65 72 6d 69 6e 61 6c 2e 20 20 4c 65 61  onterminal.  Lea
2e482 76 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 76 61  ving the.  ** va
2e483 6c 75 65 20 6f 66 20 74 68 65 20 6e 6f 6e 74 65  lue of the nonte
2e484 72 6d 69 6e 61 6c 20 75 6e 69 6e 69 74 69 61 6c  rminal uninitial
2e485 69 7a 65 64 20 69 73 20 75 74 74 65 72 6c 79 20  ized is utterly 
2e486 68 61 72 6d 6c 65 73 73 20 61 73 20 6c 6f 6e 67  harmless as long
2e487 0a 20 20 2a 2a 20 61 73 20 74 68 65 20 76 61 6c  .  ** as the val
2e488 75 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64  ue is never used
2e489 2e 20 20 53 6f 20 72 65 61 6c 6c 79 20 74 68 65  .  So really the
2e48a 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 69 73   only thing this
2e48b 20 63 6f 64 65 0a 20 20 2a 2a 20 61 63 63 6f 6d   code.  ** accom
2e48c 70 6c 69 73 68 65 73 20 69 73 20 74 6f 20 71 75  plishes is to qu
2e48d 69 65 74 65 6e 20 70 75 72 69 66 79 2e 20 20 0a  ieten purify.  .
2e48e 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 37 2d 30    **.  ** 2007-0
2e48f 31 2d 31 36 3a 20 20 54 68 65 20 77 69 72 65 73  1-16:  The wires
2e490 68 61 72 6b 20 70 72 6f 6a 65 63 74 20 28 77 77  hark project (ww
2e491 77 2e 77 69 72 65 73 68 61 72 6b 2e 6f 72 67 29  w.wireshark.org)
2e492 20 72 65 70 6f 72 74 73 20 74 68 61 74 0a 20 20   reports that.  
2e493 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20  ** without this 
2e494 63 6f 64 65 2c 20 74 68 65 69 72 20 70 61 72 73  code, their pars
2e495 65 72 20 73 65 67 66 61 75 6c 74 73 2e 20 20 49  er segfaults.  I
2e496 27 6d 20 6e 6f 74 20 73 75 72 65 20 77 68 61 74  'm not sure what
2e497 20 74 68 65 72 65 0a 20 20 2a 2a 20 70 61 72 73   there.  ** pars
2e498 65 72 20 69 73 20 64 6f 69 6e 67 20 74 6f 20 6d  er is doing to m
2e499 61 6b 65 20 74 68 69 73 20 68 61 70 70 65 6e 2e  ake this happen.
2e49a 20 20 54 68 69 73 20 69 73 20 74 68 65 20 73 65    This is the se
2e49b 63 6f 6e 64 20 62 75 67 20 72 65 70 6f 72 74 0a  cond bug report.
2e49c 20 20 2a 2a 20 66 72 6f 6d 20 77 69 72 65 73 68    ** from wiresh
2e49d 61 72 6b 20 74 68 69 73 20 77 65 65 6b 2e 20 20  ark this week.  
2e49e 43 6c 65 61 72 6c 79 20 74 68 65 79 20 61 72 65  Clearly they are
2e49f 20 73 74 72 65 73 73 69 6e 67 20 4c 65 6d 6f 6e   stressing Lemon
2e4a0 20 69 6e 20 77 61 79 73 0a 20 20 2a 2a 20 74 68   in ways.  ** th
2e4a1 61 74 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65  at it has not be
2e4a2 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74  en previously st
2e4a3 72 65 73 73 65 64 2e 2e 2e 20 20 28 53 51 4c 69  ressed...  (SQLi
2e4a4 74 65 20 74 69 63 6b 65 74 20 23 32 31 37 32 29  te ticket #2172)
2e4a5 0a 20 20 2a 2f 0a 20 20 2f 2a 6d 65 6d 73 65 74  .  */.  /*memset
2e4a6 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2c 20 30  (&yygotominor, 0
2e4a7 2c 20 73 69 7a 65 6f 66 28 79 79 67 6f 74 6f 6d  , sizeof(yygotom
2e4a8 69 6e 6f 72 29 29 3b 2a 2f 0a 20 20 79 79 67 6f  inor));*/.  yygo
2e4a9 74 6f 6d 69 6e 6f 72 20 3d 20 79 79 7a 65 72 6f  tominor = yyzero
2e4aa 6d 69 6e 6f 72 3b 0a 0a 0a 20 20 73 77 69 74 63  minor;...  switc
2e4ab 68 28 20 79 79 72 75 6c 65 6e 6f 20 29 7b 0a 20  h( yyruleno ){. 
2e4ac 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 68 65   /* Beginning he
2e4ad 72 65 20 61 72 65 20 74 68 65 20 72 65 64 75 63  re are the reduc
2e4ae 74 69 6f 6e 20 63 61 73 65 73 2e 20 20 41 20 74  tion cases.  A t
2e4af 79 70 69 63 61 6c 20 65 78 61 6d 70 6c 65 0a 20  ypical example. 
2e4b0 20 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a   ** follows:.  *
2e4b1 2a 20 20 20 63 61 73 65 20 30 3a 0a 20 20 2a 2a  *   case 0:.  **
2e4b2 20 20 23 6c 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e    #line <lineno>
2e4b3 20 3c 67 72 61 6d 6d 61 72 66 69 6c 65 3e 0a 20   <grammarfile>. 
2e4b4 20 2a 2a 20 20 20 20 20 7b 20 2e 2e 2e 20 7d 20   **     { ... } 
2e4b5 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55 73 65            // Use
2e4b6 72 20 73 75 70 70 6c 69 65 64 20 63 6f 64 65 0a  r supplied code.
2e4b7 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69 6e    **  #line <lin
2e4b8 65 6e 6f 3e 20 3c 74 68 69 73 66 69 6c 65 3e 0a  eno> <thisfile>.
2e4b9 20 20 2a 2a 20 20 20 20 20 62 72 65 61 6b 3b 0a    **     break;.
2e4ba 20 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20    */.      case 
2e4bb 30 3a 20 2f 2a 20 69 6e 70 75 74 20 3a 3a 3d 20  0: /* input ::= 
2e4bc 63 6d 64 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20  cmdlist */.     
2e4bd 20 63 61 73 65 20 31 3a 20 2f 2a 20 63 6d 64 6c   case 1: /* cmdl
2e4be 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20  ist ::= cmdlist 
2e4bf 65 63 6d 64 20 2a 2f 0a 20 20 20 20 20 20 63 61  ecmd */.      ca
2e4c0 73 65 20 32 3a 20 2f 2a 20 63 6d 64 6c 69 73 74  se 2: /* cmdlist
2e4c1 20 3a 3a 3d 20 65 63 6d 64 20 2a 2f 0a 20 20 20   ::= ecmd */.   
2e4c2 20 20 20 63 61 73 65 20 33 3a 20 2f 2a 20 65 63     case 3: /* ec
2e4c3 6d 64 20 3a 3a 3d 20 53 45 4d 49 20 2a 2f 0a 20  md ::= SEMI */. 
2e4c4 20 20 20 20 20 63 61 73 65 20 34 3a 20 2f 2a 20       case 4: /* 
2e4c5 65 63 6d 64 20 3a 3a 3d 20 65 78 70 6c 61 69 6e  ecmd ::= explain
2e4c6 20 63 6d 64 78 20 53 45 4d 49 20 2a 2f 0a 20 20   cmdx SEMI */.  
2e4c7 20 20 20 20 63 61 73 65 20 31 30 3a 20 2f 2a 20      case 10: /* 
2e4c8 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  trans_opt ::= */
2e4c9 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 3a 20  .      case 11: 
2e4ca 2f 2a 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d  /* trans_opt ::=
2e4cb 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 2a 2f 0a   TRANSACTION */.
2e4cc 20 20 20 20 20 20 63 61 73 65 20 31 32 3a 20 2f        case 12: /
2e4cd 2a 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20  * trans_opt ::= 
2e4ce 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 20 2a  TRANSACTION nm *
2e4cf 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 3a  /.      case 20:
2e4d0 20 2f 2a 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70   /* savepoint_op
2e4d1 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20  t ::= SAVEPOINT 
2e4d2 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  */.      case 21
2e4d3 3a 20 2f 2a 20 73 61 76 65 70 6f 69 6e 74 5f 6f  : /* savepoint_o
2e4d4 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  pt ::= */.      
2e4d5 63 61 73 65 20 32 35 3a 20 2f 2a 20 63 6d 64 20  case 25: /* cmd 
2e4d6 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65  ::= create_table
2e4d7 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72   create_table_ar
2e4d8 67 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  gs */.      case
2e4d9 20 33 34 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 6c 69   34: /* columnli
2e4da 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73  st ::= columnlis
2e4db 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 20 2a  t COMMA column *
2e4dc 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 35 3a  /.      case 35:
2e4dd 20 2f 2a 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a   /* columnlist :
2e4de 3a 3d 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20  := column */.   
2e4df 20 20 20 63 61 73 65 20 34 34 3a 20 2f 2a 20 74     case 44: /* t
2e4e0 79 70 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20  ype ::= */.     
2e4e1 20 63 61 73 65 20 35 31 3a 20 2f 2a 20 73 69 67   case 51: /* sig
2e4e2 6e 65 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d  ned ::= plus_num
2e4e3 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 35   */.      case 5
2e4e4 32 3a 20 2f 2a 20 73 69 67 6e 65 64 20 3a 3a 3d  2: /* signed ::=
2e4e5 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f 0a 20 20   minus_num */.  
2e4e6 20 20 20 20 63 61 73 65 20 35 33 3a 20 2f 2a 20      case 53: /* 
2e4e7 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72  carglist ::= car
2e4e8 67 6c 69 73 74 20 63 61 72 67 20 2a 2f 0a 20 20  glist carg */.  
2e4e9 20 20 20 20 63 61 73 65 20 35 34 3a 20 2f 2a 20      case 54: /* 
2e4ea 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 2a 2f 0a  carglist ::= */.
2e4eb 20 20 20 20 20 20 63 61 73 65 20 35 35 3a 20 2f        case 55: /
2e4ec 2a 20 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54  * carg ::= CONST
2e4ed 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 20 2a  RAINT nm ccons *
2e4ee 2f 0a 20 20 20 20 20 20 63 61 73 65 20 35 36 3a  /.      case 56:
2e4ef 20 2f 2a 20 63 61 72 67 20 3a 3a 3d 20 63 63 6f   /* carg ::= cco
2e4f0 6e 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  ns */.      case
2e4f1 20 36 32 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a   62: /* ccons ::
2e4f2 3d 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f  = NULL onconf */
2e4f3 0a 20 20 20 20 20 20 63 61 73 65 20 38 39 3a 20  .      case 89: 
2e4f4 2f 2a 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20  /* conslist ::= 
2e4f5 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74  conslist COMMA t
2e4f6 63 6f 6e 73 20 2a 2f 0a 20 20 20 20 20 20 63 61  cons */.      ca
2e4f7 73 65 20 39 30 3a 20 2f 2a 20 63 6f 6e 73 6c 69  se 90: /* consli
2e4f8 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20  st ::= conslist 
2e4f9 74 63 6f 6e 73 20 2a 2f 0a 20 20 20 20 20 20 63  tcons */.      c
2e4fa 61 73 65 20 39 31 3a 20 2f 2a 20 63 6f 6e 73 6c  ase 91: /* consl
2e4fb 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 20 2a 2f  ist ::= tcons */
2e4fc 0a 20 20 20 20 20 20 63 61 73 65 20 39 32 3a 20  .      case 92: 
2e4fd 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e  /* tcons ::= CON
2e4fe 53 54 52 41 49 4e 54 20 6e 6d 20 2a 2f 0a 20 20  STRAINT nm */.  
2e4ff 20 20 20 20 63 61 73 65 20 32 36 38 3a 20 2f 2a      case 268: /*
2e500 20 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c   plus_opt ::= PL
2e501 55 53 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  US */.      case
2e502 20 32 36 39 3a 20 2f 2a 20 70 6c 75 73 5f 6f 70   269: /* plus_op
2e503 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  t ::= */.      c
2e504 61 73 65 20 32 37 39 3a 20 2f 2a 20 66 6f 72 65  ase 279: /* fore
2e505 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a  ach_clause ::= *
2e506 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 30  /.      case 280
2e507 3a 20 2f 2a 20 66 6f 72 65 61 63 68 5f 63 6c 61  : /* foreach_cla
2e508 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48  use ::= FOR EACH
2e509 20 52 4f 57 20 2a 2f 0a 20 20 20 20 20 20 63 61   ROW */.      ca
2e50a 73 65 20 33 30 30 3a 20 2f 2a 20 64 61 74 61 62  se 300: /* datab
2e50b 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44  ase_kw_opt ::= D
2e50c 41 54 41 42 41 53 45 20 2a 2f 0a 20 20 20 20 20  ATABASE */.     
2e50d 20 63 61 73 65 20 33 30 31 3a 20 2f 2a 20 64 61   case 301: /* da
2e50e 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a  tabase_kw_opt ::
2e50f 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2e510 33 30 39 3a 20 2f 2a 20 6b 77 63 6f 6c 75 6d 6e  309: /* kwcolumn
2e511 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
2e512 20 20 63 61 73 65 20 33 31 30 3a 20 2f 2a 20 6b    case 310: /* k
2e513 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20  wcolumn_opt ::= 
2e514 43 4f 4c 55 4d 4e 4b 57 20 2a 2f 0a 20 20 20 20  COLUMNKW */.    
2e515 20 20 63 61 73 65 20 33 31 34 3a 20 2f 2a 20 76    case 314: /* v
2e516 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
2e517 74 61 62 61 72 67 20 2a 2f 0a 20 20 20 20 20 20  tabarg */.      
2e518 63 61 73 65 20 33 31 35 3a 20 2f 2a 20 76 74 61  case 315: /* vta
2e519 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61  barglist ::= vta
2e51a 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20 76  barglist COMMA v
2e51b 74 61 62 61 72 67 20 2a 2f 0a 20 20 20 20 20 20  tabarg */.      
2e51c 63 61 73 65 20 33 31 37 3a 20 2f 2a 20 76 74 61  case 317: /* vta
2e51d 62 61 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67  barg ::= vtabarg
2e51e 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 2a 2f   vtabargtoken */
2e51f 0a 20 20 20 20 20 20 63 61 73 65 20 33 32 31 3a  .      case 321:
2e520 20 2f 2a 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20   /* anylist ::= 
2e521 2a 2f 0a 7b 0a 7d 0a 20 20 20 20 20 20 20 20 62  */.{.}.        b
2e522 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e523 20 35 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a   5: /* explain :
2e524 3a 3d 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42  := */.{ sqlite3B
2e525 65 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65  eginParse(pParse
2e526 2c 20 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  , 0); }.        
2e527 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e528 65 20 36 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20  e 6: /* explain 
2e529 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 2a 2f 0a 7b  ::= EXPLAIN */.{
2e52a 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72   sqlite3BeginPar
2e52b 73 65 28 70 50 61 72 73 65 2c 20 31 29 3b 20 7d  se(pParse, 1); }
2e52c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e52d 20 20 20 20 20 20 63 61 73 65 20 37 3a 20 2f 2a        case 7: /*
2e52e 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50   explain ::= EXP
2e52f 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20  LAIN QUERY PLAN 
2e530 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69  */.{ sqlite3Begi
2e531 6e 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 32  nParse(pParse, 2
2e532 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
2e533 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38  ak;.      case 8
2e534 3a 20 2f 2a 20 63 6d 64 78 20 3a 3a 3d 20 63 6d  : /* cmdx ::= cm
2e535 64 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 46 69  d */.{ sqlite3Fi
2e536 6e 69 73 68 43 6f 64 69 6e 67 28 70 50 61 72 73  nishCoding(pPars
2e537 65 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  e); }.        br
2e538 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e539 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 42 45  9: /* cmd ::= BE
2e53a 47 49 4e 20 74 72 61 6e 73 74 79 70 65 20 74 72  GIN transtype tr
2e53b 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c 69  ans_opt */.{sqli
2e53c 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74  te3BeginTransact
2e53d 69 6f 6e 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ion(pParse, yyms
2e53e 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39  p[-1].minor.yy19
2e53f 34 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  4);}.        bre
2e540 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e541 33 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20  3: /* transtype 
2e542 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
2e543 6e 6f 72 2e 79 79 31 39 34 20 3d 20 54 4b 5f 44  nor.yy194 = TK_D
2e544 45 46 45 52 52 45 44 3b 7d 0a 20 20 20 20 20 20  EFERRED;}.      
2e545 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e546 61 73 65 20 31 34 3a 20 2f 2a 20 74 72 61 6e 73  ase 14: /* trans
2e547 74 79 70 65 20 3a 3a 3d 20 44 45 46 45 52 52 45  type ::= DEFERRE
2e548 44 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  D */.      case 
2e549 31 35 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65  15: /* transtype
2e54a 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 20 2a   ::= IMMEDIATE *
2e54b 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 3a  /.      case 16:
2e54c 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a   /* transtype ::
2e54d 3d 20 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20  = EXCLUSIVE */. 
2e54e 20 20 20 20 20 63 61 73 65 20 31 31 34 3a 20 2f       case 114: /
2e54f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70  * multiselect_op
2e550 20 3a 3a 3d 20 55 4e 49 4f 4e 20 2a 2f 0a 20 20   ::= UNION */.  
2e551 20 20 20 20 63 61 73 65 20 31 31 36 3a 20 2f 2a      case 116: /*
2e552 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20   multiselect_op 
2e553 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52  ::= EXCEPT|INTER
2e554 53 45 43 54 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  SECT */.{yygotom
2e555 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 79 79 6d  inor.yy194 = yym
2e556 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a 20 20  sp[0].major;}.  
2e557 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e558 20 20 20 63 61 73 65 20 31 37 3a 20 2f 2a 20 63     case 17: /* c
2e559 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72  md ::= COMMIT tr
2e55a 61 6e 73 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20  ans_opt */.     
2e55b 20 63 61 73 65 20 31 38 3a 20 2f 2a 20 63 6d 64   case 18: /* cmd
2e55c 20 3a 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f   ::= END trans_o
2e55d 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 6f  pt */.{sqlite3Co
2e55e 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28  mmitTransaction(
2e55f 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20  pParse);}.      
2e560 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e561 61 73 65 20 31 39 3a 20 2f 2a 20 63 6d 64 20 3a  ase 19: /* cmd :
2e562 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e  := ROLLBACK tran
2e563 73 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65  s_opt */.{sqlite
2e564 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63  3RollbackTransac
2e565 74 69 6f 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20  tion(pParse);}. 
2e566 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e567 20 20 20 20 63 61 73 65 20 32 32 3a 20 2f 2a 20      case 22: /* 
2e568 63 6d 64 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e  cmd ::= SAVEPOIN
2e569 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  T nm */.{.  sqli
2e56a 74 65 33 53 61 76 65 70 6f 69 6e 74 28 70 50 61  te3Savepoint(pPa
2e56b 72 73 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f 42  rse, SAVEPOINT_B
2e56c 45 47 49 4e 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  EGIN, &yymsp[0].
2e56d 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
2e56e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e56f 20 20 20 63 61 73 65 20 32 33 3a 20 2f 2a 20 63     case 23: /* c
2e570 6d 64 20 3a 3a 3d 20 52 45 4c 45 41 53 45 20 73  md ::= RELEASE s
2e571 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 20  avepoint_opt nm 
2e572 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61  */.{.  sqlite3Sa
2e573 76 65 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20  vepoint(pParse, 
2e574 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
2e575 45 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  E, &yymsp[0].min
2e576 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
2e577 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e578 63 61 73 65 20 32 34 3a 20 2f 2a 20 63 6d 64 20  case 24: /* cmd 
2e579 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61  ::= ROLLBACK tra
2e57a 6e 73 5f 6f 70 74 20 54 4f 20 73 61 76 65 70 6f  ns_opt TO savepo
2e57b 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a  int_opt nm */.{.
2e57c 20 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69    sqlite3Savepoi
2e57d 6e 74 28 70 50 61 72 73 65 2c 20 53 41 56 45 50  nt(pParse, SAVEP
2e57e 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 26  OINT_ROLLBACK, &
2e57f 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e580 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
2e581 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e582 20 32 36 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74   26: /* create_t
2e583 61 62 6c 65 20 3a 3a 3d 20 63 72 65 61 74 65 6b  able ::= createk
2e584 77 20 74 65 6d 70 20 54 41 42 4c 45 20 69 66 6e  w temp TABLE ifn
2e585 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d  otexists nm dbnm
2e586 20 2a 2f 0a 7b 0a 20 20 20 73 71 6c 69 74 65 33   */.{.   sqlite3
2e587 53 74 61 72 74 54 61 62 6c 65 28 70 50 61 72 73  StartTable(pPars
2e588 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e,&yymsp[-1].min
2e589 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
2e58a 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70  .minor.yy0,yymsp
2e58b 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34  [-4].minor.yy194
2e58c 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ,0,0,yymsp[-2].m
2e58d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 0a 7d 0a 20  inor.yy194);.}. 
2e58e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e58f 20 20 20 20 63 61 73 65 20 32 37 3a 20 2f 2a 20      case 27: /* 
2e590 63 72 65 61 74 65 6b 77 20 3a 3a 3d 20 43 52 45  createkw ::= CRE
2e591 41 54 45 20 2a 2f 0a 7b 0a 20 20 70 50 61 72 73  ATE */.{.  pPars
2e592 65 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  e->db->lookaside
2e593 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20  .bEnabled = 0;. 
2e594 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
2e595 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
2e596 72 2e 79 79 30 3b 0a 7d 0a 20 20 20 20 20 20 20  r.yy0;.}.       
2e597 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e598 73 65 20 32 38 3a 20 2f 2a 20 69 66 6e 6f 74 65  se 28: /* ifnote
2e599 78 69 73 74 73 20 3a 3a 3d 20 2a 2f 0a 20 20 20  xists ::= */.   
2e59a 20 20 20 63 61 73 65 20 33 31 3a 20 2f 2a 20 74     case 31: /* t
2e59b 65 6d 70 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20  emp ::= */.     
2e59c 20 63 61 73 65 20 37 30 3a 20 2f 2a 20 61 75 74   case 70: /* aut
2e59d 6f 69 6e 63 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  oinc ::= */.    
2e59e 20 20 63 61 73 65 20 38 34 3a 20 2f 2a 20 69 6e    case 84: /* in
2e59f 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
2e5a0 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
2e5a1 20 20 63 61 73 65 20 38 36 3a 20 2f 2a 20 69 6e    case 86: /* in
2e5a2 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
2e5a3 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c  _opt ::= INITIAL
2e5a4 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 2a 2f 0a  LY IMMEDIATE */.
2e5a5 20 20 20 20 20 20 63 61 73 65 20 39 37 3a 20 2f        case 97: /
2e5a6 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73  * defer_subclaus
2e5a7 65 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20  e_opt ::= */.   
2e5a8 20 20 20 63 61 73 65 20 31 30 38 3a 20 2f 2a 20     case 108: /* 
2e5a9 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 2a 2f 0a  ifexists ::= */.
2e5aa 20 20 20 20 20 20 63 61 73 65 20 31 31 39 3a 20        case 119: 
2e5ab 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20  /* distinct ::= 
2e5ac 41 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ALL */.      cas
2e5ad 65 20 31 32 30 3a 20 2f 2a 20 64 69 73 74 69 6e  e 120: /* distin
2e5ae 63 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  ct ::= */.      
2e5af 63 61 73 65 20 32 32 32 3a 20 2f 2a 20 62 65 74  case 222: /* bet
2e5b0 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45 54 57  ween_op ::= BETW
2e5b1 45 45 4e 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  EEN */.      cas
2e5b2 65 20 32 32 35 3a 20 2f 2a 20 69 6e 5f 6f 70 20  e 225: /* in_op 
2e5b3 3a 3a 3d 20 49 4e 20 2a 2f 0a 7b 79 79 67 6f 74  ::= IN */.{yygot
2e5b4 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 30  ominor.yy194 = 0
2e5b5 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2e5b6 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 3a  ;.      case 29:
2e5b7 20 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74 73 20   /* ifnotexists 
2e5b8 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58 49 53 54  ::= IF NOT EXIST
2e5b9 53 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  S */.      case 
2e5ba 33 30 3a 20 2f 2a 20 74 65 6d 70 20 3a 3a 3d 20  30: /* temp ::= 
2e5bb 54 45 4d 50 20 2a 2f 0a 20 20 20 20 20 20 63 61  TEMP */.      ca
2e5bc 73 65 20 37 31 3a 20 2f 2a 20 61 75 74 6f 69 6e  se 71: /* autoin
2e5bd 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 20 2a  c ::= AUTOINCR *
2e5be 2f 0a 20 20 20 20 20 20 63 61 73 65 20 38 35 3a  /.      case 85:
2e5bf 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72 72 65   /* init_deferre
2e5c0 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49  d_pred_opt ::= I
2e5c1 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45  NITIALLY DEFERRE
2e5c2 44 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  D */.      case 
2e5c3 31 30 37 3a 20 2f 2a 20 69 66 65 78 69 73 74 73  107: /* ifexists
2e5c4 20 3a 3a 3d 20 49 46 20 45 58 49 53 54 53 20 2a   ::= IF EXISTS *
2e5c5 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 38  /.      case 118
2e5c6 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a  : /* distinct ::
2e5c7 3d 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20  = DISTINCT */.  
2e5c8 20 20 20 20 63 61 73 65 20 32 32 33 3a 20 2f 2a      case 223: /*
2e5c9 20 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20   between_op ::= 
2e5ca 4e 4f 54 20 42 45 54 57 45 45 4e 20 2a 2f 0a 20  NOT BETWEEN */. 
2e5cb 20 20 20 20 20 63 61 73 65 20 32 32 36 3a 20 2f       case 226: /
2e5cc 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20  * in_op ::= NOT 
2e5cd 49 4e 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  IN */.{yygotomin
2e5ce 6f 72 2e 79 79 31 39 34 20 3d 20 31 3b 7d 0a 20  or.yy194 = 1;}. 
2e5cf 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e5d0 20 20 20 20 63 61 73 65 20 33 32 3a 20 2f 2a 20      case 32: /* 
2e5d1 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67  create_table_arg
2e5d2 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d 6e 6c  s ::= LP columnl
2e5d3 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74  ist conslist_opt
2e5d4 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74   RP */.{.  sqlit
2e5d5 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73  e3EndTable(pPars
2e5d6 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e,&yymsp[-1].min
2e5d7 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
2e5d8 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 0a 7d  .minor.yy0,0);.}
2e5d9 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e5da 20 20 20 20 20 20 63 61 73 65 20 33 33 3a 20 2f        case 33: /
2e5db 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61  * create_table_a
2e5dc 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c 65 63  rgs ::= AS selec
2e5dd 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  t */.{.  sqlite3
2e5de 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c  EndTable(pParse,
2e5df 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0,0,yymsp[0].min
2e5e0 6f 72 2e 79 79 32 34 33 29 3b 0a 20 20 73 71 6c  or.yy243);.  sql
2e5e1 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2e5e2 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
2e5e3 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34  sp[0].minor.yy24
2e5e4 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  3);.}.        br
2e5e5 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e5e6 33 36 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 20 3a 3a  36: /* column ::
2e5e7 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79 70 65 20  = columnid type 
2e5e8 63 61 72 67 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20  carglist */.{.  
2e5e9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
2e5ea 7a 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  z = yymsp[-2].mi
2e5eb 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67  nor.yy0.z;.  yyg
2e5ec 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d  otominor.yy0.n =
2e5ed 20 28 69 6e 74 29 28 70 50 61 72 73 65 2d 3e 73   (int)(pParse->s
2e5ee 4c 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79 79 6d 73  LastToken.z-yyms
2e5ef 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-2].minor.yy0.
2e5f0 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e 73 4c 61  z) + pParse->sLa
2e5f1 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 79 79 67  stToken.n;.  yyg
2e5f2 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 71 75 6f  otominor.yy0.quo
2e5f3 74 65 64 20 3d 20 30 3b 0a 20 20 79 79 67 6f 74  ted = 0;.  yygot
2e5f4 6f 6d 69 6e 6f 72 2e 79 79 30 2e 64 79 6e 20 3d  ominor.yy0.dyn =
2e5f5 20 30 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72   0;.}.        br
2e5f6 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e5f7 33 37 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 69 64 20  37: /* columnid 
2e5f8 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71  ::= nm */.{.  sq
2e5f9 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 70  lite3AddColumn(p
2e5fa 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e  Parse,&yymsp[0].
2e5fb 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 79 79  minor.yy0);.  yy
2e5fc 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20  gotominor.yy0 = 
2e5fd 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e5fe 79 30 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  y0;.}.        br
2e5ff 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e600 33 38 3a 20 2f 2a 20 69 64 20 3a 3a 3d 20 49 44  38: /* id ::= ID
2e601 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
2e602 39 3a 20 2f 2a 20 69 64 20 3a 3a 3d 20 49 4e 44  9: /* id ::= IND
2e603 45 58 45 44 20 2a 2f 0a 20 20 20 20 20 20 63 61  EXED */.      ca
2e604 73 65 20 34 30 3a 20 2f 2a 20 69 64 73 20 3a 3a  se 40: /* ids ::
2e605 3d 20 49 44 7c 53 54 52 49 4e 47 20 2a 2f 0a 20  = ID|STRING */. 
2e606 20 20 20 20 20 63 61 73 65 20 34 31 3a 20 2f 2a       case 41: /*
2e607 20 6e 6d 20 3a 3a 3d 20 69 64 20 2a 2f 0a 20 20   nm ::= id */.  
2e608 20 20 20 20 63 61 73 65 20 34 32 3a 20 2f 2a 20      case 42: /* 
2e609 6e 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 20 2a 2f  nm ::= STRING */
2e60a 0a 20 20 20 20 20 20 63 61 73 65 20 34 33 3a 20  .      case 43: 
2e60b 2f 2a 20 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  /* nm ::= JOIN_K
2e60c 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  W */.      case 
2e60d 34 36 3a 20 2f 2a 20 74 79 70 65 74 6f 6b 65 6e  46: /* typetoken
2e60e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 2a 2f   ::= typename */
2e60f 0a 20 20 20 20 20 20 63 61 73 65 20 34 39 3a 20  .      case 49: 
2e610 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  /* typename ::= 
2e611 69 64 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ids */.      cas
2e612 65 20 31 32 36 3a 20 2f 2a 20 61 73 20 3a 3a 3d  e 126: /* as ::=
2e613 20 41 53 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20   AS nm */.      
2e614 63 61 73 65 20 31 32 37 3a 20 2f 2a 20 61 73 20  case 127: /* as 
2e615 3a 3a 3d 20 69 64 73 20 2a 2f 0a 20 20 20 20 20  ::= ids */.     
2e616 20 63 61 73 65 20 31 33 37 3a 20 2f 2a 20 64 62   case 137: /* db
2e617 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 20 2a 2f  nm ::= DOT nm */
2e618 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 36 3a  .      case 146:
2e619 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20   /* indexed_opt 
2e61a 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20 6e  ::= INDEXED BY n
2e61b 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  m */.      case 
2e61c 32 35 31 3a 20 2f 2a 20 63 6f 6c 6c 61 74 65 20  251: /* collate 
2e61d 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 20  ::= COLLATE ids 
2e61e 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  */.      case 26
2e61f 30 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20  0: /* nmnum ::= 
2e620 70 6c 75 73 5f 6e 75 6d 20 2a 2f 0a 20 20 20 20  plus_num */.    
2e621 20 20 63 61 73 65 20 32 36 31 3a 20 2f 2a 20 6e    case 261: /* n
2e622 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 20  mnum ::= nm */. 
2e623 20 20 20 20 20 63 61 73 65 20 32 36 32 3a 20 2f       case 262: /
2e624 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e 20 2a  * nmnum ::= ON *
2e625 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 33  /.      case 263
2e626 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 44  : /* nmnum ::= D
2e627 45 4c 45 54 45 20 2a 2f 0a 20 20 20 20 20 20 63  ELETE */.      c
2e628 61 73 65 20 32 36 34 3a 20 2f 2a 20 6e 6d 6e 75  ase 264: /* nmnu
2e629 6d 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 2a 2f  m ::= DEFAULT */
2e62a 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 35 3a  .      case 265:
2e62b 20 2f 2a 20 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d   /* plus_num ::=
2e62c 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65 72   plus_opt number
2e62d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2e62e 36 36 3a 20 2f 2a 20 6d 69 6e 75 73 5f 6e 75 6d  66: /* minus_num
2e62f 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65   ::= MINUS numbe
2e630 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2e631 32 36 37 3a 20 2f 2a 20 6e 75 6d 62 65 72 20 3a  267: /* number :
2e632 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54  := INTEGER|FLOAT
2e633 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2e634 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  .yy0 = yymsp[0].
2e635 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20  minor.yy0;}.    
2e636 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e637 20 63 61 73 65 20 34 35 3a 20 2f 2a 20 74 79 70   case 45: /* typ
2e638 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 20  e ::= typetoken 
2e639 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 6f  */.{sqlite3AddCo
2e63a 6c 75 6d 6e 54 79 70 65 28 70 50 61 72 73 65 2c  lumnType(pParse,
2e63b 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2e63c 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
2e63d 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e63e 20 34 37 3a 20 2f 2a 20 74 79 70 65 74 6f 6b 65   47: /* typetoke
2e63f 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c  n ::= typename L
2e640 50 20 73 69 67 6e 65 64 20 52 50 20 2a 2f 0a 7b  P signed RP */.{
2e641 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2e642 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d  y0.z = yymsp[-3]
2e643 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
2e644 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
2e645 6e 20 3d 20 28 69 6e 74 29 28 26 79 79 6d 73 70  n = (int)(&yymsp
2e646 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b  [0].minor.yy0.z[
2e647 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e648 79 30 2e 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 33  y0.n] - yymsp[-3
2e649 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 0a  ].minor.yy0.z);.
2e64a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e64b 0a 20 20 20 20 20 20 63 61 73 65 20 34 38 3a 20  .      case 48: 
2e64c 2f 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d  /* typetoken ::=
2e64d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67   typename LP sig
2e64e 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67 6e 65 64  ned COMMA signed
2e64f 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
2e650 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79  ominor.yy0.z = y
2e651 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
2e652 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y0.z;.  yygotomi
2e653 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74  nor.yy0.n = (int
2e654 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  )(&yymsp[0].mino
2e655 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d  r.yy0.z[yymsp[0]
2e656 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20 2d 20  .minor.yy0.n] - 
2e657 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
2e658 79 79 30 2e 7a 29 3b 0a 7d 0a 20 20 20 20 20 20  yy0.z);.}.      
2e659 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e65a 61 73 65 20 35 30 3a 20 2f 2a 20 74 79 70 65 6e  ase 50: /* typen
2e65b 61 6d 65 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65  ame ::= typename
2e65c 20 69 64 73 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ids */.{yygotom
2e65d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 79 79 6d 73 70  inor.yy0.z=yymsp
2e65e 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-1].minor.yy0.z
2e65f 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
2e660 30 2e 6e 3d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0.n=yymsp[0].min
2e661 6f 72 2e 79 79 30 2e 6e 2b 28 69 6e 74 29 28 79  or.yy0.n+(int)(y
2e662 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e663 30 2e 7a 2d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0.z-yymsp[-1].mi
2e664 6e 6f 72 2e 79 79 30 2e 7a 29 3b 7d 0a 20 20 20  nor.yy0.z);}.   
2e665 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e666 20 20 63 61 73 65 20 35 37 3a 20 2f 2a 20 63 63    case 57: /* cc
2e667 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
2e668 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61  term */.      ca
2e669 73 65 20 35 39 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 59: /* ccons 
2e66a 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55 53  ::= DEFAULT PLUS
2e66b 20 74 65 72 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65   term */.{sqlite
2e66c 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65  3AddDefaultValue
2e66d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d  (pParse,yymsp[0]
2e66e 2e 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 7d 0a 20  .minor.yy72);}. 
2e66f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e670 20 20 20 20 63 61 73 65 20 35 38 3a 20 2f 2a 20      case 58: /* 
2e671 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
2e672 54 20 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0a  T LP expr RP */.
2e673 7b 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75  {sqlite3AddDefau
2e674 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c 79  ltValue(pParse,y
2e675 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e676 79 37 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  y72);}.        b
2e677 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e678 20 36 30 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a   60: /* ccons ::
2e679 3d 20 44 45 46 41 55 4c 54 20 4d 49 4e 55 53 20  = DEFAULT MINUS 
2e67a 74 65 72 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  term */.{.  Expr
2e67b 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78   *p = sqlite3PEx
2e67c 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 55 4d  pr(pParse, TK_UM
2e67d 49 4e 55 53 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  INUS, yymsp[0].m
2e67e 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20 30 29  inor.yy72, 0, 0)
2e67f 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  ;.  sqlite3ExprS
2e680 70 61 6e 28 70 2c 26 79 79 6d 73 70 5b 2d 31 5d  pan(p,&yymsp[-1]
2e681 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2e682 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d  p[0].minor.yy72-
2e683 3e 73 70 61 6e 29 3b 0a 20 20 73 71 6c 69 74 65  >span);.  sqlite
2e684 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65  3AddDefaultValue
2e685 28 70 50 61 72 73 65 2c 70 29 3b 0a 7d 0a 20 20  (pParse,p);.}.  
2e686 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e687 20 20 20 63 61 73 65 20 36 31 3a 20 2f 2a 20 63     case 61: /* c
2e688 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54  cons ::= DEFAULT
2e689 20 69 64 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20   id */.{.  Expr 
2e68a 2a 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  *p = sqlite3PExp
2e68b 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 53 54 52  r(pParse, TK_STR
2e68c 49 4e 47 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  ING, 0, 0, &yyms
2e68d 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2e68e 0a 20 20 73 71 6c 69 74 65 33 41 64 64 44 65 66  .  sqlite3AddDef
2e68f 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65  aultValue(pParse
2e690 2c 70 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  ,p);.}.        b
2e691 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e692 20 36 33 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a   63: /* ccons ::
2e693 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e  = NOT NULL oncon
2e694 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64  f */.{sqlite3Add
2e695 4e 6f 74 4e 75 6c 6c 28 70 50 61 72 73 65 2c 20  NotNull(pParse, 
2e696 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e697 79 31 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20  y194);}.        
2e698 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e699 65 20 36 34 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 64: /* ccons :
2e69a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20 73  := PRIMARY KEY s
2e69b 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f 6e 66 20  ortorder onconf 
2e69c 61 75 74 6f 69 6e 63 20 2a 2f 0a 7b 73 71 6c 69  autoinc */.{sqli
2e69d 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79  te3AddPrimaryKey
2e69e 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b  (pParse,0,yymsp[
2e69f 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c  -1].minor.yy194,
2e6a0 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e6a1 79 31 39 34 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y194,yymsp[-2].m
2e6a2 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a 20 20  inor.yy194);}.  
2e6a3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e6a4 20 20 20 63 61 73 65 20 36 35 3a 20 2f 2a 20 63     case 65: /* c
2e6a5 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20  cons ::= UNIQUE 
2e6a6 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74  onconf */.{sqlit
2e6a7 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50  e3CreateIndex(pP
2e6a8 61 72 73 65 2c 30 2c 30 2c 30 2c 30 2c 79 79 6d  arse,0,0,0,0,yym
2e6a9 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39  sp[0].minor.yy19
2e6aa 34 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20 20  4,0,0,0,0);}.   
2e6ab 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e6ac 20 20 63 61 73 65 20 36 36 3a 20 2f 2a 20 63 63    case 66: /* cc
2e6ad 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50  ons ::= CHECK LP
2e6ae 20 65 78 70 72 20 52 50 20 2a 2f 0a 7b 73 71 6c   expr RP */.{sql
2e6af 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73  ite3AddCheckCons
2e6b0 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 79 79  traint(pParse,yy
2e6b1 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2e6b2 37 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  72);}.        br
2e6b3 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e6b4 36 37 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  67: /* ccons ::=
2e6b5 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69   REFERENCES nm i
2e6b6 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72  dxlist_opt refar
2e6b7 67 73 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 72  gs */.{sqlite3Cr
2e6b8 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 70  eateForeignKey(p
2e6b9 50 61 72 73 65 2c 30 2c 26 79 79 6d 73 70 5b 2d  Parse,0,&yymsp[-
2e6ba 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d  2].minor.yy0,yym
2e6bb 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2e6bc 34 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  48,yymsp[0].mino
2e6bd 72 2e 79 79 31 39 34 29 3b 7d 0a 20 20 20 20 20  r.yy194);}.     
2e6be 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e6bf 63 61 73 65 20 36 38 3a 20 2f 2a 20 63 63 6f 6e  case 68: /* ccon
2e6c0 73 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62 63  s ::= defer_subc
2e6c1 6c 61 75 73 65 20 2a 2f 0a 7b 73 71 6c 69 74 65  lause */.{sqlite
2e6c2 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79  3DeferForeignKey
2e6c3 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d  (pParse,yymsp[0]
2e6c4 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a  .minor.yy194);}.
2e6c5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e6c6 20 20 20 20 20 63 61 73 65 20 36 39 3a 20 2f 2a       case 69: /*
2e6c7 20 63 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41   ccons ::= COLLA
2e6c8 54 45 20 69 64 73 20 2a 2f 0a 7b 73 71 6c 69 74  TE ids */.{sqlit
2e6c9 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65  e3AddCollateType
2e6ca 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
2e6cb 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
2e6cc 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e6cd 20 20 20 20 20 63 61 73 65 20 37 32 3a 20 2f 2a       case 72: /*
2e6ce 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 2a 2f 0a   refargs ::= */.
2e6cf 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
2e6d0 31 39 34 20 3d 20 4f 45 5f 52 65 73 74 72 69 63  194 = OE_Restric
2e6d1 74 20 2a 20 30 78 30 31 30 31 30 31 3b 20 7d 0a  t * 0x010101; }.
2e6d2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e6d3 20 20 20 20 20 63 61 73 65 20 37 33 3a 20 2f 2a       case 73: /*
2e6d4 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 72 65 66   refargs ::= ref
2e6d5 61 72 67 73 20 72 65 66 61 72 67 20 2a 2f 0a 7b  args refarg */.{
2e6d6 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2e6d7 39 34 20 3d 20 28 79 79 6d 73 70 5b 2d 31 5d 2e  94 = (yymsp[-1].
2e6d8 6d 69 6e 6f 72 2e 79 79 31 39 34 20 26 20 7e 79  minor.yy194 & ~y
2e6d9 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e6da 34 39 37 2e 6d 61 73 6b 29 20 7c 20 79 79 6d 73  497.mask) | yyms
2e6db 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 39 37  p[0].minor.yy497
2e6dc 2e 76 61 6c 75 65 3b 20 7d 0a 20 20 20 20 20 20  .value; }.      
2e6dd 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e6de 61 73 65 20 37 34 3a 20 2f 2a 20 72 65 66 61 72  ase 74: /* refar
2e6df 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d 20 2a  g ::= MATCH nm *
2e6e0 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
2e6e1 79 79 34 39 37 2e 76 61 6c 75 65 20 3d 20 30 3b  yy497.value = 0;
2e6e2 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
2e6e3 2e 79 79 34 39 37 2e 6d 61 73 6b 20 3d 20 30 78  .yy497.mask = 0x
2e6e4 30 30 30 30 30 30 3b 20 7d 0a 20 20 20 20 20 20  000000; }.      
2e6e5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e6e6 61 73 65 20 37 35 3a 20 2f 2a 20 72 65 66 61 72  ase 75: /* refar
2e6e7 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c 45 54 45 20  g ::= ON DELETE 
2e6e8 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f  refact */.{ yygo
2e6e9 74 6f 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 76 61  tominor.yy497.va
2e6ea 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  lue = yymsp[0].m
2e6eb 69 6e 6f 72 2e 79 79 31 39 34 3b 20 20 20 20 20  inor.yy194;     
2e6ec 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 39  yygotominor.yy49
2e6ed 37 2e 6d 61 73 6b 20 3d 20 30 78 30 30 30 30 66  7.mask = 0x0000f
2e6ee 66 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  f; }.        bre
2e6ef 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37  ak;.      case 7
2e6f0 36 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d  6: /* refarg ::=
2e6f1 20 4f 4e 20 55 50 44 41 54 45 20 72 65 66 61 63   ON UPDATE refac
2e6f2 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  t */.{ yygotomin
2e6f3 6f 72 2e 79 79 34 39 37 2e 76 61 6c 75 65 20 3d  or.yy497.value =
2e6f4 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e6f5 79 79 31 39 34 3c 3c 38 3b 20 20 79 79 67 6f 74  yy194<<8;  yygot
2e6f6 6f 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 6d 61 73  ominor.yy497.mas
2e6f7 6b 20 3d 20 30 78 30 30 66 66 30 30 3b 20 7d 0a  k = 0x00ff00; }.
2e6f8 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e6f9 20 20 20 20 20 63 61 73 65 20 37 37 3a 20 2f 2a       case 77: /*
2e6fa 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 49   refarg ::= ON I
2e6fb 4e 53 45 52 54 20 72 65 66 61 63 74 20 2a 2f 0a  NSERT refact */.
2e6fc 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
2e6fd 34 39 37 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73  497.value = yyms
2e6fe 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34  p[0].minor.yy194
2e6ff 3c 3c 31 36 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  <<16; yygotomino
2e700 72 2e 79 79 34 39 37 2e 6d 61 73 6b 20 3d 20 30  r.yy497.mask = 0
2e701 78 66 66 30 30 30 30 3b 20 7d 0a 20 20 20 20 20  xff0000; }.     
2e702 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e703 63 61 73 65 20 37 38 3a 20 2f 2a 20 72 65 66 61  case 78: /* refa
2e704 63 74 20 3a 3a 3d 20 53 45 54 20 4e 55 4c 4c 20  ct ::= SET NULL 
2e705 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
2e706 2e 79 79 31 39 34 20 3d 20 4f 45 5f 53 65 74 4e  .yy194 = OE_SetN
2e707 75 6c 6c 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  ull; }.        b
2e708 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e709 20 37 39 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a   79: /* refact :
2e70a 3a 3d 20 53 45 54 20 44 45 46 41 55 4c 54 20 2a  := SET DEFAULT *
2e70b 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
2e70c 79 79 31 39 34 20 3d 20 4f 45 5f 53 65 74 44 66  yy194 = OE_SetDf
2e70d 6c 74 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  lt; }.        br
2e70e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e70f 38 30 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a  80: /* refact ::
2e710 3d 20 43 41 53 43 41 44 45 20 2a 2f 0a 7b 20 79  = CASCADE */.{ y
2e711 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34  ygotominor.yy194
2e712 20 3d 20 4f 45 5f 43 61 73 63 61 64 65 3b 20 7d   = OE_Cascade; }
2e713 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e714 20 20 20 20 20 20 63 61 73 65 20 38 31 3a 20 2f        case 81: /
2e715 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 52 45 53  * refact ::= RES
2e716 54 52 49 43 54 20 2a 2f 0a 7b 20 79 79 67 6f 74  TRICT */.{ yygot
2e717 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 4f  ominor.yy194 = O
2e718 45 5f 52 65 73 74 72 69 63 74 3b 20 7d 0a 20 20  E_Restrict; }.  
2e719 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e71a 20 20 20 63 61 73 65 20 38 32 3a 20 2f 2a 20 64     case 82: /* d
2e71b 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a  efer_subclause :
2e71c 3a 3d 20 4e 4f 54 20 44 45 46 45 52 52 41 42 4c  := NOT DEFERRABL
2e71d 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f  E init_deferred_
2e71e 70 72 65 64 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  pred_opt */.    
2e71f 20 20 63 61 73 65 20 38 33 3a 20 2f 2a 20 64 65    case 83: /* de
2e720 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a  fer_subclause ::
2e721 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  = DEFERRABLE ini
2e722 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
2e723 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  opt */.      cas
2e724 65 20 39 38 3a 20 2f 2a 20 64 65 66 65 72 5f 73  e 98: /* defer_s
2e725 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d  ubclause_opt ::=
2e726 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
2e727 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2e728 30 30 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a  00: /* onconf ::
2e729 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65  = ON CONFLICT re
2e72a 73 6f 6c 76 65 74 79 70 65 20 2a 2f 0a 20 20 20  solvetype */.   
2e72b 20 20 20 63 61 73 65 20 31 30 32 3a 20 2f 2a 20     case 102: /* 
2e72c 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72 65  orconf ::= OR re
2e72d 73 6f 6c 76 65 74 79 70 65 20 2a 2f 0a 20 20 20  solvetype */.   
2e72e 20 20 20 63 61 73 65 20 31 30 33 3a 20 2f 2a 20     case 103: /* 
2e72f 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20  resolvetype ::= 
2e730 72 61 69 73 65 74 79 70 65 20 2a 2f 0a 20 20 20  raisetype */.   
2e731 20 20 20 63 61 73 65 20 31 37 35 3a 20 2f 2a 20     case 175: /* 
2e732 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 49  insert_cmd ::= I
2e733 4e 53 45 52 54 20 6f 72 63 6f 6e 66 20 2a 2f 0a  NSERT orconf */.
2e734 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
2e735 39 34 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  94 = yymsp[0].mi
2e736 6e 6f 72 2e 79 79 31 39 34 3b 7d 0a 20 20 20 20  nor.yy194;}.    
2e737 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e738 20 63 61 73 65 20 38 37 3a 20 2f 2a 20 63 6f 6e   case 87: /* con
2e739 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  slist_opt ::= */
2e73a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2e73b 30 2e 6e 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d  0.n = 0; yygotom
2e73c 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 30 3b 7d  inor.yy0.z = 0;}
2e73d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e73e 20 20 20 20 20 20 63 61 73 65 20 38 38 3a 20 2f        case 88: /
2e73f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a  * conslist_opt :
2e740 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69 73  := COMMA conslis
2e741 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  t */.{yygotomino
2e742 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 2d 31  r.yy0 = yymsp[-1
2e743 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20  ].minor.yy0;}.  
2e744 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e745 20 20 20 63 61 73 65 20 39 33 3a 20 2f 2a 20 74     case 93: /* t
2e746 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59  cons ::= PRIMARY
2e747 20 4b 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20   KEY LP idxlist 
2e748 61 75 74 6f 69 6e 63 20 52 50 20 6f 6e 63 6f 6e  autoinc RP oncon
2e749 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64  f */.{sqlite3Add
2e74a 50 72 69 6d 61 72 79 4b 65 79 28 70 50 61 72 73  PrimaryKey(pPars
2e74b 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  e,yymsp[-3].mino
2e74c 72 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b 30 5d  r.yy148,yymsp[0]
2e74d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 79 79 6d  .minor.yy194,yym
2e74e 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
2e74f 39 34 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  94,0);}.        
2e750 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e751 65 20 39 34 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a  e 94: /* tcons :
2e752 3a 3d 20 55 4e 49 51 55 45 20 4c 50 20 69 64 78  := UNIQUE LP idx
2e753 6c 69 73 74 20 52 50 20 6f 6e 63 6f 6e 66 20 2a  list RP onconf *
2e754 2f 0a 7b 73 71 6c 69 74 65 33 43 72 65 61 74 65  /.{sqlite3Create
2e755 49 6e 64 65 78 28 70 50 61 72 73 65 2c 30 2c 30  Index(pParse,0,0
2e756 2c 30 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  ,0,yymsp[-2].min
2e757 6f 72 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b 30  or.yy148,yymsp[0
2e758 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 30 2c  ].minor.yy194,0,
2e759 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20  0,0,0);}.       
2e75a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e75b 73 65 20 39 35 3a 20 2f 2a 20 74 63 6f 6e 73 20  se 95: /* tcons 
2e75c 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70  ::= CHECK LP exp
2e75d 72 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b  r RP onconf */.{
2e75e 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43  sqlite3AddCheckC
2e75f 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65  onstraint(pParse
2e760 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
2e761 2e 79 79 37 32 29 3b 7d 0a 20 20 20 20 20 20 20  .yy72);}.       
2e762 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e763 73 65 20 39 36 3a 20 2f 2a 20 74 63 6f 6e 73 20  se 96: /* tcons 
2e764 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20  ::= FOREIGN KEY 
2e765 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 52 45  LP idxlist RP RE
2e766 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c  FERENCES nm idxl
2e767 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20  ist_opt refargs 
2e768 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
2e769 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c  opt */.{.    sql
2e76a 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67  ite3CreateForeig
2e76b 6e 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d  nKey(pParse, yym
2e76c 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-6].minor.yy1
2e76d 34 38 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  48, &yymsp[-3].m
2e76e 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b  inor.yy0, yymsp[
2e76f 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c  -2].minor.yy148,
2e770 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2e771 2e 79 79 31 39 34 29 3b 0a 20 20 20 20 73 71 6c  .yy194);.    sql
2e772 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
2e773 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73  Key(pParse, yyms
2e774 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34  p[0].minor.yy194
2e775 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2e776 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
2e777 39 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d  9: /* onconf ::=
2e778 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2e779 30 31 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a  01: /* orconf ::
2e77a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
2e77b 72 2e 79 79 31 39 34 20 3d 20 4f 45 5f 44 65 66  r.yy194 = OE_Def
2e77c 61 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20 20 62  ault;}.        b
2e77d 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e77e 20 31 30 34 3a 20 2f 2a 20 72 65 73 6f 6c 76 65   104: /* resolve
2e77f 74 79 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45 20  type ::= IGNORE 
2e780 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2e781 79 79 31 39 34 20 3d 20 4f 45 5f 49 67 6e 6f 72  yy194 = OE_Ignor
2e782 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  e;}.        brea
2e783 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
2e784 35 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70  5: /* resolvetyp
2e785 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f  e ::= REPLACE */
2e786 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 36 3a  .      case 176:
2e787 20 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a   /* insert_cmd :
2e788 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79  := REPLACE */.{y
2e789 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34  ygotominor.yy194
2e78a 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a   = OE_Replace;}.
2e78b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e78c 20 20 20 20 20 63 61 73 65 20 31 30 36 3a 20 2f       case 106: /
2e78d 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54  * cmd ::= DROP T
2e78e 41 42 4c 45 20 69 66 65 78 69 73 74 73 20 66 75  ABLE ifexists fu
2e78f 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71  llname */.{.  sq
2e790 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 70  lite3DropTable(p
2e791 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
2e792 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 20 30 2c 20  minor.yy185, 0, 
2e793 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2e794 79 79 31 39 34 29 3b 0a 7d 0a 20 20 20 20 20 20  yy194);.}.      
2e795 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e796 61 73 65 20 31 30 39 3a 20 2f 2a 20 63 6d 64 20  ase 109: /* cmd 
2e797 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d  ::= createkw tem
2e798 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73  p VIEW ifnotexis
2e799 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65  ts nm dbnm AS se
2e79a 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  lect */.{.  sqli
2e79b 74 65 33 43 72 65 61 74 65 56 69 65 77 28 70 50  te3CreateView(pP
2e79c 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d  arse, &yymsp[-7]
2e79d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
2e79e 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
2e79f 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
2e7a0 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 30 5d  or.yy0, yymsp[0]
2e7a1 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 2c 20 79 79  .minor.yy243, yy
2e7a2 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
2e7a3 31 39 34 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  194, yymsp[-4].m
2e7a4 69 6e 6f 72 2e 79 79 31 39 34 29 3b 0a 7d 0a 20  inor.yy194);.}. 
2e7a5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e7a6 20 20 20 20 63 61 73 65 20 31 31 30 3a 20 2f 2a      case 110: /*
2e7a7 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49   cmd ::= DROP VI
2e7a8 45 57 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c  EW ifexists full
2e7a9 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  name */.{.  sqli
2e7aa 74 65 33 44 72 6f 70 54 61 62 6c 65 28 70 50 61  te3DropTable(pPa
2e7ab 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
2e7ac 6e 6f 72 2e 79 79 31 38 35 2c 20 31 2c 20 79 79  nor.yy185, 1, yy
2e7ad 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2e7ae 31 39 34 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  194);.}.        
2e7af 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e7b0 65 20 31 31 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 111: /* cmd ::
2e7b1 3d 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20  = select */.{.  
2e7b2 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 20  SelectDest dest 
2e7b3 3d 20 7b 53 52 54 5f 4f 75 74 70 75 74 2c 20 30  = {SRT_Output, 0
2e7b4 2c 20 30 2c 20 30 2c 20 30 7d 3b 0a 20 20 73 71  , 0, 0, 0};.  sq
2e7b5 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
2e7b6 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se, yymsp[0].min
2e7b7 6f 72 2e 79 79 32 34 33 2c 20 26 64 65 73 74 29  or.yy243, &dest)
2e7b8 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2e7b9 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
2e7ba 64 62 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  db, yymsp[0].min
2e7bb 6f 72 2e 79 79 32 34 33 29 3b 0a 7d 0a 20 20 20  or.yy243);.}.   
2e7bc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e7bd 20 20 63 61 73 65 20 31 31 32 3a 20 2f 2a 20 73    case 112: /* s
2e7be 65 6c 65 63 74 20 3a 3a 3d 20 6f 6e 65 73 65 6c  elect ::= onesel
2e7bf 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ect */.{yygotomi
2e7c0 6e 6f 72 2e 79 79 32 34 33 20 3d 20 79 79 6d 73  nor.yy243 = yyms
2e7c1 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33  p[0].minor.yy243
2e7c2 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2e7c3 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 33  ;.      case 113
2e7c4 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a 3d 20  : /* select ::= 
2e7c5 73 65 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c 65  select multisele
2e7c6 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74 20  ct_op oneselect 
2e7c7 2a 2f 0a 7b 0a 20 20 69 66 28 20 79 79 6d 73 70  */.{.  if( yymsp
2e7c8 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 20  [0].minor.yy243 
2e7c9 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b 30 5d 2e  ){.    yymsp[0].
2e7ca 6d 69 6e 6f 72 2e 79 79 32 34 33 2d 3e 6f 70 20  minor.yy243->op 
2e7cb 3d 20 28 75 38 29 79 79 6d 73 70 5b 2d 31 5d 2e  = (u8)yymsp[-1].
2e7cc 6d 69 6e 6f 72 2e 79 79 31 39 34 3b 0a 20 20 20  minor.yy194;.   
2e7cd 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e7ce 79 79 32 34 33 2d 3e 70 50 72 69 6f 72 20 3d 20  yy243->pPrior = 
2e7cf 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e7d0 79 79 32 34 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a  yy243;.  }else{.
2e7d1 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2e7d2 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
2e7d3 64 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  db, yymsp[-2].mi
2e7d4 6e 6f 72 2e 79 79 32 34 33 29 3b 0a 20 20 7d 0a  nor.yy243);.  }.
2e7d5 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2e7d6 32 34 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  243 = yymsp[0].m
2e7d7 69 6e 6f 72 2e 79 79 32 34 33 3b 0a 7d 0a 20 20  inor.yy243;.}.  
2e7d8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e7d9 20 20 20 63 61 73 65 20 31 31 35 3a 20 2f 2a 20     case 115: /* 
2e7da 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
2e7db 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c 20 2a 2f 0a  := UNION ALL */.
2e7dc 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
2e7dd 39 34 20 3d 20 54 4b 5f 41 4c 4c 3b 7d 0a 20 20  94 = TK_ALL;}.  
2e7de 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e7df 20 20 20 63 61 73 65 20 31 31 37 3a 20 2f 2a 20     case 117: /* 
2e7e0 6f 6e 65 73 65 6c 65 63 74 20 3a 3a 3d 20 53 45  oneselect ::= SE
2e7e1 4c 45 43 54 20 64 69 73 74 69 6e 63 74 20 73 65  LECT distinct se
2e7e2 6c 63 6f 6c 6c 69 73 74 20 66 72 6f 6d 20 77 68  lcollist from wh
2e7e3 65 72 65 5f 6f 70 74 20 67 72 6f 75 70 62 79 5f  ere_opt groupby_
2e7e4 6f 70 74 20 68 61 76 69 6e 67 5f 6f 70 74 20 6f  opt having_opt o
2e7e5 72 64 65 72 62 79 5f 6f 70 74 20 6c 69 6d 69 74  rderby_opt limit
2e7e6 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  _opt */.{.  yygo
2e7e7 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 33 20 3d 20  tominor.yy243 = 
2e7e8 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
2e7e9 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36  (pParse,yymsp[-6
2e7ea 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 79 79  ].minor.yy148,yy
2e7eb 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
2e7ec 31 38 35 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  185,yymsp[-4].mi
2e7ed 6e 6f 72 2e 79 79 37 32 2c 79 79 6d 73 70 5b 2d  nor.yy72,yymsp[-
2e7ee 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 79  3].minor.yy148,y
2e7ef 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2e7f0 79 37 32 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y72,yymsp[-1].mi
2e7f1 6e 6f 72 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b  nor.yy148,yymsp[
2e7f2 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c  -7].minor.yy194,
2e7f3 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e7f4 79 33 35 34 2e 70 4c 69 6d 69 74 2c 79 79 6d 73  y354.pLimit,yyms
2e7f5 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 34  p[0].minor.yy354
2e7f6 2e 70 4f 66 66 73 65 74 29 3b 0a 7d 0a 20 20 20  .pOffset);.}.   
2e7f7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e7f8 20 20 63 61 73 65 20 31 32 31 3a 20 2f 2a 20 73    case 121: /* s
2e7f9 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69  clp ::= selcolli
2e7fa 73 74 20 43 4f 4d 4d 41 20 2a 2f 0a 20 20 20 20  st COMMA */.    
2e7fb 20 20 63 61 73 65 20 32 34 37 3a 20 2f 2a 20 69    case 247: /* i
2e7fc 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c  dxlist_opt ::= L
2e7fd 50 20 69 64 78 6c 69 73 74 20 52 50 20 2a 2f 0a  P idxlist RP */.
2e7fe 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
2e7ff 34 38 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  48 = yymsp[-1].m
2e800 69 6e 6f 72 2e 79 79 31 34 38 3b 7d 0a 20 20 20  inor.yy148;}.   
2e801 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e802 20 20 63 61 73 65 20 31 32 32 3a 20 2f 2a 20 73    case 122: /* s
2e803 63 6c 70 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20  clp ::= */.     
2e804 20 63 61 73 65 20 31 35 30 3a 20 2f 2a 20 6f 72   case 150: /* or
2e805 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  derby_opt ::= */
2e806 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 38 3a  .      case 158:
2e807 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20   /* groupby_opt 
2e808 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2e809 65 20 32 34 30 3a 20 2f 2a 20 65 78 70 72 6c 69  e 240: /* exprli
2e80a 73 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  st ::= */.      
2e80b 63 61 73 65 20 32 34 36 3a 20 2f 2a 20 69 64 78  case 246: /* idx
2e80c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  list_opt ::= */.
2e80d 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
2e80e 34 38 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  48 = 0;}.       
2e80f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e810 73 65 20 31 32 33 3a 20 2f 2a 20 73 65 6c 63 6f  se 123: /* selco
2e811 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 65  llist ::= sclp e
2e812 78 70 72 20 61 73 20 2a 2f 0a 7b 0a 20 20 20 79  xpr as */.{.   y
2e813 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38  ygotominor.yy148
2e814 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2e815 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2e816 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e817 79 79 31 34 38 2c 79 79 6d 73 70 5b 2d 31 5d 2e  yy148,yymsp[-1].
2e818 6d 69 6e 6f 72 2e 79 79 37 32 2c 79 79 6d 73 70  minor.yy72,yymsp
2e819 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3f  [0].minor.yy0.n?
2e81a 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2e81b 79 79 30 3a 30 29 3b 0a 7d 0a 20 20 20 20 20 20  yy0:0);.}.      
2e81c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e81d 61 73 65 20 31 32 34 3a 20 2f 2a 20 73 65 6c 63  ase 124: /* selc
2e81e 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20  ollist ::= sclp 
2e81f 53 54 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  STAR */.{.  Expr
2e820 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78   *p = sqlite3PEx
2e821 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 4c  pr(pParse, TK_AL
2e822 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 79  L, 0, 0, 0);.  y
2e823 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38  ygotominor.yy148
2e824 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2e825 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2e826 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2e827 2e 79 79 31 34 38 2c 20 70 2c 20 30 29 3b 0a 7d  .yy148, p, 0);.}
2e828 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e829 20 20 20 20 20 20 63 61 73 65 20 31 32 35 3a 20        case 125: 
2e82a 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a  /* selcollist ::
2e82b 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54  = sclp nm DOT ST
2e82c 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a  AR */.{.  Expr *
2e82d 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33  pRight = sqlite3
2e82e 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2e82f 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 26 79 79 6d  _ALL, 0, 0, &yym
2e830 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
2e831 3b 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20  ;.  Expr *pLeft 
2e832 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2e833 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
2e834 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d   0, &yymsp[-2].m
2e835 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70  inor.yy0);.  Exp
2e836 72 20 2a 70 44 6f 74 20 3d 20 73 71 6c 69 74 65  r *pDot = sqlite
2e837 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2e838 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52  K_DOT, pLeft, pR
2e839 69 67 68 74 2c 20 30 29 3b 0a 20 20 79 79 67 6f  ight, 0);.  yygo
2e83a 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20  tominor.yy148 = 
2e83b 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2e83c 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d  ppend(pParse,yym
2e83d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
2e83e 34 38 2c 20 70 44 6f 74 2c 20 30 29 3b 0a 7d 0a  48, pDot, 0);.}.
2e83f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e840 20 20 20 20 20 63 61 73 65 20 31 32 38 3a 20 2f       case 128: /
2e841 2a 20 61 73 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  * as ::= */.{yyg
2e842 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d  otominor.yy0.n =
2e843 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
2e844 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e845 32 39 3a 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20  29: /* from ::= 
2e846 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2e847 79 79 31 38 35 20 3d 20 73 71 6c 69 74 65 33 44  yy185 = sqlite3D
2e848 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72  bMallocZero(pPar
2e849 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a  se->db, sizeof(*
2e84a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38  yygotominor.yy18
2e84b 35 29 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  5));}.        br
2e84c 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e84d 31 33 30 3a 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d  130: /* from ::=
2e84e 20 46 52 4f 4d 20 73 65 6c 74 61 62 6c 69 73 74   FROM seltablist
2e84f 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
2e850 6e 6f 72 2e 79 79 31 38 35 20 3d 20 79 79 6d 73  nor.yy185 = yyms
2e851 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35  p[0].minor.yy185
2e852 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  ;.  sqlite3SrcLi
2e853 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
2e854 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38  yygotominor.yy18
2e855 35 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  5);.}.        br
2e856 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e857 31 33 31 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66  131: /* stl_pref
2e858 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73  ix ::= seltablis
2e859 74 20 6a 6f 69 6e 6f 70 20 2a 2f 0a 7b 0a 20 20  t joinop */.{.  
2e85a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2e85b 38 35 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  85 = yymsp[-1].m
2e85c 69 6e 6f 72 2e 79 79 31 38 35 3b 0a 20 20 20 69  inor.yy185;.   i
2e85d 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2e85e 79 31 38 35 20 26 26 20 79 79 67 6f 74 6f 6d 69  y185 && yygotomi
2e85f 6e 6f 72 2e 79 79 31 38 35 2d 3e 6e 53 72 63 3e  nor.yy185->nSrc>
2e860 30 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  0 ) yygotominor.
2e861 79 79 31 38 35 2d 3e 61 5b 79 79 67 6f 74 6f 6d  yy185->a[yygotom
2e862 69 6e 6f 72 2e 79 79 31 38 35 2d 3e 6e 53 72 63  inor.yy185->nSrc
2e863 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 28  -1].jointype = (
2e864 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  u8)yymsp[0].mino
2e865 72 2e 79 79 31 39 34 3b 0a 7d 0a 20 20 20 20 20  r.yy194;.}.     
2e866 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e867 63 61 73 65 20 31 33 32 3a 20 2f 2a 20 73 74 6c  case 132: /* stl
2e868 5f 70 72 65 66 69 78 20 3a 3a 3d 20 2a 2f 0a 7b  _prefix ::= */.{
2e869 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38  yygotominor.yy18
2e86a 35 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  5 = 0;}.        
2e86b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e86c 65 20 31 33 33 3a 20 2f 2a 20 73 65 6c 74 61 62  e 133: /* seltab
2e86d 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65  list ::= stl_pre
2e86e 66 69 78 20 6e 6d 20 64 62 6e 6d 20 61 73 20 69  fix nm dbnm as i
2e86f 6e 64 65 78 65 64 5f 6f 70 74 20 6f 6e 5f 6f 70  ndexed_opt on_op
2e870 74 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b  t using_opt */.{
2e871 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2e872 79 31 38 35 20 3d 20 73 71 6c 69 74 65 33 53 72  y185 = sqlite3Sr
2e873 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54  cListAppendFromT
2e874 65 72 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70  erm(pParse,yymsp
2e875 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35  [-6].minor.yy185
2e876 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  ,&yymsp[-5].mino
2e877 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 34 5d  r.yy0,&yymsp[-4]
2e878 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2e879 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
2e87a 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  0,yymsp[-1].mino
2e87b 72 2e 79 79 37 32 2c 79 79 6d 73 70 5b 30 5d 2e  r.yy72,yymsp[0].
2e87c 6d 69 6e 6f 72 2e 79 79 32 35 34 29 3b 0a 20 20  minor.yy254);.  
2e87d 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e  sqlite3SrcListIn
2e87e 64 65 78 65 64 42 79 28 70 50 61 72 73 65 2c 20  dexedBy(pParse, 
2e87f 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38  yygotominor.yy18
2e880 35 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  5, &yymsp[-2].mi
2e881 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
2e882 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e883 20 63 61 73 65 20 31 33 34 3a 20 2f 2a 20 73 65   case 134: /* se
2e884 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c  ltablist ::= stl
2e885 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c 65 63  _prefix LP selec
2e886 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75  t RP as on_opt u
2e887 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20  sing_opt */.{.  
2e888 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2e889 31 38 35 20 3d 20 73 71 6c 69 74 65 33 53 72 63  185 = sqlite3Src
2e88a 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65  ListAppendFromTe
2e88b 72 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  rm(pParse,yymsp[
2e88c 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c  -6].minor.yy185,
2e88d 30 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  0,0,&yymsp[-2].m
2e88e 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d  inor.yy0,yymsp[-
2e88f 34 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 2c 79  4].minor.yy243,y
2e890 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e891 79 37 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y72,yymsp[0].min
2e892 6f 72 2e 79 79 32 35 34 29 3b 0a 20 20 7d 0a 20  or.yy254);.  }. 
2e893 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e894 20 20 20 20 63 61 73 65 20 31 33 35 3a 20 2f 2a      case 135: /*
2e895 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20   seltablist ::= 
2e896 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65  stl_prefix LP se
2e897 6c 74 61 62 6c 69 73 74 20 52 50 20 61 73 20 6f  ltablist RP as o
2e898 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20  n_opt using_opt 
2e899 2a 2f 0a 7b 0a 20 20 20 20 69 66 28 20 79 79 6d  */.{.    if( yym
2e89a 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-6].minor.yy1
2e89b 38 35 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b 2d  85==0 && yymsp[-
2e89c 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 3d  2].minor.yy0.n==
2e89d 30 20 26 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  0 && yymsp[-1].m
2e89e 69 6e 6f 72 2e 79 79 37 32 3d 3d 30 20 26 26 20  inor.yy72==0 && 
2e89f 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e8a0 79 32 35 34 3d 3d 30 20 29 7b 0a 20 20 20 20 20  y254==0 ){.     
2e8a1 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2e8a2 38 35 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  85 = yymsp[-4].m
2e8a3 69 6e 6f 72 2e 79 79 31 38 35 3b 0a 20 20 20 20  inor.yy185;.    
2e8a4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 53 65 6c  }else{.      Sel
2e8a5 65 63 74 20 2a 70 53 75 62 71 75 65 72 79 3b 0a  ect *pSubquery;.
2e8a6 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63        sqlite3Src
2e8a7 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70  ListShiftJoinTyp
2e8a8 65 28 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  e(yymsp[-4].mino
2e8a9 72 2e 79 79 31 38 35 29 3b 0a 20 20 20 20 20 20  r.yy185);.      
2e8aa 70 53 75 62 71 75 65 72 79 20 3d 20 73 71 6c 69  pSubquery = sqli
2e8ab 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
2e8ac 72 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e  rse,0,yymsp[-4].
2e8ad 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 30 2c 30 2c  minor.yy185,0,0,
2e8ae 30 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20  0,0,0,0,0);.    
2e8af 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2e8b0 31 38 35 20 3d 20 73 71 6c 69 74 65 33 53 72 63  185 = sqlite3Src
2e8b1 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65  ListAppendFromTe
2e8b2 72 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  rm(pParse,yymsp[
2e8b3 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c  -6].minor.yy185,
2e8b4 30 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  0,0,&yymsp[-2].m
2e8b5 69 6e 6f 72 2e 79 79 30 2c 70 53 75 62 71 75 65  inor.yy0,pSubque
2e8b6 72 79 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  ry,yymsp[-1].min
2e8b7 6f 72 2e 79 79 37 32 2c 79 79 6d 73 70 5b 30 5d  or.yy72,yymsp[0]
2e8b8 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 29 3b 0a 20  .minor.yy254);. 
2e8b9 20 20 20 7d 0a 20 20 7d 0a 20 20 20 20 20 20 20     }.  }.       
2e8ba 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e8bb 73 65 20 31 33 36 3a 20 2f 2a 20 64 62 6e 6d 20  se 136: /* dbnm 
2e8bc 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2e8bd 65 20 31 34 35 3a 20 2f 2a 20 69 6e 64 65 78 65  e 145: /* indexe
2e8be 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79  d_opt ::= */.{yy
2e8bf 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d  gotominor.yy0.z=
2e8c0 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  0; yygotominor.y
2e8c1 79 30 2e 6e 3d 30 3b 7d 0a 20 20 20 20 20 20 20  y0.n=0;}.       
2e8c2 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e8c3 73 65 20 31 33 38 3a 20 2f 2a 20 66 75 6c 6c 6e  se 138: /* fulln
2e8c4 61 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62 6e 6d 20  ame ::= nm dbnm 
2e8c5 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2e8c6 79 79 31 38 35 20 3d 20 73 71 6c 69 74 65 33 53  yy185 = sqlite3S
2e8c7 72 63 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  rcListAppend(pPa
2e8c8 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d 73 70  rse->db,0,&yymsp
2e8c9 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-1].minor.yy0,&
2e8ca 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e8cb 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
2e8cc 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e8cd 31 33 39 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a  139: /* joinop :
2e8ce 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 20 2a 2f  := COMMA|JOIN */
2e8cf 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
2e8d0 79 31 39 34 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b  y194 = JT_INNER;
2e8d1 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2e8d2 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 30  ;.      case 140
2e8d3 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20  : /* joinop ::= 
2e8d4 4a 4f 49 4e 5f 4b 57 20 4a 4f 49 4e 20 2a 2f 0a  JOIN_KW JOIN */.
2e8d5 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
2e8d6 31 39 34 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69  194 = sqlite3Joi
2e8d7 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79  nType(pParse,&yy
2e8d8 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2e8d9 30 2c 30 2c 30 29 3b 20 7d 0a 20 20 20 20 20 20  0,0,0); }.      
2e8da 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e8db 61 73 65 20 31 34 31 3a 20 2f 2a 20 6a 6f 69 6e  ase 141: /* join
2e8dc 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e  op ::= JOIN_KW n
2e8dd 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f  m JOIN */.{ yygo
2e8de 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20  tominor.yy194 = 
2e8df 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28  sqlite3JoinType(
2e8e0 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 32  pParse,&yymsp[-2
2e8e1 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2e8e2 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2e8e3 2c 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  ,0); }.        b
2e8e4 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e8e5 20 31 34 32 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20   142: /* joinop 
2e8e6 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e  ::= JOIN_KW nm n
2e8e7 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f  m JOIN */.{ yygo
2e8e8 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20  tominor.yy194 = 
2e8e9 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28  sqlite3JoinType(
2e8ea 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33  pParse,&yymsp[-3
2e8eb 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2e8ec 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
2e8ed 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
2e8ee 72 2e 79 79 30 29 3b 20 7d 0a 20 20 20 20 20 20  r.yy0); }.      
2e8ef 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e8f0 61 73 65 20 31 34 33 3a 20 2f 2a 20 6f 6e 5f 6f  ase 143: /* on_o
2e8f1 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70 72 20 2a  pt ::= ON expr *
2e8f2 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 34  /.      case 154
2e8f3 3a 20 2f 2a 20 73 6f 72 74 69 74 65 6d 20 3a 3a  : /* sortitem ::
2e8f4 3d 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  = expr */.      
2e8f5 63 61 73 65 20 31 36 31 3a 20 2f 2a 20 68 61 76  case 161: /* hav
2e8f6 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56 49  ing_opt ::= HAVI
2e8f7 4e 47 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  NG expr */.     
2e8f8 20 63 61 73 65 20 31 36 38 3a 20 2f 2a 20 77 68   case 168: /* wh
2e8f9 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57 48 45 52  ere_opt ::= WHER
2e8fa 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  E expr */.      
2e8fb 63 61 73 65 20 31 38 33 3a 20 2f 2a 20 65 78 70  case 183: /* exp
2e8fc 72 20 3a 3a 3d 20 74 65 72 6d 20 2a 2f 0a 20 20  r ::= term */.  
2e8fd 20 20 20 20 63 61 73 65 20 32 31 31 3a 20 2f 2a      case 211: /*
2e8fe 20 65 73 63 61 70 65 20 3a 3a 3d 20 45 53 43 41   escape ::= ESCA
2e8ff 50 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  PE expr */.     
2e900 20 63 61 73 65 20 32 33 35 3a 20 2f 2a 20 63 61   case 235: /* ca
2e901 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 45 4c 53 45  se_else ::= ELSE
2e902 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
2e903 61 73 65 20 32 33 37 3a 20 2f 2a 20 63 61 73 65  ase 237: /* case
2e904 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65 78 70  _operand ::= exp
2e905 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  r */.{yygotomino
2e906 72 2e 79 79 37 32 20 3d 20 79 79 6d 73 70 5b 30  r.yy72 = yymsp[0
2e907 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b 7d 0a 20  ].minor.yy72;}. 
2e908 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e909 20 20 20 20 63 61 73 65 20 31 34 34 3a 20 2f 2a      case 144: /*
2e90a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20   on_opt ::= */. 
2e90b 20 20 20 20 20 63 61 73 65 20 31 36 30 3a 20 2f       case 160: /
2e90c 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d  * having_opt ::=
2e90d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2e90e 36 37 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74  67: /* where_opt
2e90f 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
2e910 73 65 20 32 31 32 3a 20 2f 2a 20 65 73 63 61 70  se 212: /* escap
2e911 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  e ::= */.      c
2e912 61 73 65 20 32 33 36 3a 20 2f 2a 20 63 61 73 65  ase 236: /* case
2e913 5f 65 6c 73 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20  _else ::= */.   
2e914 20 20 20 63 61 73 65 20 32 33 38 3a 20 2f 2a 20     case 238: /* 
2e915 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d  case_operand ::=
2e916 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2e917 2e 79 79 37 32 20 3d 20 30 3b 7d 0a 20 20 20 20  .yy72 = 0;}.    
2e918 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e919 20 63 61 73 65 20 31 34 37 3a 20 2f 2a 20 69 6e   case 147: /* in
2e91a 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e 4f  dexed_opt ::= NO
2e91b 54 20 49 4e 44 45 58 45 44 20 2a 2f 0a 7b 79 79  T INDEXED */.{yy
2e91c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d  gotominor.yy0.z=
2e91d 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  0; yygotominor.y
2e91e 79 30 2e 6e 3d 31 3b 7d 0a 20 20 20 20 20 20 20  y0.n=1;}.       
2e91f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e920 73 65 20 31 34 38 3a 20 2f 2a 20 75 73 69 6e 67  se 148: /* using
2e921 5f 6f 70 74 20 3a 3a 3d 20 55 53 49 4e 47 20 4c  _opt ::= USING L
2e922 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20  P inscollist RP 
2e923 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38  */.      case 18
2e924 30 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74  0: /* inscollist
2e925 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63  _opt ::= LP insc
2e926 6f 6c 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 79 79  ollist RP */.{yy
2e927 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 34 20  gotominor.yy254 
2e928 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
2e929 72 2e 79 79 32 35 34 3b 7d 0a 20 20 20 20 20 20  r.yy254;}.      
2e92a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e92b 61 73 65 20 31 34 39 3a 20 2f 2a 20 75 73 69 6e  ase 149: /* usin
2e92c 67 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20  g_opt ::= */.   
2e92d 20 20 20 63 61 73 65 20 31 37 39 3a 20 2f 2a 20     case 179: /* 
2e92e 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a  inscollist_opt :
2e92f 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  := */.{yygotomin
2e930 6f 72 2e 79 79 32 35 34 20 3d 20 30 3b 7d 0a 20  or.yy254 = 0;}. 
2e931 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e932 20 20 20 20 63 61 73 65 20 31 35 31 3a 20 2f 2a      case 151: /*
2e933 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d   orderby_opt ::=
2e934 20 4f 52 44 45 52 20 42 59 20 73 6f 72 74 6c 69   ORDER BY sortli
2e935 73 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  st */.      case
2e936 20 31 35 39 3a 20 2f 2a 20 67 72 6f 75 70 62 79   159: /* groupby
2e937 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f 55 50 20 42  _opt ::= GROUP B
2e938 59 20 6e 65 78 70 72 6c 69 73 74 20 2a 2f 0a 20  Y nexprlist */. 
2e939 20 20 20 20 20 63 61 73 65 20 32 33 39 3a 20 2f       case 239: /
2e93a 2a 20 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e  * exprlist ::= n
2e93b 65 78 70 72 6c 69 73 74 20 2a 2f 0a 7b 79 79 67  exprlist */.{yyg
2e93c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d  otominor.yy148 =
2e93d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e93e 79 79 31 34 38 3b 7d 0a 20 20 20 20 20 20 20 20  yy148;}.        
2e93f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e940 65 20 31 35 32 3a 20 2f 2a 20 73 6f 72 74 6c 69  e 152: /* sortli
2e941 73 74 20 3a 3a 3d 20 73 6f 72 74 6c 69 73 74 20  st ::= sortlist 
2e942 43 4f 4d 4d 41 20 73 6f 72 74 69 74 65 6d 20 73  COMMA sortitem s
2e943 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20  ortorder */.{.  
2e944 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
2e945 38 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  8 = sqlite3ExprL
2e946 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2e947 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  ,yymsp[-3].minor
2e948 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b 2d 31 5d  .yy148,yymsp[-1]
2e949 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 30 29 3b 0a  .minor.yy72,0);.
2e94a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2e94b 72 2e 79 79 31 34 38 20 29 20 79 79 67 6f 74 6f  r.yy148 ) yygoto
2e94c 6d 69 6e 6f 72 2e 79 79 31 34 38 2d 3e 61 5b 79  minor.yy148->a[y
2e94d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38  ygotominor.yy148
2e94e 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f  ->nExpr-1].sortO
2e94f 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70  rder = (u8)yymsp
2e950 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 3b  [0].minor.yy194;
2e951 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2e952 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 33  ;.      case 153
2e953 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a  : /* sortlist ::
2e954 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f  = sortitem sorto
2e955 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  rder */.{.  yygo
2e956 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20  tominor.yy148 = 
2e957 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2e958 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 79  ppend(pParse,0,y
2e959 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e95a 79 37 32 2c 30 29 3b 0a 20 20 69 66 28 20 79 79  y72,0);.  if( yy
2e95b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20  gotominor.yy148 
2e95c 26 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  && yygotominor.y
2e95d 79 31 34 38 2d 3e 61 20 29 20 79 79 67 6f 74 6f  y148->a ) yygoto
2e95e 6d 69 6e 6f 72 2e 79 79 31 34 38 2d 3e 61 5b 30  minor.yy148->a[0
2e95f 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  ].sortOrder = (u
2e960 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  8)yymsp[0].minor
2e961 2e 79 79 31 39 34 3b 0a 7d 0a 20 20 20 20 20 20  .yy194;.}.      
2e962 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e963 61 73 65 20 31 35 35 3a 20 2f 2a 20 73 6f 72 74  ase 155: /* sort
2e964 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43 20 2a 2f  order ::= ASC */
2e965 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 37 3a  .      case 157:
2e966 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a   /* sortorder ::
2e967 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
2e968 72 2e 79 79 31 39 34 20 3d 20 53 51 4c 49 54 45  r.yy194 = SQLITE
2e969 5f 53 4f 5f 41 53 43 3b 7d 0a 20 20 20 20 20 20  _SO_ASC;}.      
2e96a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e96b 61 73 65 20 31 35 36 3a 20 2f 2a 20 73 6f 72 74  ase 156: /* sort
2e96c 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 20 2a  order ::= DESC *
2e96d 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2e96e 79 31 39 34 20 3d 20 53 51 4c 49 54 45 5f 53 4f  y194 = SQLITE_SO
2e96f 5f 44 45 53 43 3b 7d 0a 20 20 20 20 20 20 20 20  _DESC;}.        
2e970 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e971 65 20 31 36 32 3a 20 2f 2a 20 6c 69 6d 69 74 5f  e 162: /* limit_
2e972 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f  opt ::= */.{yygo
2e973 74 6f 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4c  tominor.yy354.pL
2e974 69 6d 69 74 20 3d 20 30 3b 20 79 79 67 6f 74 6f  imit = 0; yygoto
2e975 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4f 66 66  minor.yy354.pOff
2e976 73 65 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  set = 0;}.      
2e977 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e978 61 73 65 20 31 36 33 3a 20 2f 2a 20 6c 69 6d 69  ase 163: /* limi
2e979 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20  t_opt ::= LIMIT 
2e97a 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  expr */.{yygotom
2e97b 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4c 69 6d 69  inor.yy354.pLimi
2e97c 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  t = yymsp[0].min
2e97d 6f 72 2e 79 79 37 32 3b 20 79 79 67 6f 74 6f 6d  or.yy72; yygotom
2e97e 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4f 66 66 73  inor.yy354.pOffs
2e97f 65 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  et = 0;}.       
2e980 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e981 73 65 20 31 36 34 3a 20 2f 2a 20 6c 69 6d 69 74  se 164: /* limit
2e982 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65  _opt ::= LIMIT e
2e983 78 70 72 20 4f 46 46 53 45 54 20 65 78 70 72 20  xpr OFFSET expr 
2e984 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2e985 79 79 33 35 34 2e 70 4c 69 6d 69 74 20 3d 20 79  yy354.pLimit = y
2e986 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2e987 79 37 32 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  y72; yygotominor
2e988 2e 79 79 33 35 34 2e 70 4f 66 66 73 65 74 20 3d  .yy354.pOffset =
2e989 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e98a 79 79 37 32 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy72;}.        b
2e98b 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e98c 20 31 36 35 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   165: /* limit_o
2e98d 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
2e98e 72 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a  r COMMA expr */.
2e98f 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
2e990 35 34 2e 70 4f 66 66 73 65 74 20 3d 20 79 79 6d  54.pOffset = yym
2e991 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-2].minor.yy7
2e992 32 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  2; yygotominor.y
2e993 79 33 35 34 2e 70 4c 69 6d 69 74 20 3d 20 79 79  y354.pLimit = yy
2e994 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37  msp[0].minor.yy7
2e995 32 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  2;}.        brea
2e996 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36  k;.      case 16
2e997 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45  6: /* cmd ::= DE
2e998 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61  LETE FROM fullna
2e999 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 77  me indexed_opt w
2e99a 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20  here_opt */.{.  
2e99b 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e  sqlite3SrcListIn
2e99c 64 65 78 65 64 42 79 28 70 50 61 72 73 65 2c 20  dexedBy(pParse, 
2e99d 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e99e 79 79 31 38 35 2c 20 26 79 79 6d 73 70 5b 2d 31  yy185, &yymsp[-1
2e99f 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
2e9a0 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f  sqlite3DeleteFro
2e9a1 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  m(pParse,yymsp[-
2e9a2 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 79  2].minor.yy185,y
2e9a3 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e9a4 37 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  72);.}.        b
2e9a5 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e9a6 20 31 36 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   169: /* cmd ::=
2e9a7 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 66   UPDATE orconf f
2e9a8 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f  ullname indexed_
2e9a9 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73 74 20  opt SET setlist 
2e9aa 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20  where_opt */.{. 
2e9ab 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49   sqlite3SrcListI
2e9ac 6e 64 65 78 65 64 42 79 28 70 50 61 72 73 65 2c  ndexedBy(pParse,
2e9ad 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
2e9ae 2e 79 79 31 38 35 2c 20 26 79 79 6d 73 70 5b 2d  .yy185, &yymsp[-
2e9af 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  3].minor.yy0);. 
2e9b0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2e9b1 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72  CheckLength(pPar
2e9b2 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  se,yymsp[-1].min
2e9b3 6f 72 2e 79 79 31 34 38 2c 22 73 65 74 20 6c 69  or.yy148,"set li
2e9b4 73 74 22 29 3b 20 0a 20 20 73 71 6c 69 74 65 33  st"); .  sqlite3
2e9b5 55 70 64 61 74 65 28 70 50 61 72 73 65 2c 79 79  Update(pParse,yy
2e9b6 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2e9b7 31 38 35 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  185,yymsp[-1].mi
2e9b8 6e 6f 72 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b  nor.yy148,yymsp[
2e9b9 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 79 79  0].minor.yy72,yy
2e9ba 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
2e9bb 31 39 34 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  194);.}.        
2e9bc 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e9bd 65 20 31 37 30 3a 20 2f 2a 20 73 65 74 6c 69 73  e 170: /* setlis
2e9be 74 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f  t ::= setlist CO
2e9bf 4d 4d 41 20 6e 6d 20 45 51 20 65 78 70 72 20 2a  MMA nm EQ expr *
2e9c0 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2e9c1 79 31 34 38 20 3d 20 73 71 6c 69 74 65 33 45 78  y148 = sqlite3Ex
2e9c2 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2e9c3 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  rse,yymsp[-4].mi
2e9c4 6e 6f 72 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b  nor.yy148,yymsp[
2e9c5 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79  0].minor.yy72,&y
2e9c6 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2e9c7 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
2e9c8 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e9c9 31 37 31 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20  171: /* setlist 
2e9ca 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72 20 2a  ::= nm EQ expr *
2e9cb 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2e9cc 79 31 34 38 20 3d 20 73 71 6c 69 74 65 33 45 78  y148 = sqlite3Ex
2e9cd 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2e9ce 72 73 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d  rse,0,yymsp[0].m
2e9cf 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70  inor.yy72,&yymsp
2e9d0 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-2].minor.yy0);
2e9d1 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e9d2 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 32 3a  .      case 172:
2e9d3 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65   /* cmd ::= inse
2e9d4 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
2e9d5 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
2e9d6 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74  opt VALUES LP it
2e9d7 65 6d 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 73 71  emlist RP */.{sq
2e9d8 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72  lite3Insert(pPar
2e9d9 73 65 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  se, yymsp[-5].mi
2e9da 6e 6f 72 2e 79 79 31 38 35 2c 20 79 79 6d 73 70  nor.yy185, yymsp
2e9db 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38  [-1].minor.yy148
2e9dc 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  , 0, yymsp[-4].m
2e9dd 69 6e 6f 72 2e 79 79 32 35 34 2c 20 79 79 6d 73  inor.yy254, yyms
2e9de 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39  p[-7].minor.yy19
2e9df 34 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  4);}.        bre
2e9e0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e9e1 37 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69  73: /* cmd ::= i
2e9e2 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66  nsert_cmd INTO f
2e9e3 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69  ullname inscolli
2e9e4 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 20 2a 2f  st_opt select */
2e9e5 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28  .{sqlite3Insert(
2e9e6 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32  pParse, yymsp[-2
2e9e7 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 20 30  ].minor.yy185, 0
2e9e8 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2e9e9 2e 79 79 32 34 33 2c 20 79 79 6d 73 70 5b 2d 31  .yy243, yymsp[-1
2e9ea 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 2c 20 79  ].minor.yy254, y
2e9eb 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2e9ec 79 31 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20  y194);}.        
2e9ed 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e9ee 65 20 31 37 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 174: /* cmd ::
2e9ef 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
2e9f0 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f  O fullname insco
2e9f1 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46 41 55 4c  llist_opt DEFAUL
2e9f2 54 20 56 41 4c 55 45 53 20 2a 2f 0a 7b 73 71 6c  T VALUES */.{sql
2e9f3 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73  ite3Insert(pPars
2e9f4 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  e, yymsp[-3].min
2e9f5 6f 72 2e 79 79 31 38 35 2c 20 30 2c 20 30 2c 20  or.yy185, 0, 0, 
2e9f6 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e9f7 79 79 32 35 34 2c 20 79 79 6d 73 70 5b 2d 35 5d  yy254, yymsp[-5]
2e9f8 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a  .minor.yy194);}.
2e9f9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e9fa 20 20 20 20 20 63 61 73 65 20 31 37 37 3a 20 2f       case 177: /
2e9fb 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69  * itemlist ::= i
2e9fc 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78  temlist COMMA ex
2e9fd 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
2e9fe 20 32 34 31 3a 20 2f 2a 20 6e 65 78 70 72 6c 69   241: /* nexprli
2e9ff 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74  st ::= nexprlist
2ea00 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a 7b   COMMA expr */.{
2ea01 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
2ea02 38 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  8 = sqlite3ExprL
2ea03 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2ea04 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
2ea05 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b 30 5d 2e  .yy148,yymsp[0].
2ea06 6d 69 6e 6f 72 2e 79 79 37 32 2c 30 29 3b 7d 0a  minor.yy72,0);}.
2ea07 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2ea08 20 20 20 20 20 63 61 73 65 20 31 37 38 3a 20 2f       case 178: /
2ea09 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65  * itemlist ::= e
2ea0a 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
2ea0b 65 20 32 34 32 3a 20 2f 2a 20 6e 65 78 70 72 6c  e 242: /* nexprl
2ea0c 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 0a  ist ::= expr */.
2ea0d 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
2ea0e 34 38 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  48 = sqlite3Expr
2ea0f 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2ea10 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e,0,yymsp[0].min
2ea11 6f 72 2e 79 79 37 32 2c 30 29 3b 7d 0a 20 20 20  or.yy72,0);}.   
2ea12 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ea13 20 20 63 61 73 65 20 31 38 31 3a 20 2f 2a 20 69    case 181: /* i
2ea14 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e  nscollist ::= in
2ea15 73 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 6e  scollist COMMA n
2ea16 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  m */.{yygotomino
2ea17 72 2e 79 79 32 35 34 20 3d 20 73 71 6c 69 74 65  r.yy254 = sqlite
2ea18 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 70 50  3IdListAppend(pP
2ea19 61 72 73 65 2d 3e 64 62 2c 79 79 6d 73 70 5b 2d  arse->db,yymsp[-
2ea1a 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 2c 26  2].minor.yy254,&
2ea1b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ea1c 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
2ea1d 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ea1e 31 38 32 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69  182: /* inscolli
2ea1f 73 74 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b 79 79  st ::= nm */.{yy
2ea20 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 34 20  gotominor.yy254 
2ea21 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41  = sqlite3IdListA
2ea22 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
2ea23 2c 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  ,0,&yymsp[0].min
2ea24 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
2ea25 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2ea26 61 73 65 20 31 38 34 3a 20 2f 2a 20 65 78 70 72  ase 184: /* expr
2ea27 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52 50 20   ::= LP expr RP 
2ea28 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2ea29 79 79 37 32 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  yy72 = yymsp[-1]
2ea2a 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b 20 73 71 6c  .minor.yy72; sql
2ea2b 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67  ite3ExprSpan(yyg
2ea2c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79  otominor.yy72,&y
2ea2d 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2ea2e 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
2ea2f 6f 72 2e 79 79 30 29 3b 20 7d 0a 20 20 20 20 20  or.yy0); }.     
2ea30 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ea31 63 61 73 65 20 31 38 35 3a 20 2f 2a 20 74 65 72  case 185: /* ter
2ea32 6d 20 3a 3a 3d 20 4e 55 4c 4c 20 2a 2f 0a 20 20  m ::= NULL */.  
2ea33 20 20 20 20 63 61 73 65 20 31 39 30 3a 20 2f 2a      case 190: /*
2ea34 20 74 65 72 6d 20 3a 3a 3d 20 49 4e 54 45 47 45   term ::= INTEGE
2ea35 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42 20 2a 2f 0a  R|FLOAT|BLOB */.
2ea36 20 20 20 20 20 20 63 61 73 65 20 31 39 31 3a 20        case 191: 
2ea37 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 53 54 52 49  /* term ::= STRI
2ea38 4e 47 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  NG */.{yygotomin
2ea39 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65  or.yy72 = sqlite
2ea3a 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 79  3PExpr(pParse, y
2ea3b 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 20 30  ymsp[0].major, 0
2ea3c 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  , 0, &yymsp[0].m
2ea3d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
2ea3e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ea3f 20 63 61 73 65 20 31 38 36 3a 20 2f 2a 20 65 78   case 186: /* ex
2ea40 70 72 20 3a 3a 3d 20 69 64 20 2a 2f 0a 20 20 20  pr ::= id */.   
2ea41 20 20 20 63 61 73 65 20 31 38 37 3a 20 2f 2a 20     case 187: /* 
2ea42 65 78 70 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57  expr ::= JOIN_KW
2ea43 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2ea44 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50  .yy72 = sqlite3P
2ea45 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2ea46 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70  ID, 0, 0, &yymsp
2ea47 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
2ea48 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ea49 20 20 20 20 20 20 63 61 73 65 20 31 38 38 3a 20        case 188: 
2ea4a 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44  /* expr ::= nm D
2ea4b 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70  OT nm */.{.  Exp
2ea4c 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74  r *temp1 = sqlit
2ea4d 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2ea4e 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
2ea4f 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2ea50 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  0);.  Expr *temp
2ea51 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2ea52 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
2ea53 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  0, 0, &yymsp[0].
2ea54 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 79 79  minor.yy0);.  yy
2ea55 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d  gotominor.yy72 =
2ea56 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2ea57 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65  arse, TK_DOT, te
2ea58 6d 70 31 2c 20 74 65 6d 70 32 2c 20 30 29 3b 0a  mp1, temp2, 0);.
2ea59 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ea5a 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 39 3a  .      case 189:
2ea5b 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 6e 6d 20   /* expr ::= nm 
2ea5c 44 4f 54 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f  DOT nm DOT nm */
2ea5d 0a 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 31  .{.  Expr *temp1
2ea5e 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2ea5f 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
2ea60 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e  , 0, &yymsp[-4].
2ea61 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78  minor.yy0);.  Ex
2ea62 70 72 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69  pr *temp2 = sqli
2ea63 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2ea64 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79   TK_ID, 0, 0, &y
2ea65 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2ea66 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d  y0);.  Expr *tem
2ea67 70 33 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  p3 = sqlite3PExp
2ea68 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
2ea69 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d   0, 0, &yymsp[0]
2ea6a 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
2ea6b 78 70 72 20 2a 74 65 6d 70 34 20 3d 20 73 71 6c  xpr *temp4 = sql
2ea6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2ea6d 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 32 2c  , TK_DOT, temp2,
2ea6e 20 74 65 6d 70 33 2c 20 30 29 3b 0a 20 20 79 79   temp3, 0);.  yy
2ea6f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d  gotominor.yy72 =
2ea70 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2ea71 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65  arse, TK_DOT, te
2ea72 6d 70 31 2c 20 74 65 6d 70 34 2c 20 30 29 3b 0a  mp1, temp4, 0);.
2ea73 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ea74 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 32 3a  .      case 192:
2ea75 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 52 45 47   /* expr ::= REG
2ea76 49 53 54 45 52 20 2a 2f 0a 7b 79 79 67 6f 74 6f  ISTER */.{yygoto
2ea77 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c  minor.yy72 = sql
2ea78 69 74 65 33 52 65 67 69 73 74 65 72 45 78 70 72  ite3RegisterExpr
2ea79 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
2ea7a 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
2ea7b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2ea7c 20 20 20 20 20 63 61 73 65 20 31 39 33 3a 20 2f       case 193: /
2ea7d 2a 20 65 78 70 72 20 3a 3a 3d 20 56 41 52 49 41  * expr ::= VARIA
2ea7e 42 4c 45 20 2a 2f 0a 7b 0a 20 20 54 6f 6b 65 6e  BLE */.{.  Token
2ea7f 20 2a 70 54 6f 6b 65 6e 20 3d 20 26 79 79 6d 73   *pToken = &yyms
2ea80 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a  p[0].minor.yy0;.
2ea81 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20    Expr *pExpr = 
2ea82 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2ea83 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2ea84 70 50 61 72 73 65 2c 20 54 4b 5f 56 41 52 49 41  pParse, TK_VARIA
2ea85 42 4c 45 2c 20 30 2c 20 30 2c 20 70 54 6f 6b 65  BLE, 0, 0, pToke
2ea86 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  n);.  sqlite3Exp
2ea87 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72  rAssignVarNumber
2ea88 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b  (pParse, pExpr);
2ea89 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2ea8a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 34  ;.      case 194
2ea8b 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
2ea8c 70 72 20 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a  pr COLLATE ids *
2ea8d 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2ea8e 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33  r.yy72 = sqlite3
2ea8f 45 78 70 72 53 65 74 43 6f 6c 6c 28 70 50 61 72  ExprSetColl(pPar
2ea90 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  se, yymsp[-2].mi
2ea91 6e 6f 72 2e 79 79 37 32 2c 20 26 79 79 6d 73 70  nor.yy72, &yymsp
2ea92 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2ea93 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ea94 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 35 3a  .      case 195:
2ea95 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41 53   /* expr ::= CAS
2ea96 54 20 4c 50 20 65 78 70 72 20 41 53 20 74 79 70  T LP expr AS typ
2ea97 65 74 6f 6b 65 6e 20 52 50 20 2a 2f 0a 7b 0a 20  etoken RP */.{. 
2ea98 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2ea99 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2ea9a 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 41 53 54  (pParse, TK_CAST
2ea9b 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  , yymsp[-3].mino
2ea9c 72 2e 79 79 37 32 2c 20 30 2c 20 26 79 79 6d 73  r.yy72, 0, &yyms
2ea9d 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
2ea9e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  ;.  sqlite3ExprS
2ea9f 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pan(yygotominor.
2eaa0 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e  yy72,&yymsp[-5].
2eaa1 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2eaa2 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2eaa3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2eaa4 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 36 3a  .      case 196:
2eaa5 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20   /* expr ::= ID 
2eaa6 4c 50 20 64 69 73 74 69 6e 63 74 20 65 78 70 72  LP distinct expr
2eaa7 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 69  list RP */.{.  i
2eaa8 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  f( yymsp[-1].min
2eaa9 6f 72 2e 79 79 31 34 38 20 26 26 20 79 79 6d 73  or.yy148 && yyms
2eaaa 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2eaab 38 2d 3e 6e 45 78 70 72 3e 53 51 4c 49 54 45 5f  8->nExpr>SQLITE_
2eaac 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MAX_FUNCTION_ARG
2eaad 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2eaae 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2eaaf 22 74 6f 6f 20 6d 61 6e 79 20 61 72 67 75 6d 65  "too many argume
2eab0 6e 74 73 20 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  nts on function 
2eab1 25 54 22 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e  %T", &yymsp[-4].
2eab2 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a  minor.yy0);.  }.
2eab3 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2eab4 37 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  72 = sqlite3Expr
2eab5 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c  Function(pParse,
2eab6 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2eab7 2e 79 79 31 34 38 2c 20 26 79 79 6d 73 70 5b 2d  .yy148, &yymsp[-
2eab8 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  4].minor.yy0);. 
2eab9 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
2eaba 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37  (yygotominor.yy7
2eabb 32 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  2,&yymsp[-4].min
2eabc 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
2eabd 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69  .minor.yy0);.  i
2eabe 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  f( yymsp[-2].min
2eabf 6f 72 2e 79 79 31 39 34 20 26 26 20 79 79 67 6f  or.yy194 && yygo
2eac0 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29 7b 0a  tominor.yy72 ){.
2eac1 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
2eac2 79 79 37 32 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  yy72->flags |= E
2eac3 50 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a  P_Distinct;.  }.
2eac4 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2eac5 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 37 3a  .      case 197:
2eac6 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20   /* expr ::= ID 
2eac7 4c 50 20 53 54 41 52 20 52 50 20 2a 2f 0a 7b 0a  LP STAR RP */.{.
2eac8 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2eac9 37 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  72 = sqlite3Expr
2eaca 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c  Function(pParse,
2eacb 20 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d   0, &yymsp[-3].m
2eacc 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c  inor.yy0);.  sql
2eacd 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67  ite3ExprSpan(yyg
2eace 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79  otominor.yy72,&y
2eacf 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
2ead0 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
2ead1 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
2ead2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ead3 63 61 73 65 20 31 39 38 3a 20 2f 2a 20 74 65 72  case 198: /* ter
2ead4 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57 20 2a  m ::= CTIME_KW *
2ead5 2f 0a 7b 0a 20 20 2f 2a 20 54 68 65 20 43 55 52  /.{.  /* The CUR
2ead6 52 45 4e 54 5f 54 49 4d 45 2c 20 43 55 52 52 45  RENT_TIME, CURRE
2ead7 4e 54 5f 44 41 54 45 2c 20 61 6e 64 20 43 55 52  NT_DATE, and CUR
2ead8 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50 20 76  RENT_TIMESTAMP v
2ead9 61 6c 75 65 73 20 61 72 65 0a 20 20 2a 2a 20 74  alues are.  ** t
2eada 72 65 61 74 65 64 20 61 73 20 66 75 6e 63 74 69  reated as functi
2eadb 6f 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e 20  ons that return 
2eadc 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 79  constants */.  y
2eadd 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2eade 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e  = sqlite3ExprFun
2eadf 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ction(pParse, 0,
2eae0 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2eae1 79 79 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f  yy0);.  if( yygo
2eae2 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29 7b 0a  tominor.yy72 ){.
2eae3 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
2eae4 79 79 37 32 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f  yy72->op = TK_CO
2eae5 4e 53 54 5f 46 55 4e 43 3b 20 20 0a 20 20 20 20  NST_FUNC;  .    
2eae6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2eae7 2d 3e 73 70 61 6e 20 3d 20 79 79 6d 73 70 5b 30  ->span = yymsp[0
2eae8 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 7d  ].minor.yy0;.  }
2eae9 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2eaea 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 39  ;.      case 199
2eaeb 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
2eaec 70 72 20 41 4e 44 20 65 78 70 72 20 2a 2f 0a 20  pr AND expr */. 
2eaed 20 20 20 20 20 63 61 73 65 20 32 30 30 3a 20 2f       case 200: /
2eaee 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
2eaef 4f 52 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  OR expr */.     
2eaf0 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20 65 78   case 201: /* ex
2eaf1 70 72 20 3a 3a 3d 20 65 78 70 72 20 4c 54 7c 47  pr ::= expr LT|G
2eaf2 54 7c 47 45 7c 4c 45 20 65 78 70 72 20 2a 2f 0a  T|GE|LE expr */.
2eaf3 20 20 20 20 20 20 63 61 73 65 20 32 30 32 3a 20        case 202: 
2eaf4 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
2eaf5 20 45 51 7c 4e 45 20 65 78 70 72 20 2a 2f 0a 20   EQ|NE expr */. 
2eaf6 20 20 20 20 20 63 61 73 65 20 32 30 33 3a 20 2f       case 203: /
2eaf7 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
2eaf8 42 49 54 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48  BITAND|BITOR|LSH
2eaf9 49 46 54 7c 52 53 48 49 46 54 20 65 78 70 72 20  IFT|RSHIFT expr 
2eafa 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 30  */.      case 20
2eafb 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  4: /* expr ::= e
2eafc 78 70 72 20 50 4c 55 53 7c 4d 49 4e 55 53 20 65  xpr PLUS|MINUS e
2eafd 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
2eafe 65 20 32 30 35 3a 20 2f 2a 20 65 78 70 72 20 3a  e 205: /* expr :
2eaff 3a 3d 20 65 78 70 72 20 53 54 41 52 7c 53 4c 41  := expr STAR|SLA
2eb00 53 48 7c 52 45 4d 20 65 78 70 72 20 2a 2f 0a 20  SH|REM expr */. 
2eb01 20 20 20 20 20 63 61 73 65 20 32 30 36 3a 20 2f       case 206: /
2eb02 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
2eb03 43 4f 4e 43 41 54 20 65 78 70 72 20 2a 2f 0a 7b  CONCAT expr */.{
2eb04 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2eb05 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2eb06 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d  pParse,yymsp[-1]
2eb07 2e 6d 61 6a 6f 72 2c 79 79 6d 73 70 5b 2d 32 5d  .major,yymsp[-2]
2eb08 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 79 79 6d 73  .minor.yy72,yyms
2eb09 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c  p[0].minor.yy72,
2eb0a 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2eb0b 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2eb0c 30 37 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a  07: /* likeop ::
2eb0d 3d 20 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20  = LIKE_KW */.   
2eb0e 20 20 20 63 61 73 65 20 32 30 39 3a 20 2f 2a 20     case 209: /* 
2eb0f 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48  likeop ::= MATCH
2eb10 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2eb11 2e 79 79 33 39 32 2e 65 4f 70 65 72 61 74 6f 72  .yy392.eOperator
2eb12 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
2eb13 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  r.yy0; yygotomin
2eb14 6f 72 2e 79 79 33 39 32 2e 6e 6f 74 20 3d 20 30  or.yy392.not = 0
2eb15 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2eb16 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 38  ;.      case 208
2eb17 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20  : /* likeop ::= 
2eb18 4e 4f 54 20 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20  NOT LIKE_KW */. 
2eb19 20 20 20 20 20 63 61 73 65 20 32 31 30 3a 20 2f       case 210: /
2eb1a 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54  * likeop ::= NOT
2eb1b 20 4d 41 54 43 48 20 2a 2f 0a 7b 79 79 67 6f 74   MATCH */.{yygot
2eb1c 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 2e 65 4f 70  ominor.yy392.eOp
2eb1d 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30  erator = yymsp[0
2eb1e 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67  ].minor.yy0; yyg
2eb1f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 2e 6e  otominor.yy392.n
2eb20 6f 74 20 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20  ot = 1;}.       
2eb21 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2eb22 73 65 20 32 31 33 3a 20 2f 2a 20 65 78 70 72 20  se 213: /* expr 
2eb23 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f 70 20  ::= expr likeop 
2eb24 65 78 70 72 20 65 73 63 61 70 65 20 2a 2f 0a 7b  expr escape */.{
2eb25 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
2eb26 73 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71  st;.  pList = sq
2eb27 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2eb28 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79  end(pParse,0, yy
2eb29 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2eb2a 37 32 2c 20 30 29 3b 0a 20 20 70 4c 69 73 74 20  72, 0);.  pList 
2eb2b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2eb2c 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70  tAppend(pParse,p
2eb2d 4c 69 73 74 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e  List, yymsp[-3].
2eb2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a  minor.yy72, 0);.
2eb2f 20 20 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d    if( yymsp[0].m
2eb30 69 6e 6f 72 2e 79 79 37 32 20 29 7b 0a 20 20 20  inor.yy72 ){.   
2eb31 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
2eb32 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2eb33 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79 6d  Parse,pList, yym
2eb34 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  sp[0].minor.yy72
2eb35 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f  , 0);.  }.  yygo
2eb36 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73  tominor.yy72 = s
2eb37 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69  qlite3ExprFuncti
2eb38 6f 6e 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74  on(pParse, pList
2eb39 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
2eb3a 6f 72 2e 79 79 33 39 32 2e 65 4f 70 65 72 61 74  or.yy392.eOperat
2eb3b 6f 72 29 3b 0a 20 20 69 66 28 20 79 79 6d 73 70  or);.  if( yymsp
2eb3c 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32  [-2].minor.yy392
2eb3d 2e 6e 6f 74 20 29 20 79 79 67 6f 74 6f 6d 69 6e  .not ) yygotomin
2eb3e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65  or.yy72 = sqlite
2eb3f 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2eb40 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e  K_NOT, yygotomin
2eb41 6f 72 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a  or.yy72, 0, 0);.
2eb42 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2eb43 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2eb44 37 32 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  72, &yymsp[-3].m
2eb45 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 2c  inor.yy72->span,
2eb46 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f   &yymsp[-1].mino
2eb47 72 2e 79 79 37 32 2d 3e 73 70 61 6e 29 3b 0a 20  r.yy72->span);. 
2eb48 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
2eb49 2e 79 79 37 32 20 29 20 79 79 67 6f 74 6f 6d 69  .yy72 ) yygotomi
2eb4a 6e 6f 72 2e 79 79 37 32 2d 3e 66 6c 61 67 73 20  nor.yy72->flags 
2eb4b 7c 3d 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 3b  |= EP_InfixFunc;
2eb4c 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2eb4d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 34  ;.      case 214
2eb4e 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
2eb4f 70 72 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c  pr ISNULL|NOTNUL
2eb50 4c 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  L */.{.  yygotom
2eb51 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69  inor.yy72 = sqli
2eb52 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2eb53 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c   yymsp[0].major,
2eb54 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2eb55 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  .yy72, 0, 0);.  
2eb56 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
2eb57 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2eb58 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
2eb59 72 2e 79 79 37 32 2d 3e 73 70 61 6e 2c 26 79 79  r.yy72->span,&yy
2eb5a 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2eb5b 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2eb5c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2eb5d 31 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  15: /* expr ::= 
2eb5e 65 78 70 72 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a  expr IS NULL */.
2eb5f 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2eb60 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45  yy72 = sqlite3PE
2eb61 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
2eb62 53 4e 55 4c 4c 2c 20 79 79 6d 73 70 5b 2d 32 5d  SNULL, yymsp[-2]
2eb63 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20  .minor.yy72, 0, 
2eb64 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
2eb65 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2eb66 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 32  r.yy72,&yymsp[-2
2eb67 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70  ].minor.yy72->sp
2eb68 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  an,&yymsp[0].min
2eb69 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
2eb6a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2eb6b 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 65 78 70  case 216: /* exp
2eb6c 72 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e  r ::= expr NOT N
2eb6d 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  ULL */.{.  yygot
2eb6e 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71  ominor.yy72 = sq
2eb6f 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2eb70 65 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 79  e, TK_NOTNULL, y
2eb71 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2eb72 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71  y72, 0, 0);.  sq
2eb73 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79  lite3ExprSpan(yy
2eb74 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26  gotominor.yy72,&
2eb75 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2eb76 79 79 37 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73  yy72->span,&yyms
2eb77 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2eb78 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2eb79 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 37  ;.      case 217
2eb7a 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
2eb7b 70 72 20 49 53 20 4e 4f 54 20 4e 55 4c 4c 20 2a  pr IS NOT NULL *
2eb7c 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2eb7d 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33  r.yy72 = sqlite3
2eb7e 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2eb7f 5f 4e 4f 54 4e 55 4c 4c 2c 20 79 79 6d 73 70 5b  _NOTNULL, yymsp[
2eb80 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20  -3].minor.yy72, 
2eb81 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0);.  sqlite3
2eb82 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d  ExprSpan(yygotom
2eb83 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70  inor.yy72,&yymsp
2eb84 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d  [-3].minor.yy72-
2eb85 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e  >span,&yymsp[0].
2eb86 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
2eb87 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2eb88 20 20 20 63 61 73 65 20 32 31 38 3a 20 2f 2a 20     case 218: /* 
2eb89 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70  expr ::= NOT exp
2eb8a 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2eb8b 32 31 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  219: /* expr ::=
2eb8c 20 42 49 54 4e 4f 54 20 65 78 70 72 20 2a 2f 0a   BITNOT expr */.
2eb8d 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2eb8e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45  yy72 = sqlite3PE
2eb8f 78 70 72 28 70 50 61 72 73 65 2c 20 79 79 6d 73  xpr(pParse, yyms
2eb90 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 20 79 79 6d  p[-1].major, yym
2eb91 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  sp[0].minor.yy72
2eb92 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
2eb93 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2eb94 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d  ominor.yy72,&yym
2eb95 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2eb96 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2eb97 2e 79 79 37 32 2d 3e 73 70 61 6e 29 3b 0a 7d 0a  .yy72->span);.}.
2eb98 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2eb99 20 20 20 20 20 63 61 73 65 20 32 32 30 3a 20 2f       case 220: /
2eb9a 2a 20 65 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53  * expr ::= MINUS
2eb9b 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67   expr */.{.  yyg
2eb9c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20  otominor.yy72 = 
2eb9d 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2eb9e 72 73 65 2c 20 54 4b 5f 55 4d 49 4e 55 53 2c 20  rse, TK_UMINUS, 
2eb9f 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2eba0 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71  y72, 0, 0);.  sq
2eba1 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79  lite3ExprSpan(yy
2eba2 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26  gotominor.yy72,&
2eba3 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2eba4 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
2eba5 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 29 3b  nor.yy72->span);
2eba6 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2eba7 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 31  ;.      case 221
2eba8 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 50 4c  : /* expr ::= PL
2eba9 55 53 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79  US expr */.{.  y
2ebaa 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2ebab 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2ebac 50 61 72 73 65 2c 20 54 4b 5f 55 50 4c 55 53 2c  Parse, TK_UPLUS,
2ebad 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2ebae 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73  yy72, 0, 0);.  s
2ebaf 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
2ebb0 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c  ygotominor.yy72,
2ebb1 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
2ebb2 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
2ebb3 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 29  inor.yy72->span)
2ebb4 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2ebb5 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
2ebb6 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  4: /* expr ::= e
2ebb7 78 70 72 20 62 65 74 77 65 65 6e 5f 6f 70 20 65  xpr between_op e
2ebb8 78 70 72 20 41 4e 44 20 65 78 70 72 20 2a 2f 0a  xpr AND expr */.
2ebb9 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  {.  ExprList *pL
2ebba 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
2ebbb 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2ebbc 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  se,0, yymsp[-2].
2ebbd 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a  minor.yy72, 0);.
2ebbe 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
2ebbf 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2ebc0 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79  pParse,pList, yy
2ebc1 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37  msp[0].minor.yy7
2ebc2 32 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  2, 0);.  yygotom
2ebc3 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69  inor.yy72 = sqli
2ebc4 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2ebc5 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20 79 79 6d   TK_BETWEEN, yym
2ebc6 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-4].minor.yy7
2ebc7 32 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20  2, 0, 0);.  if( 
2ebc8 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2ebc9 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69   ){.    yygotomi
2ebca 6e 6f 72 2e 79 79 37 32 2d 3e 78 2e 70 4c 69 73  nor.yy72->x.pLis
2ebcb 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 7d 65 6c  t = pList;.  }el
2ebcc 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  se{.    sqlite3E
2ebcd 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
2ebce 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29  arse->db, pList)
2ebcf 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 79 79 6d  ;.  } .  if( yym
2ebd0 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
2ebd1 39 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  94 ) yygotominor
2ebd2 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50  .yy72 = sqlite3P
2ebd3 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2ebd4 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  NOT, yygotominor
2ebd5 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  .yy72, 0, 0);.  
2ebd6 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
2ebd7 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2ebd8 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  ,&yymsp[-4].mino
2ebd9 72 2e 79 79 37 32 2d 3e 73 70 61 6e 2c 26 79 79  r.yy72->span,&yy
2ebda 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37  msp[0].minor.yy7
2ebdb 32 2d 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20 20 20  2->span);.}.    
2ebdc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ebdd 20 63 61 73 65 20 32 32 37 3a 20 2f 2a 20 65 78   case 227: /* ex
2ebde 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f  pr ::= expr in_o
2ebdf 70 20 4c 50 20 65 78 70 72 6c 69 73 74 20 52 50  p LP exprlist RP
2ebe0 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f   */.{.    yygoto
2ebe1 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c  minor.yy72 = sql
2ebe2 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2ebe3 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d  , TK_IN, yymsp[-
2ebe4 34 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30  4].minor.yy72, 0
2ebe5 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79  , 0);.    if( yy
2ebe6 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29  gotominor.yy72 )
2ebe7 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69  {.      yygotomi
2ebe8 6e 6f 72 2e 79 79 37 32 2d 3e 78 2e 70 4c 69 73  nor.yy72->x.pLis
2ebe9 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  t = yymsp[-1].mi
2ebea 6e 6f 72 2e 79 79 31 34 38 3b 0a 20 20 20 20 20  nor.yy148;.     
2ebeb 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
2ebec 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79  eight(pParse, yy
2ebed 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 29 3b  gotominor.yy72);
2ebee 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2ebef 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2ebf0 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
2ebf1 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  db, yymsp[-1].mi
2ebf2 6e 6f 72 2e 79 79 31 34 38 29 3b 0a 20 20 20 20  nor.yy148);.    
2ebf3 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b  }.    if( yymsp[
2ebf4 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 20  -3].minor.yy194 
2ebf5 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
2ebf6 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
2ebf7 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
2ebf8 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
2ebf9 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  72, 0, 0);.    s
2ebfa 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
2ebfb 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c  ygotominor.yy72,
2ebfc 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
2ebfd 2e 79 79 37 32 2d 3e 73 70 61 6e 2c 26 79 79 6d  .yy72->span,&yym
2ebfe 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
2ebff 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ;.  }.        br
2ec00 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ec01 32 32 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  228: /* expr ::=
2ec02 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f   LP select RP */
2ec03 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .{.    yygotomin
2ec04 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65  or.yy72 = sqlite
2ec05 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2ec06 4b 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20  K_SELECT, 0, 0, 
2ec07 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f  0);.    if( yygo
2ec08 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29 7b 0a  tominor.yy72 ){.
2ec09 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f        yygotomino
2ec0a 72 2e 79 79 37 32 2d 3e 78 2e 70 53 65 6c 65 63  r.yy72->x.pSelec
2ec0b 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  t = yymsp[-1].mi
2ec0c 6e 6f 72 2e 79 79 32 34 33 3b 0a 20 20 20 20 20  nor.yy243;.     
2ec0d 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
2ec0e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37  (yygotominor.yy7
2ec0f 32 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  2, EP_xIsSelect)
2ec10 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2ec11 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61  xprSetHeight(pPa
2ec12 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  rse, yygotominor
2ec13 2e 79 79 37 32 29 3b 0a 20 20 20 20 7d 65 6c 73  .yy72);.    }els
2ec14 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
2ec15 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61  SelectDelete(pPa
2ec16 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d  rse->db, yymsp[-
2ec17 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 29 3b  1].minor.yy243);
2ec18 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2ec19 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2ec1a 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d  ominor.yy72,&yym
2ec1b 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
2ec1c 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2ec1d 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20  .yy0);.  }.     
2ec1e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ec1f 63 61 73 65 20 32 32 39 3a 20 2f 2a 20 65 78 70  case 229: /* exp
2ec20 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70  r ::= expr in_op
2ec21 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f   LP select RP */
2ec22 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .{.    yygotomin
2ec23 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65  or.yy72 = sqlite
2ec24 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2ec25 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  K_IN, yymsp[-4].
2ec26 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20 30  minor.yy72, 0, 0
2ec27 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74  );.    if( yygot
2ec28 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29 7b 0a 20  ominor.yy72 ){. 
2ec29 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
2ec2a 2e 79 79 37 32 2d 3e 78 2e 70 53 65 6c 65 63 74  .yy72->x.pSelect
2ec2b 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
2ec2c 6f 72 2e 79 79 32 34 33 3b 0a 20 20 20 20 20 20  or.yy243;.      
2ec2d 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28  ExprSetProperty(
2ec2e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2ec2f 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b  , EP_xIsSelect);
2ec30 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2ec31 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72  prSetHeight(pPar
2ec32 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
2ec33 79 79 37 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65  yy72);.    }else
2ec34 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
2ec35 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72  electDelete(pPar
2ec36 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31  se->db, yymsp[-1
2ec37 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 29 3b 0a  ].minor.yy243);.
2ec38 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79 79      }.    if( yy
2ec39 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2ec3a 31 39 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  194 ) yygotomino
2ec3b 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33  r.yy72 = sqlite3
2ec3c 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2ec3d 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  _NOT, yygotomino
2ec3e 72 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20  r.yy72, 0, 0);. 
2ec3f 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70     sqlite3ExprSp
2ec40 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2ec41 79 37 32 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  y72,&yymsp[-4].m
2ec42 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 2c  inor.yy72->span,
2ec43 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2ec44 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20  yy0);.  }.      
2ec45 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2ec46 61 73 65 20 32 33 30 3a 20 2f 2a 20 65 78 70 72  ase 230: /* expr
2ec47 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20   ::= expr in_op 
2ec48 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20  nm dbnm */.{.   
2ec49 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20 3d   SrcList *pSrc =
2ec4a 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
2ec4b 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
2ec4c 2c 20 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  , 0,&yymsp[-1].m
2ec4d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2ec4e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
2ec4f 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
2ec50 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78  y72 = sqlite3PEx
2ec51 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e  pr(pParse, TK_IN
2ec52 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  , yymsp[-3].mino
2ec53 72 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20  r.yy72, 0, 0);. 
2ec54 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e     if( yygotomin
2ec55 6f 72 2e 79 79 37 32 20 29 7b 0a 20 20 20 20 20  or.yy72 ){.     
2ec56 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2ec57 32 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 73  2->x.pSelect = s
2ec58 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
2ec59 70 50 61 72 73 65 2c 20 30 2c 70 53 72 63 2c 30  pParse, 0,pSrc,0
2ec5a 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20  ,0,0,0,0,0,0);. 
2ec5b 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70       ExprSetProp
2ec5c 65 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  erty(yygotominor
2ec5d 2e 79 79 37 32 2c 20 45 50 5f 78 49 73 53 65 6c  .yy72, EP_xIsSel
2ec5e 65 63 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ect);.      sqli
2ec5f 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
2ec60 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
2ec61 69 6e 6f 72 2e 79 79 37 32 29 3b 0a 20 20 20 20  inor.yy72);.    
2ec62 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
2ec63 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
2ec64 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  e(pParse->db, pS
2ec65 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  rc);.    }.    i
2ec66 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  f( yymsp[-2].min
2ec67 6f 72 2e 79 79 31 39 34 20 29 20 79 79 67 6f 74  or.yy194 ) yygot
2ec68 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71  ominor.yy72 = sq
2ec69 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ec6a 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74  e, TK_NOT, yygot
2ec6b 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20  ominor.yy72, 0, 
2ec6c 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  0);.    sqlite3E
2ec6d 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2ec6e 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b  nor.yy72,&yymsp[
2ec6f 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e  -3].minor.yy72->
2ec70 73 70 61 6e 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  span,yymsp[0].mi
2ec71 6e 6f 72 2e 79 79 30 2e 7a 3f 26 79 79 6d 73 70  nor.yy0.z?&yymsp
2ec72 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a 26 79  [0].minor.yy0:&y
2ec73 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ec74 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20  y0);.  }.       
2ec75 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2ec76 73 65 20 32 33 31 3a 20 2f 2a 20 65 78 70 72 20  se 231: /* expr 
2ec77 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20 73 65  ::= EXISTS LP se
2ec78 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20  lect RP */.{.   
2ec79 20 45 78 70 72 20 2a 70 20 3d 20 79 79 67 6f 74   Expr *p = yygot
2ec7a 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71  ominor.yy72 = sq
2ec7b 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ec7c 65 2c 20 54 4b 5f 45 58 49 53 54 53 2c 20 30 2c  e, TK_EXISTS, 0,
2ec7d 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   0, 0);.    if( 
2ec7e 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 78 2e  p ){.      p->x.
2ec7f 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b  pSelect = yymsp[
2ec80 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 3b  -1].minor.yy243;
2ec81 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72  .      ExprSetPr
2ec82 6f 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e  operty(yygotomin
2ec83 6f 72 2e 79 79 37 32 2c 20 45 50 5f 78 49 73 53  or.yy72, EP_xIsS
2ec84 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 73 71  elect);.      sq
2ec85 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 70 2c  lite3ExprSpan(p,
2ec86 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
2ec87 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
2ec88 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20 20  inor.yy0);.     
2ec89 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
2ec8a 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79  eight(pParse, yy
2ec8b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 29 3b  gotominor.yy72);
2ec8c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2ec8d 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2ec8e 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2ec8f 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2ec90 72 2e 79 79 32 34 33 29 3b 0a 20 20 20 20 7d 0a  r.yy243);.    }.
2ec91 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
2ec92 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  k;.      case 23
2ec93 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43  2: /* expr ::= C
2ec94 41 53 45 20 63 61 73 65 5f 6f 70 65 72 61 6e 64  ASE case_operand
2ec95 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 63   case_exprlist c
2ec96 61 73 65 5f 65 6c 73 65 20 45 4e 44 20 2a 2f 0a  ase_else END */.
2ec97 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2ec98 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45  yy72 = sqlite3PE
2ec99 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43  xpr(pParse, TK_C
2ec9a 41 53 45 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  ASE, yymsp[-3].m
2ec9b 69 6e 6f 72 2e 79 79 37 32 2c 20 79 79 6d 73 70  inor.yy72, yymsp
2ec9c 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c  [-1].minor.yy72,
2ec9d 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74   0);.  if( yygot
2ec9e 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29 7b 0a 20  ominor.yy72 ){. 
2ec9f 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
2eca0 79 37 32 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79  y72->x.pList = y
2eca1 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2eca2 79 31 34 38 3b 0a 20 20 20 20 73 71 6c 69 74 65  y148;.    sqlite
2eca3 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70  3ExprSetHeight(p
2eca4 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
2eca5 6f 72 2e 79 79 37 32 29 3b 0a 20 20 7d 65 6c 73  or.yy72);.  }els
2eca6 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  e{.    sqlite3Ex
2eca7 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  prListDelete(pPa
2eca8 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d  rse->db, yymsp[-
2eca9 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 29 3b  2].minor.yy148);
2ecaa 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78  .  }.  sqlite3Ex
2ecab 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e  prSpan(yygotomin
2ecac 6f 72 2e 79 79 37 32 2c 20 26 79 79 6d 73 70 5b  or.yy72, &yymsp[
2ecad 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  -4].minor.yy0, &
2ecae 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ecaf 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
2ecb0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ecb1 20 32 33 33 3a 20 2f 2a 20 63 61 73 65 5f 65 78   233: /* case_ex
2ecb2 70 72 6c 69 73 74 20 3a 3a 3d 20 63 61 73 65 5f  prlist ::= case_
2ecb3 65 78 70 72 6c 69 73 74 20 57 48 45 4e 20 65 78  exprlist WHEN ex
2ecb4 70 72 20 54 48 45 4e 20 65 78 70 72 20 2a 2f 0a  pr THEN expr */.
2ecb5 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2ecb6 79 79 31 34 38 20 3d 20 73 71 6c 69 74 65 33 45  yy148 = sqlite3E
2ecb7 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2ecb8 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d  arse,yymsp[-4].m
2ecb9 69 6e 6f 72 2e 79 79 31 34 38 2c 20 79 79 6d 73  inor.yy148, yyms
2ecba 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  p[-2].minor.yy72
2ecbb 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  , 0);.  yygotomi
2ecbc 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69  nor.yy148 = sqli
2ecbd 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2ecbe 64 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d  d(pParse,yygotom
2ecbf 69 6e 6f 72 2e 79 79 31 34 38 2c 20 79 79 6d 73  inor.yy148, yyms
2ecc0 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c  p[0].minor.yy72,
2ecc1 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62   0);.}.        b
2ecc2 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ecc3 20 32 33 34 3a 20 2f 2a 20 63 61 73 65 5f 65 78   234: /* case_ex
2ecc4 70 72 6c 69 73 74 20 3a 3a 3d 20 57 48 45 4e 20  prlist ::= WHEN 
2ecc5 65 78 70 72 20 54 48 45 4e 20 65 78 70 72 20 2a  expr THEN expr *
2ecc6 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2ecc7 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69 74 65  r.yy148 = sqlite
2ecc8 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2ecc9 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b  pParse,0, yymsp[
2ecca 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20  -2].minor.yy72, 
2eccb 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
2eccc 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69 74 65  r.yy148 = sqlite
2eccd 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2ecce 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e  pParse,yygotomin
2eccf 6f 72 2e 79 79 31 34 38 2c 20 79 79 6d 73 70 5b  or.yy148, yymsp[
2ecd0 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30  0].minor.yy72, 0
2ecd1 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2ecd2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2ecd3 34 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63  43: /* cmd ::= c
2ecd4 72 65 61 74 65 6b 77 20 75 6e 69 71 75 65 66 6c  reatekw uniquefl
2ecd5 61 67 20 49 4e 44 45 58 20 69 66 6e 6f 74 65 78  ag INDEX ifnotex
2ecd6 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e 20  ists nm dbnm ON 
2ecd7 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50  nm LP idxlist RP
2ecd8 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 43   */.{.  sqlite3C
2ecd9 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72 73  reateIndex(pPars
2ecda 65 2c 20 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  e, &yymsp[-6].mi
2ecdb 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
2ecdc 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 0a  -5].minor.yy0, .
2ecdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ecde 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c       sqlite3SrcL
2ecdf 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2ece0 2d 3e 64 62 2c 30 2c 26 79 79 6d 73 70 5b 2d 33  ->db,0,&yymsp[-3
2ece1 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 2c 20  ].minor.yy0,0), 
2ece2 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2ece3 79 79 31 34 38 2c 20 79 79 6d 73 70 5b 2d 39 5d  yy148, yymsp[-9]
2ece4 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 0a 20 20  .minor.yy194,.  
2ece5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ece6 20 20 20 20 26 79 79 6d 73 70 5b 2d 31 30 5d 2e      &yymsp[-10].
2ece7 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
2ece8 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  p[0].minor.yy0, 
2ece9 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 2c 20 79  SQLITE_SO_ASC, y
2ecea 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
2eceb 79 31 39 34 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y194);.}.       
2ecec 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2eced 73 65 20 32 34 34 3a 20 2f 2a 20 75 6e 69 71 75  se 244: /* uniqu
2ecee 65 66 6c 61 67 20 3a 3a 3d 20 55 4e 49 51 55 45  eflag ::= UNIQUE
2ecef 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2ecf0 39 33 3a 20 2f 2a 20 72 61 69 73 65 74 79 70 65  93: /* raisetype
2ecf1 20 3a 3a 3d 20 41 42 4f 52 54 20 2a 2f 0a 7b 79   ::= ABORT */.{y
2ecf2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34  ygotominor.yy194
2ecf3 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 7d 0a 20 20   = OE_Abort;}.  
2ecf4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ecf5 20 20 20 63 61 73 65 20 32 34 35 3a 20 2f 2a 20     case 245: /* 
2ecf6 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 2a  uniqueflag ::= *
2ecf7 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2ecf8 79 31 39 34 20 3d 20 4f 45 5f 4e 6f 6e 65 3b 7d  y194 = OE_None;}
2ecf9 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ecfa 20 20 20 20 20 20 63 61 73 65 20 32 34 38 3a 20        case 248: 
2ecfb 2f 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20 69  /* idxlist ::= i
2ecfc 64 78 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20  dxlist COMMA nm 
2ecfd 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65  collate sortorde
2ecfe 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70  r */.{.  Expr *p
2ecff 20 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d 73   = 0;.  if( yyms
2ed00 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-1].minor.yy0.
2ed01 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20 73  n>0 ){.    p = s
2ed02 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2ed03 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30  se, TK_COLUMN, 0
2ed04 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
2ed05 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28  ite3ExprSetColl(
2ed06 70 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d 73  pParse, p, &yyms
2ed07 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
2ed08 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69  ;.  }.  yygotomi
2ed09 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69  nor.yy148 = sqli
2ed0a 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2ed0b 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
2ed0c 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20  4].minor.yy148, 
2ed0d 70 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  p, &yymsp[-2].mi
2ed0e 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69  nor.yy0);.  sqli
2ed0f 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b  te3ExprListCheck
2ed10 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20 79  Length(pParse, y
2ed11 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38  ygotominor.yy148
2ed12 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20 20 69 66  , "index");.  if
2ed13 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2ed14 31 34 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  148 ) yygotomino
2ed15 72 2e 79 79 31 34 38 2d 3e 61 5b 79 79 67 6f 74  r.yy148->a[yygot
2ed16 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 2d 3e 6e 45  ominor.yy148->nE
2ed17 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72  xpr-1].sortOrder
2ed18 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e   = (u8)yymsp[0].
2ed19 6d 69 6e 6f 72 2e 79 79 31 39 34 3b 0a 7d 0a 20  minor.yy194;.}. 
2ed1a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ed1b 20 20 20 20 63 61 73 65 20 32 34 39 3a 20 2f 2a      case 249: /*
2ed1c 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20   idxlist ::= nm 
2ed1d 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65  collate sortorde
2ed1e 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70  r */.{.  Expr *p
2ed1f 20 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d 73   = 0;.  if( yyms
2ed20 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-1].minor.yy0.
2ed21 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20 73  n>0 ){.    p = s
2ed22 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2ed23 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30  se, TK_COLUMN, 0
2ed24 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
2ed25 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28  ite3ExprSetColl(
2ed26 70 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d 73  pParse, p, &yyms
2ed27 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
2ed28 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69  ;.  }.  yygotomi
2ed29 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69  nor.yy148 = sqli
2ed2a 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2ed2b 64 28 70 50 61 72 73 65 2c 30 2c 20 70 2c 20 26  d(pParse,0, p, &
2ed2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2ed2d 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  yy0);.  sqlite3E
2ed2e 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67  xprListCheckLeng
2ed2f 74 68 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  th(pParse, yygot
2ed30 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20 22 69  ominor.yy148, "i
2ed31 6e 64 65 78 22 29 3b 0a 20 20 69 66 28 20 79 79  ndex");.  if( yy
2ed32 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20  gotominor.yy148 
2ed33 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
2ed34 31 34 38 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e  148->a[yygotomin
2ed35 6f 72 2e 79 79 31 34 38 2d 3e 6e 45 78 70 72 2d  or.yy148->nExpr-
2ed36 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28  1].sortOrder = (
2ed37 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  u8)yymsp[0].mino
2ed38 72 2e 79 79 31 39 34 3b 0a 7d 0a 20 20 20 20 20  r.yy194;.}.     
2ed39 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ed3a 63 61 73 65 20 32 35 30 3a 20 2f 2a 20 63 6f 6c  case 250: /* col
2ed3b 6c 61 74 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  late ::= */.{yyg
2ed3c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d  otominor.yy0.z =
2ed3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   0; yygotominor.
2ed3e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20  yy0.n = 0;}.    
2ed3f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ed40 20 63 61 73 65 20 32 35 32 3a 20 2f 2a 20 63 6d   case 252: /* cm
2ed41 64 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58  d ::= DROP INDEX
2ed42 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
2ed43 6d 65 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 44 72  me */.{sqlite3Dr
2ed44 6f 70 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  opIndex(pParse, 
2ed45 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ed46 79 31 38 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  y185, yymsp[-1].
2ed47 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a 20  minor.yy194);}. 
2ed48 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ed49 20 20 20 20 63 61 73 65 20 32 35 33 3a 20 2f 2a      case 253: /*
2ed4a 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20   cmd ::= VACUUM 
2ed4b 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  */.      case 25
2ed4c 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41  4: /* cmd ::= VA
2ed4d 43 55 55 4d 20 6e 6d 20 2a 2f 0a 7b 73 71 6c 69  CUUM nm */.{sqli
2ed4e 74 65 33 56 61 63 75 75 6d 28 70 50 61 72 73 65  te3Vacuum(pParse
2ed4f 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2ed50 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  k;.      case 25
2ed51 35 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52  5: /* cmd ::= PR
2ed52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a  AGMA nm dbnm */.
2ed53 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70  {sqlite3Pragma(p
2ed54 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d  Parse,&yymsp[-1]
2ed55 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2ed56 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30  p[0].minor.yy0,0
2ed57 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ,0);}.        br
2ed58 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ed59 32 35 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  256: /* cmd ::= 
2ed5a 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45  PRAGMA nm dbnm E
2ed5b 51 20 6e 6d 6e 75 6d 20 2a 2f 0a 7b 73 71 6c 69  Q nmnum */.{sqli
2ed5c 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65  te3Pragma(pParse
2ed5d 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
2ed5e 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d  r.yy0,&yymsp[-2]
2ed5f 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2ed60 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30  p[0].minor.yy0,0
2ed61 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2ed62 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  k;.      case 25
2ed63 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52  7: /* cmd ::= PR
2ed64 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20  AGMA nm dbnm LP 
2ed65 6e 6d 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73 71 6c  nmnum RP */.{sql
2ed66 69 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73  ite3Pragma(pPars
2ed67 65 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  e,&yymsp[-4].min
2ed68 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33  or.yy0,&yymsp[-3
2ed69 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2ed6a 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2ed6b 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ,0);}.        br
2ed6c 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ed6d 32 35 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  258: /* cmd ::= 
2ed6e 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45  PRAGMA nm dbnm E
2ed6f 51 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f 0a 7b  Q minus_num */.{
2ed70 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50  sqlite3Pragma(pP
2ed71 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e  arse,&yymsp[-3].
2ed72 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2ed73 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-2].minor.yy0,&
2ed74 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ed75 79 30 2c 31 29 3b 7d 0a 20 20 20 20 20 20 20 20  y0,1);}.        
2ed76 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2ed77 65 20 32 35 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 259: /* cmd ::
2ed78 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
2ed79 20 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d 20 52 50   LP minus_num RP
2ed7a 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67   */.{sqlite3Prag
2ed7b 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  ma(pParse,&yymsp
2ed7c 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-4].minor.yy0,&
2ed7d 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2ed7e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  yy0,&yymsp[-1].m
2ed7f 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 7d 0a 20 20  inor.yy0,1);}.  
2ed80 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ed81 20 20 20 63 61 73 65 20 32 37 30 3a 20 2f 2a 20     case 270: /* 
2ed82 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77  cmd ::= createkw
2ed83 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20 42 45   trigger_decl BE
2ed84 47 49 4e 20 74 72 69 67 67 65 72 5f 63 6d 64 5f  GIN trigger_cmd_
2ed85 6c 69 73 74 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20  list END */.{.  
2ed86 54 6f 6b 65 6e 20 61 6c 6c 3b 0a 20 20 61 6c 6c  Token all;.  all
2ed87 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  .z = yymsp[-3].m
2ed88 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 61 6c  inor.yy0.z;.  al
2ed89 6c 2e 6e 20 3d 20 28 69 6e 74 29 28 79 79 6d 73  l.n = (int)(yyms
2ed8a 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
2ed8b 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   - yymsp[-3].min
2ed8c 6f 72 2e 79 79 30 2e 7a 29 20 2b 20 79 79 6d 73  or.yy0.z) + yyms
2ed8d 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
2ed8e 3b 0a 20 20 73 71 6c 69 74 65 33 46 69 6e 69 73  ;.  sqlite3Finis
2ed8f 68 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  hTrigger(pParse,
2ed90 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2ed91 2e 79 79 31 34 35 2c 20 26 61 6c 6c 29 3b 0a 7d  .yy145, &all);.}
2ed92 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ed93 20 20 20 20 20 20 63 61 73 65 20 32 37 31 3a 20        case 271: 
2ed94 2f 2a 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20  /* trigger_decl 
2ed95 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47 45 52  ::= temp TRIGGER
2ed96 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2ed97 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74 69 6d  dbnm trigger_tim
2ed98 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  e trigger_event 
2ed99 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65  ON fullname fore
2ed9a 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f  ach_clause when_
2ed9b 63 6c 61 75 73 65 20 2a 2f 0a 7b 0a 20 20 73 71  clause */.{.  sq
2ed9c 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65  lite3BeginTrigge
2ed9d 72 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70  r(pParse, &yymsp
2ed9e 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-7].minor.yy0, 
2ed9f 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  &yymsp[-6].minor
2eda0 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e  .yy0, yymsp[-5].
2eda1 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 20 79 79 6d  minor.yy194, yym
2eda2 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
2eda3 33 32 2e 61 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  32.a, yymsp[-4].
2eda4 6d 69 6e 6f 72 2e 79 79 33 33 32 2e 62 2c 20 79  minor.yy332.b, y
2eda5 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2eda6 79 31 38 35 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  y185, yymsp[0].m
2eda7 69 6e 6f 72 2e 79 79 37 32 2c 20 79 79 6d 73 70  inor.yy72, yymsp
2eda8 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39  [-10].minor.yy19
2eda9 34 2c 20 79 79 6d 73 70 5b 2d 38 5d 2e 6d 69 6e  4, yymsp[-8].min
2edaa 6f 72 2e 79 79 31 39 34 29 3b 0a 20 20 79 79 67  or.yy194);.  yyg
2edab 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 28  otominor.yy0 = (
2edac 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
2edad 79 79 30 2e 6e 3d 3d 30 3f 79 79 6d 73 70 5b 2d  yy0.n==0?yymsp[-
2edae 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a 79 79 6d  7].minor.yy0:yym
2edaf 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-6].minor.yy0
2edb0 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2edb1 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2edb2 37 32 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74  72: /* trigger_t
2edb3 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 20 2a  ime ::= BEFORE *
2edb4 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 35  /.      case 275
2edb5 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d  : /* trigger_tim
2edb6 65 20 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74  e ::= */.{ yygot
2edb7 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 54  ominor.yy194 = T
2edb8 4b 5f 42 45 46 4f 52 45 3b 20 7d 0a 20 20 20 20  K_BEFORE; }.    
2edb9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2edba 20 63 61 73 65 20 32 37 33 3a 20 2f 2a 20 74 72   case 273: /* tr
2edbb 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41  igger_time ::= A
2edbc 46 54 45 52 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  FTER */.{ yygoto
2edbd 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 54 4b  minor.yy194 = TK
2edbe 5f 41 46 54 45 52 3b 20 20 7d 0a 20 20 20 20 20  _AFTER;  }.     
2edbf 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2edc0 63 61 73 65 20 32 37 34 3a 20 2f 2a 20 74 72 69  case 274: /* tri
2edc1 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e  gger_time ::= IN
2edc2 53 54 45 41 44 20 4f 46 20 2a 2f 0a 7b 20 79 79  STEAD OF */.{ yy
2edc3 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20  gotominor.yy194 
2edc4 3d 20 54 4b 5f 49 4e 53 54 45 41 44 3b 7d 0a 20  = TK_INSTEAD;}. 
2edc5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2edc6 20 20 20 20 63 61 73 65 20 32 37 36 3a 20 2f 2a      case 276: /*
2edc7 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a   trigger_event :
2edc8 3a 3d 20 44 45 4c 45 54 45 7c 49 4e 53 45 52 54  := DELETE|INSERT
2edc9 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2edca 37 37 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65  77: /* trigger_e
2edcb 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20  vent ::= UPDATE 
2edcc 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2edcd 79 79 33 33 32 2e 61 20 3d 20 79 79 6d 73 70 5b  yy332.a = yymsp[
2edce 30 5d 2e 6d 61 6a 6f 72 3b 20 79 79 67 6f 74 6f  0].major; yygoto
2edcf 6d 69 6e 6f 72 2e 79 79 33 33 32 2e 62 20 3d 20  minor.yy332.b = 
2edd0 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2edd1 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
2edd2 38 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76  8: /* trigger_ev
2edd3 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f  ent ::= UPDATE O
2edd4 46 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a  F inscollist */.
2edd5 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
2edd6 33 32 2e 61 20 3d 20 54 4b 5f 55 50 44 41 54 45  32.a = TK_UPDATE
2edd7 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
2edd8 33 33 32 2e 62 20 3d 20 79 79 6d 73 70 5b 30 5d  332.b = yymsp[0]
2edd9 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 3b 7d 0a 20  .minor.yy254;}. 
2edda 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2eddb 20 20 20 20 63 61 73 65 20 32 38 31 3a 20 2f 2a      case 281: /*
2eddc 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d   when_clause ::=
2eddd 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2edde 39 38 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a  98: /* key_opt :
2eddf 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  := */.{ yygotomi
2ede0 6e 6f 72 2e 79 79 37 32 20 3d 20 30 3b 20 7d 0a  nor.yy72 = 0; }.
2ede1 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2ede2 20 20 20 20 20 63 61 73 65 20 32 38 32 3a 20 2f       case 282: /
2ede3 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a  * when_clause ::
2ede4 3d 20 57 48 45 4e 20 65 78 70 72 20 2a 2f 0a 20  = WHEN expr */. 
2ede5 20 20 20 20 20 63 61 73 65 20 32 39 39 3a 20 2f       case 299: /
2ede6 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45  * key_opt ::= KE
2ede7 59 20 65 78 70 72 20 2a 2f 0a 7b 20 79 79 67 6f  Y expr */.{ yygo
2ede8 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 79  tominor.yy72 = y
2ede9 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2edea 37 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  72; }.        br
2edeb 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2edec 32 38 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  283: /* trigger_
2eded 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69  cmd_list ::= tri
2edee 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72  gger_cmd_list tr
2edef 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a  igger_cmd SEMI *
2edf0 2f 0a 7b 0a 2f 2a 0a 20 20 69 66 28 20 79 79 6d  /.{./*.  if( yym
2edf1 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
2edf2 34 35 20 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b  45 ){.    yymsp[
2edf3 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 2d  -2].minor.yy145-
2edf4 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20  >pLast->pNext = 
2edf5 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2edf6 79 79 31 34 35 3b 0a 20 20 7d 65 6c 73 65 7b 0a  yy145;.  }else{.
2edf7 20 20 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69      yymsp[-2].mi
2edf8 6e 6f 72 2e 79 79 31 34 35 20 3d 20 79 79 6d 73  nor.yy145 = yyms
2edf9 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2edfa 35 3b 0a 20 20 7d 0a 2a 2f 0a 20 20 61 73 73 65  5;.  }.*/.  asse
2edfb 72 74 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  rt( yymsp[-2].mi
2edfc 6e 6f 72 2e 79 79 31 34 35 21 3d 30 20 29 3b 0a  nor.yy145!=0 );.
2edfd 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f    yymsp[-2].mino
2edfe 72 2e 79 79 31 34 35 2d 3e 70 4c 61 73 74 2d 3e  r.yy145->pLast->
2edff 70 4e 65 78 74 20 3d 20 79 79 6d 73 70 5b 2d 31  pNext = yymsp[-1
2ee00 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 3b 0a 20  ].minor.yy145;. 
2ee01 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
2ee02 2e 79 79 31 34 35 2d 3e 70 4c 61 73 74 20 3d 20  .yy145->pLast = 
2ee03 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2ee04 79 79 31 34 35 3b 0a 20 20 79 79 67 6f 74 6f 6d  yy145;.  yygotom
2ee05 69 6e 6f 72 2e 79 79 31 34 35 20 3d 20 79 79 6d  inor.yy145 = yym
2ee06 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
2ee07 34 35 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  45;.}.        br
2ee08 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ee09 32 38 34 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  284: /* trigger_
2ee0a 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69  cmd_list ::= tri
2ee0b 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a 2f  gger_cmd SEMI */
2ee0c 0a 7b 20 0a 20 20 2f 2a 20 69 66 28 20 79 79 6d  .{ .  /* if( yym
2ee0d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2ee0e 34 35 20 29 20 2a 2f 0a 20 20 61 73 73 65 72 74  45 ) */.  assert
2ee0f 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ( yymsp[-1].mino
2ee10 72 2e 79 79 31 34 35 21 3d 30 20 29 3b 0a 20 20  r.yy145!=0 );.  
2ee11 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2ee12 79 79 31 34 35 2d 3e 70 4c 61 73 74 20 3d 20 79  yy145->pLast = y
2ee13 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ee14 79 31 34 35 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y145;.  yygotomi
2ee15 6e 6f 72 2e 79 79 31 34 35 20 3d 20 79 79 6d 73  nor.yy145 = yyms
2ee16 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2ee17 35 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  5;.}.        bre
2ee18 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2ee19 38 35 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63  85: /* trigger_c
2ee1a 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72  md ::= UPDATE or
2ee1b 63 6f 6e 66 20 6e 6d 20 53 45 54 20 73 65 74 6c  conf nm SET setl
2ee1c 69 73 74 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f  ist where_opt */
2ee1d 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
2ee1e 79 31 34 35 20 3d 20 73 71 6c 69 74 65 33 54 72  y145 = sqlite3Tr
2ee1f 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70 28  iggerUpdateStep(
2ee20 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d  pParse->db, &yym
2ee21 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
2ee22 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2ee23 72 2e 79 79 31 34 38 2c 20 79 79 6d 73 70 5b 30  r.yy148, yymsp[0
2ee24 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 79 79  ].minor.yy72, yy
2ee25 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2ee26 31 39 34 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  194); }.        
2ee27 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2ee28 65 20 32 38 36 3a 20 2f 2a 20 74 72 69 67 67 65  e 286: /* trigge
2ee29 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74  r_cmd ::= insert
2ee2a 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73  _cmd INTO nm ins
2ee2b 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55  collist_opt VALU
2ee2c 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52  ES LP itemlist R
2ee2d 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  P */.{yygotomino
2ee2e 72 2e 79 79 31 34 35 20 3d 20 73 71 6c 69 74 65  r.yy145 = sqlite
2ee2f 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74  3TriggerInsertSt
2ee30 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26  ep(pParse->db, &
2ee31 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
2ee32 79 79 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  yy0, yymsp[-4].m
2ee33 69 6e 6f 72 2e 79 79 32 35 34 2c 20 79 79 6d 73  inor.yy254, yyms
2ee34 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2ee35 38 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e  8, 0, yymsp[-7].
2ee36 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a 20  minor.yy194);}. 
2ee37 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ee38 20 20 20 20 63 61 73 65 20 32 38 37 3a 20 2f 2a      case 287: /*
2ee39 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
2ee3a 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
2ee3b 20 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f   nm inscollist_o
2ee3c 70 74 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 79 79  pt select */.{yy
2ee3d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 35 20  gotominor.yy145 
2ee3e 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
2ee3f 49 6e 73 65 72 74 53 74 65 70 28 70 50 61 72 73  InsertStep(pPars
2ee40 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 32  e->db, &yymsp[-2
2ee41 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d  ].minor.yy0, yym
2ee42 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-1].minor.yy2
2ee43 35 34 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e  54, 0, yymsp[0].
2ee44 6d 69 6e 6f 72 2e 79 79 32 34 33 2c 20 79 79 6d  minor.yy243, yym
2ee45 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
2ee46 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  94);}.        br
2ee47 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ee48 32 38 38 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  288: /* trigger_
2ee49 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46  cmd ::= DELETE F
2ee4a 52 4f 4d 20 6e 6d 20 77 68 65 72 65 5f 6f 70 74  ROM nm where_opt
2ee4b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2ee4c 2e 79 79 31 34 35 20 3d 20 73 71 6c 69 74 65 33  .yy145 = sqlite3
2ee4d 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65  TriggerDeleteSte
2ee4e 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79  p(pParse->db, &y
2ee4f 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ee50 79 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0, yymsp[0].min
2ee51 6f 72 2e 79 79 37 32 29 3b 7d 0a 20 20 20 20 20  or.yy72);}.     
2ee52 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ee53 63 61 73 65 20 32 38 39 3a 20 2f 2a 20 74 72 69  case 289: /* tri
2ee54 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c  gger_cmd ::= sel
2ee55 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ect */.{yygotomi
2ee56 6e 6f 72 2e 79 79 31 34 35 20 3d 20 73 71 6c 69  nor.yy145 = sqli
2ee57 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74  te3TriggerSelect
2ee58 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
2ee59 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2ee5a 79 79 32 34 33 29 3b 20 7d 0a 20 20 20 20 20 20  yy243); }.      
2ee5b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2ee5c 61 73 65 20 32 39 30 3a 20 2f 2a 20 65 78 70 72  ase 290: /* expr
2ee5d 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 49 47   ::= RAISE LP IG
2ee5e 4e 4f 52 45 20 52 50 20 2a 2f 0a 7b 0a 20 20 79  NORE RP */.{.  y
2ee5f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2ee60 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2ee61 50 61 72 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c  Parse, TK_RAISE,
2ee62 20 30 2c 20 30 2c 20 30 29 3b 20 0a 20 20 69 66   0, 0, 0); .  if
2ee63 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2ee64 37 32 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f  72 ){.    yygoto
2ee65 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 61 66 66 69  minor.yy72->affi
2ee66 6e 69 74 79 20 3d 20 4f 45 5f 49 67 6e 6f 72 65  nity = OE_Ignore
2ee67 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2ee68 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2ee69 72 2e 79 79 37 32 2c 20 26 79 79 6d 73 70 5b 2d  r.yy72, &yymsp[-
2ee6a 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  3].minor.yy0, &y
2ee6b 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2ee6c 30 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20  0);.  }.}.      
2ee6d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2ee6e 61 73 65 20 32 39 31 3a 20 2f 2a 20 65 78 70 72  ase 291: /* expr
2ee6f 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61   ::= RAISE LP ra
2ee70 69 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d  isetype COMMA nm
2ee71 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
2ee72 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71  ominor.yy72 = sq
2ee73 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ee74 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c 20  e, TK_RAISE, 0, 
2ee75 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0, &yymsp[-1].mi
2ee76 6e 6f 72 2e 79 79 30 29 3b 20 0a 20 20 69 66 28  nor.yy0); .  if(
2ee77 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2ee78 32 20 29 20 7b 0a 20 20 20 20 79 79 67 6f 74 6f  2 ) {.    yygoto
2ee79 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 61 66 66 69  minor.yy72->affi
2ee7a 6e 69 74 79 20 3d 20 28 63 68 61 72 29 79 79 6d  nity = (char)yym
2ee7b 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
2ee7c 39 34 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  94;.    sqlite3E
2ee7d 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2ee7e 6e 6f 72 2e 79 79 37 32 2c 20 26 79 79 6d 73 70  nor.yy72, &yymsp
2ee7f 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-5].minor.yy0, 
2ee80 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2ee81 79 79 30 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20  yy0);.  }.}.    
2ee82 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ee83 20 63 61 73 65 20 32 39 32 3a 20 2f 2a 20 72 61   case 292: /* ra
2ee84 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c  isetype ::= ROLL
2ee85 42 41 43 4b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  BACK */.{yygotom
2ee86 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 4f 45 5f  inor.yy194 = OE_
2ee87 52 6f 6c 6c 62 61 63 6b 3b 7d 0a 20 20 20 20 20  Rollback;}.     
2ee88 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ee89 63 61 73 65 20 32 39 34 3a 20 2f 2a 20 72 61 69  case 294: /* rai
2ee8a 73 65 74 79 70 65 20 3a 3a 3d 20 46 41 49 4c 20  setype ::= FAIL 
2ee8b 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2ee8c 79 79 31 39 34 20 3d 20 4f 45 5f 46 61 69 6c 3b  yy194 = OE_Fail;
2ee8d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ee8e 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 35 3a  .      case 295:
2ee8f 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50   /* cmd ::= DROP
2ee90 20 54 52 49 47 47 45 52 20 69 66 65 78 69 73 74   TRIGGER ifexist
2ee91 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a  s fullname */.{.
2ee92 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69    sqlite3DropTri
2ee93 67 67 65 72 28 70 50 61 72 73 65 2c 79 79 6d 73  gger(pParse,yyms
2ee94 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35  p[0].minor.yy185
2ee95 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2ee96 2e 79 79 31 39 34 29 3b 0a 7d 0a 20 20 20 20 20  .yy194);.}.     
2ee97 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ee98 63 61 73 65 20 32 39 36 3a 20 2f 2a 20 63 6d 64  case 296: /* cmd
2ee99 20 3a 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61   ::= ATTACH data
2ee9a 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72  base_kw_opt expr
2ee9b 20 41 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74   AS expr key_opt
2ee9c 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41   */.{.  sqlite3A
2ee9d 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 79 79  ttach(pParse, yy
2ee9e 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2ee9f 37 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  72, yymsp[-1].mi
2eea0 6e 6f 72 2e 79 79 37 32 2c 20 79 79 6d 73 70 5b  nor.yy72, yymsp[
2eea1 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 0a  0].minor.yy72);.
2eea2 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2eea3 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 37 3a  .      case 297:
2eea4 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41   /* cmd ::= DETA
2eea5 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  CH database_kw_o
2eea6 70 74 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73  pt expr */.{.  s
2eea7 71 6c 69 74 65 33 44 65 74 61 63 68 28 70 50 61  qlite3Detach(pPa
2eea8 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
2eea9 6e 6f 72 2e 79 79 37 32 29 3b 0a 7d 0a 20 20 20  nor.yy72);.}.   
2eeaa 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2eeab 20 20 63 61 73 65 20 33 30 32 3a 20 2f 2a 20 63    case 302: /* c
2eeac 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 2a  md ::= REINDEX *
2eead 2f 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65  /.{sqlite3Reinde
2eeae 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b  x(pParse, 0, 0);
2eeaf 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2eeb0 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 33 3a  .      case 303:
2eeb1 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e   /* cmd ::= REIN
2eeb2 44 45 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b  DEX nm dbnm */.{
2eeb3 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70  sqlite3Reindex(p
2eeb4 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31  Parse, &yymsp[-1
2eeb5 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
2eeb6 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2eeb7 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2eeb8 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
2eeb9 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4e  4: /* cmd ::= AN
2eeba 41 4c 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69 74 65  ALYZE */.{sqlite
2eebb 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c  3Analyze(pParse,
2eebc 20 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20 20   0, 0);}.       
2eebd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2eebe 73 65 20 33 30 35 3a 20 2f 2a 20 63 6d 64 20 3a  se 305: /* cmd :
2eebf 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62  := ANALYZE nm db
2eec0 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 6e  nm */.{sqlite3An
2eec1 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 26 79  alyze(pParse, &y
2eec2 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2eec3 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0, &yymsp[0].mi
2eec4 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
2eec5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2eec6 63 61 73 65 20 33 30 36 3a 20 2f 2a 20 63 6d 64  case 306: /* cmd
2eec7 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45   ::= ALTER TABLE
2eec8 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45   fullname RENAME
2eec9 20 54 4f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71   TO nm */.{.  sq
2eeca 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65  lite3AlterRename
2eecb 54 61 62 6c 65 28 70 50 61 72 73 65 2c 79 79 6d  Table(pParse,yym
2eecc 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
2eecd 38 35 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  85,&yymsp[0].min
2eece 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
2eecf 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2eed0 63 61 73 65 20 33 30 37 3a 20 2f 2a 20 63 6d 64  case 307: /* cmd
2eed1 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45   ::= ALTER TABLE
2eed2 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c   add_column_full
2eed3 6e 61 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d  name ADD kwcolum
2eed4 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  n_opt column */.
2eed5 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72  {.  sqlite3Alter
2eed6 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28  FinishAddColumn(
2eed7 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30  pParse, &yymsp[0
2eed8 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
2eed9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2eeda 20 20 20 20 20 63 61 73 65 20 33 30 38 3a 20 2f       case 308: /
2eedb 2a 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c  * add_column_ful
2eedc 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61  lname ::= fullna
2eedd 6d 65 20 2a 2f 0a 7b 0a 20 20 70 50 61 72 73 65  me */.{.  pParse
2eede 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ->db->lookaside.
2eedf 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20  bEnabled = 0;.  
2eee0 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69  sqlite3AlterBegi
2eee1 6e 41 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73  nAddColumn(pPars
2eee2 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e, yymsp[0].mino
2eee3 72 2e 79 79 31 38 35 29 3b 0a 7d 0a 20 20 20 20  r.yy185);.}.    
2eee4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2eee5 20 63 61 73 65 20 33 31 31 3a 20 2f 2a 20 63 6d   case 311: /* cm
2eee6 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61  d ::= create_vta
2eee7 62 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61  b */.{sqlite3Vta
2eee8 62 46 69 6e 69 73 68 50 61 72 73 65 28 70 50 61  bFinishParse(pPa
2eee9 72 73 65 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20  rse,0);}.       
2eeea 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2eeeb 73 65 20 33 31 32 3a 20 2f 2a 20 63 6d 64 20 3a  se 312: /* cmd :
2eeec 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62 20 4c  := create_vtab L
2eeed 50 20 76 74 61 62 61 72 67 6c 69 73 74 20 52 50  P vtabarglist RP
2eeee 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62   */.{sqlite3Vtab
2eeef 46 69 6e 69 73 68 50 61 72 73 65 28 70 50 61 72  FinishParse(pPar
2eef0 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se,&yymsp[0].min
2eef1 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
2eef2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2eef3 61 73 65 20 33 31 33 3a 20 2f 2a 20 63 72 65 61  ase 313: /* crea
2eef4 74 65 5f 76 74 61 62 20 3a 3a 3d 20 63 72 65 61  te_vtab ::= crea
2eef5 74 65 6b 77 20 56 49 52 54 55 41 4c 20 54 41 42  tekw VIRTUAL TAB
2eef6 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47  LE nm dbnm USING
2eef7 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c   nm */.{.    sql
2eef8 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 72  ite3VtabBeginPar
2eef9 73 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  se(pParse, &yyms
2eefa 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
2eefb 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
2eefc 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d  r.yy0, &yymsp[0]
2eefd 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
2eefe 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2eeff 20 20 20 20 63 61 73 65 20 33 31 36 3a 20 2f 2a      case 316: /*
2ef00 20 76 74 61 62 61 72 67 20 3a 3a 3d 20 2a 2f 0a   vtabarg ::= */.
2ef01 7b 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 49  {sqlite3VtabArgI
2ef02 6e 69 74 28 70 50 61 72 73 65 29 3b 7d 0a 20 20  nit(pParse);}.  
2ef03 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ef04 20 20 20 63 61 73 65 20 33 31 38 3a 20 2f 2a 20     case 318: /* 
2ef05 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d  vtabargtoken ::=
2ef06 20 41 4e 59 20 2a 2f 0a 20 20 20 20 20 20 63 61   ANY */.      ca
2ef07 73 65 20 33 31 39 3a 20 2f 2a 20 76 74 61 62 61  se 319: /* vtaba
2ef08 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61  rgtoken ::= lp a
2ef09 6e 79 6c 69 73 74 20 52 50 20 2a 2f 0a 20 20 20  nylist RP */.   
2ef0a 20 20 20 63 61 73 65 20 33 32 30 3a 20 2f 2a 20     case 320: /* 
2ef0b 6c 70 20 3a 3a 3d 20 4c 50 20 2a 2f 0a 20 20 20  lp ::= LP */.   
2ef0c 20 20 20 63 61 73 65 20 33 32 32 3a 20 2f 2a 20     case 322: /* 
2ef0d 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c  anylist ::= anyl
2ef0e 69 73 74 20 41 4e 59 20 2a 2f 0a 7b 73 71 6c 69  ist ANY */.{sqli
2ef0f 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64  te3VtabArgExtend
2ef10 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30  (pParse,&yymsp[0
2ef11 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
2ef12 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ef13 7d 3b 0a 20 20 79 79 67 6f 74 6f 20 3d 20 79 79  };.  yygoto = yy
2ef14 52 75 6c 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e  RuleInfo[yyrulen
2ef15 6f 5d 2e 6c 68 73 3b 0a 20 20 79 79 73 69 7a 65  o].lhs;.  yysize
2ef16 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f 5b 79 79   = yyRuleInfo[yy
2ef17 72 75 6c 65 6e 6f 5d 2e 6e 72 68 73 3b 0a 20 20  ruleno].nrhs;.  
2ef18 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
2ef19 20 2d 3d 20 79 79 73 69 7a 65 3b 0a 20 20 79 79   -= yysize;.  yy
2ef1a 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65  act = yy_find_re
2ef1b 64 75 63 65 5f 61 63 74 69 6f 6e 28 79 79 6d 73  duce_action(yyms
2ef1c 70 5b 2d 79 79 73 69 7a 65 5d 2e 73 74 61 74 65  p[-yysize].state
2ef1d 6e 6f 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79  no,(YYCODETYPE)y
2ef1e 79 67 6f 74 6f 29 3b 0a 20 20 69 66 28 20 79 79  ygoto);.  if( yy
2ef1f 61 63 74 20 3c 20 59 59 4e 53 54 41 54 45 20 29  act < YYNSTATE )
2ef20 7b 0a 23 69 66 64 65 66 20 4e 44 45 42 55 47 0a  {.#ifdef NDEBUG.
2ef21 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65      /* If we are
2ef22 20 6e 6f 74 20 64 65 62 75 67 67 69 6e 67 20 61   not debugging a
2ef23 6e 64 20 74 68 65 20 72 65 64 75 63 65 20 61 63  nd the reduce ac
2ef24 74 69 6f 6e 20 70 6f 70 70 65 64 20 61 74 20 6c  tion popped at l
2ef25 65 61 73 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20  east.    ** one 
2ef26 65 6c 65 6d 65 6e 74 20 6f 66 66 20 74 68 65 20  element off the 
2ef27 73 74 61 63 6b 2c 20 74 68 65 6e 20 77 65 20 63  stack, then we c
2ef28 61 6e 20 70 75 73 68 20 74 68 65 20 6e 65 77 20  an push the new 
2ef29 65 6c 65 6d 65 6e 74 20 62 61 63 6b 0a 20 20 20  element back.   
2ef2a 20 2a 2a 20 6f 6e 74 6f 20 74 68 65 20 73 74 61   ** onto the sta
2ef2b 63 6b 20 68 65 72 65 2c 20 61 6e 64 20 73 6b 69  ck here, and ski
2ef2c 70 20 74 68 65 20 73 74 61 63 6b 20 6f 76 65 72  p the stack over
2ef2d 66 6c 6f 77 20 74 65 73 74 20 69 6e 20 79 79 5f  flow test in yy_
2ef2e 73 68 69 66 74 28 29 2e 0a 20 20 20 20 2a 2a 20  shift()..    ** 
2ef2f 54 68 61 74 20 67 69 76 65 73 20 61 20 73 69 67  That gives a sig
2ef30 6e 69 66 69 63 61 6e 74 20 73 70 65 65 64 20 69  nificant speed i
2ef31 6d 70 72 6f 76 65 6d 65 6e 74 2e 20 2a 2f 0a 20  mprovement. */. 
2ef32 20 20 20 69 66 28 20 79 79 73 69 7a 65 20 29 7b     if( yysize ){
2ef33 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72  .      yypParser
2ef34 2d 3e 79 79 69 64 78 2b 2b 3b 0a 20 20 20 20 20  ->yyidx++;.     
2ef35 20 79 79 6d 73 70 20 2d 3d 20 79 79 73 69 7a 65   yymsp -= yysize
2ef36 2d 31 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d  -1;.      yymsp-
2ef37 3e 73 74 61 74 65 6e 6f 20 3d 20 28 59 59 41 43  >stateno = (YYAC
2ef38 54 49 4f 4e 54 59 50 45 29 79 79 61 63 74 3b 0a  TIONTYPE)yyact;.
2ef39 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 6d 61 6a        yymsp->maj
2ef3a 6f 72 20 3d 20 28 59 59 43 4f 44 45 54 59 50 45  or = (YYCODETYPE
2ef3b 29 79 79 67 6f 74 6f 3b 0a 20 20 20 20 20 20 79  )yygoto;.      y
2ef3c 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d 20 79 79  ymsp->minor = yy
2ef3d 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 7d  gotominor;.    }
2ef3e 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
2ef3f 7b 0a 20 20 20 20 20 20 79 79 5f 73 68 69 66 74  {.      yy_shift
2ef40 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74  (yypParser,yyact
2ef41 2c 79 79 67 6f 74 6f 2c 26 79 79 67 6f 74 6f 6d  ,yygoto,&yygotom
2ef42 69 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  inor);.    }.  }
2ef43 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
2ef44 28 20 79 79 61 63 74 20 3d 3d 20 59 59 4e 53 54  ( yyact == YYNST
2ef45 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 2b 20  ATE + YYNRULE + 
2ef46 31 20 29 3b 0a 20 20 20 20 79 79 5f 61 63 63 65  1 );.    yy_acce
2ef47 70 74 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  pt(yypParser);. 
2ef48 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
2ef49 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65  following code e
2ef4a 78 65 63 75 74 65 73 20 77 68 65 6e 20 74 68 65  xecutes when the
2ef4b 20 70 61 72 73 65 20 66 61 69 6c 73 0a 2a 2f 0a   parse fails.*/.
2ef4c 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 70  static void yy_p
2ef4d 61 72 73 65 5f 66 61 69 6c 65 64 28 0a 20 20 79  arse_failed(.  y
2ef4e 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2ef4f 65 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er           /* 
2ef50 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b  The parser */.){
2ef51 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
2ef52 41 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64  ARG_FETCH;.#ifnd
2ef53 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20  ef NDEBUG.  if( 
2ef54 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
2ef55 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
2ef56 63 65 46 49 4c 45 2c 22 25 73 46 61 69 6c 21 5c  ceFILE,"%sFail!\
2ef57 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
2ef58 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
2ef59 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72  while( yypParser
2ef5a 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f  ->yyidx>=0 ) yy_
2ef5b 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
2ef5c 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f  (yypParser);.  /
2ef5d 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69  * Here code is i
2ef5e 6e 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69  nserted which wi
2ef5f 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77  ll be executed w
2ef60 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a  henever the.  **
2ef61 20 70 61 72 73 65 72 20 66 61 69 6c 73 20 2a 2f   parser fails */
2ef62 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
2ef63 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
2ef64 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
2ef65 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
2ef66 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69  ra_argument vari
2ef67 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a  able */.}../*.**
2ef68 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
2ef69 6f 64 65 20 65 78 65 63 75 74 65 73 20 77 68 65  ode executes whe
2ef6a 6e 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72  n a syntax error
2ef6b 20 66 69 72 73 74 20 6f 63 63 75 72 73 2e 0a 2a   first occurs..*
2ef6c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  /.static void yy
2ef6d 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 0a 20  _syntax_error(. 
2ef6e 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61   yyParser *yypPa
2ef6f 72 73 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  rser,           
2ef70 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
2ef71 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20  .  int yymajor, 
2ef72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef73 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74    /* The major t
2ef74 79 70 65 20 6f 66 20 74 68 65 20 65 72 72 6f 72  ype of the error
2ef75 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59 4d 49   token */.  YYMI
2ef76 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 20  NORTYPE yyminor 
2ef77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ef78 65 20 6d 69 6e 6f 72 20 74 79 70 65 20 6f 66 20  e minor type of 
2ef79 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20  the error token 
2ef7a 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50  */.){.  sqlite3P
2ef7b 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a  arserARG_FETCH;.
2ef7c 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 20 28 79  #define TOKEN (y
2ef7d 79 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a 20 20 55  yminor.yy0)..  U
2ef7e 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
2ef7f 79 79 6d 61 6a 6f 72 29 3b 20 20 2f 2a 20 53 69  yymajor);  /* Si
2ef80 6c 65 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70 69  lence some compi
2ef81 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f 0a  ler warnings */.
2ef82 20 20 61 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e    assert( TOKEN.
2ef83 7a 5b 30 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20  z[0] );  /* The 
2ef84 74 6f 6b 65 6e 69 7a 65 72 20 61 6c 77 61 79 73  tokenizer always
2ef85 20 67 69 76 65 73 20 75 73 20 61 20 74 6f 6b 65   gives us a toke
2ef86 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72  n */.  sqlite3Er
2ef87 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2ef88 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e  near \"%T\": syn
2ef89 74 61 78 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b  tax error", &TOK
2ef8a 45 4e 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70  EN);.  pParse->p
2ef8b 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20  arseError = 1;. 
2ef8c 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2ef8d 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70  G_STORE; /* Supp
2ef8e 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f  ress warning abo
2ef8f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61  ut unused %extra
2ef90 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62  _argument variab
2ef91 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  le */.}../*.** T
2ef92 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
2ef93 65 78 65 63 75 74 65 64 20 77 68 65 6e 20 74 68  executed when th
2ef94 65 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73  e parser accepts
2ef95 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2ef96 79 79 5f 61 63 63 65 70 74 28 0a 20 20 79 79 50  yy_accept(.  yyP
2ef97 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
2ef98 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ef99 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20  e parser */.){. 
2ef9a 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2ef9b 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65 66  G_FETCH;.#ifndef
2ef9c 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
2ef9d 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
2ef9e 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
2ef9f 46 49 4c 45 2c 22 25 73 41 63 63 65 70 74 21 5c  FILE,"%sAccept!\
2efa0 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
2efa1 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
2efa2 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72  while( yypParser
2efa3 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f  ->yyidx>=0 ) yy_
2efa4 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
2efa5 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f  (yypParser);.  /
2efa6 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69  * Here code is i
2efa7 6e 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69  nserted which wi
2efa8 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77  ll be executed w
2efa9 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a  henever the.  **
2efaa 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73 20   parser accepts 
2efab 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
2efac 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20  erARG_STORE; /* 
2efad 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67  Suppress warning
2efae 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65   about unused %e
2efaf 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61  xtra_argument va
2efb0 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20  riable */.}../* 
2efb1 54 68 65 20 6d 61 69 6e 20 70 61 72 73 65 72 20  The main parser 
2efb2 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 54 68 65 20  program..** The 
2efb3 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
2efb4 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
2efb5 20 73 74 72 75 63 74 75 72 65 20 6f 62 74 61 69   structure obtai
2efb6 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 22 73 71 6c  ned from.** "sql
2efb7 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 22  ite3ParserAlloc"
2efb8 20 77 68 69 63 68 20 64 65 73 63 72 69 62 65 73   which describes
2efb9 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
2efba 74 65 20 6f 66 20 74 68 65 20 70 61 72 73 65 72  te of the parser
2efbb 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
2efbc 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2efbd 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  major token numb
2efbe 65 72 2e 20 20 54 68 65 20 74 68 69 72 64 20 69  er.  The third i
2efbf 73 0a 2a 2a 20 74 68 65 20 6d 69 6e 6f 72 20 74  s.** the minor t
2efc0 6f 6b 65 6e 2e 20 20 54 68 65 20 66 6f 75 72 74  oken.  The fourt
2efc1 68 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d  h optional argum
2efc2 65 6e 74 20 69 73 20 77 68 61 74 65 76 65 72 20  ent is whatever 
2efc3 74 68 65 0a 2a 2a 20 75 73 65 72 20 77 61 6e 74  the.** user want
2efc4 73 20 28 61 6e 64 20 73 70 65 63 69 66 69 65 64  s (and specified
2efc5 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 29   in the grammar)
2efc6 20 61 6e 64 20 69 73 20 61 76 61 69 6c 61 62 6c   and is availabl
2efc7 65 20 66 6f 72 0a 2a 2a 20 75 73 65 20 62 79 20  e for.** use by 
2efc8 74 68 65 20 61 63 74 69 6f 6e 20 72 6f 75 74 69  the action routi
2efc9 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74  nes..**.** Input
2efca 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  s:.** <ul>.** <l
2efcb 69 3e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  i> A pointer to 
2efcc 74 68 65 20 70 61 72 73 65 72 20 28 61 6e 20 6f  the parser (an o
2efcd 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 2e  paque structure.
2efce 29 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 61  ).** <li> The ma
2efcf 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72  jor token number
2efd0 2e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 69  ..** <li> The mi
2efd1 6e 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72  nor token number
2efd2 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 6f 70 74  ..** <li> An opt
2efd3 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20  ion argument of 
2efd4 61 20 67 72 61 6d 6d 61 72 2d 73 70 65 63 69 66  a grammar-specif
2efd5 69 65 64 20 74 79 70 65 2e 0a 2a 2a 20 3c 2f 75  ied type..** </u
2efd6 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73  l>.**.** Outputs
2efd7 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51  :.** None..*/.SQ
2efd8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2efd9 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28  d sqlite3Parser(
2efda 0a 20 20 76 6f 69 64 20 2a 79 79 70 2c 20 20 20  .  void *yyp,   
2efdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2efdc 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
2efdd 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20  .  int yymajor, 
2efde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2efdf 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b  /* The major tok
2efe0 65 6e 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 2a  en code number *
2efe1 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
2efe2 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 6d 69 6e  rTOKENTYPE yymin
2efe3 6f 72 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  or       /* The 
2efe4 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 74 6f  value for the to
2efe5 6b 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ken */.  sqlite3
2efe6 50 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20  ParserARG_PDECL 
2efe7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2efe8 20 4f 70 74 69 6f 6e 61 6c 20 25 65 78 74 72 61   Optional %extra
2efe9 5f 61 72 67 75 6d 65 6e 74 20 70 61 72 61 6d 65  _argument parame
2efea 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 59 59 4d 49  ter */.){.  YYMI
2efeb 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 75  NORTYPE yyminoru
2efec 6e 69 6f 6e 3b 0a 20 20 69 6e 74 20 79 79 61 63  nion;.  int yyac
2efed 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
2efee 20 54 68 65 20 70 61 72 73 65 72 20 61 63 74 69   The parser acti
2efef 6f 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 79 79 65  on. */.  int yye
2eff0 6e 64 6f 66 69 6e 70 75 74 3b 20 20 20 20 20 2f  ndofinput;     /
2eff1 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65  * True if we are
2eff2 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 69   at the end of i
2eff3 6e 70 75 74 20 2a 2f 0a 23 69 66 64 65 66 20 59  nput */.#ifdef Y
2eff4 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69  YERRORSYMBOL.  i
2eff5 6e 74 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20  nt yyerrorhit = 
2eff6 30 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  0;   /* True if 
2eff7 79 79 6d 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f  yymajor has invo
2eff8 6b 65 64 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ked an error */.
2eff9 23 65 6e 64 69 66 0a 20 20 79 79 50 61 72 73 65  #endif.  yyParse
2effa 72 20 2a 79 79 70 50 61 72 73 65 72 3b 20 20 2f  r *yypParser;  /
2effb 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
2effc 0a 20 20 2f 2a 20 28 72 65 29 69 6e 69 74 69 61  .  /* (re)initia
2effd 6c 69 7a 65 20 74 68 65 20 70 61 72 73 65 72 2c  lize the parser,
2effe 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f   if necessary */
2efff 0a 20 20 79 79 70 50 61 72 73 65 72 20 3d 20 28  .  yypParser = (
2f000 79 79 50 61 72 73 65 72 2a 29 79 79 70 3b 0a 20  yyParser*)yyp;. 
2f001 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
2f002 79 79 69 64 78 3c 30 20 29 7b 0a 23 69 66 20 59  yyidx<0 ){.#if Y
2f003 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20  YSTACKDEPTH<=0. 
2f004 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
2f005 2d 3e 79 79 73 74 6b 73 7a 20 3c 3d 30 20 29 7b  ->yystksz <=0 ){
2f006 0a 20 20 20 20 20 20 2f 2a 6d 65 6d 73 65 74 28  .      /*memset(
2f007 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30  &yyminorunion, 0
2f008 2c 20 73 69 7a 65 6f 66 28 79 79 6d 69 6e 6f 72  , sizeof(yyminor
2f009 75 6e 69 6f 6e 29 29 3b 2a 2f 0a 20 20 20 20 20  union));*/.     
2f00a 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 20 3d 20   yyminorunion = 
2f00b 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 20 20 20  yyzerominor;.   
2f00c 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c     yyStackOverfl
2f00d 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20 26 79  ow(yypParser, &y
2f00e 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
2f00f 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2f010 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 79 79 70  }.#endif.    yyp
2f011 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20  Parser->yyidx = 
2f012 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72  0;.    yypParser
2f013 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b  ->yyerrcnt = -1;
2f014 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  .    yypParser->
2f015 79 79 73 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65  yystack[0].state
2f016 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 79 79 70 50  no = 0;.    yypP
2f017 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30  arser->yystack[0
2f018 5d 2e 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d  ].major = 0;.  }
2f019 0a 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e  .  yyminorunion.
2f01a 79 79 30 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20  yy0 = yyminor;. 
2f01b 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20   yyendofinput = 
2f01c 28 79 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20  (yymajor==0);.  
2f01d 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
2f01e 5f 53 54 4f 52 45 3b 0a 0a 23 69 66 6e 64 65 66  _STORE;..#ifndef
2f01f 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
2f020 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
2f021 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
2f022 46 49 4c 45 2c 22 25 73 49 6e 70 75 74 20 25 73  FILE,"%sInput %s
2f023 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70  \n",yyTracePromp
2f024 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79  t,yyTokenName[yy
2f025 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e  major]);.  }.#en
2f026 64 69 66 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 79  dif..  do{.    y
2f027 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73  yact = yy_find_s
2f028 68 69 66 74 5f 61 63 74 69 6f 6e 28 79 79 70 50  hift_action(yypP
2f029 61 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50  arser,(YYCODETYP
2f02a 45 29 79 79 6d 61 6a 6f 72 29 3b 0a 20 20 20 20  E)yymajor);.    
2f02b 69 66 28 20 79 79 61 63 74 3c 59 59 4e 53 54 41  if( yyact<YYNSTA
2f02c 54 45 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  TE ){.      asse
2f02d 72 74 28 20 21 79 79 65 6e 64 6f 66 69 6e 70 75  rt( !yyendofinpu
2f02e 74 20 29 3b 20 20 2f 2a 20 49 6d 70 6f 73 73 69  t );  /* Impossi
2f02f 62 6c 65 20 74 6f 20 73 68 69 66 74 20 74 68 65  ble to shift the
2f030 20 24 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20   $ token */.    
2f031 20 20 79 79 5f 73 68 69 66 74 28 79 79 70 50 61    yy_shift(yypPa
2f032 72 73 65 72 2c 79 79 61 63 74 2c 79 79 6d 61 6a  rser,yyact,yymaj
2f033 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  or,&yyminorunion
2f034 29 3b 0a 20 20 20 20 20 20 79 79 70 50 61 72 73  );.      yypPars
2f035 65 72 2d 3e 79 79 65 72 72 63 6e 74 2d 2d 3b 0a  er->yyerrcnt--;.
2f036 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20        yymajor = 
2f037 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 7d 65  YYNOCODE;.    }e
2f038 6c 73 65 20 69 66 28 20 79 79 61 63 74 20 3c 20  lse if( yyact < 
2f039 59 59 4e 53 54 41 54 45 20 2b 20 59 59 4e 52 55  YYNSTATE + YYNRU
2f03a 4c 45 20 29 7b 0a 20 20 20 20 20 20 79 79 5f 72  LE ){.      yy_r
2f03b 65 64 75 63 65 28 79 79 70 50 61 72 73 65 72 2c  educe(yypParser,
2f03c 79 79 61 63 74 2d 59 59 4e 53 54 41 54 45 29 3b  yyact-YYNSTATE);
2f03d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2f03e 20 20 61 73 73 65 72 74 28 20 79 79 61 63 74 20    assert( yyact 
2f03f 3d 3d 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49  == YY_ERROR_ACTI
2f040 4f 4e 20 29 3b 0a 23 69 66 64 65 66 20 59 59 45  ON );.#ifdef YYE
2f041 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20  RRORSYMBOL.     
2f042 20 69 6e 74 20 79 79 6d 78 3b 0a 23 65 6e 64 69   int yymx;.#endi
2f043 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  f.#ifndef NDEBUG
2f044 0a 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61  .      if( yyTra
2f045 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20  ceFILE ){.      
2f046 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
2f047 65 46 49 4c 45 2c 22 25 73 53 79 6e 74 61 78 20  eFILE,"%sSyntax 
2f048 45 72 72 6f 72 21 5c 6e 22 2c 79 79 54 72 61 63  Error!\n",yyTrac
2f049 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 20 20  ePrompt);.      
2f04a 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  }.#endif.#ifdef 
2f04b 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20  YYERRORSYMBOL.  
2f04c 20 20 20 20 2f 2a 20 41 20 73 79 6e 74 61 78 20      /* A syntax 
2f04d 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
2f04e 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65  ed..      ** The
2f04f 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 6e 20   response to an 
2f050 65 72 72 6f 72 20 64 65 70 65 6e 64 73 20 75 70  error depends up
2f051 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  on whether or no
2f052 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 67  t the.      ** g
2f053 72 61 6d 6d 61 72 20 64 65 66 69 6e 65 73 20 61  rammar defines a
2f054 6e 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 22 45  n error token "E
2f055 52 52 4f 52 22 2e 20 20 0a 20 20 20 20 20 20 2a  RROR".  .      *
2f056 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20  *.      ** This 
2f057 69 73 20 77 68 61 74 20 77 65 20 64 6f 20 69 66  is what we do if
2f058 20 74 68 65 20 67 72 61 6d 6d 61 72 20 64 6f 65   the grammar doe
2f059 73 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a  s define ERROR:.
2f05a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2f05b 2a 20 20 2a 20 43 61 6c 6c 20 74 68 65 20 25 73  *  * Call the %s
2f05c 79 6e 74 61 78 5f 65 72 72 6f 72 20 66 75 6e 63  yntax_error func
2f05d 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  tion..      **. 
2f05e 20 20 20 20 20 2a 2a 20 20 2a 20 42 65 67 69 6e       **  * Begin
2f05f 20 70 6f 70 70 69 6e 67 20 74 68 65 20 73 74 61   popping the sta
2f060 63 6b 20 75 6e 74 69 6c 20 77 65 20 65 6e 74 65  ck until we ente
2f061 72 20 61 20 73 74 61 74 65 20 77 68 65 72 65 0a  r a state where.
2f062 20 20 20 20 20 20 2a 2a 20 20 20 20 69 74 20 69        **    it i
2f063 73 20 6c 65 67 61 6c 20 74 6f 20 73 68 69 66 74  s legal to shift
2f064 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f   the error symbo
2f065 6c 2c 20 74 68 65 6e 20 73 68 69 66 74 0a 20 20  l, then shift.  
2f066 20 20 20 20 2a 2a 20 20 20 20 74 68 65 20 65 72      **    the er
2f067 72 6f 72 20 73 79 6d 62 6f 6c 2e 0a 20 20 20 20  ror symbol..    
2f068 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a    **.      **  *
2f069 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20 63   Set the error c
2f06a 6f 75 6e 74 20 74 6f 20 74 68 72 65 65 2e 0a 20  ount to three.. 
2f06b 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
2f06c 20 20 2a 20 42 65 67 69 6e 20 61 63 63 65 70 74    * Begin accept
2f06d 69 6e 67 20 61 6e 64 20 73 68 69 66 74 69 6e 67  ing and shifting
2f06e 20 6e 65 77 20 74 6f 6b 65 6e 73 2e 20 20 4e 6f   new tokens.  No
2f06f 20 6e 65 77 20 65 72 72 6f 72 0a 20 20 20 20 20   new error.     
2f070 20 2a 2a 20 20 20 20 70 72 6f 63 65 73 73 69 6e   **    processin
2f071 67 20 77 69 6c 6c 20 6f 63 63 75 72 20 75 6e 74  g will occur unt
2f072 69 6c 20 74 68 72 65 65 20 74 6f 6b 65 6e 73 20  il three tokens 
2f073 68 61 76 65 20 62 65 65 6e 0a 20 20 20 20 20 20  have been.      
2f074 2a 2a 20 20 20 20 73 68 69 66 74 65 64 20 73 75  **    shifted su
2f075 63 63 65 73 73 66 75 6c 6c 79 2e 0a 20 20 20 20  ccessfully..    
2f076 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2f 0a 20 20    **.      */.  
2f077 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65      if( yypParse
2f078 72 2d 3e 79 79 65 72 72 63 6e 74 3c 30 20 29 7b  r->yyerrcnt<0 ){
2f079 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e 74  .        yy_synt
2f07a 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73  ax_error(yypPars
2f07b 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e  er,yymajor,yymin
2f07c 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20  orunion);.      
2f07d 7d 0a 20 20 20 20 20 20 79 79 6d 78 20 3d 20 79  }.      yymx = y
2f07e 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63  ypParser->yystac
2f07f 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  k[yypParser->yyi
2f080 64 78 5d 2e 6d 61 6a 6f 72 3b 0a 20 20 20 20 20  dx].major;.     
2f081 20 69 66 28 20 79 79 6d 78 3d 3d 59 59 45 52 52   if( yymx==YYERR
2f082 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20 79 79 65 72  ORSYMBOL || yyer
2f083 72 6f 72 68 69 74 20 29 7b 0a 23 69 66 6e 64 65  rorhit ){.#ifnde
2f084 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20  f NDEBUG.       
2f085 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
2f086 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70   ){.          fp
2f087 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2f088 45 2c 22 25 73 44 69 73 63 61 72 64 20 69 6e 70  E,"%sDiscard inp
2f089 75 74 20 74 6f 6b 65 6e 20 25 73 5c 6e 22 2c 0a  ut token %s\n",.
2f08a 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79 54               yyT
2f08b 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54 6f 6b  racePrompt,yyTok
2f08c 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29  enName[yymajor])
2f08d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64  ;.        }.#end
2f08e 69 66 0a 20 20 20 20 20 20 20 20 79 79 5f 64 65  if.        yy_de
2f08f 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72 73  structor(yypPars
2f090 65 72 2c 20 28 59 59 43 4f 44 45 54 59 50 45 29  er, (YYCODETYPE)
2f091 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
2f092 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 20 20  union);.        
2f093 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f  yymajor = YYNOCO
2f094 44 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  DE;.      }else{
2f095 0a 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  .         while(
2f096 0a 20 20 20 20 20 20 20 20 20 20 79 79 70 50 61  .          yypPa
2f097 72 73 65 72 2d 3e 79 79 69 64 78 20 3e 3d 20 30  rser->yyidx >= 0
2f098 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 79 79   &&.          yy
2f099 6d 78 20 21 3d 20 59 59 45 52 52 4f 52 53 59 4d  mx != YYERRORSYM
2f09a 42 4f 4c 20 26 26 0a 20 20 20 20 20 20 20 20 20  BOL &&.         
2f09b 20 28 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e   (yyact = yy_fin
2f09c 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28  d_reduce_action(
2f09d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f09e 20 20 20 20 20 20 20 20 20 79 79 70 50 61 72 73           yypPars
2f09f 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50  er->yystack[yypP
2f0a0 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74  arser->yyidx].st
2f0a1 61 74 65 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20  ateno,.         
2f0a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 59                 Y
2f0a3 59 45 52 52 4f 52 53 59 4d 42 4f 4c 29 29 20 3e  YERRORSYMBOL)) >
2f0a4 3d 20 59 59 4e 53 54 41 54 45 0a 20 20 20 20 20  = YYNSTATE.     
2f0a5 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
2f0a6 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74  yy_pop_parser_st
2f0a7 61 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a  ack(yypParser);.
2f0a8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f0a9 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
2f0aa 3e 79 79 69 64 78 20 3c 20 30 20 7c 7c 20 79 79  >yyidx < 0 || yy
2f0ab 6d 61 6a 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20  major==0 ){.    
2f0ac 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75 63        yy_destruc
2f0ad 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 28 59  tor(yypParser,(Y
2f0ae 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f  YCODETYPE)yymajo
2f0af 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29  r,&yyminorunion)
2f0b0 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 70  ;.          yy_p
2f0b1 61 72 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50  arse_failed(yypP
2f0b2 61 72 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20  arser);.        
2f0b3 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f    yymajor = YYNO
2f0b4 43 4f 44 45 3b 0a 20 20 20 20 20 20 20 20 7d 65  CODE;.        }e
2f0b5 6c 73 65 20 69 66 28 20 79 79 6d 78 21 3d 59 59  lse if( yymx!=YY
2f0b6 45 52 52 4f 52 53 59 4d 42 4f 4c 20 29 7b 0a 20  ERRORSYMBOL ){. 
2f0b7 20 20 20 20 20 20 20 20 20 59 59 4d 49 4e 4f 52           YYMINOR
2f0b8 54 59 50 45 20 75 32 3b 0a 20 20 20 20 20 20 20  TYPE u2;.       
2f0b9 20 20 20 75 32 2e 59 59 45 52 52 53 59 4d 44 54     u2.YYERRSYMDT
2f0ba 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
2f0bb 79 79 5f 73 68 69 66 74 28 79 79 70 50 61 72 73  yy_shift(yypPars
2f0bc 65 72 2c 79 79 61 63 74 2c 59 59 45 52 52 4f 52  er,yyact,YYERROR
2f0bd 53 59 4d 42 4f 4c 2c 26 75 32 29 3b 0a 20 20 20  SYMBOL,&u2);.   
2f0be 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2f0bf 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e       yypParser->
2f0c0 79 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20  yyerrcnt = 3;.  
2f0c1 20 20 20 20 79 79 65 72 72 6f 72 68 69 74 20 3d      yyerrorhit =
2f0c2 20 31 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 59 59   1;.#else  /* YY
2f0c3 45 52 52 4f 52 53 59 4d 42 4f 4c 20 69 73 20 6e  ERRORSYMBOL is n
2f0c4 6f 74 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20 20  ot defined */.  
2f0c5 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 77      /* This is w
2f0c6 68 61 74 20 77 65 20 64 6f 20 69 66 20 74 68 65  hat we do if the
2f0c7 20 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 6e 6f   grammar does no
2f0c8 74 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a  t define ERROR:.
2f0c9 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2f0ca 2a 20 20 2a 20 52 65 70 6f 72 74 20 61 6e 20 65  *  * Report an e
2f0cb 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 61 6e  rror message, an
2f0cc 64 20 74 68 72 6f 77 20 61 77 61 79 20 74 68 65  d throw away the
2f0cd 20 69 6e 70 75 74 20 74 6f 6b 65 6e 2e 0a 20 20   input token..  
2f0ce 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
2f0cf 20 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20   * If the input 
2f0d0 74 6f 6b 65 6e 20 69 73 20 24 2c 20 74 68 65 6e  token is $, then
2f0d1 20 66 61 69 6c 20 74 68 65 20 70 61 72 73 65 2e   fail the parse.
2f0d2 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
2f0d3 2a 2a 20 41 73 20 62 65 66 6f 72 65 2c 20 73 75  ** As before, su
2f0d4 62 73 65 71 75 65 6e 74 20 65 72 72 6f 72 20 6d  bsequent error m
2f0d5 65 73 73 61 67 65 73 20 61 72 65 20 73 75 70 70  essages are supp
2f0d6 72 65 73 73 65 64 20 75 6e 74 69 6c 0a 20 20 20  ressed until.   
2f0d7 20 20 20 2a 2a 20 74 68 72 65 65 20 69 6e 70 75     ** three inpu
2f0d8 74 20 74 6f 6b 65 6e 73 20 68 61 76 65 20 62 65  t tokens have be
2f0d9 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  en successfully 
2f0da 73 68 69 66 74 65 64 2e 0a 20 20 20 20 20 20 2a  shifted..      *
2f0db 2f 0a 20 20 20 20 20 20 69 66 28 20 79 79 70 50  /.      if( yypP
2f0dc 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c  arser->yyerrcnt<
2f0dd 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79  =0 ){.        yy
2f0de 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79  _syntax_error(yy
2f0df 70 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c  pParser,yymajor,
2f0e0 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20  yyminorunion);. 
2f0e1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 70       }.      yyp
2f0e2 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74  Parser->yyerrcnt
2f0e3 20 3d 20 33 3b 0a 20 20 20 20 20 20 79 79 5f 64   = 3;.      yy_d
2f0e4 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72  estructor(yypPar
2f0e5 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45 29  ser,(YYCODETYPE)
2f0e6 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
2f0e7 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 69 66  union);.      if
2f0e8 28 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29  ( yyendofinput )
2f0e9 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 70 61 72  {.        yy_par
2f0ea 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72  se_failed(yypPar
2f0eb 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ser);.      }.  
2f0ec 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
2f0ed 4e 4f 43 4f 44 45 3b 0a 23 65 6e 64 69 66 0a 20  NOCODE;.#endif. 
2f0ee 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 79     }.  }while( y
2f0ef 79 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f 44 45  ymajor!=YYNOCODE
2f0f0 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79   && yypParser->y
2f0f1 79 69 64 78 3e 3d 30 20 29 3b 0a 20 20 72 65 74  yidx>=0 );.  ret
2f0f2 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  urn;.}../*******
2f0f3 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
2f0f4 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  arse.c *********
2f0f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0f7 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2f0f8 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2f0f9 6c 65 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  le tokenize.c **
2f0fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0fc 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2f0fd 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
2f0fe 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2f0ff 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2f100 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2f101 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2f102 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2f103 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2f104 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2f105 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2f106 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2f107 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2f108 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2f109 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2f10a 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2f10b 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2f10c 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2f10d 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2f10e 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2f10f 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2f110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f111 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f112 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f113 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2f114 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66  * An tokenizer f
2f115 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  or SQL.**.** Thi
2f116 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2f117 43 20 63 6f 64 65 20 74 68 61 74 20 73 70 6c 69  C code that spli
2f118 74 73 20 61 6e 20 53 51 4c 20 69 6e 70 75 74 20  ts an SQL input 
2f119 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f 0a 2a  string up into.*
2f11a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 6f 6b  * individual tok
2f11b 65 6e 73 20 61 6e 64 20 73 65 6e 64 73 20 74 68  ens and sends th
2f11c 6f 73 65 20 74 6f 6b 65 6e 73 20 6f 6e 65 2d 62  ose tokens one-b
2f11d 79 2d 6f 6e 65 20 6f 76 65 72 20 74 6f 20 74 68  y-one over to th
2f11e 65 0a 2a 2a 20 70 61 72 73 65 72 20 66 6f 72 20  e.** parser for 
2f11f 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20  analysis..**.** 
2f120 24 49 64 3a 20 74 6f 6b 65 6e 69 7a 65 2e 63 2c  $Id: tokenize.c,
2f121 76 20 31 2e 31 35 36 20 32 30 30 39 2f 30 35 2f  v 1.156 2009/05/
2f122 30 31 20 32 31 3a 31 33 3a 33 37 20 64 72 68 20  01 21:13:37 drh 
2f123 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
2f124 54 68 65 20 63 68 61 72 4d 61 70 28 29 20 6d 61  The charMap() ma
2f125 63 72 6f 20 6d 61 70 73 20 61 6c 70 68 61 62 65  cro maps alphabe
2f126 74 69 63 20 63 68 61 72 61 63 74 65 72 73 20 69  tic characters i
2f127 6e 74 6f 20 74 68 65 69 72 0a 2a 2a 20 6c 6f 77  nto their.** low
2f128 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 65 71  er-case ASCII eq
2f129 75 69 76 61 6c 65 6e 74 2e 20 20 4f 6e 20 41 53  uivalent.  On AS
2f12a 43 49 49 20 6d 61 63 68 69 6e 65 73 2c 20 74 68  CII machines, th
2f12b 69 73 20 69 73 20 6a 75 73 74 0a 2a 2a 20 61 6e  is is just.** an
2f12c 20 75 70 70 65 72 2d 74 6f 2d 6c 6f 77 65 72 20   upper-to-lower 
2f12d 63 61 73 65 20 6d 61 70 2e 20 20 4f 6e 20 45 42  case map.  On EB
2f12e 43 44 49 43 20 6d 61 63 68 69 6e 65 73 20 77 65  CDIC machines we
2f12f 20 61 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20 74 6f   also need.** to
2f130 20 61 64 6a 75 73 74 20 74 68 65 20 65 6e 63 6f   adjust the enco
2f131 64 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c 70 68  ding.  Only alph
2f132 61 62 65 74 69 63 20 63 68 61 72 61 63 74 65 72  abetic character
2f133 73 20 61 6e 64 20 75 6e 64 65 72 73 63 6f 72 65  s and underscore
2f134 73 0a 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65 20  s.** need to be 
2f135 74 72 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f 0a 23  translated..*/.#
2f136 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
2f137 49 49 0a 23 20 64 65 66 69 6e 65 20 63 68 61 72  II.# define char
2f138 4d 61 70 28 58 29 20 73 71 6c 69 74 65 33 55 70  Map(X) sqlite3Up
2f139 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69  perToLower[(unsi
2f13a 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 23 65 6e  gned char)X].#en
2f13b 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
2f13c 45 5f 45 42 43 44 49 43 0a 23 20 64 65 66 69 6e  E_EBCDIC.# defin
2f13d 65 20 63 68 61 72 4d 61 70 28 58 29 20 65 62 63  e charMap(X) ebc
2f13e 64 69 63 54 6f 41 73 63 69 69 5b 28 75 6e 73 69  dicToAscii[(unsi
2f13f 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 63 6f 6e  gned char)X].con
2f140 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2f141 20 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b 5d   ebcdicToAscii[]
2f142 20 3d 20 7b 0a 2f 2a 20 30 20 20 20 31 20 20 20   = {./* 0   1   
2f143 32 20 20 20 33 20 20 20 34 20 20 20 35 20 20 20  2   3   4   5   
2f144 36 20 20 20 37 20 20 20 38 20 20 20 39 20 20 20  6   7   8   9   
2f145 41 20 20 20 42 20 20 20 43 20 20 20 44 20 20 20  A   B   C   D   
2f146 45 20 20 20 46 20 2a 2f 0a 20 20 20 30 2c 20 20  E   F */.   0,  
2f147 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f148 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f149 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f14a 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 30  0,  0,  0,  /* 0
2f14b 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
2f14c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f14d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f14e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f14f 30 2c 20 20 30 2c 20 20 2f 2a 20 31 78 20 2a 2f  0,  0,  /* 1x */
2f150 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
2f151 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f152 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f153 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f154 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20  0,  /* 2x */.   
2f155 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f156 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f157 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f158 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f159 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 3x */.   0,  
2f15a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f15b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f15c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f15d 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 34  0,  0,  0,  /* 4
2f15e 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
2f15f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f160 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f161 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f162 30 2c 20 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f  0,  0,  /* 5x */
2f163 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
2f164 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f165 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f166 30 2c 20 20 30 2c 20 39 35 2c 20 20 30 2c 20 20  0,  0, 95,  0,  
2f167 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20  0,  /* 6x */.   
2f168 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f169 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f16a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f16b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f16c 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 30 2c 20 39  /* 7x */.   0, 9
2f16d 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
2f16e 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
2f16f 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  5,  0,  0,  0,  
2f170 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 38  0,  0,  0,  /* 8
2f171 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30  x */.   0,106,10
2f172 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
2f173 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20  1,112,113,114,  
2f174 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f175 30 2c 20 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f  0,  0,  /* 9x */
2f176 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31  .   0,  0,115,11
2f177 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
2f178 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20  0,121,122,  0,  
2f179 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f17a 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20  0,  /* Ax */.   
2f17b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f17c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f17d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f17e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f17f 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 30 2c 20 39  /* Bx */.   0, 9
2f180 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
2f181 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
2f182 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  5,  0,  0,  0,  
2f183 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 43  0,  0,  0,  /* C
2f184 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30  x */.   0,106,10
2f185 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
2f186 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20  1,112,113,114,  
2f187 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f188 30 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20 2a 2f  0,  0,  /* Dx */
2f189 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31  .   0,  0,115,11
2f18a 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
2f18b 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20  0,121,122,  0,  
2f18c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f18d 30 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20  0,  /* Ex */.   
2f18e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f18f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f190 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f191 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2f192 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65 6e 64  /* Fx */.};.#end
2f193 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  if../*.** The sq
2f194 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65  lite3KeywordCode
2f195 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b 73 20   function looks 
2f196 75 70 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  up an identifier
2f197 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66   to determine if
2f198 0a 2a 2a 20 69 74 20 69 73 20 61 20 6b 65 79 77  .** it is a keyw
2f199 6f 72 64 2e 20 20 49 66 20 69 74 20 69 73 20 61  ord.  If it is a
2f19a 20 6b 65 79 77 6f 72 64 2c 20 74 68 65 20 74 6f   keyword, the to
2f19b 6b 65 6e 20 63 6f 64 65 20 6f 66 20 74 68 61 74  ken code of that
2f19c 20 6b 65 79 77 6f 72 64 20 69 73 20 0a 2a 2a 20   keyword is .** 
2f19d 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68  returned.  If th
2f19e 65 20 69 6e 70 75 74 20 69 73 20 6e 6f 74 20 61  e input is not a
2f19f 20 6b 65 79 77 6f 72 64 2c 20 54 4b 5f 49 44 20   keyword, TK_ID 
2f1a0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
2f1a1 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ** The implement
2f1a2 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f  ation of this ro
2f1a3 75 74 69 6e 65 20 77 61 73 20 67 65 6e 65 72 61  utine was genera
2f1a4 74 65 64 20 62 79 20 61 20 70 72 6f 67 72 61 6d  ted by a program
2f1a5 2c 0a 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64 68 61  ,.** mkkeywordha
2f1a6 73 68 2e 68 2c 20 6c 6f 63 61 74 65 64 20 69 6e  sh.h, located in
2f1a7 20 74 68 65 20 74 6f 6f 6c 20 73 75 62 64 69 72   the tool subdir
2f1a8 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64 69  ectory of the di
2f1a9 73 74 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a 20 54  stribution..** T
2f1aa 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65  he output of the
2f1ab 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63   mkkeywordhash.c
2f1ac 20 70 72 6f 67 72 61 6d 20 69 73 20 77 72 69 74   program is writ
2f1ad 74 65 6e 20 69 6e 74 6f 20 61 20 66 69 6c 65 0a  ten into a file.
2f1ae 2a 2a 20 6e 61 6d 65 64 20 6b 65 79 77 6f 72 64  ** named keyword
2f1af 68 61 73 68 2e 68 20 61 6e 64 20 74 68 65 6e 20  hash.h and then 
2f1b0 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68  included into th
2f1b1 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 62  is source file b
2f1b2 79 0a 2a 2a 20 74 68 65 20 23 69 6e 63 6c 75 64  y.** the #includ
2f1b3 65 20 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a  e below..*/./***
2f1b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
2f1b5 75 64 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e  ude keywordhash.
2f1b6 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
2f1b7 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  of tokenize.c **
2f1b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2f1b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2f1ba 6e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64 68 61  n file keywordha
2f1bb 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
2f1bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f1bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2f1be 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
2f1bf 74 61 69 6e 73 20 61 75 74 6f 6d 61 74 69 63 61  tains automatica
2f1c0 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f  lly generated co
2f1c1 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  de ******.**.** 
2f1c2 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
2f1c3 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 61   file has been a
2f1c4 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e  utomatically gen
2f1c5 65 72 61 74 65 64 20 62 79 0a 2a 2a 0a 2a 2a 20  erated by.**.** 
2f1c6 20 20 20 20 24 48 65 61 64 65 72 3a 20 2f 73 71      $Header: /sq
2f1c7 6c 69 74 65 2f 73 71 6c 69 74 65 2f 74 6f 6f 6c  lite/sqlite/tool
2f1c8 2f 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63  /mkkeywordhash.c
2f1c9 2c 76 20 31 2e 33 37 20 32 30 30 39 2f 30 32 2f  ,v 1.37 2009/02/
2f1ca 30 31 20 30 30 3a 30 30 3a 34 36 20 64 72 68 20  01 00:00:46 drh 
2f1cb 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 65 20  Exp $.**.** The 
2f1cc 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
2f1cd 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66  e implements a f
2f1ce 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 65 74  unction that det
2f1cf 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 0a  ermines whether.
2f1d0 2a 2a 20 6f 72 20 6e 6f 74 20 61 20 67 69 76 65  ** or not a give
2f1d1 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 20  n identifier is 
2f1d2 72 65 61 6c 6c 79 20 61 6e 20 53 51 4c 20 6b 65  really an SQL ke
2f1d3 79 77 6f 72 64 2e 20 20 54 68 65 20 73 61 6d 65  yword.  The same
2f1d4 20 74 68 69 6e 67 0a 2a 2a 20 6d 69 67 68 74 20   thing.** might 
2f1d5 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6d  be implemented m
2f1d6 6f 72 65 20 64 69 72 65 63 74 6c 79 20 75 73 69  ore directly usi
2f1d7 6e 67 20 61 20 68 61 6e 64 2d 77 72 69 74 74 65  ng a hand-writte
2f1d8 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2a  n hash table..**
2f1d9 20 42 75 74 20 62 79 20 75 73 69 6e 67 20 74 68   But by using th
2f1da 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
2f1db 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 2c   generated code,
2f1dc 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
2f1dd 20 63 6f 64 65 0a 2a 2a 20 69 73 20 73 75 62 73   code.** is subs
2f1de 74 61 6e 74 69 61 6c 6c 79 20 72 65 64 75 63 65  tantially reduce
2f1df 64 2e 20 20 54 68 69 73 20 69 73 20 69 6d 70 6f  d.  This is impo
2f1e0 72 74 61 6e 74 20 66 6f 72 20 65 6d 62 65 64 64  rtant for embedd
2f1e1 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  ed applications.
2f1e2 2a 2a 20 6f 6e 20 70 6c 61 74 66 6f 72 6d 73 20  ** on platforms 
2f1e3 77 69 74 68 20 6c 69 6d 69 74 65 64 20 6d 65 6d  with limited mem
2f1e4 6f 72 79 2e 0a 2a 2f 0a 2f 2a 20 48 61 73 68 20  ory..*/./* Hash 
2f1e5 73 63 6f 72 65 3a 20 31 37 31 20 2a 2f 0a 73 74  score: 171 */.st
2f1e6 61 74 69 63 20 69 6e 74 20 6b 65 79 77 6f 72 64  atic int keyword
2f1e7 43 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20  Code(const char 
2f1e8 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 2f 2a  *z, int n){.  /*
2f1e9 20 7a 54 65 78 74 5b 5d 20 65 6e 63 6f 64 65 73   zText[] encodes
2f1ea 20 38 30 31 20 62 79 74 65 73 20 6f 66 20 6b 65   801 bytes of ke
2f1eb 79 77 6f 72 64 73 20 69 6e 20 35 34 31 20 62 79  ywords in 541 by
2f1ec 74 65 73 20 2a 2f 0a 20 20 2f 2a 20 20 20 52 45  tes */.  /*   RE
2f1ed 49 4e 44 45 58 45 44 45 53 43 41 50 45 41 43 48  INDEXEDESCAPEACH
2f1ee 45 43 4b 45 59 42 45 46 4f 52 45 49 47 4e 4f 52  ECKEYBEFOREIGNOR
2f1ef 45 47 45 58 50 4c 41 49 4e 53 54 45 41 44 44 41  EGEXPLAINSTEADDA
2f1f0 54 41 42 41 53 45 4c 45 43 54 20 20 20 20 20 20  TABASELECT      
2f1f1 20 2a 2f 0a 20 20 2f 2a 20 20 20 41 42 4c 45 46   */.  /*   ABLEF
2f1f2 54 48 45 4e 44 45 46 45 52 52 41 42 4c 45 4c 53  THENDEFERRABLELS
2f1f3 45 58 43 45 50 54 52 41 4e 53 41 43 54 49 4f 4e  EXCEPTRANSACTION
2f1f4 41 54 55 52 41 4c 54 45 52 41 49 53 45 58 43 4c  ATURALTERAISEXCL
2f1f5 55 53 49 56 45 20 20 20 20 20 20 20 20 20 2a 2f  USIVE         */
2f1f6 0a 20 20 2f 2a 20 20 20 58 49 53 54 53 41 56 45  .  /*   XISTSAVE
2f1f7 50 4f 49 4e 54 45 52 53 45 43 54 52 49 47 47 45  POINTERSECTRIGGE
2f1f8 52 45 46 45 52 45 4e 43 45 53 43 4f 4e 53 54 52  REFERENCESCONSTR
2f1f9 41 49 4e 54 4f 46 46 53 45 54 45 4d 50 4f 52 41  AINTOFFSETEMPORA
2f1fa 52 59 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  RY         */.  
2f1fb 2f 2a 20 20 20 55 4e 49 51 55 45 52 59 41 54 54  /*   UNIQUERYATT
2f1fc 41 43 48 41 56 49 4e 47 52 4f 55 50 44 41 54 45  ACHAVINGROUPDATE
2f1fd 42 45 47 49 4e 4e 45 52 45 4c 45 41 53 45 42 45  BEGINNERELEASEBE
2f1fe 54 57 45 45 4e 4f 54 4e 55 4c 4c 49 4b 45 20 20  TWEENOTNULLIKE  
2f1ff 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20          */.  /* 
2f200 20 20 43 41 53 43 41 44 45 4c 45 54 45 43 41 53    CASCADELETECAS
2f201 45 43 4f 4c 4c 41 54 45 43 52 45 41 54 45 43 55  ECOLLATECREATECU
2f202 52 52 45 4e 54 5f 44 41 54 45 44 45 54 41 43 48  RRENT_DATEDETACH
2f203 49 4d 4d 45 44 49 41 54 45 4a 4f 49 4e 20 20 20  IMMEDIATEJOIN   
2f204 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 53       */.  /*   S
2f205 45 52 54 4d 41 54 43 48 50 4c 41 4e 41 4c 59 5a  ERTMATCHPLANALYZ
2f206 45 50 52 41 47 4d 41 42 4f 52 54 56 41 4c 55 45  EPRAGMABORTVALUE
2f207 53 56 49 52 54 55 41 4c 49 4d 49 54 57 48 45 4e  SVIRTUALIMITWHEN
2f208 57 48 45 52 45 4e 41 4d 45 20 20 20 20 20 20 20  WHERENAME       
2f209 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 41 46 54 45    */.  /*   AFTE
2f20a 52 45 50 4c 41 43 45 41 4e 44 45 46 41 55 4c 54  REPLACEANDEFAULT
2f20b 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 43 41 53  AUTOINCREMENTCAS
2f20c 54 43 4f 4c 55 4d 4e 43 4f 4d 4d 49 54 43 4f 4e  TCOLUMNCOMMITCON
2f20d 46 4c 49 43 54 43 52 4f 53 53 20 20 20 20 20 2a  FLICTCROSS     *
2f20e 2f 0a 20 20 2f 2a 20 20 20 43 55 52 52 45 4e 54  /.  /*   CURRENT
2f20f 5f 54 49 4d 45 53 54 41 4d 50 52 49 4d 41 52 59  _TIMESTAMPRIMARY
2f210 44 45 46 45 52 52 45 44 49 53 54 49 4e 43 54 44  DEFERREDISTINCTD
2f211 52 4f 50 46 41 49 4c 46 52 4f 4d 46 55 4c 4c 47  ROPFAILFROMFULLG
2f212 4c 4f 42 59 49 46 20 20 20 20 20 20 2a 2f 0a 20  LOBYIF      */. 
2f213 20 2f 2a 20 20 20 49 53 4e 55 4c 4c 4f 52 44 45   /*   ISNULLORDE
2f214 52 45 53 54 52 49 43 54 4f 55 54 45 52 49 47 48  RESTRICTOUTERIGH
2f215 54 52 4f 4c 4c 42 41 43 4b 52 4f 57 55 4e 49 4f  TROLLBACKROWUNIO
2f216 4e 55 53 49 4e 47 56 41 43 55 55 4d 56 49 45 57  NUSINGVACUUMVIEW
2f217 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a           */.  /*
2f218 20 20 20 49 4e 49 54 49 41 4c 4c 59 20 20 20 20     INITIALLY    
2f219 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f21a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f21b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f21c 20 20 20 20 20 20 2a 2f 0a 20 20 73 74 61 74 69        */.  stati
2f21d 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 54 65  c const char zTe
2f21e 78 74 5b 35 34 30 5d 20 3d 20 7b 0a 20 20 20 20  xt[540] = {.    
2f21f 27 52 27 2c 27 45 27 2c 27 49 27 2c 27 4e 27 2c  'R','E','I','N',
2f220 27 44 27 2c 27 45 27 2c 27 58 27 2c 27 45 27 2c  'D','E','X','E',
2f221 27 44 27 2c 27 45 27 2c 27 53 27 2c 27 43 27 2c  'D','E','S','C',
2f222 27 41 27 2c 27 50 27 2c 27 45 27 2c 27 41 27 2c  'A','P','E','A',
2f223 27 43 27 2c 27 48 27 2c 0a 20 20 20 20 27 45 27  'C','H',.    'E'
2f224 2c 27 43 27 2c 27 4b 27 2c 27 45 27 2c 27 59 27  ,'C','K','E','Y'
2f225 2c 27 42 27 2c 27 45 27 2c 27 46 27 2c 27 4f 27  ,'B','E','F','O'
2f226 2c 27 52 27 2c 27 45 27 2c 27 49 27 2c 27 47 27  ,'R','E','I','G'
2f227 2c 27 4e 27 2c 27 4f 27 2c 27 52 27 2c 27 45 27  ,'N','O','R','E'
2f228 2c 27 47 27 2c 0a 20 20 20 20 27 45 27 2c 27 58  ,'G',.    'E','X
2f229 27 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c 27 49  ','P','L','A','I
2f22a 27 2c 27 4e 27 2c 27 53 27 2c 27 54 27 2c 27 45  ','N','S','T','E
2f22b 27 2c 27 41 27 2c 27 44 27 2c 27 44 27 2c 27 41  ','A','D','D','A
2f22c 27 2c 27 54 27 2c 27 41 27 2c 27 42 27 2c 27 41  ','T','A','B','A
2f22d 27 2c 0a 20 20 20 20 27 53 27 2c 27 45 27 2c 27  ',.    'S','E','
2f22e 4c 27 2c 27 45 27 2c 27 43 27 2c 27 54 27 2c 27  L','E','C','T','
2f22f 41 27 2c 27 42 27 2c 27 4c 27 2c 27 45 27 2c 27  A','B','L','E','
2f230 46 27 2c 27 54 27 2c 27 48 27 2c 27 45 27 2c 27  F','T','H','E','
2f231 4e 27 2c 27 44 27 2c 27 45 27 2c 27 46 27 2c 0a  N','D','E','F',.
2f232 20 20 20 20 27 45 27 2c 27 52 27 2c 27 52 27 2c      'E','R','R',
2f233 27 41 27 2c 27 42 27 2c 27 4c 27 2c 27 45 27 2c  'A','B','L','E',
2f234 27 4c 27 2c 27 53 27 2c 27 45 27 2c 27 58 27 2c  'L','S','E','X',
2f235 27 43 27 2c 27 45 27 2c 27 50 27 2c 27 54 27 2c  'C','E','P','T',
2f236 27 52 27 2c 27 41 27 2c 27 4e 27 2c 0a 20 20 20  'R','A','N',.   
2f237 20 27 53 27 2c 27 41 27 2c 27 43 27 2c 27 54 27   'S','A','C','T'
2f238 2c 27 49 27 2c 27 4f 27 2c 27 4e 27 2c 27 41 27  ,'I','O','N','A'
2f239 2c 27 54 27 2c 27 55 27 2c 27 52 27 2c 27 41 27  ,'T','U','R','A'
2f23a 2c 27 4c 27 2c 27 54 27 2c 27 45 27 2c 27 52 27  ,'L','T','E','R'
2f23b 2c 27 41 27 2c 27 49 27 2c 0a 20 20 20 20 27 53  ,'A','I',.    'S
2f23c 27 2c 27 45 27 2c 27 58 27 2c 27 43 27 2c 27 4c  ','E','X','C','L
2f23d 27 2c 27 55 27 2c 27 53 27 2c 27 49 27 2c 27 56  ','U','S','I','V
2f23e 27 2c 27 45 27 2c 27 58 27 2c 27 49 27 2c 27 53  ','E','X','I','S
2f23f 27 2c 27 54 27 2c 27 53 27 2c 27 41 27 2c 27 56  ','T','S','A','V
2f240 27 2c 27 45 27 2c 0a 20 20 20 20 27 50 27 2c 27  ','E',.    'P','
2f241 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 54 27 2c 27  O','I','N','T','
2f242 45 27 2c 27 52 27 2c 27 53 27 2c 27 45 27 2c 27  E','R','S','E','
2f243 43 27 2c 27 54 27 2c 27 52 27 2c 27 49 27 2c 27  C','T','R','I','
2f244 47 27 2c 27 47 27 2c 27 45 27 2c 27 52 27 2c 27  G','G','E','R','
2f245 45 27 2c 0a 20 20 20 20 27 46 27 2c 27 45 27 2c  E',.    'F','E',
2f246 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 43 27 2c  'R','E','N','C',
2f247 27 45 27 2c 27 53 27 2c 27 43 27 2c 27 4f 27 2c  'E','S','C','O',
2f248 27 4e 27 2c 27 53 27 2c 27 54 27 2c 27 52 27 2c  'N','S','T','R',
2f249 27 41 27 2c 27 49 27 2c 27 4e 27 2c 27 54 27 2c  'A','I','N','T',
2f24a 0a 20 20 20 20 27 4f 27 2c 27 46 27 2c 27 46 27  .    'O','F','F'
2f24b 2c 27 53 27 2c 27 45 27 2c 27 54 27 2c 27 45 27  ,'S','E','T','E'
2f24c 2c 27 4d 27 2c 27 50 27 2c 27 4f 27 2c 27 52 27  ,'M','P','O','R'
2f24d 2c 27 41 27 2c 27 52 27 2c 27 59 27 2c 27 55 27  ,'A','R','Y','U'
2f24e 2c 27 4e 27 2c 27 49 27 2c 27 51 27 2c 0a 20 20  ,'N','I','Q',.  
2f24f 20 20 27 55 27 2c 27 45 27 2c 27 52 27 2c 27 59    'U','E','R','Y
2f250 27 2c 27 41 27 2c 27 54 27 2c 27 54 27 2c 27 41  ','A','T','T','A
2f251 27 2c 27 43 27 2c 27 48 27 2c 27 41 27 2c 27 56  ','C','H','A','V
2f252 27 2c 27 49 27 2c 27 4e 27 2c 27 47 27 2c 27 52  ','I','N','G','R
2f253 27 2c 27 4f 27 2c 27 55 27 2c 0a 20 20 20 20 27  ','O','U',.    '
2f254 50 27 2c 27 44 27 2c 27 41 27 2c 27 54 27 2c 27  P','D','A','T','
2f255 45 27 2c 27 42 27 2c 27 45 27 2c 27 47 27 2c 27  E','B','E','G','
2f256 49 27 2c 27 4e 27 2c 27 4e 27 2c 27 45 27 2c 27  I','N','N','E','
2f257 52 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27  R','E','L','E','
2f258 41 27 2c 27 53 27 2c 0a 20 20 20 20 27 45 27 2c  A','S',.    'E',
2f259 27 42 27 2c 27 45 27 2c 27 54 27 2c 27 57 27 2c  'B','E','T','W',
2f25a 27 45 27 2c 27 45 27 2c 27 4e 27 2c 27 4f 27 2c  'E','E','N','O',
2f25b 27 54 27 2c 27 4e 27 2c 27 55 27 2c 27 4c 27 2c  'T','N','U','L',
2f25c 27 4c 27 2c 27 49 27 2c 27 4b 27 2c 27 45 27 2c  'L','I','K','E',
2f25d 27 43 27 2c 0a 20 20 20 20 27 41 27 2c 27 53 27  'C',.    'A','S'
2f25e 2c 27 43 27 2c 27 41 27 2c 27 44 27 2c 27 45 27  ,'C','A','D','E'
2f25f 2c 27 4c 27 2c 27 45 27 2c 27 54 27 2c 27 45 27  ,'L','E','T','E'
2f260 2c 27 43 27 2c 27 41 27 2c 27 53 27 2c 27 45 27  ,'C','A','S','E'
2f261 2c 27 43 27 2c 27 4f 27 2c 27 4c 27 2c 27 4c 27  ,'C','O','L','L'
2f262 2c 0a 20 20 20 20 27 41 27 2c 27 54 27 2c 27 45  ,.    'A','T','E
2f263 27 2c 27 43 27 2c 27 52 27 2c 27 45 27 2c 27 41  ','C','R','E','A
2f264 27 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c 27 55  ','T','E','C','U
2f265 27 2c 27 52 27 2c 27 52 27 2c 27 45 27 2c 27 4e  ','R','R','E','N
2f266 27 2c 27 54 27 2c 27 5f 27 2c 27 44 27 2c 0a 20  ','T','_','D',. 
2f267 20 20 20 27 41 27 2c 27 54 27 2c 27 45 27 2c 27     'A','T','E','
2f268 44 27 2c 27 45 27 2c 27 54 27 2c 27 41 27 2c 27  D','E','T','A','
2f269 43 27 2c 27 48 27 2c 27 49 27 2c 27 4d 27 2c 27  C','H','I','M','
2f26a 4d 27 2c 27 45 27 2c 27 44 27 2c 27 49 27 2c 27  M','E','D','I','
2f26b 41 27 2c 27 54 27 2c 27 45 27 2c 0a 20 20 20 20  A','T','E',.    
2f26c 27 4a 27 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c  'J','O','I','N',
2f26d 27 53 27 2c 27 45 27 2c 27 52 27 2c 27 54 27 2c  'S','E','R','T',
2f26e 27 4d 27 2c 27 41 27 2c 27 54 27 2c 27 43 27 2c  'M','A','T','C',
2f26f 27 48 27 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c  'H','P','L','A',
2f270 27 4e 27 2c 27 41 27 2c 0a 20 20 20 20 27 4c 27  'N','A',.    'L'
2f271 2c 27 59 27 2c 27 5a 27 2c 27 45 27 2c 27 50 27  ,'Y','Z','E','P'
2f272 2c 27 52 27 2c 27 41 27 2c 27 47 27 2c 27 4d 27  ,'R','A','G','M'
2f273 2c 27 41 27 2c 27 42 27 2c 27 4f 27 2c 27 52 27  ,'A','B','O','R'
2f274 2c 27 54 27 2c 27 56 27 2c 27 41 27 2c 27 4c 27  ,'T','V','A','L'
2f275 2c 27 55 27 2c 0a 20 20 20 20 27 45 27 2c 27 53  ,'U',.    'E','S
2f276 27 2c 27 56 27 2c 27 49 27 2c 27 52 27 2c 27 54  ','V','I','R','T
2f277 27 2c 27 55 27 2c 27 41 27 2c 27 4c 27 2c 27 49  ','U','A','L','I
2f278 27 2c 27 4d 27 2c 27 49 27 2c 27 54 27 2c 27 57  ','M','I','T','W
2f279 27 2c 27 48 27 2c 27 45 27 2c 27 4e 27 2c 27 57  ','H','E','N','W
2f27a 27 2c 0a 20 20 20 20 27 48 27 2c 27 45 27 2c 27  ',.    'H','E','
2f27b 52 27 2c 27 45 27 2c 27 4e 27 2c 27 41 27 2c 27  R','E','N','A','
2f27c 4d 27 2c 27 45 27 2c 27 41 27 2c 27 46 27 2c 27  M','E','A','F','
2f27d 54 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27  T','E','R','E','
2f27e 50 27 2c 27 4c 27 2c 27 41 27 2c 27 43 27 2c 0a  P','L','A','C',.
2f27f 20 20 20 20 27 45 27 2c 27 41 27 2c 27 4e 27 2c      'E','A','N',
2f280 27 44 27 2c 27 45 27 2c 27 46 27 2c 27 41 27 2c  'D','E','F','A',
2f281 27 55 27 2c 27 4c 27 2c 27 54 27 2c 27 41 27 2c  'U','L','T','A',
2f282 27 55 27 2c 27 54 27 2c 27 4f 27 2c 27 49 27 2c  'U','T','O','I',
2f283 27 4e 27 2c 27 43 27 2c 27 52 27 2c 0a 20 20 20  'N','C','R',.   
2f284 20 27 45 27 2c 27 4d 27 2c 27 45 27 2c 27 4e 27   'E','M','E','N'
2f285 2c 27 54 27 2c 27 43 27 2c 27 41 27 2c 27 53 27  ,'T','C','A','S'
2f286 2c 27 54 27 2c 27 43 27 2c 27 4f 27 2c 27 4c 27  ,'T','C','O','L'
2f287 2c 27 55 27 2c 27 4d 27 2c 27 4e 27 2c 27 43 27  ,'U','M','N','C'
2f288 2c 27 4f 27 2c 27 4d 27 2c 0a 20 20 20 20 27 4d  ,'O','M',.    'M
2f289 27 2c 27 49 27 2c 27 54 27 2c 27 43 27 2c 27 4f  ','I','T','C','O
2f28a 27 2c 27 4e 27 2c 27 46 27 2c 27 4c 27 2c 27 49  ','N','F','L','I
2f28b 27 2c 27 43 27 2c 27 54 27 2c 27 43 27 2c 27 52  ','C','T','C','R
2f28c 27 2c 27 4f 27 2c 27 53 27 2c 27 53 27 2c 27 43  ','O','S','S','C
2f28d 27 2c 27 55 27 2c 0a 20 20 20 20 27 52 27 2c 27  ','U',.    'R','
2f28e 52 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27  R','E','N','T','
2f28f 5f 27 2c 27 54 27 2c 27 49 27 2c 27 4d 27 2c 27  _','T','I','M','
2f290 45 27 2c 27 53 27 2c 27 54 27 2c 27 41 27 2c 27  E','S','T','A','
2f291 4d 27 2c 27 50 27 2c 27 52 27 2c 27 49 27 2c 27  M','P','R','I','
2f292 4d 27 2c 0a 20 20 20 20 27 41 27 2c 27 52 27 2c  M',.    'A','R',
2f293 27 59 27 2c 27 44 27 2c 27 45 27 2c 27 46 27 2c  'Y','D','E','F',
2f294 27 45 27 2c 27 52 27 2c 27 52 27 2c 27 45 27 2c  'E','R','R','E',
2f295 27 44 27 2c 27 49 27 2c 27 53 27 2c 27 54 27 2c  'D','I','S','T',
2f296 27 49 27 2c 27 4e 27 2c 27 43 27 2c 27 54 27 2c  'I','N','C','T',
2f297 0a 20 20 20 20 27 44 27 2c 27 52 27 2c 27 4f 27  .    'D','R','O'
2f298 2c 27 50 27 2c 27 46 27 2c 27 41 27 2c 27 49 27  ,'P','F','A','I'
2f299 2c 27 4c 27 2c 27 46 27 2c 27 52 27 2c 27 4f 27  ,'L','F','R','O'
2f29a 2c 27 4d 27 2c 27 46 27 2c 27 55 27 2c 27 4c 27  ,'M','F','U','L'
2f29b 2c 27 4c 27 2c 27 47 27 2c 27 4c 27 2c 0a 20 20  ,'L','G','L',.  
2f29c 20 20 27 4f 27 2c 27 42 27 2c 27 59 27 2c 27 49    'O','B','Y','I
2f29d 27 2c 27 46 27 2c 27 49 27 2c 27 53 27 2c 27 4e  ','F','I','S','N
2f29e 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 4f  ','U','L','L','O
2f29f 27 2c 27 52 27 2c 27 44 27 2c 27 45 27 2c 27 52  ','R','D','E','R
2f2a0 27 2c 27 45 27 2c 27 53 27 2c 0a 20 20 20 20 27  ','E','S',.    '
2f2a1 54 27 2c 27 52 27 2c 27 49 27 2c 27 43 27 2c 27  T','R','I','C','
2f2a2 54 27 2c 27 4f 27 2c 27 55 27 2c 27 54 27 2c 27  T','O','U','T','
2f2a3 45 27 2c 27 52 27 2c 27 49 27 2c 27 47 27 2c 27  E','R','I','G','
2f2a4 48 27 2c 27 54 27 2c 27 52 27 2c 27 4f 27 2c 27  H','T','R','O','
2f2a5 4c 27 2c 27 4c 27 2c 0a 20 20 20 20 27 42 27 2c  L','L',.    'B',
2f2a6 27 41 27 2c 27 43 27 2c 27 4b 27 2c 27 52 27 2c  'A','C','K','R',
2f2a7 27 4f 27 2c 27 57 27 2c 27 55 27 2c 27 4e 27 2c  'O','W','U','N',
2f2a8 27 49 27 2c 27 4f 27 2c 27 4e 27 2c 27 55 27 2c  'I','O','N','U',
2f2a9 27 53 27 2c 27 49 27 2c 27 4e 27 2c 27 47 27 2c  'S','I','N','G',
2f2aa 27 56 27 2c 0a 20 20 20 20 27 41 27 2c 27 43 27  'V',.    'A','C'
2f2ab 2c 27 55 27 2c 27 55 27 2c 27 4d 27 2c 27 56 27  ,'U','U','M','V'
2f2ac 2c 27 49 27 2c 27 45 27 2c 27 57 27 2c 27 49 27  ,'I','E','W','I'
2f2ad 2c 27 4e 27 2c 27 49 27 2c 27 54 27 2c 27 49 27  ,'N','I','T','I'
2f2ae 2c 27 41 27 2c 27 4c 27 2c 27 4c 27 2c 27 59 27  ,'A','L','L','Y'
2f2af 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  ,.  };.  static 
2f2b0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2f2b1 68 61 72 20 61 48 61 73 68 5b 31 32 37 5d 20 3d  har aHash[127] =
2f2b2 20 7b 0a 20 20 20 20 20 20 37 30 2c 20 20 39 39   {.      70,  99
2f2b3 2c 20 31 31 32 2c 20 20 36 38 2c 20 20 20 30 2c  , 112,  68,   0,
2f2b4 20 20 34 33 2c 20 20 20 30 2c 20 20 20 30 2c 20    43,   0,   0, 
2f2b5 20 37 36 2c 20 20 20 30 2c 20 20 37 31 2c 20 20   76,   0,  71,  
2f2b6 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 34   0,   0,.      4
2f2b7 31 2c 20 20 31 32 2c 20 20 37 32 2c 20 20 31 35  1,  12,  72,  15
2f2b8 2c 20 20 20 30 2c 20 31 31 31 2c 20 20 37 39 2c  ,   0, 111,  79,
2f2b9 20 20 34 39 2c 20 31 30 36 2c 20 20 20 30 2c 20    49, 106,   0, 
2f2ba 20 31 39 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20   19,   0,   0,. 
2f2bb 20 20 20 20 31 31 36 2c 20 20 20 30 2c 20 31 31      116,   0, 11
2f2bc 34 2c 20 31 30 39 2c 20 20 20 30 2c 20 20 32 32  4, 109,   0,  22
2f2bd 2c 20 20 38 37 2c 20 20 20 30 2c 20 20 20 39 2c  ,  87,   0,   9,
2f2be 20 20 20 30 2c 20 20 20 30 2c 20 20 36 34 2c 20     0,   0,  64, 
2f2bf 20 36 35 2c 0a 20 20 20 20 20 20 20 30 2c 20 20   65,.       0,  
2f2c0 36 33 2c 20 20 20 36 2c 20 20 20 30 2c 20 20 34  63,   6,   0,  4
2f2c1 37 2c 20 20 38 34 2c 20 20 39 36 2c 20 20 20 30  7,  84,  96,   0
2f2c2 2c 20 31 31 33 2c 20 20 39 35 2c 20 20 20 30 2c  , 113,  95,   0,
2f2c3 20 20 20 30 2c 20 20 34 34 2c 0a 20 20 20 20 20     0,  44,.     
2f2c4 20 20 30 2c 20 20 39 37 2c 20 20 32 34 2c 20 20    0,  97,  24,  
2f2c5 20 30 2c 20 20 31 37 2c 20 20 20 30 2c 20 31 31   0,  17,   0, 11
2f2c6 37 2c 20 20 34 38 2c 20 20 32 33 2c 20 20 20 30  7,  48,  23,   0
2f2c7 2c 20 20 20 35 2c 20 31 30 34 2c 20 20 32 35 2c  ,   5, 104,  25,
2f2c8 0a 20 20 20 20 20 20 39 30 2c 20 20 20 30 2c 20  .      90,   0, 
2f2c9 20 20 30 2c 20 31 31 39 2c 20 31 30 30 2c 20 20    0, 119, 100,  
2f2ca 35 35 2c 20 31 31 38 2c 20 20 35 32 2c 20 20 20  55, 118,  52,   
2f2cb 37 2c 20 20 35 30 2c 20 20 20 30 2c 20 20 38 35  7,  50,   0,  85
2f2cc 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 39 34 2c  ,   0,.      94,
2f2cd 20 20 32 36 2c 20 20 20 30 2c 20 20 39 33 2c 20    26,   0,  93, 
2f2ce 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2f2cf 38 39 2c 20 20 38 36 2c 20 20 39 31 2c 20 20 38  89,  86,  91,  8
2f2d0 32 2c 20 31 30 33 2c 20 20 31 34 2c 0a 20 20 20  2, 103,  14,.   
2f2d1 20 20 20 33 38 2c 20 31 30 32 2c 20 20 20 30 2c     38, 102,   0,
2f2d2 20 20 37 35 2c 20 20 20 30 2c 20 20 31 38 2c 20    75,   0,  18, 
2f2d3 20 38 33 2c 20 31 30 35 2c 20 20 33 31 2c 20 20   83, 105,  31,  
2f2d4 20 30 2c 20 31 31 35 2c 20 20 37 34 2c 20 31 30   0, 115,  74, 10
2f2d5 37 2c 0a 20 20 20 20 20 20 35 37 2c 20 20 34 35  7,.      57,  45
2f2d6 2c 20 20 37 38 2c 20 20 20 30 2c 20 20 20 30 2c  ,  78,   0,   0,
2f2d7 20 20 38 38 2c 20 20 33 39 2c 20 20 20 30 2c 20    88,  39,   0, 
2f2d8 31 31 30 2c 20 20 20 30 2c 20 20 33 35 2c 20 20  110,   0,  35,  
2f2d9 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 32   0,   0,.      2
2f2da 38 2c 20 20 20 30 2c 20 20 38 30 2c 20 20 35 33  8,   0,  80,  53
2f2db 2c 20 20 35 38 2c 20 20 20 30 2c 20 20 32 30 2c  ,  58,   0,  20,
2f2dc 20 20 35 36 2c 20 20 20 30 2c 20 20 35 31 2c 0a    56,   0,  51,.
2f2dd 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f    };.  static co
2f2de 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2f2df 72 20 61 4e 65 78 74 5b 31 31 39 5d 20 3d 20 7b  r aNext[119] = {
2f2e0 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20  .       0,   0, 
2f2e1 20 20 30 2c 20 20 20 30 2c 20 20 20 34 2c 20 20    0,   0,   4,  
2f2e2 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
2f2e3 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2f2e4 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c  ,   0,.       0,
2f2e5 20 20 20 32 2c 20 20 20 30 2c 20 20 20 30 2c 20     2,   0,   0, 
2f2e6 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2f2e7 20 30 2c 20 20 31 33 2c 20 20 20 30 2c 20 20 20   0,  13,   0,   
2f2e8 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20  0,   0,   0,.   
2f2e9 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c      0,   0,   0,
2f2ea 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2f2eb 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2f2ec 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
2f2ed 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30  0,.       0,   0
2f2ee 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33 32 2c  ,   0,   0,  32,
2f2ef 20 20 32 31 2c 20 20 20 30 2c 20 20 20 30 2c 20    21,   0,   0, 
2f2f0 20 20 30 2c 20 20 34 32 2c 20 20 20 33 2c 20 20    0,  42,   3,  
2f2f1 34 36 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20  46,   0,.       
2f2f2 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 32 39  0,   0,   0,  29
2f2f3 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33 37 2c  ,   0,   0,  37,
2f2f4 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2f2f5 20 20 31 2c 20 20 36 30 2c 20 20 20 30 2c 0a 20    1,  60,   0,. 
2f2f6 20 20 20 20 20 20 30 2c 20 20 36 31 2c 20 20 20        0,  61,   
2f2f7 30 2c 20 20 34 30 2c 20 20 20 30 2c 20 20 20 30  0,  40,   0,   0
2f2f8 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2f2f9 20 20 20 30 2c 20 20 20 30 2c 20 20 35 39 2c 20     0,   0,  59, 
2f2fa 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20    0,.       0,  
2f2fb 20 30 2c 20 20 20 30 2c 20 20 33 30 2c 20 20 35   0,   0,  30,  5
2f2fc 34 2c 20 20 31 36 2c 20 20 33 33 2c 20 20 31 30  4,  16,  33,  10
2f2fd 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2f2fe 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
2f2ff 20 20 30 2c 20 20 20 30 2c 20 20 31 31 2c 20 20    0,   0,  11,  
2f300 36 36 2c 20 20 37 33 2c 20 20 20 30 2c 20 20 20  66,  73,   0,   
2f301 38 2c 20 20 20 30 2c 20 20 39 38 2c 20 20 39 32  8,   0,  98,  92
2f302 2c 20 20 20 30 2c 20 31 30 31 2c 20 20 20 30 2c  ,   0, 101,   0,
2f303 0a 20 20 20 20 20 20 38 31 2c 20 20 20 30 2c 20  .      81,   0, 
2f304 20 36 39 2c 20 20 20 30 2c 20 20 20 30 2c 20 31   69,   0,   0, 1
2f305 30 38 2c 20 20 32 37 2c 20 20 33 36 2c 20 20 36  08,  27,  36,  6
2f306 37 2c 20 20 37 37 2c 20 20 20 30 2c 20 20 33 34  7,  77,   0,  34
2f307 2c 20 20 36 32 2c 0a 20 20 20 20 20 20 20 30 2c  ,  62,.       0,
2f308 20 20 20 30 2c 0a 20 20 7d 3b 0a 20 20 73 74 61     0,.  };.  sta
2f309 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
2f30a 65 64 20 63 68 61 72 20 61 4c 65 6e 5b 31 31 39  ed char aLen[119
2f30b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 37 2c 20  ] = {.       7, 
2f30c 20 20 37 2c 20 20 20 35 2c 20 20 20 34 2c 20 20    7,   5,   4,  
2f30d 20 36 2c 20 20 20 34 2c 20 20 20 35 2c 20 20 20   6,   4,   5,   
2f30e 33 2c 20 20 20 36 2c 20 20 20 37 2c 20 20 20 33  3,   6,   7,   3
2f30f 2c 20 20 20 36 2c 20 20 20 36 2c 0a 20 20 20 20  ,   6,   6,.    
2f310 20 20 20 37 2c 20 20 20 37 2c 20 20 20 33 2c 20     7,   7,   3, 
2f311 20 20 38 2c 20 20 20 32 2c 20 20 20 36 2c 20 20    8,   2,   6,  
2f312 20 35 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20   5,   4,   4,   
2f313 33 2c 20 20 31 30 2c 20 20 20 34 2c 20 20 20 36  3,  10,   4,   6
2f314 2c 0a 20 20 20 20 20 20 31 31 2c 20 20 20 32 2c  ,.      11,   2,
2f315 20 20 20 37 2c 20 20 20 35 2c 20 20 20 35 2c 20     7,   5,   5, 
2f316 20 20 39 2c 20 20 20 36 2c 20 20 20 39 2c 20 20    9,   6,   9,  
2f317 20 39 2c 20 20 20 37 2c 20 20 31 30 2c 20 20 31   9,   7,  10,  1
2f318 30 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 36  0,   4,.       6
2f319 2c 20 20 20 32 2c 20 20 20 33 2c 20 20 20 34 2c  ,   2,   3,   4,
2f31a 20 20 20 39 2c 20 20 20 32 2c 20 20 20 36 2c 20     9,   2,   6, 
2f31b 20 20 35 2c 20 20 20 36 2c 20 20 20 36 2c 20 20    5,   6,   6,  
2f31c 20 35 2c 20 20 20 36 2c 20 20 20 35 2c 0a 20 20   5,   6,   5,.  
2f31d 20 20 20 20 20 35 2c 20 20 20 37 2c 20 20 20 37       5,   7,   7
2f31e 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 34 2c  ,   7,   3,   4,
2f31f 20 20 20 34 2c 20 20 20 37 2c 20 20 20 33 2c 20     4,   7,   3, 
2f320 20 20 36 2c 20 20 20 34 2c 20 20 20 37 2c 20 20    6,   4,   7,  
2f321 20 36 2c 0a 20 20 20 20 20 20 31 32 2c 20 20 20   6,.      12,   
2f322 36 2c 20 20 20 39 2c 20 20 20 34 2c 20 20 20 36  6,   9,   4,   6
2f323 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20 37 2c  ,   5,   4,   7,
2f324 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 20     6,   5,   6, 
2f325 20 20 37 2c 20 20 20 35 2c 0a 20 20 20 20 20 20    7,   5,.      
2f326 20 34 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20   4,   5,   6,   
2f327 35 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 37  5,   7,   3,   7
2f328 2c 20 20 31 33 2c 20 20 20 32 2c 20 20 20 32 2c  ,  13,   2,   2,
2f329 20 20 20 34 2c 20 20 20 36 2c 20 20 20 36 2c 0a     4,   6,   6,.
2f32a 20 20 20 20 20 20 20 38 2c 20 20 20 35 2c 20 20         8,   5,  
2f32b 31 37 2c 20 20 31 32 2c 20 20 20 37 2c 20 20 20  17,  12,   7,   
2f32c 38 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 34  8,   8,   2,   4
2f32d 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 34 2c  ,   4,   4,   4,
2f32e 20 20 20 34 2c 0a 20 20 20 20 20 20 20 32 2c 20     4,.       2, 
2f32f 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c 20 20    2,   6,   5,  
2f330 20 38 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20   8,   5,   5,   
2f331 38 2c 20 20 20 33 2c 20 20 20 35 2c 20 20 20 35  8,   3,   5,   5
2f332 2c 20 20 20 36 2c 20 20 20 34 2c 0a 20 20 20 20  ,   6,   4,.    
2f333 20 20 20 39 2c 20 20 20 33 2c 0a 20 20 7d 3b 0a     9,   3,.  };.
2f334 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2f335 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e  nsigned short in
2f336 74 20 61 4f 66 66 73 65 74 5b 31 31 39 5d 20 3d  t aOffset[119] =
2f337 20 7b 0a 20 20 20 20 20 20 20 30 2c 20 20 20 32   {.       0,   2
2f338 2c 20 20 20 32 2c 20 20 20 38 2c 20 20 20 39 2c  ,   2,   8,   9,
2f339 20 20 31 34 2c 20 20 31 36 2c 20 20 32 30 2c 20    14,  16,  20, 
2f33a 20 32 33 2c 20 20 32 35 2c 20 20 32 35 2c 20 20   23,  25,  25,  
2f33b 32 39 2c 20 20 33 33 2c 0a 20 20 20 20 20 20 33  29,  33,.      3
2f33c 36 2c 20 20 34 31 2c 20 20 34 36 2c 20 20 34 38  6,  41,  46,  48
2f33d 2c 20 20 35 33 2c 20 20 35 34 2c 20 20 35 39 2c  ,  53,  54,  59,
2f33e 20 20 36 32 2c 20 20 36 35 2c 20 20 36 37 2c 20    62,  65,  67, 
2f33f 20 36 39 2c 20 20 37 38 2c 20 20 38 31 2c 0a 20   69,  78,  81,. 
2f340 20 20 20 20 20 38 36 2c 20 20 39 35 2c 20 20 39       86,  95,  9
2f341 36 2c 20 31 30 31 2c 20 31 30 35 2c 20 31 30 39  6, 101, 105, 109
2f342 2c 20 31 31 37 2c 20 31 32 32 2c 20 31 32 38 2c  , 117, 122, 128,
2f343 20 31 33 36 2c 20 31 34 32 2c 20 31 35 32 2c 20   136, 142, 152, 
2f344 31 35 39 2c 0a 20 20 20 20 20 31 36 32 2c 20 31  159,.     162, 1
2f345 36 32 2c 20 31 36 35 2c 20 31 36 37 2c 20 31 36  62, 165, 167, 16
2f346 37 2c 20 31 37 31 2c 20 31 37 36 2c 20 31 37 39  7, 171, 176, 179
2f347 2c 20 31 38 34 2c 20 31 38 39 2c 20 31 39 34 2c  , 184, 189, 194,
2f348 20 31 39 37 2c 20 32 30 33 2c 0a 20 20 20 20 20   197, 203,.     
2f349 32 30 36 2c 20 32 31 30 2c 20 32 31 37 2c 20 32  206, 210, 217, 2
2f34a 32 33 2c 20 32 32 33 2c 20 32 32 36 2c 20 32 32  23, 223, 226, 22
2f34b 39 2c 20 32 33 33 2c 20 32 33 34 2c 20 32 33 38  9, 233, 234, 238
2f34c 2c 20 32 34 34 2c 20 32 34 38 2c 20 32 35 35 2c  , 244, 248, 255,
2f34d 0a 20 20 20 20 20 32 36 31 2c 20 32 37 33 2c 20  .     261, 273, 
2f34e 32 37 39 2c 20 32 38 38 2c 20 32 39 30 2c 20 32  279, 288, 290, 2
2f34f 39 36 2c 20 33 30 31 2c 20 33 30 33 2c 20 33 31  96, 301, 303, 31
2f350 30 2c 20 33 31 35 2c 20 33 32 30 2c 20 33 32 36  0, 315, 320, 326
2f351 2c 20 33 33 32 2c 0a 20 20 20 20 20 33 33 37 2c  , 332,.     337,
2f352 20 33 34 31 2c 20 33 34 34 2c 20 33 35 30 2c 20   341, 344, 350, 
2f353 33 35 34 2c 20 33 36 31 2c 20 33 36 33 2c 20 33  354, 361, 363, 3
2f354 37 30 2c 20 33 37 32 2c 20 33 37 34 2c 20 33 38  70, 372, 374, 38
2f355 33 2c 20 33 38 37 2c 20 33 39 33 2c 0a 20 20 20  3, 387, 393,.   
2f356 20 20 33 39 39 2c 20 34 30 37 2c 20 34 31 32 2c    399, 407, 412,
2f357 20 34 31 32 2c 20 34 32 38 2c 20 34 33 35 2c 20   412, 428, 435, 
2f358 34 34 32 2c 20 34 34 33 2c 20 34 35 30 2c 20 34  442, 443, 450, 4
2f359 35 34 2c 20 34 35 38 2c 20 34 36 32 2c 20 34 36  54, 458, 462, 46
2f35a 36 2c 0a 20 20 20 20 20 34 36 39 2c 20 34 37 31  6,.     469, 471
2f35b 2c 20 34 37 33 2c 20 34 37 39 2c 20 34 38 33 2c  , 473, 479, 483,
2f35c 20 34 39 31 2c 20 34 39 35 2c 20 35 30 30 2c 20   491, 495, 500, 
2f35d 35 30 38 2c 20 35 31 31 2c 20 35 31 36 2c 20 35  508, 511, 516, 5
2f35e 32 31 2c 20 35 32 37 2c 0a 20 20 20 20 20 35 33  21, 527,.     53
2f35f 31 2c 20 35 33 36 2c 0a 20 20 7d 3b 0a 20 20 73  1, 536,.  };.  s
2f360 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
2f361 67 6e 65 64 20 63 68 61 72 20 61 43 6f 64 65 5b  gned char aCode[
2f362 31 31 39 5d 20 3d 20 7b 0a 20 20 20 20 54 4b 5f  119] = {.    TK_
2f363 52 45 49 4e 44 45 58 2c 20 20 20 20 54 4b 5f 49  REINDEX,    TK_I
2f364 4e 44 45 58 45 44 2c 20 20 20 20 54 4b 5f 49 4e  NDEXED,    TK_IN
2f365 44 45 58 2c 20 20 20 20 20 20 54 4b 5f 44 45 53  DEX,      TK_DES
2f366 43 2c 20 20 20 20 20 20 20 54 4b 5f 45 53 43 41  C,       TK_ESCA
2f367 50 45 2c 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  PE,     .    TK_
2f368 45 41 43 48 2c 20 20 20 20 20 20 20 54 4b 5f 43  EACH,       TK_C
2f369 48 45 43 4b 2c 20 20 20 20 20 20 54 4b 5f 4b 45  HECK,      TK_KE
2f36a 59 2c 20 20 20 20 20 20 20 20 54 4b 5f 42 45 46  Y,        TK_BEF
2f36b 4f 52 45 2c 20 20 20 20 20 54 4b 5f 46 4f 52 45  ORE,     TK_FORE
2f36c 49 47 4e 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f  IGN,    .    TK_
2f36d 46 4f 52 2c 20 20 20 20 20 20 20 20 54 4b 5f 49  FOR,        TK_I
2f36e 47 4e 4f 52 45 2c 20 20 20 20 20 54 4b 5f 4c 49  GNORE,     TK_LI
2f36f 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 45 58 50  KE_KW,    TK_EXP
2f370 4c 41 49 4e 2c 20 20 20 20 54 4b 5f 49 4e 53 54  LAIN,    TK_INST
2f371 45 41 44 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f  EAD,    .    TK_
2f372 41 44 44 2c 20 20 20 20 20 20 20 20 54 4b 5f 44  ADD,        TK_D
2f373 41 54 41 42 41 53 45 2c 20 20 20 54 4b 5f 41 53  ATABASE,   TK_AS
2f374 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 53 45 4c  ,         TK_SEL
2f375 45 43 54 2c 20 20 20 20 20 54 4b 5f 54 41 42 4c  ECT,     TK_TABL
2f376 45 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  E,      .    TK_
2f377 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 54  JOIN_KW,    TK_T
2f378 48 45 4e 2c 20 20 20 20 20 20 20 54 4b 5f 45 4e  HEN,       TK_EN
2f379 44 2c 20 20 20 20 20 20 20 20 54 4b 5f 44 45 46  D,        TK_DEF
2f37a 45 52 52 41 42 4c 45 2c 20 54 4b 5f 45 4c 53 45  ERRABLE, TK_ELSE
2f37b 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  ,       .    TK_
2f37c 45 58 43 45 50 54 2c 20 20 20 20 20 54 4b 5f 54  EXCEPT,     TK_T
2f37d 52 41 4e 53 41 43 54 49 4f 4e 2c 54 4b 5f 4f 4e  RANSACTION,TK_ON
2f37e 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 4a 4f 49  ,         TK_JOI
2f37f 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 41 4c 54 45  N_KW,    TK_ALTE
2f380 52 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  R,      .    TK_
2f381 52 41 49 53 45 2c 20 20 20 20 20 20 54 4b 5f 45  RAISE,      TK_E
2f382 58 43 4c 55 53 49 56 45 2c 20 20 54 4b 5f 45 58  XCLUSIVE,  TK_EX
2f383 49 53 54 53 2c 20 20 20 20 20 54 4b 5f 53 41 56  ISTS,     TK_SAV
2f384 45 50 4f 49 4e 54 2c 20 20 54 4b 5f 49 4e 54 45  EPOINT,  TK_INTE
2f385 52 53 45 43 54 2c 20 20 0a 20 20 20 20 54 4b 5f  RSECT,  .    TK_
2f386 54 52 49 47 47 45 52 2c 20 20 20 20 54 4b 5f 52  TRIGGER,    TK_R
2f387 45 46 45 52 45 4e 43 45 53 2c 20 54 4b 5f 43 4f  EFERENCES, TK_CO
2f388 4e 53 54 52 41 49 4e 54 2c 20 54 4b 5f 49 4e 54  NSTRAINT, TK_INT
2f389 4f 2c 20 20 20 20 20 20 20 54 4b 5f 4f 46 46 53  O,       TK_OFFS
2f38a 45 54 2c 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  ET,     .    TK_
2f38b 4f 46 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 53  OF,         TK_S
2f38c 45 54 2c 20 20 20 20 20 20 20 20 54 4b 5f 54 45  ET,        TK_TE
2f38d 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f 54 45 4d  MP,       TK_TEM
2f38e 50 2c 20 20 20 20 20 20 20 54 4b 5f 4f 52 2c 20  P,       TK_OR, 
2f38f 20 20 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f          .    TK_
2f390 55 4e 49 51 55 45 2c 20 20 20 20 20 54 4b 5f 51  UNIQUE,     TK_Q
2f391 55 45 52 59 2c 20 20 20 20 20 20 54 4b 5f 41 54  UERY,      TK_AT
2f392 54 41 43 48 2c 20 20 20 20 20 54 4b 5f 48 41 56  TACH,     TK_HAV
2f393 49 4e 47 2c 20 20 20 20 20 54 4b 5f 47 52 4f 55  ING,     TK_GROU
2f394 50 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  P,      .    TK_
2f395 55 50 44 41 54 45 2c 20 20 20 20 20 54 4b 5f 42  UPDATE,     TK_B
2f396 45 47 49 4e 2c 20 20 20 20 20 20 54 4b 5f 4a 4f  EGIN,      TK_JO
2f397 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 52 45 4c  IN_KW,    TK_REL
2f398 45 41 53 45 2c 20 20 20 20 54 4b 5f 42 45 54 57  EASE,    TK_BETW
2f399 45 45 4e 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f  EEN,    .    TK_
2f39a 4e 4f 54 4e 55 4c 4c 2c 20 20 20 20 54 4b 5f 4e  NOTNULL,    TK_N
2f39b 4f 54 2c 20 20 20 20 20 20 20 20 54 4b 5f 4e 55  OT,        TK_NU
2f39c 4c 4c 2c 20 20 20 20 20 20 20 54 4b 5f 4c 49 4b  LL,       TK_LIK
2f39d 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 43 41 53 43  E_KW,    TK_CASC
2f39e 41 44 45 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f  ADE,    .    TK_
2f39f 41 53 43 2c 20 20 20 20 20 20 20 20 54 4b 5f 44  ASC,        TK_D
2f3a0 45 4c 45 54 45 2c 20 20 20 20 20 54 4b 5f 43 41  ELETE,     TK_CA
2f3a1 53 45 2c 20 20 20 20 20 20 20 54 4b 5f 43 4f 4c  SE,       TK_COL
2f3a2 4c 41 54 45 2c 20 20 20 20 54 4b 5f 43 52 45 41  LATE,    TK_CREA
2f3a3 54 45 2c 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  TE,     .    TK_
2f3a4 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f 44  CTIME_KW,   TK_D
2f3a5 45 54 41 43 48 2c 20 20 20 20 20 54 4b 5f 49 4d  ETACH,     TK_IM
2f3a6 4d 45 44 49 41 54 45 2c 20 20 54 4b 5f 4a 4f 49  MEDIATE,  TK_JOI
2f3a7 4e 2c 20 20 20 20 20 20 20 54 4b 5f 49 4e 53 45  N,       TK_INSE
2f3a8 52 54 2c 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  RT,     .    TK_
2f3a9 4d 41 54 43 48 2c 20 20 20 20 20 20 54 4b 5f 50  MATCH,      TK_P
2f3aa 4c 41 4e 2c 20 20 20 20 20 20 20 54 4b 5f 41 4e  LAN,       TK_AN
2f3ab 41 4c 59 5a 45 2c 20 20 20 20 54 4b 5f 50 52 41  ALYZE,    TK_PRA
2f3ac 47 4d 41 2c 20 20 20 20 20 54 4b 5f 41 42 4f 52  GMA,     TK_ABOR
2f3ad 54 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  T,      .    TK_
2f3ae 56 41 4c 55 45 53 2c 20 20 20 20 20 54 4b 5f 56  VALUES,     TK_V
2f3af 49 52 54 55 41 4c 2c 20 20 20 20 54 4b 5f 4c 49  IRTUAL,    TK_LI
2f3b0 4d 49 54 2c 20 20 20 20 20 20 54 4b 5f 57 48 45  MIT,      TK_WHE
2f3b1 4e 2c 20 20 20 20 20 20 20 54 4b 5f 57 48 45 52  N,       TK_WHER
2f3b2 45 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  E,      .    TK_
2f3b3 52 45 4e 41 4d 45 2c 20 20 20 20 20 54 4b 5f 41  RENAME,     TK_A
2f3b4 46 54 45 52 2c 20 20 20 20 20 20 54 4b 5f 52 45  FTER,      TK_RE
2f3b5 50 4c 41 43 45 2c 20 20 20 20 54 4b 5f 41 4e 44  PLACE,    TK_AND
2f3b6 2c 20 20 20 20 20 20 20 20 54 4b 5f 44 45 46 41  ,        TK_DEFA
2f3b7 55 4c 54 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f  ULT,    .    TK_
2f3b8 41 55 54 4f 49 4e 43 52 2c 20 20 20 54 4b 5f 54  AUTOINCR,   TK_T
2f3b9 4f 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 49 4e  O,         TK_IN
2f3ba 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 43 41 53  ,         TK_CAS
2f3bb 54 2c 20 20 20 20 20 20 20 54 4b 5f 43 4f 4c 55  T,       TK_COLU
2f3bc 4d 4e 4b 57 2c 20 20 20 0a 20 20 20 20 54 4b 5f  MNKW,   .    TK_
2f3bd 43 4f 4d 4d 49 54 2c 20 20 20 20 20 54 4b 5f 43  COMMIT,     TK_C
2f3be 4f 4e 46 4c 49 43 54 2c 20 20 20 54 4b 5f 4a 4f  ONFLICT,   TK_JO
2f3bf 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 43 54 49  IN_KW,    TK_CTI
2f3c0 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f 43 54 49 4d  ME_KW,   TK_CTIM
2f3c1 45 5f 4b 57 2c 20 20 20 0a 20 20 20 20 54 4b 5f  E_KW,   .    TK_
2f3c2 50 52 49 4d 41 52 59 2c 20 20 20 20 54 4b 5f 44  PRIMARY,    TK_D
2f3c3 45 46 45 52 52 45 44 2c 20 20 20 54 4b 5f 44 49  EFERRED,   TK_DI
2f3c4 53 54 49 4e 43 54 2c 20 20 20 54 4b 5f 49 53 2c  STINCT,   TK_IS,
2f3c5 20 20 20 20 20 20 20 20 20 54 4b 5f 44 52 4f 50           TK_DROP
2f3c6 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  ,       .    TK_
2f3c7 46 41 49 4c 2c 20 20 20 20 20 20 20 54 4b 5f 46  FAIL,       TK_F
2f3c8 52 4f 4d 2c 20 20 20 20 20 20 20 54 4b 5f 4a 4f  ROM,       TK_JO
2f3c9 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 4c 49 4b  IN_KW,    TK_LIK
2f3ca 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 42 59 2c 20  E_KW,    TK_BY, 
2f3cb 20 20 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f          .    TK_
2f3cc 49 46 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 49  IF,         TK_I
2f3cd 53 4e 55 4c 4c 2c 20 20 20 20 20 54 4b 5f 4f 52  SNULL,     TK_OR
2f3ce 44 45 52 2c 20 20 20 20 20 20 54 4b 5f 52 45 53  DER,      TK_RES
2f3cf 54 52 49 43 54 2c 20 20 20 54 4b 5f 4a 4f 49 4e  TRICT,   TK_JOIN
2f3d0 5f 4b 57 2c 20 20 20 20 0a 20 20 20 20 54 4b 5f  _KW,    .    TK_
2f3d1 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 52  JOIN_KW,    TK_R
2f3d2 4f 4c 4c 42 41 43 4b 2c 20 20 20 54 4b 5f 52 4f  OLLBACK,   TK_RO
2f3d3 57 2c 20 20 20 20 20 20 20 20 54 4b 5f 55 4e 49  W,        TK_UNI
2f3d4 4f 4e 2c 20 20 20 20 20 20 54 4b 5f 55 53 49 4e  ON,      TK_USIN
2f3d5 47 2c 20 20 20 20 20 20 0a 20 20 20 20 54 4b 5f  G,      .    TK_
2f3d6 56 41 43 55 55 4d 2c 20 20 20 20 20 54 4b 5f 56  VACUUM,     TK_V
2f3d7 49 45 57 2c 20 20 20 20 20 20 20 54 4b 5f 49 4e  IEW,       TK_IN
2f3d8 49 54 49 41 4c 4c 59 2c 20 20 54 4b 5f 41 4c 4c  ITIALLY,  TK_ALL
2f3d9 2c 20 20 20 20 20 20 20 20 0a 20 20 7d 3b 0a 20  ,        .  };. 
2f3da 20 69 6e 74 20 68 2c 20 69 3b 0a 20 20 69 66 28   int h, i;.  if(
2f3db 20 6e 3c 32 20 29 20 72 65 74 75 72 6e 20 54 4b   n<2 ) return TK
2f3dc 5f 49 44 3b 0a 20 20 68 20 3d 20 28 28 63 68 61  _ID;.  h = ((cha
2f3dd 72 4d 61 70 28 7a 5b 30 5d 29 2a 34 29 20 5e 0a  rMap(z[0])*4) ^.
2f3de 20 20 20 20 20 20 28 63 68 61 72 4d 61 70 28 7a        (charMap(z
2f3df 5b 6e 2d 31 5d 29 2a 33 29 20 5e 0a 20 20 20 20  [n-1])*3) ^.    
2f3e0 20 20 6e 29 20 25 20 31 32 37 3b 0a 20 20 66 6f    n) % 127;.  fo
2f3e1 72 28 69 3d 28 28 69 6e 74 29 61 48 61 73 68 5b  r(i=((int)aHash[
2f3e2 68 5d 29 2d 31 3b 20 69 3e 3d 30 3b 20 69 3d 28  h])-1; i>=0; i=(
2f3e3 28 69 6e 74 29 61 4e 65 78 74 5b 69 5d 29 2d 31  (int)aNext[i])-1
2f3e4 29 7b 0a 20 20 20 20 69 66 28 20 61 4c 65 6e 5b  ){.    if( aLen[
2f3e5 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33  i]==n && sqlite3
2f3e6 53 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b  StrNICmp(&zText[
2f3e7 61 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29  aOffset[i]],z,n)
2f3e8 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 74 65 73  ==0 ){.      tes
2f3e9 74 63 61 73 65 28 20 69 3d 3d 30 20 29 3b 20 2f  tcase( i==0 ); /
2f3ea 2a 20 54 4b 5f 52 45 49 4e 44 45 58 20 2a 2f 0a  * TK_REINDEX */.
2f3eb 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f3ec 69 3d 3d 31 20 29 3b 20 2f 2a 20 54 4b 5f 49 4e  i==1 ); /* TK_IN
2f3ed 44 45 58 45 44 20 2a 2f 0a 20 20 20 20 20 20 74  DEXED */.      t
2f3ee 65 73 74 63 61 73 65 28 20 69 3d 3d 32 20 29 3b  estcase( i==2 );
2f3ef 20 2f 2a 20 54 4b 5f 49 4e 44 45 58 20 2a 2f 0a   /* TK_INDEX */.
2f3f0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f3f1 69 3d 3d 33 20 29 3b 20 2f 2a 20 54 4b 5f 44 45  i==3 ); /* TK_DE
2f3f2 53 43 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  SC */.      test
2f3f3 63 61 73 65 28 20 69 3d 3d 34 20 29 3b 20 2f 2a  case( i==4 ); /*
2f3f4 20 54 4b 5f 45 53 43 41 50 45 20 2a 2f 0a 20 20   TK_ESCAPE */.  
2f3f5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f3f6 3d 35 20 29 3b 20 2f 2a 20 54 4b 5f 45 41 43 48  =5 ); /* TK_EACH
2f3f7 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f3f8 73 65 28 20 69 3d 3d 36 20 29 3b 20 2f 2a 20 54  se( i==6 ); /* T
2f3f9 4b 5f 43 48 45 43 4b 20 2a 2f 0a 20 20 20 20 20  K_CHECK */.     
2f3fa 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 20   testcase( i==7 
2f3fb 29 3b 20 2f 2a 20 54 4b 5f 4b 45 59 20 2a 2f 0a  ); /* TK_KEY */.
2f3fc 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f3fd 69 3d 3d 38 20 29 3b 20 2f 2a 20 54 4b 5f 42 45  i==8 ); /* TK_BE
2f3fe 46 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  FORE */.      te
2f3ff 73 74 63 61 73 65 28 20 69 3d 3d 39 20 29 3b 20  stcase( i==9 ); 
2f400 2f 2a 20 54 4b 5f 46 4f 52 45 49 47 4e 20 2a 2f  /* TK_FOREIGN */
2f401 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f402 20 69 3d 3d 31 30 20 29 3b 20 2f 2a 20 54 4b 5f   i==10 ); /* TK_
2f403 46 4f 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  FOR */.      tes
2f404 74 63 61 73 65 28 20 69 3d 3d 31 31 20 29 3b 20  tcase( i==11 ); 
2f405 2f 2a 20 54 4b 5f 49 47 4e 4f 52 45 20 2a 2f 0a  /* TK_IGNORE */.
2f406 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f407 69 3d 3d 31 32 20 29 3b 20 2f 2a 20 54 4b 5f 4c  i==12 ); /* TK_L
2f408 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  IKE_KW */.      
2f409 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 33 20  testcase( i==13 
2f40a 29 3b 20 2f 2a 20 54 4b 5f 45 58 50 4c 41 49 4e  ); /* TK_EXPLAIN
2f40b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f40c 73 65 28 20 69 3d 3d 31 34 20 29 3b 20 2f 2a 20  se( i==14 ); /* 
2f40d 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20  TK_INSTEAD */.  
2f40e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f40f 3d 31 35 20 29 3b 20 2f 2a 20 54 4b 5f 41 44 44  =15 ); /* TK_ADD
2f410 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f411 73 65 28 20 69 3d 3d 31 36 20 29 3b 20 2f 2a 20  se( i==16 ); /* 
2f412 54 4b 5f 44 41 54 41 42 41 53 45 20 2a 2f 0a 20  TK_DATABASE */. 
2f413 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f414 3d 3d 31 37 20 29 3b 20 2f 2a 20 54 4b 5f 41 53  ==17 ); /* TK_AS
2f415 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f416 73 65 28 20 69 3d 3d 31 38 20 29 3b 20 2f 2a 20  se( i==18 ); /* 
2f417 54 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20  TK_SELECT */.   
2f418 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f419 31 39 20 29 3b 20 2f 2a 20 54 4b 5f 54 41 42 4c  19 ); /* TK_TABL
2f41a 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
2f41b 61 73 65 28 20 69 3d 3d 32 30 20 29 3b 20 2f 2a  ase( i==20 ); /*
2f41c 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 20   TK_JOIN_KW */. 
2f41d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f41e 3d 3d 32 31 20 29 3b 20 2f 2a 20 54 4b 5f 54 48  ==21 ); /* TK_TH
2f41f 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  EN */.      test
2f420 63 61 73 65 28 20 69 3d 3d 32 32 20 29 3b 20 2f  case( i==22 ); /
2f421 2a 20 54 4b 5f 45 4e 44 20 2a 2f 0a 20 20 20 20  * TK_END */.    
2f422 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32    testcase( i==2
2f423 33 20 29 3b 20 2f 2a 20 54 4b 5f 44 45 46 45 52  3 ); /* TK_DEFER
2f424 52 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20 74  RABLE */.      t
2f425 65 73 74 63 61 73 65 28 20 69 3d 3d 32 34 20 29  estcase( i==24 )
2f426 3b 20 2f 2a 20 54 4b 5f 45 4c 53 45 20 2a 2f 0a  ; /* TK_ELSE */.
2f427 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f428 69 3d 3d 32 35 20 29 3b 20 2f 2a 20 54 4b 5f 45  i==25 ); /* TK_E
2f429 58 43 45 50 54 20 2a 2f 0a 20 20 20 20 20 20 74  XCEPT */.      t
2f42a 65 73 74 63 61 73 65 28 20 69 3d 3d 32 36 20 29  estcase( i==26 )
2f42b 3b 20 2f 2a 20 54 4b 5f 54 52 41 4e 53 41 43 54  ; /* TK_TRANSACT
2f42c 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ION */.      tes
2f42d 74 63 61 73 65 28 20 69 3d 3d 32 37 20 29 3b 20  tcase( i==27 ); 
2f42e 2f 2a 20 54 4b 5f 4f 4e 20 2a 2f 0a 20 20 20 20  /* TK_ON */.    
2f42f 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32    testcase( i==2
2f430 38 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f  8 ); /* TK_JOIN_
2f431 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  KW */.      test
2f432 63 61 73 65 28 20 69 3d 3d 32 39 20 29 3b 20 2f  case( i==29 ); /
2f433 2a 20 54 4b 5f 41 4c 54 45 52 20 2a 2f 0a 20 20  * TK_ALTER */.  
2f434 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f435 3d 33 30 20 29 3b 20 2f 2a 20 54 4b 5f 52 41 49  =30 ); /* TK_RAI
2f436 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  SE */.      test
2f437 63 61 73 65 28 20 69 3d 3d 33 31 20 29 3b 20 2f  case( i==31 ); /
2f438 2a 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 20 2a  * TK_EXCLUSIVE *
2f439 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f43a 28 20 69 3d 3d 33 32 20 29 3b 20 2f 2a 20 54 4b  ( i==32 ); /* TK
2f43b 5f 45 58 49 53 54 53 20 2a 2f 0a 20 20 20 20 20  _EXISTS */.     
2f43c 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 33   testcase( i==33
2f43d 20 29 3b 20 2f 2a 20 54 4b 5f 53 41 56 45 50 4f   ); /* TK_SAVEPO
2f43e 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  INT */.      tes
2f43f 74 63 61 73 65 28 20 69 3d 3d 33 34 20 29 3b 20  tcase( i==34 ); 
2f440 2f 2a 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20  /* TK_INTERSECT 
2f441 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f442 65 28 20 69 3d 3d 33 35 20 29 3b 20 2f 2a 20 54  e( i==35 ); /* T
2f443 4b 5f 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 20  K_TRIGGER */.   
2f444 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f445 33 36 20 29 3b 20 2f 2a 20 54 4b 5f 52 45 46 45  36 ); /* TK_REFE
2f446 52 45 4e 43 45 53 20 2a 2f 0a 20 20 20 20 20 20  RENCES */.      
2f447 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 37 20  testcase( i==37 
2f448 29 3b 20 2f 2a 20 54 4b 5f 43 4f 4e 53 54 52 41  ); /* TK_CONSTRA
2f449 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  INT */.      tes
2f44a 74 63 61 73 65 28 20 69 3d 3d 33 38 20 29 3b 20  tcase( i==38 ); 
2f44b 2f 2a 20 54 4b 5f 49 4e 54 4f 20 2a 2f 0a 20 20  /* TK_INTO */.  
2f44c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f44d 3d 33 39 20 29 3b 20 2f 2a 20 54 4b 5f 4f 46 46  =39 ); /* TK_OFF
2f44e 53 45 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  SET */.      tes
2f44f 74 63 61 73 65 28 20 69 3d 3d 34 30 20 29 3b 20  tcase( i==40 ); 
2f450 2f 2a 20 54 4b 5f 4f 46 20 2a 2f 0a 20 20 20 20  /* TK_OF */.    
2f451 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
2f452 31 20 29 3b 20 2f 2a 20 54 4b 5f 53 45 54 20 2a  1 ); /* TK_SET *
2f453 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f454 28 20 69 3d 3d 34 32 20 29 3b 20 2f 2a 20 54 4b  ( i==42 ); /* TK
2f455 5f 54 45 4d 50 20 2a 2f 0a 20 20 20 20 20 20 74  _TEMP */.      t
2f456 65 73 74 63 61 73 65 28 20 69 3d 3d 34 33 20 29  estcase( i==43 )
2f457 3b 20 2f 2a 20 54 4b 5f 54 45 4d 50 20 2a 2f 0a  ; /* TK_TEMP */.
2f458 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f459 69 3d 3d 34 34 20 29 3b 20 2f 2a 20 54 4b 5f 4f  i==44 ); /* TK_O
2f45a 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  R */.      testc
2f45b 61 73 65 28 20 69 3d 3d 34 35 20 29 3b 20 2f 2a  ase( i==45 ); /*
2f45c 20 54 4b 5f 55 4e 49 51 55 45 20 2a 2f 0a 20 20   TK_UNIQUE */.  
2f45d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f45e 3d 34 36 20 29 3b 20 2f 2a 20 54 4b 5f 51 55 45  =46 ); /* TK_QUE
2f45f 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RY */.      test
2f460 63 61 73 65 28 20 69 3d 3d 34 37 20 29 3b 20 2f  case( i==47 ); /
2f461 2a 20 54 4b 5f 41 54 54 41 43 48 20 2a 2f 0a 20  * TK_ATTACH */. 
2f462 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f463 3d 3d 34 38 20 29 3b 20 2f 2a 20 54 4b 5f 48 41  ==48 ); /* TK_HA
2f464 56 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65  VING */.      te
2f465 73 74 63 61 73 65 28 20 69 3d 3d 34 39 20 29 3b  stcase( i==49 );
2f466 20 2f 2a 20 54 4b 5f 47 52 4f 55 50 20 2a 2f 0a   /* TK_GROUP */.
2f467 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f468 69 3d 3d 35 30 20 29 3b 20 2f 2a 20 54 4b 5f 55  i==50 ); /* TK_U
2f469 50 44 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74  PDATE */.      t
2f46a 65 73 74 63 61 73 65 28 20 69 3d 3d 35 31 20 29  estcase( i==51 )
2f46b 3b 20 2f 2a 20 54 4b 5f 42 45 47 49 4e 20 2a 2f  ; /* TK_BEGIN */
2f46c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f46d 20 69 3d 3d 35 32 20 29 3b 20 2f 2a 20 54 4b 5f   i==52 ); /* TK_
2f46e 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 20 20 20 20 20  JOIN_KW */.     
2f46f 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 33   testcase( i==53
2f470 20 29 3b 20 2f 2a 20 54 4b 5f 52 45 4c 45 41 53   ); /* TK_RELEAS
2f471 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
2f472 61 73 65 28 20 69 3d 3d 35 34 20 29 3b 20 2f 2a  ase( i==54 ); /*
2f473 20 54 4b 5f 42 45 54 57 45 45 4e 20 2a 2f 0a 20   TK_BETWEEN */. 
2f474 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f475 3d 3d 35 35 20 29 3b 20 2f 2a 20 54 4b 5f 4e 4f  ==55 ); /* TK_NO
2f476 54 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74  TNULL */.      t
2f477 65 73 74 63 61 73 65 28 20 69 3d 3d 35 36 20 29  estcase( i==56 )
2f478 3b 20 2f 2a 20 54 4b 5f 4e 4f 54 20 2a 2f 0a 20  ; /* TK_NOT */. 
2f479 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f47a 3d 3d 35 37 20 29 3b 20 2f 2a 20 54 4b 5f 4e 55  ==57 ); /* TK_NU
2f47b 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  LL */.      test
2f47c 63 61 73 65 28 20 69 3d 3d 35 38 20 29 3b 20 2f  case( i==58 ); /
2f47d 2a 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 2a 2f 0a  * TK_LIKE_KW */.
2f47e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f47f 69 3d 3d 35 39 20 29 3b 20 2f 2a 20 54 4b 5f 43  i==59 ); /* TK_C
2f480 41 53 43 41 44 45 20 2a 2f 0a 20 20 20 20 20 20  ASCADE */.      
2f481 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 30 20  testcase( i==60 
2f482 29 3b 20 2f 2a 20 54 4b 5f 41 53 43 20 2a 2f 0a  ); /* TK_ASC */.
2f483 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f484 69 3d 3d 36 31 20 29 3b 20 2f 2a 20 54 4b 5f 44  i==61 ); /* TK_D
2f485 45 4c 45 54 45 20 2a 2f 0a 20 20 20 20 20 20 74  ELETE */.      t
2f486 65 73 74 63 61 73 65 28 20 69 3d 3d 36 32 20 29  estcase( i==62 )
2f487 3b 20 2f 2a 20 54 4b 5f 43 41 53 45 20 2a 2f 0a  ; /* TK_CASE */.
2f488 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f489 69 3d 3d 36 33 20 29 3b 20 2f 2a 20 54 4b 5f 43  i==63 ); /* TK_C
2f48a 4f 4c 4c 41 54 45 20 2a 2f 0a 20 20 20 20 20 20  OLLATE */.      
2f48b 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 34 20  testcase( i==64 
2f48c 29 3b 20 2f 2a 20 54 4b 5f 43 52 45 41 54 45 20  ); /* TK_CREATE 
2f48d 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f48e 65 28 20 69 3d 3d 36 35 20 29 3b 20 2f 2a 20 54  e( i==65 ); /* T
2f48f 4b 5f 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 20 20  K_CTIME_KW */.  
2f490 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f491 3d 36 36 20 29 3b 20 2f 2a 20 54 4b 5f 44 45 54  =66 ); /* TK_DET
2f492 41 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ACH */.      tes
2f493 74 63 61 73 65 28 20 69 3d 3d 36 37 20 29 3b 20  tcase( i==67 ); 
2f494 2f 2a 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20  /* TK_IMMEDIATE 
2f495 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f496 65 28 20 69 3d 3d 36 38 20 29 3b 20 2f 2a 20 54  e( i==68 ); /* T
2f497 4b 5f 4a 4f 49 4e 20 2a 2f 0a 20 20 20 20 20 20  K_JOIN */.      
2f498 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 39 20  testcase( i==69 
2f499 29 3b 20 2f 2a 20 54 4b 5f 49 4e 53 45 52 54 20  ); /* TK_INSERT 
2f49a 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f49b 65 28 20 69 3d 3d 37 30 20 29 3b 20 2f 2a 20 54  e( i==70 ); /* T
2f49c 4b 5f 4d 41 54 43 48 20 2a 2f 0a 20 20 20 20 20  K_MATCH */.     
2f49d 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 31   testcase( i==71
2f49e 20 29 3b 20 2f 2a 20 54 4b 5f 50 4c 41 4e 20 2a   ); /* TK_PLAN *
2f49f 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4a0 28 20 69 3d 3d 37 32 20 29 3b 20 2f 2a 20 54 4b  ( i==72 ); /* TK
2f4a1 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a 20 20 20 20  _ANALYZE */.    
2f4a2 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
2f4a3 33 20 29 3b 20 2f 2a 20 54 4b 5f 50 52 41 47 4d  3 ); /* TK_PRAGM
2f4a4 41 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  A */.      testc
2f4a5 61 73 65 28 20 69 3d 3d 37 34 20 29 3b 20 2f 2a  ase( i==74 ); /*
2f4a6 20 54 4b 5f 41 42 4f 52 54 20 2a 2f 0a 20 20 20   TK_ABORT */.   
2f4a7 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f4a8 37 35 20 29 3b 20 2f 2a 20 54 4b 5f 56 41 4c 55  75 ); /* TK_VALU
2f4a9 45 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ES */.      test
2f4aa 63 61 73 65 28 20 69 3d 3d 37 36 20 29 3b 20 2f  case( i==76 ); /
2f4ab 2a 20 54 4b 5f 56 49 52 54 55 41 4c 20 2a 2f 0a  * TK_VIRTUAL */.
2f4ac 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f4ad 69 3d 3d 37 37 20 29 3b 20 2f 2a 20 54 4b 5f 4c  i==77 ); /* TK_L
2f4ae 49 4d 49 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  IMIT */.      te
2f4af 73 74 63 61 73 65 28 20 69 3d 3d 37 38 20 29 3b  stcase( i==78 );
2f4b0 20 2f 2a 20 54 4b 5f 57 48 45 4e 20 2a 2f 0a 20   /* TK_WHEN */. 
2f4b1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f4b2 3d 3d 37 39 20 29 3b 20 2f 2a 20 54 4b 5f 57 48  ==79 ); /* TK_WH
2f4b3 45 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ERE */.      tes
2f4b4 74 63 61 73 65 28 20 69 3d 3d 38 30 20 29 3b 20  tcase( i==80 ); 
2f4b5 2f 2a 20 54 4b 5f 52 45 4e 41 4d 45 20 2a 2f 0a  /* TK_RENAME */.
2f4b6 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f4b7 69 3d 3d 38 31 20 29 3b 20 2f 2a 20 54 4b 5f 41  i==81 ); /* TK_A
2f4b8 46 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65  FTER */.      te
2f4b9 73 74 63 61 73 65 28 20 69 3d 3d 38 32 20 29 3b  stcase( i==82 );
2f4ba 20 2f 2a 20 54 4b 5f 52 45 50 4c 41 43 45 20 2a   /* TK_REPLACE *
2f4bb 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4bc 28 20 69 3d 3d 38 33 20 29 3b 20 2f 2a 20 54 4b  ( i==83 ); /* TK
2f4bd 5f 41 4e 44 20 2a 2f 0a 20 20 20 20 20 20 74 65  _AND */.      te
2f4be 73 74 63 61 73 65 28 20 69 3d 3d 38 34 20 29 3b  stcase( i==84 );
2f4bf 20 2f 2a 20 54 4b 5f 44 45 46 41 55 4c 54 20 2a   /* TK_DEFAULT *
2f4c0 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4c1 28 20 69 3d 3d 38 35 20 29 3b 20 2f 2a 20 54 4b  ( i==85 ); /* TK
2f4c2 5f 41 55 54 4f 49 4e 43 52 20 2a 2f 0a 20 20 20  _AUTOINCR */.   
2f4c3 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f4c4 38 36 20 29 3b 20 2f 2a 20 54 4b 5f 54 4f 20 2a  86 ); /* TK_TO *
2f4c5 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4c6 28 20 69 3d 3d 38 37 20 29 3b 20 2f 2a 20 54 4b  ( i==87 ); /* TK
2f4c7 5f 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  _IN */.      tes
2f4c8 74 63 61 73 65 28 20 69 3d 3d 38 38 20 29 3b 20  tcase( i==88 ); 
2f4c9 2f 2a 20 54 4b 5f 43 41 53 54 20 2a 2f 0a 20 20  /* TK_CAST */.  
2f4ca 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f4cb 3d 38 39 20 29 3b 20 2f 2a 20 54 4b 5f 43 4f 4c  =89 ); /* TK_COL
2f4cc 55 4d 4e 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74  UMNKW */.      t
2f4cd 65 73 74 63 61 73 65 28 20 69 3d 3d 39 30 20 29  estcase( i==90 )
2f4ce 3b 20 2f 2a 20 54 4b 5f 43 4f 4d 4d 49 54 20 2a  ; /* TK_COMMIT *
2f4cf 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4d0 28 20 69 3d 3d 39 31 20 29 3b 20 2f 2a 20 54 4b  ( i==91 ); /* TK
2f4d1 5f 43 4f 4e 46 4c 49 43 54 20 2a 2f 0a 20 20 20  _CONFLICT */.   
2f4d2 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f4d3 39 32 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e  92 ); /* TK_JOIN
2f4d4 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  _KW */.      tes
2f4d5 74 63 61 73 65 28 20 69 3d 3d 39 33 20 29 3b 20  tcase( i==93 ); 
2f4d6 2f 2a 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 2a  /* TK_CTIME_KW *
2f4d7 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4d8 28 20 69 3d 3d 39 34 20 29 3b 20 2f 2a 20 54 4b  ( i==94 ); /* TK
2f4d9 5f 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 20 20 20  _CTIME_KW */.   
2f4da 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f4db 39 35 20 29 3b 20 2f 2a 20 54 4b 5f 50 52 49 4d  95 ); /* TK_PRIM
2f4dc 41 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ARY */.      tes
2f4dd 74 63 61 73 65 28 20 69 3d 3d 39 36 20 29 3b 20  tcase( i==96 ); 
2f4de 2f 2a 20 54 4b 5f 44 45 46 45 52 52 45 44 20 2a  /* TK_DEFERRED *
2f4df 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4e0 28 20 69 3d 3d 39 37 20 29 3b 20 2f 2a 20 54 4b  ( i==97 ); /* TK
2f4e1 5f 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 20  _DISTINCT */.   
2f4e2 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f4e3 39 38 20 29 3b 20 2f 2a 20 54 4b 5f 49 53 20 2a  98 ); /* TK_IS *
2f4e4 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4e5 28 20 69 3d 3d 39 39 20 29 3b 20 2f 2a 20 54 4b  ( i==99 ); /* TK
2f4e6 5f 44 52 4f 50 20 2a 2f 0a 20 20 20 20 20 20 74  _DROP */.      t
2f4e7 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 30 20  estcase( i==100 
2f4e8 29 3b 20 2f 2a 20 54 4b 5f 46 41 49 4c 20 2a 2f  ); /* TK_FAIL */
2f4e9 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f4ea 20 69 3d 3d 31 30 31 20 29 3b 20 2f 2a 20 54 4b   i==101 ); /* TK
2f4eb 5f 46 52 4f 4d 20 2a 2f 0a 20 20 20 20 20 20 74  _FROM */.      t
2f4ec 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 32 20  estcase( i==102 
2f4ed 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b 57  ); /* TK_JOIN_KW
2f4ee 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f4ef 73 65 28 20 69 3d 3d 31 30 33 20 29 3b 20 2f 2a  se( i==103 ); /*
2f4f0 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20   TK_LIKE_KW */. 
2f4f1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f4f2 3d 3d 31 30 34 20 29 3b 20 2f 2a 20 54 4b 5f 42  ==104 ); /* TK_B
2f4f3 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  Y */.      testc
2f4f4 61 73 65 28 20 69 3d 3d 31 30 35 20 29 3b 20 2f  ase( i==105 ); /
2f4f5 2a 20 54 4b 5f 49 46 20 2a 2f 0a 20 20 20 20 20  * TK_IF */.     
2f4f6 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
2f4f7 36 20 29 3b 20 2f 2a 20 54 4b 5f 49 53 4e 55 4c  6 ); /* TK_ISNUL
2f4f8 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  L */.      testc
2f4f9 61 73 65 28 20 69 3d 3d 31 30 37 20 29 3b 20 2f  ase( i==107 ); /
2f4fa 2a 20 54 4b 5f 4f 52 44 45 52 20 2a 2f 0a 20 20  * TK_ORDER */.  
2f4fb 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f4fc 3d 31 30 38 20 29 3b 20 2f 2a 20 54 4b 5f 52 45  =108 ); /* TK_RE
2f4fd 53 54 52 49 43 54 20 2a 2f 0a 20 20 20 20 20 20  STRICT */.      
2f4fe 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 39  testcase( i==109
2f4ff 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b   ); /* TK_JOIN_K
2f500 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  W */.      testc
2f501 61 73 65 28 20 69 3d 3d 31 31 30 20 29 3b 20 2f  ase( i==110 ); /
2f502 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a  * TK_JOIN_KW */.
2f503 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f504 69 3d 3d 31 31 31 20 29 3b 20 2f 2a 20 54 4b 5f  i==111 ); /* TK_
2f505 52 4f 4c 4c 42 41 43 4b 20 2a 2f 0a 20 20 20 20  ROLLBACK */.    
2f506 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
2f507 31 32 20 29 3b 20 2f 2a 20 54 4b 5f 52 4f 57 20  12 ); /* TK_ROW 
2f508 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f509 65 28 20 69 3d 3d 31 31 33 20 29 3b 20 2f 2a 20  e( i==113 ); /* 
2f50a 54 4b 5f 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20 20  TK_UNION */.    
2f50b 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
2f50c 31 34 20 29 3b 20 2f 2a 20 54 4b 5f 55 53 49 4e  14 ); /* TK_USIN
2f50d 47 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  G */.      testc
2f50e 61 73 65 28 20 69 3d 3d 31 31 35 20 29 3b 20 2f  ase( i==115 ); /
2f50f 2a 20 54 4b 5f 56 41 43 55 55 4d 20 2a 2f 0a 20  * TK_VACUUM */. 
2f510 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f511 3d 3d 31 31 36 20 29 3b 20 2f 2a 20 54 4b 5f 56  ==116 ); /* TK_V
2f512 49 45 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  IEW */.      tes
2f513 74 63 61 73 65 28 20 69 3d 3d 31 31 37 20 29 3b  tcase( i==117 );
2f514 20 2f 2a 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59   /* TK_INITIALLY
2f515 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f516 73 65 28 20 69 3d 3d 31 31 38 20 29 3b 20 2f 2a  se( i==118 ); /*
2f517 20 54 4b 5f 41 4c 4c 20 2a 2f 0a 20 20 20 20 20   TK_ALL */.     
2f518 20 72 65 74 75 72 6e 20 61 43 6f 64 65 5b 69 5d   return aCode[i]
2f519 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2f51a 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d 0a 53 51  turn TK_ID;.}.SQ
2f51b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2f51c 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43   sqlite3KeywordC
2f51d 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ode(const unsign
2f51e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  ed char *z, int 
2f51f 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 6b 65 79  n){.  return key
2f520 77 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a 29  wordCode((char*)
2f521 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  z, n);.}../*****
2f522 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2f523 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 2a   keywordhash.h *
2f524 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f525 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f526 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2f527 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
2f528 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
2f529 66 74 20 6f 66 66 20 69 6e 20 74 6f 6b 65 6e 69  ft off in tokeni
2f52a 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ze.c ***********
2f52b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a  ********/.../*.*
2f52c 2a 20 49 66 20 58 20 69 73 20 61 20 63 68 61 72  * If X is a char
2f52d 61 63 74 65 72 20 74 68 61 74 20 63 61 6e 20 62  acter that can b
2f52e 65 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64 65  e used in an ide
2f52f 6e 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a 20  ntifier then.** 
2f530 49 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20 62  IdChar(X) will b
2f531 65 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69  e true.  Otherwi
2f532 73 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a  se it is false..
2f533 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49 2c  **.** For ASCII,
2f534 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77   any character w
2f535 69 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72 64  ith the high-ord
2f536 65 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a 2a  er bit set is.**
2f537 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20 69   allowed in an i
2f538 64 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72 20  dentifier.  For 
2f539 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 73  7-bit characters
2f53a 2c 20 0a 2a 2a 20 73 71 6c 69 74 65 33 49 73 49  , .** sqlite3IsI
2f53b 64 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62 65  dChar[X] must be
2f53c 20 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 45 42   1..**.** For EB
2f53d 43 44 49 43 2c 20 74 68 65 20 72 75 6c 65 73 20  CDIC, the rules 
2f53e 61 72 65 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78  are more complex
2f53f 20 62 75 74 20 68 61 76 65 20 74 68 65 20 73 61   but have the sa
2f540 6d 65 0a 2a 2a 20 65 6e 64 20 72 65 73 75 6c 74  me.** end result
2f541 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23  ..**.** Ticket #
2f542 31 30 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73  1066.  the SQL s
2f543 74 61 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74  tandard does not
2f544 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68   allow '$' in th
2f545 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69  e.** middle of i
2f546 64 65 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20  dentfiers.  But 
2f547 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65  many SQL impleme
2f548 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a  ntations do. .**
2f549 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c   SQLite will all
2f54a 6f 77 20 27 24 27 20 69 6e 20 69 64 65 6e 74 69  ow '$' in identi
2f54b 66 69 65 72 73 20 66 6f 72 20 63 6f 6d 70 61 74  fiers for compat
2f54c 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20  ibility..** But 
2f54d 74 68 65 20 66 65 61 74 75 72 65 20 69 73 20 75  the feature is u
2f54e 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a  ndocumented..*/.
2f54f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
2f550 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  CII.SQLITE_PRIVA
2f551 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  TE const char sq
2f552 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68  lite3IsAsciiIdCh
2f553 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78  ar[] = {./* x0 x
2f554 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36  1 x2 x3 x4 x5 x6
2f555 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20   x7 x8 x9 xA xB 
2f556 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20  xC xD xE xF */. 
2f557 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31     0, 0, 0, 0, 1
2f558 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2f559 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2f55a 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20  0,  /* 2x */.   
2f55b 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f55c 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
2f55d 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2f55e 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30    /* 3x */.    0
2f55f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f560 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f561 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20  1, 1, 1, 1, 1,  
2f562 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20  /* 4x */.    1, 
2f563 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f564 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
2f565 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a   0, 0, 0, 1,  /*
2f566 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c   5x */.    0, 1,
2f567 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f568 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f569 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36  , 1, 1, 1,  /* 6
2f56a 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31  x */.    1, 1, 1
2f56b 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f56c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
2f56d 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20  0, 0, 0,  /* 7x 
2f56e 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 49 64  */.};.#define Id
2f56f 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29  Char(C)  (((c=C)
2f570 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e  &0x80)!=0 || (c>
2f571 30 78 31 66 20 26 26 20 73 71 6c 69 74 65 33 49  0x1f && sqlite3I
2f572 73 41 73 63 69 69 49 64 43 68 61 72 5b 63 2d 30  sAsciiIdChar[c-0
2f573 78 32 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 69  x20])).#endif.#i
2f574 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
2f575 49 43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  IC.SQLITE_PRIVAT
2f576 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c  E const char sql
2f577 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68  ite3IsEbcdicIdCh
2f578 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78  ar[] = {./* x0 x
2f579 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36  1 x2 x3 x4 x5 x6
2f57a 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20   x7 x8 x9 xA xB 
2f57b 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20  xC xD xE xF */. 
2f57c 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31     0, 0, 1, 1, 1
2f57d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f57e 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2f57f 30 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20  0,  /* 4x */.   
2f580 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2f581 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
2f582 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
2f583 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30    /* 5x */.    0
2f584 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
2f585 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
2f586 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 20  0, 0, 1, 0, 0,  
2f587 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* 6x */.    0, 
2f588 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f589 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
2f58a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
2f58b 20 37 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c   7x */.    0, 1,
2f58c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f58d 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31  1, 1, 1, 0, 0, 1
2f58e 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 38  , 1, 1, 0,  /* 8
2f58f 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31  x */.    0, 1, 1
2f590 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f591 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20   1, 1, 0, 0, 1, 
2f592 30 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 39 78 20  0, 1, 0,  /* 9x 
2f593 2a 2f 0a 20 20 20 20 31 2c 20 30 2c 20 31 2c 20  */.    1, 0, 1, 
2f594 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f595 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c  , 1, 1, 0, 1, 1,
2f596 20 31 2c 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f   1, 0,  /* Ax */
2f597 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c  .    0, 0, 0, 0,
2f598 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2f599 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2f59a 2c 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a 20  , 0,  /* Bx */. 
2f59b 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
2f59c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f59d 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2f59e 31 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20  1,  /* Cx */.   
2f59f 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2f5a0 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
2f5a1 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f5a2 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 30    /* Dx */.    0
2f5a3 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
2f5a4 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
2f5a5 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20  1, 1, 1, 1, 1,  
2f5a6 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 31 2c 20  /* Ex */.    1, 
2f5a7 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f5a8 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c  , 1, 1, 1, 0, 1,
2f5a9 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a   1, 1, 1, 0,  /*
2f5aa 20 46 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e   Fx */.};.#defin
2f5ab 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28 28  e IdChar(C)  (((
2f5ac 63 3d 43 29 3e 3d 30 78 34 32 20 26 26 20 73 71  c=C)>=0x42 && sq
2f5ad 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43  lite3IsEbcdicIdC
2f5ae 68 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23 65  har[c-0x40])).#e
2f5af 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ndif.../*.** Ret
2f5b0 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  urn the length o
2f5b1 66 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74  f the token that
2f5b2 20 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e   begins at z[0].
2f5b3 20 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 74   .** Store the t
2f5b4 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f  oken type in *to
2f5b5 6b 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20 72  kenType before r
2f5b6 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  eturning..*/.SQL
2f5b7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2f5b8 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28  sqlite3GetToken(
2f5b9 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2f5ba 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b  har *z, int *tok
2f5bb 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 69  enType){.  int i
2f5bc 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20 2a  , c;.  switch( *
2f5bd 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 20  z ){.    case ' 
2f5be 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20 63 61  ': case '\t': ca
2f5bf 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c  se '\n': case '\
2f5c0 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a 20 7b  f': case '\r': {
2f5c1 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  .      for(i=1; 
2f5c2 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a  sqlite3Isspace(z
2f5c3 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
2f5c4 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2f5c5 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20  TK_SPACE;.      
2f5c6 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
2f5c7 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20 7b 0a      case '-': {.
2f5c8 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 3d 3d        if( z[1]==
2f5c9 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66  '-' ){.        f
2f5ca 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69 5d 29  or(i=2; (c=z[i])
2f5cb 21 3d 30 20 26 26 20 63 21 3d 27 5c 6e 27 3b 20  !=0 && c!='\n'; 
2f5cc 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 2a  i++){}.        *
2f5cd 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
2f5ce 50 41 43 45 3b 0a 20 20 20 20 20 20 20 20 72 65  PACE;.        re
2f5cf 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a  turn i;.      }.
2f5d0 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2f5d1 20 3d 20 54 4b 5f 4d 49 4e 55 53 3b 0a 20 20 20   = TK_MINUS;.   
2f5d2 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
2f5d3 20 7d 0a 20 20 20 20 63 61 73 65 20 27 28 27 3a   }.    case '(':
2f5d4 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
2f5d5 79 70 65 20 3d 20 54 4b 5f 4c 50 3b 0a 20 20 20  ype = TK_LP;.   
2f5d6 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
2f5d7 20 7d 0a 20 20 20 20 63 61 73 65 20 27 29 27 3a   }.    case ')':
2f5d8 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
2f5d9 79 70 65 20 3d 20 54 4b 5f 52 50 3b 0a 20 20 20  ype = TK_RP;.   
2f5da 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
2f5db 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3b 27 3a   }.    case ';':
2f5dc 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
2f5dd 79 70 65 20 3d 20 54 4b 5f 53 45 4d 49 3b 0a 20  ype = TK_SEMI;. 
2f5de 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2f5df 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2b     }.    case '+
2f5e0 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
2f5e1 6e 54 79 70 65 20 3d 20 54 4b 5f 50 4c 55 53 3b  nType = TK_PLUS;
2f5e2 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2f5e3 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2f5e4 27 2a 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '*': {.      *to
2f5e5 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 54 41  kenType = TK_STA
2f5e6 52 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  R;.      return 
2f5e7 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
2f5e8 65 20 27 2f 27 3a 20 7b 0a 20 20 20 20 20 20 69  e '/': {.      i
2f5e9 66 28 20 7a 5b 31 5d 21 3d 27 2a 27 20 7c 7c 20  f( z[1]!='*' || 
2f5ea 7a 5b 32 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  z[2]==0 ){.     
2f5eb 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2f5ec 54 4b 5f 53 4c 41 53 48 3b 0a 20 20 20 20 20 20  TK_SLASH;.      
2f5ed 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2f5ee 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d    }.      for(i=
2f5ef 33 2c 20 63 3d 7a 5b 32 5d 3b 20 28 63 21 3d 27  3, c=z[2]; (c!='
2f5f0 2a 27 20 7c 7c 20 7a 5b 69 5d 21 3d 27 2f 27 29  *' || z[i]!='/')
2f5f1 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b   && (c=z[i])!=0;
2f5f2 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66   i++){}.      if
2f5f3 28 20 63 20 29 20 69 2b 2b 3b 0a 20 20 20 20 20  ( c ) i++;.     
2f5f4 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2f5f5 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20 72 65  _SPACE;.      re
2f5f6 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
2f5f7 20 20 63 61 73 65 20 27 25 27 3a 20 7b 0a 20 20    case '%': {.  
2f5f8 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2f5f9 20 54 4b 5f 52 45 4d 3b 0a 20 20 20 20 20 20 72   TK_REM;.      r
2f5fa 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
2f5fb 20 20 20 63 61 73 65 20 27 3d 27 3a 20 7b 0a 20     case '=': {. 
2f5fc 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2f5fd 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20 20 72  = TK_EQ;.      r
2f5fe 65 74 75 72 6e 20 31 20 2b 20 28 7a 5b 31 5d 3d  eturn 1 + (z[1]=
2f5ff 3d 27 3d 27 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ='=');.    }.   
2f600 20 63 61 73 65 20 27 3c 27 3a 20 7b 0a 20 20 20   case '<': {.   
2f601 20 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d     if( (c=z[1])=
2f602 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='=' ){.        
2f603 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2f604 4c 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  LE;.        retu
2f605 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 2;.      }els
2f606 65 20 69 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0a  e if( c=='>' ){.
2f607 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2f608 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20 20  pe = TK_NE;.    
2f609 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
2f60a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
2f60b 3d 27 3c 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='<' ){.        
2f60c 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2f60d 4c 53 48 49 46 54 3b 0a 20 20 20 20 20 20 20 20  LSHIFT;.        
2f60e 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
2f60f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a  }else{.        *
2f610 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c  tokenType = TK_L
2f611 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  T;.        retur
2f612 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
2f613 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3e 27 3a   }.    case '>':
2f614 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28 63 3d   {.      if( (c=
2f615 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a 20 20  z[1])=='=' ){.  
2f616 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2f617 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20   = TK_GE;.      
2f618 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
2f619 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27    }else if( c=='
2f61a 3e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  >' ){.        *t
2f61b 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 53  okenType = TK_RS
2f61c 48 49 46 54 3b 0a 20 20 20 20 20 20 20 20 72 65  HIFT;.        re
2f61d 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65  turn 2;.      }e
2f61e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  lse{.        *to
2f61f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47 54 3b  kenType = TK_GT;
2f620 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2f621 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
2f622 0a 20 20 20 20 63 61 73 65 20 27 21 27 3a 20 7b  .    case '!': {
2f623 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21  .      if( z[1]!
2f624 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='=' ){.        
2f625 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2f626 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20  ILLEGAL;.       
2f627 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20   return 2;.     
2f628 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2f629 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2f62a 4e 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  NE;.        retu
2f62b 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 2;.      }.  
2f62c 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 7c 27    }.    case '|'
2f62d 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  : {.      if( z[
2f62e 31 5d 21 3d 27 7c 27 20 29 7b 0a 20 20 20 20 20  1]!='|' ){.     
2f62f 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2f630 54 4b 5f 42 49 54 4f 52 3b 0a 20 20 20 20 20 20  TK_BITOR;.      
2f631 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2f632 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2f633 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2f634 5f 43 4f 4e 43 41 54 3b 0a 20 20 20 20 20 20 20  _CONCAT;.       
2f635 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20   return 2;.     
2f636 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73   }.    }.    cas
2f637 65 20 27 2c 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e ',': {.      *
2f638 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43  tokenType = TK_C
2f639 4f 4d 4d 41 3b 0a 20 20 20 20 20 20 72 65 74 75  OMMA;.      retu
2f63a 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
2f63b 63 61 73 65 20 27 26 27 3a 20 7b 0a 20 20 20 20  case '&': {.    
2f63c 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2f63d 4b 5f 42 49 54 41 4e 44 3b 0a 20 20 20 20 20 20  K_BITAND;.      
2f63e 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
2f63f 20 20 20 20 63 61 73 65 20 27 7e 27 3a 20 7b 0a      case '~': {.
2f640 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2f641 20 3d 20 54 4b 5f 42 49 54 4e 4f 54 3b 0a 20 20   = TK_BITNOT;.  
2f642 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2f643 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 60 27    }.    case '`'
2f644 3a 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a  :.    case '\'':
2f645 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 7b  .    case '"': {
2f646 0a 20 20 20 20 20 20 69 6e 74 20 64 65 6c 69 6d  .      int delim
2f647 20 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20 20 20 66   = z[0];.      f
2f648 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29  or(i=1; (c=z[i])
2f649 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  !=0; i++){.     
2f64a 20 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20     if( c==delim 
2f64b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
2f64c 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29   z[i+1]==delim )
2f64d 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b  {.            i+
2f64e 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  +;.          }el
2f64f 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
2f650 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
2f651 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2f652 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63     }.      if( c
2f653 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20  =='\'' ){.      
2f654 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2f655 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 20 20  K_STRING;.      
2f656 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20    return i+1;.  
2f657 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 21      }else if( c!
2f658 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  =0 ){.        *t
2f659 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 44  okenType = TK_ID
2f65a 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2f65b 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73   i+1;.      }els
2f65c 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  e{.        *toke
2f65d 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47  nType = TK_ILLEG
2f65e 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  AL;.        retu
2f65f 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn i;.      }.  
2f660 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2e 27    }.    case '.'
2f661 3a 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  : {.#ifndef SQLI
2f662 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
2f663 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28  _POINT.      if(
2f664 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74   !sqlite3Isdigit
2f665 28 7a 5b 31 5d 29 20 29 0a 23 65 6e 64 69 66 0a  (z[1]) ).#endif.
2f666 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
2f667 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2f668 44 4f 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74  DOT;.        ret
2f669 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
2f66a 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6e       /* If the n
2f66b 65 78 74 20 63 68 61 72 61 63 74 65 72 20 69 73  ext character is
2f66c 20 61 20 64 69 67 69 74 2c 20 74 68 69 73 20 69   a digit, this i
2f66d 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  s a floating poi
2f66e 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62  nt.      ** numb
2f66f 65 72 20 74 68 61 74 20 62 65 67 69 6e 73 20 77  er that begins w
2f670 69 74 68 20 22 2e 22 2e 20 20 46 61 6c 6c 20 74  ith ".".  Fall t
2f671 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e 65 78  hru into the nex
2f672 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a  t case */.    }.
2f673 20 20 20 20 63 61 73 65 20 27 30 27 3a 20 63 61      case '0': ca
2f674 73 65 20 27 31 27 3a 20 63 61 73 65 20 27 32 27  se '1': case '2'
2f675 3a 20 63 61 73 65 20 27 33 27 3a 20 63 61 73 65  : case '3': case
2f676 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '4':.    case '
2f677 35 27 3a 20 63 61 73 65 20 27 36 27 3a 20 63 61  5': case '6': ca
2f678 73 65 20 27 37 27 3a 20 63 61 73 65 20 27 38 27  se '7': case '8'
2f679 3a 20 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20  : case '9': {.  
2f67a 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2f67b 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20   TK_INTEGER;.   
2f67c 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69     for(i=0; sqli
2f67d 74 65 33 49 73 64 69 67 69 74 28 7a 5b 69 5d 29  te3Isdigit(z[i])
2f67e 3b 20 69 2b 2b 29 7b 7d 0a 23 69 66 6e 64 65 66  ; i++){}.#ifndef
2f67f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
2f680 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20  ATING_POINT.    
2f681 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2e 27 20    if( z[i]=='.' 
2f682 29 7b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a  ){.        i++;.
2f683 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 73          while( s
2f684 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b  qlite3Isdigit(z[
2f685 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20  i]) ){ i++; }.  
2f686 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2f687 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a 20 20 20   = TK_FLOAT;.   
2f688 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28     }.      if( (
2f689 7a 5b 69 5d 3d 3d 27 65 27 20 7c 7c 20 7a 5b 69  z[i]=='e' || z[i
2f68a 5d 3d 3d 27 45 27 29 20 26 26 0a 20 20 20 20 20  ]=='E') &&.     
2f68b 20 20 20 20 20 20 28 20 73 71 6c 69 74 65 33 49        ( sqlite3I
2f68c 73 64 69 67 69 74 28 7a 5b 69 2b 31 5d 29 20 0a  sdigit(z[i+1]) .
2f68d 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28              || (
2f68e 28 7a 5b 69 2b 31 5d 3d 3d 27 2b 27 20 7c 7c 20  (z[i+1]=='+' || 
2f68f 7a 5b 69 2b 31 5d 3d 3d 27 2d 27 29 20 26 26 20  z[i+1]=='-') && 
2f690 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a  sqlite3Isdigit(z
2f691 5b 69 2b 32 5d 29 29 0a 20 20 20 20 20 20 20 20  [i+2])).        
2f692 20 20 20 29 0a 20 20 20 20 20 20 29 7b 0a 20 20     ).      ){.  
2f693 20 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20        i += 2;.  
2f694 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c        while( sql
2f695 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 69 5d  ite3Isdigit(z[i]
2f696 29 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 20 20  ) ){ i++; }.    
2f697 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2f698 20 54 4b 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 20   TK_FLOAT;.     
2f699 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
2f69a 77 68 69 6c 65 28 20 49 64 43 68 61 72 28 7a 5b  while( IdChar(z[
2f69b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  i]) ){.        *
2f69c 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
2f69d 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20  LLEGAL;.        
2f69e 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i++;.      }.   
2f69f 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
2f6a0 20 7d 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a   }.    case '[':
2f6a1 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31   {.      for(i=1
2f6a2 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27 5d 27  , c=z[0]; c!=']'
2f6a3 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b   && (c=z[i])!=0;
2f6a4 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74   i++){}.      *t
2f6a5 6f 6b 65 6e 54 79 70 65 20 3d 20 63 3d 3d 27 5d  okenType = c==']
2f6a6 27 20 3f 20 54 4b 5f 49 44 20 3a 20 54 4b 5f 49  ' ? TK_ID : TK_I
2f6a7 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 72 65  LLEGAL;.      re
2f6a8 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
2f6a9 20 20 63 61 73 65 20 27 3f 27 3a 20 7b 0a 20 20    case '?': {.  
2f6aa 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2f6ab 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a 20 20   TK_VARIABLE;.  
2f6ac 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 73 71 6c      for(i=1; sql
2f6ad 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 69 5d  ite3Isdigit(z[i]
2f6ae 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20  ); i++){}.      
2f6af 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
2f6b0 20 20 20 20 63 61 73 65 20 27 23 27 3a 20 7b 0a      case '#': {.
2f6b1 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 73        for(i=1; s
2f6b2 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b  qlite3Isdigit(z[
2f6b3 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20  i]); i++){}.    
2f6b4 20 20 69 66 28 20 69 3e 31 20 29 7b 0a 20 20 20    if( i>1 ){.   
2f6b5 20 20 20 20 20 2f 2a 20 50 61 72 61 6d 65 74 65       /* Paramete
2f6b6 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 23  rs of the form #
2f6b7 4e 4e 4e 20 28 77 68 65 72 65 20 4e 4e 4e 20 69  NNN (where NNN i
2f6b8 73 20 61 20 6e 75 6d 62 65 72 29 20 61 72 65 20  s a number) are 
2f6b9 75 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  used.        ** 
2f6ba 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 73 71  internally by sq
2f6bb 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2f6bc 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 2a 74  .  */.        *t
2f6bd 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 45  okenType = TK_RE
2f6be 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 20 20  GISTER;.        
2f6bf 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20  return i;.      
2f6c0 7d 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20  }.      /* Fall 
2f6c1 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74 68 65  through into the
2f6c2 20 6e 65 78 74 20 63 61 73 65 20 69 66 20 74 68   next case if th
2f6c3 65 20 27 23 27 20 69 73 20 6e 6f 74 20 66 6f 6c  e '#' is not fol
2f6c4 6c 6f 77 65 64 20 62 79 0a 20 20 20 20 20 20 2a  lowed by.      *
2f6c5 2a 20 61 20 64 69 67 69 74 2e 20 54 72 79 20 74  * a digit. Try t
2f6c6 6f 20 6d 61 74 63 68 20 23 41 41 41 41 20 77 68  o match #AAAA wh
2f6c7 65 72 65 20 41 41 41 41 20 69 73 20 61 20 70 61  ere AAAA is a pa
2f6c8 72 61 6d 65 74 65 72 20 6e 61 6d 65 2e 20 2a 2f  rameter name. */
2f6c9 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
2f6ca 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56  QLITE_OMIT_TCL_V
2f6cb 41 52 49 41 42 4c 45 0a 20 20 20 20 63 61 73 65  ARIABLE.    case
2f6cc 20 27 24 27 3a 0a 23 65 6e 64 69 66 0a 20 20 20   '$':.#endif.   
2f6cd 20 63 61 73 65 20 27 40 27 3a 20 20 2f 2a 20 46   case '@':  /* F
2f6ce 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  or compatibility
2f6cf 20 77 69 74 68 20 4d 53 20 53 51 4c 20 53 65 72   with MS SQL Ser
2f6d0 76 65 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ver */.    case 
2f6d1 27 3a 27 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ':': {.      int
2f6d2 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a 74   n = 0;.      *t
2f6d3 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56 41  okenType = TK_VA
2f6d4 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66 6f  RIABLE;.      fo
2f6d5 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=1; (c=z[i])!
2f6d6 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  =0; i++){.      
2f6d7 20 20 69 66 28 20 49 64 43 68 61 72 28 63 29 20    if( IdChar(c) 
2f6d8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b  ){.          n++
2f6d9 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2f6da 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42  _OMIT_TCL_VARIAB
2f6db 4c 45 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  LE.        }else
2f6dc 20 69 66 28 20 63 3d 3d 27 28 27 20 26 26 20 6e   if( c=='(' && n
2f6dd 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >0 ){.          
2f6de 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  do{.            
2f6df 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  i++;.          }
2f6e0 77 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d 29 21  while( (c=z[i])!
2f6e1 3d 30 20 26 26 20 21 73 71 6c 69 74 65 33 49 73  =0 && !sqlite3Is
2f6e2 73 70 61 63 65 28 63 29 20 26 26 20 63 21 3d 27  space(c) && c!='
2f6e3 29 27 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )' );.          
2f6e4 69 66 28 20 63 3d 3d 27 29 27 20 29 7b 0a 20 20  if( c==')' ){.  
2f6e5 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20            i++;. 
2f6e6 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
2f6e7 20 20 20 20 20 20 20 20 20 20 20 20 2a 74 6f 6b              *tok
2f6e8 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
2f6e9 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  GAL;.          }
2f6ea 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
2f6eb 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
2f6ec 69 66 28 20 63 3d 3d 27 3a 27 20 26 26 20 7a 5b  if( c==':' && z[
2f6ed 69 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20  i+1]==':' ){.   
2f6ee 20 20 20 20 20 20 20 69 2b 2b 3b 0a 23 65 6e 64         i++;.#end
2f6ef 69 66 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  if.        }else
2f6f0 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  {.          brea
2f6f1 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
2f6f2 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e     }.      if( n
2f6f3 3d 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65  ==0 ) *tokenType
2f6f4 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
2f6f5 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
2f6f6 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
2f6f7 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
2f6f8 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 27  TERAL.    case '
2f6f9 78 27 3a 20 63 61 73 65 20 27 58 27 3a 20 7b 0a  x': case 'X': {.
2f6fa 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 3d 3d        if( z[1]==
2f6fb 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20 20 20  '\'' ){.        
2f6fc 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2f6fd 42 4c 4f 42 3b 0a 20 20 20 20 20 20 20 20 66 6f  BLOB;.        fo
2f6fe 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=2; (c=z[i])!
2f6ff 3d 30 20 26 26 20 63 21 3d 27 5c 27 27 3b 20 69  =0 && c!='\''; i
2f700 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
2f701 66 28 20 21 73 71 6c 69 74 65 33 49 73 78 64 69  f( !sqlite3Isxdi
2f702 67 69 74 28 63 29 20 29 7b 0a 20 20 20 20 20 20  git(c) ){.      
2f703 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2f704 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
2f705 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2f706 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
2f707 20 69 25 32 20 7c 7c 20 21 63 20 29 20 2a 74 6f   i%2 || !c ) *to
2f708 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c  kenType = TK_ILL
2f709 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 69 66  EGAL;.        if
2f70a 28 20 63 20 29 20 69 2b 2b 3b 0a 20 20 20 20 20  ( c ) i++;.     
2f70b 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
2f70c 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 4f 74     }.      /* Ot
2f70d 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72  herwise fall thr
2f70e 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
2f70f 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 23   case */.    }.#
2f710 65 6e 64 69 66 0a 20 20 20 20 64 65 66 61 75 6c  endif.    defaul
2f711 74 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 21  t: {.      if( !
2f712 49 64 43 68 61 72 28 2a 7a 29 20 29 7b 0a 20 20  IdChar(*z) ){.  
2f713 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2f714 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69     }.      for(i
2f715 3d 31 3b 20 49 64 43 68 61 72 28 7a 5b 69 5d 29  =1; IdChar(z[i])
2f716 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a  ; i++){}.      *
2f717 74 6f 6b 65 6e 54 79 70 65 20 3d 20 6b 65 79 77  tokenType = keyw
2f718 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a 29 7a  ordCode((char*)z
2f719 2c 20 69 29 3b 0a 20 20 20 20 20 20 72 65 74 75  , i);.      retu
2f71a 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn i;.    }.  }.
2f71b 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2f71c 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 72 65 74  K_ILLEGAL;.  ret
2f71d 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 1;.}../*.** 
2f71e 52 75 6e 20 74 68 65 20 70 61 72 73 65 72 20 6f  Run the parser o
2f71f 6e 20 74 68 65 20 67 69 76 65 6e 20 53 51 4c 20  n the given SQL 
2f720 73 74 72 69 6e 67 2e 20 20 54 68 65 20 70 61 72  string.  The par
2f721 73 65 72 20 73 74 72 75 63 74 75 72 65 20 69 73  ser structure is
2f722 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e 2e 20 20  .** passed in.  
2f723 41 6e 20 53 51 4c 49 54 45 5f 20 73 74 61 74 75  An SQLITE_ statu
2f724 73 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  s code is return
2f725 65 64 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72  ed.  If an error
2f726 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68 65 6e 20   occurs.** then 
2f727 61 6e 20 61 6e 64 20 61 74 74 65 6d 70 74 20 69  an and attempt i
2f728 73 20 6d 61 64 65 20 74 6f 20 77 72 69 74 65 20  s made to write 
2f729 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
2f72a 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79   into .** memory
2f72b 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
2f72c 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
2f72d 61 6e 64 20 74 6f 20 6d 61 6b 65 20 2a 70 7a 45  and to make *pzE
2f72e 72 72 4d 73 67 20 70 6f 69 6e 74 20 74 6f 20 74  rrMsg point to t
2f72f 68 61 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73  hat.** error mes
2f730 73 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sage..*/.SQLITE_
2f731 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2f732 74 65 33 52 75 6e 50 61 72 73 65 72 28 50 61 72  te3RunParser(Par
2f733 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
2f734 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 63 68  t char *zSql, ch
2f735 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a  ar **pzErrMsg){.
2f736 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 20    int nErr = 0; 
2f737 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f738 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
2f739 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65  rrors encountere
2f73a 64 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  d */.  int i;   
2f73b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f73c 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2f73d 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 76 6f 69 64  ounter */.  void
2f73e 20 2a 70 45 6e 67 69 6e 65 3b 20 20 20 20 20 20   *pEngine;      
2f73f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2f740 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 61 74  he LEMON-generat
2f741 65 64 20 4c 41 4c 52 28 31 29 20 70 61 72 73 65  ed LALR(1) parse
2f742 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 6b 65 6e  r */.  int token
2f743 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  Type;           
2f744 20 20 20 20 20 20 20 2f 2a 20 74 79 70 65 20 6f         /* type o
2f745 66 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  f the next token
2f746 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 73 74 54 6f   */.  int lastTo
2f747 6b 65 6e 50 61 72 73 65 64 20 3d 20 2d 31 3b 20  kenParsed = -1; 
2f748 20 20 20 20 20 20 2f 2a 20 74 79 70 65 20 6f 66        /* type of
2f749 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 6f   the previous to
2f74a 6b 65 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 61 62  ken */.  u8 enab
2f74b 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20  leLookaside;    
2f74c 20 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65           /* Save
2f74d 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6c  d value of db->l
2f74e 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
2f74f 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  d */.  sqlite3 *
2f750 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2f751 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2f752 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2f753 6e 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 71 6c  n */.  int mxSql
2f754 4c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  Len;            
2f755 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 6c 65         /* Max le
2f756 6e 67 74 68 20 6f 66 20 61 6e 20 53 51 4c 20 73  ngth of an SQL s
2f757 74 72 69 6e 67 20 2a 2f 0a 0a 0a 20 20 6d 78 53  tring */...  mxS
2f758 71 6c 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d  qlLen = db->aLim
2f759 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
2f75a 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 69  SQL_LENGTH];.  i
2f75b 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  f( db->activeVdb
2f75c 65 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64  eCnt==0 ){.    d
2f75d 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
2f75e 74 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70  ted = 0;.  }.  p
2f75f 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
2f760 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 72 73 65 2d  TE_OK;.  pParse-
2f761 3e 7a 54 61 69 6c 20 3d 20 70 50 61 72 73 65 2d  >zTail = pParse-
2f762 3e 7a 53 71 6c 20 3d 20 7a 53 71 6c 3b 0a 20 20  >zSql = zSql;.  
2f763 69 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  i = 0;.  assert(
2f764 20 70 7a 45 72 72 4d 73 67 21 3d 30 20 29 3b 0a   pzErrMsg!=0 );.
2f765 20 20 70 45 6e 67 69 6e 65 20 3d 20 73 71 6c 69    pEngine = sqli
2f766 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 28  te3ParserAlloc((
2f767 76 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29  void*(*)(size_t)
2f768 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 29 3b  )sqlite3Malloc);
2f769 0a 20 20 69 66 28 20 70 45 6e 67 69 6e 65 3d 3d  .  if( pEngine==
2f76a 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  0 ){.    db->mal
2f76b 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2f76c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2f76d 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73  _NOMEM;.  }.  as
2f76e 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 73 4c  sert( pParse->sL
2f76f 61 73 74 54 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20  astToken.dyn==0 
2f770 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
2f771 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d  rse->pNewTable==
2f772 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2f773 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2f774 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  er==0 );.  asser
2f775 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3d  t( pParse->nVar=
2f776 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2f777 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
2f778 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
2f779 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70   pParse->nVarExp
2f77a 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 61  rAlloc==0 );.  a
2f77b 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61  ssert( pParse->a
2f77c 70 56 61 72 45 78 70 72 3d 3d 30 20 29 3b 0a 20  pVarExpr==0 );. 
2f77d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65   enableLookaside
2f77e 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
2f77f 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 69 66 28  .bEnabled;.  if(
2f780 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
2f781 53 74 61 72 74 20 29 20 64 62 2d 3e 6c 6f 6f 6b  Start ) db->look
2f782 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d  aside.bEnabled =
2f783 20 31 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 4c   1;.  pParse->sL
2f784 61 73 74 54 6f 6b 65 6e 2e 71 75 6f 74 65 64 20  astToken.quoted 
2f785 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 21 64  = 1;.  while( !d
2f786 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2f787 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30 20 29 7b  && zSql[i]!=0 ){
2f788 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d  .    assert( i>=
2f789 30 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  0 );.    pParse-
2f78a 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 20 3d 20  >sLastToken.z = 
2f78b 28 75 38 2a 29 26 7a 53 71 6c 5b 69 5d 3b 0a 20  (u8*)&zSql[i];. 
2f78c 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
2f78d 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 64 79  e->sLastToken.dy
2f78e 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  n==0 );.    asse
2f78f 72 74 28 20 70 50 61 72 73 65 2d 3e 73 4c 61 73  rt( pParse->sLas
2f790 74 54 6f 6b 65 6e 2e 71 75 6f 74 65 64 20 29 3b  tToken.quoted );
2f791 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4c 61  .    pParse->sLa
2f792 73 74 54 6f 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69  stToken.n = sqli
2f793 74 65 33 47 65 74 54 6f 6b 65 6e 28 28 75 6e 73  te3GetToken((uns
2f794 69 67 6e 65 64 20 63 68 61 72 2a 29 26 7a 53 71  igned char*)&zSq
2f795 6c 5b 69 5d 2c 26 74 6f 6b 65 6e 54 79 70 65 29  l[i],&tokenType)
2f796 3b 0a 20 20 20 20 69 20 2b 3d 20 70 50 61 72 73  ;.    i += pPars
2f797 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b  e->sLastToken.n;
2f798 0a 20 20 20 20 69 66 28 20 69 3e 6d 78 53 71 6c  .    if( i>mxSql
2f799 4c 65 6e 20 29 7b 0a 20 20 20 20 20 20 70 50 61  Len ){.      pPa
2f79a 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
2f79b 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 20 20 62  _TOOBIG;.      b
2f79c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2f79d 73 77 69 74 63 68 28 20 74 6f 6b 65 6e 54 79 70  switch( tokenTyp
2f79e 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  e ){.      case 
2f79f 54 4b 5f 53 50 41 43 45 3a 20 7b 0a 20 20 20 20  TK_SPACE: {.    
2f7a0 20 20 20 20 69 66 28 20 64 62 2d 3e 75 31 2e 69      if( db->u1.i
2f7a1 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 7b 0a  sInterrupted ){.
2f7a2 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
2f7a3 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e  ->rc = SQLITE_IN
2f7a4 54 45 52 52 55 50 54 3b 0a 20 20 20 20 20 20 20  TERRUPT;.       
2f7a5 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
2f7a6 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62  ing(pzErrMsg, db
2f7a7 2c 20 22 69 6e 74 65 72 72 75 70 74 22 29 3b 0a  , "interrupt");.
2f7a8 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61            goto a
2f7a9 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20  bort_parse;.    
2f7aa 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72      }.        br
2f7ab 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2f7ac 20 20 20 63 61 73 65 20 54 4b 5f 49 4c 4c 45 47     case TK_ILLEG
2f7ad 41 4c 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  AL: {.        sq
2f7ae 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2f7af 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  *pzErrMsg);.    
2f7b0 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
2f7b1 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2f7b2 62 2c 20 22 75 6e 72 65 63 6f 67 6e 69 7a 65 64  b, "unrecognized
2f7b3 20 74 6f 6b 65 6e 3a 20 5c 22 25 54 5c 22 22 2c   token: \"%T\"",
2f7b4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f7b5 20 20 20 20 20 20 20 20 20 26 70 50 61 72 73 65           &pParse
2f7b6 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 29 3b 0a 20  ->sLastToken);. 
2f7b7 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20         nErr++;. 
2f7b8 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
2f7b9 74 5f 70 61 72 73 65 3b 0a 20 20 20 20 20 20 7d  t_parse;.      }
2f7ba 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53  .      case TK_S
2f7bb 45 4d 49 3a 20 7b 0a 20 20 20 20 20 20 20 20 70  EMI: {.        p
2f7bc 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26  Parse->zTail = &
2f7bd 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20  zSql[i];.       
2f7be 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e   /* Fall thru in
2f7bf 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  to the default c
2f7c0 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ase */.      }. 
2f7c1 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
2f7c2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
2f7c3 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 74  arser(pEngine, t
2f7c4 6f 6b 65 6e 54 79 70 65 2c 20 70 50 61 72 73 65  okenType, pParse
2f7c5 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50  ->sLastToken, pP
2f7c6 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 6c  arse);.        l
2f7c7 61 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d  astTokenParsed =
2f7c8 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 20 20   tokenType;.    
2f7c9 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
2f7ca 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2f7cb 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
2f7cc 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20  abort_parse;.   
2f7cd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
2f7ce 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2f7cf 20 20 7d 0a 20 20 7d 0a 61 62 6f 72 74 5f 70 61    }.  }.abort_pa
2f7d0 72 73 65 3a 0a 20 20 69 66 28 20 7a 53 71 6c 5b  rse:.  if( zSql[
2f7d1 69 5d 3d 3d 30 20 26 26 20 6e 45 72 72 3d 3d 30  i]==0 && nErr==0
2f7d2 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d   && pParse->rc==
2f7d3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2f7d4 20 69 66 28 20 6c 61 73 74 54 6f 6b 65 6e 50 61   if( lastTokenPa
2f7d5 72 73 65 64 21 3d 54 4b 5f 53 45 4d 49 20 29 7b  rsed!=TK_SEMI ){
2f7d6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61  .      sqlite3Pa
2f7d7 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 54 4b  rser(pEngine, TK
2f7d8 5f 53 45 4d 49 2c 20 70 50 61 72 73 65 2d 3e 73  _SEMI, pParse->s
2f7d9 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73  LastToken, pPars
2f7da 65 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  e);.      pParse
2f7db 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b  ->zTail = &zSql[
2f7dc 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  i];.    }.    sq
2f7dd 6c 69 74 65 33 50 61 72 73 65 72 28 70 45 6e 67  lite3Parser(pEng
2f7de 69 6e 65 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e  ine, 0, pParse->
2f7df 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72  sLastToken, pPar
2f7e0 73 65 29 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20  se);.  }.#ifdef 
2f7e1 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
2f7e2 45 50 54 48 0a 20 20 73 71 6c 69 74 65 33 53 74  EPTH.  sqlite3St
2f7e3 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
2f7e4 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54 41  TATUS_PARSER_STA
2f7e5 43 4b 2c 0a 20 20 20 20 20 20 73 71 6c 69 74 65  CK,.      sqlite
2f7e6 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b  3ParserStackPeak
2f7e7 28 70 45 6e 67 69 6e 65 29 0a 20 20 29 3b 0a 23  (pEngine).  );.#
2f7e8 65 6e 64 69 66 20 2f 2a 20 59 59 44 45 42 55 47  endif /* YYDEBUG
2f7e9 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
2f7ea 73 65 72 46 72 65 65 28 70 45 6e 67 69 6e 65 2c  serFree(pEngine,
2f7eb 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
2f7ec 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
2f7ed 62 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c  bEnabled = enabl
2f7ee 65 4c 6f 6f 6b 61 73 69 64 65 3b 0a 20 20 69 66  eLookaside;.  if
2f7ef 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2f7f0 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  ed ){.    pParse
2f7f1 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
2f7f2 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  MEM;.  }.  if( p
2f7f3 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54  Parse->rc!=SQLIT
2f7f4 45 5f 4f 4b 20 26 26 20 70 50 61 72 73 65 2d 3e  E_OK && pParse->
2f7f5 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
2f7f6 26 26 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  && pParse->zErrM
2f7f7 73 67 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  sg==0 ){.    sql
2f7f8 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
2f7f9 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20  Parse->zErrMsg, 
2f7fa 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
2f7fb 33 45 72 72 53 74 72 28 70 50 61 72 73 65 2d 3e  3ErrStr(pParse->
2f7fc 72 63 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rc));.  }.  if( 
2f7fd 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
2f7fe 29 7b 0a 20 20 20 20 69 66 28 20 2a 70 7a 45 72  ){.    if( *pzEr
2f7ff 72 4d 73 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20  rMsg==0 ){.     
2f800 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 70 50 61   *pzErrMsg = pPa
2f801 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  rse->zErrMsg;.  
2f802 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
2f803 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2f804 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
2f805 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  );.    }.    pPa
2f806 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30  rse->zErrMsg = 0
2f807 3b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20  ;.    nErr++;.  
2f808 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  }.  if( pParse->
2f809 70 56 64 62 65 20 26 26 20 70 50 61 72 73 65 2d  pVdbe && pParse-
2f80a 3e 6e 45 72 72 3e 30 20 26 26 20 70 50 61 72 73  >nErr>0 && pPars
2f80b 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a  e->nested==0 ){.
2f80c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44      sqlite3VdbeD
2f80d 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 70 56  elete(pParse->pV
2f80e 64 62 65 29 3b 0a 20 20 20 20 70 50 61 72 73 65  dbe);.    pParse
2f80f 2d 3e 70 56 64 62 65 20 3d 20 30 3b 0a 20 20 7d  ->pVdbe = 0;.  }
2f810 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2f811 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
2f812 45 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  E.  if( pParse->
2f813 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20  nested==0 ){.   
2f814 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2f815 62 2c 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c  b, pParse->aTabl
2f816 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 70 50 61 72  eLock);.    pPar
2f817 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d  se->aTableLock =
2f818 20 30 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   0;.    pParse->
2f819 6e 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a  nTableLock = 0;.
2f81a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64    }.#endif.#ifnd
2f81b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2f81c 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71  IRTUALTABLE.  sq
2f81d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2f81e 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f  pParse->apVtabLo
2f81f 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69  ck);.#endif..  i
2f820 66 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56  f( !IN_DECLARE_V
2f821 54 41 42 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  TAB ){.    /* If
2f822 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64 65 63   the pParse->dec
2f823 6c 61 72 65 56 74 61 62 20 66 6c 61 67 20 69 73  lareVtab flag is
2f824 20 73 65 74 2c 20 64 6f 20 6e 6f 74 20 64 65 6c   set, do not del
2f825 65 74 65 20 61 6e 79 20 74 61 62 6c 65 20 0a 20  ete any table . 
2f826 20 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 20     ** structure 
2f827 62 75 69 6c 74 20 75 70 20 69 6e 20 70 50 61 72  built up in pPar
2f828 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 20 54  se->pNewTable. T
2f829 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 64 65 20  he calling code 
2f82a 28 73 65 65 20 76 74 61 62 2e 63 29 0a 20 20 20  (see vtab.c).   
2f82b 20 2a 2a 20 77 69 6c 6c 20 74 61 6b 65 20 72 65   ** will take re
2f82c 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 66 6f 72  sponsibility for
2f82d 20 66 72 65 65 69 6e 67 20 74 68 65 20 54 61 62   freeing the Tab
2f82e 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20  le structure..  
2f82f 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
2f830 44 65 6c 65 74 65 54 61 62 6c 65 28 70 50 61 72  DeleteTable(pPar
2f831 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0a  se->pNewTable);.
2f832 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 44 65    }..  sqlite3De
2f833 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20  leteTrigger(db, 
2f834 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
2f835 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ger);.  sqlite3D
2f836 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  bFree(db, pParse
2f837 2d 3e 61 70 56 61 72 45 78 70 72 29 3b 0a 20 20  ->apVarExpr);.  
2f838 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2f839 2c 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73  , pParse->aAlias
2f83a 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72  );.  while( pPar
2f83b 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 29  se->pZombieTab )
2f83c 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 20 3d  {.    Table *p =
2f83d 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65   pParse->pZombie
2f83e 54 61 62 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  Tab;.    pParse-
2f83f 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70 2d  >pZombieTab = p-
2f840 3e 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 0a 20 20  >pNextZombie;.  
2f841 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
2f842 61 62 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69  able(p);.  }.  i
2f843 66 28 20 6e 45 72 72 3e 30 20 26 26 20 28 70 50  f( nErr>0 && (pP
2f844 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  arse->rc==SQLITE
2f845 5f 4f 4b 20 7c 7c 20 70 50 61 72 73 65 2d 3e 72  _OK || pParse->r
2f846 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20  c==SQLITE_DONE) 
2f847 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  ){.    pParse->r
2f848 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
2f849 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
2f84a 45 72 72 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  Err;.}../*******
2f84b 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
2f84c 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a  okenize.c ******
2f84d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f84e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f84f 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2f850 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2f851 6c 65 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a  le complete.c **
2f852 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f853 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f854 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2f855 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
2f856 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2f857 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2f858 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2f859 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2f85a 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2f85b 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2f85c 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2f85d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2f85e 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2f85f 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2f860 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2f861 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2f862 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2f863 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2f864 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2f865 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2f866 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2f867 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2f868 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f869 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f86a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f86b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2f86c 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66  * An tokenizer f
2f86d 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  or SQL.**.** Thi
2f86e 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2f86f 43 20 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c  C code that impl
2f870 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74  ements the sqlit
2f871 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 41 50  e3_complete() AP
2f872 49 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20  I..** This code 
2f873 75 73 65 64 20 74 6f 20 62 65 20 70 61 72 74 20  used to be part 
2f874 6f 66 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  of the tokenizer
2f875 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 20  .c source file. 
2f876 20 42 75 74 20 62 79 0a 2a 2a 20 73 65 70 61 72   But by.** separ
2f877 61 74 69 6e 67 20 69 74 20 6f 75 74 2c 20 74 68  ating it out, th
2f878 65 20 63 6f 64 65 20 77 69 6c 6c 20 62 65 20 61  e code will be a
2f879 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6f 6d 69  utomatically omi
2f87a 74 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 74 61  tted from.** sta
2f87b 74 69 63 20 6c 69 6e 6b 73 20 74 68 61 74 20 64  tic links that d
2f87c 6f 20 6e 6f 74 20 75 73 65 20 69 74 2e 0a 2a 2a  o not use it..**
2f87d 0a 2a 2a 20 24 49 64 3a 20 63 6f 6d 70 6c 65 74  .** $Id: complet
2f87e 65 2e 63 2c 76 20 31 2e 38 20 32 30 30 39 2f 30  e.c,v 1.8 2009/0
2f87f 34 2f 32 38 20 30 34 3a 34 36 3a 34 32 20 64 72  4/28 04:46:42 dr
2f880 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  h Exp $.*/.#ifnd
2f881 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
2f882 4f 4d 50 4c 45 54 45 0a 0a 2f 2a 0a 2a 2a 20 54  OMPLETE../*.** T
2f883 68 69 73 20 69 73 20 64 65 66 69 6e 65 64 20 69  his is defined i
2f884 6e 20 74 6f 6b 65 6e 69 7a 65 2e 63 2e 20 20 57  n tokenize.c.  W
2f885 65 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20 69  e just have to i
2f886 6d 70 6f 72 74 20 74 68 65 20 64 65 66 69 6e 69  mport the defini
2f887 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tion..*/.#ifndef
2f888 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
2f889 54 49 4f 4e 0a 23 69 66 64 65 66 20 53 51 4c 49  TION.#ifdef SQLI
2f88a 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54 45 5f  TE_ASCII.SQLITE_
2f88b 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
2f88c 61 72 20 73 71 6c 69 74 65 33 49 73 41 73 63 69  ar sqlite3IsAsci
2f88d 69 49 64 43 68 61 72 5b 5d 3b 0a 23 64 65 66 69  iIdChar[];.#defi
2f88e 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28  ne IdChar(C)  ((
2f88f 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30 20 7c  (c=C)&0x80)!=0 |
2f890 7c 20 28 63 3e 30 78 31 66 20 26 26 20 73 71 6c  | (c>0x1f && sql
2f891 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68 61  ite3IsAsciiIdCha
2f892 72 5b 63 2d 30 78 32 30 5d 29 29 0a 23 65 6e 64  r[c-0x20])).#end
2f893 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
2f894 5f 45 42 43 44 49 43 0a 53 51 4c 49 54 45 5f 50  _EBCDIC.SQLITE_P
2f895 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
2f896 72 20 73 71 6c 69 74 65 33 49 73 45 62 63 64 69  r sqlite3IsEbcdi
2f897 63 49 64 43 68 61 72 5b 5d 3b 0a 23 64 65 66 69  cIdChar[];.#defi
2f898 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28  ne IdChar(C)  ((
2f899 28 63 3d 43 29 3e 3d 30 78 34 32 20 26 26 20 73  (c=C)>=0x42 && s
2f89a 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64  qlite3IsEbcdicId
2f89b 43 68 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23  Char[c-0x40])).#
2f89c 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20  endif.#endif /* 
2f89d 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
2f89e 49 4f 4e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  ION */.../*.** T
2f89f 6f 6b 65 6e 20 74 79 70 65 73 20 75 73 65 64 20  oken types used 
2f8a0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  by the sqlite3_c
2f8a1 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75 74 69 6e  omplete() routin
2f8a2 65 2e 20 20 53 65 65 20 74 68 65 20 68 65 61 64  e.  See the head
2f8a3 65 72 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 6f  er.** comments o
2f8a4 6e 20 74 68 61 74 20 70 72 6f 63 65 64 75 72 65  n that procedure
2f8a5 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
2f8a6 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
2f8a7 23 64 65 66 69 6e 65 20 74 6b 53 45 4d 49 20 20  #define tkSEMI  
2f8a8 20 20 30 0a 23 64 65 66 69 6e 65 20 74 6b 57 53    0.#define tkWS
2f8a9 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
2f8aa 74 6b 4f 54 48 45 52 20 20 20 32 0a 23 64 65 66  tkOTHER   2.#def
2f8ab 69 6e 65 20 74 6b 45 58 50 4c 41 49 4e 20 33 0a  ine tkEXPLAIN 3.
2f8ac 23 64 65 66 69 6e 65 20 74 6b 43 52 45 41 54 45  #define tkCREATE
2f8ad 20 20 34 0a 23 64 65 66 69 6e 65 20 74 6b 54 45    4.#define tkTE
2f8ae 4d 50 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20  MP    5.#define 
2f8af 74 6b 54 52 49 47 47 45 52 20 36 0a 23 64 65 66  tkTRIGGER 6.#def
2f8b0 69 6e 65 20 74 6b 45 4e 44 20 20 20 20 20 37 0a  ine tkEND     7.
2f8b1 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
2f8b2 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  UE if the given 
2f8b3 53 51 4c 20 73 74 72 69 6e 67 20 65 6e 64 73 20  SQL string ends 
2f8b4 69 6e 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a  in a semicolon..
2f8b5 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 68 61  **.** Special ha
2f8b6 6e 64 6c 69 6e 67 20 69 73 20 72 65 71 75 69 72  ndling is requir
2f8b7 65 20 66 6f 72 20 43 52 45 41 54 45 20 54 52 49  e for CREATE TRI
2f8b8 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 73 2e  GGER statements.
2f8b9 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 74 68 65  .** Whenever the
2f8ba 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
2f8bb 6b 65 79 77 6f 72 64 73 20 61 72 65 20 73 65 65  keywords are see
2f8bc 6e 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  n, the statement
2f8bd 0a 2a 2a 20 6d 75 73 74 20 65 6e 64 20 77 69 74  .** must end wit
2f8be 68 20 22 3b 45 4e 44 3b 22 2e 0a 2a 2a 0a 2a 2a  h ";END;"..**.**
2f8bf 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
2f8c0 74 69 6f 6e 20 75 73 65 73 20 61 20 73 74 61 74  tion uses a stat
2f8c1 65 20 6d 61 63 68 69 6e 65 20 77 69 74 68 20 37  e machine with 7
2f8c2 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
2f8c3 20 28 30 29 20 53 54 41 52 54 20 20 20 20 20 41   (0) START     A
2f8c4 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
2f8c5 6f 72 20 65 6e 64 20 6f 66 20 61 6e 20 53 51 4c  or end of an SQL
2f8c6 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69   statement.  Thi
2f8c7 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20 20  s routine.**    
2f8c8 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
2f8c9 75 72 6e 73 20 31 20 69 66 20 69 74 20 65 6e 64  urns 1 if it end
2f8ca 73 20 69 6e 20 74 68 65 20 53 54 41 52 54 20 73  s in the START s
2f8cb 74 61 74 65 20 61 6e 64 20 30 20 69 66 20 69 74  tate and 0 if it
2f8cc 20 65 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20   ends.**        
2f8cd 20 20 20 20 20 20 20 20 20 69 6e 20 61 6e 79 20           in any 
2f8ce 6f 74 68 65 72 20 73 74 61 74 65 2e 0a 2a 2a 0a  other state..**.
2f8cf 2a 2a 20 20 20 28 31 29 20 4e 4f 52 4d 41 4c 20  **   (1) NORMAL 
2f8d0 20 20 20 57 65 20 61 72 65 20 69 6e 20 74 68 65     We are in the
2f8d1 20 6d 69 64 64 6c 65 20 6f 66 20 73 74 61 74 65   middle of state
2f8d2 6d 65 6e 74 20 77 68 69 63 68 20 65 6e 64 73 20  ment which ends 
2f8d3 77 69 74 68 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  with a single.**
2f8d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8d5 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a   semicolon..**.*
2f8d6 2a 20 20 20 28 32 29 20 45 58 50 4c 41 49 4e 20  *   (2) EXPLAIN 
2f8d7 20 20 54 68 65 20 6b 65 79 77 6f 72 64 20 45 58    The keyword EX
2f8d8 50 4c 41 49 4e 20 68 61 73 20 62 65 65 6e 20 73  PLAIN has been s
2f8d9 65 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e  een at the begin
2f8da 6e 69 6e 67 20 6f 66 20 0a 2a 2a 20 20 20 20 20  ning of .**     
2f8db 20 20 20 20 20 20 20 20 20 20 20 20 61 20 73 74              a st
2f8dc 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
2f8dd 20 28 33 29 20 43 52 45 41 54 45 20 20 20 20 54   (3) CREATE    T
2f8de 68 65 20 6b 65 79 77 6f 72 64 20 43 52 45 41 54  he keyword CREAT
2f8df 45 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e 20  E has been seen 
2f8e0 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
2f8e1 20 6f 66 20 61 0a 2a 2a 20 20 20 20 20 20 20 20   of a.**        
2f8e2 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d 65           stateme
2f8e3 6e 74 2c 20 70 6f 73 73 69 62 6c 79 20 70 72 65  nt, possibly pre
2f8e4 63 65 65 64 65 64 20 62 79 20 45 58 50 4c 41 49  ceeded by EXPLAI
2f8e5 4e 20 61 6e 64 2f 6f 72 20 66 6f 6c 6c 6f 77 65  N and/or followe
2f8e6 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  d by.**         
2f8e7 20 20 20 20 20 20 20 20 54 45 4d 50 20 6f 72 20          TEMP or 
2f8e8 54 45 4d 50 4f 52 41 52 59 0a 2a 2a 0a 2a 2a 20  TEMPORARY.**.** 
2f8e9 20 20 28 34 29 20 54 52 49 47 47 45 52 20 20 20    (4) TRIGGER   
2f8ea 57 65 20 61 72 65 20 69 6e 20 74 68 65 20 6d 69  We are in the mi
2f8eb 64 64 6c 65 20 6f 66 20 61 20 74 72 69 67 67 65  ddle of a trigge
2f8ec 72 20 64 65 66 69 6e 69 74 69 6f 6e 20 74 68 61  r definition tha
2f8ed 74 20 6d 75 73 74 20 62 65 0a 2a 2a 20 20 20 20  t must be.**    
2f8ee 20 20 20 20 20 20 20 20 20 20 20 20 20 65 6e 64               end
2f8ef 65 64 20 62 79 20 61 20 73 65 6d 69 63 6f 6c 6f  ed by a semicolo
2f8f0 6e 2c 20 74 68 65 20 6b 65 79 77 6f 72 64 20 45  n, the keyword E
2f8f1 4e 44 2c 20 61 6e 64 20 61 6e 6f 74 68 65 72 20  ND, and another 
2f8f2 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a  semicolon..**.**
2f8f3 20 20 20 28 35 29 20 53 45 4d 49 20 20 20 20 20     (5) SEMI     
2f8f4 20 57 65 27 76 65 20 73 65 65 6e 20 74 68 65 20   We've seen the 
2f8f5 66 69 72 73 74 20 73 65 6d 69 63 6f 6c 6f 6e 20  first semicolon 
2f8f6 69 6e 20 74 68 65 20 22 3b 45 4e 44 3b 22 20 74  in the ";END;" t
2f8f7 68 61 74 20 6f 63 63 75 72 73 20 61 74 0a 2a 2a  hat occurs at.**
2f8f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8f9 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74 72   the end of a tr
2f8fa 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e  igger definition
2f8fb 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 45 4e  ..**.**   (6) EN
2f8fc 44 20 20 20 20 20 20 20 57 65 27 76 65 20 73 65  D       We've se
2f8fd 65 6e 20 74 68 65 20 22 3b 45 4e 44 22 20 6f 66  en the ";END" of
2f8fe 20 74 68 65 20 22 3b 45 4e 44 3b 22 20 74 68 61   the ";END;" tha
2f8ff 74 20 6f 63 63 75 72 73 20 61 74 20 74 68 65 20  t occurs at the 
2f900 65 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  end.**          
2f901 20 20 20 20 20 20 20 6f 66 20 61 20 74 72 69 67         of a trig
2f902 67 65 72 20 64 69 66 69 6e 69 74 69 6f 6e 2e 0a  ger difinition..
2f903 2a 2a 0a 2a 2a 20 54 72 61 6e 73 69 74 69 6f 6e  **.** Transition
2f904 73 20 62 65 74 77 65 65 6e 20 73 74 61 74 65 73  s between states
2f905 20 61 62 6f 76 65 20 61 72 65 20 64 65 74 65 72   above are deter
2f906 6d 69 6e 65 64 20 62 79 20 74 6f 6b 65 6e 73 20  mined by tokens 
2f907 65 78 74 72 61 63 74 65 64 0a 2a 2a 20 66 72 6f  extracted.** fro
2f908 6d 20 74 68 65 20 69 6e 70 75 74 2e 20 20 54 68  m the input.  Th
2f909 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 6b 65  e following toke
2f90a 6e 73 20 61 72 65 20 73 69 67 6e 69 66 69 63 61  ns are significa
2f90b 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29 20  nt:.**.**   (0) 
2f90c 74 6b 53 45 4d 49 20 20 20 20 20 20 41 20 73 65  tkSEMI      A se
2f90d 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 20 20 20 28 31  micolon..**   (1
2f90e 29 20 74 6b 57 53 20 20 20 20 20 20 20 20 57 68  ) tkWS        Wh
2f90f 69 74 65 73 70 61 63 65 0a 2a 2a 20 20 20 28 32  itespace.**   (2
2f910 29 20 74 6b 4f 54 48 45 52 20 20 20 20 20 41 6e  ) tkOTHER     An
2f911 79 20 6f 74 68 65 72 20 53 51 4c 20 74 6f 6b 65  y other SQL toke
2f912 6e 2e 0a 2a 2a 20 20 20 28 33 29 20 74 6b 45 58  n..**   (3) tkEX
2f913 50 4c 41 49 4e 20 20 20 54 68 65 20 22 65 78 70  PLAIN   The "exp
2f914 6c 61 69 6e 22 20 6b 65 79 77 6f 72 64 2e 0a 2a  lain" keyword..*
2f915 2a 20 20 20 28 34 29 20 74 6b 43 52 45 41 54 45  *   (4) tkCREATE
2f916 20 20 20 20 54 68 65 20 22 63 72 65 61 74 65 22      The "create"
2f917 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28   keyword..**   (
2f918 35 29 20 74 6b 54 45 4d 50 20 20 20 20 20 20 54  5) tkTEMP      T
2f919 68 65 20 22 74 65 6d 70 22 20 6f 72 20 22 74 65  he "temp" or "te
2f91a 6d 70 6f 72 61 72 79 22 20 6b 65 79 77 6f 72 64  mporary" keyword
2f91b 2e 0a 2a 2a 20 20 20 28 36 29 20 74 6b 54 52 49  ..**   (6) tkTRI
2f91c 47 47 45 52 20 20 20 54 68 65 20 22 74 72 69 67  GGER   The "trig
2f91d 67 65 72 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a  ger" keyword..**
2f91e 20 20 20 28 37 29 20 74 6b 45 4e 44 20 20 20 20     (7) tkEND    
2f91f 20 20 20 54 68 65 20 22 65 6e 64 22 20 6b 65 79     The "end" key
2f920 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 69 74  word..**.** Whit
2f921 65 73 70 61 63 65 20 6e 65 76 65 72 20 63 61 75  espace never cau
2f922 73 65 73 20 61 20 73 74 61 74 65 20 74 72 61 6e  ses a state tran
2f923 73 69 74 69 6f 6e 20 61 6e 64 20 69 73 20 61 6c  sition and is al
2f924 77 61 79 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a  ways ignored..**
2f925 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c  .** If we compil
2f926 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f 4d  e with SQLITE_OM
2f927 49 54 5f 54 52 49 47 47 45 52 2c 20 61 6c 6c 20  IT_TRIGGER, all 
2f928 6f 66 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69  of the computati
2f929 6f 6e 20 6e 65 65 64 65 64 0a 2a 2a 20 74 6f 20  on needed.** to 
2f92a 72 65 63 6f 67 6e 69 7a 65 20 74 68 65 20 65 6e  recognize the en
2f92b 64 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 63  d of a trigger c
2f92c 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20  an be omitted.  
2f92d 41 6c 6c 20 77 65 20 68 61 76 65 20 74 6f 20 64  All we have to d
2f92e 6f 0a 2a 2a 20 69 73 20 6c 6f 6f 6b 20 66 6f 72  o.** is look for
2f92f 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 68 61   a semicolon tha
2f930 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
2f931 20 61 6e 20 73 74 72 69 6e 67 20 6f 72 20 63 6f   an string or co
2f932 6d 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  mment..*/.SQLITE
2f933 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2f934 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74 20  _complete(const 
2f935 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 75  char *zSql){.  u
2f936 38 20 73 74 61 74 65 20 3d 20 30 3b 20 20 20 2f  8 state = 0;   /
2f937 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74 65 2c  * Current state,
2f938 20 75 73 69 6e 67 20 6e 75 6d 62 65 72 73 20 64   using numbers d
2f939 65 66 69 6e 65 64 20 69 6e 20 68 65 61 64 65 72  efined in header
2f93a 20 63 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 75 38   comment */.  u8
2f93b 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 2f 2a   token;       /*
2f93c 20 56 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65   Value of the ne
2f93d 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 23 69 66  xt token */..#if
2f93e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f93f 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 41 20  _TRIGGER.  /* A 
2f940 63 6f 6d 70 6c 65 78 20 73 74 61 74 65 6d 65 6e  complex statemen
2f941 74 20 6d 61 63 68 69 6e 65 20 75 73 65 64 20 74  t machine used t
2f942 6f 20 64 65 74 65 63 74 20 74 68 65 20 65 6e 64  o detect the end
2f943 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49   of a CREATE TRI
2f944 47 47 45 52 0a 20 20 2a 2a 20 73 74 61 74 65 6d  GGER.  ** statem
2f945 65 6e 74 2e 20 20 54 68 69 73 20 69 73 20 74 68  ent.  This is th
2f946 65 20 6e 6f 72 6d 61 6c 20 63 61 73 65 2e 0a 20  e normal case.. 
2f947 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
2f948 73 74 20 75 38 20 74 72 61 6e 73 5b 37 5d 5b 38  st u8 trans[7][8
2f949 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ] = {.          
2f94a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
2f94b 6b 65 6e 3a 20 20 20 20 20 20 20 20 20 20 20 20  ken:            
2f94c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f94d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f94e 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 53      */.     /* S
2f94f 74 61 74 65 3a 20 20 20 20 20 20 20 2a 2a 20 20  tate:       **  
2f950 53 45 4d 49 20 20 57 53 20 20 4f 54 48 45 52 20  SEMI  WS  OTHER 
2f951 45 58 50 4c 41 49 4e 20 20 43 52 45 41 54 45 20  EXPLAIN  CREATE 
2f952 20 54 45 4d 50 20 20 54 52 49 47 47 45 52 20 20   TEMP  TRIGGER  
2f953 45 4e 44 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20  END  */.     /* 
2f954 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b 20  0   START: */ { 
2f955 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20 31 2c     0,  0,     1,
2f956 20 20 20 20 20 20 32 2c 20 20 20 20 20 20 33 2c        2,      3,
2f957 20 20 20 20 31 2c 20 20 20 20 20 20 20 31 2c 20      1,       1, 
2f958 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a    1,  },.     /*
2f959 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b   1  NORMAL: */ {
2f95a 20 20 20 20 30 2c 20 20 31 2c 20 20 20 20 20 31      0,  1,     1
2f95b 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 31  ,      1,      1
2f95c 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20 31 2c  ,    1,       1,
2f95d 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f     1,  },.     /
2f95e 2a 20 32 20 45 58 50 4c 41 49 4e 3a 20 2a 2f 20  * 2 EXPLAIN: */ 
2f95f 7b 20 20 20 20 30 2c 20 20 32 2c 20 20 20 20 20  {    0,  2,     
2f960 32 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20 20  2,      1,      
2f961 33 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20 31  3,    1,       1
2f962 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20  ,   1,  },.     
2f963 2f 2a 20 33 20 20 43 52 45 41 54 45 3a 20 2a 2f  /* 3  CREATE: */
2f964 20 7b 20 20 20 20 30 2c 20 20 33 2c 20 20 20 20   {    0,  3,    
2f965 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20   1,      1,     
2f966 20 31 2c 20 20 20 20 33 2c 20 20 20 20 20 20 20   1,    3,       
2f967 34 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20  4,   1,  },.    
2f968 20 2f 2a 20 34 20 54 52 49 47 47 45 52 3a 20 2a   /* 4 TRIGGER: *
2f969 2f 20 7b 20 20 20 20 35 2c 20 20 34 2c 20 20 20  / {    5,  4,   
2f96a 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20    4,      4,    
2f96b 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20 20 20    4,    4,      
2f96c 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a 20 20 20   4,   4,  },.   
2f96d 20 20 2f 2a 20 35 20 20 20 20 53 45 4d 49 3a 20    /* 5    SEMI: 
2f96e 2a 2f 20 7b 20 20 20 20 35 2c 20 20 35 2c 20 20  */ {    5,  5,  
2f96f 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20     4,      4,   
2f970 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20 20     4,    4,     
2f971 20 20 34 2c 20 20 20 36 2c 20 20 7d 2c 0a 20 20    4,   6,  },.  
2f972 20 20 20 2f 2a 20 36 20 20 20 20 20 45 4e 44 3a     /* 6     END:
2f973 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 36 2c 20   */ {    0,  6, 
2f974 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20      4,      4,  
2f975 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20      4,    4,    
2f976 20 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a 20     4,   4,  },. 
2f977 20 7d 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 49   };.#else.  /* I
2f978 66 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6e  f triggers are n
2f979 6f 74 20 73 75 70 70 6f 72 65 64 20 62 79 20 74  ot suppored by t
2f97a 68 69 73 20 63 6f 6d 70 69 6c 65 20 74 68 65 6e  his compile then
2f97b 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6d   the statement m
2f97c 61 63 68 69 6e 65 0a 20 20 2a 2a 20 75 73 65 64  achine.  ** used
2f97d 20 74 6f 20 64 65 74 65 63 74 20 74 68 65 20 65   to detect the e
2f97e 6e 64 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e  nd of a statemen
2f97f 74 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c 69  t is much simpli
2f980 65 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63  er.  */.  static
2f981 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e 73 5b   const u8 trans[
2f982 32 5d 5b 33 5d 20 3d 20 7b 0a 20 20 20 20 20 20  2][3] = {.      
2f983 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f984 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20 20 20  * Token:        
2f985 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 53 74     */.     /* St
2f986 61 74 65 3a 20 20 20 20 20 20 20 2a 2a 20 20 53  ate:       **  S
2f987 45 4d 49 20 20 57 53 20 20 4f 54 48 45 52 20 2a  EMI  WS  OTHER *
2f988 2f 0a 20 20 20 20 20 2f 2a 20 30 20 20 20 53 54  /.     /* 0   ST
2f989 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20  ART: */ {    0, 
2f98a 20 30 2c 20 20 20 20 20 31 2c 20 7d 2c 0a 20 20   0,     1, },.  
2f98b 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c 3a     /* 1  NORMAL:
2f98c 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c 20   */ {    0,  1, 
2f98d 20 20 20 20 31 2c 20 7d 2c 0a 20 20 7d 3b 0a 23      1, },.  };.#
2f98e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2f98f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
2f990 0a 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20  .  while( *zSql 
2f991 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 2a  ){.    switch( *
2f992 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 63 61  zSql ){.      ca
2f993 73 65 20 27 3b 27 3a 20 7b 20 20 2f 2a 20 41 20  se ';': {  /* A 
2f994 73 65 6d 69 63 6f 6c 6f 6e 20 2a 2f 0a 20 20 20  semicolon */.   
2f995 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 53       token = tkS
2f996 45 4d 49 3b 0a 20 20 20 20 20 20 20 20 62 72 65  EMI;.        bre
2f997 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2f998 20 20 63 61 73 65 20 27 20 27 3a 0a 20 20 20 20    case ' ':.    
2f999 20 20 63 61 73 65 20 27 5c 72 27 3a 0a 20 20 20    case '\r':.   
2f99a 20 20 20 63 61 73 65 20 27 5c 74 27 3a 0a 20 20     case '\t':.  
2f99b 20 20 20 20 63 61 73 65 20 27 5c 6e 27 3a 0a 20      case '\n':. 
2f99c 20 20 20 20 20 63 61 73 65 20 27 5c 66 27 3a 20       case '\f': 
2f99d 7b 20 20 2f 2a 20 57 68 69 74 65 20 73 70 61 63  {  /* White spac
2f99e 65 20 69 73 20 69 67 6e 6f 72 65 64 20 2a 2f 0a  e is ignored */.
2f99f 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
2f9a0 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62 72  tkWS;.        br
2f9a1 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2f9a2 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b 20 20     case '/': {  
2f9a3 20 2f 2a 20 43 2d 73 74 79 6c 65 20 63 6f 6d 6d   /* C-style comm
2f9a4 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  ents */.        
2f9a5 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27 2a 27  if( zSql[1]!='*'
2f9a6 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 6f   ){.          to
2f9a7 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20  ken = tkOTHER;. 
2f9a8 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f9a9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f9aa 20 20 7a 53 71 6c 20 2b 3d 20 32 3b 0a 20 20 20    zSql += 2;.   
2f9ab 20 20 20 20 20 77 68 69 6c 65 28 20 7a 53 71 6c       while( zSql
2f9ac 5b 30 5d 20 26 26 20 28 7a 53 71 6c 5b 30 5d 21  [0] && (zSql[0]!
2f9ad 3d 27 2a 27 20 7c 7c 20 7a 53 71 6c 5b 31 5d 21  ='*' || zSql[1]!
2f9ae 3d 27 2f 27 29 20 29 7b 20 7a 53 71 6c 2b 2b 3b  ='/') ){ zSql++;
2f9af 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a   }.        if( z
2f9b0 53 71 6c 5b 30 5d 3d 3d 30 20 29 20 72 65 74 75  Sql[0]==0 ) retu
2f9b1 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 53  rn 0;.        zS
2f9b2 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 74 6f  ql++;.        to
2f9b3 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20  ken = tkWS;.    
2f9b4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2f9b5 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 2d   }.      case '-
2f9b6 27 3a 20 7b 20 20 20 2f 2a 20 53 51 4c 2d 73 74  ': {   /* SQL-st
2f9b7 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20 66 72 6f  yle comments fro
2f9b8 6d 20 22 2d 2d 22 20 74 6f 20 65 6e 64 20 6f 66  m "--" to end of
2f9b9 20 6c 69 6e 65 20 2a 2f 0a 20 20 20 20 20 20 20   line */.       
2f9ba 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27 2d   if( zSql[1]!='-
2f9bb 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74  ' ){.          t
2f9bc 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
2f9bd 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
2f9be 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2f9bf 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20     while( *zSql 
2f9c0 26 26 20 2a 7a 53 71 6c 21 3d 27 5c 6e 27 20 29  && *zSql!='\n' )
2f9c1 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20  { zSql++; }.    
2f9c2 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30      if( *zSql==0
2f9c3 20 29 20 72 65 74 75 72 6e 20 73 74 61 74 65 3d   ) return state=
2f9c4 3d 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65  =0;.        toke
2f9c5 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20 20  n = tkWS;.      
2f9c6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2f9c7 0a 20 20 20 20 20 20 63 61 73 65 20 27 5b 27 3a  .      case '[':
2f9c8 20 7b 20 20 20 2f 2a 20 4d 69 63 72 6f 73 6f 66   {   /* Microsof
2f9c9 74 2d 73 74 79 6c 65 20 69 64 65 6e 74 69 66 69  t-style identifi
2f9ca 65 72 73 20 69 6e 20 5b 2e 2e 2e 5d 20 2a 2f 0a  ers in [...] */.
2f9cb 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a          zSql++;.
2f9cc 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a          while( *
2f9cd 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 27  zSql && *zSql!='
2f9ce 5d 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a  ]' ){ zSql++; }.
2f9cf 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71          if( *zSq
2f9d0 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  l==0 ) return 0;
2f9d1 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d  .        token =
2f9d2 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20   tkOTHER;.      
2f9d3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2f9d4 0a 20 20 20 20 20 20 63 61 73 65 20 27 60 27 3a  .      case '`':
2f9d5 20 20 20 20 20 2f 2a 20 47 72 61 76 65 2d 61 63       /* Grave-ac
2f9d6 63 65 6e 74 20 71 75 6f 74 65 64 20 73 79 6d 62  cent quoted symb
2f9d7 6f 6c 73 20 75 73 65 64 20 62 79 20 4d 79 53 51  ols used by MySQ
2f9d8 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  L */.      case 
2f9d9 27 22 27 3a 20 20 20 20 20 2f 2a 20 73 69 6e 67  '"':     /* sing
2f9da 6c 65 2d 20 61 6e 64 20 64 6f 75 62 6c 65 2d 71  le- and double-q
2f9db 75 6f 74 65 64 20 73 74 72 69 6e 67 73 20 2a 2f  uoted strings */
2f9dc 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 27 27  .      case '\''
2f9dd 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  : {.        int 
2f9de 63 20 3d 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 20  c = *zSql;.     
2f9df 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20     zSql++;.     
2f9e0 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20     while( *zSql 
2f9e1 26 26 20 2a 7a 53 71 6c 21 3d 63 20 29 7b 20 7a  && *zSql!=c ){ z
2f9e2 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  Sql++; }.       
2f9e3 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20   if( *zSql==0 ) 
2f9e4 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2f9e5 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
2f9e6 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  R;.        break
2f9e7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2f9e8 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
2f9e9 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20     int c;.      
2f9ea 20 20 69 66 28 20 49 64 43 68 61 72 28 28 75 38    if( IdChar((u8
2f9eb 29 2a 7a 53 71 6c 29 20 29 7b 0a 20 20 20 20 20  )*zSql) ){.     
2f9ec 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64 73       /* Keywords
2f9ed 20 61 6e 64 20 75 6e 71 75 6f 74 65 64 20 69 64   and unquoted id
2f9ee 65 6e 74 69 66 69 65 72 73 20 2a 2f 0a 20 20 20  entifiers */.   
2f9ef 20 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 0a         int nId;.
2f9f0 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e 49            for(nI
2f9f1 64 3d 31 3b 20 49 64 43 68 61 72 28 7a 53 71 6c  d=1; IdChar(zSql
2f9f2 5b 6e 49 64 5d 29 3b 20 6e 49 64 2b 2b 29 7b 7d  [nId]); nId++){}
2f9f3 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
2f9f4 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20  MIT_TRIGGER.    
2f9f5 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
2f9f6 4f 54 48 45 52 3b 0a 23 65 6c 73 65 0a 20 20 20  OTHER;.#else.   
2f9f7 20 20 20 20 20 20 20 73 77 69 74 63 68 28 20 2a         switch( *
2f9f8 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  zSql ){.        
2f9f9 20 20 20 20 63 61 73 65 20 27 63 27 3a 20 63 61      case 'c': ca
2f9fa 73 65 20 27 43 27 3a 20 7b 0a 20 20 20 20 20 20  se 'C': {.      
2f9fb 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d          if( nId=
2f9fc 3d 36 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =6 && sqlite3Str
2f9fd 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 63 72 65  NICmp(zSql, "cre
2f9fe 61 74 65 22 2c 20 36 29 3d 3d 30 20 29 7b 0a 20  ate", 6)==0 ){. 
2f9ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
2fa00 6f 6b 65 6e 20 3d 20 74 6b 43 52 45 41 54 45 3b  oken = tkCREATE;
2fa01 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
2fa02 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2fa03 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
2fa04 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20  OTHER;.         
2fa05 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2fa06 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2fa07 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2fa08 20 20 20 20 20 20 63 61 73 65 20 27 74 27 3a 20        case 't': 
2fa09 63 61 73 65 20 27 54 27 3a 20 7b 0a 20 20 20 20  case 'T': {.    
2fa0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 49            if( nI
2fa0b 64 3d 3d 37 20 26 26 20 73 71 6c 69 74 65 33 53  d==7 && sqlite3S
2fa0c 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74  trNICmp(zSql, "t
2fa0d 72 69 67 67 65 72 22 2c 20 37 29 3d 3d 30 20 29  rigger", 7)==0 )
2fa0e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2fa0f 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 52 49 47    token = tkTRIG
2fa10 47 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20  GER;.           
2fa11 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 64     }else if( nId
2fa12 3d 3d 34 20 26 26 20 73 71 6c 69 74 65 33 53 74  ==4 && sqlite3St
2fa13 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 65  rNICmp(zSql, "te
2fa14 6d 70 22 2c 20 34 29 3d 3d 30 20 29 7b 0a 20 20  mp", 4)==0 ){.  
2fa15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
2fa16 6b 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20  ken = tkTEMP;.  
2fa17 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
2fa18 65 20 69 66 28 20 6e 49 64 3d 3d 39 20 26 26 20  e if( nId==9 && 
2fa19 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2fa1a 7a 53 71 6c 2c 20 22 74 65 6d 70 6f 72 61 72 79  zSql, "temporary
2fa1b 22 2c 20 39 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ", 9)==0 ){.    
2fa1c 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
2fa1d 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20 20  n = tkTEMP;.    
2fa1e 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
2fa1f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2fa20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
2fa21 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2fa22 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
2fa23 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
2fa24 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
2fa25 20 63 61 73 65 20 27 65 27 3a 20 20 63 61 73 65   case 'e':  case
2fa26 20 27 45 27 3a 20 7b 0a 20 20 20 20 20 20 20 20   'E': {.        
2fa27 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 33        if( nId==3
2fa28 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
2fa29 43 6d 70 28 7a 53 71 6c 2c 20 22 65 6e 64 22 2c  Cmp(zSql, "end",
2fa2a 20 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   3)==0 ){.      
2fa2b 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2fa2c 3d 20 74 6b 45 4e 44 3b 0a 20 20 20 20 20 20 20  = tkEND;.       
2fa2d 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 69 66         }else.#if
2fa2e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2fa2f 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 20 20 20  _EXPLAIN.       
2fa30 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d         if( nId==
2fa31 37 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  7 && sqlite3StrN
2fa32 49 43 6d 70 28 7a 53 71 6c 2c 20 22 65 78 70 6c  ICmp(zSql, "expl
2fa33 61 69 6e 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20  ain", 7)==0 ){. 
2fa34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
2fa35 6f 6b 65 6e 20 3d 20 74 6b 45 58 50 4c 41 49 4e  oken = tkEXPLAIN
2fa36 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2fa37 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20  }else.#endif.   
2fa38 20 20 20 20 20 20 20 20 20 20 20 7b 0a 20 20 20             {.   
2fa39 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
2fa3a 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
2fa3b 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2fa3c 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
2fa3d 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  k;.            }
2fa3e 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66  .            def
2fa3f 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20  ault: {.        
2fa40 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
2fa41 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20  OTHER;.         
2fa42 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2fa43 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2fa44 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
2fa45 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2fa46 47 45 52 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  GER */.         
2fa47 20 7a 53 71 6c 20 2b 3d 20 6e 49 64 2d 31 3b 0a   zSql += nId-1;.
2fa48 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2fa49 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72           /* Oper
2fa4a 61 74 6f 72 73 20 61 6e 64 20 73 70 65 63 69 61  ators and specia
2fa4b 6c 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a 20 20 20  l symbols */.   
2fa4c 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2fa4d 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
2fa4e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2fa4f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2fa50 20 20 20 73 74 61 74 65 20 3d 20 74 72 61 6e 73     state = trans
2fa51 5b 73 74 61 74 65 5d 5b 74 6f 6b 65 6e 5d 3b 0a  [state][token];.
2fa52 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 7d 0a      zSql++;.  }.
2fa53 20 20 72 65 74 75 72 6e 20 73 74 61 74 65 3d 3d    return state==
2fa54 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  0;.}..#ifndef SQ
2fa55 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
2fa56 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2fa57 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ne is the same a
2fa58 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  s the sqlite3_co
2fa59 6d 70 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65  mplete() routine
2fa5a 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 61 62   described.** ab
2fa5b 6f 76 65 2c 20 65 78 63 65 70 74 20 74 68 61 74  ove, except that
2fa5c 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
2fa5d 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65  s required to be
2fa5e 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 2c   UTF-16 encoded,
2fa5f 20 6e 6f 74 0a 2a 2a 20 55 54 46 2d 38 2e 0a 2a   not.** UTF-8..*
2fa60 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2fa61 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
2fa62 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  e16(const void *
2fa63 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33  zSql){.  sqlite3
2fa64 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20  _value *pVal;.  
2fa65 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c  char const *zSql
2fa66 38 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  8;.  int rc = SQ
2fa67 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 23 69 66  LITE_NOMEM;..#if
2fa68 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2fa69 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d  _AUTOINIT.  rc =
2fa6a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
2fa6b 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20  ize();.  if( rc 
2fa6c 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e  ) return rc;.#en
2fa6d 64 69 66 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c  dif.  pVal = sql
2fa6e 69 74 65 33 56 61 6c 75 65 4e 65 77 28 30 29 3b  ite3ValueNew(0);
2fa6f 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53  .  sqlite3ValueS
2fa70 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20  etStr(pVal, -1, 
2fa71 7a 53 71 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  zSql, SQLITE_UTF
2fa72 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45  16NATIVE, SQLITE
2fa73 5f 53 54 41 54 49 43 29 3b 0a 20 20 7a 53 71 6c  _STATIC);.  zSql
2fa74 38 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  8 = sqlite3Value
2fa75 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
2fa76 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28 20 7a  E_UTF8);.  if( z
2fa77 53 71 6c 38 20 29 7b 0a 20 20 20 20 72 63 20 3d  Sql8 ){.    rc =
2fa78 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
2fa79 65 28 7a 53 71 6c 38 29 3b 0a 20 20 7d 65 6c 73  e(zSql8);.  }els
2fa7a 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  e{.    rc = SQLI
2fa7b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
2fa7c 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
2fa7d 28 70 56 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e  (pVal);.  return
2fa7e 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
2fa7f 30 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69 66  0, rc);.}.#endif
2fa80 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2fa81 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20  UTF16 */.#endif 
2fa82 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  /* SQLITE_OMIT_C
2fa83 4f 4d 50 4c 45 54 45 20 2a 2f 0a 0a 2f 2a 2a 2a  OMPLETE */../***
2fa84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2fa85 6f 66 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a  of complete.c **
2fa86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2fa89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2fa8a 6e 20 66 69 6c 65 20 6d 61 69 6e 2e 63 20 2a 2a  n file main.c **
2fa8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2fa8e 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
2fa8f 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
2fa90 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
2fa91 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
2fa92 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
2fa93 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
2fa94 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
2fa95 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
2fa96 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
2fa97 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
2fa98 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
2fa99 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
2fa9a 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
2fa9b 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
2fa9c 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
2fa9d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
2fa9e 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
2fa9f 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
2faa0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
2faa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faa4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faa5 2a 2a 0a 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20  **.** Main file 
2faa6 66 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20 6c  for the SQLite l
2faa7 69 62 72 61 72 79 2e 20 20 54 68 65 20 72 6f 75  ibrary.  The rou
2faa8 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
2faa9 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  le.** implement 
2faaa 74 68 65 20 70 72 6f 67 72 61 6d 6d 65 72 20 69  the programmer i
2faab 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20  nterface to the 
2faac 6c 69 62 72 61 72 79 2e 20 20 52 6f 75 74 69 6e  library.  Routin
2faad 65 73 20 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66  es in.** other f
2faae 69 6c 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74  iles are for int
2faaf 65 72 6e 61 6c 20 75 73 65 20 62 79 20 53 51 4c  ernal use by SQL
2fab0 69 74 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e  ite and should n
2fab1 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 65  ot be.** accesse
2fab2 64 20 62 79 20 75 73 65 72 73 20 6f 66 20 74 68  d by users of th
2fab3 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  e library..**.**
2fab4 20 24 49 64 3a 20 6d 61 69 6e 2e 63 2c 76 20 31   $Id: main.c,v 1
2fab5 2e 35 34 38 20 32 30 30 39 2f 30 35 2f 30 36 20  .548 2009/05/06 
2fab6 31 39 3a 30 33 3a 31 34 20 64 72 68 20 45 78 70  19:03:14 drh Exp
2fab7 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51   $.*/..#ifdef SQ
2fab8 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
2fab9 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2faba 20 49 6e 63 6c 75 64 65 20 66 74 73 33 2e 68 20   Include fts3.h 
2fabb 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
2fabc 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
2fabd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2fabe 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2fabf 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
2fac0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
2fac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fac2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2fac3 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20  ./*.** 2006 Oct 
2fac4 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  10.**.** The aut
2fac5 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2fac6 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2fac7 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2fac8 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
2fac9 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
2faca 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
2facb 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
2facc 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
2facd 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
2face 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
2facf 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
2fad0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
2fad1 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
2fad2 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
2fad3 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
2fad4 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
2fad5 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
2fad6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fad7 2a 2a 2a 2a 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 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
2fadb 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73  s header file is
2fadc 20 75 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d   used by program
2fadd 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c  s that want to l
2fade 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a  ink against the.
2fadf 2a 2a 20 46 54 53 33 20 6c 69 62 72 61 72 79 2e  ** FTS3 library.
2fae0 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73    All it does is
2fae1 20 64 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c   declare the sql
2fae2 69 74 65 33 46 74 73 33 49 6e 69 74 28 29 20 69  ite3Fts3Init() i
2fae3 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69  nterface..*/..#i
2fae4 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b  f 0.extern "C" {
2fae5 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70  .#endif  /* __cp
2fae6 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49  lusplus */..SQLI
2fae7 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2fae8 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 73  qlite3Fts3Init(s
2fae9 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69  qlite3 *db);..#i
2faea 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e  f 0.}  /* extern
2faeb 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20   "C" */.#endif  
2faec 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a  /* __cplusplus *
2faed 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2faee 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 2e 68  ** End of fts3.h
2faef 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2faf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faf2 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2faf3 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
2faf4 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
2faf5 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a  in main.c ******
2faf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faf7 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  */.#endif.#ifdef
2faf8 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52   SQLITE_ENABLE_R
2faf9 54 52 45 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  TREE./**********
2fafa 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 72 74 72  **** Include rtr
2fafb 65 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  ee.h in the midd
2fafc 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a  le of main.c ***
2fafd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fafe 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2faff 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2fb00 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  rtree.h ********
2fb01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb03 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
2fb04 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65  May 26.**.** The
2fb05 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
2fb06 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
2fb07 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
2fb08 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
2fb09 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2fb0a 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2fb0b 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2fb0c 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2fb0d 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
2fb0e 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
2fb0f 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
2fb10 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
2fb11 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
2fb12 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2fb13 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
2fb14 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
2fb15 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
2fb16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
2fb1b 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
2fb1c 65 20 69 73 20 75 73 65 64 20 62 79 20 70 72 6f  e is used by pro
2fb1d 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20  grams that want 
2fb1e 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  to link against 
2fb1f 74 68 65 0a 2a 2a 20 52 54 52 45 45 20 6c 69 62  the.** RTREE lib
2fb20 72 61 72 79 2e 20 20 41 6c 6c 20 69 74 20 64 6f  rary.  All it do
2fb21 65 73 20 69 73 20 64 65 63 6c 61 72 65 20 74 68  es is declare th
2fb22 65 20 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e  e sqlite3RtreeIn
2fb23 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a  it() interface..
2fb24 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e  */..#if 0.extern
2fb25 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f   "C" {.#endif  /
2fb26 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f  * __cplusplus */
2fb27 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
2fb28 20 69 6e 74 20 73 71 6c 69 74 65 33 52 74 72 65   int sqlite3Rtre
2fb29 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  eInit(sqlite3 *d
2fb2a 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a  b);..#if 0.}  /*
2fb2b 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23   extern "C" */.#
2fb2c 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75  endif  /* __cplu
2fb2d 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  splus */../*****
2fb2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2fb2f 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   rtree.h *******
2fb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb32 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2fb33 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
2fb34 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
2fb35 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63  ft off in main.c
2fb36 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2fb37 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66  ********/.#endif
2fb38 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
2fb39 4e 41 42 4c 45 5f 49 43 55 0a 2f 2a 2a 2a 2a 2a  NABLE_ICU./*****
2fb3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
2fb3b 65 20 73 71 6c 69 74 65 69 63 75 2e 68 20 69 6e  e sqliteicu.h in
2fb3c 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d   the middle of m
2fb3d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ain.c **********
2fb3e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2fb3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2fb40 66 69 6c 65 20 73 71 6c 69 74 65 69 63 75 2e 68  file sqliteicu.h
2fb41 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2fb42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb43 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2fb44 32 30 30 38 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a  2008 May 26.**.*
2fb45 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2fb46 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2fb47 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2fb48 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2fb49 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2fb4a 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2fb4b 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2fb4c 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2fb4d 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2fb4e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2fb4f 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2fb50 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2fb51 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2fb52 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2fb53 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2fb54 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2fb55 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2fb56 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2fb57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
2fb5b 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
2fb5c 72 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 62  r file is used b
2fb5d 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20  y programs that 
2fb5e 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61  want to link aga
2fb5f 69 6e 73 74 20 74 68 65 0a 2a 2a 20 49 43 55 20  inst the.** ICU 
2fb60 65 78 74 65 6e 73 69 6f 6e 2e 20 20 41 6c 6c 20  extension.  All 
2fb61 69 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61  it does is decla
2fb62 72 65 20 74 68 65 20 73 71 6c 69 74 65 33 49 63  re the sqlite3Ic
2fb63 75 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63  uInit() interfac
2fb64 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74  e..*/..#if 0.ext
2fb65 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66  ern "C" {.#endif
2fb66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73    /* __cplusplus
2fb67 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
2fb68 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
2fb69 63 75 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a  cuInit(sqlite3 *
2fb6a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f  db);..#if 0.}  /
2fb6b 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a  * extern "C" */.
2fb6c 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c  #endif  /* __cpl
2fb6d 75 73 70 6c 75 73 20 2a 2f 0a 0a 0a 2f 2a 2a 2a  usplus */.../***
2fb6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2fb6f 6f 66 20 73 71 6c 69 74 65 69 63 75 2e 68 20 2a  of sqliteicu.h *
2fb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2fb73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
2fb74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
2fb75 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e  left off in main
2fb76 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2fb77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64  **********/.#end
2fb78 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 65  if../*.** The ve
2fb79 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62  rsion of the lib
2fb7a 72 61 72 79 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  rary.*/.#ifndef 
2fb7b 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
2fb7c 49 4f 4e 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  ION.SQLITE_API c
2fb7d 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65  onst char sqlite
2fb7e 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51  3_version[] = SQ
2fb7f 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 23 65  LITE_VERSION;.#e
2fb80 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20  ndif.SQLITE_API 
2fb81 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
2fb82 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76  te3_libversion(v
2fb83 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73 71 6c  oid){ return sql
2fb84 69 74 65 33 5f 76 65 72 73 69 6f 6e 3b 20 7d 0a  ite3_version; }.
2fb85 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2fb86 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
2fb87 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 7b 20  n_number(void){ 
2fb88 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 56 45  return SQLITE_VE
2fb89 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 3b 20 7d 0a  RSION_NUMBER; }.
2fb8a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2fb8b 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66  qlite3_threadsaf
2fb8c 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  e(void){ return 
2fb8d 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
2fb8e 45 3b 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e  E; }..#if !defin
2fb8f 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
2fb90 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 64  RACE) && defined
2fb91 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49  (SQLITE_ENABLE_I
2fb92 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 49 66  OTRACE)./*.** If
2fb93 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
2fb94 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20  unction pointer 
2fb95 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20  is not NULL and 
2fb96 69 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41  if.** SQLITE_ENA
2fb97 42 4c 45 5f 49 4f 54 52 41 43 45 20 69 73 20 65  BLE_IOTRACE is e
2fb98 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 6d 65 73  nabled, then mes
2fb99 73 61 67 65 73 20 64 65 73 63 72 69 62 69 6e 67  sages describing
2fb9a 0a 2a 2a 20 49 2f 4f 20 61 63 74 69 76 65 20 61  .** I/O active a
2fb9b 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67  re written using
2fb9c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20   this function. 
2fb9d 20 54 68 65 73 65 20 6d 65 73 73 61 67 65 73 0a   These messages.
2fb9e 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  ** are intended 
2fb9f 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 61 63  for debugging ac
2fba0 74 69 76 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2f 0a  tivity only..*/.
2fba1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2fba2 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 49 6f 54  oid (*sqlite3IoT
2fba3 72 61 63 65 29 28 63 6f 6e 73 74 20 63 68 61 72  race)(const char
2fba4 2a 2c 20 2e 2e 2e 29 20 3d 20 30 3b 0a 23 65 6e  *, ...) = 0;.#en
2fba5 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  dif../*.** If th
2fba6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62  e following glob
2fba7 61 6c 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e  al variable poin
2fba8 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77  ts to a string w
2fba9 68 69 63 68 20 69 73 20 74 68 65 0a 2a 2a 20 6e  hich is the.** n
2fbaa 61 6d 65 20 6f 66 20 61 20 64 69 72 65 63 74 6f  ame of a directo
2fbab 72 79 2c 20 74 68 65 6e 20 74 68 61 74 20 64 69  ry, then that di
2fbac 72 65 63 74 6f 72 79 20 77 69 6c 6c 20 62 65 20  rectory will be 
2fbad 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a  used to store.**
2fbae 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
2fbaf 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
2fbb0 20 74 68 65 20 22 50 52 41 47 4d 41 20 74 65 6d   the "PRAGMA tem
2fbb1 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72  p_store_director
2fbb2 79 22 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 2e 0a  y" SQL command..
2fbb3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  */.SQLITE_API ch
2fbb4 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65 6d 70  ar *sqlite3_temp
2fbb5 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a  _directory = 0;.
2fbb6 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
2fbb7 65 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a  e SQLite.  .**.*
2fbb8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  * This routine m
2fbb9 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 74 6f  ust be called to
2fbba 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
2fbbb 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2fbbc 6e 2c 0a 2a 2a 20 56 46 53 2c 20 61 6e 64 20 6d  n,.** VFS, and m
2fbbd 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 73 20  utex subsystems 
2fbbe 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e 67 20 61  prior to doing a
2fbbf 6e 79 20 73 65 72 69 6f 75 73 20 77 6f 72 6b 20  ny serious work 
2fbc0 77 69 74 68 0a 2a 2a 20 53 51 4c 69 74 65 2e 20  with.** SQLite. 
2fbc1 20 42 75 74 20 61 73 20 6c 6f 6e 67 20 61 73 20   But as long as 
2fbc2 79 6f 75 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 69  you do not compi
2fbc3 6c 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f  le with SQLITE_O
2fbc4 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 2a 2a 20  MIT_AUTOINIT.** 
2fbc5 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
2fbc6 6c 20 62 65 20 63 61 6c 6c 65 64 20 61 75 74 6f  l be called auto
2fbc7 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 6b 65 79  matically by key
2fbc8 20 72 6f 75 74 69 6e 65 73 20 73 75 63 68 20 61   routines such a
2fbc9 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65  s.** sqlite3_ope
2fbca 6e 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69  n().  .**.** Thi
2fbcb 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
2fbcc 6f 2d 6f 70 20 65 78 63 65 70 74 20 6f 6e 20 69  o-op except on i
2fbcd 74 73 20 76 65 72 79 20 66 69 72 73 74 20 63 61  ts very first ca
2fbce 6c 6c 20 66 6f 72 20 74 68 65 20 70 72 6f 63 65  ll for the proce
2fbcf 73 73 2c 0a 2a 2a 20 6f 72 20 66 6f 72 20 74 68  ss,.** or for th
2fbd0 65 20 66 69 72 73 74 20 63 61 6c 6c 20 61 66 74  e first call aft
2fbd1 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  er a call to sql
2fbd2 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 2e 0a 2a  ite3_shutdown..*
2fbd3 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
2fbd4 68 72 65 61 64 20 74 6f 20 63 61 6c 6c 20 74 68  hread to call th
2fbd5 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20  is routine runs 
2fbd6 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  the initializati
2fbd7 6f 6e 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c 65 74  on to.** complet
2fbd8 69 6f 6e 2e 20 20 49 66 20 73 75 62 73 65 71 75  ion.  If subsequ
2fbd9 65 6e 74 20 74 68 72 65 61 64 73 20 63 61 6c 6c  ent threads call
2fbda 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 62 65   this routine be
2fbdb 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 2a  fore the first.*
2fbdc 2a 20 74 68 72 65 61 64 20 68 61 73 20 66 69 6e  * thread has fin
2fbdd 69 73 68 65 64 20 74 68 65 20 69 6e 69 74 69 61  ished the initia
2fbde 6c 69 7a 61 74 69 6f 6e 20 70 72 6f 63 65 73 73  lization process
2fbdf 2c 20 74 68 65 6e 20 74 68 65 20 73 75 62 73 65  , then the subse
2fbe0 71 75 65 6e 74 0a 2a 2a 20 74 68 72 65 61 64 73  quent.** threads
2fbe1 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e 74 69   must block unti
2fbe2 6c 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65  l the first thre
2fbe3 61 64 20 66 69 6e 69 73 68 65 73 20 77 69 74 68  ad finishes with
2fbe4 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74   the initializat
2fbe5 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ion..**.** The f
2fbe6 69 72 73 74 20 74 68 72 65 61 64 20 6d 69 67 68  irst thread migh
2fbe7 74 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  t call this rout
2fbe8 69 6e 65 20 72 65 63 75 72 73 69 76 65 6c 79 2e  ine recursively.
2fbe9 20 20 52 65 63 75 72 73 69 76 65 0a 2a 2a 20 63    Recursive.** c
2fbea 61 6c 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75  alls to this rou
2fbeb 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  tine should not 
2fbec 62 6c 6f 63 6b 2c 20 6f 66 20 63 6f 75 72 73 65  block, of course
2fbed 2e 20 20 4f 74 68 65 72 77 69 73 65 20 74 68 65  .  Otherwise the
2fbee 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  .** initializati
2fbef 6f 6e 20 70 72 6f 63 65 73 73 20 77 6f 75 6c 64  on process would
2fbf0 20 6e 65 76 65 72 20 63 6f 6d 70 6c 65 74 65 2e   never complete.
2fbf1 0a 2a 2a 0a 2a 2a 20 4c 65 74 20 58 20 62 65 20  .**.** Let X be 
2fbf2 74 68 65 20 66 69 72 73 74 20 74 68 72 65 61 64  the first thread
2fbf3 20 74 6f 20 65 6e 74 65 72 20 74 68 69 73 20 72   to enter this r
2fbf4 6f 75 74 69 6e 65 2e 20 20 4c 65 74 20 59 20 62  outine.  Let Y b
2fbf5 65 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20  e some other.** 
2fbf6 74 68 72 65 61 64 2e 20 20 54 68 65 6e 20 77 68  thread.  Then wh
2fbf7 69 6c 65 20 74 68 65 20 69 6e 69 74 69 61 6c 20  ile the initial 
2fbf8 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68  invocation of th
2fbf9 69 73 20 72 6f 75 74 69 6e 65 20 62 79 20 58 20  is routine by X 
2fbfa 69 73 0a 2a 2a 20 69 6e 63 6f 6d 70 6c 65 74 65  is.** incomplete
2fbfb 2c 20 69 74 20 69 73 20 72 65 71 75 69 72 65 64  , it is required
2fbfc 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   that:.**.**    
2fbfd 2a 20 20 43 61 6c 6c 73 20 74 6f 20 74 68 69 73  *  Calls to this
2fbfe 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 59 20   routine from Y 
2fbff 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e 74 69 6c  must block until
2fc00 20 74 68 65 20 6f 75 74 65 72 2d 6d 6f 73 74 0a   the outer-most.
2fc01 2a 2a 20 20 20 20 20 20 20 63 61 6c 6c 20 62 79  **       call by
2fc02 20 58 20 63 6f 6d 70 6c 65 74 65 73 2e 0a 2a 2a   X completes..**
2fc03 0a 2a 2a 20 20 20 20 2a 20 20 52 65 63 75 72 73  .**    *  Recurs
2fc04 69 76 65 20 63 61 6c 6c 73 20 74 6f 20 74 68 69  ive calls to thi
2fc05 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 74  s routine from t
2fc06 68 72 65 61 64 20 58 20 72 65 74 75 72 6e 20 69  hread X return i
2fc07 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 20 20  mmediately.**   
2fc08 20 20 20 20 77 69 74 68 6f 75 74 20 62 6c 6f 63      without bloc
2fc09 6b 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  king..*/.SQLITE_
2fc0a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2fc0b 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29  initialize(void)
2fc0c 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
2fc0d 78 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 20 20  x *pMaster;     
2fc0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc0f 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 73 74 61   /* The main sta
2fc10 74 69 63 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69  tic mutex */.  i
2fc11 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
2fc12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc13 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2fc14 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 23  esult code */..#
2fc15 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
2fc16 54 5f 57 53 44 0a 20 20 72 63 20 3d 20 73 71 6c  T_WSD.  rc = sql
2fc17 69 74 65 33 5f 77 73 64 5f 69 6e 69 74 28 34 30  ite3_wsd_init(40
2fc18 39 36 2c 20 32 34 29 3b 0a 20 20 69 66 28 20 72  96, 24);.  if( r
2fc19 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
2fc1a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
2fc1b 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
2fc1c 49 66 20 53 51 4c 69 74 65 20 69 73 20 61 6c 72  If SQLite is alr
2fc1d 65 61 64 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20  eady completely 
2fc1e 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65  initialized, the
2fc1f 6e 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a  n this call.  **
2fc20 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74   to sqlite3_init
2fc21 69 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64 20  ialize() should 
2fc22 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 74  be a no-op.  But
2fc23 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74   the initializat
2fc24 69 6f 6e 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65  ion.  ** must be
2fc25 20 63 6f 6d 70 6c 65 74 65 2e 20 20 53 6f 20 69   complete.  So i
2fc26 73 49 6e 69 74 20 6d 75 73 74 20 6e 6f 74 20 62  sInit must not b
2fc27 65 20 73 65 74 20 75 6e 74 69 6c 20 74 68 65 20  e set until the 
2fc28 76 65 72 79 20 65 6e 64 0a 20 20 2a 2a 20 6f 66  very end.  ** of
2fc29 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 20   this routine.. 
2fc2a 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
2fc2b 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
2fc2c 49 6e 69 74 20 29 20 72 65 74 75 72 6e 20 53 51  Init ) return SQ
2fc2d 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4d  LITE_OK;..  /* M
2fc2e 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 75 74  ake sure the mut
2fc2f 65 78 20 73 75 62 73 79 73 74 65 6d 20 69 73 20  ex subsystem is 
2fc30 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 49 66  initialized.  If
2fc31 20 75 6e 61 62 6c 65 20 74 6f 20 0a 20 20 2a 2a   unable to .  **
2fc32 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
2fc33 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2c  mutex subsystem,
2fc34 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20 77 69   return early wi
2fc35 74 68 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 20  th the error..  
2fc36 2a 2a 20 49 66 20 74 68 65 20 73 79 73 74 65 6d  ** If the system
2fc37 20 69 73 20 73 6f 20 73 69 63 6b 20 74 68 61 74   is so sick that
2fc38 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 74   we are unable t
2fc39 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75 74  o allocate a mut
2fc3a 65 78 2c 0a 20 20 2a 2a 20 74 68 65 72 65 20 69  ex,.  ** there i
2fc3b 73 20 6e 6f 74 20 6d 75 63 68 20 53 51 4c 69 74  s not much SQLit
2fc3c 65 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 62 65  e is going to be
2fc3d 20 61 62 6c 65 20 74 6f 20 64 6f 2e 0a 20 20 2a   able to do..  *
2fc3e 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 75 74 65 78  *.  ** The mutex
2fc3f 20 73 75 62 73 79 73 74 65 6d 20 6d 75 73 74 20   subsystem must 
2fc40 74 61 6b 65 20 63 61 72 65 20 6f 66 20 73 65 72  take care of ser
2fc41 69 61 6c 69 7a 69 6e 67 20 69 74 73 20 6f 77 6e  ializing its own
2fc42 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61  .  ** initializa
2fc43 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  tion..  */.  rc 
2fc44 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e  = sqlite3MutexIn
2fc45 69 74 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29  it();.  if( rc )
2fc46 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
2fc47 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
2fc48 20 6d 61 6c 6c 6f 63 28 29 20 73 79 73 74 65 6d   malloc() system
2fc49 20 61 6e 64 20 74 68 65 20 72 65 63 75 72 73 69   and the recursi
2fc4a 76 65 20 70 49 6e 69 74 4d 75 74 65 78 20 6d 75  ve pInitMutex mu
2fc4b 74 65 78 2e 0a 20 20 2a 2a 20 54 68 69 73 20 6f  tex..  ** This o
2fc4c 70 65 72 61 74 69 6f 6e 20 69 73 20 70 72 6f 74  peration is prot
2fc4d 65 63 74 65 64 20 62 79 20 74 68 65 20 53 54 41  ected by the STA
2fc4e 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
2fc4f 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a  .  Note that.  *
2fc50 2a 20 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 69  * MutexAlloc() i
2fc51 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 73  s called for a s
2fc52 74 61 74 69 63 20 6d 75 74 65 78 20 70 72 69 6f  tatic mutex prio
2fc53 72 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 69 6e  r to initializin
2fc54 67 20 74 68 65 0a 20 20 2a 2a 20 6d 61 6c 6c 6f  g the.  ** mallo
2fc55 63 20 73 75 62 73 79 73 74 65 6d 20 2d 20 74 68  c subsystem - th
2fc56 69 73 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20  is implies that 
2fc57 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  the allocation o
2fc58 66 20 61 20 73 74 61 74 69 63 0a 20 20 2a 2a 20  f a static.  ** 
2fc59 6d 75 74 65 78 20 6d 75 73 74 20 6e 6f 74 20 72  mutex must not r
2fc5a 65 71 75 69 72 65 20 73 75 70 70 6f 72 74 20 66  equire support f
2fc5b 72 6f 6d 20 74 68 65 20 6d 61 6c 6c 6f 63 20 73  rom the malloc s
2fc5c 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20  ubsystem..  */. 
2fc5d 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74   pMaster = sqlit
2fc5e 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
2fc5f 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
2fc60 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c 69  _MASTER);.  sqli
2fc61 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2fc62 70 4d 61 73 74 65 72 29 3b 0a 20 20 69 66 28 20  pMaster);.  if( 
2fc63 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  !sqlite3GlobalCo
2fc64 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69  nfig.isMallocIni
2fc65 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  t ){.    rc = sq
2fc66 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28  lite3MallocInit(
2fc67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
2fc68 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2fc69 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2fc6a 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e  onfig.isMallocIn
2fc6b 69 74 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20  it = 1;.    if( 
2fc6c 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  !sqlite3GlobalCo
2fc6d 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20  nfig.pInitMutex 
2fc6e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2fc6f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e  GlobalConfig.pIn
2fc70 69 74 4d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  itMutex = sqlite
2fc71 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
2fc72 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
2fc73 56 45 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73  VE);.      if( s
2fc74 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fc75 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 26 26  ig.bCoreMutex &&
2fc76 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   !sqlite3GlobalC
2fc77 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
2fc78 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
2fc79 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2fc7a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2fc7b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2fc7c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
2fc7d 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2fc7e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 2b 2b 3b  nRefInitMutex++;
2fc7f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
2fc80 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74  utex_leave(pMast
2fc81 65 72 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 75 6e  er);..  /* If un
2fc82 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61 6c 69  able to initiali
2fc83 7a 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 73 75  ze the malloc su
2fc84 62 73 79 73 74 65 6d 2c 20 74 68 65 6e 20 72 65  bsystem, then re
2fc85 74 75 72 6e 20 65 61 72 6c 79 2e 0a 20 20 2a 2a  turn early..  **
2fc86 20 54 68 65 72 65 20 69 73 20 6c 69 74 74 6c 65   There is little
2fc87 20 68 6f 70 65 20 6f 66 20 67 65 74 74 69 6e 67   hope of getting
2fc88 20 53 51 4c 69 74 65 20 74 6f 20 72 75 6e 20 69   SQLite to run i
2fc89 66 20 74 68 65 20 6d 61 6c 6c 6f 63 0a 20 20 2a  f the malloc.  *
2fc8a 2a 20 73 75 62 73 79 73 74 65 6d 20 63 61 6e 6e  * subsystem cann
2fc8b 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ot be initialize
2fc8c 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  d..  */.  if( rc
2fc8d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
2fc8e 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
2fc8f 7d 0a 0a 20 20 2f 2a 20 44 6f 20 74 68 65 20 72  }..  /* Do the r
2fc90 65 73 74 20 6f 66 20 74 68 65 20 69 6e 69 74 69  est of the initi
2fc91 61 6c 69 7a 61 74 69 6f 6e 20 75 6e 64 65 72 20  alization under 
2fc92 74 68 65 20 72 65 63 75 72 73 69 76 65 20 6d 75  the recursive mu
2fc93 74 65 78 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74  tex so.  ** that
2fc94 20 77 65 20 77 69 6c 6c 20 62 65 20 61 62 6c 65   we will be able
2fc95 20 74 6f 20 68 61 6e 64 6c 65 20 72 65 63 75 72   to handle recur
2fc96 73 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a  sive calls into.
2fc97 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69    ** sqlite3_ini
2fc98 74 69 61 6c 69 7a 65 28 29 2e 20 20 54 68 65 20  tialize().  The 
2fc99 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20  recursive calls 
2fc9a 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6d 65 20 74 68  normally come th
2fc9b 72 6f 75 67 68 0a 20 20 2a 2a 20 73 71 6c 69 74  rough.  ** sqlit
2fc9c 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 77 68 65  e3_os_init() whe
2fc9d 6e 20 69 74 20 69 6e 76 6f 6b 65 73 20 73 71 6c  n it invokes sql
2fc9e 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
2fc9f 72 28 29 2c 20 62 75 74 20 6f 74 68 65 72 0a 20  r(), but other. 
2fca0 20 2a 2a 20 72 65 63 75 72 73 69 76 65 20 63 61   ** recursive ca
2fca1 6c 6c 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 62  lls might also b
2fca2 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 2a 2f  e possible..  */
2fca3 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2fca4 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 47 6c  _enter(sqlite3Gl
2fca5 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74  obalConfig.pInit
2fca6 4d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 73 71  Mutex);.  if( sq
2fca7 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2fca8 67 2e 69 73 49 6e 69 74 3d 3d 30 20 26 26 20 73  g.isInit==0 && s
2fca9 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fcaa 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 3d 3d 30  ig.inProgress==0
2fcab 20 29 7b 0a 20 20 20 20 46 75 6e 63 44 65 66 48   ){.    FuncDefH
2fcac 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 47 4c  ash *pHash = &GL
2fcad 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61 73 68  OBAL(FuncDefHash
2fcae 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46  , sqlite3GlobalF
2fcaf 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 20 20 73  unctions);.    s
2fcb0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fcb1 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 20 3d 20  ig.inProgress = 
2fcb2 31 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 48  1;.    memset(pH
2fcb3 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  ash, 0, sizeof(s
2fcb4 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63  qlite3GlobalFunc
2fcb5 74 69 6f 6e 73 29 29 3b 0a 20 20 20 20 73 71 6c  tions));.    sql
2fcb6 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62  ite3RegisterGlob
2fcb7 61 6c 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a 20  alFunctions();. 
2fcb8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
2fcb9 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28  cacheInitialize(
2fcba 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
2fcbb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2fcbc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f    rc = sqlite3_o
2fcbd 73 5f 69 6e 69 74 28 29 3b 0a 20 20 20 20 7d 0a  s_init();.    }.
2fcbe 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
2fcbf 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
2fcc0 71 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66  qlite3PCacheBuff
2fcc1 65 72 53 65 74 75 70 28 20 73 71 6c 69 74 65 33  erSetup( sqlite3
2fcc2 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61  GlobalConfig.pPa
2fcc3 67 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73  ge, .          s
2fcc4 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fcc5 69 67 2e 73 7a 50 61 67 65 2c 20 73 71 6c 69 74  ig.szPage, sqlit
2fcc6 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
2fcc7 50 61 67 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  Page);.      sql
2fcc8 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2fcc9 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20  .isInit = 1;.   
2fcca 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c   }.    sqlite3Gl
2fccb 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f  obalConfig.inPro
2fccc 67 72 65 73 73 20 3d 20 30 3b 0a 20 20 7d 0a 20  gress = 0;.  }. 
2fccd 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2fcce 65 61 76 65 28 73 71 6c 69 74 65 33 47 6c 6f 62  eave(sqlite3Glob
2fccf 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75  alConfig.pInitMu
2fcd0 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 47 6f 20 62  tex);..  /* Go b
2fcd1 61 63 6b 20 75 6e 64 65 72 20 74 68 65 20 73 74  ack under the st
2fcd2 61 74 69 63 20 6d 75 74 65 78 20 61 6e 64 20 63  atic mutex and c
2fcd3 6c 65 61 6e 20 75 70 20 74 68 65 20 72 65 63 75  lean up the recu
2fcd4 72 73 69 76 65 0a 20 20 2a 2a 20 6d 75 74 65 78  rsive.  ** mutex
2fcd5 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 72 65   to prevent a re
2fcd6 73 6f 75 72 63 65 20 6c 65 61 6b 2e 0a 20 20 2a  source leak..  *
2fcd7 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  /.  sqlite3_mute
2fcd8 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29  x_enter(pMaster)
2fcd9 3b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ;.  sqlite3Globa
2fcda 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74  lConfig.nRefInit
2fcdb 4d 75 74 65 78 2d 2d 3b 0a 20 20 69 66 28 20 73  Mutex--;.  if( s
2fcdc 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fcdd 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78  ig.nRefInitMutex
2fcde 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  <=0 ){.    asser
2fcdf 74 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  t( sqlite3Global
2fce0 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d  Config.nRefInitM
2fce1 75 74 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 73  utex==0 );.    s
2fce2 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65  qlite3_mutex_fre
2fce3 65 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e(sqlite3GlobalC
2fce4 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
2fce5 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  );.    sqlite3Gl
2fce6 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74  obalConfig.pInit
2fce7 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 20  Mutex = 0;.  }. 
2fce8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2fce9 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 0a  eave(pMaster);..
2fcea 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
2fceb 6e 67 20 69 73 20 6a 75 73 74 20 61 20 73 61 6e  ng is just a san
2fcec 69 74 79 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b  ity check to mak
2fced 65 20 73 75 72 65 20 53 51 4c 69 74 65 20 68 61  e sure SQLite ha
2fcee 73 0a 20 20 2a 2a 20 62 65 65 6e 20 63 6f 6d 70  s.  ** been comp
2fcef 69 6c 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 20  iled correctly. 
2fcf0 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   It is important
2fcf1 20 74 6f 20 72 75 6e 20 74 68 69 73 20 63 6f 64   to run this cod
2fcf2 65 2c 20 62 75 74 0a 20 20 2a 2a 20 77 65 20 64  e, but.  ** we d
2fcf3 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 72 75 6e  on't want to run
2fcf4 20 69 74 20 74 6f 6f 20 6f 66 74 65 6e 20 61 6e   it too often an
2fcf5 64 20 73 6f 61 6b 20 75 70 20 43 50 55 20 63 79  d soak up CPU cy
2fcf6 63 6c 65 73 20 66 6f 72 20 6e 6f 0a 20 20 2a 2a  cles for no.  **
2fcf7 20 72 65 61 73 6f 6e 2e 20 20 53 6f 20 77 65 20   reason.  So we 
2fcf8 72 75 6e 20 69 74 20 6f 6e 63 65 20 64 75 72 69  run it once duri
2fcf9 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ng initializatio
2fcfa 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  n..  */.#ifndef 
2fcfb 4e 44 45 42 55 47 0a 23 69 66 6e 64 65 66 20 53  NDEBUG.#ifndef S
2fcfc 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
2fcfd 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 2f 2a 20 54  ING_POINT.  /* T
2fcfe 68 69 73 20 73 65 63 74 69 6f 6e 20 6f 66 20 63  his section of c
2fcff 6f 64 65 27 73 20 6f 6e 6c 79 20 22 6f 75 74 70  ode's only "outp
2fd00 75 74 22 20 69 73 20 76 69 61 20 61 73 73 65 72  ut" is via asser
2fd01 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  t() statements. 
2fd02 2a 2f 0a 20 20 69 66 20 28 20 72 63 3d 3d 53 51  */.  if ( rc==SQ
2fd03 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75  LITE_OK ){.    u
2fd04 36 34 20 78 20 3d 20 28 28 28 75 36 34 29 31 29  64 x = (((u64)1)
2fd05 3c 3c 36 33 29 2d 31 3b 0a 20 20 20 20 64 6f 75  <<63)-1;.    dou
2fd06 62 6c 65 20 79 3b 0a 20 20 20 20 61 73 73 65 72  ble y;.    asser
2fd07 74 28 73 69 7a 65 6f 66 28 78 29 3d 3d 38 29 3b  t(sizeof(x)==8);
2fd08 0a 20 20 20 20 61 73 73 65 72 74 28 73 69 7a 65  .    assert(size
2fd09 6f 66 28 78 29 3d 3d 73 69 7a 65 6f 66 28 79 29  of(x)==sizeof(y)
2fd0a 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 79  );.    memcpy(&y
2fd0b 2c 20 26 78 2c 20 38 29 3b 0a 20 20 20 20 61 73  , &x, 8);.    as
2fd0c 73 65 72 74 28 20 73 71 6c 69 74 65 33 49 73 4e  sert( sqlite3IsN
2fd0d 61 4e 28 79 29 20 29 3b 0a 20 20 7d 0a 23 65 6e  aN(y) );.  }.#en
2fd0e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65  dif.#endif..  re
2fd0f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2fd10 2a 20 55 6e 64 6f 20 74 68 65 20 65 66 66 65 63  * Undo the effec
2fd11 74 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e  ts of sqlite3_in
2fd12 69 74 69 61 6c 69 7a 65 28 29 2e 20 20 4d 75 73  itialize().  Mus
2fd13 74 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  t not be called 
2fd14 77 68 69 6c 65 0a 2a 2a 20 74 68 65 72 65 20 61  while.** there a
2fd15 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64  re outstanding d
2fd16 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2fd17 6f 6e 73 20 6f 72 20 6d 65 6d 6f 72 79 20 61 6c  ons or memory al
2fd18 6c 6f 63 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20  locations or.** 
2fd19 77 68 69 6c 65 20 61 6e 79 20 70 61 72 74 20 6f  while any part o
2fd1a 66 20 53 51 4c 69 74 65 20 69 73 20 6f 74 68 65  f SQLite is othe
2fd1b 72 77 69 73 65 20 69 6e 20 75 73 65 20 69 6e 20  rwise in use in 
2fd1c 61 6e 79 20 74 68 72 65 61 64 2e 20 20 54 68 69  any thread.  Thi
2fd1d 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
2fd1e 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20  not threadsafe. 
2fd1f 20 42 75 74 20 69 74 20 69 73 20 73 61 66 65 20   But it is safe 
2fd20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 72  to invoke this r
2fd21 6f 75 74 69 6e 65 0a 2a 2a 20 6f 6e 20 77 68 65  outine.** on whe
2fd22 6e 20 53 51 4c 69 74 65 20 69 73 20 61 6c 72 65  n SQLite is alre
2fd23 61 64 79 20 73 68 75 74 20 64 6f 77 6e 2e 20 20  ady shut down.  
2fd24 49 66 20 53 51 4c 69 74 65 20 69 73 20 61 6c 72  If SQLite is alr
2fd25 65 61 64 79 20 73 68 75 74 20 64 6f 77 6e 0a 2a  eady shut down.*
2fd26 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  * when this rout
2fd27 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20  ine is invoked, 
2fd28 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
2fd29 65 20 69 73 20 61 20 68 61 72 6d 6c 65 73 73 20  e is a harmless 
2fd2a 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45  no-op..*/.SQLITE
2fd2b 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2fd2c 5f 73 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b  _shutdown(void){
2fd2d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
2fd2e 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69  obalConfig.isIni
2fd2f 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
2fd30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d  GlobalConfig.isM
2fd31 61 6c 6c 6f 63 49 6e 69 74 20 3d 20 30 3b 0a 20  allocInit = 0;. 
2fd32 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
2fd33 53 68 75 74 64 6f 77 6e 28 29 3b 0a 20 20 20 20  Shutdown();.    
2fd34 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29  sqlite3_os_end()
2fd35 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
2fd36 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69  set_auto_extensi
2fd37 6f 6e 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  on();.    sqlite
2fd38 33 4d 61 6c 6c 6f 63 45 6e 64 28 29 3b 0a 20 20  3MallocEnd();.  
2fd39 20 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e    sqlite3MutexEn
2fd3a 64 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  d();.    sqlite3
2fd3b 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49  GlobalConfig.isI
2fd3c 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  nit = 0;.  }.  r
2fd3d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2fd3e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 41  .}../*.** This A
2fd3f 50 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c 69 63  PI allows applic
2fd40 61 74 69 6f 6e 73 20 74 6f 20 6d 6f 64 69 66 79  ations to modify
2fd41 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66   the global conf
2fd42 69 67 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20  iguration of.** 
2fd43 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
2fd44 72 79 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 0a  ry at run-time..
2fd45 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2fd46 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62  ne should only b
2fd47 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  e called when th
2fd48 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74  ere are no outst
2fd49 61 6e 64 69 6e 67 0a 2a 2a 20 64 61 74 61 62 61  anding.** databa
2fd4a 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f  se connections o
2fd4b 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  r memory allocat
2fd4c 69 6f 6e 73 2e 20 20 54 68 69 73 20 72 6f 75 74  ions.  This rout
2fd4d 69 6e 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 74 68  ine is not.** th
2fd4e 72 65 61 64 73 61 66 65 2e 20 20 46 61 69 6c 75  readsafe.  Failu
2fd4f 72 65 20 74 6f 20 68 65 65 64 20 74 68 65 73 65  re to heed these
2fd50 20 77 61 72 6e 69 6e 67 73 20 63 61 6e 20 6c 65   warnings can le
2fd51 61 64 20 74 6f 20 75 6e 70 72 65 64 69 63 74 61  ad to unpredicta
2fd52 62 6c 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e  ble.** behavior.
2fd53 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2fd54 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  nt sqlite3_confi
2fd55 67 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a  g(int op, ...){.
2fd56 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
2fd57 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2fd58 4f 4b 3b 0a 0a 20 20 2f 2a 20 73 71 6c 69 74 65  OK;..  /* sqlite
2fd59 33 5f 63 6f 6e 66 69 67 28 29 20 73 68 61 6c 6c  3_config() shall
2fd5a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
2fd5b 49 53 55 53 45 20 69 66 20 69 74 20 69 73 20 69  ISUSE if it is i
2fd5c 6e 76 6f 6b 65 64 20 77 68 69 6c 65 0a 20 20 2a  nvoked while.  *
2fd5d 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  * the SQLite lib
2fd5e 72 61 72 79 20 69 73 20 69 6e 20 75 73 65 2e 20  rary is in use. 
2fd5f 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
2fd60 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49  GlobalConfig.isI
2fd61 6e 69 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c  nit ) return SQL
2fd62 49 54 45 5f 4d 49 53 55 53 45 3b 0a 0a 20 20 76  ITE_MISUSE;..  v
2fd63 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b  a_start(ap, op);
2fd64 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
2fd65 0a 0a 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63  ..    /* Mutex c
2fd66 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
2fd67 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 20 61 76  ions are only av
2fd68 61 69 6c 61 62 6c 65 20 69 6e 20 61 20 74 68 72  ailable in a thr
2fd69 65 61 64 73 61 66 65 0a 20 20 20 20 2a 2a 20 63  eadsafe.    ** c
2fd6a 6f 6d 70 69 6c 65 2e 20 0a 20 20 20 20 2a 2f 0a  ompile. .    */.
2fd6b 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
2fd6c 44 53 41 46 45 0a 20 20 20 20 63 61 73 65 20 53  DSAFE.    case S
2fd6d 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e  QLITE_CONFIG_SIN
2fd6e 47 4c 45 54 48 52 45 41 44 3a 20 7b 0a 20 20 20  GLETHREAD: {.   
2fd6f 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 61 6c     /* Disable al
2fd70 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20  l mutexing */.  
2fd71 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2fd72 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
2fd73 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  ex = 0;.      sq
2fd74 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2fd75 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20 30  g.bFullMutex = 0
2fd76 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2fd77 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
2fd78 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54  LITE_CONFIG_MULT
2fd79 49 54 48 52 45 41 44 3a 20 7b 0a 20 20 20 20 20  ITHREAD: {.     
2fd7a 20 2f 2a 20 44 69 73 61 62 6c 65 20 6d 75 74 65   /* Disable mute
2fd7b 78 69 6e 67 20 6f 66 20 64 61 74 61 62 61 73 65  xing of database
2fd7c 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a   connections */.
2fd7d 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
2fd7e 6d 75 74 65 78 69 6e 67 20 6f 66 20 63 6f 72 65  mutexing of core
2fd7f 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
2fd80 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2fd81 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
2fd82 6f 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20  oreMutex = 1;.  
2fd83 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2fd84 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74  lConfig.bFullMut
2fd85 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72  ex = 0;.      br
2fd86 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
2fd87 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
2fd88 47 5f 53 45 52 49 41 4c 49 5a 45 44 3a 20 7b 0a  G_SERIALIZED: {.
2fd89 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
2fd8a 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a  all mutexing */.
2fd8b 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
2fd8c 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
2fd8d 75 74 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20  utex = 1;.      
2fd8e 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2fd8f 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d  fig.bFullMutex =
2fd90 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   1;.      break;
2fd91 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2fd92 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
2fd93 54 45 58 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  TEX: {.      /* 
2fd94 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72  Specify an alter
2fd95 6e 61 74 69 76 65 20 6d 75 74 65 78 20 69 6d 70  native mutex imp
2fd96 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
2fd97 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2fd98 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 20 3d  alConfig.mutex =
2fd99 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c   *va_arg(ap, sql
2fd9a 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
2fd9b 64 73 2a 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ds*);.      brea
2fd9c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
2fd9d 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
2fd9e 47 45 54 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20  GETMUTEX: {.    
2fd9f 20 20 2f 2a 20 52 65 74 72 69 65 76 65 20 74 68    /* Retrieve th
2fda0 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 20  e current mutex 
2fda1 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
2fda2 2f 0a 20 20 20 20 20 20 2a 76 61 5f 61 72 67 28  /.      *va_arg(
2fda3 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ap, sqlite3_mute
2fda4 78 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71  x_methods*) = sq
2fda5 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2fda6 67 2e 6d 75 74 65 78 3b 0a 20 20 20 20 20 20 62  g.mutex;.      b
2fda7 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
2fda8 69 66 0a 0a 0a 20 20 20 20 63 61 73 65 20 53 51  if...    case SQ
2fda9 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c  LITE_CONFIG_MALL
2fdaa 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53  OC: {.      /* S
2fdab 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
2fdac 61 74 69 76 65 20 6d 61 6c 6c 6f 63 20 69 6d 70  ative malloc imp
2fdad 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
2fdae 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2fdaf 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 76 61  alConfig.m = *va
2fdb0 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33  _arg(ap, sqlite3
2fdb1 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a  _mem_methods*);.
2fdb2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2fdb3 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
2fdb4 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c  TE_CONFIG_GETMAL
2fdb5 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  LOC: {.      /* 
2fdb6 52 65 74 72 69 65 76 65 20 74 68 65 20 63 75 72  Retrieve the cur
2fdb7 72 65 6e 74 20 6d 61 6c 6c 6f 63 28 29 20 69 6d  rent malloc() im
2fdb8 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  plementation */.
2fdb9 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2fdba 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
2fdbb 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 20 73 71 6c  xMalloc==0 ) sql
2fdbc 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c  ite3MemSetDefaul
2fdbd 74 28 29 3b 0a 20 20 20 20 20 20 2a 76 61 5f 61  t();.      *va_a
2fdbe 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d  rg(ap, sqlite3_m
2fdbf 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73  em_methods*) = s
2fdc0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fdc1 69 67 2e 6d 3b 0a 20 20 20 20 20 20 62 72 65 61  ig.m;.      brea
2fdc2 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
2fdc3 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
2fdc4 4d 45 4d 53 54 41 54 55 53 3a 20 7b 0a 20 20 20  MEMSTATUS: {.   
2fdc5 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6f 72 20     /* Enable or 
2fdc6 64 69 73 61 62 6c 65 20 74 68 65 20 6d 61 6c 6c  disable the mall
2fdc7 6f 63 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 63  oc status collec
2fdc8 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71  tion */.      sq
2fdc9 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2fdca 67 2e 62 4d 65 6d 73 74 61 74 20 3d 20 76 61 5f  g.bMemstat = va_
2fdcb 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
2fdcc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2fdcd 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2fdce 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3a  _CONFIG_SCRATCH:
2fdcf 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69   {.      /* Desi
2fdd0 67 6e 61 74 65 20 61 20 62 75 66 66 65 72 20 66  gnate a buffer f
2fdd1 6f 72 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72  or scratch memor
2fdd2 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20  y space */.     
2fdd3 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2fdd4 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20  nfig.pScratch = 
2fdd5 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a  va_arg(ap, void*
2fdd6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2fdd7 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
2fdd8 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72 67 28  cratch = va_arg(
2fdd9 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
2fdda 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2fddb 66 69 67 2e 6e 53 63 72 61 74 63 68 20 3d 20 76  fig.nScratch = v
2fddc 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
2fddd 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2fdde 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
2fddf 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
2fde0 43 48 45 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  CHE: {.      /* 
2fde1 44 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66  Designate a buff
2fde2 65 72 20 66 6f 72 20 73 63 72 61 74 63 68 20 6d  er for scratch m
2fde3 65 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20  emory space */. 
2fde4 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2fde5 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d  alConfig.pPage =
2fde6 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64   va_arg(ap, void
2fde7 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  *);.      sqlite
2fde8 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
2fde9 50 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70  Page = va_arg(ap
2fdea 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71  , int);.      sq
2fdeb 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2fdec 67 2e 6e 50 61 67 65 20 3d 20 76 61 5f 61 72 67  g.nPage = va_arg
2fded 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
2fdee 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
2fdef 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
2fdf0 4f 4e 46 49 47 5f 50 43 41 43 48 45 3a 20 7b 0a  ONFIG_PCACHE: {.
2fdf1 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 79        /* Specify
2fdf2 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20   an alternative 
2fdf3 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74  malloc implement
2fdf4 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73  ation */.      s
2fdf5 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fdf6 69 67 2e 70 63 61 63 68 65 20 3d 20 2a 76 61 5f  ig.pcache = *va_
2fdf7 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f  arg(ap, sqlite3_
2fdf8 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29  pcache_methods*)
2fdf9 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2fdfa 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53     }..    case S
2fdfb 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
2fdfc 50 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20 20  PCACHE: {.      
2fdfd 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
2fdfe 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78  lConfig.pcache.x
2fdff 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Init==0 ){.     
2fe00 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65     sqlite3PCache
2fe01 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20  SetDefault();.  
2fe02 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 76 61 5f      }.      *va_
2fe03 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f  arg(ap, sqlite3_
2fe04 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29  pcache_methods*)
2fe05 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
2fe06 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 3b 0a 20  Config.pcache;. 
2fe07 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2fe08 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  }..#if defined(S
2fe09 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
2fe0a 53 59 53 33 29 20 7c 7c 20 64 65 66 69 6e 65 64  SYS3) || defined
2fe0b 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d  (SQLITE_ENABLE_M
2fe0c 45 4d 53 59 53 35 29 0a 20 20 20 20 63 61 73 65  EMSYS5).    case
2fe0d 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48   SQLITE_CONFIG_H
2fe0e 45 41 50 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  EAP: {.      /* 
2fe0f 44 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66  Designate a buff
2fe10 65 72 20 66 6f 72 20 68 65 61 70 20 6d 65 6d 6f  er for heap memo
2fe11 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20  ry space */.    
2fe12 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2fe13 6f 6e 66 69 67 2e 70 48 65 61 70 20 3d 20 76 61  onfig.pHeap = va
2fe14 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b  _arg(ap, void*);
2fe15 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
2fe16 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70  obalConfig.nHeap
2fe17 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
2fe18 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
2fe19 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e  3GlobalConfig.mn
2fe1a 52 65 71 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  Req = va_arg(ap,
2fe1b 20 69 6e 74 29 3b 0a 0a 20 20 20 20 20 20 69 66   int);..      if
2fe1c 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
2fe1d 6f 6e 66 69 67 2e 70 48 65 61 70 3d 3d 30 20 29  onfig.pHeap==0 )
2fe1e 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
2fe1f 74 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65 72  the heap pointer
2fe20 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72   is NULL, then r
2fe21 65 73 74 6f 72 65 20 74 68 65 20 6d 61 6c 6c 6f  estore the mallo
2fe22 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  c implementation
2fe23 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 61 63 6b  .        ** back
2fe24 20 74 6f 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72   to NULL pointer
2fe25 73 20 74 6f 6f 2e 20 20 54 68 69 73 20 77 69 6c  s too.  This wil
2fe26 6c 20 63 61 75 73 65 20 74 68 65 20 6d 61 6c 6c  l cause the mall
2fe27 6f 63 20 74 6f 20 67 6f 0a 20 20 20 20 20 20 20  oc to go.       
2fe28 20 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20   ** back to its 
2fe29 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e  default implemen
2fe2a 74 61 74 69 6f 6e 20 77 68 65 6e 20 73 71 6c 69  tation when sqli
2fe2b 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
2fe2c 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 72   is.        ** r
2fe2d 75 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  un..        */. 
2fe2e 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 73         memset(&s
2fe2f 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fe30 69 67 2e 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  ig.m, 0, sizeof(
2fe31 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2fe32 66 69 67 2e 6d 29 29 3b 0a 20 20 20 20 20 20 7d  fig.m));.      }
2fe33 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
2fe34 20 54 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65   The heap pointe
2fe35 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  r is not NULL, t
2fe36 68 65 6e 20 69 6e 73 74 61 6c 6c 20 6f 6e 65 20  hen install one 
2fe37 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  of the.        *
2fe38 2a 20 6d 65 6d 35 2e 63 2f 6d 65 6d 33 2e 63 20  * mem5.c/mem3.c 
2fe39 6d 65 74 68 6f 64 73 2e 20 49 66 20 6e 65 69 74  methods. If neit
2fe3a 68 65 72 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  her ENABLE_MEMSY
2fe3b 53 33 20 6e 6f 72 0a 20 20 20 20 20 20 20 20 2a  S3 nor.        *
2fe3c 2a 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  * ENABLE_MEMSYS5
2fe3d 20 69 73 20 64 65 66 69 6e 65 64 2c 20 72 65 74   is defined, ret
2fe3e 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  urn an error..  
2fe3f 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66        ** the def
2fe40 61 75 6c 74 20 63 61 73 65 20 61 6e 64 20 72 65  ault case and re
2fe41 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20  turn an error.. 
2fe42 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 64 65 66         */.#ifdef
2fe43 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
2fe44 45 4d 53 59 53 33 0a 20 20 20 20 20 20 20 20 73  EMSYS3.        s
2fe45 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fe46 69 67 2e 6d 20 3d 20 2a 73 71 6c 69 74 65 33 4d  ig.m = *sqlite3M
2fe47 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 29 3b 0a  emGetMemsys3();.
2fe48 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
2fe49 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
2fe4a 59 53 35 0a 20 20 20 20 20 20 20 20 73 71 6c 69  YS5.        sqli
2fe4b 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2fe4c 6d 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47  m = *sqlite3MemG
2fe4d 65 74 4d 65 6d 73 79 73 35 28 29 3b 0a 23 65 6e  etMemsys5();.#en
2fe4e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  dif.      }.    
2fe4f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
2fe50 65 6e 64 69 66 0a 0a 20 20 20 20 63 61 73 65 20  endif..    case 
2fe51 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f  SQLITE_CONFIG_LO
2fe52 4f 4b 41 53 49 44 45 3a 20 7b 0a 20 20 20 20 20  OKASIDE: {.     
2fe53 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2fe54 6e 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65  nfig.szLookaside
2fe55 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
2fe56 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
2fe57 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c  3GlobalConfig.nL
2fe58 6f 6f 6b 61 73 69 64 65 20 3d 20 76 61 5f 61 72  ookaside = va_ar
2fe59 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
2fe5a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
2fe5b 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
2fe5c 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
2fe5d 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72  _ERROR;.      br
2fe5e 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
2fe5f 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72   va_end(ap);.  r
2fe60 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
2fe61 2a 2a 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f  ** Set up the lo
2fe62 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 73 20  okaside buffers 
2fe63 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63  for a database c
2fe64 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 20 52 65  onnection..** Re
2fe65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
2fe66 6e 20 73 75 63 63 65 73 73 2e 20 20 0a 2a 2a 20  n success.  .** 
2fe67 49 66 20 6c 6f 6f 6b 61 73 69 64 65 20 69 73 20  If lookaside is 
2fe68 61 6c 72 65 61 64 79 20 61 63 74 69 76 65 2c 20  already active, 
2fe69 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
2fe6a 53 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a  SY..**.** The sz
2fe6b 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
2fe6c 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
2fe6d 73 20 69 6e 20 65 61 63 68 20 6c 6f 6f 6b 61 73  s in each lookas
2fe6e 69 64 65 20 73 6c 6f 74 2e 0a 2a 2a 20 54 68 65  ide slot..** The
2fe6f 20 63 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69   cnt parameter i
2fe70 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
2fe71 73 6c 6f 74 73 2e 20 20 49 66 20 70 53 74 61 72  slots.  If pStar
2fe72 74 20 69 73 20 4e 55 4c 4c 20 74 68 65 0a 2a 2a  t is NULL the.**
2fe73 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 6c   space for the l
2fe74 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20  ookaside memory 
2fe75 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
2fe76 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
2fe77 29 2e 0a 2a 2a 20 49 66 20 70 53 74 61 72 74 20  )..** If pStart 
2fe78 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e  is not NULL then
2fe79 20 69 74 20 69 73 20 73 7a 2a 63 6e 74 20 62 79   it is sz*cnt by
2fe7a 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f  tes of memory to
2fe7b 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20   use for.** the 
2fe7c 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
2fe7d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2fe7e 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 73  setupLookaside(s
2fe7f 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
2fe80 20 2a 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20   *pBuf, int sz, 
2fe81 69 6e 74 20 63 6e 74 29 7b 0a 20 20 76 6f 69 64  int cnt){.  void
2fe82 20 2a 70 53 74 61 72 74 3b 0a 20 20 69 66 28 20   *pStart;.  if( 
2fe83 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f  db->lookaside.nO
2fe84 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ut ){.    return
2fe85 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
2fe86 7d 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20  }.  /* Free any 
2fe87 65 78 69 73 74 69 6e 67 20 6c 6f 6f 6b 61 73 69  existing lookasi
2fe88 64 65 20 62 75 66 66 65 72 20 66 6f 72 20 74 68  de buffer for th
2fe89 69 73 20 68 61 6e 64 6c 65 20 62 65 66 6f 72 65  is handle before
2fe8a 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6e 67  .  ** allocating
2fe8b 20 61 20 6e 65 77 20 6f 6e 65 20 73 6f 20 77 65   a new one so we
2fe8c 20 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 68   don't have to h
2fe8d 61 76 65 20 73 70 61 63 65 20 66 6f 72 20 0a 20  ave space for . 
2fe8e 20 2a 2a 20 62 6f 74 68 20 61 74 20 74 68 65 20   ** both at the 
2fe8f 73 61 6d 65 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a  same time..  */.
2fe90 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73    if( db->lookas
2fe91 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 29 7b  ide.bMalloced ){
2fe92 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
2fe93 65 28 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  e(db->lookaside.
2fe94 70 53 74 61 72 74 29 3b 0a 20 20 7d 0a 20 20 2f  pStart);.  }.  /
2fe95 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20  * The size of a 
2fe96 6c 6f 6f 6b 61 73 69 64 65 20 73 6c 6f 74 20 6e  lookaside slot n
2fe97 65 65 64 73 20 74 6f 20 62 65 20 6c 61 72 67 65  eeds to be large
2fe98 72 20 74 68 61 6e 20 61 20 70 6f 69 6e 74 65 72  r than a pointer
2fe99 0a 20 20 2a 2a 20 74 6f 20 62 65 20 75 73 65 66  .  ** to be usef
2fe9a 75 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ul..  */.  if( s
2fe9b 7a 3c 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28 4c  z<=(int)sizeof(L
2fe9c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 20 29  ookasideSlot*) )
2fe9d 20 73 7a 20 3d 20 30 3b 0a 20 20 69 66 28 20 63   sz = 0;.  if( c
2fe9e 6e 74 3c 30 20 29 20 63 6e 74 20 3d 20 30 3b 0a  nt<0 ) cnt = 0;.
2fe9f 20 20 69 66 28 20 73 7a 3d 3d 30 20 7c 7c 20 63    if( sz==0 || c
2fea0 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 7a 20  nt==0 ){.    sz 
2fea1 3d 20 30 3b 0a 20 20 20 20 70 53 74 61 72 74 20  = 0;.    pStart 
2fea2 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 0;.  }else if(
2fea3 20 70 42 75 66 3d 3d 30 20 29 7b 0a 20 20 20 20   pBuf==0 ){.    
2fea4 73 7a 20 3d 20 52 4f 55 4e 44 38 28 73 7a 29 3b  sz = ROUND8(sz);
2fea5 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
2fea6 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
2fea7 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20 73 71  .    pStart = sq
2fea8 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 7a 2a  lite3Malloc( sz*
2fea9 63 6e 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  cnt );.    sqlit
2feaa 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
2feab 63 28 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  c();.  }else{.  
2feac 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e    sz = ROUNDDOWN
2fead 38 28 73 7a 29 3b 0a 20 20 20 20 70 53 74 61 72  8(sz);.    pStar
2feae 74 20 3d 20 70 42 75 66 3b 0a 20 20 7d 0a 20 20  t = pBuf;.  }.  
2feaf 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53  db->lookaside.pS
2feb0 74 61 72 74 20 3d 20 70 53 74 61 72 74 3b 0a 20  tart = pStart;. 
2feb1 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
2feb2 46 72 65 65 20 3d 20 30 3b 0a 20 20 64 62 2d 3e  Free = 0;.  db->
2feb3 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 3d 20 28  lookaside.sz = (
2feb4 75 31 36 29 73 7a 3b 0a 20 20 69 66 28 20 70 53  u16)sz;.  if( pS
2feb5 74 61 72 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  tart ){.    int 
2feb6 69 3b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65  i;.    Lookaside
2feb7 53 6c 6f 74 20 2a 70 3b 0a 20 20 20 20 61 73 73  Slot *p;.    ass
2feb8 65 72 74 28 20 73 7a 20 3e 20 28 69 6e 74 29 73  ert( sz > (int)s
2feb9 69 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64 65 53  izeof(LookasideS
2feba 6c 6f 74 2a 29 20 29 3b 0a 20 20 20 20 70 20 3d  lot*) );.    p =
2febb 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a   (LookasideSlot*
2febc 29 70 53 74 61 72 74 3b 0a 20 20 20 20 66 6f 72  )pStart;.    for
2febd 28 69 3d 63 6e 74 2d 31 3b 20 69 3e 3d 30 3b 20  (i=cnt-1; i>=0; 
2febe 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  i--){.      p->p
2febf 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  Next = db->looka
2fec0 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 20  side.pFree;.    
2fec1 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
2fec2 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20  pFree = p;.     
2fec3 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53   p = (LookasideS
2fec4 6c 6f 74 2a 29 26 28 28 75 38 2a 29 70 29 5b 73  lot*)&((u8*)p)[s
2fec5 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  z];.    }.    db
2fec6 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64  ->lookaside.pEnd
2fec7 20 3d 20 70 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f   = p;.    db->lo
2fec8 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
2fec9 20 3d 20 31 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f   = 1;.    db->lo
2feca 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65  okaside.bMalloce
2fecb 64 20 3d 20 70 42 75 66 3d 3d 30 20 3f 31 3a 30  d = pBuf==0 ?1:0
2fecc 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
2fecd 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e  b->lookaside.pEn
2fece 64 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6c  d = 0;.    db->l
2fecf 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
2fed0 64 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6c  d = 0;.    db->l
2fed1 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63  ookaside.bMalloc
2fed2 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ed = 0;.  }.  re
2fed3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2fed4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2fed5 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69  the mutex associ
2fed6 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74 61  ated with a data
2fed7 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
2fed8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
2fed9 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71  qlite3_mutex *sq
2feda 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73  lite3_db_mutex(s
2fedb 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72  qlite3 *db){.  r
2fedc 65 74 75 72 6e 20 64 62 2d 3e 6d 75 74 65 78 3b  eturn db->mutex;
2fedd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67  .}../*.** Config
2fede 75 72 61 74 69 6f 6e 20 73 65 74 74 69 6e 67 73  uration settings
2fedf 20 66 6f 72 20 61 6e 20 69 6e 64 69 76 69 64 75   for an individu
2fee0 61 6c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  al database conn
2fee1 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45  ection.*/.SQLITE
2fee2 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2fee3 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74  _db_config(sqlit
2fee4 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 70 2c 20  e3 *db, int op, 
2fee5 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
2fee6 61 70 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  ap;.  int rc;.  
2fee7 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29  va_start(ap, op)
2fee8 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  ;.  switch( op )
2fee9 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
2feea 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  E_DBCONFIG_LOOKA
2feeb 53 49 44 45 3a 20 7b 0a 20 20 20 20 20 20 76 6f  SIDE: {.      vo
2feec 69 64 20 2a 70 42 75 66 20 3d 20 76 61 5f 61 72  id *pBuf = va_ar
2feed 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20  g(ap, void*);.  
2feee 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 76 61 5f      int sz = va_
2feef 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
2fef0 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 76 61      int cnt = va
2fef1 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
2fef2 20 20 20 20 20 72 63 20 3d 20 73 65 74 75 70 4c       rc = setupL
2fef3 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 42 75  ookaside(db, pBu
2fef4 66 2c 20 73 7a 2c 20 63 6e 74 29 3b 0a 20 20 20  f, sz, cnt);.   
2fef5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2fef6 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
2fef7 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
2fef8 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72  _ERROR;.      br
2fef9 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
2fefa 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72   va_end(ap);.  r
2fefb 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
2fefc 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
2fefd 69 66 20 74 68 65 20 62 75 66 66 65 72 20 7a 5b  if the buffer z[
2fefe 30 2e 2e 6e 2d 31 5d 20 63 6f 6e 74 61 69 6e 73  0..n-1] contains
2feff 20 61 6c 6c 20 73 70 61 63 65 73 2e 0a 2a 2f 0a   all spaces..*/.
2ff00 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 53 70  static int allSp
2ff01 61 63 65 73 28 63 6f 6e 73 74 20 63 68 61 72 20  aces(const char 
2ff02 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 77 68  *z, int n){.  wh
2ff03 69 6c 65 28 20 6e 3e 30 20 26 26 20 7a 5b 6e 2d  ile( n>0 && z[n-
2ff04 31 5d 3d 3d 27 20 27 20 29 7b 20 6e 2d 2d 3b 20  1]==' ' ){ n--; 
2ff05 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3d 3d 30 3b  }.  return n==0;
2ff06 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
2ff07 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f  s the default co
2ff08 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
2ff09 20 6e 61 6d 65 64 20 22 42 49 4e 41 52 59 22 20   named "BINARY" 
2ff0a 77 68 69 63 68 20 69 73 20 61 6c 77 61 79 73 0a  which is always.
2ff0b 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a  ** available..**
2ff0c 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 64 46 6c  .** If the padFl
2ff0d 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  ag argument is n
2ff0e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 73 70 61  ot NULL then spa
2ff0f 63 65 20 70 61 64 64 69 6e 67 20 61 74 20 74 68  ce padding at th
2ff10 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 73 74 72 69  e end.** of stri
2ff11 6e 67 73 20 69 73 20 69 67 6e 6f 72 65 64 2e 20  ngs is ignored. 
2ff12 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 73   This implements
2ff13 20 74 68 65 20 52 54 52 49 4d 20 63 6f 6c 6c 61   the RTRIM colla
2ff14 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
2ff15 69 6e 74 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 28  int binCollFunc(
2ff16 0a 20 20 76 6f 69 64 20 2a 70 61 64 46 6c 61 67  .  void *padFlag
2ff17 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63  ,.  int nKey1, c
2ff18 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
2ff19 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c 20 63  ,.  int nKey2, c
2ff1a 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
2ff1b 0a 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 3b  .){.  int rc, n;
2ff1c 0a 20 20 6e 20 3d 20 6e 4b 65 79 31 3c 6e 4b 65  .  n = nKey1<nKe
2ff1d 79 32 20 3f 20 6e 4b 65 79 31 20 3a 20 6e 4b 65  y2 ? nKey1 : nKe
2ff1e 79 32 3b 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d  y2;.  rc = memcm
2ff1f 70 28 70 4b 65 79 31 2c 20 70 4b 65 79 32 2c 20  p(pKey1, pKey2, 
2ff20 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20  n);.  if( rc==0 
2ff21 29 7b 0a 20 20 20 20 69 66 28 20 70 61 64 46 6c  ){.    if( padFl
2ff22 61 67 0a 20 20 20 20 20 26 26 20 61 6c 6c 53 70  ag.     && allSp
2ff23 61 63 65 73 28 28 28 63 68 61 72 2a 29 70 4b 65  aces(((char*)pKe
2ff24 79 31 29 2b 6e 2c 20 6e 4b 65 79 31 2d 6e 29 0a  y1)+n, nKey1-n).
2ff25 20 20 20 20 20 26 26 20 61 6c 6c 53 70 61 63 65       && allSpace
2ff26 73 28 28 28 63 68 61 72 2a 29 70 4b 65 79 32 29  s(((char*)pKey2)
2ff27 2b 6e 2c 20 6e 4b 65 79 32 2d 6e 29 0a 20 20 20  +n, nKey2-n).   
2ff28 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 61   ){.      /* Lea
2ff29 76 65 20 72 63 20 75 6e 63 68 61 6e 67 65 64 20  ve rc unchanged 
2ff2a 61 74 20 30 20 2a 2f 0a 20 20 20 20 7d 65 6c 73  at 0 */.    }els
2ff2b 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 4b  e{.      rc = nK
2ff2c 65 79 31 20 2d 20 6e 4b 65 79 32 3b 0a 20 20 20  ey1 - nKey2;.   
2ff2d 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2ff2e 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 6f  rc;.}../*.** Ano
2ff2f 74 68 65 72 20 62 75 69 6c 74 2d 69 6e 20 63 6f  ther built-in co
2ff30 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2ff31 3a 20 4e 4f 43 41 53 45 2e 20 0a 2a 2a 0a 2a 2a  : NOCASE. .**.**
2ff32 20 54 68 69 73 20 63 6f 6c 6c 61 74 69 6e 67 20   This collating 
2ff33 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e 74 65  sequence is inte
2ff34 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 20  nded to be used 
2ff35 66 6f 72 20 22 63 61 73 65 20 69 6e 64 65 70 65  for "case indepe
2ff36 6e 64 61 6e 74 0a 2a 2a 20 63 6f 6d 70 61 72 69  ndant.** compari
2ff37 73 6f 6e 22 2e 20 53 51 4c 69 74 65 27 73 20 6b  son". SQLite's k
2ff38 6e 6f 77 6c 65 64 67 65 20 6f 66 20 75 70 70 65  nowledge of uppe
2ff39 72 20 61 6e 64 20 6c 6f 77 65 72 20 63 61 73 65  r and lower case
2ff3a 20 65 71 75 69 76 61 6c 65 6e 74 73 0a 2a 2a 20   equivalents.** 
2ff3b 65 78 74 65 6e 64 73 20 6f 6e 6c 79 20 74 6f 20  extends only to 
2ff3c 74 68 65 20 32 36 20 63 68 61 72 61 63 74 65 72  the 26 character
2ff3d 73 20 75 73 65 64 20 69 6e 20 74 68 65 20 45 6e  s used in the En
2ff3e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 2e 0a  glish language..
2ff3f 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 6d 6f 6d  **.** At the mom
2ff40 65 6e 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c  ent there is onl
2ff41 79 20 61 20 55 54 46 2d 38 20 69 6d 70 6c 65 6d  y a UTF-8 implem
2ff42 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  entation..*/.sta
2ff43 74 69 63 20 69 6e 74 20 6e 6f 63 61 73 65 43 6f  tic int nocaseCo
2ff44 6c 6c 61 74 69 6e 67 46 75 6e 63 28 0a 20 20 76  llatingFunc(.  v
2ff45 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 0a 20 20  oid *NotUsed,.  
2ff46 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74  int nKey1, const
2ff47 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20   void *pKey1,.  
2ff48 69 6e 74 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74  int nKey2, const
2ff49 20 76 6f 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a   void *pKey2.){.
2ff4a 20 20 69 6e 74 20 72 20 3d 20 73 71 6c 69 74 65    int r = sqlite
2ff4b 33 53 74 72 4e 49 43 6d 70 28 0a 20 20 20 20 20  3StrNICmp(.     
2ff4c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70   (const char *)p
2ff4d 4b 65 79 31 2c 20 28 63 6f 6e 73 74 20 63 68 61  Key1, (const cha
2ff4e 72 20 2a 29 70 4b 65 79 32 2c 20 28 6e 4b 65 79  r *)pKey2, (nKey
2ff4f 31 3c 6e 4b 65 79 32 29 3f 6e 4b 65 79 31 3a 6e  1<nKey2)?nKey1:n
2ff50 4b 65 79 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f  Key2);.  UNUSED_
2ff51 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
2ff52 64 29 3b 0a 20 20 69 66 28 20 30 3d 3d 72 20 29  d);.  if( 0==r )
2ff53 7b 0a 20 20 20 20 72 20 3d 20 6e 4b 65 79 31 2d  {.    r = nKey1-
2ff54 6e 4b 65 79 32 3b 0a 20 20 7d 0a 20 20 72 65 74  nKey2;.  }.  ret
2ff55 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn r;.}../*.** 
2ff56 52 65 74 75 72 6e 20 74 68 65 20 52 4f 57 49 44  Return the ROWID
2ff57 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
2ff58 65 6e 74 20 69 6e 73 65 72 74 0a 2a 2f 0a 53 51  ent insert.*/.SQ
2ff59 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f  LITE_API sqlite_
2ff5a 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6c 61  int64 sqlite3_la
2ff5b 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
2ff5c 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2ff5d 72 65 74 75 72 6e 20 64 62 2d 3e 6c 61 73 74 52  return db->lastR
2ff5e 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  owid;.}../*.** R
2ff5f 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
2ff60 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74   of changes in t
2ff61 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63  he most recent c
2ff62 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 65  all to sqlite3_e
2ff63 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  xec()..*/.SQLITE
2ff64 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2ff65 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33  _changes(sqlite3
2ff66 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20   *db){.  return 
2ff67 64 62 2d 3e 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a  db->nChange;.}..
2ff68 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
2ff69 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67   number of chang
2ff6a 65 73 20 73 69 6e 63 65 20 74 68 65 20 64 61 74  es since the dat
2ff6b 61 62 61 73 65 20 68 61 6e 64 6c 65 20 77 61 73  abase handle was
2ff6c 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49   opened..*/.SQLI
2ff6d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2ff6e 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
2ff6f 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2ff70 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e 54 6f 74   return db->nTot
2ff71 61 6c 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a  alChange;.}../*.
2ff72 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 6f 70 65  ** Close all ope
2ff73 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 54 68  n savepoints. Th
2ff74 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79  is function only
2ff75 20 6d 61 6e 69 70 75 6c 61 74 65 73 20 66 69 65   manipulates fie
2ff76 6c 64 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  lds of the.** da
2ff77 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6f 62  tabase handle ob
2ff78 6a 65 63 74 2c 20 69 74 20 64 6f 65 73 20 6e 6f  ject, it does no
2ff79 74 20 63 6c 6f 73 65 20 61 6e 79 20 73 61 76 65  t close any save
2ff7a 70 6f 69 6e 74 73 20 74 68 61 74 20 6d 61 79 20  points that may 
2ff7b 62 65 20 6f 70 65 6e 0a 2a 2a 20 61 74 20 74 68  be open.** at th
2ff7c 65 20 62 2d 74 72 65 65 2f 70 61 67 65 72 20 6c  e b-tree/pager l
2ff7d 65 76 65 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  evel..*/.SQLITE_
2ff7e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2ff7f 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69  ite3CloseSavepoi
2ff80 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  nts(sqlite3 *db)
2ff81 7b 0a 20 20 77 68 69 6c 65 28 20 64 62 2d 3e 70  {.  while( db->p
2ff82 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20  Savepoint ){.   
2ff83 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70   Savepoint *pTmp
2ff84 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e   = db->pSavepoin
2ff85 74 3b 0a 20 20 20 20 64 62 2d 3e 70 53 61 76 65  t;.    db->pSave
2ff86 70 6f 69 6e 74 20 3d 20 70 54 6d 70 2d 3e 70 4e  point = pTmp->pN
2ff87 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ext;.    sqlite3
2ff88 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70 29  DbFree(db, pTmp)
2ff89 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 6e 53 61 76  ;.  }.  db->nSav
2ff8a 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 64 62  epoint = 0;.  db
2ff8b 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20 30  ->nStatement = 0
2ff8c 3b 0a 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61  ;.  db->isTransa
2ff8d 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d  ctionSavepoint =
2ff8e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f   0;.}../*.** Clo
2ff8f 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 53  se an existing S
2ff90 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 0a 2a  QLite database.*
2ff91 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2ff92 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73   sqlite3_close(s
2ff93 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 48  qlite3 *db){.  H
2ff94 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 6e  ashElem *i;.  in
2ff95 74 20 6a 3b 0a 0a 20 20 69 66 28 20 21 64 62 20  t j;..  if( !db 
2ff96 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2ff97 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
2ff98 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
2ff99 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
2ff9a 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
2ff9b 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
2ff9c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
2ff9d 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2ff9e 74 65 78 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  tex);..#ifdef SQ
2ff9f 4c 49 54 45 5f 53 53 45 0a 20 20 7b 0a 20 20 20  LITE_SSE.  {.   
2ffa0 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c   extern void sql
2ffa1 69 74 65 33 53 73 65 43 6c 65 61 6e 75 70 28 73  ite3SseCleanup(s
2ffa2 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 73 71  qlite3*);.    sq
2ffa3 6c 69 74 65 33 53 73 65 43 6c 65 61 6e 75 70 28  lite3SseCleanup(
2ffa4 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  db);.  }.#endif 
2ffa5 0a 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74  ..  sqlite3Reset
2ffa6 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2ffa7 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  b, 0);..  /* If 
2ffa8 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
2ffa9 20 6f 70 65 6e 2c 20 74 68 65 20 52 65 73 65 74   open, the Reset
2ffaa 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 29  InternalSchema()
2ffab 20 63 61 6c 6c 20 61 62 6f 76 65 0a 20 20 2a 2a   call above.  **
2ffac 20 77 69 6c 6c 20 6e 6f 74 20 68 61 76 65 20 63   will not have c
2ffad 61 6c 6c 65 64 20 74 68 65 20 78 44 69 73 63 6f  alled the xDisco
2ffae 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20 6f  nnect() method o
2ffaf 6e 20 61 6e 79 20 76 69 72 74 75 61 6c 0a 20 20  n any virtual.  
2ffb0 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  ** tables in the
2ffb1 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20 61   db->aVTrans[] a
2ffb2 72 72 61 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  rray. The follow
2ffb3 69 6e 67 20 73 71 6c 69 74 65 33 56 74 61 62 52  ing sqlite3VtabR
2ffb4 6f 6c 6c 62 61 63 6b 28 29 0a 20 20 2a 2a 20 63  ollback().  ** c
2ffb5 61 6c 6c 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 20  all will do so. 
2ffb6 57 65 20 6e 65 65 64 20 74 6f 20 64 6f 20 74 68  We need to do th
2ffb7 69 73 20 62 65 66 6f 72 65 20 74 68 65 20 63 68  is before the ch
2ffb8 65 63 6b 20 66 6f 72 20 61 63 74 69 76 65 0a 20  eck for active. 
2ffb9 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   ** SQL statemen
2ffba 74 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65  ts below, as the
2ffbb 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65   v-table impleme
2ffbc 6e 74 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 73  ntation may be s
2ffbd 74 6f 72 69 6e 67 0a 20 20 2a 2a 20 73 6f 6d 65  toring.  ** some
2ffbe 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2ffbf 65 6e 74 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e  ents internally.
2ffc0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
2ffc1 74 61 62 52 6f 6c 6c 62 61 63 6b 28 64 62 29 3b  tabRollback(db);
2ffc2 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
2ffc3 61 72 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64  are any outstand
2ffc4 69 6e 67 20 56 4d 73 2c 20 72 65 74 75 72 6e 20  ing VMs, return 
2ffc5 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a  SQLITE_BUSY. */.
2ffc6 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20    if( db->pVdbe 
2ffc7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2ffc8 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42  ror(db, SQLITE_B
2ffc9 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22 75  USY, .        "u
2ffca 6e 61 62 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64  nable to close d
2ffcb 75 65 20 74 6f 20 75 6e 66 69 6e 61 6c 69 73 65  ue to unfinalise
2ffcc 64 20 73 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a  d statements");.
2ffcd 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
2ffce 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2ffcf 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  x);.    return S
2ffd0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
2ffd1 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2ffd2 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
2ffd3 4f 72 4f 6b 28 64 62 29 20 29 3b 0a 0a 20 20 66  OrOk(db) );..  f
2ffd4 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44  or(j=0; j<db->nD
2ffd5 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 42 74 72  b; j++){.    Btr
2ffd6 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
2ffd7 62 5b 6a 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[j].pBt;.    if
2ffd8 28 20 70 42 74 20 26 26 20 73 71 6c 69 74 65 33  ( pBt && sqlite3
2ffd9 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28  BtreeIsInBackup(
2ffda 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71  pBt) ){.      sq
2ffdb 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
2ffdc 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20  QLITE_BUSY, .   
2ffdd 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74         "unable t
2ffde 6f 20 63 6c 6f 73 65 20 64 75 65 20 74 6f 20 75  o close due to u
2ffdf 6e 66 69 6e 69 73 68 65 64 20 62 61 63 6b 75 70  nfinished backup
2ffe0 20 6f 70 65 72 61 74 69 6f 6e 22 29 3b 0a 20 20   operation");.  
2ffe1 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
2ffe2 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2ffe3 78 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  x);.      return
2ffe4 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
2ffe5 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72    }.  }..  /* Fr
2ffe6 65 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69  ee any outstandi
2ffe7 6e 67 20 53 61 76 65 70 6f 69 6e 74 20 73 74 72  ng Savepoint str
2ffe8 75 63 74 75 72 65 73 2e 20 2a 2f 0a 20 20 73 71  uctures. */.  sq
2ffe9 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f  lite3CloseSavepo
2ffea 69 6e 74 73 28 64 62 29 3b 0a 0a 20 20 66 6f 72  ints(db);..  for
2ffeb 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b  (j=0; j<db->nDb;
2ffec 20 6a 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63   j++){.    struc
2ffed 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d  t Db *pDb = &db-
2ffee 3e 61 44 62 5b 6a 5d 3b 0a 20 20 20 20 69 66 28  >aDb[j];.    if(
2ffef 20 70 44 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20   pDb->pBt ){.   
2fff0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
2fff1 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a  lose(pDb->pBt);.
2fff2 20 20 20 20 20 20 70 44 62 2d 3e 70 42 74 20 3d        pDb->pBt =
2fff3 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6a 21   0;.      if( j!
2fff4 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  =1 ){.        pD
2fff5 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a  b->pSchema = 0;.
2fff6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2fff7 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74  }.  sqlite3Reset
2fff8 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2fff9 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 65 6c  b, 0);..  /* Tel
2fffa 6c 20 74 68 65 20 63 6f 64 65 20 69 6e 20 6e 6f  l the code in no
2fffb 74 69 66 79 2e 63 20 74 68 61 74 20 74 68 65 20  tify.c that the 
2fffc 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e 6f 20 6c 6f  connection no lo
2fffd 6e 67 65 72 20 68 6f 6c 64 73 20 61 6e 79 0a 20  nger holds any. 
2fffe 20 2a 2a 20 6c 6f 63 6b 73 20 61 6e 64 20 64 6f   ** locks and do
2ffff 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61  es not require a
30000 6e 79 20 66 75 72 74 68 65 72 20 75 6e 6c 6f 63  ny further unloc
30001 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
30002 6b 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ks..  */.  sqlit
30003 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73  e3ConnectionClos
30004 65 64 28 64 62 29 3b 0a 0a 20 20 61 73 73 65 72  ed(db);..  asser
30005 74 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 29 3b  t( db->nDb<=2 );
30006 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61  .  assert( db->a
30007 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db==db->aDbStati
30008 63 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20  c );.  for(j=0; 
30009 6a 3c 41 72 72 61 79 53 69 7a 65 28 64 62 2d 3e  j<ArraySize(db->
3000a 61 46 75 6e 63 2e 61 29 3b 20 6a 2b 2b 29 7b 0a  aFunc.a); j++){.
3000b 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65      FuncDef *pNe
3000c 78 74 2c 20 2a 70 48 61 73 68 2c 20 2a 70 3b 0a  xt, *pHash, *p;.
3000d 20 20 20 20 66 6f 72 28 70 3d 64 62 2d 3e 61 46      for(p=db->aF
3000e 75 6e 63 2e 61 5b 6a 5d 3b 20 70 3b 20 70 3d 70  unc.a[j]; p; p=p
3000f 48 61 73 68 29 7b 0a 20 20 20 20 20 20 70 48 61  Hash){.      pHa
30010 73 68 20 3d 20 70 2d 3e 70 48 61 73 68 3b 0a 20  sh = p->pHash;. 
30011 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b       while( p ){
30012 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20 3d  .        pNext =
30013 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20   p->pNext;.     
30014 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
30015 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20  (db, p);.       
30016 20 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20 20 20   p = pNext;.    
30017 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
30018 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
30019 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c  First(&db->aColl
3001a 53 65 71 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74  Seq); i; i=sqlit
3001b 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
3001c 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
3001d 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73  l = (CollSeq *)s
3001e 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
3001f 3b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ;.    /* Invoke 
30020 61 6e 79 20 64 65 73 74 72 75 63 74 6f 72 73 20  any destructors 
30021 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 63  registered for c
30022 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
30023 65 20 75 73 65 72 20 64 61 74 61 2e 20 2a 2f 0a  e user data. */.
30024 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33      for(j=0; j<3
30025 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
30026 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 20  ( pColl[j].xDel 
30027 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  ){.        pColl
30028 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c 6c 5b 6a  [j].xDel(pColl[j
30029 5d 2e 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20  ].pUser);.      
3002a 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
3002b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
3002c 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  oll);.  }.  sqli
3002d 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 64 62  te3HashClear(&db
3002e 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66  ->aCollSeq);.#if
3002f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
30030 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
30031 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
30032 46 69 72 73 74 28 26 64 62 2d 3e 61 4d 6f 64 75  First(&db->aModu
30033 6c 65 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65  le); i; i=sqlite
30034 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20  HashNext(i)){.  
30035 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20 3d    Module *pMod =
30036 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74   (Module *)sqlit
30037 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20  eHashData(i);.  
30038 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 44 65 73    if( pMod->xDes
30039 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 4d  troy ){.      pM
3003a 6f 64 2d 3e 78 44 65 73 74 72 6f 79 28 70 4d 6f  od->xDestroy(pMo
3003b 64 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a  d->pAux);.    }.
3003c 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
3003d 65 28 64 62 2c 20 70 4d 6f 64 29 3b 0a 20 20 7d  e(db, pMod);.  }
3003e 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  .  sqlite3HashCl
3003f 65 61 72 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  ear(&db->aModule
30040 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c  );.#endif..  sql
30041 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
30042 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 20 2f 2a 20  LITE_OK, 0); /* 
30043 44 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20  Deallocates any 
30044 63 61 63 68 65 64 20 65 72 72 6f 72 20 73 74 72  cached error str
30045 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 66 28 20 64  ings. */.  if( d
30046 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20 73  b->pErr ){.    s
30047 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
30048 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 7d 0a 20  db->pErr);.  }. 
30049 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74   sqlite3CloseExt
3004a 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20  ensions(db);..  
3004b 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
3004c 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a  TE_MAGIC_ERROR;.
3004d 0a 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 2d 64  .  /* The temp-d
3004e 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69  atabase schema i
3004f 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 69 66 66  s allocated diff
30050 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65  erently from the
30051 20 6f 74 68 65 72 20 73 63 68 65 6d 61 0a 20 20   other schema.  
30052 2a 2a 20 6f 62 6a 65 63 74 73 20 28 75 73 69 6e  ** objects (usin
30053 67 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  g sqliteMalloc()
30054 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74 65   directly, inste
30055 61 64 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72  ad of sqlite3Btr
30056 65 65 53 63 68 65 6d 61 28 29 29 2e 0a 20 20 2a  eeSchema())..  *
30057 2a 20 53 6f 20 69 74 20 6e 65 65 64 73 20 74 6f  * So it needs to
30058 20 62 65 20 66 72 65 65 64 20 68 65 72 65 2e 20   be freed here. 
30059 54 6f 64 6f 3a 20 57 68 79 20 6e 6f 74 20 72 6f  Todo: Why not ro
3005a 6c 6c 20 74 68 65 20 74 65 6d 70 20 73 63 68 65  ll the temp sche
3005b 6d 61 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  ma into.  ** the
3005c 20 73 61 6d 65 20 73 71 6c 69 74 65 4d 61 6c 6c   same sqliteMall
3005d 6f 63 28 29 20 61 73 20 74 68 65 20 6f 6e 65 20  oc() as the one 
3005e 74 68 61 74 20 61 6c 6c 6f 63 61 74 65 73 20 74  that allocates t
3005f 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 2a  he database .  *
30060 2a 20 73 74 72 75 63 74 75 72 65 3f 0a 20 20 2a  * structure?.  *
30061 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  /.  sqlite3DbFre
30062 65 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 31 5d  e(db, db->aDb[1]
30063 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c  .pSchema);.  sql
30064 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
30065 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
30066 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
30067 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b 0a  E_MAGIC_CLOSED;.
30068 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
30069 66 72 65 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  free(db->mutex);
3006a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6c  .  assert( db->l
3006b 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3d 3d 30  ookaside.nOut==0
3006c 20 29 3b 20 20 2f 2a 20 46 61 69 6c 73 20 6f 6e   );  /* Fails on
3006d 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d   a lookaside mem
3006e 6f 72 79 20 6c 65 61 6b 20 2a 2f 0a 20 20 69 66  ory leak */.  if
3006f 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
30070 62 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a 20 20 20  bMalloced ){.   
30071 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62   sqlite3_free(db
30072 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61  ->lookaside.pSta
30073 72 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  rt);.  }.  sqlit
30074 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20 20 72  e3_free(db);.  r
30075 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
30076 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61  .}../*.** Rollba
30077 63 6b 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20  ck all database 
30078 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  files..*/.SQLITE
30079 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
3007a 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c  lite3RollbackAll
3007b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
3007c 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 6e   int i;.  int in
3007d 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 61 73 73  Trans = 0;.  ass
3007e 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
3007f 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
30080 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  x) );.  sqlite3B
30081 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
30082 28 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ();.  for(i=0; i
30083 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
30084 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
30085 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20 20  i].pBt ){.      
30086 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  if( sqlite3Btree
30087 49 73 49 6e 54 72 61 6e 73 28 64 62 2d 3e 61 44  IsInTrans(db->aD
30088 62 5b 69 5d 2e 70 42 74 29 20 29 7b 0a 20 20 20  b[i].pBt) ){.   
30089 20 20 20 20 20 69 6e 54 72 61 6e 73 20 3d 20 31       inTrans = 1
3008a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3008b 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
3008c 62 61 63 6b 28 64 62 2d 3e 61 44 62 5b 69 5d 2e  back(db->aDb[i].
3008d 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  pBt);.      db->
3008e 61 44 62 5b 69 5d 2e 69 6e 54 72 61 6e 73 20 3d  aDb[i].inTrans =
3008f 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
30090 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62  sqlite3VtabRollb
30091 61 63 6b 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  ack(db);.  sqlit
30092 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
30093 63 28 29 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e  c();..  if( db->
30094 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
30095 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20  ernChanges ){.  
30096 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50    sqlite3ExpireP
30097 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
30098 73 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74  s(db);.    sqlit
30099 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
3009a 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
3009b 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 68  }..  /* If one h
3009c 61 73 20 62 65 65 6e 20 63 6f 6e 66 69 67 75 72  as been configur
3009d 65 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 72  ed, invoke the r
3009e 6f 6c 6c 62 61 63 6b 2d 68 6f 6f 6b 20 63 61 6c  ollback-hook cal
3009f 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 64  lback */.  if( d
300a0 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c  b->xRollbackCall
300a1 62 61 63 6b 20 26 26 20 28 69 6e 54 72 61 6e 73  back && (inTrans
300a2 20 7c 7c 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d   || !db->autoCom
300a3 6d 69 74 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e  mit) ){.    db->
300a4 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
300a5 6b 28 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41  k(db->pRollbackA
300a6 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  rg);.  }.}../*.*
300a7 2a 20 52 65 74 75 72 6e 20 61 20 73 74 61 74 69  * Return a stati
300a8 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65  c string that de
300a9 73 63 72 69 62 65 73 20 74 68 65 20 6b 69 6e 64  scribes the kind
300aa 20 6f 66 20 65 72 72 6f 72 20 73 70 65 63 69 66   of error specif
300ab 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72  ied in the.** ar
300ac 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
300ad 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
300ae 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72  char *sqlite3Err
300af 53 74 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 73  Str(int rc){.  s
300b0 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
300b1 2a 20 63 6f 6e 73 74 20 61 4d 73 67 5b 5d 20 3d  * const aMsg[] =
300b2 20 7b 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45   {.    /* SQLITE
300b3 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 2a 2f 20  _OK          */ 
300b4 22 6e 6f 74 20 61 6e 20 65 72 72 6f 72 22 2c 0a  "not an error",.
300b5 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 45 52      /* SQLITE_ER
300b6 52 4f 52 20 20 20 20 20 20 20 2a 2f 20 22 53 51  ROR       */ "SQ
300b7 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 6f 72  L logic error or
300b8 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 61 73   missing databas
300b9 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  e",.    /* SQLIT
300ba 45 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20 2a 2f  E_INTERNAL    */
300bb 20 30 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54   0,.    /* SQLIT
300bc 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20 2a 2f  E_PERM        */
300bd 20 22 61 63 63 65 73 73 20 70 65 72 6d 69 73 73   "access permiss
300be 69 6f 6e 20 64 65 6e 69 65 64 22 2c 0a 20 20 20  ion denied",.   
300bf 20 2f 2a 20 53 51 4c 49 54 45 5f 41 42 4f 52 54   /* SQLITE_ABORT
300c0 20 20 20 20 20 20 20 2a 2f 20 22 63 61 6c 6c 62         */ "callb
300c1 61 63 6b 20 72 65 71 75 65 73 74 65 64 20 71 75  ack requested qu
300c2 65 72 79 20 61 62 6f 72 74 22 2c 0a 20 20 20 20  ery abort",.    
300c3 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20  /* SQLITE_BUSY  
300c4 20 20 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61        */ "databa
300c5 73 65 20 69 73 20 6c 6f 63 6b 65 64 22 2c 0a 20  se is locked",. 
300c6 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43     /* SQLITE_LOC
300c7 4b 45 44 20 20 20 20 20 20 2a 2f 20 22 64 61 74  KED      */ "dat
300c8 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c  abase table is l
300c9 6f 63 6b 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  ocked",.    /* S
300ca 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20  QLITE_NOMEM     
300cb 20 20 2a 2f 20 22 6f 75 74 20 6f 66 20 6d 65 6d    */ "out of mem
300cc 6f 72 79 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ory",.    /* SQL
300cd 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20  ITE_READONLY    
300ce 2a 2f 20 22 61 74 74 65 6d 70 74 20 74 6f 20 77  */ "attempt to w
300cf 72 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20  rite a readonly 
300d0 64 61 74 61 62 61 73 65 22 2c 0a 20 20 20 20 2f  database",.    /
300d1 2a 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  * SQLITE_INTERRU
300d2 50 54 20 20 20 2a 2f 20 22 69 6e 74 65 72 72 75  PT   */ "interru
300d3 70 74 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51  pted",.    /* SQ
300d4 4c 49 54 45 5f 49 4f 45 52 52 20 20 20 20 20 20  LITE_IOERR      
300d5 20 2a 2f 20 22 64 69 73 6b 20 49 2f 4f 20 65 72   */ "disk I/O er
300d6 72 6f 72 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ror",.    /* SQL
300d7 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20  ITE_CORRUPT     
300d8 2a 2f 20 22 64 61 74 61 62 61 73 65 20 64 69 73  */ "database dis
300d9 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f  k image is malfo
300da 72 6d 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51  rmed",.    /* SQ
300db 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20  LITE_NOTFOUND   
300dc 20 2a 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53 51   */ 0,.    /* SQ
300dd 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20 20 20 20  LITE_FULL       
300de 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20 6f 72   */ "database or
300df 20 64 69 73 6b 20 69 73 20 66 75 6c 6c 22 2c 0a   disk is full",.
300e0 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 41      /* SQLITE_CA
300e1 4e 54 4f 50 45 4e 20 20 20 20 2a 2f 20 22 75 6e  NTOPEN    */ "un
300e2 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74  able to open dat
300e3 61 62 61 73 65 20 66 69 6c 65 22 2c 0a 20 20 20  abase file",.   
300e4 20 2f 2a 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f   /* SQLITE_PROTO
300e5 43 4f 4c 20 20 20 20 2a 2f 20 30 2c 0a 20 20 20  COL    */ 0,.   
300e6 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4d 50 54 59   /* SQLITE_EMPTY
300e7 20 20 20 20 20 20 20 2a 2f 20 22 74 61 62 6c 65         */ "table
300e8 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 61 74   contains no dat
300e9 61 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  a",.    /* SQLIT
300ea 45 5f 53 43 48 45 4d 41 20 20 20 20 20 20 2a 2f  E_SCHEMA      */
300eb 20 22 64 61 74 61 62 61 73 65 20 73 63 68 65 6d   "database schem
300ec 61 20 68 61 73 20 63 68 61 6e 67 65 64 22 2c 0a  a has changed",.
300ed 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 54 4f      /* SQLITE_TO
300ee 4f 42 49 47 20 20 20 20 20 20 2a 2f 20 22 53 74  OBIG      */ "St
300ef 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63  ring or BLOB exc
300f0 65 65 64 65 64 20 73 69 7a 65 20 6c 69 6d 69 74  eeded size limit
300f1 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
300f2 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 2a 2f 20  _CONSTRAINT  */ 
300f3 22 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c  "constraint fail
300f4 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  ed",.    /* SQLI
300f5 54 45 5f 4d 49 53 4d 41 54 43 48 20 20 20 20 2a  TE_MISMATCH    *
300f6 2f 20 22 64 61 74 61 74 79 70 65 20 6d 69 73 6d  / "datatype mism
300f7 61 74 63 68 22 2c 0a 20 20 20 20 2f 2a 20 53 51  atch",.    /* SQ
300f8 4c 49 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20  LITE_MISUSE     
300f9 20 2a 2f 20 22 6c 69 62 72 61 72 79 20 72 6f 75   */ "library rou
300fa 74 69 6e 65 20 63 61 6c 6c 65 64 20 6f 75 74 20  tine called out 
300fb 6f 66 20 73 65 71 75 65 6e 63 65 22 2c 0a 20 20  of sequence",.  
300fc 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 4c 46    /* SQLITE_NOLF
300fd 53 20 20 20 20 20 20 20 2a 2f 20 22 6c 61 72 67  S       */ "larg
300fe 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 69  e file support i
300ff 73 20 64 69 73 61 62 6c 65 64 22 2c 0a 20 20 20  s disabled",.   
30100 20 2f 2a 20 53 51 4c 49 54 45 5f 41 55 54 48 20   /* SQLITE_AUTH 
30101 20 20 20 20 20 20 20 2a 2f 20 22 61 75 74 68 6f         */ "autho
30102 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 22  rization denied"
30103 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
30104 46 4f 52 4d 41 54 20 20 20 20 20 20 2a 2f 20 22  FORMAT      */ "
30105 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
30106 73 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 22  se format error"
30107 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
30108 52 41 4e 47 45 20 20 20 20 20 20 20 2a 2f 20 22  RANGE       */ "
30109 62 69 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e 20 69  bind or column i
3010a 6e 64 65 78 20 6f 75 74 20 6f 66 20 72 61 6e 67  ndex out of rang
3010b 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  e",.    /* SQLIT
3010c 45 5f 4e 4f 54 41 44 42 20 20 20 20 20 20 2a 2f  E_NOTADB      */
3010d 20 22 66 69 6c 65 20 69 73 20 65 6e 63 72 79 70   "file is encryp
3010e 74 65 64 20 6f 72 20 69 73 20 6e 6f 74 20 61 20  ted or is not a 
3010f 64 61 74 61 62 61 73 65 22 2c 0a 20 20 7d 3b 0a  database",.  };.
30110 20 20 72 63 20 26 3d 20 30 78 66 66 3b 0a 20 20    rc &= 0xff;.  
30111 69 66 28 20 41 4c 57 41 59 53 28 72 63 3e 3d 30  if( ALWAYS(rc>=0
30112 29 20 26 26 20 72 63 3c 28 69 6e 74 29 28 73 69  ) && rc<(int)(si
30113 7a 65 6f 66 28 61 4d 73 67 29 2f 73 69 7a 65 6f  zeof(aMsg)/sizeo
30114 66 28 61 4d 73 67 5b 30 5d 29 29 20 26 26 20 61  f(aMsg[0])) && a
30115 4d 73 67 5b 72 63 5d 21 3d 30 20 29 7b 0a 20 20  Msg[rc]!=0 ){.  
30116 20 20 72 65 74 75 72 6e 20 61 4d 73 67 5b 72 63    return aMsg[rc
30117 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
30118 72 65 74 75 72 6e 20 22 75 6e 6b 6e 6f 77 6e 20  return "unknown 
30119 65 72 72 6f 72 22 3b 0a 20 20 7d 0a 7d 0a 0a 2f  error";.  }.}../
3011a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
3011b 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 62  e implements a b
3011c 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 74 68 61  usy callback tha
3011d 74 20 73 6c 65 65 70 73 20 61 6e 64 20 74 72 69  t sleeps and tri
3011e 65 73 0a 2a 2a 20 61 67 61 69 6e 20 75 6e 74 69  es.** again unti
3011f 6c 20 61 20 74 69 6d 65 6f 75 74 20 76 61 6c 75  l a timeout valu
30120 65 20 69 73 20 72 65 61 63 68 65 64 2e 20 20 54  e is reached.  T
30121 68 65 20 74 69 6d 65 6f 75 74 20 76 61 6c 75 65  he timeout value
30122 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65   is.** an intege
30123 72 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c  r number of mill
30124 69 73 65 63 6f 6e 64 73 20 70 61 73 73 65 64 20  iseconds passed 
30125 69 6e 20 61 73 20 74 68 65 20 66 69 72 73 74 0a  in as the first.
30126 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  ** argument..*/.
30127 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
30128 65 44 65 66 61 75 6c 74 42 75 73 79 43 61 6c 6c  eDefaultBusyCall
30129 62 61 63 6b 28 0a 20 76 6f 69 64 20 2a 70 74 72  back(. void *ptr
3012a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3012b 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
3012c 65 63 74 69 6f 6e 20 2a 2f 0a 20 69 6e 74 20 63  ection */. int c
3012d 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  ount            
3012e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3012f 20 74 69 6d 65 73 20 74 61 62 6c 65 20 68 61 73   times table has
30130 20 62 65 65 6e 20 62 75 73 79 20 2a 2f 0a 29 7b   been busy */.){
30131 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
30132 49 4e 20 7c 7c 20 28 64 65 66 69 6e 65 64 28 48  IN || (defined(H
30133 41 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48  AVE_USLEEP) && H
30134 41 56 45 5f 55 53 4c 45 45 50 29 0a 20 20 73 74  AVE_USLEEP).  st
30135 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 64 65  atic const u8 de
30136 6c 61 79 73 5b 5d 20 3d 0a 20 20 20 20 20 7b 20  lays[] =.     { 
30137 31 2c 20 32 2c 20 35 2c 20 31 30 2c 20 31 35 2c  1, 2, 5, 10, 15,
30138 20 32 30 2c 20 32 35 2c 20 32 35 2c 20 20 32 35   20, 25, 25,  25
30139 2c 20 20 35 30 2c 20 20 35 30 2c 20 31 30 30 20  ,  50,  50, 100 
3013a 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
3013b 74 20 75 38 20 74 6f 74 61 6c 73 5b 5d 20 3d 0a  t u8 totals[] =.
3013c 20 20 20 20 20 7b 20 30 2c 20 31 2c 20 33 2c 20       { 0, 1, 3, 
3013d 20 38 2c 20 31 38 2c 20 33 33 2c 20 35 33 2c 20   8, 18, 33, 53, 
3013e 37 38 2c 20 31 30 33 2c 20 31 32 38 2c 20 31 37  78, 103, 128, 17
3013f 38 2c 20 32 32 38 20 7d 3b 0a 23 20 64 65 66 69  8, 228 };.# defi
30140 6e 65 20 4e 44 45 4c 41 59 20 28 73 69 7a 65 6f  ne NDELAY (sizeo
30141 66 28 64 65 6c 61 79 73 29 2f 73 69 7a 65 6f 66  f(delays)/sizeof
30142 28 64 65 6c 61 79 73 5b 30 5d 29 29 0a 20 20 73  (delays[0])).  s
30143 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71  qlite3 *db = (sq
30144 6c 69 74 65 33 20 2a 29 70 74 72 3b 0a 20 20 69  lite3 *)ptr;.  i
30145 6e 74 20 74 69 6d 65 6f 75 74 20 3d 20 64 62 2d  nt timeout = db-
30146 3e 62 75 73 79 54 69 6d 65 6f 75 74 3b 0a 20 20  >busyTimeout;.  
30147 69 6e 74 20 64 65 6c 61 79 2c 20 70 72 69 6f 72  int delay, prior
30148 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 6f 75  ;..  assert( cou
30149 6e 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 63  nt>=0 );.  if( c
3014a 6f 75 6e 74 20 3c 20 4e 44 45 4c 41 59 20 29 7b  ount < NDELAY ){
3014b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20 64 65 6c  .    delay = del
3014c 61 79 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 20 20  ays[count];.    
3014d 70 72 69 6f 72 20 3d 20 74 6f 74 61 6c 73 5b 63  prior = totals[c
3014e 6f 75 6e 74 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ount];.  }else{.
3014f 20 20 20 20 64 65 6c 61 79 20 3d 20 64 65 6c 61      delay = dela
30150 79 73 5b 4e 44 45 4c 41 59 2d 31 5d 3b 0a 20 20  ys[NDELAY-1];.  
30151 20 20 70 72 69 6f 72 20 3d 20 74 6f 74 61 6c 73    prior = totals
30152 5b 4e 44 45 4c 41 59 2d 31 5d 20 2b 20 64 65 6c  [NDELAY-1] + del
30153 61 79 2a 28 63 6f 75 6e 74 2d 28 4e 44 45 4c 41  ay*(count-(NDELA
30154 59 2d 31 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28  Y-1));.  }.  if(
30155 20 70 72 69 6f 72 20 2b 20 64 65 6c 61 79 20 3e   prior + delay >
30156 20 74 69 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20   timeout ){.    
30157 64 65 6c 61 79 20 3d 20 74 69 6d 65 6f 75 74 20  delay = timeout 
30158 2d 20 70 72 69 6f 72 3b 0a 20 20 20 20 69 66 28  - prior;.    if(
30159 20 64 65 6c 61 79 3c 3d 30 20 29 20 72 65 74 75   delay<=0 ) retu
3015a 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  rn 0;.  }.  sqli
3015b 74 65 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e 70  te3OsSleep(db->p
3015c 56 66 73 2c 20 64 65 6c 61 79 2a 31 30 30 30 29  Vfs, delay*1000)
3015d 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65  ;.  return 1;.#e
3015e 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  lse.  sqlite3 *d
3015f 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 70  b = (sqlite3 *)p
30160 74 72 3b 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75  tr;.  int timeou
30161 74 20 3d 20 28 28 73 71 6c 69 74 65 33 20 2a 29  t = ((sqlite3 *)
30162 70 74 72 29 2d 3e 62 75 73 79 54 69 6d 65 6f 75  ptr)->busyTimeou
30163 74 3b 0a 20 20 69 66 28 20 28 63 6f 75 6e 74 2b  t;.  if( (count+
30164 31 29 2a 31 30 30 30 20 3e 20 74 69 6d 65 6f 75  1)*1000 > timeou
30165 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
30166 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
30167 4f 73 53 6c 65 65 70 28 64 62 2d 3e 70 56 66 73  OsSleep(db->pVfs
30168 2c 20 31 30 30 30 30 30 30 29 3b 0a 20 20 72 65  , 1000000);.  re
30169 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d  turn 1;.#endif.}
3016a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74  ../*.** Invoke t
3016b 68 65 20 67 69 76 65 6e 20 62 75 73 79 20 68 61  he given busy ha
3016c 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ndler..**.** Thi
3016d 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
3016e 6c 65 64 20 77 68 65 6e 20 61 6e 20 6f 70 65 72  led when an oper
3016f 61 74 69 6f 6e 20 66 61 69 6c 65 64 20 77 69 74  ation failed wit
30170 68 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20  h a lock..** If 
30171 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
30172 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
30173 68 65 20 6c 6f 63 6b 20 69 73 20 72 65 74 72 69  he lock is retri
30174 65 64 2e 20 20 49 66 20 69 74 0a 2a 2a 20 72 65  ed.  If it.** re
30175 74 75 72 6e 73 20 30 2c 20 74 68 65 20 6f 70 65  turns 0, the ope
30176 72 61 74 69 6f 6e 20 61 62 6f 72 74 73 20 77 69  ration aborts wi
30177 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 42 55 53  th an SQLITE_BUS
30178 59 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  Y error..*/.SQLI
30179 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3017a 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79  qlite3InvokeBusy
3017b 48 61 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64  Handler(BusyHand
3017c 6c 65 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ler *p){.  int r
3017d 63 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  c;.  if( NEVER(p
3017e 3d 3d 30 29 20 7c 7c 20 70 2d 3e 78 46 75 6e 63  ==0) || p->xFunc
3017f 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 42 75 73 79 3c  ==0 || p->nBusy<
30180 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
30181 72 63 20 3d 20 70 2d 3e 78 46 75 6e 63 28 70 2d  rc = p->xFunc(p-
30182 3e 70 41 72 67 2c 20 70 2d 3e 6e 42 75 73 79 29  >pArg, p->nBusy)
30183 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b  ;.  if( rc==0 ){
30184 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79 20 3d 20  .    p->nBusy = 
30185 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  -1;.  }else{.   
30186 20 70 2d 3e 6e 42 75 73 79 2b 2b 3b 0a 20 20 7d   p->nBusy++;.  }
30187 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d  .  return rc; .}
30188 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
30189 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 62 75  tine sets the bu
3018a 73 79 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  sy callback for 
3018b 61 6e 20 53 71 6c 69 74 65 20 64 61 74 61 62 61  an Sqlite databa
3018c 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20 67 69 76  se to the.** giv
3018d 65 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  en callback func
3018e 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69  tion with the gi
3018f 76 65 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  ven argument..*/
30190 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
30191 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e  sqlite3_busy_han
30192 64 6c 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20  dler(.  sqlite3 
30193 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 42 75  *db,.  int (*xBu
30194 73 79 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 0a  sy)(void*,int),.
30195 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a    void *pArg.){.
30196 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
30197 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
30198 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64  ;.  db->busyHand
30199 6c 65 72 2e 78 46 75 6e 63 20 3d 20 78 42 75 73  ler.xFunc = xBus
3019a 79 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e  y;.  db->busyHan
3019b 64 6c 65 72 2e 70 41 72 67 20 3d 20 70 41 72 67  dler.pArg = pArg
3019c 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64  ;.  db->busyHand
3019d 6c 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20  ler.nBusy = 0;. 
3019e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
3019f 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
301a0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
301a1 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  _OK;.}..#ifndef 
301a2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47  SQLITE_OMIT_PROG
301a3 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 2f 2a  RESS_CALLBACK./*
301a4 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
301a5 20 73 65 74 73 20 74 68 65 20 70 72 6f 67 72 65   sets the progre
301a6 73 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  ss callback for 
301a7 61 6e 20 53 71 6c 69 74 65 20 64 61 74 61 62 61  an Sqlite databa
301a8 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20 67 69 76  se to the.** giv
301a9 65 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  en callback func
301aa 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69  tion with the gi
301ab 76 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  ven argument. Th
301ac 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
301ad 61 63 6b 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 69  ack will.** be i
301ae 6e 76 6f 6b 65 64 20 65 76 65 72 79 20 6e 4f 70  nvoked every nOp
301af 73 20 6f 70 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51  s opcodes..*/.SQ
301b0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
301b1 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
301b2 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69 74 65  andler(.  sqlite
301b3 33 20 2a 64 62 2c 20 0a 20 20 69 6e 74 20 6e 4f  3 *db, .  int nO
301b4 70 73 2c 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f  ps,.  int (*xPro
301b5 67 72 65 73 73 29 28 76 6f 69 64 2a 29 2c 20 0a  gress)(void*), .
301b6 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a    void *pArg.){.
301b7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
301b8 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
301b9 3b 0a 20 20 69 66 28 20 6e 4f 70 73 3e 30 20 29  ;.  if( nOps>0 )
301ba 7b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 67 72  {.    db->xProgr
301bb 65 73 73 20 3d 20 78 50 72 6f 67 72 65 73 73 3b  ess = xProgress;
301bc 0a 20 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65  .    db->nProgre
301bd 73 73 4f 70 73 20 3d 20 6e 4f 70 73 3b 0a 20 20  ssOps = nOps;.  
301be 20 20 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41    db->pProgressA
301bf 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 7d 65 6c  rg = pArg;.  }el
301c0 73 65 7b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f  se{.    db->xPro
301c1 67 72 65 73 73 20 3d 20 30 3b 0a 20 20 20 20 64  gress = 0;.    d
301c2 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 20  b->nProgressOps 
301c3 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72  = 0;.    db->pPr
301c4 6f 67 72 65 73 73 41 72 67 20 3d 20 30 3b 0a 20  ogressArg = 0;. 
301c5 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
301c6 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
301c7 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  ex);.}.#endif...
301c8 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
301c9 6e 65 20 69 6e 73 74 61 6c 6c 73 20 61 20 64 65  ne installs a de
301ca 66 61 75 6c 74 20 62 75 73 79 20 68 61 6e 64 6c  fault busy handl
301cb 65 72 20 74 68 61 74 20 77 61 69 74 73 20 66 6f  er that waits fo
301cc 72 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69  r the.** specifi
301cd 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c  ed number of mil
301ce 6c 69 73 65 63 6f 6e 64 73 20 62 65 66 6f 72 65  liseconds before
301cf 20 72 65 74 75 72 6e 69 6e 67 20 30 2e 0a 2a 2f   returning 0..*/
301d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
301d1 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d  sqlite3_busy_tim
301d2 65 6f 75 74 28 73 71 6c 69 74 65 33 20 2a 64 62  eout(sqlite3 *db
301d3 2c 20 69 6e 74 20 6d 73 29 7b 0a 20 20 69 66 28  , int ms){.  if(
301d4 20 6d 73 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d   ms>0 ){.    db-
301d5 3e 62 75 73 79 54 69 6d 65 6f 75 74 20 3d 20 6d  >busyTimeout = m
301d6 73 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  s;.    sqlite3_b
301d7 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20  usy_handler(db, 
301d8 73 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73  sqliteDefaultBus
301d9 79 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64  yCallback, (void
301da 2a 29 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  *)db);.  }else{.
301db 20 20 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79      sqlite3_busy
301dc 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20 30 2c 20  _handler(db, 0, 
301dd 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
301de 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
301df 2a 0a 2a 2a 20 43 61 75 73 65 20 61 6e 79 20 70  *.** Cause any p
301e0 65 6e 64 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ending operation
301e1 20 74 6f 20 73 74 6f 70 20 61 74 20 69 74 73 20   to stop at its 
301e2 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75  earliest opportu
301e3 6e 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nity..*/.SQLITE_
301e4 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
301e5 5f 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74  _interrupt(sqlit
301e6 65 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 75  e3 *db){.  db->u
301e7 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
301e8 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  = 1;.}.../*.** T
301e9 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
301ea 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65  exactly the same
301eb 20 61 73 20 73 71 6c 69 74 65 33 5f 63 72 65 61   as sqlite3_crea
301ec 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20 65  te_function(), e
301ed 78 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 74  xcept.** that it
301ee 20 69 73 20 64 65 73 69 67 6e 65 64 20 74 6f 20   is designed to 
301ef 62 65 20 63 61 6c 6c 65 64 20 62 79 20 69 6e 74  be called by int
301f0 65 72 6e 61 6c 20 63 6f 64 65 2e 20 54 68 65 20  ernal code. The 
301f1 64 69 66 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a  difference is.**
301f2 20 74 68 61 74 20 69 66 20 61 20 6d 61 6c 6c 6f   that if a mallo
301f3 63 28 29 20 66 61 69 6c 73 20 69 6e 20 73 71 6c  c() fails in sql
301f4 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
301f5 74 69 6f 6e 28 29 2c 20 61 6e 20 65 72 72 6f 72  tion(), an error
301f6 20 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75   code.** is retu
301f7 72 6e 65 64 20 61 6e 64 20 74 68 65 20 6d 61 6c  rned and the mal
301f8 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 63  locFailed flag c
301f9 6c 65 61 72 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49  leared. .*/.SQLI
301fa 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
301fb 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
301fc 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
301fd 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
301fe 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20  FunctionName,.  
301ff 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20  int nArg,.  int 
30200 65 6e 63 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73  enc,.  void *pUs
30201 65 72 44 61 74 61 2c 0a 20 20 76 6f 69 64 20 28  erData,.  void (
30202 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
30203 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
30204 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a  ite3_value **),.
30205 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28    void (*xStep)(
30206 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
30207 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
30208 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  ue **),.  void (
30209 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33  *xFinal)(sqlite3
3020a 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20  _context*).){.  
3020b 46 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20 69 6e  FuncDef *p;.  in
3020c 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73 65  t nName;..  asse
3020d 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
3020e 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
3020f 29 20 29 3b 0a 20 20 69 66 28 20 7a 46 75 6e 63  ) );.  if( zFunc
30210 74 69 6f 6e 4e 61 6d 65 3d 3d 30 20 7c 7c 0a 20  tionName==0 ||. 
30211 20 20 20 20 20 28 78 46 75 6e 63 20 26 26 20 28       (xFunc && (
30212 78 46 69 6e 61 6c 20 7c 7c 20 78 53 74 65 70 29  xFinal || xStep)
30213 29 20 7c 7c 20 0a 20 20 20 20 20 20 28 21 78 46  ) || .      (!xF
30214 75 6e 63 20 26 26 20 28 78 46 69 6e 61 6c 20 26  unc && (xFinal &
30215 26 20 21 78 53 74 65 70 29 29 20 7c 7c 0a 20 20  & !xStep)) ||.  
30216 20 20 20 20 28 21 78 46 75 6e 63 20 26 26 20 28      (!xFunc && (
30217 21 78 46 69 6e 61 6c 20 26 26 20 78 53 74 65 70  !xFinal && xStep
30218 29 29 20 7c 7c 0a 20 20 20 20 20 20 28 6e 41 72  )) ||.      (nAr
30219 67 3c 2d 31 20 7c 7c 20 6e 41 72 67 3e 53 51 4c  g<-1 || nArg>SQL
3021a 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
3021b 5f 41 52 47 29 20 7c 7c 0a 20 20 20 20 20 20 28  _ARG) ||.      (
3021c 32 35 35 3c 28 6e 4e 61 6d 65 20 3d 20 73 71 6c  255<(nName = sql
3021d 69 74 65 33 53 74 72 6c 65 6e 33 30 28 20 7a 46  ite3Strlen30( zF
3021e 75 6e 63 74 69 6f 6e 4e 61 6d 65 29 29 29 20 29  unctionName))) )
3021f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
30220 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 52  or(db, SQLITE_ER
30221 52 4f 52 2c 20 22 62 61 64 20 70 61 72 61 6d 65  ROR, "bad parame
30222 74 65 72 73 22 29 3b 0a 20 20 20 20 72 65 74 75  ters");.    retu
30223 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
30224 0a 20 20 7d 0a 20 20 0a 23 69 66 6e 64 65 66 20  .  }.  .#ifndef 
30225 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
30226 36 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45  6.  /* If SQLITE
30227 5f 55 54 46 31 36 20 69 73 20 73 70 65 63 69 66  _UTF16 is specif
30228 69 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f 64  ied as the encod
30229 69 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73 66  ing type, transf
3022a 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f  orm this.  ** to
3022b 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55   one of SQLITE_U
3022c 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45  TF16LE or SQLITE
3022d 5f 55 54 46 31 36 42 45 20 75 73 69 6e 67 20 74  _UTF16BE using t
3022e 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55  he.  ** SQLITE_U
3022f 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72 6f  TF16NATIVE macro
30230 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69  . SQLITE_UTF16 i
30231 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72  s not used inter
30232 6e 61 6c 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  nally..  **.  **
30233 20 49 66 20 53 51 4c 49 54 45 5f 41 4e 59 20 69   If SQLITE_ANY i
30234 73 20 73 70 65 63 69 66 69 65 64 2c 20 61 64 64  s specified, add
30235 20 74 68 72 65 65 20 76 65 72 73 69 6f 6e 73 20   three versions 
30236 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a  of the function.
30237 20 20 2a 2a 20 74 6f 20 74 68 65 20 68 61 73 68    ** to the hash
30238 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69   table..  */.  i
30239 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  f( enc==SQLITE_U
3023a 54 46 31 36 20 29 7b 0a 20 20 20 20 65 6e 63 20  TF16 ){.    enc 
3023b 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  = SQLITE_UTF16NA
3023c 54 49 56 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66  TIVE;.  }else if
3023d 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 41 4e  ( enc==SQLITE_AN
3023e 59 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b  Y ){.    int rc;
3023f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
30240 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
30241 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e  zFunctionName, n
30242 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Arg, SQLITE_UTF8
30243 2c 0a 20 20 20 20 20 20 20 20 20 70 55 73 65 72  ,.         pUser
30244 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78 53 74  Data, xFunc, xSt
30245 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20 20  ep, xFinal);.   
30246 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
30247 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
30248 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
30249 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  nc(db, zFunction
3024a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49  Name, nArg, SQLI
3024b 54 45 5f 55 54 46 31 36 4c 45 2c 0a 20 20 20 20  TE_UTF16LE,.    
3024c 20 20 20 20 20 20 70 55 73 65 72 44 61 74 61 2c        pUserData,
3024d 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78   xFunc, xStep, x
3024e 46 69 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  Final);.    }.  
3024f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
30250 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
30251 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
30252 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55    enc = SQLITE_U
30253 54 46 31 36 42 45 3b 0a 20 20 7d 0a 23 65 6c 73  TF16BE;.  }.#els
30254 65 0a 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45  e.  enc = SQLITE
30255 5f 55 54 46 38 3b 0a 23 65 6e 64 69 66 0a 20 20  _UTF8;.#endif.  
30256 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61  .  /* Check if a
30257 6e 20 65 78 69 73 74 69 6e 67 20 66 75 6e 63 74  n existing funct
30258 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 6f 76 65  ion is being ove
30259 72 72 69 64 64 65 6e 20 6f 72 20 64 65 6c 65 74  rridden or delet
3025a 65 64 2e 20 49 66 20 73 6f 2c 0a 20 20 2a 2a 20  ed. If so,.  ** 
3025b 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 63  and there are ac
3025c 74 69 76 65 20 56 4d 73 2c 20 74 68 65 6e 20 72  tive VMs, then r
3025d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
3025e 59 2e 20 49 66 20 61 20 66 75 6e 63 74 69 6f 6e  Y. If a function
3025f 0a 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 6f  .  ** is being o
30260 76 65 72 72 69 64 64 65 6e 2f 64 65 6c 65 74 65  verridden/delete
30261 64 20 62 75 74 20 74 68 65 72 65 20 61 72 65 20  d but there are 
30262 6e 6f 20 61 63 74 69 76 65 20 56 4d 73 2c 20 61  no active VMs, a
30263 6c 6c 6f 77 20 74 68 65 0a 20 20 2a 2a 20 6f 70  llow the.  ** op
30264 65 72 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 74 69  eration to conti
30265 6e 75 65 20 62 75 74 20 69 6e 76 61 6c 69 64 61  nue but invalida
30266 74 65 20 61 6c 6c 20 70 72 65 63 6f 6d 70 69 6c  te all precompil
30267 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20  ed statements.. 
30268 20 2a 2f 0a 20 20 70 20 3d 20 73 71 6c 69 74 65   */.  p = sqlite
30269 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62  3FindFunction(db
3026a 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c  , zFunctionName,
3026b 20 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 28 75   nName, nArg, (u
3026c 38 29 65 6e 63 2c 20 30 29 3b 0a 20 20 69 66 28  8)enc, 0);.  if(
3026d 20 70 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e   p && p->iPrefEn
3026e 63 3d 3d 65 6e 63 20 26 26 20 70 2d 3e 6e 41 72  c==enc && p->nAr
3026f 67 3d 3d 6e 41 72 67 20 29 7b 0a 20 20 20 20 69  g==nArg ){.    i
30270 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  f( db->activeVdb
30271 65 43 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71  eCnt ){.      sq
30272 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
30273 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20  QLITE_BUSY, .   
30274 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
30275 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20 75 73  delete/modify us
30276 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 75 65 20  er-function due 
30277 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65 6d  to active statem
30278 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 61 73  ents");.      as
30279 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
3027a 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20  cFailed );.     
3027b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
3027c 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  USY;.    }else{.
3027d 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
3027e 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
3027f 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 7d  ments(db);.    }
30280 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69  .  }..  p = sqli
30281 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  te3FindFunction(
30282 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  db, zFunctionNam
30283 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  e, nName, nArg, 
30284 28 75 38 29 65 6e 63 2c 20 31 29 3b 0a 20 20 61  (u8)enc, 1);.  a
30285 73 73 65 72 74 28 70 20 7c 7c 20 64 62 2d 3e 6d  ssert(p || db->m
30286 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20  allocFailed);.  
30287 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65  if( !p ){.    re
30288 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
30289 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 66 6c 61 67  M;.  }.  p->flag
3028a 73 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 46 75 6e  s = 0;.  p->xFun
3028b 63 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 2d 3e  c = xFunc;.  p->
3028c 78 53 74 65 70 20 3d 20 78 53 74 65 70 3b 0a 20  xStep = xStep;. 
3028d 20 70 2d 3e 78 46 69 6e 61 6c 69 7a 65 20 3d 20   p->xFinalize = 
3028e 78 46 69 6e 61 6c 3b 0a 20 20 70 2d 3e 70 55 73  xFinal;.  p->pUs
3028f 65 72 44 61 74 61 20 3d 20 70 55 73 65 72 44 61  erData = pUserDa
30290 74 61 3b 0a 20 20 70 2d 3e 6e 41 72 67 20 3d 20  ta;.  p->nArg = 
30291 28 75 31 36 29 6e 41 72 67 3b 0a 20 20 72 65 74  (u16)nArg;.  ret
30292 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
30293 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 6e  ../*.** Create n
30294 65 77 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  ew user function
30295 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
30296 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
30297 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20  ate_function(.  
30298 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
30299 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63  onst char *zFunc
3029a 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
3029b 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 6e 63 2c  nArg,.  int enc,
3029c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20 20 76 6f  .  void *p,.  vo
3029d 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
3029e 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
3029f 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
302a0 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74  *),.  void (*xSt
302a1 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
302a2 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
302a3 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f  _value **),.  vo
302a4 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
302a5 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29  ite3_context*).)
302a6 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
302a7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
302a8 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
302a9 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  rc = sqlite3Crea
302aa 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63  teFunc(db, zFunc
302ab 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  tionName, nArg, 
302ac 65 6e 63 2c 20 70 2c 20 78 46 75 6e 63 2c 20 78  enc, p, xFunc, x
302ad 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20  Step, xFinal);. 
302ae 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
302af 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
302b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
302b1 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
302b2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
302b3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
302b4 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
302b5 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
302b6 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
302b7 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  16(.  sqlite3 *d
302b8 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  b,.  const void 
302b9 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a  *zFunctionName,.
302ba 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e    int nArg,.  in
302bb 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f  t eTextRep,.  vo
302bc 69 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a  id *p,.  void (*
302bd 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
302be 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
302bf 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
302c0 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
302c1 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
302c2 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
302c3 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46  **),.  void (*xF
302c4 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f  inal)(sqlite3_co
302c5 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e 74  ntext*).){.  int
302c6 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 75   rc;.  char *zFu
302c7 6e 63 38 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  nc8;.  sqlite3_m
302c8 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
302c9 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28  utex);.  assert(
302ca 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
302cb 65 64 20 29 3b 0a 20 20 7a 46 75 6e 63 38 20 3d  ed );.  zFunc8 =
302cc 20 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38   sqlite3Utf16to8
302cd 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
302ce 6d 65 2c 20 2d 31 29 3b 0a 20 20 72 63 20 3d 20  me, -1);.  rc = 
302cf 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
302d0 63 28 64 62 2c 20 7a 46 75 6e 63 38 2c 20 6e 41  c(db, zFunc8, nA
302d1 72 67 2c 20 65 54 65 78 74 52 65 70 2c 20 70 2c  rg, eTextRep, p,
302d2 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78   xFunc, xStep, x
302d3 46 69 6e 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65  Final);.  sqlite
302d4 33 44 62 46 72 65 65 28 64 62 2c 20 7a 46 75 6e  3DbFree(db, zFun
302d5 63 38 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  c8);.  rc = sqli
302d6 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
302d7 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
302d8 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
302d9 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
302da 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  c;.}.#endif.../*
302db 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 68 61 74  .** Declare that
302dc 20 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20   a function has 
302dd 62 65 65 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20  been overloaded 
302de 62 79 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  by a virtual tab
302df 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  le..**.** If the
302e0 20 66 75 6e 63 74 69 6f 6e 20 61 6c 72 65 61 64   function alread
302e1 79 20 65 78 69 73 74 73 20 61 73 20 61 20 72 65  y exists as a re
302e2 67 75 6c 61 72 20 67 6c 6f 62 61 6c 20 66 75 6e  gular global fun
302e3 63 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 74  ction, then.** t
302e4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
302e5 20 6e 6f 2d 6f 70 2e 20 20 49 66 20 74 68 65 20   no-op.  If the 
302e6 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  function does no
302e7 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 63 72  t exist, then cr
302e8 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e  eate.** a new on
302e9 65 20 74 68 61 74 20 61 6c 77 61 79 73 20 74 68  e that always th
302ea 72 6f 77 73 20 61 20 72 75 6e 2d 74 69 6d 65 20  rows a run-time 
302eb 65 72 72 6f 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 57  error.  .**.** W
302ec 68 65 6e 20 76 69 72 74 75 61 6c 20 74 61 62 6c  hen virtual tabl
302ed 65 73 20 69 6e 74 65 6e 64 20 74 6f 20 70 72 6f  es intend to pro
302ee 76 69 64 65 20 61 6e 20 6f 76 65 72 6c 6f 61 64  vide an overload
302ef 65 64 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65  ed function, the
302f0 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c  y.** should call
302f1 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
302f2 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 67   make sure the g
302f3 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20 65  lobal function e
302f4 78 69 73 74 73 2e 0a 2a 2a 20 41 20 67 6c 6f 62  xists..** A glob
302f5 61 6c 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  al function must
302f6 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 20   exist in order 
302f7 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74  for name resolut
302f8 69 6f 6e 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 70  ion to work.** p
302f9 72 6f 70 65 72 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  roperly..*/.SQLI
302fa 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
302fb 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
302fc 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  tion(.  sqlite3 
302fd 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  *db,.  const cha
302fe 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  r *zName,.  int 
302ff 6e 41 72 67 0a 29 7b 0a 20 20 69 6e 74 20 6e 4e  nArg.){.  int nN
30300 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
30301 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20  len30(zName);.  
30302 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
30303 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
30304 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
30305 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
30306 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e  ion(db, zName, n
30307 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49  Name, nArg, SQLI
30308 54 45 5f 55 54 46 38 2c 20 30 29 3d 3d 30 20 29  TE_UTF8, 0)==0 )
30309 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65  {.    sqlite3Cre
3030a 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 4e 61 6d  ateFunc(db, zNam
3030b 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f  e, nArg, SQLITE_
3030c 55 54 46 38 2c 0a 20 20 20 20 20 20 20 20 20 20  UTF8,.          
3030d 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 73              0, s
3030e 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e  qlite3InvalidFun
3030f 63 74 69 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20 20  ction, 0, 0);.  
30310 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
30311 41 70 69 45 78 69 74 28 64 62 2c 20 53 51 4c 49  ApiExit(db, SQLI
30312 54 45 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69 74 65  TE_OK);.  sqlite
30313 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
30314 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
30315 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65  rn rc;.}..#ifnde
30316 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
30317 41 43 45 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  ACE./*.** Regist
30318 65 72 20 61 20 74 72 61 63 65 20 66 75 6e 63 74  er a trace funct
30319 69 6f 6e 2e 20 20 54 68 65 20 70 41 72 67 20 66  ion.  The pArg f
3031a 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73  rom the previous
3031b 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 74 72  ly registered tr
3031c 61 63 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  ace.** is return
3031d 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  ed.  .**.** A NU
3031e 4c 4c 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f  LL trace functio
3031f 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f 20  n means that no 
30320 74 72 61 63 69 6e 67 20 69 73 20 65 78 65 63 75  tracing is execu
30321 74 65 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c  tes.  A non-NULL
30322 0a 2a 2a 20 74 72 61 63 65 20 69 73 20 61 20 70  .** trace is a p
30323 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
30324 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76  tion that is inv
30325 6f 6b 65 64 20 61 74 20 74 68 65 20 73 74 61 72  oked at the star
30326 74 20 6f 66 20 65 61 63 68 0a 2a 2a 20 53 51 4c  t of each.** SQL
30327 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
30328 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
30329 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71  sqlite3_trace(sq
3032a 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
3032b 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c  (*xTrace)(void*,
3032c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 20 76 6f  const char*), vo
3032d 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 76 6f 69  id *pArg){.  voi
3032e 64 20 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74  d *pOld;.  sqlit
3032f 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
30330 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c  b->mutex);.  pOl
30331 64 20 3d 20 64 62 2d 3e 70 54 72 61 63 65 41 72  d = db->pTraceAr
30332 67 3b 0a 20 20 64 62 2d 3e 78 54 72 61 63 65 20  g;.  db->xTrace 
30333 3d 20 78 54 72 61 63 65 3b 0a 20 20 64 62 2d 3e  = xTrace;.  db->
30334 70 54 72 61 63 65 41 72 67 20 3d 20 70 41 72 67  pTraceArg = pArg
30335 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
30336 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
30337 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c  x);.  return pOl
30338 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  d;.}./*.** Regis
30339 74 65 72 20 61 20 70 72 6f 66 69 6c 65 20 66 75  ter a profile fu
3033a 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 70 41 72  nction.  The pAr
3033b 67 20 66 72 6f 6d 20 74 68 65 20 70 72 65 76 69  g from the previ
3033c 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
3033d 20 0a 2a 2a 20 70 72 6f 66 69 6c 65 20 66 75 6e   .** profile fun
3033e 63 74 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65  ction is returne
3033f 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c  d.  .**.** A NUL
30340 4c 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69  L profile functi
30341 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f  on means that no
30342 20 70 72 6f 66 69 6c 69 6e 67 20 69 73 20 65 78   profiling is ex
30343 65 63 75 74 65 73 2e 20 20 41 20 6e 6f 6e 2d 4e  ecutes.  A non-N
30344 55 4c 4c 0a 2a 2a 20 70 72 6f 66 69 6c 65 20 69  ULL.** profile i
30345 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
30346 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 69   function that i
30347 73 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 65  s invoked at the
30348 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 0a 2a   conclusion of.*
30349 2a 20 65 61 63 68 20 53 51 4c 20 73 74 61 74 65  * each SQL state
3034a 6d 65 6e 74 20 74 68 61 74 20 69 73 20 72 75 6e  ment that is run
3034b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3034c 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72  void *sqlite3_pr
3034d 6f 66 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  ofile(.  sqlite3
3034e 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 28 2a 78   *db,.  void (*x
3034f 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63  Profile)(void*,c
30350 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74  onst char*,sqlit
30351 65 5f 75 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69  e_uint64),.  voi
30352 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 76 6f 69  d *pArg.){.  voi
30353 64 20 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74  d *pOld;.  sqlit
30354 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
30355 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c  b->mutex);.  pOl
30356 64 20 3d 20 64 62 2d 3e 70 50 72 6f 66 69 6c 65  d = db->pProfile
30357 41 72 67 3b 0a 20 20 64 62 2d 3e 78 50 72 6f 66  Arg;.  db->xProf
30358 69 6c 65 20 3d 20 78 50 72 6f 66 69 6c 65 3b 0a  ile = xProfile;.
30359 20 20 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72    db->pProfileAr
3035a 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69  g = pArg;.  sqli
3035b 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
3035c 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
3035d 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 23 65 6e  turn pOld;.}.#en
3035e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
3035f 49 54 5f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 2a  IT_TRACE */../**
30360 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2a  * EXPERIMENTAL *
30361 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65  **.**.** Registe
30362 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  r a function to 
30363 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20  be invoked when 
30364 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  a transaction co
30365 6d 6d 65 6e 74 73 2e 0a 2a 2a 20 49 66 20 74 68  mments..** If th
30366 65 20 69 6e 76 6f 6b 65 64 20 66 75 6e 63 74 69  e invoked functi
30367 6f 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  on returns non-z
30368 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  ero, then the co
30369 6d 6d 69 74 20 62 65 63 6f 6d 65 73 20 61 0a 2a  mmit becomes a.*
3036a 2a 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 53  * rollback..*/.S
3036b 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
3036c 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68  sqlite3_commit_h
3036d 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ook(.  sqlite3 *
3036e 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
3036f 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20 68   /* Attach the h
30370 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64 61 74 61  ook to this data
30371 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  base */.  int (*
30372 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  xCallback)(void*
30373 29 2c 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20  ),  /* Function 
30374 74 6f 20 69 6e 76 6f 6b 65 20 6f 6e 20 65 61 63  to invoke on eac
30375 68 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 76 6f  h commit */.  vo
30376 69 64 20 2a 70 41 72 67 20 20 20 20 20 20 20 20  id *pArg        
30377 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
30378 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ent to the funct
30379 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64  ion */.){.  void
3037a 20 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65   *pOld;.  sqlite
3037b 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
3037c 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64  ->mutex);.  pOld
3037d 20 3d 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72   = db->pCommitAr
3037e 67 3b 0a 20 20 64 62 2d 3e 78 43 6f 6d 6d 69 74  g;.  db->xCommit
3037f 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c  Callback = xCall
30380 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 43 6f 6d  back;.  db->pCom
30381 6d 69 74 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  mitArg = pArg;. 
30382 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
30383 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
30384 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a  .  return pOld;.
30385 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
30386 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20  r a callback to 
30387 62 65 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20  be invoked each 
30388 74 69 6d 65 20 61 20 72 6f 77 20 69 73 20 75 70  time a row is up
30389 64 61 74 65 64 2c 0a 2a 2a 20 69 6e 73 65 72 74  dated,.** insert
3038a 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 75 73  ed or deleted us
3038b 69 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73  ing this databas
3038c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
3038d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3038e 20 2a 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65   *sqlite3_update
3038f 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33  _hook(.  sqlite3
30390 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
30391 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65     /* Attach the
30392 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64 61   hook to this da
30393 74 61 62 61 73 65 20 2a 2f 0a 20 20 76 6f 69 64  tabase */.  void
30394 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f   (*xCallback)(vo
30395 69 64 2a 2c 69 6e 74 2c 63 68 61 72 20 63 6f 6e  id*,int,char con
30396 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20  st *,char const 
30397 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 2c  *,sqlite_int64),
30398 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20 20  .  void *pArg   
30399 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3039a 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  Argument to the 
3039b 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  function */.){. 
3039c 20 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20 73   void *pRet;.  s
3039d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3039e 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
3039f 20 70 52 65 74 20 3d 20 64 62 2d 3e 70 55 70 64   pRet = db->pUpd
303a0 61 74 65 41 72 67 3b 0a 20 20 64 62 2d 3e 78 55  ateArg;.  db->xU
303a1 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 3d 20  pdateCallback = 
303a2 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64 62 2d  xCallback;.  db-
303a3 3e 70 55 70 64 61 74 65 41 72 67 20 3d 20 70 41  >pUpdateArg = pA
303a4 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rg;.  sqlite3_mu
303a5 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
303a6 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  tex);.  return p
303a7 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  Ret;.}../*.** Re
303a8 67 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63  gister a callbac
303a9 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  k to be invoked 
303aa 65 61 63 68 20 74 69 6d 65 20 61 20 74 72 61 6e  each time a tran
303ab 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
303ac 64 0a 2a 2a 20 62 61 63 6b 20 62 79 20 74 68 69  d.** back by thi
303ad 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
303ae 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
303af 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
303b0 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
303b1 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
303b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
303b3 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b   Attach the hook
303b4 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73   to this databas
303b5 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43  e */.  void (*xC
303b6 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c  allback)(void*),
303b7 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e   /* Callback fun
303b8 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
303b9 2a 70 41 72 67 20 20 20 20 20 20 20 20 20 20 20  *pArg           
303ba 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
303bb 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
303bc 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70   */.){.  void *p
303bd 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Ret;.  sqlite3_m
303be 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
303bf 75 74 65 78 29 3b 0a 20 20 70 52 65 74 20 3d 20  utex);.  pRet = 
303c0 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67  db->pRollbackArg
303c1 3b 0a 20 20 64 62 2d 3e 78 52 6f 6c 6c 62 61 63  ;.  db->xRollbac
303c2 6b 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c  kCallback = xCal
303c3 6c 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 52 6f  lback;.  db->pRo
303c4 6c 6c 62 61 63 6b 41 72 67 20 3d 20 70 41 72 67  llbackArg = pArg
303c5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
303c6 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
303c7 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  x);.  return pRe
303c8 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  t;.}../*.** This
303c9 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
303ca 73 20 74 72 75 65 20 69 66 20 6d 61 69 6e 2d 6d  s true if main-m
303cb 65 6d 6f 72 79 20 73 68 6f 75 6c 64 20 62 65 20  emory should be 
303cc 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a  used instead of.
303cd 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  ** a temporary f
303ce 69 6c 65 20 66 6f 72 20 74 72 61 6e 73 69 65 6e  ile for transien
303cf 74 20 70 61 67 65 72 20 66 69 6c 65 73 20 61 6e  t pager files an
303d0 64 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  d statement jour
303d1 6e 61 6c 73 2e 0a 2a 2a 20 54 68 65 20 76 61 6c  nals..** The val
303d2 75 65 20 72 65 74 75 72 6e 65 64 20 64 65 70 65  ue returned depe
303d3 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  nds on the value
303d4 20 6f 66 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f   of db->temp_sto
303d5 72 65 20 28 72 75 6e 74 69 6d 65 0a 2a 2a 20 70  re (runtime.** p
303d6 61 72 61 6d 65 74 65 72 29 20 61 6e 64 20 74 68  arameter) and th
303d7 65 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 76  e compile time v
303d8 61 6c 75 65 20 6f 66 20 53 51 4c 49 54 45 5f 54  alue of SQLITE_T
303d9 45 4d 50 5f 53 54 4f 52 45 2e 20 54 68 65 0a 2a  EMP_STORE. The.*
303da 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  * following tabl
303db 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  e describes the 
303dc 72 65 6c 61 74 69 6f 6e 73 68 69 70 20 62 65 74  relationship bet
303dd 77 65 65 6e 20 74 68 65 73 65 20 74 77 6f 20 76  ween these two v
303de 61 6c 75 65 73 0a 2a 2a 20 61 6e 64 20 74 68 69  alues.** and thi
303df 73 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75  s functions retu
303e0 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  rn value..**.** 
303e1 20 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54    SQLITE_TEMP_ST
303e2 4f 52 45 20 20 20 20 20 64 62 2d 3e 74 65 6d 70  ORE     db->temp
303e3 5f 73 74 6f 72 65 20 20 20 20 20 4c 6f 63 61 74  _store     Locat
303e4 69 6f 6e 20 6f 66 20 74 65 6d 70 6f 72 61 72 79  ion of temporary
303e5 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 2d   database.**   -
303e6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
303e7 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       -----------
303e8 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ---     --------
303e9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
303ea 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 30 20 20 20  ------.**   0   
303eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303ec 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
303ed 20 20 20 20 20 66 69 6c 65 20 20 20 20 20 20 28       file      (
303ee 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20 20 20 31  return 0).**   1
303ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303f0 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20       1          
303f1 20 20 20 20 20 20 20 20 66 69 6c 65 20 20 20 20          file    
303f2 20 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20    (return 0).** 
303f3 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20    1             
303f4 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
303f5 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72             memor
303f6 79 20 20 20 20 28 72 65 74 75 72 6e 20 31 29 0a  y    (return 1).
303f7 2a 2a 20 20 20 31 20 20 20 20 20 20 20 20 20 20  **   1          
303f8 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20             0    
303f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
303fa 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e 20  le      (return 
303fb 30 29 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20  0).**   2       
303fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
303fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303fe 20 66 69 6c 65 20 20 20 20 20 20 28 72 65 74 75   file      (retu
303ff 72 6e 20 30 29 0a 2a 2a 20 20 20 32 20 20 20 20  rn 0).**   2    
30400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30401 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
30402 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72      memory    (r
30403 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20 20 32 20  eturn 1).**   2 
30404 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30405 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
30406 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20         memory   
30407 20 28 72 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20   (return 1).**  
30408 20 33 20 20 20 20 20 20 20 20 20 20 20 20 20 20   3              
30409 20 20 20 20 20 20 20 61 6e 79 20 20 20 20 20 20         any      
3040a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79            memory
3040b 20 20 20 20 28 72 65 74 75 72 6e 20 31 29 0a 2a      (return 1).*
3040c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
3040d 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65 6d 70   int sqlite3Temp
3040e 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73  InMemory(const s
3040f 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 23 69 66  qlite3 *db){.#if
30410 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
30411 52 45 3d 3d 31 0a 20 20 72 65 74 75 72 6e 20 28  RE==1.  return (
30412 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d   db->temp_store=
30413 3d 32 20 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  =2 );.#endif.#if
30414 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
30415 52 45 3d 3d 32 0a 20 20 72 65 74 75 72 6e 20 28  RE==2.  return (
30416 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 21   db->temp_store!
30417 3d 31 20 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  =1 );.#endif.#if
30418 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
30419 52 45 3d 3d 33 0a 20 20 72 65 74 75 72 6e 20 31  RE==3.  return 1
3041a 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e  ;.#else.  return
3041b 20 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a   0;.#endif.}../*
3041c 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
3041d 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72   is called to cr
3041e 65 61 74 65 20 61 20 63 6f 6e 6e 65 63 74 69 6f  eate a connectio
3041f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  n to a database 
30420 42 54 72 65 65 0a 2a 2a 20 64 72 69 76 65 72 2e  BTree.** driver.
30421 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69    If zFilename i
30422 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  s the name of a 
30423 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 61 74 20  file, then that 
30424 66 69 6c 65 20 69 73 0a 2a 2a 20 6f 70 65 6e 65  file is.** opene
30425 64 20 61 6e 64 20 75 73 65 64 2e 20 20 49 66 20  d and used.  If 
30426 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65  zFilename is the
30427 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65   magic name ":me
30428 6d 6f 72 79 3a 22 20 74 68 65 6e 0a 2a 2a 20 74  mory:" then.** t
30429 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 73  he database is s
3042a 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  tored in memory 
3042b 28 61 6e 64 20 69 73 20 74 68 75 73 20 66 6f 72  (and is thus for
3042c 67 6f 74 74 65 6e 20 61 73 20 73 6f 6f 6e 20 61  gotten as soon a
3042d 73 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74  s.** the connect
3042e 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 29 20  ion is closed.) 
3042f 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73   If zFilename is
30430 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 64   NULL then the d
30431 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 61 20  atabase.** is a 
30432 22 76 69 72 74 75 61 6c 22 20 64 61 74 61 62 61  "virtual" databa
30433 73 65 20 66 6f 72 20 74 72 61 6e 73 69 65 6e 74  se for transient
30434 20 75 73 65 20 6f 6e 6c 79 20 61 6e 64 20 69 73   use only and is
30435 20 64 65 6c 65 74 65 64 20 61 73 0a 2a 2a 20 73   deleted as.** s
30436 6f 6f 6e 20 61 73 20 74 68 65 20 63 6f 6e 6e 65  oon as the conne
30437 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e  ction is closed.
30438 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c  .**.** A virtual
30439 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65   database can be
3043a 20 65 69 74 68 65 72 20 61 20 64 69 73 6b 20 66   either a disk f
3043b 69 6c 65 20 28 74 68 61 74 20 69 73 20 61 75 74  ile (that is aut
3043c 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 64 65  omatically.** de
3043d 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 66  leted when the f
3043e 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 29 20 6f  ile is closed) o
3043f 72 20 69 74 20 61 6e 20 62 65 20 68 65 6c 64 20  r it an be held 
30440 65 6e 74 69 72 65 6c 79 20 69 6e 20 6d 65 6d 6f  entirely in memo
30441 72 79 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ry..** The sqlit
30442 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 29  e3TempInMemory()
30443 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
30444 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  d to determine w
30445 68 69 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hich..*/.SQLITE_
30446 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
30447 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
30448 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
30449 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20   *db,        /* 
3044a 4d 61 69 6e 20 64 61 74 61 62 61 73 65 20 77 68  Main database wh
3044b 65 6e 20 6f 70 65 6e 69 6e 67 20 61 75 78 20 6f  en opening aux o
3044c 74 68 65 72 77 69 73 65 20 30 20 2a 2f 0a 20 20  therwise 0 */.  
3044d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
3044e 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d  ename,    /* Nam
3044f 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f  e of the file co
30450 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72  ntaining the BTr
30451 65 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  ee database */. 
30452 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c   int omitJournal
30453 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 66  ,          /* if
30454 20 54 52 55 45 20 74 68 65 6e 20 64 6f 20 6e 6f   TRUE then do no
30455 74 20 6a 6f 75 72 6e 61 6c 20 74 68 69 73 20 66  t journal this f
30456 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61  ile */.  int nCa
30457 63 68 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  che,            
30458 20 20 20 2f 2a 20 48 6f 77 20 6d 61 6e 79 20 70     /* How many p
30459 61 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65  ages in the page
3045a 20 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20   cache */.  int 
3045b 76 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20 20  vfsFlags,       
3045c 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70        /* Flags p
3045d 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f  assed through to
3045e 20 76 66 73 4f 70 65 6e 20 2a 2f 0a 20 20 42 74   vfsOpen */.  Bt
3045f 72 65 65 20 2a 2a 70 70 42 74 72 65 65 20 20 20  ree **ppBtree   
30460 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
30461 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65 65 20  er to new Btree 
30462 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e 20 68  object written h
30463 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
30464 62 74 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 69  btFlags = 0;.  i
30465 6e 74 20 72 63 3b 0a 20 20 0a 20 20 61 73 73 65  nt rc;.  .  asse
30466 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
30467 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
30468 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
30469 70 42 74 72 65 65 20 21 3d 20 30 29 3b 0a 20 20  pBtree != 0);.  
3046a 69 66 28 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c 20  if( omitJournal 
3046b 29 7b 0a 20 20 20 20 62 74 46 6c 61 67 73 20 7c  ){.    btFlags |
3046c 3d 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55  = BTREE_OMIT_JOU
3046d 52 4e 41 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  RNAL;.  }.  if( 
3046e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
3046f 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b  TE_NoReadlock ){
30470 0a 20 20 20 20 62 74 46 6c 61 67 73 20 7c 3d 20  .    btFlags |= 
30471 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43  BTREE_NO_READLOC
30472 4b 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  K;.  }.#ifndef S
30473 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
30474 59 44 42 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  YDB.  if( zFilen
30475 61 6d 65 3d 3d 30 20 26 26 20 73 71 6c 69 74 65  ame==0 && sqlite
30476 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 64 62  3TempInMemory(db
30477 29 20 29 7b 0a 20 20 20 20 7a 46 69 6c 65 6e 61  ) ){.    zFilena
30478 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79 3a 22 3b  me = ":memory:";
30479 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
3047a 66 28 20 28 76 66 73 46 6c 61 67 73 20 26 20 53  f( (vfsFlags & S
3047b 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
3047c 44 42 29 21 3d 30 20 26 26 20 28 7a 46 69 6c 65  DB)!=0 && (zFile
3047d 6e 61 6d 65 3d 3d 30 20 7c 7c 20 2a 7a 46 69 6c  name==0 || *zFil
3047e 65 6e 61 6d 65 3d 3d 30 29 20 29 7b 0a 20 20 20  ename==0) ){.   
3047f 20 76 66 73 46 6c 61 67 73 20 3d 20 28 76 66 73   vfsFlags = (vfs
30480 46 6c 61 67 73 20 26 20 7e 53 51 4c 49 54 45 5f  Flags & ~SQLITE_
30481 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 20 7c 20  OPEN_MAIN_DB) | 
30482 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
30483 5f 44 42 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  _DB;.  }.  rc = 
30484 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e  sqlite3BtreeOpen
30485 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 28 73 71 6c  (zFilename, (sql
30486 69 74 65 33 20 2a 29 64 62 2c 20 70 70 42 74 72  ite3 *)db, ppBtr
30487 65 65 2c 20 62 74 46 6c 61 67 73 2c 20 76 66 73  ee, btFlags, vfs
30488 46 6c 61 67 73 29 3b 0a 0a 20 20 2f 2a 20 49 66  Flags);..  /* If
30489 20 74 68 65 20 42 2d 54 72 65 65 20 77 61 73 20   the B-Tree was 
3048a 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65  successfully ope
3048b 6e 65 64 2c 20 73 65 74 20 74 68 65 20 70 61 67  ned, set the pag
3048c 65 72 2d 63 61 63 68 65 20 73 69 7a 65 20 74 6f  er-cache size to
3048d 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75 6c   the.  ** defaul
3048e 74 20 76 61 6c 75 65 2e 20 45 78 63 65 70 74 2c  t value. Except,
3048f 20 69 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20   if the call to 
30490 42 74 72 65 65 4f 70 65 6e 28 29 20 72 65 74 75  BtreeOpen() retu
30491 72 6e 65 64 20 61 20 68 61 6e 64 6c 65 0a 20 20  rned a handle.  
30492 2a 2a 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 65 78  ** open on an ex
30493 69 73 74 69 6e 67 20 73 68 61 72 65 64 20 70 61  isting shared pa
30494 67 65 72 2d 63 61 63 68 65 2c 20 64 6f 20 6e 6f  ger-cache, do no
30495 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 67  t change the pag
30496 65 72 2d 63 61 63 68 65 20 0a 20 20 2a 2a 20 73  er-cache .  ** s
30497 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ize..  */.  if( 
30498 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
30499 20 30 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65   0==sqlite3Btree
3049a 53 63 68 65 6d 61 28 2a 70 70 42 74 72 65 65 2c  Schema(*ppBtree,
3049b 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 73 71   0, 0) ){.    sq
3049c 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
3049d 68 65 53 69 7a 65 28 2a 70 70 42 74 72 65 65 2c  heSize(*ppBtree,
3049e 20 6e 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20   nCache);.  }.  
3049f 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
304a0 0a 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 38  .** Return UTF-8
304a1 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68   encoded English
304a2 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e   language explan
304a3 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73  ation of the mos
304a4 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f  t recent.** erro
304a5 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
304a6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
304a7 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c 69  ite3_errmsg(sqli
304a8 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 6f 6e 73  te3 *db){.  cons
304a9 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28  t char *z;.  if(
304aa 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74 75   !db ){.    retu
304ab 72 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  rn sqlite3ErrStr
304ac 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a  (SQLITE_NOMEM);.
304ad 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69 74    }.  if( !sqlit
304ae 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63  e3SafetyCheckSic
304af 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20  kOrOk(db) ){.   
304b0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45   return sqlite3E
304b1 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4d 49 53  rrStr(SQLITE_MIS
304b2 55 53 45 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  USE);.  }.  sqli
304b3 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
304b4 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66  db->mutex);.  if
304b5 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
304b6 65 64 20 29 7b 0a 20 20 20 20 7a 20 3d 20 73 71  ed ){.    z = sq
304b7 6c 69 74 65 33 45 72 72 53 74 72 28 53 51 4c 49  lite3ErrStr(SQLI
304b8 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 65 6c  TE_NOMEM);.  }el
304b9 73 65 7b 0a 20 20 20 20 7a 20 3d 20 28 63 68 61  se{.    z = (cha
304ba 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r*)sqlite3_value
304bb 5f 74 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b  _text(db->pErr);
304bc 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 64 62  .    assert( !db
304bd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
304be 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29  ;.    if( z==0 )
304bf 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69  {.      z = sqli
304c0 74 65 33 45 72 72 53 74 72 28 64 62 2d 3e 65 72  te3ErrStr(db->er
304c1 72 43 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20  rCode);.    }.  
304c2 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
304c3 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
304c4 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  x);.  return z;.
304c5 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
304c6 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
304c7 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 31 36  ** Return UTF-16
304c8 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68   encoded English
304c9 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e   language explan
304ca 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73  ation of the mos
304cb 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f  t recent.** erro
304cc 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
304cd 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
304ce 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 73 71  ite3_errmsg16(sq
304cf 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 74  lite3 *db){.  st
304d0 61 74 69 63 20 63 6f 6e 73 74 20 75 31 36 20 6f  atic const u16 o
304d1 75 74 4f 66 4d 65 6d 5b 5d 20 3d 20 7b 0a 20 20  utOfMem[] = {.  
304d2 20 20 27 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c    'o', 'u', 't',
304d3 20 27 20 27 2c 20 27 6f 27 2c 20 27 66 27 2c 20   ' ', 'o', 'f', 
304d4 27 20 27 2c 20 27 6d 27 2c 20 27 65 27 2c 20 27  ' ', 'm', 'e', '
304d5 6d 27 2c 20 27 6f 27 2c 20 27 72 27 2c 20 27 79  m', 'o', 'r', 'y
304d6 27 2c 20 30 0a 20 20 7d 3b 0a 20 20 73 74 61 74  ', 0.  };.  stat
304d7 69 63 20 63 6f 6e 73 74 20 75 31 36 20 6d 69 73  ic const u16 mis
304d8 75 73 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 27 6c  use[] = {.    'l
304d9 27 2c 20 27 69 27 2c 20 27 62 27 2c 20 27 72 27  ', 'i', 'b', 'r'
304da 2c 20 27 61 27 2c 20 27 72 27 2c 20 27 79 27 2c  , 'a', 'r', 'y',
304db 20 27 20 27 2c 20 0a 20 20 20 20 27 72 27 2c 20   ' ', .    'r', 
304dc 27 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20 27  'o', 'u', 't', '
304dd 69 27 2c 20 27 6e 27 2c 20 27 65 27 2c 20 27 20  i', 'n', 'e', ' 
304de 27 2c 20 0a 20 20 20 20 27 63 27 2c 20 27 61 27  ', .    'c', 'a'
304df 2c 20 27 6c 27 2c 20 27 6c 27 2c 20 27 65 27 2c  , 'l', 'l', 'e',
304e0 20 27 64 27 2c 20 27 20 27 2c 20 0a 20 20 20 20   'd', ' ', .    
304e1 27 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20 27  'o', 'u', 't', '
304e2 20 27 2c 20 0a 20 20 20 20 27 6f 27 2c 20 27 66   ', .    'o', 'f
304e3 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27 73 27  ', ' ', .    's'
304e4 2c 20 27 65 27 2c 20 27 71 27 2c 20 27 75 27 2c  , 'e', 'q', 'u',
304e5 20 27 65 27 2c 20 27 6e 27 2c 20 27 63 27 2c 20   'e', 'n', 'c', 
304e6 27 65 27 2c 20 30 0a 20 20 7d 3b 0a 0a 20 20 63  'e', 0.  };..  c
304e7 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 3b 0a 20 20  onst void *z;.  
304e8 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20 20 72  if( !db ){.    r
304e9 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 6f 75  eturn (void *)ou
304ea 74 4f 66 4d 65 6d 3b 0a 20 20 7d 0a 20 20 69 66  tOfMem;.  }.  if
304eb 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
304ec 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62  CheckSickOrOk(db
304ed 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
304ee 28 76 6f 69 64 20 2a 29 6d 69 73 75 73 65 3b 0a  (void *)misuse;.
304ef 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
304f0 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
304f1 74 65 78 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  tex);.  if( db->
304f2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
304f3 20 20 20 20 7a 20 3d 20 28 76 6f 69 64 20 2a 29      z = (void *)
304f4 6f 75 74 4f 66 4d 65 6d 3b 0a 20 20 7d 65 6c 73  outOfMem;.  }els
304f5 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  e{.    z = sqlit
304f6 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
304f7 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 69  db->pErr);.    i
304f8 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( z==0 ){.     
304f9 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
304fa 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31  Str(db->pErr, -1
304fb 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  , sqlite3ErrStr(
304fc 64 62 2d 3e 65 72 72 43 6f 64 65 29 2c 0a 20 20  db->errCode),.  
304fd 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
304fe 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
304ff 54 49 43 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20  TIC);.      z = 
30500 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
30501 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a  xt16(db->pErr);.
30502 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 41 20 6d      }.    /* A m
30503 61 6c 6c 6f 63 28 29 20 6d 61 79 20 68 61 76 65  alloc() may have
30504 20 66 61 69 6c 65 64 20 77 69 74 68 69 6e 20 74   failed within t
30505 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
30506 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
30507 29 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 2e 20  ).    ** above. 
30508 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
30509 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64 62  ase, then the db
3050a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  ->mallocFailed f
3050b 6c 61 67 20 6e 65 65 64 73 20 74 6f 0a 20 20 20  lag needs to.   
3050c 20 2a 2a 20 62 65 20 63 6c 65 61 72 65 64 20 62   ** be cleared b
3050d 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
3050e 20 44 6f 20 74 68 69 73 20 64 69 72 65 63 74 6c   Do this directl
3050f 79 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 76 69  y, instead of vi
30510 61 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  a.    ** sqlite3
30511 41 70 69 45 78 69 74 28 29 2c 20 74 6f 20 61 76  ApiExit(), to av
30512 6f 69 64 20 73 65 74 74 69 6e 67 20 74 68 65 20  oid setting the 
30513 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
30514 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 20  error message.. 
30515 20 20 20 2a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61     */.    db->ma
30516 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
30517 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
30518 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
30519 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  tex);.  return z
3051a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
3051b 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
3051c 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
3051d 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
3051e 20 65 72 72 6f 72 20 63 6f 64 65 20 67 65 6e 65   error code gene
3051f 72 61 74 65 64 20 62 79 20 61 6e 20 53 51 4c 69  rated by an SQLi
30520 74 65 20 72 6f 75 74 69 6e 65 2e 20 49 66 20 4e  te routine. If N
30521 55 4c 4c 20 69 73 0a 2a 2a 20 70 61 73 73 65 64  ULL is.** passed
30522 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
30523 6e 2c 20 77 65 20 61 73 73 75 6d 65 20 61 20 6d  n, we assume a m
30524 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64  alloc() failed d
30525 75 72 69 6e 67 20 73 71 6c 69 74 65 33 5f 6f 70  uring sqlite3_op
30526 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  en()..*/.SQLITE_
30527 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
30528 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20  errcode(sqlite3 
30529 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 20 26  *db){.  if( db &
3052a 26 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  & !sqlite3Safety
3052b 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62  CheckSickOrOk(db
3052c 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
3052d 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
3052e 20 7d 0a 20 20 69 66 28 20 21 64 62 20 7c 7c 20   }.  if( !db || 
3052f 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
30530 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
30531 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
30532 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 65 72  .  return db->er
30533 72 43 6f 64 65 20 26 20 64 62 2d 3e 65 72 72 4d  rCode & db->errM
30534 61 73 6b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  ask;.}.SQLITE_AP
30535 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78  I int sqlite3_ex
30536 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73  tended_errcode(s
30537 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
30538 66 28 20 64 62 20 26 26 20 21 73 71 6c 69 74 65  f( db && !sqlite
30539 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
3053a 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  OrOk(db) ){.    
3053b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
3053c 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SUSE;.  }.  if( 
3053d 21 64 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  !db || db->mallo
3053e 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72  cFailed ){.    r
3053f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
30540 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  EM;.  }.  return
30541 20 64 62 2d 3e 65 72 72 43 6f 64 65 3b 0a 7d 0a   db->errCode;.}.
30542 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
30543 6e 65 77 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75  new collating fu
30544 6e 63 74 69 6f 6e 20 66 6f 72 20 64 61 74 61 62  nction for datab
30545 61 73 65 20 22 64 62 22 2e 20 20 54 68 65 20 6e  ase "db".  The n
30546 61 6d 65 20 69 73 20 7a 4e 61 6d 65 0a 2a 2a 20  ame is zName.** 
30547 61 6e 64 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  and the encoding
30548 20 69 73 20 65 6e 63 2e 0a 2a 2f 0a 73 74 61 74   is enc..*/.stat
30549 69 63 20 69 6e 74 20 63 72 65 61 74 65 43 6f 6c  ic int createCol
3054a 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  lation(.  sqlite
3054b 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20  3* db, .  const 
3054c 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
3054d 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64  int enc, .  void
3054e 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78  * pCtx,.  int(*x
3054f 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69  Compare)(void*,i
30550 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
30551 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c  nt,const void*),
30552 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29 28 76  .  void(*xDel)(v
30553 6f 69 64 2a 29 0a 29 7b 0a 20 20 43 6f 6c 6c 53  oid*).){.  CollS
30554 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74  eq *pColl;.  int
30555 20 65 6e 63 32 3b 0a 20 20 69 6e 74 20 6e 4e 61   enc2;.  int nNa
30556 6d 65 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28  me;.  .  assert(
30557 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
30558 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
30559 3b 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54  ;..  /* If SQLIT
3055a 45 5f 55 54 46 31 36 20 69 73 20 73 70 65 63 69  E_UTF16 is speci
3055b 66 69 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f  fied as the enco
3055c 64 69 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73  ding type, trans
3055d 66 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74  form this.  ** t
3055e 6f 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f  o one of SQLITE_
3055f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54  UTF16LE or SQLIT
30560 45 5f 55 54 46 31 36 42 45 20 75 73 69 6e 67 20  E_UTF16BE using 
30561 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f  the.  ** SQLITE_
30562 55 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72  UTF16NATIVE macr
30563 6f 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20  o. SQLITE_UTF16 
30564 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65  is not used inte
30565 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 65  rnally..  */.  e
30566 6e 63 32 20 3d 20 65 6e 63 3b 0a 20 20 74 65 73  nc2 = enc;.  tes
30567 74 63 61 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c  tcase( enc2==SQL
30568 49 54 45 5f 55 54 46 31 36 20 29 3b 0a 20 20 74  ITE_UTF16 );.  t
30569 65 73 74 63 61 73 65 28 20 65 6e 63 32 3d 3d 53  estcase( enc2==S
3056a 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
3056b 4e 45 44 20 29 3b 0a 20 20 69 66 28 20 65 6e 63  NED );.  if( enc
3056c 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20  2==SQLITE_UTF16 
3056d 7c 7c 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f  || enc2==SQLITE_
3056e 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 29 7b  UTF16_ALIGNED ){
3056f 0a 20 20 20 20 65 6e 63 32 20 3d 20 53 51 4c 49  .    enc2 = SQLI
30570 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a  TE_UTF16NATIVE;.
30571 20 20 7d 0a 20 20 69 66 28 20 65 6e 63 32 3c 53    }.  if( enc2<S
30572 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e  QLITE_UTF8 || en
30573 63 32 3e 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c2>SQLITE_UTF16B
30574 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  E ){.    return 
30575 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
30576 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69   }..  /* Check i
30577 66 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 72  f this call is r
30578 65 6d 6f 76 69 6e 67 20 6f 72 20 72 65 70 6c 61  emoving or repla
30579 63 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67  cing an existing
3057a 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a   collation .  **
3057b 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20 73 6f   sequence. If so
3057c 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20  , and there are 
3057d 61 63 74 69 76 65 20 56 4d 73 2c 20 72 65 74 75  active VMs, retu
3057e 72 6e 20 62 75 73 79 2e 20 49 66 20 74 68 65 72  rn busy. If ther
3057f 65 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 20 61 63  e.  ** are no ac
30580 74 69 76 65 20 56 4d 73 2c 20 69 6e 76 61 6c 69  tive VMs, invali
30581 64 61 74 65 20 61 6e 79 20 70 72 65 2d 63 6f 6d  date any pre-com
30582 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73  piled statements
30583 2e 0a 20 20 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d  ..  */.  nName =
30584 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
30585 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c 6c  (zName);.  pColl
30586 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
30587 6c 6c 53 65 71 28 64 62 2c 20 28 75 38 29 65 6e  llSeq(db, (u8)en
30588 63 32 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  c2, zName, nName
30589 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c  , 0);.  if( pCol
3058a 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70  l && pColl->xCmp
3058b 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
3058c 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 7b  activeVdbeCnt ){
3058d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
3058e 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42  ror(db, SQLITE_B
3058f 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22 75  USY, .        "u
30590 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 2f  nable to delete/
30591 6d 6f 64 69 66 79 20 63 6f 6c 6c 61 74 69 6f 6e  modify collation
30592 20 73 65 71 75 65 6e 63 65 20 64 75 65 20 74 6f   sequence due to
30593 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e   active statemen
30594 74 73 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ts");.      retu
30595 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
30596 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
30597 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
30598 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 0a  tatements(db);..
30599 20 20 20 20 2f 2a 20 49 66 20 63 6f 6c 6c 61 74      /* If collat
3059a 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 70 43 6f  ion sequence pCo
3059b 6c 6c 20 77 61 73 20 63 72 65 61 74 65 64 20 64  ll was created d
3059c 69 72 65 63 74 6c 79 20 62 79 20 61 20 63 61 6c  irectly by a cal
3059d 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69  l to.    ** sqli
3059e 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
3059f 74 69 6f 6e 2c 20 61 6e 64 20 6e 6f 74 20 67 65  tion, and not ge
305a0 6e 65 72 61 74 65 64 20 62 79 20 73 79 6e 74 68  nerated by synth
305a1 43 6f 6c 6c 53 65 71 28 29 2c 0a 20 20 20 20 2a  CollSeq(),.    *
305a2 2a 20 74 68 65 6e 20 61 6e 79 20 63 6f 70 69 65  * then any copie
305a3 73 20 6d 61 64 65 20 62 79 20 73 79 6e 74 68 43  s made by synthC
305a4 6f 6c 6c 53 65 71 28 29 20 6e 65 65 64 20 74 6f  ollSeq() need to
305a5 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e   be invalidated.
305a6 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 63 6f  .    ** Also, co
305a7 6c 6c 61 74 69 6f 6e 20 64 65 73 74 72 75 63 74  llation destruct
305a8 6f 72 20 2d 20 43 6f 6c 6c 53 65 71 2e 78 44 65  or - CollSeq.xDe
305a9 6c 28 29 20 2d 20 66 75 6e 63 74 69 6f 6e 20 6d  l() - function m
305aa 61 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74  ay need.    ** t
305ab 6f 20 62 65 20 63 61 6c 6c 65 64 2e 0a 20 20 20  o be called..   
305ac 20 2a 2f 20 0a 20 20 20 20 69 66 28 20 28 70 43   */ .    if( (pC
305ad 6f 6c 6c 2d 3e 65 6e 63 20 26 20 7e 53 51 4c 49  oll->enc & ~SQLI
305ae 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
305af 29 3d 3d 65 6e 63 32 20 29 7b 0a 20 20 20 20 20  )==enc2 ){.     
305b0 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 20   CollSeq *aColl 
305b1 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  = sqlite3HashFin
305b2 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c  d(&db->aCollSeq,
305b3 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
305b4 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
305b5 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b     for(j=0; j<3;
305b6 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 43   j++){.        C
305b7 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 26 61 43 6f  ollSeq *p = &aCo
305b8 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69  ll[j];.        i
305b9 66 28 20 70 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c  f( p->enc==pColl
305ba 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  ->enc ){.       
305bb 20 20 20 69 66 28 20 70 2d 3e 78 44 65 6c 20 29     if( p->xDel )
305bc 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 2d  {.            p-
305bd 3e 78 44 65 6c 28 70 2d 3e 70 55 73 65 72 29 3b  >xDel(p->pUser);
305be 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
305bf 20 20 20 20 20 20 20 70 2d 3e 78 43 6d 70 20 3d         p->xCmp =
305c0 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
305c1 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
305c2 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  pColl = sqlit
305c3 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
305c4 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e 61 6d  , (u8)enc2, zNam
305c5 65 2c 20 6e 4e 61 6d 65 2c 20 31 29 3b 0a 20 20  e, nName, 1);.  
305c6 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
305c7 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d 20 78   pColl->xCmp = x
305c8 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 70 43 6f  Compare;.    pCo
305c9 6c 6c 2d 3e 70 55 73 65 72 20 3d 20 70 43 74 78  ll->pUser = pCtx
305ca 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65  ;.    pColl->xDe
305cb 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 70 43  l = xDel;.    pC
305cc 6f 6c 6c 2d 3e 65 6e 63 20 3d 20 28 75 38 29 28  oll->enc = (u8)(
305cd 65 6e 63 32 20 7c 20 28 65 6e 63 20 26 20 53 51  enc2 | (enc & SQ
305ce 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
305cf 45 44 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ED));.  }.  sqli
305d0 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
305d1 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65  ITE_OK, 0);.  re
305d2 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
305d3 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61  }.../*.** This a
305d4 72 72 61 79 20 64 65 66 69 6e 65 73 20 68 61 72  rray defines har
305d5 64 20 75 70 70 65 72 20 62 6f 75 6e 64 73 20 6f  d upper bounds o
305d6 6e 20 6c 69 6d 69 74 20 76 61 6c 75 65 73 2e 20  n limit values. 
305d7 20 54 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 69   The.** initiali
305d8 7a 65 72 20 6d 75 73 74 20 62 65 20 6b 65 70 74  zer must be kept
305d9 20 69 6e 20 73 79 6e 63 20 77 69 74 68 20 74 68   in sync with th
305da 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 2a  e SQLITE_LIMIT_*
305db 0a 2a 2a 20 23 64 65 66 69 6e 65 73 20 69 6e 20  .** #defines in 
305dc 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2f 0a 73 74  sqlite3.h..*/.st
305dd 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 61  atic const int a
305de 48 61 72 64 4c 69 6d 69 74 5b 5d 20 3d 20 7b 0a  HardLimit[] = {.
305df 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e    SQLITE_MAX_LEN
305e0 47 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  GTH,.  SQLITE_MA
305e1 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 2c 0a 20 20  X_SQL_LENGTH,.  
305e2 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
305e3 4e 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  N,.  SQLITE_MAX_
305e4 45 58 50 52 5f 44 45 50 54 48 2c 0a 20 20 53 51  EXPR_DEPTH,.  SQ
305e5 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e  LITE_MAX_COMPOUN
305e6 44 5f 53 45 4c 45 43 54 2c 0a 20 20 53 51 4c 49  D_SELECT,.  SQLI
305e7 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 2c 0a  TE_MAX_VDBE_OP,.
305e8 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e    SQLITE_MAX_FUN
305e9 43 54 49 4f 4e 5f 41 52 47 2c 0a 20 20 53 51 4c  CTION_ARG,.  SQL
305ea 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44  ITE_MAX_ATTACHED
305eb 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  ,.  SQLITE_MAX_L
305ec 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47  IKE_PATTERN_LENG
305ed 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58  TH,.  SQLITE_MAX
305ee 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
305ef 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  ,.};../*.** Make
305f0 20 73 75 72 65 20 74 68 65 20 68 61 72 64 20 6c   sure the hard l
305f1 69 6d 69 74 73 20 61 72 65 20 73 65 74 20 74 6f  imits are set to
305f2 20 72 65 61 73 6f 6e 61 62 6c 65 20 76 61 6c 75   reasonable valu
305f3 65 73 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  es.*/.#if SQLITE
305f4 5f 4d 41 58 5f 4c 45 4e 47 54 48 3c 31 30 30 0a  _MAX_LENGTH<100.
305f5 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
305f6 41 58 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62  AX_LENGTH must b
305f7 65 20 61 74 20 6c 65 61 73 74 20 31 30 30 0a 23  e at least 100.#
305f8 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
305f9 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c  _MAX_SQL_LENGTH<
305fa 31 30 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  100.# error SQLI
305fb 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54  TE_MAX_SQL_LENGT
305fc 48 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61  H must be at lea
305fd 73 74 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 69  st 100.#endif.#i
305fe 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c  f SQLITE_MAX_SQL
305ff 5f 4c 45 4e 47 54 48 3e 53 51 4c 49 54 45 5f 4d  _LENGTH>SQLITE_M
30600 41 58 5f 4c 45 4e 47 54 48 0a 23 20 65 72 72 6f  AX_LENGTH.# erro
30601 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c  r SQLITE_MAX_SQL
30602 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 6e 6f 74  _LENGTH must not
30603 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
30604 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
30605 54 48 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  TH.#endif.#if SQ
30606 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e  LITE_MAX_COMPOUN
30607 44 5f 53 45 4c 45 43 54 3c 32 0a 23 20 65 72 72  D_SELECT<2.# err
30608 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  or SQLITE_MAX_CO
30609 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 6d 75  MPOUND_SELECT mu
3060a 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 32  st be at least 2
3060b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
3060c 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 3c 34  TE_MAX_VDBE_OP<4
3060d 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  0.# error SQLITE
3060e 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20 6d 75 73  _MAX_VDBE_OP mus
3060f 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 30  t be at least 40
30610 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
30611 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
30612 41 52 47 3c 30 20 7c 7c 20 53 51 4c 49 54 45 5f  ARG<0 || SQLITE_
30613 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MAX_FUNCTION_ARG
30614 3e 31 30 30 30 0a 23 20 65 72 72 6f 72 20 53 51  >1000.# error SQ
30615 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f  LITE_MAX_FUNCTIO
30616 4e 5f 41 52 47 20 6d 75 73 74 20 62 65 20 62 65  N_ARG must be be
30617 74 77 65 65 6e 20 30 20 61 6e 64 20 31 30 30 30  tween 0 and 1000
30618 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
30619 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 3c  TE_MAX_ATTACHED<
3061a 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f  0 || SQLITE_MAX_
3061b 41 54 54 41 43 48 45 44 3e 33 30 0a 23 20 65 72  ATTACHED>30.# er
3061c 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41  ror SQLITE_MAX_A
3061d 54 54 41 43 48 45 44 20 6d 75 73 74 20 62 65 20  TTACHED must be 
3061e 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 33 30  between 0 and 30
3061f 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
30620 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54  TE_MAX_LIKE_PATT
30621 45 52 4e 5f 4c 45 4e 47 54 48 3c 31 0a 23 20 65  ERN_LENGTH<1.# e
30622 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
30623 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
30624 47 54 48 20 6d 75 73 74 20 62 65 20 61 74 20 6c  GTH must be at l
30625 65 61 73 74 20 31 0a 23 65 6e 64 69 66 0a 23 69  east 1.#endif.#i
30626 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52  f SQLITE_MAX_VAR
30627 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 31 0a 23  IABLE_NUMBER<1.#
30628 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
30629 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  X_VARIABLE_NUMBE
3062a 52 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61  R must be at lea
3062b 73 74 20 31 0a 23 65 6e 64 69 66 0a 23 69 66 20  st 1.#endif.#if 
3062c 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
3062d 4e 3e 33 32 37 36 37 0a 23 20 65 72 72 6f 72 20  N>32767.# error 
3062e 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
3062f 4e 20 6d 75 73 74 20 6e 6f 74 20 65 78 63 65 65  N must not excee
30630 64 20 33 32 37 36 37 0a 23 65 6e 64 69 66 0a 0a  d 32767.#endif..
30631 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
30632 65 20 76 61 6c 75 65 20 6f 66 20 61 20 6c 69 6d  e value of a lim
30633 69 74 2e 20 20 52 65 70 6f 72 74 20 74 68 65 20  it.  Report the 
30634 6f 6c 64 20 76 61 6c 75 65 2e 0a 2a 2a 20 49 66  old value..** If
30635 20 61 6e 20 69 6e 76 61 6c 69 64 20 6c 69 6d 69   an invalid limi
30636 74 20 69 6e 64 65 78 20 69 73 20 73 75 70 70 6c  t index is suppl
30637 69 65 64 2c 20 72 65 70 6f 72 74 20 2d 31 2e 0a  ied, report -1..
30638 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67  ** Make no chang
30639 65 73 20 62 75 74 20 73 74 69 6c 6c 20 72 65 70  es but still rep
3063a 6f 72 74 20 74 68 65 20 6f 6c 64 20 76 61 6c 75  ort the old valu
3063b 65 20 69 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20  e if the.** new 
3063c 6c 69 6d 69 74 20 69 73 20 6e 65 67 61 74 69 76  limit is negativ
3063d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 6c  e..**.** A new l
3063e 6f 77 65 72 20 6c 69 6d 69 74 20 64 6f 65 73 20  ower limit does 
3063f 6e 6f 74 20 73 68 72 69 6e 6b 20 65 78 69 73 74  not shrink exist
30640 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 73 2e 0a  ing constructs..
30641 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 70 72 65  ** It merely pre
30642 76 65 6e 74 73 20 6e 65 77 20 63 6f 6e 73 74 72  vents new constr
30643 75 63 74 73 20 74 68 61 74 20 65 78 63 65 65 64  ucts that exceed
30644 20 74 68 65 20 6c 69 6d 69 74 0a 2a 2a 20 66 72   the limit.** fr
30645 6f 6d 20 66 6f 72 6d 69 6e 67 2e 0a 2a 2f 0a 53  om forming..*/.S
30646 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
30647 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71 6c 69  lite3_limit(sqli
30648 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6c 69 6d  te3 *db, int lim
30649 69 74 49 64 2c 20 69 6e 74 20 6e 65 77 4c 69 6d  itId, int newLim
3064a 69 74 29 7b 0a 20 20 69 6e 74 20 6f 6c 64 4c 69  it){.  int oldLi
3064b 6d 69 74 3b 0a 20 20 69 66 28 20 6c 69 6d 69 74  mit;.  if( limit
3064c 49 64 3c 30 20 7c 7c 20 6c 69 6d 69 74 49 64 3e  Id<0 || limitId>
3064d 3d 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 20  =SQLITE_N_LIMIT 
3064e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31  ){.    return -1
3064f 3b 0a 20 20 7d 0a 20 20 6f 6c 64 4c 69 6d 69 74  ;.  }.  oldLimit
30650 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 6c 69   = db->aLimit[li
30651 6d 69 74 49 64 5d 3b 0a 20 20 69 66 28 20 6e 65  mitId];.  if( ne
30652 77 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a 20 20 20  wLimit>=0 ){.   
30653 20 69 66 28 20 6e 65 77 4c 69 6d 69 74 3e 61 48   if( newLimit>aH
30654 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64  ardLimit[limitId
30655 5d 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 69  ] ){.      newLi
30656 6d 69 74 20 3d 20 61 48 61 72 64 4c 69 6d 69 74  mit = aHardLimit
30657 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20 20 20 20 7d  [limitId];.    }
30658 0a 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b  .    db->aLimit[
30659 6c 69 6d 69 74 49 64 5d 20 3d 20 6e 65 77 4c 69  limitId] = newLi
3065a 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  mit;.  }.  retur
3065b 6e 20 6f 6c 64 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f  n oldLimit;.}../
3065c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
3065d 65 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20  e does the work 
3065e 6f 66 20 6f 70 65 6e 69 6e 67 20 61 20 64 61 74  of opening a dat
3065f 61 62 61 73 65 20 6f 6e 20 62 65 68 61 6c 66 20  abase on behalf 
30660 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70  of.** sqlite3_op
30661 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  en() and sqlite3
30662 5f 6f 70 65 6e 31 36 28 29 2e 20 54 68 65 20 64  _open16(). The d
30663 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
30664 20 22 7a 46 69 6c 65 6e 61 6d 65 22 20 20 0a 2a   "zFilename"  .*
30665 2a 20 69 73 20 55 54 46 2d 38 20 65 6e 63 6f 64  * is UTF-8 encod
30666 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
30667 74 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 0a  t openDatabase(.
30668 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
30669 69 6c 65 6e 61 6d 65 2c 20 2f 2a 20 44 61 74 61  ilename, /* Data
3066a 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 55 54  base filename UT
3066b 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20  F-8 encoded */. 
3066c 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c   sqlite3 **ppDb,
3066d 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3066e 52 65 74 75 72 6e 65 64 20 64 61 74 61 62 61 73  Returned databas
3066f 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 75 6e  e handle */.  un
30670 73 69 67 6e 65 64 20 66 6c 61 67 73 2c 20 20 20  signed flags,   
30671 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f       /* Operatio
30672 6e 61 6c 20 66 6c 61 67 73 20 2a 2f 0a 20 20 63  nal flags */.  c
30673 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20  onst char *zVfs 
30674 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
30675 20 74 68 65 20 56 46 53 20 74 6f 20 75 73 65 20   the VFS to use 
30676 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
30677 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  *db;.  int rc;. 
30678 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
30679 0a 20 20 69 6e 74 20 69 73 54 68 72 65 61 64 73  .  int isThreads
3067a 61 66 65 3b 0a 0a 20 20 2a 70 70 44 62 20 3d 20  afe;..  *ppDb = 
3067b 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  0;.#ifndef SQLIT
3067c 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
3067d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69    rc = sqlite3_i
3067e 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69  nitialize();.  i
3067f 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
30680 63 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28  c;.#endif..  if(
30681 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
30682 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 3d  nfig.bCoreMutex=
30683 3d 30 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65  =0 ){.    isThre
30684 61 64 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d 65  adsafe = 0;.  }e
30685 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
30686 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
30687 54 45 58 20 29 7b 0a 20 20 20 20 69 73 54 68 72  TEX ){.    isThr
30688 65 61 64 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d  eadsafe = 0;.  }
30689 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
3068a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c   SQLITE_OPEN_FUL
3068b 4c 4d 55 54 45 58 20 29 7b 0a 20 20 20 20 69 73  LMUTEX ){.    is
3068c 54 68 72 65 61 64 73 61 66 65 20 3d 20 31 3b 0a  Threadsafe = 1;.
3068d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 73 54    }else{.    isT
3068e 68 72 65 61 64 73 61 66 65 20 3d 20 73 71 6c 69  hreadsafe = sqli
3068f 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
30690 62 46 75 6c 6c 4d 75 74 65 78 3b 0a 20 20 7d 0a  bFullMutex;.  }.
30691 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 68 61 72  .  /* Remove har
30692 6d 66 75 6c 20 62 69 74 73 20 66 72 6f 6d 20 74  mful bits from t
30693 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74  he flags paramet
30694 65 72 20 2a 2f 0a 20 20 66 6c 61 67 73 20 26 3d  er */.  flags &=
30695 20 20 7e 28 20 53 51 4c 49 54 45 5f 4f 50 45 4e    ~( SQLITE_OPEN
30696 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c  _DELETEONCLOSE |
30697 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
30698 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
30699 5f 44 42 20 7c 0a 20 20 20 20 20 20 20 20 20 20  _DB |.          
3069a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
3069b 5f 54 45 4d 50 5f 44 42 20 7c 20 0a 20 20 20 20  _TEMP_DB | .    
3069c 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
3069d 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54  E_OPEN_TRANSIENT
3069e 5f 44 42 20 7c 20 0a 20 20 20 20 20 20 20 20 20  _DB | .         
3069f 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
306a0 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c  N_MAIN_JOURNAL |
306a1 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
306a2 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d   SQLITE_OPEN_TEM
306a3 50 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20  P_JOURNAL | .   
306a4 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
306a5 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e  TE_OPEN_SUBJOURN
306a6 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20  AL | .          
306a7 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
306a8 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
306a9 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
306aa 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d   SQLITE_OPEN_NOM
306ab 55 54 45 58 20 7c 0a 20 20 20 20 20 20 20 20 20  UTEX |.         
306ac 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
306ad 4e 5f 46 55 4c 4c 4d 55 54 45 58 0a 20 20 20 20  N_FULLMUTEX.    
306ae 20 20 20 20 20 20 20 20 20 29 3b 0a 0a 20 20 2f           );..  /
306af 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73  * Allocate the s
306b0 71 6c 69 74 65 20 64 61 74 61 20 73 74 72 75 63  qlite data struc
306b1 74 75 72 65 20 2a 2f 0a 20 20 64 62 20 3d 20 73  ture */.  db = s
306b2 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
306b3 28 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  ( sizeof(sqlite3
306b4 29 20 29 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30  ) );.  if( db==0
306b5 20 29 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f   ) goto opendb_o
306b6 75 74 3b 0a 20 20 69 66 28 20 69 73 54 68 72 65  ut;.  if( isThre
306b7 61 64 73 61 66 65 20 29 7b 0a 20 20 20 20 64 62  adsafe ){.    db
306b8 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  ->mutex = sqlite
306b9 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
306ba 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
306bb 56 45 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  VE);.    if( db-
306bc 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20  >mutex==0 ){.   
306bd 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
306be 64 62 29 3b 0a 20 20 20 20 20 20 64 62 20 3d 20  db);.      db = 
306bf 30 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70  0;.      goto op
306c0 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  endb_out;.    }.
306c1 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
306c2 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
306c3 74 65 78 29 3b 0a 20 20 64 62 2d 3e 65 72 72 4d  tex);.  db->errM
306c4 61 73 6b 20 3d 20 30 78 66 66 3b 0a 20 20 64 62  ask = 0xff;.  db
306c5 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 20  ->priorNewRowid 
306c6 3d 20 30 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d  = 0;.  db->nDb =
306c7 20 32 3b 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20   2;.  db->magic 
306c8 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  = SQLITE_MAGIC_B
306c9 55 53 59 3b 0a 20 20 64 62 2d 3e 61 44 62 20 3d  USY;.  db->aDb =
306ca 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b 0a   db->aDbStatic;.
306cb 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
306cc 66 28 64 62 2d 3e 61 4c 69 6d 69 74 29 3d 3d 73  f(db->aLimit)==s
306cd 69 7a 65 6f 66 28 61 48 61 72 64 4c 69 6d 69 74  izeof(aHardLimit
306ce 29 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28 64 62  ) );.  memcpy(db
306cf 2d 3e 61 4c 69 6d 69 74 2c 20 61 48 61 72 64 4c  ->aLimit, aHardL
306d0 69 6d 69 74 2c 20 73 69 7a 65 6f 66 28 64 62 2d  imit, sizeof(db-
306d1 3e 61 4c 69 6d 69 74 29 29 3b 0a 20 20 64 62 2d  >aLimit));.  db-
306d2 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
306d3 0a 20 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76  .  db->nextAutov
306d4 61 63 20 3d 20 2d 31 3b 0a 20 20 64 62 2d 3e 6e  ac = -1;.  db->n
306d5 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20 30 3b  extPagesize = 0;
306d6 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20  .  db->flags |= 
306d7 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
306d8 61 6d 65 73 0a 23 69 66 20 53 51 4c 49 54 45 5f  ames.#if SQLITE_
306d9 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52  DEFAULT_FILE_FOR
306da 4d 41 54 3c 34 0a 20 20 20 20 20 20 20 20 20 20  MAT<4.          
306db 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
306dc 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 0a 23 65  LegacyFileFmt.#e
306dd 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
306de 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45  TE_ENABLE_LOAD_E
306df 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 20 20 20  XTENSION.       
306e0 20 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49            | SQLI
306e1 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
306e2 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 3b 0a  .#endif.      ;.
306e3 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
306e4 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29  t(&db->aCollSeq)
306e5 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
306e6 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
306e7 4c 45 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68  LE.  sqlite3Hash
306e8 49 6e 69 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  Init(&db->aModul
306e9 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62  e);.#endif..  db
306ea 2d 3e 70 56 66 73 20 3d 20 73 71 6c 69 74 65 33  ->pVfs = sqlite3
306eb 5f 76 66 73 5f 66 69 6e 64 28 7a 56 66 73 29 3b  _vfs_find(zVfs);
306ec 0a 20 20 69 66 28 20 21 64 62 2d 3e 70 56 66 73  .  if( !db->pVfs
306ed 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
306ee 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73  ITE_ERROR;.    s
306ef 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
306f0 72 63 2c 20 22 6e 6f 20 73 75 63 68 20 76 66 73  rc, "no such vfs
306f1 3a 20 25 73 22 2c 20 7a 56 66 73 29 3b 0a 20 20  : %s", zVfs);.  
306f2 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
306f3 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64  t;.  }..  /* Add
306f4 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c   the default col
306f5 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
306f6 42 49 4e 41 52 59 2e 20 42 49 4e 41 52 59 20 77  BINARY. BINARY w
306f7 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20 55 54  orks for both UT
306f8 46 2d 38 0a 20 20 2a 2a 20 61 6e 64 20 55 54 46  F-8.  ** and UTF
306f9 2d 31 36 2c 20 73 6f 20 61 64 64 20 61 20 76 65  -16, so add a ve
306fa 72 73 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74  rsion for each t
306fb 6f 20 61 76 6f 69 64 20 61 6e 79 20 75 6e 6e 65  o avoid any unne
306fc 63 65 73 73 61 72 79 0a 20 20 2a 2a 20 63 6f 6e  cessary.  ** con
306fd 76 65 72 73 69 6f 6e 73 2e 20 54 68 65 20 6f 6e  versions. The on
306fe 6c 79 20 65 72 72 6f 72 20 74 68 61 74 20 63 61  ly error that ca
306ff 6e 20 6f 63 63 75 72 20 68 65 72 65 20 69 73 20  n occur here is 
30700 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75  a malloc() failu
30701 72 65 2e 0a 20 20 2a 2f 0a 20 20 63 72 65 61 74  re..  */.  creat
30702 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22  eCollation(db, "
30703 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45 5f  BINARY", SQLITE_
30704 55 54 46 38 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c  UTF8, 0, binColl
30705 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61  Func, 0);.  crea
30706 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20  teCollation(db, 
30707 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45  "BINARY", SQLITE
30708 5f 55 54 46 31 36 42 45 2c 20 30 2c 20 62 69 6e  _UTF16BE, 0, bin
30709 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20  CollFunc, 0);.  
3070a 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
3070b 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51  db, "BINARY", SQ
3070c 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 30 2c  LITE_UTF16LE, 0,
3070d 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29   binCollFunc, 0)
3070e 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  ;.  createCollat
3070f 69 6f 6e 28 64 62 2c 20 22 52 54 52 49 4d 22 2c  ion(db, "RTRIM",
30710 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 28 76   SQLITE_UTF8, (v
30711 6f 69 64 2a 29 31 2c 20 62 69 6e 43 6f 6c 6c 46  oid*)1, binCollF
30712 75 6e 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 64  unc, 0);.  if( d
30713 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
30714 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  ){.    goto open
30715 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 62  db_out;.  }.  db
30716 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71  ->pDfltColl = sq
30717 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
30718 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  (db, SQLITE_UTF8
30719 2c 20 22 42 49 4e 41 52 59 22 2c 20 36 2c 20 30  , "BINARY", 6, 0
3071a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
3071b 3e 70 44 66 6c 74 43 6f 6c 6c 21 3d 30 20 29 3b  >pDfltColl!=0 );
3071c 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 61 64 64 20  ..  /* Also add 
3071d 61 20 55 54 46 2d 38 20 63 61 73 65 2d 69 6e 73  a UTF-8 case-ins
3071e 65 6e 73 69 74 69 76 65 20 63 6f 6c 6c 61 74 69  ensitive collati
3071f 6f 6e 20 73 65 71 75 65 6e 63 65 2e 20 2a 2f 0a  on sequence. */.
30720 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f    createCollatio
30721 6e 28 64 62 2c 20 22 4e 4f 43 41 53 45 22 2c 20  n(db, "NOCASE", 
30722 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20  SQLITE_UTF8, 0, 
30723 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67 46  nocaseCollatingF
30724 75 6e 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 53  unc, 0);..  /* S
30725 65 74 20 66 6c 61 67 73 20 6f 6e 20 74 68 65 20  et flags on the 
30726 62 75 69 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74 69  built-in collati
30727 6e 67 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a  ng sequences */.
30728 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d    db->pDfltColl-
30729 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 43  >type = SQLITE_C
3072a 4f 4c 4c 5f 42 49 4e 41 52 59 3b 0a 20 20 70 43  OLL_BINARY;.  pC
3072b 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
3072c 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c  dCollSeq(db, SQL
3072d 49 54 45 5f 55 54 46 38 2c 20 22 4e 4f 43 41 53  ITE_UTF8, "NOCAS
3072e 45 22 2c 20 36 2c 20 30 29 3b 0a 20 20 69 66 28  E", 6, 0);.  if(
3072f 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43   pColl ){.    pC
30730 6f 6c 6c 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  oll->type = SQLI
30731 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 3b 0a  TE_COLL_NOCASE;.
30732 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74    }..  /* Open t
30733 68 65 20 62 61 63 6b 65 6e 64 20 64 61 74 61 62  he backend datab
30734 61 73 65 20 64 72 69 76 65 72 20 2a 2f 0a 20 20  ase driver */.  
30735 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20  db->openFlags = 
30736 66 6c 61 67 73 3b 0a 20 20 72 63 20 3d 20 73 71  flags;.  rc = sq
30737 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 6f 72  lite3BtreeFactor
30738 79 28 64 62 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  y(db, zFilename,
30739 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55   0, SQLITE_DEFAU
3073a 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 0a  LT_CACHE_SIZE, .
3073b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3073c 20 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 73             flags
3073d 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d   | SQLITE_OPEN_M
3073e 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20 20 20 20  AIN_DB,.        
3073f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30740 20 20 20 26 64 62 2d 3e 61 44 62 5b 30 5d 2e 70     &db->aDb[0].p
30741 42 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  Bt);.  if( rc!=S
30742 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
30743 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49  if( rc==SQLITE_I
30744 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  OERR_NOMEM ){.  
30745 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
30746 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
30747 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
30748 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 67 6f  , rc, 0);.    go
30749 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20  to opendb_out;. 
3074a 20 7d 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e   }.  db->aDb[0].
3074b 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65  pSchema = sqlite
3074c 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 64  3SchemaGet(db, d
3074d 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a  b->aDb[0].pBt);.
3074e 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63    db->aDb[1].pSc
3074f 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63  hema = sqlite3Sc
30750 68 65 6d 61 47 65 74 28 64 62 2c 20 30 29 3b 0a  hemaGet(db, 0);.
30751 0a 0a 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75  ..  /* The defau
30752 6c 74 20 73 61 66 65 74 79 5f 6c 65 76 65 6c 20  lt safety_level 
30753 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  for the main dat
30754 61 62 61 73 65 20 69 73 20 27 66 75 6c 6c 27 3b  abase is 'full';
30755 20 66 6f 72 20 74 68 65 20 74 65 6d 70 0a 20 20   for the temp.  
30756 2a 2a 20 64 61 74 61 62 61 73 65 20 69 74 20 69  ** database it i
30757 73 20 27 4e 4f 4e 45 27 2e 20 54 68 69 73 20 6d  s 'NONE'. This m
30758 61 74 63 68 65 73 20 74 68 65 20 70 61 67 65 72  atches the pager
30759 20 6c 61 79 65 72 20 64 65 66 61 75 6c 74 73 2e   layer defaults.
3075a 20 20 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 44    .  */.  db->aD
3075b 62 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 22 6d 61  b[0].zName = "ma
3075c 69 6e 22 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 30  in";.  db->aDb[0
3075d 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d  ].safety_level =
3075e 20 33 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   3;.#ifndef SQLI
3075f 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20  TE_OMIT_TEMPDB. 
30760 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d   db->aDb[1].zNam
30761 65 20 3d 20 22 74 65 6d 70 22 3b 0a 20 20 64 62  e = "temp";.  db
30762 2d 3e 61 44 62 5b 31 5d 2e 73 61 66 65 74 79 5f  ->aDb[1].safety_
30763 6c 65 76 65 6c 20 3d 20 31 3b 0a 23 65 6e 64 69  level = 1;.#endi
30764 66 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  f..  db->magic =
30765 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
30766 45 4e 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  EN;.  if( db->ma
30767 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
30768 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
30769 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67  t;.  }..  /* Reg
3076a 69 73 74 65 72 20 61 6c 6c 20 62 75 69 6c 74 2d  ister all built-
3076b 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62 75  in functions, bu
3076c 74 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74  t do not attempt
3076d 20 74 6f 20 72 65 61 64 20 74 68 65 0a 20 20 2a   to read the.  *
3076e 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  * database schem
3076f 61 20 79 65 74 2e 20 54 68 69 73 20 69 73 20 64  a yet. This is d
30770 65 6c 61 79 65 64 20 75 6e 74 69 6c 20 74 68 65  elayed until the
30771 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 65 20   first time the 
30772 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 69 73  database.  ** is
30773 20 61 63 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a   accessed..  */.
30774 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
30775 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29  b, SQLITE_OK, 0)
30776 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73  ;.  sqlite3Regis
30777 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69  terBuiltinFuncti
30778 6f 6e 73 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4c  ons(db);..  /* L
30779 6f 61 64 20 61 75 74 6f 6d 61 74 69 63 20 65 78  oad automatic ex
3077a 74 65 6e 73 69 6f 6e 73 20 2d 20 65 78 74 65 6e  tensions - exten
3077b 73 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20  sions that have 
3077c 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 0a  been registered.
3077d 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73    ** using the s
3077e 71 6c 69 74 65 33 5f 61 75 74 6f 6d 61 74 69 63  qlite3_automatic
3077f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 41 50 49  _extension() API
30780 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73  ..  */.  (void)s
30781 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78  qlite3AutoLoadEx
30782 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 20 20  tensions(db);.  
30783 69 66 28 20 73 71 6c 69 74 65 33 5f 65 72 72 63  if( sqlite3_errc
30784 6f 64 65 28 64 62 29 21 3d 53 51 4c 49 54 45 5f  ode(db)!=SQLITE_
30785 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f  OK ){.    goto o
30786 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  pendb_out;.  }..
30787 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
30788 41 42 4c 45 5f 46 54 53 31 0a 20 20 69 66 28 20  ABLE_FTS1.  if( 
30789 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
3078a 64 20 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20  d ){.    extern 
3078b 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 31 49  int sqlite3Fts1I
3078c 6e 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  nit(sqlite3*);. 
3078d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46     rc = sqlite3F
3078e 74 73 31 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d  ts1Init(db);.  }
3078f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
30790 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
30791 53 32 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  S2.  if( !db->ma
30792 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63  llocFailed && rc
30793 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
30794 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71     extern int sq
30795 6c 69 74 65 33 46 74 73 32 49 6e 69 74 28 73 71  lite3Fts2Init(sq
30796 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72 63 20  lite3*);.    rc 
30797 3d 20 73 71 6c 69 74 65 33 46 74 73 32 49 6e 69  = sqlite3Fts2Ini
30798 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
30799 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
3079a 5f 45 4e 41 42 4c 45 5f 46 54 53 33 0a 20 20 69  _ENABLE_FTS3.  i
3079b 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
3079c 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49  iled && rc==SQLI
3079d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
3079e 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  = sqlite3Fts3Ini
3079f 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
307a0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
307a1 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 20 20 69 66  _ENABLE_ICU.  if
307a2 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
307a3 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  led && rc==SQLIT
307a4 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
307a5 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28   sqlite3IcuInit(
307a6 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  db);.  }.#endif.
307a7 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
307a8 4e 41 42 4c 45 5f 52 54 52 45 45 0a 20 20 69 66  NABLE_RTREE.  if
307a9 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
307aa 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  led && rc==SQLIT
307ab 45 5f 4f 4b 29 7b 0a 20 20 20 20 72 63 20 3d 20  E_OK){.    rc = 
307ac 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
307ad 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  (db);.  }.#endif
307ae 0a 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  ..  sqlite3Error
307af 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20  (db, rc, 0);..  
307b0 2f 2a 20 2d 44 53 51 4c 49 54 45 5f 44 45 46 41  /* -DSQLITE_DEFA
307b1 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
307b2 3d 31 20 6d 61 6b 65 73 20 45 58 43 4c 55 53 49  =1 makes EXCLUSI
307b3 56 45 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c  VE the default l
307b4 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65  ocking.  ** mode
307b5 2e 20 20 2d 44 53 51 4c 49 54 45 5f 44 45 46 41  .  -DSQLITE_DEFA
307b6 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
307b7 3d 30 20 6d 61 6b 65 20 4e 4f 52 4d 41 4c 20 74  =0 make NORMAL t
307b8 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69  he default locki
307b9 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 44  ng.  ** mode.  D
307ba 6f 69 6e 67 20 6e 6f 74 68 69 6e 67 20 61 74 20  oing nothing at 
307bb 61 6c 6c 20 61 6c 73 6f 20 6d 61 6b 65 73 20 4e  all also makes N
307bc 4f 52 4d 41 4c 20 74 68 65 20 64 65 66 61 75 6c  ORMAL the defaul
307bd 74 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  t..  */.#ifdef S
307be 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f  QLITE_DEFAULT_LO
307bf 43 4b 49 4e 47 5f 4d 4f 44 45 0a 20 20 64 62 2d  CKING_MODE.  db-
307c0 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20  >dfltLockMode = 
307c1 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
307c2 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3b 0a 20 20 73  OCKING_MODE;.  s
307c3 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
307c4 6e 67 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74  ngMode(sqlite3Bt
307c5 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62  reePager(db->aDb
307c6 5b 30 5d 2e 70 42 74 29 2c 0a 20 20 20 20 20 20  [0].pBt),.      
307c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
307c8 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55      SQLITE_DEFAU
307c9 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 29  LT_LOCKING_MODE)
307ca 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45  ;.#endif..  /* E
307cb 6e 61 62 6c 65 20 74 68 65 20 6c 6f 6f 6b 61 73  nable the lookas
307cc 69 64 65 2d 6d 61 6c 6c 6f 63 20 73 75 62 73 79  ide-malloc subsy
307cd 73 74 65 6d 20 2a 2f 0a 20 20 73 65 74 75 70 4c  stem */.  setupL
307ce 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 30 2c 20  ookaside(db, 0, 
307cf 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
307d0 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 2c  fig.szLookaside,
307d1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
307d2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
307d3 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f  GlobalConfig.nLo
307d4 6f 6b 61 73 69 64 65 29 3b 0a 0a 6f 70 65 6e 64  okaside);..opend
307d5 62 5f 6f 75 74 3a 0a 20 20 69 66 28 20 64 62 20  b_out:.  if( db 
307d6 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  ){.    assert( d
307d7 62 2d 3e 6d 75 74 65 78 21 3d 30 20 7c 7c 20 69  b->mutex!=0 || i
307d8 73 54 68 72 65 61 64 73 61 66 65 3d 3d 30 20 7c  sThreadsafe==0 |
307d9 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  | sqlite3GlobalC
307da 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78  onfig.bFullMutex
307db 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==0 );.    sqlit
307dc 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
307dd 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20  b->mutex);.  }. 
307de 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 72   rc = sqlite3_er
307df 72 63 6f 64 65 28 64 62 29 3b 0a 20 20 69 66 28  rcode(db);.  if(
307e0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
307e1 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  M ){.    sqlite3
307e2 5f 63 6c 6f 73 65 28 64 62 29 3b 0a 20 20 20 20  _close(db);.    
307e3 64 62 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  db = 0;.  }else 
307e4 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
307e5 4b 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67  K ){.    db->mag
307e6 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49  ic = SQLITE_MAGI
307e7 43 5f 53 49 43 4b 3b 0a 20 20 7d 0a 20 20 2a 70  C_SICK;.  }.  *p
307e8 70 44 62 20 3d 20 64 62 3b 0a 20 20 72 65 74 75  pDb = db;.  retu
307e9 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  rn sqlite3ApiExi
307ea 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a  t(0, rc);.}../*.
307eb 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61  ** Open a new da
307ec 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a  tabase handle..*
307ed 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
307ee 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20   sqlite3_open(. 
307ef 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
307f0 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c 69 74  lename, .  sqlit
307f1 65 33 20 2a 2a 70 70 44 62 20 0a 29 7b 0a 20 20  e3 **ppDb .){.  
307f2 72 65 74 75 72 6e 20 6f 70 65 6e 44 61 74 61 62  return openDatab
307f3 61 73 65 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70  ase(zFilename, p
307f4 70 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  pDb,.           
307f5 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
307f6 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
307f7 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   | SQLITE_OPEN_C
307f8 52 45 41 54 45 2c 20 30 29 3b 0a 7d 0a 53 51 4c  REATE, 0);.}.SQL
307f9 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
307fa 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63  te3_open_v2(.  c
307fb 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e  onst char *filen
307fc 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61  ame,   /* Databa
307fd 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46  se filename (UTF
307fe 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -8) */.  sqlite3
307ff 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20   **ppDb,        
30800 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20   /* OUT: SQLite 
30801 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69  db handle */.  i
30802 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
30803 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
30804 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
30805 2a 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20  *zVfs        /* 
30806 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75  Name of VFS modu
30807 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a  le to use */.){.
30808 20 20 72 65 74 75 72 6e 20 6f 70 65 6e 44 61 74    return openDat
30809 61 62 61 73 65 28 66 69 6c 65 6e 61 6d 65 2c 20  abase(filename, 
3080a 70 70 44 62 2c 20 66 6c 61 67 73 2c 20 7a 56 66  ppDb, flags, zVf
3080b 73 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  s);.}..#ifndef S
3080c 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
3080d 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65  ./*.** Open a ne
3080e 77 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  w database handl
3080f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
30810 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
30811 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69  n16(.  const voi
30812 64 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20  d *zFilename, . 
30813 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 0a   sqlite3 **ppDb.
30814 29 7b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  ){.  char const 
30815 2a 7a 46 69 6c 65 6e 61 6d 65 38 3b 20 20 20 2f  *zFilename8;   /
30816 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 6e 63 6f  * zFilename enco
30817 64 65 64 20 69 6e 20 55 54 46 2d 38 20 69 6e 73  ded in UTF-8 ins
30818 74 65 61 64 20 6f 66 20 55 54 46 2d 31 36 20 2a  tead of UTF-16 *
30819 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  /.  sqlite3_valu
3081a 65 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 72  e *pVal;.  int r
3081b 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 46  c;..  assert( zF
3081c 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 61 73 73  ilename );.  ass
3081d 65 72 74 28 20 70 70 44 62 20 29 3b 0a 20 20 2a  ert( ppDb );.  *
3081e 70 70 44 62 20 3d 20 30 3b 0a 23 69 66 6e 64 65  ppDb = 0;.#ifnde
3081f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
30820 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71  TOINIT.  rc = sq
30821 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
30822 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  ();.  if( rc ) r
30823 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66  eturn rc;.#endif
30824 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65  .  pVal = sqlite
30825 33 56 61 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20  3ValueNew(0);.  
30826 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
30827 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 46 69  tr(pVal, -1, zFi
30828 6c 65 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 55  lename, SQLITE_U
30829 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49  TF16NATIVE, SQLI
3082a 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7a 46  TE_STATIC);.  zF
3082b 69 6c 65 6e 61 6d 65 38 20 3d 20 73 71 6c 69 74  ilename8 = sqlit
3082c 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c  e3ValueText(pVal
3082d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
3082e 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 38    if( zFilename8
3082f 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65   ){.    rc = ope
30830 6e 44 61 74 61 62 61 73 65 28 7a 46 69 6c 65 6e  nDatabase(zFilen
30831 61 6d 65 38 2c 20 70 70 44 62 2c 0a 20 20 20 20  ame8, ppDb,.    
30832 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30833 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45    SQLITE_OPEN_RE
30834 41 44 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45  ADWRITE | SQLITE
30835 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2c 20 30 29  _OPEN_CREATE, 0)
30836 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70  ;.    assert( *p
30837 70 44 62 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  pDb || rc==SQLIT
30838 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 69  E_NOMEM );.    i
30839 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3083a 20 26 26 20 21 44 62 48 61 73 50 72 6f 70 65 72   && !DbHasProper
3083b 74 79 28 2a 70 70 44 62 2c 20 30 2c 20 44 42 5f  ty(*ppDb, 0, DB_
3083c 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b  SchemaLoaded) ){
3083d 0a 20 20 20 20 20 20 45 4e 43 28 2a 70 70 44 62  .      ENC(*ppDb
3083e 29 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ) = SQLITE_UTF16
3083f 4e 41 54 49 56 45 3b 0a 20 20 20 20 7d 0a 20 20  NATIVE;.    }.  
30840 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
30841 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
30842 7d 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65  }.  sqlite3Value
30843 46 72 65 65 28 70 56 61 6c 29 3b 0a 0a 20 20 72  Free(pVal);..  r
30844 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69  eturn sqlite3Api
30845 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 23  Exit(0, rc);.}.#
30846 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
30847 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f  OMIT_UTF16 */../
30848 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
30849 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  new collation se
3084a 71 75 65 6e 63 65 20 77 69 74 68 20 74 68 65 20  quence with the 
3084b 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
3084c 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  db..*/.SQLITE_AP
3084d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
3084e 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a  eate_collation(.
3084f 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a    sqlite3* db, .
30850 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
30851 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63 2c  ame, .  int enc,
30852 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a   .  void* pCtx,.
30853 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29    int(*xCompare)
30854 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
30855 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
30856 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74   void*).){.  int
30857 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d   rc;.  sqlite3_m
30858 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
30859 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28  utex);.  assert(
3085a 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
3085b 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 63 72 65  ed );.  rc = cre
3085c 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c  ateCollation(db,
3085d 20 7a 4e 61 6d 65 2c 20 65 6e 63 2c 20 70 43 74   zName, enc, pCt
3085e 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30 29 3b  x, xCompare, 0);
3085f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
30860 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
30861 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
30862 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
30863 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
30864 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
30865 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e   a new collation
30866 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20 74   sequence with t
30867 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
30868 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45  le db..*/.SQLITE
30869 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3086a 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
3086b 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 2a  n_v2(.  sqlite3*
3086c 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68   db, .  const ch
3086d 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e  ar *zName, .  in
3086e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20  t enc, .  void* 
3086f 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f  pCtx,.  int(*xCo
30870 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74  mpare)(void*,int
30871 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
30872 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20  ,const void*),. 
30873 20 76 6f 69 64 28 2a 78 44 65 6c 29 28 76 6f 69   void(*xDel)(voi
30874 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  d*).){.  int rc;
30875 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
30876 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
30877 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  );.  assert( !db
30878 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
30879 3b 0a 20 20 72 63 20 3d 20 63 72 65 61 74 65 43  ;.  rc = createC
3087a 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61  ollation(db, zNa
3087b 6d 65 2c 20 65 6e 63 2c 20 70 43 74 78 2c 20 78  me, enc, pCtx, x
3087c 43 6f 6d 70 61 72 65 2c 20 78 44 65 6c 29 3b 0a  Compare, xDel);.
3087d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
3087e 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
3087f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
30880 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
30881 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
30882 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
30883 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
30884 20 52 65 67 69 73 74 65 72 20 61 20 6e 65 77 20   Register a new 
30885 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
30886 63 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ce with the data
30887 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a  base handle db..
30888 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
30889 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
3088a 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20  _collation16(.  
3088b 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20  sqlite3* db, .  
3088c 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d  const void *zNam
3088d 65 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20  e,.  int enc, . 
3088e 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69   void* pCtx,.  i
3088f 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
30890 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
30891 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
30892 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63  id*).){.  int rc
30893 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
30894 63 68 61 72 20 2a 7a 4e 61 6d 65 38 3b 0a 20 20  char *zName8;.  
30895 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
30896 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
30897 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
30898 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
30899 20 7a 4e 61 6d 65 38 20 3d 20 73 71 6c 69 74 65   zName8 = sqlite
3089a 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 4e  3Utf16to8(db, zN
3089b 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 69 66 28 20  ame, -1);.  if( 
3089c 7a 4e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72 63  zName8 ){.    rc
3089d 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69   = createCollati
3089e 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38 2c 20 65  on(db, zName8, e
3089f 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70 61  nc, pCtx, xCompa
308a0 72 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  re, 0);.    sqli
308a1 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
308a2 61 6d 65 38 29 3b 0a 20 20 7d 0a 20 20 72 63 20  ame8);.  }.  rc 
308a3 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
308a4 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
308a5 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
308a6 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
308a7 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
308a8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
308a9 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _UTF16 */../*.**
308aa 20 52 65 67 69 73 74 65 72 20 61 20 63 6f 6c 6c   Register a coll
308ab 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
308ac 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20  actory callback 
308ad 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
308ae 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20  e handle.** db. 
308af 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72 65 76  Replace any prev
308b0 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64  iously installed
308b1 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
308b2 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a  nce factory..*/.
308b3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
308b4 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
308b5 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74  _needed(.  sqlit
308b6 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20  e3 *db, .  void 
308b7 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c  *pCollNeededArg,
308b8 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e   .  void(*xCollN
308b9 65 65 64 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c  eeded)(void*,sql
308ba 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52  ite3*,int eTextR
308bb 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a  ep,const char*).
308bc 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
308bd 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
308be 65 78 29 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c  ex);.  db->xColl
308bf 4e 65 65 64 65 64 20 3d 20 78 43 6f 6c 6c 4e 65  Needed = xCollNe
308c0 65 64 65 64 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c  eded;.  db->xCol
308c1 6c 4e 65 65 64 65 64 31 36 20 3d 20 30 3b 0a 20  lNeeded16 = 0;. 
308c2 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64   db->pCollNeeded
308c3 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65 64 65  Arg = pCollNeede
308c4 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  dArg;.  sqlite3_
308c5 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
308c6 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
308c7 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
308c8 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
308c9 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52  IT_UTF16./*.** R
308ca 65 67 69 73 74 65 72 20 61 20 63 6f 6c 6c 61 74  egister a collat
308cb 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63  ion sequence fac
308cc 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20 77 69  tory callback wi
308cd 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
308ce 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65  handle.** db. Re
308cf 70 6c 61 63 65 20 61 6e 79 20 70 72 65 76 69 6f  place any previo
308d0 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64 20 63  usly installed c
308d1 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
308d2 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51  e factory..*/.SQ
308d3 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
308d4 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
308d5 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c 69 74  eeded16(.  sqlit
308d6 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20  e3 *db, .  void 
308d7 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c  *pCollNeededArg,
308d8 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e   .  void(*xCollN
308d9 65 65 64 65 64 31 36 29 28 76 6f 69 64 2a 2c 73  eeded16)(void*,s
308da 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78  qlite3*,int eTex
308db 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  tRep,const void*
308dc 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ).){.  sqlite3_m
308dd 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
308de 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78 43 6f  utex);.  db->xCo
308df 6c 6c 4e 65 65 64 65 64 20 3d 20 30 3b 0a 20 20  llNeeded = 0;.  
308e0 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31  db->xCollNeeded1
308e1 36 20 3d 20 78 43 6f 6c 6c 4e 65 65 64 65 64 31  6 = xCollNeeded1
308e2 36 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65  6;.  db->pCollNe
308e3 65 64 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e  ededArg = pCollN
308e4 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69  eededArg;.  sqli
308e5 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
308e6 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
308e7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
308e8 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
308e9 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
308ea 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
308eb 5f 4f 4d 49 54 5f 47 4c 4f 42 41 4c 52 45 43 4f  _OMIT_GLOBALRECO
308ec 56 45 52 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  VER.#ifndef SQLI
308ed 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
308ee 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ED./*.** This fu
308ef 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 77 20 61 6e  nction is now an
308f0 20 61 6e 61 63 68 72 6f 6e 69 73 6d 2e 20 49 74   anachronism. It
308f1 20 75 73 65 64 20 74 6f 20 62 65 20 75 73 65 64   used to be used
308f2 20 74 6f 20 72 65 63 6f 76 65 72 20 66 72 6f 6d   to recover from
308f3 20 61 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66   a.** malloc() f
308f4 61 69 6c 75 72 65 2c 20 62 75 74 20 53 51 4c 69  ailure, but SQLi
308f5 74 65 20 6e 6f 77 20 64 6f 65 73 20 74 68 69 73  te now does this
308f6 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a   automatically..
308f7 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
308f8 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c  t sqlite3_global
308f9 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29 7b 0a  _recover(void){.
308fa 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
308fb 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 23 65 6e  OK;.}.#endif.#en
308fc 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20  dif../*.** Test 
308fd 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 20 6f  to see whether o
308fe 72 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61  r not the databa
308ff 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
30900 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 0a 2a   in autocommit.*
30901 2a 20 6d 6f 64 65 2e 20 20 52 65 74 75 72 6e 20  * mode.  Return 
30902 54 52 55 45 20 69 66 20 69 74 20 69 73 20 61 6e  TRUE if it is an
30903 64 20 46 41 4c 53 45 20 69 66 20 6e 6f 74 2e 20  d FALSE if not. 
30904 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65   Autocommit mode
30905 20 69 73 20 6f 6e 0a 2a 2a 20 62 79 20 64 65 66   is on.** by def
30906 61 75 6c 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69  ault.  Autocommi
30907 74 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79  t is disabled by
30908 20 61 20 42 45 47 49 4e 20 73 74 61 74 65 6d 65   a BEGIN stateme
30909 6e 74 20 61 6e 64 20 72 65 65 6e 61 62 6c 65 64  nt and reenabled
3090a 0a 2a 2a 20 62 79 20 74 68 65 20 6e 65 78 74 20  .** by the next 
3090b 43 4f 4d 4d 49 54 20 6f 72 20 52 4f 4c 4c 42 41  COMMIT or ROLLBA
3090c 43 4b 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54  CK..**.******* T
3090d 48 49 53 20 49 53 20 41 4e 20 45 58 50 45 52 49  HIS IS AN EXPERI
3090e 4d 45 4e 54 41 4c 20 41 50 49 20 41 4e 44 20 49  MENTAL API AND I
3090f 53 20 53 55 42 4a 45 43 54 20 54 4f 20 43 48 41  S SUBJECT TO CHA
30910 4e 47 45 20 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 53 51  NGE ******.*/.SQ
30911 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
30912 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d  ite3_get_autocom
30913 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  mit(sqlite3 *db)
30914 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 61  {.  return db->a
30915 75 74 6f 43 6f 6d 6d 69 74 3b 0a 7d 0a 0a 23 69  utoCommit;.}..#i
30916 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
30917 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  G./*.** The foll
30918 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73  owing routine is
30919 20 73 75 62 74 69 74 75 74 65 64 20 66 6f 72 20   subtituted for 
3091a 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f  constant SQLITE_
3091b 43 4f 52 52 55 50 54 20 69 6e 0a 2a 2a 20 64 65  CORRUPT in.** de
3091c 62 75 67 67 69 6e 67 20 62 75 69 6c 64 73 2e 20  bugging builds. 
3091d 20 54 68 69 73 20 70 72 6f 76 69 64 65 73 20 61   This provides a
3091e 20 77 61 79 20 74 6f 20 73 65 74 20 61 20 62 72   way to set a br
3091f 65 61 6b 70 6f 69 6e 74 20 66 6f 72 20 77 68 65  eakpoint for whe
30920 6e 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20  n.** corruption 
30921 69 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65  is first detecte
30922 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
30923 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
30924 43 6f 72 72 75 70 74 28 76 6f 69 64 29 7b 0a 20  Corrupt(void){. 
30925 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
30926 4f 52 52 55 50 54 3b 0a 7d 0a 23 65 6e 64 69 66  ORRUPT;.}.#endif
30927 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
30928 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
30929 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  ./*.** This is a
3092a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 72 6f 75   convenience rou
3092b 74 69 6e 65 20 74 68 61 74 20 6d 61 6b 65 73 20  tine that makes 
3092c 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 74 68  sure that all th
3092d 72 65 61 64 2d 73 70 65 63 69 66 69 63 0a 2a 2a  read-specific.**
3092e 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 74   data for this t
3092f 68 72 65 61 64 20 68 61 73 20 62 65 65 6e 20 64  hread has been d
30930 65 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a  eallocated..**.*
30931 2a 20 53 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67  * SQLite no long
30932 65 72 20 75 73 65 73 20 74 68 72 65 61 64 2d 73  er uses thread-s
30933 70 65 63 69 66 69 63 20 64 61 74 61 20 73 6f 20  pecific data so 
30934 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
30935 6e 6f 77 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20  now a.** no-op. 
30936 20 49 74 20 69 73 20 72 65 74 61 69 6e 65 64 20   It is retained 
30937 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 63  for historical c
30938 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2f  ompatibility..*/
30939 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3093a 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f   sqlite3_thread_
3093b 63 6c 65 61 6e 75 70 28 76 6f 69 64 29 7b 0a 7d  cleanup(void){.}
3093c 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
3093d 65 74 75 72 6e 20 6d 65 74 61 20 69 6e 66 6f 72  eturn meta infor
3093e 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 20 73  mation about a s
3093f 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 20 6f  pecific column o
30940 66 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62  f a database tab
30941 6c 65 2e 0a 2a 2a 20 53 65 65 20 63 6f 6d 6d 65  le..** See comme
30942 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 2e 68 20  nt in sqlite3.h 
30943 28 73 71 6c 69 74 65 2e 68 2e 69 6e 29 20 66 6f  (sqlite.h.in) fo
30944 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69  r details..*/.#i
30945 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
30946 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
30947 54 41 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  TA.SQLITE_API in
30948 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f  t sqlite3_table_
30949 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 28  column_metadata(
3094a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
3094b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3094c 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  * Connection han
3094d 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
3094e 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 20  har *zDbName,   
3094f 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
30950 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f   name or NULL */
30951 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
30952 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f  TableName,     /
30953 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  * Table name */.
30954 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
30955 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a  olumnName,    /*
30956 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a   Column name */.
30957 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70    char const **p
30958 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 2f 2a  zDataType,    /*
30959 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 72 65   OUTPUT: Declare
3095a 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f 0a 20  d data type */. 
3095b 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a   char const **pz
3095c 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a 20  CollSeq,     /* 
3095d 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69 6f  OUTPUT: Collatio
3095e 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20  n sequence name 
3095f 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e 75  */.  int *pNotNu
30960 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ll,             
30961 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
30962 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e   if NOT NULL con
30963 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 20 2a  straint exists *
30964 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d 61 72  /.  int *pPrimar
30965 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20  yKey,           
30966 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20  /* OUTPUT: True 
30967 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f  if column part o
30968 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  f PK */.  int *p
30969 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 20 20  Autoinc         
3096a 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a        /* OUTPUT:
3096b 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20   True if column 
3096c 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e  is auto-incremen
3096d 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  t */.){.  int rc
3096e 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  ;.  char *zErrMs
3096f 67 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a  g = 0;.  Table *
30970 70 54 61 62 20 3d 20 30 3b 0a 20 20 43 6f 6c 75  pTab = 0;.  Colu
30971 6d 6e 20 2a 70 43 6f 6c 20 3d 20 30 3b 0a 20 20  mn *pCol = 0;.  
30972 69 6e 74 20 69 43 6f 6c 3b 0a 0a 20 20 63 68 61  int iCol;..  cha
30973 72 20 63 6f 6e 73 74 20 2a 7a 44 61 74 61 54 79  r const *zDataTy
30974 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63  pe = 0;.  char c
30975 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 53 65 71 20 3d  onst *zCollSeq =
30976 20 30 3b 0a 20 20 69 6e 74 20 6e 6f 74 6e 75 6c   0;.  int notnul
30977 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 70 72 69  l = 0;.  int pri
30978 6d 61 72 79 6b 65 79 20 3d 20 30 3b 0a 20 20 69  marykey = 0;.  i
30979 6e 74 20 61 75 74 6f 69 6e 63 20 3d 20 30 3b 0a  nt autoinc = 0;.
3097a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65  .  /* Ensure the
3097b 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
3097c 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 64   has been loaded
3097d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
3097e 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
3097f 74 65 78 29 3b 0a 20 20 28 76 6f 69 64 29 73 71  tex);.  (void)sq
30980 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
30981 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
30982 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20  eEnterAll(db);. 
30983 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69   rc = sqlite3Ini
30984 74 28 64 62 2c 20 26 7a 45 72 72 4d 73 67 29 3b  t(db, &zErrMsg);
30985 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
30986 21 3d 72 63 20 29 7b 0a 20 20 20 20 67 6f 74 6f  !=rc ){.    goto
30987 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a   error_out;.  }.
30988 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65  .  /* Locate the
30989 20 74 61 62 6c 65 20 69 6e 20 71 75 65 73 74 69   table in questi
3098a 6f 6e 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73  on */.  pTab = s
3098b 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
3098c 64 62 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20  db, zTableName, 
3098d 7a 44 62 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  zDbName);.  if( 
3098e 21 70 54 61 62 20 7c 7c 20 70 54 61 62 2d 3e 70  !pTab || pTab->p
3098f 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 70 54  Select ){.    pT
30990 61 62 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f  ab = 0;.    goto
30991 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a   error_out;.  }.
30992 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 63  .  /* Find the c
30993 6f 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20  olumn for which 
30994 69 6e 66 6f 20 69 73 20 72 65 71 75 65 73 74 65  info is requeste
30995 64 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  d */.  if( sqlit
30996 65 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 75 6d  e3IsRowid(zColum
30997 6e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 69 43  nName) ){.    iC
30998 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  ol = pTab->iPKey
30999 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d  ;.    if( iCol>=
3099a 30 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20  0 ){.      pCol 
3099b 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43  = &pTab->aCol[iC
3099c 6f 6c 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ol];.    }.  }el
3099d 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c  se{.    for(iCol
3099e 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e  =0; iCol<pTab->n
3099f 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20  Col; iCol++){.  
309a0 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62      pCol = &pTab
309a1 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20  ->aCol[iCol];.  
309a2 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74      if( 0==sqlit
309a3 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e  e3StrICmp(pCol->
309a4 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 4e 61  zName, zColumnNa
309a5 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  me) ){.        b
309a6 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
309a7 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c    }.    if( iCol
309a8 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a  ==pTab->nCol ){.
309a9 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a        pTab = 0;.
309aa 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72        goto error
309ab 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  _out;.    }.  }.
309ac 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
309ad 69 6e 67 20 62 6c 6f 63 6b 20 73 74 6f 72 65 73  ing block stores
309ae 20 74 68 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d   the meta inform
309af 61 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20  ation that will 
309b0 62 65 20 72 65 74 75 72 6e 65 64 0a 20 20 2a 2a  be returned.  **
309b1 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69   to the caller i
309b2 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  n local variable
309b3 73 20 7a 44 61 74 61 54 79 70 65 2c 20 7a 43 6f  s zDataType, zCo
309b4 6c 6c 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c 20  llSeq, notnull, 
309b5 70 72 69 6d 61 72 79 6b 65 79 0a 20 20 2a 2a 20  primarykey.  ** 
309b6 61 6e 64 20 61 75 74 6f 69 6e 63 2e 20 41 74 20  and autoinc. At 
309b7 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65  this point there
309b8 20 61 72 65 20 74 77 6f 20 70 6f 73 73 69 62 69   are two possibi
309b9 6c 69 74 69 65 73 3a 0a 20 20 2a 2a 20 0a 20 20  lities:.  ** .  
309ba 2a 2a 20 20 20 20 20 31 2e 20 54 68 65 20 73 70  **     1. The sp
309bb 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 6e  ecified column n
309bc 61 6d 65 20 77 61 73 20 72 6f 77 69 64 22 2c 20  ame was rowid", 
309bd 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64  "oid" or "_rowid
309be 5f 22 20 0a 20 20 2a 2a 20 20 20 20 20 20 20 20  _" .  **        
309bf 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20  and there is no 
309c0 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61  explicitly decla
309c1 72 65 64 20 49 50 4b 20 63 6f 6c 75 6d 6e 2e 20  red IPK column. 
309c2 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 32  .  **.  **     2
309c3 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6e  . The table is n
309c4 6f 74 20 61 20 76 69 65 77 20 61 6e 64 20 74 68  ot a view and th
309c5 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 64  e column name id
309c6 65 6e 74 69 66 69 65 64 20 61 6e 20 0a 20 20 2a  entified an .  *
309c7 2a 20 20 20 20 20 20 20 20 65 78 70 6c 69 63 69  *        explici
309c8 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c  tly declared col
309c9 75 6d 6e 2e 20 43 6f 70 79 20 6d 65 74 61 20 69  umn. Copy meta i
309ca 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
309cb 2a 70 43 6f 6c 2e 0a 20 20 2a 2f 20 0a 20 20 69  *pCol..  */ .  i
309cc 66 28 20 70 43 6f 6c 20 29 7b 0a 20 20 20 20 7a  f( pCol ){.    z
309cd 44 61 74 61 54 79 70 65 20 3d 20 70 43 6f 6c 2d  DataType = pCol-
309ce 3e 7a 54 79 70 65 3b 0a 20 20 20 20 7a 43 6f 6c  >zType;.    zCol
309cf 6c 53 65 71 20 3d 20 70 43 6f 6c 2d 3e 7a 43 6f  lSeq = pCol->zCo
309d0 6c 6c 3b 0a 20 20 20 20 6e 6f 74 6e 75 6c 6c 20  ll;.    notnull 
309d1 3d 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 21  = pCol->notNull!
309d2 3d 30 3b 0a 20 20 20 20 70 72 69 6d 61 72 79 6b  =0;.    primaryk
309d3 65 79 20 20 3d 20 70 43 6f 6c 2d 3e 69 73 50 72  ey  = pCol->isPr
309d4 69 6d 4b 65 79 21 3d 30 3b 0a 20 20 20 20 61 75  imKey!=0;.    au
309d5 74 6f 69 6e 63 20 3d 20 70 54 61 62 2d 3e 69 50  toinc = pTab->iP
309d6 4b 65 79 3d 3d 69 43 6f 6c 20 26 26 20 28 70 54  Key==iCol && (pT
309d7 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
309d8 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29  F_Autoincrement)
309d9 21 3d 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  !=0;.  }else{.  
309da 20 20 7a 44 61 74 61 54 79 70 65 20 3d 20 22 49    zDataType = "I
309db 4e 54 45 47 45 52 22 3b 0a 20 20 20 20 70 72 69  NTEGER";.    pri
309dc 6d 61 72 79 6b 65 79 20 3d 20 31 3b 0a 20 20 7d  marykey = 1;.  }
309dd 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 53 65 71  .  if( !zCollSeq
309de 20 29 7b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71   ){.    zCollSeq
309df 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 7d   = "BINARY";.  }
309e0 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 73  ..error_out:.  s
309e1 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
309e2 41 6c 6c 28 64 62 29 3b 0a 20 20 28 76 6f 69 64  All(db);.  (void
309e3 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
309e4 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 57 68 65  f(db);..  /* Whe
309e5 74 68 65 72 20 74 68 65 20 66 75 6e 63 74 69 6f  ther the functio
309e6 6e 20 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64  n call succeeded
309e7 20 6f 72 20 66 61 69 6c 65 64 2c 20 73 65 74 20   or failed, set 
309e8 74 68 65 20 6f 75 74 70 75 74 20 70 61 72 61 6d  the output param
309e9 65 74 65 72 73 0a 20 20 2a 2a 20 74 6f 20 77 68  eters.  ** to wh
309ea 61 74 65 76 65 72 20 74 68 65 69 72 20 6c 6f 63  atever their loc
309eb 61 6c 20 63 6f 75 6e 74 65 72 70 61 72 74 73 20  al counterparts 
309ec 63 6f 6e 74 61 69 6e 2e 20 49 66 20 61 6e 20 65  contain. If an e
309ed 72 72 6f 72 20 64 69 64 20 6f 63 63 75 72 2c 0a  rror did occur,.
309ee 20 20 2a 2a 20 74 68 69 73 20 68 61 73 20 74 68    ** this has th
309ef 65 20 65 66 66 65 63 74 20 6f 66 20 7a 65 72 6f  e effect of zero
309f0 69 6e 67 20 61 6c 6c 20 6f 75 74 70 75 74 20 70  ing all output p
309f1 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a  arameters..  */.
309f2 20 20 69 66 28 20 70 7a 44 61 74 61 54 79 70 65    if( pzDataType
309f3 20 29 20 2a 70 7a 44 61 74 61 54 79 70 65 20 3d   ) *pzDataType =
309f4 20 7a 44 61 74 61 54 79 70 65 3b 0a 20 20 69 66   zDataType;.  if
309f5 28 20 70 7a 43 6f 6c 6c 53 65 71 20 29 20 2a 70  ( pzCollSeq ) *p
309f6 7a 43 6f 6c 6c 53 65 71 20 3d 20 7a 43 6f 6c 6c  zCollSeq = zColl
309f7 53 65 71 3b 0a 20 20 69 66 28 20 70 4e 6f 74 4e  Seq;.  if( pNotN
309f8 75 6c 6c 20 29 20 2a 70 4e 6f 74 4e 75 6c 6c 20  ull ) *pNotNull 
309f9 3d 20 6e 6f 74 6e 75 6c 6c 3b 0a 20 20 69 66 28  = notnull;.  if(
309fa 20 70 50 72 69 6d 61 72 79 4b 65 79 20 29 20 2a   pPrimaryKey ) *
309fb 70 50 72 69 6d 61 72 79 4b 65 79 20 3d 20 70 72  pPrimaryKey = pr
309fc 69 6d 61 72 79 6b 65 79 3b 0a 20 20 69 66 28 20  imarykey;.  if( 
309fd 70 41 75 74 6f 69 6e 63 20 29 20 2a 70 41 75 74  pAutoinc ) *pAut
309fe 6f 69 6e 63 20 3d 20 61 75 74 6f 69 6e 63 3b 0a  oinc = autoinc;.
309ff 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
30a00 3d 3d 72 63 20 26 26 20 21 70 54 61 62 20 29 7b  ==rc && !pTab ){
30a01 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
30a02 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b  ee(db, zErrMsg);
30a03 0a 20 20 20 20 7a 45 72 72 4d 73 67 20 3d 20 73  .    zErrMsg = s
30a04 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
30a05 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65  , "no such table
30a06 20 63 6f 6c 75 6d 6e 3a 20 25 73 2e 25 73 22 2c   column: %s.%s",
30a07 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 0a 20 20 20   zTableName,.   
30a08 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65       zColumnName
30a09 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
30a0a 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
30a0b 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
30a0c 20 72 63 2c 20 28 7a 45 72 72 4d 73 67 3f 22 25   rc, (zErrMsg?"%
30a0d 73 22 3a 30 29 2c 20 7a 45 72 72 4d 73 67 29 3b  s":0), zErrMsg);
30a0e 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
30a0f 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20  (db, zErrMsg);. 
30a10 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
30a11 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
30a12 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
30a13 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
30a14 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
30a15 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65  endif../*.** Sle
30a16 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20  ep for a little 
30a17 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74  while.  Return t
30a18 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  he amount of tim
30a19 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 53 51 4c 49  e slept..*/.SQLI
30a1a 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
30a1b 65 33 5f 73 6c 65 65 70 28 69 6e 74 20 6d 73 29  e3_sleep(int ms)
30a1c 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
30a1d 2a 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b  *pVfs;.  int rc;
30a1e 0a 20 20 70 56 66 73 20 3d 20 73 71 6c 69 74 65  .  pVfs = sqlite
30a1f 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 3b 0a 20  3_vfs_find(0);. 
30a20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29 20 72   if( pVfs==0 ) r
30a21 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 54  eturn 0;..  /* T
30a22 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72  his function wor
30a23 6b 73 20 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e  ks in millisecon
30a24 64 73 2c 20 62 75 74 20 74 68 65 20 75 6e 64 65  ds, but the unde
30a25 72 6c 79 69 6e 67 20 4f 73 53 6c 65 65 70 28 29  rlying OsSleep()
30a26 20 0a 20 20 2a 2a 20 41 50 49 20 75 73 65 73 20   .  ** API uses 
30a27 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2e 20 48 65  microseconds. He
30a28 6e 63 65 20 74 68 65 20 31 30 30 30 27 73 2e 0a  nce the 1000's..
30a29 20 20 2a 2f 0a 20 20 72 63 20 3d 20 28 73 71 6c    */.  rc = (sql
30a2a 69 74 65 33 4f 73 53 6c 65 65 70 28 70 56 66 73  ite3OsSleep(pVfs
30a2b 2c 20 31 30 30 30 2a 6d 73 29 2f 31 30 30 30 29  , 1000*ms)/1000)
30a2c 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
30a2d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f  ../*.** Enable o
30a2e 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 65 78  r disable the ex
30a2f 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
30a30 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  des..*/.SQLITE_A
30a31 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
30a32 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63  xtended_result_c
30a33 6f 64 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62  odes(sqlite3 *db
30a34 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a 20 20  , int onoff){.  
30a35 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
30a36 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
30a37 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d 20    db->errMask = 
30a38 6f 6e 6f 66 66 20 3f 20 30 78 66 66 66 66 66 66  onoff ? 0xffffff
30a39 66 66 20 3a 20 30 78 66 66 3b 0a 20 20 73 71 6c  ff : 0xff;.  sql
30a3a 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
30a3b 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
30a3c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
30a3d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65  .}../*.** Invoke
30a3e 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   the xFileContro
30a3f 6c 20 6d 65 74 68 6f 64 20 6f 6e 20 61 20 70 61  l method on a pa
30a40 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
30a41 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
30a42 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c   int sqlite3_fil
30a43 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  e_control(sqlite
30a44 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
30a45 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20  r *zDbName, int 
30a46 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
30a47 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
30a48 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 6e 74 20  TE_ERROR;.  int 
30a49 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  iDb;.  sqlite3_m
30a4a 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
30a4b 75 74 65 78 29 3b 0a 20 20 69 66 28 20 7a 44 62  utex);.  if( zDb
30a4c 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Name==0 ){.    i
30a4d 44 62 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  Db = 0;.  }else{
30a4e 0a 20 20 20 20 66 6f 72 28 69 44 62 3d 30 3b 20  .    for(iDb=0; 
30a4f 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62  iDb<db->nDb; iDb
30a50 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ++){.      if( s
30a51 74 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44  trcmp(db->aDb[iD
30a52 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d  b].zName, zDbNam
30a53 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  e)==0 ) break;. 
30a54 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69     }.  }.  if( i
30a55 44 62 3c 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20  Db<db->nDb ){.  
30a56 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 20    Btree *pBtree 
30a57 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
30a58 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74 72  Bt;.    if( pBtr
30a59 65 65 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65  ee ){.      Page
30a5a 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20  r *pPager;.     
30a5b 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66   sqlite3_file *f
30a5c 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  d;.      sqlite3
30a5d 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65  BtreeEnter(pBtre
30a5e 65 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72  e);.      pPager
30a5f 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
30a60 61 67 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20  ager(pBtree);.  
30a61 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
30a62 65 72 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 66  er!=0 );.      f
30a63 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  d = sqlite3Pager
30a64 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20  File(pPager);.  
30a65 20 20 20 20 61 73 73 65 72 74 28 20 66 64 21 3d      assert( fd!=
30a66 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66  0 );.      if( f
30a67 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20  d->pMethods ){. 
30a68 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
30a69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
30a6a 28 66 64 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a  (fd, op, pArg);.
30a6b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
30a6c 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
30a6d 70 42 74 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20  pBtree);.    }. 
30a6e 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
30a6f 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
30a70 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
30a71 3b 20 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ;   .}../*.** In
30a72 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 74  terface to the t
30a73 65 73 74 69 6e 67 20 6c 6f 67 69 63 2e 0a 2a 2f  esting logic..*/
30a74 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
30a75 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
30a76 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e  trol(int op, ...
30a77 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b  ){.  int rc = 0;
30a78 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
30a79 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
30a7a 54 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  T.  va_list ap;.
30a7b 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f    va_start(ap, o
30a7c 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  p);.  switch( op
30a7d 20 29 7b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20   ){..    /*.    
30a7e 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72  ** Save the curr
30a7f 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74 68 65  ent state of the
30a80 20 50 52 4e 47 2e 0a 20 20 20 20 2a 2f 0a 20 20   PRNG..    */.  
30a81 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
30a82 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45  STCTRL_PRNG_SAVE
30a83 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
30a84 33 50 72 6e 67 53 61 76 65 53 74 61 74 65 28 29  3PrngSaveState()
30a85 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
30a86 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
30a87 20 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20   ** Restore the 
30a88 73 74 61 74 65 20 6f 66 20 74 68 65 20 50 52 4e  state of the PRN
30a89 47 20 74 6f 20 74 68 65 20 6c 61 73 74 20 73 74  G to the last st
30a8a 61 74 65 20 73 61 76 65 64 20 75 73 69 6e 67 0a  ate saved using.
30a8b 20 20 20 20 2a 2a 20 50 52 4e 47 5f 53 41 56 45      ** PRNG_SAVE
30a8c 2e 20 20 49 66 20 50 52 4e 47 5f 53 41 56 45 20  .  If PRNG_SAVE 
30a8d 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65  has never before
30a8e 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68   been called, th
30a8f 65 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 76  en.    ** this v
30a90 65 72 62 20 61 63 74 73 20 6c 69 6b 65 20 50 52  erb acts like PR
30a91 4e 47 5f 52 45 53 45 54 2e 0a 20 20 20 20 2a 2f  NG_RESET..    */
30a92 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
30a93 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52  _TESTCTRL_PRNG_R
30a94 45 53 54 4f 52 45 3a 20 7b 0a 20 20 20 20 20 20  ESTORE: {.      
30a95 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f  sqlite3PrngResto
30a96 72 65 53 74 61 74 65 28 29 3b 0a 20 20 20 20 20  reState();.     
30a97 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
30a98 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 52 65 73     /*.    ** Res
30a99 65 74 20 74 68 65 20 50 52 4e 47 20 62 61 63 6b  et the PRNG back
30a9a 20 74 6f 20 69 74 73 20 75 6e 69 6e 69 74 69 61   to its uninitia
30a9b 6c 69 7a 65 64 20 73 74 61 74 65 2e 20 20 54 68  lized state.  Th
30a9c 65 20 6e 65 78 74 20 63 61 6c 6c 0a 20 20 20 20  e next call.    
30a9d 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 61  ** to sqlite3_ra
30a9e 6e 64 6f 6d 6e 65 73 73 28 29 20 77 69 6c 6c 20  ndomness() will 
30a9f 72 65 73 65 65 64 20 74 68 65 20 50 52 4e 47 20  reseed the PRNG 
30aa0 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 63  using a single c
30aa1 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68  all.    ** to th
30aa2 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65  e xRandomness me
30aa3 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61  thod of the defa
30aa4 75 6c 74 20 56 46 53 2e 0a 20 20 20 20 2a 2f 0a  ult VFS..    */.
30aa5 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
30aa6 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45  TESTCTRL_PRNG_RE
30aa7 53 45 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  SET: {.      sql
30aa8 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61  ite3PrngResetSta
30aa9 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61  te();.      brea
30aaa 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
30aab 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33  .    **  sqlite3
30aac 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 42 49  _test_control(BI
30aad 54 56 45 43 5f 54 45 53 54 2c 20 73 69 7a 65 2c  TVEC_TEST, size,
30aae 20 70 72 6f 67 72 61 6d 29 0a 20 20 20 20 2a 2a   program).    **
30aaf 0a 20 20 20 20 2a 2a 20 52 75 6e 20 61 20 74 65  .    ** Run a te
30ab0 73 74 20 61 67 61 69 6e 73 74 20 61 20 42 69 74  st against a Bit
30ab1 76 65 63 20 6f 62 6a 65 63 74 20 6f 66 20 73 69  vec object of si
30ab2 7a 65 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d  ze.  The program
30ab3 20 61 72 67 75 6d 65 6e 74 0a 20 20 20 20 2a 2a   argument.    **
30ab4 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20   is an array of 
30ab5 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 64 65  integers that de
30ab6 66 69 6e 65 73 20 74 68 65 20 74 65 73 74 2e 20  fines the test. 
30ab7 20 52 65 74 75 72 6e 20 2d 31 20 6f 6e 20 61 0a   Return -1 on a.
30ab8 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c      ** memory al
30ab9 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 2c 20  location error, 
30aba 30 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 6f 72  0 on success, or
30abb 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 61 6e   non-zero for an
30abc 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2a 20 53   error..    ** S
30abd 65 65 20 74 68 65 20 73 71 6c 69 74 65 33 42 69  ee the sqlite3Bi
30abe 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28  tvecBuiltinTest(
30abf 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  ) for additional
30ac0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20   information..  
30ac1 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51    */.    case SQ
30ac2 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49  LITE_TESTCTRL_BI
30ac3 54 56 45 43 5f 54 45 53 54 3a 20 7b 0a 20 20 20  TVEC_TEST: {.   
30ac4 20 20 20 69 6e 74 20 73 7a 20 3d 20 76 61 5f 61     int sz = va_a
30ac5 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
30ac6 20 20 20 69 6e 74 20 2a 61 50 72 6f 67 20 3d 20     int *aProg = 
30ac7 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29  va_arg(ap, int*)
30ac8 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
30ac9 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69  ite3BitvecBuilti
30aca 6e 54 65 73 74 28 73 7a 2c 20 61 50 72 6f 67 29  nTest(sz, aProg)
30acb 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
30acc 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
30acd 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73   **  sqlite3_tes
30ace 74 5f 63 6f 6e 74 72 6f 6c 28 42 45 4e 49 47 4e  t_control(BENIGN
30acf 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 2c 20 78  _MALLOC_HOOKS, x
30ad0 42 65 67 69 6e 2c 20 78 45 6e 64 29 0a 20 20 20  Begin, xEnd).   
30ad1 20 2a 2a 0a 20 20 20 20 2a 2a 20 52 65 67 69 73   **.    ** Regis
30ad2 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c  ter hooks to cal
30ad3 6c 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68  l to indicate wh
30ad4 69 63 68 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  ich malloc() fai
30ad5 6c 75 72 65 73 20 0a 20 20 20 20 2a 2a 20 61 72  lures .    ** ar
30ad6 65 20 62 65 6e 69 67 6e 2e 0a 20 20 20 20 2a 2f  e benign..    */
30ad7 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
30ad8 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e  _TESTCTRL_BENIGN
30ad9 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 3a 20 7b  _MALLOC_HOOKS: {
30ada 0a 20 20 20 20 20 20 74 79 70 65 64 65 66 20 76  .      typedef v
30adb 6f 69 64 20 28 2a 76 6f 69 64 5f 66 75 6e 63 74  oid (*void_funct
30adc 69 6f 6e 29 28 76 6f 69 64 29 3b 0a 20 20 20 20  ion)(void);.    
30add 20 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 20    void_function 
30ade 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20  xBenignBegin;.  
30adf 20 20 20 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f      void_functio
30ae0 6e 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 20 20  n xBenignEnd;.  
30ae1 20 20 20 20 78 42 65 6e 69 67 6e 42 65 67 69 6e      xBenignBegin
30ae2 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
30ae3 69 64 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a 20 20  id_function);.  
30ae4 20 20 20 20 78 42 65 6e 69 67 6e 45 6e 64 20 3d      xBenignEnd =
30ae5 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64   va_arg(ap, void
30ae6 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a 20 20 20 20  _function);.    
30ae7 20 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d    sqlite3BenignM
30ae8 61 6c 6c 6f 63 48 6f 6f 6b 73 28 78 42 65 6e 69  allocHooks(xBeni
30ae9 67 6e 42 65 67 69 6e 2c 20 78 42 65 6e 69 67 6e  gnBegin, xBenign
30aea 45 6e 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61  End);.      brea
30aeb 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
30aec 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33  .    **  sqlite3
30aed 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 50 45  _test_control(PE
30aee 4e 44 49 4e 47 5f 42 59 54 45 2c 20 75 6e 73 69  NDING_BYTE, unsi
30aef 67 6e 65 64 20 69 6e 74 20 58 29 0a 20 20 20 20  gned int X).    
30af0 2a 2a 0a 20 20 20 20 2a 2a 20 53 65 74 20 74 68  **.    ** Set th
30af1 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65 20 74  e PENDING byte t
30af2 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 74  o the value in t
30af3 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 69 66 20  he argument, if 
30af4 58 3e 30 2e 0a 20 20 20 20 2a 2a 20 4d 61 6b 65  X>0..    ** Make
30af5 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 20 58   no changes if X
30af6 3d 3d 30 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ==0.  Return the
30af7 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 65   value of the pe
30af8 6e 64 69 6e 67 20 62 79 74 65 0a 20 20 20 20 2a  nding byte.    *
30af9 2a 20 61 73 20 69 74 20 65 78 69 73 74 69 6e 67  * as it existing
30afa 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75   before this rou
30afb 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2e  tine was called.
30afc 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
30afd 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61 6e 67  MPORTANT:  Chang
30afe 69 6e 67 20 74 68 65 20 50 45 4e 44 49 4e 47 20  ing the PENDING 
30aff 62 79 74 65 20 66 72 6f 6d 20 30 78 34 30 30 30  byte from 0x4000
30b00 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e 0a  0000 results in.
30b01 20 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6d 70      ** an incomp
30b02 61 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 20  atible database 
30b03 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20 43 68  file format.  Ch
30b04 61 6e 67 69 6e 67 20 74 68 65 20 50 45 4e 44 49  anging the PENDI
30b05 4e 47 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 77  NG byte.    ** w
30b06 68 69 6c 65 20 61 6e 79 20 64 61 74 61 62 61 73  hile any databas
30b07 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
30b08 6f 70 65 6e 20 72 65 73 75 6c 74 73 20 69 6e 20  open results in 
30b09 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a 20 20  undefined and.  
30b0a 20 20 2a 2a 20 64 69 6c 65 74 65 72 69 6f 75 73    ** dileterious
30b0b 20 62 65 68 61 76 69 6f 72 2e 0a 20 20 20 20 2a   behavior..    *
30b0c 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
30b0d 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 49  E_TESTCTRL_PENDI
30b0e 4e 47 5f 42 59 54 45 3a 20 7b 0a 20 20 20 20 20  NG_BYTE: {.     
30b0f 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 65   unsigned int ne
30b10 77 56 61 6c 20 3d 20 76 61 5f 61 72 67 28 61 70  wVal = va_arg(ap
30b11 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b  , unsigned int);
30b12 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
30b13 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a  te3PendingByte;.
30b14 20 20 20 20 20 20 69 66 28 20 6e 65 77 56 61 6c        if( newVal
30b15 20 29 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e   ) sqlite3Pendin
30b16 67 42 79 74 65 20 3d 20 6e 65 77 56 61 6c 3b 0a  gByte = newVal;.
30b17 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30b18 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28   }.  }.  va_end(
30b19 61 70 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ap);.#endif /* S
30b1a 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
30b1b 49 4e 5f 54 45 53 54 20 2a 2f 0a 20 20 72 65 74  IN_TEST */.  ret
30b1c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  urn rc;.}../****
30b1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
30b1e 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f main.c *******
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 2f 0a 2f 2a 2a 2a 2a  *********/./****
30b22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
30b23 20 66 69 6c 65 20 6e 6f 74 69 66 79 2e 63 20 2a   file notify.c *
30b24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
30b27 20 32 30 30 39 20 4d 61 72 63 68 20 33 0a 2a 2a   2009 March 3.**
30b28 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
30b29 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
30b2a 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
30b2b 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
30b2c 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
30b2d 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
30b2e 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
30b2f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
30b30 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
30b31 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
30b32 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
30b33 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
30b34 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
30b35 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
30b36 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
30b37 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
30b38 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
30b39 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
30b3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
30b3e 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
30b3f 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d  tains the implem
30b40 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
30b41 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
30b42 6f 74 69 66 79 28 29 0a 2a 2a 20 41 50 49 20 6d  otify().** API m
30b43 65 74 68 6f 64 20 61 6e 64 20 69 74 73 20 61 73  ethod and its as
30b44 73 6f 63 69 61 74 65 64 20 66 75 6e 63 74 69 6f  sociated functio
30b45 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49  nality..**.** $I
30b46 64 3a 20 6e 6f 74 69 66 79 2e 63 2c 76 20 31 2e  d: notify.c,v 1.
30b47 34 20 32 30 30 39 2f 30 34 2f 30 37 20 32 32 3a  4 2009/04/07 22:
30b48 30 36 3a 35 37 20 64 72 68 20 45 78 70 20 24 0a  06:57 drh Exp $.
30b49 2a 2f 0a 0a 2f 2a 20 4f 6d 69 74 20 74 68 69 73  */../* Omit this
30b4a 20 65 6e 74 69 72 65 20 66 69 6c 65 20 69 66 20   entire file if 
30b4b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e  SQLITE_ENABLE_UN
30b4c 4c 4f 43 4b 5f 4e 4f 54 49 46 59 20 69 73 20 6e  LOCK_NOTIFY is n
30b4d 6f 74 20 64 65 66 69 6e 65 64 2e 20 2a 2f 0a 23  ot defined. */.#
30b4e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
30b4f 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46  BLE_UNLOCK_NOTIF
30b50 59 0a 0a 2f 2a 0a 2a 2a 20 50 75 62 6c 69 63 20  Y../*.** Public 
30b51 69 6e 74 65 72 66 61 63 65 73 3a 0a 2a 2a 0a 2a  interfaces:.**.*
30b52 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  *   sqlite3Conne
30b53 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 29 0a 2a  ctionBlocked().*
30b54 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  *   sqlite3Conne
30b55 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 0a  ctionUnlocked().
30b56 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e  **   sqlite3Conn
30b57 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 29 0a 2a  ectionClosed().*
30b58 2a 20 20 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  *   sqlite3_unlo
30b59 63 6b 5f 6e 6f 74 69 66 79 28 29 0a 2a 2f 0a 0a  ck_notify().*/..
30b5a 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 4d 75  #define assertMu
30b5b 74 65 78 48 65 6c 64 28 29 20 5c 0a 20 20 61 73  texHeld() \.  as
30b5c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
30b5d 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33  tex_held(sqlite3
30b5e 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
30b5f 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
30b60 41 53 54 45 52 29 29 20 29 0a 0a 2f 2a 0a 2a 2a  ASTER)) )../*.**
30b61 20 48 65 61 64 20 6f 66 20 61 20 6c 69 6e 6b 65   Head of a linke
30b62 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 71  d list of all sq
30b63 6c 69 74 65 33 20 6f 62 6a 65 63 74 73 20 63 72  lite3 objects cr
30b64 65 61 74 65 64 20 62 79 20 74 68 69 73 20 70 72  eated by this pr
30b65 6f 63 65 73 73 0a 2a 2a 20 66 6f 72 20 77 68 69  ocess.** for whi
30b66 63 68 20 65 69 74 68 65 72 20 73 71 6c 69 74 65  ch either sqlite
30b67 33 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  3.pBlockingConne
30b68 63 74 69 6f 6e 20 6f 72 20 73 71 6c 69 74 65 33  ction or sqlite3
30b69 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69  .pUnlockConnecti
30b6a 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c  on.** is not NUL
30b6b 4c 2e 20 54 68 69 73 20 76 61 72 69 61 62 6c 65  L. This variable
30b6c 20 6d 61 79 20 6f 6e 6c 79 20 61 63 63 65 73 73   may only access
30b6d 65 64 20 77 68 69 6c 65 20 74 68 65 20 53 54 41  ed while the STA
30b6e 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d 75  TIC_MASTER.** mu
30b6f 74 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2f 0a  tex is held..*/.
30b70 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 20 2a  static sqlite3 *
30b71 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74  SQLITE_WSD sqlit
30b72 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 20 3d 20  e3BlockedList = 
30b73 30 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  0;..#ifndef NDEB
30b74 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  UG./*.** This fu
30b75 6e 63 74 69 6f 6e 20 69 73 20 61 20 63 6f 6d 70  nction is a comp
30b76 6c 65 78 20 61 73 73 65 72 74 28 29 20 74 68 61  lex assert() tha
30b77 74 20 76 65 72 69 66 69 65 73 20 74 68 65 20 66  t verifies the f
30b78 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 70 72 6f  ollowing .** pro
30b79 70 65 72 74 69 65 73 20 6f 66 20 74 68 65 20 62  perties of the b
30b7a 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
30b7b 6e 73 20 6c 69 73 74 3a 0a 2a 2a 0a 2a 2a 20 20  ns list:.**.**  
30b7c 20 31 29 20 45 61 63 68 20 65 6e 74 72 79 20 69   1) Each entry i
30b7d 6e 20 74 68 65 20 6c 69 73 74 20 68 61 73 20 61  n the list has a
30b7e 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 20   non-NULL value 
30b7f 66 6f 72 20 65 69 74 68 65 72 20 0a 2a 2a 20 20  for either .**  
30b80 20 20 20 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65      pUnlockConne
30b81 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b 69  ction or pBlocki
30b82 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 72  ngConnection, or
30b83 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 20 20 32   both..**.**   2
30b84 29 20 41 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e  ) All entries in
30b85 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 20 73   the list that s
30b86 68 61 72 65 20 61 20 63 6f 6d 6d 6f 6e 20 76 61  hare a common va
30b87 6c 75 65 20 66 6f 72 20 0a 2a 2a 20 20 20 20 20  lue for .**     
30b88 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 61   xUnlockNotify a
30b89 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74  re grouped toget
30b8a 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20  her..**.**   3) 
30b8b 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  If the argument 
30b8c 64 62 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  db is not NULL, 
30b8d 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65  then none of the
30b8e 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a   entries in the.
30b8f 2a 2a 20 20 20 20 20 20 62 6c 6f 63 6b 65 64 20  **      blocked 
30b90 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74  connections list
30b91 20 68 61 76 65 20 70 55 6e 6c 6f 63 6b 43 6f 6e   have pUnlockCon
30b92 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63  nection or pBloc
30b93 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  kingConnection.*
30b94 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 64 62  *      set to db
30b95 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 77  . This is used w
30b96 68 65 6e 20 63 6c 6f 73 69 6e 67 20 63 6f 6e 6e  hen closing conn
30b97 65 63 74 69 6f 6e 20 64 62 2e 0a 2a 2f 0a 73 74  ection db..*/.st
30b98 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c  atic void checkL
30b99 69 73 74 50 72 6f 70 65 72 74 69 65 73 28 73 71  istProperties(sq
30b9a 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71  lite3 *db){.  sq
30b9b 6c 69 74 65 33 20 2a 70 3b 0a 20 20 66 6f 72 28  lite3 *p;.  for(
30b9c 70 3d 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64  p=sqlite3Blocked
30b9d 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  List; p; p=p->pN
30b9e 65 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20  extBlocked){.   
30b9f 20 69 6e 74 20 73 65 65 6e 20 3d 20 30 3b 0a 20   int seen = 0;. 
30ba0 20 20 20 73 71 6c 69 74 65 33 20 2a 70 32 3b 0a     sqlite3 *p2;.
30ba1 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 70  .    /* Verify p
30ba2 72 6f 70 65 72 74 79 20 28 31 29 20 2a 2f 0a 20  roperty (1) */. 
30ba3 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 55     assert( p->pU
30ba4 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20  nlockConnection 
30ba5 7c 7c 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43  || p->pBlockingC
30ba6 6f 6e 6e 65 63 74 69 6f 6e 20 29 3b 0a 0a 20 20  onnection );..  
30ba7 20 20 2f 2a 20 56 65 72 69 66 79 20 70 72 6f 70    /* Verify prop
30ba8 65 72 74 79 20 28 32 29 20 2a 2f 0a 20 20 20 20  erty (2) */.    
30ba9 66 6f 72 28 70 32 3d 73 71 6c 69 74 65 33 42 6c  for(p2=sqlite3Bl
30baa 6f 63 6b 65 64 4c 69 73 74 3b 20 70 32 21 3d 70  ockedList; p2!=p
30bab 3b 20 70 32 3d 70 32 2d 3e 70 4e 65 78 74 42 6c  ; p2=p2->pNextBl
30bac 6f 63 6b 65 64 29 7b 0a 20 20 20 20 20 20 69 66  ocked){.      if
30bad 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  ( p2->xUnlockNot
30bae 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e  ify==p->xUnlockN
30baf 6f 74 69 66 79 20 29 20 73 65 65 6e 20 3d 20 31  otify ) seen = 1
30bb0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
30bb1 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  p2->xUnlockNotif
30bb2 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  y==p->xUnlockNot
30bb3 69 66 79 20 7c 7c 20 21 73 65 65 6e 20 29 3b 0a  ify || !seen );.
30bb4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62        assert( db
30bb5 3d 3d 30 20 7c 7c 20 70 2d 3e 70 55 6e 6c 6f 63  ==0 || p->pUnloc
30bb6 6b 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64 62 20  kConnection!=db 
30bb7 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
30bb8 20 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 42 6c   db==0 || p->pBl
30bb9 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
30bba 21 3d 64 62 20 29 3b 0a 20 20 20 20 7d 0a 20 20  !=db );.    }.  
30bbb 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  }.}.#else.# defi
30bbc 6e 65 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70  ne checkListProp
30bbd 65 72 74 69 65 73 28 78 29 0a 23 65 6e 64 69 66  erties(x).#endif
30bbe 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 63  ../*.** Remove c
30bbf 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 66 72 6f  onnection db fro
30bc0 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f  m the blocked co
30bc1 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20  nnections list. 
30bc2 49 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  If connection.**
30bc3 20 64 62 20 69 73 20 6e 6f 74 20 63 75 72 72 65   db is not curre
30bc4 6e 74 6c 79 20 61 20 70 61 72 74 20 6f 66 20 74  ntly a part of t
30bc5 68 65 20 6c 69 73 74 2c 20 74 68 69 73 20 66 75  he list, this fu
30bc6 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
30bc7 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  p..*/.static voi
30bc8 64 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63  d removeFromBloc
30bc9 6b 65 64 4c 69 73 74 28 73 71 6c 69 74 65 33 20  kedList(sqlite3 
30bca 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  *db){.  sqlite3 
30bcb 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 4d 75  **pp;.  assertMu
30bcc 74 65 78 48 65 6c 64 28 29 3b 0a 20 20 66 6f 72  texHeld();.  for
30bcd 28 70 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f 63  (pp=&sqlite3Bloc
30bce 6b 65 64 4c 69 73 74 3b 20 2a 70 70 3b 20 70 70  kedList; *pp; pp
30bcf 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74   = &(*pp)->pNext
30bd0 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20 69 66  Blocked){.    if
30bd1 28 20 2a 70 70 3d 3d 64 62 20 29 7b 0a 20 20 20  ( *pp==db ){.   
30bd2 20 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e     *pp = (*pp)->
30bd3 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20  pNextBlocked;.  
30bd4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
30bd5 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
30bd6 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20  d connection db 
30bd7 74 6f 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63  to the blocked c
30bd8 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e  onnections list.
30bd9 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a   It is assumed.*
30bda 2a 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74  * that it is not
30bdb 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20   already a part 
30bdc 6f 66 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a  of the list..*/.
30bdd 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 54  static void addT
30bde 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28 73 71 6c  oBlockedList(sql
30bdf 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c  ite3 *db){.  sql
30be0 69 74 65 33 20 2a 2a 70 70 3b 0a 20 20 61 73 73  ite3 **pp;.  ass
30be1 65 72 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a  ertMutexHeld();.
30be2 20 20 66 6f 72 28 0a 20 20 20 20 70 70 3d 26 73    for(.    pp=&s
30be3 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73  qlite3BlockedLis
30be4 74 3b 20 0a 20 20 20 20 2a 70 70 20 26 26 20 28  t; .    *pp && (
30be5 2a 70 70 29 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  *pp)->xUnlockNot
30be6 69 66 79 21 3d 64 62 2d 3e 78 55 6e 6c 6f 63 6b  ify!=db->xUnlock
30be7 4e 6f 74 69 66 79 3b 20 0a 20 20 20 20 70 70 3d  Notify; .    pp=
30be8 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c 6f  &(*pp)->pNextBlo
30be9 63 6b 65 64 0a 20 20 29 3b 0a 20 20 64 62 2d 3e  cked.  );.  db->
30bea 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20 2a  pNextBlocked = *
30beb 70 70 3b 0a 20 20 2a 70 70 20 3d 20 64 62 3b 0a  pp;.  *pp = db;.
30bec 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  }../*.** Obtain 
30bed 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45  the STATIC_MASTE
30bee 52 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74  R mutex..*/.stat
30bef 69 63 20 76 6f 69 64 20 65 6e 74 65 72 4d 75 74  ic void enterMut
30bf0 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ex(void){.  sqli
30bf1 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
30bf2 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
30bf3 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
30bf4 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
30bf5 20 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65    checkListPrope
30bf6 72 74 69 65 73 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a  rties(0);.}../*.
30bf7 2a 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 53  ** Release the S
30bf8 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74  TATIC_MASTER mut
30bf9 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ex..*/.static vo
30bfa 69 64 20 6c 65 61 76 65 4d 75 74 65 78 28 76 6f  id leaveMutex(vo
30bfb 69 64 29 7b 0a 20 20 61 73 73 65 72 74 4d 75 74  id){.  assertMut
30bfc 65 78 48 65 6c 64 28 29 3b 0a 20 20 63 68 65 63  exHeld();.  chec
30bfd 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65 73 28  kListProperties(
30bfe 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  0);.  sqlite3_mu
30bff 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
30c00 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
30c01 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
30c02 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 0a 2f 2a 0a  MASTER));.}../*.
30c03 2a 2a 20 52 65 67 69 73 74 65 72 20 61 6e 20 75  ** Register an u
30c04 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
30c05 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  lback..**.** Thi
30c06 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  s is called afte
30c07 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 22 64 62  r connection "db
30c08 22 20 68 61 73 20 61 74 74 65 6d 70 74 65 64 20  " has attempted 
30c09 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a  some operation.*
30c0a 2a 20 62 75 74 20 68 61 73 20 72 65 63 65 69 76  * but has receiv
30c0b 65 64 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  ed an SQLITE_LOC
30c0c 4b 45 44 20 65 72 72 6f 72 20 62 65 63 61 75 73  KED error becaus
30c0d 65 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63  e another connec
30c0e 74 69 6f 6e 0a 2a 2a 20 28 63 61 6c 6c 20 69 74  tion.** (call it
30c0f 20 70 4f 74 68 65 72 29 20 69 6e 20 74 68 65 20   pOther) in the 
30c10 73 61 6d 65 20 70 72 6f 63 65 73 73 20 77 61 73  same process was
30c11 20 62 75 73 79 20 75 73 69 6e 67 20 74 68 65 20   busy using the 
30c12 73 61 6d 65 20 73 68 61 72 65 64 0a 2a 2a 20 63  same shared.** c
30c13 61 63 68 65 2e 20 20 70 4f 74 68 65 72 20 69 73  ache.  pOther is
30c14 20 66 6f 75 6e 64 20 62 79 20 6c 6f 6f 6b 69 6e   found by lookin
30c15 67 20 61 74 20 64 62 2d 3e 70 42 6c 6f 63 6b 69  g at db->pBlocki
30c16 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a  ngConnection..**
30c17 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
30c18 6e 6f 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e  no blocking conn
30c19 65 63 74 69 6f 6e 2c 20 74 68 65 20 63 61 6c 6c  ection, the call
30c1a 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
30c1b 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0a 2a 2a 20  immediately,.** 
30c1c 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74  before this rout
30c1d 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a  ine returns..**.
30c1e 2a 2a 20 49 66 20 70 4f 74 68 65 72 20 69 73 20  ** If pOther is 
30c1f 61 6c 72 65 61 64 79 20 62 6c 6f 63 6b 65 64 20  already blocked 
30c20 6f 6e 20 64 62 2c 20 74 68 65 6e 20 72 65 70 6f  on db, then repo
30c21 72 74 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rt SQLITE_LOCKED
30c22 2c 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a  , to indicate.**
30c23 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a   a deadlock..**.
30c24 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 6d 61  ** Otherwise, ma
30c25 6b 65 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20  ke arrangements 
30c26 74 6f 20 69 6e 76 6f 6b 65 20 78 4e 6f 74 69 66  to invoke xNotif
30c27 79 20 77 68 65 6e 20 70 4f 74 68 65 72 20 64 72  y when pOther dr
30c28 6f 70 73 0a 2a 2a 20 69 74 73 20 6c 6f 63 6b 73  ops.** its locks
30c29 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c  ..**.** Each cal
30c2a 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  l to this routin
30c2b 65 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20  e overrides any 
30c2c 70 72 69 6f 72 20 63 61 6c 6c 62 61 63 6b 73 20  prior callbacks 
30c2d 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 6f 6e  registered.** on
30c2e 20 74 68 65 20 73 61 6d 65 20 22 64 62 22 2e 20   the same "db". 
30c2f 20 49 66 20 78 4e 6f 74 69 66 79 3d 3d 30 20 74   If xNotify==0 t
30c30 68 65 6e 20 61 6e 79 20 70 72 69 6f 72 20 63 61  hen any prior ca
30c31 6c 6c 62 61 63 6b 73 20 61 72 65 20 69 6d 6d 65  llbacks are imme
30c32 64 69 61 74 65 6c 79 0a 2a 2a 20 63 61 6e 63 65  diately.** cance
30c33 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lled..*/.SQLITE_
30c34 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
30c35 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 0a 20  unlock_notify(. 
30c36 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
30c37 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28  void (*xNotify)(
30c38 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 2c 0a 20  void **, int),. 
30c39 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20   void *pArg.){. 
30c3a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
30c3b 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  _OK;..  sqlite3_
30c3c 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
30c3d 6d 75 74 65 78 29 3b 0a 20 20 65 6e 74 65 72 4d  mutex);.  enterM
30c3e 75 74 65 78 28 29 3b 0a 0a 20 20 69 66 28 20 78  utex();..  if( x
30c3f 4e 6f 74 69 66 79 3d 3d 30 20 29 7b 0a 20 20 20  Notify==0 ){.   
30c40 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b   removeFromBlock
30c41 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20  edList(db);.    
30c42 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65  db->pUnlockConne
30c43 63 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 64  ction = 0;.    d
30c44 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  b->xUnlockNotify
30c45 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 55   = 0;.    db->pU
30c46 6e 6c 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20  nlockArg = 0;.  
30c47 7d 65 6c 73 65 20 69 66 28 20 30 3d 3d 64 62 2d  }else if( 0==db-
30c48 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
30c49 74 69 6f 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54  tion ){.    /* T
30c4a 68 65 20 62 6c 6f 63 6b 69 6e 67 20 74 72 61 6e  he blocking tran
30c4b 73 61 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e  saction has been
30c4c 20 63 6f 6e 63 6c 75 64 65 64 2e 20 4f 72 20 74   concluded. Or t
30c4d 68 65 72 65 20 6e 65 76 65 72 20 77 61 73 20 61  here never was a
30c4e 20 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 69 6e   .    ** blockin
30c4f 67 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49  g transaction. I
30c50 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 69  n either case, i
30c51 6e 76 6f 6b 65 20 74 68 65 20 6e 6f 74 69 66 79  nvoke the notify
30c52 20 63 61 6c 6c 62 61 63 6b 0a 20 20 20 20 2a 2a   callback.    **
30c53 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20   immediately. . 
30c54 20 20 20 2a 2f 0a 20 20 20 20 78 4e 6f 74 69 66     */.    xNotif
30c55 79 28 26 70 41 72 67 2c 20 31 29 3b 0a 20 20 7d  y(&pArg, 1);.  }
30c56 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
30c57 33 20 2a 70 3b 0a 0a 20 20 20 20 66 6f 72 28 70  3 *p;..    for(p
30c58 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f  =db->pBlockingCo
30c59 6e 6e 65 63 74 69 6f 6e 3b 20 70 20 26 26 20 70  nnection; p && p
30c5a 21 3d 64 62 3b 20 70 3d 70 2d 3e 70 55 6e 6c 6f  !=db; p=p->pUnlo
30c5b 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 29 7b 7d 0a  ckConnection){}.
30c5c 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
30c5d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c     rc = SQLITE_L
30c5e 4f 43 4b 45 44 3b 20 20 20 20 20 20 20 20 20 20  OCKED;          
30c5f 20 20 20 20 2f 2a 20 44 65 61 64 6c 6f 63 6b 20      /* Deadlock 
30c60 64 65 74 65 63 74 65 64 2e 20 2a 2f 0a 20 20 20  detected. */.   
30c61 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62   }else{.      db
30c62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
30c63 69 6f 6e 20 3d 20 64 62 2d 3e 70 42 6c 6f 63 6b  ion = db->pBlock
30c64 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 0a 20  ingConnection;. 
30c65 20 20 20 20 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b       db->xUnlock
30c66 4e 6f 74 69 66 79 20 3d 20 78 4e 6f 74 69 66 79  Notify = xNotify
30c67 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 55 6e 6c  ;.      db->pUnl
30c68 6f 63 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  ockArg = pArg;. 
30c69 20 20 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42       removeFromB
30c6a 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a  lockedList(db);.
30c6b 20 20 20 20 20 20 61 64 64 54 6f 42 6c 6f 63 6b        addToBlock
30c6c 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20  edList(db);.    
30c6d 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76 65 4d 75  }.  }..  leaveMu
30c6e 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28  tex();.  assert(
30c6f 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
30c70 65 64 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ed );.  sqlite3E
30c71 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 72 63  rror(db, rc, (rc
30c72 3f 22 64 61 74 61 62 61 73 65 20 69 73 20 64 65  ?"database is de
30c73 61 64 6c 6f 63 6b 65 64 22 3a 30 29 29 3b 0a 20  adlocked":0));. 
30c74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
30c75 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
30c76 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
30c77 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
30c78 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77  tion is called w
30c79 68 69 6c 65 20 73 74 65 70 70 69 6e 67 20 6f 72  hile stepping or
30c7a 20 70 72 65 70 61 72 69 6e 67 20 61 20 73 74 61   preparing a sta
30c7b 74 65 6d 65 6e 74 20 0a 2a 2a 20 61 73 73 6f 63  tement .** assoc
30c7c 69 61 74 65 64 20 77 69 74 68 20 63 6f 6e 6e 65  iated with conne
30c7d 63 74 69 6f 6e 20 64 62 2e 20 54 68 65 20 6f 70  ction db. The op
30c7e 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 74  eration will ret
30c7f 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
30c80 44 0a 2a 2a 20 74 6f 20 74 68 65 20 75 73 65 72  D.** to the user
30c81 20 62 65 63 61 75 73 65 20 69 74 20 72 65 71 75   because it requ
30c82 69 72 65 73 20 61 20 6c 6f 63 6b 20 74 68 61 74  ires a lock that
30c83 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 76 61   will not be ava
30c84 69 6c 61 62 6c 65 0a 2a 2a 20 75 6e 74 69 6c 20  ilable.** until 
30c85 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 42 6c 6f 63  connection pBloc
30c86 6b 65 72 20 63 6f 6e 63 6c 75 64 65 73 20 69 74  ker concludes it
30c87 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  s current transa
30c88 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
30c89 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
30c8a 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
30c8b 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a  locked(sqlite3 *
30c8c 64 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 42 6c  db, sqlite3 *pBl
30c8d 6f 63 6b 65 72 29 7b 0a 20 20 65 6e 74 65 72 4d  ocker){.  enterM
30c8e 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 64 62  utex();.  if( db
30c8f 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  ->pBlockingConne
30c90 63 74 69 6f 6e 3d 3d 30 20 26 26 20 64 62 2d 3e  ction==0 && db->
30c91 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
30c92 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 54  n==0 ){.    addT
30c93 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29  oBlockedList(db)
30c94 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 42 6c 6f  ;.  }.  db->pBlo
30c95 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20  ckingConnection 
30c96 3d 20 70 42 6c 6f 63 6b 65 72 3b 0a 20 20 6c 65  = pBlocker;.  le
30c97 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f  aveMutex();.}../
30c98 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
30c99 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  on is called whe
30c9a 6e 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63  n.** the transac
30c9b 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62 79 20 64  tion opened by d
30c9c 61 74 61 62 61 73 65 20 64 62 20 68 61 73 20 6a  atabase db has j
30c9d 75 73 74 20 66 69 6e 69 73 68 65 64 2e 20 4c 6f  ust finished. Lo
30c9e 63 6b 73 20 68 65 6c 64 20 0a 2a 2a 20 62 79 20  cks held .** by 
30c9f 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
30ca0 69 6f 6e 20 64 62 20 68 61 76 65 20 62 65 65 6e  ion db have been
30ca1 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a   released..**.**
30ca2 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6c   This function l
30ca3 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63  oops through eac
30ca4 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62  h entry in the b
30ca5 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
30ca6 6e 73 0a 2a 2a 20 6c 69 73 74 20 61 6e 64 20 64  ns.** list and d
30ca7 6f 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  oes the followin
30ca8 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66  g:.**.**   1) If
30ca9 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 42 6c   the sqlite3.pBl
30caa 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
30cab 20 6d 65 6d 62 65 72 20 6f 66 20 61 20 6c 69 73   member of a lis
30cac 74 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 20 20  t entry is.**   
30cad 20 20 20 73 65 74 20 74 6f 20 64 62 2c 20 74 68     set to db, th
30cae 65 6e 20 73 65 74 20 70 42 6c 6f 63 6b 69 6e 67  en set pBlocking
30caf 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a  Connection=0..**
30cb0 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68 65 20  .**   2) If the 
30cb1 73 71 6c 69 74 65 33 2e 70 55 6e 6c 6f 63 6b 43  sqlite3.pUnlockC
30cb2 6f 6e 6e 65 63 74 69 6f 6e 20 6d 65 6d 62 65 72  onnection member
30cb3 20 6f 66 20 61 20 6c 69 73 74 20 65 6e 74 72 79   of a list entry
30cb4 20 69 73 0a 2a 2a 20 20 20 20 20 20 73 65 74 20   is.**      set 
30cb5 74 6f 20 64 62 2c 20 74 68 65 6e 20 69 6e 76 6f  to db, then invo
30cb6 6b 65 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65  ke the configure
30cb7 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  d unlock-notify 
30cb8 63 61 6c 6c 62 61 63 6b 20 61 6e 64 0a 2a 2a 20  callback and.** 
30cb9 20 20 20 20 20 73 65 74 20 70 55 6e 6c 6f 63 6b       set pUnlock
30cba 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a  Connection=0..**
30cbb 0a 2a 2a 20 20 20 33 29 20 49 66 20 74 68 65 20  .**   3) If the 
30cbc 74 77 6f 20 73 74 65 70 73 20 61 62 6f 76 65 20  two steps above 
30cbd 6d 65 61 6e 20 74 68 61 74 20 70 42 6c 6f 63 6b  mean that pBlock
30cbe 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30  ingConnection==0
30cbf 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 55 6e   and.**      pUn
30cc0 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  lockConnection==
30cc1 30 2c 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e  0, remove the en
30cc2 74 72 79 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f  try from the blo
30cc3 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
30cc4 0a 2a 2a 20 20 20 20 20 20 6c 69 73 74 2e 0a 2a  .**      list..*
30cc5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
30cc6 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e   void sqlite3Con
30cc7 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
30cc8 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
30cc9 76 6f 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f  void (*xUnlockNo
30cca 74 69 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69  tify)(void **, i
30ccb 6e 74 29 20 3d 20 30 3b 20 2f 2a 20 55 6e 6c 6f  nt) = 0; /* Unlo
30ccc 63 6b 2d 6e 6f 74 69 66 79 20 63 62 20 74 6f 20  ck-notify cb to 
30ccd 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 69 6e 74 20  invoke */.  int 
30cce 6e 41 72 67 20 3d 20 30 3b 20 20 20 20 20 20 20  nArg = 0;       
30ccf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30cd0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
30cd1 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 41 72  f entries in aAr
30cd2 67 5b 5d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  g[] */.  sqlite3
30cd3 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20   **pp;          
30cd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30cd5 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
30cd6 72 69 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64  riable */.  void
30cd7 20 2a 2a 61 41 72 67 3b 20 20 20 20 20 20 20 20   **aArg;        
30cd8 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
30cd9 6e 74 73 20 74 6f 20 74 68 65 20 75 6e 6c 6f 63  nts to the unloc
30cda 6b 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  k callback */.  
30cdb 76 6f 69 64 20 2a 2a 61 44 79 6e 20 3d 20 30 3b  void **aDyn = 0;
30cdc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 79             /* Dy
30cdd 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
30cde 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 61 41  ted space for aA
30cdf 72 67 5b 5d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  rg[] */.  void *
30ce0 61 53 74 61 74 69 63 5b 31 36 5d 3b 20 20 20 20  aStatic[16];    
30ce1 20 20 20 20 20 2f 2a 20 53 74 61 72 74 65 72 20       /* Starter 
30ce2 73 70 61 63 65 20 66 6f 72 20 61 41 72 67 5b 5d  space for aArg[]
30ce3 2e 20 20 4e 6f 20 6d 61 6c 6c 6f 63 20 72 65 71  .  No malloc req
30ce4 75 69 72 65 64 20 2a 2f 0a 0a 20 20 61 41 72 67  uired */..  aArg
30ce5 20 3d 20 61 53 74 61 74 69 63 3b 0a 20 20 65 6e   = aStatic;.  en
30ce6 74 65 72 4d 75 74 65 78 28 29 3b 20 20 20 20 20  terMutex();     
30ce7 20 20 20 20 2f 2a 20 45 6e 74 65 72 20 53 54 41      /* Enter STA
30ce8 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
30ce9 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c   */..  /* This l
30cea 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f  oop runs once fo
30ceb 72 20 65 61 63 68 20 65 6e 74 72 79 20 69 6e 20  r each entry in 
30cec 74 68 65 20 62 6c 6f 63 6b 65 64 2d 63 6f 6e 6e  the blocked-conn
30ced 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20 2a 2f  ections list. */
30cee 0a 20 20 66 6f 72 28 70 70 3d 26 73 71 6c 69 74  .  for(pp=&sqlit
30cef 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 2a  e3BlockedList; *
30cf0 70 70 3b 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 20  pp; /* no-op */ 
30cf1 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
30cf2 70 20 3d 20 2a 70 70 3b 0a 0a 20 20 20 20 2f 2a  p = *pp;..    /*
30cf3 20 53 74 65 70 20 31 2e 20 2a 2f 0a 20 20 20 20   Step 1. */.    
30cf4 69 66 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67  if( p->pBlocking
30cf5 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29  Connection==db )
30cf6 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 6c 6f 63  {.      p->pBloc
30cf7 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  kingConnection =
30cf8 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   0;.    }..    /
30cf9 2a 20 53 74 65 70 20 32 2e 20 2a 2f 0a 20 20 20  * Step 2. */.   
30cfa 20 69 66 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43   if( p->pUnlockC
30cfb 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29 7b  onnection==db ){
30cfc 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
30cfd 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20  ->xUnlockNotify 
30cfe 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  );.      if( p->
30cff 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 21 3d 78  xUnlockNotify!=x
30d00 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 26 26 20  UnlockNotify && 
30d01 6e 41 72 67 21 3d 30 20 29 7b 0a 20 20 20 20 20  nArg!=0 ){.     
30d02 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79     xUnlockNotify
30d03 28 61 41 72 67 2c 20 6e 41 72 67 29 3b 0a 20 20  (aArg, nArg);.  
30d04 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a        nArg = 0;.
30d05 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 73        }..      s
30d06 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
30d07 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  nMalloc();.     
30d08 20 61 73 73 65 72 74 28 20 61 41 72 67 3d 3d 61   assert( aArg==a
30d09 44 79 6e 20 7c 7c 20 28 61 44 79 6e 3d 3d 30 20  Dyn || (aDyn==0 
30d0a 26 26 20 61 41 72 67 3d 3d 61 53 74 61 74 69 63  && aArg==aStatic
30d0b 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ) );.      asser
30d0c 74 28 20 6e 41 72 67 3c 3d 28 69 6e 74 29 41 72  t( nArg<=(int)Ar
30d0d 72 61 79 53 69 7a 65 28 61 53 74 61 74 69 63 29  raySize(aStatic)
30d0e 20 7c 7c 20 61 41 72 67 3d 3d 61 44 79 6e 20 29   || aArg==aDyn )
30d0f 3b 0a 20 20 20 20 20 20 69 66 28 20 28 21 61 44  ;.      if( (!aD
30d10 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74  yn && nArg==(int
30d11 29 41 72 72 61 79 53 69 7a 65 28 61 53 74 61 74  )ArraySize(aStat
30d12 69 63 29 29 0a 20 20 20 20 20 20 20 7c 7c 20 28  ic)).       || (
30d13 61 44 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28 69  aDyn && nArg==(i
30d14 6e 74 29 28 73 71 6c 69 74 65 33 44 62 4d 61 6c  nt)(sqlite3DbMal
30d15 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61 44 79 6e  locSize(db, aDyn
30d16 29 2f 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29  )/sizeof(void*))
30d17 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  ).      ){.     
30d18 20 20 20 2f 2a 20 54 68 65 20 61 41 72 67 5b 5d     /* The aArg[]
30d19 20 61 72 72 61 79 20 6e 65 65 64 73 20 74 6f 20   array needs to 
30d1a 67 72 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20  grow. */.       
30d1b 20 76 6f 69 64 20 2a 2a 70 4e 65 77 20 3d 20 28   void **pNew = (
30d1c 76 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65 33 4d  void **)sqlite3M
30d1d 61 6c 6c 6f 63 28 6e 41 72 67 2a 73 69 7a 65 6f  alloc(nArg*sizeo
30d1e 66 28 76 6f 69 64 20 2a 29 2a 32 29 3b 0a 20 20  f(void *)*2);.  
30d1f 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29        if( pNew )
30d20 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63  {.          memc
30d21 70 79 28 70 4e 65 77 2c 20 61 41 72 67 2c 20 6e  py(pNew, aArg, n
30d22 41 72 67 2a 73 69 7a 65 6f 66 28 76 6f 69 64 20  Arg*sizeof(void 
30d23 2a 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  *));.          s
30d24 71 6c 69 74 65 33 5f 66 72 65 65 28 61 44 79 6e  qlite3_free(aDyn
30d25 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 44 79  );.          aDy
30d26 6e 20 3d 20 61 41 72 67 20 3d 20 70 4e 65 77 3b  n = aArg = pNew;
30d27 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
30d28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
30d29 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68  s occurs when th
30d2a 65 20 61 72 72 61 79 20 6f 66 20 63 6f 6e 74 65  e array of conte
30d2b 78 74 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74  xt pointers that
30d2c 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20   need to.       
30d2d 20 20 20 2a 2a 20 62 65 20 70 61 73 73 65 64 20     ** be passed 
30d2e 74 6f 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  to the unlock-no
30d2f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73  tify callback is
30d30 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
30d31 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 53  .          ** aS
30d32 74 61 74 69 63 5b 5d 20 61 72 72 61 79 20 61 6c  tatic[] array al
30d33 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73  located on the s
30d34 74 61 63 6b 20 61 6e 64 20 74 68 65 20 61 74 74  tack and the att
30d35 65 6d 70 74 20 74 6f 20 0a 20 20 20 20 20 20 20  empt to .       
30d36 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 61     ** allocate a
30d37 20 6c 61 72 67 65 72 20 61 72 72 61 79 20 66 72   larger array fr
30d38 6f 6d 20 74 68 65 20 68 65 61 70 20 68 61 73 20  om the heap has 
30d39 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 20 20  failed..        
30d3a 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a    **.          *
30d3b 2a 20 54 68 69 73 20 69 73 20 61 20 64 69 66 66  * This is a diff
30d3c 69 63 75 6c 74 20 73 69 74 75 61 74 69 6f 6e 20  icult situation 
30d3d 74 6f 20 68 61 6e 64 6c 65 2e 20 52 65 74 75 72  to handle. Retur
30d3e 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 0a 20 20  ning an error.  
30d3f 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 20          ** code 
30d40 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73  to the caller is
30d41 20 69 6e 73 75 66 66 69 63 69 65 6e 74 2c 20 61   insufficient, a
30d42 73 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72  s even if an err
30d43 6f 72 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20  or code.        
30d44 20 20 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64    ** is returned
30d45 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
30d46 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64   on connection d
30d47 62 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 0a  b will still be.
30d48 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6c 6f            ** clo
30d49 73 65 64 20 61 6e 64 20 74 68 65 20 75 6e 6c 6f  sed and the unlo
30d4a 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
30d4b 63 6b 73 20 6f 6e 20 62 6c 6f 63 6b 65 64 20 63  cks on blocked c
30d4c 6f 6e 6e 65 63 74 69 6f 6e 73 0a 20 20 20 20 20  onnections.     
30d4d 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 67 6f 20       ** will go 
30d4e 75 6e 69 73 73 75 65 64 2e 20 54 68 69 73 20 6d  unissued. This m
30d4f 69 67 68 74 20 63 61 75 73 65 20 74 68 65 20 61  ight cause the a
30d50 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 77 61  pplication to wa
30d51 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  it.          ** 
30d52 69 6e 64 65 66 69 6e 69 74 65 6c 79 20 66 6f 72  indefinitely for
30d53 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
30d54 79 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20  y callback that 
30d55 77 69 6c 6c 20 6e 65 76 65 72 20 0a 20 20 20 20  will never .    
30d56 20 20 20 20 20 20 2a 2a 20 61 72 72 69 76 65 2e        ** arrive.
30d57 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  .          **.  
30d58 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 73 74 65          ** Inste
30d59 61 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 75  ad, invoke the u
30d5a 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
30d5b 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20 63  lback with the c
30d5c 6f 6e 74 65 78 74 0a 20 20 20 20 20 20 20 20 20  ontext.         
30d5d 20 2a 2a 20 61 72 72 61 79 20 61 6c 72 65 61 64   ** array alread
30d5e 79 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 57  y accumulated. W
30d5f 65 20 63 61 6e 20 74 68 65 6e 20 63 6c 65 61 72  e can then clear
30d60 20 74 68 65 20 61 72 72 61 79 20 61 6e 64 0a 20   the array and. 
30d61 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 67 69           ** begi
30d62 6e 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 61  n accumulating a
30d63 6e 79 20 66 75 72 74 68 65 72 20 63 6f 6e 74 65  ny further conte
30d64 78 74 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68  xt pointers with
30d65 6f 75 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a  out .          *
30d66 2a 20 72 65 71 75 69 72 69 6e 67 20 61 6e 79 20  * requiring any 
30d67 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69  dynamic allocati
30d68 6f 6e 2e 20 54 68 69 73 20 69 73 20 73 75 62 2d  on. This is sub-
30d69 6f 70 74 69 6d 61 6c 20 62 65 63 61 75 73 65 0a  optimal because.
30d6a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20            ** it 
30d6b 6d 65 61 6e 73 20 74 68 61 74 20 69 6e 73 74 65  means that inste
30d6c 61 64 20 6f 66 20 6f 6e 65 20 63 61 6c 6c 62 61  ad of one callba
30d6d 63 6b 20 77 69 74 68 20 61 20 6c 61 72 67 65 20  ck with a large 
30d6e 61 72 72 61 79 20 6f 66 0a 20 20 20 20 20 20 20  array of.       
30d6f 20 20 20 2a 2a 20 63 6f 6e 74 65 78 74 20 70 6f     ** context po
30d70 69 6e 74 65 72 73 20 74 68 65 20 61 70 70 6c 69  inters the appli
30d71 63 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 63 65  cation will rece
30d72 69 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a  ive two or more.
30d73 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6c            ** cal
30d74 6c 62 61 63 6b 73 20 77 69 74 68 20 73 6d 61 6c  lbacks with smal
30d75 6c 65 72 20 61 72 72 61 79 73 20 6f 66 20 63 6f  ler arrays of co
30d76 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 2c 20  ntext pointers, 
30d77 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 20  which will.     
30d78 20 20 20 20 20 2a 2a 20 72 65 64 75 63 65 20 74       ** reduce t
30d79 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  he applications 
30d7a 61 62 69 6c 69 74 79 20 74 6f 20 70 72 69 6f 72  ability to prior
30d7b 69 74 69 7a 65 20 6d 75 6c 74 69 70 6c 65 20 0a  itize multiple .
30d7c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e            ** con
30d7d 6e 65 63 74 69 6f 6e 73 2e 20 42 75 74 20 69 74  nections. But it
30d7e 20 69 73 20 74 68 65 20 62 65 73 74 20 74 68 61   is the best tha
30d7f 74 20 63 61 6e 20 62 65 20 64 6f 6e 65 20 75 6e  t can be done un
30d80 64 65 72 20 74 68 65 0a 20 20 20 20 20 20 20 20  der the.        
30d81 20 20 2a 2a 20 63 69 72 63 75 6d 73 74 61 6e 63    ** circumstanc
30d82 65 73 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  es..          */
30d83 0a 20 20 20 20 20 20 20 20 20 20 78 55 6e 6c 6f  .          xUnlo
30d84 63 6b 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e  ckNotify(aArg, n
30d85 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Arg);.          
30d86 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  nArg = 0;.      
30d87 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
30d88 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
30d89 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 20  gnMalloc();..   
30d8a 20 20 20 61 41 72 67 5b 6e 41 72 67 2b 2b 5d 20     aArg[nArg++] 
30d8b 3d 20 70 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 3b  = p->pUnlockArg;
30d8c 0a 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f  .      xUnlockNo
30d8d 74 69 66 79 20 3d 20 70 2d 3e 78 55 6e 6c 6f 63  tify = p->xUnloc
30d8e 6b 4e 6f 74 69 66 79 3b 0a 20 20 20 20 20 20 70  kNotify;.      p
30d8f 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
30d90 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ion = 0;.      p
30d91 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20  ->xUnlockNotify 
30d92 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 55  = 0;.      p->pU
30d93 6e 6c 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20  nlockArg = 0;.  
30d94 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70    }..    /* Step
30d95 20 33 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70   3. */.    if( p
30d96 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  ->pBlockingConne
30d97 63 74 69 6f 6e 3d 3d 30 20 26 26 20 70 2d 3e 70  ction==0 && p->p
30d98 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
30d99 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
30d9a 52 65 6d 6f 76 65 20 63 6f 6e 6e 65 63 74 69 6f  Remove connectio
30d9b 6e 20 70 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f  n p from the blo
30d9c 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
30d9d 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20   list. */.      
30d9e 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 42 6c  *pp = p->pNextBl
30d9f 6f 63 6b 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e  ocked;.      p->
30da0 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20 30  pNextBlocked = 0
30da1 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
30da2 20 20 20 70 70 20 3d 20 26 70 2d 3e 70 4e 65 78     pp = &p->pNex
30da3 74 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 7d 0a  tBlocked;.    }.
30da4 20 20 7d 0a 0a 20 20 69 66 28 20 6e 41 72 67 21    }..  if( nArg!
30da5 3d 30 20 29 7b 0a 20 20 20 20 78 55 6e 6c 6f 63  =0 ){.    xUnloc
30da6 6b 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41  kNotify(aArg, nA
30da7 72 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  rg);.  }.  sqlit
30da8 65 33 5f 66 72 65 65 28 61 44 79 6e 29 3b 0a 20  e3_free(aDyn);. 
30da9 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20   leaveMutex();  
30daa 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20         /* Leave 
30dab 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75  STATIC_MASTER mu
30dac 74 65 78 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex */.}../*.** 
30dad 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 77  This is called w
30dae 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
30daf 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73   connection pass
30db0 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
30db1 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 63  t is .** being c
30db2 6c 6f 73 65 64 2e 20 54 68 65 20 63 6f 6e 6e 65  losed. The conne
30db3 63 74 69 6f 6e 20 69 73 20 72 65 6d 6f 76 65 64  ction is removed
30db4 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65   from the blocke
30db5 64 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54  d list..*/.SQLIT
30db6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
30db7 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
30db8 43 6c 6f 73 65 64 28 73 71 6c 69 74 65 33 20 2a  Closed(sqlite3 *
30db9 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 43 6f  db){.  sqlite3Co
30dba 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64  nnectionUnlocked
30dbb 28 64 62 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74  (db);.  enterMut
30dbc 65 78 28 29 3b 0a 20 20 72 65 6d 6f 76 65 46 72  ex();.  removeFr
30dbd 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62  omBlockedList(db
30dbe 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74 50 72  );.  checkListPr
30dbf 6f 70 65 72 74 69 65 73 28 64 62 29 3b 0a 20 20  operties(db);.  
30dc0 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a  leaveMutex();.}.
30dc1 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
30dc2 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6e  ******* End of n
30dc3 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  otify.c ********
30dc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dc6 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
30dc7 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
30dc8 6c 65 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a  le fts3.c ******
30dc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dcb 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
30dcc 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20  06 Oct 10.**.** 
30dcd 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
30dce 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
30dcf 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
30dd0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
30dd1 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
30dd2 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
30dd3 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
30dd4 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
30dd5 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
30dd6 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
30dd7 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
30dd8 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
30dd9 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
30dda 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
30ddb 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
30ddc 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
30ddd 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
30dde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30ddf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30de1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30de2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
30de3 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 53  .** This is an S
30de4 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70  QLite module imp
30de5 6c 65 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74  lementing full-t
30de6 65 78 74 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 0a  ext search..*/..
30de7 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  /*.** The code i
30de8 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
30de9 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a  nly compiled if:
30dea 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
30deb 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
30dec 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
30ded 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20  n extension.**  
30dee 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63       (in which c
30def 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ase SQLITE_CORE 
30df0 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
30df1 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20   or.**.**     * 
30df2 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
30df3 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69  is being built i
30df4 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a  nto the core of.
30df5 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20  **       SQLite 
30df6 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
30df7 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
30df8 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a  3 is defined)..*
30df9 2f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  /../* TODO(shess
30dfa 29 20 43 6f 6e 73 69 64 65 72 20 65 78 70 6f 72  ) Consider expor
30dfb 74 69 6e 67 20 74 68 69 73 20 63 6f 6d 6d 65 6e  ting this commen
30dfc 74 20 74 6f 20 61 6e 20 48 54 4d 4c 20 66 69 6c  t to an HTML fil
30dfd 65 20 6f 72 20 74 68 65 0a 2a 2a 20 77 69 6b 69  e or the.** wiki
30dfe 2e 0a 2a 2f 0a 2f 2a 20 54 68 65 20 66 75 6c 6c  ..*/./* The full
30dff 2d 74 65 78 74 20 69 6e 64 65 78 20 69 73 20 73  -text index is s
30e00 74 6f 72 65 64 20 69 6e 20 61 20 73 65 72 69 65  tored in a serie
30e01 73 20 6f 66 20 62 2b 74 72 65 65 20 28 2d 6c 69  s of b+tree (-li
30e02 6b 65 29 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ke).** structure
30e03 73 20 63 61 6c 6c 65 64 20 73 65 67 6d 65 6e 74  s called segment
30e04 73 20 77 68 69 63 68 20 6d 61 70 20 74 65 72 6d  s which map term
30e05 73 20 74 6f 20 64 6f 63 6c 69 73 74 73 2e 20 20  s to doclists.  
30e06 54 68 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  The.** structure
30e07 73 20 61 72 65 20 6c 69 6b 65 20 62 2b 74 72 65  s are like b+tre
30e08 65 73 20 69 6e 20 6c 61 79 6f 75 74 2c 20 62 75  es in layout, bu
30e09 74 20 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65  t are constructe
30e0a 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f  d from the.** bo
30e0b 74 74 6f 6d 20 75 70 20 69 6e 20 6f 70 74 69 6d  ttom up in optim
30e0c 61 6c 20 66 61 73 68 69 6f 6e 20 61 6e 64 20 61  al fashion and a
30e0d 72 65 20 6e 6f 74 20 75 70 64 61 74 61 62 6c 65  re not updatable
30e0e 2e 20 20 53 69 6e 63 65 20 74 72 65 65 73 0a 2a  .  Since trees.*
30e0f 2a 20 61 72 65 20 62 75 69 6c 74 20 66 72 6f 6d  * are built from
30e10 20 74 68 65 20 62 6f 74 74 6f 6d 20 75 70 2c 20   the bottom up, 
30e11 74 68 69 6e 67 73 20 77 69 6c 6c 20 62 65 20 64  things will be d
30e12 65 73 63 72 69 62 65 64 20 66 72 6f 6d 20 74 68  escribed from th
30e13 65 0a 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 2e 0a  e.** bottom up..
30e14 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 56 61 72 69 6e  **.**.**** Varin
30e15 74 73 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 62  ts ****.** The b
30e16 61 73 69 63 20 75 6e 69 74 20 6f 66 20 65 6e 63  asic unit of enc
30e17 6f 64 69 6e 67 20 69 73 20 61 20 76 61 72 69 61  oding is a varia
30e18 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
30e19 65 72 20 63 61 6c 6c 65 64 20 61 0a 2a 2a 20 76  er called a.** v
30e1a 61 72 69 6e 74 2e 20 20 57 65 20 65 6e 63 6f 64  arint.  We encod
30e1b 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  e variable-lengt
30e1c 68 20 69 6e 74 65 67 65 72 73 20 69 6e 20 6c 69  h integers in li
30e1d 74 74 6c 65 2d 65 6e 64 69 61 6e 20 6f 72 64 65  ttle-endian orde
30e1e 72 0a 2a 2a 20 75 73 69 6e 67 20 73 65 76 65 6e  r.** using seven
30e1f 20 62 69 74 73 20 2a 20 70 65 72 20 62 79 74 65   bits * per byte
30e20 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
30e21 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20  ** KEY:.**      
30e22 20 20 20 41 20 3d 20 30 78 78 78 78 78 78 78 20     A = 0xxxxxxx 
30e23 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 74     7 bits of dat
30e24 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62  a and one flag b
30e25 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20  it.**         B 
30e26 3d 20 31 78 78 78 78 78 78 78 20 20 20 20 37 20  = 1xxxxxxx    7 
30e27 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64  bits of data and
30e28 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a   one flag bit.**
30e29 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a  .**  7 bits - A.
30e2a 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a  ** 14 bits - BA.
30e2b 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42 41  ** 21 bits - BBA
30e2c 0a 2a 2a 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a  .** and so on..*
30e2d 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 69 64 65  *.** This is ide
30e2e 6e 74 69 63 61 6c 20 74 6f 20 68 6f 77 20 73 71  ntical to how sq
30e2f 6c 69 74 65 20 65 6e 63 6f 64 65 73 20 76 61 72  lite encodes var
30e30 69 6e 74 73 20 28 73 65 65 20 75 74 69 6c 2e 63  ints (see util.c
30e31 29 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f  )..**.**.**** Do
30e32 63 75 6d 65 6e 74 20 6c 69 73 74 73 20 2a 2a 2a  cument lists ***
30e33 2a 0a 2a 2a 20 41 20 64 6f 63 6c 69 73 74 20 28  *.** A doclist (
30e34 64 6f 63 75 6d 65 6e 74 20 6c 69 73 74 29 20 68  document list) h
30e35 6f 6c 64 73 20 61 20 64 6f 63 69 64 2d 73 6f 72  olds a docid-sor
30e36 74 65 64 20 6c 69 73 74 20 6f 66 20 68 69 74 73  ted list of hits
30e37 20 66 6f 72 20 61 0a 2a 2a 20 67 69 76 65 6e 20   for a.** given 
30e38 74 65 72 6d 2e 20 20 44 6f 63 6c 69 73 74 73 20  term.  Doclists 
30e39 68 6f 6c 64 20 64 6f 63 69 64 73 2c 20 61 6e 64  hold docids, and
30e3a 20 63 61 6e 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   can optionally 
30e3b 61 73 73 6f 63 69 61 74 65 0a 2a 2a 20 74 6f 6b  associate.** tok
30e3c 65 6e 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64  en positions and
30e3d 20 6f 66 66 73 65 74 73 20 77 69 74 68 20 64 6f   offsets with do
30e3e 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c  cids..**.** A DL
30e3f 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45  _POSITIONS_OFFSE
30e40 54 53 20 64 6f 63 6c 69 73 74 20 69 73 20 73 74  TS doclist is st
30e41 6f 72 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ored like this:.
30e42 2a 2a 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a  **.** array {.**
30e43 20 20 20 76 61 72 69 6e 74 20 64 6f 63 69 64 3b     varint docid;
30e44 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 20 20 20  .**   array {   
30e45 20 20 20 20 20 20 20 20 20 20 20 20 20 28 70 6f               (po
30e46 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20  sition list for 
30e47 63 6f 6c 75 6d 6e 20 30 29 0a 2a 2a 20 20 20 20  column 0).**    
30e48 20 76 61 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e   varint position
30e49 3b 20 20 20 20 20 28 64 65 6c 74 61 20 66 72 6f  ;     (delta fro
30e4a 6d 20 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74  m previous posit
30e4b 69 6f 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53  ion plus POS_BAS
30e4c 45 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74  E).**     varint
30e4d 20 73 74 61 72 74 4f 66 66 73 65 74 3b 20 20 28   startOffset;  (
30e4e 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69  delta from previ
30e4f 6f 75 73 20 73 74 61 72 74 4f 66 66 73 65 74 29  ous startOffset)
30e50 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 65  .**     varint e
30e51 6e 64 4f 66 66 73 65 74 3b 20 20 20 20 28 64 65  ndOffset;    (de
30e52 6c 74 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66  lta from startOf
30e53 66 73 65 74 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20  fset).**   }.** 
30e54 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20    array {.**    
30e55 20 76 61 72 69 6e 74 20 50 4f 53 5f 43 4f 4c 55   varint POS_COLU
30e56 4d 4e 3b 20 20 20 28 6d 61 72 6b 73 20 73 74 61  MN;   (marks sta
30e57 72 74 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c  rt of position l
30e58 69 73 74 20 66 6f 72 20 6e 65 77 20 63 6f 6c 75  ist for new colu
30e59 6d 6e 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e  mn).**     varin
30e5a 74 20 63 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  t column;       
30e5b 28 69 6e 64 65 78 20 6f 66 20 6e 65 77 20 63 6f  (index of new co
30e5c 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20 20 61 72 72  lumn).**     arr
30e5d 61 79 20 7b 0a 2a 2a 20 20 20 20 20 20 20 76 61  ay {.**       va
30e5e 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20  rint position;  
30e5f 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65   (delta from pre
30e60 76 69 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20 70  vious position p
30e61 6c 75 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a 2a  lus POS_BASE).**
30e62 20 20 20 20 20 20 20 76 61 72 69 6e 74 20 73 74         varint st
30e63 61 72 74 4f 66 66 73 65 74 3b 28 64 65 6c 74 61  artOffset;(delta
30e64 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 73   from previous s
30e65 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20  tartOffset).**  
30e66 20 20 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f       varint endO
30e67 66 66 73 65 74 3b 20 20 28 64 65 6c 74 61 20 66  ffset;  (delta f
30e68 72 6f 6d 20 73 74 61 72 74 4f 66 66 73 65 74 29  rom startOffset)
30e69 0a 2a 2a 20 20 20 20 20 7d 0a 2a 2a 20 20 20 7d  .**     }.**   }
30e6a 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 50 4f 53  .**   varint POS
30e6b 5f 45 4e 44 3b 20 20 20 20 20 20 20 20 28 6d 61  _END;        (ma
30e6c 72 6b 73 20 65 6e 64 20 6f 66 20 70 6f 73 69 74  rks end of posit
30e6d 69 6f 6e 73 20 66 6f 72 20 74 68 69 73 20 64 6f  ions for this do
30e6e 63 75 6d 65 6e 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a  cument..** }.**.
30e6f 2a 2a 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b  ** Here, array {
30e70 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20   X } means zero 
30e71 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e  or more occurren
30e72 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65  ces of X, adjace
30e73 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e  nt in.** memory.
30e74 20 20 41 20 22 70 6f 73 69 74 69 6f 6e 22 20 69    A "position" i
30e75 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 61 20  s an index of a 
30e76 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 74 6f 6b  token in the tok
30e77 65 6e 20 73 74 72 65 61 6d 0a 2a 2a 20 67 65 6e  en stream.** gen
30e78 65 72 61 74 65 64 20 62 79 20 74 68 65 20 74 6f  erated by the to
30e79 6b 65 6e 69 7a 65 72 2c 20 77 68 69 6c 65 20 61  kenizer, while a
30e7a 6e 20 22 6f 66 66 73 65 74 22 20 69 73 20 61 20  n "offset" is a 
30e7b 62 79 74 65 20 6f 66 66 73 65 74 2c 0a 2a 2a 20  byte offset,.** 
30e7c 62 6f 74 68 20 62 61 73 65 64 20 61 74 20 30 2e  both based at 0.
30e7d 20 20 4e 6f 74 65 20 74 68 61 74 20 50 4f 53 5f    Note that POS_
30e7e 45 4e 44 20 61 6e 64 20 50 4f 53 5f 43 4f 4c 55  END and POS_COLU
30e7f 4d 4e 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a  MN occur in the.
30e80 2a 2a 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20  ** same logical 
30e81 70 6c 61 63 65 20 61 73 20 74 68 65 20 70 6f 73  place as the pos
30e82 69 74 69 6f 6e 20 65 6c 65 6d 65 6e 74 2c 20 61  ition element, a
30e83 6e 64 20 61 63 74 20 61 73 20 73 65 6e 74 69 6e  nd act as sentin
30e84 61 6c 73 0a 2a 2a 20 65 6e 64 69 6e 67 20 61 20  als.** ending a 
30e85 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 72  position list ar
30e86 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f  ray..**.** A DL_
30e87 50 4f 53 49 54 49 4f 4e 53 20 64 6f 63 6c 69 73  POSITIONS doclis
30e88 74 20 6f 6d 69 74 73 20 74 68 65 20 73 74 61 72  t omits the star
30e89 74 4f 66 66 73 65 74 20 61 6e 64 20 65 6e 64 4f  tOffset and endO
30e8a 66 66 73 65 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ffset.** informa
30e8b 74 69 6f 6e 2e 20 20 41 20 44 4c 5f 44 4f 43 49  tion.  A DL_DOCI
30e8c 44 53 20 64 6f 63 6c 69 73 74 20 6f 6d 69 74 73  DS doclist omits
30e8d 20 62 6f 74 68 20 74 68 65 20 70 6f 73 69 74 69   both the positi
30e8e 6f 6e 20 61 6e 64 0a 2a 2a 20 6f 66 66 73 65 74  on and.** offset
30e8f 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 62 65   information, be
30e90 63 6f 6d 69 6e 67 20 61 6e 20 61 72 72 61 79 20  coming an array 
30e91 6f 66 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65  of varint-encode
30e92 64 20 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20  d docids..**.** 
30e93 4f 6e 2d 64 69 73 6b 20 64 61 74 61 20 69 73 20  On-disk data is 
30e94 73 74 6f 72 65 64 20 61 73 20 74 79 70 65 20 44  stored as type D
30e95 4c 5f 44 45 46 41 55 4c 54 2c 20 73 6f 20 77 65  L_DEFAULT, so we
30e96 20 64 6f 6e 27 74 20 73 65 72 69 61 6c 69 7a 65   don't serialize
30e97 0a 2a 2a 20 74 68 65 20 74 79 70 65 2e 20 20 44  .** the type.  D
30e98 75 65 20 74 6f 20 68 6f 77 20 64 65 6c 65 74 69  ue to how deleti
30e99 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  on is implemente
30e9a 64 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  d in the segment
30e9b 61 74 69 6f 6e 0a 2a 2a 20 73 79 73 74 65 6d 2c  ation.** system,
30e9c 20 6f 6e 2d 64 69 73 6b 20 64 6f 63 6c 69 73 74   on-disk doclist
30e9d 73 20 4d 55 53 54 20 73 74 6f 72 65 20 61 74 20  s MUST store at 
30e9e 6c 65 61 73 74 20 70 6f 73 69 74 69 6f 6e 73 2e  least positions.
30e9f 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d  .**.**.**** Segm
30ea0 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 2a  ent leaf nodes *
30ea1 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c  ***.** Segment l
30ea2 65 61 66 20 6e 6f 64 65 73 20 73 74 6f 72 65 20  eaf nodes store 
30ea3 74 65 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69 73  terms and doclis
30ea4 74 73 2c 20 6f 72 64 65 72 65 64 20 62 79 20 74  ts, ordered by t
30ea5 65 72 6d 2e 20 20 4c 65 61 66 0a 2a 2a 20 6e 6f  erm.  Leaf.** no
30ea6 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20  des are written 
30ea7 75 73 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72  using LeafWriter
30ea8 2c 20 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67  , and read using
30ea9 20 4c 65 61 66 52 65 61 64 65 72 20 28 74 6f 0a   LeafReader (to.
30eaa 2a 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f 75  ** iterate throu
30eab 67 68 20 61 20 73 69 6e 67 6c 65 20 6c 65 61 66  gh a single leaf
30eac 20 6e 6f 64 65 27 73 20 64 61 74 61 29 20 61 6e   node's data) an
30ead 64 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 28  d LeavesReader (
30eae 74 6f 0a 2a 2a 20 69 74 65 72 61 74 65 20 74 68  to.** iterate th
30eaf 72 6f 75 67 68 20 61 20 73 65 67 6d 65 6e 74 27  rough a segment'
30eb0 73 20 65 6e 74 69 72 65 20 6c 65 61 66 20 6c 61  s entire leaf la
30eb1 79 65 72 29 2e 20 20 4c 65 61 66 20 6e 6f 64 65  yer).  Leaf node
30eb2 73 20 68 61 76 65 0a 2a 2a 20 74 68 65 20 66 6f  s have.** the fo
30eb3 72 6d 61 74 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69  rmat:.**.** vari
30eb4 6e 74 20 69 48 65 69 67 68 74 3b 20 20 20 20 20  nt iHeight;     
30eb5 20 20 20 20 20 20 20 20 28 68 65 69 67 68 74 20          (height 
30eb6 66 72 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c  from leaf level,
30eb7 20 61 6c 77 61 79 73 20 30 29 0a 2a 2a 20 76 61   always 0).** va
30eb8 72 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20  rint nTerm;     
30eb9 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
30eba 68 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29  h of first term)
30ebb 0a 2a 2a 20 63 68 61 72 20 70 54 65 72 6d 5b 6e  .** char pTerm[n
30ebc 54 65 72 6d 5d 3b 20 20 20 20 20 20 20 20 20 20  Term];          
30ebd 28 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73  (content of firs
30ebe 74 20 74 65 72 6d 29 0a 2a 2a 20 76 61 72 69 6e  t term).** varin
30ebf 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20  t nDoclist;     
30ec0 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f         (length o
30ec1 66 20 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61  f term's associa
30ec2 74 65 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20  ted doclist).** 
30ec3 63 68 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44  char pDoclist[nD
30ec4 6f 63 6c 69 73 74 5d 3b 20 20 20 20 28 63 6f 6e  oclist];    (con
30ec5 74 65 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29  tent of doclist)
30ec6 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20  .** array {.**  
30ec7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ec8 20 20 20 20 20 20 20 20 20 20 20 28 66 75 72 74             (furt
30ec9 68 65 72 20 74 65 72 6d 73 20 61 72 65 20 64 65  her terms are de
30eca 6c 74 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a 20  lta-encoded).** 
30ecb 20 20 76 61 72 69 6e 74 20 6e 50 72 65 66 69 78    varint nPrefix
30ecc 3b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e  ;           (len
30ecd 67 74 68 20 6f 66 20 70 72 65 66 69 78 20 73 68  gth of prefix sh
30ece 61 72 65 64 20 77 69 74 68 20 70 72 65 76 69 6f  ared with previo
30ecf 75 73 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61  us term).**   va
30ed0 72 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20  rint nSuffix;   
30ed1 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
30ed2 6f 66 20 75 6e 73 68 61 72 65 64 20 73 75 66 66  of unshared suff
30ed3 69 78 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 54  ix).**   char pT
30ed4 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69  ermSuffix[nSuffi
30ed5 78 5d 3b 28 75 6e 73 68 61 72 65 64 20 73 75 66  x];(unshared suf
30ed6 66 69 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d  fix of next term
30ed7 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 44  ).**   varint nD
30ed8 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20  oclist;         
30ed9 20 28 6c 65 6e 67 74 68 20 6f 66 20 74 65 72 6d   (length of term
30eda 27 73 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f  's associated do
30edb 63 6c 69 73 74 29 0a 2a 2a 20 20 20 63 68 61 72  clist).**   char
30edc 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69   pDoclist[nDocli
30edd 73 74 5d 3b 20 20 28 63 6f 6e 74 65 6e 74 20 6f  st];  (content o
30ede 66 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 7d 0a  f doclist).** }.
30edf 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72 61  **.** Here, arra
30ee0 79 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65  y { X } means ze
30ee1 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72  ro or more occur
30ee2 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a  rences of X, adj
30ee3 61 63 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f  acent in.** memo
30ee4 72 79 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 20 6e  ry..**.** Leaf n
30ee5 6f 64 65 73 20 61 72 65 20 62 72 6f 6b 65 6e 20  odes are broken 
30ee6 69 6e 74 6f 20 62 6c 6f 63 6b 73 20 77 68 69 63  into blocks whic
30ee7 68 20 61 72 65 20 73 74 6f 72 65 64 20 63 6f 6e  h are stored con
30ee8 74 69 67 75 6f 75 73 6c 79 20 69 6e 0a 2a 2a 20  tiguously in.** 
30ee9 74 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74  the %_segments t
30eea 61 62 6c 65 20 69 6e 20 73 6f 72 74 65 64 20 6f  able in sorted o
30eeb 72 64 65 72 2e 20 20 54 68 69 73 20 6d 65 61 6e  rder.  This mean
30eec 73 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 20  s that when the 
30eed 65 6e 64 0a 2a 2a 20 6f 66 20 61 20 6e 6f 64 65  end.** of a node
30eee 20 69 73 20 72 65 61 63 68 65 64 2c 20 74 68 65   is reached, the
30eef 20 6e 65 78 74 20 74 65 72 6d 20 69 73 20 69 6e   next term is in
30ef0 20 74 68 65 20 6e 6f 64 65 20 77 69 74 68 20 74   the node with t
30ef1 68 65 20 6e 65 78 74 0a 2a 2a 20 67 72 65 61 74  he next.** great
30ef2 65 72 20 6e 6f 64 65 20 69 64 2e 0a 2a 2a 0a 2a  er node id..**.*
30ef3 2a 20 4e 65 77 20 64 61 74 61 20 69 73 20 73 70  * New data is sp
30ef4 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 6c  illed to a new l
30ef5 65 61 66 20 6e 6f 64 65 20 77 68 65 6e 20 74 68  eaf node when th
30ef6 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 0a 2a  e current node.*
30ef7 2a 20 65 78 63 65 65 64 73 20 4c 45 41 46 5f 4d  * exceeds LEAF_M
30ef8 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75 6c  AX bytes (defaul
30ef9 74 20 32 30 34 38 29 2e 20 20 4e 65 77 20 64 61  t 2048).  New da
30efa 74 61 20 77 68 69 63 68 20 69 74 73 65 6c 66 20  ta which itself 
30efb 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61  is.** larger tha
30efc 6e 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e  n STANDALONE_MIN
30efd 20 28 64 65 66 61 75 6c 74 20 31 30 32 34 29 20   (default 1024) 
30efe 69 73 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73  is placed in a s
30eff 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64  tandalone.** nod
30f00 65 20 28 61 20 6c 65 61 66 20 6e 6f 64 65 20 77  e (a leaf node w
30f01 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 65 72  ith a single ter
30f02 6d 20 61 6e 64 20 64 6f 63 6c 69 73 74 29 2e 20  m and doclist). 
30f03 20 54 68 65 20 67 6f 61 6c 20 6f 66 0a 2a 2a 20   The goal of.** 
30f04 74 68 65 73 65 20 73 65 74 74 69 6e 67 73 20 69  these settings i
30f05 73 20 74 6f 20 70 61 63 6b 20 74 6f 67 65 74 68  s to pack togeth
30f06 65 72 20 67 72 6f 75 70 73 20 6f 66 20 73 6d 61  er groups of sma
30f07 6c 6c 20 64 6f 63 6c 69 73 74 73 20 77 68 69 6c  ll doclists whil
30f08 65 0a 2a 2a 20 6d 61 6b 69 6e 67 20 69 74 20 65  e.** making it e
30f09 66 66 69 63 69 65 6e 74 20 74 6f 20 64 69 72 65  fficient to dire
30f0a 63 74 6c 79 20 61 63 63 65 73 73 20 6c 61 72 67  ctly access larg
30f0b 65 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65  e doclists.  The
30f0c 0a 2a 2a 20 61 73 73 75 6d 70 74 69 6f 6e 20 69  .** assumption i
30f0d 73 20 74 68 61 74 20 6c 61 72 67 65 20 64 6f 63  s that large doc
30f0e 6c 69 73 74 73 20 72 65 70 72 65 73 65 6e 74 20  lists represent 
30f0f 74 65 72 6d 73 20 77 68 69 63 68 20 61 72 65 20  terms which are 
30f10 6d 6f 72 65 0a 2a 2a 20 6c 69 6b 65 6c 79 20 74  more.** likely t
30f11 6f 20 62 65 20 71 75 65 72 79 20 74 61 72 67 65  o be query targe
30f12 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73  ts..**.** TODO(s
30f13 68 65 73 73 29 20 49 74 20 6d 61 79 20 62 65 20  hess) It may be 
30f14 75 73 65 66 75 6c 20 66 6f 72 20 62 6c 6f 63 6b  useful for block
30f15 69 6e 67 20 64 65 63 69 73 69 6f 6e 73 20 74 6f  ing decisions to
30f16 20 62 65 20 6d 6f 72 65 0a 2a 2a 20 64 79 6e 61   be more.** dyna
30f17 6d 69 63 2e 20 20 46 6f 72 20 69 6e 73 74 61 6e  mic.  For instan
30f18 63 65 2c 20 69 74 20 6d 61 79 20 6d 61 6b 65 20  ce, it may make 
30f19 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 68 61  more sense to ha
30f1a 76 65 20 61 20 32 2e 35 6b 20 6c 65 61 66 0a 2a  ve a 2.5k leaf.*
30f1b 2a 20 6e 6f 64 65 20 72 61 74 68 65 72 20 74 68  * node rather th
30f1c 61 6e 20 73 70 6c 69 74 74 69 6e 67 20 69 6e 74  an splitting int
30f1d 6f 20 32 6b 20 61 6e 64 20 2e 35 6b 20 6e 6f 64  o 2k and .5k nod
30f1e 65 73 2e 20 20 4d 79 20 69 6e 74 75 69 74 69 6f  es.  My intuitio
30f1f 6e 20 69 73 0a 2a 2a 20 74 68 61 74 20 74 68 69  n is.** that thi
30f20 73 20 6d 69 67 68 74 20 65 78 74 65 6e 64 20 74  s might extend t
30f21 68 72 6f 75 67 68 20 32 78 20 6f 72 20 34 78 20  hrough 2x or 4x 
30f22 74 68 65 20 70 61 67 65 73 69 7a 65 2e 0a 2a 2a  the pagesize..**
30f23 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74  .**.**** Segment
30f24 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
30f25 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20  ****.** Segment 
30f26 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 73  interior nodes s
30f27 74 6f 72 65 20 62 6c 6f 63 6b 69 64 73 20 66 6f  tore blockids fo
30f28 72 20 73 75 62 74 72 65 65 20 6e 6f 64 65 73 20  r subtree nodes 
30f29 61 6e 64 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20  and terms.** to 
30f2a 64 65 73 63 72 69 62 65 20 77 68 61 74 20 64 61  describe what da
30f2b 74 61 20 69 73 20 73 74 6f 72 65 64 20 62 79 20  ta is stored by 
30f2c 74 68 65 20 65 61 63 68 20 73 75 62 74 72 65 65  the each subtree
30f2d 2e 20 20 49 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e  .  Interior.** n
30f2e 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e  odes are written
30f2f 20 75 73 69 6e 67 20 49 6e 74 65 72 69 6f 72 57   using InteriorW
30f30 72 69 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20  riter, and read 
30f31 75 73 69 6e 67 0a 2a 2a 20 49 6e 74 65 72 69 6f  using.** Interio
30f32 72 52 65 61 64 65 72 2e 20 20 49 6e 74 65 72 69  rReader.  Interi
30f33 6f 72 57 72 69 74 65 72 73 20 61 72 65 20 63 72  orWriters are cr
30f34 65 61 74 65 64 20 61 73 20 6e 65 65 64 65 64 20  eated as needed 
30f35 77 68 65 6e 0a 2a 2a 20 53 65 67 6d 65 6e 74 57  when.** SegmentW
30f36 72 69 74 65 72 20 63 72 65 61 74 65 73 20 6e 65  riter creates ne
30f37 77 20 6c 65 61 66 20 6e 6f 64 65 73 2c 20 6f 72  w leaf nodes, or
30f38 20 77 68 65 6e 20 61 6e 20 69 6e 74 65 72 69 6f   when an interio
30f39 72 20 6e 6f 64 65 0a 2a 2a 20 69 74 73 65 6c 66  r node.** itself
30f3a 20 67 72 6f 77 73 20 74 6f 6f 20 62 69 67 20 61   grows too big a
30f3b 6e 64 20 6d 75 73 74 20 62 65 20 73 70 6c 69 74  nd must be split
30f3c 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66  .  The format of
30f3d 20 69 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64   interior.** nod
30f3e 65 73 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74  es:.**.** varint
30f3f 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20   iHeight;       
30f40 20 20 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d      (height from
30f41 20 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77   leaf level, alw
30f42 61 79 73 20 3e 30 29 0a 2a 2a 20 76 61 72 69 6e  ays >0).** varin
30f43 74 20 69 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20  t iBlockid;     
30f44 20 20 20 20 20 28 62 6c 6f 63 6b 20 69 64 20 6f       (block id o
30f45 66 20 6e 6f 64 65 27 73 20 6c 65 66 74 6d 6f 73  f node's leftmos
30f46 74 20 73 75 62 74 72 65 65 29 0a 2a 2a 20 6f 70  t subtree).** op
30f47 74 69 6f 6e 61 6c 20 7b 0a 2a 2a 20 20 20 76 61  tional {.**   va
30f48 72 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20  rint nTerm;     
30f49 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
30f4a 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20   first term).** 
30f4b 20 20 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65    char pTerm[nTe
30f4c 72 6d 5d 3b 20 20 20 20 20 20 28 63 6f 6e 74 65  rm];      (conte
30f4d 6e 74 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  nt of first term
30f4e 29 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a  ).**   array {.*
30f4f 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
30f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f51 20 28 66 75 72 74 68 65 72 20 74 65 72 6d 73 20   (further terms 
30f52 61 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65  are delta-encode
30f53 64 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74  d).**     varint
30f54 20 6e 50 72 65 66 69 78 3b 20 20 20 20 20 20 20   nPrefix;       
30f55 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
30f56 73 68 61 72 65 64 20 70 72 65 66 69 78 20 77 69  shared prefix wi
30f57 74 68 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d  th previous term
30f58 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20  ).**     varint 
30f59 6e 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20  nSuffix;        
30f5a 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75      (length of u
30f5b 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 29 0a  nshared suffix).
30f5c 2a 2a 20 20 20 20 20 63 68 61 72 20 70 54 65 72  **     char pTer
30f5d 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d  mSuffix[nSuffix]
30f5e 3b 20 28 75 6e 73 68 61 72 65 64 20 73 75 66 66  ; (unshared suff
30f5f 69 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29  ix of next term)
30f60 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a  .**   }.** }.**.
30f61 2a 2a 20 48 65 72 65 2c 20 6f 70 74 69 6f 6e 61  ** Here, optiona
30f62 6c 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 61 6e  l { X } means an
30f63 20 6f 70 74 69 6f 6e 61 6c 20 65 6c 65 6d 65 6e   optional elemen
30f64 74 2c 20 77 68 69 6c 65 20 61 72 72 61 79 20 7b  t, while array {
30f65 20 58 20 7d 0a 2a 2a 20 6d 65 61 6e 73 20 7a 65   X }.** means ze
30f66 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72  ro or more occur
30f67 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a  rences of X, adj
30f68 61 63 65 6e 74 20 69 6e 20 6d 65 6d 6f 72 79 2e  acent in memory.
30f69 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 72 69  .**.** An interi
30f6a 6f 72 20 6e 6f 64 65 20 65 6e 63 6f 64 65 73 20  or node encodes 
30f6b 6e 20 74 65 72 6d 73 20 73 65 70 61 72 61 74 69  n terms separati
30f6c 6e 67 20 6e 2b 31 20 73 75 62 74 72 65 65 73 2e  ng n+1 subtrees.
30f6d 20 20 54 68 65 0a 2a 2a 20 73 75 62 74 72 65 65    The.** subtree
30f6e 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 6e 74   blocks are cont
30f6f 69 67 75 6f 75 73 2c 20 73 6f 20 6f 6e 6c 79 20  iguous, so only 
30f70 74 68 65 20 66 69 72 73 74 20 73 75 62 74 72 65  the first subtre
30f71 65 27 73 20 62 6c 6f 63 6b 69 64 0a 2a 2a 20 69  e's blockid.** i
30f72 73 20 65 6e 63 6f 64 65 64 2e 20 20 54 68 65 20  s encoded.  The 
30f73 73 75 62 74 72 65 65 20 61 74 20 69 42 6c 6f 63  subtree at iBloc
30f74 6b 69 64 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e  kid will contain
30f75 20 61 6c 6c 20 74 65 72 6d 73 20 6c 65 73 73 0a   all terms less.
30f76 2a 2a 20 74 68 61 6e 20 74 68 65 20 66 69 72 73  ** than the firs
30f77 74 20 74 65 72 6d 20 65 6e 63 6f 64 65 64 20 28  t term encoded (
30f78 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20 69 66 20  or all terms if 
30f79 6e 6f 20 74 65 72 6d 20 69 73 20 65 6e 63 6f 64  no term is encod
30f7a 65 64 29 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ed)..** Otherwis
30f7b 65 2c 20 66 6f 72 20 74 65 72 6d 73 20 67 72 65  e, for terms gre
30f7c 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
30f7d 61 6c 20 74 6f 20 70 54 65 72 6d 5b 69 5d 20 62  al to pTerm[i] b
30f7e 75 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  ut less.** than 
30f7f 70 54 65 72 6d 5b 69 2b 31 5d 2c 20 74 68 65 20  pTerm[i+1], the 
30f80 73 75 62 74 72 65 65 20 66 6f 72 20 74 68 61 74  subtree for that
30f81 20 74 65 72 6d 20 77 69 6c 6c 20 62 65 20 72 6f   term will be ro
30f82 6f 74 65 64 20 61 74 0a 2a 2a 20 69 42 6c 6f 63  oted at.** iBloc
30f83 6b 69 64 2b 69 2e 20 20 49 6e 74 65 72 69 6f 72  kid+i.  Interior
30f84 20 6e 6f 64 65 73 20 6f 6e 6c 79 20 73 74 6f 72   nodes only stor
30f85 65 20 65 6e 6f 75 67 68 20 74 65 72 6d 20 64 61  e enough term da
30f86 74 61 20 74 6f 0a 2a 2a 20 64 69 73 74 69 6e 67  ta to.** disting
30f87 75 69 73 68 20 61 64 6a 61 63 65 6e 74 20 63 68  uish adjacent ch
30f88 69 6c 64 72 65 6e 20 28 69 66 20 74 68 65 20 72  ildren (if the r
30f89 69 67 68 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66  ightmost term of
30f8a 20 74 68 65 20 6c 65 66 74 0a 2a 2a 20 63 68 69   the left.** chi
30f8b 6c 64 20 69 73 20 22 73 6f 6d 65 74 68 69 6e 67  ld is "something
30f8c 22 2c 20 61 6e 64 20 74 68 65 20 6c 65 66 74 6d  ", and the leftm
30f8d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ost term of the 
30f8e 72 69 67 68 74 20 63 68 69 6c 64 20 69 73 0a 2a  right child is.*
30f8f 2a 20 22 77 69 63 6b 65 64 22 2c 20 6f 6e 6c 79  * "wicked", only
30f90 20 22 77 22 20 69 73 20 73 74 6f 72 65 64 29 2e   "w" is stored).
30f91 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20  .**.** New data 
30f92 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20  is spilled to a 
30f93 6e 65 77 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  new interior nod
30f94 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 68 65  e at the same he
30f95 69 67 68 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65  ight when.** the
30f96 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 20 65 78   current node ex
30f97 63 65 65 64 73 20 49 4e 54 45 52 49 4f 52 5f 4d  ceeds INTERIOR_M
30f98 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75 6c  AX bytes (defaul
30f99 74 20 32 30 34 38 29 2e 0a 2a 2a 20 49 4e 54 45  t 2048)..** INTE
30f9a 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 28  RIOR_MIN_TERMS (
30f9b 64 65 66 61 75 6c 74 20 37 29 20 6b 65 65 70 73  default 7) keeps
30f9c 20 6c 61 72 67 65 20 74 65 72 6d 73 20 66 72 6f   large terms fro
30f9d 6d 20 6d 6f 6e 6f 70 6f 6c 69 7a 69 6e 67 0a 2a  m monopolizing.*
30f9e 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73  * interior nodes
30f9f 20 61 6e 64 20 6d 61 6b 69 6e 67 20 74 68 65 20   and making the 
30fa0 74 72 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79 2e  tree too skinny.
30fa1 20 20 54 68 65 20 69 6e 74 65 72 69 6f 72 20 6e    The interior n
30fa2 6f 64 65 73 0a 2a 2a 20 61 74 20 61 20 67 69 76  odes.** at a giv
30fa3 65 6e 20 68 65 69 67 68 74 20 61 72 65 20 6e 61  en height are na
30fa4 74 75 72 61 6c 6c 79 20 74 72 61 63 6b 65 64 20  turally tracked 
30fa5 62 79 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  by interior node
30fa6 73 20 61 74 0a 2a 2a 20 68 65 69 67 68 74 2b 31  s at.** height+1
30fa7 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  , and so on..**.
30fa8 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20  **.**** Segment 
30fa9 64 69 72 65 63 74 6f 72 79 20 2a 2a 2a 2a 0a 2a  directory ****.*
30faa 2a 20 54 68 65 20 73 65 67 6d 65 6e 74 20 64 69  * The segment di
30fab 72 65 63 74 6f 72 79 20 69 6e 20 74 61 62 6c 65  rectory in table
30fac 20 25 5f 73 65 67 64 69 72 20 73 74 6f 72 65 73   %_segdir stores
30fad 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f   meta-informatio
30fae 6e 20 66 6f 72 0a 2a 2a 20 6d 65 72 67 69 6e 67  n for.** merging
30faf 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 20 73 65   and deleting se
30fb0 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f  gments, and also
30fb1 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f   the root node o
30fb2 66 20 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74  f the.** segment
30fb3 27 73 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54  's tree..**.** T
30fb4 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20  he root node is 
30fb5 74 68 65 20 74 6f 70 20 6e 6f 64 65 20 6f 66 20  the top node of 
30fb6 74 68 65 20 73 65 67 6d 65 6e 74 27 73 20 74 72  the segment's tr
30fb7 65 65 20 61 66 74 65 72 20 65 6e 63 6f 64 69 6e  ee after encodin
30fb8 67 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20  g.** the entire 
30fb9 73 65 67 6d 65 6e 74 2c 20 72 65 73 74 72 69 63  segment, restric
30fba 74 65 64 20 74 6f 20 52 4f 4f 54 5f 4d 41 58 20  ted to ROOT_MAX 
30fbb 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20 31  bytes (default 1
30fbc 30 32 34 29 2e 0a 2a 2a 20 54 68 69 73 20 63 6f  024)..** This co
30fbd 75 6c 64 20 62 65 20 65 69 74 68 65 72 20 61 20  uld be either a 
30fbe 6c 65 61 66 20 6e 6f 64 65 20 6f 72 20 61 6e 20  leaf node or an 
30fbf 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20  interior node.  
30fc0 49 66 20 74 68 65 20 74 6f 70 0a 2a 2a 20 6e 6f  If the top.** no
30fc1 64 65 20 72 65 71 75 69 72 65 73 20 6d 6f 72 65  de requires more
30fc2 20 74 68 61 6e 20 52 4f 4f 54 5f 4d 41 58 20 62   than ROOT_MAX b
30fc3 79 74 65 73 2c 20 69 74 20 69 73 20 66 6c 75 73  ytes, it is flus
30fc4 68 65 64 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74  hed to %_segment
30fc5 73 0a 2a 2a 20 61 6e 64 20 61 20 6e 65 77 20 72  s.** and a new r
30fc6 6f 6f 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  oot interior nod
30fc7 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 28  e is generated (
30fc8 77 68 69 63 68 20 73 68 6f 75 6c 64 20 61 6c 77  which should alw
30fc9 61 79 73 20 66 69 74 0a 2a 2a 20 77 69 74 68 69  ays fit.** withi
30fca 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 65 63 61 75  n ROOT_MAX becau
30fcb 73 65 20 69 74 20 6f 6e 6c 79 20 6e 65 65 64 73  se it only needs
30fcc 20 73 70 61 63 65 20 66 6f 72 20 32 20 76 61 72   space for 2 var
30fcd 69 6e 74 73 2c 20 74 68 65 0a 2a 2a 20 68 65 69  ints, the.** hei
30fce 67 68 74 20 61 6e 64 20 74 68 65 20 62 6c 6f 63  ght and the bloc
30fcf 6b 69 64 20 6f 66 20 74 68 65 20 70 72 65 76 69  kid of the previ
30fd0 6f 75 73 20 72 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a  ous root)..**.**
30fd1 20 54 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d   The meta-inform
30fd2 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 65 67  ation in the seg
30fd3 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69  ment directory i
30fd4 73 3a 0a 2a 2a 20 20 20 6c 65 76 65 6c 20 20 20  s:.**   level   
30fd5 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 73 65              - se
30fd6 67 6d 65 6e 74 20 6c 65 76 65 6c 20 28 73 65 65  gment level (see
30fd7 20 62 65 6c 6f 77 29 0a 2a 2a 20 20 20 69 64 78   below).**   idx
30fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30fd9 20 2d 20 69 6e 64 65 78 20 77 69 74 68 69 6e 20   - index within 
30fda 6c 65 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20  level.**        
30fdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
30fdc 20 28 6c 65 76 65 6c 2c 69 64 78 20 75 6e 69 71   (level,idx uniq
30fdd 75 65 6c 79 20 69 64 65 6e 74 69 66 79 20 61 20  uely identify a 
30fde 73 65 67 6d 65 6e 74 29 0a 2a 2a 20 20 20 73 74  segment).**   st
30fdf 61 72 74 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20  art_block       
30fe0 20 20 2d 20 66 69 72 73 74 20 6c 65 61 66 20 6e    - first leaf n
30fe1 6f 64 65 0a 2a 2a 20 20 20 6c 65 61 76 65 73 5f  ode.**   leaves_
30fe2 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20 2d 20 6c  end_block    - l
30fe3 61 73 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a 2a  ast leaf node.**
30fe4 20 20 20 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20     end_block    
30fe5 20 20 20 20 20 20 20 2d 20 6c 61 73 74 20 62 6c         - last bl
30fe6 6f 63 6b 20 28 69 6e 63 6c 75 64 69 6e 67 20 69  ock (including i
30fe7 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 29 0a 2a  nterior nodes).*
30fe8 2a 20 20 20 72 6f 6f 74 20 20 20 20 20 20 20 20  *   root        
30fe9 20 20 20 20 20 20 20 20 2d 20 63 6f 6e 74 65 6e          - conten
30fea 74 73 20 6f 66 20 72 6f 6f 74 20 6e 6f 64 65 0a  ts of root node.
30feb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f  **.** If the roo
30fec 74 20 6e 6f 64 65 20 69 73 20 61 20 6c 65 61 66  t node is a leaf
30fed 20 6e 6f 64 65 2c 20 74 68 65 6e 20 73 74 61 72   node, then star
30fee 74 5f 62 6c 6f 63 6b 2c 0a 2a 2a 20 6c 65 61 76  t_block,.** leav
30fef 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 61 6e  es_end_block, an
30ff0 64 20 65 6e 64 5f 62 6c 6f 63 6b 20 61 72 65 20  d end_block are 
30ff1 61 6c 6c 20 30 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a  all 0..**.**.***
30ff2 2a 20 53 65 67 6d 65 6e 74 20 6d 65 72 67 69 6e  * Segment mergin
30ff3 67 20 2a 2a 2a 2a 0a 2a 2a 20 54 6f 20 61 6d 6f  g ****.** To amo
30ff4 72 74 69 7a 65 20 75 70 64 61 74 65 20 63 6f 73  rtize update cos
30ff5 74 73 2c 20 73 65 67 6d 65 6e 74 73 20 61 72 65  ts, segments are
30ff6 20 67 72 6f 75 70 65 64 20 69 6e 74 6f 20 6c 65   grouped into le
30ff7 76 65 6c 73 20 61 6e 64 0a 2a 2a 20 6d 65 72 67  vels and.** merg
30ff8 65 64 20 69 6e 20 62 61 74 63 68 65 73 2e 20 20  ed in batches.  
30ff9 45 61 63 68 20 69 6e 63 72 65 61 73 65 20 69 6e  Each increase in
30ffa 20 6c 65 76 65 6c 20 72 65 70 72 65 73 65 6e 74   level represent
30ffb 73 20 65 78 70 6f 6e 65 6e 74 69 61 6c 6c 79 0a  s exponentially.
30ffc 2a 2a 20 6d 6f 72 65 20 64 6f 63 75 6d 65 6e 74  ** more document
30ffd 73 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 6f 63  s..**.** New doc
30ffe 75 6d 65 6e 74 73 20 28 61 63 74 75 61 6c 6c 79  uments (actually
30fff 2c 20 64 6f 63 75 6d 65 6e 74 20 75 70 64 61 74  , document updat
31000 65 73 29 20 61 72 65 20 74 6f 6b 65 6e 69 7a 65  es) are tokenize
31001 64 20 61 6e 64 0a 2a 2a 20 77 72 69 74 74 65 6e  d and.** written
31002 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 28 75   individually (u
31003 73 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72 29  sing LeafWriter)
31004 20 74 6f 20 61 20 6c 65 76 65 6c 20 30 20 73 65   to a level 0 se
31005 67 6d 65 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 69  gment, with.** i
31006 6e 63 72 65 6d 65 6e 74 69 6e 67 20 69 64 78 2e  ncrementing idx.
31007 20 20 57 68 65 6e 20 69 64 78 20 72 65 61 63 68    When idx reach
31008 65 73 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 28  es MERGE_COUNT (
31009 64 65 66 61 75 6c 74 20 31 36 29 2c 20 61 6c 6c  default 16), all
3100a 0a 2a 2a 20 6c 65 76 65 6c 20 30 20 73 65 67 6d  .** level 0 segm
3100b 65 6e 74 73 20 61 72 65 20 6d 65 72 67 65 64 20  ents are merged 
3100c 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65  into a single le
3100d 76 65 6c 20 31 20 73 65 67 6d 65 6e 74 2e 20 20  vel 1 segment.  
3100e 4c 65 76 65 6c 20 31 0a 2a 2a 20 69 73 20 70 6f  Level 1.** is po
3100f 70 75 6c 61 74 65 64 20 6c 69 6b 65 20 6c 65 76  pulated like lev
31010 65 6c 20 30 2c 20 61 6e 64 20 65 76 65 6e 74 75  el 0, and eventu
31011 61 6c 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e 54  ally MERGE_COUNT
31012 20 6c 65 76 65 6c 20 31 0a 2a 2a 20 73 65 67 6d   level 1.** segm
31013 65 6e 74 73 20 61 72 65 20 6d 65 72 67 65 64 20  ents are merged 
31014 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65 76 65  to a single leve
31015 6c 20 32 20 73 65 67 6d 65 6e 74 20 28 72 65 70  l 2 segment (rep
31016 72 65 73 65 6e 74 69 6e 67 0a 2a 2a 20 4d 45 52  resenting.** MER
31017 47 45 5f 43 4f 55 4e 54 5e 32 20 75 70 64 61 74  GE_COUNT^2 updat
31018 65 73 29 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a  es), and so on..
31019 2a 2a 0a 2a 2a 20 41 20 73 65 67 6d 65 6e 74 20  **.** A segment 
3101a 6d 65 72 67 65 20 74 72 61 76 65 72 73 65 73 20  merge traverses 
3101b 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20  all segments at 
3101c 61 20 67 69 76 65 6e 20 6c 65 76 65 6c 20 69 6e  a given level in
3101d 0a 2a 2a 20 70 61 72 61 6c 6c 65 6c 2c 20 70 65  .** parallel, pe
3101e 72 66 6f 72 6d 69 6e 67 20 61 20 73 74 72 61 69  rforming a strai
3101f 67 68 74 66 6f 72 77 61 72 64 20 73 6f 72 74 65  ghtforward sorte
31020 64 20 6d 65 72 67 65 2e 20 20 53 69 6e 63 65 20  d merge.  Since 
31021 73 65 67 6d 65 6e 74 0a 2a 2a 20 6c 65 61 66 20  segment.** leaf 
31022 6e 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65  nodes are writte
31023 6e 20 69 6e 20 74 6f 20 74 68 65 20 25 5f 73 65  n in to the %_se
31024 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 69 6e 20  gments table in 
31025 6f 72 64 65 72 2c 20 74 68 69 73 0a 2a 2a 20 6d  order, this.** m
31026 65 72 67 65 20 74 72 61 76 65 72 73 65 73 20 74  erge traverses t
31027 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 73 71  he underlying sq
31028 6c 69 74 65 20 64 69 73 6b 20 73 74 72 75 63 74  lite disk struct
31029 75 72 65 73 20 65 66 66 69 63 69 65 6e 74 6c 79  ures efficiently
3102a 2e 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6d  ..** After the m
3102b 65 72 67 65 2c 20 61 6c 6c 20 73 65 67 6d 65 6e  erge, all segmen
3102c 74 20 62 6c 6f 63 6b 73 20 66 72 6f 6d 20 74 68  t blocks from th
3102d 65 20 6d 65 72 67 65 64 20 6c 65 76 65 6c 20 61  e merged level a
3102e 72 65 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a  re.** deleted..*
3102f 2a 0a 2a 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54  *.** MERGE_COUNT
31030 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f 66   controls how of
31031 74 65 6e 20 77 65 20 6d 65 72 67 65 20 73 65 67  ten we merge seg
31032 6d 65 6e 74 73 2e 20 20 31 36 20 73 65 65 6d 73  ments.  16 seems
31033 20 74 6f 20 62 65 0a 2a 2a 20 73 6f 6d 65 77 68   to be.** somewh
31034 61 74 20 6f 66 20 61 20 73 77 65 65 74 20 73 70  at of a sweet sp
31035 6f 74 20 66 6f 72 20 69 6e 73 65 72 74 69 6f 6e  ot for insertion
31036 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 33   performance.  3
31037 32 20 61 6e 64 20 36 34 20 73 68 6f 77 0a 2a 2a  2 and 64 show.**
31038 20 76 65 72 79 20 73 69 6d 69 6c 61 72 20 70 65   very similar pe
31039 72 66 6f 72 6d 61 6e 63 65 20 6e 75 6d 62 65 72  rformance number
3103a 73 20 74 6f 20 31 36 20 6f 6e 20 69 6e 73 65 72  s to 16 on inser
3103b 74 69 6f 6e 2c 20 74 68 6f 75 67 68 20 74 68 65  tion, though the
3103c 79 27 72 65 0a 2a 2a 20 61 20 74 69 6e 79 20 62  y're.** a tiny b
3103d 69 74 20 73 6c 6f 77 65 72 20 28 70 65 72 68 61  it slower (perha
3103e 70 73 20 64 75 65 20 74 6f 20 6d 6f 72 65 20 6f  ps due to more o
3103f 76 65 72 68 65 61 64 20 69 6e 20 6d 65 72 67 65  verhead in merge
31040 2d 74 69 6d 65 0a 2a 2a 20 73 6f 72 74 69 6e 67  -time.** sorting
31041 29 2e 20 20 38 20 69 73 20 61 62 6f 75 74 20 32  ).  8 is about 2
31042 30 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 31  0% slower than 1
31043 36 2c 20 34 20 61 62 6f 75 74 20 35 30 25 20 73  6, 4 about 50% s
31044 6c 6f 77 65 72 20 74 68 61 6e 0a 2a 2a 20 31 36  lower than.** 16
31045 2c 20 32 20 61 62 6f 75 74 20 36 36 25 20 73 6c  , 2 about 66% sl
31046 6f 77 65 72 20 74 68 61 6e 20 31 36 2e 0a 2a 2a  ower than 16..**
31047 0a 2a 2a 20 41 74 20 71 75 65 72 79 20 74 69 6d  .** At query tim
31048 65 2c 20 68 69 67 68 20 4d 45 52 47 45 5f 43 4f  e, high MERGE_CO
31049 55 4e 54 20 69 6e 63 72 65 61 73 65 73 20 74 68  UNT increases th
3104a 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d  e number of segm
3104b 65 6e 74 73 0a 2a 2a 20 77 68 69 63 68 20 6e 65  ents.** which ne
3104c 65 64 20 74 6f 20 62 65 20 73 63 61 6e 6e 65 64  ed to be scanned
3104d 20 61 6e 64 20 6d 65 72 67 65 64 2e 20 20 46 6f   and merged.  Fo
3104e 72 20 69 6e 73 74 61 6e 63 65 2c 20 77 69 74 68  r instance, with
3104f 20 31 30 30 6b 20 64 6f 63 73 0a 2a 2a 20 69 6e   100k docs.** in
31050 73 65 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  serted:.**.**   
31051 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 20 20 73   MERGE_COUNT   s
31052 65 67 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20  egments.**      
31053 20 31 36 20 20 20 20 20 20 20 20 20 20 20 32 35   16           25
31054 0a 2a 2a 20 20 20 20 20 20 20 20 38 20 20 20 20  .**        8    
31055 20 20 20 20 20 20 20 31 32 0a 2a 2a 20 20 20 20         12.**    
31056 20 20 20 20 34 20 20 20 20 20 20 20 20 20 20 20      4           
31057 31 30 0a 2a 2a 20 20 20 20 20 20 20 20 32 20 20  10.**        2  
31058 20 20 20 20 20 20 20 20 20 20 36 0a 2a 2a 0a 2a            6.**.*
31059 2a 20 54 68 69 73 20 61 70 70 65 61 72 73 20 74  * This appears t
3105a 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 6d 6f  o have only a mo
3105b 64 65 72 61 74 65 20 69 6d 70 61 63 74 20 6f 6e  derate impact on
3105c 20 71 75 65 72 69 65 73 20 66 6f 72 20 76 65 72   queries for ver
3105d 79 0a 2a 2a 20 66 72 65 71 75 65 6e 74 20 74 65  y.** frequent te
3105e 72 6d 73 20 28 77 68 69 63 68 20 61 72 65 20 73  rms (which are s
3105f 6f 6d 65 77 68 61 74 20 64 6f 6d 69 6e 61 74 65  omewhat dominate
31060 64 20 62 79 20 73 65 67 6d 65 6e 74 20 6d 65 72  d by segment mer
31061 67 65 0a 2a 2a 20 63 6f 73 74 73 29 2c 20 61 6e  ge.** costs), an
31062 64 20 69 6e 66 72 65 71 75 65 6e 74 20 61 6e 64  d infrequent and
31063 20 6e 6f 6e 2d 65 78 69 73 74 65 6e 74 20 74 65   non-existent te
31064 72 6d 73 20 73 74 69 6c 6c 20 73 65 65 6d 20 74  rms still seem t
31065 6f 20 62 65 20 66 61 73 74 0a 2a 2a 20 65 76 65  o be fast.** eve
31066 6e 20 77 69 74 68 20 6d 61 6e 79 20 73 65 67 6d  n with many segm
31067 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f  ents..**.** TODO
31068 28 73 68 65 73 73 29 20 54 68 61 74 20 73 61 69  (shess) That sai
31069 64 2c 20 69 74 20 77 6f 75 6c 64 20 62 65 20 6e  d, it would be n
3106a 69 63 65 20 74 6f 20 68 61 76 65 20 61 20 62 65  ice to have a be
3106b 74 74 65 72 20 71 75 65 72 79 2d 73 69 64 65 0a  tter query-side.
3106c 2a 2a 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20  ** argument for 
3106d 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6f 66 20 31  MERGE_COUNT of 1
3106e 36 2e 20 20 41 6c 73 6f 2c 20 69 74 20 69 73 20  6.  Also, it is 
3106f 70 6f 73 73 69 62 6c 65 2f 6c 69 6b 65 6c 79 20  possible/likely 
31070 74 68 61 74 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61  that.** optimiza
31071 74 69 6f 6e 73 20 74 6f 20 74 68 69 6e 67 73 20  tions to things 
31072 6c 69 6b 65 20 64 6f 63 6c 69 73 74 20 6d 65 72  like doclist mer
31073 67 69 6e 67 20 77 69 6c 6c 20 73 77 69 6e 67 20  ging will swing 
31074 74 68 65 20 73 77 65 65 74 0a 2a 2a 20 73 70 6f  the sweet.** spo
31075 74 20 61 72 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a  t around..**.**.
31076 2a 2a 0a 2a 2a 2a 2a 20 48 61 6e 64 6c 69 6e 67  **.**** Handling
31077 20 6f 66 20 64 65 6c 65 74 69 6f 6e 73 20 61 6e   of deletions an
31078 64 20 75 70 64 61 74 65 73 20 2a 2a 2a 2a 0a 2a  d updates ****.*
31079 2a 20 53 69 6e 63 65 20 77 65 27 72 65 20 75 73  * Since we're us
3107a 69 6e 67 20 61 20 73 65 67 6d 65 6e 74 65 64 20  ing a segmented 
3107b 73 74 72 75 63 74 75 72 65 2c 20 77 69 74 68 20  structure, with 
3107c 6e 6f 20 64 6f 63 69 64 2d 6f 72 69 65 6e 74 65  no docid-oriente
3107d 64 0a 2a 2a 20 69 6e 64 65 78 20 69 6e 74 6f 20  d.** index into 
3107e 74 68 65 20 74 65 72 6d 20 69 6e 64 65 78 2c 20  the term index, 
3107f 77 65 20 63 6c 65 61 72 6c 79 20 63 61 6e 6e 6f  we clearly canno
31080 74 20 73 69 6d 70 6c 79 20 75 70 64 61 74 65 20  t simply update 
31081 74 68 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 64 65  the term.** inde
31082 78 20 77 68 65 6e 20 61 20 64 6f 63 75 6d 65 6e  x when a documen
31083 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20  t is deleted or 
31084 75 70 64 61 74 65 64 2e 20 20 46 6f 72 20 64 65  updated.  For de
31085 6c 65 74 69 6f 6e 73 2c 20 77 65 0a 2a 2a 20 77  letions, we.** w
31086 72 69 74 65 20 61 6e 20 65 6d 70 74 79 20 64 6f  rite an empty do
31087 63 6c 69 73 74 20 28 76 61 72 69 6e 74 28 64 6f  clist (varint(do
31088 63 69 64 29 20 76 61 72 69 6e 74 28 50 4f 53 5f  cid) varint(POS_
31089 45 4e 44 29 29 2c 20 66 6f 72 20 75 70 64 61 74  END)), for updat
3108a 65 73 0a 2a 2a 20 77 65 20 73 69 6d 70 6c 79 20  es.** we simply 
3108b 77 72 69 74 65 20 74 68 65 20 6e 65 77 20 64 6f  write the new do
3108c 63 6c 69 73 74 2e 20 20 53 65 67 6d 65 6e 74 20  clist.  Segment 
3108d 6d 65 72 67 65 73 20 6f 76 65 72 77 72 69 74 65  merges overwrite
3108e 20 6f 6c 64 65 72 0a 2a 2a 20 64 61 74 61 20 66   older.** data f
3108f 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  or a particular 
31090 64 6f 63 69 64 20 77 69 74 68 20 6e 65 77 65 72  docid with newer
31091 20 64 61 74 61 2c 20 73 6f 20 64 65 6c 65 74 65   data, so delete
31092 73 20 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a 20  s or updates.** 
31093 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20  will eventually 
31094 6f 76 65 72 74 61 6b 65 20 74 68 65 20 65 61 72  overtake the ear
31095 6c 69 65 72 20 64 61 74 61 20 61 6e 64 20 6b 6e  lier data and kn
31096 6f 63 6b 20 69 74 20 6f 75 74 2e 20 20 54 68 65  ock it out.  The
31097 0a 2a 2a 20 71 75 65 72 79 20 6c 6f 67 69 63 20  .** query logic 
31098 6c 69 6b 65 77 69 73 65 20 6d 65 72 67 65 73 20  likewise merges 
31099 64 6f 63 6c 69 73 74 73 20 73 6f 20 74 68 61 74  doclists so that
3109a 20 6e 65 77 65 72 20 64 61 74 61 20 6b 6e 6f 63   newer data knoc
3109b 6b 73 20 6f 75 74 0a 2a 2a 20 6f 6c 64 65 72 20  ks out.** older 
3109c 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f  data..**.** TODO
3109d 28 73 68 65 73 73 29 20 50 72 6f 76 69 64 65 20  (shess) Provide 
3109e 61 20 56 41 43 55 55 4d 20 74 79 70 65 20 6f 70  a VACUUM type op
3109f 65 72 61 74 69 6f 6e 20 74 6f 20 63 6c 65 61 72  eration to clear
310a0 20 6f 75 74 20 61 6c 6c 0a 2a 2a 20 64 65 6c 65   out all.** dele
310a1 74 69 6f 6e 73 20 61 6e 64 20 64 75 70 6c 69 63  tions and duplic
310a2 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 77 6f  ations.  This wo
310a3 75 6c 64 20 62 61 73 69 63 61 6c 6c 79 20 62 65  uld basically be
310a4 20 61 20 66 6f 72 63 65 64 20 6d 65 72 67 65 0a   a forced merge.
310a5 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  ** into a single
310a6 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 0a 23 69   segment..*/..#i
310a7 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
310a8 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
310a9 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
310aa 5f 46 54 53 33 29 0a 0a 23 69 66 20 64 65 66 69  _FTS3)..#if defi
310ab 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
310ac 45 5f 46 54 53 33 29 20 26 26 20 21 64 65 66 69  E_FTS3) && !defi
310ad 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
310ae 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
310af 5f 43 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a  _CORE 1.#endif..
310b0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
310b1 20 49 6e 63 6c 75 64 65 20 66 74 73 33 5f 65 78   Include fts3_ex
310b2 70 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  pr.h in the midd
310b3 6c 65 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a  le of fts3.c ***
310b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
310b5 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
310b6 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
310b7 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.h ********
310b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
310ba 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 20  ./*.** 2008 Nov 
310bb 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  28.**.** The aut
310bc 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
310bd 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
310be 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
310bf 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
310c0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
310c1 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
310c2 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
310c3 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
310c4 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
310c5 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
310c6 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
310c7 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
310c8 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
310c9 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
310ca 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
310cb 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
310cc 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
310cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310d1 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a  ******.**.*/../*
310d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
310d3 63 6c 75 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e  clude fts3_token
310d4 69 7a 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69  izer.h in the mi
310d5 64 64 6c 65 20 6f 66 20 66 74 73 33 5f 65 78 70  ddle of fts3_exp
310d6 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  r.h ********/./*
310d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
310d8 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f  gin file fts3_to
310d9 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a  kenizer.h ******
310da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
310dc 0a 2a 2a 20 32 30 30 36 20 4a 75 6c 79 20 31 30  .** 2006 July 10
310dd 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
310de 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
310df 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
310e0 75 72 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  urce code..**.**
310e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310e5 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 44 65 66 69 6e  *******.** Defin
310e6 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  es the interface
310e7 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 72 73 20 75   to tokenizers u
310e8 73 65 64 20 62 79 20 66 75 6c 6c 74 65 78 74 2d  sed by fulltext-
310e9 73 65 61 72 63 68 2e 20 20 54 68 65 72 65 0a 2a  search.  There.*
310ea 2a 20 61 72 65 20 74 68 72 65 65 20 62 61 73 69  * are three basi
310eb 63 20 63 6f 6d 70 6f 6e 65 6e 74 73 3a 0a 2a 2a  c components:.**
310ec 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  .** sqlite3_toke
310ed 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 73 20  nizer_module is 
310ee 61 20 73 69 6e 67 6c 65 74 6f 6e 20 64 65 66 69  a singleton defi
310ef 6e 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a  ning the tokeniz
310f0 65 72 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  er.** interface 
310f1 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73  functions.  This
310f2 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79 20   is essentially 
310f3 74 68 65 20 63 6c 61 73 73 20 73 74 72 75 63 74  the class struct
310f4 75 72 65 20 66 6f 72 0a 2a 2a 20 74 6f 6b 65 6e  ure for.** token
310f5 69 7a 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c  izers..**.** sql
310f6 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69  ite3_tokenizer i
310f7 73 20 75 73 65 64 20 74 6f 20 64 65 66 69 6e 65  s used to define
310f8 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 6f   a particular to
310f9 6b 65 6e 69 7a 65 72 2c 20 70 65 72 68 61 70 73  kenizer, perhaps
310fa 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 75  .** including cu
310fb 73 74 6f 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f  stomization info
310fc 72 6d 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  rmation defined 
310fd 61 74 20 63 72 65 61 74 69 6f 6e 20 74 69 6d 65  at creation time
310fe 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ..**.** sqlite3_
310ff 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
31100 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79   is generated by
31101 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20   a tokenizer to 
31102 67 65 6e 65 72 61 74 65 0a 2a 2a 20 74 6f 6b 65  generate.** toke
31103 6e 73 20 66 72 6f 6d 20 61 20 70 61 72 74 69 63  ns from a partic
31104 75 6c 61 72 20 69 6e 70 75 74 2e 0a 2a 2f 0a 23  ular input..*/.#
31105 69 66 6e 64 65 66 20 5f 46 54 53 33 5f 54 4f 4b  ifndef _FTS3_TOK
31106 45 4e 49 5a 45 52 5f 48 5f 0a 23 64 65 66 69 6e  ENIZER_H_.#defin
31107 65 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  e _FTS3_TOKENIZE
31108 52 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  R_H_../* TODO(sh
31109 65 73 73 29 20 4f 6e 6c 79 20 75 73 65 64 20 66  ess) Only used f
3110a 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64  or SQLITE_OK and
3110b 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 61 74 20   SQLITE_DONE at 
3110c 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 20 49 66  this time..** If
3110d 20 74 6f 6b 65 6e 69 7a 65 72 73 20 61 72 65 20   tokenizers are 
3110e 74 6f 20 62 65 20 61 6c 6c 6f 77 65 64 20 74 6f  to be allowed to
3110f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 2a 28   call sqlite3_*(
31110 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 74 68 65  ) functions, the
31111 6e 0a 2a 2a 20 77 65 20 77 69 6c 6c 20 6e 65 65  n.** we will nee
31112 64 20 61 20 77 61 79 20 74 6f 20 72 65 67 69 73  d a way to regis
31113 74 65 72 20 74 68 65 20 41 50 49 20 63 6f 6e 73  ter the API cons
31114 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a  istently..*/../*
31115 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 73 20 75  .** Structures u
31116 73 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e  sed by the token
31117 69 7a 65 72 20 69 6e 74 65 72 66 61 63 65 2e 20  izer interface. 
31118 57 68 65 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e  When a new token
31119 69 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  izer.** implemen
3111a 74 61 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74  tation is regist
3111b 65 72 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 72  ered, the caller
3111c 20 70 72 6f 76 69 64 65 73 20 61 20 70 6f 69 6e   provides a poin
3111d 74 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c  ter to.** an sql
3111e 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
3111f 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  odule containing
31120 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
31121 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e   callback.** fun
31122 63 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6b 65  ctions that make
31123 20 75 70 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74   up an implement
31124 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  ation..**.** Whe
31125 6e 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 20  n an fts3 table 
31126 69 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 70  is created, it p
31127 61 73 73 65 73 20 61 6e 79 20 61 72 67 75 6d 65  asses any argume
31128 6e 74 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a  nts passed to.**
31129 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63   the tokenizer c
3112a 6c 61 75 73 65 20 6f 66 20 74 68 65 20 43 52 45  lause of the CRE
3112b 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
3112c 45 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74  E statement to t
3112d 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f  he.** sqlite3_to
3112e 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78  kenizer_module.x
3112f 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f  Create() functio
31130 6e 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  n of the request
31131 65 64 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20  ed tokenizer.** 
31132 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
31133 54 68 65 20 78 43 72 65 61 74 65 28 29 20 66 75  The xCreate() fu
31134 6e 63 74 69 6f 6e 20 69 6e 20 74 75 72 6e 20 72  nction in turn r
31135 65 74 75 72 6e 73 20 61 6e 20 0a 2a 2a 20 73 71  eturns an .** sq
31136 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
31137 73 74 72 75 63 74 75 72 65 20 72 65 70 72 65 73  structure repres
31138 65 6e 74 69 6e 67 20 74 68 65 20 73 70 65 63 69  enting the speci
31139 66 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f  fic tokenizer to
3113a 0a 2a 2a 20 62 65 20 75 73 65 64 20 66 6f 72 20  .** be used for 
3113b 74 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 28  the fts3 table (
3113c 63 75 73 74 6f 6d 69 7a 65 64 20 62 79 20 74 68  customized by th
3113d 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75  e tokenizer clau
3113e 73 65 20 61 72 67 75 6d 65 6e 74 73 29 2e 0a 2a  se arguments)..*
3113f 2a 0a 2a 2a 20 54 6f 20 74 6f 6b 65 6e 69 7a 65  *.** To tokenize
31140 20 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72   an input buffer
31141 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f  , the sqlite3_to
31142 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78  kenizer_module.x
31143 4f 70 65 6e 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  Open().** method
31144 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 72   is called. It r
31145 65 74 75 72 6e 73 20 61 6e 20 73 71 6c 69 74 65  eturns an sqlite
31146 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
31147 6f 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61  or object.** tha
31148 74 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f  t may be used to
31149 20 74 6f 6b 65 6e 69 7a 65 20 61 20 73 70 65 63   tokenize a spec
3114a 69 66 69 63 20 69 6e 70 75 74 20 62 75 66 66 65  ific input buffe
3114b 72 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68  r based on.** th
3114c 65 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72  e tokenization r
3114d 75 6c 65 73 20 73 75 70 70 6c 69 65 64 20 62 79  ules supplied by
3114e 20 61 20 73 70 65 63 69 66 69 63 20 73 71 6c 69   a specific sqli
3114f 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a  te3_tokenizer.**
31150 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65   object..*/.type
31151 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
31152 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
31153 75 6c 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ule sqlite3_toke
31154 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 3b 0a 74 79  nizer_module;.ty
31155 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
31156 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73  ite3_tokenizer s
31157 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
31158 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
31159 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3115a 65 72 5f 63 75 72 73 6f 72 20 73 71 6c 69 74 65  er_cursor sqlite
3115b 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
3115c 6f 72 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69  or;..struct sqli
3115d 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
3115e 64 75 6c 65 20 7b 0a 0a 20 20 2f 2a 0a 20 20 2a  dule {..  /*.  *
3115f 2a 20 53 74 72 75 63 74 75 72 65 20 76 65 72 73  * Structure vers
31160 69 6f 6e 2e 20 53 68 6f 75 6c 64 20 61 6c 77 61  ion. Should alwa
31161 79 73 20 62 65 20 73 65 74 20 74 6f 20 30 2e 0a  ys be set to 0..
31162 20 20 2a 2f 0a 20 20 69 6e 74 20 69 56 65 72 73    */.  int iVers
31163 69 6f 6e 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ion;..  /*.  ** 
31164 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b  Create a new tok
31165 65 6e 69 7a 65 72 2e 20 54 68 65 20 76 61 6c 75  enizer. The valu
31166 65 73 20 69 6e 20 74 68 65 20 61 72 67 76 5b 5d  es in the argv[]
31167 20 61 72 72 61 79 20 61 72 65 20 74 68 65 0a 20   array are the. 
31168 20 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 70 61   ** arguments pa
31169 73 73 65 64 20 74 6f 20 74 68 65 20 22 74 6f 6b  ssed to the "tok
3116a 65 6e 69 7a 65 72 22 20 63 6c 61 75 73 65 20 6f  enizer" clause o
3116b 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52  f the CREATE VIR
3116c 54 55 41 4c 0a 20 20 2a 2a 20 54 41 42 4c 45 20  TUAL.  ** TABLE 
3116d 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63  statement that c
3116e 72 65 61 74 65 64 20 74 68 65 20 66 74 73 33 20  reated the fts3 
3116f 74 61 62 6c 65 2e 20 46 6f 72 20 65 78 61 6d 70  table. For examp
31170 6c 65 2c 20 69 66 0a 20 20 2a 2a 20 74 68 65 20  le, if.  ** the 
31171 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 20 69 73  following SQL is
31172 20 65 78 65 63 75 74 65 64 3a 0a 20 20 2a 2a 0a   executed:.  **.
31173 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 2e 2e    **   CREATE ..
31174 20 55 53 49 4e 47 20 66 74 73 33 28 20 2e 2e 2e   USING fts3( ...
31175 20 2c 20 74 6f 6b 65 6e 69 7a 65 72 20 3c 74 6f   , tokenizer <to
31176 6b 65 6e 69 7a 65 72 2d 6e 61 6d 65 3e 20 61 72  kenizer-name> ar
31177 67 31 20 61 72 67 32 29 0a 20 20 2a 2a 0a 20 20  g1 arg2).  **.  
31178 2a 2a 20 74 68 65 6e 20 61 72 67 63 20 69 73 20  ** then argc is 
31179 73 65 74 20 74 6f 20 32 2c 20 61 6e 64 20 74 68  set to 2, and th
3117a 65 20 61 72 67 76 5b 5d 20 61 72 72 61 79 20 63  e argv[] array c
3117b 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73  ontains pointers
3117c 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 73 74 72  .  ** to the str
3117d 69 6e 67 73 20 22 61 72 67 31 22 20 61 6e 64 20  ings "arg1" and 
3117e 22 61 72 67 32 22 2e 0a 20 20 2a 2a 0a 20 20 2a  "arg2"..  **.  *
3117f 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20 73 68  * This method sh
31180 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68  ould return eith
31181 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29  er SQLITE_OK (0)
31182 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65  , or an SQLite e
31183 72 72 6f 72 20 0a 20 20 2a 2a 20 63 6f 64 65 2e  rror .  ** code.
31184 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73   If SQLITE_OK is
31185 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20   returned, then 
31186 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 73 68 6f  *ppTokenizer sho
31187 75 6c 64 20 62 65 20 73 65 74 0a 20 20 2a 2a 20  uld be set.  ** 
31188 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
31189 6e 65 77 6c 79 20 63 72 65 61 74 65 64 20 74 6f  newly created to
3118a 6b 65 6e 69 7a 65 72 20 73 74 72 75 63 74 75 72  kenizer structur
3118b 65 2e 20 54 68 65 20 67 65 6e 65 72 69 63 0a 20  e. The generic. 
3118c 20 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65   ** sqlite3_toke
3118d 6e 69 7a 65 72 2e 70 4d 6f 64 75 6c 65 20 76 61  nizer.pModule va
3118e 72 69 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f  riable should no
3118f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64  t be initialised
31190 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 63 61   by.  ** this ca
31191 6c 6c 62 61 63 6b 2e 20 54 68 65 20 63 61 6c 6c  llback. The call
31192 65 72 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 0a 20  er will do so.. 
31193 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 72 65   */.  int (*xCre
31194 61 74 65 29 28 0a 20 20 20 20 69 6e 74 20 61 72  ate)(.    int ar
31195 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gc,             
31196 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
31197 20 53 69 7a 65 20 6f 66 20 61 72 67 76 20 61 72   Size of argv ar
31198 72 61 79 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  ray */.    const
31199 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
3119a 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  v,             /
3119b 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 61 72 67 75  * Tokenizer argu
3119c 6d 65 6e 74 20 73 74 72 69 6e 67 73 20 2a 2f 0a  ment strings */.
3119d 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65      sqlite3_toke
3119e 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69  nizer **ppTokeni
3119f 7a 65 72 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  zer     /* OUT: 
311a0 43 72 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65  Created tokenize
311a1 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a  r */.  );..  /*.
311a2 20 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20    ** Destroy an 
311a3 65 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a  existing tokeniz
311a4 65 72 2e 20 54 68 65 20 66 74 73 33 20 6d 6f 64  er. The fts3 mod
311a5 75 6c 65 20 63 61 6c 6c 73 20 74 68 69 73 20 6d  ule calls this m
311a6 65 74 68 6f 64 0a 20 20 2a 2a 20 65 78 61 63 74  ethod.  ** exact
311a7 6c 79 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ly once for each
311a8 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
311a9 20 74 6f 20 78 43 72 65 61 74 65 28 29 2e 0a 20   to xCreate().. 
311aa 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 44 65 73   */.  int (*xDes
311ab 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 74 6f  troy)(sqlite3_to
311ac 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
311ad 7a 65 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  zer);..  /*.  **
311ae 20 43 72 65 61 74 65 20 61 20 74 6f 6b 65 6e 69   Create a tokeni
311af 7a 65 72 20 63 75 72 73 6f 72 20 74 6f 20 74 6f  zer cursor to to
311b0 6b 65 6e 69 7a 65 20 61 6e 20 69 6e 70 75 74 20  kenize an input 
311b1 62 75 66 66 65 72 2e 20 54 68 65 20 63 61 6c 6c  buffer. The call
311b2 65 72 0a 20 20 2a 2a 20 69 73 20 72 65 73 70 6f  er.  ** is respo
311b3 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72  nsible for ensur
311b4 69 6e 67 20 74 68 61 74 20 74 68 65 20 69 6e 70  ing that the inp
311b5 75 74 20 62 75 66 66 65 72 20 72 65 6d 61 69 6e  ut buffer remain
311b6 73 20 76 61 6c 69 64 0a 20 20 2a 2a 20 75 6e 74  s valid.  ** unt
311b7 69 6c 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  il the cursor is
311b8 20 63 6c 6f 73 65 64 20 28 75 73 69 6e 67 20 74   closed (using t
311b9 68 65 20 78 43 6c 6f 73 65 28 29 20 6d 65 74 68  he xClose() meth
311ba 6f 64 29 2e 20 0a 20 20 2a 2f 0a 20 20 69 6e 74  od). .  */.  int
311bb 20 28 2a 78 4f 70 65 6e 29 28 0a 20 20 20 20 73   (*xOpen)(.    s
311bc 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
311bd 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20   *pTokenizer,   
311be 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
311bf 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 63   object */.    c
311c0 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75  onst char *pInpu
311c1 74 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  t, int nBytes,  
311c2 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 62 75 66      /* Input buf
311c3 66 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  fer */.    sqlit
311c4 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
311c5 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20  sor **ppCursor  
311c6 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20  /* OUT: Created 
311c7 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72  tokenizer cursor
311c8 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20   */.  );..  /*. 
311c9 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65   ** Destroy an e
311ca 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65  xisting tokenize
311cb 72 20 63 75 72 73 6f 72 2e 20 54 68 65 20 66 74  r cursor. The ft
311cc 73 33 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20  s3 module calls 
311cd 74 68 69 73 20 0a 20 20 2a 2a 20 6d 65 74 68 6f  this .  ** metho
311ce 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 66  d exactly once f
311cf 6f 72 20 65 61 63 68 20 73 75 63 63 65 73 73 66  or each successf
311d0 75 6c 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e  ul call to xOpen
311d1 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  ()..  */.  int (
311d2 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33  *xClose)(sqlite3
311d3 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
311d4 72 20 2a 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20  r *pCursor);..  
311d5 2f 2a 0a 20 20 2a 2a 20 52 65 74 72 69 65 76 65  /*.  ** Retrieve
311d6 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20   the next token 
311d7 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a  from the tokeniz
311d8 65 72 20 63 75 72 73 6f 72 20 70 43 75 72 73 6f  er cursor pCurso
311d9 72 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d 65 74  r. This.  ** met
311da 68 6f 64 20 73 68 6f 75 6c 64 20 65 69 74 68 65  hod should eithe
311db 72 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r return SQLITE_
311dc 4f 4b 20 61 6e 64 20 73 65 74 20 74 68 65 20 76  OK and set the v
311dd 61 6c 75 65 73 20 6f 66 20 74 68 65 0a 20 20 2a  alues of the.  *
311de 2a 20 22 4f 55 54 22 20 76 61 72 69 61 62 6c 65  * "OUT" variable
311df 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 65 6c  s identified bel
311e0 6f 77 2c 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f  ow, or SQLITE_DO
311e1 4e 45 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  NE to indicate t
311e2 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64  hat.  ** the end
311e3 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 68   of the buffer h
311e4 61 73 20 62 65 65 6e 20 72 65 61 63 68 65 64 2c  as been reached,
311e5 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72   or an SQLite er
311e6 72 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20  ror code..  **. 
311e7 20 2a 2a 20 2a 70 70 54 6f 6b 65 6e 20 73 68 6f   ** *ppToken sho
311e8 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 70 6f  uld be set to po
311e9 69 6e 74 20 61 74 20 61 20 62 75 66 66 65 72 20  int at a buffer 
311ea 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 0a  containing the .
311eb 20 20 2a 2a 20 6e 6f 72 6d 61 6c 69 7a 65 64 20    ** normalized 
311ec 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 74  version of the t
311ed 6f 6b 65 6e 20 28 69 2e 65 2e 20 61 66 74 65 72  oken (i.e. after
311ee 20 61 6e 79 20 63 61 73 65 2d 66 6f 6c 64 69 6e   any case-foldin
311ef 67 20 61 6e 64 2f 6f 72 0a 20 20 2a 2a 20 73 74  g and/or.  ** st
311f0 65 6d 6d 69 6e 67 20 68 61 73 20 62 65 65 6e 20  emming has been 
311f1 70 65 72 66 6f 72 6d 65 64 29 2e 20 2a 70 6e 42  performed). *pnB
311f2 79 74 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73  ytes should be s
311f3 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 68  et to the length
311f4 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 62 75  .  ** of this bu
311f5 66 66 65 72 20 69 6e 20 62 79 74 65 73 2e 20 54  ffer in bytes. T
311f6 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 74 68  he input text th
311f7 61 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 65  at generated the
311f8 20 74 6f 6b 65 6e 20 69 73 0a 20 20 2a 2a 20 69   token is.  ** i
311f9 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
311fa 20 62 79 74 65 20 6f 66 66 73 65 74 73 20 72 65   byte offsets re
311fb 74 75 72 6e 65 64 20 69 6e 20 2a 70 69 53 74 61  turned in *piSta
311fc 72 74 4f 66 66 73 65 74 20 61 6e 64 0a 20 20 2a  rtOffset and.  *
311fd 2a 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2e 20  * *piEndOffset. 
311fe 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 73  *piStartOffset s
311ff 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  hould be set to 
31200 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
31201 20 66 69 72 73 74 0a 20 20 2a 2a 20 62 79 74 65   first.  ** byte
31202 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e   of the token in
31203 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65   the input buffe
31204 72 2e 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20  r. *piEndOffset 
31205 73 68 6f 75 6c 64 20 62 65 20 73 65 74 0a 20 20  should be set.  
31206 2a 2a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20  ** to the index 
31207 6f 66 20 74 68 65 20 66 69 72 73 74 20 62 79 74  of the first byt
31208 65 20 6a 75 73 74 20 70 61 73 74 20 74 68 65 20  e just past the 
31209 65 6e 64 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  end of the token
3120a 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 69 6e 70   in.  ** the inp
3120b 75 74 20 62 75 66 66 65 72 2e 0a 20 20 2a 2a 0a  ut buffer..  **.
3120c 20 20 2a 2a 20 54 68 65 20 62 75 66 66 65 72 20    ** The buffer 
3120d 2a 70 70 54 6f 6b 65 6e 20 69 73 20 73 65 74 20  *ppToken is set 
3120e 74 6f 20 70 6f 69 6e 74 20 61 74 20 69 73 20 6d  to point at is m
3120f 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 74 6f  anaged by the to
31210 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6d 70  kenizer.  ** imp
31211 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49 74 20  lementation. It 
31212 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  is only required
31213 20 74 6f 20 62 65 20 76 61 6c 69 64 20 75 6e 74   to be valid unt
31214 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  il the next call
31215 0a 20 20 2a 2a 20 74 6f 20 78 4e 65 78 74 28 29  .  ** to xNext()
31216 20 6f 72 20 78 43 6c 6f 73 65 28 29 2e 20 0a 20   or xClose(). . 
31217 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68   */.  /* TODO(sh
31218 65 73 73 29 20 63 75 72 72 65 6e 74 20 69 6d 70  ess) current imp
31219 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75  lementation requ
3121a 69 72 65 73 20 70 49 6e 70 75 74 20 74 6f 20 62  ires pInput to b
3121b 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  e.  ** nul-termi
3121c 6e 61 74 65 64 2e 20 20 54 68 69 73 20 73 68 6f  nated.  This sho
3121d 75 6c 64 20 65 69 74 68 65 72 20 62 65 20 66 69  uld either be fi
3121e 78 65 64 2c 20 6f 72 20 70 49 6e 70 75 74 2f 6e  xed, or pInput/n
3121f 42 79 74 65 73 0a 20 20 2a 2a 20 73 68 6f 75 6c  Bytes.  ** shoul
31220 64 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74  d be converted t
31221 6f 20 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20  o zInput..  */. 
31222 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 0a 20   int (*xNext)(. 
31223 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e     sqlite3_token
31224 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
31225 72 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b 65 6e  rsor,   /* Token
31226 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
31227 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a     const char **
31228 70 70 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a 70 6e  ppToken, int *pn
31229 42 79 74 65 73 2c 20 20 2f 2a 20 4f 55 54 3a 20  Bytes,  /* OUT: 
3122a 4e 6f 72 6d 61 6c 69 7a 65 64 20 74 65 78 74 20  Normalized text 
3122b 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  for token */.   
3122c 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
3122d 73 65 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 42 79  set,  /* OUT: By
3122e 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b  te offset of tok
3122f 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
31230 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
31231 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 2f  iEndOffset,    /
31232 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66 66 73  * OUT: Byte offs
31233 65 74 20 6f 66 20 65 6e 64 20 6f 66 20 74 6f 6b  et of end of tok
31234 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
31235 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
31236 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 2f  iPosition      /
31237 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
31238 20 74 6f 6b 65 6e 73 20 72 65 74 75 72 6e 65 64   tokens returned
31239 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 6e 65   before this one
3123a 20 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73 74 72   */.  );.};..str
3123b 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  uct sqlite3_toke
3123c 6e 69 7a 65 72 20 7b 0a 20 20 63 6f 6e 73 74 20  nizer {.  const 
3123d 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3123e 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  r_module *pModul
3123f 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c  e;  /* The modul
31240 65 20 66 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e  e for this token
31241 69 7a 65 72 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b  izer */.  /* Tok
31242 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
31243 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
31244 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
31245 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
31246 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  ;..struct sqlite
31247 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
31248 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
31249 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
3124a 69 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54  izer;       /* T
3124b 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74 68 69  okenizer for thi
3124c 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 2f  s cursor. */.  /
3124d 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c  * Tokenizer impl
3124e 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c  ementations will
3124f 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61   typically add a
31250 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
31251 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66 20 2f   */.};..#endif /
31252 2a 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  * _FTS3_TOKENIZE
31253 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  R_H_ */../******
31254 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
31255 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68  fts3_tokenizer.h
31256 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
31257 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31258 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
31259 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
3125a 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
3125b 74 20 6f 66 66 20 69 6e 20 66 74 73 33 5f 65 78  t off in fts3_ex
3125c 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pr.h ***********
3125d 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
3125e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65  The following de
3125f 73 63 72 69 62 65 73 20 74 68 65 20 73 79 6e 74  scribes the synt
31260 61 78 20 73 75 70 70 6f 72 74 65 64 20 62 79 20  ax supported by 
31261 74 68 65 20 66 74 73 33 20 4d 41 54 43 48 0a 2a  the fts3 MATCH.*
31262 2a 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 61 20  * operator in a 
31263 73 69 6d 69 6c 61 72 20 66 6f 72 6d 61 74 20 74  similar format t
31264 6f 20 74 68 61 74 20 75 73 65 64 20 62 79 20 74  o that used by t
31265 68 65 20 6c 65 6d 6f 6e 20 70 61 72 73 65 72 0a  he lemon parser.
31266 2a 2a 20 67 65 6e 65 72 61 74 6f 72 2e 20 54 68  ** generator. Th
31267 69 73 20 6d 6f 64 75 6c 65 20 64 6f 65 73 20 6e  is module does n
31268 6f 74 20 75 73 65 20 61 63 74 75 61 6c 6c 79 20  ot use actually 
31269 6c 65 6d 6f 6e 2c 20 69 74 20 75 73 65 73 20 61  lemon, it uses a
3126a 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 61 72 73 65  .** custom parse
3126b 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 71 75 65 72 79  r..**.**   query
3126c 20 3a 3a 3d 20 61 6e 64 65 78 70 72 20 28 4f 52   ::= andexpr (OR
3126d 20 61 6e 64 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a   andexpr)*..**.*
3126e 2a 20 20 20 61 6e 64 65 78 70 72 20 3a 3a 3d 20  *   andexpr ::= 
3126f 6e 6f 74 65 78 70 72 20 28 41 4e 44 3f 20 6e 6f  notexpr (AND? no
31270 74 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20  texpr)*..**.**  
31271 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 6e 65 61   notexpr ::= nea
31272 72 65 78 70 72 20 28 4e 4f 54 20 6e 65 61 72 65  rexpr (NOT neare
31273 78 70 72 7c 2d 54 4f 4b 45 4e 29 2a 2e 0a 2a 2a  xpr|-TOKEN)*..**
31274 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 4c     notexpr ::= L
31275 50 20 71 75 65 72 79 20 52 50 2e 0a 2a 2a 0a 2a  P query RP..**.*
31276 2a 20 20 20 6e 65 61 72 65 78 70 72 20 3a 3a 3d  *   nearexpr ::=
31277 20 70 68 72 61 73 65 20 28 4e 45 41 52 20 64 69   phrase (NEAR di
31278 73 74 61 6e 63 65 5f 6f 70 74 20 6e 65 61 72 65  stance_opt neare
31279 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 64  xpr)*..**.**   d
3127a 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a 3a 3d 20  istance_opt ::= 
3127b 2e 0a 2a 2a 20 20 20 64 69 73 74 61 6e 63 65 5f  ..**   distance_
3127c 6f 70 74 20 3a 3a 3d 20 2f 20 49 4e 54 45 47 45  opt ::= / INTEGE
3127d 52 2e 0a 2a 2a 0a 2a 2a 20 20 20 70 68 72 61 73  R..**.**   phras
3127e 65 20 3a 3a 3d 20 54 4f 4b 45 4e 2e 0a 2a 2a 20  e ::= TOKEN..** 
3127f 20 20 70 68 72 61 73 65 20 3a 3a 3d 20 43 4f 4c    phrase ::= COL
31280 55 4d 4e 3a 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20  UMN:TOKEN..**   
31281 70 68 72 61 73 65 20 3a 3a 3d 20 22 54 4f 4b 45  phrase ::= "TOKE
31282 4e 20 54 4f 4b 45 4e 20 54 4f 4b 45 4e 2e 2e 2e  N TOKEN TOKEN...
31283 22 2e 0a 2a 2f 0a 0a 74 79 70 65 64 65 66 20 73  "..*/..typedef s
31284 74 72 75 63 74 20 46 74 73 33 45 78 70 72 20 46  truct Fts3Expr F
31285 74 73 33 45 78 70 72 3b 0a 74 79 70 65 64 65 66  ts3Expr;.typedef
31286 20 73 74 72 75 63 74 20 46 74 73 33 50 68 72 61   struct Fts3Phra
31287 73 65 20 46 74 73 33 50 68 72 61 73 65 3b 0a 0a  se Fts3Phrase;..
31288 2f 2a 0a 2a 2a 20 41 20 22 70 68 72 61 73 65 22  /*.** A "phrase"
31289 20 69 73 20 61 20 73 65 71 75 65 6e 63 65 20 6f   is a sequence o
3128a 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 6f  f one or more to
3128b 6b 65 6e 73 20 74 68 61 74 20 6d 75 73 74 20 6d  kens that must m
3128c 61 74 63 68 20 69 6e 0a 2a 2a 20 73 65 71 75 65  atch in.** seque
3128d 6e 63 65 2e 20 20 41 20 73 69 6e 67 6c 65 20 74  nce.  A single t
3128e 6f 6b 65 6e 20 69 73 20 74 68 65 20 62 61 73 65  oken is the base
3128f 20 63 61 73 65 20 61 6e 64 20 74 68 65 20 6d 6f   case and the mo
31290 73 74 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 2e 0a  st common case..
31291 2a 2a 20 46 6f 72 20 61 20 73 65 71 75 65 6e 63  ** For a sequenc
31292 65 20 6f 66 20 74 6f 6b 65 6e 73 20 63 6f 6e 74  e of tokens cont
31293 61 69 6e 65 64 20 69 6e 20 22 2e 2e 2e 22 2c 20  ained in "...", 
31294 6e 54 6f 6b 65 6e 20 77 69 6c 6c 20 62 65 20 74  nToken will be t
31295 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20  he number.** of 
31296 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 73 74  tokens in the st
31297 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ring..*/.struct 
31298 46 74 73 33 50 68 72 61 73 65 20 7b 0a 20 20 69  Fts3Phrase {.  i
31299 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20  nt nToken;      
3129a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3129b 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 70   tokens in the p
3129c 68 72 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  hrase */.  int i
3129d 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
3129e 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
3129f 6d 6e 20 74 68 69 73 20 70 68 72 61 73 65 20 6d  mn this phrase m
312a0 75 73 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 69  ust match */.  i
312a1 6e 74 20 69 73 4e 6f 74 3b 20 20 20 20 20 20 20  nt isNot;       
312a2 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20 70 72      /* Phrase pr
312a3 65 66 69 78 65 64 20 62 79 20 75 6e 61 72 79 20  efixed by unary 
312a4 6e 6f 74 20 28 2d 29 20 6f 70 65 72 61 74 6f 72  not (-) operator
312a5 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 50 68 72   */.  struct Phr
312a6 61 73 65 54 6f 6b 65 6e 20 7b 0a 20 20 20 20 63  aseToken {.    c
312a7 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
312a8 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20       /* Text of 
312a9 74 68 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  the token */.   
312aa 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
312ab 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
312ac 20 6f 66 20 62 79 74 65 73 20 69 6e 20 62 75 66   of bytes in buf
312ad 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  fer pointed to b
312ae 79 20 7a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  y z */.    int i
312af 73 50 72 65 66 69 78 3b 20 20 20 20 20 20 20 20  sPrefix;        
312b0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 6f 6b 65   /* True if toke
312b1 6e 20 65 6e 64 73 20 69 6e 20 77 69 74 68 20 61  n ends in with a
312b2 20 22 2a 22 20 63 68 61 72 61 63 74 65 72 20 2a   "*" character *
312b3 2f 0a 20 20 7d 20 61 54 6f 6b 65 6e 5b 31 5d 3b  /.  } aToken[1];
312b4 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
312b5 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 74  entry for each t
312b6 6f 6b 65 6e 20 69 6e 20 74 68 65 20 70 68 72 61  oken in the phra
312b7 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  se */.};../*.** 
312b8 41 20 74 72 65 65 20 6f 66 20 74 68 65 73 65 20  A tree of these 
312b9 6f 62 6a 65 63 74 73 20 66 6f 72 6d 73 20 74 68  objects forms th
312ba 65 20 52 48 53 20 6f 66 20 61 20 4d 41 54 43 48  e RHS of a MATCH
312bb 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74   operator..*/.st
312bc 72 75 63 74 20 46 74 73 33 45 78 70 72 20 7b 0a  ruct Fts3Expr {.
312bd 20 20 69 6e 74 20 65 54 79 70 65 3b 20 20 20 20    int eType;    
312be 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
312bf 4f 6e 65 20 6f 66 20 74 68 65 20 46 54 53 51 55  One of the FTSQU
312c0 45 52 59 5f 58 58 58 20 76 61 6c 75 65 73 20 64  ERY_XXX values d
312c1 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a  efined below */.
312c2 20 20 69 6e 74 20 6e 4e 65 61 72 3b 20 20 20 20    int nNear;    
312c3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
312c4 56 61 6c 69 64 20 69 66 20 65 54 79 70 65 3d 3d  Valid if eType==
312c5 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 2a 2f  FTSQUERY_NEAR */
312c6 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 50 61  .  Fts3Expr *pPa
312c7 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  rent;         /*
312c8 20 70 50 61 72 65 6e 74 2d 3e 70 4c 65 66 74 3d   pParent->pLeft=
312c9 3d 74 68 69 73 20 6f 72 20 70 50 61 72 65 6e 74  =this or pParent
312ca 2d 3e 70 52 69 67 68 74 3d 3d 74 68 69 73 20 2a  ->pRight==this *
312cb 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c  /.  Fts3Expr *pL
312cc 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  eft;           /
312cd 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a  * Left operand *
312ce 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 52  /.  Fts3Expr *pR
312cf 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f  ight;          /
312d0 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20  * Right operand 
312d1 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73 65 20  */.  Fts3Phrase 
312d2 2a 70 50 68 72 61 73 65 3b 20 20 20 20 20 20 20  *pPhrase;       
312d3 2f 2a 20 56 61 6c 69 64 20 69 66 20 65 54 79 70  /* Valid if eTyp
312d4 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e==FTSQUERY_PHRA
312d5 53 45 20 2a 2f 0a 7d 3b 0a 0a 53 51 4c 49 54 45  SE */.};..SQLITE
312d6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
312d7 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73  ite3Fts3ExprPars
312d8 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
312d9 7a 65 72 20 2a 2c 20 63 68 61 72 20 2a 2a 2c 20  zer *, char **, 
312da 69 6e 74 2c 20 69 6e 74 2c 20 0a 20 20 20 20 20  int, int, .     
312db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
312dc 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
312dd 2c 20 69 6e 74 2c 20 46 74 73 33 45 78 70 72 20  , int, Fts3Expr 
312de 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
312df 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
312e0 46 74 73 33 45 78 70 72 46 72 65 65 28 46 74 73  Fts3ExprFree(Fts
312e1 33 45 78 70 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  3Expr *);../*.**
312e2 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65   Candidate value
312e3 73 20 66 6f 72 20 46 74 73 33 51 75 65 72 79 2e  s for Fts3Query.
312e4 65 54 79 70 65 2e 20 4e 6f 74 65 20 74 68 61 74  eType. Note that
312e5 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68   the order of th
312e6 65 20 66 69 72 73 74 0a 2a 2a 20 66 6f 75 72 20  e first.** four 
312e7 76 61 6c 75 65 73 20 69 73 20 69 6e 20 6f 72 64  values is in ord
312e8 65 72 20 6f 66 20 70 72 65 63 65 64 65 6e 63 65  er of precedence
312e9 20 77 68 65 6e 20 70 61 72 73 69 6e 67 20 65 78   when parsing ex
312ea 70 72 65 73 73 69 6f 6e 73 2e 20 46 6f 72 20 0a  pressions. For .
312eb 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  ** example, the 
312ec 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
312ed 20 20 20 22 61 20 4f 52 20 62 20 41 4e 44 20 63     "a OR b AND c
312ee 20 4e 4f 54 20 64 20 4e 45 41 52 20 65 22 0a 2a   NOT d NEAR e".*
312ef 2a 0a 2a 2a 20 69 73 20 65 71 75 69 76 61 6c 65  *.** is equivale
312f0 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 22  nt to:.**.**   "
312f1 61 20 4f 52 20 28 62 20 41 4e 44 20 28 63 20 4e  a OR (b AND (c N
312f2 4f 54 20 28 64 20 4e 45 41 52 20 65 29 29 29 22  OT (d NEAR e)))"
312f3 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 51  .*/.#define FTSQ
312f4 55 45 52 59 5f 4e 45 41 52 20 20 20 31 0a 23 64  UERY_NEAR   1.#d
312f5 65 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4e  efine FTSQUERY_N
312f6 4f 54 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20  OT    2.#define 
312f7 46 54 53 51 55 45 52 59 5f 41 4e 44 20 20 20 20  FTSQUERY_AND    
312f8 33 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45  3.#define FTSQUE
312f9 52 59 5f 4f 52 20 20 20 20 20 34 0a 23 64 65 66  RY_OR     4.#def
312fa 69 6e 65 20 46 54 53 51 55 45 52 59 5f 50 48 52  ine FTSQUERY_PHR
312fb 41 53 45 20 35 0a 0a 23 69 66 64 65 66 20 53 51  ASE 5..#ifdef SQ
312fc 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
312fd 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
312fe 6c 69 74 65 33 46 74 73 33 45 78 70 72 49 6e 69  lite3Fts3ExprIni
312ff 74 54 65 73 74 49 6e 74 65 72 66 61 63 65 28 73  tTestInterface(s
31300 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6e  qlite3 *db);.#en
31301 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
31302 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
31303 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.h ********
31304 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31305 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31306 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
31307 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
31308 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
31309 66 20 69 6e 20 66 74 73 33 2e 63 20 2a 2a 2a 2a  f in fts3.c ****
3130a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3130b 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
3130c 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73  **** Include fts
3130d 33 5f 68 61 73 68 2e 68 20 69 6e 20 74 68 65 20  3_hash.h in the 
3130e 6d 69 64 64 6c 65 20 6f 66 20 66 74 73 33 2e 63  middle of fts3.c
3130f 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
31310 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
31311 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
31312 66 74 73 33 5f 68 61 73 68 2e 68 20 2a 2a 2a 2a  fts3_hash.h ****
31313 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31314 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31315 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
31316 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a  September 22.**.
31317 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
31318 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
31319 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
3131a 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
3131b 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
3131c 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
3131d 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
3131e 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
3131f 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
31320 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
31321 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
31322 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
31323 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
31324 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
31325 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
31326 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
31327 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
31328 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
31329 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3132a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3132b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3132c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
3132d 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65  his is the heade
3132e 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67  r file for the g
3132f 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c  eneric hash-tabl
31330 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a  e implemenation.
31331 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  ** used in SQLit
31332 65 2e 20 20 57 65 27 76 65 20 6d 6f 64 69 66 69  e.  We've modifi
31333 65 64 20 69 74 20 73 6c 69 67 68 74 6c 79 20 74  ed it slightly t
31334 6f 20 73 65 72 76 65 20 61 73 20 61 20 73 74 61  o serve as a sta
31335 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 68 61 73 68 20  ndalone.** hash 
31336 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
31337 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 75 6c  tion for the ful
31338 6c 2d 74 65 78 74 20 69 6e 64 65 78 69 6e 67 20  l-text indexing 
31339 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2f 0a 23 69  module..**.*/.#i
3133a 66 6e 64 65 66 20 5f 46 54 53 33 5f 48 41 53 48  fndef _FTS3_HASH
3133b 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 46 54 53  _H_.#define _FTS
3133c 33 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f  3_HASH_H_../* Fo
3133d 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
3133e 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 73  ns of structures
3133f 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
31340 75 63 74 20 66 74 73 33 48 61 73 68 20 66 74 73  uct fts3Hash fts
31341 33 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73  3Hash;.typedef s
31342 74 72 75 63 74 20 66 74 73 33 48 61 73 68 45 6c  truct fts3HashEl
31343 65 6d 20 66 74 73 33 48 61 73 68 45 6c 65 6d 3b  em fts3HashElem;
31344 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20  ../* A complete 
31345 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e  hash table is an
31346 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
31347 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
31348 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74  ture..** The int
31349 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73  ernals of this s
3134a 74 72 75 63 74 75 72 65 20 61 72 65 20 69 6e 74  tructure are int
3134b 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
3134c 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20  ue -- client.** 
3134d 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  code should not 
3134e 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73  attempt to acces
3134f 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20  s or modify the 
31350 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73  fields of this s
31351 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65  tructure.** dire
31352 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68  ctly.  Change th
31353 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c  is structure onl
31354 79 20 62 79 20 75 73 69 6e 67 20 74 68 65 20 72  y by using the r
31355 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a  outines below..*
31356 2a 20 48 6f 77 65 76 65 72 2c 20 6d 61 6e 79 20  * However, many 
31357 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64 75 72  of the "procedur
31358 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f  es" and "functio
31359 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e  ns" for modifyin
3135a 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69  g and.** accessi
3135b 6e 67 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ng this structur
3135c 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63  e are really mac
3135d 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74  ros, so we can't
3135e 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20   really make.** 
3135f 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
31360 70 61 71 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  paque..*/.struct
31361 20 66 74 73 33 48 61 73 68 20 7b 0a 20 20 63 68   fts3Hash {.  ch
31362 61 72 20 6b 65 79 43 6c 61 73 73 3b 20 20 20 20  ar keyClass;    
31363 20 20 20 20 20 20 2f 2a 20 48 41 53 48 5f 49 4e        /* HASH_IN
31364 54 2c 20 5f 50 4f 49 4e 54 45 52 2c 20 5f 53 54  T, _POINTER, _ST
31365 52 49 4e 47 2c 20 5f 42 49 4e 41 52 59 20 2a 2f  RING, _BINARY */
31366 0a 20 20 63 68 61 72 20 63 6f 70 79 4b 65 79 3b  .  char copyKey;
31367 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
31368 75 65 20 69 66 20 63 6f 70 79 20 6f 66 20 6b 65  ue if copy of ke
31369 79 20 6d 61 64 65 20 6f 6e 20 69 6e 73 65 72 74  y made on insert
3136a 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b   */.  int count;
3136b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3136c 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
3136d 65 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  es in this table
3136e 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c   */.  fts3HashEl
3136f 65 6d 20 2a 66 69 72 73 74 3b 20 20 20 20 2f 2a  em *first;    /*
31370 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   The first eleme
31371 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20  nt of the array 
31372 2a 2f 0a 20 20 69 6e 74 20 68 74 73 69 7a 65 3b  */.  int htsize;
31373 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31374 4e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74  Number of bucket
31375 73 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61  s in the hash ta
31376 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ble */.  struct 
31377 5f 66 74 73 33 68 74 20 7b 20 20 20 20 20 20 20  _fts3ht {       
31378 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61 62   /* the hash tab
31379 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f  le */.    int co
3137a 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  unt;            
3137b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
3137c 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 69  entries with thi
3137d 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 66 74  s hash */.    ft
3137e 73 33 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69  s3HashElem *chai
3137f 6e 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  n;     /* Pointe
31380 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79  r to first entry
31381 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20   with this hash 
31382 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a  */.  } *ht;.};..
31383 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 20  /* Each element 
31384 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
31385 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
31386 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
31387 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  g .** structure.
31388 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61    All elements a
31389 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73  re stored on a s
3138a 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e  ingle doubly-lin
3138b 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ked list..**.** 
3138c 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74 72 75  Again, this stru
3138d 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e 64 65  cture is intende
3138e 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 2c 20  d to be opaque, 
3138f 62 75 74 20 69 74 20 63 61 6e 27 74 20 72 65 61  but it can't rea
31390 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65  lly.** be opaque
31391 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 75   because it is u
31392 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a  sed by macros..*
31393 2f 0a 73 74 72 75 63 74 20 66 74 73 33 48 61 73  /.struct fts3Has
31394 68 45 6c 65 6d 20 7b 0a 20 20 66 74 73 33 48 61  hElem {.  fts3Ha
31395 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70  shElem *next, *p
31396 72 65 76 3b 20 2f 2a 20 4e 65 78 74 20 61 6e 64  rev; /* Next and
31397 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e   previous elemen
31398 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ts in the table 
31399 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b  */.  void *data;
3139a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3139b 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69 61 74  /* Data associat
3139c 65 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65  ed with this ele
3139d 6d 65 6e 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ment */.  void *
3139e 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20  pKey; int nKey; 
3139f 20 20 20 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f       /* Key asso
313a0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
313a1 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a   element */.};..
313a2 2f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  /*.** There are 
313a3 32 20 64 69 66 66 65 72 65 6e 74 20 6d 6f 64 65  2 different mode
313a4 73 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 66  s of operation f
313a5 6f 72 20 61 20 68 61 73 68 20 74 61 62 6c 65 3a  or a hash table:
313a6 0a 2a 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41  .**.**   FTS3_HA
313a7 53 48 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20  SH_STRING       
313a8 20 70 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20   pKey points to 
313a9 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73  a string that is
313aa 20 6e 4b 65 79 20 62 79 74 65 73 20 6c 6f 6e 67   nKey bytes long
313ab 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
313ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69                (i
313ad 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 6e 75 6c  ncluding the nul
313ae 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66  l-terminator, if
313af 20 61 6e 79 29 2e 20 20 43 61 73 65 0a 2a 2a 20   any).  Case.** 
313b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
313b1 20 20 20 20 20 20 20 20 20 20 69 73 20 72 65 73            is res
313b2 70 65 63 74 65 64 20 69 6e 20 63 6f 6d 70 61 72  pected in compar
313b3 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46  isons..**.**   F
313b4 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20  TS3_HASH_BINARY 
313b5 20 20 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e         pKey poin
313b6 74 73 20 74 6f 20 62 69 6e 61 72 79 20 64 61 74  ts to binary dat
313b7 61 20 6e 4b 65 79 20 62 79 74 65 73 20 6c 6f 6e  a nKey bytes lon
313b8 67 2e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  g. .**          
313b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
313ba 20 6d 65 6d 63 6d 70 28 29 20 69 73 20 75 73 65   memcmp() is use
313bb 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 6b 65 79  d to compare key
313bc 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 70 79 20  s..**.** A copy 
313bd 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20 6d 61  of the key is ma
313be 64 65 20 69 66 20 74 68 65 20 63 6f 70 79 4b 65  de if the copyKe
313bf 79 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 66  y parameter to f
313c0 74 73 33 48 61 73 68 49 6e 69 74 20 69 73 20 31  ts3HashInit is 1
313c1 2e 20 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  .  .*/.#define F
313c2 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20  TS3_HASH_STRING 
313c3 20 20 20 31 0a 23 64 65 66 69 6e 65 20 46 54 53     1.#define FTS
313c4 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 20 20  3_HASH_BINARY   
313c5 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73   2../*.** Access
313c6 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64   routines.  To d
313c7 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20  elete, insert a 
313c8 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
313c9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
313ca 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
313cb 48 61 73 68 49 6e 69 74 28 66 74 73 33 48 61 73  HashInit(fts3Has
313cc 68 2a 2c 20 69 6e 74 20 6b 65 79 74 79 70 65 2c  h*, int keytype,
313cd 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 3b 0a 53   int copyKey);.S
313ce 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
313cf 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
313d0 61 73 68 49 6e 73 65 72 74 28 66 74 73 33 48 61  ashInsert(fts3Ha
313d1 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  sh*, const void 
313d2 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c  *pKey, int nKey,
313d3 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53   void *pData);.S
313d4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
313d5 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
313d6 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 66 74  ashFind(const ft
313d7 73 33 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76  s3Hash*, const v
313d8 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
313d9 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Key);.SQLITE_PRI
313da 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
313db 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 66  3Fts3HashClear(f
313dc 74 73 33 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a  ts3Hash*);../*.*
313dd 2a 20 53 68 6f 72 74 68 61 6e 64 20 66 6f 72 20  * Shorthand for 
313de 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 62  the functions ab
313df 6f 76 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ove.*/.#define f
313e0 74 73 33 48 61 73 68 49 6e 69 74 20 20 20 73 71  ts3HashInit   sq
313e1 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69  lite3Fts3HashIni
313e2 74 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  t.#define fts3Ha
313e3 73 68 49 6e 73 65 72 74 20 73 71 6c 69 74 65 33  shInsert sqlite3
313e4 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 0a 23  Fts3HashInsert.#
313e5 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 46  define fts3HashF
313e6 69 6e 64 20 20 20 73 71 6c 69 74 65 33 46 74 73  ind   sqlite3Fts
313e7 33 48 61 73 68 46 69 6e 64 0a 23 64 65 66 69 6e  3HashFind.#defin
313e8 65 20 66 74 73 33 48 61 73 68 43 6c 65 61 72 20  e fts3HashClear 
313e9 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
313ea 43 6c 65 61 72 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  Clear../*.** Mac
313eb 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20  ros for looping 
313ec 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74  over all element
313ed 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c  s of a hash tabl
313ee 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73  e.  The idiom is
313ef 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  .** like this:.*
313f0 2a 0a 2a 2a 20 20 20 66 74 73 33 48 61 73 68 20  *.**   fts3Hash 
313f1 68 3b 0a 2a 2a 20 20 20 66 74 73 33 48 61 73 68  h;.**   fts3Hash
313f2 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e  Elem *p;.**   ..
313f3 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d 66 74 73  ..**   for(p=fts
313f4 33 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20  3HashFirst(&h); 
313f5 70 3b 20 70 3d 66 74 73 33 48 61 73 68 4e 65 78  p; p=fts3HashNex
313f6 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f  t(p)){.**     So
313f7 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44 61  meStructure *pDa
313f8 74 61 20 3d 20 66 74 73 33 48 61 73 68 44 61 74  ta = fts3HashDat
313f9 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20  a(p);.**     // 
313fa 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74  do something wit
313fb 68 20 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a  h pData.**   }.*
313fc 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  /.#define fts3Ha
313fd 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48 29  shFirst(H)  ((H)
313fe 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65  ->first).#define
313ff 20 66 74 73 33 48 61 73 68 4e 65 78 74 28 45 29   fts3HashNext(E)
31400 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23     ((E)->next).#
31401 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 44  define fts3HashD
31402 61 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64  ata(E)   ((E)->d
31403 61 74 61 29 0a 23 64 65 66 69 6e 65 20 66 74 73  ata).#define fts
31404 33 48 61 73 68 4b 65 79 28 45 29 20 20 20 20 28  3HashKey(E)    (
31405 28 45 29 2d 3e 70 4b 65 79 29 0a 23 64 65 66 69  (E)->pKey).#defi
31406 6e 65 20 66 74 73 33 48 61 73 68 4b 65 79 73 69  ne fts3HashKeysi
31407 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79  ze(E) ((E)->nKey
31408 29 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20  )../*.** Number 
31409 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20  of entries in a 
3140a 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64  hash table.*/.#d
3140b 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 43 6f  efine fts3HashCo
3140c 75 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f  unt(H)  ((H)->co
3140d 75 6e 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  unt)..#endif /* 
3140e 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 20 2a 2f  _FTS3_HASH_H_ */
3140f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
31410 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 68 61  * End of fts3_ha
31411 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
31412 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31413 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31414 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
31415 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
31416 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
31417 6e 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  n fts3.c *******
31418 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31419 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
3141a 5f 43 4f 52 45 20 0a 20 20 53 51 4c 49 54 45 5f  _CORE .  SQLITE_
3141b 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a  EXTENSION_INIT1.
3141c 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 54 4f 44 4f  #endif.../* TODO
3141d 28 73 68 65 73 73 29 20 4d 41 4e 2c 20 74 68 69  (shess) MAN, thi
3141e 73 20 74 68 69 6e 67 20 6e 65 65 64 73 20 73 6f  s thing needs so
3141f 6d 65 20 72 65 66 61 63 74 6f 72 69 6e 67 2e 20  me refactoring. 
31420 20 41 74 20 6d 69 6e 69 6d 75 6d 2c 20 69 74 0a   At minimum, it.
31421 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65  ** would be nice
31422 20 74 6f 20 6f 72 64 65 72 20 74 68 65 20 66 69   to order the fi
31423 6c 65 20 62 65 74 74 65 72 2c 20 70 65 72 68 61  le better, perha
31424 70 73 20 73 6f 6d 65 74 68 69 6e 67 20 61 6c 6f  ps something alo
31425 6e 67 20 74 68 65 0a 2a 2a 20 6c 69 6e 65 73 20  ng the.** lines 
31426 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 2d 20 75 74 69  of:.**.**  - uti
31427 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  lity functions.*
31428 2a 20 20 2d 20 74 61 62 6c 65 20 73 65 74 75 70  *  - table setup
31429 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d   functions.**  -
3142a 20 74 61 62 6c 65 20 75 70 64 61 74 65 20 66 75   table update fu
3142b 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61  nctions.**  - ta
3142c 62 6c 65 20 71 75 65 72 79 20 66 75 6e 63 74 69  ble query functi
3142d 6f 6e 73 0a 2a 2a 0a 2a 2a 20 50 75 74 20 74 68  ons.**.** Put th
3142e 65 20 71 75 65 72 79 20 66 75 6e 63 74 69 6f 6e  e query function
3142f 73 20 6c 61 73 74 20 62 65 63 61 75 73 65 20 74  s last because t
31430 68 65 79 27 72 65 20 6c 69 6b 65 6c 79 20 74 6f  hey're likely to
31431 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 79   reference.** ty
31432 70 65 64 65 66 73 20 6f 72 20 66 75 6e 63 74 69  pedefs or functi
31433 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 74 61 62  ons from the tab
31434 6c 65 20 75 70 64 61 74 65 20 73 65 63 74 69 6f  le update sectio
31435 6e 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 23 20 64  n..*/..#if 0.# d
31436 65 66 69 6e 65 20 46 54 53 54 52 41 43 45 28 41  efine FTSTRACE(A
31437 29 20 20 70 72 69 6e 74 66 20 41 3b 20 66 66 6c  )  printf A; ffl
31438 75 73 68 28 73 74 64 6f 75 74 29 0a 23 65 6c 73  ush(stdout).#els
31439 65 0a 23 20 64 65 66 69 6e 65 20 46 54 53 54 52  e.# define FTSTR
3143a 41 43 45 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f  ACE(A).#endif../
3143b 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65  * It is not safe
3143c 20 74 6f 20 63 61 6c 6c 20 69 73 73 70 61 63 65   to call isspace
3143d 28 29 2c 20 74 6f 6c 6f 77 65 72 28 29 2c 20 6f  (), tolower(), o
3143e 72 20 69 73 61 6c 6e 75 6d 28 29 20 6f 6e 0a 2a  r isalnum() on.*
3143f 2a 20 68 69 2d 62 69 74 2d 73 65 74 20 63 68 61  * hi-bit-set cha
31440 72 61 63 74 65 72 73 2e 20 20 54 68 69 73 20 69  racters.  This i
31441 73 20 74 68 65 20 73 61 6d 65 20 73 6f 6c 75 74  s the same solut
31442 69 6f 6e 20 75 73 65 64 20 69 6e 20 74 68 65 0a  ion used in the.
31443 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f  ** tokenizer..*/
31444 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
31445 54 68 65 20 73 6e 69 70 70 65 74 2d 67 65 6e 65  The snippet-gene
31446 72 61 74 69 6f 6e 20 63 6f 64 65 20 73 68 6f 75  ration code shou
31447 6c 64 20 62 65 20 75 73 69 6e 67 20 74 68 65 0a  ld be using the.
31448 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 2d 67 65 6e  ** tokenizer-gen
31449 65 72 61 74 65 64 20 74 6f 6b 65 6e 73 20 72 61  erated tokens ra
3144a 74 68 65 72 20 74 68 61 6e 20 64 6f 69 6e 67 20  ther than doing 
3144b 69 74 73 20 6f 77 6e 20 6c 6f 63 61 6c 0a 2a 2a  its own local.**
3144c 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a   tokenization..*
3144d 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
3144e 20 49 73 20 5f 5f 69 73 61 73 63 69 69 28 29 20   Is __isascii() 
3144f 61 20 70 6f 72 74 61 62 6c 65 20 76 65 72 73 69  a portable versi
31450 6f 6e 20 6f 66 20 28 63 26 30 78 38 30 29 3d 3d  on of (c&0x80)==
31451 30 3f 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  0? */.static int
31452 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 63 68   safe_isspace(ch
31453 61 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  ar c){.  return 
31454 28 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73  (c&0x80)==0 ? is
31455 73 70 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a  space(c) : 0;.}.
31456 73 74 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f  static int safe_
31457 74 6f 6c 6f 77 65 72 28 63 68 61 72 20 63 29 7b  tolower(char c){
31458 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38  .  return (c&0x8
31459 30 29 3d 3d 30 20 3f 20 74 6f 6c 6f 77 65 72 28  0)==0 ? tolower(
3145a 63 29 20 3a 20 63 3b 0a 7d 0a 73 74 61 74 69 63  c) : c;.}.static
3145b 20 69 6e 74 20 73 61 66 65 5f 69 73 61 6c 6e 75   int safe_isalnu
3145c 6d 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74  m(char c){.  ret
3145d 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20  urn (c&0x80)==0 
3145e 3f 20 69 73 61 6c 6e 75 6d 28 63 29 20 3a 20 30  ? isalnum(c) : 0
3145f 3b 0a 7d 0a 0a 74 79 70 65 64 65 66 20 65 6e 75  ;.}..typedef enu
31460 6d 20 44 6f 63 4c 69 73 74 54 79 70 65 20 7b 0a  m DocListType {.
31461 20 20 44 4c 5f 44 4f 43 49 44 53 2c 20 20 20 20    DL_DOCIDS,    
31462 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 6f 63            /* doc
31463 69 64 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 44 4c  ids only */.  DL
31464 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 20 20 20 20  _POSITIONS,     
31465 20 20 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20        /* docids 
31466 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20  + positions */. 
31467 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46   DL_POSITIONS_OF
31468 46 53 45 54 53 20 20 20 20 2f 2a 20 64 6f 63 69  FSETS    /* doci
31469 64 73 20 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2b  ds + positions +
3146a 20 6f 66 66 73 65 74 73 20 2a 2f 0a 7d 20 44 6f   offsets */.} Do
3146b 63 4c 69 73 74 54 79 70 65 3b 0a 0a 2f 2a 0a 2a  cListType;../*.*
3146c 2a 20 42 79 20 64 65 66 61 75 6c 74 2c 20 6f 6e  * By default, on
3146d 6c 79 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64  ly positions and
3146e 20 6e 6f 74 20 6f 66 66 73 65 74 73 20 61 72 65   not offsets are
3146f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
31470 6f 63 6c 69 73 74 73 2e 0a 2a 2a 20 54 6f 20 63  oclists..** To c
31471 68 61 6e 67 65 20 74 68 69 73 20 73 6f 20 74 68  hange this so th
31472 61 74 20 6f 66 66 73 65 74 73 20 61 72 65 20 73  at offsets are s
31473 74 6f 72 65 64 20 74 6f 6f 2c 20 63 6f 6d 70 69  tored too, compi
31474 6c 65 20 77 69 74 68 0a 2a 2a 0a 2a 2a 20 20 20  le with.**.**   
31475 20 20 20 20 20 20 20 2d 44 44 4c 5f 44 45 46 41         -DDL_DEFA
31476 55 4c 54 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  ULT=DL_POSITIONS
31477 5f 4f 46 46 53 45 54 53 0a 2a 2a 0a 2a 2a 20 49  _OFFSETS.**.** I
31478 66 20 44 4c 5f 44 45 46 41 55 4c 54 20 69 73 20  f DL_DEFAULT is 
31479 73 65 74 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53  set to DL_DOCIDS
3147a 2c 20 79 6f 75 72 20 74 61 62 6c 65 20 63 61 6e  , your table can
3147b 20 6f 6e 6c 79 20 62 65 20 69 6e 73 65 72 74 65   only be inserte
3147c 64 0a 2a 2a 20 69 6e 74 6f 20 28 6e 6f 20 64 65  d.** into (no de
3147d 6c 65 74 65 73 20 6f 72 20 75 70 64 61 74 65 73  letes or updates
3147e 29 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 44 4c  )..*/.#ifndef DL
3147f 5f 44 45 46 41 55 4c 54 0a 23 20 64 65 66 69 6e  _DEFAULT.# defin
31480 65 20 44 4c 5f 44 45 46 41 55 4c 54 20 44 4c 5f  e DL_DEFAULT DL_
31481 50 4f 53 49 54 49 4f 4e 53 0a 23 65 6e 64 69 66  POSITIONS.#endif
31482 0a 0a 65 6e 75 6d 20 7b 0a 20 20 50 4f 53 5f 45  ..enum {.  POS_E
31483 4e 44 20 3d 20 30 2c 20 20 20 20 20 20 20 20 2f  ND = 0,        /
31484 2a 20 65 6e 64 20 6f 66 20 74 68 69 73 20 70 6f  * end of this po
31485 73 69 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20  sition list */. 
31486 20 50 4f 53 5f 43 4f 4c 55 4d 4e 2c 20 20 20 20   POS_COLUMN,    
31487 20 20 20 20 20 2f 2a 20 66 6f 6c 6c 6f 77 65 64       /* followed
31488 20 62 79 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e   by new column n
31489 75 6d 62 65 72 20 2a 2f 0a 20 20 50 4f 53 5f 42  umber */.  POS_B
3148a 41 53 45 0a 7d 3b 0a 0a 2f 2a 20 4d 45 52 47 45  ASE.};../* MERGE
3148b 5f 43 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20  _COUNT controls 
3148c 68 6f 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72  how often we mer
3148d 67 65 20 73 65 67 6d 65 6e 74 73 20 28 73 65 65  ge segments (see
3148e 20 63 6f 6d 6d 65 6e 74 20 61 74 0a 2a 2a 20 74   comment at.** t
3148f 6f 70 20 6f 66 20 66 69 6c 65 29 2e 0a 2a 2f 0a  op of file)..*/.
31490 23 64 65 66 69 6e 65 20 4d 45 52 47 45 5f 43 4f  #define MERGE_CO
31491 55 4e 54 20 31 36 0a 0a 2f 2a 20 75 74 69 6c 69  UNT 16../* utili
31492 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  ty functions */.
31493 0a 2f 2a 20 43 4c 45 41 52 28 29 20 61 6e 64 20  ./* CLEAR() and 
31494 53 43 52 41 4d 42 4c 45 28 29 20 61 62 73 74 72  SCRAMBLE() abstr
31495 61 63 74 20 6d 65 6d 73 65 74 28 29 20 6f 6e 20  act memset() on 
31496 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
31497 69 6e 67 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 20  ingle.** record 
31498 74 6f 20 70 72 65 76 65 6e 74 20 65 72 72 6f 72  to prevent error
31499 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
3149a 2a 0a 2a 2a 20 6d 79 5f 66 75 6e 63 74 69 6f 6e  *.** my_function
3149b 28 53 6f 6d 65 54 79 70 65 20 2a 62 29 7b 0a 2a  (SomeType *b){.*
3149c 2a 20 20 20 6d 65 6d 73 65 74 28 62 2c 20 27 5c  *   memset(b, '\
3149d 30 27 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 20  0', sizeof(b)); 
3149e 20 2f 2f 20 73 69 7a 65 6f 66 28 62 29 21 3d 73   // sizeof(b)!=s
3149f 69 7a 65 6f 66 28 2a 62 29 0a 2a 2a 20 7d 0a 2a  izeof(*b).** }.*
314a0 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
314a1 20 4f 62 76 69 6f 75 73 20 63 61 6e 64 69 64 61   Obvious candida
314a2 74 65 73 20 66 6f 72 20 61 20 68 65 61 64 65 72  tes for a header
314a3 20 66 69 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e   file. */.#defin
314a4 65 20 43 4c 45 41 52 28 62 29 20 6d 65 6d 73 65  e CLEAR(b) memse
314a5 74 28 62 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f  t(b, '\0', sizeo
314a6 66 28 2a 28 62 29 29 29 0a 0a 23 69 66 6e 64 65  f(*(b)))..#ifnde
314a7 66 20 4e 44 45 42 55 47 0a 23 20 20 64 65 66 69  f NDEBUG.#  defi
314a8 6e 65 20 53 43 52 41 4d 42 4c 45 28 62 29 20 6d  ne SCRAMBLE(b) m
314a9 65 6d 73 65 74 28 62 2c 20 30 78 35 35 2c 20 73  emset(b, 0x55, s
314aa 69 7a 65 6f 66 28 2a 28 62 29 29 29 0a 23 65 6c  izeof(*(b))).#el
314ab 73 65 0a 23 20 20 64 65 66 69 6e 65 20 53 43 52  se.#  define SCR
314ac 41 4d 42 4c 45 28 62 29 0a 23 65 6e 64 69 66 0a  AMBLE(b).#endif.
314ad 0a 2f 2a 20 57 65 20 6d 61 79 20 6e 65 65 64 20  ./* We may need 
314ae 75 70 20 74 6f 20 56 41 52 49 4e 54 5f 4d 41 58  up to VARINT_MAX
314af 20 62 79 74 65 73 20 74 6f 20 73 74 6f 72 65 20   bytes to store 
314b0 61 6e 20 65 6e 63 6f 64 65 64 20 36 34 2d 62 69  an encoded 64-bi
314b1 74 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 23 64  t integer. */.#d
314b2 65 66 69 6e 65 20 56 41 52 49 4e 54 5f 4d 41 58  efine VARINT_MAX
314b3 20 31 30 0a 0a 2f 2a 20 57 72 69 74 65 20 61 20   10../* Write a 
314b4 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d  64-bit variable-
314b5 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74  length integer t
314b6 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e  o memory startin
314b7 67 20 61 74 20 70 5b 30 5d 2e 0a 20 2a 20 54 68  g at p[0].. * Th
314b8 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61  e length of data
314b9 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65   written will be
314ba 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 56   between 1 and V
314bb 41 52 49 4e 54 5f 4d 41 58 20 62 79 74 65 73 2e  ARINT_MAX bytes.
314bc 0a 20 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  . * The number o
314bd 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20  f bytes written 
314be 69 73 20 72 65 74 75 72 6e 65 64 2e 20 2a 2f 0a  is returned. */.
314bf 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50  static int fts3P
314c0 75 74 56 61 72 69 6e 74 28 63 68 61 72 20 2a 70  utVarint(char *p
314c1 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76  , sqlite_int64 v
314c2 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
314c3 61 72 20 2a 71 20 3d 20 28 75 6e 73 69 67 6e 65  ar *q = (unsigne
314c4 64 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73  d char *) p;.  s
314c5 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 76 75 20  qlite_uint64 vu 
314c6 3d 20 76 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 2a  = v;.  do{.    *
314c7 71 2b 2b 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  q++ = (unsigned 
314c8 63 68 61 72 29 20 28 28 76 75 20 26 20 30 78 37  char) ((vu & 0x7
314c9 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20  f) | 0x80);.    
314ca 76 75 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69  vu >>= 7;.  }whi
314cb 6c 65 28 20 76 75 21 3d 30 20 29 3b 0a 20 20 71  le( vu!=0 );.  q
314cc 5b 2d 31 5d 20 26 3d 20 30 78 37 66 3b 20 20 2f  [-1] &= 0x7f;  /
314cd 2a 20 74 75 72 6e 20 6f 66 66 20 68 69 67 68 20  * turn off high 
314ce 62 69 74 20 69 6e 20 66 69 6e 61 6c 20 62 79 74  bit in final byt
314cf 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 71  e */.  assert( q
314d0 20 2d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   - (unsigned cha
314d1 72 20 2a 29 70 20 3c 3d 20 56 41 52 49 4e 54 5f  r *)p <= VARINT_
314d2 4d 41 58 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  MAX );.  return 
314d3 28 69 6e 74 29 20 28 71 20 2d 20 28 75 6e 73 69  (int) (q - (unsi
314d4 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 3b 0a  gned char *)p);.
314d5 7d 0a 0a 2f 2a 20 52 65 61 64 20 61 20 36 34 2d  }../* Read a 64-
314d6 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  bit variable-len
314d7 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d  gth integer from
314d8 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
314d9 20 61 74 20 70 5b 30 5d 2e 0a 20 2a 20 52 65 74   at p[0].. * Ret
314da 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
314db 66 20 62 79 74 65 73 20 72 65 61 64 2c 20 6f 72  f bytes read, or
314dc 20 30 20 6f 6e 20 65 72 72 6f 72 2e 0a 20 2a 20   0 on error.. * 
314dd 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f  The value is sto
314de 72 65 64 20 69 6e 20 2a 76 2e 20 2a 2f 0a 73 74  red in *v. */.st
314df 61 74 69 63 20 69 6e 74 20 66 74 73 33 47 65 74  atic int fts3Get
314e0 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 63 68 61  Varint(const cha
314e1 72 20 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e 74  r *p, sqlite_int
314e2 36 34 20 2a 76 29 7b 0a 20 20 63 6f 6e 73 74 20  64 *v){.  const 
314e3 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71  unsigned char *q
314e4 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e   = (const unsign
314e5 65 64 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20  ed char *) p;.  
314e6 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 78 20  sqlite_uint64 x 
314e7 3d 20 30 2c 20 79 20 3d 20 31 3b 0a 20 20 77 68  = 0, y = 1;.  wh
314e8 69 6c 65 28 20 28 2a 71 20 26 20 30 78 38 30 29  ile( (*q & 0x80)
314e9 20 3d 3d 20 30 78 38 30 20 29 7b 0a 20 20 20 20   == 0x80 ){.    
314ea 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 20 26  x += y * (*q++ &
314eb 20 30 78 37 66 29 3b 0a 20 20 20 20 79 20 3c 3c   0x7f);.    y <<
314ec 3d 20 37 3b 0a 20 20 20 20 69 66 28 20 71 20 2d  = 7;.    if( q -
314ed 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
314ee 2a 29 70 20 3e 3d 20 56 41 52 49 4e 54 5f 4d 41  *)p >= VARINT_MA
314ef 58 20 29 7b 20 20 2f 2a 20 62 61 64 20 64 61 74  X ){  /* bad dat
314f0 61 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  a */.      asser
314f1 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 72 65  t( 0 );.      re
314f2 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
314f3 7d 0a 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71  }.  x += y * (*q
314f4 2b 2b 29 3b 0a 20 20 2a 76 20 3d 20 28 73 71 6c  ++);.  *v = (sql
314f5 69 74 65 5f 69 6e 74 36 34 29 20 78 3b 0a 20 20  ite_int64) x;.  
314f6 72 65 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20  return (int) (q 
314f7 2d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  - (unsigned char
314f8 20 2a 29 70 29 3b 0a 7d 0a 0a 73 74 61 74 69 63   *)p);.}..static
314f9 20 69 6e 74 20 66 74 73 33 47 65 74 56 61 72 69   int fts3GetVari
314fa 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20  nt32(const char 
314fb 2a 70 2c 20 69 6e 74 20 2a 70 69 29 7b 0a 20 73  *p, int *pi){. s
314fc 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 3b 0a 20  qlite_int64 i;. 
314fd 69 6e 74 20 72 65 74 20 3d 20 66 74 73 33 47 65  int ret = fts3Ge
314fe 74 56 61 72 69 6e 74 28 70 2c 20 26 69 29 3b 0a  tVarint(p, &i);.
314ff 20 2a 70 69 20 3d 20 28 69 6e 74 29 20 69 3b 0a   *pi = (int) i;.
31500 20 61 73 73 65 72 74 28 20 2a 70 69 3d 3d 69 20   assert( *pi==i 
31501 29 3b 0a 20 72 65 74 75 72 6e 20 72 65 74 3b 0a  );. return ret;.
31502 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
31503 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31504 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31505 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31506 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 61 74 61  *******/./* Data
31507 42 75 66 66 65 72 20 69 73 20 75 73 65 64 20 74  Buffer is used t
31508 6f 20 63 6f 6c 6c 65 63 74 20 64 61 74 61 20 69  o collect data i
31509 6e 74 6f 20 61 20 62 75 66 66 65 72 20 69 6e 20  nto a buffer in 
3150a 70 69 65 63 65 6d 65 61 6c 0a 2a 2a 20 66 61 73  piecemeal.** fas
3150b 68 69 6f 6e 2e 20 20 49 74 20 69 6d 70 6c 65 6d  hion.  It implem
3150c 65 6e 74 73 20 74 68 65 20 75 73 75 61 6c 20 64  ents the usual d
3150d 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65  istinction betwe
3150e 65 6e 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20  en amount of.** 
3150f 64 61 74 61 20 63 75 72 72 65 6e 74 6c 79 20 73  data currently s
31510 74 6f 72 65 64 20 28 6e 44 61 74 61 29 20 61 6e  tored (nData) an
31511 64 20 62 75 66 66 65 72 20 63 61 70 61 63 69 74  d buffer capacit
31512 79 20 28 6e 43 61 70 61 63 69 74 79 29 2e 0a 2a  y (nCapacity)..*
31513 2a 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 49  *.** dataBufferI
31514 6e 69 74 20 2d 20 63 72 65 61 74 65 20 61 20 62  nit - create a b
31515 75 66 66 65 72 20 77 69 74 68 20 67 69 76 65 6e  uffer with given
31516 20 69 6e 69 74 69 61 6c 20 63 61 70 61 63 69 74   initial capacit
31517 79 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72  y..** dataBuffer
31518 52 65 73 65 74 20 2d 20 66 6f 72 67 65 74 20 62  Reset - forget b
31519 75 66 66 65 72 27 73 20 64 61 74 61 2c 20 72 65  uffer's data, re
3151a 74 61 69 6e 69 6e 67 20 63 61 70 61 63 69 74 79  taining capacity
3151b 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 44  ..** dataBufferD
3151c 65 73 74 72 6f 79 20 2d 20 66 72 65 65 20 62 75  estroy - free bu
3151d 66 66 65 72 27 73 20 64 61 74 61 2e 0a 2a 2a 20  ffer's data..** 
3151e 64 61 74 61 42 75 66 66 65 72 53 77 61 70 20 2d  dataBufferSwap -
3151f 20 73 77 61 70 20 63 6f 6e 74 65 6e 74 73 20 6f   swap contents o
31520 66 20 74 77 6f 20 62 75 66 66 65 72 73 2e 0a 2a  f two buffers..*
31521 2a 20 64 61 74 61 42 75 66 66 65 72 45 78 70 61  * dataBufferExpa
31522 6e 64 20 2d 20 65 78 70 61 6e 64 20 63 61 70 61  nd - expand capa
31523 63 69 74 79 20 77 69 74 68 6f 75 74 20 61 64 64  city without add
31524 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74  ing data..** dat
31525 61 42 75 66 66 65 72 41 70 70 65 6e 64 20 2d 20  aBufferAppend - 
31526 61 70 70 65 6e 64 20 64 61 74 61 2e 0a 2a 2a 20  append data..** 
31527 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
31528 32 20 2d 20 61 70 70 65 6e 64 20 74 77 6f 20 70  2 - append two p
31529 69 65 63 65 73 20 6f 66 20 64 61 74 61 20 61 74  ieces of data at
3152a 20 6f 6e 63 65 2e 0a 2a 2a 20 64 61 74 61 42 75   once..** dataBu
3152b 66 66 65 72 52 65 70 6c 61 63 65 20 2d 20 72 65  fferReplace - re
3152c 70 6c 61 63 65 20 62 75 66 66 65 72 27 73 20 64  place buffer's d
3152d 61 74 61 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ata..*/.typedef 
3152e 73 74 72 75 63 74 20 44 61 74 61 42 75 66 66 65  struct DataBuffe
3152f 72 20 7b 0a 20 20 63 68 61 72 20 2a 70 44 61 74  r {.  char *pDat
31530 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  a;          /* P
31531 6f 69 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63  ointer to malloc
31532 27 65 64 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20  'ed buffer. */. 
31533 20 69 6e 74 20 6e 43 61 70 61 63 69 74 79 3b 20   int nCapacity; 
31534 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
31535 66 20 70 44 61 74 61 20 62 75 66 66 65 72 2e 20  f pData buffer. 
31536 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20  */.  int nData; 
31537 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
31538 64 20 6f 66 20 64 61 74 61 20 6c 6f 61 64 65 64  d of data loaded
31539 20 69 6e 74 6f 20 70 44 61 74 61 2e 20 2a 2f 0a   into pData. */.
3153a 7d 20 44 61 74 61 42 75 66 66 65 72 3b 0a 0a 73  } DataBuffer;..s
3153b 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
3153c 75 66 66 65 72 49 6e 69 74 28 44 61 74 61 42 75  ufferInit(DataBu
3153d 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69  ffer *pBuffer, i
3153e 6e 74 20 6e 43 61 70 61 63 69 74 79 29 7b 0a 20  nt nCapacity){. 
3153f 20 61 73 73 65 72 74 28 20 6e 43 61 70 61 63 69   assert( nCapaci
31540 74 79 3e 3d 30 20 29 3b 0a 20 20 70 42 75 66 66  ty>=0 );.  pBuff
31541 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20  er->nData = 0;. 
31542 20 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63   pBuffer->nCapac
31543 69 74 79 20 3d 20 6e 43 61 70 61 63 69 74 79 3b  ity = nCapacity;
31544 0a 20 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74  .  pBuffer->pDat
31545 61 20 3d 20 6e 43 61 70 61 63 69 74 79 3d 3d 30  a = nCapacity==0
31546 20 3f 20 4e 55 4c 4c 20 3a 20 73 71 6c 69 74 65   ? NULL : sqlite
31547 33 5f 6d 61 6c 6c 6f 63 28 6e 43 61 70 61 63 69  3_malloc(nCapaci
31548 74 79 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  ty);.}.static vo
31549 69 64 20 64 61 74 61 42 75 66 66 65 72 52 65 73  id dataBufferRes
3154a 65 74 28 44 61 74 61 42 75 66 66 65 72 20 2a 70  et(DataBuffer *p
3154b 42 75 66 66 65 72 29 7b 0a 20 20 70 42 75 66 66  Buffer){.  pBuff
3154c 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 7d  er->nData = 0;.}
3154d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
3154e 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 44  aBufferDestroy(D
3154f 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66  ataBuffer *pBuff
31550 65 72 29 7b 0a 20 20 69 66 28 20 70 42 75 66 66  er){.  if( pBuff
31551 65 72 2d 3e 70 44 61 74 61 21 3d 4e 55 4c 4c 20  er->pData!=NULL 
31552 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70  ) sqlite3_free(p
31553 42 75 66 66 65 72 2d 3e 70 44 61 74 61 29 3b 0a  Buffer->pData);.
31554 20 20 53 43 52 41 4d 42 4c 45 28 70 42 75 66 66    SCRAMBLE(pBuff
31555 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  er);.}.static vo
31556 69 64 20 64 61 74 61 42 75 66 66 65 72 53 77 61  id dataBufferSwa
31557 70 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42  p(DataBuffer *pB
31558 75 66 66 65 72 31 2c 20 44 61 74 61 42 75 66 66  uffer1, DataBuff
31559 65 72 20 2a 70 42 75 66 66 65 72 32 29 7b 0a 20  er *pBuffer2){. 
3155a 20 44 61 74 61 42 75 66 66 65 72 20 74 6d 70 20   DataBuffer tmp 
3155b 3d 20 2a 70 42 75 66 66 65 72 31 3b 0a 20 20 2a  = *pBuffer1;.  *
3155c 70 42 75 66 66 65 72 31 20 3d 20 2a 70 42 75 66  pBuffer1 = *pBuf
3155d 66 65 72 32 3b 0a 20 20 2a 70 42 75 66 66 65 72  fer2;.  *pBuffer
3155e 32 20 3d 20 74 6d 70 3b 0a 7d 0a 73 74 61 74 69  2 = tmp;.}.stati
3155f 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
31560 72 45 78 70 61 6e 64 28 44 61 74 61 42 75 66 66  rExpand(DataBuff
31561 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74  er *pBuffer, int
31562 20 6e 41 64 64 43 61 70 61 63 69 74 79 29 7b 0a   nAddCapacity){.
31563 20 20 61 73 73 65 72 74 28 20 6e 41 64 64 43 61    assert( nAddCa
31564 70 61 63 69 74 79 3e 30 20 29 3b 0a 20 20 2f 2a  pacity>0 );.  /*
31565 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
31566 73 69 64 65 72 20 65 78 70 61 6e 64 69 6e 67 20  sider expanding 
31567 6d 6f 72 65 20 61 67 67 72 65 73 73 69 76 65 6c  more aggressivel
31568 79 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68  y.  Note that th
31569 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e  e.  ** underlyin
3156a 67 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65  g malloc impleme
3156b 6e 74 61 74 69 6f 6e 20 6d 61 79 20 74 61 6b 65  ntation may take
3156c 20 63 61 72 65 20 6f 66 20 73 75 63 68 20 74 68   care of such th
3156d 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 75 73  ings for.  ** us
3156e 20 61 6c 72 65 61 64 79 2e 0a 20 20 2a 2f 0a 20   already..  */. 
3156f 20 69 66 28 20 70 42 75 66 66 65 72 2d 3e 6e 44   if( pBuffer->nD
31570 61 74 61 2b 6e 41 64 64 43 61 70 61 63 69 74 79  ata+nAddCapacity
31571 3e 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63  >pBuffer->nCapac
31572 69 74 79 20 29 7b 0a 20 20 20 20 70 42 75 66 66  ity ){.    pBuff
31573 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20 3d 20  er->nCapacity = 
31574 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e  pBuffer->nData+n
31575 41 64 64 43 61 70 61 63 69 74 79 3b 0a 20 20 20  AddCapacity;.   
31576 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20   pBuffer->pData 
31577 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
31578 63 28 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61  c(pBuffer->pData
31579 2c 20 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61  , pBuffer->nCapa
3157a 63 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  city);.  }.}.sta
3157b 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
3157c 66 65 72 41 70 70 65 6e 64 28 44 61 74 61 42 75  ferAppend(DataBu
3157d 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20  ffer *pBuffer,. 
3157e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3157f 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
31580 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c  t char *pSource,
31581 20 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20   int nSource){. 
31582 20 61 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65   assert( nSource
31583 3e 30 20 26 26 20 70 53 6f 75 72 63 65 21 3d 4e  >0 && pSource!=N
31584 55 4c 4c 20 29 3b 0a 20 20 64 61 74 61 42 75 66  ULL );.  dataBuf
31585 66 65 72 45 78 70 61 6e 64 28 70 42 75 66 66 65  ferExpand(pBuffe
31586 72 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 20 20 6d  r, nSource);.  m
31587 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70  emcpy(pBuffer->p
31588 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44  Data+pBuffer->nD
31589 61 74 61 2c 20 70 53 6f 75 72 63 65 2c 20 6e 53  ata, pSource, nS
3158a 6f 75 72 63 65 29 3b 0a 20 20 70 42 75 66 66 65  ource);.  pBuffe
3158b 72 2d 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75  r->nData += nSou
3158c 72 63 65 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  rce;.}.static vo
3158d 69 64 20 64 61 74 61 42 75 66 66 65 72 41 70 70  id dataBufferApp
3158e 65 6e 64 32 28 44 61 74 61 42 75 66 66 65 72 20  end2(DataBuffer 
3158f 2a 70 42 75 66 66 65 72 2c 0a 20 20 20 20 20 20  *pBuffer,.      
31590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31591 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
31592 61 72 20 2a 70 53 6f 75 72 63 65 31 2c 20 69 6e  ar *pSource1, in
31593 74 20 6e 53 6f 75 72 63 65 31 2c 0a 20 20 20 20  t nSource1,.    
31594 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31595 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
31596 63 68 61 72 20 2a 70 53 6f 75 72 63 65 32 2c 20  char *pSource2, 
31597 69 6e 74 20 6e 53 6f 75 72 63 65 32 29 7b 0a 20  int nSource2){. 
31598 20 61 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65   assert( nSource
31599 31 3e 30 20 26 26 20 70 53 6f 75 72 63 65 31 21  1>0 && pSource1!
3159a 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72  =NULL );.  asser
3159b 74 28 20 6e 53 6f 75 72 63 65 32 3e 30 20 26 26  t( nSource2>0 &&
3159c 20 70 53 6f 75 72 63 65 32 21 3d 4e 55 4c 4c 20   pSource2!=NULL 
3159d 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 45  );.  dataBufferE
3159e 78 70 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e  xpand(pBuffer, n
3159f 53 6f 75 72 63 65 31 2b 6e 53 6f 75 72 63 65 32  Source1+nSource2
315a0 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 75 66  );.  memcpy(pBuf
315a1 66 65 72 2d 3e 70 44 61 74 61 2b 70 42 75 66 66  fer->pData+pBuff
315a2 65 72 2d 3e 6e 44 61 74 61 2c 20 70 53 6f 75 72  er->nData, pSour
315a3 63 65 31 2c 20 6e 53 6f 75 72 63 65 31 29 3b 0a  ce1, nSource1);.
315a4 20 20 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72    memcpy(pBuffer
315a5 2d 3e 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d  ->pData+pBuffer-
315a6 3e 6e 44 61 74 61 2b 6e 53 6f 75 72 63 65 31 2c  >nData+nSource1,
315a7 20 70 53 6f 75 72 63 65 32 2c 20 6e 53 6f 75 72   pSource2, nSour
315a8 63 65 32 29 3b 0a 20 20 70 42 75 66 66 65 72 2d  ce2);.  pBuffer-
315a9 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63  >nData += nSourc
315aa 65 31 2b 6e 53 6f 75 72 63 65 32 3b 0a 7d 0a 73  e1+nSource2;.}.s
315ab 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
315ac 75 66 66 65 72 52 65 70 6c 61 63 65 28 44 61 74  ufferReplace(Dat
315ad 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
315ae 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
315af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
315b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75  const char *pSou
315b1 72 63 65 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65  rce, int nSource
315b2 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72 52  ){.  dataBufferR
315b3 65 73 65 74 28 70 42 75 66 66 65 72 29 3b 0a 20  eset(pBuffer);. 
315b4 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
315b5 64 28 70 42 75 66 66 65 72 2c 20 70 53 6f 75 72  d(pBuffer, pSour
315b6 63 65 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 7d 0a  ce, nSource);.}.
315b7 0a 2f 2a 20 53 74 72 69 6e 67 42 75 66 66 65 72  ./* StringBuffer
315b8 20 69 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69   is a null-termi
315b9 6e 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  nated version of
315ba 20 44 61 74 61 42 75 66 66 65 72 2e 20 2a 2f 0a   DataBuffer. */.
315bb 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
315bc 74 72 69 6e 67 42 75 66 66 65 72 20 7b 0a 20 20  tringBuffer {.  
315bd 44 61 74 61 42 75 66 66 65 72 20 62 3b 20 20 20  DataBuffer b;   
315be 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 6c           /* Incl
315bf 75 64 65 73 20 6e 75 6c 6c 20 74 65 72 6d 69 6e  udes null termin
315c0 61 74 6f 72 2e 20 2a 2f 0a 7d 20 53 74 72 69 6e  ator. */.} Strin
315c1 67 42 75 66 66 65 72 3b 0a 0a 73 74 61 74 69 63  gBuffer;..static
315c2 20 76 6f 69 64 20 69 6e 69 74 53 74 72 69 6e 67   void initString
315c3 42 75 66 66 65 72 28 53 74 72 69 6e 67 42 75 66  Buffer(StringBuf
315c4 66 65 72 20 2a 73 62 29 7b 0a 20 20 64 61 74 61  fer *sb){.  data
315c5 42 75 66 66 65 72 49 6e 69 74 28 26 73 62 2d 3e  BufferInit(&sb->
315c6 62 2c 20 31 30 30 29 3b 0a 20 20 64 61 74 61 42  b, 100);.  dataB
315c7 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 73 62  ufferReplace(&sb
315c8 2d 3e 62 2c 20 22 22 2c 20 31 29 3b 0a 7d 0a 73  ->b, "", 1);.}.s
315c9 74 61 74 69 63 20 69 6e 74 20 73 74 72 69 6e 67  tatic int string
315ca 42 75 66 66 65 72 4c 65 6e 67 74 68 28 53 74 72  BufferLength(Str
315cb 69 6e 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a  ingBuffer *sb){.
315cc 20 20 72 65 74 75 72 6e 20 73 62 2d 3e 62 2e 6e    return sb->b.n
315cd 44 61 74 61 2d 31 3b 0a 7d 0a 73 74 61 74 69 63  Data-1;.}.static
315ce 20 63 68 61 72 20 2a 73 74 72 69 6e 67 42 75 66   char *stringBuf
315cf 66 65 72 44 61 74 61 28 53 74 72 69 6e 67 42 75  ferData(StringBu
315d0 66 66 65 72 20 2a 73 62 29 7b 0a 20 20 72 65 74  ffer *sb){.  ret
315d1 75 72 6e 20 73 62 2d 3e 62 2e 70 44 61 74 61 3b  urn sb->b.pData;
315d2 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  .}.static void s
315d3 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74 72  tringBufferDestr
315d4 6f 79 28 53 74 72 69 6e 67 42 75 66 66 65 72 20  oy(StringBuffer 
315d5 2a 73 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66  *sb){.  dataBuff
315d6 65 72 44 65 73 74 72 6f 79 28 26 73 62 2d 3e 62  erDestroy(&sb->b
315d7 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
315d8 64 20 6e 61 70 70 65 6e 64 28 53 74 72 69 6e 67  d nappend(String
315d9 42 75 66 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73  Buffer *sb, cons
315da 74 20 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 69  t char *zFrom, i
315db 6e 74 20 6e 46 72 6f 6d 29 7b 0a 20 20 61 73 73  nt nFrom){.  ass
315dc 65 72 74 28 20 73 62 2d 3e 62 2e 6e 44 61 74 61  ert( sb->b.nData
315dd 3e 30 20 29 3b 0a 20 20 69 66 28 20 6e 46 72 6f  >0 );.  if( nFro
315de 6d 3e 30 20 29 7b 0a 20 20 20 20 73 62 2d 3e 62  m>0 ){.    sb->b
315df 2e 6e 44 61 74 61 2d 2d 3b 0a 20 20 20 20 64 61  .nData--;.    da
315e0 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
315e1 26 73 62 2d 3e 62 2c 20 7a 46 72 6f 6d 2c 20 6e  &sb->b, zFrom, n
315e2 46 72 6f 6d 2c 20 22 22 2c 20 31 29 3b 0a 20 20  From, "", 1);.  
315e3 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
315e4 61 70 70 65 6e 64 28 53 74 72 69 6e 67 42 75 66  append(StringBuf
315e5 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63  fer *sb, const c
315e6 68 61 72 20 2a 7a 46 72 6f 6d 29 7b 0a 20 20 6e  har *zFrom){.  n
315e7 61 70 70 65 6e 64 28 73 62 2c 20 7a 46 72 6f 6d  append(sb, zFrom
315e8 2c 20 73 74 72 6c 65 6e 28 7a 46 72 6f 6d 29 29  , strlen(zFrom))
315e9 3b 0a 7d 0a 0a 2f 2a 20 41 70 70 65 6e 64 20 61  ;.}../* Append a
315ea 20 6c 69 73 74 20 6f 66 20 73 74 72 69 6e 67 73   list of strings
315eb 20 73 65 70 61 72 61 74 65 64 20 62 79 20 63 6f   separated by co
315ec 6d 6d 61 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20  mmas. */.static 
315ed 76 6f 69 64 20 61 70 70 65 6e 64 4c 69 73 74 28  void appendList(
315ee 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62  StringBuffer *sb
315ef 2c 20 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20 63  , int nString, c
315f0 68 61 72 20 2a 2a 61 7a 53 74 72 69 6e 67 29 7b  har **azString){
315f1 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
315f2 69 3d 30 3b 20 69 3c 6e 53 74 72 69 6e 67 3b 20  i=0; i<nString; 
315f3 2b 2b 69 29 7b 0a 20 20 20 20 69 66 28 20 69 3e  ++i){.    if( i>
315f4 30 20 29 20 61 70 70 65 6e 64 28 73 62 2c 20 22  0 ) append(sb, "
315f5 2c 20 22 29 3b 0a 20 20 20 20 61 70 70 65 6e 64  , ");.    append
315f6 28 73 62 2c 20 61 7a 53 74 72 69 6e 67 5b 69 5d  (sb, azString[i]
315f7 29 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63  );.  }.}..static
315f8 20 69 6e 74 20 65 6e 64 73 49 6e 57 68 69 74 65   int endsInWhite
315f9 53 70 61 63 65 28 53 74 72 69 6e 67 42 75 66 66  Space(StringBuff
315fa 65 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  er *p){.  return
315fb 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e   stringBufferLen
315fc 67 74 68 28 70 29 3e 30 20 26 26 0a 20 20 20 20  gth(p)>0 &&.    
315fd 73 61 66 65 5f 69 73 73 70 61 63 65 28 73 74 72  safe_isspace(str
315fe 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 70 29  ingBufferData(p)
315ff 5b 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e  [stringBufferLen
31600 67 74 68 28 70 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f  gth(p)-1]);.}../
31601 2a 20 49 66 20 74 68 65 20 53 74 72 69 6e 67 42  * If the StringB
31602 75 66 66 65 72 20 65 6e 64 73 20 69 6e 20 73 6f  uffer ends in so
31603 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68  mething other th
31604 61 6e 20 77 68 69 74 65 20 73 70 61 63 65 2c 20  an white space, 
31605 61 64 64 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20  add a.** single 
31606 73 70 61 63 65 20 63 68 61 72 61 63 74 65 72 20  space character 
31607 74 6f 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73  to the end..*/.s
31608 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e  tatic void appen
31609 64 57 68 69 74 65 53 70 61 63 65 28 53 74 72 69  dWhiteSpace(Stri
3160a 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20  ngBuffer *p){.  
3160b 69 66 28 20 73 74 72 69 6e 67 42 75 66 66 65 72  if( stringBuffer
3160c 4c 65 6e 67 74 68 28 70 29 3d 3d 30 20 29 20 72  Length(p)==0 ) r
3160d 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 65 6e  eturn;.  if( !en
3160e 64 73 49 6e 57 68 69 74 65 53 70 61 63 65 28 70  dsInWhiteSpace(p
3160f 29 20 29 20 61 70 70 65 6e 64 28 70 2c 20 22 20  ) ) append(p, " 
31610 22 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65  ");.}../* Remove
31611 20 77 68 69 74 65 20 73 70 61 63 65 20 66 72 6f   white space fro
31612 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  m the end of the
31613 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 2f   StringBuffer */
31614 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 69  .static void tri
31615 6d 57 68 69 74 65 53 70 61 63 65 28 53 74 72 69  mWhiteSpace(Stri
31616 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20  ngBuffer *p){.  
31617 77 68 69 6c 65 28 20 65 6e 64 73 49 6e 57 68 69  while( endsInWhi
31618 74 65 53 70 61 63 65 28 70 29 20 29 7b 0a 20 20  teSpace(p) ){.  
31619 20 20 70 2d 3e 62 2e 70 44 61 74 61 5b 2d 2d 70    p->b.pData[--p
3161a 2d 3e 62 2e 6e 44 61 74 61 2d 31 5d 20 3d 20 27  ->b.nData-1] = '
3161b 5c 30 27 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a  \0';.  }.}../***
3161c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3161d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3161e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3161f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31620 2f 0a 2f 2a 20 44 4c 52 65 61 64 65 72 20 69 73  /./* DLReader is
31621 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 6f   used to read do
31622 63 75 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20  cument elements 
31623 66 72 6f 6d 20 61 20 64 6f 63 6c 69 73 74 2e 20  from a doclist. 
31624 20 54 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   The.** current 
31625 64 6f 63 69 64 20 69 73 20 63 61 63 68 65 64 2c  docid is cached,
31626 20 73 6f 20 64 6c 72 44 6f 63 69 64 28 29 20 69   so dlrDocid() i
31627 73 20 66 61 73 74 2e 20 20 44 4c 52 65 61 64 65  s fast.  DLReade
31628 72 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6f 77  r does not.** ow
31629 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 62 75  n the doclist bu
3162a 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 64 6c 72 41  ffer..**.** dlrA
3162b 74 45 6e 64 20 2d 20 74 72 75 65 20 69 66 20 74  tEnd - true if t
3162c 68 65 72 65 27 73 20 6e 6f 20 6d 6f 72 65 20 64  here's no more d
3162d 61 74 61 20 74 6f 20 72 65 61 64 2e 0a 2a 2a 20  ata to read..** 
3162e 64 6c 72 44 6f 63 69 64 20 2d 20 64 6f 63 69 64  dlrDocid - docid
3162f 20 6f 66 20 63 75 72 72 65 6e 74 20 64 6f 63 75   of current docu
31630 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44  ment..** dlrDocD
31631 61 74 61 20 2d 20 64 6f 63 6c 69 73 74 20 64 61  ata - doclist da
31632 74 61 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64  ta for current d
31633 6f 63 75 6d 65 6e 74 20 28 69 6e 63 6c 75 64 69  ocument (includi
31634 6e 67 20 64 6f 63 69 64 29 2e 0a 2a 2a 20 64 6c  ng docid)..** dl
31635 72 44 6f 63 44 61 74 61 42 79 74 65 73 20 2d 20  rDocDataBytes - 
31636 6c 65 6e 67 74 68 20 6f 66 20 73 61 6d 65 2e 0a  length of same..
31637 2a 2a 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74  ** dlrAllDataByt
31638 65 73 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 61  es - length of a
31639 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 64 61 74  ll remaining dat
3163a 61 2e 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61  a..** dlrPosData
3163b 20 2d 20 70 6f 73 69 74 69 6f 6e 20 64 61 74 61   - position data
3163c 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63   for current doc
3163d 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 50 6f 73  ument..** dlrPos
3163e 44 61 74 61 4c 65 6e 20 2d 20 6c 65 6e 67 74 68  DataLen - length
3163f 20 6f 66 20 70 6f 73 20 64 61 74 61 20 66 6f 72   of pos data for
31640 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e   current documen
31641 74 20 28 69 6e 63 6c 20 50 4f 53 5f 45 4e 44 29  t (incl POS_END)
31642 2e 0a 2a 2a 20 64 6c 72 53 74 65 70 20 2d 20 73  ..** dlrStep - s
31643 74 65 70 20 74 6f 20 63 75 72 72 65 6e 74 20 64  tep to current d
31644 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 49  ocument..** dlrI
31645 6e 69 74 20 2d 20 69 6e 69 74 69 61 6c 20 66 6f  nit - initial fo
31646 72 20 64 6f 63 6c 69 73 74 20 6f 66 20 67 69 76  r doclist of giv
31647 65 6e 20 74 79 70 65 20 61 67 61 69 6e 73 74 20  en type against 
31648 67 69 76 65 6e 20 64 61 74 61 2e 0a 2a 2a 20 64  given data..** d
31649 6c 72 44 65 73 74 72 6f 79 20 2d 20 63 6c 65 61  lrDestroy - clea
3164a 6e 20 75 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 65  n up..**.** Expe
3164b 63 74 65 64 20 75 73 61 67 65 20 69 73 20 73 6f  cted usage is so
3164c 6d 65 74 68 69 6e 67 20 6c 69 6b 65 3a 0a 2a 2a  mething like:.**
3164d 0a 2a 2a 20 20 20 44 4c 52 65 61 64 65 72 20 72  .**   DLReader r
3164e 65 61 64 65 72 3b 0a 2a 2a 20 20 20 64 6c 72 49  eader;.**   dlrI
3164f 6e 69 74 28 26 72 65 61 64 65 72 2c 20 70 44 61  nit(&reader, pDa
31650 74 61 2c 20 6e 44 61 74 61 29 3b 0a 2a 2a 20 20  ta, nData);.**  
31651 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e   while( !dlrAtEn
31652 64 28 26 72 65 61 64 65 72 29 20 29 7b 0a 2a 2a  d(&reader) ){.**
31653 20 20 20 20 20 2f 2f 20 63 61 6c 6c 73 20 74 6f       // calls to
31654 20 64 6c 72 44 6f 63 69 64 28 29 20 61 6e 64 20   dlrDocid() and 
31655 6b 69 6e 2e 0a 2a 2a 20 20 20 20 20 64 6c 72 53  kin..**     dlrS
31656 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a 2a 2a  tep(&reader);.**
31657 20 20 20 7d 0a 2a 2a 20 20 20 64 6c 72 44 65 73     }.**   dlrDes
31658 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 2a  troy(&reader);.*
31659 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
3165a 20 44 4c 52 65 61 64 65 72 20 7b 0a 20 20 44 6f   DLReader {.  Do
3165b 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 3b  cListType iType;
3165c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
3165d 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74  Data;.  int nDat
3165e 61 3b 0a 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  a;..  sqlite_int
3165f 36 34 20 69 44 6f 63 69 64 3b 0a 20 20 69 6e 74  64 iDocid;.  int
31660 20 6e 45 6c 65 6d 65 6e 74 3b 0a 7d 20 44 4c 52   nElement;.} DLR
31661 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69  eader;..static i
31662 6e 74 20 64 6c 72 41 74 45 6e 64 28 44 4c 52 65  nt dlrAtEnd(DLRe
31663 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
31664 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
31665 72 2d 3e 6e 44 61 74 61 3e 3d 30 20 29 3b 0a 20  r->nData>=0 );. 
31666 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
31667 3e 6e 44 61 74 61 3d 3d 30 3b 0a 7d 0a 73 74 61  >nData==0;.}.sta
31668 74 69 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  tic sqlite_int64
31669 20 64 6c 72 44 6f 63 69 64 28 44 4c 52 65 61 64   dlrDocid(DLRead
3166a 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
3166b 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e  assert( !dlrAtEn
3166c 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
3166d 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
3166e 69 44 6f 63 69 64 3b 0a 7d 0a 73 74 61 74 69 63  iDocid;.}.static
3166f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72   const char *dlr
31670 44 6f 63 44 61 74 61 28 44 4c 52 65 61 64 65 72  DocData(DLReader
31671 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
31672 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28  sert( !dlrAtEnd(
31673 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
31674 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44  turn pReader->pD
31675 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  ata;.}.static in
31676 74 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65  t dlrDocDataByte
31677 73 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61  s(DLReader *pRea
31678 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
31679 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !dlrAtEnd(pReade
3167a 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
3167b 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74  Reader->nElement
3167c 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64  ;.}.static int d
3167d 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73 28 44  lrAllDataBytes(D
3167e 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
3167f 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c  ){.  assert( !dl
31680 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
31681 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
31682 64 65 72 2d 3e 6e 44 61 74 61 3b 0a 7d 0a 2f 2a  der->nData;.}./*
31683 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
31684 73 69 64 65 72 20 61 64 64 69 6e 67 20 61 20 66  sider adding a f
31685 69 65 6c 64 20 74 6f 20 74 72 61 63 6b 20 69 44  ield to track iD
31686 6f 63 69 64 20 76 61 72 69 6e 74 20 6c 65 6e 67  ocid varint leng
31687 74 68 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 74 68  th.** to make th
31688 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e  ese two function
31689 73 20 66 61 73 74 65 72 2e 20 20 54 68 69 73 20  s faster.  This 
3168a 6d 69 67 68 74 20 6d 61 74 74 65 72 20 28 61 20  might matter (a 
3168b 74 69 6e 79 20 62 69 74 29 0a 2a 2a 20 66 6f 72  tiny bit).** for
3168c 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 73 74 61   queries..*/.sta
3168d 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
3168e 64 6c 72 50 6f 73 44 61 74 61 28 44 4c 52 65 61  dlrPosData(DLRea
3168f 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
31690 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
31691 75 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  ummy;.  int n = 
31692 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 52  fts3GetVarint(pR
31693 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69  eader->pData, &i
31694 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74  Dummy);.  assert
31695 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !dlrAtEnd(pRea
31696 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
31697 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b   pReader->pData+
31698 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  n;.}.static int 
31699 64 6c 72 50 6f 73 44 61 74 61 4c 65 6e 28 44 4c  dlrPosDataLen(DL
3169a 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
3169b 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  {.  sqlite_int64
3169c 20 69 44 75 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e   iDummy;.  int n
3169d 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
3169e 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
3169f 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73   &iDummy);.  ass
316a0 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70  ert( !dlrAtEnd(p
316a1 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74  Reader) );.  ret
316a2 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c  urn pReader->nEl
316a3 65 6d 65 6e 74 2d 6e 3b 0a 7d 0a 73 74 61 74 69  ement-n;.}.stati
316a4 63 20 76 6f 69 64 20 64 6c 72 53 74 65 70 28 44  c void dlrStep(D
316a5 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
316a6 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c  ){.  assert( !dl
316a7 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
316a8 29 3b 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 61  );..  /* Skip pa
316a9 73 74 20 63 75 72 72 65 6e 74 20 64 6f 63 6c 69  st current docli
316aa 73 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20  st element. */. 
316ab 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72   assert( pReader
316ac 2d 3e 6e 45 6c 65 6d 65 6e 74 3c 3d 70 52 65 61  ->nElement<=pRea
316ad 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
316ae 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b  pReader->pData +
316af 3d 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d  = pReader->nElem
316b0 65 6e 74 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  ent;.  pReader->
316b1 6e 44 61 74 61 20 2d 3d 20 70 52 65 61 64 65 72  nData -= pReader
316b2 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a 0a 20 20 2f  ->nElement;..  /
316b3 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6d 6f  * If there is mo
316b4 72 65 20 64 61 74 61 2c 20 72 65 61 64 20 74 68  re data, read th
316b5 65 20 6e 65 78 74 20 64 6f 63 6c 69 73 74 20 65  e next doclist e
316b6 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 66 28  lement. */.  if(
316b7 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 21   pReader->nData!
316b8 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
316b9 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 44 65 6c  _int64 iDocidDel
316ba 74 61 3b 0a 20 20 20 20 69 6e 74 20 69 44 75 6d  ta;.    int iDum
316bb 6d 79 2c 20 6e 20 3d 20 66 74 73 33 47 65 74 56  my, n = fts3GetV
316bc 61 72 69 6e 74 28 70 52 65 61 64 65 72 2d 3e 70  arint(pReader->p
316bd 44 61 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c  Data, &iDocidDel
316be 74 61 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72  ta);.    pReader
316bf 2d 3e 69 44 6f 63 69 64 20 2b 3d 20 69 44 6f 63  ->iDocid += iDoc
316c0 69 64 44 65 6c 74 61 3b 0a 20 20 20 20 69 66 28  idDelta;.    if(
316c1 20 70 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3e   pReader->iType>
316c2 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b  =DL_POSITIONS ){
316c3 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
316c4 3c 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  <pReader->nData 
316c5 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
316c6 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 20 2b  1 ){.        n +
316c7 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
316c8 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
316c9 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  +n, &iDummy);.  
316ca 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c        assert( n<
316cb 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
316cc 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
316cd 44 75 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29  Dummy==POS_END )
316ce 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
316cf 69 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f  if( iDummy==POS_
316d0 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
316d1 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
316d2 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
316d3 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d  ->pData+n, &iDum
316d4 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  my);.          a
316d5 73 73 65 72 74 28 20 6e 3c 70 52 65 61 64 65 72  ssert( n<pReader
316d6 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20  ->nData );.     
316d7 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 65     }else if( pRe
316d8 61 64 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f  ader->iType==DL_
316d9 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
316da 53 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  S ){.          n
316db 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
316dc 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
316dd 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  ta+n, &iDummy);.
316de 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66            n += f
316df 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
316e0 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
316e1 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20   &iDummy);.     
316e2 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70       assert( n<p
316e3 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
316e4 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
316e5 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65   }.    }.    pRe
316e6 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d  ader->nElement =
316e7 20 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   n;.    assert( 
316e8 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e  pReader->nElemen
316e9 74 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  t<=pReader->nDat
316ea 61 20 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  a );.  }.}.stati
316eb 63 20 76 6f 69 64 20 64 6c 72 49 6e 69 74 28 44  c void dlrInit(D
316ec 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
316ed 2c 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54  , DocListType iT
316ee 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ype,.           
316ef 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
316f0 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
316f1 6e 44 61 74 61 29 7b 0a 20 20 61 73 73 65 72 74  nData){.  assert
316f2 28 20 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26  ( pData!=NULL &&
316f3 20 6e 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 70   nData!=0 );.  p
316f4 52 65 61 64 65 72 2d 3e 69 54 79 70 65 20 3d 20  Reader->iType = 
316f5 69 54 79 70 65 3b 0a 20 20 70 52 65 61 64 65 72  iType;.  pReader
316f6 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 3b  ->pData = pData;
316f7 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  .  pReader->nDat
316f8 61 20 3d 20 6e 44 61 74 61 3b 0a 20 20 70 52 65  a = nData;.  pRe
316f9 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d  ader->nElement =
316fa 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69   0;.  pReader->i
316fb 44 6f 63 69 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a  Docid = 0;..  /*
316fc 20 4c 6f 61 64 20 74 68 65 20 66 69 72 73 74 20   Load the first 
316fd 65 6c 65 6d 65 6e 74 27 73 20 64 61 74 61 2e 20  element's data. 
316fe 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   There must be a
316ff 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 2e 20   first element. 
31700 2a 2f 0a 20 20 64 6c 72 53 74 65 70 28 70 52 65  */.  dlrStep(pRe
31701 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
31702 76 6f 69 64 20 64 6c 72 44 65 73 74 72 6f 79 28  void dlrDestroy(
31703 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  DLReader *pReade
31704 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70  r){.  SCRAMBLE(p
31705 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 23 69 66 6e  Reader);.}..#ifn
31706 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65  def NDEBUG./* Ve
31707 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64 6f  rify that the do
31708 63 6c 69 73 74 20 63 61 6e 20 62 65 20 76 61 6c  clist can be val
31709 69 64 6c 79 20 64 65 63 6f 64 65 64 2e 20 20 41  idly decoded.  A
3170a 6c 73 6f 20 72 65 74 75 72 6e 73 20 74 68 65 0a  lso returns the.
3170b 2a 2a 20 6c 61 73 74 20 64 6f 63 69 64 20 66 6f  ** last docid fo
3170c 75 6e 64 20 62 65 63 61 75 73 65 20 69 74 20 69  und because it i
3170d 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 69 6e 20  s convenient in 
3170e 6f 74 68 65 72 20 61 73 73 65 72 74 69 6f 6e 73  other assertions
3170f 20 66 6f 72 0a 2a 2a 20 44 4c 57 72 69 74 65 72   for.** DLWriter
31710 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
31711 20 64 6f 63 4c 69 73 74 56 61 6c 69 64 61 74 65   docListValidate
31712 28 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79  (DocListType iTy
31713 70 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  pe, const char *
31714 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
31715 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
31716 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
31717 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 4c 61 73  lite_int64 *pLas
31718 74 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74  tDocid){.  sqlit
31719 65 5f 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63  e_int64 iPrevDoc
3171a 69 64 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  id = 0;.  assert
3171b 28 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61  ( nData>0 );.  a
3171c 73 73 65 72 74 28 20 70 44 61 74 61 21 3d 30 20  ssert( pData!=0 
3171d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  );.  assert( pDa
3171e 74 61 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29  ta+nData>pData )
3171f 3b 0a 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61  ;.  while( nData
31720 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  !=0 ){.    sqlit
31721 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 44 65  e_int64 iDocidDe
31722 6c 74 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  lta;.    int n =
31723 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70   fts3GetVarint(p
31724 44 61 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c  Data, &iDocidDel
31725 74 61 29 3b 0a 20 20 20 20 69 50 72 65 76 44 6f  ta);.    iPrevDo
31726 63 69 64 20 2b 3d 20 69 44 6f 63 69 64 44 65 6c  cid += iDocidDel
31727 74 61 3b 0a 20 20 20 20 69 66 28 20 69 54 79 70  ta;.    if( iTyp
31728 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20  e>DL_DOCIDS ){. 
31729 20 20 20 20 20 69 6e 74 20 69 44 75 6d 6d 79 3b       int iDummy;
3172a 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 31 20  .      while( 1 
3172b 29 7b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  ){.        n += 
3172c 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
3172d 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79  pData+n, &iDummy
3172e 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
3172f 44 75 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29  Dummy==POS_END )
31730 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
31731 69 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f  if( iDummy==POS_
31732 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
31733 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
31734 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2b 6e  Varint32(pData+n
31735 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
31736 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 54      }else if( iT
31737 79 70 65 3e 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  ype>DL_POSITIONS
31738 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20   ){.          n 
31739 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
3173a 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75  32(pData+n, &iDu
3173b 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  mmy);.          
3173c 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69  n += fts3GetVari
3173d 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69  nt32(pData+n, &i
3173e 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20  Dummy);.        
3173f 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
31740 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20  ( n<=nData );.  
31741 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
31742 61 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61  assert( n<=nData
31743 20 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d   );.    pData +=
31744 20 6e 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d   n;.    nData -=
31745 20 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c   n;.  }.  if( pL
31746 61 73 74 44 6f 63 69 64 20 29 20 2a 70 4c 61 73  astDocid ) *pLas
31747 74 44 6f 63 69 64 20 3d 20 69 50 72 65 76 44 6f  tDocid = iPrevDo
31748 63 69 64 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 41  cid;.}.#define A
31749 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c  SSERT_VALID_DOCL
3174a 49 53 54 28 69 2c 20 70 2c 20 6e 2c 20 6f 29 20  IST(i, p, n, o) 
3174b 64 6f 63 4c 69 73 74 56 61 6c 69 64 61 74 65 28  docListValidate(
3174c 69 2c 20 70 2c 20 6e 2c 20 6f 29 0a 23 65 6c 73  i, p, n, o).#els
3174d 65 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54  e.#define ASSERT
3174e 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 69  _VALID_DOCLIST(i
3174f 2c 20 70 2c 20 6e 2c 20 6f 29 20 61 73 73 65 72  , p, n, o) asser
31750 74 28 20 31 20 29 0a 23 65 6e 64 69 66 0a 0a 2f  t( 1 ).#endif../
31751 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31752 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31753 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31754 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31755 2a 2a 2a 2f 0a 2f 2a 20 44 4c 57 72 69 74 65 72  ***/./* DLWriter
31756 20 69 73 20 75 73 65 64 20 74 6f 20 77 72 69 74   is used to writ
31757 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74  e doclist data t
31758 6f 20 61 20 44 61 74 61 42 75 66 66 65 72 2e 20  o a DataBuffer. 
31759 20 44 4c 57 72 69 74 65 72 0a 2a 2a 20 61 6c 77   DLWriter.** alw
3175a 61 79 73 20 61 70 70 65 6e 64 73 20 74 6f 20 74  ays appends to t
3175b 68 65 20 62 75 66 66 65 72 20 61 6e 64 20 64 6f  he buffer and do
3175c 65 73 20 6e 6f 74 20 6f 77 6e 20 69 74 2e 0a 2a  es not own it..*
3175d 2a 0a 2a 2a 20 64 6c 77 49 6e 69 74 20 2d 20 69  *.** dlwInit - i
3175e 6e 69 74 69 61 6c 69 7a 65 20 74 6f 20 77 72 69  nitialize to wri
3175f 74 65 20 61 20 67 69 76 65 6e 20 74 79 70 65 20  te a given type 
31760 64 6f 63 6c 69 73 74 74 6f 20 61 20 62 75 66 66  doclistto a buff
31761 65 72 2e 0a 2a 2a 20 64 6c 77 44 65 73 74 72 6f  er..** dlwDestro
31762 79 20 2d 20 63 6c 65 61 72 20 74 68 65 20 77 72  y - clear the wr
31763 69 74 65 72 27 73 20 6d 65 6d 6f 72 79 2e 20 20  iter's memory.  
31764 44 6f 65 73 20 6e 6f 74 20 66 72 65 65 20 62 75  Does not free bu
31765 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 41 70 70 65  ffer..** dlwAppe
31766 6e 64 20 2d 20 61 70 70 65 6e 64 20 72 61 77 20  nd - append raw 
31767 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20  doclist data to 
31768 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 43 6f  buffer..** dlwCo
31769 70 79 20 2d 20 63 6f 70 79 20 6e 65 78 74 20 64  py - copy next d
3176a 6f 63 6c 69 73 74 20 66 72 6f 6d 20 72 65 61 64  oclist from read
3176b 65 72 20 74 6f 20 77 72 69 74 65 72 2e 0a 2a 2a  er to writer..**
3176c 20 64 6c 77 41 64 64 20 2d 20 63 6f 6e 73 74 72   dlwAdd - constr
3176d 75 63 74 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d  uct doclist elem
3176e 65 6e 74 20 61 6e 64 20 61 70 70 65 6e 64 20 74  ent and append t
3176f 6f 20 62 75 66 66 65 72 2e 0a 2a 2a 20 20 20 20  o buffer..**    
31770 4f 6e 6c 79 20 61 70 70 6c 79 20 64 6c 77 41 64  Only apply dlwAd
31771 64 28 29 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53  d() to DL_DOCIDS
31772 20 64 6f 63 6c 69 73 74 73 20 28 65 6c 73 65 20   doclists (else 
31773 75 73 65 20 50 4c 57 72 69 74 65 72 29 2e 0a 2a  use PLWriter)..*
31774 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
31775 20 44 4c 57 72 69 74 65 72 20 7b 0a 20 20 44 6f   DLWriter {.  Do
31776 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 3b  cListType iType;
31777 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 62  .  DataBuffer *b
31778 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
31779 20 69 50 72 65 76 44 6f 63 69 64 3b 0a 23 69 66   iPrevDocid;.#if
3177a 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 6e  ndef NDEBUG.  in
3177b 74 20 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  t has_iPrevDocid
3177c 3b 0a 23 65 6e 64 69 66 0a 7d 20 44 4c 57 72 69  ;.#endif.} DLWri
3177d 74 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69  ter;..static voi
3177e 64 20 64 6c 77 49 6e 69 74 28 44 4c 57 72 69 74  d dlwInit(DLWrit
3177f 65 72 20 2a 70 57 72 69 74 65 72 2c 20 44 6f 63  er *pWriter, Doc
31780 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 20  ListType iType, 
31781 44 61 74 61 42 75 66 66 65 72 20 2a 62 29 7b 0a  DataBuffer *b){.
31782 20 20 70 57 72 69 74 65 72 2d 3e 62 20 3d 20 62    pWriter->b = b
31783 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 54 79  ;.  pWriter->iTy
31784 70 65 20 3d 20 69 54 79 70 65 3b 0a 20 20 70 57  pe = iType;.  pW
31785 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
31786 64 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e  d = 0;.#ifndef N
31787 44 45 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d  DEBUG.  pWriter-
31788 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20  >has_iPrevDocid 
31789 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74  = 0;.#endif.}.st
3178a 61 74 69 63 20 76 6f 69 64 20 64 6c 77 44 65 73  atic void dlwDes
3178b 74 72 6f 79 28 44 4c 57 72 69 74 65 72 20 2a 70  troy(DLWriter *p
3178c 57 72 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d  Writer){.  SCRAM
3178d 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a  BLE(pWriter);.}.
3178e 2f 2a 20 69 46 69 72 73 74 44 6f 63 69 64 20 69  /* iFirstDocid i
3178f 73 20 74 68 65 20 66 69 72 73 74 20 64 6f 63 69  s the first doci
31790 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74  d in the doclist
31791 20 69 6e 20 70 44 61 74 61 2e 20 20 49 74 20 69   in pData.  It i
31792 73 0a 2a 2a 20 6e 65 65 64 65 64 20 62 65 63 61  s.** needed beca
31793 75 73 65 20 70 44 61 74 61 20 6d 61 79 20 70 6f  use pData may po
31794 69 6e 74 20 77 69 74 68 69 6e 20 61 20 6c 61 72  int within a lar
31795 67 65 72 20 64 6f 63 6c 69 73 74 2c 20 69 6e 20  ger doclist, in 
31796 77 68 69 63 68 0a 2a 2a 20 63 61 73 65 20 74 68  which.** case th
31797 65 20 66 69 72 73 74 20 69 74 65 6d 20 77 6f 75  e first item wou
31798 6c 64 20 62 65 20 64 65 6c 74 61 2d 65 6e 63 6f  ld be delta-enco
31799 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 69 4c 61 73 74  ded..**.** iLast
3179a 44 6f 63 69 64 20 69 73 20 74 68 65 20 66 69 6e  Docid is the fin
3179b 61 6c 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20  al docid in the 
3179c 64 6f 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61  doclist in pData
3179d 2e 20 20 49 74 20 69 73 0a 2a 2a 20 6e 65 65 64  .  It is.** need
3179e 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  ed to create the
3179f 20 6e 65 77 20 69 50 72 65 76 44 6f 63 69 64 20   new iPrevDocid 
317a0 66 6f 72 20 66 75 74 75 72 65 20 64 65 6c 74 61  for future delta
317a1 2d 65 6e 63 6f 64 69 6e 67 2e 20 20 54 68 65 0a  -encoding.  The.
317a2 2a 2a 20 63 6f 64 65 20 63 6f 75 6c 64 20 64 65  ** code could de
317a3 63 6f 64 65 20 74 68 65 20 70 61 73 73 65 64 20  code the passed 
317a4 64 6f 63 6c 69 73 74 20 74 6f 20 72 65 63 72 65  doclist to recre
317a5 61 74 65 20 69 4c 61 73 74 44 6f 63 69 64 2c 20  ate iLastDocid, 
317a6 62 75 74 0a 2a 2a 20 74 68 65 20 6f 6e 6c 79 20  but.** the only 
317a7 63 75 72 72 65 6e 74 20 75 73 65 72 20 28 64 6f  current user (do
317a8 63 4c 69 73 74 4d 65 72 67 65 29 20 61 6c 72 65  cListMerge) alre
317a9 61 64 79 20 68 61 73 20 64 65 63 6f 64 65 64 20  ady has decoded 
317aa 74 68 69 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  this.** informat
317ab 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28  ion..*/./* TODO(
317ac 73 68 65 73 73 29 20 54 68 69 73 20 68 61 73 20  shess) This has 
317ad 62 65 63 6f 6d 65 20 6a 75 73 74 20 61 20 68 65  become just a he
317ae 6c 70 65 72 20 66 6f 72 20 64 6f 63 4c 69 73 74  lper for docList
317af 4d 65 72 67 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64  Merge..** Consid
317b0 65 72 20 61 20 72 65 66 61 63 74 6f 72 20 74 6f  er a refactor to
317b1 20 6d 61 6b 65 20 74 68 69 73 20 63 6c 65 61 6e   make this clean
317b2 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
317b3 69 64 20 64 6c 77 41 70 70 65 6e 64 28 44 4c 57  id dlwAppend(DLW
317b4 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a  riter *pWriter,.
317b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
317b6 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
317b7 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61   *pData, int nDa
317b8 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ta,.            
317b9 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
317ba 5f 69 6e 74 36 34 20 69 46 69 72 73 74 44 6f 63  _int64 iFirstDoc
317bb 69 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  id, sqlite_int64
317bc 20 69 4c 61 73 74 44 6f 63 69 64 29 7b 0a 20 20   iLastDocid){.  
317bd 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
317be 63 69 64 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  cid = 0;.  char 
317bf 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  c[VARINT_MAX];. 
317c0 20 69 6e 74 20 6e 46 69 72 73 74 4f 6c 64 2c 20   int nFirstOld, 
317c1 6e 46 69 72 73 74 4e 65 77 3b 20 20 20 20 20 2f  nFirstNew;     /
317c2 2a 20 4f 6c 64 20 61 6e 64 20 6e 65 77 20 76 61  * Old and new va
317c3 72 69 6e 74 20 6c 65 6e 20 6f 66 20 66 69 72 73  rint len of firs
317c4 74 20 64 6f 63 69 64 2e 20 2a 2f 0a 23 69 66 6e  t docid. */.#ifn
317c5 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 71 6c  def NDEBUG.  sql
317c6 69 74 65 5f 69 6e 74 36 34 20 69 4c 61 73 74 44  ite_int64 iLastD
317c7 6f 63 69 64 44 65 6c 74 61 3b 0a 23 65 6e 64 69  ocidDelta;.#endi
317c8 66 0a 0a 20 20 2f 2a 20 52 65 63 6f 64 65 20 74  f..  /* Recode t
317c9 68 65 20 69 6e 69 74 69 61 6c 20 64 6f 63 69 64  he initial docid
317ca 20 61 73 20 64 65 6c 74 61 20 66 72 6f 6d 20 69   as delta from i
317cb 50 72 65 76 44 6f 63 69 64 2e 20 2a 2f 0a 20 20  PrevDocid. */.  
317cc 6e 46 69 72 73 74 4f 6c 64 20 3d 20 66 74 73 33  nFirstOld = fts3
317cd 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c  GetVarint(pData,
317ce 20 26 69 44 6f 63 69 64 29 3b 0a 20 20 61 73 73   &iDocid);.  ass
317cf 65 72 74 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e  ert( nFirstOld<n
317d0 44 61 74 61 20 7c 7c 20 28 6e 46 69 72 73 74 4f  Data || (nFirstO
317d1 6c 64 3d 3d 6e 44 61 74 61 20 26 26 20 70 57 72  ld==nData && pWr
317d2 69 74 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f  iter->iType==DL_
317d3 44 4f 43 49 44 53 29 20 29 3b 0a 20 20 6e 46 69  DOCIDS) );.  nFi
317d4 72 73 74 4e 65 77 20 3d 20 66 74 73 33 50 75 74  rstNew = fts3Put
317d5 56 61 72 69 6e 74 28 63 2c 20 69 46 69 72 73 74  Varint(c, iFirst
317d6 44 6f 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 69  Docid-pWriter->i
317d7 50 72 65 76 44 6f 63 69 64 29 3b 0a 0a 20 20 2f  PrevDocid);..  /
317d8 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  * Verify that th
317d9 65 20 69 6e 63 6f 6d 69 6e 67 20 64 6f 63 6c 69  e incoming docli
317da 73 74 20 69 73 20 76 61 6c 69 64 20 41 4e 44 20  st is valid AND 
317db 74 68 61 74 20 69 74 20 65 6e 64 73 20 77 69 74  that it ends wit
317dc 68 0a 20 20 2a 2a 20 74 68 65 20 65 78 70 65 63  h.  ** the expec
317dd 74 65 64 20 64 6f 63 69 64 2e 20 20 54 68 69 73  ted docid.  This
317de 20 69 73 20 65 73 73 65 6e 74 69 61 6c 20 62 65   is essential be
317df 63 61 75 73 65 20 77 65 27 6c 6c 20 74 72 75 73  cause we'll trus
317e0 74 20 74 68 69 73 0a 20 20 2a 2a 20 64 6f 63 69  t this.  ** doci
317e1 64 20 69 6e 20 66 75 74 75 72 65 20 64 65 6c 74  d in future delt
317e2 61 2d 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f  a-encoding..  */
317e3 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  .  ASSERT_VALID_
317e4 44 4f 43 4c 49 53 54 28 70 57 72 69 74 65 72 2d  DOCLIST(pWriter-
317e5 3e 69 54 79 70 65 2c 20 70 44 61 74 61 2c 20 6e  >iType, pData, n
317e6 44 61 74 61 2c 20 26 69 4c 61 73 74 44 6f 63 69  Data, &iLastDoci
317e7 64 44 65 6c 74 61 29 3b 0a 20 20 61 73 73 65 72  dDelta);.  asser
317e8 74 28 20 69 4c 61 73 74 44 6f 63 69 64 3d 3d 69  t( iLastDocid==i
317e9 46 69 72 73 74 44 6f 63 69 64 2d 69 44 6f 63 69  FirstDocid-iDoci
317ea 64 2b 69 4c 61 73 74 44 6f 63 69 64 44 65 6c 74  d+iLastDocidDelt
317eb 61 20 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65 6e  a );..  /* Appen
317ec 64 20 72 65 63 6f 64 65 64 20 69 6e 69 74 69 61  d recoded initia
317ed 6c 20 64 6f 63 69 64 20 61 6e 64 20 65 76 65 72  l docid and ever
317ee 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 52 65  ything else.  Re
317ef 73 74 20 6f 66 20 64 6f 63 69 64 73 0a 20 20 2a  st of docids.  *
317f0 2a 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  * should have be
317f1 65 6e 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64  en delta-encoded
317f2 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 69   from previous i
317f3 6e 69 74 69 61 6c 20 64 6f 63 69 64 2e 0a 20 20  nitial docid..  
317f4 2a 2f 0a 20 20 69 66 28 20 6e 46 69 72 73 74 4f  */.  if( nFirstO
317f5 6c 64 3c 6e 44 61 74 61 20 29 7b 0a 20 20 20 20  ld<nData ){.    
317f6 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
317f7 32 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c  2(pWriter->b, c,
317f8 20 6e 46 69 72 73 74 4e 65 77 2c 0a 20 20 20 20   nFirstNew,.    
317f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
317fa 20 20 70 44 61 74 61 2b 6e 46 69 72 73 74 4f 6c    pData+nFirstOl
317fb 64 2c 20 6e 44 61 74 61 2d 6e 46 69 72 73 74 4f  d, nData-nFirstO
317fc 6c 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ld);.  }else{.  
317fd 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
317fe 6e 64 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63  nd(pWriter->b, c
317ff 2c 20 6e 46 69 72 73 74 4e 65 77 29 3b 0a 20 20  , nFirstNew);.  
31800 7d 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72  }.  pWriter->iPr
31801 65 76 44 6f 63 69 64 20 3d 20 69 4c 61 73 74 44  evDocid = iLastD
31802 6f 63 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 76  ocid;.}.static v
31803 6f 69 64 20 64 6c 77 43 6f 70 79 28 44 4c 57 72  oid dlwCopy(DLWr
31804 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20 44  iter *pWriter, D
31805 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
31806 29 7b 0a 20 20 64 6c 77 41 70 70 65 6e 64 28 70  ){.  dlwAppend(p
31807 57 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 44 61  Writer, dlrDocDa
31808 74 61 28 70 52 65 61 64 65 72 29 2c 20 64 6c 72  ta(pReader), dlr
31809 44 6f 63 44 61 74 61 42 79 74 65 73 28 70 52 65  DocDataBytes(pRe
3180a 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20 20  ader),.         
3180b 20 20 20 64 6c 72 44 6f 63 69 64 28 70 52 65 61     dlrDocid(pRea
3180c 64 65 72 29 2c 20 64 6c 72 44 6f 63 69 64 28 70  der), dlrDocid(p
3180d 52 65 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74  Reader));.}.stat
3180e 69 63 20 76 6f 69 64 20 64 6c 77 41 64 64 28 44  ic void dlwAdd(D
3180f 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
31810 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
31811 44 6f 63 69 64 29 7b 0a 20 20 63 68 61 72 20 63  Docid){.  char c
31812 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20  [VARINT_MAX];.  
31813 69 6e 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56  int n = fts3PutV
31814 61 72 69 6e 74 28 63 2c 20 69 44 6f 63 69 64 2d  arint(c, iDocid-
31815 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f  pWriter->iPrevDo
31816 63 69 64 29 3b 0a 0a 20 20 2f 2a 20 44 6f 63 69  cid);..  /* Doci
31817 64 73 20 6d 75 73 74 20 61 73 63 65 6e 64 2e 20  ds must ascend. 
31818 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70 57  */.  assert( !pW
31819 72 69 74 65 72 2d 3e 68 61 73 5f 69 50 72 65 76  riter->has_iPrev
3181a 44 6f 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e  Docid || iDocid>
3181b 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f  pWriter->iPrevDo
3181c 63 69 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28  cid );.  assert(
3181d 20 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65 3d   pWriter->iType=
3181e 3d 44 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 0a 20  =DL_DOCIDS );.. 
3181f 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
31820 64 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c  d(pWriter->b, c,
31821 20 6e 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e   n);.  pWriter->
31822 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f  iPrevDocid = iDo
31823 63 69 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  cid;.#ifndef NDE
31824 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68  BUG.  pWriter->h
31825 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20  as_iPrevDocid = 
31826 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a  1;.#endif.}../**
31827 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31828 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31829 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3182a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3182b 2a 2f 0a 2f 2a 20 50 4c 52 65 61 64 65 72 20 69  */./* PLReader i
3182c 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64  s used to read d
3182d 61 74 61 20 66 72 6f 6d 20 61 20 64 6f 63 75 6d  ata from a docum
3182e 65 6e 74 27 73 20 70 6f 73 69 74 69 6f 6e 20 6c  ent's position l
3182f 69 73 74 2e 20 20 41 73 0a 2a 2a 20 74 68 65 20  ist.  As.** the 
31830 63 61 6c 6c 65 72 20 73 74 65 70 73 20 74 68 72  caller steps thr
31831 6f 75 67 68 20 74 68 65 20 6c 69 73 74 2c 20 64  ough the list, d
31832 61 74 61 20 69 73 20 63 61 63 68 65 64 20 73 6f  ata is cached so
31833 20 74 68 61 74 20 76 61 72 69 6e 74 73 0a 2a 2a   that varints.**
31834 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 62 65   only need to be
31835 20 64 65 63 6f 64 65 64 20 6f 6e 63 65 2e 0a 2a   decoded once..*
31836 2a 0a 2a 2a 20 70 6c 72 49 6e 69 74 2c 20 70 6c  *.** plrInit, pl
31837 72 44 65 73 74 72 6f 79 20 2d 20 63 72 65 61 74  rDestroy - creat
31838 65 2f 64 65 73 74 72 6f 79 20 61 20 72 65 61 64  e/destroy a read
31839 65 72 2e 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e  er..** plrColumn
3183a 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 2c 20 70  , plrPosition, p
3183b 6c 72 53 74 61 72 74 4f 66 66 73 65 74 2c 20 70  lrStartOffset, p
3183c 6c 72 45 6e 64 4f 66 66 73 65 74 20 2d 20 61 63  lrEndOffset - ac
3183d 63 65 73 73 6f 72 73 0a 2a 2a 20 70 6c 72 41 74  cessors.** plrAt
3183e 45 6e 64 20 2d 20 61 74 20 65 6e 64 20 6f 66 20  End - at end of 
3183f 73 74 72 65 61 6d 2c 20 6f 6e 6c 79 20 63 61 6c  stream, only cal
31840 6c 20 70 6c 72 44 65 73 74 72 6f 79 20 6f 6e 63  l plrDestroy onc
31841 65 20 74 72 75 65 2e 0a 2a 2a 20 70 6c 72 53 74  e true..** plrSt
31842 65 70 20 2d 20 73 74 65 70 20 74 6f 20 74 68 65  ep - step to the
31843 20 6e 65 78 74 20 65 6c 65 6d 65 6e 74 2e 0a 2a   next element..*
31844 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
31845 20 50 4c 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a   PLReader {.  /*
31846 20 54 68 65 73 65 20 72 65 66 65 72 20 74 6f 20   These refer to 
31847 74 68 65 20 6e 65 78 74 20 70 6f 73 69 74 69 6f  the next positio
31848 6e 27 73 20 64 61 74 61 2e 20 20 6e 44 61 74 61  n's data.  nData
31849 20 77 69 6c 6c 20 72 65 61 63 68 20 30 20 77 68   will reach 0 wh
3184a 65 6e 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 20  en.  ** reading 
3184b 74 68 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f  the last positio
3184c 6e 2c 20 73 6f 20 70 6c 72 53 74 65 70 28 29 20  n, so plrStep() 
3184d 73 69 67 6e 61 6c 73 20 45 4f 46 20 62 79 20 73  signals EOF by s
3184e 65 74 74 69 6e 67 0a 20 20 2a 2a 20 70 44 61 74  etting.  ** pDat
3184f 61 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a  a to NULL..  */.
31850 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44    const char *pD
31851 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61  ata;.  int nData
31852 3b 0a 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65  ;..  DocListType
31853 20 69 54 79 70 65 3b 0a 20 20 69 6e 74 20 69 43   iType;.  int iC
31854 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f  olumn;         /
31855 2a 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  * the last colum
31856 6e 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20  n read */.  int 
31857 69 50 6f 73 69 74 69 6f 6e 3b 20 20 20 20 20 20  iPosition;      
31858 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73   /* the last pos
31859 69 74 69 6f 6e 20 72 65 61 64 20 2a 2f 0a 20 20  ition read */.  
3185a 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74  int iStartOffset
3185b 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74  ;    /* the last
3185c 20 73 74 61 72 74 20 6f 66 66 73 65 74 20 72 65   start offset re
3185d 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64  ad */.  int iEnd
3185e 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  Offset;      /* 
3185f 74 68 65 20 6c 61 73 74 20 65 6e 64 20 6f 66 66  the last end off
31860 73 65 74 20 72 65 61 64 20 2a 2f 0a 7d 20 50 4c  set read */.} PL
31861 52 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20  Reader;..static 
31862 69 6e 74 20 70 6c 72 41 74 45 6e 64 28 50 4c 52  int plrAtEnd(PLR
31863 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
31864 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
31865 72 2d 3e 70 44 61 74 61 3d 3d 4e 55 4c 4c 3b 0a  r->pData==NULL;.
31866 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
31867 43 6f 6c 75 6d 6e 28 50 4c 52 65 61 64 65 72 20  Column(PLReader 
31868 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
31869 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70  ert( !plrAtEnd(p
3186a 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74  Reader) );.  ret
3186b 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 43 6f  urn pReader->iCo
3186c 6c 75 6d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69  lumn;.}.static i
3186d 6e 74 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 50  nt plrPosition(P
3186e 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
3186f 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c  ){.  assert( !pl
31870 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
31871 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
31872 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 3b 0a  der->iPosition;.
31873 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
31874 53 74 61 72 74 4f 66 66 73 65 74 28 50 4c 52 65  StartOffset(PLRe
31875 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
31876 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41 74    assert( !plrAt
31877 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
31878 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
31879 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a  ->iStartOffset;.
3187a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
3187b 45 6e 64 4f 66 66 73 65 74 28 50 4c 52 65 61 64  EndOffset(PLRead
3187c 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
3187d 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
3187e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
3187f 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
31880 69 45 6e 64 4f 66 66 73 65 74 3b 0a 7d 0a 73 74  iEndOffset;.}.st
31881 61 74 69 63 20 76 6f 69 64 20 70 6c 72 53 74 65  atic void plrSte
31882 70 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61  p(PLReader *pRea
31883 64 65 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e  der){.  int i, n
31884 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c  ;..  assert( !pl
31885 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
31886 29 3b 0a 0a 20 20 69 66 28 20 70 52 65 61 64 65  );..  if( pReade
31887 72 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20  r->nData==0 ){. 
31888 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74     pReader->pDat
31889 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65  a = NULL;.    re
3188a 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 6e 20 3d  turn;.  }..  n =
3188b 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
3188c 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
3188d 20 26 69 29 3b 0a 20 20 69 66 28 20 69 3d 3d 50   &i);.  if( i==P
3188e 4f 53 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  OS_COLUMN ){.   
3188f 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72   n += fts3GetVar
31890 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
31891 44 61 74 61 2b 6e 2c 20 26 70 52 65 61 64 65 72  Data+n, &pReader
31892 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  ->iColumn);.    
31893 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69  pReader->iPositi
31894 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 70 52 65 61  on = 0;.    pRea
31895 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65  der->iStartOffse
31896 74 20 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20  t = 0;.    n += 
31897 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
31898 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
31899 2c 20 26 69 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20  , &i);.  }.  /* 
3189a 53 68 6f 75 6c 64 20 6e 65 76 65 72 20 73 65 65  Should never see
3189b 20 61 64 6a 61 63 65 6e 74 20 63 6f 6c 75 6d 6e   adjacent column
3189c 20 63 68 61 6e 67 65 73 2e 20 2a 2f 0a 20 20 61   changes. */.  a
3189d 73 73 65 72 74 28 20 69 21 3d 50 4f 53 5f 43 4f  ssert( i!=POS_CO
3189e 4c 55 4d 4e 20 29 3b 0a 0a 20 20 69 66 28 20 69  LUMN );..  if( i
3189f 3d 3d 50 4f 53 5f 45 4e 44 20 29 7b 0a 20 20 20  ==POS_END ){.   
318a0 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
318a1 3d 20 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72  = 0;.    pReader
318a2 2d 3e 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a  ->pData = NULL;.
318a3 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
318a4 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 50 6f 73  .  pReader->iPos
318a5 69 74 69 6f 6e 20 2b 3d 20 69 2d 50 4f 53 5f 42  ition += i-POS_B
318a6 41 53 45 3b 0a 20 20 69 66 28 20 70 52 65 61 64  ASE;.  if( pRead
318a7 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  er->iType==DL_PO
318a8 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
318a9 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  ){.    n += fts3
318aa 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
318ab 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69  der->pData+n, &i
318ac 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
318ad 69 53 74 61 72 74 4f 66 66 73 65 74 20 2b 3d 20  iStartOffset += 
318ae 69 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  i;.    n += fts3
318af 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
318b0 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69  der->pData+n, &i
318b1 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
318b2 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 70 52 65  iEndOffset = pRe
318b3 61 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73  ader->iStartOffs
318b4 65 74 2b 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65  et+i;.  }.  asse
318b5 72 74 28 20 6e 3c 3d 70 52 65 61 64 65 72 2d 3e  rt( n<=pReader->
318b6 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64  nData );.  pRead
318b7 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 3b 0a  er->pData += n;.
318b8 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
318b9 20 2d 3d 20 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63   -= n;.}..static
318ba 20 76 6f 69 64 20 70 6c 72 49 6e 69 74 28 50 4c   void plrInit(PL
318bb 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c  Reader *pReader,
318bc 20 44 4c 52 65 61 64 65 72 20 2a 70 44 4c 52 65   DLReader *pDLRe
318bd 61 64 65 72 29 7b 0a 20 20 70 52 65 61 64 65 72  ader){.  pReader
318be 2d 3e 70 44 61 74 61 20 3d 20 64 6c 72 50 6f 73  ->pData = dlrPos
318bf 44 61 74 61 28 70 44 4c 52 65 61 64 65 72 29 3b  Data(pDLReader);
318c0 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  .  pReader->nDat
318c1 61 20 3d 20 64 6c 72 50 6f 73 44 61 74 61 4c 65  a = dlrPosDataLe
318c2 6e 28 70 44 4c 52 65 61 64 65 72 29 3b 0a 20 20  n(pDLReader);.  
318c3 70 52 65 61 64 65 72 2d 3e 69 54 79 70 65 20 3d  pReader->iType =
318c4 20 70 44 4c 52 65 61 64 65 72 2d 3e 69 54 79 70   pDLReader->iTyp
318c5 65 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 43  e;.  pReader->iC
318c6 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 52 65  olumn = 0;.  pRe
318c7 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20  ader->iPosition 
318c8 3d 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  = 0;.  pReader->
318c9 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 30  iStartOffset = 0
318ca 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 45 6e  ;.  pReader->iEn
318cb 64 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70  dOffset = 0;.  p
318cc 6c 72 53 74 65 70 28 70 52 65 61 64 65 72 29 3b  lrStep(pReader);
318cd 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  .}.static void p
318ce 6c 72 44 65 73 74 72 6f 79 28 50 4c 52 65 61 64  lrDestroy(PLRead
318cf 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
318d0 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72  SCRAMBLE(pReader
318d1 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
318d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
318d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
318d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
318d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50  **********/./* P
318d6 4c 57 72 69 74 65 72 20 69 73 20 75 73 65 64 20  LWriter is used 
318d7 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20  in constructing 
318d8 61 20 64 6f 63 75 6d 65 6e 74 27 73 20 70 6f 73  a document's pos
318d9 69 74 69 6f 6e 20 6c 69 73 74 2e 20 20 41 73 20  ition list.  As 
318da 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65  a.** convenience
318db 2c 20 69 66 20 69 54 79 70 65 20 69 73 20 44 4c  , if iType is DL
318dc 5f 44 4f 43 49 44 53 2c 20 50 4c 57 72 69 74 65  _DOCIDS, PLWrite
318dd 72 20 62 65 63 6f 6d 65 73 20 61 20 6e 6f 2d 6f  r becomes a no-o
318de 70 2e 0a 2a 2a 20 50 4c 57 72 69 74 65 72 20 77  p..** PLWriter w
318df 72 69 74 65 73 20 74 6f 20 74 68 65 20 61 73 73  rites to the ass
318e0 6f 63 69 61 74 65 64 20 44 4c 57 72 69 74 65 72  ociated DLWriter
318e1 27 73 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a  's buffer..**.**
318e2 20 70 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 20   plwInit - init 
318e3 66 6f 72 20 77 72 69 74 69 6e 67 20 61 20 64 6f  for writing a do
318e4 63 75 6d 65 6e 74 27 73 20 70 6f 73 6c 69 73 74  cument's poslist
318e5 2e 0a 2a 2a 20 70 6c 77 44 65 73 74 72 6f 79 20  ..** plwDestroy 
318e6 2d 20 63 6c 65 61 72 20 61 20 77 72 69 74 65 72  - clear a writer
318e7 2e 0a 2a 2a 20 70 6c 77 41 64 64 20 2d 20 61 70  ..** plwAdd - ap
318e8 70 65 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61 6e  pend position an
318e9 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61  d offset informa
318ea 74 69 6f 6e 2e 0a 2a 2a 20 70 6c 77 43 6f 70 79  tion..** plwCopy
318eb 20 2d 20 63 6f 70 79 20 6e 65 78 74 20 70 6f 73   - copy next pos
318ec 69 74 69 6f 6e 27 73 20 64 61 74 61 20 66 72 6f  ition's data fro
318ed 6d 20 72 65 61 64 65 72 20 74 6f 20 77 72 69 74  m reader to writ
318ee 65 72 2e 0a 2a 2a 20 70 6c 77 54 65 72 6d 69 6e  er..** plwTermin
318ef 61 74 65 20 2d 20 61 64 64 20 61 6e 79 20 6e 65  ate - add any ne
318f0 63 65 73 73 61 72 79 20 64 6f 63 6c 69 73 74 20  cessary doclist 
318f1 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a  terminator..**.*
318f2 2a 20 43 61 6c 6c 69 6e 67 20 70 6c 77 41 64 64  * Calling plwAdd
318f3 28 29 20 61 66 74 65 72 20 70 6c 77 54 65 72 6d  () after plwTerm
318f4 69 6e 61 74 65 28 29 20 6d 61 79 20 72 65 73 75  inate() may resu
318f5 6c 74 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a  lt in a corrupt.
318f6 2a 2a 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f  ** doclist..*/./
318f7 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 55 6e  * TODO(shess) Un
318f8 74 69 6c 20 77 65 27 76 65 20 77 72 69 74 74 65  til we've writte
318f9 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65  n the second ite
318fa 6d 2c 20 77 65 20 63 61 6e 20 63 61 63 68 65 20  m, we can cache 
318fb 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 69 74 65  the.** first ite
318fc 6d 27 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  m's information.
318fd 20 20 54 68 65 6e 20 77 65 27 64 20 68 61 76 65    Then we'd have
318fe 20 74 68 72 65 65 20 73 74 61 74 65 73 3a 0a 2a   three states:.*
318ff 2a 0a 2a 2a 20 2d 20 69 6e 69 74 69 61 6c 69 7a  *.** - initializ
31900 65 64 20 77 69 74 68 20 64 6f 63 69 64 2c 20 6e  ed with docid, n
31901 6f 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 20  o positions..** 
31902 2d 20 64 6f 63 69 64 20 61 6e 64 20 6f 6e 65 20  - docid and one 
31903 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 20 2d 20 64  position..** - d
31904 6f 63 69 64 20 61 6e 64 20 6d 75 6c 74 69 70 6c  ocid and multipl
31905 65 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a  e positions..**.
31906 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 6c 61 73 74  ** Only the last
31907 20 73 74 61 74 65 20 6e 65 65 64 73 20 74 6f 20   state needs to 
31908 61 63 74 75 61 6c 6c 79 20 77 72 69 74 65 20 74  actually write t
31909 6f 20 64 6c 77 2d 3e 62 2c 20 77 68 69 63 68 20  o dlw->b, which 
3190a 77 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 6e 20 69  would.** be an i
3190b 6d 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 74 68  mprovement in th
3190c 65 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 63 61  e DLCollector ca
3190d 73 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  se..*/.typedef s
3190e 74 72 75 63 74 20 50 4c 57 72 69 74 65 72 20 7b  truct PLWriter {
3190f 0a 20 20 44 4c 57 72 69 74 65 72 20 2a 64 6c 77  .  DLWriter *dlw
31910 3b 0a 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  ;..  int iColumn
31911 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74  ;    /* the last
31912 20 63 6f 6c 75 6d 6e 20 77 72 69 74 74 65 6e 20   column written 
31913 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73 3b 20 20  */.  int iPos;  
31914 20 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74       /* the last
31915 20 70 6f 73 69 74 69 6f 6e 20 77 72 69 74 74 65   position writte
31916 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73  n */.  int iOffs
31917 65 74 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61  et;    /* the la
31918 73 74 20 73 74 61 72 74 20 6f 66 66 73 65 74 20  st start offset 
31919 77 72 69 74 74 65 6e 20 2a 2f 0a 7d 20 50 4c 57  written */.} PLW
3191a 72 69 74 65 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28  riter;../* TODO(
3191b 73 68 65 73 73 29 20 49 6e 20 74 68 65 20 63 61  shess) In the ca
3191c 73 65 20 77 68 65 72 65 20 74 68 65 20 70 61 72  se where the par
3191d 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20 74  ent is reading t
3191e 68 65 73 65 20 76 61 6c 75 65 73 0a 2a 2a 20 66  hese values.** f
3191f 72 6f 6d 20 61 20 50 4c 52 65 61 64 65 72 2c 20  rom a PLReader, 
31920 77 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a  we could optimiz
31921 65 20 74 6f 20 61 20 63 6f 70 79 20 69 66 20 74  e to a copy if t
31922 68 61 74 20 50 4c 52 65 61 64 65 72 20 68 61 73  hat PLReader has
31923 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70  .** the same typ
31924 65 20 61 73 20 70 57 72 69 74 65 72 2e 0a 2a 2f  e as pWriter..*/
31925 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77  .static void plw
31926 41 64 64 28 50 4c 57 72 69 74 65 72 20 2a 70 57  Add(PLWriter *pW
31927 72 69 74 65 72 2c 20 69 6e 74 20 69 43 6f 6c 75  riter, int iColu
31928 6d 6e 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20  mn, int iPos,.  
31929 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3192a 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65   int iStartOffse
3192b 74 2c 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65  t, int iEndOffse
3192c 74 29 7b 0a 20 20 2f 2a 20 57 6f 72 73 74 2d 63  t){.  /* Worst-c
3192d 61 73 65 20 73 70 61 63 65 20 66 6f 72 20 50 4f  ase space for PO
3192e 53 5f 43 4f 4c 55 4d 4e 2c 20 69 43 6f 6c 75 6d  S_COLUMN, iColum
3192f 6e 2c 20 69 50 6f 73 44 65 6c 74 61 2c 0a 20 20  n, iPosDelta,.  
31930 2a 2a 20 69 53 74 61 72 74 4f 66 66 73 65 74 44  ** iStartOffsetD
31931 65 6c 74 61 2c 20 61 6e 64 20 69 45 6e 64 4f 66  elta, and iEndOf
31932 66 73 65 74 44 65 6c 74 61 2e 0a 20 20 2a 2f 0a  fsetDelta..  */.
31933 20 20 63 68 61 72 20 63 5b 35 2a 56 41 52 49 4e    char c[5*VARIN
31934 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 20  T_MAX];.  int n 
31935 3d 20 30 3b 0a 0a 20 20 2f 2a 20 42 61 6e 20 70  = 0;..  /* Ban p
31936 6c 77 41 64 64 28 29 20 61 66 74 65 72 20 70 6c  lwAdd() after pl
31937 77 54 65 72 6d 69 6e 61 74 65 28 29 2e 20 2a 2f  wTerminate(). */
31938 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
31939 65 72 2d 3e 69 50 6f 73 21 3d 2d 31 20 29 3b 0a  er->iPos!=-1 );.
3193a 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
3193b 64 6c 77 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44  dlw->iType==DL_D
3193c 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e 3b 0a  OCIDS ) return;.
3193d 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 21 3d  .  if( iColumn!=
3193e 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e  pWriter->iColumn
3193f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73   ){.    n += fts
31940 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20  3PutVarint(c+n, 
31941 50 4f 53 5f 43 4f 4c 55 4d 4e 29 3b 0a 20 20 20  POS_COLUMN);.   
31942 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72   n += fts3PutVar
31943 69 6e 74 28 63 2b 6e 2c 20 69 43 6f 6c 75 6d 6e  int(c+n, iColumn
31944 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
31945 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 75 6d  iColumn = iColum
31946 6e 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  n;.    pWriter->
31947 69 50 6f 73 20 3d 20 30 3b 0a 20 20 20 20 70 57  iPos = 0;.    pW
31948 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d  riter->iOffset =
31949 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
3194a 28 20 69 50 6f 73 3e 3d 70 57 72 69 74 65 72 2d  ( iPos>=pWriter-
3194b 3e 69 50 6f 73 20 29 3b 0a 20 20 6e 20 2b 3d 20  >iPos );.  n += 
3194c 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
3194d 6e 2c 20 50 4f 53 5f 42 41 53 45 2b 28 69 50 6f  n, POS_BASE+(iPo
3194e 73 2d 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 29  s-pWriter->iPos)
3194f 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50  );.  pWriter->iP
31950 6f 73 20 3d 20 69 50 6f 73 3b 0a 20 20 69 66 28  os = iPos;.  if(
31951 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69   pWriter->dlw->i
31952 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  Type==DL_POSITIO
31953 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20  NS_OFFSETS ){.  
31954 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72 74    assert( iStart
31955 4f 66 66 73 65 74 3e 3d 70 57 72 69 74 65 72 2d  Offset>=pWriter-
31956 3e 69 4f 66 66 73 65 74 20 29 3b 0a 20 20 20 20  >iOffset );.    
31957 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
31958 6e 74 28 63 2b 6e 2c 20 69 53 74 61 72 74 4f 66  nt(c+n, iStartOf
31959 66 73 65 74 2d 70 57 72 69 74 65 72 2d 3e 69 4f  fset-pWriter->iO
3195a 66 66 73 65 74 29 3b 0a 20 20 20 20 70 57 72 69  ffset);.    pWri
3195b 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69  ter->iOffset = i
3195c 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20  StartOffset;.   
3195d 20 61 73 73 65 72 74 28 20 69 45 6e 64 4f 66 66   assert( iEndOff
3195e 73 65 74 3e 3d 69 53 74 61 72 74 4f 66 66 73 65  set>=iStartOffse
3195f 74 20 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74  t );.    n += ft
31960 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c  s3PutVarint(c+n,
31961 20 69 45 6e 64 4f 66 66 73 65 74 2d 69 53 74 61   iEndOffset-iSta
31962 72 74 4f 66 66 73 65 74 29 3b 0a 20 20 7d 0a 20  rtOffset);.  }. 
31963 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
31964 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e  d(pWriter->dlw->
31965 62 2c 20 63 2c 20 6e 29 3b 0a 7d 0a 73 74 61 74  b, c, n);.}.stat
31966 69 63 20 76 6f 69 64 20 70 6c 77 43 6f 70 79 28  ic void plwCopy(
31967 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  PLWriter *pWrite
31968 72 2c 20 50 4c 52 65 61 64 65 72 20 2a 70 52 65  r, PLReader *pRe
31969 61 64 65 72 29 7b 0a 20 20 70 6c 77 41 64 64 28  ader){.  plwAdd(
3196a 70 57 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75  pWriter, plrColu
3196b 6d 6e 28 70 52 65 61 64 65 72 29 2c 20 70 6c 72  mn(pReader), plr
3196c 50 6f 73 69 74 69 6f 6e 28 70 52 65 61 64 65 72  Position(pReader
3196d 29 2c 0a 20 20 20 20 20 20 20 20 20 70 6c 72 53  ),.         plrS
3196e 74 61 72 74 4f 66 66 73 65 74 28 70 52 65 61 64  tartOffset(pRead
3196f 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65  er), plrEndOffse
31970 74 28 70 52 65 61 64 65 72 29 29 3b 0a 7d 0a 73  t(pReader));.}.s
31971 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 49 6e  tatic void plwIn
31972 69 74 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72  it(PLWriter *pWr
31973 69 74 65 72 2c 20 44 4c 57 72 69 74 65 72 20 2a  iter, DLWriter *
31974 64 6c 77 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  dlw, sqlite_int6
31975 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 63 68 61  4 iDocid){.  cha
31976 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b  r c[VARINT_MAX];
31977 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 70 57 72  .  int n;..  pWr
31978 69 74 65 72 2d 3e 64 6c 77 20 3d 20 64 6c 77 3b  iter->dlw = dlw;
31979 0a 0a 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d 75  ..  /* Docids mu
3197a 73 74 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20 20  st ascend. */.  
3197b 61 73 73 65 72 74 28 20 21 70 57 72 69 74 65 72  assert( !pWriter
3197c 2d 3e 64 6c 77 2d 3e 68 61 73 5f 69 50 72 65 76  ->dlw->has_iPrev
3197d 44 6f 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e  Docid || iDocid>
3197e 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50  pWriter->dlw->iP
3197f 72 65 76 44 6f 63 69 64 20 29 3b 0a 20 20 6e 20  revDocid );.  n 
31980 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
31981 63 2c 20 69 44 6f 63 69 64 2d 70 57 72 69 74 65  c, iDocid-pWrite
31982 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76 44 6f 63  r->dlw->iPrevDoc
31983 69 64 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  id);.  dataBuffe
31984 72 41 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d  rAppend(pWriter-
31985 3e 64 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a  >dlw->b, c, n);.
31986 20 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e    pWriter->dlw->
31987 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f  iPrevDocid = iDo
31988 63 69 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  cid;.#ifndef NDE
31989 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 64  BUG.  pWriter->d
3198a 6c 77 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63  lw->has_iPrevDoc
3198b 69 64 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a  id = 1;.#endif..
3198c 20 20 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75    pWriter->iColu
3198d 6d 6e 20 3d 20 30 3b 0a 20 20 70 57 72 69 74 65  mn = 0;.  pWrite
3198e 72 2d 3e 69 50 6f 73 20 3d 20 30 3b 0a 20 20 70  r->iPos = 0;.  p
3198f 57 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20  Writer->iOffset 
31990 3d 20 30 3b 0a 7d 0a 2f 2a 20 54 4f 44 4f 28 73  = 0;.}./* TODO(s
31991 68 65 73 73 29 20 53 68 6f 75 6c 64 20 70 6c 77  hess) Should plw
31992 44 65 73 74 72 6f 79 28 29 20 61 6c 73 6f 20 74  Destroy() also t
31993 65 72 6d 69 6e 61 74 65 20 74 68 65 20 64 6f 63  erminate the doc
31994 6c 69 73 74 3f 20 20 42 75 74 0a 2a 2a 20 74 68  list?  But.** th
31995 65 6e 20 70 6c 77 44 65 73 74 72 6f 79 28 29 20  en plwDestroy() 
31996 77 6f 75 6c 64 20 6e 6f 20 6c 6f 6e 67 65 72 20  would no longer 
31997 62 65 20 6a 75 73 74 20 61 20 64 65 73 74 72 75  be just a destru
31998 63 74 6f 72 2c 20 69 74 20 77 6f 75 6c 64 0a 2a  ctor, it would.*
31999 2a 20 61 6c 73 6f 20 62 65 20 64 6f 69 6e 67 20  * also be doing 
3199a 77 6f 72 6b 2c 20 77 68 69 63 68 20 69 73 6e 27  work, which isn'
3199b 74 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74  t consistent wit
3199c 68 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 69 64  h the overall id
3199d 69 6f 6d 2e 0a 2a 2a 20 41 6e 6f 74 68 65 72 20  iom..** Another 
3199e 6f 70 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20  option would be 
3199f 66 6f 72 20 70 6c 77 41 64 64 28 29 20 74 6f 20  for plwAdd() to 
319a0 61 6c 77 61 79 73 20 61 70 70 65 6e 64 20 61 6e  always append an
319a1 79 20 6e 65 63 65 73 73 61 72 79 0a 2a 2a 20 74  y necessary.** t
319a2 65 72 6d 69 6e 61 74 6f 72 2c 20 73 6f 20 74 68  erminator, so th
319a3 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 69 73  at the output is
319a4 20 61 6c 77 61 79 73 20 63 6f 72 72 65 63 74 2e   always correct.
319a5 20 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64    But that would
319a6 0a 2a 2a 20 61 64 64 20 69 6e 63 72 65 6d 65 6e  .** add incremen
319a7 74 61 6c 20 77 6f 72 6b 20 74 6f 20 74 68 65 20  tal work to the 
319a8 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 69 74 68  common case with
319a9 20 74 68 65 20 6f 6e 6c 79 20 62 65 6e 65 66 69   the only benefi
319aa 74 20 62 65 69 6e 67 0a 2a 2a 20 41 50 49 20 65  t being.** API e
319ab 6c 65 67 61 6e 63 65 2e 20 20 50 75 6e 74 20 66  legance.  Punt f
319ac 6f 72 20 6e 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69  or now..*/.stati
319ad 63 20 76 6f 69 64 20 70 6c 77 54 65 72 6d 69 6e  c void plwTermin
319ae 61 74 65 28 50 4c 57 72 69 74 65 72 20 2a 70 57  ate(PLWriter *pW
319af 72 69 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57  riter){.  if( pW
319b0 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70  riter->dlw->iTyp
319b1 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20  e>DL_DOCIDS ){. 
319b2 20 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54     char c[VARINT
319b3 5f 4d 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e  _MAX];.    int n
319b4 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
319b5 28 63 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20  (c, POS_END);.  
319b6 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
319b7 6e 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d  nd(pWriter->dlw-
319b8 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a 23  >b, c, n);.  }.#
319b9 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
319ba 2f 2a 20 4d 61 72 6b 20 61 73 20 74 65 72 6d 69  /* Mark as termi
319bb 6e 61 74 65 64 20 66 6f 72 20 61 73 73 65 72 74  nated for assert
319bc 20 69 6e 20 70 6c 77 41 64 64 28 29 2e 20 2a 2f   in plwAdd(). */
319bd 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73  .  pWriter->iPos
319be 20 3d 20 2d 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a   = -1;.#endif.}.
319bf 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 44  static void plwD
319c0 65 73 74 72 6f 79 28 50 4c 57 72 69 74 65 72 20  estroy(PLWriter 
319c1 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 53 43 52  *pWriter){.  SCR
319c2 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a  AMBLE(pWriter);.
319c3 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
319c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
319c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
319c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
319c7 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 43 6f  *******/./* DLCo
319c8 6c 6c 65 63 74 6f 72 20 77 72 61 70 73 20 50 4c  llector wraps PL
319c9 57 72 69 74 65 72 20 61 6e 64 20 44 4c 57 72 69  Writer and DLWri
319ca 74 65 72 20 74 6f 20 70 72 6f 76 69 64 65 20 61  ter to provide a
319cb 0a 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 2d  .** dynamically-
319cc 61 6c 6c 6f 63 61 74 65 64 20 64 6f 63 6c 69 73  allocated doclis
319cd 74 20 61 72 65 61 20 74 6f 20 75 73 65 20 64 75  t area to use du
319ce 72 69 6e 67 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  ring tokenizatio
319cf 6e 2e 0a 2a 2a 0a 2a 2a 20 64 6c 63 4e 65 77 20  n..**.** dlcNew 
319d0 2d 20 6d 61 6c 6c 6f 63 20 75 70 20 61 6e 64 20  - malloc up and 
319d1 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 63 6f 6c  initialize a col
319d2 6c 65 63 74 6f 72 2e 0a 2a 2a 20 64 6c 63 44 65  lector..** dlcDe
319d3 6c 65 74 65 20 2d 20 64 65 73 74 72 6f 79 20 61  lete - destroy a
319d4 20 63 6f 6c 6c 65 63 74 6f 72 20 61 6e 64 20 61   collector and a
319d5 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 69 74 65  ll contained ite
319d6 6d 73 2e 0a 2a 2a 20 64 6c 63 41 64 64 50 6f 73  ms..** dlcAddPos
319d7 20 2d 20 61 70 70 65 6e 64 20 70 6f 73 69 74 69   - append positi
319d8 6f 6e 20 61 6e 64 20 6f 66 66 73 65 74 20 69 6e  on and offset in
319d9 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 64 6c  formation..** dl
319da 63 41 64 64 44 6f 63 6c 69 73 74 20 2d 20 61 64  cAddDoclist - ad
319db 64 20 74 68 65 20 63 6f 6c 6c 65 63 74 65 64 20  d the collected 
319dc 64 6f 63 6c 69 73 74 20 74 6f 20 74 68 65 20 67  doclist to the g
319dd 69 76 65 6e 20 62 75 66 66 65 72 2e 0a 2a 2a 20  iven buffer..** 
319de 64 6c 63 4e 65 78 74 20 2d 20 74 65 72 6d 69 6e  dlcNext - termin
319df 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
319e0 64 6f 63 75 6d 65 6e 74 20 61 6e 64 20 6f 70 65  document and ope
319e1 6e 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 74 79  n another..*/.ty
319e2 70 65 64 65 66 20 73 74 72 75 63 74 20 44 4c 43  pedef struct DLC
319e3 6f 6c 6c 65 63 74 6f 72 20 7b 0a 20 20 44 61 74  ollector {.  Dat
319e4 61 42 75 66 66 65 72 20 62 3b 0a 20 20 44 4c 57  aBuffer b;.  DLW
319e5 72 69 74 65 72 20 64 6c 77 3b 0a 20 20 50 4c 57  riter dlw;.  PLW
319e6 72 69 74 65 72 20 70 6c 77 3b 0a 7d 20 44 4c 43  riter plw;.} DLC
319e7 6f 6c 6c 65 63 74 6f 72 3b 0a 0a 2f 2a 20 54 4f  ollector;../* TO
319e8 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 63  DO(shess) This c
319e9 6f 75 6c 64 20 61 6c 73 6f 20 62 65 20 64 6f 6e  ould also be don
319ea 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 70 6c 77  e by calling plw
319eb 54 65 72 6d 69 6e 61 74 65 28 29 20 61 6e 64 0a  Terminate() and.
319ec 2a 2a 20 64 61 74 61 42 75 66 66 65 72 41 70 70  ** dataBufferApp
319ed 65 6e 64 28 29 2e 20 20 49 20 74 72 69 65 64 20  end().  I tried 
319ee 74 68 61 74 2c 20 65 78 70 65 63 74 69 6e 67 20  that, expecting 
319ef 6e 6f 6d 69 6e 61 6c 20 70 65 72 66 6f 72 6d 61  nominal performa
319f0 6e 63 65 0a 2a 2a 20 64 69 66 66 65 72 65 6e 63  nce.** differenc
319f1 65 73 2c 20 62 75 74 20 69 74 20 73 65 65 6d 65  es, but it seeme
319f2 64 20 74 6f 20 70 72 65 74 74 79 20 72 65 6c 69  d to pretty reli
319f3 61 62 6c 79 20 62 65 20 77 6f 72 74 68 20 31 25  ably be worth 1%
319f4 20 74 6f 20 63 6f 64 65 0a 2a 2a 20 69 74 20 74   to code.** it t
319f5 68 69 73 20 77 61 79 2e 20 20 49 20 73 75 73 70  his way.  I susp
319f6 65 63 74 20 69 74 20 69 73 20 74 68 65 20 69 6e  ect it is the in
319f7 63 72 65 6d 65 6e 74 61 6c 20 6d 61 6c 6c 6f 63  cremental malloc
319f8 20 6f 76 65 72 68 65 61 64 20 28 73 6f 6d 65 0a   overhead (some.
319f9 2a 2a 20 70 65 72 63 65 6e 74 61 67 65 20 6f 66  ** percentage of
319fa 20 74 68 65 20 70 6c 77 54 65 72 6d 69 6e 61 74   the plwTerminat
319fb 65 28 29 20 63 61 6c 6c 73 20 77 69 6c 6c 20 63  e() calls will c
319fc 61 75 73 65 20 61 20 72 65 61 6c 6c 6f 63 29 2c  ause a realloc),
319fd 20 73 6f 0a 2a 2a 20 74 68 69 73 20 6d 69 67 68   so.** this migh
319fe 74 20 62 65 20 77 6f 72 74 68 20 72 65 76 69 73  t be worth revis
319ff 69 74 69 6e 67 20 69 66 20 74 68 65 20 44 61 74  iting if the Dat
31a00 61 42 75 66 66 65 72 20 69 6d 70 6c 65 6d 65 6e  aBuffer implemen
31a01 74 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65  tation.** change
31a02 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
31a03 64 20 64 6c 63 41 64 64 44 6f 63 6c 69 73 74 28  d dlcAddDoclist(
31a04 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f  DLCollector *pCo
31a05 6c 6c 65 63 74 6f 72 2c 20 44 61 74 61 42 75 66  llector, DataBuf
31a06 66 65 72 20 2a 62 29 7b 0a 20 20 69 66 28 20 70  fer *b){.  if( p
31a07 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2e 69  Collector->dlw.i
31a08 54 79 70 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29  Type>DL_DOCIDS )
31a09 7b 0a 20 20 20 20 63 68 61 72 20 63 5b 56 41 52  {.    char c[VAR
31a0a 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 20 20 69 6e  INT_MAX];.    in
31a0b 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72  t n = fts3PutVar
31a0c 69 6e 74 28 63 2c 20 50 4f 53 5f 45 4e 44 29 3b  int(c, POS_END);
31a0d 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
31a0e 70 70 65 6e 64 32 28 62 2c 20 70 43 6f 6c 6c 65  ppend2(b, pColle
31a0f 63 74 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70  ctor->b.pData, p
31a10 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61  Collector->b.nDa
31a11 74 61 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 65 6c  ta, c, n);.  }el
31a12 73 65 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66  se{.    dataBuff
31a13 65 72 41 70 70 65 6e 64 28 62 2c 20 70 43 6f 6c  erAppend(b, pCol
31a14 6c 65 63 74 6f 72 2d 3e 62 2e 70 44 61 74 61 2c  lector->b.pData,
31a15 20 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e   pCollector->b.n
31a16 44 61 74 61 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  Data);.  }.}.sta
31a17 74 69 63 20 76 6f 69 64 20 64 6c 63 4e 65 78 74  tic void dlcNext
31a18 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43  (DLCollector *pC
31a19 6f 6c 6c 65 63 74 6f 72 2c 20 73 71 6c 69 74 65  ollector, sqlite
31a1a 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a  _int64 iDocid){.
31a1b 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26    plwTerminate(&
31a1c 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29  pCollector->plw)
31a1d 3b 0a 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26  ;.  plwDestroy(&
31a1e 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29  pCollector->plw)
31a1f 3b 0a 20 20 70 6c 77 49 6e 69 74 28 26 70 43 6f  ;.  plwInit(&pCo
31a20 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 26 70  llector->plw, &p
31a21 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20  Collector->dlw, 
31a22 69 44 6f 63 69 64 29 3b 0a 7d 0a 73 74 61 74 69  iDocid);.}.stati
31a23 63 20 76 6f 69 64 20 64 6c 63 41 64 64 50 6f 73  c void dlcAddPos
31a24 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43  (DLCollector *pC
31a25 6f 6c 6c 65 63 74 6f 72 2c 20 69 6e 74 20 69 43  ollector, int iC
31a26 6f 6c 75 6d 6e 2c 20 69 6e 74 20 69 50 6f 73 2c  olumn, int iPos,
31a27 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
31a28 20 20 20 20 20 20 20 69 6e 74 20 69 53 74 61 72         int iStar
31a29 74 4f 66 66 73 65 74 2c 20 69 6e 74 20 69 45 6e  tOffset, int iEn
31a2a 64 4f 66 66 73 65 74 29 7b 0a 20 20 70 6c 77 41  dOffset){.  plwA
31a2b 64 64 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  dd(&pCollector->
31a2c 70 6c 77 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50  plw, iColumn, iP
31a2d 6f 73 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74  os, iStartOffset
31a2e 2c 20 69 45 6e 64 4f 66 66 73 65 74 29 3b 0a 7d  , iEndOffset);.}
31a2f 0a 0a 73 74 61 74 69 63 20 44 4c 43 6f 6c 6c 65  ..static DLColle
31a30 63 74 6f 72 20 2a 64 6c 63 4e 65 77 28 73 71 6c  ctor *dlcNew(sql
31a31 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  ite_int64 iDocid
31a32 2c 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54  , DocListType iT
31a33 79 70 65 29 7b 0a 20 20 44 4c 43 6f 6c 6c 65 63  ype){.  DLCollec
31a34 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 20  tor *pCollector 
31a35 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
31a36 28 73 69 7a 65 6f 66 28 44 4c 43 6f 6c 6c 65 63  (sizeof(DLCollec
31a37 74 6f 72 29 29 3b 0a 20 20 64 61 74 61 42 75 66  tor));.  dataBuf
31a38 66 65 72 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63  ferInit(&pCollec
31a39 74 6f 72 2d 3e 62 2c 20 30 29 3b 0a 20 20 64 6c  tor->b, 0);.  dl
31a3a 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f  wInit(&pCollecto
31a3b 72 2d 3e 64 6c 77 2c 20 69 54 79 70 65 2c 20 26  r->dlw, iType, &
31a3c 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a  pCollector->b);.
31a3d 20 20 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c    plwInit(&pColl
31a3e 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f  ector->plw, &pCo
31a3f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44  llector->dlw, iD
31a40 6f 63 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  ocid);.  return 
31a41 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 0a 73 74  pCollector;.}.st
31a42 61 74 69 63 20 76 6f 69 64 20 64 6c 63 44 65 6c  atic void dlcDel
31a43 65 74 65 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20  ete(DLCollector 
31a44 2a 70 43 6f 6c 6c 65 63 74 6f 72 29 7b 0a 20 20  *pCollector){.  
31a45 70 6c 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c  plwDestroy(&pCol
31a46 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20  lector->plw);.  
31a47 64 6c 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c  dlwDestroy(&pCol
31a48 6c 65 63 74 6f 72 2d 3e 64 6c 77 29 3b 0a 20 20  lector->dlw);.  
31a49 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
31a4a 79 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62  y(&pCollector->b
31a4b 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 43  );.  SCRAMBLE(pC
31a4c 6f 6c 6c 65 63 74 6f 72 29 3b 0a 20 20 73 71 6c  ollector);.  sql
31a4d 69 74 65 33 5f 66 72 65 65 28 70 43 6f 6c 6c 65  ite3_free(pColle
31a4e 63 74 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 20 43 6f  ctor);.}.../* Co
31a4f 70 79 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64  py the doclist d
31a50 61 74 61 20 6f 66 20 69 54 79 70 65 20 69 6e 20  ata of iType in 
31a51 70 44 61 74 61 2f 6e 44 61 74 61 20 69 6e 74 6f  pData/nData into
31a52 20 2a 6f 75 74 2c 20 74 72 69 6d 6d 69 6e 67 0a   *out, trimming.
31a53 2a 2a 20 75 6e 6e 65 63 65 73 73 61 72 79 20 64  ** unnecessary d
31a54 61 74 61 20 61 73 20 77 65 20 67 6f 2e 20 20 4f  ata as we go.  O
31a55 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20 6d 61 74 63  nly columns matc
31a56 68 69 6e 67 20 69 43 6f 6c 75 6d 6e 20 61 72 65  hing iColumn are
31a57 0a 2a 2a 20 63 6f 70 69 65 64 2c 20 61 6c 6c 20  .** copied, all 
31a58 63 6f 6c 75 6d 6e 73 20 63 6f 70 69 65 64 20 69  columns copied i
31a59 66 20 69 43 6f 6c 75 6d 6e 20 69 73 20 2d 31 2e  f iColumn is -1.
31a5a 20 20 45 6c 65 6d 65 6e 74 73 20 77 69 74 68 20    Elements with 
31a5b 6e 6f 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 20 63  no.** matching c
31a5c 6f 6c 75 6d 6e 73 20 61 72 65 20 64 72 6f 70 70  olumns are dropp
31a5d 65 64 2e 20 20 54 68 65 20 6f 75 74 70 75 74 20  ed.  The output 
31a5e 69 73 20 61 6e 20 69 4f 75 74 54 79 70 65 20 64  is an iOutType d
31a5f 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 4e 4f  oclist..*/./* NO
31a60 54 45 28 73 68 65 73 73 29 20 54 68 69 73 20 63  TE(shess) This c
31a61 6f 64 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69  ode is only vali
31a62 64 20 61 66 74 65 72 20 61 6c 6c 20 64 6f 63 6c  d after all docl
31a63 69 73 74 73 20 61 72 65 20 6d 65 72 67 65 64 2e  ists are merged.
31a64 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 72  .** If this is r
31a65 75 6e 20 62 65 66 6f 72 65 20 6d 65 72 67 65 73  un before merges
31a66 2c 20 74 68 65 6e 20 64 6f 63 6c 69 73 74 20 69  , then doclist i
31a67 74 65 6d 73 20 77 68 69 63 68 20 72 65 70 72 65  tems which repre
31a68 73 65 6e 74 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e  sent.** deletion
31a69 20 77 69 6c 6c 20 62 65 20 74 72 69 6d 6d 65 64   will be trimmed
31a6a 2c 20 61 6e 64 20 77 69 6c 6c 20 74 68 75 73 20  , and will thus 
31a6b 6e 6f 74 20 65 66 66 65 63 74 20 61 20 64 65 6c  not effect a del
31a6c 65 74 69 6f 6e 0a 2a 2a 20 64 75 72 69 6e 67 20  etion.** during 
31a6d 74 68 65 20 6d 65 72 67 65 2e 0a 2a 2f 0a 73 74  the merge..*/.st
31a6e 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73  atic void docLis
31a6f 74 54 72 69 6d 28 44 6f 63 4c 69 73 74 54 79 70  tTrim(DocListTyp
31a70 65 20 69 54 79 70 65 2c 20 63 6f 6e 73 74 20 63  e iType, const c
31a71 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
31a72 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
31a73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
31a74 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 44 6f 63 4c  nt iColumn, DocL
31a75 69 73 74 54 79 70 65 20 69 4f 75 74 54 79 70 65  istType iOutType
31a76 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75  , DataBuffer *ou
31a77 74 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64  t){.  DLReader d
31a78 6c 52 65 61 64 65 72 3b 0a 20 20 44 4c 57 72 69  lReader;.  DLWri
31a79 74 65 72 20 64 6c 57 72 69 74 65 72 3b 0a 0a 20  ter dlWriter;.. 
31a7a 20 61 73 73 65 72 74 28 20 69 4f 75 74 54 79 70   assert( iOutTyp
31a7b 65 3c 3d 69 54 79 70 65 20 29 3b 0a 0a 20 20 64  e<=iType );..  d
31a7c 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72  lrInit(&dlReader
31a7d 2c 20 69 54 79 70 65 2c 20 70 44 61 74 61 2c 20  , iType, pData, 
31a7e 6e 44 61 74 61 29 3b 0a 20 20 64 6c 77 49 6e 69  nData);.  dlwIni
31a7f 74 28 26 64 6c 57 72 69 74 65 72 2c 20 69 4f 75  t(&dlWriter, iOu
31a80 74 54 79 70 65 2c 20 6f 75 74 29 3b 0a 0a 20 20  tType, out);..  
31a81 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64  while( !dlrAtEnd
31a82 28 26 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20  (&dlReader) ){. 
31a83 20 20 20 50 4c 52 65 61 64 65 72 20 70 6c 52 65     PLReader plRe
31a84 61 64 65 72 3b 0a 20 20 20 20 50 4c 57 72 69 74  ader;.    PLWrit
31a85 65 72 20 70 6c 57 72 69 74 65 72 3b 0a 20 20 20  er plWriter;.   
31a86 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a   int match = 0;.
31a87 0a 20 20 20 20 70 6c 72 49 6e 69 74 28 26 70 6c  .    plrInit(&pl
31a88 52 65 61 64 65 72 2c 20 26 64 6c 52 65 61 64 65  Reader, &dlReade
31a89 72 29 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20  r);..    while( 
31a8a 21 70 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61  !plrAtEnd(&plRea
31a8b 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20 69 66  der) ){.      if
31a8c 28 20 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c  ( iColumn==-1 ||
31a8d 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65   plrColumn(&plRe
31a8e 61 64 65 72 29 3d 3d 69 43 6f 6c 75 6d 6e 20 29  ader)==iColumn )
31a8f 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6d  {.        if( !m
31a90 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20  atch ){.        
31a91 20 20 70 6c 77 49 6e 69 74 28 26 70 6c 57 72 69    plwInit(&plWri
31a92 74 65 72 2c 20 26 64 6c 57 72 69 74 65 72 2c 20  ter, &dlWriter, 
31a93 64 6c 72 44 6f 63 69 64 28 26 64 6c 52 65 61 64  dlrDocid(&dlRead
31a94 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  er));.          
31a95 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 20  match = 1;.     
31a96 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 6c 77     }.        plw
31a97 41 64 64 28 26 70 6c 57 72 69 74 65 72 2c 20 70  Add(&plWriter, p
31a98 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64  lrColumn(&plRead
31a99 65 72 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e  er), plrPosition
31a9a 28 26 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20  (&plReader),.   
31a9b 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72 53              plrS
31a9c 74 61 72 74 4f 66 66 73 65 74 28 26 70 6c 52 65  tartOffset(&plRe
31a9d 61 64 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66  ader), plrEndOff
31a9e 73 65 74 28 26 70 6c 52 65 61 64 65 72 29 29 3b  set(&plReader));
31a9f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
31aa0 6c 72 53 74 65 70 28 26 70 6c 52 65 61 64 65 72  lrStep(&plReader
31aa1 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
31aa2 20 6d 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20   match ){.      
31aa3 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26 70 6c  plwTerminate(&pl
31aa4 57 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20 70  Writer);.      p
31aa5 6c 77 44 65 73 74 72 6f 79 28 26 70 6c 57 72 69  lwDestroy(&plWri
31aa6 74 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ter);.    }..   
31aa7 20 70 6c 72 44 65 73 74 72 6f 79 28 26 70 6c 52   plrDestroy(&plR
31aa8 65 61 64 65 72 29 3b 0a 20 20 20 20 64 6c 72 53  eader);.    dlrS
31aa9 74 65 70 28 26 64 6c 52 65 61 64 65 72 29 3b 0a  tep(&dlReader);.
31aaa 20 20 7d 0a 20 20 64 6c 77 44 65 73 74 72 6f 79    }.  dlwDestroy
31aab 28 26 64 6c 57 72 69 74 65 72 29 3b 0a 20 20 64  (&dlWriter);.  d
31aac 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61  lrDestroy(&dlRea
31aad 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 55 73 65 64  der);.}../* Used
31aae 20 62 79 20 64 6f 63 4c 69 73 74 4d 65 72 67 65   by docListMerge
31aaf 28 29 20 74 6f 20 6b 65 65 70 20 64 6f 63 6c 69  () to keep docli
31ab0 73 74 73 20 69 6e 20 74 68 65 20 61 73 63 65 6e  sts in the ascen
31ab1 64 69 6e 67 20 6f 72 64 65 72 20 62 79 0a 2a 2a  ding order by.**
31ab2 20 64 6f 63 69 64 2c 20 74 68 65 6e 20 61 73 63   docid, then asc
31ab3 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20  ending order by 
31ab4 61 67 65 20 28 73 6f 20 74 68 65 20 6e 65 77 65  age (so the newe
31ab5 73 74 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e  st comes first).
31ab6 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
31ab7 63 74 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64  ct OrderedDLRead
31ab8 65 72 20 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  er {.  DLReader 
31ab9 2a 70 52 65 61 64 65 72 3b 0a 0a 20 20 2f 2a 20  *pReader;..  /* 
31aba 54 4f 44 4f 28 73 68 65 73 73 29 20 49 66 20 77  TODO(shess) If w
31abb 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 64 6f  e assume that do
31abc 63 4c 69 73 74 4d 65 72 67 65 20 70 52 65 61 64  cListMerge pRead
31abd 65 72 73 20 69 73 20 6f 72 64 65 72 65 64 20 62  ers is ordered b
31abe 79 0a 20 20 2a 2a 20 61 67 65 20 28 77 68 69 63  y.  ** age (whic
31abf 68 20 77 65 20 64 6f 29 2c 20 74 68 65 6e 20 77  h we do), then w
31ac0 65 20 63 6f 75 6c 64 20 75 73 65 20 70 52 65 61  e could use pRea
31ac1 64 65 72 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20  der comparisons 
31ac2 74 6f 20 62 72 65 61 6b 0a 20 20 2a 2a 20 74 69  to break.  ** ti
31ac3 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69  es..  */.  int i
31ac4 64 78 3b 0a 7d 20 4f 72 64 65 72 65 64 44 4c 52  dx;.} OrderedDLR
31ac5 65 61 64 65 72 3b 0a 0a 2f 2a 20 4f 72 64 65 72  eader;../* Order
31ac6 20 65 6f 66 20 74 6f 20 65 6e 64 2c 20 74 68 65   eof to end, the
31ac7 6e 20 62 79 20 64 6f 63 69 64 20 61 73 63 2c 20  n by docid asc, 
31ac8 69 64 78 20 64 65 73 63 2e 20 2a 2f 0a 73 74 61  idx desc. */.sta
31ac9 74 69 63 20 69 6e 74 20 6f 72 64 65 72 65 64 44  tic int orderedD
31aca 4c 52 65 61 64 65 72 43 6d 70 28 4f 72 64 65 72  LReaderCmp(Order
31acb 65 64 44 4c 52 65 61 64 65 72 20 2a 72 31 2c 20  edDLReader *r1, 
31acc 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 20  OrderedDLReader 
31acd 2a 72 32 29 7b 0a 20 20 69 66 28 20 64 6c 72 41  *r2){.  if( dlrA
31ace 74 45 6e 64 28 72 31 2d 3e 70 52 65 61 64 65 72  tEnd(r1->pReader
31acf 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72  ) ){.    if( dlr
31ad0 41 74 45 6e 64 28 72 32 2d 3e 70 52 65 61 64 65  AtEnd(r2->pReade
31ad1 72 29 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  r) ) return 0;  
31ad2 2f 2a 20 42 6f 74 68 20 61 74 45 6e 64 28 29 2e  /* Both atEnd().
31ad3 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
31ad4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
31ad5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
31ad6 2a 20 4f 6e 6c 79 20 72 31 20 61 74 45 6e 64 28  * Only r1 atEnd(
31ad7 29 2e 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ). */.  }.  if( 
31ad8 64 6c 72 41 74 45 6e 64 28 72 32 2d 3e 70 52 65  dlrAtEnd(r2->pRe
31ad9 61 64 65 72 29 20 29 20 72 65 74 75 72 6e 20 2d  ader) ) return -
31ada 31 3b 20 20 20 2f 2a 20 4f 6e 6c 79 20 72 32 20  1;   /* Only r2 
31adb 61 74 45 6e 64 28 29 2e 20 2a 2f 0a 0a 20 20 69  atEnd(). */..  i
31adc 66 28 20 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e  f( dlrDocid(r1->
31add 70 52 65 61 64 65 72 29 3c 64 6c 72 44 6f 63 69  pReader)<dlrDoci
31ade 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29  d(r2->pReader) )
31adf 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
31ae0 28 20 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70  ( dlrDocid(r1->p
31ae1 52 65 61 64 65 72 29 3e 64 6c 72 44 6f 63 69 64  Reader)>dlrDocid
31ae2 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20  (r2->pReader) ) 
31ae3 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 2f 2a 20  return 1;..  /* 
31ae4 44 65 73 63 65 6e 64 69 6e 67 20 6f 6e 20 69 64  Descending on id
31ae5 78 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  x. */.  return r
31ae6 32 2d 3e 69 64 78 2d 72 31 2d 3e 69 64 78 3b 0a  2->idx-r1->idx;.
31ae7 7d 0a 0a 2f 2a 20 42 75 62 62 6c 65 20 70 5b 30  }../* Bubble p[0
31ae8 5d 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65  ] to appropriate
31ae9 20 70 6c 61 63 65 20 69 6e 20 70 5b 31 2e 2e 6e   place in p[1..n
31aea 2d 31 5d 2e 20 20 41 73 73 75 6d 65 73 20 74 68  -1].  Assumes th
31aeb 61 74 0a 2a 2a 20 70 5b 31 2e 2e 6e 2d 31 5d 20  at.** p[1..n-1] 
31aec 69 73 20 61 6c 72 65 61 64 79 20 73 6f 72 74 65  is already sorte
31aed 64 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  d..*/./* TODO(sh
31aee 65 73 73 29 20 49 73 20 74 68 69 73 20 66 72 65  ess) Is this fre
31aef 71 75 65 6e 74 20 65 6e 6f 75 67 68 20 74 6f 20  quent enough to 
31af0 77 61 72 72 61 6e 74 20 61 20 62 69 6e 61 72 79  warrant a binary
31af1 20 73 65 61 72 63 68 3f 0a 2a 2a 20 42 65 66 6f   search?.** Befo
31af2 72 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  re implementing 
31af3 74 68 61 74 2c 20 69 6e 73 74 72 75 6d 65 6e 74  that, instrument
31af4 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63 68 65   the code to che
31af5 63 6b 2e 20 20 49 6e 20 6d 6f 73 74 0a 2a 2a 20  ck.  In most.** 
31af6 63 75 72 72 65 6e 74 20 75 73 61 67 65 2c 20 49  current usage, I
31af7 20 65 78 70 65 63 74 20 74 68 61 74 20 70 5b 30   expect that p[0
31af8 5d 20 77 69 6c 6c 20 62 65 20 6c 65 73 73 20 74  ] will be less t
31af9 68 61 6e 20 70 5b 31 5d 20 61 20 76 65 72 79 0a  han p[1] a very.
31afa 2a 2a 20 68 69 67 68 20 70 72 6f 70 6f 72 74 69  ** high proporti
31afb 6f 6e 20 6f 66 20 74 68 65 20 74 69 6d 65 2e 0a  on of the time..
31afc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
31afd 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 52 65  rderedDLReaderRe
31afe 6f 72 64 65 72 28 4f 72 64 65 72 65 64 44 4c 52  order(OrderedDLR
31aff 65 61 64 65 72 20 2a 70 2c 20 69 6e 74 20 6e 29  eader *p, int n)
31b00 7b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26  {.  while( n>1 &
31b01 26 20 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65  & orderedDLReade
31b02 72 43 6d 70 28 70 2c 20 70 2b 31 29 3e 30 20 29  rCmp(p, p+1)>0 )
31b03 7b 0a 20 20 20 20 4f 72 64 65 72 65 64 44 4c 52  {.    OrderedDLR
31b04 65 61 64 65 72 20 74 6d 70 20 3d 20 70 5b 30 5d  eader tmp = p[0]
31b05 3b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 70 5b 31  ;.    p[0] = p[1
31b06 5d 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 20 74 6d  ];.    p[1] = tm
31b07 70 3b 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20 20 20  p;.    n--;.    
31b08 70 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 47  p++;.  }.}../* G
31b09 69 76 65 6e 20 61 6e 20 61 72 72 61 79 20 6f 66  iven an array of
31b0a 20 64 6f 63 6c 69 73 74 20 72 65 61 64 65 72 73   doclist readers
31b0b 2c 20 6d 65 72 67 65 20 74 68 65 69 72 20 64 6f  , merge their do
31b0c 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74 73 0a 2a  clist elements.*
31b0d 2a 20 69 6e 74 6f 20 6f 75 74 20 69 6e 20 73 6f  * into out in so
31b0e 72 74 65 64 20 6f 72 64 65 72 20 28 62 79 20 64  rted order (by d
31b0f 6f 63 69 64 29 2c 20 64 72 6f 70 70 69 6e 67 20  ocid), dropping 
31b10 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6c  elements from ol
31b11 64 65 72 0a 2a 2a 20 72 65 61 64 65 72 73 20 77  der.** readers w
31b12 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 64  hen there is a d
31b13 75 70 6c 69 63 61 74 65 20 64 6f 63 69 64 2e 20  uplicate docid. 
31b14 20 70 52 65 61 64 65 72 73 20 69 73 20 61 73 73   pReaders is ass
31b15 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 6f 72  umed to be.** or
31b16 64 65 72 65 64 20 62 79 20 61 67 65 2c 20 6f 6c  dered by age, ol
31b17 64 65 73 74 20 66 69 72 73 74 2e 0a 2a 2f 0a 2f  dest first..*/./
31b18 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 6e 52  * TODO(shess) nR
31b19 65 61 64 65 72 73 20 6d 75 73 74 20 62 65 20 3c  eaders must be <
31b1a 3d 20 4d 45 52 47 45 5f 43 4f 55 4e 54 2e 20 20  = MERGE_COUNT.  
31b1b 54 68 69 73 20 73 68 6f 75 6c 64 20 70 72 6f 62  This should prob
31b1c 61 62 6c 79 0a 2a 2a 20 62 65 20 66 69 78 65 64  ably.** be fixed
31b1d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
31b1e 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 44 61   docListMerge(Da
31b1f 74 61 42 75 66 66 65 72 20 2a 6f 75 74 2c 0a 20  taBuffer *out,. 
31b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31b21 20 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72          DLReader
31b22 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20   *pReaders, int 
31b23 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 4f 72 64  nReaders){.  Ord
31b24 65 72 65 64 44 4c 52 65 61 64 65 72 20 72 65 61  eredDLReader rea
31b25 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54  ders[MERGE_COUNT
31b26 5d 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72  ];.  DLWriter wr
31b27 69 74 65 72 3b 0a 20 20 69 6e 74 20 69 2c 20 6e  iter;.  int i, n
31b28 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
31b29 70 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e  pStart = 0;.  in
31b2a 74 20 6e 53 74 61 72 74 20 3d 20 30 3b 0a 20 20  t nStart = 0;.  
31b2b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 46 69  sqlite_int64 iFi
31b2c 72 73 74 44 6f 63 69 64 20 3d 20 30 2c 20 69 4c  rstDocid = 0, iL
31b2d 61 73 74 44 6f 63 69 64 20 3d 20 30 3b 0a 0a 20  astDocid = 0;.. 
31b2e 20 61 73 73 65 72 74 28 20 6e 52 65 61 64 65 72   assert( nReader
31b2f 73 3e 30 20 29 3b 0a 20 20 69 66 28 20 6e 52 65  s>0 );.  if( nRe
31b30 61 64 65 72 73 3d 3d 31 20 29 7b 0a 20 20 20 20  aders==1 ){.    
31b31 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
31b32 28 6f 75 74 2c 20 64 6c 72 44 6f 63 44 61 74 61  (out, dlrDocData
31b33 28 70 52 65 61 64 65 72 73 29 2c 20 64 6c 72 41  (pReaders), dlrA
31b34 6c 6c 44 61 74 61 42 79 74 65 73 28 70 52 65 61  llDataBytes(pRea
31b35 64 65 72 73 29 29 3b 0a 20 20 20 20 72 65 74 75  ders));.    retu
31b36 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  rn;.  }..  asser
31b37 74 28 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45 52  t( nReaders<=MER
31b38 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 6e 20  GE_COUNT );.  n 
31b39 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
31b3a 69 3c 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29  i<nReaders; i++)
31b3b 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52  {.    assert( pR
31b3c 65 61 64 65 72 73 5b 69 5d 2e 69 54 79 70 65 3d  eaders[i].iType=
31b3d 3d 70 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79  =pReaders[0].iTy
31b3e 70 65 20 29 3b 0a 20 20 20 20 72 65 61 64 65 72  pe );.    reader
31b3f 73 5b 69 5d 2e 70 52 65 61 64 65 72 20 3d 20 70  s[i].pReader = p
31b40 52 65 61 64 65 72 73 2b 69 3b 0a 20 20 20 20 72  Readers+i;.    r
31b41 65 61 64 65 72 73 5b 69 5d 2e 69 64 78 20 3d 20  eaders[i].idx = 
31b42 69 3b 0a 20 20 20 20 6e 20 2b 3d 20 64 6c 72 41  i;.    n += dlrA
31b43 6c 6c 44 61 74 61 42 79 74 65 73 28 26 70 52 65  llDataBytes(&pRe
31b44 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aders[i]);.  }. 
31b45 20 2f 2a 20 43 6f 6e 73 65 72 76 61 74 69 76 65   /* Conservative
31b46 6c 79 20 73 69 7a 65 20 6f 75 74 70 75 74 20 74  ly size output t
31b47 6f 20 73 75 6d 20 6f 66 20 69 6e 70 75 74 73 2e  o sum of inputs.
31b48 20 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20    Output should 
31b49 65 6e 64 0a 20 20 2a 2a 20 75 70 20 73 74 72 69  end.  ** up stri
31b4a 63 74 6c 79 20 73 6d 61 6c 6c 65 72 20 74 68 61  ctly smaller tha
31b4b 6e 20 69 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20 20  n input..  */.  
31b4c 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e 64  dataBufferExpand
31b4d 28 6f 75 74 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20  (out, n);..  /* 
31b4e 47 65 74 20 74 68 65 20 72 65 61 64 65 72 73 20  Get the readers 
31b4f 69 6e 74 6f 20 73 6f 72 74 65 64 20 6f 72 64 65  into sorted orde
31b50 72 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69  r. */.  while( i
31b51 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 6f 72 64 65  -->0 ){.    orde
31b52 72 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64  redDLReaderReord
31b53 65 72 28 72 65 61 64 65 72 73 2b 69 2c 20 6e 52  er(readers+i, nR
31b54 65 61 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 0a  eaders-i);.  }..
31b55 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65    dlwInit(&write
31b56 72 2c 20 70 52 65 61 64 65 72 73 5b 30 5d 2e 69  r, pReaders[0].i
31b57 54 79 70 65 2c 20 6f 75 74 29 3b 0a 20 20 77 68  Type, out);.  wh
31b58 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 72  ile( !dlrAtEnd(r
31b59 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65  eaders[0].pReade
31b5a 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  r) ){.    sqlite
31b5b 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 20 3d 20  _int64 iDocid = 
31b5c 64 6c 72 44 6f 63 69 64 28 72 65 61 64 65 72 73  dlrDocid(readers
31b5d 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 0a 20  [0].pReader);.. 
31b5e 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
31b5f 20 61 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20   a continuation 
31b60 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 62  of the current b
31b61 75 66 66 65 72 20 74 6f 20 63 6f 70 79 2c 20 65  uffer to copy, e
31b62 78 74 65 6e 64 0a 20 20 20 20 2a 2a 20 74 68 61  xtend.    ** tha
31b63 74 20 62 75 66 66 65 72 2e 20 20 6d 65 6d 63 70  t buffer.  memcp
31b64 79 28 29 20 73 65 65 6d 73 20 74 6f 20 62 65 20  y() seems to be 
31b65 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 69  more efficient i
31b66 66 20 69 74 20 68 61 73 20 61 0a 20 20 20 20 2a  f it has a.    *
31b67 2a 20 6c 6f 74 73 20 6f 66 20 64 61 74 61 20 74  * lots of data t
31b68 6f 20 63 6f 70 79 2e 0a 20 20 20 20 2a 2f 0a 20  o copy..    */. 
31b69 20 20 20 69 66 28 20 64 6c 72 44 6f 63 44 61 74     if( dlrDocDat
31b6a 61 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65  a(readers[0].pRe
31b6b 61 64 65 72 29 3d 3d 70 53 74 61 72 74 2b 6e 53  ader)==pStart+nS
31b6c 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 6e 53  tart ){.      nS
31b6d 74 61 72 74 20 2b 3d 20 64 6c 72 44 6f 63 44 61  tart += dlrDocDa
31b6e 74 61 42 79 74 65 73 28 72 65 61 64 65 72 73 5b  taBytes(readers[
31b6f 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20  0].pReader);.   
31b70 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
31b71 28 20 70 53 74 61 72 74 21 3d 30 20 29 7b 0a 20  ( pStart!=0 ){. 
31b72 20 20 20 20 20 20 20 64 6c 77 41 70 70 65 6e 64         dlwAppend
31b73 28 26 77 72 69 74 65 72 2c 20 70 53 74 61 72 74  (&writer, pStart
31b74 2c 20 6e 53 74 61 72 74 2c 20 69 46 69 72 73 74  , nStart, iFirst
31b75 44 6f 63 69 64 2c 20 69 4c 61 73 74 44 6f 63 69  Docid, iLastDoci
31b76 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  d);.      }.    
31b77 20 20 70 53 74 61 72 74 20 3d 20 64 6c 72 44 6f    pStart = dlrDo
31b78 63 44 61 74 61 28 72 65 61 64 65 72 73 5b 30 5d  cData(readers[0]
31b79 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20  .pReader);.     
31b7a 20 6e 53 74 61 72 74 20 3d 20 64 6c 72 44 6f 63   nStart = dlrDoc
31b7b 44 61 74 61 42 79 74 65 73 28 72 65 61 64 65 72  DataBytes(reader
31b7c 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20  s[0].pReader);. 
31b7d 20 20 20 20 20 69 46 69 72 73 74 44 6f 63 69 64       iFirstDocid
31b7e 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20 20 20 7d   = iDocid;.    }
31b7f 0a 20 20 20 20 69 4c 61 73 74 44 6f 63 69 64 20  .    iLastDocid 
31b80 3d 20 69 44 6f 63 69 64 3b 0a 20 20 20 20 64 6c  = iDocid;.    dl
31b81 72 53 74 65 70 28 72 65 61 64 65 72 73 5b 30 5d  rStep(readers[0]
31b82 2e 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20  .pReader);..    
31b83 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 6f 66 20 74  /* Drop all of t
31b84 68 65 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74  he older element
31b85 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
31b86 64 6f 63 69 64 2e 20 2a 2f 0a 20 20 20 20 66 6f  docid. */.    fo
31b87 72 28 69 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72  r(i=1; i<nReader
31b88 73 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  s &&.           
31b89 20 20 21 64 6c 72 41 74 45 6e 64 28 72 65 61 64    !dlrAtEnd(read
31b8a 65 72 73 5b 69 5d 2e 70 52 65 61 64 65 72 29 20  ers[i].pReader) 
31b8b 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  &&.             
31b8c 64 6c 72 44 6f 63 69 64 28 72 65 61 64 65 72 73  dlrDocid(readers
31b8d 5b 69 5d 2e 70 52 65 61 64 65 72 29 3d 3d 69 44  [i].pReader)==iD
31b8e 6f 63 69 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ocid; i++){.    
31b8f 20 20 64 6c 72 53 74 65 70 28 72 65 61 64 65 72    dlrStep(reader
31b90 73 5b 69 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20  s[i].pReader);. 
31b91 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 74     }..    /* Get
31b92 20 74 68 65 20 72 65 61 64 65 72 73 20 62 61 63   the readers bac
31b93 6b 20 69 6e 74 6f 20 6f 72 64 65 72 2e 20 2a 2f  k into order. */
31b94 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e  .    while( i-->
31b95 30 20 29 7b 0a 20 20 20 20 20 20 6f 72 64 65 72  0 ){.      order
31b96 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65  edDLReaderReorde
31b97 72 28 72 65 61 64 65 72 73 2b 69 2c 20 6e 52 65  r(readers+i, nRe
31b98 61 64 65 72 73 2d 69 29 3b 0a 20 20 20 20 7d 0a  aders-i);.    }.
31b99 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f    }..  /* Copy o
31b9a 76 65 72 20 61 6e 79 20 72 65 6d 61 69 6e 69 6e  ver any remainin
31b9b 67 20 65 6c 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20  g elements. */. 
31b9c 20 69 66 28 20 6e 53 74 61 72 74 3e 30 20 29 20   if( nStart>0 ) 
31b9d 64 6c 77 41 70 70 65 6e 64 28 26 77 72 69 74 65  dlwAppend(&write
31b9e 72 2c 20 70 53 74 61 72 74 2c 20 6e 53 74 61 72  r, pStart, nStar
31b9f 74 2c 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20  t, iFirstDocid, 
31ba0 69 4c 61 73 74 44 6f 63 69 64 29 3b 0a 20 20 64  iLastDocid);.  d
31ba1 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  lwDestroy(&write
31ba2 72 29 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72  r);.}../* Helper
31ba3 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 6f   function for po
31ba4 73 4c 69 73 74 55 6e 69 6f 6e 28 29 2e 20 20 43  sListUnion().  C
31ba5 6f 6d 70 61 72 65 73 20 74 68 65 20 63 75 72 72  ompares the curr
31ba6 65 6e 74 20 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20  ent position.** 
31ba7 62 65 74 77 65 65 6e 20 6c 65 66 74 20 61 6e 64  between left and
31ba8 20 72 69 67 68 74 2c 20 72 65 74 75 72 6e 69 6e   right, returnin
31ba9 67 20 61 73 20 73 74 61 6e 64 61 72 64 20 43 20  g as standard C 
31baa 69 64 69 6f 6d 20 6f 66 20 3c 30 20 69 66 0a 2a  idiom of <0 if.*
31bab 2a 20 6c 65 66 74 3c 72 69 67 68 74 2c 20 3e 30  * left<right, >0
31bac 20 69 66 20 6c 65 66 74 3e 72 69 67 68 74 2c 20   if left>right, 
31bad 61 6e 64 20 30 20 69 66 20 6c 65 66 74 3d 3d 72  and 0 if left==r
31bae 69 67 68 74 2e 20 20 22 45 6e 64 22 20 61 6c 77  ight.  "End" alw
31baf 61 79 73 0a 2a 2a 20 63 6f 6d 70 61 72 65 73 20  ays.** compares 
31bb0 67 72 65 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74  greater..*/.stat
31bb1 69 63 20 69 6e 74 20 70 6f 73 4c 69 73 74 43 6d  ic int posListCm
31bb2 70 28 50 4c 52 65 61 64 65 72 20 2a 70 4c 65 66  p(PLReader *pLef
31bb3 74 2c 20 50 4c 52 65 61 64 65 72 20 2a 70 52 69  t, PLReader *pRi
31bb4 67 68 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ght){.  assert( 
31bb5 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 52  pLeft->iType==pR
31bb6 69 67 68 74 2d 3e 69 54 79 70 65 20 29 3b 0a 20  ight->iType );. 
31bb7 20 69 66 28 20 70 4c 65 66 74 2d 3e 69 54 79 70   if( pLeft->iTyp
31bb8 65 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72  e==DL_DOCIDS ) r
31bb9 65 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20  eturn 0;..  if( 
31bba 70 6c 72 41 74 45 6e 64 28 70 4c 65 66 74 29 20  plrAtEnd(pLeft) 
31bbb 29 20 72 65 74 75 72 6e 20 70 6c 72 41 74 45 6e  ) return plrAtEn
31bbc 64 28 70 52 69 67 68 74 29 20 3f 20 30 20 3a 20  d(pRight) ? 0 : 
31bbd 31 3b 0a 20 20 69 66 28 20 70 6c 72 41 74 45 6e  1;.  if( plrAtEn
31bbe 64 28 70 52 69 67 68 74 29 20 29 20 72 65 74 75  d(pRight) ) retu
31bbf 72 6e 20 2d 31 3b 0a 0a 20 20 69 66 28 20 70 6c  rn -1;..  if( pl
31bc0 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3c 70  rColumn(pLeft)<p
31bc1 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29  lrColumn(pRight)
31bc2 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
31bc3 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c  if( plrColumn(pL
31bc4 65 66 74 29 3e 70 6c 72 43 6f 6c 75 6d 6e 28 70  eft)>plrColumn(p
31bc5 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
31bc6 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 50 6f 73  1;..  if( plrPos
31bc7 69 74 69 6f 6e 28 70 4c 65 66 74 29 3c 70 6c 72  ition(pLeft)<plr
31bc8 50 6f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29  Position(pRight)
31bc9 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
31bca 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28  if( plrPosition(
31bcb 70 4c 65 66 74 29 3e 70 6c 72 50 6f 73 69 74 69  pLeft)>plrPositi
31bcc 6f 6e 28 70 52 69 67 68 74 29 20 29 20 72 65 74  on(pRight) ) ret
31bcd 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 70 4c 65  urn 1;.  if( pLe
31bce 66 74 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  ft->iType==DL_PO
31bcf 53 49 54 49 4f 4e 53 20 29 20 72 65 74 75 72 6e  SITIONS ) return
31bd0 20 30 3b 0a 0a 20 20 69 66 28 20 70 6c 72 53 74   0;..  if( plrSt
31bd1 61 72 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29  artOffset(pLeft)
31bd2 3c 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28  <plrStartOffset(
31bd3 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
31bd4 20 2d 31 3b 0a 20 20 69 66 28 20 70 6c 72 53 74   -1;.  if( plrSt
31bd5 61 72 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29  artOffset(pLeft)
31bd6 3e 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28  >plrStartOffset(
31bd7 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
31bd8 20 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 45 6e   1;..  if( plrEn
31bd9 64 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70  dOffset(pLeft)<p
31bda 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 52 69 67  lrEndOffset(pRig
31bdb 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  ht) ) return -1;
31bdc 0a 20 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66  .  if( plrEndOff
31bdd 73 65 74 28 70 4c 65 66 74 29 3e 70 6c 72 45 6e  set(pLeft)>plrEn
31bde 64 4f 66 66 73 65 74 28 70 52 69 67 68 74 29 20  dOffset(pRight) 
31bdf 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 72  ) return 1;..  r
31be0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57  eturn 0;.}../* W
31be1 72 69 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f  rite the union o
31be2 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73  f position lists
31be3 20 69 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52   in pLeft and pR
31be4 69 67 68 74 20 74 6f 20 70 4f 75 74 2e 0a 2a 2a  ight to pOut..**
31be5 20 22 55 6e 69 6f 6e 22 20 69 6e 20 74 68 69 73   "Union" in this
31be6 20 63 61 73 65 20 6d 65 61 6e 69 6e 67 20 22 41   case meaning "A
31be7 6c 6c 20 75 6e 69 71 75 65 20 70 6f 73 69 74 69  ll unique positi
31be8 6f 6e 20 74 75 70 6c 65 73 22 2e 20 20 53 68 6f  on tuples".  Sho
31be9 75 6c 64 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68  uld.** work with
31bea 20 61 6e 79 20 64 6f 63 6c 69 73 74 20 74 79 70   any doclist typ
31beb 65 2c 20 74 68 6f 75 67 68 20 62 6f 74 68 20 69  e, though both i
31bec 6e 70 75 74 73 20 61 6e 64 20 74 68 65 20 6f 75  nputs and the ou
31bed 74 70 75 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  tput.** should b
31bee 65 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 2e  e the same type.
31bef 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
31bf0 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28 44 4c 52  posListUnion(DLR
31bf1 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 44 4c  eader *pLeft, DL
31bf2 52 65 61 64 65 72 20 2a 70 52 69 67 68 74 2c 20  Reader *pRight, 
31bf3 44 4c 57 72 69 74 65 72 20 2a 70 4f 75 74 29 7b  DLWriter *pOut){
31bf4 0a 20 20 50 4c 52 65 61 64 65 72 20 6c 65 66 74  .  PLReader left
31bf5 2c 20 72 69 67 68 74 3b 0a 20 20 50 4c 57 72 69  , right;.  PLWri
31bf6 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 61  ter writer;..  a
31bf7 73 73 65 72 74 28 20 64 6c 72 44 6f 63 69 64 28  ssert( dlrDocid(
31bf8 70 4c 65 66 74 29 3d 3d 64 6c 72 44 6f 63 69 64  pLeft)==dlrDocid
31bf9 28 70 52 69 67 68 74 29 20 29 3b 0a 20 20 61 73  (pRight) );.  as
31bfa 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 69 54 79  sert( pLeft->iTy
31bfb 70 65 3d 3d 70 52 69 67 68 74 2d 3e 69 54 79 70  pe==pRight->iTyp
31bfc 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
31bfd 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 4f 75  Left->iType==pOu
31bfe 74 2d 3e 69 54 79 70 65 20 29 3b 0a 0a 20 20 70  t->iType );..  p
31bff 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 70 4c  lrInit(&left, pL
31c00 65 66 74 29 3b 0a 20 20 70 6c 72 49 6e 69 74 28  eft);.  plrInit(
31c01 26 72 69 67 68 74 2c 20 70 52 69 67 68 74 29 3b  &right, pRight);
31c02 0a 20 20 70 6c 77 49 6e 69 74 28 26 77 72 69 74  .  plwInit(&writ
31c03 65 72 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f 63  er, pOut, dlrDoc
31c04 69 64 28 70 4c 65 66 74 29 29 3b 0a 0a 20 20 77  id(pLeft));..  w
31c05 68 69 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28  hile( !plrAtEnd(
31c06 26 6c 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74  &left) || !plrAt
31c07 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  End(&right) ){. 
31c08 20 20 20 69 6e 74 20 63 20 3d 20 70 6f 73 4c 69     int c = posLi
31c09 73 74 43 6d 70 28 26 6c 65 66 74 2c 20 26 72 69  stCmp(&left, &ri
31c0a 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 63 3c  ght);.    if( c<
31c0b 30 20 29 7b 0a 20 20 20 20 20 20 70 6c 77 43 6f  0 ){.      plwCo
31c0c 70 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66  py(&writer, &lef
31c0d 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65  t);.      plrSte
31c0e 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65  p(&left);.    }e
31c0f 6c 73 65 20 69 66 28 20 63 3e 30 20 29 7b 0a 20  lse if( c>0 ){. 
31c10 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72       plwCopy(&wr
31c11 69 74 65 72 2c 20 26 72 69 67 68 74 29 3b 0a 20  iter, &right);. 
31c12 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72 69       plrStep(&ri
31c13 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ght);.    }else{
31c14 0a 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26  .      plwCopy(&
31c15 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a  writer, &left);.
31c16 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c        plrStep(&l
31c17 65 66 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53  eft);.      plrS
31c18 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
31c19 20 7d 0a 20 20 7d 0a 0a 20 20 70 6c 77 54 65 72   }.  }..  plwTer
31c1a 6d 69 6e 61 74 65 28 26 77 72 69 74 65 72 29 3b  minate(&writer);
31c1b 0a 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77  .  plwDestroy(&w
31c1c 72 69 74 65 72 29 3b 0a 20 20 70 6c 72 44 65 73  riter);.  plrDes
31c1d 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 70  troy(&left);.  p
31c1e 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74  lrDestroy(&right
31c1f 29 3b 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65 20 74  );.}../* Write t
31c20 68 65 20 75 6e 69 6f 6e 20 6f 66 20 64 6f 63 6c  he union of docl
31c21 69 73 74 73 20 69 6e 20 70 4c 65 66 74 20 61 6e  ists in pLeft an
31c22 64 20 70 52 69 67 68 74 20 74 6f 20 70 4f 75 74  d pRight to pOut
31c23 2e 20 20 46 6f 72 0a 2a 2a 20 64 6f 63 69 64 73  .  For.** docids
31c24 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 65 74 77 65   in common betwe
31c25 65 6e 20 74 68 65 20 69 6e 70 75 74 73 2c 20 74  en the inputs, t
31c26 68 65 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 20  he union of the 
31c27 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 6c 69 73 74  position.** list
31c28 73 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49  s is written.  I
31c29 6e 70 75 74 73 20 61 6e 64 20 6f 75 74 70 75 74  nputs and output
31c2a 73 20 61 72 65 20 61 6c 77 61 79 73 20 74 79 70  s are always typ
31c2b 65 20 44 4c 5f 44 45 46 41 55 4c 54 2e 0a 2a 2f  e DL_DEFAULT..*/
31c2c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63  .static void doc
31c2d 4c 69 73 74 55 6e 69 6f 6e 28 0a 20 20 63 6f 6e  ListUnion(.  con
31c2e 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74 2c 20  st char *pLeft, 
31c2f 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e  int nLeft,.  con
31c30 73 74 20 63 68 61 72 20 2a 70 52 69 67 68 74 2c  st char *pRight,
31c31 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 44   int nRight,.  D
31c32 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75 74 20  ataBuffer *pOut 
31c33 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
31c34 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c 69  e combined docli
31c35 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  st here */.){.  
31c36 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72  DLReader left, r
31c37 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74 65 72  ight;.  DLWriter
31c38 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66 28 20   writer;..  if( 
31c39 6e 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20  nLeft==0 ){.    
31c3a 69 66 28 20 6e 52 69 67 68 74 21 3d 30 29 20 64  if( nRight!=0) d
31c3b 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
31c3c 70 4f 75 74 2c 20 70 52 69 67 68 74 2c 20 6e 52  pOut, pRight, nR
31c3d 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72  ight);.    retur
31c3e 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 69  n;.  }.  if( nRi
31c3f 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61  ght==0 ){.    da
31c40 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70  taBufferAppend(p
31c41 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66  Out, pLeft, nLef
31c42 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
31c43 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26    }..  dlrInit(&
31c44 6c 65 66 74 2c 20 44 4c 5f 44 45 46 41 55 4c 54  left, DL_DEFAULT
31c45 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
31c46 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68  .  dlrInit(&righ
31c47 74 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  t, DL_DEFAULT, p
31c48 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a  Right, nRight);.
31c49 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65    dlwInit(&write
31c4a 72 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  r, DL_DEFAULT, p
31c4b 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
31c4c 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
31c4d 20 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 72   || !dlrAtEnd(&r
31c4e 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28  ight) ){.    if(
31c4f 20 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74   dlrAtEnd(&right
31c50 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f  ) ){.      dlwCo
31c51 70 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66  py(&writer, &lef
31c52 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  t);.      dlrSte
31c53 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65  p(&left);.    }e
31c54 6c 73 65 20 69 66 28 20 64 6c 72 41 74 45 6e 64  lse if( dlrAtEnd
31c55 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20  (&left) ){.     
31c56 20 64 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72   dlwCopy(&writer
31c57 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
31c58 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
31c59 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
31c5a 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c  dlrDocid(&left)<
31c5b 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
31c5c 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f 70   ){.      dlwCop
31c5d 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74  y(&writer, &left
31c5e 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
31c5f 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c  (&left);.    }el
31c60 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
31c61 26 6c 65 66 74 29 3e 64 6c 72 44 6f 63 69 64 28  &left)>dlrDocid(
31c62 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
31c63 20 64 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72   dlwCopy(&writer
31c64 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
31c65 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
31c66 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
31c67 20 20 20 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28     posListUnion(
31c68 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20 26  &left, &right, &
31c69 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20 64  writer);.      d
31c6a 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20  lrStep(&left);. 
31c6b 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69       dlrStep(&ri
31c6c 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ght);.    }.  }.
31c6d 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c  .  dlrDestroy(&l
31c6e 65 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72  eft);.  dlrDestr
31c6f 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c  oy(&right);.  dl
31c70 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  wDestroy(&writer
31c71 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69  );.}../* .** Thi
31c72 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
31c73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
31c74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
31c75 20 6f 66 20 70 68 72 61 73 65 20 61 6e 64 0a 2a   of phrase and.*
31c76 2a 20 4e 45 41 52 20 6d 61 74 63 68 69 6e 67 2e  * NEAR matching.
31c77 0a 2a 2a 0a 2a 2a 20 70 4c 65 66 74 20 61 6e 64  .**.** pLeft and
31c78 20 70 52 69 67 68 74 20 61 72 65 20 44 4c 52 65   pRight are DLRe
31c79 61 64 65 72 73 20 70 6f 73 69 74 69 6f 6e 65 64  aders positioned
31c7a 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 6f 63   to the same doc
31c7b 69 64 20 69 6e 0a 2a 2a 20 6c 69 73 74 73 20 6f  id in.** lists o
31c7c 66 20 74 79 70 65 20 44 4c 5f 50 4f 53 49 54 49  f type DL_POSITI
31c7d 4f 4e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ON. This functio
31c7e 6e 20 77 72 69 74 65 73 20 61 6e 20 65 6e 74 72  n writes an entr
31c7f 79 20 74 6f 20 74 68 65 0a 2a 2a 20 44 4c 57 72  y to the.** DLWr
31c80 69 74 65 72 20 70 4f 75 74 20 66 6f 72 20 65 61  iter pOut for ea
31c81 63 68 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70  ch position in p
31c82 52 69 67 68 74 20 74 68 61 74 20 69 73 20 6c 65  Right that is le
31c83 73 73 20 74 68 61 6e 0a 2a 2a 20 28 6e 4e 65 61  ss than.** (nNea
31c84 72 2b 31 29 20 67 72 65 61 74 65 72 20 28 62 75  r+1) greater (bu
31c85 74 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 6f  t not equal to o
31c86 72 20 73 6d 61 6c 6c 65 72 29 20 74 68 61 6e 20  r smaller) than 
31c87 61 20 70 6f 73 69 74 69 6f 6e 20 0a 2a 2a 20 69  a position .** i
31c88 6e 20 70 4c 65 66 74 2e 20 46 6f 72 20 65 78 61  n pLeft. For exa
31c89 6d 70 6c 65 2c 20 69 66 20 6e 4e 65 61 72 20 69  mple, if nNear i
31c8a 73 20 30 2c 20 61 6e 64 20 74 68 65 20 70 6f 73  s 0, and the pos
31c8b 69 74 69 6f 6e 73 20 63 6f 6e 74 61 69 6e 65 64  itions contained
31c8c 0a 2a 2a 20 62 79 20 70 4c 65 66 74 20 61 6e 64  .** by pLeft and
31c8d 20 70 52 69 67 68 74 20 61 72 65 3a 0a 2a 2a 0a   pRight are:.**.
31c8e 2a 2a 20 20 20 20 70 4c 65 66 74 3a 20 20 35 20  **    pLeft:  5 
31c8f 31 30 20 31 35 20 32 30 0a 2a 2a 20 20 20 20 70  10 15 20.**    p
31c90 52 69 67 68 74 3a 20 36 20 20 39 20 31 37 20 32  Right: 6  9 17 2
31c91 31 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65  1.**.** then the
31c92 20 64 6f 63 69 64 20 69 73 20 61 64 64 65 64 20   docid is added 
31c93 74 6f 20 70 4f 75 74 2e 20 49 66 20 70 4f 75 74  to pOut. If pOut
31c94 20 69 73 20 6f 66 20 74 79 70 65 20 44 4c 5f 50   is of type DL_P
31c95 4f 53 49 54 49 4f 4e 53 2c 0a 2a 2a 20 74 68 65  OSITIONS,.** the
31c96 6e 20 61 20 70 6f 73 69 74 69 6f 6e 69 64 73 20  n a positionids 
31c97 22 36 22 20 61 6e 64 20 22 32 31 22 20 61 72 65  "6" and "21" are
31c98 20 61 6c 73 6f 20 61 64 64 65 64 20 74 6f 20 70   also added to p
31c99 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f  Out..**.** If bo
31c9a 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 69  olean argument i
31c9b 73 53 61 76 65 4c 65 66 74 20 69 73 20 74 72 75  sSaveLeft is tru
31c9c 65 2c 20 74 68 65 6e 20 70 6f 73 69 74 69 6f 6e  e, then position
31c9d 69 64 73 20 61 72 65 20 63 6f 70 69 65 64 0a 2a  ids are copied.*
31c9e 2a 20 66 72 6f 6d 20 70 4c 65 66 74 20 69 6e 73  * from pLeft ins
31c9f 74 65 61 64 20 6f 66 20 70 52 69 67 68 74 2e 20  tead of pRight. 
31ca0 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61  In the example a
31ca1 62 6f 76 65 2c 20 74 68 65 20 70 6f 73 69 74 69  bove, the positi
31ca2 6f 6e 73 20 22 35 22 0a 2a 2a 20 61 6e 64 20 22  ons "5".** and "
31ca3 32 30 22 20 77 6f 75 6c 64 20 62 65 20 61 64 64  20" would be add
31ca4 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 22 36  ed instead of "6
31ca5 22 20 61 6e 64 20 22 32 31 22 2e 0a 2a 2f 0a 73  " and "21"..*/.s
31ca6 74 61 74 69 63 20 76 6f 69 64 20 70 6f 73 4c 69  tatic void posLi
31ca7 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 0a 20  stPhraseMerge(. 
31ca8 20 44 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74   DLReader *pLeft
31ca9 2c 20 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70  , .  DLReader *p
31caa 52 69 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65  Right,.  int nNe
31cab 61 72 2c 0a 20 20 69 6e 74 20 69 73 53 61 76 65  ar,.  int isSave
31cac 4c 65 66 74 2c 0a 20 20 44 4c 57 72 69 74 65 72  Left,.  DLWriter
31cad 20 2a 70 4f 75 74 0a 29 7b 0a 20 20 50 4c 52 65   *pOut.){.  PLRe
31cae 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ader left, right
31caf 3b 0a 20 20 50 4c 57 72 69 74 65 72 20 77 72 69  ;.  PLWriter wri
31cb0 74 65 72 3b 0a 20 20 69 6e 74 20 6d 61 74 63 68  ter;.  int match
31cb1 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
31cb2 20 64 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29   dlrDocid(pLeft)
31cb3 3d 3d 64 6c 72 44 6f 63 69 64 28 70 52 69 67 68  ==dlrDocid(pRigh
31cb4 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  t) );.  assert( 
31cb5 70 4f 75 74 2d 3e 69 54 79 70 65 21 3d 44 4c 5f  pOut->iType!=DL_
31cb6 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
31cb7 53 20 29 3b 0a 0a 20 20 70 6c 72 49 6e 69 74 28  S );..  plrInit(
31cb8 26 6c 65 66 74 2c 20 70 4c 65 66 74 29 3b 0a 20  &left, pLeft);. 
31cb9 20 70 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c   plrInit(&right,
31cba 20 70 52 69 67 68 74 29 3b 0a 0a 20 20 77 68 69   pRight);..  whi
31cbb 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c  le( !plrAtEnd(&l
31cbc 65 66 74 29 20 26 26 20 21 70 6c 72 41 74 45 6e  eft) && !plrAtEn
31cbd 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
31cbe 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26   if( plrColumn(&
31cbf 6c 65 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28  left)<plrColumn(
31cc0 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
31cc1 20 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b   plrStep(&left);
31cc2 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
31cc3 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 3e  lrColumn(&left)>
31cc4 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74  plrColumn(&right
31cc5 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74  ) ){.      plrSt
31cc6 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
31cc7 7d 65 6c 73 65 20 69 66 28 20 70 6c 72 50 6f 73  }else if( plrPos
31cc8 69 74 69 6f 6e 28 26 6c 65 66 74 29 3e 3d 70 6c  ition(&left)>=pl
31cc9 72 50 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74  rPosition(&right
31cca 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74  ) ){.      plrSt
31ccb 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
31ccc 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
31ccd 20 28 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 72   (plrPosition(&r
31cce 69 67 68 74 29 2d 70 6c 72 50 6f 73 69 74 69 6f  ight)-plrPositio
31ccf 6e 28 26 6c 65 66 74 29 29 3c 3d 28 6e 4e 65 61  n(&left))<=(nNea
31cd0 72 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  r+1) ){.        
31cd1 69 66 28 20 21 6d 61 74 63 68 20 29 7b 0a 20 20  if( !match ){.  
31cd2 20 20 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28          plwInit(
31cd3 26 77 72 69 74 65 72 2c 20 70 4f 75 74 2c 20 64  &writer, pOut, d
31cd4 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29 29 3b  lrDocid(pLeft));
31cd5 0a 20 20 20 20 20 20 20 20 20 20 6d 61 74 63 68  .          match
31cd6 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
31cd7 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 53          if( !isS
31cd8 61 76 65 4c 65 66 74 20 29 7b 0a 20 20 20 20 20  aveLeft ){.     
31cd9 20 20 20 20 20 70 6c 77 41 64 64 28 26 77 72 69       plwAdd(&wri
31cda 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26  ter, plrColumn(&
31cdb 72 69 67 68 74 29 2c 20 70 6c 72 50 6f 73 69 74  right), plrPosit
31cdc 69 6f 6e 28 26 72 69 67 68 74 29 2c 20 30 2c 20  ion(&right), 0, 
31cdd 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  0);.        }els
31cde 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77  e{.          plw
31cdf 41 64 64 28 26 77 72 69 74 65 72 2c 20 70 6c 72  Add(&writer, plr
31ce0 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 2c 20 70  Column(&left), p
31ce1 6c 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65 66 74  lrPosition(&left
31ce2 29 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  ), 0, 0);.      
31ce3 20 20 7d 0a 20 20 20 20 20 20 20 20 70 6c 72 53    }.        plrS
31ce4 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
31ce5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
31ce6 20 20 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29    plrStep(&left)
31ce7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
31ce8 20 20 7d 0a 0a 20 20 69 66 28 20 6d 61 74 63 68    }..  if( match
31ce9 20 29 7b 0a 20 20 20 20 70 6c 77 54 65 72 6d 69   ){.    plwTermi
31cea 6e 61 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20  nate(&writer);. 
31ceb 20 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77     plwDestroy(&w
31cec 72 69 74 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 70  riter);.  }..  p
31ced 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
31cee 3b 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26  ;.  plrDestroy(&
31cef 72 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  right);.}../*.**
31cf0 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c   Compare the val
31cf1 75 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ues pointed to b
31cf2 79 20 74 68 65 20 50 4c 52 65 61 64 65 72 73 20  y the PLReaders 
31cf3 70 61 73 73 65 64 20 61 73 20 61 72 67 75 6d 65  passed as argume
31cf4 6e 74 73 2e 20 0a 2a 2a 20 52 65 74 75 72 6e 20  nts. .** Return 
31cf5 2d 31 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  -1 if the value 
31cf6 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 4c  pointed to by pL
31cf7 65 66 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65  eft is considere
31cf8 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 74  d less than.** t
31cf9 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64  he value pointed
31cfa 20 74 6f 20 62 79 20 70 52 69 67 68 74 2c 20 2b   to by pRight, +
31cfb 31 20 69 66 20 69 74 20 69 73 20 63 6f 6e 73 69  1 if it is consi
31cfc 64 65 72 65 64 20 67 72 65 61 74 65 72 0a 2a 2a  dered greater.**
31cfd 20 74 68 61 6e 20 69 74 2c 20 6f 72 20 30 20 69   than it, or 0 i
31cfe 66 20 69 74 20 69 73 20 65 71 75 61 6c 2e 20 69  f it is equal. i
31cff 2e 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 2a  .e..**.**     (*
31d00 70 4c 65 66 74 20 2d 20 2a 70 52 69 67 68 74 29  pLeft - *pRight)
31d01 0a 2a 2a 0a 2a 2a 20 41 20 50 4c 52 65 61 64 65  .**.** A PLReade
31d02 72 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65  r that is in the
31d03 20 45 4f 46 20 63 6f 6e 64 69 74 69 6f 6e 20 69   EOF condition i
31d04 73 20 63 6f 6e 73 69 64 65 72 65 64 20 67 72 65  s considered gre
31d05 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 79  ater than.** any
31d06 20 6f 74 68 65 72 2e 20 49 66 20 6e 65 69 74 68   other. If neith
31d07 65 72 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69  er argument is i
31d08 6e 20 45 4f 46 20 73 74 61 74 65 2c 20 74 68 65  n EOF state, the
31d09 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
31d0a 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e 28 29 20  .** plrColumn() 
31d0b 69 73 20 75 73 65 64 2e 20 49 66 20 74 68 65 20  is used. If the 
31d0c 70 6c 72 43 6f 6c 75 6d 6e 28 29 20 76 61 6c 75  plrColumn() valu
31d0d 65 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68  es are equal, th
31d0e 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20  e.** comparison 
31d0f 69 73 20 6f 6e 20 74 68 65 20 62 61 73 69 73 20  is on the basis 
31d10 6f 66 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 29  of plrPosition()
31d11 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31d12 70 6c 72 43 6f 6d 70 61 72 65 28 50 4c 52 65 61  plrCompare(PLRea
31d13 64 65 72 20 2a 70 4c 65 66 74 2c 20 50 4c 52 65  der *pLeft, PLRe
31d14 61 64 65 72 20 2a 70 52 69 67 68 74 29 7b 0a 20  ader *pRight){. 
31d15 20 61 73 73 65 72 74 28 21 70 6c 72 41 74 45 6e   assert(!plrAtEn
31d16 64 28 70 4c 65 66 74 29 20 7c 7c 20 21 70 6c 72  d(pLeft) || !plr
31d17 41 74 45 6e 64 28 70 52 69 67 68 74 29 29 3b 0a  AtEnd(pRight));.
31d18 0a 20 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28  .  if( plrAtEnd(
31d19 70 52 69 67 68 74 29 20 7c 7c 20 70 6c 72 41 74  pRight) || plrAt
31d1a 45 6e 64 28 70 4c 65 66 74 29 20 29 7b 0a 20 20  End(pLeft) ){.  
31d1b 20 20 72 65 74 75 72 6e 20 28 70 6c 72 41 74 45    return (plrAtE
31d1c 6e 64 28 70 52 69 67 68 74 29 20 3f 20 2d 31 20  nd(pRight) ? -1 
31d1d 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  : 1);.  }.  if( 
31d1e 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29  plrColumn(pLeft)
31d1f 21 3d 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67  !=plrColumn(pRig
31d20 68 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ht) ){.    retur
31d21 6e 20 28 28 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c  n ((plrColumn(pL
31d22 65 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 70  eft)<plrColumn(p
31d23 52 69 67 68 74 29 29 20 3f 20 2d 31 20 3a 20 31  Right)) ? -1 : 1
31d24 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c 72  );.  }.  if( plr
31d25 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66 74 29 21  Position(pLeft)!
31d26 3d 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52 69  =plrPosition(pRi
31d27 67 68 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ght) ){.    retu
31d28 72 6e 20 28 28 70 6c 72 50 6f 73 69 74 69 6f 6e  rn ((plrPosition
31d29 28 70 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69 74  (pLeft)<plrPosit
31d2a 69 6f 6e 28 70 52 69 67 68 74 29 29 20 3f 20 2d  ion(pRight)) ? -
31d2b 31 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65  1 : 1);.  }.  re
31d2c 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 65  turn 0;.}../* We
31d2d 20 68 61 76 65 20 74 77 6f 20 64 6f 63 6c 69 73   have two doclis
31d2e 74 73 20 77 69 74 68 20 70 6f 73 69 74 69 6f 6e  ts with position
31d2f 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52  s:  pLeft and pR
31d30 69 67 68 74 2e 20 44 65 70 65 6e 64 69 6e 67 0a  ight. Depending.
31d31 2a 2a 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ** on the value 
31d32 6f 66 20 74 68 65 20 6e 4e 65 61 72 20 70 61 72  of the nNear par
31d33 61 6d 65 74 65 72 2c 20 70 65 72 66 6f 72 6d 20  ameter, perform 
31d34 65 69 74 68 65 72 20 61 20 70 68 72 61 73 65 0a  either a phrase.
31d35 2a 2a 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  ** intersection 
31d36 28 69 66 20 6e 4e 65 61 72 3d 3d 30 29 20 6f 72  (if nNear==0) or
31d37 20 61 20 4e 45 41 52 20 69 6e 74 65 72 73 65 63   a NEAR intersec
31d38 74 69 6f 6e 20 28 69 66 20 6e 4e 65 61 72 3e 30  tion (if nNear>0
31d39 29 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74  ).** and write t
31d3a 68 65 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20  he results into 
31d3b 70 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 68  pOut..**.** A ph
31d3c 72 61 73 65 20 69 6e 74 65 72 73 65 63 74 69 6f  rase intersectio
31d3d 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 74 77 6f  n means that two
31d3e 20 64 6f 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20   documents only 
31d3f 6d 61 74 63 68 0a 2a 2a 20 69 66 20 70 4c 65 66  match.** if pLef
31d40 74 2e 69 50 6f 73 2b 31 3d 3d 70 52 69 67 68 74  t.iPos+1==pRight
31d41 2e 69 50 6f 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e  .iPos..**.** A N
31d42 45 41 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e  EAR intersection
31d43 20 6d 65 61 6e 73 20 74 68 61 74 20 74 77 6f 20   means that two 
31d44 64 6f 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d  documents only m
31d45 61 74 63 68 20 69 66 20 0a 2a 2a 20 28 61 62 73  atch if .** (abs
31d46 28 70 4c 65 66 74 2e 69 50 6f 73 2d 70 52 69 67  (pLeft.iPos-pRig
31d47 68 74 2e 69 50 6f 73 29 3c 6e 4e 65 61 72 29 2e  ht.iPos)<nNear).
31d48 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 4e 45 41 52  .**.** If a NEAR
31d49 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 69 73   intersection is
31d4a 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e   requested, then
31d4b 20 74 68 65 20 6e 50 68 72 61 73 65 20 61 72 67   the nPhrase arg
31d4c 75 6d 65 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a 20  ument should.** 
31d4d 62 65 20 70 61 73 73 65 64 20 74 68 65 20 6e 75  be passed the nu
31d4e 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69  mber of tokens i
31d4f 6e 20 74 68 65 20 74 77 6f 20 6f 70 65 72 61 6e  n the two operan
31d50 64 73 20 74 6f 20 74 68 65 20 4e 45 41 52 20 6f  ds to the NEAR o
31d51 70 65 72 61 74 6f 72 0a 2a 2a 20 63 6f 6d 62 69  perator.** combi
31d52 6e 65 64 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ned. For example
31d53 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 51 75  :.**.**       Qu
31d54 65 72 79 20 73 79 6e 74 61 78 20 20 20 20 20 20  ery syntax      
31d55 20 20 20 20 20 20 20 20 20 6e 50 68 72 61 73 65           nPhrase
31d56 0a 2a 2a 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  .**      -------
31d57 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31d58 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
31d59 20 20 20 20 20 20 20 22 41 20 42 20 43 22 20 4e         "A B C" N
31d5a 45 41 52 20 22 44 20 45 22 20 20 20 20 20 20 20  EAR "D E"       
31d5b 20 20 35 0a 2a 2a 20 20 20 20 20 20 20 41 20 4e    5.**       A N
31d5c 45 41 52 20 42 20 20 20 20 20 20 20 20 20 20 20  EAR B           
31d5d 20 20 20 20 20 20 20 20 32 0a 2a 2a 0a 2a 2a 20          2.**.** 
31d5e 69 54 79 70 65 20 63 6f 6e 74 72 6f 6c 73 20 74  iType controls t
31d5f 68 65 20 74 79 70 65 20 6f 66 20 64 61 74 61 20  he type of data 
31d60 77 72 69 74 74 65 6e 20 74 6f 20 70 4f 75 74 2e  written to pOut.
31d61 20 20 49 66 20 69 54 79 70 65 20 69 73 0a 2a 2a    If iType is.**
31d62 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 74   DL_POSITIONS, t
31d63 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 61 72 65  he positions are
31d64 20 74 68 6f 73 65 20 66 72 6f 6d 20 70 52 69 67   those from pRig
31d65 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ht..*/.static vo
31d66 69 64 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65  id docListPhrase
31d67 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63  Merge(.  const c
31d68 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20  har *pLeft, int 
31d69 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63  nLeft,.  const c
31d6a 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74  har *pRight, int
31d6b 20 6e 52 69 67 68 74 2c 0a 20 20 69 6e 74 20 6e   nRight,.  int n
31d6c 4e 65 61 72 2c 20 20 20 20 20 20 20 20 20 20 20  Near,           
31d6d 20 2f 2a 20 30 20 66 6f 72 20 61 20 70 68 72 61   /* 0 for a phra
31d6e 73 65 20 6d 65 72 67 65 2c 20 6e 6f 6e 2d 7a 65  se merge, non-ze
31d6f 72 6f 20 66 6f 72 20 61 20 4e 45 41 52 20 6d 65  ro for a NEAR me
31d70 72 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 68  rge */.  int nPh
31d71 72 61 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  rase,          /
31d72 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  * Number of toke
31d73 6e 73 20 69 6e 20 6c 65 66 74 2b 72 69 67 68 74  ns in left+right
31d74 20 6f 70 65 72 61 6e 64 73 20 74 6f 20 4e 45 41   operands to NEA
31d75 52 20 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79  R */.  DocListTy
31d76 70 65 20 69 54 79 70 65 2c 20 20 20 20 2f 2a 20  pe iType,    /* 
31d77 54 79 70 65 20 6f 66 20 64 6f 63 6c 69 73 74 20  Type of doclist 
31d78 74 6f 20 77 72 69 74 65 20 74 6f 20 70 4f 75 74  to write to pOut
31d79 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
31d7a 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
31d7b 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
31d7c 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
31d7d 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
31d7e 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
31d7f 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
31d80 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
31d81 7c 7c 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72  || nRight==0 ) r
31d82 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74  eturn;..  assert
31d83 28 20 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49  ( iType!=DL_POSI
31d84 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b  TIONS_OFFSETS );
31d85 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66  ..  dlrInit(&lef
31d86 74 2c 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c  t, DL_POSITIONS,
31d87 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a   pLeft, nLeft);.
31d88 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74    dlrInit(&right
31d89 2c 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20  , DL_POSITIONS, 
31d8a 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
31d8b 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74  .  dlwInit(&writ
31d8c 65 72 2c 20 69 54 79 70 65 2c 20 70 4f 75 74 29  er, iType, pOut)
31d8d 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72  ;..  while( !dlr
31d8e 41 74 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20  AtEnd(&left) && 
31d8f 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74  !dlrAtEnd(&right
31d90 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72  ) ){.    if( dlr
31d91 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72  Docid(&left)<dlr
31d92 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b  Docid(&right) ){
31d93 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
31d94 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  left);.    }else
31d95 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72   if( dlrDocid(&r
31d96 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26  ight)<dlrDocid(&
31d97 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64  left) ){.      d
31d98 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
31d99 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
31d9a 20 69 66 28 20 6e 4e 65 61 72 3d 3d 30 20 29 7b   if( nNear==0 ){
31d9b 0a 20 20 20 20 20 20 20 20 70 6f 73 4c 69 73 74  .        posList
31d9c 50 68 72 61 73 65 4d 65 72 67 65 28 26 6c 65 66  PhraseMerge(&lef
31d9d 74 2c 20 26 72 69 67 68 74 2c 20 30 2c 20 30 2c  t, &right, 0, 0,
31d9e 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20   &writer);.     
31d9f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
31da0 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63 63  /* This case occ
31da1 75 72 73 20 77 68 65 6e 20 74 77 6f 20 74 65 72  urs when two ter
31da2 6d 73 20 28 73 69 6d 70 6c 65 20 74 65 72 6d 73  ms (simple terms
31da3 20 6f 72 20 70 68 72 61 73 65 73 29 20 61 72 65   or phrases) are
31da4 0a 20 20 20 20 20 20 20 20 20 2a 20 63 6f 6e 6e  .         * conn
31da5 65 63 74 65 64 20 62 79 20 61 20 4e 45 41 52 20  ected by a NEAR 
31da6 6f 70 65 72 61 74 6f 72 2c 20 73 70 61 6e 20 28  operator, span (
31da7 6e 4e 65 61 72 2b 31 29 2e 20 69 2e 65 2e 0a 20  nNear+1). i.e.. 
31da8 20 20 20 20 20 20 20 20 2a 0a 20 20 20 20 20 20          *.      
31da9 20 20 20 2a 20 20 20 20 20 27 22 74 65 72 72 69     *     '"terri
31daa 62 6c 65 20 63 6f 6d 70 61 6e 79 22 20 4e 45 41  ble company" NEA
31dab 52 20 77 69 64 67 65 74 27 0a 20 20 20 20 20 20  R widget'.      
31dac 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 61     */.        Da
31dad 74 61 42 75 66 66 65 72 20 6f 6e 65 20 3d 20 7b  taBuffer one = {
31dae 30 2c 20 30 2c 20 30 7d 3b 0a 20 20 20 20 20 20  0, 0, 0};.      
31daf 20 20 44 61 74 61 42 75 66 66 65 72 20 74 77 6f    DataBuffer two
31db0 20 3d 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20   = {0, 0, 0};.. 
31db1 20 20 20 20 20 20 20 44 4c 57 72 69 74 65 72 20         DLWriter 
31db2 64 6c 77 72 69 74 65 72 32 3b 0a 20 20 20 20 20  dlwriter2;.     
31db3 20 20 20 44 4c 52 65 61 64 65 72 20 64 72 31 20     DLReader dr1 
31db4 3d 20 7b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  = {0, 0, 0, 0, 0
31db5 7d 3b 20 0a 20 20 20 20 20 20 20 20 44 4c 52 65  }; .        DLRe
31db6 61 64 65 72 20 64 72 32 20 3d 20 7b 30 2c 20 30  ader dr2 = {0, 0
31db7 2c 20 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20  , 0, 0, 0};..   
31db8 20 20 20 20 20 64 6c 77 49 6e 69 74 28 26 64 6c       dlwInit(&dl
31db9 77 72 69 74 65 72 32 2c 20 69 54 79 70 65 2c 20  writer2, iType, 
31dba 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 70  &one);.        p
31dbb 6f 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67  osListPhraseMerg
31dbc 65 28 26 72 69 67 68 74 2c 20 26 6c 65 66 74 2c  e(&right, &left,
31dbd 20 6e 4e 65 61 72 2d 33 2b 6e 50 68 72 61 73 65   nNear-3+nPhrase
31dbe 2c 20 31 2c 20 26 64 6c 77 72 69 74 65 72 32 29  , 1, &dlwriter2)
31dbf 3b 0a 20 20 20 20 20 20 20 20 64 6c 77 49 6e 69  ;.        dlwIni
31dc0 74 28 26 64 6c 77 72 69 74 65 72 32 2c 20 69 54  t(&dlwriter2, iT
31dc1 79 70 65 2c 20 26 74 77 6f 29 3b 0a 20 20 20 20  ype, &two);.    
31dc2 20 20 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73      posListPhras
31dc3 65 4d 65 72 67 65 28 26 6c 65 66 74 2c 20 26 72  eMerge(&left, &r
31dc4 69 67 68 74 2c 20 6e 4e 65 61 72 2d 31 2c 20 30  ight, nNear-1, 0
31dc5 2c 20 26 64 6c 77 72 69 74 65 72 32 29 3b 0a 0a  , &dlwriter2);..
31dc6 20 20 20 20 20 20 20 20 69 66 28 20 6f 6e 65 2e          if( one.
31dc7 6e 44 61 74 61 29 20 64 6c 72 49 6e 69 74 28 26  nData) dlrInit(&
31dc8 64 72 31 2c 20 69 54 79 70 65 2c 20 6f 6e 65 2e  dr1, iType, one.
31dc9 70 44 61 74 61 2c 20 6f 6e 65 2e 6e 44 61 74 61  pData, one.nData
31dca 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 74  );.        if( t
31dcb 77 6f 2e 6e 44 61 74 61 29 20 64 6c 72 49 6e 69  wo.nData) dlrIni
31dcc 74 28 26 64 72 32 2c 20 69 54 79 70 65 2c 20 74  t(&dr2, iType, t
31dcd 77 6f 2e 70 44 61 74 61 2c 20 74 77 6f 2e 6e 44  wo.pData, two.nD
31dce 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 20 20 69  ata);..        i
31dcf 66 28 20 21 64 6c 72 41 74 45 6e 64 28 26 64 72  f( !dlrAtEnd(&dr
31dd0 31 29 20 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28  1) || !dlrAtEnd(
31dd1 26 64 72 32 29 20 29 7b 0a 20 20 20 20 20 20 20  &dr2) ){.       
31dd2 20 20 20 50 4c 52 65 61 64 65 72 20 70 72 31 20     PLReader pr1 
31dd3 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 20 20 20  = {0};.         
31dd4 20 50 4c 52 65 61 64 65 72 20 70 72 32 20 3d 20   PLReader pr2 = 
31dd5 7b 30 7d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  {0};..          
31dd6 50 4c 57 72 69 74 65 72 20 70 6c 77 72 69 74 65  PLWriter plwrite
31dd7 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77  r;.          plw
31dd8 49 6e 69 74 28 26 70 6c 77 72 69 74 65 72 2c 20  Init(&plwriter, 
31dd9 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69  &writer, dlrDoci
31dda 64 28 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29  d(dlrAtEnd(&dr1)
31ddb 3f 26 64 72 32 3a 26 64 72 31 29 29 3b 0a 0a 20  ?&dr2:&dr1));.. 
31ddc 20 20 20 20 20 20 20 20 20 69 66 28 20 6f 6e 65           if( one
31ddd 2e 6e 44 61 74 61 20 29 20 70 6c 72 49 6e 69 74  .nData ) plrInit
31dde 28 26 70 72 31 2c 20 26 64 72 31 29 3b 0a 20 20  (&pr1, &dr1);.  
31ddf 20 20 20 20 20 20 20 20 69 66 28 20 74 77 6f 2e          if( two.
31de0 6e 44 61 74 61 20 29 20 70 6c 72 49 6e 69 74 28  nData ) plrInit(
31de1 26 70 72 32 2c 20 26 64 72 32 29 3b 0a 20 20 20  &pr2, &dr2);.   
31de2 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 70         while( !p
31de3 6c 72 41 74 45 6e 64 28 26 70 72 31 29 20 7c 7c  lrAtEnd(&pr1) ||
31de4 20 21 70 6c 72 41 74 45 6e 64 28 26 70 72 32 29   !plrAtEnd(&pr2)
31de5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
31de6 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 70  int iCompare = p
31de7 6c 72 43 6f 6d 70 61 72 65 28 26 70 72 31 2c 20  lrCompare(&pr1, 
31de8 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20  &pr2);.         
31de9 20 20 20 73 77 69 74 63 68 28 20 69 43 6f 6d 70     switch( iComp
31dea 61 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  are ){.         
31deb 20 20 20 20 20 63 61 73 65 20 2d 31 3a 0a 20 20       case -1:.  
31dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
31ded 77 43 6f 70 79 28 26 70 6c 77 72 69 74 65 72 2c  wCopy(&plwriter,
31dee 20 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20   &pr1);.        
31def 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28          plrStep(
31df0 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20  &pr1);.         
31df1 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31df2 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
31df3 20 31 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20   1:.            
31df4 20 20 20 20 70 6c 77 43 6f 70 79 28 26 70 6c 77      plwCopy(&plw
31df5 72 69 74 65 72 2c 20 26 70 72 32 29 3b 0a 20 20  riter, &pr2);.  
31df6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
31df7 72 53 74 65 70 28 26 70 72 32 29 3b 0a 20 20 20  rStep(&pr2);.   
31df8 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
31df9 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
31dfa 20 20 63 61 73 65 20 30 3a 0a 20 20 20 20 20 20    case 0:.      
31dfb 20 20 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70            plwCop
31dfc 79 28 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72  y(&plwriter, &pr
31dfd 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
31dfe 20 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 31      plrStep(&pr1
31dff 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
31e00 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 32 29     plrStep(&pr2)
31e01 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
31e02 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
31e03 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
31e04 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77   }.          plw
31e05 54 65 72 6d 69 6e 61 74 65 28 26 70 6c 77 72 69  Terminate(&plwri
31e06 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ter);.        }.
31e07 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
31e08 65 72 44 65 73 74 72 6f 79 28 26 6f 6e 65 29 3b  erDestroy(&one);
31e09 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
31e0a 66 65 72 44 65 73 74 72 6f 79 28 26 74 77 6f 29  ferDestroy(&two)
31e0b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
31e0c 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a  dlrStep(&left);.
31e0d 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72        dlrStep(&r
31e0e 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ight);.    }.  }
31e0f 0a 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ..  dlrDestroy(&
31e10 6c 65 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74  left);.  dlrDest
31e11 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64  roy(&right);.  d
31e12 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  lwDestroy(&write
31e13 72 29 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76  r);.}../* We hav
31e14 65 20 74 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20  e two DL_DOCIDS 
31e15 64 6f 63 6c 69 73 74 73 3a 20 20 70 4c 65 66 74  doclists:  pLeft
31e16 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20   and pRight..** 
31e17 57 72 69 74 65 20 74 68 65 20 69 6e 74 65 72 73  Write the inters
31e18 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  ection of these 
31e19 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 69 6e 74  two doclists int
31e1a 6f 20 70 4f 75 74 20 61 73 20 61 0a 2a 2a 20 44  o pOut as a.** D
31e1b 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74  L_DOCIDS doclist
31e1c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
31e1d 20 64 6f 63 4c 69 73 74 41 6e 64 4d 65 72 67 65   docListAndMerge
31e1e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
31e1f 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
31e20 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
31e21 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
31e22 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72  ht,.  DataBuffer
31e23 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
31e24 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
31e25 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
31e26 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
31e27 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
31e28 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
31e29 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
31e2a 7c 7c 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72  || nRight==0 ) r
31e2b 65 74 75 72 6e 3b 0a 0a 20 20 64 6c 72 49 6e 69  eturn;..  dlrIni
31e2c 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49  t(&left, DL_DOCI
31e2d 44 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74  DS, pLeft, nLeft
31e2e 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69  );.  dlrInit(&ri
31e2f 67 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20  ght, DL_DOCIDS, 
31e30 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
31e31 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74  .  dlwInit(&writ
31e32 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70  er, DL_DOCIDS, p
31e33 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
31e34 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
31e35 20 26 26 20 21 64 6c 72 41 74 45 6e 64 28 26 72   && !dlrAtEnd(&r
31e36 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28  ight) ){.    if(
31e37 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
31e38 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74  <dlrDocid(&right
31e39 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74  ) ){.      dlrSt
31e3a 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d  ep(&left);.    }
31e3b 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69  else if( dlrDoci
31e3c 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63  d(&right)<dlrDoc
31e3d 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20  id(&left) ){.   
31e3e 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
31e3f 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
31e40 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69       dlwAdd(&wri
31e41 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c  ter, dlrDocid(&l
31e42 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  eft));.      dlr
31e43 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
31e44 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
31e45 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
31e46 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66   dlrDestroy(&lef
31e47 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79  t);.  dlrDestroy
31e48 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44  (&right);.  dlwD
31e49 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
31e4a 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74  .}../* We have t
31e4b 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63  wo DL_DOCIDS doc
31e4c 6c 69 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e  lists:  pLeft an
31e4d 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69  d pRight..** Wri
31e4e 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  te the union of 
31e4f 74 68 65 73 65 20 74 77 6f 20 64 6f 63 6c 69 73  these two doclis
31e50 74 73 20 69 6e 74 6f 20 70 4f 75 74 20 61 73 20  ts into pOut as 
31e51 61 0a 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 20 64  a.** DL_DOCIDS d
31e52 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  oclist..*/.stati
31e53 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 4f 72  c void docListOr
31e54 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63  Merge(.  const c
31e55 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20  har *pLeft, int 
31e56 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63  nLeft,.  const c
31e57 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74  har *pRight, int
31e58 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42   nRight,.  DataB
31e59 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20  uffer *pOut     
31e5a 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
31e5b 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68  mbined doclist h
31e5c 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65  ere */.){.  DLRe
31e5d 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ader left, right
31e5e 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69  ;.  DLWriter wri
31e5f 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66  ter;..  if( nLef
31e60 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  t==0 ){.    if( 
31e61 6e 52 69 67 68 74 21 3d 30 20 29 20 64 61 74 61  nRight!=0 ) data
31e62 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75  BufferAppend(pOu
31e63 74 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68  t, pRight, nRigh
31e64 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
31e65 20 20 7d 0a 20 20 69 66 28 20 6e 52 69 67 68 74    }.  if( nRight
31e66 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42  ==0 ){.    dataB
31e67 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74  ufferAppend(pOut
31e68 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
31e69 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
31e6a 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66  ..  dlrInit(&lef
31e6b 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4c  t, DL_DOCIDS, pL
31e6c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64  eft, nLeft);.  d
31e6d 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44  lrInit(&right, D
31e6e 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69 67 68 74  L_DOCIDS, pRight
31e6f 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77  , nRight);.  dlw
31e70 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c  Init(&writer, DL
31e71 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74 29 3b 0a  _DOCIDS, pOut);.
31e72 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74  .  while( !dlrAt
31e73 45 6e 64 28 26 6c 65 66 74 29 20 7c 7c 20 21 64  End(&left) || !d
31e74 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
31e75 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74  ){.    if( dlrAt
31e76 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  End(&right) ){. 
31e77 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69       dlwAdd(&wri
31e78 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c  ter, dlrDocid(&l
31e79 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  eft));.      dlr
31e7a 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
31e7b 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 41 74   }else if( dlrAt
31e7c 45 6e 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20  End(&left) ){.  
31e7d 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74      dlwAdd(&writ
31e7e 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69  er, dlrDocid(&ri
31e7f 67 68 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  ght));.      dlr
31e80 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
31e81 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44    }else if( dlrD
31e82 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44  ocid(&left)<dlrD
31e83 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a  ocid(&right) ){.
31e84 20 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72        dlwAdd(&wr
31e85 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26  iter, dlrDocid(&
31e86 6c 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c  left));.      dl
31e87 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
31e88 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44    }else if( dlrD
31e89 6f 63 69 64 28 26 72 69 67 68 74 29 3c 64 6c 72  ocid(&right)<dlr
31e8a 44 6f 63 69 64 28 26 6c 65 66 74 29 20 29 7b 0a  Docid(&left) ){.
31e8b 20 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72        dlwAdd(&wr
31e8c 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26  iter, dlrDocid(&
31e8d 72 69 67 68 74 29 29 3b 0a 20 20 20 20 20 20 64  right));.      d
31e8e 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
31e8f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
31e90 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
31e91 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
31e92 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
31e93 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64  (&left);.      d
31e94 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
31e95 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72      }.  }..  dlr
31e96 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a  Destroy(&left);.
31e97 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69    dlrDestroy(&ri
31e98 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72  ght);.  dlwDestr
31e99 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a  oy(&writer);.}..
31e9a 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f 20 44  /* We have two D
31e9b 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74  L_DOCIDS doclist
31e9c 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52  s:  pLeft and pR
31e9d 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 69  ight..** Write i
31e9e 6e 74 6f 20 70 4f 75 74 20 61 73 20 44 4c 5f 44  nto pOut as DL_D
31e9f 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20 63 6f  OCIDS doclist co
31ea0 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 64 6f 63  ntaining all doc
31ea1 75 6d 65 6e 74 73 20 74 68 61 74 0a 2a 2a 20 6f  uments that.** o
31ea2 63 63 75 72 20 69 6e 20 70 4c 65 66 74 20 62 75  ccur in pLeft bu
31ea3 74 20 6e 6f 74 20 69 6e 20 70 52 69 67 68 74 2e  t not in pRight.
31ea4 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
31ea5 64 6f 63 4c 69 73 74 45 78 63 65 70 74 4d 65 72  docListExceptMer
31ea6 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ge(.  const char
31ea7 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65   *pLeft, int nLe
31ea8 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ft,.  const char
31ea9 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52   *pRight, int nR
31eaa 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66  ight,.  DataBuff
31eab 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a  er *pOut      /*
31eac 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69   Write the combi
31ead 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65  ned doclist here
31eae 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65   */.){.  DLReade
31eaf 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20  r left, right;. 
31eb0 20 44 4c 57 72 69 74 65 72 20 77 72 69 74 65 72   DLWriter writer
31eb1 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d  ;..  if( nLeft==
31eb2 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
31eb3 28 20 6e 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20  ( nRight==0 ){. 
31eb4 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
31eb5 65 6e 64 28 70 4f 75 74 2c 20 70 4c 65 66 74 2c  end(pOut, pLeft,
31eb6 20 6e 4c 65 66 74 29 3b 0a 20 20 20 20 72 65 74   nLeft);.    ret
31eb7 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 49  urn;.  }..  dlrI
31eb8 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f  nit(&left, DL_DO
31eb9 43 49 44 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65  CIDS, pLeft, nLe
31eba 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26  ft);.  dlrInit(&
31ebb 72 69 67 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53  right, DL_DOCIDS
31ebc 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74  , pRight, nRight
31ebd 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72  );.  dlwInit(&wr
31ebe 69 74 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c  iter, DL_DOCIDS,
31ebf 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65   pOut);..  while
31ec0 28 20 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66  ( !dlrAtEnd(&lef
31ec1 74 29 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28  t) ){.    while(
31ec2 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68   !dlrAtEnd(&righ
31ec3 74 29 20 26 26 20 64 6c 72 44 6f 63 69 64 28 26  t) && dlrDocid(&
31ec4 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28  right)<dlrDocid(
31ec5 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20  &left) ){.      
31ec6 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  dlrStep(&right);
31ec7 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
31ec8 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
31ec9 7c 7c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  || dlrDocid(&lef
31eca 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67  t)<dlrDocid(&rig
31ecb 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77  ht) ){.      dlw
31ecc 41 64 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72  Add(&writer, dlr
31ecd 44 6f 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20  Docid(&left));. 
31ece 20 20 20 7d 0a 20 20 20 20 64 6c 72 53 74 65 70     }.    dlrStep
31ecf 28 26 6c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20  (&left);.  }..  
31ed0 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74  dlrDestroy(&left
31ed1 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28  );.  dlrDestroy(
31ed2 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65  &right);.  dlwDe
31ed3 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
31ed4 7d 0a 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  }..static char *
31ed5 73 74 72 69 6e 67 5f 64 75 70 5f 6e 28 63 6f 6e  string_dup_n(con
31ed6 73 74 20 63 68 61 72 20 2a 73 2c 20 69 6e 74 20  st char *s, int 
31ed7 6e 29 7b 0a 20 20 63 68 61 72 20 2a 73 74 72 20  n){.  char *str 
31ed8 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
31ed9 28 6e 20 2b 20 31 29 3b 0a 20 20 6d 65 6d 63 70  (n + 1);.  memcp
31eda 79 28 73 74 72 2c 20 73 2c 20 6e 29 3b 0a 20 20  y(str, s, n);.  
31edb 73 74 72 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20  str[n] = '\0';. 
31edc 20 72 65 74 75 72 6e 20 73 74 72 3b 0a 7d 0a 0a   return str;.}..
31edd 2f 2a 20 44 75 70 6c 69 63 61 74 65 20 61 20 73  /* Duplicate a s
31ede 74 72 69 6e 67 3b 20 74 68 65 20 63 61 6c 6c 65  tring; the calle
31edf 72 20 6d 75 73 74 20 66 72 65 65 28 29 20 74 68  r must free() th
31ee0 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
31ee1 67 2e 0a 20 2a 20 28 57 65 20 64 6f 6e 27 74 20  g.. * (We don't 
31ee2 75 73 65 20 73 74 72 64 75 70 28 29 20 73 69 6e  use strdup() sin
31ee3 63 65 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72  ce it is not par
31ee4 74 20 6f 66 20 74 68 65 20 73 74 61 6e 64 61 72  t of the standar
31ee5 64 20 43 20 6c 69 62 72 61 72 79 20 61 6e 64 0a  d C library and.
31ee6 20 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 76   * may not be av
31ee7 61 69 6c 61 62 6c 65 20 65 76 65 72 79 77 68 65  ailable everywhe
31ee8 72 65 2e 29 20 2a 2f 0a 73 74 61 74 69 63 20 63  re.) */.static c
31ee9 68 61 72 20 2a 73 74 72 69 6e 67 5f 64 75 70 28  har *string_dup(
31eea 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 29 7b 0a  const char *s){.
31eeb 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 5f    return string_
31eec 64 75 70 5f 6e 28 73 2c 20 73 74 72 6c 65 6e 28  dup_n(s, strlen(
31eed 73 29 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 6d 61  s));.}../* Forma
31eee 74 20 61 20 73 74 72 69 6e 67 2c 20 72 65 70 6c  t a string, repl
31eef 61 63 69 6e 67 20 65 61 63 68 20 6f 63 63 75 72  acing each occur
31ef0 72 65 6e 63 65 20 6f 66 20 74 68 65 20 25 20 63  rence of the % c
31ef1 68 61 72 61 63 74 65 72 20 77 69 74 68 0a 20 2a  haracter with. *
31ef2 20 7a 44 62 2e 7a 4e 61 6d 65 2e 20 20 54 68 69   zDb.zName.  Thi
31ef3 73 20 6d 61 79 20 62 65 20 6d 6f 72 65 20 63 6f  s may be more co
31ef4 6e 76 65 6e 69 65 6e 74 20 74 68 61 6e 20 73 71  nvenient than sq
31ef5 6c 69 74 65 5f 6d 70 72 69 6e 74 66 28 29 0a 20  lite_mprintf(). 
31ef6 2a 20 77 68 65 6e 20 6f 6e 65 20 73 74 72 69 6e  * when one strin
31ef7 67 20 69 73 20 75 73 65 64 20 72 65 70 65 61 74  g is used repeat
31ef8 65 64 6c 79 20 69 6e 20 61 20 66 6f 72 6d 61 74  edly in a format
31ef9 20 73 74 72 69 6e 67 2e 0a 20 2a 20 54 68 65 20   string.. * The 
31efa 63 61 6c 6c 65 72 20 6d 75 73 74 20 66 72 65 65  caller must free
31efb 28 29 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  () the returned 
31efc 73 74 72 69 6e 67 2e 20 2a 2f 0a 73 74 61 74 69  string. */.stati
31efd 63 20 63 68 61 72 20 2a 73 74 72 69 6e 67 5f 66  c char *string_f
31efe 6f 72 6d 61 74 28 63 6f 6e 73 74 20 63 68 61 72  ormat(const char
31eff 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 20 20 20   *zFormat,.     
31f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f01 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
31f02 20 2a 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68 61   *zDb, const cha
31f03 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 63 6f 6e  r *zName){.  con
31f04 73 74 20 63 68 61 72 20 2a 70 3b 0a 20 20 73 69  st char *p;.  si
31f05 7a 65 5f 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20  ze_t len = 0;.  
31f06 73 69 7a 65 5f 74 20 6e 44 62 20 3d 20 73 74 72  size_t nDb = str
31f07 6c 65 6e 28 7a 44 62 29 3b 0a 20 20 73 69 7a 65  len(zDb);.  size
31f08 5f 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65  _t nName = strle
31f09 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 73 69 7a 65  n(zName);.  size
31f0a 5f 74 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d  _t nFullTableNam
31f0b 65 20 3d 20 6e 44 62 2b 31 2b 6e 4e 61 6d 65 3b  e = nDb+1+nName;
31f0c 0a 20 20 63 68 61 72 20 2a 72 65 73 75 6c 74 3b  .  char *result;
31f0d 0a 20 20 63 68 61 72 20 2a 72 3b 0a 0a 20 20 2f  .  char *r;..  /
31f0e 2a 20 66 69 72 73 74 20 63 6f 6d 70 75 74 65 20  * first compute 
31f0f 6c 65 6e 67 74 68 20 6e 65 65 64 65 64 20 2a 2f  length needed */
31f10 0a 20 20 66 6f 72 28 70 20 3d 20 7a 46 6f 72 6d  .  for(p = zForm
31f11 61 74 20 3b 20 2a 70 20 3b 20 2b 2b 70 29 7b 0a  at ; *p ; ++p){.
31f12 20 20 20 20 6c 65 6e 20 2b 3d 20 28 2a 70 3d 3d      len += (*p==
31f13 27 25 27 20 3f 20 6e 46 75 6c 6c 54 61 62 6c 65  '%' ? nFullTable
31f14 4e 61 6d 65 20 3a 20 31 29 3b 0a 20 20 7d 0a 20  Name : 1);.  }. 
31f15 20 6c 65 6e 20 2b 3d 20 31 3b 20 20 2f 2a 20 66   len += 1;  /* f
31f16 6f 72 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74  or null terminat
31f17 6f 72 20 2a 2f 0a 0a 20 20 72 20 3d 20 72 65 73  or */..  r = res
31f18 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ult = sqlite3_ma
31f19 6c 6c 6f 63 28 6c 65 6e 29 3b 0a 20 20 66 6f 72  lloc(len);.  for
31f1a 28 70 20 3d 20 7a 46 6f 72 6d 61 74 3b 20 2a 70  (p = zFormat; *p
31f1b 3b 20 2b 2b 70 29 7b 0a 20 20 20 20 69 66 28 20  ; ++p){.    if( 
31f1c 2a 70 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20  *p=='%' ){.     
31f1d 20 6d 65 6d 63 70 79 28 72 2c 20 7a 44 62 2c 20   memcpy(r, zDb, 
31f1e 6e 44 62 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d  nDb);.      r +=
31f1f 20 6e 44 62 3b 0a 20 20 20 20 20 20 2a 72 2b 2b   nDb;.      *r++
31f20 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 6d 65   = '.';.      me
31f21 6d 63 70 79 28 72 2c 20 7a 4e 61 6d 65 2c 20 6e  mcpy(r, zName, n
31f22 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 20 2b  Name);.      r +
31f23 3d 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 65  = nName;.    } e
31f24 6c 73 65 20 7b 0a 20 20 20 20 20 20 2a 72 2b 2b  lse {.      *r++
31f25 20 3d 20 2a 70 3b 0a 20 20 20 20 7d 0a 20 20 7d   = *p;.    }.  }
31f26 0a 20 20 2a 72 2b 2b 20 3d 20 27 5c 30 27 3b 0a  .  *r++ = '\0';.
31f27 20 20 61 73 73 65 72 74 28 20 72 20 3d 3d 20 72    assert( r == r
31f28 65 73 75 6c 74 20 2b 20 6c 65 6e 20 29 3b 0a 20  esult + len );. 
31f29 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 3b 0a   return result;.
31f2a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  }..static int sq
31f2b 6c 5f 65 78 65 63 28 73 71 6c 69 74 65 33 20 2a  l_exec(sqlite3 *
31f2c 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
31f2d 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  zDb, const char 
31f2e 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  *zName,.        
31f2f 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
31f30 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29  t char *zFormat)
31f31 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61  {.  char *zComma
31f32 6e 64 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d  nd = string_form
31f33 61 74 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c  at(zFormat, zDb,
31f34 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72   zName);.  int r
31f35 63 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  c;.  FTSTRACE(("
31f36 46 54 53 33 20 73 71 6c 3a 20 25 73 5c 6e 22 2c  FTS3 sql: %s\n",
31f37 20 7a 43 6f 6d 6d 61 6e 64 29 29 3b 0a 20 20 72   zCommand));.  r
31f38 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
31f39 28 64 62 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20 4e  (db, zCommand, N
31f3a 55 4c 4c 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a 20  ULL, 0, NULL);. 
31f3b 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
31f3c 6f 6d 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72  ommand);.  retur
31f3d 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
31f3e 69 6e 74 20 73 71 6c 5f 70 72 65 70 61 72 65 28  int sql_prepare(
31f3f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
31f40 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 63 6f  st char *zDb, co
31f41 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
31f42 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
31f43 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
31f44 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 63  stmt **ppStmt, c
31f45 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
31f46 61 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f  at){.  char *zCo
31f47 6d 6d 61 6e 64 20 3d 20 73 74 72 69 6e 67 5f 66  mmand = string_f
31f48 6f 72 6d 61 74 28 7a 46 6f 72 6d 61 74 2c 20 7a  ormat(zFormat, z
31f49 44 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e  Db, zName);.  in
31f4a 74 20 72 63 3b 0a 20 20 46 54 53 54 52 41 43 45  t rc;.  FTSTRACE
31f4b 28 28 22 46 54 53 33 20 70 72 65 70 61 72 65 3a  (("FTS3 prepare:
31f4c 20 25 73 5c 6e 22 2c 20 7a 43 6f 6d 6d 61 6e 64   %s\n", zCommand
31f4d 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ));.  rc = sqlit
31f4e 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
31f4f 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20 2d 31 2c 20  , zCommand, -1, 
31f50 70 70 53 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20  ppStmt, NULL);. 
31f51 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
31f52 6f 6d 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72  ommand);.  retur
31f53 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 65 6e 64 20  n rc;.}../* end 
31f54 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e  utility function
31f55 73 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  s */../* Forward
31f56 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 74 79   reference */.ty
31f57 70 65 64 65 66 20 73 74 72 75 63 74 20 66 75 6c  pedef struct ful
31f58 6c 74 65 78 74 5f 76 74 61 62 20 66 75 6c 6c 74  ltext_vtab fullt
31f59 65 78 74 5f 76 74 61 62 3b 0a 0a 2f 2a 0a 2a 2a  ext_vtab;../*.**
31f5a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
31f5b 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
31f5c 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72  ructure keeps tr
31f5d 61 63 6b 20 6f 66 20 67 65 6e 65 72 61 74 65 64  ack of generated
31f5e 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 2d 77 6f 72  .** matching-wor
31f5f 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61  d offset informa
31f60 74 69 6f 6e 20 61 6e 64 20 73 6e 69 70 70 65 74  tion and snippet
31f61 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
31f62 72 75 63 74 20 53 6e 69 70 70 65 74 20 7b 0a 20  ruct Snippet {. 
31f63 20 69 6e 74 20 6e 4d 61 74 63 68 3b 20 20 20 20   int nMatch;    
31f64 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
31f65 20 6f 66 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20   of matches */. 
31f66 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   int nAlloc;    
31f67 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c 6f 63 61   /* Space alloca
31f68 74 65 64 20 66 6f 72 20 61 4d 61 74 63 68 5b 5d  ted for aMatch[]
31f69 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 6e 69   */.  struct sni
31f6a 70 70 65 74 4d 61 74 63 68 20 7b 20 2f 2a 20 4f  ppetMatch { /* O
31f6b 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63  ne entry for eac
31f6c 68 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 20  h matching term 
31f6d 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 6e 53 74  */.    char snSt
31f6e 61 74 75 73 3b 20 20 20 20 20 20 20 2f 2a 20 53  atus;       /* S
31f6f 74 61 74 75 73 20 66 6c 61 67 20 66 6f 72 20 75  tatus flag for u
31f70 73 65 20 77 68 69 6c 65 20 63 6f 6e 73 74 72 75  se while constru
31f71 63 74 69 6e 67 20 73 6e 69 70 70 65 74 73 20 2a  cting snippets *
31f72 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20  /.    short int 
31f73 69 43 6f 6c 3b 20 20 20 20 20 20 2f 2a 20 54 68  iCol;      /* Th
31f74 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 63 6f  e column that co
31f75 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 74 63 68  ntains the match
31f76 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e   */.    short in
31f77 74 20 69 54 65 72 6d 3b 20 20 20 20 20 2f 2a 20  t iTerm;     /* 
31f78 54 68 65 20 69 6e 64 65 78 20 69 6e 20 51 75 65  The index in Que
31f79 72 79 2e 70 54 65 72 6d 73 5b 5d 20 6f 66 20 74  ry.pTerms[] of t
31f7a 68 65 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d  he matching term
31f7b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 6f 6b   */.    int iTok
31f7c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  en;          /* 
31f7d 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  The index of the
31f7e 20 6d 61 74 63 68 69 6e 67 20 64 6f 63 75 6d 65   matching docume
31f7f 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  nt token */.    
31f80 73 68 6f 72 74 20 69 6e 74 20 6e 42 79 74 65 3b  short int nByte;
31f81 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
31f82 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 74  f bytes in the t
31f83 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  erm */.    int i
31f84 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20  Start;          
31f85 2f 2a 20 54 68 65 20 6f 66 66 73 65 74 20 74 6f  /* The offset to
31f86 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61   the first chara
31f87 63 74 65 72 20 6f 66 20 74 68 65 20 74 65 72 6d  cter of the term
31f88 20 2a 2f 0a 20 20 7d 20 2a 61 4d 61 74 63 68 3b   */.  } *aMatch;
31f89 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 73 20        /* Points 
31f8a 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  to space obtaine
31f8b 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 2a 2f  d from malloc */
31f8c 0a 20 20 63 68 61 72 20 2a 7a 4f 66 66 73 65 74  .  char *zOffset
31f8d 3b 20 20 2f 2a 20 54 65 78 74 20 72 65 6e 64 65  ;  /* Text rende
31f8e 72 69 6e 67 20 6f 66 20 61 4d 61 74 63 68 5b 5d  ring of aMatch[]
31f8f 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66 66 73 65   */.  int nOffse
31f90 74 3b 20 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28  t;    /* strlen(
31f91 7a 4f 66 66 73 65 74 29 20 2a 2f 0a 20 20 63 68  zOffset) */.  ch
31f92 61 72 20 2a 7a 53 6e 69 70 70 65 74 3b 20 2f 2a  ar *zSnippet; /*
31f93 20 53 6e 69 70 70 65 74 20 74 65 78 74 20 2a 2f   Snippet text */
31f94 0a 20 20 69 6e 74 20 6e 53 6e 69 70 70 65 74 3b  .  int nSnippet;
31f95 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28 7a 53 6e     /* strlen(zSn
31f96 69 70 70 65 74 29 20 2a 2f 0a 7d 20 53 6e 69 70  ippet) */.} Snip
31f97 70 65 74 3b 0a 0a 0a 74 79 70 65 64 65 66 20 65  pet;...typedef e
31f98 6e 75 6d 20 51 75 65 72 79 54 79 70 65 20 7b 0a  num QueryType {.
31f99 20 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43 2c    QUERY_GENERIC,
31f9a 20 20 20 2f 2a 20 74 61 62 6c 65 20 73 63 61 6e     /* table scan
31f9b 20 2a 2f 0a 20 20 51 55 45 52 59 5f 44 4f 43 49   */.  QUERY_DOCI
31f9c 44 2c 20 20 20 20 20 2f 2a 20 6c 6f 6f 6b 75 70  D,     /* lookup
31f9d 20 62 79 20 64 6f 63 69 64 20 2a 2f 0a 20 20 51   by docid */.  Q
31f9e 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 20 20  UERY_FULLTEXT   
31f9f 2f 2a 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58  /* QUERY_FULLTEX
31fa0 54 20 2b 20 5b 69 5d 20 69 73 20 61 20 66 75 6c  T + [i] is a ful
31fa1 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20 66 6f  l-text search fo
31fa2 72 20 63 6f 6c 75 6d 6e 20 69 2a 2f 0a 7d 20 51  r column i*/.} Q
31fa3 75 65 72 79 54 79 70 65 3b 0a 0a 74 79 70 65 64  ueryType;..typed
31fa4 65 66 20 65 6e 75 6d 20 66 75 6c 6c 74 65 78 74  ef enum fulltext
31fa5 5f 73 74 61 74 65 6d 65 6e 74 20 7b 0a 20 20 43  _statement {.  C
31fa6 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54  ONTENT_INSERT_ST
31fa7 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 53 45  MT,.  CONTENT_SE
31fa8 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e  LECT_STMT,.  CON
31fa9 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d 54  TENT_UPDATE_STMT
31faa 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45  ,.  CONTENT_DELE
31fab 54 45 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45  TE_STMT,.  CONTE
31fac 4e 54 5f 45 58 49 53 54 53 5f 53 54 4d 54 2c 0a  NT_EXISTS_STMT,.
31fad 0a 20 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 5f  .  BLOCK_INSERT_
31fae 53 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 53 45  STMT,.  BLOCK_SE
31faf 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f  LECT_STMT,.  BLO
31fb0 43 4b 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 0a  CK_DELETE_STMT,.
31fb1 20 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41    BLOCK_DELETE_A
31fb2 4c 4c 5f 53 54 4d 54 2c 0a 0a 20 20 53 45 47 44  LL_STMT,..  SEGD
31fb3 49 52 5f 4d 41 58 5f 49 4e 44 45 58 5f 53 54 4d  IR_MAX_INDEX_STM
31fb4 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 45 54 5f  T,.  SEGDIR_SET_
31fb5 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53  STMT,.  SEGDIR_S
31fb6 45 4c 45 43 54 5f 4c 45 56 45 4c 5f 53 54 4d 54  ELECT_LEVEL_STMT
31fb7 2c 0a 20 20 53 45 47 44 49 52 5f 53 50 41 4e 5f  ,.  SEGDIR_SPAN_
31fb8 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 44  STMT,.  SEGDIR_D
31fb9 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20 53 45  ELETE_STMT,.  SE
31fba 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45 47 4d  GDIR_SELECT_SEGM
31fbb 45 4e 54 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44  ENT_STMT,.  SEGD
31fbc 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54  IR_SELECT_ALL_ST
31fbd 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 44 45 4c  MT,.  SEGDIR_DEL
31fbe 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 20 20  ETE_ALL_STMT,.  
31fbf 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f 53 54 4d  SEGDIR_COUNT_STM
31fc0 54 2c 0a 0a 20 20 4d 41 58 5f 53 54 4d 54 20 20  T,..  MAX_STMT  
31fc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31fc2 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 61 74 20     /* Always at 
31fc3 65 6e 64 21 20 2a 2f 0a 7d 20 66 75 6c 6c 74 65  end! */.} fullte
31fc4 78 74 5f 73 74 61 74 65 6d 65 6e 74 3b 0a 0a 2f  xt_statement;../
31fc5 2a 20 54 68 65 73 65 20 6d 75 73 74 20 65 78 61  * These must exa
31fc6 63 74 6c 79 20 6d 61 74 63 68 20 74 68 65 20 65  ctly match the e
31fc7 6e 75 6d 20 61 62 6f 76 65 2e 20 2a 2f 0a 2f 2a  num above. */./*
31fc8 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 49 73   TODO(shess): Is
31fc9 20 74 68 65 72 65 20 73 6f 6d 65 20 72 69 73 6b   there some risk
31fca 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e   that a statemen
31fcb 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69  t will be used i
31fcc 6e 20 74 77 6f 0a 2a 2a 20 63 75 72 73 6f 72 73  n two.** cursors
31fcd 20 61 74 20 6f 6e 63 65 2c 20 65 2e 67 2e 20 20   at once, e.g.  
31fce 69 66 20 61 20 71 75 65 72 79 20 6a 6f 69 6e 73  if a query joins
31fcf 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
31fd0 20 74 6f 20 69 74 73 65 6c 66 3f 0a 2a 2a 20 49   to itself?.** I
31fd1 66 20 73 6f 20 70 65 72 68 61 70 73 20 77 65 20  f so perhaps we 
31fd2 73 68 6f 75 6c 64 20 6d 6f 76 65 20 73 6f 6d 65  should move some
31fd3 20 6f 66 20 74 68 65 73 65 20 74 6f 20 74 68 65   of these to the
31fd4 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a   cursor object..
31fd5 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
31fd6 63 68 61 72 20 2a 63 6f 6e 73 74 20 66 75 6c 6c  char *const full
31fd7 74 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b  text_zStatement[
31fd8 4d 41 58 5f 53 54 4d 54 5d 20 3d 20 7b 0a 20 20  MAX_STMT] = {.  
31fd9 2f 2a 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  /* CONTENT_INSER
31fda 54 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67  T */ NULL,  /* g
31fdb 65 6e 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74  enerated in cont
31fdc 65 6e 74 49 6e 73 65 72 74 53 74 61 74 65 6d 65  entInsertStateme
31fdd 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e  nt() */.  /* CON
31fde 54 45 4e 54 5f 53 45 4c 45 43 54 20 2a 2f 20 4e  TENT_SELECT */ N
31fdf 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61 74  ULL,  /* generat
31fe0 65 64 20 69 6e 20 63 6f 6e 74 65 6e 74 53 65 6c  ed in contentSel
31fe1 65 63 74 53 74 61 74 65 6d 65 6e 74 28 29 20 2a  ectStatement() *
31fe2 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 55  /.  /* CONTENT_U
31fe3 50 44 41 54 45 20 2a 2f 20 4e 55 4c 4c 2c 20 20  PDATE */ NULL,  
31fe4 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  /* generated in 
31fe5 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61  contentUpdateSta
31fe6 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a  tement() */.  /*
31fe7 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 20   CONTENT_DELETE 
31fe8 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20  */ "delete from 
31fe9 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20  %_content where 
31fea 64 6f 63 69 64 20 3d 20 3f 22 2c 0a 20 20 2f 2a  docid = ?",.  /*
31feb 20 43 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53 20   CONTENT_EXISTS 
31fec 2a 2f 20 22 73 65 6c 65 63 74 20 64 6f 63 69 64  */ "select docid
31fed 20 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20   from %_content 
31fee 6c 69 6d 69 74 20 31 22 2c 0a 0a 20 20 2f 2a 20  limit 1",..  /* 
31fef 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 20 2a 2f 0a  BLOCK_INSERT */.
31ff0 20 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 25    "insert into %
31ff1 5f 73 65 67 6d 65 6e 74 73 20 28 62 6c 6f 63 6b  _segments (block
31ff2 69 64 2c 20 62 6c 6f 63 6b 29 20 76 61 6c 75 65  id, block) value
31ff3 73 20 28 6e 75 6c 6c 2c 20 3f 29 22 2c 0a 20 20  s (null, ?)",.  
31ff4 2f 2a 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 20  /* BLOCK_SELECT 
31ff5 2a 2f 20 22 73 65 6c 65 63 74 20 62 6c 6f 63 6b  */ "select block
31ff6 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73   from %_segments
31ff7 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20 3d   where blockid =
31ff8 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f   ?",.  /* BLOCK_
31ff9 44 45 4c 45 54 45 20 2a 2f 20 22 64 65 6c 65 74  DELETE */ "delet
31ffa 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74  e from %_segment
31ffb 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20  s where blockid 
31ffc 62 65 74 77 65 65 6e 20 3f 20 61 6e 64 20 3f 22  between ? and ?"
31ffd 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f 44 45 4c  ,.  /* BLOCK_DEL
31ffe 45 54 45 5f 41 4c 4c 20 2a 2f 20 22 64 65 6c 65  ETE_ALL */ "dele
31fff 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e  te from %_segmen
32000 74 73 22 2c 0a 0a 20 20 2f 2a 20 53 45 47 44 49  ts",..  /* SEGDI
32001 52 5f 4d 41 58 5f 49 4e 44 45 58 20 2a 2f 20 22  R_MAX_INDEX */ "
32002 73 65 6c 65 63 74 20 6d 61 78 28 69 64 78 29 20  select max(idx) 
32003 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 77 68  from %_segdir wh
32004 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a  ere level = ?",.
32005 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 54 20    /* SEGDIR_SET 
32006 2a 2f 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20  */ "insert into 
32007 25 5f 73 65 67 64 69 72 20 76 61 6c 75 65 73 20  %_segdir values 
32008 28 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20  (?, ?, ?, ?, ?, 
32009 3f 29 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52  ?)",.  /* SEGDIR
3200a 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 20 2a 2f  _SELECT_LEVEL */
3200b 0a 20 20 22 73 65 6c 65 63 74 20 73 74 61 72 74  .  "select start
3200c 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65  _block, leaves_e
3200d 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66  nd_block, root f
3200e 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22 0a 20  rom %_segdir ". 
3200f 20 22 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d   " where level =
32010 20 3f 20 6f 72 64 65 72 20 62 79 20 69 64 78 22   ? order by idx"
32011 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 50  ,.  /* SEGDIR_SP
32012 41 4e 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20  AN */.  "select 
32013 6d 69 6e 28 73 74 61 72 74 5f 62 6c 6f 63 6b 29  min(start_block)
32014 2c 20 6d 61 78 28 65 6e 64 5f 62 6c 6f 63 6b 29  , max(end_block)
32015 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22   from %_segdir "
32016 0a 20 20 22 20 77 68 65 72 65 20 6c 65 76 65 6c  .  " where level
32017 20 3d 20 3f 20 61 6e 64 20 73 74 61 72 74 5f 62   = ? and start_b
32018 6c 6f 63 6b 20 3c 3e 20 30 22 2c 0a 20 20 2f 2a  lock <> 0",.  /*
32019 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45 20 2a   SEGDIR_DELETE *
3201a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20 25  / "delete from %
3201b 5f 73 65 67 64 69 72 20 77 68 65 72 65 20 6c 65  _segdir where le
3201c 76 65 6c 20 3d 20 3f 22 2c 0a 0a 20 20 2f 2a 20  vel = ?",..  /* 
3201d 4e 4f 54 45 28 73 68 65 73 73 29 3a 20 54 68 65  NOTE(shess): The
3201e 20 66 69 72 73 74 20 74 68 72 65 65 20 72 65 73   first three res
3201f 75 6c 74 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ults of the foll
32020 6f 77 69 6e 67 20 74 77 6f 0a 20 20 2a 2a 20 73  owing two.  ** s
32021 74 61 74 65 6d 65 6e 74 73 20 6d 75 73 74 20 6d  tatements must m
32022 61 74 63 68 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20  atch..  */.  /* 
32023 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45  SEGDIR_SELECT_SE
32024 47 4d 45 4e 54 20 2a 2f 0a 20 20 22 73 65 6c 65  GMENT */.  "sele
32025 63 74 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20  ct start_block, 
32026 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
32027 2c 20 72 6f 6f 74 20 66 72 6f 6d 20 25 5f 73 65  , root from %_se
32028 67 64 69 72 20 22 0a 20 20 22 20 77 68 65 72 65  gdir ".  " where
32029 20 6c 65 76 65 6c 20 3d 20 3f 20 61 6e 64 20 69   level = ? and i
3202a 64 78 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 53 45  dx = ?",.  /* SE
3202b 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 20  GDIR_SELECT_ALL 
3202c 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 73 74 61  */.  "select sta
3202d 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73  rt_block, leaves
3202e 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74  _end_block, root
3202f 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22   from %_segdir "
32030 0a 20 20 22 20 6f 72 64 65 72 20 62 79 20 6c 65  .  " order by le
32031 76 65 6c 20 64 65 73 63 2c 20 69 64 78 20 61 73  vel desc, idx as
32032 63 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f  c",.  /* SEGDIR_
32033 44 45 4c 45 54 45 5f 41 4c 4c 20 2a 2f 20 22 64  DELETE_ALL */ "d
32034 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67  elete from %_seg
32035 64 69 72 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49  dir",.  /* SEGDI
32036 52 5f 43 4f 55 4e 54 20 2a 2f 20 22 73 65 6c 65  R_COUNT */ "sele
32037 63 74 20 63 6f 75 6e 74 28 2a 29 2c 20 69 66 6e  ct count(*), ifn
32038 75 6c 6c 28 6d 61 78 28 6c 65 76 65 6c 29 2c 30  ull(max(level),0
32039 29 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 22  ) from %_segdir"
3203a 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f  ,.};../*.** A co
3203b 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 66 75  nnection to a fu
3203c 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 69 73 20  lltext index is 
3203d 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
3203e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
3203f 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
32040 78 43 72 65 61 74 65 20 61 6e 64 20 78 43 6f 6e  xCreate and xCon
32041 6e 65 63 74 20 6d 65 74 68 6f 64 73 20 63 72 65  nect methods cre
32042 61 74 65 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a  ate an instance.
32043 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ** of this struc
32044 74 75 72 65 20 61 6e 64 20 78 44 65 73 74 72 6f  ture and xDestro
32045 79 20 61 6e 64 20 78 44 69 73 63 6f 6e 6e 65 63  y and xDisconnec
32046 74 20 66 72 65 65 20 74 68 61 74 20 69 6e 73 74  t free that inst
32047 61 6e 63 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68  ance..** All oth
32048 65 72 20 6d 65 74 68 6f 64 73 20 72 65 63 65 69  er methods recei
32049 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ve a pointer to 
3204a 74 68 65 20 73 74 72 75 63 74 75 72 65 20 61 73  the structure as
3204b 20 6f 6e 65 20 6f 66 20 74 68 65 69 72 0a 2a 2a   one of their.**
3204c 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 73   arguments..*/.s
3204d 74 72 75 63 74 20 66 75 6c 6c 74 65 78 74 5f 76  truct fulltext_v
3204e 74 61 62 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  tab {.  sqlite3_
3204f 76 74 61 62 20 62 61 73 65 3b 20 20 20 20 20 20  vtab base;      
32050 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
32051 20 63 6c 61 73 73 20 75 73 65 64 20 62 79 20 53   class used by S
32052 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 20 20  QLite core */.  
32053 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
32054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32055 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
32056 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
32057 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
32058 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
32059 20 20 2f 2a 20 6c 6f 67 69 63 61 6c 20 64 61 74    /* logical dat
3205a 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  abase name */.  
3205b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
3205c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
3205d 20 2f 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c   /* virtual tabl
3205e 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  e name */.  int 
3205f 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  nColumn;        
32060 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32061 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
32062 73 20 69 6e 20 76 69 72 74 75 61 6c 20 74 61 62  s in virtual tab
32063 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
32064 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  zColumn;        
32065 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75           /* colu
32066 6d 6e 20 6e 61 6d 65 73 2e 20 20 6d 61 6c 6c 6f  mn names.  mallo
32067 63 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ced */.  char **
32068 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b  azContentColumn;
32069 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c            /* col
3206a 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 63 6f 6e  umn names in con
3206b 74 65 6e 74 20 74 61 62 6c 65 3b 20 6d 61 6c 6c  tent table; mall
3206c 6f 63 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  oced */.  sqlite
3206d 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
3206e 6b 65 6e 69 7a 65 72 3b 20 20 20 2f 2a 20 74 6f  kenizer;   /* to
3206f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 69 6e 73 65  kenizer for inse
32070 72 74 73 20 61 6e 64 20 71 75 65 72 69 65 73 20  rts and queries 
32071 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 63 6f 6d 70  */..  /* Precomp
32072 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  iled statements 
32073 77 68 69 63 68 20 77 65 20 6b 65 65 70 20 61 73  which we keep as
32074 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 61 62   long as the tab
32075 6c 65 20 69 73 0a 20 20 2a 2a 20 6f 70 65 6e 2e  le is.  ** open.
32076 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
32077 73 74 6d 74 20 2a 70 46 75 6c 6c 74 65 78 74 53  stmt *pFulltextS
32078 74 61 74 65 6d 65 6e 74 73 5b 4d 41 58 5f 53 54  tatements[MAX_ST
32079 4d 54 5d 3b 0a 0a 20 20 2f 2a 20 50 72 65 63 6f  MT];..  /* Preco
3207a 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74  mpiled statement
3207b 73 20 75 73 65 64 20 66 6f 72 20 73 65 67 6d 65  s used for segme
3207c 6e 74 20 6d 65 72 67 65 73 2e 20 20 57 65 20 72  nt merges.  We r
3207d 75 6e 20 61 0a 20 20 2a 2a 20 73 65 70 61 72 61  un a.  ** separa
3207e 74 65 20 73 65 6c 65 63 74 20 61 63 72 6f 73 73  te select across
3207f 20 74 68 65 20 6c 65 61 66 20 6c 65 76 65 6c 20   the leaf level 
32080 6f 66 20 65 61 63 68 20 74 72 65 65 20 62 65 69  of each tree bei
32081 6e 67 20 6d 65 72 67 65 64 2e 0a 20 20 2a 2f 0a  ng merged..  */.
32082 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
32083 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
32084 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20  [MERGE_COUNT];. 
32085 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e   /* The statemen
32086 74 20 75 73 65 64 20 74 6f 20 70 72 65 70 61 72  t used to prepar
32087 65 20 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d  e pLeafSelectStm
32088 74 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4c  ts. */.#define L
32089 45 41 46 5f 53 45 4c 45 43 54 20 5c 0a 20 20 22  EAF_SELECT \.  "
3208a 73 65 6c 65 63 74 20 62 6c 6f 63 6b 20 66 72 6f  select block fro
3208b 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65  m %_segments whe
3208c 72 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77 65  re blockid betwe
3208d 65 6e 20 3f 20 61 6e 64 20 3f 20 6f 72 64 65 72  en ? and ? order
3208e 20 62 79 20 62 6c 6f 63 6b 69 64 22 0a 0a 20 20   by blockid"..  
3208f 2f 2a 20 54 68 65 73 65 20 62 75 66 66 65 72 20  /* These buffer 
32090 70 65 6e 64 69 6e 67 20 69 6e 64 65 78 20 75 70  pending index up
32091 64 61 74 65 73 20 64 75 72 69 6e 67 20 74 72 61  dates during tra
32092 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20  nsactions..  ** 
32093 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 65 73 74  nPendingData est
32094 69 6d 61 74 65 73 20 74 68 65 20 6d 65 6d 6f 72  imates the memor
32095 79 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 65  y size of the pe
32096 6e 64 69 6e 67 20 64 61 74 61 2e 20 20 49 74 0a  nding data.  It.
32097 20 20 2a 2a 20 64 6f 65 73 6e 27 74 20 69 6e 63    ** doesn't inc
32098 6c 75 64 65 20 74 68 65 20 68 61 73 68 2d 62 75  lude the hash-bu
32099 63 6b 65 74 20 6f 76 65 72 68 65 61 64 2c 20 6e  cket overhead, n
3209a 6f 72 20 61 6e 79 20 6d 61 6c 6c 6f 63 0a 20 20  or any malloc.  
3209b 2a 2a 20 6f 76 65 72 68 65 61 64 2e 20 20 57 68  ** overhead.  Wh
3209c 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 20  en nPendingData 
3209d 65 78 63 65 65 64 73 20 6b 50 65 6e 64 69 6e 67  exceeds kPending
3209e 54 68 72 65 73 68 6f 6c 64 2c 20 74 68 65 0a 20  Threshold, the. 
3209f 20 2a 2a 20 62 75 66 66 65 72 20 69 73 20 66 6c   ** buffer is fl
320a0 75 73 68 65 64 20 65 76 65 6e 20 62 65 66 6f 72  ushed even befor
320a1 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
320a2 6e 20 63 6c 6f 73 65 73 2e 0a 20 20 2a 2a 20 70  n closes..  ** p
320a3 65 6e 64 69 6e 67 54 65 72 6d 73 20 73 74 6f 72  endingTerms stor
320a4 65 73 20 74 68 65 20 64 61 74 61 2c 20 61 6e 64  es the data, and
320a5 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 77   is only valid w
320a6 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74 61  hen nPendingData
320a7 0a 20 20 2a 2a 20 69 73 20 3e 3d 30 20 28 6e 50  .  ** is >=0 (nP
320a8 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 6d 65 61  endingData<0 mea
320a9 6e 73 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  ns pendingTerms 
320aa 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 20 20 2a  has not been.  *
320ab 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 29 2e 20  * initialized). 
320ac 20 69 50 72 65 76 44 6f 63 69 64 20 69 73 20 74   iPrevDocid is t
320ad 68 65 20 6c 61 73 74 20 64 6f 63 69 64 20 77 72  he last docid wr
320ae 69 74 74 65 6e 2c 20 75 73 65 64 20 74 6f 20 6d  itten, used to m
320af 61 6b 65 0a 20 20 2a 2a 20 63 65 72 74 61 69 6e  ake.  ** certain
320b0 20 77 65 27 72 65 20 69 6e 73 65 72 74 69 6e 67   we're inserting
320b1 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
320b2 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 65  ..  */.  int nPe
320b3 6e 64 69 6e 67 44 61 74 61 3b 0a 23 64 65 66 69  ndingData;.#defi
320b4 6e 65 20 6b 50 65 6e 64 69 6e 67 54 68 72 65 73  ne kPendingThres
320b5 68 6f 6c 64 20 28 31 2a 31 30 32 34 2a 31 30 32  hold (1*1024*102
320b6 34 29 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  4).  sqlite_int6
320b7 34 20 69 50 72 65 76 44 6f 63 69 64 3b 0a 20 20  4 iPrevDocid;.  
320b8 66 74 73 33 48 61 73 68 20 70 65 6e 64 69 6e 67  fts3Hash pending
320b9 54 65 72 6d 73 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  Terms;.};../*.**
320ba 20 57 68 65 6e 20 74 68 65 20 63 6f 72 65 20 77   When the core w
320bb 61 6e 74 73 20 74 6f 20 64 6f 20 61 20 71 75 65  ants to do a que
320bc 72 79 2c 20 69 74 20 63 72 65 61 74 65 20 61 20  ry, it create a 
320bd 63 75 72 73 6f 72 20 75 73 69 6e 67 20 61 0a 2a  cursor using a.*
320be 2a 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e 2e  * call to xOpen.
320bf 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
320c0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
320c1 6f 66 20 61 20 63 75 72 73 6f 72 2e 20 20 49 74  of a cursor.  It
320c2 0a 2a 2a 20 69 73 20 64 65 73 74 72 6f 79 65 64  .** is destroyed
320c3 20 62 79 20 78 43 6c 6f 73 65 2e 0a 2a 2f 0a 74   by xClose..*/.t
320c4 79 70 65 64 65 66 20 73 74 72 75 63 74 20 66 75  ypedef struct fu
320c5 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 7b 0a  lltext_cursor {.
320c6 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63    sqlite3_vtab_c
320c7 75 72 73 6f 72 20 62 61 73 65 3b 20 20 20 20 20  ursor base;     
320c8 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73     /* Base class
320c9 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
320ca 63 6f 72 65 20 2a 2f 0a 20 20 51 75 65 72 79 54  core */.  QueryT
320cb 79 70 65 20 69 43 75 72 73 6f 72 54 79 70 65 3b  ype iCursorType;
320cc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
320cd 70 79 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e  py of sqlite3_in
320ce 64 65 78 5f 69 6e 66 6f 2e 69 64 78 4e 75 6d 20  dex_info.idxNum 
320cf 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
320d0 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20 20 20  t *pStmt;       
320d1 20 20 20 20 20 20 2f 2a 20 50 72 65 70 61 72 65        /* Prepare
320d2 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 75  d statement in u
320d3 73 65 20 62 79 20 74 68 65 20 63 75 72 73 6f 72  se by the cursor
320d4 20 2a 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20   */.  int eof;  
320d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
320d6 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
320d7 66 20 61 74 20 45 6e 64 20 4f 66 20 52 65 73 75  f at End Of Resu
320d8 6c 74 73 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  lts */.  Fts3Exp
320d9 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
320da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
320db 73 65 64 20 4d 41 54 43 48 20 71 75 65 72 79 20  sed MATCH query 
320dc 73 74 72 69 6e 67 20 2a 2f 0a 20 20 53 6e 69 70  string */.  Snip
320dd 70 65 74 20 73 6e 69 70 70 65 74 3b 20 20 20 20  pet snippet;    
320de 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
320df 43 61 63 68 65 64 20 73 6e 69 70 70 65 74 20 66  Cached snippet f
320e0 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
320e1 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  ow */.  int iCol
320e2 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
320e3 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
320e4 6d 6e 20 62 65 69 6e 67 20 73 65 61 72 63 68 65  mn being searche
320e5 64 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65  d */.  DataBuffe
320e6 72 20 72 65 73 75 6c 74 3b 20 20 20 20 20 20 20  r result;       
320e7 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69          /* Docli
320e8 73 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20  st results from 
320e9 66 75 6c 6c 74 65 78 74 51 75 65 72 79 20 2a 2f  fulltextQuery */
320ea 0a 20 20 44 4c 52 65 61 64 65 72 20 72 65 61 64  .  DLReader read
320eb 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
320ec 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72 65      /* Result re
320ed 61 64 65 72 20 69 66 20 72 65 73 75 6c 74 20 6e  ader if result n
320ee 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 7d 20 66 75  ot empty */.} fu
320ef 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 3b 0a 0a  lltext_cursor;..
320f0 73 74 61 74 69 63 20 66 75 6c 6c 74 65 78 74 5f  static fulltext_
320f1 76 74 61 62 20 2a 63 75 72 73 6f 72 5f 76 74 61  vtab *cursor_vta
320f2 62 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f  b(fulltext_curso
320f3 72 20 2a 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  r *c){.  return 
320f4 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
320f5 29 20 63 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b  ) c->base.pVtab;
320f6 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .}..static const
320f7 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
320f8 66 74 73 33 4d 6f 64 75 6c 65 3b 20 20 20 2f 2a  fts3Module;   /*
320f9 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   forward declara
320fa 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 20 52 65 74 75  tion */../* Retu
320fb 72 6e 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  rn a dynamically
320fc 20 67 65 6e 65 72 61 74 65 64 20 73 74 61 74 65   generated state
320fd 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d  ment of the form
320fe 0a 20 2a 20 20 20 69 6e 73 65 72 74 20 69 6e 74  . *   insert int
320ff 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63  o %_content (doc
32100 69 64 2c 20 2e 2e 2e 29 20 76 61 6c 75 65 73 20  id, ...) values 
32101 28 3f 2c 20 2e 2e 2e 29 0a 20 2a 2f 0a 73 74 61  (?, ...). */.sta
32102 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
32103 63 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53 74 61  contentInsertSta
32104 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  tement(fulltext_
32105 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69  vtab *v){.  Stri
32106 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
32107 6e 74 20 69 3b 0a 0a 20 20 69 6e 69 74 53 74 72  nt i;..  initStr
32108 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a  ingBuffer(&sb);.
32109 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 69    append(&sb, "i
3210a 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 63 6f 6e  nsert into %_con
3210b 74 65 6e 74 20 28 64 6f 63 69 64 2c 20 22 29 3b  tent (docid, ");
3210c 0a 20 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73  .  appendList(&s
3210d 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76  b, v->nColumn, v
3210e 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
3210f 6e 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62  n);.  append(&sb
32110 2c 20 22 29 20 76 61 6c 75 65 73 20 28 3f 22 29  , ") values (?")
32111 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76  ;.  for(i=0; i<v
32112 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 0a  ->nColumn; ++i).
32113 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20      append(&sb, 
32114 22 2c 20 3f 22 29 3b 0a 20 20 61 70 70 65 6e 64  ", ?");.  append
32115 28 26 73 62 2c 20 22 29 22 29 3b 0a 20 20 72 65  (&sb, ")");.  re
32116 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66 66 65  turn stringBuffe
32117 72 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f  rData(&sb);.}../
32118 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d  * Return a dynam
32119 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
3211a 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68   statement of th
3211b 65 20 66 6f 72 6d 0a 20 2a 20 20 20 73 65 6c 65  e form. *   sele
3211c 63 74 20 3c 63 6f 6e 74 65 6e 74 20 63 6f 6c 75  ct <content colu
3211d 6d 6e 73 3e 20 66 72 6f 6d 20 25 5f 63 6f 6e 74  mns> from %_cont
3211e 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64 20  ent where docid 
3211f 3d 20 3f 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63  = ?. */.static c
32120 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65  onst char *conte
32121 6e 74 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e  ntSelectStatemen
32122 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
32123 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66  *v){.  StringBuf
32124 66 65 72 20 73 62 3b 0a 20 20 69 6e 69 74 53 74  fer sb;.  initSt
32125 72 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b  ringBuffer(&sb);
32126 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22  .  append(&sb, "
32127 53 45 4c 45 43 54 20 22 29 3b 0a 20 20 61 70 70  SELECT ");.  app
32128 65 6e 64 4c 69 73 74 28 26 73 62 2c 20 76 2d 3e  endList(&sb, v->
32129 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43 6f  nColumn, v->azCo
3212a 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ntentColumn);.  
3212b 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 46 52  append(&sb, " FR
3212c 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74 20 57 48 45  OM %_content WHE
3212d 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a  RE docid = ?");.
3212e 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42    return stringB
3212f 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a  ufferData(&sb);.
32130 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 64  }../* Return a d
32131 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65 72  ynamically gener
32132 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6f  ated statement o
32133 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20 20  f the form. *   
32134 75 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74  update %_content
32135 20 73 65 74 20 5b 63 6f 6c 5f 30 5d 20 3d 20 3f   set [col_0] = ?
32136 2c 20 5b 63 6f 6c 5f 31 5d 20 3d 20 3f 2c 20 2e  , [col_1] = ?, .
32137 2e 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  ... *           
32138 20 20 20 20 20 20 20 20 20 77 68 65 72 65 20 64           where d
32139 6f 63 69 64 20 3d 20 3f 0a 20 2a 2f 0a 73 74 61  ocid = ?. */.sta
3213a 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
3213b 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61  contentUpdateSta
3213c 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  tement(fulltext_
3213d 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69  vtab *v){.  Stri
3213e 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
3213f 6e 74 20 69 3b 0a 0a 20 20 69 6e 69 74 53 74 72  nt i;..  initStr
32140 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a  ingBuffer(&sb);.
32141 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 75    append(&sb, "u
32142 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74 20  pdate %_content 
32143 73 65 74 20 22 29 3b 0a 20 20 66 6f 72 28 69 3d  set ");.  for(i=
32144 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b  0; i<v->nColumn;
32145 20 2b 2b 69 29 20 7b 0a 20 20 20 20 69 66 28 20   ++i) {.    if( 
32146 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 61 70 70  i>0 ){.      app
32147 65 6e 64 28 26 73 62 2c 20 22 2c 20 22 29 3b 0a  end(&sb, ", ");.
32148 20 20 20 20 7d 0a 20 20 20 20 61 70 70 65 6e 64      }.    append
32149 28 26 73 62 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65  (&sb, v->azConte
3214a 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20  ntColumn[i]);.  
3214b 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20    append(&sb, " 
3214c 3d 20 3f 22 29 3b 0a 20 20 7d 0a 20 20 61 70 70  = ?");.  }.  app
3214d 65 6e 64 28 26 73 62 2c 20 22 20 77 68 65 72 65  end(&sb, " where
3214e 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a 20 20   docid = ?");.  
3214f 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66  return stringBuf
32150 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a  ferData(&sb);.}.
32151 0a 2f 2a 20 50 75 74 73 20 61 20 66 72 65 73 68  ./* Puts a fresh
32152 6c 79 2d 70 72 65 70 61 72 65 64 20 73 74 61 74  ly-prepared stat
32153 65 6d 65 6e 74 20 64 65 74 65 72 6d 69 6e 65 64  ement determined
32154 20 62 79 20 69 53 74 6d 74 20 69 6e 20 2a 70 70   by iStmt in *pp
32155 53 74 6d 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  Stmt..** If the 
32156 69 6e 64 69 63 61 74 65 64 20 73 74 61 74 65 6d  indicated statem
32157 65 6e 74 20 68 61 73 20 6e 65 76 65 72 20 62 65  ent has never be
32158 65 6e 20 70 72 65 70 61 72 65 64 2c 20 69 74 20  en prepared, it 
32159 69 73 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 61  is prepared.** a
3215a 6e 64 20 63 61 63 68 65 64 2c 20 6f 74 68 65 72  nd cached, other
3215b 77 69 73 65 20 74 68 65 20 63 61 63 68 65 64 20  wise the cached 
3215c 76 65 72 73 69 6f 6e 20 69 73 20 72 65 73 65 74  version is reset
3215d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3215e 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
3215f 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
32160 2a 76 2c 20 66 75 6c 6c 74 65 78 74 5f 73 74 61  *v, fulltext_sta
32161 74 65 6d 65 6e 74 20 69 53 74 6d 74 2c 0a 20 20  tement iStmt,.  
32162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32163 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
32164 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
32165 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74  ){.  assert( iSt
32166 6d 74 3c 4d 41 58 5f 53 54 4d 54 20 29 3b 0a 20  mt<MAX_STMT );. 
32167 20 69 66 28 20 76 2d 3e 70 46 75 6c 6c 74 65 78   if( v->pFulltex
32168 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d  tStatements[iStm
32169 74 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  t]==NULL ){.    
3216a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 6d  const char *zStm
3216b 74 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  t;.    int rc;. 
3216c 20 20 20 73 77 69 74 63 68 28 20 69 53 74 6d 74     switch( iStmt
3216d 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 43   ){.      case C
3216e 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54  ONTENT_INSERT_ST
3216f 4d 54 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d  MT:.        zStm
32170 74 20 3d 20 63 6f 6e 74 65 6e 74 49 6e 73 65 72  t = contentInser
32171 74 53 74 61 74 65 6d 65 6e 74 28 76 29 3b 20 62  tStatement(v); b
32172 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32173 20 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f   CONTENT_SELECT_
32174 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20 7a 53  STMT:.        zS
32175 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 53 65 6c  tmt = contentSel
32176 65 63 74 53 74 61 74 65 6d 65 6e 74 28 76 29 3b  ectStatement(v);
32177 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32178 73 65 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54  se CONTENT_UPDAT
32179 45 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20  E_STMT:.        
3217a 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 55  zStmt = contentU
3217b 70 64 61 74 65 53 74 61 74 65 6d 65 6e 74 28 76  pdateStatement(v
3217c 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
3217d 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20  default:.       
3217e 20 7a 53 74 6d 74 20 3d 20 66 75 6c 6c 74 65 78   zStmt = fulltex
3217f 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b 69 53 74  t_zStatement[iSt
32180 6d 74 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  mt];.    }.    r
32181 63 20 3d 20 73 71 6c 5f 70 72 65 70 61 72 65 28  c = sql_prepare(
32182 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76  v->db, v->zDb, v
32183 2d 3e 7a 4e 61 6d 65 2c 20 26 76 2d 3e 70 46 75  ->zName, &v->pFu
32184 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
32185 5b 69 53 74 6d 74 5d 2c 0a 20 20 20 20 20 20 20  [iStmt],.       
32186 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32187 20 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 69 66    zStmt);.    if
32188 28 20 7a 53 74 6d 74 20 21 3d 20 66 75 6c 6c 74  ( zStmt != fullt
32189 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b 69  ext_zStatement[i
3218a 53 74 6d 74 5d 29 20 73 71 6c 69 74 65 33 5f 66  Stmt]) sqlite3_f
3218b 72 65 65 28 28 76 6f 69 64 20 2a 29 20 7a 53 74  ree((void *) zSt
3218c 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  mt);.    if( rc!
3218d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3218e 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65  urn rc;.  } else
3218f 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20   {.    int rc = 
32190 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 76 2d  sqlite3_reset(v-
32191 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
32192 65 6e 74 73 5b 69 53 74 6d 74 5d 29 3b 0a 20 20  ents[iStmt]);.  
32193 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32194 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
32195 0a 20 20 7d 0a 0a 20 20 2a 70 70 53 74 6d 74 20  .  }..  *ppStmt 
32196 3d 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74  = v->pFulltextSt
32197 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 3b  atements[iStmt];
32198 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
32199 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20  _OK;.}../* Like 
3219a 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2c 20  sqlite3_step(), 
3219b 62 75 74 20 63 6f 6e 76 65 72 74 20 53 51 4c 49  but convert SQLI
3219c 54 45 5f 44 4f 4e 45 20 74 6f 20 53 51 4c 49 54  TE_DONE to SQLIT
3219d 45 5f 4f 4b 20 61 6e 64 0a 2a 2a 20 53 51 4c 49  E_OK and.** SQLI
3219e 54 45 5f 52 4f 57 20 74 6f 20 53 51 4c 49 54 45  TE_ROW to SQLITE
3219f 5f 45 52 52 4f 52 2e 20 20 55 73 65 66 75 6c 20  _ERROR.  Useful 
321a0 66 6f 72 20 73 74 61 74 65 6d 65 6e 74 73 20 6c  for statements l
321a1 69 6b 65 20 55 50 44 41 54 45 2c 0a 2a 2a 20 77  ike UPDATE,.** w
321a2 68 65 72 65 20 77 65 20 65 78 70 65 63 74 20 6e  here we expect n
321a3 6f 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74  o results..*/.st
321a4 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 73 69 6e  atic int sql_sin
321a5 67 6c 65 5f 73 74 65 70 28 73 71 6c 69 74 65 33  gle_step(sqlite3
321a6 5f 73 74 6d 74 20 2a 73 29 7b 0a 20 20 69 6e 74  _stmt *s){.  int
321a7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
321a8 65 70 28 73 29 3b 0a 20 20 72 65 74 75 72 6e 20  ep(s);.  return 
321a9 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  (rc==SQLITE_DONE
321aa 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  ) ? SQLITE_OK : 
321ab 72 63 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 73  rc;.}../* Like s
321ac 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
321ad 28 29 2c 20 62 75 74 20 66 6f 72 20 73 70 65 63  (), but for spec
321ae 69 61 6c 20 72 65 70 6c 69 63 61 74 65 64 20 4c  ial replicated L
321af 45 41 46 5f 53 45 4c 45 43 54 0a 2a 2a 20 73 74  EAF_SELECT.** st
321b0 61 74 65 6d 65 6e 74 73 2e 20 20 69 64 78 20 2d  atements.  idx -
321b1 31 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 63  1 is a special c
321b2 61 73 65 20 66 6f 72 20 61 6e 20 75 6e 63 61 63  ase for an uncac
321b3 68 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 0a 2a  hed version of.*
321b4 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
321b5 28 75 73 65 64 20 69 6e 20 74 68 65 20 6f 70 74  (used in the opt
321b6 69 6d 69 7a 65 20 69 6d 70 6c 65 6d 65 6e 74 61  imize implementa
321b7 74 69 6f 6e 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  tion)..*/./* TOD
321b8 4f 28 73 68 65 73 73 29 20 57 72 69 74 65 20 76  O(shess) Write v
321b9 65 72 73 69 6f 6e 20 66 6f 72 20 67 65 6e 65 72  ersion for gener
321ba 69 63 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e  ic statements an
321bb 64 20 74 68 65 6e 20 73 68 61 72 65 0a 2a 2a 20  d then share.** 
321bc 74 68 61 74 20 62 65 74 77 65 65 6e 20 74 68 65  that between the
321bd 20 63 61 63 68 65 64 2d 73 74 61 74 65 6d 65 6e   cached-statemen
321be 74 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  t functions..*/.
321bf 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 67  static int sql_g
321c0 65 74 5f 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e  et_leaf_statemen
321c1 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
321c2 2a 76 2c 20 69 6e 74 20 69 64 78 2c 0a 20 20 20  *v, int idx,.   
321c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
321c5 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
321c6 53 74 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28  Stmt){.  assert(
321c7 20 69 64 78 3e 3d 2d 31 20 26 26 20 69 64 78 3c   idx>=-1 && idx<
321c8 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 20  MERGE_COUNT );. 
321c9 20 69 66 28 20 69 64 78 3d 3d 2d 31 20 29 7b 0a   if( idx==-1 ){.
321ca 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 70      return sql_p
321cb 72 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d  repare(v->db, v-
321cc 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20  >zDb, v->zName, 
321cd 70 70 53 74 6d 74 2c 20 4c 45 41 46 5f 53 45 4c  ppStmt, LEAF_SEL
321ce 45 43 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ECT);.  }else if
321cf 28 20 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74  ( v->pLeafSelect
321d0 53 74 6d 74 73 5b 69 64 78 5d 3d 3d 4e 55 4c 4c  Stmts[idx]==NULL
321d1 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d   ){.    int rc =
321d2 20 73 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e   sql_prepare(v->
321d3 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a  db, v->zDb, v->z
321d4 4e 61 6d 65 2c 20 26 76 2d 3e 70 4c 65 61 66 53  Name, &v->pLeafS
321d5 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78 5d 2c  electStmts[idx],
321d6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
321d7 20 20 20 20 20 20 20 20 20 20 4c 45 41 46 5f 53            LEAF_S
321d8 45 4c 45 43 54 29 3b 0a 20 20 20 20 69 66 28 20  ELECT);.    if( 
321d9 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
321da 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c  return rc;.  }el
321db 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d  se{.    int rc =
321dc 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 76   sqlite3_reset(v
321dd 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d  ->pLeafSelectStm
321de 74 73 5b 69 64 78 5d 29 3b 0a 20 20 20 20 69 66  ts[idx]);.    if
321df 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
321e0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
321e1 0a 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 76 2d  ..  *ppStmt = v-
321e2 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74  >pLeafSelectStmt
321e3 73 5b 69 64 78 5d 3b 0a 20 20 72 65 74 75 72 6e  s[idx];.  return
321e4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
321e5 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f  * insert into %_
321e6 63 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c 20  content (docid, 
321e7 2e 2e 2e 29 20 76 61 6c 75 65 73 20 28 5b 64 6f  ...) values ([do
321e8 63 69 64 5d 2c 20 5b 70 56 61 6c 75 65 73 5d 29  cid], [pValues])
321e9 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 63 69 64  .** If the docid
321ea 20 63 6f 6e 74 61 69 6e 73 20 53 51 4c 20 4e 55   contains SQL NU
321eb 4c 4c 2c 20 74 68 65 6e 20 61 20 75 6e 69 71 75  LL, then a uniqu
321ec 65 20 64 6f 63 69 64 20 77 69 6c 6c 20 62 65 0a  e docid will be.
321ed 2a 2a 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f  ** generated..*/
321ee 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74  .static int cont
321ef 65 6e 74 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74  ent_insert(fullt
321f0 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
321f1 69 74 65 33 5f 76 61 6c 75 65 20 2a 64 6f 63 69  ite3_value *doci
321f2 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
321f3 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
321f4 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
321f5 6c 75 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33  lues){.  sqlite3
321f6 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
321f7 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71  i;.  int rc = sq
321f8 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28  l_get_statement(
321f9 76 2c 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  v, CONTENT_INSER
321fa 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  T_STMT, &s);.  i
321fb 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
321fc 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
321fd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
321fe 6e 64 5f 76 61 6c 75 65 28 73 2c 20 31 2c 20 64  nd_value(s, 1, d
321ff 6f 63 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ocid);.  if( rc!
32200 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
32201 75 72 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69  urn rc;..  for(i
32202 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e  =0; i<v->nColumn
32203 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 72 63 20 3d  ; ++i){.    rc =
32204 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
32205 6c 75 65 28 73 2c 20 32 2b 69 2c 20 70 56 61 6c  lue(s, 2+i, pVal
32206 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  ues[i]);.    if(
32207 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32208 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
32209 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69  .  return sql_si
3220a 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a  ngle_step(s);.}.
3220b 0a 2f 2a 20 75 70 64 61 74 65 20 25 5f 63 6f 6e  ./* update %_con
3220c 74 65 6e 74 20 73 65 74 20 63 6f 6c 30 20 3d 20  tent set col0 = 
3220d 70 56 61 6c 75 65 73 5b 30 5d 2c 20 63 6f 6c 31  pValues[0], col1
3220e 20 3d 20 70 56 61 6c 75 65 73 5b 31 5d 2c 20 2e   = pValues[1], .
3220f 2e 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  ... *           
32210 20 20 20 20 20 20 20 77 68 65 72 65 20 64 6f 63         where doc
32211 69 64 20 3d 20 5b 69 44 6f 63 69 64 5d 20 2a 2f  id = [iDocid] */
32212 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74  .static int cont
32213 65 6e 74 5f 75 70 64 61 74 65 28 66 75 6c 6c 74  ent_update(fullt
32214 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
32215 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
32216 6c 75 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lues,.          
32217 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32218 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
32219 63 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  cid){.  sqlite3_
3221a 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 69  stmt *s;.  int i
3221b 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
3221c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
3221d 2c 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45  , CONTENT_UPDATE
3221e 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
3221f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
32220 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
32221 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43  for(i=0; i<v->nC
32222 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20  olumn; ++i){.   
32223 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
32224 6e 64 5f 76 61 6c 75 65 28 73 2c 20 31 2b 69 2c  nd_value(s, 1+i,
32225 20 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20   pValues[i]);.  
32226 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32227 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
32228 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c  .  }..  rc = sql
32229 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
3222a 73 2c 20 31 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c  s, 1+v->nColumn,
3222b 20 69 44 6f 63 69 64 29 3b 0a 20 20 69 66 28 20   iDocid);.  if( 
3222c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3222d 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65  return rc;..  re
3222e 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f  turn sql_single_
3222f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 73 74 61 74  step(s);.}..stat
32230 69 63 20 76 6f 69 64 20 66 72 65 65 53 74 72 69  ic void freeStri
32231 6e 67 41 72 72 61 79 28 69 6e 74 20 6e 53 74 72  ngArray(int nStr
32232 69 6e 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ing, const char 
32233 2a 2a 70 53 74 72 69 6e 67 29 7b 0a 20 20 69 6e  **pString){.  in
32234 74 20 69 3b 0a 0a 20 20 66 6f 72 20 28 69 3d 30  t i;..  for (i=0
32235 20 3b 20 69 20 3c 20 6e 53 74 72 69 6e 67 20 3b   ; i < nString ;
32236 20 2b 2b 69 29 20 7b 0a 20 20 20 20 69 66 28 20   ++i) {.    if( 
32237 70 53 74 72 69 6e 67 5b 69 5d 21 3d 4e 55 4c 4c  pString[i]!=NULL
32238 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28   ) sqlite3_free(
32239 28 76 6f 69 64 20 2a 29 20 70 53 74 72 69 6e 67  (void *) pString
3223a 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  [i]);.  }.  sqli
3223b 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a  te3_free((void *
3223c 29 20 70 53 74 72 69 6e 67 29 3b 0a 7d 0a 0a 2f  ) pString);.}../
3223d 2a 20 73 65 6c 65 63 74 20 2a 20 66 72 6f 6d 20  * select * from 
3223e 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20  %_content where 
3223f 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63 69 64 5d  docid = [iDocid]
32240 0a 20 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  . * The caller m
32241 75 73 74 20 64 65 6c 65 74 65 20 74 68 65 20 72  ust delete the r
32242 65 74 75 72 6e 65 64 20 61 72 72 61 79 20 61 6e  eturned array an
32243 64 20 61 6c 6c 20 73 74 72 69 6e 67 73 20 69 6e  d all strings in
32244 20 69 74 2e 0a 20 2a 20 6e 75 6c 6c 20 66 69 65   it.. * null fie
32245 6c 64 73 20 77 69 6c 6c 20 62 65 20 4e 55 4c 4c  lds will be NULL
32246 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64   in the returned
32247 20 61 72 72 61 79 2e 0a 20 2a 0a 20 2a 20 54 4f   array.. *. * TO
32248 44 4f 3a 20 50 65 72 68 61 70 73 20 77 65 20 73  DO: Perhaps we s
32249 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 70 6f 69  hould return poi
3224a 6e 74 65 72 2f 6c 65 6e 67 74 68 20 73 74 72 69  nter/length stri
3224b 6e 67 73 20 68 65 72 65 20 66 6f 72 20 63 6f 6e  ngs here for con
3224c 73 69 73 74 65 6e 63 79 0a 20 2a 20 77 69 74 68  sistency. * with
3224d 20 6f 74 68 65 72 20 63 6f 64 65 20 77 68 69 63   other code whic
3224e 68 20 75 73 65 73 20 70 6f 69 6e 74 65 72 2f 6c  h uses pointer/l
3224f 65 6e 67 74 68 2e 20 2a 2f 0a 73 74 61 74 69 63  ength. */.static
32250 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 73 65 6c   int content_sel
32251 65 63 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ect(fulltext_vta
32252 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
32253 36 34 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20  64 iDocid,.     
32254 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32255 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
32256 2a 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 73  ***pValues){.  s
32257 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
32258 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 76    const char **v
32259 61 6c 75 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a  alues;.  int i;.
3225a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2a 70 56    int rc;..  *pV
3225b 61 6c 75 65 73 20 3d 20 4e 55 4c 4c 3b 0a 0a 20  alues = NULL;.. 
3225c 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
3225d 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45  atement(v, CONTE
3225e 4e 54 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20  NT_SELECT_STMT, 
3225f 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
32260 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
32261 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
32262 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
32263 28 73 2c 20 31 2c 20 69 44 6f 63 69 64 29 3b 0a  (s, 1, iDocid);.
32264 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32265 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
32266 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
32267 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
32268 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc!=SQLITE_ROW )
32269 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 76   return rc;..  v
3226a 61 6c 75 65 73 20 3d 20 28 63 6f 6e 73 74 20 63  alues = (const c
3226b 68 61 72 20 2a 2a 29 20 73 71 6c 69 74 65 33 5f  har **) sqlite3_
3226c 6d 61 6c 6c 6f 63 28 76 2d 3e 6e 43 6f 6c 75 6d  malloc(v->nColum
3226d 6e 20 2a 20 73 69 7a 65 6f 66 28 63 6f 6e 73 74  n * sizeof(const
3226e 20 63 68 61 72 20 2a 29 29 3b 0a 20 20 66 6f 72   char *));.  for
3226f 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75  (i=0; i<v->nColu
32270 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 69 66  mn; ++i){.    if
32271 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  ( sqlite3_column
32272 5f 74 79 70 65 28 73 2c 20 69 29 3d 3d 53 51 4c  _type(s, i)==SQL
32273 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
32274 20 20 76 61 6c 75 65 73 5b 69 5d 20 3d 20 4e 55    values[i] = NU
32275 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  LL;.    }else{. 
32276 20 20 20 20 20 76 61 6c 75 65 73 5b 69 5d 20 3d       values[i] =
32277 20 73 74 72 69 6e 67 5f 64 75 70 28 28 63 68 61   string_dup((cha
32278 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r*)sqlite3_colum
32279 6e 5f 74 65 78 74 28 73 2c 20 69 29 29 3b 0a 20  n_text(s, i));. 
3227a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57     }.  }..  /* W
3227b 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e  e expect only on
3227c 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20  e row.  We must 
3227d 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20  execute another 
3227e 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20  sqlite3_step(). 
3227f 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20    * to complete 
32280 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f  the iteration; o
32281 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62  therwise the tab
32282 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c  le will remain l
32283 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d  ocked. */.  rc =
32284 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
32285 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
32286 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2a  TE_DONE ){.    *
32287 70 56 61 6c 75 65 73 20 3d 20 76 61 6c 75 65 73  pValues = values
32288 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
32289 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 66  ITE_OK;.  }..  f
3228a 72 65 65 53 74 72 69 6e 67 41 72 72 61 79 28 76  reeStringArray(v
3228b 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 61 6c 75 65  ->nColumn, value
3228c 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  s);.  return rc;
3228d 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74 65 20 66 72  .}../* delete fr
3228e 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65  om %_content whe
3228f 72 65 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63  re docid = [iDoc
32290 69 64 20 5d 20 2a 2f 0a 73 74 61 74 69 63 20 69  id ] */.static i
32291 6e 74 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 65 74  nt content_delet
32292 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
32293 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
32294 20 69 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69   iDocid){.  sqli
32295 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
32296 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
32297 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e  statement(v, CON
32298 54 45 4e 54 5f 44 45 4c 45 54 45 5f 53 54 4d 54  TENT_DELETE_STMT
32299 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
3229a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3229b 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
3229c 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
3229d 36 34 28 73 2c 20 31 2c 20 69 44 6f 63 69 64 29  64(s, 1, iDocid)
3229e 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
3229f 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
322a0 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  c;..  return sql
322a1 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b  _single_step(s);
322a2 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53  .}../* Returns S
322a3 51 4c 49 54 45 5f 52 4f 57 20 69 66 20 61 6e 79  QLITE_ROW if any
322a4 20 72 6f 77 73 20 65 78 69 73 74 20 69 6e 20 25   rows exist in %
322a5 5f 63 6f 6e 74 65 6e 74 2c 20 53 51 4c 49 54 45  _content, SQLITE
322a6 5f 44 4f 4e 45 20 69 66 0a 2a 2a 20 6e 6f 20 72  _DONE if.** no r
322a7 6f 77 73 20 65 78 69 73 74 2c 20 61 6e 64 20 61  ows exist, and a
322a8 6e 79 20 65 72 72 6f 72 20 69 6e 20 63 61 73 65  ny error in case
322a9 20 6f 66 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   of failure..*/.
322aa 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74 65  static int conte
322ab 6e 74 5f 65 78 69 73 74 73 28 66 75 6c 6c 74 65  nt_exists(fullte
322ac 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 73  xt_vtab *v){.  s
322ad 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
322ae 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
322af 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
322b0 43 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53 5f 53  CONTENT_EXISTS_S
322b1 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
322b2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
322b3 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
322b4 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
322b5 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
322b6 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
322b7 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 57 65 20 65  n rc;..  /* We e
322b8 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72  xpect only one r
322b9 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65  ow.  We must exe
322ba 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c  cute another sql
322bb 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a  ite3_step().   *
322bc 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
322bd 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65   iteration; othe
322be 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20  rwise the table 
322bf 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b  will remain lock
322c0 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ed. */.  rc = sq
322c1 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
322c2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
322c3 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51  DONE ) return SQ
322c4 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 69 66 28 20  LITE_ROW;.  if( 
322c5 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
322c6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
322c7 52 52 4f 52 3b 0a 20 20 72 65 74 75 72 6e 20 72  RROR;.  return r
322c8 63 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20  c;.}../* insert 
322c9 69 6e 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 20  into %_segments 
322ca 76 61 6c 75 65 73 20 28 5b 70 44 61 74 61 5d 29  values ([pData])
322cb 0a 2a 2a 20 20 20 72 65 74 75 72 6e 73 20 61 73  .**   returns as
322cc 73 69 67 6e 65 64 20 62 6c 6f 63 6b 69 64 20 69  signed blockid i
322cd 6e 20 2a 70 69 42 6c 6f 63 6b 69 64 0a 2a 2f 0a  n *piBlockid.*/.
322ce 73 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 63 6b  static int block
322cf 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74  _insert(fulltext
322d0 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20  _vtab *v, const 
322d1 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74  char *pData, int
322d2 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20   nData,.        
322d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
322d4 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
322d5 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69  Blockid){.  sqli
322d6 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
322d7 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
322d8 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f  statement(v, BLO
322d9 43 4b 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c 20  CK_INSERT_STMT, 
322da 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
322db 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
322dc 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
322dd 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
322de 73 2c 20 31 2c 20 70 44 61 74 61 2c 20 6e 44 61  s, 1, pData, nDa
322df 74 61 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  ta, SQLITE_STATI
322e0 43 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  C);.  if( rc!=SQ
322e1 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
322e2 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
322e3 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
322e4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
322e5 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  OW ) return SQLI
322e6 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20  TE_ERROR;.  if( 
322e7 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
322e8 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
322e9 2f 2a 20 62 6c 6f 63 6b 69 64 20 63 6f 6c 75 6d  /* blockid colum
322ea 6e 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66 6f  n is an alias fo
322eb 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a 70  r rowid. */.  *p
322ec 69 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74  iBlockid = sqlit
322ed 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
322ee 6f 77 69 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72  owid(v->db);.  r
322ef 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
322f0 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74 65 20 66 72  .}../* delete fr
322f1 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 0a 2a 2a  om %_segments.**
322f2 20 20 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64     where blockid
322f3 20 62 65 74 77 65 65 6e 20 5b 69 53 74 61 72 74   between [iStart
322f4 42 6c 6f 63 6b 69 64 5d 20 61 6e 64 20 5b 69 45  Blockid] and [iE
322f5 6e 64 42 6c 6f 63 6b 69 64 5d 0a 2a 2a 0a 2a 2a  ndBlockid].**.**
322f6 20 44 65 6c 65 74 65 73 20 74 68 65 20 72 61 6e   Deletes the ran
322f7 67 65 20 6f 66 20 62 6c 6f 63 6b 73 2c 20 69 6e  ge of blocks, in
322f8 63 6c 75 73 69 76 65 2c 20 75 73 65 64 20 74 6f  clusive, used to
322f9 20 64 65 6c 65 74 65 20 74 68 65 20 62 6c 6f 63   delete the bloc
322fa 6b 73 0a 2a 2a 20 77 68 69 63 68 20 66 6f 72 6d  ks.** which form
322fb 20 61 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 73   a segment..*/.s
322fc 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 63 6b 5f  tatic int block_
322fd 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f  delete(fulltext_
322fe 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20  vtab *v,.       
322ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32300 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53   sqlite_int64 iS
32301 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 73 71 6c  tartBlockid, sql
32302 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c  ite_int64 iEndBl
32303 6f 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  ockid){.  sqlite
32304 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
32305 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
32306 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b  atement(v, BLOCK
32307 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73  _DELETE_STMT, &s
32308 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
32309 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
3230a 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
3230b 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
3230c 2c 20 31 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b  , 1, iStartBlock
3230d 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
3230e 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3230f 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
32310 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
32311 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c 6f 63 6b  (s, 2, iEndBlock
32312 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
32313 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
32314 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20  n rc;..  return 
32315 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28  sql_single_step(
32316 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e  s);.}../* Return
32317 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 77 69 74  s SQLITE_ROW wit
32318 68 20 2a 70 69 64 78 20 73 65 74 20 74 6f 20 74  h *pidx set to t
32319 68 65 20 6d 61 78 69 6d 75 6d 20 73 65 67 6d 65  he maximum segme
3231a 6e 74 20 69 64 78 20 66 6f 75 6e 64 0a 2a 2a 20  nt idx found.** 
3231b 61 74 20 69 4c 65 76 65 6c 2e 20 20 52 65 74 75  at iLevel.  Retu
3231c 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rns SQLITE_DONE 
3231d 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  if there are no 
3231e 73 65 67 6d 65 6e 74 73 20 61 74 0a 2a 2a 20 69  segments at.** i
3231f 4c 65 76 65 6c 2e 20 20 4f 74 68 65 72 77 69 73  Level.  Otherwis
32320 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  e returns an err
32321 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  or..*/.static in
32322 74 20 73 65 67 64 69 72 5f 6d 61 78 5f 69 6e 64  t segdir_max_ind
32323 65 78 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ex(fulltext_vtab
32324 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c   *v, int iLevel,
32325 20 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20 73   int *pidx){.  s
32326 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
32327 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
32328 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
32329 53 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58  SEGDIR_MAX_INDEX
3232a 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
3232b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3232c 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
3232d 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
3232e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65 76  d_int(s, 1, iLev
3232f 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  el);.  if( rc!=S
32330 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
32331 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
32332 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
32333 20 2f 2a 20 53 68 6f 75 6c 64 20 61 6c 77 61 79   /* Should alway
32334 73 20 67 65 74 20 61 74 20 6c 65 61 73 74 20 6f  s get at least o
32335 6e 65 20 72 6f 77 20 64 75 65 20 74 6f 20 68 6f  ne row due to ho
32336 77 20 6d 61 78 28 29 20 77 6f 72 6b 73 2e 20 2a  w max() works. *
32337 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
32338 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e  TE_DONE ) return
32339 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
3233a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
3233b 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OW ) return rc;.
3233c 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6d 65 61 6e 73  .  /* NULL means
3233d 20 74 68 61 74 20 74 68 65 72 65 20 77 65 72 65   that there were
3233e 20 6e 6f 20 69 6e 70 75 74 73 20 74 6f 20 6d 61   no inputs to ma
3233f 78 28 29 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51  x(). */.  if( SQ
32340 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74  LITE_NULL==sqlit
32341 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73  e3_column_type(s
32342 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  , 0) ){.    rc =
32343 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
32344 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
32345 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
32346 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
32347 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
32348 20 7d 0a 0a 20 20 2a 70 69 64 78 20 3d 20 73 71   }..  *pidx = sq
32349 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
3234a 28 73 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 57 65  (s, 0);..  /* We
3234b 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65   expect only one
3234c 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65   row.  We must e
3234d 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73  xecute another s
3234e 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20  qlite3_step().  
3234f 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74   * to complete t
32350 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74  he iteration; ot
32351 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c  herwise the tabl
32352 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f  e will remain lo
32353 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  cked. */.  rc = 
32354 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
32355 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
32356 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53  E_ROW ) return S
32357 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69  QLITE_ERROR;.  i
32358 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  f( rc!=SQLITE_DO
32359 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  NE ) return rc;.
3235a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3235b 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72  ROW;.}../* inser
3235c 74 20 69 6e 74 6f 20 25 5f 73 65 67 64 69 72 20  t into %_segdir 
3235d 76 61 6c 75 65 73 20 28 0a 2a 2a 20 20 20 5b 69  values (.**   [i
3235e 4c 65 76 65 6c 5d 2c 20 5b 69 64 78 5d 2c 0a 2a  Level], [idx],.*
3235f 2a 20 20 20 5b 69 53 74 61 72 74 42 6c 6f 63 6b  *   [iStartBlock
32360 69 64 5d 2c 20 5b 69 4c 65 61 76 65 73 45 6e 64  id], [iLeavesEnd
32361 42 6c 6f 63 6b 69 64 5d 2c 20 5b 69 45 6e 64 42  Blockid], [iEndB
32362 6c 6f 63 6b 69 64 5d 2c 0a 2a 2a 20 20 20 5b 70  lockid],.**   [p
32363 52 6f 6f 74 44 61 74 61 5d 0a 2a 2a 20 29 0a 2a  RootData].** ).*
32364 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67  /.static int seg
32365 64 69 72 5f 73 65 74 28 66 75 6c 6c 74 65 78 74  dir_set(fulltext
32366 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
32367 65 76 65 6c 2c 20 69 6e 74 20 69 64 78 2c 0a 20  evel, int idx,. 
32368 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32369 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
3236a 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  4 iStartBlockid,
3236b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3236c 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
3236d 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 42 6c  t64 iLeavesEndBl
3236e 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
3236f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
32370 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c  ite_int64 iEndBl
32371 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
32372 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
32373 73 74 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61  st char *pRootDa
32374 74 61 2c 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74  ta, int nRootDat
32375 61 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  a){.  sqlite3_st
32376 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20  mt *s;.  int rc 
32377 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
32378 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45  ent(v, SEGDIR_SE
32379 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  T_STMT, &s);.  i
3237a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3237b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
3237c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
3237d 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65  nd_int(s, 1, iLe
3237e 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  vel);.  if( rc!=
3237f 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
32380 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
32381 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
32382 73 2c 20 32 2c 20 69 64 78 29 3b 0a 20 20 69 66  s, 2, idx);.  if
32383 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
32384 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
32385 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
32386 64 5f 69 6e 74 36 34 28 73 2c 20 33 2c 20 69 53  d_int64(s, 3, iS
32387 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  tartBlockid);.  
32388 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
32389 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
3238a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
3238b 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 34 2c 20  ind_int64(s, 4, 
3238c 69 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69  iLeavesEndBlocki
3238d 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
3238e 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3238f 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
32390 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
32391 73 2c 20 35 2c 20 69 45 6e 64 42 6c 6f 63 6b 69  s, 5, iEndBlocki
32392 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
32393 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
32394 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
32395 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73  ite3_bind_blob(s
32396 2c 20 36 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20  , 6, pRootData, 
32397 6e 52 6f 6f 74 44 61 74 61 2c 20 53 51 4c 49 54  nRootData, SQLIT
32398 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28  E_STATIC);.  if(
32399 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3239a 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
3239b 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65  eturn sql_single
3239c 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20  _step(s);.}../* 
3239d 51 75 65 72 69 65 73 20 25 5f 73 65 67 64 69 72  Queries %_segdir
3239e 20 66 6f 72 20 74 68 65 20 62 6c 6f 63 6b 20 73   for the block s
3239f 70 61 6e 20 6f 66 20 74 68 65 20 73 65 67 6d 65  pan of the segme
323a0 6e 74 73 20 69 6e 20 6c 65 76 65 6c 0a 2a 2a 20  nts in level.** 
323a1 69 4c 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 73  iLevel.  Returns
323a2 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 66 20   SQLITE_DONE if 
323a3 74 68 65 72 65 20 61 72 65 20 6e 6f 20 62 6c 6f  there are no blo
323a4 63 6b 73 20 66 6f 72 20 69 4c 65 76 65 6c 2c 0a  cks for iLevel,.
323a5 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57 20 69 66  ** SQLITE_ROW if
323a6 20 74 68 65 72 65 20 61 72 65 20 62 6c 6f 63 6b   there are block
323a7 73 2c 20 65 6c 73 65 20 61 6e 20 65 72 72 6f 72  s, else an error
323a8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
323a9 73 65 67 64 69 72 5f 73 70 61 6e 28 66 75 6c 6c  segdir_span(full
323aa 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e  text_vtab *v, in
323ab 74 20 69 4c 65 76 65 6c 2c 0a 20 20 20 20 20 20  t iLevel,.      
323ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
323ad 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
323ae 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 0a 20  iStartBlockid,. 
323af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
323b0 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
323b1 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  64 *piEndBlockid
323b2 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
323b3 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t *s;.  int rc =
323b4 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
323b5 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 50 41  nt(v, SEGDIR_SPA
323b6 4e 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  N_STMT, &s);.  i
323b7 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
323b8 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
323b9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
323ba 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65  nd_int(s, 1, iLe
323bb 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  vel);.  if( rc!=
323bc 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
323bd 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
323be 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
323bf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
323c0 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53  _DONE ) return S
323c1 51 4c 49 54 45 5f 44 4f 4e 45 3b 20 20 2f 2a 20  QLITE_DONE;  /* 
323c2 53 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70  Should never hap
323c3 70 65 6e 20 2a 2f 0a 20 20 69 66 28 20 72 63 21  pen */.  if( rc!
323c4 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
323c5 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54  turn rc;..  /* T
323c6 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61  his happens if a
323c7 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 74  ll segments at t
323c8 68 69 73 20 6c 65 76 65 6c 20 61 72 65 20 65 6e  his level are en
323c9 74 69 72 65 6c 79 20 69 6e 6c 69 6e 65 2e 20 2a  tirely inline. *
323ca 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e  /.  if( SQLITE_N
323cb 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 63 6f 6c  ULL==sqlite3_col
323cc 75 6d 6e 5f 74 79 70 65 28 73 2c 20 30 29 20 29  umn_type(s, 0) )
323cd 7b 0a 20 20 20 20 2f 2a 20 57 65 20 65 78 70 65  {.    /* We expe
323ce 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e  ct only one row.
323cf 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74    We must execut
323d0 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65  e another sqlite
323d1 33 5f 73 74 65 70 28 29 0a 20 20 20 20 20 2a 20  3_step().     * 
323d2 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20  to complete the 
323d3 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72  iteration; other
323d4 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77  wise the table w
323d5 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65  ill remain locke
323d6 64 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63  d. */.    int rc
323d7 32 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  2 = sqlite3_step
323d8 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32  (s);.    if( rc2
323d9 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  ==SQLITE_ROW ) r
323da 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
323db 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  OR;.    return r
323dc 63 32 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 53 74  c2;.  }..  *piSt
323dd 61 72 74 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c  artBlockid = sql
323de 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
323df 34 28 73 2c 20 30 29 3b 0a 20 20 2a 70 69 45 6e  4(s, 0);.  *piEn
323e0 64 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74  dBlockid = sqlit
323e1 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
323e2 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 57 65 20  s, 1);..  /* We 
323e3 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20  expect only one 
323e4 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78  row.  We must ex
323e5 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71  ecute another sq
323e6 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20  lite3_step().   
323e7 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  * to complete th
323e8 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68  e iteration; oth
323e9 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65  erwise the table
323ea 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63   will remain loc
323eb 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73  ked. */.  rc = s
323ec 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
323ed 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
323ee 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51  _ROW ) return SQ
323ef 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66  LITE_ERROR;.  if
323f0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
323f1 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  E ) return rc;. 
323f2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52   return SQLITE_R
323f3 4f 57 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65  OW;.}../* Delete
323f4 20 74 68 65 20 73 65 67 6d 65 6e 74 20 62 6c 6f   the segment blo
323f5 63 6b 73 20 61 6e 64 20 73 65 67 6d 65 6e 74 20  cks and segment 
323f6 64 69 72 65 63 74 6f 72 79 20 72 65 63 6f 72 64  directory record
323f7 73 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 73 65 67  s for all.** seg
323f8 6d 65 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 2e  ments at iLevel.
323f9 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
323fa 65 67 64 69 72 5f 64 65 6c 65 74 65 28 66 75 6c  egdir_delete(ful
323fb 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
323fc 6e 74 20 69 4c 65 76 65 6c 29 7b 0a 20 20 73 71  nt iLevel){.  sq
323fd 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
323fe 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53   sqlite_int64 iS
323ff 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e  tartBlockid, iEn
32400 64 42 6c 6f 63 6b 69 64 3b 0a 20 20 69 6e 74 20  dBlockid;.  int 
32401 72 63 20 3d 20 73 65 67 64 69 72 5f 73 70 61 6e  rc = segdir_span
32402 28 76 2c 20 69 4c 65 76 65 6c 2c 20 26 69 53 74  (v, iLevel, &iSt
32403 61 72 74 42 6c 6f 63 6b 69 64 2c 20 26 69 45 6e  artBlockid, &iEn
32404 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28  dBlockid);.  if(
32405 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc!=SQLITE_ROW 
32406 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  && rc!=SQLITE_DO
32407 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  NE ) return rc;.
32408 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
32409 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 63 20  E_ROW ){.    rc 
3240a 3d 20 62 6c 6f 63 6b 5f 64 65 6c 65 74 65 28 76  = block_delete(v
3240b 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  , iStartBlockid,
3240c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20   iEndBlockid);. 
3240d 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
3240e 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
3240f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65  ;.  }..  /* Dele
32410 74 65 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64  te the segment d
32411 69 72 65 63 74 6f 72 79 20 69 74 73 65 6c 66 2e  irectory itself.
32412 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67   */.  rc = sql_g
32413 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
32414 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f 53 54  SEGDIR_DELETE_ST
32415 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
32416 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
32417 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
32418 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
32419 6e 74 36 34 28 73 2c 20 31 2c 20 69 4c 65 76 65  nt64(s, 1, iLeve
3241a 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  l);.  if( rc!=SQ
3241b 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3241c 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73   rc;..  return s
3241d 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
3241e 29 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65 20  );.}../* Delete 
3241f 65 6e 74 69 72 65 20 66 74 73 20 69 6e 64 65 78  entire fts index
32420 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  , SQLITE_OK on s
32421 75 63 63 65 73 73 2c 20 72 65 6c 65 76 61 6e 74  uccess, relevant
32422 20 65 72 72 6f 72 20 6f 6e 0a 2a 2a 20 66 61 69   error on.** fai
32423 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lure..*/.static 
32424 69 6e 74 20 73 65 67 64 69 72 5f 64 65 6c 65 74  int segdir_delet
32425 65 5f 61 6c 6c 28 66 75 6c 6c 74 65 78 74 5f 76  e_all(fulltext_v
32426 74 61 62 20 2a 76 29 7b 0a 20 20 73 71 6c 69 74  tab *v){.  sqlit
32427 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e  e3_stmt *s;.  in
32428 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73  t rc = sql_get_s
32429 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44  tatement(v, SEGD
3242a 49 52 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54  IR_DELETE_ALL_ST
3242b 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
3242c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
3242d 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
3242e 3d 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65  = sql_single_ste
3242f 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  p(s);.  if( rc!=
32430 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
32431 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
32432 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
32433 28 76 2c 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45  (v, BLOCK_DELETE
32434 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  _ALL_STMT, &s);.
32435 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32436 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
32437 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73  ..  return sql_s
32438 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d  ingle_step(s);.}
32439 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53 51 4c  ../* Returns SQL
3243a 49 54 45 5f 4f 4b 20 77 69 74 68 20 2a 70 6e 53  ITE_OK with *pnS
3243b 65 67 6d 65 6e 74 73 20 73 65 74 20 74 6f 20 74  egments set to t
3243c 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
3243d 72 69 65 73 20 69 6e 0a 2a 2a 20 25 5f 73 65 67  ries in.** %_seg
3243e 64 69 72 20 61 6e 64 20 2a 70 69 4d 61 78 4c 65  dir and *piMaxLe
3243f 76 65 6c 20 73 65 74 20 74 6f 20 74 68 65 20 68  vel set to the h
32440 69 67 68 65 73 74 20 6c 65 76 65 6c 20 77 68 69  ighest level whi
32441 63 68 20 68 61 73 20 61 0a 2a 2a 20 73 65 67 6d  ch has a.** segm
32442 65 6e 74 2e 20 20 4f 74 68 65 72 77 69 73 65 20  ent.  Otherwise 
32443 72 65 74 75 72 6e 73 20 74 68 65 20 53 51 4c 69  returns the SQLi
32444 74 65 20 65 72 72 6f 72 20 77 68 69 63 68 20 63  te error which c
32445 61 75 73 65 64 20 66 61 69 6c 75 72 65 2e 0a 2a  aused failure..*
32446 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67  /.static int seg
32447 64 69 72 5f 63 6f 75 6e 74 28 66 75 6c 6c 74 65  dir_count(fullte
32448 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20  xt_vtab *v, int 
32449 2a 70 6e 53 65 67 6d 65 6e 74 73 2c 20 69 6e 74  *pnSegments, int
3244a 20 2a 70 69 4d 61 78 4c 65 76 65 6c 29 7b 0a 20   *piMaxLevel){. 
3244b 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
3244c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
3244d 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
3244e 2c 20 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f 53  , SEGDIR_COUNT_S
3244f 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
32450 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
32451 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
32452 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
32453 73 29 3b 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68  s);.  /* TODO(sh
32454 65 73 73 29 3a 20 54 68 69 73 20 63 61 73 65 20  ess): This case 
32455 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 70 6f  should not be po
32456 73 73 69 62 6c 65 3f 20 20 53 68 6f 75 6c 64 20  ssible?  Should 
32457 73 74 72 6f 6e 67 65 72 0a 20 20 2a 2a 20 6d 65  stronger.  ** me
32458 61 73 75 72 65 73 20 62 65 20 74 61 6b 65 6e 20  asures be taken 
32459 69 66 20 69 74 20 68 61 70 70 65 6e 73 3f 0a 20  if it happens?. 
3245a 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
3245b 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
3245c 20 2a 70 6e 53 65 67 6d 65 6e 74 73 20 3d 20 30   *pnSegments = 0
3245d 3b 0a 20 20 20 20 2a 70 69 4d 61 78 4c 65 76 65  ;.    *piMaxLeve
3245e 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72  l = 0;.    retur
3245f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
32460 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
32461 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72  E_ROW ) return r
32462 63 3b 0a 0a 20 20 2a 70 6e 53 65 67 6d 65 6e 74  c;..  *pnSegment
32463 73 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  s = sqlite3_colu
32464 6d 6e 5f 69 6e 74 28 73 2c 20 30 29 3b 0a 20 20  mn_int(s, 0);.  
32465 2a 70 69 4d 61 78 4c 65 76 65 6c 20 3d 20 73 71  *piMaxLevel = sq
32466 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
32467 28 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 57 65  (s, 1);..  /* We
32468 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65   expect only one
32469 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65   row.  We must e
3246a 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73  xecute another s
3246b 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20  qlite3_step().  
3246c 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74   * to complete t
3246d 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74  he iteration; ot
3246e 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c  herwise the tabl
3246f 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f  e will remain lo
32470 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  cked. */.  rc = 
32471 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
32472 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
32473 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
32474 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
32475 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc==SQLITE_ROW 
32476 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
32477 45 52 52 4f 52 3b 0a 20 20 72 65 74 75 72 6e 20  ERROR;.  return 
32478 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73  rc;.}../* TODO(s
32479 68 65 73 73 29 20 63 6c 65 61 72 50 65 6e 64 69  hess) clearPendi
3247a 6e 67 54 65 72 6d 73 28 29 20 69 73 20 66 61 72  ngTerms() is far
3247b 20 64 6f 77 6e 20 74 68 65 20 66 69 6c 65 20 62   down the file b
3247c 65 63 61 75 73 65 0a 2a 2a 20 77 72 69 74 65 5a  ecause.** writeZ
3247d 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 69 73 20  eroSegment() is 
3247e 66 61 72 20 64 6f 77 6e 20 74 68 65 20 66 69 6c  far down the fil
3247f 65 20 62 65 63 61 75 73 65 20 4c 65 61 66 57 72  e because LeafWr
32480 69 74 65 72 20 69 73 20 66 61 72 0a 2a 2a 20 64  iter is far.** d
32481 6f 77 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 43  own the file.  C
32482 6f 6e 73 69 64 65 72 20 72 65 66 61 63 74 6f 72  onsider refactor
32483 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74 6f 20  ing the code to 
32484 6d 6f 76 65 20 74 68 65 20 6e 6f 6e 2d 76 74 61  move the non-vta
32485 62 0a 2a 2a 20 63 6f 64 65 20 61 62 6f 76 65 20  b.** code above 
32486 74 68 65 20 76 74 61 62 20 63 6f 64 65 20 73 6f  the vtab code so
32487 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 6e   that we don't n
32488 65 65 64 20 74 68 69 73 20 66 6f 72 77 61 72 64  eed this forward
32489 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a  .** reference..*
3248a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65  /.static int cle
3248b 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66  arPendingTerms(f
3248c 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
3248d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68  ;../*.** Free th
3248e 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f  e memory used to
3248f 20 63 6f 6e 74 61 69 6e 20 61 20 66 75 6c 6c 74   contain a fullt
32490 65 78 74 5f 76 74 61 62 20 73 74 72 75 63 74 75  ext_vtab structu
32491 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  re..*/.static vo
32492 69 64 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  id fulltext_vtab
32493 5f 64 65 73 74 72 6f 79 28 66 75 6c 6c 74 65 78  _destroy(fulltex
32494 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69 6e  t_vtab *v){.  in
32495 74 20 69 53 74 6d 74 2c 20 69 3b 0a 0a 20 20 46  t iStmt, i;..  F
32496 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 44  TSTRACE(("FTS3 D
32497 65 73 74 72 6f 79 20 25 70 5c 6e 22 2c 20 76 29  estroy %p\n", v)
32498 29 3b 0a 20 20 66 6f 72 28 20 69 53 74 6d 74 3d  );.  for( iStmt=
32499 30 3b 20 69 53 74 6d 74 3c 4d 41 58 5f 53 54 4d  0; iStmt<MAX_STM
3249a 54 3b 20 69 53 74 6d 74 2b 2b 20 29 7b 0a 20 20  T; iStmt++ ){.  
3249b 20 20 69 66 28 20 76 2d 3e 70 46 75 6c 6c 74 65    if( v->pFullte
3249c 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74  xtStatements[iSt
3249d 6d 74 5d 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  mt]!=NULL ){.   
3249e 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
3249f 69 7a 65 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74  ize(v->pFulltext
324a0 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74  Statements[iStmt
324a1 5d 29 3b 0a 20 20 20 20 20 20 76 2d 3e 70 46 75  ]);.      v->pFu
324a2 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
324a3 5b 69 53 74 6d 74 5d 20 3d 20 4e 55 4c 4c 3b 0a  [iStmt] = NULL;.
324a4 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72      }.  }..  for
324a5 28 20 69 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43  ( i=0; i<MERGE_C
324a6 4f 55 4e 54 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  OUNT; i++ ){.   
324a7 20 69 66 28 20 76 2d 3e 70 4c 65 61 66 53 65 6c   if( v->pLeafSel
324a8 65 63 74 53 74 6d 74 73 5b 69 5d 21 3d 4e 55 4c  ectStmts[i]!=NUL
324a9 4c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L ){.      sqlit
324aa 65 33 5f 66 69 6e 61 6c 69 7a 65 28 76 2d 3e 70  e3_finalize(v->p
324ab 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b  LeafSelectStmts[
324ac 69 5d 29 3b 0a 20 20 20 20 20 20 76 2d 3e 70 4c  i]);.      v->pL
324ad 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69  eafSelectStmts[i
324ae 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a  ] = NULL;.    }.
324af 20 20 7d 0a 0a 20 20 69 66 28 20 76 2d 3e 70 54    }..  if( v->pT
324b0 6f 6b 65 6e 69 7a 65 72 21 3d 4e 55 4c 4c 20 29  okenizer!=NULL )
324b1 7b 0a 20 20 20 20 76 2d 3e 70 54 6f 6b 65 6e 69  {.    v->pTokeni
324b2 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44  zer->pModule->xD
324b3 65 73 74 72 6f 79 28 76 2d 3e 70 54 6f 6b 65 6e  estroy(v->pToken
324b4 69 7a 65 72 29 3b 0a 20 20 20 20 76 2d 3e 70 54  izer);.    v->pT
324b5 6f 6b 65 6e 69 7a 65 72 20 3d 20 4e 55 4c 4c 3b  okenizer = NULL;
324b6 0a 20 20 7d 0a 0a 20 20 63 6c 65 61 72 50 65 6e  .  }..  clearPen
324b7 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 0a 20  dingTerms(v);.. 
324b8 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d   sqlite3_free(v-
324b9 3e 61 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 66 6f  >azColumn);.  fo
324ba 72 28 69 20 3d 20 30 3b 20 69 20 3c 20 76 2d 3e  r(i = 0; i < v->
324bb 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a  nColumn; ++i) {.
324bc 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
324bd 28 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c  (v->azContentCol
324be 75 6d 6e 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73  umn[i]);.  }.  s
324bf 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d 3e 61  qlite3_free(v->a
324c0 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b  zContentColumn);
324c1 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
324c2 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b  v);.}../*.** Tok
324c3 65 6e 20 74 79 70 65 73 20 66 6f 72 20 70 61 72  en types for par
324c4 73 69 6e 67 20 74 68 65 20 61 72 67 75 6d 65 6e  sing the argumen
324c5 74 73 20 74 6f 20 78 43 6f 6e 6e 65 63 74 20 6f  ts to xConnect o
324c6 72 20 78 43 72 65 61 74 65 2e 0a 2a 2f 0a 23 64  r xCreate..*/.#d
324c7 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 45 4f 46 20  efine TOKEN_EOF 
324c8 20 20 20 20 20 20 20 20 30 20 20 20 20 2f 2a 20          0    /* 
324c9 45 6e 64 20 6f 66 20 66 69 6c 65 20 2a 2f 0a 23  End of file */.#
324ca 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 53 50 41  define TOKEN_SPA
324cb 43 45 20 20 20 20 20 20 20 31 20 20 20 20 2f 2a  CE       1    /*
324cc 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 77 68 69   Any kind of whi
324cd 74 65 73 70 61 63 65 20 2a 2f 0a 23 64 65 66 69  tespace */.#defi
324ce 6e 65 20 54 4f 4b 45 4e 5f 49 44 20 20 20 20 20  ne TOKEN_ID     
324cf 20 20 20 20 20 32 20 20 20 20 2f 2a 20 41 6e 20       2    /* An 
324d0 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 23 64  identifier */.#d
324d1 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 53 54 52 49  efine TOKEN_STRI
324d2 4e 47 20 20 20 20 20 20 33 20 20 20 20 2f 2a 20  NG      3    /* 
324d3 41 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c  A string literal
324d4 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45   */.#define TOKE
324d5 4e 5f 50 55 4e 43 54 20 20 20 20 20 20 20 34 20  N_PUNCT       4 
324d6 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 70     /* A single p
324d7 75 6e 63 74 75 61 74 69 6f 6e 20 63 68 61 72 61  unctuation chara
324d8 63 74 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  cter */../*.** I
324d9 66 20 58 20 69 73 20 61 20 63 68 61 72 61 63 74  f X is a charact
324da 65 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  er that can be u
324db 73 65 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69  sed in an identi
324dc 66 69 65 72 20 74 68 65 6e 0a 2a 2a 20 66 74 73  fier then.** fts
324dd 49 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20 62  IdChar(X) will b
324de 65 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69  e true.  Otherwi
324df 73 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a  se it is false..
324e0 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49 2c  **.** For ASCII,
324e1 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77   any character w
324e2 69 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72 64  ith the high-ord
324e3 65 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a 2a  er bit set is.**
324e4 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20 69   allowed in an i
324e5 64 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72 20  dentifier.  For 
324e6 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 73  7-bit characters
324e7 2c 20 0a 2a 2a 20 69 73 46 74 73 49 64 43 68 61  , .** isFtsIdCha
324e8 72 5b 58 5d 20 6d 75 73 74 20 62 65 20 31 2e 0a  r[X] must be 1..
324e9 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 30  **.** Ticket #10
324ea 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73 74 61  66.  the SQL sta
324eb 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74 20 61  ndard does not a
324ec 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68 65 0a  llow '$' in the.
324ed 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69 64 65  ** middle of ide
324ee 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20 6d 61  ntfiers.  But ma
324ef 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65 6e 74  ny SQL implement
324f0 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a 20 53  ations do. .** S
324f1 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f 77  QLite will allow
324f2 20 27 24 27 20 69 6e 20 69 64 65 6e 74 69 66 69   '$' in identifi
324f3 65 72 73 20 66 6f 72 20 63 6f 6d 70 61 74 69 62  ers for compatib
324f4 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 74 68  ility..** But th
324f5 65 20 66 65 61 74 75 72 65 20 69 73 20 75 6e 64  e feature is und
324f6 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 73 74  ocumented..*/.st
324f7 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
324f8 69 73 46 74 73 49 64 43 68 61 72 5b 5d 20 3d 20  isFtsIdChar[] = 
324f9 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33  {./* x0 x1 x2 x3
324fa 20 78 34 20 78 35 20 78 36 20 78 37 20 78 38 20   x4 x5 x6 x7 x8 
324fb 78 39 20 78 41 20 78 42 20 78 43 20 78 44 20 78  x9 xA xB xC xD x
324fc 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30  E xF */.    0, 0
324fd 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c  , 0, 0, 1, 0, 0,
324fe 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
324ff 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
32500 32 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  2x */.    1, 1, 
32501 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32502 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
32503 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78   0, 0, 0,  /* 3x
32504 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
32505 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32506 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32507 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a  , 1, 1,  /* 4x *
32508 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
32509 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3250a 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
3250b 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a  0, 1,  /* 5x */.
3250c 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
3250d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3250e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3250f 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   1,  /* 6x */.  
32510 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
32511 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32512 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
32513 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23  ,  /* 7x */.};.#
32514 64 65 66 69 6e 65 20 66 74 73 49 64 43 68 61 72  define ftsIdChar
32515 28 43 29 20 20 28 28 28 63 3d 43 29 26 30 78 38  (C)  (((c=C)&0x8
32516 30 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66  0)!=0 || (c>0x1f
32517 20 26 26 20 69 73 46 74 73 49 64 43 68 61 72 5b   && isFtsIdChar[
32518 63 2d 30 78 32 30 5d 29 29 0a 0a 0a 2f 2a 0a 2a  c-0x20])).../*.*
32519 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e  * Return the len
3251a 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  gth of the token
3251b 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 74 20   that begins at 
3251c 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72 65 20  z[0]. .** Store 
3251d 74 68 65 20 74 6f 6b 65 6e 20 74 79 70 65 20 69  the token type i
3251e 6e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 62 65 66  n *tokenType bef
3251f 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
32520 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
32521 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 63  GetToken(const c
32522 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b  har *z, int *tok
32523 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 69  enType){.  int i
32524 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20 2a  , c;.  switch( *
32525 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 30 3a  z ){.    case 0:
32526 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
32527 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 45 4f 46 3b  ype = TOKEN_EOF;
32528 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
32529 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
3252a 27 20 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20  ' ': case '\t': 
3252b 63 61 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20  case '\n': case 
3252c 27 5c 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a  '\f': case '\r':
3252d 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31   {.      for(i=1
3252e 3b 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 7a  ; safe_isspace(z
3252f 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
32530 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
32531 54 4f 4b 45 4e 5f 53 50 41 43 45 3b 0a 20 20 20  TOKEN_SPACE;.   
32532 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
32533 20 7d 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a   }.    case '`':
32534 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0a  .    case '\'':.
32535 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 7b 0a      case '"': {.
32536 20 20 20 20 20 20 69 6e 74 20 64 65 6c 69 6d 20        int delim 
32537 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20 20 20 66 6f  = z[0];.      fo
32538 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=1; (c=z[i])!
32539 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  =0; i++){.      
3253a 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29    if( c==delim )
3253b 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
3253c 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b  z[i+1]==delim ){
3253d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b  .            i++
3253e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
3253f 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  e{.            b
32540 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
32541 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
32542 20 20 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e    }.      *token
32543 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 53 54 52  Type = TOKEN_STR
32544 49 4e 47 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ING;.      retur
32545 6e 20 69 20 2b 20 28 63 21 3d 30 29 3b 0a 20 20  n i + (c!=0);.  
32546 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 5b 27    }.    case '['
32547 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  : {.      for(i=
32548 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27 5d  1, c=z[0]; c!=']
32549 27 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30  ' && (c=z[i])!=0
3254a 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a  ; i++){}.      *
3254b 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45  tokenType = TOKE
3254c 4e 5f 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75  N_ID;.      retu
3254d 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn i;.    }.    
3254e 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
3254f 20 69 66 28 20 21 66 74 73 49 64 43 68 61 72 28   if( !ftsIdChar(
32550 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  *z) ){.        b
32551 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
32552 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 66 74 73      for(i=1; fts
32553 49 64 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b  IdChar(z[i]); i+
32554 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65  +){}.      *toke
32555 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 49 44  nType = TOKEN_ID
32556 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
32557 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74  ;.    }.  }.  *t
32558 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e  okenType = TOKEN
32559 5f 50 55 4e 43 54 3b 0a 20 20 72 65 74 75 72 6e  _PUNCT;.  return
3255a 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 74   1;.}../*.** A t
3255b 6f 6b 65 6e 20 65 78 74 72 61 63 74 65 64 20 66  oken extracted f
3255c 72 6f 6d 20 61 20 73 74 72 69 6e 67 20 69 73 20  rom a string is 
3255d 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
3255e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
3255f 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79  structure..*/.ty
32560 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73  pedef struct Fts
32561 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20  Token {.  const 
32562 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 2f  char *z;       /
32563 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b  * Pointer to tok
32564 65 6e 20 74 65 78 74 2e 20 20 4e 6f 74 20 27 5c  en text.  Not '\
32565 30 30 30 27 20 74 65 72 6d 69 6e 61 74 65 64 20  000' terminated 
32566 2a 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74 20 6e  */.  short int n
32567 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e  ;         /* Len
32568 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  gth of the token
32569 20 74 65 78 74 20 69 6e 20 62 79 74 65 73 2e 20   text in bytes. 
3256a 2a 2f 0a 7d 20 46 74 73 54 6f 6b 65 6e 3b 0a 0a  */.} FtsToken;..
3256b 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 69 6e  /*.** Given a in
3256c 70 75 74 20 73 74 72 69 6e 67 20 28 77 68 69 63  put string (whic
3256d 68 20 69 73 20 72 65 61 6c 6c 79 20 6f 6e 65 20  h is really one 
3256e 6f 66 20 74 68 65 20 61 72 67 76 5b 5d 20 70 61  of the argv[] pa
3256f 72 61 6d 65 74 65 72 73 0a 2a 2a 20 70 61 73 73  rameters.** pass
32570 65 64 20 69 6e 74 6f 20 78 43 6f 6e 6e 65 63 74  ed into xConnect
32571 20 6f 72 20 78 43 72 65 61 74 65 29 20 73 70 6c   or xCreate) spl
32572 69 74 20 74 68 65 20 73 74 72 69 6e 67 20 75 70  it the string up
32573 20 69 6e 74 6f 20 74 6f 6b 65 6e 73 2e 0a 2a 2a   into tokens..**
32574 20 52 65 74 75 72 6e 20 61 6e 20 61 72 72 61 79   Return an array
32575 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
32576 27 5c 30 30 30 27 20 74 65 72 6d 69 6e 61 74 65  '\000' terminate
32577 64 20 73 74 72 69 6e 67 73 2c 20 6f 6e 65 20 73  d strings, one s
32578 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 65 61 63  tring.** for eac
32579 68 20 6e 6f 6e 2d 77 68 69 74 65 73 70 61 63 65  h non-whitespace
3257a 20 74 6f 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68   token..**.** Th
3257b 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61 79  e returned array
3257c 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64 20 62   is terminated b
3257d 79 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20  y a single NULL 
3257e 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 53  pointer..**.** S
3257f 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
32580 20 72 65 74 75 72 6e 65 64 20 61 72 72 61 79 20   returned array 
32581 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
32582 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d 61 6c   a single.** mal
32583 6c 6f 63 20 61 6e 64 20 73 68 6f 75 6c 64 20 62  loc and should b
32584 65 20 66 72 65 65 64 20 62 79 20 70 61 73 73 69  e freed by passi
32585 6e 67 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ng the return va
32586 6c 75 65 20 74 6f 20 66 72 65 65 28 29 2e 0a 2a  lue to free()..*
32587 2a 20 54 68 65 20 69 6e 64 69 76 69 64 75 61 6c  * The individual
32588 20 73 74 72 69 6e 67 73 20 77 69 74 68 69 6e 20   strings within 
32589 74 68 65 20 74 6f 6b 65 6e 20 6c 69 73 74 20 61  the token list a
3258a 72 65 20 61 6c 6c 20 61 20 70 61 72 74 20 6f 66  re all a part of
3258b 0a 2a 2a 20 74 68 65 20 73 69 6e 67 6c 65 20 6d  .** the single m
3258c 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
3258d 20 61 6e 64 20 77 69 6c 6c 20 61 6c 6c 20 62 65   and will all be
3258e 20 66 72 65 65 64 20 61 74 20 6f 6e 63 65 2e 0a   freed at once..
3258f 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
32590 2a 74 6f 6b 65 6e 69 7a 65 53 74 72 69 6e 67 28  *tokenizeString(
32591 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
32592 6e 74 20 2a 70 6e 54 6f 6b 65 6e 29 7b 0a 20 20  nt *pnToken){.  
32593 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a  int nToken = 0;.
32594 20 20 46 74 73 54 6f 6b 65 6e 20 2a 61 54 6f 6b    FtsToken *aTok
32595 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  en = sqlite3_mal
32596 6c 6f 63 28 20 73 74 72 6c 65 6e 28 7a 29 20 2a  loc( strlen(z) *
32597 20 73 69 7a 65 6f 66 28 61 54 6f 6b 65 6e 5b 30   sizeof(aToken[0
32598 5d 29 20 29 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  ]) );.  int n = 
32599 31 3b 0a 20 20 69 6e 74 20 65 2c 20 69 3b 0a 20  1;.  int e, i;. 
3259a 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 20 3d   int totalSize =
3259b 20 30 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 54   0;.  char **azT
3259c 6f 6b 65 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 43  oken;.  char *zC
3259d 6f 70 79 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e  opy;.  while( n>
3259e 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73  0 ){.    n = fts
3259f 47 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 65 29 3b  GetToken(z, &e);
325a0 0a 20 20 20 20 69 66 28 20 65 21 3d 54 4f 4b 45  .    if( e!=TOKE
325a1 4e 5f 53 50 41 43 45 20 29 7b 0a 20 20 20 20 20  N_SPACE ){.     
325a2 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e   aToken[nToken].
325a3 7a 20 3d 20 7a 3b 0a 20 20 20 20 20 20 61 54 6f  z = z;.      aTo
325a4 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e 6e 20 3d 20  ken[nToken].n = 
325a5 6e 3b 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e 2b  n;.      nToken+
325a6 2b 3b 0a 20 20 20 20 20 20 74 6f 74 61 6c 53 69  +;.      totalSi
325a7 7a 65 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20 7d  ze += n+1;.    }
325a8 0a 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 7d  .    z += n;.  }
325a9 0a 20 20 61 7a 54 6f 6b 65 6e 20 3d 20 28 63 68  .  azToken = (ch
325aa 61 72 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ar**)sqlite3_mal
325ab 6c 6f 63 28 20 6e 54 6f 6b 65 6e 2a 73 69 7a 65  loc( nToken*size
325ac 6f 66 28 63 68 61 72 2a 29 20 2b 20 74 6f 74 61  of(char*) + tota
325ad 6c 53 69 7a 65 20 29 3b 0a 20 20 7a 43 6f 70 79  lSize );.  zCopy
325ae 20 3d 20 28 63 68 61 72 2a 29 26 61 7a 54 6f 6b   = (char*)&azTok
325af 65 6e 5b 6e 54 6f 6b 65 6e 5d 3b 0a 20 20 6e 54  en[nToken];.  nT
325b0 6f 6b 65 6e 2d 2d 3b 0a 20 20 66 6f 72 28 69 3d  oken--;.  for(i=
325b1 30 3b 20 69 3c 6e 54 6f 6b 65 6e 3b 20 69 2b 2b  0; i<nToken; i++
325b2 29 7b 0a 20 20 20 20 61 7a 54 6f 6b 65 6e 5b 69  ){.    azToken[i
325b3 5d 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20 20 6e  ] = zCopy;.    n
325b4 20 3d 20 61 54 6f 6b 65 6e 5b 69 5d 2e 6e 3b 0a   = aToken[i].n;.
325b5 20 20 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70 79      memcpy(zCopy
325b6 2c 20 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 2c 20 6e  , aToken[i].z, n
325b7 29 3b 0a 20 20 20 20 7a 43 6f 70 79 5b 6e 5d 20  );.    zCopy[n] 
325b8 3d 20 30 3b 0a 20 20 20 20 7a 43 6f 70 79 20 2b  = 0;.    zCopy +
325b9 3d 20 6e 2b 31 3b 0a 20 20 7d 0a 20 20 61 7a 54  = n+1;.  }.  azT
325ba 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 20 3d 20 30  oken[nToken] = 0
325bb 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
325bc 28 61 54 6f 6b 65 6e 29 3b 0a 20 20 2a 70 6e 54  (aToken);.  *pnT
325bd 6f 6b 65 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20  oken = nToken;. 
325be 20 72 65 74 75 72 6e 20 61 7a 54 6f 6b 65 6e 3b   return azToken;
325bf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
325c0 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71  t an SQL-style q
325c1 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e 74  uoted string int
325c2 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e  o a normal strin
325c3 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a  g by removing.**
325c4 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72 61   the quote chara
325c5 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e 76  cters.  The conv
325c6 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69  ersion is done i
325c7 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68 65  n-place.  If the
325c8 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 20 6e  .** input does n
325c9 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
325ca 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c  quote character,
325cb 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
325cc 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70  ne.** is a no-op
325cd 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 73  ..**.** Examples
325ce 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 61 62 63  :.**.**     "abc
325cf 22 20 20 20 62 65 63 6f 6d 65 73 20 20 20 61 62  "   becomes   ab
325d0 63 0a 2a 2a 20 20 20 20 20 27 78 79 7a 27 20 20  c.**     'xyz'  
325d1 20 62 65 63 6f 6d 65 73 20 20 20 78 79 7a 0a 2a   becomes   xyz.*
325d2 2a 20 20 20 20 20 5b 70 71 72 5d 20 20 20 62 65  *     [pqr]   be
325d3 63 6f 6d 65 73 20 20 20 70 71 72 0a 2a 2a 20 20  comes   pqr.**  
325d4 20 20 20 60 6d 6e 6f 60 20 20 20 62 65 63 6f 6d     `mno`   becom
325d5 65 73 20 20 20 6d 6e 6f 0a 2a 2f 0a 73 74 61 74  es   mno.*/.stat
325d6 69 63 20 76 6f 69 64 20 64 65 71 75 6f 74 65 53  ic void dequoteS
325d7 74 72 69 6e 67 28 63 68 61 72 20 2a 7a 29 7b 0a  tring(char *z){.
325d8 20 20 69 6e 74 20 71 75 6f 74 65 3b 0a 20 20 69    int quote;.  i
325d9 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a  nt i, j;.  if( z
325da 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
325db 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20  quote = z[0];.  
325dc 73 77 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b  switch( quote ){
325dd 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20  .    case '\'': 
325de 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
325df 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20   '"':   break;. 
325e0 20 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 62     case '`':   b
325e1 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20  reak;           
325e2 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51       /* For MySQ
325e3 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  L compatibility 
325e4 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a  */.    case '[':
325e5 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20     quote = ']'; 
325e6 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20   break;  /* For 
325e7 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d  MS SqlServer com
325e8 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20  patibility */.  
325e9 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65    default:    re
325ea 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
325eb 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20  i=1, j=0; z[i]; 
325ec 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b  i++){.    if( z[
325ed 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20  i]==quote ){.   
325ee 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71     if( z[i+1]==q
325ef 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  uote ){.        
325f0 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a  z[j++] = quote;.
325f1 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
325f2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
325f3 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20    z[j++] = 0;.  
325f4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
325f5 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
325f6 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
325f7 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  [i];.    }.  }.}
325f8 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ../*.** The inpu
325f9 74 20 61 7a 49 6e 20 69 73 20 61 20 4e 55 4c 4c  t azIn is a NULL
325fa 2d 74 65 72 6d 69 6e 61 74 65 64 20 6c 69 73 74  -terminated list
325fb 20 6f 66 20 74 6f 6b 65 6e 73 2e 20 20 52 65 6d   of tokens.  Rem
325fc 6f 76 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a  ove the first.**
325fd 20 74 6f 6b 65 6e 20 61 6e 64 20 61 6c 6c 20 70   token and all p
325fe 75 6e 63 74 75 61 74 69 6f 6e 20 74 6f 6b 65 6e  unctuation token
325ff 73 2e 20 20 52 65 6d 6f 76 65 20 74 68 65 20 71  s.  Remove the q
32600 75 6f 74 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 72  uotes from.** ar
32601 6f 75 6e 64 20 73 74 72 69 6e 67 20 6c 69 74 65  ound string lite
32602 72 61 6c 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 0a 2a  ral tokens..**.*
32603 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  * Example:.**.**
32604 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20       input:     
32605 20 74 6f 6b 65 6e 69 7a 65 20 63 68 69 6e 65 73   tokenize chines
32606 65 20 28 20 27 73 69 6d 70 6c 69 66 65 64 27 20  e ( 'simplifed' 
32607 2c 20 27 6d 69 78 65 64 27 20 29 0a 2a 2a 20 20  , 'mixed' ).**  
32608 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 20 63     output:     c
32609 68 69 6e 65 73 65 20 73 69 6d 70 6c 69 66 65 64  hinese simplifed
3260a 20 6d 69 78 65 64 0a 2a 2a 0a 2a 2a 20 41 6e 6f   mixed.**.** Ano
3260b 74 68 65 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a  ther example:.**
3260c 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20  .**     input:  
3260d 20 20 20 20 64 65 6c 69 6d 69 74 65 72 73 20 28      delimiters (
3260e 20 27 5b 27 20 2c 20 27 5d 27 20 2c 20 27 2e 2e   '[' , ']' , '..
3260f 2e 27 20 29 0a 2a 2a 20 20 20 20 20 6f 75 74 70  .' ).**     outp
32610 75 74 3a 20 20 20 20 20 5b 20 5d 20 2e 2e 2e 0a  ut:     [ ] ....
32611 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
32612 6f 6b 65 6e 4c 69 73 74 54 6f 49 64 4c 69 73 74  okenListToIdList
32613 28 63 68 61 72 20 2a 2a 61 7a 49 6e 29 7b 0a 20  (char **azIn){. 
32614 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28   int i, j;.  if(
32615 20 61 7a 49 6e 20 29 7b 0a 20 20 20 20 66 6f 72   azIn ){.    for
32616 28 69 3d 30 2c 20 6a 3d 2d 31 3b 20 61 7a 49 6e  (i=0, j=-1; azIn
32617 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  [i]; i++){.     
32618 20 69 66 28 20 73 61 66 65 5f 69 73 61 6c 6e 75   if( safe_isalnu
32619 6d 28 61 7a 49 6e 5b 69 5d 5b 30 5d 29 20 7c 7c  m(azIn[i][0]) ||
3261a 20 61 7a 49 6e 5b 69 5d 5b 31 5d 20 29 7b 0a 20   azIn[i][1] ){. 
3261b 20 20 20 20 20 20 20 64 65 71 75 6f 74 65 53 74         dequoteSt
3261c 72 69 6e 67 28 61 7a 49 6e 5b 69 5d 29 3b 0a 20  ring(azIn[i]);. 
3261d 20 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20         if( j>=0 
3261e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 7a 49  ){.          azI
3261f 6e 5b 6a 5d 20 3d 20 61 7a 49 6e 5b 69 5d 3b 0a  n[j] = azIn[i];.
32620 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
32621 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20    j++;.      }. 
32622 20 20 20 7d 0a 20 20 20 20 61 7a 49 6e 5b 6a 5d     }.    azIn[j]
32623 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a   = 0;.  }.}.../*
32624 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72  .** Find the fir
32625 73 74 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20  st alphanumeric 
32626 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 74 72  token in the str
32627 69 6e 67 20 7a 49 6e 2e 20 20 4e 75 6c 6c 2d 74  ing zIn.  Null-t
32628 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 69 73  erminate.** this
32629 20 74 6f 6b 65 6e 2e 20 20 52 65 6d 6f 76 65 20   token.  Remove 
3262a 61 6e 79 20 71 75 6f 74 61 74 69 6f 6e 20 6d 61  any quotation ma
3262b 72 6b 73 2e 20 20 41 6e 64 20 72 65 74 75 72 6e  rks.  And return
3262c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
3262d 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a   the result..*/.
3262e 73 74 61 74 69 63 20 63 68 61 72 20 2a 66 69 72  static char *fir
3262f 73 74 54 6f 6b 65 6e 28 63 68 61 72 20 2a 7a 49  stToken(char *zI
32630 6e 2c 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  n, char **pzTail
32631 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 74 74 79 70  ){.  int n, ttyp
32632 65 3b 0a 20 20 77 68 69 6c 65 28 31 29 7b 0a 20  e;.  while(1){. 
32633 20 20 20 6e 20 3d 20 66 74 73 47 65 74 54 6f 6b     n = ftsGetTok
32634 65 6e 28 7a 49 6e 2c 20 26 74 74 79 70 65 29 3b  en(zIn, &ttype);
32635 0a 20 20 20 20 69 66 28 20 74 74 79 70 65 3d 3d  .    if( ttype==
32636 54 4f 4b 45 4e 5f 53 50 41 43 45 20 29 7b 0a 20  TOKEN_SPACE ){. 
32637 20 20 20 20 20 7a 49 6e 20 2b 3d 20 6e 3b 0a 20       zIn += n;. 
32638 20 20 20 7d 65 6c 73 65 20 69 66 28 20 74 74 79     }else if( tty
32639 70 65 3d 3d 54 4f 4b 45 4e 5f 45 4f 46 20 29 7b  pe==TOKEN_EOF ){
3263a 0a 20 20 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d  .      *pzTail =
3263b 20 7a 49 6e 3b 0a 20 20 20 20 20 20 72 65 74 75   zIn;.      retu
3263c 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rn 0;.    }else{
3263d 0a 20 20 20 20 20 20 7a 49 6e 5b 6e 5d 20 3d 20  .      zIn[n] = 
3263e 30 3b 0a 20 20 20 20 20 20 2a 70 7a 54 61 69 6c  0;.      *pzTail
3263f 20 3d 20 26 7a 49 6e 5b 31 5d 3b 0a 20 20 20 20   = &zIn[1];.    
32640 20 20 64 65 71 75 6f 74 65 53 74 72 69 6e 67 28    dequoteString(
32641 7a 49 6e 29 3b 0a 20 20 20 20 20 20 72 65 74 75  zIn);.      retu
32642 72 6e 20 7a 49 6e 3b 0a 20 20 20 20 7d 0a 20 20  rn zIn;.    }.  
32643 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44  }.  /*NOTREACHED
32644 2a 2f 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20  */.}../* Return 
32645 74 72 75 65 20 69 66 2e 2e 2e 0a 2a 2a 0a 2a 2a  true if....**.**
32646 20 20 20 2a 20 20 73 20 62 65 67 69 6e 73 20 77     *  s begins w
32647 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 20 74  ith the string t
32648 2c 20 69 67 6e 6f 72 69 6e 67 20 63 61 73 65 0a  , ignoring case.
32649 2a 2a 20 20 20 2a 20 20 73 20 69 73 20 6c 6f 6e  **   *  s is lon
3264a 67 65 72 20 74 68 61 6e 20 74 0a 2a 2a 20 20 20  ger than t.**   
3264b 2a 20 20 54 68 65 20 66 69 72 73 74 20 63 68 61  *  The first cha
3264c 72 61 63 74 65 72 20 6f 66 20 73 20 62 65 79 6f  racter of s beyo
3264d 6e 64 20 74 20 69 73 20 6e 6f 74 20 61 20 61 6c  nd t is not a al
3264e 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a 20 0a 2a  phanumeric.** .*
3264f 2a 20 49 67 6e 6f 72 65 20 6c 65 61 64 69 6e 67  * Ignore leading
32650 20 73 70 61 63 65 20 69 6e 20 2a 73 2e 0a 2a 2a   space in *s..**
32651 0a 2a 2a 20 54 6f 20 70 75 74 20 69 74 20 61 6e  .** To put it an
32652 6f 74 68 65 72 20 77 61 79 2c 20 72 65 74 75 72  other way, retur
32653 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66 69  n true if the fi
32654 72 73 74 20 74 6f 6b 65 6e 20 6f 66 0a 2a 2a 20  rst token of.** 
32655 73 5b 5d 20 69 73 20 74 5b 5d 2e 0a 2a 2f 0a 73  s[] is t[]..*/.s
32656 74 61 74 69 63 20 69 6e 74 20 73 74 61 72 74 73  tatic int starts
32657 57 69 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20  With(const char 
32658 2a 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *s, const char *
32659 74 29 7b 0a 20 20 77 68 69 6c 65 28 20 73 61 66  t){.  while( saf
3265a 65 5f 69 73 73 70 61 63 65 28 2a 73 29 20 29 7b  e_isspace(*s) ){
3265b 20 73 2b 2b 3b 20 7d 0a 20 20 77 68 69 6c 65 28   s++; }.  while(
3265c 20 2a 74 20 29 7b 0a 20 20 20 20 69 66 28 20 73   *t ){.    if( s
3265d 61 66 65 5f 74 6f 6c 6f 77 65 72 28 2a 73 2b 2b  afe_tolower(*s++
3265e 29 21 3d 73 61 66 65 5f 74 6f 6c 6f 77 65 72 28  )!=safe_tolower(
3265f 2a 74 2b 2b 29 20 29 20 72 65 74 75 72 6e 20 30  *t++) ) return 0
32660 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a  ;.  }.  return *
32661 73 21 3d 27 5f 27 20 26 26 20 21 73 61 66 65 5f  s!='_' && !safe_
32662 69 73 61 6c 6e 75 6d 28 2a 73 29 3b 0a 7d 0a 0a  isalnum(*s);.}..
32663 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
32664 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
32665 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ure defines the 
32666 22 73 70 65 63 22 20 6f 66 20 61 0a 2a 2a 20 66  "spec" of a.** f
32667 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 2e 20  ull text index. 
32668 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
32669 69 73 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20  is populated by 
3266a 70 61 72 73 65 53 70 65 63 0a 2a 2a 20 61 6e 64  parseSpec.** and
3266b 20 75 73 65 20 62 79 20 66 75 6c 6c 74 65 78 74   use by fulltext
3266c 43 6f 6e 6e 65 63 74 20 61 6e 64 20 66 75 6c 6c  Connect and full
3266d 74 65 78 74 43 72 65 61 74 65 2e 0a 2a 2f 0a 74  textCreate..*/.t
3266e 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61  ypedef struct Ta
3266f 62 6c 65 53 70 65 63 20 7b 0a 20 20 63 6f 6e 73  bleSpec {.  cons
32670 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20  t char *zDb;    
32671 20 20 20 20 20 2f 2a 20 4c 6f 67 69 63 61 6c 20       /* Logical 
32672 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f  database name */
32673 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
32674 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e  Name;       /* N
32675 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d  ame of the full-
32676 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20  text index */.  
32677 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
32678 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
32679 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f  er of columns to
3267a 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20   be indexed */. 
3267b 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 75 6d 6e   char **azColumn
3267c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69  ;         /* Ori
3267d 67 69 6e 61 6c 20 6e 61 6d 65 73 20 6f 66 20 63  ginal names of c
3267e 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64  olumns to be ind
3267f 65 78 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  exed */.  char *
32680 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  *azContentColumn
32681 3b 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d  ;  /* Column nam
32682 65 73 20 66 6f 72 20 25 5f 63 6f 6e 74 65 6e 74  es for %_content
32683 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 54   */.  char **azT
32684 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 2f  okenizer;      /
32685 2a 20 4e 61 6d 65 20 6f 66 20 74 6f 6b 65 6e 69  * Name of tokeni
32686 7a 65 72 20 61 6e 64 20 69 74 73 20 61 72 67 75  zer and its argu
32687 6d 65 6e 74 73 20 2a 2f 0a 7d 20 54 61 62 6c 65  ments */.} Table
32688 53 70 65 63 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63  Spec;../*.** Rec
32689 6c 61 69 6d 20 61 6c 6c 20 6f 66 20 74 68 65 20  laim all of the 
3268a 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61  memory used by a
3268b 20 54 61 62 6c 65 53 70 65 63 0a 2a 2f 0a 73 74   TableSpec.*/.st
3268c 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 54  atic void clearT
3268d 61 62 6c 65 53 70 65 63 28 54 61 62 6c 65 53 70  ableSpec(TableSp
3268e 65 63 20 2a 70 29 20 7b 0a 20 20 73 71 6c 69 74  ec *p) {.  sqlit
3268f 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 43 6f 6c  e3_free(p->azCol
32690 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  umn);.  sqlite3_
32691 66 72 65 65 28 70 2d 3e 61 7a 43 6f 6e 74 65 6e  free(p->azConten
32692 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69  tColumn);.  sqli
32693 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 54 6f  te3_free(p->azTo
32694 6b 65 6e 69 7a 65 72 29 3b 0a 7d 0a 0a 2f 2a 20  kenizer);.}../* 
32695 50 61 72 73 65 20 61 20 43 52 45 41 54 45 20 56  Parse a CREATE V
32696 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
32697 74 65 6d 65 6e 74 2c 20 77 68 69 63 68 20 6c 6f  tement, which lo
32698 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20  oks like this:. 
32699 2a 0a 20 2a 20 43 52 45 41 54 45 20 56 49 52 54  *. * CREATE VIRT
3269a 55 41 4c 20 54 41 42 4c 45 20 65 6d 61 69 6c 0a  UAL TABLE email.
3269b 20 2a 20 20 20 20 20 20 20 20 55 53 49 4e 47 20   *        USING 
3269c 66 74 73 33 28 73 75 62 6a 65 63 74 2c 20 62 6f  fts3(subject, bo
3269d 64 79 2c 20 74 6f 6b 65 6e 69 7a 65 20 6d 79 74  dy, tokenize myt
3269e 6f 6b 65 6e 69 7a 65 72 28 6d 79 61 72 67 29 29  okenizer(myarg))
3269f 0a 20 2a 0a 20 2a 20 57 65 20 72 65 74 75 72 6e  . *. * We return
326a0 20 70 61 72 73 65 64 20 69 6e 66 6f 72 6d 61 74   parsed informat
326a1 69 6f 6e 20 69 6e 20 61 20 54 61 62 6c 65 53 70  ion in a TableSp
326a2 65 63 20 73 74 72 75 63 74 75 72 65 2e 0a 20 2a  ec structure.. *
326a3 20 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   . */.static int
326a4 20 70 61 72 73 65 53 70 65 63 28 54 61 62 6c 65   parseSpec(Table
326a5 53 70 65 63 20 2a 70 53 70 65 63 2c 20 69 6e 74  Spec *pSpec, int
326a6 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
326a7 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20  r *const*argv,. 
326a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
326a9 20 20 20 20 63 68 61 72 2a 2a 70 7a 45 72 72 29      char**pzErr)
326aa 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  {.  int i, n;.  
326ab 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 75 6d 6d 79  char *z, *zDummy
326ac 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 41 72 67  ;.  char **azArg
326ad 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
326ae 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 20  zTokenizer = 0; 
326af 20 20 20 2f 2a 20 61 72 67 76 5b 5d 20 65 6e 74     /* argv[] ent
326b0 72 79 20 64 65 73 63 72 69 62 69 6e 67 20 74 68  ry describing th
326b1 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 0a  e tokenizer */..
326b2 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3e 3d    assert( argc>=
326b3 33 20 29 3b 0a 20 20 2f 2a 20 43 75 72 72 65 6e  3 );.  /* Curren
326b4 74 20 69 6e 74 65 72 66 61 63 65 3a 0a 20 20 2a  t interface:.  *
326b5 2a 20 61 72 67 76 5b 30 5d 20 2d 20 6d 6f 64 75  * argv[0] - modu
326b6 6c 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 72 67  le name.  ** arg
326b7 76 5b 31 5d 20 2d 20 64 61 74 61 62 61 73 65 20  v[1] - database 
326b8 6e 61 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b 32  name.  ** argv[2
326b9 5d 20 2d 20 74 61 62 6c 65 20 6e 61 6d 65 0a 20  ] - table name. 
326ba 20 2a 2a 20 61 72 67 76 5b 33 2e 2e 5d 20 2d 20   ** argv[3..] - 
326bb 63 6f 6c 75 6d 6e 73 2c 20 6f 70 74 69 6f 6e 61  columns, optiona
326bc 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  lly followed by 
326bd 74 6f 6b 65 6e 69 7a 65 72 20 73 70 65 63 69 66  tokenizer specif
326be 69 63 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20  ication.  **    
326bf 20 20 20 20 20 20 20 20 20 61 6e 64 20 73 6e 69           and sni
326c0 70 70 65 74 20 64 65 6c 69 6d 69 74 65 72 73 20  ppet delimiters 
326c1 73 70 65 63 69 66 69 63 61 74 69 6f 6e 2e 0a 20  specification.. 
326c2 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61   */..  /* Make a
326c3 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6d   copy of the com
326c4 70 6c 65 74 65 20 61 72 67 76 5b 5d 5b 5d 20 61  plete argv[][] a
326c5 72 72 61 79 20 69 6e 20 61 20 73 69 6e 67 6c 65  rray in a single
326c6 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 20 20 2a   allocation..  *
326c7 2a 20 54 68 65 20 61 72 67 76 5b 5d 5b 5d 20 61  * The argv[][] a
326c8 72 72 61 79 20 69 73 20 72 65 61 64 2d 6f 6e 6c  rray is read-onl
326c9 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 2e  y and transient.
326ca 20 20 57 65 20 63 61 6e 20 77 72 69 74 65 20 74    We can write t
326cb 6f 20 74 68 65 0a 20 20 2a 2a 20 63 6f 70 79 20  o the.  ** copy 
326cc 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f 64 69  in order to modi
326cd 66 79 20 74 68 69 6e 67 73 20 61 6e 64 20 74 68  fy things and th
326ce 65 20 63 6f 70 79 20 69 73 20 70 65 72 73 69 73  e copy is persis
326cf 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 43 4c 45  tent..  */.  CLE
326d0 41 52 28 70 53 70 65 63 29 3b 0a 20 20 66 6f 72  AR(pSpec);.  for
326d1 28 69 3d 6e 3d 30 3b 20 69 3c 61 72 67 63 3b 20  (i=n=0; i<argc; 
326d2 69 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 73  i++){.    n += s
326d3 74 72 6c 65 6e 28 61 72 67 76 5b 69 5d 29 20 2b  trlen(argv[i]) +
326d4 20 31 3b 0a 20 20 7d 0a 20 20 61 7a 41 72 67 20   1;.  }.  azArg 
326d5 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
326d6 28 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a  ( sizeof(char*)*
326d7 61 72 67 63 20 2b 20 6e 20 29 3b 0a 20 20 69 66  argc + n );.  if
326d8 28 20 61 7a 41 72 67 3d 3d 30 20 29 7b 0a 20 20  ( azArg==0 ){.  
326d9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
326da 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d  NOMEM;.  }.  z =
326db 20 28 63 68 61 72 2a 29 26 61 7a 41 72 67 5b 61   (char*)&azArg[a
326dc 72 67 63 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  rgc];.  for(i=0;
326dd 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20   i<argc; i++){. 
326de 20 20 20 61 7a 41 72 67 5b 69 5d 20 3d 20 7a 3b     azArg[i] = z;
326df 0a 20 20 20 20 73 74 72 63 70 79 28 7a 2c 20 61  .    strcpy(z, a
326e0 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 7a 20 2b  rgv[i]);.    z +
326e1 3d 20 73 74 72 6c 65 6e 28 7a 29 2b 31 3b 0a 20  = strlen(z)+1;. 
326e2 20 7d 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66   }..  /* Identif
326e3 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  y the column nam
326e4 65 73 20 61 6e 64 20 74 68 65 20 74 6f 6b 65 6e  es and the token
326e5 69 7a 65 72 20 61 6e 64 20 64 65 6c 69 6d 69 74  izer and delimit
326e6 65 72 20 61 72 67 75 6d 65 6e 74 73 0a 20 20 2a  er arguments.  *
326e7 2a 20 69 6e 20 74 68 65 20 61 72 67 76 5b 5d 5b  * in the argv[][
326e8 5d 20 61 72 72 61 79 2e 0a 20 20 2a 2f 0a 20 20  ] array..  */.  
326e9 70 53 70 65 63 2d 3e 7a 44 62 20 3d 20 61 7a 41  pSpec->zDb = azA
326ea 72 67 5b 31 5d 3b 0a 20 20 70 53 70 65 63 2d 3e  rg[1];.  pSpec->
326eb 7a 4e 61 6d 65 20 3d 20 61 7a 41 72 67 5b 32 5d  zName = azArg[2]
326ec 3b 0a 20 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75  ;.  pSpec->nColu
326ed 6d 6e 20 3d 20 30 3b 0a 20 20 70 53 70 65 63 2d  mn = 0;.  pSpec-
326ee 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 61 7a 41 72  >azColumn = azAr
326ef 67 3b 0a 20 20 7a 54 6f 6b 65 6e 69 7a 65 72 20  g;.  zTokenizer 
326f0 3d 20 22 74 6f 6b 65 6e 69 7a 65 20 73 69 6d 70  = "tokenize simp
326f1 6c 65 22 3b 0a 20 20 66 6f 72 28 69 3d 33 3b 20  le";.  for(i=3; 
326f2 69 3c 61 72 67 63 3b 20 2b 2b 69 29 7b 0a 20 20  i<argc; ++i){.  
326f3 20 20 69 66 28 20 73 74 61 72 74 73 57 69 74 68    if( startsWith
326f4 28 61 7a 41 72 67 5b 69 5d 2c 22 74 6f 6b 65 6e  (azArg[i],"token
326f5 69 7a 65 22 29 20 29 7b 0a 20 20 20 20 20 20 7a  ize") ){.      z
326f6 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 61 7a 41 72  Tokenizer = azAr
326f7 67 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  g[i];.    }else{
326f8 0a 20 20 20 20 20 20 7a 20 3d 20 61 7a 41 72 67  .      z = azArg
326f9 5b 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 5d  [pSpec->nColumn]
326fa 20 3d 20 66 69 72 73 74 54 6f 6b 65 6e 28 61 7a   = firstToken(az
326fb 41 72 67 5b 69 5d 2c 20 26 7a 44 75 6d 6d 79 29  Arg[i], &zDummy)
326fc 3b 0a 20 20 20 20 20 20 70 53 70 65 63 2d 3e 6e  ;.      pSpec->n
326fd 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 7d 0a  Column++;.    }.
326fe 20 20 7d 0a 20 20 69 66 28 20 70 53 70 65 63 2d    }.  if( pSpec-
326ff 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20  >nColumn==0 ){. 
32700 20 20 20 61 7a 41 72 67 5b 30 5d 20 3d 20 22 63     azArg[0] = "c
32701 6f 6e 74 65 6e 74 22 3b 0a 20 20 20 20 70 53 70  ontent";.    pSp
32702 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b  ec->nColumn = 1;
32703 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
32704 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 6c 69  Construct the li
32705 73 74 20 6f 66 20 63 6f 6e 74 65 6e 74 20 63 6f  st of content co
32706 6c 75 6d 6e 20 6e 61 6d 65 73 2e 0a 20 20 2a 2a  lumn names..  **
32707 0a 20 20 2a 2a 20 45 61 63 68 20 63 6f 6e 74 65  .  ** Each conte
32708 6e 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77  nt column name w
32709 69 6c 6c 20 62 65 20 6f 66 20 74 68 65 20 66 6f  ill be of the fo
3270a 72 6d 20 63 4e 4e 41 41 41 41 0a 20 20 2a 2a 20  rm cNNAAAA.  ** 
3270b 77 68 65 72 65 20 4e 4e 20 69 73 20 74 68 65 20  where NN is the 
3270c 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 61 6e  column number an
3270d 64 20 41 41 41 41 20 69 73 20 74 68 65 20 73 61  d AAAA is the sa
3270e 6e 69 74 69 7a 65 64 0a 20 20 2a 2a 20 63 6f 6c  nitized.  ** col
3270f 75 6d 6e 20 6e 61 6d 65 2e 20 20 22 73 61 6e 69  umn name.  "sani
32710 74 69 7a 65 64 22 20 6d 65 61 6e 73 20 74 68 61  tized" means tha
32711 74 20 73 70 65 63 69 61 6c 20 63 68 61 72 61 63  t special charac
32712 74 65 72 73 20 61 72 65 0a 20 20 2a 2a 20 63 6f  ters are.  ** co
32713 6e 76 65 72 74 65 64 20 74 6f 20 22 5f 22 2e 20  nverted to "_". 
32714 20 54 68 65 20 63 4e 4e 20 70 72 65 66 69 78 20   The cNN prefix 
32715 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20  guarantees that 
32716 61 6c 6c 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20  all column.  ** 
32717 6e 61 6d 65 73 20 61 72 65 20 75 6e 69 71 75 65  names are unique
32718 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
32719 41 41 41 41 20 73 75 66 66 69 78 20 69 73 20 6e  AAAA suffix is n
3271a 6f 74 20 73 74 72 69 63 74 6c 79 20 6e 65 63 65  ot strictly nece
3271b 73 73 61 72 79 2e 20 20 49 74 20 69 73 20 69 6e  ssary.  It is in
3271c 63 6c 75 64 65 64 0a 20 20 2a 2a 20 66 6f 72 20  cluded.  ** for 
3271d 74 68 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  the convenience 
3271e 6f 66 20 70 65 6f 70 6c 65 20 77 68 6f 20 6d 69  of people who mi
3271f 67 68 74 20 65 78 61 6d 69 6e 65 20 74 68 65 20  ght examine the 
32720 67 65 6e 65 72 61 74 65 64 0a 20 20 2a 2a 20 25  generated.  ** %
32721 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 61  _content table a
32722 6e 64 20 77 6f 6e 64 65 72 20 77 68 61 74 20 74  nd wonder what t
32723 68 65 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75  he columns are u
32724 73 65 64 20 66 6f 72 2e 0a 20 20 2a 2f 0a 20 20  sed for..  */.  
32725 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74  pSpec->azContent
32726 43 6f 6c 75 6d 6e 20 3d 20 73 71 6c 69 74 65 33  Column = sqlite3
32727 5f 6d 61 6c 6c 6f 63 28 20 70 53 70 65 63 2d 3e  _malloc( pSpec->
32728 6e 43 6f 6c 75 6d 6e 20 2a 20 73 69 7a 65 6f 66  nColumn * sizeof
32729 28 63 68 61 72 20 2a 29 20 29 3b 0a 20 20 69 66  (char *) );.  if
3272a 28 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65  ( pSpec->azConte
3272b 6e 74 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20  ntColumn==0 ){. 
3272c 20 20 20 63 6c 65 61 72 54 61 62 6c 65 53 70 65     clearTableSpe
3272d 63 28 70 53 70 65 63 29 3b 0a 20 20 20 20 72 65  c(pSpec);.    re
3272e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
3272f 4d 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  M;.  }.  for(i=0
32730 3b 20 69 3c 70 53 70 65 63 2d 3e 6e 43 6f 6c 75  ; i<pSpec->nColu
32731 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68  mn; i++){.    ch
32732 61 72 20 2a 70 3b 0a 20 20 20 20 70 53 70 65 63  ar *p;.    pSpec
32733 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
32734 6e 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 5f 6d  n[i] = sqlite3_m
32735 70 72 69 6e 74 66 28 22 63 25 64 25 73 22 2c 20  printf("c%d%s", 
32736 69 2c 20 61 7a 41 72 67 5b 69 5d 29 3b 0a 20 20  i, azArg[i]);.  
32737 20 20 66 6f 72 20 28 70 20 3d 20 70 53 70 65 63    for (p = pSpec
32738 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
32739 6e 5b 69 5d 3b 20 2a 70 20 3b 20 2b 2b 70 29 20  n[i]; *p ; ++p) 
3273a 7b 0a 20 20 20 20 20 20 69 66 28 20 21 73 61 66  {.      if( !saf
3273b 65 5f 69 73 61 6c 6e 75 6d 28 2a 70 29 20 29 20  e_isalnum(*p) ) 
3273c 2a 70 20 3d 20 27 5f 27 3b 0a 20 20 20 20 7d 0a  *p = '_';.    }.
3273d 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50    }..  /*.  ** P
3273e 61 72 73 65 20 74 68 65 20 74 6f 6b 65 6e 69 7a  arse the tokeniz
3273f 65 72 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e  er specification
32740 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20   string..  */.  
32741 70 53 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a  pSpec->azTokeniz
32742 65 72 20 3d 20 74 6f 6b 65 6e 69 7a 65 53 74 72  er = tokenizeStr
32743 69 6e 67 28 7a 54 6f 6b 65 6e 69 7a 65 72 2c 20  ing(zTokenizer, 
32744 26 6e 29 3b 0a 20 20 74 6f 6b 65 6e 4c 69 73 74  &n);.  tokenList
32745 54 6f 49 64 4c 69 73 74 28 70 53 70 65 63 2d 3e  ToIdList(pSpec->
32746 61 7a 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20  azTokenizer);.. 
32747 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
32748 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  K;.}../*.** Gene
32749 72 61 74 65 20 61 20 43 52 45 41 54 45 20 54 41  rate a CREATE TA
3274a 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68  BLE statement th
3274b 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65  at describes the
3274c 20 73 63 68 65 6d 61 20 6f 66 0a 2a 2a 20 74 68   schema of.** th
3274d 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
3274e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
3274f 65 72 20 74 6f 20 74 68 69 73 20 73 63 68 65 6d  er to this schem
32750 61 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  a string..**.** 
32751 53 70 61 63 65 20 69 73 20 6f 62 74 61 69 6e 65  Space is obtaine
32752 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
32753 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 68 6f  printf() and sho
32754 75 6c 64 20 62 65 20 66 72 65 65 64 0a 2a 2a 20  uld be freed.** 
32755 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72  using sqlite3_fr
32756 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ee()..*/.static 
32757 63 68 61 72 20 2a 66 75 6c 6c 74 65 78 74 53 63  char *fulltextSc
32758 68 65 6d 61 28 0a 20 20 69 6e 74 20 6e 43 6f 6c  hema(.  int nCol
32759 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  umn,            
3275a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
3275b 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
3275c 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
3275d 74 2a 20 61 7a 43 6f 6c 75 6d 6e 2c 20 20 2f 2a  t* azColumn,  /*
3275e 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   List of columns
3275f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
32760 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 20 20 20   *zTableName    
32761 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
32762 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  he table */.){. 
32763 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a   int i;.  char *
32764 7a 53 63 68 65 6d 61 2c 20 2a 7a 4e 65 78 74 3b  zSchema, *zNext;
32765 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
32766 53 65 70 20 3d 20 22 28 22 3b 0a 20 20 7a 53 63  Sep = "(";.  zSc
32767 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 5f 6d  hema = sqlite3_m
32768 70 72 69 6e 74 66 28 22 43 52 45 41 54 45 20 54  printf("CREATE T
32769 41 42 4c 45 20 78 22 29 3b 0a 20 20 66 6f 72 28  ABLE x");.  for(
3276a 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20  i=0; i<nColumn; 
3276b 69 2b 2b 29 7b 0a 20 20 20 20 7a 4e 65 78 74 20  i++){.    zNext 
3276c 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
3276d 66 28 22 25 73 25 73 25 51 22 2c 20 7a 53 63 68  f("%s%s%Q", zSch
3276e 65 6d 61 2c 20 7a 53 65 70 2c 20 61 7a 43 6f 6c  ema, zSep, azCol
3276f 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20 73 71 6c  umn[i]);.    sql
32770 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68 65 6d  ite3_free(zSchem
32771 61 29 3b 0a 20 20 20 20 7a 53 63 68 65 6d 61 20  a);.    zSchema 
32772 3d 20 7a 4e 65 78 74 3b 0a 20 20 20 20 7a 53 65  = zNext;.    zSe
32773 70 20 3d 20 22 2c 22 3b 0a 20 20 7d 0a 20 20 7a  p = ",";.  }.  z
32774 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Next = sqlite3_m
32775 70 72 69 6e 74 66 28 22 25 73 2c 25 51 20 48 49  printf("%s,%Q HI
32776 44 44 45 4e 22 2c 20 7a 53 63 68 65 6d 61 2c 20  DDEN", zSchema, 
32777 7a 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 73  zTableName);.  s
32778 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68  qlite3_free(zSch
32779 65 6d 61 29 3b 0a 20 20 7a 53 63 68 65 6d 61 20  ema);.  zSchema 
3277a 3d 20 7a 4e 65 78 74 3b 0a 20 20 7a 4e 65 78 74  = zNext;.  zNext
3277b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
3277c 74 66 28 22 25 73 2c 64 6f 63 69 64 20 48 49 44  tf("%s,docid HID
3277d 44 45 4e 29 22 2c 20 7a 53 63 68 65 6d 61 29 3b  DEN)", zSchema);
3277e 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3277f 7a 53 63 68 65 6d 61 29 3b 0a 20 20 72 65 74 75  zSchema);.  retu
32780 72 6e 20 7a 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a  rn zNext;.}../*.
32781 2a 2a 20 42 75 69 6c 64 20 61 20 6e 65 77 20 73  ** Build a new s
32782 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
32783 63 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20  cture that will 
32784 64 65 73 63 72 69 62 65 20 74 68 65 0a 2a 2a 20  describe the.** 
32785 66 75 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 64  fulltext index d
32786 65 66 69 6e 65 64 20 62 79 20 73 70 65 63 2e 0a  efined by spec..
32787 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
32788 6e 73 74 72 75 63 74 56 74 61 62 28 0a 20 20 73  nstructVtab(.  s
32789 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
3278a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3278b 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
3278c 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
3278d 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c  fts3Hash *pHash,
3278e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
3278f 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  h table containi
32790 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 73 20 2a 2f  ng tokenizers */
32791 0a 20 20 54 61 62 6c 65 53 70 65 63 20 2a 73 70  .  TableSpec *sp
32792 65 63 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ec,          /* 
32793 50 61 72 73 65 64 20 73 70 65 63 20 69 6e 66 6f  Parsed spec info
32794 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 70 61 72  rmation from par
32795 73 65 53 70 65 63 28 29 20 2a 2f 0a 20 20 73 71  seSpec() */.  sq
32796 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56  lite3_vtab **ppV
32797 54 61 62 2c 20 20 20 20 2f 2a 20 57 72 69 74 65  Tab,    /* Write
32798 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 76   the resulting v
32799 74 61 62 20 73 74 72 75 63 74 75 72 65 20 68 65  tab structure he
3279a 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  re */.  char **p
3279b 7a 45 72 72 20 20 20 20 20 20 20 20 20 20 20 20  zErr            
3279c 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 79 20 65    /* Write any e
3279d 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72  rror message her
3279e 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
3279f 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 75 6c  ;.  int n;.  ful
327a0 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20  ltext_vtab *v = 
327a1 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  0;.  const sqlit
327a2 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
327a3 75 6c 65 20 2a 6d 20 3d 20 4e 55 4c 4c 3b 0a 20  ule *m = NULL;. 
327a4 20 63 68 61 72 20 2a 73 63 68 65 6d 61 3b 0a 0a   char *schema;..
327a5 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54    char const *zT
327a6 6f 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ok;         /* N
327a7 61 6d 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72  ame of tokenizer
327a8 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73   to use for this
327a9 20 66 74 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20   fts table */.  
327aa 69 6e 74 20 6e 54 6f 6b 3b 20 20 20 20 20 20 20  int nTok;       
327ab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
327ac 67 74 68 20 6f 66 20 7a 54 6f 6b 2c 20 69 6e 63  gth of zTok, inc
327ad 6c 75 64 69 6e 67 20 6e 75 6c 20 74 65 72 6d 69  luding nul termi
327ae 6e 61 74 6f 72 20 2a 2f 0a 0a 20 20 76 20 3d 20  nator */..  v = 
327af 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
327b0 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ) sqlite3_malloc
327b1 28 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74  (sizeof(fulltext
327b2 5f 76 74 61 62 29 29 3b 0a 20 20 69 66 28 20 76  _vtab));.  if( v
327b3 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
327b4 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 43 4c 45  ITE_NOMEM;.  CLE
327b5 41 52 28 76 29 3b 0a 20 20 2f 2a 20 73 71 6c 69  AR(v);.  /* sqli
327b6 74 65 20 77 69 6c 6c 20 69 6e 69 74 69 61 6c 69  te will initiali
327b7 7a 65 20 76 2d 3e 62 61 73 65 20 2a 2f 0a 20 20  ze v->base */.  
327b8 76 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 76 2d  v->db = db;.  v-
327b9 3e 7a 44 62 20 3d 20 73 70 65 63 2d 3e 7a 44 62  >zDb = spec->zDb
327ba 3b 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 64  ;       /* Freed
327bb 20 77 68 65 6e 20 61 7a 43 6f 6c 75 6d 6e 20 69   when azColumn i
327bc 73 20 66 72 65 65 64 20 2a 2f 0a 20 20 76 2d 3e  s freed */.  v->
327bd 7a 4e 61 6d 65 20 3d 20 73 70 65 63 2d 3e 7a 4e  zName = spec->zN
327be 61 6d 65 3b 20 20 20 2f 2a 20 46 72 65 65 64 20  ame;   /* Freed 
327bf 77 68 65 6e 20 61 7a 43 6f 6c 75 6d 6e 20 69 73  when azColumn is
327c0 20 66 72 65 65 64 20 2a 2f 0a 20 20 76 2d 3e 6e   freed */.  v->n
327c1 43 6f 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e 6e  Column = spec->n
327c2 43 6f 6c 75 6d 6e 3b 0a 20 20 76 2d 3e 61 7a 43  Column;.  v->azC
327c3 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20 73  ontentColumn = s
327c4 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f  pec->azContentCo
327c5 6c 75 6d 6e 3b 0a 20 20 73 70 65 63 2d 3e 61 7a  lumn;.  spec->az
327c6 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20  ContentColumn = 
327c7 30 3b 0a 20 20 76 2d 3e 61 7a 43 6f 6c 75 6d 6e  0;.  v->azColumn
327c8 20 3d 20 73 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d   = spec->azColum
327c9 6e 3b 0a 20 20 73 70 65 63 2d 3e 61 7a 43 6f 6c  n;.  spec->azCol
327ca 75 6d 6e 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  umn = 0;..  if( 
327cb 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65  spec->azTokenize
327cc 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  r==0 ){.    retu
327cd 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
327ce 0a 20 20 7d 0a 0a 20 20 7a 54 6f 6b 20 3d 20 73  .  }..  zTok = s
327cf 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72  pec->azTokenizer
327d0 5b 30 5d 3b 20 0a 20 20 69 66 28 20 21 7a 54 6f  [0]; .  if( !zTo
327d1 6b 20 29 7b 0a 20 20 20 20 7a 54 6f 6b 20 3d 20  k ){.    zTok = 
327d2 22 73 69 6d 70 6c 65 22 3b 0a 20 20 7d 0a 20 20  "simple";.  }.  
327d3 6e 54 6f 6b 20 3d 20 73 74 72 6c 65 6e 28 7a 54  nTok = strlen(zT
327d4 6f 6b 29 2b 31 3b 0a 0a 20 20 6d 20 3d 20 28 73  ok)+1;..  m = (s
327d5 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
327d6 5f 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  _module *)sqlite
327d7 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48  3Fts3HashFind(pH
327d8 61 73 68 2c 20 7a 54 6f 6b 2c 20 6e 54 6f 6b 29  ash, zTok, nTok)
327d9 3b 0a 20 20 69 66 28 20 21 6d 20 29 7b 0a 20 20  ;.  if( !m ){.  
327da 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
327db 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
327dc 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25  own tokenizer: %
327dd 73 22 2c 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65  s", spec->azToke
327de 6e 69 7a 65 72 5b 30 5d 29 3b 0a 20 20 20 20 72  nizer[0]);.    r
327df 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
327e0 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 3b 0a  ;.    goto err;.
327e1 20 20 7d 0a 0a 20 20 66 6f 72 28 6e 3d 30 3b 20    }..  for(n=0; 
327e2 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65  spec->azTokenize
327e3 72 5b 6e 5d 3b 20 6e 2b 2b 29 7b 7d 0a 20 20 69  r[n]; n++){}.  i
327e4 66 28 20 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d  f( n ){.    rc =
327e5 20 6d 2d 3e 78 43 72 65 61 74 65 28 6e 2d 31 2c   m->xCreate(n-1,
327e6 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e   (const char*con
327e7 73 74 2a 29 26 73 70 65 63 2d 3e 61 7a 54 6f 6b  st*)&spec->azTok
327e8 65 6e 69 7a 65 72 5b 31 5d 2c 0a 20 20 20 20 20  enizer[1],.     
327e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
327ea 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a  v->pTokenizer);.
327eb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
327ec 3d 20 6d 2d 3e 78 43 72 65 61 74 65 28 30 2c 20  = m->xCreate(0, 
327ed 30 2c 20 26 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65  0, &v->pTokenize
327ee 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  r);.  }.  if( rc
327ef 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
327f0 74 6f 20 65 72 72 3b 0a 20 20 76 2d 3e 70 54 6f  to err;.  v->pTo
327f1 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
327f2 20 3d 20 6d 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f   = m;..  /* TODO
327f3 3a 20 76 65 72 69 66 79 20 74 68 65 20 65 78 69  : verify the exi
327f4 73 74 65 6e 63 65 20 6f 66 20 62 61 63 6b 69 6e  stence of backin
327f5 67 20 74 61 62 6c 65 73 20 66 6f 6f 5f 63 6f 6e  g tables foo_con
327f6 74 65 6e 74 2c 20 66 6f 6f 5f 74 65 72 6d 20 2a  tent, foo_term *
327f7 2f 0a 0a 20 20 73 63 68 65 6d 61 20 3d 20 66 75  /..  schema = fu
327f8 6c 6c 74 65 78 74 53 63 68 65 6d 61 28 76 2d 3e  lltextSchema(v->
327f9 6e 43 6f 6c 75 6d 6e 2c 20 28 63 6f 6e 73 74 20  nColumn, (const 
327fa 63 68 61 72 2a 63 6f 6e 73 74 2a 29 76 2d 3e 61  char*const*)v->a
327fb 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20  zColumn,.       
327fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
327fd 20 20 20 73 70 65 63 2d 3e 7a 4e 61 6d 65 29 3b     spec->zName);
327fe 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
327ff 64 65 63 6c 61 72 65 5f 76 74 61 62 28 64 62 2c  declare_vtab(db,
32800 20 73 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69   schema);.  sqli
32801 74 65 33 5f 66 72 65 65 28 73 63 68 65 6d 61 29  te3_free(schema)
32802 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
32803 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
32804 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 76 2d 3e 70  ;..  memset(v->p
32805 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e  FulltextStatemen
32806 74 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 76 2d  ts, 0, sizeof(v-
32807 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
32808 65 6e 74 73 29 29 3b 0a 0a 20 20 2f 2a 20 49 6e  ents));..  /* In
32809 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20  dicate that the 
3280a 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 6c 69  buffer is not li
3280b 76 65 2e 20 2a 2f 0a 20 20 76 2d 3e 6e 50 65 6e  ve. */.  v->nPen
3280c 64 69 6e 67 44 61 74 61 20 3d 20 2d 31 3b 0a 0a  dingData = -1;..
3280d 20 20 2a 70 70 56 54 61 62 20 3d 20 26 76 2d 3e    *ppVTab = &v->
3280e 62 61 73 65 3b 0a 20 20 46 54 53 54 52 41 43 45  base;.  FTSTRACE
3280f 28 28 22 46 54 53 33 20 43 6f 6e 6e 65 63 74 20  (("FTS3 Connect 
32810 25 70 5c 6e 22 2c 20 76 29 29 3b 0a 0a 20 20 72  %p\n", v));..  r
32811 65 74 75 72 6e 20 72 63 3b 0a 0a 65 72 72 3a 0a  eturn rc;..err:.
32812 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 5f    fulltext_vtab_
32813 64 65 73 74 72 6f 79 28 76 29 3b 0a 20 20 72 65  destroy(v);.  re
32814 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
32815 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43  ic int fulltextC
32816 6f 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69 74 65  onnect(.  sqlite
32817 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70  3 *db,.  void *p
32818 41 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  Aux,.  int argc,
32819 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
3281a 73 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74  st*argv,.  sqlit
3281b 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62  e3_vtab **ppVTab
3281c 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  ,.  char **pzErr
3281d 0a 29 7b 0a 20 20 54 61 62 6c 65 53 70 65 63 20  .){.  TableSpec 
3281e 73 70 65 63 3b 0a 20 20 69 6e 74 20 72 63 20 3d  spec;.  int rc =
3281f 20 70 61 72 73 65 53 70 65 63 28 26 73 70 65 63   parseSpec(&spec
32820 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70 7a  , argc, argv, pz
32821 45 72 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  Err);.  if( rc!=
32822 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
32823 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 63  rn rc;..  rc = c
32824 6f 6e 73 74 72 75 63 74 56 74 61 62 28 64 62 2c  onstructVtab(db,
32825 20 28 66 74 73 33 48 61 73 68 20 2a 29 70 41 75   (fts3Hash *)pAu
32826 78 2c 20 26 73 70 65 63 2c 20 70 70 56 54 61 62  x, &spec, ppVTab
32827 2c 20 70 7a 45 72 72 29 3b 0a 20 20 63 6c 65 61  , pzErr);.  clea
32828 72 54 61 62 6c 65 53 70 65 63 28 26 73 70 65 63  rTableSpec(&spec
32829 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
3282a 7d 0a 0a 2f 2a 20 54 68 65 20 25 5f 63 6f 6e 74  }../* The %_cont
3282b 65 6e 74 20 74 61 62 6c 65 20 68 6f 6c 64 73 20  ent table holds 
3282c 74 68 65 20 74 65 78 74 20 6f 66 20 65 61 63 68  the text of each
3282d 20 64 6f 63 75 6d 65 6e 74 2c 20 77 69 74 68 0a   document, with.
3282e 2a 2a 20 74 68 65 20 64 6f 63 69 64 20 63 6f 6c  ** the docid col
3282f 75 6d 6e 20 65 78 70 6f 73 65 64 20 61 73 20 74  umn exposed as t
32830 68 65 20 53 51 4c 69 74 65 20 72 6f 77 69 64 20  he SQLite rowid 
32831 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a  for the table..*
32832 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
32833 20 54 68 69 73 20 63 6f 6d 6d 65 6e 74 20 6e 65   This comment ne
32834 65 64 73 20 65 6c 61 62 6f 72 61 74 69 6f 6e 20  eds elaboration 
32835 74 6f 20 6d 61 74 63 68 20 74 68 65 20 75 70 64  to match the upd
32836 61 74 65 64 0a 2a 2a 20 63 6f 64 65 2e 20 20 57  ated.** code.  W
32837 6f 72 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20  ork it into the 
32838 74 6f 70 2d 6f 66 2d 66 69 6c 65 20 63 6f 6d 6d  top-of-file comm
32839 65 6e 74 20 61 74 20 74 68 61 74 20 74 69 6d 65  ent at that time
3283a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3283b 66 75 6c 6c 74 65 78 74 43 72 65 61 74 65 28 73  fulltextCreate(s
3283c 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
3283d 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20   *pAux,.        
3283e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3283f 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
32840 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
32841 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20  argv,.          
32842 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32843 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
32844 70 56 54 61 62 2c 20 63 68 61 72 20 2a 2a 70 7a  pVTab, char **pz
32845 45 72 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Err){.  int rc;.
32846 20 20 54 61 62 6c 65 53 70 65 63 20 73 70 65 63    TableSpec spec
32847 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66 65 72  ;.  StringBuffer
32848 20 73 63 68 65 6d 61 3b 0a 20 20 46 54 53 54 52   schema;.  FTSTR
32849 41 43 45 28 28 22 46 54 53 33 20 43 72 65 61 74  ACE(("FTS3 Creat
3284a 65 5c 6e 22 29 29 3b 0a 0a 20 20 72 63 20 3d 20  e\n"));..  rc = 
3284b 70 61 72 73 65 53 70 65 63 28 26 73 70 65 63 2c  parseSpec(&spec,
3284c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70 7a 45   argc, argv, pzE
3284d 72 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  rr);.  if( rc!=S
3284e 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3284f 6e 20 72 63 3b 0a 0a 20 20 69 6e 69 74 53 74 72  n rc;..  initStr
32850 69 6e 67 42 75 66 66 65 72 28 26 73 63 68 65 6d  ingBuffer(&schem
32851 61 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63  a);.  append(&sc
32852 68 65 6d 61 2c 20 22 43 52 45 41 54 45 20 54 41  hema, "CREATE TA
32853 42 4c 45 20 25 5f 63 6f 6e 74 65 6e 74 28 22 29  BLE %_content(")
32854 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63 68 65  ;.  append(&sche
32855 6d 61 2c 20 22 20 20 64 6f 63 69 64 20 49 4e 54  ma, "  docid INT
32856 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
32857 2c 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69 73  ,");.  appendLis
32858 74 28 26 73 63 68 65 6d 61 2c 20 73 70 65 63 2e  t(&schema, spec.
32859 6e 43 6f 6c 75 6d 6e 2c 20 73 70 65 63 2e 61 7a  nColumn, spec.az
3285a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a  ContentColumn);.
3285b 20 20 61 70 70 65 6e 64 28 26 73 63 68 65 6d 61    append(&schema
3285c 2c 20 22 29 22 29 3b 0a 20 20 72 63 20 3d 20 73  , ")");.  rc = s
3285d 71 6c 5f 65 78 65 63 28 64 62 2c 20 73 70 65 63  ql_exec(db, spec
3285e 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61 6d 65  .zDb, spec.zName
3285f 2c 20 73 74 72 69 6e 67 42 75 66 66 65 72 44 61  , stringBufferDa
32860 74 61 28 26 73 63 68 65 6d 61 29 29 3b 0a 20 20  ta(&schema));.  
32861 73 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74  stringBufferDest
32862 72 6f 79 28 26 73 63 68 65 6d 61 29 3b 0a 20 20  roy(&schema);.  
32863 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
32864 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20  K ) goto out;.. 
32865 20 72 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 64   rc = sql_exec(d
32866 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20 73 70 65  b, spec.zDb, spe
32867 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  c.zName,.       
32868 20 20 20 20 20 20 20 20 20 22 63 72 65 61 74 65           "create
32869 20 74 61 62 6c 65 20 25 5f 73 65 67 6d 65 6e 74   table %_segment
3286a 73 28 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  s(".            
3286b 20 20 20 20 22 20 20 62 6c 6f 63 6b 69 64 20 49      "  blockid I
3286c 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
3286d 45 59 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20  EY,".           
3286e 20 20 20 20 20 22 20 20 62 6c 6f 63 6b 20 62 6c       "  block bl
3286f 6f 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  ob".            
32870 20 20 20 20 22 29 3b 22 0a 20 20 20 20 20 20 20      ");".       
32871 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 69 66           );.  if
32872 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
32873 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20 20 72  ) goto out;..  r
32874 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 64 62 2c  c = sql_exec(db,
32875 20 73 70 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e   spec.zDb, spec.
32876 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
32877 20 20 20 20 20 20 20 22 63 72 65 61 74 65 20 74         "create t
32878 61 62 6c 65 20 25 5f 73 65 67 64 69 72 28 22 0a  able %_segdir(".
32879 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3287a 22 20 20 6c 65 76 65 6c 20 69 6e 74 65 67 65 72  "  level integer
3287b 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,".             
3287c 20 20 20 22 20 20 69 64 78 20 69 6e 74 65 67 65     "  idx intege
3287d 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  r,".            
3287e 20 20 20 20 22 20 20 73 74 61 72 74 5f 62 6c 6f      "  start_blo
3287f 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20  ck integer,".   
32880 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
32881 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
32882 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20 20 20   integer,".     
32883 20 20 20 20 20 20 20 20 20 20 20 22 20 20 65 6e             "  en
32884 64 5f 62 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c  d_block integer,
32885 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
32886 20 20 22 20 20 72 6f 6f 74 20 62 6c 6f 62 2c 22    "  root blob,"
32887 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32888 20 22 20 20 70 72 69 6d 61 72 79 20 6b 65 79 28   "  primary key(
32889 6c 65 76 65 6c 2c 20 69 64 78 29 22 0a 20 20 20  level, idx)".   
3288a 20 20 20 20 20 20 20 20 20 20 20 20 20 22 29 3b               ");
3288b 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ");.  if( rc!=SQ
3288c 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f  LITE_OK ) goto o
3288d 75 74 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 73  ut;..  rc = cons
3288e 74 72 75 63 74 56 74 61 62 28 64 62 2c 20 28 66  tructVtab(db, (f
3288f 74 73 33 48 61 73 68 20 2a 29 70 41 75 78 2c 20  ts3Hash *)pAux, 
32890 26 73 70 65 63 2c 20 70 70 56 54 61 62 2c 20 70  &spec, ppVTab, p
32891 7a 45 72 72 29 3b 0a 0a 6f 75 74 3a 0a 20 20 63  zErr);..out:.  c
32892 6c 65 61 72 54 61 62 6c 65 53 70 65 63 28 26 73  learTableSpec(&s
32893 70 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pec);.  return r
32894 63 3b 0a 7d 0a 0a 2f 2a 20 44 65 63 69 64 65 20  c;.}../* Decide 
32895 68 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 61 6e  how to handle an
32896 20 53 51 4c 20 71 75 65 72 79 2e 20 2a 2f 0a 73   SQL query. */.s
32897 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
32898 78 74 42 65 73 74 49 6e 64 65 78 28 73 71 6c 69  xtBestIndex(sqli
32899 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
3289a 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
3289b 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 66  nfo *pInfo){.  f
3289c 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20  ulltext_vtab *v 
3289d 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  = (fulltext_vtab
3289e 20 2a 29 70 56 54 61 62 3b 0a 20 20 69 6e 74 20   *)pVTab;.  int 
3289f 69 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  i;.  FTSTRACE(("
328a0 46 54 53 33 20 42 65 73 74 49 6e 64 65 78 5c 6e  FTS3 BestIndex\n
328a1 22 29 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  "));..  for(i=0;
328a2 20 69 3c 70 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74   i<pInfo->nConst
328a3 72 61 69 6e 74 3b 20 2b 2b 69 29 7b 0a 20 20 20  raint; ++i){.   
328a4 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71   const struct sq
328a5 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
328a6 74 72 61 69 6e 74 20 2a 70 43 6f 6e 73 74 72 61  traint *pConstra
328a7 69 6e 74 3b 0a 20 20 20 20 70 43 6f 6e 73 74 72  int;.    pConstr
328a8 61 69 6e 74 20 3d 20 26 70 49 6e 66 6f 2d 3e 61  aint = &pInfo->a
328a9 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 3b 0a 20  Constraint[i];. 
328aa 20 20 20 69 66 28 20 70 43 6f 6e 73 74 72 61 69     if( pConstrai
328ab 6e 74 2d 3e 75 73 61 62 6c 65 20 29 20 7b 0a 20  nt->usable ) {. 
328ac 20 20 20 20 20 69 66 28 20 28 70 43 6f 6e 73 74       if( (pConst
328ad 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d  raint->iColumn==
328ae 2d 31 20 7c 7c 20 70 43 6f 6e 73 74 72 61 69 6e  -1 || pConstrain
328af 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 76 2d 3e 6e  t->iColumn==v->n
328b0 43 6f 6c 75 6d 6e 2b 31 29 20 26 26 0a 20 20 20  Column+1) &&.   
328b1 20 20 20 20 20 20 20 70 43 6f 6e 73 74 72 61 69         pConstrai
328b2 6e 74 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49  nt->op==SQLITE_I
328b3 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
328b4 45 51 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49  EQ ){.        pI
328b5 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55  nfo->idxNum = QU
328b6 45 52 59 5f 44 4f 43 49 44 3b 20 20 20 20 20 20  ERY_DOCID;      
328b7 2f 2a 20 6c 6f 6f 6b 75 70 20 62 79 20 64 6f 63  /* lookup by doc
328b8 69 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 54  id */.        FT
328b9 53 54 52 41 43 45 28 28 22 46 54 53 33 20 51 55  STRACE(("FTS3 QU
328ba 45 52 59 5f 44 4f 43 49 44 5c 6e 22 29 29 3b 0a  ERY_DOCID\n"));.
328bb 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28        } else if(
328bc 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43   pConstraint->iC
328bd 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 43 6f 6e  olumn>=0 && pCon
328be 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e  straint->iColumn
328bf 3c 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 26 26 0a  <=v->nColumn &&.
328c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328c1 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 6f 70   pConstraint->op
328c2 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
328c3 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20  ONSTRAINT_MATCH 
328c4 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 75  ){.        /* fu
328c5 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20 2a  ll-text search *
328c6 2f 0a 20 20 20 20 20 20 20 20 70 49 6e 66 6f 2d  /.        pInfo-
328c7 3e 69 64 78 4e 75 6d 20 3d 20 51 55 45 52 59 5f  >idxNum = QUERY_
328c8 46 55 4c 4c 54 45 58 54 20 2b 20 70 43 6f 6e 73  FULLTEXT + pCons
328c9 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3b  traint->iColumn;
328ca 0a 20 20 20 20 20 20 20 20 46 54 53 54 52 41 43  .        FTSTRAC
328cb 45 28 28 22 46 54 53 33 20 51 55 45 52 59 5f 46  E(("FTS3 QUERY_F
328cc 55 4c 4c 54 45 58 54 20 25 64 5c 6e 22 2c 20 70  ULLTEXT %d\n", p
328cd 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c  Constraint->iCol
328ce 75 6d 6e 29 29 3b 0a 20 20 20 20 20 20 7d 20 65  umn));.      } e
328cf 6c 73 65 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20  lse continue;.. 
328d0 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e       pInfo->aCon
328d1 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
328d2 61 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a 20  argvIndex = 1;. 
328d3 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e       pInfo->aCon
328d4 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
328d5 6f 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 20 20 20  omit = 1;..     
328d6 20 2f 2a 20 41 6e 20 61 72 62 69 74 72 61 72 79   /* An arbitrary
328d7 20 76 61 6c 75 65 20 66 6f 72 20 6e 6f 77 2e 0a   value for now..
328d8 20 20 20 20 20 20 20 2a 20 54 4f 44 4f 3a 20 50         * TODO: P
328d9 65 72 68 61 70 73 20 64 6f 63 69 64 20 6d 61 74  erhaps docid mat
328da 63 68 65 73 20 73 68 6f 75 6c 64 20 62 65 20 63  ches should be c
328db 6f 6e 73 69 64 65 72 65 64 20 63 68 65 61 70 65  onsidered cheape
328dc 72 20 74 68 61 6e 0a 20 20 20 20 20 20 20 2a 20  r than.       * 
328dd 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68  full-text search
328de 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 6e  es. */.      pIn
328df 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73  fo->estimatedCos
328e0 74 20 3d 20 31 2e 30 3b 20 20 20 0a 0a 20 20 20  t = 1.0;   ..   
328e1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
328e2 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  _OK;.    }.  }. 
328e3 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d   pInfo->idxNum =
328e4 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43 3b 0a   QUERY_GENERIC;.
328e5 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
328e6 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  OK;.}..static in
328e7 74 20 66 75 6c 6c 74 65 78 74 44 69 73 63 6f 6e  t fulltextDiscon
328e8 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76 74 61  nect(sqlite3_vta
328e9 62 20 2a 70 56 54 61 62 29 7b 0a 20 20 46 54 53  b *pVTab){.  FTS
328ea 54 52 41 43 45 28 28 22 46 54 53 33 20 44 69 73  TRACE(("FTS3 Dis
328eb 63 6f 6e 6e 65 63 74 20 25 70 5c 6e 22 2c 20 70  connect %p\n", p
328ec 56 54 61 62 29 29 3b 0a 20 20 66 75 6c 6c 74 65  VTab));.  fullte
328ed 78 74 5f 76 74 61 62 5f 64 65 73 74 72 6f 79 28  xt_vtab_destroy(
328ee 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
328ef 29 70 56 54 61 62 29 3b 0a 20 20 72 65 74 75 72  )pVTab);.  retur
328f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
328f1 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
328f2 65 78 74 44 65 73 74 72 6f 79 28 73 71 6c 69 74  extDestroy(sqlit
328f3 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 7b  e3_vtab *pVTab){
328f4 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
328f5 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *v = (fulltext_
328f6 76 74 61 62 20 2a 29 70 56 54 61 62 3b 0a 20 20  vtab *)pVTab;.  
328f7 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52  int rc;..  FTSTR
328f8 41 43 45 28 28 22 46 54 53 33 20 44 65 73 74 72  ACE(("FTS3 Destr
328f9 6f 79 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 29  oy %p\n", pVTab)
328fa 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78  );.  rc = sql_ex
328fb 65 63 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62  ec(v->db, v->zDb
328fc 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20  , v->zName,.    
328fd 20 20 20 20 20 20 20 20 20 20 20 20 22 64 72 6f              "dro
328fe 70 20 74 61 62 6c 65 20 69 66 20 65 78 69 73 74  p table if exist
328ff 73 20 25 5f 63 6f 6e 74 65 6e 74 3b 22 0a 20 20  s %_content;".  
32900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 64                "d
32901 72 6f 70 20 74 61 62 6c 65 20 69 66 20 65 78 69  rop table if exi
32902 73 74 73 20 25 5f 73 65 67 6d 65 6e 74 73 3b 22  sts %_segments;"
32903 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32904 20 22 64 72 6f 70 20 74 61 62 6c 65 20 69 66 20   "drop table if 
32905 65 78 69 73 74 73 20 25 5f 73 65 67 64 69 72 3b  exists %_segdir;
32906 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
32907 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53    );.  if( rc!=S
32908 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
32909 6e 20 72 63 3b 0a 0a 20 20 66 75 6c 6c 74 65 78  n rc;..  fulltex
3290a 74 5f 76 74 61 62 5f 64 65 73 74 72 6f 79 28 28  t_vtab_destroy((
3290b 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
3290c 70 56 54 61 62 29 3b 0a 20 20 72 65 74 75 72 6e  pVTab);.  return
3290d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73   SQLITE_OK;.}..s
3290e 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
3290f 78 74 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  xtOpen(sqlite3_v
32910 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69  tab *pVTab, sqli
32911 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
32912 2a 2a 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 66  **ppCursor){.  f
32913 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
32914 63 3b 0a 0a 20 20 63 20 3d 20 28 66 75 6c 6c 74  c;..  c = (fullt
32915 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 73 71  ext_cursor *) sq
32916 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
32917 65 6f 66 28 66 75 6c 6c 74 65 78 74 5f 63 75 72  eof(fulltext_cur
32918 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 63 20 29  sor));.  if( c )
32919 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 63 2c 20  {.    memset(c, 
3291a 30 2c 20 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65  0, sizeof(fullte
3291b 78 74 5f 63 75 72 73 6f 72 29 29 3b 0a 20 20 20  xt_cursor));.   
3291c 20 2f 2a 20 73 71 6c 69 74 65 20 77 69 6c 6c 20   /* sqlite will 
3291d 69 6e 69 74 69 61 6c 69 7a 65 20 63 2d 3e 62 61  initialize c->ba
3291e 73 65 20 2a 2f 0a 20 20 20 20 2a 70 70 43 75 72  se */.    *ppCur
3291f 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a  sor = &c->base;.
32920 20 20 20 20 46 54 53 54 52 41 43 45 28 28 22 46      FTSTRACE(("F
32921 54 53 33 20 4f 70 65 6e 20 25 70 3a 20 25 70 5c  TS3 Open %p: %p\
32922 6e 22 2c 20 70 56 54 61 62 2c 20 63 29 29 3b 0a  n", pVTab, c));.
32923 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
32924 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
32925 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
32926 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 7d 0a 0a 2f  _NOMEM;.  }.}../
32927 2a 20 46 72 65 65 20 61 6c 6c 20 6f 66 20 74 68  * Free all of th
32928 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  e dynamically al
32929 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 68  located memory h
3292a 65 6c 64 20 62 79 20 74 68 65 0a 2a 2a 20 53 6e  eld by the.** Sn
3292b 69 70 70 65 74 0a 2a 2f 0a 73 74 61 74 69 63 20  ippet.*/.static 
3292c 76 6f 69 64 20 73 6e 69 70 70 65 74 43 6c 65 61  void snippetClea
3292d 72 28 53 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20  r(Snippet *p){. 
3292e 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d   sqlite3_free(p-
3292f 3e 61 4d 61 74 63 68 29 3b 0a 20 20 73 71 6c 69  >aMatch);.  sqli
32930 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 4f 66 66  te3_free(p->zOff
32931 73 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  set);.  sqlite3_
32932 66 72 65 65 28 70 2d 3e 7a 53 6e 69 70 70 65 74  free(p->zSnippet
32933 29 3b 0a 20 20 43 4c 45 41 52 28 70 29 3b 0a 7d  );.  CLEAR(p);.}
32934 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61  ../*.** Append a
32935 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 74 6f   single entry to
32936 20 74 68 65 20 70 2d 3e 61 4d 61 74 63 68 5b 5d   the p->aMatch[]
32937 20 6c 6f 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   log..*/.static 
32938 76 6f 69 64 20 73 6e 69 70 70 65 74 41 70 70 65  void snippetAppe
32939 6e 64 4d 61 74 63 68 28 0a 20 20 53 6e 69 70 70  ndMatch(.  Snipp
3293a 65 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  et *p,          
3293b 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74       /* Append t
3293c 68 65 20 65 6e 74 72 79 20 74 6f 20 74 68 69 73  he entry to this
3293d 20 73 6e 69 70 70 65 74 20 2a 2f 0a 20 20 69 6e   snippet */.  in
3293e 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 54 65 72  t iCol, int iTer
3293f 6d 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  m,      /* The c
32940 6f 6c 75 6d 6e 20 61 6e 64 20 71 75 65 72 79 20  olumn and query 
32941 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 54  term */.  int iT
32942 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  oken,           
32943 20 20 20 20 2f 2a 20 4d 61 74 63 68 69 6e 67 20      /* Matching 
32944 74 6f 6b 65 6e 20 69 6e 20 64 6f 63 75 6d 65 6e  token in documen
32945 74 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 72  t */.  int iStar
32946 74 2c 20 69 6e 74 20 6e 42 79 74 65 20 20 20 20  t, int nByte    
32947 20 2f 2a 20 4f 66 66 73 65 74 20 61 6e 64 20 73   /* Offset and s
32948 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 74 63 68  ize of the match
32949 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
3294a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74    struct snippet
3294b 4d 61 74 63 68 20 2a 70 4d 61 74 63 68 3b 0a 20  Match *pMatch;. 
3294c 20 69 66 28 20 70 2d 3e 6e 4d 61 74 63 68 2b 31   if( p->nMatch+1
3294d 3e 3d 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  >=p->nAlloc ){. 
3294e 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70     p->nAlloc = p
3294f 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 31 30 3b  ->nAlloc*2 + 10;
32950 0a 20 20 20 20 70 2d 3e 61 4d 61 74 63 68 20 3d  .    p->aMatch =
32951 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
32952 28 70 2d 3e 61 4d 61 74 63 68 2c 20 70 2d 3e 6e  (p->aMatch, p->n
32953 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 2d 3e  Alloc*sizeof(p->
32954 61 4d 61 74 63 68 5b 30 5d 29 20 29 3b 0a 20 20  aMatch[0]) );.  
32955 20 20 69 66 28 20 70 2d 3e 61 4d 61 74 63 68 3d    if( p->aMatch=
32956 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e  =0 ){.      p->n
32957 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20  Match = 0;.     
32958 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a   p->nAlloc = 0;.
32959 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
3295a 20 20 7d 0a 20 20 7d 0a 20 20 69 20 3d 20 70 2d    }.  }.  i = p-
3295b 3e 6e 4d 61 74 63 68 2b 2b 3b 0a 20 20 70 4d 61  >nMatch++;.  pMa
3295c 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61 74 63 68  tch = &p->aMatch
3295d 5b 69 5d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69  [i];.  pMatch->i
3295e 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 70 4d  Col = iCol;.  pM
3295f 61 74 63 68 2d 3e 69 54 65 72 6d 20 3d 20 69 54  atch->iTerm = iT
32960 65 72 6d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69  erm;.  pMatch->i
32961 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65 6e 3b 0a  Token = iToken;.
32962 20 20 70 4d 61 74 63 68 2d 3e 69 53 74 61 72 74    pMatch->iStart
32963 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 70 4d 61   = iStart;.  pMa
32964 74 63 68 2d 3e 6e 42 79 74 65 20 3d 20 6e 42 79  tch->nByte = nBy
32965 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a  te;.}../*.** Siz
32966 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
32967 66 6f 72 20 74 68 65 20 63 69 72 63 75 6c 61 72  for the circular
32968 20 62 75 66 66 65 72 20 75 73 65 64 20 69 6e 20   buffer used in 
32969 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66  snippetOffsetsOf
3296a 43 6f 6c 75 6d 6e 28 29 0a 2a 2f 0a 23 64 65 66  Column().*/.#def
3296b 69 6e 65 20 46 54 53 33 5f 52 4f 54 4f 52 5f 53  ine FTS3_ROTOR_S
3296c 5a 20 20 20 28 33 32 29 0a 23 64 65 66 69 6e 65  Z   (32).#define
3296d 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b   FTS3_ROTOR_MASK
3296e 20 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d   (FTS3_ROTOR_SZ-
3296f 31 29 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  1)../*.** Functi
32970 6f 6e 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  on to iterate th
32971 72 6f 75 67 68 20 74 68 65 20 74 6f 6b 65 6e 73  rough the tokens
32972 20 6f 66 20 61 20 63 6f 6d 70 69 6c 65 64 20 65   of a compiled e
32973 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
32974 20 45 78 63 65 70 74 2c 20 73 6b 69 70 20 61 6c   Except, skip al
32975 6c 20 74 6f 6b 65 6e 73 20 6f 6e 20 74 68 65 20  l tokens on the 
32976 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
32977 6f 66 20 61 20 4e 4f 54 20 6f 70 65 72 61 74 6f  of a NOT operato
32978 72 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  r..** This funct
32979 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 66  ion is used to f
3297a 69 6e 64 20 74 6f 6b 65 6e 73 20 61 73 20 70 61  ind tokens as pa
3297b 72 74 20 6f 66 20 73 6e 69 70 70 65 74 20 61 6e  rt of snippet an
3297c 64 20 6f 66 66 73 65 74 0a 2a 2a 20 67 65 6e 65  d offset.** gene
3297d 72 61 74 69 6f 6e 20 61 6e 64 20 77 65 20 64 6f  ration and we do
3297e 20 6e 74 20 77 61 6e 74 20 73 6e 69 70 70 65 74   nt want snippet
3297f 73 20 61 6e 64 20 6f 66 66 73 65 74 73 20 74 6f  s and offsets to
32980 20 72 65 70 6f 72 74 20 6d 61 74 63 68 65 73 0a   report matches.
32981 2a 2a 20 66 6f 72 20 74 6f 6b 65 6e 73 20 6f 6e  ** for tokens on
32982 20 74 68 65 20 52 48 53 20 6f 66 20 61 20 4e 4f   the RHS of a NO
32983 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  T..*/.static int
32984 20 66 74 73 33 4e 65 78 74 45 78 70 72 54 6f 6b   fts3NextExprTok
32985 65 6e 28 46 74 73 33 45 78 70 72 20 2a 2a 70 70  en(Fts3Expr **pp
32986 45 78 70 72 2c 20 69 6e 74 20 2a 70 69 54 6f 6b  Expr, int *piTok
32987 65 6e 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  en){.  Fts3Expr 
32988 2a 70 20 3d 20 2a 70 70 45 78 70 72 3b 0a 20 20  *p = *ppExpr;.  
32989 69 6e 74 20 69 54 6f 6b 65 6e 20 3d 20 2a 70 69  int iToken = *pi
3298a 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 69 54 6f  Token;.  if( iTo
3298b 6b 65 6e 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ken<0 ){.    /* 
3298c 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
3298d 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73   expression p is
3298e 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e 20   the root of an 
3298f 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
32990 0a 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 6f 20  .    ** Move to 
32991 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20  the first token 
32992 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
32993 6e 20 74 72 65 65 2e 0a 20 20 20 20 2a 2f 0a 20  n tree..    */. 
32994 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4c 65     while( p->pLe
32995 66 74 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20  ft ){.      p = 
32996 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 0a  p->pLeft;.    }.
32997 20 20 20 20 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a      iToken = 0;.
32998 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
32999 65 72 74 28 70 20 26 26 20 70 2d 3e 65 54 79 70  ert(p && p->eTyp
3299a 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e==FTSQUERY_PHRA
3299b 53 45 20 29 3b 0a 20 20 20 20 69 66 28 20 69 54  SE );.    if( iT
3299c 6f 6b 65 6e 3c 28 70 2d 3e 70 50 68 72 61 73 65  oken<(p->pPhrase
3299d 2d 3e 6e 54 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20  ->nToken-1) ){. 
3299e 20 20 20 20 20 69 54 6f 6b 65 6e 2b 2b 3b 0a 20       iToken++;. 
3299f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
329a0 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20  iToken = 0;.    
329a1 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 61 72    while( p->pPar
329a2 65 6e 74 20 26 26 20 70 2d 3e 70 50 61 72 65 6e  ent && p->pParen
329a3 74 2d 3e 70 4c 65 66 74 21 3d 70 20 29 7b 0a 20  t->pLeft!=p ){. 
329a4 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
329a5 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68  ->pParent->pRigh
329a6 74 3d 3d 70 20 29 3b 0a 20 20 20 20 20 20 20 20  t==p );.        
329a7 70 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b 0a  p = p->pParent;.
329a8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 20        }.      p 
329a9 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  = p->pParent;.  
329aa 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
329ab 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
329ac 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 20  pRight!=0 );.   
329ad 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67       p = p->pRig
329ae 68 74 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ht;.        whil
329af 65 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b 0a 20  e( p->pLeft ){. 
329b0 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e           p = p->
329b1 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 7d  pLeft;.        }
329b2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
329b3 20 7d 0a 0a 20 20 2a 70 70 45 78 70 72 20 3d 20   }..  *ppExpr = 
329b4 70 3b 0a 20 20 2a 70 69 54 6f 6b 65 6e 20 3d 20  p;.  *piToken = 
329b5 69 54 6f 6b 65 6e 3b 0a 20 20 72 65 74 75 72 6e  iToken;.  return
329b6 20 70 3f 31 3a 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   p?1:0;.}../*.**
329b7 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
329b8 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  the expression n
329b9 6f 64 65 20 70 45 78 70 72 20 69 73 20 6c 6f 63  ode pExpr is loc
329ba 61 74 65 64 20 62 65 6e 65 61 74 68 20 74 68 65  ated beneath the
329bb 0a 2a 2a 20 52 48 53 20 6f 66 20 61 20 4e 4f 54  .** RHS of a NOT
329bc 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74   operator..*/.st
329bd 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 78 70  atic int fts3Exp
329be 72 42 65 6e 65 61 74 68 4e 6f 74 28 46 74 73 33  rBeneathNot(Fts3
329bf 45 78 70 72 20 2a 70 29 7b 0a 20 20 46 74 73 33  Expr *p){.  Fts3
329c0 45 78 70 72 20 2a 70 50 61 72 65 6e 74 3b 0a 20  Expr *pParent;. 
329c1 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
329c2 20 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50   pParent = p->pP
329c3 61 72 65 6e 74 3b 0a 20 20 20 20 69 66 28 20 70  arent;.    if( p
329c4 50 61 72 65 6e 74 20 26 26 20 70 50 61 72 65 6e  Parent && pParen
329c5 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  t->eType==FTSQUE
329c6 52 59 5f 4e 4f 54 20 26 26 20 70 50 61 72 65 6e  RY_NOT && pParen
329c7 74 2d 3e 70 52 69 67 68 74 3d 3d 70 20 29 7b 0a  t->pRight==p ){.
329c8 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
329c9 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 50      }.    p = pP
329ca 61 72 65 6e 74 3b 0a 20 20 7d 0a 20 20 72 65 74  arent;.  }.  ret
329cb 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
329cc 41 64 64 20 65 6e 74 72 69 65 73 20 74 6f 20 70  Add entries to p
329cd 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b  Snippet->aMatch[
329ce 5d 20 66 6f 72 20 65 76 65 72 79 20 6d 61 74 63  ] for every matc
329cf 68 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 67  h that occurs ag
329d0 61 69 6e 73 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e  ainst.** documen
329d1 74 20 7a 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31  t zDoc[0..nDoc-1
329d2 5d 20 77 68 69 63 68 20 69 73 20 73 74 6f 72 65  ] which is store
329d3 64 20 69 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c  d in column iCol
329d4 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  umn..*/.static v
329d5 6f 69 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65  oid snippetOffse
329d6 74 73 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20 66 75  tsOfColumn(.  fu
329d7 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
329d8 43 75 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Cur,         /* 
329d9 54 68 65 20 66 75 6c 6c 74 65 73 74 20 73 65 61  The fulltest sea
329da 72 63 68 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  rch cursor */.  
329db 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65  Snippet *pSnippe
329dc 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t,             /
329dd 2a 20 54 68 65 20 53 6e 69 70 70 65 74 20 6f 62  * The Snippet ob
329de 6a 65 63 74 20 74 6f 20 62 65 20 66 69 6c 6c 65  ject to be fille
329df 64 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43  d in */.  int iC
329e0 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
329e1 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
329e2 78 20 6f 66 20 66 75 6c 6c 74 65 78 74 20 74 61  x of fulltext ta
329e3 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  ble column */.  
329e4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63  const char *zDoc
329e5 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
329e6 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 66 75  * Text of the fu
329e7 6c 6c 74 65 78 74 20 74 61 62 6c 65 20 63 6f 6c  lltext table col
329e8 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f  umn */.  int nDo
329e9 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
329ea 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
329eb 68 20 6f 66 20 7a 44 6f 63 20 69 6e 20 62 79 74  h of zDoc in byt
329ec 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  es */.){.  const
329ed 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
329ee 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 54 4d 6f 64  er_module *pTMod
329ef 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 6f 6b  ule;  /* The tok
329f0 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f  enizer module */
329f1 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
329f2 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
329f3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
329f4 20 54 68 65 20 73 70 65 63 69 66 69 63 20 74 6f   The specific to
329f5 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 73 71 6c  kenizer */.  sql
329f6 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
329f7 75 72 73 6f 72 20 2a 70 54 43 75 72 73 6f 72 3b  ursor *pTCursor;
329f8 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e          /* Token
329f9 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
329fa 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
329fb 70 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 20  pVtab;          
329fc 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6c        /* The ful
329fd 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a  l text index */.
329fe 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20    int nColumn;  
329ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
32a01 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
32a02 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  he index */.  in
32a03 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
32a04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a05 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
32a06 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ers */.  int rc;
32a07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32a09 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
32a0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d    unsigned int m
32a0b 61 74 63 68 2c 20 70 72 65 76 4d 61 74 63 68 3b  atch, prevMatch;
32a0c 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65         /* Phrase
32a0d 20 73 65 61 72 63 68 20 62 69 74 6d 61 73 6b 73   search bitmasks
32a0e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
32a0f 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20   *zToken;       
32a10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
32a11 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 74 68  xt token from th
32a12 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20  e tokenizer */. 
32a13 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20   int nToken;    
32a14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a15 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
32a16 20 7a 54 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74   zToken */.  int
32a17 20 69 42 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69   iBegin, iEnd, i
32a18 50 6f 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  Pos;            
32a19 20 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66 20    /* Offsets of 
32a1a 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e  beginning and en
32a1b 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66  d */..  /* The f
32a1c 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
32a1d 65 73 20 6b 65 65 70 20 61 20 63 69 72 63 75 6c  es keep a circul
32a1e 61 72 20 62 75 66 66 65 72 20 6f 66 20 74 68 65  ar buffer of the
32a1f 20 6c 61 73 74 0a 20 20 2a 2a 20 66 65 77 20 74   last.  ** few t
32a20 6f 6b 65 6e 73 20 2a 2f 0a 20 20 75 6e 73 69 67  okens */.  unsig
32a21 6e 65 64 20 69 6e 74 20 69 52 6f 74 6f 72 20 3d  ned int iRotor =
32a22 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
32a23 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72  /* Index of curr
32a24 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69  ent token */.  i
32a25 6e 74 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 46  nt iRotorBegin[F
32a26 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b 20 20  TS3_ROTOR_SZ];  
32a27 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67      /* Beginning
32a28 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
32a29 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 74 6f 72   */.  int iRotor
32a2a 4c 65 6e 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53  Len[FTS3_ROTOR_S
32a2b 5a 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  Z];        /* Le
32a2c 6e 67 74 68 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  ngth of token */
32a2d 0a 0a 20 20 70 56 74 61 62 20 3d 20 63 75 72 73  ..  pVtab = curs
32a2e 6f 72 5f 76 74 61 62 28 70 43 75 72 29 3b 0a 20  or_vtab(pCur);. 
32a2f 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 56 74 61 62   nColumn = pVtab
32a30 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 70 54 6f  ->nColumn;.  pTo
32a31 6b 65 6e 69 7a 65 72 20 3d 20 70 56 74 61 62 2d  kenizer = pVtab-
32a32 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 70  >pTokenizer;.  p
32a33 54 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e  TModule = pToken
32a34 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  izer->pModule;. 
32a35 20 72 63 20 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e   rc = pTModule->
32a36 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72  xOpen(pTokenizer
32a37 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 26 70  , zDoc, nDoc, &p
32a38 54 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20  TCursor);.  if( 
32a39 72 63 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  rc ) return;.  p
32a3a 54 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69  TCursor->pTokeni
32a3b 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72  zer = pTokenizer
32a3c 3b 0a 0a 20 20 70 72 65 76 4d 61 74 63 68 20 3d  ;..  prevMatch =
32a3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 21 70 54   0;.  while( !pT
32a3e 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 54  Module->xNext(pT
32a3f 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c  Cursor, &zToken,
32a40 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69   &nToken, &iBegi
32a41 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29  n, &iEnd, &iPos)
32a42 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78 70 72   ){.    Fts3Expr
32a43 20 2a 70 49 74 65 72 20 3d 20 70 43 75 72 2d 3e   *pIter = pCur->
32a44 70 45 78 70 72 3b 0a 20 20 20 20 69 6e 74 20 69  pExpr;.    int i
32a45 49 74 65 72 20 3d 20 2d 31 3b 0a 20 20 20 20 69  Iter = -1;.    i
32a46 52 6f 74 6f 72 42 65 67 69 6e 5b 69 52 6f 74 6f  RotorBegin[iRoto
32a47 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53  r&FTS3_ROTOR_MAS
32a48 4b 5d 20 3d 20 69 42 65 67 69 6e 3b 0a 20 20 20  K] = iBegin;.   
32a49 20 69 52 6f 74 6f 72 4c 65 6e 5b 69 52 6f 74 6f   iRotorLen[iRoto
32a4a 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53  r&FTS3_ROTOR_MAS
32a4b 4b 5d 20 3d 20 69 45 6e 64 2d 69 42 65 67 69 6e  K] = iEnd-iBegin
32a4c 3b 0a 20 20 20 20 6d 61 74 63 68 20 3d 20 30 3b  ;.    match = 0;
32a4d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
32a4e 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d 31  (FTS3_ROTOR_SZ-1
32a4f 29 20 26 26 20 66 74 73 33 4e 65 78 74 45 78 70  ) && fts3NextExp
32a50 72 54 6f 6b 65 6e 28 26 70 49 74 65 72 2c 20 26  rToken(&pIter, &
32a51 69 49 74 65 72 29 3b 20 69 2b 2b 29 7b 0a 20 20  iIter); i++){.  
32a52 20 20 20 20 69 6e 74 20 6e 50 68 72 61 73 65 3b      int nPhrase;
32a53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a54 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
32a55 20 74 6f 6b 65 6e 73 20 69 6e 20 63 75 72 72 65   tokens in curre
32a56 6e 74 20 70 68 72 61 73 65 20 2a 2f 0a 20 20 20  nt phrase */.   
32a57 20 20 20 73 74 72 75 63 74 20 50 68 72 61 73 65     struct Phrase
32a58 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 20 20  Token *pToken;  
32a59 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 74 6f     /* Current to
32a5a 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ken */.      int
32a5b 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   iCol;          
32a5c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32a5d 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 2a 2f 0a  Column index */.
32a5e 0a 20 20 20 20 20 20 69 66 28 20 66 74 73 33 45  .      if( fts3E
32a5f 78 70 72 42 65 6e 65 61 74 68 4e 6f 74 28 70 49  xprBeneathNot(pI
32a60 74 65 72 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ter) ) continue;
32a61 0a 20 20 20 20 20 20 6e 50 68 72 61 73 65 20 3d  .      nPhrase =
32a62 20 70 49 74 65 72 2d 3e 70 50 68 72 61 73 65 2d   pIter->pPhrase-
32a63 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 70  >nToken;.      p
32a64 54 6f 6b 65 6e 20 3d 20 26 70 49 74 65 72 2d 3e  Token = &pIter->
32a65 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
32a66 69 49 74 65 72 5d 3b 0a 20 20 20 20 20 20 69 43  iIter];.      iC
32a67 6f 6c 20 3d 20 70 49 74 65 72 2d 3e 70 50 68 72  ol = pIter->pPhr
32a68 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  ase->iColumn;.  
32a69 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20      if( iCol>=0 
32a6a 26 26 20 69 43 6f 6c 3c 6e 43 6f 6c 75 6d 6e 20  && iCol<nColumn 
32a6b 26 26 20 69 43 6f 6c 21 3d 69 43 6f 6c 75 6d 6e  && iCol!=iColumn
32a6c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
32a6d 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 6e     if( pToken->n
32a6e 3e 6e 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69 6e  >nToken ) contin
32a6f 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  ue;.      if( !p
32a70 54 6f 6b 65 6e 2d 3e 69 73 50 72 65 66 69 78 20  Token->isPrefix 
32a71 26 26 20 70 54 6f 6b 65 6e 2d 3e 6e 3c 6e 54 6f  && pToken->n<nTo
32a72 6b 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ken ) continue;.
32a73 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
32a74 6f 6b 65 6e 2d 3e 6e 3c 3d 6e 54 6f 6b 65 6e 20  oken->n<=nToken 
32a75 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  );.      if( mem
32a76 63 6d 70 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 7a  cmp(pToken->z, z
32a77 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 6e  Token, pToken->n
32a78 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
32a79 20 20 20 20 69 66 28 20 69 49 74 65 72 3e 30 20      if( iIter>0 
32a7a 26 26 20 28 70 72 65 76 4d 61 74 63 68 20 26 20  && (prevMatch & 
32a7b 28 31 3c 3c 69 29 29 3d 3d 30 20 29 20 63 6f 6e  (1<<i))==0 ) con
32a7c 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 6d 61 74  tinue;.      mat
32a7d 63 68 20 7c 3d 20 31 3c 3c 69 3b 0a 20 20 20 20  ch |= 1<<i;.    
32a7e 20 20 69 66 28 20 69 3d 3d 28 46 54 53 33 5f 52    if( i==(FTS3_R
32a7f 4f 54 4f 52 5f 53 5a 2d 32 29 20 7c 7c 20 6e 50  OTOR_SZ-2) || nP
32a80 68 72 61 73 65 3d 3d 69 49 74 65 72 2b 31 20 29  hrase==iIter+1 )
32a81 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  {.        for(j=
32a82 6e 50 68 72 61 73 65 2d 31 3b 20 6a 3e 3d 30 3b  nPhrase-1; j>=0;
32a83 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20   j--){.         
32a84 20 69 6e 74 20 6b 20 3d 20 28 69 52 6f 74 6f 72   int k = (iRotor
32a85 2d 6a 29 20 26 20 46 54 53 33 5f 52 4f 54 4f 52  -j) & FTS3_ROTOR
32a86 5f 4d 41 53 4b 3b 0a 20 20 20 20 20 20 20 20 20  _MASK;.         
32a87 20 73 6e 69 70 70 65 74 41 70 70 65 6e 64 4d 61   snippetAppendMa
32a88 74 63 68 28 70 53 6e 69 70 70 65 74 2c 20 69 43  tch(pSnippet, iC
32a89 6f 6c 75 6d 6e 2c 20 69 2d 6a 2c 20 69 50 6f 73  olumn, i-j, iPos
32a8a 2d 6a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  -j,.            
32a8b 20 20 20 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b      iRotorBegin[
32a8c 6b 5d 2c 20 69 52 6f 74 6f 72 4c 65 6e 5b 6b 5d  k], iRotorLen[k]
32a8d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
32a8e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
32a8f 72 65 76 4d 61 74 63 68 20 3d 20 6d 61 74 63 68  revMatch = match
32a90 3c 3c 31 3b 0a 20 20 20 20 69 52 6f 74 6f 72 2b  <<1;.    iRotor+
32a91 2b 3b 0a 20 20 7d 0a 20 20 70 54 4d 6f 64 75 6c  +;.  }.  pTModul
32a92 65 2d 3e 78 43 6c 6f 73 65 28 70 54 43 75 72 73  e->xClose(pTCurs
32a93 6f 72 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  or);  .}../*.** 
32a94 52 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66  Remove entries f
32a95 72 6f 6d 20 74 68 65 20 70 53 6e 69 70 70 65 74  rom the pSnippet
32a96 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 61 63   structure to ac
32a97 63 6f 75 6e 74 20 66 6f 72 20 74 68 65 20 4e 45  count for the NE
32a98 41 52 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 20  AR.** operator. 
32a99 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c  When this is cal
32a9a 6c 65 64 2c 20 70 53 6e 69 70 70 65 74 20 63 6f  led, pSnippet co
32a9b 6e 74 61 69 6e 73 20 74 68 65 20 6c 69 73 74 20  ntains the list 
32a9c 6f 66 20 74 6f 6b 65 6e 20 0a 2a 2a 20 6f 66 66  of token .** off
32a9d 73 65 74 73 20 70 72 6f 64 75 63 65 64 20 62 79  sets produced by
32a9e 20 74 72 65 61 74 69 6e 67 20 61 6c 6c 20 4e 45   treating all NE
32a9f 41 52 20 6f 70 65 72 61 74 6f 72 73 20 61 73 20  AR operators as 
32aa0 41 4e 44 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  AND operators..*
32aa1 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
32aa2 72 65 6d 6f 76 65 73 20 61 6e 79 20 65 6e 74 72  removes any entr
32aa3 69 65 73 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ies that should 
32aa4 6e 6f 74 20 62 65 20 70 72 65 73 65 6e 74 20 61  not be present a
32aa5 66 74 65 72 0a 2a 2a 20 61 63 63 6f 75 6e 74 69  fter.** accounti
32aa6 6e 67 20 66 6f 72 20 74 68 65 20 4e 45 41 52 20  ng for the NEAR 
32aa7 72 65 73 74 72 69 63 74 69 6f 6e 2e 20 46 6f 72  restriction. For
32aa8 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
32aa9 20 71 75 65 72 69 65 64 0a 2a 2a 20 64 6f 63 75   queried.** docu
32aaa 6d 65 6e 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ment is:.**.**  
32aab 20 20 20 22 41 20 42 20 43 20 44 20 45 20 41 22     "A B C D E A"
32aac 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 71  .**.** and the q
32aad 75 65 72 79 20 69 73 3a 0a 2a 2a 20 0a 2a 2a 20  uery is:.** .** 
32aae 20 20 20 20 41 20 4e 45 41 52 2f 30 20 45 0a 2a      A NEAR/0 E.*
32aaf 2a 0a 2a 2a 20 74 68 65 6e 20 77 68 65 6e 20 74  *.** then when t
32ab0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
32ab1 63 61 6c 6c 65 64 20 74 68 65 20 53 6e 69 70 70  called the Snipp
32ab2 65 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f 6b 65  et contains toke
32ab3 6e 20 6f 66 66 73 65 74 73 0a 2a 2a 20 30 2c 20  n offsets.** 0, 
32ab4 34 20 61 6e 64 20 35 2e 20 54 68 69 73 20 66 75  4 and 5. This fu
32ab5 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65 73 20 74  nction removes t
32ab6 68 65 20 22 30 22 20 65 6e 74 72 79 20 28 62 65  he "0" entry (be
32ab7 63 61 75 73 65 20 74 68 65 20 66 69 72 73 74 20  cause the first 
32ab8 41 0a 2a 2a 20 69 73 20 6e 6f 74 20 6e 65 61 72  A.** is not near
32ab9 20 65 6e 6f 75 67 68 20 74 6f 20 61 6e 20 45 29   enough to an E)
32aba 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
32abb 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
32abc 6c 6c 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20  lled, the value 
32abd 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 61  pointed to by pa
32abe 72 61 6d 65 74 65 72 20 70 69 4c 65 66 74 20 69  rameter piLeft i
32abf 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72  s.** the integer
32ac0 20 69 64 20 6f 66 20 74 68 65 20 6c 65 66 74 2d   id of the left-
32ac1 6d 6f 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74 68  most token in th
32ac2 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  e expression tre
32ac3 65 20 68 65 61 64 65 64 20 62 79 0a 2a 2a 20 70  e headed by.** p
32ac4 45 78 70 72 2e 20 54 68 69 73 20 66 75 6e 63 74  Expr. This funct
32ac5 69 6f 6e 20 69 6e 63 72 65 6d 65 6e 74 73 20 2a  ion increments *
32ac6 70 69 4c 65 66 74 20 62 79 20 74 68 65 20 74 6f  piLeft by the to
32ac7 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f  tal number of to
32ac8 6b 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 65  kens.** in the e
32ac9 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68  xpression tree h
32aca 65 61 64 65 64 20 62 79 20 70 45 78 70 72 2e 0a  eaded by pExpr..
32acb 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69  **.** Return 1 i
32acc 66 20 61 6e 79 20 74 72 69 6d 6d 69 6e 67 20 6f  f any trimming o
32acd 63 63 75 72 73 2e 20 20 52 65 74 75 72 6e 20 30  ccurs.  Return 0
32ace 20 69 66 20 6e 6f 20 74 72 69 6d 6d 69 6e 67 20   if no trimming 
32acf 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a  is required..*/.
32ad0 73 74 61 74 69 63 20 69 6e 74 20 74 72 69 6d 53  static int trimS
32ad1 6e 69 70 70 65 74 4f 66 66 73 65 74 73 28 0a 20  nippetOffsets(. 
32ad2 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72   Fts3Expr *pExpr
32ad3 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 65  ,      /* The se
32ad4 61 72 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20  arch expression 
32ad5 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 2a 70 53  */.  Snippet *pS
32ad6 6e 69 70 70 65 74 2c 20 20 20 20 2f 2a 20 54 68  nippet,    /* Th
32ad7 65 20 73 65 74 20 6f 66 20 73 6e 69 70 70 65 74  e set of snippet
32ad8 20 6f 66 66 73 65 74 73 20 74 6f 20 62 65 20 74   offsets to be t
32ad9 72 69 6d 6d 65 64 20 2a 2f 0a 20 20 69 6e 74 20  rimmed */.  int 
32ada 2a 70 69 4c 65 66 74 20 20 20 20 20 20 20 20 20  *piLeft         
32adb 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6c 65    /* Index of le
32adc 66 74 2d 6d 6f 73 74 20 74 6f 6b 65 6e 20 69 6e  ft-most token in
32add 20 70 45 78 70 72 20 2a 2f 0a 29 7b 0a 20 20 69   pExpr */.){.  i
32ade 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20  f( pExpr ){.    
32adf 69 66 28 20 74 72 69 6d 53 6e 69 70 70 65 74 4f  if( trimSnippetO
32ae0 66 66 73 65 74 73 28 70 45 78 70 72 2d 3e 70 4c  ffsets(pExpr->pL
32ae1 65 66 74 2c 20 70 53 6e 69 70 70 65 74 2c 20 70  eft, pSnippet, p
32ae2 69 4c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20  iLeft) ){.      
32ae3 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
32ae4 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 45 78  .    switch( pEx
32ae5 70 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20 20 20  pr->eType ){.   
32ae6 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
32ae7 5f 50 48 52 41 53 45 3a 0a 20 20 20 20 20 20 20  _PHRASE:.       
32ae8 20 2a 70 69 4c 65 66 74 20 2b 3d 20 70 45 78 70   *piLeft += pExp
32ae9 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  r->pPhrase->nTok
32aea 65 6e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  en;.        brea
32aeb 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 46 54  k;.      case FT
32aec 53 51 55 45 52 59 5f 4e 45 41 52 3a 20 7b 0a 20  SQUERY_NEAR: {. 
32aed 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69         /* The ri
32aee 67 68 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66  ght-hand-side of
32aef 20 61 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72   a NEAR operator
32af0 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 68 72   is always a phr
32af1 61 73 65 2e 20 54 68 65 0a 20 20 20 20 20 20 20  ase. The.       
32af2 20 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69   ** left-hand-si
32af3 64 65 20 69 73 20 65 69 74 68 65 72 20 61 20 70  de is either a p
32af4 68 72 61 73 65 20 6f 72 20 61 6e 20 65 78 70 72  hrase or an expr
32af5 65 73 73 69 6f 6e 20 74 72 65 65 20 74 68 61 74  ession tree that
32af6 20 69 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20   is .        ** 
32af7 69 74 73 65 6c 66 20 68 65 61 64 65 64 20 62 79  itself headed by
32af8 20 61 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72   a NEAR operator
32af9 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
32afa 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 73 0a  initializations.
32afb 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6c          ** set l
32afc 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 69 4c  ocal variable iL
32afd 65 66 74 20 74 6f 20 74 68 65 20 74 6f 6b 65 6e  eft to the token
32afe 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c   number of the l
32aff 65 66 74 2d 6d 6f 73 74 0a 20 20 20 20 20 20 20  eft-most.       
32b00 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65   ** token in the
32b01 20 72 69 67 68 74 2d 68 61 6e 64 20 70 68 72 61   right-hand phra
32b02 73 65 2c 20 61 6e 64 20 69 52 69 67 68 74 20 74  se, and iRight t
32b03 6f 20 74 68 65 20 72 69 67 68 74 20 6d 6f 73 74  o the right most
32b04 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65  .        ** toke
32b05 6e 20 69 6e 20 74 68 65 20 73 61 6d 65 20 70 68  n in the same ph
32b06 72 61 73 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c  rase. For exampl
32b07 65 2c 20 69 66 20 77 65 20 68 61 64 3a 0a 20 20  e, if we had:.  
32b08 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
32b09 20 2a 2a 20 20 20 20 20 3c 63 6f 6c 3e 20 4d 41   **     <col> MA
32b0a 54 43 48 20 27 22 61 62 63 20 64 65 66 22 20 4e  TCH '"abc def" N
32b0b 45 41 52 2f 32 20 22 67 68 69 20 6a 6b 6c 22 27  EAR/2 "ghi jkl"'
32b0c 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
32b0d 20 20 20 20 2a 2a 20 74 68 65 6e 20 69 4c 65 66      ** then iLef
32b0e 74 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f  t will be set to
32b0f 20 32 20 28 74 6f 6b 65 6e 20 6e 75 6d 62 65 72   2 (token number
32b10 20 6f 66 20 67 68 69 29 20 61 6e 64 20 6e 54 6f   of ghi) and nTo
32b11 6b 65 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 20  ken will.       
32b12 20 2a 2a 20 62 65 20 73 65 74 20 74 6f 20 34 2e   ** be set to 4.
32b13 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
32b14 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c      Fts3Expr *pL
32b15 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  eft = pExpr->pLe
32b16 66 74 3b 0a 20 20 20 20 20 20 20 20 46 74 73 33  ft;.        Fts3
32b17 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70  Expr *pRight = p
32b18 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20  Expr->pRight;.  
32b19 20 20 20 20 20 20 69 6e 74 20 69 4c 65 66 74 20        int iLeft 
32b1a 3d 20 2a 70 69 4c 65 66 74 3b 0a 20 20 20 20 20  = *piLeft;.     
32b1b 20 20 20 69 6e 74 20 6e 4e 65 61 72 20 3d 20 70     int nNear = p
32b1c 45 78 70 72 2d 3e 6e 4e 65 61 72 3b 0a 20 20 20  Expr->nNear;.   
32b1d 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20       int nToken 
32b1e 3d 20 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73  = pRight->pPhras
32b1f 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  e->nToken;.     
32b20 20 20 20 69 6e 74 20 6a 6a 2c 20 69 69 3b 0a 20     int jj, ii;. 
32b21 20 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74         if( pLeft
32b22 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
32b23 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20  Y_NEAR ){.      
32b24 20 20 20 20 70 4c 65 66 74 20 3d 20 70 4c 65 66      pLeft = pLef
32b25 74 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20  t->pRight;.     
32b26 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73     }.        ass
32b27 65 72 74 28 20 70 52 69 67 68 74 2d 3e 65 54 79  ert( pRight->eTy
32b28 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe==FTSQUERY_PHR
32b29 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ASE );.        a
32b2a 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 65 54  ssert( pLeft->eT
32b2b 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
32b2c 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20  RASE );.        
32b2d 6e 54 6f 6b 65 6e 20 2b 3d 20 70 4c 65 66 74 2d  nToken += pLeft-
32b2e 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
32b2f 3b 0a 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ;..        for(i
32b30 69 3d 30 3b 20 69 69 3c 70 53 6e 69 70 70 65 74  i=0; ii<pSnippet
32b31 2d 3e 6e 4d 61 74 63 68 3b 20 69 69 2b 2b 29 7b  ->nMatch; ii++){
32b32 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63  .          struc
32b33 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a  t snippetMatch *
32b34 70 20 3d 20 26 70 53 6e 69 70 70 65 74 2d 3e 61  p = &pSnippet->a
32b35 4d 61 74 63 68 5b 69 69 5d 3b 0a 20 20 20 20 20  Match[ii];.     
32b36 20 20 20 20 20 69 66 28 20 70 2d 3e 69 54 65 72       if( p->iTer
32b37 6d 3d 3d 69 4c 65 66 74 20 29 7b 0a 20 20 20 20  m==iLeft ){.    
32b38 20 20 20 20 20 20 20 20 69 6e 74 20 69 73 4f 6b          int isOk
32b39 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
32b3a 20 20 2f 2a 20 53 6e 69 70 70 65 74 20 69 69 20    /* Snippet ii 
32b3b 69 73 20 61 6e 20 6f 63 63 75 72 65 6e 63 65 20  is an occurence 
32b3c 6f 66 20 71 75 65 72 79 20 74 65 72 6d 20 69 4c  of query term iL
32b3d 65 66 74 20 69 6e 20 74 68 65 20 64 6f 63 75 6d  eft in the docum
32b3e 65 6e 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20  ent..           
32b3f 20 2a 2a 20 49 74 20 6f 63 63 75 72 73 20 61 74   ** It occurs at
32b40 20 70 6f 73 69 74 69 6f 6e 20 28 70 2d 3e 69 54   position (p->iT
32b41 6f 6b 65 6e 29 20 6f 66 20 74 68 65 20 64 6f 63  oken) of the doc
32b42 75 6d 65 6e 74 2e 20 57 65 20 6e 6f 77 0a 20 20  ument. We now.  
32b43 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 65 61            ** sea
32b44 72 63 68 20 66 6f 72 20 61 6e 20 69 6e 73 74 61  rch for an insta
32b45 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 20 28 69 4c  nce of token (iL
32b46 65 66 74 2d 31 29 20 73 6f 6d 65 77 68 65 72 65  eft-1) somewhere
32b47 20 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 20   in the .       
32b48 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 28 70       ** range (p
32b49 2d 3e 69 54 6f 6b 65 6e 20 2d 20 6e 4e 65 61 72  ->iToken - nNear
32b4a 29 2e 2e 2e 28 70 2d 3e 69 54 6f 6b 65 6e 20 2b  )...(p->iToken +
32b4b 20 6e 4e 65 61 72 20 2b 20 6e 54 6f 6b 65 6e 29   nNear + nToken)
32b4c 20 77 69 74 68 69 6e 20 0a 20 20 20 20 20 20 20   within .       
32b4d 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65 74 20       ** the set 
32b4e 6f 66 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20  of snippetMatch 
32b4f 73 74 72 75 63 74 75 72 65 73 2e 20 49 66 20 6f  structures. If o
32b50 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 70 72 6f  ne is found, pro
32b51 63 65 65 64 2e 20 0a 20 20 20 20 20 20 20 20 20  ceed. .         
32b52 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 63 61 6e     ** If one can
32b53 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 74 68  not be found, th
32b54 65 6e 20 72 65 6d 6f 76 65 20 73 6e 69 70 70 65  en remove snippe
32b55 74 73 20 69 69 2e 2e 28 69 69 2b 4e 2d 31 29 20  ts ii..(ii+N-1) 
32b56 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
32b57 66 72 6f 6d 20 74 68 65 20 6d 61 74 63 68 69 6e  from the matchin
32b58 67 20 73 6e 69 70 70 65 74 73 2c 20 77 68 65 72  g snippets, wher
32b59 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
32b5a 72 20 6f 66 20 74 6f 6b 65 6e 73 20 0a 20 20 20  r of tokens .   
32b5b 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 70           ** in p
32b5c 68 72 61 73 65 20 70 52 69 67 68 74 2d 3e 70 50  hrase pRight->pP
32b5d 68 72 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20  hrase..         
32b5e 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
32b5f 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b    for(jj=0; isOk
32b60 3d 3d 30 20 26 26 20 6a 6a 3c 70 53 6e 69 70 70  ==0 && jj<pSnipp
32b61 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b  et->nMatch; jj++
32b62 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
32b63 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d   struct snippetM
32b64 61 74 63 68 20 2a 70 32 20 3d 20 26 70 53 6e 69  atch *p2 = &pSni
32b65 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d  ppet->aMatch[jj]
32b66 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
32b67 69 66 28 20 70 32 2d 3e 69 54 65 72 6d 3d 3d 28  if( p2->iTerm==(
32b68 69 4c 65 66 74 2d 31 29 20 29 7b 0a 20 20 20 20  iLeft-1) ){.    
32b69 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
32b6a 70 32 2d 3e 69 54 6f 6b 65 6e 3e 3d 28 70 2d 3e  p2->iToken>=(p->
32b6b 69 54 6f 6b 65 6e 2d 6e 4e 65 61 72 2d 31 29 20  iToken-nNear-1) 
32b6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32b6d 20 20 26 26 20 70 32 2d 3e 69 54 6f 6b 65 6e 3c    && p2->iToken<
32b6e 28 70 2d 3e 69 54 6f 6b 65 6e 2b 6e 4e 65 61 72  (p->iToken+nNear
32b6f 2b 6e 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20 20  +nToken) .      
32b70 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20            ){.   
32b71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
32b72 73 4f 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  sOk = 1;.       
32b73 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
32b74 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
32b75 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32b76 20 20 20 20 20 69 66 28 20 21 69 73 4f 6b 20 29       if( !isOk )
32b77 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
32b78 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20 20 20  int kk;.        
32b79 20 20 20 20 20 20 66 6f 72 28 6b 6b 3d 30 3b 20        for(kk=0; 
32b7a 6b 6b 3c 70 52 69 67 68 74 2d 3e 70 50 68 72 61  kk<pRight->pPhra
32b7b 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b  se->nToken; kk++
32b7c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
32b7d 20 20 20 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61     pSnippet->aMa
32b7e 74 63 68 5b 6b 6b 2b 69 69 5d 2e 69 54 65 72 6d  tch[kk+ii].iTerm
32b7f 20 3d 20 2d 32 3b 0a 20 20 20 20 20 20 20 20 20   = -2;.         
32b80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
32b81 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
32b82 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
32b83 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32b84 20 20 20 69 66 28 20 70 2d 3e 69 54 65 72 6d 3d     if( p->iTerm=
32b85 3d 28 69 4c 65 66 74 2d 31 29 20 29 7b 0a 20 20  =(iLeft-1) ){.  
32b86 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 73            int is
32b87 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  Ok = 0;.        
32b88 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 69 73      for(jj=0; is
32b89 4f 6b 3d 3d 30 20 26 26 20 6a 6a 3c 70 53 6e 69  Ok==0 && jj<pSni
32b8a 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 6a 6a  ppet->nMatch; jj
32b8b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
32b8c 20 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65     struct snippe
32b8d 74 4d 61 74 63 68 20 2a 70 32 20 3d 20 26 70 53  tMatch *p2 = &pS
32b8e 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6a  nippet->aMatch[j
32b8f 6a 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  j];.            
32b90 20 20 69 66 28 20 70 32 2d 3e 69 54 65 72 6d 3d    if( p2->iTerm=
32b91 3d 69 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  =iLeft ){.      
32b92 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 32            if( p2
32b93 2d 3e 69 54 6f 6b 65 6e 3c 3d 28 70 2d 3e 69 54  ->iToken<=(p->iT
32b94 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 31 29 20 0a 20  oken+nNear+1) . 
32b95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b96 26 26 20 70 32 2d 3e 69 54 6f 6b 65 6e 3e 28 70  && p2->iToken>(p
32b97 2d 3e 69 54 6f 6b 65 6e 2d 6e 4e 65 61 72 2d 6e  ->iToken-nNear-n
32b98 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20 20 20 20  Token) .        
32b99 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
32b9a 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 4f               isO
32b9b 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  k = 1;.         
32b9c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32b9d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32b9e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
32b9f 20 20 20 69 66 28 20 21 69 73 4f 6b 20 29 7b 0a     if( !isOk ){.
32ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
32ba1 74 20 6b 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  t kk;.          
32ba2 20 20 20 20 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b      for(kk=0; kk
32ba3 3c 70 4c 65 66 74 2d 3e 70 50 68 72 61 73 65 2d  <pLeft->pPhrase-
32ba4 3e 6e 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a  >nToken; kk++){.
32ba5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ba6 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68  pSnippet->aMatch
32ba7 5b 69 69 2d 6b 6b 5d 2e 69 54 65 72 6d 20 3d 20  [ii-kk].iTerm = 
32ba8 2d 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -2;.            
32ba9 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
32baa 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
32bab 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
32bac 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
32bad 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32bae 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
32baf 20 20 69 66 28 20 74 72 69 6d 53 6e 69 70 70 65    if( trimSnippe
32bb0 74 4f 66 66 73 65 74 73 28 70 45 78 70 72 2d 3e  tOffsets(pExpr->
32bb1 70 52 69 67 68 74 2c 20 70 53 6e 69 70 70 65 74  pRight, pSnippet
32bb2 2c 20 70 69 4c 65 66 74 29 20 29 7b 0a 20 20 20  , piLeft) ){.   
32bb3 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
32bb4 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
32bb5 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  0;.}../*.** Comp
32bb6 75 74 65 20 61 6c 6c 20 6f 66 66 73 65 74 73 20  ute all offsets 
32bb7 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
32bb8 72 6f 77 20 6f 66 20 74 68 65 20 71 75 65 72 79  row of the query
32bb9 2e 20 20 0a 2a 2a 20 49 66 20 74 68 65 20 6f 66  .  .** If the of
32bba 66 73 65 74 73 20 68 61 76 65 20 61 6c 72 65 61  fsets have alrea
32bbb 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64  dy been computed
32bbc 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
32bbd 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
32bbe 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65  atic void snippe
32bbf 74 41 6c 6c 4f 66 66 73 65 74 73 28 66 75 6c 6c  tAllOffsets(full
32bc0 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70 29 7b  text_cursor *p){
32bc1 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 0a  .  int nColumn;.
32bc2 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69    int iColumn, i
32bc3 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20  ;.  int iFirst, 
32bc4 69 4c 61 73 74 3b 0a 20 20 69 6e 74 20 69 54 65  iLast;.  int iTe
32bc5 72 6d 20 3d 20 30 3b 0a 20 20 66 75 6c 6c 74 65  rm = 0;.  fullte
32bc6 78 74 5f 76 74 61 62 20 2a 70 46 74 73 20 3d 20  xt_vtab *pFts = 
32bc7 63 75 72 73 6f 72 5f 76 74 61 62 28 70 29 3b 0a  cursor_vtab(p);.
32bc8 0a 20 20 69 66 28 20 70 2d 3e 73 6e 69 70 70 65  .  if( p->snippe
32bc9 74 2e 6e 4d 61 74 63 68 20 7c 7c 20 70 2d 3e 70  t.nMatch || p->p
32bca 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Expr==0 ){.    r
32bcb 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 43 6f  eturn;.  }.  nCo
32bcc 6c 75 6d 6e 20 3d 20 70 46 74 73 2d 3e 6e 43 6f  lumn = pFts->nCo
32bcd 6c 75 6d 6e 3b 0a 20 20 69 43 6f 6c 75 6d 6e 20  lumn;.  iColumn 
32bce 3d 20 28 70 2d 3e 69 43 75 72 73 6f 72 54 79 70  = (p->iCursorTyp
32bcf 65 20 2d 20 51 55 45 52 59 5f 46 55 4c 4c 54 45  e - QUERY_FULLTE
32bd0 58 54 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75  XT);.  if( iColu
32bd1 6d 6e 3c 30 20 7c 7c 20 69 43 6f 6c 75 6d 6e 3e  mn<0 || iColumn>
32bd2 3d 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  =nColumn ){.    
32bd3 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 6d 61 74 63  /* Look for matc
32bd4 68 65 73 20 6f 76 65 72 20 61 6c 6c 20 63 6f 6c  hes over all col
32bd5 75 6d 6e 73 20 6f 66 20 74 68 65 20 66 75 6c 6c  umns of the full
32bd6 2d 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20  -text index */. 
32bd7 20 20 20 69 46 69 72 73 74 20 3d 20 30 3b 0a 20     iFirst = 0;. 
32bd8 20 20 20 69 4c 61 73 74 20 3d 20 6e 43 6f 6c 75     iLast = nColu
32bd9 6d 6e 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  mn-1;.  }else{. 
32bda 20 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 6d     /* Look for m
32bdb 61 74 63 68 65 73 20 69 6e 20 74 68 65 20 69 43  atches in the iC
32bdc 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20  olumn-th column 
32bdd 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 6c  of the index onl
32bde 79 20 2a 2f 0a 20 20 20 20 69 46 69 72 73 74 20  y */.    iFirst 
32bdf 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69  = iColumn;.    i
32be0 4c 61 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a  Last = iColumn;.
32be1 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 46 69 72    }.  for(i=iFir
32be2 73 74 3b 20 69 3c 3d 69 4c 61 73 74 3b 20 69 2b  st; i<=iLast; i+
32be3 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  +){.    const ch
32be4 61 72 20 2a 7a 44 6f 63 3b 0a 20 20 20 20 69 6e  ar *zDoc;.    in
32be5 74 20 6e 44 6f 63 3b 0a 20 20 20 20 7a 44 6f 63  t nDoc;.    zDoc
32be6 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
32be7 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
32be8 65 78 74 28 70 2d 3e 70 53 74 6d 74 2c 20 69 2b  ext(p->pStmt, i+
32be9 31 29 3b 0a 20 20 20 20 6e 44 6f 63 20 3d 20 73  1);.    nDoc = s
32bea 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
32beb 74 65 73 28 70 2d 3e 70 53 74 6d 74 2c 20 69 2b  tes(p->pStmt, i+
32bec 31 29 3b 0a 20 20 20 20 73 6e 69 70 70 65 74 4f  1);.    snippetO
32bed 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28 70  ffsetsOfColumn(p
32bee 2c 20 26 70 2d 3e 73 6e 69 70 70 65 74 2c 20 69  , &p->snippet, i
32bef 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 29 3b 0a 20  , zDoc, nDoc);. 
32bf0 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 74 72 69   }..  while( tri
32bf1 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73 28  mSnippetOffsets(
32bf2 70 2d 3e 70 45 78 70 72 2c 20 26 70 2d 3e 73 6e  p->pExpr, &p->sn
32bf3 69 70 70 65 74 2c 20 26 69 54 65 72 6d 29 20 29  ippet, &iTerm) )
32bf4 7b 0a 20 20 20 20 69 54 65 72 6d 20 3d 20 30 3b  {.    iTerm = 0;
32bf5 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .  }.}../*.** Co
32bf6 6e 76 65 72 74 20 74 68 65 20 69 6e 66 6f 72 6d  nvert the inform
32bf7 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 61 4d 61  ation in the aMa
32bf8 74 63 68 5b 5d 20 61 72 72 61 79 20 6f 66 20 74  tch[] array of t
32bf9 68 65 20 73 6e 69 70 70 65 74 0a 2a 2a 20 69 6e  he snippet.** in
32bfa 74 6f 20 74 68 65 20 73 74 72 69 6e 67 20 7a 4f  to the string zO
32bfb 66 66 73 65 74 5b 30 2e 2e 6e 4f 66 66 73 65 74  ffset[0..nOffset
32bfc 2d 31 5d 2e 20 54 68 69 73 20 73 74 72 69 6e 67  -1]. This string
32bfd 20 69 73 20 75 73 65 64 20 61 73 0a 2a 2a 20 74   is used as.** t
32bfe 68 65 20 72 65 74 75 72 6e 20 6f 66 20 74 68 65  he return of the
32bff 20 53 51 4c 20 6f 66 66 73 65 74 73 28 29 20 66   SQL offsets() f
32c00 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
32c01 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74 4f  ic void snippetO
32c02 66 66 73 65 74 54 65 78 74 28 53 6e 69 70 70 65  ffsetText(Snippe
32c03 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  t *p){.  int i;.
32c04 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
32c05 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62   StringBuffer sb
32c06 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30  ;.  char zBuf[20
32c07 30 5d 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 4f 66  0];.  if( p->zOf
32c08 66 73 65 74 20 29 20 72 65 74 75 72 6e 3b 0a 20  fset ) return;. 
32c09 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65   initStringBuffe
32c0a 72 28 26 73 62 29 3b 0a 20 20 66 6f 72 28 69 3d  r(&sb);.  for(i=
32c0b 30 3b 20 69 3c 70 2d 3e 6e 4d 61 74 63 68 3b 20  0; i<p->nMatch; 
32c0c 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74  i++){.    struct
32c0d 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70   snippetMatch *p
32c0e 4d 61 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61 74  Match = &p->aMat
32c0f 63 68 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  ch[i];.    if( p
32c10 4d 61 74 63 68 2d 3e 69 54 65 72 6d 3e 3d 30 20  Match->iTerm>=0 
32c11 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 73  ){.      /* If s
32c12 6e 69 70 70 65 74 4d 61 74 63 68 2e 69 54 65 72  nippetMatch.iTer
32c13 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 30  m is less than 0
32c14 2c 20 74 68 65 6e 20 74 68 65 20 6d 61 74 63 68  , then the match
32c15 20 77 61 73 20 0a 20 20 20 20 20 20 2a 2a 20 64   was .      ** d
32c16 69 73 63 61 72 64 65 64 20 61 73 20 70 61 72 74  iscarded as part
32c17 20 6f 66 20 70 72 6f 63 65 73 73 69 6e 67 20 74   of processing t
32c18 68 65 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72  he NEAR operator
32c19 20 28 73 65 65 20 74 68 65 20 0a 20 20 20 20 20   (see the .     
32c1a 20 2a 2a 20 74 72 69 6d 53 6e 69 70 70 65 74 4f   ** trimSnippetO
32c1b 66 66 73 65 74 73 46 6f 72 4e 65 61 72 28 29 20  ffsetsForNear() 
32c1c 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 64 65 74  function for det
32c1d 61 69 6c 73 29 2e 20 49 67 6e 6f 72 65 20 0a 20  ails). Ignore . 
32c1e 20 20 20 20 20 2a 2a 20 69 74 20 69 6e 20 74 68       ** it in th
32c1f 69 73 20 63 61 73 65 0a 20 20 20 20 20 20 2a 2f  is case.      */
32c20 0a 20 20 20 20 20 20 7a 42 75 66 5b 30 5d 20 3d  .      zBuf[0] =
32c21 20 27 20 27 3b 0a 20 20 20 20 20 20 73 71 6c 69   ' ';.      sqli
32c22 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
32c23 65 6f 66 28 7a 42 75 66 29 2d 31 2c 20 26 7a 42  eof(zBuf)-1, &zB
32c24 75 66 5b 63 6e 74 3e 30 5d 2c 20 22 25 64 20 25  uf[cnt>0], "%d %
32c25 64 20 25 64 20 25 64 22 2c 0a 20 20 20 20 20 20  d %d %d",.      
32c26 20 20 20 20 70 4d 61 74 63 68 2d 3e 69 43 6f 6c      pMatch->iCol
32c27 2c 20 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 2c  , pMatch->iTerm,
32c28 20 70 4d 61 74 63 68 2d 3e 69 53 74 61 72 74 2c   pMatch->iStart,
32c29 20 70 4d 61 74 63 68 2d 3e 6e 42 79 74 65 29 3b   pMatch->nByte);
32c2a 0a 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73  .      append(&s
32c2b 62 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 20 20  b, zBuf);.      
32c2c 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  cnt++;.    }.  }
32c2d 0a 20 20 70 2d 3e 7a 4f 66 66 73 65 74 20 3d 20  .  p->zOffset = 
32c2e 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61  stringBufferData
32c2f 28 26 73 62 29 3b 0a 20 20 70 2d 3e 6e 4f 66 66  (&sb);.  p->nOff
32c30 73 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66  set = stringBuff
32c31 65 72 4c 65 6e 67 74 68 28 26 73 62 29 3b 0a 7d  erLength(&sb);.}
32c32 0a 0a 2f 2a 0a 2a 2a 20 7a 44 6f 63 5b 30 2e 2e  ../*.** zDoc[0..
32c33 6e 44 6f 63 2d 31 5d 20 69 73 20 70 68 72 61 73  nDoc-1] is phras
32c34 65 20 6f 66 20 74 65 78 74 2e 20 20 61 4d 61 74  e of text.  aMat
32c35 63 68 5b 30 2e 2e 6e 4d 61 74 63 68 2d 31 5d 20  ch[0..nMatch-1] 
32c36 61 72 65 20 61 20 73 65 74 0a 2a 2a 20 6f 66 20  are a set.** of 
32c37 6d 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 73  matching words s
32c38 6f 6d 65 20 6f 66 20 77 68 69 63 68 20 6d 69 67  ome of which mig
32c39 68 74 20 62 65 20 69 6e 20 7a 44 6f 63 2e 20 20  ht be in zDoc.  
32c3a 7a 44 6f 63 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a  zDoc is column.*
32c3b 2a 20 6e 75 6d 62 65 72 20 69 43 6f 6c 2e 0a 2a  * number iCol..*
32c3c 2a 0a 2a 2a 20 69 42 72 65 61 6b 20 69 73 20 73  *.** iBreak is s
32c3d 75 67 67 65 73 74 65 64 20 73 70 6f 74 20 69 6e  uggested spot in
32c3e 20 7a 44 6f 63 20 77 68 65 72 65 20 77 65 20 63   zDoc where we c
32c3f 6f 75 6c 64 20 62 65 67 69 6e 20 6f 72 20 65 6e  ould begin or en
32c40 64 20 61 6e 0a 2a 2a 20 65 78 63 65 72 70 74 2e  d an.** excerpt.
32c41 20 20 52 65 74 75 72 6e 20 61 20 76 61 6c 75 65    Return a value
32c42 20 73 69 6d 69 6c 61 72 20 74 6f 20 69 42 72 65   similar to iBre
32c43 61 6b 20 62 75 74 20 70 6f 73 73 69 62 6c 79 20  ak but possibly 
32c44 61 64 6a 75 73 74 65 64 0a 2a 2a 20 74 6f 20 62  adjusted.** to b
32c45 65 20 61 20 6c 69 74 74 6c 65 20 6c 65 66 74 20  e a little left 
32c46 6f 72 20 72 69 67 68 74 20 73 6f 20 74 68 61 74  or right so that
32c47 20 74 68 65 20 62 72 65 61 6b 20 70 6f 69 6e 74   the break point
32c48 20 69 73 20 62 65 74 74 65 72 2e 0a 2a 2f 0a 73   is better..*/.s
32c49 74 61 74 69 63 20 69 6e 74 20 77 6f 72 64 42 6f  tatic int wordBo
32c4a 75 6e 64 61 72 79 28 0a 20 20 69 6e 74 20 69 42  undary(.  int iB
32c4b 72 65 61 6b 2c 20 20 20 20 20 20 20 20 20 20 20  reak,           
32c4c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
32c4d 75 67 67 65 73 74 65 64 20 62 72 65 61 6b 20 70  uggested break p
32c4e 6f 69 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  oint */.  const 
32c4f 63 68 61 72 20 2a 7a 44 6f 63 2c 20 20 20 20 20  char *zDoc,     
32c50 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 75 6d          /* Docum
32c51 65 6e 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  ent text */.  in
32c52 74 20 6e 44 6f 63 2c 20 20 20 20 20 20 20 20 20  t nDoc,         
32c53 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
32c54 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
32c55 6e 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 20 20 73 74  n zDoc[] */.  st
32c56 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
32c57 68 20 2a 61 4d 61 74 63 68 2c 20 20 2f 2a 20 4d  h *aMatch,  /* M
32c58 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 2a 2f  atching words */
32c59 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 2c 20 20  .  int nMatch,  
32c5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c5b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
32c5c 74 72 69 65 73 20 69 6e 20 61 4d 61 74 63 68 5b  tries in aMatch[
32c5d 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 20  ] */.  int iCol 
32c5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c5f 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75       /* The colu
32c60 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 7a 44  mn number for zD
32c61 6f 63 5b 5d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oc[] */.){.  int
32c62 20 69 3b 0a 20 20 69 66 28 20 69 42 72 65 61 6b   i;.  if( iBreak
32c63 3c 3d 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75  <=10 ){.    retu
32c64 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
32c65 69 42 72 65 61 6b 3e 3d 6e 44 6f 63 2d 31 30 20  iBreak>=nDoc-10 
32c66 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 44  ){.    return nD
32c67 6f 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  oc;.  }.  for(i=
32c68 30 3b 20 69 3c 6e 4d 61 74 63 68 20 26 26 20 61  0; i<nMatch && a
32c69 4d 61 74 63 68 5b 69 5d 2e 69 43 6f 6c 3c 69 43  Match[i].iCol<iC
32c6a 6f 6c 3b 20 69 2b 2b 29 7b 7d 0a 20 20 77 68 69  ol; i++){}.  whi
32c6b 6c 65 28 20 69 3c 6e 4d 61 74 63 68 20 26 26 20  le( i<nMatch && 
32c6c 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74  aMatch[i].iStart
32c6d 2b 61 4d 61 74 63 68 5b 69 5d 2e 6e 42 79 74 65  +aMatch[i].nByte
32c6e 3c 69 42 72 65 61 6b 20 29 7b 20 69 2b 2b 3b 20  <iBreak ){ i++; 
32c6f 7d 0a 20 20 69 66 28 20 69 3c 6e 4d 61 74 63 68  }.  if( i<nMatch
32c70 20 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74   ){.    if( aMat
32c71 63 68 5b 69 5d 2e 69 53 74 61 72 74 3c 69 42 72  ch[i].iStart<iBr
32c72 65 61 6b 2b 31 30 20 29 7b 0a 20 20 20 20 20 20  eak+10 ){.      
32c73 72 65 74 75 72 6e 20 61 4d 61 74 63 68 5b 69 5d  return aMatch[i]
32c74 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a 20  .iStart;.    }. 
32c75 20 20 20 69 66 28 20 69 3e 30 20 26 26 20 61 4d     if( i>0 && aM
32c76 61 74 63 68 5b 69 2d 31 5d 2e 69 53 74 61 72 74  atch[i-1].iStart
32c77 2b 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 6e 42 79  +aMatch[i-1].nBy
32c78 74 65 3e 3d 69 42 72 65 61 6b 20 29 7b 0a 20 20  te>=iBreak ){.  
32c79 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61 74 63      return aMatc
32c7a 68 5b 69 2d 31 5d 2e 69 53 74 61 72 74 3b 0a 20  h[i-1].iStart;. 
32c7b 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
32c7c 3d 31 3b 20 69 3c 3d 31 30 3b 20 69 2b 2b 29 7b  =1; i<=10; i++){
32c7d 0a 20 20 20 20 69 66 28 20 73 61 66 65 5f 69 73  .    if( safe_is
32c7e 73 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61  space(zDoc[iBrea
32c7f 6b 2d 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72  k-i]) ){.      r
32c80 65 74 75 72 6e 20 69 42 72 65 61 6b 20 2d 20 69  eturn iBreak - i
32c81 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   + 1;.    }.    
32c82 69 66 28 20 73 61 66 65 5f 69 73 73 70 61 63 65  if( safe_isspace
32c83 28 7a 44 6f 63 5b 69 42 72 65 61 6b 2b 69 5d 29  (zDoc[iBreak+i])
32c84 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
32c85 20 69 42 72 65 61 6b 20 2b 20 69 20 2b 20 31 3b   iBreak + i + 1;
32c86 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
32c87 75 72 6e 20 69 42 72 65 61 6b 3b 0a 7d 0a 0a 0a  urn iBreak;.}...
32c88 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
32c89 61 6c 75 65 73 20 66 6f 72 20 53 6e 69 70 70 65  alues for Snippe
32c8a 74 2e 61 4d 61 74 63 68 5b 5d 2e 73 6e 53 74 61  t.aMatch[].snSta
32c8b 74 75 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  tus.*/.#define S
32c8c 4e 49 50 50 45 54 5f 49 47 4e 4f 52 45 20 20 30  NIPPET_IGNORE  0
32c8d 20 20 20 2f 2a 20 49 74 20 69 73 20 6f 6b 20 74     /* It is ok t
32c8e 6f 20 6f 6d 69 74 20 74 68 69 73 20 6d 61 74 63  o omit this matc
32c8f 68 20 66 72 6f 6d 20 74 68 65 20 73 6e 69 70 70  h from the snipp
32c90 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 4e  et */.#define SN
32c91 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 31 20  IPPET_DESIRED 1 
32c92 20 20 2f 2a 20 57 65 20 77 61 6e 74 20 74 6f 20    /* We want to 
32c93 69 6e 63 6c 75 64 65 20 74 68 69 73 20 6d 61 74  include this mat
32c94 63 68 20 69 6e 20 74 68 65 20 73 6e 69 70 70 65  ch in the snippe
32c95 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  t */../*.** Gene
32c96 72 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66  rate the text of
32c97 20 61 20 73 6e 69 70 70 65 74 2e 0a 2a 2f 0a 73   a snippet..*/.s
32c98 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70  tatic void snipp
32c99 65 74 54 65 78 74 28 0a 20 20 66 75 6c 6c 74 65  etText(.  fullte
32c9a 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  xt_cursor *pCurs
32c9b 6f 72 2c 20 20 20 2f 2a 20 54 68 65 20 63 75 72  or,   /* The cur
32c9c 73 6f 72 20 77 65 20 6e 65 65 64 20 74 68 65 20  sor we need the 
32c9d 73 6e 69 70 70 65 74 20 66 6f 72 20 2a 2f 0a 20  snippet for */. 
32c9e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74   const char *zSt
32c9f 61 72 74 4d 61 72 6b 2c 20 20 20 20 20 2f 2a 20  artMark,     /* 
32ca0 4d 61 72 6b 75 70 20 74 6f 20 61 70 70 65 61 72  Markup to appear
32ca1 20 62 65 66 6f 72 65 20 65 61 63 68 20 6d 61 74   before each mat
32ca2 63 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ch */.  const ch
32ca3 61 72 20 2a 7a 45 6e 64 4d 61 72 6b 2c 20 20 20  ar *zEndMark,   
32ca4 20 20 20 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f      /* Markup to
32ca5 20 61 70 70 65 61 72 20 61 66 74 65 72 20 65 61   appear after ea
32ca6 63 68 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f  ch match */.  co
32ca7 6e 73 74 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70  nst char *zEllip
32ca8 73 69 73 20 20 20 20 20 20 20 2f 2a 20 45 6c 6c  sis       /* Ell
32ca9 69 70 73 69 73 20 6d 61 72 6b 20 2a 2f 0a 29 7b  ipsis mark */.){
32caa 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73  .  int i, j;.  s
32cab 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74  truct snippetMat
32cac 63 68 20 2a 61 4d 61 74 63 68 3b 0a 20 20 69 6e  ch *aMatch;.  in
32cad 74 20 6e 4d 61 74 63 68 3b 0a 20 20 69 6e 74 20  t nMatch;.  int 
32cae 6e 44 65 73 69 72 65 64 3b 0a 20 20 53 74 72 69  nDesired;.  Stri
32caf 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
32cb0 6e 74 20 74 61 69 6c 43 6f 6c 3b 0a 20 20 69 6e  nt tailCol;.  in
32cb1 74 20 74 61 69 6c 4f 66 66 73 65 74 3b 0a 20 20  t tailOffset;.  
32cb2 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 69 6e 74 20  int iCol;.  int 
32cb3 6e 44 6f 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68  nDoc;.  const ch
32cb4 61 72 20 2a 7a 44 6f 63 3b 0a 20 20 69 6e 74 20  ar *zDoc;.  int 
32cb5 69 53 74 61 72 74 2c 20 69 45 6e 64 3b 0a 20 20  iStart, iEnd;.  
32cb6 69 6e 74 20 74 61 69 6c 45 6c 6c 69 70 73 69 73  int tailEllipsis
32cb7 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4d 61 74   = 0;.  int iMat
32cb8 63 68 3b 0a 20 20 0a 0a 20 20 73 71 6c 69 74 65  ch;.  ..  sqlite
32cb9 33 5f 66 72 65 65 28 70 43 75 72 73 6f 72 2d 3e  3_free(pCursor->
32cba 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70 65 74  snippet.zSnippet
32cbb 29 3b 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e  );.  pCursor->sn
32cbc 69 70 70 65 74 2e 7a 53 6e 69 70 70 65 74 20 3d  ippet.zSnippet =
32cbd 20 30 3b 0a 20 20 61 4d 61 74 63 68 20 3d 20 70   0;.  aMatch = p
32cbe 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
32cbf 61 4d 61 74 63 68 3b 0a 20 20 6e 4d 61 74 63 68  aMatch;.  nMatch
32cc0 20 3d 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70   = pCursor->snip
32cc1 70 65 74 2e 6e 4d 61 74 63 68 3b 0a 20 20 69 6e  pet.nMatch;.  in
32cc2 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26  itStringBuffer(&
32cc3 73 62 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  sb);..  for(i=0;
32cc4 20 69 3c 6e 4d 61 74 63 68 3b 20 69 2b 2b 29 7b   i<nMatch; i++){
32cc5 0a 20 20 20 20 61 4d 61 74 63 68 5b 69 5d 2e 73  .    aMatch[i].s
32cc6 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
32cc7 54 5f 49 47 4e 4f 52 45 3b 0a 20 20 7d 0a 20 20  T_IGNORE;.  }.  
32cc8 6e 44 65 73 69 72 65 64 20 3d 20 30 3b 0a 20 20  nDesired = 0;.  
32cc9 66 6f 72 28 69 3d 30 3b 20 69 3c 46 54 53 33 5f  for(i=0; i<FTS3_
32cca 52 4f 54 4f 52 5f 53 5a 3b 20 69 2b 2b 29 7b 0a  ROTOR_SZ; i++){.
32ccb 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e      for(j=0; j<n
32ccc 4d 61 74 63 68 3b 20 6a 2b 2b 29 7b 0a 20 20 20  Match; j++){.   
32ccd 20 20 20 69 66 28 20 61 4d 61 74 63 68 5b 6a 5d     if( aMatch[j]
32cce 2e 69 54 65 72 6d 3d 3d 69 20 29 7b 0a 20 20 20  .iTerm==i ){.   
32ccf 20 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73       aMatch[j].s
32cd0 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
32cd1 54 5f 44 45 53 49 52 45 44 3b 0a 20 20 20 20 20  T_DESIRED;.     
32cd2 20 20 20 6e 44 65 73 69 72 65 64 2b 2b 3b 0a 20     nDesired++;. 
32cd3 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32cd4 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
32cd5 0a 20 20 69 4d 61 74 63 68 20 3d 20 30 3b 0a 20  .  iMatch = 0;. 
32cd6 20 74 61 69 6c 43 6f 6c 20 3d 20 2d 31 3b 0a 20   tailCol = -1;. 
32cd7 20 74 61 69 6c 4f 66 66 73 65 74 20 3d 20 30 3b   tailOffset = 0;
32cd8 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d  .  for(i=0; i<nM
32cd9 61 74 63 68 20 26 26 20 6e 44 65 73 69 72 65 64  atch && nDesired
32cda 3e 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  >0; i++){.    if
32cdb 28 20 61 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74  ( aMatch[i].snSt
32cdc 61 74 75 73 21 3d 53 4e 49 50 50 45 54 5f 44 45  atus!=SNIPPET_DE
32cdd 53 49 52 45 44 20 29 20 63 6f 6e 74 69 6e 75 65  SIRED ) continue
32cde 3b 0a 20 20 20 20 6e 44 65 73 69 72 65 64 2d 2d  ;.    nDesired--
32cdf 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 61 4d 61  ;.    iCol = aMa
32ce0 74 63 68 5b 69 5d 2e 69 43 6f 6c 3b 0a 20 20 20  tch[i].iCol;.   
32ce1 20 7a 44 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63   zDoc = (const c
32ce2 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c  har*)sqlite3_col
32ce3 75 6d 6e 5f 74 65 78 74 28 70 43 75 72 73 6f 72  umn_text(pCursor
32ce4 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29  ->pStmt, iCol+1)
32ce5 3b 0a 20 20 20 20 6e 44 6f 63 20 3d 20 73 71 6c  ;.    nDoc = sql
32ce6 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
32ce7 73 28 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d 74  s(pCursor->pStmt
32ce8 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 69  , iCol+1);.    i
32ce9 53 74 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69  Start = aMatch[i
32cea 5d 2e 69 53 74 61 72 74 20 2d 20 34 30 3b 0a 20  ].iStart - 40;. 
32ceb 20 20 20 69 53 74 61 72 74 20 3d 20 77 6f 72 64     iStart = word
32cec 42 6f 75 6e 64 61 72 79 28 69 53 74 61 72 74 2c  Boundary(iStart,
32ced 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 61 4d 61   zDoc, nDoc, aMa
32cee 74 63 68 2c 20 6e 4d 61 74 63 68 2c 20 69 43 6f  tch, nMatch, iCo
32cef 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 53 74 61  l);.    if( iSta
32cf0 72 74 3c 3d 31 30 20 29 7b 0a 20 20 20 20 20 20  rt<=10 ){.      
32cf1 69 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 20 20  iStart = 0;.    
32cf2 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d  }.    if( iCol==
32cf3 74 61 69 6c 43 6f 6c 20 26 26 20 69 53 74 61 72  tailCol && iStar
32cf4 74 3c 3d 74 61 69 6c 4f 66 66 73 65 74 2b 32 30  t<=tailOffset+20
32cf5 20 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74   ){.      iStart
32cf6 20 3d 20 74 61 69 6c 4f 66 66 73 65 74 3b 0a 20   = tailOffset;. 
32cf7 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 69 43     }.    if( (iC
32cf8 6f 6c 21 3d 74 61 69 6c 43 6f 6c 20 26 26 20 74  ol!=tailCol && t
32cf9 61 69 6c 43 6f 6c 3e 3d 30 29 20 7c 7c 20 69 53  ailCol>=0) || iS
32cfa 74 61 72 74 21 3d 74 61 69 6c 4f 66 66 73 65 74  tart!=tailOffset
32cfb 20 29 7b 0a 20 20 20 20 20 20 74 72 69 6d 57 68   ){.      trimWh
32cfc 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20  iteSpace(&sb);. 
32cfd 20 20 20 20 20 61 70 70 65 6e 64 57 68 69 74 65       appendWhite
32cfe 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20  Space(&sb);.    
32cff 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45    append(&sb, zE
32d00 6c 6c 69 70 73 69 73 29 3b 0a 20 20 20 20 20 20  llipsis);.      
32d01 61 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65  appendWhiteSpace
32d02 28 26 73 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20  (&sb);.    }.   
32d03 20 69 45 6e 64 20 3d 20 61 4d 61 74 63 68 5b 69   iEnd = aMatch[i
32d04 5d 2e 69 53 74 61 72 74 20 2b 20 61 4d 61 74 63  ].iStart + aMatc
32d05 68 5b 69 5d 2e 6e 42 79 74 65 20 2b 20 34 30 3b  h[i].nByte + 40;
32d06 0a 20 20 20 20 69 45 6e 64 20 3d 20 77 6f 72 64  .    iEnd = word
32d07 42 6f 75 6e 64 61 72 79 28 69 45 6e 64 2c 20 7a  Boundary(iEnd, z
32d08 44 6f 63 2c 20 6e 44 6f 63 2c 20 61 4d 61 74 63  Doc, nDoc, aMatc
32d09 68 2c 20 6e 4d 61 74 63 68 2c 20 69 43 6f 6c 29  h, nMatch, iCol)
32d0a 3b 0a 20 20 20 20 69 66 28 20 69 45 6e 64 3e 3d  ;.    if( iEnd>=
32d0b 6e 44 6f 63 2d 31 30 20 29 7b 0a 20 20 20 20 20  nDoc-10 ){.     
32d0c 20 69 45 6e 64 20 3d 20 6e 44 6f 63 3b 0a 20 20   iEnd = nDoc;.  
32d0d 20 20 20 20 74 61 69 6c 45 6c 6c 69 70 73 69 73      tailEllipsis
32d0e 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
32d0f 0a 20 20 20 20 20 20 74 61 69 6c 45 6c 6c 69 70  .      tailEllip
32d10 73 69 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  sis = 1;.    }. 
32d11 20 20 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68     while( iMatch
32d12 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63  <nMatch && aMatc
32d13 68 5b 69 4d 61 74 63 68 5d 2e 69 43 6f 6c 3c 69  h[iMatch].iCol<i
32d14 43 6f 6c 20 29 7b 20 69 4d 61 74 63 68 2b 2b 3b  Col ){ iMatch++;
32d15 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53   }.    while( iS
32d16 74 61 72 74 3c 69 45 6e 64 20 29 7b 0a 20 20 20  tart<iEnd ){.   
32d17 20 20 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68     while( iMatch
32d18 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63  <nMatch && aMatc
32d19 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74  h[iMatch].iStart
32d1a 3c 69 53 74 61 72 74 0a 20 20 20 20 20 20 20 20  <iStart.        
32d1b 20 20 20 20 20 26 26 20 61 4d 61 74 63 68 5b 69       && aMatch[i
32d1c 4d 61 74 63 68 5d 2e 69 43 6f 6c 3c 3d 69 43 6f  Match].iCol<=iCo
32d1d 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4d 61  l ){.        iMa
32d1e 74 63 68 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20  tch++;.      }. 
32d1f 20 20 20 20 20 69 66 28 20 69 4d 61 74 63 68 3c       if( iMatch<
32d20 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68  nMatch && aMatch
32d21 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3c  [iMatch].iStart<
32d22 69 45 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20  iEnd.           
32d23 20 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74    && aMatch[iMat
32d24 63 68 5d 2e 69 43 6f 6c 3d 3d 69 43 6f 6c 20 29  ch].iCol==iCol )
32d25 7b 0a 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e  {.        nappen
32d26 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74  d(&sb, &zDoc[iSt
32d27 61 72 74 5d 2c 20 61 4d 61 74 63 68 5b 69 4d 61  art], aMatch[iMa
32d28 74 63 68 5d 2e 69 53 74 61 72 74 20 2d 20 69 53  tch].iStart - iS
32d29 74 61 72 74 29 3b 0a 20 20 20 20 20 20 20 20 69  tart);.        i
32d2a 53 74 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69  Start = aMatch[i
32d2b 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3b 0a 20  Match].iStart;. 
32d2c 20 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73         append(&s
32d2d 62 2c 20 7a 53 74 61 72 74 4d 61 72 6b 29 3b 0a  b, zStartMark);.
32d2e 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e 64 28          nappend(
32d2f 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72  &sb, &zDoc[iStar
32d30 74 5d 2c 20 61 4d 61 74 63 68 5b 69 4d 61 74 63  t], aMatch[iMatc
32d31 68 5d 2e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  h].nByte);.     
32d32 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a     append(&sb, z
32d33 45 6e 64 4d 61 72 6b 29 3b 0a 20 20 20 20 20 20  EndMark);.      
32d34 20 20 69 53 74 61 72 74 20 2b 3d 20 61 4d 61 74    iStart += aMat
32d35 63 68 5b 69 4d 61 74 63 68 5d 2e 6e 42 79 74 65  ch[iMatch].nByte
32d36 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
32d37 69 4d 61 74 63 68 2b 31 3b 20 6a 3c 6e 4d 61 74  iMatch+1; j<nMat
32d38 63 68 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ch; j++){.      
32d39 20 20 20 20 69 66 28 20 61 4d 61 74 63 68 5b 6a      if( aMatch[j
32d3a 5d 2e 69 54 65 72 6d 3d 3d 61 4d 61 74 63 68 5b  ].iTerm==aMatch[
32d3b 69 4d 61 74 63 68 5d 2e 69 54 65 72 6d 0a 20 20  iMatch].iTerm.  
32d3c 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 61              && a
32d3d 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75  Match[j].snStatu
32d3e 73 3d 3d 53 4e 49 50 50 45 54 5f 44 45 53 49 52  s==SNIPPET_DESIR
32d3f 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ED ){.          
32d40 20 20 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20    nDesired--;.  
32d41 20 20 20 20 20 20 20 20 20 20 61 4d 61 74 63 68            aMatch
32d42 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53  [j].snStatus = S
32d43 4e 49 50 50 45 54 5f 49 47 4e 4f 52 45 3b 0a 20  NIPPET_IGNORE;. 
32d44 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
32d45 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
32d46 7b 0a 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e  {.        nappen
32d47 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74  d(&sb, &zDoc[iSt
32d48 61 72 74 5d 2c 20 69 45 6e 64 20 2d 20 69 53 74  art], iEnd - iSt
32d49 61 72 74 29 3b 0a 20 20 20 20 20 20 20 20 69 53  art);.        iS
32d4a 74 61 72 74 20 3d 20 69 45 6e 64 3b 0a 20 20 20  tart = iEnd;.   
32d4b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74     }.    }.    t
32d4c 61 69 6c 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20  ailCol = iCol;. 
32d4d 20 20 20 74 61 69 6c 4f 66 66 73 65 74 20 3d 20     tailOffset = 
32d4e 69 45 6e 64 3b 0a 20 20 7d 0a 20 20 74 72 69 6d  iEnd;.  }.  trim
32d4f 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b  WhiteSpace(&sb);
32d50 0a 20 20 69 66 28 20 74 61 69 6c 45 6c 6c 69 70  .  if( tailEllip
32d51 73 69 73 20 29 7b 0a 20 20 20 20 61 70 70 65 6e  sis ){.    appen
32d52 64 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29  dWhiteSpace(&sb)
32d53 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62  ;.    append(&sb
32d54 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20  , zEllipsis);.  
32d55 7d 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69  }.  pCursor->sni
32d56 70 70 65 74 2e 7a 53 6e 69 70 70 65 74 20 3d 20  ppet.zSnippet = 
32d57 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61  stringBufferData
32d58 28 26 73 62 29 3b 0a 20 20 70 43 75 72 73 6f 72  (&sb);.  pCursor
32d59 2d 3e 73 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70  ->snippet.nSnipp
32d5a 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65  et = stringBuffe
32d5b 72 4c 65 6e 67 74 68 28 26 73 62 29 3b 0a 7d 0a  rLength(&sb);.}.
32d5c 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68  ../*.** Close th
32d5d 65 20 63 75 72 73 6f 72 2e 20 20 46 6f 72 20 61  e cursor.  For a
32d5e 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
32d5f 61 74 69 6f 6e 20 73 65 65 20 74 68 65 20 64 6f  ation see the do
32d60 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f  cumentation.** o
32d61 6e 20 74 68 65 20 78 43 6c 6f 73 65 20 6d 65 74  n the xClose met
32d62 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74 75  hod of the virtu
32d63 61 6c 20 74 61 62 6c 65 20 69 6e 74 65 72 66 61  al table interfa
32d64 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
32d65 74 20 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 28  t fulltextClose(
32d66 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
32d67 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
32d68 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
32d69 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *c = (fulltext_
32d6a 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
32d6b 72 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  r;.  FTSTRACE(("
32d6c 46 54 53 33 20 43 6c 6f 73 65 20 25 70 5c 6e 22  FTS3 Close %p\n"
32d6d 2c 20 63 29 29 3b 0a 20 20 73 71 6c 69 74 65 33  , c));.  sqlite3
32d6e 5f 66 69 6e 61 6c 69 7a 65 28 63 2d 3e 70 53 74  _finalize(c->pSt
32d6f 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74  mt);.  sqlite3Ft
32d70 73 33 45 78 70 72 46 72 65 65 28 63 2d 3e 70 45  s3ExprFree(c->pE
32d71 78 70 72 29 3b 0a 20 20 73 6e 69 70 70 65 74 43  xpr);.  snippetC
32d72 6c 65 61 72 28 26 63 2d 3e 73 6e 69 70 70 65 74  lear(&c->snippet
32d73 29 3b 0a 20 20 69 66 28 20 63 2d 3e 72 65 73 75  );.  if( c->resu
32d74 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20  lt.nData!=0 ){. 
32d75 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 63     dlrDestroy(&c
32d76 2d 3e 72 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  ->reader);.  }. 
32d77 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
32d78 6f 79 28 26 63 2d 3e 72 65 73 75 6c 74 29 3b 0a  oy(&c->result);.
32d79 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
32d7a 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
32d7b 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63  TE_OK;.}..static
32d7c 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 4e 65 78   int fulltextNex
32d7d 74 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  t(sqlite3_vtab_c
32d7e 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
32d7f 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
32d80 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78  or *c = (fulltex
32d81 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  t_cursor *) pCur
32d82 73 6f 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  sor;.  int rc;..
32d83 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
32d84 33 20 4e 65 78 74 20 25 70 5c 6e 22 2c 20 70 43  3 Next %p\n", pC
32d85 75 72 73 6f 72 29 29 3b 0a 20 20 73 6e 69 70 70  ursor));.  snipp
32d86 65 74 43 6c 65 61 72 28 26 63 2d 3e 73 6e 69 70  etClear(&c->snip
32d87 70 65 74 29 3b 0a 20 20 69 66 28 20 63 2d 3e 69  pet);.  if( c->i
32d88 43 75 72 73 6f 72 54 79 70 65 20 3c 20 51 55 45  CursorType < QUE
32d89 52 59 5f 46 55 4c 4c 54 45 58 54 20 29 7b 0a 20  RY_FULLTEXT ){. 
32d8a 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
32d8b 29 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f  ) Handle SQLITE_
32d8c 53 43 48 45 4d 41 20 41 4e 44 20 53 51 4c 49 54  SCHEMA AND SQLIT
32d8d 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 20 20 72  E_BUSY. */.    r
32d8e 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
32d8f 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20  (c->pStmt);.    
32d90 73 77 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20  switch( rc ){.  
32d91 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
32d92 52 4f 57 3a 0a 20 20 20 20 20 20 20 20 63 2d 3e  ROW:.        c->
32d93 65 6f 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  eof = 0;.       
32d94 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
32d95 4b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51  K;.      case SQ
32d96 4c 49 54 45 5f 44 4f 4e 45 3a 0a 20 20 20 20 20  LITE_DONE:.     
32d97 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20     c->eof = 1;. 
32d98 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
32d99 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 64  LITE_OK;.      d
32d9a 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20  efault:.        
32d9b 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20  c->eof = 1;.    
32d9c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
32d9d 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 20     }.  } else { 
32d9e 20 2f 2a 20 66 75 6c 6c 2d 74 65 78 74 20 71 75   /* full-text qu
32d9f 65 72 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ery */.    rc = 
32da0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 63 2d  sqlite3_reset(c-
32da1 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28  >pStmt);.    if(
32da2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32da3 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
32da4 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e   if( c->result.n
32da5 44 61 74 61 3d 3d 30 20 7c 7c 20 64 6c 72 41 74  Data==0 || dlrAt
32da6 45 6e 64 28 26 63 2d 3e 72 65 61 64 65 72 29 20  End(&c->reader) 
32da7 29 7b 0a 20 20 20 20 20 20 63 2d 3e 65 6f 66 20  ){.      c->eof 
32da8 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 1;.      retur
32da9 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
32daa 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
32dab 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 63  te3_bind_int64(c
32dac 2d 3e 70 53 74 6d 74 2c 20 31 2c 20 64 6c 72 44  ->pStmt, 1, dlrD
32dad 6f 63 69 64 28 26 63 2d 3e 72 65 61 64 65 72 29  ocid(&c->reader)
32dae 29 3b 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26  );.    dlrStep(&
32daf 63 2d 3e 72 65 61 64 65 72 29 3b 0a 20 20 20 20  c->reader);.    
32db0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
32db1 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
32db2 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
32db3 29 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f  ) Handle SQLITE_
32db4 53 43 48 45 4d 41 20 41 4e 44 20 53 51 4c 49 54  SCHEMA AND SQLIT
32db5 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 20 20 72  E_BUSY. */.    r
32db6 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
32db7 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20  (c->pStmt);.    
32db8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
32db9 4f 57 20 29 7b 20 20 20 2f 2a 20 74 68 65 20 63  OW ){   /* the c
32dba 61 73 65 20 77 65 20 65 78 70 65 63 74 20 2a 2f  ase we expect */
32dbb 0a 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20  .      c->eof = 
32dbc 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
32dbd 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
32dbe 0a 20 20 20 20 2f 2a 20 61 6e 20 65 72 72 6f 72  .    /* an error
32dbf 20 6f 63 63 75 72 72 65 64 3b 20 61 62 6f 72 74   occurred; abort
32dc0 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 72   */.    return r
32dc1 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f  c==SQLITE_DONE ?
32dc2 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20   SQLITE_ERROR : 
32dc3 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 54  rc;.  }.}.../* T
32dc4 4f 44 4f 28 73 68 65 73 73 29 20 49 66 20 77 65  ODO(shess) If we
32dc5 20 70 75 73 68 65 64 20 4c 65 61 66 52 65 61 64   pushed LeafRead
32dc6 65 72 20 74 6f 20 74 68 65 20 74 6f 70 20 6f 66  er to the top of
32dc7 20 74 68 65 20 66 69 6c 65 2c 20 6f 72 20 74 6f   the file, or to
32dc8 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 66 69 6c 65  .** another file
32dc9 2c 20 74 65 72 6d 5f 73 65 6c 65 63 74 28 29 20  , term_select() 
32dca 63 6f 75 6c 64 20 62 65 20 70 75 73 68 65 64 20  could be pushed 
32dcb 61 62 6f 76 65 0a 2a 2a 20 64 6f 63 4c 69 73 74  above.** docList
32dcc 4f 66 54 65 72 6d 28 29 2e 0a 2a 2f 0a 73 74 61  OfTerm()..*/.sta
32dcd 74 69 63 20 69 6e 74 20 74 65 72 6d 53 65 6c 65  tic int termSele
32dce 63 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ct(fulltext_vtab
32dcf 20 2a 76 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e   *v, int iColumn
32dd0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32dd1 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
32dd2 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
32dd3 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66  Term, int isPref
32dd4 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
32dd5 20 20 20 20 20 20 20 20 20 20 44 6f 63 4c 69 73            DocLis
32dd6 74 54 79 70 65 20 69 54 79 70 65 2c 20 44 61 74  tType iType, Dat
32dd7 61 42 75 66 66 65 72 20 2a 6f 75 74 29 3b 0a 0a  aBuffer *out);..
32dd8 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  /* .** Return a 
32dd9 44 6f 63 4c 69 73 74 20 63 6f 72 72 65 73 70 6f  DocList correspo
32dda 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 68 72  nding to the phr
32ddb 61 73 65 20 2a 70 50 68 72 61 73 65 2e 0a 2a 2a  ase *pPhrase..**
32ddc 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 69 6e  .** The resultin
32ddd 67 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c  g DL_DOCIDS docl
32dde 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ist is stored in
32ddf 20 70 52 65 73 75 6c 74 2c 20 77 68 69 63 68 20   pResult, which 
32de0 69 73 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65  is.** overwritte
32de1 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
32de2 20 64 6f 63 4c 69 73 74 4f 66 50 68 72 61 73 65   docListOfPhrase
32de3 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  (.  fulltext_vta
32de4 62 20 2a 70 54 61 62 2c 20 20 20 2f 2a 20 54 68  b *pTab,   /* Th
32de5 65 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65  e full text inde
32de6 78 20 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73  x */.  Fts3Phras
32de7 65 20 2a 70 50 68 72 61 73 65 2c 20 20 20 2f 2a  e *pPhrase,   /*
32de8 20 50 68 72 61 73 65 20 74 6f 20 72 65 74 75 72   Phrase to retur
32de9 6e 20 61 20 64 6f 63 6c 69 73 74 20 63 6f 72 72  n a doclist corr
32dea 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 2a 2f 0a  esponding to */.
32deb 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 65 4c    DocListType eL
32dec 69 73 74 54 79 70 65 2c 20 2f 2a 20 45 69 74 68  istType, /* Eith
32ded 65 72 20 44 4c 5f 44 4f 43 49 44 53 20 6f 72 20  er DL_DOCIDS or 
32dee 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 2a 2f 0a  DL_POSITIONS */.
32def 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 52    DataBuffer *pR
32df0 65 73 75 6c 74 20 20 20 20 2f 2a 20 57 72 69 74  esult    /* Writ
32df1 65 20 74 68 65 20 72 65 73 75 6c 74 20 68 65 72  e the result her
32df2 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 69  e */.){.  int ii
32df3 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
32df4 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 43  ITE_OK;.  int iC
32df5 6f 6c 20 3d 20 70 50 68 72 61 73 65 2d 3e 69 43  ol = pPhrase->iC
32df6 6f 6c 75 6d 6e 3b 0a 20 20 44 6f 63 4c 69 73 74  olumn;.  DocList
32df7 54 79 70 65 20 65 54 79 70 65 20 3d 20 65 4c 69  Type eType = eLi
32df8 73 74 54 79 70 65 3b 0a 20 20 61 73 73 65 72 74  stType;.  assert
32df9 28 20 65 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ( eType==DL_POSI
32dfa 54 49 4f 4e 53 20 7c 7c 20 65 54 79 70 65 3d 3d  TIONS || eType==
32dfb 44 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 20 20 69  DL_DOCIDS );.  i
32dfc 66 28 20 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  f( pPhrase->nTok
32dfd 65 6e 3e 31 20 29 7b 0a 20 20 20 20 65 54 79 70  en>1 ){.    eTyp
32dfe 65 20 3d 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  e = DL_POSITIONS
32dff 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73  ;.  }..  /* This
32e00 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 65 76   code should nev
32e01 65 72 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74  er be called wit
32e02 68 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  h buffered updat
32e03 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  es. */.  assert(
32e04 20 70 54 61 62 2d 3e 6e 50 65 6e 64 69 6e 67 44   pTab->nPendingD
32e05 61 74 61 3c 30 20 29 3b 0a 0a 20 20 66 6f 72 28  ata<0 );..  for(
32e06 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  ii=0; rc==SQLITE
32e07 5f 4f 4b 20 26 26 20 69 69 3c 70 50 68 72 61 73  _OK && ii<pPhras
32e08 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 69 2b 2b 29  e->nToken; ii++)
32e09 7b 0a 20 20 20 20 44 61 74 61 42 75 66 66 65 72  {.    DataBuffer
32e0a 20 74 6d 70 3b 0a 20 20 20 20 73 74 72 75 63 74   tmp;.    struct
32e0b 20 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70 20   PhraseToken *p 
32e0c 3d 20 26 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  = &pPhrase->aTok
32e0d 65 6e 5b 69 69 5d 3b 0a 20 20 20 20 72 63 20 3d  en[ii];.    rc =
32e0e 20 74 65 72 6d 53 65 6c 65 63 74 28 70 54 61 62   termSelect(pTab
32e0f 2c 20 69 43 6f 6c 2c 20 70 2d 3e 7a 2c 20 70 2d  , iCol, p->z, p-
32e10 3e 6e 2c 20 70 2d 3e 69 73 50 72 65 66 69 78 2c  >n, p->isPrefix,
32e11 20 65 54 79 70 65 2c 20 26 74 6d 70 29 3b 0a 20   eType, &tmp);. 
32e12 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
32e13 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66  E_OK ){.      if
32e14 28 20 69 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ( ii==0 ){.     
32e15 20 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 74 6d     *pResult = tm
32e16 70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  p;.      }else{.
32e17 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66          DataBuff
32e18 65 72 20 72 65 73 20 3d 20 2a 70 52 65 73 75 6c  er res = *pResul
32e19 74 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  t;.        dataB
32e1a 75 66 66 65 72 49 6e 69 74 28 70 52 65 73 75 6c  ufferInit(pResul
32e1b 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  t, 0);.        i
32e1c 66 28 20 69 69 3d 3d 28 70 50 68 72 61 73 65 2d  f( ii==(pPhrase-
32e1d 3e 6e 54 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20 20  >nToken-1) ){.  
32e1e 20 20 20 20 20 20 20 20 65 54 79 70 65 20 3d 20          eType = 
32e1f 65 4c 69 73 74 54 79 70 65 3b 0a 20 20 20 20 20  eListType;.     
32e20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 6f 63     }.        doc
32e21 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  ListPhraseMerge(
32e22 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 2e 70  .          res.p
32e23 44 61 74 61 2c 20 72 65 73 2e 6e 44 61 74 61 2c  Data, res.nData,
32e24 20 74 6d 70 2e 70 44 61 74 61 2c 20 74 6d 70 2e   tmp.pData, tmp.
32e25 6e 44 61 74 61 2c 20 30 2c 20 30 2c 20 65 54 79  nData, 0, 0, eTy
32e26 70 65 2c 20 70 52 65 73 75 6c 74 0a 20 20 20 20  pe, pResult.    
32e27 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 64      );.        d
32e28 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
32e29 28 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20  (&res);.        
32e2a 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
32e2b 79 28 26 74 6d 70 29 3b 0a 20 20 20 20 20 20 7d  y(&tmp);.      }
32e2c 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
32e2d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
32e2e 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20 66  * Evaluate the f
32e2f 75 6c 6c 2d 74 65 78 74 20 65 78 70 72 65 73 73  ull-text express
32e30 69 6f 6e 20 70 45 78 70 72 20 61 67 61 69 6e 73  ion pExpr agains
32e31 74 20 66 74 73 33 20 74 61 62 6c 65 20 70 54 61  t fts3 table pTa
32e32 62 2e 20 57 72 69 74 65 0a 2a 2a 20 74 68 65 20  b. Write.** the 
32e33 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 70 52 65  results into pRe
32e34 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
32e35 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 0a 20   evalFts3Expr(. 
32e36 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
32e37 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  pTab,           
32e38 2f 2a 20 46 74 73 33 20 56 69 72 74 75 61 6c 20  /* Fts3 Virtual 
32e39 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 2a 2f 0a  table object */.
32e3a 20 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70    Fts3Expr *pExp
32e3b 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
32e3c 20 2f 2a 20 50 61 72 73 65 64 20 66 74 73 33 20   /* Parsed fts3 
32e3d 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
32e3e 44 61 74 61 42 75 66 66 65 72 20 2a 70 52 65 73  DataBuffer *pRes
32e3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32e40 2a 20 4f 55 54 3a 20 57 72 69 74 65 20 72 65 73  * OUT: Write res
32e41 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78 70 72  ults of the expr
32e42 65 73 73 69 6f 6e 20 68 65 72 65 20 2a 2f 0a 29  ession here */.)
32e43 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
32e44 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 6e  ITE_OK;..  /* In
32e45 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 75 74  itialize the out
32e46 70 75 74 20 62 75 66 66 65 72 2e 20 49 66 20 74  put buffer. If t
32e47 68 69 73 20 69 73 20 61 6e 20 65 6d 70 74 79 20  his is an empty 
32e48 71 75 65 72 79 20 28 70 45 78 70 72 3d 3d 30 29  query (pExpr==0)
32e49 2c 20 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20  , .  ** this is 
32e4a 61 6c 6c 20 74 68 61 74 20 6e 65 65 64 73 20 74  all that needs t
32e4b 6f 20 62 65 20 64 6f 6e 65 2e 20 45 6d 70 74 79  o be done. Empty
32e4c 20 71 75 65 72 69 65 73 20 70 72 6f 64 75 63 65   queries produce
32e4d 20 65 6d 70 74 79 20 0a 20 20 2a 2a 20 72 65 73   empty .  ** res
32e4e 75 6c 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a 20  ult sets..  */. 
32e4f 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
32e50 70 52 65 73 2c 20 30 29 3b 0a 0a 20 20 69 66 28  pRes, 0);..  if(
32e51 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66   pExpr ){.    if
32e52 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d  ( pExpr->eType==
32e53 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
32e54 29 7b 0a 20 20 20 20 20 20 44 6f 63 4c 69 73 74  ){.      DocList
32e55 54 79 70 65 20 65 54 79 70 65 20 3d 20 44 4c 5f  Type eType = DL_
32e56 44 4f 43 49 44 53 3b 0a 20 20 20 20 20 20 69 66  DOCIDS;.      if
32e57 28 20 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74  ( pExpr->pParent
32e58 20 26 26 20 70 45 78 70 72 2d 3e 70 50 61 72 65   && pExpr->pPare
32e59 6e 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  nt->eType==FTSQU
32e5a 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20  ERY_NEAR ){.    
32e5b 20 20 20 20 65 54 79 70 65 20 3d 20 44 4c 5f 50      eType = DL_P
32e5c 4f 53 49 54 49 4f 4e 53 3b 0a 20 20 20 20 20 20  OSITIONS;.      
32e5d 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 64 6f 63  }.      rc = doc
32e5e 4c 69 73 74 4f 66 50 68 72 61 73 65 28 70 54 61  ListOfPhrase(pTa
32e5f 62 2c 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73  b, pExpr->pPhras
32e60 65 2c 20 65 54 79 70 65 2c 20 70 52 65 73 29 3b  e, eType, pRes);
32e61 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
32e62 20 20 44 61 74 61 42 75 66 66 65 72 20 6c 68 73    DataBuffer lhs
32e63 3b 0a 20 20 20 20 20 20 44 61 74 61 42 75 66 66  ;.      DataBuff
32e64 65 72 20 72 68 73 3b 0a 0a 20 20 20 20 20 20 64  er rhs;..      d
32e65 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 72  ataBufferInit(&r
32e66 68 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  hs, 0);.      if
32e67 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
32e68 20 3d 20 65 76 61 6c 46 74 73 33 45 78 70 72 28   = evalFts3Expr(
32e69 70 54 61 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65  pTab, pExpr->pLe
32e6a 66 74 2c 20 26 6c 68 73 29 29 20 0a 20 20 20 20  ft, &lhs)) .    
32e6b 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
32e6c 3d 28 72 63 20 3d 20 65 76 61 6c 46 74 73 33 45  =(rc = evalFts3E
32e6d 78 70 72 28 70 54 61 62 2c 20 70 45 78 70 72 2d  xpr(pTab, pExpr-
32e6e 3e 70 52 69 67 68 74 2c 20 26 72 68 73 29 29 20  >pRight, &rhs)) 
32e6f 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
32e70 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
32e71 3e 65 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20  >eType ){.      
32e72 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
32e73 59 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20  Y_NEAR: {.      
32e74 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e        int nToken
32e75 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 46 74  ;.            Ft
32e76 73 33 45 78 70 72 20 2a 70 4c 65 66 74 3b 0a 20  s3Expr *pLeft;. 
32e77 20 20 20 20 20 20 20 20 20 20 20 44 6f 63 4c 69             DocLi
32e78 73 74 54 79 70 65 20 65 54 79 70 65 20 3d 20 44  stType eType = D
32e79 4c 5f 44 4f 43 49 44 53 3b 0a 20 20 20 20 20 20  L_DOCIDS;.      
32e7a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
32e7b 3e 70 50 61 72 65 6e 74 20 26 26 20 70 45 78 70  >pParent && pExp
32e7c 72 2d 3e 70 50 61 72 65 6e 74 2d 3e 65 54 79 70  r->pParent->eTyp
32e7d 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
32e7e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
32e7f 20 20 65 54 79 70 65 20 3d 20 44 4c 5f 50 4f 53    eType = DL_POS
32e80 49 54 49 4f 4e 53 3b 0a 20 20 20 20 20 20 20 20  ITIONS;.        
32e81 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
32e82 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d    pLeft = pExpr-
32e83 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20  >pLeft;.        
32e84 20 20 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74      while( pLeft
32e85 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
32e86 59 5f 4e 45 41 52 20 29 7b 20 0a 20 20 20 20 20  Y_NEAR ){ .     
32e87 20 20 20 20 20 20 20 20 20 70 4c 65 66 74 3d 70           pLeft=p
32e88 4c 65 66 74 2d 3e 70 52 69 67 68 74 3b 0a 20 20  Left->pRight;.  
32e89 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
32e8a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
32e8b 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 65  pExpr->pRight->e
32e8c 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50  Type==FTSQUERY_P
32e8d 48 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20  HRASE );.       
32e8e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65       assert( pLe
32e8f 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  ft->eType==FTSQU
32e90 45 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20  ERY_PHRASE );.  
32e91 20 20 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e            nToken
32e92 20 3d 20 70 4c 65 66 74 2d 3e 70 50 68 72 61 73   = pLeft->pPhras
32e93 65 2d 3e 6e 54 6f 6b 65 6e 20 2b 20 70 45 78 70  e->nToken + pExp
32e94 72 2d 3e 70 52 69 67 68 74 2d 3e 70 50 68 72 61  r->pRight->pPhra
32e95 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20  se->nToken;.    
32e96 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 50          docListP
32e97 68 72 61 73 65 4d 65 72 67 65 28 6c 68 73 2e 70  hraseMerge(lhs.p
32e98 44 61 74 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c  Data, lhs.nData,
32e99 20 72 68 73 2e 70 44 61 74 61 2c 20 72 68 73 2e   rhs.pData, rhs.
32e9a 6e 44 61 74 61 2c 20 0a 20 20 20 20 20 20 20 20  nData, .        
32e9b 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6e          pExpr->n
32e9c 4e 65 61 72 2b 31 2c 20 6e 54 6f 6b 65 6e 2c 20  Near+1, nToken, 
32e9d 65 54 79 70 65 2c 20 70 52 65 73 0a 20 20 20 20  eType, pRes.    
32e9e 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
32e9f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32ea0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
32ea1 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
32ea2 59 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 20  Y_NOT: {.       
32ea3 20 20 20 20 20 64 6f 63 4c 69 73 74 45 78 63 65       docListExce
32ea4 70 74 4d 65 72 67 65 28 6c 68 73 2e 70 44 61 74  ptMerge(lhs.pDat
32ea5 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72 68  a, lhs.nData, rh
32ea6 73 2e 70 44 61 74 61 2c 20 72 68 73 2e 6e 44 61  s.pData, rhs.nDa
32ea7 74 61 2c 70 52 65 73 29 3b 0a 20 20 20 20 20 20  ta,pRes);.      
32ea8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32ea9 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32eaa 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
32eab 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 20 20  _AND: {.        
32eac 20 20 20 20 64 6f 63 4c 69 73 74 41 6e 64 4d 65      docListAndMe
32ead 72 67 65 28 6c 68 73 2e 70 44 61 74 61 2c 20 6c  rge(lhs.pData, l
32eae 68 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70 44  hs.nData, rhs.pD
32eaf 61 74 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c 20  ata, rhs.nData, 
32eb0 70 52 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20  pRes);.         
32eb1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32eb2 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
32eb3 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52  case FTSQUERY_OR
32eb4 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
32eb5 64 6f 63 4c 69 73 74 4f 72 4d 65 72 67 65 28 6c  docListOrMerge(l
32eb6 68 73 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44  hs.pData, lhs.nD
32eb7 61 74 61 2c 20 72 68 73 2e 70 44 61 74 61 2c 20  ata, rhs.pData, 
32eb8 72 68 73 2e 6e 44 61 74 61 2c 20 70 52 65 73 29  rhs.nData, pRes)
32eb9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
32eba 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
32ebb 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
32ebc 20 7d 0a 20 20 20 20 20 20 64 61 74 61 42 75 66   }.      dataBuf
32ebd 66 65 72 44 65 73 74 72 6f 79 28 26 6c 68 73 29  ferDestroy(&lhs)
32ebe 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  ;.      dataBuff
32ebf 65 72 44 65 73 74 72 6f 79 28 26 72 68 73 29 3b  erDestroy(&rhs);
32ec0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
32ec1 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54  turn rc;.}../* T
32ec2 4f 44 4f 28 73 68 65 73 73 29 20 52 65 66 61 63  ODO(shess) Refac
32ec3 74 6f 72 20 74 68 65 20 63 6f 64 65 20 74 6f 20  tor the code to 
32ec4 72 65 6d 6f 76 65 20 74 68 69 73 20 66 6f 72 77  remove this forw
32ec5 61 72 64 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61  ard decl. */.sta
32ec6 74 69 63 20 69 6e 74 20 66 6c 75 73 68 50 65 6e  tic int flushPen
32ec7 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65  dingTerms(fullte
32ec8 78 74 5f 76 74 61 62 20 2a 76 29 3b 0a 0a 2f 2a  xt_vtab *v);../*
32ec9 20 50 65 72 66 6f 72 6d 20 61 20 66 75 6c 6c 2d   Perform a full-
32eca 74 65 78 74 20 71 75 65 72 79 20 75 73 69 6e 67  text query using
32ecb 20 74 68 65 20 73 65 61 72 63 68 20 65 78 70 72   the search expr
32ecc 65 73 73 69 6f 6e 20 69 6e 0a 2a 2a 20 7a 49 6e  ession in.** zIn
32ecd 70 75 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d  put[0..nInput-1]
32ece 2e 20 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74  .  Return a list
32ecf 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 64 6f 63   of matching doc
32ed0 75 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 70 52 65  uments.** in pRe
32ed1 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 51 75 65 72  sult..**.** Quer
32ed2 69 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 63  ies must match c
32ed3 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 20 20  olumn iColumn.  
32ed4 4f 72 20 69 66 20 69 43 6f 6c 75 6d 6e 3e 3d 6e  Or if iColumn>=n
32ed5 43 6f 6c 75 6d 6e 0a 2a 2a 20 74 68 65 79 20 61  Column.** they a
32ed6 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61  re allowed to ma
32ed7 74 63 68 20 61 67 61 69 6e 73 74 20 61 6e 79 20  tch against any 
32ed8 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69  column..*/.stati
32ed9 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 51 75  c int fulltextQu
32eda 65 72 79 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f  ery(.  fulltext_
32edb 76 74 61 62 20 2a 76 2c 20 20 20 20 20 20 2f 2a  vtab *v,      /*
32edc 20 54 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69   The full text i
32edd 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ndex */.  int iC
32ede 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
32edf 20 2f 2a 20 4d 61 74 63 68 20 61 67 61 69 6e 73   /* Match agains
32ee0 74 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 62 79  t this column by
32ee1 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 63 6f   default */.  co
32ee2 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
32ee3 2c 20 20 20 20 2f 2a 20 54 68 65 20 71 75 65 72  ,    /* The quer
32ee4 79 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 69 6e  y string */.  in
32ee5 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20  t nInput,       
32ee6 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
32ee7 66 20 62 79 74 65 73 20 69 6e 20 7a 49 6e 70 75  f bytes in zInpu
32ee8 74 5b 5d 20 2a 2f 0a 20 20 44 61 74 61 42 75 66  t[] */.  DataBuf
32ee9 66 65 72 20 2a 70 52 65 73 75 6c 74 2c 20 20 20  fer *pResult,   
32eea 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73  /* Write the res
32eeb 75 6c 74 20 64 6f 63 6c 69 73 74 20 68 65 72 65  ult doclist here
32eec 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
32eed 2a 70 70 45 78 70 72 20 20 20 20 20 20 20 20 2f  *ppExpr        /
32eee 2a 20 50 75 74 20 70 61 72 73 65 64 20 71 75 65  * Put parsed que
32eef 72 79 20 73 74 72 69 6e 67 20 68 65 72 65 20 2a  ry string here *
32ef0 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  /.){.  int rc;..
32ef1 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
32ef2 20 49 6e 73 74 65 61 64 20 6f 66 20 66 6c 75 73   Instead of flus
32ef3 68 69 6e 67 20 70 65 6e 64 69 6e 67 54 65 72 6d  hing pendingTerm
32ef4 73 2c 20 77 65 20 63 6f 75 6c 64 20 71 75 65 72  s, we could quer
32ef5 79 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72  y for.  ** the r
32ef6 65 6c 65 76 61 6e 74 20 74 65 72 6d 20 61 6e 64  elevant term and
32ef7 20 6d 65 72 67 65 20 74 68 65 20 64 6f 63 6c 69   merge the docli
32ef8 73 74 20 69 6e 74 6f 20 77 68 61 74 20 77 65 20  st into what we 
32ef9 72 65 63 65 69 76 65 20 66 72 6f 6d 0a 20 20 2a  receive from.  *
32efa 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  * the database. 
32efb 20 57 61 69 74 20 61 6e 64 20 73 65 65 20 69 66   Wait and see if
32efc 20 74 68 69 73 20 69 73 20 61 20 63 6f 6d 6d 6f   this is a commo
32efd 6e 20 69 73 73 75 65 2c 20 66 69 72 73 74 2e 0a  n issue, first..
32efe 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 67 6f 6f 64    **.  ** A good
32eff 20 72 65 61 73 6f 6e 20 6e 6f 74 20 74 6f 20 66   reason not to f
32f00 6c 75 73 68 20 69 73 20 74 6f 20 6e 6f 74 20 67  lush is to not g
32f01 65 6e 65 72 61 74 65 20 75 70 64 61 74 65 2d 72  enerate update-r
32f02 65 6c 61 74 65 64 0a 20 20 2a 2a 20 65 72 72 6f  elated.  ** erro
32f03 72 20 63 6f 64 65 73 20 66 72 6f 6d 20 68 65 72  r codes from her
32f04 65 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 46 6c  e..  */..  /* Fl
32f05 75 73 68 20 61 6e 79 20 62 75 66 66 65 72 65 64  ush any buffered
32f06 20 75 70 64 61 74 65 73 20 62 65 66 6f 72 65 20   updates before 
32f07 65 78 65 63 75 74 69 6e 67 20 74 68 65 20 71 75  executing the qu
32f08 65 72 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66  ery. */.  rc = f
32f09 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73  lushPendingTerms
32f0a 28 76 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  (v);.  if( rc!=S
32f0b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
32f0c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
32f0d 20 20 2f 2a 20 50 61 72 73 65 20 74 68 65 20 71    /* Parse the q
32f0e 75 65 72 79 20 70 61 73 73 65 64 20 74 6f 20 74  uery passed to t
32f0f 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f  he MATCH operato
32f10 72 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  r. */.  rc = sql
32f11 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73  ite3Fts3ExprPars
32f12 65 28 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2c  e(v->pTokenizer,
32f13 20 0a 20 20 20 20 20 20 76 2d 3e 61 7a 43 6f 6c   .      v->azCol
32f14 75 6d 6e 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c  umn, v->nColumn,
32f15 20 69 43 6f 6c 75 6d 6e 2c 20 7a 49 6e 70 75 74   iColumn, zInput
32f16 2c 20 6e 49 6e 70 75 74 2c 20 70 70 45 78 70 72  , nInput, ppExpr
32f17 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  .  );.  if( rc!=
32f18 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
32f19 20 61 73 73 65 72 74 28 20 30 3d 3d 28 2a 70 70   assert( 0==(*pp
32f1a 45 78 70 72 29 20 29 3b 0a 20 20 20 20 72 65 74  Expr) );.    ret
32f1b 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72  urn rc;.  }..  r
32f1c 65 74 75 72 6e 20 65 76 61 6c 46 74 73 33 45 78  eturn evalFts3Ex
32f1d 70 72 28 76 2c 20 2a 70 70 45 78 70 72 2c 20 70  pr(v, *ppExpr, p
32f1e 52 65 73 75 6c 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Result);.}../*.*
32f1f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 78 46  * This is the xF
32f20 69 6c 74 65 72 20 69 6e 74 65 72 66 61 63 65 20  ilter interface 
32f21 66 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20  for the virtual 
32f22 74 61 62 6c 65 2e 20 20 53 65 65 0a 2a 2a 20 74  table.  See.** t
32f23 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
32f24 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20   xFilter method 
32f25 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  documentation fo
32f26 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  r additional.** 
32f27 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a  information..**.
32f28 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d 3d 51 55  ** If idxNum==QU
32f29 45 52 59 5f 47 45 4e 45 52 49 43 20 74 68 65 6e  ERY_GENERIC then
32f2a 20 64 6f 20 61 20 66 75 6c 6c 20 74 61 62 6c 65   do a full table
32f2b 20 73 63 61 6e 20 61 67 61 69 6e 73 74 0a 2a 2a   scan against.**
32f2c 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
32f2d 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  able..**.** If i
32f2e 64 78 4e 75 6d 3d 3d 51 55 45 52 59 5f 44 4f 43  dxNum==QUERY_DOC
32f2f 49 44 20 74 68 65 6e 20 64 6f 20 61 20 64 6f 63  ID then do a doc
32f30 69 64 20 6c 6f 6f 6b 75 70 20 66 6f 72 20 61 20  id lookup for a 
32f31 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20  single entry.** 
32f32 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74  in the %_content
32f33 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
32f34 20 69 64 78 4e 75 6d 3e 3d 51 55 45 52 59 5f 46   idxNum>=QUERY_F
32f35 55 4c 4c 54 45 58 54 20 74 68 65 6e 20 75 73 65  ULLTEXT then use
32f36 20 74 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69   the full text i
32f37 6e 64 65 78 2e 20 20 54 68 65 0a 2a 2a 20 63 6f  ndex.  The.** co
32f38 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 6c 65 66 74  lumn on the left
32f39 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
32f3a 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72  e MATCH operator
32f3b 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75   is column.** nu
32f3c 6d 62 65 72 20 69 64 78 4e 75 6d 2d 51 55 45 52  mber idxNum-QUER
32f3d 59 5f 46 55 4c 4c 54 45 58 54 2c 20 30 20 69 6e  Y_FULLTEXT, 0 in
32f3e 64 65 78 65 64 2e 20 20 61 72 67 76 5b 30 5d 20  dexed.  argv[0] 
32f3f 69 73 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  is the right-han
32f40 64 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68 65  d.** side of the
32f41 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e   MATCH operator.
32f42 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
32f43 73 29 20 55 70 67 72 61 64 65 20 74 68 65 20 63  s) Upgrade the c
32f44 75 72 73 6f 72 20 69 6e 69 74 69 61 6c 69 7a 61  ursor initializa
32f45 74 69 6f 6e 20 61 6e 64 20 64 65 73 74 72 75 63  tion and destruc
32f46 74 69 6f 6e 20 74 6f 0a 2a 2a 20 61 63 63 6f 75  tion to.** accou
32f47 6e 74 20 66 6f 72 20 66 75 6c 6c 74 65 78 74 46  nt for fulltextF
32f48 69 6c 74 65 72 28 29 20 62 65 69 6e 67 20 63 61  ilter() being ca
32f49 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  lled multiple ti
32f4a 6d 65 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 61  mes on the.** sa
32f4b 6d 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20  me cursor.  The 
32f4c 63 75 72 72 65 6e 74 20 73 6f 6c 75 74 69 6f 6e  current solution
32f4d 20 69 73 20 76 65 72 79 20 66 72 61 67 69 6c 65   is very fragile
32f4e 2e 20 20 41 70 70 6c 79 20 66 69 78 20 74 6f 0a  .  Apply fix to.
32f4f 2a 2a 20 66 74 73 33 20 61 73 20 61 70 70 72 6f  ** fts3 as appro
32f50 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69  priate..*/.stati
32f51 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 46 69  c int fulltextFi
32f52 6c 74 65 72 28 0a 20 20 73 71 6c 69 74 65 33 5f  lter(.  sqlite3_
32f53 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
32f54 72 73 6f 72 2c 20 20 20 20 20 2f 2a 20 54 68 65  rsor,     /* The
32f55 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72   cursor used for
32f56 20 74 68 69 73 20 71 75 65 72 79 20 2a 2f 0a 20   this query */. 
32f57 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
32f58 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
32f59 20 20 20 2f 2a 20 57 68 69 63 68 20 69 6e 64 65     /* Which inde
32f5a 78 69 6e 67 20 73 63 68 65 6d 65 20 74 6f 20 75  xing scheme to u
32f5b 73 65 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63  se */.  int argc
32f5c 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
32f5d 2a 2a 61 72 67 76 20 20 20 20 2f 2a 20 41 72 67  **argv    /* Arg
32f5e 75 6d 65 6e 74 73 20 66 6f 72 20 74 68 65 20 69  uments for the i
32f5f 6e 64 65 78 69 6e 67 20 73 63 68 65 6d 65 20 2a  ndexing scheme *
32f60 2f 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  /.){.  fulltext_
32f61 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c  cursor *c = (ful
32f62 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20  ltext_cursor *) 
32f63 70 43 75 72 73 6f 72 3b 0a 20 20 66 75 6c 6c 74  pCursor;.  fullt
32f64 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 63 75  ext_vtab *v = cu
32f65 72 73 6f 72 5f 76 74 61 62 28 63 29 3b 0a 20 20  rsor_vtab(c);.  
32f66 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52  int rc;..  FTSTR
32f67 41 43 45 28 28 22 46 54 53 33 20 46 69 6c 74 65  ACE(("FTS3 Filte
32f68 72 20 25 70 5c 6e 22 2c 70 43 75 72 73 6f 72 29  r %p\n",pCursor)
32f69 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
32f6a 63 75 72 73 6f 72 20 68 61 73 20 61 20 73 74 61  cursor has a sta
32f6b 74 65 6d 65 6e 74 20 74 68 61 74 20 77 61 73 20  tement that was 
32f6c 6e 6f 74 20 70 72 65 70 61 72 65 64 20 61 63 63  not prepared acc
32f6d 6f 72 64 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 69  ording to.  ** i
32f6e 64 78 4e 75 6d 2c 20 63 6c 65 61 72 20 69 74 2e  dxNum, clear it.
32f6f 20 20 49 20 62 65 6c 69 65 76 65 20 61 6c 6c 20    I believe all 
32f70 63 61 6c 6c 73 20 74 6f 20 66 75 6c 6c 74 65 78  calls to fulltex
32f71 74 46 69 6c 74 65 72 20 77 69 74 68 20 61 0a 20  tFilter with a. 
32f72 20 2a 2a 20 67 69 76 65 6e 20 63 75 72 73 6f 72   ** given cursor
32f73 20 77 69 6c 6c 20 68 61 76 65 20 74 68 65 20 73   will have the s
32f74 61 6d 65 20 69 64 78 4e 75 6d 20 2c 20 62 75 74  ame idxNum , but
32f75 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74   in this case it
32f76 27 73 0a 20 20 2a 2a 20 65 61 73 79 20 74 6f 20  's.  ** easy to 
32f77 62 65 20 73 61 66 65 2e 0a 20 20 2a 2f 0a 20 20  be safe..  */.  
32f78 69 66 28 20 63 2d 3e 70 53 74 6d 74 20 26 26 20  if( c->pStmt && 
32f79 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 21 3d  c->iCursorType!=
32f7a 69 64 78 4e 75 6d 20 29 7b 0a 20 20 20 20 73 71  idxNum ){.    sq
32f7b 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 63  lite3_finalize(c
32f7c 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 63 2d  ->pStmt);.    c-
32f7d 3e 70 53 74 6d 74 20 3d 20 4e 55 4c 4c 3b 0a 20  >pStmt = NULL;. 
32f7e 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 66   }..  /* Get a f
32f7f 72 65 73 68 20 73 74 61 74 65 6d 65 6e 74 20 61  resh statement a
32f80 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 69 64  ppropriate to id
32f81 78 4e 75 6d 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f  xNum. */.  /* TO
32f82 44 4f 28 73 68 65 73 73 29 3a 20 41 64 64 20 61  DO(shess): Add a
32f83 20 70 72 65 70 61 72 65 64 2d 73 74 61 74 65 6d   prepared-statem
32f84 65 6e 74 20 63 61 63 68 65 20 69 6e 20 74 68 65  ent cache in the
32f85 20 76 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20   vt structure.. 
32f86 20 2a 2a 20 54 68 65 20 63 61 63 68 65 20 6d 75   ** The cache mu
32f87 73 74 20 68 61 6e 64 6c 65 20 6d 75 6c 74 69 70  st handle multip
32f88 6c 65 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e  le open cursors.
32f89 20 20 45 61 73 69 65 72 20 74 6f 20 63 61 63 68    Easier to cach
32f8a 65 20 74 68 65 0a 20 20 2a 2a 20 73 74 61 74 65  e the.  ** state
32f8b 6d 65 6e 74 20 76 61 72 69 61 6e 74 73 20 61 74  ment variants at
32f8c 20 74 68 65 20 76 74 20 74 6f 20 72 65 64 75 63   the vt to reduc
32f8d 65 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63  e malloc/realloc
32f8e 2f 66 72 65 65 20 68 65 72 65 2e 0a 20 20 2a 2a  /free here..  **
32f8f 20 4f 72 20 77 65 20 63 6f 75 6c 64 20 68 61 76   Or we could hav
32f90 65 20 61 20 53 74 72 69 6e 67 42 75 66 66 65 72  e a StringBuffer
32f91 20 76 61 72 69 61 6e 74 20 77 68 69 63 68 20 61   variant which a
32f92 6c 6c 6f 77 65 64 20 73 74 61 63 6b 0a 20 20 2a  llowed stack.  *
32f93 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 66  * construction f
32f94 6f 72 20 73 6d 61 6c 6c 20 76 61 6c 75 65 73 2e  or small values.
32f95 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63 2d 3e  .  */.  if( !c->
32f96 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 53 74 72  pStmt ){.    Str
32f97 69 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20  ingBuffer sb;.  
32f98 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66    initStringBuff
32f99 65 72 28 26 73 62 29 3b 0a 20 20 20 20 61 70 70  er(&sb);.    app
32f9a 65 6e 64 28 26 73 62 2c 20 22 53 45 4c 45 43 54  end(&sb, "SELECT
32f9b 20 64 6f 63 69 64 2c 20 22 29 3b 0a 20 20 20 20   docid, ");.    
32f9c 61 70 70 65 6e 64 4c 69 73 74 28 26 73 62 2c 20  appendList(&sb, 
32f9d 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61  v->nColumn, v->a
32f9e 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b  zContentColumn);
32f9f 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c  .    append(&sb,
32fa0 20 22 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e   " FROM %_conten
32fa1 74 22 29 3b 0a 20 20 20 20 69 66 28 20 69 64 78  t");.    if( idx
32fa2 4e 75 6d 21 3d 51 55 45 52 59 5f 47 45 4e 45 52  Num!=QUERY_GENER
32fa3 49 43 20 29 20 61 70 70 65 6e 64 28 26 73 62 2c  IC ) append(&sb,
32fa4 20 22 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d   " WHERE docid =
32fa5 20 3f 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 73   ?");.    rc = s
32fa6 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62  ql_prepare(v->db
32fa7 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
32fa8 6d 65 2c 20 26 63 2d 3e 70 53 74 6d 74 2c 0a 20  me, &c->pStmt,. 
32fa9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32faa 20 20 20 20 73 74 72 69 6e 67 42 75 66 66 65 72      stringBuffer
32fab 44 61 74 61 28 26 73 62 29 29 3b 0a 20 20 20 20  Data(&sb));.    
32fac 73 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74  stringBufferDest
32fad 72 6f 79 28 26 73 62 29 3b 0a 20 20 20 20 69 66  roy(&sb);.    if
32fae 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
32faf 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
32fb0 20 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20   c->iCursorType 
32fb1 3d 20 69 64 78 4e 75 6d 3b 0a 20 20 7d 65 6c 73  = idxNum;.  }els
32fb2 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  e{.    sqlite3_r
32fb3 65 73 65 74 28 63 2d 3e 70 53 74 6d 74 29 3b 0a  eset(c->pStmt);.
32fb4 20 20 20 20 61 73 73 65 72 74 28 20 63 2d 3e 69      assert( c->i
32fb5 43 75 72 73 6f 72 54 79 70 65 3d 3d 69 64 78 4e  CursorType==idxN
32fb6 75 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  um );.  }..  swi
32fb7 74 63 68 28 20 69 64 78 4e 75 6d 20 29 7b 0a 20  tch( idxNum ){. 
32fb8 20 20 20 63 61 73 65 20 51 55 45 52 59 5f 47 45     case QUERY_GE
32fb9 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 62 72 65  NERIC:.      bre
32fba 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 51 55  ak;..    case QU
32fbb 45 52 59 5f 44 4f 43 49 44 3a 0a 20 20 20 20 20  ERY_DOCID:.     
32fbc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
32fbd 6e 64 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74 6d  nd_int64(c->pStm
32fbe 74 2c 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61  t, 1, sqlite3_va
32fbf 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
32fc0 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ]));.      if( r
32fc1 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
32fc2 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
32fc3 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61  break;..    defa
32fc4 75 6c 74 3a 20 20 20 2f 2a 20 66 75 6c 6c 2d 74  ult:   /* full-t
32fc5 65 78 74 20 73 65 61 72 63 68 20 2a 2f 0a 20 20  ext search */.  
32fc6 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43    {.      int iC
32fc7 6f 6c 20 3d 20 69 64 78 4e 75 6d 2d 51 55 45 52  ol = idxNum-QUER
32fc8 59 5f 46 55 4c 4c 54 45 58 54 3b 0a 20 20 20 20  Y_FULLTEXT;.    
32fc9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 51    const char *zQ
32fca 75 65 72 79 20 3d 20 28 63 6f 6e 73 74 20 63 68  uery = (const ch
32fcb 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
32fcc 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
32fcd 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
32fce 69 64 78 4e 75 6d 3c 3d 51 55 45 52 59 5f 46 55  idxNum<=QUERY_FU
32fcf 4c 4c 54 45 58 54 2b 76 2d 3e 6e 43 6f 6c 75 6d  LLTEXT+v->nColum
32fd0 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  n);.      assert
32fd1 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 20  ( argc==1 );.   
32fd2 20 20 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74     if( c->result
32fd3 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20  .nData!=0 ){.   
32fd4 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73       /* This cas
32fd5 65 20 68 61 70 70 65 6e 73 20 69 66 20 74 68 65  e happens if the
32fd6 20 73 61 6d 65 20 63 75 72 73 6f 72 20 69 73 20   same cursor is 
32fd7 75 73 65 64 20 72 65 70 65 61 74 65 64 6c 79 2e  used repeatedly.
32fd8 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 6c 72 44   */.        dlrD
32fd9 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 61 64 65  estroy(&c->reade
32fda 72 29 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61  r);.        data
32fdb 42 75 66 66 65 72 52 65 73 65 74 28 26 63 2d 3e  BufferReset(&c->
32fdc 72 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d  result);.      }
32fdd 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 61  else{.        da
32fde 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 63 2d  taBufferInit(&c-
32fdf 3e 72 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20 20  >result, 0);.   
32fe0 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
32fe1 66 75 6c 6c 74 65 78 74 51 75 65 72 79 28 76 2c  fulltextQuery(v,
32fe2 20 69 43 6f 6c 2c 20 7a 51 75 65 72 79 2c 20 2d   iCol, zQuery, -
32fe3 31 2c 20 26 63 2d 3e 72 65 73 75 6c 74 2c 20 26  1, &c->result, &
32fe4 63 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  c->pExpr);.     
32fe5 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
32fe6 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
32fe7 20 20 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73        if( c->res
32fe8 75 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a  ult.nData!=0 ){.
32fe9 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28          dlrInit(
32fea 26 63 2d 3e 72 65 61 64 65 72 2c 20 44 4c 5f 44  &c->reader, DL_D
32feb 4f 43 49 44 53 2c 20 63 2d 3e 72 65 73 75 6c 74  OCIDS, c->result
32fec 2e 70 44 61 74 61 2c 20 63 2d 3e 72 65 73 75 6c  .pData, c->resul
32fed 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  t.nData);.      
32fee 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
32fef 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
32ff0 72 6e 20 66 75 6c 6c 74 65 78 74 4e 65 78 74 28  rn fulltextNext(
32ff1 70 43 75 72 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20  pCursor);.}../* 
32ff2 54 68 69 73 20 69 73 20 74 68 65 20 78 45 6f 66  This is the xEof
32ff3 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76   method of the v
32ff4 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
32ff5 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a  he SQLite core.*
32ff6 2a 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  * calls this rou
32ff7 74 69 6e 65 20 74 6f 20 66 69 6e 64 20 6f 75 74  tine to find out
32ff8 20 69 66 20 69 74 20 68 61 73 20 72 65 61 63 68   if it has reach
32ff9 65 64 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a  ed the end of.**
32ffa 20 61 20 71 75 65 72 79 27 73 20 72 65 73 75 6c   a query's resul
32ffb 74 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  ts set..*/.stati
32ffc 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 45 6f  c int fulltextEo
32ffd 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  f(sqlite3_vtab_c
32ffe 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
32fff 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
33000 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78  or *c = (fulltex
33001 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  t_cursor *) pCur
33002 73 6f 72 3b 0a 20 20 72 65 74 75 72 6e 20 63 2d  sor;.  return c-
33003 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73  >eof;.}../* This
33004 20 69 73 20 74 68 65 20 78 43 6f 6c 75 6d 6e 20   is the xColumn 
33005 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69  method of the vi
33006 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
33007 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65  e SQLite.** core
33008 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68   calls this meth
33009 6f 64 20 64 75 72 69 6e 67 20 61 20 71 75 65 72  od during a quer
3300a 79 20 77 68 65 6e 20 69 74 20 6e 65 65 64 73 20  y when it needs 
3300b 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  the value.** of 
3300c 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68  a column from th
3300d 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
3300e 20 20 54 68 69 73 20 6d 65 74 68 6f 64 20 6e 65    This method ne
3300f 65 64 73 20 74 6f 20 75 73 65 0a 2a 2a 20 6f 6e  eds to use.** on
33010 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  e of the sqlite3
33011 5f 72 65 73 75 6c 74 5f 2a 28 29 20 72 6f 75 74  _result_*() rout
33012 69 6e 65 73 20 74 6f 20 73 74 6f 72 65 20 74 68  ines to store th
33013 65 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 76  e requested.** v
33014 61 6c 75 65 20 62 61 63 6b 20 69 6e 20 74 68 65  alue back in the
33015 20 70 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 73 74   pContext..*/.st
33016 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
33017 74 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f  tColumn(sqlite3_
33018 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
33019 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20  rsor,.          
3301a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3301b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3301c 2a 70 43 6f 6e 74 65 78 74 2c 20 69 6e 74 20 69  *pContext, int i
3301d 64 78 43 6f 6c 29 7b 0a 20 20 66 75 6c 6c 74 65  dxCol){.  fullte
3301e 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  xt_cursor *c = (
3301f 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
33020 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 66 75  *) pCursor;.  fu
33021 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
33022 20 63 75 72 73 6f 72 5f 76 74 61 62 28 63 29 3b   cursor_vtab(c);
33023 0a 0a 20 20 69 66 28 20 69 64 78 43 6f 6c 3c 76  ..  if( idxCol<v
33024 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
33025 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
33026 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63  pVal = sqlite3_c
33027 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 63 2d 3e 70  olumn_value(c->p
33028 53 74 6d 74 2c 20 69 64 78 43 6f 6c 2b 31 29 3b  Stmt, idxCol+1);
33029 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3302a 75 6c 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65  ult_value(pConte
3302b 78 74 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 65 6c  xt, pVal);.  }el
3302c 73 65 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76  se if( idxCol==v
3302d 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
3302e 20 2f 2a 20 54 68 65 20 65 78 74 72 61 20 63 6f   /* The extra co
3302f 6c 75 6d 6e 20 77 68 6f 73 65 20 6e 61 6d 65 20  lumn whose name 
33030 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
33031 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a  he table..    **
33032 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 62 20 77   Return a blob w
33033 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65  hich is a pointe
33034 72 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72 0a  r to the cursor.
33035 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
33036 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 70  e3_result_blob(p
33037 43 6f 6e 74 65 78 74 2c 20 26 63 2c 20 73 69 7a  Context, &c, siz
33038 65 6f 66 28 63 29 2c 20 53 51 4c 49 54 45 5f 54  eof(c), SQLITE_T
33039 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c  RANSIENT);.  }el
3303a 73 65 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76  se if( idxCol==v
3303b 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20 29 7b 0a 20  ->nColumn+1 ){. 
3303c 20 20 20 2f 2a 20 54 68 65 20 64 6f 63 69 64 20     /* The docid 
3303d 63 6f 6c 75 6d 6e 2c 20 77 68 69 63 68 20 69 73  column, which is
3303e 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f   an alias for ro
3303f 77 69 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  wid. */.    sqli
33040 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20  te3_value *pVal 
33041 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
33042 5f 76 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c  _value(c->pStmt,
33043 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
33044 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 70 43  _result_value(pC
33045 6f 6e 74 65 78 74 2c 20 70 56 61 6c 29 3b 0a 20  ontext, pVal);. 
33046 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
33047 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  TE_OK;.}../* Thi
33048 73 20 69 73 20 74 68 65 20 78 52 6f 77 69 64 20  s is the xRowid 
33049 6d 65 74 68 6f 64 2e 20 20 54 68 65 20 53 51 4c  method.  The SQL
3304a 69 74 65 20 63 6f 72 65 20 63 61 6c 6c 73 20 74  ite core calls t
3304b 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 0a 2a  his routine to.*
3304c 2a 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72  * retrieve the r
3304d 6f 77 69 64 20 66 6f 72 20 74 68 65 20 63 75 72  owid for the cur
3304e 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
3304f 72 65 73 75 6c 74 20 73 65 74 2e 20 20 66 74 73  result set.  fts
33050 33 0a 2a 2a 20 65 78 70 6f 73 65 73 20 25 5f 63  3.** exposes %_c
33051 6f 6e 74 65 6e 74 2e 64 6f 63 69 64 20 61 73 20  ontent.docid as 
33052 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68  the rowid for th
33053 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
33054 20 20 54 68 65 0a 2a 2a 20 72 6f 77 69 64 20 73    The.** rowid s
33055 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e  hould be written
33056 20 74 6f 20 2a 70 52 6f 77 69 64 2e 0a 2a 2f 0a   to *pRowid..*/.
33057 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
33058 65 78 74 52 6f 77 69 64 28 73 71 6c 69 74 65 33  extRowid(sqlite3
33059 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43  _vtab_cursor *pC
3305a 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e  ursor, sqlite_in
3305b 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20  t64 *pRowid){.  
3305c 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
3305d 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63  *c = (fulltext_c
3305e 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72  ursor *) pCursor
3305f 3b 0a 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 73  ;..  *pRowid = s
33060 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
33061 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29  t64(c->pStmt, 0)
33062 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
33063 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20  E_OK;.}../* Add 
33064 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 5b 7a 54  all terms in [zT
33065 65 78 74 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54  ext] to pendingT
33066 65 72 6d 73 20 74 61 62 6c 65 2e 20 20 49 66 20  erms table.  If 
33067 5b 69 43 6f 6c 75 6d 6e 5d 20 3e 20 30 2c 0a 2a  [iColumn] > 0,.*
33068 2a 20 77 65 20 61 6c 73 6f 20 73 74 6f 72 65 20  * we also store 
33069 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f 66  positions and of
3306a 66 73 65 74 73 20 69 6e 20 74 68 65 20 68 61 73  fsets in the has
3306b 68 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68  h table using th
3306c 61 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d  at.** column num
3306d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ber..*/.static i
3306e 6e 74 20 62 75 69 6c 64 54 65 72 6d 73 28 66 75  nt buildTerms(fu
3306f 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
33070 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
33071 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cid,.           
33072 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
33073 20 63 68 61 72 20 2a 7a 54 65 78 74 2c 20 69 6e   char *zText, in
33074 74 20 69 43 6f 6c 75 6d 6e 29 7b 0a 20 20 73 71  t iColumn){.  sq
33075 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
33076 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 76 2d  *pTokenizer = v-
33077 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73  >pTokenizer;.  s
33078 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
33079 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
3307a 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
3307b 70 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54  pToken;.  int nT
3307c 6f 6b 65 6e 42 79 74 65 73 3b 0a 20 20 69 6e 74  okenBytes;.  int
3307d 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69   iStartOffset, i
3307e 45 6e 64 4f 66 66 73 65 74 2c 20 69 50 6f 73 69  EndOffset, iPosi
3307f 74 69 6f 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  tion;.  int rc;.
33080 0a 20 20 72 63 20 3d 20 70 54 6f 6b 65 6e 69 7a  .  rc = pTokeniz
33081 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70  er->pModule->xOp
33082 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a  en(pTokenizer, z
33083 54 65 78 74 2c 20 2d 31 2c 20 26 70 43 75 72 73  Text, -1, &pCurs
33084 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  or);.  if( rc!=S
33085 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
33086 6e 20 72 63 3b 0a 0a 20 20 70 43 75 72 73 6f 72  n rc;..  pCursor
33087 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70  ->pTokenizer = p
33088 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 77 68 69  Tokenizer;.  whi
33089 6c 65 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  le( SQLITE_OK==(
3308a 72 63 3d 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70  rc=pTokenizer->p
3308b 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43  Module->xNext(pC
3308c 75 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  ursor,.         
3308d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3308e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3308f 20 20 20 20 20 20 20 20 20 20 26 70 54 6f 6b 65            &pToke
33090 6e 2c 20 26 6e 54 6f 6b 65 6e 42 79 74 65 73 2c  n, &nTokenBytes,
33091 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33092 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33094 20 20 20 20 26 69 53 74 61 72 74 4f 66 66 73 65      &iStartOffse
33095 74 2c 20 26 69 45 6e 64 4f 66 66 73 65 74 2c 0a  t, &iEndOffset,.
33096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33097 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33098 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33099 20 20 20 26 69 50 6f 73 69 74 69 6f 6e 29 29 20     &iPosition)) 
3309a 29 7b 0a 20 20 20 20 44 4c 43 6f 6c 6c 65 63 74  ){.    DLCollect
3309b 6f 72 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e  or *p;.    int n
3309c 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
3309d 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
3309e 6f 66 20 64 6f 63 6c 69 73 74 20 62 65 66 6f 72  of doclist befor
3309f 65 20 6f 75 72 20 75 70 64 61 74 65 2e 20 2a 2f  e our update. */
330a0 0a 0a 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f  ..    /* Positio
330a1 6e 73 20 63 61 6e 27 74 20 62 65 20 6e 65 67 61  ns can't be nega
330a2 74 69 76 65 3b 20 77 65 20 75 73 65 20 2d 31 20  tive; we use -1 
330a3 61 73 20 61 20 74 65 72 6d 69 6e 61 74 6f 72 0a  as a terminator.
330a4 20 20 20 20 20 2a 20 69 6e 74 65 72 6e 61 6c 6c       * internall
330a5 79 2e 20 20 54 6f 6b 65 6e 20 63 61 6e 27 74 20  y.  Token can't 
330a6 62 65 20 4e 55 4c 4c 20 6f 72 20 65 6d 70 74 79  be NULL or empty
330a7 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 6f  . */.    if( iPo
330a8 73 69 74 69 6f 6e 3c 30 20 7c 7c 20 70 54 6f 6b  sition<0 || pTok
330a9 65 6e 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 6e 54  en == NULL || nT
330aa 6f 6b 65 6e 42 79 74 65 73 20 3d 3d 20 30 20 29  okenBytes == 0 )
330ab 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
330ac 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
330ad 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
330ae 20 20 20 70 20 3d 20 66 74 73 33 48 61 73 68 46     p = fts3HashF
330af 69 6e 64 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54  ind(&v->pendingT
330b0 65 72 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20 6e 54  erms, pToken, nT
330b1 6f 6b 65 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  okenBytes);.    
330b2 69 66 28 20 70 3d 3d 4e 55 4c 4c 20 29 7b 0a 20  if( p==NULL ){. 
330b3 20 20 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a       nData = 0;.
330b4 20 20 20 20 20 20 70 20 3d 20 64 6c 63 4e 65 77        p = dlcNew
330b5 28 69 44 6f 63 69 64 2c 20 44 4c 5f 44 45 46 41  (iDocid, DL_DEFA
330b6 55 4c 54 29 3b 0a 20 20 20 20 20 20 66 74 73 33  ULT);.      fts3
330b7 48 61 73 68 49 6e 73 65 72 74 28 26 76 2d 3e 70  HashInsert(&v->p
330b8 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 70 54 6f  endingTerms, pTo
330b9 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 42 79 74 65 73  ken, nTokenBytes
330ba 2c 20 70 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  , p);..      /* 
330bb 4f 76 65 72 68 65 61 64 20 66 6f 72 20 6f 75 72  Overhead for our
330bc 20 68 61 73 68 20 74 61 62 6c 65 20 65 6e 74 72   hash table entr
330bd 79 2c 20 74 68 65 20 6b 65 79 2c 20 61 6e 64 20  y, the key, and 
330be 74 68 65 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20  the value. */.  
330bf 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44      v->nPendingD
330c0 61 74 61 20 2b 3d 20 73 69 7a 65 6f 66 28 73 74  ata += sizeof(st
330c1 72 75 63 74 20 66 74 73 33 48 61 73 68 45 6c 65  ruct fts3HashEle
330c2 6d 29 2b 73 69 7a 65 6f 66 28 2a 70 29 2b 6e 54  m)+sizeof(*p)+nT
330c3 6f 6b 65 6e 42 79 74 65 73 3b 0a 20 20 20 20 7d  okenBytes;.    }
330c4 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 44 61 74  else{.      nDat
330c5 61 20 3d 20 70 2d 3e 62 2e 6e 44 61 74 61 3b 0a  a = p->b.nData;.
330c6 20 20 20 20 20 20 69 66 28 20 70 2d 3e 64 6c 77        if( p->dlw
330c7 2e 69 50 72 65 76 44 6f 63 69 64 21 3d 69 44 6f  .iPrevDocid!=iDo
330c8 63 69 64 20 29 20 64 6c 63 4e 65 78 74 28 70 2c  cid ) dlcNext(p,
330c9 20 69 44 6f 63 69 64 29 3b 0a 20 20 20 20 7d 0a   iDocid);.    }.
330ca 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3e      if( iColumn>
330cb 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6c 63 41  =0 ){.      dlcA
330cc 64 64 50 6f 73 28 70 2c 20 69 43 6f 6c 75 6d 6e  ddPos(p, iColumn
330cd 2c 20 69 50 6f 73 69 74 69 6f 6e 2c 20 69 53 74  , iPosition, iSt
330ce 61 72 74 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f  artOffset, iEndO
330cf 66 66 73 65 74 29 3b 0a 20 20 20 20 7d 0a 0a 20  ffset);.    }.. 
330d0 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65     /* Accumulate
330d1 20 64 61 74 61 20 61 64 64 65 64 20 62 79 20 64   data added by d
330d2 6c 63 4e 65 77 20 6f 72 20 64 6c 63 4e 65 78 74  lcNew or dlcNext
330d3 2c 20 61 6e 64 20 64 6c 63 41 64 64 50 6f 73 2e  , and dlcAddPos.
330d4 20 2a 2f 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64   */.    v->nPend
330d5 69 6e 67 44 61 74 61 20 2b 3d 20 70 2d 3e 62 2e  ingData += p->b.
330d6 6e 44 61 74 61 2d 6e 44 61 74 61 3b 0a 20 20 7d  nData-nData;.  }
330d7 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  ..  /* TODO(shes
330d8 73 29 20 43 68 65 63 6b 20 72 65 74 75 72 6e 3f  s) Check return?
330d9 20 20 53 68 6f 75 6c 64 20 74 68 69 73 20 62 65    Should this be
330da 20 61 62 6c 65 20 74 6f 20 63 61 75 73 65 20 65   able to cause e
330db 72 72 6f 72 73 20 61 74 0a 20 20 2a 2a 20 74 68  rrors at.  ** th
330dc 69 73 20 70 6f 69 6e 74 3f 20 20 41 63 74 75 61  is point?  Actua
330dd 6c 6c 79 2c 20 73 61 6d 65 20 71 75 65 73 74 69  lly, same questi
330de 6f 6e 20 61 62 6f 75 74 20 73 71 6c 69 74 65 33  on about sqlite3
330df 5f 66 69 6e 61 6c 69 7a 65 28 29 2c 0a 20 20 2a  _finalize(),.  *
330e0 2a 20 74 68 6f 75 67 68 20 6f 6e 65 20 63 6f 75  * though one cou
330e1 6c 64 20 61 72 67 75 65 20 74 68 61 74 20 66 61  ld argue that fa
330e2 69 6c 75 72 65 20 74 68 65 72 65 20 6d 65 61 6e  ilure there mean
330e3 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20  s that the data 
330e4 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 64 75 72 61  is.  ** not dura
330e5 62 6c 65 2e 20 20 2a 70 6f 6e 64 65 72 2a 0a 20  ble.  *ponder*. 
330e6 20 2a 2f 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72   */.  pTokenizer
330e7 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73  ->pModule->xClos
330e8 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66  e(pCursor);.  if
330e9 28 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3d 3d  ( SQLITE_DONE ==
330ea 20 72 63 20 29 20 72 65 74 75 72 6e 20 53 51 4c   rc ) return SQL
330eb 49 54 45 5f 4f 4b 3b 0a 20 20 72 65 74 75 72 6e  ITE_OK;.  return
330ec 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 64   rc;.}../* Add d
330ed 6f 63 6c 69 73 74 73 20 66 6f 72 20 61 6c 6c 20  oclists for all 
330ee 74 65 72 6d 73 20 69 6e 20 5b 70 56 61 6c 75 65  terms in [pValue
330ef 73 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72  s] to pendingTer
330f0 6d 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 73 74 61  ms table. */.sta
330f1 74 69 63 20 69 6e 74 20 69 6e 73 65 72 74 54 65  tic int insertTe
330f2 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rms(fulltext_vta
330f3 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
330f4 36 34 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20  64 iDocid,.     
330f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
330f6 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
330f7 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e  **pValues){.  in
330f8 74 20 69 3b 0a 20 20 66 6f 72 28 69 20 3d 20 30  t i;.  for(i = 0
330f9 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e  ; i < v->nColumn
330fa 20 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 63 68 61   ; ++i){.    cha
330fb 72 20 2a 7a 54 65 78 74 20 3d 20 28 63 68 61 72  r *zText = (char
330fc 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
330fd 74 65 78 74 28 70 56 61 6c 75 65 73 5b 69 5d 29  text(pValues[i])
330fe 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62  ;.    int rc = b
330ff 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69 44 6f  uildTerms(v, iDo
33100 63 69 64 2c 20 7a 54 65 78 74 2c 20 69 29 3b 0a  cid, zText, i);.
33101 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
33102 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
33103 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  c;.  }.  return 
33104 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
33105 20 41 64 64 20 65 6d 70 74 79 20 64 6f 63 6c 69   Add empty docli
33106 73 74 73 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d  sts for all term
33107 73 20 69 6e 20 74 68 65 20 67 69 76 65 6e 20 72  s in the given r
33108 6f 77 27 73 20 63 6f 6e 74 65 6e 74 20 74 6f 0a  ow's content to.
33109 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e  ** pendingTerms.
3310a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
3310b 65 6c 65 74 65 54 65 72 6d 73 28 66 75 6c 6c 74  eleteTerms(fullt
3310c 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
3310d 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  ite_int64 iDocid
3310e 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
3310f 2a 2a 70 56 61 6c 75 65 73 3b 0a 20 20 69 6e 74  **pValues;.  int
33110 20 69 2c 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 4f   i, rc;..  /* TO
33111 44 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64  DO(shess) Should
33112 20 77 65 20 61 6c 6c 6f 77 20 73 75 63 68 20 74   we allow such t
33113 61 62 6c 65 73 20 61 74 20 61 6c 6c 3f 20 2a 2f  ables at all? */
33114 0a 20 20 69 66 28 20 44 4c 5f 44 45 46 41 55 4c  .  if( DL_DEFAUL
33115 54 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72  T==DL_DOCIDS ) r
33116 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
33117 4f 52 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74  OR;..  rc = cont
33118 65 6e 74 5f 73 65 6c 65 63 74 28 76 2c 20 69 44  ent_select(v, iD
33119 6f 63 69 64 2c 20 26 70 56 61 6c 75 65 73 29 3b  ocid, &pValues);
3311a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3311b 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
3311c 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30 20 3b  ;..  for(i = 0 ;
3311d 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b   i < v->nColumn;
3311e 20 2b 2b 69 29 20 7b 0a 20 20 20 20 72 63 20 3d   ++i) {.    rc =
3311f 20 62 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69   buildTerms(v, i
33120 44 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 5b 69  Docid, pValues[i
33121 5d 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20  ], -1);.    if( 
33122 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
33123 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 66 72  break;.  }..  fr
33124 65 65 53 74 72 69 6e 67 41 72 72 61 79 28 76 2d  eeStringArray(v-
33125 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 56 61 6c 75 65  >nColumn, pValue
33126 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  s);.  return SQL
33127 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 4f  ITE_OK;.}../* TO
33128 44 4f 28 73 68 65 73 73 29 20 52 65 66 61 63 74  DO(shess) Refact
33129 6f 72 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72  or the code to r
3312a 65 6d 6f 76 65 20 74 68 69 73 20 66 6f 72 77 61  emove this forwa
3312b 72 64 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61 74  rd decl. */.stat
3312c 69 63 20 69 6e 74 20 69 6e 69 74 50 65 6e 64 69  ic int initPendi
3312d 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ngTerms(fulltext
3312e 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65  _vtab *v, sqlite
3312f 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 3b 0a  _int64 iDocid);.
33130 0a 2f 2a 20 49 6e 73 65 72 74 20 61 20 72 6f 77  ./* Insert a row
33131 20 69 6e 74 6f 20 74 68 65 20 25 5f 63 6f 6e 74   into the %_cont
33132 65 6e 74 20 74 61 62 6c 65 3b 20 73 65 74 20 2a  ent table; set *
33133 70 69 44 6f 63 69 64 20 74 6f 20 62 65 20 74 68  piDocid to be th
33134 65 20 49 44 20 6f 66 20 74 68 65 0a 2a 2a 20 6e  e ID of the.** n
33135 65 77 20 72 6f 77 2e 20 20 41 64 64 20 64 6f 63  ew row.  Add doc
33136 6c 69 73 74 73 20 66 6f 72 20 74 65 72 6d 73 20  lists for terms 
33137 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e  to pendingTerms.
33138 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
33139 6e 64 65 78 5f 69 6e 73 65 72 74 28 66 75 6c 6c  ndex_insert(full
3313a 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
3313b 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65  lite3_value *pRe
3313c 71 75 65 73 74 44 6f 63 69 64 2c 0a 20 20 20 20  questDocid,.    
3313d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3313e 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
3313f 65 20 2a 2a 70 56 61 6c 75 65 73 2c 20 73 71 6c  e **pValues, sql
33140 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 44 6f 63  ite_int64 *piDoc
33141 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  id){.  int rc;..
33142 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 69    rc = content_i
33143 6e 73 65 72 74 28 76 2c 20 70 52 65 71 75 65 73  nsert(v, pReques
33144 74 44 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29  tDocid, pValues)
33145 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61 6e  ;  /* execute an
33146 20 53 51 4c 20 49 4e 53 45 52 54 20 2a 2f 0a 20   SQL INSERT */. 
33147 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
33148 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
33149 0a 20 20 2f 2a 20 64 6f 63 69 64 20 63 6f 6c 75  .  /* docid colu
3314a 6d 6e 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66  mn is an alias f
3314b 6f 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a  or rowid. */.  *
3314c 70 69 44 6f 63 69 64 20 3d 20 73 71 6c 69 74 65  piDocid = sqlite
3314d 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
3314e 77 69 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72 63  wid(v->db);.  rc
3314f 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65   = initPendingTe
33150 72 6d 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 29  rms(v, *piDocid)
33151 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
33152 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
33153 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e 73  c;..  return ins
33154 65 72 74 54 65 72 6d 73 28 76 2c 20 2a 70 69 44  ertTerms(v, *piD
33155 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29 3b 0a  ocid, pValues);.
33156 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65 20 61 20 72  }../* Delete a r
33157 6f 77 20 66 72 6f 6d 20 74 68 65 20 25 5f 63 6f  ow from the %_co
33158 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20 61 64 64  ntent table; add
33159 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 73 20   empty doclists 
3315a 66 6f 72 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20  for terms.** to 
3315b 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f  pendingTerms..*/
3315c 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65  .static int inde
3315d 78 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78  x_delete(fulltex
3315e 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
3315f 65 5f 69 6e 74 36 34 20 69 52 6f 77 29 7b 0a 20  e_int64 iRow){. 
33160 20 69 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65   int rc = initPe
33161 6e 64 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52  ndingTerms(v, iR
33162 6f 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ow);.  if( rc!=S
33163 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
33164 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 64 65  n rc;..  rc = de
33165 6c 65 74 65 54 65 72 6d 73 28 76 2c 20 69 52 6f  leteTerms(v, iRo
33166 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  w);.  if( rc!=SQ
33167 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
33168 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 63   rc;..  return c
33169 6f 6e 74 65 6e 74 5f 64 65 6c 65 74 65 28 76 2c  ontent_delete(v,
3316a 20 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65 63   iRow);  /* exec
3316b 75 74 65 20 61 6e 20 53 51 4c 20 44 45 4c 45 54  ute an SQL DELET
3316c 45 20 2a 2f 0a 7d 0a 0a 2f 2a 20 55 70 64 61 74  E */.}../* Updat
3316d 65 20 61 20 72 6f 77 20 69 6e 20 74 68 65 20 25  e a row in the %
3316e 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20  _content table; 
3316f 61 64 64 20 64 65 6c 65 74 65 20 64 6f 63 6c 69  add delete docli
33170 73 74 73 20 74 6f 0a 2a 2a 20 70 65 6e 64 69 6e  sts to.** pendin
33171 67 54 65 72 6d 73 20 66 6f 72 20 6f 6c 64 20 74  gTerms for old t
33172 65 72 6d 73 20 6e 6f 74 20 69 6e 20 74 68 65 20  erms not in the 
33173 6e 65 77 20 64 61 74 61 2c 20 61 64 64 20 69 6e  new data, add in
33174 73 65 72 74 20 64 6f 63 6c 69 73 74 73 0a 2a 2a  sert doclists.**
33175 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   to pendingTerms
33176 20 66 6f 72 20 74 65 72 6d 73 20 69 6e 20 74 68   for terms in th
33177 65 20 6e 65 77 20 64 61 74 61 2e 0a 2a 2f 0a 73  e new data..*/.s
33178 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f  tatic int index_
33179 75 70 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f  update(fulltext_
3317a 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f  vtab *v, sqlite_
3317b 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 20 20  int64 iRow,.    
3317c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3317d 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
3317e 65 20 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20  e **pValues){.  
3317f 69 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65 6e  int rc = initPen
33180 64 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52 6f  dingTerms(v, iRo
33181 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  w);.  if( rc!=SQ
33182 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
33183 20 72 63 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72   rc;..  /* Gener
33184 61 74 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63  ate an empty doc
33185 6c 69 73 74 20 66 6f 72 20 65 61 63 68 20 74 65  list for each te
33186 72 6d 20 74 68 61 74 20 70 72 65 76 69 6f 75 73  rm that previous
33187 6c 79 20 61 70 70 65 61 72 65 64 20 69 6e 20 74  ly appeared in t
33188 68 69 73 0a 20 20 20 2a 20 72 6f 77 2e 20 2a 2f  his.   * row. */
33189 0a 20 20 72 63 20 3d 20 64 65 6c 65 74 65 54 65  .  rc = deleteTe
3318a 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20  rms(v, iRow);.  
3318b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3318c 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
3318d 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 75    rc = content_u
3318e 70 64 61 74 65 28 76 2c 20 70 56 61 6c 75 65 73  pdate(v, pValues
3318f 2c 20 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65  , iRow);  /* exe
33190 63 75 74 65 20 61 6e 20 53 51 4c 20 55 50 44 41  cute an SQL UPDA
33191 54 45 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d  TE */.  if( rc!=
33192 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
33193 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4e 6f 77  rn rc;..  /* Now
33194 20 61 64 64 20 70 6f 73 69 74 69 6f 6e 73 20 66   add positions f
33195 6f 72 20 74 65 72 6d 73 20 77 68 69 63 68 20 61  or terms which a
33196 70 70 65 61 72 20 69 6e 20 74 68 65 20 75 70 64  ppear in the upd
33197 61 74 65 64 20 72 6f 77 2e 20 2a 2f 0a 20 20 72  ated row. */.  r
33198 65 74 75 72 6e 20 69 6e 73 65 72 74 54 65 72 6d  eturn insertTerm
33199 73 28 76 2c 20 69 52 6f 77 2c 20 70 56 61 6c 75  s(v, iRow, pValu
3319a 65 73 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  es);.}../*******
3319b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3319c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3319d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3319e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
3319f 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20   InteriorWriter 
331a0 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65  is used to colle
331a1 63 74 20 74 65 72 6d 73 20 61 6e 64 20 62 6c 6f  ct terms and blo
331a2 63 6b 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e  ck references in
331a3 74 6f 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e  to.** interior n
331a4 6f 64 65 73 20 69 6e 20 25 5f 73 65 67 6d 65 6e  odes in %_segmen
331a5 74 73 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74  ts.  See comment
331a6 61 72 79 20 61 74 20 74 6f 70 20 6f 66 20 66 69  ary at top of fi
331a7 6c 65 20 66 6f 72 0a 2a 2a 20 66 6f 72 6d 61 74  le for.** format
331a8 2e 0a 2a 2f 0a 0a 2f 2a 20 48 6f 77 20 6c 61 72  ..*/../* How lar
331a9 67 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ge interior node
331aa 73 20 63 61 6e 20 67 72 6f 77 2e 20 2a 2f 0a 23  s can grow. */.#
331ab 64 65 66 69 6e 65 20 49 4e 54 45 52 49 4f 52 5f  define INTERIOR_
331ac 4d 41 58 20 32 30 34 38 0a 0a 2f 2a 20 4d 69 6e  MAX 2048../* Min
331ad 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74  imum number of t
331ae 65 72 6d 73 20 70 65 72 20 69 6e 74 65 72 69 6f  erms per interio
331af 72 20 6e 6f 64 65 20 28 65 78 63 65 70 74 20 74  r node (except t
331b0 68 65 20 72 6f 6f 74 29 2e 20 54 68 69 73 0a 2a  he root). This.*
331b1 2a 20 70 72 65 76 65 6e 74 73 20 6c 61 72 67 65  * prevents large
331b2 20 74 65 72 6d 73 20 66 72 6f 6d 20 6d 61 6b 69   terms from maki
331b3 6e 67 20 74 68 65 20 74 72 65 65 20 74 6f 6f 20  ng the tree too 
331b4 73 6b 69 6e 6e 79 20 2d 20 6d 75 73 74 20 62 65  skinny - must be
331b5 20 3e 30 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74   >0.** so that t
331b6 68 65 20 74 72 65 65 20 61 6c 77 61 79 73 20 6d  he tree always m
331b7 61 6b 65 73 20 70 72 6f 67 72 65 73 73 2e 20 20  akes progress.  
331b8 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6d 69  Note that the mi
331b9 6e 20 74 72 65 65 0a 2a 2a 20 66 61 6e 6f 75 74  n tree.** fanout
331ba 20 77 69 6c 6c 20 62 65 20 49 4e 54 45 52 49 4f   will be INTERIO
331bb 52 5f 4d 49 4e 5f 54 45 52 4d 53 2b 31 2e 0a 2a  R_MIN_TERMS+1..*
331bc 2f 0a 23 64 65 66 69 6e 65 20 49 4e 54 45 52 49  /.#define INTERI
331bd 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 37 0a 23  OR_MIN_TERMS 7.#
331be 69 66 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f  if INTERIOR_MIN_
331bf 54 45 52 4d 53 3c 31 0a 23 20 65 72 72 6f 72 20  TERMS<1.# error 
331c0 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
331c1 4d 53 20 6d 75 73 74 20 62 65 20 67 72 65 61 74  MS must be great
331c2 65 72 20 74 68 61 6e 20 30 2e 0a 23 65 6e 64 69  er than 0..#endi
331c3 66 0a 0a 2f 2a 20 52 4f 4f 54 5f 4d 41 58 20 63  f../* ROOT_MAX c
331c4 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6d 75 63 68  ontrols how much
331c5 20 64 61 74 61 20 69 73 20 73 74 6f 72 65 64 20   data is stored 
331c6 69 6e 6c 69 6e 65 20 69 6e 20 74 68 65 20 73 65  inline in the se
331c7 67 6d 65 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6f  gment.** directo
331c8 72 79 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  ry..*/./* TODO(s
331c9 68 65 73 73 29 20 50 75 73 68 20 52 4f 4f 54 5f  hess) Push ROOT_
331ca 4d 41 58 20 64 6f 77 6e 20 74 6f 20 77 68 6f 65  MAX down to whoe
331cb 76 65 72 20 69 73 20 77 72 69 74 69 6e 67 20 74  ver is writing t
331cc 68 69 6e 67 73 2e 20 20 49 74 27 73 0a 2a 2a 20  hings.  It's.** 
331cd 6f 6e 6c 79 20 68 65 72 65 20 73 6f 20 74 68 61  only here so tha
331ce 74 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  t interiorWriter
331cf 52 6f 6f 74 49 6e 66 6f 28 29 20 61 6e 64 20 6c  RootInfo() and l
331d0 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49 6e 66  eafWriterRootInf
331d1 6f 28 29 0a 2a 2a 20 63 61 6e 20 62 6f 74 68 20  o().** can both 
331d2 73 65 65 20 69 74 2c 20 62 75 74 20 69 66 20 74  see it, but if t
331d3 68 65 20 63 61 6c 6c 65 72 20 70 61 73 73 65 64  he caller passed
331d4 20 69 74 20 69 6e 2c 20 77 65 20 77 6f 75 6c 64   it in, we would
331d5 6e 27 74 20 65 76 65 6e 0a 2a 2a 20 6e 65 65 64  n't even.** need
331d6 20 61 20 64 65 66 69 6e 65 2e 0a 2a 2f 0a 23 64   a define..*/.#d
331d7 65 66 69 6e 65 20 52 4f 4f 54 5f 4d 41 58 20 31  efine ROOT_MAX 1
331d8 30 32 34 0a 23 69 66 20 52 4f 4f 54 5f 4d 41 58  024.#if ROOT_MAX
331d9 3c 56 41 52 49 4e 54 5f 4d 41 58 2a 32 0a 23 20  <VARINT_MAX*2.# 
331da 65 72 72 6f 72 20 52 4f 4f 54 5f 4d 41 58 20 6d  error ROOT_MAX m
331db 75 73 74 20 68 61 76 65 20 65 6e 6f 75 67 68 20  ust have enough 
331dc 73 70 61 63 65 20 66 6f 72 20 61 20 68 65 61 64  space for a head
331dd 65 72 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49  er..#endif../* I
331de 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 73 74 6f  nteriorBlock sto
331df 72 65 73 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73  res a linked-lis
331e0 74 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 62 6c  t of interior bl
331e1 6f 63 6b 73 20 77 68 69 6c 65 20 61 20 6c 6f 77  ocks while a low
331e2 65 72 0a 2a 2a 20 6c 61 79 65 72 20 69 73 20 62  er.** layer is b
331e3 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64  eing constructed
331e4 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
331e5 75 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63  uct InteriorBloc
331e6 6b 20 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72  k {.  DataBuffer
331e7 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20   term;          
331e8 20 2f 2a 20 4c 65 66 74 6d 6f 73 74 20 74 65 72   /* Leftmost ter
331e9 6d 20 69 6e 20 62 6c 6f 63 6b 27 73 20 73 75 62  m in block's sub
331ea 74 72 65 65 2e 20 2a 2f 0a 20 20 44 61 74 61 42  tree. */.  DataB
331eb 75 66 66 65 72 20 64 61 74 61 3b 20 20 20 20 20  uffer data;     
331ec 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
331ed 61 74 65 64 20 64 61 74 61 20 66 6f 72 20 74 68  ated data for th
331ee 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 73 74  e block. */.  st
331ef 72 75 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f  ruct InteriorBlo
331f0 63 6b 20 2a 6e 65 78 74 3b 0a 7d 20 49 6e 74 65  ck *next;.} Inte
331f1 72 69 6f 72 42 6c 6f 63 6b 3b 0a 0a 73 74 61 74  riorBlock;..stat
331f2 69 63 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  ic InteriorBlock
331f3 20 2a 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e   *interiorBlockN
331f4 65 77 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20  ew(int iHeight, 
331f5 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 43 68  sqlite_int64 iCh
331f6 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20  ildBlock,.      
331f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
331f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
331f9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
331fa 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a  rm, int nTerm){.
331fb 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
331fc 2a 62 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33  *block = sqlite3
331fd 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 49  _malloc(sizeof(I
331fe 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 29 29 3b 0a  nteriorBlock));.
331ff 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f    char c[VARINT_
33200 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b  MAX+VARINT_MAX];
33201 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28  .  int n;..  if(
33202 20 62 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 6d 65   block ){.    me
33203 6d 73 65 74 28 62 6c 6f 63 6b 2c 20 30 2c 20 73  mset(block, 0, s
33204 69 7a 65 6f 66 28 2a 62 6c 6f 63 6b 29 29 3b 0a  izeof(*block));.
33205 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e      dataBufferIn
33206 69 74 28 26 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2c  it(&block->term,
33207 20 30 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66   0);.    dataBuf
33208 66 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f 63  ferReplace(&bloc
33209 6b 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20  k->term, pTerm, 
3320a 6e 54 65 72 6d 29 3b 0a 0a 20 20 20 20 6e 20 3d  nTerm);..    n =
3320b 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
3320c 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20  , iHeight);.    
3320d 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
3320e 6e 74 28 63 2b 6e 2c 20 69 43 68 69 6c 64 42 6c  nt(c+n, iChildBl
3320f 6f 63 6b 29 3b 0a 20 20 20 20 64 61 74 61 42 75  ock);.    dataBu
33210 66 66 65 72 49 6e 69 74 28 26 62 6c 6f 63 6b 2d  fferInit(&block-
33211 3e 64 61 74 61 2c 20 49 4e 54 45 52 49 4f 52 5f  >data, INTERIOR_
33212 4d 41 58 29 3b 0a 20 20 20 20 64 61 74 61 42 75  MAX);.    dataBu
33213 66 66 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f  fferReplace(&blo
33214 63 6b 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 29 3b  ck->data, c, n);
33215 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 62 6c  .  }.  return bl
33216 6f 63 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ock;.}..#ifndef 
33217 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79  NDEBUG./* Verify
33218 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20 69   that the data i
33219 73 20 72 65 61 64 61 62 6c 65 20 61 73 20 61 6e  s readable as an
3321a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20   interior node. 
3321b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
3321c 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c 69  nteriorBlockVali
3321d 64 61 74 65 28 49 6e 74 65 72 69 6f 72 42 6c 6f  date(InteriorBlo
3321e 63 6b 20 2a 70 42 6c 6f 63 6b 29 7b 0a 20 20 63  ck *pBlock){.  c
3321f 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
33220 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e   = pBlock->data.
33221 70 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61  pData;.  int nDa
33222 74 61 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74  ta = pBlock->dat
33223 61 2e 6e 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e  a.nData;.  int n
33224 2c 20 69 44 75 6d 6d 79 3b 0a 20 20 73 71 6c 69  , iDummy;.  sqli
33225 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69  te_int64 iBlocki
33226 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44  d;..  assert( nD
33227 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ata>0 );.  asser
33228 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20  t( pData!=0 );. 
33229 20 61 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e   assert( pData+n
3322a 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a 20  Data>pData );.. 
3322b 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64 20 77 69   /* Must lead wi
3322c 74 68 20 68 65 69 67 68 74 20 6f 66 20 6e 6f 64  th height of nod
3322d 65 20 61 73 20 61 20 76 61 72 69 6e 74 28 6e 29  e as a varint(n)
3322e 2c 20 6e 3e 30 20 2a 2f 0a 20 20 6e 20 3d 20 66  , n>0 */.  n = f
3322f 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
33230 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
33231 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b    assert( n>0 );
33232 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d  .  assert( iDumm
33233 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  y>0 );.  assert(
33234 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44   n<nData );.  pD
33235 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74  ata += n;.  nDat
33236 61 20 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75  a -= n;..  /* Mu
33237 73 74 20 63 6f 6e 74 61 69 6e 20 69 42 6c 6f 63  st contain iBloc
33238 6b 69 64 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  kid. */.  n = ft
33239 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61 74  s3GetVarint(pDat
3323a 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  a, &iBlockid);. 
3323b 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a   assert( n>0 );.
3323c 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61    assert( n<=nDa
3323d 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d  ta );.  pData +=
3323e 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e   n;.  nData -= n
3323f 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20  ;..  /* Zero or 
33240 6d 6f 72 65 20 74 65 72 6d 73 20 6f 66 20 70 6f  more terms of po
33241 73 69 74 69 76 65 20 6c 65 6e 67 74 68 20 2a 2f  sitive length */
33242 0a 20 20 69 66 28 20 6e 44 61 74 61 21 3d 30 20  .  if( nData!=0 
33243 29 7b 0a 20 20 20 20 2f 2a 20 46 69 72 73 74 20  ){.    /* First 
33244 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 65 6c 74  term is not delt
33245 61 2d 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20  a-encoded. */.  
33246 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
33247 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
33248 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72  ummy);.    asser
33249 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73  t( n>0 );.    as
3324a 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29  sert( iDummy>0 )
3324b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b  ;.    assert( n+
3324c 69 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20 61  iDummy>0);.    a
3324d 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
3324e 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44  =nData );.    pD
3324f 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata += n+iDummy;
33250 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b  .    nData -= n+
33251 69 44 75 6d 6d 79 3b 0a 0a 20 20 20 20 2f 2a 20  iDummy;..    /* 
33252 46 6f 6c 6c 6f 77 69 6e 67 20 74 65 72 6d 73 20  Following terms 
33253 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 20 2a  delta-encoded. *
33254 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 44 61  /.    while( nDa
33255 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ta!=0 ){.      /
33256 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 68 61 72  * Length of shar
33257 65 64 20 70 72 65 66 69 78 2e 20 2a 2f 0a 20 20  ed prefix. */.  
33258 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56      n = fts3GetV
33259 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26  arint32(pData, &
3325a 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 61  iDummy);.      a
3325b 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
3325c 20 20 20 20 61 73 73 65 72 74 28 20 69 44 75 6d      assert( iDum
3325d 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  my>=0 );.      a
3325e 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29  ssert( n<nData )
3325f 3b 0a 20 20 20 20 20 20 70 44 61 74 61 20 2b 3d  ;.      pData +=
33260 20 6e 3b 0a 20 20 20 20 20 20 6e 44 61 74 61 20   n;.      nData 
33261 2d 3d 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  -= n;..      /* 
33262 4c 65 6e 67 74 68 20 61 6e 64 20 64 61 74 61 20  Length and data 
33263 6f 66 20 64 69 73 74 69 6e 63 74 20 73 75 66 66  of distinct suff
33264 69 78 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 20 3d  ix. */.      n =
33265 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
33266 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29  (pData, &iDummy)
33267 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
33268 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  n>0 );.      ass
33269 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
3326a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
3326b 2b 69 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20  +iDummy>0);.    
3326c 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
3326d 6d 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20  my<=nData );.   
3326e 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44     pData += n+iD
3326f 75 6d 6d 79 3b 0a 20 20 20 20 20 20 6e 44 61 74  ummy;.      nDat
33270 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a -= n+iDummy;. 
33271 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 64 65 66 69     }.  }.}.#defi
33272 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
33273 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 78  INTERIOR_BLOCK(x
33274 29 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56  ) interiorBlockV
33275 61 6c 69 64 61 74 65 28 78 29 0a 23 65 6c 73 65  alidate(x).#else
33276 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f  .#define ASSERT_
33277 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
33278 4c 4f 43 4b 28 78 29 20 61 73 73 65 72 74 28 20  LOCK(x) assert( 
33279 31 20 29 0a 23 65 6e 64 69 66 0a 0a 74 79 70 65  1 ).#endif..type
3327a 64 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 72  def struct Inter
3327b 69 6f 72 57 72 69 74 65 72 20 7b 0a 20 20 69 6e  iorWriter {.  in
3327c 74 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20  t iHeight;      
3327d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3327e 66 72 6f 6d 20 30 20 61 74 20 6c 65 61 76 65 73  from 0 at leaves
3327f 2e 20 2a 2f 0a 20 20 49 6e 74 65 72 69 6f 72 42  . */.  InteriorB
33280 6c 6f 63 6b 20 2a 66 69 72 73 74 2c 20 2a 6c 61  lock *first, *la
33281 73 74 3b 0a 20 20 73 74 72 75 63 74 20 49 6e 74  st;.  struct Int
33282 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70 61 72  eriorWriter *par
33283 65 6e 74 57 72 69 74 65 72 3b 0a 0a 20 20 44 61  entWriter;..  Da
33284 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20  taBuffer term;  
33285 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33286 4c 61 73 74 20 74 65 72 6d 20 77 72 69 74 74 65  Last term writte
33287 6e 20 74 6f 20 62 6c 6f 63 6b 20 22 6c 61 73 74  n to block "last
33288 22 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ". */.  sqlite_i
33289 6e 74 36 34 20 69 4f 70 65 6e 69 6e 67 43 68 69  nt64 iOpeningChi
3328a 6c 64 42 6c 6f 63 6b 3b 20 2f 2a 20 46 69 72 73  ldBlock; /* Firs
3328b 74 20 63 68 69 6c 64 20 62 6c 6f 63 6b 20 69 6e  t child block in
3328c 20 62 6c 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a   block "last". *
3328d 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
3328e 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
3328f 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b 3b  iLastChildBlock;
33290 20 20 2f 2a 20 66 6f 72 20 63 6f 6e 73 69 73 74    /* for consist
33291 65 6e 63 79 20 63 68 65 63 6b 73 2e 20 2a 2f 0a  ency checks. */.
33292 23 65 6e 64 69 66 0a 7d 20 49 6e 74 65 72 69 6f  #endif.} Interio
33293 72 57 72 69 74 65 72 3b 0a 0a 2f 2a 20 49 6e 69  rWriter;../* Ini
33294 74 69 61 6c 69 7a 65 20 61 6e 20 69 6e 74 65 72  tialize an inter
33295 69 6f 72 20 6e 6f 64 65 20 77 68 65 72 65 20 70  ior node where p
33296 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 6d 61 72 6b  Term[nTerm] mark
33297 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 0a 2a  s the leftmost.*
33298 2a 20 74 65 72 6d 20 69 6e 20 74 68 65 20 74 72  * term in the tr
33299 65 65 2e 20 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ee.  iChildBlock
3329a 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74   is the leftmost
3329b 20 63 68 69 6c 64 20 62 6c 6f 63 6b 20 61 74 20   child block at 
3329c 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6c 65 76 65  the.** next leve
3329d 6c 20 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e  l down the tree.
3329e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3329f 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e  interiorWriterIn
332a0 69 74 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20  it(int iHeight, 
332a1 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
332a2 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20  m, int nTerm,.  
332a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332a4 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
332a5 69 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64  ite_int64 iChild
332a6 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20  Block,.         
332a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332a8 20 20 20 20 20 20 49 6e 74 65 72 69 6f 72 57 72        InteriorWr
332a9 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
332aa 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
332ab 2a 62 6c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74  *block;.  assert
332ac 28 20 69 48 65 69 67 68 74 3e 30 20 29 3b 0a 20  ( iHeight>0 );. 
332ad 20 43 4c 45 41 52 28 70 57 72 69 74 65 72 29 3b   CLEAR(pWriter);
332ae 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 48 65  ..  pWriter->iHe
332af 69 67 68 74 20 3d 20 69 48 65 69 67 68 74 3b 0a  ight = iHeight;.
332b0 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e    pWriter->iOpen
332b1 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20  ingChildBlock = 
332b2 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a 23 69 66  iChildBlock;.#if
332b3 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57  ndef NDEBUG.  pW
332b4 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c  riter->iLastChil
332b5 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64 42  dBlock = iChildB
332b6 6c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 20 20 62  lock;.#endif.  b
332b7 6c 6f 63 6b 20 3d 20 69 6e 74 65 72 69 6f 72 42  lock = interiorB
332b8 6c 6f 63 6b 4e 65 77 28 69 48 65 69 67 68 74 2c  lockNew(iHeight,
332b9 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 20 70 54   iChildBlock, pT
332ba 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 70  erm, nTerm);.  p
332bb 57 72 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70  Writer->last = p
332bc 57 72 69 74 65 72 2d 3e 66 69 72 73 74 20 3d 20  Writer->first = 
332bd 62 6c 6f 63 6b 3b 0a 20 20 41 53 53 45 52 54 5f  block;.  ASSERT_
332be 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
332bf 4c 4f 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61  LOCK(pWriter->la
332c0 73 74 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  st);.  dataBuffe
332c1 72 49 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e  rInit(&pWriter->
332c2 74 65 72 6d 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 20  term, 0);.}../* 
332c3 41 70 70 65 6e 64 20 74 68 65 20 63 68 69 6c 64  Append the child
332c4 20 6e 6f 64 65 20 72 6f 6f 74 65 64 20 61 74 20   node rooted at 
332c5 69 43 68 69 6c 64 42 6c 6f 63 6b 20 74 6f 20 74  iChildBlock to t
332c6 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  he interior node
332c7 2c 0a 2a 2a 20 77 69 74 68 20 70 54 65 72 6d 5b  ,.** with pTerm[
332c8 6e 54 65 72 6d 5d 20 61 73 20 74 68 65 20 6c 65  nTerm] as the le
332c9 66 74 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20 69  ftmost term in i
332ca 43 68 69 6c 64 42 6c 6f 63 6b 27 73 20 73 75 62  ChildBlock's sub
332cb 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tree..*/.static 
332cc 76 6f 69 64 20 69 6e 74 65 72 69 6f 72 57 72 69  void interiorWri
332cd 74 65 72 41 70 70 65 6e 64 28 49 6e 74 65 72 69  terAppend(Interi
332ce 6f 72 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  orWriter *pWrite
332cf 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
332d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332d1 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
332d2 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
332d3 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
332d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332d5 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
332d6 69 43 68 69 6c 64 42 6c 6f 63 6b 29 7b 0a 20 20  iChildBlock){.  
332d7 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
332d8 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
332d9 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20   int n, nPrefix 
332da 3d 20 30 3b 0a 0a 20 20 41 53 53 45 52 54 5f 56  = 0;..  ASSERT_V
332db 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c  ALID_INTERIOR_BL
332dc 4f 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73  OCK(pWriter->las
332dd 74 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 69  t);..  /* The fi
332de 72 73 74 20 74 65 72 6d 20 77 72 69 74 74 65 6e  rst term written
332df 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f   into an interio
332e0 72 20 6e 6f 64 65 20 69 73 20 61 63 74 75 61 6c  r node is actual
332e1 6c 79 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74  ly.  ** associat
332e2 65 64 20 77 69 74 68 20 74 68 65 20 73 65 63 6f  ed with the seco
332e3 6e 64 20 63 68 69 6c 64 20 61 64 64 65 64 20 28  nd child added (
332e4 74 68 65 20 66 69 72 73 74 20 63 68 69 6c 64 20  the first child 
332e5 77 61 73 20 61 64 64 65 64 0a 20 20 2a 2a 20 69  was added.  ** i
332e6 6e 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  n interiorWriter
332e7 49 6e 69 74 2c 20 6f 72 20 69 6e 20 74 68 65 20  Init, or in the 
332e8 69 66 20 63 6c 61 75 73 65 20 61 74 20 74 68 65  if clause at the
332e9 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 0a   bottom of this.
332ea 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 29 2e 20    ** function). 
332eb 20 54 68 61 74 20 74 65 72 6d 20 67 65 74 73 20   That term gets 
332ec 65 6e 63 6f 64 65 64 20 73 74 72 61 69 67 68 74  encoded straight
332ed 20 75 70 2c 20 77 69 74 68 20 6e 50 72 65 66 69   up, with nPrefi
332ee 78 20 6c 65 66 74 0a 20 20 2a 2a 20 61 74 20 30  x left.  ** at 0
332ef 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 72  ..  */.  if( pWr
332f0 69 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  iter->term.nData
332f1 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66  ==0 ){.    n = f
332f2 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
332f3 6e 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  nTerm);.  }else{
332f4 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 50 72 65  .    while( nPre
332f5 66 69 78 3c 70 57 72 69 74 65 72 2d 3e 74 65 72  fix<pWriter->ter
332f6 6d 2e 6e 44 61 74 61 20 26 26 0a 20 20 20 20 20  m.nData &&.     
332f7 20 20 20 20 20 20 70 54 65 72 6d 5b 6e 50 72 65        pTerm[nPre
332f8 66 69 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e 74  fix]==pWriter->t
332f9 65 72 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66 69  erm.pData[nPrefi
332fa 78 5d 20 29 7b 0a 20 20 20 20 20 20 6e 50 72 65  x] ){.      nPre
332fb 66 69 78 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  fix++;.    }..  
332fc 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72    n = fts3PutVar
332fd 69 6e 74 28 63 2c 20 6e 50 72 65 66 69 78 29 3b  int(c, nPrefix);
332fe 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
332ff 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65  tVarint(c+n, nTe
33300 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20 7d  rm-nPrefix);.  }
33301 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
33302 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 4c 61 73  .  pWriter->iLas
33303 74 43 68 69 6c 64 42 6c 6f 63 6b 2b 2b 3b 0a 23  tChildBlock++;.#
33304 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20  endif.  assert( 
33305 70 57 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68  pWriter->iLastCh
33306 69 6c 64 42 6c 6f 63 6b 3d 3d 69 43 68 69 6c 64  ildBlock==iChild
33307 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20 2f 2a 20 4f  Block );..  /* O
33308 76 65 72 66 6c 6f 77 20 74 6f 20 61 20 6e 65 77  verflow to a new
33309 20 62 6c 6f 63 6b 20 69 66 20 74 68 65 20 6e 65   block if the ne
3330a 77 20 74 65 72 6d 20 6d 61 6b 65 73 20 74 68 65  w term makes the
3330b 20 63 75 72 72 65 6e 74 20 62 6c 6f 63 6b 0a 20   current block. 
3330c 20 2a 2a 20 74 6f 6f 20 62 69 67 2c 20 61 6e 64   ** too big, and
3330d 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 6c 6f   the current blo
3330e 63 6b 20 61 6c 72 65 61 64 79 20 68 61 73 20 65  ck already has e
3330f 6e 6f 75 67 68 20 74 65 72 6d 73 2e 0a 20 20 2a  nough terms..  *
33310 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  /.  if( pWriter-
33311 3e 6c 61 73 74 2d 3e 64 61 74 61 2e 6e 44 61 74  >last->data.nDat
33312 61 2b 6e 2b 6e 54 65 72 6d 2d 6e 50 72 65 66 69  a+n+nTerm-nPrefi
33313 78 3e 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 26  x>INTERIOR_MAX &
33314 26 0a 20 20 20 20 20 20 69 43 68 69 6c 64 42 6c  &.      iChildBl
33315 6f 63 6b 2d 70 57 72 69 74 65 72 2d 3e 69 4f 70  ock-pWriter->iOp
33316 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 3e  eningChildBlock>
33317 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
33318 4d 53 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65  MS ){.    pWrite
33319 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78 74 20 3d 20  r->last->next = 
3331a 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77  interiorBlockNew
3331b 28 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68  (pWriter->iHeigh
3331c 74 2c 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a  t, iChildBlock,.
3331d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3331e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3331f 20 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d             pTerm
33320 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 70 57  , nTerm);.    pW
33321 72 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70 57  riter->last = pW
33322 72 69 74 65 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78  riter->last->nex
33323 74 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  t;.    pWriter->
33324 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f  iOpeningChildBlo
33325 63 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ck = iChildBlock
33326 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
33327 52 65 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e  Reset(&pWriter->
33328 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  term);.  }else{.
33329 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
3332a 70 65 6e 64 32 28 26 70 57 72 69 74 65 72 2d 3e  pend2(&pWriter->
3332b 6c 61 73 74 2d 3e 64 61 74 61 2c 20 63 2c 20 6e  last->data, c, n
3332c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3332d 20 20 20 20 20 20 20 20 70 54 65 72 6d 2b 6e 50          pTerm+nP
3332e 72 65 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72  refix, nTerm-nPr
3332f 65 66 69 78 29 3b 0a 20 20 20 20 64 61 74 61 42  efix);.    dataB
33330 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70 57  ufferReplace(&pW
33331 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65  riter->term, pTe
33332 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d 0a  rm, nTerm);.  }.
33333 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49    ASSERT_VALID_I
33334 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70 57  NTERIOR_BLOCK(pW
33335 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 7d 0a  riter->last);.}.
33336 0a 2f 2a 20 46 72 65 65 20 74 68 65 20 73 70 61  ./* Free the spa
33337 63 65 20 75 73 65 64 20 62 79 20 70 57 72 69 74  ce used by pWrit
33338 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  er, including th
33339 65 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66  e linked-list of
3333a 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63  .** InteriorBloc
3333b 6b 73 2c 20 61 6e 64 20 70 61 72 65 6e 74 57 72  ks, and parentWr
3333c 69 74 65 72 2c 20 69 66 20 70 72 65 73 65 6e 74  iter, if present
3333d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3333e 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 44 65  interiorWriterDe
3333f 73 74 72 6f 79 28 49 6e 74 65 72 69 6f 72 57 72  stroy(InteriorWr
33340 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
33341 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
33342 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72  *block = pWriter
33343 2d 3e 66 69 72 73 74 3b 0a 0a 20 20 77 68 69 6c  ->first;..  whil
33344 65 28 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 20 29  e( block!=NULL )
33345 7b 0a 20 20 20 20 49 6e 74 65 72 69 6f 72 42 6c  {.    InteriorBl
33346 6f 63 6b 20 2a 62 20 3d 20 62 6c 6f 63 6b 3b 0a  ock *b = block;.
33347 20 20 20 20 62 6c 6f 63 6b 20 3d 20 62 6c 6f 63      block = bloc
33348 6b 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 64 61 74  k->next;.    dat
33349 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
3334a 62 2d 3e 74 65 72 6d 29 3b 0a 20 20 20 20 64 61  b->term);.    da
3334b 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
3334c 26 62 2d 3e 64 61 74 61 29 3b 0a 20 20 20 20 73  &b->data);.    s
3334d 71 6c 69 74 65 33 5f 66 72 65 65 28 62 29 3b 0a  qlite3_free(b);.
3334e 20 20 7d 0a 20 20 69 66 28 20 70 57 72 69 74 65    }.  if( pWrite
3334f 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 21  r->parentWriter!
33350 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74  =NULL ){.    int
33351 65 72 69 6f 72 57 72 69 74 65 72 44 65 73 74 72  eriorWriterDestr
33352 6f 79 28 70 57 72 69 74 65 72 2d 3e 70 61 72 65  oy(pWriter->pare
33353 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20 73  ntWriter);.    s
33354 71 6c 69 74 65 33 5f 66 72 65 65 28 70 57 72 69  qlite3_free(pWri
33355 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65  ter->parentWrite
33356 72 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42 75  r);.  }.  dataBu
33357 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 57 72  fferDestroy(&pWr
33358 69 74 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53  iter->term);.  S
33359 43 52 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29  CRAMBLE(pWriter)
3335a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
3335b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70  E_OK;.}../* If p
3335c 57 72 69 74 65 72 20 63 61 6e 20 66 69 74 20 65  Writer can fit e
3335d 6e 74 69 72 65 6c 79 20 69 6e 20 52 4f 4f 54 5f  ntirely in ROOT_
3335e 4d 41 58 2c 20 72 65 74 75 72 6e 20 69 74 20 61  MAX, return it a
3335f 73 20 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f 0a  s the root info.
33360 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 6c 65 61  ** directly, lea
33361 76 69 6e 67 20 2a 70 69 45 6e 64 42 6c 6f 63 6b  ving *piEndBlock
33362 69 64 20 75 6e 63 68 61 6e 67 65 64 2e 20 20 4f  id unchanged.  O
33363 74 68 65 72 77 69 73 65 2c 20 66 6c 75 73 68 0a  therwise, flush.
33364 2a 2a 20 70 57 72 69 74 65 72 20 74 6f 20 25 5f  ** pWriter to %_
33365 73 65 67 6d 65 6e 74 73 2c 20 62 75 69 6c 64 69  segments, buildi
33366 6e 67 20 61 20 6e 65 77 20 6c 61 79 65 72 20 6f  ng a new layer o
33367 66 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73  f interior nodes
33368 2c 20 61 6e 64 0a 2a 2a 20 72 65 63 75 72 73 69  , and.** recursi
33369 76 65 6c 79 20 61 73 6b 20 66 6f 72 20 74 68 65  vely ask for the
3336a 69 72 20 72 6f 6f 74 20 69 6e 74 6f 2e 0a 2a 2f  ir root into..*/
3336b 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65  .static int inte
3336c 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e  riorWriterRootIn
3336d 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  fo(fulltext_vtab
3336e 20 2a 76 2c 20 49 6e 74 65 72 69 6f 72 57 72 69   *v, InteriorWri
3336f 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
33370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33371 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33372 63 68 61 72 20 2a 2a 70 70 52 6f 6f 74 49 6e 66  char **ppRootInf
33373 6f 2c 20 69 6e 74 20 2a 70 6e 52 6f 6f 74 49 6e  o, int *pnRootIn
33374 66 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  fo,.            
33375 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33376 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
33377 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  64 *piEndBlockid
33378 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f  ){.  InteriorBlo
33379 63 6b 20 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69  ck *block = pWri
3337a 74 65 72 2d 3e 66 69 72 73 74 3b 0a 20 20 73 71  ter->first;.  sq
3337b 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63  lite_int64 iBloc
3337c 6b 69 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  kid = 0;.  int r
3337d 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  c;..  /* If we c
3337e 61 6e 20 66 69 74 20 74 68 65 20 73 65 67 6d 65  an fit the segme
3337f 6e 74 20 69 6e 6c 69 6e 65 20 2a 2f 0a 20 20 69  nt inline */.  i
33380 66 28 20 62 6c 6f 63 6b 3d 3d 70 57 72 69 74 65  f( block==pWrite
33381 72 2d 3e 6c 61 73 74 20 26 26 20 62 6c 6f 63 6b  r->last && block
33382 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f  ->data.nData<ROO
33383 54 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2a 70 70  T_MAX ){.    *pp
33384 52 6f 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b  RootInfo = block
33385 2d 3e 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20  ->data.pData;.  
33386 20 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20    *pnRootInfo = 
33387 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74  block->data.nDat
33388 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  a;.    return SQ
33389 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
3338a 2f 2a 20 46 6c 75 73 68 20 74 68 65 20 66 69 72  /* Flush the fir
3338b 73 74 20 62 6c 6f 63 6b 20 74 6f 20 25 5f 73 65  st block to %_se
3338c 67 6d 65 6e 74 73 2c 20 61 6e 64 20 63 72 65 61  gments, and crea
3338d 74 65 20 61 20 6e 65 77 20 6c 65 76 65 6c 20 6f  te a new level o
3338e 66 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20  f.  ** interior 
3338f 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 41 53 53  node..  */.  ASS
33390 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
33391 4f 52 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b  OR_BLOCK(block);
33392 0a 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e  .  rc = block_in
33393 73 65 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64  sert(v, block->d
33394 61 74 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b  ata.pData, block
33395 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69  ->data.nData, &i
33396 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20  Blockid);.  if( 
33397 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
33398 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 69  return rc;.  *pi
33399 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c  EndBlockid = iBl
3339a 6f 63 6b 69 64 3b 0a 0a 20 20 70 57 72 69 74 65  ockid;..  pWrite
3339b 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 20  r->parentWriter 
3339c 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
3339d 28 73 69 7a 65 6f 66 28 2a 70 57 72 69 74 65 72  (sizeof(*pWriter
3339e 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 29  ->parentWriter))
3339f 3b 0a 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74  ;.  interiorWrit
333a0 65 72 49 6e 69 74 28 70 57 72 69 74 65 72 2d 3e  erInit(pWriter->
333a1 69 48 65 69 67 68 74 2b 31 2c 0a 20 20 20 20 20  iHeight+1,.     
333a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333a3 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74  block->term.pDat
333a4 61 2c 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e  a, block->term.n
333a5 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
333a6 20 20 20 20 20 20 20 20 20 20 20 69 42 6c 6f 63             iBloc
333a7 6b 69 64 2c 20 70 57 72 69 74 65 72 2d 3e 70 61  kid, pWriter->pa
333a8 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 0a 20 20  rentWriter);..  
333a9 2f 2a 20 46 6c 75 73 68 20 61 64 64 69 74 69 6f  /* Flush additio
333aa 6e 61 6c 20 62 6c 6f 63 6b 73 20 61 6e 64 20 61  nal blocks and a
333ab 70 70 65 6e 64 20 74 6f 20 74 68 65 20 68 69 67  ppend to the hig
333ac 68 65 72 20 69 6e 74 65 72 69 6f 72 0a 20 20 2a  her interior.  *
333ad 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 66  * node..  */.  f
333ae 6f 72 28 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e  or(block=block->
333af 6e 65 78 74 3b 20 62 6c 6f 63 6b 21 3d 4e 55 4c  next; block!=NUL
333b0 4c 3b 20 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e  L; block=block->
333b1 6e 65 78 74 29 7b 0a 20 20 20 20 41 53 53 45 52  next){.    ASSER
333b2 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52  T_VALID_INTERIOR
333b3 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20  _BLOCK(block);. 
333b4 20 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e     rc = block_in
333b5 73 65 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64  sert(v, block->d
333b6 61 74 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b  ata.pData, block
333b7 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69  ->data.nData, &i
333b8 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66  Blockid);.    if
333b9 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
333ba 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
333bb 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d   *piEndBlockid =
333bc 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 20 20   iBlockid;..    
333bd 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 41 70  interiorWriterAp
333be 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 70 61  pend(pWriter->pa
333bf 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20  rentWriter,.    
333c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333c1 20 20 20 20 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d       block->term
333c2 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 74  .pData, block->t
333c3 65 72 6d 2e 6e 44 61 74 61 2c 20 69 42 6c 6f 63  erm.nData, iBloc
333c4 6b 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  kid);.  }..  /* 
333c5 50 61 72 65 6e 74 20 6e 6f 64 65 20 67 65 74 73  Parent node gets
333c6 20 74 68 65 20 63 68 61 6e 63 65 20 74 6f 20 62   the chance to b
333c7 65 20 74 68 65 20 72 6f 6f 74 2e 20 2a 2f 0a 20  e the root. */. 
333c8 20 72 65 74 75 72 6e 20 69 6e 74 65 72 69 6f 72   return interior
333c9 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76  WriterRootInfo(v
333ca 2c 20 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e  , pWriter->paren
333cb 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  tWriter,.       
333cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333cd 20 20 20 20 20 20 20 20 20 70 70 52 6f 6f 74 49           ppRootI
333ce 6e 66 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66 6f 2c  nfo, pnRootInfo,
333cf 20 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a   piEndBlockid);.
333d0 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
333d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333d4 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e 74 65 72 69 6f  ****/./* Interio
333d5 72 52 65 61 64 65 72 20 69 73 20 75 73 65 64 20  rReader is used 
333d6 74 6f 20 72 65 61 64 20 6f 66 66 20 74 68 65 20  to read off the 
333d7 64 61 74 61 20 66 72 6f 6d 20 61 6e 20 69 6e 74  data from an int
333d8 65 72 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 28 73  erior node.** (s
333d9 65 65 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74 6f  ee comment at to
333da 70 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 74 68  p of file for th
333db 65 20 66 6f 72 6d 61 74 29 2e 0a 2a 2f 0a 74 79  e format)..*/.ty
333dc 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 74  pedef struct Int
333dd 65 72 69 6f 72 52 65 61 64 65 72 20 7b 0a 20 20  eriorReader {.  
333de 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
333df 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  a;.  int nData;.
333e0 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65  .  DataBuffer te
333e1 72 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  rm;          /* 
333e2 70 72 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 66  previous term, f
333e3 6f 72 20 64 65 63 6f 64 69 6e 67 20 74 65 72 6d  or decoding term
333e4 20 64 65 6c 74 61 2e 20 2a 2f 0a 0a 20 20 73 71   delta. */..  sq
333e5 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63  lite_int64 iBloc
333e6 6b 69 64 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 52  kid;.} InteriorR
333e7 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 76  eader;..static v
333e8 6f 69 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64  oid interiorRead
333e9 65 72 44 65 73 74 72 6f 79 28 49 6e 74 65 72 69  erDestroy(Interi
333ea 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
333eb 72 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72  r){.  dataBuffer
333ec 44 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72  Destroy(&pReader
333ed 2d 3e 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d  ->term);.  SCRAM
333ee 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a  BLE(pReader);.}.
333ef 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
333f0 54 68 65 20 61 73 73 65 72 74 69 6f 6e 73 20 61  The assertions a
333f1 72 65 20 67 72 65 61 74 2c 20 62 75 74 20 77 68  re great, but wh
333f2 61 74 20 69 66 20 77 65 27 72 65 20 69 6e 20 4e  at if we're in N
333f3 44 45 42 55 47 0a 2a 2a 20 61 6e 64 20 74 68 65  DEBUG.** and the
333f4 20 62 6c 6f 62 20 69 73 20 65 6d 70 74 79 20 6f   blob is empty o
333f5 72 20 6f 74 68 65 72 77 69 73 65 20 63 6f 6e 74  r otherwise cont
333f6 61 69 6e 73 20 73 75 73 70 65 63 74 20 64 61 74  ains suspect dat
333f7 61 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  a?.*/.static voi
333f8 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  d interiorReader
333f9 49 6e 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20  Init(const char 
333fa 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
333fb 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
333fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333fd 20 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72    InteriorReader
333fe 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e   *pReader){.  in
333ff 74 20 6e 2c 20 6e 54 65 72 6d 3b 0a 0a 20 20 2f  t n, nTerm;..  /
33400 2a 20 52 65 71 75 69 72 65 20 61 74 20 6c 65 61  * Require at lea
33401 73 74 20 74 68 65 20 6c 65 61 64 69 6e 67 20 66  st the leading f
33402 6c 61 67 20 62 79 74 65 20 2a 2f 0a 20 20 61 73  lag byte */.  as
33403 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b  sert( nData>0 );
33404 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
33405 5b 30 5d 21 3d 27 5c 30 27 20 29 3b 0a 0a 20 20  [0]!='\0' );..  
33406 43 4c 45 41 52 28 70 52 65 61 64 65 72 29 3b 0a  CLEAR(pReader);.
33407 0a 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65  .  /* Decode the
33408 20 62 61 73 65 20 62 6c 6f 63 6b 69 64 2c 20 61   base blockid, a
33409 6e 64 20 73 65 74 20 74 68 65 20 63 75 72 73 6f  nd set the curso
3340a 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 74  r to the first t
3340b 65 72 6d 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  erm. */.  n = ft
3340c 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61 74  s3GetVarint(pDat
3340d 61 2b 31 2c 20 26 70 52 65 61 64 65 72 2d 3e 69  a+1, &pReader->i
3340e 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65  Blockid);.  asse
3340f 72 74 28 20 31 2b 6e 3c 3d 6e 44 61 74 61 20 29  rt( 1+n<=nData )
33410 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61  ;.  pReader->pDa
33411 74 61 20 3d 20 70 44 61 74 61 2b 31 2b 6e 3b 0a  ta = pData+1+n;.
33412 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
33413 20 3d 20 6e 44 61 74 61 2d 28 31 2b 6e 29 3b 0a   = nData-(1+n);.
33414 0a 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 2d 63  .  /* A single-c
33415 68 69 6c 64 20 69 6e 74 65 72 69 6f 72 20 6e 6f  hild interior no
33416 64 65 20 28 73 75 63 68 20 61 73 20 77 68 65 6e  de (such as when
33417 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 77 61 73   a leaf node was
33418 20 74 6f 6f 0a 20 20 2a 2a 20 6c 61 72 67 65 20   too.  ** large 
33419 66 6f 72 20 74 68 65 20 73 65 67 6d 65 6e 74 20  for the segment 
3341a 64 69 72 65 63 74 6f 72 79 29 20 77 6f 6e 27 74  directory) won't
3341b 20 68 61 76 65 20 61 6e 79 20 74 65 72 6d 73 2e   have any terms.
3341c 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  .  ** Otherwise,
3341d 20 64 65 63 6f 64 65 20 74 68 65 20 66 69 72 73   decode the firs
3341e 74 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69  t term..  */.  i
3341f 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  f( pReader->nDat
33420 61 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61  a==0 ){.    data
33421 42 75 66 66 65 72 49 6e 69 74 28 26 70 52 65 61  BufferInit(&pRea
33422 64 65 72 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 20  der->term, 0);. 
33423 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 20 3d 20   }else{.    n = 
33424 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
33425 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
33426 26 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74  &nTerm);.    dat
33427 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 52 65  aBufferInit(&pRe
33428 61 64 65 72 2d 3e 74 65 72 6d 2c 20 6e 54 65 72  ader->term, nTer
33429 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  m);.    dataBuff
3342a 65 72 52 65 70 6c 61 63 65 28 26 70 52 65 61 64  erReplace(&pRead
3342b 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65  er->term, pReade
3342c 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e 54 65 72  r->pData+n, nTer
3342d 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  m);.    assert( 
3342e 6e 2b 6e 54 65 72 6d 3c 3d 70 52 65 61 64 65 72  n+nTerm<=pReader
3342f 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70  ->nData );.    p
33430 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d  Reader->pData +=
33431 20 6e 2b 6e 54 65 72 6d 3b 0a 20 20 20 20 70 52   n+nTerm;.    pR
33432 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20  eader->nData -= 
33433 6e 2b 6e 54 65 72 6d 3b 0a 20 20 7d 0a 7d 0a 0a  n+nTerm;.  }.}..
33434 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
33435 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 49  iorReaderAtEnd(I
33436 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
33437 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
33438 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
33439 6e 44 61 74 61 3d 3d 30 3b 0a 7d 0a 0a 73 74 61  nData==0;.}..sta
3343a 74 69 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  tic sqlite_int64
3343b 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
3343c 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 49 6e  urrentBlockid(In
3343d 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52  teriorReader *pR
3343e 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  eader){.  return
3343f 20 70 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b   pReader->iBlock
33440 69 64 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  id;.}..static in
33441 74 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  t interiorReader
33442 54 65 72 6d 42 79 74 65 73 28 49 6e 74 65 72 69  TermBytes(Interi
33443 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
33444 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69  r){.  assert( !i
33445 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45  nteriorReaderAtE
33446 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20  nd(pReader) );. 
33447 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
33448 3e 74 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73  >term.nData;.}.s
33449 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
3344a 20 2a 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72   *interiorReader
3344b 54 65 72 6d 28 49 6e 74 65 72 69 6f 72 52 65 61  Term(InteriorRea
3344c 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
3344d 20 61 73 73 65 72 74 28 20 21 69 6e 74 65 72 69   assert( !interi
3344e 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 70 52  orReaderAtEnd(pR
3344f 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
33450 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  rn pReader->term
33451 2e 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 53 74  .pData;.}../* St
33452 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68  ep forward to th
33453 65 20 6e 65 78 74 20 74 65 72 6d 20 69 6e 20 74  e next term in t
33454 68 65 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74  he node. */.stat
33455 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72  ic void interior
33456 52 65 61 64 65 72 53 74 65 70 28 49 6e 74 65 72  ReaderStep(Inter
33457 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64  iorReader *pRead
33458 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
33459 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74  interiorReaderAt
3345a 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
3345b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6c 61 73  .  /* If the las
3345c 74 20 74 65 72 6d 20 68 61 73 20 62 65 65 6e 20  t term has been 
3345d 72 65 61 64 2c 20 73 69 67 6e 61 6c 20 65 6f 66  read, signal eof
3345e 2c 20 65 6c 73 65 20 63 6f 6e 73 74 72 75 63 74  , else construct
3345f 20 74 68 65 0a 20 20 2a 2a 20 6e 65 78 74 20 74   the.  ** next t
33460 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  erm..  */.  if( 
33461 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d  pReader->nData==
33462 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
33463 66 65 72 52 65 73 65 74 28 26 70 52 65 61 64 65  ferReset(&pReade
33464 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73  r->term);.  }els
33465 65 7b 0a 20 20 20 20 69 6e 74 20 6e 2c 20 6e 50  e{.    int n, nP
33466 72 65 66 69 78 2c 20 6e 53 75 66 66 69 78 3b 0a  refix, nSuffix;.
33467 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74  .    n = fts3Get
33468 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
33469 2d 3e 70 44 61 74 61 2c 20 26 6e 50 72 65 66 69  ->pData, &nPrefi
3346a 78 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73  x);.    n += fts
3346b 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
3346c 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
3346d 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20 2f  nSuffix);..    /
3346e 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 63  * Truncate the c
3346f 75 72 72 65 6e 74 20 74 65 72 6d 20 61 6e 64 20  urrent term and 
33470 61 70 70 65 6e 64 20 73 75 66 66 69 78 20 64 61  append suffix da
33471 74 61 2e 20 2a 2f 0a 20 20 20 20 70 52 65 61 64  ta. */.    pRead
33472 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 3d  er->term.nData =
33473 20 6e 50 72 65 66 69 78 3b 0a 20 20 20 20 64 61   nPrefix;.    da
33474 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26  taBufferAppend(&
33475 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70  pReader->term, p
33476 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
33477 20 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20   nSuffix);..    
33478 61 73 73 65 72 74 28 20 6e 2b 6e 53 75 66 66 69  assert( n+nSuffi
33479 78 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  x<=pReader->nDat
3347a 61 20 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72  a );.    pReader
3347b 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75  ->pData += n+nSu
3347c 66 66 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65  ffix;.    pReade
3347d 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53  r->nData -= n+nS
3347e 75 66 66 69 78 3b 0a 20 20 7d 0a 20 20 70 52 65  uffix;.  }.  pRe
3347f 61 64 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 2b 2b  ader->iBlockid++
33480 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6d 70 61 72 65 20  ;.}../* Compare 
33481 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d  the current term
33482 20 74 6f 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d   to pTerm[nTerm]
33483 2c 20 72 65 74 75 72 6e 69 6e 67 20 73 74 72 63  , returning strc
33484 6d 70 2d 73 74 79 6c 65 0a 2a 2a 20 72 65 73 75  mp-style.** resu
33485 6c 74 73 2e 20 20 49 66 20 69 73 50 72 65 66 69  lts.  If isPrefi
33486 78 2c 20 65 71 75 61 6c 69 74 79 20 6d 65 61 6e  x, equality mean
33487 73 20 65 71 75 61 6c 20 74 68 72 6f 75 67 68 20  s equal through 
33488 6e 54 65 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a  nTerm bytes..*/.
33489 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
3348a 69 6f 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70  iorReaderTermCmp
3348b 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20  (InteriorReader 
3348c 2a 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20  *pReader,.      
3348d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3348e 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
3348f 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
33490 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
33491 72 65 66 69 78 29 7b 0a 20 20 63 6f 6e 73 74 20  refix){.  const 
33492 63 68 61 72 20 2a 70 52 65 61 64 65 72 54 65 72  char *pReaderTer
33493 6d 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64  m = interiorRead
33494 65 72 54 65 72 6d 28 70 52 65 61 64 65 72 29 3b  erTerm(pReader);
33495 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 54 65  .  int nReaderTe
33496 72 6d 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61  rm = interiorRea
33497 64 65 72 54 65 72 6d 42 79 74 65 73 28 70 52 65  derTermBytes(pRe
33498 61 64 65 72 29 3b 0a 20 20 69 6e 74 20 63 2c 20  ader);.  int c, 
33499 6e 20 3d 20 6e 52 65 61 64 65 72 54 65 72 6d 3c  n = nReaderTerm<
3349a 6e 54 65 72 6d 20 3f 20 6e 52 65 61 64 65 72 54  nTerm ? nReaderT
3349b 65 72 6d 20 3a 20 6e 54 65 72 6d 3b 0a 0a 20 20  erm : nTerm;..  
3349c 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  if( n==0 ){.    
3349d 69 66 28 20 6e 52 65 61 64 65 72 54 65 72 6d 3e  if( nReaderTerm>
3349e 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  0 ) return -1;. 
3349f 20 20 20 69 66 28 20 6e 54 65 72 6d 3e 30 20 29     if( nTerm>0 )
334a0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 72   return 1;.    r
334a1 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
334a2 63 20 3d 20 6d 65 6d 63 6d 70 28 70 52 65 61 64  c = memcmp(pRead
334a3 65 72 54 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e  erTerm, pTerm, n
334a4 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20  );.  if( c!=0 ) 
334a5 72 65 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20  return c;.  if( 
334a6 69 73 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e  isPrefix && n==n
334a7 54 65 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b  Term ) return 0;
334a8 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 61 64 65  .  return nReade
334a9 72 54 65 72 6d 20 2d 20 6e 54 65 72 6d 3b 0a 7d  rTerm - nTerm;.}
334aa 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
334ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334ae 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 66 57 72 69 74  ***/./* LeafWrit
334af 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  er is used to co
334b0 6c 6c 65 63 74 20 74 65 72 6d 73 20 61 6e 64 20  llect terms and 
334b1 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69  associated docli
334b2 73 74 20 64 61 74 61 0a 2a 2a 20 69 6e 74 6f 20  st data.** into 
334b3 6c 65 61 66 20 62 6c 6f 63 6b 73 20 69 6e 20 25  leaf blocks in %
334b4 5f 73 65 67 6d 65 6e 74 73 20 28 73 65 65 20 74  _segments (see t
334b5 6f 70 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 66  op of file for f
334b6 6f 72 6d 61 74 20 69 6e 66 6f 29 2e 0a 2a 2a 20  ormat info)..** 
334b7 45 78 70 65 63 74 65 64 20 75 73 61 67 65 20 69  Expected usage i
334b8 73 3a 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 57 72 69  s:.**.** LeafWri
334b9 74 65 72 20 77 72 69 74 65 72 3b 0a 2a 2a 20 6c  ter writer;.** l
334ba 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 30 2c  eafWriterInit(0,
334bb 20 30 2c 20 26 77 72 69 74 65 72 29 3b 0a 2a 2a   0, &writer);.**
334bc 20 77 68 69 6c 65 28 20 73 6f 72 74 65 64 5f 74   while( sorted_t
334bd 65 72 6d 73 5f 6c 65 66 74 5f 74 6f 5f 70 72 6f  erms_left_to_pro
334be 63 65 73 73 20 29 7b 0a 2a 2a 20 20 20 2f 2f 20  cess ){.**   // 
334bf 64 61 74 61 20 69 73 20 64 6f 63 6c 69 73 74 20  data is doclist 
334c0 64 61 74 61 20 66 6f 72 20 74 68 61 74 20 74 65  data for that te
334c1 72 6d 2e 0a 2a 2a 20 20 20 72 63 20 3d 20 6c 65  rm..**   rc = le
334c2 61 66 57 72 69 74 65 72 53 74 65 70 28 76 2c 20  afWriterStep(v, 
334c3 26 77 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20  &writer, pTerm, 
334c4 6e 54 65 72 6d 2c 20 70 44 61 74 61 2c 20 6e 44  nTerm, pData, nD
334c5 61 74 61 29 3b 0a 2a 2a 20 20 20 69 66 28 20 72  ata);.**   if( r
334c6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
334c7 6f 74 6f 20 65 72 72 3b 0a 2a 2a 20 7d 0a 2a 2a  oto err;.** }.**
334c8 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
334c9 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69  Finalize(v, &wri
334ca 74 65 72 29 3b 0a 2a 2a 65 72 72 3a 0a 2a 2a 20  ter);.**err:.** 
334cb 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f  leafWriterDestro
334cc 79 28 26 77 72 69 74 65 72 29 3b 0a 2a 2a 20 72  y(&writer);.** r
334cd 65 74 75 72 6e 20 72 63 3b 0a 2a 2a 0a 2a 2a 20  eturn rc;.**.** 
334ce 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28 29  leafWriterStep()
334cf 20 6d 61 79 20 77 72 69 74 65 20 61 20 63 6f 6c   may write a col
334d0 6c 65 63 74 65 64 20 6c 65 61 66 20 6f 75 74 20  lected leaf out 
334d1 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a  to %_segments..*
334d2 2a 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61  * leafWriterFina
334d3 6c 69 7a 65 28 29 20 66 69 6e 69 73 68 65 73 20  lize() finishes 
334d4 77 72 69 74 69 6e 67 20 61 6e 79 20 62 75 66 66  writing any buff
334d5 65 72 65 64 20 64 61 74 61 20 61 6e 64 20 73 74  ered data and st
334d6 6f 72 65 73 0a 2a 2a 20 61 20 72 6f 6f 74 20 6e  ores.** a root n
334d7 6f 64 65 20 69 6e 20 25 5f 73 65 67 64 69 72 2e  ode in %_segdir.
334d8 20 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74    leafWriterDest
334d9 72 6f 79 28 29 20 66 72 65 65 73 20 61 6c 6c 20  roy() frees all 
334da 62 75 66 66 65 72 73 20 61 6e 64 0a 2a 2a 20 49  buffers and.** I
334db 6e 74 65 72 69 6f 72 57 72 69 74 65 72 73 20 61  nteriorWriters a
334dc 6c 6c 6f 63 61 74 65 64 20 61 73 20 70 61 72 74  llocated as part
334dd 20 6f 66 20 77 72 69 74 69 6e 67 20 74 68 69 73   of writing this
334de 20 73 65 67 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   segment..**.** 
334df 54 4f 44 4f 28 73 68 65 73 73 29 20 44 6f 63 75  TODO(shess) Docu
334e0 6d 65 6e 74 20 6c 65 61 66 57 72 69 74 65 72 53  ment leafWriterS
334e1 74 65 70 4d 65 72 67 65 28 29 2e 0a 2a 2f 0a 0a  tepMerge()..*/..
334e2 2f 2a 20 50 75 74 20 74 65 72 6d 73 20 77 69 74  /* Put terms wit
334e3 68 20 64 61 74 61 20 74 68 69 73 20 62 69 67 20  h data this big 
334e4 69 6e 20 74 68 65 69 72 20 6f 77 6e 20 62 6c 6f  in their own blo
334e5 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ck. */.#define S
334e6 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 31 30  TANDALONE_MIN 10
334e7 32 34 0a 0a 2f 2a 20 4b 65 65 70 20 6c 65 61 66  24../* Keep leaf
334e8 20 62 6c 6f 63 6b 73 20 62 65 6c 6f 77 20 74 68   blocks below th
334e9 69 73 20 73 69 7a 65 2e 20 2a 2f 0a 23 64 65 66  is size. */.#def
334ea 69 6e 65 20 4c 45 41 46 5f 4d 41 58 20 32 30 34  ine LEAF_MAX 204
334eb 38 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  8..typedef struc
334ec 74 20 4c 65 61 66 57 72 69 74 65 72 20 7b 0a 20  t LeafWriter {. 
334ed 20 69 6e 74 20 69 4c 65 76 65 6c 3b 0a 20 20 69   int iLevel;.  i
334ee 6e 74 20 69 64 78 3b 0a 20 20 73 71 6c 69 74 65  nt idx;.  sqlite
334ef 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f  _int64 iStartBlo
334f0 63 6b 69 64 3b 20 20 20 20 20 2f 2a 20 6e 65 65  ckid;     /* nee
334f1 64 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68  ded to create th
334f2 65 20 72 6f 6f 74 20 69 6e 66 6f 20 2a 2f 0a 20  e root info */. 
334f3 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45   sqlite_int64 iE
334f4 6e 64 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20  ndBlockid;      
334f5 20 2f 2a 20 77 68 65 6e 20 77 65 27 72 65 20 64   /* when we're d
334f6 6f 6e 65 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a  one writing. */.
334f7 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65  .  DataBuffer te
334f8 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
334f9 20 20 20 2f 2a 20 70 72 65 76 69 6f 75 73 20 65     /* previous e
334fa 6e 63 6f 64 65 64 20 74 65 72 6d 20 2a 2f 0a 20  ncoded term */. 
334fb 20 44 61 74 61 42 75 66 66 65 72 20 64 61 74 61   DataBuffer data
334fc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
334fd 20 2f 2a 20 65 6e 63 6f 64 69 6e 67 20 62 75 66   /* encoding buf
334fe 66 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 62 79 74  fer */..  /* byt
334ff 65 73 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  es of first term
33500 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
33501 6e 6f 64 65 20 77 68 69 63 68 20 64 69 73 74 69  node which disti
33502 6e 67 75 69 73 68 65 73 20 74 68 61 74 0a 20 20  nguishes that.  
33503 2a 2a 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65  ** term from the
33504 20 6c 61 73 74 20 74 65 72 6d 20 6f 66 20 74 68   last term of th
33505 65 20 70 72 65 76 69 6f 75 73 20 6e 6f 64 65 2e  e previous node.
33506 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  .  */.  int nTer
33507 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 49 6e  mDistinct;..  In
33508 74 65 72 69 6f 72 57 72 69 74 65 72 20 70 61 72  teriorWriter par
33509 65 6e 74 57 72 69 74 65 72 3b 20 20 20 20 2f 2a  entWriter;    /*
3350a 20 69 66 20 77 65 20 6f 76 65 72 66 6c 6f 77 20   if we overflow 
3350b 2a 2f 0a 20 20 69 6e 74 20 68 61 73 5f 70 61 72  */.  int has_par
3350c 65 6e 74 3b 0a 7d 20 4c 65 61 66 57 72 69 74 65  ent;.} LeafWrite
3350d 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
3350e 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 69  leafWriterInit(i
3350f 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69  nt iLevel, int i
33510 64 78 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  dx, LeafWriter *
33511 70 57 72 69 74 65 72 29 7b 0a 20 20 43 4c 45 41  pWriter){.  CLEA
33512 52 28 70 57 72 69 74 65 72 29 3b 0a 20 20 70 57  R(pWriter);.  pW
33513 72 69 74 65 72 2d 3e 69 4c 65 76 65 6c 20 3d 20  riter->iLevel = 
33514 69 4c 65 76 65 6c 3b 0a 20 20 70 57 72 69 74 65  iLevel;.  pWrite
33515 72 2d 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20  r->idx = idx;.. 
33516 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
33517 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20  &pWriter->term, 
33518 33 32 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74  32);..  /* Start
33519 20 6f 75 74 20 77 69 74 68 20 61 20 72 65 61 73   out with a reas
3351a 6f 6e 61 62 6c 79 20 73 69 7a 65 64 20 62 6c 6f  onably sized blo
3351b 63 6b 2c 20 74 68 6f 75 67 68 20 69 74 20 63 61  ck, though it ca
3351c 6e 20 67 72 6f 77 2e 20 2a 2f 0a 20 20 64 61 74  n grow. */.  dat
3351d 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 57 72  aBufferInit(&pWr
3351e 69 74 65 72 2d 3e 64 61 74 61 2c 20 4c 45 41 46  iter->data, LEAF
3351f 5f 4d 41 58 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  _MAX);.}..#ifnde
33520 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69  f NDEBUG./* Veri
33521 66 79 20 74 68 61 74 20 74 68 65 20 64 61 74 61  fy that the data
33522 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 73 20   is readable as 
33523 61 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a  a leaf node. */.
33524 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66  static void leaf
33525 4e 6f 64 65 56 61 6c 69 64 61 74 65 28 63 6f 6e  NodeValidate(con
33526 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
33527 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 69 6e  int nData){.  in
33528 74 20 6e 2c 20 69 44 75 6d 6d 79 3b 0a 0a 20 20  t n, iDummy;..  
33529 69 66 28 20 6e 44 61 74 61 3d 3d 30 20 29 20 72  if( nData==0 ) r
3352a 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
3352b 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73   nData>0 );.  as
3352c 73 65 72 74 28 20 70 44 61 74 61 21 3d 30 20 29  sert( pData!=0 )
3352d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74  ;.  assert( pDat
3352e 61 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29 3b  a+nData>pData );
3352f 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64  ..  /* Must lead
33530 20 77 69 74 68 20 61 20 76 61 72 69 6e 74 28 30   with a varint(0
33531 29 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47  ) */.  n = fts3G
33532 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
33533 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73  , &iDummy);.  as
33534 73 65 72 74 28 20 69 44 75 6d 6d 79 3d 3d 30 20  sert( iDummy==0 
33535 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30  );.  assert( n>0
33536 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c   );.  assert( n<
33537 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61 74 61  nData );.  pData
33538 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d   += n;.  nData -
33539 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64 69  = n;..  /* Leadi
3353a 6e 67 20 74 65 72 6d 20 6c 65 6e 67 74 68 20 61  ng term length a
3353b 6e 64 20 64 61 74 61 20 6d 75 73 74 20 66 69 74  nd data must fit
3353c 20 69 6e 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20   in buffer. */. 
3353d 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
3353e 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
3353f 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
33540 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  n>0 );.  assert(
33541 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61   iDummy>0 );.  a
33542 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e  ssert( n+iDummy>
33543 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
33544 2b 69 44 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b  +iDummy<nData );
33545 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44  .  pData += n+iD
33546 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d  ummy;.  nData -=
33547 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a   n+iDummy;..  /*
33548 20 4c 65 61 64 69 6e 67 20 74 65 72 6d 27 73 20   Leading term's 
33549 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74 68 20 61  doclist length a
3354a 6e 64 20 64 61 74 61 20 6d 75 73 74 20 66 69 74  nd data must fit
3354b 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47  . */.  n = fts3G
3354c 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
3354d 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73  , &iDummy);.  as
3354e 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61  sert( n>0 );.  a
3354f 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20  ssert( iDummy>0 
33550 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69  );.  assert( n+i
33551 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73  Dummy>0 );.  ass
33552 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
33553 44 61 74 61 20 29 3b 0a 20 20 41 53 53 45 52 54  Data );.  ASSERT
33554 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44  _VALID_DOCLIST(D
33555 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61  L_DEFAULT, pData
33556 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c  +n, iDummy, NULL
33557 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b  );.  pData += n+
33558 69 44 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20  iDummy;.  nData 
33559 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20  -= n+iDummy;..  
3355a 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
3355b 72 61 69 6c 69 6e 67 20 74 65 72 6d 73 20 61 6e  railing terms an
3355c 64 20 64 6f 63 6c 69 73 74 73 20 61 6c 73 6f 20  d doclists also 
3355d 61 72 65 20 72 65 61 64 61 62 6c 65 2e 20 2a 2f  are readable. */
3355e 0a 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21  .  while( nData!
3355f 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74  =0 ){.    n = ft
33560 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
33561 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  ata, &iDummy);. 
33562 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29     assert( n>0 )
33563 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  ;.    assert( iD
33564 75 6d 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 61  ummy>=0 );.    a
33565 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29  ssert( n<nData )
33566 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
33567 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e  ;.    nData -= n
33568 3b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65  ;.    n = fts3Ge
33569 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
3356a 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61   &iDummy);.    a
3356b 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
3356c 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
3356d 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
3356e 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  ( n+iDummy>0 );.
3356f 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44      assert( n+iD
33570 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20 20  ummy<nData );.  
33571 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75    pData += n+iDu
33572 6d 6d 79 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d  mmy;.    nData -
33573 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 20  = n+iDummy;..   
33574 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
33575 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
33576 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74  mmy);.    assert
33577 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  ( n>0 );.    ass
33578 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
33579 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69  .    assert( n+i
3357a 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61  Dummy>0 );.    a
3357b 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
3357c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 41 53  =nData );.    AS
3357d 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49  SERT_VALID_DOCLI
3357e 53 54 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  ST(DL_DEFAULT, p
3357f 44 61 74 61 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20  Data+n, iDummy, 
33580 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 44 61 74 61  NULL);.    pData
33581 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20   += n+iDummy;.  
33582 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75    nData -= n+iDu
33583 6d 6d 79 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69  mmy;.  }.}.#defi
33584 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
33585 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20  LEAF_NODE(p, n) 
33586 6c 65 61 66 4e 6f 64 65 56 61 6c 69 64 61 74 65  leafNodeValidate
33587 28 70 2c 20 6e 29 0a 23 65 6c 73 65 0a 23 64 65  (p, n).#else.#de
33588 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49  fine ASSERT_VALI
33589 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e  D_LEAF_NODE(p, n
3358a 29 20 61 73 73 65 72 74 28 20 31 20 29 0a 23 65  ) assert( 1 ).#e
3358b 6e 64 69 66 0a 0a 2f 2a 20 46 6c 75 73 68 20 74  ndif../* Flush t
3358c 68 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 20  he current leaf 
3358d 6e 6f 64 65 20 74 6f 20 25 5f 73 65 67 6d 65 6e  node to %_segmen
3358e 74 73 2c 20 61 6e 64 20 61 64 64 69 6e 67 20 74  ts, and adding t
3358f 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20  he resulting.** 
33590 62 6c 6f 63 6b 69 64 20 61 6e 64 20 74 68 65 20  blockid and the 
33591 73 74 61 72 74 69 6e 67 20 74 65 72 6d 20 74 6f  starting term to
33592 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f   the interior no
33593 64 65 20 77 68 69 63 68 20 77 69 6c 6c 0a 2a 2a  de which will.**
33594 20 63 6f 6e 74 61 69 6e 20 69 74 2e 0a 2a 2f 0a   contain it..*/.
33595 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
33596 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75  riterInternalFlu
33597 73 68 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  sh(fulltext_vtab
33598 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20   *v, LeafWriter 
33599 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  *pWriter,.      
3359a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3359b 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
3359c 20 69 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74   iData, int nDat
3359d 61 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  a){.  sqlite_int
3359e 36 34 20 69 42 6c 6f 63 6b 69 64 20 3d 20 30 3b  64 iBlockid = 0;
3359f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
335a0 53 74 61 72 74 69 6e 67 54 65 72 6d 3b 0a 20 20  StartingTerm;.  
335a1 69 6e 74 20 6e 53 74 61 72 74 69 6e 67 54 65 72  int nStartingTer
335a2 6d 2c 20 72 63 2c 20 6e 3b 0a 0a 20 20 2f 2a 20  m, rc, n;..  /* 
335a3 4d 75 73 74 20 68 61 76 65 20 74 68 65 20 6c 65  Must have the le
335a4 61 64 69 6e 67 20 76 61 72 69 6e 74 28 30 29 20  ading varint(0) 
335a5 66 6c 61 67 2c 20 70 6c 75 73 20 61 74 20 6c 65  flag, plus at le
335a6 61 73 74 20 73 6f 6d 65 0a 20 20 2a 2a 20 76 61  ast some.  ** va
335a7 6c 69 64 2d 6c 6f 6f 6b 69 6e 67 20 64 61 74 61  lid-looking data
335a8 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
335a9 20 6e 44 61 74 61 3e 32 20 29 3b 0a 20 20 61 73   nData>2 );.  as
335aa 73 65 72 74 28 20 69 44 61 74 61 3e 3d 30 20 29  sert( iData>=0 )
335ab 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 61 74  ;.  assert( iDat
335ac 61 2b 6e 44 61 74 61 3c 3d 70 57 72 69 74 65 72  a+nData<=pWriter
335ad 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20 29 3b 0a  ->data.nData );.
335ae 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c    ASSERT_VALID_L
335af 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72  EAF_NODE(pWriter
335b0 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61  ->data.pData+iDa
335b1 74 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20 72  ta, nData);..  r
335b2 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74  c = block_insert
335b3 28 76 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74  (v, pWriter->dat
335b4 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e  a.pData+iData, n
335b5 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29  Data, &iBlockid)
335b6 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
335b7 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
335b8 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c  c;.  assert( iBl
335b9 6f 63 6b 69 64 21 3d 30 20 29 3b 0a 0a 20 20 2f  ockid!=0 );..  /
335ba 2a 20 52 65 63 6f 6e 73 74 72 75 63 74 20 74 68  * Reconstruct th
335bb 65 20 66 69 72 73 74 20 74 65 72 6d 20 69 6e 20  e first term in 
335bc 74 68 65 20 6c 65 61 66 20 66 6f 72 20 70 75 72  the leaf for pur
335bd 70 6f 73 65 73 20 6f 66 20 62 75 69 6c 64 69 6e  poses of buildin
335be 67 0a 20 20 2a 2a 20 74 68 65 20 69 6e 74 65 72  g.  ** the inter
335bf 69 6f 72 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ior node..  */. 
335c0 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
335c1 6e 74 33 32 28 70 57 72 69 74 65 72 2d 3e 64 61  nt32(pWriter->da
335c2 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31  ta.pData+iData+1
335c3 2c 20 26 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  , &nStartingTerm
335c4 29 3b 0a 20 20 70 53 74 61 72 74 69 6e 67 54 65  );.  pStartingTe
335c5 72 6d 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61  rm = pWriter->da
335c6 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31  ta.pData+iData+1
335c7 2b 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57  +n;.  assert( pW
335c8 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
335c9 61 3e 69 44 61 74 61 2b 31 2b 6e 2b 6e 53 74 61  a>iData+1+n+nSta
335ca 72 74 69 6e 67 54 65 72 6d 20 29 3b 0a 20 20 61  rtingTerm );.  a
335cb 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
335cc 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3e 30 20  nTermDistinct>0 
335cd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72  );.  assert( pWr
335ce 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69  iter->nTermDisti
335cf 6e 63 74 3c 3d 6e 53 74 61 72 74 69 6e 67 54 65  nct<=nStartingTe
335d0 72 6d 20 29 3b 0a 20 20 6e 53 74 61 72 74 69 6e  rm );.  nStartin
335d1 67 54 65 72 6d 20 3d 20 70 57 72 69 74 65 72 2d  gTerm = pWriter-
335d2 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a  >nTermDistinct;.
335d3 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
335d4 68 61 73 5f 70 61 72 65 6e 74 20 29 7b 0a 20 20  has_parent ){.  
335d5 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72    interiorWriter
335d6 41 70 70 65 6e 64 28 26 70 57 72 69 74 65 72 2d  Append(&pWriter-
335d7 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20  >parentWriter,. 
335d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335d9 20 20 20 20 20 20 20 20 70 53 74 61 72 74 69 6e          pStartin
335da 67 54 65 72 6d 2c 20 6e 53 74 61 72 74 69 6e 67  gTerm, nStarting
335db 54 65 72 6d 2c 20 69 42 6c 6f 63 6b 69 64 29 3b  Term, iBlockid);
335dc 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
335dd 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74  teriorWriterInit
335de 28 31 2c 20 70 53 74 61 72 74 69 6e 67 54 65 72  (1, pStartingTer
335df 6d 2c 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  m, nStartingTerm
335e0 2c 20 69 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  , iBlockid,.    
335e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335e2 20 20 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72     &pWriter->par
335e3 65 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20  entWriter);.    
335e4 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72  pWriter->has_par
335e5 65 6e 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  ent = 1;.  }..  
335e6 2f 2a 20 54 72 61 63 6b 20 74 68 65 20 73 70 61  /* Track the spa
335e7 6e 20 6f 66 20 74 68 69 73 20 73 65 67 6d 65 6e  n of this segmen
335e8 74 27 73 20 6c 65 61 66 20 6e 6f 64 65 73 2e 20  t's leaf nodes. 
335e9 2a 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72  */.  if( pWriter
335ea 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 3d 3d 30  ->iEndBlockid==0
335eb 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d   ){.    pWriter-
335ec 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70  >iEndBlockid = p
335ed 57 72 69 74 65 72 2d 3e 69 53 74 61 72 74 42 6c  Writer->iStartBl
335ee 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64  ockid = iBlockid
335ef 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
335f0 57 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63  Writer->iEndBloc
335f1 6b 69 64 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72  kid++;.    asser
335f2 74 28 20 69 42 6c 6f 63 6b 69 64 3d 3d 70 57 72  t( iBlockid==pWr
335f3 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
335f4 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  d );.  }..  retu
335f5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
335f6 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
335f7 72 69 74 65 72 46 6c 75 73 68 28 66 75 6c 6c 74  riterFlush(fullt
335f8 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
335f9 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
335fa 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 6c 65  ){.  int rc = le
335fb 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c  afWriterInternal
335fc 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72  Flush(v, pWriter
335fd 2c 20 30 2c 20 70 57 72 69 74 65 72 2d 3e 64 61  , 0, pWriter->da
335fe 74 61 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66 28  ta.nData);.  if(
335ff 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
33600 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
33601 2a 20 52 65 2d 69 6e 69 74 69 61 6c 69 7a 65 20  * Re-initialize 
33602 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
33603 72 2e 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66  r. */.  dataBuff
33604 65 72 52 65 73 65 74 28 26 70 57 72 69 74 65 72  erReset(&pWriter
33605 2d 3e 64 61 74 61 29 3b 0a 0a 20 20 72 65 74 75  ->data);..  retu
33606 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
33607 0a 2f 2a 20 46 65 74 63 68 20 74 68 65 20 72 6f  ./* Fetch the ro
33608 6f 74 20 69 6e 66 6f 20 66 6f 72 20 74 68 65 20  ot info for the 
33609 73 65 67 6d 65 6e 74 2e 20 20 49 66 20 74 68 65  segment.  If the
3360a 20 65 6e 74 69 72 65 20 6c 65 61 66 20 66 69 74   entire leaf fit
3360b 73 0a 2a 2a 20 77 69 74 68 69 6e 20 52 4f 4f 54  s.** within ROOT
3360c 5f 4d 41 58 2c 20 74 68 65 6e 20 69 74 20 77 69  _MAX, then it wi
3360d 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 64  ll be returned d
3360e 69 72 65 63 74 6c 79 2c 20 6f 74 68 65 72 77 69  irectly, otherwi
3360f 73 65 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 62 65  se it.** will be
33610 20 66 6c 75 73 68 65 64 20 61 6e 64 20 74 68 65   flushed and the
33611 20 72 6f 6f 74 20 69 6e 66 6f 20 77 69 6c 6c 20   root info will 
33612 62 65 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  be returned from
33613 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 69 6f 72   the.** interior
33614 20 6e 6f 64 65 2e 20 20 2a 70 69 45 6e 64 42 6c   node.  *piEndBl
33615 6f 63 6b 69 64 20 69 73 20 73 65 74 20 74 6f 20  ockid is set to 
33616 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f 66 20 74  the blockid of t
33617 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 74 65 72  he last.** inter
33618 69 6f 72 20 6f 72 20 6c 65 61 66 20 6e 6f 64 65  ior or leaf node
33619 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b   written to disk
3361a 20 28 30 20 69 66 20 6e 6f 6e 65 20 61 72 65 20   (0 if none are 
3361b 77 72 69 74 74 65 6e 20 61 74 0a 2a 2a 20 61 6c  written at.** al
3361c 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  l)..*/.static in
3361d 74 20 6c 65 61 66 57 72 69 74 65 72 52 6f 6f 74  t leafWriterRoot
3361e 49 6e 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74  Info(fulltext_vt
3361f 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65  ab *v, LeafWrite
33620 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
33621 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33622 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
33623 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e 74  *ppRootInfo, int
33624 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20 20   *pnRootInfo,.  
33625 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33626 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
33627 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42  te_int64 *piEndB
33628 6c 6f 63 6b 69 64 29 7b 0a 20 20 2f 2a 20 77 65  lockid){.  /* we
33629 20 63 61 6e 20 66 69 74 20 74 68 65 20 73 65 67   can fit the seg
3362a 6d 65 6e 74 20 65 6e 74 69 72 65 6c 79 20 69 6e  ment entirely in
3362b 6c 69 6e 65 20 2a 2f 0a 20 20 69 66 28 20 21 70  line */.  if( !p
3362c 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65  Writer->has_pare
3362d 6e 74 20 26 26 20 70 57 72 69 74 65 72 2d 3e 64  nt && pWriter->d
3362e 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d  ata.nData<ROOT_M
3362f 41 58 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f 6f  AX ){.    *ppRoo
33630 74 49 6e 66 6f 20 3d 20 70 57 72 69 74 65 72 2d  tInfo = pWriter-
33631 3e 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 20  >data.pData;.   
33632 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20 70   *pnRootInfo = p
33633 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
33634 74 61 3b 0a 20 20 20 20 2a 70 69 45 6e 64 42 6c  ta;.    *piEndBl
33635 6f 63 6b 69 64 20 3d 20 30 3b 0a 20 20 20 20 72  ockid = 0;.    r
33636 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
33637 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6c 75 73 68  .  }..  /* Flush
33638 20 72 65 6d 61 69 6e 69 6e 67 20 6c 65 61 66 20   remaining leaf 
33639 64 61 74 61 2e 20 2a 2f 0a 20 20 69 66 28 20 70  data. */.  if( p
3363a 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
3363b 74 61 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  ta>0 ){.    int 
3363c 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46  rc = leafWriterF
3363d 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 29  lush(v, pWriter)
3363e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
3363f 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
33640 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57   rc;.  }..  /* W
33641 65 20 6d 75 73 74 20 68 61 76 65 20 66 6c 75 73  e must have flus
33642 68 65 64 20 61 20 6c 65 61 66 20 61 74 20 73 6f  hed a leaf at so
33643 6d 65 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 61  me point. */.  a
33644 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
33645 68 61 73 5f 70 61 72 65 6e 74 20 29 3b 0a 0a 20  has_parent );.. 
33646 20 2f 2a 20 54 65 6e 61 74 69 76 65 6c 79 20 73   /* Tenatively s
33647 65 74 20 74 68 65 20 65 6e 64 20 6c 65 61 66 20  et the end leaf 
33648 62 6c 6f 63 6b 69 64 20 61 73 20 74 68 65 20 65  blockid as the e
33649 6e 64 20 62 6c 6f 63 6b 69 64 2e 20 20 49 66 20  nd blockid.  If 
3364a 74 68 65 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f  the.  ** interio
3364b 72 20 6e 6f 64 65 20 63 61 6e 20 62 65 20 72 65  r node can be re
3364c 74 75 72 6e 65 64 20 69 6e 6c 69 6e 65 2c 20 74  turned inline, t
3364d 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20  his will be the 
3364e 66 69 6e 61 6c 0a 20 20 2a 2a 20 62 6c 6f 63 6b  final.  ** block
3364f 69 64 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74  id, otherwise it
33650 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69   will be overwri
33651 74 74 65 6e 20 62 79 0a 20 20 2a 2a 20 69 6e 74  tten by.  ** int
33652 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49  eriorWriterRootI
33653 6e 66 6f 28 29 2e 0a 20 20 2a 2f 0a 20 20 2a 70  nfo()..  */.  *p
33654 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70 57  iEndBlockid = pW
33655 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b  riter->iEndBlock
33656 69 64 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e  id;..  return in
33657 74 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74  teriorWriterRoot
33658 49 6e 66 6f 28 76 2c 20 26 70 57 72 69 74 65 72  Info(v, &pWriter
33659 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a  ->parentWriter,.
3365a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3365b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3365c 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 70 6e 52 6f  ppRootInfo, pnRo
3365d 6f 74 49 6e 66 6f 2c 20 70 69 45 6e 64 42 6c 6f  otInfo, piEndBlo
3365e 63 6b 69 64 29 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6c  ckid);.}../* Col
3365f 6c 65 63 74 20 74 68 65 20 72 6f 6f 74 49 6e 66  lect the rootInf
33660 6f 20 64 61 74 61 20 61 6e 64 20 73 74 6f 72 65  o data and store
33661 20 69 74 20 69 6e 74 6f 20 74 68 65 20 73 65 67   it into the seg
33662 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 2e 0a  ment directory..
33663 2a 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20  ** This has the 
33664 65 66 66 65 63 74 20 6f 66 20 66 6c 75 73 68 69  effect of flushi
33665 6e 67 20 74 68 65 20 73 65 67 6d 65 6e 74 27 73  ng the segment's
33666 20 6c 65 61 66 20 64 61 74 61 20 74 6f 0a 2a 2a   leaf data to.**
33667 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64   %_segments, and
33668 20 61 6c 73 6f 20 66 6c 75 73 68 69 6e 67 20 61   also flushing a
33669 6e 79 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ny interior node
3366a 73 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e  s to %_segments.
3366b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
3366c 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a  eafWriterFinaliz
3366d 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
3366e 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  *v, LeafWriter *
3366f 70 57 72 69 74 65 72 29 7b 0a 20 20 73 71 6c 69  pWriter){.  sqli
33670 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f  te_int64 iEndBlo
33671 63 6b 69 64 3b 0a 20 20 63 68 61 72 20 2a 70 52  ckid;.  char *pR
33672 6f 6f 74 49 6e 66 6f 3b 0a 20 20 69 6e 74 20 72  ootInfo;.  int r
33673 63 2c 20 6e 52 6f 6f 74 49 6e 66 6f 3b 0a 0a 20  c, nRootInfo;.. 
33674 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
33675 52 6f 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72 69  RootInfo(v, pWri
33676 74 65 72 2c 20 26 70 52 6f 6f 74 49 6e 66 6f 2c  ter, &pRootInfo,
33677 20 26 6e 52 6f 6f 74 49 6e 66 6f 2c 20 26 69 45   &nRootInfo, &iE
33678 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66  ndBlockid);.  if
33679 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3367a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
3367b 2f 2a 20 44 6f 6e 27 74 20 62 6f 74 68 65 72 20  /* Don't bother 
3367c 73 74 6f 72 69 6e 67 20 61 6e 20 65 6e 74 69 72  storing an entir
3367d 65 6c 79 20 65 6d 70 74 79 20 73 65 67 6d 65 6e  ely empty segmen
3367e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 69 45 6e 64  t. */.  if( iEnd
3367f 42 6c 6f 63 6b 69 64 3d 3d 30 20 26 26 20 6e 52  Blockid==0 && nR
33680 6f 6f 74 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74  ootInfo==0 ) ret
33681 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
33682 20 20 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f    return segdir_
33683 73 65 74 28 76 2c 20 70 57 72 69 74 65 72 2d 3e  set(v, pWriter->
33684 69 4c 65 76 65 6c 2c 20 70 57 72 69 74 65 72 2d  iLevel, pWriter-
33685 3e 69 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20  >idx,.          
33686 20 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65            pWrite
33687 72 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  r->iStartBlockid
33688 2c 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42  , pWriter->iEndB
33689 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20  lockid,.        
3368a 20 20 20 20 20 20 20 20 20 20 20 20 69 45 6e 64              iEnd
3368b 42 6c 6f 63 6b 69 64 2c 20 70 52 6f 6f 74 49 6e  Blockid, pRootIn
3368c 66 6f 2c 20 6e 52 6f 6f 74 49 6e 66 6f 29 3b 0a  fo, nRootInfo);.
3368d 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  }..static void l
3368e 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79  eafWriterDestroy
3368f 28 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72  (LeafWriter *pWr
33690 69 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57 72  iter){.  if( pWr
33691 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74  iter->has_parent
33692 20 29 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65   ) interiorWrite
33693 72 44 65 73 74 72 6f 79 28 26 70 57 72 69 74 65  rDestroy(&pWrite
33694 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29  r->parentWriter)
33695 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
33696 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e  stroy(&pWriter->
33697 74 65 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66  term);.  dataBuf
33698 66 65 72 44 65 73 74 72 6f 79 28 26 70 57 72 69  ferDestroy(&pWri
33699 74 65 72 2d 3e 64 61 74 61 29 3b 0a 7d 0a 0a 2f  ter->data);.}../
3369a 2a 20 45 6e 63 6f 64 65 20 61 20 74 65 72 6d 20  * Encode a term 
3369b 69 6e 74 6f 20 74 68 65 20 6c 65 61 66 57 72 69  into the leafWri
3369c 74 65 72 2c 20 64 65 6c 74 61 2d 65 6e 63 6f 64  ter, delta-encod
3369d 69 6e 67 20 61 73 20 61 70 70 72 6f 70 72 69 61  ing as appropria
3369e 74 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74  te..** Returns t
3369f 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
336a0 20 6e 65 77 20 74 65 72 6d 20 77 68 69 63 68 20   new term which 
336a1 64 69 73 74 69 6e 67 75 69 73 68 65 73 20 69 74  distinguishes it
336a2 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 70 72 65   from the.** pre
336a3 76 69 6f 75 73 20 74 65 72 6d 2c 20 77 68 69 63  vious term, whic
336a4 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  h can be used to
336a5 20 73 65 74 20 6e 54 65 72 6d 44 69 73 74 69 6e   set nTermDistin
336a6 63 74 20 77 68 65 6e 20 61 20 6e 6f 64 65 0a 2a  ct when a node.*
336a7 2a 20 62 6f 75 6e 64 61 72 79 20 69 73 20 63 72  * boundary is cr
336a8 6f 73 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ossed..*/.static
336a9 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 45   int leafWriterE
336aa 6e 63 6f 64 65 54 65 72 6d 28 4c 65 61 66 57 72  ncodeTerm(LeafWr
336ab 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20  iter *pWriter,. 
336ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
336ae 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
336af 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20  , int nTerm){.  
336b0 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
336b1 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
336b2 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20   int n, nPrefix 
336b3 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
336b4 6e 54 65 72 6d 3e 30 20 29 3b 0a 20 20 77 68 69  nTerm>0 );.  whi
336b5 6c 65 28 20 6e 50 72 65 66 69 78 3c 70 57 72 69  le( nPrefix<pWri
336b6 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20  ter->term.nData 
336b7 26 26 0a 20 20 20 20 20 20 20 20 20 70 54 65 72  &&.         pTer
336b8 6d 5b 6e 50 72 65 66 69 78 5d 3d 3d 70 57 72 69  m[nPrefix]==pWri
336b9 74 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 5b  ter->term.pData[
336ba 6e 50 72 65 66 69 78 5d 20 29 7b 0a 20 20 20 20  nPrefix] ){.    
336bb 6e 50 72 65 66 69 78 2b 2b 3b 0a 20 20 20 20 2f  nPrefix++;.    /
336bc 2a 20 46 61 69 6c 69 6e 67 20 74 68 69 73 20 69  * Failing this i
336bd 6d 70 6c 69 65 73 20 74 68 61 74 20 74 68 65 20  mplies that the 
336be 74 65 72 6d 73 20 77 65 72 65 6e 27 74 20 69 6e  terms weren't in
336bf 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 61   order. */.    a
336c0 73 73 65 72 74 28 20 6e 50 72 65 66 69 78 3c 6e  ssert( nPrefix<n
336c1 54 65 72 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  Term );.  }..  i
336c2 66 28 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  f( pWriter->data
336c3 2e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  .nData==0 ){.   
336c4 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 6e   /* Encode the n
336c5 6f 64 65 20 68 65 61 64 65 72 20 61 6e 64 20 6c  ode header and l
336c6 65 61 64 69 6e 67 20 74 65 72 6d 20 61 73 3a 0a  eading term as:.
336c7 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 30      **  varint(0
336c8 29 0a 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74  ).    **  varint
336c9 28 6e 54 65 72 6d 29 0a 20 20 20 20 2a 2a 20 20  (nTerm).    **  
336ca 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d  char pTerm[nTerm
336cb 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d  ].    */.    n =
336cc 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
336cd 2c 20 27 5c 30 27 29 3b 0a 20 20 20 20 6e 20 2b  , '\0');.    n +
336ce 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
336cf 63 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  c+n, nTerm);.   
336d0 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
336d1 64 32 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  d2(&pWriter->dat
336d2 61 2c 20 63 2c 20 6e 2c 20 70 54 65 72 6d 2c 20  a, c, n, pTerm, 
336d3 6e 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  nTerm);.  }else{
336d4 0a 20 20 20 20 2f 2a 20 44 65 6c 74 61 2d 65 6e  .    /* Delta-en
336d5 63 6f 64 65 20 74 68 65 20 74 65 72 6d 20 61 73  code the term as
336d6 3a 0a 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74  :.    **  varint
336d7 28 6e 50 72 65 66 69 78 29 0a 20 20 20 20 2a 2a  (nPrefix).    **
336d8 20 20 76 61 72 69 6e 74 28 6e 53 75 66 66 69 78    varint(nSuffix
336d9 29 0a 20 20 20 20 2a 2a 20 20 63 68 61 72 20 70  ).    **  char p
336da 54 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66  TermSuffix[nSuff
336db 69 78 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  ix].    */.    n
336dc 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
336dd 28 63 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20  (c, nPrefix);.  
336de 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61    n += fts3PutVa
336df 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d  rint(c+n, nTerm-
336e0 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 64 61  nPrefix);.    da
336e1 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
336e2 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20  &pWriter->data, 
336e3 63 2c 20 6e 2c 20 70 54 65 72 6d 2b 6e 50 72 65  c, n, pTerm+nPre
336e4 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66  fix, nTerm-nPref
336e5 69 78 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42  ix);.  }.  dataB
336e6 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70 57  ufferReplace(&pW
336e7 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65  riter->term, pTe
336e8 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72  rm, nTerm);..  r
336e9 65 74 75 72 6e 20 6e 50 72 65 66 69 78 2b 31 3b  eturn nPrefix+1;
336ea 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 74 6f 20 61  .}../* Used to a
336eb 76 6f 69 64 20 61 20 6d 65 6d 6d 6f 76 65 20 77  void a memmove w
336ec 68 65 6e 20 61 20 6c 61 72 67 65 20 61 6d 6f 75  hen a large amou
336ed 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 20 64 61  nt of doclist da
336ee 74 61 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20  ta is in.** the 
336ef 62 75 66 66 65 72 2e 20 20 54 68 69 73 20 63 6f  buffer.  This co
336f0 6e 73 74 72 75 63 74 73 20 61 20 6e 6f 64 65 20  nstructs a node 
336f1 61 6e 64 20 74 65 72 6d 20 68 65 61 64 65 72 20  and term header 
336f2 62 65 66 6f 72 65 0a 2a 2a 20 69 44 6f 63 6c 69  before.** iDocli
336f3 73 74 44 61 74 61 20 61 6e 64 20 66 6c 75 73 68  stData and flush
336f4 65 73 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  es the resulting
336f5 20 63 6f 6d 70 6c 65 74 65 20 6e 6f 64 65 20 75   complete node u
336f6 73 69 6e 67 0a 2a 2a 20 6c 65 61 66 57 72 69 74  sing.** leafWrit
336f7 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28  erInternalFlush(
336f8 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
336f9 20 6c 65 61 66 57 72 69 74 65 72 49 6e 6c 69 6e   leafWriterInlin
336fa 65 46 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f  eFlush(fulltext_
336fb 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69  vtab *v, LeafWri
336fc 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
336fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
336ff 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
33700 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20  , int nTerm,.   
33701 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33702 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
33703 74 20 69 44 6f 63 6c 69 73 74 44 61 74 61 29 7b  t iDoclistData){
33704 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54  .  char c[VARINT
33705 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d  _MAX+VARINT_MAX]
33706 3b 0a 20 20 69 6e 74 20 69 44 61 74 61 2c 20 6e  ;.  int iData, n
33707 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
33708 28 63 2c 20 30 29 3b 0a 20 20 6e 20 2b 3d 20 66  (c, 0);.  n += f
33709 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e  ts3PutVarint(c+n
3370a 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20  , nTerm);..  /* 
3370b 54 68 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77  There should alw
3370c 61 79 73 20 62 65 20 72 6f 6f 6d 20 66 6f 72 20  ays be room for 
3370d 74 68 65 20 68 65 61 64 65 72 2e 20 20 45 76 65  the header.  Eve
3370e 6e 20 69 66 20 70 54 65 72 6d 20 73 68 61 72 65  n if pTerm share
3370f 64 0a 20 20 2a 2a 20 61 20 73 75 62 73 74 61 6e  d.  ** a substan
33710 74 69 61 6c 20 70 72 65 66 69 78 20 77 69 74 68  tial prefix with
33711 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 65   the previous te
33712 72 6d 2c 20 74 68 65 20 65 6e 74 69 72 65 20 70  rm, the entire p
33713 72 65 66 69 78 0a 20 20 2a 2a 20 63 6f 75 6c 64  refix.  ** could
33714 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20   be constructed 
33715 66 72 6f 6d 20 65 61 72 6c 69 65 72 20 64 61 74  from earlier dat
33716 61 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74  a in the doclist
33717 2c 20 73 6f 20 74 68 65 72 65 0a 20 20 2a 2a 20  , so there.  ** 
33718 73 68 6f 75 6c 64 20 62 65 20 72 6f 6f 6d 2e 0a  should be room..
33719 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69    */.  assert( i
3371a 44 6f 63 6c 69 73 74 44 61 74 61 3e 3d 6e 2b 6e  DoclistData>=n+n
3371b 54 65 72 6d 20 29 3b 0a 0a 20 20 69 44 61 74 61  Term );..  iData
3371c 20 3d 20 69 44 6f 63 6c 69 73 74 44 61 74 61 2d   = iDoclistData-
3371d 28 6e 2b 6e 54 65 72 6d 29 3b 0a 20 20 6d 65 6d  (n+nTerm);.  mem
3371e 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74  cpy(pWriter->dat
3371f 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 63  a.pData+iData, c
33720 2c 20 6e 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  , n);.  memcpy(p
33721 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
33722 74 61 2b 69 44 61 74 61 2b 6e 2c 20 70 54 65 72  ta+iData+n, pTer
33723 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72 65  m, nTerm);..  re
33724 74 75 72 6e 20 6c 65 61 66 57 72 69 74 65 72 49  turn leafWriterI
33725 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20  nternalFlush(v, 
33726 70 57 72 69 74 65 72 2c 20 69 44 61 74 61 2c 20  pWriter, iData, 
33727 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
33728 61 74 61 2d 69 44 61 74 61 29 3b 0a 7d 0a 0a 2f  ata-iData);.}../
33729 2a 20 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65  * Push pTerm[nTe
3372a 72 6d 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74  rm] along with t
3372b 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  he doclist data 
3372c 74 6f 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65  to the leaf laye
3372d 72 20 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e  r of.** %_segmen
3372e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ts..*/.static in
3372f 74 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  t leafWriterStep
33730 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76  Merge(fulltext_v
33731 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74  tab *v, LeafWrit
33732 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20  er *pWriter,.   
33733 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33734 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
33735 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
33736 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20  nt nTerm,.      
33737 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33738 20 20 20 20 20 20 20 20 20 44 4c 52 65 61 64 65           DLReade
33739 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
3373a 20 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 63 68   nReaders){.  ch
3373b 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
3373c 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
3373d 6e 74 20 69 54 65 72 6d 44 61 74 61 20 3d 20 70  nt iTermData = p
3373e 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
3373f 74 61 2c 20 69 44 6f 63 6c 69 73 74 44 61 74 61  ta, iDoclistData
33740 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 44 61 74 61  ;.  int i, nData
33741 2c 20 6e 2c 20 6e 41 63 74 75 61 6c 44 61 74 61  , n, nActualData
33742 2c 20 6e 41 63 74 75 61 6c 2c 20 72 63 2c 20 6e  , nActual, rc, n
33743 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20  TermDistinct;.. 
33744 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45   ASSERT_VALID_LE
33745 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d  AF_NODE(pWriter-
33746 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 70 57 72  >data.pData, pWr
33747 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
33748 29 3b 0a 20 20 6e 54 65 72 6d 44 69 73 74 69 6e  );.  nTermDistin
33749 63 74 20 3d 20 6c 65 61 66 57 72 69 74 65 72 45  ct = leafWriterE
3374a 6e 63 6f 64 65 54 65 72 6d 28 70 57 72 69 74 65  ncodeTerm(pWrite
3374b 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29  r, pTerm, nTerm)
3374c 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72  ;..  /* Remember
3374d 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 69   nTermDistinct i
3374e 66 20 6f 70 65 6e 69 6e 67 20 61 20 6e 65 77 20  f opening a new 
3374f 6e 6f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 69  node. */.  if( i
33750 54 65 72 6d 44 61 74 61 3d 3d 30 20 29 20 70 57  TermData==0 ) pW
33751 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
33752 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
33753 69 6e 63 74 3b 0a 0a 20 20 69 44 6f 63 6c 69 73  inct;..  iDoclis
33754 74 44 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d  tData = pWriter-
33755 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 0a 20 20  >data.nData;..  
33756 2f 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20  /* Estimate the 
33757 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6d 65  length of the me
33758 72 67 65 64 20 64 6f 63 6c 69 73 74 20 73 6f 20  rged doclist so 
33759 77 65 20 63 61 6e 20 6c 65 61 76 65 20 73 70 61  we can leave spa
3375a 63 65 0a 20 20 2a 2a 20 74 6f 20 65 6e 63 6f 64  ce.  ** to encod
3375b 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  e it..  */.  for
3375c 28 69 3d 30 2c 20 6e 44 61 74 61 3d 30 3b 20 69  (i=0, nData=0; i
3375d 3c 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b  <nReaders; i++){
3375e 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 64 6c  .    nData += dl
3375f 72 41 6c 6c 44 61 74 61 42 79 74 65 73 28 26 70  rAllDataBytes(&p
33760 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 7d  Readers[i]);.  }
33761 0a 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61  .  n = fts3PutVa
33762 72 69 6e 74 28 63 2c 20 6e 44 61 74 61 29 3b 0a  rint(c, nData);.
33763 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
33764 6e 64 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  nd(&pWriter->dat
33765 61 2c 20 63 2c 20 6e 29 3b 0a 0a 20 20 64 6f 63  a, c, n);..  doc
33766 4c 69 73 74 4d 65 72 67 65 28 26 70 57 72 69 74  ListMerge(&pWrit
33767 65 72 2d 3e 64 61 74 61 2c 20 70 52 65 61 64 65  er->data, pReade
33768 72 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20  rs, nReaders);. 
33769 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f   ASSERT_VALID_DO
3376a 43 4c 49 53 54 28 44 4c 5f 44 45 46 41 55 4c 54  CLIST(DL_DEFAULT
3376b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3376c 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
3376d 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f  ->data.pData+iDo
3376e 63 6c 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20  clistData+n,.   
3376f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33770 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74      pWriter->dat
33771 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73 74  a.nData-iDoclist
33772 44 61 74 61 2d 6e 2c 20 4e 55 4c 4c 29 3b 0a 0a  Data-n, NULL);..
33773 20 20 2f 2a 20 54 68 65 20 61 63 74 75 61 6c 20    /* The actual 
33774 61 6d 6f 75 6e 74 20 6f 66 20 64 6f 63 6c 69 73  amount of doclis
33775 74 20 64 61 74 61 20 61 74 20 74 68 69 73 20 70  t data at this p
33776 6f 69 6e 74 20 63 6f 75 6c 64 20 62 65 20 73 6d  oint could be sm
33777 61 6c 6c 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20  aller.  ** than 
33778 74 68 65 20 6c 65 6e 67 74 68 20 77 65 20 65 6e  the length we en
33779 63 6f 64 65 64 2e 20 20 41 64 64 69 74 69 6f 6e  coded.  Addition
3377a 61 6c 6c 79 2c 20 74 68 65 20 73 70 61 63 65 20  ally, the space 
3377b 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a  required to.  **
3377c 20 65 6e 63 6f 64 65 20 74 68 69 73 20 6c 65 6e   encode this len
3377d 67 74 68 20 63 6f 75 6c 64 20 62 65 20 73 6d 61  gth could be sma
3377e 6c 6c 65 72 2e 20 20 46 6f 72 20 73 6d 61 6c 6c  ller.  For small
3377f 20 64 6f 63 6c 69 73 74 73 2c 20 74 68 69 73 20   doclists, this 
33780 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 61 20 62 69  is.  ** not a bi
33781 67 20 64 65 61 6c 2c 20 77 65 20 63 61 6e 20 6a  g deal, we can j
33782 75 73 74 20 75 73 65 20 6d 65 6d 6d 6f 76 65 28  ust use memmove(
33783 29 20 74 6f 20 61 64 6a 75 73 74 20 74 68 69 6e  ) to adjust thin
33784 67 73 2e 0a 20 20 2a 2f 0a 20 20 6e 41 63 74 75  gs..  */.  nActu
33785 61 6c 44 61 74 61 20 3d 20 70 57 72 69 74 65 72  alData = pWriter
33786 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44  ->data.nData-(iD
33787 6f 63 6c 69 73 74 44 61 74 61 2b 6e 29 3b 0a 20  oclistData+n);. 
33788 20 6e 41 63 74 75 61 6c 20 3d 20 66 74 73 33 50   nActual = fts3P
33789 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 41 63 74  utVarint(c, nAct
3378a 75 61 6c 44 61 74 61 29 3b 0a 20 20 61 73 73 65  ualData);.  asse
3378b 72 74 28 20 6e 41 63 74 75 61 6c 44 61 74 61 3c  rt( nActualData<
3378c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65  =nData );.  asse
3378d 72 74 28 20 6e 41 63 74 75 61 6c 3c 3d 6e 20 29  rt( nActual<=n )
3378e 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e  ;..  /* If the n
3378f 65 77 20 64 6f 63 6c 69 73 74 20 69 73 20 62 69  ew doclist is bi
33790 67 20 65 6e 6f 75 67 68 20 66 6f 72 20 66 6f 72  g enough for for
33791 63 65 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20  ce a standalone 
33792 6c 65 61 66 0a 20 20 2a 2a 20 6e 6f 64 65 2c 20  leaf.  ** node, 
33793 77 65 20 63 61 6e 20 69 6d 6d 65 64 69 61 74 65  we can immediate
33794 6c 79 20 66 6c 75 73 68 20 69 74 20 69 6e 6c 69  ly flush it inli
33795 6e 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  ne without doing
33796 20 74 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76   the.  ** memmov
33797 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54  e()..  */.  /* T
33798 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
33799 74 65 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61  test matches lea
3379a 66 57 72 69 74 65 72 53 74 65 70 28 29 2c 20 77  fWriterStep(), w
3379b 68 69 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20  hich does this. 
3379c 20 2a 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20   ** test before 
3379d 69 74 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73  it knows the cos
3379e 74 20 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f  t to varint-enco
3379f 64 65 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a  de the term and.
337a0 20 20 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e    ** doclist len
337a1 67 74 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70  gths.  At some p
337a2 6f 69 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a  oint, change to.
337a3 20 20 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61    ** pWriter->da
337a4 74 61 2e 6e 44 61 74 61 2d 69 54 65 72 6d 44 61  ta.nData-iTermDa
337a5 74 61 3e 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49  ta>STANDALONE_MI
337a6 4e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 54  N..  */.  if( nT
337a7 65 72 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e  erm+nActualData>
337a8 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 29  STANDALONE_MIN )
337a9 7b 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 6c 65  {.    /* Push le
337aa 61 66 20 6e 6f 64 65 20 66 72 6f 6d 20 62 65 66  af node from bef
337ab 6f 72 65 20 74 68 69 73 20 74 65 72 6d 2e 20 2a  ore this term. *
337ac 2f 0a 20 20 20 20 69 66 28 20 69 54 65 72 6d 44  /.    if( iTermD
337ad 61 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 72  ata>0 ){.      r
337ae 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e  c = leafWriterIn
337af 74 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70  ternalFlush(v, p
337b0 57 72 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d  Writer, 0, iTerm
337b1 44 61 74 61 29 3b 0a 20 20 20 20 20 20 69 66 28  Data);.      if(
337b2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
337b3 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
337b4 20 20 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72     pWriter->nTer
337b5 6d 44 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72  mDistinct = nTer
337b6 6d 44 69 73 74 69 6e 63 74 3b 0a 20 20 20 20 7d  mDistinct;.    }
337b7 0a 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65  ..    /* Fix the
337b8 20 65 6e 63 6f 64 65 64 20 64 6f 63 6c 69 73 74   encoded doclist
337b9 20 6c 65 6e 67 74 68 2e 20 2a 2f 0a 20 20 20 20   length. */.    
337ba 69 44 6f 63 6c 69 73 74 44 61 74 61 20 2b 3d 20  iDoclistData += 
337bb 6e 20 2d 20 6e 41 63 74 75 61 6c 3b 0a 20 20 20  n - nActual;.   
337bc 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d   memcpy(pWriter-
337bd 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
337be 6c 69 73 74 44 61 74 61 2c 20 63 2c 20 6e 41 63  listData, c, nAc
337bf 74 75 61 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 50  tual);..    /* P
337c0 75 73 68 20 74 68 65 20 73 74 61 6e 64 61 6c 6f  ush the standalo
337c1 6e 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f  ne leaf node. */
337c2 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72  .    rc = leafWr
337c3 69 74 65 72 49 6e 6c 69 6e 65 46 6c 75 73 68 28  iterInlineFlush(
337c4 76 2c 20 70 57 72 69 74 65 72 2c 20 70 54 65 72  v, pWriter, pTer
337c5 6d 2c 20 6e 54 65 72 6d 2c 20 69 44 6f 63 6c 69  m, nTerm, iDocli
337c6 73 74 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28  stData);.    if(
337c7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
337c8 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
337c9 20 2f 2a 20 4c 65 61 76 65 20 74 68 65 20 6e 6f   /* Leave the no
337ca 64 65 20 65 6d 70 74 79 2e 20 2a 2f 0a 20 20 20  de empty. */.   
337cb 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
337cc 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 29  (&pWriter->data)
337cd 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ;..    return rc
337ce 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74  ;.  }..  /* At t
337cf 68 69 73 20 70 6f 69 6e 74 2c 20 77 65 20 6b 6e  his point, we kn
337d0 6f 77 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c  ow that the docl
337d1 69 73 74 20 77 61 73 20 73 6d 61 6c 6c 2c 20 73  ist was small, s
337d2 6f 20 64 6f 20 74 68 65 0a 20 20 2a 2a 20 6d 65  o do the.  ** me
337d3 6d 6d 6f 76 65 20 69 66 20 69 6e 64 69 63 61 74  mmove if indicat
337d4 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  ed..  */.  if( n
337d5 41 63 74 75 61 6c 3c 6e 20 29 7b 0a 20 20 20 20  Actual<n ){.    
337d6 6d 65 6d 6d 6f 76 65 28 70 57 72 69 74 65 72 2d  memmove(pWriter-
337d7 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
337d8 6c 69 73 74 44 61 74 61 2b 6e 41 63 74 75 61 6c  listData+nActual
337d9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 57  ,.            pW
337da 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
337db 61 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e  a+iDoclistData+n
337dc 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 57  ,.            pW
337dd 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
337de 61 2d 28 69 44 6f 63 6c 69 73 74 44 61 74 61 2b  a-(iDoclistData+
337df 6e 29 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72  n));.    pWriter
337e0 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20  ->data.nData -= 
337e1 6e 2d 6e 41 63 74 75 61 6c 3b 0a 20 20 7d 0a 0a  n-nActual;.  }..
337e2 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 77 72 69    /* Replace wri
337e3 74 74 65 6e 20 6c 65 6e 67 74 68 20 77 69 74 68  tten length with
337e4 20 61 63 74 75 61 6c 20 6c 65 6e 67 74 68 2e 20   actual length. 
337e5 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 70 57 72 69  */.  memcpy(pWri
337e6 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
337e7 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 20 63 2c  iDoclistData, c,
337e8 20 6e 41 63 74 75 61 6c 29 3b 0a 0a 20 20 2f 2a   nActual);..  /*
337e9 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20   If the node is 
337ea 74 6f 6f 20 6c 61 72 67 65 2c 20 62 72 65 61 6b  too large, break
337eb 20 74 68 69 6e 67 73 20 75 70 2e 20 2a 2f 0a 20   things up. */. 
337ec 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
337ed 54 68 69 73 20 74 65 73 74 20 6d 61 74 63 68 65  This test matche
337ee 73 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  s leafWriterStep
337ef 28 29 2c 20 77 68 69 63 68 20 64 6f 65 73 20 74  (), which does t
337f0 68 69 73 0a 20 20 2a 2a 20 74 65 73 74 20 62 65  his.  ** test be
337f1 66 6f 72 65 20 69 74 20 6b 6e 6f 77 73 20 74 68  fore it knows th
337f2 65 20 63 6f 73 74 20 74 6f 20 76 61 72 69 6e 74  e cost to varint
337f3 2d 65 6e 63 6f 64 65 20 74 68 65 20 74 65 72 6d  -encode the term
337f4 20 61 6e 64 0a 20 20 2a 2a 20 64 6f 63 6c 69 73   and.  ** doclis
337f5 74 20 6c 65 6e 67 74 68 73 2e 20 20 41 74 20 73  t lengths.  At s
337f6 6f 6d 65 20 70 6f 69 6e 74 2c 20 63 68 61 6e 67  ome point, chang
337f7 65 20 74 6f 0a 20 20 2a 2a 20 70 57 72 69 74 65  e to.  ** pWrite
337f8 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e 4c 45  r->data.nData>LE
337f9 41 46 5f 4d 41 58 2e 0a 20 20 2a 2f 0a 20 20 69  AF_MAX..  */.  i
337fa 66 28 20 69 54 65 72 6d 44 61 74 61 2b 6e 54 65  f( iTermData+nTe
337fb 72 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e 4c  rm+nActualData>L
337fc 45 41 46 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2f  EAF_MAX ){.    /
337fd 2a 20 46 6c 75 73 68 20 6f 75 74 20 74 68 65 20  * Flush out the 
337fe 6c 65 61 64 69 6e 67 20 64 61 74 61 20 61 73 20  leading data as 
337ff 61 20 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 72 63  a node */.    rc
33800 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74   = leafWriterInt
33801 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57  ernalFlush(v, pW
33802 72 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d 44  riter, 0, iTermD
33803 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ata);.    if( rc
33804 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
33805 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 70 57  turn rc;..    pW
33806 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
33807 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
33808 69 6e 63 74 3b 0a 0a 20 20 20 20 2f 2a 20 52 65  inct;..    /* Re
33809 62 75 69 6c 64 20 68 65 61 64 65 72 20 75 73 69  build header usi
3380a 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  ng the current t
3380b 65 72 6d 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66  erm */.    n = f
3380c 74 73 33 50 75 74 56 61 72 69 6e 74 28 70 57 72  ts3PutVarint(pWr
3380d 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
3380e 2c 20 30 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66  , 0);.    n += f
3380f 74 73 33 50 75 74 56 61 72 69 6e 74 28 70 57 72  ts3PutVarint(pWr
33810 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
33811 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20  +n, nTerm);.    
33812 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e  memcpy(pWriter->
33813 64 61 74 61 2e 70 44 61 74 61 2b 6e 2c 20 70 54  data.pData+n, pT
33814 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  erm, nTerm);.   
33815 20 6e 20 2b 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20   n += nTerm;..  
33816 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c    /* There shoul
33817 64 20 61 6c 77 61 79 73 20 62 65 20 72 6f 6f 6d  d always be room
33818 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 70 72  , because the pr
33819 65 76 69 6f 75 73 20 65 6e 63 6f 64 69 6e 67 0a  evious encoding.
3381a 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20      ** included 
3381b 61 6c 6c 20 64 61 74 61 20 6e 65 63 65 73 73 61  all data necessa
3381c 72 79 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20  ry to construct 
3381d 74 68 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f  the term..    */
3381e 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 69  .    assert( n<i
3381f 44 6f 63 6c 69 73 74 44 61 74 61 20 29 3b 0a 20  DoclistData );. 
33820 20 20 20 2f 2a 20 53 6f 20 6c 6f 6e 67 20 61 73     /* So long as
33821 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20   STANDALONE_MIN 
33822 69 73 20 68 61 6c 66 20 6f 72 20 6c 65 73 73 20  is half or less 
33823 6f 66 20 4c 45 41 46 5f 4d 41 58 2c 20 74 68 65  of LEAF_MAX, the
33824 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  .    ** followin
33825 67 20 6d 65 6d 63 70 79 28 29 20 69 73 20 73 61  g memcpy() is sa
33826 66 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74  fe (as opposed t
33827 6f 20 6e 65 65 64 69 6e 67 20 61 20 6d 65 6d 6d  o needing a memm
33828 6f 76 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ove)..    */.   
33829 20 61 73 73 65 72 74 28 20 32 2a 53 54 41 4e 44   assert( 2*STAND
3382a 41 4c 4f 4e 45 5f 4d 49 4e 3c 3d 4c 45 41 46 5f  ALONE_MIN<=LEAF_
3382b 4d 41 58 20 29 3b 0a 20 20 20 20 61 73 73 65 72  MAX );.    asser
3382c 74 28 20 6e 2b 70 57 72 69 74 65 72 2d 3e 64 61  t( n+pWriter->da
3382d 74 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73  ta.nData-iDoclis
3382e 74 44 61 74 61 3c 69 44 6f 63 6c 69 73 74 44 61  tData<iDoclistDa
3382f 74 61 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ta );.    memcpy
33830 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
33831 44 61 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20  Data+n,.        
33832 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61     pWriter->data
33833 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
33834 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
33835 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
33836 61 74 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61  ata-iDoclistData
33837 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
33838 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20 69 44  data.nData -= iD
33839 6f 63 6c 69 73 74 44 61 74 61 2d 6e 3b 0a 20 20  oclistData-n;.  
3383a 7d 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44  }.  ASSERT_VALID
3383b 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74  _LEAF_NODE(pWrit
3383c 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20  er->data.pData, 
3383d 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
3383e 61 74 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ata);..  return 
3383f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
33840 20 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65 72   Push pTerm[nTer
33841 6d 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  m] along with th
33842 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74  e doclist data t
33843 6f 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65 72  o the leaf layer
33844 20 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74   of.** %_segment
33845 73 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  s..*/./* TODO(sh
33846 65 73 73 29 20 52 65 76 69 73 65 20 77 72 69 74  ess) Revise writ
33847 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 73  eZeroSegment() s
33848 6f 20 74 68 61 74 20 64 6f 63 6c 69 73 74 73 20  o that doclists 
33849 61 72 65 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74  are.** construct
3384a 65 64 20 64 69 72 65 63 74 6c 79 20 69 6e 20 70  ed directly in p
3384b 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 0a 2a 2f  Writer->data..*/
3384c 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66  .static int leaf
3384d 57 72 69 74 65 72 53 74 65 70 28 66 75 6c 6c 74  WriterStep(fullt
3384e 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
3384f 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
33850 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33851 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
33852 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
33853 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20  nt nTerm,.      
33854 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33855 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
33856 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
33857 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44  ){.  int rc;.  D
33858 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  LReader reader;.
33859 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61 64  .  dlrInit(&read
3385a 65 72 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  er, DL_DEFAULT, 
3385b 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20  pData, nData);. 
3385c 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
3385d 53 74 65 70 4d 65 72 67 65 28 76 2c 20 70 57 72  StepMerge(v, pWr
3385e 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65  iter, pTerm, nTe
3385f 72 6d 2c 20 26 72 65 61 64 65 72 2c 20 31 29 3b  rm, &reader, 1);
33860 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
33861 65 61 64 65 72 29 3b 0a 0a 20 20 72 65 74 75 72  eader);..  retur
33862 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a  n rc;.}.../*****
33863 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33864 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33865 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33866 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
33867 4c 65 61 66 52 65 61 64 65 72 20 69 73 20 75 73  LeafReader is us
33868 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 6f 76  ed to iterate ov
33869 65 72 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c  er an individual
3386a 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 74   leaf node. */.t
3386b 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 65  ypedef struct Le
3386c 61 66 52 65 61 64 65 72 20 7b 0a 20 20 44 61 74  afReader {.  Dat
3386d 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20  aBuffer term;   
3386e 20 20 20 20 20 20 20 2f 2a 20 63 6f 70 79 20 6f         /* copy o
3386f 66 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20  f current term. 
33870 2a 2f 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
33871 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20   *pData;        
33872 2f 2a 20 64 61 74 61 20 66 6f 72 20 63 75 72 72  /* data for curr
33873 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 69  ent term. */.  i
33874 6e 74 20 6e 44 61 74 61 3b 0a 7d 20 4c 65 61 66  nt nData;.} Leaf
33875 52 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20  Reader;..static 
33876 76 6f 69 64 20 6c 65 61 66 52 65 61 64 65 72 44  void leafReaderD
33877 65 73 74 72 6f 79 28 4c 65 61 66 52 65 61 64 65  estroy(LeafReade
33878 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 64  r *pReader){.  d
33879 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
3387a 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 29  (&pReader->term)
3387b 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  ;.  SCRAMBLE(pRe
3387c 61 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  ader);.}..static
3387d 20 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 41   int leafReaderA
3387e 74 45 6e 64 28 4c 65 61 66 52 65 61 64 65 72 20  tEnd(LeafReader 
3387f 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
33880 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61  urn pReader->nDa
33881 74 61 3c 3d 30 3b 0a 7d 0a 0a 2f 2a 20 41 63 63  ta<=0;.}../* Acc
33882 65 73 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  ess the current 
33883 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20  term. */.static 
33884 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 54 65  int leafReaderTe
33885 72 6d 42 79 74 65 73 28 4c 65 61 66 52 65 61 64  rmBytes(LeafRead
33886 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
33887 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
33888 74 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74  term.nData;.}.st
33889 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
3388a 2a 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d 28  *leafReaderTerm(
3388b 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
3388c 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
3388d 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
3388e 61 74 61 3e 30 20 29 3b 0a 20 20 72 65 74 75 72  ata>0 );.  retur
3388f 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
33890 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 41 63 63  pData;.}../* Acc
33891 65 73 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20  ess the doclist 
33892 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  data for the cur
33893 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74  rent term. */.st
33894 61 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61  atic int leafRea
33895 64 65 72 44 61 74 61 42 79 74 65 73 28 4c 65 61  derDataBytes(Lea
33896 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  fReader *pReader
33897 29 7b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  ){.  int nData;.
33898 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
33899 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20  r->term.nData>0 
3389a 29 3b 0a 20 20 66 74 73 33 47 65 74 56 61 72 69  );.  fts3GetVari
3389b 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
3389c 61 74 61 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20  ata, &nData);.  
3389d 72 65 74 75 72 6e 20 6e 44 61 74 61 3b 0a 7d 0a  return nData;.}.
3389e 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
3389f 72 20 2a 6c 65 61 66 52 65 61 64 65 72 44 61 74  r *leafReaderDat
338a0 61 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  a(LeafReader *pR
338a1 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c  eader){.  int n,
338a2 20 6e 44 61 74 61 3b 0a 20 20 61 73 73 65 72 74   nData;.  assert
338a3 28 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  ( pReader->term.
338a4 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 6e 20 3d  nData>0 );.  n =
338a5 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
338a6 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
338a7 20 26 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75   &nData);.  retu
338a8 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  rn pReader->pDat
338a9 61 2b 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  a+n;.}..static v
338aa 6f 69 64 20 6c 65 61 66 52 65 61 64 65 72 49 6e  oid leafReaderIn
338ab 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  it(const char *p
338ac 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
338ad 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
338ae 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
338af 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
338b0 7b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 6e  {.  int nTerm, n
338b1 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  ;..  assert( nDa
338b2 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>0 );.  assert
338b3 28 20 70 44 61 74 61 5b 30 5d 3d 3d 27 5c 30 27  ( pData[0]=='\0'
338b4 20 29 3b 0a 0a 20 20 43 4c 45 41 52 28 70 52 65   );..  CLEAR(pRe
338b5 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  ader);..  /* Rea
338b6 64 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  d the first term
338b7 2c 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20 68  , skipping the h
338b8 65 61 64 65 72 20 62 79 74 65 2e 20 2a 2f 0a 20  eader byte. */. 
338b9 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
338ba 6e 74 33 32 28 70 44 61 74 61 2b 31 2c 20 26 6e  nt32(pData+1, &n
338bb 54 65 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66  Term);.  dataBuf
338bc 66 65 72 49 6e 69 74 28 26 70 52 65 61 64 65 72  ferInit(&pReader
338bd 2d 3e 74 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a  ->term, nTerm);.
338be 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c    dataBufferRepl
338bf 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 74 65  ace(&pReader->te
338c0 72 6d 2c 20 70 44 61 74 61 2b 31 2b 6e 2c 20 6e  rm, pData+1+n, n
338c1 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20 50 6f 73  Term);..  /* Pos
338c2 69 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20  ition after the 
338c3 66 69 72 73 74 20 74 65 72 6d 2e 20 2a 2f 0a 20  first term. */. 
338c4 20 61 73 73 65 72 74 28 20 31 2b 6e 2b 6e 54 65   assert( 1+n+nTe
338c5 72 6d 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 52  rm<nData );.  pR
338c6 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70  eader->pData = p
338c7 44 61 74 61 2b 31 2b 6e 2b 6e 54 65 72 6d 3b 0a  Data+1+n+nTerm;.
338c8 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
338c9 20 3d 20 6e 44 61 74 61 2d 31 2d 6e 2d 6e 54 65   = nData-1-n-nTe
338ca 72 6d 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 74  rm;.}../* Step t
338cb 68 65 20 72 65 61 64 65 72 20 66 6f 72 77 61 72  he reader forwar
338cc 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65  d to the next te
338cd 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  rm. */.static vo
338ce 69 64 20 6c 65 61 66 52 65 61 64 65 72 53 74 65  id leafReaderSte
338cf 70 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  p(LeafReader *pR
338d0 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c  eader){.  int n,
338d1 20 6e 44 61 74 61 2c 20 6e 50 72 65 66 69 78 2c   nData, nPrefix,
338d2 20 6e 53 75 66 66 69 78 3b 0a 20 20 61 73 73 65   nSuffix;.  asse
338d3 72 74 28 20 21 6c 65 61 66 52 65 61 64 65 72 41  rt( !leafReaderA
338d4 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
338d5 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 72 65 76  ..  /* Skip prev
338d6 69 6f 75 73 20 65 6e 74 72 79 27 73 20 64 61 74  ious entry's dat
338d7 61 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6e 20  a block. */.  n 
338d8 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
338d9 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
338da 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20 61 73 73  , &nData);.  ass
338db 65 72 74 28 20 6e 2b 6e 44 61 74 61 3c 3d 70 52  ert( n+nData<=pR
338dc 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
338dd 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
338de 20 2b 3d 20 6e 2b 6e 44 61 74 61 3b 0a 20 20 70   += n+nData;.  p
338df 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d  Reader->nData -=
338e0 20 6e 2b 6e 44 61 74 61 3b 0a 0a 20 20 69 66 28   n+nData;..  if(
338e1 20 21 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e   !leafReaderAtEn
338e2 64 28 70 52 65 61 64 65 72 29 20 29 7b 0a 20 20  d(pReader) ){.  
338e3 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74    /* Construct t
338e4 68 65 20 6e 65 77 20 74 65 72 6d 20 75 73 69 6e  he new term usin
338e5 67 20 61 20 70 72 65 66 69 78 20 66 72 6f 6d 20  g a prefix from 
338e6 74 68 65 20 6f 6c 64 20 74 65 72 6d 20 70 6c 75  the old term plu
338e7 73 20 61 0a 20 20 20 20 2a 2a 20 73 75 66 66 69  s a.    ** suffi
338e8 78 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20  x from the leaf 
338e9 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  data..    */.   
338ea 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
338eb 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
338ec 61 74 61 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a  ata, &nPrefix);.
338ed 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
338ee 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
338ef 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66  ->pData+n, &nSuf
338f0 66 69 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74  fix);.    assert
338f1 28 20 6e 2b 6e 53 75 66 66 69 78 3c 70 52 65 61  ( n+nSuffix<pRea
338f2 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
338f3 20 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e    pReader->term.
338f4 6e 44 61 74 61 20 3d 20 6e 50 72 65 66 69 78 3b  nData = nPrefix;
338f5 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
338f6 70 70 65 6e 64 28 26 70 52 65 61 64 65 72 2d 3e  ppend(&pReader->
338f7 74 65 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70  term, pReader->p
338f8 44 61 74 61 2b 6e 2c 20 6e 53 75 66 66 69 78 29  Data+n, nSuffix)
338f9 3b 0a 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  ;..    pReader->
338fa 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66  pData += n+nSuff
338fb 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d  ix;.    pReader-
338fc 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66  >nData -= n+nSuf
338fd 66 69 78 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 73  fix;.  }.}../* s
338fe 74 72 63 6d 70 2d 73 74 79 6c 65 20 63 6f 6d 70  trcmp-style comp
338ff 61 72 69 73 6f 6e 20 6f 66 20 70 52 65 61 64 65  arison of pReade
33900 72 27 73 20 63 75 72 72 65 6e 74 20 74 65 72 6d  r's current term
33901 20 61 67 61 69 6e 73 74 20 70 54 65 72 6d 2e 0a   against pTerm..
33902 2a 2a 20 49 66 20 69 73 50 72 65 66 69 78 2c 20  ** If isPrefix, 
33903 65 71 75 61 6c 69 74 79 20 6d 65 61 6e 73 20 65  equality means e
33904 71 75 61 6c 20 74 68 72 6f 75 67 68 20 6e 54 65  qual through nTe
33905 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  rm bytes..*/.sta
33906 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61 64  tic int leafRead
33907 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 66 52 65  erTermCmp(LeafRe
33908 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20  ader *pReader,. 
33909 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3390a 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
3390b 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
3390c 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
3390d 50 72 65 66 69 78 29 7b 0a 20 20 69 6e 74 20 63  Prefix){.  int c
3390e 2c 20 6e 20 3d 20 70 52 65 61 64 65 72 2d 3e 74  , n = pReader->t
3390f 65 72 6d 2e 6e 44 61 74 61 3c 6e 54 65 72 6d 20  erm.nData<nTerm 
33910 3f 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  ? pReader->term.
33911 6e 44 61 74 61 20 3a 20 6e 54 65 72 6d 3b 0a 20  nData : nTerm;. 
33912 20 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20   if( n==0 ){.   
33913 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 74 65   if( pReader->te
33914 72 6d 2e 6e 44 61 74 61 3e 30 20 29 20 72 65 74  rm.nData>0 ) ret
33915 75 72 6e 20 2d 31 3b 0a 20 20 20 20 69 66 28 6e  urn -1;.    if(n
33916 54 65 72 6d 3e 30 20 29 20 72 65 74 75 72 6e 20  Term>0 ) return 
33917 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  1;.    return 0;
33918 0a 20 20 7d 0a 0a 20 20 63 20 3d 20 6d 65 6d 63  .  }..  c = memc
33919 6d 70 28 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  mp(pReader->term
3391a 2e 70 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e  .pData, pTerm, n
3391b 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20  );.  if( c!=0 ) 
3391c 72 65 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20  return c;.  if( 
3391d 69 73 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e  isPrefix && n==n
3391e 54 65 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b  Term ) return 0;
3391f 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
33920 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 2d 20  r->term.nData - 
33921 6e 54 65 72 6d 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a  nTerm;.}.../****
33922 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33924 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33925 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
33926 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 77 72   LeavesReader wr
33927 61 70 73 20 4c 65 61 66 52 65 61 64 65 72 20 74  aps LeafReader t
33928 6f 20 61 6c 6c 6f 77 20 69 74 65 72 61 74 69 6e  o allow iteratin
33929 67 20 6f 76 65 72 20 74 68 65 20 65 6e 74 69 72  g over the entir
3392a 65 0a 2a 2a 20 6c 65 61 66 20 6c 61 79 65 72 20  e.** leaf layer 
3392b 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a  of the tree..*/.
3392c 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c  typedef struct L
3392d 65 61 76 65 73 52 65 61 64 65 72 20 7b 0a 20 20  eavesReader {.  
3392e 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20  int idx;        
3392f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
33930 65 78 20 77 69 74 68 69 6e 20 74 68 65 20 73 65  ex within the se
33931 67 6d 65 6e 74 2e 20 2a 2f 0a 0a 20 20 73 71 6c  gment. */..  sql
33932 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
33933 3b 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d  ;      /* Statem
33934 65 6e 74 20 77 65 27 72 65 20 73 74 72 65 61 6d  ent we're stream
33935 69 6e 67 20 6c 65 61 76 65 73 20 66 72 6f 6d 2e  ing leaves from.
33936 20 2a 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20   */.  int eof;  
33937 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33938 2f 2a 20 77 65 27 76 65 20 73 65 65 6e 20 53 51  /* we've seen SQ
33939 4c 49 54 45 5f 44 4f 4e 45 20 66 72 6f 6d 20 70  LITE_DONE from p
3393a 53 74 6d 74 2e 20 2a 2f 0a 0a 20 20 4c 65 61 66  Stmt. */..  Leaf
3393b 52 65 61 64 65 72 20 6c 65 61 66 52 65 61 64 65  Reader leafReade
3393c 72 3b 20 20 20 20 2f 2a 20 72 65 61 64 65 72 20  r;    /* reader 
3393d 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
3393e 6c 65 61 66 2e 20 2a 2f 0a 20 20 44 61 74 61 42  leaf. */.  DataB
3393f 75 66 66 65 72 20 72 6f 6f 74 44 61 74 61 3b 20  uffer rootData; 
33940 20 20 20 20 20 2f 2a 20 72 6f 6f 74 20 64 61 74       /* root dat
33941 61 20 66 6f 72 20 69 6e 6c 69 6e 65 2e 20 2a 2f  a for inline. */
33942 0a 7d 20 4c 65 61 76 65 73 52 65 61 64 65 72 3b  .} LeavesReader;
33943 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68 65 20  ../* Access the 
33944 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f  current term. */
33945 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
33946 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  esReaderTermByte
33947 73 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  s(LeavesReader *
33948 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
33949 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f  rt( !pReader->eo
3394a 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65  f );.  return le
3394b 61 66 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  afReaderTermByte
3394c 73 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66  s(&pReader->leaf
3394d 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69  Reader);.}.stati
3394e 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65  c const char *le
3394f 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 4c  avesReaderTerm(L
33950 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65  eavesReader *pRe
33951 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
33952 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29   !pReader->eof )
33953 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52  ;.  return leafR
33954 65 61 64 65 72 54 65 72 6d 28 26 70 52 65 61 64  eaderTerm(&pRead
33955 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b  er->leafReader);
33956 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68  .}../* Access th
33957 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66  e doclist data f
33958 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  or the current t
33959 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  erm. */.static i
3395a 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 44  nt leavesReaderD
3395b 61 74 61 42 79 74 65 73 28 4c 65 61 76 65 73 52  ataBytes(LeavesR
3395c 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
3395d 0a 20 20 61 73 73 65 72 74 28 20 21 70 52 65 61  .  assert( !pRea
3395e 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65  der->eof );.  re
3395f 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 44  turn leafReaderD
33960 61 74 61 42 79 74 65 73 28 26 70 52 65 61 64 65  ataBytes(&pReade
33961 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
33962 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  }.static const c
33963 68 61 72 20 2a 6c 65 61 76 65 73 52 65 61 64 65  har *leavesReade
33964 72 44 61 74 61 28 4c 65 61 76 65 73 52 65 61 64  rData(LeavesRead
33965 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
33966 61 73 73 65 72 74 28 20 21 70 52 65 61 64 65 72  assert( !pReader
33967 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72  ->eof );.  retur
33968 6e 20 6c 65 61 66 52 65 61 64 65 72 44 61 74 61  n leafReaderData
33969 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52  (&pReader->leafR
3396a 65 61 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69  eader);.}..stati
3396b 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
3396c 65 72 41 74 45 6e 64 28 4c 65 61 76 65 73 52 65  erAtEnd(LeavesRe
3396d 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
3396e 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
3396f 2d 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 6c 6f 61  ->eof;.}../* loa
33970 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 29  dSegmentLeaves()
33971 20 6d 61 79 20 6e 6f 74 20 72 65 61 64 20 61 6c   may not read al
33972 6c 20 74 68 65 20 77 61 79 20 74 6f 20 53 51 4c  l the way to SQL
33973 49 54 45 5f 44 4f 4e 45 2c 20 74 68 75 73 0a 2a  ITE_DONE, thus.*
33974 2a 20 6c 65 61 76 69 6e 67 20 74 68 65 20 73 74  * leaving the st
33975 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 6f  atement handle o
33976 70 65 6e 2c 20 77 68 69 63 68 20 6c 6f 63 6b 73  pen, which locks
33977 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 2f   the table..*/./
33978 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
33979 69 73 20 22 73 6f 6c 75 74 69 6f 6e 22 20 69 73  is "solution" is
3397a 20 6e 6f 74 20 73 61 74 69 73 66 61 63 74 6f 72   not satisfactor
3397b 79 2e 20 20 52 65 61 6c 6c 79 2c 20 74 68 65 72  y.  Really, ther
3397c 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63  e.** should be c
3397d 68 65 63 6b 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  heck-in function
3397e 20 66 6f 72 20 61 6c 6c 20 73 74 61 74 65 6d 65   for all stateme
3397f 6e 74 20 68 61 6e 64 6c 65 73 20 77 68 69 63 68  nt handles which
33980 0a 2a 2a 20 61 72 72 61 6e 67 65 73 20 74 6f 20  .** arranges to 
33981 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73  call sqlite3_res
33982 65 74 28 29 2e 20 20 54 68 69 73 20 6d 6f 73 74  et().  This most
33983 20 6c 69 6b 65 6c 79 20 77 69 6c 6c 20 72 65 71   likely will req
33984 75 69 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 63 61  uire.** modifica
33985 74 69 6f 6e 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  tion to control 
33986 66 6c 6f 77 20 61 6c 6c 20 6f 76 65 72 20 74 68  flow all over th
33987 65 20 70 6c 61 63 65 2c 20 74 68 6f 75 67 68 2c  e place, though,
33988 20 73 6f 20 66 6f 72 20 6e 6f 77 0a 2a 2a 20 6a   so for now.** j
33989 75 73 74 20 70 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20  ust punt..**.** 
3398a 4e 6f 74 65 20 74 68 65 20 74 68 65 20 63 75 72  Note the the cur
3398b 72 65 6e 74 20 73 79 73 74 65 6d 20 61 73 73 75  rent system assu
3398c 6d 65 73 20 74 68 61 74 20 73 65 67 6d 65 6e 74  mes that segment
3398d 20 6d 65 72 67 65 73 20 77 69 6c 6c 20 72 75 6e   merges will run
3398e 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f   to.** completio
3398f 6e 2c 20 77 68 69 63 68 20 69 73 20 77 68 79 20  n, which is why 
33990 74 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20  this particular 
33991 70 72 6f 62 61 62 6c 79 20 68 61 73 6e 27 74 20  probably hasn't 
33992 61 72 69 73 65 6e 20 69 6e 0a 2a 2a 20 74 68 69  arisen in.** thi
33993 73 20 63 61 73 65 2e 20 20 50 72 6f 62 61 62 6c  s case.  Probabl
33994 79 20 61 20 62 72 69 74 74 6c 65 20 61 73 73 75  y a brittle assu
33995 6d 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  mption..*/.stati
33996 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
33997 65 72 52 65 73 65 74 28 4c 65 61 76 65 73 52 65  erReset(LeavesRe
33998 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
33999 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3399a 5f 72 65 73 65 74 28 70 52 65 61 64 65 72 2d 3e  _reset(pReader->
3399b 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69  pStmt);.}..stati
3399c 63 20 76 6f 69 64 20 6c 65 61 76 65 73 52 65 61  c void leavesRea
3399d 64 65 72 44 65 73 74 72 6f 79 28 4c 65 61 76 65  derDestroy(Leave
3399e 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
3399f 29 7b 0a 20 20 2f 2a 20 49 66 20 69 64 78 20 69  ){.  /* If idx i
339a0 73 20 2d 31 2c 20 74 68 61 74 20 6d 65 61 6e 73  s -1, that means
339a1 20 77 65 27 72 65 20 75 73 69 6e 67 20 61 20 6e   we're using a n
339a2 6f 6e 2d 63 61 63 68 65 64 20 73 74 61 74 65 6d  on-cached statem
339a3 65 6e 74 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20  ent.  ** handle 
339a4 69 6e 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 28  in the optimize(
339a5 29 20 63 61 73 65 2c 20 73 6f 20 77 65 20 6e 65  ) case, so we ne
339a6 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74  ed to release it
339a7 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65  ..  */.  if( pRe
339a8 61 64 65 72 2d 3e 70 53 74 6d 74 21 3d 4e 55 4c  ader->pStmt!=NUL
339a9 4c 20 26 26 20 70 52 65 61 64 65 72 2d 3e 69 64  L && pReader->id
339aa 78 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c  x==-1 ){.    sql
339ab 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
339ac 65 61 64 65 72 2d 3e 70 53 74 6d 74 29 3b 0a 20  eader->pStmt);. 
339ad 20 7d 0a 20 20 6c 65 61 66 52 65 61 64 65 72 44   }.  leafReaderD
339ae 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d  estroy(&pReader-
339af 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20  >leafReader);.  
339b0 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
339b1 79 28 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  y(&pReader->root
339b2 44 61 74 61 29 3b 0a 20 20 53 43 52 41 4d 42 4c  Data);.  SCRAMBL
339b3 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f  E(pReader);.}../
339b4 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 52 65  * Initialize pRe
339b5 61 64 65 72 20 77 69 74 68 20 74 68 65 20 67 69  ader with the gi
339b6 76 65 6e 20 72 6f 6f 74 20 64 61 74 61 20 28 69  ven root data (i
339b7 66 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3d  f iStartBlockid=
339b8 3d 30 0a 2a 2a 20 74 68 65 20 6c 65 61 66 20 64  =0.** the leaf d
339b9 61 74 61 20 77 61 73 20 65 6e 74 69 72 65 6c 79  ata was entirely
339ba 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68   contained in th
339bb 65 20 72 6f 6f 74 29 2c 20 6f 72 20 66 72 6f 6d  e root), or from
339bc 20 74 68 65 0a 2a 2a 20 73 74 72 65 61 6d 20 6f   the.** stream o
339bd 66 20 62 6c 6f 63 6b 73 20 62 65 74 77 65 65 6e  f blocks between
339be 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 61   iStartBlockid a
339bf 6e 64 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20  nd iEndBlockid, 
339c0 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2f 0a 73 74  inclusive..*/.st
339c1 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52  atic int leavesR
339c2 65 61 64 65 72 49 6e 69 74 28 66 75 6c 6c 74 65  eaderInit(fullte
339c3 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
339c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
339c5 20 20 20 20 20 20 20 20 69 6e 74 20 69 64 78 2c          int idx,
339c6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
339c7 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
339c8 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74  ite_int64 iStart
339c9 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20  Blockid,.       
339ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
339cb 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
339cc 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20  4 iEndBlockid,. 
339cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
339ce 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
339cf 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61 74 61   char *pRootData
339d0 2c 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 2c  , int nRootData,
339d1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
339d2 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61               Lea
339d3 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
339d4 65 72 29 7b 0a 20 20 43 4c 45 41 52 28 70 52 65  er){.  CLEAR(pRe
339d5 61 64 65 72 29 3b 0a 20 20 70 52 65 61 64 65 72  ader);.  pReader
339d6 2d 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20 20  ->idx = idx;..  
339d7 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
339d8 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74  pReader->rootDat
339d9 61 2c 20 30 29 3b 0a 20 20 69 66 28 20 69 53 74  a, 0);.  if( iSt
339da 61 72 74 42 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b  artBlockid==0 ){
339db 0a 20 20 20 20 2f 2a 20 45 6e 74 69 72 65 20 6c  .    /* Entire l
339dc 65 61 66 20 6c 65 76 65 6c 20 66 69 74 20 69 6e  eaf level fit in
339dd 20 72 6f 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20   root data. */. 
339de 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70     dataBufferRep
339df 6c 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 72  lace(&pReader->r
339e0 6f 6f 74 44 61 74 61 2c 20 70 52 6f 6f 74 44 61  ootData, pRootDa
339e1 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 29 3b 0a  ta, nRootData);.
339e2 20 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e      leafReaderIn
339e3 69 74 28 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  it(pReader->root
339e4 44 61 74 61 2e 70 44 61 74 61 2c 20 70 52 65 61  Data.pData, pRea
339e5 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 6e 44  der->rootData.nD
339e6 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
339e7 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72          &pReader
339e8 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20  ->leafReader);. 
339e9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
339ea 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 20  te3_stmt *s;.   
339eb 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
339ec 74 5f 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e 74  t_leaf_statement
339ed 28 76 2c 20 69 64 78 2c 20 26 73 29 3b 0a 20 20  (v, idx, &s);.  
339ee 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
339ef 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
339f0 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
339f1 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
339f2 20 31 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69   1, iStartBlocki
339f3 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  d);.    if( rc!=
339f4 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
339f5 72 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d  rn rc;..    rc =
339f6 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
339f7 74 36 34 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c  t64(s, 2, iEndBl
339f8 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20  ockid);.    if( 
339f9 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
339fa 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
339fb 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
339fc 70 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  p(s);.    if( rc
339fd 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
339fe 0a 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e  .      pReader->
339ff 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  eof = 1;.      r
33a00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
33a01 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
33a02 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
33a03 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
33a04 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 20 3d  pReader->pStmt =
33a05 20 73 3b 0a 20 20 20 20 6c 65 61 66 52 65 61 64   s;.    leafRead
33a06 65 72 49 6e 69 74 28 73 71 6c 69 74 65 33 5f 63  erInit(sqlite3_c
33a07 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52 65 61 64  olumn_blob(pRead
33a08 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20  er->pStmt, 0),. 
33a09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33a0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
33a0b 5f 62 79 74 65 73 28 70 52 65 61 64 65 72 2d 3e  _bytes(pReader->
33a0c 70 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20  pStmt, 0),.     
33a0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
33a0e 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64  Reader->leafRead
33a0f 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  er);.  }.  retur
33a10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
33a11 2f 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 72  /* Step the curr
33a12 65 6e 74 20 6c 65 61 66 20 66 6f 72 77 61 72 64  ent leaf forward
33a13 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72   to the next ter
33a14 6d 2e 20 20 49 66 20 77 65 20 72 65 61 63 68 20  m.  If we reach 
33a15 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68  the.** end of th
33a16 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 2c 20  e current leaf, 
33a17 73 74 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20  step forward to 
33a18 74 68 65 20 6e 65 78 74 20 6c 65 61 66 20 62 6c  the next leaf bl
33a19 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ock..*/.static i
33a1a 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 53  nt leavesReaderS
33a1b 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  tep(fulltext_vta
33a1c 62 20 2a 76 2c 20 4c 65 61 76 65 73 52 65 61 64  b *v, LeavesRead
33a1d 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
33a1e 61 73 73 65 72 74 28 20 21 6c 65 61 76 65 73 52  assert( !leavesR
33a1f 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61 64  eaderAtEnd(pRead
33a20 65 72 29 20 29 3b 0a 20 20 6c 65 61 66 52 65 61  er) );.  leafRea
33a21 64 65 72 53 74 65 70 28 26 70 52 65 61 64 65 72  derStep(&pReader
33a22 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 0a  ->leafReader);..
33a23 20 20 69 66 28 20 6c 65 61 66 52 65 61 64 65 72    if( leafReader
33a24 41 74 45 6e 64 28 26 70 52 65 61 64 65 72 2d 3e  AtEnd(&pReader->
33a25 6c 65 61 66 52 65 61 64 65 72 29 20 29 7b 0a 20  leafReader) ){. 
33a26 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69     int rc;.    i
33a27 66 28 20 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  f( pReader->root
33a28 44 61 74 61 2e 70 44 61 74 61 20 29 7b 0a 20 20  Data.pData ){.  
33a29 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65 6f 66      pReader->eof
33a2a 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75   = 1;.      retu
33a2b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
33a2c 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
33a2d 69 74 65 33 5f 73 74 65 70 28 70 52 65 61 64 65  ite3_step(pReade
33a2e 72 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69  r->pStmt);.    i
33a2f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
33a30 57 20 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64  W ){.      pRead
33a31 65 72 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20  er->eof = 1;.   
33a32 20 20 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51     return rc==SQ
33a33 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
33a34 54 45 5f 4f 4b 20 3a 20 72 63 3b 0a 20 20 20 20  TE_OK : rc;.    
33a35 7d 0a 20 20 20 20 6c 65 61 66 52 65 61 64 65 72  }.    leafReader
33a36 44 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72  Destroy(&pReader
33a37 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20  ->leafReader);. 
33a38 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69     leafReaderIni
33a39 74 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t(sqlite3_column
33a3a 5f 62 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70  _blob(pReader->p
33a3b 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20  Stmt, 0),.      
33a3c 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
33a3d 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
33a3e 73 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  s(pReader->pStmt
33a3f 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20  , 0),.          
33a40 20 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65           &pReade
33a41 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
33a42 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
33a43 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4f 72  ITE_OK;.}../* Or
33a44 64 65 72 20 4c 65 61 76 65 73 52 65 61 64 65 72  der LeavesReader
33a45 73 20 62 79 20 74 68 65 69 72 20 74 65 72 6d 2c  s by their term,
33a46 20 69 67 6e 6f 72 69 6e 67 20 69 64 78 2e 20 20   ignoring idx.  
33a47 52 65 61 64 65 72 73 20 61 74 20 65 6f 66 0a 2a  Readers at eof.*
33a48 2a 20 61 6c 77 61 79 73 20 73 6f 72 74 20 74 6f  * always sort to
33a49 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
33a4a 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
33a4b 61 64 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 76  aderTermCmp(Leav
33a4c 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c  esReader *lr1, L
33a4d 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32  eavesReader *lr2
33a4e 29 7b 0a 20 20 69 66 28 20 6c 65 61 76 65 73 52  ){.  if( leavesR
33a4f 65 61 64 65 72 41 74 45 6e 64 28 6c 72 31 29 20  eaderAtEnd(lr1) 
33a50 29 7b 0a 20 20 20 20 69 66 28 20 6c 65 61 76 65  ){.    if( leave
33a51 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c 72 32  sReaderAtEnd(lr2
33a52 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
33a53 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
33a54 20 20 69 66 28 20 6c 65 61 76 65 73 52 65 61 64    if( leavesRead
33a55 65 72 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72  erAtEnd(lr2) ) r
33a56 65 74 75 72 6e 20 2d 31 3b 0a 0a 20 20 72 65 74  eturn -1;..  ret
33a57 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 54 65  urn leafReaderTe
33a58 72 6d 43 6d 70 28 26 6c 72 31 2d 3e 6c 65 61 66  rmCmp(&lr1->leaf
33a59 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20  Reader,.        
33a5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33a5b 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 54     leavesReaderT
33a5c 65 72 6d 28 6c 72 32 29 2c 20 6c 65 61 76 65 73  erm(lr2), leaves
33a5d 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
33a5e 6c 72 32 29 2c 0a 20 20 20 20 20 20 20 20 20 20  lr2),.          
33a5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33a60 20 30 29 3b 0a 7d 0a 0a 2f 2a 20 53 69 6d 69 6c   0);.}../* Simil
33a61 61 72 20 74 6f 20 6c 65 61 76 65 73 52 65 61 64  ar to leavesRead
33a62 65 72 54 65 72 6d 43 6d 70 28 29 2c 20 77 69 74  erTermCmp(), wit
33a63 68 20 61 64 64 69 74 69 6f 6e 61 6c 20 6f 72 64  h additional ord
33a64 65 72 69 6e 67 20 62 79 20 69 64 78 0a 2a 2a 20  ering by idx.** 
33a65 73 6f 20 74 68 61 74 20 6f 6c 64 65 72 20 73 65  so that older se
33a66 67 6d 65 6e 74 73 20 73 6f 72 74 20 62 65 66 6f  gments sort befo
33a67 72 65 20 6e 65 77 65 72 20 73 65 67 6d 65 6e 74  re newer segment
33a68 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
33a69 20 6c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70   leavesReaderCmp
33a6a 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c  (LeavesReader *l
33a6b 72 31 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72  r1, LeavesReader
33a6c 20 2a 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20   *lr2){.  int c 
33a6d 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  = leavesReaderTe
33a6e 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29 3b  rmCmp(lr1, lr2);
33a6f 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65  .  if( c!=0 ) re
33a70 74 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e  turn c;.  return
33a71 20 6c 72 31 2d 3e 69 64 78 2d 6c 72 32 2d 3e 69   lr1->idx-lr2->i
33a72 64 78 3b 0a 7d 0a 0a 2f 2a 20 41 73 73 75 6d 65  dx;.}../* Assume
33a73 20 74 68 61 74 20 70 4c 72 5b 31 5d 2e 2e 70 4c   that pLr[1]..pL
33a74 72 5b 6e 4c 72 5d 20 61 72 65 20 73 6f 72 74 65  r[nLr] are sorte
33a75 64 2e 20 20 42 75 62 62 6c 65 20 70 4c 72 5b 30  d.  Bubble pLr[0
33a76 5d 20 69 6e 74 6f 20 69 74 73 0a 2a 2a 20 73 6f  ] into its.** so
33a77 72 74 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a  rted position..*
33a78 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
33a79 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
33a7a 72 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  r(LeavesReader *
33a7b 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20  pLr, int nLr){. 
33a7c 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26   while( nLr>1 &&
33a7d 20 6c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70   leavesReaderCmp
33a7e 28 70 4c 72 2c 20 70 4c 72 2b 31 29 3e 30 20 29  (pLr, pLr+1)>0 )
33a7f 7b 0a 20 20 20 20 4c 65 61 76 65 73 52 65 61 64  {.    LeavesRead
33a80 65 72 20 74 6d 70 20 3d 20 70 4c 72 5b 30 5d 3b  er tmp = pLr[0];
33a81 0a 20 20 20 20 70 4c 72 5b 30 5d 20 3d 20 70 4c  .    pLr[0] = pL
33a82 72 5b 31 5d 3b 0a 20 20 20 20 70 4c 72 5b 31 5d  r[1];.    pLr[1]
33a83 20 3d 20 74 6d 70 3b 0a 20 20 20 20 6e 4c 72 2d   = tmp;.    nLr-
33a84 2d 3b 0a 20 20 20 20 70 4c 72 2b 2b 3b 0a 20 20  -;.    pLr++;.  
33a85 7d 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69  }.}../* Initiali
33a86 7a 65 73 20 70 52 65 61 64 65 72 73 20 77 69 74  zes pReaders wit
33a87 68 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66  h the segments f
33a88 72 6f 6d 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c  rom level iLevel
33a89 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 74  , returning.** t
33a8a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67  he number of seg
33a8b 6d 65 6e 74 73 20 69 6e 20 2a 70 69 52 65 61 64  ments in *piRead
33a8c 65 72 73 2e 20 20 4c 65 61 76 65 73 20 70 52 65  ers.  Leaves pRe
33a8d 61 64 65 72 73 20 69 6e 20 73 6f 72 74 65 64 0a  aders in sorted.
33a8e 2a 2a 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61  ** order..*/.sta
33a8f 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
33a90 61 64 65 72 73 49 6e 69 74 28 66 75 6c 6c 74 65  adersInit(fullte
33a91 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20  xt_vtab *v, int 
33a92 69 4c 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20  iLevel,.        
33a93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33a94 20 20 20 20 20 4c 65 61 76 65 73 52 65 61 64 65       LeavesReade
33a95 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
33a96 20 2a 70 69 52 65 61 64 65 72 73 29 7b 0a 20 20   *piReaders){.  
33a97 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
33a98 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 73  .  int i, rc = s
33a99 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
33a9a 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
33a9b 54 5f 4c 45 56 45 4c 5f 53 54 4d 54 2c 20 26 73  T_LEVEL_STMT, &s
33a9c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
33a9d 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
33a9e 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
33a9f 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20  te3_bind_int(s, 
33aa0 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66  1, iLevel);.  if
33aa1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
33aa2 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
33aa3 69 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  i = 0;.  while( 
33aa4 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74  (rc = sqlite3_st
33aa5 65 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(s))==SQLITE_R
33aa6 4f 57 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OW ){.    sqlite
33aa7 5f 69 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20  _int64 iStart = 
33aa8 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
33aa9 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20  nt64(s, 0);.    
33aaa 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e  sqlite_int64 iEn
33aab 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
33aac 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
33aad 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
33aae 70 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69  pRootData = sqli
33aaf 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
33ab0 73 2c 20 32 29 3b 0a 20 20 20 20 69 6e 74 20 6e  s, 2);.    int n
33ab1 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74  RootData = sqlit
33ab2 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
33ab3 73 2c 20 32 29 3b 0a 0a 20 20 20 20 61 73 73 65  s, 2);..    asse
33ab4 72 74 28 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  rt( i<MERGE_COUN
33ab5 54 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 6c 65  T );.    rc = le
33ab6 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 76  avesReaderInit(v
33ab7 2c 20 69 2c 20 69 53 74 61 72 74 2c 20 69 45 6e  , i, iStart, iEn
33ab8 64 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52  d, pRootData, nR
33ab9 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20 20 20 20  ootData,.       
33aba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33abb 20 20 20 26 70 52 65 61 64 65 72 73 5b 69 5d 29     &pReaders[i])
33abc 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
33abd 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b  LITE_OK ) break;
33abe 0a 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20  ..    i++;.  }. 
33abf 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
33ac0 44 4f 4e 45 20 29 7b 0a 20 20 20 20 77 68 69 6c  DONE ){.    whil
33ac1 65 28 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20  e( i-->0 ){.    
33ac2 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65    leavesReaderDe
33ac3 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 73 5b  stroy(&pReaders[
33ac4 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  i]);.    }.    r
33ac5 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
33ac6 20 2a 70 69 52 65 61 64 65 72 73 20 3d 20 69 3b   *piReaders = i;
33ac7 0a 0a 20 20 2f 2a 20 4c 65 61 76 65 20 6f 75 72  ..  /* Leave our
33ac8 20 72 65 73 75 6c 74 73 20 73 6f 72 74 65 64 20   results sorted 
33ac9 62 79 20 74 65 72 6d 2c 20 74 68 65 6e 20 61 67  by term, then ag
33aca 65 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69  e. */.  while( i
33acb 2d 2d 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 73  -- ){.    leaves
33acc 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 70 52  ReaderReorder(pR
33acd 65 61 64 65 72 73 2b 69 2c 20 2a 70 69 52 65 61  eaders+i, *piRea
33ace 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 20 20 72  ders-i);.  }.  r
33acf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
33ad0 0a 7d 0a 0a 2f 2a 20 4d 65 72 67 65 20 64 6f 63  .}../* Merge doc
33ad1 6c 69 73 74 73 20 66 72 6f 6d 20 70 52 65 61 64  lists from pRead
33ad2 65 72 73 5b 6e 52 65 61 64 65 72 73 5d 20 69 6e  ers[nReaders] in
33ad3 74 6f 20 61 20 73 69 6e 67 6c 65 20 64 6f 63 6c  to a single docl
33ad4 69 73 74 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73  ist, which.** is
33ad5 20 77 72 69 74 74 65 6e 20 74 6f 20 70 57 72 69   written to pWri
33ad6 74 65 72 2e 20 20 41 73 73 75 6d 65 73 20 70 52  ter.  Assumes pR
33ad7 65 61 64 65 72 73 20 69 73 20 6f 72 64 65 72 65  eaders is ordere
33ad8 64 20 6f 6c 64 65 73 74 20 74 6f 0a 2a 2a 20 6e  d oldest to.** n
33ad9 65 77 65 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  ewest..*/./* TOD
33ada 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65  O(shess) Conside
33adb 72 20 70 75 74 74 69 6e 67 20 74 68 69 73 20 69  r putting this i
33adc 6e 6c 69 6e 65 20 69 6e 20 73 65 67 6d 65 6e 74  nline in segment
33add 4d 65 72 67 65 28 29 2e 20 2a 2f 0a 73 74 61 74  Merge(). */.stat
33ade 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
33adf 64 65 72 73 4d 65 72 67 65 28 66 75 6c 6c 74 65  dersMerge(fullte
33ae0 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
33ae1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ae2 20 20 20 20 20 20 20 20 20 20 4c 65 61 76 65 73            Leaves
33ae3 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 73  Reader *pReaders
33ae4 2c 20 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a  , int nReaders,.
33ae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ae6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65                Le
33ae7 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  afWriter *pWrite
33ae8 72 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64  r){.  DLReader d
33ae9 6c 52 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43  lReaders[MERGE_C
33aea 4f 55 4e 54 5d 3b 0a 20 20 63 6f 6e 73 74 20 63  OUNT];.  const c
33aeb 68 61 72 20 2a 70 54 65 72 6d 20 3d 20 6c 65 61  har *pTerm = lea
33aec 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 70 52  vesReaderTerm(pR
33aed 65 61 64 65 72 73 29 3b 0a 20 20 69 6e 74 20 69  eaders);.  int i
33aee 2c 20 6e 54 65 72 6d 20 3d 20 6c 65 61 76 65 73  , nTerm = leaves
33aef 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
33af0 70 52 65 61 64 65 72 73 29 3b 0a 0a 20 20 61 73  pReaders);..  as
33af1 73 65 72 74 28 20 6e 52 65 61 64 65 72 73 3c 3d  sert( nReaders<=
33af2 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 0a  MERGE_COUNT );..
33af3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65    for(i=0; i<nRe
33af4 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  aders; i++){.   
33af5 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64   dlrInit(&dlRead
33af6 65 72 73 5b 69 5d 2c 20 44 4c 5f 44 45 46 41 55  ers[i], DL_DEFAU
33af7 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LT,.            
33af8 6c 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61  leavesReaderData
33af9 28 70 52 65 61 64 65 72 73 2b 69 29 2c 0a 20 20  (pReaders+i),.  
33afa 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73            leaves
33afb 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73 28  ReaderDataBytes(
33afc 70 52 65 61 64 65 72 73 2b 69 29 29 3b 0a 20 20  pReaders+i));.  
33afd 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66  }..  return leaf
33afe 57 72 69 74 65 72 53 74 65 70 4d 65 72 67 65 28  WriterStepMerge(
33aff 76 2c 20 70 57 72 69 74 65 72 2c 20 70 54 65 72  v, pWriter, pTer
33b00 6d 2c 20 6e 54 65 72 6d 2c 20 64 6c 52 65 61 64  m, nTerm, dlRead
33b01 65 72 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a  ers, nReaders);.
33b02 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  }../* Forward re
33b03 66 20 64 75 65 20 74 6f 20 6d 75 74 75 61 6c 20  f due to mutual 
33b04 72 65 63 75 72 73 69 6f 6e 20 77 69 74 68 20 73  recursion with s
33b05 65 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 29  egdirNextIndex()
33b06 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
33b07 73 65 67 6d 65 6e 74 4d 65 72 67 65 28 66 75 6c  segmentMerge(ful
33b08 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
33b09 6e 74 20 69 4c 65 76 65 6c 29 3b 0a 0a 2f 2a 20  nt iLevel);../* 
33b0a 50 75 74 20 74 68 65 20 6e 65 78 74 20 61 76 61  Put the next ava
33b0b 69 6c 61 62 6c 65 20 69 6e 64 65 78 20 61 74 20  ilable index at 
33b0c 69 4c 65 76 65 6c 20 69 6e 74 6f 20 2a 70 69 64  iLevel into *pid
33b0d 78 2e 20 20 49 66 20 69 4c 65 76 65 6c 0a 2a 2a  x.  If iLevel.**
33b0e 20 61 6c 72 65 61 64 79 20 68 61 73 20 4d 45 52   already has MER
33b0f 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74  GE_COUNT segment
33b10 73 2c 20 74 68 65 79 20 61 72 65 20 6d 65 72 67  s, they are merg
33b11 65 64 20 74 6f 20 61 20 68 69 67 68 65 72 0a 2a  ed to a higher.*
33b12 2a 20 6c 65 76 65 6c 20 74 6f 20 6d 61 6b 65 20  * level to make 
33b13 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  room..*/.static 
33b14 69 6e 74 20 73 65 67 64 69 72 4e 65 78 74 49 6e  int segdirNextIn
33b15 64 65 78 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  dex(fulltext_vta
33b16 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c  b *v, int iLevel
33b17 2c 20 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20  , int *pidx){.  
33b18 69 6e 74 20 72 63 20 3d 20 73 65 67 64 69 72 5f  int rc = segdir_
33b19 6d 61 78 5f 69 6e 64 65 78 28 76 2c 20 69 4c 65  max_index(v, iLe
33b1a 76 65 6c 2c 20 70 69 64 78 29 3b 0a 20 20 69 66  vel, pidx);.  if
33b1b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
33b1c 45 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  E ){            
33b1d 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73    /* No segments
33b1e 20 61 74 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20   at iLevel. */. 
33b1f 20 20 20 2a 70 69 64 78 20 3d 20 30 3b 0a 20 20     *pidx = 0;.  
33b20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51  }else if( rc==SQ
33b21 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
33b22 69 66 28 20 2a 70 69 64 78 3d 3d 28 4d 45 52 47  if( *pidx==(MERG
33b23 45 5f 43 4f 55 4e 54 2d 31 29 20 29 7b 0a 20 20  E_COUNT-1) ){.  
33b24 20 20 20 20 72 63 20 3d 20 73 65 67 6d 65 6e 74      rc = segment
33b25 4d 65 72 67 65 28 76 2c 20 69 4c 65 76 65 6c 29  Merge(v, iLevel)
33b26 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
33b27 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
33b28 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 2a 70 69  rn rc;.      *pi
33b29 64 78 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  dx = 0;.    }els
33b2a 65 7b 0a 20 20 20 20 20 20 28 2a 70 69 64 78 29  e{.      (*pidx)
33b2b 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ++;.    }.  }els
33b2c 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  e{.    return rc
33b2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
33b2e 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
33b2f 4d 65 72 67 65 20 4d 45 52 47 45 5f 43 4f 55 4e  Merge MERGE_COUN
33b30 54 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c  T segments at iL
33b31 65 76 65 6c 20 69 6e 74 6f 20 61 20 6e 65 77 20  evel into a new 
33b32 73 65 67 6d 65 6e 74 20 61 74 0a 2a 2a 20 69 4c  segment at.** iL
33b33 65 76 65 6c 2b 31 2e 20 20 49 66 20 69 4c 65 76  evel+1.  If iLev
33b34 65 6c 2b 31 20 69 73 20 61 6c 72 65 61 64 79 20  el+1 is already 
33b35 66 75 6c 6c 20 6f 66 20 73 65 67 6d 65 6e 74 73  full of segments
33b36 2c 20 74 68 6f 73 65 20 77 69 6c 6c 20 62 65 0a  , those will be.
33b37 2a 2a 20 6d 65 72 67 65 64 20 74 6f 20 6d 61 6b  ** merged to mak
33b38 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69  e room..*/.stati
33b39 63 20 69 6e 74 20 73 65 67 6d 65 6e 74 4d 65 72  c int segmentMer
33b3a 67 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ge(fulltext_vtab
33b3b 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29   *v, int iLevel)
33b3c 7b 0a 20 20 4c 65 61 66 57 72 69 74 65 72 20 77  {.  LeafWriter w
33b3d 72 69 74 65 72 3b 0a 20 20 4c 65 61 76 65 73 52  riter;.  LeavesR
33b3e 65 61 64 65 72 20 6c 72 73 5b 4d 45 52 47 45 5f  eader lrs[MERGE_
33b3f 43 4f 55 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 2c  COUNT];.  int i,
33b40 20 72 63 2c 20 69 64 78 20 3d 20 30 3b 0a 0a 20   rc, idx = 0;.. 
33b41 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68   /* Determine th
33b42 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c 65  e next available
33b43 20 73 65 67 6d 65 6e 74 20 69 6e 64 65 78 20 61   segment index a
33b44 74 20 74 68 65 20 6e 65 78 74 20 6c 65 76 65 6c  t the next level
33b45 2c 0a 20 20 2a 2a 20 6d 65 72 67 69 6e 67 20 61  ,.  ** merging a
33b46 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a  s necessary..  *
33b47 2f 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72 4e  /.  rc = segdirN
33b48 65 78 74 49 6e 64 65 78 28 76 2c 20 69 4c 65 76  extIndex(v, iLev
33b49 65 6c 2b 31 2c 20 26 69 64 78 29 3b 0a 20 20 69  el+1, &idx);.  i
33b4a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
33b4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
33b4c 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
33b4d 54 68 69 73 20 61 73 73 75 6d 65 73 20 74 68 61  This assumes tha
33b4e 74 20 77 65 27 6c 6c 20 61 6c 77 61 79 73 20 73  t we'll always s
33b4f 65 65 20 65 78 61 63 74 6c 79 0a 20 20 2a 2a 20  ee exactly.  ** 
33b50 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d  MERGE_COUNT segm
33b51 65 6e 74 73 20 74 6f 20 6d 65 72 67 65 20 61 74  ents to merge at
33b52 20 61 20 67 69 76 65 6e 20 6c 65 76 65 6c 2e 20   a given level. 
33b53 20 54 68 61 74 20 77 69 6c 6c 20 62 65 0a 20 20   That will be.  
33b54 2a 2a 20 62 72 6f 6b 65 6e 20 69 66 20 77 65 20  ** broken if we 
33b55 61 6c 6c 6f 77 20 74 68 65 20 64 65 76 65 6c 6f  allow the develo
33b56 70 65 72 20 74 6f 20 72 65 71 75 65 73 74 20 70  per to request p
33b57 72 65 65 6d 70 74 69 76 65 20 6f 72 0a 20 20 2a  reemptive or.  *
33b58 2a 20 64 65 66 65 72 72 65 64 20 6d 65 72 67 69  * deferred mergi
33b59 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65  ng..  */.  memse
33b5a 74 28 26 6c 72 73 2c 20 27 5c 30 27 2c 20 73 69  t(&lrs, '\0', si
33b5b 7a 65 6f 66 28 6c 72 73 29 29 3b 0a 20 20 72 63  zeof(lrs));.  rc
33b5c 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 73   = leavesReaders
33b5d 49 6e 69 74 28 76 2c 20 69 4c 65 76 65 6c 2c 20  Init(v, iLevel, 
33b5e 6c 72 73 2c 20 26 69 29 3b 0a 20 20 69 66 28 20  lrs, &i);.  if( 
33b5f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
33b60 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73  return rc;.  ass
33b61 65 72 74 28 20 69 3d 3d 4d 45 52 47 45 5f 43 4f  ert( i==MERGE_CO
33b62 55 4e 54 20 29 3b 0a 0a 20 20 6c 65 61 66 57 72  UNT );..  leafWr
33b63 69 74 65 72 49 6e 69 74 28 69 4c 65 76 65 6c 2b  iterInit(iLevel+
33b64 31 2c 20 69 64 78 2c 20 26 77 72 69 74 65 72 29  1, idx, &writer)
33b65 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 6c 65  ;..  /* Since le
33b66 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
33b67 72 28 29 20 70 75 73 68 65 73 20 72 65 61 64 65  r() pushes reade
33b68 72 73 20 61 74 20 65 6f 66 20 74 6f 20 74 68 65  rs at eof to the
33b69 20 65 6e 64 2c 0a 20 20 2a 2a 20 77 68 65 6e 20   end,.  ** when 
33b6a 74 68 65 20 66 69 72 73 74 20 72 65 61 64 65 72  the first reader
33b6b 20 69 73 20 65 6d 70 74 79 2c 20 61 6c 6c 20 77   is empty, all w
33b6c 69 6c 6c 20 62 65 20 65 6d 70 74 79 2e 0a 20 20  ill be empty..  
33b6d 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21 6c 65 61  */.  while( !lea
33b6e 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c  vesReaderAtEnd(l
33b6f 72 73 29 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69  rs) ){.    /* Fi
33b70 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
33b71 79 20 72 65 61 64 65 72 73 20 73 68 61 72 65 20  y readers share 
33b72 74 68 65 69 72 20 6e 65 78 74 20 74 65 72 6d 2e  their next term.
33b73 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   */.    for(i=1;
33b74 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20 26   i<MERGE_COUNT &
33b75 26 20 21 6c 65 61 76 65 73 52 65 61 64 65 72 41  & !leavesReaderA
33b76 74 45 6e 64 28 6c 72 73 2b 69 29 3b 20 69 2b 2b  tEnd(lrs+i); i++
33b77 29 7b 0a 20 20 20 20 20 20 69 66 28 20 30 21 3d  ){.      if( 0!=
33b78 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
33b79 43 6d 70 28 6c 72 73 2c 20 6c 72 73 2b 69 29 20  Cmp(lrs, lrs+i) 
33b7a 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a  ) break;.    }..
33b7b 20 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52      rc = leavesR
33b7c 65 61 64 65 72 73 4d 65 72 67 65 28 76 2c 20 6c  eadersMerge(v, l
33b7d 72 73 2c 20 69 2c 20 26 77 72 69 74 65 72 29 3b  rs, i, &writer);
33b7e 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
33b7f 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
33b80 72 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20  r;..    /* Step 
33b81 66 6f 72 77 61 72 64 20 74 68 6f 73 65 20 74 68  forward those th
33b82 61 74 20 77 65 72 65 20 6d 65 72 67 65 64 2e 20  at were merged. 
33b83 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d  */.    while( i-
33b84 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ->0 ){.      rc 
33b85 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74  = leavesReaderSt
33b86 65 70 28 76 2c 20 6c 72 73 2b 69 29 3b 0a 20 20  ep(v, lrs+i);.  
33b87 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
33b88 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
33b89 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6f 72  ;..      /* Reor
33b8a 64 65 72 20 62 79 20 74 65 72 6d 2c 20 74 68 65  der by term, the
33b8b 6e 20 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 20  n by age. */.   
33b8c 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 52     leavesReaderR
33b8d 65 6f 72 64 65 72 28 6c 72 73 2b 69 2c 20 4d 45  eorder(lrs+i, ME
33b8e 52 47 45 5f 43 4f 55 4e 54 2d 69 29 3b 0a 20 20  RGE_COUNT-i);.  
33b8f 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69    }.  }..  for(i
33b90 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  =0; i<MERGE_COUN
33b91 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61  T; i++){.    lea
33b92 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
33b93 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 0a  (&lrs[i]);.  }..
33b94 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
33b95 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72  rFinalize(v, &wr
33b96 69 74 65 72 29 3b 0a 20 20 6c 65 61 66 57 72 69  iter);.  leafWri
33b97 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69 74  terDestroy(&writ
33b98 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  er);.  if( rc!=S
33b99 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
33b9a 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65  n rc;..  /* Dele
33b9b 74 65 20 74 68 65 20 6d 65 72 67 65 64 20 73 65  te the merged se
33b9c 67 6d 65 6e 74 20 64 61 74 61 2e 20 2a 2f 0a 20  gment data. */. 
33b9d 20 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f 64   return segdir_d
33b9e 65 6c 65 74 65 28 76 2c 20 69 4c 65 76 65 6c 29  elete(v, iLevel)
33b9f 3b 0a 0a 20 65 72 72 3a 0a 20 20 66 6f 72 28 69  ;.. err:.  for(i
33ba0 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  =0; i<MERGE_COUN
33ba1 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61  T; i++){.    lea
33ba2 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
33ba3 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20  (&lrs[i]);.  }. 
33ba4 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72   leafWriterDestr
33ba5 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 72  oy(&writer);.  r
33ba6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
33ba7 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 75  Accumulate the u
33ba8 6e 69 6f 6e 20 6f 66 20 2a 61 63 63 20 61 6e 64  nion of *acc and
33ba9 20 2a 70 44 61 74 61 20 69 6e 74 6f 20 2a 61 63   *pData into *ac
33baa 63 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  c. */.static voi
33bab 64 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c  d docListAccumul
33bac 61 74 65 55 6e 69 6f 6e 28 44 61 74 61 42 75 66  ateUnion(DataBuf
33bad 66 65 72 20 2a 61 63 63 2c 0a 20 20 20 20 20 20  fer *acc,.      
33bae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33baf 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
33bb0 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
33bb1 69 6e 74 20 6e 44 61 74 61 29 20 7b 0a 20 20 44  int nData) {.  D
33bb2 61 74 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20  ataBuffer tmp = 
33bb3 2a 61 63 63 3b 0a 20 20 64 61 74 61 42 75 66 66  *acc;.  dataBuff
33bb4 65 72 49 6e 69 74 28 61 63 63 2c 20 74 6d 70 2e  erInit(acc, tmp.
33bb5 6e 44 61 74 61 2b 6e 44 61 74 61 29 3b 0a 20 20  nData+nData);.  
33bb6 64 6f 63 4c 69 73 74 55 6e 69 6f 6e 28 74 6d 70  docListUnion(tmp
33bb7 2e 70 44 61 74 61 2c 20 74 6d 70 2e 6e 44 61 74  .pData, tmp.nDat
33bb8 61 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  a, pData, nData,
33bb9 20 61 63 63 29 3b 0a 20 20 64 61 74 61 42 75 66   acc);.  dataBuf
33bba 66 65 72 44 65 73 74 72 6f 79 28 26 74 6d 70 29  ferDestroy(&tmp)
33bbb 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ;.}../* TODO(she
33bbc 73 73 29 20 49 74 20 6d 69 67 68 74 20 62 65 20  ss) It might be 
33bbd 69 6e 74 65 72 65 73 74 69 6e 67 20 74 6f 20 65  interesting to e
33bbe 78 70 6c 6f 72 65 20 64 69 66 66 65 72 65 6e 74  xplore different
33bbf 20 6d 65 72 67 65 0a 2a 2a 20 73 74 72 61 74 65   merge.** strate
33bc0 67 69 65 73 2c 20 68 65 72 65 2e 20 20 46 6f 72  gies, here.  For
33bc1 20 69 6e 73 74 61 6e 63 65 2c 20 73 69 6e 63 65   instance, since
33bc2 20 74 68 69 73 20 69 73 20 61 20 73 6f 72 74 65   this is a sorte
33bc3 64 20 6d 65 72 67 65 2c 20 77 65 0a 2a 2a 20 63  d merge, we.** c
33bc4 6f 75 6c 64 20 65 61 73 69 6c 79 20 6d 65 72 67  ould easily merg
33bc5 65 20 6d 61 6e 79 20 64 6f 63 6c 69 73 74 73 20  e many doclists 
33bc6 69 6e 20 70 61 72 61 6c 6c 65 6c 2e 20 20 57 69  in parallel.  Wi
33bc7 74 68 20 73 6f 6d 65 0a 2a 2a 20 63 6f 6d 70 72  th some.** compr
33bc8 65 68 65 6e 73 69 6f 6e 20 6f 66 20 74 68 65 20  ehension of the 
33bc9 73 74 6f 72 61 67 65 20 66 6f 72 6d 61 74 2c 20  storage format, 
33bca 77 65 20 63 6f 75 6c 64 20 6d 65 72 67 65 20 61  we could merge a
33bcb 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 64 6f 63  ll of the.** doc
33bcc 6c 69 73 74 73 20 77 69 74 68 69 6e 20 61 20 6c  lists within a l
33bcd 65 61 66 20 6e 6f 64 65 20 64 69 72 65 63 74 6c  eaf node directl
33bce 79 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20  y from the leaf 
33bcf 6e 6f 64 65 27 73 20 73 74 6f 72 61 67 65 2e 0a  node's storage..
33bd0 2a 2a 20 49 74 20 6d 61 79 20 62 65 20 77 6f 72  ** It may be wor
33bd1 74 68 77 68 69 6c 65 20 74 6f 20 6d 65 72 67 65  thwhile to merge
33bd2 20 73 6d 61 6c 6c 65 72 20 64 6f 63 6c 69 73 74   smaller doclist
33bd3 73 20 62 65 66 6f 72 65 20 6c 61 72 67 65 72 0a  s before larger.
33bd4 2a 2a 20 64 6f 63 6c 69 73 74 73 2c 20 73 69 6e  ** doclists, sin
33bd5 63 65 20 74 68 65 79 20 63 61 6e 20 62 65 20 74  ce they can be t
33bd6 72 61 76 65 72 73 65 64 20 6d 6f 72 65 20 71 75  raversed more qu
33bd7 69 63 6b 6c 79 20 2d 20 62 75 74 20 74 68 65 0a  ickly - but the.
33bd8 2a 2a 20 72 65 73 75 6c 74 73 20 6d 61 79 20 68  ** results may h
33bd9 61 76 65 20 6c 65 73 73 20 6f 76 65 72 6c 61 70  ave less overlap
33bda 2c 20 6d 61 6b 69 6e 67 20 74 68 65 6d 20 6d 6f  , making them mo
33bdb 72 65 20 65 78 70 65 6e 73 69 76 65 20 69 6e 20  re expensive in 
33bdc 61 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 77  a.** different w
33bdd 61 79 2e 0a 2a 2f 0a 0a 2f 2a 20 53 63 61 6e 20  ay..*/../* Scan 
33bde 70 52 65 61 64 65 72 20 66 6f 72 20 70 54 65 72  pReader for pTer
33bdf 6d 2f 6e 54 65 72 6d 2c 20 61 6e 64 20 6d 65 72  m/nTerm, and mer
33be0 67 65 20 74 68 65 20 74 65 72 6d 27 73 20 64 6f  ge the term's do
33be1 63 6c 69 73 74 20 6f 76 65 72 0a 2a 2a 20 2a 6f  clist over.** *o
33be2 75 74 20 28 61 6e 79 20 64 6f 63 6c 69 73 74 73  ut (any doclists
33be3 20 77 69 74 68 20 64 75 70 6c 69 63 61 74 65 20   with duplicate 
33be4 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69 74 65  docids overwrite
33be5 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29 2e   those in *out).
33be6 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e  .** Internal fun
33be7 63 74 69 6f 6e 20 66 6f 72 20 6c 6f 61 64 53 65  ction for loadSe
33be8 67 6d 65 6e 74 4c 65 61 66 28 29 2e 0a 2a 2f 0a  gmentLeaf()..*/.
33be9 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53  static int loadS
33bea 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28  egmentLeavesInt(
33beb 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
33bec 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  , LeavesReader *
33bed 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20  pReader,.       
33bee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33bef 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
33bf0 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
33bf1 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65  nTerm, int isPre
33bf2 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fix,.           
33bf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33bf4 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
33bf5 2a 6f 75 74 29 7b 0a 20 20 2f 2a 20 64 6f 63 6c  *out){.  /* docl
33bf6 69 73 74 20 64 61 74 61 20 69 73 20 61 63 63 75  ist data is accu
33bf7 6d 75 6c 61 74 65 64 20 69 6e 74 6f 20 70 42 75  mulated into pBu
33bf8 66 66 65 72 73 20 73 69 6d 69 6c 61 72 20 74 6f  ffers similar to
33bf9 20 68 6f 77 20 6f 6e 65 20 64 6f 65 73 0a 20 20   how one does.  
33bfa 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 69 6e 20  ** increment in 
33bfb 62 69 6e 61 72 79 20 61 72 69 74 68 6d 65 74 69  binary arithmeti
33bfc 63 2e 20 20 49 66 20 69 6e 64 65 78 20 30 20 69  c.  If index 0 i
33bfd 73 20 65 6d 70 74 79 2c 20 74 68 65 20 64 61 74  s empty, the dat
33bfe 61 20 69 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64  a is.  ** stored
33bff 20 74 68 65 72 65 2e 20 20 49 66 20 74 68 65 72   there.  If ther
33c00 65 20 69 73 20 64 61 74 61 20 74 68 65 72 65 2c  e is data there,
33c01 20 69 74 20 69 73 20 6d 65 72 67 65 64 20 61 6e   it is merged an
33c02 64 20 74 68 65 0a 20 20 2a 2a 20 72 65 73 75 6c  d the.  ** resul
33c03 74 73 20 63 61 72 72 69 65 64 20 69 6e 74 6f 20  ts carried into 
33c04 70 6f 73 69 74 69 6f 6e 20 31 2c 20 77 69 74 68  position 1, with
33c05 20 66 75 72 74 68 65 72 20 6d 65 72 67 65 2d 61   further merge-a
33c06 6e 64 2d 63 61 72 72 79 0a 20 20 2a 2a 20 75 6e  nd-carry.  ** un
33c07 74 69 6c 20 61 6e 20 65 6d 70 74 79 20 70 6f 73  til an empty pos
33c08 69 74 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2e 0a  ition is found..
33c09 20 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65    */.  DataBuffe
33c0a 72 20 2a 70 42 75 66 66 65 72 73 20 3d 20 4e 55  r *pBuffers = NU
33c0b 4c 4c 3b 0a 20 20 69 6e 74 20 6e 42 75 66 66 65  LL;.  int nBuffe
33c0c 72 73 20 3d 20 30 2c 20 6e 4d 61 78 42 75 66 66  rs = 0, nMaxBuff
33c0d 65 72 73 20 3d 20 30 2c 20 72 63 3b 0a 0a 20 20  ers = 0, rc;..  
33c0e 61 73 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20  assert( nTerm>0 
33c0f 29 3b 0a 0a 20 20 66 6f 72 28 72 63 3d 53 51 4c  );..  for(rc=SQL
33c10 49 54 45 5f 4f 4b 3b 20 72 63 3d 3d 53 51 4c 49  ITE_OK; rc==SQLI
33c11 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76 65 73  TE_OK && !leaves
33c12 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61  ReaderAtEnd(pRea
33c13 64 65 72 29 3b 0a 20 20 20 20 20 20 72 63 3d 6c  der);.      rc=l
33c14 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28  eavesReaderStep(
33c15 76 2c 20 70 52 65 61 64 65 72 29 29 7b 0a 20 20  v, pReader)){.  
33c16 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
33c17 20 52 65 61 6c 6c 79 20 77 61 6e 74 20 6c 65 61   Really want lea
33c18 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70  vesReaderTermCmp
33c19 28 29 2c 20 62 75 74 20 74 68 61 74 20 6e 61 6d  (), but that nam
33c1a 65 20 69 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65  e is.    ** alre
33c1b 61 64 79 20 74 61 6b 65 6e 20 74 6f 20 63 6f 6d  ady taken to com
33c1c 70 61 72 65 20 74 68 65 20 74 65 72 6d 73 20 6f  pare the terms o
33c1d 66 20 74 77 6f 20 4c 65 61 76 65 73 52 65 61 64  f two LeavesRead
33c1e 65 72 73 2e 20 20 54 68 69 6e 6b 0a 20 20 20 20  ers.  Think.    
33c1f 2a 2a 20 6f 6e 20 61 20 62 65 74 74 65 72 20 6e  ** on a better n
33c20 61 6d 65 2e 20 20 5b 4d 65 61 6e 77 68 69 6c 65  ame.  [Meanwhile
33c21 2c 20 62 72 65 61 6b 20 65 6e 63 61 70 73 75 6c  , break encapsul
33c22 61 74 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61  ation rather tha
33c23 6e 0a 20 20 20 20 2a 2a 20 75 73 65 20 61 20 63  n.    ** use a c
33c24 6f 6e 66 75 73 69 6e 67 20 6e 61 6d 65 2e 5d 0a  onfusing name.].
33c25 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63      */.    int c
33c26 20 3d 20 6c 65 61 66 52 65 61 64 65 72 54 65 72   = leafReaderTer
33c27 6d 43 6d 70 28 26 70 52 65 61 64 65 72 2d 3e 6c  mCmp(&pReader->l
33c28 65 61 66 52 65 61 64 65 72 2c 20 70 54 65 72 6d  eafReader, pTerm
33c29 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
33c2a 78 29 3b 0a 20 20 20 20 69 66 28 20 63 3e 30 20  x);.    if( c>0 
33c2b 29 20 62 72 65 61 6b 3b 20 20 20 20 20 20 2f 2a  ) break;      /*
33c2c 20 50 61 73 74 20 61 6e 79 20 70 6f 73 73 69 62   Past any possib
33c2d 6c 65 20 6d 61 74 63 68 65 73 2e 20 2a 2f 0a 20  le matches. */. 
33c2e 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20     if( c==0 ){. 
33c2f 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
33c30 2a 70 44 61 74 61 20 3d 20 6c 65 61 76 65 73 52  *pData = leavesR
33c31 65 61 64 65 72 44 61 74 61 28 70 52 65 61 64 65  eaderData(pReade
33c32 72 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 42  r);.      int iB
33c33 75 66 66 65 72 2c 20 6e 44 61 74 61 20 3d 20 6c  uffer, nData = l
33c34 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42  eavesReaderDataB
33c35 79 74 65 73 28 70 52 65 61 64 65 72 29 3b 0a 0a  ytes(pReader);..
33c36 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68        /* Find th
33c37 65 20 66 69 72 73 74 20 65 6d 70 74 79 20 62 75  e first empty bu
33c38 66 66 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 66  ffer. */.      f
33c39 6f 72 28 69 42 75 66 66 65 72 3d 30 3b 20 69 42  or(iBuffer=0; iB
33c3a 75 66 66 65 72 3c 6e 42 75 66 66 65 72 73 3b 20  uffer<nBuffers; 
33c3b 2b 2b 69 42 75 66 66 65 72 29 7b 0a 20 20 20 20  ++iBuffer){.    
33c3c 20 20 20 20 69 66 28 20 30 3d 3d 70 42 75 66 66      if( 0==pBuff
33c3d 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61  ers[iBuffer].nDa
33c3e 74 61 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  ta ) break;.    
33c3f 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75    }..      /* Ou
33c40 74 20 6f 66 20 62 75 66 66 65 72 73 2c 20 61 64  t of buffers, ad
33c41 64 20 61 6e 20 65 6d 70 74 79 20 6f 6e 65 2e 20  d an empty one. 
33c42 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 42 75  */.      if( iBu
33c43 66 66 65 72 3d 3d 6e 42 75 66 66 65 72 73 20 29  ffer==nBuffers )
33c44 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 42  {.        if( nB
33c45 75 66 66 65 72 73 3d 3d 6e 4d 61 78 42 75 66 66  uffers==nMaxBuff
33c46 65 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ers ){.         
33c47 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 3b 0a   DataBuffer *p;.
33c48 20 20 20 20 20 20 20 20 20 20 6e 4d 61 78 42 75            nMaxBu
33c49 66 66 65 72 73 20 2b 3d 20 32 30 3b 0a 0a 20 20  ffers += 20;..  
33c4a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6e 75 61          /* Manua
33c4b 6c 20 72 65 61 6c 6c 6f 63 20 73 6f 20 77 65 20  l realloc so we 
33c4c 63 61 6e 20 68 61 6e 64 6c 65 20 4e 55 4c 4c 20  can handle NULL 
33c4d 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a  appropriately. *
33c4e 2f 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20  /.          p = 
33c4f 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
33c50 4d 61 78 42 75 66 66 65 72 73 2a 73 69 7a 65 6f  MaxBuffers*sizeo
33c51 66 28 2a 70 42 75 66 66 65 72 73 29 29 3b 0a 20  f(*pBuffers));. 
33c52 20 20 20 20 20 20 20 20 20 69 66 28 20 70 3d 3d           if( p==
33c53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20  NULL ){.        
33c54 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
33c55 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
33c56 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
33c57 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
33c58 20 69 66 28 20 6e 42 75 66 66 65 72 73 3e 30 20   if( nBuffers>0 
33c59 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
33c5a 73 73 65 72 74 28 70 42 75 66 66 65 72 73 21 3d  ssert(pBuffers!=
33c5b 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 20  NULL);.         
33c5c 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 42 75     memcpy(p, pBu
33c5d 66 66 65 72 73 2c 20 6e 42 75 66 66 65 72 73 2a  ffers, nBuffers*
33c5e 73 69 7a 65 6f 66 28 2a 70 42 75 66 66 65 72 73  sizeof(*pBuffers
33c5f 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ));.            
33c60 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75  sqlite3_free(pBu
33c61 66 66 65 72 73 29 3b 0a 20 20 20 20 20 20 20 20  ffers);.        
33c62 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 42    }.          pB
33c63 75 66 66 65 72 73 20 3d 20 70 3b 0a 20 20 20 20  uffers = p;.    
33c64 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61      }.        da
33c65 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 28 70  taBufferInit(&(p
33c66 42 75 66 66 65 72 73 5b 6e 42 75 66 66 65 72 73  Buffers[nBuffers
33c67 5d 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ]), 0);.        
33c68 6e 42 75 66 66 65 72 73 2b 2b 3b 0a 20 20 20 20  nBuffers++;.    
33c69 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74    }..      /* At
33c6a 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 6d 75 73   this point, mus
33c6b 74 20 68 61 76 65 20 61 6e 20 65 6d 70 74 79 20  t have an empty 
33c6c 61 74 20 69 42 75 66 66 65 72 2e 20 2a 2f 0a 20  at iBuffer. */. 
33c6d 20 20 20 20 20 61 73 73 65 72 74 28 69 42 75 66       assert(iBuf
33c6e 66 65 72 3c 6e 42 75 66 66 65 72 73 20 26 26 20  fer<nBuffers && 
33c6f 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
33c70 5d 2e 6e 44 61 74 61 3d 3d 30 29 3b 0a 0a 20 20  ].nData==0);..  
33c71 20 20 20 20 2f 2a 20 49 66 20 65 6d 70 74 79 20      /* If empty 
33c72 77 61 73 20 66 69 72 73 74 20 62 75 66 66 65 72  was first buffer
33c73 2c 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 6d 65  , no need for me
33c74 72 67 65 20 6c 6f 67 69 63 2e 20 2a 2f 0a 20 20  rge logic. */.  
33c75 20 20 20 20 69 66 28 20 69 42 75 66 66 65 72 3d      if( iBuffer=
33c76 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64 61  =0 ){.        da
33c77 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28  taBufferReplace(
33c78 26 28 70 42 75 66 66 65 72 73 5b 30 5d 29 2c 20  &(pBuffers[0]), 
33c79 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20  pData, nData);. 
33c7a 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
33c7b 20 20 20 20 2f 2a 20 70 41 63 63 20 69 73 20 74      /* pAcc is t
33c7c 68 65 20 65 6d 70 74 79 20 62 75 66 66 65 72 20  he empty buffer 
33c7d 74 68 65 20 6d 65 72 67 65 64 20 64 61 74 61 20  the merged data 
33c7e 77 69 6c 6c 20 65 6e 64 20 75 70 20 69 6e 2e 20  will end up in. 
33c7f 2a 2f 0a 20 20 20 20 20 20 20 20 44 61 74 61 42  */.        DataB
33c80 75 66 66 65 72 20 2a 70 41 63 63 20 3d 20 26 28  uffer *pAcc = &(
33c81 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
33c82 5d 29 3b 0a 20 20 20 20 20 20 20 20 44 61 74 61  ]);.        Data
33c83 42 75 66 66 65 72 20 2a 70 20 3d 20 26 28 70 42  Buffer *p = &(pB
33c84 75 66 66 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20  uffers[0]);..   
33c85 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 70       /* Handle p
33c86 6f 73 69 74 69 6f 6e 20 30 20 73 70 65 63 69 61  osition 0 specia
33c87 6c 6c 79 20 74 6f 20 61 76 6f 69 64 20 6e 65 65  lly to avoid nee
33c88 64 20 74 6f 20 70 72 69 6d 65 20 70 41 63 63 0a  d to prime pAcc.
33c89 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20          ** with 
33c8a 70 44 61 74 61 2f 6e 44 61 74 61 2e 0a 20 20 20  pData/nData..   
33c8b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
33c8c 64 61 74 61 42 75 66 66 65 72 53 77 61 70 28 70  dataBufferSwap(p
33c8d 2c 20 70 41 63 63 29 3b 0a 20 20 20 20 20 20 20  , pAcc);.       
33c8e 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61   docListAccumula
33c8f 74 65 55 6e 69 6f 6e 28 70 41 63 63 2c 20 70 44  teUnion(pAcc, pD
33c90 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20  ata, nData);..  
33c91 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
33c92 61 74 65 20 72 65 6d 61 69 6e 69 6e 67 20 64 6f  ate remaining do
33c93 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 41 63 63  clists into pAcc
33c94 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72  . */.        for
33c95 28 2b 2b 70 3b 20 70 3c 70 41 63 63 3b 20 2b 2b  (++p; p<pAcc; ++
33c96 70 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f  p){.          do
33c97 63 4c 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55  cListAccumulateU
33c98 6e 69 6f 6e 28 70 41 63 63 2c 20 70 2d 3e 70 44  nion(pAcc, p->pD
33c99 61 74 61 2c 20 70 2d 3e 6e 44 61 74 61 29 3b 0a  ata, p->nData);.
33c9a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 61  .          /* da
33c9b 74 61 42 75 66 66 65 72 52 65 73 65 74 28 29 20  taBufferReset() 
33c9c 63 6f 75 6c 64 20 61 6c 6c 6f 77 20 61 20 6c 61  could allow a la
33c9d 72 67 65 20 64 6f 63 6c 69 73 74 20 74 6f 20 62  rge doclist to b
33c9e 6c 6f 77 20 75 70 0a 20 20 20 20 20 20 20 20 20  low up.         
33c9f 20 2a 2a 20 6f 75 72 20 6d 65 6d 6f 72 79 20 72   ** our memory r
33ca0 65 71 75 69 72 65 6d 65 6e 74 73 2e 0a 20 20 20  equirements..   
33ca1 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
33ca2 20 20 20 20 69 66 28 20 70 2d 3e 6e 43 61 70 61      if( p->nCapa
33ca3 63 69 74 79 3c 31 30 32 34 20 29 7b 0a 20 20 20  city<1024 ){.   
33ca4 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66           dataBuf
33ca5 66 65 72 52 65 73 65 74 28 70 29 3b 0a 20 20 20  ferReset(p);.   
33ca6 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
33ca7 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75            dataBu
33ca8 66 66 65 72 44 65 73 74 72 6f 79 28 70 29 3b 0a  fferDestroy(p);.
33ca9 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
33caa 42 75 66 66 65 72 49 6e 69 74 28 70 2c 20 30 29  BufferInit(p, 0)
33cab 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
33cac 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
33cad 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
33cae 55 6e 69 6f 6e 20 61 6c 6c 20 74 68 65 20 64 6f  Union all the do
33caf 63 6c 69 73 74 73 20 74 6f 67 65 74 68 65 72 20  clists together 
33cb0 69 6e 74 6f 20 2a 6f 75 74 2e 20 2a 2f 0a 20 20  into *out. */.  
33cb1 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57  /* TODO(shess) W
33cb2 68 61 74 20 69 66 20 2a 6f 75 74 20 69 73 20 62  hat if *out is b
33cb3 69 67 3f 20 20 53 69 67 68 2e 20 2a 2f 0a 20 20  ig?  Sigh. */.  
33cb4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
33cb5 4b 20 26 26 20 6e 42 75 66 66 65 72 73 3e 30 20  K && nBuffers>0 
33cb6 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 66 66  ){.    int iBuff
33cb7 65 72 3b 0a 20 20 20 20 66 6f 72 28 69 42 75 66  er;.    for(iBuf
33cb8 66 65 72 3d 30 3b 20 69 42 75 66 66 65 72 3c 6e  fer=0; iBuffer<n
33cb9 42 75 66 66 65 72 73 3b 20 2b 2b 69 42 75 66 66  Buffers; ++iBuff
33cba 65 72 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  er){.      if( p
33cbb 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d  Buffers[iBuffer]
33cbc 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20  .nData>0 ){.    
33cbd 20 20 20 20 69 66 28 20 6f 75 74 2d 3e 6e 44 61      if( out->nDa
33cbe 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ta==0 ){.       
33cbf 20 20 20 64 61 74 61 42 75 66 66 65 72 53 77 61     dataBufferSwa
33cc0 70 28 6f 75 74 2c 20 26 28 70 42 75 66 66 65 72  p(out, &(pBuffer
33cc1 73 5b 69 42 75 66 66 65 72 5d 29 29 3b 0a 20 20  s[iBuffer]));.  
33cc2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
33cc3 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 41 63         docListAc
33cc4 63 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 6f 75  cumulateUnion(ou
33cc5 74 2c 20 70 42 75 66 66 65 72 73 5b 69 42 75 66  t, pBuffers[iBuf
33cc6 66 65 72 5d 2e 70 44 61 74 61 2c 0a 20 20 20 20  fer].pData,.    
33cc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33cc8 20 20 20 20 20 20 20 20 20 20 20 20 20 70 42 75               pBu
33cc9 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e  ffers[iBuffer].n
33cca 44 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20 7d  Data);.        }
33ccb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
33ccc 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 6e 42 75   }..  while( nBu
33ccd 66 66 65 72 73 2d 2d 20 29 7b 0a 20 20 20 20 64  ffers-- ){.    d
33cce 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
33ccf 28 26 28 70 42 75 66 66 65 72 73 5b 6e 42 75 66  (&(pBuffers[nBuf
33cd0 66 65 72 73 5d 29 29 3b 0a 20 20 7d 0a 20 20 69  fers]));.  }.  i
33cd1 66 28 20 70 42 75 66 66 65 72 73 21 3d 4e 55 4c  f( pBuffers!=NUL
33cd2 4c 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65  L ) sqlite3_free
33cd3 28 70 42 75 66 66 65 72 73 29 3b 0a 0a 20 20 72  (pBuffers);..  r
33cd4 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
33cd5 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74  Call loadSegment
33cd6 4c 65 61 76 65 73 49 6e 74 28 29 20 77 69 74 68  LeavesInt() with
33cd7 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61 73 20   pData/nData as 
33cd8 69 6e 70 75 74 2e 20 2a 2f 0a 73 74 61 74 69 63  input. */.static
33cd9 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74   int loadSegment
33cda 4c 65 61 66 28 66 75 6c 6c 74 65 78 74 5f 76 74  Leaf(fulltext_vt
33cdb 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61  ab *v, const cha
33cdc 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
33cdd 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
33cde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33cdf 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
33ce0 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
33ce1 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20  t isPrefix,.    
33ce2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ce3 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65         DataBuffe
33ce4 72 20 2a 6f 75 74 29 7b 0a 20 20 4c 65 61 76 65  r *out){.  Leave
33ce5 73 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  sReader reader;.
33ce6 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
33ce7 65 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b 0a  ert( nData>1 );.
33ce8 20 20 61 73 73 65 72 74 28 20 2a 70 44 61 74 61    assert( *pData
33ce9 3d 3d 27 5c 30 27 20 29 3b 0a 20 20 72 63 20 3d  =='\0' );.  rc =
33cea 20 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69   leavesReaderIni
33ceb 74 28 76 2c 20 30 2c 20 30 2c 20 30 2c 20 70 44  t(v, 0, 0, 0, pD
33cec 61 74 61 2c 20 6e 44 61 74 61 2c 20 26 72 65 61  ata, nData, &rea
33ced 64 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  der);.  if( rc!=
33cee 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
33cef 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c  rn rc;..  rc = l
33cf0 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
33cf1 49 6e 74 28 76 2c 20 26 72 65 61 64 65 72 2c 20  Int(v, &reader, 
33cf2 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
33cf3 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20  Prefix, out);.  
33cf4 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 73 65  leavesReaderRese
33cf5 74 28 26 72 65 61 64 65 72 29 3b 0a 20 20 6c 65  t(&reader);.  le
33cf6 61 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f  avesReaderDestro
33cf7 79 28 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65  y(&reader);.  re
33cf8 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43  turn rc;.}../* C
33cf9 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c  all loadSegmentL
33cfa 65 61 76 65 73 49 6e 74 28 29 20 77 69 74 68 20  eavesInt() with 
33cfb 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 73 20 66  the leaf nodes f
33cfc 72 6f 6d 20 69 53 74 61 72 74 4c 65 61 66 20 74  rom iStartLeaf t
33cfd 6f 0a 2a 2a 20 69 45 6e 64 4c 65 61 66 20 28 69  o.** iEndLeaf (i
33cfe 6e 63 6c 75 73 69 76 65 29 20 61 73 20 69 6e 70  nclusive) as inp
33cff 75 74 2c 20 61 6e 64 20 6d 65 72 67 65 20 74 68  ut, and merge th
33d00 65 20 72 65 73 75 6c 74 69 6e 67 20 64 6f 63 6c  e resulting docl
33d01 69 73 74 20 69 6e 74 6f 0a 2a 2a 20 6f 75 74 2e  ist into.** out.
33d02 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
33d03 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
33d04 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
33d05 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
33d06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d07 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74  sqlite_int64 iSt
33d08 61 72 74 4c 65 61 66 2c 20 73 71 6c 69 74 65 5f  artLeaf, sqlite_
33d09 69 6e 74 36 34 20 69 45 6e 64 4c 65 61 66 2c 0a  int64 iEndLeaf,.
33d0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d0b 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
33d0c 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
33d0d 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
33d0e 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
33d0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d10 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
33d11 20 2a 6f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63   *out){.  int rc
33d12 3b 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65 72  ;.  LeavesReader
33d13 20 72 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65   reader;..  asse
33d14 72 74 28 20 69 53 74 61 72 74 4c 65 61 66 3c 3d  rt( iStartLeaf<=
33d15 69 45 6e 64 4c 65 61 66 20 29 3b 0a 20 20 72 63  iEndLeaf );.  rc
33d16 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 49   = leavesReaderI
33d17 6e 69 74 28 76 2c 20 30 2c 20 69 53 74 61 72 74  nit(v, 0, iStart
33d18 4c 65 61 66 2c 20 69 45 6e 64 4c 65 61 66 2c 20  Leaf, iEndLeaf, 
33d19 4e 55 4c 4c 2c 20 30 2c 20 26 72 65 61 64 65 72  NULL, 0, &reader
33d1a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
33d1b 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
33d1c 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61 64  rc;..  rc = load
33d1d 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74  SegmentLeavesInt
33d1e 28 76 2c 20 26 72 65 61 64 65 72 2c 20 70 54 65  (v, &reader, pTe
33d1f 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
33d20 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65 61  fix, out);.  lea
33d21 76 65 73 52 65 61 64 65 72 52 65 73 65 74 28 26  vesReaderReset(&
33d22 72 65 61 64 65 72 29 3b 0a 20 20 6c 65 61 76 65  reader);.  leave
33d23 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  sReaderDestroy(&
33d24 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72  reader);.  retur
33d25 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 61 6b 69  n rc;.}../* Taki
33d26 6e 67 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61  ng pData/nData a
33d27 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s an interior no
33d28 64 65 2c 20 66 69 6e 64 20 74 68 65 20 73 65 71  de, find the seq
33d29 75 65 6e 63 65 20 6f 66 20 63 68 69 6c 64 0a 2a  uence of child.*
33d2a 2a 20 6e 6f 64 65 73 20 77 68 69 63 68 20 63 6f  * nodes which co
33d2b 75 6c 64 20 69 6e 63 6c 75 64 65 20 70 54 65 72  uld include pTer
33d2c 6d 2f 6e 54 65 72 6d 2f 69 73 50 72 65 66 69 78  m/nTerm/isPrefix
33d2d 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  .  Note that the
33d2e 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  .** interior nod
33d2f 65 20 74 65 72 6d 73 20 6c 6f 67 69 63 61 6c 6c  e terms logicall
33d30 79 20 63 6f 6d 65 20 62 65 74 77 65 65 6e 20 74  y come between t
33d31 68 65 20 62 6c 6f 63 6b 73 2c 20 73 6f 20 74 68  he blocks, so th
33d32 65 72 65 20 69 73 0a 2a 2a 20 6f 6e 65 20 6d 6f  ere is.** one mo
33d33 72 65 20 62 6c 6f 63 6b 69 64 20 74 68 61 6e 20  re blockid than 
33d34 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73 20  there are terms 
33d35 28 74 68 61 74 20 62 6c 6f 63 6b 20 63 6f 6e 74  (that block cont
33d36 61 69 6e 73 20 74 65 72 6d 73 20 3e 3d 0a 2a 2a  ains terms >=.**
33d37 20 74 68 65 20 6c 61 73 74 20 69 6e 74 65 72 69   the last interi
33d38 6f 72 2d 6e 6f 64 65 20 74 65 72 6d 29 2e 0a 2a  or-node term)..*
33d39 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
33d3a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 64   The calling cod
33d3b 65 20 6d 61 79 20 61 6c 72 65 61 64 79 20 6b 6e  e may already kn
33d3c 6f 77 20 74 68 61 74 20 74 68 65 20 65 6e 64 20  ow that the end 
33d3d 63 68 69 6c 64 20 69 73 0a 2a 2a 20 6e 6f 74 20  child is.** not 
33d3e 77 6f 72 74 68 20 63 61 6c 63 75 6c 61 74 69 6e  worth calculatin
33d3f 67 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 65  g, because the e
33d40 6e 64 20 6d 61 79 20 62 65 20 69 6e 20 61 20 6c  nd may be in a l
33d41 61 74 65 72 20 73 69 62 6c 69 6e 67 0a 2a 2a 20  ater sibling.** 
33d42 6e 6f 64 65 2e 20 20 43 6f 6e 73 69 64 65 72 20  node.  Consider 
33d43 77 68 65 74 68 65 72 20 62 72 65 61 6b 69 6e 67  whether breaking
33d44 20 73 79 6d 6d 65 74 72 79 20 69 73 20 77 6f 72   symmetry is wor
33d45 74 68 77 68 69 6c 65 2e 20 20 49 20 73 75 73 70  thwhile.  I susp
33d46 65 63 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74  ect.** it is not
33d47 20 77 6f 72 74 68 77 68 69 6c 65 2e 0a 2a 2f 0a   worthwhile..*/.
33d48 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43  static void getC
33d49 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
33d4a 67 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  g(const char *pD
33d4b 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
33d4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d4e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
33d4f 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
33d50 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
33d51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d53 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
33d54 53 74 61 72 74 43 68 69 6c 64 2c 0a 20 20 20 20  StartChild,.    
33d55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
33d57 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e  lite_int64 *piEn
33d58 64 43 68 69 6c 64 29 7b 0a 20 20 49 6e 74 65 72  dChild){.  Inter
33d59 69 6f 72 52 65 61 64 65 72 20 72 65 61 64 65 72  iorReader reader
33d5a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  ;..  assert( nDa
33d5b 74 61 3e 31 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>1 );.  assert
33d5c 28 20 2a 70 44 61 74 61 21 3d 27 5c 30 27 20 29  ( *pData!='\0' )
33d5d 3b 0a 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64  ;.  interiorRead
33d5e 65 72 49 6e 69 74 28 70 44 61 74 61 2c 20 6e 44  erInit(pData, nD
33d5f 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 0a  ata, &reader);..
33d60 20 20 2f 2a 20 53 63 61 6e 20 66 6f 72 20 74 68    /* Scan for th
33d61 65 20 66 69 72 73 74 20 63 68 69 6c 64 20 77 68  e first child wh
33d62 69 63 68 20 63 6f 75 6c 64 20 63 6f 6e 74 61 69  ich could contai
33d63 6e 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 2a  n pTerm/nTerm. *
33d64 2f 0a 20 20 77 68 69 6c 65 28 20 21 69 6e 74 65  /.  while( !inte
33d65 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28  riorReaderAtEnd(
33d66 26 72 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  &reader) ){.    
33d67 69 66 28 20 69 6e 74 65 72 69 6f 72 52 65 61 64  if( interiorRead
33d68 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64 65  erTermCmp(&reade
33d69 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  r, pTerm, nTerm,
33d6a 20 30 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20   0)>0 ) break;. 
33d6b 20 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65     interiorReade
33d6c 72 53 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a  rStep(&reader);.
33d6d 20 20 7d 0a 20 20 2a 70 69 53 74 61 72 74 43 68    }.  *piStartCh
33d6e 69 6c 64 20 3d 20 69 6e 74 65 72 69 6f 72 52 65  ild = interiorRe
33d6f 61 64 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b  aderCurrentBlock
33d70 69 64 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20  id(&reader);..  
33d71 2f 2a 20 4b 65 65 70 20 73 63 61 6e 6e 69 6e 67  /* Keep scanning
33d72 20 74 6f 20 66 69 6e 64 20 61 20 74 65 72 6d 20   to find a term 
33d73 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 75 72  greater than our
33d74 20 74 65 72 6d 2c 20 75 73 69 6e 67 20 70 72 65   term, using pre
33d75 66 69 78 0a 20 20 2a 2a 20 63 6f 6d 70 61 72 69  fix.  ** compari
33d76 73 6f 6e 20 69 66 20 69 6e 64 69 63 61 74 65 64  son if indicated
33d77 2e 20 20 49 66 20 69 73 50 72 65 66 69 78 20 69  .  If isPrefix i
33d78 73 20 66 61 6c 73 65 2c 20 74 68 69 73 20 77 69  s false, this wi
33d79 6c 6c 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 73  ll be the.  ** s
33d7a 61 6d 65 20 62 6c 6f 63 6b 69 64 20 61 73 20 74  ame blockid as t
33d7b 68 65 20 73 74 61 72 74 69 6e 67 20 62 6c 6f 63  he starting bloc
33d7c 6b 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  k..  */.  while(
33d7d 20 21 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72   !interiorReader
33d7e 41 74 45 6e 64 28 26 72 65 61 64 65 72 29 20 29  AtEnd(&reader) )
33d7f 7b 0a 20 20 20 20 69 66 28 20 69 6e 74 65 72 69  {.    if( interi
33d80 6f 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  orReaderTermCmp(
33d81 26 72 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20  &reader, pTerm, 
33d82 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 29  nTerm, isPrefix)
33d83 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  >0 ) break;.    
33d84 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 53 74  interiorReaderSt
33d85 65 70 28 26 72 65 61 64 65 72 29 3b 0a 20 20 7d  ep(&reader);.  }
33d86 0a 20 20 2a 70 69 45 6e 64 43 68 69 6c 64 20 3d  .  *piEndChild =
33d87 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
33d88 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 26 72  urrentBlockid(&r
33d89 65 61 64 65 72 29 3b 0a 0a 20 20 69 6e 74 65 72  eader);..  inter
33d8a 69 6f 72 52 65 61 64 65 72 44 65 73 74 72 6f 79  iorReaderDestroy
33d8b 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a  (&reader);..  /*
33d8c 20 43 68 69 6c 64 72 65 6e 20 6d 75 73 74 20 61   Children must a
33d8d 73 63 65 6e 64 2c 20 61 6e 64 20 69 66 20 21 70  scend, and if !p
33d8e 72 65 66 69 78 2c 20 62 6f 74 68 20 6d 75 73 74  refix, both must
33d8f 20 62 65 20 74 68 65 20 73 61 6d 65 2e 20 2a 2f   be the same. */
33d90 0a 20 20 61 73 73 65 72 74 28 20 2a 70 69 45 6e  .  assert( *piEn
33d91 64 43 68 69 6c 64 3e 3d 2a 70 69 53 74 61 72 74  dChild>=*piStart
33d92 43 68 69 6c 64 20 29 3b 0a 20 20 61 73 73 65 72  Child );.  asser
33d93 74 28 20 69 73 50 72 65 66 69 78 20 7c 7c 20 2a  t( isPrefix || *
33d94 70 69 53 74 61 72 74 43 68 69 6c 64 3d 3d 2a 70  piStartChild==*p
33d95 69 45 6e 64 43 68 69 6c 64 20 29 3b 0a 7d 0a 0a  iEndChild );.}..
33d96 2f 2a 20 52 65 61 64 20 62 6c 6f 63 6b 20 61 74  /* Read block at
33d97 20 69 42 6c 6f 63 6b 69 64 20 61 6e 64 20 70 61   iBlockid and pa
33d98 73 73 20 69 74 20 77 69 74 68 20 6f 74 68 65 72  ss it with other
33d99 20 70 61 72 61 6d 73 20 74 6f 0a 2a 2a 20 67 65   params to.** ge
33d9a 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
33d9b 69 6e 67 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ing()..*/.static
33d9c 20 69 6e 74 20 6c 6f 61 64 41 6e 64 47 65 74 43   int loadAndGetC
33d9d 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
33d9e 67 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  g(.  fulltext_vt
33d9f 61 62 20 2a 76 2c 0a 20 20 73 71 6c 69 74 65 5f  ab *v,.  sqlite_
33da0 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 2c 0a  int64 iBlockid,.
33da1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
33da2 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
33da3 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
33da4 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
33da5 53 74 61 72 74 43 68 69 6c 64 2c 20 73 71 6c 69  StartChild, sqli
33da6 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 43  te_int64 *piEndC
33da7 68 69 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65  hild.){.  sqlite
33da8 33 5f 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c  3_stmt *s = NULL
33da9 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
33daa 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64 21  ssert( iBlockid!
33dab 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
33dac 70 54 65 72 6d 21 3d 4e 55 4c 4c 20 29 3b 0a 20  pTerm!=NULL );. 
33dad 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 21 3d   assert( nTerm!=
33dae 30 20 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  0 );        /* T
33daf 4f 44 4f 28 73 68 65 73 73 29 20 57 68 79 20 6e  ODO(shess) Why n
33db0 6f 74 20 61 6c 6c 6f 77 20 74 68 69 73 3f 20 2a  ot allow this? *
33db1 2f 0a 20 20 61 73 73 65 72 74 28 20 70 69 53 74  /.  assert( piSt
33db2 61 72 74 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29  artChild!=NULL )
33db3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 69 45 6e  ;.  assert( piEn
33db4 64 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a  dChild!=NULL );.
33db5 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  .  rc = sql_get_
33db6 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f  statement(v, BLO
33db7 43 4b 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20  CK_SELECT_STMT, 
33db8 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
33db9 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
33dba 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
33dbb 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
33dbc 28 73 2c 20 31 2c 20 69 42 6c 6f 63 6b 69 64 29  (s, 1, iBlockid)
33dbd 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
33dbe 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
33dbf 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
33dc0 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
33dc1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
33dc2 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  E ) return SQLIT
33dc3 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72  E_ERROR;.  if( r
33dc4 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
33dc5 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 67 65  return rc;..  ge
33dc6 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
33dc7 69 6e 67 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ing(sqlite3_colu
33dc8 6d 6e 5f 62 6c 6f 62 28 73 2c 20 30 29 2c 20 73  mn_blob(s, 0), s
33dc9 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
33dca 74 65 73 28 73 2c 20 30 29 2c 0a 20 20 20 20 20  tes(s, 0),.     
33dcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33dcc 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c     pTerm, nTerm,
33dcd 20 69 73 50 72 65 66 69 78 2c 20 70 69 53 74 61   isPrefix, piSta
33dce 72 74 43 68 69 6c 64 2c 20 70 69 45 6e 64 43 68  rtChild, piEndCh
33dcf 69 6c 64 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65  ild);..  /* We e
33dd0 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72  xpect only one r
33dd1 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65  ow.  We must exe
33dd2 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c  cute another sql
33dd3 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a  ite3_step().   *
33dd4 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
33dd5 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65   iteration; othe
33dd6 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20  rwise the table 
33dd7 77 69 6c 6c 20 72 65 6d 61 69 6e 0a 20 20 20 2a  will remain.   *
33dd8 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63   locked. */.  rc
33dd9 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
33dda 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
33ddb 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
33ddc 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
33ddd 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
33dde 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72  _DONE ) return r
33ddf 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  c;..  return SQL
33de0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 72  ITE_OK;.}../* Tr
33de1 61 76 65 72 73 65 20 74 68 65 20 74 72 65 65 20  averse the tree 
33de2 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 70  represented by p
33de3 44 61 74 61 5b 6e 44 61 74 61 5d 20 6c 6f 6f 6b  Data[nData] look
33de4 69 6e 67 20 66 6f 72 0a 2a 2a 20 70 54 65 72 6d  ing for.** pTerm
33de5 5b 6e 54 65 72 6d 5d 2c 20 70 6c 61 63 69 6e 67  [nTerm], placing
33de6 20 69 74 73 20 64 6f 63 6c 69 73 74 20 69 6e 74   its doclist int
33de7 6f 20 2a 6f 75 74 2e 20 20 54 68 69 73 20 69 73  o *out.  This is
33de8 20 69 6e 74 65 72 6e 61 6c 20 74 6f 0a 2a 2a 20   internal to.** 
33de9 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29 20 74 6f  loadSegment() to
33dea 20 6d 61 6b 65 20 65 72 72 6f 72 2d 68 61 6e 64   make error-hand
33deb 6c 69 6e 67 20 63 6c 65 61 6e 65 72 2e 0a 2a 2f  ling cleaner..*/
33dec 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64  .static int load
33ded 53 65 67 6d 65 6e 74 49 6e 74 28 66 75 6c 6c 74  SegmentInt(fullt
33dee 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e  ext_vtab *v, con
33def 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
33df0 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
33df1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33df2 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
33df3 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20  4 iLeavesEnd,.  
33df4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33df5 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
33df6 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
33df7 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66  Term, int isPref
33df8 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
33df9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 61                Da
33dfa 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a  taBuffer *out){.
33dfb 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
33dfc 65 20 77 68 65 72 65 20 72 6f 6f 74 20 69 73 20  e where root is 
33dfd 61 20 6c 65 61 66 2e 20 2a 2f 0a 20 20 69 66 28  a leaf. */.  if(
33dfe 20 2a 70 44 61 74 61 3d 3d 27 5c 30 27 20 29 7b   *pData=='\0' ){
33dff 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 61 64  .    return load
33e00 53 65 67 6d 65 6e 74 4c 65 61 66 28 76 2c 20 70  SegmentLeaf(v, p
33e01 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65  Data, nData, pTe
33e02 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
33e03 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 7d 65 6c  fix, out);.  }el
33e04 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a  se{.    int rc;.
33e05 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
33e06 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20 69 45   iStartChild, iE
33e07 6e 64 43 68 69 6c 64 3b 0a 0a 20 20 20 20 2f 2a  ndChild;..    /*
33e08 20 50 72 6f 63 65 73 73 20 70 44 61 74 61 20 61   Process pData a
33e09 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s an interior no
33e0a 64 65 2c 20 74 68 65 6e 20 6c 6f 6f 70 20 64 6f  de, then loop do
33e0b 77 6e 20 74 68 65 20 74 72 65 65 0a 20 20 20 20  wn the tree.    
33e0c 2a 2a 20 75 6e 74 69 6c 20 77 65 20 66 69 6e 64  ** until we find
33e0d 20 74 68 65 20 73 65 74 20 6f 66 20 6c 65 61 66   the set of leaf
33e0e 20 6e 6f 64 65 73 20 74 6f 20 73 63 61 6e 20 66   nodes to scan f
33e0f 6f 72 20 74 68 65 20 74 65 72 6d 2e 0a 20 20 20  or the term..   
33e10 20 2a 2f 0a 20 20 20 20 67 65 74 43 68 69 6c 64   */.    getChild
33e11 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 70 44  renContaining(pD
33e12 61 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65 72  ata, nData, pTer
33e13 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66  m, nTerm, isPref
33e14 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
33e15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69                &i
33e16 53 74 61 72 74 43 68 69 6c 64 2c 20 26 69 45 6e  StartChild, &iEn
33e17 64 43 68 69 6c 64 29 3b 0a 20 20 20 20 77 68 69  dChild);.    whi
33e18 6c 65 28 20 69 53 74 61 72 74 43 68 69 6c 64 3e  le( iStartChild>
33e19 69 4c 65 61 76 65 73 45 6e 64 20 29 7b 0a 20 20  iLeavesEnd ){.  
33e1a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
33e1b 20 69 4e 65 78 74 53 74 61 72 74 2c 20 69 4e 65   iNextStart, iNe
33e1c 78 74 45 6e 64 3b 0a 20 20 20 20 20 20 72 63 20  xtEnd;.      rc 
33e1d 3d 20 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c  = loadAndGetChil
33e1e 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 76  drenContaining(v
33e1f 2c 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20 70  , iStartChild, p
33e20 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50  Term, nTerm, isP
33e21 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
33e22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
33e24 69 4e 65 78 74 53 74 61 72 74 2c 20 26 69 4e 65  iNextStart, &iNe
33e25 78 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 69 66  xtEnd);.      if
33e26 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
33e27 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
33e28 20 20 20 20 2f 2a 20 49 66 20 77 65 27 76 65 20      /* If we've 
33e29 62 72 61 6e 63 68 65 64 2c 20 66 6f 6c 6c 6f 77  branched, follow
33e2a 20 74 68 65 20 65 6e 64 20 62 72 61 6e 63 68 2c   the end branch,
33e2b 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 20 20 20 69   too. */.      i
33e2c 66 28 20 69 53 74 61 72 74 43 68 69 6c 64 21 3d  f( iStartChild!=
33e2d 69 45 6e 64 43 68 69 6c 64 20 29 7b 0a 20 20 20  iEndChild ){.   
33e2e 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
33e2f 34 20 69 44 75 6d 6d 79 3b 0a 20 20 20 20 20 20  4 iDummy;.      
33e30 20 20 72 63 20 3d 20 6c 6f 61 64 41 6e 64 47 65    rc = loadAndGe
33e31 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
33e32 69 6e 67 28 76 2c 20 69 45 6e 64 43 68 69 6c 64  ing(v, iEndChild
33e33 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
33e34 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20  isPrefix,.      
33e35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e37 20 20 20 20 26 69 44 75 6d 6d 79 2c 20 26 69 4e      &iDummy, &iN
33e38 65 78 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 20  extEnd);.       
33e39 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
33e3a 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
33e3b 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61        }..      a
33e3c 73 73 65 72 74 28 20 69 4e 65 78 74 53 74 61 72  ssert( iNextStar
33e3d 74 3c 3d 69 4e 65 78 74 45 6e 64 20 29 3b 0a 20  t<=iNextEnd );. 
33e3e 20 20 20 20 20 69 53 74 61 72 74 43 68 69 6c 64       iStartChild
33e3f 20 3d 20 69 4e 65 78 74 53 74 61 72 74 3b 0a 20   = iNextStart;. 
33e40 20 20 20 20 20 69 45 6e 64 43 68 69 6c 64 20 3d       iEndChild =
33e41 20 69 4e 65 78 74 45 6e 64 3b 0a 20 20 20 20 7d   iNextEnd;.    }
33e42 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53 74  .    assert( iSt
33e43 61 72 74 43 68 69 6c 64 3c 3d 69 4c 65 61 76 65  artChild<=iLeave
33e44 73 45 6e 64 20 29 3b 0a 20 20 20 20 61 73 73 65  sEnd );.    asse
33e45 72 74 28 20 69 45 6e 64 43 68 69 6c 64 3c 3d 69  rt( iEndChild<=i
33e46 4c 65 61 76 65 73 45 6e 64 20 29 3b 0a 0a 20 20  LeavesEnd );..  
33e47 20 20 2f 2a 20 53 63 61 6e 20 74 68 72 6f 75 67    /* Scan throug
33e48 68 20 74 68 65 20 6c 65 61 66 20 73 65 67 6d 65  h the leaf segme
33e49 6e 74 73 20 66 6f 72 20 64 6f 63 6c 69 73 74 73  nts for doclists
33e4a 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
33e4b 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65  loadSegmentLeave
33e4c 73 28 76 2c 20 69 53 74 61 72 74 43 68 69 6c 64  s(v, iStartChild
33e4d 2c 20 69 45 6e 64 43 68 69 6c 64 2c 0a 20 20 20  , iEndChild,.   
33e4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e4f 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c            pTerm,
33e50 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78   nTerm, isPrefix
33e51 2c 20 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  , out);.  }.}../
33e52 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65  * Call loadSegme
33e53 6e 74 49 6e 74 28 29 20 74 6f 20 63 6f 6c 6c 65  ntInt() to colle
33e54 63 74 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66  ct the doclist f
33e55 6f 72 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2c 20  or pTerm/nTerm, 
33e56 74 68 65 6e 0a 2a 2a 20 6d 65 72 67 65 20 69 74  then.** merge it
33e57 73 20 64 6f 63 6c 69 73 74 20 6f 76 65 72 20 2a  s doclist over *
33e58 6f 75 74 20 28 61 6e 79 20 64 75 70 6c 69 63 61  out (any duplica
33e59 74 65 20 64 6f 63 6c 69 73 74 73 20 72 65 61 64  te doclists read
33e5a 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 65 67   from the.** seg
33e5b 6d 65 6e 74 20 72 6f 6f 74 65 64 20 61 74 20 70  ment rooted at p
33e5c 44 61 74 61 20 77 69 6c 6c 20 6f 76 65 72 77 72  Data will overwr
33e5d 69 74 65 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75  ite those in *ou
33e5e 74 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  t)..*/./* TODO(s
33e5f 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20 63  hess) Consider c
33e60 68 61 6e 67 69 6e 67 20 74 68 69 73 20 74 6f 20  hanging this to 
33e61 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65  determine the de
33e62 70 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 65  pth of the.** le
33e63 61 76 65 73 20 75 73 69 6e 67 20 65 69 74 68 65  aves using eithe
33e64 72 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72  r the first char
33e65 61 63 74 65 72 73 20 6f 66 20 69 6e 74 65 72 69  acters of interi
33e66 6f 72 20 6e 6f 64 65 73 20 28 77 68 65 6e 0a 2a  or nodes (when.*
33e67 2a 20 3d 3d 31 2c 20 77 65 27 72 65 20 6f 6e 65  * ==1, we're one
33e68 20 6c 65 76 65 6c 20 61 62 6f 76 65 20 74 68 65   level above the
33e69 20 6c 65 61 76 65 73 29 2c 20 6f 72 20 74 68 65   leaves), or the
33e6a 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
33e6b 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20   of.** the root 
33e6c 28 77 68 69 63 68 20 77 69 6c 6c 20 64 65 73 63  (which will desc
33e6d 72 69 62 65 20 74 68 65 20 68 65 69 67 68 74 20  ribe the height 
33e6e 6f 66 20 74 68 65 20 74 72 65 65 20 64 69 72 65  of the tree dire
33e6f 63 74 6c 79 29 2e 0a 2a 2a 20 45 69 74 68 65 72  ctly)..** Either
33e70 20 66 65 65 6c 73 20 73 6f 6d 65 77 68 61 74 20   feels somewhat 
33e71 74 72 69 63 6b 79 20 74 6f 20 6d 65 2e 0a 2a 2f  tricky to me..*/
33e72 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
33e73 54 68 65 20 63 75 72 72 65 6e 74 20 6d 65 72 67  The current merg
33e74 65 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62  e is likely to b
33e75 65 20 73 6c 6f 77 20 66 6f 72 20 6c 61 72 67 65  e slow for large
33e76 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 20 28 74 68  .** doclists (th
33e77 6f 75 67 68 20 69 74 20 73 68 6f 75 6c 64 20 70  ough it should p
33e78 72 6f 63 65 73 73 20 66 72 6f 6d 20 6e 65 77 65  rocess from newe
33e79 73 74 2f 73 6d 61 6c 6c 65 73 74 20 74 6f 0a 2a  st/smallest to.*
33e7a 2a 20 6f 6c 64 65 73 74 2f 6c 61 72 67 65 73 74  * oldest/largest
33e7b 2c 20 73 6f 20 69 74 20 6d 61 79 20 6e 6f 74 20  , so it may not 
33e7c 62 65 20 74 68 61 74 20 62 61 64 29 2e 20 20 49  be that bad).  I
33e7d 74 20 6d 69 67 68 74 20 62 65 20 75 73 65 66 75  t might be usefu
33e7e 6c 20 74 6f 0a 2a 2a 20 6d 6f 64 69 66 79 20 74  l to.** modify t
33e7f 68 69 6e 67 73 20 74 6f 20 61 6c 6c 6f 77 20 66  hings to allow f
33e80 6f 72 20 4e 2d 77 61 79 20 6d 65 72 67 69 6e 67  or N-way merging
33e81 2e 20 20 54 68 69 73 20 63 6f 75 6c 64 20 65 69  .  This could ei
33e82 74 68 65 72 20 62 65 0a 2a 2a 20 77 69 74 68 69  ther be.** withi
33e83 6e 20 61 20 73 65 67 6d 65 6e 74 2c 20 77 69 74  n a segment, wit
33e84 68 20 70 61 69 72 77 69 73 65 20 6d 65 72 67 65  h pairwise merge
33e85 73 20 61 63 72 6f 73 73 20 73 65 67 6d 65 6e 74  s across segment
33e86 73 2c 20 6f 72 20 61 63 72 6f 73 73 0a 2a 2a 20  s, or across.** 
33e87 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20  all segments at 
33e88 6f 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  once..*/.static 
33e89 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28  int loadSegment(
33e8a 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
33e8b 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
33e8c 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
33e8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e8e 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
33e8f 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a  t64 iLeavesEnd,.
33e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e91 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
33e92 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
33e93 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
33e94 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
33e95 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75            DataBu
33e96 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 44 61  ffer *out){.  Da
33e97 74 61 42 75 66 66 65 72 20 72 65 73 75 6c 74 3b  taBuffer result;
33e98 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
33e99 73 65 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b  sert( nData>1 );
33e9a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65  ..  /* This code
33e9b 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65   should never be
33e9c 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66   called with buf
33e9d 66 65 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a  fered updates. *
33e9e 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e  /.  assert( v->n
33e9f 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b  PendingData<0 );
33ea0 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ..  dataBufferIn
33ea1 69 74 28 26 72 65 73 75 6c 74 2c 20 30 29 3b 0a  it(&result, 0);.
33ea2 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65    rc = loadSegme
33ea3 6e 74 49 6e 74 28 76 2c 20 70 44 61 74 61 2c 20  ntInt(v, pData, 
33ea4 6e 44 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e  nData, iLeavesEn
33ea5 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
33ea6 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20           pTerm, 
33ea7 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c  nTerm, isPrefix,
33ea8 20 26 72 65 73 75 6c 74 29 3b 0a 20 20 69 66 28   &result);.  if(
33ea9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
33eaa 26 20 72 65 73 75 6c 74 2e 6e 44 61 74 61 3e 30  & result.nData>0
33eab 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 75 74 2d   ){.    if( out-
33eac 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >nData==0 ){.   
33ead 20 20 20 44 61 74 61 42 75 66 66 65 72 20 74 6d     DataBuffer tm
33eae 70 20 3d 20 2a 6f 75 74 3b 0a 20 20 20 20 20 20  p = *out;.      
33eaf 2a 6f 75 74 20 3d 20 72 65 73 75 6c 74 3b 0a 20  *out = result;. 
33eb0 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 74 6d       result = tm
33eb1 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  p;.    }else{.  
33eb2 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20 6d      DataBuffer m
33eb3 65 72 67 65 64 3b 0a 20 20 20 20 20 20 44 4c 52  erged;.      DLR
33eb4 65 61 64 65 72 20 72 65 61 64 65 72 73 5b 32 5d  eader readers[2]
33eb5 3b 0a 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74  ;..      dlrInit
33eb6 28 26 72 65 61 64 65 72 73 5b 30 5d 2c 20 44 4c  (&readers[0], DL
33eb7 5f 44 45 46 41 55 4c 54 2c 20 6f 75 74 2d 3e 70  _DEFAULT, out->p
33eb8 44 61 74 61 2c 20 6f 75 74 2d 3e 6e 44 61 74 61  Data, out->nData
33eb9 29 3b 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74  );.      dlrInit
33eba 28 26 72 65 61 64 65 72 73 5b 31 5d 2c 20 44 4c  (&readers[1], DL
33ebb 5f 44 45 46 41 55 4c 54 2c 20 72 65 73 75 6c 74  _DEFAULT, result
33ebc 2e 70 44 61 74 61 2c 20 72 65 73 75 6c 74 2e 6e  .pData, result.n
33ebd 44 61 74 61 29 3b 0a 20 20 20 20 20 20 64 61 74  Data);.      dat
33ebe 61 42 75 66 66 65 72 49 6e 69 74 28 26 6d 65 72  aBufferInit(&mer
33ebf 67 65 64 2c 20 6f 75 74 2d 3e 6e 44 61 74 61 2b  ged, out->nData+
33ec0 72 65 73 75 6c 74 2e 6e 44 61 74 61 29 3b 0a 20  result.nData);. 
33ec1 20 20 20 20 20 64 6f 63 4c 69 73 74 4d 65 72 67       docListMerg
33ec2 65 28 26 6d 65 72 67 65 64 2c 20 72 65 61 64 65  e(&merged, reade
33ec3 72 73 2c 20 32 29 3b 0a 20 20 20 20 20 20 64 61  rs, 2);.      da
33ec4 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
33ec5 6f 75 74 29 3b 0a 20 20 20 20 20 20 2a 6f 75 74  out);.      *out
33ec6 20 3d 20 6d 65 72 67 65 64 3b 0a 20 20 20 20 20   = merged;.     
33ec7 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61   dlrDestroy(&rea
33ec8 64 65 72 73 5b 30 5d 29 3b 0a 20 20 20 20 20 20  ders[0]);.      
33ec9 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61 64  dlrDestroy(&read
33eca 65 72 73 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20  ers[1]);.    }. 
33ecb 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 44   }.  dataBufferD
33ecc 65 73 74 72 6f 79 28 26 72 65 73 75 6c 74 29 3b  estroy(&result);
33ecd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
33ece 0a 2f 2a 20 53 63 61 6e 20 74 68 65 20 64 61 74  ./* Scan the dat
33ecf 61 62 61 73 65 20 61 6e 64 20 6d 65 72 67 65 20  abase and merge 
33ed0 74 6f 67 65 74 68 65 72 20 74 68 65 20 70 6f 73  together the pos
33ed1 74 69 6e 67 20 6c 69 73 74 73 20 66 6f 72 20 74  ting lists for t
33ed2 68 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 74 6f 20  he term.** into 
33ed3 2a 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  *out..*/.static 
33ed4 69 6e 74 20 74 65 72 6d 53 65 6c 65 63 74 28 0a  int termSelect(.
33ed5 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
33ed6 2a 76 2c 20 0a 20 20 69 6e 74 20 69 43 6f 6c 75  *v, .  int iColu
33ed7 6d 6e 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  mn,.  const char
33ed8 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
33ed9 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rm,             
33eda 2f 2a 20 54 65 72 6d 20 74 6f 20 71 75 65 72 79  /* Term to query
33edb 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 73   for */.  int is
33edc 50 72 65 66 69 78 2c 20 20 20 20 20 20 20 20 20  Prefix,         
33edd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ede 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
33edf 61 20 70 72 65 66 69 78 20 73 65 61 72 63 68 20  a prefix search 
33ee0 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65  */.  DocListType
33ee1 20 69 54 79 70 65 2c 20 0a 20 20 44 61 74 61 42   iType, .  DataB
33ee2 75 66 66 65 72 20 2a 6f 75 74 20 20 20 20 20 20  uffer *out      
33ee3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ee4 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65       /* Write re
33ee5 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b  sults here */.){
33ee6 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 64 6f  .  DataBuffer do
33ee7 63 6c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33  clist;.  sqlite3
33ee8 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
33ee9 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
33eea 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
33eeb 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54  _SELECT_ALL_STMT
33eec 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
33eed 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
33eee 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68  urn rc;..  /* Th
33eef 69 73 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e  is code should n
33ef0 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64 20 77  ever be called w
33ef1 69 74 68 20 62 75 66 66 65 72 65 64 20 75 70 64  ith buffered upd
33ef2 61 74 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72  ates. */.  asser
33ef3 74 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  t( v->nPendingDa
33ef4 74 61 3c 30 20 29 3b 0a 0a 20 20 64 61 74 61 42  ta<0 );..  dataB
33ef5 75 66 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69  ufferInit(&docli
33ef6 73 74 2c 20 30 29 3b 0a 20 20 64 61 74 61 42 75  st, 0);.  dataBu
33ef7 66 66 65 72 49 6e 69 74 28 6f 75 74 2c 20 30 29  fferInit(out, 0)
33ef8 3b 0a 0a 20 20 2f 2a 20 54 72 61 76 65 72 73 65  ;..  /* Traverse
33ef9 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66 72   the segments fr
33efa 6f 6d 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77  om oldest to new
33efb 65 73 74 20 73 6f 20 74 68 61 74 20 6e 65 77 65  est so that newe
33efc 72 20 64 6f 63 6c 69 73 74 0a 20 20 2a 2a 20 65  r doclist.  ** e
33efd 6c 65 6d 65 6e 74 73 20 66 6f 72 20 67 69 76 65  lements for give
33efe 6e 20 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69  n docids overwri
33eff 74 65 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74  te older element
33f00 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  s..  */.  while(
33f01 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73   (rc = sqlite3_s
33f02 74 65 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f  tep(s))==SQLITE_
33f03 52 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ROW ){.    const
33f04 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 73   char *pData = s
33f05 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
33f06 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f  ob(s, 2);.    co
33f07 6e 73 74 20 69 6e 74 20 6e 44 61 74 61 20 3d 20  nst int nData = 
33f08 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
33f09 79 74 65 73 28 73 2c 20 32 29 3b 0a 20 20 20 20  ytes(s, 2);.    
33f0a 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69 6e 74  const sqlite_int
33f0b 36 34 20 69 4c 65 61 76 65 73 45 6e 64 20 3d 20  64 iLeavesEnd = 
33f0c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
33f0d 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20  nt64(s, 1);.    
33f0e 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74  rc = loadSegment
33f0f 28 76 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  (v, pData, nData
33f10 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c 20 70 54  , iLeavesEnd, pT
33f11 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
33f12 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20  efix,.          
33f13 20 20 20 20 20 20 20 20 20 20 20 26 64 6f 63 6c             &docl
33f14 69 73 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ist);.    if( rc
33f15 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
33f16 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 20 20 69 66  to err;.  }.  if
33f17 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
33f18 45 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6f 63  E ){.    if( doc
33f19 6c 69 73 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b  list.nData!=0 ){
33f1a 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73  .      /* TODO(s
33f1b 68 65 73 73 29 20 54 68 65 20 6f 6c 64 20 74 65  hess) The old te
33f1c 72 6d 5f 73 65 6c 65 63 74 5f 61 6c 6c 28 29 20  rm_select_all() 
33f1d 63 6f 64 65 20 61 70 70 6c 69 65 64 20 74 68 65  code applied the
33f1e 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a   column.      **
33f1f 20 72 65 73 74 72 69 63 74 20 61 73 20 77 65 20   restrict as we 
33f20 6d 65 72 67 65 64 20 73 65 67 6d 65 6e 74 73 2c  merged segments,
33f21 20 6c 65 61 64 69 6e 67 20 74 6f 20 73 6d 61 6c   leading to smal
33f22 6c 65 72 20 62 75 66 66 65 72 73 2e 0a 20 20 20  ler buffers..   
33f23 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 70 72     ** This is pr
33f24 6f 62 61 62 6c 79 20 77 6f 72 74 68 77 68 69 6c  obably worthwhil
33f25 65 20 74 6f 20 62 72 69 6e 67 20 62 61 63 6b 2c  e to bring back,
33f26 20 6f 6e 63 65 20 74 68 65 20 6e 65 77 20 73 74   once the new st
33f27 6f 72 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 73  orage.      ** s
33f28 79 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64  ystem is checked
33f29 20 69 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   in..      */.  
33f2a 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d      if( iColumn=
33f2b 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 69 43 6f  =v->nColumn) iCo
33f2c 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20  lumn = -1;.     
33f2d 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c 5f   docListTrim(DL_
33f2e 44 45 46 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74  DEFAULT, doclist
33f2f 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e  .pData, doclist.
33f30 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
33f31 20 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e           iColumn
33f32 2c 20 69 54 79 70 65 2c 20 6f 75 74 29 3b 0a 20  , iType, out);. 
33f33 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51     }.    rc = SQ
33f34 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 65  LITE_OK;.  }.. e
33f35 72 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72  rr:.  dataBuffer
33f36 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74  Destroy(&doclist
33f37 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
33f38 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
33f39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f3c 2a 2a 2a 2a 2f 0a 2f 2a 20 55 73 65 64 20 74 6f  ****/./* Used to
33f3d 20 68 6f 6c 64 20 68 61 73 68 74 61 62 6c 65 20   hold hashtable 
33f3e 64 61 74 61 20 66 6f 72 20 73 6f 72 74 69 6e 67  data for sorting
33f3f 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
33f40 75 63 74 20 54 65 72 6d 44 61 74 61 20 7b 0a 20  uct TermData {. 
33f41 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
33f42 72 6d 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b  rm;.  int nTerm;
33f43 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a  .  DLCollector *
33f44 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 20 54 65  pCollector;.} Te
33f45 72 6d 44 61 74 61 3b 0a 0a 2f 2a 20 4f 72 64 65  rmData;../* Orde
33f46 72 73 20 54 65 72 6d 44 61 74 61 20 65 6c 65 6d  rs TermData elem
33f47 65 6e 74 73 20 69 6e 20 73 74 72 63 6d 70 20 66  ents in strcmp f
33f48 61 73 68 69 6f 6e 20 28 20 3c 30 20 66 6f 72 20  ashion ( <0 for 
33f49 6c 65 73 73 2d 74 68 61 6e 2c 20 30 0a 2a 2a 20  less-than, 0.** 
33f4a 66 6f 72 20 65 71 75 61 6c 2c 20 3e 30 20 66 6f  for equal, >0 fo
33f4b 72 20 67 72 65 61 74 65 72 2d 74 68 61 6e 29 2e  r greater-than).
33f4c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
33f4d 65 72 6d 44 61 74 61 43 6d 70 28 63 6f 6e 73 74  ermDataCmp(const
33f4e 20 76 6f 69 64 20 2a 61 76 2c 20 63 6f 6e 73 74   void *av, const
33f4f 20 76 6f 69 64 20 2a 62 76 29 7b 0a 20 20 63 6f   void *bv){.  co
33f50 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 61 20  nst TermData *a 
33f51 3d 20 28 63 6f 6e 73 74 20 54 65 72 6d 44 61 74  = (const TermDat
33f52 61 20 2a 29 61 76 3b 0a 20 20 63 6f 6e 73 74 20  a *)av;.  const 
33f53 54 65 72 6d 44 61 74 61 20 2a 62 20 3d 20 28 63  TermData *b = (c
33f54 6f 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 29  onst TermData *)
33f55 62 76 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 61 2d  bv;.  int n = a-
33f56 3e 6e 54 65 72 6d 3c 62 2d 3e 6e 54 65 72 6d 20  >nTerm<b->nTerm 
33f57 3f 20 61 2d 3e 6e 54 65 72 6d 20 3a 20 62 2d 3e  ? a->nTerm : b->
33f58 6e 54 65 72 6d 3b 0a 20 20 69 6e 74 20 63 20 3d  nTerm;.  int c =
33f59 20 6d 65 6d 63 6d 70 28 61 2d 3e 70 54 65 72 6d   memcmp(a->pTerm
33f5a 2c 20 62 2d 3e 70 54 65 72 6d 2c 20 6e 29 3b 0a  , b->pTerm, n);.
33f5b 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65 74    if( c!=0 ) ret
33f5c 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20  urn c;.  return 
33f5d 61 2d 3e 6e 54 65 72 6d 2d 62 2d 3e 6e 54 65 72  a->nTerm-b->nTer
33f5e 6d 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 70  m;.}../* Order p
33f5f 54 65 72 6d 73 20 64 61 74 61 20 62 79 20 74 65  Terms data by te
33f60 72 6d 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61  rm, then write a
33f61 20 6e 65 77 20 6c 65 76 65 6c 20 30 20 73 65 67   new level 0 seg
33f62 6d 65 6e 74 20 75 73 69 6e 67 0a 2a 2a 20 4c 65  ment using.** Le
33f63 61 66 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61  afWriter..*/.sta
33f64 74 69 63 20 69 6e 74 20 77 72 69 74 65 5a 65 72  tic int writeZer
33f65 6f 53 65 67 6d 65 6e 74 28 66 75 6c 6c 74 65 78  oSegment(fulltex
33f66 74 5f 76 74 61 62 20 2a 76 2c 20 66 74 73 33 48  t_vtab *v, fts3H
33f67 61 73 68 20 2a 70 54 65 72 6d 73 29 7b 0a 20 20  ash *pTerms){.  
33f68 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b  fts3HashElem *e;
33f69 0a 20 20 69 6e 74 20 69 64 78 2c 20 72 63 2c 20  .  int idx, rc, 
33f6a 69 2c 20 6e 3b 0a 20 20 54 65 72 6d 44 61 74 61  i, n;.  TermData
33f6b 20 2a 70 44 61 74 61 3b 0a 20 20 4c 65 61 66 57   *pData;.  LeafW
33f6c 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 20 20  riter writer;.  
33f6d 44 61 74 61 42 75 66 66 65 72 20 64 6c 3b 0a 0a  DataBuffer dl;..
33f6e 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74    /* Determine t
33f6f 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 61 74  he next index at
33f70 20 6c 65 76 65 6c 20 30 2c 20 6d 65 72 67 69 6e   level 0, mergin
33f71 67 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20  g as necessary. 
33f72 2a 2f 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72  */.  rc = segdir
33f73 4e 65 78 74 49 6e 64 65 78 28 76 2c 20 30 2c 20  NextIndex(v, 0, 
33f74 26 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21  &idx);.  if( rc!
33f75 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
33f76 75 72 6e 20 72 63 3b 0a 0a 20 20 6e 20 3d 20 66  urn rc;..  n = f
33f77 74 73 33 48 61 73 68 43 6f 75 6e 74 28 70 54 65  ts3HashCount(pTe
33f78 72 6d 73 29 3b 0a 20 20 70 44 61 74 61 20 3d 20  rms);.  pData = 
33f79 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
33f7a 2a 73 69 7a 65 6f 66 28 54 65 72 6d 44 61 74 61  *sizeof(TermData
33f7b 29 29 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30  ));..  for(i = 0
33f7c 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 46 69  , e = fts3HashFi
33f7d 72 73 74 28 70 54 65 72 6d 73 29 3b 20 65 3b 20  rst(pTerms); e; 
33f7e 69 2b 2b 2c 20 65 20 3d 20 66 74 73 33 48 61 73  i++, e = fts3Has
33f7f 68 4e 65 78 74 28 65 29 29 7b 0a 20 20 20 20 61  hNext(e)){.    a
33f80 73 73 65 72 74 28 20 69 3c 6e 20 29 3b 0a 20 20  ssert( i<n );.  
33f81 20 20 70 44 61 74 61 5b 69 5d 2e 70 54 65 72 6d    pData[i].pTerm
33f82 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 28 65   = fts3HashKey(e
33f83 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e  );.    pData[i].
33f84 6e 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68  nTerm = fts3Hash
33f85 4b 65 79 73 69 7a 65 28 65 29 3b 0a 20 20 20 20  Keysize(e);.    
33f86 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65 63  pData[i].pCollec
33f87 74 6f 72 20 3d 20 66 74 73 33 48 61 73 68 44 61  tor = fts3HashDa
33f88 74 61 28 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ta(e);.  }.  ass
33f89 65 72 74 28 20 69 3d 3d 6e 20 29 3b 0a 0a 20 20  ert( i==n );..  
33f8a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 53  /* TODO(shess) S
33f8b 68 6f 75 6c 64 20 77 65 20 61 6c 6c 6f 77 20 75  hould we allow u
33f8c 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c  ser-defined coll
33f8d 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2c  ation sequences,
33f8e 0a 20 20 2a 2a 20 68 65 72 65 3f 20 20 49 20 74  .  ** here?  I t
33f8f 68 69 6e 6b 20 77 65 20 6f 6e 6c 79 20 6e 65 65  hink we only nee
33f90 64 20 74 68 61 74 20 6f 6e 63 65 20 77 65 20 73  d that once we s
33f91 75 70 70 6f 72 74 20 70 72 65 66 69 78 20 73 65  upport prefix se
33f92 61 72 63 68 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  arches..  */.  i
33f93 66 28 20 6e 3e 31 20 29 20 71 73 6f 72 74 28 70  f( n>1 ) qsort(p
33f94 44 61 74 61 2c 20 6e 2c 20 73 69 7a 65 6f 66 28  Data, n, sizeof(
33f95 2a 70 44 61 74 61 29 2c 20 74 65 72 6d 44 61 74  *pData), termDat
33f96 61 43 6d 70 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44  aCmp);..  /* TOD
33f97 4f 28 73 68 65 73 73 29 20 52 65 66 61 63 74 6f  O(shess) Refacto
33f98 72 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e  r so that we can
33f99 20 77 72 69 74 65 20 64 69 72 65 63 74 6c 79 20   write directly 
33f9a 74 6f 20 74 68 65 20 73 65 67 6d 65 6e 74 0a 20  to the segment. 
33f9b 20 2a 2a 20 44 61 74 61 42 75 66 66 65 72 2c 20   ** DataBuffer, 
33f9c 61 73 20 68 61 70 70 65 6e 73 20 66 6f 72 20 73  as happens for s
33f9d 65 67 6d 65 6e 74 20 6d 65 72 67 65 73 2e 0a 20  egment merges.. 
33f9e 20 2a 2f 0a 20 20 6c 65 61 66 57 72 69 74 65 72   */.  leafWriter
33f9f 49 6e 69 74 28 30 2c 20 69 64 78 2c 20 26 77 72  Init(0, idx, &wr
33fa0 69 74 65 72 29 3b 0a 20 20 64 61 74 61 42 75 66  iter);.  dataBuf
33fa1 66 65 72 49 6e 69 74 28 26 64 6c 2c 20 30 29 3b  ferInit(&dl, 0);
33fa2 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b  .  for(i=0; i<n;
33fa3 20 69 2b 2b 29 7b 0a 20 20 20 20 64 61 74 61 42   i++){.    dataB
33fa4 75 66 66 65 72 52 65 73 65 74 28 26 64 6c 29 3b  ufferReset(&dl);
33fa5 0a 20 20 20 20 64 6c 63 41 64 64 44 6f 63 6c 69  .    dlcAddDocli
33fa6 73 74 28 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c  st(pData[i].pCol
33fa7 6c 65 63 74 6f 72 2c 20 26 64 6c 29 3b 0a 20 20  lector, &dl);.  
33fa8 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
33fa9 72 53 74 65 70 28 76 2c 20 26 77 72 69 74 65 72  rStep(v, &writer
33faa 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33fab 20 20 20 20 20 20 20 20 20 20 70 44 61 74 61 5b            pData[
33fac 69 5d 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b  i].pTerm, pData[
33fad 69 5d 2e 6e 54 65 72 6d 2c 20 64 6c 2e 70 44 61  i].nTerm, dl.pDa
33fae 74 61 2c 20 64 6c 2e 6e 44 61 74 61 29 3b 0a 20  ta, dl.nData);. 
33faf 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
33fb0 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
33fb1 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6c 65 61 66  .  }.  rc = leaf
33fb2 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76  WriterFinalize(v
33fb3 2c 20 26 77 72 69 74 65 72 29 3b 0a 0a 20 65 72  , &writer);.. er
33fb4 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  r:.  dataBufferD
33fb5 65 73 74 72 6f 79 28 26 64 6c 29 3b 0a 20 20 73  estroy(&dl);.  s
33fb6 71 6c 69 74 65 33 5f 66 72 65 65 28 70 44 61 74  qlite3_free(pDat
33fb7 61 29 3b 0a 20 20 6c 65 61 66 57 72 69 74 65 72  a);.  leafWriter
33fb8 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
33fb9 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
33fba 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54  ../* If pendingT
33fbb 65 72 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66  erms has data, f
33fbc 72 65 65 20 69 74 2e 20 2a 2f 0a 73 74 61 74 69  ree it. */.stati
33fbd 63 20 69 6e 74 20 63 6c 65 61 72 50 65 6e 64 69  c int clearPendi
33fbe 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ngTerms(fulltext
33fbf 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69 66 28  _vtab *v){.  if(
33fc0 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
33fc1 3e 3d 30 20 29 7b 0a 20 20 20 20 66 74 73 33 48  >=0 ){.    fts3H
33fc2 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 20 20 20 20  ashElem *e;.    
33fc3 66 6f 72 28 65 3d 66 74 73 33 48 61 73 68 46 69  for(e=fts3HashFi
33fc4 72 73 74 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54  rst(&v->pendingT
33fc5 65 72 6d 73 29 3b 20 65 3b 20 65 3d 66 74 73 33  erms); e; e=fts3
33fc6 48 61 73 68 4e 65 78 74 28 65 29 29 7b 0a 20 20  HashNext(e)){.  
33fc7 20 20 20 20 64 6c 63 44 65 6c 65 74 65 28 66 74      dlcDelete(ft
33fc8 73 33 48 61 73 68 44 61 74 61 28 65 29 29 3b 0a  s3HashData(e));.
33fc9 20 20 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61      }.    fts3Ha
33fca 73 68 43 6c 65 61 72 28 26 76 2d 3e 70 65 6e 64  shClear(&v->pend
33fcb 69 6e 67 54 65 72 6d 73 29 3b 0a 20 20 20 20 76  ingTerms);.    v
33fcc 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d  ->nPendingData =
33fcd 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   -1;.  }.  retur
33fce 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
33fcf 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72  /* If pendingTer
33fd0 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66 6c 75  ms has data, flu
33fd1 73 68 20 69 74 20 74 6f 20 61 20 6c 65 76 65 6c  sh it to a level
33fd2 2d 7a 65 72 6f 20 73 65 67 6d 65 6e 74 2c 20 61  -zero segment, a
33fd3 6e 64 0a 2a 2a 20 66 72 65 65 20 69 74 2e 0a 2a  nd.** free it..*
33fd4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 75  /.static int flu
33fd5 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66  shPendingTerms(f
33fd6 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
33fd7 7b 0a 20 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64  {.  if( v->nPend
33fd8 69 6e 67 44 61 74 61 3e 3d 30 20 29 7b 0a 20 20  ingData>=0 ){.  
33fd9 20 20 69 6e 74 20 72 63 20 3d 20 77 72 69 74 65    int rc = write
33fda 5a 65 72 6f 53 65 67 6d 65 6e 74 28 76 2c 20 26  ZeroSegment(v, &
33fdb 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29  v->pendingTerms)
33fdc 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
33fdd 4c 49 54 45 5f 4f 4b 20 29 20 63 6c 65 61 72 50  LITE_OK ) clearP
33fde 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a  endingTerms(v);.
33fdf 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
33fe0 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
33fe1 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20  TE_OK;.}../* If 
33fe2 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20  pendingTerms is 
33fe3 22 74 6f 6f 20 62 69 67 22 2c 20 6f 72 20 64 6f  "too big", or do
33fe4 63 69 64 20 69 73 20 6f 75 74 20 6f 66 20 6f 72  cid is out of or
33fe5 64 65 72 2c 20 66 6c 75 73 68 20 69 74 2e 0a 2a  der, flush it..*
33fe6 2a 20 52 65 67 61 72 64 6c 65 73 73 2c 20 62 65  * Regardless, be
33fe7 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 70 65   certain that pe
33fe8 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20 69 6e  ndingTerms is in
33fe9 69 74 69 61 6c 69 7a 65 64 20 66 6f 72 20 75 73  itialized for us
33fea 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
33feb 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d   initPendingTerm
33fec 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
33fed 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
33fee 20 69 44 6f 63 69 64 29 7b 0a 20 20 2f 2a 20 54   iDocid){.  /* T
33fef 4f 44 4f 28 73 68 65 73 73 29 20 45 78 70 6c 6f  ODO(shess) Explo
33ff0 72 65 20 77 68 65 74 68 65 72 20 70 61 72 74 69  re whether parti
33ff1 61 6c 6c 79 20 66 6c 75 73 68 69 6e 67 20 74 68  ally flushing th
33ff2 65 20 62 75 66 66 65 72 20 6f 6e 0a 20 20 2a 2a  e buffer on.  **
33ff3 20 66 6f 72 63 65 64 2d 66 6c 75 73 68 20 77 6f   forced-flush wo
33ff4 75 6c 64 20 70 72 6f 76 69 64 65 20 62 65 74 74  uld provide bett
33ff5 65 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20  er performance. 
33ff6 20 49 20 73 75 73 70 65 63 74 20 74 68 61 74 20   I suspect that 
33ff7 69 66 0a 20 20 2a 2a 20 77 65 20 6f 72 64 65 72  if.  ** we order
33ff8 65 64 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20  ed the doclists 
33ff9 62 79 20 73 69 7a 65 20 61 6e 64 20 66 6c 75 73  by size and flus
33ffa 68 65 64 20 74 68 65 20 6c 61 72 67 65 73 74 20  hed the largest 
33ffb 75 6e 74 69 6c 20 74 68 65 0a 20 20 2a 2a 20 62  until the.  ** b
33ffc 75 66 66 65 72 20 77 61 73 20 68 61 6c 66 20 65  uffer was half e
33ffd 6d 70 74 79 2c 20 74 68 61 74 20 77 6f 75 6c 64  mpty, that would
33ffe 20 6c 65 74 20 74 68 65 20 6c 65 73 73 20 66 72   let the less fr
33fff 65 71 75 65 6e 74 20 74 65 72 6d 73 0a 20 20 2a  equent terms.  *
34000 2a 20 67 65 6e 65 72 61 74 65 20 6c 6f 6e 67 65  * generate longe
34001 72 20 64 6f 63 6c 69 73 74 73 2e 0a 20 20 2a 2f  r doclists..  */
34002 0a 20 20 69 66 28 20 69 44 6f 63 69 64 3c 3d 76  .  if( iDocid<=v
34003 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20  ->iPrevDocid || 
34004 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e  v->nPendingData>
34005 6b 50 65 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c  kPendingThreshol
34006 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  d ){.    int rc 
34007 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  = flushPendingTe
34008 72 6d 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20  rms(v);.    if( 
34009 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3400a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
3400b 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67   if( v->nPending
3400c 44 61 74 61 3c 30 20 29 7b 0a 20 20 20 20 66 74  Data<0 ){.    ft
3400d 73 33 48 61 73 68 49 6e 69 74 28 26 76 2d 3e 70  s3HashInit(&v->p
3400e 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 46 54 53  endingTerms, FTS
3400f 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31  3_HASH_STRING, 1
34010 29 3b 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69  );.    v->nPendi
34011 6e 67 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 0a  ngData = 0;.  }.
34012 20 20 76 2d 3e 69 50 72 65 76 44 6f 63 69 64 20    v->iPrevDocid 
34013 3d 20 69 44 6f 63 69 64 3b 0a 20 20 72 65 74 75  = iDocid;.  retu
34014 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
34015 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ./* This functio
34016 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  n implements the
34017 20 78 55 70 64 61 74 65 20 63 61 6c 6c 62 61 63   xUpdate callbac
34018 6b 3b 20 69 74 20 69 73 20 74 68 65 20 74 6f 70  k; it is the top
34019 2d 6c 65 76 65 6c 20 65 6e 74 72 79 0a 20 2a 20  -level entry. * 
3401a 70 6f 69 6e 74 20 66 6f 72 20 69 6e 73 65 72 74  point for insert
3401b 69 6e 67 2c 20 64 65 6c 65 74 69 6e 67 20 6f 72  ing, deleting or
3401c 20 75 70 64 61 74 69 6e 67 20 61 20 72 6f 77 20   updating a row 
3401d 69 6e 20 61 20 66 75 6c 6c 2d 74 65 78 74 20 74  in a full-text t
3401e 61 62 6c 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20  able. */.static 
3401f 69 6e 74 20 66 75 6c 6c 74 65 78 74 55 70 64 61  int fulltextUpda
34020 74 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  te(sqlite3_vtab 
34021 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67  *pVtab, int nArg
34022 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
34023 2a 2a 70 70 41 72 67 2c 0a 20 20 20 20 20 20 20  **ppArg,.       
34024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34025 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
34026 2a 70 52 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c  *pRowid){.  full
34027 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28  text_vtab *v = (
34028 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
34029 20 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63   pVtab;.  int rc
3402a 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  ;..  FTSTRACE(("
3402b 46 54 53 33 20 55 70 64 61 74 65 20 25 70 5c 6e  FTS3 Update %p\n
3402c 22 2c 20 70 56 74 61 62 29 29 3b 0a 0a 20 20 69  ", pVtab));..  i
3402d 66 28 20 6e 41 72 67 3c 32 20 29 7b 0a 20 20 20  f( nArg<2 ){.   
3402e 20 72 63 20 3d 20 69 6e 64 65 78 5f 64 65 6c 65   rc = index_dele
3402f 74 65 28 76 2c 20 73 71 6c 69 74 65 33 5f 76 61  te(v, sqlite3_va
34030 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b  lue_int64(ppArg[
34031 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63  0]));.    if( rc
34032 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
34033 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 6a 75       /* If we ju
34034 73 74 20 64 65 6c 65 74 65 64 20 74 68 65 20 6c  st deleted the l
34035 61 73 74 20 72 6f 77 20 69 6e 20 74 68 65 20 74  ast row in the t
34036 61 62 6c 65 2c 20 63 6c 65 61 72 20 6f 75 74 20  able, clear out 
34037 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64  the.      ** ind
34038 65 78 20 64 61 74 61 2e 0a 20 20 20 20 20 20 2a  ex data..      *
34039 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e  /.      rc = con
3403a 74 65 6e 74 5f 65 78 69 73 74 73 28 76 29 3b 0a  tent_exists(v);.
3403b 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
3403c 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
3403d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
3403e 4f 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  OK;.      }else 
3403f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
34040 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ONE ){.        /
34041 2a 20 43 6c 65 61 72 20 74 68 65 20 70 65 6e 64  * Clear the pend
34042 69 6e 67 20 74 65 72 6d 73 20 73 6f 20 77 65 20  ing terms so we 
34043 64 6f 6e 27 74 20 66 6c 75 73 68 20 61 20 75 73  don't flush a us
34044 65 6c 65 73 73 20 6c 65 76 65 6c 2d 30 0a 20 20  eless level-0.  
34045 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74        ** segment
34046 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61   when the transa
34047 63 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20 20  ction closes..  
34048 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
34049 20 72 63 20 3d 20 63 6c 65 61 72 50 65 6e 64 69   rc = clearPendi
3404a 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20  ngTerms(v);.    
3404b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
3404c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
3404d 20 20 20 72 63 20 3d 20 73 65 67 64 69 72 5f 64     rc = segdir_d
3404e 65 6c 65 74 65 5f 61 6c 6c 28 76 29 3b 0a 20 20  elete_all(v);.  
3404f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
34050 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 69      }.  } else i
34051 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
34052 5f 74 79 70 65 28 70 70 41 72 67 5b 30 5d 29 20  _type(ppArg[0]) 
34053 21 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  != SQLITE_NULL )
34054 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 75 70 64 61  {.    /* An upda
34055 74 65 3a 0a 20 20 20 20 20 2a 20 70 70 41 72 67  te:.     * ppArg
34056 5b 30 5d 20 3d 20 6f 6c 64 20 72 6f 77 69 64 0a  [0] = old rowid.
34057 20 20 20 20 20 2a 20 70 70 41 72 67 5b 31 5d 20       * ppArg[1] 
34058 3d 20 6e 65 77 20 72 6f 77 69 64 0a 20 20 20 20  = new rowid.    
34059 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d   * ppArg[2..2+v-
3405a 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61  >nColumn-1] = va
3405b 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70 41 72  lues.     * ppAr
3405c 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20  g[2+v->nColumn] 
3405d 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61 67 69  = value for magi
3405e 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e  c column (we ign
3405f 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20 20 2a  ore this).     *
34060 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
34061 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66  umn+1] = value f
34062 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f  or docid.     */
34063 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
34064 34 20 72 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  4 rowid = sqlite
34065 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70  3_value_int64(pp
34066 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  Arg[0]);.    if(
34067 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
34068 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20 21 3d  ype(ppArg[1]) !=
34069 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20   SQLITE_INTEGER 
3406a 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ||.        sqlit
3406b 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70  e3_value_int64(p
3406c 70 41 72 67 5b 31 5d 29 20 21 3d 20 72 6f 77 69  pArg[1]) != rowi
3406d 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  d ){.      rc = 
3406e 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f  SQLITE_ERROR;  /
3406f 2a 20 77 65 20 64 6f 6e 27 74 20 61 6c 6c 6f 77  * we don't allow
34070 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 72 6f   changing the ro
34071 77 69 64 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65  wid */.    }else
34072 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
34073 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b 32 2b  ue_type(ppArg[2+
34074 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21  v->nColumn+1]) !
34075 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
34076 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
34077 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
34078 69 6e 74 36 34 28 70 70 41 72 67 5b 32 2b 76 2d  int64(ppArg[2+v-
34079 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20  >nColumn+1]) != 
3407a 72 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72  rowid ){.      r
3407b 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
3407c 3b 20 20 2f 2a 20 77 65 20 64 6f 6e 27 74 20 61  ;  /* we don't a
3407d 6c 6c 6f 77 20 63 68 61 6e 67 69 6e 67 20 74 68  llow changing th
3407e 65 20 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 7d  e docid */.    }
3407f 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
34080 72 74 28 20 6e 41 72 67 3d 3d 32 2b 76 2d 3e 6e  rt( nArg==2+v->n
34081 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 20  Column+2);.     
34082 20 72 63 20 3d 20 69 6e 64 65 78 5f 75 70 64 61   rc = index_upda
34083 74 65 28 76 2c 20 72 6f 77 69 64 2c 20 26 70 70  te(v, rowid, &pp
34084 41 72 67 5b 32 5d 29 3b 0a 20 20 20 20 7d 0a 20  Arg[2]);.    }. 
34085 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a   } else {.    /*
34086 20 41 6e 20 69 6e 73 65 72 74 3a 0a 20 20 20 20   An insert:.    
34087 20 2a 20 70 70 41 72 67 5b 31 5d 20 3d 20 72 65   * ppArg[1] = re
34088 71 75 65 73 74 65 64 20 72 6f 77 69 64 0a 20 20  quested rowid.  
34089 20 20 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b     * ppArg[2..2+
3408a 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20  v->nColumn-1] = 
3408b 76 61 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70  values.     * pp
3408c 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e  Arg[2+v->nColumn
3408d 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61  ] = value for ma
3408e 67 69 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69  gic column (we i
3408f 67 6e 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20  gnore this).    
34090 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43   * ppArg[2+v->nC
34091 6f 6c 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65  olumn+1] = value
34092 20 66 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20   for docid.     
34093 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  */.    sqlite3_v
34094 61 6c 75 65 20 2a 70 52 65 71 75 65 73 74 44 6f  alue *pRequestDo
34095 63 69 64 20 3d 20 70 70 41 72 67 5b 32 2b 76 2d  cid = ppArg[2+v-
34096 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 3b 0a 20 20 20  >nColumn+1];.   
34097 20 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 32   assert( nArg==2
34098 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a  +v->nColumn+2);.
34099 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e      if( SQLITE_N
3409a 55 4c 4c 20 21 3d 20 73 71 6c 69 74 65 33 5f 76  ULL != sqlite3_v
3409b 61 6c 75 65 5f 74 79 70 65 28 70 52 65 71 75 65  alue_type(pReque
3409c 73 74 44 6f 63 69 64 29 20 26 26 0a 20 20 20 20  stDocid) &&.    
3409d 20 20 20 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20      SQLITE_NULL 
3409e 21 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  != sqlite3_value
3409f 5f 74 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20  _type(ppArg[1]) 
340a0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f  ){.      /* TODO
340a1 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72  (shess) Consider
340a2 20 61 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 74   allowing this t
340a3 6f 20 77 6f 72 6b 20 69 66 20 74 68 65 20 76 61  o work if the va
340a4 6c 75 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a  lues are.      *
340a5 2a 20 69 64 65 6e 74 69 63 61 6c 2e 20 20 49 27  * identical.  I'
340a6 6d 20 69 6e 63 6c 69 6e 65 64 20 74 6f 20 64 69  m inclined to di
340a7 73 63 6f 75 72 61 67 65 20 74 68 61 74 20 75 73  scourage that us
340a8 61 67 65 2c 20 74 68 6f 75 67 68 2c 0a 20 20 20  age, though,.   
340a9 20 20 20 2a 2a 20 67 69 76 65 6e 20 74 68 61 74     ** given that
340aa 20 62 6f 74 68 20 72 6f 77 69 64 20 61 6e 64 20   both rowid and 
340ab 64 6f 63 69 64 20 61 72 65 20 73 70 65 63 69 61  docid are specia
340ac 6c 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 65 74 74  l columns.  Bett
340ad 65 72 0a 20 20 20 20 20 20 2a 2a 20 77 6f 75 6c  er.      ** woul
340ae 64 20 62 65 20 74 6f 20 64 65 66 69 6e 65 20 6f  d be to define o
340af 6e 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  ne or the other 
340b0 61 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 77  as the default w
340b1 69 6e 6e 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20  inner,.      ** 
340b2 62 75 74 20 73 68 6f 75 6c 64 20 69 74 20 62 65  but should it be
340b3 20 66 74 73 33 2d 63 65 6e 74 72 69 63 20 28 64   fts3-centric (d
340b4 6f 63 69 64 29 20 6f 72 20 53 51 4c 69 74 65 2d  ocid) or SQLite-
340b5 63 65 6e 74 72 69 63 0a 20 20 20 20 20 20 2a 2a  centric.      **
340b6 20 28 72 6f 77 69 64 29 3f 0a 20 20 20 20 20 20   (rowid)?.      
340b7 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  */.      rc = SQ
340b8 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
340b9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
340ba 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 3d 3d 20   SQLITE_NULL == 
340bb 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
340bc 70 65 28 70 52 65 71 75 65 73 74 44 6f 63 69 64  pe(pRequestDocid
340bd 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65  ) ){.        pRe
340be 71 75 65 73 74 44 6f 63 69 64 20 3d 20 70 70 41  questDocid = ppA
340bf 72 67 5b 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20  rg[1];.      }. 
340c0 20 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f       rc = index_
340c1 69 6e 73 65 72 74 28 76 2c 20 70 52 65 71 75 65  insert(v, pReque
340c2 73 74 44 6f 63 69 64 2c 20 26 70 70 41 72 67 5b  stDocid, &ppArg[
340c3 32 5d 2c 20 70 52 6f 77 69 64 29 3b 0a 20 20 20  2], pRowid);.   
340c4 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
340c5 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69   rc;.}..static i
340c6 6e 74 20 66 75 6c 6c 74 65 78 74 53 79 6e 63 28  nt fulltextSync(
340c7 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
340c8 74 61 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45  tab){.  FTSTRACE
340c9 28 28 22 46 54 53 33 20 78 53 79 6e 63 28 29 5c  (("FTS3 xSync()\
340ca 6e 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 66  n"));.  return f
340cb 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73  lushPendingTerms
340cc 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  ((fulltext_vtab 
340cd 2a 29 70 56 74 61 62 29 3b 0a 7d 0a 0a 73 74 61  *)pVtab);.}..sta
340ce 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
340cf 42 65 67 69 6e 28 73 71 6c 69 74 65 33 5f 76 74  Begin(sqlite3_vt
340d0 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 66 75  ab *pVtab){.  fu
340d1 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
340d2 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20   (fulltext_vtab 
340d3 2a 29 20 70 56 74 61 62 3b 0a 20 20 46 54 53 54  *) pVtab;.  FTST
340d4 52 41 43 45 28 28 22 46 54 53 33 20 78 42 65 67  RACE(("FTS3 xBeg
340d5 69 6e 28 29 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a  in()\n"));..  /*
340d6 20 41 6e 79 20 62 75 66 66 65 72 65 64 20 75 70   Any buffered up
340d7 64 61 74 65 73 20 73 68 6f 75 6c 64 20 68 61 76  dates should hav
340d8 65 20 62 65 65 6e 20 63 6c 65 61 72 65 64 20 62  e been cleared b
340d9 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 20  y the previous. 
340da 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
340db 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
340dc 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  v->nPendingData<
340dd 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c  0 );.  return cl
340de 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  earPendingTerms(
340df 76 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  v);.}..static in
340e0 74 20 66 75 6c 6c 74 65 78 74 43 6f 6d 6d 69 74  t fulltextCommit
340e1 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
340e2 56 74 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78  Vtab){.  fulltex
340e3 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c  t_vtab *v = (ful
340e4 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56  ltext_vtab *) pV
340e5 74 61 62 3b 0a 20 20 46 54 53 54 52 41 43 45 28  tab;.  FTSTRACE(
340e6 28 22 46 54 53 33 20 78 43 6f 6d 6d 69 74 28 29  ("FTS3 xCommit()
340e7 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 42 75 66  \n"));..  /* Buf
340e8 66 65 72 65 64 20 75 70 64 61 74 65 73 20 73 68  fered updates sh
340e9 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 63  ould have been c
340ea 6c 65 61 72 65 64 20 62 79 20 66 75 6c 6c 74 65  leared by fullte
340eb 78 74 53 79 6e 63 28 29 2e 20 2a 2f 0a 20 20 61  xtSync(). */.  a
340ec 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69  ssert( v->nPendi
340ed 6e 67 44 61 74 61 3c 30 20 29 3b 0a 20 20 72 65  ngData<0 );.  re
340ee 74 75 72 6e 20 63 6c 65 61 72 50 65 6e 64 69 6e  turn clearPendin
340ef 67 54 65 72 6d 73 28 76 29 3b 0a 7d 0a 0a 73 74  gTerms(v);.}..st
340f0 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
340f1 74 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65  tRollback(sqlite
340f2 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
340f3 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
340f4 33 20 78 52 6f 6c 6c 62 61 63 6b 28 29 5c 6e 22  3 xRollback()\n"
340f5 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c 65  ));.  return cle
340f6 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 28  arPendingTerms((
340f7 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
340f8 70 56 74 61 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pVtab);.}../*.**
340f9 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
340fa 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 28 29  of the snippet()
340fb 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54   function for FT
340fc 53 33 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  S3.*/.static voi
340fd 64 20 73 6e 69 70 70 65 74 46 75 6e 63 28 0a 20  d snippetFunc(. 
340fe 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
340ff 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e   *pContext,.  in
34100 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
34101 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
34102 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  {.  fulltext_cur
34103 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20  sor *pCursor;.  
34104 69 66 28 20 61 72 67 63 3c 31 20 29 20 72 65 74  if( argc<1 ) ret
34105 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  urn;.  if( sqlit
34106 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
34107 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42  gv[0])!=SQLITE_B
34108 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c  LOB ||.      sql
34109 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
3410a 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f  (argv[0])!=sizeo
3410b 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20  f(pCursor) ){.  
3410c 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3410d 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  _error(pContext,
3410e 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20   "illegal first 
3410f 61 72 67 75 6d 65 6e 74 20 74 6f 20 68 74 6d 6c  argument to html
34110 5f 73 6e 69 70 70 65 74 22 2c 2d 31 29 3b 0a 20  _snippet",-1);. 
34111 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73   }else{.    cons
34112 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d  t char *zStart =
34113 20 22 3c 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73   "<b>";.    cons
34114 74 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 22  t char *zEnd = "
34115 3c 2f 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73 74  </b>";.    const
34116 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73   char *zEllipsis
34117 20 3d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b   = "<b>...</b>";
34118 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75  .    memcpy(&pCu
34119 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61  rsor, sqlite3_va
3411a 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
3411b 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  ), sizeof(pCurso
3411c 72 29 29 3b 0a 20 20 20 20 69 66 28 20 61 72 67  r));.    if( arg
3411d 63 3e 3d 32 20 29 7b 0a 20 20 20 20 20 20 7a 53  c>=2 ){.      zS
3411e 74 61 72 74 20 3d 20 28 63 6f 6e 73 74 20 63 68  tart = (const ch
3411f 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
34120 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
34121 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3e  .      if( argc>
34122 3d 33 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 45  =3 ){.        zE
34123 6e 64 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  nd = (const char
34124 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
34125 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20  text(argv[2]);. 
34126 20 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3e         if( argc>
34127 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =4 ){.          
34128 7a 45 6c 6c 69 70 73 69 73 20 3d 20 28 63 6f 6e  zEllipsis = (con
34129 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33  st char*)sqlite3
3412a 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
3412b 5b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  [3]);.        }.
3412c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
3412d 20 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73    snippetAllOffs
3412e 65 74 73 28 70 43 75 72 73 6f 72 29 3b 0a 20 20  ets(pCursor);.  
3412f 20 20 73 6e 69 70 70 65 74 54 65 78 74 28 70 43    snippetText(pC
34130 75 72 73 6f 72 2c 20 7a 53 74 61 72 74 2c 20 7a  ursor, zStart, z
34131 45 6e 64 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b  End, zEllipsis);
34132 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
34133 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
34134 74 2c 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70  t, pCursor->snip
34135 70 65 74 2e 7a 53 6e 69 70 70 65 74 2c 0a 20 20  pet.zSnippet,.  
34136 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34137 20 20 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 73        pCursor->s
34138 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70 65 74 2c  nippet.nSnippet,
34139 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
3413a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
3413b 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
3413c 74 68 65 20 6f 66 66 73 65 74 73 28 29 20 66 75  the offsets() fu
3413d 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a  nction for FTS3.
3413e 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
3413f 6e 69 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e  nippetOffsetsFun
34140 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
34141 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a  text *pContext,.
34142 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
34143 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
34144 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  gv.){.  fulltext
34145 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
34146 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 29  ;.  if( argc<1 )
34147 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73   return;.  if( s
34148 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
34149 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49  e(argv[0])!=SQLI
3414a 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20  TE_BLOB ||.     
3414b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
3414c 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73  ytes(argv[0])!=s
3414d 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29  izeof(pCursor) )
3414e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
3414f 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74  sult_error(pCont
34150 65 78 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69  ext, "illegal fi
34151 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
34152 6f 66 66 73 65 74 73 22 2c 2d 31 29 3b 0a 20 20  offsets",-1);.  
34153 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70  }else{.    memcp
34154 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69  y(&pCursor, sqli
34155 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
34156 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28  rgv[0]), sizeof(
34157 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 73  pCursor));.    s
34158 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73  nippetAllOffsets
34159 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73  (pCursor);.    s
3415a 6e 69 70 70 65 74 4f 66 66 73 65 74 54 65 78 74  nippetOffsetText
3415b 28 26 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70  (&pCursor->snipp
3415c 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  et);.    sqlite3
3415d 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
3415e 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
3415f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
34160 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
34161 7a 4f 66 66 73 65 74 2c 20 70 43 75 72 73 6f 72  zOffset, pCursor
34162 2d 3e 73 6e 69 70 70 65 74 2e 6e 4f 66 66 73 65  ->snippet.nOffse
34163 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
34164 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
34165 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d  E_STATIC);.  }.}
34166 0a 0a 2f 2a 20 4f 70 74 4c 65 61 76 65 73 52 65  ../* OptLeavesRe
34167 61 64 65 72 20 69 73 20 6e 65 61 72 6c 79 20 69  ader is nearly i
34168 64 65 6e 74 69 63 61 6c 20 74 6f 20 4c 65 61 76  dentical to Leav
34169 65 73 52 65 61 64 65 72 2c 20 65 78 63 65 70 74  esReader, except
3416a 20 74 68 61 74 0a 2a 2a 20 77 68 65 72 65 20 4c   that.** where L
3416b 65 61 76 65 73 52 65 61 64 65 72 20 69 73 20 67  eavesReader is g
3416c 65 61 72 65 64 20 74 6f 77 61 72 64 73 20 74 68  eared towards th
3416d 65 20 6d 65 72 67 69 6e 67 20 6f 66 20 63 6f 6d  e merging of com
3416e 70 6c 65 74 65 0a 2a 2a 20 73 65 67 6d 65 6e 74  plete.** segment
3416f 20 6c 65 76 65 6c 73 20 28 77 69 74 68 20 65 78   levels (with ex
34170 61 63 74 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e  actly MERGE_COUN
34171 54 20 73 65 67 6d 65 6e 74 73 29 2c 20 4f 70 74  T segments), Opt
34172 4c 65 61 76 65 73 52 65 61 64 65 72 0a 2a 2a 20  LeavesReader.** 
34173 69 73 20 67 65 61 72 65 64 20 74 6f 77 61 72 64  is geared toward
34174 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
34175 20 6f 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 65   of the optimize
34176 28 29 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 64  () function, and
34177 0a 2a 2a 20 63 61 6e 20 6d 65 72 67 65 20 61 6c  .** can merge al
34178 6c 20 73 65 67 6d 65 6e 74 73 20 73 69 6d 75 6c  l segments simul
34179 74 61 6e 65 6f 75 73 6c 79 2e 20 20 54 68 69 73  taneously.  This
3417a 20 76 65 72 73 69 6f 6e 20 6d 61 79 20 62 65 0a   version may be.
3417b 2a 2a 20 73 6f 6d 65 77 68 61 74 20 6c 65 73 73  ** somewhat less
3417c 20 65 66 66 69 63 69 65 6e 74 20 74 68 61 6e 20   efficient than 
3417d 4c 65 61 76 65 73 52 65 61 64 65 72 20 62 65 63  LeavesReader bec
3417e 61 75 73 65 20 69 74 20 6d 65 72 67 65 73 20 69  ause it merges i
3417f 6e 74 6f 20 61 6e 0a 2a 2a 20 61 63 63 75 6d 75  nto an.** accumu
34180 6c 61 74 6f 72 20 72 61 74 68 65 72 20 74 68 61  lator rather tha
34181 6e 20 64 6f 69 6e 67 20 61 6e 20 4e 2d 77 61 79  n doing an N-way
34182 20 6d 65 72 67 65 2c 20 62 75 74 20 73 69 6e 63   merge, but sinc
34183 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 69 7a  e segment.** siz
34184 65 20 67 72 6f 77 73 20 65 78 70 6f 6e 65 6e 74  e grows exponent
34185 69 61 6c 6c 79 20 28 73 6f 20 73 65 67 6d 65 6e  ially (so segmen
34186 74 20 63 6f 75 6e 74 20 6c 6f 67 72 69 74 68 6d  t count logrithm
34187 69 63 61 6c 6c 79 29 20 74 68 69 73 20 69 73 0a  ically) this is.
34188 2a 2a 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20  ** probably not 
34189 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 70 72 6f  an immediate pro
3418a 62 6c 65 6d 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  blem..*/./* TODO
3418b 28 73 68 65 73 73 29 3a 20 50 72 6f 76 65 20 74  (shess): Prove t
3418c 68 61 74 20 61 73 73 65 72 74 69 6f 6e 2c 20 6f  hat assertion, o
3418d 72 20 65 78 74 65 6e 64 20 74 68 65 20 6d 65 72  r extend the mer
3418e 67 65 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 6d 65  ge code to.** me
3418f 72 67 65 20 74 72 65 65 20 66 61 73 68 69 6f 6e  rge tree fashion
34190 20 28 6c 69 6b 65 20 74 68 65 20 70 72 65 66 69   (like the prefi
34191 78 2d 73 65 61 72 63 68 69 6e 67 20 63 6f 64 65  x-searching code
34192 20 64 6f 65 73 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f   does)..*/./* TO
34193 44 4f 28 73 68 65 73 73 29 3a 20 4f 70 74 4c 65  DO(shess): OptLe
34194 61 76 65 73 52 65 61 64 65 72 20 61 6e 64 20 4c  avesReader and L
34195 65 61 76 65 73 52 65 61 64 65 72 20 63 6f 75 6c  eavesReader coul
34196 64 20 70 72 6f 62 61 62 6c 79 20 62 65 0a 2a 2a  d probably be.**
34197 20 6d 65 72 67 65 64 20 77 69 74 68 20 6c 69 74   merged with lit
34198 74 6c 65 20 6f 72 20 6e 6f 20 6c 6f 73 73 20 6f  tle or no loss o
34199 66 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 66 6f  f performance fo
3419a 72 20 4c 65 61 76 65 73 52 65 61 64 65 72 2e 20  r LeavesReader. 
3419b 20 54 68 65 0a 2a 2a 20 6d 65 72 67 65 64 20 63   The.** merged c
3419c 6f 64 65 20 77 6f 75 6c 64 20 6e 65 65 64 20 74  ode would need t
3419d 6f 20 68 61 6e 64 6c 65 20 3e 4d 45 52 47 45 5f  o handle >MERGE_
3419e 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c 20  COUNT segments, 
3419f 61 6e 64 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73  and would.** als
341a0 6f 20 6e 65 65 64 20 74 6f 20 62 65 20 61 62 6c  o need to be abl
341a1 65 20 74 6f 20 6f 70 74 69 6f 6e 61 6c 6c 79 20  e to optionally 
341a2 6f 70 74 69 6d 69 7a 65 20 61 77 61 79 20 64 65  optimize away de
341a3 6c 65 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  letes..*/.typede
341a4 66 20 73 74 72 75 63 74 20 4f 70 74 4c 65 61 76  f struct OptLeav
341a5 65 73 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20  esReader {.  /* 
341a6 53 65 67 6d 65 6e 74 20 6e 75 6d 62 65 72 2c 20  Segment number, 
341a7 74 6f 20 6f 72 64 65 72 20 72 65 61 64 65 72 73  to order readers
341a8 20 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 69 6e   by age. */.  in
341a9 74 20 73 65 67 6d 65 6e 74 3b 0a 20 20 4c 65 61  t segment;.  Lea
341aa 76 65 73 52 65 61 64 65 72 20 72 65 61 64 65 72  vesReader reader
341ab 3b 0a 7d 20 4f 70 74 4c 65 61 76 65 73 52 65 61  ;.} OptLeavesRea
341ac 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  der;..static int
341ad 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
341ae 41 74 45 6e 64 28 4f 70 74 4c 65 61 76 65 73 52  AtEnd(OptLeavesR
341af 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
341b0 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73  .  return leaves
341b1 52 65 61 64 65 72 41 74 45 6e 64 28 26 70 52 65  ReaderAtEnd(&pRe
341b2 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d  ader->reader);.}
341b3 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c  .static int optL
341b4 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42  eavesReaderTermB
341b5 79 74 65 73 28 4f 70 74 4c 65 61 76 65 73 52 65  ytes(OptLeavesRe
341b6 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
341b7 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52    return leavesR
341b8 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 26  eaderTermBytes(&
341b9 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29  pReader->reader)
341ba 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.}.static const
341bb 20 63 68 61 72 20 2a 6f 70 74 4c 65 61 76 65 73   char *optLeaves
341bc 52 65 61 64 65 72 44 61 74 61 28 4f 70 74 4c 65  ReaderData(OptLe
341bd 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61  avesReader *pRea
341be 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c  der){.  return l
341bf 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
341c0 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72  &pReader->reader
341c1 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
341c2 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
341c3 61 74 61 42 79 74 65 73 28 4f 70 74 4c 65 61 76  ataBytes(OptLeav
341c4 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
341c5 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  r){.  return lea
341c6 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
341c7 65 73 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61  es(&pReader->rea
341c8 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63  der);.}.static c
341c9 6f 6e 73 74 20 63 68 61 72 20 2a 6f 70 74 4c 65  onst char *optLe
341ca 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 4f  avesReaderTerm(O
341cb 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
341cc 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75  pReader){.  retu
341cd 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 54  rn leavesReaderT
341ce 65 72 6d 28 26 70 52 65 61 64 65 72 2d 3e 72 65  erm(&pReader->re
341cf 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
341d0 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61  int optLeavesRea
341d1 64 65 72 53 74 65 70 28 66 75 6c 6c 74 65 78 74  derStep(fulltext
341d2 5f 76 74 61 62 20 2a 76 2c 20 4f 70 74 4c 65 61  _vtab *v, OptLea
341d3 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
341d4 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65  er){.  return le
341d5 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76  avesReaderStep(v
341d6 2c 20 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64  , &pReader->read
341d7 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  er);.}.static in
341d8 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  t optLeavesReade
341d9 72 54 65 72 6d 43 6d 70 28 4f 70 74 4c 65 61 76  rTermCmp(OptLeav
341da 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f  esReader *lr1, O
341db 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
341dc 6c 72 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c  lr2){.  return l
341dd 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 43  eavesReaderTermC
341de 6d 70 28 26 6c 72 31 2d 3e 72 65 61 64 65 72 2c  mp(&lr1->reader,
341df 20 26 6c 72 32 2d 3e 72 65 61 64 65 72 29 3b 0a   &lr2->reader);.
341e0 7d 0a 2f 2a 20 4f 72 64 65 72 20 62 79 20 74 65  }./* Order by te
341e1 72 6d 20 61 73 63 65 6e 64 69 6e 67 2c 20 73 65  rm ascending, se
341e2 67 6d 65 6e 74 20 61 73 63 65 6e 64 69 6e 67 20  gment ascending 
341e3 28 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73  (oldest to newes
341e4 74 29 2c 20 77 69 74 68 0a 2a 2a 20 65 78 68 61  t), with.** exha
341e5 75 73 74 65 64 20 72 65 61 64 65 72 73 20 74 6f  usted readers to
341e6 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
341e7 74 69 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65  tic int optLeave
341e8 73 52 65 61 64 65 72 43 6d 70 28 4f 70 74 4c 65  sReaderCmp(OptLe
341e9 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c  avesReader *lr1,
341ea 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   OptLeavesReader
341eb 20 2a 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20   *lr2){.  int c 
341ec 3d 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  = optLeavesReade
341ed 72 54 65 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72  rTermCmp(lr1, lr
341ee 32 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29  2);.  if( c!=0 )
341ef 20 72 65 74 75 72 6e 20 63 3b 0a 20 20 72 65 74   return c;.  ret
341f0 75 72 6e 20 6c 72 31 2d 3e 73 65 67 6d 65 6e 74  urn lr1->segment
341f1 2d 6c 72 32 2d 3e 73 65 67 6d 65 6e 74 3b 0a 7d  -lr2->segment;.}
341f2 0a 2f 2a 20 42 75 62 62 6c 65 20 70 4c 72 5b 30  ./* Bubble pLr[0
341f3 5d 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65  ] to appropriate
341f4 20 70 6c 61 63 65 20 69 6e 20 70 4c 72 5b 31 2e   place in pLr[1.
341f5 2e 6e 4c 72 2d 31 5d 2e 20 20 41 73 73 75 6d 65  .nLr-1].  Assume
341f6 73 20 74 68 61 74 0a 2a 2a 20 70 4c 72 5b 31 2e  s that.** pLr[1.
341f7 2e 6e 4c 72 2d 31 5d 20 69 73 20 61 6c 72 65 61  .nLr-1] is alrea
341f8 64 79 20 73 6f 72 74 65 64 2e 0a 2a 2f 0a 73 74  dy sorted..*/.st
341f9 61 74 69 63 20 76 6f 69 64 20 6f 70 74 4c 65 61  atic void optLea
341fa 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72  vesReaderReorder
341fb 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  (OptLeavesReader
341fc 20 2a 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b   *pLr, int nLr){
341fd 0a 20 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20  .  while( nLr>1 
341fe 26 26 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64  && optLeavesRead
341ff 65 72 43 6d 70 28 70 4c 72 2c 20 70 4c 72 2b 31  erCmp(pLr, pLr+1
34200 29 3e 30 20 29 7b 0a 20 20 20 20 4f 70 74 4c 65  )>0 ){.    OptLe
34201 61 76 65 73 52 65 61 64 65 72 20 74 6d 70 20 3d  avesReader tmp =
34202 20 70 4c 72 5b 30 5d 3b 0a 20 20 20 20 70 4c 72   pLr[0];.    pLr
34203 5b 30 5d 20 3d 20 70 4c 72 5b 31 5d 3b 0a 20 20  [0] = pLr[1];.  
34204 20 20 70 4c 72 5b 31 5d 20 3d 20 74 6d 70 3b 0a    pLr[1] = tmp;.
34205 20 20 20 20 6e 4c 72 2d 2d 3b 0a 20 20 20 20 70      nLr--;.    p
34206 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  Lr++;.  }.}../* 
34207 6f 70 74 69 6d 69 7a 65 28 29 20 68 65 6c 70 65  optimize() helpe
34208 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20 50 75 74  r function.  Put
34209 20 74 68 65 20 72 65 61 64 65 72 73 20 69 6e 20   the readers in 
3420a 6f 72 64 65 72 20 61 6e 64 20 69 74 65 72 61 74  order and iterat
3420b 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65  e.** through the
3420c 6d 2c 20 6d 65 72 67 69 6e 67 20 64 6f 63 6c 69  m, merging docli
3420d 73 74 73 20 66 6f 72 20 6d 61 74 63 68 69 6e 67  sts for matching
3420e 20 74 65 72 6d 73 20 69 6e 74 6f 20 70 57 72 69   terms into pWri
3420f 74 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20  ter..** Returns 
34210 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
34211 63 65 73 73 2c 20 6f 72 20 74 68 65 20 53 51 4c  cess, or the SQL
34212 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 77  ite error code w
34213 68 69 63 68 0a 2a 2a 20 70 72 65 76 65 6e 74 65  hich.** prevente
34214 64 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74  d success..*/.st
34215 61 74 69 63 20 69 6e 74 20 6f 70 74 69 6d 69 7a  atic int optimiz
34216 65 49 6e 74 65 72 6e 61 6c 28 66 75 6c 6c 74 65  eInternal(fullte
34217 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
34218 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34219 20 20 20 20 20 20 20 20 4f 70 74 4c 65 61 76 65          OptLeave
3421a 73 52 65 61 64 65 72 20 2a 72 65 61 64 65 72 73  sReader *readers
3421b 2c 20 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a  , int nReaders,.
3421c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3421d 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
3421e 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29  Writer *pWriter)
3421f 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20  {.  int i, rc = 
34220 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 44 61 74  SQLITE_OK;.  Dat
34221 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 2c  aBuffer doclist,
34222 20 6d 65 72 67 65 64 2c 20 74 6d 70 3b 0a 0a 20   merged, tmp;.. 
34223 20 2f 2a 20 4f 72 64 65 72 20 74 68 65 20 72 65   /* Order the re
34224 61 64 65 72 73 2e 20 2a 2f 0a 20 20 69 20 3d 20  aders. */.  i = 
34225 6e 52 65 61 64 65 72 73 3b 0a 20 20 77 68 69 6c  nReaders;.  whil
34226 65 28 20 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20  e( i-- > 0 ){.  
34227 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65    optLeavesReade
34228 72 52 65 6f 72 64 65 72 28 26 72 65 61 64 65 72  rReorder(&reader
34229 73 5b 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69  s[i], nReaders-i
3422a 29 3b 0a 20 20 7d 0a 0a 20 20 64 61 74 61 42 75  );.  }..  dataBu
3422b 66 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73  fferInit(&doclis
3422c 74 2c 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 20 20  t, LEAF_MAX);.  
3422d 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
3422e 6d 65 72 67 65 64 2c 20 4c 45 41 46 5f 4d 41 58  merged, LEAF_MAX
3422f 29 3b 0a 0a 20 20 2f 2a 20 45 78 68 61 75 73 74  );..  /* Exhaust
34230 65 64 20 72 65 61 64 65 72 73 20 62 75 62 62 6c  ed readers bubbl
34231 65 20 74 6f 20 74 68 65 20 65 6e 64 2c 20 73 6f  e to the end, so
34232 20 77 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   when the first 
34233 72 65 61 64 65 72 20 69 73 0a 20 20 2a 2a 20 61  reader is.  ** a
34234 74 20 65 6f 66 2c 20 61 6c 6c 20 61 72 65 20 61  t eof, all are a
34235 74 20 65 6f 66 2e 0a 20 20 2a 2f 0a 20 20 77 68  t eof..  */.  wh
34236 69 6c 65 28 20 21 6f 70 74 4c 65 61 76 65 73 52  ile( !optLeavesR
34237 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64  eaderAtEnd(&read
34238 65 72 73 5b 30 5d 29 20 29 7b 0a 0a 20 20 20 20  ers[0]) ){..    
34239 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
3423a 77 20 6d 61 6e 79 20 72 65 61 64 65 72 73 20 73  w many readers s
3423b 68 61 72 65 20 74 68 65 20 6e 65 78 74 20 74 65  hare the next te
3423c 72 6d 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  rm. */.    for(i
3423d 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72 73 20 26  =1; i<nReaders &
3423e 26 20 21 6f 70 74 4c 65 61 76 65 73 52 65 61 64  & !optLeavesRead
3423f 65 72 41 74 45 6e 64 28 26 72 65 61 64 65 72 73  erAtEnd(&readers
34240 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  [i]); i++){.    
34241 20 20 69 66 28 20 30 21 3d 6f 70 74 4c 65 61 76    if( 0!=optLeav
34242 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  esReaderTermCmp(
34243 26 72 65 61 64 65 72 73 5b 30 5d 2c 20 26 72 65  &readers[0], &re
34244 61 64 65 72 73 5b 69 5d 29 20 29 20 62 72 65 61  aders[i]) ) brea
34245 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
34246 20 53 70 65 63 69 61 6c 2d 63 61 73 65 20 66 6f   Special-case fo
34247 72 20 6e 6f 20 6d 65 72 67 65 2e 20 2a 2f 0a 20  r no merge. */. 
34248 20 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a 20     if( i==1 ){. 
34249 20 20 20 20 20 2f 2a 20 54 72 69 6d 20 64 65 6c       /* Trim del
3424a 65 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20  etions from the 
3424b 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20  doclist. */.    
3424c 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65    dataBufferRese
3424d 74 28 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20  t(&merged);.    
3424e 20 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c    docListTrim(DL
3424f 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20  _DEFAULT,.      
34250 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c              optL
34251 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
34252 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20  &readers[0]),.  
34253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34254 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
34255 61 74 61 42 79 74 65 73 28 26 72 65 61 64 65 72  ataBytes(&reader
34256 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20  s[0]),.         
34257 20 20 20 20 20 20 20 20 20 2d 31 2c 20 44 4c 5f           -1, DL_
34258 44 45 46 41 55 4c 54 2c 20 26 6d 65 72 67 65 64  DEFAULT, &merged
34259 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
3425a 20 20 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52      DLReader dlR
3425b 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55  eaders[MERGE_COU
3425c 4e 54 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  NT];.      int i
3425d 52 65 61 64 65 72 2c 20 6e 52 65 61 64 65 72 73  Reader, nReaders
3425e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 50 72 69 6d  ;..      /* Prim
3425f 65 20 74 68 65 20 70 69 70 65 6c 69 6e 65 20 77  e the pipeline w
34260 69 74 68 20 74 68 65 20 66 69 72 73 74 20 72 65  ith the first re
34261 61 64 65 72 27 73 20 64 6f 63 6c 69 73 74 2e 20  ader's doclist. 
34262 20 41 66 74 65 72 0a 20 20 20 20 20 20 2a 2a 20   After.      ** 
34263 6f 6e 65 20 70 61 73 73 20 69 6e 64 65 78 20 30  one pass index 0
34264 20 77 69 6c 6c 20 72 65 66 65 72 65 6e 63 65 20   will reference 
34265 74 68 65 20 61 63 63 75 6d 75 6c 61 74 65 64 20  the accumulated 
34266 64 6f 63 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a  doclist..      *
34267 2f 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28  /.      dlrInit(
34268 26 64 6c 52 65 61 64 65 72 73 5b 30 5d 2c 20 44  &dlReaders[0], D
34269 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20  L_DEFAULT,.     
3426a 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76           optLeav
3426b 65 73 52 65 61 64 65 72 44 61 74 61 28 26 72 65  esReaderData(&re
3426c 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20  aders[0]),.     
3426d 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76           optLeav
3426e 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74 65  esReaderDataByte
3426f 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29 29 3b  s(&readers[0]));
34270 0a 20 20 20 20 20 20 69 52 65 61 64 65 72 20 3d  .      iReader =
34271 20 31 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72   1;..      asser
34272 74 28 20 69 52 65 61 64 65 72 3c 69 20 29 3b 20  t( iReader<i ); 
34273 20 2f 2a 20 4d 75 73 74 20 65 78 65 63 75 74 65   /* Must execute
34274 20 74 68 65 20 6c 6f 6f 70 20 61 74 20 6c 65 61   the loop at lea
34275 73 74 20 6f 6e 63 65 2e 20 2a 2f 0a 20 20 20 20  st once. */.    
34276 20 20 77 68 69 6c 65 28 20 69 52 65 61 64 65 72    while( iReader
34277 3c 69 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  <i ){.        /*
34278 20 4d 65 72 67 65 20 31 36 20 69 6e 70 75 74 73   Merge 16 inputs
34279 20 70 65 72 20 70 61 73 73 2e 20 2a 2f 0a 20 20   per pass. */.  
3427a 20 20 20 20 20 20 66 6f 72 28 20 6e 52 65 61 64        for( nRead
3427b 65 72 73 3d 31 3b 20 69 52 65 61 64 65 72 3c 69  ers=1; iReader<i
3427c 20 26 26 20 6e 52 65 61 64 65 72 73 3c 4d 45 52   && nReaders<MER
3427d 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 20 20 20 20  GE_COUNT;.      
3427e 20 20 20 20 20 20 20 69 52 65 61 64 65 72 2b 2b         iReader++
3427f 2c 20 6e 52 65 61 64 65 72 73 2b 2b 20 29 7b 0a  , nReaders++ ){.
34280 20 20 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69            dlrIni
34281 74 28 26 64 6c 52 65 61 64 65 72 73 5b 6e 52 65  t(&dlReaders[nRe
34282 61 64 65 72 73 5d 2c 20 44 4c 5f 44 45 46 41 55  aders], DL_DEFAU
34283 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LT,.            
34284 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52        optLeavesR
34285 65 61 64 65 72 44 61 74 61 28 26 72 65 61 64 65  eaderData(&reade
34286 72 73 5b 69 52 65 61 64 65 72 5d 29 2c 0a 20 20  rs[iReader]),.  
34287 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34288 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
34289 61 74 61 42 79 74 65 73 28 26 72 65 61 64 65 72  ataBytes(&reader
3428a 73 5b 69 52 65 61 64 65 72 5d 29 29 3b 0a 20 20  s[iReader]));.  
3428b 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
3428c 20 2f 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69 73   /* Merge doclis
3428d 74 73 20 61 6e 64 20 73 77 61 70 20 72 65 73 75  ts and swap resu
3428e 6c 74 20 69 6e 74 6f 20 61 63 63 75 6d 75 6c 61  lt into accumula
3428f 74 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tor. */.        
34290 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
34291 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 20 20  &merged);.      
34292 20 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26    docListMerge(&
34293 6d 65 72 67 65 64 2c 20 64 6c 52 65 61 64 65 72  merged, dlReader
34294 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20 20  s, nReaders);.  
34295 20 20 20 20 20 20 74 6d 70 20 3d 20 6d 65 72 67        tmp = merg
34296 65 64 3b 0a 20 20 20 20 20 20 20 20 6d 65 72 67  ed;.        merg
34297 65 64 20 3d 20 64 6f 63 6c 69 73 74 3b 0a 20 20  ed = doclist;.  
34298 20 20 20 20 20 20 64 6f 63 6c 69 73 74 20 3d 20        doclist = 
34299 74 6d 70 3b 0a 0a 20 20 20 20 20 20 20 20 77 68  tmp;..        wh
3429a 69 6c 65 28 20 6e 52 65 61 64 65 72 73 2d 2d 20  ile( nReaders-- 
3429b 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  > 0 ){.         
3429c 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52   dlrDestroy(&dlR
3429d 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72 73 5d  eaders[nReaders]
3429e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  );.        }..  
3429f 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
342a0 61 74 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20  ated doclist to 
342a1 72 65 61 64 65 72 20 30 20 66 6f 72 20 6e 65 78  reader 0 for nex
342a2 74 20 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20 20  t pass. */.     
342a3 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65     dlrInit(&dlRe
342a4 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46  aders[0], DL_DEF
342a5 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44  AULT, doclist.pD
342a6 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61  ata, doclist.nDa
342a7 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  ta);.      }..  
342a8 20 20 20 20 2f 2a 20 44 65 73 74 72 6f 79 20 72      /* Destroy r
342a9 65 61 64 65 72 20 74 68 61 74 20 77 61 73 20 6c  eader that was l
342aa 65 66 74 20 69 6e 20 74 68 65 20 70 69 70 65 6c  eft in the pipel
342ab 69 6e 65 2e 20 2a 2f 0a 20 20 20 20 20 20 64 6c  ine. */.      dl
342ac 72 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64  rDestroy(&dlRead
342ad 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20 20  ers[0]);..      
342ae 2f 2a 20 54 72 69 6d 20 64 65 6c 65 74 69 6f 6e  /* Trim deletion
342af 73 20 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c 69  s from the docli
342b0 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 64 61 74  st. */.      dat
342b1 61 42 75 66 66 65 72 52 65 73 65 74 28 26 6d 65  aBufferReset(&me
342b2 72 67 65 64 29 3b 0a 20 20 20 20 20 20 64 6f 63  rged);.      doc
342b3 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41  ListTrim(DL_DEFA
342b4 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61  ULT, doclist.pDa
342b5 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74  ta, doclist.nDat
342b6 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
342b7 20 20 20 20 20 2d 31 2c 20 44 4c 5f 44 45 46 41       -1, DL_DEFA
342b8 55 4c 54 2c 20 26 6d 65 72 67 65 64 29 3b 0a 20  ULT, &merged);. 
342b9 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c     }..    /* Onl
342ba 79 20 70 61 73 73 20 64 6f 63 6c 69 73 74 73 20  y pass doclists 
342bb 77 69 74 68 20 68 69 74 73 20 28 73 6b 69 70 20  with hits (skip 
342bc 69 66 20 61 6c 6c 20 68 69 74 73 20 64 65 6c 65  if all hits dele
342bd 74 65 64 29 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ted). */.    if(
342be 20 6d 65 72 67 65 64 2e 6e 44 61 74 61 3e 30 20   merged.nData>0 
342bf 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65  ){.      rc = le
342c0 61 66 57 72 69 74 65 72 53 74 65 70 28 76 2c 20  afWriterStep(v, 
342c1 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  pWriter,.       
342c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
342c3 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64     optLeavesRead
342c4 65 72 54 65 72 6d 28 26 72 65 61 64 65 72 73 5b  erTerm(&readers[
342c5 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0]),.           
342c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
342c7 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54 65  ptLeavesReaderTe
342c8 72 6d 42 79 74 65 73 28 26 72 65 61 64 65 72 73  rmBytes(&readers
342c9 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  [0]),.          
342ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
342cb 6d 65 72 67 65 64 2e 70 44 61 74 61 2c 20 6d 65  merged.pData, me
342cc 72 67 65 64 2e 6e 44 61 74 61 29 3b 0a 20 20 20  rged.nData);.   
342cd 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
342ce 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
342cf 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
342d0 74 65 70 20 6d 65 72 67 65 64 20 72 65 61 64 65  tep merged reade
342d1 72 73 20 74 6f 20 6e 65 78 74 20 74 65 72 6d 20  rs to next term 
342d2 61 6e 64 20 72 65 6f 72 64 65 72 2e 20 2a 2f 0a  and reorder. */.
342d3 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e      while( i-- >
342d4 20 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d   0 ){.      rc =
342d5 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
342d6 53 74 65 70 28 76 2c 20 26 72 65 61 64 65 72 73  Step(v, &readers
342d7 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [i]);.      if( 
342d8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
342d9 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 20  goto err;..     
342da 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
342db 52 65 6f 72 64 65 72 28 26 72 65 61 64 65 72 73  Reorder(&readers
342dc 5b 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69 29  [i], nReaders-i)
342dd 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 65 72  ;.    }.  }.. er
342de 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  r:.  dataBufferD
342df 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29  estroy(&doclist)
342e0 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
342e1 73 74 72 6f 79 28 26 6d 65 72 67 65 64 29 3b 0a  stroy(&merged);.
342e2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
342e3 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 6f 70 74  /* Implement opt
342e4 69 6d 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e  imize() function
342e5 20 66 6f 72 20 46 54 53 33 2e 20 20 6f 70 74 69   for FTS3.  opti
342e6 6d 69 7a 65 28 74 29 20 6d 65 72 67 65 73 20 61  mize(t) merges a
342e7 6c 6c 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20 69  ll.** segments i
342e8 6e 20 74 68 65 20 66 74 73 20 69 6e 64 65 78 20  n the fts index 
342e9 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65  into a single se
342ea 67 6d 65 6e 74 2e 20 20 27 74 27 20 69 73 20 74  gment.  't' is t
342eb 68 65 20 6d 61 67 69 63 0a 2a 2a 20 74 61 62 6c  he magic.** tabl
342ec 65 2d 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 2e 0a  e-named column..
342ed 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
342ee 70 74 69 6d 69 7a 65 46 75 6e 63 28 73 71 6c 69  ptimizeFunc(sqli
342ef 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
342f0 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
342f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
342f2 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
342f3 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
342f4 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
342f5 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69  or *pCursor;.  i
342f6 66 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20  f( argc>1 ){.   
342f7 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
342f8 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  error(pContext, 
342f9 22 65 78 63 65 73 73 20 61 72 67 75 6d 65 6e 74  "excess argument
342fa 73 20 74 6f 20 6f 70 74 69 6d 69 7a 65 28 29 22  s to optimize()"
342fb 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ,-1);.  }else if
342fc 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
342fd 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53  type(argv[0])!=S
342fe 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20  QLITE_BLOB ||.  
342ff 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
34300 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
34301 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70  gv[0])!=sizeof(p
34302 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73  Cursor) ){.    s
34303 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
34304 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 69  ror(pContext, "i
34305 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72 67  llegal first arg
34306 75 6d 65 6e 74 20 74 6f 20 6f 70 74 69 6d 69 7a  ument to optimiz
34307 65 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  e",-1);.  }else{
34308 0a 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  .    fulltext_vt
34309 61 62 20 2a 76 3b 0a 20 20 20 20 69 6e 74 20 69  ab *v;.    int i
3430a 2c 20 72 63 2c 20 69 4d 61 78 4c 65 76 65 6c 3b  , rc, iMaxLevel;
3430b 0a 20 20 20 20 4f 70 74 4c 65 61 76 65 73 52 65  .    OptLeavesRe
3430c 61 64 65 72 20 2a 72 65 61 64 65 72 73 3b 0a 20  ader *readers;. 
3430d 20 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b     int nReaders;
3430e 0a 20 20 20 20 4c 65 61 66 57 72 69 74 65 72 20  .    LeafWriter 
3430f 77 72 69 74 65 72 3b 0a 20 20 20 20 73 71 6c 69  writer;.    sqli
34310 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 0a 20 20  te3_stmt *s;..  
34311 20 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f    memcpy(&pCurso
34312 72 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r, sqlite3_value
34313 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20  _blob(argv[0]), 
34314 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29  sizeof(pCursor))
34315 3b 0a 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72  ;.    v = cursor
34316 5f 76 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a  _vtab(pCursor);.
34317 0a 20 20 20 20 2f 2a 20 46 6c 75 73 68 20 61 6e  .    /* Flush an
34318 79 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  y buffered updat
34319 65 73 20 62 65 66 6f 72 65 20 6f 70 74 69 6d 69  es before optimi
3431a 7a 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20  zing. */.    rc 
3431b 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  = flushPendingTe
3431c 72 6d 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20  rms(v);.    if( 
3431d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3431e 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 72  goto err;..    r
3431f 63 20 3d 20 73 65 67 64 69 72 5f 63 6f 75 6e 74  c = segdir_count
34320 28 76 2c 20 26 6e 52 65 61 64 65 72 73 2c 20 26  (v, &nReaders, &
34321 69 4d 61 78 4c 65 76 65 6c 29 3b 0a 20 20 20 20  iMaxLevel);.    
34322 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
34323 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20  K ) goto err;.  
34324 20 20 69 66 28 20 6e 52 65 61 64 65 72 73 3d 3d    if( nReaders==
34325 30 20 7c 7c 20 6e 52 65 61 64 65 72 73 3d 3d 31  0 || nReaders==1
34326 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
34327 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
34328 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65 78 20 61  ontext, "Index a
34329 6c 72 65 61 64 79 20 6f 70 74 69 6d 61 6c 22 2c  lready optimal",
3432a 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20   -1,.           
3432b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
3432c 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
3432d 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
3432e 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   }..    rc = sql
3432f 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
34330 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  , SEGDIR_SELECT_
34331 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ALL_STMT, &s);. 
34332 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
34333 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
34334 0a 0a 20 20 20 20 72 65 61 64 65 72 73 20 3d 20  ..    readers = 
34335 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
34336 52 65 61 64 65 72 73 2a 73 69 7a 65 6f 66 28 72  Readers*sizeof(r
34337 65 61 64 65 72 73 5b 30 5d 29 29 3b 0a 20 20 20  eaders[0]));.   
34338 20 69 66 28 20 72 65 61 64 65 72 73 3d 3d 4e 55   if( readers==NU
34339 4c 4c 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a  LL ) goto err;..
3433a 20 20 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74      /* Note that
3433b 20 74 68 65 72 65 20 77 69 6c 6c 20 61 6c 72 65   there will alre
3433c 61 64 79 20 62 65 20 61 20 73 65 67 6d 65 6e 74  ady be a segment
3433d 20 61 74 20 74 68 69 73 20 70 6f 73 69 74 69 6f   at this positio
3433e 6e 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 77  n.    ** until w
3433f 65 20 63 61 6c 6c 20 73 65 67 64 69 72 5f 64 65  e call segdir_de
34340 6c 65 74 65 28 29 20 6f 6e 20 69 4d 61 78 4c 65  lete() on iMaxLe
34341 76 65 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  vel..    */.    
34342 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 69  leafWriterInit(i
34343 4d 61 78 4c 65 76 65 6c 2c 20 30 2c 20 26 77 72  MaxLevel, 0, &wr
34344 69 74 65 72 29 3b 0a 0a 20 20 20 20 69 20 3d 20  iter);..    i = 
34345 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 72  0;.    while( (r
34346 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
34347 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (s))==SQLITE_ROW
34348 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
34349 5f 69 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20  _int64 iStart = 
3434a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
3434b 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20  nt64(s, 0);.    
3434c 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
3434d 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  End = sqlite3_co
3434e 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29  lumn_int64(s, 1)
3434f 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
34350 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20  ar *pRootData = 
34351 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
34352 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 20  lob(s, 2);.     
34353 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d   int nRootData =
34354 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
34355 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20  bytes(s, 2);..  
34356 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 52      assert( i<nR
34357 65 61 64 65 72 73 20 29 3b 0a 20 20 20 20 20 20  eaders );.      
34358 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rc = leavesReade
34359 72 49 6e 69 74 28 76 2c 20 2d 31 2c 20 69 53 74  rInit(v, -1, iSt
3435a 61 72 74 2c 20 69 45 6e 64 2c 20 70 52 6f 6f 74  art, iEnd, pRoot
3435b 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c  Data, nRootData,
3435c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3435d 20 20 20 20 20 20 20 20 20 20 20 20 20 26 72 65               &re
3435e 61 64 65 72 73 5b 69 5d 2e 72 65 61 64 65 72 29  aders[i].reader)
3435f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
34360 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61  SQLITE_OK ) brea
34361 6b 3b 0a 0a 20 20 20 20 20 20 72 65 61 64 65 72  k;..      reader
34362 73 5b 69 5d 2e 73 65 67 6d 65 6e 74 20 3d 20 69  s[i].segment = i
34363 3b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20  ;.      i++;.   
34364 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65   }..    /* If we
34365 20 6d 61 6e 61 67 65 64 20 74 6f 20 73 75 63 63   managed to succ
34366 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 74 68  essfully read th
34367 65 6d 20 61 6c 6c 2c 20 6f 70 74 69 6d 69 7a 65  em all, optimize
34368 20 74 68 65 6d 2e 20 2a 2f 0a 20 20 20 20 69 66   them. */.    if
34369 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
3436a 45 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  E ){.      asser
3436b 74 28 20 69 3d 3d 6e 52 65 61 64 65 72 73 20 29  t( i==nReaders )
3436c 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6f 70 74  ;.      rc = opt
3436d 69 6d 69 7a 65 49 6e 74 65 72 6e 61 6c 28 76 2c  imizeInternal(v,
3436e 20 72 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65   readers, nReade
3436f 72 73 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20  rs, &writer);.  
34370 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28 20    }..    while( 
34371 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 20  i-- > 0 ){.     
34372 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73   leavesReaderDes
34373 74 72 6f 79 28 26 72 65 61 64 65 72 73 5b 69 5d  troy(&readers[i]
34374 2e 72 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a  .reader);.    }.
34375 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
34376 28 72 65 61 64 65 72 73 29 3b 0a 0a 20 20 20 20  (readers);..    
34377 2f 2a 20 49 66 20 77 65 27 76 65 20 73 75 63 63  /* If we've succ
34378 65 73 73 66 75 6c 6c 79 20 67 6f 74 74 65 6e 20  essfully gotten 
34379 74 6f 20 68 65 72 65 2c 20 64 65 6c 65 74 65 20  to here, delete 
3437a 74 68 65 20 6f 6c 64 20 73 65 67 6d 65 6e 74 73  the old segments
3437b 0a 20 20 20 20 2a 2a 20 61 6e 64 20 66 6c 75 73  .    ** and flus
3437c 68 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 73  h the interior s
3437d 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 65 20  tructure of the 
3437e 6e 65 77 20 73 65 67 6d 65 6e 74 2e 0a 20 20 20  new segment..   
3437f 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
34380 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
34381 20 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 3d     for( i=0; i<=
34382 69 4d 61 78 4c 65 76 65 6c 3b 20 69 2b 2b 20 29  iMaxLevel; i++ )
34383 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
34384 65 67 64 69 72 5f 64 65 6c 65 74 65 28 76 2c 20  egdir_delete(v, 
34385 69 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  i);.        if( 
34386 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
34387 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a  break;.      }..
34388 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
34389 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 6c  LITE_OK ) rc = l
3438a 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a  eafWriterFinaliz
3438b 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b 0a 20  e(v, &writer);. 
3438c 20 20 20 7d 0a 0a 20 20 20 20 6c 65 61 66 57 72     }..    leafWr
3438d 69 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69  iterDestroy(&wri
3438e 74 65 72 29 3b 0a 0a 20 20 20 20 69 66 28 20 72  ter);..    if( r
3438f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
34390 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 73 71  oto err;..    sq
34391 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
34392 74 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64  t(pContext, "Ind
34393 65 78 20 6f 70 74 69 6d 69 7a 65 64 22 2c 20 2d  ex optimized", -
34394 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
34395 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a  );.    return;..
34396 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73      /* TODO(shes
34397 73 29 3a 20 45 72 72 6f 72 2d 68 61 6e 64 6c 69  s): Error-handli
34398 6e 67 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69  ng needs to be i
34399 6d 70 72 6f 76 65 64 20 61 6c 6f 6e 67 20 74 68  mproved along th
3439a 65 0a 20 20 20 20 2a 2a 20 6c 69 6e 65 73 20 6f  e.    ** lines o
3439b 66 20 74 68 65 20 64 75 6d 70 5f 20 66 75 6e 63  f the dump_ func
3439c 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 65  tions..    */. e
3439d 72 72 3a 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  rr:.    {.      
3439e 63 68 61 72 20 62 75 66 5b 35 31 32 5d 3b 0a 20  char buf[512];. 
3439f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
343a0 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
343a1 29 2c 20 62 75 66 2c 20 22 45 72 72 6f 72 20 69  ), buf, "Error i
343a2 6e 20 6f 70 74 69 6d 69 7a 65 3a 20 25 73 22 2c  n optimize: %s",
343a3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
343a4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
343a5 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 5f 63  errmsg(sqlite3_c
343a6 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
343a7 28 70 43 6f 6e 74 65 78 74 29 29 29 3b 0a 20 20  (pContext)));.  
343a8 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
343a9 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78  lt_error(pContex
343aa 74 2c 20 62 75 66 2c 20 2d 31 29 3b 0a 20 20 20  t, buf, -1);.   
343ab 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66   }.  }.}..#ifdef
343ac 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20   SQLITE_TEST./* 
343ad 47 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f  Generate an erro
343ae 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c  r of the form "<
343af 70 72 65 66 69 78 3e 3a 20 3c 6d 73 67 3e 22 2e  prefix>: <msg>".
343b0 20 20 49 66 20 6d 73 67 20 69 73 20 4e 55 4c 4c    If msg is NULL
343b1 2c 0a 2a 2a 20 70 75 6c 6c 20 74 68 65 20 65 72  ,.** pull the er
343b2 72 6f 72 20 66 72 6f 6d 20 74 68 65 20 63 6f 6e  ror from the con
343b3 74 65 78 74 27 73 20 64 62 20 68 61 6e 64 6c 65  text's db handle
343b4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
343b5 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 73   generateError(s
343b6 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
343b7 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20  pContext,.      
343b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
343b9 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
343ba 70 72 65 66 69 78 2c 20 63 6f 6e 73 74 20 63 68  prefix, const ch
343bb 61 72 20 2a 6d 73 67 29 7b 0a 20 20 63 68 61 72  ar *msg){.  char
343bc 20 62 75 66 5b 35 31 32 5d 3b 0a 20 20 69 66 28   buf[512];.  if(
343bd 20 6d 73 67 3d 3d 4e 55 4c 4c 20 29 20 6d 73 67   msg==NULL ) msg
343be 20 3d 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73   = sqlite3_errms
343bf 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  g(sqlite3_contex
343c0 74 5f 64 62 5f 68 61 6e 64 6c 65 28 70 43 6f 6e  t_db_handle(pCon
343c1 74 65 78 74 29 29 3b 0a 20 20 73 71 6c 69 74 65  text));.  sqlite
343c2 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
343c3 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 73  f(buf), buf, "%s
343c4 3a 20 25 73 22 2c 20 70 72 65 66 69 78 2c 20 6d  : %s", prefix, m
343c5 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  sg);.  sqlite3_r
343c6 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e  esult_error(pCon
343c7 74 65 78 74 2c 20 62 75 66 2c 20 2d 31 29 3b 0a  text, buf, -1);.
343c8 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e  }../* Helper fun
343c9 63 74 69 6f 6e 20 74 6f 20 63 6f 6c 6c 65 63 74  ction to collect
343ca 20 74 68 65 20 73 65 74 20 6f 66 20 74 65 72 6d   the set of term
343cb 73 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  s in the segment
343cc 20 69 6e 74 6f 0a 2a 2a 20 70 54 65 72 6d 73 2e   into.** pTerms.
343cd 20 20 54 68 65 20 73 65 67 6d 65 6e 74 20 69 73    The segment is
343ce 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 20   defined by the 
343cf 6c 65 61 66 20 6e 6f 64 65 73 20 62 65 74 77 65  leaf nodes betwe
343d0 65 6e 0a 2a 2a 20 69 53 74 61 72 74 42 6c 6f 63  en.** iStartBloc
343d1 6b 69 64 20 61 6e 64 20 69 45 6e 64 42 6c 6f 63  kid and iEndBloc
343d2 6b 69 64 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20  kid, inclusive, 
343d3 6f 72 20 62 79 20 74 68 65 20 63 6f 6e 74 65 6e  or by the conten
343d4 74 73 20 6f 66 0a 2a 2a 20 70 52 6f 6f 74 44 61  ts of.** pRootDa
343d5 74 61 20 69 66 20 69 53 74 61 72 74 42 6c 6f 63  ta if iStartBloc
343d6 6b 69 64 20 69 73 20 30 20 28 69 6e 20 77 68 69  kid is 0 (in whi
343d7 63 68 20 63 61 73 65 20 74 68 65 20 65 6e 74 69  ch case the enti
343d8 72 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 66 69  re segment.** fi
343d9 74 20 69 6e 20 61 20 6c 65 61 66 29 2e 0a 2a 2f  t in a leaf)..*/
343da 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c  .static int coll
343db 65 63 74 53 65 67 6d 65 6e 74 54 65 72 6d 73 28  ectSegmentTerms(
343dc 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
343dd 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  , sqlite3_stmt *
343de 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
343df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
343e0 20 20 66 74 73 33 48 61 73 68 20 2a 70 54 65 72    fts3Hash *pTer
343e1 6d 73 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  ms){.  const sql
343e2 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74  ite_int64 iStart
343e3 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65  Blockid = sqlite
343e4 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73  3_column_int64(s
343e5 2c 20 30 29 3b 0a 20 20 63 6f 6e 73 74 20 73 71  , 0);.  const sq
343e6 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42  lite_int64 iEndB
343e7 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33  lockid = sqlite3
343e8 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c  _column_int64(s,
343e9 20 31 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61   1);.  const cha
343ea 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20 73  r *pRootData = s
343eb 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
343ec 6f 62 28 73 2c 20 32 29 3b 0a 20 20 63 6f 6e 73  ob(s, 2);.  cons
343ed 74 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20  t int nRootData 
343ee 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
343ef 5f 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 20 20  _bytes(s, 2);.  
343f0 4c 65 61 76 65 73 52 65 61 64 65 72 20 72 65 61  LeavesReader rea
343f1 64 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  der;.  int rc = 
343f2 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69 74  leavesReaderInit
343f3 28 76 2c 20 30 2c 20 69 53 74 61 72 74 42 6c 6f  (v, 0, iStartBlo
343f4 63 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69  ckid, iEndBlocki
343f5 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
343f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
343f7 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44  RootData, nRootD
343f8 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 20  ata, &reader);. 
343f9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
343fa 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
343fb 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
343fc 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76  LITE_OK && !leav
343fd 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 26 72  esReaderAtEnd(&r
343fe 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 63 6f  eader) ){.    co
343ff 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20  nst char *pTerm 
34400 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  = leavesReaderTe
34401 72 6d 28 26 72 65 61 64 65 72 29 3b 0a 20 20 20  rm(&reader);.   
34402 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d   const int nTerm
34403 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54   = leavesReaderT
34404 65 72 6d 42 79 74 65 73 28 26 72 65 61 64 65 72  ermBytes(&reader
34405 29 3b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64  );.    void *old
34406 56 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 46  Value = sqlite3F
34407 74 73 33 48 61 73 68 46 69 6e 64 28 70 54 65 72  ts3HashFind(pTer
34408 6d 73 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  ms, pTerm, nTerm
34409 29 3b 0a 20 20 20 20 76 6f 69 64 20 2a 6e 65 77  );.    void *new
3440a 56 61 6c 75 65 20 3d 20 28 76 6f 69 64 20 2a 29  Value = (void *)
3440b 28 28 63 68 61 72 20 2a 29 6f 6c 64 56 61 6c 75  ((char *)oldValu
3440c 65 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 72  e+1);..    /* Fr
3440d 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 62  om the comment b
3440e 65 66 6f 72 65 20 73 71 6c 69 74 65 33 46 74 73  efore sqlite3Fts
3440f 33 48 61 73 68 49 6e 73 65 72 74 20 69 6e 20 66  3HashInsert in f
34410 74 73 33 5f 68 61 73 68 2e 63 2c 0a 20 20 20 20  ts3_hash.c,.    
34411 2a 2a 20 74 68 65 20 64 61 74 61 20 76 61 6c 75  ** the data valu
34412 65 20 70 61 73 73 65 64 20 69 73 20 72 65 74 75  e passed is retu
34413 72 6e 65 64 20 69 6e 20 63 61 73 65 20 6f 66 20  rned in case of 
34414 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 2e 0a  malloc failure..
34415 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e      */.    if( n
34416 65 77 56 61 6c 75 65 3d 3d 73 71 6c 69 74 65 33  ewValue==sqlite3
34417 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70  Fts3HashInsert(p
34418 54 65 72 6d 73 2c 20 70 54 65 72 6d 2c 20 6e 54  Terms, pTerm, nT
34419 65 72 6d 2c 20 6e 65 77 56 61 6c 75 65 29 20 29  erm, newValue) )
3441a 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
3441b 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
3441c 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
3441d 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74 65   leavesReaderSte
3441e 70 28 76 2c 20 26 72 65 61 64 65 72 29 3b 0a 20  p(v, &reader);. 
3441f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76     }.  }..  leav
34420 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  esReaderDestroy(
34421 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75  &reader);.  retu
34422 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c  rn rc;.}../* Hel
34423 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  per function to 
34424 62 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74  build the result
34425 20 73 74 72 69 6e 67 20 66 6f 72 20 64 75 6d 70   string for dump
34426 5f 74 65 72 6d 73 28 29 2e 20 2a 2f 0a 73 74 61  _terms(). */.sta
34427 74 69 63 20 69 6e 74 20 67 65 6e 65 72 61 74 65  tic int generate
34428 54 65 72 6d 73 52 65 73 75 6c 74 28 73 71 6c 69  TermsResult(sqli
34429 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
3442a 6e 74 65 78 74 2c 20 66 74 73 33 48 61 73 68 20  ntext, fts3Hash 
3442b 2a 70 54 65 72 6d 73 29 7b 0a 20 20 69 6e 74 20  *pTerms){.  int 
3442c 69 54 65 72 6d 2c 20 6e 54 65 72 6d 73 2c 20 6e  iTerm, nTerms, n
3442d 52 65 73 75 6c 74 42 79 74 65 73 2c 20 69 42 79  ResultBytes, iBy
3442e 74 65 3b 0a 20 20 63 68 61 72 20 2a 72 65 73 75  te;.  char *resu
3442f 6c 74 3b 0a 20 20 54 65 72 6d 44 61 74 61 20 2a  lt;.  TermData *
34430 70 44 61 74 61 3b 0a 20 20 66 74 73 33 48 61 73  pData;.  fts3Has
34431 68 45 6c 65 6d 20 2a 65 3b 0a 0a 20 20 2f 2a 20  hElem *e;..  /* 
34432 49 74 65 72 61 74 65 20 70 54 65 72 6d 73 20 74  Iterate pTerms t
34433 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20 61 72  o generate an ar
34434 72 61 79 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ray of terms in 
34435 70 44 61 74 61 20 66 6f 72 0a 20 20 2a 2a 20 73  pData for.  ** s
34436 6f 72 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6e  orting..  */.  n
34437 54 65 72 6d 73 20 3d 20 66 74 73 33 48 61 73 68  Terms = fts3Hash
34438 43 6f 75 6e 74 28 70 54 65 72 6d 73 29 3b 0a 20  Count(pTerms);. 
34439 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 73 3e   assert( nTerms>
3443a 30 20 29 3b 0a 20 20 70 44 61 74 61 20 3d 20 73  0 );.  pData = s
3443b 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 54  qlite3_malloc(nT
3443c 65 72 6d 73 2a 73 69 7a 65 6f 66 28 54 65 72 6d  erms*sizeof(Term
3443d 44 61 74 61 29 29 3b 0a 20 20 69 66 28 20 70 44  Data));.  if( pD
3443e 61 74 61 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ata==NULL ) retu
3443f 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
34440 0a 0a 20 20 6e 52 65 73 75 6c 74 42 79 74 65 73  ..  nResultBytes
34441 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 54 65 72   = 0;.  for(iTer
34442 6d 20 3d 20 30 2c 20 65 20 3d 20 66 74 73 33 48  m = 0, e = fts3H
34443 61 73 68 46 69 72 73 74 28 70 54 65 72 6d 73 29  ashFirst(pTerms)
34444 3b 20 65 3b 20 69 54 65 72 6d 2b 2b 2c 20 65 20  ; e; iTerm++, e 
34445 3d 20 66 74 73 33 48 61 73 68 4e 65 78 74 28 65  = fts3HashNext(e
34446 29 29 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 42  )){.    nResultB
34447 79 74 65 73 20 2b 3d 20 66 74 73 33 48 61 73 68  ytes += fts3Hash
34448 4b 65 79 73 69 7a 65 28 65 29 2b 31 3b 20 20 20  Keysize(e)+1;   
34449 2f 2a 20 54 65 72 6d 20 70 6c 75 73 20 74 72 61  /* Term plus tra
3444a 69 6c 69 6e 67 20 73 70 61 63 65 20 2a 2f 0a 20  iling space */. 
3444b 20 20 20 61 73 73 65 72 74 28 20 69 54 65 72 6d     assert( iTerm
3444c 3c 6e 54 65 72 6d 73 20 29 3b 0a 20 20 20 20 70  <nTerms );.    p
3444d 44 61 74 61 5b 69 54 65 72 6d 5d 2e 70 54 65 72  Data[iTerm].pTer
3444e 6d 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 28  m = fts3HashKey(
3444f 65 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 54  e);.    pData[iT
34450 65 72 6d 5d 2e 6e 54 65 72 6d 20 3d 20 66 74 73  erm].nTerm = fts
34451 33 48 61 73 68 4b 65 79 73 69 7a 65 28 65 29 3b  3HashKeysize(e);
34452 0a 20 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d  .    pData[iTerm
34453 5d 2e 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 66  ].pCollector = f
34454 74 73 33 48 61 73 68 44 61 74 61 28 65 29 3b 20  ts3HashData(e); 
34455 20 2f 2a 20 75 6e 75 73 65 64 20 2a 2f 0a 20 20   /* unused */.  
34456 7d 0a 20 20 61 73 73 65 72 74 28 20 69 54 65 72  }.  assert( iTer
34457 6d 3d 3d 6e 54 65 72 6d 73 20 29 3b 0a 0a 20 20  m==nTerms );..  
34458 61 73 73 65 72 74 28 20 6e 52 65 73 75 6c 74 42  assert( nResultB
34459 79 74 65 73 3e 30 20 29 3b 20 20 20 2f 2a 20 6e  ytes>0 );   /* n
3445a 54 65 72 6d 73 3e 30 2c 20 6e 52 65 73 75 6c 74  Terms>0, nResult
3445b 73 42 79 74 65 73 20 6d 75 73 74 20 62 65 2c 20  sBytes must be, 
3445c 74 6f 6f 2e 20 2a 2f 0a 20 20 72 65 73 75 6c 74  too. */.  result
3445d 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
3445e 63 28 6e 52 65 73 75 6c 74 42 79 74 65 73 29 3b  c(nResultBytes);
3445f 0a 20 20 69 66 28 20 72 65 73 75 6c 74 3d 3d 4e  .  if( result==N
34460 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ULL ){.    sqlit
34461 65 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a  e3_free(pData);.
34462 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
34463 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
34464 69 66 28 20 6e 54 65 72 6d 73 3e 31 20 29 20 71  if( nTerms>1 ) q
34465 73 6f 72 74 28 70 44 61 74 61 2c 20 6e 54 65 72  sort(pData, nTer
34466 6d 73 2c 20 73 69 7a 65 6f 66 28 2a 70 44 61 74  ms, sizeof(*pDat
34467 61 29 2c 20 74 65 72 6d 44 61 74 61 43 6d 70 29  a), termDataCmp)
34468 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  ;..  /* Read the
34469 20 74 65 72 6d 73 20 69 6e 20 6f 72 64 65 72 20   terms in order 
3446a 74 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65 73  to build the res
3446b 75 6c 74 2e 20 2a 2f 0a 20 20 69 42 79 74 65 20  ult. */.  iByte 
3446c 3d 20 30 3b 0a 20 20 66 6f 72 28 69 54 65 72 6d  = 0;.  for(iTerm
3446d 3d 30 3b 20 69 54 65 72 6d 3c 6e 54 65 72 6d 73  =0; iTerm<nTerms
3446e 3b 20 2b 2b 69 54 65 72 6d 29 7b 0a 20 20 20 20  ; ++iTerm){.    
3446f 6d 65 6d 63 70 79 28 72 65 73 75 6c 74 2b 69 42  memcpy(result+iB
34470 79 74 65 2c 20 70 44 61 74 61 5b 69 54 65 72 6d  yte, pData[iTerm
34471 5d 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b 69  ].pTerm, pData[i
34472 54 65 72 6d 5d 2e 6e 54 65 72 6d 29 3b 0a 20 20  Term].nTerm);.  
34473 20 20 69 42 79 74 65 20 2b 3d 20 70 44 61 74 61    iByte += pData
34474 5b 69 54 65 72 6d 5d 2e 6e 54 65 72 6d 3b 0a 20  [iTerm].nTerm;. 
34475 20 20 20 72 65 73 75 6c 74 5b 69 42 79 74 65 2b     result[iByte+
34476 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 7d 0a 20 20  +] = ' ';.  }.  
34477 61 73 73 65 72 74 28 20 69 42 79 74 65 3d 3d 6e  assert( iByte==n
34478 52 65 73 75 6c 74 42 79 74 65 73 20 29 3b 0a 20  ResultBytes );. 
34479 20 61 73 73 65 72 74 28 20 72 65 73 75 6c 74 5b   assert( result[
3447a 6e 52 65 73 75 6c 74 42 79 74 65 73 2d 31 5d 3d  nResultBytes-1]=
3447b 3d 27 20 27 20 29 3b 0a 20 20 72 65 73 75 6c 74  =' ' );.  result
3447c 5b 6e 52 65 73 75 6c 74 42 79 74 65 73 2d 31 5d  [nResultBytes-1]
3447d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 2f 2a 20 50   = '\0';..  /* P
3447e 61 73 73 65 73 20 61 77 61 79 20 6f 77 6e 65 72  asses away owner
3447f 73 68 69 70 20 6f 66 20 72 65 73 75 6c 74 2e 20  ship of result. 
34480 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  */.  sqlite3_res
34481 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
34482 74 2c 20 72 65 73 75 6c 74 2c 20 6e 52 65 73 75  t, result, nResu
34483 6c 74 42 79 74 65 73 2d 31 2c 20 73 71 6c 69 74  ltBytes-1, sqlit
34484 65 33 5f 66 72 65 65 29 3b 0a 20 20 73 71 6c 69  e3_free);.  sqli
34485 74 65 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b  te3_free(pData);
34486 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
34487 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65  _OK;.}../* Imple
34488 6d 65 6e 74 73 20 64 75 6d 70 5f 74 65 72 6d 73  ments dump_terms
34489 28 29 20 66 6f 72 20 75 73 65 20 69 6e 20 69 6e  () for use in in
3448a 73 70 65 63 74 69 6e 67 20 74 68 65 20 66 74 73  specting the fts
3448b 33 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20  3 index from.** 
3448c 74 65 73 74 73 2e 20 20 54 45 58 54 20 72 65 73  tests.  TEXT res
3448d 75 6c 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ult containing t
3448e 68 65 20 6f 72 64 65 72 65 64 20 6c 69 73 74 20  he ordered list 
3448f 6f 66 20 74 65 72 6d 73 20 6a 6f 69 6e 65 64 20  of terms joined 
34490 62 79 0a 2a 2a 20 73 70 61 63 65 73 2e 20 20 64  by.** spaces.  d
34491 75 6d 70 5f 74 65 72 6d 73 28 74 2c 20 6c 65 76  ump_terms(t, lev
34492 65 6c 2c 20 69 64 78 29 20 64 75 6d 70 73 20 74  el, idx) dumps t
34493 68 65 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65  he terms for the
34494 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 70 65 63   segment.** spec
34495 69 66 69 65 64 20 62 79 20 6c 65 76 65 6c 2c 20  ified by level, 
34496 69 64 78 20 28 69 6e 20 25 5f 73 65 67 64 69 72  idx (in %_segdir
34497 29 2c 20 77 68 69 6c 65 20 64 75 6d 70 5f 74 65  ), while dump_te
34498 72 6d 73 28 74 29 20 64 75 6d 70 73 0a 2a 2a 20  rms(t) dumps.** 
34499 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 74 68 65  all terms in the
3449a 20 69 6e 64 65 78 2e 20 20 49 6e 20 62 6f 74 68   index.  In both
3449b 20 63 61 73 65 73 20 74 20 69 73 20 74 68 65 20   cases t is the 
3449c 66 74 73 20 74 61 62 6c 65 27 73 20 6d 61 67 69  fts table's magi
3449d 63 0a 2a 2a 20 74 61 62 6c 65 2d 6e 61 6d 65 64  c.** table-named
3449e 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74   column..*/.stat
3449f 69 63 20 76 6f 69 64 20 64 75 6d 70 54 65 72 6d  ic void dumpTerm
344a0 73 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  sFunc(.  sqlite3
344a1 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  _context *pConte
344a2 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  xt,.  int argc, 
344a3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
344a4 61 72 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65  argv.){.  fullte
344a5 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  xt_cursor *pCurs
344a6 6f 72 3b 0a 20 20 69 66 28 20 61 72 67 63 21 3d  or;.  if( argc!=
344a7 33 20 26 26 20 61 72 67 63 21 3d 31 20 29 7b 0a  3 && argc!=1 ){.
344a8 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
344a9 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
344aa 70 5f 74 65 72 6d 73 22 2c 20 22 69 6e 63 6f 72  p_terms", "incor
344ab 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 22 29  rect arguments")
344ac 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
344ad 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
344ae 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54  (argv[0])!=SQLIT
344af 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20  E_BLOB ||.      
344b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
344b1 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
344b2 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73  ])!=sizeof(pCurs
344b3 6f 72 29 20 29 7b 0a 20 20 20 20 67 65 6e 65 72  or) ){.    gener
344b4 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78  ateError(pContex
344b5 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c  t, "dump_terms",
344b6 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20   "illegal first 
344b7 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65  argument");.  }e
344b8 6c 73 65 7b 0a 20 20 20 20 66 75 6c 6c 74 65 78  lse{.    fulltex
344b9 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20 20 66  t_vtab *v;.    f
344ba 74 73 33 48 61 73 68 20 74 65 72 6d 73 3b 0a 20  ts3Hash terms;. 
344bb 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
344bc 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69  *s = NULL;.    i
344bd 6e 74 20 72 63 3b 0a 0a 20 20 20 20 6d 65 6d 63  nt rc;..    memc
344be 70 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c  py(&pCursor, sql
344bf 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
344c0 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66  argv[0]), sizeof
344c1 28 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20  (pCursor));.    
344c2 76 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28  v = cursor_vtab(
344c3 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20 2f  pCursor);..    /
344c4 2a 20 49 66 20 70 61 73 73 65 64 20 6f 6e 6c 79  * If passed only
344c5 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f 6c 75   the cursor colu
344c6 6d 6e 2c 20 67 65 74 20 61 6c 6c 20 73 65 67 6d  mn, get all segm
344c7 65 6e 74 73 2e 20 20 4f 74 68 65 72 77 69 73 65  ents.  Otherwise
344c8 0a 20 20 20 20 2a 2a 20 67 65 74 20 74 68 65 20  .    ** get the 
344c9 73 65 67 6d 65 6e 74 20 64 65 73 63 72 69 62 65  segment describe
344ca 64 20 62 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d by the followi
344cb 6e 67 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  ng two arguments
344cc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
344cd 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20   argc==1 ){.    
344ce 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73    rc = sql_get_s
344cf 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44  tatement(v, SEGD
344d0 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54  IR_SELECT_ALL_ST
344d1 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 7d 65 6c  MT, &s);.    }el
344d2 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
344d3 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
344d4 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
344d5 54 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 20  T_SEGMENT_STMT, 
344d6 26 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  &s);.      if( r
344d7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
344d8 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
344d9 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c  ite3_bind_int(s,
344da 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   1, sqlite3_valu
344db 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 29 3b  e_int(argv[1]));
344dc 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
344dd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
344de 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
344df 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c  ite3_bind_int(s,
344e0 20 32 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   2, sqlite3_valu
344e1 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 29 3b  e_int(argv[2]));
344e2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
344e3 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
344e4 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
344e5 29 7b 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74  ){.      generat
344e6 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
344e7 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 4e   "dump_terms", N
344e8 55 4c 4c 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ULL);.      retu
344e9 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rn;.    }..    /
344ea 2a 20 43 6f 6c 6c 65 63 74 20 74 68 65 20 74 65  * Collect the te
344eb 72 6d 73 20 66 6f 72 20 65 61 63 68 20 73 65 67  rms for each seg
344ec 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 73 71 6c  ment. */.    sql
344ed 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74  ite3Fts3HashInit
344ee 28 26 74 65 72 6d 73 2c 20 46 54 53 33 5f 48 41  (&terms, FTS3_HA
344ef 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20  SH_STRING, 1);. 
344f0 20 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20     while( (rc = 
344f1 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 29  sqlite3_step(s))
344f2 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
344f3 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6c 6c 65        rc = colle
344f4 63 74 53 65 67 6d 65 6e 74 54 65 72 6d 73 28 76  ctSegmentTerms(v
344f5 2c 20 73 2c 20 26 74 65 72 6d 73 29 3b 0a 20 20  , s, &terms);.  
344f6 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
344f7 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20  TE_OK ) break;. 
344f8 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
344f9 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  !=SQLITE_DONE ){
344fa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
344fb 65 73 65 74 28 73 29 3b 0a 20 20 20 20 20 20 67  eset(s);.      g
344fc 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
344fd 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72  ntext, "dump_ter
344fe 6d 73 22 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20  ms", NULL);.    
344ff 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e  }else{.      con
34500 73 74 20 69 6e 74 20 6e 54 65 72 6d 73 20 3d 20  st int nTerms = 
34501 66 74 73 33 48 61 73 68 43 6f 75 6e 74 28 26 74  fts3HashCount(&t
34502 65 72 6d 73 29 3b 0a 20 20 20 20 20 20 69 66 28  erms);.      if(
34503 20 6e 54 65 72 6d 73 3e 30 20 29 7b 0a 20 20 20   nTerms>0 ){.   
34504 20 20 20 20 20 72 63 20 3d 20 67 65 6e 65 72 61       rc = genera
34505 74 65 54 65 72 6d 73 52 65 73 75 6c 74 28 70 43  teTermsResult(pC
34506 6f 6e 74 65 78 74 2c 20 26 74 65 72 6d 73 29 3b  ontext, &terms);
34507 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
34508 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
34509 0a 20 20 20 20 20 20 20 20 20 20 67 65 6e 65 72  .          gener
3450a 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78  ateError(pContex
3450b 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c  t, "dump_terms",
3450c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22   "out of memory"
3450d 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
3450e 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
3450f 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
34510 4b 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  K );.        }. 
34511 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61       }else if( a
34512 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 20 20  rgc==3 ){.      
34513 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69    /* The specifi
34514 63 20 73 65 67 6d 65 6e 74 20 61 73 6b 65 64 20  c segment asked 
34515 66 6f 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  for could not be
34516 20 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20   found. */.     
34517 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
34518 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
34519 5f 74 65 72 6d 73 22 2c 20 22 73 65 67 6d 65 6e  _terms", "segmen
3451a 74 20 6e 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20  t not found");. 
3451b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3451c 20 20 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e      /* No segmen
3451d 74 73 20 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20  ts found. */.   
3451e 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65       /* TODO(she
3451f 73 73 29 3a 20 49 74 20 73 68 6f 75 6c 64 20 62  ss): It should b
34520 65 20 69 6d 70 6f 73 73 69 62 6c 65 20 74 6f 20  e impossible to 
34521 72 65 61 63 68 20 74 68 69 73 2e 20 20 54 68 69  reach this.  Thi
34522 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73  s.        ** cas
34523 65 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65  e can only happe
34524 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 74  n for an empty t
34525 61 62 6c 65 2c 20 69 6e 20 77 68 69 63 68 20 63  able, in which c
34526 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53  ase.        ** S
34527 51 4c 69 74 65 20 68 61 73 20 6e 6f 20 72 6f 77  QLite has no row
34528 73 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66  s to call this f
34529 75 6e 63 74 69 6f 6e 20 6f 6e 2e 0a 20 20 20 20  unction on..    
3452a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73      */.        s
3452b 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
3452c 6c 6c 28 70 43 6f 6e 74 65 78 74 29 3b 0a 20 20  ll(pContext);.  
3452d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
3452e 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43  sqlite3Fts3HashC
3452f 6c 65 61 72 28 26 74 65 72 6d 73 29 3b 0a 20 20  lear(&terms);.  
34530 7d 0a 7d 0a 0a 2f 2a 20 45 78 70 61 6e 64 20 74  }.}../* Expand t
34531 68 65 20 44 4c 5f 44 45 46 41 55 4c 54 20 64 6f  he DL_DEFAULT do
34532 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61 20 69  clist in pData i
34533 6e 74 6f 20 61 20 74 65 78 74 20 72 65 73 75 6c  nto a text resul
34534 74 20 69 6e 0a 2a 2a 20 70 43 6f 6e 74 65 78 74  t in.** pContext
34535 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
34536 20 63 72 65 61 74 65 44 6f 63 6c 69 73 74 52 65   createDoclistRe
34537 73 75 6c 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  sult(sqlite3_con
34538 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a  text *pContext,.
34539 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3453a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3453b 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
3453c 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20  a, int nData){. 
3453d 20 44 61 74 61 42 75 66 66 65 72 20 64 75 6d 70   DataBuffer dump
3453e 3b 0a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52  ;.  DLReader dlR
3453f 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  eader;..  assert
34540 28 20 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26  ( pData!=NULL &&
34541 20 6e 44 61 74 61 3e 30 20 29 3b 0a 0a 20 20 64   nData>0 );..  d
34542 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 64  ataBufferInit(&d
34543 75 6d 70 2c 20 30 29 3b 0a 20 20 64 6c 72 49 6e  ump, 0);.  dlrIn
34544 69 74 28 26 64 6c 52 65 61 64 65 72 2c 20 44 4c  it(&dlReader, DL
34545 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2c  _DEFAULT, pData,
34546 20 6e 44 61 74 61 29 3b 0a 20 20 66 6f 72 28 20   nData);.  for( 
34547 3b 20 21 64 6c 72 41 74 45 6e 64 28 26 64 6c 52  ; !dlrAtEnd(&dlR
34548 65 61 64 65 72 29 3b 20 64 6c 72 53 74 65 70 28  eader); dlrStep(
34549 26 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20  &dlReader) ){.  
3454a 20 20 63 68 61 72 20 62 75 66 5b 32 35 36 5d 3b    char buf[256];
3454b 0a 20 20 20 20 50 4c 52 65 61 64 65 72 20 70 6c  .    PLReader pl
3454c 52 65 61 64 65 72 3b 0a 0a 20 20 20 20 70 6c 72  Reader;..    plr
3454d 49 6e 69 74 28 26 70 6c 52 65 61 64 65 72 2c 20  Init(&plReader, 
3454e 26 64 6c 52 65 61 64 65 72 29 3b 0a 20 20 20 20  &dlReader);.    
3454f 69 66 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d  if( DL_DEFAULT==
34550 44 4c 5f 44 4f 43 49 44 53 20 7c 7c 20 70 6c 72  DL_DOCIDS || plr
34551 41 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29  AtEnd(&plReader)
34552 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
34553 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
34554 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5b 25  f(buf), buf, "[%
34555 6c 6c 64 5d 20 22 2c 20 64 6c 72 44 6f 63 69 64  lld] ", dlrDocid
34556 28 26 64 6c 52 65 61 64 65 72 29 29 3b 0a 20 20  (&dlReader));.  
34557 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
34558 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c  pend(&dump, buf,
34559 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 20   strlen(buf));. 
3455a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3455b 69 6e 74 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c  int iColumn = pl
3455c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65  rColumn(&plReade
3455d 72 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  r);..      sqlit
3455e 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
3455f 6f 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5b  of(buf), buf, "[
34560 25 6c 6c 64 20 25 64 5b 22 2c 0a 20 20 20 20 20  %lld %d[",.     
34561 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34562 20 20 64 6c 72 44 6f 63 69 64 28 26 64 6c 52 65    dlrDocid(&dlRe
34563 61 64 65 72 29 2c 20 69 43 6f 6c 75 6d 6e 29 3b  ader), iColumn);
34564 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65  .      dataBuffe
34565 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 62  rAppend(&dump, b
34566 75 66 2c 20 73 74 72 6c 65 6e 28 62 75 66 29 29  uf, strlen(buf))
34567 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 20 3b 20  ;..      for( ; 
34568 21 70 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61  !plrAtEnd(&plRea
34569 64 65 72 29 3b 20 70 6c 72 53 74 65 70 28 26 70  der); plrStep(&p
3456a 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  lReader) ){.    
3456b 20 20 20 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d      if( plrColum
3456c 6e 28 26 70 6c 52 65 61 64 65 72 29 21 3d 69 43  n(&plReader)!=iC
3456d 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
3456e 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72     iColumn = plr
3456f 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72  Column(&plReader
34570 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
34571 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
34572 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66 2c 20  zeof(buf), buf, 
34573 22 5d 20 25 64 5b 22 2c 20 69 43 6f 6c 75 6d 6e  "] %d[", iColumn
34574 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
34575 65 72 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e  ert( dump.nData>
34576 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64  0 );.          d
34577 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20  ump.nData--;    
34578 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34579 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74 72   /* Overwrite tr
3457a 61 69 6c 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f  ailing space. */
3457b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
3457c 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75  t( dump.pData[du
3457d 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b  mp.nData]==' ');
3457e 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42  .          dataB
3457f 75 66 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d  ufferAppend(&dum
34580 70 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62  p, buf, strlen(b
34581 75 66 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  uf));.        }.
34582 20 20 20 20 20 20 20 20 69 66 28 20 44 4c 5f 44          if( DL_D
34583 45 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54  EFAULT==DL_POSIT
34584 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a  IONS_OFFSETS ){.
34585 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
34586 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
34587 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 64  f(buf), buf, "%d
34588 2c 25 64 2c 25 64 20 22 2c 0a 20 20 20 20 20 20  ,%d,%d ",.      
34589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3458a 20 20 20 20 20 70 6c 72 50 6f 73 69 74 69 6f 6e       plrPosition
3458b 28 26 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20  (&plReader),.   
3458c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3458d 20 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74          plrStart
3458e 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64 65 72  Offset(&plReader
3458f 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28  ), plrEndOffset(
34590 26 70 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20  &plReader));.   
34591 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 44       }else if( D
34592 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f  L_DEFAULT==DL_PO
34593 53 49 54 49 4f 4e 53 20 29 7b 0a 20 20 20 20 20  SITIONS ){.     
34594 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
34595 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
34596 29 2c 20 62 75 66 2c 20 22 25 64 20 22 2c 20 70  ), buf, "%d ", p
34597 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52 65  lrPosition(&plRe
34598 61 64 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20  ader));.        
34599 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
3459a 20 61 73 73 65 72 74 28 20 4e 55 4c 4c 3d 3d 22   assert( NULL=="
3459b 55 6e 68 61 6e 64 6c 65 64 20 44 4c 5f 44 45 46  Unhandled DL_DEF
3459c 41 55 4c 54 20 76 61 6c 75 65 22 29 3b 0a 20 20  AULT value");.  
3459d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3459e 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
3459f 28 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72  (&dump, buf, str
345a0 6c 65 6e 28 62 75 66 29 29 3b 0a 20 20 20 20 20  len(buf));.     
345a1 20 7d 0a 20 20 20 20 20 20 70 6c 72 44 65 73 74   }.      plrDest
345a2 72 6f 79 28 26 70 6c 52 65 61 64 65 72 29 3b 0a  roy(&plReader);.
345a3 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
345a4 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20  ump.nData>0 );. 
345a5 20 20 20 20 20 64 75 6d 70 2e 6e 44 61 74 61 2d       dump.nData-
345a6 2d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  -;              
345a7 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 77 72         /* Overwr
345a8 69 74 65 20 74 72 61 69 6c 69 6e 67 20 73 70 61  ite trailing spa
345a9 63 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  ce. */.      ass
345aa 65 72 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b  ert( dump.pData[
345ab 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27  dump.nData]==' '
345ac 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66  );.      dataBuf
345ad 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c  ferAppend(&dump,
345ae 20 22 5d 5d 20 22 2c 20 33 29 3b 0a 20 20 20 20   "]] ", 3);.    
345af 7d 0a 20 20 7d 0a 20 20 64 6c 72 44 65 73 74 72  }.  }.  dlrDestr
345b0 6f 79 28 26 64 6c 52 65 61 64 65 72 29 3b 0a 0a  oy(&dlReader);..
345b1 20 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e    assert( dump.n
345b2 44 61 74 61 3e 30 20 29 3b 0a 20 20 64 75 6d 70  Data>0 );.  dump
345b3 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20 20 20 20  .nData--;       
345b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
345b5 20 4f 76 65 72 77 72 69 74 65 20 74 72 61 69 6c   Overwrite trail
345b6 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20  ing space. */.  
345b7 61 73 73 65 72 74 28 20 64 75 6d 70 2e 70 44 61  assert( dump.pDa
345b8 74 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d  ta[dump.nData]==
345b9 27 20 27 29 3b 0a 20 20 64 75 6d 70 2e 70 44 61  ' ');.  dump.pDa
345ba 74 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 20 3d  ta[dump.nData] =
345bb 20 27 5c 30 27 3b 0a 20 20 61 73 73 65 72 74 28   '\0';.  assert(
345bc 20 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b   dump.nData>0 );
345bd 0a 0a 20 20 2f 2a 20 50 61 73 73 65 73 20 6f 77  ..  /* Passes ow
345be 6e 65 72 73 68 69 70 20 6f 66 20 64 75 6d 70 27  nership of dump'
345bf 73 20 62 75 66 66 65 72 20 74 6f 20 70 43 6f 6e  s buffer to pCon
345c0 74 65 78 74 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  text. */.  sqlit
345c1 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
345c2 43 6f 6e 74 65 78 74 2c 20 64 75 6d 70 2e 70 44  Context, dump.pD
345c3 61 74 61 2c 20 64 75 6d 70 2e 6e 44 61 74 61 2c  ata, dump.nData,
345c4 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
345c5 20 20 64 75 6d 70 2e 70 44 61 74 61 20 3d 20 4e    dump.pData = N
345c6 55 4c 4c 3b 0a 20 20 64 75 6d 70 2e 6e 44 61 74  ULL;.  dump.nDat
345c7 61 20 3d 20 64 75 6d 70 2e 6e 43 61 70 61 63 69  a = dump.nCapaci
345c8 74 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6d  ty = 0;.}../* Im
345c9 70 6c 65 6d 65 6e 74 73 20 64 75 6d 70 5f 64 6f  plements dump_do
345ca 63 6c 69 73 74 28 29 20 66 6f 72 20 75 73 65 20  clist() for use 
345cb 69 6e 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68  in inspecting th
345cc 65 20 66 74 73 33 20 69 6e 64 65 78 20 66 72 6f  e fts3 index fro
345cd 6d 0a 2a 2a 20 74 65 73 74 73 2e 20 20 54 45 58  m.** tests.  TEX
345ce 54 20 72 65 73 75 6c 74 20 63 6f 6e 74 61 69 6e  T result contain
345cf 69 6e 67 20 61 20 73 74 72 69 6e 67 20 72 65 70  ing a string rep
345d0 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
345d1 68 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f  he.** doclist fo
345d2 72 20 74 68 65 20 69 6e 64 69 63 61 74 65 64 20  r the indicated 
345d3 74 65 72 6d 2e 20 20 64 75 6d 70 5f 64 6f 63 6c  term.  dump_docl
345d4 69 73 74 28 74 2c 20 74 65 72 6d 2c 20 6c 65 76  ist(t, term, lev
345d5 65 6c 2c 20 69 64 78 29 0a 2a 2a 20 64 75 6d 70  el, idx).** dump
345d6 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66 6f  s the doclist fo
345d7 72 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65 20  r term from the 
345d8 73 65 67 6d 65 6e 74 20 73 70 65 63 69 66 69 65  segment specifie
345d9 64 20 62 79 20 6c 65 76 65 6c 2c 20 69 64 78 0a  d by level, idx.
345da 2a 2a 20 28 69 6e 20 25 5f 73 65 67 64 69 72 29  ** (in %_segdir)
345db 2c 20 77 68 69 6c 65 20 64 75 6d 70 5f 64 6f 63  , while dump_doc
345dc 6c 69 73 74 28 74 2c 20 74 65 72 6d 29 20 64 75  list(t, term) du
345dd 6d 70 73 20 74 68 65 20 6c 6f 67 69 63 61 6c 0a  mps the logical.
345de 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74  ** doclist for t
345df 68 65 20 74 65 72 6d 20 61 63 72 6f 73 73 20 61  he term across a
345e0 6c 6c 20 73 65 67 6d 65 6e 74 73 2e 20 20 54 68  ll segments.  Th
345e1 65 20 70 65 72 2d 73 65 67 6d 65 6e 74 20 64 6f  e per-segment do
345e2 63 6c 69 73 74 0a 2a 2a 20 63 61 6e 20 63 6f 6e  clist.** can con
345e3 74 61 69 6e 20 64 65 6c 65 74 69 6f 6e 73 2c 20  tain deletions, 
345e4 77 68 69 6c 65 20 74 68 65 20 66 75 6c 6c 2d 69  while the full-i
345e5 6e 64 65 78 20 64 6f 63 6c 69 73 74 20 77 69 6c  ndex doclist wil
345e6 6c 20 6e 6f 74 0a 2a 2a 20 28 64 65 6c 65 74 69  l not.** (deleti
345e7 6f 6e 73 20 61 72 65 20 6f 6d 69 74 74 65 64 29  ons are omitted)
345e8 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 75 6c 74 20 66  ..**.** Result f
345e9 6f 72 6d 61 74 73 20 64 69 66 66 65 72 20 77 69  ormats differ wi
345ea 74 68 20 74 68 65 20 73 65 74 74 69 6e 67 20 6f  th the setting o
345eb 66 20 44 4c 5f 44 45 46 41 55 4c 54 53 2e 20 20  f DL_DEFAULTS.  
345ec 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  Examples:.**.** 
345ed 44 4c 5f 44 4f 43 49 44 53 3a 20 5b 31 5d 20 5b  DL_DOCIDS: [1] [
345ee 33 5d 20 5b 37 5d 0a 2a 2a 20 44 4c 5f 50 4f 53  3] [7].** DL_POS
345ef 49 54 49 4f 4e 53 3a 20 5b 31 20 30 5b 30 20 34  ITIONS: [1 0[0 4
345f0 5d 20 31 5b 31 37 5d 5d 20 5b 33 20 31 5b 35 5d  ] 1[17]] [3 1[5]
345f1 5d 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  ].** DL_POSITION
345f2 53 5f 4f 46 46 53 45 54 53 3a 20 5b 31 20 30 5b  S_OFFSETS: [1 0[
345f3 30 2c 30 2c 33 20 34 2c 32 33 2c 32 36 5d 20 31  0,0,3 4,23,26] 1
345f4 5b 31 37 2c 31 30 32 2c 31 30 35 5d 5d 20 5b 33  [17,102,105]] [3
345f5 20 31 5b 35 2c 32 30 2c 32 33 5d 5d 0a 2a 2a 0a   1[5,20,23]].**.
345f6 2a 2a 20 49 6e 20 65 61 63 68 20 63 61 73 65 20  ** In each case 
345f7 74 68 65 20 6e 75 6d 62 65 72 20 61 66 74 65 72  the number after
345f8 20 74 68 65 20 6f 75 74 65 72 20 27 5b 27 20 69   the outer '[' i
345f9 73 20 74 68 65 20 64 6f 63 69 64 2e 20 20 49 6e  s the docid.  In
345fa 20 74 68 65 0a 2a 2a 20 6c 61 74 74 65 72 20 74   the.** latter t
345fb 77 6f 20 63 61 73 65 73 2c 20 74 68 65 20 6e 75  wo cases, the nu
345fc 6d 62 65 72 20 62 65 66 6f 72 65 20 74 68 65 20  mber before the 
345fd 69 6e 6e 65 72 20 27 5b 27 20 69 73 20 74 68 65  inner '[' is the
345fe 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 73 73 6f 63   column.** assoc
345ff 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 76  iated with the v
34600 61 6c 75 65 73 20 77 69 74 68 69 6e 2e 20 20 46  alues within.  F
34601 6f 72 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20  or DL_POSITIONS 
34602 74 68 65 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77  the numbers.** w
34603 69 74 68 69 6e 20 61 72 65 20 74 68 65 20 70 6f  ithin are the po
34604 73 69 74 69 6f 6e 73 2c 20 66 6f 72 20 44 4c 5f  sitions, for DL_
34605 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
34606 53 20 74 68 65 79 20 61 72 65 20 74 68 65 0a 2a  S they are the.*
34607 2a 20 70 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20  * position, the 
34608 73 74 61 72 74 20 6f 66 66 73 65 74 2c 20 61 6e  start offset, an
34609 64 20 74 68 65 20 65 6e 64 20 6f 66 66 73 65 74  d the end offset
3460a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
3460b 20 64 75 6d 70 44 6f 63 6c 69 73 74 46 75 6e 63   dumpDoclistFunc
3460c 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
3460d 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20  ext *pContext,. 
3460e 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
3460f 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
34610 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
34611 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20  rsor *pCursor;. 
34612 20 69 66 28 20 61 72 67 63 21 3d 32 20 26 26 20   if( argc!=2 && 
34613 61 72 67 63 21 3d 34 20 29 7b 0a 20 20 20 20 67  argc!=4 ){.    g
34614 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
34615 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63  ntext, "dump_doc
34616 6c 69 73 74 22 2c 20 22 69 6e 63 6f 72 72 65 63  list", "incorrec
34617 74 20 61 72 67 75 6d 65 6e 74 73 22 29 3b 0a 20  t arguments");. 
34618 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
34619 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
3461a 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42  gv[0])!=SQLITE_B
3461b 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  LOB ||.         
3461c 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
3461d 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21  _bytes(argv[0])!
3461e 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29  =sizeof(pCursor)
3461f 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65   ){.    generate
34620 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
34621 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20  "dump_doclist", 
34622 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61  "illegal first a
34623 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c  rgument");.  }el
34624 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  se if( sqlite3_v
34625 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
34626 5d 29 3d 3d 4e 55 4c 4c 20 7c 7c 0a 20 20 20 20  ])==NULL ||.    
34627 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
34628 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
34629 31 5d 29 5b 30 5d 3d 3d 27 5c 30 27 20 29 7b 0a  1])[0]=='\0' ){.
3462a 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
3462b 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
3462c 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22 65 6d 70  p_doclist", "emp
3462d 74 79 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  ty second argume
3462e 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  nt");.  }else{. 
3462f 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
34630 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20 63 68  Term = (const ch
34631 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
34632 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
34633 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  ;.    const int 
34634 6e 54 65 72 6d 20 3d 20 73 74 72 6c 65 6e 28 70  nTerm = strlen(p
34635 54 65 72 6d 29 3b 0a 20 20 20 20 66 75 6c 6c 74  Term);.    fullt
34636 65 78 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20  ext_vtab *v;.   
34637 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 44 61 74   int rc;.    Dat
34638 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 3b  aBuffer doclist;
34639 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43  ..    memcpy(&pC
3463a 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76  ursor, sqlite3_v
3463b 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
3463c 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73  ]), sizeof(pCurs
3463d 6f 72 29 29 3b 0a 20 20 20 20 76 20 3d 20 63 75  or));.    v = cu
3463e 72 73 6f 72 5f 76 74 61 62 28 70 43 75 72 73 6f  rsor_vtab(pCurso
3463f 72 29 3b 0a 0a 20 20 20 20 64 61 74 61 42 75 66  r);..    dataBuf
34640 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73 74  ferInit(&doclist
34641 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 74 65  , 0);..    /* te
34642 72 6d 53 65 6c 65 63 74 28 29 20 79 69 65 6c 64  rmSelect() yield
34643 73 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63  s the same logic
34644 61 6c 20 64 6f 63 6c 69 73 74 20 74 68 61 74 20  al doclist that 
34645 71 75 65 72 69 65 73 20 61 72 65 0a 20 20 20 20  queries are.    
34646 2a 2a 20 72 75 6e 20 61 67 61 69 6e 73 74 2e 0a  ** run against..
34647 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61      */.    if( a
34648 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  rgc==2 ){.      
34649 72 63 20 3d 20 74 65 72 6d 53 65 6c 65 63 74 28  rc = termSelect(
3464a 76 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 70  v, v->nColumn, p
3464b 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 2c 20  Term, nTerm, 0, 
3464c 44 4c 5f 44 45 46 41 55 4c 54 2c 20 26 64 6f 63  DL_DEFAULT, &doc
3464d 6c 69 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  list);.    }else
3464e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
3464f 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a  stmt *s = NULL;.
34650 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 6f 75  .      /* Get ou
34651 72 20 73 70 65 63 69 66 69 63 20 73 65 67 6d 65  r specific segme
34652 6e 74 27 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  nt's information
34653 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  . */.      rc = 
34654 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
34655 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45  t(v, SEGDIR_SELE
34656 43 54 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c  CT_SEGMENT_STMT,
34657 20 26 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20   &s);.      if( 
34658 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
34659 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
3465a 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73  lite3_bind_int(s
3465b 2c 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  , 1, sqlite3_val
3465c 75 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 29  ue_int(argv[2]))
3465d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
3465e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3465f 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
34660 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73  lite3_bind_int(s
34661 2c 20 32 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  , 2, sqlite3_val
34662 75 65 5f 69 6e 74 28 61 72 67 76 5b 33 5d 29 29  ue_int(argv[3]))
34663 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
34664 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72    }..      if( r
34665 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
34666 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
34667 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a 20  ite3_step(s);.. 
34668 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
34669 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
3466a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
3466b 65 72 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69  erDestroy(&docli
3466c 73 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67  st);.          g
3466d 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
3466e 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63  ntext, "dump_doc
3466f 6c 69 73 74 22 2c 20 22 73 65 67 6d 65 6e 74 20  list", "segment 
34670 6e 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20  not found");.   
34671 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
34672 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
34673 20 20 2f 2a 20 46 6f 75 6e 64 20 61 20 73 65 67    /* Found a seg
34674 6d 65 6e 74 2c 20 6c 6f 61 64 20 69 74 20 69 6e  ment, load it in
34675 74 6f 20 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20  to doclist. */. 
34676 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
34677 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
34678 20 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c         const sql
34679 69 74 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65  ite_int64 iLeave
3467a 73 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63  sEnd = sqlite3_c
3467b 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31  olumn_int64(s, 1
3467c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  );.          con
3467d 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d  st char *pData =
3467e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
3467f 62 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20  blob(s, 2);.    
34680 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
34681 6e 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f  nData = sqlite3_
34682 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20  column_bytes(s, 
34683 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  2);..          /
34684 2a 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29 20  * loadSegment() 
34685 69 73 20 75 73 65 64 20 62 79 20 74 65 72 6d 53  is used by termS
34686 65 6c 65 63 74 28 29 20 74 6f 20 6c 6f 61 64 20  elect() to load 
34687 65 61 63 68 0a 20 20 20 20 20 20 20 20 20 20 2a  each.          *
34688 2a 20 73 65 67 6d 65 6e 74 27 73 20 64 61 74 61  * segment's data
34689 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
3468a 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f           rc = lo
3468b 61 64 53 65 67 6d 65 6e 74 28 76 2c 20 70 44 61  adSegment(v, pDa
3468c 74 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65 61 76  ta, nData, iLeav
3468d 65 73 45 6e 64 2c 20 70 54 65 72 6d 2c 20 6e 54  esEnd, pTerm, nT
3468e 65 72 6d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  erm, 0,.        
3468f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34690 20 20 20 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20     &doclist);.  
34691 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
34692 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
34693 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
34694 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a  lite3_step(s);..
34695 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
34696 68 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 6d  hould not have m
34697 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6d 61 74  ore than one mat
34698 63 68 69 6e 67 20 73 65 67 6d 65 6e 74 2e 20 2a  ching segment. *
34699 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  /.            if
3469a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
3469b 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  E ){.           
3469c 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
3469d 28 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  (s);.           
3469e 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
3469f 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a  troy(&doclist);.
346a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 65                ge
346a1 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e  nerateError(pCon
346a2 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c  text, "dump_docl
346a3 69 73 74 22 2c 20 22 69 6e 76 61 6c 69 64 20 73  ist", "invalid s
346a4 65 67 64 69 72 22 29 3b 0a 20 20 20 20 20 20 20  egdir");.       
346a5 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
346a6 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
346a7 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
346a8 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
346a9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
346aa 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 73 71       }..      sq
346ab 6c 69 74 65 33 5f 72 65 73 65 74 28 73 29 3b 0a  lite3_reset(s);.
346ac 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72      }..    if( r
346ad 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
346ae 20 20 20 20 20 20 69 66 28 20 64 6f 63 6c 69 73        if( doclis
346af 74 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20  t.nData>0 ){.   
346b0 20 20 20 20 20 63 72 65 61 74 65 44 6f 63 6c 69       createDocli
346b1 73 74 52 65 73 75 6c 74 28 70 43 6f 6e 74 65 78  stResult(pContex
346b2 74 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61  t, doclist.pData
346b3 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 29  , doclist.nData)
346b4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
346b5 20 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73         /* TODO(s
346b6 68 65 73 73 29 3a 20 54 68 69 73 20 63 61 6e 20  hess): This can 
346b7 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 74 65  happen if the te
346b8 72 6d 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e  rm is not presen
346b9 74 2c 20 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a  t, or.        **
346ba 20 69 66 20 61 6c 6c 20 69 6e 73 74 61 6e 63 65   if all instance
346bb 73 20 6f 66 20 74 68 65 20 74 65 72 6d 20 68 61  s of the term ha
346bc 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20  ve been deleted 
346bd 61 6e 64 20 74 68 69 73 20 69 73 0a 20 20 20 20  and this is.    
346be 20 20 20 20 2a 2a 20 61 6e 20 61 6c 6c 2d 69 6e      ** an all-in
346bf 64 65 78 20 64 75 6d 70 2e 20 20 49 74 20 6d 61  dex dump.  It ma
346c0 79 20 62 65 20 69 6e 74 65 72 65 73 74 69 6e 67  y be interesting
346c1 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68 0a   to distinguish.
346c2 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 73 65          ** these
346c3 20 63 61 73 65 73 2e 0a 20 20 20 20 20 20 20 20   cases..        
346c4 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
346c5 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
346c6 43 6f 6e 74 65 78 74 2c 20 22 22 2c 20 30 2c 20  Context, "", 0, 
346c7 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
346c8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
346c9 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45  e if( rc==SQLITE
346ca 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
346cb 2f 2a 20 48 61 6e 64 6c 65 20 6f 75 74 2d 6f 66  /* Handle out-of
346cc 2d 6d 65 6d 6f 72 79 20 63 61 73 65 73 20 73 70  -memory cases sp
346cd 65 63 69 61 6c 6c 79 20 62 65 63 61 75 73 65 20  ecially because 
346ce 69 66 20 74 68 65 79 20 61 72 65 0a 20 20 20 20  if they are.    
346cf 20 20 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 69    ** generated i
346d0 6e 20 66 74 73 33 20 63 6f 64 65 20 74 68 65 79  n fts3 code they
346d1 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 66 6c   may not be refl
346d2 65 63 74 65 64 20 69 6e 20 74 68 65 20 64 62 0a  ected in the db.
346d3 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2e        ** handle.
346d4 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
346d5 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20  /* TODO(shess): 
346d6 48 61 6e 64 6c 65 20 74 68 69 73 20 6d 6f 72 65  Handle this more
346d7 20 63 6f 6d 70 72 65 68 65 6e 73 69 76 65 6c 79   comprehensively
346d8 2e 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74  ..      ** sqlit
346d9 65 33 45 72 72 53 74 72 28 29 20 68 61 73 20 77  e3ErrStr() has w
346da 68 61 74 20 49 20 6e 65 65 64 2c 20 62 75 74 20  hat I need, but 
346db 69 73 20 69 6e 74 65 72 6e 61 6c 2e 0a 20 20 20  is internal..   
346dc 20 20 20 2a 2f 0a 20 20 20 20 20 20 67 65 6e 65     */.      gene
346dd 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
346de 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73  xt, "dump_doclis
346df 74 22 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  t", "out of memo
346e0 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ry");.    }else{
346e1 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45  .      generateE
346e2 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
346e3 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 4e  dump_doclist", N
346e4 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ULL);.    }..   
346e5 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
346e6 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20  oy(&doclist);.  
346e7 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
346e8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
346e9 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 78 46  mplements the xF
346ea 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68  indFunction meth
346eb 6f 64 20 66 6f 72 20 74 68 65 20 46 54 53 33 0a  od for the FTS3.
346ec 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
346ed 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
346ee 66 75 6c 6c 74 65 78 74 46 69 6e 64 46 75 6e 63  fulltextFindFunc
346ef 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  tion(.  sqlite3_
346f0 76 74 61 62 20 2a 70 56 74 61 62 2c 0a 20 20 69  vtab *pVtab,.  i
346f1 6e 74 20 6e 41 72 67 2c 0a 20 20 63 6f 6e 73 74  nt nArg,.  const
346f2 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20   char *zName,.  
346f3 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28  void (**pxFunc)(
346f4 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
346f5 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
346f6 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 2a  ue**),.  void **
346f7 70 70 41 72 67 0a 29 7b 0a 20 20 69 66 28 20 73  ppArg.){.  if( s
346f8 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 73 6e 69  trcmp(zName,"sni
346f9 70 70 65 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20  ppet")==0 ){.   
346fa 20 2a 70 78 46 75 6e 63 20 3d 20 73 6e 69 70 70   *pxFunc = snipp
346fb 65 74 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75  etFunc;.    retu
346fc 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
346fd 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22  ( strcmp(zName,"
346fe 6f 66 66 73 65 74 73 22 29 3d 3d 30 20 29 7b 0a  offsets")==0 ){.
346ff 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 73 6e      *pxFunc = sn
34700 69 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e 63  ippetOffsetsFunc
34701 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
34702 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63    }else if( strc
34703 6d 70 28 7a 4e 61 6d 65 2c 22 6f 70 74 69 6d 69  mp(zName,"optimi
34704 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  ze")==0 ){.    *
34705 70 78 46 75 6e 63 20 3d 20 6f 70 74 69 6d 69 7a  pxFunc = optimiz
34706 65 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72  eFunc;.    retur
34707 6e 20 31 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  n 1;.#ifdef SQLI
34708 54 45 5f 54 45 53 54 0a 20 20 20 20 2f 2a 20 4e  TE_TEST.    /* N
34709 4f 54 45 28 73 68 65 73 73 29 3a 20 54 68 65 73  OTE(shess): Thes
3470a 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
3470b 70 72 65 73 65 6e 74 20 6f 6e 6c 79 20 66 6f 72  present only for
3470c 20 74 65 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20   testing.    ** 
3470d 70 75 72 70 6f 73 65 73 2e 20 20 4e 6f 20 70 61  purposes.  No pa
3470e 72 74 69 63 75 6c 61 72 20 65 66 66 6f 72 74 20  rticular effort 
3470f 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 74 69 6d  is made to optim
34710 69 7a 65 20 74 68 65 69 72 0a 20 20 20 20 2a 2a  ize their.    **
34711 20 65 78 65 63 75 74 69 6f 6e 20 6f 72 20 68 6f   execution or ho
34712 77 20 74 68 65 79 20 62 75 69 6c 64 20 74 68 65  w they build the
34713 69 72 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20  ir results..    
34714 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  */.  }else if( s
34715 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 64 75 6d  trcmp(zName,"dum
34716 70 5f 74 65 72 6d 73 22 29 3d 3d 30 20 29 7b 0a  p_terms")==0 ){.
34717 20 20 20 20 2f 2a 20 66 70 72 69 6e 74 66 28 73      /* fprintf(s
34718 74 64 65 72 72 2c 20 22 46 6f 75 6e 64 20 64 75  tderr, "Found du
34719 6d 70 5f 74 65 72 6d 73 5c 6e 22 29 3b 20 2a 2f  mp_terms\n"); */
3471a 0a 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 64  .    *pxFunc = d
3471b 75 6d 70 54 65 72 6d 73 46 75 6e 63 3b 0a 20 20  umpTermsFunc;.  
3471c 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65    return 1;.  }e
3471d 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a  lse if( strcmp(z
3471e 4e 61 6d 65 2c 22 64 75 6d 70 5f 64 6f 63 6c 69  Name,"dump_docli
3471f 73 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  st")==0 ){.    /
34720 2a 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72  * fprintf(stderr
34721 2c 20 22 46 6f 75 6e 64 20 64 75 6d 70 5f 64 6f  , "Found dump_do
34722 63 6c 69 73 74 5c 6e 22 29 3b 20 2a 2f 0a 20 20  clist\n"); */.  
34723 20 20 2a 70 78 46 75 6e 63 20 3d 20 64 75 6d 70    *pxFunc = dump
34724 44 6f 63 6c 69 73 74 46 75 6e 63 3b 0a 20 20 20  DoclistFunc;.   
34725 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69   return 1;.#endi
34726 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  f.  }.  return 0
34727 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6e 61 6d  ;.}../*.** Renam
34728 65 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 2e  e an fts3 table.
34729 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
3472a 75 6c 6c 74 65 78 74 52 65 6e 61 6d 65 28 0a 20  ulltextRename(. 
3472b 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
3472c 56 74 61 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Vtab,.  const ch
3472d 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 66  ar *zName.){.  f
3472e 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 20  ulltext_vtab *p 
3472f 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  = (fulltext_vtab
34730 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20   *)pVtab;.  int 
34731 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
34732 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20  M;.  char *zSql 
34733 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
34734 66 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41  f(.    "ALTER TA
34735 42 4c 45 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65  BLE %Q.'%q_conte
34736 6e 74 27 20 20 52 45 4e 41 4d 45 20 54 4f 20 27  nt'  RENAME TO '
34737 25 71 5f 63 6f 6e 74 65 6e 74 27 3b 22 0a 20 20  %q_content';".  
34738 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
34739 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 20  Q.'%q_segments' 
3473a 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73 65  RENAME TO '%q_se
3473b 67 6d 65 6e 74 73 27 3b 22 0a 20 20 20 20 22 41  gments';".    "A
3473c 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25  LTER TABLE %Q.'%
3473d 71 5f 73 65 67 64 69 72 27 20 20 20 52 45 4e 41  q_segdir'   RENA
3473e 4d 45 20 54 4f 20 27 25 71 5f 73 65 67 64 69 72  ME TO '%q_segdir
3473f 27 3b 22 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62  ';".    , p->zDb
34740 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  , p->zName, zNam
34741 65 20 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c  e .    , p->zDb,
34742 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65   p->zName, zName
34743 20 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20   .    , p->zDb, 
34744 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0a  p->zName, zName.
34745 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20    );.  if( zSql 
34746 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
34747 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20  te3_exec(p->db, 
34748 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  zSql, 0, 0, 0);.
34749 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
3474a 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72 65  (zSql);.  }.  re
3474b 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
3474c 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
3474d 5f 6d 6f 64 75 6c 65 20 66 74 73 33 4d 6f 64 75  _module fts3Modu
3474e 6c 65 20 3d 20 7b 0a 20 20 2f 2a 20 69 56 65 72  le = {.  /* iVer
3474f 73 69 6f 6e 20 20 20 20 20 20 2a 2f 20 30 2c 0a  sion      */ 0,.
34750 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 20 20    /* xCreate    
34751 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 72     */ fulltextCr
34752 65 61 74 65 2c 0a 20 20 2f 2a 20 78 43 6f 6e 6e  eate,.  /* xConn
34753 65 63 74 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c  ect      */ full
34754 74 65 78 74 43 6f 6e 6e 65 63 74 2c 0a 20 20 2f  textConnect,.  /
34755 2a 20 78 42 65 73 74 49 6e 64 65 78 20 20 20 20  * xBestIndex    
34756 2a 2f 20 66 75 6c 6c 74 65 78 74 42 65 73 74 49  */ fulltextBestI
34757 6e 64 65 78 2c 0a 20 20 2f 2a 20 78 44 69 73 63  ndex,.  /* xDisc
34758 6f 6e 6e 65 63 74 20 20 20 2a 2f 20 66 75 6c 6c  onnect   */ full
34759 74 65 78 74 44 69 73 63 6f 6e 6e 65 63 74 2c 0a  textDisconnect,.
3475a 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 20 20    /* xDestroy   
3475b 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 44 65     */ fulltextDe
3475c 73 74 72 6f 79 2c 0a 20 20 2f 2a 20 78 4f 70 65  stroy,.  /* xOpe
3475d 6e 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  n         */ ful
3475e 6c 74 65 78 74 4f 70 65 6e 2c 0a 20 20 2f 2a 20  ltextOpen,.  /* 
3475f 78 43 6c 6f 73 65 20 20 20 20 20 20 20 20 2a 2f  xClose        */
34760 20 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 2c 0a   fulltextClose,.
34761 20 20 2f 2a 20 78 46 69 6c 74 65 72 20 20 20 20    /* xFilter    
34762 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 46 69     */ fulltextFi
34763 6c 74 65 72 2c 0a 20 20 2f 2a 20 78 4e 65 78 74  lter,.  /* xNext
34764 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c           */ full
34765 74 65 78 74 4e 65 78 74 2c 0a 20 20 2f 2a 20 78  textNext,.  /* x
34766 45 6f 66 20 20 20 20 20 20 20 20 20 20 2a 2f 20  Eof          */ 
34767 66 75 6c 6c 74 65 78 74 45 6f 66 2c 0a 20 20 2f  fulltextEof,.  /
34768 2a 20 78 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20  * xColumn       
34769 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6f 6c 75 6d  */ fulltextColum
3476a 6e 2c 0a 20 20 2f 2a 20 78 52 6f 77 69 64 20 20  n,.  /* xRowid  
3476b 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
3476c 74 52 6f 77 69 64 2c 0a 20 20 2f 2a 20 78 55 70  tRowid,.  /* xUp
3476d 64 61 74 65 20 20 20 20 20 20 20 2a 2f 20 66 75  date       */ fu
3476e 6c 6c 74 65 78 74 55 70 64 61 74 65 2c 0a 20 20  lltextUpdate,.  
3476f 2f 2a 20 78 42 65 67 69 6e 20 20 20 20 20 20 20  /* xBegin       
34770 20 2a 2f 20 66 75 6c 6c 74 65 78 74 42 65 67 69   */ fulltextBegi
34771 6e 2c 0a 20 20 2f 2a 20 78 53 79 6e 63 20 20 20  n,.  /* xSync   
34772 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
34773 74 53 79 6e 63 2c 0a 20 20 2f 2a 20 78 43 6f 6d  tSync,.  /* xCom
34774 6d 69 74 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  mit       */ ful
34775 6c 74 65 78 74 43 6f 6d 6d 69 74 2c 0a 20 20 2f  ltextCommit,.  /
34776 2a 20 78 52 6f 6c 6c 62 61 63 6b 20 20 20 20 20  * xRollback     
34777 2a 2f 20 66 75 6c 6c 74 65 78 74 52 6f 6c 6c 62  */ fulltextRollb
34778 61 63 6b 2c 0a 20 20 2f 2a 20 78 46 69 6e 64 46  ack,.  /* xFindF
34779 75 6e 63 74 69 6f 6e 20 2a 2f 20 66 75 6c 6c 74  unction */ fullt
3477a 65 78 74 46 69 6e 64 46 75 6e 63 74 69 6f 6e 2c  extFindFunction,
3477b 0a 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20 2a 2f  .  /* xRename */
3477c 20 20 20 20 20 20 20 66 75 6c 6c 74 65 78 74 52         fulltextR
3477d 65 6e 61 6d 65 2c 0a 7d 3b 0a 0a 73 74 61 74 69  ename,.};..stati
3477e 63 20 76 6f 69 64 20 68 61 73 68 44 65 73 74 72  c void hashDestr
3477f 6f 79 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 66  oy(void *p){.  f
34780 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 20 3d  ts3Hash *pHash =
34781 20 28 66 74 73 33 48 61 73 68 20 2a 29 70 3b 0a   (fts3Hash *)p;.
34782 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73    sqlite3Fts3Has
34783 68 43 6c 65 61 72 28 70 48 61 73 68 29 3b 0a 20  hClear(pHash);. 
34784 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48   sqlite3_free(pH
34785 61 73 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ash);.}../*.** T
34786 68 65 20 66 74 73 33 20 62 75 69 6c 74 2d 69 6e  he fts3 built-in
34787 20 74 6f 6b 65 6e 69 7a 65 72 73 20 2d 20 22 73   tokenizers - "s
34788 69 6d 70 6c 65 22 20 61 6e 64 20 22 70 6f 72 74  imple" and "port
34789 65 72 22 20 2d 20 61 72 65 20 69 6d 70 6c 65 6d  er" - are implem
3478a 65 6e 74 65 64 0a 2a 2a 20 69 6e 20 66 69 6c 65  ented.** in file
3478b 73 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  s fts3_tokenizer
3478c 31 2e 63 20 61 6e 64 20 66 74 73 33 5f 70 6f 72  1.c and fts3_por
3478d 74 65 72 2e 63 20 72 65 73 70 65 63 74 69 76 65  ter.c respective
3478e 6c 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ly. The followin
3478f 67 0a 2a 2a 20 74 77 6f 20 66 6f 72 77 61 72 64  g.** two forward
34790 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 61 72   declarations ar
34791 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20  e for functions 
34792 64 65 63 6c 61 72 65 64 20 69 6e 20 74 68 65 73  declared in thes
34793 65 20 66 69 6c 65 73 0a 2a 2a 20 75 73 65 64 20  e files.** used 
34794 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20  to retrieve the 
34795 72 65 73 70 65 63 74 69 76 65 20 69 6d 70 6c 65  respective imple
34796 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  mentations..**.*
34797 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  * Calling sqlite
34798 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e  3Fts3SimpleToken
34799 69 7a 65 72 4d 6f 64 75 6c 65 28 29 20 73 65 74  izerModule() set
3479a 73 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e  s the value poin
3479b 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65  ted.** to by the
3479c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 6f 69   argument to poi
3479d 6e 74 20 61 20 74 68 65 20 22 73 69 6d 70 6c 65  nt a the "simple
3479e 22 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c  " tokenizer impl
3479f 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 46  ementation..** F
347a0 75 6e 63 74 69 6f 6e 20 2e 2e 2e 50 6f 72 74 65  unction ...Porte
347a1 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  rTokenizerModule
347a2 28 29 20 73 65 74 73 20 2a 70 4d 6f 64 75 6c 65  () sets *pModule
347a3 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
347a4 0a 2a 2a 20 70 6f 72 74 65 72 20 74 6f 6b 65 6e  .** porter token
347a5 69 7a 65 72 2f 73 74 65 6d 6d 65 72 20 69 6d 70  izer/stemmer imp
347a6 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a  lementation..*/.
347a7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
347a8 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
347a9 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
347aa 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  dule(sqlite3_tok
347ab 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
347ac 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a  nst**ppModule);.
347ad 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
347ae 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 50  oid sqlite3Fts3P
347af 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f  orterTokenizerMo
347b0 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  dule(sqlite3_tok
347b1 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
347b2 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a  nst**ppModule);.
347b3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
347b4 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 49  oid sqlite3Fts3I
347b5 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  cuTokenizerModul
347b6 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
347b7 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
347b8 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a 53 51  **ppModule);..SQ
347b9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
347ba 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
347bb 48 61 73 68 54 61 62 6c 65 28 73 71 6c 69 74 65  HashTable(sqlite
347bc 33 20 2a 2c 20 66 74 73 33 48 61 73 68 20 2a 2c  3 *, fts3Hash *,
347bd 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
347be 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 73  ./*.** Initialis
347bf 65 20 74 68 65 20 66 74 73 33 20 65 78 74 65 6e  e the fts3 exten
347c0 73 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 65 78  sion. If this ex
347c1 74 65 6e 73 69 6f 6e 20 69 73 20 62 75 69 6c 74  tension is built
347c2 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74   as part.** of t
347c3 68 65 20 73 71 6c 69 74 65 20 6c 69 62 72 61 72  he sqlite librar
347c4 79 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  y, then this fun
347c5 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
347c6 64 69 72 65 63 74 6c 79 20 62 79 0a 2a 2a 20 53  directly by.** S
347c7 51 4c 69 74 65 2e 20 49 66 20 66 74 73 33 20 69  QLite. If fts3 i
347c8 73 20 62 75 69 6c 74 20 61 73 20 61 20 64 79 6e  s built as a dyn
347c9 61 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 61 62 6c  amically loadabl
347ca 65 20 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69  e extension, thi
347cb 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  s.** function is
347cc 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 73   called by the s
347cd 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e  qlite3_extension
347ce 5f 69 6e 69 74 28 29 20 65 6e 74 72 79 20 70 6f  _init() entry po
347cf 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  int..*/.SQLITE_P
347d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
347d1 65 33 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74  e3Fts3Init(sqlit
347d2 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72  e3 *db){.  int r
347d3 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
347d4 20 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68   fts3Hash *pHash
347d5 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71   = 0;.  const sq
347d6 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
347d7 6d 6f 64 75 6c 65 20 2a 70 53 69 6d 70 6c 65 20  module *pSimple 
347d8 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  = 0;.  const sql
347d9 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
347da 6f 64 75 6c 65 20 2a 70 50 6f 72 74 65 72 20 3d  odule *pPorter =
347db 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   0;.  const sqli
347dc 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
347dd 64 75 6c 65 20 2a 70 49 63 75 20 3d 20 30 3b 0a  dule *pIcu = 0;.
347de 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 69  .  sqlite3Fts3Si
347df 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
347e0 75 6c 65 28 26 70 53 69 6d 70 6c 65 29 3b 0a 20  ule(&pSimple);. 
347e1 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74   sqlite3Fts3Port
347e2 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  erTokenizerModul
347e3 65 28 26 70 50 6f 72 74 65 72 29 3b 0a 23 69 66  e(&pPorter);.#if
347e4 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
347e5 45 5f 49 43 55 0a 20 20 73 71 6c 69 74 65 33 46  E_ICU.  sqlite3F
347e6 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d  ts3IcuTokenizerM
347e7 6f 64 75 6c 65 28 26 70 49 63 75 29 3b 0a 23 65  odule(&pIcu);.#e
347e8 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  ndif..  /* Alloc
347e9 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ate and initiali
347ea 73 65 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c  se the hash-tabl
347eb 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  e used to store 
347ec 74 6f 6b 65 6e 69 7a 65 72 73 2e 20 2a 2f 0a 20  tokenizers. */. 
347ed 20 70 48 61 73 68 20 3d 20 73 71 6c 69 74 65 33   pHash = sqlite3
347ee 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66  _malloc(sizeof(f
347ef 74 73 33 48 61 73 68 29 29 3b 0a 20 20 69 66 28  ts3Hash));.  if(
347f0 20 21 70 48 61 73 68 20 29 7b 0a 20 20 20 20 72   !pHash ){.    r
347f1 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
347f2 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
347f3 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
347f4 69 74 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48  it(pHash, FTS3_H
347f5 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a  ASH_STRING, 1);.
347f6 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74    }..  /* Load t
347f7 68 65 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65  he built-in toke
347f8 6e 69 7a 65 72 73 20 69 6e 74 6f 20 74 68 65 20  nizers into the 
347f9 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
347fa 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
347fb 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  K ){.    if( sql
347fc 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
347fd 72 74 28 70 48 61 73 68 2c 20 22 73 69 6d 70 6c  rt(pHash, "simpl
347fe 65 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70  e", 7, (void *)p
347ff 53 69 6d 70 6c 65 29 0a 20 20 20 20 20 7c 7c 20  Simple).     || 
34800 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
34801 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22 70 6f  nsert(pHash, "po
34802 72 74 65 72 22 2c 20 37 2c 20 28 76 6f 69 64 20  rter", 7, (void 
34803 2a 29 70 50 6f 72 74 65 72 29 20 0a 20 20 20 20  *)pPorter) .    
34804 20 7c 7c 20 28 70 49 63 75 20 26 26 20 73 71 6c   || (pIcu && sql
34805 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
34806 72 74 28 70 48 61 73 68 2c 20 22 69 63 75 22 2c  rt(pHash, "icu",
34807 20 34 2c 20 28 76 6f 69 64 20 2a 29 70 49 63 75   4, (void *)pIcu
34808 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  )).    ){.      
34809 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
3480a 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69  M;.    }.  }..#i
3480b 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
3480c 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  .  sqlite3Fts3Ex
3480d 70 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72 66  prInitTestInterf
3480e 61 63 65 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a  ace(db);.#endif.
3480f 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65  .  /* Create the
34810 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 77   virtual table w
34811 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68  rapper around th
34812 65 20 68 61 73 68 2d 74 61 62 6c 65 20 61 6e 64  e hash-table and
34813 20 6f 76 65 72 6c 6f 61 64 20 0a 20 20 2a 2a 20   overload .  ** 
34814 74 68 65 20 74 77 6f 20 73 63 61 6c 61 72 20 66  the two scalar f
34815 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 69  unctions. If thi
34816 73 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  s is successful,
34817 20 72 65 67 69 73 74 65 72 20 74 68 65 0a 20 20   register the.  
34818 2a 2a 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 73  ** module with s
34819 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  qlite..  */.  if
3481a 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
3481b 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b  .   && SQLITE_OK
3481c 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 46  ==(rc = sqlite3F
3481d 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65  ts3InitHashTable
3481e 28 64 62 2c 20 70 48 61 73 68 2c 20 22 66 74 73  (db, pHash, "fts
3481f 33 5f 74 6f 6b 65 6e 69 7a 65 72 22 29 29 0a 20  3_tokenizer")). 
34820 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
34821 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76  (rc = sqlite3_ov
34822 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
34823 64 62 2c 20 22 73 6e 69 70 70 65 74 22 2c 20 2d  db, "snippet", -
34824 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45  1)).   && SQLITE
34825 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
34826 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
34827 74 69 6f 6e 28 64 62 2c 20 22 6f 66 66 73 65 74  tion(db, "offset
34828 73 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20 53  s", -1)).   && S
34829 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
3482a 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
3482b 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6f  _function(db, "o
3482c 70 74 69 6d 69 7a 65 22 2c 20 2d 31 29 29 0a 23  ptimize", -1)).#
3482d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
3482e 54 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f  T.   && SQLITE_O
3482f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
34830 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
34831 6f 6e 28 64 62 2c 20 22 64 75 6d 70 5f 74 65 72  on(db, "dump_ter
34832 6d 73 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20  ms", -1)).   && 
34833 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
34834 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
34835 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22  d_function(db, "
34836 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 2d  dump_doclist", -
34837 31 29 29 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a  1)).#endif.  ){.
34838 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
34839 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
3483a 5f 76 32 28 0a 20 20 20 20 20 20 20 20 64 62 2c  _v2(.        db,
3483b 20 22 66 74 73 33 22 2c 20 26 66 74 73 33 4d 6f   "fts3", &fts3Mo
3483c 64 75 6c 65 2c 20 28 76 6f 69 64 20 2a 29 70 48  dule, (void *)pH
3483d 61 73 68 2c 20 68 61 73 68 44 65 73 74 72 6f 79  ash, hashDestroy
3483e 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  .    );.  }..  /
3483f 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  * An error has o
34840 63 63 75 72 72 65 64 2e 20 44 65 6c 65 74 65 20  ccurred. Delete 
34841 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61  the hash table a
34842 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  nd return the er
34843 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 61  ror code. */.  a
34844 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
34845 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 48  E_OK );.  if( pH
34846 61 73 68 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ash ){.    sqlit
34847 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28  e3Fts3HashClear(
34848 70 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c 69  pHash);.    sqli
34849 74 65 33 5f 66 72 65 65 28 70 48 61 73 68 29 3b  te3_free(pHash);
3484a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
3484b 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54 45  ;.}..#if !SQLITE
3484c 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50 49  _CORE.SQLITE_API
3484d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74   int sqlite3_ext
3484e 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20 73  ension_init(.  s
3484f 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63  qlite3 *db, .  c
34850 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a  har **pzErrMsg,.
34851 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
34852 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70 41  api_routines *pA
34853 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f 45  pi.){.  SQLITE_E
34854 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 70  XTENSION_INIT2(p
34855 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73 71  Api).  return sq
34856 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 64 62  lite3Fts3Init(db
34857 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  );.}.#endif..#en
34858 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
34859 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
3485a 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
3485b 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
3485c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
3485d 45 6e 64 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a  End of fts3.c **
3485e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3485f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34861 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34862 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
34863 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  expr.c *********
34864 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34865 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34866 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 20 32  /*.** 2008 Nov 2
34867 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
34868 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
34869 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3486a 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3486b 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
3486c 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
3486d 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
3486e 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
3486f 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
34870 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
34871 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
34872 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
34873 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
34874 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
34875 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
34876 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
34877 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
34878 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
34879 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3487a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3487b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3487c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3487d 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
3487e 20 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 73   module contains
3487f 20 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c 65   code that imple
34880 6d 65 6e 74 73 20 61 20 70 61 72 73 65 72 20 66  ments a parser f
34881 6f 72 20 66 74 73 33 20 71 75 65 72 79 20 73 74  or fts3 query st
34882 72 69 6e 67 73 0a 2a 2a 20 28 74 68 65 20 72 69  rings.** (the ri
34883 67 68 74 2d 68 61 6e 64 20 61 72 67 75 6d 65 6e  ght-hand argumen
34884 74 20 74 6f 20 74 68 65 20 4d 41 54 43 48 20 6f  t to the MATCH o
34885 70 65 72 61 74 6f 72 29 2e 20 42 65 63 61 75 73  perator). Becaus
34886 65 20 74 68 65 20 73 75 70 70 6f 72 74 65 64 20  e the supported 
34887 0a 2a 2a 20 73 79 6e 74 61 78 20 69 73 20 72 65  .** syntax is re
34888 6c 61 74 69 76 65 6c 79 20 73 69 6d 70 6c 65 2c  latively simple,
34889 20 74 68 65 20 77 68 6f 6c 65 20 74 6f 6b 65 6e   the whole token
3488a 69 7a 65 72 2f 70 61 72 73 65 72 20 73 79 73 74  izer/parser syst
3488b 65 6d 20 69 73 0a 2a 2a 20 68 61 6e 64 2d 63 6f  em is.** hand-co
3488c 64 65 64 2e 20 54 68 65 20 70 75 62 6c 69 63 20  ded. The public 
3488d 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 69  interface to thi
3488e 73 20 6d 6f 64 75 6c 65 20 69 73 20 64 65 63 6c  s module is decl
3488f 61 72 65 64 20 69 6e 20 73 6f 75 72 63 65 0a 2a  ared in source.*
34890 2a 20 63 6f 64 65 20 66 69 6c 65 20 22 66 74 73  * code file "fts
34891 33 5f 65 78 70 72 2e 68 22 2e 0a 2a 2f 0a 23 69  3_expr.h"..*/.#i
34892 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
34893 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
34894 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
34895 5f 46 54 53 33 29 0a 0a 2f 2a 0a 2a 2a 20 42 79  _FTS3)../*.** By
34896 20 64 65 66 61 75 6c 74 2c 20 74 68 69 73 20 6d   default, this m
34897 6f 64 75 6c 65 20 70 61 72 73 65 73 20 74 68 65  odule parses the
34898 20 6c 65 67 61 63 79 20 73 79 6e 74 61 78 20 74   legacy syntax t
34899 68 61 74 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a  hat has been .**
3489a 20 74 72 61 64 69 74 69 6f 6e 61 6c 6c 79 20 75   traditionally u
3489b 73 65 64 20 62 79 20 66 74 73 33 2e 20 4f 72 2c  sed by fts3. Or,
3489c 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
3489d 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53  E_FTS3_PARENTHES
3489e 49 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64  IS.** is defined
3489f 2c 20 74 68 65 6e 20 69 74 20 75 73 65 73 20 74  , then it uses t
348a0 68 65 20 6e 65 77 20 73 79 6e 74 61 78 2e 20 54  he new syntax. T
348a1 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 62  he differences b
348a2 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  etween.** the ne
348a3 77 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 73 79  w and the old sy
348a4 6e 74 61 78 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a  ntaxes are:.**.*
348a5 2a 20 20 61 29 20 54 68 65 20 6e 65 77 20 73 79  *  a) The new sy
348a6 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 70 61  ntax supports pa
348a7 72 65 6e 74 68 65 73 69 73 2e 20 54 68 65 20 6f  renthesis. The o
348a8 6c 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a  ld does not..**.
348a9 2a 2a 20 20 62 29 20 54 68 65 20 6e 65 77 20 73  **  b) The new s
348aa 79 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 74  yntax supports t
348ab 68 65 20 41 4e 44 20 61 6e 64 20 4e 4f 54 20 6f  he AND and NOT o
348ac 70 65 72 61 74 6f 72 73 2e 20 54 68 65 20 6f 6c  perators. The ol
348ad 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a  d does not..**.*
348ae 2a 20 20 63 29 20 54 68 65 20 6f 6c 64 20 73 79  *  c) The old sy
348af 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 74 68  ntax supports th
348b0 65 20 22 2d 22 20 74 6f 6b 65 6e 20 71 75 61 6c  e "-" token qual
348b1 69 66 69 65 72 2e 20 54 68 69 73 20 69 73 20 6e  ifier. This is n
348b2 6f 74 20 0a 2a 2a 20 20 20 20 20 73 75 70 70 6f  ot .**     suppo
348b3 72 74 65 64 20 62 79 20 74 68 65 20 6e 65 77 20  rted by the new 
348b4 73 79 6e 74 61 78 20 28 69 74 20 69 73 20 72 65  syntax (it is re
348b5 70 6c 61 63 65 64 20 62 79 20 74 68 65 20 4e 4f  placed by the NO
348b6 54 20 6f 70 65 72 61 74 6f 72 29 2e 0a 2a 2a 0a  T operator)..**.
348b7 2a 2a 20 20 64 29 20 57 68 65 6e 20 75 73 69 6e  **  d) When usin
348b8 67 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61 78  g the old syntax
348b9 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61 74 6f  , the OR operato
348ba 72 20 68 61 73 20 61 20 67 72 65 61 74 65 72 20  r has a greater 
348bb 70 72 65 63 65 64 65 6e 63 65 0a 2a 2a 20 20 20  precedence.**   
348bc 20 20 74 68 61 6e 20 61 6e 20 69 6d 70 6c 69 63    than an implic
348bd 69 74 20 41 4e 44 2e 20 57 68 65 6e 20 75 73 69  it AND. When usi
348be 6e 67 20 74 68 65 20 6e 65 77 2c 20 62 6f 74 68  ng the new, both
348bf 20 69 6d 70 6c 69 63 69 74 79 20 61 6e 64 20 65   implicity and e
348c0 78 70 6c 69 63 69 74 0a 2a 2a 20 20 20 20 20 41  xplicit.**     A
348c1 4e 44 20 6f 70 65 72 61 74 6f 72 73 20 68 61 76  ND operators hav
348c2 65 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65  e a higher prece
348c3 64 65 6e 63 65 20 74 68 61 6e 20 4f 52 2e 0a 2a  dence than OR..*
348c4 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64  *.** If compiled
348c5 20 77 69 74 68 20 53 51 4c 49 54 45 5f 54 45 53   with SQLITE_TES
348c6 54 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20  T defined, then 
348c7 74 68 69 73 20 6d 6f 64 75 6c 65 20 65 78 70 6f  this module expo
348c8 72 74 73 20 74 68 65 0a 2a 2a 20 73 79 6d 62 6f  rts the.** symbo
348c9 6c 20 22 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  l "int sqlite3_f
348ca 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
348cb 74 68 65 73 65 73 22 2e 20 53 65 74 74 69 6e 67  theses". Setting
348cc 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a   this variable.*
348cd 2a 20 74 6f 20 7a 65 72 6f 20 63 61 75 73 65 73  * to zero causes
348ce 20 74 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 75   the module to u
348cf 73 65 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61  se the old synta
348d0 78 2e 20 49 66 20 69 74 20 69 73 20 73 65 74 20  x. If it is set 
348d1 74 6f 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20  to .** non-zero 
348d2 74 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20 69  the new syntax i
348d3 73 20 61 63 74 69 76 61 74 65 64 2e 20 54 68 69  s activated. Thi
348d4 73 20 69 73 20 73 6f 20 62 6f 74 68 20 73 79 6e  s is so both syn
348d5 74 61 78 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20  taxes can.** be 
348d6 74 65 73 74 65 64 20 75 73 69 6e 67 20 61 20 73  tested using a s
348d7 69 6e 67 6c 65 20 62 75 69 6c 64 20 6f 66 20 74  ingle build of t
348d8 65 73 74 66 69 78 74 75 72 65 2e 0a 2a 2f 0a 23  estfixture..*/.#
348d9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
348da 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
348db 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e   sqlite3_fts3_en
348dc 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
348dd 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 23 20 69 66   = 0;.#else.# if
348de 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
348df 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53  E_FTS3_PARENTHES
348e0 49 53 20 0a 23 20 20 64 65 66 69 6e 65 20 73 71  IS .#  define sq
348e1 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
348e2 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 31 0a  e_parentheses 1.
348e3 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
348e4 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e   sqlite3_fts3_en
348e5 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
348e6 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69   0.# endif.#endi
348e7 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74  f../*.** Default
348e8 20 73 70 61 6e 20 66 6f 72 20 4e 45 41 52 20 6f   span for NEAR o
348e9 70 65 72 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65  perators..*/.#de
348ea 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 54 53 33  fine SQLITE_FTS3
348eb 5f 44 45 46 41 55 4c 54 5f 4e 45 41 52 5f 50 41  _DEFAULT_NEAR_PA
348ec 52 41 4d 20 31 30 0a 0a 0a 74 79 70 65 64 65 66  RAM 10...typedef
348ed 20 73 74 72 75 63 74 20 50 61 72 73 65 43 6f 6e   struct ParseCon
348ee 74 65 78 74 20 50 61 72 73 65 43 6f 6e 74 65 78  text ParseContex
348ef 74 3b 0a 73 74 72 75 63 74 20 50 61 72 73 65 43  t;.struct ParseC
348f0 6f 6e 74 65 78 74 20 7b 0a 20 20 73 71 6c 69 74  ontext {.  sqlit
348f1 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
348f2 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 2f  okenizer;      /
348f3 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  * Tokenizer modu
348f4 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
348f5 61 72 20 2a 2a 61 7a 43 6f 6c 3b 20 20 20 20 20  ar **azCol;     
348f6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
348f7 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e  rray of column n
348f8 61 6d 65 73 20 66 6f 72 20 66 74 73 33 20 74 61  ames for fts3 ta
348f9 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
348fa 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
348fb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
348fc 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
348fd 73 20 69 6e 20 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a  s in azCol[] */.
348fe 20 20 69 6e 74 20 69 44 65 66 61 75 6c 74 43 6f    int iDefaultCo
348ff 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
34900 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
34901 20 63 6f 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79   column to query
34902 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f   */.  sqlite3_co
34903 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 20 20 20  ntext *pCtx;    
34904 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
34905 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
34906 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   here */.  int n
34907 4e 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Nest;           
34908 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34909 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74  * Number of nest
3490a 65 64 20 62 72 61 63 6b 65 74 73 20 2a 2f 0a 7d  ed brackets */.}
3490b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ;../*.** This fu
3490c 6e 63 74 69 6f 6e 20 69 73 20 65 71 75 69 76 61  nction is equiva
3490d 6c 65 6e 74 20 74 6f 20 74 68 65 20 73 74 61 6e  lent to the stan
3490e 64 61 72 64 20 69 73 73 70 61 63 65 28 29 20 66  dard isspace() f
3490f 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20  unction. .**.** 
34910 54 68 65 20 73 74 61 6e 64 61 72 64 20 69 73 73  The standard iss
34911 70 61 63 65 28 29 20 63 61 6e 20 62 65 20 61 77  pace() can be aw
34912 6b 77 61 72 64 20 74 6f 20 75 73 65 20 73 61 66  kward to use saf
34913 65 6c 79 2c 20 62 65 63 61 75 73 65 20 61 6c 74  ely, because alt
34914 68 6f 75 67 68 20 69 74 0a 2a 2a 20 69 73 20 64  hough it.** is d
34915 65 66 69 6e 65 64 20 74 6f 20 61 63 63 65 70 74  efined to accept
34916 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20   an argument of 
34917 74 79 70 65 20 69 6e 74 2c 20 69 74 73 20 62 65  type int, its be
34918 68 61 76 69 6f 75 72 20 77 68 65 6e 20 70 61 73  haviour when pas
34919 73 65 64 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65  sed.** an intege
3491a 72 20 74 68 61 74 20 66 61 6c 6c 73 20 6f 75 74  r that falls out
3491b 73 69 64 65 20 6f 66 20 74 68 65 20 72 61 6e 67  side of the rang
3491c 65 20 6f 66 20 74 68 65 20 75 6e 73 69 67 6e 65  e of the unsigne
3491d 64 20 63 68 61 72 20 74 79 70 65 0a 2a 2a 20 69  d char type.** i
3491e 73 20 75 6e 64 65 66 69 6e 65 64 20 28 61 6e 64  s undefined (and
3491f 20 73 6f 6d 65 74 69 6d 65 73 2c 20 22 75 6e 64   sometimes, "und
34920 65 66 69 6e 65 64 22 20 6d 65 61 6e 73 20 73 65  efined" means se
34921 67 66 61 75 6c 74 29 2e 20 54 68 69 73 20 77 72  gfault). This wr
34922 61 70 70 65 72 0a 2a 2a 20 69 73 20 64 65 66 69  apper.** is defi
34923 6e 65 64 20 74 6f 20 61 63 63 65 70 74 20 61 6e  ned to accept an
34924 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70   argument of typ
34925 65 20 63 68 61 72 2c 20 61 6e 64 20 61 6c 77 61  e char, and alwa
34926 79 73 20 72 65 74 75 72 6e 73 20 30 20 66 6f 72  ys returns 0 for
34927 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65 73 20 74  .** any values t
34928 68 61 74 20 66 61 6c 6c 20 6f 75 74 73 69 64 65  hat fall outside
34929 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66   of the range of
3492a 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20 63 68   the unsigned ch
3492b 61 72 20 74 79 70 65 20 28 69 2e 65 2e 0a 2a 2a  ar type (i.e..**
3492c 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 73   negative values
3492d 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
3492e 20 66 74 73 33 69 73 73 70 61 63 65 28 63 68 61   fts3isspace(cha
3492f 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  r c){.  return (
34930 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 73  c&0x80)==0 ? iss
34931 70 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 0a  pace(c) : 0;.}..
34932 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
34933 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  e next token fro
34934 6d 20 62 75 66 66 65 72 20 7a 20 28 6c 65 6e 67  m buffer z (leng
34935 74 68 20 6e 29 20 75 73 69 6e 67 20 74 68 65 20  th n) using the 
34936 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 61 6e 64  tokenizer.** and
34937 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69   other informati
34938 6f 6e 20 28 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  on (column names
34939 20 65 74 63 2e 29 20 69 6e 20 70 50 61 72 73 65   etc.) in pParse
3493a 2e 20 43 72 65 61 74 65 20 61 6e 20 46 74 73 33  . Create an Fts3
3493b 45 78 70 72 0a 2a 2a 20 73 74 72 75 63 74 75 72  Expr.** structur
3493c 65 20 6f 66 20 74 79 70 65 20 46 54 53 51 55 45  e of type FTSQUE
3493d 52 59 5f 50 48 52 41 53 45 20 63 6f 6e 74 61 69  RY_PHRASE contai
3493e 6e 69 6e 67 20 61 20 70 68 72 61 73 65 20 63 6f  ning a phrase co
3493f 6e 73 69 73 74 69 6e 67 20 6f 66 20 74 68 69 73  nsisting of this
34940 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e  .** single token
34941 20 61 6e 64 20 73 65 74 20 2a 70 70 45 78 70 72   and set *ppExpr
34942 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 69 74 2e   to point to it.
34943 20 49 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   If the end of t
34944 68 65 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20  he buffer is.** 
34945 72 65 61 63 68 65 64 20 62 65 66 6f 72 65 20 61  reached before a
34946 20 74 6f 6b 65 6e 20 69 73 20 66 6f 75 6e 64 2c   token is found,
34947 20 73 65 74 20 2a 70 70 45 78 70 72 20 74 6f 20   set *ppExpr to 
34948 7a 65 72 6f 2e 20 49 74 20 69 73 20 74 68 65 0a  zero. It is the.
34949 2a 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  ** responsibilit
3494a 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20  y of the caller 
3494b 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 64 65  to eventually de
3494c 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 61 6c 6c  allocate the all
3494d 6f 63 61 74 65 64 20 0a 2a 2a 20 46 74 73 33 45  ocated .** Fts3E
3494e 78 70 72 20 73 74 72 75 63 74 75 72 65 20 28 69  xpr structure (i
3494f 66 20 61 6e 79 29 20 62 79 20 70 61 73 73 69 6e  f any) by passin
34950 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  g it to sqlite3_
34951 66 72 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65  free()..**.** Re
34952 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
34953 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  f successful, or
34954 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
34955 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
34956 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2e 0a 2a  tion.** fails..*
34957 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
34958 4e 65 78 74 54 6f 6b 65 6e 28 0a 20 20 50 61 72  NextToken(.  Par
34959 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73  seContext *pPars
3495a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
3495b 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65       /* fts3 que
3495c 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  ry parse context
3495d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20   */.  int iCol, 
3495e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3495f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34960 20 56 61 6c 75 65 20 66 6f 72 20 46 74 73 33 50   Value for Fts3P
34961 68 72 61 73 65 2e 69 43 6f 6c 75 6d 6e 20 2a 2f  hrase.iColumn */
34962 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
34963 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20  , int n,        
34964 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
34965 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
34966 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
34967 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
34968 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
34969 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
3496a 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20  int *pnConsumed 
3496b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3496c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3496d 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
3496e 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20  consumed */.){. 
3496f 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
34970 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  er *pTokenizer =
34971 20 70 50 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69   pParse->pTokeni
34972 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  zer;.  sqlite3_t
34973 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
34974 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d  const *pModule =
34975 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
34976 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  dule;.  int rc;.
34977 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
34978 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
34979 73 6f 72 3b 0a 20 20 46 74 73 33 45 78 70 72 20  sor;.  Fts3Expr 
3497a 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 69 6e 74  *pRet = 0;.  int
3497b 20 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a   nConsumed = 0;.
3497c 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d  .  rc = pModule-
3497d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65  >xOpen(pTokenize
3497e 72 2c 20 7a 2c 20 6e 2c 20 26 70 43 75 72 73 6f  r, z, n, &pCurso
3497f 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
34980 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63  LITE_OK ){.    c
34981 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65  onst char *zToke
34982 6e 3b 0a 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65  n;.    int nToke
34983 6e 2c 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2c  n, iStart, iEnd,
34984 20 69 50 6f 73 69 74 69 6f 6e 3b 0a 20 20 20 20   iPosition;.    
34985 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20  int nByte;      
34986 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34987 20 20 20 20 20 20 20 20 20 2f 2a 20 74 6f 74 61           /* tota
34988 6c 20 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f 63  l space to alloc
34989 61 74 65 20 2a 2f 0a 0a 20 20 20 20 70 43 75 72  ate */..    pCur
3498a 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  sor->pTokenizer 
3498b 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20  = pTokenizer;.  
3498c 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
3498d 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 20 26  xNext(pCursor, &
3498e 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c  zToken, &nToken,
3498f 20 26 69 53 74 61 72 74 2c 20 26 69 45 6e 64 2c   &iStart, &iEnd,
34990 20 26 69 50 6f 73 69 74 69 6f 6e 29 3b 0a 0a 20   &iPosition);.. 
34991 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
34992 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 42  E_OK ){.      nB
34993 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73  yte = sizeof(Fts
34994 33 45 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28  3Expr) + sizeof(
34995 46 74 73 33 50 68 72 61 73 65 29 20 2b 20 6e 54  Fts3Phrase) + nT
34996 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 70 52 65 74  oken;.      pRet
34997 20 3d 20 28 46 74 73 33 45 78 70 72 20 2a 29 73   = (Fts3Expr *)s
34998 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
34999 79 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  yte);.      if( 
3499a 21 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20  !pRet ){.       
3499b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
3499c 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  EM;.      }else{
3499d 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
3499e 70 52 65 74 2c 20 30 2c 20 6e 42 79 74 65 29 3b  pRet, 0, nByte);
3499f 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 65  .        pRet->e
349a0 54 79 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f  Type = FTSQUERY_
349a1 50 48 52 41 53 45 3b 0a 20 20 20 20 20 20 20 20  PHRASE;.        
349a2 70 52 65 74 2d 3e 70 50 68 72 61 73 65 20 3d 20  pRet->pPhrase = 
349a3 28 46 74 73 33 50 68 72 61 73 65 20 2a 29 26 70  (Fts3Phrase *)&p
349a4 52 65 74 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20  Ret[1];.        
349a5 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  pRet->pPhrase->n
349a6 54 6f 6b 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20  Token = 1;.     
349a7 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65     pRet->pPhrase
349a8 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c  ->iColumn = iCol
349a9 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ;.        pRet->
349aa 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
349ab 30 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20  0].n = nToken;. 
349ac 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
349ad 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e  rase->aToken[0].
349ae 7a 20 3d 20 28 63 68 61 72 20 2a 29 26 70 52 65  z = (char *)&pRe
349af 74 2d 3e 70 50 68 72 61 73 65 5b 31 5d 3b 0a 20  t->pPhrase[1];. 
349b0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 52         memcpy(pR
349b1 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f  et->pPhrase->aTo
349b2 6b 65 6e 5b 30 5d 2e 7a 2c 20 7a 54 6f 6b 65 6e  ken[0].z, zToken
349b3 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20  , nToken);..    
349b4 20 20 20 20 69 66 28 20 69 45 6e 64 3c 6e 20 26      if( iEnd<n &
349b5 26 20 7a 5b 69 45 6e 64 5d 3d 3d 27 2a 27 20 29  & z[iEnd]=='*' )
349b6 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 65 74  {.          pRet
349b7 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
349b8 6e 5b 30 5d 2e 69 73 50 72 65 66 69 78 20 3d 20  n[0].isPrefix = 
349b9 31 3b 0a 20 20 20 20 20 20 20 20 20 20 69 45 6e  1;.          iEn
349ba 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  d++;.        }. 
349bb 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69         if( !sqli
349bc 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
349bd 70 61 72 65 6e 74 68 65 73 65 73 20 26 26 20 69  parentheses && i
349be 53 74 61 72 74 3e 30 20 26 26 20 7a 5b 69 53 74  Start>0 && z[iSt
349bf 61 72 74 2d 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20  art-1]=='-' ){. 
349c0 20 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70           pRet->p
349c1 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74 20 3d 20  Phrase->isNot = 
349c2 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
349c3 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 6f 6e 73     }.      nCons
349c4 75 6d 65 64 20 3d 20 69 45 6e 64 3b 0a 20 20 20  umed = iEnd;.   
349c5 20 7d 0a 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d   }..    pModule-
349c6 3e 78 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29  >xClose(pCursor)
349c7 3b 0a 20 20 7d 0a 20 20 0a 20 20 2a 70 6e 43 6f  ;.  }.  .  *pnCo
349c8 6e 73 75 6d 65 64 20 3d 20 6e 43 6f 6e 73 75 6d  nsumed = nConsum
349c9 65 64 3b 0a 20 20 2a 70 70 45 78 70 72 20 3d 20  ed;.  *ppExpr = 
349ca 70 52 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 72  pRet;.  return r
349cb 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 6c  c;.}.../*.** Enl
349cc 61 72 67 65 20 61 20 6d 65 6d 6f 72 79 20 61 6c  arge a memory al
349cd 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 20 61 6e  location.  If an
349ce 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 61   out-of-memory a
349cf 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73  llocation occurs
349d0 2c 0a 2a 2a 20 74 68 65 6e 20 66 72 65 65 20 74  ,.** then free t
349d1 68 65 20 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 6f  he old allocatio
349d2 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 2a 66 74 73 33  n..*/.void *fts3
349d3 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 6f  ReallocOrFree(vo
349d4 69 64 20 2a 70 4f 72 69 67 2c 20 69 6e 74 20 6e  id *pOrig, int n
349d5 4e 65 77 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52  New){.  void *pR
349d6 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  et = sqlite3_rea
349d7 6c 6c 6f 63 28 70 4f 72 69 67 2c 20 6e 4e 65 77  lloc(pOrig, nNew
349d8 29 3b 0a 20 20 69 66 28 20 21 70 52 65 74 20 29  );.  if( !pRet )
349d9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
349da 65 65 28 70 4f 72 69 67 29 3b 0a 20 20 7d 0a 20  ee(pOrig);.  }. 
349db 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
349dc 0a 2f 2a 0a 2a 2a 20 42 75 66 66 65 72 20 7a 49  ./*.** Buffer zI
349dd 6e 70 75 74 2c 20 6c 65 6e 67 74 68 20 6e 49 6e  nput, length nIn
349de 70 75 74 2c 20 63 6f 6e 74 61 69 6e 73 20 74 68  put, contains th
349df 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20  e contents of a 
349e0 71 75 6f 74 65 64 20 73 74 72 69 6e 67 0a 2a 2a  quoted string.**
349e1 20 74 68 61 74 20 61 70 70 65 61 72 65 64 20 61   that appeared a
349e2 73 20 70 61 72 74 20 6f 66 20 61 6e 20 66 74 73  s part of an fts
349e3 33 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  3 query expressi
349e4 6f 6e 2e 20 4e 65 69 74 68 65 72 20 71 75 6f 74  on. Neither quot
349e5 65 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69  e character.** i
349e6 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  s included in th
349e7 65 20 62 75 66 66 65 72 2e 20 54 68 69 73 20 66  e buffer. This f
349e8 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
349e9 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 74 68 65   to tokenize the
349ea 20 65 6e 74 69 72 65 0a 2a 2a 20 69 6e 70 75 74   entire.** input
349eb 20 62 75 66 66 65 72 20 61 6e 64 20 63 72 65 61   buffer and crea
349ec 74 65 20 61 6e 20 46 74 73 33 45 78 70 72 20 73  te an Fts3Expr s
349ed 74 72 75 63 74 75 72 65 20 6f 66 20 74 79 70 65  tructure of type
349ee 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
349ef 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20   .** containing 
349f0 74 68 65 20 72 65 73 75 6c 74 73 2e 0a 2a 2a 0a  the results..**.
349f1 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
349f2 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
349f3 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45  eturned and *ppE
349f4 78 70 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74  xpr set to point
349f5 20 61 74 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63   at the.** alloc
349f6 61 74 65 64 20 46 74 73 33 45 78 70 72 20 73 74  ated Fts3Expr st
349f7 72 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69  ructure. Otherwi
349f8 73 65 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54  se, either SQLIT
349f9 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74 20 6f 66 20  E_NOMEM (out of 
349fa 6d 65 6d 6f 72 79 0a 2a 2a 20 65 72 72 6f 72 29  memory.** error)
349fb 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   or SQLITE_ERROR
349fc 20 28 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 65   (tokenization e
349fd 72 72 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65  rror) is returne
349fe 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 73 65  d and *ppExpr se
349ff 74 0a 2a 2a 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74  t.** to 0..*/.st
34a00 61 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74  atic int getNext
34a01 53 74 72 69 6e 67 28 0a 20 20 50 61 72 73 65 43  String(.  ParseC
34a02 6f 6e 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20  ontext *pParse, 
34a03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34a04 20 20 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20    /* fts3 query 
34a05 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  parse context */
34a06 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
34a07 49 6e 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75  Input, int nInpu
34a08 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  t,         /* In
34a09 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
34a0a 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
34a0b 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
34a0c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
34a0d 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b  expression */.){
34a0e 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
34a0f 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
34a10 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 6f 6b 65   = pParse->pToke
34a11 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33  nizer;.  sqlite3
34a12 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
34a13 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65  e const *pModule
34a14 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70   = pTokenizer->p
34a15 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63  Module;.  int rc
34a16 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 20  ;.  Fts3Expr *p 
34a17 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  = 0;.  sqlite3_t
34a18 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
34a19 2a 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  *pCursor = 0;.  
34a1a 63 68 61 72 20 2a 7a 54 65 6d 70 20 3d 20 30 3b  char *zTemp = 0;
34a1b 0a 20 20 69 6e 74 20 6e 54 65 6d 70 20 3d 20 30  .  int nTemp = 0
34a1c 3b 0a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  ;..  rc = pModul
34a1d 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  e->xOpen(pTokeni
34a1e 7a 65 72 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  zer, zInput, nIn
34a1f 70 75 74 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a  put, &pCursor);.
34a20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
34a21 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  _OK ){.    int i
34a22 69 3b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e  i;.    pCursor->
34a23 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
34a24 6b 65 6e 69 7a 65 72 3b 0a 20 20 20 20 66 6f 72  kenizer;.    for
34a25 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  (ii=0; rc==SQLIT
34a26 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  E_OK; ii++){.   
34a27 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
34a28 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69 6e 74  Token;.      int
34a29 20 6e 54 6f 6b 65 6e 2c 20 69 42 65 67 69 6e 2c   nToken, iBegin,
34a2a 20 69 45 6e 64 2c 20 69 50 6f 73 3b 0a 20 20 20   iEnd, iPos;.   
34a2b 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d     rc = pModule-
34a2c 3e 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 20  >xNext(pCursor, 
34a2d 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e  &zToken, &nToken
34a2e 2c 20 26 69 42 65 67 69 6e 2c 20 26 69 45 6e 64  , &iBegin, &iEnd
34a2f 2c 20 26 69 50 6f 73 29 3b 0a 20 20 20 20 20 20  , &iPos);.      
34a30 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
34a31 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  K ){.        int
34a32 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
34a33 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65  Fts3Expr) + size
34a34 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 3b 0a  of(Fts3Phrase);.
34a35 20 20 20 20 20 20 20 20 70 20 3d 20 66 74 73 33          p = fts3
34a36 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2c  ReallocOrFree(p,
34a37 20 6e 42 79 74 65 2b 69 69 2a 73 69 7a 65 6f 66   nByte+ii*sizeof
34a38 28 73 74 72 75 63 74 20 50 68 72 61 73 65 54 6f  (struct PhraseTo
34a39 6b 65 6e 29 29 3b 0a 20 20 20 20 20 20 20 20 7a  ken));.        z
34a3a 54 65 6d 70 20 3d 20 66 74 73 33 52 65 61 6c 6c  Temp = fts3Reall
34a3b 6f 63 4f 72 46 72 65 65 28 7a 54 65 6d 70 2c 20  ocOrFree(zTemp, 
34a3c 6e 54 65 6d 70 20 2b 20 6e 54 6f 6b 65 6e 29 3b  nTemp + nToken);
34a3d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 20  .        if( !p 
34a3e 7c 7c 20 21 7a 54 65 6d 70 20 29 7b 0a 20 20 20  || !zTemp ){.   
34a3f 20 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d         goto no_m
34a40 65 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  em;.        }.  
34a41 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d 30 20        if( ii==0 
34a42 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  ){.          mem
34a43 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74 65 29  set(p, 0, nByte)
34a44 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  ;.          p->p
34a45 50 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68  Phrase = (Fts3Ph
34a46 72 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20  rase *)&p[1];.  
34a47 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
34a48 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74  p->pPhrase = (Ft
34a49 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d  s3Phrase *)&p[1]
34a4a 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68  ;.        p->pPh
34a4b 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20 69  rase->nToken = i
34a4c 69 2b 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  i+1;.        p->
34a4d 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
34a4e 69 69 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a  ii].n = nToken;.
34a4f 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
34a50 7a 54 65 6d 70 5b 6e 54 65 6d 70 5d 2c 20 7a 54  zTemp[nTemp], zT
34a51 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20  oken, nToken);. 
34a52 20 20 20 20 20 20 20 6e 54 65 6d 70 20 2b 3d 20         nTemp += 
34a53 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  nToken;.        
34a54 69 66 28 20 69 45 6e 64 3c 6e 49 6e 70 75 74 20  if( iEnd<nInput 
34a55 26 26 20 7a 49 6e 70 75 74 5b 69 45 6e 64 5d 3d  && zInput[iEnd]=
34a56 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
34a57 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54    p->pPhrase->aT
34a58 6f 6b 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69  oken[ii].isPrefi
34a59 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  x = 1;.        }
34a5a 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
34a5b 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  p->pPhrase->aTok
34a5c 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69 78 20  en[ii].isPrefix 
34a5d 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
34a5e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
34a5f 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73    pModule->xClos
34a60 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  e(pCursor);.    
34a61 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 7d  pCursor = 0;.  }
34a62 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
34a63 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 69  TE_DONE ){.    i
34a64 6e 74 20 6a 6a 3b 0a 20 20 20 20 63 68 61 72 20  nt jj;.    char 
34a65 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e  *zNew;.    int n
34a66 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  New = 0;.    int
34a67 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
34a68 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65  Fts3Expr) + size
34a69 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 3b 0a  of(Fts3Phrase);.
34a6a 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 28 70 3f      nByte += (p?
34a6b 28 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  (p->pPhrase->nTo
34a6c 6b 65 6e 2d 31 29 3a 30 29 20 2a 20 73 69 7a 65  ken-1):0) * size
34a6d 6f 66 28 73 74 72 75 63 74 20 50 68 72 61 73 65  of(struct Phrase
34a6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 70 20 3d 20  Token);.    p = 
34a6f 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65  fts3ReallocOrFre
34a70 65 28 70 2c 20 6e 42 79 74 65 20 2b 20 6e 54 65  e(p, nByte + nTe
34a71 6d 70 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20  mp);.    if( !p 
34a72 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
34a73 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
34a74 69 66 28 20 7a 54 65 6d 70 20 29 7b 0a 20 20 20  if( zTemp ){.   
34a75 20 20 20 7a 4e 65 77 20 3d 20 26 28 28 28 63 68     zNew = &(((ch
34a76 61 72 20 2a 29 70 29 5b 6e 42 79 74 65 5d 29 3b  ar *)p)[nByte]);
34a77 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e  .      memcpy(zN
34a78 65 77 2c 20 7a 54 65 6d 70 2c 20 6e 54 65 6d 70  ew, zTemp, nTemp
34a79 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
34a7a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c      memset(p, 0,
34a7b 20 6e 42 79 74 65 2b 6e 54 65 6d 70 29 3b 0a 20   nByte+nTemp);. 
34a7c 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50 68 72     }.    p->pPhr
34a7d 61 73 65 20 3d 20 28 46 74 73 33 50 68 72 61 73  ase = (Fts3Phras
34a7e 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 66  e *)&p[1];.    f
34a7f 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 70 2d 3e 70  or(jj=0; jj<p->p
34a80 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20  Phrase->nToken; 
34a81 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e  jj++){.      p->
34a82 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
34a83 6a 6a 5d 2e 7a 20 3d 20 26 7a 4e 65 77 5b 6e 4e  jj].z = &zNew[nN
34a84 65 77 5d 3b 0a 20 20 20 20 20 20 6e 4e 65 77 20  ew];.      nNew 
34a85 2b 3d 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61  += p->pPhrase->a
34a86 54 6f 6b 65 6e 5b 6a 6a 5d 2e 6e 3b 0a 20 20 20  Token[jj].n;.   
34a87 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
34a88 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20  ree(zTemp);.    
34a89 70 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55  p->eType = FTSQU
34a8a 45 52 59 5f 50 48 52 41 53 45 3b 0a 20 20 20 20  ERY_PHRASE;.    
34a8b 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c  p->pPhrase->iCol
34a8c 75 6d 6e 20 3d 20 70 50 61 72 73 65 2d 3e 69 44  umn = pParse->iD
34a8d 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 20 20 72  efaultCol;.    r
34a8e 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
34a8f 20 7d 0a 0a 20 20 2a 70 70 45 78 70 72 20 3d 20   }..  *ppExpr = 
34a90 70 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  p;.  return rc;.
34a91 6e 6f 5f 6d 65 6d 3a 0a 0a 20 20 69 66 28 20 70  no_mem:..  if( p
34a92 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 70 4d  Cursor ){.    pM
34a93 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43  odule->xClose(pC
34a94 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 73 71  ursor);.  }.  sq
34a95 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 6d 70  lite3_free(zTemp
34a96 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
34a97 65 28 70 29 3b 0a 20 20 2a 70 70 45 78 70 72 20  e(p);.  *ppExpr 
34a98 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  = 0;.  return SQ
34a99 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f  LITE_NOMEM;.}../
34a9a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 67 65  *.** Function ge
34a9b 74 4e 65 78 74 4e 6f 64 65 28 29 2c 20 77 68 69  tNextNode(), whi
34a9c 63 68 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  ch is called by 
34a9d 66 74 73 33 45 78 70 72 50 61 72 73 65 28 29 2c  fts3ExprParse(),
34a9e 20 6d 61 79 20 69 74 73 65 6c 66 0a 2a 2a 20 63   may itself.** c
34a9f 61 6c 6c 20 66 74 73 33 45 78 70 72 50 61 72 73  all fts3ExprPars
34aa0 65 28 29 2e 20 53 6f 20 74 68 69 73 20 66 6f 72  e(). So this for
34aa1 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
34aa2 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f   is required..*/
34aa3 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
34aa4 45 78 70 72 50 61 72 73 65 28 50 61 72 73 65 43  ExprParse(ParseC
34aa5 6f 6e 74 65 78 74 20 2a 2c 20 63 6f 6e 73 74 20  ontext *, const 
34aa6 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74 73  char *, int, Fts
34aa7 33 45 78 70 72 20 2a 2a 2c 20 69 6e 74 20 2a 29  3Expr **, int *)
34aa8 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 75 74  ;../*.** The out
34aa9 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a 70 70  put variable *pp
34aaa 45 78 70 72 20 69 73 20 70 6f 70 75 6c 61 74 65  Expr is populate
34aab 64 20 77 69 74 68 20 61 6e 20 61 6c 6c 6f 63 61  d with an alloca
34aac 74 65 64 20 46 74 73 33 45 78 70 72 20 0a 2a 2a  ted Fts3Expr .**
34aad 20 73 74 72 75 63 74 75 72 65 2c 20 6f 72 20 73   structure, or s
34aae 65 74 20 74 6f 20 30 20 69 66 20 74 68 65 20 65  et to 0 if the e
34aaf 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20  nd of the input 
34ab0 62 75 66 66 65 72 20 69 73 20 72 65 61 63 68 65  buffer is reache
34ab1 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73  d..**.** Returns
34ab2 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
34ab3 20 63 6f 64 65 2e 20 53 51 4c 49 54 45 5f 4f 4b   code. SQLITE_OK
34ab4 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   if everything w
34ab5 6f 72 6b 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d  orks, SQLITE_NOM
34ab6 45 4d 0a 2a 2a 20 69 66 20 61 20 6d 61 6c 6c 6f  EM.** if a mallo
34ab7 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73  c failure occurs
34ab8 2c 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f  , or SQLITE_ERRO
34ab9 52 20 69 66 20 61 20 70 61 72 73 65 20 65 72 72  R if a parse err
34aba 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
34abb 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  d..** If SQLITE_
34abc 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65  ERROR is returne
34abd 64 2c 20 70 43 6f 6e 74 65 78 74 20 69 73 20 70  d, pContext is p
34abe 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e  opulated with an
34abf 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
34ac0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
34ac1 74 4e 65 78 74 4e 6f 64 65 28 0a 20 20 50 61 72  tNextNode(.  Par
34ac2 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73  seContext *pPars
34ac3 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
34ac4 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65       /* fts3 que
34ac5 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  ry parse context
34ac6 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
34ac7 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20   *z, int n,     
34ac8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34ac9 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f   Input string */
34aca 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70  .  Fts3Expr **pp
34acb 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
34acc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
34acd 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  T: expression */
34ace 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d  .  int *pnConsum
34acf 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ed              
34ad0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
34ad1 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
34ad2 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29  es consumed */.)
34ad3 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
34ad4 20 73 74 72 75 63 74 20 46 74 73 33 4b 65 79 77   struct Fts3Keyw
34ad5 6f 72 64 20 7b 0a 20 20 20 20 63 68 61 72 20 7a  ord {.    char z
34ad6 5b 34 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  [4];            
34ad7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34ad8 2f 2a 20 4b 65 79 77 6f 72 64 20 74 65 78 74 20  /* Keyword text 
34ad9 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
34ada 63 68 61 72 20 6e 3b 20 20 20 20 20 20 20 20 20  char n;         
34adb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34adc 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6b 65  Length of the ke
34add 79 77 6f 72 64 20 2a 2f 0a 20 20 20 20 75 6e 73  yword */.    uns
34ade 69 67 6e 65 64 20 63 68 61 72 20 70 61 72 65 6e  igned char paren
34adf 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20  Only;           
34ae0 20 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69 64     /* Only valid
34ae1 20 69 6e 20 70 61 72 65 6e 20 6d 6f 64 65 20 2a   in paren mode *
34ae2 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
34ae3 68 61 72 20 65 54 79 70 65 3b 20 20 20 20 20 20  har eType;      
34ae4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b              /* K
34ae5 65 79 77 6f 72 64 20 63 6f 64 65 20 2a 2f 0a 20  eyword code */. 
34ae6 20 7d 20 61 4b 65 79 77 6f 72 64 5b 5d 20 3d 20   } aKeyword[] = 
34ae7 7b 0a 20 20 20 20 7b 20 22 4f 52 22 20 2c 20 20  {.    { "OR" ,  
34ae8 32 2c 20 30 2c 20 46 54 53 51 55 45 52 59 5f 4f  2, 0, FTSQUERY_O
34ae9 52 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 41 4e  R   },.    { "AN
34aea 44 22 2c 20 20 33 2c 20 31 2c 20 46 54 53 51 55  D",  3, 1, FTSQU
34aeb 45 52 59 5f 41 4e 44 20 20 7d 2c 0a 20 20 20 20  ERY_AND  },.    
34aec 7b 20 22 4e 4f 54 22 2c 20 20 33 2c 20 31 2c 20  { "NOT",  3, 1, 
34aed 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 20 7d 2c  FTSQUERY_NOT  },
34aee 0a 20 20 20 20 7b 20 22 4e 45 41 52 22 2c 20 34  .    { "NEAR", 4
34aef 2c 20 30 2c 20 46 54 53 51 55 45 52 59 5f 4e 45  , 0, FTSQUERY_NE
34af0 41 52 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20  AR }.  };.  int 
34af1 69 69 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a  ii;.  int iCol;.
34af2 20 20 69 6e 74 20 69 43 6f 6c 4c 65 6e 3b 0a 20    int iColLen;. 
34af3 20 69 6e 74 20 72 63 3b 0a 20 20 46 74 73 33 45   int rc;.  Fts3E
34af4 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a 0a  xpr *pRet = 0;..
34af5 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49    const char *zI
34af6 6e 70 75 74 20 3d 20 7a 3b 0a 20 20 69 6e 74 20  nput = z;.  int 
34af7 6e 49 6e 70 75 74 20 3d 20 6e 3b 0a 0a 20 20 2f  nInput = n;..  /
34af8 2a 20 53 6b 69 70 20 6f 76 65 72 20 61 6e 79 20  * Skip over any 
34af9 77 68 69 74 65 73 70 61 63 65 20 62 65 66 6f 72  whitespace befor
34afa 65 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 61  e checking for a
34afb 20 6b 65 79 77 6f 72 64 2c 20 61 6e 20 6f 70 65   keyword, an ope
34afc 6e 20 6f 72 0a 20 20 2a 2a 20 63 6c 6f 73 65 20  n or.  ** close 
34afd 62 72 61 63 6b 65 74 2c 20 6f 72 20 61 20 71 75  bracket, or a qu
34afe 6f 74 65 64 20 73 74 72 69 6e 67 2e 20 0a 20 20  oted string. .  
34aff 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 49 6e 70  */.  while( nInp
34b00 75 74 3e 30 20 26 26 20 66 74 73 33 69 73 73 70  ut>0 && fts3issp
34b01 61 63 65 28 2a 7a 49 6e 70 75 74 29 20 29 7b 0a  ace(*zInput) ){.
34b02 20 20 20 20 6e 49 6e 70 75 74 2d 2d 3b 0a 20 20      nInput--;.  
34b03 20 20 7a 49 6e 70 75 74 2b 2b 3b 0a 20 20 7d 0a    zInput++;.  }.
34b04 20 20 69 66 28 20 6e 49 6e 70 75 74 3d 3d 30 20    if( nInput==0 
34b05 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
34b06 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 0a  LITE_DONE;.  }..
34b07 20 20 2f 2a 20 53 65 65 20 69 66 20 77 65 20 61    /* See if we a
34b08 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
34b09 61 20 6b 65 79 77 6f 72 64 2e 20 2a 2f 0a 20 20  a keyword. */.  
34b0a 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 69 6e  for(ii=0; ii<(in
34b0b 74 29 28 73 69 7a 65 6f 66 28 61 4b 65 79 77 6f  t)(sizeof(aKeywo
34b0c 72 64 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63  rd)/sizeof(struc
34b0d 74 20 46 74 73 33 4b 65 79 77 6f 72 64 29 29 3b  t Fts3Keyword));
34b0e 20 69 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73   ii++){.    cons
34b0f 74 20 73 74 72 75 63 74 20 46 74 73 33 4b 65 79  t struct Fts3Key
34b10 77 6f 72 64 20 2a 70 4b 65 79 20 3d 20 26 61 4b  word *pKey = &aK
34b11 65 79 77 6f 72 64 5b 69 69 5d 3b 0a 0a 20 20 20  eyword[ii];..   
34b12 20 69 66 28 20 28 70 4b 65 79 2d 3e 70 61 72 65   if( (pKey->pare
34b13 6e 4f 6e 6c 79 20 26 20 7e 73 71 6c 69 74 65 33  nOnly & ~sqlite3
34b14 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
34b15 65 6e 74 68 65 73 65 73 29 21 3d 30 20 29 7b 0a  entheses)!=0 ){.
34b16 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
34b17 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 6e      }..    if( n
34b18 49 6e 70 75 74 3e 3d 70 4b 65 79 2d 3e 6e 20 26  Input>=pKey->n &
34b19 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 49 6e 70  & 0==memcmp(zInp
34b1a 75 74 2c 20 70 4b 65 79 2d 3e 7a 2c 20 70 4b 65  ut, pKey->z, pKe
34b1b 79 2d 3e 6e 29 20 29 7b 0a 20 20 20 20 20 20 69  y->n) ){.      i
34b1c 6e 74 20 6e 4e 65 61 72 20 3d 20 53 51 4c 49 54  nt nNear = SQLIT
34b1d 45 5f 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e  E_FTS3_DEFAULT_N
34b1e 45 41 52 5f 50 41 52 41 4d 3b 0a 20 20 20 20 20  EAR_PARAM;.     
34b1f 20 69 6e 74 20 6e 4b 65 79 20 3d 20 70 4b 65 79   int nKey = pKey
34b20 2d 3e 6e 3b 0a 20 20 20 20 20 20 63 68 61 72 20  ->n;.      char 
34b21 63 4e 65 78 74 3b 0a 0a 20 20 20 20 20 20 2f 2a  cNext;..      /*
34b22 20 49 66 20 74 68 69 73 20 69 73 20 61 20 22 4e   If this is a "N
34b23 45 41 52 22 20 6b 65 79 77 6f 72 64 2c 20 63 68  EAR" keyword, ch
34b24 65 63 6b 20 66 6f 72 20 61 6e 20 65 78 70 6c 69  eck for an expli
34b25 63 69 74 20 6e 65 61 72 6e 65 73 73 2e 20 2a 2f  cit nearness. */
34b26 0a 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 2d  .      if( pKey-
34b27 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
34b28 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20 20  _NEAR ){.       
34b29 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 34   assert( nKey==4
34b2a 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
34b2b 7a 49 6e 70 75 74 5b 34 5d 3d 3d 27 2f 27 20 26  zInput[4]=='/' &
34b2c 26 20 7a 49 6e 70 75 74 5b 35 5d 3e 3d 27 30 27  & zInput[5]>='0'
34b2d 20 26 26 20 7a 49 6e 70 75 74 5b 35 5d 3c 3d 27   && zInput[5]<='
34b2e 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  9' ){.          
34b2f 6e 4e 65 61 72 20 3d 20 30 3b 0a 20 20 20 20 20  nNear = 0;.     
34b30 20 20 20 20 20 66 6f 72 28 6e 4b 65 79 3d 35 3b       for(nKey=5;
34b31 20 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3e 3d 27   zInput[nKey]>='
34b32 30 27 20 26 26 20 7a 49 6e 70 75 74 5b 6e 4b 65  0' && zInput[nKe
34b33 79 5d 3c 3d 27 39 27 3b 20 6e 4b 65 79 2b 2b 29  y]<='9'; nKey++)
34b34 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e  {.            nN
34b35 65 61 72 20 3d 20 6e 4e 65 61 72 20 2a 20 31 30  ear = nNear * 10
34b36 20 2b 20 28 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d   + (zInput[nKey]
34b37 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 20   - '0');.       
34b38 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
34b39 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
34b3a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
34b3b 68 69 73 20 69 73 20 70 72 6f 62 61 62 6c 79 20  his is probably 
34b3c 61 20 6b 65 79 77 6f 72 64 2e 20 42 75 74 20 66  a keyword. But f
34b3d 6f 72 20 74 68 61 74 20 74 6f 20 62 65 20 74 72  or that to be tr
34b3e 75 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  ue,.      ** the
34b3f 20 6e 65 78 74 20 62 79 74 65 20 6d 75 73 74 20   next byte must 
34b40 63 6f 6e 74 61 69 6e 20 65 69 74 68 65 72 20 77  contain either w
34b41 68 69 74 65 73 70 61 63 65 2c 20 61 6e 20 6f 70  hitespace, an op
34b42 65 6e 20 6f 72 20 63 6c 6f 73 65 0a 20 20 20 20  en or close.    
34b43 20 20 2a 2a 20 70 61 72 65 6e 74 68 65 73 69 73    ** parenthesis
34b44 2c 20 61 20 71 75 6f 74 65 20 63 68 61 72 61 63  , a quote charac
34b45 74 65 72 2c 20 6f 72 20 45 4f 46 2e 20 0a 20 20  ter, or EOF. .  
34b46 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 4e 65      */.      cNe
34b47 78 74 20 3d 20 7a 49 6e 70 75 74 5b 6e 4b 65 79  xt = zInput[nKey
34b48 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 66 74 73  ];.      if( fts
34b49 33 69 73 73 70 61 63 65 28 63 4e 65 78 74 29 20  3isspace(cNext) 
34b4a 0a 20 20 20 20 20 20 20 7c 7c 20 63 4e 65 78 74  .       || cNext
34b4b 3d 3d 27 22 27 20 7c 7c 20 63 4e 65 78 74 3d 3d  =='"' || cNext==
34b4c 27 28 27 20 7c 7c 20 63 4e 65 78 74 3d 3d 27 29  '(' || cNext==')
34b4d 27 20 7c 7c 20 63 4e 65 78 74 3d 3d 30 0a 20 20  ' || cNext==0.  
34b4e 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70      ){.        p
34b4f 52 65 74 20 3d 20 28 46 74 73 33 45 78 70 72 20  Ret = (Fts3Expr 
34b50 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
34b51 28 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72  (sizeof(Fts3Expr
34b52 29 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  ));.        mems
34b53 65 74 28 70 52 65 74 2c 20 30 2c 20 73 69 7a 65  et(pRet, 0, size
34b54 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20  of(Fts3Expr));. 
34b55 20 20 20 20 20 20 20 70 52 65 74 2d 3e 65 54 79         pRet->eTy
34b56 70 65 20 3d 20 70 4b 65 79 2d 3e 65 54 79 70 65  pe = pKey->eType
34b57 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ;.        pRet->
34b58 6e 4e 65 61 72 20 3d 20 6e 4e 65 61 72 3b 0a 20  nNear = nNear;. 
34b59 20 20 20 20 20 20 20 2a 70 70 45 78 70 72 20 3d         *ppExpr =
34b5a 20 70 52 65 74 3b 0a 20 20 20 20 20 20 20 20 2a   pRet;.        *
34b5b 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49  pnConsumed = (zI
34b5c 6e 70 75 74 20 2d 20 7a 29 20 2b 20 6e 4b 65 79  nput - z) + nKey
34b5d 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
34b5e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
34b5f 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 75    }..      /* Tu
34b60 72 6e 73 20 6f 75 74 20 74 68 61 74 20 77 61 73  rns out that was
34b61 6e 27 74 20 61 20 6b 65 79 77 6f 72 64 20 61 66  n't a keyword af
34b62 74 65 72 20 61 6c 6c 2e 20 54 68 69 73 20 68 61  ter all. This ha
34b63 70 70 65 6e 73 20 69 66 20 74 68 65 0a 20 20 20  ppens if the.   
34b64 20 20 20 2a 2a 20 75 73 65 72 20 68 61 73 20 73     ** user has s
34b65 75 70 70 6c 69 65 64 20 61 20 74 6f 6b 65 6e 20  upplied a token 
34b66 73 75 63 68 20 61 73 20 22 4f 52 61 63 6c 65 22  such as "ORacle"
34b67 2e 20 43 6f 6e 74 69 6e 75 65 2e 0a 20 20 20 20  . Continue..    
34b68 20 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a    */.    }.  }..
34b69 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61    /* Check for a
34b6a 6e 20 6f 70 65 6e 20 62 72 61 63 6b 65 74 2e 20  n open bracket. 
34b6b 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
34b6c 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
34b6d 65 6e 74 68 65 73 65 73 20 29 7b 0a 20 20 20 20  entheses ){.    
34b6e 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d 27 28 27  if( *zInput=='('
34b6f 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43   ){.      int nC
34b70 6f 6e 73 75 6d 65 64 3b 0a 20 20 20 20 20 20 69  onsumed;.      i
34b71 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 70 50 61  nt rc;.      pPa
34b72 72 73 65 2d 3e 6e 4e 65 73 74 2b 2b 3b 0a 20 20  rse->nNest++;.  
34b73 20 20 20 20 72 63 20 3d 20 66 74 73 33 45 78 70      rc = fts3Exp
34b74 72 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 26  rParse(pParse, &
34b75 7a 49 6e 70 75 74 5b 31 5d 2c 20 6e 49 6e 70 75  zInput[1], nInpu
34b76 74 2d 31 2c 20 70 70 45 78 70 72 2c 20 26 6e 43  t-1, ppExpr, &nC
34b77 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 20 20 20 20  onsumed);.      
34b78 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
34b79 4b 20 26 26 20 21 2a 70 70 45 78 70 72 20 29 7b  K && !*ppExpr ){
34b7a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
34b7b 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20  LITE_DONE;.     
34b7c 20 7d 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73   }.      *pnCons
34b7d 75 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d  umed = (zInput -
34b7e 20 7a 29 20 2b 20 31 20 2b 20 6e 43 6f 6e 73 75   z) + 1 + nConsu
34b7f 6d 65 64 3b 0a 20 20 20 20 20 20 72 65 74 75 72  med;.      retur
34b80 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  n rc;.    }.  . 
34b81 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20     /* Check for 
34b82 61 20 63 6c 6f 73 65 20 62 72 61 63 6b 65 74 2e  a close bracket.
34b83 20 2a 2f 0a 20 20 20 20 69 66 28 20 2a 7a 49 6e   */.    if( *zIn
34b84 70 75 74 3d 3d 27 29 27 20 29 7b 0a 20 20 20 20  put==')' ){.    
34b85 20 20 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2d    pParse->nNest-
34b86 2d 3b 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73  -;.      *pnCons
34b87 75 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d  umed = (zInput -
34b88 20 7a 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 72   z) + 1;.      r
34b89 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
34b8a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  E;.    }.  }..  
34b8b 2f 2a 20 53 65 65 20 69 66 20 77 65 20 61 72 65  /* See if we are
34b8c 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20   dealing with a 
34b8d 71 75 6f 74 65 64 20 70 68 72 61 73 65 2e 20 49  quoted phrase. I
34b8e 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
34b8f 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 73 65  se, then.  ** se
34b90 61 72 63 68 20 66 6f 72 20 74 68 65 20 63 6c 6f  arch for the clo
34b91 73 69 6e 67 20 71 75 6f 74 65 20 61 6e 64 20 70  sing quote and p
34b92 61 73 73 20 74 68 65 20 77 68 6f 6c 65 20 73 74  ass the whole st
34b93 72 69 6e 67 20 74 6f 20 67 65 74 4e 65 78 74 53  ring to getNextS
34b94 74 72 69 6e 67 28 29 0a 20 20 2a 2a 20 66 6f 72  tring().  ** for
34b95 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 54 68 69   processing. Thi
34b96 73 20 69 73 20 65 61 73 79 20 74 6f 20 64 6f 2c  s is easy to do,
34b97 20 61 73 20 66 74 73 33 20 68 61 73 20 6e 6f 20   as fts3 has no 
34b98 73 79 6e 74 61 78 20 66 6f 72 20 65 73 63 61 70  syntax for escap
34b99 69 6e 67 0a 20 20 2a 2a 20 61 20 71 75 6f 74 65  ing.  ** a quote
34b9a 20 63 68 61 72 61 63 74 65 72 20 65 6d 62 65 64   character embed
34b9b 64 65 64 20 69 6e 20 61 20 73 74 72 69 6e 67 2e  ded in a string.
34b9c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 49 6e  .  */.  if( *zIn
34b9d 70 75 74 3d 3d 27 22 27 20 29 7b 0a 20 20 20 20  put=='"' ){.    
34b9e 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 6e 49 6e  for(ii=1; ii<nIn
34b9f 70 75 74 20 26 26 20 7a 49 6e 70 75 74 5b 69 69  put && zInput[ii
34ba0 5d 21 3d 27 22 27 3b 20 69 69 2b 2b 29 3b 0a 20  ]!='"'; ii++);. 
34ba1 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d     *pnConsumed =
34ba2 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20   (zInput - z) + 
34ba3 69 69 20 2b 20 31 3b 0a 20 20 20 20 69 66 28 20  ii + 1;.    if( 
34ba4 69 69 3d 3d 6e 49 6e 70 75 74 20 29 7b 0a 20 20  ii==nInput ){.  
34ba5 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
34ba6 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
34ba7 20 20 20 72 65 74 75 72 6e 20 67 65 74 4e 65 78     return getNex
34ba8 74 53 74 72 69 6e 67 28 70 50 61 72 73 65 2c 20  tString(pParse, 
34ba9 26 7a 49 6e 70 75 74 5b 31 5d 2c 20 69 69 2d 31  &zInput[1], ii-1
34baa 2c 20 70 70 45 78 70 72 29 3b 0a 20 20 7d 0a 0a  , ppExpr);.  }..
34bab 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c  .  /* If control
34bac 20 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70   flows to this p
34bad 6f 69 6e 74 2c 20 74 68 69 73 20 6d 75 73 74 20  oint, this must 
34bae 62 65 20 61 20 72 65 67 75 6c 61 72 20 74 6f 6b  be a regular tok
34baf 65 6e 2c 20 6f 72 20 0a 20 20 2a 2a 20 74 68 65  en, or .  ** the
34bb0 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75   end of the inpu
34bb1 74 2e 20 52 65 61 64 20 61 20 72 65 67 75 6c 61  t. Read a regula
34bb2 72 20 74 6f 6b 65 6e 20 75 73 69 6e 67 20 74 68  r token using th
34bb3 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e sqlite3_tokeni
34bb4 7a 65 72 0a 20 20 2a 2a 20 69 6e 74 65 72 66 61  zer.  ** interfa
34bb5 63 65 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67  ce. Before doing
34bb6 20 73 6f 2c 20 66 69 67 75 72 65 20 6f 75 74 20   so, figure out 
34bb7 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 65  if there is an e
34bb8 78 70 6c 69 63 69 74 0a 20 20 2a 2a 20 63 6f 6c  xplicit.  ** col
34bb9 75 6d 6e 20 73 70 65 63 69 66 69 65 72 20 66 6f  umn specifier fo
34bba 72 20 74 68 65 20 74 6f 6b 65 6e 2e 20 0a 20 20  r the token. .  
34bbb 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 53 74  **.  ** TODO: St
34bbc 72 61 6e 67 65 6c 79 2c 20 69 74 20 69 73 20 6e  rangely, it is n
34bbd 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61  ot possible to a
34bbe 73 73 6f 63 69 61 74 65 20 61 20 63 6f 6c 75 6d  ssociate a colum
34bbf 6e 20 73 70 65 63 69 66 69 65 72 0a 20 20 2a 2a  n specifier.  **
34bc0 20 77 69 74 68 20 61 20 71 75 6f 74 65 64 20 70   with a quoted p
34bc1 68 72 61 73 65 2c 20 6f 6e 6c 79 20 77 69 74 68  hrase, only with
34bc2 20 61 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 2e   a single token.
34bc3 20 4e 6f 74 20 73 75 72 65 20 69 66 20 74 68 69   Not sure if thi
34bc4 73 20 77 61 73 0a 20 20 2a 2a 20 61 6e 20 69 6d  s was.  ** an im
34bc5 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 72 74  plementation art
34bc6 69 66 61 63 74 20 6f 72 20 61 6e 20 69 6e 74 65  ifact or an inte
34bc7 6e 74 69 6f 6e 61 6c 20 64 65 63 69 73 69 6f 6e  ntional decision
34bc8 20 77 68 65 6e 20 66 74 73 33 20 77 61 73 0a 20   when fts3 was. 
34bc9 20 2a 2a 20 66 69 72 73 74 20 69 6d 70 6c 65 6d   ** first implem
34bca 65 6e 74 65 64 2e 20 57 68 69 63 68 65 76 65 72  ented. Whichever
34bcb 20 69 74 20 77 61 73 2c 20 74 68 69 73 20 6d 6f   it was, this mo
34bcc 64 75 6c 65 20 64 75 70 6c 69 63 61 74 65 73 20  dule duplicates 
34bcd 74 68 65 20 0a 20 20 2a 2a 20 6c 69 6d 69 74 61  the .  ** limita
34bce 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 43 6f  tion..  */.  iCo
34bcf 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 44 65 66  l = pParse->iDef
34bd0 61 75 6c 74 43 6f 6c 3b 0a 20 20 69 43 6f 6c 4c  aultCol;.  iColL
34bd1 65 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69  en = 0;.  for(ii
34bd2 3d 30 3b 20 69 69 3c 70 50 61 72 73 65 2d 3e 6e  =0; ii<pParse->n
34bd3 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  Col; ii++){.    
34bd4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 72  const char *zStr
34bd5 20 3d 20 70 50 61 72 73 65 2d 3e 61 7a 43 6f 6c   = pParse->azCol
34bd6 5b 69 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 53  [ii];.    int nS
34bd7 74 72 20 3d 20 73 74 72 6c 65 6e 28 7a 53 74 72  tr = strlen(zStr
34bd8 29 3b 0a 20 20 20 20 69 66 28 20 6e 49 6e 70 75  );.    if( nInpu
34bd9 74 3e 6e 53 74 72 20 26 26 20 7a 49 6e 70 75 74  t>nStr && zInput
34bda 5b 6e 53 74 72 5d 3d 3d 27 3a 27 20 26 26 20 6d  [nStr]==':' && m
34bdb 65 6d 63 6d 70 28 7a 53 74 72 2c 20 7a 49 6e 70  emcmp(zStr, zInp
34bdc 75 74 2c 20 6e 53 74 72 29 3d 3d 30 20 29 7b 0a  ut, nStr)==0 ){.
34bdd 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 69 69 3b        iCol = ii;
34bde 0a 20 20 20 20 20 20 69 43 6f 6c 4c 65 6e 20 3d  .      iColLen =
34bdf 20 28 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b   ((zInput - z) +
34be0 20 6e 53 74 72 20 2b 20 31 29 3b 0a 20 20 20 20   nStr + 1);.    
34be1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
34be2 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 4e 65 78   }.  rc = getNex
34be3 74 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c 20 69  tToken(pParse, i
34be4 43 6f 6c 2c 20 26 7a 5b 69 43 6f 6c 4c 65 6e 5d  Col, &z[iColLen]
34be5 2c 20 6e 2d 69 43 6f 6c 4c 65 6e 2c 20 70 70 45  , n-iColLen, ppE
34be6 78 70 72 2c 20 70 6e 43 6f 6e 73 75 6d 65 64 29  xpr, pnConsumed)
34be7 3b 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20  ;.  *pnConsumed 
34be8 2b 3d 20 69 43 6f 6c 4c 65 6e 3b 0a 20 20 72 65  += iColLen;.  re
34be9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
34bea 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
34beb 73 20 61 6e 20 46 74 73 33 45 78 70 72 20 73 74  s an Fts3Expr st
34bec 72 75 63 74 75 72 65 20 66 6f 72 20 61 20 62 69  ructure for a bi
34bed 6e 61 72 79 20 6f 70 65 72 61 74 6f 72 20 28 61  nary operator (a
34bee 6e 79 20 74 79 70 65 0a 2a 2a 20 65 78 63 65 70  ny type.** excep
34bef 74 20 61 6e 20 46 54 53 51 55 45 52 59 5f 50 48  t an FTSQUERY_PH
34bf0 52 41 53 45 29 2e 20 52 65 74 75 72 6e 20 61 6e  RASE). Return an
34bf1 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 72   integer value r
34bf2 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 0a  epresenting the.
34bf3 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20 6f 66  ** precedence of
34bf4 20 74 68 65 20 6f 70 65 72 61 74 6f 72 2e 20 4c   the operator. L
34bf5 6f 77 65 72 20 76 61 6c 75 65 73 20 68 61 76 65  ower values have
34bf6 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65 64   a higher preced
34bf7 65 6e 63 65 20 28 69 2e 65 2e 0a 2a 2a 20 67 72  ence (i.e..** gr
34bf8 6f 75 70 20 6d 6f 72 65 20 74 69 67 68 74 6c 79  oup more tightly
34bf9 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ). For example, 
34bfa 69 6e 20 74 68 65 20 43 20 6c 61 6e 67 75 61 67  in the C languag
34bfb 65 2c 20 74 68 65 20 3d 3d 20 6f 70 65 72 61 74  e, the == operat
34bfc 6f 72 0a 2a 2a 20 67 72 6f 75 70 73 20 6d 6f 72  or.** groups mor
34bfd 65 20 74 69 67 68 74 6c 79 20 74 68 61 6e 20 7c  e tightly than |
34bfe 7c 2c 20 61 6e 64 20 77 6f 75 6c 64 20 74 68 65  |, and would the
34bff 72 65 66 6f 72 65 20 68 61 76 65 20 61 20 68 69  refore have a hi
34c00 67 68 65 72 20 70 72 65 63 65 64 65 6e 63 65 2e  gher precedence.
34c01 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 75 73 69 6e  .**.** When usin
34c02 67 20 74 68 65 20 6e 65 77 20 66 74 73 33 20 71  g the new fts3 q
34c03 75 65 72 79 20 73 79 6e 74 61 78 20 28 77 68 65  uery syntax (whe
34c04 6e 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  n SQLITE_ENABLE_
34c05 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53 49 53  FTS3_PARENTHESIS
34c06 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 29 2c  .** is defined),
34c07 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68   the order of th
34c08 65 20 6f 70 65 72 61 74 6f 72 73 20 69 6e 20 70  e operators in p
34c09 72 65 63 65 64 65 6e 63 65 20 66 72 6f 6d 20 68  recedence from h
34c0a 69 67 68 65 73 74 20 74 6f 0a 2a 2a 20 6c 6f 77  ighest to.** low
34c0b 65 73 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  est is:.**.**   
34c0c 4e 45 41 52 0a 2a 2a 20 20 20 4e 4f 54 0a 2a 2a  NEAR.**   NOT.**
34c0d 20 20 20 41 4e 44 20 28 69 6e 63 6c 75 64 69 6e     AND (includin
34c0e 67 20 69 6d 70 6c 69 63 69 74 20 41 4e 44 73 29  g implicit ANDs)
34c0f 0a 2a 2a 20 20 20 4f 52 0a 2a 2a 0a 2a 2a 20 4e  .**   OR.**.** N
34c10 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 75 73  ote that when us
34c11 69 6e 67 20 74 68 65 20 6f 6c 64 20 71 75 65 72  ing the old quer
34c12 79 20 73 79 6e 74 61 78 2c 20 74 68 65 20 4f 52  y syntax, the OR
34c13 20 6f 70 65 72 61 74 6f 72 20 68 61 73 20 61 20   operator has a 
34c14 68 69 67 68 65 72 0a 2a 2a 20 70 72 65 63 65 64  higher.** preced
34c15 65 6e 63 65 20 74 68 61 6e 20 74 68 65 20 41 4e  ence than the AN
34c16 44 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73  D operator..*/.s
34c17 74 61 74 69 63 20 69 6e 74 20 6f 70 50 72 65 63  tatic int opPrec
34c18 65 64 65 6e 63 65 28 46 74 73 33 45 78 70 72 20  edence(Fts3Expr 
34c19 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
34c1a 2d 3e 65 54 79 70 65 21 3d 46 54 53 51 55 45 52  ->eType!=FTSQUER
34c1b 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 69 66  Y_PHRASE );.  if
34c1c 28 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65  ( sqlite3_fts3_e
34c1d 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
34c1e 73 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  s ){.    return 
34c1f 70 2d 3e 65 54 79 70 65 3b 0a 20 20 7d 65 6c 73  p->eType;.  }els
34c20 65 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d  e if( p->eType==
34c21 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b  FTSQUERY_NEAR ){
34c22 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
34c23 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 54   }else if( p->eT
34c24 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4f 52  ype==FTSQUERY_OR
34c25 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   ){.    return 2
34c26 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
34c27 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
34c28 52 59 5f 41 4e 44 20 29 3b 0a 20 20 72 65 74 75  RY_AND );.  retu
34c29 72 6e 20 33 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn 3;.}../*.** A
34c2a 72 67 75 6d 65 6e 74 20 70 70 48 65 61 64 20 63  rgument ppHead c
34c2b 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
34c2c 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  r to the current
34c2d 20 68 65 61 64 20 6f 66 20 61 20 71 75 65 72 79   head of a query
34c2e 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20   .** expression 
34c2f 74 72 65 65 20 62 65 69 6e 67 20 70 61 72 73 65  tree being parse
34c30 64 2e 20 70 50 72 65 76 20 69 73 20 74 68 65 20  d. pPrev is the 
34c31 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
34c32 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a  most recently.**
34c33 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
34c34 68 65 20 74 72 65 65 2e 20 54 68 69 73 20 66 75  he tree. This fu
34c35 6e 63 74 69 6f 6e 20 61 64 64 73 20 70 4e 65 77  nction adds pNew
34c36 2c 20 77 68 69 63 68 20 69 73 20 61 6c 77 61 79  , which is alway
34c37 73 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 6f 70  s a binary.** op
34c38 65 72 61 74 6f 72 20 6e 6f 64 65 2c 20 69 6e 74  erator node, int
34c39 6f 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  o the expression
34c3a 20 74 72 65 65 20 62 61 73 65 64 20 6f 6e 20 74   tree based on t
34c3b 68 65 20 72 65 6c 61 74 69 76 65 20 70 72 65 63  he relative prec
34c3c 65 64 65 6e 63 65 0a 2a 2a 20 6f 66 20 70 4e 65  edence.** of pNe
34c3d 77 20 61 6e 64 20 74 68 65 20 65 78 69 73 74 69  w and the existi
34c3e 6e 67 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 20  ng nodes of the 
34c3f 74 72 65 65 2e 20 54 68 69 73 20 6d 61 79 20 72  tree. This may r
34c40 65 73 75 6c 74 20 69 6e 20 74 68 65 20 68 65 61  esult in the hea
34c41 64 0a 2a 2a 20 6f 66 20 74 68 65 20 74 72 65 65  d.** of the tree
34c42 20 63 68 61 6e 67 69 6e 67 2c 20 69 6e 20 77 68   changing, in wh
34c43 69 63 68 20 63 61 73 65 20 2a 70 70 48 65 61 64  ich case *ppHead
34c44 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
34c45 65 77 20 72 6f 6f 74 20 6e 6f 64 65 2e 0a 2a 2f  ew root node..*/
34c46 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73  .static void ins
34c47 65 72 74 42 69 6e 61 72 79 4f 70 65 72 61 74 6f  ertBinaryOperato
34c48 72 28 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a  r(.  Fts3Expr **
34c49 70 70 48 65 61 64 2c 20 20 20 20 20 20 20 2f 2a  ppHead,       /*
34c4a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
34c4b 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 61 20 74  root node of a t
34c4c 72 65 65 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  ree */.  Fts3Exp
34c4d 72 20 2a 70 50 72 65 76 2c 20 20 20 20 20 20 20  r *pPrev,       
34c4e 20 20 2f 2a 20 4e 6f 64 65 20 6d 6f 73 74 20 72    /* Node most r
34c4f 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64  ecently inserted
34c50 20 69 6e 74 6f 20 74 68 65 20 74 72 65 65 20 2a   into the tree *
34c51 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 4e  /.  Fts3Expr *pN
34c52 65 77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ew           /* 
34c53 4e 65 77 20 62 69 6e 61 72 79 20 6e 6f 64 65 20  New binary node 
34c54 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 65  to insert into e
34c55 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 2a  xpression tree *
34c56 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  /.){.  Fts3Expr 
34c57 2a 70 53 70 6c 69 74 20 3d 20 70 50 72 65 76 3b  *pSplit = pPrev;
34c58 0a 20 20 77 68 69 6c 65 28 20 70 53 70 6c 69 74  .  while( pSplit
34c59 2d 3e 70 50 61 72 65 6e 74 20 26 26 20 6f 70 50  ->pParent && opP
34c5a 72 65 63 65 64 65 6e 63 65 28 70 53 70 6c 69 74  recedence(pSplit
34c5b 2d 3e 70 50 61 72 65 6e 74 29 3c 3d 6f 70 50 72  ->pParent)<=opPr
34c5c 65 63 65 64 65 6e 63 65 28 70 4e 65 77 29 20 29  ecedence(pNew) )
34c5d 7b 0a 20 20 20 20 70 53 70 6c 69 74 20 3d 20 70  {.    pSplit = p
34c5e 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b 0a  Split->pParent;.
34c5f 20 20 7d 0a 0a 20 20 69 66 28 20 70 53 70 6c 69    }..  if( pSpli
34c60 74 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20  t->pParent ){.  
34c61 20 20 61 73 73 65 72 74 28 20 70 53 70 6c 69 74    assert( pSplit
34c62 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68  ->pParent->pRigh
34c63 74 3d 3d 70 53 70 6c 69 74 20 29 3b 0a 20 20 20  t==pSplit );.   
34c64 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74   pSplit->pParent
34c65 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e 65 77 3b  ->pRight = pNew;
34c66 0a 20 20 20 20 70 4e 65 77 2d 3e 70 50 61 72 65  .    pNew->pPare
34c67 6e 74 20 3d 20 70 53 70 6c 69 74 2d 3e 70 50 61  nt = pSplit->pPa
34c68 72 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rent;.  }else{. 
34c69 20 20 20 2a 70 70 48 65 61 64 20 3d 20 70 4e 65     *ppHead = pNe
34c6a 77 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70  w;.  }.  pNew->p
34c6b 4c 65 66 74 20 3d 20 70 53 70 6c 69 74 3b 0a 20  Left = pSplit;. 
34c6c 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74   pSplit->pParent
34c6d 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a   = pNew;.}../*.*
34c6e 2a 20 50 61 72 73 65 20 74 68 65 20 66 74 73 33  * Parse the fts3
34c6f 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
34c70 6e 20 66 6f 75 6e 64 20 69 6e 20 62 75 66 66 65  n found in buffe
34c71 72 20 7a 2c 20 6c 65 6e 67 74 68 20 6e 2e 20 54  r z, length n. T
34c72 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
34c73 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 20 77  returns either w
34c74 68 65 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 74  hen the end of t
34c75 68 65 20 62 75 66 66 65 72 20 69 73 20 72 65 61  he buffer is rea
34c76 63 68 65 64 20 6f 72 20 61 6e 20 75 6e 6d 61 74  ched or an unmat
34c77 63 68 65 64 20 0a 2a 2a 20 63 6c 6f 73 69 6e 67  ched .** closing
34c78 20 62 72 61 63 6b 65 74 20 2d 20 27 29 27 20 2d   bracket - ')' -
34c79 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e   is encountered.
34c7a 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
34c7b 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  sful, SQLITE_OK 
34c7c 69 73 20 72 65 74 75 72 6e 65 64 2c 20 2a 70 70  is returned, *pp
34c7d 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 70  Expr is set to p
34c7e 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 70  oint to the.** p
34c7f 61 72 73 65 64 20 66 6f 72 6d 20 6f 66 20 74 68  arsed form of th
34c80 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  e expression and
34c81 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 69 73 20   *pnConsumed is 
34c82 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
34c83 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 72 65  r of.** bytes re
34c84 61 64 20 66 72 6f 6d 20 62 75 66 66 65 72 20 7a  ad from buffer z
34c85 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 2a 70 70  . Otherwise, *pp
34c86 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 30  Expr is set to 0
34c87 20 61 6e 64 20 53 51 4c 49 54 45 5f 4e 4f 4d 45   and SQLITE_NOME
34c88 4d 0a 2a 2a 20 28 6f 75 74 20 6f 66 20 6d 65 6d  M.** (out of mem
34c89 6f 72 79 20 65 72 72 6f 72 29 20 6f 72 20 53 51  ory error) or SQ
34c8a 4c 49 54 45 5f 45 52 52 4f 52 20 28 70 61 72 73  LITE_ERROR (pars
34c8b 65 20 65 72 72 6f 72 29 20 69 73 20 72 65 74 75  e error) is retu
34c8c 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
34c8d 69 6e 74 20 66 74 73 33 45 78 70 72 50 61 72 73  int fts3ExprPars
34c8e 65 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  e(.  ParseContex
34c8f 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  t *pParse,      
34c90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34c91 66 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65  fts3 query parse
34c92 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
34c93 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
34c94 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
34c95 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66        /* Text of
34c96 20 4d 41 54 43 48 20 71 75 65 72 79 20 2a 2f 0a   MATCH query */.
34c97 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45    Fts3Expr **ppE
34c98 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  xpr,            
34c99 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
34c9a 3a 20 50 61 72 73 65 64 20 71 75 65 72 79 20 73  : Parsed query s
34c9b 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e  tructure */.  in
34c9c 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20  t *pnConsumed   
34c9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34c9e 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
34c9f 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f  mber of bytes co
34ca0 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 46  nsumed */.){.  F
34ca1 74 73 33 45 78 70 72 20 2a 70 52 65 74 20 3d 20  ts3Expr *pRet = 
34ca2 30 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  0;.  Fts3Expr *p
34ca3 50 72 65 76 20 3d 20 30 3b 0a 20 20 46 74 73 33  Prev = 0;.  Fts3
34ca4 45 78 70 72 20 2a 70 4e 6f 74 42 72 61 6e 63 68  Expr *pNotBranch
34ca5 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
34ca6 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 75 73 65 64      /* Only used
34ca7 20 69 6e 20 6c 65 67 61 63 79 20 70 61 72 73 65   in legacy parse
34ca8 20 6d 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   mode */.  int n
34ca9 49 6e 20 3d 20 6e 3b 0a 20 20 63 6f 6e 73 74 20  In = n;.  const 
34caa 63 68 61 72 20 2a 7a 49 6e 20 3d 20 7a 3b 0a 20  char *zIn = z;. 
34cab 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
34cac 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 73 52 65 71  _OK;.  int isReq
34cad 75 69 72 65 50 68 72 61 73 65 20 3d 20 31 3b 0a  uirePhrase = 1;.
34cae 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
34caf 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 46  LITE_OK ){.    F
34cb0 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a  ts3Expr *p = 0;.
34cb1 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20      int nByte = 
34cb2 30 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 4e  0;.    rc = getN
34cb3 65 78 74 4e 6f 64 65 28 70 50 61 72 73 65 2c 20  extNode(pParse, 
34cb4 7a 49 6e 2c 20 6e 49 6e 2c 20 26 70 2c 20 26 6e  zIn, nIn, &p, &n
34cb5 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Byte);.    if( r
34cb6 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
34cb7 20 20 20 20 20 20 69 6e 74 20 69 73 50 68 72 61        int isPhra
34cb8 73 65 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 21  se;..      if( !
34cb9 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61  sqlite3_fts3_ena
34cba 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20  ble_parentheses 
34cbb 0a 20 20 20 20 20 20 20 26 26 20 70 2d 3e 65 54  .       && p->eT
34cbc 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
34cbd 52 41 53 45 20 26 26 20 70 2d 3e 70 50 68 72 61  RASE && p->pPhra
34cbe 73 65 2d 3e 69 73 4e 6f 74 20 0a 20 20 20 20 20  se->isNot .     
34cbf 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
34cc0 72 65 61 74 65 20 61 6e 20 69 6d 70 6c 69 63 69  reate an implici
34cc1 74 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 20  t NOT operator. 
34cc2 2a 2f 0a 20 20 20 20 20 20 20 20 46 74 73 33 45  */.        Fts3E
34cc3 78 70 72 20 2a 70 4e 6f 74 20 3d 20 73 71 6c 69  xpr *pNot = sqli
34cc4 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
34cc5 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20  f(Fts3Expr));.  
34cc6 20 20 20 20 20 20 69 66 28 20 21 70 4e 6f 74 20        if( !pNot 
34cc7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
34cc8 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
34cc9 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  (p);.          r
34cca 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
34ccb 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
34ccc 20 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a   exprparse_out;.
34ccd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
34cce 20 20 6d 65 6d 73 65 74 28 70 4e 6f 74 2c 20 30    memset(pNot, 0
34ccf 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70  , sizeof(Fts3Exp
34cd0 72 29 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f  r));.        pNo
34cd1 74 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55  t->eType = FTSQU
34cd2 45 52 59 5f 4e 4f 54 3b 0a 20 20 20 20 20 20 20  ERY_NOT;.       
34cd3 20 70 4e 6f 74 2d 3e 70 52 69 67 68 74 20 3d 20   pNot->pRight = 
34cd4 70 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  p;.        if( p
34cd5 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0a 20 20 20  NotBranch ){.   
34cd6 20 20 20 20 20 20 20 70 4e 6f 74 42 72 61 6e 63         pNotBranc
34cd7 68 2d 3e 70 4c 65 66 74 20 3d 20 70 3b 0a 20 20  h->pLeft = p;.  
34cd8 20 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e 70 52          pNot->pR
34cd9 69 67 68 74 20 3d 20 70 4e 6f 74 42 72 61 6e 63  ight = pNotBranc
34cda 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  h;.        }.   
34cdb 20 20 20 20 20 70 4e 6f 74 42 72 61 6e 63 68 20       pNotBranch 
34cdc 3d 20 70 4e 6f 74 3b 0a 20 20 20 20 20 20 7d 65  = pNot;.      }e
34cdd 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  lse{.        int
34cde 20 65 54 79 70 65 20 3d 20 70 2d 3e 65 54 79 70   eType = p->eTyp
34cdf 65 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e;.        asser
34ce0 74 28 20 65 54 79 70 65 21 3d 46 54 53 51 55 45  t( eType!=FTSQUE
34ce1 52 59 5f 50 48 52 41 53 45 20 7c 7c 20 21 70 2d  RY_PHRASE || !p-
34ce2 3e 70 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74 20  >pPhrase->isNot 
34ce3 29 3b 0a 20 20 20 20 20 20 20 20 69 73 50 68 72  );.        isPhr
34ce4 61 73 65 20 3d 20 28 65 54 79 70 65 3d 3d 46 54  ase = (eType==FT
34ce5 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 7c 7c  SQUERY_PHRASE ||
34ce6 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20 20   p->pLeft);..   
34ce7 20 20 20 20 20 2f 2a 20 54 68 65 20 69 73 52 65       /* The isRe
34ce8 71 75 69 72 65 50 68 72 61 73 65 20 76 61 72 69  quirePhrase vari
34ce9 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 74  able is set to t
34cea 72 75 65 20 69 66 20 61 20 70 68 72 61 73 65 20  rue if a phrase 
34ceb 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  or.        ** an
34cec 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74   expression cont
34ced 61 69 6e 65 64 20 69 6e 20 70 61 72 65 6e 74 68  ained in parenth
34cee 65 73 69 73 20 69 73 20 72 65 71 75 69 72 65 64  esis is required
34cef 2e 20 49 66 20 61 0a 20 20 20 20 20 20 20 20 2a  . If a.        *
34cf0 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 6f  * binary operato
34cf1 72 20 28 41 4e 44 2c 20 4f 52 2c 20 4e 4f 54 20  r (AND, OR, NOT 
34cf2 6f 72 20 4e 45 41 52 29 20 69 73 20 65 6e 63 6f  or NEAR) is enco
34cf3 75 6e 74 65 64 20 77 68 65 6e 0a 20 20 20 20 20  unted when.     
34cf4 20 20 20 2a 2a 20 69 73 52 65 71 75 69 72 65 50     ** isRequireP
34cf5 68 72 61 73 65 20 69 73 20 73 65 74 2c 20 74 68  hrase is set, th
34cf6 69 73 20 69 73 20 61 20 73 79 6e 74 61 78 20 65  is is a syntax e
34cf7 72 72 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f  rror..        */
34cf8 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73  .        if( !is
34cf9 50 68 72 61 73 65 20 26 26 20 69 73 52 65 71 75  Phrase && isRequ
34cfa 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20 20  irePhrase ){.   
34cfb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74         sqlite3Ft
34cfc 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20  s3ExprFree(p);. 
34cfd 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
34cfe 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
34cff 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70        goto exprp
34d00 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20  arse_out;.      
34d01 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 69    }.  .        i
34d02 66 28 20 69 73 50 68 72 61 73 65 20 26 26 20 21  f( isPhrase && !
34d03 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
34d04 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
34d05 49 6e 73 65 72 74 20 61 6e 20 69 6d 70 6c 69 63  Insert an implic
34d06 69 74 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e  it AND operator.
34d07 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 46 74   */.          Ft
34d08 73 33 45 78 70 72 20 2a 70 41 6e 64 3b 0a 20 20  s3Expr *pAnd;.  
34d09 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
34d0a 70 52 65 74 20 26 26 20 70 50 72 65 76 20 29 3b  pRet && pPrev );
34d0b 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e 64 20  .          pAnd 
34d0c 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
34d0d 28 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72  (sizeof(Fts3Expr
34d0e 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ));.          if
34d0f 28 20 21 70 41 6e 64 20 29 7b 0a 20 20 20 20 20  ( !pAnd ){.     
34d10 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74         sqlite3Ft
34d11 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20  s3ExprFree(p);. 
34d12 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
34d13 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
34d14 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
34d15 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20  xprparse_out;.  
34d16 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
34d17 20 20 20 20 6d 65 6d 73 65 74 28 70 41 6e 64 2c      memset(pAnd,
34d18 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 45   0, sizeof(Fts3E
34d19 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20 20  xpr));.         
34d1a 20 70 41 6e 64 2d 3e 65 54 79 70 65 20 3d 20 46   pAnd->eType = F
34d1b 54 53 51 55 45 52 59 5f 41 4e 44 3b 0a 20 20 20  TSQUERY_AND;.   
34d1c 20 20 20 20 20 20 20 69 6e 73 65 72 74 42 69 6e         insertBin
34d1d 61 72 79 4f 70 65 72 61 74 6f 72 28 26 70 52 65  aryOperator(&pRe
34d1e 74 2c 20 70 50 72 65 76 2c 20 70 41 6e 64 29 3b  t, pPrev, pAnd);
34d1f 0a 20 20 20 20 20 20 20 20 20 20 70 50 72 65 76  .          pPrev
34d20 20 3d 20 70 41 6e 64 3b 0a 20 20 20 20 20 20 20   = pAnd;.       
34d21 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   }..        /* T
34d22 68 69 73 20 74 65 73 74 20 63 61 74 63 68 65 73  his test catches
34d23 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6d 61 6b   attempts to mak
34d24 65 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  e either operand
34d25 20 6f 66 20 61 20 4e 45 41 52 0a 20 20 20 20 20   of a NEAR.     
34d26 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20 73     ** operator s
34d27 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74  omething other t
34d28 68 61 6e 20 61 20 70 68 72 61 73 65 2e 20 46 6f  han a phrase. Fo
34d29 72 20 65 78 61 6d 70 6c 65 2c 20 65 69 74 68 65  r example, eithe
34d2a 72 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20  r of.        ** 
34d2b 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20  the following:. 
34d2c 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
34d2d 20 20 2a 2a 20 20 20 20 28 62 72 61 63 6b 65 74    **    (bracket
34d2e 65 64 20 65 78 70 72 65 73 73 69 6f 6e 29 20 4e  ed expression) N
34d2f 45 41 52 20 70 68 72 61 73 65 0a 20 20 20 20 20  EAR phrase.     
34d30 20 20 20 2a 2a 20 20 20 20 70 68 72 61 73 65 20     **    phrase 
34d31 4e 45 41 52 20 28 62 72 61 63 6b 65 74 65 64 20  NEAR (bracketed 
34d32 65 78 70 72 65 73 73 69 6f 6e 29 0a 20 20 20 20  expression).    
34d33 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
34d34 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  * Return an erro
34d35 72 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 65  r in either case
34d36 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
34d37 20 20 20 20 20 69 66 28 20 70 50 72 65 76 20 26       if( pPrev &
34d38 26 20 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  & (.            
34d39 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  (eType==FTSQUERY
34d3a 5f 4e 45 41 52 20 26 26 20 21 69 73 50 68 72 61  _NEAR && !isPhra
34d3b 73 65 20 26 26 20 70 50 72 65 76 2d 3e 65 54 79  se && pPrev->eTy
34d3c 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe!=FTSQUERY_PHR
34d3d 41 53 45 29 0a 20 20 20 20 20 20 20 20 20 7c 7c  ASE).         ||
34d3e 20 28 65 54 79 70 65 21 3d 46 54 53 51 55 45 52   (eType!=FTSQUER
34d3f 59 5f 50 48 52 41 53 45 20 26 26 20 69 73 50 68  Y_PHRASE && isPh
34d40 72 61 73 65 20 26 26 20 70 50 72 65 76 2d 3e 65  rase && pPrev->e
34d41 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e  Type==FTSQUERY_N
34d42 45 41 52 29 0a 20 20 20 20 20 20 20 20 29 29 7b  EAR).        )){
34d43 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
34d44 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
34d45 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
34d46 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
34d47 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
34d48 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20  xprparse_out;.  
34d49 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20        }.  .     
34d4a 20 20 20 69 66 28 20 69 73 50 68 72 61 73 65 20     if( isPhrase 
34d4b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
34d4c 20 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20   pRet ){.       
34d4d 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72       assert( pPr
34d4e 65 76 20 26 26 20 70 50 72 65 76 2d 3e 70 4c 65  ev && pPrev->pLe
34d4f 66 74 20 26 26 20 70 50 72 65 76 2d 3e 70 52 69  ft && pPrev->pRi
34d50 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ght==0 );.      
34d51 20 20 20 20 20 20 70 50 72 65 76 2d 3e 70 52 69        pPrev->pRi
34d52 67 68 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20  ght = p;.       
34d53 20 20 20 20 20 70 2d 3e 70 50 61 72 65 6e 74 20       p->pParent 
34d54 3d 20 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20  = pPrev;.       
34d55 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
34d56 20 20 20 20 20 20 70 52 65 74 20 3d 20 70 3b 0a        pRet = p;.
34d57 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
34d58 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
34d59 20 20 20 20 20 69 6e 73 65 72 74 42 69 6e 61 72       insertBinar
34d5a 79 4f 70 65 72 61 74 6f 72 28 26 70 52 65 74 2c  yOperator(&pRet,
34d5b 20 70 50 72 65 76 2c 20 70 29 3b 0a 20 20 20 20   pPrev, p);.    
34d5c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 73      }.        is
34d5d 52 65 71 75 69 72 65 50 68 72 61 73 65 20 3d 20  RequirePhrase = 
34d5e 21 69 73 50 68 72 61 73 65 3b 0a 20 20 20 20 20  !isPhrase;.     
34d5f 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
34d60 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 20 20   nByte>0 );.    
34d61 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  }.    assert( rc
34d62 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28  !=SQLITE_OK || (
34d63 6e 42 79 74 65 3e 30 20 26 26 20 6e 42 79 74 65  nByte>0 && nByte
34d64 3c 3d 6e 49 6e 29 20 29 3b 0a 20 20 20 20 6e 49  <=nIn) );.    nI
34d65 6e 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20  n -= nByte;.    
34d66 7a 49 6e 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20  zIn += nByte;.  
34d67 20 20 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d    pPrev = p;.  }
34d68 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
34d69 54 45 5f 44 4f 4e 45 20 26 26 20 70 52 65 74 20  TE_DONE && pRet 
34d6a 26 26 20 69 73 52 65 71 75 69 72 65 50 68 72 61  && isRequirePhra
34d6b 73 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  se ){.    rc = S
34d6c 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
34d6d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
34d6e 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 72  TE_DONE ){.    r
34d6f 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
34d70 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f     if( !sqlite3_
34d71 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65  fts3_enable_pare
34d72 6e 74 68 65 73 65 73 20 26 26 20 70 4e 6f 74 42  ntheses && pNotB
34d73 72 61 6e 63 68 20 29 7b 0a 20 20 20 20 20 20 69  ranch ){.      i
34d74 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20 20  f( !pRet ){.    
34d75 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
34d76 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 65 6c  ERROR;.      }el
34d77 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4e 6f 74  se{.        pNot
34d78 42 72 61 6e 63 68 2d 3e 70 4c 65 66 74 20 3d 20  Branch->pLeft = 
34d79 70 52 65 74 3b 0a 20 20 20 20 20 20 20 20 70 52  pRet;.        pR
34d7a 65 74 20 3d 20 70 4e 6f 74 42 72 61 6e 63 68 3b  et = pNotBranch;
34d7b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
34d7c 20 7d 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64   }.  *pnConsumed
34d7d 20 3d 20 6e 20 2d 20 6e 49 6e 3b 0a 0a 65 78 70   = n - nIn;..exp
34d7e 72 70 61 72 73 65 5f 6f 75 74 3a 0a 20 20 69 66  rparse_out:.  if
34d7f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
34d80 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  ){.    sqlite3Ft
34d81 73 33 45 78 70 72 46 72 65 65 28 70 52 65 74 29  s3ExprFree(pRet)
34d82 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  ;.    sqlite3Fts
34d83 33 45 78 70 72 46 72 65 65 28 70 4e 6f 74 42 72  3ExprFree(pNotBr
34d84 61 6e 63 68 29 3b 0a 20 20 20 20 70 52 65 74 20  anch);.    pRet 
34d85 3d 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 70 45 78  = 0;.  }.  *ppEx
34d86 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 72 65 74  pr = pRet;.  ret
34d87 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
34d88 20 50 61 72 61 6d 65 74 65 72 73 20 7a 20 61 6e   Parameters z an
34d89 64 20 6e 20 63 6f 6e 74 61 69 6e 20 61 20 70 6f  d n contain a po
34d8a 69 6e 74 65 72 20 74 6f 20 61 6e 64 20 6c 65 6e  inter to and len
34d8b 67 74 68 20 6f 66 20 61 20 62 75 66 66 65 72 20  gth of a buffer 
34d8c 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 6e  containing.** an
34d8d 20 66 74 73 33 20 71 75 65 72 79 20 65 78 70 72   fts3 query expr
34d8e 65 73 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69  ession, respecti
34d8f 76 65 6c 79 2e 20 54 68 69 73 20 66 75 6e 63 74  vely. This funct
34d90 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ion attempts to 
34d91 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 71 75 65  parse the.** que
34d92 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  ry expression an
34d93 64 20 63 72 65 61 74 65 20 61 20 74 72 65 65 20  d create a tree 
34d94 6f 66 20 46 74 73 33 45 78 70 72 20 73 74 72 75  of Fts3Expr stru
34d95 63 74 75 72 65 73 20 72 65 70 72 65 73 65 6e 74  ctures represent
34d96 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 73 65  ing the.** parse
34d97 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 66  d expression. If
34d98 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 70   successful, *pp
34d99 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 70  Expr is set to p
34d9a 6f 69 6e 74 20 74 6f 20 74 68 65 20 68 65 61 64  oint to the head
34d9b 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 73 65  .** of the parse
34d9c 64 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  d expression tre
34d9d 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  e and SQLITE_OK 
34d9e 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
34d9f 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75  an error.** occu
34da0 72 73 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54  rs, either SQLIT
34da1 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74 2d 6f 66 2d  E_NOMEM (out-of-
34da2 6d 65 6d 6f 72 79 20 65 72 72 6f 72 29 20 6f 72  memory error) or
34da3 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 70   SQLITE_ERROR (p
34da4 61 72 73 65 0a 2a 2a 20 65 72 72 6f 72 29 20 69  arse.** error) i
34da5 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  s returned and *
34da6 70 70 45 78 70 72 20 69 73 20 73 65 74 20 74 6f  ppExpr is set to
34da7 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72   0..**.** If par
34da8 61 6d 65 74 65 72 20 6e 20 69 73 20 61 20 6e 65  ameter n is a ne
34da9 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74  gative number, t
34daa 68 65 6e 20 7a 20 69 73 20 61 73 73 75 6d 65 64  hen z is assumed
34dab 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 0a 2a   to point to a.*
34dac 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  * nul-terminated
34dad 20 73 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20   string and the 
34dae 6c 65 6e 67 74 68 20 69 73 20 64 65 74 65 72 6d  length is determ
34daf 69 6e 65 64 20 75 73 69 6e 67 20 73 74 72 6c 65  ined using strle
34db0 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  n()..**.** The f
34db1 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20  irst parameter, 
34db2 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 69 73 20 70  pTokenizer, is p
34db3 61 73 73 65 64 20 74 68 65 20 66 74 73 33 20 74  assed the fts3 t
34db4 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20  okenizer module 
34db5 74 6f 0a 2a 2a 20 75 73 65 20 74 6f 20 6e 6f 72  to.** use to nor
34db6 6d 61 6c 69 7a 65 20 71 75 65 72 79 20 74 6f 6b  malize query tok
34db7 65 6e 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e  ens while parsin
34db8 67 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  g the expression
34db9 2e 20 54 68 65 20 61 7a 43 6f 6c 5b 5d 0a 2a 2a  . The azCol[].**
34dba 20 61 72 72 61 79 2c 20 77 68 69 63 68 20 69 73   array, which is
34dbb 20 61 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74   assumed to cont
34dbc 61 69 6e 20 6e 43 6f 6c 20 65 6e 74 72 69 65 73  ain nCol entries
34dbd 2c 20 73 68 6f 75 6c 64 20 63 6f 6e 74 61 69 6e  , should contain
34dbe 20 74 68 65 20 6e 61 6d 65 73 0a 2a 2a 20 6f 66   the names.** of
34dbf 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 20   each column in 
34dc0 74 68 65 20 74 61 72 67 65 74 20 66 74 73 33 20  the target fts3 
34dc1 74 61 62 6c 65 2c 20 69 6e 20 6f 72 64 65 72 20  table, in order 
34dc2 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67  from left to rig
34dc3 68 74 2e 20 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 6e  ht. .** Column n
34dc4 61 6d 65 73 20 6d 75 73 74 20 62 65 20 6e 75 6c  ames must be nul
34dc5 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
34dc6 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ngs..**.** The i
34dc7 44 65 66 61 75 6c 74 43 6f 6c 20 70 61 72 61 6d  DefaultCol param
34dc8 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 70  eter should be p
34dc9 61 73 73 65 64 20 74 68 65 20 69 6e 64 65 78 20  assed the index 
34dca 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
34dcb 75 6d 6e 0a 2a 2a 20 74 68 61 74 20 61 70 70 65  umn.** that appe
34dcc 61 72 73 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d  ars on the left-
34dcd 68 61 6e 64 2d 73 69 64 65 20 6f 66 20 74 68 65  hand-side of the
34dce 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20   MATCH operator 
34dcf 28 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20  (the default.** 
34dd0 63 6f 6c 75 6d 6e 20 74 6f 20 6d 61 74 63 68 20  column to match 
34dd1 61 67 61 69 6e 73 74 20 66 6f 72 20 74 6f 6b 65  against for toke
34dd2 6e 73 20 66 6f 72 20 77 68 69 63 68 20 61 20 63  ns for which a c
34dd3 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 73 20 6e 6f  olumn name is no
34dd4 74 20 65 78 70 6c 69 63 69 74 6c 79 0a 2a 2a 20  t explicitly.** 
34dd5 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72  specified as par
34dd6 74 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 73  t of the query s
34dd7 74 72 69 6e 67 29 2c 20 6f 72 20 2d 31 20 69 66  tring), or -1 if
34dd8 20 74 6f 6b 65 6e 73 20 6d 61 79 20 62 79 20 64   tokens may by d
34dd9 65 66 61 75 6c 74 0a 2a 2a 20 6d 61 74 63 68 20  efault.** match 
34dda 61 6e 79 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  any table column
34ddb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
34ddc 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
34ddd 74 73 33 45 78 70 72 50 61 72 73 65 28 0a 20 20  ts3ExprParse(.  
34dde 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34ddf 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20  r *pTokenizer,  
34de0 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
34de1 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61   module */.  cha
34de2 72 20 2a 2a 61 7a 43 6f 6c 2c 20 20 20 20 20 20  r **azCol,      
34de3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34de4 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c   /* Array of col
34de5 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 66 74  umn names for ft
34de6 73 33 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  s3 table */.  in
34de7 74 20 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  t nCol,         
34de8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34de9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
34dea 6e 74 72 69 65 73 20 69 6e 20 61 7a 43 6f 6c 5b  ntries in azCol[
34deb 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65 66 61  ] */.  int iDefa
34dec 75 6c 74 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  ultCol,         
34ded 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
34dee 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20 74 6f 20  fault column to 
34def 71 75 65 72 79 20 2a 2f 0a 20 20 63 6f 6e 73 74  query */.  const
34df0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
34df1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34df2 2a 20 54 65 78 74 20 6f 66 20 4d 41 54 43 48 20  * Text of MATCH 
34df3 71 75 65 72 79 20 2a 2f 0a 20 20 46 74 73 33 45  query */.  Fts3E
34df4 78 70 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20  xpr **ppExpr    
34df5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34df6 2a 20 4f 55 54 3a 20 50 61 72 73 65 64 20 71 75  * OUT: Parsed qu
34df7 65 72 79 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ery structure */
34df8 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 72 73 65  .){.  int nParse
34df9 64 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50  d;.  int rc;.  P
34dfa 61 72 73 65 43 6f 6e 74 65 78 74 20 73 50 61 72  arseContext sPar
34dfb 73 65 3b 0a 20 20 73 50 61 72 73 65 2e 70 54 6f  se;.  sParse.pTo
34dfc 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e  kenizer = pToken
34dfd 69 7a 65 72 3b 0a 20 20 73 50 61 72 73 65 2e 61  izer;.  sParse.a
34dfe 7a 43 6f 6c 20 3d 20 28 63 6f 6e 73 74 20 63 68  zCol = (const ch
34dff 61 72 20 2a 2a 29 61 7a 43 6f 6c 3b 0a 20 20 73  ar **)azCol;.  s
34e00 50 61 72 73 65 2e 6e 43 6f 6c 20 3d 20 6e 43 6f  Parse.nCol = nCo
34e01 6c 3b 0a 20 20 73 50 61 72 73 65 2e 69 44 65 66  l;.  sParse.iDef
34e02 61 75 6c 74 43 6f 6c 20 3d 20 69 44 65 66 61 75  aultCol = iDefau
34e03 6c 74 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65 2e  ltCol;.  sParse.
34e04 6e 4e 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28  nNest = 0;.  if(
34e05 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 70   z==0 ){.    *pp
34e06 45 78 70 72 20 3d 20 30 3b 0a 20 20 20 20 72 65  Expr = 0;.    re
34e07 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
34e08 20 20 7d 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b    }.  if( n<0 ){
34e09 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28  .    n = strlen(
34e0a 7a 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66  z);.  }.  rc = f
34e0b 74 73 33 45 78 70 72 50 61 72 73 65 28 26 73 50  ts3ExprParse(&sP
34e0c 61 72 73 65 2c 20 7a 2c 20 6e 2c 20 70 70 45 78  arse, z, n, ppEx
34e0d 70 72 2c 20 26 6e 50 61 72 73 65 64 29 3b 0a 0a  pr, &nParsed);..
34e0e 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6d    /* Check for m
34e0f 69 73 6d 61 74 63 68 65 64 20 70 61 72 65 6e 74  ismatched parent
34e10 68 65 73 69 73 20 2a 2f 0a 20 20 69 66 28 20 72  hesis */.  if( r
34e11 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
34e12 73 50 61 72 73 65 2e 6e 4e 65 73 74 20 29 7b 0a  sParse.nNest ){.
34e13 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
34e14 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74  ERROR;.    sqlit
34e15 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 2a  e3Fts3ExprFree(*
34e16 70 70 45 78 70 72 29 3b 0a 20 20 20 20 2a 70 70  ppExpr);.    *pp
34e17 45 78 70 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  Expr = 0;.  }.. 
34e18 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
34e19 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 72 73  *.** Free a pars
34e1a 65 64 20 66 74 73 33 20 71 75 65 72 79 20 65 78  ed fts3 query ex
34e1b 70 72 65 73 73 69 6f 6e 20 61 6c 6c 6f 63 61 74  pression allocat
34e1c 65 64 20 62 79 20 73 71 6c 69 74 65 33 46 74 73  ed by sqlite3Fts
34e1d 33 45 78 70 72 50 61 72 73 65 28 29 2e 0a 2a 2f  3ExprParse()..*/
34e1e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
34e1f 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
34e20 45 78 70 72 46 72 65 65 28 46 74 73 33 45 78 70  ExprFree(Fts3Exp
34e21 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  r *p){.  if( p )
34e22 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
34e23 33 45 78 70 72 46 72 65 65 28 70 2d 3e 70 4c 65  3ExprFree(p->pLe
34e24 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ft);.    sqlite3
34e25 46 74 73 33 45 78 70 72 46 72 65 65 28 70 2d 3e  Fts3ExprFree(p->
34e26 70 52 69 67 68 74 29 3b 0a 20 20 20 20 73 71 6c  pRight);.    sql
34e27 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
34e28 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.}../**********
34e29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e2d 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
34e2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e32 0a 2a 2a 20 45 76 65 72 79 74 68 69 6e 67 20 61  .** Everything a
34e33 66 74 65 72 20 74 68 69 73 20 70 6f 69 6e 74 20  fter this point 
34e34 69 73 20 6a 75 73 74 20 74 65 73 74 20 63 6f 64  is just test cod
34e35 65 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  e..*/..#ifdef SQ
34e36 4c 49 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a  LITE_TEST.../*.*
34e37 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 71 75  * Function to qu
34e38 65 72 79 20 74 68 65 20 68 61 73 68 2d 74 61 62  ery the hash-tab
34e39 6c 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72 73  le of tokenizers
34e3a 20 28 73 65 65 20 52 45 41 44 4d 45 2e 74 6f 6b   (see README.tok
34e3b 65 6e 69 7a 65 72 73 29 2e 0a 2a 2f 0a 73 74 61  enizers)..*/.sta
34e3c 74 69 63 20 69 6e 74 20 71 75 65 72 79 54 65 73  tic int queryTes
34e3d 74 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71  tTokenizer(.  sq
34e3e 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 6f  lite3 *db, .  co
34e3f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
34e40 20 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74    .  const sqlit
34e41 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
34e42 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e  ule **pp.){.  in
34e43 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
34e44 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63  stmt *pStmt;.  c
34e45 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d  onst char zSql[]
34e46 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f   = "SELECT fts3_
34e47 74 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a  tokenizer(?)";..
34e48 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 72 63 20    *pp = 0;.  rc 
34e49 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
34e4a 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
34e4b 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
34e4c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
34e4d 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
34e4e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69   rc;.  }..  sqli
34e4f 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70 53  te3_bind_text(pS
34e50 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d  tmt, 1, zName, -
34e51 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
34e52 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
34e53 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
34e54 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20  p(pStmt) ){.    
34e55 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  if( sqlite3_colu
34e56 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 30  mn_type(pStmt, 0
34e57 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  )==SQLITE_BLOB )
34e58 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  {.      memcpy(p
34e59 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  p, sqlite3_colum
34e5a 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29  n_blob(pStmt, 0)
34e5b 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a  , sizeof(*pp));.
34e5c 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
34e5d 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
34e5e 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
34e5f 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
34e60 69 6f 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74  ion is part of t
34e61 68 65 20 74 65 73 74 20 69 6e 74 65 72 66 61 63  he test interfac
34e62 65 20 66 6f 72 20 74 68 65 20 71 75 65 72 79 20  e for the query 
34e63 70 61 72 73 65 72 2e 20 49 74 0a 2a 2a 20 77 72  parser. It.** wr
34e64 69 74 65 73 20 61 20 74 65 78 74 20 72 65 70 72  ites a text repr
34e65 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
34e66 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  e query expressi
34e67 6f 6e 20 70 45 78 70 72 20 69 6e 74 6f 20 74 68  on pExpr into th
34e68 65 0a 2a 2a 20 62 75 66 66 65 72 20 70 6f 69 6e  e.** buffer poin
34e69 74 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65  ted to by argume
34e6a 6e 74 20 7a 42 75 66 2e 20 49 74 20 69 73 20 61  nt zBuf. It is a
34e6b 73 73 75 6d 65 64 20 74 68 61 74 20 7a 42 75 66  ssumed that zBuf
34e6c 20 69 73 20 6c 61 72 67 65 20 0a 2a 2a 20 65 6e   is large .** en
34e6d 6f 75 67 68 20 74 6f 20 73 74 6f 72 65 20 74 68  ough to store th
34e6e 65 20 72 65 71 75 69 72 65 64 20 74 65 78 74 20  e required text 
34e6f 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a  representation..
34e70 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65  */.static void e
34e71 78 70 72 54 6f 53 74 72 69 6e 67 28 46 74 73 33  xprToString(Fts3
34e72 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
34e73 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 77 69 74  r *zBuf){.  swit
34e74 63 68 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65  ch( pExpr->eType
34e75 20 29 7b 0a 20 20 20 20 63 61 73 65 20 46 54 53   ){.    case FTS
34e76 51 55 45 52 59 5f 50 48 52 41 53 45 3a 20 7b 0a  QUERY_PHRASE: {.
34e77 20 20 20 20 20 20 46 74 73 33 50 68 72 61 73 65        Fts3Phrase
34e78 20 2a 70 50 68 72 61 73 65 20 3d 20 70 45 78 70   *pPhrase = pExp
34e79 72 2d 3e 70 50 68 72 61 73 65 3b 0a 20 20 20 20  r->pPhrase;.    
34e7a 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 7a    int i;.      z
34e7b 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a  Buf += sprintf(z
34e7c 42 75 66 2c 20 22 50 48 52 41 53 45 20 25 64 20  Buf, "PHRASE %d 
34e7d 25 64 22 2c 20 70 50 68 72 61 73 65 2d 3e 69 43  %d", pPhrase->iC
34e7e 6f 6c 75 6d 6e 2c 20 70 50 68 72 61 73 65 2d 3e  olumn, pPhrase->
34e7f 69 73 4e 6f 74 29 3b 0a 20 20 20 20 20 20 66 6f  isNot);.      fo
34e80 72 28 69 3d 30 3b 20 69 3c 70 50 68 72 61 73 65  r(i=0; i<pPhrase
34e81 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0a  ->nToken; i++){.
34e82 20 20 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20          zBuf += 
34e83 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 22 20 25  sprintf(zBuf," %
34e84 2e 2a 73 22 2c 70 50 68 72 61 73 65 2d 3e 61 54  .*s",pPhrase->aT
34e85 6f 6b 65 6e 5b 69 5d 2e 6e 2c 70 50 68 72 61 73  oken[i].n,pPhras
34e86 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 29 3b  e->aToken[i].z);
34e87 0a 20 20 20 20 20 20 20 20 7a 42 75 66 20 2b 3d  .        zBuf +=
34e88 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 22 25   sprintf(zBuf,"%
34e89 73 22 2c 20 28 70 50 68 72 61 73 65 2d 3e 61 54  s", (pPhrase->aT
34e8a 6f 6b 65 6e 5b 69 5d 2e 69 73 50 72 65 66 69 78  oken[i].isPrefix
34e8b 3f 22 2b 22 3a 22 22 29 29 3b 0a 20 20 20 20 20  ?"+":""));.     
34e8c 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b   }.      return;
34e8d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65  .    }..    case
34e8e 20 46 54 53 51 55 45 52 59 5f 4e 45 41 52 3a 0a   FTSQUERY_NEAR:.
34e8f 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70        zBuf += sp
34e90 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 4e 45 41  rintf(zBuf, "NEA
34e91 52 2f 25 64 20 22 2c 20 70 45 78 70 72 2d 3e 6e  R/%d ", pExpr->n
34e92 4e 65 61 72 29 3b 0a 20 20 20 20 20 20 62 72 65  Near);.      bre
34e93 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 46 54 53  ak;.    case FTS
34e94 51 55 45 52 59 5f 4e 4f 54 3a 0a 20 20 20 20 20  QUERY_NOT:.     
34e95 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66   zBuf += sprintf
34e96 28 7a 42 75 66 2c 20 22 4e 4f 54 20 22 29 3b 0a  (zBuf, "NOT ");.
34e97 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
34e98 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 41   case FTSQUERY_A
34e99 4e 44 3a 0a 20 20 20 20 20 20 7a 42 75 66 20 2b  ND:.      zBuf +
34e9a 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20  = sprintf(zBuf, 
34e9b 22 41 4e 44 20 22 29 3b 0a 20 20 20 20 20 20 62  "AND ");.      b
34e9c 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 46  reak;.    case F
34e9d 54 53 51 55 45 52 59 5f 4f 52 3a 0a 20 20 20 20  TSQUERY_OR:.    
34e9e 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
34e9f 66 28 7a 42 75 66 2c 20 22 4f 52 20 22 29 3b 0a  f(zBuf, "OR ");.
34ea0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d        break;.  }
34ea1 0a 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69  ..  zBuf += spri
34ea2 6e 74 66 28 7a 42 75 66 2c 20 22 7b 22 29 3b 0a  ntf(zBuf, "{");.
34ea3 20 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70    exprToString(p
34ea4 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 7a 42 75  Expr->pLeft, zBu
34ea5 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 74  f);.  zBuf += st
34ea6 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 7a 42  rlen(zBuf);.  zB
34ea7 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
34ea8 75 66 2c 20 22 7d 20 22 29 3b 0a 0a 20 20 7a 42  uf, "} ");..  zB
34ea9 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
34eaa 75 66 2c 20 22 7b 22 29 3b 0a 20 20 65 78 70 72  uf, "{");.  expr
34eab 54 6f 53 74 72 69 6e 67 28 70 45 78 70 72 2d 3e  ToString(pExpr->
34eac 70 52 69 67 68 74 2c 20 7a 42 75 66 29 3b 0a 20  pRight, zBuf);. 
34ead 20 7a 42 75 66 20 2b 3d 20 73 74 72 6c 65 6e 28   zBuf += strlen(
34eae 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d  zBuf);.  zBuf +=
34eaf 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
34eb0 7d 22 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  }");.}../*.** Th
34eb1 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d  is is the implem
34eb2 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 63  entation of a sc
34eb3 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f  alar SQL functio
34eb4 6e 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 74  n used to test t
34eb5 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  he .** expressio
34eb6 6e 20 70 61 72 73 65 72 2e 20 49 74 20 73 68 6f  n parser. It sho
34eb7 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 61 73  uld be called as
34eb8 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
34eb9 20 20 66 74 73 33 5f 65 78 70 72 74 65 73 74 28    fts3_exprtest(
34eba 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c 20 3c 65 78  <tokenizer>, <ex
34ebb 70 72 3e 2c 20 3c 63 6f 6c 75 6d 6e 20 31 3e 2c  pr>, <column 1>,
34ebc 20 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65   ...);.**.** The
34ebd 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c   first argument,
34ebe 20 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c 20 69 73   <tokenizer>, is
34ebf 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
34ec0 20 66 74 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20   fts3 tokenizer 
34ec1 75 73 65 64 0a 2a 2a 20 74 6f 20 70 61 72 73 65  used.** to parse
34ec2 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65   the query expre
34ec3 73 73 69 6f 6e 20 28 73 65 65 20 52 45 41 44 4d  ssion (see READM
34ec4 45 2e 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 20 54  E.tokenizers). T
34ec5 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
34ec6 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 71 75 65  nt.** is the que
34ec7 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  ry expression to
34ec8 20 70 61 72 73 65 2e 20 45 61 63 68 20 73 75 62   parse. Each sub
34ec9 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74  sequent argument
34eca 20 69 73 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20   is the name.** 
34ecb 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  of a column of t
34ecc 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 74 68  he fts3 table th
34ecd 61 74 20 74 68 65 20 71 75 65 72 79 20 65 78 70  at the query exp
34ece 72 65 73 73 69 6f 6e 20 6d 61 79 20 72 65 66 65  ression may refe
34ecf 72 20 74 6f 2e 0a 2a 2a 20 46 6f 72 20 65 78 61  r to..** For exa
34ed0 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  mple:.**.**   SE
34ed1 4c 45 43 54 20 66 74 73 33 5f 65 78 70 72 74 65  LECT fts3_exprte
34ed2 73 74 28 27 73 69 6d 70 6c 65 27 2c 20 27 42 69  st('simple', 'Bi
34ed3 6c 6c 20 63 6f 6c 32 3a 42 6c 6f 67 67 73 27 2c  ll col2:Bloggs',
34ed4 20 27 63 6f 6c 31 27 2c 20 27 63 6f 6c 32 27 29   'col1', 'col2')
34ed5 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.*/.static void
34ed6 20 66 74 73 33 45 78 70 72 54 65 73 74 28 0a 20   fts3ExprTest(. 
34ed7 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
34ed8 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
34ed9 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
34eda 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
34edb 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
34edc 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
34edd 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 30 3b 0a  t *pModule = 0;.
34ede 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
34edf 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20  zer *pTokenizer 
34ee0 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  = 0;.  int rc;. 
34ee1 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 20 3d 20   char **azCol = 
34ee2 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  0;.  const char 
34ee3 2a 7a 45 78 70 72 3b 0a 20 20 69 6e 74 20 6e 45  *zExpr;.  int nE
34ee4 78 70 72 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  xpr;.  int nCol;
34ee5 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 46 74 73  .  int ii;.  Fts
34ee6 33 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20  3Expr *pExpr;.  
34ee7 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
34ee8 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
34ee9 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
34eea 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3c 33 20  ;..  if( argc<3 
34eeb 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
34eec 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
34eed 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 22 55  ext, .        "U
34eee 73 61 67 65 3a 20 66 74 73 33 5f 65 78 70 72 74  sage: fts3_exprt
34eef 65 73 74 28 74 6f 6b 65 6e 69 7a 65 72 2c 20 65  est(tokenizer, e
34ef0 78 70 72 2c 20 63 6f 6c 31 2c 20 2e 2e 2e 22 2c  xpr, col1, ...",
34ef1 20 2d 31 0a 20 20 20 20 29 3b 0a 20 20 20 20 72   -1.    );.    r
34ef2 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 72 63  eturn;.  }..  rc
34ef3 20 3d 20 71 75 65 72 79 54 65 73 74 54 6f 6b 65   = queryTestToke
34ef4 6e 69 7a 65 72 28 64 62 2c 0a 20 20 20 20 20 20  nizer(db,.      
34ef5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34ef6 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20      (const char 
34ef7 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
34ef8 74 65 78 74 28 61 72 67 76 5b 30 5d 29 2c 20 26  text(argv[0]), &
34ef9 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20  pModule);.  if( 
34efa 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
34efb 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
34efc 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
34efd 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
34efe 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f   goto exprtest_o
34eff 75 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ut;.  }else if( 
34f00 21 70 4d 6f 64 75 6c 65 20 29 7b 0a 20 20 20 20  !pModule ){.    
34f01 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
34f02 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4e  rror(context, "N
34f03 6f 20 73 75 63 68 20 74 6f 6b 65 6e 69 7a 65 72  o such tokenizer
34f04 20 6d 6f 64 75 6c 65 22 2c 20 2d 31 29 3b 0a 20   module", -1);. 
34f05 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74     goto exprtest
34f06 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  _out;.  }..  rc 
34f07 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61  = pModule->xCrea
34f08 74 65 28 30 2c 20 30 2c 20 26 70 54 6f 6b 65 6e  te(0, 0, &pToken
34f09 69 7a 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28  izer);.  assert(
34f0a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
34f0b 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  M || rc==SQLITE_
34f0c 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  OK );.  if( rc==
34f0d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
34f0e 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
34f0f 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63  lt_error_nomem(c
34f10 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74  ontext);.    got
34f11 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a  o exprtest_out;.
34f12 20 20 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72    }.  pTokenizer
34f13 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64  ->pModule = pMod
34f14 75 6c 65 3b 0a 0a 20 20 7a 45 78 70 72 20 3d 20  ule;..  zExpr = 
34f15 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
34f16 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
34f17 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 6e 45 78  (argv[1]);.  nEx
34f18 70 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pr = sqlite3_val
34f19 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d  ue_bytes(argv[1]
34f1a 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 61 72 67 63  );.  nCol = argc
34f1b 2d 32 3b 0a 20 20 61 7a 43 6f 6c 20 3d 20 28 63  -2;.  azCol = (c
34f1c 68 61 72 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d  har **)sqlite3_m
34f1d 61 6c 6c 6f 63 28 6e 43 6f 6c 2a 73 69 7a 65 6f  alloc(nCol*sizeo
34f1e 66 28 63 68 61 72 20 2a 29 29 3b 0a 20 20 69 66  f(char *));.  if
34f1f 28 20 21 61 7a 43 6f 6c 20 29 7b 0a 20 20 20 20  ( !azCol ){.    
34f20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
34f21 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
34f22 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  xt);.    goto ex
34f23 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a  prtest_out;.  }.
34f24 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e    for(ii=0; ii<n
34f25 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  Col; ii++){.    
34f26 61 7a 43 6f 6c 5b 69 69 5d 20 3d 20 28 63 68 61  azCol[ii] = (cha
34f27 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
34f28 65 5f 74 65 78 74 28 61 72 67 76 5b 69 69 2b 32  e_text(argv[ii+2
34f29 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  ]);.  }..  rc = 
34f2a 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 50  sqlite3Fts3ExprP
34f2b 61 72 73 65 28 0a 20 20 20 20 20 20 70 54 6f 6b  arse(.      pTok
34f2c 65 6e 69 7a 65 72 2c 20 61 7a 43 6f 6c 2c 20 6e  enizer, azCol, n
34f2d 43 6f 6c 2c 20 6e 43 6f 6c 2c 20 7a 45 78 70 72  Col, nCol, zExpr
34f2e 2c 20 6e 45 78 70 72 2c 20 26 70 45 78 70 72 0a  , nExpr, &pExpr.
34f2f 20 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53    );.  if( rc==S
34f30 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
34f31 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
34f32 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
34f33 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f  ntext);.    goto
34f34 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20   exprtest_out;. 
34f35 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53   }else if( rc==S
34f36 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
34f37 63 68 61 72 20 7a 42 75 66 5b 34 30 39 36 5d 3b  char zBuf[4096];
34f38 0a 20 20 20 20 65 78 70 72 54 6f 53 74 72 69 6e  .    exprToStrin
34f39 67 28 70 45 78 70 72 2c 20 7a 42 75 66 29 3b 0a  g(pExpr, zBuf);.
34f3a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
34f3b 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
34f3c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54   zBuf, -1, SQLIT
34f3d 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
34f3e 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
34f3f 72 46 72 65 65 28 70 45 78 70 72 29 3b 0a 20 20  rFree(pExpr);.  
34f40 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
34f41 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
34f42 63 6f 6e 74 65 78 74 2c 20 22 45 72 72 6f 72 20  context, "Error 
34f43 70 61 72 73 69 6e 67 20 65 78 70 72 65 73 73 69  parsing expressi
34f44 6f 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 0a 65  on", -1);.  }..e
34f45 78 70 72 74 65 73 74 5f 6f 75 74 3a 0a 20 20 69  xprtest_out:.  i
34f46 66 28 20 70 4d 6f 64 75 6c 65 20 26 26 20 70 54  f( pModule && pT
34f47 6f 6b 65 6e 69 7a 65 72 20 29 7b 0a 20 20 20 20  okenizer ){.    
34f48 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 44  rc = pModule->xD
34f49 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65  estroy(pTokenize
34f4a 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  r);.  }.  sqlite
34f4b 33 5f 66 72 65 65 28 61 7a 43 6f 6c 29 3b 0a 7d  3_free(azCol);.}
34f4c 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
34f4d 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65   the query expre
34f4e 73 73 69 6f 6e 20 70 61 72 73 65 72 20 74 65 73  ssion parser tes
34f4f 74 20 66 75 6e 63 74 69 6f 6e 20 66 74 73 33 5f  t function fts3_
34f50 65 78 70 72 74 65 73 74 28 29 20 0a 2a 2a 20 77  exprtest() .** w
34f51 69 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ith database con
34f52 6e 65 63 74 69 6f 6e 20 64 62 2e 20 0a 2a 2f 0a  nection db. .*/.
34f53 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
34f54 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 45  oid sqlite3Fts3E
34f55 78 70 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72  xprInitTestInter
34f56 66 61 63 65 28 73 71 6c 69 74 65 33 2a 20 64 62  face(sqlite3* db
34f57 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  ){.  sqlite3_cre
34f58 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20  ate_function(.  
34f59 20 20 20 20 64 62 2c 20 22 66 74 73 33 5f 65 78      db, "fts3_ex
34f5a 70 72 74 65 73 74 22 2c 20 2d 31 2c 20 53 51 4c  prtest", -1, SQL
34f5b 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 66 74 73  ITE_UTF8, 0, fts
34f5c 33 45 78 70 72 54 65 73 74 2c 20 30 2c 20 30 0a  3ExprTest, 0, 0.
34f5d 20 20 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 23    );.}..#endif.#
34f5e 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
34f5f 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
34f60 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
34f61 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f  _ENABLE_FTS3) */
34f62 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
34f63 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 65 78  * End of fts3_ex
34f64 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pr.c ***********
34f65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f67 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
34f68 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
34f69 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a  3_hash.c *******
34f6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f6c 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
34f6d 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20  tember 22.**.** 
34f6e 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
34f6f 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
34f70 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
34f71 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
34f72 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
34f73 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
34f74 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
34f75 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
34f76 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
34f77 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
34f78 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
34f79 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
34f7a 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
34f7b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
34f7c 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
34f7d 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
34f7e 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
34f7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f83 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
34f84 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   is the implemen
34f85 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69  tation of generi
34f86 63 20 68 61 73 68 2d 74 61 62 6c 65 73 20 75 73  c hash-tables us
34f87 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a  ed in SQLite..**
34f88 20 57 65 27 76 65 20 6d 6f 64 69 66 69 65 64 20   We've modified 
34f89 69 74 20 73 6c 69 67 68 74 6c 79 20 74 6f 20 73  it slightly to s
34f8a 65 72 76 65 20 61 73 20 61 20 73 74 61 6e 64 61  erve as a standa
34f8b 6c 6f 6e 65 20 68 61 73 68 20 74 61 62 6c 65 0a  lone hash table.
34f8c 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
34f8d 6e 20 66 6f 72 20 74 68 65 20 66 75 6c 6c 2d 74  n for the full-t
34f8e 65 78 74 20 69 6e 64 65 78 69 6e 67 20 6d 6f 64  ext indexing mod
34f8f 75 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ule..*/../*.** T
34f90 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
34f91 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d  file is only com
34f92 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20  piled if:.**.** 
34f93 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
34f94 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
34f95 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e  uilt as an exten
34f96 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69  sion.**       (i
34f97 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
34f98 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20  ITE_CORE is not 
34f99 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a  defined), or.**.
34f9a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
34f9b 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
34f9c 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65  g built into the
34f9d 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20   core of.**     
34f9e 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69    SQLite (in whi
34f9f 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45  ch case SQLITE_E
34fa0 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65  NABLE_FTS3 is de
34fa1 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21  fined)..*/.#if !
34fa2 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
34fa3 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
34fa4 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
34fa5 53 33 29 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c  S3)..../*.** Mal
34fa6 6c 6f 63 20 61 6e 64 20 46 72 65 65 20 66 75 6e  loc and Free fun
34fa7 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 63  ctions.*/.static
34fa8 20 76 6f 69 64 20 2a 66 74 73 33 48 61 73 68 4d   void *fts3HashM
34fa9 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20  alloc(int n){.  
34faa 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65  void *p = sqlite
34fab 33 5f 6d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69  3_malloc(n);.  i
34fac 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73  f( p ){.    mems
34fad 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d  et(p, 0, n);.  }
34fae 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 73  .  return p;.}.s
34faf 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 48  tatic void fts3H
34fb0 61 73 68 46 72 65 65 28 76 6f 69 64 20 2a 70 29  ashFree(void *p)
34fb1 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  {.  sqlite3_free
34fb2 28 70 29 3b 0a 7d 0a 0a 2f 2a 20 54 75 72 6e 20  (p);.}../* Turn 
34fb3 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f  bulk memory into
34fb4 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 62   a hash table ob
34fb5 6a 65 63 74 20 62 79 20 69 6e 69 74 69 61 6c 69  ject by initiali
34fb6 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c  zing the.** fiel
34fb7 64 73 20 6f 66 20 74 68 65 20 48 61 73 68 20 73  ds of the Hash s
34fb8 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
34fb9 22 70 4e 65 77 22 20 69 73 20 61 20 70 6f 69 6e  "pNew" is a poin
34fba 74 65 72 20 74 6f 20 74 68 65 20 68 61 73 68 20  ter to the hash 
34fbb 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f  table that is to
34fbc 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   be initialized.
34fbd 0a 2a 2a 20 6b 65 79 43 6c 61 73 73 20 69 73 20  .** keyClass is 
34fbe 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6e 73 74  one of the const
34fbf 61 6e 74 73 20 0a 2a 2a 20 46 54 53 33 5f 48 41  ants .** FTS3_HA
34fc0 53 48 5f 42 49 4e 41 52 59 20 6f 72 20 46 54 53  SH_BINARY or FTS
34fc1 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2e 20 20  3_HASH_STRING.  
34fc2 54 68 65 20 76 61 6c 75 65 20 6f 66 20 6b 65 79  The value of key
34fc3 43 6c 61 73 73 20 0a 2a 2a 20 64 65 74 65 72 6d  Class .** determ
34fc4 69 6e 65 73 20 77 68 61 74 20 6b 69 6e 64 20 6f  ines what kind o
34fc5 66 20 6b 65 79 20 74 68 65 20 68 61 73 68 20 74  f key the hash t
34fc6 61 62 6c 65 20 77 69 6c 6c 20 75 73 65 2e 20 20  able will use.  
34fc7 22 63 6f 70 79 4b 65 79 22 20 69 73 0a 2a 2a 20  "copyKey" is.** 
34fc8 74 72 75 65 20 69 66 20 74 68 65 20 68 61 73 68  true if the hash
34fc9 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 6d 61   table should ma
34fca 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61  ke its own priva
34fcb 74 65 20 63 6f 70 79 20 6f 66 20 6b 65 79 73 20  te copy of keys 
34fcc 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20  and.** false if 
34fcd 69 74 20 73 68 6f 75 6c 64 20 6a 75 73 74 20 75  it should just u
34fce 73 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  se the supplied 
34fcf 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
34fd0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
34fd1 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
34fd2 6e 69 74 28 66 74 73 33 48 61 73 68 20 2a 70 4e  nit(fts3Hash *pN
34fd3 65 77 2c 20 69 6e 74 20 6b 65 79 43 6c 61 73 73  ew, int keyClass
34fd4 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 7b 0a  , int copyKey){.
34fd5 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d    assert( pNew!=
34fd6 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b  0 );.  assert( k
34fd7 65 79 43 6c 61 73 73 3e 3d 46 54 53 33 5f 48 41  eyClass>=FTS3_HA
34fd8 53 48 5f 53 54 52 49 4e 47 20 26 26 20 6b 65 79  SH_STRING && key
34fd9 43 6c 61 73 73 3c 3d 46 54 53 33 5f 48 41 53 48  Class<=FTS3_HASH
34fda 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20 70 4e 65  _BINARY );.  pNe
34fdb 77 2d 3e 6b 65 79 43 6c 61 73 73 20 3d 20 6b 65  w->keyClass = ke
34fdc 79 43 6c 61 73 73 3b 0a 20 20 70 4e 65 77 2d 3e  yClass;.  pNew->
34fdd 63 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79 4b 65  copyKey = copyKe
34fde 79 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74  y;.  pNew->first
34fdf 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f   = 0;.  pNew->co
34fe0 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  unt = 0;.  pNew-
34fe1 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70  >htsize = 0;.  p
34fe2 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a  New->ht = 0;.}..
34fe3 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e  /* Remove all en
34fe4 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73  tries from a has
34fe5 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69  h table.  Reclai
34fe6 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  m all memory..**
34fe7 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   Call this routi
34fe8 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68  ne to delete a h
34fe9 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20  ash table or to 
34fea 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62  reset a hash tab
34feb 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70  le.** to the emp
34fec 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c  ty state..*/.SQL
34fed 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
34fee 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
34fef 43 6c 65 61 72 28 66 74 73 33 48 61 73 68 20 2a  Clear(fts3Hash *
34ff0 70 48 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45  pH){.  fts3HashE
34ff1 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20  lem *elem;      
34ff2 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
34ff3 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65  g over all eleme
34ff4 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  nts of the table
34ff5 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
34ff6 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d  H!=0 );.  elem =
34ff7 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 48   pH->first;.  pH
34ff8 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 66  ->first = 0;.  f
34ff9 74 73 33 48 61 73 68 46 72 65 65 28 70 48 2d 3e  ts3HashFree(pH->
34ffa 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20  ht);.  pH->ht = 
34ffb 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20  0;.  pH->htsize 
34ffc 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 6c  = 0;.  while( el
34ffd 65 6d 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61  em ){.    fts3Ha
34ffe 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65  shElem *next_ele
34fff 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
35000 20 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79      if( pH->copy
35001 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65  Key && elem->pKe
35002 79 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 48  y ){.      fts3H
35003 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70 4b  ashFree(elem->pK
35004 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  ey);.    }.    f
35005 74 73 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d  ts3HashFree(elem
35006 29 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65  );.    elem = ne
35007 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70  xt_elem;.  }.  p
35008 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a  H->count = 0;.}.
35009 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20  ./*.** Hash and 
3500a 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
3500b 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f  ions when the mo
3500c 64 65 20 69 73 20 46 54 53 33 5f 48 41 53 48 5f  de is FTS3_HASH_
3500d 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 63  STRING.*/.static
3500e 20 69 6e 74 20 66 74 73 33 53 74 72 48 61 73 68   int fts3StrHash
3500f 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65  (const void *pKe
35010 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20  y, int nKey){.  
35011 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
35012 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b  (const char *)pK
35013 65 79 3b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b  ey;.  int h = 0;
35014 0a 20 20 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29  .  if( nKey<=0 )
35015 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29 20 73 74   nKey = (int) st
35016 72 6c 65 6e 28 7a 29 3b 0a 20 20 77 68 69 6c 65  rlen(z);.  while
35017 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20  ( nKey > 0  ){. 
35018 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20     h = (h<<3) ^ 
35019 68 20 5e 20 2a 7a 2b 2b 3b 0a 20 20 20 20 6e 4b  h ^ *z++;.    nK
3501a 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ey--;.  }.  retu
3501b 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66 66  rn h & 0x7ffffff
3501c 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  f;.}.static int 
3501d 66 74 73 33 53 74 72 43 6f 6d 70 61 72 65 28 63  fts3StrCompare(c
3501e 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
3501f 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20  , int n1, const 
35020 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74  void *pKey2, int
35021 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d   n2){.  if( n1!=
35022 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  n2 ) return 1;. 
35023 20 72 65 74 75 72 6e 20 73 74 72 6e 63 6d 70 28   return strncmp(
35024 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65  (const char*)pKe
35025 79 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  y1,(const char*)
35026 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a  pKey2,n1);.}../*
35027 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d  .** Hash and com
35028 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
35029 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20  s when the mode 
3502a 69 73 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e  is FTS3_HASH_BIN
3502b 41 52 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ARY.*/.static in
3502c 74 20 66 74 73 33 42 69 6e 48 61 73 68 28 63 6f  t fts3BinHash(co
3502d 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
3502e 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74  int nKey){.  int
3502f 20 68 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20   h = 0;.  const 
35030 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  char *z = (const
35031 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20   char *)pKey;.  
35032 77 68 69 6c 65 28 20 6e 4b 65 79 2d 2d 20 3e 20  while( nKey-- > 
35033 30 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c  0 ){.    h = (h<
35034 3c 33 29 20 5e 20 68 20 5e 20 2a 28 7a 2b 2b 29  <3) ^ h ^ *(z++)
35035 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68  ;.  }.  return h
35036 20 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d   & 0x7fffffff;.}
35037 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
35038 42 69 6e 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74  BinCompare(const
35039 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e   void *pKey1, in
3503a 74 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64  t n1, const void
3503b 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29   *pKey2, int n2)
3503c 7b 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29  {.  if( n1!=n2 )
3503d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74   return 1;.  ret
3503e 75 72 6e 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31  urn memcmp(pKey1
3503f 2c 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f  ,pKey2,n1);.}../
35040 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
35041 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70  inter to the app
35042 72 6f 70 72 69 61 74 65 20 68 61 73 68 20 66 75  ropriate hash fu
35043 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65  nction given the
35044 20 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a   key class..**.*
35045 2a 20 54 68 65 20 43 20 73 79 6e 74 61 78 20 69  * The C syntax i
35046 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
35047 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 79 20 62  definition may b
35048 65 20 75 6e 66 61 6d 69 6c 61 72 20 74 6f 20 73  e unfamilar to s
35049 6f 6d 65 20 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d  ome .** programm
3504a 65 72 73 2c 20 73 6f 20 77 65 20 70 72 6f 76 69  ers, so we provi
3504b 64 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  de the following
3504c 20 61 64 64 69 74 69 6f 6e 61 6c 20 65 78 70 6c   additional expl
3504d 61 6e 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54  anation:.**.** T
3504e 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66  he name of the f
3504f 75 6e 63 74 69 6f 6e 20 69 73 20 22 66 74 73 48  unction is "ftsH
35050 61 73 68 46 75 6e 63 74 69 6f 6e 22 2e 20 20 54  ashFunction".  T
35051 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65  he function take
35052 73 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 70 61  s a.** single pa
35053 72 61 6d 65 74 65 72 20 22 6b 65 79 43 6c 61 73  rameter "keyClas
35054 73 22 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  s".  The return 
35055 76 61 6c 75 65 20 6f 66 20 66 74 73 48 61 73 68  value of ftsHash
35056 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 20 69 73  Function().** is
35057 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
35058 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20  other function. 
35059 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74   Specifically, t
3505a 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
3505b 2a 2a 20 6f 66 20 66 74 73 48 61 73 68 46 75 6e  ** of ftsHashFun
3505c 63 74 69 6f 6e 28 29 20 69 73 20 61 20 70 6f 69  ction() is a poi
3505d 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69  nter to a functi
3505e 6f 6e 20 74 68 61 74 20 74 61 6b 65 73 20 74 77  on that takes tw
3505f 6f 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  o parameters.** 
35060 77 69 74 68 20 74 79 70 65 73 20 22 63 6f 6e 73  with types "cons
35061 74 20 76 6f 69 64 2a 22 20 61 6e 64 20 22 69 6e  t void*" and "in
35062 74 22 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  t" and returns a
35063 6e 20 22 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61 74  n "int"..*/.stat
35064 69 63 20 69 6e 74 20 28 2a 66 74 73 48 61 73 68  ic int (*ftsHash
35065 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79  Function(int key
35066 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76 6f  Class))(const vo
35067 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28 20  id*,int){.  if( 
35068 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48  keyClass==FTS3_H
35069 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20  ASH_STRING ){.  
3506a 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 53 74    return &fts3St
3506b 72 48 61 73 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rHash;.  }else{.
3506c 20 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43      assert( keyC
3506d 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f  lass==FTS3_HASH_
3506e 42 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65  BINARY );.    re
3506f 74 75 72 6e 20 26 66 74 73 33 42 69 6e 48 61 73  turn &fts3BinHas
35070 68 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  h;.  }.}../*.** 
35071 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
35072 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69   to the appropri
35073 61 74 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  ate hash functio
35074 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20  n given the key 
35075 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  class..**.** For
35076 20 68 65 6c 70 20 69 6e 20 69 6e 74 65 72 70 72   help in interpr
35077 65 74 65 64 20 74 68 65 20 6f 62 73 63 75 72 65  eted the obscure
35078 20 43 20 63 6f 64 65 20 69 6e 20 74 68 65 20 66   C code in the f
35079 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69  unction definiti
3507a 6f 6e 2c 0a 2a 2a 20 73 65 65 20 74 68 65 20 68  on,.** see the h
3507b 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e  eader comment on
3507c 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 66 75   the previous fu
3507d 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
3507e 63 20 69 6e 74 20 28 2a 66 74 73 43 6f 6d 70 61  c int (*ftsCompa
3507f 72 65 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b  reFunction(int k
35080 65 79 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20  eyClass))(const 
35081 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
35082 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66  void*,int){.  if
35083 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33  ( keyClass==FTS3
35084 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a  _HASH_STRING ){.
35085 20 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33      return &fts3
35086 53 74 72 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 65  StrCompare;.  }e
35087 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
35088 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f   keyClass==FTS3_
35089 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20  HASH_BINARY );. 
3508a 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 42     return &fts3B
3508b 69 6e 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 0a 7d  inCompare;.  }.}
3508c 0a 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65  ../* Link an ele
3508d 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61  ment into the ha
3508e 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74  sh table.*/.stat
3508f 69 63 20 76 6f 69 64 20 66 74 73 33 48 61 73 68  ic void fts3Hash
35090 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20  InsertElement(. 
35091 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20   fts3Hash *pH,  
35092 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
35093 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74   complete hash t
35094 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  able */.  struct
35095 20 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79   _fts3ht *pEntry
35096 2c 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20  ,  /* The entry 
35097 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20  into which pNew 
35098 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20  is inserted */. 
35099 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70   fts3HashElem *p
3509a 4e 65 77 20 20 20 20 20 20 20 2f 2a 20 54 68 65  New       /* The
3509b 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69   element to be i
3509c 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20  nserted */.){.  
3509d 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 48  fts3HashElem *pH
3509e 65 61 64 3b 20 20 20 20 20 2f 2a 20 46 69 72 73  ead;     /* Firs
3509f 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64  t element alread
350a0 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20  y in pEntry */. 
350a1 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d   pHead = pEntry-
350a2 3e 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70 48  >chain;.  if( pH
350a3 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d  ead ){.    pNew-
350a4 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20  >next = pHead;. 
350a5 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20     pNew->prev = 
350a6 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20  pHead->prev;.   
350a7 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76   if( pHead->prev
350a8 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d   ){ pHead->prev-
350a9 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a  >next = pNew; }.
350aa 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20      else        
350ab 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74       { pH->first
350ac 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70   = pNew; }.    p
350ad 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65  Head->prev = pNe
350ae 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  w;.  }else{.    
350af 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d  pNew->next = pH-
350b0 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20  >first;.    if( 
350b1 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d  pH->first ){ pH-
350b2 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70  >first->prev = p
350b3 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d  New; }.    pNew-
350b4 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70  >prev = 0;.    p
350b5 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b  H->first = pNew;
350b6 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63  .  }.  pEntry->c
350b7 6f 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79  ount++;.  pEntry
350b8 2d 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a  ->chain = pNew;.
350b9 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68  }.../* Resize th
350ba 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20  e hash table so 
350bb 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73  that it cantains
350bc 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b   "new_size" buck
350bd 65 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a  ets..** "new_siz
350be 65 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77  e" must be a pow
350bf 65 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68 61  er of 2.  The ha
350c0 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66  sh table might f
350c1 61 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a  ail .** to resiz
350c2 65 20 69 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  e if sqliteMallo
350c3 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74  c() fails..*/.st
350c4 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 52 65  atic void fts3Re
350c5 68 61 73 68 28 66 74 73 33 48 61 73 68 20 2a 70  hash(fts3Hash *p
350c6 48 2c 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29  H, int new_size)
350c7 7b 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33  {.  struct _fts3
350c8 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20 20  ht *new_ht;     
350c9 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
350ca 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
350cb 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c  fts3HashElem *el
350cc 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20  em, *next_elem; 
350cd 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
350ce 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 65 6c  over existing el
350cf 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  ements */.  int 
350d0 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76  (*xHash)(const v
350d1 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 20 2f 2a 20  oid*,int);   /* 
350d2 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  The hash functio
350d3 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  n */..  assert( 
350d4 28 6e 65 77 5f 73 69 7a 65 20 26 20 28 6e 65 77  (new_size & (new
350d5 5f 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a  _size-1))==0 );.
350d6 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75    new_ht = (stru
350d7 63 74 20 5f 66 74 73 33 68 74 20 2a 29 66 74 73  ct _fts3ht *)fts
350d8 33 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 65 77  3HashMalloc( new
350d9 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72  _size*sizeof(str
350da 75 63 74 20 5f 66 74 73 33 68 74 29 20 29 3b 0a  uct _fts3ht) );.
350db 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20    if( new_ht==0 
350dc 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 74 73 33  ) return;.  fts3
350dd 48 61 73 68 46 72 65 65 28 70 48 2d 3e 68 74 29  HashFree(pH->ht)
350de 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77  ;.  pH->ht = new
350df 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a  _ht;.  pH->htsiz
350e0 65 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20  e = new_size;.  
350e1 78 48 61 73 68 20 3d 20 66 74 73 48 61 73 68 46  xHash = ftsHashF
350e2 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43  unction(pH->keyC
350e3 6c 61 73 73 29 3b 0a 20 20 66 6f 72 28 65 6c 65  lass);.  for(ele
350e4 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d  m=pH->first, pH-
350e5 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20  >first=0; elem; 
350e6 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d  elem = next_elem
350e7 29 7b 0a 20 20 20 20 69 6e 74 20 68 20 3d 20 28  ){.    int h = (
350e8 2a 78 48 61 73 68 29 28 65 6c 65 6d 2d 3e 70 4b  *xHash)(elem->pK
350e9 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20  ey, elem->nKey) 
350ea 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 3b 0a  & (new_size-1);.
350eb 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20      next_elem = 
350ec 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20  elem->next;.    
350ed 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c  fts3HashInsertEl
350ee 65 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68  ement(pH, &new_h
350ef 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d  t[h], elem);.  }
350f0 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63  .}../* This func
350f1 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e  tion (for intern
350f2 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63  al use only) loc
350f3 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20  ates an element 
350f4 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61  in an.** hash ta
350f5 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65 73  ble that matches
350f6 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20   the given key. 
350f7 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68   The hash for th
350f8 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c  is key has.** al
350f9 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75  ready been compu
350fa 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65  ted and is passe
350fb 64 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72  d as the 4th par
350fc 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  ameter..*/.stati
350fd 63 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a  c fts3HashElem *
350fe 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42  fts3FindElementB
350ff 79 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 66  yHash(.  const f
35100 74 73 33 48 61 73 68 20 2a 70 48 2c 20 2f 2a 20  ts3Hash *pH, /* 
35101 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61  The pH to be sea
35102 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  rched */.  const
35103 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f   void *pKey,   /
35104 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65  * The key we are
35105 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a   searching for *
35106 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a 20 20  /.  int nKey,.  
35107 69 6e 74 20 68 20 20 20 20 20 20 20 20 20 20 20  int h           
35108 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20      /* The hash 
35109 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f  for this key. */
3510a 0a 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c  .){.  fts3HashEl
3510b 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20  em *elem;       
3510c 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20       /* Used to 
3510d 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c  loop thru the el
3510e 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20  ement list */.  
3510f 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
35110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35111 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
35112 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73  ents left to tes
35113 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f  t */.  int (*xCo
35114 6d 70 61 72 65 29 28 63 6f 6e 73 74 20 76 6f 69  mpare)(const voi
35115 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
35116 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 63 6f 6d  d*,int);  /* com
35117 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
35118 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68   */..  if( pH->h
35119 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  t ){.    struct 
3511a 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79 20  _fts3ht *pEntry 
3511b 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20  = &pH->ht[h];.  
3511c 20 20 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d    elem = pEntry-
3511d 3e 63 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e  >chain;.    coun
3511e 74 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  t = pEntry->coun
3511f 74 3b 0a 20 20 20 20 78 43 6f 6d 70 61 72 65 20  t;.    xCompare 
35120 3d 20 66 74 73 43 6f 6d 70 61 72 65 46 75 6e 63  = ftsCompareFunc
35121 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73  tion(pH->keyClas
35122 73 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63  s);.    while( c
35123 6f 75 6e 74 2d 2d 20 26 26 20 65 6c 65 6d 20 29  ount-- && elem )
35124 7b 0a 20 20 20 20 20 20 69 66 28 20 28 2a 78 43  {.      if( (*xC
35125 6f 6d 70 61 72 65 29 28 65 6c 65 6d 2d 3e 70 4b  ompare)(elem->pK
35126 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70 4b  ey,elem->nKey,pK
35127 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a  ey,nKey)==0 ){ .
35128 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 65          return e
35129 6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lem;.      }.   
3512a 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e     elem = elem->
3512b 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  next;.    }.  }.
3512c 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
3512d 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e 67 6c  * Remove a singl
3512e 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  e entry from the
3512f 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 65   hash table give
35130 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
35131 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 61  hat.** element a
35132 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65  nd a hash on the
35133 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79 2e 0a   element's key..
35134 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
35135 74 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74  ts3RemoveElement
35136 42 79 48 61 73 68 28 0a 20 20 66 74 73 33 48 61  ByHash(.  fts3Ha
35137 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20  sh *pH,         
35138 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69  /* The pH contai
35139 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20  ning "elem" */. 
3513a 20 66 74 73 33 48 61 73 68 45 6c 65 6d 2a 20 65   fts3HashElem* e
3513b 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c  lem,   /* The el
3513c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f  ement to be remo
3513d 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20  ved from the pH 
3513e 2a 2f 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20  */.  int h      
3513f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
35140 73 68 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  sh value for the
35141 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20   element */.){. 
35142 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20   struct _fts3ht 
35143 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 65  *pEntry;.  if( e
35144 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20 20  lem->prev ){.   
35145 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78   elem->prev->nex
35146 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20  t = elem->next; 
35147 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48  .  }else{.    pH
35148 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d 3e  ->first = elem->
35149 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  next;.  }.  if( 
3514a 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 20  elem->next ){.  
3514b 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72    elem->next->pr
3514c 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 3b  ev = elem->prev;
3514d 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20  .  }.  pEntry = 
3514e 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69 66  &pH->ht[h];.  if
3514f 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d  ( pEntry->chain=
35150 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45 6e  =elem ){.    pEn
35151 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65  try->chain = ele
35152 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 70  m->next;.  }.  p
35153 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a  Entry->count--;.
35154 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 6f    if( pEntry->co
35155 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70 45  unt<=0 ){.    pE
35156 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30 3b  ntry->chain = 0;
35157 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 63  .  }.  if( pH->c
35158 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e  opyKey && elem->
35159 70 4b 65 79 20 29 7b 0a 20 20 20 20 66 74 73 33  pKey ){.    fts3
3515a 48 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70  HashFree(elem->p
3515b 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 66 74 73 33  Key);.  }.  fts3
3515c 48 61 73 68 46 72 65 65 28 20 65 6c 65 6d 20 29  HashFree( elem )
3515d 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b  ;.  pH->count--;
3515e 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74  .  if( pH->count
3515f 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  <=0 ){.    asser
35160 74 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20  t( pH->first==0 
35161 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
35162 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20  H->count==0 );. 
35163 20 20 20 66 74 73 33 48 61 73 68 43 6c 65 61 72     fts3HashClear
35164 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  (pH);.  }.}../* 
35165 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74  Attempt to locat
35166 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20  e an element of 
35167 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70  the hash table p
35168 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20  H with a key.** 
35169 74 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65  that matches pKe
3516a 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20  y,nKey.  Return 
3516b 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69  the data for thi
3516c 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20  s element if it 
3516d 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20  is.** found, or 
3516e 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
3516f 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51   no match..*/.SQ
35170 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
35171 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61  d *sqlite3Fts3Ha
35172 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 66 74 73  shFind(const fts
35173 33 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74  3Hash *pH, const
35174 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
35175 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 3b   nKey){.  int h;
35176 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35177 20 2f 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65   /* A hash on ke
35178 79 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45  y */.  fts3HashE
35179 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a  lem *elem;    /*
3517a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61   The element tha
3517b 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f  t matches key */
3517c 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28  .  int (*xHash)(
3517d 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29  const void*,int)
3517e 3b 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66  ;  /* The hash f
3517f 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66  unction */..  if
35180 28 20 70 48 3d 3d 30 20 7c 7c 20 70 48 2d 3e 68  ( pH==0 || pH->h
35181 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  t==0 ) return 0;
35182 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73 48 61  .  xHash = ftsHa
35183 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b  shFunction(pH->k
35184 65 79 43 6c 61 73 73 29 3b 0a 20 20 61 73 73 65  eyClass);.  asse
35185 72 74 28 20 78 48 61 73 68 21 3d 30 20 29 3b 0a  rt( xHash!=0 );.
35186 20 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28 70    h = (*xHash)(p
35187 4b 65 79 2c 6e 4b 65 79 29 3b 0a 20 20 61 73 73  Key,nKey);.  ass
35188 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65  ert( (pH->htsize
35189 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
3518a 29 29 3d 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20  ))==0 );.  elem 
3518b 3d 20 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e  = fts3FindElemen
3518c 74 42 79 48 61 73 68 28 70 48 2c 70 4b 65 79 2c  tByHash(pH,pKey,
3518d 6e 4b 65 79 2c 20 68 20 26 20 28 70 48 2d 3e 68  nKey, h & (pH->h
3518e 74 73 69 7a 65 2d 31 29 29 3b 0a 20 20 72 65 74  tsize-1));.  ret
3518f 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d  urn elem ? elem-
35190 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a  >data : 0;.}../*
35191 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65   Insert an eleme
35192 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68  nt into the hash
35193 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20   table pH.  The 
35194 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79  key is pKey,nKey
35195 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61  .** and the data
35196 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a   is "data"..**.*
35197 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20  * If no element 
35198 65 78 69 73 74 73 20 77 69 74 68 20 61 20 6d 61  exists with a ma
35199 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e  tching key, then
3519a 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e   a new.** elemen
3519b 74 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 41  t is created.  A
3519c 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79   copy of the key
3519d 20 69 73 20 6d 61 64 65 20 69 66 20 74 68 65 20   is made if the 
3519e 63 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20  copyKey.** flag 
3519f 69 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73  is set.  NULL is
351a0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
351a1 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d   If another elem
351a2 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  ent already exis
351a3 74 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  ts with the same
351a4 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a   key, then the.*
351a5 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61  * new data repla
351a6 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61  ces the old data
351a7 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74   and the old dat
351a8 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  a is returned..*
351a9 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74  * The key is not
351aa 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20   copied in this 
351ab 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20  instance.  If a 
351ac 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68  malloc fails, th
351ad 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61  en.** the new da
351ae 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  ta is returned a
351af 6e 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  nd the hash tabl
351b0 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a  e is unchanged..
351b1 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61  **.** If the "da
351b2 74 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ta" parameter to
351b3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
351b4 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  s NULL, then the
351b5 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72  .** element corr
351b6 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65  esponding to "ke
351b7 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72  y" is removed fr
351b8 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
351b9 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
351ba 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
351bb 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
351bc 28 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48  (.  fts3Hash *pH
351bd 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
351be 68 61 73 68 20 74 61 62 6c 65 20 74 6f 20 69 6e  hash table to in
351bf 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 63  sert into */.  c
351c0 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
351c1 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a      /* The key *
351c2 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20  /.  int nKey,   
351c3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
351c4 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
351c5 68 65 20 6b 65 79 20 2a 2f 0a 20 20 76 6f 69 64  he key */.  void
351c6 20 2a 64 61 74 61 20 20 20 20 20 20 20 20 20 20   *data          
351c7 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0a   /* The data */.
351c8 29 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b 20 20  ){.  int hraw;  
351c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
351ca 2a 20 52 61 77 20 68 61 73 68 20 76 61 6c 75 65  * Raw hash value
351cb 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20   of the key */. 
351cc 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
351cd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68             /* th
351ce 65 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65  e hash of the ke
351cf 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61  y modulo hash ta
351d0 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 66 74  ble size */.  ft
351d1 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d  s3HashElem *elem
351d2 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20  ;       /* Used 
351d3 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65  to loop thru the
351d4 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f   element list */
351d5 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20  .  fts3HashElem 
351d6 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20  *new_elem;   /* 
351d7 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65  New element adde
351d8 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 20  d to the pH */. 
351d9 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f   int (*xHash)(co
351da 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
351db 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e   /* The hash fun
351dc 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65  ction */..  asse
351dd 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 78  rt( pH!=0 );.  x
351de 48 61 73 68 20 3d 20 66 74 73 48 61 73 68 46 75  Hash = ftsHashFu
351df 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c  nction(pH->keyCl
351e0 61 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ass);.  assert( 
351e1 78 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68 72  xHash!=0 );.  hr
351e2 61 77 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b  aw = (*xHash)(pK
351e3 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 61 73 73  ey, nKey);.  ass
351e4 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65  ert( (pH->htsize
351e5 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
351e6 29 29 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68  ))==0 );.  h = h
351e7 72 61 77 20 26 20 28 70 48 2d 3e 68 74 73 69 7a  raw & (pH->htsiz
351e8 65 2d 31 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66  e-1);.  elem = f
351e9 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79  ts3FindElementBy
351ea 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65  Hash(pH,pKey,nKe
351eb 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d  y,h);.  if( elem
351ec 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c   ){.    void *ol
351ed 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64  d_data = elem->d
351ee 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74  ata;.    if( dat
351ef 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74  a==0 ){.      ft
351f0 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42  s3RemoveElementB
351f1 79 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29  yHash(pH,elem,h)
351f2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
351f3 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20     elem->data = 
351f4 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20  data;.    }.    
351f5 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61 3b  return old_data;
351f6 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61 3d  .  }.  if( data=
351f7 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
351f8 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d   if( pH->htsize=
351f9 3d 30 20 29 7b 0a 20 20 20 20 66 74 73 33 52 65  =0 ){.    fts3Re
351fa 68 61 73 68 28 70 48 2c 38 29 3b 0a 20 20 20 20  hash(pH,8);.    
351fb 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d  if( pH->htsize==
351fc 30 20 29 7b 0a 20 20 20 20 20 20 70 48 2d 3e 63  0 ){.      pH->c
351fd 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  ount = 0;.      
351fe 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20  return data;.   
351ff 20 7d 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c 65   }.  }.  new_ele
35200 6d 20 3d 20 28 66 74 73 33 48 61 73 68 45 6c 65  m = (fts3HashEle
35201 6d 2a 29 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f  m*)fts3HashMallo
35202 63 28 20 73 69 7a 65 6f 66 28 66 74 73 33 48 61  c( sizeof(fts3Ha
35203 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66 28  shElem) );.  if(
35204 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20 72   new_elem==0 ) r
35205 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 69 66  eturn data;.  if
35206 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26  ( pH->copyKey &&
35207 20 70 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20 20   pKey!=0 ){.    
35208 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d  new_elem->pKey =
35209 20 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28   fts3HashMalloc(
3520a 20 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28   nKey );.    if(
3520b 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 3d   new_elem->pKey=
3520c 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33  =0 ){.      fts3
3520d 48 61 73 68 46 72 65 65 28 6e 65 77 5f 65 6c 65  HashFree(new_ele
3520e 6d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  m);.      return
3520f 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20   data;.    }.   
35210 20 6d 65 6d 63 70 79 28 28 76 6f 69 64 2a 29 6e   memcpy((void*)n
35211 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 70  ew_elem->pKey, p
35212 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 7d 65  Key, nKey);.  }e
35213 6c 73 65 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65  lse{.    new_ele
35214 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 2a  m->pKey = (void*
35215 29 70 4b 65 79 3b 0a 20 20 7d 0a 20 20 6e 65 77  )pKey;.  }.  new
35216 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b  _elem->nKey = nK
35217 65 79 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b  ey;.  pH->count+
35218 2b 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75  +;.  if( pH->cou
35219 6e 74 20 3e 20 70 48 2d 3e 68 74 73 69 7a 65 20  nt > pH->htsize 
3521a 29 7b 0a 20 20 20 20 66 74 73 33 52 65 68 61 73  ){.    fts3Rehas
3521b 68 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a 65 2a  h(pH,pH->htsize*
3521c 32 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  2);.  }.  assert
3521d 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29  ( pH->htsize>0 )
3521e 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 48 2d  ;.  assert( (pH-
3521f 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68  >htsize & (pH->h
35220 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a  tsize-1))==0 );.
35221 20 20 68 20 3d 20 68 72 61 77 20 26 20 28 70 48    h = hraw & (pH
35222 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20 66  ->htsize-1);.  f
35223 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65  ts3HashInsertEle
35224 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74  ment(pH, &pH->ht
35225 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a  [h], new_elem);.
35226 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61    new_elem->data
35227 20 3d 20 64 61 74 61 3b 0a 20 20 72 65 74 75 72   = data;.  retur
35228 6e 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  n 0;.}..#endif /
35229 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
3522a 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3522b 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3522c 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
3522d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
3522e 66 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a  f fts3_hash.c **
3522f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35231 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
35232 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
35233 20 66 69 6c 65 20 66 74 73 33 5f 70 6f 72 74 65   file fts3_porte
35234 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
35235 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35236 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
35237 20 32 30 30 36 20 53 65 70 74 65 6d 62 65 72 20   2006 September 
35238 33 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  30.**.** The aut
35239 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
3523a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
3523b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
3523c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
3523d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
3523e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
3523f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
35240 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
35241 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
35242 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
35243 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
35244 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
35245 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
35246 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
35247 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
35248 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
35249 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
3524a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3524b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3524c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3524d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3524e 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3524f 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d  ion of the full-
35250 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65  text-search toke
35251 6e 69 7a 65 72 20 74 68 61 74 20 69 6d 70 6c 65  nizer that imple
35252 6d 65 6e 74 73 0a 2a 2a 20 61 20 50 6f 72 74 65  ments.** a Porte
35253 72 20 73 74 65 6d 6d 65 72 2e 0a 2a 2f 0a 0a 2f  r stemmer..*/../
35254 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
35255 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
35256 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a  ly compiled if:.
35257 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
35258 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
35259 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e  eing built as an
3525a 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20   extension.**   
3525b 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61      (in which ca
3525c 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69  se SQLITE_CORE i
3525d 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20  s not defined), 
3525e 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54  or.**.**     * T
3525f 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69  he FTS3 module i
35260 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e  s being built in
35261 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a  to the core of.*
35262 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28  *       SQLite (
35263 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51  in which case SQ
35264 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
35265 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f   is defined)..*/
35266 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
35267 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
35268 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
35269 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a 2f 2a  BLE_FTS3)...../*
3526a 0a 2a 2a 20 43 6c 61 73 73 20 64 65 72 69 76 65  .** Class derive
3526b 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 74  d from sqlite3_t
3526c 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 74 79 70 65  okenizer.*/.type
3526d 64 65 66 20 73 74 72 75 63 74 20 70 6f 72 74 65  def struct porte
3526e 72 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20  r_tokenizer {.  
3526f 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
35270 72 20 62 61 73 65 3b 20 20 20 20 20 20 2f 2a 20  r base;      /* 
35271 42 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 7d 20  Base class */.} 
35272 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
35273 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64  ;../*.** Class d
35274 65 72 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69  erived from sqli
35275 74 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  t3_tokenizer_cur
35276 73 6f 72 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  sor.*/.typedef s
35277 74 72 75 63 74 20 70 6f 72 74 65 72 5f 74 6f 6b  truct porter_tok
35278 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a  enizer_cursor {.
35279 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3527a 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  zer_cursor base;
3527b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3527c 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20  Input;          
3527d 2f 2a 20 69 6e 70 75 74 20 77 65 20 61 72 65 20  /* input we are 
3527e 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20  tokenizing */.  
3527f 69 6e 74 20 6e 49 6e 70 75 74 3b 20 20 20 20 20  int nInput;     
35280 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35281 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75  size of the inpu
35282 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73  t */.  int iOffs
35283 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  et;             
35284 20 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70      /* current p
35285 6f 73 69 74 69 6f 6e 20 69 6e 20 7a 49 6e 70 75  osition in zInpu
35286 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65  t */.  int iToke
35287 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
35288 20 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20      /* index of 
35289 6e 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65  next token to be
3528a 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63   returned */.  c
3528b 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20  har *zToken;    
3528c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
3528d 74 6f 72 61 67 65 20 66 6f 72 20 63 75 72 72 65  torage for curre
3528e 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  nt token */.  in
3528f 74 20 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20  t nAllocated;   
35290 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70             /* sp
35291 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f  ace allocated to
35292 20 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a   zToken buffer *
35293 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  /.} porter_token
35294 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f  izer_cursor;.../
35295 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
35296 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20  ation */.static 
35297 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
35298 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 70  kenizer_module p
35299 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f  orterTokenizerMo
3529a 64 75 6c 65 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72  dule;.../*.** Cr
3529b 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e  eate a new token
3529c 69 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a  izer instance..*
3529d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72  /.static int por
3529e 74 65 72 43 72 65 61 74 65 28 0a 20 20 69 6e 74  terCreate(.  int
3529f 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
352a0 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
352a1 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
352a2 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a  izer **ppTokeniz
352a3 65 72 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74  er.){.  porter_t
352a4 6f 6b 65 6e 69 7a 65 72 20 2a 74 3b 0a 20 20 74  okenizer *t;.  t
352a5 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e   = (porter_token
352a6 69 7a 65 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  izer *) sqlite3_
352a7 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 74  malloc(sizeof(*t
352a8 29 29 3b 0a 20 20 69 66 28 20 74 3d 3d 4e 55 4c  ));.  if( t==NUL
352a9 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
352aa 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65  E_NOMEM;.  memse
352ab 74 28 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  t(t, 0, sizeof(*
352ac 74 29 29 3b 0a 20 20 2a 70 70 54 6f 6b 65 6e 69  t));.  *ppTokeni
352ad 7a 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b 0a  zer = &t->base;.
352ae 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
352af 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73  OK;.}../*.** Des
352b0 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72  troy a tokenizer
352b1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
352b2 6f 72 74 65 72 44 65 73 74 72 6f 79 28 73 71 6c  orterDestroy(sql
352b3 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
352b4 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 73  pTokenizer){.  s
352b5 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f 6b  qlite3_free(pTok
352b6 65 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75 72  enizer);.  retur
352b7 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
352b8 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f  /*.** Prepare to
352b9 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e   begin tokenizin
352ba 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73  g a particular s
352bb 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75  tring.  The inpu
352bc 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62  t.** string to b
352bd 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 7a  e tokenized is z
352be 49 6e 70 75 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d  Input[0..nInput-
352bf 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a  1].  A cursor.**
352c0 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65   used to increme
352c1 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20  ntally tokenize 
352c2 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 72  this string is r
352c3 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a  eturned in .** *
352c4 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61  ppCursor..*/.sta
352c5 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 4f 70  tic int porterOp
352c6 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  en(.  sqlite3_to
352c7 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
352c8 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  zer,         /* 
352c9 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  The tokenizer */
352ca 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
352cb 49 6e 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75  Input, int nInpu
352cc 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72  t,        /* Str
352cd 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69  ing to be tokeni
352ce 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  zed */.  sqlite3
352cf 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
352d0 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20  r **ppCursor    
352d1 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61  /* OUT: Tokeniza
352d2 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29  tion cursor */.)
352d3 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  {.  porter_token
352d4 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b 0a  izer_cursor *c;.
352d5 0a 20 20 63 20 3d 20 28 70 6f 72 74 65 72 5f 74  .  c = (porter_t
352d6 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
352d7 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
352d8 63 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b 0a 20  c(sizeof(*c));. 
352d9 20 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29 20 72   if( c==NULL ) r
352da 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
352db 45 4d 3b 0a 0a 20 20 63 2d 3e 7a 49 6e 70 75 74  EM;..  c->zInput
352dc 20 3d 20 7a 49 6e 70 75 74 3b 0a 20 20 69 66 28   = zInput;.  if(
352dd 20 7a 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20   zInput==0 ){.  
352de 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 30 3b    c->nInput = 0;
352df 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 6e  .  }else if( nIn
352e0 70 75 74 3c 30 20 29 7b 0a 20 20 20 20 63 2d 3e  put<0 ){.    c->
352e1 6e 49 6e 70 75 74 20 3d 20 28 69 6e 74 29 73 74  nInput = (int)st
352e2 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20  rlen(zInput);.  
352e3 7d 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e 6e 49  }else{.    c->nI
352e4 6e 70 75 74 20 3d 20 6e 49 6e 70 75 74 3b 0a 20  nput = nInput;. 
352e5 20 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74 20   }.  c->iOffset 
352e6 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
352e7 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20 74 6f       /* start to
352e8 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68 65 20  kenizing at the 
352e9 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63  beginning */.  c
352ea 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20  ->iToken = 0;.  
352eb 63 2d 3e 7a 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c  c->zToken = NULL
352ec 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
352ed 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c 6f  /* no space allo
352ee 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0a 20  cated, yet. */. 
352ef 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d   c->nAllocated =
352f0 20 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72   0;..  *ppCursor
352f1 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20 72   = &c->base;.  r
352f2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
352f3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
352f4 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  a tokenization c
352f5 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c 79  ursor previously
352f6 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61 6c   opened by a cal
352f7 6c 20 74 6f 0a 2a 2a 20 70 6f 72 74 65 72 4f 70  l to.** porterOp
352f8 65 6e 28 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73  en() above..*/.s
352f9 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72  tatic int porter
352fa 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 74 6f  Close(sqlite3_to
352fb 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
352fc 70 43 75 72 73 6f 72 29 7b 0a 20 20 70 6f 72 74  pCursor){.  port
352fd 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
352fe 73 6f 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72  sor *c = (porter
352ff 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
35300 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
35301 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 2d 3e  sqlite3_free(c->
35302 7a 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74  zToken);.  sqlit
35303 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20 72 65  e3_free(c);.  re
35304 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
35305 7d 0a 2f 2a 0a 2a 2a 20 56 6f 77 65 6c 20 6f 72  }./*.** Vowel or
35306 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2f 0a 73 74   consonant.*/.st
35307 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
35308 63 54 79 70 65 5b 5d 20 3d 20 7b 0a 20 20 20 30  cType[] = {.   0
35309 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c  , 1, 1, 1, 0, 1,
3530a 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 1, 0, 1, 1, 
3530b 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31  1, 1, 1, 0, 1, 1
3530c 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 0a 20 20  , 1, 1, 1, 0,.  
3530d 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20 31 0a 7d   1, 1, 1, 2, 1.}
3530e 3b 0a 0a 2f 2a 0a 2a 2a 20 69 73 43 6f 6e 73 6f  ;../*.** isConso
3530f 6e 61 6e 74 28 29 20 61 6e 64 20 69 73 56 6f 77  nant() and isVow
35310 65 6c 28 29 20 64 65 74 65 72 6d 69 6e 65 20 69  el() determine i
35311 66 20 74 68 65 69 72 20 66 69 72 73 74 20 63 68  f their first ch
35312 61 72 61 63 74 65 72 20 69 6e 0a 2a 2a 20 74 68  aracter in.** th
35313 65 20 73 74 72 69 6e 67 20 74 68 65 79 20 70 6f  e string they po
35314 69 6e 74 20 74 6f 20 69 73 20 61 20 63 6f 6e 73  int to is a cons
35315 6f 6e 61 6e 74 20 6f 72 20 61 20 76 6f 77 65 6c  onant or a vowel
35316 2c 20 61 63 63 6f 72 64 69 6e 67 0a 2a 2a 20 74  , according.** t
35317 6f 20 50 6f 72 74 65 72 20 72 75 6c 73 2e 20 20  o Porter ruls.  
35318 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 6e 73 6f 6e 61  .**.** A consona
35319 74 65 20 69 73 20 61 6e 79 20 6c 65 74 74 65 72  te is any letter
3531a 20 6f 74 68 65 72 20 74 68 61 6e 20 27 61 27 2c   other than 'a',
3531b 20 27 65 27 2c 20 27 69 27 2c 20 27 6f 27 2c 20   'e', 'i', 'o', 
3531c 6f 72 20 27 75 27 2e 0a 2a 2a 20 27 59 27 20 69  or 'u'..** 'Y' i
3531d 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e  s a consonant un
3531e 6c 65 73 73 20 69 74 20 66 6f 6c 6c 6f 77 73 20  less it follows 
3531f 61 6e 6f 74 68 65 72 20 63 6f 6e 73 6f 6e 61 6e  another consonan
35320 74 2c 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63  t,.** in which c
35321 61 73 65 20 69 74 20 69 73 20 61 20 76 6f 77 65  ase it is a vowe
35322 6c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 73  l..**.** In thes
35323 65 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 6c  e routine, the l
35324 65 74 74 65 72 73 20 61 72 65 20 69 6e 20 72 65  etters are in re
35325 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20 53 6f  verse order.  So
35326 20 74 68 65 20 27 79 27 20 72 75 6c 65 0a 2a 2a   the 'y' rule.**
35327 20 69 73 20 74 68 61 74 20 27 79 27 20 69 73 20   is that 'y' is 
35328 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65  a consonant unle
35329 73 73 20 69 74 20 69 73 20 66 6f 6c 6c 6f 77 65  ss it is followe
3532a 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20  d by another.** 
3532b 63 6f 6e 73 6f 6e 65 6e 74 2e 0a 2a 2f 0a 73 74  consonent..*/.st
3532c 61 74 69 63 20 69 6e 74 20 69 73 56 6f 77 65 6c  atic int isVowel
3532d 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 73  (const char*);.s
3532e 74 61 74 69 63 20 69 6e 74 20 69 73 43 6f 6e 73  tatic int isCons
3532f 6f 6e 61 6e 74 28 63 6f 6e 73 74 20 63 68 61 72  onant(const char
35330 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20   *z){.  int j;. 
35331 20 63 68 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20   char x = *z;.  
35332 69 66 28 20 78 3d 3d 30 20 29 20 72 65 74 75 72  if( x==0 ) retur
35333 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 78  n 0;.  assert( x
35334 3e 3d 27 61 27 20 26 26 20 78 3c 3d 27 7a 27 20  >='a' && x<='z' 
35335 29 3b 0a 20 20 6a 20 3d 20 63 54 79 70 65 5b 78  );.  j = cType[x
35336 2d 27 61 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32  -'a'];.  if( j<2
35337 20 29 20 72 65 74 75 72 6e 20 6a 3b 0a 20 20 72   ) return j;.  r
35338 65 74 75 72 6e 20 7a 5b 31 5d 3d 3d 30 20 7c 7c  eturn z[1]==0 ||
35339 20 69 73 56 6f 77 65 6c 28 7a 20 2b 20 31 29 3b   isVowel(z + 1);
3533a 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  .}.static int is
3533b 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72  Vowel(const char
3533c 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20   *z){.  int j;. 
3533d 20 63 68 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20   char x = *z;.  
3533e 69 66 28 20 78 3d 3d 30 20 29 20 72 65 74 75 72  if( x==0 ) retur
3533f 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 78  n 0;.  assert( x
35340 3e 3d 27 61 27 20 26 26 20 78 3c 3d 27 7a 27 20  >='a' && x<='z' 
35341 29 3b 0a 20 20 6a 20 3d 20 63 54 79 70 65 5b 78  );.  j = cType[x
35342 2d 27 61 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32  -'a'];.  if( j<2
35343 20 29 20 72 65 74 75 72 6e 20 31 2d 6a 3b 0a 20   ) return 1-j;. 
35344 20 72 65 74 75 72 6e 20 69 73 43 6f 6e 73 6f 6e   return isConson
35345 61 6e 74 28 7a 20 2b 20 31 29 3b 0a 7d 0a 0a 2f  ant(z + 1);.}../
35346 2a 0a 2a 2a 20 4c 65 74 20 61 6e 79 20 73 65 71  *.** Let any seq
35347 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20  uence of one or 
35348 6d 6f 72 65 20 76 6f 77 65 6c 73 20 62 65 20 72  more vowels be r
35349 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 56 20  epresented by V 
3534a 61 6e 64 20 6c 65 74 0a 2a 2a 20 43 20 62 65 20  and let.** C be 
3534b 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20  sequence of one 
3534c 6f 72 20 6d 6f 72 65 20 63 6f 6e 73 6f 6e 61 6e  or more consonan
3534d 74 73 2e 20 20 54 68 65 6e 20 65 76 65 72 79 20  ts.  Then every 
3534e 77 6f 72 64 20 63 61 6e 20 62 65 0a 2a 2a 20 72  word can be.** r
3534f 65 70 72 65 73 65 6e 74 65 64 20 61 73 3a 0a 2a  epresented as:.*
35350 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b  *.**           [
35351 43 5d 20 28 56 43 29 7b 6d 7d 20 5b 56 5d 0a 2a  C] (VC){m} [V].*
35352 2a 0a 2a 2a 20 49 6e 20 70 72 6f 73 65 3a 20 20  *.** In prose:  
35353 41 20 77 6f 72 64 20 69 73 20 61 6e 20 6f 70 74  A word is an opt
35354 69 6f 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20  ional consonant 
35355 66 6f 6c 6c 6f 77 65 64 20 62 79 20 7a 65 72 6f  followed by zero
35356 20 6f 72 0a 2a 2a 20 76 6f 77 65 6c 2d 63 6f 6e   or.** vowel-con
35357 73 6f 6e 61 6e 74 20 70 61 69 72 73 20 66 6f 6c  sonant pairs fol
35358 6c 6f 77 65 64 20 62 79 20 61 6e 20 6f 70 74 69  lowed by an opti
35359 6f 6e 61 6c 20 76 6f 77 65 6c 2e 20 20 22 6d 22  onal vowel.  "m"
3535a 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65   is the.** numbe
3535b 72 20 6f 66 20 76 6f 77 65 6c 20 63 6f 6e 73 6f  r of vowel conso
3535c 6e 61 6e 74 20 70 61 69 72 73 2e 20 20 54 68 69  nant pairs.  Thi
3535d 73 20 72 6f 75 74 69 6e 65 20 63 6f 6d 70 75 74  s routine comput
3535e 65 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  es the value.** 
3535f 6f 66 20 6d 20 66 6f 72 20 74 68 65 20 66 69 72  of m for the fir
35360 73 74 20 69 20 62 79 74 65 73 20 6f 66 20 61 20  st i bytes of a 
35361 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  word..**.** Retu
35362 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 6d  rn true if the m
35363 2d 76 61 6c 75 65 20 66 6f 72 20 7a 20 69 73 20  -value for z is 
35364 31 20 6f 72 20 6d 6f 72 65 2e 20 20 49 6e 20 6f  1 or more.  In o
35365 74 68 65 72 20 77 6f 72 64 73 2c 0a 2a 2a 20 72  ther words,.** r
35366 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 7a 20  eturn true if z 
35367 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73  contains at leas
35368 74 20 6f 6e 65 20 76 6f 77 65 6c 20 74 68 61 74  t one vowel that
35369 20 69 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20   is followed.** 
3536a 62 79 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a  by a consonant..
3536b 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f  **.** In this ro
3536c 75 74 69 6e 65 20 7a 5b 5d 20 69 73 20 69 6e 20  utine z[] is in 
3536d 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20  reverse order.  
3536e 53 6f 20 77 65 20 61 72 65 20 72 65 61 6c 6c 79  So we are really
3536f 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20   looking.** for 
35370 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 6f  an instance of o
35371 66 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f  f a consonant fo
35372 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 6f 77 65  llowed by a vowe
35373 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
35374 20 6d 5f 67 74 5f 30 28 63 6f 6e 73 74 20 63 68   m_gt_0(const ch
35375 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
35376 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
35377 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
35378 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
35379 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
3537a 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
3537b 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a    return *z!=0;.
3537c 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20  }../* Like mgt0 
3537d 61 62 6f 76 65 20 65 78 63 65 70 74 20 77 65 20  above except we 
3537e 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  are looking for 
3537f 61 20 76 61 6c 75 65 20 6f 66 20 6d 20 77 68 69  a value of m whi
35380 63 68 20 69 73 0a 2a 2a 20 65 78 61 63 74 6c 79  ch is.** exactly
35381 20 31 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   1.*/.static int
35382 20 6d 5f 65 71 5f 31 28 63 6f 6e 73 74 20 63 68   m_eq_1(const ch
35383 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
35384 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
35385 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
35386 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
35387 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
35388 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
35389 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65    if( *z==0 ) re
3538a 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28  turn 0;.  while(
3538b 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
3538c 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
3538d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
3538e 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
3538f 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
35390 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a    return *z==0;.
35391 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20  }../* Like mgt0 
35392 61 62 6f 76 65 20 65 78 63 65 70 74 20 77 65 20  above except we 
35393 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  are looking for 
35394 61 20 76 61 6c 75 65 20 6f 66 20 6d 3e 31 20 69  a value of m>1 i
35395 6e 73 74 65 61 64 0a 2a 2a 20 6f 72 20 6d 3e 30  nstead.** or m>0
35396 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
35397 5f 67 74 5f 31 28 63 6f 6e 73 74 20 63 68 61 72  _gt_1(const char
35398 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69   *z){.  while( i
35399 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b  sVowel(z) ){ z++
3539a 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
3539b 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
3539c 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
3539d 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
3539e 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75  if( *z==0 ) retu
3539f 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69  rn 0;.  while( i
353a0 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b  sVowel(z) ){ z++
353a1 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
353a2 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
353a3 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
353a4 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
353a5 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a  return *z!=0;.}.
353a6 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
353a7 55 45 20 69 66 20 74 68 65 72 65 20 69 73 20 61  UE if there is a
353a8 20 76 6f 77 65 6c 20 61 6e 79 77 68 65 72 65 20   vowel anywhere 
353a9 77 69 74 68 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d  within z[0..n-1]
353aa 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
353ab 61 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68  asVowel(const ch
353ac 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
353ad 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20   isConsonant(z) 
353ae 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75  ){ z++; }.  retu
353af 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  rn *z!=0;.}../*.
353b0 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
353b1 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64 73 20  f the word ends 
353b2 69 6e 20 61 20 64 6f 75 62 6c 65 20 63 6f 6e 73  in a double cons
353b3 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  onant..**.** The
353b4 20 74 65 78 74 20 69 73 20 72 65 76 65 72 73 65   text is reverse
353b5 64 20 68 65 72 65 2e 20 53 6f 20 77 65 20 61 72  d here. So we ar
353b6 65 20 72 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67  e really looking
353b7 20 61 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74   at.** the first
353b8 20 74 77 6f 20 63 68 61 72 61 63 74 65 72 73 20   two characters 
353b9 6f 66 20 7a 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69  of z[]..*/.stati
353ba 63 20 69 6e 74 20 64 6f 75 62 6c 65 43 6f 6e 73  c int doubleCons
353bb 6f 6e 61 6e 74 28 63 6f 6e 73 74 20 63 68 61 72  onant(const char
353bc 20 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 69   *z){.  return i
353bd 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26  sConsonant(z) &&
353be 20 7a 5b 30 5d 3d 3d 7a 5b 31 5d 20 26 26 20 69   z[0]==z[1] && i
353bf 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 31 29 3b  sConsonant(z+1);
353c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
353c1 20 54 52 55 45 20 69 66 20 74 68 65 20 77 6f 72   TRUE if the wor
353c2 64 20 65 6e 64 73 20 77 69 74 68 20 74 68 72 65  d ends with thre
353c3 65 20 6c 65 74 74 65 72 73 20 77 68 69 63 68 0a  e letters which.
353c4 2a 2a 20 61 72 65 20 63 6f 6e 73 6f 6e 61 6e 74  ** are consonant
353c5 2d 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 65 6e 74  -vowel-consonent
353c6 20 61 6e 64 20 77 68 65 72 65 20 74 68 65 20 66   and where the f
353c7 69 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a  inal consonant.*
353c8 2a 20 69 73 20 6e 6f 74 20 27 77 27 2c 20 27 78  * is not 'w', 'x
353c9 27 2c 20 6f 72 20 27 79 27 2e 0a 2a 2a 0a 2a 2a  ', or 'y'..**.**
353ca 20 54 68 65 20 77 6f 72 64 20 69 73 20 72 65 76   The word is rev
353cb 65 72 73 65 64 20 68 65 72 65 2e 20 20 53 6f 20  ersed here.  So 
353cc 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20 63 68  we are really ch
353cd 65 63 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69  ecking the.** fi
353ce 72 73 74 20 74 68 72 65 65 20 6c 65 74 74 65 72  rst three letter
353cf 73 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20  s and the first 
353d0 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e  one cannot be in
353d1 20 5b 77 78 79 5d 2e 0a 2a 2f 0a 73 74 61 74 69   [wxy]..*/.stati
353d2 63 20 69 6e 74 20 73 74 61 72 5f 6f 68 28 63 6f  c int star_oh(co
353d3 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
353d4 72 65 74 75 72 6e 0a 20 20 20 20 7a 5b 30 5d 21  return.    z[0]!
353d5 3d 30 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e  =0 && isConsonan
353d6 74 28 7a 29 20 26 26 0a 20 20 20 20 7a 5b 30 5d  t(z) &&.    z[0]
353d7 21 3d 27 77 27 20 26 26 20 7a 5b 30 5d 21 3d 27  !='w' && z[0]!='
353d8 78 27 20 26 26 20 7a 5b 30 5d 21 3d 27 79 27 20  x' && z[0]!='y' 
353d9 26 26 0a 20 20 20 20 7a 5b 31 5d 21 3d 30 20 26  &&.    z[1]!=0 &
353da 26 20 69 73 56 6f 77 65 6c 28 7a 2b 31 29 20 26  & isVowel(z+1) &
353db 26 0a 20 20 20 20 7a 5b 32 5d 21 3d 30 20 26 26  &.    z[2]!=0 &&
353dc 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 32   isConsonant(z+2
353dd 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  );.}../*.** If t
353de 68 65 20 77 6f 72 64 20 65 6e 64 73 20 77 69 74  he word ends wit
353df 68 20 7a 46 72 6f 6d 20 61 6e 64 20 78 43 6f 6e  h zFrom and xCon
353e0 64 28 29 20 69 73 20 74 72 75 65 20 66 6f 72 20  d() is true for 
353e1 74 68 65 20 73 74 65 6d 0a 2a 2a 20 6f 66 20 74  the stem.** of t
353e2 68 65 20 77 6f 72 64 20 74 68 61 74 20 70 72 65  he word that pre
353e3 63 65 65 64 73 20 74 68 65 20 7a 46 72 6f 6d 20  ceeds the zFrom 
353e4 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 63 68 61  ending, then cha
353e5 6e 67 65 20 74 68 65 20 0a 2a 2a 20 65 6e 64 69  nge the .** endi
353e6 6e 67 20 74 6f 20 7a 54 6f 2e 0a 2a 2a 0a 2a 2a  ng to zTo..**.**
353e7 20 54 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20   The input word 
353e8 2a 70 7a 20 61 6e 64 20 7a 46 72 6f 6d 20 61 72  *pz and zFrom ar
353e9 65 20 62 6f 74 68 20 69 6e 20 72 65 76 65 72 73  e both in revers
353ea 65 20 6f 72 64 65 72 2e 20 20 7a 54 6f 0a 2a 2a  e order.  zTo.**
353eb 20 69 73 20 69 6e 20 6e 6f 72 6d 61 6c 20 6f 72   is in normal or
353ec 64 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 74 75  der. .**.** Retu
353ed 72 6e 20 54 52 55 45 20 69 66 20 7a 46 72 6f 6d  rn TRUE if zFrom
353ee 20 6d 61 74 63 68 65 73 2e 20 20 52 65 74 75 72   matches.  Retur
353ef 6e 20 46 41 4c 53 45 20 69 66 20 7a 46 72 6f 6d  n FALSE if zFrom
353f0 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74   does not.** mat
353f1 63 68 2e 20 20 4e 6f 74 20 74 68 61 74 20 54 52  ch.  Not that TR
353f2 55 45 20 69 73 20 72 65 74 75 72 6e 65 64 20 65  UE is returned e
353f3 76 65 6e 20 69 66 20 78 43 6f 6e 64 28 29 20 66  ven if xCond() f
353f4 61 69 6c 73 20 61 6e 64 0a 2a 2a 20 6e 6f 20 73  ails and.** no s
353f5 75 62 73 74 69 74 75 74 69 6f 6e 20 6f 63 63 75  ubstitution occu
353f6 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rs..*/.static in
353f7 74 20 73 74 65 6d 28 0a 20 20 63 68 61 72 20 2a  t stem(.  char *
353f8 2a 70 7a 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pz,            
353f9 20 2f 2a 20 54 68 65 20 77 6f 72 64 20 62 65 69   /* The word bei
353fa 6e 67 20 73 74 65 6d 6d 65 64 20 28 52 65 76 65  ng stemmed (Reve
353fb 72 73 65 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74  rsed) */.  const
353fc 20 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 20 20   char *zFrom,   
353fd 20 20 2f 2a 20 49 66 20 74 68 65 20 65 6e 64 69    /* If the endi
353fe 6e 67 20 6d 61 74 63 68 65 73 20 74 68 69 73 2e  ng matches this.
353ff 2e 2e 20 28 52 65 76 65 72 73 65 64 29 20 2a 2f  .. (Reversed) */
35400 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
35401 54 6f 2c 20 20 20 20 20 20 20 2f 2a 20 2e 2e 2e  To,       /* ...
35402 20 63 68 61 6e 67 65 20 74 68 65 20 65 6e 64 69   change the endi
35403 6e 67 20 74 6f 20 74 68 69 73 20 28 6e 6f 74 20  ng to this (not 
35404 72 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20 69  reversed) */.  i
35405 6e 74 20 28 2a 78 43 6f 6e 64 29 28 63 6f 6e 73  nt (*xCond)(cons
35406 74 20 63 68 61 72 2a 29 20 20 20 2f 2a 20 43 6f  t char*)   /* Co
35407 6e 64 69 74 69 6f 6e 20 74 68 61 74 20 6d 75 73  ndition that mus
35408 74 20 62 65 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  t be true */.){.
35409 20 20 63 68 61 72 20 2a 7a 20 3d 20 2a 70 7a 3b    char *z = *pz;
3540a 0a 20 20 77 68 69 6c 65 28 20 2a 7a 46 72 6f 6d  .  while( *zFrom
3540b 20 26 26 20 2a 7a 46 72 6f 6d 3d 3d 2a 7a 20 29   && *zFrom==*z )
3540c 7b 20 7a 2b 2b 3b 20 7a 46 72 6f 6d 2b 2b 3b 20  { z++; zFrom++; 
3540d 7d 0a 20 20 69 66 28 20 2a 7a 46 72 6f 6d 21 3d  }.  if( *zFrom!=
3540e 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
3540f 69 66 28 20 78 43 6f 6e 64 20 26 26 20 21 78 43  if( xCond && !xC
35410 6f 6e 64 28 7a 29 20 29 20 72 65 74 75 72 6e 20  ond(z) ) return 
35411 31 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 54 6f  1;.  while( *zTo
35412 20 29 7b 0a 20 20 20 20 2a 28 2d 2d 7a 29 20 3d   ){.    *(--z) =
35413 20 2a 28 7a 54 6f 2b 2b 29 3b 0a 20 20 7d 0a 20   *(zTo++);.  }. 
35414 20 2a 70 7a 20 3d 20 7a 3b 0a 20 20 72 65 74 75   *pz = z;.  retu
35415 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 1;.}../*.** T
35416 68 69 73 20 69 73 20 74 68 65 20 66 61 6c 6c 62  his is the fallb
35417 61 63 6b 20 73 74 65 6d 6d 65 72 20 75 73 65 64  ack stemmer used
35418 20 77 68 65 6e 20 74 68 65 20 70 6f 72 74 65 72   when the porter
35419 20 73 74 65 6d 6d 65 72 20 69 73 0a 2a 2a 20 69   stemmer is.** i
3541a 6e 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20 54  nappropriate.  T
3541b 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 69 73  he input word is
3541c 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
3541d 20 6f 75 74 70 75 74 20 77 69 74 68 0a 2a 2a 20   output with.** 
3541e 55 53 2d 41 53 43 49 49 20 63 61 73 65 20 66 6f  US-ASCII case fo
3541f 6c 64 69 6e 67 2e 20 20 49 66 20 74 68 65 20 69  lding.  If the i
35420 6e 70 75 74 20 77 6f 72 64 20 69 73 20 74 6f 6f  nput word is too
35421 20 6c 6f 6e 67 20 28 6d 6f 72 65 0a 2a 2a 20 74   long (more.** t
35422 68 61 6e 20 32 30 20 62 79 74 65 73 20 69 66 20  han 20 bytes if 
35423 69 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64  it contains no d
35424 69 67 69 74 73 20 6f 72 20 6d 6f 72 65 20 74 68  igits or more th
35425 61 6e 20 36 20 62 79 74 65 73 20 69 66 0a 2a 2a  an 6 bytes if.**
35426 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 64 69 67   it contains dig
35427 69 74 73 29 20 74 68 65 6e 20 77 6f 72 64 20 69  its) then word i
35428 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 32  s truncated to 2
35429 30 20 6f 72 20 36 20 62 79 74 65 73 0a 2a 2a 20  0 or 6 bytes.** 
3542a 62 79 20 74 61 6b 69 6e 67 20 31 30 20 6f 72 20  by taking 10 or 
3542b 33 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65  3 bytes from the
3542c 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65   beginning and e
3542d 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nd..*/.static vo
3542e 69 64 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28  id copy_stemmer(
3542f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c  const char *zIn,
35430 20 69 6e 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a   int nIn, char *
35431 7a 4f 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74  zOut, int *pnOut
35432 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6d 78 2c 20  ){.  int i, mx, 
35433 6a 3b 0a 20 20 69 6e 74 20 68 61 73 44 69 67 69  j;.  int hasDigi
35434 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  t = 0;.  for(i=0
35435 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 29 7b 0a 20  ; i<nIn; i++){. 
35436 20 20 20 69 6e 74 20 63 20 3d 20 7a 49 6e 5b 69     int c = zIn[i
35437 5d 3b 0a 20 20 20 20 69 66 28 20 63 3e 3d 27 41  ];.    if( c>='A
35438 27 20 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a 20  ' && c<='Z' ){. 
35439 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 63       zOut[i] = c
3543a 20 2d 20 27 41 27 20 2b 20 27 61 27 3b 0a 20 20   - 'A' + 'a';.  
3543b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
3543c 66 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d  f( c>='0' && c<=
3543d 27 39 27 20 29 20 68 61 73 44 69 67 69 74 20 3d  '9' ) hasDigit =
3543e 20 31 3b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69   1;.      zOut[i
3543f 5d 20 3d 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  ] = c;.    }.  }
35440 0a 20 20 6d 78 20 3d 20 68 61 73 44 69 67 69 74  .  mx = hasDigit
35441 20 3f 20 33 20 3a 20 31 30 3b 0a 20 20 69 66 28   ? 3 : 10;.  if(
35442 20 6e 49 6e 3e 6d 78 2a 32 20 29 7b 0a 20 20 20   nIn>mx*2 ){.   
35443 20 66 6f 72 28 6a 3d 6d 78 2c 20 69 3d 6e 49 6e   for(j=mx, i=nIn
35444 2d 6d 78 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c  -mx; i<nIn; i++,
35445 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 4f 75   j++){.      zOu
35446 74 5b 6a 5d 20 3d 20 7a 4f 75 74 5b 69 5d 3b 0a  t[j] = zOut[i];.
35447 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 6a 3b      }.    i = j;
35448 0a 20 20 7d 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d  .  }.  zOut[i] =
35449 20 30 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69   0;.  *pnOut = i
3544a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 6d  ;.}.../*.** Stem
3544b 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20   the input word 
3544c 7a 49 6e 5b 30 2e 2e 6e 49 6e 2d 31 5d 2e 20 20  zIn[0..nIn-1].  
3544d 53 74 6f 72 65 20 74 68 65 20 6f 75 74 70 75 74  Store the output
3544e 20 69 6e 20 7a 4f 75 74 2e 0a 2a 2a 20 7a 4f 75   in zOut..** zOu
3544f 74 20 69 73 20 61 74 20 6c 65 61 73 74 20 62 69  t is at least bi
35450 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  g enough to hold
35451 20 6e 49 6e 20 62 79 74 65 73 2e 20 20 57 72 69   nIn bytes.  Wri
35452 74 65 20 74 68 65 20 61 63 74 75 61 6c 0a 2a 2a  te the actual.**
35453 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74   size of the out
35454 70 75 74 20 77 6f 72 64 20 28 65 78 63 6c 75 73  put word (exclus
35455 69 76 65 20 6f 66 20 74 68 65 20 27 5c 30 27 20  ive of the '\0' 
35456 74 65 72 6d 69 6e 61 74 6f 72 29 20 69 6e 74 6f  terminator) into
35457 20 2a 70 6e 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41   *pnOut..**.** A
35458 6e 79 20 75 70 70 65 72 2d 63 61 73 65 20 63 68  ny upper-case ch
35459 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20  aracters in the 
3545a 55 53 2d 41 53 43 49 49 20 63 68 61 72 61 63 74  US-ASCII charact
3545b 65 72 20 73 65 74 20 28 5b 41 2d 5a 5d 29 0a 2a  er set ([A-Z]).*
3545c 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20  * are converted 
3545d 74 6f 20 6c 6f 77 65 72 20 63 61 73 65 2e 20 20  to lower case.  
3545e 55 70 70 65 72 2d 63 61 73 65 20 55 54 46 20 63  Upper-case UTF c
3545f 68 61 72 61 63 74 65 72 73 20 61 72 65 0a 2a 2a  haracters are.**
35460 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a   unchanged..**.*
35461 2a 20 57 6f 72 64 73 20 74 68 61 74 20 61 72 65  * Words that are
35462 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 62 6f   longer than abo
35463 75 74 20 32 30 20 62 79 74 65 73 20 61 72 65 20  ut 20 bytes are 
35464 73 74 65 6d 6d 65 64 20 62 79 20 72 65 74 61 69  stemmed by retai
35465 6e 69 6e 67 0a 2a 2a 20 61 20 66 65 77 20 62 79  ning.** a few by
35466 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67  tes from the beg
35467 69 6e 6e 69 6e 67 20 61 6e 64 20 74 68 65 20 65  inning and the e
35468 6e 64 20 6f 66 20 74 68 65 20 77 6f 72 64 2e 20  nd of the word. 
35469 20 49 66 20 74 68 65 0a 2a 2a 20 77 6f 72 64 20   If the.** word 
3546a 63 6f 6e 74 61 69 6e 73 20 64 69 67 69 74 73 2c  contains digits,
3546b 20 33 20 62 79 74 65 73 20 61 72 65 20 74 61 6b   3 bytes are tak
3546c 65 6e 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  en from the begi
3546d 6e 6e 69 6e 67 20 61 6e 64 0a 2a 2a 20 33 20 62  nning and.** 3 b
3546e 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e  ytes from the en
3546f 64 2e 20 20 46 6f 72 20 6c 6f 6e 67 20 77 6f 72  d.  For long wor
35470 64 73 20 77 69 74 68 6f 75 74 20 64 69 67 69 74  ds without digit
35471 73 2c 20 31 30 20 62 79 74 65 73 0a 2a 2a 20 61  s, 10 bytes.** a
35472 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 65 61  re taken from ea
35473 63 68 20 65 6e 64 2e 20 20 55 53 2d 41 53 43 49  ch end.  US-ASCI
35474 49 20 63 61 73 65 20 66 6f 6c 64 69 6e 67 20 73  I case folding s
35475 74 69 6c 6c 20 61 70 70 6c 69 65 73 2e 0a 2a 2a  till applies..**
35476 20 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75   .** If the inpu
35477 74 20 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20  t word contains 
35478 6e 6f 74 20 64 69 67 69 74 73 20 62 75 74 20 64  not digits but d
35479 6f 65 73 20 63 68 61 72 61 63 74 65 72 73 20 6e  oes characters n
3547a 6f 74 20 0a 2a 2a 20 69 6e 20 5b 61 2d 7a 41 2d  ot .** in [a-zA-
3547b 5a 5d 20 74 68 65 6e 20 6e 6f 20 73 74 65 6d 6d  Z] then no stemm
3547c 69 6e 67 20 69 73 20 61 74 74 65 6d 70 74 65 64  ing is attempted
3547d 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
3547e 65 20 6a 75 73 74 20 0a 2a 2a 20 63 6f 70 69 65  e just .** copie
3547f 73 20 74 68 65 20 69 6e 70 75 74 20 69 6e 74 6f  s the input into
35480 20 74 68 65 20 69 6e 70 75 74 20 69 6e 74 6f 20   the input into 
35481 74 68 65 20 6f 75 74 70 75 74 20 77 69 74 68 20  the output with 
35482 55 53 2d 41 53 43 49 49 0a 2a 2a 20 63 61 73 65  US-ASCII.** case
35483 20 66 6f 6c 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   folding..**.** 
35484 53 74 65 6d 6d 69 6e 67 20 6e 65 76 65 72 20 69  Stemming never i
35485 6e 63 72 65 61 73 65 73 20 74 68 65 20 6c 65 6e  ncreases the len
35486 67 74 68 20 6f 66 20 74 68 65 20 77 6f 72 64 2e  gth of the word.
35487 20 20 53 6f 20 74 68 65 72 65 20 69 73 0a 2a 2a    So there is.**
35488 20 6e 6f 20 63 68 61 6e 63 65 20 6f 66 20 6f 76   no chance of ov
35489 65 72 66 6c 6f 77 69 6e 67 20 74 68 65 20 7a 4f  erflowing the zO
3548a 75 74 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74  ut buffer..*/.st
3548b 61 74 69 63 20 76 6f 69 64 20 70 6f 72 74 65 72  atic void porter
3548c 5f 73 74 65 6d 6d 65 72 28 63 6f 6e 73 74 20 63  _stemmer(const c
3548d 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49  har *zIn, int nI
3548e 6e 2c 20 63 68 61 72 20 2a 7a 4f 75 74 2c 20 69  n, char *zOut, i
3548f 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 69 6e  nt *pnOut){.  in
35490 74 20 69 2c 20 6a 2c 20 63 3b 0a 20 20 63 68 61  t i, j, c;.  cha
35491 72 20 7a 52 65 76 65 72 73 65 5b 32 38 5d 3b 0a  r zReverse[28];.
35492 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 32 3b 0a    char *z, *z2;.
35493 20 20 69 66 28 20 6e 49 6e 3c 33 20 7c 7c 20 6e    if( nIn<3 || n
35494 49 6e 3e 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65  In>=sizeof(zReve
35495 72 73 65 29 2d 37 20 29 7b 0a 20 20 20 20 2f 2a  rse)-7 ){.    /*
35496 20 54 68 65 20 77 6f 72 64 20 69 73 20 74 6f 6f   The word is too
35497 20 62 69 67 20 6f 72 20 74 6f 6f 20 73 6d 61 6c   big or too smal
35498 6c 20 66 6f 72 20 74 68 65 20 70 6f 72 74 65 72  l for the porter
35499 20 73 74 65 6d 6d 65 72 2e 0a 20 20 20 20 2a 2a   stemmer..    **
3549a 20 46 61 6c 6c 62 61 63 6b 20 74 6f 20 74 68 65   Fallback to the
3549b 20 63 6f 70 79 20 73 74 65 6d 6d 65 72 20 2a 2f   copy stemmer */
3549c 0a 20 20 20 20 63 6f 70 79 5f 73 74 65 6d 6d 65  .    copy_stemme
3549d 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74  r(zIn, nIn, zOut
3549e 2c 20 70 6e 4f 75 74 29 3b 0a 20 20 20 20 72 65  , pnOut);.    re
3549f 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
354a0 69 3d 30 2c 20 6a 3d 73 69 7a 65 6f 66 28 7a 52  i=0, j=sizeof(zR
354a1 65 76 65 72 73 65 29 2d 36 3b 20 69 3c 6e 49 6e  everse)-6; i<nIn
354a2 3b 20 69 2b 2b 2c 20 6a 2d 2d 29 7b 0a 20 20 20  ; i++, j--){.   
354a3 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20   c = zIn[i];.   
354a4 20 69 66 28 20 63 3e 3d 27 41 27 20 26 26 20 63   if( c>='A' && c
354a5 3c 3d 27 5a 27 20 29 7b 0a 20 20 20 20 20 20 7a  <='Z' ){.      z
354a6 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20 63 20 2b  Reverse[j] = c +
354a7 20 27 61 27 20 2d 20 27 41 27 3b 0a 20 20 20 20   'a' - 'A';.    
354a8 7d 65 6c 73 65 20 69 66 28 20 63 3e 3d 27 61 27  }else if( c>='a'
354a9 20 26 26 20 63 3c 3d 27 7a 27 20 29 7b 0a 20 20   && c<='z' ){.  
354aa 20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a 5d 20      zReverse[j] 
354ab 3d 20 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = c;.    }else{.
354ac 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 73 65        /* The use
354ad 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20   of a character 
354ae 6e 6f 74 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20  not in [a-zA-Z] 
354af 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 66 61  means that we fa
354b0 6c 6c 62 61 63 6b 0a 20 20 20 20 20 20 2a 2a 20  llback.      ** 
354b1 74 6f 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d  to the copy stem
354b2 6d 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 70  mer */.      cop
354b3 79 5f 73 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e  y_stemmer(zIn, n
354b4 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29  In, zOut, pnOut)
354b5 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
354b6 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73      }.  }.  mems
354b7 65 74 28 26 7a 52 65 76 65 72 73 65 5b 73 69 7a  et(&zReverse[siz
354b8 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 35 5d  eof(zReverse)-5]
354b9 2c 20 30 2c 20 35 29 3b 0a 20 20 7a 20 3d 20 26  , 0, 5);.  z = &
354ba 7a 52 65 76 65 72 73 65 5b 6a 2b 31 5d 3b 0a 0a  zReverse[j+1];..
354bb 0a 20 20 2f 2a 20 53 74 65 70 20 31 61 20 2a 2f  .  /* Step 1a */
354bc 0a 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 73 27  .  if( z[0]=='s'
354bd 20 29 7b 0a 20 20 20 20 69 66 28 0a 20 20 20 20   ){.    if(.    
354be 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 65 73 73   !stem(&z, "sess
354bf 22 2c 20 22 73 73 22 2c 20 30 29 20 26 26 0a 20  ", "ss", 0) &&. 
354c0 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20 22 73      !stem(&z, "s
354c1 65 69 22 2c 20 22 69 22 2c 20 30 29 20 20 26 26  ei", "i", 0)  &&
354c2 0a 20 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20  .     !stem(&z, 
354c3 22 73 73 22 2c 20 22 73 73 22 2c 20 30 29 0a 20  "ss", "ss", 0). 
354c4 20 20 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b     ){.      z++;
354c5 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
354c6 20 53 74 65 70 20 31 62 20 2a 2f 20 20 0a 20 20   Step 1b */  .  
354c7 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 73 74  z2 = z;.  if( st
354c8 65 6d 28 26 7a 2c 20 22 64 65 65 22 2c 20 22 65  em(&z, "dee", "e
354c9 65 22 2c 20 6d 5f 67 74 5f 30 29 20 29 7b 0a 20  e", m_gt_0) ){. 
354ca 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67     /* Do nothing
354cb 2e 20 20 54 68 65 20 77 6f 72 6b 20 77 61 73 20  .  The work was 
354cc 61 6c 6c 20 69 6e 20 74 68 65 20 74 65 73 74 20  all in the test 
354cd 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0a  */.  }else if( .
354ce 20 20 20 20 20 28 73 74 65 6d 28 26 7a 2c 20 22       (stem(&z, "
354cf 67 6e 69 22 2c 20 22 22 2c 20 68 61 73 56 6f 77  gni", "", hasVow
354d0 65 6c 29 20 7c 7c 20 73 74 65 6d 28 26 7a 2c 20  el) || stem(&z, 
354d1 22 64 65 22 2c 20 22 22 2c 20 68 61 73 56 6f 77  "de", "", hasVow
354d2 65 6c 29 29 0a 20 20 20 20 20 20 26 26 20 7a 21  el)).      && z!
354d3 3d 7a 32 0a 20 20 29 7b 0a 20 20 20 20 20 69 66  =z2.  ){.     if
354d4 28 20 73 74 65 6d 28 26 7a 2c 20 22 74 61 22 2c  ( stem(&z, "ta",
354d5 20 22 61 74 65 22 2c 20 30 29 20 7c 7c 0a 20 20   "ate", 0) ||.  
354d6 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
354d7 22 6c 62 22 2c 20 22 62 6c 65 22 2c 20 30 29 20  "lb", "ble", 0) 
354d8 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
354d9 28 26 7a 2c 20 22 7a 69 22 2c 20 22 69 7a 65 22  (&z, "zi", "ize"
354da 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f  , 0) ){.       /
354db 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54  * Do nothing.  T
354dc 68 65 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20  he work was all 
354dd 69 6e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a 20  in the test */. 
354de 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6f      }else if( do
354df 75 62 6c 65 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ubleConsonant(z)
354e0 20 26 26 20 28 2a 7a 21 3d 27 6c 27 20 26 26 20   && (*z!='l' && 
354e1 2a 7a 21 3d 27 73 27 20 26 26 20 2a 7a 21 3d 27  *z!='s' && *z!='
354e2 7a 27 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 2b  z') ){.       z+
354e3 2b 3b 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66  +;.     }else if
354e4 28 20 6d 5f 65 71 5f 31 28 7a 29 20 26 26 20 73  ( m_eq_1(z) && s
354e5 74 61 72 5f 6f 68 28 7a 29 20 29 7b 0a 20 20 20  tar_oh(z) ){.   
354e6 20 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 27 65 27      *(--z) = 'e'
354e7 3b 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  ;.     }.  }..  
354e8 2f 2a 20 53 74 65 70 20 31 63 20 2a 2f 0a 20 20  /* Step 1c */.  
354e9 69 66 28 20 7a 5b 30 5d 3d 3d 27 79 27 20 26 26  if( z[0]=='y' &&
354ea 20 68 61 73 56 6f 77 65 6c 28 7a 2b 31 29 20 29   hasVowel(z+1) )
354eb 7b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 27 69 27  {.    z[0] = 'i'
354ec 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  ;.  }..  /* Step
354ed 20 32 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20   2 */.  switch( 
354ee 7a 5b 31 5d 20 29 7b 0a 20 20 20 63 61 73 65 20  z[1] ){.   case 
354ef 27 61 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'a':.     stem(&
354f0 7a 2c 20 22 6c 61 6e 6f 69 74 61 22 2c 20 22 61  z, "lanoita", "a
354f1 74 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  te", m_gt_0) ||.
354f2 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
354f3 61 6e 6f 69 74 22 2c 20 22 74 69 6f 6e 22 2c 20  anoit", "tion", 
354f4 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72  m_gt_0);.     br
354f5 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 63 27  eak;.   case 'c'
354f6 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
354f7 22 69 63 6e 65 22 2c 20 22 65 6e 63 65 22 2c 20  "icne", "ence", 
354f8 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
354f9 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e 61 22 2c  stem(&z, "icna",
354fa 20 22 61 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29   "ance", m_gt_0)
354fb 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
354fc 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20   case 'e':.     
354fd 73 74 65 6d 28 26 7a 2c 20 22 72 65 7a 69 22 2c  stem(&z, "rezi",
354fe 20 22 69 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ize", m_gt_0);
354ff 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
35500 63 61 73 65 20 27 67 27 3a 0a 20 20 20 20 20 73  case 'g':.     s
35501 74 65 6d 28 26 7a 2c 20 22 69 67 6f 6c 22 2c 20  tem(&z, "igol", 
35502 22 6c 6f 67 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  "log", m_gt_0);.
35503 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
35504 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 73 74  ase 'l':.     st
35505 65 6d 28 26 7a 2c 20 22 69 6c 62 22 2c 20 22 62  em(&z, "ilb", "b
35506 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  le", m_gt_0) ||.
35507 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
35508 6c 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74  lla", "al", m_gt
35509 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
3550a 28 26 7a 2c 20 22 69 6c 74 6e 65 22 2c 20 22 65  (&z, "iltne", "e
3550b 6e 74 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  nt", m_gt_0) ||.
3550c 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
3550d 6c 65 22 2c 20 22 65 22 2c 20 6d 5f 67 74 5f 30  le", "e", m_gt_0
3550e 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
3550f 7a 2c 20 22 69 6c 73 75 6f 22 2c 20 22 6f 75 73  z, "ilsuo", "ous
35510 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
35511 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
35512 27 6f 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'o':.     stem(&
35513 7a 2c 20 22 6e 6f 69 74 61 7a 69 22 2c 20 22 69  z, "noitazi", "i
35514 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ze", m_gt_0) ||.
35515 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e       stem(&z, "n
35516 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f  oita", "ate", m_
35517 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
35518 65 6d 28 26 7a 2c 20 22 72 6f 74 61 22 2c 20 22  em(&z, "rota", "
35519 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ate", m_gt_0);. 
3551a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
3551b 73 65 20 27 73 27 3a 0a 20 20 20 20 20 73 74 65  se 's':.     ste
3551c 6d 28 26 7a 2c 20 22 6d 73 69 6c 61 22 2c 20 22  m(&z, "msila", "
3551d 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  al", m_gt_0) ||.
3551e 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
3551f 73 65 6e 65 76 69 22 2c 20 22 69 76 65 22 2c 20  senevi", "ive", 
35520 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
35521 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 6c 75  stem(&z, "ssenlu
35522 66 22 2c 20 22 66 75 6c 22 2c 20 6d 5f 67 74 5f  f", "ful", m_gt_
35523 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
35524 26 7a 2c 20 22 73 73 65 6e 73 75 6f 22 2c 20 22  &z, "ssensuo", "
35525 6f 75 73 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ous", m_gt_0);. 
35526 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
35527 73 65 20 27 74 27 3a 0a 20 20 20 20 20 73 74 65  se 't':.     ste
35528 6d 28 26 7a 2c 20 22 69 74 69 6c 61 22 2c 20 22  m(&z, "itila", "
35529 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  al", m_gt_0) ||.
3552a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
3552b 74 69 76 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f  tivi", "ive", m_
3552c 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
3552d 65 6d 28 26 7a 2c 20 22 69 74 69 6c 69 62 22 2c  em(&z, "itilib",
3552e 20 22 62 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ble", m_gt_0);
3552f 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  .     break;.  }
35530 0a 0a 20 20 2f 2a 20 53 74 65 70 20 33 20 2a 2f  ..  /* Step 3 */
35531 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30 5d 20  .  switch( z[0] 
35532 29 7b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a  ){.   case 'e':.
35533 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65       stem(&z, "e
35534 74 61 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67  taci", "ic", m_g
35535 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65  t_0) ||.     ste
35536 6d 28 26 7a 2c 20 22 65 76 69 74 61 22 2c 20 22  m(&z, "evita", "
35537 22 2c 20 6d 5f 67 74 5f 30 29 20 20 20 7c 7c 0a  ", m_gt_0)   ||.
35538 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65       stem(&z, "e
35539 7a 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67  zila", "al", m_g
3553a 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
3553b 3b 0a 20 20 20 63 61 73 65 20 27 69 27 3a 0a 20  ;.   case 'i':. 
3553c 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74      stem(&z, "it
3553d 69 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74  ici", "ic", m_gt
3553e 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
3553f 0a 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20  .   case 'l':.  
35540 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 63     stem(&z, "lac
35541 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30  i", "ic", m_gt_0
35542 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
35543 7a 2c 20 22 6c 75 66 22 2c 20 22 22 2c 20 6d 5f  z, "luf", "", m_
35544 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
35545 6b 3b 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a  k;.   case 's':.
35546 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
35547 73 65 6e 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30  sen", "", m_gt_0
35548 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
35549 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 34 20   }..  /* Step 4 
3554a 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31  */.  switch( z[1
3554b 5d 20 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27  ] ){.   case 'a'
3554c 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
3554d 3d 27 6c 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a  ='l' && m_gt_1(z
3554e 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +2) ){.       z 
3554f 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 2;.     }.   
35550 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
35551 20 27 63 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'c':.     if( z
35552 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d  [0]=='e' && z[2]
35553 3d 3d 27 6e 27 20 26 26 20 28 7a 5b 33 5d 3d 3d  =='n' && (z[3]==
35554 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 65 27  'a' || z[3]=='e'
35555 29 20 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 34  )  && m_gt_1(z+4
35556 29 20 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  )  ){.       z +
35557 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 4;.     }.    
35558 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
35559 27 65 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'e':.     if( z[
3555a 30 5d 3d 3d 27 72 27 20 26 26 20 6d 5f 67 74 5f  0]=='r' && m_gt_
3555b 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20  1(z+2) ){.      
3555c 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a   z += 2;.     }.
3555d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
3555e 61 73 65 20 27 69 27 3a 0a 20 20 20 20 20 69 66  ase 'i':.     if
3555f 28 20 7a 5b 30 5d 3d 3d 27 63 27 20 26 26 20 6d  ( z[0]=='c' && m
35560 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20  _gt_1(z+2) ){.  
35561 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20       z += 2;.   
35562 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
35563 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20 20     case 'l':.   
35564 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
35565 26 26 20 7a 5b 32 5d 3d 3d 27 62 27 20 26 26 20  && z[2]=='b' && 
35566 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b  (z[3]=='a' || z[
35567 33 5d 3d 3d 27 69 27 29 20 26 26 20 6d 5f 67 74  3]=='i') && m_gt
35568 5f 31 28 7a 2b 34 29 20 29 7b 0a 20 20 20 20 20  _1(z+4) ){.     
35569 20 20 7a 20 2b 3d 20 34 3b 0a 20 20 20 20 20 7d    z += 4;.     }
3556a 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
3556b 63 61 73 65 20 27 6e 27 3a 0a 20 20 20 20 20 69  case 'n':.     i
3556c 66 28 20 7a 5b 30 5d 3d 3d 27 74 27 20 29 7b 0a  f( z[0]=='t' ){.
3556d 20 20 20 20 20 20 20 69 66 28 20 7a 5b 32 5d 3d         if( z[2]=
3556e 3d 27 61 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='a' ){.        
3556f 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 33 29   if( m_gt_1(z+3)
35570 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 7a   ){.           z
35571 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20   += 3;.         
35572 7d 0a 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  }.       }else i
35573 66 28 20 7a 5b 32 5d 3d 3d 27 65 27 20 29 7b 0a  f( z[2]=='e' ){.
35574 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a           stem(&z
35575 2c 20 22 74 6e 65 6d 65 22 2c 20 22 22 2c 20 6d  , "tneme", "", m
35576 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20  _gt_1) ||.      
35577 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65     stem(&z, "tne
35578 6d 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20  m", "", m_gt_1) 
35579 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
3557a 28 26 7a 2c 20 22 74 6e 65 22 2c 20 22 22 2c 20  (&z, "tne", "", 
3557b 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20 20 20  m_gt_1);.       
3557c 7d 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  }.     }.     br
3557d 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27  eak;.   case 'o'
3557e 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
3557f 3d 27 75 27 20 29 7b 0a 20 20 20 20 20 20 20 69  ='u' ){.       i
35580 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29  f( m_gt_1(z+2) )
35581 7b 0a 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20  {.         z += 
35582 32 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20  2;.       }.    
35583 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b 33 5d 3d   }else if( z[3]=
35584 3d 27 73 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 74  ='s' || z[3]=='t
35585 27 20 29 7b 0a 20 20 20 20 20 20 20 73 74 65 6d  ' ){.       stem
35586 28 26 7a 2c 20 22 6e 6f 69 22 2c 20 22 22 2c 20  (&z, "noi", "", 
35587 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20 7d 0a  m_gt_1);.     }.
35588 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
35589 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 69 66  ase 's':.     if
3558a 28 20 7a 5b 30 5d 3d 3d 27 6d 27 20 26 26 20 7a  ( z[0]=='m' && z
3558b 5b 32 5d 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74  [2]=='i' && m_gt
3558c 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
3558d 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d    z += 3;.     }
3558e 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
3558f 63 61 73 65 20 27 74 27 3a 0a 20 20 20 20 20 73  case 't':.     s
35590 74 65 6d 28 26 7a 2c 20 22 65 74 61 22 2c 20 22  tem(&z, "eta", "
35591 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20  ", m_gt_1) ||.  
35592 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69     stem(&z, "iti
35593 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a  ", "", m_gt_1);.
35594 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
35595 61 73 65 20 27 75 27 3a 0a 20 20 20 20 20 69 66  ase 'u':.     if
35596 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 26 26 20 7a  ( z[0]=='s' && z
35597 5b 32 5d 3d 3d 27 6f 27 20 26 26 20 6d 5f 67 74  [2]=='o' && m_gt
35598 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
35599 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d    z += 3;.     }
3559a 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
3559b 63 61 73 65 20 27 76 27 3a 0a 20 20 20 63 61 73  case 'v':.   cas
3559c 65 20 27 7a 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'z':.     if( 
3559d 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32  z[0]=='e' && z[2
3559e 5d 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74 5f 31  ]=='i' && m_gt_1
3559f 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+3) ){.       
355a0 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20  z += 3;.     }. 
355a1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
355a2 20 20 2f 2a 20 53 74 65 70 20 35 61 20 2a 2f 0a    /* Step 5a */.
355a3 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
355a4 29 7b 0a 20 20 20 20 69 66 28 20 6d 5f 67 74 5f  ){.    if( m_gt_
355a5 31 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20 20 20  1(z+1) ){.      
355a6 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  z++;.    }else i
355a7 66 28 20 6d 5f 65 71 5f 31 28 7a 2b 31 29 20 26  f( m_eq_1(z+1) &
355a8 26 20 21 73 74 61 72 5f 6f 68 28 7a 2b 31 29 20  & !star_oh(z+1) 
355a9 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  ){.      z++;.  
355aa 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74    }.  }..  /* St
355ab 65 70 20 35 62 20 2a 2f 0a 20 20 69 66 28 20 6d  ep 5b */.  if( m
355ac 5f 67 74 5f 31 28 7a 29 20 26 26 20 7a 5b 30 5d  _gt_1(z) && z[0]
355ad 3d 3d 27 6c 27 20 26 26 20 7a 5b 31 5d 3d 3d 27  =='l' && z[1]=='
355ae 6c 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  l' ){.    z++;. 
355af 20 7d 0a 0a 20 20 2f 2a 20 7a 5b 5d 20 69 73 20   }..  /* z[] is 
355b0 6e 6f 77 20 74 68 65 20 73 74 65 6d 6d 65 64 20  now the stemmed 
355b1 77 6f 72 64 20 69 6e 20 72 65 76 65 72 73 65 20  word in reverse 
355b2 6f 72 64 65 72 2e 20 20 46 6c 69 70 20 69 74 20  order.  Flip it 
355b3 62 61 63 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64  back.  ** around
355b4 20 69 6e 74 6f 20 66 6f 72 77 61 72 64 20 6f 72   into forward or
355b5 64 65 72 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a  der and return..
355b6 20 20 2a 2f 0a 20 20 2a 70 6e 4f 75 74 20 3d 20    */.  *pnOut = 
355b7 69 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  i = strlen(z);. 
355b8 20 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20   zOut[i] = 0;.  
355b9 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20  while( *z ){.   
355ba 20 7a 4f 75 74 5b 2d 2d 69 5d 20 3d 20 2a 28 7a   zOut[--i] = *(z
355bb 2b 2b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ++);.  }.}../*.*
355bc 2a 20 43 68 61 72 61 63 74 65 72 73 20 74 68 61  * Characters tha
355bd 74 20 63 61 6e 20 62 65 20 70 61 72 74 20 6f 66  t can be part of
355be 20 61 20 74 6f 6b 65 6e 2e 20 20 57 65 20 61 73   a token.  We as
355bf 73 75 6d 65 20 61 6e 79 20 63 68 61 72 61 63 74  sume any charact
355c0 65 72 0a 2a 2a 20 77 68 6f 73 65 20 76 61 6c 75  er.** whose valu
355c1 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  e is greater tha
355c2 6e 20 30 78 38 30 20 28 61 6e 79 20 55 54 46 20  n 0x80 (any UTF 
355c3 63 68 61 72 61 63 74 65 72 29 20 63 61 6e 20 62  character) can b
355c4 65 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 20 74  e.** part of a t
355c5 6f 6b 65 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20  oken.  In other 
355c6 77 6f 72 64 73 2c 20 64 65 6c 69 6d 69 74 65 72  words, delimiter
355c7 73 20 61 6c 6c 20 6d 75 73 74 20 68 61 76 65 0a  s all must have.
355c8 2a 2a 20 76 61 6c 75 65 73 20 6f 66 20 30 78 37  ** values of 0x7
355c9 66 20 6f 72 20 6c 6f 77 65 72 2e 0a 2a 2f 0a 73  f or lower..*/.s
355ca 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
355cb 20 70 6f 72 74 65 72 49 64 43 68 61 72 5b 5d 20   porterIdChar[] 
355cc 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20  = {./* x0 x1 x2 
355cd 78 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78  x3 x4 x5 x6 x7 x
355ce 38 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44  8 x9 xA xB xC xD
355cf 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20 31 2c   xE xF */.    1,
355d0 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
355d1 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
355d2 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
355d3 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 3x */.    0, 1
355d4 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
355d5 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
355d6 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
355d7 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  4x */.    1, 1, 
355d8 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
355d9 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
355da 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78   0, 0, 1,  /* 5x
355db 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
355dc 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
355dd 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
355de 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a  , 1, 1,  /* 6x *
355df 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
355e0 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
355e1 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
355e2 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a  0, 0,  /* 7x */.
355e3 7d 3b 0a 23 64 65 66 69 6e 65 20 69 73 44 65 6c  };.#define isDel
355e4 69 6d 28 43 29 20 28 28 28 63 68 3d 43 29 26 30  im(C) (((ch=C)&0
355e5 78 38 30 29 3d 3d 30 20 26 26 20 28 63 68 3c 30  x80)==0 && (ch<0
355e6 78 33 30 20 7c 7c 20 21 70 6f 72 74 65 72 49 64  x30 || !porterId
355e7 43 68 61 72 5b 63 68 2d 30 78 33 30 5d 29 29 0a  Char[ch-0x30])).
355e8 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74  ./*.** Extract t
355e9 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72  he next token fr
355ea 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  om a tokenizatio
355eb 6e 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63  n cursor.  The c
355ec 75 72 73 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61  ursor must.** ha
355ed 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62  ve been opened b
355ee 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74  y a prior call t
355ef 6f 20 70 6f 72 74 65 72 4f 70 65 6e 28 29 2e 0a  o porterOpen()..
355f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f  */.static int po
355f1 72 74 65 72 4e 65 78 74 28 0a 20 20 73 71 6c 69  rterNext(.  sqli
355f2 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
355f3 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20  rsor *pCursor,  
355f4 2f 2a 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e  /* Cursor return
355f5 65 64 20 62 79 20 70 6f 72 74 65 72 4f 70 65 6e  ed by porterOpen
355f6 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
355f7 20 2a 2a 70 7a 54 6f 6b 65 6e 2c 20 20 20 20 20   **pzToken,     
355f8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
355f9 3a 20 2a 70 7a 54 6f 6b 65 6e 20 69 73 20 74 68  : *pzToken is th
355fa 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a  e token text */.
355fb 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20    int *pnBytes, 
355fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355fd 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
355fe 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
355ff 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
35600 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  *piStartOffset, 
35601 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35602 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67  /* OUT: Starting
35603 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
35604 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64   */.  int *piEnd
35605 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
35606 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
35607 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20  : Ending offset 
35608 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  of token */.  in
35609 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20  t *piPosition   
3560a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3560b 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69    /* OUT: Positi
3560c 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f  on integer of to
3560d 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74  ken */.){.  port
3560e 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
3560f 73 6f 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72  sor *c = (porter
35610 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
35611 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
35612 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
35613 63 2d 3e 7a 49 6e 70 75 74 3b 0a 0a 20 20 77 68  c->zInput;..  wh
35614 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c  ile( c->iOffset<
35615 63 2d 3e 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20  c->nInput ){.   
35616 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65   int iStartOffse
35617 74 2c 20 63 68 3b 0a 0a 20 20 20 20 2f 2a 20 53  t, ch;..    /* S
35618 63 61 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74  can past delimit
35619 65 72 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f  er characters */
3561a 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69  .    while( c->i
3561b 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74  Offset<c->nInput
3561c 20 26 26 20 69 73 44 65 6c 69 6d 28 7a 5b 63 2d   && isDelim(z[c-
3561d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20  >iOffset]) ){.  
3561e 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b      c->iOffset++
3561f 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
35620 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69  Count non-delimi
35621 74 65 72 20 63 68 61 72 61 63 74 65 72 73 2e 20  ter characters. 
35622 2a 2f 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66  */.    iStartOff
35623 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74  set = c->iOffset
35624 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e  ;.    while( c->
35625 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75  iOffset<c->nInpu
35626 74 20 26 26 20 21 69 73 44 65 6c 69 6d 28 7a 5b  t && !isDelim(z[
35627 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a  c->iOffset]) ){.
35628 20 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74        c->iOffset
35629 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
3562a 66 28 20 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53  f( c->iOffset>iS
3562b 74 61 72 74 4f 66 66 73 65 74 20 29 7b 0a 20 20  tartOffset ){.  
3562c 20 20 20 20 69 6e 74 20 6e 20 3d 20 63 2d 3e 69      int n = c->i
3562d 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66  Offset-iStartOff
3562e 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  set;.      if( n
3562f 3e 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 29  >c->nAllocated )
35630 7b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e 41 6c  {.        c->nAl
35631 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a  located = n+20;.
35632 20 20 20 20 20 20 20 20 63 2d 3e 7a 54 6f 6b 65          c->zToke
35633 6e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  n = sqlite3_real
35634 6c 6f 63 28 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 63  loc(c->zToken, c
35635 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20  ->nAllocated);. 
35636 20 20 20 20 20 20 20 69 66 28 20 63 2d 3e 7a 54         if( c->zT
35637 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74  oken==NULL ) ret
35638 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
35639 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3563a 70 6f 72 74 65 72 5f 73 74 65 6d 6d 65 72 28 26  porter_stemmer(&
3563b 7a 5b 69 53 74 61 72 74 4f 66 66 73 65 74 5d 2c  z[iStartOffset],
3563c 20 6e 2c 20 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 70   n, c->zToken, p
3563d 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 2a  nBytes);.      *
3563e 70 7a 54 6f 6b 65 6e 20 3d 20 63 2d 3e 7a 54 6f  pzToken = c->zTo
3563f 6b 65 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53 74  ken;.      *piSt
35640 61 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74 61  artOffset = iSta
35641 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  rtOffset;.      
35642 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 63  *piEndOffset = c
35643 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->iOffset;.     
35644 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63   *piPosition = c
35645 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20  ->iToken++;.    
35646 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
35647 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OK;.    }.  }.  
35648 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
35649 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  NE;.}../*.** The
3564a 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
3564b 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
3564c 74 68 65 20 70 6f 72 74 65 72 2d 73 74 65 6d 6d  the porter-stemm
3564d 65 72 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a  er tokenizer.*/.
3564e 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
3564f 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
35650 6f 64 75 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65  odule porterToke
35651 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a  nizerModule = {.
35652 20 20 30 2c 0a 20 20 70 6f 72 74 65 72 43 72 65    0,.  porterCre
35653 61 74 65 2c 0a 20 20 70 6f 72 74 65 72 44 65 73  ate,.  porterDes
35654 74 72 6f 79 2c 0a 20 20 70 6f 72 74 65 72 4f 70  troy,.  porterOp
35655 65 6e 2c 0a 20 20 70 6f 72 74 65 72 43 6c 6f 73  en,.  porterClos
35656 65 2c 0a 20 20 70 6f 72 74 65 72 4e 65 78 74 2c  e,.  porterNext,
35657 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .};../*.** Alloc
35658 61 74 65 20 61 20 6e 65 77 20 70 6f 72 74 65 72  ate a new porter
35659 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74   tokenizer.  Ret
3565a 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
3565b 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65   the new.** toke
3565c 6e 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64 75  nizer in *ppModu
3565d 6c 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  le.*/.SQLITE_PRI
3565e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
3565f 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e  3Fts3PorterToken
35660 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71  izerModule(.  sq
35661 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
35662 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
35663 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d  Module.){.  *ppM
35664 6f 64 75 6c 65 20 3d 20 26 70 6f 72 74 65 72 54  odule = &porterT
35665 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a  okenizerModule;.
35666 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  }..#endif /* !de
35667 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
35668 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
35669 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
3566a 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
3566b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
3566c 33 5f 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a  3_porter.c *****
3566d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3566e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3566f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
35670 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
35671 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e   fts3_tokenizer.
35672 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
35673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35674 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
35675 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54   June 22.**.** T
35676 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
35677 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
35678 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
35679 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
3567a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
3567b 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
3567c 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
3567d 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
3567e 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
3567f 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
35680 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
35681 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
35682 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
35683 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
35684 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
35685 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
35686 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
35687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35688 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35689 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3568a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3568b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
3568c 2a 2a 20 54 68 69 73 20 69 73 20 70 61 72 74 20  ** This is part 
3568d 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64  of an SQLite mod
3568e 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  ule implementing
3568f 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63   full-text searc
35690 68 2e 0a 2a 2a 20 54 68 69 73 20 70 61 72 74 69  h..** This parti
35691 63 75 6c 61 72 20 66 69 6c 65 20 69 6d 70 6c 65  cular file imple
35692 6d 65 6e 74 73 20 74 68 65 20 67 65 6e 65 72 69  ments the generi
35693 63 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65  c tokenizer inte
35694 72 66 61 63 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  rface..*/../*.**
35695 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
35696 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63  s file is only c
35697 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a  ompiled if:.**.*
35698 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
35699 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
3569a 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74   built as an ext
3569b 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ension.**       
3569c 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
3569d 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
3569e 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a  t defined), or.*
3569f 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
356a0 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
356a1 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ing built into t
356a2 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20  he core of.**   
356a3 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77      SQLite (in w
356a4 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
356a5 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20  _ENABLE_FTS3 is 
356a6 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66  defined)..*/.#if
356a7 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
356a8 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
356a9 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
356aa 46 54 53 33 29 0a 0a 23 69 66 6e 64 65 66 20 53  FTS3)..#ifndef S
356ab 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c  QLITE_CORE.  SQL
356ac 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
356ad 49 54 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  IT1.#endif.../*.
356ae 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
356af 6e 20 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61  n of the SQL sca
356b0 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  lar function for
356b1 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 75   accessing the u
356b2 6e 64 65 72 6c 79 69 6e 67 20 0a 2a 2a 20 68 61  nderlying .** ha
356b3 73 68 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66  sh table. This f
356b4 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63  unction may be c
356b5 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  alled as follows
356b6 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54  :.**.**   SELECT
356b7 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
356b8 28 3c 6b 65 79 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a  (<key-name>);.**
356b9 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74     SELECT <funct
356ba 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e  ion-name>(<key-n
356bb 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29  ame>, <pointer>)
356bc 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66  ;.**.** where <f
356bd 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73  unction-name> is
356be 20 74 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64   the name passed
356bf 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
356c0 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
356c1 65 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  e sqlite3Fts3Ini
356c2 74 48 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e  tHashTable() fun
356c3 63 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73  ction (e.g. 'fts
356c4 33 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 2e 0a 2a  3_tokenizer')..*
356c5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 3c 70 6f 69  *.** If the <poi
356c6 6e 74 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69  nter> argument i
356c7 73 20 73 70 65 63 69 66 69 65 64 2c 20 69 74 20  s specified, it 
356c8 6d 75 73 74 20 62 65 20 61 20 62 6c 6f 62 20 76  must be a blob v
356c9 61 6c 75 65 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69  alue.** containi
356ca 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
356cb 62 65 20 73 74 6f 72 65 64 20 61 73 20 74 68 65  be stored as the
356cc 20 68 61 73 68 20 64 61 74 61 20 63 6f 72 72 65   hash data corre
356cd 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 74  sponding.** to t
356ce 68 65 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e  he string <key-n
356cf 61 6d 65 3e 2e 20 49 66 20 3c 70 6f 69 6e 74 65  ame>. If <pointe
356d0 72 3e 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  r> is not specif
356d1 69 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ied, then.** the
356d2 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d   string <key-nam
356d3 65 3e 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20  e> must already 
356d4 65 78 69 73 74 20 69 6e 20 74 68 65 20 68 61 73  exist in the has
356d5 20 74 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73   table. Otherwis
356d6 65 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69  e,.** an error i
356d7 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
356d8 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
356d9 20 74 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61   the <pointer> a
356da 72 67 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69  rgument is speci
356db 66 69 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20  fied, the value 
356dc 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 61  returned.** is a
356dd 20 62 6c 6f 62 20 63 6f 6e 74 61 69 6e 69 6e 67   blob containing
356de 20 74 68 65 20 70 6f 69 6e 74 65 72 20 73 74 6f   the pointer sto
356df 72 65 64 20 61 73 20 74 68 65 20 68 61 73 68 20  red as the hash 
356e0 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
356e1 6e 67 0a 2a 2a 20 74 6f 20 73 74 72 69 6e 67 20  ng.** to string 
356e2 3c 6b 65 79 2d 6e 61 6d 65 3e 20 28 61 66 74 65  <key-name> (afte
356e3 72 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  r the hash-table
356e4 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 66 20   is updated, if 
356e5 61 70 70 6c 69 63 61 62 6c 65 29 2e 0a 2a 2f 0a  applicable)..*/.
356e6 73 74 61 74 69 63 20 76 6f 69 64 20 73 63 61 6c  static void scal
356e7 61 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  arFunc(.  sqlite
356e8 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
356e9 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
356ea 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
356eb 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 74 73 33  **argv.){.  fts3
356ec 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 76  Hash *pHash;.  v
356ed 6f 69 64 20 2a 70 50 74 72 20 3d 20 30 3b 0a 20  oid *pPtr = 0;. 
356ee 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
356ef 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69  char *zName;.  i
356f0 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73  nt nName;..  ass
356f1 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20  ert( argc==1 || 
356f2 61 72 67 63 3d 3d 32 20 29 3b 0a 0a 20 20 70 48  argc==2 );..  pH
356f3 61 73 68 20 3d 20 28 66 74 73 33 48 61 73 68 20  ash = (fts3Hash 
356f4 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  *)sqlite3_user_d
356f5 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20  ata(context);.. 
356f6 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
356f7 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
356f8 5b 30 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20  [0]);.  nName = 
356f9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
356fa 74 65 73 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a  tes(argv[0])+1;.
356fb 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29  .  if( argc==2 )
356fc 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4f 6c 64  {.    void *pOld
356fd 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71  ;.    int n = sq
356fe 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
356ff 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  s(argv[1]);.    
35700 69 66 28 20 6e 21 3d 73 69 7a 65 6f 66 28 70 50  if( n!=sizeof(pP
35701 74 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tr) ){.      sql
35702 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
35703 72 28 63 6f 6e 74 65 78 74 2c 20 22 61 72 67 75  r(context, "argu
35704 6d 65 6e 74 20 74 79 70 65 20 6d 69 73 6d 61 74  ment type mismat
35705 63 68 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  ch", -1);.      
35706 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
35707 20 20 70 50 74 72 20 3d 20 2a 28 76 6f 69 64 20    pPtr = *(void 
35708 2a 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  **)sqlite3_value
35709 5f 62 6c 6f 62 28 61 72 67 76 5b 31 5d 29 3b 0a  _blob(argv[1]);.
3570a 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74      pOld = sqlit
3570b 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
3570c 28 70 48 61 73 68 2c 20 28 76 6f 69 64 20 2a 29  (pHash, (void *)
3570d 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 50  zName, nName, pP
3570e 74 72 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  tr);.    if( pOl
3570f 64 3d 3d 70 50 74 72 20 29 7b 0a 20 20 20 20 20  d==pPtr ){.     
35710 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
35711 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22  error(context, "
35712 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20  out of memory", 
35713 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  -1);.      retur
35714 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  n;.    }.  }else
35715 7b 0a 20 20 20 20 70 50 74 72 20 3d 20 73 71 6c  {.    pPtr = sql
35716 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
35717 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  (pHash, zName, n
35718 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21  Name);.    if( !
35719 70 50 74 72 20 29 7b 0a 20 20 20 20 20 20 63 68  pPtr ){.      ch
3571a 61 72 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74  ar *zErr = sqlit
3571b 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
3571c 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25  own tokenizer: %
3571d 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
3571e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3571f 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
35720 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20 20  zErr, -1);.     
35721 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
35722 72 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  rr);.      retur
35723 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
35724 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
35725 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 76 6f  lob(context, (vo
35726 69 64 20 2a 29 26 70 50 74 72 2c 20 73 69 7a 65  id *)&pPtr, size
35727 6f 66 28 70 50 74 72 29 2c 20 53 51 4c 49 54 45  of(pPtr), SQLITE
35728 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a  _TRANSIENT);.}..
35729 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
3572a 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ST.../*.** Imple
3572b 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73  mentation of a s
3572c 70 65 63 69 61 6c 20 53 51 4c 20 73 63 61 6c 61  pecial SQL scala
3572d 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74  r function for t
3572e 65 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72  esting tokenizer
3572f 73 20 0a 2a 2a 20 64 65 73 69 67 6e 65 64 20 74  s .** designed t
35730 6f 20 62 65 20 75 73 65 64 20 69 6e 20 63 6f 6e  o be used in con
35731 63 65 72 74 20 77 69 74 68 20 74 68 65 20 54 63  cert with the Tc
35732 6c 20 74 65 73 74 69 6e 67 20 66 72 61 6d 65 77  l testing framew
35733 6f 72 6b 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e  ork. This.** fun
35734 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61  ction must be ca
35735 6c 6c 65 64 20 77 69 74 68 20 74 77 6f 20 61 72  lled with two ar
35736 67 75 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  guments:.**.**  
35737 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f   SELECT <functio
35738 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d  n-name>(<key-nam
35739 65 3e 2c 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e  e>, <input-strin
3573a 67 3e 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54  g>);.**   SELECT
3573b 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
3573c 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f  (<key-name>, <po
3573d 69 6e 74 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77  inter>);.**.** w
3573e 68 65 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  here <function-n
3573f 61 6d 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65  ame> is the name
35740 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
35741 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a  econd argument.*
35742 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  * to the sqlite3
35743 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c  Fts3InitHashTabl
35744 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e  e() function (e.
35745 67 2e 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  g. 'fts3_tokeniz
35746 65 72 27 29 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e  er').** concaten
35747 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 74  ated with the st
35748 72 69 6e 67 20 27 5f 74 65 73 74 27 20 28 65 2e  ring '_test' (e.
35749 67 2e 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  g. 'fts3_tokeniz
3574a 65 72 5f 74 65 73 74 27 29 2e 0a 2a 2a 0a 2a 2a  er_test')..**.**
3574b 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
3574c 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68  e is a string th
3574d 61 74 20 6d 61 79 20 62 65 20 69 6e 74 65 72 70  at may be interp
3574e 72 65 74 65 64 20 61 73 20 61 20 54 63 6c 0a 2a  reted as a Tcl.*
3574f 2a 20 6c 69 73 74 2e 20 46 6f 72 20 65 61 63 68  * list. For each
35750 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 3c 69   token in the <i
35751 6e 70 75 74 2d 73 74 72 69 6e 67 3e 2c 20 74 68  nput-string>, th
35752 72 65 65 20 65 6c 65 6d 65 6e 74 73 20 61 72 65  ree elements are
35753 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65  .** added to the
35754 20 72 65 74 75 72 6e 65 64 20 6c 69 73 74 2e 20   returned list. 
35755 54 68 65 20 66 69 72 73 74 20 69 73 20 74 68 65  The first is the
35756 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 2c   token position,
35757 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20   the .** second 
35758 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78  is the token tex
35759 74 20 28 66 6f 6c 64 65 64 2c 20 73 74 65 6d 6d  t (folded, stemm
3575a 65 64 2c 20 65 74 63 2e 29 20 61 6e 64 20 74 68  ed, etc.) and th
3575b 65 20 74 68 69 72 64 20 69 73 20 74 68 65 0a 2a  e third is the.*
3575c 2a 20 73 75 62 73 74 72 69 6e 67 20 6f 66 20 3c  * substring of <
3575d 69 6e 70 75 74 2d 73 74 72 69 6e 67 3e 20 61 73  input-string> as
3575e 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
3575f 65 20 74 6f 6b 65 6e 2e 20 46 6f 72 20 65 78 61  e token. For exa
35760 6d 70 6c 65 2c 20 0a 2a 2a 20 75 73 69 6e 67 20  mple, .** using 
35761 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 22 73 69  the built-in "si
35762 6d 70 6c 65 22 20 74 6f 6b 65 6e 69 7a 65 72 3a  mple" tokenizer:
35763 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  .**.**   SELECT 
35764 66 74 73 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65  fts_tokenizer_te
35765 73 74 28 27 73 69 6d 70 6c 65 27 2c 20 27 49 20  st('simple', 'I 
35766 64 6f 6e 27 74 20 73 65 65 20 68 6f 77 27 29 3b  don't see how');
35767 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75  .**.** will retu
35768 72 6e 20 74 68 65 20 73 74 72 69 6e 67 3a 0a 2a  rn the string:.*
35769 2a 0a 2a 2a 20 20 20 22 7b 30 20 69 20 49 20 31  *.**   "{0 i I 1
3576a 20 64 6f 6e 74 20 64 6f 6e 27 74 20 32 20 73 65   dont don't 2 se
3576b 65 20 73 65 65 20 33 20 68 6f 77 20 68 6f 77 7d  e see 3 how how}
3576c 22 0a 2a 2a 20 20 20 0a 2a 2f 0a 73 74 61 74 69  ".**   .*/.stati
3576d 63 20 76 6f 69 64 20 74 65 73 74 46 75 6e 63 28  c void testFunc(
3576e 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
3576f 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
35770 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
35771 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
35772 29 7b 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  ){.  fts3Hash *p
35773 48 61 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Hash;.  sqlite3_
35774 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
35775 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74   *p;.  sqlite3_t
35776 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
35777 69 7a 65 72 20 3d 20 30 3b 0a 20 20 73 71 6c 69  izer = 0;.  sqli
35778 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
35779 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 30 3b 0a  rsor *pCsr = 0;.
3577a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3577b 45 72 72 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73  Err = 0;..  cons
3577c 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20  t char *zName;. 
3577d 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 63 6f   int nName;.  co
3577e 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
3577f 3b 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a  ;.  int nInput;.
35780 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
35781 41 72 67 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73  Arg = 0;..  cons
35782 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a  t char *zToken;.
35783 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20    int nToken;.  
35784 69 6e 74 20 69 53 74 61 72 74 3b 0a 20 20 69 6e  int iStart;.  in
35785 74 20 69 45 6e 64 3b 0a 20 20 69 6e 74 20 69 50  t iEnd;.  int iP
35786 6f 73 3b 0a 0a 20 20 54 63 6c 5f 4f 62 6a 20 2a  os;..  Tcl_Obj *
35787 70 52 65 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  pRet;..  assert(
35788 20 61 72 67 63 3d 3d 32 20 7c 7c 20 61 72 67 63   argc==2 || argc
35789 3d 3d 33 20 29 3b 0a 0a 20 20 6e 4e 61 6d 65 20  ==3 );..  nName 
3578a 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3578b 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
3578c 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74    zName = (const
3578d 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
3578e 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
3578f 30 5d 29 3b 0a 20 20 6e 49 6e 70 75 74 20 3d 20  0]);.  nInput = 
35790 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
35791 74 65 73 28 61 72 67 76 5b 61 72 67 63 2d 31 5d  tes(argv[argc-1]
35792 29 3b 0a 20 20 7a 49 6e 70 75 74 20 3d 20 28 63  );.  zInput = (c
35793 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
35794 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
35795 72 67 76 5b 61 72 67 63 2d 31 5d 29 3b 0a 0a 20  rgv[argc-1]);.. 
35796 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a   if( argc==3 ){.
35797 20 20 20 20 7a 41 72 67 20 3d 20 28 63 6f 6e 73      zArg = (cons
35798 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
35799 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
3579a 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 70 48 61  [1]);.  }..  pHa
3579b 73 68 20 3d 20 28 66 74 73 33 48 61 73 68 20 2a  sh = (fts3Hash *
3579c 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61  )sqlite3_user_da
3579d 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 70  ta(context);.  p
3579e 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65   = (sqlite3_toke
3579f 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73  nizer_module *)s
357a0 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
357a1 6e 64 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  nd(pHash, zName,
357a2 20 6e 4e 61 6d 65 2b 31 29 3b 0a 0a 20 20 69 66   nName+1);..  if
357a3 28 20 21 70 20 29 7b 0a 20 20 20 20 63 68 61 72  ( !p ){.    char
357a4 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33   *zErr = sqlite3
357a5 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77  _mprintf("unknow
357a6 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22  n tokenizer: %s"
357a7 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71  , zName);.    sq
357a8 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
357a9 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72  or(context, zErr
357aa 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , -1);.    sqlit
357ab 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 20  e3_free(zErr);. 
357ac 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
357ad 20 20 70 52 65 74 20 3d 20 54 63 6c 5f 4e 65 77    pRet = Tcl_New
357ae 4f 62 6a 28 29 3b 0a 20 20 54 63 6c 5f 49 6e 63  Obj();.  Tcl_Inc
357af 72 52 65 66 43 6f 75 6e 74 28 70 52 65 74 29 3b  rRefCount(pRet);
357b0 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
357b1 4b 21 3d 70 2d 3e 78 43 72 65 61 74 65 28 7a 41  K!=p->xCreate(zA
357b2 72 67 20 3f 20 31 20 3a 20 30 2c 20 26 7a 41 72  rg ? 1 : 0, &zAr
357b3 67 2c 20 26 70 54 6f 6b 65 6e 69 7a 65 72 29 20  g, &pTokenizer) 
357b4 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65  ){.    zErr = "e
357b5 72 72 6f 72 20 69 6e 20 78 43 72 65 61 74 65 28  rror in xCreate(
357b6 29 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e  )";.    goto fin
357b7 69 73 68 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65  ish;.  }.  pToke
357b8 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d  nizer->pModule =
357b9 20 70 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45   p;.  if( SQLITE
357ba 5f 4f 4b 21 3d 70 2d 3e 78 4f 70 65 6e 28 70 54  _OK!=p->xOpen(pT
357bb 6f 6b 65 6e 69 7a 65 72 2c 20 7a 49 6e 70 75 74  okenizer, zInput
357bc 2c 20 6e 49 6e 70 75 74 2c 20 26 70 43 73 72 29  , nInput, &pCsr)
357bd 20 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22   ){.    zErr = "
357be 65 72 72 6f 72 20 69 6e 20 78 4f 70 65 6e 28 29  error in xOpen()
357bf 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69  ";.    goto fini
357c0 73 68 3b 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e  sh;.  }.  pCsr->
357c1 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
357c2 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20 77 68 69 6c  kenizer;..  whil
357c3 65 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 2d  e( SQLITE_OK==p-
357c4 3e 78 4e 65 78 74 28 70 43 73 72 2c 20 26 7a 54  >xNext(pCsr, &zT
357c5 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26  oken, &nToken, &
357c6 69 53 74 61 72 74 2c 20 26 69 45 6e 64 2c 20 26  iStart, &iEnd, &
357c7 69 50 6f 73 29 20 29 7b 0a 20 20 20 20 54 63 6c  iPos) ){.    Tcl
357c8 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c  _ListObjAppendEl
357c9 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c 20 54  ement(0, pRet, T
357ca 63 6c 5f 4e 65 77 49 6e 74 4f 62 6a 28 69 50 6f  cl_NewIntObj(iPo
357cb 73 29 29 3b 0a 20 20 20 20 54 63 6c 5f 4c 69 73  s));.    Tcl_Lis
357cc 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e  tObjAppendElemen
357cd 74 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e  t(0, pRet, Tcl_N
357ce 65 77 53 74 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b  ewStringObj(zTok
357cf 65 6e 2c 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20 20  en, nToken));.  
357d0 20 20 7a 54 6f 6b 65 6e 20 3d 20 26 7a 49 6e 70    zToken = &zInp
357d1 75 74 5b 69 53 74 61 72 74 5d 3b 0a 20 20 20 20  ut[iStart];.    
357d2 6e 54 6f 6b 65 6e 20 3d 20 69 45 6e 64 2d 69 53  nToken = iEnd-iS
357d3 74 61 72 74 3b 0a 20 20 20 20 54 63 6c 5f 4c 69  tart;.    Tcl_Li
357d4 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65  stObjAppendEleme
357d5 6e 74 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f  nt(0, pRet, Tcl_
357d6 4e 65 77 53 74 72 69 6e 67 4f 62 6a 28 7a 54 6f  NewStringObj(zTo
357d7 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20  ken, nToken));. 
357d8 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45   }..  if( SQLITE
357d9 5f 4f 4b 21 3d 70 2d 3e 78 43 6c 6f 73 65 28 70  _OK!=p->xClose(p
357da 43 73 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72  Csr) ){.    zErr
357db 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 43 6c   = "error in xCl
357dc 6f 73 65 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f  ose()";.    goto
357dd 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20 69   finish;.  }.  i
357de 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d  f( SQLITE_OK!=p-
357df 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e  >xDestroy(pToken
357e0 69 7a 65 72 29 20 29 7b 0a 20 20 20 20 7a 45 72  izer) ){.    zEr
357e1 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 44  r = "error in xD
357e2 65 73 74 72 6f 79 28 29 22 3b 0a 20 20 20 20 67  estroy()";.    g
357e3 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a  oto finish;.  }.
357e4 0a 66 69 6e 69 73 68 3a 0a 20 20 69 66 28 20 7a  .finish:.  if( z
357e5 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Err ){.    sqlit
357e6 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
357e7 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
357e8 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  1);.  }else{.   
357e9 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
357ea 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 54 63  text(context, Tc
357eb 6c 5f 47 65 74 53 74 72 69 6e 67 28 70 52 65 74  l_GetString(pRet
357ec 29 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52  ), -1, SQLITE_TR
357ed 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 20 20  ANSIENT);.  }.  
357ee 54 63 6c 5f 44 65 63 72 52 65 66 43 6f 75 6e 74  Tcl_DecrRefCount
357ef 28 70 52 65 74 29 3b 0a 7d 0a 0a 73 74 61 74 69  (pRet);.}..stati
357f0 63 0a 69 6e 74 20 72 65 67 69 73 74 65 72 54 6f  c.int registerTo
357f1 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  kenizer(.  sqlit
357f2 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20  e3 *db, .  char 
357f3 2a 7a 4e 61 6d 65 2c 20 0a 20 20 63 6f 6e 73 74  *zName, .  const
357f4 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
357f5 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 0a 29 7b 0a  er_module *p.){.
357f6 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
357f7 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
357f8 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53  .  const char zS
357f9 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20 66  ql[] = "SELECT f
357fa 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 2c  ts3_tokenizer(?,
357fb 20 3f 29 22 3b 0a 0a 20 20 72 63 20 3d 20 73 71   ?)";..  rc = sq
357fc 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
357fd 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
357fe 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28  pStmt, 0);.  if(
357ff 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35800 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
35801 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
35802 62 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2c  bind_text(pStmt,
35803 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53   1, zName, -1, S
35804 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
35805 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
35806 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20 26 70 2c  ob(pStmt, 2, &p,
35807 20 73 69 7a 65 6f 66 28 70 29 2c 20 53 51 4c 49   sizeof(p), SQLI
35808 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71  TE_STATIC);.  sq
35809 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
3580a 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  );..  return sql
3580b 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
3580c 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a  tmt);.}..static.
3580d 69 6e 74 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  int queryTokeniz
3580e 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  er(.  sqlite3 *d
3580f 62 2c 20 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  b, .  char *zNam
35810 65 2c 20 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c  e,  .  const sql
35811 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
35812 6f 64 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a 20 20  odule **pp.){.  
35813 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
35814 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20  3_stmt *pStmt;. 
35815 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c   const char zSql
35816 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73  [] = "SELECT fts
35817 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b  3_tokenizer(?)";
35818 0a 0a 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 72  ..  *pp = 0;.  r
35819 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  c = sqlite3_prep
3581a 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c  are_v2(db, zSql,
3581b 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b   -1, &pStmt, 0);
3581c 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3581d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
3581e 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71  rn rc;.  }..  sq
3581f 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28  lite3_bind_text(
35820 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c  pStmt, 1, zName,
35821 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54   -1, SQLITE_STAT
35822 49 43 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  IC);.  if( SQLIT
35823 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
35824 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20  tep(pStmt) ){.  
35825 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f    if( sqlite3_co
35826 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c  lumn_type(pStmt,
35827 20 30 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42   0)==SQLITE_BLOB
35828 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
35829 28 70 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c  (pp, sqlite3_col
3582a 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20  umn_blob(pStmt, 
3582b 30 29 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29 29  0), sizeof(*pp))
3582c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
3582d 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69  eturn sqlite3_fi
3582e 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d  nalize(pStmt);.}
3582f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
35830 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
35831 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72  3SimpleTokenizer
35832 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74  Module(sqlite3_t
35833 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
35834 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29  const**ppModule)
35835 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  ;../*.** Impleme
35836 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
35837 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66  calar function f
35838 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e  ts3_tokenizer_in
35839 74 65 72 6e 61 6c 5f 74 65 73 74 28 29 2e 0a 2a  ternal_test()..*
3583a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
3583b 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
3583c 69 6e 67 20 6f 6e 6c 79 2c 20 69 74 20 69 73 20  ing only, it is 
3583d 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  not included in 
3583e 74 68 65 0a 2a 2a 20 62 75 69 6c 64 20 75 6e 6c  the.** build unl
3583f 65 73 73 20 53 51 4c 49 54 45 5f 54 45 53 54 20  ess SQLITE_TEST 
35840 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a  is defined..**.*
35841 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66  * The purpose of
35842 20 74 68 69 73 20 69 73 20 74 6f 20 74 65 73 74   this is to test
35843 20 74 68 61 74 20 74 68 65 20 66 74 73 33 5f 74   that the fts3_t
35844 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74  okenizer() funct
35845 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73  ion.** can be us
35846 65 64 20 61 73 20 64 65 73 69 67 6e 65 64 20 62  ed as designed b
35847 79 20 74 68 65 20 43 2d 63 6f 64 65 20 69 6e 20  y the C-code in 
35848 74 68 65 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  the queryTokeniz
35849 65 72 20 61 6e 64 0a 2a 2a 20 72 65 67 69 73 74  er and.** regist
3584a 65 72 54 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75  erTokenizer() fu
3584b 6e 63 74 69 6f 6e 73 20 61 62 6f 76 65 2e 20 54  nctions above. T
3584c 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f  hese two functio
3584d 6e 73 20 61 72 65 20 72 65 70 65 61 74 65 64 0a  ns are repeated.
3584e 2a 2a 20 69 6e 20 74 68 65 20 52 45 41 44 4d 45  ** in the README
3584f 2e 74 6f 6b 65 6e 69 7a 65 72 20 66 69 6c 65 20  .tokenizer file 
35850 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 20 73  as an example, s
35851 6f 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  o it is importan
35852 74 20 74 6f 0a 2a 2a 20 74 65 73 74 20 74 68 65  t to.** test the
35853 6d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 75 6e 20  m..**.** To run 
35854 74 68 65 20 74 65 73 74 73 2c 20 65 76 61 6c 75  the tests, evalu
35855 61 74 65 20 74 68 65 20 66 74 73 33 5f 74 6f 6b  ate the fts3_tok
35856 65 6e 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f  enizer_internal_
35857 74 65 73 74 28 29 20 73 63 61 6c 61 72 0a 2a 2a  test() scalar.**
35858 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e   function with n
35859 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20 41 6e 20  o arguments. An 
3585a 61 73 73 65 72 74 28 29 20 77 69 6c 6c 20 66 61  assert() will fa
3585b 69 6c 20 69 66 20 61 20 70 72 6f 62 6c 65 6d 20  il if a problem 
3585c 69 73 0a 2a 2a 20 64 65 74 65 63 74 65 64 2e 20  is.** detected. 
3585d 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  i.e.:.**.**     
3585e 53 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65  SELECT fts3_toke
3585f 6e 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74  nizer_internal_t
35860 65 73 74 28 29 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61  est();.**.*/.sta
35861 74 69 63 20 76 6f 69 64 20 69 6e 74 54 65 73 74  tic void intTest
35862 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
35863 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
35864 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
35865 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
35866 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 72 63  argv.){.  int rc
35867 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
35868 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
35869 6c 65 20 2a 70 31 3b 0a 20 20 63 6f 6e 73 74 20  le *p1;.  const 
3586a 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3586b 72 5f 6d 6f 64 75 6c 65 20 2a 70 32 3b 0a 20 20  r_module *p2;.  
3586c 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73  sqlite3 *db = (s
3586d 71 6c 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33  qlite3 *)sqlite3
3586e 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
3586f 78 74 29 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20  xt);..  /* Test 
35870 74 68 65 20 71 75 65 72 79 20 66 75 6e 63 74 69  the query functi
35871 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 46  on */.  sqlite3F
35872 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  ts3SimpleTokeniz
35873 65 72 4d 6f 64 75 6c 65 28 26 70 31 29 3b 0a 20  erModule(&p1);. 
35874 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e   rc = queryToken
35875 69 7a 65 72 28 64 62 2c 20 22 73 69 6d 70 6c 65  izer(db, "simple
35876 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65 72  ", &p2);.  asser
35877 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
35878 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31   );.  assert( p1
35879 3d 3d 70 32 20 29 3b 0a 20 20 72 63 20 3d 20 71  ==p2 );.  rc = q
3587a 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62  ueryTokenizer(db
3587b 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a  , "nosuchtokeniz
3587c 65 72 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73  er", &p2);.  ass
3587d 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
3587e 45 52 52 4f 52 20 29 3b 0a 20 20 61 73 73 65 72  ERROR );.  asser
3587f 74 28 20 70 32 3d 3d 30 20 29 3b 0a 20 20 61 73  t( p2==0 );.  as
35880 73 65 72 74 28 20 30 3d 3d 73 74 72 63 6d 70 28  sert( 0==strcmp(
35881 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
35882 62 29 2c 20 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b  b), "unknown tok
35883 65 6e 69 7a 65 72 3a 20 6e 6f 73 75 63 68 74 6f  enizer: nosuchto
35884 6b 65 6e 69 7a 65 72 22 29 20 29 3b 0a 0a 20 20  kenizer") );..  
35885 2f 2a 20 54 65 73 74 20 74 68 65 20 73 74 6f 72  /* Test the stor
35886 61 67 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  age function */.
35887 20 20 72 63 20 3d 20 72 65 67 69 73 74 65 72 54    rc = registerT
35888 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f  okenizer(db, "no
35889 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20  suchtokenizer", 
3588a 70 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  p1);.  assert( r
3588b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
3588c 20 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65    rc = queryToke
3588d 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75 63  nizer(db, "nosuc
3588e 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20 26 70 32  htokenizer", &p2
3588f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  );.  assert( rc=
35890 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
35891 61 73 73 65 72 74 28 20 70 32 3d 3d 70 31 20 29  assert( p2==p1 )
35892 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ;..  sqlite3_res
35893 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
35894 2c 20 22 6f 6b 22 2c 20 2d 31 2c 20 53 51 4c 49  , "ok", -1, SQLI
35895 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 23  TE_STATIC);.}..#
35896 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  endif../*.** Set
35897 20 75 70 20 53 51 4c 20 6f 62 6a 65 63 74 73 20   up SQL objects 
35898 69 6e 20 64 61 74 61 62 61 73 65 20 64 62 20 75  in database db u
35899 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  sed to access th
3589a 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a  e contents of.**
3589b 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
3589c 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 72  pointed to by ar
3589d 67 75 6d 65 6e 74 20 70 48 61 73 68 2e 20 54 68  gument pHash. Th
3589e 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 75 73  e hash table mus
3589f 74 0a 2a 2a 20 62 65 65 6e 20 69 6e 69 74 69 61  t.** been initia
358a0 6c 69 73 65 64 20 74 6f 20 75 73 65 20 73 74 72  lised to use str
358a1 69 6e 67 20 6b 65 79 73 2c 20 61 6e 64 20 74 6f  ing keys, and to
358a2 20 74 61 6b 65 20 61 20 70 72 69 76 61 74 65 20   take a private 
358a3 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74 68 65 20  copy .** of the 
358a4 6b 65 79 20 77 68 65 6e 20 61 20 76 61 6c 75 65  key when a value
358a5 20 69 73 20 69 6e 73 65 72 74 65 64 2e 20 69 2e   is inserted. i.
358a6 65 2e 20 62 79 20 61 20 63 61 6c 6c 20 73 69 6d  e. by a call sim
358a7 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20  ilar to:.**.**  
358a8 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73    sqlite3Fts3Has
358a9 68 49 6e 69 74 28 70 48 61 73 68 2c 20 46 54 53  hInit(pHash, FTS
358aa 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31  3_HASH_STRING, 1
358ab 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  );.**.** This fu
358ac 6e 63 74 69 6f 6e 20 61 64 64 73 20 61 20 73 63  nction adds a sc
358ad 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 28 73  alar function (s
358ae 65 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  ee header commen
358af 74 20 61 62 6f 76 65 0a 2a 2a 20 73 63 61 6c 61  t above.** scala
358b0 72 46 75 6e 63 28 29 20 69 6e 20 74 68 69 73 20  rFunc() in this 
358b1 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73  file for details
358b2 29 20 61 6e 64 2c 20 69 66 20 45 4e 41 42 4c 45  ) and, if ENABLE
358b3 5f 54 41 42 4c 45 20 69 73 0a 2a 2a 20 64 65 66  _TABLE is.** def
358b4 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 61 74  ined at compilat
358b5 69 6f 6e 20 74 69 6d 65 2c 20 61 20 74 65 6d 70  ion time, a temp
358b6 6f 72 61 72 79 20 76 69 72 74 75 61 6c 20 74 61  orary virtual ta
358b7 62 6c 65 20 28 73 65 65 20 68 65 61 64 65 72 20  ble (see header 
358b8 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76  .** comment abov
358b9 65 20 73 74 72 75 63 74 20 48 61 73 68 54 61 62  e struct HashTab
358ba 6c 65 56 74 61 62 29 20 74 6f 20 74 68 65 20 64  leVtab) to the d
358bb 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
358bc 42 6f 74 68 20 0a 2a 2a 20 70 72 6f 76 69 64 65  Both .** provide
358bd 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65   read/write acce
358be 73 73 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e  ss to the conten
358bf 74 73 20 6f 66 20 2a 70 48 61 73 68 2e 0a 2a 2a  ts of *pHash..**
358c0 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72  .** The third ar
358c1 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
358c2 75 6e 63 74 69 6f 6e 2c 20 7a 4e 61 6d 65 2c 20  unction, zName, 
358c3 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 6e  is used as the n
358c4 61 6d 65 0a 2a 2a 20 6f 66 20 62 6f 74 68 20 74  ame.** of both t
358c5 68 65 20 73 63 61 6c 61 72 20 61 6e 64 2c 20 69  he scalar and, i
358c6 66 20 63 72 65 61 74 65 64 2c 20 74 68 65 20 76  f created, the v
358c7 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f  irtual table..*/
358c8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
358c9 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49  int sqlite3Fts3I
358ca 6e 69 74 48 61 73 68 54 61 62 6c 65 28 0a 20 20  nitHashTable(.  
358cb 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
358cc 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c  fts3Hash *pHash,
358cd 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
358ce 7a 4e 61 6d 65 0a 29 7b 0a 20 20 69 6e 74 20 72  zName.){.  int r
358cf 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
358d0 20 76 6f 69 64 20 2a 70 20 3d 20 28 76 6f 69 64   void *p = (void
358d1 20 2a 29 70 48 61 73 68 3b 0a 20 20 63 6f 6e 73   *)pHash;.  cons
358d2 74 20 69 6e 74 20 61 6e 79 20 3d 20 53 51 4c 49  t int any = SQLI
358d3 54 45 5f 41 4e 59 3b 0a 20 20 63 68 61 72 20 2a  TE_ANY;.  char *
358d4 7a 54 65 73 74 20 3d 20 30 3b 0a 20 20 63 68 61  zTest = 0;.  cha
358d5 72 20 2a 7a 54 65 73 74 32 20 3d 20 30 3b 0a 0a  r *zTest2 = 0;..
358d6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
358d7 53 54 0a 20 20 76 6f 69 64 20 2a 70 64 62 20 3d  ST.  void *pdb =
358d8 20 28 76 6f 69 64 20 2a 29 64 62 3b 0a 20 20 7a   (void *)db;.  z
358d9 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Test = sqlite3_m
358da 70 72 69 6e 74 66 28 22 25 73 5f 74 65 73 74 22  printf("%s_test"
358db 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7a 54 65 73  , zName);.  zTes
358dc 74 32 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  t2 = sqlite3_mpr
358dd 69 6e 74 66 28 22 25 73 5f 69 6e 74 65 72 6e 61  intf("%s_interna
358de 6c 5f 74 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b  l_test", zName);
358df 0a 20 20 69 66 28 20 21 7a 54 65 73 74 20 7c 7c  .  if( !zTest ||
358e0 20 21 7a 54 65 73 74 32 20 29 7b 0a 20 20 20 20   !zTest2 ){.    
358e1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
358e2 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  M;.  }.#endif.. 
358e3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
358e4 4f 4b 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20 73  OK.   || (rc = s
358e5 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
358e6 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65  nction(db, zName
358e7 2c 20 31 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61  , 1, any, p, sca
358e8 6c 61 72 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a  larFunc, 0, 0)).
358e9 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
358ea 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
358eb 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 32  ion(db, zName, 2
358ec 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72  , any, p, scalar
358ed 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 23 69 66  Func, 0, 0)).#if
358ee 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
358ef 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
358f0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
358f1 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c 20 32  ion(db, zTest, 2
358f2 2c 20 61 6e 79 2c 20 70 2c 20 74 65 73 74 46 75  , any, p, testFu
358f3 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c  nc, 0, 0)).   ||
358f4 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63   (rc = sqlite3_c
358f5 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
358f6 62 2c 20 7a 54 65 73 74 2c 20 33 2c 20 61 6e 79  b, zTest, 3, any
358f7 2c 20 70 2c 20 74 65 73 74 46 75 6e 63 2c 20 30  , p, testFunc, 0
358f8 2c 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20  , 0)).   || (rc 
358f9 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
358fa 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54  _function(db, zT
358fb 65 73 74 32 2c 20 30 2c 20 61 6e 79 2c 20 70 64  est2, 0, any, pd
358fc 62 2c 20 69 6e 74 54 65 73 74 46 75 6e 63 2c 20  b, intTestFunc, 
358fd 30 2c 20 30 29 29 0a 23 65 6e 64 69 66 0a 20 20  0, 0)).#endif.  
358fe 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
358ff 65 65 28 7a 54 65 73 74 29 3b 0a 20 20 73 71 6c  ee(zTest);.  sql
35900 69 74 65 33 5f 66 72 65 65 28 7a 54 65 73 74 32  ite3_free(zTest2
35901 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
35902 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  }..#endif /* !de
35903 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
35904 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
35905 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
35906 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
35907 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
35908 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a 2a  3_tokenizer.c **
35909 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3590a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3590b 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
3590c 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
3590d 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31   fts3_tokenizer1
3590e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
3590f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35910 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
35911 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68   Oct 10.**.** Th
35912 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
35913 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
35914 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
35915 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
35916 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
35917 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
35918 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
35919 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
3591a 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
3591b 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
3591c 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
3591d 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
3591e 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
3591f 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
35920 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
35921 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
35922 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
35923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35924 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35925 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35926 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35927 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
35928 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
35929 20 6f 66 20 74 68 65 20 22 73 69 6d 70 6c 65 22   of the "simple"
3592a 20 66 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63   full-text-searc
3592b 68 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a  h tokenizer..*/.
3592c 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  ./*.** The code 
3592d 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20  in this file is 
3592e 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66  only compiled if
3592f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  :.**.**     * Th
35930 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
35931 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20   being built as 
35932 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20  an extension.** 
35933 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20        (in which 
35934 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  case SQLITE_CORE
35935 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29   is not defined)
35936 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  , or.**.**     *
35937 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
35938 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
35939 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66  into the core of
3593a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65  .**       SQLite
3593b 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
3593c 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
3593d 53 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a  S3 is defined)..
3593e 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
3593f 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
35940 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
35941 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a  NABLE_FTS3).....
35942 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
35943 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
35944 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  {.  sqlite3_toke
35945 6e 69 7a 65 72 20 62 61 73 65 3b 0a 20 20 63 68  nizer base;.  ch
35946 61 72 20 64 65 6c 69 6d 5b 31 32 38 5d 3b 20 20  ar delim[128];  
35947 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c             /* fl
35948 61 67 20 41 53 43 49 49 20 64 65 6c 69 6d 69 74  ag ASCII delimit
35949 65 72 73 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f  ers */.} simple_
3594a 74 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 74 79 70 65  tokenizer;..type
3594b 64 65 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c  def struct simpl
3594c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  e_tokenizer_curs
3594d 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
3594e 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
3594f 62 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68  base;.  const ch
35950 61 72 20 2a 70 49 6e 70 75 74 3b 20 20 20 20 20  ar *pInput;     
35951 20 20 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65       /* input we
35952 20 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20   are tokenizing 
35953 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b  */.  int nBytes;
35954 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35955 20 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65    /* size of the
35956 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20   input */.  int 
35957 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  iOffset;        
35958 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72           /* curr
35959 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  ent position in 
3595a 70 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  pInput */.  int 
3595b 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  iToken;         
3595c 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65           /* inde
3595d 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20  x of next token 
3595e 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a  to be returned *
3595f 2f 0a 20 20 63 68 61 72 20 2a 70 54 6f 6b 65 6e  /.  char *pToken
35960 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35961 20 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f 72 20   /* storage for 
35962 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  current token */
35963 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 41 6c 6c  .  int nTokenAll
35964 6f 63 61 74 65 64 3b 20 20 20 20 20 20 20 20 20  ocated;         
35965 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  /* space allocat
35966 65 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66  ed to zToken buf
35967 66 65 72 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f  fer */.} simple_
35968 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
35969 3b 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  ;.../* Forward d
3596a 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74  eclaration */.st
3596b 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
3596c 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3596d 75 6c 65 20 73 69 6d 70 6c 65 54 6f 6b 65 6e 69  ule simpleTokeni
3596e 7a 65 72 4d 6f 64 75 6c 65 3b 0a 0a 73 74 61 74  zerModule;..stat
3596f 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 6c  ic int simpleDel
35970 69 6d 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  im(simple_tokeni
35971 7a 65 72 20 2a 74 2c 20 75 6e 73 69 67 6e 65 64  zer *t, unsigned
35972 20 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75   char c){.  retu
35973 72 6e 20 63 3c 30 78 38 30 20 26 26 20 74 2d 3e  rn c<0x80 && t->
35974 64 65 6c 69 6d 5b 63 5d 3b 0a 7d 0a 0a 2f 2a 0a  delim[c];.}../*.
35975 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
35976 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e  tokenizer instan
35977 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
35978 74 20 73 69 6d 70 6c 65 43 72 65 61 74 65 28 0a  t simpleCreate(.
35979 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
3597a 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
3597b 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argv,.  sqlite3_
3597c 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f  tokenizer **ppTo
3597d 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 73 69 6d  kenizer.){.  sim
3597e 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74  ple_tokenizer *t
3597f 3b 0a 0a 20 20 74 20 3d 20 28 73 69 6d 70 6c 65  ;..  t = (simple
35980 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71  _tokenizer *) sq
35981 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
35982 65 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66 28 20  eof(*t));.  if( 
35983 74 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  t==NULL ) return
35984 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
35985 20 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20 73 69   memset(t, 0, si
35986 7a 65 6f 66 28 2a 74 29 29 3b 0a 0a 20 20 2f 2a  zeof(*t));..  /*
35987 20 54 4f 44 4f 28 73 68 65 73 73 29 20 44 65 6c   TODO(shess) Del
35988 69 6d 69 74 65 72 73 20 6e 65 65 64 20 74 6f 20  imiters need to 
35989 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d 65 20  remain the same 
3598a 66 72 6f 6d 20 72 75 6e 20 74 6f 20 72 75 6e 2c  from run to run,
3598b 0a 20 20 2a 2a 20 65 6c 73 65 20 77 65 20 6e 65  .  ** else we ne
3598c 65 64 20 74 6f 20 72 65 69 6e 64 65 78 2e 20 20  ed to reindex.  
3598d 4f 6e 65 20 73 6f 6c 75 74 69 6f 6e 20 77 6f 75  One solution wou
3598e 6c 64 20 62 65 20 61 20 6d 65 74 61 2d 74 61 62  ld be a meta-tab
3598f 6c 65 20 74 6f 0a 20 20 2a 2a 20 74 72 61 63 6b  le to.  ** track
35990 20 73 75 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f   such informatio
35991 6e 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  n in the databas
35992 65 2c 20 74 68 65 6e 20 77 65 27 64 20 6f 6e 6c  e, then we'd onl
35993 79 20 77 61 6e 74 20 74 68 69 73 0a 20 20 2a 2a  y want this.  **
35994 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20   information on 
35995 74 68 65 20 69 6e 69 74 69 61 6c 20 63 72 65 61  the initial crea
35996 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61  te..  */.  if( a
35997 72 67 63 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74  rgc>1 ){.    int
35998 20 69 2c 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61   i, n = strlen(a
35999 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 66 6f 72  rgv[1]);.    for
3599a 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b  (i=0; i<n; i++){
3599b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
3599c 63 68 61 72 20 63 68 20 3d 20 61 72 67 76 5b 31  char ch = argv[1
3599d 5d 5b 69 5d 3b 0a 20 20 20 20 20 20 2f 2a 20 57  ][i];.      /* W
3599e 65 20 65 78 70 6c 69 63 69 74 6c 79 20 64 6f 6e  e explicitly don
3599f 27 74 20 73 75 70 70 6f 72 74 20 55 54 46 2d 38  't support UTF-8
359a0 20 64 65 6c 69 6d 69 74 65 72 73 20 66 6f 72 20   delimiters for 
359a1 6e 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  now. */.      if
359a2 28 20 63 68 3e 3d 30 78 38 30 20 29 7b 0a 20 20  ( ch>=0x80 ){.  
359a3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
359a4 65 65 28 74 29 3b 0a 20 20 20 20 20 20 20 20 72  ee(t);.        r
359a5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
359a6 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OR;.      }.    
359a7 20 20 74 2d 3e 64 65 6c 69 6d 5b 63 68 5d 20 3d    t->delim[ch] =
359a8 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c   1;.    }.  } el
359a9 73 65 20 7b 0a 20 20 20 20 2f 2a 20 4d 61 72 6b  se {.    /* Mark
359aa 20 6e 6f 6e 2d 61 6c 70 68 61 6e 75 6d 65 72 69   non-alphanumeri
359ab 63 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65  c ASCII characte
359ac 72 73 20 61 73 20 64 65 6c 69 6d 69 74 65 72 73  rs as delimiters
359ad 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   */.    int i;. 
359ae 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 30 78     for(i=1; i<0x
359af 38 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  80; i++){.      
359b0 74 2d 3e 64 65 6c 69 6d 5b 69 5d 20 3d 20 21 69  t->delim[i] = !i
359b1 73 61 6c 6e 75 6d 28 69 29 3b 0a 20 20 20 20 7d  salnum(i);.    }
359b2 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e  .  }..  *ppToken
359b3 69 7a 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b  izer = &t->base;
359b4 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
359b5 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
359b6 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65  stroy a tokenize
359b7 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  r.*/.static int 
359b8 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 28 73 71  simpleDestroy(sq
359b9 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
359ba 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20  *pTokenizer){.  
359bb 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f  sqlite3_free(pTo
359bc 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75  kenizer);.  retu
359bd 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
359be 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74  ./*.** Prepare t
359bf 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69  o begin tokenizi
359c0 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
359c1 73 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70  string.  The inp
359c2 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20  ut.** string to 
359c3 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20  be tokenized is 
359c4 70 49 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73  pInput[0..nBytes
359c5 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a  -1].  A cursor.*
359c6 2a 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d  * used to increm
359c7 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65  entally tokenize
359c8 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20   this string is 
359c9 72 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20  returned in .** 
359ca 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74  *ppCursor..*/.st
359cb 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 4f  atic int simpleO
359cc 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  pen(.  sqlite3_t
359cd 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
359ce 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
359cf 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
359d0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
359d1 70 49 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74  pInput, int nByt
359d2 65 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74  es,        /* St
359d3 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e  ring to be token
359d4 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ized */.  sqlite
359d5 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
359d6 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20  or **ppCursor   
359d7 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a   /* OUT: Tokeniz
359d8 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a  ation cursor */.
359d9 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ){.  simple_toke
359da 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b  nizer_cursor *c;
359db 0a 0a 20 20 63 20 3d 20 28 73 69 6d 70 6c 65 5f  ..  c = (simple_
359dc 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
359dd 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   *) sqlite3_mall
359de 6f 63 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b 0a  oc(sizeof(*c));.
359df 20 20 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29 20    if( c==NULL ) 
359e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
359e1 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 70 49 6e 70 75  MEM;..  c->pInpu
359e2 74 20 3d 20 70 49 6e 70 75 74 3b 0a 20 20 69 66  t = pInput;.  if
359e3 28 20 70 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20  ( pInput==0 ){. 
359e4 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 30     c->nBytes = 0
359e5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 42  ;.  }else if( nB
359e6 79 74 65 73 3c 30 20 29 7b 0a 20 20 20 20 63 2d  ytes<0 ){.    c-
359e7 3e 6e 42 79 74 65 73 20 3d 20 28 69 6e 74 29 73  >nBytes = (int)s
359e8 74 72 6c 65 6e 28 70 49 6e 70 75 74 29 3b 0a 20  trlen(pInput);. 
359e9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e 6e   }else{.    c->n
359ea 42 79 74 65 73 20 3d 20 6e 42 79 74 65 73 3b 0a  Bytes = nBytes;.
359eb 20 20 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74    }.  c->iOffset
359ec 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
359ed 20 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20 74        /* start t
359ee 6f 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68 65  okenizing at the
359ef 20 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20   beginning */.  
359f0 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20  c->iToken = 0;. 
359f1 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20 4e 55 4c   c->pToken = NUL
359f2 4c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L;              
359f3 20 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c   /* no space all
359f4 6f 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0a  ocated, yet. */.
359f5 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63    c->nTokenAlloc
359f6 61 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70 70  ated = 0;..  *pp
359f7 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73  Cursor = &c->bas
359f8 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  e;.  return SQLI
359f9 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
359fa 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61  Close a tokeniza
359fb 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65 76  tion cursor prev
359fc 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62 79  iously opened by
359fd 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 69   a call to.** si
359fe 6d 70 6c 65 4f 70 65 6e 28 29 20 61 62 6f 76 65  mpleOpen() above
359ff 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35a00 73 69 6d 70 6c 65 43 6c 6f 73 65 28 73 71 6c 69  simpleClose(sqli
35a01 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
35a02 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a  rsor *pCursor){.
35a03 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a    simple_tokeniz
35a04 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  er_cursor *c = (
35a05 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
35a06 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73  _cursor *) pCurs
35a07 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  or;.  sqlite3_fr
35a08 65 65 28 63 2d 3e 70 54 6f 6b 65 6e 29 3b 0a 20  ee(c->pToken);. 
35a09 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 29   sqlite3_free(c)
35a0a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
35a0b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  E_OK;.}../*.** E
35a0c 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74 20  xtract the next 
35a0d 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b  token from a tok
35a0e 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
35a0f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75  .  The cursor mu
35a10 73 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20  st.** have been 
35a11 6f 70 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f  opened by a prio
35a12 72 20 63 61 6c 6c 20 74 6f 20 73 69 6d 70 6c 65  r call to simple
35a13 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Open()..*/.stati
35a14 63 20 69 6e 74 20 73 69 6d 70 6c 65 4e 65 78 74  c int simpleNext
35a15 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
35a16 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
35a17 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f  ursor,  /* Curso
35a18 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 69  r returned by si
35a19 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f  mpleOpen */.  co
35a1a 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f 6b  nst char **ppTok
35a1b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  en,             
35a1c 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 70 54 6f 6b    /* OUT: *ppTok
35a1d 65 6e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20  en is the token 
35a1e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  text */.  int *p
35a1f 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
35a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35a21 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20   OUT: Number of 
35a22 62 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a  bytes in token *
35a23 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74  /.  int *piStart
35a24 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
35a25 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
35a26 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20  Starting offset 
35a27 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  of token */.  in
35a28 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20  t *piEndOffset, 
35a29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a2a 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67    /* OUT: Ending
35a2b 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
35a2c 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73   */.  int *piPos
35a2d 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20  ition           
35a2e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
35a2f 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67  : Position integ
35a30 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29  er of token */.)
35a31 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  {.  simple_token
35a32 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d  izer_cursor *c =
35a33 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a   (simple_tokeniz
35a34 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75  er_cursor *) pCu
35a35 72 73 6f 72 3b 0a 20 20 73 69 6d 70 6c 65 5f 74  rsor;.  simple_t
35a36 6f 6b 65 6e 69 7a 65 72 20 2a 74 20 3d 20 28 73  okenizer *t = (s
35a37 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
35a38 2a 29 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b  *) pCursor->pTok
35a39 65 6e 69 7a 65 72 3b 0a 20 20 75 6e 73 69 67 6e  enizer;.  unsign
35a3a 65 64 20 63 68 61 72 20 2a 70 20 3d 20 28 75 6e  ed char *p = (un
35a3b 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 63 2d  signed char *)c-
35a3c 3e 70 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c  >pInput;..  whil
35a3d 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
35a3e 3e 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 69  >nBytes ){.    i
35a3f 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b  nt iStartOffset;
35a40 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 70 61  ..    /* Scan pa
35a41 73 74 20 64 65 6c 69 6d 69 74 65 72 20 63 68 61  st delimiter cha
35a42 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 20 20 77  racters */.    w
35a43 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74  hile( c->iOffset
35a44 3c 63 2d 3e 6e 42 79 74 65 73 20 26 26 20 73 69  <c->nBytes && si
35a45 6d 70 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63  mpleDelim(t, p[c
35a46 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20  ->iOffset]) ){. 
35a47 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b       c->iOffset+
35a48 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  +;.    }..    /*
35a49 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d   Count non-delim
35a4a 69 74 65 72 20 63 68 61 72 61 63 74 65 72 73 2e  iter characters.
35a4b 20 2a 2f 0a 20 20 20 20 69 53 74 61 72 74 4f 66   */.    iStartOf
35a4c 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65  fset = c->iOffse
35a4d 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d  t;.    while( c-
35a4e 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74  >iOffset<c->nByt
35a4f 65 73 20 26 26 20 21 73 69 6d 70 6c 65 44 65 6c  es && !simpleDel
35a50 69 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73  im(t, p[c->iOffs
35a51 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 2d  et]) ){.      c-
35a52 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20  >iOffset++;.    
35a53 7d 0a 0a 20 20 20 20 69 66 28 20 63 2d 3e 69 4f  }..    if( c->iO
35a54 66 66 73 65 74 3e 69 53 74 61 72 74 4f 66 66 73  ffset>iStartOffs
35a55 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  et ){.      int 
35a56 69 2c 20 6e 20 3d 20 63 2d 3e 69 4f 66 66 73 65  i, n = c->iOffse
35a57 74 2d 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a  t-iStartOffset;.
35a58 20 20 20 20 20 20 69 66 28 20 6e 3e 63 2d 3e 6e        if( n>c->n
35a59 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 29  TokenAllocated )
35a5a 7b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e 54 6f  {.        c->nTo
35a5b 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e  kenAllocated = n
35a5c 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e  +20;.        c->
35a5d 70 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33  pToken = sqlite3
35a5e 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 70 54 6f 6b  _realloc(c->pTok
35a5f 65 6e 2c 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c  en, c->nTokenAll
35a60 6f 63 61 74 65 64 29 3b 0a 20 20 20 20 20 20 20  ocated);.       
35a61 20 69 66 28 20 63 2d 3e 70 54 6f 6b 65 6e 3d 3d   if( c->pToken==
35a62 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51  NULL ) return SQ
35a63 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
35a64 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d    }.      for(i=
35a65 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<n; i++){.  
35a66 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68        /* TODO(sh
35a67 65 73 73 29 20 54 68 69 73 20 6e 65 65 64 73 20  ess) This needs 
35a68 65 78 70 61 6e 73 69 6f 6e 20 74 6f 20 68 61 6e  expansion to han
35a69 64 6c 65 20 55 54 46 2d 38 0a 20 20 20 20 20 20  dle UTF-8.      
35a6a 20 20 2a 2a 20 63 61 73 65 2d 69 6e 73 65 6e 73    ** case-insens
35a6b 69 74 69 76 69 74 79 2e 0a 20 20 20 20 20 20 20  itivity..       
35a6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 73 69   */.        unsi
35a6d 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d 20 70  gned char ch = p
35a6e 5b 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69 5d  [iStartOffset+i]
35a6f 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e 70 54 6f  ;.        c->pTo
35a70 6b 65 6e 5b 69 5d 20 3d 20 63 68 3c 30 78 38 30  ken[i] = ch<0x80
35a71 20 3f 20 74 6f 6c 6f 77 65 72 28 63 68 29 20 3a   ? tolower(ch) :
35a72 20 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   ch;.      }.   
35a73 20 20 20 2a 70 70 54 6f 6b 65 6e 20 3d 20 63 2d     *ppToken = c-
35a74 3e 70 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 2a  >pToken;.      *
35a75 70 6e 42 79 74 65 73 20 3d 20 6e 3b 0a 20 20 20  pnBytes = n;.   
35a76 20 20 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65     *piStartOffse
35a77 74 20 3d 20 69 53 74 61 72 74 4f 66 66 73 65 74  t = iStartOffset
35a78 3b 0a 20 20 20 20 20 20 2a 70 69 45 6e 64 4f 66  ;.      *piEndOf
35a79 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65  fset = c->iOffse
35a7a 74 3b 0a 20 20 20 20 20 20 2a 70 69 50 6f 73 69  t;.      *piPosi
35a7b 74 69 6f 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e  tion = c->iToken
35a7c 2b 2b 3b 0a 0a 20 20 20 20 20 20 72 65 74 75 72  ++;..      retur
35a7d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
35a7e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
35a7f 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a  SQLITE_DONE;.}..
35a80 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66  /*.** The set of
35a81 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 69   routines that i
35a82 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 69 6d  mplement the sim
35a83 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f  ple tokenizer.*/
35a84 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
35a85 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
35a86 6d 6f 64 75 6c 65 20 73 69 6d 70 6c 65 54 6f 6b  module simpleTok
35a87 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b  enizerModule = {
35a88 0a 20 20 30 2c 0a 20 20 73 69 6d 70 6c 65 43 72  .  0,.  simpleCr
35a89 65 61 74 65 2c 0a 20 20 73 69 6d 70 6c 65 44 65  eate,.  simpleDe
35a8a 73 74 72 6f 79 2c 0a 20 20 73 69 6d 70 6c 65 4f  stroy,.  simpleO
35a8b 70 65 6e 2c 0a 20 20 73 69 6d 70 6c 65 43 6c 6f  pen,.  simpleClo
35a8c 73 65 2c 0a 20 20 73 69 6d 70 6c 65 4e 65 78 74  se,.  simpleNext
35a8d 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  ,.};../*.** Allo
35a8e 63 61 74 65 20 61 20 6e 65 77 20 73 69 6d 70 6c  cate a new simpl
35a8f 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65  e tokenizer.  Re
35a90 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
35a91 6f 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b  o the new.** tok
35a92 65 6e 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64  enizer in *ppMod
35a93 75 6c 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ule.*/.SQLITE_PR
35a94 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
35a95 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
35a96 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73  nizerModule(.  s
35a97 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
35a98 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
35a99 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70  pModule.){.  *pp
35a9a 4d 6f 64 75 6c 65 20 3d 20 26 73 69 6d 70 6c 65  Module = &simple
35a9b 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b  TokenizerModule;
35a9c 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  .}..#endif /* !d
35a9d 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
35a9e 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
35a9f 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
35aa0 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  3) */../********
35aa1 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
35aa2 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20  s3_tokenizer1.c 
35aa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35aa4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35aa5 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
35aa6 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
35aa7 65 20 72 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a  e rtree.c ******
35aa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35aa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35aaa 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
35aab 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
35aac 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
35aad 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
35aae 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
35aaf 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
35ab0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
35ab1 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
35ab2 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
35ab3 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
35ab4 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
35ab5 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
35ab6 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
35ab7 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
35ab8 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
35ab9 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
35aba 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
35abb 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
35abc 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
35abd 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
35abe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35abf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35ac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
35ac2 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
35ac3 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 69 6d 70  ins code for imp
35ac4 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
35ac5 74 68 65 20 72 2d 74 72 65 65 20 61 6e 64 20 72  the r-tree and r
35ac6 2a 2d 74 72 65 65 0a 2a 2a 20 61 6c 67 6f 72 69  *-tree.** algori
35ac7 74 68 6d 73 20 70 61 63 6b 61 67 65 64 20 61 73  thms packaged as
35ac8 20 61 6e 20 53 51 4c 69 74 65 20 76 69 72 74 75   an SQLite virtu
35ac9 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e  al table module.
35aca 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 74 72 65  .**.** $Id: rtre
35acb 65 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 38 2f  e.c,v 1.12 2008/
35acc 31 32 2f 32 32 20 31 35 3a 30 34 3a 33 32 20 64  12/22 15:04:32 d
35acd 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
35ace 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  .*/..#if !define
35acf 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
35ad0 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
35ad1 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 29 0a 0a  _ENABLE_RTREE)..
35ad2 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  /*.** This file 
35ad3 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6d 70 6c  contains an impl
35ad4 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20  ementation of a 
35ad5 63 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72  couple of differ
35ad6 65 6e 74 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20  ent variants.** 
35ad7 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 61 6c  of the r-tree al
35ad8 67 6f 72 69 74 68 6d 2e 20 53 65 65 20 74 68 65  gorithm. See the
35ad9 20 52 45 41 44 4d 45 20 66 69 6c 65 20 66 6f 72   README file for
35ada 20 66 75 72 74 68 65 72 20 64 65 74 61 69 6c 73   further details
35adb 2e 20 54 68 65 20 0a 2a 2a 20 73 61 6d 65 20 64  . The .** same d
35adc 61 74 61 2d 73 74 72 75 63 74 75 72 65 20 69 73  ata-structure is
35add 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 2c 20 62   used for all, b
35ade 75 74 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d  ut the algorithm
35adf 73 20 66 6f 72 20 69 6e 73 65 72 74 20 61 6e 64  s for insert and
35ae0 0a 2a 2a 20 64 65 6c 65 74 65 20 6f 70 65 72 61  .** delete opera
35ae1 74 69 6f 6e 73 20 76 61 72 79 2e 20 54 68 65 20  tions vary. The 
35ae2 76 61 72 69 61 6e 74 73 20 75 73 65 64 20 61 72  variants used ar
35ae3 65 20 73 65 6c 65 63 74 65 64 20 61 74 20 63 6f  e selected at co
35ae4 6d 70 69 6c 65 20 74 69 6d 65 20 0a 2a 2a 20 62  mpile time .** b
35ae5 79 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 66  y defining the f
35ae6 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 73  ollowing symbols
35ae7 3a 0a 2a 2f 0a 0a 2f 2a 20 45 69 74 68 65 72 2c  :.*/../* Either,
35ae8 20 62 6f 74 68 20 6f 72 20 6e 6f 6e 65 20 6f 66   both or none of
35ae9 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
35aea 61 79 20 62 65 20 73 65 74 20 74 6f 20 61 63 74  ay be set to act
35aeb 69 76 61 74 65 20 0a 2a 2a 20 72 2a 74 72 65 65  ivate .** r*tree
35aec 20 76 61 72 69 61 6e 74 20 61 6c 67 6f 72 69 74   variant algorit
35aed 68 6d 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  hms..*/.#define 
35aee 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
35aef 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 20  E_CHOOSESUBTREE 
35af0 30 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e  0.#define VARIAN
35af1 54 5f 52 53 54 41 52 54 52 45 45 5f 52 45 49 4e  T_RSTARTREE_REIN
35af2 53 45 52 54 20 20 20 20 20 20 31 0a 0a 2f 2a 20  SERT      1../* 
35af3 0a 2a 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20  .** Exactly one 
35af4 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
35af5 20 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20   must be set to 
35af6 31 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41  1..*/.#define VA
35af7 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55  RIANT_GUTTMAN_QU
35af8 41 44 52 41 54 49 43 5f 53 50 4c 49 54 20 30 0a  ADRATIC_SPLIT 0.
35af9 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f  #define VARIANT_
35afa 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53  GUTTMAN_LINEAR_S
35afb 50 4c 49 54 20 20 20 20 30 0a 23 64 65 66 69 6e  PLIT    0.#defin
35afc 65 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54  e VARIANT_RSTART
35afd 52 45 45 5f 53 50 4c 49 54 20 20 20 20 20 20 20  REE_SPLIT       
35afe 20 20 31 0a 0a 23 64 65 66 69 6e 65 20 56 41 52    1..#define VAR
35aff 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c  IANT_GUTTMAN_SPL
35b00 49 54 20 5c 0a 20 20 20 20 20 20 20 20 28 56 41  IT \.        (VA
35b01 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49  RIANT_GUTTMAN_LI
35b02 4e 45 41 52 5f 53 50 4c 49 54 7c 7c 56 41 52 49  NEAR_SPLIT||VARI
35b03 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44  ANT_GUTTMAN_QUAD
35b04 52 41 54 49 43 5f 53 50 4c 49 54 29 0a 0a 23 69  RATIC_SPLIT)..#i
35b05 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
35b06 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49  N_QUADRATIC_SPLI
35b07 54 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  T.  #define Pick
35b08 4e 65 78 74 20 51 75 61 64 72 61 74 69 63 50 69  Next QuadraticPi
35b09 63 6b 4e 65 78 74 0a 20 20 23 64 65 66 69 6e 65  ckNext.  #define
35b0a 20 50 69 63 6b 53 65 65 64 73 20 51 75 61 64 72   PickSeeds Quadr
35b0b 61 74 69 63 50 69 63 6b 53 65 65 64 73 0a 20 20  aticPickSeeds.  
35b0c 23 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65  #define AssignCe
35b0d 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65 47 75 74  lls splitNodeGut
35b0e 74 6d 61 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20  tman.#endif.#if 
35b0f 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
35b10 4c 49 4e 45 41 52 5f 53 50 4c 49 54 0a 20 20 23  LINEAR_SPLIT.  #
35b11 64 65 66 69 6e 65 20 50 69 63 6b 4e 65 78 74 20  define PickNext 
35b12 4c 69 6e 65 61 72 50 69 63 6b 4e 65 78 74 0a 20  LinearPickNext. 
35b13 20 23 64 65 66 69 6e 65 20 50 69 63 6b 53 65 65   #define PickSee
35b14 64 73 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65  ds LinearPickSee
35b15 64 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73  ds.  #define Ass
35b16 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f  ignCells splitNo
35b17 64 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66  deGuttman.#endif
35b18 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54  .#if VARIANT_RST
35b19 41 52 54 52 45 45 5f 53 50 4c 49 54 0a 20 20 23  ARTREE_SPLIT.  #
35b1a 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65 6c  define AssignCel
35b1b 6c 73 20 73 70 6c 69 74 4e 6f 64 65 53 74 61 72  ls splitNodeStar
35b1c 74 72 65 65 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  tree.#endif...#i
35b1d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52  fndef SQLITE_COR
35b1e 45 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  E.  SQLITE_EXTEN
35b1f 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65  SION_INIT1.#else
35b20 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65  .#endif...#ifnde
35b21 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  f SQLITE_AMALGAM
35b22 41 54 49 4f 4e 0a 74 79 70 65 64 65 66 20 73 71  ATION.typedef sq
35b23 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 36 34 3b  lite3_int64 i64;
35b24 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
35b25 64 20 63 68 61 72 20 75 38 3b 0a 74 79 70 65 64  d char u8;.typed
35b26 65 66 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  ef unsigned int 
35b27 75 33 32 3b 0a 23 65 6e 64 69 66 0a 0a 74 79 70  u32;.#endif..typ
35b28 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65  edef struct Rtre
35b29 65 20 52 74 72 65 65 3b 0a 74 79 70 65 64 65 66  e Rtree;.typedef
35b2a 20 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72   struct RtreeCur
35b2b 73 6f 72 20 52 74 72 65 65 43 75 72 73 6f 72 3b  sor RtreeCursor;
35b2c 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
35b2d 52 74 72 65 65 4e 6f 64 65 20 52 74 72 65 65 4e  RtreeNode RtreeN
35b2e 6f 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ode;.typedef str
35b2f 75 63 74 20 52 74 72 65 65 43 65 6c 6c 20 52 74  uct RtreeCell Rt
35b30 72 65 65 43 65 6c 6c 3b 0a 74 79 70 65 64 65 66  reeCell;.typedef
35b31 20 73 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e   struct RtreeCon
35b32 73 74 72 61 69 6e 74 20 52 74 72 65 65 43 6f 6e  straint RtreeCon
35b33 73 74 72 61 69 6e 74 3b 0a 74 79 70 65 64 65 66  straint;.typedef
35b34 20 75 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72   union RtreeCoor
35b35 64 20 52 74 72 65 65 43 6f 6f 72 64 3b 0a 0a 2f  d RtreeCoord;../
35b36 2a 20 54 68 65 20 72 74 72 65 65 20 6d 61 79 20  * The rtree may 
35b37 68 61 76 65 20 62 65 74 77 65 65 6e 20 31 20 61  have between 1 a
35b38 6e 64 20 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d  nd RTREE_MAX_DIM
35b39 45 4e 53 49 4f 4e 53 20 64 69 6d 65 6e 73 69 6f  ENSIONS dimensio
35b3a 6e 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ns. */.#define R
35b3b 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49  TREE_MAX_DIMENSI
35b3c 4f 4e 53 20 35 0a 0a 2f 2a 20 53 69 7a 65 20 6f  ONS 5../* Size o
35b3d 66 20 68 61 73 68 20 74 61 62 6c 65 20 52 74 72  f hash table Rtr
35b3e 65 65 2e 61 48 61 73 68 2e 20 54 68 69 73 20 68  ee.aHash. This h
35b3f 61 73 68 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  ash table is not
35b40 20 65 78 70 65 63 74 65 64 20 74 6f 0a 2a 2a 20   expected to.** 
35b41 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 76 65 72  ever contain ver
35b42 79 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 2c 20  y many entries, 
35b43 73 6f 20 61 20 66 69 78 65 64 20 6e 75 6d 62 65  so a fixed numbe
35b44 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 73 20  r of buckets is 
35b45 0a 2a 2a 20 75 73 65 64 2e 0a 2a 2f 0a 23 64 65  .** used..*/.#de
35b46 66 69 6e 65 20 48 41 53 48 53 49 5a 45 20 31 32  fine HASHSIZE 12
35b47 38 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72  8../* .** An rtr
35b48 65 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  ee virtual-table
35b49 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75   object..*/.stru
35b4a 63 74 20 52 74 72 65 65 20 7b 0a 20 20 73 71 6c  ct Rtree {.  sql
35b4b 69 74 65 33 5f 76 74 61 62 20 62 61 73 65 3b 0a  ite3_vtab base;.
35b4c 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
35b4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35b4e 20 48 6f 73 74 20 64 61 74 61 62 61 73 65 20 63   Host database c
35b4f 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69  onnection */.  i
35b50 6e 74 20 69 4e 6f 64 65 53 69 7a 65 3b 20 20 20  nt iNodeSize;   
35b51 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
35b52 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 65  ze in bytes of e
35b53 61 63 68 20 6e 6f 64 65 20 69 6e 20 74 68 65 20  ach node in the 
35b54 6e 6f 64 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  node table */.  
35b55 69 6e 74 20 6e 44 69 6d 3b 20 20 20 20 20 20 20  int nDim;       
35b56 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
35b57 75 6d 62 65 72 20 6f 66 20 64 69 6d 65 6e 73 69  umber of dimensi
35b58 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  ons */.  int nBy
35b59 74 65 73 50 65 72 43 65 6c 6c 3b 20 20 20 20 20  tesPerCell;     
35b5a 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 63 6f       /* Bytes co
35b5b 6e 73 75 6d 65 64 20 70 65 72 20 63 65 6c 6c 20  nsumed per cell 
35b5c 2a 2f 0a 20 20 69 6e 74 20 69 44 65 70 74 68 3b  */.  int iDepth;
35b5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35b5e 20 2f 2a 20 43 75 72 72 65 6e 74 20 64 65 70 74   /* Current dept
35b5f 68 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  h of the r-tree 
35b60 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63  structure */.  c
35b61 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
35b62 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
35b63 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 63  me of database c
35b64 6f 6e 74 61 69 6e 69 6e 67 20 72 2d 74 72 65 65  ontaining r-tree
35b65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
35b66 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
35b67 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
35b68 6f 66 20 72 2d 74 72 65 65 20 74 61 62 6c 65 20  of r-tree table 
35b69 2a 2f 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  */ .  RtreeNode 
35b6a 2a 61 48 61 73 68 5b 48 41 53 48 53 49 5a 45 5d  *aHash[HASHSIZE]
35b6b 3b 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20  ; /* Hash table 
35b6c 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64  of in-memory nod
35b6d 65 73 2e 20 2a 2f 20 0a 20 20 69 6e 74 20 6e 42  es. */ .  int nB
35b6e 75 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  usy;            
35b6f 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
35b70 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73   number of users
35b71 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
35b72 72 65 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 69 73 74  re */..  /* List
35b73 20 6f 66 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65   of nodes remove
35b74 64 20 64 75 72 69 6e 67 20 61 20 43 6f 6e 64 65  d during a Conde
35b75 6e 73 65 54 72 65 65 20 6f 70 65 72 61 74 69 6f  nseTree operatio
35b76 6e 2e 20 4c 69 73 74 20 69 73 0a 20 20 2a 2a 20  n. List is.  ** 
35b77 6c 69 6e 6b 65 64 20 74 6f 67 65 74 68 65 72 20  linked together 
35b78 76 69 61 20 74 68 65 20 70 6f 69 6e 74 65 72 20  via the pointer 
35b79 6e 6f 72 6d 61 6c 6c 79 20 75 73 65 64 20 66 6f  normally used fo
35b7a 72 20 68 61 73 68 20 63 68 61 69 6e 73 20 2d 0a  r hash chains -.
35b7b 20 20 2a 2a 20 52 74 72 65 65 4e 6f 64 65 2e 70    ** RtreeNode.p
35b7c 4e 65 78 74 2e 20 52 74 72 65 65 4e 6f 64 65 2e  Next. RtreeNode.
35b7d 69 4e 6f 64 65 20 73 74 6f 72 65 73 20 74 68 65  iNode stores the
35b7e 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 73 75   depth of the su
35b7f 62 2d 74 72 65 65 20 0a 20 20 2a 2a 20 68 65 61  b-tree .  ** hea
35b80 64 65 64 20 62 79 20 74 68 65 20 6e 6f 64 65 20  ded by the node 
35b81 28 6c 65 61 66 20 6e 6f 64 65 73 20 68 61 76 65  (leaf nodes have
35b82 20 52 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65   RtreeNode.iNode
35b83 3d 3d 30 29 2e 0a 20 20 2a 2f 0a 20 20 52 74 72  ==0)..  */.  Rtr
35b84 65 65 4e 6f 64 65 20 2a 70 44 65 6c 65 74 65 64  eeNode *pDeleted
35b85 3b 0a 20 20 69 6e 74 20 69 52 65 69 6e 73 65 72  ;.  int iReinser
35b86 74 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  tHeight;        
35b87 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 73 75 62  /* Height of sub
35b88 2d 74 72 65 65 73 20 52 65 69 6e 73 65 72 74 28  -trees Reinsert(
35b89 29 20 68 61 73 20 72 75 6e 20 6f 6e 20 2a 2f 0a  ) has run on */.
35b8a 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73  .  /* Statements
35b8b 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64   to read/write/d
35b8c 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20 66  elete a record f
35b8d 72 6f 6d 20 78 78 78 5f 6e 6f 64 65 20 2a 2f 0a  rom xxx_node */.
35b8e 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
35b8f 70 52 65 61 64 4e 6f 64 65 3b 0a 20 20 73 71 6c  pReadNode;.  sql
35b90 69 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74  ite3_stmt *pWrit
35b91 65 4e 6f 64 65 3b 0a 20 20 73 71 6c 69 74 65 33  eNode;.  sqlite3
35b92 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65 4e 6f  _stmt *pDeleteNo
35b93 64 65 3b 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d  de;..  /* Statem
35b94 65 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72 69  ents to read/wri
35b95 74 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63 6f  te/delete a reco
35b96 72 64 20 66 72 6f 6d 20 78 78 78 5f 72 6f 77 69  rd from xxx_rowi
35b97 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  d */.  sqlite3_s
35b98 74 6d 74 20 2a 70 52 65 61 64 52 6f 77 69 64 3b  tmt *pReadRowid;
35b99 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
35b9a 2a 70 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20  *pWriteRowid;.  
35b9b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44  sqlite3_stmt *pD
35b9c 65 6c 65 74 65 52 6f 77 69 64 3b 0a 0a 20 20 2f  eleteRowid;..  /
35b9d 2a 20 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20  * Statements to 
35b9e 72 65 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74  read/write/delet
35b9f 65 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20  e a record from 
35ba0 78 78 78 5f 70 61 72 65 6e 74 20 2a 2f 0a 20 20  xxx_parent */.  
35ba1 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 52  sqlite3_stmt *pR
35ba2 65 61 64 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c  eadParent;.  sql
35ba3 69 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74  ite3_stmt *pWrit
35ba4 65 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74  eParent;.  sqlit
35ba5 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65  e3_stmt *pDelete
35ba6 50 61 72 65 6e 74 3b 0a 0a 20 20 69 6e 74 20 65  Parent;..  int e
35ba7 43 6f 6f 72 64 54 79 70 65 3b 0a 7d 3b 0a 0a 2f  CoordType;.};../
35ba8 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  * Possible value
35ba9 73 20 66 6f 72 20 65 43 6f 6f 72 64 54 79 70 65  s for eCoordType
35baa 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54 52  : */.#define RTR
35bab 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20  EE_COORD_REAL32 
35bac 30 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  0.#define RTREE_
35bad 43 4f 4f 52 44 5f 49 4e 54 33 32 20 20 31 0a 0a  COORD_INT32  1..
35bae 2f 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75  /*.** The minimu
35baf 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  m number of cell
35bb0 73 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 61 20  s allowed for a 
35bb1 6e 6f 64 65 20 69 73 20 61 20 74 68 69 72 64 20  node is a third 
35bb2 6f 66 20 74 68 65 20 0a 2a 2a 20 6d 61 78 69 6d  of the .** maxim
35bb3 75 6d 2e 20 49 6e 20 47 75 74 6d 61 6e 27 73 20  um. In Gutman's 
35bb4 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  notation:.**.** 
35bb5 20 20 20 20 6d 20 3d 20 4d 2f 33 0a 2a 2a 0a 2a      m = M/3.**.*
35bb6 2a 20 49 66 20 61 6e 20 52 2a 2d 74 72 65 65 20  * If an R*-tree 
35bb7 22 52 65 69 6e 73 65 72 74 22 20 6f 70 65 72 61  "Reinsert" opera
35bb8 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64  tion is required
35bb9 2c 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65  , the same numbe
35bba 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 61 72  r of.** cells ar
35bbb 65 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  e removed from t
35bbc 68 65 20 6f 76 65 72 66 75 6c 6c 20 6e 6f 64 65  he overfull node
35bbd 20 61 6e 64 20 72 65 69 6e 73 65 72 74 65 64 20   and reinserted 
35bbe 69 6e 74 6f 20 74 68 65 20 74 72 65 65 2e 0a 2a  into the tree..*
35bbf 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
35bc0 4d 49 4e 43 45 4c 4c 53 28 70 29 20 28 28 28 28  MINCELLS(p) ((((
35bc1 70 29 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 34 29  p)->iNodeSize-4)
35bc2 2f 28 70 29 2d 3e 6e 42 79 74 65 73 50 65 72 43  /(p)->nBytesPerC
35bc3 65 6c 6c 29 2f 33 29 0a 23 64 65 66 69 6e 65 20  ell)/3).#define 
35bc4 52 54 52 45 45 5f 52 45 49 4e 53 45 52 54 28 70  RTREE_REINSERT(p
35bc5 29 20 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53  ) RTREE_MINCELLS
35bc6 28 70 29 0a 23 64 65 66 69 6e 65 20 52 54 52 45  (p).#define RTRE
35bc7 45 5f 4d 41 58 43 45 4c 4c 53 20 35 31 0a 0a 2f  E_MAXCELLS 51../
35bc8 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20 63  * .** An rtree c
35bc9 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f  ursor object..*/
35bca 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72  .struct RtreeCur
35bcb 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  sor {.  sqlite3_
35bcc 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65  vtab_cursor base
35bcd 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ;.  RtreeNode *p
35bce 4e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Node;           
35bcf 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 63 75        /* Node cu
35bd0 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c  rsor is currentl
35bd1 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f  y pointing at */
35bd2 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20  .  int iCell;   
35bd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bd4 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
35bd5 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e   current cell in
35bd6 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20   pNode */.  int 
35bd7 69 53 74 72 61 74 65 67 79 3b 20 20 20 20 20 20  iStrategy;      
35bd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35bd9 20 43 6f 70 79 20 6f 66 20 69 64 78 4e 75 6d 20   Copy of idxNum 
35bda 73 65 61 72 63 68 20 70 61 72 61 6d 65 74 65 72  search parameter
35bdb 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74   */.  int nConst
35bdc 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  raint;          
35bdd 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
35bde 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
35bdf 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20  aConstraint */. 
35be0 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74   RtreeConstraint
35be1 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20   *aConstraint;  
35be2 20 20 20 2f 2a 20 53 65 61 72 63 68 20 63 6f 6e     /* Search con
35be3 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a 7d 3b 0a  straints. */.};.
35be4 0a 75 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72  .union RtreeCoor
35be5 64 20 7b 0a 20 20 66 6c 6f 61 74 20 66 3b 0a 20  d {.  float f;. 
35be6 20 69 6e 74 20 69 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a   int i;.};../*.*
35be7 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
35be8 73 20 61 6e 20 52 74 72 65 65 43 6f 6f 72 64 2e  s an RtreeCoord.
35be9 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75   Return the valu
35bea 65 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20  e stored within 
35beb 74 68 65 20 52 74 72 65 65 43 6f 6f 72 64 0a 2a  the RtreeCoord.*
35bec 2a 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 61  * formatted as a
35bed 20 64 6f 75 62 6c 65 2e 20 54 68 69 73 20 6d 61   double. This ma
35bee 63 72 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74  cro assumes that
35bef 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20   local variable 
35bf0 70 52 74 72 65 65 20 70 6f 69 6e 74 73 0a 2a 2a  pRtree points.**
35bf1 20 74 6f 20 74 68 65 20 52 74 72 65 65 20 73 74   to the Rtree st
35bf2 72 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74  ructure associat
35bf3 65 64 20 77 69 74 68 20 74 68 65 20 52 74 72 65  ed with the Rtre
35bf4 65 43 6f 6f 72 64 2e 0a 2a 2f 0a 23 64 65 66 69  eCoord..*/.#defi
35bf5 6e 65 20 44 43 4f 4f 52 44 28 63 6f 6f 72 64 29  ne DCOORD(coord)
35bf6 20 28 20 20 20 20 20 20 20 20 20 20 20 20 20 20   (              
35bf7 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
35bf8 20 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64   (pRtree->eCoord
35bf9 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52  Type==RTREE_COOR
35bfa 44 5f 52 45 41 4c 33 32 29 20 3f 20 20 20 20 20  D_REAL32) ?     
35bfb 20 5c 0a 20 20 20 20 28 28 64 6f 75 62 6c 65 29   \.    ((double)
35bfc 63 6f 6f 72 64 2e 66 29 20 3a 20 20 20 20 20 20  coord.f) :      
35bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bfe 20 20 20 20 20 5c 0a 20 20 20 20 28 28 64 6f 75       \.    ((dou
35bff 62 6c 65 29 63 6f 6f 72 64 2e 69 29 20 20 20 20  ble)coord.i)    
35c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c01 20 20 20 20 20 20 20 20 20 5c 0a 29 0a 0a 2f 2a           \.)../*
35c02 0a 2a 2a 20 41 20 73 65 61 72 63 68 20 63 6f 6e  .** A search con
35c03 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 72 75  straint..*/.stru
35c04 63 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  ct RtreeConstrai
35c05 6e 74 20 7b 0a 20 20 69 6e 74 20 69 43 6f 6f 72  nt {.  int iCoor
35c06 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
35c07 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
35c08 78 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 65 64  x of constrained
35c09 20 63 6f 6f 72 64 69 6e 61 74 65 20 2a 2f 0a 20   coordinate */. 
35c0a 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20   int op;        
35c0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c0c 20 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 69     /* Constraini
35c0d 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  ng operation */.
35c0e 20 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 3b    double rValue;
35c0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c10 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e      /* Constrain
35c11 74 20 76 61 6c 75 65 2e 20 2a 2f 0a 7d 3b 0a 0a  t value. */.};..
35c12 2f 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75  /* Possible valu
35c13 65 73 20 66 6f 72 20 52 74 72 65 65 43 6f 6e 73  es for RtreeCons
35c14 74 72 61 69 6e 74 2e 6f 70 20 2a 2f 0a 23 64 65  traint.op */.#de
35c15 66 69 6e 65 20 52 54 52 45 45 5f 45 51 20 30 78  fine RTREE_EQ 0x
35c16 34 31 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  41.#define RTREE
35c17 5f 4c 45 20 30 78 34 32 0a 23 64 65 66 69 6e 65  _LE 0x42.#define
35c18 20 52 54 52 45 45 5f 4c 54 20 30 78 34 33 0a 23   RTREE_LT 0x43.#
35c19 64 65 66 69 6e 65 20 52 54 52 45 45 5f 47 45 20  define RTREE_GE 
35c1a 30 78 34 34 0a 23 64 65 66 69 6e 65 20 52 54 52  0x44.#define RTR
35c1b 45 45 5f 47 54 20 30 78 34 35 0a 0a 2f 2a 20 0a  EE_GT 0x45../* .
35c1c 2a 2a 20 41 6e 20 72 74 72 65 65 20 73 74 72 75  ** An rtree stru
35c1d 63 74 75 72 65 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a  cture node..**.*
35c1e 2a 20 44 61 74 61 20 66 6f 72 6d 61 74 20 28 52  * Data format (R
35c1f 74 72 65 65 4e 6f 64 65 2e 7a 44 61 74 61 29 3a  treeNode.zData):
35c20 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 49 66 20 74  .**.**   1. If t
35c21 68 65 20 6e 6f 64 65 20 69 73 20 74 68 65 20 72  he node is the r
35c22 6f 6f 74 20 6e 6f 64 65 20 28 6e 6f 64 65 20 31  oot node (node 1
35c23 29 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73  ), then the firs
35c24 74 20 32 20 62 79 74 65 73 0a 2a 2a 20 20 20 20  t 2 bytes.**    
35c25 20 20 6f 66 20 74 68 65 20 6e 6f 64 65 20 63 6f    of the node co
35c26 6e 74 61 69 6e 20 74 68 65 20 74 72 65 65 20 64  ntain the tree d
35c27 65 70 74 68 20 61 73 20 61 20 62 69 67 2d 65 6e  epth as a big-en
35c28 64 69 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a  dian integer..**
35c29 20 20 20 20 20 20 46 6f 72 20 6e 6f 6e 2d 72 6f        For non-ro
35c2a 6f 74 20 6e 6f 64 65 73 2c 20 74 68 65 20 66 69  ot nodes, the fi
35c2b 72 73 74 20 32 20 62 79 74 65 73 20 61 72 65 20  rst 2 bytes are 
35c2c 6c 65 66 74 20 75 6e 75 73 65 64 2e 0a 2a 2a 0a  left unused..**.
35c2d 2a 2a 20 20 20 32 2e 20 54 68 65 20 6e 65 78 74  **   2. The next
35c2e 20 32 20 62 79 74 65 73 20 63 6f 6e 74 61 69 6e   2 bytes contain
35c2f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
35c30 6e 74 72 69 65 73 20 63 75 72 72 65 6e 74 6c 79  ntries currently
35c31 20 0a 2a 2a 20 20 20 20 20 20 73 74 6f 72 65 64   .**      stored
35c32 20 69 6e 20 74 68 65 20 6e 6f 64 65 2e 0a 2a 2a   in the node..**
35c33 0a 2a 2a 20 20 20 33 2e 20 54 68 65 20 72 65 6d  .**   3. The rem
35c34 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 6e 6f  ainder of the no
35c35 64 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  de contains the 
35c36 6e 6f 64 65 20 65 6e 74 72 69 65 73 2e 20 45 61  node entries. Ea
35c37 63 68 20 65 6e 74 72 79 0a 2a 2a 20 20 20 20 20  ch entry.**     
35c38 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73   consists of a s
35c39 69 6e 67 6c 65 20 38 2d 62 79 74 65 20 69 6e 74  ingle 8-byte int
35c3a 65 67 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79  eger followed by
35c3b 20 61 6e 20 65 76 65 6e 20 6e 75 6d 62 65 72 0a   an even number.
35c3c 2a 2a 20 20 20 20 20 20 6f 66 20 34 2d 62 79 74  **      of 4-byt
35c3d 65 20 63 6f 6f 72 64 69 6e 61 74 65 73 2e 20 46  e coordinates. F
35c3e 6f 72 20 6c 65 61 66 20 6e 6f 64 65 73 20 74 68  or leaf nodes th
35c3f 65 20 69 6e 74 65 67 65 72 20 69 73 20 74 68 65  e integer is the
35c40 20 72 6f 77 69 64 0a 2a 2a 20 20 20 20 20 20 6f   rowid.**      o
35c41 66 20 61 20 72 65 63 6f 72 64 2e 20 46 6f 72 20  f a record. For 
35c42 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 73 20 69  internal nodes i
35c43 74 20 69 73 20 74 68 65 20 6e 6f 64 65 20 6e 75  t is the node nu
35c44 6d 62 65 72 20 6f 66 20 61 0a 2a 2a 20 20 20 20  mber of a.**    
35c45 20 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 2a 2f    child page..*/
35c46 0a 73 74 72 75 63 74 20 52 74 72 65 65 4e 6f 64  .struct RtreeNod
35c47 65 20 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  e {.  RtreeNode 
35c48 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20  *pParent;       
35c49 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e          /* Paren
35c4a 74 20 6e 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20  t node */.  i64 
35c4b 69 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 6e 52 65  iNode;.  int nRe
35c4c 66 3b 0a 20 20 69 6e 74 20 69 73 44 69 72 74 79  f;.  int isDirty
35c4d 3b 0a 20 20 75 38 20 2a 7a 44 61 74 61 3b 0a 20  ;.  u8 *zData;. 
35c4e 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 65 78   RtreeNode *pNex
35c4f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
35c50 20 20 20 2f 2a 20 4e 65 78 74 20 6e 6f 64 65 20     /* Next node 
35c51 69 6e 20 74 68 69 73 20 68 61 73 68 20 63 68 61  in this hash cha
35c52 69 6e 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65  in */.};.#define
35c53 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20 72 65   NCELL(pNode) re
35c54 61 64 49 6e 74 31 36 28 26 28 70 4e 6f 64 65 29  adInt16(&(pNode)
35c55 2d 3e 7a 44 61 74 61 5b 32 5d 29 0a 0a 2f 2a 20  ->zData[2])../* 
35c56 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 74 6f  .** Structure to
35c57 20 73 74 6f 72 65 20 61 20 64 65 73 65 72 69 61   store a deseria
35c58 6c 69 7a 65 64 20 72 74 72 65 65 20 72 65 63 6f  lized rtree reco
35c59 72 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74  rd..*/.struct Rt
35c5a 72 65 65 43 65 6c 6c 20 7b 0a 20 20 69 36 34 20  reeCell {.  i64 
35c5b 69 52 6f 77 69 64 3b 0a 20 20 52 74 72 65 65 43  iRowid;.  RtreeC
35c5c 6f 6f 72 64 20 61 43 6f 6f 72 64 5b 52 54 52 45  oord aCoord[RTRE
35c5d 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53  E_MAX_DIMENSIONS
35c5e 2a 32 5d 3b 0a 7d 3b 0a 0a 23 69 66 6e 64 65 66  *2];.};..#ifndef
35c5f 20 4d 41 58 0a 23 20 64 65 66 69 6e 65 20 4d 41   MAX.# define MA
35c60 58 28 78 2c 79 29 20 28 28 78 29 20 3c 20 28 79  X(x,y) ((x) < (y
35c61 29 20 3f 20 28 79 29 20 3a 20 28 78 29 29 0a 23  ) ? (y) : (x)).#
35c62 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4d 49  endif.#ifndef MI
35c63 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78  N.# define MIN(x
35c64 2c 79 29 20 28 28 78 29 20 3e 20 28 79 29 20 3f  ,y) ((x) > (y) ?
35c65 20 28 79 29 20 3a 20 28 78 29 29 0a 23 65 6e 64   (y) : (x)).#end
35c66 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  if../*.** Functi
35c67 6f 6e 73 20 74 6f 20 64 65 73 65 72 69 61 6c 69  ons to deseriali
35c68 7a 65 20 61 20 31 36 20 62 69 74 20 69 6e 74 65  ze a 16 bit inte
35c69 67 65 72 2c 20 33 32 20 62 69 74 20 72 65 61 6c  ger, 32 bit real
35c6a 20 6e 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20 36   number and.** 6
35c6b 34 20 62 69 74 20 69 6e 74 65 67 65 72 2e 20 54  4 bit integer. T
35c6c 68 65 20 64 65 73 65 72 69 61 6c 69 7a 65 64 20  he deserialized 
35c6d 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65  value is returne
35c6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
35c6f 20 72 65 61 64 49 6e 74 31 36 28 75 38 20 2a 70   readInt16(u8 *p
35c70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30  ){.  return (p[0
35c71 5d 3c 3c 38 29 20 2b 20 70 5b 31 5d 3b 0a 7d 0a  ]<<8) + p[1];.}.
35c72 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 61 64  static void read
35c73 43 6f 6f 72 64 28 75 38 20 2a 70 2c 20 52 74 72  Coord(u8 *p, Rtr
35c74 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 29  eeCoord *pCoord)
35c75 7b 0a 20 20 75 33 32 20 69 20 3d 20 28 0a 20 20  {.  u32 i = (.  
35c76 20 20 28 28 28 75 33 32 29 70 5b 30 5d 29 20 3c    (((u32)p[0]) <
35c77 3c 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28  < 24) + .    (((
35c78 75 33 32 29 70 5b 31 5d 29 20 3c 3c 20 31 36 29  u32)p[1]) << 16)
35c79 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
35c7a 5b 32 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20  [2]) <<  8) + . 
35c7b 20 20 20 28 28 28 75 33 32 29 70 5b 33 5d 29 20     (((u32)p[3]) 
35c7c 3c 3c 20 20 30 29 0a 20 20 29 3b 0a 20 20 2a 28  <<  0).  );.  *(
35c7d 75 33 32 20 2a 29 70 43 6f 6f 72 64 20 3d 20 69  u32 *)pCoord = i
35c7e 3b 0a 7d 0a 73 74 61 74 69 63 20 69 36 34 20 72  ;.}.static i64 r
35c7f 65 61 64 49 6e 74 36 34 28 75 38 20 2a 70 29 7b  eadInt64(u8 *p){
35c80 0a 20 20 72 65 74 75 72 6e 20 28 0a 20 20 20 20  .  return (.    
35c81 28 28 28 69 36 34 29 70 5b 30 5d 29 20 3c 3c 20  (((i64)p[0]) << 
35c82 35 36 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  56) + .    (((i6
35c83 34 29 70 5b 31 5d 29 20 3c 3c 20 34 38 29 20 2b  4)p[1]) << 48) +
35c84 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 32   .    (((i64)p[2
35c85 5d 29 20 3c 3c 20 34 30 29 20 2b 20 0a 20 20 20  ]) << 40) + .   
35c86 20 28 28 28 69 36 34 29 70 5b 33 5d 29 20 3c 3c   (((i64)p[3]) <<
35c87 20 33 32 29 20 2b 20 0a 20 20 20 20 28 28 28 69   32) + .    (((i
35c88 36 34 29 70 5b 34 5d 29 20 3c 3c 20 32 34 29 20  64)p[4]) << 24) 
35c89 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
35c8a 35 5d 29 20 3c 3c 20 31 36 29 20 2b 20 0a 20 20  5]) << 16) + .  
35c8b 20 20 28 28 28 69 36 34 29 70 5b 36 5d 29 20 3c    (((i64)p[6]) <
35c8c 3c 20 20 38 29 20 2b 20 0a 20 20 20 20 28 28 28  <  8) + .    (((
35c8d 69 36 34 29 70 5b 37 5d 29 20 3c 3c 20 20 30 29  i64)p[7]) <<  0)
35c8e 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  .  );.}../*.** F
35c8f 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 65 72 69  unctions to seri
35c90 61 6c 69 7a 65 20 61 20 31 36 20 62 69 74 20 69  alize a 16 bit i
35c91 6e 74 65 67 65 72 2c 20 33 32 20 62 69 74 20 72  nteger, 32 bit r
35c92 65 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64 0a 2a  eal number and.*
35c93 2a 20 36 34 20 62 69 74 20 69 6e 74 65 67 65 72  * 64 bit integer
35c94 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  . The value retu
35c95 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62  rned is the numb
35c96 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74  er of bytes writ
35c97 74 65 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 61 72  ten.** to the ar
35c98 67 75 6d 65 6e 74 20 62 75 66 66 65 72 20 28 61  gument buffer (a
35c99 6c 77 61 79 73 20 32 2c 20 34 20 61 6e 64 20 38  lways 2, 4 and 8
35c9a 20 72 65 73 70 65 63 74 69 76 65 6c 79 29 2e 0a   respectively)..
35c9b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  */.static int wr
35c9c 69 74 65 49 6e 74 31 36 28 75 38 20 2a 70 2c 20  iteInt16(u8 *p, 
35c9d 69 6e 74 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d  int i){.  p[0] =
35c9e 20 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20   (i>> 8)&0xFF;. 
35c9f 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 20 30 29 26   p[1] = (i>> 0)&
35ca0 30 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 32  0xFF;.  return 2
35ca1 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  ;.}.static int w
35ca2 72 69 74 65 43 6f 6f 72 64 28 75 38 20 2a 70 2c  riteCoord(u8 *p,
35ca3 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43 6f   RtreeCoord *pCo
35ca4 6f 72 64 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20  ord){.  u32 i;. 
35ca5 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
35ca6 52 74 72 65 65 43 6f 6f 72 64 29 3d 3d 34 20 29  RtreeCoord)==4 )
35ca7 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
35ca8 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20  of(u32)==4 );.  
35ca9 69 20 3d 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f  i = *(u32 *)pCoo
35caa 72 64 3b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e  rd;.  p[0] = (i>
35cab 3e 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 31  >24)&0xFF;.  p[1
35cac 5d 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46  ] = (i>>16)&0xFF
35cad 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 20  ;.  p[2] = (i>> 
35cae 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20  8)&0xFF;.  p[3] 
35caf 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a  = (i>> 0)&0xFF;.
35cb0 20 20 72 65 74 75 72 6e 20 34 3b 0a 7d 0a 73 74    return 4;.}.st
35cb1 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 49 6e  atic int writeIn
35cb2 74 36 34 28 75 38 20 2a 70 2c 20 69 36 34 20 69  t64(u8 *p, i64 i
35cb3 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e  ){.  p[0] = (i>>
35cb4 35 36 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d  56)&0xFF;.  p[1]
35cb5 20 3d 20 28 69 3e 3e 34 38 29 26 30 78 46 46 3b   = (i>>48)&0xFF;
35cb6 0a 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 34 30  .  p[2] = (i>>40
35cb7 29 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20 3d  )&0xFF;.  p[3] =
35cb8 20 28 69 3e 3e 33 32 29 26 30 78 46 46 3b 0a 20   (i>>32)&0xFF;. 
35cb9 20 70 5b 34 5d 20 3d 20 28 69 3e 3e 32 34 29 26   p[4] = (i>>24)&
35cba 30 78 46 46 3b 0a 20 20 70 5b 35 5d 20 3d 20 28  0xFF;.  p[5] = (
35cbb 69 3e 3e 31 36 29 26 30 78 46 46 3b 0a 20 20 70  i>>16)&0xFF;.  p
35cbc 5b 36 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [6] = (i>> 8)&0x
35cbd 46 46 3b 0a 20 20 70 5b 37 5d 20 3d 20 28 69 3e  FF;.  p[7] = (i>
35cbe 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
35cbf 75 72 6e 20 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 8;.}../*.** 
35cc0 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65  Increment the re
35cc1 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66  ference count of
35cc2 20 6e 6f 64 65 20 70 2e 0a 2a 2f 0a 73 74 61 74   node p..*/.stat
35cc3 69 63 20 76 6f 69 64 20 6e 6f 64 65 52 65 66 65  ic void nodeRefe
35cc4 72 65 6e 63 65 28 52 74 72 65 65 4e 6f 64 65 20  rence(RtreeNode 
35cc5 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  *p){.  if( p ){.
35cc6 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20      p->nRef++;. 
35cc7 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61   }.}../*.** Clea
35cc8 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  r the content of
35cc9 20 6e 6f 64 65 20 70 20 28 73 65 74 20 61 6c 6c   node p (set all
35cca 20 62 79 74 65 73 20 74 6f 20 30 78 30 30 29 2e   bytes to 0x00).
35ccb 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
35ccc 6e 6f 64 65 5a 65 72 6f 28 52 74 72 65 65 20 2a  nodeZero(Rtree *
35ccd 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
35cce 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  e *p){.  if( p )
35ccf 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 2d  {.    memset(&p-
35cd0 3e 7a 44 61 74 61 5b 32 5d 2c 20 30 2c 20 70 52  >zData[2], 0, pR
35cd1 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d  tree->iNodeSize-
35cd2 32 29 3b 0a 20 20 20 20 70 2d 3e 69 73 44 69 72  2);.    p->isDir
35cd3 74 79 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ty = 1;.  }.}../
35cd4 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 6e 6f 64  *.** Given a nod
35cd5 65 20 6e 75 6d 62 65 72 20 69 4e 6f 64 65 2c 20  e number iNode, 
35cd6 72 65 74 75 72 6e 20 74 68 65 20 63 6f 72 72 65  return the corre
35cd7 73 70 6f 6e 64 69 6e 67 20 6b 65 79 20 74 6f 20  sponding key to 
35cd8 75 73 65 0a 2a 2a 20 69 6e 20 74 68 65 20 52 74  use.** in the Rt
35cd9 72 65 65 2e 61 48 61 73 68 20 74 61 62 6c 65 2e  ree.aHash table.
35cda 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
35cdb 6f 64 65 48 61 73 68 28 69 36 34 20 69 4e 6f 64  odeHash(i64 iNod
35cdc 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 0a 20  e){.  return (. 
35cdd 20 20 20 28 69 4e 6f 64 65 3e 3e 35 36 29 20 5e     (iNode>>56) ^
35cde 20 28 69 4e 6f 64 65 3e 3e 34 38 29 20 5e 20 28   (iNode>>48) ^ (
35cdf 69 4e 6f 64 65 3e 3e 34 30 29 20 5e 20 28 69 4e  iNode>>40) ^ (iN
35ce0 6f 64 65 3e 3e 33 32 29 20 5e 20 0a 20 20 20 20  ode>>32) ^ .    
35ce1 28 69 4e 6f 64 65 3e 3e 32 34 29 20 5e 20 28 69  (iNode>>24) ^ (i
35ce2 4e 6f 64 65 3e 3e 31 36 29 20 5e 20 28 69 4e 6f  Node>>16) ^ (iNo
35ce3 64 65 3e 3e 20 38 29 20 5e 20 28 69 4e 6f 64 65  de>> 8) ^ (iNode
35ce4 3e 3e 20 30 29 0a 20 20 29 20 25 20 48 41 53 48  >> 0).  ) % HASH
35ce5 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  SIZE;.}../*.** S
35ce6 65 61 72 63 68 20 74 68 65 20 6e 6f 64 65 20 68  earch the node h
35ce7 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 6e 6f  ash table for no
35ce8 64 65 20 69 4e 6f 64 65 2e 20 49 66 20 66 6f 75  de iNode. If fou
35ce9 6e 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  nd, return a poi
35cea 6e 74 65 72 0a 2a 2a 20 74 6f 20 69 74 2e 20 4f  nter.** to it. O
35ceb 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e  therwise, return
35cec 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74   0..*/.static Rt
35ced 72 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 48 61 73  reeNode *nodeHas
35cee 68 4c 6f 6f 6b 75 70 28 52 74 72 65 65 20 2a 70  hLookup(Rtree *p
35cef 52 74 72 65 65 2c 20 69 36 34 20 69 4e 6f 64 65  Rtree, i64 iNode
35cf0 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ){.  RtreeNode *
35cf1 70 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4e 6f  p;.  assert( iNo
35cf2 64 65 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70  de!=0 );.  for(p
35cf3 3d 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e  =pRtree->aHash[n
35cf4 6f 64 65 48 61 73 68 28 69 4e 6f 64 65 29 5d 3b  odeHash(iNode)];
35cf5 20 70 20 26 26 20 70 2d 3e 69 4e 6f 64 65 21 3d   p && p->iNode!=
35cf6 69 4e 6f 64 65 3b 20 70 3d 70 2d 3e 70 4e 65 78  iNode; p=p->pNex
35cf7 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  t);.  return p;.
35cf8 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 6e 6f 64  }../*.** Add nod
35cf9 65 20 70 4e 6f 64 65 20 74 6f 20 74 68 65 20 6e  e pNode to the n
35cfa 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ode hash table..
35cfb 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
35cfc 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28 52 74  odeHashInsert(Rt
35cfd 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
35cfe 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
35cff 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
35d00 20 20 20 69 6e 74 20 69 48 61 73 68 3b 0a 20 20     int iHash;.  
35d01 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d    assert( pNode-
35d02 3e 70 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 20  >pNext==0 );.   
35d03 20 69 48 61 73 68 20 3d 20 6e 6f 64 65 48 61 73   iHash = nodeHas
35d04 68 28 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  h(pNode->iNode);
35d05 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78  .    pNode->pNex
35d06 74 20 3d 20 70 52 74 72 65 65 2d 3e 61 48 61 73  t = pRtree->aHas
35d07 68 5b 69 48 61 73 68 5d 3b 0a 20 20 20 20 70 52  h[iHash];.    pR
35d08 74 72 65 65 2d 3e 61 48 61 73 68 5b 69 48 61 73  tree->aHash[iHas
35d09 68 5d 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 7d 0a  h] = pNode;.  }.
35d0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
35d0b 6e 6f 64 65 20 70 4e 6f 64 65 20 66 72 6f 6d 20  node pNode from 
35d0c 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61  the node hash ta
35d0d 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
35d0e 6f 69 64 20 6e 6f 64 65 48 61 73 68 44 65 6c 65  oid nodeHashDele
35d0f 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  te(Rtree *pRtree
35d10 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
35d11 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65  de){.  RtreeNode
35d12 20 2a 2a 70 70 3b 0a 20 20 69 66 28 20 70 4e 6f   **pp;.  if( pNo
35d13 64 65 2d 3e 69 4e 6f 64 65 21 3d 30 20 29 7b 0a  de->iNode!=0 ){.
35d14 20 20 20 20 70 70 20 3d 20 26 70 52 74 72 65 65      pp = &pRtree
35d15 2d 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61 73 68  ->aHash[nodeHash
35d16 28 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 5d 3b  (pNode->iNode)];
35d17 0a 20 20 20 20 66 6f 72 28 20 3b 20 28 2a 70 70  .    for( ; (*pp
35d18 29 21 3d 70 4e 6f 64 65 3b 20 70 70 20 3d 20 26  )!=pNode; pp = &
35d19 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 7b 20 61  (*pp)->pNext){ a
35d1a 73 73 65 72 74 28 2a 70 70 29 3b 20 7d 0a 20 20  ssert(*pp); }.  
35d1b 20 20 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e 70    *pp = pNode->p
35d1c 4e 65 78 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  Next;.    pNode-
35d1d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 7d 0a  >pNext = 0;.  }.
35d1e 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
35d1f 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 65 77  e and return new
35d20 20 72 2d 74 72 65 65 20 6e 6f 64 65 2e 20 49 6e   r-tree node. In
35d21 69 74 69 61 6c 6c 79 2c 20 28 52 74 72 65 65 4e  itially, (RtreeN
35d22 6f 64 65 2e 69 4e 6f 64 65 3d 3d 30 29 2c 0a 2a  ode.iNode==0),.*
35d23 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61  * indicating tha
35d24 74 20 6e 6f 64 65 20 68 61 73 20 6e 6f 74 20 79  t node has not y
35d25 65 74 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64  et been assigned
35d26 20 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20   a node number. 
35d27 49 74 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65  It is.** assigne
35d28 64 20 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20  d a node number 
35d29 77 68 65 6e 20 6e 6f 64 65 57 72 69 74 65 28 29  when nodeWrite()
35d2a 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 77 72   is called to wr
35d2b 69 74 65 20 74 68 65 0a 2a 2a 20 6e 6f 64 65 20  ite the.** node 
35d2c 63 6f 6e 74 65 6e 74 73 20 6f 75 74 20 74 6f 20  contents out to 
35d2d 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  the database..*/
35d2e 0a 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f 64  .static RtreeNod
35d2f 65 20 2a 6e 6f 64 65 4e 65 77 28 52 74 72 65 65  e *nodeNew(Rtree
35d30 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
35d31 6f 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e  ode *pParent, in
35d32 74 20 7a 65 72 6f 29 7b 0a 20 20 52 74 72 65 65  t zero){.  Rtree
35d33 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20 70  Node *pNode;.  p
35d34 4e 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f 64  Node = (RtreeNod
35d35 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  e *)sqlite3_mall
35d36 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 4e  oc(sizeof(RtreeN
35d37 6f 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e 69  ode) + pRtree->i
35d38 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66 28  NodeSize);.  if(
35d39 20 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 6d 65   pNode ){.    me
35d3a 6d 73 65 74 28 70 4e 6f 64 65 2c 20 30 2c 20 73  mset(pNode, 0, s
35d3b 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29  izeof(RtreeNode)
35d3c 20 2b 20 28 7a 65 72 6f 3f 70 52 74 72 65 65 2d   + (zero?pRtree-
35d3d 3e 69 4e 6f 64 65 53 69 7a 65 3a 30 29 29 3b 0a  >iNodeSize:0));.
35d3e 20 20 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61      pNode->zData
35d3f 20 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b   = (u8 *)&pNode[
35d40 31 5d 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e  1];.    pNode->n
35d41 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4e 6f  Ref = 1;.    pNo
35d42 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50  de->pParent = pP
35d43 61 72 65 6e 74 3b 0a 20 20 20 20 70 4e 6f 64 65  arent;.    pNode
35d44 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20  ->isDirty = 1;. 
35d45 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65     nodeReference
35d46 28 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 20  (pParent);.  }. 
35d47 20 72 65 74 75 72 6e 20 70 4e 6f 64 65 3b 0a 7d   return pNode;.}
35d48 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61  ../*.** Obtain a
35d49 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e   reference to an
35d4a 20 72 2d 74 72 65 65 20 6e 6f 64 65 2e 0a 2a 2f   r-tree node..*/
35d4b 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
35d4c 41 63 71 75 69 72 65 28 0a 20 20 52 74 72 65 65  Acquire(.  Rtree
35d4d 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20   *pRtree,       
35d4e 20 20 20 20 20 20 2f 2a 20 52 2d 74 72 65 65 20        /* R-tree 
35d4f 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69  structure */.  i
35d50 36 34 20 69 4e 6f 64 65 2c 20 20 20 20 20 20 20  64 iNode,       
35d51 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64            /* Nod
35d52 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 61 64  e number to load
35d53 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20   */.  RtreeNode 
35d54 2a 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20  *pParent,       
35d55 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20 70   /* Either the p
35d56 61 72 65 6e 74 20 6e 6f 64 65 20 6f 72 20 4e 55  arent node or NU
35d57 4c 4c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  LL */.  RtreeNod
35d58 65 20 2a 2a 70 70 4e 6f 64 65 20 20 20 20 20 20  e **ppNode      
35d59 20 20 20 2f 2a 20 4f 55 54 3a 20 41 63 71 75 69     /* OUT: Acqui
35d5a 72 65 64 20 6e 6f 64 65 20 2a 2f 0a 29 7b 0a 20  red node */.){. 
35d5b 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65   int rc;.  Rtree
35d5c 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 0a 20 20  Node *pNode;..  
35d5d 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20  /* Check if the 
35d5e 72 65 71 75 65 73 74 65 64 20 6e 6f 64 65 20 69  requested node i
35d5f 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
35d60 20 68 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20   hash table. If 
35d61 73 6f 2c 0a 20 20 2a 2a 20 69 6e 63 72 65 61 73  so,.  ** increas
35d62 65 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20  e its reference 
35d63 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e  count and return
35d64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
35d65 28 70 4e 6f 64 65 20 3d 20 6e 6f 64 65 48 61 73  (pNode = nodeHas
35d66 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20  hLookup(pRtree, 
35d67 69 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 61  iNode)) ){.    a
35d68 73 73 65 72 74 28 20 21 70 50 61 72 65 6e 74 20  ssert( !pParent 
35d69 7c 7c 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72 65  || !pNode->pPare
35d6a 6e 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 70 50 61  nt || pNode->pPa
35d6b 72 65 6e 74 3d 3d 70 50 61 72 65 6e 74 20 29 3b  rent==pParent );
35d6c 0a 20 20 20 20 69 66 28 20 70 50 61 72 65 6e 74  .    if( pParent
35d6d 20 26 26 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72   && !pNode->pPar
35d6e 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  ent ){.      nod
35d6f 65 52 65 66 65 72 65 6e 63 65 28 70 50 61 72 65  eReference(pPare
35d70 6e 74 29 3b 0a 20 20 20 20 20 20 70 4e 6f 64 65  nt);.      pNode
35d71 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72  ->pParent = pPar
35d72 65 6e 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ent;.    }.    p
35d73 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  Node->nRef++;.  
35d74 20 20 2a 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64    *ppNode = pNod
35d75 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e;.    return SQ
35d76 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
35d77 70 4e 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f  pNode = (RtreeNo
35d78 64 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  de *)sqlite3_mal
35d79 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
35d7a 4e 6f 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e  Node) + pRtree->
35d7b 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66  iNodeSize);.  if
35d7c 28 20 21 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20  ( !pNode ){.    
35d7d 2a 70 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20  *ppNode = 0;.   
35d7e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
35d7f 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64  OMEM;.  }.  pNod
35d80 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61  e->pParent = pPa
35d81 72 65 6e 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 7a  rent;.  pNode->z
35d82 44 61 74 61 20 3d 20 28 75 38 20 2a 29 26 70 4e  Data = (u8 *)&pN
35d83 6f 64 65 5b 31 5d 3b 0a 20 20 70 4e 6f 64 65 2d  ode[1];.  pNode-
35d84 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 6f  >nRef = 1;.  pNo
35d85 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 69 4e 6f 64  de->iNode = iNod
35d86 65 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69  e;.  pNode->isDi
35d87 72 74 79 20 3d 20 30 3b 0a 20 20 70 4e 6f 64 65  rty = 0;.  pNode
35d88 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20  ->pNext = 0;..  
35d89 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
35d8a 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  64(pRtree->pRead
35d8b 4e 6f 64 65 2c 20 31 2c 20 69 4e 6f 64 65 29 3b  Node, 1, iNode);
35d8c 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
35d8d 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65  step(pRtree->pRe
35d8e 61 64 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20 72  adNode);.  if( r
35d8f 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
35d90 0a 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  .    const u8 *z
35d91 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 63  Blob = sqlite3_c
35d92 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52 74 72 65  olumn_blob(pRtre
35d93 65 2d 3e 70 52 65 61 64 4e 6f 64 65 2c 20 30 29  e->pReadNode, 0)
35d94 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 6f  ;.    memcpy(pNo
35d95 64 65 2d 3e 7a 44 61 74 61 2c 20 7a 42 6c 6f 62  de->zData, zBlob
35d96 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53  , pRtree->iNodeS
35d97 69 7a 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ize);.    nodeRe
35d98 66 65 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29  ference(pParent)
35d99 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
35d9a 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64  qlite3_free(pNod
35d9b 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 20 3d 20  e);.    pNode = 
35d9c 30 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4e 6f 64  0;.  }..  *ppNod
35d9d 65 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 63 20  e = pNode;.  rc 
35d9e 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
35d9f 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
35da0 65 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  e);..  if( rc==S
35da1 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4e 6f 64  QLITE_OK && iNod
35da2 65 3d 3d 31 20 29 7b 0a 20 20 20 20 70 52 74 72  e==1 ){.    pRtr
35da3 65 65 2d 3e 69 44 65 70 74 68 20 3d 20 72 65 61  ee->iDepth = rea
35da4 64 49 6e 74 31 36 28 70 4e 6f 64 65 2d 3e 7a 44  dInt16(pNode->zD
35da5 61 74 61 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  ata);.  }..  ass
35da6 65 72 74 28 20 28 72 63 3d 3d 53 51 4c 49 54 45  ert( (rc==SQLITE
35da7 5f 4f 4b 20 26 26 20 70 4e 6f 64 65 29 20 7c 7c  _OK && pNode) ||
35da8 20 28 70 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63   (pNode==0 && rc
35da9 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 3b 0a  !=SQLITE_OK) );.
35daa 20 20 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74    nodeHashInsert
35dab 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
35dac 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
35dad 0a 0a 2f 2a 0a 2a 2a 20 4f 76 65 72 77 72 69 74  ../*.** Overwrit
35dae 65 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20  e cell iCell of 
35daf 6e 6f 64 65 20 70 4e 6f 64 65 20 77 69 74 68 20  node pNode with 
35db0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
35db1 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  pCell..*/.static
35db2 20 76 6f 69 64 20 6e 6f 64 65 4f 76 65 72 77 72   void nodeOverwr
35db3 69 74 65 43 65 6c 6c 28 0a 20 20 52 74 72 65 65  iteCell(.  Rtree
35db4 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72   *pRtree, .  Rtr
35db5 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20  eeNode *pNode,  
35db6 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
35db7 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c  ell, .  int iCel
35db8 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  l.){.  int ii;. 
35db9 20 75 38 20 2a 70 20 3d 20 26 70 4e 6f 64 65 2d   u8 *p = &pNode-
35dba 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65  >zData[4 + pRtre
35dbb 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
35dbc 2a 69 43 65 6c 6c 5d 3b 0a 20 20 70 20 2b 3d 20  *iCell];.  p += 
35dbd 77 72 69 74 65 49 6e 74 36 34 28 70 2c 20 70 43  writeInt64(p, pC
35dbe 65 6c 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20  ell->iRowid);.  
35dbf 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52  for(ii=0; ii<(pR
35dc0 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69  tree->nDim*2); i
35dc1 69 2b 2b 29 7b 0a 20 20 20 20 70 20 2b 3d 20 77  i++){.    p += w
35dc2 72 69 74 65 43 6f 6f 72 64 28 70 2c 20 26 70 43  riteCoord(p, &pC
35dc3 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29  ell->aCoord[ii])
35dc4 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d 3e 69  ;.  }.  pNode->i
35dc5 73 44 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f  sDirty = 1;.}../
35dc6 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 63 65 6c 6c  *.** Remove cell
35dc7 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69   the cell with i
35dc8 6e 64 65 78 20 69 43 65 6c 6c 20 66 72 6f 6d 20  ndex iCell from 
35dc9 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73  node pNode..*/.s
35dca 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 44  tatic void nodeD
35dcb 65 6c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20  eleteCell(Rtree 
35dcc 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
35dcd 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69  de *pNode, int i
35dce 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 44 73  Cell){.  u8 *pDs
35dcf 74 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  t = &pNode->zDat
35dd0 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42  a[4 + pRtree->nB
35dd1 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c  ytesPerCell*iCel
35dd2 6c 5d 3b 0a 20 20 75 38 20 2a 70 53 72 63 20 3d  l];.  u8 *pSrc =
35dd3 20 26 70 44 73 74 5b 70 52 74 72 65 65 2d 3e 6e   &pDst[pRtree->n
35dd4 42 79 74 65 73 50 65 72 43 65 6c 6c 5d 3b 0a 20  BytesPerCell];. 
35dd5 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 4e 43   int nByte = (NC
35dd6 45 4c 4c 28 70 4e 6f 64 65 29 20 2d 20 69 43 65  ELL(pNode) - iCe
35dd7 6c 6c 20 2d 20 31 29 20 2a 20 70 52 74 72 65 65  ll - 1) * pRtree
35dd8 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b  ->nBytesPerCell;
35dd9 0a 20 20 6d 65 6d 6d 6f 76 65 28 70 44 73 74 2c  .  memmove(pDst,
35dda 20 70 53 72 63 2c 20 6e 42 79 74 65 29 3b 0a 20   pSrc, nByte);. 
35ddb 20 77 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f   writeInt16(&pNo
35ddc 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 4e 43  de->zData[2], NC
35ddd 45 4c 4c 28 70 4e 6f 64 65 29 2d 31 29 3b 0a 20  ELL(pNode)-1);. 
35dde 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20   pNode->isDirty 
35ddf 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  = 1;.}../*.** In
35de0 73 65 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  sert the content
35de1 73 20 6f 66 20 63 65 6c 6c 20 70 43 65 6c 6c 20  s of cell pCell 
35de2 69 6e 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  into node pNode.
35de3 20 49 66 20 74 68 65 20 69 6e 73 65 72 74 0a 2a   If the insert.*
35de4 2a 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  * is successful,
35de5 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
35de6 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  K..**.** If ther
35de7 65 20 69 73 20 6e 6f 74 20 65 6e 6f 75 67 68 20  e is not enough 
35de8 66 72 65 65 20 73 70 61 63 65 20 69 6e 20 70 4e  free space in pN
35de9 6f 64 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ode, return SQLI
35dea 54 45 5f 46 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74  TE_FULL..*/.stat
35deb 69 63 20 69 6e 74 0a 6e 6f 64 65 49 6e 73 65 72  ic int.nodeInser
35dec 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a  tCell(.  Rtree *
35ded 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65  pRtree, .  Rtree
35dee 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20  Node *pNode, .  
35def 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c  RtreeCell *pCell
35df0 20 0a 29 7b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c   .){.  int nCell
35df1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35df2 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
35df3 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  number of cells 
35df4 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e  in pNode */.  in
35df5 74 20 6e 4d 61 78 43 65 6c 6c 3b 20 20 20 20 20  t nMaxCell;     
35df6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
35df7 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
35df8 20 63 65 6c 6c 73 20 66 6f 72 20 70 4e 6f 64 65   cells for pNode
35df9 20 2a 2f 0a 0a 20 20 6e 4d 61 78 43 65 6c 6c 20   */..  nMaxCell 
35dfa 3d 20 28 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  = (pRtree->iNode
35dfb 53 69 7a 65 2d 34 29 2f 70 52 74 72 65 65 2d 3e  Size-4)/pRtree->
35dfc 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20  nBytesPerCell;. 
35dfd 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
35dfe 4e 6f 64 65 29 3b 0a 0a 20 20 61 73 73 65 72 74  Node);..  assert
35dff 28 6e 43 65 6c 6c 3c 3d 6e 4d 61 78 43 65 6c 6c  (nCell<=nMaxCell
35e00 29 3b 0a 0a 20 20 69 66 28 20 6e 43 65 6c 6c 3c  );..  if( nCell<
35e01 6e 4d 61 78 43 65 6c 6c 20 29 7b 0a 20 20 20 20  nMaxCell ){.    
35e02 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c  nodeOverwriteCel
35e03 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
35e04 20 70 43 65 6c 6c 2c 20 6e 43 65 6c 6c 29 3b 0a   pCell, nCell);.
35e05 20 20 20 20 77 72 69 74 65 49 6e 74 31 36 28 26      writeInt16(&
35e06 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c  pNode->zData[2],
35e07 20 6e 43 65 6c 6c 2b 31 29 3b 0a 20 20 20 20 70   nCell+1);.    p
35e08 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
35e09 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  1;.  }..  return
35e0a 20 28 6e 43 65 6c 6c 3d 3d 6e 4d 61 78 43 65 6c   (nCell==nMaxCel
35e0b 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  l);.}../*.** If 
35e0c 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69 72 74  the node is dirt
35e0d 79 2c 20 77 72 69 74 65 20 69 74 20 6f 75 74 20  y, write it out 
35e0e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
35e0f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e  .*/.static int.n
35e10 6f 64 65 57 72 69 74 65 28 52 74 72 65 65 20 2a  odeWrite(Rtree *
35e11 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
35e12 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74  e *pNode){.  int
35e13 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
35e14 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 73  .  if( pNode->is
35e15 44 69 72 74 79 20 29 7b 0a 20 20 20 20 73 71 6c  Dirty ){.    sql
35e16 69 74 65 33 5f 73 74 6d 74 20 2a 70 20 3d 20 70  ite3_stmt *p = p
35e17 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64  Rtree->pWriteNod
35e18 65 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65  e;.    if( pNode
35e19 2d 3e 69 4e 6f 64 65 20 29 7b 0a 20 20 20 20 20  ->iNode ){.     
35e1a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
35e1b 74 36 34 28 70 2c 20 31 2c 20 70 4e 6f 64 65 2d  t64(p, 1, pNode-
35e1c 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c  >iNode);.    }el
35e1d 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
35e1e 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 2c 20 31  3_bind_null(p, 1
35e1f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
35e20 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70  ite3_bind_blob(p
35e21 2c 20 32 2c 20 70 4e 6f 64 65 2d 3e 7a 44 61 74  , 2, pNode->zDat
35e22 61 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  a, pRtree->iNode
35e23 53 69 7a 65 2c 20 53 51 4c 49 54 45 5f 53 54 41  Size, SQLITE_STA
35e24 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
35e25 33 5f 73 74 65 70 28 70 29 3b 0a 20 20 20 20 70  3_step(p);.    p
35e26 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
35e27 30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  0;.    rc = sqli
35e28 74 65 33 5f 72 65 73 65 74 28 70 29 3b 0a 20 20  te3_reset(p);.  
35e29 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
35e2a 64 65 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c  de==0 && rc==SQL
35e2b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
35e2c 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 73  pNode->iNode = s
35e2d 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
35e2e 72 74 5f 72 6f 77 69 64 28 70 52 74 72 65 65 2d  rt_rowid(pRtree-
35e2f 3e 64 62 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65  >db);.      node
35e30 48 61 73 68 49 6e 73 65 72 74 28 70 52 74 72 65  HashInsert(pRtre
35e31 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d  e, pNode);.    }
35e32 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
35e33 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
35e34 73 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  se a reference t
35e35 6f 20 61 20 6e 6f 64 65 2e 20 49 66 20 74 68 65  o a node. If the
35e36 20 6e 6f 64 65 20 69 73 20 64 69 72 74 79 20 61   node is dirty a
35e37 6e 64 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  nd the reference
35e38 0a 2a 2a 20 63 6f 75 6e 74 20 64 72 6f 70 73 20  .** count drops 
35e39 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20 6e 6f 64  to zero, the nod
35e3a 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65  e data is writte
35e3b 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  n to the databas
35e3c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
35e3d 0a 6e 6f 64 65 52 65 6c 65 61 73 65 28 52 74 72  .nodeRelease(Rtr
35e3e 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
35e3f 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20  eNode *pNode){. 
35e40 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
35e41 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65  _OK;.  if( pNode
35e42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
35e43 70 4e 6f 64 65 2d 3e 6e 52 65 66 3e 30 20 29 3b  pNode->nRef>0 );
35e44 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  .    pNode->nRef
35e45 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64  --;.    if( pNod
35e46 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  e->nRef==0 ){.  
35e47 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69      if( pNode->i
35e48 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 20  Node==1 ){.     
35e49 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74     pRtree->iDept
35e4a 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a  h = -1;.      }.
35e4b 20 20 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d        if( pNode-
35e4c 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  >pParent ){.    
35e4d 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c      rc = nodeRel
35e4e 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  ease(pRtree, pNo
35e4f 64 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  de->pParent);.  
35e50 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
35e51 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
35e52 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  .        rc = no
35e53 64 65 57 72 69 74 65 28 70 52 74 72 65 65 2c 20  deWrite(pRtree, 
35e54 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a  pNode);.      }.
35e55 20 20 20 20 20 20 6e 6f 64 65 48 61 73 68 44 65        nodeHashDe
35e56 6c 65 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  lete(pRtree, pNo
35e57 64 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  de);.      sqlit
35e58 65 33 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a  e3_free(pNode);.
35e59 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
35e5a 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
35e5b 52 65 74 75 72 6e 20 74 68 65 20 36 34 2d 62 69  Return the 64-bi
35e5c 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  t integer value 
35e5d 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
35e5e 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 0a 2a 2a  cell iCell of.**
35e5f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 49 66 20   node pNode. If 
35e60 70 4e 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20  pNode is a leaf 
35e61 6e 6f 64 65 2c 20 74 68 69 73 20 69 73 20 61 20  node, this is a 
35e62 72 6f 77 69 64 2e 20 49 66 20 69 74 20 69 73 0a  rowid. If it is.
35e63 2a 2a 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e  ** an internal n
35e64 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 36 34  ode, then the 64
35e65 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20  -bit integer is 
35e66 61 20 63 68 69 6c 64 20 70 61 67 65 20 6e 75 6d  a child page num
35e67 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ber..*/.static i
35e68 36 34 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28  64 nodeGetRowid(
35e69 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
35e6a 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  , .  RtreeNode *
35e6b 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20 69 43  pNode, .  int iC
35e6c 65 6c 6c 0a 29 7b 0a 20 20 61 73 73 65 72 74 28  ell.){.  assert(
35e6d 20 69 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 4e 6f   iCell<NCELL(pNo
35e6e 64 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  de) );.  return 
35e6f 72 65 61 64 49 6e 74 36 34 28 26 70 4e 6f 64 65  readInt64(&pNode
35e70 2d 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72  ->zData[4 + pRtr
35e71 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
35e72 6c 2a 69 43 65 6c 6c 5d 29 3b 0a 7d 0a 0a 2f 2a  l*iCell]);.}../*
35e73 0a 2a 2a 20 52 65 74 75 72 6e 20 63 6f 6f 72 64  .** Return coord
35e74 69 6e 61 74 65 20 69 43 6f 6f 72 64 20 66 72 6f  inate iCoord fro
35e75 6d 20 63 65 6c 6c 20 69 43 65 6c 6c 20 69 6e 20  m cell iCell in 
35e76 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73  node pNode..*/.s
35e77 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47  tatic void nodeG
35e78 65 74 43 6f 6f 72 64 28 0a 20 20 52 74 72 65 65  etCoord(.  Rtree
35e79 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72   *pRtree, .  Rtr
35e7a 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a  eeNode *pNode, .
35e7b 20 20 69 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 69    int iCell,.  i
35e7c 6e 74 20 69 43 6f 6f 72 64 2c 0a 20 20 52 74 72  nt iCoord,.  Rtr
35e7d 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 20  eeCoord *pCoord 
35e7e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
35e7f 63 65 20 74 6f 20 77 72 69 74 65 20 72 65 73 75  ce to write resu
35e80 6c 74 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 72 65  lt to */.){.  re
35e81 61 64 43 6f 6f 72 64 28 26 70 4e 6f 64 65 2d 3e  adCoord(&pNode->
35e82 7a 44 61 74 61 5b 31 32 20 2b 20 70 52 74 72 65  zData[12 + pRtre
35e83 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
35e84 2a 69 43 65 6c 6c 20 2b 20 34 2a 69 43 6f 6f 72  *iCell + 4*iCoor
35e85 64 5d 2c 20 70 43 6f 6f 72 64 29 3b 0a 7d 0a 0a  d], pCoord);.}..
35e86 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 7a  /*.** Deserializ
35e87 65 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20  e cell iCell of 
35e88 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 50 6f 70 75  node pNode. Popu
35e89 6c 61 74 65 20 74 68 65 20 73 74 72 75 63 74 75  late the structu
35e8a 72 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f  re pointed.** to
35e8b 20 62 79 20 70 43 65 6c 6c 20 77 69 74 68 20 74   by pCell with t
35e8c 68 65 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73  he results..*/.s
35e8d 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47  tatic void nodeG
35e8e 65 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20  etCell(.  Rtree 
35e8f 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
35e90 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20  eNode *pNode, . 
35e91 20 69 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 52 74   int iCell,.  Rt
35e92 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 0a 29  reeCell *pCell.)
35e93 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 70 43  {.  int ii;.  pC
35e94 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 3d 20 6e 6f  ell->iRowid = no
35e95 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65  deGetRowid(pRtre
35e96 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 29  e, pNode, iCell)
35e97 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
35e98 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 3b  <pRtree->nDim*2;
35e99 20 69 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 64 65   ii++){.    node
35e9a 47 65 74 43 6f 6f 72 64 28 70 52 74 72 65 65 2c  GetCoord(pRtree,
35e9b 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 69   pNode, iCell, i
35e9c 69 2c 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72  i, &pCell->aCoor
35e9d 64 5b 69 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a  d[ii]);.  }.}...
35e9e 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
35e9f 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66  ration for the f
35ea0 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65  unction that doe
35ea1 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a  s the work of.**
35ea2 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
35ea3 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72 65 61 74  le module xCreat
35ea4 65 28 29 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74  e() and xConnect
35ea5 28 29 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 73  () methods..*/.s
35ea6 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49  tatic int rtreeI
35ea7 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit(.  sqlite3 *
35ea8 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 63  , void *, int, c
35ea9 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
35eaa 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  *, sqlite3_vtab 
35eab 2a 2a 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74  **, char **, int
35eac 0a 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65  .);../* .** Rtre
35ead 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
35eae 6d 6f 64 75 6c 65 20 78 43 72 65 61 74 65 20 6d  module xCreate m
35eaf 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
35eb0 20 69 6e 74 20 72 74 72 65 65 43 72 65 61 74 65   int rtreeCreate
35eb1 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
35eb2 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20  .  void *pAux,. 
35eb3 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
35eb4 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
35eb5 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  v,.  sqlite3_vta
35eb6 62 20 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68  b **ppVtab,.  ch
35eb7 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20  ar **pzErr.){.  
35eb8 72 65 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74  return rtreeInit
35eb9 28 64 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c  (db, pAux, argc,
35eba 20 61 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70   argv, ppVtab, p
35ebb 7a 45 72 72 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20  zErr, 1);.}../* 
35ebc 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
35ebd 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
35ebe 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a  Connect method..
35ebf 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
35ec0 72 65 65 43 6f 6e 6e 65 63 74 28 0a 20 20 73 71  reeConnect(.  sq
35ec1 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69  lite3 *db,.  voi
35ec2 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20 61  d *pAux,.  int a
35ec3 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
35ec4 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 73  *const*argv,.  s
35ec5 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
35ec6 56 74 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70  Vtab,.  char **p
35ec7 7a 45 72 72 0a 29 7b 0a 20 20 72 65 74 75 72 6e  zErr.){.  return
35ec8 20 72 74 72 65 65 49 6e 69 74 28 64 62 2c 20 70   rtreeInit(db, p
35ec9 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  Aux, argc, argv,
35eca 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72 2c 20   ppVtab, pzErr, 
35ecb 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  0);.}../*.** Inc
35ecc 72 65 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65  rement the r-tre
35ecd 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
35ece 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
35ecf 64 20 72 74 72 65 65 52 65 66 65 72 65 6e 63 65  d rtreeReference
35ed0 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b  (Rtree *pRtree){
35ed1 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79  .  pRtree->nBusy
35ed2 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63  ++;.}../*.** Dec
35ed3 72 65 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65  rement the r-tre
35ed4 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
35ed5 74 2e 20 57 68 65 6e 20 74 68 65 20 72 65 66 65  t. When the refe
35ed6 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63  rence count reac
35ed7 68 65 73 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20  hes.** zero the 
35ed8 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c  structure is del
35ed9 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eted..*/.static 
35eda 76 6f 69 64 20 72 74 72 65 65 52 65 6c 65 61 73  void rtreeReleas
35edb 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 29  e(Rtree *pRtree)
35edc 7b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73  {.  pRtree->nBus
35edd 79 2d 2d 3b 0a 20 20 69 66 28 20 70 52 74 72 65  y--;.  if( pRtre
35ede 65 2d 3e 6e 42 75 73 79 3d 3d 30 20 29 7b 0a 20  e->nBusy==0 ){. 
35edf 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
35ee0 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61  ize(pRtree->pRea
35ee1 64 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69  dNode);.    sqli
35ee2 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74  te3_finalize(pRt
35ee3 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 29  ree->pWriteNode)
35ee4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
35ee5 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
35ee6 44 65 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20 20  DeleteNode);.   
35ee7 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
35ee8 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52  e(pRtree->pReadR
35ee9 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
35eea 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
35eeb 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29  ee->pWriteRowid)
35eec 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
35eed 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
35eee 44 65 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20  DeleteRowid);.  
35eef 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
35ef0 7a 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ze(pRtree->pRead
35ef1 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c  Parent);.    sql
35ef2 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
35ef3 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65  tree->pWritePare
35ef4 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nt);.    sqlite3
35ef5 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
35ef6 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74 29  ->pDeleteParent)
35ef7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
35ef8 65 65 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a  ee(pRtree);.  }.
35ef9 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  }../* .** Rtree 
35efa 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
35efb 64 75 6c 65 20 78 44 69 73 63 6f 6e 6e 65 63 74  dule xDisconnect
35efc 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
35efd 69 63 20 69 6e 74 20 72 74 72 65 65 44 69 73 63  ic int rtreeDisc
35efe 6f 6e 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76  onnect(sqlite3_v
35eff 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 72  tab *pVtab){.  r
35f00 74 72 65 65 52 65 6c 65 61 73 65 28 28 52 74 72  treeRelease((Rtr
35f01 65 65 20 2a 29 70 56 74 61 62 29 3b 0a 20 20 72  ee *)pVtab);.  r
35f02 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
35f03 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  .}../* .** Rtree
35f04 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
35f05 6f 64 75 6c 65 20 78 44 65 73 74 72 6f 79 20 6d  odule xDestroy m
35f06 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
35f07 20 69 6e 74 20 72 74 72 65 65 44 65 73 74 72 6f   int rtreeDestro
35f08 79 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  y(sqlite3_vtab *
35f09 70 56 74 61 62 29 7b 0a 20 20 52 74 72 65 65 20  pVtab){.  Rtree 
35f0a 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
35f0b 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20   *)pVtab;.  int 
35f0c 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 43 72 65  rc;.  char *zCre
35f0d 61 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  ate = sqlite3_mp
35f0e 72 69 6e 74 66 28 0a 20 20 20 20 22 44 52 4f 50  rintf(.    "DROP
35f0f 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f   TABLE '%q'.'%q_
35f10 6e 6f 64 65 27 3b 22 0a 20 20 20 20 22 44 52 4f  node';".    "DRO
35f11 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71  P TABLE '%q'.'%q
35f12 5f 72 6f 77 69 64 27 3b 22 0a 20 20 20 20 22 44  _rowid';".    "D
35f13 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27  ROP TABLE '%q'.'
35f14 25 71 5f 70 61 72 65 6e 74 27 3b 22 2c 0a 20 20  %q_parent';",.  
35f15 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70    pRtree->zDb, p
35f16 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20  Rtree->zName, . 
35f17 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20     pRtree->zDb, 
35f18 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 0a 20  pRtree->zName,. 
35f19 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20     pRtree->zDb, 
35f1a 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 0a 20 20  pRtree->zName.  
35f1b 29 3b 0a 20 20 69 66 28 20 21 7a 43 72 65 61 74  );.  if( !zCreat
35f1c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  e ){.    rc = SQ
35f1d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
35f1e 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  lse{.    rc = sq
35f1f 6c 69 74 65 33 5f 65 78 65 63 28 70 52 74 72 65  lite3_exec(pRtre
35f20 65 2d 3e 64 62 2c 20 7a 43 72 65 61 74 65 2c 20  e->db, zCreate, 
35f21 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  0, 0, 0);.    sq
35f22 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61  lite3_free(zCrea
35f23 74 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  te);.  }.  if( r
35f24 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
35f25 20 20 20 20 72 74 72 65 65 52 65 6c 65 61 73 65      rtreeRelease
35f26 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 0a 20  (pRtree);.  }.. 
35f27 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
35f28 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74  * .** Rtree virt
35f29 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
35f2a 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 2e 0a 2a   xOpen method..*
35f2b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
35f2c 65 65 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  eeOpen(sqlite3_v
35f2d 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69  tab *pVTab, sqli
35f2e 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
35f2f 2a 2a 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 69  **ppCursor){.  i
35f30 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  nt rc = SQLITE_N
35f31 4f 4d 45 4d 3b 0a 20 20 52 74 72 65 65 43 75 72  OMEM;.  RtreeCur
35f32 73 6f 72 20 2a 70 43 73 72 3b 0a 0a 20 20 70 43  sor *pCsr;..  pC
35f33 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f  sr = (RtreeCurso
35f34 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
35f35 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43  oc(sizeof(RtreeC
35f36 75 72 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 70  ursor));.  if( p
35f37 43 73 72 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  Csr ){.    memse
35f38 74 28 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f  t(pCsr, 0, sizeo
35f39 66 28 52 74 72 65 65 43 75 72 73 6f 72 29 29 3b  f(RtreeCursor));
35f3a 0a 20 20 20 20 70 43 73 72 2d 3e 62 61 73 65 2e  .    pCsr->base.
35f3b 70 56 74 61 62 20 3d 20 70 56 54 61 62 3b 0a 20  pVtab = pVTab;. 
35f3c 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
35f3d 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70 43 75 72 73  K;.  }.  *ppCurs
35f3e 6f 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74  or = (sqlite3_vt
35f3f 61 62 5f 63 75 72 73 6f 72 20 2a 29 70 43 73 72  ab_cursor *)pCsr
35f40 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  ;..  return rc;.
35f41 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  }../* .** Rtree 
35f42 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
35f43 64 75 6c 65 20 78 43 6c 6f 73 65 20 6d 65 74 68  dule xClose meth
35f44 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
35f45 74 20 72 74 72 65 65 43 6c 6f 73 65 28 73 71 6c  t rtreeClose(sql
35f46 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
35f47 20 2a 63 75 72 29 7b 0a 20 20 52 74 72 65 65 20   *cur){.  Rtree 
35f48 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
35f49 20 2a 29 28 63 75 72 2d 3e 70 56 74 61 62 29 3b   *)(cur->pVtab);
35f4a 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72  .  int rc;.  Rtr
35f4b 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
35f4c 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
35f4d 63 75 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  cur;.  sqlite3_f
35f4e 72 65 65 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74  ree(pCsr->aConst
35f4f 72 61 69 6e 74 29 3b 0a 20 20 72 63 20 3d 20 6e  raint);.  rc = n
35f50 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
35f51 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b  e, pCsr->pNode);
35f52 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
35f53 70 43 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  pCsr);.  return 
35f54 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72  rc;.}../*.** Rtr
35f55 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ee virtual table
35f56 20 6d 6f 64 75 6c 65 20 78 45 6f 66 20 6d 65 74   module xEof met
35f57 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  hod..**.** Retur
35f58 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68  n non-zero if th
35f59 65 20 63 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f  e cursor does no
35f5a 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  t currently poin
35f5b 74 20 74 6f 20 61 20 76 61 6c 69 64 20 0a 2a 2a  t to a valid .**
35f5c 20 72 65 63 6f 72 64 20 28 69 2e 65 20 69 66 20   record (i.e if 
35f5d 74 68 65 20 73 63 61 6e 20 68 61 73 20 66 69 6e  the scan has fin
35f5e 69 73 68 65 64 29 2c 20 6f 72 20 7a 65 72 6f 20  ished), or zero 
35f5f 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
35f60 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 45 6f  atic int rtreeEo
35f61 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  f(sqlite3_vtab_c
35f62 75 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52  ursor *cur){.  R
35f63 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72  treeCursor *pCsr
35f64 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
35f65 2a 29 63 75 72 3b 0a 20 20 72 65 74 75 72 6e 20  *)cur;.  return 
35f66 28 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 30 29  (pCsr->pNode==0)
35f67 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 43 75 72 73  ;.}../* .** Curs
35f68 6f 72 20 70 43 75 72 73 6f 72 20 63 75 72 72 65  or pCursor curre
35f69 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 61  ntly points to a
35f6a 20 63 65 6c 6c 20 69 6e 20 61 20 6e 6f 6e 2d 6c   cell in a non-l
35f6b 65 61 66 20 70 61 67 65 2e 0a 2a 2a 20 52 65 74  eaf page..** Ret
35f6c 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
35f6d 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
35f6e 62 79 20 74 68 65 20 63 65 6c 6c 20 69 73 20 66  by the cell is f
35f6f 69 6c 74 65 72 65 64 0a 2a 2a 20 28 65 78 63 6c  iltered.** (excl
35f70 75 64 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e  uded) by the con
35f71 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20  straints in the 
35f72 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72  pCursor->aConstr
35f73 61 69 6e 74 5b 5d 20 0a 2a 2a 20 61 72 72 61 79  aint[] .** array
35f74 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72  , or false other
35f75 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
35f76 69 6e 74 20 74 65 73 74 52 74 72 65 65 43 65 6c  int testRtreeCel
35f77 6c 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  l(Rtree *pRtree,
35f78 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
35f79 75 72 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 43  ursor){.  RtreeC
35f7a 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20  ell cell;.  int 
35f7b 69 69 3b 0a 20 20 69 6e 74 20 62 52 65 73 20 3d  ii;.  int bRes =
35f7c 20 30 3b 0a 0a 20 20 6e 6f 64 65 47 65 74 43 65   0;..  nodeGetCe
35f7d 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  ll(pRtree, pCurs
35f7e 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73  or->pNode, pCurs
35f7f 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c  or->iCell, &cell
35f80 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 62  );.  for(ii=0; b
35f81 52 65 73 3d 3d 30 20 26 26 20 69 69 3c 70 43 75  Res==0 && ii<pCu
35f82 72 73 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e  rsor->nConstrain
35f83 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74  t; ii++){.    Rt
35f84 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70  reeConstraint *p
35f85 20 3d 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f   = &pCursor->aCo
35f86 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20  nstraint[ii];.  
35f87 20 20 64 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d 69    double cell_mi
35f88 6e 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e  n = DCOORD(cell.
35f89 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72  aCoord[(p->iCoor
35f8a 64 3e 3e 31 29 2a 32 5d 29 3b 0a 20 20 20 20 64  d>>1)*2]);.    d
35f8b 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d 61 78 20 3d  ouble cell_max =
35f8c 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f   DCOORD(cell.aCo
35f8d 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e  ord[(p->iCoord>>
35f8e 31 29 2a 32 2b 31 5d 29 3b 0a 0a 20 20 20 20 61  1)*2+1]);..    a
35f8f 73 73 65 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52  ssert(p->op==RTR
35f90 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_LE || p->op==
35f91 52 54 52 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f  RTREE_LT || p->o
35f92 70 3d 3d 52 54 52 45 45 5f 47 45 20 0a 20 20 20  p==RTREE_GE .   
35f93 20 20 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52       || p->op==R
35f94 54 52 45 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70  TREE_GT || p->op
35f95 3d 3d 52 54 52 45 45 5f 45 51 0a 20 20 20 20 29  ==RTREE_EQ.    )
35f96 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  ;..    switch( p
35f97 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61  ->op ){.      ca
35f98 73 65 20 52 54 52 45 45 5f 4c 45 3a 20 63 61 73  se RTREE_LE: cas
35f99 65 20 52 54 52 45 45 5f 4c 54 3a 20 62 52 65 73  e RTREE_LT: bRes
35f9a 20 3d 20 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c   = p->rValue<cel
35f9b 6c 5f 6d 69 6e 3b 20 62 72 65 61 6b 3b 0a 20 20  l_min; break;.  
35f9c 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 47      case RTREE_G
35f9d 45 3a 20 63 61 73 65 20 52 54 52 45 45 5f 47 54  E: case RTREE_GT
35f9e 3a 20 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c  : bRes = p->rVal
35f9f 75 65 3e 63 65 6c 6c 5f 6d 61 78 3b 20 62 72 65  ue>cell_max; bre
35fa0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52  ak;.      case R
35fa1 54 52 45 45 5f 45 51 3a 20 0a 20 20 20 20 20 20  TREE_EQ: .      
35fa2 20 20 62 52 65 73 20 3d 20 28 70 2d 3e 72 56 61    bRes = (p->rVa
35fa3 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78 20 7c 7c 20  lue>cell_max || 
35fa4 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d  p->rValue<cell_m
35fa5 69 6e 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  in);.        bre
35fa6 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ak;.    }.  }.. 
35fa7 20 72 65 74 75 72 6e 20 62 52 65 73 3b 0a 7d 0a   return bRes;.}.
35fa8 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./* .** Return t
35fa9 72 75 65 20 69 66 20 74 68 65 20 63 65 6c 6c 20  rue if the cell 
35faa 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72  that cursor pCur
35fab 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  sor currently po
35fac 69 6e 74 73 20 74 6f 0a 2a 2a 20 77 6f 75 6c 64  ints to.** would
35fad 20 62 65 20 66 69 6c 74 65 72 65 64 20 28 65 78   be filtered (ex
35fae 63 6c 75 64 65 64 29 20 62 79 20 74 68 65 20 63  cluded) by the c
35faf 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68  onstraints in th
35fb0 65 20 0a 2a 2a 20 70 43 75 72 73 6f 72 2d 3e 61  e .** pCursor->a
35fb1 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72  Constraint[] arr
35fb2 61 79 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68  ay, or false oth
35fb3 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  erwise..**.** Th
35fb4 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75  is function assu
35fb5 6d 65 73 20 74 68 61 74 20 74 68 65 20 63 65 6c  mes that the cel
35fb6 6c 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c  l is part of a l
35fb7 65 61 66 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  eaf node..*/.sta
35fb8 74 69 63 20 69 6e 74 20 74 65 73 74 52 74 72 65  tic int testRtre
35fb9 65 45 6e 74 72 79 28 52 74 72 65 65 20 2a 70 52  eEntry(Rtree *pR
35fba 74 72 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f  tree, RtreeCurso
35fbb 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52  r *pCursor){.  R
35fbc 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
35fbd 20 69 6e 74 20 69 69 3b 0a 0a 20 20 6e 6f 64 65   int ii;..  node
35fbe 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
35fbf 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
35fc0 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20  pCursor->iCell, 
35fc1 26 63 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69  &cell);.  for(ii
35fc2 3d 30 3b 20 69 69 3c 70 43 75 72 73 6f 72 2d 3e  =0; ii<pCursor->
35fc3 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b  nConstraint; ii+
35fc4 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f 6e  +){.    RtreeCon
35fc5 73 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 43  straint *p = &pC
35fc6 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69  ursor->aConstrai
35fc7 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 64 6f 75 62  nt[ii];.    doub
35fc8 6c 65 20 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52  le coord = DCOOR
35fc9 44 28 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 70 2d  D(cell.aCoord[p-
35fca 3e 69 43 6f 6f 72 64 5d 29 3b 0a 20 20 20 20 69  >iCoord]);.    i
35fcb 6e 74 20 72 65 73 3b 0a 20 20 20 20 61 73 73 65  nt res;.    asse
35fcc 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  rt(p->op==RTREE_
35fcd 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  LE || p->op==RTR
35fce 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_LT || p->op==
35fcf 52 54 52 45 45 5f 47 45 20 0a 20 20 20 20 20 20  RTREE_GE .      
35fd0 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45    || p->op==RTRE
35fd1 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52  E_GT || p->op==R
35fd2 54 52 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a 20  TREE_EQ.    );. 
35fd3 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70     switch( p->op
35fd4 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52   ){.      case R
35fd5 54 52 45 45 5f 4c 45 3a 20 72 65 73 20 3d 20 28  TREE_LE: res = (
35fd6 63 6f 6f 72 64 3c 3d 70 2d 3e 72 56 61 6c 75 65  coord<=p->rValue
35fd7 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
35fd8 63 61 73 65 20 52 54 52 45 45 5f 4c 54 3a 20 72  case RTREE_LT: r
35fd9 65 73 20 3d 20 28 63 6f 6f 72 64 3c 70 2d 3e 72  es = (coord<p->r
35fda 56 61 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b 0a  Value);  break;.
35fdb 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45        case RTREE
35fdc 5f 47 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72  _GE: res = (coor
35fdd 64 3e 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62  d>=p->rValue); b
35fde 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
35fdf 20 52 54 52 45 45 5f 47 54 3a 20 72 65 73 20 3d   RTREE_GT: res =
35fe0 20 28 63 6f 6f 72 64 3e 70 2d 3e 72 56 61 6c 75   (coord>p->rValu
35fe1 65 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  e);  break;.    
35fe2 20 20 63 61 73 65 20 52 54 52 45 45 5f 45 51 3a    case RTREE_EQ:
35fe3 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3d 3d 70   res = (coord==p
35fe4 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72 65 61 6b  ->rValue); break
35fe5 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
35fe6 20 21 72 65 73 20 29 20 72 65 74 75 72 6e 20 31   !res ) return 1
35fe7 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
35fe8 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 75 72 73  0;.}../*.** Curs
35fe9 6f 72 20 70 43 75 72 73 6f 72 20 63 75 72 72 65  or pCursor curre
35fea 6e 74 6c 79 20 70 6f 69 6e 74 73 20 61 74 20 61  ntly points at a
35feb 20 6e 6f 64 65 20 74 68 61 74 20 68 65 61 64 73   node that heads
35fec 20 61 20 73 75 62 2d 74 72 65 65 20 6f 66 0a 2a   a sub-tree of.*
35fed 2a 20 68 65 69 67 68 74 20 69 48 65 69 67 68 74  * height iHeight
35fee 20 28 69 66 20 69 48 65 69 67 68 74 3d 3d 30 2c   (if iHeight==0,
35fef 20 74 68 65 6e 20 74 68 65 20 6e 6f 64 65 20 69   then the node i
35ff0 73 20 61 20 6c 65 61 66 29 2e 20 44 65 73 63 65  s a leaf). Desce
35ff1 6e 64 0a 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 74  nd.** to point t
35ff2 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  o the left-most 
35ff3 63 65 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 2d  cell of the sub-
35ff4 74 72 65 65 20 74 68 61 74 20 6d 61 74 63 68 65  tree that matche
35ff5 73 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 66 69 67  s the .** config
35ff6 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73  ured constraints
35ff7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35ff8 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 0a 20  descendToCell(. 
35ff9 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
35ffa 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
35ffb 70 43 75 72 73 6f 72 2c 20 0a 20 20 69 6e 74 20  pCursor, .  int 
35ffc 69 48 65 69 67 68 74 2c 0a 20 20 69 6e 74 20 2a  iHeight,.  int *
35ffd 70 45 6f 66 20 20 20 20 20 20 20 20 20 20 20 20  pEof            
35ffe 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74       /* OUT: Set
35fff 20 74 6f 20 74 72 75 65 20 69 66 20 63 61 6e 6e   to true if cann
36000 6f 74 20 64 65 73 63 65 6e 64 20 2a 2f 0a 29 7b  ot descend */.){
36001 0a 20 20 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20  .  int isEof;.  
36002 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69  int rc;.  int ii
36003 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ;.  RtreeNode *p
36004 43 68 69 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33  Child;.  sqlite3
36005 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 3b 0a 0a  _int64 iRowid;..
36006 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 53 61    RtreeNode *pSa
36007 76 65 64 4e 6f 64 65 20 3d 20 70 43 75 72 73 6f  vedNode = pCurso
36008 72 2d 3e 70 4e 6f 64 65 3b 0a 20 20 69 6e 74 20  r->pNode;.  int 
36009 69 53 61 76 65 64 43 65 6c 6c 20 3d 20 70 43 75  iSavedCell = pCu
3600a 72 73 6f 72 2d 3e 69 43 65 6c 6c 3b 0a 0a 20 20  rsor->iCell;..  
3600b 61 73 73 65 72 74 28 20 69 48 65 69 67 68 74 3e  assert( iHeight>
3600c 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 69 48 65  =0 );..  if( iHe
3600d 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ight==0 ){.    i
3600e 73 45 6f 66 20 3d 20 74 65 73 74 52 74 72 65 65  sEof = testRtree
3600f 45 6e 74 72 79 28 70 52 74 72 65 65 2c 20 70 43  Entry(pRtree, pC
36010 75 72 73 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  ursor);.  }else{
36011 0a 20 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73  .    isEof = tes
36012 74 52 74 72 65 65 43 65 6c 6c 28 70 52 74 72 65  tRtreeCell(pRtre
36013 65 2c 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  e, pCursor);.  }
36014 0a 20 20 69 66 28 20 69 73 45 6f 66 20 7c 7c 20  .  if( isEof || 
36015 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20  iHeight==0 ){.  
36016 20 20 2a 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b    *pEof = isEof;
36017 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
36018 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 52  TE_OK;.  }..  iR
36019 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  owid = nodeGetRo
3601a 77 69 64 28 70 52 74 72 65 65 2c 20 70 43 75 72  wid(pRtree, pCur
3601b 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72  sor->pNode, pCur
3601c 73 6f 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 72  sor->iCell);.  r
3601d 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
3601e 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20  pRtree, iRowid, 
3601f 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
36020 26 70 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 20  &pChild);.  if( 
36021 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
36022 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
36023 20 20 7d 0a 0a 20 20 6e 6f 64 65 52 65 6c 65 61    }..  nodeRelea
36024 73 65 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  se(pRtree, pCurs
36025 6f 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 70 43  or->pNode);.  pC
36026 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d 20 70  ursor->pNode = p
36027 43 68 69 6c 64 3b 0a 20 20 69 73 45 6f 66 20 3d  Child;.  isEof =
36028 20 31 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   1;.  for(ii=0; 
36029 69 73 45 6f 66 20 26 26 20 69 69 3c 4e 43 45 4c  isEof && ii<NCEL
3602a 4c 28 70 43 68 69 6c 64 29 3b 20 69 69 2b 2b 29  L(pChild); ii++)
3602b 7b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69  {.    pCursor->i
3602c 43 65 6c 6c 20 3d 20 69 69 3b 0a 20 20 20 20 72  Cell = ii;.    r
3602d 63 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c  c = descendToCel
3602e 6c 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f  l(pRtree, pCurso
3602f 72 2c 20 69 48 65 69 67 68 74 2d 31 2c 20 26 69  r, iHeight-1, &i
36030 73 45 6f 66 29 3b 0a 20 20 20 20 69 66 28 20 72  sEof);.    if( r
36031 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
36032 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
36033 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
36034 28 20 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 61  ( isEof ){.    a
36035 73 73 65 72 74 28 20 70 43 75 72 73 6f 72 2d 3e  ssert( pCursor->
36036 70 4e 6f 64 65 3d 3d 70 43 68 69 6c 64 20 29 3b  pNode==pChild );
36037 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e  .    nodeReferen
36038 63 65 28 70 53 61 76 65 64 4e 6f 64 65 29 3b 0a  ce(pSavedNode);.
36039 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
3603a 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 29 3b  pRtree, pChild);
3603b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e  .    pCursor->pN
3603c 6f 64 65 20 3d 20 70 53 61 76 65 64 4e 6f 64 65  ode = pSavedNode
3603d 3b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69  ;.    pCursor->i
3603e 43 65 6c 6c 20 3d 20 69 53 61 76 65 64 43 65 6c  Cell = iSavedCel
3603f 6c 3b 0a 20 20 7d 0a 0a 20 20 2a 70 45 6f 66 20  l;.  }..  *pEof 
36040 3d 20 69 73 45 6f 66 3b 0a 20 20 72 65 74 75 72  = isEof;.  retur
36041 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
36042 2f 2a 0a 2a 2a 20 4f 6e 65 20 6f 66 20 74 68 65  /*.** One of the
36043 20 63 65 6c 6c 73 20 69 6e 20 6e 6f 64 65 20 70   cells in node p
36044 4e 6f 64 65 20 69 73 20 67 75 61 72 61 6e 74 65  Node is guarante
36045 65 64 20 74 6f 20 68 61 76 65 20 61 20 36 34 2d  ed to have a 64-
36046 62 69 74 20 0a 2a 2a 20 69 6e 74 65 67 65 72 20  bit .** integer 
36047 76 61 6c 75 65 20 65 71 75 61 6c 20 74 6f 20 69  value equal to i
36048 52 6f 77 69 64 2e 20 52 65 74 75 72 6e 20 74 68  Rowid. Return th
36049 65 20 69 6e 64 65 78 20 6f 66 20 74 68 69 73 20  e index of this 
3604a 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cell..*/.static 
3604b 69 6e 74 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64  int nodeRowidInd
3604c 65 78 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ex(Rtree *pRtree
3604d 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
3604e 64 65 2c 20 69 36 34 20 69 52 6f 77 69 64 29 7b  de, i64 iRowid){
3604f 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72  .  int ii;.  for
36050 28 69 69 3d 30 3b 20 6e 6f 64 65 47 65 74 52 6f  (ii=0; nodeGetRo
36051 77 69 64 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  wid(pRtree, pNod
36052 65 2c 20 69 69 29 21 3d 69 52 6f 77 69 64 3b 20  e, ii)!=iRowid; 
36053 69 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72  ii++){.    asser
36054 74 28 20 69 69 3c 28 4e 43 45 4c 4c 28 70 4e 6f  t( ii<(NCELL(pNo
36055 64 65 29 2d 31 29 20 29 3b 0a 20 20 7d 0a 20 20  de)-1) );.  }.  
36056 72 65 74 75 72 6e 20 69 69 3b 0a 7d 0a 0a 2f 2a  return ii;.}../*
36057 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69  .** Return the i
36058 6e 64 65 78 20 6f 66 20 74 68 65 20 63 65 6c 6c  ndex of the cell
36059 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 6f   containing a po
3605a 69 6e 74 65 72 20 74 6f 20 6e 6f 64 65 20 70 4e  inter to node pN
3605b 6f 64 65 0a 2a 2a 20 69 6e 20 69 74 73 20 70 61  ode.** in its pa
3605c 72 65 6e 74 2e 20 49 66 20 70 4e 6f 64 65 20 69  rent. If pNode i
3605d 73 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2c  s the root node,
3605e 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 73   return -1..*/.s
3605f 74 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 50 61  tatic int nodePa
36060 72 65 6e 74 49 6e 64 65 78 28 52 74 72 65 65 20  rentIndex(Rtree 
36061 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
36062 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74  de *pNode){.  Rt
36063 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  reeNode *pParent
36064 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
36065 74 3b 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74  t;.  if( pParent
36066 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e   ){.    return n
36067 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28 70 52  odeRowidIndex(pR
36068 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 70  tree, pParent, p
36069 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
3606a 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d  }.  return -1;.}
3606b 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
3606c 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
3606d 75 6c 65 20 78 4e 65 78 74 20 6d 65 74 68 6f 64  ule xNext method
3606e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3606f 72 74 72 65 65 4e 65 78 74 28 73 71 6c 69 74 65  rtreeNext(sqlite
36070 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
36071 56 74 61 62 43 75 72 73 6f 72 29 7b 0a 20 20 52  VtabCursor){.  R
36072 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
36073 52 74 72 65 65 20 2a 29 28 70 56 74 61 62 43 75  Rtree *)(pVtabCu
36074 72 73 6f 72 2d 3e 70 56 74 61 62 29 3b 0a 20 20  rsor->pVtab);.  
36075 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73  RtreeCursor *pCs
36076 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72  r = (RtreeCursor
36077 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a   *)pVtabCursor;.
36078 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
36079 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 43 73  E_OK;..  if( pCs
3607a 72 2d 3e 69 53 74 72 61 74 65 67 79 3d 3d 31 20  r->iStrategy==1 
3607b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 22  ){.    /* This "
3607c 73 63 61 6e 22 20 69 73 20 61 20 64 69 72 65 63  scan" is a direc
3607d 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69  t lookup by rowi
3607e 64 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e  d. There is no n
3607f 65 78 74 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20  ext entry. */.  
36080 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
36081 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64  tree, pCsr->pNod
36082 65 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e  e);.    pCsr->pN
36083 6f 64 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  ode = 0;.  }..  
36084 65 6c 73 65 20 69 66 28 20 70 43 73 72 2d 3e 70  else if( pCsr->p
36085 4e 6f 64 65 20 29 7b 0a 20 20 20 20 2f 2a 20 4d  Node ){.    /* M
36086 6f 76 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ove to the next 
36087 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68  entry that match
36088 65 73 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65  es the configure
36089 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a  d constraints. *
3608a 2f 0a 20 20 20 20 69 6e 74 20 69 48 65 69 67 68  /.    int iHeigh
3608b 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  t = 0;.    while
3608c 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 29 7b  ( pCsr->pNode ){
3608d 0a 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65  .      RtreeNode
3608e 20 2a 70 4e 6f 64 65 20 3d 20 70 43 73 72 2d 3e   *pNode = pCsr->
3608f 70 4e 6f 64 65 3b 0a 20 20 20 20 20 20 69 6e 74  pNode;.      int
36090 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
36091 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 66 6f 72  Node);.      for
36092 28 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 3b 20  (pCsr->iCell++; 
36093 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c  pCsr->iCell<nCel
36094 6c 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b  l; pCsr->iCell++
36095 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
36096 73 45 6f 66 3b 0a 20 20 20 20 20 20 20 20 72 63  sEof;.        rc
36097 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c   = descendToCell
36098 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c 20 69  (pRtree, pCsr, i
36099 48 65 69 67 68 74 2c 20 26 69 73 45 6f 66 29 3b  Height, &isEof);
3609a 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
3609b 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 69  =SQLITE_OK || !i
3609c 73 45 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20  sEof ){.        
3609d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
3609e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
3609f 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
360a0 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
360a1 74 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69  t;.      pCsr->i
360a2 43 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e  Cell = nodeParen
360a3 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70  tIndex(pRtree, p
360a4 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64  Node);.      nod
360a5 65 52 65 66 65 72 65 6e 63 65 28 70 43 73 72 2d  eReference(pCsr-
360a6 3e 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e  >pNode);.      n
360a7 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
360a8 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e, pNode);.     
360a9 20 69 48 65 69 67 68 74 2b 2b 3b 0a 20 20 20 20   iHeight++;.    
360aa 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
360ab 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  rc;.}../* .** Rt
360ac 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
360ad 65 20 6d 6f 64 75 6c 65 20 78 52 6f 77 69 64 20  e module xRowid 
360ae 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69  method..*/.stati
360af 63 20 69 6e 74 20 72 74 72 65 65 52 6f 77 69 64  c int rtreeRowid
360b0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
360b1 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f  rsor *pVtabCurso
360b2 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  r, sqlite_int64 
360b3 2a 70 52 6f 77 69 64 29 7b 0a 20 20 52 74 72 65  *pRowid){.  Rtre
360b4 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72  e *pRtree = (Rtr
360b5 65 65 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72  ee *)pVtabCursor
360b6 2d 3e 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65  ->pVtab;.  Rtree
360b7 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
360b8 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 70 56  RtreeCursor *)pV
360b9 74 61 62 43 75 72 73 6f 72 3b 0a 0a 20 20 61 73  tabCursor;..  as
360ba 73 65 72 74 28 70 43 73 72 2d 3e 70 4e 6f 64 65  sert(pCsr->pNode
360bb 29 3b 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 6e  );.  *pRowid = n
360bc 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72  odeGetRowid(pRtr
360bd 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c  ee, pCsr->pNode,
360be 20 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 0a   pCsr->iCell);..
360bf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
360c0 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  OK;.}../* .** Rt
360c1 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
360c2 65 20 6d 6f 64 75 6c 65 20 78 43 6f 6c 75 6d 6e  e module xColumn
360c3 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
360c4 69 63 20 69 6e 74 20 72 74 72 65 65 43 6f 6c 75  ic int rtreeColu
360c5 6d 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  mn(sqlite3_vtab_
360c6 63 75 72 73 6f 72 20 2a 63 75 72 2c 20 73 71 6c  cursor *cur, sql
360c7 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74  ite3_context *ct
360c8 78 2c 20 69 6e 74 20 69 29 7b 0a 20 20 52 74 72  x, int i){.  Rtr
360c9 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
360ca 72 65 65 20 2a 29 63 75 72 2d 3e 70 56 74 61 62  ree *)cur->pVtab
360cb 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20  ;.  RtreeCursor 
360cc 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75  *pCsr = (RtreeCu
360cd 72 73 6f 72 20 2a 29 63 75 72 3b 0a 0a 20 20 69  rsor *)cur;..  i
360ce 66 28 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 69  f( i==0 ){.    i
360cf 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  64 iRowid = node
360d0 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
360d1 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43   pCsr->pNode, pC
360d2 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 20 20  sr->iCell);.    
360d3 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
360d4 6e 74 36 34 28 63 74 78 2c 20 69 52 6f 77 69 64  nt64(ctx, iRowid
360d5 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
360d6 52 74 72 65 65 43 6f 6f 72 64 20 63 3b 0a 20 20  RtreeCoord c;.  
360d7 20 20 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70    nodeGetCoord(p
360d8 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f  Rtree, pCsr->pNo
360d9 64 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2c  de, pCsr->iCell,
360da 20 69 2d 31 2c 20 26 63 29 3b 0a 20 20 20 20 69   i-1, &c);.    i
360db 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72  f( pRtree->eCoor
360dc 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f  dType==RTREE_COO
360dd 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20  RD_REAL32 ){.   
360de 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
360df 74 5f 64 6f 75 62 6c 65 28 63 74 78 2c 20 63 2e  t_double(ctx, c.
360e0 66 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  f);.    }else{. 
360e1 20 20 20 20 20 61 73 73 65 72 74 28 20 70 52 74       assert( pRt
360e2 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
360e3 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54  =RTREE_COORD_INT
360e4 33 32 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  32 );.      sqli
360e5 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
360e6 74 78 2c 20 63 2e 69 29 3b 0a 20 20 20 20 7d 0a  tx, c.i);.    }.
360e7 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
360e8 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a  LITE_OK;.}../* .
360e9 2a 2a 20 55 73 65 20 6e 6f 64 65 41 63 71 75 69  ** Use nodeAcqui
360ea 72 65 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 74  re() to obtain t
360eb 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 63 6f 6e  he leaf node con
360ec 74 61 69 6e 69 6e 67 20 74 68 65 20 72 65 63 6f  taining the reco
360ed 72 64 20 77 69 74 68 20 0a 2a 2a 20 72 6f 77 69  rd with .** rowi
360ee 64 20 69 52 6f 77 69 64 2e 20 49 66 20 73 75 63  d iRowid. If suc
360ef 63 65 73 73 66 75 6c 2c 20 73 65 74 20 2a 70 70  cessful, set *pp
360f0 4c 65 61 66 20 74 6f 20 70 6f 69 6e 74 20 74 6f  Leaf to point to
360f1 20 74 68 65 20 6e 6f 64 65 20 61 6e 64 0a 2a 2a   the node and.**
360f2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
360f3 4b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  K. If there is n
360f4 6f 20 73 75 63 68 20 72 65 63 6f 72 64 20 69 6e  o such record in
360f5 20 74 68 65 20 74 61 62 6c 65 2c 20 73 65 74 0a   the table, set.
360f6 2a 2a 20 2a 70 70 4c 65 61 66 20 74 6f 20 30 20  ** *ppLeaf to 0 
360f7 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
360f8 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 65 72 72 6f  E_OK. If an erro
360f9 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 2a 70  r occurs, set *p
360fa 70 4c 65 61 66 0a 2a 2a 20 74 6f 20 7a 65 72 6f  pLeaf.** to zero
360fb 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 53   and return an S
360fc 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
360fd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
360fe 66 69 6e 64 4c 65 61 66 4e 6f 64 65 28 52 74 72  findLeafNode(Rtr
360ff 65 65 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20  ee *pRtree, i64 
36100 69 52 6f 77 69 64 2c 20 52 74 72 65 65 4e 6f 64  iRowid, RtreeNod
36101 65 20 2a 2a 70 70 4c 65 61 66 29 7b 0a 20 20 69  e **ppLeaf){.  i
36102 6e 74 20 72 63 3b 0a 20 20 2a 70 70 4c 65 61 66  nt rc;.  *ppLeaf
36103 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
36104 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
36105 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 31  e->pReadRowid, 1
36106 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 69 66 28  , iRowid);.  if(
36107 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
36108 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
36109 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
3610a 0a 20 20 20 20 69 36 34 20 69 4e 6f 64 65 20 3d  .    i64 iNode =
3610b 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
3610c 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
3610d 65 61 64 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20  eadRowid, 0);.  
3610e 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69    rc = nodeAcqui
3610f 72 65 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65  re(pRtree, iNode
36110 2c 20 30 2c 20 70 70 4c 65 61 66 29 3b 0a 20 20  , 0, ppLeaf);.  
36111 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
36112 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
36113 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  id);.  }else{.  
36114 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
36115 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
36116 61 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 20 20  adRowid);.  }.  
36117 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
36118 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74  * .** Rtree virt
36119 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
3611a 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e   xFilter method.
3611b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
3611c 74 72 65 65 46 69 6c 74 65 72 28 0a 20 20 73 71  treeFilter(.  sq
3611d 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
3611e 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20  r *pVtabCursor, 
3611f 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63  .  int idxNum, c
36120 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74  onst char *idxSt
36121 72 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73  r,.  int argc, s
36122 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
36123 72 67 76 0a 29 7b 0a 20 20 52 74 72 65 65 20 2a  rgv.){.  Rtree *
36124 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20  pRtree = (Rtree 
36125 2a 29 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  *)pVtabCursor->p
36126 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72  Vtab;.  RtreeCur
36127 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72  sor *pCsr = (Rtr
36128 65 65 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62  eeCursor *)pVtab
36129 43 75 72 73 6f 72 3b 0a 0a 20 20 52 74 72 65 65  Cursor;..  Rtree
3612a 4e 6f 64 65 20 2a 70 52 6f 6f 74 20 3d 20 30 3b  Node *pRoot = 0;
3612b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
3612c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
3612d 0a 0a 20 20 72 74 72 65 65 52 65 66 65 72 65 6e  ..  rtreeReferen
3612e 63 65 28 70 52 74 72 65 65 29 3b 0a 0a 20 20 73  ce(pRtree);..  s
3612f 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
36130 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a  ->aConstraint);.
36131 20 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61    pCsr->aConstra
36132 69 6e 74 20 3d 20 30 3b 0a 20 20 70 43 73 72 2d  int = 0;.  pCsr-
36133 3e 69 53 74 72 61 74 65 67 79 20 3d 20 69 64 78  >iStrategy = idx
36134 4e 75 6d 3b 0a 0a 20 20 69 66 28 20 69 64 78 4e  Num;..  if( idxN
36135 75 6d 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  um==1 ){.    /* 
36136 53 70 65 63 69 61 6c 20 63 61 73 65 20 2d 20 6c  Special case - l
36137 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 20  ookup by rowid. 
36138 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  */.    RtreeNode
36139 20 2a 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20   *pLeaf;        
3613a 2f 2a 20 4c 65 61 66 20 6f 6e 20 77 68 69 63 68  /* Leaf on which
3613b 20 74 68 65 20 72 65 71 75 69 72 65 64 20 63 65   the required ce
3613c 6c 6c 20 72 65 73 69 64 65 73 20 2a 2f 0a 20 20  ll resides */.  
3613d 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20 73    i64 iRowid = s
3613e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
3613f 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  64(argv[0]);.   
36140 20 72 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f   rc = findLeafNo
36141 64 65 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69  de(pRtree, iRowi
36142 64 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20  d, &pLeaf);.    
36143 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 4c  pCsr->pNode = pL
36144 65 61 66 3b 20 0a 20 20 20 20 69 66 28 20 70 4c  eaf; .    if( pL
36145 65 61 66 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  eaf && rc==SQLIT
36146 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43  E_OK ){.      pC
36147 73 72 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  sr->iCell = node
36148 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72 65  RowidIndex(pRtre
36149 65 2c 20 70 4c 65 61 66 2c 20 69 52 6f 77 69 64  e, pLeaf, iRowid
3614a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
3614b 7b 0a 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20  {.    /* Normal 
3614c 63 61 73 65 20 2d 20 72 2d 74 72 65 65 20 73 63  case - r-tree sc
3614d 61 6e 2e 20 53 65 74 20 75 70 20 74 68 65 20 52  an. Set up the R
3614e 74 72 65 65 43 75 72 73 6f 72 2e 61 43 6f 6e 73  treeCursor.aCons
3614f 74 72 61 69 6e 74 20 61 72 72 61 79 20 0a 20 20  traint array .  
36150 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 63 6f    ** with the co
36151 6e 66 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61  nfigured constra
36152 69 6e 74 73 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  ints. .    */.  
36153 20 20 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a    if( argc>0 ){.
36154 20 20 20 20 20 20 70 43 73 72 2d 3e 61 43 6f 6e        pCsr->aCon
36155 73 74 72 61 69 6e 74 20 3d 20 73 71 6c 69 74 65  straint = sqlite
36156 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
36157 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 29  RtreeConstraint)
36158 2a 61 72 67 63 29 3b 0a 20 20 20 20 20 20 70 43  *argc);.      pC
36159 73 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20  sr->nConstraint 
3615a 3d 20 61 72 67 63 3b 0a 20 20 20 20 20 20 69 66  = argc;.      if
3615b 28 20 21 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72  ( !pCsr->aConstr
3615c 61 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  aint ){.        
3615d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
3615e 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  M;.      }else{.
3615f 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
36160 28 69 64 78 53 74 72 3d 3d 30 20 26 26 20 61 72  (idxStr==0 && ar
36161 67 63 3d 3d 30 29 20 7c 7c 20 73 74 72 6c 65 6e  gc==0) || strlen
36162 28 69 64 78 53 74 72 29 3d 3d 61 72 67 63 2a 32  (idxStr)==argc*2
36163 20 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   );.        for(
36164 69 69 3d 30 3b 20 69 69 3c 61 72 67 63 3b 20 69  ii=0; ii<argc; i
36165 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
36166 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20  RtreeConstraint 
36167 2a 70 20 3d 20 26 70 43 73 72 2d 3e 61 43 6f 6e  *p = &pCsr->aCon
36168 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20  straint[ii];.   
36169 20 20 20 20 20 20 20 70 2d 3e 6f 70 20 3d 20 69         p->op = i
3616a 64 78 53 74 72 5b 69 69 2a 32 5d 3b 0a 20 20 20  dxStr[ii*2];.   
3616b 20 20 20 20 20 20 20 70 2d 3e 69 43 6f 6f 72 64         p->iCoord
3616c 20 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 2b 31   = idxStr[ii*2+1
3616d 5d 2d 27 61 27 3b 0a 20 20 20 20 20 20 20 20 20  ]-'a';.         
3616e 20 70 2d 3e 72 56 61 6c 75 65 20 3d 20 73 71 6c   p->rValue = sql
3616f 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
36170 65 28 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20  e(argv[ii]);.   
36171 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
36172 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20     }.  .    if( 
36173 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
36174 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f  .      pCsr->pNo
36175 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  de = 0;.      rc
36176 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70   = nodeAcquire(p
36177 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26 70 52  Rtree, 1, 0, &pR
36178 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oot);.    }.    
36179 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3617a 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  K ){.      int i
3617b 73 45 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  sEof = 1;.      
3617c 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  int nCell = NCEL
3617d 4c 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20  L(pRoot);.      
3617e 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 52  pCsr->pNode = pR
3617f 6f 6f 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  oot;.      for(p
36180 43 73 72 2d 3e 69 43 65 6c 6c 3d 30 3b 20 72 63  Csr->iCell=0; rc
36181 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
36182 43 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c  Csr->iCell<nCell
36183 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29  ; pCsr->iCell++)
36184 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
36185 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70  ( pCsr->pNode==p
36186 52 6f 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20  Root );.        
36187 72 63 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65  rc = descendToCe
36188 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c  ll(pRtree, pCsr,
36189 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2c   pRtree->iDepth,
3618a 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20 20 20   &isEof);.      
3618b 20 20 69 66 28 20 21 69 73 45 6f 66 20 29 7b 0a    if( !isEof ){.
3618c 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
3618d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3618e 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d   }.      if( rc=
3618f 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 73  =SQLITE_OK && is
36190 45 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20 61  Eof ){.        a
36191 73 73 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f  ssert( pCsr->pNo
36192 64 65 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20  de==pRoot );.   
36193 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65       nodeRelease
36194 28 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b  (pRtree, pRoot);
36195 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e 70  .        pCsr->p
36196 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Node = 0;.      
36197 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
36198 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
36199 20 21 70 43 73 72 2d 3e 70 4e 6f 64 65 20 7c 7c   !pCsr->pNode ||
3619a 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 4e 43 45   pCsr->iCell<NCE
3619b 4c 4c 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 20  LL(pCsr->pNode) 
3619c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
3619d 72 74 72 65 65 52 65 6c 65 61 73 65 28 70 52 74  rtreeRelease(pRt
3619e 72 65 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ree);.  return r
3619f 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65  c;.}../*.** Rtre
361a0 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
361a1 6d 6f 64 75 6c 65 20 78 42 65 73 74 49 6e 64 65  module xBestInde
361a2 78 20 6d 65 74 68 6f 64 2e 20 54 68 65 72 65 20  x method. There 
361a3 61 72 65 20 74 68 72 65 65 0a 2a 2a 20 74 61 62  are three.** tab
361a4 6c 65 20 73 63 61 6e 20 73 74 72 61 74 65 67 69  le scan strategi
361a5 65 73 20 74 6f 20 63 68 6f 6f 73 65 20 66 72 6f  es to choose fro
361a6 6d 20 28 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d  m (in order from
361a7 20 6d 6f 73 74 20 74 6f 20 0a 2a 2a 20 6c 65 61   most to .** lea
361a8 73 74 20 64 65 73 69 72 61 62 6c 65 29 3a 0a 2a  st desirable):.*
361a9 2a 0a 2a 2a 20 20 20 69 64 78 4e 75 6d 20 20 20  *.**   idxNum   
361aa 20 20 69 64 78 53 74 72 20 20 20 20 20 20 20 20    idxStr        
361ab 53 74 72 61 74 65 67 79 0a 2a 2a 20 20 20 2d 2d  Strategy.**   --
361ac 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
361ad 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
361ae 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
361af 2a 20 20 20 20 20 31 20 20 20 20 20 20 20 20 55  *     1        U
361b0 6e 75 73 65 64 20 20 20 20 20 20 20 20 44 69 72  nused        Dir
361b1 65 63 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f  ect lookup by ro
361b2 77 69 64 2e 0a 2a 2a 20 20 20 20 20 32 20 20 20  wid..**     2   
361b3 20 20 20 20 20 53 65 65 20 62 65 6c 6f 77 20 20       See below  
361b4 20 20 20 52 2d 74 72 65 65 20 71 75 65 72 79 2e     R-tree query.
361b5 0a 2a 2a 20 20 20 20 20 33 20 20 20 20 20 20 20  .**     3       
361b6 20 55 6e 75 73 65 64 20 20 20 20 20 20 20 20 46   Unused        F
361b7 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e 0a  ull table scan..
361b8 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
361b9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
361ba 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
361bb 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 49 66 20 73  -----.**.** If s
361bc 74 72 61 74 65 67 79 20 31 20 6f 72 20 33 20 69  trategy 1 or 3 i
361bd 73 20 75 73 65 64 2c 20 74 68 65 6e 20 69 64 78  s used, then idx
361be 53 74 72 20 69 73 20 6e 6f 74 20 6d 65 61 6e 69  Str is not meani
361bf 6e 67 66 75 6c 2e 20 49 66 20 73 74 72 61 74 65  ngful. If strate
361c0 67 79 0a 2a 2a 20 32 20 69 73 20 75 73 65 64 2c  gy.** 2 is used,
361c1 20 69 64 78 53 74 72 20 69 73 20 66 6f 72 6d 61   idxStr is forma
361c2 74 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20  tted to contain 
361c3 32 20 62 79 74 65 73 20 66 6f 72 20 65 61 63 68  2 bytes for each
361c4 20 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20   .** constraint 
361c5 75 73 65 64 2e 20 54 68 65 20 66 69 72 73 74 20  used. The first 
361c6 74 77 6f 20 62 79 74 65 73 20 6f 66 20 69 64 78  two bytes of idx
361c7 53 74 72 20 63 6f 72 72 65 73 70 6f 6e 64 20 74  Str correspond t
361c8 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72  o .** the constr
361c9 61 69 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 5f  aint in sqlite3_
361ca 69 6e 64 65 78 5f 69 6e 66 6f 2e 61 43 6f 6e 73  index_info.aCons
361cb 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69  traintUsage[] wi
361cc 74 68 0a 2a 2a 20 28 61 72 67 76 49 6e 64 65 78  th.** (argvIndex
361cd 3d 3d 31 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20  ==1) etc..**.** 
361ce 54 68 65 20 66 69 72 73 74 20 6f 66 20 65 61 63  The first of eac
361cf 68 20 70 61 69 72 20 6f 66 20 62 79 74 65 73 20  h pair of bytes 
361d0 69 6e 20 69 64 78 53 74 72 20 69 64 65 6e 74 69  in idxStr identi
361d1 66 69 65 73 20 74 68 65 20 63 6f 6e 73 74 72 61  fies the constra
361d2 69 6e 74 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20  int.** operator 
361d3 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
361d4 2a 20 20 20 4f 70 65 72 61 74 6f 72 20 20 20 20  *   Operator    
361d5 42 79 74 65 20 56 61 6c 75 65 0a 2a 2a 20 20 20  Byte Value.**   
361d6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
361d7 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 3d  ------.**      =
361d8 20 20 20 20 20 20 20 20 30 78 34 31 20 28 27 41          0x41 ('A
361d9 27 29 0a 2a 2a 20 20 20 20 20 3c 3d 20 20 20 20  ').**     <=    
361da 20 20 20 20 30 78 34 32 20 28 27 42 27 29 0a 2a      0x42 ('B').*
361db 2a 20 20 20 20 20 20 3c 20 20 20 20 20 20 20 20  *      <        
361dc 30 78 34 33 20 28 27 43 27 29 0a 2a 2a 20 20 20  0x43 ('C').**   
361dd 20 20 3e 3d 20 20 20 20 20 20 20 20 30 78 34 34    >=        0x44
361de 20 28 27 44 27 29 0a 2a 2a 20 20 20 20 20 20 3e   ('D').**      >
361df 20 20 20 20 20 20 20 20 30 78 34 35 20 28 27 45          0x45 ('E
361e0 27 29 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ').**   --------
361e1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
361e2 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
361e3 6f 66 20 65 61 63 68 20 70 61 69 72 20 6f 66 20  of each pair of 
361e4 62 79 74 65 73 20 69 64 65 6e 74 69 66 69 65 73  bytes identifies
361e5 20 74 68 65 20 63 6f 6f 72 64 69 6e 61 74 65 20   the coordinate 
361e6 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 77 68 69  column.** to whi
361e7 63 68 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  ch the constrain
361e8 74 20 61 70 70 6c 69 65 73 2e 20 54 68 65 20 6c  t applies. The l
361e9 65 66 74 6d 6f 73 74 20 63 6f 6f 72 64 69 6e 61  eftmost coordina
361ea 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20  te column.** is 
361eb 27 61 27 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  'a', the second 
361ec 66 72 6f 6d 20 74 68 65 20 6c 65 66 74 20 27 62  from the left 'b
361ed 27 20 65 74 63 2e 0a 2a 2f 0a 73 74 61 74 69 63  ' etc..*/.static
361ee 20 69 6e 74 20 72 74 72 65 65 42 65 73 74 49 6e   int rtreeBestIn
361ef 64 65 78 28 73 71 6c 69 74 65 33 5f 76 74 61 62  dex(sqlite3_vtab
361f0 20 2a 74 61 62 2c 20 73 71 6c 69 74 65 33 5f 69   *tab, sqlite3_i
361f1 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49  ndex_info *pIdxI
361f2 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  nfo){.  int rc =
361f3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
361f4 74 20 69 69 2c 20 63 43 6f 6c 3b 0a 0a 20 20 69  t ii, cCol;..  i
361f5 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 63  nt iIdx = 0;.  c
361f6 68 61 72 20 7a 49 64 78 53 74 72 5b 52 54 52 45  har zIdxStr[RTRE
361f7 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53  E_MAX_DIMENSIONS
361f8 2a 38 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28  *8+1];.  memset(
361f9 7a 49 64 78 53 74 72 2c 20 30 2c 20 73 69 7a 65  zIdxStr, 0, size
361fa 6f 66 28 7a 49 64 78 53 74 72 29 29 3b 0a 0a 20  of(zIdxStr));.. 
361fb 20 61 73 73 65 72 74 28 20 70 49 64 78 49 6e 66   assert( pIdxInf
361fc 6f 2d 3e 69 64 78 53 74 72 3d 3d 30 20 29 3b 0a  o->idxStr==0 );.
361fd 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
361fe 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72  IdxInfo->nConstr
361ff 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  aint; ii++){.   
36200 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
36201 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
36202 20 2a 70 20 3d 20 26 70 49 64 78 49 6e 66 6f 2d   *p = &pIdxInfo-
36203 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d  >aConstraint[ii]
36204 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73  ;..    if( p->us
36205 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75  able && p->iColu
36206 6d 6e 3d 3d 30 20 26 26 20 70 2d 3e 6f 70 3d 3d  mn==0 && p->op==
36207 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
36208 53 54 52 41 49 4e 54 5f 45 51 20 29 7b 0a 20 20  STRAINT_EQ ){.  
36209 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61      /* We have a
3620a 6e 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  n equality const
3620b 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 72 6f 77  raint on the row
3620c 69 64 2e 20 55 73 65 20 73 74 72 61 74 65 67 79  id. Use strategy
3620d 20 31 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74   1. */.      int
3620e 20 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a   jj;.      for(j
3620f 6a 3d 30 3b 20 6a 6a 3c 69 69 3b 20 6a 6a 2b 2b  j=0; jj<ii; jj++
36210 29 7b 0a 20 20 20 20 20 20 20 20 70 49 64 78 49  ){.        pIdxI
36211 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
36212 55 73 61 67 65 5b 6a 6a 5d 2e 61 72 67 76 49 6e  Usage[jj].argvIn
36213 64 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dex = 0;.       
36214 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73   pIdxInfo->aCons
36215 74 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e  traintUsage[jj].
36216 6f 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  omit = 0;.      
36217 7d 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f  }.      pIdxInfo
36218 2d 3e 69 64 78 4e 75 6d 20 3d 20 31 3b 0a 20 20  ->idxNum = 1;.  
36219 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43      pIdxInfo->aC
3621a 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69  onstraintUsage[i
3621b 69 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 31  i].argvIndex = 1
3621c 3b 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f  ;.      pIdxInfo
3621d 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
3621e 67 65 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 31 3b  ge[jj].omit = 1;
3621f 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  ..      /* This 
36220 73 74 72 61 74 65 67 79 20 69 6e 76 6f 6c 76 65  strategy involve
36221 73 20 61 20 74 77 6f 20 72 6f 77 69 64 20 6c 6f  s a two rowid lo
36222 6f 6b 75 70 73 20 6f 6e 20 61 6e 20 42 2d 54 72  okups on an B-Tr
36223 65 65 20 73 74 72 75 63 74 75 72 65 73 0a 20 20  ee structures.  
36224 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 6e 20      ** and then 
36225 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20  a linear search 
36226 6f 66 20 61 6e 20 52 2d 54 72 65 65 20 6e 6f 64  of an R-Tree nod
36227 65 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  e. This should b
36228 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73  e .      ** cons
36229 69 64 65 72 65 64 20 61 6c 6d 6f 73 74 20 61 73  idered almost as
3622a 20 71 75 69 63 6b 20 61 73 20 61 20 64 69 72 65   quick as a dire
3622b 63 74 20 72 6f 77 69 64 20 6c 6f 6f 6b 75 70 20  ct rowid lookup 
3622c 28 66 6f 72 20 77 68 69 63 68 20 0a 20 20 20 20  (for which .    
3622d 20 20 2a 2a 20 73 71 6c 69 74 65 20 75 73 65 73    ** sqlite uses
3622e 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 73   an internal cos
3622f 74 20 6f 66 20 30 2e 30 29 2e 0a 20 20 20 20 20  t of 0.0)..     
36230 20 2a 2f 20 0a 20 20 20 20 20 20 70 49 64 78 49   */ .      pIdxI
36231 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
36232 73 74 20 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20  st = 10.0;.     
36233 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
36234 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  K;.    }..    if
36235 28 20 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70  ( p->usable && p
36236 2d 3e 69 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20  ->iColumn>0 ){. 
36237 20 20 20 20 20 75 38 20 6f 70 20 3d 20 30 3b 0a       u8 op = 0;.
36238 20 20 20 20 20 20 73 77 69 74 63 68 28 20 70 2d        switch( p-
36239 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63  >op ){.        c
3623a 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ase SQLITE_INDEX
3623b 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 3a 20  _CONSTRAINT_EQ: 
3623c 6f 70 20 3d 20 52 54 52 45 45 5f 45 51 3b 20 62  op = RTREE_EQ; b
3623d 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
3623e 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  se SQLITE_INDEX_
3623f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 3a 20 6f  CONSTRAINT_GT: o
36240 70 20 3d 20 52 54 52 45 45 5f 47 54 3b 20 62 72  p = RTREE_GT; br
36241 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
36242 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
36243 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 3a 20 6f 70  ONSTRAINT_LE: op
36244 20 3d 20 52 54 52 45 45 5f 4c 45 3b 20 62 72 65   = RTREE_LE; bre
36245 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
36246 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
36247 4e 53 54 52 41 49 4e 54 5f 4c 54 3a 20 6f 70 20  NSTRAINT_LT: op 
36248 3d 20 52 54 52 45 45 5f 4c 54 3b 20 62 72 65 61  = RTREE_LT; brea
36249 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
3624a 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
3624b 53 54 52 41 49 4e 54 5f 47 45 3a 20 6f 70 20 3d  STRAINT_GE: op =
3624c 20 52 54 52 45 45 5f 47 45 3b 20 62 72 65 61 6b   RTREE_GE; break
3624d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3624e 69 66 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20  if( op ){.      
3624f 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
36250 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 63  his particular c
36251 6f 6e 73 74 72 61 69 6e 74 20 68 61 73 20 6e 6f  onstraint has no
36252 74 20 62 65 65 6e 20 75 73 65 64 20 62 65 66 6f  t been used befo
36253 72 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49  re..        ** I
36254 66 20 69 74 20 68 61 73 20 62 65 65 6e 20 75 73  f it has been us
36255 65 64 20 62 65 66 6f 72 65 2c 20 69 67 6e 6f 72  ed before, ignor
36256 65 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a  e it..        **
36257 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3c 3d  .        ** A <=
36258 20 6f 72 20 3c 20 63 61 6e 20 62 65 20 75 73 65   or < can be use
36259 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  d if there is a 
3625a 70 72 69 6f 72 20 3e 3d 20 6f 72 20 3e 2e 0a 20  prior >= or >.. 
3625b 20 20 20 20 20 20 20 2a 2a 20 41 20 3e 3d 20 6f         ** A >= o
3625c 72 20 3e 20 63 61 6e 20 62 65 20 75 73 65 64 20  r > can be used 
3625d 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 72  if there is a pr
3625e 69 6f 72 20 3c 20 6f 72 20 3c 3d 2e 0a 20 20 20  ior < or <=..   
3625f 20 20 20 20 20 2a 2a 20 41 20 3c 3d 20 6f 72 20       ** A <= or 
36260 3c 20 69 73 20 64 69 73 71 75 61 6c 69 66 69 65  < is disqualifie
36261 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  d if there is a 
36262 70 72 69 6f 72 20 3c 3d 2c 20 3c 2c 20 6f 72 20  prior <=, <, or 
36263 3d 3d 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41  ==..        ** A
36264 20 3e 3d 20 6f 72 20 3e 20 69 73 20 64 69 73 71   >= or > is disq
36265 75 61 6c 69 66 69 65 64 20 69 66 20 74 68 65 72  ualified if ther
36266 65 20 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 2c  e is a prior >=,
36267 20 3e 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20   >, or ==..     
36268 20 20 20 2a 2a 20 41 20 3d 3d 20 69 73 20 64 69     ** A == is di
36269 73 71 75 61 6c 69 66 65 64 20 69 66 20 74 68 65  squalifed if the
3626a 72 65 20 69 73 20 61 6e 79 20 70 72 69 6f 72 20  re is any prior 
3626b 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20 20  constraint..    
3626c 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
3626d 6e 74 20 6a 2c 20 6f 70 6d 73 6b 3b 0a 20 20 20  nt j, opmsk;.   
3626e 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
3626f 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
36270 63 6f 6d 70 61 74 69 62 6c 65 5b 5d 20 3d 20 7b  compatible[] = {
36271 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 32 2c 20   0, 0, 1, 1, 2, 
36272 32 20 7d 3b 0a 20 20 20 20 20 20 20 20 61 73 73  2 };.        ass
36273 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b  ert( compatible[
36274 52 54 52 45 45 5f 45 51 20 26 20 37 5d 3d 3d 30  RTREE_EQ & 7]==0
36275 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
36276 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52  rt( compatible[R
36277 54 52 45 45 5f 4c 54 20 26 20 37 5d 3d 3d 31 20  TREE_LT & 7]==1 
36278 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
36279 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54  t( compatible[RT
3627a 52 45 45 5f 4c 45 20 26 20 37 5d 3d 3d 31 20 29  REE_LE & 7]==1 )
3627b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
3627c 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52  ( compatible[RTR
3627d 45 45 5f 47 54 20 26 20 37 5d 3d 3d 32 20 29 3b  EE_GT & 7]==2 );
3627e 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
3627f 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45   compatible[RTRE
36280 45 5f 47 45 20 26 20 37 5d 3d 3d 32 20 29 3b 0a  E_GE & 7]==2 );.
36281 20 20 20 20 20 20 20 20 63 43 6f 6c 20 3d 20 70          cCol = p
36282 2d 3e 69 43 6f 6c 75 6d 6e 20 2d 20 31 20 2b 20  ->iColumn - 1 + 
36283 27 61 27 3b 0a 20 20 20 20 20 20 20 20 6f 70 6d  'a';.        opm
36284 73 6b 20 3d 20 63 6f 6d 70 61 74 69 62 6c 65 5b  sk = compatible[
36285 6f 70 20 26 20 37 5d 3b 0a 20 20 20 20 20 20 20  op & 7];.       
36286 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69 49 64 78   for(j=0; j<iIdx
36287 3b 20 6a 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20  ; j+=2){.       
36288 20 20 20 69 66 28 20 7a 49 64 78 53 74 72 5b 6a     if( zIdxStr[j
36289 2b 31 5d 3d 3d 63 43 6f 6c 20 26 26 20 28 63 6f  +1]==cCol && (co
3628a 6d 70 61 74 69 62 6c 65 5b 7a 49 64 78 53 74 72  mpatible[zIdxStr
3628b 5b 6a 5d 20 26 20 37 5d 20 26 20 6f 70 6d 73 6b  [j] & 7] & opmsk
3628c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
3628d 20 20 20 20 6f 70 20 3d 20 30 3b 0a 20 20 20 20      op = 0;.    
3628e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3628f 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
36290 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
36291 20 20 20 69 66 28 20 6f 70 20 29 7b 0a 20 20 20     if( op ){.   
36292 20 20 20 20 20 61 73 73 65 72 74 28 20 69 49 64       assert( iId
36293 78 3c 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72  x<sizeof(zIdxStr
36294 29 2d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 7a  )-1 );.        z
36295 49 64 78 53 74 72 5b 69 49 64 78 2b 2b 5d 20 3d  IdxStr[iIdx++] =
36296 20 6f 70 3b 0a 20 20 20 20 20 20 20 20 7a 49 64   op;.        zId
36297 78 53 74 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 63  xStr[iIdx++] = c
36298 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 64  Col;.        pId
36299 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
3629a 6e 74 55 73 61 67 65 5b 69 69 5d 2e 61 72 67 76  ntUsage[ii].argv
3629b 49 6e 64 65 78 20 3d 20 28 69 49 64 78 2f 32 29  Index = (iIdx/2)
3629c 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78 49 6e  ;.        pIdxIn
3629d 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55  fo->aConstraintU
3629e 73 61 67 65 5b 69 69 5d 2e 6f 6d 69 74 20 3d 20  sage[ii].omit = 
3629f 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
362a0 0a 20 20 7d 0a 0a 20 20 70 49 64 78 49 6e 66 6f  .  }..  pIdxInfo
362a1 2d 3e 69 64 78 4e 75 6d 20 3d 20 32 3b 0a 20 20  ->idxNum = 2;.  
362a2 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f  pIdxInfo->needTo
362a3 46 72 65 65 49 64 78 53 74 72 20 3d 20 31 3b 0a  FreeIdxStr = 1;.
362a4 20 20 69 66 28 20 69 49 64 78 3e 30 20 26 26 20    if( iIdx>0 && 
362a5 30 3d 3d 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64  0==(pIdxInfo->id
362a6 78 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  xStr = sqlite3_m
362a7 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a 49 64  printf("%s", zId
362a8 78 53 74 72 29 29 20 29 7b 0a 20 20 20 20 72 65  xStr)) ){.    re
362a9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
362aa 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  M;.  }.  assert(
362ab 20 69 49 64 78 3e 3d 30 20 29 3b 0a 20 20 70 49   iIdx>=0 );.  pI
362ac 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65  dxInfo->estimate
362ad 64 43 6f 73 74 20 3d 20 28 32 30 30 30 30 30 30  dCost = (2000000
362ae 2e 30 20 2f 20 28 64 6f 75 62 6c 65 29 28 69 49  .0 / (double)(iI
362af 64 78 20 2b 20 31 29 29 3b 0a 20 20 72 65 74 75  dx + 1));.  retu
362b0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
362b1 52 65 74 75 72 6e 20 74 68 65 20 4e 2d 64 69 6d  Return the N-dim
362b2 65 6e 73 69 6f 6e 61 6c 20 76 6f 6c 75 6d 6e 20  ensional volumn 
362b3 6f 66 20 74 68 65 20 63 65 6c 6c 20 73 74 6f 72  of the cell stor
362b4 65 64 20 69 6e 20 2a 70 2e 0a 2a 2f 0a 73 74 61  ed in *p..*/.sta
362b5 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 41 72  tic float cellAr
362b6 65 61 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ea(Rtree *pRtree
362b7 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 29 7b  , RtreeCell *p){
362b8 0a 20 20 66 6c 6f 61 74 20 61 72 65 61 20 3d 20  .  float area = 
362b9 31 2e 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  1.0;.  int ii;. 
362ba 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70   for(ii=0; ii<(p
362bb 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20  Rtree->nDim*2); 
362bc 69 69 2b 3d 32 29 7b 0a 20 20 20 20 61 72 65 61  ii+=2){.    area
362bd 20 3d 20 61 72 65 61 20 2a 20 28 44 43 4f 4f 52   = area * (DCOOR
362be 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31  D(p->aCoord[ii+1
362bf 5d 29 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61  ]) - DCOORD(p->a
362c0 43 6f 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d  Coord[ii]));.  }
362c1 0a 20 20 72 65 74 75 72 6e 20 61 72 65 61 3b 0a  .  return area;.
362c2 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
362c3 74 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74  the margin lengt
362c4 68 20 6f 66 20 63 65 6c 6c 20 70 2e 20 54 68 65  h of cell p. The
362c5 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 69   margin length i
362c6 73 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20  s the sum.** of 
362c7 74 68 65 20 6f 62 6a 65 63 74 73 20 73 69 7a 65  the objects size
362c8 20 69 6e 20 65 61 63 68 20 64 69 6d 65 6e 73 69   in each dimensi
362c9 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c  on..*/.static fl
362ca 6f 61 74 20 63 65 6c 6c 4d 61 72 67 69 6e 28 52  oat cellMargin(R
362cb 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
362cc 72 65 65 43 65 6c 6c 20 2a 70 29 7b 0a 20 20 66  reeCell *p){.  f
362cd 6c 6f 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e  loat margin = 0.
362ce 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66  0;.  int ii;.  f
362cf 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
362d0 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
362d1 2b 3d 32 29 7b 0a 20 20 20 20 6d 61 72 67 69 6e  +=2){.    margin
362d2 20 2b 3d 20 28 44 43 4f 4f 52 44 28 70 2d 3e 61   += (DCOORD(p->a
362d3 43 6f 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20 44  Coord[ii+1]) - D
362d4 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b  COORD(p->aCoord[
362d5 69 69 5d 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ii]));.  }.  ret
362d6 75 72 6e 20 6d 61 72 67 69 6e 3b 0a 7d 0a 0a 2f  urn margin;.}../
362d7 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 75  *.** Store the u
362d8 6e 69 6f 6e 20 6f 66 20 63 65 6c 6c 73 20 70 31  nion of cells p1
362d9 20 61 6e 64 20 70 32 20 69 6e 20 70 31 2e 0a 2a   and p2 in p1..*
362da 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 65  /.static void ce
362db 6c 6c 55 6e 69 6f 6e 28 52 74 72 65 65 20 2a 70  llUnion(Rtree *p
362dc 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
362dd 20 2a 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20   *p1, RtreeCell 
362de 2a 70 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  *p2){.  int ii;.
362df 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43    if( pRtree->eC
362e0 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f  oordType==RTREE_
362e1 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a  COORD_REAL32 ){.
362e2 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
362e3 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
362e4 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20  ); ii+=2){.     
362e5 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e   p1->aCoord[ii].
362e6 66 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f  f = MIN(p1->aCoo
362e7 72 64 5b 69 69 5d 2e 66 2c 20 70 32 2d 3e 61 43  rd[ii].f, p2->aC
362e8 6f 6f 72 64 5b 69 69 5d 2e 66 29 3b 0a 20 20 20  oord[ii].f);.   
362e9 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69     p1->aCoord[ii
362ea 2b 31 5d 2e 66 20 3d 20 4d 41 58 28 70 31 2d 3e  +1].f = MAX(p1->
362eb 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 2c 20  aCoord[ii+1].f, 
362ec 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  p2->aCoord[ii+1]
362ed 2e 66 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  .f);.    }.  }el
362ee 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30  se{.    for(ii=0
362ef 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44  ; ii<(pRtree->nD
362f0 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20  im*2); ii+=2){. 
362f1 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b       p1->aCoord[
362f2 69 69 5d 2e 69 20 3d 20 4d 49 4e 28 70 31 2d 3e  ii].i = MIN(p1->
362f3 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 2c 20 70 32  aCoord[ii].i, p2
362f4 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 29 3b  ->aCoord[ii].i);
362f5 0a 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72  .      p1->aCoor
362f6 64 5b 69 69 2b 31 5d 2e 69 20 3d 20 4d 41 58 28  d[ii+1].i = MAX(
362f7 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  p1->aCoord[ii+1]
362f8 2e 69 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69  .i, p2->aCoord[i
362f9 69 2b 31 5d 2e 69 29 3b 0a 20 20 20 20 7d 0a 20  i+1].i);.    }. 
362fa 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
362fb 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 61  rn true if the a
362fc 72 65 61 20 63 6f 76 65 72 65 64 20 62 79 20 70  rea covered by p
362fd 32 20 69 73 20 61 20 73 75 62 73 65 74 20 6f 66  2 is a subset of
362fe 20 74 68 65 20 61 72 65 61 20 63 6f 76 65 72 65   the area covere
362ff 64 0a 2a 2a 20 62 79 20 70 31 2e 20 46 61 6c 73  d.** by p1. Fals
36300 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a  e otherwise..*/.
36301 73 74 61 74 69 63 20 69 6e 74 20 63 65 6c 6c 43  static int cellC
36302 6f 6e 74 61 69 6e 73 28 52 74 72 65 65 20 2a 70  ontains(Rtree *p
36303 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
36304 20 2a 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20   *p1, RtreeCell 
36305 2a 70 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  *p2){.  int ii;.
36306 20 20 69 6e 74 20 69 73 49 6e 74 20 3d 20 28 70    int isInt = (p
36307 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70  Rtree->eCoordTyp
36308 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49  e==RTREE_COORD_I
36309 4e 54 33 32 29 3b 0a 20 20 66 6f 72 28 69 69 3d  NT32);.  for(ii=
3630a 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e  0; ii<(pRtree->n
3630b 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a  Dim*2); ii+=2){.
3630c 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a      RtreeCoord *
3630d 61 31 20 3d 20 26 70 31 2d 3e 61 43 6f 6f 72 64  a1 = &p1->aCoord
3630e 5b 69 69 5d 3b 0a 20 20 20 20 52 74 72 65 65 43  [ii];.    RtreeC
3630f 6f 6f 72 64 20 2a 61 32 20 3d 20 26 70 32 2d 3e  oord *a2 = &p2->
36310 61 43 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20  aCoord[ii];.    
36311 69 66 28 20 28 21 69 73 49 6e 74 20 26 26 20 28  if( (!isInt && (
36312 61 32 5b 30 5d 2e 66 3c 61 31 5b 30 5d 2e 66 20  a2[0].f<a1[0].f 
36313 7c 7c 20 61 32 5b 31 5d 2e 66 3e 61 31 5b 31 5d  || a2[1].f>a1[1]
36314 2e 66 29 29 20 0a 20 20 20 20 20 7c 7c 20 28 20  .f)) .     || ( 
36315 69 73 49 6e 74 20 26 26 20 28 61 32 5b 30 5d 2e  isInt && (a2[0].
36316 69 3c 61 31 5b 30 5d 2e 69 20 7c 7c 20 61 32 5b  i<a1[0].i || a2[
36317 31 5d 2e 69 3e 61 31 5b 31 5d 2e 69 29 29 20 0a  1].i>a1[1].i)) .
36318 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74      ){.      ret
36319 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
3631a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
3631b 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
3631c 20 61 6d 6f 75 6e 74 20 63 65 6c 6c 20 70 20 77   amount cell p w
3631d 6f 75 6c 64 20 67 72 6f 77 20 62 79 20 69 66 20  ould grow by if 
3631e 69 74 20 77 65 72 65 20 75 6e 69 6f 6e 65 64 20  it were unioned 
3631f 77 69 74 68 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73  with pCell..*/.s
36320 74 61 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c  tatic float cell
36321 47 72 6f 77 74 68 28 52 74 72 65 65 20 2a 70 52  Growth(Rtree *pR
36322 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20  tree, RtreeCell 
36323 2a 70 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  *p, RtreeCell *p
36324 43 65 6c 6c 29 7b 0a 20 20 66 6c 6f 61 74 20 61  Cell){.  float a
36325 72 65 61 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c  rea;.  RtreeCell
36326 20 63 65 6c 6c 3b 0a 20 20 6d 65 6d 63 70 79 28   cell;.  memcpy(
36327 26 63 65 6c 6c 2c 20 70 2c 20 73 69 7a 65 6f 66  &cell, p, sizeof
36328 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
36329 61 72 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28  area = cellArea(
3632a 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29 3b 0a  pRtree, &cell);.
3632b 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72    cellUnion(pRtr
3632c 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c  ee, &cell, pCell
3632d 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 63 65 6c  );.  return (cel
3632e 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63  lArea(pRtree, &c
3632f 65 6c 6c 29 2d 61 72 65 61 29 3b 0a 7d 0a 0a 23  ell)-area);.}..#
36330 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
36331 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52  TREE_CHOOSESUBTR
36332 45 45 20 7c 7c 20 56 41 52 49 41 4e 54 5f 52 53  EE || VARIANT_RS
36333 54 41 52 54 52 45 45 5f 53 50 4c 49 54 0a 73 74  TARTREE_SPLIT.st
36334 61 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4f  atic float cellO
36335 76 65 72 6c 61 70 28 0a 20 20 52 74 72 65 65 20  verlap(.  Rtree 
36336 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
36337 65 43 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72  eCell *p, .  Rtr
36338 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a  eeCell *aCell, .
36339 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20    int nCell, .  
3633a 69 6e 74 20 69 45 78 63 6c 75 64 65 0a 29 7b 0a  int iExclude.){.
3633b 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6c 6f 61    int ii;.  floa
3633c 74 20 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b  t overlap = 0.0;
3633d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
3633e 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20  nCell; ii++){.  
3633f 20 20 69 66 28 20 69 69 21 3d 69 45 78 63 6c 75    if( ii!=iExclu
36340 64 65 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  de ){.      int 
36341 6a 6a 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  jj;.      float 
36342 6f 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 66  o = 1.0;.      f
36343 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 28 70 52 74  or(jj=0; jj<(pRt
36344 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 6a 6a  ree->nDim*2); jj
36345 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 64 6f  +=2){.        do
36346 75 62 6c 65 20 78 31 3b 0a 20 20 20 20 20 20 20  uble x1;.       
36347 20 64 6f 75 62 6c 65 20 78 32 3b 0a 0a 20 20 20   double x2;..   
36348 20 20 20 20 20 78 31 20 3d 20 4d 41 58 28 44 43       x1 = MAX(DC
36349 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a  OORD(p->aCoord[j
3634a 6a 5d 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c  j]), DCOORD(aCel
3634b 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 5d  l[ii].aCoord[jj]
3634c 29 29 3b 0a 20 20 20 20 20 20 20 20 78 32 20 3d  ));.        x2 =
3634d 20 4d 49 4e 28 44 43 4f 4f 52 44 28 70 2d 3e 61   MIN(DCOORD(p->a
3634e 43 6f 6f 72 64 5b 6a 6a 2b 31 5d 29 2c 20 44 43  Coord[jj+1]), DC
3634f 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61  OORD(aCell[ii].a
36350 43 6f 6f 72 64 5b 6a 6a 2b 31 5d 29 29 3b 0a 0a  Coord[jj+1]));..
36351 20 20 20 20 20 20 20 20 69 66 28 20 78 32 3c 78          if( x2<x
36352 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f  1 ){.          o
36353 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20 20 20   = 0.0;.        
36354 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
36355 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
36356 20 20 6f 20 3d 20 6f 20 2a 20 28 78 32 2d 78 31    o = o * (x2-x1
36357 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
36358 20 20 20 7d 0a 20 20 20 20 20 20 6f 76 65 72 6c     }.      overl
36359 61 70 20 2b 3d 20 6f 3b 0a 20 20 20 20 7d 0a 20  ap += o;.    }. 
3635a 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f 76 65 72   }.  return over
3635b 6c 61 70 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  lap;.}.#endif..#
3635c 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
3635d 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52  TREE_CHOOSESUBTR
3635e 45 45 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20  EE.static float 
3635f 63 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72  cellOverlapEnlar
36360 67 65 6d 65 6e 74 28 0a 20 20 52 74 72 65 65 20  gement(.  Rtree 
36361 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
36362 65 43 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72  eCell *p, .  Rtr
36363 65 65 43 65 6c 6c 20 2a 70 49 6e 73 65 72 74 2c  eeCell *pInsert,
36364 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61   .  RtreeCell *a
36365 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
36366 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c  ll, .  int iExcl
36367 75 64 65 0a 29 7b 0a 20 20 66 6c 6f 61 74 20 62  ude.){.  float b
36368 65 66 6f 72 65 3b 0a 20 20 66 6c 6f 61 74 20 61  efore;.  float a
36369 66 74 65 72 3b 0a 20 20 62 65 66 6f 72 65 20 3d  fter;.  before =
3636a 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74   cellOverlap(pRt
3636b 72 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e  ree, p, aCell, n
3636c 43 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b  Cell, iExclude);
3636d 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74  .  cellUnion(pRt
3636e 72 65 65 2c 20 70 2c 20 70 49 6e 73 65 72 74 29  ree, p, pInsert)
3636f 3b 0a 20 20 61 66 74 65 72 20 3d 20 63 65 6c 6c  ;.  after = cell
36370 4f 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20  Overlap(pRtree, 
36371 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c  p, aCell, nCell,
36372 20 69 45 78 63 6c 75 64 65 29 3b 0a 20 20 72 65   iExclude);.  re
36373 74 75 72 6e 20 61 66 74 65 72 2d 62 65 66 6f 72  turn after-befor
36374 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  e;.}.#endif.../*
36375 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
36376 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  n implements the
36377 20 43 68 6f 6f 73 65 4c 65 61 66 20 61 6c 67 6f   ChooseLeaf algo
36378 72 69 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61  rithm from Gutma
36379 6e 5b 38 34 5d 2e 0a 2a 2a 20 43 68 6f 6f 73 65  n[84]..** Choose
3637a 53 75 62 54 72 65 65 20 69 6e 20 72 2a 74 72 65  SubTree in r*tre
3637b 65 20 74 65 72 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a  e terminology..*
3637c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 43 68 6f  /.static int Cho
3637d 6f 73 65 4c 65 61 66 28 0a 20 20 52 74 72 65 65  oseLeaf(.  Rtree
3637e 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20   *pRtree,       
3637f 20 20 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65          /* Rtree
36380 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65   table */.  Rtre
36381 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 20 20  eCell *pCell,   
36382 20 20 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c           /* Cell
36383 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20   to insert into 
36384 72 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69  rtree */.  int i
36385 48 65 69 67 68 74 2c 20 20 20 20 20 20 20 20 20  Height,         
36386 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68          /* Heigh
36387 74 20 6f 66 20 73 75 62 2d 74 72 65 65 20 72 6f  t of sub-tree ro
36388 6f 74 65 64 20 61 74 20 70 43 65 6c 6c 20 2a 2f  oted at pCell */
36389 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  .  RtreeNode **p
3638a 70 4c 65 61 66 20 20 20 20 20 20 20 20 20 20 20  pLeaf           
3638b 2f 2a 20 4f 55 54 3a 20 53 65 6c 65 63 74 65 64  /* OUT: Selected
3638c 20 6c 65 61 66 20 70 61 67 65 20 2a 2f 0a 29 7b   leaf page */.){
3638d 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
3638e 20 69 69 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65   ii;.  RtreeNode
3638f 20 2a 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20   *pNode;.  rc = 
36390 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
36391 65 65 2c 20 31 2c 20 30 2c 20 26 70 4e 6f 64 65  ee, 1, 0, &pNode
36392 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
36393 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
36394 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 69 44 65   ii<(pRtree->iDe
36395 70 74 68 2d 69 48 65 69 67 68 74 29 3b 20 69 69  pth-iHeight); ii
36396 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 65  ++){.    int iCe
36397 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ll;.    sqlite3_
36398 69 6e 74 36 34 20 69 42 65 73 74 3b 0a 0a 20 20  int64 iBest;..  
36399 20 20 66 6c 6f 61 74 20 66 4d 69 6e 47 72 6f 77    float fMinGrow
3639a 74 68 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d  th;.    float fM
3639b 69 6e 41 72 65 61 3b 0a 20 20 20 20 66 6c 6f 61  inArea;.    floa
3639c 74 20 66 4d 69 6e 4f 76 65 72 6c 61 70 3b 0a 0a  t fMinOverlap;..
3639d 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20      int nCell = 
3639e 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20  NCELL(pNode);.  
3639f 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c    RtreeCell cell
363a0 3b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  ;.    RtreeNode 
363a1 2a 70 43 68 69 6c 64 3b 0a 0a 20 20 20 20 52 74  *pChild;..    Rt
363a2 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 20 3d  reeCell *aCell =
363a3 20 30 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54   0;..#if VARIANT
363a4 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53  _RSTARTREE_CHOOS
363a5 45 53 55 42 54 52 45 45 0a 20 20 20 20 69 66 28  ESUBTREE.    if(
363a6 20 69 69 3d 3d 28 70 52 74 72 65 65 2d 3e 69 44   ii==(pRtree->iD
363a7 65 70 74 68 2d 31 29 20 29 7b 0a 20 20 20 20 20  epth-1) ){.     
363a8 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 61   int jj;.      a
363a9 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Cell = sqlite3_m
363aa 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72  alloc(sizeof(Rtr
363ab 65 65 43 65 6c 6c 29 2a 6e 43 65 6c 6c 29 3b 0a  eeCell)*nCell);.
363ac 20 20 20 20 20 20 69 66 28 20 21 61 43 65 6c 6c        if( !aCell
363ad 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
363ae 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
363af 20 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61         nodeRelea
363b0 73 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  se(pRtree, pNode
363b1 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f 64 65  );.        pNode
363b2 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f   = 0;.        co
363b3 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a  ntinue;.      }.
363b4 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20        for(jj=0; 
363b5 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b  jj<nCell; jj++){
363b6 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 47 65 74  .        nodeGet
363b7 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f  Cell(pRtree, pNo
363b8 64 65 2c 20 6a 6a 2c 20 26 61 43 65 6c 6c 5b 6a  de, jj, &aCell[j
363b9 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  j]);.      }.   
363ba 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   }.#endif..    /
363bb 2a 20 53 65 6c 65 63 74 20 74 68 65 20 63 68 69  * Select the chi
363bc 6c 64 20 6e 6f 64 65 20 77 68 69 63 68 20 77 69  ld node which wi
363bd 6c 6c 20 62 65 20 65 6e 6c 61 72 67 65 64 20 74  ll be enlarged t
363be 68 65 20 6c 65 61 73 74 20 69 66 20 70 43 65 6c  he least if pCel
363bf 6c 0a 20 20 20 20 2a 2a 20 69 73 20 69 6e 73 65  l.    ** is inse
363c0 72 74 65 64 20 69 6e 74 6f 20 69 74 2e 20 52 65  rted into it. Re
363c1 73 6f 6c 76 65 20 74 69 65 73 20 62 79 20 63 68  solve ties by ch
363c2 6f 6f 73 69 6e 67 20 74 68 65 20 65 6e 74 72 79  oosing the entry
363c3 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65   with.    ** the
363c4 20 73 6d 61 6c 6c 65 73 74 20 61 72 65 61 2e 0a   smallest area..
363c5 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
363c6 43 65 6c 6c 3d 30 3b 20 69 43 65 6c 6c 3c 6e 43  Cell=0; iCell<nC
363c7 65 6c 6c 3b 20 69 43 65 6c 6c 2b 2b 29 7b 0a 20  ell; iCell++){. 
363c8 20 20 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74       float growt
363c9 68 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 61  h;.      float a
363ca 72 65 61 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  rea;.      float
363cb 20 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0a   overlap = 0.0;.
363cc 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c        nodeGetCel
363cd 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
363ce 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a   iCell, &cell);.
363cf 20 20 20 20 20 20 67 72 6f 77 74 68 20 3d 20 63        growth = c
363d0 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65  ellGrowth(pRtree
363d1 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b  , &cell, pCell);
363d2 0a 20 20 20 20 20 20 61 72 65 61 20 3d 20 63 65  .      area = ce
363d3 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26  llArea(pRtree, &
363d4 63 65 6c 6c 29 3b 0a 23 69 66 20 56 41 52 49 41  cell);.#if VARIA
363d5 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f  NT_RSTARTREE_CHO
363d6 4f 53 45 53 55 42 54 52 45 45 0a 20 20 20 20 20  OSESUBTREE.     
363d7 20 69 66 28 20 69 69 3d 3d 28 70 52 74 72 65 65   if( ii==(pRtree
363d8 2d 3e 69 44 65 70 74 68 2d 31 29 20 29 7b 0a 20  ->iDepth-1) ){. 
363d9 20 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d         overlap =
363da 20 63 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61   cellOverlapEnla
363db 72 67 65 6d 65 6e 74 28 70 52 74 72 65 65 2c 26  rgement(pRtree,&
363dc 63 65 6c 6c 2c 70 43 65 6c 6c 2c 61 43 65 6c 6c  cell,pCell,aCell
363dd 2c 6e 43 65 6c 6c 2c 69 43 65 6c 6c 29 3b 0a 20  ,nCell,iCell);. 
363de 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
363df 20 20 20 20 69 66 28 20 28 69 43 65 6c 6c 3d 3d      if( (iCell==
363e0 30 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f  0) .       || (o
363e1 76 65 72 6c 61 70 3c 66 4d 69 6e 4f 76 65 72 6c  verlap<fMinOverl
363e2 61 70 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28  ap) .       || (
363e3 6f 76 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76 65  overlap==fMinOve
363e4 72 6c 61 70 20 26 26 20 67 72 6f 77 74 68 3c 66  rlap && growth<f
363e5 4d 69 6e 47 72 6f 77 74 68 29 0a 20 20 20 20 20  MinGrowth).     
363e6 20 20 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66    || (overlap==f
363e7 4d 69 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72  MinOverlap && gr
363e8 6f 77 74 68 3d 3d 66 4d 69 6e 47 72 6f 77 74 68  owth==fMinGrowth
363e9 20 26 26 20 61 72 65 61 3c 66 4d 69 6e 41 72 65   && area<fMinAre
363ea 61 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  a).      ){.    
363eb 20 20 20 20 66 4d 69 6e 4f 76 65 72 6c 61 70 20      fMinOverlap 
363ec 3d 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20  = overlap;.     
363ed 20 20 20 66 4d 69 6e 47 72 6f 77 74 68 20 3d 20     fMinGrowth = 
363ee 67 72 6f 77 74 68 3b 0a 20 20 20 20 20 20 20 20  growth;.        
363ef 66 4d 69 6e 41 72 65 61 20 3d 20 61 72 65 61 3b  fMinArea = area;
363f0 0a 20 20 20 20 20 20 20 20 69 42 65 73 74 20 3d  .        iBest =
363f1 20 63 65 6c 6c 2e 69 52 6f 77 69 64 3b 0a 20 20   cell.iRowid;.  
363f2 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
363f3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43   sqlite3_free(aC
363f4 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e  ell);.    rc = n
363f5 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65  odeAcquire(pRtre
363f6 65 2c 20 69 42 65 73 74 2c 20 70 4e 6f 64 65 2c  e, iBest, pNode,
363f7 20 26 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 6e   &pChild);.    n
363f8 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
363f9 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70  e, pNode);.    p
363fa 4e 6f 64 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20  Node = pChild;. 
363fb 20 7d 0a 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20   }..  *ppLeaf = 
363fc 70 4e 6f 64 65 3b 0a 20 20 72 65 74 75 72 6e 20  pNode;.  return 
363fd 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 63  rc;.}../*.** A c
363fe 65 6c 6c 20 77 69 74 68 20 74 68 65 20 73 61 6d  ell with the sam
363ff 65 20 63 6f 6e 74 65 6e 74 20 61 73 20 70 43 65  e content as pCe
36400 6c 6c 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ll has just been
36401 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a   inserted into.*
36402 2a 20 74 68 65 20 6e 6f 64 65 20 70 4e 6f 64 65  * the node pNode
36403 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
36404 75 70 64 61 74 65 73 20 74 68 65 20 62 6f 75 6e  updates the boun
36405 64 69 6e 67 20 62 6f 78 20 63 65 6c 6c 73 20 69  ding box cells i
36406 6e 0a 2a 2a 20 61 6c 6c 20 61 6e 63 65 73 74 6f  n.** all ancesto
36407 72 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  r elements..*/.s
36408 74 61 74 69 63 20 76 6f 69 64 20 41 64 6a 75 73  tatic void Adjus
36409 74 54 72 65 65 28 0a 20 20 52 74 72 65 65 20 2a  tTree(.  Rtree *
3640a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pRtree,         
3640b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 74             /* Rt
3640c 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52  ree table */.  R
3640d 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
3640e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3640f 20 2f 2a 20 41 64 6a 75 73 74 20 61 6e 63 65 73   /* Adjust ances
36410 74 72 79 20 6f 66 20 74 68 69 73 20 6e 6f 64 65  try of this node
36411 2e 20 2a 2f 0a 20 20 52 74 72 65 65 43 65 6c 6c  . */.  RtreeCell
36412 20 2a 70 43 65 6c 6c 20 20 20 20 20 20 20 20 20   *pCell         
36413 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
36414 20 63 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69   cell was just i
36415 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20  nserted */.){.  
36416 52 74 72 65 65 4e 6f 64 65 20 2a 70 20 3d 20 70  RtreeNode *p = p
36417 4e 6f 64 65 3b 0a 20 20 77 68 69 6c 65 28 20 70  Node;.  while( p
36418 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20  ->pParent ){.   
36419 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
3641a 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
3641b 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50 61  pParent = p->pPa
3641c 72 65 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 43  rent;.    int iC
3641d 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74  ell = nodeParent
3641e 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 29  Index(pRtree, p)
3641f 3b 0a 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65  ;..    nodeGetCe
36420 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ll(pRtree, pPare
36421 6e 74 2c 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c  nt, iCell, &cell
36422 29 3b 0a 20 20 20 20 69 66 28 20 21 63 65 6c 6c  );.    if( !cell
36423 43 6f 6e 74 61 69 6e 73 28 70 52 74 72 65 65 2c  Contains(pRtree,
36424 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 20 29   &cell, pCell) )
36425 7b 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f  {.      cellUnio
36426 6e 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c  n(pRtree, &cell,
36427 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 6e   pCell);.      n
36428 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c  odeOverwriteCell
36429 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74  (pRtree, pParent
3642a 2c 20 26 63 65 6c 6c 2c 20 69 43 65 6c 6c 29 3b  , &cell, iCell);
3642b 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 70 20 3d  .    }. .    p =
3642c 20 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 7d 0a   pParent;.  }.}.
3642d 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70  ./*.** Write map
3642e 70 69 6e 67 20 28 69 52 6f 77 69 64 2d 3e 69 4e  ping (iRowid->iN
3642f 6f 64 65 29 20 74 6f 20 74 68 65 20 3c 72 74 72  ode) to the <rtr
36430 65 65 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e  ee>_rowid table.
36431 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
36432 6f 77 69 64 57 72 69 74 65 28 52 74 72 65 65 20  owidWrite(Rtree 
36433 2a 70 52 74 72 65 65 2c 20 73 71 6c 69 74 65 33  *pRtree, sqlite3
36434 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 2c 20 73  _int64 iRowid, s
36435 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f  qlite3_int64 iNo
36436 64 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  de){.  sqlite3_b
36437 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
36438 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20 31  ->pWriteRowid, 1
36439 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c  , iRowid);.  sql
3643a 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
3643b 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
3643c 77 69 64 2c 20 32 2c 20 69 4e 6f 64 65 29 3b 0a  wid, 2, iNode);.
3643d 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
3643e 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
3643f 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  id);.  return sq
36440 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
36441 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29  ee->pWriteRowid)
36442 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
36443 20 6d 61 70 70 69 6e 67 20 28 69 4e 6f 64 65 2d   mapping (iNode-
36444 3e 69 50 61 72 29 20 74 6f 20 74 68 65 20 3c 72  >iPar) to the <r
36445 74 72 65 65 3e 5f 70 61 72 65 6e 74 20 74 61 62  tree>_parent tab
36446 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
36447 74 20 70 61 72 65 6e 74 57 72 69 74 65 28 52 74  t parentWrite(Rt
36448 72 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c  ree *pRtree, sql
36449 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65  ite3_int64 iNode
3644a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
3644b 69 50 61 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  iPar){.  sqlite3
3644c 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
3644d 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
3644e 2c 20 31 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 73  , 1, iNode);.  s
3644f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
36450 34 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  4(pRtree->pWrite
36451 50 61 72 65 6e 74 2c 20 32 2c 20 69 50 61 72 29  Parent, 2, iPar)
36452 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ;.  sqlite3_step
36453 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  (pRtree->pWriteP
36454 61 72 65 6e 74 29 3b 0a 20 20 72 65 74 75 72 6e  arent);.  return
36455 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
36456 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72  Rtree->pWritePar
36457 65 6e 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  ent);.}..static 
36458 69 6e 74 20 72 74 72 65 65 49 6e 73 65 72 74 43  int rtreeInsertC
36459 65 6c 6c 28 52 74 72 65 65 20 2a 2c 20 52 74 72  ell(Rtree *, Rtr
3645a 65 65 4e 6f 64 65 20 2a 2c 20 52 74 72 65 65 43  eeNode *, RtreeC
3645b 65 6c 6c 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69  ell *, int);..#i
3645c 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
3645d 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 0a 2f  N_LINEAR_SPLIT./
3645e 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3645f 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6e 65 61  ion of the linea
36460 72 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65  r variant of the
36461 20 50 69 63 6b 4e 65 78 74 28 29 20 66 75 6e 63   PickNext() func
36462 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74  tion from.** Gut
36463 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61  tman[84]..*/.sta
36464 74 69 63 20 52 74 72 65 65 43 65 6c 6c 20 2a 4c  tic RtreeCell *L
36465 69 6e 65 61 72 50 69 63 6b 4e 65 78 74 28 0a 20  inearPickNext(. 
36466 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a   Rtree *pRtree,.
36467 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
36468 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
36469 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  , .  RtreeCell *
3646a 70 4c 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72  pLeftBox, .  Rtr
3646b 65 65 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f  eeCell *pRightBo
3646c 78 2c 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64  x,.  int *aiUsed
3646d 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  .){.  int ii;.  
3646e 66 6f 72 28 69 69 3d 30 3b 20 61 69 55 73 65 64  for(ii=0; aiUsed
3646f 5b 69 69 5d 3b 20 69 69 2b 2b 29 3b 0a 20 20 61  [ii]; ii++);.  a
36470 69 55 73 65 64 5b 69 69 5d 20 3d 20 31 3b 0a 20  iUsed[ii] = 1;. 
36471 20 72 65 74 75 72 6e 20 26 61 43 65 6c 6c 5b 69   return &aCell[i
36472 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  i];.}../*.** Imp
36473 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
36474 68 65 20 6c 69 6e 65 61 72 20 76 61 72 69 61 6e  he linear varian
36475 74 20 6f 66 20 74 68 65 20 50 69 63 6b 53 65 65  t of the PickSee
36476 64 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 72  ds() function fr
36477 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34  om.** Guttman[84
36478 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ]..*/.static voi
36479 64 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64  d LinearPickSeed
3647a 73 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  s(.  Rtree *pRtr
3647b 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
3647c 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
3647d 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69  Cell, .  int *pi
3647e 4c 65 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74  LeftSeed, .  int
3647f 20 2a 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b   *piRightSeed.){
36480 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
36481 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20  iLeftSeed = 0;. 
36482 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64 20   int iRightSeed 
36483 3d 20 31 3b 0a 20 20 66 6c 6f 61 74 20 6d 61 78  = 1;.  float max
36484 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68  NormalInnerWidth
36485 20 3d 20 30 2e 30 3b 0a 0a 20 20 2f 2a 20 50 69   = 0.0;..  /* Pi
36486 63 6b 20 74 77 6f 20 22 73 65 65 64 22 20 63 65  ck two "seed" ce
36487 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 61 72 72  lls from the arr
36488 61 79 20 6f 66 20 63 65 6c 6c 73 2e 20 54 68 65  ay of cells. The
36489 20 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 0a   algorithm used.
3648a 20 20 2a 2a 20 68 65 72 65 20 69 73 20 74 68 65    ** here is the
3648b 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73   LinearPickSeeds
3648c 20 61 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20   algorithm from 
3648d 47 75 74 6d 61 6e 5b 31 39 38 34 5d 2e 20 54 68  Gutman[1984]. Th
3648e 65 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20  e .  ** indices 
3648f 6f 66 20 74 68 65 20 74 77 6f 20 73 65 65 64 20  of the two seed 
36490 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 61 72 72  cells in the arr
36491 61 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e  ay are stored in
36492 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20 76 61 72 69   local.  ** vari
36493 61 62 6c 65 73 20 69 4c 65 66 74 53 65 65 6b 20  ables iLeftSeek 
36494 61 6e 64 20 69 52 69 67 68 74 53 65 65 64 2e 0a  and iRightSeed..
36495 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
36496 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20  i<pRtree->nDim; 
36497 69 2b 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20  i++){.    float 
36498 78 31 20 3d 20 61 43 65 6c 6c 5b 30 5d 2e 61 43  x1 = aCell[0].aC
36499 6f 6f 72 64 5b 69 2a 32 5d 3b 0a 20 20 20 20 66  oord[i*2];.    f
3649a 6c 6f 61 74 20 78 32 20 3d 20 61 43 65 6c 6c 5b  loat x2 = aCell[
3649b 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d  0].aCoord[i*2+1]
3649c 3b 0a 20 20 20 20 66 6c 6f 61 74 20 78 33 20 3d  ;.    float x3 =
3649d 20 78 31 3b 0a 20 20 20 20 66 6c 6f 61 74 20 78   x1;.    float x
3649e 34 20 3d 20 78 32 3b 0a 20 20 20 20 69 6e 74 20  4 = x2;.    int 
3649f 6a 6a 3b 0a 0a 20 20 20 20 69 6e 74 20 69 43 65  jj;..    int iCe
364a0 6c 6c 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20  llLeft = 0;.    
364a1 69 6e 74 20 69 43 65 6c 6c 52 69 67 68 74 20 3d  int iCellRight =
364a2 20 30 3b 0a 0a 20 20 20 20 66 6f 72 28 6a 6a 3d   0;..    for(jj=
364a3 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b  1; jj<nCell; jj+
364a4 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  +){.      float 
364a5 6c 65 66 74 20 3d 20 61 43 65 6c 6c 5b 6a 6a 5d  left = aCell[jj]
364a6 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d 3b 0a 20 20  .aCoord[i*2];.  
364a7 20 20 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20      float right 
364a8 3d 20 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f 6f  = aCell[jj].aCoo
364a9 72 64 5b 69 2a 32 2b 31 5d 3b 0a 0a 20 20 20 20  rd[i*2+1];..    
364aa 20 20 69 66 28 20 6c 65 66 74 3c 78 31 20 29 20    if( left<x1 ) 
364ab 78 31 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20  x1 = left;.     
364ac 20 69 66 28 20 72 69 67 68 74 3e 78 34 20 29 20   if( right>x4 ) 
364ad 78 34 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20  x4 = right;.    
364ae 20 20 69 66 28 20 6c 65 66 74 3e 78 33 20 29 7b    if( left>x3 ){
364af 0a 20 20 20 20 20 20 20 20 78 33 20 3d 20 6c 65  .        x3 = le
364b0 66 74 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c  ft;.        iCel
364b1 6c 52 69 67 68 74 20 3d 20 6a 6a 3b 0a 20 20 20  lRight = jj;.   
364b2 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72     }.      if( r
364b3 69 67 68 74 3c 78 32 20 29 7b 0a 20 20 20 20 20  ight<x2 ){.     
364b4 20 20 20 78 32 20 3d 20 72 69 67 68 74 3b 0a 20     x2 = right;. 
364b5 20 20 20 20 20 20 20 69 43 65 6c 6c 4c 65 66 74         iCellLeft
364b6 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a 20   = jj;.      }. 
364b7 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 78 34     }..    if( x4
364b8 21 3d 78 31 20 29 7b 0a 20 20 20 20 20 20 66 6c  !=x1 ){.      fl
364b9 6f 61 74 20 6e 6f 72 6d 61 6c 77 69 64 74 68 20  oat normalwidth 
364ba 3d 20 28 78 33 20 2d 20 78 32 29 20 2f 20 28 78  = (x3 - x2) / (x
364bb 34 20 2d 20 78 31 29 3b 0a 20 20 20 20 20 20 69  4 - x1);.      i
364bc 66 28 20 6e 6f 72 6d 61 6c 77 69 64 74 68 3e 6d  f( normalwidth>m
364bd 61 78 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64  axNormalInnerWid
364be 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c  th ){.        iL
364bf 65 66 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 4c  eftSeed = iCellL
364c0 65 66 74 3b 0a 20 20 20 20 20 20 20 20 69 52 69  eft;.        iRi
364c1 67 68 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 52  ghtSeed = iCellR
364c2 69 67 68 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ight;.      }.  
364c3 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c 65    }.  }..  *piLe
364c4 66 74 53 65 65 64 20 3d 20 69 4c 65 66 74 53 65  ftSeed = iLeftSe
364c5 65 64 3b 0a 20 20 2a 70 69 52 69 67 68 74 53 65  ed;.  *piRightSe
364c6 65 64 20 3d 20 69 52 69 67 68 74 53 65 65 64 3b  ed = iRightSeed;
364c7 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41 52  .}.#endif /* VAR
364c8 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
364c9 45 41 52 5f 53 50 4c 49 54 20 2a 2f 0a 0a 23 69  EAR_SPLIT */..#i
364ca 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
364cb 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49  N_QUADRATIC_SPLI
364cc 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  T./*.** Implemen
364cd 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75  tation of the qu
364ce 61 64 72 61 74 69 63 20 76 61 72 69 61 6e 74 20  adratic variant 
364cf 6f 66 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28  of the PickNext(
364d0 29 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a  ) function from.
364d1 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a  ** Guttman[84]..
364d2 2a 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 43  */.static RtreeC
364d3 65 6c 6c 20 2a 51 75 61 64 72 61 74 69 63 50 69  ell *QuadraticPi
364d4 63 6b 4e 65 78 74 28 0a 20 20 52 74 72 65 65 20  ckNext(.  Rtree 
364d5 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
364d6 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
364d7 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 52 74  int nCell, .  Rt
364d8 72 65 65 43 65 6c 6c 20 2a 70 4c 65 66 74 42 6f  reeCell *pLeftBo
364d9 78 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  x, .  RtreeCell 
364da 2a 70 52 69 67 68 74 42 6f 78 2c 0a 20 20 69 6e  *pRightBox,.  in
364db 74 20 2a 61 69 55 73 65 64 0a 29 7b 0a 20 20 23  t *aiUsed.){.  #
364dc 64 65 66 69 6e 65 20 46 41 42 53 28 61 29 20 28  define FABS(a) (
364dd 28 61 29 3c 30 2e 30 3f 2d 31 2e 30 2a 28 61 29  (a)<0.0?-1.0*(a)
364de 3a 28 61 29 29 0a 0a 20 20 69 6e 74 20 69 53 65  :(a))..  int iSe
364df 6c 65 63 74 20 3d 20 2d 31 3b 0a 20 20 66 6c 6f  lect = -1;.  flo
364e0 61 74 20 66 44 69 66 66 3b 0a 20 20 69 6e 74 20  at fDiff;.  int 
364e1 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ii;.  for(ii=0; 
364e2 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
364e3 0a 20 20 20 20 69 66 28 20 61 69 55 73 65 64 5b  .    if( aiUsed[
364e4 69 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ii]==0 ){.      
364e5 66 6c 6f 61 74 20 6c 65 66 74 20 3d 20 63 65 6c  float left = cel
364e6 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20  lGrowth(pRtree, 
364e7 70 4c 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c  pLeftBox, &aCell
364e8 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f  [ii]);.      flo
364e9 61 74 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 47  at right = cellG
364ea 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c  rowth(pRtree, pL
364eb 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69  eftBox, &aCell[i
364ec 69 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  i]);.      float
364ed 20 64 69 66 66 20 3d 20 46 41 42 53 28 72 69 67   diff = FABS(rig
364ee 68 74 2d 6c 65 66 74 29 3b 0a 20 20 20 20 20 20  ht-left);.      
364ef 69 66 28 20 69 53 65 6c 65 63 74 3c 30 20 7c 7c  if( iSelect<0 ||
364f0 20 64 69 66 66 3e 66 44 69 66 66 20 29 7b 0a 20   diff>fDiff ){. 
364f1 20 20 20 20 20 20 20 66 44 69 66 66 20 3d 20 64         fDiff = d
364f2 69 66 66 3b 0a 20 20 20 20 20 20 20 20 69 53 65  iff;.        iSe
364f3 6c 65 63 74 20 3d 20 69 69 3b 0a 20 20 20 20 20  lect = ii;.     
364f4 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61   }.    }.  }.  a
364f5 69 55 73 65 64 5b 69 53 65 6c 65 63 74 5d 20 3d  iUsed[iSelect] =
364f6 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43   1;.  return &aC
364f7 65 6c 6c 5b 69 53 65 6c 65 63 74 5d 3b 0a 7d 0a  ell[iSelect];.}.
364f8 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
364f9 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61  ation of the qua
364fa 64 72 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f  dratic variant o
364fb 66 20 74 68 65 20 50 69 63 6b 53 65 65 64 73 28  f the PickSeeds(
364fc 29 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a  ) function from.
364fd 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a  ** Guttman[84]..
364fe 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 51  */.static void Q
364ff 75 61 64 72 61 74 69 63 50 69 63 6b 53 65 65 64  uadraticPickSeed
36500 73 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  s(.  Rtree *pRtr
36501 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
36502 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
36503 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69  Cell, .  int *pi
36504 4c 65 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74  LeftSeed, .  int
36505 20 2a 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b   *piRightSeed.){
36506 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
36507 20 6a 6a 3b 0a 0a 20 20 69 6e 74 20 69 4c 65 66   jj;..  int iLef
36508 74 53 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74  tSeed = 0;.  int
36509 20 69 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b   iRightSeed = 1;
3650a 0a 20 20 66 6c 6f 61 74 20 66 57 61 73 74 65 20  .  float fWaste 
3650b 3d 20 30 2e 30 3b 0a 0a 20 20 66 6f 72 28 69 69  = 0.0;..  for(ii
3650c 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69  =0; ii<nCell; ii
3650d 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d  ++){.    for(jj=
3650e 69 69 2b 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20  ii+1; jj<nCell; 
3650f 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f  jj++){.      flo
36510 61 74 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 41  at right = cellA
36511 72 65 61 28 70 52 74 72 65 65 2c 20 26 61 43 65  rea(pRtree, &aCe
36512 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 66  ll[jj]);.      f
36513 6c 6f 61 74 20 67 72 6f 77 74 68 20 3d 20 63 65  loat growth = ce
36514 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c  llGrowth(pRtree,
36515 20 26 61 43 65 6c 6c 5b 69 69 5d 2c 20 26 61 43   &aCell[ii], &aC
36516 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20  ell[jj]);.      
36517 66 6c 6f 61 74 20 77 61 73 74 65 20 3d 20 67 72  float waste = gr
36518 6f 77 74 68 20 2d 20 72 69 67 68 74 3b 0a 0a 20  owth - right;.. 
36519 20 20 20 20 20 69 66 28 20 77 61 73 74 65 3e 66       if( waste>f
3651a 57 61 73 74 65 20 29 7b 0a 20 20 20 20 20 20 20  Waste ){.       
3651b 20 69 4c 65 66 74 53 65 65 64 20 3d 20 69 69 3b   iLeftSeed = ii;
3651c 0a 20 20 20 20 20 20 20 20 69 52 69 67 68 74 53  .        iRightS
3651d 65 65 64 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20  eed = jj;.      
3651e 20 20 66 57 61 73 74 65 20 3d 20 77 61 73 74 65    fWaste = waste
3651f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
36520 20 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65    }..  *piLeftSe
36521 65 64 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a  ed = iLeftSeed;.
36522 20 20 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d    *piRightSeed =
36523 20 69 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23   iRightSeed;.}.#
36524 65 6e 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54  endif /* VARIANT
36525 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54  _GUTTMAN_QUADRAT
36526 49 43 5f 53 50 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a  IC_SPLIT */../*.
36527 2a 2a 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64  ** Arguments aId
36528 78 2c 20 61 44 69 73 74 61 6e 63 65 20 61 6e 64  x, aDistance and
36529 20 61 53 70 61 72 65 20 61 6c 6c 20 70 6f 69 6e   aSpare all poin
3652a 74 20 74 6f 20 61 72 72 61 79 73 20 6f 66 20 73  t to arrays of s
3652b 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68 65  ize.** nIdx. The
3652c 20 61 49 64 78 20 61 72 72 61 79 20 63 6f 6e 74   aIdx array cont
3652d 61 69 6e 73 20 74 68 65 20 73 65 74 20 6f 66 20  ains the set of 
3652e 69 6e 74 65 67 65 72 73 20 66 72 6f 6d 20 30 20  integers from 0 
3652f 74 6f 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29 20  to .** (nIdx-1) 
36530 69 6e 20 6e 6f 20 70 61 72 74 69 63 75 6c 61 72  in no particular
36531 20 6f 72 64 65 72 2e 20 54 68 69 73 20 66 75 6e   order. This fun
36532 63 74 69 6f 6e 20 73 6f 72 74 73 20 74 68 65 20  ction sorts the 
36533 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49 64  values.** in aId
36534 78 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  x according to t
36535 68 65 20 69 6e 64 65 78 65 64 20 76 61 6c 75 65  he indexed value
36536 73 20 69 6e 20 61 44 69 73 74 61 6e 63 65 2e 20  s in aDistance. 
36537 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20  For.** example, 
36538 61 73 73 75 6d 69 6e 67 20 74 68 65 20 69 6e 70  assuming the inp
36539 75 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64  uts:.**.**   aId
3653a 78 20 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20  x      = { 0,   
3653b 31 2c 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a  1,   2,   3 }.**
3653c 20 20 20 61 44 69 73 74 61 6e 63 65 20 3d 20 7b     aDistance = {
3653d 20 35 2e 30 2c 20 32 2e 30 2c 20 37 2e 30 2c 20   5.0, 2.0, 7.0, 
3653e 36 2e 30 20 7d 0a 2a 2a 0a 2a 2a 20 74 68 69 73  6.0 }.**.** this
3653f 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74   function sets t
36540 68 65 20 61 49 64 78 20 61 72 72 61 79 20 74 6f  he aIdx array to
36541 20 63 6f 6e 74 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20   contain:.**.** 
36542 20 20 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20    aIdx      = { 
36543 30 2c 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33  0,   1,   2,   3
36544 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 53 70   }.**.** The aSp
36545 61 72 65 20 61 72 72 61 79 20 69 73 20 75 73 65  are array is use
36546 64 20 61 73 20 74 65 6d 70 6f 72 61 72 79 20 77  d as temporary w
36547 6f 72 6b 69 6e 67 20 73 70 61 63 65 20 62 79 20  orking space by 
36548 74 68 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 61  the.** sorting a
36549 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61  lgorithm..*/.sta
3654a 74 69 63 20 76 6f 69 64 20 53 6f 72 74 42 79 44  tic void SortByD
3654b 69 73 74 61 6e 63 65 28 0a 20 20 69 6e 74 20 2a  istance(.  int *
3654c 61 49 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64  aIdx, .  int nId
3654d 78 2c 20 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69  x, .  float *aDi
3654e 73 74 61 6e 63 65 2c 20 0a 20 20 69 6e 74 20 2a  stance, .  int *
3654f 61 53 70 61 72 65 0a 29 7b 0a 20 20 69 66 28 20  aSpare.){.  if( 
36550 6e 49 64 78 3e 31 20 29 7b 0a 20 20 20 20 69 6e  nIdx>1 ){.    in
36551 74 20 69 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20  t iLeft = 0;.   
36552 20 69 6e 74 20 69 52 69 67 68 74 20 3d 20 30 3b   int iRight = 0;
36553 0a 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66 74 20  ..    int nLeft 
36554 3d 20 6e 49 64 78 2f 32 3b 0a 20 20 20 20 69 6e  = nIdx/2;.    in
36555 74 20 6e 52 69 67 68 74 20 3d 20 6e 49 64 78 2d  t nRight = nIdx-
36556 6e 4c 65 66 74 3b 0a 20 20 20 20 69 6e 74 20 2a  nLeft;.    int *
36557 61 4c 65 66 74 20 3d 20 61 49 64 78 3b 0a 20 20  aLeft = aIdx;.  
36558 20 20 69 6e 74 20 2a 61 52 69 67 68 74 20 3d 20    int *aRight = 
36559 26 61 49 64 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20  &aIdx[nLeft];.. 
3655a 20 20 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63     SortByDistanc
3655b 65 28 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20  e(aLeft, nLeft, 
3655c 61 44 69 73 74 61 6e 63 65 2c 20 61 53 70 61 72  aDistance, aSpar
3655d 65 29 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69  e);.    SortByDi
3655e 73 74 61 6e 63 65 28 61 52 69 67 68 74 2c 20 6e  stance(aRight, n
3655f 52 69 67 68 74 2c 20 61 44 69 73 74 61 6e 63 65  Right, aDistance
36560 2c 20 61 53 70 61 72 65 29 3b 0a 0a 20 20 20 20  , aSpare);..    
36561 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c 20 61  memcpy(aSpare, a
36562 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74  Left, sizeof(int
36563 29 2a 6e 4c 65 66 74 29 3b 0a 20 20 20 20 61 4c  )*nLeft);.    aL
36564 65 66 74 20 3d 20 61 53 70 61 72 65 3b 0a 0a 20  eft = aSpare;.. 
36565 20 20 20 77 68 69 6c 65 28 20 69 4c 65 66 74 3c     while( iLeft<
36566 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67 68 74 3c  nLeft || iRight<
36567 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  nRight ){.      
36568 69 66 28 20 69 4c 65 66 74 3d 3d 6e 4c 65 66 74  if( iLeft==nLeft
36569 20 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78   ){.        aIdx
3656a 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d  [iLeft+iRight] =
3656b 20 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b   aRight[iRight];
3656c 0a 20 20 20 20 20 20 20 20 69 52 69 67 68 74 2b  .        iRight+
3656d 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  +;.      }else i
3656e 66 28 20 69 52 69 67 68 74 3d 3d 6e 52 69 67 68  f( iRight==nRigh
3656f 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64  t ){.        aId
36570 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20  x[iLeft+iRight] 
36571 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a  = aLeft[iLeft];.
36572 20 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b          iLeft++;
36573 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
36574 20 20 20 20 20 20 66 6c 6f 61 74 20 66 4c 65 66        float fLef
36575 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 4c  t = aDistance[aL
36576 65 66 74 5b 69 4c 65 66 74 5d 5d 3b 0a 20 20 20  eft[iLeft]];.   
36577 20 20 20 20 20 66 6c 6f 61 74 20 66 52 69 67 68       float fRigh
36578 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 52  t = aDistance[aR
36579 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 3b 0a 20  ight[iRight]];. 
3657a 20 20 20 20 20 20 20 69 66 28 20 66 4c 65 66 74         if( fLeft
3657b 3c 66 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  <fRight ){.     
3657c 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
3657d 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b  iRight] = aLeft[
3657e 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20 20 20 20  iLeft];.        
3657f 20 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20    iLeft++;.     
36580 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
36581 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69      aIdx[iLeft+i
36582 52 69 67 68 74 5d 20 3d 20 61 52 69 67 68 74 5b  Right] = aRight[
36583 69 52 69 67 68 74 5d 3b 0a 20 20 20 20 20 20 20  iRight];.       
36584 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20     iRight++;.   
36585 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
36586 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20     }..#if 0.    
36587 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
36588 65 20 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f  e sort worked */
36589 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74  .    {.      int
3658a 20 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a   jj;.      for(j
3658b 6a 3d 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a  j=1; jj<nIdx; jj
3658c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f  ++){.        flo
3658d 61 74 20 6c 65 66 74 20 3d 20 61 44 69 73 74 61  at left = aDista
3658e 6e 63 65 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 3b  nce[aIdx[jj-1]];
3658f 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 72  .        float r
36590 69 67 68 74 20 3d 20 61 44 69 73 74 61 6e 63 65  ight = aDistance
36591 5b 61 49 64 78 5b 6a 6a 5d 5d 3b 0a 20 20 20 20  [aIdx[jj]];.    
36592 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 66 74      assert( left
36593 3c 3d 72 69 67 68 74 20 29 3b 0a 20 20 20 20 20  <=right );.     
36594 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
36595 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67    }.}../*.** Arg
36596 75 6d 65 6e 74 73 20 61 49 64 78 2c 20 61 43 65  uments aIdx, aCe
36597 6c 6c 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c  ll and aSpare al
36598 6c 20 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79  l point to array
36599 73 20 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64  s of size.** nId
3659a 78 2e 20 54 68 65 20 61 49 64 78 20 61 72 72 61  x. The aIdx arra
3659b 79 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73  y contains the s
3659c 65 74 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66  et of integers f
3659d 72 6f 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49  rom 0 to .** (nI
3659e 64 78 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74  dx-1) in no part
3659f 69 63 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68  icular order. Th
365a0 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74  is function sort
365a1 73 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20  s the values.** 
365a2 69 6e 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e  in aIdx accordin
365a3 67 20 74 6f 20 64 69 6d 65 6e 73 69 6f 6e 20 69  g to dimension i
365a4 44 69 6d 20 6f 66 20 74 68 65 20 63 65 6c 6c 73  Dim of the cells
365a5 20 69 6e 20 61 43 65 6c 6c 2e 20 54 68 65 0a 2a   in aCell. The.*
365a6 2a 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20  * minimum value 
365a7 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69  of dimension iDi
365a8 6d 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  m is considered 
365a9 66 69 72 73 74 2c 20 74 68 65 0a 2a 2a 20 6d 61  first, the.** ma
365aa 78 69 6d 75 6d 20 75 73 65 64 20 74 6f 20 62 72  ximum used to br
365ab 65 61 6b 20 74 69 65 73 2e 0a 2a 2a 0a 2a 2a 20  eak ties..**.** 
365ac 54 68 65 20 61 53 70 61 72 65 20 61 72 72 61 79  The aSpare array
365ad 20 69 73 20 75 73 65 64 20 61 73 20 74 65 6d 70   is used as temp
365ae 6f 72 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70  orary working sp
365af 61 63 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f  ace by the.** so
365b0 72 74 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e  rting algorithm.
365b1 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
365b2 53 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28  SortByDimension(
365b3 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
365b4 2c 0a 20 20 69 6e 74 20 2a 61 49 64 78 2c 20 0a  ,.  int *aIdx, .
365b5 20 20 69 6e 74 20 6e 49 64 78 2c 20 0a 20 20 69    int nIdx, .  i
365b6 6e 74 20 69 44 69 6d 2c 20 0a 20 20 52 74 72 65  nt iDim, .  Rtre
365b7 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20  eCell *aCell, . 
365b8 20 69 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a   int *aSpare.){.
365b9 20 20 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a    if( nIdx>1 ){.
365ba 0a 20 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d  .    int iLeft =
365bb 20 30 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67   0;.    int iRig
365bc 68 74 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74  ht = 0;..    int
365bd 20 6e 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b   nLeft = nIdx/2;
365be 0a 20 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20  .    int nRight 
365bf 3d 20 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20  = nIdx-nLeft;.  
365c0 20 20 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61    int *aLeft = a
365c1 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52  Idx;.    int *aR
365c2 69 67 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65  ight = &aIdx[nLe
365c3 66 74 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79  ft];..    SortBy
365c4 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65  Dimension(pRtree
365c5 2c 20 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20  , aLeft, nLeft, 
365c6 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70  iDim, aCell, aSp
365c7 61 72 65 29 3b 0a 20 20 20 20 53 6f 72 74 42 79  are);.    SortBy
365c8 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65  Dimension(pRtree
365c9 2c 20 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74  , aRight, nRight
365ca 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61  , iDim, aCell, a
365cb 53 70 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65 6d  Spare);..    mem
365cc 63 70 79 28 61 53 70 61 72 65 2c 20 61 4c 65 66  cpy(aSpare, aLef
365cd 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e  t, sizeof(int)*n
365ce 4c 65 66 74 29 3b 0a 20 20 20 20 61 4c 65 66 74  Left);.    aLeft
365cf 20 3d 20 61 53 70 61 72 65 3b 0a 20 20 20 20 77   = aSpare;.    w
365d0 68 69 6c 65 28 20 69 4c 65 66 74 3c 6e 4c 65 66  hile( iLeft<nLef
365d1 74 20 7c 7c 20 69 52 69 67 68 74 3c 6e 52 69 67  t || iRight<nRig
365d2 68 74 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62  ht ){.      doub
365d3 6c 65 20 78 6c 65 66 74 31 20 3d 20 44 43 4f 4f  le xleft1 = DCOO
365d4 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69  RD(aCell[aLeft[i
365d5 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44  Left]].aCoord[iD
365d6 69 6d 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f  im*2]);.      do
365d7 75 62 6c 65 20 78 6c 65 66 74 32 20 3d 20 44 43  uble xleft2 = DC
365d8 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74  OORD(aCell[aLeft
365d9 5b 69 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b  [iLeft]].aCoord[
365da 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20  iDim*2+1]);.    
365db 20 20 64 6f 75 62 6c 65 20 78 72 69 67 68 74 31    double xright1
365dc 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
365dd 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e  aRight[iRight]].
365de 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b  aCoord[iDim*2]);
365df 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72  .      double xr
365e0 69 67 68 74 32 20 3d 20 44 43 4f 4f 52 44 28 61  ight2 = DCOORD(a
365e1 43 65 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67  Cell[aRight[iRig
365e2 68 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ht]].aCoord[iDim
365e3 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66  *2+1]);.      if
365e4 28 20 28 69 4c 65 66 74 21 3d 6e 4c 65 66 74 29  ( (iLeft!=nLeft)
365e5 20 26 26 20 28 28 69 52 69 67 68 74 3d 3d 6e 52   && ((iRight==nR
365e6 69 67 68 74 29 0a 20 20 20 20 20 20 20 7c 7c 20  ight).       || 
365e7 28 78 6c 65 66 74 31 3c 78 72 69 67 68 74 31 29  (xleft1<xright1)
365e8 0a 20 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66  .       || (xlef
365e9 74 31 3d 3d 78 72 69 67 68 74 31 20 26 26 20 78  t1==xright1 && x
365ea 6c 65 66 74 32 3c 78 72 69 67 68 74 32 29 0a 20  left2<xright2). 
365eb 20 20 20 20 20 29 29 7b 0a 20 20 20 20 20 20 20       )){.       
365ec 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
365ed 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66  ht] = aLeft[iLef
365ee 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65 66  t];.        iLef
365ef 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  t++;.      }else
365f0 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b 69  {.        aIdx[i
365f1 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
365f2 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20  Right[iRight];. 
365f3 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b         iRight++;
365f4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
365f5 23 69 66 20 30 0a 20 20 20 20 2f 2a 20 43 68 65  #if 0.    /* Che
365f6 63 6b 20 74 68 61 74 20 74 68 65 20 73 6f 72 74  ck that the sort
365f7 20 77 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20 7b   worked */.    {
365f8 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20  .      int jj;. 
365f9 20 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a       for(jj=1; j
365fa 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a 20  j<nIdx; jj++){. 
365fb 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65         float xle
365fc 66 74 31 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78  ft1 = aCell[aIdx
365fd 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69  [jj-1]].aCoord[i
365fe 44 69 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20  Dim*2];.        
365ff 66 6c 6f 61 74 20 78 6c 65 66 74 32 20 3d 20 61  float xleft2 = a
36600 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d  Cell[aIdx[jj-1]]
36601 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31  .aCoord[iDim*2+1
36602 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ];.        float
36603 20 78 72 69 67 68 74 31 20 3d 20 61 43 65 6c 6c   xright1 = aCell
36604 5b 61 49 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72  [aIdx[jj]].aCoor
36605 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20 20 20 20  d[iDim*2];.     
36606 20 20 20 66 6c 6f 61 74 20 78 72 69 67 68 74 32     float xright2
36607 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a   = aCell[aIdx[jj
36608 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
36609 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73  +1];.        ass
3660a 65 72 74 28 20 78 6c 65 66 74 31 3c 3d 78 72 69  ert( xleft1<=xri
3660b 67 68 74 31 20 26 26 20 28 78 6c 65 66 74 31 3c  ght1 && (xleft1<
3660c 78 72 69 67 68 74 31 20 7c 7c 20 78 6c 65 66 74  xright1 || xleft
3660d 32 3c 3d 78 72 69 67 68 74 32 29 20 29 3b 0a 20  2<=xright2) );. 
3660e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
3660f 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 56  dif.  }.}..#if V
36610 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
36611 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70  _SPLIT./*.** Imp
36612 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
36613 68 65 20 52 2a 2d 74 72 65 65 20 76 61 72 69 61  he R*-tree varia
36614 6e 74 20 6f 66 20 53 70 6c 69 74 4e 6f 64 65 20  nt of SplitNode 
36615 66 72 6f 6d 20 42 65 63 6b 6d 61 6e 5b 31 39 39  from Beckman[199
36616 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  0]..*/.static in
36617 74 20 73 70 6c 69 74 4e 6f 64 65 53 74 61 72 74  t splitNodeStart
36618 72 65 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ree(.  Rtree *pR
36619 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c  tree,.  RtreeCel
3661a 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20  l *aCell,.  int 
3661b 6e 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f  nCell,.  RtreeNo
3661c 64 65 20 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72  de *pLeft,.  Rtr
3661d 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a  eeNode *pRight,.
3661e 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62    RtreeCell *pBb
3661f 6f 78 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 43  oxLeft,.  RtreeC
36620 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67 68 74 0a  ell *pBboxRight.
36621 29 7b 0a 20 20 69 6e 74 20 2a 2a 61 61 53 6f 72  ){.  int **aaSor
36622 74 65 64 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61  ted;.  int *aSpa
36623 72 65 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20  re;.  int ii;.. 
36624 20 69 6e 74 20 69 42 65 73 74 44 69 6d 3b 0a 20   int iBestDim;. 
36625 20 69 6e 74 20 69 42 65 73 74 53 70 6c 69 74 3b   int iBestSplit;
36626 0a 20 20 66 6c 6f 61 74 20 66 42 65 73 74 4d 61  .  float fBestMa
36627 72 67 69 6e 3b 0a 0a 20 20 69 6e 74 20 6e 42 79  rgin;..  int nBy
36628 74 65 20 3d 20 28 70 52 74 72 65 65 2d 3e 6e 44  te = (pRtree->nD
36629 69 6d 2b 31 29 2a 28 73 69 7a 65 6f 66 28 69 6e  im+1)*(sizeof(in
3662a 74 2a 29 2b 6e 43 65 6c 6c 2a 73 69 7a 65 6f 66  t*)+nCell*sizeof
3662b 28 69 6e 74 29 29 3b 0a 0a 20 20 61 61 53 6f 72  (int));..  aaSor
3662c 74 65 64 20 3d 20 28 69 6e 74 20 2a 2a 29 73 71  ted = (int **)sq
3662d 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79  lite3_malloc(nBy
3662e 74 65 29 3b 0a 20 20 69 66 28 20 21 61 61 53 6f  te);.  if( !aaSo
3662f 72 74 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  rted ){.    retu
36630 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
36631 0a 20 20 7d 0a 0a 20 20 61 53 70 61 72 65 20 3d  .  }..  aSpare =
36632 20 26 28 28 69 6e 74 20 2a 29 26 61 61 53 6f 72   &((int *)&aaSor
36633 74 65 64 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ted[pRtree->nDim
36634 5d 29 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  ])[pRtree->nDim*
36635 6e 43 65 6c 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74  nCell];.  memset
36636 28 61 61 53 6f 72 74 65 64 2c 20 30 2c 20 6e 42  (aaSorted, 0, nB
36637 79 74 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30  yte);.  for(ii=0
36638 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69  ; ii<pRtree->nDi
36639 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  m; ii++){.    in
3663a 74 20 6a 6a 3b 0a 20 20 20 20 61 61 53 6f 72 74  t jj;.    aaSort
3663b 65 64 5b 69 69 5d 20 3d 20 26 28 28 69 6e 74 20  ed[ii] = &((int 
3663c 2a 29 26 61 61 53 6f 72 74 65 64 5b 70 52 74 72  *)&aaSorted[pRtr
3663d 65 65 2d 3e 6e 44 69 6d 5d 29 5b 69 69 2a 6e 43  ee->nDim])[ii*nC
3663e 65 6c 6c 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 6a  ell];.    for(jj
3663f 3d 30 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a  =0; jj<nCell; jj
36640 2b 2b 29 7b 0a 20 20 20 20 20 20 61 61 53 6f 72  ++){.      aaSor
36641 74 65 64 5b 69 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a  ted[ii][jj] = jj
36642 3b 0a 20 20 20 20 7d 0a 20 20 20 20 53 6f 72 74  ;.    }.    Sort
36643 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72  ByDimension(pRtr
36644 65 65 2c 20 61 61 53 6f 72 74 65 64 5b 69 69 5d  ee, aaSorted[ii]
36645 2c 20 6e 43 65 6c 6c 2c 20 69 69 2c 20 61 43 65  , nCell, ii, aCe
36646 6c 6c 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 7d  ll, aSpare);.  }
36647 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
36648 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69  <pRtree->nDim; i
36649 69 2b 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20  i++){.    float 
3664a 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b 0a 20 20  margin = 0.0;.  
3664b 20 20 66 6c 6f 61 74 20 66 42 65 73 74 4f 76 65    float fBestOve
3664c 72 6c 61 70 3b 0a 20 20 20 20 66 6c 6f 61 74 20  rlap;.    float 
3664d 66 42 65 73 74 41 72 65 61 3b 0a 20 20 20 20 69  fBestArea;.    i
3664e 6e 74 20 69 42 65 73 74 4c 65 66 74 3b 0a 20 20  nt iBestLeft;.  
3664f 20 20 69 6e 74 20 6e 4c 65 66 74 3b 0a 0a 20 20    int nLeft;..  
36650 20 20 66 6f 72 28 0a 20 20 20 20 20 20 6e 4c 65    for(.      nLe
36651 66 74 3d 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c  ft=RTREE_MINCELL
36652 53 28 70 52 74 72 65 65 29 3b 20 0a 20 20 20 20  S(pRtree); .    
36653 20 20 6e 4c 65 66 74 3c 3d 28 6e 43 65 6c 6c 2d    nLeft<=(nCell-
36654 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
36655 52 74 72 65 65 29 29 3b 20 0a 20 20 20 20 20 20  Rtree)); .      
36656 6e 4c 65 66 74 2b 2b 0a 20 20 20 20 29 7b 0a 20  nLeft++.    ){. 
36657 20 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 6c       RtreeCell l
36658 65 66 74 3b 0a 20 20 20 20 20 20 52 74 72 65 65  eft;.      Rtree
36659 43 65 6c 6c 20 72 69 67 68 74 3b 0a 20 20 20 20  Cell right;.    
3665a 20 20 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20    int kk;.      
3665b 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 3b 0a 20  float overlap;. 
3665c 20 20 20 20 20 66 6c 6f 61 74 20 61 72 65 61 3b       float area;
3665d 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ..      memcpy(&
3665e 6c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  left, &aCell[aaS
3665f 6f 72 74 65 64 5b 69 69 5d 5b 30 5d 5d 2c 20 73  orted[ii][0]], s
36660 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
36661 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
36662 26 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61  &right, &aCell[a
36663 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6e 43 65 6c  aSorted[ii][nCel
36664 6c 2d 31 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74  l-1]], sizeof(Rt
36665 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20  reeCell));.     
36666 20 66 6f 72 28 6b 6b 3d 31 3b 20 6b 6b 3c 28 6e   for(kk=1; kk<(n
36667 43 65 6c 6c 2d 31 29 3b 20 6b 6b 2b 2b 29 7b 0a  Cell-1); kk++){.
36668 20 20 20 20 20 20 20 20 69 66 28 20 6b 6b 3c 6e          if( kk<n
36669 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Left ){.        
3666a 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72    cellUnion(pRtr
3666b 65 65 2c 20 26 6c 65 66 74 2c 20 26 61 43 65 6c  ee, &left, &aCel
3666c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6b  l[aaSorted[ii][k
3666d 6b 5d 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  k]]);.        }e
3666e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
3666f 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c  ellUnion(pRtree,
36670 20 26 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b   &right, &aCell[
36671 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d  aaSorted[ii][kk]
36672 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ]);.        }.  
36673 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 61 72 67      }.      marg
36674 69 6e 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e  in += cellMargin
36675 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74 29 3b  (pRtree, &left);
36676 0a 20 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d  .      margin +=
36677 20 63 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72   cellMargin(pRtr
36678 65 65 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20  ee, &right);.   
36679 20 20 20 6f 76 65 72 6c 61 70 20 3d 20 63 65 6c     overlap = cel
3667a 6c 4f 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c  lOverlap(pRtree,
3667b 20 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20   &left, &right, 
3667c 31 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 61 72  1, -1);.      ar
3667d 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52  ea = cellArea(pR
3667e 74 72 65 65 2c 20 26 6c 65 66 74 29 20 2b 20 63  tree, &left) + c
3667f 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20  ellArea(pRtree, 
36680 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69  &right);.      i
36681 66 28 20 28 6e 4c 65 66 74 3d 3d 52 54 52 45 45  f( (nLeft==RTREE
36682 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
36683 29 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76  )).       || (ov
36684 65 72 6c 61 70 3c 66 42 65 73 74 4f 76 65 72 6c  erlap<fBestOverl
36685 61 70 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f  ap).       || (o
36686 76 65 72 6c 61 70 3d 3d 66 42 65 73 74 4f 76 65  verlap==fBestOve
36687 72 6c 61 70 20 26 26 20 61 72 65 61 3c 66 42 65  rlap && area<fBe
36688 73 74 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b  stArea).      ){
36689 0a 20 20 20 20 20 20 20 20 69 42 65 73 74 4c 65  .        iBestLe
3668a 66 74 20 3d 20 6e 4c 65 66 74 3b 0a 20 20 20 20  ft = nLeft;.    
3668b 20 20 20 20 66 42 65 73 74 4f 76 65 72 6c 61 70      fBestOverlap
3668c 20 3d 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20   = overlap;.    
3668d 20 20 20 20 66 42 65 73 74 41 72 65 61 20 3d 20      fBestArea = 
3668e 61 72 65 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20  area;.      }.  
3668f 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 69 3d    }..    if( ii=
36690 3d 30 20 7c 7c 20 6d 61 72 67 69 6e 3c 66 42 65  =0 || margin<fBe
36691 73 74 4d 61 72 67 69 6e 20 29 7b 0a 20 20 20 20  stMargin ){.    
36692 20 20 69 42 65 73 74 44 69 6d 20 3d 20 69 69 3b    iBestDim = ii;
36693 0a 20 20 20 20 20 20 66 42 65 73 74 4d 61 72 67  .      fBestMarg
36694 69 6e 20 3d 20 6d 61 72 67 69 6e 3b 0a 20 20 20  in = margin;.   
36695 20 20 20 69 42 65 73 74 53 70 6c 69 74 20 3d 20     iBestSplit = 
36696 69 42 65 73 74 4c 65 66 74 3b 0a 20 20 20 20 7d  iBestLeft;.    }
36697 0a 20 20 7d 0a 0a 20 20 6d 65 6d 63 70 79 28 70  .  }..  memcpy(p
36698 42 62 6f 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c  BboxLeft, &aCell
36699 5b 61 61 53 6f 72 74 65 64 5b 69 42 65 73 74 44  [aaSorted[iBestD
3669a 69 6d 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66 28  im][0]], sizeof(
3669b 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d  RtreeCell));.  m
3669c 65 6d 63 70 79 28 70 42 62 6f 78 52 69 67 68 74  emcpy(pBboxRight
3669d 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65  , &aCell[aaSorte
3669e 64 5b 69 42 65 73 74 44 69 6d 5d 5b 69 42 65 73  d[iBestDim][iBes
3669f 74 53 70 6c 69 74 5d 5d 2c 20 73 69 7a 65 6f 66  tSplit]], sizeof
366a0 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
366a1 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65  for(ii=0; ii<nCe
366a2 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52  ll; ii++){.    R
366a3 74 72 65 65 4e 6f 64 65 20 2a 70 54 61 72 67 65  treeNode *pTarge
366a4 74 20 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c  t = (ii<iBestSpl
366a5 69 74 29 3f 70 4c 65 66 74 3a 70 52 69 67 68 74  it)?pLeft:pRight
366a6 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  ;.    RtreeCell 
366a7 2a 70 42 62 6f 78 20 3d 20 28 69 69 3c 69 42 65  *pBbox = (ii<iBe
366a8 73 74 53 70 6c 69 74 29 3f 70 42 62 6f 78 4c 65  stSplit)?pBboxLe
366a9 66 74 3a 70 42 62 6f 78 52 69 67 68 74 3b 0a 20  ft:pBboxRight;. 
366aa 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43     RtreeCell *pC
366ab 65 6c 6c 20 3d 20 26 61 43 65 6c 6c 5b 61 61 53  ell = &aCell[aaS
366ac 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b  orted[iBestDim][
366ad 69 69 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65 49 6e  ii]];.    nodeIn
366ae 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
366af 20 70 54 61 72 67 65 74 2c 20 70 43 65 6c 6c 29   pTarget, pCell)
366b0 3b 0a 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28  ;.    cellUnion(
366b1 70 52 74 72 65 65 2c 20 70 42 62 6f 78 2c 20 70  pRtree, pBbox, p
366b2 43 65 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 73 71  Cell);.  }..  sq
366b3 6c 69 74 65 33 5f 66 72 65 65 28 61 61 53 6f 72  lite3_free(aaSor
366b4 74 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ted);.  return S
366b5 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
366b6 69 66 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  if..#if VARIANT_
366b7 47 55 54 54 4d 41 4e 5f 53 50 4c 49 54 0a 2f 2a  GUTTMAN_SPLIT./*
366b8 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
366b9 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 75 6c 61  on of the regula
366ba 72 20 52 2d 74 72 65 65 20 53 70 6c 69 74 4e 6f  r R-tree SplitNo
366bb 64 65 20 66 72 6f 6d 20 47 75 74 74 6d 61 6e 5b  de from Guttman[
366bc 31 39 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  1984]..*/.static
366bd 20 69 6e 74 20 73 70 6c 69 74 4e 6f 64 65 47 75   int splitNodeGu
366be 74 74 6d 61 6e 28 0a 20 20 52 74 72 65 65 20 2a  ttman(.  Rtree *
366bf 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43  pRtree,.  RtreeC
366c0 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e  ell *aCell,.  in
366c1 74 20 6e 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65  t nCell,.  Rtree
366c2 4e 6f 64 65 20 2a 70 4c 65 66 74 2c 0a 20 20 52  Node *pLeft,.  R
366c3 74 72 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74  treeNode *pRight
366c4 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  ,.  RtreeCell *p
366c5 42 62 6f 78 4c 65 66 74 2c 0a 20 20 52 74 72 65  BboxLeft,.  Rtre
366c6 65 43 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67 68  eCell *pBboxRigh
366c7 74 0a 29 7b 0a 20 20 69 6e 74 20 69 4c 65 66 74  t.){.  int iLeft
366c8 53 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Seed = 0;.  int 
366c9 69 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a  iRightSeed = 1;.
366ca 20 20 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 20    int *aiUsed;. 
366cb 20 69 6e 74 20 69 3b 0a 0a 20 20 61 69 55 73 65   int i;..  aiUse
366cc 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  d = sqlite3_mall
366cd 6f 63 28 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e  oc(sizeof(int)*n
366ce 43 65 6c 6c 29 3b 0a 20 20 6d 65 6d 73 65 74 28  Cell);.  memset(
366cf 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f  aiUsed, 0, sizeo
366d0 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0a 0a  f(int)*nCell);..
366d1 20 20 50 69 63 6b 53 65 65 64 73 28 70 52 74 72    PickSeeds(pRtr
366d2 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c  ee, aCell, nCell
366d3 2c 20 26 69 4c 65 66 74 53 65 65 64 2c 20 26 69  , &iLeftSeed, &i
366d4 52 69 67 68 74 53 65 65 64 29 3b 0a 0a 20 20 6d  RightSeed);..  m
366d5 65 6d 63 70 79 28 70 42 62 6f 78 4c 65 66 74 2c  emcpy(pBboxLeft,
366d6 20 26 61 43 65 6c 6c 5b 69 4c 65 66 74 53 65 65   &aCell[iLeftSee
366d7 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  d], sizeof(Rtree
366d8 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d 63 70 79  Cell));.  memcpy
366d9 28 70 42 62 6f 78 52 69 67 68 74 2c 20 26 61 43  (pBboxRight, &aC
366da 65 6c 6c 5b 69 52 69 67 68 74 53 65 65 64 5d 2c  ell[iRightSeed],
366db 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c   sizeof(RtreeCel
366dc 6c 29 29 3b 0a 20 20 6e 6f 64 65 49 6e 73 65 72  l));.  nodeInser
366dd 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c  tCell(pRtree, pL
366de 65 66 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66  eft, &aCell[iLef
366df 74 53 65 65 64 5d 29 3b 0a 20 20 6e 6f 64 65 49  tSeed]);.  nodeI
366e0 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
366e1 2c 20 70 52 69 67 68 74 2c 20 26 61 43 65 6c 6c  , pRight, &aCell
366e2 5b 69 52 69 67 68 74 53 65 65 64 5d 29 3b 0a 20  [iRightSeed]);. 
366e3 20 61 69 55 73 65 64 5b 69 4c 65 66 74 53 65 65   aiUsed[iLeftSee
366e4 64 5d 20 3d 20 31 3b 0a 20 20 61 69 55 73 65 64  d] = 1;.  aiUsed
366e5 5b 69 52 69 67 68 74 53 65 65 64 5d 20 3d 20 31  [iRightSeed] = 1
366e6 3b 0a 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c 6c  ;..  for(i=nCell
366e7 2d 32 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20  -2; i>0; i--){. 
366e8 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4e     RtreeCell *pN
366e9 65 78 74 3b 0a 20 20 20 20 70 4e 65 78 74 20 3d  ext;.    pNext =
366ea 20 50 69 63 6b 4e 65 78 74 28 70 52 74 72 65 65   PickNext(pRtree
366eb 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20  , aCell, nCell, 
366ec 70 42 62 6f 78 4c 65 66 74 2c 20 70 42 62 6f 78  pBboxLeft, pBbox
366ed 52 69 67 68 74 2c 20 61 69 55 73 65 64 29 3b 0a  Right, aiUsed);.
366ee 20 20 20 20 66 6c 6f 61 74 20 64 69 66 66 20 3d      float diff =
366ef 20 20 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f    .      cellGro
366f0 77 74 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f  wth(pRtree, pBbo
366f1 78 4c 65 66 74 2c 20 70 4e 65 78 74 29 20 2d 20  xLeft, pNext) - 
366f2 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74  .      cellGrowt
366f3 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78 52  h(pRtree, pBboxR
366f4 69 67 68 74 2c 20 70 4e 65 78 74 29 0a 20 20 20  ight, pNext).   
366f5 20 3b 0a 20 20 20 20 69 66 28 20 28 52 54 52 45   ;.    if( (RTRE
366f6 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65  E_MINCELLS(pRtre
366f7 65 29 2d 4e 43 45 4c 4c 28 70 52 69 67 68 74 29  e)-NCELL(pRight)
366f8 3d 3d 69 29 0a 20 20 20 20 20 7c 7c 20 28 64 69  ==i).     || (di
366f9 66 66 3e 30 2e 30 20 26 26 20 28 52 54 52 45 45  ff>0.0 && (RTREE
366fa 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
366fb 29 2d 4e 43 45 4c 4c 28 70 4c 65 66 74 29 21 3d  )-NCELL(pLeft)!=
366fc 69 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  i)).    ){.     
366fd 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28   nodeInsertCell(
366fe 70 52 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20  pRtree, pRight, 
366ff 70 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 63 65  pNext);.      ce
36700 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20  llUnion(pRtree, 
36701 70 42 62 6f 78 52 69 67 68 74 2c 20 70 4e 65 78  pBboxRight, pNex
36702 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
36703 20 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43       nodeInsertC
36704 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66  ell(pRtree, pLef
36705 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20  t, pNext);.     
36706 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
36707 65 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 4e  e, pBboxLeft, pN
36708 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ext);.    }.  }.
36709 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3670a 61 69 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  aiUsed);.  retur
3670b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
3670c 65 6e 64 69 66 0a 0a 73 74 61 74 69 63 20 69 6e  endif..static in
3670d 74 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28  t updateMapping(
3670e 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
3670f 2c 20 0a 20 20 69 36 34 20 69 52 6f 77 69 64 2c  , .  i64 iRowid,
36710 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70   .  RtreeNode *p
36711 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20 69 48 65  Node, .  int iHe
36712 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 28 2a  ight.){.  int (*
36713 78 53 65 74 4d 61 70 70 69 6e 67 29 28 52 74 72  xSetMapping)(Rtr
36714 65 65 20 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e  ee *, sqlite3_in
36715 74 36 34 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  t64, sqlite3_int
36716 36 34 29 3b 0a 20 20 78 53 65 74 4d 61 70 70 69  64);.  xSetMappi
36717 6e 67 20 3d 20 28 28 69 48 65 69 67 68 74 3d 3d  ng = ((iHeight==
36718 30 29 3f 72 6f 77 69 64 57 72 69 74 65 3a 70 61  0)?rowidWrite:pa
36719 72 65 6e 74 57 72 69 74 65 29 3b 0a 20 20 69 66  rentWrite);.  if
3671a 28 20 69 48 65 69 67 68 74 3e 30 20 29 7b 0a 20  ( iHeight>0 ){. 
3671b 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43     RtreeNode *pC
3671c 68 69 6c 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c  hild = nodeHashL
3671d 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20 69 52  ookup(pRtree, iR
3671e 6f 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70  owid);.    if( p
3671f 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e  Child ){.      n
36720 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
36721 65 2c 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65  e, pChild->pPare
36722 6e 74 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52  nt);.      nodeR
36723 65 66 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b  eference(pNode);
36724 0a 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70  .      pChild->p
36725 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a  Parent = pNode;.
36726 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
36727 72 6e 20 78 53 65 74 4d 61 70 70 69 6e 67 28 70  rn xSetMapping(p
36728 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70  Rtree, iRowid, p
36729 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 7d 0a  Node->iNode);.}.
3672a 0a 73 74 61 74 69 63 20 69 6e 74 20 53 70 6c 69  .static int Spli
3672b 74 4e 6f 64 65 28 0a 20 20 52 74 72 65 65 20 2a  tNode(.  Rtree *
3672c 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 4e  pRtree,.  RtreeN
3672d 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a 20 20 52 74  ode *pNode,.  Rt
3672e 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a  reeCell *pCell,.
3672f 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b    int iHeight.){
36730 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
36731 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d  newCellIsRight =
36732 20 30 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20   0;..  int rc = 
36733 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
36734 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
36735 4e 6f 64 65 29 3b 0a 20 20 52 74 72 65 65 43 65  Node);.  RtreeCe
36736 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20 20 69 6e 74  ll *aCell;.  int
36737 20 2a 61 69 55 73 65 64 3b 0a 0a 20 20 52 74 72   *aiUsed;..  Rtr
36738 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 20 3d 20  eeNode *pLeft = 
36739 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  0;.  RtreeNode *
3673a 70 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 52  pRight = 0;..  R
3673b 74 72 65 65 43 65 6c 6c 20 6c 65 66 74 62 62 6f  treeCell leftbbo
3673c 78 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 72  x;.  RtreeCell r
3673d 69 67 68 74 62 62 6f 78 3b 0a 0a 20 20 2f 2a 20  ightbbox;..  /* 
3673e 41 6c 6c 6f 63 61 74 65 20 61 6e 20 61 72 72 61  Allocate an arra
3673f 79 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 69  y and populate i
36740 74 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66  t with a copy of
36741 20 70 43 65 6c 6c 20 61 6e 64 20 0a 20 20 2a 2a   pCell and .  **
36742 20 61 6c 6c 20 63 65 6c 6c 73 20 66 72 6f 6d 20   all cells from 
36743 6e 6f 64 65 20 70 4c 65 66 74 2e 20 54 68 65 6e  node pLeft. Then
36744 20 7a 65 72 6f 20 74 68 65 20 6f 72 69 67 69 6e   zero the origin
36745 61 6c 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  al node..  */.  
36746 61 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f  aCell = sqlite3_
36747 6d 61 6c 6c 6f 63 28 28 73 69 7a 65 6f 66 28 52  malloc((sizeof(R
36748 74 72 65 65 43 65 6c 6c 29 2b 73 69 7a 65 6f 66  treeCell)+sizeof
36749 28 69 6e 74 29 29 2a 28 6e 43 65 6c 6c 2b 31 29  (int))*(nCell+1)
3674a 29 3b 0a 20 20 69 66 28 20 21 61 43 65 6c 6c 20  );.  if( !aCell 
3674b 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
3674c 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  TE_NOMEM;.    go
3674d 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74  to splitnode_out
3674e 3b 0a 20 20 7d 0a 20 20 61 69 55 73 65 64 20 3d  ;.  }.  aiUsed =
3674f 20 28 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e   (int *)&aCell[n
36750 43 65 6c 6c 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65  Cell+1];.  memse
36751 74 28 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a  t(aiUsed, 0, siz
36752 65 6f 66 28 69 6e 74 29 2a 28 6e 43 65 6c 6c 2b  eof(int)*(nCell+
36753 31 29 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  1));.  for(i=0; 
36754 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20  i<nCell; i++){. 
36755 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70     nodeGetCell(p
36756 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 2c  Rtree, pNode, i,
36757 20 26 61 43 65 6c 6c 5b 69 5d 29 3b 0a 20 20 7d   &aCell[i]);.  }
36758 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72  .  nodeZero(pRtr
36759 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 6d 65  ee, pNode);.  me
3675a 6d 63 70 79 28 26 61 43 65 6c 6c 5b 6e 43 65 6c  mcpy(&aCell[nCel
3675b 6c 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f  l], pCell, sizeo
3675c 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20  f(RtreeCell));. 
3675d 20 6e 43 65 6c 6c 2b 2b 3b 0a 0a 20 20 69 66 28   nCell++;..  if(
3675e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31   pNode->iNode==1
3675f 20 29 7b 0a 20 20 20 20 70 52 69 67 68 74 20 3d   ){.    pRight =
36760 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c   nodeNew(pRtree,
36761 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20 20 20 20   pNode, 1);.    
36762 70 4c 65 66 74 20 3d 20 6e 6f 64 65 4e 65 77 28  pLeft = nodeNew(
36763 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 31  pRtree, pNode, 1
36764 29 3b 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 69  );.    pRtree->i
36765 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 70 4e 6f  Depth++;.    pNo
36766 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
36767 0a 20 20 20 20 77 72 69 74 65 49 6e 74 31 36 28  .    writeInt16(
36768 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 70 52  pNode->zData, pR
36769 74 72 65 65 2d 3e 69 44 65 70 74 68 29 3b 0a 20  tree->iDepth);. 
3676a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 65 66   }else{.    pLef
3676b 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 70  t = pNode;.    p
3676c 52 69 67 68 74 20 3d 20 6e 6f 64 65 4e 65 77 28  Right = nodeNew(
3676d 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2d 3e 70  pRtree, pLeft->p
3676e 50 61 72 65 6e 74 2c 20 31 29 3b 0a 20 20 20 20  Parent, 1);.    
3676f 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 4c  nodeReference(pL
36770 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  eft);.  }..  if(
36771 20 21 70 4c 65 66 74 20 7c 7c 20 21 70 52 69 67   !pLeft || !pRig
36772 68 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  ht ){.    rc = S
36773 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
36774 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
36775 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 73  out;.  }..  mems
36776 65 74 28 70 4c 65 66 74 2d 3e 7a 44 61 74 61 2c  et(pLeft->zData,
36777 20 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64   0, pRtree->iNod
36778 65 53 69 7a 65 29 3b 0a 20 20 6d 65 6d 73 65 74  eSize);.  memset
36779 28 70 52 69 67 68 74 2d 3e 7a 44 61 74 61 2c 20  (pRight->zData, 
3677a 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  0, pRtree->iNode
3677b 53 69 7a 65 29 3b 0a 0a 20 20 72 63 20 3d 20 41  Size);..  rc = A
3677c 73 73 69 67 6e 43 65 6c 6c 73 28 70 52 74 72 65  ssignCells(pRtre
3677d 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c  e, aCell, nCell,
3677e 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20   pLeft, pRight, 
3677f 26 6c 65 66 74 62 62 6f 78 2c 20 26 72 69 67 68  &leftbbox, &righ
36780 74 62 62 6f 78 29 3b 0a 20 20 69 66 28 20 72 63  tbbox);.  if( rc
36781 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
36782 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64     goto splitnod
36783 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e_out;.  }..  /*
36784 20 45 6e 73 75 72 65 20 62 6f 74 68 20 63 68 69   Ensure both chi
36785 6c 64 20 6e 6f 64 65 73 20 68 61 76 65 20 6e 6f  ld nodes have no
36786 64 65 20 6e 75 6d 62 65 72 73 20 61 73 73 69 67  de numbers assig
36787 6e 65 64 20 74 6f 20 74 68 65 6d 2e 20 2a 2f 0a  ned to them. */.
36788 20 20 69 66 28 20 28 30 3d 3d 70 52 69 67 68 74    if( (0==pRight
36789 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51 4c 49 54  ->iNode && SQLIT
3678a 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65  E_OK!=(rc = node
3678b 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 52  Write(pRtree, pR
3678c 69 67 68 74 29 29 29 0a 20 20 20 7c 7c 20 28 30  ight))).   || (0
3678d 3d 3d 70 4c 65 66 74 2d 3e 69 4e 6f 64 65 20 26  ==pLeft->iNode &
3678e 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  & SQLITE_OK!=(rc
3678f 20 3d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74   = nodeWrite(pRt
36790 72 65 65 2c 20 70 4c 65 66 74 29 29 29 0a 20 20  ree, pLeft))).  
36791 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  ){.    goto spli
36792 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  tnode_out;.  }..
36793 20 20 72 69 67 68 74 62 62 6f 78 2e 69 52 6f 77    rightbbox.iRow
36794 69 64 20 3d 20 70 52 69 67 68 74 2d 3e 69 4e 6f  id = pRight->iNo
36795 64 65 3b 0a 20 20 6c 65 66 74 62 62 6f 78 2e 69  de;.  leftbbox.i
36796 52 6f 77 69 64 20 3d 20 70 4c 65 66 74 2d 3e 69  Rowid = pLeft->i
36797 4e 6f 64 65 3b 0a 0a 20 20 69 66 28 20 70 4e 6f  Node;..  if( pNo
36798 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a  de->iNode==1 ){.
36799 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e      rc = rtreeIn
3679a 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
3679b 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 2c   pLeft->pParent,
3679c 20 26 6c 65 66 74 62 62 6f 78 2c 20 69 48 65 69   &leftbbox, iHei
3679d 67 68 74 2b 31 29 3b 0a 20 20 20 20 69 66 28 20  ght+1);.    if( 
3679e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
3679f 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  .      goto spli
367a0 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d  tnode_out;.    }
367a1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74  .  }else{.    Rt
367a2 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  reeNode *pParent
367a3 20 3d 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e   = pLeft->pParen
367a4 74 3b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  t;.    int iCell
367a5 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64   = nodeParentInd
367a6 65 78 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  ex(pRtree, pLeft
367a7 29 3b 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77  );.    nodeOverw
367a8 72 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c  riteCell(pRtree,
367a9 20 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62   pParent, &leftb
367aa 62 6f 78 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 20  box, iCell);.   
367ab 20 41 64 6a 75 73 74 54 72 65 65 28 70 52 74 72   AdjustTree(pRtr
367ac 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 6c 65  ee, pParent, &le
367ad 66 74 62 62 6f 78 29 3b 0a 20 20 7d 0a 20 20 69  ftbbox);.  }.  i
367ae 66 28 20 28 72 63 20 3d 20 72 74 72 65 65 49 6e  f( (rc = rtreeIn
367af 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
367b0 20 70 52 69 67 68 74 2d 3e 70 50 61 72 65 6e 74   pRight->pParent
367b1 2c 20 26 72 69 67 68 74 62 62 6f 78 2c 20 69 48  , &rightbbox, iH
367b2 65 69 67 68 74 2b 31 29 29 20 29 7b 0a 20 20 20  eight+1)) ){.   
367b3 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
367b4 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28  out;.  }..  for(
367b5 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28 70 52 69  i=0; i<NCELL(pRi
367b6 67 68 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ght); i++){.    
367b7 69 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64  i64 iRowid = nod
367b8 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
367b9 2c 20 70 52 69 67 68 74 2c 20 69 29 3b 0a 20 20  , pRight, i);.  
367ba 20 20 72 63 20 3d 20 75 70 64 61 74 65 4d 61 70    rc = updateMap
367bb 70 69 6e 67 28 70 52 74 72 65 65 2c 20 69 52 6f  ping(pRtree, iRo
367bc 77 69 64 2c 20 70 52 69 67 68 74 2c 20 69 48 65  wid, pRight, iHe
367bd 69 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 69  ight);.    if( i
367be 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52  Rowid==pCell->iR
367bf 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 6e 65  owid ){.      ne
367c0 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 31  wCellIsRight = 1
367c1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
367c2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
367c3 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  .      goto spli
367c4 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d  tnode_out;.    }
367c5 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 6f 64 65  .  }.  if( pNode
367c6 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20  ->iNode==1 ){.  
367c7 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45    for(i=0; i<NCE
367c8 4c 4c 28 70 4c 65 66 74 29 3b 20 69 2b 2b 29 7b  LL(pLeft); i++){
367c9 0a 20 20 20 20 20 20 69 36 34 20 69 52 6f 77 69  .      i64 iRowi
367ca 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64  d = nodeGetRowid
367cb 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20  (pRtree, pLeft, 
367cc 69 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 75  i);.      rc = u
367cd 70 64 61 74 65 4d 61 70 70 69 6e 67 28 70 52 74  pdateMapping(pRt
367ce 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 4c 65  ree, iRowid, pLe
367cf 66 74 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20  ft, iHeight);.  
367d0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
367d1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
367d2 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
367d3 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  out;.      }.   
367d4 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e   }.  }else if( n
367d5 65 77 43 65 6c 6c 49 73 52 69 67 68 74 3d 3d 30  ewCellIsRight==0
367d6 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 75 70 64   ){.    rc = upd
367d7 61 74 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65  ateMapping(pRtre
367d8 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  e, pCell->iRowid
367d9 2c 20 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74  , pLeft, iHeight
367da 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  );.  }..  if( rc
367db 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
367dc 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65     rc = nodeRele
367dd 61 73 65 28 70 52 74 72 65 65 2c 20 70 52 69 67  ase(pRtree, pRig
367de 68 74 29 3b 0a 20 20 20 20 70 52 69 67 68 74 20  ht);.    pRight 
367df 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  = 0;.  }.  if( r
367e0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
367e1 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c      rc = nodeRel
367e2 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
367e3 66 74 29 3b 0a 20 20 20 20 70 4c 65 66 74 20 3d  ft);.    pLeft =
367e4 20 30 3b 0a 20 20 7d 0a 0a 73 70 6c 69 74 6e 6f   0;.  }..splitno
367e5 64 65 5f 6f 75 74 3a 0a 20 20 6e 6f 64 65 52 65  de_out:.  nodeRe
367e6 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52  lease(pRtree, pR
367e7 69 67 68 74 29 3b 0a 20 20 6e 6f 64 65 52 65 6c  ight);.  nodeRel
367e8 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
367e9 66 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ft);.  sqlite3_f
367ea 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20 72 65  ree(aCell);.  re
367eb 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
367ec 69 63 20 69 6e 74 20 66 69 78 4c 65 61 66 50 61  ic int fixLeafPa
367ed 72 65 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72  rent(Rtree *pRtr
367ee 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
367ef 4c 65 61 66 29 7b 0a 20 20 69 6e 74 20 72 63 20  Leaf){.  int rc 
367f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
367f1 66 28 20 70 4c 65 61 66 2d 3e 69 4e 6f 64 65 21  f( pLeaf->iNode!
367f2 3d 31 20 26 26 20 70 4c 65 61 66 2d 3e 70 50 61  =1 && pLeaf->pPa
367f3 72 65 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73  rent==0 ){.    s
367f4 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
367f5 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50  4(pRtree->pReadP
367f6 61 72 65 6e 74 2c 20 31 2c 20 70 4c 65 61 66 2d  arent, 1, pLeaf-
367f7 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 69 66 28  >iNode);.    if(
367f8 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
367f9 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
367fa 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  t)==SQLITE_ROW )
367fb 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4e 6f 64  {.      i64 iNod
367fc 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
367fd 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d  mn_int64(pRtree-
367fe 3e 70 52 65 61 64 50 61 72 65 6e 74 2c 20 30 29  >pReadParent, 0)
367ff 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64  ;.      rc = nod
36800 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
36801 20 69 4e 6f 64 65 2c 20 30 2c 20 26 70 4c 65 61   iNode, 0, &pLea
36802 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20  f->pParent);.   
36803 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
36804 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
36805 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
36806 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d  e3_reset(pRtree-
36807 3e 70 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20  >pReadParent);. 
36808 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
36809 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
3680a 20 3d 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74   = fixLeafParent
3680b 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2d 3e  (pRtree, pLeaf->
3680c 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a  pParent);.    }.
3680d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
3680e 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .}..static int d
3680f 65 6c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20  eleteCell(Rtree 
36810 2a 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20  *, RtreeNode *, 
36811 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 73 74 61 74  int, int);..stat
36812 69 63 20 69 6e 74 20 72 65 6d 6f 76 65 4e 6f 64  ic int removeNod
36813 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
36814 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
36815 65 2c 20 69 6e 74 20 69 48 65 69 67 68 74 29 7b  e, int iHeight){
36816 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72  .  int rc;.  Rtr
36817 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b  eeNode *pParent;
36818 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b 0a 0a 20  .  int iCell;.. 
36819 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e   assert( pNode->
3681a 6e 52 65 66 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a  nRef==1 );..  /*
3681b 20 52 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 72   Remove the entr
3681c 79 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  y in the parent 
3681d 63 65 6c 6c 2e 20 2a 2f 0a 20 20 69 43 65 6c 6c  cell. */.  iCell
3681e 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64   = nodeParentInd
3681f 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ex(pRtree, pNode
36820 29 3b 0a 20 20 70 50 61 72 65 6e 74 20 3d 20 70  );.  pParent = p
36821 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  Node->pParent;. 
36822 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20   pNode->pParent 
36823 3d 20 30 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  = 0;.  if( SQLIT
36824 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 64 65 6c 65  E_OK!=(rc = dele
36825 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
36826 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20 69  Parent, iCell, i
36827 48 65 69 67 68 74 2b 31 29 29 20 0a 20 20 20 7c  Height+1)) .   |
36828 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  | SQLITE_OK!=(rc
36829 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
3682a 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 29 29  Rtree, pParent))
3682b 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
3682c 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52   rc;.  }..  /* R
3682d 65 6d 6f 76 65 20 74 68 65 20 78 78 78 5f 6e 6f  emove the xxx_no
3682e 64 65 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 73  de entry. */.  s
3682f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
36830 34 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  4(pRtree->pDelet
36831 65 4e 6f 64 65 2c 20 31 2c 20 70 4e 6f 64 65 2d  eNode, 1, pNode-
36832 3e 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74  >iNode);.  sqlit
36833 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e  e3_step(pRtree->
36834 70 44 65 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20  pDeleteNode);.  
36835 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
36836 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
36837 65 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  et(pRtree->pDele
36838 74 65 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20  teNode)) ){.    
36839 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
3683a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
3683b 78 78 78 5f 70 61 72 65 6e 74 20 65 6e 74 72 79  xxx_parent entry
3683c 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62  . */.  sqlite3_b
3683d 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
3683e 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74 2c  ->pDeleteParent,
3683f 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65   1, pNode->iNode
36840 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65  );.  sqlite3_ste
36841 70 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  p(pRtree->pDelet
36842 65 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20  eParent);.  if( 
36843 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
36844 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
36845 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
36846 72 65 6e 74 29 29 20 29 7b 0a 20 20 20 20 72 65  rent)) ){.    re
36847 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 0a  turn rc;.  }.  .
36848 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
36849 6e 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 69 6e  node from the in
3684a 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62  -memory hash tab
3684b 6c 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69  le and link it i
3684c 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 52 74 72  nto.  ** the Rtr
3684d 65 65 2e 70 44 65 6c 65 74 65 64 20 6c 69 73 74  ee.pDeleted list
3684e 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 77  . Its contents w
3684f 69 6c 6c 20 62 65 20 72 65 2d 69 6e 73 65 72 74  ill be re-insert
36850 65 64 20 6c 61 74 65 72 20 6f 6e 2e 0a 20 20 2a  ed later on..  *
36851 2f 0a 20 20 6e 6f 64 65 48 61 73 68 44 65 6c 65  /.  nodeHashDele
36852 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
36853 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  );.  pNode->iNod
36854 65 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 70  e = iHeight;.  p
36855 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 70 52  Node->pNext = pR
36856 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b 0a  tree->pDeleted;.
36857 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b    pNode->nRef++;
36858 0a 20 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  .  pRtree->pDele
36859 74 65 64 20 3d 20 70 4e 6f 64 65 3b 0a 0a 20 20  ted = pNode;..  
3685a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3685b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
3685c 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28   fixBoundingBox(
3685d 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
3685e 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29  treeNode *pNode)
3685f 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  {.  RtreeNode *p
36860 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e  Parent = pNode->
36861 70 50 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70  pParent;.  if( p
36862 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e  Parent ){.    in
36863 74 20 69 69 3b 20 0a 20 20 20 20 69 6e 74 20 6e  t ii; .    int n
36864 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
36865 64 65 29 3b 0a 20 20 20 20 52 74 72 65 65 43 65  de);.    RtreeCe
36866 6c 6c 20 62 6f 78 3b 20 20 20 20 20 20 20 20 20  ll box;         
36867 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36868 20 20 20 2f 2a 20 42 6f 75 6e 64 69 6e 67 20 62     /* Bounding b
36869 6f 78 20 66 6f 72 20 70 4e 6f 64 65 20 2a 2f 0a  ox for pNode */.
3686a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
3686b 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 30  pRtree, pNode, 0
3686c 2c 20 26 62 6f 78 29 3b 0a 20 20 20 20 66 6f 72  , &box);.    for
3686d 28 69 69 3d 31 3b 20 69 69 3c 6e 43 65 6c 6c 3b  (ii=1; ii<nCell;
3686e 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 52 74   ii++){.      Rt
3686f 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20  reeCell cell;.  
36870 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
36871 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
36872 69 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 20  i, &cell);.     
36873 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
36874 65 2c 20 26 62 6f 78 2c 20 26 63 65 6c 6c 29 3b  e, &box, &cell);
36875 0a 20 20 20 20 7d 0a 20 20 20 20 62 6f 78 2e 69  .    }.    box.i
36876 52 6f 77 69 64 20 3d 20 70 4e 6f 64 65 2d 3e 69  Rowid = pNode->i
36877 4e 6f 64 65 3b 0a 20 20 20 20 69 69 20 3d 20 6e  Node;.    ii = n
36878 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70  odeParentIndex(p
36879 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
3687a 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65     nodeOverwrite
3687b 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61  Cell(pRtree, pPa
3687c 72 65 6e 74 2c 20 26 62 6f 78 2c 20 69 69 29 3b  rent, &box, ii);
3687d 0a 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e 67  .    fixBounding
3687e 42 6f 78 28 70 52 74 72 65 65 2c 20 70 50 61 72  Box(pRtree, pPar
3687f 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ent);.  }.}../*.
36880 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 63 65  ** Delete the ce
36881 6c 6c 20 61 74 20 69 6e 64 65 78 20 69 43 65 6c  ll at index iCel
36882 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l of node pNode.
36883 20 41 66 74 65 72 20 72 65 6d 6f 76 69 6e 67 20   After removing 
36884 74 68 65 0a 2a 2a 20 63 65 6c 6c 2c 20 61 64 6a  the.** cell, adj
36885 75 73 74 20 74 68 65 20 72 2d 74 72 65 65 20 64  ust the r-tree d
36886 61 74 61 20 73 74 72 75 63 74 75 72 65 20 69 66  ata structure if
36887 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74   required..*/.st
36888 61 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65 43  atic int deleteC
36889 65 6c 6c 28 52 74 72 65 65 20 2a 70 52 74 72 65  ell(Rtree *pRtre
3688a 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  e, RtreeNode *pN
3688b 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c 6c 2c 20  ode, int iCell, 
3688c 69 6e 74 20 69 48 65 69 67 68 74 29 7b 0a 20 20  int iHeight){.  
3688d 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20 53  int rc;..  if( S
3688e 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
3688f 66 69 78 4c 65 61 66 50 61 72 65 6e 74 28 70 52  fixLeafParent(pR
36890 74 72 65 65 2c 20 70 4e 6f 64 65 29 29 20 29 7b  tree, pNode)) ){
36891 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
36892 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65    }..  /* Remove
36893 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74   the cell from t
36894 68 65 20 6e 6f 64 65 2e 20 54 68 69 73 20 63 61  he node. This ca
36895 6c 6c 20 6a 75 73 74 20 6d 6f 76 65 73 20 62 79  ll just moves by
36896 74 65 73 20 61 72 6f 75 6e 64 0a 20 20 2a 2a 20  tes around.  ** 
36897 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f  the in-memory no
36898 64 65 20 69 6d 61 67 65 2c 20 73 6f 20 69 74 20  de image, so it 
36899 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 2a  cannot fail..  *
3689a 2f 0a 20 20 6e 6f 64 65 44 65 6c 65 74 65 43 65  /.  nodeDeleteCe
3689b 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
3689c 2c 20 69 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20  , iCell);..  /* 
3689d 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 6e  If the node is n
3689e 6f 74 20 74 68 65 20 74 72 65 65 20 72 6f 6f 74  ot the tree root
3689f 20 61 6e 64 20 6e 6f 77 20 68 61 73 20 6c 65 73   and now has les
368a0 73 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d  s than the minim
368a1 75 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f  um.  ** number o
368a2 66 20 63 65 6c 6c 73 2c 20 72 65 6d 6f 76 65 20  f cells, remove 
368a3 69 74 20 66 72 6f 6d 20 74 68 65 20 74 72 65 65  it from the tree
368a4 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 75 70 64  . Otherwise, upd
368a5 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c  ate the.  ** cel
368a6 6c 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  l in the parent 
368a7 6e 6f 64 65 20 73 6f 20 74 68 61 74 20 69 74 20  node so that it 
368a8 74 69 67 68 74 6c 79 20 63 6f 6e 74 61 69 6e 73  tightly contains
368a9 20 74 68 65 20 75 70 64 61 74 65 64 0a 20 20 2a   the updated.  *
368aa 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69  * node..  */.  i
368ab 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21  f( pNode->iNode!
368ac 3d 31 20 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  =1 ){.    RtreeN
368ad 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  ode *pParent = p
368ae 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  Node->pParent;. 
368af 20 20 20 69 66 28 20 28 70 50 61 72 65 6e 74 2d     if( (pParent-
368b0 3e 69 4e 6f 64 65 21 3d 31 20 7c 7c 20 4e 43 45  >iNode!=1 || NCE
368b1 4c 4c 28 70 50 61 72 65 6e 74 29 21 3d 31 29 20  LL(pParent)!=1) 
368b2 0a 20 20 20 20 20 26 26 20 28 4e 43 45 4c 4c 28  .     && (NCELL(
368b3 70 4e 6f 64 65 29 3c 52 54 52 45 45 5f 4d 49 4e  pNode)<RTREE_MIN
368b4 43 45 4c 4c 53 28 70 52 74 72 65 65 29 29 0a 20  CELLS(pRtree)). 
368b5 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d     ){.      rc =
368b6 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72   removeNode(pRtr
368b7 65 65 2c 20 70 4e 6f 64 65 2c 20 69 48 65 69 67  ee, pNode, iHeig
368b8 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ht);.    }else{.
368b9 20 20 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e        fixBoundin
368ba 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  gBox(pRtree, pNo
368bb 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  de);.    }.  }..
368bc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
368bd 73 74 61 74 69 63 20 69 6e 74 20 52 65 69 6e 73  static int Reins
368be 65 72 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ert(.  Rtree *pR
368bf 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f  tree, .  RtreeNo
368c0 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74  de *pNode, .  Rt
368c1 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20  reeCell *pCell, 
368c2 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29  .  int iHeight.)
368c3 7b 0a 20 20 69 6e 74 20 2a 61 4f 72 64 65 72 3b  {.  int *aOrder;
368c4 0a 20 20 69 6e 74 20 2a 61 53 70 61 72 65 3b 0a  .  int *aSpare;.
368c5 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
368c6 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69  ll;.  float *aDi
368c7 73 74 61 6e 63 65 3b 0a 20 20 69 6e 74 20 6e 43  stance;.  int nC
368c8 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20 61 43 65  ell;.  float aCe
368c9 6e 74 65 72 43 6f 6f 72 64 5b 52 54 52 45 45 5f  nterCoord[RTREE_
368ca 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 5d 3b  MAX_DIMENSIONS];
368cb 0a 20 20 69 6e 74 20 69 44 69 6d 3b 0a 20 20 69  .  int iDim;.  i
368cc 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20  nt ii;.  int rc 
368cd 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
368ce 6d 65 6d 73 65 74 28 61 43 65 6e 74 65 72 43 6f  memset(aCenterCo
368cf 6f 72 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66  ord, 0, sizeof(f
368d0 6c 6f 61 74 29 2a 52 54 52 45 45 5f 4d 41 58 5f  loat)*RTREE_MAX_
368d1 44 49 4d 45 4e 53 49 4f 4e 53 29 3b 0a 0a 20 20  DIMENSIONS);..  
368d2 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e  nCell = NCELL(pN
368d3 6f 64 65 29 2b 31 3b 0a 0a 20 20 2f 2a 20 41 6c  ode)+1;..  /* Al
368d4 6c 6f 63 61 74 65 20 74 68 65 20 62 75 66 66 65  locate the buffe
368d5 72 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  rs used by this 
368d6 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 61  operation. The a
368d7 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 0a 20 20 2a  llocation is.  *
368d8 2a 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20 62  * relinquished b
368d9 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
368da 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a  ion returns..  *
368db 2f 0a 20 20 61 43 65 6c 6c 20 3d 20 28 52 74 72  /.  aCell = (Rtr
368dc 65 65 43 65 6c 6c 20 2a 29 73 71 6c 69 74 65 33  eeCell *)sqlite3
368dd 5f 6d 61 6c 6c 6f 63 28 6e 43 65 6c 6c 20 2a 20  _malloc(nCell * 
368de 28 0a 20 20 20 20 73 69 7a 65 6f 66 28 52 74 72  (.    sizeof(Rtr
368df 65 65 43 65 6c 6c 29 20 2b 20 20 20 20 20 20 20  eeCell) +       
368e0 20 20 2f 2a 20 61 43 65 6c 6c 20 61 72 72 61 79    /* aCell array
368e1 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 69   */.    sizeof(i
368e2 6e 74 29 20 20 20 20 20 20 20 2b 20 20 20 20 20  nt)       +     
368e3 20 20 20 20 2f 2a 20 61 4f 72 64 65 72 20 61 72      /* aOrder ar
368e4 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f  ray */.    sizeo
368e5 66 28 69 6e 74 29 20 20 20 20 20 20 20 2b 20 20  f(int)       +  
368e6 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 72 65         /* aSpare
368e7 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69   array */.    si
368e8 7a 65 6f 66 28 66 6c 6f 61 74 29 20 20 20 20 20  zeof(float)     
368e9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 44 69            /* aDi
368ea 73 74 61 6e 63 65 20 61 72 72 61 79 20 2a 2f 0a  stance array */.
368eb 20 20 29 29 3b 0a 20 20 69 66 28 20 21 61 43 65    ));.  if( !aCe
368ec 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
368ed 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
368ee 20 7d 0a 20 20 61 4f 72 64 65 72 20 20 20 20 3d   }.  aOrder    =
368ef 20 28 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e   (int *)&aCell[n
368f0 43 65 6c 6c 5d 3b 0a 20 20 61 53 70 61 72 65 20  Cell];.  aSpare 
368f1 20 20 20 3d 20 28 69 6e 74 20 2a 29 26 61 4f 72     = (int *)&aOr
368f2 64 65 72 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 44  der[nCell];.  aD
368f3 69 73 74 61 6e 63 65 20 3d 20 28 66 6c 6f 61 74  istance = (float
368f4 20 2a 29 26 61 53 70 61 72 65 5b 6e 43 65 6c 6c   *)&aSpare[nCell
368f5 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ];..  for(ii=0; 
368f6 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
368f7 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 28 6e 43  .    if( ii==(nC
368f8 65 6c 6c 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ell-1) ){.      
368f9 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c 5b 69 69  memcpy(&aCell[ii
368fa 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f 66  ], pCell, sizeof
368fb 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
368fc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
368fd 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65  odeGetCell(pRtre
368fe 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 61  e, pNode, ii, &a
368ff 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 7d  Cell[ii]);.    }
36900 0a 20 20 20 20 61 4f 72 64 65 72 5b 69 69 5d 20  .    aOrder[ii] 
36901 3d 20 69 69 3b 0a 20 20 20 20 66 6f 72 28 69 44  = ii;.    for(iD
36902 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65  im=0; iDim<pRtre
36903 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29  e->nDim; iDim++)
36904 7b 0a 20 20 20 20 20 20 61 43 65 6e 74 65 72 43  {.      aCenterC
36905 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44 43  oord[iDim] += DC
36906 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61  OORD(aCell[ii].a
36907 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a  Coord[iDim*2]);.
36908 20 20 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f        aCenterCoo
36909 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f  rd[iDim] += DCOO
3690a 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f  RD(aCell[ii].aCo
3690b 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a  ord[iDim*2+1]);.
3690c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28      }.  }.  for(
3690d 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74  iDim=0; iDim<pRt
3690e 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b  ree->nDim; iDim+
3690f 2b 29 7b 0a 20 20 20 20 61 43 65 6e 74 65 72 43  +){.    aCenterC
36910 6f 6f 72 64 5b 69 44 69 6d 5d 20 3d 20 61 43 65  oord[iDim] = aCe
36911 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 2f  nterCoord[iDim]/
36912 28 28 66 6c 6f 61 74 29 6e 43 65 6c 6c 2a 32 2e  ((float)nCell*2.
36913 30 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69  0);.  }..  for(i
36914 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
36915 69 2b 2b 29 7b 0a 20 20 20 20 61 44 69 73 74 61  i++){.    aDista
36916 6e 63 65 5b 69 69 5d 20 3d 20 30 2e 30 3b 0a 20  nce[ii] = 0.0;. 
36917 20 20 20 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69     for(iDim=0; i
36918 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d  Dim<pRtree->nDim
36919 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20 20 20 20  ; iDim++){.     
3691a 20 66 6c 6f 61 74 20 63 6f 6f 72 64 20 3d 20 44   float coord = D
3691b 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
3691c 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
3691d 29 20 2d 20 0a 20 20 20 20 20 20 20 20 20 20 44  ) - .          D
3691e 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
3691f 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b  aCoord[iDim*2]);
36920 0a 20 20 20 20 20 20 61 44 69 73 74 61 6e 63 65  .      aDistance
36921 5b 69 69 5d 20 2b 3d 20 28 63 6f 6f 72 64 2d 61  [ii] += (coord-a
36922 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d  CenterCoord[iDim
36923 5d 29 2a 28 63 6f 6f 72 64 2d 61 43 65 6e 74 65  ])*(coord-aCente
36924 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29 3b 0a 20  rCoord[iDim]);. 
36925 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 53 6f 72 74     }.  }..  Sort
36926 42 79 44 69 73 74 61 6e 63 65 28 61 4f 72 64 65  ByDistance(aOrde
36927 72 2c 20 6e 43 65 6c 6c 2c 20 61 44 69 73 74 61  r, nCell, aDista
36928 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a 20 20  nce, aSpare);.  
36929 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72 65 65 2c  nodeZero(pRtree,
3692a 20 70 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28   pNode);..  for(
3692b 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  ii=0; rc==SQLITE
3692c 5f 4f 4b 20 26 26 20 69 69 3c 28 6e 43 65 6c 6c  _OK && ii<(nCell
3692d 2d 28 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53  -(RTREE_MINCELLS
3692e 28 70 52 74 72 65 65 29 2b 31 29 29 3b 20 69 69  (pRtree)+1)); ii
3692f 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 65  ++){.    RtreeCe
36930 6c 6c 20 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61  ll *p = &aCell[a
36931 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20  Order[ii]];.    
36932 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  nodeInsertCell(p
36933 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 29  Rtree, pNode, p)
36934 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 6f  ;.    if( p->iRo
36935 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77  wid==pCell->iRow
36936 69 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  id ){.      if( 
36937 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20  iHeight==0 ){.  
36938 20 20 20 20 20 20 72 63 20 3d 20 72 6f 77 69 64        rc = rowid
36939 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 2d  Write(pRtree, p-
3693a 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e  >iRowid, pNode->
3693b 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 65  iNode);.      }e
3693c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20  lse{.        rc 
3693d 3d 20 70 61 72 65 6e 74 57 72 69 74 65 28 70 52  = parentWrite(pR
3693e 74 72 65 65 2c 20 70 2d 3e 69 52 6f 77 69 64 2c  tree, p->iRowid,
3693f 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a   pNode->iNode);.
36940 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
36941 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
36942 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 66 69 78  TE_OK ){.    fix
36943 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70 52 74 72  BoundingBox(pRtr
36944 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 7d 0a  ee, pNode);.  }.
36945 20 20 66 6f 72 28 3b 20 72 63 3d 3d 53 51 4c 49    for(; rc==SQLI
36946 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 43 65 6c  TE_OK && ii<nCel
36947 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a  l; ii++){.    /*
36948 20 46 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20   Find a node to 
36949 73 74 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20  store this cell 
3694a 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  in. pNode->iNode
3694b 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
3694c 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68  ins.    ** the h
3694d 65 69 67 68 74 20 6f 66 20 74 68 65 20 73 75 62  eight of the sub
3694e 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20  -tree headed by 
3694f 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f  the cell..    */
36950 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
36951 70 49 6e 73 65 72 74 3b 0a 20 20 20 20 52 74 72  pInsert;.    Rtr
36952 65 65 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65  eeCell *p = &aCe
36953 6c 6c 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a  ll[aOrder[ii]];.
36954 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c      rc = ChooseL
36955 65 61 66 28 70 52 74 72 65 65 2c 20 70 2c 20 69  eaf(pRtree, p, i
36956 48 65 69 67 68 74 2c 20 26 70 49 6e 73 65 72 74  Height, &pInsert
36957 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
36958 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
36959 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20    int rc2;.     
3695a 20 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72   rc = rtreeInser
3695b 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 49  tCell(pRtree, pI
3695c 6e 73 65 72 74 2c 20 70 2c 20 69 48 65 69 67 68  nsert, p, iHeigh
3695d 74 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20  t);.      rc2 = 
3695e 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
3695f 65 65 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20  ee, pInsert);.  
36960 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
36961 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
36962 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20   rc = rc2;.     
36963 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
36964 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65  sqlite3_free(aCe
36965 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ll);.  return rc
36966 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72  ;.}../*.** Inser
36967 74 20 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e 74  t cell pCell int
36968 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 4e 6f  o node pNode. No
36969 64 65 20 70 4e 6f 64 65 20 69 73 20 74 68 65 20  de pNode is the 
3696a 68 65 61 64 20 6f 66 20 61 20 0a 2a 2a 20 73 75  head of a .** su
3696b 62 74 72 65 65 20 69 48 65 69 67 68 74 20 68 69  btree iHeight hi
3696c 67 68 20 28 6c 65 61 66 20 6e 6f 64 65 73 20 68  gh (leaf nodes h
3696d 61 76 65 20 69 48 65 69 67 68 74 3d 3d 30 29 2e  ave iHeight==0).
3696e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
3696f 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 0a  treeInsertCell(.
36970 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
36971 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
36972 6f 64 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ode,.  RtreeCell
36973 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69   *pCell,.  int i
36974 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20  Height.){.  int 
36975 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
36976 20 20 69 66 28 20 69 48 65 69 67 68 74 3e 30 20    if( iHeight>0 
36977 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  ){.    RtreeNode
36978 20 2a 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65 48   *pChild = nodeH
36979 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65  ashLookup(pRtree
3697a 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 29  , pCell->iRowid)
3697b 3b 0a 20 20 20 20 69 66 28 20 70 43 68 69 6c 64  ;.    if( pChild
3697c 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65   ){.      nodeRe
3697d 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43  lease(pRtree, pC
3697e 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  hild->pParent);.
3697f 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65        nodeRefere
36980 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  nce(pNode);.    
36981 20 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e    pChild->pParen
36982 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 7d  t = pNode;.    }
36983 0a 20 20 7d 0a 20 20 69 66 28 20 6e 6f 64 65 49  .  }.  if( nodeI
36984 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
36985 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29 20  , pNode, pCell) 
36986 29 7b 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  ){.#if VARIANT_R
36987 53 54 41 52 54 52 45 45 5f 52 45 49 4e 53 45 52  STARTREE_REINSER
36988 54 0a 20 20 20 20 69 66 28 20 69 48 65 69 67 68  T.    if( iHeigh
36989 74 3c 3d 70 52 74 72 65 65 2d 3e 69 52 65 69 6e  t<=pRtree->iRein
3698a 73 65 72 74 48 65 69 67 68 74 20 7c 7c 20 70 4e  sertHeight || pN
3698b 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 29 7b 0a  ode->iNode==1){.
3698c 20 20 20 20 20 20 72 63 20 3d 20 53 70 6c 69 74        rc = Split
3698d 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  Node(pRtree, pNo
3698e 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69 67  de, pCell, iHeig
3698f 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ht);.    }else{.
36990 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52        pRtree->iR
36991 65 69 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20  einsertHeight = 
36992 69 48 65 69 67 68 74 3b 0a 20 20 20 20 20 20 72  iHeight;.      r
36993 63 20 3d 20 52 65 69 6e 73 65 72 74 28 70 52 74  c = Reinsert(pRt
36994 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c  ree, pNode, pCel
36995 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20  l, iHeight);.   
36996 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20   }.#else.    rc 
36997 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70 52 74 72  = SplitNode(pRtr
36998 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c  ee, pNode, pCell
36999 2c 20 69 48 65 69 67 68 74 29 3b 0a 23 65 6e 64  , iHeight);.#end
3699a 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  if.  }else{.    
3699b 41 64 6a 75 73 74 54 72 65 65 28 70 52 74 72 65  AdjustTree(pRtre
3699c 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29  e, pNode, pCell)
3699d 3b 0a 20 20 20 20 69 66 28 20 69 48 65 69 67 68  ;.    if( iHeigh
3699e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  t==0 ){.      rc
3699f 20 3d 20 72 6f 77 69 64 57 72 69 74 65 28 70 52   = rowidWrite(pR
369a0 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f  tree, pCell->iRo
369a1 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  wid, pNode->iNod
369a2 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
369a3 20 20 20 20 20 72 63 20 3d 20 70 61 72 65 6e 74       rc = parent
369a4 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 43  Write(pRtree, pC
369a5 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f  ell->iRowid, pNo
369a6 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
369a7 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
369a8 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  c;.}..static int
369a9 20 72 65 69 6e 73 65 72 74 4e 6f 64 65 43 6f 6e   reinsertNodeCon
369aa 74 65 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72  tent(Rtree *pRtr
369ab 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
369ac 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b  Node){.  int ii;
369ad 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
369ae 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 43 65  TE_OK;.  int nCe
369af 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
369b0 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
369b1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
369b2 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29   ii<nCell; ii++)
369b3 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  {.    RtreeNode 
369b4 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20 20 52 74  *pInsert;.    Rt
369b5 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20  reeCell cell;.  
369b6 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
369b7 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c  tree, pNode, ii,
369b8 20 26 63 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a   &cell);..    /*
369b9 20 46 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20   Find a node to 
369ba 73 74 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20  store this cell 
369bb 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  in. pNode->iNode
369bc 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
369bd 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68  ins.    ** the h
369be 65 69 67 68 74 20 6f 66 20 74 68 65 20 73 75 62  eight of the sub
369bf 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20  -tree headed by 
369c0 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f  the cell..    */
369c1 0a 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65  .    rc = Choose
369c2 4c 65 61 66 28 70 52 74 72 65 65 2c 20 26 63 65  Leaf(pRtree, &ce
369c3 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  ll, pNode->iNode
369c4 2c 20 26 70 49 6e 73 65 72 74 29 3b 0a 20 20 20  , &pInsert);.   
369c5 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
369c6 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
369c7 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rc2;.      rc = 
369c8 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28  rtreeInsertCell(
369c9 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 2c  pRtree, pInsert,
369ca 20 26 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69   &cell, pNode->i
369cb 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 72 63 32  Node);.      rc2
369cc 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
369cd 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 29 3b  Rtree, pInsert);
369ce 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
369cf 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
369d0 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
369d1 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
369d2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
369d3 2f 2a 0a 2a 2a 20 53 65 6c 65 63 74 20 61 20 63  /*.** Select a c
369d4 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 20  urrently unused 
369d5 72 6f 77 69 64 20 66 6f 72 20 61 20 6e 65 77 20  rowid for a new 
369d6 72 2d 74 72 65 65 20 72 65 63 6f 72 64 2e 0a 2a  r-tree record..*
369d7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 77  /.static int new
369d8 52 6f 77 69 64 28 52 74 72 65 65 20 2a 70 52 74  Rowid(Rtree *pRt
369d9 72 65 65 2c 20 69 36 34 20 2a 70 69 52 6f 77 69  ree, i64 *piRowi
369da 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  d){.  int rc;.  
369db 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
369dc 6c 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  l(pRtree->pWrite
369dd 52 6f 77 69 64 2c 20 31 29 3b 0a 20 20 73 71 6c  Rowid, 1);.  sql
369de 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70  ite3_bind_null(p
369df 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
369e0 69 64 2c 20 32 29 3b 0a 20 20 73 71 6c 69 74 65  id, 2);.  sqlite
369e1 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70  3_step(pRtree->p
369e2 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 72  WriteRowid);.  r
369e3 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
369e4 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  t(pRtree->pWrite
369e5 52 6f 77 69 64 29 3b 0a 20 20 2a 70 69 52 6f 77  Rowid);.  *piRow
369e6 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73  id = sqlite3_las
369e7 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70  t_insert_rowid(p
369e8 52 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 72 65  Rtree->db);.  re
369e9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e  turn rc;.}..#ifn
369ea 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69  def NDEBUG.stati
369eb 63 20 69 6e 74 20 68 61 73 68 49 73 45 6d 70 74  c int hashIsEmpt
369ec 79 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 29  y(Rtree *pRtree)
369ed 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f  {.  int ii;.  fo
369ee 72 28 69 69 3d 30 3b 20 69 69 3c 48 41 53 48 53  r(ii=0; ii<HASHS
369ef 49 5a 45 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  IZE; ii++){.    
369f0 61 73 73 65 72 74 28 20 21 70 52 74 72 65 65 2d  assert( !pRtree-
369f1 3e 61 48 61 73 68 5b 69 69 5d 20 29 3b 0a 20 20  >aHash[ii] );.  
369f2 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
369f3 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
369f4 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64  e xUpdate method
369f5 20 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75 6c   for rtree modul
369f6 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  e virtual tables
369f7 2e 0a 2a 2f 0a 69 6e 74 20 72 74 72 65 65 55 70  ..*/.int rtreeUp
369f8 64 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  date(.  sqlite3_
369f9 76 74 61 62 20 2a 70 56 74 61 62 2c 20 0a 20 20  vtab *pVtab, .  
369fa 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 73 71  int nData, .  sq
369fb 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 7a  lite3_value **az
369fc 44 61 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 5f  Data, .  sqlite_
369fd 69 6e 74 36 34 20 2a 70 52 6f 77 69 64 0a 29 7b  int64 *pRowid.){
369fe 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
369ff 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56 74 61   = (Rtree *)pVta
36a00 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  b;.  int rc = SQ
36a01 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65  LITE_OK;..  rtre
36a02 65 52 65 66 65 72 65 6e 63 65 28 70 52 74 72 65  eReference(pRtre
36a03 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 44  e);..  assert(nD
36a04 61 74 61 3e 3d 31 29 3b 0a 20 20 61 73 73 65 72  ata>=1);.  asser
36a05 74 28 68 61 73 68 49 73 45 6d 70 74 79 28 70 52  t(hashIsEmpty(pR
36a06 74 72 65 65 29 29 3b 0a 0a 20 20 2f 2a 20 49 66  tree));..  /* If
36a07 20 61 7a 44 61 74 61 5b 30 5d 20 69 73 20 6e 6f   azData[0] is no
36a08 74 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61  t an SQL NULL va
36a09 6c 75 65 2c 20 69 74 20 69 73 20 74 68 65 20 72  lue, it is the r
36a0a 6f 77 69 64 20 6f 66 20 61 0a 20 20 2a 2a 20 72  owid of a.  ** r
36a0b 65 63 6f 72 64 20 74 6f 20 64 65 6c 65 74 65 20  ecord to delete 
36a0c 66 72 6f 6d 20 74 68 65 20 72 2d 74 72 65 65 20  from the r-tree 
36a0d 74 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f  table. The follo
36a0e 77 69 6e 67 20 62 6c 6f 63 6b 20 64 6f 65 73 0a  wing block does.
36a0f 20 20 2a 2a 20 6a 75 73 74 20 74 68 61 74 2e 0a    ** just that..
36a10 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
36a11 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a  e3_value_type(az
36a12 44 61 74 61 5b 30 5d 29 21 3d 53 51 4c 49 54 45  Data[0])!=SQLITE
36a13 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 36 34  _NULL ){.    i64
36a14 20 69 44 65 6c 65 74 65 3b 20 20 20 20 20 20 20   iDelete;       
36a15 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
36a16 72 6f 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20  rowid to delete 
36a17 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  */.    RtreeNode
36a18 20 2a 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20   *pLeaf;        
36a19 20 20 20 2f 2a 20 4c 65 61 66 20 6e 6f 64 65 20     /* Leaf node 
36a1a 63 6f 6e 74 61 69 6e 69 6e 67 20 72 65 63 6f 72  containing recor
36a1b 64 20 69 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20  d iDelete */.   
36a1c 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20 20 20   int iCell;     
36a1d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36a1e 49 6e 64 65 78 20 6f 66 20 69 44 65 6c 65 74 65  Index of iDelete
36a1f 20 63 65 6c 6c 20 69 6e 20 70 4c 65 61 66 20 2a   cell in pLeaf *
36a20 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  /.    RtreeNode 
36a21 2a 70 52 6f 6f 74 3b 0a 0a 20 20 20 20 2f 2a 20  *pRoot;..    /* 
36a22 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e  Obtain a referen
36a23 63 65 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e  ce to the root n
36a24 6f 64 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 73  ode to initialis
36a25 65 20 52 74 72 65 65 2e 69 44 65 70 74 68 20 2a  e Rtree.iDepth *
36a26 2f 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41  /.    rc = nodeA
36a27 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 31  cquire(pRtree, 1
36a28 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b 0a 0a 20  , 0, &pRoot);.. 
36a29 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 72     /* Obtain a r
36a2a 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
36a2b 6c 65 61 66 20 6e 6f 64 65 20 74 68 61 74 20 63  leaf node that c
36a2c 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72  ontains the entr
36a2d 79 20 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20  y .    ** about 
36a2e 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 0a  to be deleted. .
36a2f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
36a30 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
36a31 20 20 20 20 20 20 69 44 65 6c 65 74 65 20 3d 20        iDelete = 
36a32 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
36a33 74 36 34 28 61 7a 44 61 74 61 5b 30 5d 29 3b 0a  t64(azData[0]);.
36a34 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c        rc = findL
36a35 65 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  eafNode(pRtree, 
36a36 69 44 65 6c 65 74 65 2c 20 26 70 4c 65 61 66 29  iDelete, &pLeaf)
36a37 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
36a38 44 65 6c 65 74 65 20 74 68 65 20 63 65 6c 6c 20  Delete the cell 
36a39 69 6e 20 71 75 65 73 74 69 6f 6e 20 66 72 6f 6d  in question from
36a3a 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20   the leaf node. 
36a3b 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
36a3c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
36a3d 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20    int rc2;.     
36a3e 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f 77   iCell = nodeRow
36a3f 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20  idIndex(pRtree, 
36a40 70 4c 65 61 66 2c 20 69 44 65 6c 65 74 65 29 3b  pLeaf, iDelete);
36a41 0a 20 20 20 20 20 20 72 63 20 3d 20 64 65 6c 65  .      rc = dele
36a42 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
36a43 4c 65 61 66 2c 20 69 43 65 6c 6c 2c 20 30 29 3b  Leaf, iCell, 0);
36a44 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64  .      rc2 = nod
36a45 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
36a46 20 70 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 69   pLeaf);.      i
36a47 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
36a48 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
36a49 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20   rc2;.      }.  
36a4a 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65    }..    /* Dele
36a4b 74 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  te the correspon
36a4c 64 69 6e 67 20 65 6e 74 72 79 20 69 6e 20 74 68  ding entry in th
36a4d 65 20 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20  e <rtree>_rowid 
36a4e 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66  table. */.    if
36a4f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
36a50 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
36a51 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
36a52 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
36a53 2c 20 31 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20  , 1, iDelete);. 
36a54 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65       sqlite3_ste
36a55 70 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  p(pRtree->pDelet
36a56 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 72  eRowid);.      r
36a57 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
36a58 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  t(pRtree->pDelet
36a59 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a  eRowid);.    }..
36a5a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20      /* Check if 
36a5b 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6e 6f  the root node no
36a5c 77 20 68 61 73 20 65 78 61 63 74 6c 79 20 6f 6e  w has exactly on
36a5d 65 20 63 68 69 6c 64 2e 20 49 66 20 73 6f 2c 20  e child. If so, 
36a5e 72 65 6d 6f 76 65 0a 20 20 20 20 2a 2a 20 69 74  remove.    ** it
36a5f 2c 20 73 63 68 65 64 75 6c 65 20 74 68 65 20 63  , schedule the c
36a60 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63  ontents of the c
36a61 68 69 6c 64 20 66 6f 72 20 72 65 69 6e 73 65 72  hild for reinser
36a62 74 69 6f 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a  tion and .    **
36a63 20 72 65 64 75 63 65 20 74 68 65 20 74 72 65 65   reduce the tree
36a64 20 68 65 69 67 68 74 20 62 79 20 6f 6e 65 2e 0a   height by one..
36a65 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
36a66 69 73 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  is is equivalent
36a67 20 74 6f 20 63 6f 70 79 69 6e 67 20 74 68 65 20   to copying the 
36a68 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
36a69 63 68 69 6c 64 20 69 6e 74 6f 0a 20 20 20 20 2a  child into.    *
36a6a 2a 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20  * the root node 
36a6b 28 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74  (the operation t
36a6c 68 61 74 20 47 75 74 6d 61 6e 27 73 20 70 61 70  hat Gutman's pap
36a6d 65 72 20 73 61 79 73 20 74 6f 20 70 65 72 66 6f  er says to perfo
36a6e 72 6d 20 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68  rm .    ** in th
36a6f 69 73 20 73 63 65 6e 61 72 69 6f 29 2e 0a 20 20  is scenario)..  
36a70 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
36a71 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 52  =SQLITE_OK && pR
36a72 74 72 65 65 2d 3e 69 44 65 70 74 68 3e 30 20 29  tree->iDepth>0 )
36a73 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  {.      if( rc==
36a74 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 4e 43 45  SQLITE_OK && NCE
36a75 4c 4c 28 70 52 6f 6f 74 29 3d 3d 31 20 29 7b 0a  LL(pRoot)==1 ){.
36a76 20 20 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64          RtreeNod
36a77 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20 20 20 20  e *pChild;.     
36a78 20 20 20 69 36 34 20 69 43 68 69 6c 64 20 3d 20     i64 iChild = 
36a79 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
36a7a 72 65 65 2c 20 70 52 6f 6f 74 2c 20 30 29 3b 0a  ree, pRoot, 0);.
36a7b 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64          rc = nod
36a7c 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
36a7d 20 69 43 68 69 6c 64 2c 20 70 52 6f 6f 74 2c 20   iChild, pRoot, 
36a7e 26 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20  &pChild);.      
36a7f 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
36a80 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
36a81 20 72 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65   rc = removeNode
36a82 28 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2c  (pRtree, pChild,
36a83 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d   pRtree->iDepth-
36a84 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1);.        }.  
36a85 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
36a86 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
36a87 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65       pRtree->iDe
36a88 70 74 68 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20  pth--;.         
36a89 20 77 72 69 74 65 49 6e 74 31 36 28 70 52 6f 6f   writeInt16(pRoo
36a8a 74 2d 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65  t->zData, pRtree
36a8b 2d 3e 69 44 65 70 74 68 29 3b 0a 20 20 20 20 20  ->iDepth);.     
36a8c 20 20 20 20 20 70 52 6f 6f 74 2d 3e 69 73 44 69       pRoot->isDi
36a8d 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  rty = 1;.       
36a8e 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
36a8f 0a 0a 20 20 20 20 2f 2a 20 52 65 2d 69 6e 73 65  ..    /* Re-inse
36a90 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rt the contents 
36a91 6f 66 20 61 6e 79 20 75 6e 64 65 72 66 75 6c 6c  of any underfull
36a92 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65 64 20 66   nodes removed f
36a93 72 6f 6d 20 74 68 65 20 74 72 65 65 2e 20 2a 2f  rom the tree. */
36a94 0a 20 20 20 20 66 6f 72 28 70 4c 65 61 66 3d 70  .    for(pLeaf=p
36a95 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b  Rtree->pDeleted;
36a96 20 70 4c 65 61 66 3b 20 70 4c 65 61 66 3d 70 52   pLeaf; pLeaf=pR
36a97 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 29 7b  tree->pDeleted){
36a98 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
36a99 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
36a9a 20 20 20 20 72 63 20 3d 20 72 65 69 6e 73 65 72      rc = reinser
36a9b 74 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52 74  tNodeContent(pRt
36a9c 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20  ree, pLeaf);.   
36a9d 20 20 20 7d 0a 20 20 20 20 20 20 70 52 74 72 65     }.      pRtre
36a9e 65 2d 3e 70 44 65 6c 65 74 65 64 20 3d 20 70 4c  e->pDeleted = pL
36a9f 65 61 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  eaf->pNext;.    
36aa0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
36aa1 4c 65 61 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Leaf);.    }..  
36aa2 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65    /* Release the
36aa3 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
36aa4 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 20 2a 2f 0a  e root node. */.
36aa5 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
36aa6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
36aa7 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
36aa8 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a  pRtree, pRoot);.
36aa9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36aaa 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
36aab 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20  ree, pRoot);.   
36aac 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
36aad 74 68 65 20 61 7a 44 61 74 61 5b 5d 20 61 72 72  the azData[] arr
36aae 61 79 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65  ay contains more
36aaf 20 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65 6e   than one elemen
36ab0 74 2c 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a  t, elements.  **
36ab1 20 28 61 7a 44 61 74 61 5b 32 5d 2e 2e 61 7a 44   (azData[2]..azD
36ab2 61 74 61 5b 61 72 67 63 2d 31 5d 29 20 63 6f 6e  ata[argc-1]) con
36ab3 74 61 69 6e 20 61 20 6e 65 77 20 72 65 63 6f 72  tain a new recor
36ab4 64 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f  d to insert into
36ab5 0a 20 20 2a 2a 20 74 68 65 20 72 2d 74 72 65 65  .  ** the r-tree
36ab6 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2f   structure..  */
36ab7 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
36ab8 45 5f 4f 4b 20 26 26 20 6e 44 61 74 61 3e 31 20  E_OK && nData>1 
36ab9 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  ){.    /* Insert
36aba 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e   a new record in
36abb 74 6f 20 74 68 65 20 72 2d 74 72 65 65 20 2a 2f  to the r-tree */
36abc 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63  .    RtreeCell c
36abd 65 6c 6c 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b  ell;.    int ii;
36abe 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
36abf 70 4c 65 61 66 3b 0a 0a 20 20 20 20 2f 2a 20 50  pLeaf;..    /* P
36ac0 6f 70 75 6c 61 74 65 20 74 68 65 20 63 65 6c 6c  opulate the cell
36ac1 2e 61 43 6f 6f 72 64 5b 5d 20 61 72 72 61 79 2e  .aCoord[] array.
36ac2 20 54 68 65 20 66 69 72 73 74 20 63 6f 6f 72 64   The first coord
36ac3 69 6e 61 74 65 20 69 73 20 61 7a 44 61 74 61 5b  inate is azData[
36ac4 33 5d 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  3]. */.    asser
36ac5 74 28 20 6e 44 61 74 61 3d 3d 28 70 52 74 72 65  t( nData==(pRtre
36ac6 65 2d 3e 6e 44 69 6d 2a 32 20 2b 20 33 29 20 29  e->nDim*2 + 3) )
36ac7 3b 0a 20 20 20 20 69 66 28 20 70 52 74 72 65 65  ;.    if( pRtree
36ac8 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
36ac9 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32  REE_COORD_REAL32
36aca 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 69   ){.      for(ii
36acb 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e  =0; ii<(pRtree->
36acc 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b  nDim*2); ii+=2){
36acd 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43  .        cell.aC
36ace 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 28 66 6c  oord[ii].f = (fl
36acf 6f 61 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  oat)sqlite3_valu
36ad0 65 5f 64 6f 75 62 6c 65 28 61 7a 44 61 74 61 5b  e_double(azData[
36ad1 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20  ii+3]);.        
36ad2 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31  cell.aCoord[ii+1
36ad3 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73 71 6c  ].f = (float)sql
36ad4 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
36ad5 65 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d 29 3b  e(azData[ii+4]);
36ad6 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 65 6c  .        if( cel
36ad7 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 3e 63  l.aCoord[ii].f>c
36ad8 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  ell.aCoord[ii+1]
36ad9 2e 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  .f ){.          
36ada 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53  rc = SQLITE_CONS
36adb 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20  TRAINT;.        
36adc 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61 69 6e    goto constrain
36add 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
36ade 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
36adf 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20        for(ii=0; 
36ae0 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
36ae1 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
36ae2 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64       cell.aCoord
36ae3 5b 69 69 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33  [ii].i = sqlite3
36ae4 5f 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74  _value_int(azDat
36ae5 61 5b 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20  a[ii+3]);.      
36ae6 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69    cell.aCoord[ii
36ae7 2b 31 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f  +1].i = sqlite3_
36ae8 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74 61  value_int(azData
36ae9 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20 20 20 20  [ii+4]);.       
36aea 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f 6f 72 64   if( cell.aCoord
36aeb 5b 69 69 5d 2e 69 3e 63 65 6c 6c 2e 61 43 6f 6f  [ii].i>cell.aCoo
36aec 72 64 5b 69 69 2b 31 5d 2e 69 20 29 7b 0a 20 20  rd[ii+1].i ){.  
36aed 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
36aee 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a  ITE_CONSTRAINT;.
36aef 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63            goto c
36af0 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20  onstraint;.     
36af1 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
36af2 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72   }..    /* Figur
36af3 65 20 6f 75 74 20 74 68 65 20 72 6f 77 69 64 20  e out the rowid 
36af4 6f 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 20  of the new row. 
36af5 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
36af6 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a  e3_value_type(az
36af7 44 61 74 61 5b 32 5d 29 3d 3d 53 51 4c 49 54 45  Data[2])==SQLITE
36af8 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72  _NULL ){.      r
36af9 63 20 3d 20 6e 65 77 52 6f 77 69 64 28 70 52 74  c = newRowid(pRt
36afa 72 65 65 2c 20 26 63 65 6c 6c 2e 69 52 6f 77 69  ree, &cell.iRowi
36afb 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
36afc 20 20 20 20 20 63 65 6c 6c 2e 69 52 6f 77 69 64       cell.iRowid
36afd 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
36afe 5f 69 6e 74 36 34 28 61 7a 44 61 74 61 5b 32 5d  _int64(azData[2]
36aff 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
36b00 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
36b01 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20  ee->pReadRowid, 
36b02 31 2c 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b  1, cell.iRowid);
36b03 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
36b04 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
36b05 74 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61  tep(pRtree->pRea
36b06 64 52 6f 77 69 64 29 20 29 7b 0a 20 20 20 20 20  dRowid) ){.     
36b07 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
36b08 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
36b09 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 72 63  wid);.        rc
36b0a 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52   = SQLITE_CONSTR
36b0b 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 67 6f  AINT;.        go
36b0c 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20  to constraint;. 
36b0d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
36b0e 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
36b0f 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
36b10 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  id);.    }..    
36b11 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
36b12 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
36b13 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65  ChooseLeaf(pRtre
36b14 65 2c 20 26 63 65 6c 6c 2c 20 30 2c 20 26 70 4c  e, &cell, 0, &pL
36b15 65 61 66 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  eaf);.    }.    
36b16 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
36b17 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  K ){.      int r
36b18 63 32 3b 0a 20 20 20 20 20 20 70 52 74 72 65 65  c2;.      pRtree
36b19 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67 68  ->iReinsertHeigh
36b1a 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 63  t = -1;.      rc
36b1b 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65   = rtreeInsertCe
36b1c 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  ll(pRtree, pLeaf
36b1d 2c 20 26 63 65 6c 6c 2c 20 30 29 3b 0a 20 20 20  , &cell, 0);.   
36b1e 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c     rc2 = nodeRel
36b1f 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
36b20 61 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  af);.      if( r
36b21 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
36b22 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32          rc = rc2
36b23 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
36b24 20 20 7d 0a 0a 63 6f 6e 73 74 72 61 69 6e 74 3a    }..constraint:
36b25 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  .  rtreeRelease(
36b26 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74 75 72  pRtree);.  retur
36b27 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
36b28 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f  he xRename metho
36b29 64 20 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75  d for rtree modu
36b2a 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  le virtual table
36b2b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
36b2c 20 72 74 72 65 65 52 65 6e 61 6d 65 28 73 71 6c   rtreeRename(sql
36b2d 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
36b2e 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
36b2f 65 77 4e 61 6d 65 29 7b 0a 20 20 52 74 72 65 65  ewName){.  Rtree
36b30 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65   *pRtree = (Rtre
36b31 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74  e *)pVtab;.  int
36b32 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
36b33 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  EM;.  char *zSql
36b34 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
36b35 74 66 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54  tf(.    "ALTER T
36b36 41 42 4c 45 20 25 51 2e 27 25 71 5f 6e 6f 64 65  ABLE %Q.'%q_node
36b37 27 20 20 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22  '   RENAME TO \"
36b38 25 77 5f 6e 6f 64 65 5c 22 3b 22 0a 20 20 20 20  %w_node\";".    
36b39 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e  "ALTER TABLE %Q.
36b3a 27 25 71 5f 70 61 72 65 6e 74 27 20 52 45 4e 41  '%q_parent' RENA
36b3b 4d 45 20 54 4f 20 5c 22 25 77 5f 70 61 72 65 6e  ME TO \"%w_paren
36b3c 74 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45 52  t\";".    "ALTER
36b3d 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 72 6f   TABLE %Q.'%q_ro
36b3e 77 69 64 27 20 20 52 45 4e 41 4d 45 20 54 4f 20  wid'  RENAME TO 
36b3f 5c 22 25 77 5f 72 6f 77 69 64 5c 22 3b 22 0a 20  \"%w_rowid\";". 
36b40 20 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62     , pRtree->zDb
36b41 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c  , pRtree->zName,
36b42 20 7a 4e 65 77 4e 61 6d 65 20 0a 20 20 20 20 2c   zNewName .    ,
36b43 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
36b44 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65  tree->zName, zNe
36b45 77 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74  wName .    , pRt
36b46 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65  ree->zDb, pRtree
36b47 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d  ->zName, zNewNam
36b48 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71  e.  );.  if( zSq
36b49 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
36b4a 6c 69 74 65 33 5f 65 78 65 63 28 70 52 74 72 65  lite3_exec(pRtre
36b4b 65 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20  e->db, zSql, 0, 
36b4c 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
36b4d 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20  e3_free(zSql);. 
36b4e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
36b4f 7d 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  }..static sqlite
36b50 33 5f 6d 6f 64 75 6c 65 20 72 74 72 65 65 4d 6f  3_module rtreeMo
36b51 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 20 20 20  dule = {.  0,   
36b52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b53 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
36b54 6e 20 2a 2f 0a 20 20 72 74 72 65 65 43 72 65 61  n */.  rtreeCrea
36b55 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
36b56 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2d 20     /* xCreate - 
36b57 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 2a  create a table *
36b58 2f 0a 20 20 72 74 72 65 65 43 6f 6e 6e 65 63 74  /.  rtreeConnect
36b59 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36b5a 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 2d 20 63 6f  /* xConnect - co
36b5b 6e 6e 65 63 74 20 74 6f 20 61 6e 20 65 78 69 73  nnect to an exis
36b5c 74 69 6e 67 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ting table */.  
36b5d 72 74 72 65 65 42 65 73 74 49 6e 64 65 78 2c 20  rtreeBestIndex, 
36b5e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
36b5f 42 65 73 74 49 6e 64 65 78 20 2d 20 44 65 74 65  BestIndex - Dete
36b60 72 6d 69 6e 65 20 73 65 61 72 63 68 20 73 74 72  rmine search str
36b61 61 74 65 67 79 20 2a 2f 0a 20 20 72 74 72 65 65  ategy */.  rtree
36b62 44 69 73 63 6f 6e 6e 65 63 74 2c 20 20 20 20 20  Disconnect,     
36b63 20 20 20 20 20 20 20 2f 2a 20 78 44 69 73 63 6f         /* xDisco
36b64 6e 6e 65 63 74 20 2d 20 44 69 73 63 6f 6e 6e 65  nnect - Disconne
36b65 63 74 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20  ct from a table 
36b66 2a 2f 0a 20 20 72 74 72 65 65 44 65 73 74 72 6f  */.  rtreeDestro
36b67 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
36b68 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2d 20 44   /* xDestroy - D
36b69 72 6f 70 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20  rop a table */. 
36b6a 20 72 74 72 65 65 4f 70 65 6e 2c 20 20 20 20 20   rtreeOpen,     
36b6b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36b6c 78 4f 70 65 6e 20 2d 20 6f 70 65 6e 20 61 20 63  xOpen - open a c
36b6d 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65  ursor */.  rtree
36b6e 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
36b6f 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
36b70 20 2d 20 63 6c 6f 73 65 20 61 20 63 75 72 73 6f   - close a curso
36b71 72 20 2a 2f 0a 20 20 72 74 72 65 65 46 69 6c 74  r */.  rtreeFilt
36b72 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
36b73 20 20 20 2f 2a 20 78 46 69 6c 74 65 72 20 2d 20     /* xFilter - 
36b74 63 6f 6e 66 69 67 75 72 65 20 73 63 61 6e 20 63  configure scan c
36b75 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
36b76 72 74 72 65 65 4e 65 78 74 2c 20 20 20 20 20 20  rtreeNext,      
36b77 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
36b78 4e 65 78 74 20 2d 20 61 64 76 61 6e 63 65 20 61  Next - advance a
36b79 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72   cursor */.  rtr
36b7a 65 65 45 6f 66 2c 20 20 20 20 20 20 20 20 20 20  eeEof,          
36b7b 20 20 20 20 20 20 20 20 20 2f 2a 20 78 45 6f 66           /* xEof
36b7c 20 2a 2f 0a 20 20 72 74 72 65 65 43 6f 6c 75 6d   */.  rtreeColum
36b7d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
36b7e 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 2d 20 72    /* xColumn - r
36b7f 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20 72 74  ead data */.  rt
36b80 72 65 65 52 6f 77 69 64 2c 20 20 20 20 20 20 20  reeRowid,       
36b81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 6f            /* xRo
36b82 77 69 64 20 2d 20 72 65 61 64 20 64 61 74 61 20  wid - read data 
36b83 2a 2f 0a 20 20 72 74 72 65 65 55 70 64 61 74 65  */.  rtreeUpdate
36b84 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36b85 20 2f 2a 20 78 55 70 64 61 74 65 20 2d 20 77 72   /* xUpdate - wr
36b86 69 74 65 20 64 61 74 61 20 2a 2f 0a 20 20 30 2c  ite data */.  0,
36b87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b88 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 42 65            /* xBe
36b89 67 69 6e 20 2d 20 62 65 67 69 6e 20 74 72 61 6e  gin - begin tran
36b8a 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20  saction */.  0, 
36b8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b8c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 79 6e           /* xSyn
36b8d 63 20 2d 20 73 79 6e 63 20 74 72 61 6e 73 61 63  c - sync transac
36b8e 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20  tion */.  0,    
36b8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b90 20 20 20 20 20 20 2f 2a 20 78 43 6f 6d 6d 69 74        /* xCommit
36b91 20 2d 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61   - commit transa
36b92 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20  ction */.  0,   
36b93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b94 20 20 20 20 20 20 20 2f 2a 20 78 52 6f 6c 6c 62         /* xRollb
36b95 61 63 6b 20 2d 20 72 6f 6c 6c 62 61 63 6b 20 74  ack - rollback t
36b96 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
36b97 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
36b98 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
36b99 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2d 20 66  FindFunction - f
36b9a 75 6e 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64  unction overload
36b9b 69 6e 67 20 2a 2f 0a 20 20 72 74 72 65 65 52 65  ing */.  rtreeRe
36b9c 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
36b9d 20 20 20 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20       /* xRename 
36b9e 2d 20 72 65 6e 61 6d 65 20 74 68 65 20 74 61 62  - rename the tab
36b9f 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 61 74 69 63  le */.};..static
36ba0 20 69 6e 74 20 72 74 72 65 65 53 71 6c 49 6e 69   int rtreeSqlIni
36ba1 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
36ba2 65 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a  ee, .  sqlite3 *
36ba3 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  db, .  const cha
36ba4 72 20 2a 7a 44 62 2c 20 0a 20 20 63 6f 6e 73 74  r *zDb, .  const
36ba5 20 63 68 61 72 20 2a 7a 50 72 65 66 69 78 2c 20   char *zPrefix, 
36ba6 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65 0a  .  int isCreate.
36ba7 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
36ba8 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 23 64 65 66  LITE_OK;..  #def
36ba9 69 6e 65 20 4e 5f 53 54 41 54 45 4d 45 4e 54 20  ine N_STATEMENT 
36baa 39 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  9.  static const
36bab 20 63 68 61 72 20 2a 61 7a 53 71 6c 5b 4e 5f 53   char *azSql[N_S
36bac 54 41 54 45 4d 45 4e 54 5d 20 3d 20 7b 0a 20 20  TATEMENT] = {.  
36bad 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
36bae 69 74 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65  ite the xxx_node
36baf 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53   table */.    "S
36bb0 45 4c 45 43 54 20 64 61 74 61 20 46 52 4f 4d 20  ELECT data FROM 
36bb1 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57  '%q'.'%q_node' W
36bb2 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31  HERE nodeno = :1
36bb3 22 2c 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f  ",.    "INSERT O
36bb4 52 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27  R REPLACE INTO '
36bb5 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41  %q'.'%q_node' VA
36bb6 4c 55 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20  LUES(:1, :2)",. 
36bb7 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
36bb8 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57  '%q'.'%q_node' W
36bb9 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31  HERE nodeno = :1
36bba 22 2c 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20  ",..    /* Read 
36bbb 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 78 78  and write the xx
36bbc 78 5f 72 6f 77 69 64 20 74 61 62 6c 65 20 2a 2f  x_rowid table */
36bbd 0a 20 20 20 20 22 53 45 4c 45 43 54 20 6e 6f 64  .    "SELECT nod
36bbe 65 6e 6f 20 46 52 4f 4d 20 27 25 71 27 2e 27 25  eno FROM '%q'.'%
36bbf 71 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20 72  q_rowid' WHERE r
36bc0 6f 77 69 64 20 3d 20 3a 31 22 2c 0a 20 20 20 20  owid = :1",.    
36bc1 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41  "INSERT OR REPLA
36bc2 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71  CE INTO '%q'.'%q
36bc3 5f 72 6f 77 69 64 27 20 56 41 4c 55 45 53 28 3a  _rowid' VALUES(:
36bc4 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
36bc5 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
36bc6 25 71 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20  %q_rowid' WHERE 
36bc7 72 6f 77 69 64 20 3d 20 3a 31 22 2c 0a 0a 20 20  rowid = :1",..  
36bc8 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
36bc9 69 74 65 20 74 68 65 20 78 78 78 5f 70 61 72 65  ite the xxx_pare
36bca 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  nt table */.    
36bcb 22 53 45 4c 45 43 54 20 70 61 72 65 6e 74 6e 6f  "SELECT parentno
36bcc 64 65 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71  de FROM '%q'.'%q
36bcd 5f 70 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e  _parent' WHERE n
36bce 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20  odeno = :1",.   
36bcf 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c   "INSERT OR REPL
36bd0 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25  ACE INTO '%q'.'%
36bd1 71 5f 70 61 72 65 6e 74 27 20 56 41 4c 55 45 53  q_parent' VALUES
36bd2 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22  (:1, :2)",.    "
36bd3 44 45 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27  DELETE FROM '%q'
36bd4 2e 27 25 71 5f 70 61 72 65 6e 74 27 20 57 48 45  .'%q_parent' WHE
36bd5 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 0a  RE nodeno = :1".
36bd6 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73    };.  sqlite3_s
36bd7 74 6d 74 20 2a 2a 61 70 70 53 74 6d 74 5b 4e 5f  tmt **appStmt[N_
36bd8 53 54 41 54 45 4d 45 4e 54 5d 3b 0a 20 20 69 6e  STATEMENT];.  in
36bd9 74 20 69 3b 0a 0a 20 20 70 52 74 72 65 65 2d 3e  t i;..  pRtree->
36bda 64 62 20 3d 20 64 62 3b 0a 0a 20 20 69 66 28 20  db = db;..  if( 
36bdb 69 73 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20  isCreate ){.    
36bdc 63 68 61 72 20 2a 7a 43 72 65 61 74 65 20 3d 20  char *zCreate = 
36bdd 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
36bde 0a 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c  ."CREATE TABLE \
36bdf 22 25 77 5c 22 2e 5c 22 25 77 5f 6e 6f 64 65 5c  "%w\".\"%w_node\
36be0 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52  "(nodeno INTEGER
36be1 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 64 61   PRIMARY KEY, da
36be2 74 61 20 42 4c 4f 42 29 3b 22 0a 22 43 52 45 41  ta BLOB);"."CREA
36be3 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e  TE TABLE \"%w\".
36be4 5c 22 25 77 5f 72 6f 77 69 64 5c 22 28 72 6f 77  \"%w_rowid\"(row
36be5 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
36be6 52 59 20 4b 45 59 2c 20 6e 6f 64 65 6e 6f 20 49  RY KEY, nodeno I
36be7 4e 54 45 47 45 52 29 3b 22 0a 22 43 52 45 41 54  NTEGER);"."CREAT
36be8 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c  E TABLE \"%w\".\
36be9 22 25 77 5f 70 61 72 65 6e 74 5c 22 28 6e 6f 64  "%w_parent\"(nod
36bea 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d  eno INTEGER PRIM
36beb 41 52 59 20 4b 45 59 2c 20 70 61 72 65 6e 74 6e  ARY KEY, parentn
36bec 6f 64 65 20 49 4e 54 45 47 45 52 29 3b 22 0a 22  ode INTEGER);"."
36bed 49 4e 53 45 52 54 20 49 4e 54 4f 20 27 25 71 27  INSERT INTO '%q'
36bee 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45  .'%q_node' VALUE
36bef 53 28 31 2c 20 7a 65 72 6f 62 6c 6f 62 28 25 64  S(1, zeroblob(%d
36bf0 29 29 22 2c 0a 20 20 20 20 20 20 7a 44 62 2c 20  ))",.      zDb, 
36bf1 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50  zPrefix, zDb, zP
36bf2 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65  refix, zDb, zPre
36bf3 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69  fix, zDb, zPrefi
36bf4 78 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  x, pRtree->iNode
36bf5 53 69 7a 65 0a 20 20 20 20 29 3b 0a 20 20 20 20  Size.    );.    
36bf6 69 66 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a  if( !zCreate ){.
36bf7 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
36bf8 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
36bf9 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
36bfa 33 5f 65 78 65 63 28 64 62 2c 20 7a 43 72 65 61  3_exec(db, zCrea
36bfb 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  te, 0, 0, 0);.  
36bfc 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
36bfd 43 72 65 61 74 65 29 3b 0a 20 20 20 20 69 66 28  Create);.    if(
36bfe 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
36bff 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
36c00 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
36c01 61 70 70 53 74 6d 74 5b 30 5d 20 3d 20 26 70 52  appStmt[0] = &pR
36c02 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 3b  tree->pReadNode;
36c03 0a 20 20 61 70 70 53 74 6d 74 5b 31 5d 20 3d 20  .  appStmt[1] = 
36c04 26 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e  &pRtree->pWriteN
36c05 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 32  ode;.  appStmt[2
36c06 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65  ] = &pRtree->pDe
36c07 6c 65 74 65 4e 6f 64 65 3b 0a 20 20 61 70 70 53  leteNode;.  appS
36c08 74 6d 74 5b 33 5d 20 3d 20 26 70 52 74 72 65 65  tmt[3] = &pRtree
36c09 2d 3e 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20  ->pReadRowid;.  
36c0a 61 70 70 53 74 6d 74 5b 34 5d 20 3d 20 26 70 52  appStmt[4] = &pR
36c0b 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
36c0c 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 35 5d 20  d;.  appStmt[5] 
36c0d 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  = &pRtree->pDele
36c0e 74 65 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74  teRowid;.  appSt
36c0f 6d 74 5b 36 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[6] = &pRtree-
36c10 3e 70 52 65 61 64 50 61 72 65 6e 74 3b 0a 20 20  >pReadParent;.  
36c11 61 70 70 53 74 6d 74 5b 37 5d 20 3d 20 26 70 52  appStmt[7] = &pR
36c12 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65  tree->pWritePare
36c13 6e 74 3b 0a 20 20 61 70 70 53 74 6d 74 5b 38 5d  nt;.  appStmt[8]
36c14 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c   = &pRtree->pDel
36c15 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20 20 66 6f  eteParent;..  fo
36c16 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 54 41 54 45  r(i=0; i<N_STATE
36c17 4d 45 4e 54 20 26 26 20 72 63 3d 3d 53 51 4c 49  MENT && rc==SQLI
36c18 54 45 5f 4f 4b 3b 20 69 2b 2b 29 7b 0a 20 20 20  TE_OK; i++){.   
36c19 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71   char *zSql = sq
36c1a 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 61 7a  lite3_mprintf(az
36c1b 53 71 6c 5b 69 5d 2c 20 7a 44 62 2c 20 7a 50 72  Sql[i], zDb, zPr
36c1c 65 66 69 78 29 3b 0a 20 20 20 20 69 66 28 20 7a  efix);.    if( z
36c1d 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Sql ){.      rc 
36c1e 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
36c1f 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
36c20 31 2c 20 61 70 70 53 74 6d 74 5b 69 5d 2c 20 30  1, appStmt[i], 0
36c21 29 3b 20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ); .    }else{. 
36c22 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
36c23 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
36c24 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
36c25 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Sql);.  }..  ret
36c26 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
36c27 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 71 75   This routine qu
36c28 65 72 69 65 73 20 64 61 74 61 62 61 73 65 20 68  eries database h
36c29 61 6e 64 6c 65 20 64 62 20 66 6f 72 20 74 68 65  andle db for the
36c2a 20 70 61 67 65 2d 73 69 7a 65 20 75 73 65 64 20   page-size used 
36c2b 62 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 7a  by.** database z
36c2c 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  Db. If successfu
36c2d 6c 2c 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  l, the page-size
36c2e 20 69 6e 20 62 79 74 65 73 20 69 73 20 77 72 69   in bytes is wri
36c2f 74 74 65 6e 20 74 6f 0a 2a 2a 20 2a 70 69 50 61  tten to.** *piPa
36c30 67 65 53 69 7a 65 20 61 6e 64 20 53 51 4c 49 54  geSize and SQLIT
36c31 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 4f  E_OK returned. O
36c32 74 68 65 72 77 69 73 65 2c 20 61 6e 64 20 61 6e  therwise, and an
36c33 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a   SQLite error .*
36c34 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  * code is return
36c35 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
36c36 74 20 67 65 74 50 61 67 65 53 69 7a 65 28 73 71  t getPageSize(sq
36c37 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
36c38 20 63 68 61 72 20 2a 7a 44 62 2c 20 69 6e 74 20   char *zDb, int 
36c39 2a 70 69 50 61 67 65 53 69 7a 65 29 7b 0a 20 20  *piPageSize){.  
36c3a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
36c3b 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a  NOMEM;.  char *z
36c3c 53 71 6c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  Sql;.  sqlite3_s
36c3d 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a  tmt *pStmt = 0;.
36c3e 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
36c3f 33 5f 6d 70 72 69 6e 74 66 28 22 50 52 41 47 4d  3_mprintf("PRAGM
36c40 41 20 25 51 2e 70 61 67 65 5f 73 69 7a 65 22 2c  A %Q.page_size",
36c41 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 21 7a 53   zDb);.  if( !zS
36c42 71 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ql ){.    return
36c43 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
36c44 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
36c45 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
36c46 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
36c47 6d 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  mt, 0);.  sqlite
36c48 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
36c49 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
36c4a 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
36c4b 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53  rc;.  }..  if( S
36c4c 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
36c4d 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
36c4e 7b 0a 20 20 20 20 2a 70 69 50 61 67 65 53 69 7a  {.    *piPageSiz
36c4f 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
36c50 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20 30 29  mn_int(pStmt, 0)
36c51 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
36c52 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
36c53 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  pStmt);.}../* .*
36c54 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
36c55 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
36c56 61 74 69 6f 6e 20 6f 66 20 62 6f 74 68 20 74 68  ation of both th
36c57 65 20 78 43 6f 6e 6e 65 63 74 20 61 6e 64 20 78  e xConnect and x
36c58 43 72 65 61 74 65 0a 2a 2a 20 6d 65 74 68 6f 64  Create.** method
36c59 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  s of the r-tree 
36c5a 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
36c5b 2a 0a 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20 20  *.**   argv[0]  
36c5c 20 2d 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a   -> module name.
36c5d 2a 2a 20 20 20 61 72 67 76 5b 31 5d 20 20 20 2d  **   argv[1]   -
36c5e 3e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a  > database name.
36c5f 2a 2a 20 20 20 61 72 67 76 5b 32 5d 20 20 20 2d  **   argv[2]   -
36c60 3e 20 74 61 62 6c 65 20 6e 61 6d 65 0a 2a 2a 20  > table name.** 
36c61 20 20 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20 63    argv[...] -> c
36c62 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 2e 2e 0a 2a  olumn names....*
36c63 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
36c64 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  eeInit(.  sqlite
36c65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
36c66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36c67 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
36c68 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
36c69 70 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20  pAux,           
36c6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36c6b 20 4f 6e 65 20 6f 66 20 74 68 65 20 52 54 52 45   One of the RTRE
36c6c 45 5f 43 4f 4f 52 44 5f 2a 20 63 6f 6e 73 74 61  E_COORD_* consta
36c6d 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67  nts */.  int arg
36c6e 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
36c6f 6f 6e 73 74 2a 61 72 67 76 2c 20 20 20 2f 2a 20  onst*argv,   /* 
36c70 50 61 72 61 6d 65 74 65 72 73 20 74 6f 20 43 52  Parameters to CR
36c71 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
36c72 6d 65 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ment */.  sqlite
36c73 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
36c74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36c75 20 4f 55 54 3a 20 4e 65 77 20 76 69 72 74 75 61   OUT: New virtua
36c76 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  l table */.  cha
36c77 72 20 2a 2a 70 7a 45 72 72 2c 20 20 20 20 20 20  r **pzErr,      
36c78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c79 20 2f 2a 20 4f 55 54 3a 20 45 72 72 6f 72 20 6d   /* OUT: Error m
36c7a 65 73 73 61 67 65 2c 20 69 66 20 61 6e 79 20 2a  essage, if any *
36c7b 2f 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65  /.  int isCreate
36c7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c7d 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
36c7e 66 6f 72 20 78 43 72 65 61 74 65 2c 20 66 61 6c  for xCreate, fal
36c7f 73 65 20 66 6f 72 20 78 43 6f 6e 6e 65 63 74 20  se for xConnect 
36c80 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
36c81 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
36c82 74 20 69 50 61 67 65 53 69 7a 65 20 3d 20 30 3b  t iPageSize = 0;
36c83 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
36c84 3b 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20  ;.  int nDb;    
36c85 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
36c86 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 61 72  gth of string ar
36c87 67 76 5b 31 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  gv[1] */.  int n
36c88 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
36c89 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74   /* Length of st
36c8a 72 69 6e 67 20 61 72 67 76 5b 32 5d 20 2a 2f 0a  ring argv[2] */.
36c8b 20 20 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65    int eCoordType
36c8c 20 3d 20 28 69 6e 74 29 70 41 75 78 3b 0a 0a 20   = (int)pAux;.. 
36c8d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 45 72   const char *aEr
36c8e 72 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  rMsg[] = {.    0
36c8f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c92 20 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20       /* 0 */.   
36c93 20 22 57 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f   "Wrong number o
36c94 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e  f columns for an
36c95 20 72 74 72 65 65 20 74 61 62 6c 65 22 2c 20 20   rtree table",  
36c96 20 20 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20         /* 1 */. 
36c97 20 20 20 22 54 6f 6f 20 66 65 77 20 63 6f 6c 75     "Too few colu
36c98 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65  mns for an rtree
36c99 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20   table",        
36c9a 20 20 20 20 20 20 20 20 20 2f 2a 20 32 20 2a 2f           /* 2 */
36c9b 0a 20 20 20 20 22 54 6f 6f 20 6d 61 6e 79 20 63  .    "Too many c
36c9c 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74  olumns for an rt
36c9d 72 65 65 20 74 61 62 6c 65 22 20 20 20 20 20 20  ree table"      
36c9e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 20             /* 3 
36c9f 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69  */.  };..  int i
36ca0 45 72 72 20 3d 20 28 61 72 67 63 3c 36 29 20 3f  Err = (argc<6) ?
36ca1 20 32 20 3a 20 61 72 67 63 3e 28 52 54 52 45 45   2 : argc>(RTREE
36ca2 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a  _MAX_DIMENSIONS*
36ca3 32 2b 34 29 20 3f 20 33 20 3a 20 61 72 67 63 25  2+4) ? 3 : argc%
36ca4 32 3b 0a 20 20 69 66 28 20 61 45 72 72 4d 73 67  2;.  if( aErrMsg
36ca5 5b 69 45 72 72 5d 20 29 7b 0a 20 20 20 20 2a 70  [iErr] ){.    *p
36ca6 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
36ca7 70 72 69 6e 74 66 28 22 25 73 22 2c 20 61 45 72  printf("%s", aEr
36ca8 72 4d 73 67 5b 69 45 72 72 5d 29 3b 0a 20 20 20  rMsg[iErr]);.   
36ca9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
36caa 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  RROR;.  }..  rc 
36cab 3d 20 67 65 74 50 61 67 65 53 69 7a 65 28 64 62  = getPageSize(db
36cac 2c 20 61 72 67 76 5b 31 5d 2c 20 26 69 50 61 67  , argv[1], &iPag
36cad 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63  eSize);.  if( rc
36cae 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
36caf 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
36cb0 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
36cb1 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61   the sqlite3_vta
36cb2 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  b structure */. 
36cb3 20 6e 44 62 20 3d 20 73 74 72 6c 65 6e 28 61 72   nDb = strlen(ar
36cb4 67 76 5b 31 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20  gv[1]);.  nName 
36cb5 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 32 5d  = strlen(argv[2]
36cb6 29 3b 0a 20 20 70 52 74 72 65 65 20 3d 20 28 52  );.  pRtree = (R
36cb7 74 72 65 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d  tree *)sqlite3_m
36cb8 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72  alloc(sizeof(Rtr
36cb9 65 65 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29  ee)+nDb+nName+2)
36cba 3b 0a 20 20 69 66 28 20 21 70 52 74 72 65 65 20  ;.  if( !pRtree 
36cbb 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
36cbc 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
36cbd 20 20 6d 65 6d 73 65 74 28 70 52 74 72 65 65 2c    memset(pRtree,
36cbe 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
36cbf 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a  )+nDb+nName+2);.
36cc0 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 20    pRtree->nBusy 
36cc1 3d 20 31 3b 0a 20 20 70 52 74 72 65 65 2d 3e 62  = 1;.  pRtree->b
36cc2 61 73 65 2e 70 4d 6f 64 75 6c 65 20 3d 20 26 72  ase.pModule = &r
36cc3 74 72 65 65 4d 6f 64 75 6c 65 3b 0a 20 20 70 52  treeModule;.  pR
36cc4 74 72 65 65 2d 3e 7a 44 62 20 3d 20 28 63 68 61  tree->zDb = (cha
36cc5 72 20 2a 29 26 70 52 74 72 65 65 5b 31 5d 3b 0a  r *)&pRtree[1];.
36cc6 20 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 20    pRtree->zName 
36cc7 3d 20 26 70 52 74 72 65 65 2d 3e 7a 44 62 5b 6e  = &pRtree->zDb[n
36cc8 44 62 2b 31 5d 3b 0a 20 20 70 52 74 72 65 65 2d  Db+1];.  pRtree-
36cc9 3e 6e 44 69 6d 20 3d 20 28 61 72 67 63 2d 34 29  >nDim = (argc-4)
36cca 2f 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42  /2;.  pRtree->nB
36ccb 79 74 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20  ytesPerCell = 8 
36ccc 2b 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 34  + pRtree->nDim*4
36ccd 2a 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 65 43  *2;.  pRtree->eC
36cce 6f 6f 72 64 54 79 70 65 20 3d 20 65 43 6f 6f 72  oordType = eCoor
36ccf 64 54 79 70 65 3b 0a 20 20 6d 65 6d 63 70 79 28  dType;.  memcpy(
36cd0 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 61 72 67  pRtree->zDb, arg
36cd1 76 5b 31 5d 2c 20 6e 44 62 29 3b 0a 20 20 6d 65  v[1], nDb);.  me
36cd2 6d 63 70 79 28 70 52 74 72 65 65 2d 3e 7a 4e 61  mcpy(pRtree->zNa
36cd3 6d 65 2c 20 61 72 67 76 5b 32 5d 2c 20 6e 4e 61  me, argv[2], nNa
36cd4 6d 65 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  me);..  /* Figur
36cd5 65 20 6f 75 74 20 74 68 65 20 6e 6f 64 65 20 73  e out the node s
36cd6 69 7a 65 20 74 6f 20 75 73 65 2e 20 42 79 20 64  ize to use. By d
36cd7 65 66 61 75 6c 74 2c 20 75 73 65 20 36 34 20 62  efault, use 64 b
36cd8 79 74 65 73 20 6c 65 73 73 20 74 68 61 6e 0a 20  ytes less than. 
36cd9 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
36cda 20 70 61 67 65 2d 73 69 7a 65 2e 20 54 68 69 73   page-size. This
36cdb 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 65 61   ensures that ea
36cdc 63 68 20 6e 6f 64 65 20 69 73 20 73 74 6f 72 65  ch node is store
36cdd 64 20 6f 6e 0a 20 20 2a 2a 20 61 20 73 69 6e 67  d on.  ** a sing
36cde 6c 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  le database page
36cdf 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
36ce0 68 65 20 64 61 74 61 62 61 73 64 20 70 61 67 65  he databasd page
36ce1 2d 73 69 7a 65 20 69 73 20 73 6f 20 6c 61 72 67  -size is so larg
36ce2 65 20 74 68 61 74 20 6d 6f 72 65 20 74 68 61 6e  e that more than
36ce3 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 0a   RTREE_MAXCELLS.
36ce4 20 20 2a 2a 20 65 6e 74 72 69 65 73 20 77 6f 75    ** entries wou
36ce5 6c 64 20 66 69 74 20 69 6e 20 61 20 73 69 6e 67  ld fit in a sing
36ce6 6c 65 20 6e 6f 64 65 2c 20 75 73 65 20 61 20 73  le node, use a s
36ce7 6d 61 6c 6c 65 72 20 6e 6f 64 65 2d 73 69 7a 65  maller node-size
36ce8 2e 0a 20 20 2a 2f 0a 20 20 70 52 74 72 65 65 2d  ..  */.  pRtree-
36ce9 3e 69 4e 6f 64 65 53 69 7a 65 20 3d 20 69 50 61  >iNodeSize = iPa
36cea 67 65 53 69 7a 65 2d 36 34 3b 0a 20 20 69 66 28  geSize-64;.  if(
36ceb 20 28 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74   (4+pRtree->nByt
36cec 65 73 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f  esPerCell*RTREE_
36ced 4d 41 58 43 45 4c 4c 53 29 3c 70 52 74 72 65 65  MAXCELLS)<pRtree
36cee 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20  ->iNodeSize ){. 
36cef 20 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65     pRtree->iNode
36cf0 53 69 7a 65 20 3d 20 34 2b 70 52 74 72 65 65 2d  Size = 4+pRtree-
36cf1 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52  >nBytesPerCell*R
36cf2 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 3b 0a 20  TREE_MAXCELLS;. 
36cf3 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f   }..  /* Create/
36cf4 43 6f 6e 6e 65 63 74 20 74 6f 20 74 68 65 20 75  Connect to the u
36cf5 6e 64 65 72 6c 79 69 6e 67 20 72 65 6c 61 74 69  nderlying relati
36cf6 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73 63  onal database sc
36cf7 68 65 6d 61 2e 20 49 66 0a 20 20 2a 2a 20 74 68  hema. If.  ** th
36cf8 61 74 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  at is successful
36cf9 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 64  , call sqlite3_d
36cfa 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 74 6f  eclare_vtab() to
36cfb 20 63 6f 6e 66 69 67 75 72 65 0a 20 20 2a 2a 20   configure.  ** 
36cfc 74 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65  the r-tree table
36cfd 20 73 63 68 65 6d 61 2e 0a 20 20 2a 2f 0a 20 20   schema..  */.  
36cfe 69 66 28 20 28 72 63 20 3d 20 72 74 72 65 65 53  if( (rc = rtreeS
36cff 71 6c 49 6e 69 74 28 70 52 74 72 65 65 2c 20 64  qlInit(pRtree, d
36d00 62 2c 20 61 72 67 76 5b 31 5d 2c 20 61 72 67 76  b, argv[1], argv
36d01 5b 32 5d 2c 20 69 73 43 72 65 61 74 65 29 29 20  [2], isCreate)) 
36d02 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  ){.    *pzErr = 
36d03 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
36d04 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72  "%s", sqlite3_er
36d05 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 7d 65 6c  rmsg(db));.  }el
36d06 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53  se{.    char *zS
36d07 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
36d08 69 6e 74 66 28 22 43 52 45 41 54 45 20 54 41 42  intf("CREATE TAB
36d09 4c 45 20 78 28 25 73 22 2c 20 61 72 67 76 5b 33  LE x(%s", argv[3
36d0a 5d 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54  ]);.    char *zT
36d0b 6d 70 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a  mp;.    int ii;.
36d0c 20 20 20 20 66 6f 72 28 69 69 3d 34 3b 20 7a 53      for(ii=4; zS
36d0d 71 6c 20 26 26 20 69 69 3c 61 72 67 63 3b 20 69  ql && ii<argc; i
36d0e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 54 6d 70  i++){.      zTmp
36d0f 20 3d 20 7a 53 71 6c 3b 0a 20 20 20 20 20 20 7a   = zSql;.      z
36d10 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
36d11 72 69 6e 74 66 28 22 25 73 2c 20 25 73 22 2c 20  rintf("%s, %s", 
36d12 7a 54 6d 70 2c 20 61 72 67 76 5b 69 69 5d 29 3b  zTmp, argv[ii]);
36d13 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
36d14 72 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d  ree(zTmp);.    }
36d15 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b  .    if( zSql ){
36d16 0a 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53  .      zTmp = zS
36d17 71 6c 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d  ql;.      zSql =
36d18 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
36d19 28 22 25 73 29 3b 22 2c 20 7a 54 6d 70 29 3b 0a  ("%s);", zTmp);.
36d1a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
36d1b 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d 0a  ee(zTmp);.    }.
36d1c 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20 7c 7c      if( !zSql ||
36d1d 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
36d1e 5f 76 74 61 62 28 64 62 2c 20 7a 53 71 6c 29 20  _vtab(db, zSql) 
36d1f 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
36d20 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
36d21 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
36d22 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20  ee(zSql);.  }.. 
36d23 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
36d24 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 70 56 74 61  OK ){.    *ppVta
36d25 62 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61  b = (sqlite3_vta
36d26 62 20 2a 29 70 52 74 72 65 65 3b 0a 20 20 7d 65  b *)pRtree;.  }e
36d27 6c 73 65 7b 0a 20 20 20 20 72 74 72 65 65 52 65  lse{.    rtreeRe
36d28 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20  lease(pRtree);. 
36d29 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
36d2a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  }.../*.** Implem
36d2b 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 63  entation of a sc
36d2c 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 68  alar function th
36d2d 61 74 20 64 65 63 6f 64 65 73 20 72 2d 74 72 65  at decodes r-tre
36d2e 65 20 6e 6f 64 65 73 20 74 6f 0a 2a 2a 20 68 75  e nodes to.** hu
36d2f 6d 61 6e 20 72 65 61 64 61 62 6c 65 20 73 74 72  man readable str
36d30 69 6e 67 73 2e 20 54 68 69 73 20 63 61 6e 20 62  ings. This can b
36d31 65 20 75 73 65 64 20 66 6f 72 20 64 65 62 75 67  e used for debug
36d32 67 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69  ging and analysi
36d33 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61  s..**.** The sca
36d34 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  lar function tak
36d35 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  es two arguments
36d36 2c 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61  , a blob of data
36d37 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 61   containing.** a
36d38 6e 20 72 2d 74 72 65 65 20 6e 6f 64 65 2c 20 61  n r-tree node, a
36d39 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nd the number of
36d3a 20 64 69 6d 65 6e 73 69 6f 6e 73 20 74 68 65 20   dimensions the 
36d3b 72 2d 74 72 65 65 20 69 6e 64 65 78 65 73 2e 0a  r-tree indexes..
36d3c 2a 2a 20 46 6f 72 20 61 20 74 77 6f 2d 64 69 6d  ** For a two-dim
36d3d 65 6e 73 69 6f 6e 61 6c 20 72 2d 74 72 65 65 20  ensional r-tree 
36d3e 73 74 72 75 63 74 75 72 65 20 63 61 6c 6c 65 64  structure called
36d3f 20 22 72 74 22 2c 20 74 6f 20 64 65 73 65 72 69   "rt", to deseri
36d40 61 6c 69 7a 65 0a 2a 2a 20 61 6c 6c 20 6e 6f 64  alize.** all nod
36d41 65 73 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20  es, a statement 
36d42 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  like:.**.**   SE
36d43 4c 45 43 54 20 72 74 72 65 65 6e 6f 64 65 28 32  LECT rtreenode(2
36d44 2c 20 64 61 74 61 29 20 46 52 4f 4d 20 72 74 5f  , data) FROM rt_
36d45 6e 6f 64 65 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20  node;.**.** The 
36d46 68 75 6d 61 6e 20 72 65 61 64 61 62 6c 65 20 73  human readable s
36d47 74 72 69 6e 67 20 74 61 6b 65 73 20 74 68 65 20  tring takes the 
36d48 66 6f 72 6d 20 6f 66 20 61 20 54 63 6c 20 6c 69  form of a Tcl li
36d49 73 74 20 77 69 74 68 20 6f 6e 65 0a 2a 2a 20 65  st with one.** e
36d4a 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 63 65  ntry for each ce
36d4b 6c 6c 20 69 6e 20 74 68 65 20 72 2d 74 72 65 65  ll in the r-tree
36d4c 20 6e 6f 64 65 2e 20 45 61 63 68 20 65 6e 74 72   node. Each entr
36d4d 79 20 69 73 20 69 74 73 65 6c 66 20 61 0a 2a 2a  y is itself a.**
36d4e 20 6c 69 73 74 2c 20 63 6f 6e 74 61 69 6e 69 6e   list, containin
36d4f 67 20 74 68 65 20 38 2d 62 79 74 65 20 72 6f 77  g the 8-byte row
36d50 69 64 2f 70 61 67 65 6e 6f 20 66 6f 6c 6c 6f 77  id/pageno follow
36d51 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 3c 6e  ed by the .** <n
36d52 75 6d 2d 64 69 6d 65 6e 73 69 6f 6e 3e 2a 32 20  um-dimension>*2 
36d53 63 6f 6f 72 64 69 6e 61 74 65 73 2e 0a 2a 2f 0a  coordinates..*/.
36d54 73 74 61 74 69 63 20 76 6f 69 64 20 72 74 72 65  static void rtre
36d55 65 6e 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f  enode(sqlite3_co
36d56 6e 74 65 78 74 20 2a 63 74 78 2c 20 69 6e 74 20  ntext *ctx, int 
36d57 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61  nArg, sqlite3_va
36d58 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20  lue **apArg){.  
36d59 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b  char *zText = 0;
36d5a 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 6e 6f 64  .  RtreeNode nod
36d5b 65 3b 0a 20 20 52 74 72 65 65 20 74 72 65 65 3b  e;.  Rtree tree;
36d5c 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20 20 6d 65  .  int ii;..  me
36d5d 6d 73 65 74 28 26 6e 6f 64 65 2c 20 30 2c 20 73  mset(&node, 0, s
36d5e 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29  izeof(RtreeNode)
36d5f 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 74 72 65  );.  memset(&tre
36d60 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72  e, 0, sizeof(Rtr
36d61 65 65 29 29 3b 0a 20 20 74 72 65 65 2e 6e 44 69  ee));.  tree.nDi
36d62 6d 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  m = sqlite3_valu
36d63 65 5f 69 6e 74 28 61 70 41 72 67 5b 30 5d 29 3b  e_int(apArg[0]);
36d64 0a 20 20 74 72 65 65 2e 6e 42 79 74 65 73 50 65  .  tree.nBytesPe
36d65 72 43 65 6c 6c 20 3d 20 38 20 2b 20 38 20 2a 20  rCell = 8 + 8 * 
36d66 74 72 65 65 2e 6e 44 69 6d 3b 0a 20 20 6e 6f 64  tree.nDim;.  nod
36d67 65 2e 7a 44 61 74 61 20 3d 20 28 75 38 20 2a 29  e.zData = (u8 *)
36d68 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
36d69 6f 62 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20  ob(apArg[1]);.. 
36d6a 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 4e 43   for(ii=0; ii<NC
36d6b 45 4c 4c 28 26 6e 6f 64 65 29 3b 20 69 69 2b 2b  ELL(&node); ii++
36d6c 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 43 65 6c  ){.    char zCel
36d6d 6c 5b 35 31 32 5d 3b 0a 20 20 20 20 69 6e 74 20  l[512];.    int 
36d6e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 52  nCell = 0;.    R
36d6f 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
36d70 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20     int jj;..    
36d71 6e 6f 64 65 47 65 74 43 65 6c 6c 28 26 74 72 65  nodeGetCell(&tre
36d72 65 2c 20 26 6e 6f 64 65 2c 20 69 69 2c 20 26 63  e, &node, ii, &c
36d73 65 6c 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ell);.    sqlite
36d74 33 5f 73 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e  3_snprintf(512-n
36d75 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c  Cell,&zCell[nCel
36d76 6c 5d 2c 22 25 64 22 2c 20 63 65 6c 6c 2e 69 52  l],"%d", cell.iR
36d77 6f 77 69 64 29 3b 0a 20 20 20 20 6e 43 65 6c 6c  owid);.    nCell
36d78 20 3d 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29   = strlen(zCell)
36d79 3b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20  ;.    for(jj=0; 
36d7a 6a 6a 3c 74 72 65 65 2e 6e 44 69 6d 2a 32 3b 20  jj<tree.nDim*2; 
36d7b 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c  jj++){.      sql
36d7c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 31  ite3_snprintf(51
36d7d 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e  2-nCell,&zCell[n
36d7e 43 65 6c 6c 5d 2c 22 20 25 66 22 2c 28 64 6f 75  Cell]," %f",(dou
36d7f 62 6c 65 29 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b  ble)cell.aCoord[
36d80 6a 6a 5d 2e 66 29 3b 0a 20 20 20 20 20 20 6e 43  jj].f);.      nC
36d81 65 6c 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 43 65  ell = strlen(zCe
36d82 6c 6c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ll);.    }..    
36d83 69 66 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20  if( zText ){.   
36d84 20 20 20 63 68 61 72 20 2a 7a 54 65 78 74 4e 65     char *zTextNe
36d85 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  w = sqlite3_mpri
36d86 6e 74 66 28 22 25 73 20 7b 25 73 7d 22 2c 20 7a  ntf("%s {%s}", z
36d87 54 65 78 74 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20  Text, zCell);.  
36d88 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
36d89 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7a  (zText);.      z
36d8a 54 65 78 74 20 3d 20 7a 54 65 78 74 4e 65 77 3b  Text = zTextNew;
36d8b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
36d8c 20 20 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65    zText = sqlite
36d8d 33 5f 6d 70 72 69 6e 74 66 28 22 7b 25 73 7d 22  3_mprintf("{%s}"
36d8e 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a  , zCell);.    }.
36d8f 20 20 7d 0a 20 20 0a 20 20 73 71 6c 69 74 65 33    }.  .  sqlite3
36d90 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 74 78  _result_text(ctx
36d91 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 73 71 6c  , zText, -1, sql
36d92 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 73  ite3_free);.}..s
36d93 74 61 74 69 63 20 76 6f 69 64 20 72 74 72 65 65  tatic void rtree
36d94 64 65 70 74 68 28 73 71 6c 69 74 65 33 5f 63 6f  depth(sqlite3_co
36d95 6e 74 65 78 74 20 2a 63 74 78 2c 20 69 6e 74 20  ntext *ctx, int 
36d96 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61  nArg, sqlite3_va
36d97 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20  lue **apArg){.  
36d98 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
36d99 65 5f 74 79 70 65 28 61 70 41 72 67 5b 30 5d 29  e_type(apArg[0])
36d9a 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 0a 20  !=SQLITE_BLOB . 
36d9b 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c    || sqlite3_val
36d9c 75 65 5f 62 79 74 65 73 28 61 70 41 72 67 5b 30  ue_bytes(apArg[0
36d9d 5d 29 3c 32 0a 20 20 29 7b 0a 20 20 20 20 73 71  ])<2.  ){.    sq
36d9e 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
36d9f 6f 72 28 63 74 78 2c 20 22 49 6e 76 61 6c 69 64  or(ctx, "Invalid
36da0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 74 72   argument to rtr
36da1 65 65 64 65 70 74 68 28 29 22 2c 20 2d 31 29 3b  eedepth()", -1);
36da2 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75   .  }else{.    u
36da3 38 20 2a 7a 42 6c 6f 62 20 3d 20 28 75 38 20 2a  8 *zBlob = (u8 *
36da4 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  )sqlite3_value_b
36da5 6c 6f 62 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20  lob(apArg[0]);. 
36da6 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
36da7 74 5f 69 6e 74 28 63 74 78 2c 20 72 65 61 64 49  t_int(ctx, readI
36da8 6e 74 31 36 28 7a 42 6c 6f 62 29 29 3b 0a 20 20  nt16(zBlob));.  
36da9 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  }.}../*.** Regis
36daa 74 65 72 20 74 68 65 20 72 2d 74 72 65 65 20 6d  ter the r-tree m
36dab 6f 64 75 6c 65 20 77 69 74 68 20 64 61 74 61 62  odule with datab
36dac 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 20 54  ase handle db. T
36dad 68 69 73 20 63 72 65 61 74 65 73 20 74 68 65 0a  his creates the.
36dae 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
36daf 20 6d 6f 64 75 6c 65 20 22 72 74 72 65 65 22 20   module "rtree" 
36db0 61 6e 64 20 74 68 65 20 64 65 62 75 67 67 69 6e  and the debuggin
36db1 67 2f 61 6e 61 6c 79 73 69 73 20 73 63 61 6c 61  g/analysis scala
36db2 72 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 22  r .** function "
36db3 72 74 72 65 65 6e 6f 64 65 22 2e 0a 2a 2f 0a 53  rtreenode"..*/.S
36db4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
36db5 74 20 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e  t sqlite3RtreeIn
36db6 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  it(sqlite3 *db){
36db7 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
36db8 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 72 63  TE_OK;..  if( rc
36db9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
36dba 20 20 20 69 6e 74 20 75 74 66 38 20 3d 20 53 51     int utf8 = SQ
36dbb 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 72  LITE_UTF8;.    r
36dbc 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61  c = sqlite3_crea
36dbd 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  te_function(db, 
36dbe 22 72 74 72 65 65 6e 6f 64 65 22 2c 20 32 2c 20  "rtreenode", 2, 
36dbf 75 74 66 38 2c 20 30 2c 20 72 74 72 65 65 6e 6f  utf8, 0, rtreeno
36dc0 64 65 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20  de, 0, 0);.  }. 
36dc1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
36dc2 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 75 74  OK ){.    int ut
36dc3 66 38 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  f8 = SQLITE_UTF8
36dc4 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
36dc5 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
36dc6 6f 6e 28 64 62 2c 20 22 72 74 72 65 65 64 65 70  on(db, "rtreedep
36dc7 74 68 22 2c 20 31 2c 20 75 74 66 38 2c 20 30 2c  th", 1, utf8, 0,
36dc8 72 74 72 65 65 64 65 70 74 68 2c 20 30 2c 20 30  rtreedepth, 0, 0
36dc9 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
36dca 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
36dcb 20 20 76 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69    void *c = (voi
36dcc 64 20 2a 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f  d *)RTREE_COORD_
36dcd 52 45 41 4c 33 32 3b 0a 20 20 20 20 72 63 20 3d  REAL32;.    rc =
36dce 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
36dcf 6d 6f 64 75 6c 65 5f 76 32 28 64 62 2c 20 22 72  module_v2(db, "r
36dd0 74 72 65 65 22 2c 20 26 72 74 72 65 65 4d 6f 64  tree", &rtreeMod
36dd1 75 6c 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a  ule, c, 0);.  }.
36dd2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
36dd3 5f 4f 4b 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  _OK ){.    void 
36dd4 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29 52 54 52  *c = (void *)RTR
36dd5 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 3b 0a  EE_COORD_INT32;.
36dd6 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
36dd7 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76  _create_module_v
36dd8 32 28 64 62 2c 20 22 72 74 72 65 65 5f 69 33 32  2(db, "rtree_i32
36dd9 22 2c 20 26 72 74 72 65 65 4d 6f 64 75 6c 65 2c  ", &rtreeModule,
36dda 20 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 72   c, 0);.  }..  r
36ddb 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
36ddc 20 21 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51   !SQLITE_CORE.SQ
36ddd 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
36dde 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
36ddf 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit(.  sqlite3 *
36de0 64 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45  db,.  char **pzE
36de1 72 72 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73  rrMsg,.  const s
36de2 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
36de3 6e 65 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53  nes *pApi.){.  S
36de4 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f  QLITE_EXTENSION_
36de5 49 4e 49 54 32 28 70 41 70 69 29 0a 20 20 72 65  INIT2(pApi).  re
36de6 74 75 72 6e 20 73 71 6c 69 74 65 33 52 74 72 65  turn sqlite3Rtre
36de7 65 49 6e 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e  eInit(db);.}.#en
36de8 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  dif..#endif../**
36de9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
36dea 20 6f 66 20 72 74 72 65 65 2e 63 20 2a 2a 2a 2a   of rtree.c ****
36deb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ded 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
36dee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
36def 69 6e 20 66 69 6c 65 20 69 63 75 2e 63 20 2a 2a  in file icu.c **
36df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36df1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36df2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
36df3 2a 2a 20 32 30 30 37 20 4d 61 79 20 36 0a 2a 2a  ** 2007 May 6.**
36df4 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
36df5 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
36df6 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
36df7 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
36df8 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
36df9 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
36dfa 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
36dfb 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
36dfc 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
36dfd 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
36dfe 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
36dff 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
36e00 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
36e01 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
36e02 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
36e03 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
36e04 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
36e05 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
36e06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
36e0a 24 49 64 3a 20 69 63 75 2e 63 2c 76 20 31 2e 37  $Id: icu.c,v 1.7
36e0b 20 32 30 30 37 2f 31 32 2f 31 33 20 32 31 3a 35   2007/12/13 21:5
36e0c 34 3a 31 31 20 64 72 68 20 45 78 70 20 24 0a 2a  4:11 drh Exp $.*
36e0d 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69  *.** This file i
36e0e 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 69 6e 74  mplements an int
36e0f 65 67 72 61 74 69 6f 6e 20 62 65 74 77 65 65 6e  egration between
36e10 20 74 68 65 20 49 43 55 20 6c 69 62 72 61 72 79   the ICU library
36e11 20 0a 2a 2a 20 28 22 49 6e 74 65 72 6e 61 74 69   .** ("Internati
36e12 6f 6e 61 6c 20 43 6f 6d 70 6f 6e 65 6e 74 73 20  onal Components 
36e13 66 6f 72 20 55 6e 69 63 6f 64 65 22 2c 20 61 6e  for Unicode", an
36e14 20 6f 70 65 6e 2d 73 6f 75 72 63 65 20 6c 69 62   open-source lib
36e15 72 61 72 79 20 0a 2a 2a 20 66 6f 72 20 68 61 6e  rary .** for han
36e16 64 6c 69 6e 67 20 75 6e 69 63 6f 64 65 20 64 61  dling unicode da
36e17 74 61 29 20 61 6e 64 20 53 51 4c 69 74 65 2e 20  ta) and SQLite. 
36e18 54 68 65 20 69 6e 74 65 67 72 61 74 69 6f 6e 20  The integration 
36e19 75 73 65 73 20 0a 2a 2a 20 49 43 55 20 74 6f 20  uses .** ICU to 
36e1a 70 72 6f 76 69 64 65 20 74 68 65 20 66 6f 6c 6c  provide the foll
36e1b 6f 77 69 6e 67 20 74 6f 20 53 51 4c 69 74 65 3a  owing to SQLite:
36e1c 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d  .**.**   * An im
36e1d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
36e1e 74 68 65 20 53 51 4c 20 72 65 67 65 78 70 28 29  the SQL regexp()
36e1f 20 66 75 6e 63 74 69 6f 6e 20 28 61 6e 64 20 68   function (and h
36e20 65 6e 63 65 20 52 45 47 45 58 50 0a 2a 2a 20 20  ence REGEXP.**  
36e21 20 20 20 6f 70 65 72 61 74 6f 72 29 20 75 73 69     operator) usi
36e22 6e 67 20 74 68 65 20 49 43 55 20 75 72 65 67 65  ng the ICU urege
36e23 78 5f 58 58 28 29 20 41 50 49 73 2e 0a 2a 2a 0a  x_XX() APIs..**.
36e24 2a 2a 20 20 20 2a 20 49 6d 70 6c 65 6d 65 6e 74  **   * Implement
36e25 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 53 51  ations of the SQ
36e26 4c 20 73 63 61 6c 61 72 20 75 70 70 65 72 28 29  L scalar upper()
36e27 20 61 6e 64 20 6c 6f 77 65 72 28 29 20 66 75 6e   and lower() fun
36e28 63 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 66 6f  ctions.**     fo
36e29 72 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2e 0a  r case mapping..
36e2a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6e 74 65 67 72  **.**   * Integr
36e2b 61 74 69 6f 6e 20 6f 66 20 49 43 55 20 61 6e 64  ation of ICU and
36e2c 20 53 51 4c 69 74 65 20 63 6f 6c 6c 61 74 69 6f   SQLite collatio
36e2d 6e 20 73 65 71 65 6e 63 65 73 2e 0a 2a 2a 0a 2a  n seqences..**.*
36e2e 2a 20 20 20 2a 20 41 6e 20 69 6d 70 6c 65 6d 65  *   * An impleme
36e2f 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c  ntation of the L
36e30 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 68 61  IKE operator tha
36e31 74 20 75 73 65 73 20 49 43 55 20 74 6f 20 0a 2a  t uses ICU to .*
36e32 2a 20 20 20 20 20 70 72 6f 76 69 64 65 20 63 61  *     provide ca
36e33 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 6d  se-independent m
36e34 61 74 63 68 69 6e 67 2e 0a 2a 2f 0a 0a 23 69 66  atching..*/..#if
36e35 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
36e36 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
36e37 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
36e38 49 43 55 29 0a 0a 2f 2a 20 49 6e 63 6c 75 64 65  ICU)../* Include
36e39 20 49 43 55 20 68 65 61 64 65 72 73 20 2a 2f 0a   ICU headers */.
36e3a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64  #include <unicod
36e3b 65 2f 75 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63  e/utypes.h>.#inc
36e3c 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 72  lude <unicode/ur
36e3d 65 67 65 78 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  egex.h>.#include
36e3e 20 3c 75 6e 69 63 6f 64 65 2f 75 73 74 72 69 6e   <unicode/ustrin
36e3f 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75  g.h>.#include <u
36e40 6e 69 63 6f 64 65 2f 75 63 6f 6c 2e 68 3e 0a 0a  nicode/ucol.h>..
36e41 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
36e42 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f 45 58  CORE.  SQLITE_EX
36e43 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65  TENSION_INIT1.#e
36e44 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  lse.#endif../*.*
36e45 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
36e46 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74   (in bytes) of t
36e47 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20  he pattern in a 
36e48 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20  LIKE or GLOB.** 
36e49 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66  operator..*/.#if
36e4a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
36e4b 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
36e4c 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  GTH.# define SQL
36e4d 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54  ITE_MAX_LIKE_PAT
36e4e 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30  TERN_LENGTH 5000
36e4f 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
36e50 56 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74  Version of sqlit
36e51 65 33 5f 66 72 65 65 28 29 20 74 68 61 74 20 69  e3_free() that i
36e52 73 20 61 6c 77 61 79 73 20 61 20 66 75 6e 63 74  s always a funct
36e53 69 6f 6e 2c 20 6e 65 76 65 72 20 61 20 6d 61 63  ion, never a mac
36e54 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ro..*/.static vo
36e55 69 64 20 78 46 72 65 65 28 76 6f 69 64 20 2a 70  id xFree(void *p
36e56 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ){.  sqlite3_fre
36e57 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  e(p);.}../*.** C
36e58 6f 6d 70 61 72 65 20 74 77 6f 20 55 54 46 2d 38  ompare two UTF-8
36e59 20 73 74 72 69 6e 67 73 20 66 6f 72 20 65 71 75   strings for equ
36e5a 61 6c 69 74 79 20 77 68 65 72 65 20 74 68 65 20  ality where the 
36e5b 66 69 72 73 74 20 73 74 72 69 6e 67 20 69 73 0a  first string is.
36e5c 2a 2a 20 61 20 22 4c 49 4b 45 22 20 65 78 70 72  ** a "LIKE" expr
36e5d 65 73 73 69 6f 6e 2e 20 52 65 74 75 72 6e 20 74  ession. Return t
36e5e 72 75 65 20 28 31 29 20 69 66 20 74 68 65 79 20  rue (1) if they 
36e5f 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64  are the same and
36e60 20 0a 2a 2a 20 66 61 6c 73 65 20 28 30 29 20 69   .** false (0) i
36e61 66 20 74 68 65 79 20 61 72 65 20 64 69 66 66 65  f they are diffe
36e62 72 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rent..*/.static 
36e63 69 6e 74 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61  int icuLikeCompa
36e64 72 65 28 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74  re(.  const uint
36e65 38 5f 74 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20  8_t *zPattern,  
36e66 20 2f 2a 20 4c 49 4b 45 20 70 61 74 74 65 72 6e   /* LIKE pattern
36e67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74   */.  const uint
36e68 38 5f 74 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20  8_t *zString,   
36e69 20 2f 2a 20 54 68 65 20 55 54 46 2d 38 20 73 74   /* The UTF-8 st
36e6a 72 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65 20  ring to compare 
36e6b 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 63 6f 6e  against */.  con
36e6c 73 74 20 55 43 68 61 72 33 32 20 75 45 73 63 20  st UChar32 uEsc 
36e6d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
36e6e 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20  scape character 
36e6f 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63  */.){.  static c
36e70 6f 6e 73 74 20 69 6e 74 20 4d 41 54 43 48 5f 4f  onst int MATCH_O
36e71 4e 45 20 3d 20 28 55 43 68 61 72 33 32 29 27 5f  NE = (UChar32)'_
36e72 27 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ';.  static cons
36e73 74 20 69 6e 74 20 4d 41 54 43 48 5f 41 4c 4c 20  t int MATCH_ALL 
36e74 3d 20 28 55 43 68 61 72 33 32 29 27 25 27 3b 0a  = (UChar32)'%';.
36e75 0a 20 20 69 6e 74 20 69 50 61 74 74 65 72 6e 20  .  int iPattern 
36e76 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 43 75  = 0;       /* Cu
36e77 72 72 65 6e 74 20 62 79 74 65 20 69 6e 64 65 78  rrent byte index
36e78 20 69 6e 20 7a 50 61 74 74 65 72 6e 20 2a 2f 0a   in zPattern */.
36e79 20 20 69 6e 74 20 69 53 74 72 69 6e 67 20 3d 20    int iString = 
36e7a 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  0;        /* Cur
36e7b 72 65 6e 74 20 62 79 74 65 20 69 6e 64 65 78 20  rent byte index 
36e7c 69 6e 20 7a 53 74 72 69 6e 67 20 2a 2f 0a 0a 20  in zString */.. 
36e7d 20 69 6e 74 20 70 72 65 76 45 73 63 61 70 65 20   int prevEscape 
36e7e 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  = 0;     /* True
36e7f 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
36e80 20 63 68 61 72 61 63 74 65 72 20 77 61 73 20 75   character was u
36e81 45 73 63 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28  Esc */..  while(
36e82 20 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65   zPattern[iPatte
36e83 72 6e 5d 21 3d 30 20 29 7b 0a 0a 20 20 20 20 2f  rn]!=0 ){..    /
36e84 2a 20 52 65 61 64 20 28 61 6e 64 20 63 6f 6e 73  * Read (and cons
36e85 75 6d 65 29 20 74 68 65 20 6e 65 78 74 20 63 68  ume) the next ch
36e86 61 72 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65  aracter from the
36e87 20 69 6e 70 75 74 20 70 61 74 74 65 72 6e 2e 20   input pattern. 
36e88 2a 2f 0a 20 20 20 20 55 43 68 61 72 33 32 20 75  */.    UChar32 u
36e89 50 61 74 74 65 72 6e 3b 0a 20 20 20 20 55 38 5f  Pattern;.    U8_
36e8a 4e 45 58 54 5f 55 4e 53 41 46 45 28 7a 50 61 74  NEXT_UNSAFE(zPat
36e8b 74 65 72 6e 2c 20 69 50 61 74 74 65 72 6e 2c 20  tern, iPattern, 
36e8c 75 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 61  uPattern);.    a
36e8d 73 73 65 72 74 28 75 50 61 74 74 65 72 6e 21 3d  ssert(uPattern!=
36e8e 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 72  0);..    /* Ther
36e8f 65 20 61 72 65 20 6e 6f 77 20 34 20 70 6f 73 73  e are now 4 poss
36e90 69 62 69 6c 69 74 69 65 73 3a 0a 20 20 20 20 2a  ibilities:.    *
36e91 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 31 2e 20  *.    **     1. 
36e92 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75  uPattern is an u
36e93 6e 65 73 63 61 70 65 64 20 6d 61 74 63 68 2d 61  nescaped match-a
36e94 6c 6c 20 63 68 61 72 61 63 74 65 72 20 22 25 22  ll character "%"
36e95 2c 0a 20 20 20 20 2a 2a 20 20 20 20 20 32 2e 20  ,.    **     2. 
36e96 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75  uPattern is an u
36e97 6e 65 73 63 61 70 65 64 20 6d 61 74 63 68 2d 6f  nescaped match-o
36e98 6e 65 20 63 68 61 72 61 63 74 65 72 20 22 5f 22  ne character "_"
36e99 2c 0a 20 20 20 20 2a 2a 20 20 20 20 20 33 2e 20  ,.    **     3. 
36e9a 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75  uPattern is an u
36e9b 6e 65 73 63 61 70 65 64 20 65 73 63 61 70 65 20  nescaped escape 
36e9c 63 68 61 72 61 63 74 65 72 2c 20 6f 72 0a 20 20  character, or.  
36e9d 20 20 2a 2a 20 20 20 20 20 34 2e 20 75 50 61 74    **     4. uPat
36e9e 74 65 72 6e 20 69 73 20 74 6f 20 62 65 20 68 61  tern is to be ha
36e9f 6e 64 6c 65 64 20 61 73 20 61 6e 20 6f 72 64 69  ndled as an ordi
36ea0 6e 61 72 79 20 63 68 61 72 61 63 74 65 72 0a 20  nary character. 
36ea1 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70     */.    if( !p
36ea2 72 65 76 45 73 63 61 70 65 20 26 26 20 75 50 61  revEscape && uPa
36ea3 74 74 65 72 6e 3d 3d 4d 41 54 43 48 5f 41 4c 4c  ttern==MATCH_ALL
36ea4 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73   ){.      /* Cas
36ea5 65 20 31 2e 20 2a 2f 0a 20 20 20 20 20 20 75 69  e 1. */.      ui
36ea6 6e 74 38 5f 74 20 63 3b 0a 0a 20 20 20 20 20 20  nt8_t c;..      
36ea7 2f 2a 20 53 6b 69 70 20 61 6e 79 20 4d 41 54 43  /* Skip any MATC
36ea8 48 5f 41 4c 4c 20 6f 72 20 4d 41 54 43 48 5f 4f  H_ALL or MATCH_O
36ea9 4e 45 20 63 68 61 72 61 63 74 65 72 73 20 74 68  NE characters th
36eaa 61 74 20 66 6f 6c 6c 6f 77 20 61 0a 20 20 20 20  at follow a.    
36eab 20 20 2a 2a 20 4d 41 54 43 48 5f 41 4c 4c 2e 20    ** MATCH_ALL. 
36eac 46 6f 72 20 65 61 63 68 20 4d 41 54 43 48 5f 4f  For each MATCH_O
36ead 4e 45 2c 20 73 6b 69 70 20 6f 6e 65 20 63 68 61  NE, skip one cha
36eae 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 0a 20  racter in the . 
36eaf 20 20 20 20 20 2a 2a 20 74 65 73 74 20 73 74 72       ** test str
36eb0 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ing..      */.  
36eb1 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 50      while( (c=zP
36eb2 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d  attern[iPattern]
36eb3 29 20 3d 3d 20 4d 41 54 43 48 5f 41 4c 4c 20 7c  ) == MATCH_ALL |
36eb4 7c 20 63 20 3d 3d 20 4d 41 54 43 48 5f 4f 4e 45  | c == MATCH_ONE
36eb5 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
36eb6 63 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a  c==MATCH_ONE ){.
36eb7 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 53            if( zS
36eb8 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d  tring[iString]==
36eb9 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
36eba 20 20 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31          U8_FWD_1
36ebb 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c  _UNSAFE(zString,
36ebc 20 69 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20   iString);.     
36ebd 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 50 61     }.        iPa
36ebe 74 74 65 72 6e 2b 2b 3b 0a 20 20 20 20 20 20 7d  ttern++;.      }
36ebf 0a 0a 20 20 20 20 20 20 69 66 28 20 7a 50 61 74  ..      if( zPat
36ec0 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 3d 3d  tern[iPattern]==
36ec1 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20  0 ) return 1;.. 
36ec2 20 20 20 20 20 77 68 69 6c 65 28 20 7a 53 74 72       while( zStr
36ec3 69 6e 67 5b 69 53 74 72 69 6e 67 5d 20 29 7b 0a  ing[iString] ){.
36ec4 20 20 20 20 20 20 20 20 69 66 28 20 69 63 75 4c          if( icuL
36ec5 69 6b 65 43 6f 6d 70 61 72 65 28 26 7a 50 61 74  ikeCompare(&zPat
36ec6 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 2c 20  tern[iPattern], 
36ec7 26 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67  &zString[iString
36ec8 5d 2c 20 75 45 73 63 29 20 29 7b 0a 20 20 20 20  ], uEsc) ){.    
36ec9 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
36eca 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
36ecb 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46    U8_FWD_1_UNSAF
36ecc 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69  E(zString, iStri
36ecd 6e 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ng);.      }.   
36ece 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20     return 0;..  
36ecf 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65    }else if( !pre
36ed0 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74  vEscape && uPatt
36ed1 65 72 6e 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29  ern==MATCH_ONE )
36ed2 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  {.      /* Case 
36ed3 32 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  2. */.      if( 
36ed4 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d  zString[iString]
36ed5 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
36ed6 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55        U8_FWD_1_U
36ed7 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69  NSAFE(zString, i
36ed8 53 74 72 69 6e 67 29 3b 0a 0a 20 20 20 20 7d 65  String);..    }e
36ed9 6c 73 65 20 69 66 28 20 21 70 72 65 76 45 73 63  lse if( !prevEsc
36eda 61 70 65 20 26 26 20 75 50 61 74 74 65 72 6e 3d  ape && uPattern=
36edb 3d 75 45 73 63 29 7b 0a 20 20 20 20 20 20 2f 2a  =uEsc){.      /*
36edc 20 43 61 73 65 20 33 2e 20 2a 2f 0a 20 20 20 20   Case 3. */.    
36edd 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 31    prevEscape = 1
36ede 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ;..    }else{.  
36edf 20 20 20 20 2f 2a 20 43 61 73 65 20 34 2e 20 2a      /* Case 4. *
36ee0 2f 0a 20 20 20 20 20 20 55 43 68 61 72 33 32 20  /.      UChar32 
36ee1 75 53 74 72 69 6e 67 3b 0a 20 20 20 20 20 20 55  uString;.      U
36ee2 38 5f 4e 45 58 54 5f 55 4e 53 41 46 45 28 7a 53  8_NEXT_UNSAFE(zS
36ee3 74 72 69 6e 67 2c 20 69 53 74 72 69 6e 67 2c 20  tring, iString, 
36ee4 75 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20  uString);.      
36ee5 75 53 74 72 69 6e 67 20 3d 20 75 5f 66 6f 6c 64  uString = u_fold
36ee6 43 61 73 65 28 75 53 74 72 69 6e 67 2c 20 55 5f  Case(uString, U_
36ee7 46 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c  FOLD_CASE_DEFAUL
36ee8 54 29 3b 0a 20 20 20 20 20 20 75 50 61 74 74 65  T);.      uPatte
36ee9 72 6e 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28  rn = u_foldCase(
36eea 75 50 61 74 74 65 72 6e 2c 20 55 5f 46 4f 4c 44  uPattern, U_FOLD
36eeb 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 29 3b 0a  _CASE_DEFAULT);.
36eec 20 20 20 20 20 20 69 66 28 20 75 53 74 72 69 6e        if( uStrin
36eed 67 21 3d 75 50 61 74 74 65 72 6e 20 29 7b 0a 20  g!=uPattern ){. 
36eee 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
36eef 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
36ef0 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b 0a 20  revEscape = 0;. 
36ef1 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
36ef2 72 6e 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69  rn zString[iStri
36ef3 6e 67 5d 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ng]==0;.}../*.**
36ef4 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
36ef5 6f 66 20 74 68 65 20 6c 69 6b 65 28 29 20 53 51  of the like() SQ
36ef6 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69  L function.  Thi
36ef7 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65  s function imple
36ef8 6d 65 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75 69  ments.** the bui
36ef9 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72 61  ld-in LIKE opera
36efa 74 6f 72 2e 20 20 54 68 65 20 66 69 72 73 74 20  tor.  The first 
36efb 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
36efc 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 0a  function is the.
36efd 2a 2a 20 70 61 74 74 65 72 6e 20 61 6e 64 20 74  ** pattern and t
36efe 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
36eff 6e 74 20 69 73 20 74 68 65 20 73 74 72 69 6e 67  nt is the string
36f00 2e 20 20 53 6f 2c 20 74 68 65 20 53 51 4c 20 73  .  So, the SQL s
36f01 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a  tatements:.**.**
36f02 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42 0a         A LIKE B.
36f03 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65  **.** is impleme
36f04 6e 74 65 64 20 61 73 20 6c 69 6b 65 28 42 2c 20  nted as like(B, 
36f05 41 29 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  A). If there is 
36f06 61 6e 20 65 73 63 61 70 65 20 63 68 61 72 61 63  an escape charac
36f07 74 65 72 20 45 2c 20 0a 2a 2a 0a 2a 2a 20 20 20  ter E, .**.**   
36f08 20 20 20 20 41 20 4c 49 4b 45 20 42 20 45 53 43      A LIKE B ESC
36f09 41 50 45 20 45 0a 2a 2a 0a 2a 2a 20 69 73 20 6d  APE E.**.** is m
36f0a 61 70 70 65 64 20 74 6f 20 6c 69 6b 65 28 42 2c  apped to like(B,
36f0b 20 41 2c 20 45 29 2e 0a 2a 2f 0a 73 74 61 74 69   A, E)..*/.stati
36f0c 63 20 76 6f 69 64 20 69 63 75 4c 69 6b 65 46 75  c void icuLikeFu
36f0d 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
36f0e 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
36f0f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20  .  int argc, .  
36f10 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
36f11 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
36f12 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
36f13 41 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  A = sqlite3_valu
36f14 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
36f15 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
36f16 64 20 63 68 61 72 20 2a 7a 42 20 3d 20 73 71 6c  d char *zB = sql
36f17 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
36f18 61 72 67 76 5b 31 5d 29 3b 0a 20 20 55 43 68 61  argv[1]);.  UCha
36f19 72 33 32 20 75 45 73 63 20 3d 20 30 3b 0a 0a 20  r32 uEsc = 0;.. 
36f1a 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c 65   /* Limit the le
36f1b 6e 67 74 68 20 6f 66 20 74 68 65 20 4c 49 4b 45  ngth of the LIKE
36f1c 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e   or GLOB pattern
36f1d 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65   to avoid proble
36f1e 6d 73 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70 20  ms.  ** of deep 
36f1f 72 65 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a  recursion and N*
36f20 4e 20 62 65 68 61 76 69 6f 72 20 69 6e 20 70 61  N behavior in pa
36f21 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a  tternCompare()..
36f22 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
36f23 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
36f24 72 67 76 5b 30 5d 29 3e 53 51 4c 49 54 45 5f 4d  rgv[0])>SQLITE_M
36f25 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  AX_LIKE_PATTERN_
36f26 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71  LENGTH ){.    sq
36f27 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
36f28 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b  or(context, "LIK
36f29 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72  E or GLOB patter
36f2a 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20  n too complex", 
36f2b 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  -1);.    return;
36f2c 0a 20 20 7d 0a 0a 0a 20 20 69 66 28 20 61 72 67  .  }...  if( arg
36f2d 63 3d 3d 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54  c==3 ){.    /* T
36f2e 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63  he escape charac
36f2f 74 65 72 20 73 74 72 69 6e 67 20 6d 75 73 74 20  ter string must 
36f30 63 6f 6e 73 69 73 74 20 6f 66 20 61 20 73 69 6e  consist of a sin
36f31 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61 63  gle UTF-8 charac
36f32 74 65 72 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65  ter..    ** Othe
36f33 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e  rwise, return an
36f34 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20   error..    */. 
36f35 20 20 20 69 6e 74 20 6e 45 3d 20 73 71 6c 69 74     int nE= sqlit
36f36 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
36f37 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 63 6f 6e  rgv[2]);.    con
36f38 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
36f39 20 2a 7a 45 20 3d 20 73 71 6c 69 74 65 33 5f 76   *zE = sqlite3_v
36f3a 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32  alue_text(argv[2
36f3b 5d 29 3b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20  ]);.    int i = 
36f3c 30 3b 0a 20 20 20 20 69 66 28 20 7a 45 3d 3d 30  0;.    if( zE==0
36f3d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 55   ) return;.    U
36f3e 38 5f 4e 45 58 54 28 7a 45 2c 20 69 2c 20 6e 45  8_NEXT(zE, i, nE
36f3f 2c 20 75 45 73 63 29 3b 0a 20 20 20 20 69 66 28  , uEsc);.    if(
36f40 20 69 21 3d 6e 45 29 7b 0a 20 20 20 20 20 20 73   i!=nE){.      s
36f41 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
36f42 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20  ror(context, .  
36f43 20 20 20 20 20 20 20 20 22 45 53 43 41 50 45 20          "ESCAPE 
36f44 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20  expression must 
36f45 62 65 20 61 20 73 69 6e 67 6c 65 20 63 68 61 72  be a single char
36f46 61 63 74 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20  acter", -1);.   
36f47 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
36f48 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 41 20 26  .  }..  if( zA &
36f49 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 71 6c 69  & zB ){.    sqli
36f4a 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
36f4b 6f 6e 74 65 78 74 2c 20 69 63 75 4c 69 6b 65 43  ontext, icuLikeC
36f4c 6f 6d 70 61 72 65 28 7a 41 2c 20 7a 42 2c 20 75  ompare(zA, zB, u
36f4d 45 73 63 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  Esc));.  }.}../*
36f4e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
36f4f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
36f50 20 61 6e 20 49 43 55 20 66 75 6e 63 74 69 6f 6e   an ICU function
36f51 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
36f52 68 69 6e 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65  hin.** the imple
36f53 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20  mentation of an 
36f54 53 51 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74  SQL scalar funct
36f55 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 65  ion returns an e
36f56 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rror..**.** The 
36f57 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
36f58 63 6f 6e 74 65 78 74 20 70 61 73 73 65 64 20 61  context passed a
36f59 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
36f5a 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 6c 6f 61 64  ment is .** load
36f5b 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ed with an error
36f5c 20 6d 65 73 73 61 67 65 20 62 61 73 65 64 20 6f   message based o
36f5d 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
36f5e 74 77 6f 20 61 72 67 73 2e 0a 2a 2f 0a 73 74 61  two args..*/.sta
36f5f 74 69 63 20 76 6f 69 64 20 69 63 75 46 75 6e 63  tic void icuFunc
36f60 74 69 6f 6e 45 72 72 6f 72 28 0a 20 20 73 71 6c  tionError(.  sql
36f61 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
36f62 74 78 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  tx,       /* SQL
36f63 69 74 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74  ite scalar funct
36f64 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ion context */. 
36f65 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
36f66 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  me,           /*
36f67 20 4e 61 6d 65 20 6f 66 20 49 43 55 20 66 75 6e   Name of ICU fun
36f68 63 74 69 6f 6e 20 74 68 61 74 20 66 61 69 6c 65  ction that faile
36f69 64 20 2a 2f 0a 20 20 55 45 72 72 6f 72 43 6f 64  d */.  UErrorCod
36f6a 65 20 65 20 20 20 20 20 20 20 20 20 20 20 20 20  e e             
36f6b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64      /* Error cod
36f6c 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 49 43  e returned by IC
36f6d 55 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  U function */.){
36f6e 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 32 38  .  char zBuf[128
36f6f 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  ];.  sqlite3_snp
36f70 72 69 6e 74 66 28 31 32 38 2c 20 7a 42 75 66 2c  rintf(128, zBuf,
36f71 20 22 49 43 55 20 65 72 72 6f 72 3a 20 25 73 28   "ICU error: %s(
36f72 29 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 2c 20 75  ): %s", zName, u
36f73 5f 65 72 72 6f 72 4e 61 6d 65 28 65 29 29 3b 0a  _errorName(e));.
36f74 20 20 7a 42 75 66 5b 31 32 37 5d 20 3d 20 27 5c    zBuf[127] = '\
36f75 30 27 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  0';.  sqlite3_re
36f76 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 74 78 2c  sult_error(pCtx,
36f77 20 7a 42 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f   zBuf, -1);.}../
36f78 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f  *.** Function to
36f79 20 64 65 6c 65 74 65 20 63 6f 6d 70 69 6c 65 64   delete compiled
36f7a 20 72 65 67 65 78 70 20 6f 62 6a 65 63 74 73 2e   regexp objects.
36f7b 20 52 65 67 69 73 74 65 72 65 64 20 61 73 0a 2a   Registered as.*
36f7c 2a 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 66  * a destructor f
36f7d 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 71 6c  unction with sql
36f7e 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
36f7f 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
36f80 69 64 20 69 63 75 52 65 67 65 78 70 44 65 6c 65  id icuRegexpDele
36f81 74 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 55  te(void *p){.  U
36f82 52 65 67 75 6c 61 72 45 78 70 72 65 73 73 69 6f  RegularExpressio
36f83 6e 20 2a 70 45 78 70 72 20 3d 20 28 55 52 65 67  n *pExpr = (UReg
36f84 75 6c 61 72 45 78 70 72 65 73 73 69 6f 6e 20 2a  ularExpression *
36f85 29 70 3b 0a 20 20 75 72 65 67 65 78 5f 63 6c 6f  )p;.  uregex_clo
36f86 73 65 28 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a  se(pExpr);.}../*
36f87 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
36f88 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 52 45 47  on of SQLite REG
36f89 45 58 50 20 6f 70 65 72 61 74 6f 72 2e 20 54 68  EXP operator. Th
36f8a 69 73 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69  is scalar functi
36f8b 6f 6e 20 74 61 6b 65 73 0a 2a 2a 20 74 77 6f 20  on takes.** two 
36f8c 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 66  arguments. The f
36f8d 69 72 73 74 20 69 73 20 61 20 72 65 67 75 6c 61  irst is a regula
36f8e 72 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 74  r expression pat
36f8f 74 65 72 6e 20 74 6f 20 63 6f 6d 70 69 6c 65 0a  tern to compile.
36f90 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73  ** the second is
36f91 20 61 20 73 74 72 69 6e 67 20 74 6f 20 6d 61 74   a string to mat
36f92 63 68 20 61 67 61 69 6e 73 74 20 74 68 61 74 20  ch against that 
36f93 70 61 74 74 65 72 6e 2e 20 49 66 20 65 69 74 68  pattern. If eith
36f94 65 72 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  er .** argument 
36f95 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2c 20  is an SQL NULL, 
36f96 74 68 65 6e 20 4e 55 4c 4c 20 49 73 20 72 65 74  then NULL Is ret
36f97 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
36f98 2c 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  , the result.** 
36f99 69 73 20 31 20 69 66 20 74 68 65 20 73 74 72 69  is 1 if the stri
36f9a 6e 67 20 6d 61 74 63 68 65 73 20 74 68 65 20 70  ng matches the p
36f9b 61 74 74 65 72 6e 2c 20 6f 72 20 30 20 6f 74 68  attern, or 0 oth
36f9c 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 51  erwise..**.** SQ
36f9d 4c 69 74 65 20 6d 61 70 73 20 74 68 65 20 72 65  Lite maps the re
36f9e 67 65 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20  gexp() function 
36f9f 74 6f 20 74 68 65 20 72 65 67 65 78 70 28 29 20  to the regexp() 
36fa0 6f 70 65 72 61 74 6f 72 20 73 75 63 68 0a 2a 2a  operator such.**
36fa1 20 74 68 61 74 20 74 68 65 20 66 6f 6c 6c 6f 77   that the follow
36fa2 69 6e 67 20 74 77 6f 20 61 72 65 20 65 71 75 69  ing two are equi
36fa3 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  valent:.**.**   
36fa4 20 20 7a 53 74 72 69 6e 67 20 52 45 47 45 58 50    zString REGEXP
36fa5 20 7a 50 61 74 74 65 72 6e 0a 2a 2a 20 20 20 20   zPattern.**    
36fa6 20 72 65 67 65 78 70 28 7a 50 61 74 74 65 72 6e   regexp(zPattern
36fa7 2c 20 7a 53 74 72 69 6e 67 29 0a 2a 2a 0a 2a 2a  , zString).**.**
36fa8 20 55 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77   Uses the follow
36fa9 69 6e 67 20 49 43 55 20 72 65 67 65 78 70 20 41  ing ICU regexp A
36faa 50 49 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75  PIs:.**.**     u
36fab 72 65 67 65 78 5f 6f 70 65 6e 28 29 0a 2a 2a 20  regex_open().** 
36fac 20 20 20 20 75 72 65 67 65 78 5f 6d 61 74 63 68      uregex_match
36fad 65 73 28 29 0a 2a 2a 20 20 20 20 20 75 72 65 67  es().**     ureg
36fae 65 78 5f 63 6c 6f 73 65 28 29 0a 2a 2f 0a 73 74  ex_close().*/.st
36faf 61 74 69 63 20 76 6f 69 64 20 69 63 75 52 65 67  atic void icuReg
36fb0 65 78 70 46 75 6e 63 28 73 71 6c 69 74 65 33 5f  expFunc(sqlite3_
36fb1 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 20  context *p, int 
36fb2 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61  nArg, sqlite3_va
36fb3 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20  lue **apArg){.  
36fb4 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75  UErrorCode statu
36fb5 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52  s = U_ZERO_ERROR
36fb6 3b 0a 20 20 55 52 65 67 75 6c 61 72 45 78 70 72  ;.  URegularExpr
36fb7 65 73 73 69 6f 6e 20 2a 70 45 78 70 72 3b 0a 20  ession *pExpr;. 
36fb8 20 55 42 6f 6f 6c 20 72 65 73 3b 0a 20 20 63 6f   UBool res;.  co
36fb9 6e 73 74 20 55 43 68 61 72 20 2a 7a 53 74 72 69  nst UChar *zStri
36fba 6e 67 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ng = sqlite3_val
36fbb 75 65 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b  ue_text16(apArg[
36fbc 31 5d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  1]);..  /* If th
36fbd 65 20 6c 65 66 74 20 68 61 6e 64 20 73 69 64 65  e left hand side
36fbe 20 6f 66 20 74 68 65 20 72 65 67 65 78 70 20 6f   of the regexp o
36fbf 70 65 72 61 74 6f 72 20 69 73 20 4e 55 4c 4c 2c  perator is NULL,
36fc0 20 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20   .  ** then the 
36fc1 72 65 73 75 6c 74 20 69 73 20 61 6c 73 6f 20 4e  result is also N
36fc2 55 4c 4c 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  ULL. .  */.  if(
36fc3 20 21 7a 53 74 72 69 6e 67 20 29 7b 0a 20 20 20   !zString ){.   
36fc4 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
36fc5 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 5f  pExpr = sqlite3_
36fc6 67 65 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30  get_auxdata(p, 0
36fc7 29 3b 0a 20 20 69 66 28 20 21 70 45 78 70 72 20  );.  if( !pExpr 
36fc8 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 55 43 68  ){.    const UCh
36fc9 61 72 20 2a 7a 50 61 74 74 65 72 6e 20 3d 20 73  ar *zPattern = s
36fca 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
36fcb 74 31 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20  t16(apArg[0]);. 
36fcc 20 20 20 69 66 28 20 21 7a 50 61 74 74 65 72 6e     if( !zPattern
36fcd 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
36fce 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70  ;.    }.    pExp
36fcf 72 20 3d 20 75 72 65 67 65 78 5f 6f 70 65 6e 28  r = uregex_open(
36fd0 7a 50 61 74 74 65 72 6e 2c 20 2d 31 2c 20 30 2c  zPattern, -1, 0,
36fd1 20 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 0a 20   0, &status);.. 
36fd2 20 20 20 69 66 28 20 55 5f 53 55 43 43 45 53 53     if( U_SUCCESS
36fd3 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20  (status) ){.    
36fd4 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75    sqlite3_set_au
36fd5 78 64 61 74 61 28 70 2c 20 30 2c 20 70 45 78 70  xdata(p, 0, pExp
36fd6 72 2c 20 69 63 75 52 65 67 65 78 70 44 65 6c 65  r, icuRegexpDele
36fd7 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  te);.    }else{.
36fd8 20 20 20 20 20 20 61 73 73 65 72 74 28 21 70 45        assert(!pE
36fd9 78 70 72 29 3b 0a 20 20 20 20 20 20 69 63 75 46  xpr);.      icuF
36fda 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20  unctionError(p, 
36fdb 22 75 72 65 67 65 78 5f 6f 70 65 6e 22 2c 20 73  "uregex_open", s
36fdc 74 61 74 75 73 29 3b 0a 20 20 20 20 20 20 72 65  tatus);.      re
36fdd 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
36fde 0a 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20  .  /* Configure 
36fdf 74 68 65 20 74 65 78 74 20 74 68 61 74 20 74 68  the text that th
36fe0 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73  e regular expres
36fe1 73 69 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f 6e  sion operates on
36fe2 2e 20 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73 65  . */.  uregex_se
36fe3 74 54 65 78 74 28 70 45 78 70 72 2c 20 7a 53 74  tText(pExpr, zSt
36fe4 72 69 6e 67 2c 20 2d 31 2c 20 26 73 74 61 74 75  ring, -1, &statu
36fe5 73 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43  s);.  if( !U_SUC
36fe6 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
36fe7 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45      icuFunctionE
36fe8 72 72 6f 72 28 70 2c 20 22 75 72 65 67 65 78 5f  rror(p, "uregex_
36fe9 73 65 74 54 65 78 74 22 2c 20 73 74 61 74 75 73  setText", status
36fea 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
36feb 20 7d 0a 0a 20 20 2f 2a 20 41 74 74 65 6d 70 74   }..  /* Attempt
36fec 20 74 68 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20   the match */.  
36fed 72 65 73 20 3d 20 75 72 65 67 65 78 5f 6d 61 74  res = uregex_mat
36fee 63 68 65 73 28 70 45 78 70 72 2c 20 30 2c 20 26  ches(pExpr, 0, &
36fef 73 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21  status);.  if( !
36ff0 55 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73  U_SUCCESS(status
36ff1 29 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63  ) ){.    icuFunc
36ff2 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72  tionError(p, "ur
36ff3 65 67 65 78 5f 6d 61 74 63 68 65 73 22 2c 20 73  egex_matches", s
36ff4 74 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75  tatus);.    retu
36ff5 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  rn;.  }..  /* Se
36ff6 74 20 74 68 65 20 74 65 78 74 20 74 68 61 74 20  t the text that 
36ff7 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72  the regular expr
36ff8 65 73 73 69 6f 6e 20 6f 70 65 72 61 74 65 73 20  ession operates 
36ff9 6f 6e 20 74 6f 20 61 20 4e 55 4c 4c 0a 20 20 2a  on to a NULL.  *
36ffa 2a 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20  * pointer. This 
36ffb 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6e 65  is not really ne
36ffc 63 65 73 73 61 72 79 2c 20 62 75 74 20 69 74 20  cessary, but it 
36ffd 69 73 20 74 69 64 69 65 72 20 74 68 61 6e 20 0a  is tidier than .
36ffe 20 20 2a 2a 20 6c 65 61 76 69 6e 67 20 74 68 65    ** leaving the
36fff 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73   regular express
37000 69 6f 6e 20 6f 62 6a 65 63 74 20 63 6f 6e 66 69  ion object confi
37001 67 75 72 65 64 20 77 69 74 68 20 61 6e 20 69 6e  gured with an in
37002 76 61 6c 69 64 0a 20 20 2a 2a 20 70 6f 69 6e 74  valid.  ** point
37003 65 72 20 61 66 74 65 72 20 74 68 69 73 20 66 75  er after this fu
37004 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a  nction returns..
37005 20 20 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73 65    */.  uregex_se
37006 74 54 65 78 74 28 70 45 78 70 72 2c 20 30 2c 20  tText(pExpr, 0, 
37007 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 0a 20 20  0, &status);..  
37008 2f 2a 20 52 65 74 75 72 6e 20 31 20 6f 72 20 30  /* Return 1 or 0
37009 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72  . */.  sqlite3_r
3700a 65 73 75 6c 74 5f 69 6e 74 28 70 2c 20 72 65 73  esult_int(p, res
3700b 20 3f 20 31 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a   ? 1 : 0);.}../*
3700c 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
3700d 6f 6e 73 20 6f 66 20 73 63 61 6c 61 72 20 66 75  ons of scalar fu
3700e 6e 63 74 69 6f 6e 73 20 66 6f 72 20 63 61 73 65  nctions for case
3700f 20 6d 61 70 70 69 6e 67 20 2d 20 75 70 70 65 72   mapping - upper
37010 28 29 20 61 6e 64 20 0a 2a 2a 20 6c 6f 77 65 72  () and .** lower
37011 28 29 2e 20 46 75 6e 63 74 69 6f 6e 20 75 70 70  (). Function upp
37012 65 72 28 29 20 63 6f 6e 76 65 72 74 73 20 69 74  er() converts it
37013 73 20 69 6e 70 75 74 20 74 6f 20 75 70 70 65 72  s input to upper
37014 2d 63 61 73 65 20 28 41 42 43 29 2e 0a 2a 2a 20  -case (ABC)..** 
37015 46 75 6e 63 74 69 6f 6e 20 6c 6f 77 65 72 28 29  Function lower()
37016 20 63 6f 6e 76 65 72 74 73 20 74 6f 20 6c 6f 77   converts to low
37017 65 72 2d 63 61 73 65 20 28 61 62 63 29 2e 0a 2a  er-case (abc)..*
37018 2a 0a 2a 2a 20 49 43 55 20 70 72 6f 76 69 64 65  *.** ICU provide
37019 73 20 74 77 6f 20 74 79 70 65 73 20 6f 66 20 63  s two types of c
3701a 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 22 67 65  ase mapping, "ge
3701b 6e 65 72 61 6c 22 20 63 61 73 65 20 6d 61 70 70  neral" case mapp
3701c 69 6e 67 20 61 6e 64 0a 2a 2a 20 22 6c 61 6e 67  ing and.** "lang
3701d 75 61 67 65 20 73 70 65 63 69 66 69 63 22 2e 20  uage specific". 
3701e 52 65 66 65 72 20 74 6f 20 49 43 55 20 64 6f 63  Refer to ICU doc
3701f 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74  umentation for t
37020 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 0a 2a  he differences.*
37021 2a 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77  * between the tw
37022 6f 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 74 69 6c  o..**.** To util
37023 69 73 65 20 22 67 65 6e 65 72 61 6c 22 20 63 61  ise "general" ca
37024 73 65 20 6d 61 70 70 69 6e 67 2c 20 74 68 65 20  se mapping, the 
37025 75 70 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72  upper() or lower
37026 28 29 20 73 63 61 6c 61 72 20 0a 2a 2a 20 66 75  () scalar .** fu
37027 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f  nctions are invo
37028 6b 65 64 20 77 69 74 68 20 6f 6e 65 20 61 72 67  ked with one arg
37029 75 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ument:.**.**    
3702a 20 75 70 70 65 72 28 27 41 42 43 27 29 20 2d 3e   upper('ABC') ->
3702b 20 27 61 62 63 27 0a 2a 2a 20 20 20 20 20 6c 6f   'abc'.**     lo
3702c 77 65 72 28 27 61 62 63 27 29 20 2d 3e 20 27 41  wer('abc') -> 'A
3702d 42 43 27 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 63 63  BC'.**.** To acc
3702e 65 73 73 20 49 43 55 20 22 6c 61 6e 67 75 61 67  ess ICU "languag
3702f 65 20 73 70 65 63 69 66 69 63 22 20 63 61 73 65  e specific" case
37030 20 6d 61 70 70 69 6e 67 2c 20 75 70 70 65 72 28   mapping, upper(
37031 29 20 6f 72 20 6c 6f 77 65 72 28 29 0a 2a 2a 20  ) or lower().** 
37032 73 68 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65  should be invoke
37033 64 20 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d  d with two argum
37034 65 6e 74 73 2e 20 54 68 65 20 73 65 63 6f 6e 64  ents. The second
37035 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
37036 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 74 68 65 20   name.** of the 
37037 6c 6f 63 61 6c 65 20 74 6f 20 75 73 65 2e 20 50  locale to use. P
37038 61 73 73 69 6e 67 20 61 6e 20 65 6d 70 74 79 20  assing an empty 
37039 73 74 72 69 6e 67 20 28 22 22 29 20 6f 72 20 53  string ("") or S
3703a 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 2a 2a  QL NULL value.**
3703b 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
3703c 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73  rgument is the s
3703d 61 6d 65 20 61 73 20 69 6e 76 6f 6b 69 6e 67 20  ame as invoking 
3703e 74 68 65 20 31 20 61 72 67 75 6d 65 6e 74 20 76  the 1 argument v
3703f 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 75 70 70  ersion.** of upp
37040 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29 2e  er() or lower().
37041 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72  .**.**     lower
37042 28 27 49 27 2c 20 27 65 6e 5f 75 73 27 29 20 2d  ('I', 'en_us') -
37043 3e 20 27 69 27 0a 2a 2a 20 20 20 20 20 6c 6f 77  > 'i'.**     low
37044 65 72 28 27 49 27 2c 20 27 74 72 5f 74 72 27 29  er('I', 'tr_tr')
37045 20 2d 3e 20 27 c4 b1 27 20 28 73 6d 61 6c 6c 20   -> '..' (small 
37046 64 6f 74 6c 65 73 73 20 69 29 0a 2a 2a 0a 2a 2a  dotless i).**.**
37047 20 68 74 74 70 3a 2f 2f 77 77 77 2e 69 63 75 2d   http://www.icu-
37048 70 72 6f 6a 65 63 74 2e 6f 72 67 2f 75 73 65 72  project.org/user
37049 67 75 69 64 65 2f 70 6f 73 69 78 2e 68 74 6d 6c  guide/posix.html
3704a 23 63 61 73 65 5f 6d 61 70 70 69 6e 67 73 0a 2a  #case_mappings.*
3704b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
3704c 75 43 61 73 65 46 75 6e 63 31 36 28 73 71 6c 69  uCaseFunc16(sqli
3704d 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20  te3_context *p, 
3704e 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3704f 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
37050 7b 0a 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20  {.  const UChar 
37051 2a 7a 49 6e 70 75 74 3b 0a 20 20 55 43 68 61 72  *zInput;.  UChar
37052 20 2a 7a 4f 75 74 70 75 74 3b 0a 20 20 69 6e 74   *zOutput;.  int
37053 20 6e 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e   nInput;.  int n
37054 4f 75 74 70 75 74 3b 0a 0a 20 20 55 45 72 72 6f  Output;..  UErro
37055 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55  rCode status = U
37056 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 63  _ZERO_ERROR;.  c
37057 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 61  onst char *zLoca
37058 6c 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  le = 0;..  asser
37059 74 28 6e 41 72 67 3d 3d 31 20 7c 7c 20 6e 41 72  t(nArg==1 || nAr
3705a 67 3d 3d 32 29 3b 0a 20 20 69 66 28 20 6e 41 72  g==2);.  if( nAr
3705b 67 3d 3d 32 20 29 7b 0a 20 20 20 20 7a 4c 6f 63  g==2 ){.    zLoc
3705c 61 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ale = (const cha
3705d 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
3705e 65 5f 74 65 78 74 28 61 70 41 72 67 5b 31 5d 29  e_text(apArg[1])
3705f 3b 0a 20 20 7d 0a 0a 20 20 7a 49 6e 70 75 74 20  ;.  }..  zInput 
37060 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
37061 74 65 78 74 31 36 28 61 70 41 72 67 5b 30 5d 29  text16(apArg[0])
37062 3b 0a 20 20 69 66 28 20 21 7a 49 6e 70 75 74 20  ;.  if( !zInput 
37063 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
37064 20 7d 0a 20 20 6e 49 6e 70 75 74 20 3d 20 73 71   }.  nInput = sq
37065 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
37066 73 31 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 0a  s16(apArg[0]);..
37067 20 20 6e 4f 75 74 70 75 74 20 3d 20 6e 49 6e 70    nOutput = nInp
37068 75 74 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7a 4f  ut * 2 + 2;.  zO
37069 75 74 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f  utput = sqlite3_
3706a 6d 61 6c 6c 6f 63 28 6e 4f 75 74 70 75 74 29 3b  malloc(nOutput);
3706b 0a 20 20 69 66 28 20 21 7a 4f 75 74 70 75 74 20  .  if( !zOutput 
3706c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
3706d 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65   }..  if( sqlite
3706e 33 5f 75 73 65 72 5f 64 61 74 61 28 70 29 20 29  3_user_data(p) )
3706f 7b 0a 20 20 20 20 75 5f 73 74 72 54 6f 55 70 70  {.    u_strToUpp
37070 65 72 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74  er(zOutput, nOut
37071 70 75 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e  put/2, zInput, n
37072 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65  Input/2, zLocale
37073 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 7d 65  , &status);.  }e
37074 6c 73 65 7b 0a 20 20 20 20 75 5f 73 74 72 54 6f  lse{.    u_strTo
37075 4c 6f 77 65 72 28 7a 4f 75 74 70 75 74 2c 20 6e  Lower(zOutput, n
37076 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75 74  Output/2, zInput
37077 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f 63  , nInput/2, zLoc
37078 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0a 20  ale, &status);. 
37079 20 7d 0a 0a 20 20 69 66 28 20 21 55 5f 53 55 43   }..  if( !U_SUC
3707a 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
3707b 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45      icuFunctionE
3707c 72 72 6f 72 28 70 2c 20 22 75 5f 73 74 72 54 6f  rror(p, "u_strTo
3707d 4c 6f 77 65 72 28 29 2f 75 5f 73 74 72 54 6f 55  Lower()/u_strToU
3707e 70 70 65 72 22 2c 20 73 74 61 74 75 73 29 3b 0a  pper", status);.
3707f 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
37080 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
37081 74 5f 74 65 78 74 31 36 28 70 2c 20 7a 4f 75 74  t_text16(p, zOut
37082 70 75 74 2c 20 2d 31 2c 20 78 46 72 65 65 29 3b  put, -1, xFree);
37083 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74  .}../*.** Collat
37084 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 64 65 73  ion sequence des
37085 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e  tructor function
37086 2e 20 54 68 65 20 70 43 74 78 20 61 72 67 75 6d  . The pCtx argum
37087 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a  ent points to.**
37088 20 61 20 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72   a UCollator str
37089 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c  ucture previousl
3708a 79 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e  y allocated usin
3708b 67 20 75 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a  g ucol_open()..*
3708c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
3708d 75 43 6f 6c 6c 61 74 69 6f 6e 44 65 6c 28 76 6f  uCollationDel(vo
3708e 69 64 20 2a 70 43 74 78 29 7b 0a 20 20 55 43 6f  id *pCtx){.  UCo
3708f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55 43 6f  llator *p = (UCo
37090 6c 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a 20  llator *)pCtx;. 
37091 20 75 63 6f 6c 5f 63 6c 6f 73 65 28 70 29 3b 0a   ucol_close(p);.
37092 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74 69  }../*.** Collati
37093 6f 6e 20 73 65 71 75 65 6e 63 65 20 63 6f 6d 70  on sequence comp
37094 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e  arison function.
37095 20 54 68 65 20 70 43 74 78 20 61 72 67 75 6d 65   The pCtx argume
37096 6e 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20  nt points to.** 
37097 61 20 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72 75  a UCollator stru
37098 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79  cture previously
37099 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67   allocated using
3709a 20 75 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f   ucol_open()..*/
3709b 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 43  .static int icuC
3709c 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c 28 0a 20 20  ollationColl(.  
3709d 76 6f 69 64 20 2a 70 43 74 78 2c 0a 20 20 69 6e  void *pCtx,.  in
3709e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74  t nLeft,.  const
3709f 20 76 6f 69 64 20 2a 7a 4c 65 66 74 2c 0a 20 20   void *zLeft,.  
370a0 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 63 6f  int nRight,.  co
370a1 6e 73 74 20 76 6f 69 64 20 2a 7a 52 69 67 68 74  nst void *zRight
370a2 0a 29 7b 0a 20 20 55 43 6f 6c 6c 61 74 69 6f 6e  .){.  UCollation
370a3 52 65 73 75 6c 74 20 72 65 73 3b 0a 20 20 55 43  Result res;.  UC
370a4 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55 43  ollator *p = (UC
370a5 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a  ollator *)pCtx;.
370a6 20 20 72 65 73 20 3d 20 75 63 6f 6c 5f 73 74 72    res = ucol_str
370a7 63 6f 6c 6c 28 70 2c 20 28 55 43 68 61 72 20 2a  coll(p, (UChar *
370a8 29 7a 4c 65 66 74 2c 20 6e 4c 65 66 74 2f 32 2c  )zLeft, nLeft/2,
370a9 20 28 55 43 68 61 72 20 2a 29 7a 52 69 67 68 74   (UChar *)zRight
370aa 2c 20 6e 52 69 67 68 74 2f 32 29 3b 0a 20 20 73  , nRight/2);.  s
370ab 77 69 74 63 68 28 20 72 65 73 20 29 7b 0a 20 20  witch( res ){.  
370ac 20 20 63 61 73 65 20 55 43 4f 4c 5f 4c 45 53 53    case UCOL_LESS
370ad 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a  :    return -1;.
370ae 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f 47 52      case UCOL_GR
370af 45 41 54 45 52 3a 20 72 65 74 75 72 6e 20 2b 31  EATER: return +1
370b0 3b 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f  ;.    case UCOL_
370b1 45 51 55 41 4c 3a 20 20 20 72 65 74 75 72 6e 20  EQUAL:   return 
370b2 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  0;.  }.  assert(
370b3 21 22 55 6e 65 78 70 65 63 74 65 64 20 72 65 74  !"Unexpected ret
370b4 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 75  urn value from u
370b5 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28 29 22 29 3b  col_strcoll()");
370b6 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
370b7 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
370b8 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61 6c  tion of the scal
370b9 61 72 20 66 75 6e 63 74 69 6f 6e 20 69 63 75 5f  ar function icu_
370ba 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  load_collation()
370bb 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 63 61  ..**.** This sca
370bc 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  lar function is 
370bd 75 73 65 64 20 74 6f 20 61 64 64 20 49 43 55 20  used to add ICU 
370be 63 6f 6c 6c 61 74 69 6f 6e 20 62 61 73 65 64 20  collation based 
370bf 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 74 79  collation .** ty
370c0 70 65 73 20 74 6f 20 61 6e 20 53 51 4c 69 74 65  pes to an SQLite
370c1 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
370c2 74 69 6f 6e 2e 20 49 74 20 69 73 20 69 6e 74 65  tion. It is inte
370c3 6e 64 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65  nded to be calle
370c4 64 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  d.** as follows:
370c5 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
370c6 54 20 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61  T icu_load_colla
370c7 74 69 6f 6e 28 3c 6c 6f 63 61 6c 65 3e 2c 20 3c  tion(<locale>, <
370c8 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 29  collation-name>)
370c9 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 3c 6c  ;.**.** Where <l
370ca 6f 63 61 6c 65 3e 20 69 73 20 61 20 73 74 72 69  ocale> is a stri
370cb 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e  ng containing an
370cc 20 49 43 55 20 6c 6f 63 61 6c 65 20 69 64 65 6e   ICU locale iden
370cd 74 69 66 69 65 72 20 28 69 2e 65 2e 0a 2a 2a 20  tifier (i.e..** 
370ce 22 65 6e 5f 41 55 22 2c 20 22 74 72 5f 54 52 22  "en_AU", "tr_TR"
370cf 20 65 74 63 2e 29 20 61 6e 64 20 3c 63 6f 6c 6c   etc.) and <coll
370d0 61 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74  ation-name> is t
370d1 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a  he name of the.*
370d2 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  * collation sequ
370d3 65 6e 63 65 20 74 6f 20 63 72 65 61 74 65 2e 0a  ence to create..
370d4 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
370d5 63 75 4c 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e 28  cuLoadCollation(
370d6 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
370d7 78 74 20 2a 70 2c 20 0a 20 20 69 6e 74 20 6e 41  xt *p, .  int nA
370d8 72 67 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76  rg, .  sqlite3_v
370d9 61 6c 75 65 20 2a 2a 61 70 41 72 67 0a 29 7b 0a  alue **apArg.){.
370da 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
370db 28 73 71 6c 69 74 65 33 20 2a 29 73 71 6c 69 74  (sqlite3 *)sqlit
370dc 65 33 5f 75 73 65 72 5f 64 61 74 61 28 70 29 3b  e3_user_data(p);
370dd 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74  .  UErrorCode st
370de 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52  atus = U_ZERO_ER
370df 52 4f 52 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ROR;.  const cha
370e0 72 20 2a 7a 4c 6f 63 61 6c 65 3b 20 20 20 20 20  r *zLocale;     
370e1 20 2f 2a 20 4c 6f 63 61 6c 65 20 69 64 65 6e 74   /* Locale ident
370e2 69 66 69 65 72 20 2d 20 28 65 67 2e 20 22 6a 70  ifier - (eg. "jp
370e3 5f 4a 50 22 29 20 2a 2f 0a 20 20 63 6f 6e 73 74  _JP") */.  const
370e4 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
370e5 20 20 20 20 20 2f 2a 20 53 51 4c 20 43 6f 6c 6c       /* SQL Coll
370e6 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e  ation sequence n
370e7 61 6d 65 20 28 65 67 2e 20 22 6a 61 70 61 6e 65  ame (eg. "japane
370e8 73 65 22 29 20 2a 2f 0a 20 20 55 43 6f 6c 6c 61  se") */.  UColla
370e9 74 6f 72 20 2a 70 55 43 6f 6c 6c 61 74 6f 72 3b  tor *pUCollator;
370ea 20 20 20 20 2f 2a 20 49 43 55 20 6c 69 62 72 61      /* ICU libra
370eb 72 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 6f 62 6a  ry collation obj
370ec 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ect */.  int rc;
370ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370ee 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
370ef 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63  e from sqlite3_c
370f0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
370f1 78 28 29 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  x() */..  assert
370f2 28 6e 41 72 67 3d 3d 32 29 3b 0a 20 20 7a 4c 6f  (nArg==2);.  zLo
370f3 63 61 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  cale = (const ch
370f4 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
370f5 75 65 5f 74 65 78 74 28 61 70 41 72 67 5b 30 5d  ue_text(apArg[0]
370f6 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f  );.  zName = (co
370f7 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
370f8 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70  e3_value_text(ap
370f9 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 69 66 28 20  Arg[1]);..  if( 
370fa 21 7a 4c 6f 63 61 6c 65 20 7c 7c 20 21 7a 4e 61  !zLocale || !zNa
370fb 6d 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  me ){.    return
370fc 3b 0a 20 20 7d 0a 0a 20 20 70 55 43 6f 6c 6c 61  ;.  }..  pUColla
370fd 74 6f 72 20 3d 20 75 63 6f 6c 5f 6f 70 65 6e 28  tor = ucol_open(
370fe 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73  zLocale, &status
370ff 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43  );.  if( !U_SUCC
37100 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20  ESS(status) ){. 
37101 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72     icuFunctionEr
37102 72 6f 72 28 70 2c 20 22 75 63 6f 6c 5f 6f 70 65  ror(p, "ucol_ope
37103 6e 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20  n", status);.   
37104 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
37105 73 73 65 72 74 28 70 29 3b 0a 0a 20 20 72 63 20  ssert(p);..  rc 
37106 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
37107 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 64 62  _collation_v2(db
37108 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f  , zName, SQLITE_
37109 55 54 46 31 36 2c 20 28 76 6f 69 64 20 2a 29 70  UTF16, (void *)p
3710a 55 43 6f 6c 6c 61 74 6f 72 2c 20 0a 20 20 20 20  UCollator, .    
3710b 20 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f    icuCollationCo
3710c 6c 6c 2c 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e  ll, icuCollation
3710d 44 65 6c 0a 20 20 29 3b 0a 20 20 69 66 28 20 72  Del.  );.  if( r
3710e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
3710f 20 20 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28 70      ucol_close(p
37110 55 43 6f 6c 6c 61 74 6f 72 29 3b 0a 20 20 20 20  UCollator);.    
37111 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
37112 72 72 6f 72 28 70 2c 20 22 45 72 72 6f 72 20 72  rror(p, "Error r
37113 65 67 69 73 74 65 72 69 6e 67 20 63 6f 6c 6c 61  egistering colla
37114 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 22 2c 20  tion function", 
37115 2d 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  -1);.  }.}../*.*
37116 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20 49  * Register the I
37117 43 55 20 65 78 74 65 6e 73 69 6f 6e 20 66 75 6e  CU extension fun
37118 63 74 69 6f 6e 73 20 77 69 74 68 20 64 61 74 61  ctions with data
37119 62 61 73 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49  base db..*/.SQLI
3711a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3711b 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 73 71  qlite3IcuInit(sq
3711c 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 74  lite3 *db){.  st
3711d 72 75 63 74 20 49 63 75 53 63 61 6c 61 72 20 7b  ruct IcuScalar {
3711e 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
3711f 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
37120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37121 2a 20 46 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  * Function name 
37122 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b  */.    int nArg;
37123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37124 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37125 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
37126 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 69  guments */.    i
37127 6e 74 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20  nt enc;         
37128 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37129 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69           /* Opti
3712a 6d 61 6c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  mal text encodin
3712b 67 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70  g */.    void *p
3712c 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20  Context;        
3712d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3712e 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 75 73     /* sqlite3_us
3712f 65 72 5f 64 61 74 61 28 29 20 63 6f 6e 74 65 78  er_data() contex
37130 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a  t */.    void (*
37131 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
37132 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
37133 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20 20  te3_value**);.  
37134 7d 20 73 63 61 6c 61 72 73 5b 5d 20 3d 20 7b 0a  } scalars[] = {.
37135 20 20 20 20 7b 22 72 65 67 65 78 70 22 2c 2d 31      {"regexp",-1
37136 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 20 20  , SQLITE_ANY,   
37137 20 20 20 20 20 20 20 30 2c 20 69 63 75 52 65 67         0, icuReg
37138 65 78 70 46 75 6e 63 7d 2c 0a 0a 20 20 20 20 7b  expFunc},..    {
37139 22 6c 6f 77 65 72 22 2c 20 20 31 2c 20 53 51 4c  "lower",  1, SQL
3713a 49 54 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20  ITE_UTF16,      
3713b 20 20 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63    0, icuCaseFunc
3713c 31 36 7d 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72  16},.    {"lower
3713d 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54  ",  2, SQLITE_UT
3713e 46 31 36 2c 20 20 20 20 20 20 20 20 30 2c 20 69  F16,        0, i
3713f 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20  cuCaseFunc16},. 
37140 20 20 20 7b 22 75 70 70 65 72 22 2c 20 20 31 2c     {"upper",  1,
37141 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28   SQLITE_UTF16, (
37142 76 6f 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65  void*)1, icuCase
37143 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75  Func16},.    {"u
37144 70 70 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54  pper",  2, SQLIT
37145 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64 2a 29  E_UTF16, (void*)
37146 31 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  1, icuCaseFunc16
37147 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22  },..    {"lower"
37148 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  ,  1, SQLITE_UTF
37149 38 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69 63  8,         0, ic
3714a 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20  uCaseFunc16},.  
3714b 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 32 2c 20    {"lower",  2, 
3714c 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
3714d 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65 46       0, icuCaseF
3714e 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70  unc16},.    {"up
3714f 70 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45  per",  1, SQLITE
37150 5f 55 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31  _UTF8,  (void*)1
37151 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d  , icuCaseFunc16}
37152 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c 20  ,.    {"upper", 
37153 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
37154 20 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75 43    (void*)1, icuC
37155 61 73 65 46 75 6e 63 31 36 7d 2c 0a 0a 20 20 20  aseFunc16},..   
37156 20 7b 22 6c 69 6b 65 22 2c 20 20 20 32 2c 20 53   {"like",   2, S
37157 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20  QLITE_UTF8,     
37158 20 20 20 20 30 2c 20 69 63 75 4c 69 6b 65 46 75      0, icuLikeFu
37159 6e 63 7d 2c 0a 20 20 20 20 7b 22 6c 69 6b 65 22  nc},.    {"like"
3715a 2c 20 20 20 33 2c 20 53 51 4c 49 54 45 5f 55 54  ,   3, SQLITE_UT
3715b 46 38 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69  F8,         0, i
3715c 63 75 4c 69 6b 65 46 75 6e 63 7d 2c 0a 0a 20 20  cuLikeFunc},..  
3715d 20 20 7b 22 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c    {"icu_load_col
3715e 6c 61 74 69 6f 6e 22 2c 20 20 32 2c 20 53 51 4c  lation",  2, SQL
3715f 49 54 45 5f 55 54 46 38 2c 20 28 76 6f 69 64 2a  ITE_UTF8, (void*
37160 29 64 62 2c 20 69 63 75 4c 6f 61 64 43 6f 6c 6c  )db, icuLoadColl
37161 61 74 69 6f 6e 7d 2c 0a 20 20 7d 3b 0a 0a 20 20  ation},.  };..  
37162 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
37163 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  OK;.  int i;..  
37164 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  for(i=0; rc==SQL
37165 49 54 45 5f 4f 4b 20 26 26 20 69 3c 28 73 69 7a  ITE_OK && i<(siz
37166 65 6f 66 28 73 63 61 6c 61 72 73 29 2f 73 69 7a  eof(scalars)/siz
37167 65 6f 66 28 73 74 72 75 63 74 20 49 63 75 53 63  eof(struct IcuSc
37168 61 6c 61 72 29 29 3b 20 69 2b 2b 29 7b 0a 20 20  alar)); i++){.  
37169 20 20 73 74 72 75 63 74 20 49 63 75 53 63 61 6c    struct IcuScal
3716a 61 72 20 2a 70 20 3d 20 26 73 63 61 6c 61 72 73  ar *p = &scalars
3716b 5b 69 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  [i];.    rc = sq
3716c 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
3716d 63 74 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 64  ction(.        d
3716e 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d 3e  b, p->zName, p->
3716f 6e 41 72 67 2c 20 70 2d 3e 65 6e 63 2c 20 70 2d  nArg, p->enc, p-
37170 3e 70 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 78 46  >pContext, p->xF
37171 75 6e 63 2c 20 30 2c 20 30 0a 20 20 20 20 29 3b  unc, 0, 0.    );
37172 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
37173 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54  c;.}..#if !SQLIT
37174 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50  E_CORE.SQLITE_AP
37175 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78  I int sqlite3_ex
37176 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20  tension_init(.  
37177 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
37178 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
37179 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
3717a 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70  _api_routines *p
3717b 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f  Api.){.  SQLITE_
3717c 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28  EXTENSION_INIT2(
3717d 70 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73  pApi).  return s
3717e 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 64 62  qlite3IcuInit(db
3717f 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  );.}.#endif..#en
37180 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
37181 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 69 63 75 2e  **** End of icu.
37182 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
37183 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37184 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37185 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
37186 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
37187 66 74 73 33 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a  fts3_icu.c *****
37188 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37189 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3718a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
3718b 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  June 22.**.** Th
3718c 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
3718d 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
3718e 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
3718f 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
37190 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
37191 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
37192 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
37193 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
37194 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
37195 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
37196 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
37197 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
37198 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
37199 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
3719a 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
3719b 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
3719c 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
3719d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3719e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3719f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
371a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
371a1 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
371a2 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
371a3 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 66   tokenizer for f
371a4 74 73 33 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ts3 based on the
371a5 20 49 43 55 20 6c 69 62 72 61 72 79 2e 0a 2a 2a   ICU library..**
371a6 20 0a 2a 2a 20 24 49 64 3a 20 66 74 73 33 5f 69   .** $Id: fts3_i
371a7 63 75 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f  cu.c,v 1.3 2008/
371a8 30 39 2f 30 31 20 31 38 3a 33 34 3a 32 30 20 64  09/01 18:34:20 d
371a9 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
371aa 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  .*/..#if !define
371ab 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
371ac 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
371ad 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 23 69  _ENABLE_FTS3).#i
371ae 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
371af 4c 45 5f 49 43 55 0a 0a 0a 23 69 6e 63 6c 75 64  LE_ICU...#includ
371b0 65 20 3c 75 6e 69 63 6f 64 65 2f 75 62 72 6b 2e  e <unicode/ubrk.
371b1 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  h>.#include <uni
371b2 63 6f 64 65 2f 75 74 66 31 36 2e 68 3e 0a 0a 74  code/utf16.h>..t
371b3 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 63  ypedef struct Ic
371b4 75 54 6f 6b 65 6e 69 7a 65 72 20 49 63 75 54 6f  uTokenizer IcuTo
371b5 6b 65 6e 69 7a 65 72 3b 0a 74 79 70 65 64 65 66  kenizer;.typedef
371b6 20 73 74 72 75 63 74 20 49 63 75 43 75 72 73 6f   struct IcuCurso
371b7 72 20 49 63 75 43 75 72 73 6f 72 3b 0a 0a 73 74  r IcuCursor;..st
371b8 72 75 63 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65  ruct IcuTokenize
371b9 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  r {.  sqlite3_to
371ba 6b 65 6e 69 7a 65 72 20 62 61 73 65 3b 0a 20 20  kenizer base;.  
371bb 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 0a 7d  char *zLocale;.}
371bc 3b 0a 0a 73 74 72 75 63 74 20 49 63 75 43 75 72  ;..struct IcuCur
371bd 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  sor {.  sqlite3_
371be 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
371bf 20 62 61 73 65 3b 0a 0a 20 20 55 42 72 65 61 6b   base;..  UBreak
371c0 49 74 65 72 61 74 6f 72 20 2a 70 49 74 65 72 3b  Iterator *pIter;
371c1 20 20 20 20 20 20 2f 2a 20 49 43 55 20 62 72 65        /* ICU bre
371c2 61 6b 2d 69 74 65 72 61 74 6f 72 20 6f 62 6a 65  ak-iterator obje
371c3 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61  ct */.  int nCha
371c4 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
371c5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
371c6 20 55 43 68 61 72 20 65 6c 65 6d 65 6e 74 73 20   UChar elements 
371c7 69 6e 20 70 49 6e 70 75 74 20 2a 2f 0a 20 20 55  in pInput */.  U
371c8 43 68 61 72 20 2a 61 43 68 61 72 3b 20 20 20 20  Char *aChar;    
371c9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
371ca 70 79 20 6f 66 20 69 6e 70 75 74 20 75 73 69 6e  py of input usin
371cb 67 20 75 74 66 2d 31 36 20 65 6e 63 6f 64 69 6e  g utf-16 encodin
371cc 67 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4f 66 66  g */.  int *aOff
371cd 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  set;            
371ce 20 20 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66     /* Offsets of
371cf 20 65 61 63 68 20 63 68 61 72 61 63 74 65 72 20   each character 
371d0 69 6e 20 75 74 66 2d 38 20 69 6e 70 75 74 20 2a  in utf-8 input *
371d1 2f 0a 0a 20 20 69 6e 74 20 6e 42 75 66 66 65 72  /..  int nBuffer
371d2 3b 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 66 65  ;.  char *zBuffe
371d3 72 3b 0a 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e  r;..  int iToken
371d4 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  ;.};../*.** Crea
371d5 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a  te a new tokeniz
371d6 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a  er instance..*/.
371d7 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 43 72  static int icuCr
371d8 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63  eate(.  int argc
371d9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
371da 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
371db 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
371dc 73 20 69 6e 20 61 72 67 76 5b 5d 20 2a 2f 0a 20  s in argv[] */. 
371dd 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f   const char * co
371de 6e 73 74 20 2a 61 72 67 76 2c 20 20 20 20 20 20  nst *argv,      
371df 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a        /* Tokeniz
371e0 65 72 20 63 72 65 61 74 69 6f 6e 20 61 72 67 75  er creation argu
371e1 6d 65 6e 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74  ments */.  sqlit
371e2 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70  e3_tokenizer **p
371e3 70 54 6f 6b 65 6e 69 7a 65 72 20 20 20 20 20 20  pTokenizer      
371e4 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20  /* OUT: Created 
371e5 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 29 7b 0a  tokenizer */.){.
371e6 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a    IcuTokenizer *
371e7 70 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a  p;.  int n = 0;.
371e8 0a 20 20 69 66 28 20 61 72 67 63 3e 30 20 29 7b  .  if( argc>0 ){
371e9 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28  .    n = strlen(
371ea 61 72 67 76 5b 30 5d 29 2b 31 3b 0a 20 20 7d 0a  argv[0])+1;.  }.
371eb 20 20 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69    p = (IcuTokeni
371ec 7a 65 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  zer *)sqlite3_ma
371ed 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 49 63 75 54  lloc(sizeof(IcuT
371ee 6f 6b 65 6e 69 7a 65 72 29 2b 6e 29 3b 0a 20 20  okenizer)+n);.  
371ef 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65  if( !p ){.    re
371f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
371f1 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  M;.  }.  memset(
371f2 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 49 63 75  p, 0, sizeof(Icu
371f3 54 6f 6b 65 6e 69 7a 65 72 29 29 3b 0a 0a 20 20  Tokenizer));..  
371f4 69 66 28 20 6e 20 29 7b 0a 20 20 20 20 70 2d 3e  if( n ){.    p->
371f5 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 68 61 72 20  zLocale = (char 
371f6 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d  *)&p[1];.    mem
371f7 63 70 79 28 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20  cpy(p->zLocale, 
371f8 61 72 67 76 5b 30 5d 2c 20 6e 29 3b 0a 20 20 7d  argv[0], n);.  }
371f9 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72  ..  *ppTokenizer
371fa 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65   = (sqlite3_toke
371fb 6e 69 7a 65 72 20 2a 29 70 3b 0a 0a 20 20 72 65  nizer *)p;..  re
371fc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
371fd 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79  }../*.** Destroy
371fe 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a   a tokenizer.*/.
371ff 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 44 65  static int icuDe
37200 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f  stroy(sqlite3_to
37201 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
37202 7a 65 72 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e  zer){.  IcuToken
37203 69 7a 65 72 20 2a 70 20 3d 20 28 49 63 75 54 6f  izer *p = (IcuTo
37204 6b 65 6e 69 7a 65 72 20 2a 29 70 54 6f 6b 65 6e  kenizer *)pToken
37205 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f  izer;.  sqlite3_
37206 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72  free(p);.  retur
37207 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
37208 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f  /*.** Prepare to
37209 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e   begin tokenizin
3720a 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73  g a particular s
3720b 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75  tring.  The inpu
3720c 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62  t.** string to b
3720d 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 70  e tokenized is p
3720e 49 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73 2d  Input[0..nBytes-
3720f 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a  1].  A cursor.**
37210 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65   used to increme
37211 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20  ntally tokenize 
37212 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 72  this string is r
37213 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a  eturned in .** *
37214 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61  ppCursor..*/.sta
37215 74 69 63 20 69 6e 74 20 69 63 75 4f 70 65 6e 28  tic int icuOpen(
37216 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
37217 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
37218 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
37219 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20   tokenizer */.  
3721a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70  const char *zInp
3721b 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ut,             
3721c 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20         /* Input 
3721d 73 74 72 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  string */.  int 
3721e 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20  nInput,         
3721f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37220 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
37221 7a 49 6e 70 75 74 20 69 6e 20 62 79 74 65 73 20  zInput in bytes 
37222 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
37223 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a  enizer_cursor **
37224 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f  ppCursor    /* O
37225 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e  UT: Tokenization
37226 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20   cursor */.){.  
37227 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 20  IcuTokenizer *p 
37228 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20  = (IcuTokenizer 
37229 2a 29 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20  *)pTokenizer;.  
3722a 49 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72 3b  IcuCursor *pCsr;
3722b 0a 0a 20 20 63 6f 6e 73 74 20 69 6e 74 33 32 5f  ..  const int32_
3722c 74 20 6f 70 74 20 3d 20 55 5f 46 4f 4c 44 5f 43  t opt = U_FOLD_C
3722d 41 53 45 5f 44 45 46 41 55 4c 54 3b 0a 20 20 55  ASE_DEFAULT;.  U
3722e 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73  ErrorCode status
3722f 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b   = U_ZERO_ERROR;
37230 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 0a 0a 20  .  int nChar;.. 
37231 20 55 43 68 61 72 33 32 20 63 3b 0a 20 20 69 6e   UChar32 c;.  in
37232 74 20 69 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20  t iInput = 0;.  
37233 69 6e 74 20 69 4f 75 74 20 3d 20 30 3b 0a 0a 20  int iOut = 0;.. 
37234 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a   *ppCursor = 0;.
37235 0a 20 20 69 66 28 20 6e 49 6e 70 75 74 3c 30 20  .  if( nInput<0 
37236 29 7b 0a 20 20 20 20 6e 49 6e 70 75 74 20 3d 20  ){.    nInput = 
37237 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a  strlen(zInput);.
37238 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20 6e 49    }.  nChar = nI
37239 6e 70 75 74 2b 31 3b 0a 20 20 70 43 73 72 20 3d  nput+1;.  pCsr =
3723a 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 73 71   (IcuCursor *)sq
3723b 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 0a 20 20  lite3_malloc(.  
3723c 20 20 20 20 73 69 7a 65 6f 66 28 49 63 75 43 75      sizeof(IcuCu
3723d 72 73 6f 72 29 20 2b 20 20 20 20 20 20 20 20 20  rsor) +         
3723e 20 20 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72         /* IcuCur
3723f 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 6e 43 68  sor */.      nCh
37240 61 72 20 2a 20 73 69 7a 65 6f 66 28 55 43 68 61  ar * sizeof(UCha
37241 72 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 20  r) +            
37242 2f 2a 20 49 63 75 43 75 72 73 6f 72 2e 61 43 68  /* IcuCursor.aCh
37243 61 72 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 28 6e  ar[] */.      (n
37244 43 68 61 72 2b 31 29 20 2a 20 73 69 7a 65 6f 66  Char+1) * sizeof
37245 28 69 6e 74 29 20 20 20 20 20 20 20 20 20 20 20  (int)           
37246 20 2f 2a 20 49 63 75 43 75 72 73 6f 72 2e 61 4f   /* IcuCursor.aO
37247 66 66 73 65 74 5b 5d 20 2a 2f 0a 20 20 29 3b 0a  ffset[] */.  );.
37248 20 20 69 66 28 20 21 70 43 73 72 20 29 7b 0a 20    if( !pCsr ){. 
37249 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3724a 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65  _NOMEM;.  }.  me
3724b 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20 73 69  mset(pCsr, 0, si
3724c 7a 65 6f 66 28 49 63 75 43 75 72 73 6f 72 29 29  zeof(IcuCursor))
3724d 3b 0a 20 20 70 43 73 72 2d 3e 61 43 68 61 72 20  ;.  pCsr->aChar 
3724e 3d 20 28 55 43 68 61 72 20 2a 29 26 70 43 73 72  = (UChar *)&pCsr
3724f 5b 31 5d 3b 0a 20 20 70 43 73 72 2d 3e 61 4f 66  [1];.  pCsr->aOf
37250 66 73 65 74 20 3d 20 28 69 6e 74 20 2a 29 26 70  fset = (int *)&p
37251 43 73 72 2d 3e 61 43 68 61 72 5b 6e 43 68 61 72  Csr->aChar[nChar
37252 5d 3b 0a 0a 20 20 70 43 73 72 2d 3e 61 4f 66 66  ];..  pCsr->aOff
37253 73 65 74 5b 69 4f 75 74 5d 20 3d 20 69 49 6e 70  set[iOut] = iInp
37254 75 74 3b 0a 20 20 55 38 5f 4e 45 58 54 28 7a 49  ut;.  U8_NEXT(zI
37255 6e 70 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49  nput, iInput, nI
37256 6e 70 75 74 2c 20 63 29 3b 20 0a 20 20 77 68 69  nput, c); .  whi
37257 6c 65 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 69  le( c>0 ){.    i
37258 6e 74 20 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a  nt isError = 0;.
37259 20 20 20 20 63 20 3d 20 75 5f 66 6f 6c 64 43 61      c = u_foldCa
3725a 73 65 28 63 2c 20 6f 70 74 29 3b 0a 20 20 20 20  se(c, opt);.    
3725b 55 31 36 5f 41 50 50 45 4e 44 28 70 43 73 72 2d  U16_APPEND(pCsr-
3725c 3e 61 43 68 61 72 2c 20 69 4f 75 74 2c 20 6e 43  >aChar, iOut, nC
3725d 68 61 72 2c 20 63 2c 20 69 73 45 72 72 6f 72 29  har, c, isError)
3725e 3b 0a 20 20 20 20 69 66 28 20 69 73 45 72 72 6f  ;.    if( isErro
3725f 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
37260 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20  e3_free(pCsr);. 
37261 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
37262 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
37263 20 20 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65      pCsr->aOffse
37264 74 5b 69 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74  t[iOut] = iInput
37265 3b 0a 0a 20 20 20 20 69 66 28 20 69 49 6e 70 75  ;..    if( iInpu
37266 74 3c 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20  t<nInput ){.    
37267 20 20 55 38 5f 4e 45 58 54 28 7a 49 6e 70 75 74    U8_NEXT(zInput
37268 2c 20 69 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74  , iInput, nInput
37269 2c 20 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , c);.    }else{
3726a 0a 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20  .      c = 0;.  
3726b 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 43 73 72 2d    }.  }..  pCsr-
3726c 3e 70 49 74 65 72 20 3d 20 75 62 72 6b 5f 6f 70  >pIter = ubrk_op
3726d 65 6e 28 55 42 52 4b 5f 57 4f 52 44 2c 20 70 2d  en(UBRK_WORD, p-
3726e 3e 7a 4c 6f 63 61 6c 65 2c 20 70 43 73 72 2d 3e  >zLocale, pCsr->
3726f 61 43 68 61 72 2c 20 69 4f 75 74 2c 20 26 73 74  aChar, iOut, &st
37270 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f  atus);.  if( !U_
37271 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20  SUCCESS(status) 
37272 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
37273 72 65 65 28 70 43 73 72 29 3b 0a 20 20 20 20 72  ree(pCsr);.    r
37274 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
37275 4f 52 3b 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e  OR;.  }.  pCsr->
37276 6e 43 68 61 72 20 3d 20 69 4f 75 74 3b 0a 0a 20  nChar = iOut;.. 
37277 20 75 62 72 6b 5f 66 69 72 73 74 28 70 43 73 72   ubrk_first(pCsr
37278 2d 3e 70 49 74 65 72 29 3b 0a 20 20 2a 70 70 43  ->pIter);.  *ppC
37279 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74 65 33  ursor = (sqlite3
3727a 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
3727b 72 20 2a 29 70 43 73 72 3b 0a 20 20 72 65 74 75  r *)pCsr;.  retu
3727c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
3727d 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74  ./*.** Close a t
3727e 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73  okenization curs
3727f 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70  or previously op
37280 65 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  ened by a call t
37281 6f 20 69 63 75 4f 70 65 6e 28 29 2e 0a 2a 2f 0a  o icuOpen()..*/.
37282 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 43 6c  static int icuCl
37283 6f 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ose(sqlite3_toke
37284 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
37285 75 72 73 6f 72 29 7b 0a 20 20 49 63 75 43 75 72  ursor){.  IcuCur
37286 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 49 63 75  sor *pCsr = (Icu
37287 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72  Cursor *)pCursor
37288 3b 0a 20 20 75 62 72 6b 5f 63 6c 6f 73 65 28 70  ;.  ubrk_close(p
37289 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 73  Csr->pIter);.  s
3728a 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
3728b 2d 3e 7a 42 75 66 66 65 72 29 3b 0a 20 20 73 71  ->zBuffer);.  sq
3728c 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 29  lite3_free(pCsr)
3728d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
3728e 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  E_OK;.}../*.** E
3728f 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74 20  xtract the next 
37290 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b  token from a tok
37291 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
37292 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
37293 69 63 75 4e 65 78 74 28 0a 20 20 73 71 6c 69 74  icuNext(.  sqlit
37294 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
37295 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f  sor *pCursor,  /
37296 2a 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65  * Cursor returne
37297 64 20 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20  d by simpleOpen 
37298 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
37299 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20  **ppToken,      
3729a 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3729b 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20 74 68 65   *ppToken is the
3729c 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20   token text */. 
3729d 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20   int *pnBytes,  
3729e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3729f 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d       /* OUT: Num
372a0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
372a1 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
372a2 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20  piStartOffset,  
372a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
372a4 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20  * OUT: Starting 
372a5 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20  offset of token 
372a6 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f  */.  int *piEndO
372a7 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
372a8 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
372a9 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f   Ending offset o
372aa 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  f token */.  int
372ab 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20   *piPosition    
372ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372ad 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f   /* OUT: Positio
372ae 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b  n integer of tok
372af 65 6e 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 43 75  en */.){.  IcuCu
372b0 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 49 63  rsor *pCsr = (Ic
372b1 75 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f  uCursor *)pCurso
372b2 72 3b 0a 0a 20 20 69 6e 74 20 69 53 74 61 72 74  r;..  int iStart
372b3 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 45 6e 64   = 0;.  int iEnd
372b4 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 42 79 74   = 0;.  int nByt
372b5 65 20 3d 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28  e = 0;..  while(
372b6 20 69 53 74 61 72 74 3d 3d 69 45 6e 64 20 29 7b   iStart==iEnd ){
372b7 0a 20 20 20 20 55 43 68 61 72 33 32 20 63 3b 0a  .    UChar32 c;.
372b8 0a 20 20 20 20 69 53 74 61 72 74 20 3d 20 75 62  .    iStart = ub
372b9 72 6b 5f 63 75 72 72 65 6e 74 28 70 43 73 72 2d  rk_current(pCsr-
372ba 3e 70 49 74 65 72 29 3b 0a 20 20 20 20 69 45 6e  >pIter);.    iEn
372bb 64 20 3d 20 75 62 72 6b 5f 6e 65 78 74 28 70 43  d = ubrk_next(pC
372bc 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 20 20  sr->pIter);.    
372bd 69 66 28 20 69 45 6e 64 3d 3d 55 42 52 4b 5f 44  if( iEnd==UBRK_D
372be 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ONE ){.      ret
372bf 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
372c0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c  .    }..    whil
372c1 65 28 20 69 53 74 61 72 74 3c 69 45 6e 64 20 29  e( iStart<iEnd )
372c2 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 57 68 69  {.      int iWhi
372c3 74 65 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 20  te = iStart;.   
372c4 20 20 20 55 38 5f 4e 45 58 54 28 70 43 73 72 2d     U8_NEXT(pCsr-
372c5 3e 61 43 68 61 72 2c 20 69 57 68 69 74 65 2c 20  >aChar, iWhite, 
372c6 70 43 73 72 2d 3e 6e 43 68 61 72 2c 20 63 29 3b  pCsr->nChar, c);
372c7 0a 20 20 20 20 20 20 69 66 28 20 75 5f 69 73 73  .      if( u_iss
372c8 70 61 63 65 28 63 29 20 29 7b 0a 20 20 20 20 20  pace(c) ){.     
372c9 20 20 20 69 53 74 61 72 74 20 3d 20 69 57 68 69     iStart = iWhi
372ca 74 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  te;.      }else{
372cb 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
372cc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
372cd 20 20 61 73 73 65 72 74 28 69 53 74 61 72 74 3c    assert(iStart<
372ce 3d 69 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 64  =iEnd);.  }..  d
372cf 6f 20 7b 0a 20 20 20 20 55 45 72 72 6f 72 43 6f  o {.    UErrorCo
372d0 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45  de status = U_ZE
372d1 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 20 20 69 66  RO_ERROR;.    if
372d2 28 20 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20  ( nByte ){.     
372d3 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71   char *zNew = sq
372d4 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 43  lite3_realloc(pC
372d5 73 72 2d 3e 7a 42 75 66 66 65 72 2c 20 6e 42 79  sr->zBuffer, nBy
372d6 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  te);.      if( !
372d7 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  zNew ){.        
372d8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
372d9 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
372da 20 20 20 70 43 73 72 2d 3e 7a 42 75 66 66 65 72     pCsr->zBuffer
372db 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 70   = zNew;.      p
372dc 43 73 72 2d 3e 6e 42 75 66 66 65 72 20 3d 20 6e  Csr->nBuffer = n
372dd 42 79 74 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  Byte;.    }..   
372de 20 75 5f 73 74 72 54 6f 55 54 46 38 28 0a 20 20   u_strToUTF8(.  
372df 20 20 20 20 20 20 70 43 73 72 2d 3e 7a 42 75 66        pCsr->zBuf
372e0 66 65 72 2c 20 70 43 73 72 2d 3e 6e 42 75 66 66  fer, pCsr->nBuff
372e1 65 72 2c 20 26 6e 42 79 74 65 2c 20 20 20 20 2f  er, &nByte,    /
372e2 2a 20 4f 75 74 70 75 74 20 76 61 72 73 20 2a 2f  * Output vars */
372e3 0a 20 20 20 20 20 20 20 20 26 70 43 73 72 2d 3e  .        &pCsr->
372e4 61 43 68 61 72 5b 69 53 74 61 72 74 5d 2c 20 69  aChar[iStart], i
372e5 45 6e 64 2d 69 53 74 61 72 74 2c 20 20 20 20 20  End-iStart,     
372e6 20 20 2f 2a 20 49 6e 70 75 74 20 76 61 72 73 20    /* Input vars 
372e7 2a 2f 0a 20 20 20 20 20 20 20 20 26 73 74 61 74  */.        &stat
372e8 75 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  us              
372e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372ea 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 73 75      /* Output su
372eb 63 63 65 73 73 2f 66 61 69 6c 75 72 65 20 2a 2f  ccess/failure */
372ec 0a 20 20 20 20 29 3b 0a 20 20 7d 20 77 68 69 6c  .    );.  } whil
372ed 65 28 20 6e 42 79 74 65 3e 70 43 73 72 2d 3e 6e  e( nByte>pCsr->n
372ee 42 75 66 66 65 72 20 29 3b 0a 0a 20 20 2a 70 70  Buffer );..  *pp
372ef 54 6f 6b 65 6e 20 3d 20 70 43 73 72 2d 3e 7a 42  Token = pCsr->zB
372f0 75 66 66 65 72 3b 0a 20 20 2a 70 6e 42 79 74 65  uffer;.  *pnByte
372f1 73 20 3d 20 6e 42 79 74 65 3b 0a 20 20 2a 70 69  s = nByte;.  *pi
372f2 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 70 43  StartOffset = pC
372f3 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 53 74 61  sr->aOffset[iSta
372f4 72 74 5d 3b 0a 20 20 2a 70 69 45 6e 64 4f 66 66  rt];.  *piEndOff
372f5 73 65 74 20 3d 20 70 43 73 72 2d 3e 61 4f 66 66  set = pCsr->aOff
372f6 73 65 74 5b 69 45 6e 64 5d 3b 0a 20 20 2a 70 69  set[iEnd];.  *pi
372f7 50 6f 73 69 74 69 6f 6e 20 3d 20 70 43 73 72 2d  Position = pCsr-
372f8 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 72 65  >iToken++;..  re
372f9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
372fa 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74  }../*.** The set
372fb 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68 61   of routines tha
372fc 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  t implement the 
372fd 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72  simple tokenizer
372fe 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
372ff 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
37300 65 72 5f 6d 6f 64 75 6c 65 20 69 63 75 54 6f 6b  er_module icuTok
37301 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b  enizerModule = {
37302 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
37303 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37304 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
37305 20 69 63 75 43 72 65 61 74 65 2c 20 20 20 20 20   icuCreate,     
37306 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
37307 20 78 43 72 65 61 74 65 20 20 2a 2f 0a 20 20 69   xCreate  */.  i
37308 63 75 44 65 73 74 72 6f 79 2c 20 20 20 20 20 20  cuDestroy,      
37309 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3730a 43 72 65 61 74 65 20 20 2a 2f 0a 20 20 69 63 75  Create  */.  icu
3730b 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
3730c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70            /* xOp
3730d 65 6e 20 20 20 20 2a 2f 0a 20 20 69 63 75 43 6c  en    */.  icuCl
3730e 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ose,            
3730f 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
37310 65 20 20 20 2a 2f 0a 20 20 69 63 75 4e 65 78 74  e   */.  icuNext
37311 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
37312 20 20 20 20 20 20 2f 2a 20 78 4e 65 78 74 20 20        /* xNext  
37313 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53    */.};../*.** S
37314 65 74 20 2a 70 70 4d 6f 64 75 6c 65 20 74 6f 20  et *ppModule to 
37315 70 6f 69 6e 74 20 61 74 20 74 68 65 20 69 6d 70  point at the imp
37316 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
37317 68 65 20 49 43 55 20 74 6f 6b 65 6e 69 7a 65 72  he ICU tokenizer
37318 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
37319 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
3731a 46 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72  Fts3IcuTokenizer
3731b 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  Module(.  sqlite
3731c 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
3731d 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75  le const**ppModu
3731e 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c  le.){.  *ppModul
3731f 65 20 3d 20 26 69 63 75 54 6f 6b 65 6e 69 7a 65  e = &icuTokenize
37320 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64  rModule;.}..#end
37321 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51  if /* defined(SQ
37322 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 29  LITE_ENABLE_ICU)
37323 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64   */.#endif /* !d
37324 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
37325 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
37326 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
37327 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  3) */../********
37328 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
37329 73 33 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a  s3_icu.c *******
3732a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3732b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3732c 2a 2a 2a 2a 2a 2f 0a                             *****/.